\import Equiv
\import Equiv.Fiber
\import Equiv.Sigma
\import Function.Meta
\import InfCat.Basics
\import InfCat.Fibration.Contravariant.CartesianMap
\import InfCat.Limit.Terminal
\import InfCat.Square
\import Logic
\import Logic.Unique
\import Meta
\import Paths
\import Paths.Meta

-- | The slice of {C} over {b}.
\func SliceCat {C : \Cat} (b :⁺ C) : \Cat
  => \Sigma⁺ (c : C) (c ~> b)
  \where {
    {- | Arrows of the slice are arrows with a commutative triangle.
     -   The triangle is the naturality square of the family of structure maps.
     -}
    \lemma hom-equiv {C : \Cat} {X c d : C} {a : c ~> X} {b : d ~> X}
      : IsEquiv {(c,a) ~> {SliceCat X} (d,b)} {\Sigma (k : c ~> d) (comp k b = comp a idd)}
        (\lam p => (dpath \lam i => (p i).1, ~>-natural {SliceCat X} __.2))
      => inP $ transQEquiv (dpath-sigma-equiv (\lam _ => ~> X)) (sigma-right \lam k => IsEquiv.toQEquiv (triangle-equiv a b k))

    -- | Triangles over {k} are commutative triangles: a triangle is sent to the naturality square of the family of structure maps.
    \lemma triangle-equiv {C : \Cat} {X c d : C} (a : c ~> X) (b : d ~> X) (k : c ~> d)
      : IsEquiv {DPath (\lam i => k i ~> X) a b} {comp k b = comp a idd}
        (\lam t => ~>-natural {SliceCat X} __.2 {c,a} {d,b} {dpath \lam i => (k i, t i)})
      => transport IsEquiv (ext \lam t => inv \peval ~>-natural {SliceCat X} __.2 {c,a} {d,b} {dpath \lam i => (k i, t i)})
                   (inP $ square-char.equiv k idd `transQEquiv` flipQEquiv `transQEquiv` comp-equiv)

    -- | Triangles over {k} ending at {b} are determined by {k} and {b}.
    \lemma triangle-contr {C : \Cat} {x c d : C} (k : c ~> d) (b : d ~> x)
      : Contr (\Sigma (a : c ~> x) (DPath (\lam i => k i ~> x) a b))
      => Contr.Contr_QEquiv (symQEquiv $ sigma-right \lam a => IsEquiv.toQEquiv (triangle-equiv a b k)
            `transQEquiv` inv-equiv
            `transQEquiv` pathEquiv-right comp_idd-right)
          (rsigma (comp k b))

    -- | Arrows from objects over {r} to {y} are arrows from {r} to the underlying object of {y}.
    \lemma dom-equiv {C : \Cat} {x : C} (r : C) (y : SliceCat x)
      : IsEquiv {\Sigma (rho : r ~> x) ((r,rho) ~> {SliceCat x} y)} {r ~> y.1} (\lam a => dpath \lam i => (a.2 i).1)
      => inP $ later \new QEquiv {
           | f a => (dpath \lam i => (a.2 i).1, (a.1, dpath \lam i => (a.2 i).2))
           | ret t => (t.2.1, dpath \lam i => (t.1 i, t.2.2 i))
           | ret_f a => idp
           | f_sec t => idp
         } `transQEquiv` contr-right (triangle-contr __ y.2)

    -- | {x} with the identity arrow is a terminal object of the slice.
    \lemma terminal-contr {C : \Cat} {x : C} : IsTerminal {SliceCat x} (x, idd)
      => \lam y => Contr.Contr_QEquiv (symQEquiv (dpath-sigma-equiv (\lam _ c => c ~> x))) $
                   Contr.Contr_QEquiv (symQEquiv $ sigma-right \lam k => IsEquiv.toQEquiv (triangle-equiv y.2 idd k)
                                                                         `transQEquiv` pathEquiv-right comp_idd-right)
                                      (rsigma (comp y.2 idd))

    -- | An object of the slice over an object {t} of the slice over {x} as an object of the slice over the underlying object of {t}.
    \func over-forget {C : \Cat} {x : C} {t : SliceCat x} (c : SliceCat t) : SliceCat t.1
      => (c.1.1, dpath \lam i => (c.2 i).1)

    -- | The forgetful functor of a slice.
    \func forget {C : \Cat} {X : C} (s :⁺ SliceCat X) : C
      => s.1

    {- | Every arrow of the slice is cartesian for {forget}.
     -   Summing over the arrows into `r`, both sides of {cotriangle-map} are arrows into the source of {b}.
     -}
    \lemma forget-cartesian {C : \Cat} {X : C} {y z : SliceCat X} (b : y ~> z) : IsCartesianArrow forget b
      => \lam r w => fiberwiseEquiv (dom-equiv r.1 z)
                       (\lam a => cotriangle-map forget b a.2)
                       (IsEquiv.leftFactor proj-equiv (IsEquiv.trans total-equiv (dom-equiv r.1 y)))
                       (r.2, w)
      \where {
        \lemma proj-equiv {r : SliceCat X} : IsEquiv {\Sigma (w' : r.1 ~> z.1) (\Sigma (g : r.1 ~> y.1) (DPath (\lam i => g i ~> w' i) idd (dmap forget b)))} {r.1 ~> y.1} (\lam t => t.2.1)
          => inP $ later (\new QEquiv {\Sigma (w' : r.1 ~> z.1) (\Sigma (g : r.1 ~> y.1) (DPath (\lam i => g i ~> w' i) idd (dmap forget b)))}
                                      {\Sigma (g : r.1 ~> y.1) (\Sigma (w' : r.1 ~> z.1) (DPath (\lam i => g i ~> w' i) idd (dmap forget b)))} {
               | f t => (t.2.1, (t.1, t.2.2))
               | ret t => (t.2.1, (t.1, t.2.2))
               | ret_f t => idp
               | f_sec t => idp
             }) `transQEquiv` contr-right (\lam g => segal-contr g (dmap forget b))

        \lemma total-equiv {r : SliceCat X}
          : IsEquiv {\Sigma (a : \Sigma (rho : r.1 ~> X) ((r.1,rho) ~> {SliceCat X} z))
                            (\Sigma (k : (r.1,a.1) ~> {SliceCat X} y) (DPath (\lam i => k i ~> a.2 i) idd b))}
                    {\Sigma (rho : r.1 ~> X) ((r.1,rho) ~> {SliceCat X} y)}
                    (\lam s => (s.1.1, s.2.1))
          => inP $ later (\new QEquiv {\Sigma (a : \Sigma (rho : r.1 ~> X) ((r.1,rho) ~> {SliceCat X} z)) (\Sigma (k : (r.1,a.1) ~> {SliceCat X} y) (DPath (\lam i => k i ~> a.2 i) idd b))}
                                      {\Sigma (s : \Sigma (rho : r.1 ~> X) ((r.1,rho) ~> {SliceCat X} y)) (\Sigma (w : (r.1,s.1) ~> {SliceCat X} z) (DPath (\lam i => s.2 i ~> {SliceCat X} w i) idd b))} {
               | f s => ((s.1.1, s.2.1), (s.1.2, s.2.2))
               | ret t => ((t.1.1, t.2.1), (t.1.2, t.2.2))
               | ret_f s => idp
               | f_sec t => idp
             }) `transQEquiv` contr-right (\lam (s : \Sigma (rho : r.1 ~> X) ((r.1,rho) ~> {SliceCat X} y)) => segal-contr {SliceCat X} {r.1,s.1} s.2 b)
      }
  }

-- | Arrows from {Y} to the constant arrow on {a} are arrows from the target of {Y} to {a}.
\lemma const-ev1-equiv {C : \Cat} (a : C) (Y : DI ->⁺ C)
  : IsEquiv {Y ~> const a} {Y dright ~> a} (dmap \lam (F : DI ->⁺ C) => F dright)
  => IsEquiv.trans (inP $ later \new QEquiv {
        | f N => (dpath \lam l => N l dright, (dpath \lam l => N l dleft, dpath \lam k => dpath \lam l => N l k))
        | ret t => dpath \lam l k => t.2.2 k l
        | ret_f N => idp
        | f_sec t => idp
      })
      (inP $ contr-right \lam n1 => SliceCat.triangle-contr (dpath Y) n1)

-- | The source component of an arrow {v} into a constant arrow is the composite of {Y} with its target component.
\sfunc const-arrow-comp {C : \Cat} {a : C} {Y : DI ->⁺ C} (v : Y ~> const a)
  : comp (dpath Y) (dpath \lam i => v i dright) = dpath \lam i => v i dleft
  => ~>-natural {DI} {C} {Y} {\lam _ => a} (\lam j => dpath \lam i => v i j) {dleft} {dright} {dpath \lam i => i} *> comp_idd-right

{- | Arrows in the total category of the slices over the values of {R}, out of an object whose structure arrow {s}
 -   is a universal arrow from {c} to {R}, are the arrows between the underlying objects.
 -   An arrow over `g : b ~> b'` is an arrow `a : c ~> c'` with a square `comp a s' = comp s (dmap R g)`,
 -   and for a fixed `a` such `g` form a fiber of the equivalence {su}.
 -}
\lemma sliceFam-hom-equiv {B C : \Cat} (R : B ->⁺ C) {b b' : B} {c c' : C} {s : c ~> R b} {s' : c' ~> R b'}
                          (su : IsEquiv {b ~> b'} {c ~> R b'} (\lam g => comp s (dmap R g)))
  : IsEquiv {(b, (c, s)) ~> {\Sigma⁺ (x : B) (SliceCat (R x))} (b', (c', s'))} {c ~> c'} (\lam h => dpath \lam i => (h i).2.1)
  => inP $ dpath-sigma-equiv (\lam _ x => SliceCat (R x)) {b, (c, s)} {b', (c', s')}
     `transQEquiv` sigma-right (\lam (g : b ~> b') => dpath-sigma-equiv (\lam i y => y ~> R (g i)) `transQEquiv` sigma-right (square-equiv __ (dmap R g)))
     `transQEquiv` later \new QEquiv {
       | f t => (t.2.1, (t.1, t.2.2))
       | ret t => (t.2.1, (t.1, t.2.2))
       | ret_f t => idp
       | f_sec t => idp
     } `transQEquiv` contr-right (\lam a => Contr.Contr_QEquiv (sigma-right \lam g => inv-equiv {_} {comp s (dmap R g)}) (IsEquiv=>contrFibers su (comp a s')))