HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Moduleeffectful-core-2.7.1.2GHC2021

Effectful.State.Static.Shared

Support for access to a shared, mutable value of a particular type.

The value is shared between multiple threads. If you want each thread to manage its own version of the value, use Effectful.State.Static.Local.

Note: unlike the Control.Monad.Trans.State.StateT monad transformer from the transformers library, the State effect doesn't discard state updates when an exception is received:

Example3 expressions
import Control.Exception (ErrorCall)import Control.Monad.Catchimport Control.Monad.Trans.State.Strict qualified as S
Example1 expression
:{  (`S.execStateT` "Hi") . handle (\(_::ErrorCall) -> pure ()) $ do    S.modify (++ " there!")    error "oops":}"Hi"
Example1 expression
:{  runEff . execState "Hi" . handle (\(_::ErrorCall) -> pure ()) $ do    modify (++ " there!")    error "oops":}"Hi there!"
  • 1 type
  • 13 values

Effect

1 declaration
datadata State s (a :: Type -> Type) b
#

Provide access to a strict (WHNF), shared, mutable value of type s.

Instances2DispatchOf, StaticRep

Handlers

valuerunState :: HasCallStack => s -> Eff (State s ': es) a -> Eff es (a, s)
#

Run the State effect with the given initial state and return the final value along with the final state.

valuerunStateMVar
  1. :: HasCallStack
  2. => MVar s
  3. -> Eff (State s ': es) a
  4. -> Eff es (a, s)
#

Deprecated. If you need access to the state from outside of the State effect, manage an explicit MVar yourself.

Run the State effect with the given initial state MVar and return the final value along with the final state.

valueevalStateMVar :: HasCallStack => MVar s -> Eff (State s ': es) a -> Eff es a
#

Deprecated. If you need access to the state from outside of the State effect, manage an explicit MVar yourself.

Run the State effect with the given initial state MVar and return the final value, discarding the final state.

valueexecStateMVar :: HasCallStack => MVar s -> Eff (State s ': es) a -> Eff es s
#

Deprecated. If you need access to the state from outside of the State effect, manage an explicit MVar yourself.

Run the State effect with the given initial state MVar and return the final state, discarding the final value.

Operations

valuestate :: (HasCallStack, State s :> es) => (s -> (a, s)) -> Eff es a
#

Apply the function to the current state and return a value.

Note: this function gets an exclusive access to the state for its duration.

valuemodify :: (HasCallStack, State s :> es) => (s -> s) -> Eff es ()
#

Apply the function to the current state.

modify f ≡ state (\s -> ((), f s))

Note: this function gets an exclusive access to the state for its duration.

valuestateM :: (HasCallStack, State s :> es) => (s -> Eff es (a, s)) -> Eff es a
#

Deprecated. Operations of the same State effect used within the callback deadlock. Use a combination of get and put instead, or an explicit MVar if you need atomic updates.

Apply the monadic function to the current state and return a value.

Note: this function gets an exclusive access to the state for its duration.

valuemodifyM :: (HasCallStack, State s :> es) => (s -> Eff es s) -> Eff es ()
#

Deprecated. Operations of the same State effect used within the callback deadlock. Use a combination of get and put instead, or an explicit MVar if you need atomic updates.

Apply the monadic function to the current state.

modifyM f ≡ stateM (\s -> ((), ) <$> f s)

Note: this function gets an exclusive access to the state for its duration.