Trampoline
Trampolined interpreter using builtins.genericClosure for O(1) stack depth.
handle
Trampolined handler combinator with return clause.
handle : { return?, handlers, state? } -> Computation a -> { value, state }
Follows Kiselyov & Ishii's handle_relay pattern but trampolined
via genericClosure for O(1) stack depth.
Arguments (attrset):
- return — value -> state -> { value, state }. How to transform the final Pure value. Default: identity.
- handlers — { effectName = { param, state }: { resume | abort, state }; }. Each must return { resume; state; } or { abort; state; }.
- state — initial handler state. Default: null.
run
Run a computation through the genericClosure trampoline.
run : Computation a -> Handlers -> State -> { value : a, state : State }
Arguments:
- comp — the freer monad computation to interpret
- handlers — { effectName = { param, state }: { resume | abort, state }; ... }
- initialState — starting state passed to handlers
Handlers must return one of:
{ resume = value; state = newState; } -- invoke continuation with value
{ abort = value; state = newState; } -- discard continuation, halt
This is the defunctionalized encoding of Plotkin & Pretnar (2009):
resume ≡ invoke continuation k(v), abort ≡ discard k.
Stack depth: O(1) — constant regardless of computation length. Time: O(n) where n = number of effects in the computation.