300 likes | 394 Views
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.
E N D
Programming LanguagesCS 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 (+ 3 4) ( (x) (* x 5)) (( (x) (* x 5))(+ 3 4)) (+ 3 4( (x) (* x 5))) (+ 3 4( (x) (* x 5k)))
Exception continuations (try (*(throw ‘a)5) catcher) (throw ‘a ( (x) (* x 5 )) catcher) kcatcher
(try (*(if b 4(throw ‘a))5) catcher) [ ] normal exceptional What’s next? (if b 4(throw ‘a)) ( (x) (* x 5)) catcher
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))
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…
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
map (define (map f l) (if (null? l) '() (cons (f (car l)) (map f (cdr l)))))
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))
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))))))))
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))))))))
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)))
Ajax xmlHttp.onreadystatechange
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
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
(*5) “hole”-y Computations [ ] (+ 3 4) [] ( (x) (* x 5)) (+ 3 4) (*[(call/cc (λ (k) (+ 3 4)))] 5) 35
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)
“hole”-y Computations (define !hole #f) (*[(call/cc (λ (k) (set! !hole k) (+ 3 4)))] 5) 35 55 100 (!hole 11) (!hole 20)
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.
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
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
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)))
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))))
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?
effects-prevented? (define (effects-prevented?) (try (let ((!x 3)) (set! !x 5)) (λ (x) (eq? x 'prevent-effects))))
Data continuations Prof. Morrisett: Refining first-class stores http://www.eecs.harvard.edu/~greg/jgm.html#Morrisett93
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)))))))))))