module Pandora.Paradigm.Primary.Transformer.Reverse where

import Pandora.Pattern.Category ((.), ($), (/))
import Pandora.Pattern.Functor.Covariant (Covariant ((<$>)))
import Pandora.Pattern.Functor.Contravariant (Contravariant ((>$<)))
import Pandora.Pattern.Functor.Extractable (Extractable (extract))
import Pandora.Pattern.Functor.Pointable (Pointable (point))
import Pandora.Pattern.Functor.Applicative (Applicative ((<*>)))
import Pandora.Pattern.Functor.Traversable (Traversable ((->>)))
import Pandora.Pattern.Functor.Distributive (Distributive ((>>-)))
import Pandora.Pattern.Transformer.Liftable (Liftable (lift))
import Pandora.Pattern.Transformer.Lowerable (Lowerable (lower))
import Pandora.Pattern.Transformer.Hoistable (Hoistable (hoist))
import Pandora.Paradigm.Primary.Transformer.Backwards (Backwards (Backwards))
import Pandora.Paradigm.Controlflow.Effect.Interpreted (Interpreted (Primary, run, unite))

newtype Reverse t a = Reverse (t a)

instance Covariant t => Covariant (Reverse t) where
	a -> b
f <$> :: (a -> b) -> Reverse t a -> Reverse t b
<$> Reverse t a
x = t b -> Reverse t b
forall k (t :: k -> *) (a :: k). t a -> Reverse t a
Reverse (t b -> Reverse t b) -> t b -> Reverse t b
forall (m :: * -> * -> *). Category m => m ~~> m
/ a -> b
f (a -> b) -> t a -> t b
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
<$> t a
x

instance Pointable t => Pointable (Reverse t) where
	point :: a :=> Reverse t
point = t a -> Reverse t a
forall k (t :: k -> *) (a :: k). t a -> Reverse t a
Reverse (t a -> Reverse t a) -> (a -> t a) -> a :=> Reverse t
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. a -> t a
forall (t :: * -> *) a. Pointable t => a :=> t
point

instance Extractable t => Extractable (Reverse t) where
	extract :: a <:= Reverse t
extract (Reverse t a
x) = a <:= t
forall (t :: * -> *) a. Extractable t => a <:= t
extract t a
x

instance Applicative t => Applicative (Reverse t) where
	Reverse t (a -> b)
f <*> :: Reverse t (a -> b) -> Reverse t a -> Reverse t b
<*> Reverse t a
x = t b -> Reverse t b
forall k (t :: k -> *) (a :: k). t a -> Reverse t a
Reverse (t b -> Reverse t b) -> t b -> Reverse t b
forall (m :: * -> * -> *). Category m => m ~~> m
/ t (a -> b)
f t (a -> b) -> t a -> t b
forall (t :: * -> *) a b. Applicative t => t (a -> b) -> t a -> t b
<*> t a
x

instance Traversable t => Traversable (Reverse t) where
	Reverse t a
x ->> :: Reverse t a -> (a -> u b) -> (u :. Reverse t) := b
->> a -> u b
f = t b -> Reverse t b
forall k (t :: k -> *) (a :: k). t a -> Reverse t a
Reverse (t b -> Reverse t b) -> u (t b) -> (u :. Reverse t) := b
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
<$> Backwards u (t b) -> Primary (Backwards u) (t b)
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run (t a
x t a -> (a -> Backwards u b) -> Backwards u (t b)
forall (t :: * -> *) (u :: * -> *) a b.
(Traversable t, Pointable u, Applicative u) =>
t a -> (a -> u b) -> (u :. t) := b
->> u b -> Backwards u b
forall k (t :: k -> *) (a :: k). t a -> Backwards t a
Backwards (u b -> Backwards u b) -> (a -> u b) -> a -> Backwards u b
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. a -> u b
f)

instance Distributive t => Distributive (Reverse t) where
	u a
x >>- :: u a -> (a -> Reverse t b) -> (Reverse t :. u) := b
>>- a -> Reverse t b
f = t (u b) -> (Reverse t :. u) := b
forall k (t :: k -> *) (a :: k). t a -> Reverse t a
Reverse (t (u b) -> (Reverse t :. u) := b)
-> t (u b) -> (Reverse t :. u) := b
forall (m :: * -> * -> *). Category m => m ~~> m
$ u a
x u a -> (a -> t b) -> t (u b)
forall (t :: * -> *) (u :: * -> *) a b.
(Distributive t, Covariant u) =>
u a -> (a -> t b) -> (t :. u) := b
>>- Reverse t b -> t b
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run (Reverse t b -> t b) -> (a -> Reverse t b) -> a -> t b
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. a -> Reverse t b
f

instance Contravariant t => Contravariant (Reverse t) where
	a -> b
f >$< :: (a -> b) -> Reverse t b -> Reverse t a
>$< Reverse t b
x = t a -> Reverse t a
forall k (t :: k -> *) (a :: k). t a -> Reverse t a
Reverse (t a -> Reverse t a) -> t a -> Reverse t a
forall (m :: * -> * -> *). Category m => m ~~> m
/ a -> b
f (a -> b) -> t b -> t a
forall (t :: * -> *) a b. Contravariant t => (a -> b) -> t b -> t a
>$< t b
x

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

instance Liftable Reverse where
	lift :: u ~> Reverse u
lift = u a -> Reverse u a
forall k (t :: k -> *) (a :: k). t a -> Reverse t a
Reverse

instance Lowerable Reverse where
	lower :: Reverse u ~> u
lower = Reverse u a -> u a
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run

instance Hoistable Reverse where
	hoist :: (u ~> v) -> Reverse u ~> Reverse v
hoist u ~> v
f (Reverse u a
x) = v a -> Reverse v a
forall k (t :: k -> *) (a :: k). t a -> Reverse t a
Reverse (v a -> Reverse v a) -> v a -> Reverse v a
forall (m :: * -> * -> *). Category m => m ~~> m
/ u a -> v a
u ~> v
f u a
x