continuation passing style cps assignment converted
play

Continuation-passing Style (CPS) Assignment-converted/alphatized IR - PowerPoint PPT Presentation

Continuation-passing Style (CPS) Assignment-converted/alphatized IR (.alpha) e ::= (let ([x e] ...) e) | (lambda (x ...) e) | (lambda x e) | (apply e e) | (e e ...) | (prim op e ...) | (apply-prim op e) | (if e e e) | (call/cc e) | x |


  1. Continuation-passing Style (CPS)

  2. Assignment-converted/alphatized IR (.alpha) e ::= (let ([x e] ...) e) | (lambda (x ...) e) | (lambda x e) | (apply e e) | (e e ...) | (prim op e ...) | (apply-prim op e) | (if e e e) | (call/cc e) | x | (quote dat)

  3. Administrative normal form (ANF) (.anf) e ::= (let ([x e]) e) | (apply ae ae) | (ae ae ...) | (prim op ae ...) | (apply-prim op ae) | (if ae e e) | (call/cc ae) | ae ae ::= (lambda (x ...) e) | (lambda x e) | x | (quote dat)

  4. Continuation-passing style (CPS) (.cps) e ::= (let ([x (apply-prim op ae)]) e) | (let ([x (prim op ae ...)]) e) | (apply ae ae) | (ae ae ...) | (if ae e e) ae ::= (lambda (x ...) e) | (lambda x e) | x | (quote dat)

  5. e ::= (let ([x (apply-prim op ae)]) e) | (let ([x (prim op ae ...)]) e) | (apply ae ae) | (ae ae ...) | (if ae e e) ae ::= (lambda (x ...) e) | (lambda x e) | x | (quote dat) • Programs in CPS require no stack and never return. • Instead, at each application, a continuation (a callback function) is passed forward explicitly. • Points that would otherwise have extended the stack now create a closure (where the environment saves local variables and the stack tail). • Return points become invocations of the current continuation.

  6. (let ([x ((lambda (y z) z) a b)]) e) e cps has a free var for e’s cont. ((lambda (k y z) (k 0 z)) (lambda (k v) e cps ) a b) All functions take an extra continuation parameter. As call/cc lets us pass continuations as values, so must they (despite not using it).

  7. call/cc = (lambda (k f) (f k k))

  8. Visualizing CPS (example)

  9. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) (fib 4)

  10. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) (fib 3) fn {+} (fib (- n 2)) [n = 4]

  11. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) (fib 2) fn {+} (fib (- n 2)) [n = 3] fn {+} (fib (- n 2)) [n = 4]

  12. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) (fib 1) fn {+} (fib (- n 2)) [n = 2] fn {+} (fib (- n 2)) [n = 3] fn {+} (fib (- n 2)) [n = 4]

  13. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) {1} fn {+} (fib (- n 2)) [n = 2] fn {+} (fib (- n 2)) [n = 3] fn {+} (fib (- n 2)) [n = 4]

  14. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) (fib 0) fn {+} {1} fn {+} (fib (- n 2)) [n = 3] fn {+} (fib (- n 2)) [n = 4]

  15. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) 0 fn {+} {1} fn {+} (fib (- n 2)) [n = 3] fn {+} (fib (- n 2)) [n = 4]

  16. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) 1 fn {+} (fib (- n 2)) [n = 3] fn {+} (fib (- n 2)) [n = 4]

  17. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) (fib 1) fn {+} {1} fn {+} (fib (- n 2)) [n = 4]

  18. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) 1 fn {+} {1} fn {+} (fib (- n 2)) [n = 4]

  19. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) 2 fn {+} (fib (- n 2)) [n = 4]

  20. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) (fib 2) fn {+} {2}

  21. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) (fib 1) fn {+} (fib (- n 2)) [n = 2] fn {+} {2}

  22. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) 1 fn {+} (fib (- n 2)) [n = 2] fn {+} {2}

  23. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) (fib 0) fn {+} {1} fn {+} {2}

  24. (define (fib n) (if (<= n 1) IR n (+ (fib (- n 1)) (fib (- n 2))))) 0 fn {+} {1} fn {+} {2} 3

  25. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) (fib 4)

  26. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) (fib 3) letk v0 e 2 [n=4,n-1=3,…]

  27. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) (fib 2) letk v0 e 2 [n=3,n-1=2,…] letk v0 e 2 [n=4,n-1=3,…]

  28. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) (fib 1) -> 1 letk v0 e 2 [n=2,n-1=1,…] letk v0 e 2 [n=3,n-1=2,…] letk v0 e 2 [n=4,n-1=3,…]

  29. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) (fib 0) -> 0 letk v1 e 4 [v0=1,n=2,n-1=1,…] letk v0 e 2 [n=3,n-1=2,…] letk v0 e 2 [n=4,n-1=3,…]

  30. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) s [s=1,v0=1,v1=0,…] letk v0 e 2 [n=3,n-1=2,…] letk v0 e 2 [n=4,n-1=3,…]

  31. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) (fib 1) -> 1 letk v1 e 4 [v0=1,n=3,n-1=2,…] letk v0 e 2 [n=4,n-1=3,…]

  32. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) s [s=2,v0=1,v1=1,…] letk v0 e 2 [n=4,n-1=3,…]

  33. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) (fib 2) letk v1 e 4 [v0=2,n=4,n-1=3,…]

  34. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) (fib 1) -> 1 letk v0 e 2 [n=2,n-1=3,…] letk v1 e 4 [v0=2,n=4,n-1=3,…]

  35. (define (fib n) (let ([c (<= n 1)]) (if c n ANF 0 (let ([n-1 (- n 1)]) 1 (let ([v0 (fib n-1)]) 2 (let ([n-2 (- n 2)]) 3 (let ([v1 (fib n-2)]) 4 (let ([s (+ v0 v1)]) 5 s))))))) (fib 0) -> 0 letk v1 e 4 [v0=1,n=2,n-1=3,…] letk v1 e 4 [v0=2,n=4,n-1=3,…]

  36. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) (fib 4 print)

  37. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) (lambda (v0) …) print (fib 3 k) n=4 k =

  38. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) print (lambda (v0) …) n=4 k = (lambda (v0) …) (fib 2 k) n=3 k =

  39. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) (lambda (v0) …) print n=3 k = (lambda (v0) …) (fib 1 k) n=2 k = (lambda (v0) …) n=4 k =

  40. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) (lambda (v0) …) print n=3 k = (lambda (v0) …) (k 1) n=2 k = (lambda (v0) …) n=4 k =

  41. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) (lambda (v0) …) print n=3 k = (lambda (v1) …) (fib 0 k) n=2 v0=1 k (lambda (v0) …) n=4 k =

  42. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) (lambda (v0) …) print n=3 k = (k 0) (lambda (v1) …) n=2 v0=1 k (lambda (v0) …) n=4 k =

  43. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) (lambda (v0) …) print n=3 k = (k 1) (lambda (v0) …) n=4 k =

  44. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) (lambda (v1) …) print n=3 v0=1 k (fib 1 k) (lambda (v0) …) n=4 k =

  45. (define (fib n k) (let ([c (<= n 1)]) (if c (k n) (let ([n-1 (- n 1)]) CPS (fib n-1 (lambda (v0) (let ([n-2 (- n 2)]) (fib n-2 (lambda (v1) (let ([s (+ v0 v1)]) (k s))))))))))) (lambda (v1) …) print n=3 v0=1 k (k 1) (lambda (v0) …) n=4 k =

Recommend


More recommend