{-# LANGUAGE UndecidableInstances #-}
module Pandora.Paradigm.Controlflow.Effect.Transformer.Monadic (Monadic (..), (:>) (..)) where

import Pandora.Pattern.Morphism.Straight (Straight (Straight))
import Pandora.Pattern.Semigroupoid ((.))
import Pandora.Pattern.Functor.Covariant (Covariant ((<-|-)))
import Pandora.Pattern.Functor.Semimonoidal (Semimonoidal (mult))
import Pandora.Pattern.Functor.Monoidal (Monoidal (unit))
import Pandora.Pattern.Functor.Distributive (Distributive ((-<<), (--<<)))
import Pandora.Pattern.Functor.Traversable (Traversable ((<<-)))
import Pandora.Pattern.Functor.Bindable (Bindable ((=<<), (==<<)))
import Pandora.Pattern.Functor.Extendable (Extendable ((<<=), (<<==)))
import Pandora.Pattern.Functor.Monad (Monad)
import Pandora.Pattern.Transformer.Liftable (Liftable (lift))
import Pandora.Pattern.Transformer.Hoistable (Hoistable ((/|\)))
import Pandora.Paradigm.Primary.Algebraic.Exponential (type (-->))
import Pandora.Paradigm.Primary.Algebraic.Product ((:*:)((:*:)))
import Pandora.Paradigm.Primary.Algebraic.Sum ((:+:))
import Pandora.Paradigm.Primary.Algebraic.One (One (One))
import Pandora.Paradigm.Primary.Algebraic (Pointable, point)
import Pandora.Paradigm.Controlflow.Effect.Interpreted (Schematic, Interpreted (Primary, run, unite, (!)))

class Interpreted m t => Monadic m t where
	{-# MINIMAL wrap #-}
	wrap :: Pointable u => m (t a) ((t :> u) a)

infixr 3 :>
newtype (:>) t u a = TM { (:>) t u a -> Schematic Monad t u a
tm :: Schematic Monad t u a }

instance Covariant (->) (->) (Schematic Monad t u) => Covariant (->) (->) (t :> u) where
	a -> b
f <-|- :: (a -> b) -> (:>) t u a -> (:>) t u b
<-|- TM Schematic Monad t u a
x = Schematic Monad t u b -> (:>) t u b
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t u b -> (:>) t u b)
-> Schematic Monad t u b -> (:>) t u b
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! a -> b
f (a -> b) -> Schematic Monad t u a -> Schematic Monad t u b
forall (source :: * -> * -> *) (target :: * -> * -> *)
       (t :: * -> *) a b.
Covariant source target t =>
source a b -> target (t a) (t b)
<-|- Schematic Monad t u a
x

instance Semimonoidal (-->) (:*:) (:*:) (Schematic Monad t u) => Semimonoidal (-->) (:*:) (:*:) (t :> u) where
	mult :: ((:>) t u a :*: (:>) t u b) --> (:>) t u (a :*: b)
mult = (((:>) t u a :*: (:>) t u b) -> (:>) t u (a :*: b))
-> ((:>) t u a :*: (:>) t u b) --> (:>) t u (a :*: b)
forall (v :: * -> * -> *) a e. v a e -> Straight v a e
Straight ((((:>) t u a :*: (:>) t u b) -> (:>) t u (a :*: b))
 -> ((:>) t u a :*: (:>) t u b) --> (:>) t u (a :*: b))
-> (((:>) t u a :*: (:>) t u b) -> (:>) t u (a :*: b))
-> ((:>) t u a :*: (:>) t u b) --> (:>) t u (a :*: b)
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! \(TM Schematic Monad t u a
f :*: TM Schematic Monad t u b
x) -> Schematic Monad t u (a :*: b) -> (:>) t u (a :*: b)
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (forall k (p :: * -> * -> *) (source :: * -> * -> *)
       (target :: k -> k -> k) (t :: k -> *) (a :: k) (b :: k).
Semimonoidal p source target t =>
p (source (t a) (t b)) (t (target a b))
forall (t :: * -> *) a b.
Semimonoidal (-->) (:*:) (:*:) t =>
(t a :*: t b) --> t (a :*: b)
mult @(-->) @(:*:) @(:*:) ((Schematic Monad t u a :*: Schematic Monad t u b)
 --> Schematic Monad t u (a :*: b))
-> (Schematic Monad t u a :*: Schematic Monad t u b)
-> Schematic Monad t u (a :*: b)
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! Schematic Monad t u a
f Schematic Monad t u a
-> Schematic Monad t u b
-> Schematic Monad t u a :*: Schematic Monad t u b
forall s a. s -> a -> s :*: a
:*: Schematic Monad t u b
x)

instance Monoidal (-->) (-->) (:*:) (:*:) (Schematic Monad t u) => Monoidal (-->) (-->) (:*:) (:*:) (t :> u) where
	unit :: Proxy (:*:) -> (Unit (:*:) --> a) --> (:>) t u a
unit Proxy (:*:)
_ = ((One --> a) -> (:>) t u a)
-> Straight (->) (One --> a) ((:>) t u a)
forall (v :: * -> * -> *) a e. v a e -> Straight v a e
Straight (((One --> a) -> (:>) t u a)
 -> Straight (->) (One --> a) ((:>) t u a))
-> ((One --> a) -> (:>) t u a)
-> Straight (->) (One --> a) ((:>) t u a)
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! Schematic Monad t u a -> (:>) t u a
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t u a -> (:>) t u a)
-> ((One --> a) -> Schematic Monad t u a)
-> (One --> a)
-> (:>) t u a
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. a -> Schematic Monad t u a
forall (t :: * -> *) a. Pointable t => a -> t a
point (a -> Schematic Monad t u a)
-> ((One --> a) -> a) -> (One --> a) -> Schematic Monad t u a
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. ((One -> a) -> One -> a
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! One
One) ((One -> a) -> a) -> ((One --> a) -> One -> a) -> (One --> a) -> a
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. (One --> a) -> One -> a
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
run

instance Semimonoidal (-->) (:*:) (:+:) (Schematic Monad t u) => Semimonoidal (-->) (:*:) (:+:) (t :> u) where
	mult :: ((:>) t u a :*: (:>) t u b) --> (:>) t u (a :+: b)
mult = (((:>) t u a :*: (:>) t u b) -> (:>) t u (a :+: b))
-> ((:>) t u a :*: (:>) t u b) --> (:>) t u (a :+: b)
forall (v :: * -> * -> *) a e. v a e -> Straight v a e
Straight ((((:>) t u a :*: (:>) t u b) -> (:>) t u (a :+: b))
 -> ((:>) t u a :*: (:>) t u b) --> (:>) t u (a :+: b))
-> (((:>) t u a :*: (:>) t u b) -> (:>) t u (a :+: b))
-> ((:>) t u a :*: (:>) t u b) --> (:>) t u (a :+: b)
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! \(TM Schematic Monad t u a
f :*: TM Schematic Monad t u b
x) -> Schematic Monad t u (a :+: b) -> (:>) t u (a :+: b)
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (forall k (p :: * -> * -> *) (source :: * -> * -> *)
       (target :: k -> k -> k) (t :: k -> *) (a :: k) (b :: k).
Semimonoidal p source target t =>
p (source (t a) (t b)) (t (target a b))
forall (t :: * -> *) a b.
Semimonoidal (-->) (:*:) (:+:) t =>
(t a :*: t b) --> t (a :+: b)
mult @(-->) @(:*:) @(:+:) ((Schematic Monad t u a :*: Schematic Monad t u b)
 --> Schematic Monad t u (a :+: b))
-> (Schematic Monad t u a :*: Schematic Monad t u b)
-> Schematic Monad t u (a :+: b)
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! Schematic Monad t u a
f Schematic Monad t u a
-> Schematic Monad t u b
-> Schematic Monad t u a :*: Schematic Monad t u b
forall s a. s -> a -> s :*: a
:*: Schematic Monad t u b
x)

instance Traversable (->) (->) (Schematic Monad t u) => Traversable (->) (->) (t :> u) where
	a -> u b
f <<- :: (a -> u b) -> (:>) t u a -> u ((:>) t u b)
<<- TM Schematic Monad t u a
x = Schematic Monad t u b -> (:>) t u b
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t u b -> (:>) t u b)
-> u (Schematic Monad t u b) -> u ((:>) t u b)
forall (source :: * -> * -> *) (target :: * -> * -> *)
       (t :: * -> *) a b.
Covariant source target t =>
source a b -> target (t a) (t b)
<-|- a -> u b
f (a -> u b) -> Schematic Monad t u a -> u (Schematic Monad t u b)
forall (source :: * -> * -> *) (target :: * -> * -> *)
       (t :: * -> *) (u :: * -> *) a b.
(Traversable source target t, Covariant source target u,
 Monoidal (Straight source) (Straight target) (:*:) (:*:) u) =>
source a (u b) -> target (t a) (u (t b))
<<- Schematic Monad t u a
x

instance Distributive (->) (->) (Schematic Monad t u) => Distributive (->) (->) (t :> u) where
	a -> (:>) t u b
f -<< :: (a -> (:>) t u b) -> u a -> (:>) t u (u b)
-<< u a
x = Schematic Monad t u (u b) -> (:>) t u (u b)
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t u (u b) -> (:>) t u (u b))
-> Schematic Monad t u (u b) -> (:>) t u (u b)
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! (:>) t u b -> Schematic Monad t u b
forall (t :: * -> *) (u :: * -> *) a.
(:>) t u a -> Schematic Monad t u a
tm ((:>) t u b -> Schematic Monad t u b)
-> (a -> (:>) t u b) -> a -> Schematic Monad t u b
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. a -> (:>) t u b
f (a -> Schematic Monad t u b) -> u a -> Schematic Monad t u (u b)
forall (source :: * -> * -> *) (target :: * -> * -> *)
       (t :: * -> *) (u :: * -> *) a b.
(Distributive source target t, Covariant source target u) =>
source a (t b) -> target (u a) (t (u b))
--<< u a
x

instance Bindable (->) (Schematic Monad t u) => Bindable (->) (t :> u) where
	a -> (:>) t u b
f =<< :: (a -> (:>) t u b) -> (:>) t u a -> (:>) t u b
=<< TM Schematic Monad t u a
x = Schematic Monad t u b -> (:>) t u b
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t u b -> (:>) t u b)
-> Schematic Monad t u b -> (:>) t u b
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! (:>) t u b -> Schematic Monad t u b
forall (t :: * -> *) (u :: * -> *) a.
(:>) t u a -> Schematic Monad t u a
tm ((:>) t u b -> Schematic Monad t u b)
-> (a -> (:>) t u b) -> a -> Schematic Monad t u b
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. a -> (:>) t u b
f (a -> Schematic Monad t u b)
-> Schematic Monad t u a -> Schematic Monad t u b
forall (source :: * -> * -> *) (t :: * -> *) a b.
Bindable source t =>
source a (t b) -> source (t a) (t b)
==<< Schematic Monad t u a
x

instance Extendable (->) (Schematic Monad t u) => Extendable (->) (t :> u) where
	(:>) t u a -> b
f <<= :: ((:>) t u a -> b) -> (:>) t u a -> (:>) t u b
<<= TM Schematic Monad t u a
x = Schematic Monad t u b -> (:>) t u b
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t u b -> (:>) t u b)
-> Schematic Monad t u b -> (:>) t u b
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! (:>) t u a -> b
f ((:>) t u a -> b)
-> (Schematic Monad t u a -> (:>) t u a)
-> Schematic Monad t u a
-> b
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. Schematic Monad t u a -> (:>) t u a
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t u a -> b)
-> Schematic Monad t u a -> Schematic Monad t u b
forall (source :: * -> * -> *) (t :: * -> *) a b.
Extendable source t =>
source (t a) b -> source (t a) (t b)
<<== Schematic Monad t u a
x

instance (Covariant (->) (->) (Schematic Monad t u), Monoidal (-->) (-->) (:*:) (:*:) (Schematic Monad t u), Bindable (->) (t :> u)) => Monad (->) (t :> u) where

instance Liftable (->) (Schematic Monad t) => Liftable (->) ((:>) t) where
	lift :: u a -> (:>) t u a
lift = Schematic Monad t u a -> (:>) t u a
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t u a -> (:>) t u a)
-> (u a -> Schematic Monad t u a) -> u a -> (:>) t u a
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. u a -> Schematic Monad t u a
forall (cat :: * -> * -> *) (t :: (* -> *) -> * -> *) (u :: * -> *)
       a.
(Liftable cat t, Covariant cat cat u) =>
cat (u a) (t u a)
lift

instance Hoistable (->) (Schematic Monad t) => Hoistable (->) ((:>) t) where
	forall a. u a -> v a
f /|\ :: (forall a. u a -> v a) -> forall a. (:>) t u a -> (:>) t v a
/|\ TM Schematic Monad t u a
x = Schematic Monad t v a -> (:>) t v a
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t v a -> (:>) t v a)
-> Schematic Monad t v a -> (:>) t v a
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! forall a. u a -> v a
f (forall a. u a -> v a)
-> Schematic Monad t u a -> Schematic Monad t v a
forall k (m :: * -> * -> *) (t :: (* -> *) -> k -> *) (u :: * -> *)
       (v :: * -> *).
(Hoistable m t, Covariant m m u) =>
(forall a. m (u a) (v a)) -> forall (a :: k). m (t u a) (t v a)
/|\ Schematic Monad t u a
x

instance (Interpreted (->) (Schematic Monad t u)) => Interpreted (->) (t :> u) where
	type Primary (t :> u) a = Primary (Schematic Monad t u) a
	run :: (:>) t u a -> Primary (t :> u) a
run ~(TM Schematic Monad t u a
x) = Schematic Monad t u a -> Primary (Schematic Monad t u) a
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
run Schematic Monad t u a
x
	unite :: Primary (t :> u) a -> (:>) t u a
unite = Schematic Monad t u a -> (:>) t u a
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM (Schematic Monad t u a -> (:>) t u a)
-> (Primary (Schematic Monad t u) a -> Schematic Monad t u a)
-> Primary (Schematic Monad t u) a
-> (:>) t u a
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. Primary (Schematic Monad t u) a -> Schematic Monad t u a
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (Primary t a) (t a)
unite