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

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 of its metho ds and access them through the self variable

For example the expression

apply  self me  self self apply     apply

  

where  me   is closure converted to

apply  self me  self self f apply   f  self    apply

   

This idea leads directly to the following syntaxdirected translation

jxj  x

jemj  jejm

jef j  jejf

je f  e j  je jf je j

   



  f  je jg  y jej  m  x b

i j j k k iIjJk n i i

i

where e  m  x b   f  e

i i i i j j iIjJ

cv e  fy y g

 n



b  jb jfy y  x g x g g

i  n i  i n

i

g are fresh

k



The indices i and j range over index sets I and J  For the purp oses of this pap er

these index sets are unordered and ob ject typ es and ob jects are considered equivalentupto

reordering Ordered index sets could also b e considered and the results apply in this case

also The translation dened preserves ordering 

Glew

The notation xfy  z g denotes captureavoiding substitution of z for y in

z  The translation is straightforward for all expression forms exception ob ject

constructors In particular notice that metho d invo cation the analogue of

function application has a trivial translation For ob ject constructors the

new ob ject has the same metho ds and elds as the original ob jects and some

extra elds g  There is one extra eld for each of the closure variables After

k

translating the metho d b o dies each closure variable is replaced by a selection

from the self variable of the eld that corresp onds to the closure variable

It is worth p ointing out that the typing translation is the identity In func

tional closure conversion a function typ e is translated into a more elab orate

typ e which usually employs an existential to hide the typ es of the closure

variables In this translation the new ob ject has the same metho ds and elds

with the same typ es and some extra elds So its principle typ e is a subtyp e

of the original ob jects typ e and subsumption is used to hide the typ es of

the closure variables As we shall see this simplicity is due to the fact that

this translation is only half of a functional closureconversion translation The

other half is an ob ject enco ding that do es considerable typ e level translation

and further term translation

Now I will formalise the developments of this section in a scaled up lan

guage This more complex language includes features needed for a comparison

with functional closure conversion in particular metho d parameters Also it

supp orts the claim that the ideas scale to real languages In particular the

scaled up language will include p olymorphism as past attempts to scale clo

sure conversion to include p olymorphism have encountered di culties

 The Ob ject Language

This section formalises an ob ject language The next section will dene a for

mal translation from this language to itself that takes arbitrary terms to closed

terms The language is a variantof Abadi and Cardellis secondorder ob ject

calculus AC with a distinction between metho ds and elds variances on

elds metho d parameters only unb ounded p olymorphism and no metho d

up date lifting the latter two restrictions will b e discussed in Section  The

syntax of the language is



j

j Typ es     j m s  f 

iIjJ i i j

j

Metho d Signature s      

Variances    j j

Terms e b  x j m  M  f  e j em e  j

i i j j iIjJ

ef j e f  e j b j e

 

Metho d Denition M  x x  x  b 

  n n

Glew



The notation X denotes a sequence of elements drawn from syntax category



X  For example  means    and x means x  x  

 n   n n



j

The typ es include typ e variables   ob ject typ es m  s  f   and

i i j iIjJ

j

p olymorphic typ es   Metho ds now take b oth typ e and value parame

ters so they have signatures s instead of typ es The signature  

 m

     sp ecies a metho ds that takes m typ e parameters  through

 n 

 and n value parameters of typ es  through  and pro duces a result of

m  n

typ e   Ob ject typ es also sp ecify variances  for elds A read only eld has

variance  a write only eld has variance  a read write eld has variance



The terms include variables x ob ject constructors m  M  f e 

i i j j iIjJ

metho d invo cation em e  eld selection ef  eld up date e f  e  typ e

 

abstraction band typ e application e  An ob ject constructor gives each

of its metho ds a metho d denition M  The metho d denition x 

 m

x  x  b takes typ e parameters  through  and value param

  n n  m

eters x through x of typ es  through  and executes b with x bound to

 n  n

the ob ject A metho d invo cation em e includes b oth the actual typ e ar

guments  and the actual value arguments e  Iintend a typ eerasure interpre

tation of p olymorphism Consequently b do es not susp end the execution

of b until typ e application but evaluates it immediately

There are several syntactic restrictions that simplify the technical devel

opment In particular the m in an ob ject typ e or an ob ject constructor must

i

be distinct and similarly for the eld names typ e parameters and value

parameters Syntactic ob jects are equal up to  equivalence

The op erational semantics app ears in Figure  The notation E hei denotes

the substitution of e for the unique hole hi in E  The semantics is a determin

istic left to right call by value context based reduction semantics Again

note that elds are evaluated to values at ob jectcreation time The notation



e means that e  v for some v  e means that e starts an innite reduction

sequence

The typing rules are standard and app ear in Figure  The calculus has full

breadth and depth subtyping metho ds are contravariant in their arguments

and covariant in their results and the depth subtyping of elds is determined



by their variance The notation  is used for an empty typing context

Note that applying a typing rule with  in a hyp othesis implicitly requires

  and similarly for value contexts

The typing rules are sound with resp ect to the op erational semantics this

prop erty can be proven by standard techniques Typ e soundness and several

related prop erties of the typing rules are used in the pro of of correctness

details app ear in the companion technical rep ort Gle 



Abadi and Cardelli AC  provide a description of variances and the rules for variance

subtyping

Glew

Syntax





Contexts E  hi j m  M  f  v f  E g  e j Em e j

i i iI

vm v E  e j Ef j Ef  e j vf  E j

E j E 

Values v w  m  M  f  v jv

i i j j iIjJ

Reduction rules

E h iE hei

Where v m  M  f  v and

i i j j iIjJ

e Side Conditions

vm  v v  bf x x   vv g k I M 

k  n k

x x  x  b

  n m

vf v k J

k k





v j  k

j

 

vf  w m  M  f  v k Jv 

k i i j iIjJ

j j



w j  k

w  w f   g

Fig  Op erational Semantics

The free variables of an expression are

fv x  fxg

S S

fv e  fv M  fv m  M  f  e  

j i i i j j iIjJ

j J iI

S

fv em e e   fv e fv e 

 n i

in

fv ef   fv e

fv e f  e   fv e  fv e 

   

fv b  fv b

fv e   fv e

fv x x  x  b    fv b fx x x g

  n n  n

The closure variables of an ob ject constructor are



cv m  M  f  e  fv M 

i i j j iIjJ i

iI

Again note that only the free variables of metho ds are included An ob ject

constructor is code closed if and only if it has no closure variables An arbitrary

expression is code closed if and only if every ob jectconstructor sub expression 

Glew

Typing Contexts

    where     are distinct

 n  n

  x  x  where x   x are distinct

  n n  n

Typ e well formedness typing context well formedness and subtyping

 

i

ftv     

   x   x      

  n n



i I   s  s

 i

i







j



j

j J     



j

j



j

 m s  f 

iI j J i i j

 

j

I I  J J 

   







j

 

j

 f 

 m s  m s  f 

iI j J j iI j J i i i j

   

j i

j

       

   

i i

 

                

   n  n

       

   

 

 f g  f g

 

 

 

           

   

Expression typing

  e     

  

x 

  e     x  



     M  s   e  

i i j j



 m s  f  

i i j iIjJ

j

  m  M  f  e  

i i j j iIjJ

  e m            e   f   g

 m  n i i i

  em  e e   f   g

 m  n



  e f 

 f g

  ef  

k



  e       f    e  

 

 f g

  e f  e  

 

  e    

 b  

  b     e  f   g

x x  x  b  

  n n

   x x  x  b      

  n n  n

Fig  Typing Rules

is co de closed

 Ob ject Closure Conversion

This section formalises the translation describ ed in Section for the ob ject

language in Section  Exactly the same ideas are used An ob ject constructor 

Glew

jxj  x

jem e e j  jejm je jje j

 n  n

jef j  jejf

je f  e j  je jf je j

   

jbj  jbj

je j  jej



jej  e

where e  m  M  f  e

i i j j iIjJ

 

e  m  M  f  je jg  y g  y

i j j   n n iIjJ

i

cv e  fy y g

 n



M  x  x  b 

i i i i i i i





 x  x  jb jfy y  x g x g g M

i i i i i  n i  i n i

i

g  g are fresh

 n

Fig  Ob ject Closure Conversion Translation

is extended with elds for its closure variables and metho d b o dies reference

these closure variables by eld selection of the self variable The typing transla

tion is the identity translation and the term translation is dened inductively

over the syntax of expressions and app ears in Figure 

 Observational Equivalence

An imp ortant asp ect of the correctness of the translation is that it preserves

the meaning of expressions There are a number of ways to dene notions

of meaning preservation Unfortunately the simplest of these simulation

arguments do es not hold for this language Consider the example given in

Section  The source term makes this transition

apply  self me  self self apply     apply

  

 me  self apply  self me  self self apply     apply  

   

But the translated term makes this transition

apply  self me  self self f apply   f  self   apply

   

 me  self self f apply  

 

f apply  self me  self self f apply   f  self  

   

The reduced source term is co de closed and translates to itself not the reduced

translated term In particular notice that the outer ob ject is part of the 

Glew

metho d body of me in the reduced source term but an extra eld in the

reduced target term In general closure conversion shifts free variables to

extra elds but do es not shift the values that get substituted for them to

extra elds In eect the environment is in the metho d bodies in the source

term but in extra elds in the target term While this do es not change the

b ehaviour of terms a simulation argument cannot prove correctness

Instead this pap er uses contextual equivalence Mor Plo  two terms

are equivalent if they are indistinguishable in any context of the language

Following standard practice termination b ehaviour is used as the primitive

notion of observable dierence The formal denition of contextual equiva

lence which I shall call observational equivalence requires the denition of

contexts

Contexts C are an extension of the syntax category e with holes hi Holes

may app ear in a number of places in C and within typ e or term variable

binders and these binders capture the free typ e and term variables of the

hole The notation C hei denotes the expression that results from replacing

the holes in C with e A context C is typ ed by the judgement   C 

 

 h   i and this holds exactly when   C   is derivable with

      

the extra rule       hi    Clearly if       e   then

         

  C hei   

  

Most previous work which considered untyp ed languages used an untyp ed

version of equivalence However closure conversion is not correct under this

version A translated ob ject constructor has extra elds so a context that se

lects these extra elds will for the source term get stuck and for the translated

term converge Therefore the correctness criteria must rule out runtime typ e

errors by using a typ ed version of equivalence A term and its translation will

b e equivalent at the typ e of the source term

Denition  Observational Equivalence





e and e are Kleene equivalent e  e  i

   

  e     e   e e   e e 

     



e and e are  observational ly equivalent at   e  e  i

     

  e      e   

 

C    C  h  iC he iC he i

 

The ab ove relations are equivalence relations and observational equivalence

is a congruence These prop erties are proven in the companion technical re

p ort Gle  as part of a basic theory of observational equivalence Some basic

prop erties are proven equivalence is preserved under equivalent substitutions

and under reduction and equivalence at a subtyp e is ner than equivalence

at a sup ertyp e Some principles for establishing equivalence are also proven



The third and fourth clauses are redundant in a deterministic language such as the one

of this pap er I state the more general denition to b e consistent with my scalability theme 

Glew

Firsttoshow arbitrary op en terms are equivalent it su ces to show that un

der all appropriate substitutions they are equivalent Second to show closed

terms are equivalent it su ces to show that the values they evaluate to are

equivalent

Third a coinduction principle is proven To motivate this principle con

sider proving v and v equivalent at m   Placing v and v in arbitrary

   

contexts results in arbitrary reductions but such reductions will not observe

a dierence unless it includes a metho d invo cation of m Thus most pre

vious work includes a theorem of the form that v and v are equivalent if

 

they are Kleene equivalent in all contexts of the form C hhimi However if

v m  e and v m  e this still requires reasoning ab out e and e in

     

arbitrary contexts My coinduction principle go es further and considers just

contexts of the form him It requires showing that v v  m   is a mem

 

ber of a set of triples that is closed under reduction For this example closed

under reduction means that e and e are either equivalent or that e e 

   

is another triple in the set In general the typ e at which v and v are to be

 

equivalent determines the contexts that need to be considered details are in

the companion technical rep ort Gle 

This coinduction principle is not enough Consider proving m  xb 



equivalent to m  xb  as part of a pro of by induction on the structure



of expressions Using the coinduction principle this requires showing that

b fx  m  xb  g for i f g are equivalent or in some set of triples

i i

The induction hyp othesis says that b and b are equivalent assuming x is

 

substituted with equivalent values but x is substituted by the values we are

trying to prove equivalent This is the fundamental problem with extending

the pro of of Minamide et al MMH to recursive functions Morrisett and

Harp er MH use an unwinding lemma to solve this problem The th

unwinding of m  xb  for i f g diverge when metho d m is invoked so

i

they are clearly equivalent The n thunwinding reduces to b with the nth

i

unwinding substituted for x so by induction on n we can establish that the

nth unwindings of the ob jects are equivalent for all n The unwinding lemma

then tells us that the ob jects themselves are equivalent The basic theory

mentioned ab ove proves an unwinding lemma for the ob ject language and a

more p owerful coinduction principle

 Correctness

There are three asp ects to correctness the translation pro duces co de closed

expressions preserves typ es and preserves the meaning of expressions

Theorem  Translation Correctness If   and   e   then



jej is code closed



  jej  



e and jej are  observational ly equivalent at  

Glew

The pro of app ears in the companion technical rep ort Gle  and is by in

duction on the structure of e or its typing derivation For all cases other than

ob ject constructors the pro of is straightforward and uses the congruence

prop erty of equivalence To prove op erational correctness for ob ject construc

tors requires two key lemmas The rst shows that an ob ject is equivalent to

itself with some extra elds Intuitively this is true b ecause the extra elds

are just carried around and contexts cannot see the extra elds at the original

typ e The pro of uses the coinduction principle in a straightforward manner

The other lemma shows that one metho d b o dy can b e replaced with its trans



lation The coinduction principle is used again The substitution y  x g

i

preserves equivalence b ecause x g  y  so one of the basic lemmas says that

i k k

x g and y are equivalent

i k k

A couple of p oints are worth mentioning First the pro of actually proves

full abstraction Full abstraction is the prop erty that the target language can

not distinguish the translation of equivalent source terms formally e 

  

e implies je j  je j This holds for ob ject closure conversion for if

    

e  e then je j e  e  je j

             

Second the pro of should extend to a number of other features Consider

recursive typ es For equirecursive typ es the denitions and pro ofs remain

unchanged For isorecursive typ es the denition of closed under reduction

needs to include unroll but otherwise the pro of go es through The pro of

should also easily extend to cover all of the features mentioned in the next

section thus supp orting my claim that the pro of really do es scale to real

languages

 Some Extensions

The translation scales to a number of other language features and variations

in the language semantics including an imp erativesemantics rightextension

subtyping integers pro ducts sums arrays recursive typ es and functions

Details app ear in the companion technical rep ort Gle 

On p olymorphism the translation already includes unbounded paramet

ric p olymorphism b oth as a separate construct and for metho ds Extending

this to ordinaryb ounded Fb ounded or matchingbased parametric p olymor

phism is straightforward It is worth noting that in implementing the ob ject

language after converting ob jects to records of functions and b efore lifting

these functions to the top level the free typ e variables must be closed over

using the ideas of Morrisett et al MWCG  If a typ epassing interpreta

tion is desired the translation must be changed to close over typ e variables

as well There are two approaches The rst uses the ideas of Crary Weirich

and Morrisett CWM to convert typ e variables to value variables and a

typ eerasure interpretation This pap ers translation would then close over

the value variables The other approach involves adding typ e elds to an ob

ject constructor to store the free typ e variables This would require a typ e 

Glew

system for ob jects with typ e elds which likely would involve the complexity

describ ed by Minamide et al MMH  Leroy Ler  and Harp er and Lillib

ridge HL  Otherwise I b elieve the translation would lo ok much the same

although the pro of would be considerably more complicated

The translation can also be extended to handle metho d up date eld ex

tension and metho d extension All of these require eld extension which

interacts p o orly with breadth subtyping Thus either a dierent op erational

semantics is required such as the dictionary semantics RS  or a more com

plicated typ e system such as describ ed by Fisher Fis  Details app ear in

the companion technical rep ort Gle 

Finally as well as extensions to other language features the translation

has variants that express other environment representations The translation

 

 

 jb fy  x g gj  jb jfy  x g g It could also be b of metho d bodies is b

i i i i

i i

whichwould result in a dierentenvironment representation The rst choice

leaves y through y free in all innernested ob ject constructors and so these

 p

variables will b e closed over in the inner ob jects resulting in a at environment

representation The second choice replaces the y in an innernested ob ject

p

constructor with a free reference to x which will b e closed over resulting in

i

a linked environment representation

 Closures and Functional Closure Conversion

We are nally in a p osition to compare ob ject closure conversion and func

tional closure conversion and to formalise the well known connection b etween

closures and singlemetho d ob jects This section will show this connection by

providing a translation from a typ ed lamb da calculus to the language of this

pap er and show that the enco ding comp osed with ob ject closure conversion

and some ob ject enco ding results in a functional closure conversion I will

demonstrate this for two particular ob ject enco dings showing their equiva

lence to a closure passing and environment passing style of functional closure

conversion

Consider a simplytyp ed lamb da calculus with recursive functions

Typ es    int j   

 

Expressions e b  x j i j x f x   b j e e

    

For the remainder of this section assume an ob ject language with integers

The lamb da calculus can be enco ded into the ob ject language as follows

jintj  int

j   j  apply  j j j j

    

Glew

jxj  x

jij  i

jx f x   bj  apply  f  x j jjbjj j

     

je e j  je japply  je j

   

If the ob ject closure conversion translation is comp osed with the ab ove

enco ding the combined rule for functions is

jx f x    bj  apply  f  x j jjbjfy  fg g g  y

     i i i i

where fv b ff x g  fy g

 i

Now consider converting the ob ject calculi back into a functional calculi





via an ob ject enco ding The notation hf  ei denotes a record with elds f

and values e ef denotes eld pro jection and e f  e denotes eld up date

 

First consider an enco ding based on the selfapplication semantics Kam 

Ignoring typing the interesting comp osed translation rules are

jx f x   bj  happly  f x jbjfy  fg gg  y i

    i i i i

where fv b ff x g  fy g

 i

je e j  let x  je j in xapply x je j

   

These rules are just functional closure conversion with a closurepassing style

and a at environment representation

Second consider Pierce and Turners ob ject enco ding PT  Ignoring

typing the interesting comp osed translation rules are

jx f x    bj  let x fcode fenv x 

   

let f  hhapply  fcode i fenv i in

jbjfy  fenv g g in

i i

hhapply  fcode i hg  y ii

i i

where fv b ff x g  fy g

 i

je e j  let x  je j in xapply x  je j

   

These rules are just functional closure conversion with an environmentpassing

style and a at environment representation

Thus we see that closures and single metho d ob jects are equivalent as

witnessed by the translation given at the b eginning of this section We also

see that functional closure conversion factors into this equivalence translation

ob ject closure conversion and an ob ject enco ding

Other schemes mentioned in Morrisett and Harp er MH could also be

describ ed as sp ecialised ob ject enco dings for singlemetho d ob jects comp osed

with ob ject closure conversion In fact the ideas of this section suggest a gen 

Glew

eral framework for explaining functional closure conversion as follows First

a closure language would be dened which would be the ob ject language of

this pap er restricted to single metho ds Second a closureconversion transla

tion would be dened on this language and various choices for environment

representation would be describ ed as variations on this translation Third

a closurerepresentation translation would be presented and various passing

styles would be describ ed as various closure representations This general

framework then allows Javas innerclass transformation to be explained as a

generalisation to a full ob ject language

 Related Work and Summary

Closure conversion has been studied extensively for functions AJHan 



KKR  Lan MMH MWCGRey SteSW  To the best of my

knowledge ob ject closure conversion has not b een describ ed b efore

The connection between closures and ob jects is well known and has b een

hinted at in the literature As far as I am aware this is the rst pap er to

formalise the connection explicitly Reddy Red discuss ob jects as closures

Minamide et al MMH informally discuss closures as ob jects but do not

formalise the connection Abadi and Cardelli AC give several versions

of an enco ding of functional calculi into ob ject calculi Their enco ding is

dierent from the one here as they do not consider metho d parameters Also

they do not talk at all ab out closure conversion nor make connections b etween

functional calculi and ob ject calculi at the level of closures

Correctness pro ofs for functional closure conversion are given by Minamide

et al MMH  Stekler and Wand SW  Morrisett and Harp er MH  and

p ossibly other authors Only the latter considers recursive functions and none

of them consider recursive typ es Observational equivalence has b een studied

and used to prove correctness in b oth functional settings eg Mor Plo

MST  and in ob ject settings eg GHL  My results use similar to ols

and similar pro of techniques further supp orting the claim that my pro of will

scale to real languages

This pap er has presented an ob ject language with rstclass ob jects and

a closureconversion translation for it This translation was used to show

a formal connection between closures and singlemetho d ob jects and that

functional closure conversion factors through ob ject closure conversion These

results lend further insightinto the general problem of closure conversion and

foundations for the implementation of ob jectoriented languages

References

AC Martn Abadi and Luca Cardelli A Theory Of Objects Springer

Verlag   

Glew

AJ Andrew App el and Trevor Jim Continuationpassing closurepassing

style In th ACM SIGPLANSIGACT Symposium on Principles of

Programming Languages pages    Austin Texas USA January

  

CWM  Karl Crary Stephanie Weirich and Greg Morrisett Intensional

p olymorphism in typ eerasure semantics In  ACM SIGPLAN

International Conference on Functional Programming pages  

Baltimore Maryland USA Septemb er  

Fis Kathleen Fisher Type Systems for objectoriented programming

languages PhD thesis Stanford University Stanford California  

USA  

GHL  Andrew Gordon Paul Hankin and Sren Lassen Compilation and

equivalence of imp erative ob jects Technical Rep ort RS  BRICS

Department of Computer Science University of Aarhus Ny Munkegade

building  DK Aarhus C Denmark December   URL

httpwwwbricsdkRS

Gle Neal Glew Ob ject closure conversion Technical Rep ort TR  

Department of Computer Science Cornell University   Upson Hall

Ithaca NY   USA August   Available at http

wwwcscornelleduglewpaperlisthtml

Han  J Hannan Atyp e system for closure conversion In The Workshop on

Types for Program Analysis  

HL  Rob ert Harp er and Mark Lillibridge A typ etheoretic approach to

higherorder mo dules with sharing In st ACM SIGPLANSIGACT

Symposium on Principles of Programming Languages pages   

Portland Oregon USA January  

Kam Samuel Kamin Inheritance in smalltalk A denotational denition

In th ACM SIGPLANSIGACT Symposium on Principles of

Programming Languages pages  San Diego CA USA January

 



KKR  David Kranz R Kelsey J Rees P R Hudak J Philbin and N Adams

ORBIT An optimizing compiler for Scheme In Proceedings of the ACM

SIGPLAN  Symposium on Compiler Construction pages   

June   

Lan  P J Landin The mechanical evaluation of expressions Computer

Journal     

Ler  Xavier Leroy Manifest typ es mo dules and separate compilation

In st ACM SIGPLANSIGACT Symposium on Principles of

Programming Languages pages   Portland OR USA January

  

Glew

MH Greg Morrisett and Rob ert Harp er Simplytyp ed closure conversion

Unpublished authors contact jgmcscornelledu April  

MMH  Yasuhiko Minamide Greg Morrisett and Rob ert Harp er Typ ed closure

conversion Technical Rep ort CMUCS  Scho ol of Computer

Science Carnegie Mellon University Pittsburgh PA   USA July

 

MMH Yasuhiko Minamide Greg Morrisett and Rob ert Harp er Typ ed

closure conversion In  rd ACM SIGPLANSIGACT Symposium on

Principles of Programming Languages pages   St Petersburg

January  

Mor  J Morris LambdaCalculus Models of Programming Languages PhD

thesis Massachuetts Institute of Technology  

MST Ian Mason Scott Smith and Carolyn Talcott From op erational

semantics to domain theory Information and Computation  

  

MWCG  Greg Morrisett David Walker Karl Crary and Neal Glew From

System F to typ ed assembly language In th ACM SIGPLAN

SIGACT Symposium on Principles of Programming Languages pages

  San Diego California USA January   ACM Press

Plo Gordon Plotkin LCF considered as a programming language

Theoretical Computer Science    

PT  Benjamin Pierce and David Turner Simple typ etheoretic foundations

for ob jectoriented programming Journal of Functional Programming

 April  

Red  Uday Reddy Ob jects as closures Abstract semantics of ob jectoriented

languages In ACM Symposium on LISP and Functional Programming

pages    ACM July  

Rey John Reynolds Denitional interpreters for higherorder programming

languages In Proceedings of the Annual ACM Conference pages 

  

RS  Jon Riecke and Christopher Stone Privacy via subsumption

In th International Workshop on Foundations of Object Oriented

Programming Languages San Diego CA USA January  

Ste Guy Steele Jr Rabbit A compiler for Scheme Masters thesis MIT

 

SW Paul Steckler and Mitchell Wand Lightweight closure conversion ACM

Transactions on Progamming Languages and Systems pages  

January