HORIZON HASKELLDocslts/ghc-10.0.x8c2f8e82026-10-19Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8c2f8e8 · 2026-10-19

Modulefused-effects-1.1.2.7Haskell2010

Control.Effect.State

An effect that adds a mutable, updatable state value to a given computation.

Not all computations require a full-fledged state effect: read-only state is better served by Reader, and append-only state without reads is better served by Writer.

Predefined carriers:

  • 2 types
  • 1 class
  • 7 values

State effect

7 declarations
datadata State s (m :: Type -> Type) k where
#

Constructors

Instances9Algebra, …
valueget :: Has (State s) sig m => m s
#

Get the current state value.

runState a (get >>= k) = runState a (k a)
valuegets :: Has (State s) sig m => (s -> a) -> m a
#

Project a function out of the current state value.

gets f = fmap f get
valueput :: Has (State s) sig m => s -> m ()
#

Replace the state value with a new value.

runState a (put b >> m) = runState b m
valuemodify :: Has (State s) sig m => (s -> s) -> m ()
#

Replace the state value with the result of applying a function to the current state value. This is strict in the new state.

modify f = get >>= (put . f $!)
valuemodifyLazy :: Has (State s) sig m => (s -> s) -> m ()
#

Replace the state value with the result of applying a function to the current state value. This is lazy in the new state; injudicious use of this function may lead to space leaks.

modifyLazy f = get >>= put . f

Re-exports

3 declarations
classclass Monad m => Algebra (sig :: (Type -> Type) -> Type -> Type) (m :: Type -> Type) | m -> sig where
#

The class of carriers (results) for algebras (effect handlers) over signatures (effects), whose actions are given by the alg method.

Instances53Algebra, …
typetype Has (eff :: (Type -> Type) -> Type -> Type) (sig :: (Type -> Type) -> Type -> Type) (m :: Type -> Type) = (Members eff sig, Algebra sig m)
#

m is a carrier for sig containing eff.

Note that if eff is a sum, it will be decomposed into multiple Member constraints. While this technically allows one to combine multiple unrelated effects into a single Has constraint, doing so has two significant drawbacks:

  1. Due to a problem with recursive type families, this can lead to significantly slower compiles.

  2. It defeats ghc’s warnings for redundant constraints, and thus can lead to a proliferation of redundant constraints as code is changed.

valuerun :: Identity a -> a
#

Run an action exhausted of effects to produce its final result value.