Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Control.State

The canonical State(s) effect: a threaded piece of mutable-looking state, interpreted by parameter passing.

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. 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), e0}) -> (b, a) ! {e0}

Run action, threading init as the initial state; returns (result, final_state). The handler makes the block a state transformer s -> (a, s) and applies it to init.

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), 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), 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.