HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulepolysemy-1.9.2.0Haskell2010

Polysemy.State

  • 1 type
  • 16 values
  • Packagepolysemy-1.9.2.0
  • Exports17
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceState.hs

Effect

1 declaration
datadata State s (m :: k) a where
#

An effect for providing statefulness. Note that unlike mtl's Control.Monad.Trans.State.StateT, there is no restriction that the State effect corresponds necessarily to local state. It could could just as well be interrpeted in terms of HTTP requests or database access.

Interpreters which require statefulness can reinterpret themselves in terms of State, and subsequently call runState.

Constructors

Actions

5 declarations
valuegets :: Member (State s) r => (s -> a) -> Sem r a
#

Apply a function to the state and return the result.

Interpretations

10 declarations
valuestateToIO
  1. :: Member (Embed IO) r
  2. => s
  3. -> Sem (State s ': r) a
  4. -> Sem r (s, a)
#

Run an State effect in terms of operations in IO.

Internally, this simply creates a new IORef, passes it to runStateIORef, and then returns the result and the final value of the IORef.

Note: This is not safe in a concurrent setting, as modify isn't atomic. If you need operations over the state to be atomic, use atomicStateToIO instead.

Beware: As this uses an IORef internally, all other effects will have local state semantics in regards to State effects interpreted this way. For example, throw and catch will never revert puts, even if runError is used after stateToIO.

valuestateToST
  1. :: Member (Embed (ST st)) r
  2. => s
  3. -> Sem (State s ': r) a
  4. -> Sem r (s, a)
#

Run an State effect in terms of operations in ST.

Internally, this simply creates a new STRef, passes it to runStateSTRef, and then returns the result and the final value of the STRef.

Beware: As this uses an STRef internally, all other effects will have local state semantics in regards to State effects interpreted this way. For example, throw and catch will never revert puts, even if runError is used after stateToST.

When not using the plugin, one must introduce the existential st type to stateToST, so that the resulting type after runM can be resolved into forall st. ST st (s, a) for use with runST. Doing so requires -XScopedTypeVariables.

stResult :: forall s a. (s, a)
stResult = runST ( (runM $ stateToST @_ @st undefined $ pure undefined) :: forall st. ST st (s, a) )

Interoperation with MTL

1 declaration