Martin Escardo, July 2026.

The type of egroups.

An egroup is a setoid (defined in EGroups.Setoid) equipped with a
compatible group structure, that is, an operation that is a congruence
for the equivalence relation, whose group laws hold up to the
equivalence relation rather than up to the identity type _๏ผ_.

This is the analogue, for setoids, of the module Groups.Type. Compared
with Groups.Type:

 * The requirement "is-set X" is removed, as all types are considered
   to be sets in the setoid world.

 * The operation is required to be a congruence.

 * Every remaining occurrence of the identity type becomes the
   equivalence relation.

\begin{code}

{-# OPTIONS --safe --without-K #-}

module EGroups.Type where

open import MLTT.Spartan
open import EGroups.Setoid

\end{code}

The axioms of a compatible group structure on a setoid S, and the
structure and type of EGroups. As in Groups.Type, the inverse is given
existentially, as part of the axioms, where ฮฃ is used for existence in
the setoid world.

\begin{code}

egroup-axioms : (S : Setoid ๐“ค ๐“ฅ) โ†’ (โˆฃ S โˆฃ โ†’ โˆฃ S โˆฃ โ†’ โˆฃ S โˆฃ) โ†’ ๐“ค โŠ” ๐“ฅ ฬ‡
egroup-axioms S _ยท_ =
   is-congruence   (setoid-relation S) _ยท_
 ร— โ‰ˆ-associative   (setoid-relation S) _ยท_
 ร— (ฮฃ e ๊ž‰ โˆฃ S โˆฃ , โ‰ˆ-left-neutral  (setoid-relation S) e _ยท_
                ร— โ‰ˆ-right-neutral (setoid-relation S) e _ยท_
                ร— ((x : โˆฃ S โˆฃ) โ†’ ฮฃ x' ๊ž‰ โˆฃ S โˆฃ , ((x' ยท x) โ‰ˆโŸฆ S โŸง e)
                                              ร— ((x ยท x') โ‰ˆโŸฆ S โŸง e)))

egroup-structure : Setoid ๐“ค ๐“ฅ โ†’ ๐“ค โŠ” ๐“ฅ ฬ‡
egroup-structure S = ฮฃ _ยท_ ๊ž‰ (โˆฃ S โˆฃ โ†’ โˆฃ S โˆฃ โ†’ โˆฃ S โˆฃ) , (egroup-axioms S _ยท_)

EGroup : (๐“ค ๐“ฅ : Universe) โ†’ (๐“ค โŠ” ๐“ฅ) โบ ฬ‡
EGroup ๐“ค ๐“ฅ = ฮฃ S ๊ž‰ Setoid ๐“ค ๐“ฅ , egroup-structure S

\end{code}

We write โŸจ G โŸฉ for the underlying type and x โ‰ˆโŸจ G โŸฉ y for the
equivalence relation.

\begin{code}

underlying-setoid : EGroup ๐“ค ๐“ฅ โ†’ Setoid ๐“ค ๐“ฅ
underlying-setoid (S , _) = S

โŸจ_โŸฉ : EGroup ๐“ค ๐“ฅ โ†’ ๐“ค ฬ‡
โŸจ G โŸฉ = โˆฃ underlying-setoid G โˆฃ

underlying-relation : (G : EGroup ๐“ค ๐“ฅ) โ†’ โŸจ G โŸฉ โ†’ โŸจ G โŸฉ โ†’ ๐“ฅ ฬ‡
underlying-relation G = setoid-relation (underlying-setoid G)

syntax underlying-relation G x y = x โ‰ˆโŸจ G โŸฉ y

E-refl : (G : EGroup ๐“ค ๐“ฅ) โ†’ reflexive (underlying-relation G)
E-refl G = setoid-refl (underlying-setoid G)

E-sym : (G : EGroup ๐“ค ๐“ฅ) โ†’ symmetric (underlying-relation G)
E-sym G = setoid-sym (underlying-setoid G)

E-trans : (G : EGroup ๐“ค ๐“ฅ) โ†’ transitive (underlying-relation G)
E-trans G = setoid-trans (underlying-setoid G)

E-multiplication : (G : EGroup ๐“ค ๐“ฅ) โ†’ โŸจ G โŸฉ โ†’ โŸจ G โŸฉ โ†’ โŸจ G โŸฉ
E-multiplication (S , _ยท_ , _) = _ยท_

syntax E-multiplication G x y = x ยทโŸจ G โŸฉ y

E-is-congruence : (G : EGroup ๐“ค ๐“ฅ)
                โ†’ is-congruence (underlying-relation G) (E-multiplication G)
E-is-congruence (S , _ยท_ , cong , _) = cong

E-assoc : (G : EGroup ๐“ค ๐“ฅ)
        โ†’ โ‰ˆ-associative (underlying-relation G) (E-multiplication G)
E-assoc (S , _ยท_ , cong , assoc , _) = assoc

E-unit : (G : EGroup ๐“ค ๐“ฅ) โ†’ โŸจ G โŸฉ
E-unit (S , _ยท_ , cong , assoc , e , _) = e

E-unit-left : (G : EGroup ๐“ค ๐“ฅ)
            โ†’ โ‰ˆ-left-neutral (underlying-relation G) (E-unit G) (E-multiplication G)
E-unit-left (S , _ยท_ , cong , assoc , e , ln , _) = ln

E-unit-right : (G : EGroup ๐“ค ๐“ฅ)
             โ†’ โ‰ˆ-right-neutral (underlying-relation G) (E-unit G) (E-multiplication G)
E-unit-right (S , _ยท_ , cong , assoc , e , ln , rn , _) = rn

E-inv : (G : EGroup ๐“ค ๐“ฅ) โ†’ โŸจ G โŸฉ โ†’ โŸจ G โŸฉ
E-inv (S , _ยท_ , cong , assoc , e , ln , rn , inverses) x = prโ‚ (inverses x)

E-inv-left : (G : EGroup ๐“ค ๐“ฅ) (x : โŸจ G โŸฉ) โ†’ (E-inv G x ยทโŸจ G โŸฉ x) โ‰ˆโŸจ G โŸฉ E-unit G
E-inv-left (S , _ยท_ , cong , assoc , e , ln , rn , inverses) x = prโ‚ (prโ‚‚ (inverses x))

E-inv-right : (G : EGroup ๐“ค ๐“ฅ) (x : โŸจ G โŸฉ) โ†’ (x ยทโŸจ G โŸฉ E-inv G x) โ‰ˆโŸจ G โŸฉ E-unit G
E-inv-right (S , _ยท_ , cong , assoc , e , ln , rn , inverses) x = prโ‚‚ (prโ‚‚ (inverses x))

\end{code}

Homomorphisms of egroups: maps of the underlying types that respect the
equivalence relations and are multiplicative up to the equivalence
relation of the codomain. As in Groups.Type, preservation of the unit
and of inverses is not required but is derivable.

\begin{code}

is-hom : (G : EGroup ๐“ค ๐“ฅ) (H : EGroup ๐“ค' ๐“ฅ')
       โ†’ (โŸจ G โŸฉ โ†’ โŸจ H โŸฉ) โ†’ ๐“ค โŠ” ๐“ฅ โŠ” ๐“ฅ' ฬ‡
is-hom G H f =
   ({x y : โŸจ G โŸฉ} โ†’ x โ‰ˆโŸจ G โŸฉ y โ†’ f x โ‰ˆโŸจ H โŸฉ f y)
 ร— ({x y : โŸจ G โŸฉ} โ†’ f (x ยทโŸจ G โŸฉ y) โ‰ˆโŸจ H โŸฉ (f x ยทโŸจ H โŸฉ f y))

\end{code}

Some minimal group theory up to the equivalence relation, needed for
the universal property of free EGroups. This is a transcription of the
relevant parts of Groups.Type, with the identity type replaced by the
equivalence relation and with the congruence of the operation used
explicitly.

\begin{code}

module E-group-theory (G : EGroup ๐“ค ๐“ฅ) where

 private
  _โ‰ˆ_ = underlying-relation G
  _*_ = E-multiplication G
  e   = E-unit G
  inv = E-inv G

 open โ‰ˆ-reasoning _โ‰ˆ_ (E-refl G) (E-trans G)

 โ‰ˆ-inv-lemma : (x y z : โŸจ G โŸฉ) โ†’ (y * x) โ‰ˆ e โ†’ (x * z) โ‰ˆ e โ†’ y โ‰ˆ z
 โ‰ˆ-inv-lemma x y z q p =
  y             โ‰ˆ[ E-sym G _ _ (E-unit-right G y) ]
  (y * e)       โ‰ˆ[ E-is-congruence G (E-refl G y) (E-sym G _ _ p) ]
  (y * (x * z)) โ‰ˆ[ E-sym G _ _ (E-assoc G y x z) ]
  ((y * x) * z) โ‰ˆ[ E-is-congruence G q (E-refl G z) ]
  (e * z)       โ‰ˆ[ E-unit-left G z ]
  z             โ‰ˆโˆŽ

 one-left-inv : (x y : โŸจ G โŸฉ) โ†’ (y * x) โ‰ˆ e โ†’ y โ‰ˆ inv x
 one-left-inv x y q = โ‰ˆ-inv-lemma x y (inv x) q (E-inv-right G x)

 โ‰ˆ-idempotent-is-unit : (x : โŸจ G โŸฉ) โ†’ (x * x) โ‰ˆ x โ†’ x โ‰ˆ e
 โ‰ˆ-idempotent-is-unit x p =
  x                  โ‰ˆ[ E-sym G _ _ (E-unit-left G x) ]
  (e * x)            โ‰ˆ[ E-is-congruence G (E-sym G _ _ (E-inv-left G x)) (E-refl G x) ]
  ((inv x * x) * x)  โ‰ˆ[ E-assoc G (inv x) x x ]
  (inv x * (x * x))  โ‰ˆ[ E-is-congruence G (E-refl G (inv x)) p ]
  (inv x * x)        โ‰ˆ[ E-inv-left G x ]
  e                  โ‰ˆโˆŽ

 โ‰ˆ-inv-cong : (x y : โŸจ G โŸฉ) โ†’ x โ‰ˆ y โ†’ inv x โ‰ˆ inv y
 โ‰ˆ-inv-cong x y p = one-left-inv y (inv x)
                     (inv x * y โ‰ˆ[ E-is-congruence G (E-refl G (inv x)) (E-sym G _ _ p) ]
                      inv x * x โ‰ˆ[ E-inv-left G x ]
                      e         โ‰ˆโˆŽ)

\end{code}

Homomorphisms preserve the unit and inverses. As in Groups.Type, these
are derived from the definition of homomorphism.

\begin{code}

homs-preserve-unit : (G : EGroup ๐“ค ๐“ฅ) (H : EGroup ๐“ค' ๐“ฅ')
                     (f : โŸจ G โŸฉ โ†’ โŸจ H โŸฉ)
                   โ†’ is-hom G H f
                   โ†’ f (E-unit G) โ‰ˆโŸจ H โŸฉ E-unit H
homs-preserve-unit G H f (f-resp , f-mult) =
 โ‰ˆ-idempotent-is-unit (f eG)
  (f eG ยทโŸจ H โŸฉ f eG โ‰ˆ[ E-sym H _ _ (f-mult {eG} {eG}) ]
   f (eG ยทโŸจ G โŸฉ eG) โ‰ˆ[ f-resp (E-unit-left G eG) ]
   f eG             โ‰ˆโˆŽ)
 where
  open E-group-theory H
  open โ‰ˆ-reasoning (underlying-relation H) (E-refl H) (E-trans H)
  eG = E-unit G

homs-preserve-inv : (G : EGroup ๐“ค ๐“ฅ) (H : EGroup ๐“ค' ๐“ฅ')
                    (f : โŸจ G โŸฉ โ†’ โŸจ H โŸฉ)
                  โ†’ is-hom G H f
                  โ†’ (x : โŸจ G โŸฉ) โ†’ f (E-inv G x) โ‰ˆโŸจ H โŸฉ E-inv H (f x)
homs-preserve-inv G H f fh@(f-resp , f-mult) x =
 one-left-inv (f x) (f (E-inv G x))
  (f (E-inv G x) ยทโŸจ H โŸฉ f x โ‰ˆ[ E-sym H _ _ (f-mult {E-inv G x} {x}) ]
   f (E-inv G x ยทโŸจ G โŸฉ x)   โ‰ˆ[ f-resp (E-inv-left G x) ]
   f (E-unit G)             โ‰ˆ[ homs-preserve-unit G H f fh ]
   E-unit H                 โ‰ˆโˆŽ)
 where
  open E-group-theory H
  open โ‰ˆ-reasoning (underlying-relation H) (E-refl H) (E-trans H)

\end{code}

The identity is a homomorphism, and homomorphisms compose.

\begin{code}

id-is-hom : (G : EGroup ๐“ค ๐“ฅ) โ†’ is-hom G G id
id-is-hom G = (ฮป p โ†’ p) , (ฮป {x} {y} โ†’ E-refl G (x ยทโŸจ G โŸฉ y))

โˆ˜-is-hom : (F : EGroup ๐“ค ๐“ฅ) (G : EGroup ๐“ค' ๐“ฅ') (H : EGroup ๐“ฆ ๐“ฆ')
           (f : โŸจ F โŸฉ โ†’ โŸจ G โŸฉ) (g : โŸจ G โŸฉ โ†’ โŸจ H โŸฉ)
         โ†’ is-hom F G f โ†’ is-hom G H g โ†’ is-hom F H (g โˆ˜ f)
โˆ˜-is-hom F G H f g (f-resp , f-mult) (g-resp , g-mult) =
   (ฮป p โ†’ g-resp (f-resp p))
 , (ฮป {x} {y} โ†’ E-trans H _ _ _ (g-resp (f-mult {x} {y})) (g-mult {f x} {f y}))

\end{code}

An isomorphism of egroups is a homomorphism with a homomorphism
inverse, up to the equivalence relations.

\begin{code}

is-iso : (G : EGroup ๐“ค ๐“ฅ) (H : EGroup ๐“ค' ๐“ฅ')
       โ†’ (โŸจ G โŸฉ โ†’ โŸจ H โŸฉ) โ†’ ๐“ค โŠ” ๐“ฅ โŠ” ๐“ค' โŠ” ๐“ฅ' ฬ‡
is-iso G H f = is-hom G H f
             ร— (ฮฃ g ๊ž‰ (โŸจ H โŸฉ โ†’ โŸจ G โŸฉ) , is-hom H G g
                 ร— ((y : โŸจ H โŸฉ) โ†’ f (g y) โ‰ˆโŸจ H โŸฉ y)
                 ร— ((x : โŸจ G โŸฉ) โ†’ g (f x) โ‰ˆโŸจ G โŸฉ x))

_โ‰…_ : EGroup ๐“ค ๐“ฅ โ†’ EGroup ๐“ค' ๐“ฅ' โ†’ ๐“ค โŠ” ๐“ฅ โŠ” ๐“ค' โŠ” ๐“ฅ' ฬ‡
G โ‰… H = ฮฃ f ๊ž‰ (โŸจ G โŸฉ โ†’ โŸจ H โŸฉ) , is-iso G H f

โ‰…-refl : (G : EGroup ๐“ค ๐“ฅ) โ†’ G โ‰… G
โ‰…-refl G = id , id-is-hom G , id , id-is-hom G , E-refl G , E-refl G

โ‰…-sym : (G : EGroup ๐“ค ๐“ฅ) (H : EGroup ๐“ค' ๐“ฅ') โ†’ G โ‰… H โ†’ H โ‰… G
โ‰…-sym G H (f , fhom , g , ghom , f-ฮต , f-ฮธ) =
 g , ghom , f , fhom , f-ฮธ , f-ฮต

โ‰…-trans : (F : EGroup ๐“ค ๐“ฅ) (G : EGroup ๐“ค' ๐“ฅ') (H : EGroup ๐“ฆ ๐“ฆ')
        โ†’ F โ‰… G โ†’ G โ‰… H โ†’ F โ‰… H
โ‰…-trans F G H (f , fhom , fโป , fโปhom@(fโป-resp , _) , f-ฮต , f-ฮธ)
              (g , ghom@(g-resp , _) , gโป , gโปhom , g-ฮต , g-ฮธ) =
   g โˆ˜ f
 , โˆ˜-is-hom F G H f g fhom ghom
 , fโป โˆ˜ gโป
 , โˆ˜-is-hom H G F gโป fโป gโปhom fโปhom
 , (ฮป w โ†’ E-trans H _ _ _ (g-resp (f-ฮต (gโป w))) (g-ฮต w))
 , (ฮป x โ†’ E-trans F _ _ _ (fโป-resp (g-ฮธ (f x))) (f-ฮธ x))

\end{code}

The setoid of homomorphisms between two egroups, with the pointwise
equivalence relation.

\begin{code}

hom-setoid : (G : EGroup ๐“ค ๐“ฅ) (H : EGroup ๐“ค' ๐“ฅ')
           โ†’ Setoid (๐“ค โŠ” ๐“ค' โŠ” ๐“ฅ โŠ” ๐“ฅ') (๐“ค โŠ” ๐“ฅ')
hom-setoid G H =
   (ฮฃ f ๊ž‰ (โŸจ G โŸฉ โ†’ โŸจ H โŸฉ) , is-hom G H f)
 , (ฮป u v โ†’ (x : โŸจ G โŸฉ) โ†’ prโ‚ u x โ‰ˆโŸจ H โŸฉ prโ‚ v x)
 , (ฮป u x โ†’ E-refl H (prโ‚ u x))
 , (ฮป u v p x โ†’ E-sym H (prโ‚ u x) (prโ‚ v x) (p x))
 , (ฮป u v w p q x โ†’ E-trans H (prโ‚ u x) (prโ‚ v x) (prโ‚ w x) (p x) (q x))

\end{code}