Presentation is loading. Please wait.

Presentation is loading. Please wait.

Continuations COS 441 Princeton University Fall 2004.

Similar presentations


Presentation on theme: "Continuations COS 441 Princeton University Fall 2004."— Presentation transcript:

1 Continuations COS 441 Princeton University Fall 2004

2 Webster Says… Main Entry: re·ify Pronunciation: 'rA-&-"fI, 'rE- Function: transitive verb Inflected Form(s): re·ified; re·ify·ing Etymology: 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 e 1 to e 2 ):  ’ Invoke continuation (e 2 :  cont ) with the value of e 1 :  ( 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 Won’t 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 Numbers n 2 N Types  ::= int |  cont | … Expr’s e::=n | +(e 1,e 2 ) | throw e 1 to e 2 | letcc x in e |K (* not user visible *) Values v::=n | K

10 Frames and Stacks Frames f::= +(v 1, ¤ ) | +( ¤,e 2 ) | throw v 1 to ¤ | throw ¤ to e 2 Stack K::= ² | f B K

11 Dynamic Semantics

12 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 Types  ::= int |  cont |  stack | (  1,  2 ) frame

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.) Harper’s text has a small bug rules above are correct ` throw v 1 to ¤ : (  cont,  ’) frame v 1 value ` v 1 :  ` throw ¤ to e 2 : ( ,  ’) frame ` e 2 :  cont

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 rep( ² )  x.x rep(+(v 1, ¤ ) B K)  x.(rep(K) +(v 1,x)) rep(+( ¤,e 2 ) B K)  x.(rep(K) +(x,e 2 )) rep( throw v 1 to ¤B K)  x.(x v) rep( throw ¤ to e 2 B K)  x.(e 2 x)

22 Producer Consumer fun p(n) = (put(n);p(n+1)) fun c() = let val n = get() in printInt(n); c() end

23 Push Model fun p(n) = (c(n);p(n+1)) fun c(n) = printInt(n)

24 Pull Model fun p(n,s) = (n,n+1) 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(n+1,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' => throw k (S k')) fun put(n,s) = (buf := n;resume s) fun get(s) = (!buf,resume s)

27 Threads signature THREADS = sig exception NoMoreThreads val fork : (unit -> unit) -> unit val yield : unit -> unit val exit : unit -> ‘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


Download ppt "Continuations COS 441 Princeton University Fall 2004."

Similar presentations


Ads by Google