Control.State
The canonical State(s) effect: a threaded piece of mutable-looking state, interpreted through a private cell scoped to the run.
get reads the current state, put overwrites it; a computation performs them without ever naming a state value in its own signature. run_state discharges the effect, threading init through and handing back the result paired with the final state. Because the handler is the only interpreter, the same ops can be re-read (bounded, logged) without touching the producer. Every clause resumes exactly once in tail position, so the compiler is free to lower the whole run to direct state updates. Opt-in: not in Base.
Effects
State
effect State(s)
get() : s
put(s) : Unit
Read (get) and overwrite (put) a threaded state of type s.
Functions and Values
run_state
run_state : forall e0 a b. (a, () -> b ! {Control.State.State(a), Var@cell@3, e0}) -> (b, a) ! {e0}
Run action, threading init as the initial state; returns (result, final_state). The state lives in a cell private to this run: get resumes with its current value, put overwrites it and resumes, and the final value is read back once the action returns.
run_state(1, \() -> state(\(s) -> (s * 2, s + 1)))
(2, 2)
eval_state
eval_state : forall e0 a b. (a, () -> b ! {Control.State.State(a), Var@cell@3, e0}) -> b ! {e0}
Run action for its result only, discarding the final state.
exec_state
exec_state : forall e0 a b. (a, () -> b ! {Control.State.State(a), Var@cell@3, e0}) -> a ! {e0}
Run action for its final state only, discarding the result.
modify
modify : forall a. ((a) -> a) -> Unit ! {Control.State.State(a)}
Apply f to the current state, storing the result.
exec_state(3, \() -> modify(\(n) -> n * 2))
6
state
state : forall a b. ((a) -> (b, a)) -> b ! {Control.State.State(a)}
Run one combined read-and-write step: f maps the current state to a result and the next state, the result is returned and the state stored.
gets
gets : forall a b. ((a) -> b) -> b ! {Control.State.State(a)}
Read a projection f of the current state.