module Effectful.State.Dynamic
(
State(..)
, runStateLocal
, evalStateLocal
, execStateLocal
, runStateShared
, evalStateShared
, execStateShared
, get
, gets
, put
, state
, modify
, stateM
, modifyM
) where
import Effectful
import Effectful.Dispatch.Dynamic
import Effectful.State.Static.Local qualified as L
import Effectful.State.Static.Shared qualified as S
data State s :: Effect where
Get :: State s m s
Put :: s -> State s m ()
State :: (s -> (a, s)) -> State s m a
StateM :: (s -> m (a, s)) -> State s m a
type instance DispatchOf (State s) = Dynamic
runStateLocal :: s -> Eff (State s : es) a -> Eff es (a, s)
runStateLocal :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es (a, s)
runStateLocal s
s0 = (Eff (State s : es) a -> Eff es (a, s))
-> EffectHandler (State s) (State s : es)
-> Eff (State s : es) a
-> Eff es (a, s)
forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret (s -> Eff (State s : es) a -> Eff es (a, s)
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es (a, s)
L.runState s
s0) LocalEnv localEs (State s : es)
-> State s (Eff localEs) a -> Eff (State s : es) a
EffectHandler (State s) (State s : es)
forall s (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
LocalEnv localEs es -> State s (Eff localEs) a -> Eff es a
localState
evalStateLocal :: s -> Eff (State s : es) a -> Eff es a
evalStateLocal :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es a
evalStateLocal s
s0 = (Eff (State s : es) a -> Eff es a)
-> EffectHandler (State s) (State s : es)
-> Eff (State s : es) a
-> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret (s -> Eff (State s : es) a -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es a
L.evalState s
s0) LocalEnv localEs (State s : es)
-> State s (Eff localEs) a -> Eff (State s : es) a
EffectHandler (State s) (State s : es)
forall s (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
LocalEnv localEs es -> State s (Eff localEs) a -> Eff es a
localState
execStateLocal :: s -> Eff (State s : es) a -> Eff es s
execStateLocal :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es s
execStateLocal s
s0 = (Eff (State s : es) a -> Eff es s)
-> EffectHandler (State s) (State s : es)
-> Eff (State s : es) a
-> Eff es s
forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret (s -> Eff (State s : es) a -> Eff es s
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es s
L.execState s
s0) LocalEnv localEs (State s : es)
-> State s (Eff localEs) a -> Eff (State s : es) a
EffectHandler (State s) (State s : es)
forall s (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
LocalEnv localEs es -> State s (Eff localEs) a -> Eff es a
localState
localState
:: L.State s :> es
=> LocalEnv localEs es
-> State s (Eff localEs) a
-> Eff es a
localState :: forall s (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
LocalEnv localEs es -> State s (Eff localEs) a -> Eff es a
localState LocalEnv localEs es
env = \case
State s (Eff localEs) a
Get -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
Eff es s
L.get
Put s
s -> s -> Eff es ()
forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
s -> Eff es ()
L.put s
s
State s -> (a, s)
f -> (s -> (a, s)) -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> (a, s)) -> Eff es a
L.state s -> (a, s)
f
StateM s -> Eff localEs (a, s)
f -> LocalEnv localEs es
-> ((forall {r}. Eff localEs r -> Eff es r) -> Eff es a)
-> Eff es a
forall (es :: [(Type -> Type) -> Type -> Type])
(handlerEs :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, SharedSuffix es handlerEs) =>
LocalEnv localEs handlerEs
-> ((forall r. Eff localEs r -> Eff es r) -> Eff es a) -> Eff es a
localSeqUnlift LocalEnv localEs es
env (((forall {r}. Eff localEs r -> Eff es r) -> Eff es a) -> Eff es a)
-> ((forall {r}. Eff localEs r -> Eff es r) -> Eff es a)
-> Eff es a
forall a b. (a -> b) -> a -> b
$ \forall {r}. Eff localEs r -> Eff es r
unlift -> (s -> Eff es (a, s)) -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> Eff es (a, s)) -> Eff es a
L.stateM (Eff localEs (a, s) -> Eff es (a, s)
forall {r}. Eff localEs r -> Eff es r
unlift (Eff localEs (a, s) -> Eff es (a, s))
-> (s -> Eff localEs (a, s)) -> s -> Eff es (a, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Eff localEs (a, s)
f)
runStateShared :: s -> Eff (State s : es) a -> Eff es (a, s)
runStateShared :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es (a, s)
runStateShared s
s0 = (Eff (State s : es) a -> Eff es (a, s))
-> EffectHandler (State s) (State s : es)
-> Eff (State s : es) a
-> Eff es (a, s)
forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret (s -> Eff (State s : es) a -> Eff es (a, s)
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es (a, s)
S.runState s
s0) LocalEnv localEs (State s : es)
-> State s (Eff localEs) a -> Eff (State s : es) a
EffectHandler (State s) (State s : es)
forall s (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
LocalEnv localEs es -> State s (Eff localEs) a -> Eff es a
sharedState
evalStateShared :: s -> Eff (State s : es) a -> Eff es a
evalStateShared :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es a
evalStateShared s
s0 = (Eff (State s : es) a -> Eff es a)
-> EffectHandler (State s) (State s : es)
-> Eff (State s : es) a
-> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret (s -> Eff (State s : es) a -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es a
S.evalState s
s0) LocalEnv localEs (State s : es)
-> State s (Eff localEs) a -> Eff (State s : es) a
EffectHandler (State s) (State s : es)
forall s (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
LocalEnv localEs es -> State s (Eff localEs) a -> Eff es a
sharedState
execStateShared :: s -> Eff (State s : es) a -> Eff es s
execStateShared :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es s
execStateShared s
s0 = (Eff (State s : es) a -> Eff es s)
-> EffectHandler (State s) (State s : es)
-> Eff (State s : es) a
-> Eff es s
forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret (s -> Eff (State s : es) a -> Eff es s
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es s
S.execState s
s0) LocalEnv localEs (State s : es)
-> State s (Eff localEs) a -> Eff (State s : es) a
EffectHandler (State s) (State s : es)
forall s (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
LocalEnv localEs es -> State s (Eff localEs) a -> Eff es a
sharedState
sharedState
:: S.State s :> es
=> LocalEnv localEs es
-> State s (Eff localEs) a
-> Eff es a
sharedState :: forall s (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
LocalEnv localEs es -> State s (Eff localEs) a -> Eff es a
sharedState LocalEnv localEs es
env = \case
State s (Eff localEs) a
Get -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
Eff es s
S.get
Put s
s -> s -> Eff es ()
forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
s -> Eff es ()
S.put s
s
State s -> (a, s)
f -> (s -> (a, s)) -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> (a, s)) -> Eff es a
S.state s -> (a, s)
f
StateM s -> Eff localEs (a, s)
f -> LocalEnv localEs es
-> ((forall {r}. Eff localEs r -> Eff es r) -> Eff es a)
-> Eff es a
forall (es :: [(Type -> Type) -> Type -> Type])
(handlerEs :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, SharedSuffix es handlerEs) =>
LocalEnv localEs handlerEs
-> ((forall r. Eff localEs r -> Eff es r) -> Eff es a) -> Eff es a
localSeqUnlift LocalEnv localEs es
env (((forall {r}. Eff localEs r -> Eff es r) -> Eff es a) -> Eff es a)
-> ((forall {r}. Eff localEs r -> Eff es r) -> Eff es a)
-> Eff es a
forall a b. (a -> b) -> a -> b
$ \forall {r}. Eff localEs r -> Eff es r
unlift -> (s -> Eff es (a, s)) -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> Eff es (a, s)) -> Eff es a
S.stateM (Eff localEs (a, s) -> Eff es (a, s)
forall {r}. Eff localEs r -> Eff es r
unlift (Eff localEs (a, s) -> Eff es (a, s))
-> (s -> Eff localEs (a, s)) -> s -> Eff es (a, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Eff localEs (a, s)
f)
get
:: (HasCallStack, State s :> es)
=> Eff es s
get :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, State s :> es) =>
Eff es s
get = State s (Eff es) s -> Eff es s
forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send State s (Eff es) s
forall s (m :: Type -> Type). State s m s
Get
gets
:: (HasCallStack, State s :> es)
=> (s -> a)
-> Eff es a
gets :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, State s :> es) =>
(s -> a) -> Eff es a
gets s -> a
f = s -> a
f (s -> a) -> Eff es s -> Eff es a
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> Eff es s
forall s (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, State s :> es) =>
Eff es s
get
put
:: (HasCallStack, State s :> es)
=> s
-> Eff es ()
put :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, State s :> es) =>
s -> Eff es ()
put = State s (Eff es) () -> Eff es ()
forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (State s (Eff es) () -> Eff es ())
-> (s -> State s (Eff es) ()) -> s -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> State s (Eff es) ()
forall s (m :: Type -> Type). s -> State s m ()
Put
state
:: (HasCallStack, State s :> es)
=> (s -> (a, s))
-> Eff es a
state :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, State s :> es) =>
(s -> (a, s)) -> Eff es a
state = State s (Eff es) a -> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (State s (Eff es) a -> Eff es a)
-> ((s -> (a, s)) -> State s (Eff es) a)
-> (s -> (a, s))
-> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s -> (a, s)) -> State s (Eff es) a
forall s a (m :: Type -> Type). (s -> (a, s)) -> State s m a
State
modify
:: (HasCallStack, State s :> es)
=> (s -> s)
-> Eff es ()
modify :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, State s :> es) =>
(s -> s) -> Eff es ()
modify s -> s
f = (s -> ((), s)) -> Eff es ()
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, State s :> es) =>
(s -> (a, s)) -> Eff es a
state (\s
s -> ((), s -> s
f s
s))
stateM
:: (HasCallStack, State s :> es)
=> (s -> Eff es (a, s))
-> Eff es a
stateM :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, State s :> es) =>
(s -> Eff es (a, s)) -> Eff es a
stateM = State s (Eff es) a -> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send (State s (Eff es) a -> Eff es a)
-> ((s -> Eff es (a, s)) -> State s (Eff es) a)
-> (s -> Eff es (a, s))
-> Eff es a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s -> Eff es (a, s)) -> State s (Eff es) a
forall s (m :: Type -> Type) a. (s -> m (a, s)) -> State s m a
StateM
modifyM
:: (HasCallStack, State s :> es)
=> (s -> Eff es s)
-> Eff es ()
modifyM :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, State s :> es) =>
(s -> Eff es s) -> Eff es ()
modifyM s -> Eff es s
f = (s -> Eff es ((), s)) -> Eff es ()
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, State s :> es) =>
(s -> Eff es (a, s)) -> Eff es a
stateM (\s
s -> ((), ) (s -> ((), s)) -> Eff es s -> Eff es ((), s)
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> Eff es s
f s
s)