State II

LA home
 FP,  λ
 Logic,  π

   state 1
   state 2



Continuations can also be used to implement objects with state. A continuation is just a function, passed as a parameter, that represents a computation to be completed (i.e. continued with) at some later time. e.g.

γ : Cmd
σ : State
θ : Cont = State->Ans
Ans = List Reply
C : Cmd->Cont->State->Ans = Cmd->Cont->Cont
C `γ12' θ σ = C γ1 (C γ2 θ) σ

Note how the rule for `γ12', can be read left-to-right as "to do γ12, first do γ1 and then do γ2", and replaces what would otherwise be functional composition (C' γ2)o(C' γ1). (Continuations are used in denotational semantics to describe the semantics of jumps and other sequencers found in imperative programming languages.)

let Obj = lambda cmds.        { List Cmd -> Ans }
  { Cont = State -> Ans         -- continuation }
  let rec
  { C : Cmd->Cont->State->Ans = Cmd->Cont->Cont }
  C = lambda cmd. lambda cont. lambda state.
    let rec tag   = fst cmd,
            param = snd cmd
    in if tag = set then { change State }
            (pair ok nil)   :: cont param
       else if tag = get then
            (pair st state) :: cont state
       else (pair err nil)  :: cont state,

  Cs = lambda cmds. lambda state.
    if null cmds then nil        {finished}
    else C hd cmds (Cs tl cmds) state

  in Cs cmds state0

Note that we have two functions, C and Cs, because (here) Cmd and Cmds = List Cmd are different types.

window on the wide world:

Computer Science Education Week

free op. sys.
free office suite,
ver 3.4+

~ free photoshop
web browser
like it says!

λ ...
:: list cons
nil the [ ] list
null  predicate
hd head (1st)
tl tail (rest)

© L. Allison   (or as otherwise indicated),
Faculty of Information Technology (Clayton), Monash University, Australia 3800 (6/'05 was School of Computer Science and Software Engineering, Fac. Info. Tech., Monash University,
was Department of Computer Science, Fac. Comp. & Info. Tech., '89 was Department of Computer Science, Fac. Sci., '68-'71 was Department of Information Science, Fac. Sci.)
Created with "vi (Linux + Solaris)",  charset=iso-8859-1,  fetched Tuesday, 21-Oct-2014 14:41:24 EST.