HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduleeffectful-core-2.3.0.1Haskell2010

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 thead 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:

Example1 expression
import qualified Control.Monad.Trans.State.Strict 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 :: 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.

valueevalState :: s -> Eff (State s ': es) a -> Eff es a
#

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

valueexecState :: s -> Eff (State s ': es) a -> Eff es s
#

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

Operations

valueget :: State s :> es => Eff es s
#

Fetch the current value of the state.

valueput :: State s :> es => s -> Eff es ()
#

Set the current state to the given value.

valuestate :: 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 :: 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 :: State s :> es => (s -> Eff es (a, s)) -> Eff es a
#

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 :: State s :> es => (s -> Eff es s) -> Eff es ()
#

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.