definition example

is the Category whose objects are sets and whose morphisms are relations ; composition is

and the identity on is (Kittenlab Lecture 14, 7 Sketches Example 5.8). Restricted to it is a Prop with monoidal product (“no interaction”, 7S Exercise 5.10).

Sources: Kittenlab Lecture 14 (“Relations”, “Category of relations”), 15 (relations as spans); 7 Sketches Example 5.8, Definition 5.79 (), Theorem 5.87, §2.6 (relations form a noncommutative quantale), Exercise 5.10; DaoFP §17.1 (profunctors as proof-relevant relations); CTfS Definition 3.3.3.8, Exercises 3.3.3.10–3.3.3.12, 5.3.3.5–5.3.3.7

  • Composition is matrix multiplication of Boolean matrices (“replace any with sum and && with *”); is the category of -profunctors between discrete -categories, and -matrices (Category of Profunctors).
  • , , is a faithful Prop functor (Example 5.12); a relation is a Span (Kittenlab Lecture 15), and spans compose by Pullback.
  • is compact closed with every object self-dual (both under and ); (relations over a rig, with , 7S Exercise 5.80) is compact closed with cup and cap (Theorem 5.87), and contains via behaviours (Graphical Linear Algebra).
  • Relations on a fixed set form a noncommutative Quantale under composition (7 Sketches §2.6). In a Topos, relations are subobjects of products and dagger structure is transposition .
  • Relations are the Kleisli arrows of the power set monad (CTfS Exercise 5.3.3.5): a function is the same as a relation , and Kleisli composition (apply, then take the union) is relational composition; so (Power Set Monad). In the disjoint union is both the product and the coproduct of and (CTfS Exercises 5.3.3.6–5.3.3.7): e.g. for , both are .
  • Relations vs. graphs (CTfS Exercises 3.3.3.10–3.3.3.12): a relation is a graph with vertices and arrows (source and target the two projections); a graph gives a relation by taking the image of . Relation → graph → relation is the identity, graph → relation → graph collapses parallel arrows. E.g. on draws a tetrahedron with a loop at every vertex, while "" is not transitive and "" is not reflexive — neither is a preorder.
  • Kittenlab’s behavioural view: a relation is a joint constraint; a mathematical model “selects a subset of a universum of possibilities” (Willems).

In compilers and databases

is the archetypal Cartesian Bicategory: its order is query containment and program refinement, its maps (left adjoints) are the functions, and its internal logic is regular logic, the logic of conjunctive queries.

Docs: FinRelations — Kittenlab Lecture 14, Lecture 15

# Kittenlab Lecture 14
const BoolRel = BitMatrix                      # R[i, j] = "i is related to j"
relcompose(R::BoolRel, S::BoolRel) =
  BoolRel([any(R[i, j] && S[j, k] for j in axes(R, 2)) for i in axes(R, 1), k in axes(S, 2)])
idrel(n) = BoolRel([i == j for i in 1:n, j in 1:n])
graph_of(f::Vector{Int}, n) = BoolRel([f[i] == j for i in eachindex(f), j in 1:n])   # FinSet → Rel
relcompose(graph_of([2, 3, 3], 3), graph_of([1, 1, 2], 2)) == graph_of([1, 2, 2], 2)  # functorial: true

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

# Catlab (checked with v0.16): the category FinRel
using Catlab, Catlab.CategoricalAlgebra.FinRelations
R = FinRelation((x, y) -> x < y, 3, 3); S = compose(R, R); S(1, 3)   # true: 1 < 2 < 3
#check CategoryTheory.RelCat          -- the category of types and relations
#check @Rel.comp
example (α : Type) : Rel α α := fun a b => a = b     -- identity relation
type Rel a b = a -> b -> Bool
compRel :: [b] -> Rel a b -> Rel b c -> Rel a c
compRel ys r s x z = any (\y -> r x y && s y z) ys
idRel :: Eq a => Rel a a
idRel = (==)
plusRel :: Rel a b -> Rel c d -> Rel (Either a c) (Either b d)   -- monoidal product in the prop Rel
plusRel r _ (Left a)  (Left b)  = r a b
plusRel _ s (Right c) (Right d) = s c d
plusRel _ _ _ _ = False