module Effectful.State.Static.Shared
(
State
, runState
, evalState
, execState
, runStateMVar
, evalStateMVar
, execStateMVar
, get
, gets
, put
, state
, modify
, stateM
, modifyM
) where
import Control.Concurrent.MVar
import Effectful
import Effectful.Dispatch.Static
import Effectful.Dispatch.Static.Primitive
import Effectful.Internal.Utils
data State s :: Effect
type instance DispatchOf (State s) = Static NoSideEffects
newtype instance StaticRep (State s) = State (MVar' 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
s Eff (State s : es) a
m = do
MVar' s
v <- forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (MVar' a)
newMVar' s
s
a
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 (forall s. MVar' s -> StaticRep (State s)
State MVar' s
v) Eff (State s : es) a
m
(a
a, ) forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ (forall a. MVar' a -> IO a
readMVar' MVar' s
v)
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 Eff (State s : es) a
m = do
MVar' s
v <- forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (MVar' a)
newMVar' s
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
evalStaticRep (forall s. MVar' s -> StaticRep (State s)
State MVar' s
v) Eff (State s : es) a
m
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
s Eff (State s : es) a
m = do
MVar' s
v <- forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (MVar' a)
newMVar' s
s
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 (forall s. MVar' s -> StaticRep (State s)
State MVar' s
v) Eff (State s : es) a
m
forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. MVar' a -> IO a
readMVar' MVar' s
v
runStateMVar :: MVar s -> Eff (State s : es) a -> Eff es (a, s)
runStateMVar :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
MVar s -> Eff (State s : es) a -> Eff es (a, s)
runStateMVar MVar s
v Eff (State s : es) a
m = do
MVar' s
v' <- forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. MVar a -> IO (MVar' a)
toMVar' MVar s
v
a
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 (forall s. MVar' s -> StaticRep (State s)
State MVar' s
v') Eff (State s : es) a
m
(a
a, ) forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ (forall a. MVar a -> IO a
readMVar MVar s
v)
evalStateMVar :: MVar s -> Eff (State s : es) a -> Eff es a
evalStateMVar :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
MVar s -> Eff (State s : es) a -> Eff es a
evalStateMVar MVar s
v Eff (State s : es) a
m = do
MVar' s
v' <- forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. MVar a -> IO (MVar' a)
toMVar' MVar s
v
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 (forall s. MVar' s -> StaticRep (State s)
State MVar' s
v') Eff (State s : es) a
m
execStateMVar :: MVar s -> Eff (State s : es) a -> Eff es s
execStateMVar :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
MVar s -> Eff (State s : es) a -> Eff es s
execStateMVar MVar s
v Eff (State s : es) a
m = do
MVar' s
v' <- forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. MVar a -> IO (MVar' a)
toMVar' MVar s
v
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 (forall s. MVar' s -> StaticRep (State s)
State MVar' s
v') Eff (State s : es) a
m
forall a (es :: [(Type -> Type) -> Type -> Type]). IO a -> Eff es a
unsafeEff_ forall a b. (a -> b) -> a -> b
$ forall a. MVar a -> IO a
readMVar MVar s
v
get :: State s :> es => Eff es s
get :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
Eff es s
get = forall (es :: [(Type -> Type) -> Type -> Type]) a.
(Env es -> IO a) -> Eff es a
unsafeEff forall a b. (a -> b) -> a -> b
$ \Env es
es -> do
State MVar' s
v <- forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
(e :> es) =>
Env es -> IO (EffectRep (DispatchOf e) e)
getEnv Env es
es
forall a. MVar' a -> IO a
readMVar' MVar' s
v
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 forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (es :: [(Type -> Type) -> Type -> Type]) a.
(Env es -> IO a) -> Eff es a
unsafeEff forall a b. (a -> b) -> a -> b
$ \Env es
es -> do
State MVar' s
v <- forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
(e :> es) =>
Env es -> IO (EffectRep (DispatchOf e) e)
getEnv Env es
es
forall a. MVar' a -> (a -> IO a) -> IO ()
modifyMVar_' MVar' s
v forall a b. (a -> b) -> a -> b
$ \s
_ -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure 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 = forall (es :: [(Type -> Type) -> Type -> Type]) a.
(Env es -> IO a) -> Eff es a
unsafeEff forall a b. (a -> b) -> a -> b
$ \Env es
es -> do
State MVar' s
v <- forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
(e :> es) =>
Env es -> IO (EffectRep (DispatchOf e) e)
getEnv Env es
es
forall a r. MVar' a -> (a -> IO (a, r)) -> IO r
modifyMVar' MVar' s
v forall a b. (a -> b) -> a -> b
$ \s
s0 -> let (a
a, s
s) = s -> (a, s)
f s
s0 in forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (s
s, a
a)
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 = forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> (a, s)) -> Eff es a
state (\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 = forall (es :: [(Type -> Type) -> Type -> Type]) a.
(Env es -> IO a) -> Eff es a
unsafeEff forall a b. (a -> b) -> a -> b
$ \Env es
es -> do
State MVar' s
v <- forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]).
(e :> es) =>
Env es -> IO (EffectRep (DispatchOf e) e)
getEnv Env es
es
forall a r. MVar' a -> (a -> IO (a, r)) -> IO r
modifyMVar' MVar' s
v forall a b. (a -> b) -> a -> b
$ \s
s0 -> do
(a
a, s
s) <- forall (es :: [(Type -> Type) -> Type -> Type]) a.
Eff es a -> Env es -> IO a
unEff (s -> Eff es (a, s)
f s
s0) Env es
es
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (s
s, a
a)
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 = forall s (es :: [(Type -> Type) -> Type -> Type]) a.
(State s :> es) =>
(s -> Eff es (a, s)) -> Eff es a
stateM (\s
s -> ((), ) forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> Eff es s
f s
s)