FreeOnSetoid
Martin Escardo, July 2026. The free egroup on a setoid, in pure MLTT. deprecated.EGroups.FreeOnType constructs the free egroup on a *type* A of generators (with the identity type as the equality of generators). Here we construct the free egroup on a *setoid* ๐ธ, which is the correct free-forgetful adjunction: the underlying-setoid functor egroups โ Setoids has a left adjoint. The construction reuses the word-level free-group construction on the underlying type โฃ ๐ธ โฃ, imported from Groups.Free, but coarsens its equivalence relation _โฟ_ so as to additionally identify generators that are related in ๐ธ. Explicitly, the equivalence relation _โ_ on words is generated by _โฟ_ (cancellation of formal inverses) together with the pointwise, letter-by-letter relation _โ_ coming from ๐ธ. For simplicity we take the equivalence relation of ๐ธ to live in the same universe as its carrier. TODO. Generalize this, if it is worth for any particular application.{-# OPTIONS --safe --without-K #-} module deprecated.EGroups.FreeOnSetoid where open import MLTT.Spartan open import MLTT.List renaming (_โท_ to _โข_ ; _++_ to _โฆ_ ; ++-assoc to โฆ-assoc) open import Groups.Free using (module free-group-construction) open import deprecated.EGroups.Setoid open import deprecated.EGroups.Type open import deprecated.EGroups.MediatingMap module _ (๐ธ : Setoid ๐ค ๐ค) where open free-group-construction โฃ ๐ธ โฃThe pointwise, letter-by-letter relation on words: two words are _โ_-related when they have the same length, the same polarities, and _โโฆ ๐ธ โง_-related generators._โ_ : FA โ FA โ ๐ค ฬ [] โ [] = ๐ [] โ (_ โข _) = ๐ (_ โข _) โ [] = ๐ ((n , a) โข s) โ ((m , b) โข t) = (n ๏ผ m) ร (a โโฆ ๐ธ โง b) ร (s โ t) โ-refl : (s : FA) โ s โ s โ-refl [] = โ โ-refl ((n , a) โข s) = refl , setoid-refl ๐ธ a , โ-refl s โ-sym : (s t : FA) โ s โ t โ t โ s โ-sym [] [] _ = โ โ-sym [] (x โข t) () โ-sym (x โข s) [] () โ-sym ((n , a) โข s) ((m , b) โข t) (p , q , r) = (p โปยน) , setoid-sym ๐ธ a b q , โ-sym s t r โ-trans : (s t u : FA) โ s โ t โ t โ u โ s โ u โ-trans [] [] [] _ _ = โ โ-trans [] [] (x โข u) _ () โ-trans [] (x โข t) u () _ โ-trans (x โข s) [] u () _ โ-trans ((n , a) โข s) ((m , b) โข t) [] _ () โ-trans ((n , a) โข s) ((m , b) โข t) ((k , c) โข u) (p , q , r) (p' , q' , r') = (p โ p') , setoid-trans ๐ธ a b c q q' , โ-trans s t u r r' โ-โฆ : (s s' t t' : FA) โ s โ s' โ t โ t' โ (s โฆ t) โ (s' โฆ t') โ-โฆ [] [] t t' _ e = e โ-โฆ [] (x โข s') t t' () e โ-โฆ (x โข s) [] t t' () e โ-โฆ ((n , a) โข s) ((m , b) โข s') t t' (p , q , r) e = p , q , โ-โฆ s s' t t' r e ฮท-โ : {a b : โฃ ๐ธ โฃ} โ a โโฆ ๐ธ โง b โ ฮท a โ ฮท b ฮท-โ q = refl , q , โThe equivalence relation _โ_ on words, generated by _โฟ_ and _โ_.data _โ_ : FA โ FA โ ๐ค ฬ where โฟ-gives-โ : {s t : FA} โ s โฟ t โ s โ t โ-gives-โ : {s t : FA} โ s โ t โ s โ t โ-trans : {s t u : FA} โ s โ t โ t โ u โ s โ u infix 0 _โ_ โ-refl : (s : FA) โ s โ s โ-refl s = โฟ-gives-โ (srt-reflexive _โท_ s) โ-sym : (s t : FA) โ s โ t โ t โ s โ-sym s t (โฟ-gives-โ p) = โฟ-gives-โ (srt-symmetric _โท_ s t p) โ-sym s t (โ-gives-โ p) = โ-gives-โ (โ-sym s t p) โ-sym s t (โ-trans p q) = โ-trans (โ-sym _ t q) (โ-sym s _ p) ๏ผ-gives-โ : {s t : FA} โ s ๏ผ t โ s โ t ๏ผ-gives-โ {s} refl = โ-refl s โฆ-left-โ : (s s' t : FA) โ s โ s' โ s โฆ t โ s' โฆ t โฆ-left-โ s s' t (โฟ-gives-โ p) = โฟ-gives-โ (โฆ-cong-โฟ p (srt-reflexive _โท_ t)) โฆ-left-โ s s' t (โ-gives-โ p) = โ-gives-โ (โ-โฆ s s' t t p (โ-refl t)) โฆ-left-โ s s' t (โ-trans {s} {u} {s'} p q) = โ-trans (โฆ-left-โ s u t p) (โฆ-left-โ u s' t q) โฆ-right-โ : (s t t' : FA) โ t โ t' โ s โฆ t โ s โฆ t' โฆ-right-โ s t t' (โฟ-gives-โ p) = โฟ-gives-โ (โฆ-cong-โฟ (srt-reflexive _โท_ s) p) โฆ-right-โ s t t' (โ-gives-โ p) = โ-gives-โ (โ-โฆ s s t t' (โ-refl s) p) โฆ-right-โ s t t' (โ-trans {t} {u} {t'} p q) = โ-trans (โฆ-right-โ s t u p) (โฆ-right-โ s u t' q) โฆ-cong-โ : {s s' t t' : FA} โ s โ s' โ t โ t' โ s โฆ t โ s' โฆ t' โฆ-cong-โ {s} {s'} {t} {t'} d e = โ-trans (โฆ-left-โ s s' t d) (โฆ-right-โ s' t t' e)The free egroup on the setoid ๐ธ: words with the operation of concatenation, the empty word as unit, and finv as inverse, all up to _โ_.free-egroup-setoid : EGroup ๐ค ๐ค free-egroup-setoid = (FA , _โ_ , (โ-refl , โ-sym , (ฮป x y z โ โ-trans {x} {y} {z}))) , _โฆ_ , ( โฆ-cong-โ , (ฮป s t u โ ๏ผ-gives-โ (โฆ-assoc s t u)) , ( [] , (ฮป s โ โ-refl s) , (ฮป s โ ๏ผ-gives-โ (([]-right-neutral s) โปยน)) , (ฮป s โ finv s , โฟ-gives-โ (finv-left-โฟ s) , โฟ-gives-โ (finv-right-โฟ s)) ) )The universal property. Given a target egroup ๐ and a setoid map f : ๐ธ โ underlying-setoid ๐, the extension is the mediating map h of deprecated.EGroups.MediatingMap, which respects _โ_ because f respects the relation of ๐ธ.module universal-property (๐ : EGroup ๐ฅ ๐ฆ) (f : โฃ ๐ธ โฃ โ โจ ๐ โฉ) (f-resp : is-setoid-map ๐ธ (underlying-setoid ๐) f) where open E-group-theory ๐ open โ-reasoning (underlying-relation ๐) (E-refl ๐) (E-trans ๐) open free-group-mediating-map โฃ ๐ธ โฃ โจ ๐ โฉ (underlying-relation ๐) (E-refl ๐) (E-sym ๐) (E-trans ๐) (E-multiplication ๐) (E-is-congruence ๐) (E-assoc ๐) (E-unit ๐) (E-unit-left ๐) (E-unit-right ๐) (E-inv ๐) (E-inv-left ๐) (E-inv-right ๐) f h-resp-โ : (s t : FA) โ s โ t โ h s โโจ ๐ โฉ h t h-resp-โ [] [] _ = E-refl ๐ (h []) h-resp-โ [] (x โข t) () h-resp-โ (x โข s) [] () h-resp-โ ((โ , a) โข s) ((โ , b) โข t) (refl , q , r) = E-is-congruence ๐ (f-resp q) (h-resp-โ s t r) h-resp-โ ((โ , a) โข s) ((โ , b) โข t) (refl , q , r) = E-is-congruence ๐ (โ-inv-cong (f a) (f b) (f-resp q)) (h-resp-โ s t r) h-resp-โ ((โ , a) โข s) ((โ , b) โข t) (() , q , r) h-resp-โ ((โ , a) โข s) ((โ , b) โข t) (() , q , r) h-resp-โ : (s t : FA) โ s โ t โ h s โโจ ๐ โฉ h t h-resp-โ s t (โฟ-gives-โ p) = h-identifies-โฟ-related-points p h-resp-โ s t (โ-gives-โ p) = h-resp-โ s t p h-resp-โ s t (โ-trans {s} {u} {t} p q) = E-trans ๐ (h s) (h u) (h t) (h-resp-โ s u p) (h-resp-โ u t q) free-map : โจ free-egroup-setoid โฉ โ โจ ๐ โฉ free-map = h free-map-is-hom : is-hom free-egroup-setoid ๐ free-map free-map-is-hom = (ฮป {x} {y} โ h-resp-โ x y) , (ฮป {x} {y} โ h-is-hom x y) free-map-triangle : (a : โฃ ๐ธ โฃ) โ free-map (ฮท a) โโจ ๐ โฉ f a free-map-triangle a = E-unit-right ๐ (f a) free-map-is-unique : (g : โจ free-egroup-setoid โฉ โ โจ ๐ โฉ) โ is-hom free-egroup-setoid ๐ g โ ((a : โฃ ๐ธ โฃ) โ g (ฮท a) โโจ ๐ โฉ f a) โ (s : โจ free-egroup-setoid โฉ) โ g s โโจ ๐ โฉ free-map s free-map-is-unique g g-hom@(_ , g-mult) g-tri = u where g-pu : g [] โโจ ๐ โฉ E-unit ๐ g-pu = homs-preserve-unit free-egroup-setoid ๐ g g-hom g-pi : (w : FA) โ g (finv w) โโจ ๐ โฉ E-inv ๐ (g w) g-pi w = homs-preserve-inv free-egroup-setoid ๐ g g-hom w u : (s : FA) โ g s โโจ ๐ โฉ free-map s u [] = g-pu u ((โ , a) โข s) = g (ฮท a โฆ s) โ[ g-mult {ฮท a} {s} ] g (ฮท a) ยทโจ ๐ โฉ g s โ[ E-is-congruence ๐ (g-tri a) (u s) ] f a ยทโจ ๐ โฉ h s โโ u ((โ , a) โข s) = g (finv (ฮท a) โฆ s) โ[ g-mult {finv (ฮท a)} {s} ] g (finv (ฮท a)) ยทโจ ๐ โฉ g s โ[ E-is-congruence ๐ lemma (u s) ] E-inv ๐ (f a) ยทโจ ๐ โฉ h s โโ where lemma : g (finv (ฮท a)) โโจ ๐ โฉ E-inv ๐ (f a) lemma = g (finv (ฮท a)) โ[ g-pi (ฮท a) ] E-inv ๐ (g (ฮท a)) โ[ โ-inv-cong (g (ฮท a)) (f a) (g-tri a) ] E-inv ๐ (f a) โโAs an immediate corollary, two homomorphisms out of the free egroup that both extend f agree up to the equivalence relation.free-map-is-uniqueโ : (gโ gโ : โจ free-egroup-setoid โฉ โ โจ ๐ โฉ) โ is-hom free-egroup-setoid ๐ gโ โ is-hom free-egroup-setoid ๐ gโ โ ((a : โฃ ๐ธ โฃ) โ gโ (ฮท a) โโจ ๐ โฉ f a) โ ((a : โฃ ๐ธ โฃ) โ gโ (ฮท a) โโจ ๐ โฉ f a) โ (s : โจ free-egroup-setoid โฉ) โ gโ s โโจ ๐ โฉ gโ s free-map-is-uniqueโ gโ gโ iโ iโ tโ tโ s = E-trans ๐ (gโ s) (free-map s) (gโ s) (free-map-is-unique gโ iโ tโ s) (E-sym ๐ (gโ s) (free-map s) (free-map-is-unique gโ iโ tโ s))The universal property as an adjunction: restriction along ฮท and extension are mutually inverse setoid maps between the hom-setoid out of the free egroup and the setoid of setoid maps from ๐ธ into the underlying setoid of the target. This is the free-forgetful adjunction between setoids and deprecated.EGroups.free-adjunction-setoid : (๐ : EGroup ๐ฅ ๐ฆ) โ hom-setoid free-egroup-setoid ๐ โ หข setoid-map-setoid ๐ธ (underlying-setoid ๐) free-adjunction-setoid ๐ = record { to = to ; from = from ; to-resp = ฮป {x} {y} โ to-resp {x} {y} ; from-resp = ฮป {x} {y} โ from-resp {x} {y} ; to-from = ฮต ; from-to = ฮธ } where open universal-property ๐ S = hom-setoid free-egroup-setoid ๐ T = setoid-map-setoid ๐ธ (underlying-setoid ๐) to : โฃ S โฃ โ โฃ T โฃ to (g , g-resp , _) = (ฮป a โ g (ฮท a)) , (ฮป {a} {b} q โ g-resp (โ-gives-โ (ฮท-โ q))) from : โฃ T โฃ โ โฃ S โฃ from (f , f-resp) = free-map f f-resp , free-map-is-hom f f-resp to-resp : is-setoid-map S T to to-resp p a = p (ฮท a) from-resp : is-setoid-map T S from from-resp {f , f-resp} {f' , f'-resp} p s = free-map-is-unique f' f'-resp (free-map f f-resp) (free-map-is-hom f f-resp) (ฮป a โ E-trans ๐ (free-map f f-resp (ฮท a)) (f a) (f' a) (free-map-triangle f f-resp a) (p a)) s ฮต : (๐ : โฃ T โฃ) โ to (from ๐) โโฆ T โง ๐ ฮต (f , f-resp) a = free-map-triangle f f-resp a ฮธ : (u : โฃ S โฃ) โ from (to u) โโฆ S โง u ฮธ u@(g , g-hom) s = let (tf , tf-resp) = to u in E-sym ๐ (g s) (free-map tf tf-resp s) (free-map-is-unique tf tf-resp g g-hom (ฮป a โ E-refl ๐ (g (ฮท a))) s)