Free
Martin Escardo, July 2026. The free egroup on a setoid. Its underlying type is the type FA of words on the generators, and its equivalence relation is convertibility _βΏ_. The operation is concatenation _β¦_, and we check that it is a congruence for _βΏ_, that the group laws hold up to _βΏ_, and that the generators are inserted by Ξ·. We then prove that it is free, in the sense that every setoid map from the generators into an egroup extends along Ξ· to a homomorphism, uniquely up to the equivalence relation of that egroup.{-# OPTIONS --safe --without-K #-} open import MLTT.Spartan open import MLTT.List renaming (_β·_ to _β’_ ; _++_ to _β¦_ ; ++-assoc to β¦-assoc) open import Relations.SRTclosure module EGroups.Free {π€ : Universe} (A : π€ βΊ Μ ) (_β_ : A β A β π€ Μ ) (βr : reflexive _β_) (βs : symmetric _β_) (βt : transitive _β_) where open import EGroups.Reduction A _β_ βr βs βt open import EGroups.Setoid open import EGroups.TypeIdentities give convertibilities, and convertibility is a congruence for cons, a special case of the congruence for concatenation.οΌ-gives-βΏ : {s t : FA} β s οΌ t β s βΏ t οΌ-gives-βΏ {s} refl = srt-reflexive _β·_ s β’-βΏ : (x : X) {s t : FA} β s βΏ t β (x β’ s) βΏ (x β’ t) β’-βΏ x e = βΏ-β¦-right (x β’ []) eWe record the two basic cancellations as convertibilities.cancel : (x : X) β (x β’ (x β») β’ []) βΏ [] cancel x = srt-extension _β·_ (x β’ (x β») β’ []) [] ([] , [] , x , (x β») , refl , refl , β[X]-refl (x β»)) cancelβ» : (x : X) (s : FA) β ((x β») β’ x β’ s) βΏ s cancelβ» x s = srt-extension _β·_ ((x β») β’ x β’ s) s ([] , s , (x β») , x , refl , refl , to-β[X] ((inv-invol x) β»ΒΉ))The word inverse reverses the word and inverts each letter.inv : FA β FA inv [] = [] inv (x β’ s) = inv s β¦ ((x β») β’ []) inv-left : (x : FA) β (inv x β¦ x) βΏ [] inv-left [] = srt-reflexive _β·_ [] inv-left (a β’ s) = srt-transitive _β·_ (inv (a β’ s) β¦ (a β’ s)) (inv s β¦ ((a β») β’ a β’ s)) [] (οΌ-gives-βΏ (β¦-assoc (inv s) ((a β») β’ []) (a β’ s))) (srt-transitive _β·_ (inv s β¦ ((a β») β’ a β’ s)) (inv s β¦ s) [] (βΏ-β¦-right (inv s) (cancelβ» a s)) (inv-left s)) inv-right : (x : FA) β (x β¦ inv x) βΏ [] inv-right [] = srt-reflexive _β·_ [] inv-right (a β’ s) = srt-transitive _β·_ ((a β’ s) β¦ inv (a β’ s)) (a β’ ((s β¦ inv s) β¦ ((a β») β’ []))) [] (οΌ-gives-βΏ (ap (a β’_) ((β¦-assoc s (inv s) ((a β») β’ [])) β»ΒΉ))) (srt-transitive _β·_ (a β’ ((s β¦ inv s) β¦ ((a β») β’ []))) (a β’ ((a β») β’ [])) [] (β’-βΏ a (βΏ-β¦-left (inv-right s) ((a β») β’ []))) (cancel a))We assemble the free egroup.underlying-setoid-of-free-egroup : Setoid (π€ βΊ) (π€ βΊ) underlying-setoid-of-free-egroup = FA , _βΏ_ , srt-reflexive _β·_ , srt-symmetric _β·_ , srt-transitive _β·_ free-egroup : EGroup (π€ βΊ) (π€ βΊ) free-egroup = underlying-setoid-of-free-egroup , _β¦_ , (Ξ» {x} {x'} {y} {y'} β β¦-cong-βΏ {x} {x'} {y} {y'}) , (Ξ» x y z β οΌ-gives-βΏ (β¦-assoc x y z)) , [] , (Ξ» x β srt-reflexive _β·_ x) , (Ξ» x β οΌ-gives-βΏ (([]-right-neutral x) β»ΒΉ)) , (Ξ» x β inv x , inv-left x , inv-right x)The underlying type of the free egroup is FA, and its insertion of generators is Ξ· : A β FA.Ξ·α΄³ : A β β¨ free-egroup β© Ξ·α΄³ = Ξ·The universal property. Given an egroup π and a setoid map f from the setoid of generators to the underlying setoid of π, we extend f to a homomorphism from the free egroup, and show that the extension is unique up to the equivalence relation of π. The extension h is defined by recursion on words, sending a generator to its value under f and a formally inverted generator to the inverse of that value. That f is a setoid map is needed already to see that h identifies the two sides of a reduction, because we cancel adjacent letters whose generators are merely β-related.module free-egroup-universal-property {π₯ π¦ : Universe} (π : EGroup π₯ π¦) (f : A β β¨ π β©) (f-resp : {a b : A} β a β b β f a ββ¨ π β© f b) where open egroup-theory π open β-reasoning (underlying-relation π) (erefl π) (etrans π) private _*_ = emultiplication-of π *-cong = econgruence-of π eα΄³ = eunit-of π invα΄³ = einv π h : FA β β¨ π β© h [] = eα΄³ h ((β , a) β’ s) = f a * h s h ((β , a) β’ s) = invα΄³ (f a) * h sThe map h respects the letter-wise relation on words, and it is a homomorphism from concatenation to the operation of π.h-respects-β[FA] : (s t : FA) β s β[FA] t β h s ββ¨ π β© h t h-respects-β[FA] [] [] β = erefl π eα΄³ h-respects-β[FA] ((β , a) β’ s) ((β , b) β’ t) ((refl , q) , r) = *-cong (f-resp q) (h-respects-β[FA] s t r) h-respects-β[FA] ((β , a) β’ s) ((β , b) β’ t) ((refl , q) , r) = *-cong (β-inv-cong (f a) (f b) (f-resp q)) (h-respects-β[FA] s t r) h-is-hom : (s t : FA) β h (s β¦ t) ββ¨ π β© (h s * h t) h-is-hom [] t = esym π _ _ (eunit-left π (h t)) h-is-hom ((β , a) β’ s) t = f a * h (s β¦ t) β[ *-cong (erefl π (f a)) (h-is-hom s t) ] f a * (h s * h t) β[ esym π _ _ (eassoc π (f a) (h s) (h t)) ] (f a * h s) * h t ββ h-is-hom ((β , a) β’ s) t = invα΄³ (f a) * h (s β¦ t) β[ *-cong (erefl π (invα΄³ (f a))) (h-is-hom s t) ] invα΄³ (f a) * (h s * h t) β[ esym π _ _ (eassoc π (invα΄³ (f a)) (h s) (h t)) ] (invα΄³ (f a) * h s) * h t ββA redex is sent to the unit, and hence h identifies the two sides of a reduction, of a reduction sequence, and finally of a convertibility. The last step uses the Church-Rosser property modulo _β_, whose two reducts are related by _β[FA]_ rather than by the identity type, which is why we needed h to respect _β[FA]_.h-redex : (x y : X) β y β[X] (x β») β h (x β’ y β’ []) ββ¨ π β© eα΄³ h-redex (β , a) (β , b) (refl , q) = f a * (invα΄³ (f b) * eα΄³) β[ I ] f a * invα΄³ (f b) β[ II ] f a * invα΄³ (f a) β[ einv-right π (f a) ] eα΄³ ββ where I = *-cong (erefl π (f a)) (eunit-right π (invα΄³ (f b))) II = *-cong (erefl π (f a)) (β-inv-cong (f b) (f a) (f-resp q)) h-redex (β , a) (β , b) (refl , q) = invα΄³ (f a) * (f b * eα΄³) β[ I ] invα΄³ (f a) * f b β[ II ] invα΄³ (f a) * f a β[ einv-left π (f a) ] eα΄³ ββ where I = *-cong (erefl π (invα΄³ (f a))) (eunit-right π (f b)) II = *-cong (erefl π (invα΄³ (f a))) (f-resp q) h-identifies-β·-related-points : {s t : FA} β s β· t β h s ββ¨ π β© h t h-identifies-β·-related-points (u , v , x , y , refl , refl , c) = h (u β¦ x β’ y β’ v) β[ h-is-hom u (x β’ y β’ v) ] h u * h (x β’ y β’ v) β[ I ] h u * (h (x β’ y β’ []) * h v) β[ II ] h u * (eα΄³ * h v) β[ III ] h u * h v β[ esym π _ _ (h-is-hom u v) ] h (u β¦ v) ββ where I = *-cong (erefl π (h u)) (h-is-hom (x β’ y β’ []) v) II = *-cong (erefl π (h u)) (*-cong (h-redex x y c) (erefl π (h v))) III = *-cong (erefl π (h u)) (eunit-left π (h v)) h-identifies-β·β-related-points : (s t : FA) β s β·β t β h s ββ¨ π β© h t h-identifies-β·β-related-points s t (n , i) = Ξ³ n s t i where Ξ³ : (n : β) (s t : FA) β iteration _β·_ n s t β h s ββ¨ π β© h t Ξ³ 0 s s refl = erefl π (h s) Ξ³ (succ n) s t (z , d , i) = etrans π (h s) (h z) (h t) (h-identifies-β·-related-points d) (Ξ³ n z t i) h-identifies-βΏ-related-points : (s t : FA) β s βΏ t β h s ββ¨ π β© h t h-identifies-βΏ-related-points s t c = Ξ³ (Church-Rosserβ s t c) where Ξ³ : (Ξ£ zβ κ FA , Ξ£ zβ κ FA , (s β·β zβ) Γ (t β·β zβ) Γ (zβ β[FA] zβ)) β h s ββ¨ π β© h t Ξ³ (zβ , zβ , Ο , Ο , ez) = h s β[ h-identifies-β·β-related-points s zβ Ο ] h zβ β[ h-respects-β[FA] zβ zβ ez ] h zβ β[ esym π _ _ (h-identifies-β·β-related-points t zβ Ο) ] h t ββHence h is a homomorphism from the free egroup which extends f along the insertion of generators.free-map : β¨ free-egroup β© β β¨ π β© free-map = h free-map-is-hom : is-hom free-egroup π free-map free-map-is-hom = (Ξ» {s} {t} β h-identifies-βΏ-related-points s t) , (Ξ» {s} {t} β h-is-hom s t) free-map-triangle : (a : A) β free-map (Ξ·α΄³ a) ββ¨ π β© f a free-map-triangle a = eunit-right π (f a)Any homomorphism extending f agrees with the extension up to the equivalence relation of π. As in Groups.Free, the argument derives preservation of the unit and of inverses from the other assumptions.free-map-is-unique : (g : β¨ free-egroup β© β β¨ π β©) β is-hom free-egroup π g β ((a : A) β g (Ξ·α΄³ a) ββ¨ π β© f a) β (s : β¨ free-egroup β©) β g s ββ¨ π β© free-map s free-map-is-unique g g-hom@(_ , g-mult) g-tri = u where u : (s : FA) β g s ββ¨ π β© h s u [] = homs-preserve-unit free-egroup π g g-hom u ((β , a) β’ s) = g (Ξ·α΄³ a β¦ s) β[ g-mult {Ξ·α΄³ a} {s} ] g (Ξ·α΄³ a) * g s β[ *-cong (g-tri a) (u s) ] f a * h s ββ u ((β , a) β’ s) = g (inv (Ξ·α΄³ a) β¦ s) β[ g-mult {inv (Ξ·α΄³ a)} {s} ] g (inv (Ξ·α΄³ a)) * g s β[ *-cong I (u s) ] invα΄³ (f a) * h s ββ where I : g (inv (Ξ·α΄³ a)) ββ¨ π β© invα΄³ (f a) I = g (inv (Ξ·α΄³ a)) β[ homs-preserve-inv free-egroup π g g-hom (Ξ·α΄³ a) ] invα΄³ (g (Ξ·α΄³ a)) β[ β-inv-cong (g (Ξ·α΄³ a)) (f a) (g-tri a) ] invα΄³ (f a) ββ free-map-is-uniqueβ : (gβ gβ : β¨ free-egroup β© β β¨ π β©) β is-hom free-egroup π gβ β is-hom free-egroup π gβ β ((a : A) β gβ (Ξ·α΄³ a) ββ¨ π β© f a) β ((a : A) β gβ (Ξ·α΄³ a) ββ¨ π β© f a) β (s : β¨ free-egroup β©) β gβ s ββ¨ π β© gβ s free-map-is-uniqueβ gβ gβ iβ iβ tβ tβ s = etrans π (gβ s) (free-map s) (gβ s) (free-map-is-unique gβ iβ tβ s) (esym π (gβ s) (free-map s) (free-map-is-unique gβ iβ tβ s))