OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value...

40
Open Call-by-Value Beniamino Accattoli 1 Giulio Guerrieri 2 1 INRIA, LIX, École Polytechnique 2 I2M, Aix-Marseille Université & Dipartimento di Matematica e Fisica, Università Roma Tre DICE 2016 Eindhoven, April 2, 2016 B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 1 / 33

Transcript of OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value...

Page 1: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Open Call-by-Value

Beniamino Accattoli1 Giulio Guerrieri2

1INRIA, LIX, École Polytechnique

2I2M, Aix-Marseille Université & Dipartimento di Matematica e Fisica, Università Roma Tre

DICE 2016Eindhoven, April 2, 2016

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 1 / 33

Page 2: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Outline

1 Introduction: issues in the call-by-value λ-calculus

2 Equivalence of the proposals for Open CBV

3 Cost Models and Abstract Machines

4 On the minimality of the cost model

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 2 / 33

Page 3: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Outline

1 Introduction: issues in the call-by-value λ-calculus

2 Equivalence of the proposals for Open CBV

3 Cost Models and Abstract Machines

4 On the minimality of the cost model

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 3 / 33

Page 4: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Plotkin’s (strong) call-by-value λ-calculus (1975)Motivation: Call-by-value (CBV) λ-calculus (Plotkin, 1975) is a version ofλ-calculus closer to the real implementation of functional programming languages(e.g. Ocaml) and proof-assistants (e.g. Coq).

Syntax: same as ordinary (i.e. call-by-name) λ-calculus.

(values) U,V ::= x | λx M(terms) L,M,N ::= V | MN

Operational semantics: (V is value!)

(λxM)V 7→βv M{V /x}

→βv is the contextual closure of 7→βv where contexts (with exactly one hole) aredefined by (as usual)

C ::= L·M | λxC | CM | MC

Example: (λx y)(zI ) (where I = λx x) is a βv -normal form (but a β-redex).

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 4 / 33

Page 5: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

The perfect world: Closed CBV

The study of functional programming languages (e.g. Ocaml) is modeled by theframework of Plotkin’s CBV λ-calculus calles Closed CBV, where

evaluation is weak: it does not reduce under λ’s;terms are closed.

Harmony property for Closed CBVClosed normal forms are values (and values are normal forms).

The restriction to βv -reduction instead of β-reduction impacts on the order inwhich redexes are evaluated, but evaluation never gets stuck:

either every β-redex will eventually become a βv -redex and be firedor evaluation diverges.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 5 / 33

Page 6: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

The “real” (imperfect) world: Strong and Open CBVStrong CBV: full Plotkin’s CBV λ-calculus

reduction under λ’s is allowed and terms can be open

Open CBV: intermediate setting between Closed CBV and Strong CBVevaluation is weak: it does not reduce under λ’s;terms may be open.

Motivations to leave the perfect world:implementation model of Coq, see Grégoire & Leroy (2002)denotational semantics for CBV, see Paolini & Ronchi D. R. (1999, 2004)monad and CPS translations, see Moggi (1989), Sabry & Felleisen (1993)bisimulations and partial evaluation, see Lassen (2005), Jones et al. (1993)CBV and linear logic proof-nets, see Accattoli (2015)CBV and cost models, see Accattoli & Sacerdoti Coen (2015). . .

Here we study Open CBV.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 6 / 33

Page 7: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Plotkin’s (naïve) Open CBV (1975)

Naïve Open CBV: framework of Plotkin’s CBV where βv -reduction is weak andterms are possibly open.

terms t, u, s, r , ::= v | tuvalues v ,w ::= x | λx t

evaluation contexts C ::= L·M | Ct | tC

Root-step Contextual closure(λx t)λyu 7→βλt{λyu/x} CLtM→βλ CLuM iff t 7→βλ u

(λx t)y 7→βZt{y/x} CLtM→βZ

CLuM iff t 7→βZu

7→βv:= 7→βλ∪ 7→βZ

CLtM→βv CLuM iff t 7→βv u

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 7 / 33

Page 8: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Naïve Open CBV and its issues: stuck β-redexes

Issue: harmony does no longer hold (∃ open β-normal forms that are not values) there are stuck β-redexes, i.e. open β-redexes that never will be fired by theβv -reduction because their argument is normal but not a value. stuck β-redexes provide prematures βv -normal forms, e.g.

t := (λy δ)(zz)δ u := δ((λy δ)(zz)) where δ := λx xx (1)

while one would expect t and u to behave like the prototypical divergent term δδ:

for semantical reasons (empty semantics)for operational reasons (potential valuability)for logical reasons (translation into linear logic proof-nets)for syntactic reasons (hidden βv -redex)

This issue impacts on termination and notions of observational equivalence.

Many proposals to extend βv -reduction with some other rules or constructors(as explicit substitutions, i.e. let...in expressions): Moggi (1989), Sabry &Felleisen (1993), Sabry & Wadler (1997), Maraist et al. (1999), Curien & Herbelin(2000), Dychoff & Lengrand (2007), Herbelin & Zimmermann (2009), etc.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 8 / 33

Page 9: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Naïve Open CBV and its issues: stuck β-redexes

Issue: harmony does no longer hold (∃ open β-normal forms that are not values) there are stuck β-redexes, i.e. open β-redexes that never will be fired by theβv -reduction because their argument is normal but not a value. stuck β-redexes provide prematures βv -normal forms, e.g.

t := (λy δ)(zz)δ u := δ((λy δ)(zz)) where δ := λx xx (1)

while one would expect t and u to behave like the prototypical divergent term δδ:

for semantical reasons (empty semantics)for operational reasons (potential valuability)for logical reasons (translation into linear logic proof-nets)for syntactic reasons (hidden βv -redex)

This issue impacts on termination and notions of observational equivalence.

Many proposals to extend βv -reduction with some other rules or constructors(as explicit substitutions, i.e. let...in expressions): Moggi (1989), Sabry &Felleisen (1993), Sabry & Wadler (1997), Maraist et al. (1999), Curien & Herbelin(2000), Dychoff & Lengrand (2007), Herbelin & Zimmermann (2009), etc.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 8 / 33

Page 10: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Our contributions

1 Equivalence of the proposals: we show that the proposed generalizations ofNaïve Open CBV are equivalent, i.e. they have exactly the same sets ofnormalizing and diverging λ-terms there is just one notion of Open CBV,independently of its specific syntactic incarnation.We also compare the equational theories of the different proposals, andindicate the finest one for Open CBV.

2 Cost models and an abstract machine: the termination results arecomplemented with quantitative analyses about the number of steps.Moreover we provide insights into the size-explosion problem forClosed/Open/Strong CBV, that lead to a new abstract machine for OpenCBV, simpler than others in the literature.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 9 / 33

Page 11: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Our contributions

1 Equivalence of the proposals: we show that the proposed generalizations ofNaïve Open CBV are equivalent, i.e. they have exactly the same sets ofnormalizing and diverging λ-terms there is just one notion of Open CBV,independently of its specific syntactic incarnation.We also compare the equational theories of the different proposals, andindicate the finest one for Open CBV.

2 Cost models and an abstract machine: the termination results arecomplemented with quantitative analyses about the number of steps.Moreover we provide insights into the size-explosion problem forClosed/Open/Strong CBV, that lead to a new abstract machine for OpenCBV, simpler than others in the literature.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 9 / 33

Page 12: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Outline

1 Introduction: issues in the call-by-value λ-calculus

2 Equivalence of the proposals for Open CBV

3 Cost Models and Abstract Machines

4 On the minimality of the cost model

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 10 / 33

Page 13: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

The incarnations for Open CBV that we have studiedWe focus on three proposals for Open CBV (other solutions, e.g. Moggi’s orHerbelin & Zimmerman’s, are already known to be equivalent to these ones).

1 The Fireball Calculus λfire, that extends values to fireballs by addingso-called inert terms in order to restore harmony. It was introduced by Paolini& Ronchi Della Rocca (1999, 2004), then rediscovered independently first byLeroy & Grégoire (2002) to improve the implementation of Coq, and then byAccattoli & Sacerdoti Coen (2015) to study cost models;

2 The Value Substitution Calculus λvsub, coming from the linear logicinterpretation of CBV and using explicit substitutions (ES) and contextualrewriting rules to circumvent stuck β-redexes. It was introduced by Accattoli& Paolini (2012) and it is isomorphic to proof-nets for CBV λ-calculus;

3 The Shuffling Calculus λsh, that has rules to shuffle constructors, similar toRegnier’s σ-rules for CBN (1992, 1994), as an alternative to explicitsubstitutions. It was introduced by Carraro & Guerrieri (2014) to study theadequacy of Open/Strong CBV with respect to denotational semanticscoming from linear logic.

Remark: for λfire, λvsub and λsh, normalization and strong normalization coincide.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 11 / 33

Page 14: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

The incarnations for Open CBV that we have studiedWe focus on three proposals for Open CBV (other solutions, e.g. Moggi’s orHerbelin & Zimmerman’s, are already known to be equivalent to these ones).

1 The Fireball Calculus λfire, that extends values to fireballs by addingso-called inert terms in order to restore harmony. It was introduced by Paolini& Ronchi Della Rocca (1999, 2004), then rediscovered independently first byLeroy & Grégoire (2002) to improve the implementation of Coq, and then byAccattoli & Sacerdoti Coen (2015) to study cost models;

2 The Value Substitution Calculus λvsub, coming from the linear logicinterpretation of CBV and using explicit substitutions (ES) and contextualrewriting rules to circumvent stuck β-redexes. It was introduced by Accattoli& Paolini (2012) and it is isomorphic to proof-nets for CBV λ-calculus;

3 The Shuffling Calculus λsh, that has rules to shuffle constructors, similar toRegnier’s σ-rules for CBN (1992, 1994), as an alternative to explicitsubstitutions. It was introduced by Carraro & Guerrieri (2014) to study theadequacy of Open/Strong CBV with respect to denotational semanticscoming from linear logic.

Remark: for λfire, λvsub and λsh, normalization and strong normalization coincide.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 11 / 33

Page 15: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

The incarnations for Open CBV that we have studiedWe focus on three proposals for Open CBV (other solutions, e.g. Moggi’s orHerbelin & Zimmerman’s, are already known to be equivalent to these ones).

1 The Fireball Calculus λfire, that extends values to fireballs by addingso-called inert terms in order to restore harmony. It was introduced by Paolini& Ronchi Della Rocca (1999, 2004), then rediscovered independently first byLeroy & Grégoire (2002) to improve the implementation of Coq, and then byAccattoli & Sacerdoti Coen (2015) to study cost models;

2 The Value Substitution Calculus λvsub, coming from the linear logicinterpretation of CBV and using explicit substitutions (ES) and contextualrewriting rules to circumvent stuck β-redexes. It was introduced by Accattoli& Paolini (2012) and it is isomorphic to proof-nets for CBV λ-calculus;

3 The Shuffling Calculus λsh, that has rules to shuffle constructors, similar toRegnier’s σ-rules for CBN (1992, 1994), as an alternative to explicitsubstitutions. It was introduced by Carraro & Guerrieri (2014) to study theadequacy of Open/Strong CBV with respect to denotational semanticscoming from linear logic.

Remark: for λfire, λvsub and λsh, normalization and strong normalization coincide.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 11 / 33

Page 16: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

The incarnations for Open CBV that we have studiedWe focus on three proposals for Open CBV (other solutions, e.g. Moggi’s orHerbelin & Zimmerman’s, are already known to be equivalent to these ones).

1 The Fireball Calculus λfire, that extends values to fireballs by addingso-called inert terms in order to restore harmony. It was introduced by Paolini& Ronchi Della Rocca (1999, 2004), then rediscovered independently first byLeroy & Grégoire (2002) to improve the implementation of Coq, and then byAccattoli & Sacerdoti Coen (2015) to study cost models;

2 The Value Substitution Calculus λvsub, coming from the linear logicinterpretation of CBV and using explicit substitutions (ES) and contextualrewriting rules to circumvent stuck β-redexes. It was introduced by Accattoli& Paolini (2012) and it is isomorphic to proof-nets for CBV λ-calculus;

3 The Shuffling Calculus λsh, that has rules to shuffle constructors, similar toRegnier’s σ-rules for CBN (1992, 1994), as an alternative to explicitsubstitutions. It was introduced by Carraro & Guerrieri (2014) to study theadequacy of Open/Strong CBV with respect to denotational semanticscoming from linear logic.

Remark: for λfire, λvsub and λsh, normalization and strong normalization coincide.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 11 / 33

Page 17: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Open CBV 1: Fireball Calculus λfireIdea: it extends the notion of value (actually, of variable).

terms (as in Plotkin) t, u, s, r , ::= v | tuvalues (as in Plotkin) v ,w ::= x | λx t

fireballs f ::= i | λx tinert terms i ::= x f1 . . . fn n ≥ 0

evaluation contexts (as in Plotkin) C ::= L·M | Ct | tC

Root-step Contextual closure(λx t)λyu 7→βλt{λyu/x} CLtM→βλ CLuM iff t 7→βλ u

(λx t)i 7→βi t{i/x} CLtM→βi CLuM iff t 7→βi u

7→βf:= 7→βλ∪ 7→βi CLtM→βf

CLuM iff t 7→βfu

Example:

t := (λy δ)(zz)δ →βi δδ →βλ δδ →βλ . . .

u := δ((λy δ)(zz))→βi δδ →βλ δδ →βλ . . .

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 12 / 33

Page 18: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Properties of the Fireball Calculus λfire

Using standard rewriting techniques, we have proved:

Proposition (Open harmony)t is βf -normal iff t is a fireball.

Proposition1 →βi is strongly normalizing and strongly confluent.2 →βi and →βλ commute.3 →βf

is strongly confluent and all normalizing βf -reduction sequences d (ifany) from t have the same length |d |βf

, the same number |d |βi of βi -stepsand the same number |d |βλ of βλ-steps.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 13 / 33

Page 19: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Open CBV 2: Value Substitution Calculus λvsubIdea: It extends the syntax of terms with ES, adding rules acting at a distance.

terms t, u, s, r , ::= v | tu | t[u/x ]values v ,w ::= x | λx t

evaluation contexts C ::= L·M | Ct | tC | C[t/x ] | t[C/x ]substitution contexts S ::= L·M[t1/x1] . . . [tn/xn] n ≥ 0

Root-step Contextual closureSLλx tM u 7→m SLt[u/x ]M CLtM→m CLuM iff t 7→m u

t[SLλyuM/x ] 7→eλSLt{λyu/x}M CLtM→eλ CLuM iff t 7→eλ u

t[SLyM/x ] 7→eZ SLt{y/x}M CLtM→βi CLuM iff t 7→eZ u

7→e := 7→eλ∪ 7→eZ CLtM→e CLuM iff t 7→e u

7→vsub := 7→e∪ 7→m CLtM→vsub CLuM iff t 7→vsub u

Example:

t := (λy δ)(zz)δ →m δ[zz/y ]δ →m (xx)[δ/x ][zz/y ]→eλ (δδ)[zz/y ]→m . . .

u := δ((λy δ)(zz))→m δ(δ[zz/y ])→m (xx)[δ[zz/y ]/x ]→eλ (δδ)[zz/y ]→m . . .

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 14 / 33

Page 20: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Properties of the Value Substitution Calculus λvsub

Using standard rewriting techniques, we have proved:

Proposition1 →m and →e are strongly normalizing.2 →m and →e are strongly confluent.3 →m and →e commute.4 →vsub is strongly confluent and all normalizing vsub-reduction sequences d

(if any) from t have the same length |d |vsub, the same number |d |m ofm-steps and the same number |d |e of e-steps.

5 Any vsub-reduction sequence d from t is such that |d |e ≤ |d |m.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 15 / 33

Page 21: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Open CBV 3: Shuffling Calculus λshIdea: It adds new CBV reduction rules.

terms (as in Plotkin) t, u, s, r , ::= v | tuvalues (as in Plotkin) v ,w ::= x | λx tevaluation contexts C ::= L·M | Ct | tC | (λx C)t

Root-step Contextual closure(λx t)v 7→βv t{v/x} CLtM→β[v

CLuM iff t 7→βv u

(λx t)sr 7→σ1(λx tr)s x /∈ fv(r) CLtM→σ[1CLuM iff t 7→σ1 u

v((λx r)s) 7→σ3(λx vr)s x /∈ fv(v) CLtM→σ[3CLuM iff t 7→σ3 u

7→σ := 7→σ1∪ 7→σ3 CLtM→σ[ CLuM iff t 7→σ u

7→sh := 7→βv∪ 7→σ CLtM→sh CLuM iff t 7→sh u

Example:

t := (λy δ)(zz)δ →σ1 (λy δδ)(zz)→βv (λy δδ)(zz)→βv . . .

u := δ((λy δ)(zz))→σ3 (λy δδ)(zz)→βv (λy δδ)(zz)→βv . . .

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 16 / 33

Page 22: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Properties of the Shuffling Calculus λsh

Using standard rewriting techniques, we have proved:

Proposition1 →σ[ is strongly normalizing and (not strongly) confluent.2 →sh is (not strongly) confluent.

In contrast to λfire and λvsub, λsh is not strongly confluent and not all normalizingsh-reduction sequences (if any) from a given term have the same length.Example: Consider all normalizing sh-reduction sequences from (λy z)(δ(zz))

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 17 / 33

Page 23: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Fireball Calculus vs. Value Substitution CalculusThe unfolding of a term t with ES is the term t ↓ obtained from t by turning ESinto meta-level substitutions; it is defined by:

x ↓ := x (tu)↓ := t ↓ u ↓ (λxt)↓ := λx t ↓ (t[u/x ])↓ := t ↓{u ↓/x}

Theorem (Quantitative simulation of λfire into λvsub)For any βf -reduction sequence d : t →∗βf

u there are a term r with ES and avsub-reduction sequence e : t →∗vsub r such that:

1 (qualitative relationship) r ↓= u;2 (quantitative relationship) |d |βf

= |e|m and |d |βλ = |e|eλ = |e|e;3 (normal forms) if u is βf -normal then there are a vsub-normal term s with ES

and a vsub-reduction sequence e′ : r →eZ s such that |e′|eZ ≤ |e|m − |e|eλ .

Corollary (Linear termination equivalence of λvsub and λfire)There exists a normalizing βf -reduction sequence d from a term t iff there exists anormalizing vsub-reduction sequence e from t. Moreover,|d |βf

≤ |e|vsub ≤ 2|d |βf, i.e. they are related linearly.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 18 / 33

Page 24: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Shuffling Calculus vs. Value Substitution Calculus

Theorem (Quantitative simulation of λsh into λvsub)For any sh-reduction sequence d : t →∗sh u there are a term s with ES and avsub-reduction sequence e : t →∗vsub s such that:

1 (qualitative relationship) s ≡ m(u), the m-normal form of u;2 (quantitative relationship) |d |β[v = |e|e;3 (normal forms) if u is sh-normal then s and m(u) are vsub-normal.

Corollary (Termination equivalence of λvsub and λsh)There exists a normalizing sh-reduction sequence d from a term t iff there exists anormalizing vsub-reduction sequence e from t. Moreover, |d |β[v = |e|e.

Corollary (Number of β[v -steps in invariant in λsh)

All normalizing sh-reduction sequences (if any) from term t have the samenumber of β[v -steps.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 19 / 33

Page 25: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

What about equational theories?

Given a calculus with a reduction →r, its equational theory 'r is thereflexive-transitive and symmetric closure of →r.

TheoremThe Fireball Calculus and the Shuffling Calculus have the same equational theory,modulo some commutation rules: t 'ext

sh s iff t '≡vsub s.

On the contrary, the Fireball Calculus equates too much! Let:

I := λx x t := (λy I )(xx) s := (λy I )(xz)

then t 'βfs since t →βf

I βf← s.

But consider the context C := (λz(λxL M)δ)I : then, CLtM 6'βfCLsM since

CLtM→∗βf(λy I )(δδ)→∗βf

(λy I )(δδ)→∗βf. . . CLuM→∗βf

I

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 20 / 33

Page 26: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

What about equational theories?

Given a calculus with a reduction →r, its equational theory 'r is thereflexive-transitive and symmetric closure of →r.

TheoremThe Fireball Calculus and the Shuffling Calculus have the same equational theory,modulo some commutation rules: t 'ext

sh s iff t '≡vsub s.

On the contrary, the Fireball Calculus equates too much! Let:

I := λx x t := (λy I )(xx) s := (λy I )(xz)

then t 'βfs since t →βf

I βf← s.

But consider the context C := (λz(λxL M)δ)I : then, CLtM 6'βfCLsM since

CLtM→∗βf(λy I )(δδ)→∗βf

(λy I )(δδ)→∗βf. . . CLuM→∗βf

I

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 20 / 33

Page 27: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Outline

1 Introduction: issues in the call-by-value λ-calculus

2 Equivalence of the proposals for Open CBV

3 Cost Models and Abstract Machines

4 On the minimality of the cost model

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 21 / 33

Page 28: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

On Cost Models and All That

Aim: time complexity analyses of the λ-calculus. Is there a way to measure thecomputational complexity of a λ-term?

Natural Cost Model: the number of β-steps to reach the normal form.

Problem: is it a reasonable cost model?

Weak invariance thesis (Slot and van Emde Boas, 1984)

Reasonable (or invariant) computational models can simulate each other within apolynomial overhead in time, e.g. Random Access Machines and Turing Machines.

The difficult part (because of size-explosion):to show an implementation of λ-calculus on RAM with a polynomial overhead(wrt the # of β-steps and the size of the initial term).

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 22 / 33

Page 29: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

The Size-Explosion problem

At first sight, the number of β-steps does not seem to be a reasonable cost model.

Size-Exploding Families:

a family of terms (tn)n∈N of linear sizeevaluating in a linear number of steps

to a result (i.e. normal form) of exponential size.

The size of the result is exponential in the cost model: is it reasonable?

Problem independent of the evaluation strategy(Closed/Open/Strong λ-calculus in CBN/CBV/CBNeed, as well as optimal).

Solution:Switch to compact representations of results (sharing) and micro-step evaluation.

Different evaluation strategies Different families and different solutions.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 23 / 33

Page 30: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Size-Explosion and Evaluation Strategies (e.g. for λfire)

Example affecting all evaluation strategies (Closed/Open/Strong):

t1 := λx λy yxx r0 := λx x

tn+1 := λx tn(λy yxx) rn+1 := λy yrnrn

Abstraction Size-Explosion (note →βλ):tnr0 →n

βλrn where |tn| = O(n), |rn| = O(2n) and rn is βf -normal, ∀n > 0.

Example affecting Open and Strong strategies (not the perfect Closed world):

u1 := λx1 x1x1 un+1 := λxn+1 un(xn+1xn+1)

Inert size-explosion for λfire (note →βi ):un(xx)→n

βi(xx)2

n

where |un| = O(n) and |(xx)2n | = O(2n) is βf -normal, ∀n > 0.

Finally, Strong strategies have additional issues with abstraction size-explosion.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 24 / 33

Page 31: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

The LiteratureTheorem for Closed CBN & CBV λ-calculusThe # of steps is a reasonable cost model.Overhead: linear in the number of steps.

(Blelloch & Greiner ’95, Sands, Gustavsson & Moran ’02, Dal Lago & Martini ’08).

Theorem (Accattoli & Dal Lago, 2014) for Strong CBN λ-calculus

The # number of leftmost-outermost steps is a reasonable cost model.Overhead: quadratic in the number of steps, linear in the size of the initial term.

Theorem (Accattoli & Sacerdoti Coen, 2015) for the Fireball Calculus

The # of steps in λfire is a reasonable cost model.Overhead: linear in the number of steps, linear in the size of the initial term.

Corollary (Accattoli & G., 2016) for the Value Substitution Calculus

The # of steps in λvsub is a reasonable cost model.Overhead: bilinear, as for λfire.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 25 / 33

Page 32: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

GLAMOUR Machine

Accattoli & Sacerdoti Coen’s theorem (2015) rely on an abstract machine:

λfirebilinear overhead

Glamour (CBV abstract machine)bilinear overhead

RAM

Features of the Glamour machine:

1 Never substitutes inert terms,variables included, e.g. x [xx/x ] and x [z/x ] are normal;

2 Substitutes abstractions on-demand,i.e. abstractions are substituted only when they create a β-redex.

The Glamour machine is complex, with labeled environments.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 26 / 33

Page 33: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Easy GLAMOURFeatures of the Glamour machine:

1 Never substitutes inert terms,variables included, e.g. x [xx/x ] and x [z/x ] are normal;

2 Substitutes abstractions on-demand,i.e. abstractions are substituted only when they create a new β-redex.

We introduce Easy Glamour: CBV abstract machine implementing only feature 1.(thus it substitutes abstractions freely, i.e. always)

It does not need labels, it is reasonable, simpler, and slightly slower.

Theorem (Accattoli & G., 2016) for Fireball Calculus

The Easy Glamour is a reasonable implementation of the Fireball calculus.Overhead: linear in the number of steps, quadratic in the size of the initial term.

λfirelinear/quadratic overhead

Easy Glamourbilinear overhead

RAM

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 27 / 33

Page 34: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Easy GLAMOURFeatures of the Glamour machine:

1 Never substitutes inert terms,variables included, e.g. x [xx/x ] and x [z/x ] are normal;

2 Substitutes abstractions on-demand,i.e. abstractions are substituted only when they create a new β-redex.

We introduce Easy Glamour: CBV abstract machine implementing only feature 1.(thus it substitutes abstractions freely, i.e. always)

It does not need labels, it is reasonable, simpler, and slightly slower.

Theorem (Accattoli & G., 2016) for Fireball Calculus

The Easy Glamour is a reasonable implementation of the Fireball calculus.Overhead: linear in the number of steps, quadratic in the size of the initial term.

λfirelinear/quadratic overhead

Easy Glamourbilinear overhead

RAM

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 27 / 33

Page 35: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Modularity of Glamour abstract machinesMoral: To have a reasonable implementation of the Fireball calculus, it is enoughto never substitute compound inert terms.

We show two modular degrees of freedom for reasonable implementations:1 To Substitute Variables or Not:

the overhead is quadratic or linear in the number of steps;

2 To Substitute Abstractions Freely or On-Demand:the overhead is quadratic or linear in the size of the initial term;.

Let t →kβf

u be an evaluation sequence in the Fireball Calculus.Glamour machines have the following overhead:

never substituting substitutingvariables variables

substituting abstractions on-demand O(k · |t|) O(k2 · |t|)substituting abstractions freely O(k · |t|2) O(k2 · |t|2)

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 28 / 33

Page 36: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Open vs Strong CBVStrong strategies have additional issues with abstraction size-explosion.

Qualitatively: Iterated Open CBV = Strong CBV.

Insight:Easy Glamour is reasonable for Open CBV,Iterated Easy Glamour is not reasonable for Strong CBV.

Quantitatively:

Iterated reasonable implementation for Open CBV6=

reasonable implemementation for Strong CBV.

Moral: substituting abstractions on-demand is necessary for Strong settings.

Consequence:Reasonable implem. of Strong CBV have only one degree of freedom (variables).

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 29 / 33

Page 37: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Outline

1 Introduction: issues in the call-by-value λ-calculus

2 Equivalence of the proposals for Open CBV

3 Cost Models and Abstract Machines

4 On the minimality of the cost model

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 30 / 33

Page 38: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

The Cost of Inert Steps

The number of fireball steps is an invariant cost model.

Roughly, a fireball step costs 1.

Fireball steps = abstraction steps + inert steps.

Thus, a inert step costs 1 wrt the current reasonable cost model.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 31 / 33

Page 39: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Are Inert Steps Relevant?

(Easy) Glamour: inert steps implemented in constant time, without substitution.

Suspicion: maybe they are dominated by the number of abstraction steps.

Natural Question: can inert steps be seen as administrative work?

Roughly: do inert steps cost 1 or rather cost 0?

Answer:inert step seem to cost 1, i.e. they are relevant for complexity analyses.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 32 / 33

Page 40: OpenCall-by-Valuelipn.univ-paris13.fr/DICE2016/Talks/Guerrieri.pdf · OpenCall-by-Value BeniaminoAccattoli1 GiulioGuerrieri2 1INRIA, LIX, École Polytechnique 2I2M, Aix-Marseille

Inert Steps Seem to Be Relevant

We show a family of terms sn such that:

when applied to a inert term i , for instance i := yy

Linear Abstraction Prefix: sni evaluates in a linear number of βλ-steps,

Exponential Inert Coda: followed by an exponential number of βi -steps.

It seems the number of βλ-steps is not an invariant cost model for Open CBV.

Our result: sni →nβv

un →2n−1βi

rn.

Where

t0 := x u0 := t0i r0 := u0 s0 := x

tn+1 := λz yunun un+1 := tn+1i rn+1 := yrnrn sn := (λxλz y(xi)(xi))sn

Note that rn is a inert term, hence βf -normal, whereas un is βv -normal.

B. Accattoli, G.Guerrieri Open CBV DICE 2/4/2016 33 / 33