1 / 30

Programming Languages CS 152

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.

harvey
Download Presentation

Programming Languages CS 152

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming LanguagesCS 152 Avi Shinnar Lecture 13

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

  3. oops. back where we started. but it still returns 35! k represents what to do next Making the continuation explicit (+ 3 4) ( (x) (* x 5)) (( (x) (* x 5))(+ 3 4)) (+ 3 4( (x) (* x 5))) (+ 3 4( (x) (* x 5k)))

  4. Exception continuations (try (*(throw ‘a)5) catcher) (throw ‘a ( (x) (* x 5 )) catcher) kcatcher

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

  6. Exception continuations (try (*(if b 4(throw ‘a))5) catcher) (if b (( (x) (* x 5 k catcher) 4) (throw ‘a ( (x) (* x 5 k catcher)) catcher)) 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. What’s going on (direct) (define (map f l) (if (null? l) '() (cons (f (car l)) (map f (cdr l))))) (f 3) (map f ‘()) 6 ‘() (f 2) (map f ‘(3)) 4 ‘(6) (map f ‘(2 3)) (f 1) 2 ‘(4 6) (map f ‘(1 2 3)) ‘(2 4 6) f return map return (map f ‘(1 2 3)) (define (f x) (+ x x))

  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 (cdr l) (λ (rest-cdr) (k (cons 2 rest-cdr)))) (f 1 (λ (fc) (map-cps f (cdr l) (λ (rest-cdr) (k (cons fc rest-cdr))))) (map-cps f ‘(1 2 3) k) (map-cps f ‘(1 2 3) k) (define (f x k) (k (+ x x))) (f 2 (λ (fc) (map-cps f (cdr l) (λ (rest-cdr) ((λ (rest-cdr) (k (cons 2 rest-cdr))) (cons fc rest-cdr))))) (define (map-cps flk) (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 flk 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. d’s stack frame k c’s stack frame k b’s stack frame k a’s stack frame k CPS: naming the continuation d’s stack frame c’s stack frame b’s stack frame a’s stack frame direct (normal) CPS

  17. call-with-current-continuation(or call/cc to avoid RSI) • Gives us a way to name and use the implicitcontinuation • (call/cc(λ (k) …)) • k is a name for the implicit continuation • like setjmp/longjmp • but cooler • and safer

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

  19. 45 55 b=true b=false “hole”-y Computations (* [(call/cc (λ (k) (k 11)))] 5) 55 (*[(call/cc (λ (k) ( + 2 (if b(+ 3 4) (k 11)))))] 5)

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

  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 flk) (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)))))))))))

More Related