{-# OPTIONS_GHC -fno-warn-orphans #-}
module Pandora.Paradigm.Inventory.State where
import Pandora.Core.Functor (type (:.), type (:=))
import Pandora.Pattern.Category (identity, (.), ($))
import Pandora.Pattern.Functor.Covariant (Covariant ((<$>)))
import Pandora.Pattern.Functor.Invariant (Invariant ((<$<)))
import Pandora.Pattern.Functor.Pointable (Pointable (point))
import Pandora.Pattern.Functor.Applicative (Applicative ((<*>), (*>)))
import Pandora.Pattern.Functor.Traversable (Traversable ((->>)))
import Pandora.Pattern.Functor.Bindable (Bindable ((>>=)))
import Pandora.Pattern.Functor.Monad (Monad)
import Pandora.Pattern.Functor.Adjoint ((-|), (|-), ($|-))
import Pandora.Pattern.Functor.Bivariant ((<->))
import Pandora.Pattern.Functor.Divariant ((>->))
import Pandora.Paradigm.Primary.Transformer (Flip)
import Pandora.Paradigm.Controlflow.Effect.Adaptable (Adaptable (adapt))
import Pandora.Paradigm.Controlflow.Effect.Interpreted (Interpreted (Primary, run, unite, (||=)), Schematic)
import Pandora.Paradigm.Controlflow.Effect.Transformer.Monadic (Monadic (wrap), (:>) (TM))
import Pandora.Paradigm.Schemes.TUT (TUT (TUT), type (<:<.>:>))
import Pandora.Paradigm.Primary.Functor (Product ((:*:)), type (:*:), delta)
newtype State s a = State ((->) s :. (:*:) s := a)
instance Covariant (State s) where
a -> b
f <$> :: (a -> b) -> State s a -> State s b
<$> State s a
x = (((->) s :. (:*:) s) := b) -> State s b
forall s a. (((->) s :. (:*:) s) := a) -> State s a
State ((((->) s :. (:*:) s) := b) -> State s b)
-> (((->) s :. (:*:) s) := b) -> State s b
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ (a -> b) -> Product s a -> Product s b
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
(<$>) a -> b
f (Product s a -> Product s b)
-> (s -> Product s a) -> ((->) s :. (:*:) s) := b
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. State s a -> Primary (State s) a
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run State s a
x
instance Applicative (State s) where
State s (a -> b)
f <*> :: State s (a -> b) -> State s a -> State s b
<*> State s a
x = (((->) s :. (:*:) s) := b) -> State s b
forall s a. (((->) s :. (:*:) s) := a) -> State s a
State ((((->) s :. (:*:) s) := b) -> State s b)
-> (((->) s :. (:*:) s) := b) -> State s b
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ (Product (s :*: a) (a -> b)
-> ((a -> b) -> (s :*: a) -> s :*: b) -> s :*: b
forall (t :: * -> *) (u :: * -> *) a b.
Adjoint t u =>
t a -> (a -> u b) -> b
|- (a -> b) -> (s :*: a) -> s :*: b
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
(<$>)) (Product (s :*: a) (a -> b) -> s :*: b)
-> (s -> Product (s :*: a) (a -> b)) -> ((->) s :. (:*:) s) := b
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. (State s a -> Primary (State s) a
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run State s a
x (((->) s :. (:*:) s) := a)
-> ((a -> b) -> a -> b)
-> Product s (a -> b)
-> Product (s :*: a) (a -> b)
forall (v :: * -> * -> *) a b c d.
(Bivariant v, forall i. Covariant (v i)) =>
(a -> b) -> (c -> d) -> v a c -> v b d
<-> (a -> b) -> a -> b
forall (m :: * -> * -> *) a. Category m => m a a
identity) (Product s (a -> b) -> Product (s :*: a) (a -> b))
-> (s -> Product s (a -> b)) -> s -> Product (s :*: a) (a -> b)
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. State s (a -> b) -> Primary (State s) (a -> b)
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run State s (a -> b)
f
instance Pointable (State s) where
point :: a :=> State s
point = (((->) s :. (:*:) s) := a) -> State s a
forall s a. (((->) s :. (:*:) s) := a) -> State s a
State ((((->) s :. (:*:) s) := a) -> State s a)
-> (a -> ((->) s :. (:*:) s) := a) -> a :=> State s
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. (a -> (Product s a -> Product s a) -> ((->) s :. (:*:) s) := a
forall (t :: * -> *) (u :: * -> *) a b.
Adjoint t u =>
a -> (t a -> b) -> u b
-| Product s a -> Product s a
forall (m :: * -> * -> *) a. Category m => m a a
identity)
instance Bindable (State s) where
State s a
x >>= :: State s a -> (a -> State s b) -> State s b
>>= a -> State s b
f = (((->) s :. (:*:) s) := b) -> State s b
forall s a. (((->) s :. (:*:) s) := a) -> State s a
State ((((->) s :. (:*:) s) := b) -> State s b)
-> (((->) s :. (:*:) s) := b) -> State s b
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ State s a -> Primary (State s) a
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run State s a
x (((->) s :. (:*:) s) := a)
-> (a -> ((->) s :. (:*:) s) := b) -> ((->) s :. (:*:) s) := b
forall (t :: * -> *) (u :: * -> *) (v :: * -> *) a b.
(Adjoint t u, Covariant v) =>
v (t a) -> (a -> u b) -> v b
$|- State s b -> ((->) s :. (:*:) s) := b
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run (State s b -> ((->) s :. (:*:) s) := b)
-> (a -> State s b) -> a -> ((->) s :. (:*:) s) := b
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. a -> State s b
f
instance Monad (State s) where
instance Invariant (Flip State r) where
a -> b
f <$< :: (a -> b) -> (b -> a) -> Flip State r a -> Flip State r b
<$< b -> a
g = ((b -> a
g (b -> a)
-> (Product a r -> Product b r)
-> (a -> Product a r)
-> b
-> Product b r
forall (v :: * -> * -> *) a b c d.
Divariant v =>
(a -> b) -> (c -> d) -> v b c -> v a d
>-> (a -> b
f (a -> b) -> (r -> r) -> Product a r -> Product b r
forall (v :: * -> * -> *) a b c d.
(Bivariant v, forall i. Covariant (v i)) =>
(a -> b) -> (c -> d) -> v a c -> v b d
<-> r -> r
forall (m :: * -> * -> *) a. Category m => m a a
identity) (Primary (State a) r -> Primary (State b) r)
-> State a r -> State b r
forall (t :: * -> *) (u :: * -> *) a b.
(Interpreted t, Interpreted u) =>
(Primary t a -> Primary u b) -> t a -> u b
||=) (Primary (Flip State r) a -> Primary (Flip State r) b)
-> Flip State r a -> Flip State r b
forall (t :: * -> *) (u :: * -> *) a b.
(Interpreted t, Interpreted u) =>
(Primary t a -> Primary u b) -> t a -> u b
||=)
instance Interpreted (State s) where
type Primary (State s) a = (->) s :. (:*:) s := a
run :: State s a -> Primary (State s) a
run ~(State ((->) s :. (:*:) s) := a
x) = Primary (State s) a
((->) s :. (:*:) s) := a
x
unite :: Primary (State s) a -> State s a
unite = Primary (State s) a -> State s a
forall s a. (((->) s :. (:*:) s) := a) -> State s a
State
type instance Schematic Monad (State s) = (->) s <:<.>:> (:*:) s
instance Monadic (State s) where
wrap :: State s ~> (State s :> u)
wrap State s a
x = (<:<.>:>) ((->) s) ((:*:) s) u a -> (:>) (State s) u a
forall (t :: * -> *) (u :: * -> *) a.
Schematic Monad t u a -> (:>) t u a
TM ((<:<.>:>) ((->) s) ((:*:) s) u a -> (:>) (State s) u a)
-> ((s -> u (s :*: a)) -> (<:<.>:>) ((->) s) ((:*:) s) u a)
-> (s -> u (s :*: a))
-> (:>) (State s) u a
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. (s -> u (s :*: a)) -> (<:<.>:>) ((->) s) ((:*:) s) u a
forall k k k k k k (ct :: k) (ct' :: k) (cu :: k) (t :: k -> *)
(t' :: k -> k) (u :: k -> k) (a :: k).
((t :. (u :. t')) := a) -> TUT ct ct' cu t t' u a
TUT ((s -> u (s :*: a)) -> (:>) (State s) u a)
-> (s -> u (s :*: a)) -> (:>) (State s) u a
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ (s :*: a) :=> u
forall (t :: * -> *) a. Pointable t => a :=> t
point ((s :*: a) :=> u) -> (s -> s :*: a) -> s -> u (s :*: a)
forall (t :: * -> *) a b. Covariant t => (a -> b) -> t a -> t b
<$> State s a -> Primary (State s) a
forall (t :: * -> *) a. Interpreted t => t a -> Primary t a
run State s a
x
type Stateful s = Adaptable (State s)
current :: Stateful s t => t s
current :: t s
current = State s s -> t s
forall k (t :: k -> *) (u :: k -> *). Adaptable t u => t ~> u
adapt (State s s -> t s) -> State s s -> t s
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ (((->) s :. (:*:) s) := s) -> State s s
forall s a. (((->) s :. (:*:) s) := a) -> State s a
State ((->) s :. (:*:) s) := s
forall a. a -> a :*: a
delta
modify :: Stateful s t => (s -> s) -> t s
modify :: (s -> s) -> t s
modify s -> s
f = State s s -> t s
forall k (t :: k -> *) (u :: k -> *). Adaptable t u => t ~> u
adapt (State s s -> t s)
-> ((s -> Product s s) -> State s s) -> (s -> Product s s) -> t s
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. (s -> Product s s) -> State s s
forall s a. (((->) s :. (:*:) s) := a) -> State s a
State ((s -> Product s s) -> t s) -> (s -> Product s s) -> t s
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ \s
s -> let r :: s
r = s -> s
f s
s in s
r s -> s -> Product s s
forall s a. s -> a -> Product s a
:*: s
r
replace :: Stateful s t => s -> t s
replace :: s -> t s
replace s
s = State s s -> t s
forall k (t :: k -> *) (u :: k -> *). Adaptable t u => t ~> u
adapt (State s s -> t s)
-> ((s -> Product s s) -> State s s) -> (s -> Product s s) -> t s
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. (s -> Product s s) -> State s s
forall s a. (((->) s :. (:*:) s) := a) -> State s a
State ((s -> Product s s) -> t s) -> (s -> Product s s) -> t s
forall (m :: * -> * -> *) a b. Category m => m (m a b) (m a b)
$ \s
_ -> s
s s -> s -> Product s s
forall s a. s -> a -> Product s a
:*: s
s
reconcile :: (Bindable t, Stateful s t, Adaptable u t) => (s -> u s) -> t s
reconcile :: (s -> u s) -> t s
reconcile s -> u s
f = t s
forall s (t :: * -> *). Stateful s t => t s
current t s -> (s -> t s) -> t s
forall (t :: * -> *) a b. Bindable t => t a -> (a -> t b) -> t b
>>= u s -> t s
forall k (t :: k -> *) (u :: k -> *). Adaptable t u => t ~> u
adapt (u s -> t s) -> (s -> u s) -> s -> t s
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. s -> u s
f t s -> (s -> t s) -> t s
forall (t :: * -> *) a b. Bindable t => t a -> (a -> t b) -> t b
>>= s -> t s
forall s (t :: * -> *). Stateful s t => s -> t s
replace
type Memorable s t = (Pointable t, Applicative t, Stateful s t)
fold :: (Traversable t, Memorable s u) => (a -> s -> s) -> t a -> u s
fold :: (a -> s -> s) -> t a -> u s
fold a -> s -> s
op t a
struct = t a
struct t a -> (a -> u s) -> (u :. t) := s
forall (t :: * -> *) (u :: * -> *) a b.
(Traversable t, Pointable u, Applicative u) =>
t a -> (a -> u b) -> (u :. t) := b
->> (s -> s) -> u s
forall s (t :: * -> *). Stateful s t => (s -> s) -> t s
modify ((s -> s) -> u s) -> (a -> s -> s) -> a -> u s
forall (m :: * -> * -> *) b c a.
Category m =>
m b c -> m a b -> m a c
. a -> s -> s
op ((u :. t) := s) -> u s -> u s
forall (t :: * -> *) a b. Applicative t => t a -> t b -> t b
*> u s
forall s (t :: * -> *). Stateful s t => t s
current