{-# LANGUAGE UndecidableInstances #-}

module Pandora.Paradigm.Controlflow.Effect.Transformer.Comonadic (Comonadic (..), (:<) (..)) where

import Pandora.Pattern.Semigroupoid ((.))
import Pandora.Pattern.Category (($))
import Pandora.Pattern.Morphism.Straight (Straight (Straight))
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.Comonad (Comonad)
import Pandora.Pattern.Transformer.Lowerable (Lowerable (lower))
import Pandora.Pattern.Transformer.Hoistable (Hoistable ((/|\)))
import Pandora.Paradigm.Primary.Algebraic.Exponential (type (-->))
import Pandora.Paradigm.Primary.Algebraic.Product ((:*:)((:*:)))
import Pandora.Paradigm.Primary.Algebraic.One (One (One))
import Pandora.Paradigm.Primary.Algebraic (Extractable, point)
import Pandora.Paradigm.Controlflow.Effect.Interpreted (Schematic, Interpreted (Primary, run, unite, (!)))

class Interpreted m t => Comonadic m t where
	{-# MINIMAL bring #-}
	bring :: Extractable u => m ((t :< u) a) (t a)

infixr 3 :<
newtype (:<) t u a = TC { (:<) t u a -> Schematic Comonad t u a
tc :: Schematic Comonad t u a }

instance Covariant (->) (->) (Schematic Comonad t u) => Covariant (->) (->) (t :< u) where
	a -> b
f <-|- :: (a -> b) -> (:<) t u a -> (:<) t u b
<-|- TC Schematic Comonad t u a
x = Schematic Comonad t u b -> (:<) t u b
forall (t :: * -> *) (u :: * -> *) a.
Schematic Comonad t u a -> (:<) t u a
TC (Schematic Comonad t u b -> (:<) t u b)
-> Schematic Comonad t u b -> (:<) t u b
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ a -> b
f (a -> b) -> Schematic Comonad t u a -> Schematic Comonad t u b
forall (source :: * -> * -> *) (target :: * -> * -> *)
       (t :: * -> *) a b.
Covariant source target t =>
source a b -> target (t a) (t b)
<-|- Schematic Comonad t u a
x

instance Semimonoidal (-->) (:*:) (:*:) (Schematic Comonad 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 :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ \(TC Schematic Comonad t u a
f :*: TC Schematic Comonad t u b
x) -> Schematic Comonad t u (a :*: b) -> (:<) t u (a :*: b)
forall (t :: * -> *) (u :: * -> *) a.
Schematic Comonad t u a -> (:<) t u a
TC (Schematic Comonad t u (a :*: b) -> (:<) t u (a :*: b))
-> Schematic Comonad t u (a :*: b) -> (:<) t u (a :*: b)
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ 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 Comonad t u a :*: Schematic Comonad t u b)
 --> Schematic Comonad t u (a :*: b))
-> (Schematic Comonad t u a :*: Schematic Comonad t u b)
-> Schematic Comonad t u (a :*: b)
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
! (Schematic Comonad t u a
f Schematic Comonad t u a
-> Schematic Comonad t u b
-> Schematic Comonad t u a :*: Schematic Comonad t u b
forall s a. s -> a -> s :*: a
:*: Schematic Comonad t u b
x)

instance Monoidal (-->) (-->) (:*:) (:*:) (Schematic Comonad t u) => Monoidal (-->) (-->) (:*:) (:*:) (t :< u) where
	unit :: Proxy (:*:) -> (Unit (:*:) --> a) --> (:<) t u a
unit Proxy (:*:)
_ = (Straight (->) One a -> (:<) t u a)
-> Straight (->) (Straight (->) One a) ((:<) t u a)
forall (v :: * -> * -> *) a e. v a e -> Straight v a e
Straight ((Straight (->) One a -> (:<) t u a)
 -> Straight (->) (Straight (->) One a) ((:<) t u a))
-> (Straight (->) One a -> (:<) t u a)
-> Straight (->) (Straight (->) One a) ((:<) t u a)
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ Schematic Comonad t u a -> (:<) t u a
forall (t :: * -> *) (u :: * -> *) a.
Schematic Comonad t u a -> (:<) t u a
TC (Schematic Comonad t u a -> (:<) t u a)
-> (Straight (->) One a -> Schematic Comonad t u a)
-> Straight (->) One a
-> (:<) t u a
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. a -> Schematic Comonad t u a
forall (t :: * -> *) a. Pointable t => a -> t a
point (a -> Schematic Comonad t u a)
-> (Straight (->) One a -> a)
-> Straight (->) One a
-> Schematic Comonad 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 :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ One
One) ((One -> a) -> a)
-> (Straight (->) One a -> One -> a) -> Straight (->) One a -> a
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. Straight (->) One a -> One -> a
forall (m :: * -> * -> *) (t :: * -> *) a.
Interpreted m t =>
m (t a) (Primary t a)
run

instance Traversable (->) (->) (Schematic Comonad t u) => Traversable (->) (->) (t :< u) where
	a -> u b
f <<- :: (a -> u b) -> (:<) t u a -> u ((:<) t u b)
<<- TC Schematic Comonad t u a
x = Schematic Comonad t u b -> (:<) t u b
forall (t :: * -> *) (u :: * -> *) a.
Schematic Comonad t u a -> (:<) t u a
TC (Schematic Comonad t u b -> (:<) t u b)
-> u (Schematic Comonad 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 Comonad t u a -> u (Schematic Comonad 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 Comonad t u a
x

instance Distributive (->) (->) (Schematic Comonad 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 Comonad t u (u b) -> (:<) t u (u b)
forall (t :: * -> *) (u :: * -> *) a.
Schematic Comonad t u a -> (:<) t u a
TC (Schematic Comonad t u (u b) -> (:<) t u (u b))
-> Schematic Comonad t u (u b) -> (:<) t u (u b)
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ (:<) t u b -> Schematic Comonad t u b
forall (t :: * -> *) (u :: * -> *) a.
(:<) t u a -> Schematic Comonad t u a
tc ((:<) t u b -> Schematic Comonad t u b)
-> (a -> (:<) t u b) -> a -> Schematic Comonad 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 Comonad t u b)
-> u a -> Schematic Comonad 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 Comonad t u) => Bindable (->) (t :< u) where
	a -> (:<) t u b
f =<< :: (a -> (:<) t u b) -> (:<) t u a -> (:<) t u b
=<< TC Schematic Comonad t u a
x = Schematic Comonad t u b -> (:<) t u b
forall (t :: * -> *) (u :: * -> *) a.
Schematic Comonad t u a -> (:<) t u a
TC (Schematic Comonad t u b -> (:<) t u b)
-> Schematic Comonad t u b -> (:<) t u b
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ (:<) t u b -> Schematic Comonad t u b
forall (t :: * -> *) (u :: * -> *) a.
(:<) t u a -> Schematic Comonad t u a
tc ((:<) t u b -> Schematic Comonad t u b)
-> (a -> (:<) t u b) -> a -> Schematic Comonad 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 Comonad t u b)
-> Schematic Comonad t u a -> Schematic Comonad t u b
forall (source :: * -> * -> *) (t :: * -> *) a b.
Bindable source t =>
source a (t b) -> source (t a) (t b)
=<< Schematic Comonad t u a
x

instance Extendable (->) (Schematic Comonad t u) => Extendable (->) (t :< u) where
	(:<) t u a -> b
f <<= :: ((:<) t u a -> b) -> (:<) t u a -> (:<) t u b
<<= TC Schematic Comonad t u a
x = Schematic Comonad t u b -> (:<) t u b
forall (t :: * -> *) (u :: * -> *) a.
Schematic Comonad t u a -> (:<) t u a
TC (Schematic Comonad t u b -> (:<) t u b)
-> Schematic Comonad t u b -> (:<) t u b
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ (:<) t u a -> b
f ((:<) t u a -> b)
-> (Schematic Comonad t u a -> (:<) t u a)
-> Schematic Comonad t u a
-> b
forall (m :: * -> * -> *) b c a.
Semigroupoid m =>
m b c -> m a b -> m a c
. Schematic Comonad t u a -> (:<) t u a
forall (t :: * -> *) (u :: * -> *) a.
Schematic Comonad t u a -> (:<) t u a
TC (Schematic Comonad t u a -> b)
-> Schematic Comonad t u a -> Schematic Comonad t u b
forall (source :: * -> * -> *) (t :: * -> *) a b.
Extendable source t =>
source (t a) b -> source (t a) (t b)
<<= Schematic Comonad t u a
x

instance (Extractable (t :< u), Extendable (->) (t :< u)) => Comonad (->) (t :< u) where

instance Lowerable (->) (Schematic Comonad t) => Lowerable (->) ((:<) t) where
	lower :: (:<) t u a -> u a
lower (TC Schematic Comonad t u a
x) = Schematic Comonad t u a -> u a
forall (cat :: * -> * -> *) (t :: (* -> *) -> * -> *) (u :: * -> *)
       a.
(Lowerable cat t, Covariant cat cat u) =>
cat (t u a) (u a)
lower Schematic Comonad t u a
x

instance Hoistable (->) (Schematic Comonad 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
/|\ TC Schematic Comonad t u a
x = Schematic Comonad t v a -> (:<) t v a
forall (t :: * -> *) (u :: * -> *) a.
Schematic Comonad t u a -> (:<) t u a
TC (Schematic Comonad t v a -> (:<) t v a)
-> Schematic Comonad t v a -> (:<) t v a
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ forall a. u a -> v a
f (forall a. u a -> v a)
-> Schematic Comonad t u a -> Schematic Comonad 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 Comonad t u a
x

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