This module provides two kinds of stateful computation, one which undoes
state changes on backtracking (run_state//{1,2,3}) and another which preserves
state changes on backtracking (run_nb_state//1).
On top this are built two execution contexts which provide mutable
references (run_ref/1) and a mutable environment (run_env/1).
- run_state_handler(+Pr:prompt(R), +H:pred(+R,S,S), +G:pred, S1:S, S2:S) is det
- Run P in an context where handler H is available process requests with
state threaded through DCG style.
- run_state(+Pr:prompt, +P:pred, S1:S, S2:S) is det
- run_state(+P:pred, S1:S, S2:S) is det
- Run P in an context that allows set/1 and get/1 to be used to
to handle a mutable state, initially S1. The final state is unified
with S2. run_state/3 uses the prompt
state
.
State changes are undone on backtracking. run_state(Pr,G,S1,S2)
is equivalent to run_state_handler(Pr,handle,Goal,S1,S2)
.
- run_nb_state(+Pr:prompt, +P:pred, +S1:S, -S2:S) is det
- run_nb_state(+P:pred, +S1:S, -S2:S) is det
- Run P in a context where get/1 and set/1 manipulate a mutable state,
similar to run_state/3, but state changes are not undone on backtracking.
Note that, to ensure preservation of state on backtracking, set/1 saves a
copy of the given term, not the term itself. Implementation uses nb_getval/2
and nb_setval/2 with a dynamically generated key. run_nb_state/3 uses
prompt
state
.
Note that using this can be quite expensive if the state is large due to
the copying that occurs whenever it is changed.
Undocumented predicates
The following predicates are exported, but not or incorrectly documented.
- run_state(Arg1, Arg2, Arg3)
- set(Arg1)
- set(Arg1, Arg2)
- get(Arg1)
- get(Arg1, Arg2)
- app(Arg1)
- app(Arg1, Arg2)
- upd(Arg1, Arg2)