Thunks (continued) Olivier Danvy, John Hatcli. Department of Computing and Information Sciences. Kansas State University. Manhattan, Kansas 66506, USA

Similar documents
The Essence of Compiling with Continuations

Extracting the Range of cps from Affine Typing

Polymorphic Type Assignment and CPS. Conversion. Robert Harper Mark Lillibridge. April, School of Computer Science. Pittsburgh, PA 15213

A Typed Calculus Supporting Shallow Embeddings of Abstract Machines

Information Processing Letters Vol. 30, No. 2, pp , January Acad. Andrei Ershov, ed. Partial Evaluation of Pattern Matching in Strings

CS 242. Fundamentals. Reading: See last slide

Chapter 11 :: Functional Languages

Constrained Types and their Expressiveness

Conclusions and further reading

Polymorphic Type Assignment and CPS Conversion. (To appear: ACM SIGPLAN Workshop on Continuations, San Francisco, June 1992)

Towards Leakage Containment. Julia L. Lawall and Daniel P. Friedman. Indiana University. Abstract

Type raising, continuations, and classical logic

arxiv: v1 [cs.pl] 3 Dec 2014

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

MPRI course 2-4 Functional programming languages Exercises

Implementing Software Connectors through First-Class Methods

Operational Semantics

Elf: A Meta-Language for Deductive Systems (System Description)

Beluga: A Framework for Programming and Reasoning with Deductive Systems (System Description)

Com S 541. Programming Languages I

Programming Languages Third Edition

From the λ-calculus to Functional Programming Drew McDermott Posted

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Compiling Monads. December 1991

Algebraic Properties of CSP Model Operators? Y.C. Law and J.H.M. Lee. The Chinese University of Hong Kong.

The Compositional C++ Language. Denition. Abstract. This document gives a concise denition of the syntax and semantics

Pragmatics of Type-Directed Partial Evaluation. Olivier Danvy. Computer Science Department. Aarhus University?

Answer Type Polymorphism in Call-by-name Continuation Passing

Introduction. chapter Functions

to automatically generate parallel code for many applications that periodically update shared data structures using commuting operations and/or manipu

Functional Programming. Big Picture. Design of Programming Languages

(Refer Slide Time: 4:00)

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

the application rule M : x:a: B N : A M N : (x:a: B) N and the reduction rule (x: A: B) N! Bfx := Ng. Their algorithm is not fully satisfactory in the

A Reduction of Conway s Thrackle Conjecture

Meta-programming with Names and Necessity p.1

Runtime Behavior of Conversion Interpretation of Subtyping

Functional Programming. Pure Functional Languages

A Small Interpreted Language

Hiding local state in direct style: a higher-order anti-frame rule

Pure Lambda Calculus. Lecture 17

Functional Languages. Hwansoo Han

(Refer Slide Time: 1:36)

Derivation of an Abstract Machine for λ-calculus with Delimited Continuation Constructs

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

Figure 1: The evaluation window. ab a b \a.b (\.y)((\.)(\.)) Epressions with nested abstractions such as \.(\y.(\.w)) can be abbreviated as \y.w. v al

From Macrogeneration to Syntactic Abstraction

More about Formatting. Olivier Danvy. Aarhus University. December Abstract

Concepts of Programming Languages

Reuse Contracts As Component Interface. Descriptions. Koen De Hondt, Carine Lucas, and Patrick Steyaert. Programming Technology Lab

Improving CPS-Based Partial Evaluation: Writing Cogen by Hand

i=1 i=2 i=3 i=4 i=5 x(4) x(6) x(8)

A Theory of Parallel Computation The π-calculus

A Language for Access Control in CORBA Security

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

3. Functional Programming. Oscar Nierstrasz

Lambda Calculus and Type Inference

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Programming Language Pragmatics

hexpressioni ::= hvariablei j hself evaluating literali j (quote hdatumi) j (lambda (hvariablei*) hexpressioni + ) j (if hexpressioni hexpressioni hex

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

Organization of Programming Languages CS3200/5200N. Lecture 11

CMPUT 325 : Lambda Calculus Basics. Lambda Calculus. Dr. B. Price and Dr. R. Greiner. 13th October 2004

Functional Programming

The Formal Semantics of Programming Languages An Introduction. Glynn Winskel. The MIT Press Cambridge, Massachusetts London, England

CS 415 Midterm Exam Spring 2002

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Time Stamps for Fixed-Point Approximation

SYSTEMS MEMO #12. A Synchronization Library for ASIM. Beng-Hong Lim Laboratory for Computer Science.

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Lecture 5. Logic I. Statement Logic

Valmir C. Barbosa. Programa de Engenharia de Sistemas e Computac~ao, COPPE. Caixa Postal Abstract

Vrije Universiteit Brussel Faculteit Wetenschappen. Continuation-Passing-Style as an Intermediate Representation for Compiling Scheme

perspective, logic programs do have a notion of control ow, and the in terms of the central control ow the program embodies.

Formal Systems and their Applications

The Substitution Model

UMIACS-TR December, CS-TR-3192 Revised April, William Pugh. Dept. of Computer Science. Univ. of Maryland, College Park, MD 20742

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

1 Introduction. 3 Syntax

Rance Cleaveland The Concurrency Factory is an integrated toolset for specication, simulation,

SAT-CNF Is N P-complete

Rule Formats for Nominal Modal Transition Systems

System Description: Twelf A Meta-Logical Framework for Deductive Systems

Black-Box Program Specialization

Single-pass Static Semantic Check for Efficient Translation in YAPL

when a process of the form if be then p else q is executed and also when an output action is performed. 1. Unnecessary substitution: Let p = c!25 c?x:

An ACL2 Tutorial. Matt Kaufmann and J Strother Moore

which a value is evaluated. When parallelising a program, instances of this class need to be produced for all the program's types. The paper commented

compile Target Interpreter t:target s:source Parsing Type Checking Stack Manipulation Code Generation Updating Store Optimization

Lambda Calculus and Type Inference

yacc, lex Source Code (C) compile Executable Parser Test on HOL Examples, TPTP Library, and TSTP Library

callee s frame... arg k caller s frame

Imperative Functional Programming

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

On Computing the Minimal Labels in Time. Point Algebra Networks. IRST { Istituto per la Ricerca Scientica e Tecnologica. I Povo, Trento Italy

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Kenneth A. Lambert. Department of Computer Science Washington and Lee University Parmly Hall 406 Lexington, VA Phone: (540)

How to Make a Correct Multiprocess Program Execute Correctly on a Multiprocessor

Fundamental Concepts. Chapter 1

1.3. Conditional expressions To express case distinctions like

Transcription:

Thunks (continued) Olivier Danvy, John Hatcli Department of Computing and Information Sciences Kansas State University Manhattan, Kansas 66506, USA e-mail: (danvy, hatcli)@cis.ksu.edu Abstract: Call-by-name can be simulated in a call-by-value setting using \thunks" (i.e., parameterless procedures) or continuation-passing-style (CPS). In this paper we uncover a relationship between the two simulations. We prove that the call-by-value CPS transformation C v, when applied to a term T (t) which simulates call-by-name using thunks, yields a term identical to the call-by-name CPS transformation C n applied directly to t (modulo renaming): C v T C n This result sheds new light on the call-by-name CPS transformation it can be factored into two conceptually distinct steps: the suspension of argument evaluation (captured in T ); the sequentialization of function application to give the usual tail-calls of CPS terms (captured in C v). Keywords: -calculus, call-by-name, call-by-value, continuation-passing style transformation. 1. Introduction Among many possible implementations of call-by-name in Algol 60, one still stands the test of time: Ingerman's \thunks" [8]. A thunk forms the representation of a function argument. As it turns out, Ingerman's implementational view of thunks coincides with the representation of a parameterless procedure. Proceedings of the Second International Workshop on Static Analysis WSA'92, volume 81-82 of Bigre Journal, pages 3-11, Bordeaux, France, September 1992. IRISA, Rennes, France. This work was partly supported by NSF under grant CCR-9102625.

2 Olivier Danvy & John Hatcli Consequently, parameterless procedures (traditionally referred to as \thunks") have become part of the programmer's toolbox to implement callby-name in a call-by-value language such as Standard ML or Scheme [1, 3]. Using side eects, thunks also enable one to implement call-by-need (as in the Yale implementations of lazy functional languages [2]). In fact, thunks can be introduced systematically by wrapping each actual parameter in a parameterless procedure. Let T denote this \thunkifying" program transformation. Call-by-name (CBN) can also be simulated under call-by-value with continuation-passing-style (CPS). This simulation was formalized by Plotkin in the early '70s [9]. The call-by-name CPS transformation C n is a more drastic program transformation than T in that it changes the structure of a term more radically. C n explicitly encodes the CBN evaluation strategy into the transformed term, using functions called \continuations". Continuations express how values will be used during the evaluation of the \rest of the program". Correspondingly, call-by-value (CBV) can also be simulated under callby-name with CPS. The CBV CPS transformation C v also explicitly encodes the CBV evaluation strategy into the transformed term, using continuations. In fact, C n and C v are best known to yield terms that are independent of the evaluation order [10]. They are used in many applications, e.g., to compile eager functional programs [1, 11]. One may wonder, however, about a connection between the introduction of continuations and the introduction of thunks. We have found that the call-by-name CPS transformation is the composition of the thunkier with the call-by-value CPS transformation: C n C v T This result sheds new light on C n conceptually distinct steps: it can be factored into two the suspension of argument evaluation (captured in T ); the linearization of function application actions to give the usual tailcalls of CPS terms (captured in C v ). This factorization is illustrated in Figures 1, 2, and 3. Figure 1 displays an uncurried version of the S combinator in Scheme. Figure 2 displays the

Thunks (continued) 3 ;;; [A * B -> C] * [A -> B] * A -> C (lambda (f g x) (f x (g x))) Figure 1. Uncurried S combinator in Scheme (lambda (f g x) ((f) (lambda () (x)) (lambda () ((g) (lambda () (x)))))) Figure 2. Thunkied counterpart of Figure 1 (lambda (k0) (k0 (lambda (f g x k1) (f (lambda (v0) (v0 (lambda (k2) (x k2)) (lambda (k3) (g (lambda (v1) (v1 (lambda (k4) (x k4)) k3)))) k1)))))) Figure 3. CBN CPS-counterpart of Figure 1 & CBV CPS-counterpart of Figure 2

4 Olivier Danvy & John Hatcli \thunkied" version of the uncurried S combinator. Figure 3 displays the CBN CPS counterpart of Figure 1 and the CBV CPS counterpart of Figure 2. The alert reader will identify many possible improvements to the expressions in Figures 2 and 3, all of them based on strictness properties. 1.1. Overview Section 2 presents the transformations T, C n, and C v. Section 3 formally establishes the connection between the two simulations T and C n. This is done by composing C v and T symbolically. We show that the composed transformation is identical to C n. Finally, Section 5 concludes. 2. The Program Transformations 2.1. The source language We consider the pure -calculus generalized with uncurried functions (as in Scheme). The BNF of -terms is dened as follows. e ::= x j (x 1 ; :::; x m ) : e j e 0 (e 1 ; :::; e m ) NB: For conciseness, we forgo using parentheses for unary functions: 2.2. A thunkier (x) : ::: = x : ::: f (x) = f x Terms in the source language can be \thunkied" automatically. Figure 4 gives the denition of the thunkier function T. This gure contains two essential points: identiers are mapped into parameterless applications; function arguments are mapped into parameterless procedures (thunks). Otherwise, T is structure preserving. In the extended version of this paper [5], we prove the correctness of T formally in that it eectively simulates call-by-name in a call-by-value setting.

Thunks (continued) 5 T [x] = x () T [ (x 1 ; :::; x m ) : e] = (x 1 ; :::; x m ) : T [e] T [e 0 (e 1 ; :::; e m )] = T [e 0 ] ( () : T [e 1 ]; ::: ; (): T [e m ]) Figure 4. Thunkier for simulating call-by-name under call-by-value 2.3. The call-by-name CPS transformation Figure 5 gives Plotkin's call-by-name CPS transformation C n extended to n-ary functions [9]. C n [x] = x C n [ (x 1 ; :::; x m ) : e] = : ( (x 1 ; :::; x m ; k) : C n [e] k) C n [e 0 (e 1 ; :::; e m )] = : C n [e 0 ] ( f : f (C n [e 1 ]; ::: ; C n [e m ]; )) Figure 5. Call-by-name CPS transformation of -terms The result of transforming a term e into CPS in an empty context is given by C n [e] ( a : a) 2.4. Call-by-value CPS transformation Figure 6 gives Plotkin's call-by-value CPS transformation C v extended to n-ary functions [9]. The result of transforming a term e into CPS in an empty context is given by C v [e] ( a : a)

6 Olivier Danvy & John Hatcli C v [x] = : x C v [ (x 1 ; :::; x m ) : e] = : ( (x 1 ; :::; x m ; k) : C v [e] k) C v [e 0 (e 1 ; :::; e m )] = : C v [e 0 ] ( f : C v [e 1 ] ( v 1 : ::: C v [e m ] ( v m : f (v 1 ; :::; v m ; )) ::: )) Figure 6. Call-by-value CPS transformation of -terms 3. Relating the Call-by-Name Simulations We now derive a new CPS transformation C t by composing C v with T symbolically. We will make use of the following lemma. Lemma 1 C v [ () : e i ] ( v i : :::) = ( v i : :::) (C v [e i ]) Proof: C v [ () : e i ] ( v i : :::) = ( v i : :::) ( k : C v [e i ] k) = ( v i : :::) (C v [e i ])...-reduction The -reduction step is valid because evaluating C v [e i ] terminates. 2 The derivation for each construct follows: C t [x] = C v T [x] = C v [x ()] = : C v [x] ( f : f ) = : x = x C t [ (x 1 ; :::; x m ) : e] = C v T [ (x 1 ; :::; x m ) : e] = C v [ (x 1 ; :::; x m ) : T [e]] = : ( (x 1 ; :::; x m ; k) : (C v T [e]) k) = : ( (x 1 ; :::; x m ; k) : C t [e] k)

Thunks (continued) 7 C t [e 0 (e 1 ; :::; e m )] = C v T [e 0 (e 1 ; :::; e m )] = C v [T [e 0 ] ( () : T [e 1 ]; ::: ; (): T [e m ])] = : (C v T [e 0 ]) ( f : C v [ () : T [e 1 ]] ( v 1 : ::: C v [ () : T [e m ]] ( v m : f (v 1 ; :::; v m ; )) ::: )) = : (C v T [e 0 ]) ( f : f (C v T [e 1 ]; ::: C v T [e m ]; ))...Lemma 1 and -reduction (m times) = : C t [e 0 ] ( f : f (C t [e 1 ]; ::: C t [e m ]; )) Theorem 1 C n C v T. Proof: By inspection, C n and C t are identical. When applied to identical terms they yield identical terms (modulo renaming). 2 4. Related Work Continuations are seeing a renewed interest today in their applications to logic, constructive mathematics, programming languages, and programming [6]. This paper builds on Plotkin's fundamental work on CPS [9]. Thunks were introduced to implement parameter transmission in Algol 60 [8]. Today they are a common programming tool to simulate call-by-name or call-by-need in a call-by-value setting [2]. To the best of our knowledge, no connection has been drawn between CPS and thunks so far. 5. Conclusion This paper stems from the serendipitous observation that the call-byname CPS transformation yields the same result as composing the call-byvalue CPS transformation with a thunkier. We have formalized and proven this coincidence. Let us now analyze it. The two avors of the CPS transformation (i.e., C v and C n ) act alike in that they both yield evaluation-order independent and continuation-passing -terms. The fact that each function is not only applied to its regular arguments, but also to a continuation a representation of how succeeding

8 Olivier Danvy & John Hatcli evaluation will use the value produced by the function leads to the tail-call property of CPS terms. C v and C n act dierently because of the dierent treatment of function arguments in the CBN and CBV strategies. CBV functions must be applied to values only and thus the CBV CPS transformation explicitly encodes the evaluation of all function arguments before the function is applied. Under CBN, there is no distinction between arbitrary argument expressions and values all evaluation of arguments is suspended. Thus each argument is explicitly encoded as a suspended computation that simply needs a continuation before evaluation can proceed. This leads to the evaluationstrategy independence of CPS terms. The crux of the matter is that the thunking transformation explicitly encodes the CBN treatment of arguments as suspensions into the term. Since a suspension (i.e., a thunk) coincides with the CBV notion of value, C v has nothing further to do than to add the tail-call property (via continuations). Conversely, the existence of a Direct Style transformer D v, i.e., of a program transformation that is the inverse of C v [4], suggests another way to discover the staging of C n. The idea is to map a n -term into CPS, and, based on the evaluation-order independence of CPS, map this term back to CBV direct style. This transformation naturally thunkies the original -term. C n = C v T ) D v C n = D v C v T ) D v C n = T We are currently investigating the connection of this staging with Filinski's duality between CBN and CBV [7]. Acknowledgements Thanks are due to Julia L. Lawall for commenting an earlier version of this paper.

Thunks (continued) 9 References [1] Andrew W. Appel. Compiling with Continuations. Cambridge University Press, 1992. [2] Adrienne Bloss, Paul Hudak, and Jonathan Young. Code optimization for lazy evaluation. LISP and Symbolic Computation, 1:147{164, 1988. [3] William Clinger and Jonathan Rees (editors). Revised 4 report on the algorithmic language Scheme. LISP Pointers, IV(3):1{55, July-September 1991. [4] Olivier Danvy. Back to direct style. In Bernd Krieg-Bruckner, editor, Proceedings of the Fourth European Symposium on Programming, number 582 in Lecture Notes in Computer Science, pages 130{150, Rennes, France, February 1992. [5] Olivier Danvy and John Hatcli. Thunks (continued). Technical Report CIS-92-28, Kansas State University, Manhattan, Kansas, 1992. [6] Olivier Danvy and Carolyn L. Talcott, editors. Proceedings of the ACM SIGPLAN Workshop on Continuations, Technical report STAN-CS-92-1426, Stanford University, San Francisco, California, June 1992. [7] Andrzej Filinski. Declarative continuations: An investigation of duality in programming language semantics. In David H. Pitt et al., editors, Category Theory and Computer Science, number 389 in Lecture Notes in Computer Science, pages 224{249, Manchester, UK, September 1989. [8] Peter Z. Ingerman. Thunks, a way of compiling procedure statements with some comments on procedure declarations. Communications of the ACM, 4(1):55{58, 1961. [9] Gordon D. Plotkin. Call-by-name, call-by-value and the -calculus. Theoretical Computer Science, 1:125{159, 1975. [10] John C. Reynolds. Denitional interpreters for higher-order programming languages. In Proceedings of 25th ACM National Conference, pages 717{740, Boston, 1972. [11] Guy L. Steele Jr. Rabbit: A compiler for Scheme. Technical Report AI-TR-474, Articial Intelligence Laboratory, Massachusetts Institute of Technology, Cambridge, Massachusetts, May 1978.