Object Closure Conversion
Total Page:16
File Type:pdf, Size:1020Kb
p httpwwwelsevier nl lo ca te ent cs vol ume htm l pages View metadata, citation andURL similar papers at core.ac.uk brought to you by CORE provided by Elsevier - Publisher Connector Ob ject Closure Conversion Neal Glew Department of Computer Science Cornel l University Ithaca USA Abstract An integral part of implementing functional languages is closure conversionthe pro cess of converting co de with free variables into closed co de and auxiliary data structures Closure conversion has b een extensively studied in this context but also arises in languages with rstclass ob jects In fact one variantofJavas inner classes are an example of ob jects that need to b e closure converted and the transformation for converting these inner classes into Java Virtual Machine classes is an example of closure conversion This pap er argues that a direct formulation of ob ject closure conversion is in teresting and gives further insight into general closure conversion It presents a formal closureconversion translation for a secondorder ob ject language and proves it correct The translation and pro of generalise to other ob jectoriented languages and the pap er gives some examples to supp ort this statement Finally the pap er discusses the well known connection between function closures and singlemetho d ob jects This connection is formalised by showing that an enco ding of functions into ob jects ob ject closure conversion and various ob ject enco dings comp ose to givevarious closureconversion translations for functions Intro duction The pro cess of closure conversion and the concept of closures are old and well studied ideas arising in any language with rstclass functions Briey if a function f nested within a function g has free variables that are dened in g the compiler will need to propagate the values of these variables from the time they are computed in g to the times at which f executes The usual solution is to compile functions to closures whichare data structures that pair closed This pap er is based on work supp orted in part by the NSF grant CCR AFOSR grant F and ARPA RADC grant F Any opinions ndings and conclusions or recommendations expressed in this publication are those of the author and do not reect the views of these agencies c Published by Elsevier Science B V Open access under CC BY-NC-ND license. Glew co de with the values of free variables The application of a function f to an argument a b ecomes an expression that extracts the closed co de for f from the closure and then applies the co de to the original argument and a part of the closure containing the values of the free variables The necessity for closure conversion is not limited to functional languages In particular Abadi and Cardellis ob ject calculi AC have rstclass ob jects and an innernested ob jects metho ds could refer to variables dened in an outernested ob ject It might seem that ob ject closure conversion is less imp ortant to mainstream ob jectoriented languages so rst I shall argue that this is not the case One ob jection is that mainstream ob jectoriented lan guages are class based and b ecause classes are typically second class closure conversion is not needed However Java was recently extended with inner classes including a form that requires closure conversion In fact this form was intro duced to alleviate the tedium of manual closure conversion Another ob jection is that the combination of an ob ject enco ding and functional clo sure conversion gives ob ject closure conversion While this indirect approach is adequate the results of this pap er show that this approach misses many imp ortantpoints that a direct account exp oses First the indirect approach if implemented naively misses opp ortunities for sharing Since most ob ject enco dings pro duce separate functions for each metho d the metho ds of an ob ject will be closure converted separately and will not share their environments A direct approach naturally shares the environments Second the ob ject closureconversion translation presented here is simpler than typ ed functional closureconversion translations Ob jects combine co de and data in a single construct so the values of free variables are paired with closed co de simply by adding extra elds Instead of using existential typ es to hide the typ es of the environment the translation uses subsumption a natural feature of any ob ject calculus Third this pap er shows that functional closure conversion is equivalentto the comp osition of an enco ding of functions as ob jects ob ject closure conver sion and an ob ject enco ding This result formalises the well known connec tion between closures and single metho d ob jects We will see that standard choices in functional closure conversion corresp ond to choices in the ob ject closure conversion and the ob ject enco ding A di cult and still op en issue even in the functional literature is the correctness of closure conversion Minamide et al MMH discuss closure conversion for simplytyp ed and p olymorphicallytyp ed lamb da calculi They describ e b oth conversions as twostep translations and prove b oth typ e preser vation and op erational correctness Their notion of correctness is an observa tional equivalence dened inductively over source typ es and their correctness argument is a logicalrelations argument However this pro of do es not extend to recursive functions Morrisett and Harp er MH have used a similar tech nique but extended with an unwinding lemma to prove correct a number of Glew typ ed closureconversion translations for recursive functions Their unwinding lemma is proved by dening a mo del and proving the result there Unfortu nately it is di cult to dene mo dels for languages with recursive typ es and harder to prove results in mo dels of languages with state and advanced con trol features These pro ofs are unsatisfying since they do not scale well to real language features Steckler and Wand SW describ e an optimised closureconversion pro cess for a simple untyp ed lamb da calculus They also prove their analysis and transformation correct and this is a harder task than other pro ofs describ ed in this pap er as it shows correctness of the optimisations as well as the basic translation They avoid some of the problems of the ab ove pro ofs by carefully dening their source semantics and translation so that a simulation argument is p ossible Their real contribution however is the analysis and optimisation of known closures and the pro of that this optimisation is correct This pro of probably could be used in other settings As mentioned ab ove functional closure conversion is ob ject closure conversion comp osed with an ob ject en co ding Therefore the pro of of ob ject closure conversions correctness should b e simpler However it do es retain the key di culty that makes these pro ofs hard to construct Thus dening a direct ob ject closureconversion translation allows us fo cus on this essential problem This pap er makes three contributions it denes a direct ob ject closure conversion translation it proves this translation correct using syntactic meth ods that extend to recursive typ es and it relates ob ject closure conversion to functional closure conversion formalising the well known connection be tween closures and singlemetho d ob jects Because of space limitations the correctness theorem is stated here and the pro of app ears in a companion technical rep ort Gle First the basic ideas are explained in the setting of avery simple ob ject calculus The translation is formalised in Section for a secondorder ob ject calculus with metho d parameters describ ed in Section Some extensions of the translation to other language constructs are discussed in Section and the connection between closures and singlemetho d ob jects is shown in Section The Basic Idea This section describ es the basic idea of a direct ob ject closureconversion trans lation This translation will transform a simple ob ject calculus to itself taking arbitrary terms as input and pro ducing closed co de as output The syntax of the language is Typ es m f i i j j iIjJ Terms e b x j m x b f e j em j ef j e f e i i i i j j iIjJ The only form of typ e is the ob ject typ e m f which is for i i j j iIjJ Glew ob jects with metho ds m of typ e and elds f of typ e Ob jects are i i j j created by an ob ject constructor m x b f e The newly i i i i j j iIjJ created ob ject resp onds to metho d m by executing b with x b ound to the i i i ob ject and has e as its value for eld f Note that e is evaluated at the j j j time the ob ject is created and its free variables do not need to be closed over Metho d invo cation is written em eld selection ef and eld up date e f e I defer the formal semantics and typing rules to the next section For functions the variables that need to b e closed over are the free variables of the function For ob jects however the variables that need to be closed over are not the free variables of the ob ject but just the free variables of the ob jects metho ds Therefore I dene the notion of closure variables for ob ject constructors cv m x b f e fv b fx g i i i i j j iIjJ i i iI The goal of ob ject closure conversion is to eliminate all closure variables of all ob ject constructors that app ear in the program The idea b ehind ob ject closure conversion is simple In functional closure conversion closed co de is paired with the values of free variables Since ob jects already pair co de and data we simply add new elds to the ob ject to store the free variables