State II

home1 home2
 Bib
 Algorithms
 Bioinfo
 FP
 Logic
 MML
 Prog.Lang
and the
 Book

FP
 Lambda
  Introduction
  Examples
   state 1
   state 2

 Compj88
Semantics

Continuations

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.


Coding Ockham's Razor, L. Allison, Springer

A Practical Introduction to Denotational Semantics, L. Allison, CUP

Linux
 Ubuntu
free op. sys.
OpenOffice
free office suite
The GIMP
~ free photoshop
Firefox
web browser

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

© L. Allison   http://www.allisons.org/ll/   (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 Friday, 29-Mar-2024 02:46:44 AEDT.