{-# LANGUAGE AllowAmbiguousTypes #-}

-- | Pointwise binary coproducts as codiagonal-weighted colimits.
module Proarrow.Squares.BinaryCoproduct where

import Proarrow.Category.Instance.Coproduct (COPRODUCT, Codiag, Lft, Rgt, (:++:))
import Proarrow.Colimit.BinaryCoproduct (HasBinaryCoproducts)
import Proarrow.Core (type (+->))
import Proarrow.Path (Path (..))
import Proarrow.Profunctor.Corepresentable (Corep)
import Proarrow.Profunctor.Instance.Composition ((:.:))
import Proarrow.Profunctor.Instance.Coproduct (Sum)
import Proarrow.Profunctor.Representable
  ( CorepStar
  , Rep
  , RepCostar
  , Representable
  , unwrapRepresentable
  , wrapRepresentable
  )
import Proarrow.Squares (Sq, hArr, hReencode, (|||))
import Proarrow.Squares.Limit qualified as L

-- | A pair of functors, encoded as a corepresentable diagram.
type Diagram :: (j +-> k) -> (j +-> k) -> k +-> COPRODUCT j j
type Diagram f g = (RepCostar f :++: RepCostar g) :.: Corep Codiag

-- | The injection cocone. The vertical codiagonal weight selects either component.
--
-- > J+J-----codiag--------J
-- > |          |          |
-- > d>---------@--------->s
-- > |                     |
-- > K---------------------K
--
-- Here @d = CorepStar (Diagram f g)@ and @s = Sum f g@.
coproduct
  :: forall {j} {k} (f :: j +-> k) g
   . (HasBinaryCoproducts k, Representable f, Representable g)
  => Sq (Corep (Codiag :: COPRODUCT j j +-> j) ::: Nil) Nil (CorepStar (Diagram f g) ::: Nil) (Sum f g ::: Nil)
coproduct :: forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryCoproducts k, Representable f, Representable g) =>
Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (Sum f g ::: Nil)
coproduct = Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (CorepStar
     ((Corep Lft :.: Diagram f g) :*: (Corep Rgt :.: Diagram f g))
   ::: Nil)
Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (CorepStar (Colimit (Corep Codiag) (Diagram f g)) ::: 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 Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (CorepStar
     ((Corep Lft :.: Diagram f g) :*: (Corep Rgt :.: Diagram f g))
   ::: Nil)
-> Sq
     Nil
     Nil
     (CorepStar
        ((Corep Lft :.: Diagram f g) :*: (Corep Rgt :.: Diagram f g))
      ::: Nil)
     (Sum f g ::: Nil)
-> Sq
     (Nil +++ (Corep Codiag ::: Nil))
     (Nil +++ Nil)
     (CorepStar (Diagram f g) ::: Nil)
     (Sum f g ::: 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
     ((Corep Lft :.: Diagram f g) :*: (Corep Rgt :.: Diagram f g))
   ::: Nil)
  (Sum f g ::: Nil)
forall {j} {k} (fs :: Path j k) (gs :: Path j k).
(IsTight fs, IsTight gs, SameRep (Fold fs) (Fold gs)) =>
Sq Nil Nil fs gs
hReencode

-- | Factor a cocone through the pointwise coproduct.
-- Composing 'coproduct' with the result recovers the input cocone, uniquely.
-- The dotted inner square is the input; the outer square is the factor.
--
-- > J-------------------J
-- > |                   |
-- > |  J+J.codiag...J   |
-- > |   :     |     :   |
-- > s   d>----@-------->p
-- > |   :           :   |
-- > |   K...........K   |
-- > |                   |
-- > K-------------------K
copair
  :: forall {j} {k} (f :: j +-> k) g (p :: j +-> k)
   . (HasBinaryCoproducts k, Representable f, Representable g, Representable p)
  => Sq (Corep (Codiag :: COPRODUCT j j +-> j) ::: Nil) Nil (CorepStar (Diagram f g) ::: Nil) (p ::: Nil)
  -> Sq Nil Nil (Sum f g ::: Nil) (p ::: Nil)
copair :: forall {j} {k} (f :: j +-> k) (g :: j +-> k) (p :: j +-> k).
(HasBinaryCoproducts k, Representable f, Representable g,
 Representable p) =>
Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (p ::: Nil)
-> Sq Nil Nil (Sum f g ::: Nil) (p ::: Nil)
copair Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (p ::: Nil)
cocone = Sq
  Nil
  Nil
  (Sum f g ::: Nil)
  (CorepStar (Colimit (Corep Codiag) (Diagram f g)) ::: Nil)
forall {j} {k} (fs :: Path j k) (gs :: Path j k).
(IsTight fs, IsTight gs, SameRep (Fold fs) (Fold gs)) =>
Sq Nil Nil fs gs
hReencode Sq
  Nil
  Nil
  (Sum f g ::: Nil)
  (CorepStar (Colimit (Corep Codiag) (Diagram f g)) ::: Nil)
-> Sq
     Nil
     Nil
     (CorepStar (Colimit (Corep Codiag) (Diagram f g)) ::: Nil)
     (CorepStar (RepCostar p) ::: Nil)
-> Sq
     (Nil +++ Nil)
     (Nil +++ Nil)
     (Sum f g ::: 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
||| Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (CorepStar (RepCostar p) ::: Nil)
-> Sq
     Nil
     Nil
     (CorepStar (Colimit (Corep Codiag) (Diagram f g)) ::: 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 Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (p ::: Nil)
cocone Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (p ::: Nil)
-> Sq Nil Nil (p ::: Nil) (CorepStar (RepCostar p) ::: Nil)
-> Sq
     (Nil +++ (Corep Codiag ::: Nil))
     (Nil +++ Nil)
     (CorepStar (Diagram f g) ::: 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 (Sum f g ::: Nil) (CorepStar (RepCostar p) ::: Nil)
-> Sq Nil Nil (CorepStar (RepCostar p) ::: Nil) (p ::: Nil)
-> Sq (Nil +++ Nil) (Nil +++ Nil) (Sum f g ::: 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

-- | Select the first component of the injection cocone.
lft
  :: forall {j} {k} (f :: j +-> k) g
   . (HasBinaryCoproducts k, Representable f, Representable g)
  => Sq Nil Nil (f ::: Nil) (Sum f g ::: Nil)
lft :: forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryCoproducts k, Representable f, Representable g) =>
Sq Nil Nil (f ::: Nil) (Sum f g ::: Nil)
lft = Sq
  Nil Nil (f ::: Nil) (Rep Lft ::: (CorepStar (Diagram f g) ::: Nil))
forall {j} {k} (fs :: Path j k) (gs :: Path j k).
(IsTight fs, IsTight gs, SameRep (Fold fs) (Fold gs)) =>
Sq Nil Nil fs gs
hReencode Sq
  Nil Nil (f ::: Nil) (Rep Lft ::: (CorepStar (Diagram f g) ::: Nil))
-> Sq
     Nil
     Nil
     (Rep Lft ::: (CorepStar (Diagram f g) ::: Nil))
     (Sum f g ::: Nil)
-> Sq (Nil +++ Nil) (Nil +++ Nil) (f ::: Nil) (Sum f g ::: 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 {j1} {j2} {k} (i :: j1 +-> j2) (j3 :: j1 +-> j2)
       (d :: j2 +-> k) (p :: j1 +-> k).
(Representable i, Corepresentable j3, Representable d,
 Representable p, SameRep (CorepStar j3 :.: i) Id) =>
Sq (j3 ::: Nil) Nil (d ::: Nil) (p ::: Nil)
-> Sq Nil Nil (i ::: (d ::: Nil)) (p ::: Nil)
forall (i :: j +-> COPRODUCT j j) (j3 :: j +-> COPRODUCT j j)
       (d :: COPRODUCT j j +-> k) (p :: j +-> k).
(Representable i, Corepresentable j3, Representable d,
 Representable p, SameRep (CorepStar j3 :.: i) Id) =>
Sq (j3 ::: Nil) Nil (d ::: Nil) (p ::: Nil)
-> Sq Nil Nil (i ::: (d ::: Nil)) (p ::: Nil)
L.coconeAt @(Rep Lft) (forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryCoproducts k, Representable f, Representable g) =>
Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (Sum f g ::: Nil)
forall (f :: j +-> k) (g :: j +-> k).
(HasBinaryCoproducts k, Representable f, Representable g) =>
Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (Sum f g ::: Nil)
coproduct @f @g)

-- | Select the second component of the injection cocone.
rgt
  :: forall {j} {k} (f :: j +-> k) g
   . (HasBinaryCoproducts k, Representable f, Representable g)
  => Sq Nil Nil (g ::: Nil) (Sum f g ::: Nil)
rgt :: forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryCoproducts k, Representable f, Representable g) =>
Sq Nil Nil (g ::: Nil) (Sum f g ::: Nil)
rgt = Sq
  Nil Nil (g ::: Nil) (Rep Rgt ::: (CorepStar (Diagram f g) ::: Nil))
forall {j} {k} (fs :: Path j k) (gs :: Path j k).
(IsTight fs, IsTight gs, SameRep (Fold fs) (Fold gs)) =>
Sq Nil Nil fs gs
hReencode Sq
  Nil Nil (g ::: Nil) (Rep Rgt ::: (CorepStar (Diagram f g) ::: Nil))
-> Sq
     Nil
     Nil
     (Rep Rgt ::: (CorepStar (Diagram f g) ::: Nil))
     (Sum f g ::: Nil)
-> Sq (Nil +++ Nil) (Nil +++ Nil) (g ::: Nil) (Sum f g ::: 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 {j1} {j2} {k} (i :: j1 +-> j2) (j3 :: j1 +-> j2)
       (d :: j2 +-> k) (p :: j1 +-> k).
(Representable i, Corepresentable j3, Representable d,
 Representable p, SameRep (CorepStar j3 :.: i) Id) =>
Sq (j3 ::: Nil) Nil (d ::: Nil) (p ::: Nil)
-> Sq Nil Nil (i ::: (d ::: Nil)) (p ::: Nil)
forall (i :: j +-> COPRODUCT j j) (j3 :: j +-> COPRODUCT j j)
       (d :: COPRODUCT j j +-> k) (p :: j +-> k).
(Representable i, Corepresentable j3, Representable d,
 Representable p, SameRep (CorepStar j3 :.: i) Id) =>
Sq (j3 ::: Nil) Nil (d ::: Nil) (p ::: Nil)
-> Sq Nil Nil (i ::: (d ::: Nil)) (p ::: Nil)
L.coconeAt @(Rep Rgt) (forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryCoproducts k, Representable f, Representable g) =>
Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (Sum f g ::: Nil)
forall (f :: j +-> k) (g :: j +-> k).
(HasBinaryCoproducts k, Representable f, Representable g) =>
Sq
  (Corep Codiag ::: Nil)
  Nil
  (CorepStar (Diagram f g) ::: Nil)
  (Sum f g ::: Nil)
coproduct @f @g)