{-# LANGUAGE AllowAmbiguousTypes #-}

-- | Pointwise binary products as codiagonal-weighted limits.
module Proarrow.Squares.BinaryProduct where

import Proarrow.Category.Instance.Coproduct (COPRODUCT, Codiag, Lft, Rgt, (:++:))
import Proarrow.Core (type (+->))
import Proarrow.Limit.BinaryProduct (HasBinaryProducts)
import Proarrow.Path (Path (..))
import Proarrow.Profunctor.Instance.Composition ((:.:))
import Proarrow.Profunctor.Instance.Product ((:*:))
import Proarrow.Profunctor.Representable (Rep, Representable)
import Proarrow.Squares (Sq, hReencode, (|||))
import Proarrow.Squares.Limit qualified as L

-- | A pair of functors, viewed as one diagram on the disjoint union of their domains.
type Diagram :: (j +-> k) -> (j +-> k) -> COPRODUCT j j +-> k
type Diagram f g = Rep Codiag :.: (f :++: g)

-- | The projection cone. The vertical codiagonal weight selects either component.
--
-- > J-------codiag-------J+J
-- > |          |          |
-- > f*g>-------@--------->d
-- > |                     |
-- > K---------------------K
--
-- Here @d = Diagram f g@ and @f*g@ is the pointwise product.
product
  :: forall {j} {k} (f :: j +-> k) g
   . (HasBinaryProducts k, Representable f, Representable g)
  => Sq (Rep (Codiag :: COPRODUCT j j +-> j) ::: Nil) Nil (f :*: g ::: Nil) (Diagram f g ::: Nil)
product :: forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryProducts k, Representable f, Representable g) =>
Sq
  (Rep Codiag ::: Nil) Nil ((f :*: g) ::: Nil) (Diagram f g ::: Nil)
product = Sq
  Nil
  Nil
  ((f :*: g) ::: Nil)
  ((((Rep Codiag :.: (f :++: g)) :.: Rep Lft)
    :*: ((Rep Codiag :.: (f :++: g)) :.: Rep Rgt))
   ::: 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 :*: g) ::: Nil)
  ((((Rep Codiag :.: (f :++: g)) :.: Rep Lft)
    :*: ((Rep Codiag :.: (f :++: g)) :.: Rep Rgt))
   ::: Nil)
-> Sq
     (Rep Codiag ::: Nil)
     Nil
     ((((Rep Codiag :.: (f :++: g)) :.: Rep Lft)
       :*: ((Rep Codiag :.: (f :++: g)) :.: Rep Rgt))
      ::: Nil)
     ((Rep Codiag :.: (f :++: g)) ::: Nil)
-> Sq
     ((Rep Codiag ::: Nil) +++ Nil)
     (Nil +++ Nil)
     ((f :*: g) ::: Nil)
     ((Rep Codiag :.: (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
  (Rep Codiag ::: Nil)
  Nil
  ((((Rep Codiag :.: (f :++: g)) :.: Rep Lft)
    :*: ((Rep Codiag :.: (f :++: g)) :.: Rep Rgt))
   ::: Nil)
  ((Rep Codiag :.: (f :++: g)) ::: Nil)
Cone
  (Rep Codiag)
  (Limit (Rep Codiag) (Rep Codiag :.: (f :++: g)))
  (Rep Codiag :.: (f :++: g))
forall {i} {a} (j :: i +-> a) k (d :: i +-> k).
(HasLimits j k, Representable d) =>
Cone j (Limit j d) d
L.limit

-- | Factor a cone through the pointwise product.
-- Composing the result with 'product' recovers the input cone, uniquely.
-- The dotted inner square is the input; the outer square is the factor.
--
-- > J-------------------J
-- > |                   |
-- > |   J..codiag..J+J  |
-- > |   :     |     :   |
-- > p>--------@---->d  f*g
-- > |   :           :   |
-- > |   K...........K   |
-- > |                   |
-- > K-------------------K
pair
  :: forall {j} {k} (f :: j +-> k) g (p :: j +-> k)
   . (HasBinaryProducts k, Representable f, Representable g, Representable p)
  => Sq (Rep (Codiag :: COPRODUCT j j +-> j) ::: Nil) Nil (p ::: Nil) (Diagram f g ::: Nil)
  -> Sq Nil Nil (p ::: Nil) (f :*: g ::: Nil)
pair :: forall {j} {k} (f :: j +-> k) (g :: j +-> k) (p :: j +-> k).
(HasBinaryProducts k, Representable f, Representable g,
 Representable p) =>
Sq (Rep Codiag ::: Nil) Nil (p ::: Nil) (Diagram f g ::: Nil)
-> Sq Nil Nil (p ::: Nil) ((f :*: g) ::: Nil)
pair Sq (Rep Codiag ::: Nil) Nil (p ::: Nil) (Diagram f g ::: Nil)
cone = Sq (Rep Codiag ::: Nil) Nil (p ::: Nil) (Diagram f g ::: Nil)
-> Sq
     Nil Nil (p ::: Nil) (Limit (Rep Codiag) (Diagram f g) ::: Nil)
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)
L.limitUniv Sq (Rep Codiag ::: Nil) Nil (p ::: Nil) (Diagram f g ::: Nil)
cone Sq
  Nil
  Nil
  (p ::: Nil)
  (((Diagram f g :.: Rep Lft) :*: (Diagram f g :.: Rep Rgt)) ::: Nil)
-> Sq
     Nil
     Nil
     (((Diagram f g :.: Rep Lft) :*: (Diagram f g :.: Rep Rgt)) ::: Nil)
     ((f :*: g) ::: Nil)
-> Sq (Nil +++ Nil) (Nil +++ Nil) (p ::: Nil) ((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
  (((Diagram f g :.: Rep Lft) :*: (Diagram f g :.: Rep Rgt)) ::: Nil)
  ((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

-- | Select the first component of the projection cone.
fst
  :: forall {j} {k} (f :: j +-> k) g
   . (HasBinaryProducts k, Representable f, Representable g)
  => Sq Nil Nil (f :*: g ::: Nil) (f ::: Nil)
fst :: forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryProducts k, Representable f, Representable g) =>
Sq Nil Nil ((f :*: g) ::: Nil) (f ::: Nil)
fst = forall {j1} {j2} {k} (i :: j1 +-> j2) (j3 :: j2 +-> j1)
       (p :: j1 +-> k) (d :: j2 +-> k).
(Representable i, Representable j3, Representable p,
 Representable d, SameRep (j3 :.: i) Id) =>
Cone j3 p d -> Sq Nil Nil (p ::: Nil) (i ::: (d ::: Nil))
forall (i :: j +-> COPRODUCT j j) (j3 :: COPRODUCT j j +-> j)
       (p :: j +-> k) (d :: COPRODUCT j j +-> k).
(Representable i, Representable j3, Representable p,
 Representable d, SameRep (j3 :.: i) Id) =>
Cone j3 p d -> Sq Nil Nil (p ::: Nil) (i ::: (d ::: Nil))
L.coneAt @(Rep Lft) (forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryProducts k, Representable f, Representable g) =>
Sq
  (Rep Codiag ::: Nil) Nil ((f :*: g) ::: Nil) (Diagram f g ::: Nil)
forall (f :: j +-> k) (g :: j +-> k).
(HasBinaryProducts k, Representable f, Representable g) =>
Sq
  (Rep Codiag ::: Nil) Nil ((f :*: g) ::: Nil) (Diagram f g ::: Nil)
product @f @g) Sq Nil Nil ((f :*: g) ::: Nil) (Rep Lft ::: (Diagram f g ::: Nil))
-> Sq Nil Nil (Rep Lft ::: (Diagram f g ::: Nil)) (f ::: Nil)
-> Sq (Nil +++ Nil) (Nil +++ Nil) ((f :*: g) ::: Nil) (f ::: 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 (Rep Lft ::: (Diagram f g ::: Nil)) (f ::: 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

-- | Select the second component of the projection cone.
snd
  :: forall {j} {k} (f :: j +-> k) g
   . (HasBinaryProducts k, Representable f, Representable g)
  => Sq Nil Nil (f :*: g ::: Nil) (g ::: Nil)
snd :: forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryProducts k, Representable f, Representable g) =>
Sq Nil Nil ((f :*: g) ::: Nil) (g ::: Nil)
snd = forall {j1} {j2} {k} (i :: j1 +-> j2) (j3 :: j2 +-> j1)
       (p :: j1 +-> k) (d :: j2 +-> k).
(Representable i, Representable j3, Representable p,
 Representable d, SameRep (j3 :.: i) Id) =>
Cone j3 p d -> Sq Nil Nil (p ::: Nil) (i ::: (d ::: Nil))
forall (i :: j +-> COPRODUCT j j) (j3 :: COPRODUCT j j +-> j)
       (p :: j +-> k) (d :: COPRODUCT j j +-> k).
(Representable i, Representable j3, Representable p,
 Representable d, SameRep (j3 :.: i) Id) =>
Cone j3 p d -> Sq Nil Nil (p ::: Nil) (i ::: (d ::: Nil))
L.coneAt @(Rep Rgt) (forall {j} {k} (f :: j +-> k) (g :: j +-> k).
(HasBinaryProducts k, Representable f, Representable g) =>
Sq
  (Rep Codiag ::: Nil) Nil ((f :*: g) ::: Nil) (Diagram f g ::: Nil)
forall (f :: j +-> k) (g :: j +-> k).
(HasBinaryProducts k, Representable f, Representable g) =>
Sq
  (Rep Codiag ::: Nil) Nil ((f :*: g) ::: Nil) (Diagram f g ::: Nil)
product @f @g) Sq Nil Nil ((f :*: g) ::: Nil) (Rep Rgt ::: (Diagram f g ::: Nil))
-> Sq Nil Nil (Rep Rgt ::: (Diagram f g ::: Nil)) (g ::: Nil)
-> Sq (Nil +++ Nil) (Nil +++ Nil) ((f :*: g) ::: Nil) (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 (Rep Rgt ::: (Diagram f g ::: Nil)) (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