Electronic Notes in Theoretical 

URL httpwwwelsevier nlloc ateen tcsvo lume html  pages

Typ e Inference for Recursively Constrained

Typ es and its Application to OOP

 

Jonathan Eifrig Scott Smith Valery Trifonov

Department of Computer Science

The Johns Hopkins University

Baltimore Maryland 

feifrig scott trifonovgcsjhuedu

Abstract

We dene a p owerful typ e inference mechanism with application to ob jectoriented

programming The typ es inferred are recursively constrained typ es typ es that come

with a system of constraints These typ es may b e viewed as generalizations of recur

sivetyp es and Fb ounded p olymorphic typ es the forms of typ e that are necessary

to prop erly enco de ob ject typings The base language we study ISo op  incorp o

rates state and records the two features critical to enco de ob jects in a nonob ject

oriented language Soundness and completeness of the typ e inference algorithm are

established by op erational means Our metho d for establishing these prop erties is

somewhat novel We illustrate how the algorithm may b e fruitfully applied to infer

typ es of ob jectoriented programs

Intro duction

This pap er addresses the problem of designing an ob jectoriented program

ming language with an eectivetyp e inference mechanism Recently devel

op ed programming languages including Standard ML and Haskell incorp orate

typ e inference as a core comp onent of the language However typ e infer

ence has yet to achieve practical application to ob jectoriented programming

languages

We strongly feel the core typ e features necessary to mo del ob jectoriented

programming with typ e inference include a notion of  and a no

tion of recursively constrained p olymorphism a generalization of Fb ounded

p olymorphism   

Recursively constrained typ es are typ es of the form  n C  with  n 

reading where C is a of typ e constraints of the form    p ossibly



containing free typ e variables These constraints maybe recursive in that a

Partially supp orted by NSF grants CCR and CCR



Partially supp orted byAFOSR grant F

c

 Elsevier Science B V

Eifrig Smith and Trifonov

variable t could o ccur free in b oth  and   The recursive constraint set



ft  Nat t t t  Natg expresses t t  Nat so recursively constrained

typ es subsume recursivetyp es We will use rctype to abbreviate recursively

constrained typ e

Polymorphic rc typ es are typ es t t  n C where constraints  

n 

in C maycontain typ e variables t t free Polymorphic rc typ es generalize

n

the more wellknown b ounded typ es  t   in several ways First

they are recursive so t could o ccur free in  this is not allowed in b ounded

typ es Typ es with t o ccurring free in  are the socalled Fb ounded typ es

  Polymorphic rc typ es generalize Fb ounded typ es byallowing more than

one upp er b ound on a typ e variable as well as allowing multiple lowerb ound

constraints  t This generalized form of p olymorphic typeisvery useful

in typing ob jectoriented programs that are otherwise untypable irresp ective

of the question of typ e inference An example of such a program is given in

Section b elow

It is not dicult to see how rc p olymorphism is useful in typing classes

and ob jects for it is at least as useful as Fb ounded p olymorphism Classes

mayhave socalled binary metho ds that refer to the typ e of ob jects of their

own class for instance an ob ject with an equal metho d takes as parameter

another ob ject of its own typ e Thus a selftyp e is needed And this self

typ e needs to b e op enended since a class may b e extended wewishthetyp e

of self to b e an ob ject with all the metho ds currently dened and p ossibly

additional ones Polymorphic rc typ es capture this notion by constraining

the p olymorphic selftyp e t to include the current metho ds for instance

t  nft equal  t  Bo ol g

Binary metho ds haveproven very dicult to typ e in a general way it has

even b een suggested that they b e disallowed

One way to understand the usefulness of lower b ounds  t in rc typ es

are as generalizations of recursivetyp es It is p ossible to write an rc typ e

t nf t  g where lower b ound  diers from upp er b ound 

 

it is a recursivetyp e if    These generalized forms are useful as



intermediate results pro duced during the typ e inference pro cess as partial

forms of recursivetyp es During the typ e inference pro cess constraints are

accumulated on typ es in a b ottomup fashion and so typ es at the leaves

of typing pro ofs have small constraint sets and have fat constraint sets at

the ro ot The lower b ound  constrains the output of the typ e what

prop erties ob jects of typ e must have if an ob ject of typ e is used ie

passed to a function of typ e    an additional upp erb ound constraint

t  will b e placed on the typ e bythetyp e inference mechanism and

this could only b e contradictory if   which follows by transitivitywas

contradictory The upp er b ound is the dual of this constraining the input

of the typ e what functions of typ e t   must do

The presence of multiple upp erb ound constraints or multiple lowerb ound

constraints can b e understo o d as a restricted form of union and intersection

typ e f t  tg would b e equivalentto f   tg if there were union 

Eifrig Smith and Trifonov

typ es    in the language a dual relationship exists b etween intersections

and upp er b ounds We b elieve general union and intersection typ es cause to o

many problems to b e worthwhile but this implicit restricted form is quite

natural

In this pap er wedevelop a typ e inference algorithm for the ISo op lan

guage Inference Semantics of OOP ISo op is not an ob jectoriented lan

guage however it has an expressive enough typ e system so that typ ed OOP

may b e eectively enco ded within ISo op  We take a translational approach

b ecause we nd the factoring to help clarify ambiguities however there is also

merit in studying languages where ob jects themselves are primitive  and

the concepts herein should eventually b e recast as primitiveobjecttypings

ISo op s typ e system contains b oth subtyping and p olymorphic rc typ es We

infer shallow p olymorphic rc typ es at letexpressions as in the HindleyMilner

algorithm  In addition the underlying language includes records and a

notion of state for with these features it is p ossible to obtain an eective

enco ding of ob jectoriented programming Records are needed so record sub

typing can b e used to mo del ob ject subtyping  Without state the critical

stateholding prop erty of ob jects is lost 

Our approach to establishing the soundness of constrained typ e inference

diers from other work in the literature In other approaches eg 

  a metho d is given that either pro duces a satisfying assignmenttothe

constraints and thus establishes their consistency or establishes that no such

solution exists and the constraints are thus inconsistent In our approach an

rc typ es constraint system is considered consistent if it do es not contain

anyobvious contradictions suchas Nat Bo ol Weshow this view is sound

without ever showing the consistent constraint systems have solutions In

stead we directly establish a sub jectreduction prop ertyover a pro of of typing

with consistent rc typ es at each no de   We b elieve the standard

metho d of nding solutions to the constraint sets can b e overly restrictive

for it forces one to havearich enough typ e language or typ e mo del that can

express the solutions as typ es or sets In our language for instance we exp ect

general union and intersection typ es would b e required to express the solution

of constraints as typ es but we do not wish to pay the p enaltyofhaving these

typ es in our language

Wealsotake a more primitive approach to establishing the completeness of

typ e inference ie that all typable programs will successfully have some typ e

inferred by the typ e inference algorithm We rst dene a restricted set of

typing rules the inference rules for whichtyping derivations are deterministic

Then these rules are shown equivalent in strength to the general form of rules

without recourse to a principal typ es prop erty

 Related Work

Anumber of typ e inference systems have b een develop ed that b ear on the typ e

inference problem for OOPPap ers of Reynolds  Cardelli  and Mitchell

 are foundational pap ers in the eld that develop the basic concepts of 

Eifrig Smith and Trifonov

constraints and subtyping Many pap ers have b een written since we fo cus on

the more recentwork the most relevanttoours

Kaes  develops a typ e inference algorithm for a language containing

p olymorphic and recursivetyp es and typ e constraints This work incorp orates

subtyping constraints recursivetyp es and p olymorphism Kaes writes so

called constrained typ es  jC in close analogy to our rc typ es  n C  This

approach cannot solve general recursive constraints t  generates a non

terminating unication problem in his system if t o ccurs free in   while our

approach can handle such constraints without diculty He do es allowa

xing of such a constraintby replacing it with a recursivetyp e t butat

the cost of an imp ortant loss of generalityKaestakes the standard approach

to constraint consistencyby pro ducing a solution to the constraints He also

intends to mo del overloading not record subtyping his system has no record

typ es Sekiguchi and Yonezawa   take an approach similar to Kaes but

interpret as subtyping on record typ es making it more directly applicable

to ob jectoriented programming

Palsb erg Schwartzbach et al have written a numb er of pap ers concerning

typ e inference for ob jects    The main feature of their work is they

do not take the HindleyMilner approachtotyp e inference Instead their

inference algorithm uses ow analysis to generate a set of constraints ab out a

program and then applies another algorithm to come up with a solution to

these constraints if it exists Their work represents the current stateoftheart

in having a practical typ e inference algorithm for ob jectoriented programming

languages Other advantages of their approach include asymptotically ecient

inference algorithms and named class typ es Their system however has no

p olymorphism and they take a co deexpansion view of inheritance requiring

retyp echecking with each class extension This lack of p olymorphism has

b een partially addressed byPlevyak and Chien 

Our work is closest to that of Aiken and Wimmers  They develop a typ e

system with subtyping union and intersection typ es and a form of p olymor

phic typ e similar to p olymorphic rc typ es They prove soundness using the

ideal mo del   As with the previously mentioned researchers they havean

algorithm that pro duces a satisfying assignment to the toplevel constraints to

establish consistency of a constraint set The satisfying assignment they pro

duce is an ideal in the ideal mo del Wehave no union intersection or negation

typ es These typ es prove problematic in their system and they are in fact

unnecessary for typ e inference  if they are not used in the typ es of atomic

constructs they are not generated by the inference algorithm provided multi

ple upp er and lower b ounds to the same variable are allowed as we do Aiken

and Wimmers have not addressed the problem of using their system for typing

ob jectoriented programs their language lacks imp ortant features necessary

for the enco ding of ob jects In particular their language is a functional lan

guage without records The ideal mo del cannot mo del languages with state

so their approachwould not extend to a language with state Aiken has imple

mented the typ e inference algorithm  and this implemented system has an

optimized inference algorithm and an implementation of extensible records 

Eifrig Smith and Trifonov

Var  x

Num  n   j  j  j 

Val  v  x j n j x e j l  v  j  l v  j l  v

Exp  e  v j eej let x  e in e j l  e  j el j  l e  j l  e

Fig  Syntax of the ISo op language

Enco ding ob jectoriented features within a more basic language is one p os

sible approachtohow ob jectoriented programming should b e done  We

could take a similar approachby programming in an ob jectoriented style via

the enco ding of ob jects in ISo op that wegive in Section  Remygives a

collection of extensions to ML that allow OOP to b e enco ded Remyisthe

only author amongst of those previously discussed who has a pro of of sound

ness of his system in the presence of reference cells His enco ding is missing a

notion of subtyping and thus lacks the core feature of ob ject lifting allowing

sub class ob jects to b e implicitly co erced to b e sup erclass ob jects Instead co

ercion functions must b e explicitly supplied Remys enco ding is more ecient

than the enco ding we use each ob ject creation in our enco ding entails forming

closures for each metho d of the ob ject If our language weretobeusedas

a primitive OOP language some more ecient ob ject representations would

need to b e develop ed Remys system also has a notion of extensible record

whichwe exp ect will b e useful for enco ding delegationstyle ob jectoriented

programming

 Outline

In Section  we present ISo op and its op erational semantics Section 

presents the ISo op typ e system sketches of the pro ofs of sub ject reduction

and typ e inference app ear in Section  Then to showhow OOP can b e faith

fully enco ded an extended example is worked in Section  This example also

serves to illustrate the p ower of the typ e inference system Wedraw some

nal conclusions in Section 

 The ISo op Language

We b egin by dening the ISo op language which is roughly callbyvalue PCF

with records variants reference cells and letexpressions see Figure 

l  v is shorthand for l  v   l  v for some The vector notation

k k

k l  v is shorthand for the same and indicates that i will range over the

i i

elements of the vector The set B fsucc pred is zero ref   setg Var

contains the names of builtin primitive functions on numb ers and reference

cells Variants are dual to records the injection l  e tags the value of e with

l e  similar to the Standard ML fn construct can lab el l  and the match 

b e applied to a tagged value to extract it The b o oleans and conditional are

Eifrig Smith and Trifonov

derived from variants true and false are dened as true  and false 

resp ectively and if e then e else e stands for  true  e false  e  e

 

weuse  e to denote x e for some x not free in e

A store ranged over by s is a nite mapping from variables to values

A conguration hs ei is a pair of a store and an expression Computation

is dened via a singlestep relation  between congurations A reduction

context R is an expression with a hole  in it into which one mayputa

sub expression via Re Reduction contexts serve to isolate the next step of

computation to b e p erformedit is always in the hole

Denition  A reduction context is dened inductively in Figure 

R  jRej vRj let x  R in e

j l  v  l  v l  R l  e  l  e  j Rl

i i i i i k k

j  l v  l v l R l e  l e  j l  R

i i i i i k k

Fig  ISo op reduction contexts

Denition   is the least relation on congurations satisfying the con

ditions shown in Figure  where

ee x is the capturefree substitution of e for x in e

 x  v  is the map dened only on x with result v 

f jjg is the functional extension of f by g 

hs Rx e v i hs Revxi

hs Rlet x  v in ei hs Revxi

hs Rsucc ni hs Rn i if n n 

hs Rpred ni hs Rn i if n n 

hs Ris zero  i hs Rtruei

hs Ris zero ni hs Rfalsei if n 

hs R  l  v  l i hs Rv i

hs R  l v   l  v i hs Rvvi

hs Rref v i hsjj x  v Rai x Dom s B 

hs Rxi hs Rsxi x Dom s

hs Rset cell  xval  v i hsjj x  v Rv i x Dom s

Fig  The singlestep computation relation

Eifrig Smith and Trifonov

Here is a sample execution

h  xsucc xeld eld  ref i

 h y   x succ xeld eld  y i

 h y   succ  eld  y eldi

 h y   succ y i

 h y   succ i

 h y   i

Lemma 

ei and hs ei hs e i then there i  is deterministic if hs ei hs

is a uniform renaming of variables in s and e to those in s and e

respectively

ii  is compositional if hs ei hs eithen hs Rei hs Re i for

every reduction context R

 ISo op Typ es

The monomorphic typ es of the language are

TyVar    t j u

l    j  l    j  ref Typ     j Nat j   j

def

where t ranges over the applicative typ e variables AppTyVar ft  t g



def

and u ranges over the imperative ones ImpTyVar fu  u g This



division of variables into two classes is similar to that of Standard ML The

set of free typ e variables in a typ e  is FTV    is imperative if FTV  

ImpTyVar 

A typeconstraint is a subtyping assertion b etween two monomorphic

typ es written   We will require all sets of constraints used in typ es



and rules to b e implicitly closed under obvious laws

Denition  Constraint System A set of typ e constraints C is closed

i

i If   C and   C  then   C 

   

ii If     C then f    g C 

 

 

l    l   l g fl g then f   C and f g C  iii If

i i j i j j

j j

iv If  l     l   l g fl g then f   C and f g C 

i i j i j i

j i

v If  ref  ref C then f    g C 

  

A closed set of constraints is a constraint system

WeletC range over implicitly closed constraint systems and thus will

b e careful to make sure any new set of constraints we form is closed The 

Eifrig Smith and Trifonov

closed union of sets of constraints is denoted by C  C  an op eration that by



insp ection can b e seen to b e asso ciative

Denition  Constraint Consistency A constraint   is consis



tent if

i  TyVar or  TyVar



ii   Nator  ref and   refor   and

 



   for some     



   

l     l   and flgfl g or iii 



iv   l      l   and flg fl g



Otherwise a constraintisinconsistent

For example Nat tNat and tref m  Nat  are inconsistent con

straints while t tNat t uandu Nat are each consistent A con

straint system is consistent if all the constraints in the system are consistent

The rules will require all constraint systems to implicitly b e consistent

The typ e system assigns ISo op expressions rctypes of the form

  n C

to indicate an expression of typ e  which is constrained by the constraints in

C  Since the rules implicitly require C to b e consistent it makes sense to view

as a typ e and to write C on the right side of the turnstile as part of the

typ e

We dene the following notion of subtypingonrctyp es

Denition  Subtyping rc Typ es  n C  n C provided that C is

consistentand C f  g C 

Stronger notions of subtyping could b e dened but for our purp oses this

denition suces The typ e schemes  are as follows

    j

Note that since  n C can contain an arbitrary collection of constraints C 

shallow Fb ounded p olymorphic typ es are a sp ecial case of these p olymorphic

rc typ es

 ISoop Typing Rules

Before giving the rules we describ e notation used in the rules Notation used in

sequent judgements includes the following A type environment A is a mapping

from variables to typ e schemes we use the more intuitive notation  x   in

stead of  x    Given a typ e environment A the pro of system assigns to an

expression e arctyp e  n C  written as the type judgement A  e   n C un

der the condition that C is consistent as mentioned previously all constraint

sets C app earing in the rules implicitly must b e consistent we o ccasionally 

Eifrig Smith and Trifonov

A  e  

Sub  Num

A  e A  n Nat n

A  e    n C  e  n C Ajj x   e  n C

 

App  Abs

A  x e   n C A  e e  n C  C

 

  is a substitution on fg Ax  Ax 

PVar Var

A  x  n A  x

A  A  e  l   n C e  n C

i i i

Record Sel

U

C A  el  n C A   l  e   l   n

i i i i i

i

A  A  e  n C e   n C

i i i

Match  Inj

U

C A  l  e  l   n C A   l  e   l   n

i i i i i

i

A  e  n C Ajj x    n C  e  n C

Let

A  let x  e in e  n C  C





if e is expansive then AppClos  n C A

g where f





else Clos  n C A

Fig  Typing rules of ISo op 

may write A  e   n C e   n C to indicate several typ e judge

  

ments provable in the same environment Programs are typ echecked in the

initial environment A assigning the following typ e schemes to the builtins



zero  NatBo ol  A succ  NatNat pred  NatNat is



ref  u uu ref   t treft set  t cell  trefval  t t 

where Bo ol stands for the typ e  true false Asubstitution on f g is

amap TyVar  Typ whichistheidentityon TyVar nf  g and maps

ImpTyVar to imp erativetyp es a renaming of f g is a substitution on fg

with co dom  TyVar  An expression is expansive if and only if it is not a

value following Tofte   weformtyp e schemes by making the sets of typ e

variables we generalize over dep endent on the expansiveness of the expression

The denitions of these sets are

Clos  n C A FTV   FTV C  n FTV A

AppClos  n C A Clos  n C A  AppTyVar

where the functionalityof FTV is extended as usual to constraint systems rc

typ es typ e schemes and typ e environments

The typing rules for ISo op are given in Figure  Most of the rules have

obvious relation to those of standard systems with subtyping and records as

Eifrig Smith and Trifonov

in Toftes system   the typing of ref intro duces imp erativetyp es The main

dierence is the addition of constraints as part of typ es the asso ciated sub

sumption rule on these typ es and the way consistent constraints accumulate

from the leaves to the ro ot of a typing pro of It is imp ortant to observethat

consistency of constraints is implicitly enforced byeach rule Other presenta

tions of constrained typ e systems  do not require lo cal consistencyso

the constraints in the rules havebotha hyp othetical and assertional comp o

nent They are hyp othetical in that they may b e inconsistent and they are

assertional in that they assert prop erties of the typ e if they are consistent

For this reason they write C on the left of the turnstile and p erform some

toplevel consistency check b efore a proved typing is true Since constraints

are never inconsistent in our rules wehavenohyp othetical comp onentand

constraints are thus written on the righthand side of the turnstile

Some justication is required for the Let rule in which the constraint

system of the let expression contains not only the constraints in C  necessary

for typing its b o dy but also those in C accumulated for the typ e of the

bound variable Leaving the latter constraints out as  do but corrected

in  results in a system unsound with resp ect to the standard callbyvalue

semantics of the let expression C may contain constraints on typ e variables

free in the environment and their omission may lead to accepting programs

which get stuck while evaluating the expression assigned to the b ound variable

As an example consider the expression

x let y  x in succ x

By rules PVarVar Sub and App the constraint system C of the rc typ e

of x contains   ref for some typ e   where  is the typ e asso ciated with

x by the rule Abs This constraint will lead to inconsistency when combined

with the constraint Nat  at the outermost rule of the typing pro of App

If it were omitted from the constraint system of the let the other constraint

on   namely  Nat from the b o dy succ xwould not cause an inconsistency

and the program would typ echeck however its execution obviously leads to

the stuck state h  let y   in succ i

While the typ e language do es not have recursivetyp es x x x can b e given

the rc typ e t t nft t t gWedonothave a b ottom typ e but its

 

p ositive o ccurrences maybesimulated by an unconstrained typ e variable eg

 x x x  x x x has the rc typ e

t nft t t  t t t g

  

An unconstrained variable can also b e used instead of a top typ e in negative

p ositions Positive o ccurrences of top maybesimulated byoverconstraining

from b elow

A  if true then else   t nfNat t  tg



This constraint system is consistent Note that not all typable programs are

of this particular top typ e but they are provably of typ e t nfNat t  

Eifrig Smith and Trifonov

tgC for some C and fresh t by a single use of Sub Similarly overconstraining

from ab oveachieves the eect of b ottom in negative p ositions

 Sub ject Reduction Soundness and Typ e Inference

We prove soundness of the typ e system by demonstrating a sub ject reduction

prop erty First we strengthen the Let rule of the system to

  n C   e   n C  is a renaming of fg A  e   n C Ajj x  

Let

A  let x  e in e   n C  C





if e is expansivethen AppClos  n C A

g where f





else Clos  n C A

Obviouslyany use of the original Let rule can b e trivially transformed

into a use of the stronger rule bycho osing to b e the identity renaming

This renaming do es not add anypower to the typing system any program

that is typable with the stronger Let rule is also typable with the original it

is intro duced only to avoid certain technical complications which arise during

reductions within a let expression

Next we extend the notion of typing to congurations

Denition  A hs ei   n C if and only if

 A  e   n C

 Dom A Dom A  Dom s Dom A   Dom s andA agrees

 

with A on Dom A 

 

 for each x Dom swehave Ax  ref and A  sx n C for

x x x

some  and C C 

x x

Theorem  Sub ject Reduction If A hs ei   then either e Val

or else hs ei hs ei and there exists an environment A such that A 

hs ei  

Wepresent only a sketch of the pro of in this abbreviated version The

pro of pro ceeds in the standard fashion given a conguration and a pro of of

its typability p erform one step of computation and transform the original

typing pro of into a pro of for the new conguration The interaction b etween

letp olymorphism and reference cells is known to cause signicant diculty

  our approach to this problem derives from  avoiding Toftes complex

greatest xedp oint construction

The dierences b etween our pro of and that of  result from the con

straint systems of rc typ es and p olymorphic rc typ es Each step of computa

tion is accompanied by a pro of transformation that pushes constraints present

near the top of the pro of tree towards the leaves The complications of the

pro of arise when these constraints are pushed through uses of the Let rule 

Eifrig Smith and Trifonov

demonstrating that the typ e generalizations p erformed in the initial applica

tion of the rule remain valid is nontrivial

This pushing of constraints from the ro ot of the typing pro of towards the

leaves during reduction can b e considered a lazy approach to pro of canoni

calization An alternative approachwould b e to regularize the initial typing

pro of of a program to canonical form by pushing all of the constraints present

at the ro ot to the leaves b efore p erforming any computation This would re

sult in a more straightforward sub ject reduction pro of at the exp ense of a

more complicated pro of canonicalization lemma

The soundness of the typ e system is a corollary of the Sub ject Reduction

theorem

Theorem  Soundness If A  e   then either e diverges or e com



putes to a value

Pro of By induction on the length of computation using Theorem 

Note wehavethus proved soundness of the constrained typ e system with

out ever having shown the systems of constraints have a solution

 Type Inference

Wenow dene the typ e inference algorithm and prove it is complete ie if a

program has a typ e derivation the inference algorithm will infer a typ e for it

The strategy wetake to reach this desired outcome is the following

 Dene a new set of rules the inference rules for whichtyping derivations

are deterministic

 Prove the inference rules are equivalent in strength to the general rules

we had b een using previously

The inference rules app ear in Figure 

Theorem  For al l terms e and environments Aitisdecidable whether

there exists a such that A  e  

inf

Pro of Sketch By insp ection of the rules there is only one rule for typing

each expression construct By further insp ection the only nondeterminism

that maybeintro duced in rule application is the choice of typ e variables used

in rules AbsandPVar Wethus cho ose normal pro ofs that use fresh

variables in every place p ossible If a pro of exists there clearly must then

b e a corresp onding normal pro of For expression e the normal pro of is

unique mo dulo conversion Thus a decision pro cedure may b e dened for

constructing such a canonical pro of The algorithm fails when an inconsistent

constraint system is obtained when combining the constraint systems inferred

for subterms and detection of such inconsistencies is trivially decidable

Wenow relate the inference rules to the general rules 

Eifrig Smith and Trifonov

Ajj x t  e  n C Ax 

inf

Abs  Var

A  x e t  n C A  x  n

inf inf

A  e  n C A  e  n C

inf inf

Inj Sel

A  el t n C f   l t g A  l  e  l   n C

inf inf

e  n C A 

i i i inf

Num Record

U

C A   l  e   l   n A  n Nat n

i inf i i i i inf

i

A  e  n C

inf i i i

Match 

U

C f  t  tg A   l  e   l t  t n

i i i inf i i i i

i

A  e  n C  e  n C

inf   

App

A  e e t n C  C f   tg

inf   

Ax     is a renaming of fg

PVar

A  x 

inf

A  e  n C Ajj x    n C  e  n C

inf inf

Let

A  let x  e in e  n C  C

inf





if e is expansivethenAppClos  n C A

g where f





else Clos  n C A

Fig  Typ e inference rules of ISo op 

Theorem  Completeness of Typ e Inference Given an environment

A and an expression e the typing judgement A  e  is provable for some

if and only if A  e  is provable for some 

inf

Pro of Sketch If A  e  is provable A  e  is obviously provable

inf

as well each inference rule is a sp ecial case of a combination of Sub and a

general rule

Converselytyping pro ofs in the general set of rules may b e transformed

into ones using only the inference ones in a twostep pro cess First the pro of is

transformed into preinference form in which each rule used one of the infer

ence rules or p ossibly Sub In the pro cess certain typ es  used in the pro of

such as in the conclusion of rules Var Sel App and the like are replaced

byfreshtyp e variables t the corresp onding typ e constraints f t t  g

are added to the constraint system and bubbled to the top Similarlyeach

assumption x   is replaced with an assumption of the form x  t together with

the constraints f t t  g for some fresh t The result is a larger set of

constraints mentioning these new typ e variables Demonstrating the consis

tency of these richer constraint systems as these new constraints propagate to

the ro ot of the pro of is nontrivial

Second the pro of is reworked again eliminating uses of rule Sub induc 

Eifrig Smith and Trifonov

tively This transformation takes a preinference pro of of A  e   n C and

pro duces an inference pro of of A  e   n C whereC C and either  

or f  g C  This is p ossible b ecause the antecedents of each inference

rule are simply of the form A  e   n C the typ e  need not b e in any sp ecial

form for the rule to b e applicable Essentially this means that a use of Sub

followed by another rule can b e exchanged thus moving the subsumptions to

the ro ot of the pro of where they can b e eliminated

Thus from Theorems  and  wemay conclude that every program

typable under the general rules has a typ e inferred bythetyp e inference algo

rithm Note we establish no principal typing prop ertyThetyping pro duced

by the inference algorithm is indeed minimal in an intuitive sense but it is

not formally minimal since our denition of is weak tNat nft Natg

is not a subtyp e of NatNat n even though any term that can b e given the

former typ e can also b e given the latter Weleave the question of principal

typings for future study since completeness is ultimately all the programmer

desires

 Applications to OOP

Wenow illustrate howthistyp e inference algorithm is useful for typing ob ject

oriented programs the main motivation for our work We show its utilityin

classbased OOP we exp ect it also applies to delegationstyle OOP but that

topic is b eyond the scop e of this pap er The basic OOP concepts we wish

to incorp orate include standard notions of ob ject metho d instance variable



class inheritance metho dinstance hiding and ob ject lifting  The more

advanced notions we wish to account for include p olymorphism multiple in

heritance and binary metho ds Without binary metho ds in general metho ds

that take ob jects as parameters or return ob jects as values the ob ject typ

ing problem is not overly dicult ob jects maybeinterpreted as records of

functions metho ds and cells instance variables inheritance is subtyping

and ob ject lifting is accomplished by a subsumption rule As weshow typing

b ecomes considerably more dicult in the presence of binary metho ds  

The ideal waytoshow applicability to OOP would b e to dene a complete

OOP language typ es and inference algorithm this is b eyond the scop e of

this pap er however Instead we will showhow a collection of simple macros

allow OOP to b e emb edded into ISo op 

The basic idea of the representation is to interpret classes as functions on

records s   where s is the self  new then takes the xed p oint of a class

to pro duce an ob ject in the form of a record see  We cannot quite use

this enco ding First it is dicult to create xed p oints which are records in a

callbyvalue language Second when taking a xed p oint via a Y combinator

the semantics entails reevaluating the record with each recursive access and

thus erroneously reinitialize any instance variables In previous work  we

avoided these problems by using a memorybased xed p oint Unfortunately



Also called implicit ob ject co ercion or ob ject subsumption 

Eifrig Smith and Trifonov

this enco ding will not work here as the use of reference cells to form the xed

point will infer imp erative p olymorphic typ es for ob jects Wethus opt for

an enco ding using a Y combinator with an initial instance variable allo cation

phase In a more complete treatment of this topic a limited form of memory

based xed p oint such as the singleassignment reference SAR of  could

b e used We ignore the issue of information hiding in this presentation though

it is not dicult to incorp orate

Denition  The ob ject syntax is dened by the macros given in Figure 

def

where Y  y  x x x x z y x x z is a callbyvalue Y combinator and

def

! Yx x 

 and those Note that the class macro binds o ccurrences of s free in the e

k

 and e of u free in e

i

k j

class

class s sup er meth u of e inst x  e m  e

i i j k

j k

 

 e  in let x !in u u  u  let

i i

i i



let y  x  ref e u  u  in slet sin

j i

j i

  inst  y meth  m  e

k

k

new new  x Yx 

message send e  m e methm

instance read ex e instx

instance write e x  e set cell  e instxval  e 

 

Fig  Macros for ob ject syntax

We illustrate the typing problems involved with binary metho ds through

an example of a GcdNum class that has a binary metho d gcd that takes another

GcdNum and recursively computes the GCD of itself and the other GcdNum

In order to keep the example very simple we assume the instance variable

containing the actual number val is publicly accessible and that GcdNum

denes no other metho ds ZGcdNum is a sub class of GcdNum with an addi

tional unimp ortant metho d zero Here mo d is taken to b e a function that

computes the mo dulus of twonumbers

let GcdNum  class s sup er

inst

val  

meth

zero sval then s gcd   num if is

else if is zero mo d numval sval  then s

else sval  mo d sval numval num  gcd s 

Eifrig Smith and Trifonov

The gcd metho d takes another GcdNum ob ject num as argument Because

num is of the same typ e as the typ e of ob jects of the class we are cur

rently dening expressing the typ e of the gcd metho d will require some self

referentiality

We rst consider appropriate typ es for the inheritanceissubtyping paradigm

This is known to have serious limitations   but is nonetheless frequently

found in commercial OOP languages In this paradigm wegive GcdNum the

typ e

GcdNum  GcdTyp eGcdTyp e where

GcdTyp e t  val  Nat ref gcd  tt 

Note that  is the usual recursivetyp e constructor We use it instead of the

ISo op enco ding of recursivetyp es using recursive constraints new GcdNum

then returns an ob ject of typ e GcdTyp e Without inheritance this typ e is

p erfectly adequate Wenow lo ok at the adequacy of this typ e with inheritance

We extend our example by dening ZGcdNum a sub class of GcdNum that also

includes a metho d that tests for zero

let ZGcdNum  class s

sup er

u of GcdNum

inst

val  uval

meth

gcd  u  gcd

zero sval zero   is

In this case we did not override the gcd metho d instead we inherited it from

GcdNum denoted here by the sup erclass variable u in this enco ding we explic

itly state the sup erclass of each inherited metho d Using the inheritanceis

subtyping paradigm the inherited instance variables and metho ds must have

the same typ es as in the sup erclass since these typ es are xed Thus the typ e

of ZGcdNum must b e

ZGcdNum  ZGcdTyp eZGcdTyp e where

ZGcdTyp e

t  val  Nat ref gcd  GcdTyp eGcdTyp e zero  Bo ol 

Note the gcd metho d still op erates on GcdTyp enotZGcdTyp eThus if gcd

were overridden in ZGcdNum with a function that used nums zero metho d

this typing would fail an undesirable fact Another problem with this typing

is illustrated in the following additional co de

let zgnum  new ZGcdNum in zgnum  gcd zgnum  zero 

The gcd metho d typ e is not parametric in the typ e of the ob ject given to it

Thus it will accept an ob ject of ZGcdTyp e as an argument since bysubtyping

ZGcdTyp e GcdTyp e but the result returned is only of GcdTyp eandthus is

not known to havea zero metho d The ab ove co de will thus not typ echeck 

Eifrig Smith and Trifonov

even though it executes without error

An alternativetyping is needed Since we inherit from GcdNum the ZGcd

Num ob jects that eventually are created will have more metho ds than just gcd

To capture this wemust takea parametric or openended view of the selftyp e

in GcdNums typ e The parametricitywe desire in GcdNum is that t should

be any sub class with at least gcd and val and furthermore that gcd paramet

rically maps t to t To express the op enended view as a typ e Fb ounded

quantication is used as follows

GcdNum  t GcdTyp eFt tGcdTyp eFt where

GcdTyp eFt  val  Nat refgcd  tt 

ZGcdNum may then b e typ ed as

ZGcdNum  t ZGcdTyp eFt tZGcdTyp eFt where

ZGcdTyp eFt  val  Nat refgcd  tt zero  Bo ol 

giving zgnum the typ e t ZGcdTyp eFtThus the ab ovecodetyp echecks In

addition it would have b een p ossible to override gcd in ZGcdNum imp ossible

in the simple recursivetyp es view

The Fb ounded typing has a drawback however ZGcdNum ob jects can

no longer b e lifted to b e GcdNum ob jects since their typ es are recursivetyp es

with t o ccurring negatively and thus the following co de will not typ echeck

let gnum  new GcdNum in

let zgnum  new ZGcdNum in

gnum  gcd zgnum

Note that the recursivetyping would allow this co de to typ echeck

So b oth the Fb ounded interpretation of inheritance and the recursive

typ es interpretation fail to typ echeck certain typable programs Our typ e

inference algorithm however infers typ es that will allow b oth of the ab ove

varieties of message send to b e typ ed in a single program

 Types inferredinISoop

To simplify the presentation we will ignore the instance variable val in the

example We will also simplify the translation scheme to reect this by elim



inating the rst line from the macro expansion of class and replacing u by e

i

and dening new as Y 

First consider the typ es inferred for the classes GcdNum and ZGcdNum

The simplied translations are

let GcdNum 

s   gcd   num if then s

else if then s

else num gcd s 

in let ZGcdNum 

s let u  GcdNum s in 

Eifrig Smith and Trifonov

  gcd  u gcd

zero  zero  is

We rst sketchhow the inference system of rules   infers GcdNums

inf

typ e These rules are deterministic mo dulo variants so pro of construction

is mechanical Starting from the leaves and using rules Record App and

Selinturnwe obtain

A jj s  t    t  num  t   num gcd  t n C 

 a b inf d

where C ft t  t gcd  t g

b c c d

Next using App

A jj s  t    t  num  t   num gcd s  t n C 

 a b inf e 

where C ft t  t gcd  t  t t t g

 b c c d d e

Next expanding the conditional and using Abs and Matchtwice

A jj s  t    t   num   t t n C ft t  t t g

 a inf b   e  

FinallybyRecordandAbstwice

A  GcdNum  t t  gcd  t t  n C ft t  t t g

 inf a b   e  

This is the typ e inferred by the inference rule system An actual im

plemented typ e inference algorithm would automatically p erform a number

of simplications on this typ e that do not change the meaning Here we

present these simplications informally by giving typings deduced in the gen

eral rules that are simplied forms of the inferred typ es For GcdNum t is

a

unconstrained so it may b e replaced by  by subsumption t has only one

b

p ositive o ccurrence in the typ e so it may b e replaced with its upp er b ound

t  t and t may also each b e replaced The following typ e may then b e

c d e

deduced for GcdNum in the general rules

t  t  t  gcd   gcd  t t t  nft t g

   

Hereafter we present the simplied forms of typ es only An actual imple

mented typ e inference algorithm would automatically p erform these simpli

cations For ZGcdNum the simplied inferred typ e is

t  t  t  gcd   gcd  t t t zero  Bo ol  nft t g

   

Contrast these typ es with the Fb ounded typ e given GcdNum in the op en

self  enco ding ab ove Observe that the parameter num is an ob ject with

a gcd metho d Since that is the only metho d of num that is used no more

elds are required in the inferred typ e Contrast that with the Fb ounded

case where num has all metho ds of GcdNum the op enendedness here is more

precise each metho d that is passed the self  requires that self to only have

the metho ds actually used Note also that this is not even an Fb ounded

typ e the constraint t t is not recursive Recursive constraints maynot



arise in classes since the knot has not b een tied yet 

Eifrig Smith and Trifonov

Consider now the ob ject typ es gnum and zgnum have the following sim

plied typ es inferred

gnum  t  t  t nf gcd   gcd  t t t  t t g

   

zgnum  t  t  t nf gcd   gcd  t t t

  

zero  Bo ol  t t g



It is dicult to explain precisely what these typ es denote except to say

they are denitely not the recursivetyp es used in b oth enco dings for ob jects

ab ove

The message sends from the example have the following constrained typ es

zgnum  gcd gnum  t nf



 gcd   gcd  t t t zero  Bo ol  t

a   a

t  gcd  t t t  gcd  t t 

a b  b a 

 gcd   gcd  t t t  t t t  t t g

b   b a  b 

zgnum  gcd zgnum  t nf



zero  Bo ol  t t t  gcd   gcd  t

a   a

 t  gcd  t t t  gcd  t t

 a b  b a

 gcd   gcd  t t t zero  Bo ol  t

b   b

t t  t t g

a  b 

can b e proved to never and t Note the function upp er b ounds of t  t  t

a b

b a

b e used a more complete set of simplication transformations would justify

their removal Eachuseofgnum and zgnum gives rise to fresh variables bythe

PVar rule if these ob jects were not letp olymorphic the two message sends

ab ovewould share typ e variables and generalitywould b e lost Observe there

are no contradictions in the constraint systems of either of these message sends

Also note the result typ e t is in eect the union of t and t since it is an

 a b

upp er b ound of these twotyp es This corresp onds to the fact that the result of

gcd could b e either a gnum or a zgnum Consider sending a zero message to the

result of the second message send zgnum  gcd zgnum   zero  The rules

force t  zero  Bo ol  to b e added to the constraints but this is



still consistent On the other hand consider zgnum  gcd gnum  zero 

This maygive a runtime error so should not typ echeck Indeed t



 zero  Bo ol  by transitive closure also requires a record with

out zero to b e a subtyp e of a record with zero but this is by denition an

inconsistent constraint

Compared to other work on rigorously sound classbased ob ject languages

neither Bruces TOOPLE or TOIL languages   nor our Lo op language 

allows the ab ove program to typ echeck in fact weknowofnostatictyp e 

Eifrig Smith and Trifonov

system for ob jectoriented programming that successfully typ echecks this ex

ample So not only do we obtain ob ject typ e inference wehavearicher typ e

language where it is not required to cho ose b etween inheritance is subtyping

and the op enended view of self

 Discussion

Wehavegiven a new p owerful metho d for typ e inference for ob jectoriented

languages that is in manyways more p owerful than previously existing meth

ods Wehave hop es that the core we present here will lead to developmentofa

fullscale ob jectoriented incorp orating typ e inference

What we presenthereonlyshows this metho d is feasible however Further

study is necessary to see if it can b e implementedeciently in practice There

also is the question of howwell other language features will combine with this

inference metho d Mo dules in particular will b e a challenge There also should

b e separate syntax and typ es added for OOP features such as class denition

and message send This will provide a uniform notion of what OOP is to all

programmers and limit incompatibility of co de Lastlyeven though this sys

tem is signicantly stronger than the existing HindleyMilnerstyle inference

algorithms the typ es it pro duces are larger and less easily readable by pro

grammers Thus it is imp ortant to address b oth the problem of simplication

of these typ es and the problem of how a b etter descriptions of what led to a

typ e error can b e given to programmers

Acknow ledgemen ts

Wewould liketoacknowledge Jens Palsb erg for helpful discussions on related

work and AmyZwarico for contributions in the early phases of this pro ject

References

 M Abadi and L Cardelli A semantics of ob ject typ es In Proceedings of the

Ninth Annual IEEE Symposium on Logic in Computer Science pages 



 A Aiken Illyria system Available by anonymous ftp from ftp

s k ftpcsberkeleyedu pub personal aiken  

 A Aiken and E L Wimmers Typ e inclusion constraints and typ e inference

In Proceedings of the International ConferenceonFunctional Programming

Languages and Computer Architecture pages  

 K Bruce Safe typ e checking in a staticallytyp ed ob jectoriented programming

language In ConferenceRecord of the Twentieth Annual ACM Symposium on

Principles of Programming Languages pages  

 Kim B Bruce and Rob ert van Gent TOIL A new typ esafe ob jectoriented

imp erative language Technical rep ort Williams College  

Eifrig Smith and Trifonov

 P Canning W Co ok W Hill J Mitchell and W Oltho Fb ounded

p olymorphism for ob jectoriented programming In Proceedings of the

ConferenceonFunctional Programming Languages and Computer Architecture

pages  

 L Cardelli A semantics of multiple inheritance In Semantics of Data Types

volume  of Lecture notes in Computer Science pages  SpringerVerlag



 L Cardelli and PWegner On understanding typ es data abstraction and

p olymorphism Computing Surveys   December 

 William R Co ok Walter L Hill and Peter S Canning Inheritance is not

subtyping In ConferenceRecord of the Seventeenth Annual ACM Symposium

on Principles of Programming LanguagesACM Press 

 J Eifrig S Smith V Trifonov and A Zwarico A simple interpretation of

OOP in a language with state Technical Rep ort YALEUDCSRR Yale

University 

 J Eifrig S Smith V Trifonov and A Zwarico Application of OOP typ e

theory State decidabili tyintegration In OOPSLA  pages  

 J Eifrig S Smith V Trifonov and A Zwarico An interpretation of typ ed

OOP in a language with state Lisp and Symbolic Computation  To

app ear

 S Kaes Typ e inference in the presence of overloading subtyping and recursive

typ es In ACM Conference on Lisp and Functional Programming pages 

 

 Samuel N Kamin and Uday S ReddyTwo semantic mo dels of ob jectoriented

languages In Carl A Gunter and John C Mitchell editors Theoretical Aspects

of ObjectOrientedProgrammingchapter  pages  MIT Press 

 D Kozen J Palsb erg and M I Schwartzbach Ecient inference of partial

typ es In Foundations of Computer Science 

 D B MacQueen G Plotkin and R Sethi An ideal mo del for recursive

p olymorphic typ es Information and Control   

 R Milner A theory of typ e p olymorphism in programming Journal of

Computer and System Sciences   

 J Mitchell Co ercion and typ e inference summary In ConferenceRecord

of the Eleventh Annual ACM Symposium on Principles of Programming

Languages

 N Oxhj J Palsb erg and M I Schwartzbach Typ e inference with subtyp es

In ECOOP European Conference on ObjectOrientedProgrammingvolume

 of Lecture notes in Computer Science pages  SpringerVerlag 

 J Palsb erg and M Schwartzbach ObjectOrientedType Systems Wiley 

 Jens Palsb erg and Michael I Schwartzbach Safety analysis versus typ e

inference for partial typ es Information Processing Letters pages   

Eifrig Smith and Trifonov

 J Plevyak and A Chien Precise concrete typ e inference for ob jectoriented

languages In Proceedings of the Ninth Annual ACM Conference on Object

OrientedProgramming Systems Languages and Applications pages 



 Didier Remy Programming ob jects with MLART An extension to ML with

abstract and record typ es In Masami Hagiya and John C Mitchell editors

International Symposium on Theoretical Aspects of Computer Software pages

 Sendai Japan April  SpringerVerlag

 J C Reynolds Three approaches to typ e structure In TAPSOFT proceedings

volume  of Lecture notes in Computer Science pages  

 T Sekiguchi and A Yonezawa A complete typ e inference system for subtyp ed

recursivetyp es In Proc Theoretical Aspects of Computer Softwarevolume 

of Lecture Notes in Computer Science pages  SpringerVerlag 

 M Tofte Typ e inference for p olymorphic references Information and

Computation   

 A Wright and M Felleisen A syntactic approachtotyp e soundness Technical

Rep ort TR Rice University Department of Computer Science  To

app ear in Information and Computation