{-# LANGUAGE AllowAmbiguousTypes #-}

-- | Weighted limits and colimits as squares. The weight is a vertical profunctor;
-- diagrams and their limits or colimits are horizontal functors.
module Proarrow.Squares.Limit where

import Data.Kind (Type)

import Proarrow.Adjunction (Adjunction)
import Proarrow.Category.Instance.Prof (Prof (..))
import Proarrow.Colimit qualified as C
import Proarrow.Core (type (+->), type (~>))
import Proarrow.Limit qualified as L
import Proarrow.Path (Path (..))
import Proarrow.Profunctor.Corepresentable (Corepresentable (..))
import Proarrow.Profunctor.Instance.Composition ((:.:))
import Proarrow.Profunctor.Instance.Identity (Id (..))
import Proarrow.Profunctor.Representable
  ( CorepStar
  , Representable (..)
  , SameRep
  , SameRepAt (..)
  , mapCorepStar
  , unwrapRepresentable
  , withObRep
  , wrapRepresentable
  )
import Proarrow.Squares
  ( Sq (..)
  , fromBottom
  , fromTop
  , hArr
  , hCombine
  , hCombineCorep
  , hId
  , hSplit
  , hSplitCorep
  , mapSq
  , mkSquare
  , runSquare
  , toBottom
  , toTop
  , (===)
  , (|||)
  )
import Proarrow.Squares.Kan (corepMateInv, corepSquare)

-- | A @j@-weighted cone from an apex functor @p@ to a diagram @d@.
-- No universal property or chosen limit object is required.
type Cone :: (i +-> a) -> (a +-> k) -> (i +-> k) -> Type
type Cone j p d = Sq (j ::: Nil) Nil (p ::: Nil) (d ::: Nil)

-- | The limiting cone, with the weight vertical and the limit and diagram horizontal.
--
-- > A-----j-----I
-- > |     |     |
-- > l>----@---->d
-- > |           |
-- > K-----------K
limit
  :: forall {i} {a} (j :: i +-> a) k (d :: i +-> k)
   . (L.HasLimits j k, Representable d)
  => Cone j (L.Limit j d) d
limit :: forall {i} {a} (j :: i +-> a) k (d :: i +-> k).
(HasLimits j k, Representable d) =>
Cone j (Limit j d) d
limit = (Fold ((j ::: Nil) +++ (Limit j d ::: Nil))
 :~> Fold ((d ::: Nil) +++ Nil))
-> Sq (j ::: Nil) Nil (Limit j d ::: Nil) (d ::: Nil)
forall {j} {h} {k} {i} (p :: Path j h) (q :: Path k i)
       (f :: Path h i) (g :: Path j k).
(IsPath p, IsPath q, IsTight f, IsTight g) =>
(Fold (p +++ f) :~> Fold (g +++ q)) -> Sq p q f g
Sq (forall {a} {i} (j :: i +-> a) k (d :: i +-> k).
(HasLimits j k, Representable d) =>
(Limit j d :.: j) :~> d
forall (j :: i +-> a) k (d :: i +-> k).
(HasLimits j k, Representable d) =>
(Limit j d :.: j) :~> d
L.limit @j @k @d)

-- | Factor a cone through the limit.
--
-- The dotted inner square is the input; the solid outer square is the result.
--
-- > A-------------------A
-- > |                   |
-- > |   A.....j.....I   |
-- > |   :     |     :   |
-- > p>--------@---->d   l
-- > |   :           :   |
-- > |   K...........K   |
-- > |                   |
-- > K-------------------K
limitUniv
  :: forall {i} {a} (j :: i +-> a) k (d :: i +-> k) (p :: a +-> k)
   . (L.HasLimits j k, Representable d, Representable p)
  => Cone j p d
  -> Sq Nil Nil (p ::: Nil) (L.Limit j d ::: Nil)
limitUniv :: forall {i} {a} (j :: i +-> a) k (d :: i +-> k) (p :: a +-> k).
(HasLimits j k, Representable d, Representable p) =>
Cone j p d -> Sq Nil Nil (p ::: Nil) (Limit j d ::: Nil)
limitUniv = ((Fold ((j ::: Nil) +++ (p ::: Nil)) ~> Fold ((d ::: Nil) +++ Nil))
 -> Fold (Nil +++ (p ::: Nil))
    ~> Fold ((Limit j d ::: Nil) +++ Nil))
-> Sq (j ::: Nil) Nil (p ::: Nil) (d ::: Nil)
-> Sq Nil Nil (p ::: Nil) (Limit j d ::: Nil)
forall {j1} {h1} {i1} {k1} {j2} {h2} {k2} {i2} (ps :: Path j1 h1)
       (fs :: Path h1 i1) (gs :: Path j1 k1) (qs :: Path k1 i1)
       (ps' :: Path j2 h2) (qs' :: Path k2 i2) (fs' :: Path h2 i2)
       (gs' :: Path j2 k2).
(Profunctor (Fold (ps +++ fs)), Profunctor (Fold (gs +++ qs)),
 IsPath ps', IsPath qs', IsTight fs', IsTight gs') =>
((Fold (ps +++ fs) ~> Fold (gs +++ qs))
 -> Fold (ps' +++ fs') ~> Fold (gs' +++ qs'))
-> Sq ps qs fs gs -> Sq ps' qs' fs' gs'
mapSq (\(Prof (p :.: j) :~> d
n) -> (p :~> Limit j d) -> Prof p (Limit j d)
forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(Profunctor p, Profunctor q) =>
(p :~> q) -> Prof p q
Prof (forall {a} {i} (j :: i +-> a) k (d :: i +-> k) (p :: a +-> k).
(HasLimits j k, Representable d, Profunctor p) =>
((p :.: j) :~> d) -> p :~> Limit j d
forall (j :: i +-> a) k (d :: i +-> k) (p :: a +-> k).
(HasLimits j k, Representable d, Profunctor p) =>
((p :.: j) :~> d) -> p :~> Limit j d
L.limitUniv @j @k @d (:.:) p j a b -> d a b
(p :.: j) :~> d
n))

-- | Preservation of weighted limits by a right adjoint @p@.
-- Here @l@ is the @j@-weighted limit of @d@, and @l'@ is the limit of @p . d@.
--
-- > A-----------A
-- > |      /--->l
-- > l'>---@     |
-- > |      \--->p
-- > B-----------B
--
-- Bend the residual @p@ out of the limiting cone of @p :.: d@. Since @p@ is
-- also the conjoint of @CorepStar p@, it bends onto the left as that functor,
-- allowing 'limitUniv' to factor the cone through @l@. Bending @p@ back onto
-- the right gives the comparison above.
-- The bends use the unit and counit of @CorepStar p@ ⊣ @p@.
--
-- In the expanded composite, @f = CorepStar p@. The dotted rectangle encloses
-- the cone passed to 'limitUniv'; its factor is @l@ on the outer right boundary.
-- The three @\@@ nodes, from top to bottom, are the limiting cone of @p . d@,
-- the counit, and the unit. The pairs of bends are drawn as unit and counit
-- nodes, and combining and splitting paths is left implicit.
--
-- > A-----------------------------------A
-- > |                                   |
-- > |         A.......j...........I     |
-- > |         :       | --------->d     l
-- > |         :       |/          :     |
-- > l'>---------------@           :     |
-- > |         :        \          :     |
-- > |         :         --p       :     |
-- > |         :            \      :     |
-- > |         :             @     :     |
-- > |         :            /      :     |
-- > |       --f>-----------       :     |
-- > |      /  K...................K     |
-- > |     @                             |
-- > |      \                            |
-- > |       --------------------------->p
-- > |                                   |
-- > B-----------------------------------B
rightAdjointPreservesLimits
  :: forall {k} {k'} {i} {a} (j :: i +-> a) (p :: k +-> k') (d :: i +-> k)
   . (Adjunction p, Representable d, L.HasLimits j k, L.HasLimits j k')
  => Sq Nil Nil (L.Limit j (p :.: d) ::: Nil) (L.Limit j d ::: p ::: Nil)
rightAdjointPreservesLimits :: forall {k} {k'} {i} {a} (j :: i +-> a) (p :: k +-> k')
       (d :: i +-> k).
(Adjunction p, Representable d, HasLimits j k, HasLimits j k') =>
Sq Nil Nil (Limit j (p :.: d) ::: Nil) (Limit j d ::: (p ::: Nil))
rightAdjointPreservesLimits =
  (Sq Nil Nil (Limit j (p :.: d) ::: Nil) (Limit j (p :.: d) ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq Nil Nil (f ::: Nil) (f ::: Nil)
hId Sq Nil Nil (Limit j (p :.: d) ::: Nil) (Limit j (p :.: d) ::: Nil)
-> Sq Nil (p ::: Nil) Nil (CorepStar p ::: Nil)
-> Sq
     Nil
     (p ::: Nil)
     ((Limit j (p :.: d) ::: Nil) +++ Nil)
     ((Limit j (p :.: d) ::: Nil) +++ (CorepStar p ::: Nil))
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== forall {j} {k} (f :: j +-> k) (f' :: k +-> j).
(Proadjunction f f', Representable f) =>
Sq Nil (f' ::: Nil) Nil (f ::: Nil)
forall (f :: k' +-> k) (f' :: k +-> k').
(Proadjunction f f', Representable f) =>
Sq Nil (f' ::: Nil) Nil (f ::: Nil)
fromBottom @(CorepStar p))
    Sq
  Nil
  (p ::: Nil)
  (Limit j (p :.: d) ::: Nil)
  (Limit j (p :.: d) ::: (CorepStar p ::: Nil))
-> Sq
     Nil
     Nil
     (Limit j (p :.: d) ::: (CorepStar p ::: Nil))
     (Limit j d ::: Nil)
-> Sq
     (Nil +++ Nil)
     (Nil +++ (p ::: Nil))
     (Limit j (p :.: d) ::: Nil)
     (Limit j d ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| ( Sq
  Nil
  Nil
  (Limit j (p :.: d) ::: (CorepStar p ::: Nil))
  ((CorepStar p :.: Limit j (p :.: d)) ::: Nil)
forall {j1} {j2} {k} (f :: j1 +-> j2) (g :: j2 +-> k).
(Representable f, Representable g) =>
Sq Nil Nil (f ::: (g ::: Nil)) ((g :.: f) ::: Nil)
hCombine
            Sq
  Nil
  Nil
  (Limit j (p :.: d) ::: (CorepStar p ::: Nil))
  ((CorepStar p :.: Limit j (p :.: d)) ::: Nil)
-> Sq
     Nil
     Nil
     ((CorepStar p :.: Limit j (p :.: d)) ::: Nil)
     (Limit j d ::: Nil)
-> Sq
     (Nil +++ Nil)
     (Nil +++ Nil)
     (Limit j (p :.: d) ::: (CorepStar p ::: Nil))
     (Limit j d ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| forall {i} {a} (j :: i +-> a) k (d :: i +-> k) (p :: a +-> k).
(HasLimits j k, Representable d, Representable p) =>
Cone j p d -> Sq Nil Nil (p ::: Nil) (Limit j d ::: Nil)
forall (j :: i +-> a) k (d :: i +-> k) (p :: a +-> k).
(HasLimits j k, Representable d, Representable p) =>
Cone j p d -> Sq Nil Nil (p ::: Nil) (Limit j d ::: Nil)
limitUniv @j @k @d
              ( Sq
  Nil
  Nil
  ((CorepStar p :.: Limit j (p :.: d)) ::: Nil)
  (Limit j (p :.: d) ::: (CorepStar p ::: Nil))
forall {j1} {j2} {k} (f :: j1 +-> j2) (g :: j2 +-> k).
(Representable f, Representable g) =>
Sq Nil Nil ((g :.: f) ::: Nil) (f ::: (g ::: Nil))
hSplit
                  Sq
  Nil
  Nil
  ((CorepStar p :.: Limit j (p :.: d)) ::: Nil)
  (Limit j (p :.: d) ::: (CorepStar p ::: Nil))
-> Sq
     (j ::: Nil)
     Nil
     (Limit j (p :.: d) ::: (CorepStar p ::: Nil))
     (d ::: Nil)
-> Sq
     ((j ::: Nil) +++ Nil)
     (Nil +++ Nil)
     ((CorepStar p :.: Limit j (p :.: d)) ::: Nil)
     (d ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| ( ( forall {i} {a} (j :: i +-> a) k (d :: i +-> k).
(HasLimits j k, Representable d) =>
Cone j (Limit j d) d
forall (j :: i +-> a) k (d :: i +-> k).
(HasLimits j k, Representable d) =>
Cone j (Limit j d) d
limit @j @k' @(p :.: d)
                            Cone j (Limit j (p :.: d)) (p :.: d)
-> Sq Nil Nil ((p :.: d) ::: Nil) (d ::: (p ::: Nil))
-> Sq
     (Nil +++ (j ::: Nil))
     (Nil +++ Nil)
     (Limit j (p :.: d) ::: Nil)
     (d ::: (p ::: Nil))
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| Sq Nil Nil ((p :.: d) ::: Nil) (d ::: (p ::: Nil))
forall {j1} {j2} {k} (f :: j1 +-> j2) (g :: j2 +-> k).
(Representable f, Representable g) =>
Sq Nil Nil ((g :.: f) ::: Nil) (f ::: (g ::: Nil))
hSplit
                            Sq (j ::: Nil) Nil (Limit j (p :.: d) ::: Nil) (d ::: (p ::: Nil))
-> Sq Nil (p ::: Nil) (d ::: (p ::: Nil)) (d ::: Nil)
-> Sq
     (Nil +++ (j ::: Nil))
     ((p ::: Nil) +++ Nil)
     (Limit j (p :.: d) ::: Nil)
     (d ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| (Sq Nil Nil (d ::: Nil) (d ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq Nil Nil (f ::: Nil) (f ::: Nil)
hId Sq Nil Nil (d ::: Nil) (d ::: Nil)
-> Sq Nil (p ::: Nil) (p ::: Nil) Nil
-> Sq
     Nil (p ::: Nil) ((d ::: Nil) +++ (p ::: Nil)) ((d ::: Nil) +++ Nil)
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== Sq Nil (p ::: Nil) (p ::: Nil) Nil
forall {h} {k} (f :: h +-> k).
Representable f =>
Sq Nil (f ::: Nil) (f ::: Nil) Nil
toBottom)
                        )
                          Sq (j ::: Nil) (p ::: Nil) (Limit j (p :.: d) ::: Nil) (d ::: Nil)
-> Sq (p ::: Nil) Nil (CorepStar p ::: Nil) Nil
-> Sq
     (j ::: Nil)
     Nil
     ((Limit j (p :.: d) ::: Nil) +++ (CorepStar p ::: Nil))
     ((d ::: Nil) +++ Nil)
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== Sq (p ::: Nil) Nil (CorepStar p ::: Nil) Nil
forall {j} {i} (f :: j +-> i) (f' :: i +-> j).
(Proadjunction f f', Representable f) =>
Sq (f' ::: Nil) Nil (f ::: Nil) Nil
toTop
                      )
              )
        )
    Sq Nil (p ::: Nil) (Limit j (p :.: d) ::: Nil) (Limit j d ::: Nil)
-> Sq (p ::: Nil) Nil Nil (p ::: Nil)
-> Sq
     Nil
     Nil
     ((Limit j (p :.: d) ::: Nil) +++ Nil)
     ((Limit j d ::: Nil) +++ (p ::: Nil))
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== Sq (p ::: Nil) Nil Nil (p ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq (f ::: Nil) Nil Nil (f ::: Nil)
fromTop

-- | The inverse comparison, which exists for any representable @p@.
-- Applying @p@ to the limiting cone of @d@ gives a cone for @p :.: d@;
-- 'limitUniv' factors it through @l'@.
--
-- > A-----------A
-- > l>---\      |
-- > |     @--->l'
-- > p>---/      |
-- > B-----------B
--
-- The dotted rectangle encloses the cone obtained by applying @p@ to the
-- limiting cone of @d@. Its two right-hand functors are combined before
-- 'limitUniv' produces @l'@ on the outer boundary.
--
-- > A---------------------------A
-- > |                           |
-- > |   A.........j.........I   |
-- > |   :         |         :   |
-- > l>------------@-------->d  l'
-- > |   :                   :   |
-- > |   :                   :   |
-- > p>--------------------->p   |
-- > |   B...................B   |
-- > |                           |
-- > B---------------------------B
rightAdjointPreservesLimitsInv
  :: forall {k} {k'} {i} {a} (j :: i +-> a) (p :: k +-> k') (d :: i +-> k)
   . (Representable p, Representable d, L.HasLimits j k, L.HasLimits j k')
  => Sq Nil Nil (L.Limit j d ::: p ::: Nil) (L.Limit j (p :.: d) ::: Nil)
rightAdjointPreservesLimitsInv :: forall {k} {k'} {i} {a} (j :: i +-> a) (p :: k +-> k')
       (d :: i +-> k).
(Representable p, Representable d, HasLimits j k,
 HasLimits j k') =>
Sq Nil Nil (Limit j d ::: (p ::: Nil)) (Limit j (p :.: d) ::: Nil)
rightAdjointPreservesLimitsInv =
  Sq Nil Nil (Limit j d ::: (p ::: Nil)) ((p :.: Limit j d) ::: Nil)
forall {j1} {j2} {k} (f :: j1 +-> j2) (g :: j2 +-> k).
(Representable f, Representable g) =>
Sq Nil Nil (f ::: (g ::: Nil)) ((g :.: f) ::: Nil)
hCombine
    Sq Nil Nil (Limit j d ::: (p ::: Nil)) ((p :.: Limit j d) ::: Nil)
-> Sq
     Nil Nil ((p :.: Limit j d) ::: Nil) (Limit j (p :.: d) ::: Nil)
-> Sq
     (Nil +++ Nil)
     (Nil +++ Nil)
     (Limit j d ::: (p ::: Nil))
     (Limit j (p :.: d) ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| forall {i} {a} (j :: i +-> a) k (d :: i +-> k) (p :: a +-> k).
(HasLimits j k, Representable d, Representable p) =>
Cone j p d -> Sq Nil Nil (p ::: Nil) (Limit j d ::: Nil)
forall (j :: i +-> a) k (d :: i +-> k) (p :: a +-> k).
(HasLimits j k, Representable d, Representable p) =>
Cone j p d -> Sq Nil Nil (p ::: Nil) (Limit j d ::: Nil)
limitUniv @j @k' @(p :.: d)
      ( Sq Nil Nil ((p :.: Limit j d) ::: Nil) (Limit j d ::: (p ::: Nil))
forall {j1} {j2} {k} (f :: j1 +-> j2) (g :: j2 +-> k).
(Representable f, Representable g) =>
Sq Nil Nil ((g :.: f) ::: Nil) (f ::: (g ::: Nil))
hSplit
          Sq Nil Nil ((p :.: Limit j d) ::: Nil) (Limit j d ::: (p ::: Nil))
-> Sq
     (j ::: Nil) Nil (Limit j d ::: (p ::: Nil)) (d ::: (p ::: Nil))
-> Sq
     ((j ::: Nil) +++ Nil)
     (Nil +++ Nil)
     ((p :.: Limit j d) ::: Nil)
     (d ::: (p ::: Nil))
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| (forall {i} {a} (j :: i +-> a) k (d :: i +-> k).
(HasLimits j k, Representable d) =>
Cone j (Limit j d) d
forall (j :: i +-> a) k (d :: i +-> k).
(HasLimits j k, Representable d) =>
Cone j (Limit j d) d
limit @j @k @d Cone j (Limit j d) d
-> Sq Nil Nil (p ::: Nil) (p ::: Nil)
-> Sq
     (j ::: Nil)
     Nil
     ((Limit j d ::: Nil) +++ (p ::: Nil))
     ((d ::: Nil) +++ (p ::: Nil))
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== Sq Nil Nil (p ::: Nil) (p ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq Nil Nil (f ::: Nil) (f ::: Nil)
hId)
          Sq (j ::: Nil) Nil ((p :.: Limit j d) ::: Nil) (d ::: (p ::: Nil))
-> Sq Nil Nil (d ::: (p ::: Nil)) ((p :.: d) ::: Nil)
-> Sq
     (Nil +++ (j ::: Nil))
     (Nil +++ Nil)
     ((p :.: Limit j d) ::: Nil)
     ((p :.: d) ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| Sq Nil Nil (d ::: (p ::: Nil)) ((p :.: d) ::: Nil)
forall {j1} {j2} {k} (f :: j1 +-> j2) (g :: j2 +-> k).
(Representable f, Representable g) =>
Sq Nil Nil (f ::: (g ::: Nil)) ((g :.: f) ::: Nil)
hCombine
      )

-- | The colimiting cocone, expressed using the corepresenting functors.
--
-- > I-----j-----A
-- > |     |     |
-- > d>----@---->c
-- > |           |
-- > K-----------K
colimit
  :: forall {i} {a} (j :: a +-> i) k (d :: k +-> i)
   . (C.HasColimits j k, Corepresentable d)
  => Sq (j ::: Nil) Nil (CorepStar d ::: Nil) (CorepStar (C.Colimit j d) ::: Nil)
colimit :: forall {i} {a} (j :: a +-> i) k (d :: k +-> i).
(HasColimits j k, Corepresentable d) =>
Sq
  (j ::: Nil)
  Nil
  (CorepStar d ::: Nil)
  (CorepStar (Colimit j d) ::: Nil)
colimit = ((j :.: Colimit j d) :~> d)
-> Sq
     (j ::: Nil)
     Nil
     (CorepStar d ::: Nil)
     (CorepStar (Colimit j d) ::: Nil)
forall {j1} {j2} {k} (j3 :: j1 +-> j2) (p :: k +-> j1)
       (d :: k +-> j2).
(Profunctor j3, Corepresentable p, Corepresentable d) =>
((j3 :.: p) :~> d)
-> Sq (j3 ::: Nil) Nil (CorepStar d ::: Nil) (CorepStar p ::: Nil)
corepSquare (forall {i} {a} (j :: a +-> i) k (d :: k +-> i).
(HasColimits j k, Corepresentable d) =>
(j :.: Colimit j d) :~> d
forall (j :: a +-> i) k (d :: k +-> i).
(HasColimits j k, Corepresentable d) =>
(j :.: Colimit j d) :~> d
C.colimit @j @k @d)

-- | Factor a cocone through the colimit.
--
-- The dotted inner square is the input; the solid outer square is the result.
--
-- > A-------------------A
-- > |                   |
-- > |   I.....j.....A   |
-- > |   :     |     :   |
-- > c   d>----@-------->p
-- > |   :           :   |
-- > |   K...........K   |
-- > |                   |
-- > K-------------------K
colimitUniv
  :: forall {i} {a} (j :: a +-> i) k (d :: k +-> i) (p :: k +-> a)
   . (C.HasColimits j k, Corepresentable d, Corepresentable p)
  => Sq (j ::: Nil) Nil (CorepStar d ::: Nil) (CorepStar p ::: Nil)
  -> Sq Nil Nil (CorepStar (C.Colimit j d) ::: Nil) (CorepStar p ::: Nil)
colimitUniv :: forall {i} {a} (j :: a +-> i) k (d :: k +-> i) (p :: k +-> a).
(HasColimits j k, Corepresentable d, Corepresentable p) =>
Sq (j ::: Nil) Nil (CorepStar d ::: Nil) (CorepStar p ::: Nil)
-> Sq
     Nil Nil (CorepStar (Colimit j d) ::: Nil) (CorepStar p ::: Nil)
colimitUniv = ((Fold ((j ::: Nil) +++ (CorepStar d ::: Nil))
  ~> Fold ((CorepStar p ::: Nil) +++ Nil))
 -> Fold (Nil +++ (CorepStar (Colimit j d) ::: Nil))
    ~> Fold ((CorepStar p ::: Nil) +++ Nil))
-> Sq (j ::: Nil) Nil (CorepStar d ::: Nil) (CorepStar p ::: Nil)
-> Sq
     Nil Nil (CorepStar (Colimit j d) ::: Nil) (CorepStar p ::: Nil)
forall {j1} {h1} {i1} {k1} {j2} {h2} {k2} {i2} (ps :: Path j1 h1)
       (fs :: Path h1 i1) (gs :: Path j1 k1) (qs :: Path k1 i1)
       (ps' :: Path j2 h2) (qs' :: Path k2 i2) (fs' :: Path h2 i2)
       (gs' :: Path j2 k2).
(Profunctor (Fold (ps +++ fs)), Profunctor (Fold (gs +++ qs)),
 IsPath ps', IsPath qs', IsTight fs', IsTight gs') =>
((Fold (ps +++ fs) ~> Fold (gs +++ qs))
 -> Fold (ps' +++ fs') ~> Fold (gs' +++ qs'))
-> Sq ps qs fs gs -> Sq ps' qs' fs' gs'
mapSq (\Fold ((j ::: Nil) +++ (CorepStar d ::: Nil))
~> Fold ((CorepStar p ::: Nil) +++ Nil)
n -> (p ~> Colimit j d) -> CorepStar (Colimit j d) ~> CorepStar p
forall {k} {j} (p :: k +-> j) (q :: k +-> j).
(Corepresentable p, Corepresentable q) =>
(p ~> q) -> CorepStar q ~> CorepStar p
mapCorepStar ((p :~> Colimit j d) -> Prof p (Colimit j d)
forall {j} {k} (p :: j +-> k) (q :: j +-> k).
(Profunctor p, Profunctor q) =>
(p :~> q) -> Prof p q
Prof (forall {i} {a} (j :: a +-> i) k (d :: k +-> i) (p :: k +-> a).
(HasColimits j k, Corepresentable d, Profunctor p) =>
((j :.: p) :~> d) -> p :~> Colimit j d
forall (j :: a +-> i) k (d :: k +-> i) (p :: k +-> a).
(HasColimits j k, Corepresentable d, Profunctor p) =>
((j :.: p) :~> d) -> p :~> Colimit j d
C.colimitUniv @j @k @d (Prof (j :.: p) d -> (j :.: p) :~> d
forall {k1} {k} (p :: k1 +-> k) (q :: k1 +-> k).
Prof p q -> p :~> q
unProf (Prof (CorepStar d :.: j) (CorepStar p) -> Prof (j :.: p) d
forall {j1} {k} {j2} (j3 :: j1 +-> k) (p :: j2 +-> j1)
       (d :: j2 +-> k).
(Profunctor j3, Corepresentable p, Corepresentable d) =>
Prof (CorepStar d :.: j3) (CorepStar p) -> Prof (j3 :.: p) d
corepMateInv Fold ((j ::: Nil) +++ (CorepStar d ::: Nil))
~> Fold ((CorepStar p ::: Nil) +++ Nil)
Prof (CorepStar d :.: j) (CorepStar p)
n)))))

-- | Preservation of weighted colimits by the left adjoint @f = CorepStar p@.
-- Here @c@ is the @j@-weighted colimit of @d@, and @c'@ is the colimit of @f . d@.
--
-- > A-----------A
-- > c>---\      |
-- > |     @--->c'
-- > f>---/      |
-- > B-----------B
--
-- Transpose the cocone of @f . d@ along @f@ ⊣ @p@, factor the resulting
-- cocone through @c@ with 'colimitUniv', and transpose the factor back.
--
-- The dotted rectangle encloses the cocone passed to 'colimitUniv';
-- its factor is @c@ on the outer left boundary.
-- The three @\@@ nodes are the colimiting cocone of @f . d@, the unit, and
-- the counit. Combining and splitting functor encodings is left implicit.
--
-- > A-----------------------------------A
-- > |                                   |
-- > |     I...........j.......A         |
-- > c     d>--------- |       :         |
-- > |     :          \|       :         |
-- > |     :           @--------------->c'
-- > |     :          /        :         |
-- > |     :       f--         :         |
-- > |     :      /            :         |
-- > |     :     @             :         |
-- > |     :      \            :         |
-- > |     :       ----------->p--       |
-- > |     K...................K  \      |
-- > |                             @     |
-- > |                            /      |
-- > f>---------------------------       |
-- > |                                   |
-- > B-----------------------------------B
leftAdjointPreservesColimits
  :: forall {k} {k'} {i} {a} (j :: a +-> i) (p :: k' +-> k) (d :: k +-> i)
   . (Adjunction p, Corepresentable d, C.HasColimits j k, C.HasColimits j k')
  => Sq Nil Nil (CorepStar (C.Colimit j d) ::: CorepStar p ::: Nil) (CorepStar (C.Colimit j (d :.: p)) ::: Nil)
leftAdjointPreservesColimits :: forall {k} {k'} {i} {a} (j :: a +-> i) (p :: k' +-> k)
       (d :: k +-> i).
(Adjunction p, Corepresentable d, HasColimits j k,
 HasColimits j k') =>
Sq
  Nil
  Nil
  (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
leftAdjointPreservesColimits =
  ( ( forall {i} {a} (j :: a +-> i) k (d :: k +-> i) (p :: k +-> a).
(HasColimits j k, Corepresentable d, Corepresentable p) =>
Sq (j ::: Nil) Nil (CorepStar d ::: Nil) (CorepStar p ::: Nil)
-> Sq
     Nil Nil (CorepStar (Colimit j d) ::: Nil) (CorepStar p ::: Nil)
forall (j :: a +-> i) k (d :: k +-> i) (p :: k +-> a).
(HasColimits j k, Corepresentable d, Corepresentable p) =>
Sq (j ::: Nil) Nil (CorepStar d ::: Nil) (CorepStar p ::: Nil)
-> Sq
     Nil Nil (CorepStar (Colimit j d) ::: Nil) (CorepStar p ::: Nil)
colimitUniv @j @k @d
        ( ( (Sq Nil Nil (CorepStar d ::: Nil) (CorepStar d ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq Nil Nil (f ::: Nil) (f ::: Nil)
hId Sq Nil Nil (CorepStar d ::: Nil) (CorepStar d ::: Nil)
-> Sq Nil (p ::: Nil) Nil (CorepStar p ::: Nil)
-> Sq
     Nil
     (p ::: Nil)
     ((CorepStar d ::: Nil) +++ Nil)
     ((CorepStar d ::: Nil) +++ (CorepStar p ::: Nil))
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== forall {j} {k} (f :: j +-> k) (f' :: k +-> j).
(Proadjunction f f', Representable f) =>
Sq Nil (f' ::: Nil) Nil (f ::: Nil)
forall (f :: k +-> k') (f' :: k' +-> k).
(Proadjunction f f', Representable f) =>
Sq Nil (f' ::: Nil) Nil (f ::: Nil)
fromBottom @(CorepStar p))
              Sq
  Nil
  (p ::: Nil)
  (CorepStar d ::: Nil)
  (CorepStar d ::: (CorepStar p ::: Nil))
-> Sq
     Nil
     Nil
     (CorepStar d ::: (CorepStar p ::: Nil))
     (CorepStar (d :.: p) ::: Nil)
-> Sq
     (Nil +++ Nil)
     (Nil +++ (p ::: Nil))
     (CorepStar d ::: Nil)
     (CorepStar (d :.: p) ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| Sq
  Nil
  Nil
  (CorepStar d ::: (CorepStar p ::: Nil))
  (CorepStar (d :.: p) ::: Nil)
forall {j1} {j2} {k} (p :: j1 +-> j2) (q :: k +-> j1).
(Corepresentable p, Corepresentable q) =>
Sq
  Nil
  Nil
  (CorepStar p ::: (CorepStar q ::: Nil))
  (CorepStar (p :.: q) ::: Nil)
hCombineCorep
              Sq
  Nil (p ::: Nil) (CorepStar d ::: Nil) (CorepStar (d :.: p) ::: Nil)
-> Sq
     (j ::: Nil)
     Nil
     (CorepStar (d :.: p) ::: Nil)
     (CorepStar (Colimit j (d :.: p)) ::: Nil)
-> Sq
     ((j ::: Nil) +++ Nil)
     (Nil +++ (p ::: Nil))
     (CorepStar d ::: Nil)
     (CorepStar (Colimit j (d :.: p)) ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| forall {i} {a} (j :: a +-> i) k (d :: k +-> i).
(HasColimits j k, Corepresentable d) =>
Sq
  (j ::: Nil)
  Nil
  (CorepStar d ::: Nil)
  (CorepStar (Colimit j d) ::: Nil)
forall (j :: a +-> i) k (d :: k +-> i).
(HasColimits j k, Corepresentable d) =>
Sq
  (j ::: Nil)
  Nil
  (CorepStar d ::: Nil)
  (CorepStar (Colimit j d) ::: Nil)
colimit @j @k' @(d :.: p)
              Sq
  (j ::: Nil)
  (p ::: Nil)
  (CorepStar d ::: Nil)
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
-> Sq (p ::: Nil) Nil Nil (p ::: Nil)
-> Sq
     (j ::: Nil)
     Nil
     ((CorepStar d ::: Nil) +++ Nil)
     ((CorepStar (Colimit j (d :.: p)) ::: Nil) +++ (p ::: Nil))
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== Sq (p ::: Nil) Nil Nil (p ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq (f ::: Nil) Nil Nil (f ::: Nil)
fromTop
          )
            Sq
  (j ::: Nil)
  Nil
  (CorepStar d ::: Nil)
  ((CorepStar (Colimit j (d :.: p)) ::: Nil) +++ (p ::: Nil))
-> Sq
     Nil
     Nil
     ((CorepStar (Colimit j (d :.: p)) ::: Nil) +++ (p ::: Nil))
     (CorepStar (Colimit j (d :.: p))
      ::: (CorepStar (RepCostar p) ::: Nil))
-> Sq
     (Nil +++ (j ::: Nil))
     (Nil +++ Nil)
     (CorepStar d ::: Nil)
     (CorepStar (Colimit j (d :.: p))
      ::: (CorepStar (RepCostar p) ::: Nil))
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| (Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq Nil Nil (f ::: Nil) (f ::: Nil)
hId Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
-> Sq Nil Nil (p ::: Nil) (CorepStar (RepCostar p) ::: Nil)
-> Sq
     Nil
     Nil
     ((CorepStar (Colimit j (d :.: p)) ::: Nil) +++ (p ::: Nil))
     ((CorepStar (Colimit j (d :.: p)) ::: Nil)
      +++ (CorepStar (RepCostar p) ::: Nil))
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== (p :~> CorepStar (RepCostar p))
-> Sq Nil Nil (p ::: Nil) (CorepStar (RepCostar p) ::: Nil)
forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(Representable f, Representable g) =>
(f :~> g) -> Sq Nil Nil (f ::: Nil) (g ::: Nil)
hArr (forall {j} {k} (p :: j +-> k).
Representable p =>
p :~> CorepStar (RepCostar p)
forall (p :: k' +-> k).
Representable p =>
p :~> CorepStar (RepCostar p)
wrapRepresentable @p))
            Sq
  (j ::: Nil)
  Nil
  (CorepStar d ::: Nil)
  (CorepStar (Colimit j (d :.: p))
   ::: (CorepStar (RepCostar p) ::: Nil))
-> Sq
     Nil
     Nil
     (CorepStar (Colimit j (d :.: p))
      ::: (CorepStar (RepCostar p) ::: Nil))
     (CorepStar (Colimit j (d :.: p) :.: RepCostar p) ::: Nil)
-> Sq
     (Nil +++ (j ::: Nil))
     (Nil +++ Nil)
     (CorepStar d ::: Nil)
     (CorepStar (Colimit j (d :.: p) :.: RepCostar p) ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p))
   ::: (CorepStar (RepCostar p) ::: Nil))
  (CorepStar (Colimit j (d :.: p) :.: RepCostar p) ::: Nil)
forall {j1} {j2} {k} (p :: j1 +-> j2) (q :: k +-> j1).
(Corepresentable p, Corepresentable q) =>
Sq
  Nil
  Nil
  (CorepStar p ::: (CorepStar q ::: Nil))
  (CorepStar (p :.: q) ::: Nil)
hCombineCorep
        )
        Sq
  Nil
  Nil
  (CorepStar (Colimit j d) ::: Nil)
  (CorepStar (Colimit j (d :.: p) :.: RepCostar p) ::: Nil)
-> Sq
     Nil
     Nil
     (CorepStar (Colimit j (d :.: p) :.: RepCostar p) ::: Nil)
     (CorepStar (Colimit j (d :.: p))
      ::: (CorepStar (RepCostar p) ::: Nil))
-> Sq
     (Nil +++ Nil)
     (Nil +++ Nil)
     (CorepStar (Colimit j d) ::: Nil)
     (CorepStar (Colimit j (d :.: p))
      ::: (CorepStar (RepCostar p) ::: Nil))
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p) :.: RepCostar p) ::: Nil)
  (CorepStar (Colimit j (d :.: p))
   ::: (CorepStar (RepCostar p) ::: Nil))
forall {j1} {j2} {k} (p :: j1 +-> j2) (q :: k +-> j1).
(Corepresentable p, Corepresentable q) =>
Sq
  Nil
  Nil
  (CorepStar (p :.: q) ::: Nil)
  (CorepStar p ::: (CorepStar q ::: Nil))
hSplitCorep
        Sq
  Nil
  Nil
  (CorepStar (Colimit j d) ::: Nil)
  (CorepStar (Colimit j (d :.: p))
   ::: (CorepStar (RepCostar p) ::: Nil))
-> Sq
     Nil
     Nil
     (CorepStar (Colimit j (d :.: p))
      ::: (CorepStar (RepCostar p) ::: Nil))
     (CorepStar (Colimit j (d :.: p)) ::: (p ::: Nil))
-> Sq
     (Nil +++ Nil)
     (Nil +++ Nil)
     (CorepStar (Colimit j d) ::: Nil)
     (CorepStar (Colimit j (d :.: p)) ::: (p ::: Nil))
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| (Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq Nil Nil (f ::: Nil) (f ::: Nil)
hId Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
-> Sq Nil Nil (CorepStar (RepCostar p) ::: Nil) (p ::: Nil)
-> Sq
     Nil
     Nil
     ((CorepStar (Colimit j (d :.: p)) ::: Nil)
      +++ (CorepStar (RepCostar p) ::: Nil))
     ((CorepStar (Colimit j (d :.: p)) ::: Nil) +++ (p ::: Nil))
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== (CorepStar (RepCostar p) :~> p)
-> Sq Nil Nil (CorepStar (RepCostar p) ::: Nil) (p ::: Nil)
forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(Representable f, Representable g) =>
(f :~> g) -> Sq Nil Nil (f ::: Nil) (g ::: Nil)
hArr (forall {k1} {k} (p :: k1 +-> k).
Representable p =>
CorepStar (RepCostar p) :~> p
forall (p :: k' +-> k).
Representable p =>
CorepStar (RepCostar p) :~> p
unwrapRepresentable @p))
    )
      Sq
  Nil
  Nil
  (CorepStar (Colimit j d) ::: Nil)
  (CorepStar (Colimit j (d :.: p)) ::: (p ::: Nil))
-> Sq Nil Nil (CorepStar p ::: Nil) (CorepStar p ::: Nil)
-> Sq
     Nil
     Nil
     ((CorepStar (Colimit j d) ::: Nil) +++ (CorepStar p ::: Nil))
     ((CorepStar (Colimit j (d :.: p)) ::: (p ::: Nil))
      +++ (CorepStar p ::: Nil))
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== Sq Nil Nil (CorepStar p ::: Nil) (CorepStar p ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq Nil Nil (f ::: Nil) (f ::: Nil)
hId
  )
    Sq
  Nil
  Nil
  (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
  ((CorepStar (Colimit j (d :.: p)) ::: (p ::: Nil))
   +++ (CorepStar p ::: Nil))
-> Sq
     Nil
     Nil
     ((CorepStar (Colimit j (d :.: p)) ::: (p ::: Nil))
      +++ (CorepStar p ::: Nil))
     (CorepStar (Colimit j (d :.: p)) ::: Nil)
-> Sq
     (Nil +++ Nil)
     (Nil +++ Nil)
     (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
     (CorepStar (Colimit j (d :.: p)) ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| (Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq Nil Nil (f ::: Nil) (f ::: Nil)
hId Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
-> Sq Nil Nil (p ::: (CorepStar p ::: Nil)) Nil
-> Sq
     Nil
     Nil
     ((CorepStar (Colimit j (d :.: p)) ::: Nil)
      +++ (p ::: (CorepStar p ::: Nil)))
     ((CorepStar (Colimit j (d :.: p)) ::: Nil) +++ Nil)
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== (forall {h} {k} (f :: h +-> k).
Representable f =>
Sq Nil (f ::: Nil) (f ::: Nil) Nil
forall (f :: k' +-> k).
Representable f =>
Sq Nil (f ::: Nil) (f ::: Nil) Nil
toBottom @p Sq Nil (p ::: Nil) (p ::: Nil) Nil
-> Sq (p ::: Nil) Nil (CorepStar p ::: Nil) Nil
-> Sq Nil Nil ((p ::: Nil) +++ (CorepStar p ::: Nil)) (Nil +++ Nil)
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== forall {j} {i} (f :: j +-> i) (f' :: i +-> j).
(Proadjunction f f', Representable f) =>
Sq (f' ::: Nil) Nil (f ::: Nil) Nil
forall (f :: k +-> k') (f' :: k' +-> k).
(Proadjunction f f', Representable f) =>
Sq (f' ::: Nil) Nil (f ::: Nil) Nil
toTop @(CorepStar p)))

-- | The inverse comparison, which exists for any corepresentable @p@.
-- Applying @f = CorepStar p@ to the colimiting cocone of @d@ gives a cocone
-- for @d :.: p@; 'colimitUniv' factors it through @c'@.
--
-- > A-----------A
-- > |      /--->c
-- > c'>---@     |
-- > |      \--->f
-- > B-----------B
--
-- The dotted rectangle encloses the cocone obtained by applying @f@ to the
-- colimiting cocone of @d@. Its left-hand functors are combined before
-- 'colimitUniv' produces @c'@ on the outer boundary.
--
-- > A---------------------------A
-- > |                           |
-- > |   I.........j.........A   |
-- > |   :         |         :   |
-- > c'  d>--------@------------>c
-- > |   :                   :   |
-- > |   :                   :   |
-- > |   f>--------------------->f
-- > |   B...................B   |
-- > |                           |
-- > B---------------------------B
leftAdjointPreservesColimitsInv
  :: forall {k} {k'} {i} {a} (j :: a +-> i) (p :: k' +-> k) (d :: k +-> i)
   . (Corepresentable p, Corepresentable d, C.HasColimits j k, C.HasColimits j k')
  => Sq Nil Nil (CorepStar (C.Colimit j (d :.: p)) ::: Nil) (CorepStar (C.Colimit j d) ::: CorepStar p ::: Nil)
leftAdjointPreservesColimitsInv :: forall {k} {k'} {i} {a} (j :: a +-> i) (p :: k' +-> k)
       (d :: k +-> i).
(Corepresentable p, Corepresentable d, HasColimits j k,
 HasColimits j k') =>
Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
  (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
leftAdjointPreservesColimitsInv =
  forall {i} {a} (j :: a +-> i) k (d :: k +-> i) (p :: k +-> a).
(HasColimits j k, Corepresentable d, Corepresentable p) =>
Sq (j ::: Nil) Nil (CorepStar d ::: Nil) (CorepStar p ::: Nil)
-> Sq
     Nil Nil (CorepStar (Colimit j d) ::: Nil) (CorepStar p ::: Nil)
forall (j :: a +-> i) k (d :: k +-> i) (p :: k +-> a).
(HasColimits j k, Corepresentable d, Corepresentable p) =>
Sq (j ::: Nil) Nil (CorepStar d ::: Nil) (CorepStar p ::: Nil)
-> Sq
     Nil Nil (CorepStar (Colimit j d) ::: Nil) (CorepStar p ::: Nil)
colimitUniv @j @k' @(d :.: p)
    ( Sq
  Nil
  Nil
  (CorepStar (d :.: p) ::: Nil)
  (CorepStar d ::: (CorepStar p ::: Nil))
forall {j1} {j2} {k} (p :: j1 +-> j2) (q :: k +-> j1).
(Corepresentable p, Corepresentable q) =>
Sq
  Nil
  Nil
  (CorepStar (p :.: q) ::: Nil)
  (CorepStar p ::: (CorepStar q ::: Nil))
hSplitCorep
        Sq
  Nil
  Nil
  (CorepStar (d :.: p) ::: Nil)
  (CorepStar d ::: (CorepStar p ::: Nil))
-> Sq
     (j ::: Nil)
     Nil
     (CorepStar d ::: (CorepStar p ::: Nil))
     (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
-> Sq
     ((j ::: Nil) +++ Nil)
     (Nil +++ Nil)
     (CorepStar (d :.: p) ::: Nil)
     (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| (forall {i} {a} (j :: a +-> i) k (d :: k +-> i).
(HasColimits j k, Corepresentable d) =>
Sq
  (j ::: Nil)
  Nil
  (CorepStar d ::: Nil)
  (CorepStar (Colimit j d) ::: Nil)
forall (j :: a +-> i) k (d :: k +-> i).
(HasColimits j k, Corepresentable d) =>
Sq
  (j ::: Nil)
  Nil
  (CorepStar d ::: Nil)
  (CorepStar (Colimit j d) ::: Nil)
colimit @j @k @d Sq
  (j ::: Nil)
  Nil
  (CorepStar d ::: Nil)
  (CorepStar (Colimit j d) ::: Nil)
-> Sq Nil Nil (CorepStar p ::: Nil) (CorepStar p ::: Nil)
-> Sq
     (j ::: Nil)
     Nil
     ((CorepStar d ::: Nil) +++ (CorepStar p ::: Nil))
     ((CorepStar (Colimit j d) ::: Nil) +++ (CorepStar p ::: Nil))
forall {a1} {a2} {b1} {b2} {c1} {c2} (ps :: Path a1 a2)
       (qs :: Path b1 b2) (rs :: Path c1 c2) (ds :: Path a2 b2)
       (es :: Path a1 b1) (fs :: Path b2 c2) (gs :: Path b1 c1).
Sq ps qs ds es
-> Sq qs rs fs gs -> Sq ps rs (ds +++ fs) (es +++ gs)
=== Sq Nil Nil (CorepStar p ::: Nil) (CorepStar p ::: Nil)
forall {j} {k} (f :: j +-> k).
Representable f =>
Sq Nil Nil (f ::: Nil) (f ::: Nil)
hId)
        Sq
  (j ::: Nil)
  Nil
  (CorepStar (d :.: p) ::: Nil)
  (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
-> Sq
     Nil
     Nil
     (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
     (CorepStar (Colimit j d :.: p) ::: Nil)
-> Sq
     (Nil +++ (j ::: Nil))
     (Nil +++ Nil)
     (CorepStar (d :.: p) ::: Nil)
     (CorepStar (Colimit j d :.: p) ::: Nil)
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| Sq
  Nil
  Nil
  (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
  (CorepStar (Colimit j d :.: p) ::: Nil)
forall {j1} {j2} {k} (p :: j1 +-> j2) (q :: k +-> j1).
(Corepresentable p, Corepresentable q) =>
Sq
  Nil
  Nil
  (CorepStar p ::: (CorepStar q ::: Nil))
  (CorepStar (p :.: q) ::: Nil)
hCombineCorep
    )
    Sq
  Nil
  Nil
  (CorepStar (Colimit j (d :.: p)) ::: Nil)
  (CorepStar (Colimit j d :.: p) ::: Nil)
-> Sq
     Nil
     Nil
     (CorepStar (Colimit j d :.: p) ::: Nil)
     (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
-> Sq
     (Nil +++ Nil)
     (Nil +++ Nil)
     (CorepStar (Colimit j (d :.: p)) ::: Nil)
     (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
forall {b1} {h} {b2} {i} {j} {k} (ps :: Path b1 h)
       (qs :: Path b2 i) (fs :: Path h i) (gs :: Path b1 b2)
       (rs :: Path j b1) (ss :: Path k b2) (es :: Path j k).
Sq ps qs fs gs
-> Sq rs ss gs es -> Sq (rs +++ ps) (ss +++ qs) fs es
||| Sq
  Nil
  Nil
  (CorepStar (Colimit j d :.: p) ::: Nil)
  (CorepStar (Colimit j d) ::: (CorepStar p ::: Nil))
forall {j1} {j2} {k} (p :: j1 +-> j2) (q :: k +-> j1).
(Corepresentable p, Corepresentable q) =>
Sq
  Nil
  Nil
  (CorepStar (p :.: q) ::: Nil)
  (CorepStar p ::: (CorepStar q ::: Nil))
hSplitCorep

-- | Restrict a weighted cone along a section @i@ of its representable weight @j@.
-- The composite @j . i@ must be the identity functor, on objects and arrows.
-- The result is @p => d . i@. For the codiagonal weight, @i = Rep Lft@
-- or @i = Rep Rgt@ selects the corresponding projection.
-- This picks a component without exposing the identity-weight cancellation.
-- No universal property of the cone is required.
coneAt
  :: forall i j p d
   . (Representable i, Representable j, Representable p, Representable d, SameRep (j :.: i) Id)
  => Cone j p d
  -> Sq Nil Nil (p ::: Nil) (i ::: d ::: Nil)
coneAt :: forall {j} {j} {k} (i :: j +-> j) (j :: j +-> j) (p :: j +-> k)
       (d :: j +-> k).
(Representable i, Representable j, Representable p,
 Representable d, SameRep (j :.: i) Id) =>
Cone j p d -> Sq Nil Nil (p ::: Nil) (i ::: (d ::: Nil))
coneAt Cone j p d
cone = (forall (a :: j) (b :: j).
 (Ob a, Ob b) =>
 Fold Nil a b
 -> Fold Nil (Fold (p ::: Nil) % a) (Fold (i ::: (d ::: Nil)) % b))
-> Sq Nil Nil (p ::: Nil) (i ::: (d ::: Nil))
forall {j} {h} {k} {i} (ps :: Path j h) (qs :: Path k i)
       (fs :: Path h i) (gs :: Path j k).
(IsPath ps, IsPath qs, IsTight fs, IsTight gs) =>
(forall (a :: h) (b :: j).
 (Ob a, Ob b) =>
 Fold ps a b -> Fold qs (Fold fs % a) (Fold gs % b))
-> Sq ps qs fs gs
mkSquare \(Id (a ~> b
h :: a ~> b)) ->
  forall {j} {k} (p :: j +-> k) (a :: j) r.
(Representable p, Ob a) =>
(Ob (p % a) => r) -> r
forall (p :: j +-> j) (a :: j) r.
(Representable p, Ob a) =>
(Ob (p % a) => r) -> r
withObRep @i @b
    (forall {j} {k} (p :: j +-> k) (q :: j +-> k) (a :: j) r.
SameRepAt p q a =>
(((p % a) ~ (q % a)) => r) -> r
forall (p :: j +-> j) (q :: j +-> j) (a :: j) r.
SameRepAt p q a =>
(((p % a) ~ (q % a)) => r) -> r
withSameRepAt @(j :.: i) @Id @b (Cone j p d
-> Fold (j ::: Nil) a (i % b)
-> Fold Nil (Fold (p ::: Nil) % a) (Fold (d ::: Nil) % (i % b))
forall {j1} {j2} {k1} {k2} (ps :: Path j1 j2) (qs :: Path k1 k2)
       (fs :: Path j2 k2) (gs :: Path j1 k1) (a :: j2) (b :: j1).
Sq ps qs fs gs
-> Fold ps a b -> Fold qs (Fold fs % a) (Fold gs % b)
runSquare Cone j p d
cone (forall {j} {k} (p :: j +-> k) (b :: j) (a :: k).
(Representable p, Ob b) =>
(a ~> (p % b)) -> p a b
forall (p :: j +-> j) (b :: j) (a :: j).
(Representable p, Ob b) =>
(a ~> (p % b)) -> p a b
tabulate @j @(i % b) a ~> b
a ~> (j % (i % b))
h)))

-- | Restrict a weighted cocone along a section @i@ of its corepresenting weight.
-- The composite @CorepStar j . i@ must be the identity functor, on objects and arrows.
-- The result is @d . i => p@. For the codiagonal weight, @i = Rep Lft@
-- or @i = Rep Rgt@ selects the corresponding injection.
-- This is the cocone counterpart of 'coneAt'.
coconeAt
  :: forall i j d p
   . (Representable i, Corepresentable j, Representable d, Representable p, SameRep (CorepStar j :.: i) Id)
  => Sq (j ::: Nil) Nil (d ::: Nil) (p ::: Nil)
  -> Sq Nil Nil (i ::: d ::: Nil) (p ::: Nil)
coconeAt :: forall {j} {j} {k} (i :: j +-> j) (j :: j +-> j) (d :: j +-> k)
       (p :: j +-> k).
(Representable i, Corepresentable j, Representable d,
 Representable p, SameRep (CorepStar j :.: i) Id) =>
Sq (j ::: Nil) Nil (d ::: Nil) (p ::: Nil)
-> Sq Nil Nil (i ::: (d ::: Nil)) (p ::: Nil)
coconeAt Sq (j ::: Nil) Nil (d ::: Nil) (p ::: Nil)
cocone = (forall (a :: j) (b :: j).
 (Ob a, Ob b) =>
 Fold Nil a b
 -> Fold Nil (Fold (i ::: (d ::: Nil)) % a) (Fold (p ::: Nil) % b))
-> Sq Nil Nil (i ::: (d ::: Nil)) (p ::: Nil)
forall {j} {h} {k} {i} (ps :: Path j h) (qs :: Path k i)
       (fs :: Path h i) (gs :: Path j k).
(IsPath ps, IsPath qs, IsTight fs, IsTight gs) =>
(forall (a :: h) (b :: j).
 (Ob a, Ob b) =>
 Fold ps a b -> Fold qs (Fold fs % a) (Fold gs % b))
-> Sq ps qs fs gs
mkSquare \(Id (a ~> b
h :: a ~> b)) ->
  forall {j} {k} (p :: j +-> k) (a :: j) r.
(Representable p, Ob a) =>
(Ob (p % a) => r) -> r
forall (p :: j +-> j) (a :: j) r.
(Representable p, Ob a) =>
(Ob (p % a) => r) -> r
withObRep @i @a
    (forall {j} {k} (p :: j +-> k) (q :: j +-> k) (a :: j) r.
SameRepAt p q a =>
(((p % a) ~ (q % a)) => r) -> r
forall (p :: j +-> j) (q :: j +-> j) (a :: j) r.
SameRepAt p q a =>
(((p % a) ~ (q % a)) => r) -> r
withSameRepAt @(CorepStar j :.: i) @Id @a (Sq (j ::: Nil) Nil (d ::: Nil) (p ::: Nil)
-> Fold (j ::: Nil) (i % a) b
-> Fold Nil (Fold (d ::: Nil) % (i % a)) (Fold (p ::: Nil) % b)
forall {j1} {j2} {k1} {k2} (ps :: Path j1 j2) (qs :: Path k1 k2)
       (fs :: Path j2 k2) (gs :: Path j1 k1) (a :: j2) (b :: j1).
Sq ps qs fs gs
-> Fold ps a b -> Fold qs (Fold fs % a) (Fold gs % b)
runSquare Sq (j ::: Nil) Nil (d ::: Nil) (p ::: Nil)
cocone (forall {j} {k} (p :: j +-> k) (a :: k) (b :: j).
(Corepresentable p, Ob a) =>
((p %% a) ~> b) -> p a b
forall (p :: j +-> j) (a :: j) (b :: j).
(Corepresentable p, Ob a) =>
((p %% a) ~> b) -> p a b
cotabulate @j @(i % a) a ~> b
(j %% (i % a)) ~> b
h)))