Rank 2 typ e systems and recursive de nitions

Technical Memorandum MIT/LCS/TM{531



Trevor Jim

Lab oratory for Computer Science

Massachusetts Institute of Technology

August 1995; revised Novemb er 1995

Abstract

We demonstrate an equivalence b etween the rank 2 fragments of the

p olymorphic lamb da calculus System F and the intersection typ e dis-

cipline: exactly the same terms are typable in each system. An imme-

diate consequence is that typability in the rank 2 intersection system

is DEXPTIME-complete. Weintro duce a rank 2 system combining

intersections and p olymorphism, and prove that it typ es exactly the

same terms as the other rank 2 systems. The combined system sug-

gests a new rule for typing recursive de nitions. The result is a rank 2

typ e system with decidable typ e inference that can typ e some inter-

esting examples of p olymorphic recursion. Finally,we discuss some

applications of the typ e system in data representation optimizations

suchasunboxing and overloading.

Keywords: Rank 2 typ es, intersection typ es, p olymorphic recursion,

boxing/unboxing, overloading.

1 Intro duction

In the past decade, Milner's typ e inference algorithm for ML has b ecome

phenomenally successful. As the basis of p opular programming languages

like Standard ML and Haskell, Milner's algorithm is the preferred metho d

of typ e inference among language implementors. And in the theoretical



545 Technology Square, Cambridge, MA 02139, [email protected]. Supp orted

by NSF grants CCR{9113196 and CCR{9417382, and ONR Contract N00014{92{J{1310. 1

community, the literature on typ e inference is dominated by extensions of

ML's let-p olymorphism.

In this pap er we examine some alternatives to ML that have attracted

surprisingly little attention: the systems of rank 2 typ es intro duced by

Leivant [21]. These systems are slightly more p owerful than ML|strictly

more terms can b e assigned typ es|and the increased p ower comes for free|

the complexityoftypability is identical. But the unique feature of the rank 2

systems that justi es further study is that, in sharp contrast to other exten-

sions of ML, they abandon let-p olymorphism.

We use the expression x:xx to illustrate the limitations of let-p oly-

morphism. It is well known that this expression cannot b e typ ed in ML:

the only way for ML to typ e the self-application xx is by assigning a p oly-

morphic typ e to x, and ML do es not allow abstraction over variables with

p olymorphic typ e. In ML, the only mechanism for intro ducing variables of

p olymorphic typ e is the let-expression:

let x =y :y 

in xx

This let-expression binds x to the identity function y :y , which has the

p olymorphic typ e 8t:t ! t in ML. By ML's let-p olymorphism, x is assigned

the typ e 8t:t ! t, which is sucienttotyp e xx.

The problem with this is that we cannot typ echeck the uses of x the

application xx separately from its de nition the function y :y . So ML

must b e extended with a module language in order to supp ort programming

in the large, where it is impractical to require every p olymorphic de nition

to app ear in the same source le as every use.

In contrast, x:xxistypable in all of the rank 2 systems we consider.

Here are two rank 2 typings:

x:xx: 8t:t ! t ! 8s:s ! s;

x:xx: t ! t ^ t ! t ! t ! t ! t ! t:

The rst typing says that x:xx is a function that, when given an argument

with typ e t ! t for any typ e t, pro duces a result with typ e s ! s, for any s.

The identity function is an appropriate argument.

The second typing says that x:xx is a function that, when given an

argumenthaving both the typ es t ! t and t ! t ! t ! t, pro duces

a result of typ e t ! t. Once again, the identityy :y  is an appropriate

argument. 2

The rank 2 systems we consider are subsystems of two widely studied

typ e systems, System F and the system of intersection typ es. System F,

intro duced indep endently by Girard [7] and by Reynolds [28], predates ML

and can typ e many more terms. A recent result of Wells [34], however, shows

that typability in the system is undecidable, putting typ e inference out of

reach.

The system of intersection typ es, intro duced indep endently by Copp o

and Dezani [5] and by Sall e [29], can typ e even more terms than System F:

1

it typ es all and only the strongly normalizing terms. The equivalence of

typability and strong normalization implies that typ e inference, just as with

System F, is unattainable.

With the goal of typ e inference in mind, we seek decidable restrictions of

these typ e systems. Restrictions based on the rank of typ es were suggested

by Leivant [21]. The rank of a typ e can b e easily determined by examining it

in tree form. A typ e is of rank k if no path from the ro ot of the typetoatyp e

constructor of interest either typ e intersection `^'ortyp e quanti cation `8'

passes to the left of k arrows. The typ es shown in Figure 1 are rank 2 typ es,

b ecause no path from ro ot to ^ or 8 passes to the left of two arrows. But

the typ es shown in Figure 2 go b eyond rank 2 they are rank 3 typ es. The

typ es given ab ove for x:xx are rank 2 typ es.

!

^

6 6

6 6

 

 6  6

 

! !

s s

6 6

6 6

 

 6  6

 

s

^

t

8t

6

6



 6



! !

t

6 6

 6  6

6 6

 

 

s

t t t

t ^ t ! s ! t ^ s s ! 8t:t ! t ! s

Figure 1: Examples of rank 2 typ es

Ranks 0 and 1 of Leivant's systems are equivalent to the simply typ ed

lamb da calculus, which can typ e fewer terms than ML. But starting with

1

Without the typ e constant ! . 3

!

6

6



 6 !



6

 6

!  6

s



6

 6

!  6

s



6

 6

6 !



s



6

 6

6



^

t



6

 6

6



s

8t



s

t

t

t ^ s ! t ! s s ! 8t:t ! s ! s

Figure 2: Typ es that go b eyond rank 2

rank 2, the systems can typ e more terms than ML.

Rank 2 of System F, whichwe call  , has received the most study. Mc-

2

Cracken [23] prop osed a typ e inference algorithm for  based on Leivant's

2

ideas. This algorithm is incorrect. Kfoury and Tiuryn [12] show that the

complexityoftypabilityin is identical to that of ML. Kfoury and

2

Wells [16, 17] give a correct typ e inference algorithm, and show that ranks 3

and higher in System F are undecidable.

Leivant's original pap er is almost the only work on rank 2 of the inter-

section typ e disciplin e, whichwe call I . Leivantsketched a typ e inference

2

algorithm for I , but the algorithm was not formalized and proved correct

2

until recently [33]. Leivant also conjectured the undecidabili ty of ranks 3

and higher in the intersection system; to our knowledge the details of his

pro of idea have never b een veri ed.

I has a signi cant advantage over  : it has principal typings. This

2 2

means that for any term M ,if M is typable in I , then there is an I typing

2 2

judgment

A ` M : 

that represents all of the p ossible typing judgments for M . Other typings

for M can b e obtained from the principal typing by simple op erations sub-

stitution and subsumption.

Contributions of the pap er

Since I has principal typings, and  do es not, we b elieve I deserves

2 2 2

more study. The rst contribution of this pap er is to develop some of the 4

2

basic prop erties of I .We establish the following equivalence: a term is

2

typable in I if and only if it is typable in  . An immediate corollary is that

2 2

typabilityinI is DEXPTIME-complete, identical to typabilityin and

2 2

ML. We also consider some variants of I , and show they are all equivalent

2

in terms of typability.

The second contribution of this pap er is to intro duce a new typ e system,

P , that combines rank 2 intersection typ es and top-level quanti cation of

2

typ e variables, as in ML. P has principal typings, so it clearly improves

2

on  . Its advantage over I is more subtle. The addition of quanti ers

2 2

makes typ es more expressive: the quanti ers identify generic typ e variables,

that is, typ e variables which can safely b e instantiated with anytyp e. In

particular, this suggests an interesting typ e inference algorithm for recursive

de nitions.

A recursive de nition is written in the form xM , and is meantto

denote a program x such that

x = M;

where M may contain some uses of x. The standard rule for typing recursive

de nitions lo oks like

A [fx :  g` M : 

A ` xM :

Most typ e inference algorithms restrict the typ e  in this rule to b e a simple

typ e. The rule of polymorphic recursion relaxes this restriction by allow-

ing  to b e an ML typ e scheme. This gives a useful increase in typing

power|it can typ e some natural programs that cannot b e typ ed by the

simple recursion rule. However, p olymorphic recursion makes typ e infer-

ence undecidable [14].

We suggest another wayoftyping recursive de nitions:

A [fx :  g` M : 

where    

A ` xM :

The rule says that as long as the typ e  of M is more general than the

assumption  on x needed to typ e M ,we can deduce  as the typ e of the

recursive de nition.

We extend P to typ e recursive de nitions in this way. The resulting

2

system can typ e many but not all of the examples that seem to require

2

The equivalence b etween the rank 2 fragments of System F and the intersection typ e

discipli ne has b een shown indep endently byYokouchi [35]. 5

p olymorphic recursion. Moreover, the system has principal typ es and de-

cidable typ e inference.

Organization of the pap er

s s

In x2, weintro duce I , a syntax-directed version of I , and  , a syntax-

2

2 2

directed version of  . The main result is that a term is typable in one

2

system if and only if it is typable in the other. An immediate corollary

s

is that typabilityinI is DEXPTIME-complete, the same complexityas

2

s s

in ML and  .Inx3, we present the typ e inference algorithm for I .In

2 2

x4, we discuss some other de nitions of rank 2 intersection typ e systems,

and show their equivalence with I .Inx5, we de ne P , show that it has

2 2

principal typings, and giveatyp e inference algorithm. In x6, we discuss

various ways of typing recursive de nitions, and we prop ose an extension

of P that can typ e many examples of p olymorphic recursion. We discuss

2

applications of P to compilation in x7, and we summarize our results in x8.

2

2 Rank 2 typ e systems

2.1 Preliminaries

We will b e de ning a number of typ e systems; here we develop machinery

that will b e useful in all of them.

We use x;y;::: to range over a countable of variables, and t; s to

range over a countable set, Tv ,oftyp e variables. The terms and typ es of

the systems will vary, but in all cases we use ;;::: to range over typ es,

and M , N , . . . to range over terms.

The terms of the pure lambda calculus are de ned by the following

grammar:

M ::= x j M M  j xM :

1 2

Unless stated otherwise, terms are considered syntactically equal mo dulo

renaming of b ound variables. We adopt the usual conventions that allowus

to omit parentheses: application asso ciates to the left, and the scop e of an

abstraction `' extends to the right as far as p ossible. We write x x :M

1 n

for x x M  .

1 n

The typ es of our systems will all b e subsets of the typ es with quanti -

cation and intersection:

 ::= t j  !   j 8t  j  ^  :

1 2 1 2 6

By convention, `!' asso ciates to the right, so that, e.g., t ! t ! t may

b e written more compactly as t ! t ! t, and `^' binds more tightly than

`!', e.g.,  ^  ! t means  ^   ! t. The scop e of a quanti er `8' extends

~

as far to the right as p ossible. We write 8t  for the typ e

8t 8t :::8t   :::;

1 2 n

~

where t = t ;t ;:::;t and n  0.

1 2 n

The set of simple types, T , is de ned by the following inductive equa-

0

tion:

T = f t j t is a typ e variable g[f  !   j ; 2 T g:

0 0

A type environment is a nite set fx :  ;:::;x :  g of variable, typ e

1 1 n n

pairs, where the variables x ;:::;x are distinct. We use A to range over

1 n

typ e environments. We write Ax for the typ e paired with x in A, domA

for the set fx j9:x :   2 Ag, and A for the typ e environment A with

x

any pair for the variable x removed. We write A [ A for the union of two

1 2

typ e environments; by convention we assume that domA  and domA 

1 2

are disjoint. For any set T of typ es, wesay A is a T typ e environmentif

Ax 2 T for all x 2 domA.

The notion of free type variable is de ned as usual. We write FTV  for

the free typ e variables of a typ e  , and FTVA for the free typ e variables

of all typ es app earing in A.We write GenA;   for the 8-closure of  by

the typ e variables FTV   FTVA.

A judgment is a relation b etween typ e environments, terms, and typ es,

written A ` M :  . A term M is typable if A ` M :  for some A and  .

A pair hA;  i of a typ e environment and a typ e is called simply a pair .

Two pairs hA ; i and hA ; i are disjoint if their free typ e variables are

1 1 2 2

disjoint. An acceptable pair of a term M in a typ e system is a pair hA; i

such that the judgment A ` M :  holds in the typ e system. We write

AP M  for the set of acceptable pairs of M in a typ e system T .

T

A substitution is a mapping from typ e variables to simple typ es whichis

the identity on all but a nite number of typ e variables. We use S; R; Q; U

to range over substitutions. The domain and range of a substitution S are

de ned

domS  = ft j St 6= tg;

[

rngS  = FTVSt:

t2domS  7

If domS = ft ;t ;:::;t g and St =  for all i, then S can b e written in

1 2 n i i

the form ft :=  ;:::;t :=  g.

1 1 n n

The application of substitutions is extended to typ es, typ e environments,

and pairs in the usual way. The comp osition of substitutions is denoted

by juxtap osition, so that SRt =SRt = S Rt. Wesay S and S are

1 2

disjoint if domS  and domS  are disjoint sets. If S and S are disjoint,

1 2 1 2

then the substitution S [ S is de ned as follows:

1 2

8

S t if t 2 domS ;

<

1 1

S [ S t= S t if t 2 domS ;

1 2 2 2

:

t otherwise.

Note that wehave made a severe restriction on substitutions: they map

typ e variables only to simple typ es, and not typ es in general.

2.2 The rank 2 intersection typ e system

There are many di erent formulations of intersection typ e systems; see van

Bakel [33] for a survey.We will presentavery restricted intersection typ e

system here, the system of rank 2 intersection typ es. Our system is a slight

generalization of van Bakel's version see x4.1.

The terms of the intersection are just the terms of the

lamb da calculus. The sets T and T are de ned to b e the smallest sets

1 2

satisfying the following equations:

T = T [f ^   j ; 2 T g;

1 0 1

T = T [f !   j  2 T ; 2 T g:

2 0 1 2

The set T of rank 1 typ es consists of nite, nonemptyintersections of simple

1

typ es. T is the set of rank 2 intersection typ es: these are typ es p ossibly

2

containing intersections, but only to the left of a single arrow. Note that

T = T \ T , and for i 2f0; 1; 2g,if  2 T , then S 2 T .

0 1 2 i i

In order to simplify subsequent de nitions, we adopt the following syn-

tactic convention: we consider `^' to b e an asso ciative, commutative, and

idemp otent op erator, so that any T typ e may b e considered a nite, non-

1

V

 , where each  2 T . empty set of simple typ es, written in the form 

i i 0

i2I

De nition 1 For i 2f1; 2g,we de ne the relation  as the least partial

i

order on T closed under the following rules:

i

V V

 .     i If f j j 2 J g f j i 2 I g, then 

j i 1 j i

j 2J i2I 8

V

var A [fx :  g`x :  where i 2 I 

i i 0

0

i2I

A [fx :  g`M : 

x

abs

A ` xM : ! 

V

A ` M :   ! ; 8i 2 I  A ` N : 

i i

i2I

app

A ` MN: 

s

Figure 3: Typing rules of I .Typ es in typ e environments are in T , and

1

2

derived typ es are in T .

2

ii If    and    , then  !     !  .

1 1 1 2 2 2 1 2 2 1 2

The rst rule says that  expresses the natural ordering on intersection

1

typ es, and the second rule says that  ob eys the usual antimonotonic

2

ordering on function typ es, restricted to rank 2.

Some useful prop erties of the orderings  and  are summarized in

1 2

the following lemma.

Lemma 2

i If  2 T and  2 T , then    i  =  .

0 1 1

ii If  2 T and  2 T , then    i  =  .

2 0 2

iii For i 2f1; 2g,if    , then S  S .

i i

Judgments in our rank 2 system are de ned inductively by the rules of

s

Figure 3. We write I .A` M :  if the judgment A ` M :  follows by

2

these rules, with typ es app earing in typ e environments restricted to T , and

1

s

derived typ es restricted to T . The sup erscript `s' in I indicates that the

2

2

system is syntax-directed, in contrast with a later variant see x4.

If A and A are T typ e environments, we de ne A + A ,a T typ e

1 2 1 1 2 1

environment, as follows: for each x 2 domA  [ domA ,

1 2

8

A x if x 62 domA ;

<

1 2

A + A x= A x if x 62 domA ;

1 2 2 1

:

A x ^ A x otherwise.

1 2

s s 0

Lemma 3 Weakening If I .A` M :  , then I .A+ A ` M :  for

2 2

0

any T type environment A .

1 9

Pro of: An easy induction on typing derivations. 2

s s

Lemma 4 Substitutivity If I .A ` M :  , then I .SA ` M : S for

2 2

any substitution S .

Pro of: By induction on the structure of M .

V

i If M = x, then Ax=   and  =  for some i 2 I . Then

i i 0

0

i2I

V

s

S , I .SA ` x : S , and S = S . SAx= 

i i i

2 0 0

i2I

s

ii If M = xN then  must b e of the form  !  , and I .A [fx :

1 2 x

2

s

 g`N :  . Then by induction, I .SA [fx :  g ` N : S ,soby

1 2 x 1 2

2

s s

rule abs, I .SA ` N : S ! S ,orI .SA ` N : S  !  .

x 1 2 x 1 2

2 2

s

Then byweakening, I .SA` N : S  !  .

1 2

2

V

s

  2 T wehave I .A ` M : iii If M = M M , then for some 

i 1 1 1 2

2

i2I

V

s

  !  and I .A ` M :  for all i 2 I . By induction we 

i 2 i

2

i2I

V

s s

have I S  ! S and I .SA` M : .SA ` M : S , and by

i 1 2 i

2 2

i2I

s

rule app,wehave I .SA` M M : S, as desired.

1 2

2

2

2.3 System F

The terms of System F are exactly the terms of the lamb da calculus. The

types of System F are de ned by the following grammar:

 ::= t j  !   j 8t :

1 2

We consider System F typ es to b e syntactically equal mo dulo renaming of

b ound typ e variables, reordering of adjacent quanti ers, and elimination of

unnecessary quanti ers.

The typ es of System F can b e organized into a hierarchy as follows.

First, de ne R0 = T . Then for n  0, the set Rn + 1 is de ned to b e

0

the least set satisfying

Rn +1 = Rn [f !   j  2 Rn; 2 Rn +1g

[f8t  j  2 Rn +1g:

It will b e useful to restrict typ es so that quanti ers do not app ear to the

immediate right of arrows. Therefore we de ne the sets

0

S = S [f8t  j  2 Sg;

0 0

S = T [f !   j  2 S; 2 S g:

0 10

var A [fx :  g`x :  where   

A [fx :  g`M : 

x 1 2

abs

A ` xM : ! 

1 2

~

A ` M :8t  !  ; A ` N : 

1 2 1

app each t 62 FTV A

i

A ` MN: 

2

s

Figure 4: Typing rules of  .Typ es in typ e environments are in S1, and

2

0

derived typ es are in S 2.

0 0

We write Sn for S \ Rn and S n for S \ Rn. Note that the S1

typ es are exactly the ML typ e schemes.

0 0

De nition 5 Supp ose  = 8t t : 2 S1, and ;  2 T .Wesay  is

1 n 0

0

an instanceof  , written   , if and only if for some  ;:::; 2 T ,we

1 n 0

0 0

have  = ft :=  ;:::;t :=  g .We write  8s s   if and only

1 1 n n 1 m

0

if s ;:::;s are not free in  and   .

1 m

Note that the sense of ` ' is opp osite to that of our other relations;

for example, b oth \   " and \  "may b e read, \ is more general

2

than  ." We make an exception in the case of ` ' to b e consistent with its

use in ML [24].

s

Wenow de ne  , our version of the rank 2 fragment of System F. The

2

s

sup erscript `s' in  indicates that the system is syntax-directed. See Kfoury

2

and Tiuryn [12] for a de nition of  , the non-syntax-directed version.

2

The judgments of the system are de ned by the rules of Figure 4. We

s

.A` M :  if A ` M :  is derivable from these rules, where typ es write 

2

in typ e environments are restricted to S1, and derived typ es are restricted

0

to S 2.

s

 studied by Kfoury et al. [12,17]: is closely related to the system 

2

2

Theorem 6

s

i If  .A` M :  , then  .A` M :  .

2

2

0 0 0

ii If  .A` M :  , then  is of the form 8t t  , where  2 S 2,

1 n

2

s 0

and  .A` M :  .

2 11

var A [fx :  g`x :  where   

A ` M :  !  ; A ` N : 

1 2 1

app

A ` MN: 

2

A [fx :  g` M : 

x 1 2

abs

A ` xM :  ! 

1 2

A ` M :  ; A [fx : GenA;  g`M : 

1 1 x 1 2 2

let

A ` let x = M in M :

1 2 2

Figure 5: Typing rules of ML. Typ es in typ e environments are in S1, and

derived typ es are in T .

0

This equivalence follows immediately from results of Kfoury and Wells [17].

It implies the following useful result:

s 0 s 0

Lemma 7 If  .A ` M :  and Gen A;    , then  .A ` M :  .

2 2

2.4 ML

Many di erent formulations of the ML typ e system have b een studied; we

cho ose to present a syntax-directed version here, as in Clement et al. [4]or

Tofte [32].

The types of ML are the typ es T , and the ML type schemes are the

0

typ es S1. The terms of ML are the terms of the lamb da calculus extended

with let-expressions :

M ::= x j M M  j xM  j let x = M in M :

1 2 1 2

The judgments of ML are de ned inductively by the rules of Figure 5. We

write ML .A ` M :  if A ` M :  is derivable from these rules, where typ es

in typ e environments are restricted to S1, and derived typ es are restricted

to T .

0

De nition 8 An ML typ e  is a principal type for M in A if and only

0 0

if ML .A ` M :  , and for all ML typ es  ,ifML.A ` M :  , then

0

GenA;    . 12

Theorem 9 Principal typ es for ML If M is typable by A, then there

exists a principal type for M in A.

0 0

Lemma 10 If ML .A ` M :  , and GenA;    , then ML .A ` M :  .

s s

2.5 Relationship of  and I

2 2

s s

Wenow show that a term is typable in  if and only if it is typable in I .

2 2

The left to right implication is develop ed rst.

De nition 11

i We de ne a relation  between S1 and T as follows. Supp ose

1 1

V

 2 S1 and  ;:::; 2 T n  1. Then      if and

1 n 0 1 i

i2I

only if   for all i 2 I .

i

0

ii We de ne the relation  between S 2 and T inductively:

2 2

a For anytyp e variable t, t  t.

2

0 0 0 0

b If    and    , then  !     !  .

1 2 2

Note that the relation  is monotonic in the argument of function typ es, in

2

contrast to the relation  .We extend the relation  to typ e environments

2 1

0 0

as follows: A  A if and only if x 2 domA and Ax  A x whenever

1 1

0 0 00 0 00

x 2 domA . Note that A  A + A if A  A and A  A , and

1 1 1

0 0

A  A if A  A .

1 x 1

s s 0 0 0

Theorem 12 If  .A` M :  , then I .A ` M :  , where A  A and

1

2 2

0

   .

2

Pro of: By induction on derivations.

s s

i M = x and  .A` x :  follows by the  rule var. Then we

2 2

must have Ax  .

0 s 0 0

Let A = fx :  g. Clearly I .A ` M :  , A  A , and    .

1 2

2

s s

.A ` xN :  !  follows by the  ii M = xN ,  =  !  , and 

1 1

2 2

rule abs.

Then wemust have

s

 .A [fx :  g`N :  :

x 1

2

By induction, wehave

0 0 0 s

; .A [fx :  g`N :  I

1 2 13

0 0 0 s

where A  A ,    , and    .Soby the I rule abs,we

x 1 1 1 2

1 2

have

s 0 0 0

I .A ` N :  !  ;

2 1

0 0 0

where A  A , and  !     !  , as desired.

1 1 2

1

s s

rule app. .A` M M :  follows by the  iii M = M M and 

1 2 1 2

2 2

Then wemust have, for some  2 T ,

0 0

s

~

 .A` M :8t:  ! ;

1 0

2

s

.A` M :  ; 

2 0

2

~

where the typ e variables t do not app ear in FTVA. Then by induc-

tion wehave

^

s 0 0

I .A ` M :   !  ;

1 i

2 0

i2I

V

0 0

~

where A  A ,    , and 8t:     .

1 2 0 1 i

0

i2I

~

Then each  is an instance of 8t: , and therefore by Lemma 7,

i 0

s

 .A ` M :  for all i 2 I .

2 i

2

s 0 0

By induction wehave for all i 2 I , I .A ` M :  , where A  A .

2 i 1

2 i i

P

0 0 0 0

A , then A  A , and byweakening, + So if A = A

1

0

i2I i

V

s 0 0

I   !  ; .A ` M :

i 1

2

i2I

s 0

I .A ` M :  8i 2 I :

2 i

2

s

Then by the I rule app wehave

2

0 0 s

.A ` M M :  ; I

1 2

2

as desired.

2

Wenow show the other direction of the equivalence: any term typable

s s

in I is typable in  .

2 2

Convention 13 In the remainder of this section we do not consider terms

to b e identical mo dulo -conversion, and we will assume the following con-

vention regarding the names of b ound and free variables:

i No variable is b ound more than once. 14

ii The b ound and free variables are disjoint.

This convention is necessary to make the following function well-de ned:

De nition 14 Let  denote the empty sequence. The function, act , that

maps terms to sequences of variables, is de ned inductively by the following

3

rules.

i act x= .

ii If act M = x ;:::;x then act y M = y; x ;:::;x .

1 n 1 n

iii If act M = y; x ;:::;x n  0 then act MN= x ;:::;x .

1 n 1 n

iv If act M =  then act MN= .

De nition 15

i is the rule

xy M N ! y xM N :

ii ! is the compatible closure of .

iii A -redex is any term matching the left-hand side of the rule .We

say M is a -normal form, or -nf, if no subterm of M is a -redex.

Note that by our convention on the distinct naming of variables, there is no

capture of variables in the rule. We use the name \ " in accordance with

Kfoury and Wells [18]. See Barendregt [2] for a de nition of \compatible."

Lemma 16

i ! is strongly normalizing.

ii ! satis es the diamond prop erty.

iii -nf 's are unique.

Pro of:

i The pro of is similar to the pro of of Lemma 5.5 from Kfoury and

Wells [17]:

3

Our de nition is identical to the de nition of [12], but di ers from [11]. 15

Let appl M  b e the set of subterms of M that are applications, and

let

X

 M = max0; jactM j1:

1

M M 2applM 

1 2

If M ! N , then  M = N  + 1. Since for any M wehave  M  

0, we can conclude that ! is strongly normalizing. In fact,  M  > 0

i M contains a -redex, and M normalizes in exactly  M  steps.

If jM j is the size numb er of subterms of M , then clearly japplM j

2

jM j and jact M j jM j.Thus  M  jM j . Therefore normaliza-

2

tion of a term M takes O jM j  steps.

ii This is a simple case analysis.

iii This follows from ii.

2

Lemma 16 justi es the following de nition:

De nition 17 We write -nfM  for the -nf of M .

s s

Lemma 18 For D2fI ;  g, the fol lowing hold:

2 2

i D .A ` xy M N :  i D .A` y xM N :  .

ii If M ! N , then D .A ` M :  i D .A ` N :  .

iii D .A ` M :  i D .A` -nfM : .

Pro of:

i Simple case analysis.

ii Use i and induction on the de nition of compatible.

iii Use ii and induction on the length of rewriting.

2

s

Lemma 19 If act M =x ;:::;x and I .A` M :  , then  is of the

1 n

2

form  !  !  !  , where  2 T .

1 n 0

Pro of: By induction on the structure of M . 16

i If M = x, then n =0by the de nition of act , and  2 T by rule

0

var.

s

.A` M :  follows by rule abs, and therefore ii If M = x N , then I

1

2

0

 is of the form  !  , where  2 T .

1 1 1

s

Also wemust have act N = x ;:::;x n  1 and I .A[fx :

2 n 1

2

0 0

 g`N :  . By induction  must b e of the form  !  !  !  ,

1 2 n

where  ;:::; 2 T and  2 T .

2 n 1 0

s

iii If M = M M , then I .A` M :  follows by rule app, and

1 2

2

s 0 0

therefore wehave I .A ` M :  !  , where  2 T .

1 1

2

We consider two cases. If act M = y; x ;:::;x for some variable

1 1 n

y , then by induction,  is of the form  !  !  !  , where

1 n

 ;:::; 2 T and  2 T .

1 n 1 0

Otherwise act M = , and therefore act M = ,sowe only need

1

0

prove  2 T . And by induction, wehave !   2 T ,so 2 T .

0 0 0

2

s

Note 20 A similar lemma holds for  , c.f. Kfoury et al. [12], Lemma 15.

2

Lemma 21 Suppose M is a -nf. Then

act M  6=  i M = y N for some y; N :

Pro of: By induction on the structure of M . The cases M = x and M =

y N are trivial, so assume M = M M .Wemust show act M = .

1 2

By wayofcontradiction, assume that act M =x ;:::;x n  1. By

1 n

the de nition of act ,wemust have act M =y; x ;:::;x for some y .

1 1 n

0 0

Then act M  6= ,soby induction wehave M = y M , and act M =

1 1

1 1

0 0 00

x ;:::;x . Since n  1, act M  6= , and by induction M = x M . But

1 n 1

1 1 1

then M is a -redex, contradiction. 2

De nition 22 We de ne a mapping, ml , from terms to ML terms:

i mlx= x.

ii mlxM = x mlM .



let x = mlM  in mlN  if M = xN ,

2 1

iii mlM M =

1 2

mlM  mlM  otherwise.

1 2 17

De nition 23

i A generalization of a set T of simple typ es is a typ e  2 S1 such that

  for every  2 T. A generalization  of T is the least common

0 0

generalization of T if   for any other generalization  of T.

V V

ii If    2 T ,we de ne lcg    to b e the least common gen-

i 1 i

i2I i2I

eralization of f j i 2 I g.If , ...,  2 T and  2 T , then

i 1 n 1 0

lcg  !  !  !  = lcg   !  ! lcg   ! :

1 n 1 n

The function lcg is extended to typ e environments in the usual way.

The use of \least" in the name \least common generalization" is consistent

with the relation ` '. Recall that the sense of ` ' is opp osite to that of our

other subtyping relations, so that \least" for ` ' means \greatest" for the

other relations.

The concept of least common generalizations was develop ed by Plotkin

[26] and Reynolds [27]. They showed that any nite nonempty set of simple

typ es has a least common generalization, and they gave an algorithm to

compute it.

Lemma 24 If M is a -nf and  2 T , then

0

s

i I .A ` M :  implies ML . lcg A ` mlM :  ; and

2

s

ii  .A ` M :  if and only if ML .A` mlM : .

2

Pro of:

i By induction on the structure of M .

a The case M = x is trivial.

s s

b If M = y N , then I .A ` M :  follows by the I rule abs,so

2 2

0 0 s

 must b e of the form  !  where ;  2 T , and I .A[fy :

0

2

0

 g`N :  . Note that N is a -nf, so we can apply the induction

hyp othesis to get

0

ML . lcg A [fy :  g ` mlN :  :

Now  2 T ,so lcg A [fy :  g=lcg A [fy :  g. Therefore

0

0

ML . lcg A [fy :  g` mlN : ,soby the ML rule abs,

0

ML . lcg A ` mly N :  !  , as desired. 18

s

c If M =y M M , then our judgmentmust followby the I rules

1 2

2

abs and app.Thus wehave

V

s

I .A[fy :  g` M : ;

i 1

2

i2I

s

.A ` M :  : 8i 2 I  I

2 i

2

V

~

 , where  2 T , and no t app ears in A. Let 8t = lcg 

i 0 i

i2I

By induction, wehave

~

ML . lcg A [fy : 8t g`mlM :;

1

8i 2 I  ML . lcg A ` mlM : :

2 i

By the principal typ e prop erty of ML, wehave

ML . lcg A ` mlM ::

2

Then since mlM = let y = mlM  in mlM , wehave

2 1

ML . lcg A ` mlM :

by the ML rule let.

s

d If M = M M , where M is not an abstraction, then by the I

1 2 1

2

0

rule app,wehave for some  ,

s 0

I .A ` M :  ! ;

1

2

s 0

I .A ` M :  :

2

2

M is a -nf and is not an abstraction, so by Lemma 21, wehave

1

0

act M = . Then by Lemma 19,  !  2 T , and therefore

1 0

0

 2 T . M is also a -nf, so wemay apply the induction

0 2

hyp othesis to b oth judgments ab ove, to get

0

ML . lcg A ` mlM :  ! ;

1

0

ML . lcg A ` mlM :  :

2

Then by the ML rule app,wehave

ML . lcg A ` mlM M : ;

1 2

as desired.

ii Similar, but easier.

2 19

Note 25 The converse of Lemma 24i do es not hold. For instance, if  =

t and A = fx : t ^ t g, then lcg A=fx : 8t:tg, ml xx= xx, and

3 1 2

ML . fx : 8t:tg`xx : t , but the judgment fx : t ^ t g` xx : t cannot b e

3 1 2 3

s

derived in I .

2

s s

Theorem 26 If I .A ` M :  , then  . lcg A ` M : lcg  .

2 2

Pro of: Supp ose act M = x ;:::;x . Then by Lemma 19,  is of the form

1 n

 !  !  !  , where  2 T , and by Lemma 21, the -nf of M is of

1 n 0

the form x x N , where N is a -nf. By Lemma 18iii,

1 n

s

I .A` x x N : :

1 n

2

s

This judgmentmust followby n uses of the I rule abs,sowehave

2

s

I .A[fx :  ;:::;x :  g` N : :

1 1 n n

2

Then by Lemma 24, wehave

s

 . lcg A [fx :  ;:::;x :  g ` N : :

1 1 n n

2

s

By n uses of the  rule abs,wehave

2

s

. lcg A ` x x N : lcg  ; 

1 n

2

and by Lemma 18iii, wehave

s

. lcg A ` M : lcg  : 

2

2

Theorem 27 If M is a term of the pure lambda calculus, then M is typable

s s

in I if and only if M is typable in  .

2 2

s

Therefore, typability in I is DEXPTIME-complete.

2

s s

Pro of: The equivalence of I and  typability follows from Theorems 12

2 2

and 26.

s

Kfoury and Tiuryn [12] show that  typability is p olynomial time

2

equivalent toMLtypability.MLtypabilitywas shown to b e DEXPTIME-

complete indep endently by Kfoury et al. [15] and by Mairson [22]. 2

The equivalence of Theorem 27 has b een shown indep endently byYoko-

uchi [35]. 20

s

3 Typ e inference for I

2

s

We present the typ e inference algorithm for I and a pro of that it infers prin-

2

cipal pairs. The algorithm is not new: it was describ ed brie y in Leivant's

original pap er [21], and was de ned rigorously byvan Bakel in his disser-

tation [33]. We include it here b ecause the algorithm provides a wayto

compare a varietyoftyp e systems based on rank 2 intersection typ es, and

b ecause we will extend the algorithm in a later section.

The algorithm takes as input a term M , and pro duces a pair hA; i such

s

that I .A` M :  . Moreover, the pair hA;  i is principal in the sense

2

that any other acceptable pair of M can b e obtained from hA; i by some

well-understo o d op erations.

De nition 28

0 0

i We write A  A if x 2 domA and Ax  A x for all x 2

1 1

0

domA .

ii The ordering  on T typ e environment, T typ e pairs is de ned as

1 2

follows:

0 0 0 0

hA;  i hA ; i if and only if A  A and    .

1 2

s

iii A pair hA;  i is a principal pair for M if hA;  i2AP M , and for

I

2

0 0

s

any other pair hA ; i2 AP M , there is a substitution S such that

I

2

0 0

S hA; i hA ; i.

0

Note that  and  are transitive, and A + A  A for all T typ e envi-

1 1 1

0

ronments A; A .

3.1 Subtyp e satisfaction

In this section we give a decision pro cedure for one of our subtyping relations,

and showhow to solve a more general problem, subtype satisfaction, that

we use in our typ e inference algorithm.

Up until now, wehave relied on some syntactic conventions to simplify

our presentation, namely, that `^' is an asso ciative, commutative, and idem-

p otent op erator. Part of the problem we are addressing here is how to decide

whether twotyp es are equivalent under these assumptions. Therefore, in this

section, we do not rely on the syntactic conventions in anyway.

Subtyp e satisfaction is a generalization of the well-known problem of

uni cation, and the techniques we use here are based on those used to solve 21

uni cation. For more details, consult a survey on uni cation [19, 20,30, 10,

6, 31, 1]. One di erence b etween uni cation and our satisfaction problems is

that wework with typ es that go b eyond simple typ es, but our substitutions

involve only simple typ es. This is not the typical case with uni cation, and

it makes our problem easier to solve.

If S ;S are substitutions and V is a set of typ e variables, wesay S

1 2 1

and S areequivalent on V , written S = S ,if S t = S t for every t 2 V .

2 1 V 2 1 2

Wesay S is more general than S on V , written S  S , if there is a

1 2 1 V 2

substitution S such that S = S S . The relation  is a partial order

3 2 V 3 1 V

mo dulo = .We omit V when V = Tv . A substitution S is idempotent if

V

S = SS, or, equivalently,ifdomS  \ rng S = ?.

We de ne the relation  between T and T to b e the least relation

2;1 2 1

closed under the rule:

V

 .  If    for all i 2 I , then   

i 2 i 2;1

i2I

A  -satisfaction problem is a pair 9~s:P , where P is a nite set whose every

2;1

element is either: 1 an equalitybetween simple typ es; or 2 an inequality

between a T typ e and a T typ e. When ~s is empty 9~s may b e omitted.

2 1

We use  to range over  -satisfaction problems.

2;1

0

A substitution S is a solution to 9~s:P if there is a substitution S such

0 0 0

that S t= S t for all t 62 ~s, S   S  for all inequalities     2 P ,

2;1

0 0

and S  = S  for all equalities  =   2 P . The p ossibly empty set of

solutions to a problem  is written Solutions  . Two problems  and 

1 2

are equivalent if Solutions  = Solutions  .

1 2

De nition 29

i A substitution U is a most general solution to  if it satis es the

following conditions.

a U 2 Solutions  .

b If S 2 Solutions   then U  S .

FTV  

c U is idemp otent.

d domU  FTV  .

ii We write MGS  for the p ossibly empty set of most general solu-

tions to a  -satisfaction problem  .

2;1

We require the last two conditions on most general solutions for technical

convenience only.We could relax the de nition by eliminating those condi-

tions; but any  has a solution under the relaxed de nition if and only if it

has a solution under our de nition. 22

Sometimes it is useful to ensure that a most general solution do es not

interfere with a set of \protected" variables. For any set W of typ e variables,

wesay U is a most general solution to  away from W if U 2 MGS  and

W \ rngU = ?, and we write MGS [W ] for the p ossibly empty set

of most general solutions to  away from W .

Lemma 30 If U 2 MGS [W ] and S 2 Solutions  , then U 

W [FTV  

S .

Pro of: Since U  S , there is some R such that RU = S .

FTV   FTV  

De ne



Rt if t 2 rngU ;

0

R t =

S t otherwise:

0

If t 2 FTV , then R U t = RU t = S t. And if t 2 W FTV ,

0 0

then t 62 dom  [ rng , so R U t = R t= S t. 2

A uni cation problem is a subtyp e satisfaction problem involving only

equalities. Algorithms for solving uni cation problems are well known; in

particular, wehave the following theorem.

Theorem 31 Let  be a uni cation problem and W be a nite set of type

variables.

i Solutions  = ? i MGS = ? i MGS [W ]= ?.

ii There is an algorithm that decides whether  has a solution, and, if

so, returns an element of MGS [W ].

Pro of: See for example Snyder [31], Lemma 3.3.11. 2

Theorem 32 Every  -satisfaction problem is equivalent to a uni cation

2;1

problem, and moreover, there is an algorithm that transforms every  -

2;1

satisfaction problem into an equivalent uni cation problem.

Corollary 33 Let  bea  -satisfaction problem and W be a nite set of

2;1

type variables.

i Solutions  = ? i MGS = ? i MGS [W ]= ?.

ii There is an algorithm that decides whether  has a solution, and, if

so, returns an element of MGS [W ]. 23

 !    t  9t ;t :ft   ;  t ;t = t ! t g

1 2 1 2 1 1 2 2 1 2

if t ;t are fresh

1 2

 !     !    f   ;   g

1 2 1 2 1 1 2 2

   ^    f   ;   g

1 2 1 2

t    ft =  g

if  is a simple typ e

Figure 6: Transformational rules for  -satisfaction problems

2;1

We will prove Theorem 32 by giving an algorithm that transforms any

 -satisfaction problem into an equivalent uni cation problem. Corol-

2;1

lary 33 follows by combining the transformation with any uni cation algo-

rithm.

Our transformation is de ned by rules of the form

~

    9t:P:

The rules may need to intro duce fresh typ e variables, that is, typ e variables

that do not app ear on the left-hand side. These variables will app ear in

~

the variables t of the right-hand side but they are not the only source of

~

variables in t.

The rules are used to de ne a rewrite relation on problems:

~

    9t:P

0 0

~

9~s:P ]f   g  9~s ] t:P [ P

The op erator `]' is disjoint union; on the right of the consequent, it means

~

that the variables t must b e fresh this can always b e achieved by renaming.

The rules for transforming a  -satisfaction problem into a uni cation

2;1

problem are given in Figure 6.

Pro of of Theorem 32: We show that the rules of Figure 6 constitute an

algorithm for converting any  -satisfaction problem into an equivalent

2;1

uni cation problem.

First, note that every rule transforms a  -satisfaction problem into

2;1

another  -satisfaction problem equalities are b etween simple typ es, in-

2;1

equalities are b etween T and T typ es.

2 1 24

Second, note that each rule preserves the set of solutions, so that each

application of a rule transforms a problem into an equivalent problem.

Third, note that rep eated application of these rules must halt: every

rule reduces the number of typ e constructors `!'or`^' in inequalities or

reduces the numb er of inequalities.

Finally, note that a normal form contains no inequalities, and is therefore

a uni cation problem. 2

Theorem 34 The subtyping relation  is decidable.

2;1

Pro of: To see whether    , compute U 2 MGSf   g and check

2;1

to see whether U is the identity substitution. 2

Decision pro cedures for the other subtyping relations can b e obtained in

a similar way.

Because we so often want to ensure that U 2 MGS ischosen \away"

from a set of typ e variables, we adopt the following convention.

Convention 35 Whenever U 2 MGS  o ccurs in any mathematical con-

text, we assume that U is chosen so that it do es not interfere with \current"

typ e variables, that is, U 2 MGS [W ] where W [ FTV   is the set of

typ e variables present in the context.

3.2 Typ e inference

s

De nition 36 For any term M ,we de ne the set PP M  of pairs by

I

2

induction:

s

i If M = x, then for anytyp e variable t, hfx : tg;ti2 PP x.

I

2

s

ii If M = xN , and hA;  i2 PP N , then:

I

2

a If x 62 domA, and t is a typ e variable not app earing in hA; i,

s

xN . then hA; t !  i2PP

I

2

s

b If x 2 domA, then hA ;Ax !  i2 PP xN .

x I

2

iii If M = M M , then:

1 2

s s

a If hA ;ti2PP M  and hA ; i2PP M  are disjoint, and

1 I 1 2 2 I 2

2 2

U 2 MGSft = t ! t ;  t g where t ;t are fresh, then

1 2 2 1 1 2

s

M M : U hA + A ;t i2PP

1 2 1 2 2 I

2 25

V

s s

b If hA ;  M  for M , and hA ; i2PP   !  i2PP

1 2 1 i i I i 1 I

i2I

2 2

all i 2 I , where all pairs are chosen disjoint, and U 2 MGSf 

i

 j i 2 I g, then

i

X

s

M M : A ; i2 PP U hA +

1 2 i 1 I 1

2

i2I

s

The following lemma establishes that the elements of PP M  are just

I

2

trivial variants of each other. Therefore, the requirement of disjointness

s

is easily satis ed, and De nition 36 can b e used in the de nition of PP

I

2

adapted to a typ e inference algorithm.

Lemma 37

s

i If hA;  i2 PP M , then x 2 domA if and only if x is free in M .

I

2

s s

ii Supp ose hA ; i2 PP M . Then hA ; i2 PP M  if and only if

1 1 I 2 2 I

2 2

there is a bijection R of typ e variables such that RhA ; i = hA ; i.

1 1 2 2

Pro of: An easy induction on De nition 36. 2

Theorem 38 There is an algorithm that decides, for any M , whether the

s s

set PP M  is empty; and furthermore, if PP M  is not empty, it pro-

I I

2 2

s

duces a member of PP M .

I

2

Pro of: Just follow the rules of De nition 36, generating \fresh" typ e vari-

ables as necessary, and use the algorithm of Corollary 33 to compute MGS.

2

Example 39 We showhow the algorithm nds the principal pair of x:xx.

s

i PP x pro duces a pair hfx : t g;t i.

I 1 1

2

s

x again pro duces a pair hfx : t g;t i. ii PP

2 2 I

2

s

iii To calculate PP xx, we nd a most general solution to

I

2

ft  t ;t = t ! t g;

2 3 1 3 4

suchasft := t ;t := t ! t g. Then hfx : t ^ t ! t g;t i2

2 3 1 3 4 3 3 4 4

s

PP xx.

I

2

s

x:xx pro duces h?;t ^ t ! t  ! t i. iv Finally,PP

3 3 4 4 I

2 26

s

. Wenow establish the soundness of PP

I

2

s s

Theorem 40 If hA;  i2 PP M , then hA;  i2 AP M .

I I

2 2

s

M . Pro of: By induction on the de nition of PP

I

2

s

xby i If M = x, then hA;  i = hfx : tg;ti, and wehave hA;  i2AP

I

2

rule var.

ii If M = xN , then by Lemma 37i wehave the following two cases:

0 0

s

a x is not free in N , and  = t !  , where hA;  i2 PP N .

I

2

0

s

By induction and weakening, hA [fx : tg; i2AP N  note

I

2

that A [fx : tg is well-formed by Lemma 37i.

0

s

So by rule abs, hA; t !  i = hA;  i2 AP xN .

I

2

0 0 0 0 0

b x is free in N and hA;  i = hA ;A x !  i, where hA ; i2

x

s

N . PP

I

2

0 0

s s

By induction hA ; i2 AP N , so hA;  i2 AP xN by

I I

2 2

rule abs.

iii If M = M M , then one of the following cases holds:

1 2

s

a hA;  i = U hA + A ;t i, where hA ;ti2 PP M , hA ; i2

1 2 2 1 I 1 2 2

2

s

PP M , and U 2 MGSft = t ! t ;  t g.

I 2 1 2 2 1

2

Then by induction, weakening, and substitutivity,

s

U hA + A ;ti 2 AP M ;

1 2 I 1

2

s

U hA + A ; i 2 AP M :

2 2 2 I 2

2

Since U  Ut ,by Lemma 2ii wehave U = Ut . And

2 2 1 2 1

Ut =Ut  ! Ut , so by rule app wehave U hA + A ;t i2

1 2 1 2 2

s

AP M .

I

2

P

s

M  for all A ; i, where hA ; i2PP b hA;  i = U hA +

2 i 1 i i I 1

i2I

2

V

s

M , and U 2 MGSf    !  i2 PP i 2 I , hA ; 

1 i i 1 I 1

i2I

2

 j i 2 I g.

i

Then by induction, weakening, and substitutivity,

P V

s

U hA + A ;    !  i 2 AP M ;

1 i i 1 I 1

i2I i2I

2

P

s

A ; i 2 AP M  8i 2 I : U hA +

i i I 2 1

i2I

2

By Lemma 2ii and the fact that U  U ,wehave U =

i 2 i i

P

s

A ; i2 AP M . 2 U . Then by app, U hA +

i 1 I i 1

i2I

2 27

s

s

M , then thereis Theorem 41 Principal pairs for I  If hA; i2AP

I

2

2

0 0 0 0

s

M  and a substitution S such that S hA ; i hA;  i. apair hA ; i2 PP

I

2

Pro of: By cases on the structure of M .

s

i If M = x, then hA; i2AP M by rule var, and therefore, Ax=

I

2

V

  and  =  2 T for some i 2 I . 

i i 0 0

0

i2I

s

M . Then ft :=  g isawell-formed For any t, hfx : tg;ti2 PP

I

2

substitution and

ft :=  ghfx : tg;ti = hfx :  g;i hA;  i:

s

ii If M = xN , then by the de nition of I ,  must b e of the form

2

s

 !  , and hA [fx :  g; i2 AP N . By induction, there is a

1 2 x 1 2 I

2

0 0

s

substitution S and pair hA ; i2 PP N  such that

I

2

2

0 0

S hA ; i hA [fx :  g; i: 1

x 1 2

2

We consider two cases.

0 0 0

a If x 62 domA , then for any fresh typ e variable t, hA ;t !  i2

2

s

PP xN .

I

2

V

 , and therefore, we can pick Note that  is of the form 

i 1

i2I

0 0 0

 2 T such that    cho ose any  . Then let S = ft :=

0 1 1 i

1 1

0

 g[ S . By 1 and the de nition of ,

1

0 0 0 0 0 0

i hA ; !  i: ! S i = hSA ; S hA ;t ! 

x 1 2

2 1 2

0 0 0

ihA;  !  i, as de- Since A  A ,wehave S hA ;t ! 

1 2 1 x

2

sired.

0 0 0 0

s

b If x 2 domA , then hA xN . Then ;A x !  i2PP

I

x

2

2

by 1 and the de nition of ,

0 0 0

S hA ;A x !  i hA ; !  i;

x 1 2

x 2

0 0 0

and since A  A ,wehave S hA ;A x !  i hA;  !  i,

1 x 1 2

x

2

as desired.

V

s

iii If M = M M , then by the de nition of I   !  i2 , hA; 

1 2 i

2

i2I

s s

M  for all i 2 I . M  and hA;  i2 AP AP

2 1 i I I

2 2

s

By induction, PP M  is nonempty, and by Lemma 37ii, it is suf-

I 1

2

cient to consider the following cases on the structure of pairs in

s

PP M .

I 1

2 28

s

M . By induction, there is a substitution S such a hA ;ti2 PP

1 1 1 I

2

that

^

  !  i: S hA ;ti  hA; 

i 1 1

i2I

0 0

By the de nition of  , S t =  !  for some i 2 I and  2 T .

2 1 i 0

Then by induction and Lemma 37ii, there is a disjoint pair

s

M  and substitution S such that hA ;i2 PP

2 2 2 I

2

S hA ;i  hA;  i:

2 2 i

Let  = ft = t ! t ;  t g, where t ;t are fresh. Then

1 2 1 1 2

0

S = S [ S [ft :=  ;t :=  g is a solution to  .

1 2 1 i 2

s

Pick U 2 MGS . Then U hA + A ;t i2 PP M M .

1 2 2 I 1 2

2

By Convention 35, there exists an R such that RU hA + A ;t i =

1 2 2

S hA + A ;t i. And

1 2 2

0

S hA + A ;t i = hS A + S A ; i hA;  i;

1 2 2 1 1 2 2

as desired.

V

0 0

s

 b hA ;   !  i2 PP M .

1 I 1

j

j 2J

2

By induction there is a substitution S such that

1

^ ^

0 0

 !  i  hA;    !  i: S hA ;  

i 1 1

j

i2I j 2J

0

By the de nition of  , fS  j j 2 J g f j i 2 I g, so for all

2 1 i

j

0

j 2 J there is an i 2 I such that S  =  .

j 1 i

j

j

By induction and Lemma 37ii, for all j 2 J there are disjoint

s

M  and substitutions S such that pairs hA ; i2PP

2 j j j I

2

i: S hA ; i  hA; 

j j j i

j

S

0

j j 2 J g. Then S = S [  S  is a solution Let  = f  

1 j j

j

j 2J

0 0

. = S to  : S = S    = S 

j j j 2 i 1

j

j j

Pick U 2 MGS . Then

X

0

s

A ; i2 PP U hA + M M :

j I 1 1 2

2

j 2J 29

By Convention 35, there exists an R such that

X X

0 0

RU hA + A ; i = S hA + A ; i:

1 j 1 j

j 2J j 2J

And

X X

0 0

S A ;S  i hA;  i; A ; i = hS A + S hA +

j j 1 j 1 1 1

j 2J j 2J

as desired.

2

4 Other systems of rank 2 intersection typ es

s

4.1 A restriction of I

2

Van Bakel [33] de ned a rank 2 intersection typ e system that is a slight

s

restriction of our system I .Aversion of his rules is presented b elow.

2

var fx :  g` x :  where  2 T 

0

A [fx :  g` M : 

x 1 2

abs

A ` xM : ! 

x 1 2

V

  ! ; 8i 2 I  A ` N :  A ` M :

i i i

i2I

P

app

A + A ` MN: 

i

i2I

vb

We write I .A ` M :  if the judgment A ` M :  follows by these

2

rules, under the following restrictions: environmenttyp es are in T ; derived

1

typ es are in T ; and in every judgment A ` M :  , the typ e environment A

2

contains only assumptions actually used in the derivation of A ` M :  .

For example, the rule var has b een intentionally restricted to rule out a

judgment suchas

fx :  ^  g`x :  ;

1 2 1

in which the typ e  assumed for x is not used. Similarly, fx :  ;y :  g`

2 1 2

x :  is not derivable b ecause the assumption y :  is not used. The exact

1 2

vb s

relation b etween I and I is summed up in the following lemma.

2 2

vb s

Theorem 42 Comparison of I and I 

2 2

vb s

i If I .A ` M :  , then I .A` M :  . The converse do es not hold.

2 2 30

vb s

ii A term M is typable in I if and only if it is typable in I .

2 2

Pro of:

vb s

i Just note that the I rule var is a sp ecial case of the I rule var,

2 2

vb s

that the I rule abs is identical to the I rule abs, and that the

2 2

vb s

I rule app follows from the I rule abs and weakening.

2 2

The examples ab ove show that the converse do es not hold.

ii This follows b ecause the de nition of principal pair in van Bakel's

system is identical to our own.

2

s

4.2 An extension of I

2

s

A natural extension of I is obtained by adding the rule of subsumption to

2

s

the rules of I :

2

A ` M : 

sub where    

2

A ` M : 

s

We write I .A` M :  if the judgment A ` M :  follows by the rules of I

2

2

plus sub, with typ es app earing in typ e environments restricted to T , and

1

derived typ es restricted to T .

2

s

Clearly,every judgmentofI is a judgmentofI . The converse do es not

2

2

hold; for example, the judgment

0

fx :  !  g`x : ^   ! 

0 s

is derivable in I for any  6=  2 T , but is not derivable in I .

2 0

2

I has principal pairs, and indeed, they are identical to the principal

2

s

pairs of I the pro of is a simple extension of the pro of of Theorem 41. An

2

immediate consequence is that the terms typable in I are exactly the same

2

s

as the terms typable in I .

2

In summary:

s

Theorem 43 Comparison of I and I 

2

2

s

i If I .A ` M :  , then I .A ` M :  . The converse do es not hold.

2

2

s

ii A term M is typable in I if and only if it is typable in I .

2

2

s

Although it do es not typ e any more terms than I , I has other advantages

2

2

s

over I .

2 31

s

Example 44 The acceptable pairs of I are not closed under the op era-

2

tion :

s

I . fx : s ! tg`x : s ! t;

2

and

hfx : s ! tg;s ! ti  hfx : s ! tg; s ^ t ! ti;

but the judgment

fx : s ! tg`x :s ^ t ! t

s

is not derivable in I .

2

On the other hand, I is closed under :

2

0 0

Lemma 45 Weakening for I  If I .A` M :  and hA; i hA ; i,

2 2

0 0

then I .A ` M :  .

2

vb s

For this reason, we prefer I to either I or I .However, it was still useful

2

2 2

s

to develop I . In particular, the example ab ove shows that Lemma 19 do es

2

not hold for I ;itwas convenienttohave Lemma 19 for the pro of of the

2

s

equivalence of typability with  .

2

5 Combining intersections and quanti cation

5.1 The system P

2

Wenow describ e a typ e system that combines asp ects of rank 2 intersection

typ es and rank 2 p olymorphic typ es. The system is called P ,asitisthe

2

rank 2 subset of a typ e system P describ ed elsewhere.

The typ es of the system are the rank 2 intersection typ es extended with

top-level quanti ers:

T = T [f8t  j  2 T g:

82 2 82

In order to simplify the de nition of subtyping, we consider T typ es

82

syntactically equal mo dulo renaming of b ound typ e variables, reordering of

adjacent quanti ers, and elimination of unnecessary quanti ers. When a

T typ e is written in the form 8~s ,we assume  2 T .

82 2

De nition 46

i The relation  is the least partial order on T closed under the

82 82

following rules: 32

a If    , then    .

2 82

b If  2 T , then 8t   ft :=  g .

0 82

c If    and t is not free in  , then   8t .

82 82

ii The relation  between T and T is the least relation closed

82;1 82 1

under the rule:

V

a If    for all i 2 I , then     .

82 i 82;1 i

i2I

The rules for  express the intuition that a typ e is a subtyp e of its in-

82

stances. They are equivalent to the following rule, similar to ML's notion of

generic instance :

 If f~s := ~g   , where ~ is a vector of simple typ es, and the typ e

2

~ ~

variables t are not free in 8~s , then 8~s  8t .

82

Note that we only allow instantiation of simple typ es. This ensures that

instantiation do es not takeusbeyond rank 2. It also has less desirable

implications, e.g., 8t:t is not a least typ e in the ordering  :8t:t 6

82 82

s ^ s ! s  ! s .

1 1 2 2

The relation  is not a partial order; it is not even re exive. This is

82;1

b ecause it relates typ es \across rank." Note that in a comparison

^

 ; 8t   

i 82;1

i2I

the variable t may b e instantiated di erently for each  .

i

Some basic prop erties of  and  are summarized in the following

82 82;1

lemma.

Lemma 47

i If ; 2 T , then    i    i  =  .

0 82 82;1

ii If ; 2 T , then    i    .

2 82 2

iii If    , then 8t   8t .

82 82

~

iv If  2 T and  2 T , then 8t   i for some substitution S with

2 0 82

~

domS  t ,wehave S =  .

v For any substitution S and typ es ; 2 T ,ifS   , then

82 82

S 8t    .

82

vi For any substitution S ,typ es ; 2 T , and typ e environment A,if

82

S   , then S GenA;     .

82 82 33

V

var fx :  g`x :  where i 2 I 

i i 0

0

i2I

A [fx :  g` M : 

abs

A ` xM : ! 

V

A ` M :   ! ; 8i 2 I  A ` N : 

i i

i2I

app

A ` MN:

A ` M : 

gen where t 62 FTVA

A ` M : 8t

A ` M : 

sub where    

82

A ` M : 

A ` M : 

add-hyp

A [fx :  g`M : 

Figure 7: Typing rules of P .Typ es in typ e environments are in T , and

2 1

derived typ es are in T .

82

vii If        , then    .

1 82 2 82;1 3 1 4 1 82;1 4

The typing rules of the system are presented in Figure 7. We write

P .A` M :  if the judgment A ` M :  follows by these rules, with

2

typ es app earing in typ e environments restricted to T , and derived typ es

1

restricted to T .

82

The rules di er from the rules of I in two resp ects. First, wehave added

2

the rule gen so that we can derive quanti ed typ es, and  is used in

82

place of  in the rule sub so that the quanti ers can b e instantiated.

2

Second, wehave added the rule add-hyp, whichwas a derived rule in

all of our previous typ e systems. The addition of add-hyp will simplify

some subsequent de nitions, and also allows us to weaken the rules var

and abs slightly. The old var and abs are derivable in P ,soP

2 2

extends I :

2

Lemma 48 If I .A` M :  , then P .A ` M :  .

2 2

In fact, a stronger connection can b e shown: a term is typable in one

system if and only if it is typable in the other. 34

~ ~

Theorem 49 Comparison of P and I P .A` M : 8t for some t

2 2 2

if and only if I .A ` M :  .

2

Pro of: The right-to-left direction follows by Lemma 48. The left-to-right

direction is proved by induction on derivations; the only non-trivial case is

sub, which can b e shown as follows.

~

If P .A` M : 8t follows by rule sub, then wemust have a shorter

2

derivation of

P .A ` M : 8~s ;

2

~

and 8~s   8t . Wemust show I .A ` M :  .

82 2

By induction, I .A ` M :  .Furthermore, by the de nition of  , for

2 82

some sequence ~ of simple typ es, wehave f~s := ~g   .Wemay assume

2

that the typ e variables ~s do not app ear in A. Then by substitutivity for I ,

2

I .A ` M : f~s := ~g;

2

and by the I rule sub,wehave I .A` M :  , as desired. 2

2 2

The ordering  of De nition 28 is extended to pairs with T typ es as

82

follows:

0 0 0 0

hA; i hA ; i if and only if A  A and    .

1 82

0 0

Lemma 50 Weakening for P  If P .A ` M :  and hA;  i hA ; i,

2 2

0 0

then P .A ` M :  .

2

Lemma 51 Substitutivity for P  If P .A` M :  , then P .SA `

2 2 2

M : S for any substitution S .

5.2 Extending subtyp e satisfaction

In order to p erform typ e inference for P ,we will need to solve problems

2

that generalize the  -satisfaction problems of x3.1.

2;1

A  -satisfaction problem  is a pair 9~s:P , where P is a nite set

82;1

whose every element is either: 1 an equalitybetween simple typ es; or 2

an inequalitybetween a T typ e and a T typ e. A substitution S is a

82 1

0 0

solution to 9~s:P if there is a substitution S such that S t= S t for all

0 0 0 0

t 62 ~s, S   S  for all inequalities     2 P , and S  = S  for all

82;1

equalities  =   2 P .

Note that any  -satisfaction problem is a  -satisfaction problem

2;1 82;1

with the same set of solutions. Therefore we abuse notation and write 35

Solutions  , MGS , and MGS [W ] for the solutions, most general

solutions, and most general solutions away from W of a  -satisfaction

82;1

problem  .

Similarly,  -satisfaction problems can b e solved by extending the

82;1

transformational algorithm of Figure 6 by the following rule:

8t     9tf   g

if  is not a ^-typ e, and t is not free in 

Theorem 52 Every  -satisfaction problem is equivalent to a uni cation

82;1

problem, and moreover, there is an algorithm that transforms every  -

82;1

satisfaction problem into an equivalent uni cation problem.

Pro of: We show that the rules of Figure 6, augmented by the rule ab ove,

constitute an algorithm for converting any  -satisfaction problem into

82;1

an equivalent uni cation problem equalities are b etween simple typ es, in-

equalities are b etween T and T typ es.

82 1

First, note that every rule transforms a  -satisfaction problem into

82;1

another  -satisfaction problem.

82;1

Second, note that each rule preserves the set of solutions, so that each

application of a rule transforms a problem into an equivalent problem.

Third, note that rep eated application of these rules must halt: every rule

reduces the number of typ e constructors `!', `^', or `8' in inequalities or

reduces the numb er of inequalities.

Finally, note that a normal form contains no inequalities, and is therefore

a uni cation problem. 2

Corollary 53 Let  bea -satisfaction problem and W be a nite set

82;1

of type variables.

i Solutions  = ? i MGS = ? i MGS [W ]= ?.

ii There is an algorithm that decides whether  has a solution, and, if

so, returns an element of MGS [W ].

Theorem 54 The subtyping relation  is decidable.

82;1

Pro of: To see whether    , compute U 2 MGSf   g and check

82;1

to see whether U is the identity substitution. 2 36

5.3 Typ e inference for P

2

De nition 55 For any term M ,we de ne the set PP M by induction

P

2

on M .

i If M = x, then hfx : tg;ti2PP x for anytyp e variable t.

P

2

ii If M = xN , and hA; 8~s i2PP N , where the typ e variables ~s are

P

2

distinct from all other typ e variables, then:

a If x 62 domA, and t is a fresh typ e variable, then

hA; 8t~st !  i2 PP xN :

P

2

b If x 2 domA, then hA ; GenA ;Ax !  i2 PP xN .

x x P

2

iii If M = M M , and hA ; 8~s i2 PP M , then:

1 2 1 1 P 1

2

a If  = t a typ e variable, t and t are fresh typ e variables, the

1 1 2

typ e variables of hA ; i2 PP M  are fresh, U 2 MGSf 

2 2 P 2 2

2

t ;t = t ! t g, and A = U A + A , then

1 1 2 1 2

hA; GenA; U t i2PP M :

2 P

2

V

  !  ,8i 2 I  the typ e variables of hA ; i2 b If  =

i i i 1

i2I

PP M  are fresh, U 2 MGSf   j i 2 I g, and A =

P 2 i i

2

P

A , then U A +

i 1

i2I

hA; GenA; U  i2 PP M :

P

2

s

Just as with I , the elements of PP M  are trivial variants of each

P

2 2

other, so De nition 55 can easily b e adapted to a typ e inference algorithm.

Lemma 56

i If hA;  i2 PP M , then x 2 domA if and only if x is free in M .

P

2

M  if and only if M . Then hA ; i2 PP ii Supp ose hA ; i2 PP

2 2 P 1 1 P

2 2

there is a bijection R of typ e variables such that RhA ; i = hA ; i.

1 1 2 2

Pro of: An easy induction on De nition 55. 2

Theorem 57 There is an algorithm that decides, for any M , whether the

set PP M  is empty; and furthermore, if PP M  is not empty, it pro-

P P

2 2

duces a member of PP M .

P

2 37

Pro of: Just follow the rules of De nition 55, generating \fresh" typ e vari-

ables as necessary, and use the algorithm of Corollary 53 to compute MGS.

2

Theorem 58 If hA; i2PP M , then hA;  i2 AP M .

P P

2 2

Pro of: By induction on the de nition of PP M .

P

2

i If M = x, then hA;  i = hfx : tg;ti for some typ e variable t.

Then wehave hA;  i2 AP xby rule var.

P

2

ii If M = xN , then by Lemma 56i wemust consider two cases:

0 0

a It x is not free in N , then hA; 8~s i2 PP N  for some  , and

P

2

0

 = 8t~s t !   for some fresh typ e variable t.

0

By induction, hA; 8~s i2AP N , and byweakening,

P

2

0

hA [fx : tg; i2AP N :

P

2

Note that A [fx : tg is well-formed by Lemma 56i.

0

By rule abs, hA; t !  i2 AP xN , and by gen,

P

2

0

hA; 8t~st !  i = hA;  i2 AP xN :

P

2

0 0 0 0

b If x is free in N , then hA;  i = hA ; GenA ;A x !  i, where

x x

0 0

hA ; 8~s i2 PP N .

P

2

0 0

By induction and rule sub, hA ; i2 AP N , so by rule abs,

P

2

0 0 0

hA ;A x !  i2 AP xN . Then by gen,

P

x

2

0 0 0 0

hA ; GenA ;A x !  i = hA; i2 AP xN :

P

x x 2

iii If M = M M , then wehave hA ; 8~s i2 PP M . By induction,

1 2 1 1 P 1

2

M . M , and by sub, hA ; i2 AP hA ; 8~s i2 AP

1 1 1 1 P 1 1 P

2 2

a If  isatyp e variable t, then wemust have a pair hA ; i2

1 2 2

PP M  with fresh typ e variables, A = U A + A  and  =

P 2 1 2

2

GenA; U t , where U 2 MGSf  t ;t = t ! t g for fresh

2 2 1 1 2

typ e variables t and t .

1 2

By induction, hA ; i2 AP M . By substitutivity,

2 2 P 2

2

U hA ; i = hUA ; Ut  ! Ut i2AP M 

1 1 1 1 2 P 1

2 38

and

U hA ; i = hUA ;U i2AP M :

2 2 2 2 P 2

2

By weakening,

hUA + UA ; Ut  ! Ut i2AP M 

1 2 1 2 P 1

2

and

hUA + UA ;Ut i2AP M :

1 2 1 P 2

2

Then by rule app wehave

hUA + UA ;Ut i = hA; U t i2 AP M M ;

1 2 2 2 P 1 2

2

and by rule gen,

hA; GenA; U t i = hA; i2AP M M :

2 P 1 2

2

V

b If  =   !  , then wemust have pairs hA ; i2 PP M 

1 i i i P 2

2

i2I

with fresh typ e variables, U 2 MGSf   j i 2 I g, A =

i i

P

U A + A , and  = GenA; U  .

1 i

i2I

By induction and substitutivity, hUA ;U i2AP M  for all

i i P 2

2

i 2 I , and by substitutivitywehave

^

hUA ;U i = hUA ;  U  ! Ui2AP M :

1 1 1 i P 1

2

i2I

V

U  ! Ui2AP M  and hA; U  i2 By weakening, hA; 

i P 1 i

2

i2I

AP M  for all i 2 I .

P 2

2

Then by rules app and gen wehave

M : hA; GenUA;Ui = hA;  i2 AP

P

2

2

Theorem 59 Principal pairs for P  If hA; i2AP M , then there

2 P

2

0 0 0 0

isapair hA ; i2PP M  and a substitution S such that S hA ; i

P

2

hA;  i.

Pro of: By induction on the de nition of AP M .

P

2 39

i If hA;  i2AP M by rule var, then M = x for some variable x,

P

2

V

2 T for some i 2 I . Ax=   , and  = 

0 0 i i

0

i2I

M , where t is a fresh , hfx : tg;ti2 PP By the de nition of PP

P P

2 2

typ e variable.

Then ft :=  g is a well-formed substitution and

ft :=  ghfx : tg;ti = hfx :  g;i hA;  i:

ii If hA; i2 AP M by rule abs, then M = xN ,  is of the form

P

2

 !  , and hA [fx :  g; i2 AP N .

1 2 1 2 P

2

0 0 0

By induction, there is a substitution S and pair hA ; 8~s i2PP N 

P

2 2

such that

0 0 0

S hA ; 8~s ihA [fx :  g; i: 2

1 2

2

0

a If x 62 domA , then for any fresh typ e variable t,

0 0

hA ; 8t~st !  i2PP xN :

P

2

2

It remains to show that there is a substitution S such that

0 0

S hA ; 8t~st !  i hA;  i:

2

0 0 0

Just let S = S . By 2, wehave A  S A ,sowe only need

1

show

0 0

S 8t~st !     !  :

82 1 2

2

We can assume t; ~s are fresh, so that

0 0 0 0

S 8t~st !   = 8t~s t ! S  :

2 2

And by 2,

0 0 0 0

ft :=  g8~st ! S  = 8~s ! S     !  ;

1 1 82 1 2

2 2

0 0

so by the de nition of  , S 8t~s t !     !  as

82 82 1 2

2

desired.

0 0 0 0 0

i2 PP xN . b If x 2 domA , then hA ; GenA ;A x ! 

P

2 x x

2

Then by 2 and the de nition of ,

0 0 0 0 0

S hA ; GenA ;A x !  i hA;  !  i;

1 2

x x 2

as desired. 40

iii If hA; i2 AP M by rule app, then M = M M , and wehave

P 1 2

2

V

M  for all i 2 I . M  and hA;  i2AP hA;    !  i2 AP

2 1 i P i P

2 2

i2I

M  is nonempty, and by Lemma 56ii, it is By induction, PP

1 P

2

sucient to consider the following cases on the structure of pairs in

PP M .

P 1

2

a hA ; 8~st i2 PP M .

1 P 1

2

We assume that the typ e variables ~s are fresh, so by induction

and the de nition of  , there is a substitution S such that

82 1

^

S hA ;ti  hA;    !  i:

1 1 i

i2I

0

By the de nition of  , S t = !   for some i 2 I and

82 1 i 0

0

0 0

 2 T with    .

0 82

By induction and Lemma 56ii, there is a disjoint pair hA ;i2

2

PP M  and substitution S such that

P 2 2

2

S hA ;i  hA;  i:

2 2 i

0

Let  = f   t ;t = t ! t g, where t ;t are fresh. Note that

1 1 2 1 2

0

 has a solution, S = S [ S [ft :=  ;t :=  g. Therefore,

1 2 1 i 2

0

0

wemay pick U 2 MGS  and let A = U A + A , so that

1 2

0 0

hA ; GenA ;Ut i2 PP M M :

2 P 1 2

2

By Convention 35, there exists an R such that RU t = St =

2 2

0

   , and

82

0

A  S A + S A = S A + A = RU A + A = RA :

1 1 1 2 2 1 2 1 2

By Lemma 47vi, RGenUt    ,so

2 82

0 0

RhA ; GenA ;Ut i hA;  i;

2

as desired.

V

0 0

 b hA ; 8~s M .  !  i2PP

1 1 P

2

j

j 2J

We assume that the typ e variables ~s are fresh, so by induction

and the de nition of  , there is a substitution S such that

82 1

^ ^

0 0

 !  i  hA;    !  i: S hA ;  

i 1 1

j

i2I j 2J 41

0

Then fS  j j 2 J g f j i 2 I g, so for all j 2 J there is an

1 i

j

0

. i 2 I such that S  = 

j 1 i

j

j

By induction and Lemma 56ii, for all j 2 J there are disjoint

pairs hA ; i2PP M  and substitutions S such that

j j P 2 j

2

S hA ; i  hA;  i:

j j j i

j

S

0

Let  = f   j j 2 J g. Then S = S [  S  is a solution

j 1 j

j

j 2J

0 0

to  : S = S    = S  = S .

j j j 2 i 1

j j j

P

0

Pick U 2 MGS , and let A = U A + A . Then

1 j

j 2J

0 0 0

hA ; GenA ;U i2 PP M M :

P 1 2

2

0 0

By Convention 35, there exists an R such that RU  = S =

0

S    , and

1 82

X X X

0

A  S A + S A = S A + A = RU A + A = RA :

1 1 1 j j 1 j 1 j

j 2J j 2J j 2J

0

By Lemma 47vi, RGenU    ,so

82

0 0 0

RhA ; GenA ;U i hA;  i;

as desired.

0

iv If hA; i2AP M by rule gen, then  = 8t ,wehave a shorter

P

2

0

derivation of hA;  i2AP M , and t 62 FTVA.

P

2

0 00

By induction there is a pair hA ; i2 PP M  and a substitution S

P

2

0 00 0

such that S hA ; i hA;  i.

00 00 0

Wenow show that t 62 FTVS . Then since S   ,wehave

82

00 0

S  8t =  , as desired.

82

00 0

Assume byway of contradiction that t 2 FTV S . Since A  SA ,

1

0 0

FTVSA  FTVA. Therefore, t 62 FTV A  t 62 FTVSA .

0 00

Since t 62 FTVSA  and t 2 FTVS , there must b e some u 2

00 0

FTV  FTVA  such that t 2 FTVSu. However, it is easily

00 0 0 00

M   FTV   FTVA = ?,sowe checked that hA ; i2 PP

P

2

have reached a contradiction.

0

v If hA;  i2AP M by rule sub, then for some    ,wehave

P 82

2

0

a shorter derivation of hA;  i2 AP M .

P

2

0 00

By induction there is a pair hA ; i2 PP M  and a substitution S

P

2

0 00 0

such that S hA ; i hA;  i.

00

Then by transitivity, S   as desired.

82 42

vi If hA;  i2 AP M by rule add-hyp, then A = A [fx :  g and

P x

2

M . hA ;i2 AP

x P

2

0 0

M  and a substitution S By induction there is a pair hA ; i2PP

P

2

0 0

such that S hA ; ihA ;i.

x

0 0

Then S hA ; i hA;  i as desired.

2

6 Recursive de nitions

Wenow consider ways of typing recursive de nitions. We extend the gram-

mar of our language to include terms of the form xM . Such a term

is meant to represent the recursive program x such that x = M M may

contain o ccurrences of x.

In ML, recursive de nitions are typ ed by the following rule:

A [fx :  g`M : 

x

rec-simple where  2 T 

0

A ` xM :

6.1 Recursive de nitions in 

2

In  and ML, the rule rec-simple seems overly restrictive. Both systems

2

allowMLtyp e schemes to app ear in typ e environments and as derived typ es,

suggesting the rule of polymorphic recursion :

A [fx :  g` M : 

x

rec-poly where  2 S1

A ` xM : 

Example 60 When extended by rec-poly, b oth ML and  can typ e

2

the following terms:

w :xy :y ww : 8t:t ! t;

w :xyz:zw 3w true : 8t:t ! t;

x:xx : 8t:t:

Neither is typable with the rule rec-simple. Other examples are given

by Mycroft [25] and Kfoury et al. [13,15], who intro duced rec-poly

indep endently.

Unfortunately,typ e inference for  or ML extended by rec-poly is

2

undecidable [14, 9], so rec-simple is used in practice. 43

6.2 Recursive de nitions in I

2

The rule rec-simple is one wayoftyping recursive de nitions in intersec-

tion typ e systems. However, as with ML and  , it seems overly restrictive.

2

The rule rec-poly involves S1 typ es, so it is not appropriate for the

intersection typ e systems. Instead, we might consider a rule like the follow-

ing:

A [fx :  g` M : 

x

where  2 T 

1

A ` xM : 

Note that the full p ower of the rule is achieved only by allowing T derived

1

typ es, so the rule is not compatible with the rank 2 intersection typ e systems

that wehave de ned so far. However, the rule can b e adapted to our systems

as follows:

V

8i 2 I  A [fx :  g`M : 

x j i

j 2I

where i 2 I  rec-int

0

A ` xM :

i

0

Example 61 The system I + rec-int can typ e the following terms:

2

w :xy :y ww :  ! ;

w :xyz:zw 3w true :  ! ;

where  is any simple typ e. Neither term is typable in I + rec-simple .

2

The close connection b etween I and  casts some doubt on the de-

2 2

cidability of the system I + rec-int.However, I + rec-int cannot

2 2

typ e all of the terms that can b e typ ed by + rec-poly .For example,

2

the term x:xx cannot b e typ ed in I + rec-int. The decidabilityof

2

I + rec-int is an op en question.

2

6.3 Recursive de nitions in P

2

The system P could b e extended to typ e recursive de nitions with either

2

the rule rec-simple or the rule rec-int the rule rec-poly is not

appropriate since it requires S1 typ es to app ear in typ e environments.

Surprisingly,however, we can do b etter: by using the rule rec b elow, we

will b e able to typ e more terms than rec-simple, while retaining principal

typings and decidable typ e inference.

A [fx :  g`M : 

rec where    

82;1

A ` xM : 44

Example 62

i The following terms are typable in P +rec, but are not typable in

2

P + rec-simple :

2

w :xy :y ww : 8t:t ! t;

w :xyz:zw 3w true : 8t:t ! t:

ii The term x:xx is not typable in P +rec. It has typ e 8t:tin

2

ML+rec-poly and  +rec-poly.

2

There is an anomaly in the rule rec:ifx do es not app ear in M , and M

do es not have a simple typ e, then xM  will not b e typable with rec.

For example, the term xy :y y  is not typable b ecause y :y y  is only

typable at rank 2; there is no T typ e that could b e assigned to x so that

1

rec applies.

This could b e repaired by adding a sp ecial rule for the vacuous case:

A ` M : 

where x 62 domA rec-vac

A ` xM :

Using rule rec-vac,wemay derive the typing

w x:xx : 8s; t:s ^ s ! t ! t:

However, to simplify our de nitions we will not consider rec-vac further.

6.4 Mutual recursion in P

2

Wenow extend the grammar of our language to include terms of the form

letrec B in N , where B is a set of mutually recursive de nitions. A

particular B may b e written as x = M ;x = M ;:::;x = M or fx =

1 1 2 2 n n i

M j i 2 I g, where all of the variables x are distinct.

i i

Figure 8 gives three rules for typing mutually recursive de nitions. The

rst rule, letrec-simple, is used for typing mutual recursion in ML. No-

tice that in letrec-simple, the recursive de nitions must b e typ ed under

the assumption that the recursivevariables have simple typ e. In typing the

b o dy of the de nition, however, the typ es of the recursivevariables can b e

generalized.

We cannot use letrec-simple with P , b ecause P do es not p ermit

2 2

quanti ed typ es to app ear in typ e environments. And it is not easily adapted

to P . In ML, the p olymorphic typ e GenA;  ofx is easily obtained from

2 i i

the simple typ e  used in typing the recursive de nitions. The equivalentof

i 45

8j 2 I  A [fx :  j i 2 I g`M :   2 T 

i i j j j 0

A [fx : GenA;   j i 2 I g`M : 

i i

letrec-simple

A ` letrec fx = M j i 2 I g in M :

i i

8j 2 I A [fx :  j i 2 I g`M :     

i i j j j 82;1 j

letrec-var i 2 I

0

: A ` letrec fx = M j i 2 I g in x

i i i i

0 0

A [fx :  j i 2 I g`N : 

i i

8j 2 I A ` letrec fx = M j i 2 I g in x :

^ i i j j

N 62 fx j i 2 I g letrec

i

A ` letrec fx = M j i 2 I g in N :

i i

Figure 8: Rules for typing recursive de nitions

V

 , where each  is an instance GenA;  in P is some intersection 

j j i 2

j 2J

of GenA;  . It is not immediately clear how to get directly from  to

i i

V

 . 

j

j 2J

Instead, we use the following prop ertyof letrec-simple as a guide

in formulating the rules for P .

2

De nition 63 If B = fx = M ;x = M ;:::;x = M g, then we de ne

1 1 2 2 n n

hhletrec B in N ii to b e the term

let x =letrec B in x ;

1 1

.

.

.

x =letrec B in x 

n n

in N :

Lemma 64 If M =letrec B in N , then in ML + letrec-simple ,

A ` M :  i A `hhM ii :  .

The lemma shows howtotyp e any term letrec B in N  given only typ-

ings for terms letrec B in x, where x is de ned in B . This is the intuition

b ehind the rules letrec-var and letrec of Figure 8. letrec-var

is a straightforward generalization of the rule rec for terms of the form

letrec B in x, where x is a variable de ned in B . The ab ove lemma

suggests that wetyp e other letrec expressions by a rule of the form

A `hhletrec B in N ii : 

N is not de ned by B 

A ` letrec B in N :

Of course, let expressions are not de ned in P . Therefore, in any lan-

2

guage except ML, we will regard let x = M in N assyntactic sugar 46

for xN M , and let x = M ;x = M in N assyntactic sugar for

1 1 2 2

let x = M in let x = M in N .

1 1 2 2

Our rule letrec is obtained simply by desugaring the let expression

hhM ii into abstractions and applications, and considering how the resulting

term would b e typ ed by abs and app.We make the rule more compact

V

by using the notation A ` M :   to abbreviate 8i 2 I  A ` M :  .

^ i i

i2I

R

We write P .A` M :  if the judgment A ` M :  follows by

2

the rules of P and the rules rec, letrec-var, and letrec, with

2

typ es app earing in typ e environments restricted to T , and derived typ es

1

restricted to T .

82

R R

Lemma 65 Weakening for P  If P .A ` M :  and hA;  i

2 2

0 0 0 0 R

.A ` M :  . hA ; i, then P

2

R R R

Lemma 66 Substitutivity for P  If P .A ` M :  , then P .SA`

2 2 2

M : S for any substitution S .

R R

Lemma 67 If M =letrec B in N , then P .A ` M :  i P .A `

2 2

hhM ii :  .

R

6.5 Typ e inference in P

2

R

We de ne the typ e inference algorithm for P , and show that it is sound

2

and complete.

De nition 68 The set PP M  of principal pairs for a term M is de ned

R

P

2

by extending the de nition of PP De nition 55 by the following cases.

P

2

iv If M =xN  and hA; i2PP N , then:

R

P

2

a If x 62 domA, and U 2 MGSf  tg where t is a fresh typ e

variable,

then hUA; GenUA;Ui2 PP M .

R

P

2

b If x 2 domA and U 2 MGSf  Axg,

then hUA ; GenUA ;Ui2 PP M .

R

x x

P

2

, where i 2 I , v If M =letrec fx = M j i 2 I g in x

0 i i i

0

M  for i 2 I , and hA ; i2 PP

R

i i i

P

2

P

0

A = A ,

i

i2I

00 0 0

A = A [fx : t j i 2 I; x 62 domA ;t freshg,

i i i i

00

U 2 MGSf  A x  j i 2 I g,

i i

00

and A = UA ,

fx ji2I g

i 47

then hA; GenA; U  i2PP M .

R

i

0

P

2

vi If M =letrec fx = M j i 2 I g in N , where N 62 fx j i 2 I g, and

i i i

hA; i2PP hhM ii,

R

P

2

then hA;  i2 PP M .

R

P

2

Theorem 69 If hA; i2PP M , then hA;  i2 AP M .

R R

P P

2 2

Pro of: By induction on the de nition of PP M . For the rules of P ,

R

2

P

2

see the pro of of Theorem 58. We only need to consider the following cases.

iv If M =xN , we consider two cases.

0 0

a If x is not free in N , then for some hA ; i2 PP N , fresh

R

P

2

0

typ e variable t, and U 2 MGSf  tg,

0 0 0

hA;  i = hUA ; GenUA ;U i:

0 0

By induction and substitutivity, hUA ;U i2AP N . By

R

P

2

0 0

weakening, hUA [fx : Utg;U i2 AP N . And by the rules

R

P

2

rec and gen,

0 0 0

hUA ; GenUA ;U i2AP xN ;

R

P

2

as desired.

0 0

N  and U 2 b If x is free in N , then for some hA ; i2PP

R

P

2

0 0

MGS  A x, wehave

0 0 0

hA;  i = hUA ; GenUA ;U i:

x x

0 0 0 0

By induction, hA ; i2AP N . Then hUA ;U i2 AP N 

R R

P P

2 2

0 0

by substitutivity. Since U  UA x, by rule rec wehave

82;1

0 0

;U i2 AP xN . Finally by rule gen, hUA

R

x

P

2

0 0 0

xN : hUA ; GenUA ;U i2 AP

R

x x

P

2

 where i 2 I , v If M =letrec fx = M j i 2 I g in x

0 i i i

0

000 000

i, where then hA;  i = hA ; GenA ;U

i

0

M  for i 2 I , hA ; i2 PP

R

i i i

P

2

P

0

A = A ,

i

i2I

00 0 0

A = A [fx : t j i 2 I; x 62 domA ;t freshg

i i i i

00

U 2 MGSf  A x  j i 2 I; g,

i i

000 00

and A = UA ,

fx ji2I g

i 48

By induction, hA ; i2 AP M  for i 2 I .

R

i i i

P

2

00

By weakening and substitutivity, hUA ;U i2AP M  for i 2 I .

R

i i

P

2

000

Then by rule letrec-var, hA ;U i2 AP M , and by gen,

R

i

0

P

2

000 000

hA;  i = hA ; GenA ;U i2 AP M .

R

i

0

P

2

vi If M =letrec x = M j i 2 I in N  where N 62 fx j i 2 I g, the

i i i

result follows by Lemma 67 and induction.

2

R

Theorem 70 Principal pairs for P M , then there  If hA; i2 AP

R

2

P

2

0 0 0 0

M  and a substitution S such that S hA ; i is a pair hA ; i2PP

R

P

2

hA;  i.

M . For the rules of P , Pro of: By induction on the de nition of AP

R

2

P

2

see the pro of of Theorem 59. We only need to consider the following cases.

vii If hA;  i2AP M by rule rec, then M =xN , and for some

R

P

2

N  and    .  2 T ,wehave hA [fx :  g;i2 AP

R

82;1 1

P

2

0 0

By induction, wehave a pair hA ; i2 PP N  and a substitution S

R

P

2

such that

0 0

S hA ; i hA [fx :  g;i: 3

We consider two cases.

0 0 0

a If x 62 domA , let t b e fresh and  = f  tg.Now S 

82

0

   by 3, and since  2 T , there must b e some  2 T

82;1 1 0

0 0 0 0 0

such that    . Then S   ,soS = S [ft :=  g is a

1 82;1

solution to  , and wemay pick U 2 MGS . Then

0 0 0

xN : hUA ; GenUA ;U i2 PP

R

P

2

0 0 0

By Convention 35, there exists an R such that RU A = S A =

0 0 0 0

SA and RU  = S  = S   .Furthermore by Lemma 47vi

82

0 0

wehave RGenUA ;U    , so that

82

0 0 0

RhUA ; GenUA ;U ihA; i;

as desired. 49

0 0 0 0

b If x 2 domA , and  = A x, then by 3, S   

82 82;1

0 0 0

  S ,soS is a solution to  = f   g.

1

Then pick U 2 MGS , so that

0 0 0

hUA ; GenUA ;U i2 PP xN :

R

x x

P

2

0 0

By Convention 35, there exists an R such that RU A = SA and

x x

0 0 0

RU  = S . By 3, A  SA and by 3 and Lemma 47vi,

1

x

0 0

RGenUA ;U    . Therefore

82

0 0 0

RhUA ; GenUA ;U ihA; i;

x x

as desired.

viii If hA;  i2 AP M by rule letrec-var,

R

P

2

then M =letrec fx = M j i 2 I g in x  for some i 2 I , and

i i i 0

0

for some A = fx :  j i 2 I g,wehave  =  , and hA [ A ; i2

0 i i i 0 i

0

M  and    for all i 2 I . AP

R

i i 82;1 i

P

2

0

By induction, for all i 2 I wehave disjoint pairs hA ; M  i2 PP

R

i i

i P

2

and substitutions S such that

i

0

S hA ; i  hA [ A ; i: 4

i i 0 i

i

0 0

Then S    , and since each  2 T , there must b e a  2 T

i 82;1 i i 1 0

i i

0 0 0

such that    , and thus S    .

i 1 i 82;1

i i i

P

0 00 0 0

Let A = A , A = A [fx : t j i 2 I; x 62 domA ;t freshg,

i i i i i

i2I

0 00 0 0

 = f  A x  j i 2 I g, and S = ft :=  j i 2 I; x 62 domA g[

i i i

i i

S

 S .

i

i2I

0

By 4, if x 2 domA , then

i

0 0 00

S      SA x = SA x :

82 i 82;1 i 1 i i

i

0

Otherwise x 62 domA  and

i

0 0 00

S   = SA x :

82;1 i

i i

Therefore S is a solution to  .

000 0

Pick U 2 MGS  and let A = UA . Then

fx ji2I g

i

000 000 0

M : i2 PP hA ; GenA ;U

R

i

P

0

2 50

0 0

By Convention 35, there exists an R such that RU  = S 

82

i i

0 0

=  , and 

i

0

000 0 0

RA = RU A = SA  A:

1

fx ji2I g fx ji2I g

i i

000 0

By Lemma 47vi, RGenA ;U    ,so

82

i

0

000 000 0

hA ; GenA ;U i hA;  i:

i

0

ix If hA;  i2 AP M by rule letrec, the result follows by Lemma 67

R

P

2

and induction.

2

6.6 Comparing letrec and letrec-simple

Nowwe show that our rules for typing recursive de nitions are at least as

powerful as the usual ones for ML.

R

We write ML .A` M :  if A ` M :  follows by the rules of ML

R

and the rules rec-simple and letrec-simple, and  .A` M : 

2

s

if A ` M :  follows by the rules of  and rec-simple and letrec-

2

simple.

R

R

It is well known cf. [14] that  typ es strictly more terms than ML .

2

R R

R

Theorem 71 Comparison of ML and   If ML .A` M :  , then

2

R

 .A ` M :  . The converse does not hold.

2

R R

To show the relationship b etween  and P ,we rst state the following

2 2

result, without pro of.

R R

Lemma 72 If M =letrec B in N , then  .A ` M :  i  .A`

2 2

hhM ii :  .

R R R

.A ` M :  , then  If  and P Theorem 73 Comparison of 

2 2 2

R 0 0 0 0

P .A ` M :  , where A  A and    .

1 2

2

Pro of: We will use the following facts, whichwe state without pro of:

0 0

 If    and  2 T , then  =  .

1 0

0 0

 If    and  2 T , then  =  .

2 0

0 0

 If A  A , then SA  SA .

1 1 51

R 0 R 0

 If  .A` M :  and GenA;    , then  .A ` M :  .

2 2

We prove the theorem by induction on M . By Lemmas 67 and 72, we need

not consider the case M =letrec B in N  where N is not a variable

de ned in B . The cases M = x, M =xN , and M =M M  can b e

1 2

proved just as in Theorem 12, and the case M =xN  is trivial. That

leaves only the following case.

 M =letrec fx = M j i 2 I g in x , where i 2 I .

i i i 0

0

Then for some T typ e environment A = fx :  j j 2 I g,wehave

0 0 j j

R

 .A[ A ` M : 

0 i i

2

for all i 2 I , and GenA;    .

i

0

By induction, for all i 2 I wehave

R 0 0

P .A ` M :  ;

i

2 i i

0 0 0

where A [ A   A and    . Since  2 T wehave  =  .

0 1 i 2 i 0 i

i i i

P

0 0 0 0

Let A = A +A ; then A [ A   A , and A x =  for

0 0 1 j j

i

i2I

0 R

.A ` M :  for all i 2 I . By rule any j 2 I .Byweakening, P

i i

2

letrec-var,

00 R

.A ` letrec fx = M j i 2 I g in x : ; P

i i i i

2

0 0

00 0

where A = A . Since GenA;    , for some substitution S

i

0

fx jj 2I g

j

wehave S =  and domS  = FTV  FTVA. By substitu-

i i

0 0

tivity,

R 00

P .SA ` letrec fx = M j i 2 I g in x :;

i i i

2 0

00 00

and A  A  A = SA  SA .

1 1

2

7 Compiling with rank 2 intersection typ es

We brie y discuss some applications of rank 2 intersections in compilation.

Polymorphism allows a function F of typ e 8t:t ! t to b e applied to

arguments of anytyp e. Unfortunately, it also requires that the data rep-

resentation of its arguments b e reduced to a lowest common denominator:

the machine co de for F cannot handle b oth a 32- integer in a general 52

purp ose register and a 64-bit oating p ointnumb er in a oat register. In

practice, arguments are \b oxed," or represented as a p ointer to the actual

data value stored in main memory. Boxing and unboxing co ercions slow

program execution.

These overheads can b e reduced when more is known ab out the uses of

the p olymorphic function. For example, consider the program

M =f :f 3;f trueF:

A naive implementation would insert instructions to b ox the arguments 3

and true b efore passing them to F . A more clever implementation would

recognize that the only arguments of F are integers and b o oleans, b oth of

which can b e represented in a single 32-bit register; so F could b e compiled

to exp ect an unboxed value as its argument.

This can easily b e achieved in P .To compile M ,we rst calculate the

2

principal typings of the op erator and op erand:

f :f 3;f true : 8s; u:int ! s ^ bool ! u ! s  u;

F : 8t:t ! t:

The typ e of the op erator indicates that F will only b e applied to integers

and b o oleans, and the compiler can take advantage of this in generating the

machine co de for F . Note that this improves on Bjrner's minimal typing

derivations [3], whichwould require the arguments to b e b oxed.

P also supp orts other data representation strategies. For example, in

2

compiling the program f :f 3;f 2:4F ,we will calculate the principal

typing

f :f 3;f 2:4 : 8s; u:int ! s ^  oat ! u ! s  u:

If oating p ointnumb ers are 64-bit values, we can't just compile F to exp ect

its argument in a 32-bit register, as b efore. Boxing is one solution. But

another solution is p ossible: specialization [8]. We can generate two versions

of F , one exp ecting an unboxed integer in a 32-bit register, and one exp ecting

an unboxed oat in a 64-bit register. We are essentially overloading the

variable f , so the application f 3 invokes the integer-exp ecting F , and

f 2:4 invokes the oat-exp ecting F .

8 Conclusion

We discussed a varietyofrank2typ e systems:  , the rank 2 fragment

2

s vb

of System F; I , I , and I , all variants of the rank 2 intersection typ e

2

2 2 53

discipline ; and P , which adds ML-style, top-level quanti cation of typ e

2

variables to I .We showed that all of the systems are equivalent in terms

2

of typability|a term is typable in one system if and only if it is typable in

another. An immediate corollary is that typability in all of these systems is

vb s

DEXPTIME-complete. Wehave also determined that the sequence I , I ,

2 2

I , P is in order of increasing \expressiveness." For example, a judgment

2 2

vb

of I is a judgmentofP , but not vice versa.

2

2

We prop osed a new rule for typing recursive de nitions that can typ e

many examples of p olymorphic recursion. The extension of P by this rule

2

results in a system with principal typings and decidable typ e inference.

Finally,we discussed some applications of intersections in compilation.

The nite p olymorphism of intersections expresses data representation con-

straints more accurately than p olymorphism by quanti cation. The accu-

rate expression of these constraints leads to data representations that require

fewer b oxing and unboxing co ercions at runtime.

Acknowledgments. Alb ert Meyer, Jens Palsb erg, and Mona Singh made

many useful suggestions. I would particularly like to thank Assaf Kfoury for

his careful reading of a draft. 54

References

[1] Franz Baader and Jorg Siekmann. Uni cation theory.InDov M. Gab-

bay, C.J. Hogger, and J.A. Robinson, editors, Handbook of Logic in

Arti cial Intel ligence and Logic Programming,volume 2, pages 41{125.

Claredon Press, 1994.

[2] Henk Barendregt. The Lambda Calculus: Its Syntax and Semantics

Revised Edition,volume 103 of Studies in Logic and the Foundation

of Mathematics. North-Holland, 1984.

[3] Nikola j Skallerud Bjrner. Minimal typing derivations. In Recordof

the 1994 ACM SIGPLAN Workshop on ML and its Applications, pages

120{126, June 1994.

[4] Dominique Clement, Jo elle Desp eyroux, Thierry Desp eyroux, and

Gilles Kahn. A simple applicative language: Mini-ML. In Proceedings

of the 1986 ACM Conference on Lisp and ,

pages 13{27, 1986.

[5] M. Copp o and M. Dezani. A new typ e-assignment for lamb da terms.

Archiv fur  Math. Logik, 19:139{156, 1978.

[6] J.H. Gallier and W. Snyder. Designing uni cation pro cedures using

transformations: A survey. In Y.N. Moschovakis, editor, Logic from

Computer Science,volume 21 of Mathematical Sciences Research Insti-

tute Publications, pages 153{215. Springer-Verlag, 1992.

[7] Jean-Yves Girard. Une extension de l'interpretation de Godel a

l'analyse, et son applicational' elimination des coupures dans l'analyse

nd

et la th eorie des typ es. In J.E. Fenstad, editor, 2 Scandinavian Logic

Symp., pages 63{92. North-Holland Publishing Co., 1971.

[8] Cordelia V. Hall, Simon L. Peyton Jones, and Patrick M. Sansom.

Unboxing using sp ecialisation. In Glasgow Functional Programming

Workshop. Springer-Verlag, July 1994.

[9] Fritz Henglein. Typ e inference with p olymorphic recursion. ACM

Transactions on Programming Languages and Systems, 152:253{289,

April 1993.

[10] Jean-Pierre Jouannaud and Claude Kirchner. Solving equations in ab-

stract algebras: A rule-based survey of uni cation. In Jean-Louis Lassez 55

and Gordon Plotkin, editors, Computational Logic: Essays in Honor of

Alan Robinson,chapter 8, pages 257{321. MIT Press, 1991.

[11] A.J. Kfoury and J. Tiuryn. Typ e reconstruction in nite-rank fragments

of the p olymorphic -calculus extended summary. In Proceedings,

Fifth Annual IEEE Symposium on Logic in Computer Science, pages

2{11. IEEE Computer So ciety Press, 1990.

[12] A.J. Kfoury and J. Tiuryn. Typ e reconstruction in nite rank frag-

ments of the second-order -calculus. Information and Computation,

982:228{257, June 1992.

[13] A.J. Kfoury, J. Tiuryn, and P. Urzyczyn. A prop er extension of ML with

an e ectivetyp e-assignment. In ConferenceRecord of the Fifteenth An-

nual ACM Symposium on Principles of Programming Languages, pages

58{69, 1988.

[14] A.J. Kfoury, J. Tiuryn, and P. Urzyczyn. Typ e reconstruction in the

presence of p olymorphic recursion. ACM Transactions on Programming

Languages and Systems, 152:290{311, April 1993.

[15] A.J. Kfoury, J. Tiuryn, and P. Urzyczyn. An analysis of ML typability.

Journal of the ACM, 412, March 1994.

[16] A.J. Kfoury and J.B. Wells. A direct algorithm for typ e inference in the

rank 2 fragment of the second-order lamb da-calculus. Technical Rep ort

93{017, Boston University,Novemb er 1993.

[17] A.J. Kfoury and J.B. Wells. A direct algorithm for typ e inference in the

rank 2 fragment of the second-order lamb da-calculus. In Proceedings of

the 1994 ACM Conference on Lisp and Functional Programming, pages

196{207, 1994.

[18] A.J. Kfoury and J.B. Wells. New notions of reduction and non-semantic

pro ofs of strong -normalization in typ ed -calculi. In Proceedings,

Tenth Annual IEEE Symposium on Logic in Computer Science, pages

311{321. IEEE Computer So ciety Press, 1995.

[19] Kevin Knight. Uni cation: A multidiscipli nary survey. ACM Comput-

ing Reviews, 211:93{124, March 1989.

[20] J.-L. Lassez, M.J. Maher, and K. Marriot. Uni cation revisited. In Jack

Minker, editor, Deductive Databases and Logic Programming,chap-

ter 15, pages 587{625. Morgan Kaufman, 1988. 56

[21] Daniel Leivant. Polymorphic typ e inference. In ConferenceRecord

of the Tenth Annual ACM Symposium on Principles of Programming

Languages, pages 88{98, 1983.

[22] Harry G. Mairson. Deciding ML typability is complete for deterministic

exp onential time. In ConferenceRecord of the Seventeenth Annual ACM

Symposium on Principles of Programming Languages, pages 382{401,

1990.

[23] Nancy McCracken. The typ echecking of programs with implicit typ e

structure. In G. Kahn, D.B. MacQueen, and G. Plotkin, editors, Se-

mantics of Data Types,volume 173 of Lecture Notes in Computer Sci-

ence, pages 301{315, June 1984.

[24] Robin Milner, Mads Tofte, and Rob ert Harp er. The De nition of Stan-

dardML. MIT Press, 1990.

[25] A. Mycroft. Polymorphic typ e schemes and recursive de nitions.

In Proceedings of the International Symposium on Programming,

Toulouse,volume 167 of Lecture Notes in Computer Science, pages

217{239. Springer-Verlag, 1984.

[26] Gordon D. Plotkin. A note on inductive generalization. In Bernard

Meltzer and Donald Michie, editors, Machine Intel ligence5, 1970.

[27] John C. Reynolds. Transformational systems and the algebraic struc-

ture of atomic formulas. In Bernard Meltzer and Donald Michie, editors,

Machine Intel ligence5, pages 135{151, 1970.

[28] John C. Reynolds. Towards a theory of typ e structure. In B. Robi-

net, editor, Programming Symposium: Proceedings, Col loque sur la Pro-

grammation,volume 19 of Lecture Notes in Computer Science, pages

408{425. Springer-Verlag, 1974.

[29] Patrick Sall e. Une extension de la theorie des typ es en -calcul. In

G. Ausiello and C. Bohm, editors, Automata, Languages and Program-

ming: Fifth Col loquium,volume 62 of Lecture Notes in Computer Sci-

ence, pages 398{410. Springer-Verlag, July 1978.

[30] Jorg H. Siekmann. Uni cation theory. J. Symbolic Computation,

73&4:207{274, March/April 1989.

[31] Wayne Snyder. AProof Theory for General Uni cation,volume 11 of

Progress in Computer Science and AppliedLogic. Birkhauser, 1991. 57

[32] Mads Tofte. Operational Semantics and Polymorphic .

PhD thesis, University of Edinburgh, May 1988.

[33] Ste en van Bakel. Intersection Type Disciplines in Lambda Calculus

and Applicative Term Rewriting Systems. PhD thesis, Mathematisch

Centrum, Amsterdam, February 1993.

[34] J.B. Wells. Typability and typ e checking in the second-order -calculus

are equivalent and undecidable. Technical Rep ort 93{011, Boston Uni-

versity, 1993.

[35] Hirofumi Yokouchi. Emb edding a second-order typ e system into an

intersection typ e system. Information and Computation, 1172:206{

220, March 1995. 58

0

S  S ,22 Index

0

S  S ,22

V

Typ es

0

S = S ,22

Rn, 10

0

S = S ,22

V

Sn, 11

0

A + A ,9

0

S n, 11

0

A [ A ,7

Tv ,6

0

S [ S ,8

T ,7

0

0

M ! M ,15

T ,8

1

hhM ii,46

T ,8

2

-nfM , 16

T ,32

82

actM , 15

typ es of ML, 12

APM , 7

typ e schemes of ML, 12

applM , 16

typ es of System F, 10

domA, 7

domS , 7

Typ e systems

FTV , 7

 ,11

2

GenA;  , 7

s

 ,11

2

lcg , 18

R

,51 

2

MGS , 22, 36

I ,31

2

MGS [W ], 23, 36

s

I ,9

2

mlM , 17

vb

I ,30

2

s

PP M , 25

I

2

ML, 12

PP M , 37

P

2

R

ML ,51

PP R M , 47

P

2

P ,34

2

rngS , 7

R

P ,47

2

Solutions , 22, 36

Relations and op erators

General concepts

   ,8

pair, 7

1

0

acceptable, 7

A  A ,21

1

disjoint, 7

   ,8

2

principal, 21

   ,22

2;1

substitution, 7

   ,32

82

disjoint, 8

   ,33

82;1

idemp otent, 22

  ,11

term, 6

of ML, 12

   ,13

1

typable, 7

   ,13

2

0 0

typ e environments, 7

hA;  ihA ; i, 21, 35 59