Back to Direct Style
暂无分享,去创建一个
This paper describes the transformation of I-terms from continuation-passing style (CPS) to direct style. This transformation is the left inverse of Plotkin’s left-to-right call-by-value CPS encoding for the pure I-calculus. Not all I-terms are CPS terms, and not all CPS terms encode a left-to-right call-byvalue evaluation. These CPS terms are characterized here; they can be mapped back to direct style. In addition, the two transformations-to continuation-passing style and to direct style-are factored using a language where all intermediate values are named and their computation is sequentialized. The issue of proper tail-recursion is also addressed. Much work has been devoted to transforming programs into continuationpassing style (CPS). (For a recent survey, see Talcott’s special issue on continuations [23].) In a CPS program, all procedures take an extra parameter-the continuation-which is a functional accumulator representing “the rest of the computation”. As a consequence, all calls are tail-calls. By contrast, programs that are not in CPS (e.g., programs before CPS transformation) are said to be in “direct style” (DS). Their procedure calls can occur anywhere (i.e., not necessarily in tail position). * An expanded version is available as Technical Report CIS-921, Department of Computing and Information Sciences, Kansas State University, Manhattan, KS 66506, USA. An earlier version of this paper appeared in: B. Krieg-Bruckner, ed., Proceedings of the 4th European Symposium on Programming, Rennes, France, Lecture Notes in Computer Science 582 (Springer, Berlin, 1992) 130-150. This project was initiated during a summer visit to Xerox PARC in 1991 and carried out while the author was working at Kansas State University. This paper was completed during a spring visit to the School of Computer Science of Carnegie Mellon University in 1993, and expurgated while at Aarhus University. Part of this work was supported by NSF under grant CCR-9102625. **E-mail: danvy@daimi.aau.dk. 0167-6423/94/$07.00 @ 1994 Elsevier Science B.V. All rights reserved SSDIOl67-6423(94)00003-W 184 0. Danvy /Science of Computer Programming 22 (I 994) I83I95 In contrast to the work mentioned above, the present paper studies the transformation of CPS programs into DS programs. For brevity, only the pure (call-by-value, with left-to-right evaluation) L-calculus is considered. A more thorough study (i.e., addressing conditional expressions, primitive operations, block structure, and recursive definitions) is available in a technical report [ 5 1. 1. From direct style to continuation-passing style and back The BNF of the pure (direct-style) L-calculus reads as follows. r E DRoot -DS terms y-e= e . . e E DExp -DS expressions e::= eoei 1 t t E DTriv -DS trivial expressions t::=x 1iix.r x E Ide -identifiers Fig. 1 presents a one-pass continuation-passing style (CPS) transformer for the pure L-calculus with call-by-value evaluation from left to right. This transformer is an optimized version of Plotkin’s CPS transformer [ 191. It was derived in an earlier work [6] and is only rephrased slightly here to match the syntactic domains, The result of transforming a DS term r into CPS is given by Q-1. These equations can be read as a two-level specification d la Nielson and Nielson [ 171 and thus they can be transliterated in any functional-programming language. Operationally, the overlined 2s and @s correspond to functional C : DRoot --+ CRoot C[e]l = &k.CDExP[ej (1t.k @ t) CDExp : DExp + [CTriv -+ CExp] + CExp CDExp[[eoei~ic =CDEXP[eoJ (;ito.cDEyel] (;it1.(to@tl)@(~~.K@21))) cDEytn K = K@ (cDTriqt7j) CDTriv : DTriv --t CTriv coT”VuXj = x where k and the 21’s are fresh variables. Fig. 1. Call-by-value, left-to-right CPS transformation for the pure A-calculus. 0. Danvy/Science of Computer Programming 22 (1994) 183-195 185 l tCExP e l-CROot ;Ik.e kj FTC” t1 ; r, Cl tCTtiv to; to 50, v tCExpe t FCTrivt ; . if Pxp to t1 (hJ.f?) < k’=p k t Fig. 2. Occurrence conditions over formal parameters of continuations. The CPS transformation of Fig. 1 performs a particular tree traversal-postfix and left-to-right-yielding a flattened tree [4]. So detecting whether a CPS term encodes a call-by-value and left-to-right evaluation order amounts to parsing a string in reversed-Polish form: with a stack. This is done by scanning the CPS term with a push-down list r holding the formal parameters of continuations. . denotes the empty list. See Fig. 3 for an example. abstractions and applications in the translation program (and coincide with the so-called “administrative reductions” [ 19]), while the underlined &‘s and @‘s represent abstract-syntax constructors. 1 The corresponding BNF of CPS terms reads as follows. (NB: the original identifiers x coming from the DS term are distinguished from the fresh identifiers ‘u and k introduced by C. A single identifier k is sufficient.) r E CRoot -CPS terms r ::= J.k.e e E CExp -CPS (serious) expressions e ::= to tl (2zr.e) 1 k t t E CTriv -CPS trivial expressions t::= x 1 ;Ix.r 1 21 x E Ide -source identifiers v,k c Var -fresh variables The distinction between “serious” and “trivial” expressions is due to Reynolds [20]. Serious terms are passed a continuation-their evaluation may diverge. Trivial terms are passed to a continuation-their evaluation cannot diverge. A CPS term encodes an evaluation order-here call-by-value-but it also encodes a sequencing order-here from left to right. This sequencing order imposes occurrence conditions over the formal parameters of continuations. The conditions for left-to-right call-by-value are reproduced in Fig. 2. Transforming a DS term r with C yields a CPS term that satisfies the judgementions and applications in the translation program (and coincide with the so-called “administrative reductions” [ 19]), while the underlined &‘s and @‘s represent abstract-syntax constructors. 1 The corresponding BNF of CPS terms reads as follows. (NB: the original identifiers x coming from the DS term are distinguished from the fresh identifiers ‘u and k introduced by C. A single identifier k is sufficient.) r E CRoot -CPS terms r ::= J.k.e e E CExp -CPS (serious) expressions e ::= to tl (2zr.e) 1 k t t E CTriv -CPS trivial expressions t::= x 1 ;Ix.r 1 21 x E Ide -source identifiers v,k c Var -fresh variables The distinction between “serious” and “trivial” expressions is due to Reynolds [20]. Serious terms are passed a continuation-their evaluation may diverge. Trivial terms are passed to a continuation-their evaluation cannot diverge. A CPS term encodes an evaluation order-here call-by-value-but it also encodes a sequencing order-here from left to right. This sequencing order imposes occurrence conditions over the formal parameters of continuations. The conditions for left-to-right call-by-value are reproduced in Fig. 2. Transforming a DS term r with C yields a CPS term that satisfies the judgement
[1] Olivier Danvy,et al. Representing Control: a Study of the CPS Transformation , 1992, Mathematical Structures in Computer Science.
[2] Eugenio Moggi,et al. Notions of Computation and Monads , 1991, Inf. Comput..
[3] Julia L. Lawall,et al. Proofs by structural induction using partial evaluation , 1993, PEPM '93.
[4] John C. Reynolds,et al. Definitional Interpreters for Higher-Order Programming Languages , 1972, ACM '72.