Continuations - PowerPoint PPT Presentation

About This Presentation
Title:

Continuations

Description:

Continuations. COS 441. Princeton University. Fall 2004. Webster Says... Main Entry: re ify ... Etymology: Latin res thing -- to regard (something abstract) as ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 29
Provided by: csPrin7
Category:

less

Transcript and Presenter's Notes

Title: Continuations


1
Continuations
  • COS 441
  • Princeton University
  • Fall 2004

2
Webster Says
  • Main Entry reify Pronunciation 'rA--"fI,
    'rE-Function transitive verbInflected Form(s)
    reified reifyingEtymology Latin res thing
    -- to regard (something abstract) as a material
    or concrete thing

3
First-Class Continuations
  • Form of structured GOTO
  • Can be used to implement exception
  • Can be used to build co-routines or threads
  • Available in Scheme, Ruby, and SML/NJ but not
    Standard ML
  • Also useful as a programming abstraction for
    web-services!

4
Some Informal Examples
  • (throw e1 to e2)?
  • Invoke continuation (e2? cont) with the value
    of e1?
  • (letcc x in e)?
  • Evaluate e with x bound to a value of type ?
    cont. Expression evaluates to the value of e or
    result thrown to x. Can throw to x multiple times
    and rewind to this control point

5
Example
  • Compute product of list elements with short
    circuit when 0 is encountered

6
Example
  • Compute product of list elements with short
    circuit when 0 is encountered

7
Trickier Example
  • Compose a function with a continuation

8
Continuation Passing Style
  • Can apply a global translation of a program and
    remove all uses of throw and letcc
  • Wont study in detail in class
  • CPS transform used in compilation of functional
    languages to machine code
  • Understanding how continuations can be used is
    harder than understanding their semantics!

9
First-Class Continuations
10
Frames and Stacks
11
Dynamic Semantics
12
Dynamic Semantics
  • Capturing a continuation can be a linear time
    operation if not implemented cleverly
  • Clever implementations make continuation capture
    constant time
  • Compare this to two stack exception handler
    approach

13
Type Safety
  • Must define when a machine state is well-formed
  • Introduced the notion of types for stacks,
    frames, and also choose one arbitrary but fixed
    type for the empty stack ?ans

14
Programmer Visible Rules
15
Auxiliary Types
16
Internal Type-Checking Rules
17
Rules For Frames
  • (?,?) frame Frame which expects a value of type
    ? that can be used to fill the hole to produce an
    expression of type ?

18
Frame Rules (cont.)
  • Harpers text has a small bug rules above are
    correct

19
Example Continuation Capture
  • (²,((letcc x in 1),2))
  • ? ((,2)B²,letcc x in 1)
  • ? ((,2)B²,1)
  • ? ((1,)B²,2)
  • ? (²,3)

20
Example Thrown Continuation
  • (²,((letcc x in (1,throw 2 to x)),3))
  • ? ((,3)B²,letcc x in (1,throw 2 to x))
  • ? ((,3)B²,(1,throw 2 to (,3)B²))
  • ? ((,throw 2 to (,3)B²)B(,3)B²,1)
  • ? ((1,)B(,3)B²,throw 2 to (,3)B²)
  • ? ((throw to (,3)B²)B(1,)B(,3)B²,2)
  • ? ((throw 2 to )B(1,)B(,3)B²,(,3)B²)
  • ? ((,3)B²,2)
  • ? ((2,)B²,3)
  • ? (²,5)

21
Stacks and Frames as HOF
  • One big insight of the CPS transform is that you
    can represent stacks as HOF

22
Producer Consumer
  • fun p(n)
  • (put(n)p(n1))
  • fun c() let
  • val n get()
  • in printInt(n)
  • c()
  • end

23
Push Model
  • fun p(n)
  • (c(n)p(n1))
  • fun c(n)
  • printInt(n)

24
Pull Model
  • fun p(n,s)
  • (n,n1)
  • fun c(n,s) let
  • val (n,s) p(s)
  • in printInt(n)
  • c(s)
  • end

25
Coroutine Model
  • fun p(n,s) let
  • val s put(n,s)
  • in p(n1,s)
  • end
  • fun c(s) let
  • val (n,s) get(s)
  • in printInt(n)
  • c(s)
  • end

26
Implementation
  • datatype state S of state cont
  • val buf ref 0
  • fun resume(S k)
  • callcc(fn k' gt throw k (S k'))
  • fun put(n,s) (buf nresume s)
  • fun get(s) (!buf,resume s)

27
Threads
  • signature THREADS sig
  • exception NoMoreThreads
  • val fork (unit -gt unit) -gt unit
  • val yield unit -gt unit
  • val exit unit -gt a
  • end
  • User-level threads implemented with callcc

28
Summary
  • First-Class continuations very powerful construct
  • Simple semantics but allows powerful control
    constructs
  • Non-local exits
  • Coroutines
  • User-level threads
Write a Comment
User Comments (0)
About PowerShow.com