|
- Many variations are possible in creating a function
to represent an object with state, e.g.
- Obj :List Cmd -> List Ans
- where the commands (Cmd) are to set a state, or to query (get)
the current state, and the answers (Ans) are ok, (st,State) or err.
- The initial state is hard-wired here,
but could be an initial parameter of Obj.
If we concentrate on the state transformations, we have
- γ : Cmd
- Cmds = List Cmd
- σ : State
- C : Cmd -> State -> State
- C `γ1;γ2' σ
= (C γ2)o(C γ1) σ
and indeed the above is sufficient if the output "file", or
answer, is made a part of the state, together with the
values of the true state variables.
(For an alternative, see [state 2].)
let Obj = lambda Cmds. { List Cmd -> List Ans }
let rec
process = lambda state. lambda Cmds.
if null Cmds then nil else
let rec Cmd = hd Cmds,
tag = fst Cmd,
param = snd Cmd
in if tag = set then { change State }
(pair ok nil) :: (process param tl Cmds)
else if tag = get then
(pair st state) :: (process state tl Cmds)
else (pair err nil) :: (process state tl Cmds)
in process state0 Cmds
|
λ ...
:: | list cons |
nil | the [ ] list |
null | predicate |
hd | head (1st) |
tl | tail (rest) |
|
|
|