module Effectful.State.Static.Local
(
State
, runState
, evalState
, execState
, get
, gets
, put
, state
, modify
, stateM
, modifyM
) where
import Effectful
import Effectful.Dispatch.Static
data State s :: Effect
type instance DispatchOf (State s) = Static NoSideEffects
newtype instance StaticRep (State s) = State s
runState
:: s
-> Eff (State s : es) a
-> Eff es (a, s)
runState :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es (a, s)
runState s
s0 Eff (State s : es) a
m = do
(a
a, State s
s) <- StaticRep (State s)
-> Eff (State s : es) a -> Eff es (a, StaticRep (State s))
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(DispatchOf e ~ 'Static sideEffects, MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es (a, StaticRep e)
runStaticRep (s -> StaticRep (State s)
forall s. s -> StaticRep (State s)
State s
s0) Eff (State s : es) a
m
(a, s) -> Eff es (a, s)
forall a. a -> Eff es a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a
a, s
s)
evalState
:: s
-> Eff (State s : es) a
-> Eff es a
evalState :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es a
evalState s
s = StaticRep (State s) -> Eff (State s : es) a -> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(DispatchOf e ~ 'Static sideEffects, MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es a
evalStaticRep (s -> StaticRep (State s)
forall s. s -> StaticRep (State s)
State s
s)
execState
:: s
-> Eff (State s : es) a
-> Eff es s
execState :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
s -> Eff (State s : es) a -> Eff es s
execState s
s0 Eff (State s : es) a
m = do
State s
s <- StaticRep (State s)
-> Eff (State s : es) a -> Eff es (StaticRep (State s))
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(DispatchOf e ~ 'Static sideEffects, MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es (StaticRep e)
execStaticRep (s -> StaticRep (State s)
forall s. s -> StaticRep (State s)
State s
s0) Eff (State s : es) a
m
s -> Eff es s
forall a. a -> Eff es a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure s
s
get :: State s :> es => Eff es s
get :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
Eff es s
get = do
State s
s <- Eff es (StaticRep (State s))
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]).
(DispatchOf e ~ 'Static sideEffects, e :> es) =>
Eff es (StaticRep e)
getStaticRep
s -> Eff es s
forall a. a -> Eff es a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure s
s
gets
:: State s :> es
=> (s -> a)
-> Eff es a
gets :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(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]).
(State s :> es) =>
Eff es s
get
put :: State s :> es => s -> Eff es ()
put :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
s -> Eff es ()
put s
s = StaticRep (State s) -> Eff es ()
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]).
(DispatchOf e ~ 'Static sideEffects, e :> es) =>
StaticRep e -> Eff es ()
putStaticRep (s -> StaticRep (State s)
forall s. s -> StaticRep (State s)
State s
s)
state
:: State s :> es
=> (s -> (a, s))
-> Eff es a
state :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> (a, s)) -> Eff es a
state s -> (a, s)
f = (StaticRep (State s) -> (a, StaticRep (State s))) -> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(DispatchOf e ~ 'Static sideEffects, e :> es) =>
(StaticRep e -> (a, StaticRep e)) -> Eff es a
stateStaticRep ((StaticRep (State s) -> (a, StaticRep (State s))) -> Eff es a)
-> (StaticRep (State s) -> (a, StaticRep (State s))) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \(State s
s0) -> let (a
a, s
s) = s -> (a, s)
f s
s0 in (a
a, s -> StaticRep (State s)
forall s. s -> StaticRep (State s)
State s
s)
modify
:: State s :> es
=> (s -> s)
-> Eff es ()
modify :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
(s -> s) -> Eff es ()
modify s -> s
f = (s -> ((), s)) -> Eff es ()
forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> (a, s)) -> Eff es a
state ((s -> ((), s)) -> Eff es ()) -> (s -> ((), s)) -> Eff es ()
forall a b. (a -> b) -> a -> b
$ \s
s -> ((), s -> s
f s
s)
stateM
:: State s :> es
=> (s -> Eff es (a, s))
-> Eff es a
stateM :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> Eff es (a, s)) -> Eff es a
stateM s -> Eff es (a, s)
f = (StaticRep (State s) -> Eff es (a, StaticRep (State s)))
-> Eff es a
forall (e :: (Type -> Type) -> Type -> Type)
(sideEffects :: SideEffects)
(es :: [(Type -> Type) -> Type -> Type]) a.
(DispatchOf e ~ 'Static sideEffects, e :> es) =>
(StaticRep e -> Eff es (a, StaticRep e)) -> Eff es a
stateStaticRepM ((StaticRep (State s) -> Eff es (a, StaticRep (State s)))
-> Eff es a)
-> (StaticRep (State s) -> Eff es (a, StaticRep (State s)))
-> Eff es a
forall a b. (a -> b) -> a -> b
$ \(State s
s0) -> do
(a
a, s
s) <- s -> Eff es (a, s)
f s
s0
(a, StaticRep (State s)) -> Eff es (a, StaticRep (State s))
forall a. a -> Eff es a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a
a, s -> StaticRep (State s)
forall s. s -> StaticRep (State s)
State s
s)
modifyM
:: State s :> es
=> (s -> Eff es s)
-> Eff es ()
modifyM :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(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.
(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)