
BRICS BRICS RS-96-19 Hatcliff & Danvy: Thunks and the Basic Research in Computer Science Thunks and the λ-Calculus λ John Hatcliff -Calculus Olivier Danvy BRICS Report Series RS-96-19 ISSN 0909-0878 June 1996 Copyright c 1996, BRICS, Department of Computer Science University of Aarhus. All rights reserved. Reproduction of all or part of this work is permitted for educational or research use on condition that this copyright notice is included in any copy. See back inner page for a list of recent publications in the BRICS Report Series. Copies may be obtained by contacting: BRICS Department of Computer Science University of Aarhus Ny Munkegade, building 540 DK - 8000 Aarhus C Denmark Telephone: +45 8942 3360 Telefax: +45 8942 3255 Internet: [email protected] BRICS publications are in general accessible through WWW and anonymous FTP: http://www.brics.dk/ ftp ftp.brics.dk (cd pub/BRICS) Thunks and the λ-calculus ∗ John Hatcliff † Olivier Danvy DIKU BRICS § Copenhagen University ‡ Aarhus University ¶ ([email protected]) ([email protected]) May 1996 Abstract Thirty-five years ago, thunks were used to simulate call-by-name under call-by-value in Algol 60. Twenty years ago, Plotkin presented continuation-based simulations of call-by-name under call-by-value and vice versa in the λ-calculus. We connect all three of these classical simulations by factorizing the continuation-based call-by-name simu- lation n with a thunk-based call-by-name simulation followed by C T the continuation-based call-by-value simulation v extended to thunks. C Λ F T / Λthunks F F F F F F F F v n F F C C F F" ΛCPS We show that actually satisfies all of Plotkin’s correctness cri- T teria for n (i.e.,hisIndifference, Simulation,andTranslation C theorems). Furthermore, most of the correctness theorems for n can C now be seen as simple corollaries of the corresponding theorems for v and . C T ∗To appear in the Journal of Functional Programming. †Supported by the Danish Research Academy and by the DART project (Design, Anal- ysis and Reasoning about Tools) of the Danish Research Councils. ‡Computer Science Department, Universitetsparken 1, 2100 Copenhagen Ø, Denmark. §Basic Research in Computer Science, Centre of the Danish National Research Foundation. ¶Computer Science Department, Ny Munkegade, B. 540, 8000 Aarhus C, Denmark. 1 1 Introduction In his seminal paper, “Call-by-name, call-by-value and the λ-calculus” [27], Plotkin presents simulations of call-by-name by call-by-value (and vice- versa). Both of Plotkin’s simulations rely on continuations. Since Algol 60, however, programming wisdom has it that thunks canbeusedtoobtain a simpler simulation of call-by-name by call-by-value. We show that compos- ing a thunk-based call-by-name simulation with Plotkin’s continuation- T based call-by-value simulation v actually yields Plotkin’s continuation- C based call-by-name simulation n (Sections 2 and 3). Revisiting Plotkin’s correctness theorems (Section 4),C we provide a correction to his Transla- tion property for n, and show that the thunk-based simulation satisfies C T all of Plotkin’s properties for n. The factorization of n by v and makes C C C T it possible to derive correctness properties for n from the corresponding C results for v and . This factorization has also found several other appli- cations alreadyC (SectionT 5). The extended version of this paper [15] gives a more detailed development as well as all proofs. 2 Continuation-based and Thunk-based Simulations We consider Λ, the untyped λ-calculus parameterized by a set of basic con- stants b [27, p. 127]. e Λ ∈ e ::= b x λx.e e0 e1 | | | The sets Valuesn [Λ] and Valuesv [Λ] below represent the set of values from the language Λ under call-by-name and call-by-value evaluation respectively. v Valuesn [Λ] v Valuesv [Λ] ∈ ∈ ...where e Λ v ::= b λx.e v ::= b x λx.e ∈ | | | Figure 1 displays Plotkin’s call-by-name CPS transformation n (which simulates call-by-name under call-by-value). (Side note: the termC “CPS” stands for “Continuation-Passing Style”. It was coined in Steele’s MS the- sis [35].) Figure 2 displays Plotkin’s call-by-value CPS transformation v (which simulates call-by-value under call-by-name). Figure 3 displays theC standard thunk-based simulation of call-by-name using call-by-value evalu- ation of the language Λτ .Λτ extends Λ as follows. e Λτ e ::=∈ ... delay e force e | | 2 n [ ] :ΛΛ C h·i → n[v]=λk.k n v Chi C h i n [x] = λk.x k C h i n [e0 e1] = λk. n [e0] (λy0.y0 n [e1] k) C h i C h i C h i n : Valuesn[Λ] Λ C h·i → n b = b C h i n λx.e = λx. n [e] C h i C h i Figure 1: Call-by-name CPS transformation v [ ] :ΛΛ C h·i → v[v]=λk.k v v Chi C h i v [e0 e1] = λk. v [e0] (λy0. v [e1] (λy1.y0 y1 k)) C h i C h i C h i v : Valuesv [Λ] Λ C h·i → v b = b C h i v x = x C h i v λx.e = λx. v [e] C h i C h i Figure 2: Call-by-value CPS transformation :ΛΛτ T → [b] = b Th i [x] = force x Th i [λx.e] = λx. [e] Th i Th i [e0e1] = [e0] (delay [e1]) Th i Th i Th i Figure 3: Call-by-name thunk transformation 3 The operator delay suspends the evaluation of an expression — thereby coercing an expression to a value. Therefore, delay e is added to the value sets in Λτ . v Valuesn [Λτ] v Valuesv [Λτ] ∈ ∈ ...where e Λτ v ::= ... delay e v ::= ... delay e ∈ | | The operator force triggers the evaluation of such a suspended expression. This is formalized by the following notion of reduction. Definition 1 (τ-reduction) force (delay e) τ e −→ We also consider the conventional notions of reduction β, βv, η,andηv [3, 27, 32]. Definition 2 (β,βv, η, ηv-reduction) (λx.e1) e2 β e 1 [ x := e2] −→ (λx.e)v β e [ x := v] v Valuesv [Λ] −→ v ∈ λx.e x η exFV(e) −→ 6∈ λx.v x η vvValuesv [Λ] x FV(v) −→ v ∈∧ 6∈ For a notion of reduction r, r also denotes construct compatible one- −→ step reduction, r denotes the reflexive, transitive closure of r ,and −→−→ −→ =rdenotes the smallest equivalence relation generated by r [3]. We will −→ also write λr e1 = e2 when e1 =r e2 (similarly for the other relations). ` + Figure 4 extends v (see Figure 2) to obtain v which CPS-transforms + C C thunks. faithfully implements τ-reduction in terms of βv (and thus β) Cv reduction. We write βi below to signify that the property holds indifferently for βv and β. + + Property 1 For all e Λτ , λβi [force (delay e)] = [e] . ∈ `Cvh i Cv h i Proof: + + v [force (delay e)] = λk.(λk.k( v [e] )) (λy.y k) C h i C h+ i β i λk.(λy.y k) v [e] −→ + C h i β i λk. v [e] k −→ + C h i β [ e ] −→ i C v h i The last step holds since a simple case analysis shows that + [e] always Cv h i has the form λk.e0 for some e0 Λ. ∈ 4 3 Connecting the Continuation-based and Thunk-based Simulations n can be factored into two conceptually distinct steps: (1) the suspension ofC argument evaluation (captured in ); and (2) the sequentialization of function application to give the usualT tail-calls of CPS terms (captured in +). Cv + Theorem 1 For all e Λ, λβi ( )[e] = n [e]. ∈ ` Cv ◦T h i C h i Proof: by induction over the structure of e. case e b: ≡ + + ( v )[b] = v [b] C ◦T h i = λk.kC h bi = n [b] C h i case e x: ≡ + + ( v )[x] = v [force x] C ◦T h i = λk.C h(λk.k x)(i λy.y k) β λk.(λy.y k) x −→ i β λk.x k −→ i = n [x] C h i case e λx.e : ≡ 0 + + ( v )[λx.e0] = v [λx. [e0]] C ◦T h i = Cλk.kh (λx.Th( +ii )[e]) Cv ◦T h 0i =β λk.k (λx. n [e0] ) ...by the ind. hyp. i C h i = n [λx.e ] C h 0 i case e e0 e1: ≡ + + ( v )[e0e1] = v [ [e0] (delay [e1])] C ◦T h i C hTh+ i Th i i + = λk.( v )[e0] (λy0.(λk.k ( v )[e1])(λy1.y0 y1 k)) C+ ◦T h i C ◦T h+ i β i λk.( v )[e0] (λy0.(λy1.y0 y1 k)( v )[e1]) −→ C+ ◦T h i + C ◦T h i β λk.( )[e0] (λy0.y0 ( )[e1] k) −→ i Cv ◦T h i Cv ◦T h i =β λk. n [e0] (λy0.y0 n [e1] k) ...by the ind. hyp. i C h i C h i = n [e0 e1] C h i 5 This theorem implies that the diagram in the abstract commutes up to βi- + equivalence, i.e., indifferently up to βv-andβ-equivalence. Note that Cv ◦T and n only differ by administrative reductions. In fact, if we consider C optimizing versions of n and v that remove administrative redexes, then the diagram commutesC up to identityC (i.e.,uptoα-equivalence). Figures 5 and 6 present two such optimizing transformations n.opt and C v.opt.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages25 Page
-
File Size-