Typed Closure Conversion
Total Page:16
File Type:pdf, Size:1020Kb
Typ ed Closure Conversion Yasuhiko Minamide Greg Morrisett Rob ert Harp er Research Institute for Mathematical Sciences Scho ol of Computer Science Scho ol of Computer Science Kyoto University Carnegie Mellon University Carnegie Mellon University Kyoto 606{01, Japan Pittsburgh, PA 15213{3891 Pittsburgh, PA 15213{3891 [email protected] [email protected] [email protected] ronments and is thus exploited to go o d advantage byShao Abstract and App el [31] and Wand and Steckler [38]. We study the typing prop erties of closureconversion for Most accounts consider closure conversion as a transfor- simply-typ ed and p olymorphic -calculi . Unlikemostac- mation to untyped terms, irresp ective of whether or not the counts of closure conversion, which only treat the untyp ed source term is typ ed [35, 16, 2, 38]. This is adequate for com- -calculus, we translate well-typ ed source programs to well- pilers that make little or no use of typ es in the backendor typ ed target programs. This allows later compiler phases to at run time. However, when compiling typ ed languages, it is take advantage of typ es for representation analysis and tag- often advantageous to propagate typ e information through free garbage collection, and it facilitates correctness pro ofs. each stage of the compiler, and to make use of typ es at link Our account of closure conversion for the simply-typ ed lan- or even run time. For example, Leroy's representation analy- guage takes advantage of a simple mo del of ob jects bymap- sis [18, 32 ] uses typ es to determine pro cedure calling conven- ping closures to existentials. Closure conversion for the tions, and Ohori's record compilation [26] uses a representa- p olymorphic language requires additional typ e machinery, tion of typ es at run time to access comp onents of a record. namely translucency in the style of Harp er and Lillibri dg e's In current compilers, these phases must o ccur before closure mo dule calculus, to express the typ e of a closure. conversion b ecause the output of closure conversion is un- typ ed. Compilation strategies for p olymorphic languages, such as those prop osed by Morrison et al. [25] and Harp er 1 Intro duction and Morrisett [13], rely on analyzing typ es at run time to supp ort unboxed representations and non-parametric op er- Closureconversion [30, 35 , 6, 16, 15, 2, 38, 8] is a program ators, including printing and structural equality.Tag-free transformation that achieves a separation b etween co de and garbage collection [4, 37, 24 ] for b oth monomorphic and data. Functions with free variables are replaced bycode p olymorphic programming languages also relies up on typ es abstracted on an extra environment parameter. Free vari- at run time to determine the size and the p ointers of ob- ables in the b o dy of the function are replaced by references jects. To supp ort any of these implementation strategies, it to the environment. The abstracted co de is \partially ap- is necessary to propagate typ e information through closure plied" to an explicitly constructed environmentproviding conversion and into the generated co de. Consequently,the the bindings for these variables. This \partial application" purp ose of this pap er is to showhow closure conversion can of the co de to its environment is in fact susp ended until the b e formulated as a type-preserving transform. function is actually applied to its argument; the susp ended We are therefore interested in type-basedtransformations application is called a \closure", a data structure containing as a basis for compiling p olymorphic languages. The crucial pure co de and a representation of its environment. idea is to de ne a compiler as a series of transformations on A critical decision in closure conversion is the choice of b oth the program and its typ e, p ossibly relying on typ e in- representation of the environment as a data structure | formation to guide the transformation itself. Each stage of for example, whether to use a \ at", \linked", or hybrid the compiler is thus viewed as a typ e-preserving translation representation. This decision is in uenced by a desire to between typ ed intermediate languages. Examples of such minimize closure creation time, the space consumed byan translations are given by Leroy [18], Ohori [26], Harp er and environment, and the time to access a given variable in an Lillibri dge [9], and Harp er and Morrisett [13]. In addition environment [38, 31]. An imp ortant prop erty of closure con- to the practical advantages of propagating typ e information version is that the representation of the environmentispri- through the stages of a compiler, typ e-directed translation vate to the closure, and is not visible from the outside. This also facilitates correctness pro ofs by de ning the invariants a ords considerable exibili ty in the representation of envi- of the transformation as a typ e-indexed family of logical re- This researchwas p erformed while the author was visiting the lations [36, 7, 28, 33, 34]. Fox Pro ject at Carnegie Mellon University. We describ e closure conversion in two stages. The rst stage, abstract closureconversion,isa typ e-based transla- tion from the source language into a target language with To app ear POPL '96. explicit closures. The translation is describ ed as a deductive system in which the representation of the environmentmay be chosen indep endently for each closure. In this wayvari- ous environment representations, such as those used bythe p ossible to nd and eliminate compiler bugs since we can au- CAM [6] and the FAM [5], as wellashybrid strategies, such tomatically typ e-check the output of each compiler phase. as those suggested by Shao and App el [31] can b e explained Some compilers for ML based on representation analysis in a uniform framework. [18, 32] also propagate typ e information through closure The second stage, closurerepresentation,isa typ e-based conversion. However, their information is not enough to translation in which the implementation of closures is de- typ e-check the resulting programs b ecause p olymorphism termined. The main idea is to represent closures as objects is implemented by co ercions and all p olymorphic typ es are (in contrast to the prop osed representation of objects as clo- represented byasingletyp e. sures [29]). Following Pierce and Turner [27] we consider The remainder of this pap er is organized as follows: In ob jects to b e packages of existential typ e consisting of a sin- Section 2, we giveanoverview of closure conversion and gle metho d (the co de part of the closure) together with a the typing issues involved for the simply-typ ed -calculus. single instance variable (the environment part) whose typ e In Section 3, we provide the details of our typ e-preserving (the environment representation) is held abstract. This cap- transform for the simply-typ ed case. In Section 4, wegivean tures the critical \privacy" prop ertyofenvironment repre- overview of closure conversion and the typing issues involved sentations for closures. In the simply-typ ed case wemake for the predicative fragment of the p olymorphic -calculus. direct use of Pierce and Turner's mo del of ob jects. In the The formal development of this conversion is given in Section p olymorphic case wemust in addition exploit the notion of 5. translucency [10] (or manifest types [19]) to express the typ e of a p olymorphic closure. 2 Overview of Simply-Typ ed Closure Conversion The correctness of b oth the abstract closure conversion and the closure representation stages are proved using the The main ideas of closure conversion may b e illustrated by metho d of logical relations. The main idea is to de ne a considering the following ML program: typ e-indexed family of simulation relations that establish a let val x = 1 corresp ondence b etween the source and target terms of the val y = 2 translation. Once a suitable system of relations has b een val z = 3 de ned, it is relatively straightforward to proveby induction val f = w. x + y + w on the de nition of the compilation relation that the source in and target of the translation are related, from whichwemay f 100 conclude that a closed program and its compilation evaluate end to the same result. Due to a lack of space, the pro ofs of correctness are omitted here. However, full details are given The function f contains free variables x and y,butnotz. in the companion technical rep ort [22]. Wemay eliminate the references to these variables from the Closure conversion is discussed in descriptions of various body of f by abstracting on an environment env, and replac- functional language compilers [35, 16 , 3, 2 , 31]. It is closely ing x and y by references to the environment. In comp en- related to -lifting [14] in that it eliminates free variables in sation a suitable environmentcontaining the bindings for x the b o dies of -abstractions but di ers by making the rep- and y must b e passed to f b efore it is applied. This leads resentation of the environment explicit as a data structure. to the following translation: Making the environment explicit is imp ortant b ecause it ex- p oses environment construction and variable lo okup to an let val x = 1 optimizer. Furthermore, Shao and App el show that not all val y = 2 environment representations are \safe for space" [31], and val z = 3 thus cho osing a go o d environment representation is an im- val f = (env.