programming languages cs 152 avi shinnar lecture 13
Post on 20-Dec-2015
231 views
TRANSCRIPT
Programming LanguagesCS 152
Avi Shinnar
Lecture 13
Continuations: What’s Next
(+ 3 4) ( (x) (* x 5))[ ](* (+ 3 4) 5)
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
Exception continuations
(throw ‘a
( (x) (* x 5 ))
catcher)
(try
(* (throw ‘a) 5)
catcher)
k catcher
What’s next?
(try
(* (if b 4 (throw ‘a)) 5)
catcher) [ ]
catcher( (x) (* x 5))
(if b 4 (throw ‘a))
normal
exceptional
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))
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)))))
(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)))))
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 ‘(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))))))))
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)))
Web
Ajax
xmlHttp.onreadystatechange
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
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
[ ] ( (x) (* x 5))(* 5)[ ]
“hole”-y Computations
(+ 3 4)(+ 3 4)
(* [(call/cc (λ (k)
(+ 3 4)))]
5)
35
“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
“hole”-y Computations(define !hole #f)
(* [(call/cc (λ (k)
(set! !hole k)
(+ 3 4)))]
5)
35
(!hole 11) 55 (!hole 20) 100
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.
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
Other Fun Stuff
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 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)))))))))))