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

Moduleextensible-effects-5.0.0.1Haskell2010

Control.Eff.State.OnDemand

Lazy state effect

  • 1 type
  • 10 values
datadata OnDemandState s v where
#

State, lazy (i.e., on-demand)

Extensible effects make it clear that where the computation is delayed (which I take as an advantage) and they do maintain the degree of extensibility (the delayed computation must be effect-closed, but the whole computation does not have to be).

Constructors

Instances3MonadBaseControl, Handle, StM
valuerunState
  1. :: s

    Initial state

  2. -> Eff (OnDemandState s ': r) w

    Effect incorporating State

  3. -> Eff r (w, s)

    Effect containing final state and a return value

#

Run a State effect

valuerunStateR :: s -> Eff (Writer s ': Reader s ': r) w -> Eff r (w, s)
#

A different representation of State: decomposing State into mutation (Writer) and Reading. We don't define any new effects: we just handle the existing ones. Thus we define a handler for two effects together.

valuerunStateBack0 :: Eff '[OnDemandState s] a -> (a, s)
#

Backwards state The overall state is represented with two attributes: the inherited getAttr and the synthesized putAttr. At the root node, putAttr becomes getAttr, tying the knot. As usual, the inherited attribute is the argument (i.e., the environment) and the synthesized is the result of the handler |go| below.

valuerunStateBack :: Eff '[OnDemandState s] a -> (a, s)
#

Another implementation, exploring Haskell's laziness to make putAttr also technically inherited, to accumulate the sequence of updates. This implementation is compatible with deep handlers, and lets us play with different notions of backwardness.

A different notion of backwards is realized if we change the Put handler slightly. How?