module Pandora.Paradigm.Schemes.UT where

import Pandora.Core.Functor (type (:.), type (:=), type (~>))
import Pandora.Pattern.Category ((.), ($), identity)
import Pandora.Pattern.Functor.Covariant (Covariant ((<$>), (<$$>)), Covariant_ ((-<$>-)), (-<$$>-))
import Pandora.Pattern.Functor.Contravariant (Contravariant)
import Pandora.Pattern.Functor.Applicative (Applicative ((<*>), (<**>)), Semimonoidal)
import Pandora.Pattern.Functor.Pointable (Pointable (point))
import Pandora.Pattern.Functor.Bindable (Bindable ((=<<)))
import Pandora.Pattern.Functor.Extractable (Extractable (extract))
import Pandora.Pattern.Functor.Traversable (Traversable ((<<-)))
import Pandora.Pattern.Transformer.Liftable (Liftable (lift))
import Pandora.Pattern.Transformer.Lowerable (Lowerable (lower))
import Pandora.Paradigm.Controlflow.Effect.Interpreted (Interpreted (Primary, run, unite))
import Pandora.Paradigm.Primary.Algebraic.Product ((:*:))

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

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

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

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

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

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

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

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

instance (Traversable t (->) (->), Bindable t (->), Semimonoidal u (:*:) (->) (->), Pointable u (->), Bindable u (->)) => Bindable (t <.:> u) (->) where
	a -> (<.:>) t u b
f =<< :: (a -> (<.:>) t u b) -> (<.:>) t u a -> (<.:>) t u b
=<< UT (u :. t) := a
x = ((u :. t) := b) -> (<.:>) t u b
forall k k k k (ct :: k) (cu :: k) (t :: k -> k) (u :: k -> *)
       (a :: k).
((u :. t) := a) -> UT ct cu t u a
UT (((u :. t) := b) -> (<.:>) t u b)
-> ((u :. t) := b) -> (<.:>) t u b
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ ((t b -> t b
forall (m :: * -> * -> *) a. Category m => m a a
identity (t b -> t b) -> t (t b) -> t b
forall (t :: * -> *) (source :: * -> * -> *) a b.
Bindable t source =>
source a (t b) -> source (t a) (t b)
=<<) (t (t b) -> t b) -> u (t (t b)) -> (u :. t) := b
forall (t :: * -> *) (source :: * -> * -> *)
       (target :: * -> * -> *) a b.
Covariant_ t source target =>
source a b -> target (t a) (t b)
-<$>-) (u (t (t b)) -> (u :. t) := b)
-> (t a -> u (t (t b))) -> t a -> (u :. t) := b
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. ((<.:>) t u b -> (u :. t) := b
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run ((<.:>) t u b -> (u :. t) := b)
-> (a -> (<.:>) t u b) -> a -> (u :. t) := b
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. a -> (<.:>) t u b
f (a -> (u :. t) := b) -> t a -> u (t (t b))
forall (t :: * -> *) (source :: * -> * -> *)
       (target :: * -> * -> *) (u :: * -> *) a b.
(Traversable t source target, Covariant_ u source target,
 Pointable u target, Semimonoidal u (:*:) source target) =>
source a (u b) -> target (t a) (u (t b))
<<-) (t a -> (u :. t) := b) -> ((u :. t) := a) -> (u :. t) := b
forall (t :: * -> *) (source :: * -> * -> *) a b.
Bindable t source =>
source a (t b) -> source (t a) (t b)
=<< (u :. t) := a
x

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

instance Pointable t (->) => Liftable (UT Covariant Covariant t) where
	lift :: Covariant_ u (->) (->) => u ~> t <.:> u
	lift :: u ~> (t <.:> u)
lift u a
x = ((u :. t) := a) -> UT Covariant Covariant t u a
forall k k k k (ct :: k) (cu :: k) (t :: k -> k) (u :: k -> *)
       (a :: k).
((u :. t) := a) -> UT ct cu t u a
UT (((u :. t) := a) -> UT Covariant Covariant t u a)
-> ((u :. t) := a) -> UT Covariant Covariant t u a
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ forall a. Pointable t (->) => a -> t a
forall (t :: * -> *) (source :: * -> * -> *) a.
Pointable t source =>
source a (t a)
point @_ @(->) (a -> t a) -> u a -> (u :. t) := a
forall (t :: * -> *) (source :: * -> * -> *)
       (target :: * -> * -> *) a b.
Covariant_ t source target =>
source a b -> target (t a) (t b)
-<$>- u a
x

instance Extractable t (->) => Lowerable (UT Covariant Covariant t) where
	lower :: Covariant_ u (->) (->) => t <.:> u ~> u
	lower :: (t <.:> u) ~> u
lower (UT (u :. t) := a
x) = forall a. Extractable t (->) => t a -> a
forall (t :: * -> *) (source :: * -> * -> *) a.
Extractable t source =>
source (t a) a
extract @_ @(->) (t a -> a) -> ((u :. t) := a) -> u a
forall (t :: * -> *) (source :: * -> * -> *)
       (target :: * -> * -> *) a b.
Covariant_ t source target =>
source a b -> target (t a) (t b)
-<$>- (u :. t) := a
x