{- | The amalgamated free product $H *_G K$ of two group homomorphisms $\phi : G \to H$ and $\psi : G \to K$,
- i.e. their pushout in the category of groups, presented as a quotient of the free group on $H \sqcup K$.
-}
\import Algebra.Group
\import Algebra.Group.Free
\import Algebra.Group.GroupHom
\import Algebra.Group.QuotientProperties
\import Algebra.Group.SubGroup
\import Algebra.Meta
\import Algebra.Monoid
\import Algebra.Pointed
\import Algebra.Pointed.PointedHom
\import Data.Bool
\import Data.List
\import Data.Or
\import Equiv
\import Function.Meta
\import Logic
\import Paths
\import Paths.Meta
\import Relation.Equivalence
\open FreeGroup (can)
-- | The normal subgroup generated by a predicate.
\func normalClosure {G : Group} (R : G -> \Prop) : NormalSubGroup G \cowith
| contains x => \Pi (N : NormalSubGroup G) -> (\Pi {r : G} -> R r -> N r) -> N x
| contains_ide N _ => N.contains_ide
| contains_* c c' N q => N.contains_* (c N q) (c' N q)
| contains_inverse c N q => N.contains_inverse (c N q)
| isNormal g c N q => N.isNormal g (c N q)
\where {
\lemma gen {G : Group} (R : G -> \Prop) {r : G} (p : R r) : normalClosure R r
=> \lam _ q => q p
-- | A homomorphism killing every relator factors through the quotient by their normal closure.
\func lift {G L : Group} {R : G -> \Prop} (h : GroupHom G L) (p : \Pi {r : G} -> R r -> h r = ide)
: GroupHom (G // normalClosure R) L \cowith
| func (_ : G // normalClosure R) : L \with {
| in~ x => h x
| ~-equiv x y c => inv ide-right *> pmap (h x *) (inv (c (KernelGroup h) p)) *> inv h.func-* *> pmap h equation.group
}
| func-* {in~ x} {in~ y} => h.func-*
}
\func GroupPushout {G H K : Group} (phi : GroupHom G H) (psi : GroupHom G K) : Group
=> FreeGroup (Or H K) // normalClosure (Relator phi psi)
\where {
\truncated \data Relator {G H K : Group} (phi : GroupHom G H) (psi : GroupHom G K) (w : FreeGroup (Or H K)) : \Prop
| rel-left (h h' : H) (w = {FreeGroup (Or H K)} inverse (can (inl (h * h'))) * (can (inl h) * can (inl h')))
| rel-right (k k' : K) (w = {FreeGroup (Or H K)} inverse (can (inr (k * k'))) * (can (inr k) * can (inr k')))
| rel-glue (g : G) (w = {FreeGroup (Or H K)} inverse (can (inl (phi g))) * can (inr (psi g)))
}
\func groupPushoutLeft {G H K : Group} {phi : GroupHom G H} {psi : GroupHom G K} : GroupHom H (GroupPushout phi psi) \cowith
| func h => in~ (can (inl h))
| func-* {h} {h'} => ~-pequiv (normalClosure.gen (GroupPushout.Relator phi psi) (GroupPushout.rel-left h h' idp))
\func groupPushoutRight {G H K : Group} {phi : GroupHom G H} {psi : GroupHom G K} : GroupHom K (GroupPushout phi psi) \cowith
| func k => in~ (can (inr k))
| func-* {k} {k'} => ~-pequiv (normalClosure.gen (GroupPushout.Relator phi psi) (GroupPushout.rel-right k k' idp))
\lemma groupPushout-agree {G H K : Group} {phi : GroupHom G H} {psi : GroupHom G K} (g : G)
: groupPushoutLeft {_} {_} {_} {phi} {psi} (phi g) = groupPushoutRight (psi g)
=> ~-pequiv (normalClosure.gen (GroupPushout.Relator phi psi) (GroupPushout.rel-glue g idp))
\func groupPushoutRec {G H K L : Group} {phi : GroupHom G H} {psi : GroupHom G K}
(a : GroupHom H L) (b : GroupHom K L) (e : \Pi (g : G) -> a (phi g) = b (psi g))
: GroupHom (GroupPushout phi psi) L
=> \let f => (FreeGroupUniversalProperty (Or H K) L (Or.rec a b)).1
\in normalClosure.lift f (kill-relator f (\lam _ => ide-right))
\where {
\lemma kill-relator (f : GroupHom (FreeGroup (Or H K)) L) (fc : \Pi (s : Or H K) -> f (can s) = Or.rec a b s)
{w : FreeGroup (Or H K)} (r : GroupPushout.Relator phi psi w) : f w = ide \elim r
| GroupPushout.rel-left h h' q => pmap f q *> kill-mul f a (\lam h => can (inl h)) (\lam h => fc (inl h)) h h'
| GroupPushout.rel-right k k' q => pmap f q *> kill-mul f b (\lam k => can (inr k)) (\lam k => fc (inr k)) k k'
| GroupPushout.rel-glue g q => pmap f q *> kill-pair f (can (inl (phi g))) (can (inr (psi g))) (fc (inl (phi g)) *> e g *> inv (fc (inr (psi g))))
\lemma kill-pair {M L : Group} (f : GroupHom M L) (x y : M) (p : f x = f y) : f (inverse x * y) = ide
=> f.func-* *> pmap (`* f y) (f.func-inverse *> pmap inverse p) *> inverse-left
\lemma kill-mul {M A L : Group} (f : GroupHom M L) (a : GroupHom A L) (l : A -> M) (fl : \Pi (x : A) -> f (l x) = a x) (x y : A)
: f (inverse (l (x * y)) * (l x * l y)) = ide
=> kill-pair f _ _ $ fl (x * y) *> a.func-* *> inv (pmap2 (*) (fl x) (fl y)) *> inv f.func-*
}
\lemma groupPushoutRec-left {G H K L : Group} {phi : GroupHom G H} {psi : GroupHom G K}
(a : GroupHom H L) (b : GroupHom K L) (e : \Pi (g : G) -> a (phi g) = b (psi g)) (h : H)
: groupPushoutRec a b e (groupPushoutLeft h) = a h
=> ide-right
\lemma groupPushoutRec-right {G H K L : Group} {phi : GroupHom G H} {psi : GroupHom G K}
(a : GroupHom H L) (b : GroupHom K L) (e : \Pi (g : G) -> a (phi g) = b (psi g)) (k : K)
: groupPushoutRec a b e (groupPushoutRight k) = b k
=> ide-right
\lemma groupPushout-unique {G H K L : Group} {phi : GroupHom G H} {psi : GroupHom G K} {f g : GroupHom (GroupPushout phi psi) L}
(pl : \Pi (h : H) -> f (groupPushoutLeft h) = g (groupPushoutLeft h))
(pr : \Pi (k : K) -> f (groupPushoutRight k) = g (groupPushoutRight k))
: f = g
=> exts \lam x => \case \elim x \with {
| in~ w => freeGroup-ext {_} {_} {f GroupHom.∘ NormalSubGroup.quotHom} {g GroupHom.∘ NormalSubGroup.quotHom} (\case \elim __ \with {
| inl h => pl h
| inr k => pr k
}) w
}
\where {
-- | Homomorphisms out of a free group agreeing on the generators agree everywhere.
\lemma freeGroup-ext {S : \Set} {L : Group} {f g : GroupHom (FreeGroup S) L}
(p : \Pi (s : S) -> f (can s) = g (can s)) (x : FreeGroup S) : f x = g x \elim x
| in~ w => words w
\where
\lemma words (w : List (\Sigma S Bool)) : f (in~ w) = g (in~ w) \elim w
| nil => f.func-ide *> inv g.func-ide
| (s, true) :: w => f.func-* {can s} {in~ w} *> pmap2 (*) (p s) (words w) *> inv (g.func-* {can s} {in~ w})
| (s, false) :: w => f.func-* {inverse (can s)} {in~ w} *> pmap2 (*) (f.func-inverse *> pmap inverse (p s) *> inv g.func-inverse) (words w) *> inv (g.func-* {inverse (can s)} {in~ w})
}
-- | Homomorphisms out of the amalgamated free product are pairs of homomorphisms agreeing on $G$.
\func groupPushout-univ {G H K L : Group} {phi : GroupHom G H} {psi : GroupHom G K}
: QEquiv {GroupHom (GroupPushout phi psi) L} {\Sigma (a : GroupHom H L) (b : GroupHom K L) (\Pi (g : G) -> a (phi g) = b (psi g))} \cowith
| f h => (h GroupHom.∘ groupPushoutLeft, h GroupHom.∘ groupPushoutRight, \lam g => pmap h (groupPushout-agree g))
| ret t => groupPushoutRec t.1 t.2 t.3
| ret_f _ => groupPushout-unique (\lam _ => ide-right) (\lam _ => ide-right)
| f_sec _ => ext (exts \lam _ => ide-right, exts \lam _ => ide-right)
\func imageClosure {G H : Group} (phi : GroupHom G H) : NormalSubGroup H
=> normalClosure (Image phi)
\where
\func Image {G H : Group} (phi : GroupHom G H) (h : H) : \Prop
=> TruncP (\Sigma (g : G) (phi g = h))
-- | Amalgamating with a trivial group is the quotient by the normal closure of the image.
\lemma GroupPushout_trivial {G H K : Group} (phi : GroupHom G H) (psi : GroupHom G K) (triv : \Pi (k : K) -> k = ide)
: IsEquiv to
=> inP equiv
\where \protected {
\func trivialHom : GroupHom K (H // imageClosure phi) \cowith
| func _ => ide {H // imageClosure phi}
| func-* => inv ide-left
\lemma image-trivial (g : G) : NormalSubGroup.quotHom {imageClosure phi} (phi g) = ide {H // imageClosure phi}
=> ~-pequiv (transportInv (imageClosure phi) ide-right
(NormalSubGroup.contains_inverse {imageClosure phi} (normalClosure.gen (imageClosure.Image phi) (inP (g, idp)))))
\func to : GroupHom (GroupPushout phi psi) (H // imageClosure phi)
=> groupPushoutRec NormalSubGroup.quotHom trivialHom image-trivial
\func from : GroupHom (H // imageClosure phi) (GroupPushout phi psi)
=> normalClosure.lift {_} {_} {imageClosure.Image phi} groupPushoutLeft (\case __ \with {
| inP (g, p) => pmap groupPushoutLeft (inv p) *> groupPushout-agree g *> pmap groupPushoutRight (triv (psi g))
*> func-ide {groupPushoutRight {_} {_} {_} {phi} {psi}}
})
\func equiv : QEquiv to \cowith
| ret => from
| ret_f x => pmap (\lam (m : GroupHom (GroupPushout phi psi) (GroupPushout phi psi)) => m x)
(groupPushout-unique {_} {_} {_} {_} {phi} {psi} {from GroupHom.∘ to} {GroupHom.id}
(\lam h => pmap from (groupPushoutRec-left {_} {_} {_} {_} {phi} {psi} _ trivialHom image-trivial h))
(\lam k => pmap from (groupPushoutRec-right {_} {_} {_} {_} {phi} {psi} _ trivialHom image-trivial k) *> func-ide {from}
*> inv (func-ide {groupPushoutRight {_} {_} {_} {phi} {psi}}) *> pmap groupPushoutRight (inv (triv k))))
| f_sec y => \case \elim y \with {
| in~ h => groupPushoutRec-left {_} {_} {_} {_} {phi} {psi} _ trivialHom image-trivial h
}
}