Open Call-by-Value Beniamino Accattoli1 1 2
Giulio Guerrieri2
´ INRIA, LIX, Ecole Polytechnique
I2M, Aix-Marseille Universit´ e & Dipartimento di Matematica e Fisica, Universit` a Roma Tre
APLAS 2016
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
1 / 39
Call-by-Value
Plotkin’s Call-by-Value (CbV) λ-Calculus models: Programming Languages such as Ocaml or Standard ML; Proof assitants such as Coq. The two settings rely on different hypotheses.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
2 / 39
Flavors of Call-by-Value
Programming Languages rely on Closed CbV, that is: Weak Evaluation, i.e. function bodies are not evaluated; Closed Terms. Proof assitants rely on Strong CbV, that is: Strong Evaluation, i.e. function bodies are evaluated; Open Terms.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
3 / 39
Closed vs Strong CbV
Closed CbV is canonical and much simpler than Strong CbV. 2 Problems: Strong CbV 6= Closed CbV iterated into function bodies; No canonical presentation of Strong CbV.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
4 / 39
Open CbV
Open CbV is halfway Closed and Strong CbV: Weak Evaluation (as in Closed CbV); Open Terms (as in Strong CbV). Open terms ⇒ Open CbV can be iterated into function bodies. Then, Strong CbV = Iterated Open CbV. No canonical presentation of Open CbV, but many equivalent ones.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
5 / 39
Open CbV—Results We compare 4 presentations of Open CbV arising in different contexts: Abstract Machines; Linear Logic Proof Nets; Denotational Semantics; Sequent Calculus. We prove them equivalent with respect to termination. We establish precise quantitative relationships, relating the cost models. Summing Up: many presentations defining the same framework, Open CbV.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
6 / 39
Outline 1
Closed CbV
2
Open CbV 0: Naive Open CbV
3
Open CbV 1: The Fireball Calculus
4
Open CbV 2: The Value Substitution Calculus
5
Open CbV 3: The Shuffling Calculus
6
Open CbV 4: The Value Sequent Calculus
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
7 / 39
Plotkin’s Closed CbV Language: Terms Values
t, u, s v
:= :=
v x
| |
tu λx.t
Weak evaluation contexts: E
:=
h · i | Et
| tE
Small-step Evaluation: Rule at Top Level (λx.t)v 7→βv t{x v }
B. Accattoli, G.Guerrieri
Contextual closure E hti →βv E hui iff t 7→βv u
Open CBV
APLAS 2016
8 / 39
Harmony
Closed CbV harmony property: A closed term t is a →βv -normal forms iff t is a value. Harmony = Internal completeness wrt unconstrained β-reduction. Closed CbV never gets stuck: Every β-redex eventually becomes a βv -redex, unless evaluation diverges.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
9 / 39
Time Cost Model
The study of cost models for λ-calculi is a delicate subject. In Closed CbV the number of βv -steps is a reasonable time cost model. Reasonable = polynomially related to Turing machines. Roughly, one can count 1 for every βv -step. Result first proved by Blelloch and Greiner (1996).
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
10 / 39
Outline 1
Closed CbV
2
Open CbV 0: Naive Open CbV
3
Open CbV 1: The Fireball Calculus
4
Open CbV 2: The Value Substitution Calculus
5
Open CbV 3: The Shuffling Calculus
6
Open CbV 4: The Value Sequent Calculus
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
11 / 39
Adding Open Terms Naively
Open terms break harmony. They introduce stuck β-redexes. Consider t := (λx.u)(yz). t is stuck, it will never reduce to a value nor diverge.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
12 / 39
Adding Open Terms Naively
Stuck β-redexes provide prematures βv -normal forms, e.g. t := (λy .δ)(zz)δ
u := δ((λy .δ)(zz))
where δ := λx.xx
t and u should behave as δδ, i.e. they should diverge: for semantical reasons (empty denotational semantics) for logical reasons (evaluation into linear logic proof-nets) ⇒ Open terms impact on termination and observational equivalences.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
13 / 39
Improving CbV From the start it was clear that something is wrong with CbV. Already Plotkin noted that CPS is sound but not complete for CbV. Many proposals for a better CbV in the literature. Moggi (1989), Sabry & Felleisen (1993), Sabry & Wadler (1997), Maraist et al. (1999), Curien & Herbelin (2000), Dychoff & Lengrand (2007), Herbelin & Zimmermann (2009), ...
They all focus on Strong CbV. We consider 4 representative cases, but on Open CbV.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
14 / 39
Outline 1
Closed CbV
2
Open CbV 0: Naive Open CbV
3
Open CbV 1: The Fireball Calculus
4
Open CbV 2: The Value Substitution Calculus
5
Open CbV 3: The Shuffling Calculus
6
Open CbV 4: The Value Sequent Calculus
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
15 / 39
Fireball Calculus Statically: values are generalized to Fireballs. Dynamically: Call-by-Value ⇒ Call-by-Fireball. History: The Fireball Calculus λfire was introduced as a technical tool. (Paolini and Ronchi della Rocca 1999)
Rediscovered to improve the implementation of Coq. (Gr´ egoire and Leroy 2002)
Rediscovered (and named) to study cost models for CbV. (Accattoli and Sacerdoti Coen 2015)
Fireball is a pun on fire-able term.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
16 / 39
Fireball Calculus Language: Terms Values Fireballs
t, u, s v f
:= := :=
v x v
| | |
tu λx.t xf 1 . . . f n | {z }
n≥1
inert terms i
Weak evaluation contexts (exactly as before): E
:=
h · i | Et
| tE
Small-step Call-by-Fireball Evaluation: Rule at Top Level (λx.t)f 7→βf t{x f }
B. Accattoli, G.Guerrieri
Contextual closure E hti →βf E hui iff t 7→βf u
Open CBV
APLAS 2016
17 / 39
Properties of Fireballs 1
Open Harmony: a term t is a →βf -normal forms iff t is a fireball.
2
Conservative Extension of Closed CbV: if t is closed then t →βv u iff t →βf u.
3
Reasonable Cost Model (Accattoli and Sacerdoti Coen 2015): The number of →βf steps is a reasonable cost model. B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
18 / 39
Properties of Fireballs
1
The calculus is strongly confluent (diamond property).
2
Fireballs remove stuck β-redexes:
3
t := (λy .δ)(zz)δ
→βf
δδ
→βf
...
u := δ((λy .δ)(zz))
→βf
δδ
→βf
...
Problem: not confluent in its strong version.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
19 / 39
Outline 1
Closed CbV
2
Open CbV 0: Naive Open CbV
3
Open CbV 1: The Fireball Calculus
4
Open CbV 2: The Value Substitution Calculus
5
Open CbV 3: The Shuffling Calculus
6
Open CbV 4: The Value Sequent Calculus
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
20 / 39
The Value Substitution Calculus
The Value Substitution Calculus λvsub was introduced by Accattoli and Paolini. (2012) It is a λ-calculus with explicit substitutions. Modern approach to explicit substitutions, called at a distance. Grounded in linear logic.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
21 / 39
Value Substitution Calculus Language: t, u, s
:=
x
|
λx.t
|
|
tu
t[x u]
Evaluation contexts and Lists of substitutions contexts: :=
h·i
|
Et
L :=
h·i
|
L[x t]
E
| tE
| E [x t]
| t[x E ]
Rewriting rules (closed by evaluation contexts E h · i): Lhλx.tiu
→m
Lht[x u]i
exponential t[x Lhv i]
→e
Lht{x v }i
multiplicative
Corresponding to principal (or key) cut-elimination cases. B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
22 / 39
Value Substitution Calculus vs Stuck β-redexes Lists of substitutions contexts: L :=
h·i
|
L[x t]
Rewriting rules: Lhλx.tiu
→m
Lht[x u]i
exponential t[x Lhv i]
→e
Lht{x v }i
multiplicative
Example: (λy .δ)(zz)δ
→m
δ[y zz]δ
→m
(xx)[x δ][y zz] →e
(δδ)[y zz] . . .
(Remember δ := λx.xx). B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
23 / 39
Value Substitution Calculus—Key Properties
1
Elegant and simple rewriting theory: Strongly confluent; The two evaluation rules commute and terminate (separately);
2
Isomorphic to linear logic proof nets.
3
Moggi’s equational theory is contained in the one of λvsub .
4
Our termination equivalences are all proved using λvsub .
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
24 / 39
Fireball and Value Substitutins Termination equivalence: A λ-term t terminates in λfire iff t terminates in λvsub . Moreover: evaluation takes the same number of →βf and →m steps. ⇒ the cost model transfer from λfire to λvsub . The result is trickier than it seems: multiplicative
Lhλx.tiu
→m
Lht[x u]i
Fireball β
(λx.t)f
→βf
t{x f }
→βf requires arguments to be fireballs while →m does not!
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
25 / 39
Outline 1
Closed CbV
2
Open CbV 0: Naive Open CbV
3
Open CbV 1: The Fireball Calculus
4
Open CbV 2: The Value Substitution Calculus
5
Open CbV 3: The Shuffling Calculus
6
Open CbV 4: The Value Sequent Calculus
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
26 / 39
The Shuffling Calculus
The Shuffling Calculus λshuf was introduced by Carraro ad Guerrieri in (2014). It solves the mismatch between denotational semantics and Naive Open CbV. It refines Plotkin’s calculus with rules shuffling constructors. The shuffling rules are CbV variants of Regnier’s σ-rules (for call-by-name).
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
27 / 39
The Shuffling Calculus Language: t, u, s
:=
x
|
λx.t
|
tu
Balanced Evaluation contexts: B ::= h·i | tB | Bt | (λx.B)t Rewriting rules (closed by balanced evaluation contexts Bh · i):
B. Accattoli, G.Guerrieri
CbV β
(λx.t)v
→βv[
t{x v }
Shuffling 1
((λx.t)u)s
→σ1[
(λx.ts)u
Shuffling 3
v ((λx.s)u)
→σ3[
(λx.v s)u
Open CBV
APLAS 2016
28 / 39
Shuffling Calculus Moves Stuck β-redexes Balanced Evaluation contexts: B ::= h·i | tB | Bt | (λx.B)t Rewriting rules: CbV β
(λx.t)v
→βv[
t{x v }
Shuffling 1
((λx.t)u)s
→σ1[
(λx.ts)u
Example: stuck β-redexes are moved by shuffling rules: (λy .δ)(zz)δ
B. Accattoli, G.Guerrieri
→σ1[
(λy .δδ)(zz) →βv[
Open CBV
(λy .δδ)(zz) →βv[ . . .
APLAS 2016
29 / 39
Shuffling Calculus Key Properties
Carraro and Guerrieri show that λshuf is adequate wrt denotational semantics. Denotational semantics = relational semantics induced by linear logic. Adequacy: a term t terminates iff its interpretation is non-empty.
λshuf is not strongly confluent but SN and WN coincide.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
30 / 39
Shuffling vs Value Substitutions
We show how to simulate λshuf in λvsub , obtaining termination equivalence. Quantitatively: the number of →βv[ steps coincide with the number of →e steps. (λx.t)v
→βv[
t{x v }
exponential t[x Lhv i]
→e
Lht{x v }i
CbV β
This result does not allow to transfer the cost model. Termination equivalence ⇒ adequacy transfers to all incarnations of Open CbV.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
31 / 39
Outline 1
Closed CbV
2
Open CbV 0: Naive Open CbV
3
Open CbV 1: The Fireball Calculus
4
Open CbV 2: The Value Substitution Calculus
5
Open CbV 3: The Shuffling Calculus
6
Open CbV 4: The Value Sequent Calculus
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
32 / 39
The Value Sequent Calculus
The Value Sequent Calculus λvseq is a term syntax for sequent calculus. It is a fragment of Curien and Herbelin’s λµ˜ µ. ¯ µ (the CbV variant of λµ˜ Precisely, it is the intuitionistic restriction of λ˜ µ). Key idea: changing the applicative structure of λ-terms. Sequence of applications are looked at from the head value.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
33 / 39
The Value Sequent Calculus
Language: Commands Values Environments
c v e
::= hv | ei ::= x | λx.c ::= | µ ˜x.c | v ·e
Rewriting rules: Sequent β
hλx.c | v ·ei
→λ¯
hv | (˜ µx.c)@ei
µ ˜-rule
hv | µ ˜x.ci
→µ˜
c{x v }
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
34 / 39
Value Sequent Calculus—Key Properties
Strongly confluent. No need of contextual rewriting rules (rules at a distance). No need of shuffling rules. Drawback: drastic change of syntax.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
35 / 39
Sequent Calculus vs Value Substitution
λvseq is isomorphic to a sub-language of λvsub . The kernel λvsubk of λvsubk is obtained by restricting the application: t, u, s
:=
x
|
λx.t
|
tv
|
t[x u]
Everything else is unchanged. This can be seen as the language of administrivative normal forms of λvsub .
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
36 / 39
Sequent Calculus vs Value Substitution
Termination equivalence of λvsub and λvseq is proved in 2 steps: 1
Showing that λvsub is termination equivalent to its kernel λvsubk ;
2
Showing that the kernel λvsubk is isomorphic to λvseq .
The number of →m steps is preserved (becoming →λ¯ steps). ⇒ the cost model transfers to the value sequent calculus λvseq .
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
37 / 39
Conclusions
4 calculi arising from different perspectives. All termination equivalent. They can be seen as different incarnations of Open CbV. The cost model is stable in 3 out of 4. Adequacy of denotational semantics. Linear logic foundation. Sequent calculus interpretation.
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
38 / 39
THANKS!
B. Accattoli, G.Guerrieri
Open CBV
APLAS 2016
39 / 39