28
Continuations COS 441 Princeton University Fall 2004

Continuations COS 441 Princeton University Fall 2004

  • View
    243

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Continuations COS 441 Princeton University Fall 2004

Continuations

COS 441

Princeton University

Fall 2004

Page 2: Continuations COS 441 Princeton University Fall 2004

Webster Says…

Main Entry: re·ify Pronunciation: 'rA-&-"fI, 'rE-Function: transitive verbInflected Form(s): re·ified; re·ify·ingEtymology: Latin res thing -- to regard (something abstract) as a material or concrete thing

Page 3: Continuations COS 441 Princeton University Fall 2004

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!

Page 4: Continuations COS 441 Princeton University Fall 2004

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

Page 5: Continuations COS 441 Princeton University Fall 2004

ExampleCompute product of list elements with short

circuit when 0 is encountered

Page 6: Continuations COS 441 Princeton University Fall 2004

ExampleCompute product of list elements with short

circuit when 0 is encountered

Page 7: Continuations COS 441 Princeton University Fall 2004

Trickier Example

Compose a function with a continuation

Page 8: Continuations COS 441 Princeton University Fall 2004

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!

Page 9: Continuations COS 441 Princeton University Fall 2004

First-Class Continuations

Numbers n 2 NTypes ::= int | cont | …

Expr’s e ::= n | +(e1,e2)

| throw e1 to e2

| letcc x in e

| K (* not user visible *)

Values v ::= n | K

Page 10: Continuations COS 441 Princeton University Fall 2004

Frames and Stacks

Frames f ::= +(v1,¤) | +(¤,e2)

| throw v1 to ¤

| throw ¤ to e2

Stack K ::= ² | f B K

Page 11: Continuations COS 441 Princeton University Fall 2004

Dynamic Semantics

Page 12: Continuations COS 441 Princeton University Fall 2004

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

Page 13: Continuations COS 441 Princeton University Fall 2004

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

Page 14: Continuations COS 441 Princeton University Fall 2004

Programmer Visible Rules

Page 15: Continuations COS 441 Princeton University Fall 2004

Auxiliary Types

Types ::= int | cont

| stack

| (1,2) frame

Page 16: Continuations COS 441 Princeton University Fall 2004

Internal Type-Checking Rules

Page 17: Continuations COS 441 Princeton University Fall 2004

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 ’

Page 18: Continuations COS 441 Princeton University Fall 2004

Frame Rules (cont.)

Harper’s text has a small bug rules above are correct

` throw v1 to ¤ : (cont,’) framev1 value ` v1:

` throw ¤ to e2 : (,’) frame` e2: cont

Page 19: Continuations COS 441 Princeton University Fall 2004

Example: Continuation Capture

(²,+((letcc x in 1),2))

(+(¤,2)B²,letcc x in 1)

(+(¤,2)B²,1)

(+(1,¤)B²,2)

(²,3)

Page 20: Continuations COS 441 Princeton University Fall 2004

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)

Page 21: Continuations COS 441 Princeton University Fall 2004

Stacks and Frames as HOF

One big insight of the CPS transform is that you can represent stacks as HOF

rep(²) x.x

rep(+(v1,¤)BK) x.(rep(K) +(v1,x))

rep(+(¤,e2)BK) x.(rep(K) +(x,e2))

rep(throw v1 to ¤BK) x.(x v)

rep(throw ¤ to e2BK) x.(e2 x)

Page 22: Continuations COS 441 Princeton University Fall 2004

Producer Consumer

fun p(n) =

(put(n);p(n+1))

fun c() = let

val n = get()

in printInt(n);

c()

end

Page 23: Continuations COS 441 Princeton University Fall 2004

Push Model

fun p(n) =

(c(n);p(n+1))

fun c(n) =

printInt(n)

Page 24: Continuations COS 441 Princeton University Fall 2004

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

Page 25: Continuations COS 441 Princeton University Fall 2004

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

Page 26: Continuations COS 441 Princeton University Fall 2004

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)

Page 27: Continuations COS 441 Princeton University Fall 2004

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

Page 28: Continuations COS 441 Princeton University Fall 2004

Summary

• First-Class continuations very powerful construct

• “Simple” semantics but allows powerful control constructs– Non-local exits– Coroutines– User-level threads