{-# LANGUAGE AllowAmbiguousTypes #-}

-- | Pointwise initial objects as empty weighted colimits.
module Proarrow.Squares.Initial where

import Proarrow.Category.Instance.Zero (Absurd, VOID)
import Proarrow.Colimit.Initial (HasInitialObject)
import Proarrow.Core (CategoryOf, type (+->))
import Proarrow.Path (Path (..))
import Proarrow.Profunctor.Corepresentable (Corep)
import Proarrow.Profunctor.Instance.Terminal (TerminalProfunctor)
import Proarrow.Profunctor.Representable (CorepStar, Representable, unwrapRepresentable, wrapRepresentable)
import Proarrow.Squares (Sq, hArr, (|||))
import Proarrow.Squares.Limit qualified as L

-- | The pointwise initial functor.
type Initial :: forall j k. j +-> k
type Initial @j @k = CorepStar (TerminalProfunctor :: k +-> j)

-- | The empty corepresentable diagram.
type Diagram :: forall k. k +-> VOID
type Diagram @k = Corep (Absurd :: VOID +-> k)

-- | The empty cocone, weighted by the empty functor.
--
-- > 0----empty----J
-- > |      |      |
-- > d>-----@----->I
-- > |             |
-- > K-------------K
--
-- Here @I = Initial@ and @d = CorepStar Diagram@. The indexing category @0@ has no objects.
initial
  :: forall j k
   . (CategoryOf j, HasInitialObject k)
  => Sq (Corep (Absurd :: VOID +-> j) ::: Nil) Nil (CorepStar (Diagram :: k +-> VOID) ::: Nil) (Initial ::: Nil)
initial :: forall j k.
(CategoryOf j, HasInitialObject k) =>
Sq
  (Corep Absurd ::: Nil)
  Nil
  (CorepStar Diagram ::: Nil)
  (Initial ::: Nil)
initial = Sq
  (Corep Absurd ::: Nil)
  Nil
  (CorepStar Diagram ::: Nil)
  (CorepStar TerminalProfunctor ::: Nil)
Sq
  (Corep Absurd ::: Nil)
  Nil
  (CorepStar Diagram ::: Nil)
  (CorepStar (Colimit (Corep Absurd) Diagram) ::: Nil)
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)
L.colimit

-- | Factor an empty cocone through the initial functor, uniquely.
initiate
  :: forall {j} {k} (p :: j +-> k)
   . (HasInitialObject k, Representable p)
  => Sq (Corep (Absurd :: VOID +-> j) ::: Nil) Nil (CorepStar (Diagram :: k +-> VOID) ::: Nil) (p ::: Nil)
  -> Sq Nil Nil (Initial ::: Nil) (p ::: Nil)
initiate :: forall {j} {k} (p :: j +-> k).
(HasInitialObject k, Representable p) =>
Sq
  (Corep Absurd ::: Nil) Nil (CorepStar Diagram ::: Nil) (p ::: Nil)
-> Sq Nil Nil (Initial ::: Nil) (p ::: Nil)
initiate Sq
  (Corep Absurd ::: Nil) Nil (CorepStar Diagram ::: Nil) (p ::: Nil)
cocone = Sq
  (Corep Absurd ::: Nil)
  Nil
  (CorepStar Diagram ::: Nil)
  (CorepStar (RepCostar p) ::: Nil)
-> Sq
     Nil
     Nil
     (CorepStar (Colimit (Corep Absurd) Diagram) ::: Nil)
     (CorepStar (RepCostar p) ::: Nil)
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)
L.colimitUniv (Sq
  (Corep Absurd ::: Nil) Nil (CorepStar Diagram ::: Nil) (p ::: Nil)
cocone Sq
  (Corep Absurd ::: Nil) Nil (CorepStar Diagram ::: Nil) (p ::: Nil)
-> Sq Nil Nil (p ::: Nil) (CorepStar (RepCostar p) ::: Nil)
-> Sq
     (Nil +++ (Corep Absurd ::: Nil))
     (Nil +++ Nil)
     (CorepStar Diagram ::: Nil)
     (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
||| (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 p a b -> CorepStar (RepCostar p) a b
p :~> CorepStar (RepCostar p)
forall {j} {k} (p :: j +-> k).
Representable p =>
p :~> CorepStar (RepCostar p)
wrapRepresentable) Sq Nil Nil (Initial ::: Nil) (CorepStar (RepCostar p) ::: Nil)
-> Sq Nil Nil (CorepStar (RepCostar p) ::: Nil) (p ::: Nil)
-> Sq (Nil +++ Nil) (Nil +++ Nil) (Initial ::: Nil) (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
||| (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 CorepStar (RepCostar p) a b -> p a b
CorepStar (RepCostar p) :~> p
forall {k1} {k} (p :: k1 +-> k).
Representable p =>
CorepStar (RepCostar p) :~> p
unwrapRepresentable