Compiling with Continuations Matthew Might University of Utah - - PowerPoint PPT Presentation

compiling with continuations
SMART_READER_LITE
LIVE PREVIEW

Compiling with Continuations Matthew Might University of Utah - - PowerPoint PPT Presentation

Compiling with Continuations Matthew Might University of Utah matt.might.net www.ucombinator.org Administrivia Major update to course notes Final project after Thanksgiving Today Exceptions from continuations


slide-1
SLIDE 1

Compiling with Continuations

Matthew Might University of Utah matt.might.net www.ucombinator.org

slide-2
SLIDE 2

Administrivia

  • Major update to course notes
  • Final project after Thanksgiving
slide-3
SLIDE 3

Today

  • Exceptions from continuations
  • Continuation-passing style, CPS
  • How to compile continuations
slide-4
SLIDE 4

History

  • Plotkin, 1975
  • Steele, 1978
slide-5
SLIDE 5

Exceptions

  • (try expression catch-procedure)
  • (throw exception)
slide-6
SLIDE 6

Exceptions

(dynamic-wind before-thunk thunk after-thunk)

slide-7
SLIDE 7

Implementation

  • Save and restore the stack + context
  • Convert to continuation-passing style
slide-8
SLIDE 8

Stack save/restore

  • Pro: Works for most languages
  • Con: Stacks are large; expensive
slide-9
SLIDE 9

*context in C

  • setcontext()
  • getcontext()
  • makecontext()
  • swapcontext()
slide-10
SLIDE 10

Continuation-passing style (CPS)

slide-11
SLIDE 11

The CPS constraints

All calls are tail calls.

slide-12
SLIDE 12

The CPS constraints

Function calls never return.

slide-13
SLIDE 13

In practice

  • All arguments must be atomic
  • Atomic = must halt & be pure
slide-14
SLIDE 14

CPS grammar

v ∈ VAR ::= identifier REF ::= v ℓ lam ∈ LAM ::= (λ (v1 · · · vn) call)ℓ e, f ∈ EXP = REF + LAM call ∈ CALL ::= (f e1 · · · en)ℓ

slide-15
SLIDE 15

Intuition

  • Callers pass current continuation to callees
  • Callees invoke the continution once done
slide-16
SLIDE 16

Example

(lambda (x) x)

slide-17
SLIDE 17

Example

(lambda (x k) (k x))

slide-18
SLIDE 18

Example

(lambda (x return) (return x))

slide-19
SLIDE 19

Example

(define (f n) (if (= n 0) 1 (* n (f (- n 1)))))

slide-20
SLIDE 20

Example

(define (f n return) (if (= n 0) 1 (* n (f (- n 1)))))

slide-21
SLIDE 21

Example

(define (f n return) (if (= n 0) (return 1) (* n (f (- n 1)))))

slide-22
SLIDE 22

Example

(define (f n return) (if (= n 0) (return 1) (f (- n 1) (lambda (m) (return (* n m))))))

slide-23
SLIDE 23

Example

(define (f n return) (=$ n 0 (lambda (zero?) (if zero? (return 1) (-$ n 1 (lambda (sub) (f sub (lambda (mul) (*$ n mul return))))))))

slide-24
SLIDE 24

Example

(define (f a n return) (=$ n 0 (lambda (zero?) (if zero? (return a) (* a n (lambda (an) (- n 1 (lambda (n1) (f an n1 return)))))))))

slide-25
SLIDE 25

Example

(define (fib n return) (<=$ n 0 (lambda (zero?) (if zero? (return n) (- n 1 (lambda (n1) (- n 2 (lambda (n2) (fib n1 (lambda (f1) (fib n2 (lambda (f2) (+ f1 f2)))))))))))))

slide-26
SLIDE 26

CPS: All calls, no return

So why have a stack?

slide-27
SLIDE 27

Procedure call

It’s goto.

slide-28
SLIDE 28

Translating

(+ 1 2 (lambda (a) …)) a = 1 + 2 ;

slide-29
SLIDE 29

Translating

(f x y cc) $a1 = x $a2 = y $a3 = cc goto f

slide-30
SLIDE 30

Example

(define (fib n return) (<=$ n 0 (lambda (zero?) (if zero? (return n) (- n 1 (lambda (n1) (- n 2 (lambda (n2) (fib n1 (lambda (f1) (fib n2 (lambda (f2) (+ f1 f2))))))))))))) fib: mov n $a1 mov return $a2 leq $t1 n 0 bnz $t1 done sub n1 n 1 sub n2 n 2 ??? done: mov $a1 n goto return

slide-31
SLIDE 31

Translating

(lambda (v1 ... vN) body) (closure (lambda ($e v1 ... vN) body) (make-env ...))

slide-32
SLIDE 32

Translating

(closure (lambda ($e v1 ... vN) $body) (make-env ...)) { lambda = &&label , env = { fv1 = ... } }

slide-33
SLIDE 33

Compiling call/cc

(lambda (f current-continuation) (f (lambda (x later-continuation) (current-continuation x)) current-continuation))) call/cc =>

slide-34
SLIDE 34

Example

U

slide-35
SLIDE 35

Example

map

slide-36
SLIDE 36

In the wild

Web programming.

slide-37
SLIDE 37

Holiday puzzle

(call/cc call/cc)