Safe Haskell | Trustworthy |
---|---|
Language | Haskell2010 |
Lazy state effect
- data State s v where
- get :: Member (State s) r => Eff r s
- put :: Member (State s) r => s -> Eff r ()
- onDemand :: Member (State s) r => Eff '[State s] v -> Eff r v
- runState' :: Eff (State s ': r) w -> s -> Eff r (w, s)
- runState :: Eff (State s ': r) w -> s -> Eff r (w, s)
- modify :: Member (State s) r => (s -> s) -> Eff r ()
- evalState :: Eff (State s ': r) w -> s -> Eff r w
- execState :: Eff (State s ': r) w -> s -> Eff r s
- runStateR :: Eff (Writer s ': (Reader s ': r)) w -> s -> Eff r (w, s)
- runStateBack0 :: Eff '[State s] a -> (a, s)
- runStateBack :: Eff '[State s] a -> (a, s)
Documentation
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).
get :: Member (State s) r => Eff r s Source #
Return the current value of the state. The signatures are inferred
:: Eff (State s ': r) w | Effect incorporating State |
-> s | Initial state |
-> Eff r (w, s) | Effect containing final state and a return value |
Run a State effect
evalState :: Eff (State s ': r) w -> s -> Eff r w Source #
Run a State effect, discarding the final state.
execState :: Eff (State s ': r) w -> s -> Eff r s Source #
Run a State effect and return the final state.
runStateR :: Eff (Writer s ': (Reader s ': r)) w -> s -> Eff r (w, s) Source #
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.
runStateBack0 :: Eff '[State s] a -> (a, s) Source #
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.
runStateBack :: Eff '[State s] a -> (a, s) Source #
A different notion of backwards
is realized if we change the Put
handler slightly. How?
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