definition example theorem program

Let be a Quantale. A -matrix with rows and columns is a function ; is the -entry. The product of and is ,

joins standing in for and for in the usual formula. The identity matrix if and otherwise.

Sources: 7 Sketches §2.5.3, Definition 2.100, Example 2.102, Exercises 2.103–2.105; Kittenlab Lecture 14 (relation composition “looks suspiciously like matrix multiplication”); Chapter 4 (profunctor composition is exactly this).

Example 2.102 (, , ):

Identity matrices (7S Exercise 2.103): in , in , in .

Laws (7S Exercise 2.104): and , using and distributivity of over joins (Proposition 2.87). Hence sets and -matrices form a category — -, the category of -profunctors between discrete -categories, and for the Category of Relations.

Computing presented -categories

For a Weighted Graph with matrix (unit on the diagonal, weight on edges, elsewhere), the power records the best path using edges; for finite vertex sets the powers stabilize, and the stable power is the hom-matrix of the -category presented by (in the infinite case, take ). For this is the min-plus (tropical) computation of shortest-path distances: (§2.5.3); for the graph , (7S Exercise 2.105). For it is the Reflexive Transitive Closure (Warshall’s algorithm).

Docs: FinRelations — Kittenlab Lecture 14

Builds on: Bool (Monoidal Preorder) (BoolPre), Cost (CostPre) — run those notes’ Julia code first.

# generic quantale matrix multiplication
function qmul(V, M, N)
  [join(V, [otimes(V, M[i, k], N[k, j]) for k in axes(M, 2)]) for i in axes(M, 1), j in axes(N, 2)]
end
qid(V, n) = [i == j ? munit(V) : join(V, []) for i in 1:n, j in 1:n]
 
# Example 2.102 in Bool
join(::BoolPre, xs) = reduce(|, xs; init = false)
M = Bool[0 0; 0 1; 1 1]; N = Bool[1 1 0; 1 0 1]
qmul(BoolPre(), M, N)         # [0 0 0; 1 0 1; 1 1 1]
 
# Cost: shortest paths by powers
MY = [0.0 4 3; 3 0 Inf; Inf 4 0]
qmul(CostPre(), MY, MY)       # [0 4 3; 3 0 6; 7 4 0] = d_Y

Catlab version (run in a fresh Julia session — Catlab exports its own compose, id, FinFunction, …):

# Catlab: Bool-matrices are FinRelations, composed by `compose`
using Catlab.CategoricalAlgebra.FinRelations
-- Mathlib matrices over a semiring; (Bool, ∨, ∧) and the tropical semiring (min, +) are semirings
#check Matrix.mul
#check Tropical                    -- Tropical (WithTop ℝ): + is min, * is +
example : Semiring (Tropical (WithTop ℝ)) := inferInstance
-- Bool-matrix multiplication = relation composition:
example : Semiring Bool := inferInstance    -- ∨ as +, ∧ as *
-- matrix multiplication over a quantale
qmul :: Quantale v => [[v]] -> [[v]] -> [[v]]
qmul m n = [ [ joinAll (zipWith (<>) row col) | col <- cols ] | row <- m ]
  where cols = foldr (zipWith (:)) (repeat []) n
 
qid :: Quantale v => Int -> [[v]]
qid k = [ [ if i == j then mempty else joinAll [] | j <- [1..k] ] | i <- [1..k] ]
 
-- powers stabilize at the hom-matrix of the presented V-category
closure :: (Quantale v, Eq v) => [[v]] -> [[v]]
closure m = go m where go d = let d' = qmul d m in if d' == d then d else go d'