Programming Languages CS 152

30
Programming Languages CS 152 Avi Shinnar Lecture 13

description

Programming Languages CS 152. Avi Shinnar Lecture 13. (* (+ 3 4) 5). Continuations: What’s Next. [ ]. (+ 3 4)  (  (x) (* x 5) ). oops. back where we started. but it still returns 35!. k represents what to do next. Making the continuation explicit. - PowerPoint PPT Presentation

Transcript of Programming Languages CS 152

Page 1: Programming Languages CS 152

Programming LanguagesCS 152

Avi Shinnar

Lecture 13

Page 2: Programming Languages CS 152

Continuations: What’s Next

(+ 3 4) ( (x) (* x 5))[ ](* (+ 3 4) 5)

Page 3: Programming Languages CS 152

Making the continuation explicit

(+ 3 4) ( (x) (* x 5))

(( (x) (* x 5)) (+ 3 4))

(+ 3 4 ( (x) (* x 5)))

oops. back where we started.

(+ 3 4 ( (x) (* x 5 k)))

but it still returns 35!

k representswhat to do next

Page 4: Programming Languages CS 152

Exception continuations

(throw ‘a

( (x) (* x 5 ))

catcher)

(try

(* (throw ‘a) 5)

catcher)

k catcher

Page 5: Programming Languages CS 152

What’s next?

(try

(* (if b 4 (throw ‘a)) 5)

catcher) [ ]

catcher( (x) (* x 5))

(if b 4 (throw ‘a))

normal

exceptional

Page 6: Programming Languages CS 152

Exception continuations

(if b (( (x) (* x 5 k catcher) 4)

(throw ‘a

( (x) (* x 5 k catcher))

catcher))

catcher)

(try

(* (if b 4 (throw ‘a)) 5)

catcher)

or (4 … catcher)

where

4 = ( (k err) (k 4))

Page 7: Programming Languages CS 152

Continuation Passing Style

• whole program

• everything goes forward– every (non-primitive) is a tail-call!– and they never return

• CPS conversion can be done automatically

• Primitives are normally left alone– so + and * would behave normally…

Page 8: Programming Languages CS 152

The point of no return

• Control– With explicit continuations, we can do cooler things ---

coming soon

• simplicity• one lambda to rule them all…• Naming: all intermediate values have a name• All sequencing is explicit

– functional compilers• stack frame is explicit• CPS conversion

– theory

Page 9: Programming Languages CS 152

map

(define (map f l)

(if (null? l) '()

(cons (f (car l)) (map f (cdr l)))))

Page 10: Programming Languages CS 152

(map f ‘())

(map f ‘(3))

(map f ‘(2 3))

(map f ‘(1 2 3))

(f 2)

(f 3)

What’s going on (direct)

(f 1)

(map f ‘(1 2 3))

‘()

‘(6)

‘(4 6)

‘(2 4 6)

6

4

2

(define (f x) (+ x x))

f return map return

(define (map f l)

(if (null? l) '()

(cons (f (car l))

(map f (cdr l)))))

Page 11: Programming Languages CS 152

CPS: map

(define (map-cps f l k)

(if (null? l) (k ‘())

(f (car l)

(λ (fc)

(map-cps f (cdr l)

(λ (rest-cdr)

(k (cons fc rest-cdr))))))))

Page 12: Programming Languages CS 152

What’s going on (CPS)

(map-cps f ‘(1 2 3) k)

(map-cps f ‘(1 2 3) k)

(define (f x k) (k (+ x x)))

(f 1 (λ (fc) (map-cps f (cdr l) (λ (rest-cdr) (k (cons fc rest-cdr)))))

(map-cps f (cdr l) (λ (rest-cdr) (k (cons 2 rest-cdr))))

(f 2 (λ (fc)(map-cps f (cdr l) (λ (rest-cdr) ((λ (rest-cdr) (k (cons 2 rest-cdr))) (cons fc rest-cdr)))))

(define (map-cps f l k) (if (null? l) (k ‘()) (f (car l) (λ (fc) (map-cps f (cdr l) (λ (rest-cdr) (k (cons fc rest-cdr))))))))

Page 13: Programming Languages CS 152

CPS: map w/exceptions

(define (map-cps-exn f l k err) (if (null? l) (k ‘()) (f (car l) (λ (fc) (map-cps-exn f (cdr l) (λ (rest-cdr) (k (cons fc rest-cdr))) err))

err)))

Page 14: Programming Languages CS 152

Web

Page 15: Programming Languages CS 152

Ajax

xmlHttp.onreadystatechange

Page 16: Programming Languages CS 152

CPS: naming the continuation

d’s stack frame

c’s stack frame

b’s stack frame

a’s stack frame

d’s stack frame k

c’s stack frame k

b’s stack frame k

a’s stack frame k

direct (normal) CPS

Page 17: Programming Languages CS 152

call-with-current-continuation(or call/cc to avoid RSI)

• Gives us a way to name and use the implicit continuation

• (call/cc (λ (k) …))– k is a name for the implicit continuation

• like setjmp/longjmp– but cooler– and safer

Page 18: Programming Languages CS 152

[ ] ( (x) (* x 5))(* 5)[ ]

“hole”-y Computations

(+ 3 4)(+ 3 4)

(* [(call/cc (λ (k)

(+ 3 4)))]

5)

35

Page 19: Programming Languages CS 152

“hole”-y Computations

(* [(call/cc (λ (k)

(k 11)))]

5)

55

(* [(call/cc (λ (k)

( + 2 (if b (+ 3 4) (k 11)))))]

5) 45

b=true

55

b=false

Page 20: Programming Languages CS 152

“hole”-y Computations(define !hole #f)

(* [(call/cc (λ (k)

(set! !hole k)

(+ 3 4)))]

5)

35

(!hole 11) 55 (!hole 20) 100

Page 21: Programming Languages CS 152

simple yield

(define (map-yield f l !do-other-stuff)

(map (λ (x)

(display x) (space)

(set! !do-other-stuff (call/cc !do-other-stuff))

(f x)) l)

'())!do-other-stuff, when it invokes the continuation picks the nextplace to yield to.

Page 22: Programming Languages CS 152

co-routines

(define (map-yield2 f l1 l2)

(map-yield f l1 (λ (k) (map-yield f l2 k))))

(map-yield2

(λ (x) (+ x x))

'(1 2 3 4)

'(5 6 7 8))

1 5 2 6 3 7 4 8

Page 23: Programming Languages CS 152

And more…

• (continuable) exceptions• generators (yield, resume…)

– can turn an arbitrary traversal pattern into an iterator automatically

• general co-routines• co-operative (non-preemptive) multitasking

– threads explicitly yield

• backtracking– non-determinism– undo

Page 24: Programming Languages CS 152

Other Fun Stuff

Page 25: Programming Languages CS 152

Exception Handling

;; initialization(define !exn_handlers '())(let ((is_exn

(call/cc (λ (c)

(set! !exn_handlers (cons c !exn_handlers)) #f))))

(if is_exn (display is_exn)));; throw: goto most recent exception handle continuation(define (throw v) ((car !exn_handlers) (cons 'exn v)))

Page 26: Programming Languages CS 152

Exception Handling

(define (try-def e exn) (let ((v (call/cc (λ (c)

;; add ourselves (set! !exn_handlers (cons c !exn_handlers)) (cons 'ret (force e))))))

;; remove ourselves (no matter what) (set! !exn_handlers (cdr !exn_handlers)) (if (eq? (car v) 'ret) (cdr v) ;; normal return (exn (cdr v))))) ;; exception: call handler

(define-syntax try (syntax-rules () ((_ e exn) (try-def (delay e) exn))))

Page 27: Programming Languages CS 152

Design notes

• in-band v. out of band data– prevent-effects

• “-no-effects-for-me”

– exceptional return from thread• (cons ‘this-is-an-exception exn)

• prevent-effects– should it be observable?– is it?

Page 28: Programming Languages CS 152

effects-prevented?

(define (effects-prevented?)

(try

(let ((!x 3))

(set! !x 5))

(λ (x) (eq? x 'prevent-effects))))

Page 29: Programming Languages CS 152

Data continuations

Prof. Morrisett: Refining first-class stores

http://www.eecs.harvard.edu/~greg/jgm.html#Morrisett93

Page 30: Programming Languages CS 152

CPS map: It could be worse

(define (map-cps-full f l k) (if (null? l) (k ‘()) (car l (λ (carl) (f carl (λ (fc) (cdr l (λ (cdrl) (map-cps-full f cdrl (λ (rest-cdr) (cons fc rest-cdr k)))))))))))