Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages CS 152 Avi Shinnar Lecture 13.

Similar presentations


Presentation on theme: "Programming Languages CS 152 Avi Shinnar Lecture 13."— Presentation transcript:

1 Programming Languages CS 152 Avi Shinnar Lecture 13

2 Continuations: What’s Next (+ 3 4)  ( (x) (* x 5)) [ ] (* (+ 3 4) 5)

3 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 represents what to do next

4 Exception continuations (throw ‘a ( (x) (* x 5 )) catcher) (try (* (throw ‘a) 5) catcher) k catcher

5 What’s next? (try (* (if b 4 (throw ‘a)) 5) catcher) [ ] catcher ( (x) (* x 5)) (if b 4 (throw ‘a)) normal exceptional

6 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))

7 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…

8 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

9 map (define (map f l) (if (null? l) '() (cons (f (car l)) (map f (cdr l)))))

10 (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 returnmap return (define (map f l) (if (null? l) '() (cons (f (car l)) (map f (cdr l)))))

11 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))))))))

12 What’s going on (CPS) (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))))))))

13 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)))

14 Web

15 Ajax xmlHttp.onreadystatechange

16 CPS: naming the continuation d’s stack frame c’s stack frame b’s stack frame a’s stack frame d’s stack frame kc’s stack frame kb’s stack frame ka’s stack frame k direct (normal) CPS

17 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

18 [ ]  ( (x) (* x 5)) (* 5) [ ] “hole”-y Computations (+ 3 4) (* [(call/cc (λ (k) (+ 3 4)))] 5) 35

19 “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

20 “hole”-y Computations (define !hole #f) (* [(call/cc (λ (k) (set! !hole k) (+ 3 4)))] 5) 35 (!hole 11) 55 (!hole 20) 100

21 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 next place to yield to.

22 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

23 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

24 Other Fun Stuff

25 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)))

26 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))))

27 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?

28 effects-prevented? (define (effects-prevented?) (try (let ((!x 3)) (set! !x 5)) (λ (x) (eq? x 'prevent-effects))))

29 Data continuations Prof. Morrisett: Refining first-class stores http://www.eecs.harvard.edu/~greg/jgm.html #Morrisett93

30 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)))))))))))


Download ppt "Programming Languages CS 152 Avi Shinnar Lecture 13."

Similar presentations


Ads by Google