Professional Documents
Culture Documents
The Duality of Computation - Herbelin, Hugo
The Duality of Computation - Herbelin, Hugo
ABSTRACT [13, 12], where it was shown that simply-typed λ-terms (or
We present the λµµ̃-calculus, a syntax for λ-calculus + con- λµ-terms) in (call-by-name) normal form are in bijective
trol operators exhibiting symmetries such as program/con- correspondence with cut-free sequent calculus proofs in a
text and call-by-name/call-by-value. This calculus is derived suitable restriction of Gentzen’s LJ (or LK) [9]. Danos,
from implicational Gentzen’s sequent calculus LK, a key Joinet, and Schellinx identified the same restriction of LK
classical logical system in proof theory. Under the Curry- – and called it LKT – as part of a thorough investigation
Howard correspondence between proofs and programs, we of linear logic encodings of classical proofs [5, 6]. Hav-
can see LK, or more precisely a formulation called LKµµ̃ , ing gained through this correspondence the “naturalness”
as a syntax-directed system of simple types for λµµ̃-calculus. that was making the natural deduction usually preferred
inria-00156377, version 1 - 20 Jun 2007
For λµµ̃-calculus, choosing a call-by-name or call-by-value in practice, there was no reason any longer not to system-
discipline for reduction amounts to choosing one of the two atically study λ-calculus through sequent calculus rather
possible symmetric orientations of a critical pair. Our anal- than through the traditional Curry-Howard correspondence
ysis leads us to revisit the question of what is a natural with natural deduction. Sequent calculus is far more well-
syntax for call-by-value functional computation. We define behaved than natural deduction: it enjoys the subformula
a translation of λµ-calculus into λµµ̃-calculus and two dual property, and destruction rules – cuts – are well character-
translations back to λ-calculus, and we recover known CPS ized in contrast with the elimination rules of natural deduc-
translations by composing these translations. tion which superimpose both a construction and a destruc-
tion operation: the application is a constructor in a term
xM , but is destructive in a term (λx.M )N .
1. INTRODUCTION The leading goal at the root of the present work was
to conceive a “sequent calculus” version of call-by-value λ-
Programming languages present implicit symmetries such calculus and λµ-calculus. Our starting point was the ob-
as input/output, or program/context. Less obviously – as servation that the call-by-value discipline manipulates in-
shown recently by Selinger in a categorical setting [23] –, the put much in the same way as (the classical extension of)
picture can be extended to evaluation mechanisms: there ex- λ-calculus manipulates output. Computing M N in call-by-
ists a symmetry between call-by-name and call-by-value (an value can be viewed as filling the hole (hence an input) of
earlier attempt in this direction can be found in Filinski [8]). the context M [ ] with the result of the evaluation of N .
On the logical side, the best fit for evidencing symme- So the focus is on contexts waiting for values – a situation
tries is sequent calculus (based on left and right introduc- that sounds dual to that of (output) values being passed to
tion rules). But the correspondence between programs and continuations.
proofs is traditionally explained through natural deduction This leads us to a syntax with three different syntactic cat-
(based on right introduction and right elimination rules), egories: contexts, terms, and commands1 . Commands are
implication elimination (also called Modus Ponens) corre- pairs consisting of a term and a context, they represent a
sponding to procedure application. We believe that this tra- closed system containing both the program and its environ-
dition is in good part misleading. In this paper, we present ment. Correspondingly, we type these different categories
a sequent calculus style syntax that exhibits the above sym- with three kinds of sequents. The usual sequents Γ ⊢ ∆
metries in a precise, (and – we believe – compelling) way. type commands, while the sequents typing terms (contexts)
A key step in this program was already accomplished in are of the form Γ ⊢ A | ∆ (Γ | A ⊢ ∆). The symbol “|”
serves to single out a distinguished conclusion/output (hy-
pothesis/input), which stands for “where the computation
will continue” (“where it happened before”).
Permission to make digital or hard copies of all or part of this work for In the rest of this introduction, we offer as a prologue
personal or classroom use is granted without fee provided that copies are
not made or distributed for profit or commercial advantage and that copies 1
bear this notice and the full citation on the first page. To copy otherwise, to Danos has also recognized the relevance of these three cate-
republish, to post on servers or to redistribute to lists, requires prior specific gories in [4] where he extends the work of Ogata [18] on the
permission and/or a fee. relation between LKQ and call-by-value CPS-translations
ICFP’00 Montreal, Canada (LKQ is the other natural restriction of LK considered in
Copyright 2000 ACM 1-58113-202-6/00/0009 ..$5.00 [5, 6]).
a simple justification of the relevance of sequent calculus 2. CALL-BY-NAME λµ-CALCULUS IN SE-
to the computational study of the λ-calculus. Call-by-name QUENT CALCULUS STYLE
evaluation in the λ-calculus can be specified by the following
inference rule: In this section we present (a variant of) the λµ-calculus of
M →∗ λx.P [13]. This calculus is to sequent calculus what λµ-calculus
is to natural deduction. (The syntax and the typing rules of
M N → P [x ← N ] . simply-typed λµ-calculus are recalled in appendix A.) The
syntax (as well as those of the subsequent sections) embodies
This recursive specification may be implemented using a
the three syntactic categories discussed in the introduction:
stack as follows (M · S is the result of pushing M on top
of S): Commands c ::= hv|Ei
Contexts E ::= α | v · E
(M N , S) → (M , N · S) Terms v ::= x | µβ.c | λx.v
(λx.P , N · S) → (P [x ← N ] , S)
Its typing system is a sequent calculus based on judgements
This simple device is called Krivine abstract machine. It of the following form:
can be rephrased using contexts instead of stacks:
c : (Γ ⊢ ∆) Γ|E : A ⊢ ∆ Γ ⊢ v : A|∆
(M N , E) → (M , E[[ ]N ])
(λx.P , E[[ ]N ]) → (P [x ← N ] , E) and typing rules are given below:
Proposition 5.3. For any λµ-term M , M < and any of This rule allows us to reduce (λxx′ .M )(yN )V as follows:
its CBV reducts in λµµ̃-calculus lies in λµµ̃Q -calculus.
(λxx′ .M )(yN )V → (let x = yN in λx′ .M )V
Hence, for any λµ-term M , M < stands in the intersection
→ let x = yN in (λx′ .M )V
of λµµ̃T -calculus and λµµ̃Q -calculus (it uses only V ’s and
→ let x = yN in M [x′ ← V ] .
E’s, in our notation), and its reducts stay in the relevant
subsyntax once an evaluation discipline has been fixed. Consider now a term of the form (λx.M )((λy.V )(zN )) (y
The systems λµµ̃T -calculus and λµµ̃Q -calculus are also not free in M ), which is normal for βV , and whose (let ) +
well-behaved without reference to the λµ-calculus. It is easy (let β ) normal form is let x = (let y = zN in V ) in M . We
to check that λµµ̃T -calculus (λµµ̃Q -calculus) is stable under have:
CBN (CBV) reduction and that normal commands lies in
λµ-calculus (λµ̃-calculus, defined in next section). (let x = (let y = zN in V ) in M )<
=
6. WHAT IS CBV λ-CALCULUS? µα.hµα′ .h(zN )< |µ̃y.hV < |α′ ii|µ̃x.hM < |αii
↓(µ)
In section 2, we arrived at a perfect correspondence be- µα.h(zN )< |µ̃y.hV < |µ̃x.hM < |αiii
tween call-by-name λµ-normal forms and (call-by-name) λµ- ↓(µ̃)
normal forms. We wish to reach the same goal for call-by- µα.h(zN )< |µ̃y.hM < [x ← V < ]|αii
value normal forms. Moreover, for the purposes of duality, =
we wish to eliminate the need of the µ-operation to encode (let y = zN in M [x ← V ])<
call-by-value computation, since we did not need the µ̃ oper-
ator to encode call-by-name computation. Recall Plotkin’s Here again the translation manages to reduce the “hidden”
definition of call-by-value reduction: redex (let x = V in M ). This leads us to introduce another
rule:
(βV ) (λx.M )V → M [x ← V ]
(V variable or abstraction) . (let let ) let x = (let y = N in M ) in P
→ let y = N in (let x = M in P )
A typical (βV ) normal form is thus (λx.M )(yN ), whose (y not free in P )
translation
It is easily checked that the (let) + (let β ) + (let app ) + (let let )
µα.hµβ.hN < |µ̃z.hy|z · βii|µ̃t.hλx.M < |t · αii
normal forms are as follows:
contains a (→) redex. A simple way out is to extend the
syntax of the λ-calculus with a let construct: M ::= x | λx.M | let x = xM M1 . . . Mn in M | xM M1 . . . Mn
M ::= x | λx.M | M N | let x = N in M It is possible at this stage to write a translation V from this
and to replace (βV ) by the following reduction rules: set of normal forms to λµµ̃-terms in call-by-value normal
form. But we would have to use µ in the translation, typi-
(let) (λx.M )N → (let x = N in M ) cally in a term like x(yM ), for which one has to write
(let β ) (let x = V in M ) → M [x ← V ]
(V variable or abstraction) (x(yM ))V V
α = hx|µβ.hy|M · βi · αi .
In order to avoid placing applicative subterms in the con- defines in fact a bijection between the two syntaxes:
texts, and hence in order to achieve our second goal of “get-
xV = x
ting rid of µ”, we introduce a last rule:
(λ(x, α).c)V = λ(x, α).cV
(let exp ) M a → (let x = a in M x)
(a application or let expression) ([α](V V1 . . . Vn ))V = hV V |V1V · . . . · VnV · αi
(let x = V V1 . . . Vn in c)V = hV V |V1V · . . . · VnV · µ̃x.cV i
The (let) + (let β ) + (let app ) + (let let ) + (let exp ) normal forms
We can now state the CBV counterpart of proposition 2.2.
are as follows:
V
Proposition 6.1. The translation is an isomorphism
V ::= x | λx.M from λµ̃-terms to λµ̃-terms.
M ::= V | let x = yV V1 . . . Vn in M | xV V1 . . . Vn
Remark 6.2. In [21], Sabry and Felleisen characterized
We are now ready for our call-by-value normal form to nor- the theory induced on λ-calculus by the call-by-value CPS
mal form translation, which is defined below. Notice the use translation as the theory induced by the following two equa-
of the double abstraction (cf. remark 2.4 – the point is that tions in addition to (βV ):
we need µ only under a λ):
(βlift ) E[(λx.M )Q] = (λx.E[M ])Q
(E ::= [ ] | EN | (λx.P )E)
xV = x
(λx.M )V = λ(x, α).MαV for α fresh
(βflat ) xV1 V2 = (let y = xV1 in yV2 )
VαV = hV V |αi Our rules (let app ) and (let let ) correspond exactly to (βlift )
(xV V1 . . . Vn )V
α = hx|V
V
· V1V · . . . · VnV · αi (cases EN and (λx.P )E, respectively). Notice that the other
(let x = yV V1 . . . Vn in M )V α
(let) rules are transparent from the point of view of the λ-
= hy|V V · V1V · . . . · VnV · µ̃x.MαV i calculus (without let). The rule (βflat ), interpreted from right
to left, corresponds to the following η-like equation2 :
inria-00156377, version 1 - 20 Jun 2007
hv|ei⊳ = v ⊳ e⊳ VA = A ⊳
α⊳ = α KA = A◦⊳
KA = RVA
x⊳ = λk.kx CA = RKA
CA = RKA
(µβ.c)⊳ = λβ.c⊳
(µ̃x.c)⊳ = λx.c⊳ λ λ
(λx.v)⊳ = λk.k(λ(x, β).v ⊳ β) C Γ , K∆ ⊢ M > ⊲ : C A V Γ , K∆ ⊢ M > ⊳ : C A
(v · e)⊳ = λk.v ⊳ (λx.k(x, e⊳ ))
(βλ.e)⊳ = λ(y, β).e⊳ y KA→B VA→B
VB
(e · v)⊳ = λk.v ⊳ (λy.k(y, e⊳ )) = (B ◦ − A◦ )⊳ = RVA ×R
= KB × CA ∼
= V A → CB
Proposition 8.1.
LKµµ̃ Here, the letters V , K, and C stand for values, continu-
9 8
⊳ λ
c : (Γ ⊢ ∆) > Γ⊳ , R∆ ⊢ c⊳ : R
>
ations, and computations, respectively. Lafont-Hofmann-
>
> >
>
LKµµ̃
= <
⊳ λ A⊳
Γ ⊢
v : A|∆ > =⇒ Γ⊳ , R ∆ ⊢ v ⊳ : R R Streicher semantics maps computations to computations and
LKµµ̃
> >
: ⊳ ∆⊳ λ ⊳
> ⊳
interprets a continuation of type A → B as a pair of a com-
Γ , R ⊢ e : RA
> >
putation of type A and a continuation of type B (think of
;
Γ|e : A ⊢ ∆
the stack N · S of section 1). Plotkin’s call-by-value seman- environment). The rules of the machine are as follows:
tics maps values to computations, and interprets a value of
type A → B as a function from values to computations. h(λx.v1 ){p}|(v2 · e){q}i{s}
repeatedly p
→ hv1 {k s k}|e{q}i{(x = v2 {q}) · s}
Remark 8.4. If we combine ⊲ and ⊳ with the transla-
tion < rather than with the translation > , the picture is as h(µβ.hv|e′ i){p}|e{q}i{s}
p
follows: in CBN, M <⊲ reduces to M >⊲ , while, in CBV, M <⊳ → hv{k s k}|e′ {k s k}i{(β = e{q}) · s}
provides a right-to-left variant of Plotkin’s translation. For
M = M1 M2 , we get respectively: hv{p}|(µ̃x.hv ′ |ei){q}i{s}
q
→ hv ′ {k s k}|e{k s k}i{(x = v{p}) · s}
λk.M1>⊳ (λm1 .M2>⊳ (λm2 .m1 (m2 , k)))
λk.M2<⊳ (λm2 .M1<⊳ (λm1 .m1 (m2 , k))) .
hx{p}|e{q}i{s}
→ hs|p (x)|e{q}i (s|p (x) is defined)
9. HEAD REDUCTION IN AN ABSTRACT
MACHINE hv{p}|α{q}i{s}
→ hv{p}|s|q (α)i (s|q (α) is defined)
In this section, we specify two kinds of (weak) head re-
duction machine. hv{p}|e{q}i{s}
The first machine is quite standard and based on environ- → hv{p}|e{q}i{s|max(p,q) }
ments. Instead of commands hv|ei, we manipulate expres-
sions having the form hv{ρ1 }|e{ρ2 }i, where ρ1 and ρ2 are
(explicit) environments, i.e., lists of bindings of the form where
(x = v{ρ}) or (α = e{ρ}). Given ρ1 and x, we write n
ρ1 (x) = v{ρ2 } if (x = v{ρ2 }) is the first binding of x ap- ((x = v{p}) · s)(x) = v{p}
n
((y = v{p}) · s)(x) = s|n (x) x 6= y
inria-00156377, version 1 - 20 Jun 2007
hx{ρ1 }|e{ρ2 }i
→ hρ1 (x)|e{ρ2 }i (ρ1 (x) is defined)
10. RELATED AND FUTURE WORKS
hv{ρ1 }|α{ρ2 }i
→ hv{ρ1 }|ρ2 (α)i (ρ2 (α) is defined)
We end by miscellaneous remarks organized along hope-
Remark that bindings of terms (contexts) have the re- fully helpful keywords.
stricted form (x = V {ρ}) ((α = E{ρ})) when reducing CBV
(CBN). • Symmetry. Altogether, we have defined six calculi:
The second machine exploits the idea of encoding envi- the full λµµ̃ syntax in CBN and CBV discipline (section 4),
ronments by means of indexes in a stack as in the Pointer the subsyntaxes λµµ̃T -calculus / LKTµµ̃ and λµµ̃Q -calculus
Abstract Machine from Danos-Regnier [7] (a restriction of / LKQµµ̃ (section 5), and as further restrictions the λµ-
it was studied in a previous work of the authors [3]). calculus (section 2) and the λµ̃-calculus (section 6). At all
The stack is a sequence of bindings that bind either a term these levels, the duality of call-by-name and call-by-value is
or a context. The concrete syntax for bindings is: governed by the symmetry of the µ-terms and the µ̃-terms.
n n
(x = v{p}) or (α = e{p}) (n, p natural numbers) . The situation is summarized in the following table. In the
table, ←֓ shows a source-to-target direction (cf. proposition
The expression v{p} is a closure: p is a pointer in the 5.3), while ↔ indicates stronger one-to-one correspondences
stack to where the environment of v starts. Similarly for (cf. proposition 2.2 and 6.1). It would be interesting to com-
e{p}. Any pointer to the stack defines an environment by plete this picture by adding more strong correspondences
chaining the bindings with the pointer n. ↔. One could in particular show that CBV λµ-calculus (for
A state of the machine is hv{p}|e{q}is. If s is a stack, we which let arrives naturally) relates to CBV λµµ̃-calculus.
denote its length by k s k, and s|n is the stack restricted to One could also consider call-by-name λ-calculus plus let,
its n first elements (thus, k s|n k= n). Pointers are relative which has CBN λµµ̃-calculus as a target. In such a calculus,
not to the top but from the bottom of the stack. To evaluate one could delay some substitutions, as in let x = yM1 in M2
hv|ei, we start from hv{0}|e{0}i (with 0 denoting the empty (or µα.hy|v1 · µ̃x.hv2 |αii), i.e. we could force some sharing
of subcomputations. [4] V. Danos 1999. Sequent Calculus and
Continuation Passing Style Compilation. To appear in
Logic Syntax Evaluation Language
Proc. of the 11th Congress of Logic, Methodology and
8 Philosophy of Science, held in Cracow, Kluwer.
< ND
[5] V. Danos, J.-B. Joinet and H. Schellinx 1995.
LKµµ̃ λµµ̃ CBN
LKQ and LKT: sequent calculi for second order logic
CBV
:
based upon dual linear decompositions of classical
LKTµµ̃ λµµ̃T CBN implication. In Advances in Linear Logic, Cambridge
LKQµµ̃ λµµ̃Q CBV ←֓ CBV λµ University Press, 211–224.
LKTµ λµ CBN ↔ CBN λµ [6] V. Danos, J.-B. Joinet and H. Schellinx 1997.
LKQµ̃ λµ̃ CBV ↔ λµ̃ A New Deconstructive Logic: Linear Logic. In Journal
of Symbolic Logic 62(3), 755–807.
• Non-determinism. In [1] and [24], the non-determinism [7] V. Danos and L. Regnier 1990. Machina ex
of classical logic is encapsulated in critical pairs similar to deo, ou encore quelque chose à dire sur la machine de
the (µ) − (µ̃) pair. But no explicit connection with call-by- Krivine. Unpublished.
name / call-by-value appears in those works. [8] A. Filinski 1989. Declarative Continuations: An
• Semantics. It is fairly clear that our syntax LKµµ̃ with Investigation of Duality in Programming Language
the CBN (CBV) machine can be interpreted in Selinger’s Semantics. In Proc. of Category Theory and Computer
control (co-control) categories: the categorical construction Science (CTCS), LNCS 389, 224–249.
interpreting → (−) is an exponent (a co-exponent), and − [9] G. Gentzen 1935. Investigations into logical
(→) is a weak co-exponent (a weak exponent). It should be deduction. E.g. in Gentzen collected works, Ed M. E.
interesting and useful to work out the details of this inter- Szabo, North Holland, 68ff (1969).
pretation. [10] J.-Y. Girard 1993. On the unity of logic. Annals
of Pure and Applied Logic 59 , 201–217.
• Dynamics. Laurent has investigated (CBN) proof-nets
inria-00156377, version 1 - 20 Jun 2007