{-# LANGUAGE AllowAmbiguousTypes #-}

-- | Cartesian terms, using the syntax and binders of "Proarrow.Tools.SMC" in 'PROD' @k@.
-- Its tensor is the product of @k@, independently of any monoidal structure on @k@ itself.
-- 'F' embeds a base object, @a ':**' b@ is a product, and @a ':->' b@ is the exponential
-- of 'PROD' @k@. 'toCCC' compiles a function on terms and unwraps the resulting arrow into @k@.
--
-- Each operation asks only for the structure it uses: products for pairing and binding,
-- 'Closed' @('PROD' k)@ for 'lam' and '(!)', coproducts for 'inl' and 'inr', and
-- 'Proarrow.Category.Monoidal.Distributive.Distributive' @('PROD' k)@ for 'caseOf'. Copying and
-- discarding are supplied by products. The patterns, contexts and @QualifiedDo@ notation are
-- those of SMC; see "Proarrow.Tools.SMC" for their use, including 'recast' across binding depths.
--
-- > import Prelude hiding ((**))
-- > import Proarrow.Tools.CCC (SYN (..), type F, toCCC, (**))
-- >
-- > swapProduct = toCCC @(F a :** F b) @(F b :** F a) (\(a, b) -> (b, a))
module Proarrow.Tools.CCC
  ( -- * Types
    SYN (I, (:**), (:->), (:&&), Top, (:||), Zero)
  , type F
  , Interp
  , KnownObj

    -- * Terms

    -- ** Products
  , Term
  , ToTerm
  , toTerm
  , toCCC
  , toCCC1
  , lift
  , liftF
  , (-<)
  , returnA
  , unit
  , (**)
  , split
  , Tuple
  , BuildTuple
  , recast
  , with
  , exl
  , exr
  , absorb

    -- ** Product exponentials
  , lam
  , Apply (..)

    -- ** Coproducts
  , inl
  , inr
  , caseOf
  , ifThenElse
  , Sum
  , absurd

    -- * Contexts
  , Ctx
  , KnownCtx
  , Union
  , Merge
  , Thin
  , BindVar

    -- * Do notation
  , (>>=)
  , return
  , fail
  , Bind
  , Binds

    -- * Examples
  , injectRight
  , swapProduct
  , applyPair
  , curryPair
  , flipCurried3
  , swapSum
  , caseEither
  ) where

import Proarrow.Category.Monoidal.Cartesian ()
import Proarrow.Category.Monoidal.Closed (Closed (..))
import Proarrow.Category.Monoidal.Distributive (Distributive)
import Proarrow.Colimit.BinaryCoproduct (HasBinaryCoproducts, type (||))
import Proarrow.Core (CategoryOf (..), Hom, Is, UN)
import Proarrow.Limit.BinaryProduct (HasProducts, PROD (..), Prod (..), type (&&))
import Proarrow.Tools.SMC hiding (F, Interp, lift, liftF, (-<))
import Proarrow.Tools.SMC qualified as SMC
import Prelude (uncurry)

-- | A base-category object in the SMC syntax over 'PROD' @k@.
type F :: k -> SYN (PROD k)
type F a = SMC.F (PR a)

-- | The base-category object a cartesian type expression stands for.
type Interp :: SYN (PROD k) -> k
type Interp a = UN PR (SMC.Interp a)

-- | Compile a function on terms into an arrow of @k@. The input arrives through an SMC pattern,
-- a variable, @()@, or a tuple, and the body's operations determine the additional structure needed.
{-# INLINE toCCC #-}
toCCC
  :: forall {k} (a :: SYN (PROD k)) b t cont
   . (HasProducts k, Binds (~>) 0 '[] a b t cont)
  => (t -> cont) -> Interp a ~> Interp b
toCCC :: forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
toCCC t -> cont
f = case forall {k} (a :: SYN k) (b :: SYN k) t cont.
(Monoidal k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(Monoidal (PROD k), Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
SMC.toSMC @a @b t -> cont
f of Prod a1 ~> b1
g -> a1 ~> b1
Interp a ~> Interp b
g

-- | 'toCCC' with both boundary objects embedded by 'F'. The surrounding morphism type
-- determines the input and output objects, so their type applications can be omitted.
-- Use 'toCCC' when a boundary needs other syntax, such as a product or function type.
{-# INLINE toCCC1 #-}
toCCC1
  :: forall {k} (a :: k) b t cont
   . (HasProducts k, Binds (~>) 0 '[] (F a) (F b) t cont)
  => (t -> cont) -> a ~> b
toCCC1 :: forall {k} (a :: k) (b :: k) t cont.
(HasProducts k, Binds (~>) 0 '[] (F a) (F b) t cont) =>
(t -> cont) -> a ~> b
toCCC1 = forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
toCCC @(F a) @(F b)

-- | Lift a base-category arrow onto terms. The type expressions can be composite, as in SMC's
-- 'Proarrow.Tools.SMC.lift'; use type applications when their interpretations do not determine them.
{-# INLINE lift #-}
lift
  :: forall {k} (a :: SYN (PROD k)) b d g t
   . (CategoryOf k, Is PR (SMC.Interp a), Is PR (SMC.Interp b), ToTerm (Hom (PROD k)) t d g a)
  => Interp a ~> Interp b -> t -> Term d g b
lift :: forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) (d :: Nat)
       (g :: Ctx (PROD k)) t.
(CategoryOf k, Is PR (Interp a), Is PR (Interp b),
 ToTerm (Hom (PROD k)) t d g a) =>
(Interp a ~> Interp b) -> t -> Term d g b
lift UN PR (Interp a) ~> UN PR (Interp b)
f = forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k)
       (p :: CAT k) t.
(Promonad p, ToTerm p t d g a) =>
p (Interp a) (Interp b) -> t -> TermP p d g b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) (d :: Nat)
       (g :: Ctx (PROD k)) (p :: CAT (PROD k)) t.
(Promonad p, ToTerm p t d g a) =>
p (Interp a) (Interp b) -> t -> TermP p d g b
SMC.lift @a @b ((UN PR (Interp a) ~> UN PR (Interp b))
-> Prod (~>) (PR (UN PR (Interp a))) (PR (UN PR (Interp b)))
forall {j} {k} (p :: j +-> k) (a1 :: k) (b1 :: j).
p a1 b1 -> Prod p (PR a1) (PR b1)
Prod UN PR (Interp a) ~> UN PR (Interp b)
f)

-- | Lift a base-category arrow onto a term or tuple of terms, embedding its result by 'F'.
-- The result object is inferred from the arrow. Use 'lift' for composite result syntax.
{-# INLINE liftF #-}
liftF
  :: forall {k} (a :: SYN (PROD k)) b t d g
   . (CategoryOf k, Is PR (SMC.Interp a), BuildTuple (Hom (PROD k)) t g a)
  => (Interp a ~> b) -> t -> Term d g (F b)
liftF :: forall {k} (a :: SYN (PROD k)) (b :: k) t (d :: Nat)
       (g :: Ctx (PROD k)).
(CategoryOf k, Is PR (Interp a),
 BuildTuple (Hom (PROD k)) t g a) =>
(Interp a ~> b) -> t -> Term d g (F b)
liftF UN PR (Interp a) ~> b
f = forall {k} (a :: SYN k) (b :: k) t (p :: CAT k) (d :: Nat)
       (g :: Ctx k).
(Promonad p, BuildTuple p t g a) =>
p (Interp a) b -> t -> TermP p d g (F b)
forall (a :: SYN (PROD k)) (b :: PROD k) t (p :: CAT (PROD k))
       (d :: Nat) (g :: Ctx (PROD k)).
(Promonad p, BuildTuple p t g a) =>
p (Interp a) b -> t -> TermP p d g (F b)
SMC.liftF @a @(PR b) ((UN PR (Interp a) ~> b) -> Prod (~>) (PR (UN PR (Interp a))) (PR b)
forall {j} {k} (p :: j +-> k) (a1 :: k) (b1 :: j).
p a1 b1 -> Prod p (PR a1) (PR b1)
Prod UN PR (Interp a) ~> b
f)

infixr 0 -<

-- | Arrow application, mirroring @f -< x@ in Haskell's arrow notation.
-- This is infix 'liftF': the input may be a term or tuple, and the result is embedded by 'F'.
-- The @Arrows@ extension must be disabled, since it reserves this symbol.
{-# INLINE (-<) #-}
(-<)
  :: forall {k} (a :: SYN (PROD k)) b t d g
   . (CategoryOf k, Is PR (SMC.Interp a), BuildTuple (Hom (PROD k)) t g a)
  => (Interp a ~> b) -> t -> Term d g (F b)
-< :: forall {k} (a :: SYN (PROD k)) (b :: k) t (d :: Nat)
       (g :: Ctx (PROD k)).
(CategoryOf k, Is PR (Interp a),
 BuildTuple (Hom (PROD k)) t g a) =>
(Interp a ~> b) -> t -> Term d g (F b)
(-<) = forall {k} (a :: SYN (PROD k)) (b :: k) t (d :: Nat)
       (g :: Ctx (PROD k)).
(CategoryOf k, Is PR (Interp a),
 BuildTuple (Hom (PROD k)) t g a) =>
(Interp a ~> b) -> t -> Term d g (F b)
forall (a :: SYN (PROD k)) (b :: k) t (d :: Nat)
       (g :: Ctx (PROD k)).
(CategoryOf k, Is PR (Interp a),
 BuildTuple (Hom (PROD k)) t g a) =>
(Interp a ~> b) -> t -> Term d g (F b)
liftF @a @b

-- | Inject as the right element of a sum.
--
-- >>> import Prelude (Bool (..))
-- >>> injectRight @Bool @Bool True
-- Right True
injectRight
  :: forall {k} (a :: k) b. (HasProducts k, HasBinaryCoproducts k, Ob a, Ob b) => a ~> (b || a)
injectRight :: forall {k} (a :: k) (b :: k).
(HasProducts k, HasBinaryCoproducts k, Ob a, Ob b) =>
a ~> (b || a)
injectRight = forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
toCCC @(F a) @(F b :|| F a) (\TermP (Prod (~>)) 1 '[ '(0, F a)] (F a)
x -> TermP (Prod (~>)) 1 '[ '(0, F a)] (F a)
-> TermP (Prod (~>)) 1 '[ '(0, F a)] (F b :|| F a)
forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k)
       (p :: k +-> k).
(Profunctor p, HasBinaryCoproducts k, KnownObj a, KnownObj b) =>
TermP p d g b -> TermP p d g (a :|| b)
inr TermP (Prod (~>)) 1 '[ '(0, F a)] (F a)
x)

-- | Swap a product. Only products are needed.
--
-- >>> import Prelude (Bool (..))
-- >>> swapProduct @Bool @Bool (True, False)
-- (False,True)
swapProduct
  :: forall {k} (a :: k) b. (HasProducts k, Ob a, Ob b) => (a && b) ~> (b && a)
swapProduct :: forall {k} (a :: k) (b :: k).
(HasProducts k, Ob a, Ob b) =>
(a && b) ~> (b && a)
swapProduct = forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
toCCC @(F a :** F b) @(F b :** F a) (\(TermP (Prod (~>)) 3 '[ '(1, F a)] (F a)
x, TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)
y) -> TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)
y TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)
-> TermP (Prod (~>)) 3 '[ '(1, F a)] (F a)
-> TermP
     (Prod (~>)) 3 (Union '[ '(2, F b)] '[ '(1, F a)]) (F b :** F a)
forall {k} (d :: Nat) (g1 :: Ctx k) (g2 :: Ctx k) (a :: SYN k)
       (b :: SYN k) (p :: CAT k).
(MonoidalProfunctor p, Merge g1 g2) =>
TermP p d g1 a
-> TermP p d g2 b -> TermP p d (Union g1 g2) (a :** b)
** TermP (Prod (~>)) 3 '[ '(1, F a)] (F a)
x)

-- | Apply a function to an argument, both bundled in a product.
--
-- >>> import Prelude (Bool (..), not)
-- >>> applyPair @Bool @Bool (not, True)
-- False
applyPair
  :: forall {k} (a :: k) b
   . (HasProducts k, Closed (PROD k), Ob a, Ob b)
  => Interp ((F a :-> F b) :** F a) ~> b
applyPair :: forall {k} (a :: k) (b :: k).
(HasProducts k, Closed (PROD k), Ob a, Ob b) =>
Interp ((F a :-> F b) :** F a) ~> b
applyPair = forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
toCCC @((F a :-> F b) :** F a) @(F b) ((TermP (Prod (~>)) 3 '[ '(1, F a :-> F b)] (F a :-> F b)
 -> TermP (Prod (~>)) 3 '[ '(2, F a)] (F a)
 -> TermP
      (Prod (~>))
      (1
       + ((2
           + PSize (TermP (Prod (~>)) 3 '[ '(1, F a :-> F b)] (F a :-> F b)))
          + PSize (TermP (Prod (~>)) 3 '[ '(2, F a)] (F a))))
      (Union '[ '(1, F a :-> F b)] '[ '(2, F a)])
      (F b))
-> (TermP (Prod (~>)) 3 '[ '(1, F a :-> F b)] (F a :-> F b),
    TermP (Prod (~>)) 3 '[ '(2, F a)] (F a))
-> TermP
     (Prod (~>))
     (1
      + ((2
          + PSize (TermP (Prod (~>)) 3 '[ '(1, F a :-> F b)] (F a :-> F b)))
         + PSize (TermP (Prod (~>)) 3 '[ '(2, F a)] (F a))))
     (Union '[ '(1, F a :-> F b)] '[ '(2, F a)])
     (F b)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry TermP (Prod (~>)) 3 '[ '(1, F a :-> F b)] (F a :-> F b)
-> TermP (Prod (~>)) 3 '[ '(2, F a)] (F a)
-> TermP
     (Prod (~>))
     (1
      + ((2
          + PSize (TermP (Prod (~>)) 3 '[ '(1, F a :-> F b)] (F a :-> F b)))
         + PSize (TermP (Prod (~>)) 3 '[ '(2, F a)] (F a))))
     (Union '[ '(1, F a :-> F b)] '[ '(2, F a)])
     (F b)
forall {k} (p :: CAT k) f x r.
(Apply p f x r, Closed k, MonoidalProfunctor p) =>
f -> x -> r
(!))

-- | Curry a pairing function.
--
-- >>> import Prelude (Bool (..))
-- >>> curryPair @Bool @Bool True False
-- (True,False)
curryPair
  :: forall {k} (a :: k) b
   . (HasProducts k, Closed (PROD k), Ob a, Ob b)
  => a ~> Interp (F b :-> (F a :** F b))
curryPair :: forall {k} (a :: k) (b :: k).
(HasProducts k, Closed (PROD k), Ob a, Ob b) =>
a ~> Interp (F b :-> (F a :** F b))
curryPair = forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
toCCC @(F a) @(F b :-> (F a :** F b)) (\TermP
  (Prod (~>))
  (2 + PSize (TermP (Prod (~>)) 2 '[ '(1, F b)] (F b)))
  '[ '(0, F a)]
  (F a)
x -> (TermP (Prod (~>)) 2 '[ '(1, F b)] (F b)
 -> (TermP
       (Prod (~>))
       (2 + PSize (TermP (Prod (~>)) 2 '[ '(1, F b)] (F b)))
       '[ '(0, F a)]
       (F a),
     TermP (Prod (~>)) 2 '[ '(1, F b)] (F b)))
-> TermP
     (~>)
     (1
      + PSize
          (TermP
             (Prod (~>))
             (2 + PSize (TermP (Prod (~>)) 2 '[ '(1, F b)] (F b)))
             '[ '(0, F a)]
             (F a)))
     '[ '(0, F a)]
     (F b :-> (F a :** F b))
forall {k} (d :: Nat) (r :: Ctx k) (a :: SYN k) (b :: SYN k) t
       cont.
(Closed k, Binds (~>) d r a b t cont) =>
(t -> cont) -> Term d r (a :-> b)
lam (TermP
  (Prod (~>))
  (2 + PSize (TermP (Prod (~>)) 2 '[ '(1, F b)] (F b)))
  '[ '(0, F a)]
  (F a)
x,))

-- | Flip the first two arguments of a curried function, supplying its third argument from the
-- second as well. Nested binders and a repeated variable use SMC's contexts.
--
-- >>> import Prelude (Bool (..))
-- >>> flipCurried3 @Bool @Bool @Bool (\_ a _ -> a) True False
-- True
flipCurried3
  :: forall {k} (a :: k) b c
   . (HasProducts k, Closed (PROD k), Ob a, Ob b, Ob c)
  => Interp (F b :-> F a :-> F b :-> F c) ~> Interp (F a :-> F b :-> F c)
flipCurried3 :: forall {k} (a :: k) (b :: k) (c :: k).
(HasProducts k, Closed (PROD k), Ob a, Ob b, Ob c) =>
Interp (F b :-> (F a :-> (F b :-> F c)))
~> Interp (F a :-> (F b :-> F c))
flipCurried3 =
  forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
toCCC @(F b :-> F a :-> F b :-> F c) @(F a :-> F b :-> F c)
    (\TermP
  (Prod (~>))
  3
  '[ '(0, F b :-> (F a :-> (F b :-> F c)))]
  (F b :-> (F a :-> (F b :-> F c)))
x -> (TermP (Prod (~>)) 3 '[ '(1, F a)] (F a)
 -> TermP
      (~>)
      (2 + PSize (TermP (Prod (~>)) 3 '[ '(1, F a)] (F a)))
      '[ '(1, F a), '(0, F b :-> (F a :-> (F b :-> F c)))]
      (F b :-> F c))
-> TermP
     (~>)
     (1
      + PSize
          (TermP
             (Prod (~>))
             3
             '[ '(0, F b :-> (F a :-> (F b :-> F c)))]
             (F b :-> (F a :-> (F b :-> F c)))))
     '[ '(0, F b :-> (F a :-> (F b :-> F c)))]
     (F a :-> (F b :-> F c))
forall {k} (d :: Nat) (r :: Ctx k) (a :: SYN k) (b :: SYN k) t
       cont.
(Closed k, Binds (~>) d r a b t cont) =>
(t -> cont) -> Term d r (a :-> b)
lam (\TermP (Prod (~>)) 3 '[ '(1, F a)] (F a)
y -> (TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)
 -> TermP
      (Prod (~>))
      (3 + PSize (TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)))
      (Union
         (Union
            (Union '[ '(0, F b :-> (F a :-> (F b :-> F c)))] '[ '(2, F b)])
            '[ '(1, F a)])
         '[ '(2, F b)])
      (F c))
-> TermP
     (~>)
     (2 + PSize (TermP (Prod (~>)) 3 '[ '(1, F a)] (F a)))
     '[ '(1, F a), '(0, F b :-> (F a :-> (F b :-> F c)))]
     (F b :-> F c)
forall {k} (d :: Nat) (r :: Ctx k) (a :: SYN k) (b :: SYN k) t
       cont.
(Closed k, Binds (~>) d r a b t cont) =>
(t -> cont) -> Term d r (a :-> b)
lam (\TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)
z -> TermP
  (Prod (~>))
  3
  '[ '(0, F b :-> (F a :-> (F b :-> F c)))]
  (F b :-> (F a :-> (F b :-> F c)))
x TermP
  (Prod (~>))
  3
  '[ '(0, F b :-> (F a :-> (F b :-> F c)))]
  (F b :-> (F a :-> (F b :-> F c)))
-> TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)
-> TermP
     (Prod (~>))
     3
     (Union '[ '(0, F b :-> (F a :-> (F b :-> F c)))] '[ '(2, F b)])
     (F a :-> (F b :-> F c))
forall {k} (p :: CAT k) f x r.
(Apply p f x r, Closed k, MonoidalProfunctor p) =>
f -> x -> r
! TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)
z TermP
  (Prod (~>))
  3
  (Union '[ '(0, F b :-> (F a :-> (F b :-> F c)))] '[ '(2, F b)])
  (F a :-> (F b :-> F c))
-> TermP (Prod (~>)) 3 '[ '(1, F a)] (F a)
-> TermP
     (Prod (~>))
     3
     (Union
        (Union '[ '(0, F b :-> (F a :-> (F b :-> F c)))] '[ '(2, F b)])
        '[ '(1, F a)])
     (F b :-> F c)
forall {k} (p :: CAT k) f x r.
(Apply p f x r, Closed k, MonoidalProfunctor p) =>
f -> x -> r
! TermP (Prod (~>)) 3 '[ '(1, F a)] (F a)
y TermP
  (Prod (~>))
  3
  (Union
     (Union '[ '(0, F b :-> (F a :-> (F b :-> F c)))] '[ '(2, F b)])
     '[ '(1, F a)])
  (F b :-> F c)
-> TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)
-> TermP
     (Prod (~>))
     (3 + PSize (TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)))
     (Union
        (Union
           (Union '[ '(0, F b :-> (F a :-> (F b :-> F c)))] '[ '(2, F b)])
           '[ '(1, F a)])
        '[ '(2, F b)])
     (F c)
forall {k} (p :: CAT k) f x r.
(Apply p f x r, Closed k, MonoidalProfunctor p) =>
f -> x -> r
! TermP (Prod (~>)) 3 '[ '(2, F b)] (F b)
z)))

-- | Swap a sum by case analysis. No exponentials are needed.
--
-- >>> import Prelude (Bool (..), Either (..))
-- >>> swapSum @Bool @Bool (Left True)
-- Right True
-- >>> swapSum @Bool @Bool (Right False)
-- Left False
swapSum
  :: forall {k} (a :: k) b
   . (HasProducts k, Distributive (PROD k), Ob a, Ob b)
  => (a || b) ~> (b || a)
swapSum :: forall {k} (a :: k) (b :: k).
(HasProducts k, Distributive (PROD k), Ob a, Ob b) =>
(a || b) ~> (b || a)
swapSum = forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
toCCC @(F a :|| F b) @(F b :|| F a) (\TermP (Prod (~>)) 1 '[ '(0, F a :|| F b)] (F a :|| F b)
x -> TermP (Prod (~>)) 1 '[ '(0, F a :|| F b)] (F a :|| F b)
-> (TermP (Prod (~>)) 2 '[ '(1, F a)] (F a)
    -> TermP (Prod (~>)) 2 '[ '(1, F a)] (F b :|| F a))
-> (TermP (Prod (~>)) 2 '[ '(1, F b)] (F b)
    -> TermP (Prod (~>)) 2 '[ '(1, F b)] (F b :|| F a))
-> TermP
     (Prod (~>))
     1
     (Union (Union '[] '[]) '[ '(0, F a :|| F b)])
     (F b :|| F a)
forall {k} (a :: SYN k) (b :: SYN k) (c :: SYN k) (d :: Nat)
       (g :: Ctx k) (r1 :: Ctx k) (r2 :: Ctx k) t1 cont1 t2 cont2
       (s :: SYN k) (p :: CAT k).
(Distributive k, Promonad p, DistributiveProfunctor p, Sum s a b,
 KnownObj a, KnownObj b, Binds p d r1 a c t1 cont1,
 Binds p d r2 b c t2 cont2, Thin (Union r1 r2) r1,
 Thin (Union r1 r2) r2, Merge (Union r1 r2) g) =>
TermP p d g s
-> (t1 -> cont1)
-> (t2 -> cont2)
-> TermP p d (Union (Union r1 r2) g) c
caseOf TermP (Prod (~>)) 1 '[ '(0, F a :|| F b)] (F a :|| F b)
x TermP (Prod (~>)) 2 '[ '(1, F a)] (F a)
-> TermP (Prod (~>)) 2 '[ '(1, F a)] (F b :|| F a)
forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k)
       (p :: k +-> k).
(Profunctor p, HasBinaryCoproducts k, KnownObj a, KnownObj b) =>
TermP p d g b -> TermP p d g (a :|| b)
inr TermP (Prod (~>)) 2 '[ '(1, F b)] (F b)
-> TermP (Prod (~>)) 2 '[ '(1, F b)] (F b :|| F a)
forall {k} (a :: SYN k) (b :: SYN k) (d :: Nat) (g :: Ctx k)
       (p :: k +-> k).
(Profunctor p, HasBinaryCoproducts k, KnownObj a, KnownObj b) =>
TermP p d g a -> TermP p d g (a :|| b)
inl)

-- | Apply whichever function matches the branch of a sum. The functions are shared with the
-- selected branch through the context.
--
-- >>> import Prelude (Bool (..), Either (..), id, not)
-- >>> caseEither @Bool @Bool @Bool (Left True, (not, id))
-- False
-- >>> caseEither @Bool @Bool @Bool (Right True, (not, id))
-- True
caseEither
  :: forall {k} (a :: k) b c
   . (HasProducts k, Closed (PROD k), Distributive (PROD k), Ob a, Ob b, Ob c)
  => Interp ((F a :|| F b) :** ((F a :-> F c) :** (F b :-> F c))) ~> c
caseEither :: forall {k} (a :: k) (b :: k) (c :: k).
(HasProducts k, Closed (PROD k), Distributive (PROD k), Ob a, Ob b,
 Ob c) =>
Interp ((F a :|| F b) :** ((F a :-> F c) :** (F b :-> F c))) ~> c
caseEither =
  forall k (a :: k) (b :: k) r.
(Closed k, Ob a, Ob b) =>
(Ob (a ~~> b) => r) -> r
withObExp @(PROD k) @(PR a) @(PR c)
    ( forall k (a :: k) (b :: k) r.
(Closed k, Ob a, Ob b) =>
(Ob (a ~~> b) => r) -> r
withObExp @(PROD k) @(PR b) @(PR c)
        ( forall {k} (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
forall (a :: SYN (PROD k)) (b :: SYN (PROD k)) t cont.
(HasProducts k, Binds (~>) 0 '[] a b t cont) =>
(t -> cont) -> Interp a ~> Interp b
toCCC @((F a :|| F b) :** ((F a :-> F c) :** (F b :-> F c))) @(F c)
            (\(TermP
  (Prod (~>))
  (1
   + (2
      + ((2
          + PSize (TermP (Prod (~>)) 6 '[ '(3, F a :-> F c)] (F a :-> F c)))
         + PSize
             (TermP (Prod (~>)) 6 '[ '(4, F b :-> F c)] (F b :-> F c)))))
  '[ '(1, F a :|| F b)]
  (F a :|| F b)
ab, (TermP (Prod (~>)) 6 '[ '(3, F a :-> F c)] (F a :-> F c)
ac, TermP (Prod (~>)) 6 '[ '(4, F b :-> F c)] (F b :-> F c)
bc)) -> TermP
  (Prod (~>))
  (1
   + (2
      + ((2
          + PSize (TermP (Prod (~>)) 6 '[ '(3, F a :-> F c)] (F a :-> F c)))
         + PSize
             (TermP (Prod (~>)) 6 '[ '(4, F b :-> F c)] (F b :-> F c)))))
  '[ '(1, F a :|| F b)]
  (F a :|| F b)
-> (TermP (Prod (~>)) 6 '[ '(5, F a)] (F a)
    -> TermP
         (Prod (~>))
         (6 + PSize (TermP (Prod (~>)) 6 '[ '(5, F a)] (F a)))
         (Union '[ '(3, F a :-> F c)] '[ '(5, F a)])
         (F c))
-> (TermP (Prod (~>)) 6 '[ '(5, F b)] (F b)
    -> TermP
         (Prod (~>))
         (6 + PSize (TermP (Prod (~>)) 6 '[ '(5, F b)] (F b)))
         (Union '[ '(4, F b :-> F c)] '[ '(5, F b)])
         (F c))
-> TermP
     (Prod (~>))
     (1
      + (2
         + ((2
             + PSize (TermP (Prod (~>)) 6 '[ '(3, F a :-> F c)] (F a :-> F c)))
            + PSize
                (TermP (Prod (~>)) 6 '[ '(4, F b :-> F c)] (F b :-> F c)))))
     (Union
        (Union '[ '(3, F a :-> F c)] '[ '(4, F b :-> F c)])
        '[ '(1, F a :|| F b)])
     (F c)
forall {k} (a :: SYN k) (b :: SYN k) (c :: SYN k) (d :: Nat)
       (g :: Ctx k) (r1 :: Ctx k) (r2 :: Ctx k) t1 cont1 t2 cont2
       (s :: SYN k) (p :: CAT k).
(Distributive k, Promonad p, DistributiveProfunctor p, Sum s a b,
 KnownObj a, KnownObj b, Binds p d r1 a c t1 cont1,
 Binds p d r2 b c t2 cont2, Thin (Union r1 r2) r1,
 Thin (Union r1 r2) r2, Merge (Union r1 r2) g) =>
TermP p d g s
-> (t1 -> cont1)
-> (t2 -> cont2)
-> TermP p d (Union (Union r1 r2) g) c
caseOf TermP
  (Prod (~>))
  (1
   + (2
      + ((2
          + PSize (TermP (Prod (~>)) 6 '[ '(3, F a :-> F c)] (F a :-> F c)))
         + PSize
             (TermP (Prod (~>)) 6 '[ '(4, F b :-> F c)] (F b :-> F c)))))
  '[ '(1, F a :|| F b)]
  (F a :|| F b)
ab (\TermP (Prod (~>)) 6 '[ '(5, F a)] (F a)
a -> TermP (Prod (~>)) 6 '[ '(3, F a :-> F c)] (F a :-> F c)
ac TermP (Prod (~>)) 6 '[ '(3, F a :-> F c)] (F a :-> F c)
-> TermP (Prod (~>)) 6 '[ '(5, F a)] (F a)
-> TermP
     (Prod (~>))
     (6 + PSize (TermP (Prod (~>)) 6 '[ '(5, F a)] (F a)))
     (Union '[ '(3, F a :-> F c)] '[ '(5, F a)])
     (F c)
forall {k} (p :: CAT k) f x r.
(Apply p f x r, Closed k, MonoidalProfunctor p) =>
f -> x -> r
! TermP (Prod (~>)) 6 '[ '(5, F a)] (F a)
a) (\TermP (Prod (~>)) 6 '[ '(5, F b)] (F b)
b -> TermP (Prod (~>)) 6 '[ '(4, F b :-> F c)] (F b :-> F c)
bc TermP (Prod (~>)) 6 '[ '(4, F b :-> F c)] (F b :-> F c)
-> TermP (Prod (~>)) 6 '[ '(5, F b)] (F b)
-> TermP
     (Prod (~>))
     (6 + PSize (TermP (Prod (~>)) 6 '[ '(5, F b)] (F b)))
     (Union '[ '(4, F b :-> F c)] '[ '(5, F b)])
     (F c)
forall {k} (p :: CAT k) f x r.
(Apply p f x r, Closed k, MonoidalProfunctor p) =>
f -> x -> r
! TermP (Prod (~>)) 6 '[ '(5, F b)] (F b)
b))
        )
    )