On the Relative Usefulness of Fireballs Beniamino Accattoli

Claudio Sacerdoti Coen

INRIA, PARSIFAL team Universit` a di Bologna

Fireballs are a generalisation of values (in the sense of the call-by-value λ-calculus)

Outline Background Weak Call-by-Value λ-calculus Weak Call-by-Value Invariance, Revisited Fireballs Fireball Explosion How to Stop Worrying and Love the Bomb

The Invariance Thesis

Slot and van Emde Boas’ (Weak) Invariance Thesis:

Reasonable machines can simulate each other within a polynomially bounded overhead in time.

Long-standing problem: is λ-calculus a reasonable machine?

Bird’s Eye View

λ-Calculus

?

Turing Machines

Bird’s Eye View

Linear [A. & Dal Lago, RTA ’12]

λ-Calculus

Turing Machines

Bird’s Eye View

Linear [A. & Dal Lago, RTA ’12]

Turing Machines

λ-Calculus

?

Bird’s Eye View

Linear [A. & Dal Lago, RTA ’12]

Turing Machines

λ-Calculus Size Explosion Problem

Outline Background Weak Call-by-Value λ-calculus Weak Call-by-Value Invariance, Revisited Fireballs Fireball Explosion How to Stop Worrying and Love the Bomb

The Literature 1 Weak Call-by-Value λ-calculus is invariant. Blelloch & Greiner ’96; Sands & Gustavsson & Moran ’02; Dal Lago & Martini ’06.

Proof technique: simulation in a system with sharing. (Formalisms: abstract machines or graph reduction). Sands & al.: abstract machine with bilinear overhead. Bilinear = linear in 1. the number of β-steps, and 2. the size of the initial term (i.e. the input).

Schema of the Solution

Weak Call-by-Value λ-Calculus

Linear

RAM

Abstract Machine

Bilinear

The Literature 2

Strong Call-by-name λ-calculus is invariant. Accattoli & Dal Lago, CSL-LICS ’14.

Harder case, simple sharing is not enough. Proof technique: refined simulation with useful sharing Formalism: Linear substitution calculus aka explicit substitutions. Overhead: at least quadratic.

Schema of the Solutions

Strong Call-by-Name λ-Calculus Quadratic

RAM

Linear Substitution Calculus + Useful Sharing

Polynomial (linear?)

This Work Semi-strong Call-by-value λ-calculus is invariant. Semi-strong CBV = Weak CBV with open terms. Useful sharing is required. Proof technique: LSC + useful sharing + abstract machine. Main points: 1. Bilinear implementation λ → RAM; 2. Original combination of different techniques; (Practical values, distilleries, useful sharing);

3. Constant time implementation of useful sharing.

Outline Background Weak Call-by-Value λ-calculus Weak Call-by-Value Invariance, Revisited Fireballs Fireball Explosion How to Stop Worrying and Love the Bomb

Weak Call-by-Value Invariance, Revisited

Sands & al.’s result has been revisited last year. Decomposed in two works: 1. Distilling Abstract Machines. (Accattoli, Barenbaum, Mazza, ICFP 2014)

2. On the Value of Variables. (Accattoli & Sacerdoti Coen, WoLLIC 2014)

Distilling Abstract Machines Abstract machines = LSC + search of the redex.

Theorem The search of the redex is bilinear in the # of LSC steps. (for most CBN, CBV, and CBNeed machines in the literature)

Abstract machines = LSC + bilinear overhead. Two outcomes: 1. reasoning in the simpler LSC preserves complexity; 2. Principle for abstract machines.

Sands & al. Solution

Weak Call-by-Value λ-Calculus

Linear

RAM

Abstract Machine

Bilinear

Distilling Abstract Machines, ICFP 2014

Weak Call-by-Value λ-Calculus

RAM Bilinear

Weak Call-by-Value LSC

Bilinear

Abstract Machine

On the Value of Variables

Call-by-value schizophrenic literature: 1. Theoretical values = abstractions and variables. 2. Practical values = abstractions. The overhead in Weak CBV λ-calculus → Weak CBV LSC is: 1. Quadratic with theoretical values. 2. Linear with practical values.

Practical Values + Distillation

Weak Call-by-Value λ-Calculus

Bilinear

Bilinear

Linear

Weak Call-by-Value LSC

RAM

Bilinear

Abstract Machine

GLAM Leroy’s ZINC implements right-to-left CBV. GLAM = Global Leroy Abstract Machine. Global = just one global environments. Values are practical, i.e. are abstractions. Dump

Code

Stack

Env

Dump

Code

Stack

Env

D

ts

π

E

D : (t, π)

s



E

D : (t, π)

λx.s



E

D

t

λx.s : π

E

D

λx.t

v :π

E

β

D

t

π

[x v ]E

D

x

π

E

sub

D

E (x)α

π

E

Outline Background Weak Call-by-Value λ-calculus Weak Call-by-Value Invariance, Revisited Fireballs Fireball Explosion How to Stop Worrying and Love the Bomb

Extending CBV

Weak CBV key property: closed normal forms are values Adding free variables breaks the property. There are open normal forms that are not values, as x x. Problem: they block evaluation, as in (λy .δ) (x x) δ. Solution: generalizing values to fire-able terms, called fireballs.

The Fireball Calculus Language, extended with fireballs f : t, s, u v f

a | λx.t v |

:= := :=

x

|

|

λx.t

af . . . f | 1 {z n}

ts

n≥0

inert terms A

Right-to-left evaluation contexts: F

:=

h · i | Ff

| tF

Small-step Evaluation: Rule at Top Level (λx.t)f 7→f t{x f }

Contextual closure F hti →f F hsi iff t 7→f s

Key Property: →f normal forms are fireballs.

Open GLAM Open GLAM = kernel of Gr´egoire and Leroy’s machine (ICFP 2002). Open GLAM = GLAM + additional transition for free variables Dump

Code

Stack

Env

Dump

Code

Stack

Env

D

ts

π

E

D : (t, π)

s



E

D : (t, π)

λx.s



E

D

t

λx.s : π

E

D : (t, π)

a

π0

E

D

t

(a, π 0 ) : π

E

D

λx.t

f :π

E

β

D

t

π

[x f ]E

D

x

π

E

sub

D

E (x)α

π

E

Outline Background Weak Call-by-Value λ-calculus Weak Call-by-Value Invariance, Revisited Fireballs Fireball Explosion How to Stop Worrying and Love the Bomb

Size Explosion

Open terms bring a malicious behavior, called size explosion. Inert terms can grow exponentially with the number of β-steps. ⇒ A single substitution step can take exponential time. Key point: Substituting inert terms cannot create β-redexes.

Size Explosion

Fireball Calculus

Exponential!

Exponential!

Linear

Fireball LSC

RAM

Bilinear

Open GLAM

Outline Background Weak Call-by-Value λ-calculus Weak Call-by-Value Invariance, Revisited Fireballs Fireball Explosion How to Stop Worrying and Love the Bomb

Useful Sharing Remedy against size explosion: useful sharing. Complex definition (omitted). Surprisingly simple implementation. Intuitions (= the relative usefulness of fireballs): 1. Do a substitution step only when it creates a β-redex; 2. Therefore, never substitute inert terms, and 3. Substitute values only when the stack is non-empty. Complication: values have to include variables, i.e. are theoretical.

GLAMOUr The Useful Open GLAM, or GLAMOUr. Every term t in a π or E is labeled with l ∈ {λ, A}. Invariants: t λ unfolds (in E ) to an abstraction. t A unfolds (in E ) to a inert term. Dump

Code

Stack

Env

Dump

Code

Stack

Env

D

ts

π

E

D : (t, π)

s



E

D : (t, π) D : (t, π)

λx.s



a

π

E

0

D

E

l

E

t

λ



E

0 A



E

(λx.s)

D

t

(a, π )

β

D

t

π

usub

D



tl : π

E1 [x

s λ ]E2

[x

t l ]E

D

λx.t

t :π

D

x

tl : π

E1 [x

s λ ]E2

D : (t, π)

x



E1 [x

s λ ]E2

D

t

xλ : π

E1 [x

s λ ]E2

D : (t, π)

x

π0

E1 [x

s A ]E2

D

t

(x, π 0 )A : π

E1 [x

s A ]E2

Usefulness is tested in constant time, as it is coded by labels.

Semi-Strong CBV is Invariant

Fireball Calculus

Quadratic

Quadratic

Useful Fireball LSC

RAM Bilinear

Bilinear

GLAMOUr

Unchaining GLAMOUr

The quadratic overhead is due to theoretical values. Theoretical values are reintroduced by usefulness. A further unchaining optimization brings back practical values. The overhead becomes linear.

Final Result: Linear Overhead

Fireball Calculus

Bilinear

Linear

Unchaining Useful Fireball LSC

RAM Bilinear

Bilinear

Unchaining

GLAMOUr

Conclusions

Useful Sharing: Theoretically born concept, surprisingly relevant in practice. The GLAMOUr can be expon. faster than the Open GLAM. Strong improvement over Gr´ egoire and Leroy’s machine (their machine is used in the implementation of Coq). Abstract and sharp decomposition of call-by-value. Fireballs open the way to a new theory of call-by-value. (CBV Bohm theorem?).

THANKS!

On the Relative Usefulness of Fireballs

Inert terms can grow exponentially with the number of β-steps. ⇒ A single substitution step can take exponential time. Key point: Substituting inert terms cannot ...

225KB Sizes 1 Downloads 274 Views

Recommend Documents

No documents