91
How to Replace Sharing with Fortune-Telling! Jennifer Hackett University of Nottingham

How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

  • Upload
    others

  • View
    6

  • Download
    2

Embed Size (px)

Citation preview

Page 1: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

How to Replace Sharing with Fortune-Telling!

Jennifer Hackett University of Nottingham

Page 2: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Introduction

Page 3: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Introduction

‣ This is a talk about cost semantics — mathematical models of the cost of evaluating programs

Page 4: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Introduction

‣ This is a talk about cost semantics — mathematical models of the cost of evaluating programs

‣ This is also a talk about evaluation strategies, and how to characterise lazy evaluation

Page 5: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Introduction

‣ This is a talk about cost semantics — mathematical models of the cost of evaluating programs

‣ This is also a talk about evaluation strategies, and how to characterise lazy evaluation

‣ Finally, this is a talk about what counts as a “pure” programming language

Page 6: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Improvement Theory

Page 7: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Improvement Theory

‣ I work on improvement theory, an algebraic approach to reasoning about the relative efficiency of programs

Page 8: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Improvement Theory

‣ I work on improvement theory, an algebraic approach to reasoning about the relative efficiency of programs

‣ Essentially the study of the algebraic properties of the improvement relation:

Page 9: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Improvement Theory

‣ I work on improvement theory, an algebraic approach to reasoning about the relative efficiency of programs

‣ Essentially the study of the algebraic properties of the improvement relation:

‣ e ▹ ̰e' ⇔ ∀C.C[e]⇓k ⇒ C[e']⇓≤k

Page 10: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Improvement Theory

‣ I work on improvement theory, an algebraic approach to reasoning about the relative efficiency of programs

‣ Essentially the study of the algebraic properties of the improvement relation:

‣ e ▹ ̰e' ⇔ ∀C.C[e]⇓k ⇒ C[e']⇓≤k

‣ We say "e is improved by e'"

Page 11: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Value vs. Call-by-Name

Page 12: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Value vs. Call-by-Name

‣ Two ways to evaluate function application…

Page 13: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Value vs. Call-by-Name

‣ Two ways to evaluate function application…

‣ Call-by-Value evaluates f(x) by first reducing x to some sort of normal form, then substitutes it into the body of f

Page 14: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Value vs. Call-by-Name

‣ Two ways to evaluate function application…

‣ Call-by-Value evaluates f(x) by first reducing x to some sort of normal form, then substitutes it into the body of f

‣ Easy to understand and costs are compositional, but has strict semantics, i.e. divergence propagates

Page 15: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Value vs. Call-by-Name

‣ Two ways to evaluate function application…

‣ Call-by-Value evaluates f(x) by first reducing x to some sort of normal form, then substitutes it into the body of f

‣ Easy to understand and costs are compositional, but has strict semantics, i.e. divergence propagates

‣ Call-by-Name evaluates f(x) by substituting x into the body of f regardless of whether it is in a normal form

Page 16: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Value vs. Call-by-Name

‣ Two ways to evaluate function application…

‣ Call-by-Value evaluates f(x) by first reducing x to some sort of normal form, then substitutes it into the body of f

‣ Easy to understand and costs are compositional, but has strict semantics, i.e. divergence propagates

‣ Call-by-Name evaluates f(x) by substituting x into the body of f regardless of whether it is in a normal form

‣ Non-strict, so we can work with divergent terms like any other value, but terms appearing once in a program could be evaluated several times

Page 17: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Need

Page 18: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Need‣ Combines non-strict semantics of call-by-name with the

evaluate-once property of call-by-value

Page 19: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Need‣ Combines non-strict semantics of call-by-name with the

evaluate-once property of call-by-value

‣ Achieves this by sharing the results of let-bound terms, storing those results in a heap

Page 20: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Need‣ Combines non-strict semantics of call-by-name with the

evaluate-once property of call-by-value

‣ Achieves this by sharing the results of let-bound terms, storing those results in a heap

‣ Ignoring efficiency, this is equivalent to call-by-name

Page 21: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Call-by-Need‣ Combines non-strict semantics of call-by-name with the

evaluate-once property of call-by-value

‣ Achieves this by sharing the results of let-bound terms, storing those results in a heap

‣ Ignoring efficiency, this is equivalent to call-by-name

‣ But if we want to consider costs, we need to reason about impure heap updates…

Page 22: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Launchbury’s Semantics

Page 23: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Launchbury’s Semantics

Γ : λx . e ⇓ Γ : λx . e (Lam)

Page 24: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Launchbury’s Semantics

Γ : λx . e ⇓ Γ : λx . e (Lam)Γ : e ⇓ Δ : λy . e′� Δ : e′�[x /y] ⇓ Θ : v

Γ : e x ⇓ Θ : v(App)

Page 25: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Launchbury’s Semantics

Γ : λx . e ⇓ Γ : λx . e (Lam)Γ : e ⇓ Δ : λy . e′� Δ : e′�[x /y] ⇓ Θ : v

Γ : e x ⇓ Θ : v(App)

Γ, x ↦ e1 : e2 ⇓ Δ : vΓ : let x = e1 in e2 ⇓ Δ : v

(Let)

Page 26: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Launchbury’s Semantics

Γ : λx . e ⇓ Γ : λx . e (Lam)Γ : e ⇓ Δ : λy . e′� Δ : e′�[x /y] ⇓ Θ : v

Γ : e x ⇓ Θ : v(App)

Γ : e ⇓ Δ : vΓ, x ↦ e : x ⇓ Δ, x ↦ v : ̂v

(Var)Γ, x ↦ e1 : e2 ⇓ Δ : v

Γ : let x = e1 in e2 ⇓ Δ : v(Let)

Page 27: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Launchbury’s Semantics

Γ : λx . e ⇓ Γ : λx . e (Lam)Γ : e ⇓ Δ : λy . e′� Δ : e′�[x /y] ⇓ Θ : v

Γ : e x ⇓ Θ : v(App)

Γ : e ⇓ Δ : vΓ, x ↦ e : x ⇓ Δ, x ↦ v : ̂v

(Var)Γ, x ↦ e1 : e2 ⇓ Δ : v

Γ : let x = e1 in e2 ⇓ Δ : v(Let)

Note the heap update!

Page 28: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Launchbury’s Semantics

Γ : λx . e ⇓ Γ : λx . e (Lam)Γ : e ⇓ Δ : λy . e′� Δ : e′�[x /y] ⇓ Θ : v

Γ : e x ⇓ Θ : v(App)

Γ : e ⇓ Δ : vΓ, x ↦ e : x ⇓ Δ, x ↦ v : ̂v

(Var)Γ, x ↦ e1 : e2 ⇓ Δ : v

Γ : let x = e1 in e2 ⇓ Δ : v(Let)

0c d

c+d+1

c

c+1 c+1

c

Page 29: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Can we “purify” laziness?

Page 30: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Can we “purify” laziness?

‣ On the face of it, lazy evaluation is inherently impure; the updates are an integral part

Page 31: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Can we “purify” laziness?

‣ On the face of it, lazy evaluation is inherently impure; the updates are an integral part

‣ But the actual results hide that impurity from us…

Page 32: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Can we “purify” laziness?

‣ On the face of it, lazy evaluation is inherently impure; the updates are an integral part

‣ But the actual results hide that impurity from us…

‣ …so maybe we can construct a cost-equivalent semantics that “feels purer”?

Page 33: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Should we care?

Page 34: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Should we care?‣ Consider common

subexpression elimination:

‣ M[N] ⟶ let x = N in M[x]

Page 35: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Should we care?‣ Consider common

subexpression elimination:

‣ M[N] ⟶ let x = N in M[x]

‣ Seems obvious that this transformation should never degrade time performance…

Page 36: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Should we care?‣ Consider common

subexpression elimination:

‣ M[N] ⟶ let x = N in M[x]

‣ Seems obvious that this transformation should never degrade time performance…

‣ But it’s actually pretty hard to prove this!

Page 37: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Should we care?‣ Consider common

subexpression elimination:

‣ M[N] ⟶ let x = N in M[x]

‣ Seems obvious that this transformation should never degrade time performance…

‣ But it’s actually pretty hard to prove this! (Image from Schmidt-Schauß and Sabel, 2017)

Page 38: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Should we care?

Page 39: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Should we care?

‣ Another problem: if we want a denotational semantics that captures cost information, we have to thread state through everything we do

Page 40: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Should we care?

‣ Another problem: if we want a denotational semantics that captures cost information, we have to thread state through everything we do

‣ This makes reasoning about our semantics more difficult

Page 41: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

A Visit from St. Nicholas...

(Photo by Huhu Uet)

Page 42: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

A Visit from St. Nicholas...

(Photo by Huhu Uet)

Page 43: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

A Visit from St. Nicholas...

‣ “I see you when you're sleeping! I know when you're awake!

‣ I know which terms you'll evaluate, so do those early, for goodness' sake!"

(Photo by Huhu Uet)

Page 44: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Who Needs Determinism, Anyway?

Page 45: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Who Needs Determinism, Anyway?

‣ Nondeterministic automata can be thought of as automata that can “guess” the right path to take

Page 46: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Who Needs Determinism, Anyway?

‣ Nondeterministic automata can be thought of as automata that can “guess” the right path to take

‣ By analogy, we can implement our foresight idea with a nondeterministic semantics

Page 47: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Who Needs Determinism, Anyway?

‣ Nondeterministic automata can be thought of as automata that can “guess” the right path to take

‣ By analogy, we can implement our foresight idea with a nondeterministic semantics

‣ In other words, we try both eager evaluation and discarding, and pick the path that leads to the cheapest successful evaluation

Page 48: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Clairvoyant Call-by-Value

Γ : λx . e ⇓0 Γ : λx . e (Lam)Γ : e ⇓c Δ : λy . e′� Δ : e′�[x /y] ⇓d Θ : v

Γ : e x ⇓c+d+1 Θ : v(App)

Γ : e ⇓c Δ : vΓ, x ↦ e : x ⇓c+1 Δ, x ↦ v : ̂v

(Var)Γ, x ↦ e1 : e2 ⇓c Δ : v

Γ : let x = e1 in e2 ⇓c+1 Δ : v(Let)

Page 49: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Clairvoyant Call-by-Value

Γ : λx . e ⇓0 Γ : λx . e (Lam)Γ : e ⇓c Δ : λy . e′� Δ : e′�[x /y] ⇓d Θ : v

Γ : e x ⇓c+d+1 Θ : v(App)

Γ : e1 ⇓c Δ : v1 Δ, x ↦ v1 : e2 ⇓d Θ : v2

Γ : let x = e1 in e2 ⇓c+d+1 Θ : v2(Let) Γ : e ⇓c Δ : v

Γ, x ↦ e : x ⇓c+1 Δ, x ↦ v : ̂v(Var)

Page 50: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Clairvoyant Call-by-Value

Γ : λx . e ⇓0 Γ : λx . e (Lam)Γ : e ⇓c Δ : λy . e′� Δ : e′�[x /y] ⇓d Θ : v

Γ : e x ⇓c+d+1 Θ : v(App)

Γ, x ↦ v : x ⇓1 Γ, x ↦ v : ̂v (Var)Γ : e1 ⇓c Δ : v1 Δ, x ↦ v1 : e2 ⇓d Θ : v2

Γ : let x = e1 in e2 ⇓c+d+1 Θ : v2(Let)

Page 51: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Clairvoyant Call-by-Value

Γ : λx . e ⇓0 Γ : λx . e (Lam)Γ : e ⇓c Δ : λy . e′� Δ : e′�[x /y] ⇓d Θ : v

Γ : e x ⇓c+d+1 Θ : v(App)

Γ, x ↦ v : x ⇓1 Γ, x ↦ v : ̂v (Var)Γ : e1 ⇓c Δ : v1 Δ, x ↦ v1 : e2 ⇓d Θ : v2

Γ : let x = e1 in e2 ⇓c+d+1 Θ : v2(Let)

Γ, e2 ⇓c Δ : vΓ : let x = e1 in e2 ⇓c+1 Δ : v

(Skip)

Page 52: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Clairvoyant Call-by-Value

Γ : λx . e ⇓0 Γ : λx . e (Lam)Γ : e ⇓c Δ : λy . e′� Δ : e′�[x /y] ⇓d Θ : v

Γ : e x ⇓c+d+1 Θ : v(App)

Γ, x ↦ v : x ⇓1 Γ, x ↦ v : ̂v (Var)Γ : e1 ⇓c Δ : v1 Δ, x ↦ v1 : e2 ⇓d Θ : v2

Γ : let x = e1 in e2 ⇓c+d+1 Θ : v2(Let)

Γ, e2 ⇓c Δ : vΓ : let x = e1 in e2 ⇓c+1 Δ : v

(Skip)Ambiguous…

Page 53: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Clairvoyant Call-by-Value

Γ : λx . e ⇓0 Γ : λx . e (Lam)Γ : e ⇓c Δ : λy . e′� Δ : e′�[x /y] ⇓d Θ : v

Γ : e x ⇓c+d+1 Θ : v(App)

Γ, x ↦ v : x ⇓1 Γ, x ↦ v : ̂v (Var)Γ : e1 ⇓c Δ : v1 Δ, x ↦ v1 : e2 ⇓d Θ : v2

Γ : let x = e1 in e2 ⇓c+d+1 Θ : v2(Let)

Γ, e2 ⇓c Δ : vΓ : let x = e1 in e2 ⇓c+1 Δ : v

(Skip) …but no more updates

Ambiguous…

Page 54: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need

Page 55: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need‣ Given a term e and a heap Γ that consists only of values:

Page 56: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need‣ Given a term e and a heap Γ that consists only of values:

‣ If Γ : e⇓k in call-by-need then Γ : e⇓k in clairvoyant call-by-value

Page 57: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need‣ Given a term e and a heap Γ that consists only of values:

‣ If Γ : e⇓k in call-by-need then Γ : e⇓k in clairvoyant call-by-value

‣ Proof sketch: Traverse the derivation of Γ : e⇓k replacing the CBN Let rules with CCBV Let or Skip. If the bound variable is used later, splice in the subtree from there as a premise of the CCBV Let; this splicing is safe because the subtree will only use variables in scope at the point of the let binding. Otherwise, just use Skip.

Page 58: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need‣ Given a term e and a heap Γ that consists only of values:

‣ If Γ : e⇓k in call-by-need then Γ : e⇓k in clairvoyant call-by-value

‣ Proof sketch: Traverse the derivation of Γ : e⇓k replacing the CBN Let rules with CCBV Let or Skip. If the bound variable is used later, splice in the subtree from there as a premise of the CCBV Let; this splicing is safe because the subtree will only use variables in scope at the point of the let binding. Otherwise, just use Skip.

‣ If Γ : e⇓k in clairvoyant call-by-value then there is some k' ≤ k such that Γ : e⇓k' in call-by-need

Page 59: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need‣ Given a term e and a heap Γ that consists only of values:

‣ If Γ : e⇓k in call-by-need then Γ : e⇓k in clairvoyant call-by-value

‣ Proof sketch: Traverse the derivation of Γ : e⇓k replacing the CBN Let rules with CCBV Let or Skip. If the bound variable is used later, splice in the subtree from there as a premise of the CCBV Let; this splicing is safe because the subtree will only use variables in scope at the point of the let binding. Otherwise, just use Skip.

‣ If Γ : e⇓k in clairvoyant call-by-value then there is some k' ≤ k such that Γ : e⇓k' in call-by-need

‣ Proof sketch: Similar, but in reverse.

Page 60: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need, continued…

Page 61: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need, continued…

‣ What this means is that the call-by-need cost is the minimal clairvoyant call-by-value cost

Page 62: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need, continued…

‣ What this means is that the call-by-need cost is the minimal clairvoyant call-by-value cost

‣ As a corollary, we know that ∀C.C[e]⇓k ⇒ C[e']⇓≤k in call-by-need if and only if ∀C.C[e]⇓k ⇒ C[e']⇓≤k in clairvoyant call-by-value

Page 63: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Equivalence to Call-by-Need, continued…

‣ What this means is that the call-by-need cost is the minimal clairvoyant call-by-value cost

‣ As a corollary, we know that ∀C.C[e]⇓k ⇒ C[e']⇓≤k in call-by-need if and only if ∀C.C[e]⇓k ⇒ C[e']⇓≤k in clairvoyant call-by-value

‣ They have the same contextual improvement relation

Page 64: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Common Subexpression Elimination, Revisited

Page 65: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Common Subexpression Elimination, Revisited

‣ Now it is much easier to justify common subexpression elimination:

‣ M[N] ⟶ let x = N in M[x]

Page 66: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Common Subexpression Elimination, Revisited

‣ Now it is much easier to justify common subexpression elimination:

‣ M[N] ⟶ let x = N in M[x]

‣ If the context hole is not evaluated, we can just use Skip rule to get into the same position; should cost the same

Page 67: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Common Subexpression Elimination, Revisited

‣ Now it is much easier to justify common subexpression elimination:

‣ M[N] ⟶ let x = N in M[x]

‣ If the context hole is not evaluated, we can just use Skip rule to get into the same position; should cost the same

‣ Otherwise, the Let rule allows us to share work, replacing potentially multiple evaluations of N with only one; this can never degrade performance

Page 68: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

But Wait! There’s More!

Page 69: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

But Wait! There’s More!‣ We can use this new operational semantics for clairvoyant call-

by-value to give a denotational semantics for call-by-need that avoids heap nastiness…

Page 70: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

But Wait! There’s More!‣ We can use this new operational semantics for clairvoyant call-

by-value to give a denotational semantics for call-by-need that avoids heap nastiness…

‣ We interpret terms in the domain D = (ωop×(D → D))⊥

Page 71: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

But Wait! There’s More!‣ We can use this new operational semantics for clairvoyant call-

by-value to give a denotational semantics for call-by-need that avoids heap nastiness…

‣ We interpret terms in the domain D = (ωop×(D → D))⊥

‣ ωop captures costs

Page 72: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

But Wait! There’s More!‣ We can use this new operational semantics for clairvoyant call-

by-value to give a denotational semantics for call-by-need that avoids heap nastiness…

‣ We interpret terms in the domain D = (ωop×(D → D))⊥

‣ ωop captures costs

‣ —⊥ captures nontermination

Page 73: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

But Wait! There’s More!‣ We can use this new operational semantics for clairvoyant call-

by-value to give a denotational semantics for call-by-need that avoids heap nastiness…

‣ We interpret terms in the domain D = (ωop×(D → D))⊥

‣ ωop captures costs

‣ —⊥ captures nontermination

‣ We interpret let-binding as the join of discarding and eager evaluation

Page 74: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Soundness and Adequacy

Page 75: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Soundness and Adequacy

‣ We can relate our denotational and operational semantics with soundness and adequacy theorems

Page 76: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Soundness and Adequacy

‣ We can relate our denotational and operational semantics with soundness and adequacy theorems

‣ Trick here is to find the right statements:

Page 77: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Soundness and Adequacy

‣ We can relate our denotational and operational semantics with soundness and adequacy theorems

‣ Trick here is to find the right statements:

‣ Soundness: If Γ : e⇓k Δ : v then ⟦e⟧⟦Γ⟧ ≥ k ▹⟦v⟧⟦Δ⟧

Page 78: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Soundness and Adequacy

‣ We can relate our denotational and operational semantics with soundness and adequacy theorems

‣ Trick here is to find the right statements:

‣ Soundness: If Γ : e⇓k Δ : v then ⟦e⟧⟦Γ⟧ ≥ k ▹⟦v⟧⟦Δ⟧

‣ Adequacy: If ⟦e⟧⟦Γ⟧ = (k, f) then Γ : e⇓≤k

Page 79: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Compositionality and Abstraction

Page 80: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Compositionality and Abstraction

‣ We also can show that contexts act monotonically:

‣ ⟦e⟧ ≤ ⟦e'⟧ ⇒ ⟦C[e]⟧ ≤ ⟦C[e']⟧

Page 81: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Compositionality and Abstraction

‣ We also can show that contexts act monotonically:

‣ ⟦e⟧ ≤ ⟦e'⟧ ⇒ ⟦C[e]⟧ ≤ ⟦C[e']⟧

‣ As a corollary of all this, we get that our model is sound for contextual improvement:

‣ ⟦e⟧ ≤ ⟦e'⟧ ⇒ ∀C.C[e]⇓k ⇒ C[e']⇓≤k

Page 82: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Conclusion

Page 83: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Conclusion

‣ We don’t need updates to reason about sharing

Page 84: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Conclusion

‣ We don’t need updates to reason about sharing

‣ By constructing an update-free operational semantics, we get a cleaner theory that remains faithful to efficiency

Page 85: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Conclusion

‣ We don’t need updates to reason about sharing

‣ By constructing an update-free operational semantics, we get a cleaner theory that remains faithful to efficiency

‣ This also leads to a nicer denotational semantics

Page 86: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

So, that’s good for time…

Page 87: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

So, that’s good for time…

Father Christmas moonlights as old Father Time...

Page 88: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

…but what about space?

Page 89: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

…but what about space?

Enough room to fit?

Page 90: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

…but what about space?‣ Not clear how to capture

space usage in the same way; are heaps essential here?

Enough room to fit?

Page 91: How to Replace Sharing with Fortune-Telling!axj/files/mgs-xmas-2018-Jenny.pdf · Fortune-Telling! Jennifer Hackett University of Nottingham. Introduction. Introduction ‣ This is

Questions?