A Call-by-Need Lambda Calculus with Scoped Work Decorations David - - PowerPoint PPT Presentation

a call by need lambda calculus with scoped work
SMART_READER_LITE
LIVE PREVIEW

A Call-by-Need Lambda Calculus with Scoped Work Decorations David - - PowerPoint PPT Presentation

A Call-by-Need Lambda Calculus with Scoped Work Decorations David Sabel and Manfred Schmidt-Schau Goethe University Frankfurt am Main, Germany ATPS 2016, Vienna, Austria 1 Motivation Reasoning on program transformations, like map f ( map g


slide-1
SLIDE 1

1

A Call-by-Need Lambda Calculus with Scoped Work Decorations

David Sabel and Manfred Schmidt-Schauß

Goethe University Frankfurt am Main, Germany

ATPS 2016, Vienna, Austria

slide-2
SLIDE 2

Motivation

Reasoning on program transformations, like map f (map g xs) → map (λx.f (g x)) xs Are transformations optimizations / improvements? w.r.t. time consumption, i.e. the number of computation steps in a core language of Haskell: extended polymorphically typed lambda calculus with call-by-need evaluation

2/18

slide-3
SLIDE 3

Some Previous and Related Work

[Moran & Sands, POPL’99]: Improvement theory in an untyped call-by-need lambda calculus counting based on an abstract machine semantics tick-algebra for modular reasoning on improvements no concrete technique for list induction proofs [Hackett & Hutton, ICFP’14]: Improvement for worker-wrapper-transformations based on Moran & Sands’ tick algebra argue for the requirement of a typed language [Schmidt-Schauß & S., PPDP’15, IFL’15]: Improvement in call-by-need lambda calculi: untyped LR, typed LRP counting essential reduction steps of a small-step semantics core language with seq-operator proving list-laws being improvements, using work-decorations

3/18

slide-4
SLIDE 4

Motivation: Equational Reasoning for List-Expressions

Example: s1 := letrec xs=0 : xs in xs s2 := letrec y=((λx.x) 0), xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4}

4/18

slide-5
SLIDE 5

Motivation: Equational Reasoning for List-Expressions

Example: s1 := letrec xs=0 : xs in xs s2 := letrec y=((λx.x) 0), xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-6
SLIDE 6

Motivation: Equational Reasoning for List-Expressions

Example: s1 := letrec xs=0 : xs in xs s2 := letrec y=((λx.x) 0), xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-7
SLIDE 7

Motivation: Equational Reasoning for List-Expressions

Example: s1 ∼c 0 : 0 : (letrec xs=0 : xs in xs) s2 := letrec y=((λx.x) 0), xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-8
SLIDE 8

Motivation: Equational Reasoning for List-Expressions

Example: s1 ∼c 0 : 0 : (letrec xs=0 : xs in xs) s2 := letrec y=((λx.x) 0), xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-9
SLIDE 9

Motivation: Equational Reasoning for List-Expressions

Example: s1 ∼c 0 : 0 : (letrec xs=0 : xs in xs) s2 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-10
SLIDE 10

Motivation: Equational Reasoning for List-Expressions

Example: s1 ∼c 0 : 0 : (letrec xs=0 : xs in xs) s2 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-11
SLIDE 11

Motivation: Equational Reasoning for List-Expressions

Example: s1 ∼c 0 : 0 : (letrec xs=0 : xs in xs) s2 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s3 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s4 := letrec xs=λy.y : (xs y) in (xs 0)

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-12
SLIDE 12

Motivation: Equational Reasoning for List-Expressions

Example: s1 ∼c 0 : 0 : (letrec xs=0 : xs in xs) s2 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s3 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s4 := letrec xs=λy.y : (xs y) in (xs 0)

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-13
SLIDE 13

Motivation: Equational Reasoning for List-Expressions

Example: s1 ∼c 0 : 0 : (letrec xs=0 : xs in xs) s2 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s3 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s4 ∼c 0 : 0 : (letrec xs=λy.y : (xs y) in (xs 0))

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-14
SLIDE 14

Motivation: Equational Reasoning for List-Expressions

Example: s1 ∼c 0 : 0 : (letrec xs=0 : xs in xs) s2 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s3 ∼c 0 : 0 : (letrec xs=0 : 0 : xs in xs) s4 ∼c 0 : 0 : (letrec xs=λy.y : (xs y) in (xs 0)) further processing with the tails indeed shows si ∼c sj

Contextual equivalence: s ∼c t iff ∀ contexts C : C[s] ↓ ⇐ ⇒ C[t] ↓

Prove si ∼c sj for all i, j ∈ {1, 2, 3, 4} For list-expressions r1, r2 define: r1 R r2 iff r1 ∼c (h1 : t1), r2 ∼c (h2 : t2) such that h1 ∼c h2 and t1 R t2 Principle of co-induction: r1 gfp(R) r2 = ⇒ r1 ∼c r2

4/18

slide-15
SLIDE 15

Motivation: Reasoning Including Resources

In [Schmidt-Schauß & S., IFL 2015]: analogous reasoning, but w.r.t. improvement and cost-equivalence Improvement and Cost-Equivalence Improvement:

s t iff s ∼c t and ∀ closing contexts C : rln(C[s]) ≤ rln(C[t])

where rln(·) is the reduction length, counting essential reduction steps Cost-Equivalence:

s ≈ t iff s t and t s

5/18

slide-16
SLIDE 16

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 := letrec xs=0 : xs in xs s2 := letrec y=((λx.x) 0), xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

6/18

slide-17
SLIDE 17

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 := letrec xs=0 : xs in xs s2 := letrec y=((λx.x) 0), xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

6/18

slide-18
SLIDE 18

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 := letrec y=((λx.x) 0), xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

6/18

slide-19
SLIDE 19

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 := letrec y=((λx.x) 0), xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0)

6/18

slide-20
SLIDE 20

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ letrec y=0[1], xs=y : y : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps

6/18

slide-21
SLIDE 21

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ letrec xs=0[a→1] : 0[a→1] : xs in xs s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-22
SLIDE 22

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-23
SLIDE 23

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 := letrec y=((λx.x) 0), xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-24
SLIDE 24

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ letrec y=0[1], xs=y : ((λx.x) y) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-25
SLIDE 25

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ letrec xs=0[a→1] : ((λx.x) 0[a→1]) : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-26
SLIDE 26

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ letrec xs=0[a→1] : (0[a→1])[1] : xs in xs s4 := letrec xs=λy.y : (xs y) in (xs 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-27
SLIDE 27

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ 0[a→1] : 0[a→1,b→1] : (letrec xs=0[a→1] : 0[a→1,b→1] : xs in xs) s4 := letrec xs=λy.y : (xs y) in (xs 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-28
SLIDE 28

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ 0[a→1] : (0[a→1,b→1]) : (letrec xs=0[a→1] : 0[a→1,b→1] : xs in xs) s4 := letrec xs=λy.y : (xs y) in (xs 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-29
SLIDE 29

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ 0[a→1] : (0[a→1,b→1]) : (letrec xs=0[a→1] : 0[a→1,b→1] : xs in xs) s4 ≈ letrec xs=λy.y : (xs y) in ((λy.y : (xs y)) 0) Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-30
SLIDE 30

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ 0[a→1] : (0[a→1,b→1]) : (letrec xs=0[a→1] : 0[a→1,b→1] : xs in xs) s4 ≈ letrec xs=λy.y : (xs y) in (0 : (xs 0))[1] Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-31
SLIDE 31

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ 0[a→1] : (0[a→1,b→1]) : (letrec xs=0[a→1] : 0[a→1,b→1] : xs in xs) s4 ≈ letrec xs=λy.y : (xs y) in (0 : ((λy.y : (xs y)) 0))[1] Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-32
SLIDE 32

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ 0[a→1] : (0[a→1,b→1]) : (letrec xs=0[a→1] : 0[a→1,b→1] : xs in xs) s4 ≈ letrec xs=λy.y : (xs y) in (0 : (0 : (xs 0))[1])[1] Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-33
SLIDE 33

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ 0[a→1] : (0[a→1,b→1]) : (letrec xs=0[a→1] : 0[a→1,b→1] : xs in xs) s4 ≈ (0 : (0 : letrec xs=λy.y : (xs y) in (xs 0))[1])[1] Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-34
SLIDE 34

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ 0[a→1] : (0[a→1,b→1]) : (letrec xs=0[a→1] : 0[a→1,b→1] : xs in xs) s4 ≈ (0 : (0 : letrec xs=λy.y : (xs y) in (xs 0))[1])[1] further processing shows s1 s2 s3 s4 Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

6/18

slide-35
SLIDE 35

Motivation: Reasoning including Resources

Equational reasoning w.r.t. cost equivalence: s1 ≈ 0 : 0 : (letrec xs=0 : xs in xs) s2 ≈ 0[a→1] : 0[a→1] : (letrec xs=0[a→1] : 0[a→1] : xs in xs) s3 ≈ 0[a→1] : (0[a→1,b→1]) : (letrec xs=0[a→1] : 0[a→1,b→1] : xs in xs) s4 ≈ (0 : (0 : letrec xs=λy.y : (xs y) in (xs 0))[1])[1] further processing shows s1 s2 s3 s4 Work-decorations to keep track of rln-work:

[n] := n essential reduction steps [a→n] := n shared essential reduction steps

(label a marks the sharing)

Goal of current paper: Define and analyze the exact semantics of [a→n]

6/18

slide-36
SLIDE 36

Our Contribution

Exact semantics of (shared) work-decorations [a→n] (and [n]) Prove computation rules, like S[s[a→n], t[a→n]] S[s, t][n] The notation [a→n] is ambiguous, e.g. in letrec x=λy.s[a→n] in C[x] when inlining the binding for x: Possibilities: letrec x=λy.s[a→n] in C[λy.s[a→n]] letrec x=λy.s[a→n] in C[λy.s[b→n]] (where b is fresh)

7/18

slide-37
SLIDE 37

Our Contribution

Exact semantics of (shared) work-decorations [a→n] (and [n]) Prove computation rules, like S[s[a→n], t[a→n]] S[s, t][n] The notation [a→n] is ambiguous, e.g. in letrec x=λy.s[a→n] in C[x] when inlining the binding for x: Possibilities: letrec x=λy.s[a→n] in C[λy.s[a→n]] letrec x=λy.s[a→n] in C[λy.s[b→n]] (where b is fresh) We change the notation to add a scoping for work-decorations: Instead of [a→n] we use a binding a := n and a label [a]

7/18

slide-38
SLIDE 38

Our Contribution

Exact semantics of (shared) work-decorations [a→n] (and [n]) Prove computation rules, like S[s[a→n], t[a→n]] S[s, t][n] The notation [a→n] is ambiguous, e.g. in letrec x=λy.s[a→n] in C[x] when inlining the binding for x: Possibilities: letrec x=λy.s[a→n] in C[λy.s[a→n]] letrec x=λy.s[a→n] in C[λy.s[b→n]] (where b is fresh) We change the notation to add a scoping for work-decorations: Instead of [a→n] we use a binding a := n and a label [a] Examples: letrec a := n, x=λy.s[a] in C[x] letrec x=λy.(letrec a := n in s[a]) in C[x]

7/18

slide-39
SLIDE 39

The Calculus LRPw

LRPw extends LRP by work decorations Types: τ ∈ Typ ::= A | (τ1 → τ2) | K τ1 . . . τar(K) ρ ∈ PTyp ::= τ | λA.ρ Expressions: u ∈ PExprF ::= ΛA1. . . . .ΛAk.λx.s s, t ∈ ExprF ::= u | x :: ρ | (s τ) | (s t) | (seq s t) | (letrec bind1, . . . , bindm in t) | (cK,i :: τ s1 . . . sar(cK,i)) | (caseK s of (patK,1 -> t1) . . . (patK,|DK| -> t|DK|)) | s[a], where a is a label patK,i ::= (cK,i :: τ x1 :: τ1 . . . xar(cK,i) :: τar(cK,i)) bindi ::= xi :: ρi=si | a := n, where n ∈ N and a is a label

8/18

slide-40
SLIDE 40

The Calculus LRPw: Operational Semantics

Normal Order Reduction LRPw − − − − → Small-step reduction relation Call-by-need strategy using reduction contexts R Several reduction rules, e.g.

(lbeta) ((λx.s) t) → letrec x = t in s (cp-in) letrec x1 = (λy.t), {xi = xi−1}m

i=2, Env in C[xm]

→ letrec x1 = (λy.t), {xi = xi−1}m

i=2, Env in C[(λy.t)]

(seq-c) (seq v t) → t if v is a value (case-c) caseK (c t1 . . . tn) . . . ((c y1 . . . yn) → s) . . .

→ letrec {yi = ti}n

i=1 in s

. . .

(letwn) letrec . . . a := n . . . C[(s[a])] → letrec . . . a := n−1 . . . C[s[a]] (letw0) letrec . . . a := 0 . . . C[(s[a])] → letrec . . . a := 0 . . . C[s]

9/18

slide-41
SLIDE 41

Contextual Equivalence in LRPw

Convergence A weak head normal form (WHNF) is a value: λx.s, ΛA.u, or c− → s . letrec Env in v, where v is a value letrec x1 = c− → s , {xi = xi−1}m

i=2, Env in xm

Convergence: s ↓ t iff s

LRPw,∗

− − − − − → t ∧ t is a WHNF s ↓ iff ∃t : s ↓ t. Contextual Equivalence For s, t :: ρ, s ∼c t iff for all contexts C[· :: ρ]: C[s]↓ ⇐ ⇒ C[t]↓ Program transformation P is correct iff (s P − → t = ⇒ s ∼c t)

10/18

slide-42
SLIDE 42

Improvement in LRPw

Counting Essential Reductions For {lbeta, letwn} = A0 ⊆ A ⊆ A = {lbeta, case, seq, letwn}: rlnA(t) :=      number of A-reductions in t

LRPw,∗

− − − − − → t′, if t ↓ t′ ∞,

  • therwise

Improvement Relation For s, t :: ρ, s improves t (written s A t) iff s ∼c t, and for all C[· :: ρ] s.t. C[s], C[t] are closed: rlnA(C[s]) ≤ rlnA(C[t]). We write s ≈A t ⇐ ⇒ s A t ∧ t A s (cost-equivalence) Program transformation P is an improvement iff s P − → t = ⇒ t A s

11/18

slide-43
SLIDE 43

Do Work-Decorations Change the Semantics?

LRP LRPw= LRP+ a := n + [a] Questions: Is there a change w.r.t. contextual equivalence? Is there a change w.r.t. improvement and cost-equivalence?

12/18

slide-44
SLIDE 44

Q1: Is there a change w.r.t. contextual equivalence?

No, since: Theorem The embedding of LRP into LRPw w.r.t. ∼c is conservative and the calculi LRP and LRPw are isomorphic: The isomorphism is [s]∼c,LRPw = [rmw(s)]∼c,LRP where rmw(·) removes the decorations.

13/18

slide-45
SLIDE 45

Q2: Is there a change w.r.t. cost-equivalence?

No, if (seq)-reductions do not count for rln: Theorem Let A0 ⊆ A ⊂ A, such that seq ∈ A. Then LRP and LRPw are isomorphic w.r.t. ≈A. Encode letrec a := n, Env in s as letrec xa := idn+1, Env[seq xa t/t[a]] in s[seq xa t/t[a]]

14/18

slide-46
SLIDE 46

Q2: Is there a change w.r.t. cost-equivalence?

No, if (seq)-reductions do not count for rln: Theorem Let A0 ⊆ A ⊂ A, such that seq ∈ A. Then LRP and LRPw are isomorphic w.r.t. ≈A. Encode letrec a := n, Env in s as letrec xa := idn+1, Env[seq xa t/t[a]] in s[seq xa t/t[a]] Yes, for the isomorphism property if A = A Proposition Let A = A and let c1 and c2 be different constants. Then letrec a := 1 in (Pair c[a]

1

c[a]

2 )

is not equivalent w.r.t. ≈A to any LRP-expression.

14/18

slide-47
SLIDE 47

Q2: Is there a change w.r.t. cost-equivalence?

No, if (seq)-reductions do not count for rln: Theorem Let A0 ⊆ A ⊂ A, such that seq ∈ A. Then LRP and LRPw are isomorphic w.r.t. ≈A. Encode letrec a := n, Env in s as letrec xa := idn+1, Env[seq xa t/t[a]] in s[seq xa t/t[a]] Yes, for the isomorphism property if A = A Proposition Let A = A and let c1 and c2 be different constants. Then letrec a := 1 in (Pair c[a]

1

c[a]

2 )

is not equivalent w.r.t. ≈A to any LRP-expression. Open for conservativity: s ≈A,LRP t = ⇒ s ≈A,LRPw t?

14/18

slide-48
SLIDE 48

Equations for Transformations

Theorem Let A0 ⊆ A ⊆ A. If s

LRPw,a

− − − − − → t where a ∈ A then s ≈A t[1] If s

C,a

− − → t where a ∈ A then t A s If s

C,a

− − → t, a is (lll), (cp), (letw0), (cpx), (cpcx), (abs), (abse), (lwas), (ucp), (gc), (gcW), then s ≈A t

(lll)

letrec Env1 in letrec Env2 in s → letrec Env1, Env2 in s

(lll)

letrec Env1, x=(letrec Env2 in s) in t → letrec Env1, Env2, x=s in t

(lll)

(letrec Env in s) t → letrec Env in (s t)

(gc)

letrec {xi=si}n

i=1, Env in t → letrec Env in t, if ∀i : xi ∈ FV (t, Env)

(gc)

letrec {xi=si}n

i=1 in t → t, if for all i : xi ∈ FV (t)

(gcW) letrec {ai := ni}m

i=1, Env in s → letrec Env in s, if all ai do not occur in Env, s

(gcW) letrec {ai := ni}m

i=1 in s → s, if a1, . . . , am do not occur in s

(cpx) letrec x=y, . . . C[x] . . . → letrec x=y, . . . C[y] . . . (cpcx) letrec x=(c t1. . .tn) . . . C[x] . . . → letrec x=(c y1. . .yn), {yi=ti}n

i=1 . . . C[c y1. . .yn] . . .

. . .

15/18

slide-49
SLIDE 49

Computation Rules

Theorem Let A0 ⊆ A ⊆ A and S, T be surface contexts

1

(s[n])[m] ≈A s[n+m]

2

letrec a := n in (s[a])[a] ≈A letrec a := n in s[a]

3

S[letrec a := n in T[s[a]]] A letrec a := n in S[T[s]][a]

4

S[letrec a := n in T[s[a]]] ≈A letrec a := n in S[T[s]][a], if S[T] is strict.

5

letrec a := n, b := m in (s[a])[b] ≈A letrec a := n, b := m in (s[b])[a]

6

letrec a := n in S[s[a]

1 , . . ., s[a] n ] A letrec a := n in S[s1, . . ., sn][a].

7

letrec a := n in S[s[a]

1 , . . ., s[a] n ] ≈A letrec a := n in S[s1, . . ., sn][a],

if some hole in S is in strict position

16/18

slide-50
SLIDE 50

Conclusion

LRPw = Call-by-need calculus with scoped work-decorations LRPw not obviously encodable in LRP Several improvements and cost-equivalences hold in LRPw Expected computation rules hold in LRPw

17/18

slide-51
SLIDE 51

Further Work

Apply the results to prove further improvements and cost-equivalences Automation of program optimization Automation of proving improvement Space-improvements

18/18