-- | The dynamically dispatched variant of the 'State' effect.
--
-- /Note:/ unless you plan to change interpretations at runtime, it's
-- recommended to use one of the statically dispatched variants,
-- i.e. "Effectful.State.Static.Local" or "Effectful.State.Static.Shared".
module Effectful.State.Dynamic
  ( -- * Effect
    State(..)

    -- ** Handlers

    -- *** Local
  , runStateLocal
  , evalStateLocal
  , execStateLocal

    -- *** Shared
  , runStateShared
  , evalStateShared
  , execStateShared

    -- ** Operations
  , 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

-- | Provide access to a mutable value of type @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

----------------------------------------
-- Local

-- | Run the 'State' effect with the given initial state and return the final
-- value along with the final state (via "Effectful.State.Static.Local").
runStateLocal :: HasCallStack => s -> Eff (State s : es) a -> Eff es (a, s)
runStateLocal :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
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.
(HasCallStack, 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.
HasCallStack =>
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]).
(State s :> es) =>
EffectHandler (State s) es
localState

-- | Run the 'State' effect with the given initial state and return the final
-- value, discarding the final state (via "Effectful.State.Static.Local").
evalStateLocal :: HasCallStack => s -> Eff (State s : es) a -> Eff es a
evalStateLocal :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
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.
(HasCallStack, 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.
HasCallStack =>
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]).
(State s :> es) =>
EffectHandler (State s) es
localState

-- | Run the 'State' effect with the given initial state and return the final
-- state, discarding the final value (via "Effectful.State.Static.Local").
execStateLocal :: HasCallStack => s -> Eff (State s : es) a -> Eff es s
execStateLocal :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
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.
(HasCallStack, 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.
HasCallStack =>
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]).
(State s :> es) =>
EffectHandler (State s) es
localState

localState :: L.State s :> es => EffectHandler (State s) es
localState :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
EffectHandler (State s) es
localState LocalEnv localEs es
env = \case
  State s (Eff localEs) a
Get      -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, State s :> es) =>
Eff es s
L.get
  Put s
s    -> s -> Eff es ()
forall s (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, 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.
(HasCallStack, 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.
(HasCallStack, 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)

----------------------------------------
-- Shared

-- | Run the 'State' effect with the given initial state and return the final
-- value along with the final state (via "Effectful.State.Static.Shared").
runStateShared :: HasCallStack => s -> Eff (State s : es) a -> Eff es (a, s)
runStateShared :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
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.
(HasCallStack, 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.
HasCallStack =>
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]).
(State s :> es) =>
EffectHandler (State s) es
sharedState

-- | Run the 'State' effect with the given initial state and return the final
-- value, discarding the final state (via "Effectful.State.Static.Shared").
evalStateShared :: HasCallStack => s -> Eff (State s : es) a -> Eff es a
evalStateShared :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
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.
(HasCallStack, 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.
HasCallStack =>
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]).
(State s :> es) =>
EffectHandler (State s) es
sharedState

-- | Run the 'State' effect with the given initial state and return the final
-- state, discarding the final value (via "Effectful.State.Static.Shared").
execStateShared :: HasCallStack => s -> Eff (State s : es) a -> Eff es s
execStateShared :: forall s (es :: [(Type -> Type) -> Type -> Type]) a.
HasCallStack =>
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.
(HasCallStack, 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.
HasCallStack =>
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]).
(State s :> es) =>
EffectHandler (State s) es
sharedState

sharedState :: S.State s :> es => EffectHandler (State s) es
sharedState :: forall s (es :: [(Type -> Type) -> Type -> Type]).
(State s :> es) =>
EffectHandler (State s) es
sharedState LocalEnv localEs es
env = \case
  State s (Eff localEs) a
Get      -> Eff es a
forall s (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, State s :> es) =>
Eff es s
S.get
  Put s
s    -> s -> Eff es ()
forall s (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, 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.
(HasCallStack, 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.
(HasCallStack, 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)

----------------------------------------
-- Operations

-- | Fetch the current value of the state.
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

-- | Get a function of the current state.
--
-- @'gets' f ≡ f '<$>' '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

-- | Set the current state to the given value.
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

-- | Apply the function to the current state and return a value.
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

-- | Apply the function to the current state.
--
-- @'modify' f ≡ 'state' (\\s -> ((), f s))@
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))

-- | Apply the monadic function to the current state and return a value.
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

-- | Apply the monadic function to the current state.
--
-- @'modifyM' f ≡ 'stateM' (\\s -> ((), ) '<$>' f s)@
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)