Call-By-Name Is Just Call-By-Value with Delimited Control

Delimited control operator shift0 exhibits versatile capabilities: it can express layered monadic effects, or equivalently, algebraic effects. Little did we know it can express lambda calculus too! We present $ \Lambda_\$ $, a call-by-value lambda calculus extended with shift0 and control delimiter $ \$ $ with carefully crafted reduction theory, such that the lambda calculus with beta and eta reductions can be isomorphically embedded into $ \Lambda_\$ $ via a right inverse of a continuation-passing style translation. While call-by-name reductions of lambda calculus can trivially simulate its call-by-value version, we show that addition of shift0 and $ \$ $ is the golden mean of expressive power that suffices to simulate beta and eta reductions while still admitting a simulation back. As a corollary, calculi $ \Lambda\mu_v $, $ \lambda_\$ $, $ \Lambda_\$ $ and $ \lambda $ all correspond equationally.


Introduction
Delimited control [5,3] is a computational effect capable of expressing any monadic effect in direct style [6,7].Moreover, delimited continuations are closely related to algebraic effects [15,16], as it has been shown that delimited control, in the form of shift0 and $, are mutually macro expressible with algebraic effects with deep handlers [8,14].It is as strong as an infinite hierarchy of shifts [13].Finally, shift0 can recursively express control0 in direct style [19].
It is a basic observation that operational semantics of call-by-value lambda calculus can be simulated by call-by-name lambda calculus.Every β v (η v ) reduction step can be simulated by a β (η) step on the same term.The latter simulation is mediated by identity relation on terms.In this paper we show that there exists a relation (i.e.CPS translation * : Λ $ → λ) between call-by-value lambda terms extended with delimited control operators and vanilla, call-by-name lambda terms that admits a simulation in both ways, hence a bisimulation.We do not prove the bisimulation directly.Instead, we show that a stronger relationship between these calculi exists, namely a reflection.

14-2
Call-By-Name is Just Call-By-Value with Delimited Control In our previous, related works we followed the programme of Sabry and Wadler [18]: to seek a Galois connection (or even better, a reflection or isomophism) between reduction theories instead of equational correspondences whenever possible, as a Galois connection is a stronger relationship between calculi that already implies an equational correspondence.In [1] we developed such relationship between shift and its CPS, while in [2] we have done that for shift0.This paper improves upon the latter of these and upon undirected axiomatisation of [11], as we prove a reflection where the image of CPS is closed under βη reduction, hence a "directed axiomatisation".However, in the particular case of CPS translation for shift0-style delimited control it turns out that the reduction-closed image is not a proper subcalculus but the entire λ, hence every λ-term is, in fact, in continuation-composing style.A translation back (aka.direct style translation) from λ to Λ $ exists which allows to perfectly embed lambda calculus: image of the direct style translation is a subcalculus of Λ $ isomorphic with λ-calculus.
This paper has two goals.
(i) to bring about the definitive, fine-grained operational semantics for shift0 that can provide, a "directed axiomatisation" of delimited control.(ii) to show that call-by-value with delimited control in the form of shift0 is the same thing as call-byname purely functional programming on the level of operational semantics.
The second point cannot be stressed enough.Call-by-name reduction is exactly like call-by-value with shift0-style delimited control.It is not the case with abortive control nor shift of Danvy and Filinski [3], which can be deduced from previous axiomatisation efforts [17,9], whose CPS translations reach proper subsets of λ-terms.This puts shift0 control operator in a unique spot and as far as we know, it has not been acknowledged yet.

Outline
In Section 2 a new calculus of control is presented, named Λ $ , with its syntax and semantics.In Section 3 we show mutual macro-expressibility (and equational correspondence) of Λ $ and λ $ , an established calculus with shift0 and $, to support our claim that Λ $ is a call-by-value calculus with shift0-style delimited control.In Section 4 we prove theorems that justify the title of this paper: λ can be reflected into Λ $ .As a corollary, calculi Λµ v [4], λ $ [11], Λ $ and λ all correspond equationally.We conclude and discuss related and future work in Section 5.

Conventions
In this paper, ≡ means syntactic equality modulo renaming of bound variables, while = means term equivalence up to rewrite rules in a calculus; calculus is either implied by the usage or specified directly in the subscript.Given relation r, r ? is (≡) ∪ r, i.e. reflexive closure of r.Rewrites may occur in arbitrary contexts, including variable-binding contexts; these contexts may capture variables of a term that is plugged into.Terms are not required to be closed; it is perflectly fine for them to be open.Some classes of contexts (i.e.bindable and pure contexts) that we define may look a lot like evaluation contexts but that is not their purpose here.We are not concerned with standard reduction nor evaluation strategy, only with general reduction on open terms that may and will often be nondeterministic.
Atomic terms include variables x, parenthesised terms (M ), unary shift0 S 0 (M ) (we call it thaw ) and unary $ $(M ) (we call it freeze).Parentheses in these unary operators are mandatory and there is no space between operator and parenthesis.Plugging term in a context C[M ] and postfix translations M * , M † , M # , M ♮5 are left-associative and have the highest precedence.Application M N has the next precedence and is left-associative, followed by right-associative binary $ (known as plug [10] or dollar [13] 6 ) M $ N .We disambiguate M $(N ) as M ($(N )), and M $ (N ) as M $ N by different spacing.Syntaxes Pyzik 14-3 let x = M in N , λ x .N and S 0 x .N are right-associative, bind variable x in term N and have the lowest precedence.

Theory of preorders -Galois connections and reflections
A calculus equipped with a reduction relation can be seen as a preordered set (proset, a set with a reflexive and transitive relation).It may be helpful to think about a Galois connection as a compiler * from source language S to target language T coupled with a decompiler #.Laws of Galois connection guarantee that compiling and decompiling are harmonised: it does not matter whether simplification is performed on source or target, or on both ends.As an equivalence, a Galois connection allows two modes of reasoning: • Soundness.Every multi-step reduction in the source is valid in the target: if M ։ S N # , then M * ։ T N .
• Completeness.Every multi-step reduction in the target is valid in the source: if A reader familiar with (bi)simulations may notice that if a function f is monotone, then f is a similarity relation.When (f, g) is a Galois connection, then f is a bisimilarity relation.A reader somewhat familiar with category theory may recognise that preordered set is the same thing as a thin category.Monotone functions are functors between those categories.Galois connection is a pair of adjoint functors.A Galois connection between symmetric preorders (better known as equivalence relations) is simply an equational correspondence.
There is an alternative characterisation of a Galois connection.
Theorem 1.3 (Equivalent definition of Galois connection) Monotone functions f : A → B and g : B → A form a Galois connection if, and only if A reflection is a bit tighter kind of a Galois connection.

Background: Materzok's λ $
In this introductory subsection, we present syntax, reductions, axioms (Figure 1.4) and CPS translation (Figure 2) of λ $ , as defined by Materzok [11].It features a variable-binding control operator shift0 and binary operator $ that delimits continuation captured by shift0 in the right hand side.CPS translation to lambda calculus provides denotational semantics.The axioms were proven by Materzok to be sound and complete with respect to CPS translation.λ $ also has small-step operational semantics, which are sound (but not complete) with respect to axioms and CPS.These semantics allow shift0 to capture an arbitrarily long context in a single reduction step.
Example 1.6 Here's an evaluation example.Let I ≡ λ x .x.
Example 1.7 Terms in CPS get lengthy even for very small examples: 2 Syntax and semantics of Λ $ Reductions in Materzok's λ $ were clearly lacking.What we wanted was a set of reductions that would also stand as a complete axiomatisation of shift0.Our goal was to improve upon a sound but incomplete Pyzik 14-5 reduction theory of λ c$ [2].To reach completeness, we took the range of that previous CPS translation and closed this set under βη-reduction (in a similar vein to Sabry and Felleisen [17]).To our surprise, all quirks of the grammar disappeared and the entire λ showed up.To double check, we also closed CPS of λ $ under reduction: entire λ again.It was now a matter of time to find an improved calculus, CPS and DS (direct style) translation.Our choice to use unary operators may be considered arbitrary by some but this choice provided us with a beautiful duality of values and terms mediated by freeze and thaw and an actual, working reflection.Old semantics had to be modified anyway and syntax could use some refreshment too.
Our method was to have only one binding construct and use only local reductions to keep things as simple as possible.
We define syntax and small-step operational semantics of Λ $ in Figure 3. Figure also includes some syntactic sugar to make some idioms (like let-expressions) easier on the eye.
Indeed, let-expressions with both β and η rule are macro-definable.It is easy to check that let Initially we aimed for a calculus with a separate let construct whose CPS is diffrent than that of a β-redex, like in λ c [18], λ c S [1] and λ c$ [2].However, associativity kept derailing our theorems.We were stuck, so we got rid of the let construct and replaced its uses with a pattern S 0 k .(λx .k $ N ) $ M that behaves just like let-expressions.Due to its ubiquity in the proofs, we needed a shorthand notation; a let-expression syntax was an obvious choice.
One can think of unary $ as reifying or freezing a computation as a value.Such a value can be then reflected or thawed by unary shift0 (if given a nonvalue, it waits until its argument becomes a value).These operations are invertible using $ S 0 and S 0 $ reductions.Reductions $ v and pure tell us that a value V freezes to λ x .x V and can be recovered from it by thawing.The job of bind rule is to gives names to subcomputations and make sure contexts are managed properly.Notice that due to syntactic restriction that requires bind-redex to be of the form J[P ], there is at most one legal way to bind-contract: • S 0 (P ) bind let x = P in S 0 (x) is a legal bind-contraction, To enable further developments, Figure 2 introduces continuation-passing style translation * : Λ $ → λ, which provides alternative, denotational semantics of Λ $ .However, one does not need to worry about a mismatch of semantics -theorems of Section 4 are more than enough to ensure that * is a sound and complete translation (M = Λ $ N iff M * = λ N * ).λ-calculus that we target has both β and η reductions (Figure 6).
To lift the spirit even higher, confluence of Λ $ is proven below using definitions (translation # : λ → Λ $ ) and theorems of Section 4 but the proof is straightforward enough that it may be understandable right here.It gives us a taste of usefullness of reflections and Galois connections in general: one can transfer confluence from one calculus to another.Confluence is provided here as a sanity check of a newly introduced calculus; to avoid circular reasoning, we do not use it in proofs.Proof.For concise presentation, we chain relations and flip arguments:

14-6
Call-By-Name is Just Call-By-Value with Delimited Control term L, M, N :: (v) Apply left post-inverse theorem twice: ✷ The following lemma is quite handy in equational reasoning about Λ $ terms.Proof.By a chain of rewrites.
In this section, we defend the thesis that Λ $ is a calculus of delimited control and its control primitives behave like control operator shift0 and delimiter $.To meet these ends, we show that there exists an equational correspondence with λ $ of Materzok [11] expressed with macro-translations.As we mentioned already, λ $ has small-step operational semantics, which are sound (but not complete) with respect to axioms and CPS.These semantics allow shift0 to capture an arbitrarily long context in a single reduction step.Equational correspondence that we prove in this section shows that this traditional approach can also be used in equational reasoning about Λ $ terms.One might wonder, why we only prove equational correspondence and not a Galois connection?We clearly cannot connect directed ։ Λ $ with = λ $ because such a connection would transfer symmetry property from = λ $ to ։ Λ $ , a contradiction: I I → I →I I.We cannot connect ։ Λ $ with ։ λ $ using macros: every macro-translation would necessarily transfer λ y .x y ։ x verbatim which is provably true in Λ $ and false in λ $ .Our equational correspondence crucially uses Materzok's $ E axiom in both directions, so it is unclear how these axioms could be turned into directed reduction rules that would admit a Galois connection.We did not investigate translations that are not based on macros: such translation would weaken our claim that Λ $ is a calculus with shift0-style delimited control.
Basic reductions of Λ $ can be seen as set of axioms.It is later shown in Section 4 as corollary that for such axioms CPS translation * is sound and complete (M = Λ $ N iff M * = λ N * ), which is an improvement upon λ $ , whose reductions induce an equivalence relation on λ $ that is a proper subset of = λ $ , hence incomplete.These stronger properties make Λ $ a contender for the title of the definitive calculus of shift0-style delimited control.
To mediate the correspondence, we need translations to (ι) and from (π) Λ $ .Translations (with unfolded macros) are presented in Figure 5.With folded macro-definitions they would look like an identity function.To translate from λ $ to Λ $ , we use macros from Figure 3.To translate from Λ $ to λ $ , we use macros from Figure 2. Lemma 3.1 (Embedding ι is invertible) The following equalities hold: • Left inverse property.For all e ∈ λ $ , π(ι(e)) = e.

14-8
Call-By-Name is Just Call-By-Value with Delimited Control Proof.Both propositions are separately proven by structural induction.
x is fresh wrt.V and K.
Proof.By induction on K.In the context of this proof, we use the rules of Λ $ (Fig. 3).
• Inductive case.Use of an inductive hypothesis is marked by "IH".
• For all Proof.To prove the first proposition it suffices to show that all axioms of λ $ are also valid equalities in Λ $ .
• Axiom β v follows immediately by rule β v .
• Axiom η v follows immediately by rule η v .
To prove the second proposition, apply the first proposition on assumption π(M ) = π(M ′ ) to get ι(π(M )) = ι(π(M ′ )) and rewrite by the right inverse property of ι to infer M = M ′ .✷ Having the soundness of ι secured, we move on to completeness.In order to prove it, we show that CPS translation of Materzok is equivalent to ours.Syntax and small-step operational semantics of λ-calculus are provided for reference in Figure 6.Proof.By structural induction.

✷
We strike the final nail in the coffin of doubt with the completeness theorem.
• For all It follows that calculi Λ $ and λ $ correspond equationally via macro-definitions, hence our Λ $ is truly a calculus of shift0-style delimited control.

Reflection of lambda calculus into Λ $
We move on to the main result of this paper: the relationship between Λ $ and λ.It will be shown that λ reflects into Λ $ .Syntax and small-step operational semantics of λ-calculus are provided for reference in Figure 6.Following in the footsteps of Sabry and Wadler [18] we intended to define a backwards, direct style translation # from the range of * , just like in [1] and [2].We took the range of the old CPS and closed it under reduction, following the recipe by Sabry and Felleisen [17].What we've got was the entire set of lambda terms.A question arised: was there a better CPS that would explicitly hit every possible lambda term?The answer is positive.In other words, for every term M ∈ λ, there exists a term M # ∈ Λ $ , such that M # * ≡ M .Every lambda term is in Continuation-Passing Style, it seems!Translation # has one oddity: special treatment of λ x .x N when x is fresh wrt.N .One may notice that it is the shape of a value translated to CPS.This is not essential for the right inverse theorem to hold but it is a necessary adjustment for the left post-inverse theorem.
When it comes to discovery of those translations, it was mostly trial and error, working within confines of a reflection, fine-tuning simultaneously the translations and the calculus.Just as CPS translations come in a pair: one on value, one on general terms, both had to be inverted and therefore we have in fact two different embeddings of lambda terms: one embeds into values of Λ $ and the other into general terms of Λ $ .They need each other to compute DS translation just like the two CPS translations interleave to bring a CPS term.

Definition 1 . 2
Monotone functions f : A → B and g : B → A form a Galois connection if, and only if for all a ∈ A and b ∈ B, a ։ A g(b) ⇐⇒ f (a) ։ B b.
-Name is Just Call-By-Value with Delimited Control term M, N ::= x | λ x .M | M N reduction context C

Proof.
To prove the first proposition, apply monotonicity of * (Theorem 4.8) on assumption ι(e) = ι(e ′ ) to get ι(e) * = ι(e ′ ) * .Rewrite using equivalence of CPS translations to get C e = C e ′ and finally apply completeness of C • to conclude that e = e ′ .To prove the second proposition, rewrite assumption M = M ′ with left inverse property of ι to get ι(π(M )) = ι(π(M )).Apply the first proposition to finally arrive at π(M ) = π(M ′ ).✷