module Pandora.Paradigm.Schemes.TU where

import Pandora.Core.Functor (type (:.), type (:=), type (~>))
import Pandora.Pattern.Category ((.), ($))
import Pandora.Pattern.Functor.Covariant (Covariant ((<$>), (<$$>)))
import Pandora.Pattern.Functor.Contravariant (Contravariant)
import Pandora.Pattern.Functor.Applicative (Applicative ((<*>), (<**>)))
import Pandora.Pattern.Functor.Alternative (Alternative ((<+>)))
import Pandora.Pattern.Functor.Pointable (Pointable (point))
import Pandora.Pattern.Functor.Avoidable (Avoidable (empty))
import Pandora.Pattern.Functor.Extractable (Extractable (extract))
import Pandora.Pattern.Functor.Traversable (Traversable ((->>), (->>>)))
import Pandora.Pattern.Functor.Distributive (Distributive ((>>-)))
import Pandora.Pattern.Functor.Bindable (Bindable ((>>=), join))
import Pandora.Pattern.Transformer.Liftable (Liftable (lift))
import Pandora.Pattern.Transformer.Lowerable (Lowerable (lower))
import Pandora.Pattern.Transformer.Hoistable (Hoistable (hoist))
import Pandora.Paradigm.Controlflow.Effect.Interpreted (Interpreted (Primary, run, unite))

newtype TU ct cu t u a = TU (t :. u := a)

infixr 3 <:.>, >:.>, <:.<, >:.<

type (<:.>) = TU Covariant Covariant
type (>:.>) = TU Contravariant Covariant
type (<:.<) = TU Covariant Contravariant
type (>:.<) = TU Contravariant Contravariant

instance Interpreted (TU ct cu t u) where
	type Primary (TU ct cu t u) a = t :. u := a
	run :: TU ct cu t u a -> Primary (TU ct cu t u) a
run ~(TU (t :. u) := a
x) = (t :. u) := a
Primary (TU ct cu t u) a
x
	unite :: Primary (TU ct cu t u) a -> TU ct cu t u a
unite = Primary (TU ct cu t u) a -> TU ct cu t u a
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU

instance (Covariant t, Covariant u) => Covariant (t <:.> u) where
	a -> b
f <$> :: (a -> b) -> (<:.>) t u a -> (<:.>) t u b
<$> (<:.>) t u a
x = ((t :. u) := b) -> (<:.>) t u b
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU (((t :. u) := b) -> (<:.>) t u b)
-> ((t :. u) := b) -> (<:.>) t u b
forall (m :: * -> * -> *). Category m => m ~~> m
$ a -> b
f (a -> b) -> ((t :. u) := a) -> (t :. u) := b
forall (t :: * -> *) (u :: * -> *) a b.
(Covariant t, Covariant u) =>
(a -> b) -> ((t :. u) := a) -> (t :. u) := b
<$$> (<:.>) t u a -> Primary (t <:.> u) a
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run (<:.>) t u a
x

instance (Applicative t, Applicative u) => Applicative (t <:.> u) where
	TU (t :. u) := (a -> b)
f <*> :: (<:.>) t u (a -> b) -> (<:.>) t u a -> (<:.>) t u b
<*> TU (t :. u) := a
x = ((t :. u) := b) -> (<:.>) t u b
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU (((t :. u) := b) -> (<:.>) t u b)
-> ((t :. u) := b) -> (<:.>) t u b
forall (m :: * -> * -> *). Category m => m ~~> m
$ (t :. u) := (a -> b)
f ((t :. u) := (a -> b)) -> ((t :. u) := a) -> (t :. u) := b
forall (t :: * -> *) (u :: * -> *) a b.
(Applicative t, Applicative u) =>
((t :. u) := (a -> b)) -> ((t :. u) := a) -> (t :. u) := b
<**> (t :. u) := a
x

instance (Covariant u, Alternative t) => Alternative (t <:.> u) where
	(<:.>) t u a
x <+> :: (<:.>) t u a -> (<:.>) t u a -> (<:.>) t u a
<+> (<:.>) t u a
y = ((t :. u) := a) -> (<:.>) t u a
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU (((t :. u) := a) -> (<:.>) t u a)
-> ((t :. u) := a) -> (<:.>) t u a
forall (m :: * -> * -> *). Category m => m ~~> m
$ (<:.>) t u a -> Primary (t <:.> u) a
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run (<:.>) t u a
x ((t :. u) := a) -> ((t :. u) := a) -> (t :. u) := a
forall (t :: * -> *) a. Alternative t => t a -> t a -> t a
<+> (<:.>) t u a -> Primary (t <:.> u) a
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run (<:.>) t u a
y

instance (Covariant u, Avoidable t) => Avoidable (t <:.> u) where
	empty :: (<:.>) t u a
empty = ((t :. u) := a) -> (<:.>) t u a
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU (t :. u) := a
forall (t :: * -> *) a. Avoidable t => t a
empty

instance (Pointable t, Pointable u) => Pointable (t <:.> u) where
	point :: a :=> (t <:.> u)
point = ((t :. u) := a) -> TU Covariant Covariant t u a
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU (((t :. u) := a) -> TU Covariant Covariant t u a)
-> (a -> (t :. u) := a) -> a :=> (t <:.> u)
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. u a :=> t
forall (t :: * -> *) a. Pointable t => a :=> t
point (u a :=> t) -> (a -> u a) -> a -> (t :. u) := a
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. a -> u a
forall (t :: * -> *) a. Pointable t => a :=> t
point

instance (Extractable t, Extractable u) => Extractable (t <:.> u) where
	extract :: a <:= (t <:.> u)
extract = a <:= u
forall (t :: * -> *) a. Extractable t => a <:= t
extract (a <:= u)
-> (TU Covariant Covariant t u a -> u a) -> a <:= (t <:.> u)
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. u a <:= t
forall (t :: * -> *) a. Extractable t => a <:= t
extract (u a <:= t)
-> (TU Covariant Covariant t u a -> t (u a))
-> TU Covariant Covariant t u a
-> u a
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. TU Covariant Covariant t u a -> t (u a)
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run

instance (Traversable t, Traversable u) => Traversable (t <:.> u) where
	(<:.>) t u a
x ->> :: (<:.>) t u a -> (a -> u b) -> (u :. (t <:.> u)) := b
->> a -> u b
f = ((t :. u) := b) -> TU Covariant Covariant t u b
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU (((t :. u) := b) -> TU Covariant Covariant t u b)
-> u ((t :. u) := b) -> (u :. (t <:.> u)) := b
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
<$> (<:.>) t u a -> Primary (t <:.> u) a
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run (<:.>) t u a
x ((t :. u) := a) -> (a -> u b) -> u ((t :. u) := b)
forall (t :: * -> *) (u :: * -> *) (v :: * -> *) a b.
(Traversable t, Pointable u, Applicative u, Traversable v) =>
((v :. t) := a) -> (a -> u b) -> (u :. (v :. t)) := b
->>> a -> u b
f

instance (Bindable t, Distributive t, Bindable u) => Bindable (t <:.> u) where
	TU (t :. u) := a
x >>= :: (<:.>) t u a -> (a -> (<:.>) t u b) -> (<:.>) t u b
>>= a -> (<:.>) t u b
f = ((t :. u) := b) -> (<:.>) t u b
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU (((t :. u) := b) -> (<:.>) t u b)
-> ((t :. u) := b) -> (<:.>) t u b
forall (m :: * -> * -> *). Category m => m ~~> m
$ (t :. u) := a
x ((t :. u) := a) -> (u a -> (t :. u) := b) -> (t :. u) := b
forall (t :: * -> *) a b. Bindable t => t a -> (a -> t b) -> t b
>>= \u a
i -> ((u :. u) := b) -> u b
forall (t :: * -> *) a. Bindable t => ((t :. t) := a) -> t a
join (((u :. u) := b) -> u b) -> t ((u :. u) := b) -> (t :. u) := b
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
<$> u a
i u a -> (a -> (t :. u) := b) -> t ((u :. u) := b)
forall (t :: * -> *) (u :: * -> *) a b.
(Distributive t, Covariant u) =>
u a -> (a -> t b) -> (t :. u) := b
>>- (<:.>) t u b -> (t :. u) := b
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run ((<:.>) t u b -> (t :. u) := b)
-> (a -> (<:.>) t u b) -> a -> (t :. u) := b
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. a -> (<:.>) t u b
f

instance Pointable t => Liftable (TU Covariant Covariant t) where
	lift :: Covariant u => u ~> t <:.> u
	lift :: u ~> (t <:.> u)
lift = ((t :. u) := a) -> TU Covariant Covariant t u a
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU (((t :. u) := a) -> TU Covariant Covariant t u a)
-> (u a -> (t :. u) := a) -> u a -> TU Covariant Covariant t u a
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. u a -> (t :. u) := a
forall (t :: * -> *) a. Pointable t => a :=> t
point

instance Extractable t => Lowerable (TU Covariant Covariant t) where
	lower :: t <:.> u ~> u
	lower :: (<:.>) t u a -> u a
lower (TU (t :. u) := a
x) = u a <:= t
forall (t :: * -> *) a. Extractable t => a <:= t
extract (t :. u) := a
x

instance Covariant t => Hoistable (TU Covariant Covariant t) where
	hoist :: u ~> v -> (t <:.> u ~> t <:.> v)
	hoist :: (u ~> v) -> (t <:.> u) ~> (t <:.> v)
hoist u ~> v
f (TU (t :. u) := a
x) = ((t :. v) := a) -> TU Covariant Covariant t v a
forall k k k k (ct :: k) (cu :: k) (t :: k -> *) (u :: k -> k)
       (a :: k).
((t :. u) := a) -> TU ct cu t u a
TU (((t :. v) := a) -> TU Covariant Covariant t v a)
-> ((t :. v) := a) -> TU Covariant Covariant t v a
forall (m :: * -> * -> *). Category m => m ~~> m
$ u a -> v a
u ~> v
f (u a -> v a) -> ((t :. u) := a) -> (t :. v) := a
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
<$> (t :. u) := a
x