Continuations and Transducer Composition Olin Shivers Matthew Might - - PowerPoint PPT Presentation

continuations and transducer composition
SMART_READER_LITE
LIVE PREVIEW

Continuations and Transducer Composition Olin Shivers Matthew Might - - PowerPoint PPT Presentation

Continuations and Transducer Composition Olin Shivers Matthew Might Georgia Tech PLDI 2006 The Big Idea Observation Some programs easier to write with transducer abstraction. Goal Design features and compilation story to support this


slide-1
SLIDE 1

Continuations and Transducer Composition

Olin Shivers Matthew Might

Georgia Tech

PLDI 2006

slide-2
SLIDE 2

The Big Idea

Observation

Some programs easier to write with transducer abstraction.

Goal

Design features and compilation story to support this abstraction.

slide-3
SLIDE 3

The Big Idea

Observation

Some programs easier to write with transducer abstraction.

Goal

Design features and compilation story to support this abstraction.

Oh...

Transducer ≡ Coroutine ≡ Process

slide-4
SLIDE 4

A computational analogy

The world of functions

◮ Agents are functions. ◮ Functions are stateless. ◮ Composed with ◦ operator: h = f ◦ g.

slide-5
SLIDE 5

A computational analogy

The world of functions

◮ Agents are functions. ◮ Functions are stateless. ◮ Composed with ◦ operator: h = f ◦ g.

The world of online transducers

◮ Agents are input/compute/output processes. ◮ Processes have local, bounded state. ◮ Composed with Unix | operator: h = g | f. Input Output Compute Input Output Compute

slide-6
SLIDE 6

Online transducers

◮ DSP networks

Convolve / integrate / filter / difference / . . .

◮ Network-protocol stacks (“micro-protocols”, layer integration)

packet-assembly / checksum / order / http-parse / html-lex / . . .

◮ Graphics processing

viewpoint-transform / clip1 / . . . / clip6 / z-divide / light / scan

◮ Stream processing ◮ Unix pipelines

. . .

slide-7
SLIDE 7

Optimisation across composition

Functional paradigm

f ◦ g optimised by β-reduction: f = λy . y + 3 g = λz . z + 5

slide-8
SLIDE 8

Optimisation across composition

Functional paradigm

f ◦ g optimised by β-reduction: f = λy . y + 3 g = λz . z + 5

  • = λm n . λx.m(n x)

(“Plumbing” made explicit in λ rep.)

slide-9
SLIDE 9

Optimisation across composition

Functional paradigm

f ◦ g optimised by β-reduction: f = λy . y + 3 g = λz . z + 5

  • = λm n . λx.m(n x)

(“Plumbing” made explicit in λ rep.) f ◦ g = (λmn.λx.m(nx))(λy.y + 3)(λz.z + 5)

slide-10
SLIDE 10

Optimisation across composition

Functional paradigm

f ◦ g optimised by β-reduction: f = λy . y + 3 g = λz . z + 5

  • = λm n . λx.m(n x)

(“Plumbing” made explicit in λ rep.) f ◦ g = (λmn.λx.m(nx))(λy.y + 3)(λz.z + 5) = λx.(λy.y + 3)((λz.z + 5)x) = λx.(λy.y + 3)(x + 5) = λx.(x + 5) + 3 = λx.x + (5 + 3) = λx.x + 8

slide-11
SLIDE 11

Optimisation across composition

Transducer paradigm

No good optimisation story. Optimisation across composition is key technology supporting abstraction: Enables construction by composition.

If only. . .

Input Output Compute Input Output Compute

slide-12
SLIDE 12

Optimisation across composition

Transducer paradigm

No good optimisation story. Optimisation across composition is key technology supporting abstraction: Enables construction by composition.

If only. . .

Compute Output Compute Input

slide-13
SLIDE 13

Optimisation across composition

Transducer paradigm

No good optimisation story. Optimisation across composition is key technology supporting abstraction: Enables construction by composition.

If only. . .

Input Compute/Compute Output

slide-14
SLIDE 14

Optimisation across composition

Transducer paradigm

No good optimisation story. Optimisation across composition is key technology supporting abstraction: Enables construction by composition.

If only. . .

slide-15
SLIDE 15

Optimisation across composition

Transducer paradigm

No good optimisation story. Optimisation across composition is key technology supporting abstraction: Enables construction by composition.

If only. . .

Thread #1 Thread #2 Thread #3 Thread #1 Thread #2 Thread #3

slide-16
SLIDE 16

Optimisation across composition

Transducer paradigm

No good optimisation story. Optimisation across composition is key technology supporting abstraction: Enables construction by composition.

If only. . .

Thread #1 Thread #3 Thread #3 Thread #2 Thread #2 Thread #1

slide-17
SLIDE 17

Strategy

◮ Build transducers from continuations.

slide-18
SLIDE 18

Strategy

◮ Build transducers from continuations. ◮ Build continuations from λ.

slide-19
SLIDE 19

Strategy

◮ Build transducers from continuations. ◮ Build continuations from λ. ◮ Handle λ well.

slide-20
SLIDE 20

Strategy

◮ Build transducers from continuations. ◮ Build continuations from λ. ◮ Handle λ well. ◮ Watch what happens.

slide-21
SLIDE 21

Tool: Continuation-passing style (CPS)

Restricted subset of λ calculus: Function calls do not return. Thus cannot write f(g(x)). Must pass extra argument—the continuation—to each call, to represent rest of computation:

(- a (* b c)) ⇒ (* b c (λ (temp) (- a temp halt)))

slide-22
SLIDE 22

Tool: Continuation-passing style (CPS)

Restricted subset of λ calculus: Function calls do not return. Thus cannot write f(g(x)). Must pass extra argument—the continuation—to each call, to represent rest of computation:

(- a (* b c)) ⇒ (* b c (λ (temp) (- a temp halt)))

CPS is the “assembler” of functional languages.

slide-23
SLIDE 23

CPS Payoff

CPS is universal representation of control & env. Construct encoding fun call call to λ

slide-24
SLIDE 24

CPS Payoff

CPS is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ

slide-25
SLIDE 25

CPS Payoff

CPS is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ

slide-26
SLIDE 26

CPS Payoff

CPS is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ sequencing call to λ

slide-27
SLIDE 27

CPS Payoff

CPS is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ sequencing call to λ conditional call to λ

slide-28
SLIDE 28

CPS Payoff

CPS is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ sequencing call to λ conditional call to λ exception call to λ

slide-29
SLIDE 29

CPS Payoff

CPS is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ sequencing call to λ conditional call to λ exception call to λ continuation call to λ

slide-30
SLIDE 30

CPS Payoff

CPS is universal representation of control & env. Construct encoding fun call call to λ fun return call to λ iteration call to λ sequencing call to λ conditional call to λ exception call to λ continuation call to λ coroutine switch call to λ . . . . . .

slide-31
SLIDE 31

Writing transducers with put and get

(define (send-fives) (put 5) (send-fives))

slide-32
SLIDE 32

Writing transducers with put and get

(define (send-fives) (put 5) (send-fives)) (define (doubler) (put (* 2 (get))) (doubler))

slide-33
SLIDE 33

Writing transducers with put and get

(define (send-fives) (put 5) (send-fives)) (define (doubler) (put (* 2 (get))) (doubler)) (define (integ sum) (let ((next-sum (+ sum (get)))) (put next-sum) (integ next-sum)))

slide-34
SLIDE 34

Tool: 3CPS & transducer pipelines

f x k u d

slide-35
SLIDE 35

Tool: 3CPS & transducer pipelines

f x k u d ExpCont: rest

  • f this stage’s

computation

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans (a transducer)

slide-36
SLIDE 36

Tool: 3CPS & transducer pipelines

f x k u d ExpCont: rest

  • f this stage’s

computation UpCont: rest

  • f upstream

computation

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans (a transducer)

slide-37
SLIDE 37

Tool: 3CPS & transducer pipelines

f x k u d ExpCont: rest

  • f this stage’s

computation UpCont: rest

  • f upstream

computation DownCont: rest

  • f downstream

computation

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans (a transducer)

slide-38
SLIDE 38

Transducers in 3CPS

Get & put in 3CPS

get x k u d = put x k u d =

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-39
SLIDE 39

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u ( ) put x k u d =

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-40
SLIDE 40

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u (λ x′ u′ . ) put x k u d =

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-41
SLIDE 41

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u (λ x′ u′ . k ) put x k u d =

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-42
SLIDE 42

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u (λ x′ u′ . k x′ ) put x k u d =

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-43
SLIDE 43

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u (λ x′ u′ . k x′ u′ d) put x k u d =

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-44
SLIDE 44

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u (λ x′ u′ . k x′ u′ d) put x k u d = d

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-45
SLIDE 45

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u (λ x′ u′ . k x′ u′ d) put x k u d = d x ( )

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-46
SLIDE 46

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u (λ x′ u′ . k x′ u′ d) put x k u d = d x (λ d′ . )

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-47
SLIDE 47

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u (λ x′ u′ . k x′ u′ d) put x k u d = d x (λ d′ . k unit )

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-48
SLIDE 48

Transducers in 3CPS

Get & put in 3CPS

get x k u d = u (λ x′ u′ . k x′ u′ d) put x k u d = d x (λ d′ . k unit u d′)

Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-49
SLIDE 49

Composing transducers in 3CPS

C1

Input Output Compute

C2

Input Output Compute

U D C

compose/pull c1 c2 Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-50
SLIDE 50

Composing transducers in 3CPS

C1

Input Output Compute

C2

Input Output Compute

U D C

compose/pull c1 c2 = λ u d . Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-51
SLIDE 51

Composing transducers in 3CPS

C1

Input Output Compute

C2

Input Output Compute

U D C

compose/pull c1 c2 = λ u d . c2 Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-52
SLIDE 52

Composing transducers in 3CPS

C1

Input Output Compute

C2

Input Output Compute

U D C

compose/pull c1 c2 = λ u d . c2 (λ d′ . ) d Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-53
SLIDE 53

Composing transducers in 3CPS

C1

Input Output Compute

C2

Input Output Compute

U D C

compose/pull c1 c2 = λ u d . c2 (λ d′ . c1 ) d Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-54
SLIDE 54

Composing transducers in 3CPS

C1

Input Output Compute

C2

Input Output Compute

U D C

compose/pull c1 c2 = λ u d . c2 (λ d′ . c1 u ) d Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-55
SLIDE 55

Composing transducers in 3CPS

C1

Input Output Compute

C2

Input Output Compute

U D C

compose/pull c1 c2 = λ u d . c2 (λ d′ . c1 u d′) d Semantic domains / Types

x ∈ Value k ∈ ExpCont = Value → UpCont → DownCont → Ans u ∈ UpCont = DownCont → Ans d ∈ DownCont = Value → UpCont → Ans c ∈ CmdCont = UpCont → DownCont → Ans

slide-56
SLIDE 56

Transducer data/control flow in 3CPS

get x k u d = u (λ x u′ . k x u′ d) put x k u d = d x (λ d′ . k unit u d) compose/pull c1 c2 = λ u d . c2 (λ d′ . c1 u d′) d

slide-57
SLIDE 57

Transducer data/control flow in 3CPS

get x k u d = u (λ x u′ . k x u′ d) put x k u d = d x (λ d′ . k unit u d) compose/pull c1 c2 = λ u d . c2 (λ d′ . c1 u d′) d All the “plumbing” made explicit in three short equations.

slide-58
SLIDE 58

A toy example

(λ () ; Put-5 (letrec ((lp1 (λ () (put 5) (lp1)))) (lp1))) (λ () ; Doubler (letrec ((lp2 (λ () (put (* 2 (get))) (lp2)))) (lp2)))

slide-59
SLIDE 59

After CPS conversion

(λ (k1 u1 d1) ; Put-5 (letrec ((lp1 (λ (k1a u1a d1a) (d1a 5 (λ (d1b) (lp1 k1a u1a d1b)))))) (lp1 k1 u1 d1))) (λ (k2 u2 d2) ; Doubler (letrec ((lp2 (λ (k2a u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 k2a u2b d2b)))))))) (lp2 k2 u2 d2)))

slide-60
SLIDE 60

(compose/pull put-5 doubler)

((λ (c1 c2) ; Compose/pull (λ (k u d) (c2 k (λ (d’) (c1 k u d’)) d))) (λ (k1 u1 d1) ; Put-5 (letrec ((lp1 (λ (k1a u1a d1a) (d1a 5 (λ (d1b) (lp1 k1a u1a d1b)))))) (lp1 k1 u1 d1))) (λ (k2 u2 d2) ; Doubler (letrec ((lp2 (λ (k2a u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 k2a u2b d2b)))))))) (lp2 k2 u2 d2))))

slide-61
SLIDE 61

(compose/pull put-5 doubler)

((λ (c1 c2) ; Compose/pull (λ (k u d) (c2 k (λ (d’) (c1 k u d’)) d))) (λ (k1 u1 d1) ; Put-5 (letrec ((lp1 (λ (k1a u1a d1a) (d1a 5 (λ (d1b) (lp1 k1a u1a d1b)))))) (lp1 k1 u1 d1))) (λ (k2 u2 d2) ; Doubler (letrec ((lp2 (λ (k2a u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 k2a u2b d2b)))))))) (lp2 k2 u2 d2)))) Eliminate useless variables (1991)

slide-62
SLIDE 62

(compose/pull put-5 doubler)

((λ (c1 c2) ; Compose/pull (λ (k u d) (c2 (λ (d’) (c1 d’)) d))) (λ (d1) ; Put-5 (letrec ((lp1 (λ (d1a) (d1a 5 (λ (d1b) (lp1 d1b)))))) (lp1 d1))) (λ (u2 d2) ; Doubler (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b)))))))) (lp2 u2 d2))))

slide-63
SLIDE 63

(compose/pull put-5 doubler)

((λ (c1 c2) ; Compose/pull (λ (k u d) (c2 (λ (d’) (c1 d’)) d))) (λ (d1) ; Put-5 (letrec ((lp1 (λ (d1a) (d1a 5 (λ (d1b) (lp1 d1b)))))) (lp1 d1))) (λ (u2 d2) ; Doubler (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b)))))))) (lp2 u2 d2)))) η-reduce (1935)

slide-64
SLIDE 64

(compose/pull put-5 doubler)

((λ (c1 c2) ; Compose/pull (λ (k u d) (c2 c1 d))) (λ (d1) ; Put-5 (letrec ((lp1 (λ (d1a) (d1a 5 lp1)))) (lp1 d1))) (λ (u2 d2) ; Doubler (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b)))))))) (lp2 u2 d2))))

slide-65
SLIDE 65

(compose/pull put-5 doubler)

((λ (c1 c2) ; Compose/pull (λ (k u d) (c2 c1 d))) (λ (d1) ; Put-5 (letrec ((lp1 (λ (d1a) (d1a 5 lp1)))) (lp1 d1))) (λ (u2 d2) ; Doubler (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b)))))))) (lp2 u2 d2)))) β-reduce whole thing (1935)

slide-66
SLIDE 66

(compose/pull put-5 doubler)

(λ (k u d) ((λ (u2 d2) ; Doubler (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b)))))))) (lp2 u2 d2))) (λ (d1) ; Put-5 (letrec ((lp1 (λ (d1a) (d1a 5 lp1)))) (lp1 d1))) d))

slide-67
SLIDE 67

(compose/pull put-5 doubler)

(λ (k u d) ((λ (u2 d2) ; Doubler (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b)))))))) (lp2 u2 d2))) (λ (d1) ; Put-5 (letrec ((lp1 (λ (d1a) (d1a 5 lp1)))) (lp1 d1))) d)) β again (1935)

slide-68
SLIDE 68

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b)))))))) (lp2 (λ (d1) ; Put-5 (letrec ((lp1 (λ (d1a) (d1a 5 lp1)))) (lp1 d1))) d)))

slide-69
SLIDE 69

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b)))))))) (lp2 (λ (d1) ; Put-5 (letrec ((lp1 (λ (d1a) (d1a 5 lp1)))) (lp1 d1))) d))) Hoist inner letrec. (1980’s)

slide-70
SLIDE 70

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b))))))) (lp1 (λ (d1a) (d1a 5 lp1)))) (lp2 (λ (d1) (lp1 d1)) d)))

slide-71
SLIDE 71

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b))))))) (lp1 (λ (d1a) (d1a 5 lp1)))) (lp2 (λ (d1) (lp1 d1)) d))) η-reduce (1935)

slide-72
SLIDE 72

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b))))))) (lp1 (λ (d1a) (d1a 5 lp1)))) (lp2 lp1 d)))

slide-73
SLIDE 73

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (u2a d2a) (u2a (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 u2b d2b))))))) (lp1 (λ (d1a) (d1a 5 lp1)))) (lp2 lp1 d))) Super-β: u2a = u2b = lp1 (2006)

slide-74
SLIDE 74

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (u2a d2a) (lp1 (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 lp1 d2b))))))) (lp1 (λ (d1a) (d1a 5 lp1)))) (lp2 lp1 d)))

slide-75
SLIDE 75

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (u2a d2a) (lp1 (λ (x u2b) (d2a (* 2 x) (λ (d2b) (lp2 lp1 d2b))))))) (lp1 (λ (d1a) (d1a 5 lp1)))) (lp2 lp1 d))) Eliminate useless u2a, u2b.

slide-76
SLIDE 76

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (d2a) (lp1 (λ (x) (d2a (* 2 x) (λ (d2b) (lp2 d2b))))))) (lp1 (λ (d1a) (d1a 5)))) (lp2 d)))

slide-77
SLIDE 77

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (d2a) (lp1 (λ (x) (d2a (* 2 x) (λ (d2b) (lp2 d2b))))))) (lp1 (λ (d1a) (d1a 5)))) (lp2 d))) η-reduce. (1935)

slide-78
SLIDE 78

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (d2a) (lp1 (λ (x) (d2a (* 2 x) lp2))))) (lp1 (λ (d1a) (d1a 5)))) (lp2 d)))

slide-79
SLIDE 79

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (d2a) (lp1 (λ (x) (d2a (* 2 x) lp2))))) (lp1 (λ (d1a) (d1a 5)))) (lp2 d))) Inline & β-reduce lp1 application. (1935)

slide-80
SLIDE 80

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (d2a) ((λ (d1a) (d1a 5)) (λ (x) (d2a (* 2 x) lp2)))))) (lp2 d)))

slide-81
SLIDE 81

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (d2a) ((λ (d1a) (d1a 5)) (λ (x) (d2a (* 2 x) lp2)))))) (lp2 d))) Two more β steps. (1935)

slide-82
SLIDE 82

(compose/pull put-5 doubler)

(λ (k u d) (letrec ((lp2 (λ (d2a) (d2a (* 2 5) lp2)))) (lp2 d))) Liftoff!

slide-83
SLIDE 83

Issues

◮ Linear “pipeline” topology wired in. Can we generalise? ◮ Can it be typed? ◮ OK, it works “by hand.” Can it be implemented?

slide-84
SLIDE 84

Issues

◮ Linear “pipeline” topology wired in. Can we generalise? ◮ Can it be typed? ◮ OK, it works “by hand.” Can it be implemented?

Yes.

slide-85
SLIDE 85

Channels in CPS Explicit channels permit non-linear control/data-flow topologies. Same optimisation story applies as in 3CPS case.

slide-86
SLIDE 86

Types for functional coroutines

(α, β) Channel /* coroutine connection: send an α, get a β. */ switch : α × (α, β) Channel → β × (α, β) Channel datatype (α,β) Channel = Chan of (α * (β,α) Channel) cont; fun switch(x, Chan k) = callcc (fn k’ => throw k (x, Chan k’)); Details are in the paper.

slide-87
SLIDE 87

Composing non-iterative computations

Some producers are truly recursive: (define (gen-fringe tree chan) (if (leaf? tree) (put (leaf:val tree) chan) (let ((chan (gen-fringe (tree:left tree) chan))) (gen-fringe (tree:right tree) chan)))) What if we compose with summing consumer?

slide-88
SLIDE 88

Composing non-iterative computations

Some producers are truly recursive: (define (gen-fringe tree chan) (if (leaf? tree) (put (leaf:val tree) chan) (let ((chan (gen-fringe (tree:left tree) chan))) (gen-fringe (tree:right tree) chan)))) What if we compose with summing consumer? Prototype compiler produces recursive, tree-walk summation.

slide-89
SLIDE 89

Experience

◮ Built prototype compiler for toy dialect of Scheme.

◮ Direct-style front end ◮ Includes call/cc ◮ Standard optimisations (β, η, . . . ) ◮ Plus ∆CFA (POPL 2006), abstract GC, abstract counting (ΓCFA,

ICFP 2006)

◮ Used for testing out Ph.D. analyses/optimisations

Nothing transducer/coroutine specific—just a machine for attacking CPS.

◮ Successfully fuses put5/doubler, integrators,

(rendered with coroutines/channels)

◮ Limiting reagent: Super-β.

slide-90
SLIDE 90

Related work

Transducer fusion

◮ Deforestation ◮ Haskell’s fold/build, unfold/destroy, etc.. ◮ Clu loop generators ◮ APL ◮ Filter fusion / Integrated layer processing

slide-91
SLIDE 91

Final thoughts

◮ It’s all about the representation.

◮ λ as essential control/env/data-structure ◮ CPS ⇒ Our main concern

becomes our only concern.

Once in CPS, generic optimisations suffice.

slide-92
SLIDE 92

Final thoughts

◮ It’s all about the representation.

◮ λ as essential control/env/data-structure ◮ CPS ⇒ Our main concern

becomes our only concern.

Once in CPS, generic optimisations suffice. This generalises to exotic control structures.

slide-93
SLIDE 93

Final thoughts

◮ It’s all about the representation.

◮ λ as essential control/env/data-structure ◮ CPS ⇒ Our main concern

becomes our only concern.

Once in CPS, generic optimisations suffice. This generalises to exotic control structures.

◮ Coroutines are the neglected control structure.

slide-94
SLIDE 94

Final thoughts

◮ It’s all about the representation.

◮ λ as essential control/env/data-structure ◮ CPS ⇒ Our main concern

becomes our only concern.

Once in CPS, generic optimisations suffice. This generalises to exotic control structures.

◮ Coroutines are the neglected control structure. ◮ Coroutines don’t have to be heavyweight.

(λ, CPS & static analysis are answer to efficiency issues.)

slide-95
SLIDE 95

Final thoughts

◮ It’s all about the representation.

◮ λ as essential control/env/data-structure ◮ CPS ⇒ Our main concern

becomes our only concern.

Once in CPS, generic optimisations suffice. This generalises to exotic control structures.

◮ Coroutines are the neglected control structure. ◮ Coroutines don’t have to be heavyweight.

(λ, CPS & static analysis are answer to efficiency issues.)

◮ Lots to do! (Stay tuned)

◮ Full-blown SML compiler ◮ TCP/IP (Foxnet) ◮ DSP libs.

slide-96
SLIDE 96

Thank you.