module Cat.Instances.Free where
Graphs and free categories🔗
A graph (really, an 1) is given by a set of vertices and, for each pair of elements a set of edges from to That’s it: a set and a family of sets over Really, for our purposes, graphs by themselves are not very interesting: their utility comes in constructing new categories.
record Graph o ℓ : Type (lsuc o ⊔ lsuc ℓ) where
field
: Set o
vert : ∣ vert ∣ → ∣ vert ∣ → Set ℓ edge
Given a graph we construct a strict category in the following manner:
- The objects of are the vertices of
- The morphisms in are given by finite paths in Finite paths are defined by the following indexed-inductive type
data Path-in : ∣ G.vert ∣ → ∣ G.vert ∣ → Type (o ⊔ ℓ) where
: ∀ {a} → Path-in a a
nil : ∀ {a b c} → ∣ G.edge a b ∣ → Path-in b c → Path-in a c cons
That is: a path is either empty, in which case it starts and ends at itself (these are the identity morphisms), or we can form a path from by starting with a path and precomposing with an edge Much of the code below is dedicated to characterising the identity types between paths. Indeed, to construct a category we must show that paths in form a set.
We have a couple of options here:
We can construct paths by recursion, on their length: Defining a “path from to of length ” by recursion on and then defining a path from to as being a pair where and is a path of length
We can define paths by induction, as is done above.
The former approach makes it easy to show that paths form a set: we can directly construct the set of paths, by recursion, then project the underlying type if necessary. But working with these paths is very inconvenient, since we have to deal with explicit identities between the endpoints. The latter approach makes defining functions on paths easy, but showing that they are a set is fairly involved. Let’s see how to do it.
The first thing we’ll need is a predicate expressing that a path really encodes the empty path, and we have an identity of vertices We can do this by recursion: If is nil, then we can take this to be the unit type, otherwise it’s the bottom type.
: ∀ {a b} → Path-in a b → Type (o ⊔ ℓ)
is-nil = Lift _ ⊤
is-nil nil (cons _ _) = Lift _ ⊥ is-nil
We’d like to define a relation standing for an identification of paths But observe what happens in the case where we’ve built up a path by adding an edge: We know that the edges start at and the inner paths end at but the inner vertex may vary!
We’ll need to package an identification
in the relation for
and so, we’ll have to encode for a path
over some identification of their start points. That’s why we
have path-codep
and not
“path-code”. A value in
codes for a path
over
path-codep: ∀ (a : I → ∣ G.vert ∣) {c}
→ Path-in (a i0) c
→ Path-in (a i1) c
→ Type (o ⊔ ℓ)
Note that in the case where
Agda refines
to be definitionally
and we can no longer match on the right-hand-side path
That’s where the is-nil
predicate
comes in: We say that
is equal to
if is-nil
holds. Of course, a cons
and a
nil
can never be equal.
= is-nil ys
path-codep a nil ys (cons x xs) nil = Lift _ ⊥ path-codep a
The recursive case constructs an identification of cons
cells as a triple consisting of an
identification between their intermediate vertices, and over that data,
an identification between the added edges, and a code for an
identification between the tails.
{c} (cons {b = b} x xs) (cons {b = b'} y ys) =
path-codep a (b ≡ b') ]
Σ[ bs ∈ (PathP (λ i → ∣ G.edge (a i) (bs i) ∣) x y × path-codep (λ i → bs i) xs ys)
By recursion on the paths and the code for an equality, we can show
that if we have a code for an identification, we can indeed compute an
identification. The most involved case is actually when the lists are
empty, in which case we must show that is-nil(xs)
2 implies that
but it must be over an arbitrary identification
3. Fortunately, vertices in a graph
live in a set, so
is reflexivity.
path-encode: ∀ (a : I → ∣ G.vert ∣) {c} xs ys
→ path-codep a xs ys
→ PathP (λ i → Path-in (a i) c) xs ys
(cons x xs) (cons y ys) (p , q , r) i =
path-encode a {a = a i} {b = p i} (q i) $ path-encode (λ i → p i) xs ys r i
cons = lemma (λ i → a (~ i)) ys p where
path-encode a nil ys p : ∀ {a b} (p : a ≡ b) (q : Path-in a b)
lemma → is-nil q → PathP (λ i → Path-in (p (~ i)) b) nil q
{a = a} p nil (lift lower) = to-pathp $
lemma (λ e → Path-in e a) (sym p) nil ≡⟨ (λ i → subst (λ e → Path-in e a) (G.vert .is-tr a a (sym p) refl i) nil) ⟩
subst (λ e → Path-in e a) refl nil ≡⟨ transport-refl _ ⟩
subst
nil ∎_ (cons x p) () lemma
The next step is to show that codes for identifications between paths live in a proposition; But this is immediate by their construction: in every case, we can show that they are either literally a proposition (the base case) or built out of propositions: this last case is inductive.
path-codep-is-prop: ∀ (a : I → ∣ G.vert ∣) {b}
→ (p : Path-in (a i0) b) (q : Path-in (a i1) b) → is-prop (path-codep a p q)
= is-nil-is-prop xs x y where
path-codep-is-prop a nil xs x y : ∀ {a b} (xs : Path-in a b) → is-prop (is-nil xs)
is-nil-is-prop = refl
is-nil-is-prop nil x y (cons h t) (cons h' t') (p , q , r) (p' , q' , r') =
path-codep-is-prop a (G.vert .is-tr _ _ _ _) $
Σ-pathp
Σ-pathp(is-prop→pathp (λ i → PathP-is-hlevel' 1 (G.edge _ _ .is-tr) _ _) q q')
(is-prop→pathp
(λ i → path-codep-is-prop (λ j → G.vert .is-tr _ _ p p' i j) t t')
) r r'
And finally, by proving that there is a code for the reflexivity path, we can show that we have an identity system in the type of paths from to given by their codes. Since these codes are propositions, and identity systems give a characterisation of a type’s identity types, we conclude that paths between a pair of vertices live in a set!
: ∀ {a b} (p : Path-in a b) → path-codep (λ i → a) p p
path-codep-refl = lift tt
path-codep-refl nil (cons x p) = refl , refl , path-codep-refl p
path-codep-refl
path-identity-system: ∀ {a b}
→ is-identity-system {A = Path-in a b} (path-codep (λ i → a)) path-codep-refl
= set-identity-system
path-identity-system (path-codep-is-prop λ i → _)
(path-encode _ _ _)
: ∀ {a b} → is-set (Path-in a b)
path-is-set {a = a} = identity-system→hlevel 1 path-identity-system $
path-is-set λ i → a path-codep-is-prop
The path category🔗
By comparison, constructing the actual precategory of paths is almost trivial. The composition operation, concatenation, is defined by recursion over the left-hand-side path. This is definitionally unital on the left.
_++_ : ∀ {a b c} → Path-in a b → Path-in b c → Path-in a c
= ys
nil ++ ys = cons x (xs ++ ys) cons x xs ++ ys
Right unit and associativity are proven by induction.
: ∀ {a b} (xs : Path-in a b) → xs ++ nil ≡ xs
++-idr = refl
++-idr nil (cons x xs) = ap (cons x) (++-idr xs)
++-idr
++-assoc: ∀ {a b c d} (p : Path-in a b) (q : Path-in b c) (r : Path-in c d)
→ (p ++ q) ++ r ≡ p ++ (q ++ r)
= refl
++-assoc nil q r (cons x p) q r = ap (cons x) (++-assoc p q r) ++-assoc
And that’s it! Note that we must compose paths backwards, since the type of the concatenation operation and the type of morphism composition are mismatched (they’re reversed).
open Precategory
: Precategory o (o ⊔ ℓ)
Path-category .Ob = ∣ G.vert ∣
Path-category .Hom = Path-in
Path-category .Hom-set _ _ = path-is-set
Path-category .id = nil
Path-category ._∘_ xs ys = ys ++ xs
Path-category .idr f = refl
Path-category .idl f = ++-idr f
Path-category .assoc f g h = ++-assoc h g f Path-category
Moreover, free categories are always gaunt: they are automatically strict and,
as can be seen with a bit of work, univalent. Univalence follows because
any non-trivial isomorphism would have to arise as a cons
, but cons
can never be nil
— which would be required for a
composition to equal the identity.
While types prevent us from directly stating “if a map is invertible,
it is nil
”, we can nevertheless
pass around some equalities to make this induction acceptable.
: is-category Path-category
Path-category-is-category = r where
Path-category-is-category module Pc = Cat.Reasoning Path-category
: ∀ {x y} (j : Pc.Isomorphism x y) → Σ (x ≡ y) λ p → PathP (λ i → Pc.Isomorphism x (p i)) Pc.id-iso j
rem₁ {x = x} im = go im (im .Pc.to) refl (path-decode (im .Pc.invr)) where
rem₁ : ∀ {y} (im : Pc.Isomorphism x y) (j' : Path-in x y) → j' ≡ im .Pc.to
go → path-codep (λ _ → x) (j' ++ im .Pc.from) nil
→ Σ (x ≡ y) λ p → PathP (λ i → Pc.Isomorphism x (p i)) Pc.id-iso im
= refl , ext p
go im nil p q
: is-category Path-category
r .to-path i = rem₁ i .fst
r .to-path-over i = rem₁ i .snd
r
: is-gaunt Path-category
Path-category-is-gaunt = record
Path-category-is-gaunt { has-category = Path-category-is-category
; has-strict = hlevel 2
}