definition example program

Let be a Set. An equivalence relation on is a binary Relation, written with infix , such that for all :

(a) — reflexivity; (b) iff — symmetry; (c) if and then — transitivity.

Sources: 7 Sketches Definition 1.18, Remark 1.31, Example 1.72; Kittenlab Lecture 9; CTfS Definition 2.6.1.1, Examples 2.6.1.2–2.6.1.4, Definition 2.6.1.7, Exercises 2.6.1.3–2.6.1.10, 3.2.1.15

Relation to other notions

Examples from Category Theory for Scientists

  • iff for some (congruence mod 7) — reflexive with , symmetric with , transitive with (CTfS Example 2.6.1.2). ” spends a lot of time thinking about ” is none of reflexive, symmetric, transitive (CTfS Exercise 2.6.1.3).
  • Kernels: for any function , "" is an equivalence relation, and every equivalence relation arises this way (take the quotient map ) — partitions, fibers and equivalence relations are three faces of one idea (CTfS Exercise 2.6.1.5). “Being isomorphic” on a set of sets, and “being in the same orbit” of a Group Action, are equivalence relations (CTfS Exercise 3.2.1.15).
  • Generated equivalence relations (CTfS Definition 2.6.1.7): the smallest equivalence relation containing . Drawn in the plane , reflexivity is the diagonal, symmetry is mirror symmetry in the diagonal; the relation generated by is the diagonal plus the nine points of . On , generates everything — a single class. For a network, the relation “joined by an edge” generates “in the same connected component”, and the quotient is the set of components (CTfS Exercise 2.6.1.10). The Pushout of is for the equivalence relation generated by (CTfS Exercise 2.6.2.7).

Union-find (Kittenlab Lecture 9)

For an equivalence relation on generated by declaring pairs equal, the efficient data structure is a union-find storing a forest of representatives. Two elements are equivalent iff they have the same root. This is exactly what is needed to compute pushouts in .

In compilers and databases

An equivalence relation that is compatible with the operations of an algebra is a Congruence; only congruences can be quotiented by while keeping the algebra, and only congruences license substitution of equals for equals inside programs (E-Graph, Contextual Equivalence).

Docs: Kittenlab Lecture 9

# Kittenlab Lecture 9
struct UnionFind
  parent::Vector{Int}
  UnionFind(n::Int) = new(Vector{Int}(1:n))
end
 
function find_root(uf::UnionFind, i::Int)
  p = uf.parent[i]
  p == i ? i : find_root(uf, p)
end
 
function unite!(uf::UnionFind, i::Int, j::Int)
  iroot, jroot = find_root(uf, i), find_root(uf, j)
  uf.parent[jroot] = iroot
end
 
equivalent(uf, i, j) = find_root(uf, i) == find_root(uf, j)
 
# Catlab uses DataStructures.IntDisjointSets for the same purpose in colimits
using DataStructures
s = IntDisjointSets(5); union!(s, 1, 2); in_same_set(s, 1, 2)  # true
-- Mathlib: `Setoid α` bundles an equivalence relation
example (α : Type) (r : Setoid α) (a : α) : r.r a a := r.iseqv.refl a
#check @Equivalence   -- structure with refl, symm, trans
-- equivalence relations on α ↔ partitions of α
#check @Setoid.partition_iff_setoid
-- an equivalence relation as a predicate satisfying the three laws
type Equiv a = a -> a -> Bool
 
-- e.g. congruence modulo n on Int
modEq :: Int -> Equiv Int
modEq n a b = (a - b) `mod` n == 0
 
-- laws (not enforced): modEq n a a; modEq n a b == modEq n b a;
--   modEq n a b && modEq n b c ==> modEq n a c
-- Data.Equivalence.STT / union-find packages provide the Kittenlab structure