\import Equiv
\import Function.Meta
\import InfCat.Arrows
\import InfCat.Basics
\import InfCat.Functor.Embedding
\import InfCat.Functor.Equiv
\import InfCat.Functor.FullyFaithful
\import InfCat.Objects
\import InfCat.Paths
\import InfCat.Rezk
\import Logic
\import Logic.Meta
\import Meta
\import Paths
\func IsGroupoid (C : \Cat) : \Prop
=> \Pi {x y : C} (f : x ~> y) -> IsIso f
\lemma groupoid-char {C : \Cat} : IsGroupoid C <-> (\Pi (x y : C) -> IsEquiv {x = y} =_~>)
=> (\lam cg x y => IsEquiv.fromEmbSurj (=_~>-embedding x y) \lam f => IsIso-char.1 (cg f),
\lam cg {x} {y} f => IsIso-char.2 $ inP (IsEquiv.ret (cg x y) f, IsEquiv.f_ret (cg x y)))
\lemma type-groupoid {X : \Type} : IsGroupoid X
=> \lam f => inP (=_~> $ inv (~>_= f),
pmap+ (comp _) (inv ~>_=_~>) *> =_~>_comp *> pmap+ =_~> inv_*>,
pmap+ (comp __ _) (inv ~>_=_~>) *> =_~>_comp *> pmap+ =_~> *>_inv)
\lemma cat_ob {C : \Cat} (Cg : IsGroupoid C) : IsHEquiv {Ob C} {C} unob
=> ff-surj-equiv.1 (\lam x y => later $ (IsEquiv.parallelEquiv {x = y} (inP =_~>-equiv) (groupoid-char.1 Cg (unob x) (unob y)) \case \elim x, \elim y, \elim __ \with {
| ob x, _, idp => idp
}).1 (inP unob_=), \lam y => inP (ob y, idp))
\lemma type_ob {X : \Type} : IsHEquiv {Ob X} {X} unob
=> cat_ob type-groupoid
-- | A retract of a groupoid is a groupoid.
\lemma retract-groupoid {C G : \Cat} (Gg : IsGroupoid G) (r : C ->⁺ G) (s : G ->⁺ C) (sr : \Pi (x :⁺ C) -> s (r x) = x) : IsGroupoid C
=> \lam {x} {y} f => transport (\lam (H : C ->⁺ C) => IsIso (dmap H f)) (funExt+ sr) (dmap_IsIso s (Gg (dmap r f)))
-- | A functor inverting all morphisms factors through the groupoid of objects.
\sfunc core-lift {C D : \Cat} {F : C ->⁺ D} (Fi : \Pi {x y : C} (r : x ~> y) -> IsIso (dmap F r))
: \Sigma (G : C ->⁺ Ob D) (\Pi (x :⁺ C) -> unob (G x) = F x)
=> \have e => core-lift-equiv {C} {D} {F} Fi
\in (\lam x =>⁺ (IsHEquiv.ret e x).2.1, \lam x => (IsHEquiv.ret e x).2.2 *> pmap+ F (IsHEquiv.f_ret e))
\where {
-- | The pullback of {unob} along a functor inverting all morphisms is an equivalence.
\lemma core-lift-equiv {C D : \Cat} {F : C ->⁺ D} (Fi : \Pi {x y : C} (r : x ~> y) -> IsIso (dmap F r))
: IsHEquiv {\Sigma⁺ (x : C) (\Sigma⁺ (o : Ob D) (unob o = F x))} __.1
=> IsEmbedding.surj-equiv (IsEmbedding.pullback F unob-embedding) (\lam x => inP ((x, (ob (F x), idp)), idp))
\lam s t r => \case \elim s, \elim t, \elim r \with {
| (b, (ob d, p)), (b', (ob d', p')), r => hom-surj p p' r
}
\where {
\lemma hom-surj {b b' : C} {d d' : D} (p : d = F b) (p' : d' = F b') (r : b ~> b')
: ∃ (h : (b, (ob d, p)) ~> {\Sigma⁺ (x : C) (\Sigma⁺ (o : Ob D) (unob o = F x))} (b', (ob d', p'))) (dmap __.1 h = r) \elim p, p'
| idp, idp => \case IsIso-diag-char.1 (Fi r) \with {
| inP (c, e) => inP (dpath \lam i => (r i, lift-arrow {D} {\lam i => F (r i)} e i), idp)
}
-- | A lift of a constant arrow to the pullback of {unob}.
\func lift-arrow {D : \Cat} {a : DI ->⁺ D} {c : D} (e : (\lam _ => c) = {DI ->⁺ D} a)
: DPath (\lam i => \Sigma⁺ (o : Ob D) (unob o = a i)) (ob (a dleft), idp) (ob (a dright), idp) \elim e
| idp => idd
}
}
\lemma cov-pi-cat-equiv {C : \Cat} (Cg : IsGroupoid C) (T : C ->⁺ \Cat) : IsHEquiv {\Pi (x :⁺ C) -> T x} {\Pi (x : C) -> T x} (\lam f x => f x)
=> inP \new HQEquiv {
| finv k x => transport+ T (IsHEquiv.f_ret (cat_ob Cg)) (Ob-elim T k (IsHEquiv.ret (cat_ob Cg) x))
| finv_f s => funExt+ \lam x => pmap+ (transport+ T (IsHEquiv.f_ret (cat_ob Cg)) __) (Ob-elim.cov T s _) *> transport+_app s (IsHEquiv.f_ret (cat_ob Cg))
| f_finv k => path \lam i x => Ob-elim.transport T k _ (IsHEquiv.f_ret (cat_ob Cg)) i
}
\lemma cov-pi-type-equiv {X : \Type} (T : X ->⁺ \Type) : IsEquiv {\Pi (x :⁺ X) -> T x} {\Pi (x : X) -> T x} (\lam f x => f x)
=> IsHEquiv.toIsEquiv (cov-pi-cat-equiv type-groupoid T)
\lemma cov-fun-type-equiv {X Y : \Type} : IsEquiv {X ->⁺ Y} {X -> Y} (\lam f x => f x)
=> cov-pi-type-equiv \lam _ => Y
\where {
\func toCov (f : X -> Y) (x :⁺ X) : Y
=> IsEquiv.ret cov-fun-type-equiv f x
\func ofCov {f : X ->⁺ Y} {x :⁺ X} : toCov (\lam x => f x) x = f x
=> path \lam i => IsEquiv.ret_f cov-fun-type-equiv {f} i x
\func ofInv {f : X -> Y} {x : X} : toCov f x = f x
=> path \lam i => IsEquiv.f_ret cov-fun-type-equiv {f} i x
}
-- | An equivalence of types is an equivalence of the corresponding categories.
\func type-HQEquiv {S T : \Type} (e : QEquiv {S} {T}) : HQEquiv {S} {T} \cowith
| f => toCov e
| finv => toCov e.ret
| finv_f => IsEquiv.ret (cov-pi-type-equiv \lam x => toCov e.ret (toCov e x) = x) \lam x => pmap (toCov e.ret) ofInv *> ofInv *> e.ret_f x
| f_finv => IsEquiv.ret (cov-pi-type-equiv \lam y => toCov e (toCov e.ret y) = y) \lam y => pmap (toCov e) ofInv *> ofInv *> e.f_sec y
\where \open cov-fun-type-equiv
-- | A family of groupoids as a family of types.
\type CatTypeFamily {C : \Cat} (F : C ->⁺ \Cat) (Fg : \Pi (x : C) -> IsGroupoid (F x)) (x :⁺ C) : \Type
=> GroupoidType {F x} \lam h => catGroupoid-witness F Fg h
\where {
-- | A natural choice of a lift of an arrow in a fibre of {F} to a constant arrow.
\private \sfunc groupoid-lift {C : \Cat} (F : C ->⁺ \Cat) (Fg : \Pi (x : C) -> IsGroupoid (F x))
: \Sigma (h : (\Sigma⁺ (x : C) (a b : F x) (a ~> b)) ->⁺ \Sigma⁺ (x : C) (F x))
(\Pi (u :⁺ \Sigma⁺ (x : C) (a b : F x) (a ~> b)) -> ((h u).1, \lam _ => (h u).2) = {\Sigma⁺ (x : C) (DI ->⁺ F x)} (u.1, \lam i => u.4 i))
=> ffMap-lift diag-rel-ff {\Sigma⁺ (x : C) (a b : F x) (a ~> b)} (\lam u => (u.1, \lam i => u.4 i))
\lam u => TruncP.map (IsIso-diag-char.1 (Fg u.1 u.4)) \lam s => ((u.1, s.1), path \lam i => (u.1, s.2 i))
-- | Every arrow in a fibre of {F} comes from a path, naturally in the arrow.
\private \sfunc catGroupoid-witness {C : \Cat} (F : C ->⁺ \Cat) (Fg : \Pi (x : C) -> IsGroupoid (F x)) {x :⁺ C} {a b :⁺ F x} (h :⁺ a ~> b)
: \Sigma⁺ (p : a = b) (coe (\lam i => a ~> p i) idd right = h)
=> transport+ (\lam v => \Sigma⁺ (p : v.2 dleft = v.2 dright) (coe (\lam i => v.2 dleft ~> p i) idd right = dpath (\lam i => v.2 i)))
((groupoid-lift F Fg).2 (x,a,b,h)) (idp, idp)
\func toCatType {C : \Cat} {F : C ->⁺ \Cat} {Fg : \Pi (x : C) -> IsGroupoid (F x)} {x :⁺ C} (a :⁺ F x) : CatTypeFamily F Fg x
=> groupoidType a
\func fromCatType {C : \Cat} {F : C ->⁺ \Cat} {Fg : \Pi (x : C) -> IsGroupoid (F x)} {x :⁺ C} (t :⁺ CatTypeFamily F Fg x) : F x \elim t
| groupoidType a => a
\func toCatType_fromCatType {C : \Cat} {F : C ->⁺ \Cat} {Fg : \Pi (x : C) -> IsGroupoid (F x)} {x :⁺ C} (t :⁺ CatTypeFamily F Fg x)
: toCatType (fromCatType t) = t \elim t
| groupoidType a => idp
-- | `CatTypeFamily F Fg` is equivalent to {F}.
\func catTypeFamily-equiv {C : \Cat} {F : C ->⁺ \Cat} {Fg : \Pi (x : C) -> IsGroupoid (F x)}
: HQEquiv {\Sigma⁺ (x : C) (F x)} {\Sigma⁺ (x : C) (CatTypeFamily F Fg x)} \cowith
| f s => (s.1, toCatType s.2)
| finv s => (s.1, fromCatType s.2)
| finv_f => idpe
| f_finv t => path \lam i => (t.1, toCatType_fromCatType t.2 i)
-- | Objects of a fibre of {F} are elements of the corresponding fibre of `CatTypeFamily F Fg`.
\func ob-equiv {C : \Cat} {F : C ->⁺ \Cat} {Fg : \Pi (x : C) -> IsGroupoid (F x)} {x : C}
: QEquiv {F x} {CatTypeFamily F Fg x} \cowith
| f => toCatType
| ret => fromCatType
| ret_f => idpe
| f_sec => toCatType_fromCatType
}