HigherOrder

with

Intersection Typ es

een wetenschapp elijke pro eve op het gebied van de

Wiskunde en Informatica

proefschrift

ter verkrijging van de graad van do ctor

aan de Katholieke Universiteit Nijmegen

volgens besluit van het College van Decanen

in het op enbaar te verdedigen

op maandag januari

des namiddags te uur precies

do or

Adriana Beatriz Compagnoni

geb oren december te Buenos Aires

Promotores Professor M DezaniCiancaglini

Universit degli Studi di Torino Turijn Italie

Professor dr H P Barendregt

HigherOrder Subtyping

with

Intersection Typ es

Adriana Beatriz Compagnoni

Cover design David Aspinall and Adriana Compagnoni

cipdata koninklijke bibliotheek den haag

Compagnoni Adriana Beatriz

Higherorder subtyping with intersection typ es

Adriana Beatriz Compagnoni Sl sn I

Thesis Nijmegen With ref

ISBN

Sub ject headings lamb da calculus iv

A Susana y Mario vi

Acknowledgements

Mariangiola Dezani is the most encouraging and enthusiastic computer scientist I

have ever met From her I learned to trust my intuition and followit through to

the very end This research would not have b een p ossible without her technical

sup ervision and moral supp ort I would also like to thank my sup ervisor Henk

Barendregt His lucid lectures on calculus his abilitytohide irrelevant details

the clarity of his explanations and his eleganttechnical prose have b een constant

guides

I thank the p eople in the Netherlands esp ecially Erik Barendsen and Jan Ku

p er for their help and friendship and Steen van Bakel and Paula Severi for their

comments on previous drafts of this thesis Mieke Massink and Maria Fernandez

Ferreira started as colleagues and so on b ecame two of my dearest friends

I am grateful to Benjamin Pierce who suggested the study of a calculus

combining higherorder p olymorphism and intersection typ es He and I dened

the system F whose study is by and large the theme of this thesis together we

wrote CP from whichchapters and arose His fertile mind and his passion

for hard work make him inspiring company

I am indebted to Ro d Burstall who op ened to me the do ors of the Lab oratory

for Foundations of Computer Science There I found a highly motivating research

environment where I develop ed most of the results in this thesis and a handful

of friends who made my lifeinEdinburgh a b eautifully rich exp erience Ienjoyed

Philippa Gardner technical discussions with Stuart Anderson David Aspinall

Martin Hofmann Stefan Kahrs Zhaohui Luo Savi Mahara j James McKinna

Randy Pollack and Dilip Sequeira My sp ecial thanks to Healfdene Goguen for

many helpful comments on draft versions of this thesis

It has been an honour to have Jan Willem Klop Giusepp e Longo and Rob

Nederp elt as memb ers of the manuscript commission

From my watercolor teacher Jo ost van Moll I learned a technique which

actually changed my way of doing research it consists of carefully examining

ideas and being brave enough what has been done learning from the successful

to start over again regardless of the time invested in the previous try

In the nonacademic world I want to thank my parents Mario Compagnoni

and Susana Brunsch for their unconditional love Iwanttomention in the order

in which they app eared in my life Silvana Cantoni Karin Gottschalk Claudio

Massa Marib el Fernandez Javier Blanco Cristina Abbate Martin de Zuvira

and Esther Gerrits b ecause life would not b e the same without friends

th

Edinburgh of Decemb er vii

viii

This research was supp orted by the Dutch organization for scientic research NWO

SION pro ject Typed lambda calculus and by EPSRC GRANT GRG

Constructive logic as a basis for program development

Contents

Intro duction

Typ es and programs

Subtyping

Typ e inference and typ e checking

Background

Results

I HigherOrder Subtyping

The F Calculus

Intro duction

Syntax of F

Discussion

Conuence

The ChurchRosser theorem for

The ChurchRosser theorem for

fors

Structural prop erties

Strong normalization of

Decidability of Subtyping in F

Normal Subtyping

Structural prop erties of NF

yping Equivalence of ordinary and normal subt

Least strict upp er bound

Example

A subtyp e checking algorithm AlgF

Termination of subtyp e checking

Our decidability pro of and full F

Typing in F

Typ e checking and typ e inference

Minimal typing

Decidabilityoftyp e checking and typ e inference

Sub ject reduction ix

x CONTENTS

A PER Mo del for F

Intro duction

Total combinatory algebras

Higherorder partial equivalence relations

HOPER interpretation of F

Multiple Inheritance

Intro duction

An example of multiple inheritance

Enco ding multiple inheritance

II FirstOrder Subtyping

Implicit and Explicit Subtyping

Intro duction

The subtyping relation

Simply typ ed calculus

a system with implicit co ercions

a system with explicit co ercions

C

The relation b etween and

C

Simply typ ed calculus and

C

Conuence

Conclusions

Future research

A Summary of Denitions

F A

A Reduction rules for typ es

A Reduction rules for terms

A Context formation rules

A Kinding rules

A Subtyping rules

A Typing rules

A Subtyp e checking AlgF subtyping rules

A Typ e inference

A First order subtyping

A

A

C

Bibliography

Chapter

Intro duction

Typ es and programs

One of the basic ideas in programming is the notion of algorithm An algorithm is

a description of the rules one must follow to accomplish a task But for a machine

to be able to p erform such a task this description must be expressed in a formal

language and in particular programming languages serve this purp ose

Alan Turing intro duced a formal language for describing computable functions

now called Turing machines from which imp erative programming arose The

lambda calculus was invented by Alonso Church to dene the notion of comput

able functions Chu Since then a variety of lambda calculi have been dened

and used in the study of programming languages Lambda calculi can be seen

as simple programming languages since they are formal and describ e computa

tions In that sense a program is a term of the lambda calculus From a dier

ent p ersp ective lamb da calculi constitute metalanguages for analyzing other pro

gramming languages Although calculi are particularly wellsuited to studying

languages they have also b een used to study imp erative

programming disciplines Lan

Typ es are an imp ortant to ol in programming languages and logic which serve

to classify terms according to basic prop erties such as being a number or b eing

ber the term is and a function For example if we think of the integer num

its typ e is integer The addition function has a typ e expressing that it takes two

integer numb ers as arguments and returns an integer number as result which we

write as follows

integer integerinteger

One immediate advantage of typ es is that nonsensical expressions can b e con

sidered il legal For example the expression

go o d morning

will not b e part of the language b ecause go o d morning is not an integer numb er

but a string of characters Although this is a rather coarse example this sort of

mistakeisvery frequen t in the development of programs

CHAPTER INTRODUCTION

Further in that direction Automath dB MartinLof Typ e Theory Mar

Co quand and Huets Calculus of Constructions CH and Luos Extended Cal

culus of Constructions Luo are richtyp e systems in whichatyp e can not only

prevent the formation of nonsensical expressions but can also state prop erties of

terms For example in the case of a term corresp onding to a sorting algorithm

for lists the typ e can express the fact that the output is an ordered list

Typ e structures help organizing ideas and structuring programs in suchaway

that disciplines of programming and typ e systems walk hand in hand The de

velopment of ideas ab out programming motivates the design of typ e systems that

encourage programming in a particular style Ideally one would like to have

tailormade typ e systems for each particular style of programming so that bad

programming style results in illegal terms

We refer the reader to Bar Mitc for a more detailed analysis of the

relation b etween lamb da calculi and programming languages

Subtyping

Subtyping is a primitive relation that uniformly captures concepts from diverse

areas of computer science If S and T are sets then S T S is a subtype of T

means that elements of S are also elements of T If S and T are sp ecications then

S also satisfy T In ob jectoriented programming elements satisfying sp ecication

if S and T are ob ject descriptions then S T states that where an ob ject with

T is exp ected it is safe to use an ob ject with interface S When S and

T are mo dule interfaces in a software system an implementation of S is also an

implementation of T If S and T are theorems then a pro of of S is also a pro of

of T Understanding the essence subtleties and general prop erties of subtyping

illuminates a wide area

The idea of subtyp es app ears quite naturally in programming languages If we

think of typ es as sets we can easily picture what a subtyp e could be Informally

we can say that a typ e S is a subtyp e of T if any element of S can be seen

as an element of T We say can be seen as and not directly is because the act

of considering an element of typ e S as an element of typ e T might hide some

transformation Consider for example the typ es integer and real of integers and

real numbers resp ectively Usually on a computer integers are represented in a

dierent way than real numbers are even if we might think of the integers as a

subset of the real numb ers there is a translation to be p erformed The act of

considering an element of typ e S as an element of typ e T will b e called coercion

co erced into an element of In other words we say that an element of typ e S is

typ e T Somehow an element of typ e S has enough information to be seen as an

elementoftyp e T

While dealing with co ercions we can distinguish b etween an explicit style and

an implicit style Astyle with explicit co ercions means that co ercions are explicitly

indicated and in an implicit style as the name suggests co ercions are left unstated

In systems including subtyping there is usually a rule for typing co erced terms

AND TYPE CHECKING

in other words a rule that provided t has typ e S and S is a subtyp e of T allows

us to derive that t can be co erced into T In an explicit style the co ercion rule

mightlookas follows

t S S T

Coercion

c t T

ST

Similarly in an implicit style the corresp onding rule is as follows

t S S T

Subsumption

t T

An implicit co ercion is motivated by the fact that the same term can be con

sidered as b elonging to two dierent typ es without p erforming any change in the

term as for example is the case when one of the typ es is included in the other

with the intuitive idea of inclusion while an explicit co ercion gives explicit

information ab out the transformation We can think for example of a function

b ers If x is a f with the real numbers as domain and a subset A of real num

variable of typ e A then we would like to use f on x as well without p erforming

any extra calculation to apply f to x But if instead f is used with the integer

number as input and f happ ens to use the decimal part of its argument then

should be mapp ed into rst Therefore one can argue that the meaning of

f is f c

integ err eal

applications of subtyping in calculi was mo deling the re One of the rst

nement of interfaces in ob jectoriented languages Cara The formal subtyp e

relation S T mo dels the assertion that the ob jects in some collection S provide

more services than those in T so that it is safe to use a member of S in any

context where a member of T is exp ected

Typ e inference and typ e checking

We can say that if a term has a typ e it is to a certain extent correct This

guaranteeing that computations will not fail by correctness can be as simple as

mismatch of the exp ected argument of a function for example and as elab orate

as ensuring that certain sp ecication or prop erty is satised With these ideas

in mind a rst question one may ask is whether a given expression e is legal or

correct which in our framework means whether there exists a typ e T such that

e T This problem is traditionally called type inference A related question

given a term e and a typ e T is whether e T known as type checking In the

presence of subtyping b oth problems typ e checking and typ e inference become

more complicated b ecause typing is dened in terms of subtyping It is clear in the

Subsumption and Coercion rules that in order to answer a question of the form

e T we should b e able to answer questions of the form S T This shows that

at the heart of the decidabilityoftyping lies the question of whether the subtyping

relation is decidable In a system with the Subsumption rule each term may be

assigned more than one typ e Then to answer the typ e checking and typ e inference

aytoidentify all p ossible typ es for example by nding some questions weneedaw

CHAPTER INTRODUCTION

of representative of the typ es of each term One plausible candidate is a

minimal type with resp ect to the subtyping relation Then typ e inference consists

of nding a minimal typ e and typ e checking whether e T consists of nding a

minimal typ e S suchthate S and checking if S T Without the minimal typ e

prop ertytyp e checking b ecomes algorithmically intractable Imagine that instead

of having just one representative we have a nite set of them say S S such

n

e there exists j such that S T Imagine that e is an that for each typ e T of

j

application say e e then to nd the set of representativetyp es of eweneedto



match each representative of e against each representative of e whichproduces



a combinatorial explosion

Background

The formal study of subtyping in programming languages was b egun by Reyn

olds Rey and Cardelli Cara who used a lamb dacalculus with subtyping to

mo del the renementofinterfaces in ob ject oriented languages This led to a con

siderable b o dy of work covering an increasing range of ob jectoriented features by

combining subtyping with other typ etheoretic constructs including p olymorphic

op erat functions CW CG BCGS records with up date and extension

ors Cara CM recursive typ es AC BM and higherorder p olymorph

ism Car Mita

Typ e systems with subtyping have also arisen from the study of lamb dacalculi

with intersection typ es at the University of Torino CD BCD Most of this

work has been carried out in the setting of pure lamb dacalculi but it has also

been applied to programming language design by Reynolds Rey Some work

has b egun on combining intersections with other typing features Pie CDdL

The contribution of this thesis is to weave together these two threads by com

bining higherorder subtyping which forms the cornerstone of several recentmod

els of typ ed ob jectoriented programming CHC Bru PT with intersection

typ es leading to an extended ob ject mo del with multiple inheritance CP

Results

This thesis is divided into two parts the rst part consists of a detailed analysis

of the metatheory of a typ ed lambda calculus combining higher order b ounded

quantication and intersection typ es Our researchcovers syntactic semantic and

pragmatic asp ects

Chapter contains the denition of the system F and basic syntactic res

ults

We dene the typ ed lambda calculus F a natural generalization of

Girards system F with intersection typ es and b ounded p olymorph

ism A nov el asp ect of our presentation is the use of term rewriting

RESULTS

techniques to present intersection typ es which clearly splits the com

putational semantics reduction rules from the syntax inference rules

of the system

The reduction rules of F can be divided into two main groups re

ductions on typ es and reductions on terms Although

fors

conuence is not a mo dular prop erty in general in our case it is pos

sible to provide a mo dular pro of of it In section we combine the

indep endent pro ofs of conuence for reductions on typ es and conuence

terms towards a pro of of conuence of the reduction for reduction on

relation in the whole system

We prove the strong normalization prop erty of on wellformed

typ es

Chapter carries the most imp ortant result of this thesis Our main con

is decidable This yields as a tribution is the pro of that subtyping in F

corollary a solution to the previously op en problem of the decidability of

subtyping in F its intersection free fragment b ecause F subtyping sys

Moreover the decidabilityof ative extension of that of F tem is a conserv

subtyping is essential for the decidability of typ e checking and typ e infer

ence Another original feature is the use of a choice op erator to mo del the

behavior of variables during subtyp e checking The pro of of decidability is

divided into the following steps

We dene an algorithmic presentation of the subtyping relation where

only typ es in normal form are considered

Weprove that the algorithmic presentation is sound and complete with

resp ect to the denition of subtyping which means that it constitutes

a deterministic pro cedure to check subtyping in F

Finally we prove that the algorithmic presentation describ es a ter

minating pro cedure The pro of of termination is reduced to the strong

normalization prop erty of the reduction on typ es enriched with a choice

reduction which mo dels the b ehavior of variables during subtyp e check

ing

In chapter we prove that F satises the minimal typ e prop erty and

we provide an algorithm for computing minimal typ es We also prove that

typ e inference and typ e checking in F are decidable The minimal typ es

prop erty is used to prove that F satises the sub ject reduction prop erty

In chapter we dene a mo del based on partial equivalence relations and

we prove that the subtyping relation and the typ e assignment system are

sound with resp ect to the mo del

Although F was dened to provide a mo del of ob jectoriented programming

with multiple inheritance this thesis do es not intend to provide an account

CHAPTER INTRODUCTION

on the foundations of ob jectoriented programming In chapter we show

how to mo del multiple inheritance using intersection typ es This is a con

tinuation of the research on typ etheoretic foundations of ob jectoriented

programming by Pierce and Turner PT where multiple inheritance is not

captured

The second part of this thesis is devoted to the study of twodierentstyles of

subtyping subtyping with implicit co ercions and subtyping with explicit co ercions

We dene and study two alternative presentations of subtyping for simply typ ed

lambda calculus The rst one a system with implicit co ercions and the

a system with explicit co ercions Weshow that the system can second one

C

be translated into and that can be translated into This means that

C C

from a pragmatic p oint of view implicit or explicit co ercions are just a matter of

taste and b oth disciplines can b e compiled into the simply typ ed lamb da calculus

without subtyping

Part I

HigherOrder Subtyping

Chapter

Calculus The F

Intro duction

The system F was rst intro duced in CP where it was shown to be rich

enough to provide a typ ed mo del of ob ject oriented programming with multiple

inheritance F is an extension of F Gir with b ounded quantication and

intersection typ es which can b e seen as a natural generalization of the typ e discip

lines present in the current literature for example in CG Pie PT CP

Systems including either subtyping or intersection typ es or b oth have b een widely

studied for manyyears What follows is not intended to b e an exhaustive descrip

tion but a framework for the present work

Firstorder typ e disciplines with intersection t yp es have b een investigated by

the group in Torino CDC BCD and elsewhere see CC for background

and further references A secondorder calculus with intersection typ es was

studied in Pie Systems including subtyping were present in CW Cara



Higher order generalizations of subtyping app ear in CCH CHC Mita

BM F a secondorder calculus with b ounded quantication was studied

in Ghe and in Pieitwas proved that subtyping in F was undecidable and

yping rule for bounded quantication that undecidabilitywas caused by the subt

In CP an alternative rule for subtyping quantied typ es was presented

and the decidability of subtyping was proved for an extension of system F with

b ounded p olymorphism where all bounds app earing in SAll have the ground

kind a main limitation of that system

Allowing b ounds of functional kind forces us to intro duce a conversion rule to

have invariance of subtyping under conversion of typ es Therefore our sub

typing relation relates typ es of a more expressivetyp e system than that presented

in CP In fact treating the interaction between interface renement and en

capsulation of ob jects in ob ject oriented programming has required higherorder

generalizations of subtypingthe Fb ounded quantication of Canning Co ok Hill



Oltho and Mitchell CCH or Cardelli and Mitchells system F Car

Mita BM

Ghelli Ghe remarked that the rule for subtyping between quantied typ es

presented in CP led to a wellb ehaved subtyping relation but that the typing

CHAPTER THE F CALCULUS

relation fails to satisfy the minimal typ e prop erty This failure intro duces serious

problems in typ e checking and typ e inference as we observed in chapter At

the moment it is not clear how to solve them or even more problematic whether

the typing relation is decidable A p ossible solution to overcome this problem

is to replace the subtyping rule between quantiers by the corresp onding rule of

Cardelli and Wegners kernel fun CW

y of subtyping in the In chapter we give a p ositive answer to the decidabilit

is decidable presence of convertible typ es We prove that subtyping in F

which a fortiori gives the decidability of subtyping for the F fragment b ecause

the former is a conservative extension of the latter namely each subtyping

containing no intersections other than the empty ones statement derivable in F

is also derivable in F

that diers from the one intro duced in CP We present a denition of F

in two ways First Castagna and Pierces quantier rule has been replaced by

the Cardelli and Wegner rule Second we intro duce a richer notion of reduction

on typ es and thereby the four distributivity rules b ecome particular cases of

the conversion rule This new reduction is shown to be conuent and strongly

normalizing The latter simplication was motivated by structural prop erties of

presentation provides a dierent the former presentation Furthermore this new

view of the system that is the key to proving the decidabilityofsubtyping

This new p ersp ective suggests that to prove the decidabilityofsubtyping it is

enough to concentrate on typ es in normal form Note that the solution cannot b e

as simple as to restrict the subtyping rules of F to handle only typ es in normal

form and replace conversion by reexivity The following is a go o d example of the

problem to be solved Consider W K X Y KY K K Z X K K

Then X ZW W which is not derivable without using conversion ie

without p erforming any reduction even when the conclusion is in normal form

The subtyping rules of F are not syntax directed in the sense that the form

of a derivable subtyping statement do es not uniquely determine the last rule of its

derivation ie there might be more than one derivation of the same subtyping

judgement Todevelop a deterministic decision pro cedure to checksubtyping we

need a new presentation of the subtyping relation that provides the foundations

for a subtyp echecking deterministic algorithm

Our solution is divided in two main steps First we develop a normal subtyp

ing system NF in which only typ es in normal form are considered We prove

that derivations in NF can be normalized by eliminating transitivity and sim

plifying reexivity This simplication yields an algorithmic presentation AlgF

Moreover we prove that AlgF is indeed an alternative presentation of the F

nf nf nf

subtyping relation that is S T if and only if S T prop osition

Al g

of subtyping in F is to The second and last step towards the decidability

prove that the algorithm describ ed by AlgF terminates which is equivalent to

showing that the denition of the AlgF is wellfounded We discuss this further

in section

nf

Checking whether ST A is reduced to checking if lub ST

Al g Al g 

SYNTAX OF F

A where lub ST substitutes the leftmost innermost variable of ST by its b ound



in Such replacements may pro duce a term that is not in normal form in which

case we normalize it afterwards The main problem here is that the size of the

typ es to be examined in the recursive call do es not decrease This indicates that

the pro of of termination of the algorithm is not immediate In particular the

pro of of termination presented in CP cannot be mo died to serve our pur

of p oses b ecause of the interaction between reduction and the substitution

typ e variables by their bounds in our system We discuss this further in section

In this chapter we present the syntax of F we prove structural prop erties of

the system conuence and the strong normalization prop erty for the reduction

on typ es

Syntax of F

We now present the rules for kinding subtyping and typing in F They are

organized as pro of systems for four interdep endent judgement forms

ok wellformed context

T K wellkinded typ e

S T subtyp e

e T welltyp ed term

We sometimes use the metavariable to range over statements righthand sides

of judgements of any of these four forms

Syntactic Categories

The kinds of F are those of F the kind of prop er typ es and the kinds K K



of functions on typ es sometimes called typ e op erators

K typ es

K K typ e op erators

The language of types of F is a straightforward higherorder extension of F

Cardelli and Wegners secondorder calculus of b ounded quantication Like F

it includes typ e variables written X function typ es T T and p olymorphic

typ es X T KT in which the b ound typ e variable X ranges over all subtyp es

of the upp er b ound T Moreover like F we allowtyp es to b e abstracted on typ es

X KT and applied to argument typ es TT in eect these forms intro duce

a simply typ ed calculus at the level of typ es Finally we allow arbitrary nite

CHAPTER THE F CALCULUS

V

K

intersections T T where all the T s are memb ers of the same kind K

n i

T X typ e variable

T T function typ e

X T K T p olymorphic typ e

X K T op erator abstraction

TT op erator application

V

K

T T intersection at kind K

K

We use the abbreviation for nullary intersections and sometimes X K for

K

X K

V

K K

K

K X K X

We drop the maximal typ e Top of F since its role is played here by the empty

intersection For technical convenience weprovide kind annotations on b ound

variables and intersections so that every typ e has an obvious kind which can

be read o directly from its structure and the kind declarations in the context

The language of terms includes the variables x applications ee and func

tional abstractions x Te of the simply typ ed calculus plus the typ e abstrac

of F As in F each typ e variable is tion X T Ke and application eT

given an upp er bound at the point where it is intro duced

Intersection typ es are intro duced by expressions of the form forX T T e

n

which can be read as instructions to the typ echecker to analyze the expression

e separately under the assumptions X T X T X T and conjoin

 n

the results For example if Int Int Int RealReal Real then we can

derive

forX Int Realx Xx x Int Int RealReal

e x variable

xTe abstraction

ee application

X T K e typ e abstraction

e T typ e application

forX T Te alternation

The op erational semantics of F is given by the following reduction rules on

typ es and terms

Definition Reduction rules for typ es

X KT T T X T

 

SYNTAX OF F

V V

S T S T S T T

n n

V V

T T X S KT X S KT X S K

n n

V V

K K K



X K T T T X K T X K

n n

V V

K K K



T U T U T T U

n n

V V V

K K K

T S S T S S T T

n m n m

The rst rule is the usual reduction rule for typ es Rules through express

the fact that intersections in p ositive p ositions distribute with resp ect to the other

typ e constructors Rule states that in tersection is an asso ciative op erator In

and section we consider the reduction dened by rules through as

the one dened by as a comes from asso ciativity The lefthand side of

a

each reduction rule is a redex and the righthand side its reduct The relation

is extended so as to b ecome a compatible relation with resp ect to typ e formation

 is the transitive and reexive closure of and is the least equivalence

relation containing The captureavoiding substitution of S for X in T is

written T X S Substitution is written similarly for terms and is extended

nf

pointwise to contexts The normal form of a typ e S is written S and is

extended pointwise to contexts

Definition Reduction rules for terms

xT e e e x e

 fors 

X T K eT eX T

fors

forX T T e e forX T T e e

 fors n  n

forX T T e e if X FVe

n fors

Rules and are the reductions on terms Rule says that the for con

structor can b e pushed to the outermost level We consider the reduction dened

by rules through as and the one dened by as s comes from

for s

simplication The lefthand side of each reduction rule is a redex and the right

duct The relation is extended so as to b ecome a compatible hand side its re

fors

relation with resp ect to term formation  is the transitive reexive closure

fors

of and is the least equivalence relation containing

fors fors fors

Contexts

A context is a nite sequence of typing and subtyping assumptions for a set of

term and typ e variables

CHAPTER THE F CALCULUS

The empty context is written Term variable bindings have the form x T

typ e variable bindings have the form X T K where T is the upp er bound of X

and K is the kind of T

empty context

xT term variable declaration

X T K typ e variable declaration

When writing nonempty contexts we omit the initial The domain of is

written dom The functions FV and FTV give the sets of free term

variables and free typ e variables of a term typ e or context Since we are careful

to ensure that no variable is b ound more than once we sometimes abuse notation

and consider contexts as nite functions X yields the b ound of X in where

X is implicitly asserted to b e in dom

Typ es terms contexts statements and derivations that dier only in the

names of b ound variables are considered identical The underlying idea is that

variables are de Bruijn indexes dB

Definition Closed

FTV e dom A term e is closed with resp ect to a context if FV e

Atyp e T is closed with resp ect to a context if FTV T dom

Atyping statement e T is closed if e and T are closed with resp ect to

A kinding statement T K is closed if T is closed with resp ect to

A subtyping statement S T is closed if S and T are closed with

resp ect to

We consider only closed typing statements Observe that in the limit case

of the rule TMeet when n not having the closure convention would allow

nonsensical terms to b e typ ed On the other hand the free variable lemma lemma

guarantees that kinding statements are closed and the wellkindedness of

subtyping lemma ensures that subtyping statements are closed as well

Context Formation

The rules for wellformed contexts are the usual ones a start rule for the empty

context and rules allowing a given wellformed context to b e extended with either

a term variable binding or atyp e variable binding

ok CEmpty

T x dom

CVar

xT ok

T K X dom

CTVar

XT K ok

SYNTAX OF F

Typ e Formation

For each typ e constructor we give a rule sp ecifying how it can be used to build

wellformed typ e expressions The critical rules are KOAbs and KOApp which

form typ e abstractions and typ e applications essentially as in a simply typ ed

calculus

The wellformedness premise ok in KMeet and in TMeet below is

required for the case where n

XT K ok



KTVar

XT K X K



T T



KArrow

T T



XT K T



KAll

X T K T



XK T K

 

KOAbs

X K T K K

 

S K K T K



KOApp

ST K



ok for each i f ng T K

i

KMeet

V

K

T T K

n

Subtyping

The rules dening the subtyp e relation are a natural extension of familiar calculi

of b ounded quantication Aside from some extra wellformedness conditions the

rules STrans STVarandSArrow are the same as in the usual secondorder

case Rules SOAbs and SOApp extend the subtyp e relation p ointwise to kinds

other than The rule of typ e conversion in F that is if e T and T T

then e T is captured here as the subtyping rule SConv which also gives

reexivity as a sp ecial case The rule SAll is the rule of Cardellis Fun language

CW in which the b ounds of the quantiers are equal Rules SMeetG and

SMeetLB sp ecify that an intersection of a set of typ es is the sets ordertheoretic

greatest lower b ound

S K T K S T

SConv

S T

S T T U

STrans

S U

XT K ok



STVar

XT K X T



T S S T S S

  

SArrow

S S T T

 

CHAPTER THE F CALCULUS

XU K S T X U KS

SAll

X U KS X U KT

X K S T

SOAbs

X KS X KT

S T SU K

SOApp

SU TU

for each i f ng S T S K

i

SMeetG

V

K

S T T

n

V

K

T T K

n

SMeetLB

V

K

T T T

n i

Term Formation

Except for TMeet and TFor the term formation rules are precisely those of the

secondorder calculus of b ounded quantication TFor provides for typ e checking

under any of a set of alternate assumptions For each S thetyp e derived for the

i

instance of the b o dy e when X is replaced by S is a valid typ e of the for expression

i

itself The TMeet rule can then be used to collect these separate typings into a

single intersection Typ etheoretically TMeet is the intro duction rule for the

constructor the corresp onding elimination rule need not b e given explicitly since

it follows from TSubsumption and SMeetLB

x T ok



TVar

xT x T



xT e T



TAbs

xT e T T



f T T a T



TApp

fa T



XT K e T



TTAbs

X T K e X T K T



f X T K T S T



TTApp

fS T X S



eX S T S fS S g

n

TFor

forX S S e T

n

ok for each if ng e T

i

V

TMeet

e T T

n

e S S T

TSubsumption

e T

Most of the rules include premises whichhavetwo rather dierent sorts structural

premises whichplay an essential role in giving the rule its intended semantic force

SYNTAX OF F

and wel lformedness premises which ensure that the entities named in the rule are

of the exp ected sorts In an algorithmic presentation of the system on which an

implementation might be based the wellformation premises would be replaced

by the metatheoretic observation that recursive calls in the premises of all the

rules preserve the wellformedness of the arguments named in the conclusion

In the interest of brevitywe omit wellformation premises that can b e derived

from others For example in the rule SArrowwe drop the premise T T



since it follows from S S using the prop erties proved in section



Discussion

An equivalent presentation of intersection typ es uses binary intersections as in

CDC The intersection of S and T is then written S T and there is a

K

maximal element at each kind The rules of the system have to be mo died

according to this alternative notation In most cases each of our rules ab out

intersection typ es has to be replaced by two rules one for the binary case and

another for the maximal element For example the reduction rule

V V

X S K T T X S KT X S KT

n n

is replaced by

X S KT T X S KT X S KT and

 

X S K

Similar replacement takes place for rules through in denition The term

formation rule KMeet is replaced by the two following rules

K T K S

KInt

S T K

ok

KMax

K

K

The rule SMeetG is replaced by the following two rules

S T S T



SIntG

S T T



S K

SMax

K

S

In the cub e Bar F corresp onds to the system dened by the rules

and If K is a kind dened by the grammar K then

K

The rule corresp onds to the recursive step in the denition of K the rule

hed corresp onds to KArrow and KAll is the parallel of rule enric

with subtyping

CHAPTER THE F CALCULUS

Conuence

is conuent By that we mean that the In this section we show that the system F

reduction dened by putting together the reduction on terms

fors fors

denition and the reduction on typ es denition satises the

ChurchRosser prop erty We use the HindleyRosen lemma cf Bar to

establish this result This factors the pro of into two parts

proving that  and  commute and

fors

proving that and satisfy the ChurchRosser prop erty the results

fors

of sections and

commute if the following diagram Remember that two binary relations and



commutes

A B

 

D C

In order to prove that  and  commute we use the following lemma

fors

Lemma Bar Let and be two binary relations on a set



X Supp ose

A B

 

D C



where is the reexive closure of Hence and commute

 

We need the following auxiliary result to prove that  and  commute

fors

using the previous lemma

Lemma If T T then eX T  eX T

Proof By induction on the structure of T

Lemma

E F

fors

G H

 fors

CONFLUENCE

Proof By induction on the structure of E ObservethatifE is a typ e expression

E T then there can b e only reductions starting from E and the result holds

vacuously Consequently the meaningful cases are when E is a term E E and

of those the interesting cases are when E is a fors redex

E xT e e



F e x e



G xT e e and



T T

e e e xe the result follows Cho ose H F Since x T

 fors 

E X T K eS

F eX S

G X T eS and

S S

Cho ose H eX S Since X T K eS eX S the result

fors

follows by lemma

E forX T T T e e

i n 

F forX T T T e e

i n 

G forX T T T e e and

n 

i

T T

i

i

Cho ose H forX T T T e e

n 

i

Since forX T T T e e forX T T T e e and

n  fors n 

i i

forX T T T e e forX T T T e e

i n  n 

i

the result follows

E forX T T T e

i n

F e

G forX T T e and T

n

i

T T

i

i

Cho ose H F Since forX T T T e e the result follows

n fors

i

Corollary  and  commute

fors

CHAPTER THE F CALCULUS

The ChurchRosser theorem for

In this section we prove the ChurchRosser prop erty for the reduction dened in

The strategy we use here is similar to the one used in chapter section

of Bar to prove the corresp onding result for in the typ efree calculus

In order to prove the ChurchRosser prop erty for it is sucient to show

in T are suchthatS T and S  T the following strip lemma If S T T

 

then there exists T such that T  T and T  T Graphically

   

T S



T T



The idea of the pro of is as follows Let T b e the result of replacing the redex R in

S by its reduct R If wekeep track of what happ ens with R during the reduction

S  T then we can nd T To b e able to trace R we dene a new set of terms

 

where redexes can app ear underlined Consequently if we underline R in S we T

only need to reduce all o ccurrences of the underlined R in T to obtain T

 

Definition Underlining

is the set of terms dened by the following abstract syntax T

T X j T T

j X T K T j X K T

V

K

j T T j T T

V V

K K

j X K T T T T j T T

V V

j T T T jX T K T T

V V

K K K

T T j X K T j T T

Observe that only redexes are underlined

Underlined one step reduction is dened starting with the rewriting

rules

a X KT T T X T

 

V V

b S S T S T T T

n n

V V

c X S K T T X S KT X S KT

n n

V V

K K K



T T d X K X K T X K T

n n

V V

K K K



T U T U T T U e

n n

V V V

K K K

S S T T f T S S T

n m n m

CONFLUENCE

g X KT T T X T

 

V V

S T S T T T h S

n n

V V

i X S K T T X S KT X S KT

n n

V V

K K K



X K T X K T j X K T T

n n

V V

K K K



k T T U T U T U

n n

V V V

K K K

T S S T S S T T l

n m n m

is extended so as to b ecome a compatible relation with resp ect to T

is the reexive and transitive closure of and 

If T T then jT j T is obtained from T by erasing all underlinings

The capture avoiding substitution for underlined terms is written as usual

T X S

T is dened inductively as follows Definition The map T

X X

T T T T

 

X T KT X T KT

 

X KT X KT

T T T T

 

V V

K K

T T T T

n n

X KT T T X T

 

V V

S T S T T T S

n n

V V

X S K T T X S K T X S K T

n n

V V

K K K



X K T T X K T X K T

n n

V V

K K K



T T S T S T S

n n

V V V

K K K

T S S T T S S T

n m m n

Observe that reduces all underlined redexes

Notation jT j S and T S will b e written

jj

T S and T S

CHAPTER THE F CALCULUS

Lemma If T S T and T T are such that jT j T and T  S

then there exists S T such that T  S and jS jS Graphically

T S

jj jj

S T

Proof By induction on the denition of 

T S in one step Since S is obtained by contracting a redex in T S

can b e obtained by contracting the corresp onding redex in T

T  T Take S T

T  U and U  S Finally the result follows by the induction hyp o

thesis and the transitivityof 

Lemma Let S T and U T Then

Supp ose X Y and X FVU Then

S X T Y U S Y U X T Y U

S X T S X T

If S  S then S X U  S X U

If T S T are such that T  S then T  S Graphically

S T

T S

Proof

By induction on the structure of S

By induction on the structure of S using in the cases S X KS S



and S X KS S



It is enough to show the result for the rest follows by induction The

interesting cases are when S is a redex if S is a redex then the result

follows easily using otherwise the result follows easily by the denition of substitution

CONFLUENCE

By induction on the generation of  using

Lemma Let T T Then jT j  T Graphically

T

jj

R

jT j T

Proof By induction on the structure of T

Lemma Strip Let S T and T T If S T and S  T then

 

T Graphically there exists T T such that T  T and T 

   

T S



T T



Proof Supp ose that T is obtained from S by replacing the o ccurrence redex

R by its reduct R Then we can write S S R and T S R Let S R

be obtained from S by replacing R by its underlined version R Observe that

j S R and S R S R Then by lemma there exists T by jS R



lemma S R  T and by lemma T  T which



 

justify the following diagram

S R T



I I

jj jj

S R T



S R T



To complete the pro of let T T





Theorem ChurchRosser for

If S T and T T are such that S  T and S  T then there exists

 

and T  T Graphically T T such that T  T

   

S T



T T



CHAPTER THE F CALCULUS

Proof By induction on the generation of S  T

S T By the strip lemma

S T Take T T

 

S  T and T  T By the induction hyp othesis we can nd rst T



 T T  T and T T  T  T and then T such that T

   

  

Hence the result follows by the transitivityof 

The ChurchRosser theorem for

fors

In this section we prove the ChurchRosser prop erty for the reduction dened in

denition The idea of the pro of is as follows We prove that and

for s

are ChurchRosser that reduction steps can b e p ostp oned see lemma

s

and that if e e and e E are suchthate  e and e  e there exists e

 for s  

such that e  e and e  e see lemma

s   for 

Those four results allow us to prove the ChurchRosser theorem for Let

fors

e e e E such that e  e and e  e Then by sp ostp onement

 fors fors 

there exist f and f by ChurchRosser for there exists f and by lemma

 for 

there exist f and f and nally by ChurchRosser for there exists

  s

e which completes the following diagram



f e e

s

for

for for for

f f f

  

s

for

s s s

f e e

  

s

for

In order to provethesp ostp onement prop ertywe need the following auxiliary

lemma We will consider FVe as the set of free term and typ e variables of e

Lemma

e e implies FVe FVe

fors  

e e implies e X S e X S

s  s 

e e implies e xe e xe

s  s 

e e implies exe  ex e

s  s 

CONFLUENCE

e e implies e xe e xe

for  for 

e e implies ex e  exe

for  for 

e  e and f  f implies f xe  f xe

s  s  s  

e  e and f  f implies f xe  f x e

for  for  for  

Proof Items through followby induction on the structure of e item is a

corollary of items and and item is a corollary of items and

Lemma sp ostp onement If e e and e e then there exists

s for 

e such that e e and e  e

 for   s

Proof By induction on the structure of e using for the case e

forX T T f and for the case e x Tf f and for

n 

the case e X T Kf S

Lemma If e e and e E are such that e  e and e  e then

 for s 

there exists e such that e  e and e  e Graphically

 s   for 

e e

for

s s

e e

 

for

In order to prove this lemma we prove rst the corresp onding result for a one

step reduction

for

Lemma If e e and e E are such that e e and e  e then

 for s 

there exists e such that e  e and e  e Graphically

 s   for 

e e

for

s s

e e

 

for

Proof of lemma By induction on the derivation of e e using

for

lemma

We now prove the ChurchRosser prop erty for using the Newmans pro

s

p osition in Bar by proving that is strongly normalizing and weak

s

ChurchRosser

Lemma Strong normalization for Every sreduction sequence start

s

ing from a term e terminates

CHAPTER THE F CALCULUS

Proof Straightforward by induction on the number of symbols of the term

b eing reduced

Lemma Weak ChurchRosser for If e e ande E are such that

s 

e e and e e then there exists e such that e  e and e  e

s s   s   s 

Graphically

e e

s

s s

e e

 

s

Proof By induction on the structure of e using for the case e

forX T T f

n

Corollary ChurchRosser for If e e and e E are such that

s 

 e e  e and e  e then there exists e such that e  e and e

 s  s s   s 

Graphically

e e

s

s s

e e

 

s

Wenow prove the ChurchRosser theorem for the reduction This result

for

is obtained following a similar strategy to the one used to prove the corresp onding

prop erties for the reduction on typ es in section In order to prove

the ChurchRosser prop erty for it is sucient to show the following strip

for

then there lemma If e f and f in E are such that e f and e  f

  for for

exists f such that f  f and f  f Graphically

 for   for 

e f



for

for for

f f



for

The idea of the pro of is as follows Let f b e the result of replacing the redex R in

e by its reduct R If wekeep track of what happ ens with R during the reduction

e  f then we can nd f To b e able to trace R we dene a new set of terms

for  

E where redexes can app ear underlined Then if we underline R in e we only need

to reduce all o ccurrences of the underlined R in f to obtain f

 

Definition Underlining

CONFLUENCE

E is the set of terms dened by the following abstract syntax

x E

j x E E

j E E

j X T K E

j E T

j forX T TE

j x TE E

j X T K E T

j forX T TE E

Observe that only redexes are underlined

Underlined one step reduction is dened starting with the rewriting

for

rules

e xe a x T e e

  for

b X T K eT eX T

for

c forX T T e e forX T T e e

n  for n 

e xe d x T e e

for  

e X T K eT eX T

for

f forX T T e e forX T T e e

n  for n 

is extended so as to b ecome a compatible relation with resp ect to E

for

and  is the transitive reexive closure of

for for

If e E then jej E is obtained from e by erasing all underlinings

The captureavoiding substitution for underlined terms is written as usual

eX S and exf

E is dened inductively as follows Definition The map E

x x

xTe x Te

e e e e

 

X T Ke X T Ke

e T e T

T e forX T T e forX T

n n

CHAPTER THE F CALCULUS

xTe e e xe

 

eX T X T KeT

forX T T e e forX T T e e

n  n 

Observe that reduces all underlined redexes

Notation je j e and e e will b e written

 

jj

e and e e e

 

Lemma If e f E and e E are such that je j e and e  f

for

then there exists f E such that e  f and jf j f Graphically

for

f e

for

jj jj

f e

for

Proof By induction on the denition of 

for

e f in one step Then f is obtained by contracting a redex in e f

for

can b e obtained by contracting the corresp onding redex in e

e  e Take f e

for

e  f and f  f Hence the result follows by the induction hyp o

for for

thesis and the transitivityof 

for

Lemma Let e f and g E and S T T Then

a Supp ose x y and x FVg Then

ex f y g ey g xf y g

b Supp ose X Y and X FVS Then

eX T Y S eY S X T Y S

c Supp ose X Y Then

ex f X T e X T xf X T

a ex f exf

b eX T eX T

If e and f E are such that e  f then e  f Graphically

for for

e f

for

e f

for

CONFLUENCE

Proof

By induction on the structure of e

By induction on the structure of e using

By induction on the generation of  using

for

then jej  e Graphically Lemma If e E

for

e

jj

R

for

jej e

Proof By induction on the structure of e using

Lemma Strip If e f andf E are such that e f and e 

 for for

f then there exists f E such that f  f and f  f Graphically

  for   for 

e f



for

for for

f f



for

Proof Supp ose that f is obtained from e by replacing the o ccurrence redex

R with its reduct R Then we can write e eR and f eR Let eR

be obtained from e by replacing R by its underlined version R Observe that

jeR j eR and eR eR Then by lemma there exists f by



lemma eR  f and by lemma f  f

for  for

 

which justify the following diagram

for

eR f



I I

jj jj

for

for f eR for



for

eR f



To complete the pro of let f f

 

CHAPTER THE F CALCULUS

Theorem ChurchRosser for

for

If e f and f E are such that e  f and e  f then there exists

 for for 

f E such that f  f and f  f Graphically

 for   for 

e f



for

for for

f f



for

Proof By induction on the generation of e  f

for

e f By the strip lemma

for

e f Take f f

 

e  f and f  f By the induction hyp othesis we can nd rst f

for for



 f  f and then f such that f f  f andf f  f

for for   for   for

  

Hence the result follows by the transitivityof 

for

We have proved the conuence of the reduction on terms

fors

Theorem ChurchRosser for

fors

Let e f f E If e  f and e  f then there exists f E such

 fors fors  

that f  f and f  f Graphically

fors   fors 

e f



fors

fors fors

f f



fors

Finallywe can state and prove the conuence prop erty for the reduction rela

tion of F

Theorem ChurchRosser for

fors

If E F and G T E are such that E  F and E  Gthen

fors fors

there exists H T E suchthatF  H and G  H Graphically

fors fors

E F

fors

fors fors

H G

fors

STRUCTURAL PROPERTIES

Proof By the commutativity of  and  corollary and the

fors

ChurchRosser prop erty of and theorems and

fors

The ChurchRosser theorem has interesting corollaries that we will use in the

sequel

Corollary See chapter Bar Let R b e a reduction satisfying the

ChurchRosser prop erty Then

If T S then there exists U such that T U and S U

R R R

If T is a normal form of S then S T

R

Each term has at most one R normal form

Fact

X S KT if and only if T

X KT if and only if T

S T if and only if T

TS if and only if T

Structural prop erties

a number of structural prop erties of F Except where This section establishes

noted the pro ofs pro ceed by structural induction and are straightforward when

p erformed in the order in which they app ear

Lemma If and is a prex of then ok as a sub derivation

Moreover except for the case and ok the sub derivation is strictly

shorter

Lemma Syntaxdirectedness of context judgements

If X T K ok then T K by a prop er sub derivation



If x T ok then T by aproper sub derivation



Lemma Free variables

If T K then FTVT dom

If ok then each variable or typ e variable in dom is declared only

once

Lemma WeakeningPermutation Letand b e contexts suchthat

ok Then implies and

CHAPTER THE F CALCULUS

Proof By induction on the length of a derivation of

K



KOAbs We are given that X K T K Applying KMeet to

 

K



ok we obtain K we can assume without loss of

K



generality that X dom Then by CTVar X K

K



X K T K and the ok By the induction hyp othesis

 

result follows applying KOAbs

TAbs We are given that x T e T By lemma there exists



a prop er sub derivation of xT ok by lemma there is a

yet shorter sub derivation of T We can now apply the

induction hyp othesis to obtain T As b efore wecanassume

x dom by CVar xT ok By the induction hyp othesis

we have x T e T and applying TAbs yields the desired



result

Other cases If ok there is nothing to prove KTVar STVar TMeet and

TVar applying the corresp onding rule to ok SOAbs similar to

KOAbs KAll SAll TTAbs similar to TAbs All the other cases

followby straightforward application of the induction hyp othesis

Lemma Context kind and term strengthening

If X T K ok and X FTV then ok

  

If X T K S K and X FTV FTVS then

  

S K

If x T and x FV then

 

Moreover the derivations of the conclusions are strictly shorter than the derivation

of the premises

Proof We prove statements and bysimultaneous induction on the length of

derivations and statement by induction on the derivation of xT



CEmpty Vacuously true

CVar By part of the induction hyp othesis and CVar

CTVar The result follows from lemma



By part of the induction hyp othesis and CTVar



KTVar By part of the induction hyp othesis and KTVar

KArrow By part of the induction hyp othesis and KArrow

K Y T K T and X KAll We are given that X T

 

FTV FTV Y T K T In particular X FTVT

 

FTVT fY g Observe that by lemma X Y Then



X FTV Y T K FTV T Applying part of the

 

induction hyp othesis and KAll the result follows

STRUCTURAL PROPERTIES

KOAbs Similar to the case KAll

KOApp By part of the induction hyp othesis and KOApp

KMeet By parts and of the induction hyp othesis and KOApp

Except for the cases we consider below and the case for CEmpty which is

trivially true the result follows by straightforward application of the induc

tion hyp othesis and the corresp onding rule in each case

CVar The result follows by lemma



By the induction hyp othesis and CVar



TAbs Using lemma the induction hyp othesis and TAbs

TFor Using that FVforX T T e FVe FVeX S the

n

induction hyp othesis and TFor

Proposition Syntaxdirectedness of kindingGeneration for kinding

X K implies X T K for some T and

 

T T K implies K and T T

 

X T K T K implies K and X T K T

 

K



X K T K implies K K K and X K T K

   

for some K



ST K implies S K K and T K for some K

V

K

T K implies K K and ok and T K for each i T

n i

Moreover the pro ofs of the consequents are all strictly shorter than those of the

antecedents

Proof In each case the antecedent uniquely determines the last rule of its de

rivation The pro of follows by insp ection of the rules

Lemma Uniqueness of kinds If T K and T K then K

K

Definition Size

The size of a typ e expression T size T is dened as follows

t

a size X

t

b size S T size X S KT size STsize S size T

t t t t t

c size X KT size T

t t

V

K

d size T T size T

t n in t i

CHAPTER THE F CALCULUS

The homomorphic extension to contexts size is dened as follows

c

a size

c

b size X T K size xT size size T

c c c t

The size of a subtyping kinding or ok judgement J size J is dened as

j

follows

a size ok size

j c

b size T K size size T

j c t

c size S T size size S size T

j c t t

Lemma Wellfoundedness of context formation and kinding rules

For every kinding or ok judgement J size ok size J

j j

J J

n

is a kinding rule or a context formation rule then size J If

j i

J

size J for each ifng

j

Corollary

For any context it is decidable whether ok

For any context typ e expression T and kind K it is decidable whether

T K

Proof Lemma and prop osition imply that context formation rules

and kinding rules determine an algorithm to check context judgements and kinding

judgements and lemma implies that the algorithm terminates

Lemma Typ e substitution Let T K Then

U

If X U K S K then X T S X T K

U  S  S

If X U K ok then X T ok

U  

pro of of Proof By simultaneous induction on derivations of the premises The

part is straightforward using part of the induction hyp othesis We consider

the details of the pro of of The cases KArrow KAll KOAbsand KOApp

follow by straightforward application of part of the induction hyp othesis and

the corresp onding rule while the case of KMeet also uses part of the induction

hyp othesis We examine the case of KTVar where S Y for some variable Y

By prop osition Y T K X U K for some T There are

Y S U  Y

three cases to consider

Y T K Then we also have Y T K X T By part

Y S Y S 

of the induction hyp othesis X T ok Apply



ing KTVar we get X T Y K

 S

STRUCTURAL PROPERTIES

Y T K X U K We know that T K K From the premise

Y S U S U

of KTVar and part of the induction hyp othesis we

have X T ok The result follows byweakening



lemma

Y T K Then we have Y T X T K X T By

Y S  Y S 

part of the induction hyp othesis X T ok



from which the result follows by KTVar

Lemma Sub ject reduction for kinding judgements If S  T and

S K then T K

Proof In order to prove this result it is enough to prove the following statements

by simultaneous induction on the derivation of S K The rest follows by

denition of  induction on the

ok and implies ok

S K and S T implies T K

S K and implies S K

In chapter weprove that the sub ject reduction prop erty also holds for typing

judgements

Theorem Kind invariance under typ e conversion If S K and

S

T K with S T then K K

T S T

Proof By the ChurchRosser theorem there exists U such that S  U

and T  U and the result follows by sub ject reduction and unicityofkinds

Lemma Let S K for each j fmg Then if for every ifng

j

V V

K K

T T S S there exists j fmg such that S T then

n m j i

the following A particular case of the previous lemma is

Corollary Let S K for each ifng Then S T for

i i i

V V

K K

every ifng implies S S T T

n n

nf nf

Lemma Let S T K Then S T if and only if S T

Proof We shall consider only one part the other is similar

nf nf

y SConv S S By sub ject reduction wehavethat S K then b

nf

By similar reasoning we have T T The result follows by applying

STrans twice

Lemma Context mo dication If U K and is either ok or

T K then X U K implies X U K

 

CHAPTER THE F CALCULUS

Lemma Let S K for every ifng If for every j in fmg

i

V V

K K

there exists i in fng such that S T then S S T T

i j n m

Proposition Wellkindedness of subtyping If S T then

S K and T K for some K

Proof By induction on the derivation of S T

SConv We are given that S K and T K and S T By

lemma K K

STrans By the induction hyp othesis and uniqueness of kinds lemma

STVar We are given that X T K ok By KTVar it follows that



by lemma we have X T K X K Moreover



T K and by lemma X T K T K



SArrow We are given T S and S T and S S

  

By prop osition S S Further by the induction



hyp othesis together with uniqueness of kinds lemma wehave

the result follows by applying KArrow T T Finally



SAll We are given that X U K S T and X U K S

  

By prop osition X U K S Then applying the



induction hyp othesis and lemma we obtain T K and

K



X K T from whic h the result follows by applying



KAll

SOAbs By the induction hyp othesis and KOAbs

SOApp Similar to SAll

SMeetG Using the induction hyp othesis lemma and KMeet

V

K

SMeetLB We are given T T K which by prop osition im

n

plies T K for each i

i

Proposition Wellkindedness of typing If e T then T

Proof By induction on the derivation of e T

TVar We are given xT ok The result follows by lemma and



lemma

TAbs We are given xT e T By the induction hyp othesis x T



T By lemma it follows that T Furthermore

 

by lemmas and T Hence KArrow yields

T T



TApp By the induction hyp othesis for f T T and prop osition

STRONG NORMALIZATION OF

TTAbs We are given X T K e T By the induction hyp othesis



X T K T We obtain X T K T by apply

 

ing KAll

TTApp We know that f X T K T and also S T By the



induction hyp othesis X T K T and by prop osition



X T K T By lemmas and there exists a deriv



ation of T K By the wellkindedness of subtyping prop osition

and uniqueness of kinds lemma we have S K

Then by the typ e substitution lemma lemma T X S



TFor By the induction hyp othesis

TMeet We are given that ok and that e T for each i We have to

i

consider two cases

n Applying KMeet to ok we obtain

n By the induction hyp othesis T for every i and then the

i

result follows by applying KMeet

TSub By the induction hyp othesis prop osition and lemma

Strong normalization of

yp e that has a kind in F is strongly normalizing in three We prove that every t

are strongly normalizing Then we steps We rst prove that and also

a

prove that b oth reductions commute ie if T T and T T then there

a 

S in at least one step Finally using exists S such that S T and T 

a 

the previous two steps we prove that is strongly normalizing

Atyp e T is called strongly normalizing if and only if all reduction sequences start

ing with T terminate We write T for the set of all typ e expressions and SN for

the subset of T of strongly normalizing typ e expressions If A and B are subsets

of T then A B denotes the following subset of T

A B fF T j for all a AFaB g

Lemma is strongly normalizing

a

Proof By induction on the number of intersection symb ols of the typ e expression

b eing reduced

To prove strong normalization of we use a mo deltheoretic argument

interpreting kinds as sets of normalizing terms and the soundness of the mo del

gives as a corollary the strong normalization prop erty The interpretation of a

kind K notation K is dened as follows

SN

K K K K

 

CHAPTER THE F CALCULUS

Definition Saturated set S SN is saturated if is satises the following

conditions

If R R SN then XR R S

n n

If R R Q SN then

n

a if P X QR R S thenX KP QR R S for every K and

n n

V

K

S b if T Q T QR R

n m

V

K K



T T QR R S for every K then

m n

Intuitively a set of strongly normalizing typ e expressions is saturated if it contains

all typ e variables and is closed under expansion of expressions which may have a

kind of the form K K



Lemma

SN is saturated

If A B are saturated then A B is saturated

For any kind K K is saturated

Definition

Avaluation in T is a mapping from typ e variables to typ es

The interpretation of a typ e with resp ect to is

T T X X X X

n n

where FVT fX X g

n

Let be a valuation in T Then satises T K written j T K if

T K and satises X T K written j X T K ifX K We

saythat satises acontext jif j X S K for all X S K

A context satises T K written j T K if for every such that

jit follows that j T K

Lemma

K

K

V

K

If A K for each ifng then A A K

i n

Proof

By induction on the structure of K

STRONG NORMALIZATION OF

K is in normal form Hence SN K

K

K K K By the induction hyp othesis K Moreover if B K

 

K K



then B K by the saturation of K which means

 

K K



that K K



of K By induction on the structure

K Then by denition of K A SN for each ifng Since

i

V

K

every reduction starting from A A is a reduction con

n

V

K

s one has A A SN sisting only of steps inside the A

n i

K

for each K K K Let B K By the denition of A B K

 i 

V

K

ifng By the induction hyp othesis A BA B K

n 

V

K K



A A B K by the saturation of K Moreover

n  

V

K K



which means that A A K K

n 

K Proposition Soundness If T K then j T

Proof By induction on the derivation of T K

We consider the case for KMeet The other cases followby similar reasoning

V

K

Let T T T We have to consider two cases

n

n We are given T K for each ifng and by the induction hyp o

i

thesis j T K Let be a valuation such that jThen T K

i i

V

K

for each ifng By lemma T T K

n

K K K

n T Since the result follows by

Theorem Strong normalization for T K implies that every

reduction sequence starting from T is nite

h that X X Observe Proof By soundness j T K Cho ose suc

that j trivially Hence T T K SN



Lemma If T T and T T then there exists S such that

a 

T  S and S T

a 

Proof By induction on the structure of T

Corollary If T  T and T  T then there exists S such that

a 

T  S and S  T

a 

Proof By induction on the generation of T  T

a

Finallywe can prove strong normalization for

Theorem Strong normalization for T K implies that every

reduction sequence starting from T is nite

CHAPTER THE F CALCULUS

Proof Let T K We reason by contradiction Assume that there is an

innite reduction sequence starting from T Then lemma and theorem

imply that there are innitely many alternations of and reduction

a

sequences Graphically

T T

a

T T

 

a

T



By corollary we can construct an innite reduction whichcontradicts

theorem Graphically

T T

a

S T T

 

a a

S T S

  

a a

Chapter

Decidability of Subtyping in F

is decidable The solu In this chapter we show that the subtyping relation of F

tion is divided into two main parts First wedevelop a normal subtyping system

NF in which only typ es in normal form are considered We prove that pro ofs

can be normalized by eliminating transitivity and simplifying reexivity in NF

This simplication yields an algorithmic presentation AlgF whose rules are syn

tax directed Moreover weprove that AlgF is indeed an alternative presentation

nf nf nf

of the F subtyping relation Formally S T if and only if S T

Al g

prop osition

In the solution for the second order lambda calculus presented in Pie the

distributivity rules for intersection typ es are not considered as rewrite rules For

that reason new syntactic categories have to b e dened comp osite and individual

canonical typ es and an auxiliary mapping attening transforms a typ e into a

canonical typ e Our solution do es not need either new syntactic categories or

elab orate auxiliary mappings since the role played there by canonical typ es is

by typ es in normal form p erformed here

Indep endently Steen and Pierce proved a similar result for F SP There

are several dierences b etween our work and the pro of of decidabilityofsubtyping

in SP First our result is for a stronger system which also includes intersection

typ es Our pro of of termination has the novel idea of using a choice op erator to

mo del the behavior of typ e variables during subtyp e checking A second ma jor

dierence is the choice of the intermediate subtyping system We dene the normal

system NF which is not only the key to proving decidability of subtyping but

help ed understand the ne structure of subtyping yielding the algorithm A lgF

In SP the intermediate system called a reducing system leads to a much

more complicated pro of which involves dealing with several notions of reduction

and further reformulation of the intermediate system

CHAPTER DECIDABILITY OF SUBTYPING IN F

Normal Subtyping

An imp ortant prop erty of derivation systems is the information that a derivable

judgement contains ab out its pro ofs This information is essential to pro duce

results which not only state prop erties ab out the subpro ofs but also help identify

ill formed judgements

we can prove In F

W K X Y KY K K Z X K K X ZW W

This simple example already shows that STrans erases information obtained

conclusion any longer see for a de by SConv that is not present in the

rivation A rst step towards an algorithm to check the subtyping relation is to

design a set of rules in which the derivable judgements contain all the informa

in which tion ab out their derivations To this end we dene a set of rules NF

conversion is reduced to a minimum and as we show in lemma transitivity

can b e eliminated Both results are proved with a standard cutelimination argu

ment This yields a syntax directed subt yping relation AlgF which constitutes

a decision pro cedure for the original system

which uses the context In section we present the subtyping system NF

and typ e formation rules of F We dene rewriting rules for derivations in NF

denitions and and describ e a terminating pro cedure to normalize

pro ofs which gives as a consequence the generation for subtyping prop osition

and an algorithmic presentation AlgF see denition

Finally in section we show that there is an equivalence b etween subtyping

and subtyping in AlgF which is essential to prove the decidability of in F

subtyping in F

We now dene the normal subtyping system NF Subtyping statements in

NF are written S T and S T and all typ es app earing in are in

n

normal form

Notation A B and C range over typ es whose outermost constructor is

not an intersection

Remark It is an immediate consequence of the reduction rules that

if T is in normal form then T is either a variable X S A X S KA

V

K

X KA AS where A is not an abstraction or A A We frequently use

n

this notation as a reminder of the shap e of typ es in normal form Note that wedo

not fully use this convention in denition in order to highlightthe fact that

NSArrow NSAlland NSOAbs have the same form as SArrow SAll and

SOAbs resp ectively

We now dene lub S We prove in lemma and corollary that



when dened it is the smallest typ e beyond S with resp ect to

Definition Least strict Upp er Bound

lub X X



lub TS lub T S

 

NORMAL SUBTYPING

Definition NF subtyping rules

S K

NSRefl

S S

n

S T T U

n n

NSTrans

S U

n

X A X A

n

NSTVar

X A

n

T S S T S S

n n   

NSArrow

S S T T

n  

XU K S T X U KS

n

NSAll

X U KS X U KT

n

K

X K S T

n

NSOAbs

X KS X KT

n

nf

lub TS A TS K TS A

n 

NSOApp

TS A

n

if mg A T A K

n i

NS

V

K

A T T

n m

j f ng S A k f ng S K

n j k

NS

V

K

S S A

n n

if mgj f ng S T k f ng S K

n j i k

NS

V V

K K

T T S S

m n n

As we mentioned in the intro duction an imp ortant factor to develop this

system was to consider the distributivity rules of the presentation of F in CP

as reduction rules instead of subtyping rules This new point of view suggested

that an algorithmic system should to a certain extent concentrate on normal

forms replacing the conversion rule by reexivity Consequently aderivation of a

subtyping statement should involve only t yp es in normal form But enlightened

by the simple counterexample it is not p ossible to p erform all reductions

at once In other words the system do es not satisfy an SConv p ostp onement

prop erty Without using SConv it is not p ossible to derive Hence the

solution is not as simple as replacing SConv by NSRefl

In general the interaction between STrans and SConv can be analyzed

as follows In STrans the metavariable T of the hyp othesis is not present in

problem by itself a similar situation app ears the conclusion but this is not a

in the simply typ ed lambda calculus in its application rule and the system is

deterministic The problem is that in the presence of SConv the vanishing T

can be convertible to either S or U or to both S and U What example

shows is that S and U may b e dierent normal forms which means that searching

for T is inherently nondeterministic

CHAPTER DECIDABILITY OF SUBTYPING IN F

We cannot eliminate transitivity completelywe still need it on typ e variables

andontyp e applications In F Ghe transitivity is eliminated and hidden in a

richer variable rule in which deciding whether X T when T X is reduced

to deciding whether the b ound of X is smaller than or equal to T The bound of

X has the particular prop erty of b eing the least strict upp er b ound of X This

observation motivated the denition of our NSOApp rule in whichwe reduce the

decision of whether TS A when A TStocheck if the least strict upp er

See lemma and corollary bound of TS is smaller than or equal to A

lub TS is obtained from TS by replacing its leftmost innermost variable by the



corresp onding b ound in Consequently lub TS may be other than a normal



form That is the reason we normalize it The strength of the conversion rule that

is not captured by reexivity is hidden in this normalization step Since TS is a

well kinded typ e by the free variables lemma lemma FTV TS dom

Therefore lub TS is dened By lemma lub TS is wellkinded and

 

since wellkinded typ es are strongly normalizing its normal form exists The rules

SMeetLB and SMeetG are replaced by NS NS and NS

of NF Structural prop erties

This section establishes a number of structural prop erties of NF The pro ofs of

lemmas and are similar to those of the corresp onding prop erties for F

Lemma If S T and is a prex of then ok as a sub deriv

n

ation Moreover the sub derivation is strictly shorter

Lemma WeakeningPermutation Letand b e contexts suchthat

and ok Then S T implies S T

n n

We present rewriting rules on derivations to simplify instances of NSRefl

and NSTrans We give a terminating strategy to transform a given derivation

into a derivation with o ccurrences of NSRefl only applied to typ e variables or

typ e applications and without o ccurrences of NSTrans To improve readability

we omit kinding judgements in the transitivity elimination rules which app ear as

hyp othesis in the redex or in a prop er sub derivation of the missing ones as we

proved in generation for kinding prop osition The derivations of the kinding

judgements of each reduct of the reexivity rules are prop er sub derivations of the

kinding judgements in its redex

Definition Reexivity simplication rules

S A

NSRefl

S A S A

n

A S

NSRefl NSRefl

S S A A

R

n n

NSArrow

S A S A n

STRUCTURAL PROPERTIES OF NF

X S KA

NSRefl

X S KA X S KA

n

XS K A

NSRefl

XS K A A

R

n

NSAll

X S KA X S KA

n

X KA K K

NSRefl

X KA X KA

n

X K A K

NSRefl

X K A A

R

n

NSOAbs

X KA X KA

n

V

K

A A K

n

NSRefl

V V

K K

A A A A

n n n

A K

i

NSRefl

A A if ng

n i i

R

NS

V V

K K

A A A A

n n n

Definition Transitivity elimination rules

S K

NSRefl

S T

S S S T

n

T

n n

NSTrans

S T

n

T K

NSRefl

S T

T T S T

n

T

n n

NSTrans

S T

n

X A

n

NSTVar

X A A B

n n

NSTrans

X B

n

X A A B

n n

NSTrans

X B

T

n

NSTVar

X B n

CHAPTER DECIDABILITY OF SUBTYPING IN F

U T B C T S A B

n n n n

NSArrow NSArrow

S A T B T B U C

n n

NSTrans

S A U C

n

U T T S A B B C

n n n n

NSTrans NSTrans

U S A C

T

n n

NSArrow

S A U C

n

XS K B C XS K A B

n n

NSAll NSAll

X S KA X S KB X S KB X S KC

n n

NSTrans

X S KA X S KC

n

X S K A B XS K B C

n n

NSTrans

XS K A C

T

n

NSAll

X S KA X U KC

n

X K A B X K B C

n n

NSOAbs NSOAbs

X KA X KB X KB X KC

n n

NSTrans

X KA X KC

n

X K A B X K B C

n n

NSTrans

X K A C

T

n

NSOAbs

X KA X KC

n

nf

lub AS B

n 

NSOApp

AS B B C

n n

NSTrans

AS C

n

nf

lub AS B B C

n  n

NSTrans

nf

lub AS C T

n 

NSOApp

AS C

n

j f ng A B if ng A A

n j n i

NS NS

V V

K K

A A A A A B

n n n n

NSTrans

A B

n

j f ng A A A B

n j n j

NSTrans

T

A B n

STRUCTURAL PROPERTIES OF NF

if ng B A

n i

NS

V

K

A B B A A

n n n

NSTrans

V

K

A A A

n n

if ng A B B A

n n i

NSTrans

if ng A A

n i

T

NS

V

K

A A A

n n

j f ng A B

n j

NS

V

K

A A B B A

n n n

NSTrans

V

K

A A A

n n

j f ng A B B A

n j n

NSTrans

j f ng A A

n j

T

NS

V

K

A A A

n n

if ng A B j f mg A A

n i n j

NS NS

V V

K K

A A A B B A

m n n n

NSTrans

V V

K K

B B A A

n m n

j f mg A A if ng A B

n j n i

NSTrans

if ngj f mg A B

n j i

T

NS

V V

K K

B B A A

n m n

if ngj f mg A B k f r gif ng B C

n j i n i k

NS

V V V V

K K K K

C C B B B B A A

r n n n m n

NSTrans

V V

K K

C C A A

r n m

k f r gif ngj f mg A B B C

n j i n i k

NSTrans

k f r gj f mg A C

n j k

T

NS

V V

K K

A A C C

n m r

if ng B C if ngj f mg A B

n i n j i

NS NS

V V V

K K K

A A B B B B C

m n n n n

NSTrans

V

K

A A C

n m

j f mgif ng A B B C

n j i n i

NSTrans

j f mg A C

n j

T

NS

V

K

A A C

n m

CHAPTER DECIDABILITY OF SUBTYPING IN F

if ng A B k f r gif ng B C

n i n i k

NS NS

V V V

K K K

B B C C A B B

n r n n n

NSTrans

V

K

A C C

n r

k f r gif ng A B B C

n i n i k

NSTrans

k f r g A C

n k

T

NS

V

K

A C C

n r

A derivation of a subtyping statement is in renormal form if it has no re

exivity redexes and it is in transnormal form if it has no transitivity redexes

and it is in normal form if it has neither reexivity nor transitivity redexes The

elimination of NSTrans and the simplication of NSRefl follow a standard

cutelimination argument

Lemma Reexivity simplication Let D be a derivation of a subtyping

statement with only one application of NSRefl Then D has a renormal form

Proof Same argumentas in lemma

y elimination Let D be a derivation of a subtyping Lemma Transitivit

statement with only one application of NSTrans Then D has a transnormal

form

Proof By induction on the size of D following a case analysis of the last rule

of D If the last rule is not NSTrans then the result follows by the induction

hyp othesis Otherwise we consider all p ossible last rules of the derivations of

the premises and note that each p ossible conguration determines a transredex

Finally observe that each reduction yields either a derivation in normal form or

shorter derivations with only one o ccurrence of NSTrans in which case the result

follows by the induction hyp othesis

An immediate corollary of this last result is that transitivity elimination ter

minates Given a derivation D of S T iterate the previous lemma on all

n

sub derivations of D that have only one NSTrans application The number of

times the lemma is applied is equal to the number of o ccurrences of NSTrans

in D Furthermore lemma implies that reexivity simplication terminates

The simplication rules are such that transitivity simplication rules do not create

new reexivity redexes Therefore we can reduce all instances of NSRefl rst

and then all instances of NSTranswhich is a terminating pro cedure to normalize

a derivation Consequently we have proved the following corollary

Corollary Existence of normal derivations Given a derivation of

n

S T Then there exists a derivation in normal form of S T

n

Lemma

STRUCTURAL PROPERTIES OF NF

A derivation in normal form whose last rule is NSRefl is either a pro of of

X X or AT AT

n n

If the last rule of a subtyping derivation D is NSTrans then D is not in

normal form

Proof

According to the reexivity elimination rules any other p ossible NSRefl

application is a redex

By case analysis of the last rules of the premises of the last rule of D In

each case the result follows either by the induction hyp othesis or b ecause the

constitutes a last rule of at least one of the derivations of the premises of D

redex

We can summarise the previous results as follows

Corollary If S T then there exists a pro of of the same judge

n

ment with no applications of NSTrans and in which NSRefl is only applied to

typ e variables and typ e applications

A consequence of the normalization of pro ofs is the following generation result

Proposition Generation for normal subtyping

X B implies X B and X K for some K or X B

n n

S A C and S A B implies B T C T

n n n

S A

X S KA B implies B X S KC X S K A C and

n n

X S KA

K

X KA B implies B X KC and X K A C

n n

nf

AS B implies B ASor lub AS B and AS K

n n 

V

K

A A B implies that there exists j fmg such that

n m n

A B and k fmg A K

j k

V

K

and A B B implies that for each ifng A B

n n n i

A K

V V

K K

A A B B implies that for each ifng there exists

n m n

j fmg such that A B and k fmg A K

n j i k

Moreover given a normal pro of of any of the antecedents the pro ofs of the con

sequents are prop er sub derivations

CHAPTER DECIDABILITY OF SUBTYPING IN F

Proof In each case given a pro of of the antecedent there is also a pro of in

normal form Due to lemma such a derivation cannot end with an ap

plication of NSTrans and b ecause of lemma if it ends with NSRefl

then it is a derivation of a subtyping statement between typ e variables or typ e

applications Finallythe result follows by insp ection of the other rules

Lemma

V

K

T A A if and only if T A for each k fng

n n n k

V V

K K

A for each k fng A A if and only if T T

k n n n

V V

K K

A A T if and only if A A K Then Let

n n n

A T for some k f ng

n k

Proof By induction on derivations using lemma and generation

Equivalence of ordinary and normal subtyp

ing

In this section we show that a subtyping statement is derivable in F if and only

if the corresp onding normalized statement is derivable in NF This equivalence

is proved in theorem As usual we need some auxiliary prop erties and

denitions among which we can highlight prop ositions and

Lemma Let lub S b e dened Then



S K implies lub S K



S lub S



Proof Item follows by induction on derivations while item follows by in

duction on the structure of S

Proposition Soundness If S T then S T

n

Proof By induction on the derivation of S T

n

NSRefl By SConv

NSTVar By the induction hyp othesis STVar and Strans

NSOApp By the induction hyp othesis lemma SConv and STrans

k in fng A K and there is a j NS We are given that for each

k

V

K

in fng such that A B By KMeet A A K

n j n

V

K

A A A for each k in particular for and by SMeetLB

n k

j Hence the result follows by the induction hyp othesis and STrans

EQUIVALENCE OF ORDINARYAND NORMAL SUBTYPING

NS We are given that A K and for each i in fmg A B

n i

Hence the result follows by the induction hyp othesis and SMeetG

NS We are given that for each k in fng A K and for each i in

k

A B By KMeet fmg there is a j in fng such that

j i n

V V

K K

A A K and by SMeetLB A A A for

n n k

each k Hence the result follows by the induction hyp othesis STrans

and SMeetG

Other cases By the induction hyp othesis and the corresp onding rule in the other

system

K

The following lemma says that emptyintersections are maximal elements

of the subtyping order

Lemma

K

T K implies T

n

K

T K implies T

Proof Statement follows by the cases m in NS and NS Statement

is the case n in SMeetG

Lemma

nf

ok implies ok

nf

T K implies T K

nf

S T implies S T

nf

T K implies T K Let ok Then

  

nf

Let ok Then S T implies S T

  

nf nf nf

Let S T K Then S T if and only if S T

Proof Statements and follow by simultaneous induction on the size of de

rivations using lemma Statement follows by induction on the derivation

of S T using part part and lemma Statement follows by

nf

induction on the derivation of T K Item follows by induction on



nf

the derivation of S T using part Item is a corollary of part



part and lemma

In the last lemma items and show that well formation of contexts

kinding judgements and subtyping judgements are invariant under normalization

of contexts while items and are the converse of and resp ectively

The follo wing lemma states that STVar is an admissible rule in NF

CHAPTER DECIDABILITY OF SUBTYPING IN F

Lemma Let b e a con text in normal form suchthat ok and Y dom

Then Y Y

n

Proof Let Y T K By lemma T K If T is



not an intersection then by NSRefl and NSTVar we have Y T If

n



V

K

T B B then by generation for kinding and unicityofkinds B K

m i

for each i and K K By NSRefl B B for each i Then by NS and

n i i

NSTVar it follows that Y B for each i and by NS Y T

n i n

Lemma Substitution If U K and X K S T then

n

nf nf nf

X U S X U T X U

n

Proof By induction on the derivation of X K S T For the sake of

n

claritywe sometimes leave out kinding judgements and their justications which

follow easily from the structural prop erties in section Let X K

NSRefl By the typ e substitution lemma lemma sub ject re

duction for kinds lemma and NSRefl

NSTrans By the induction hyp othesis and NSTrans

Y A We have to consider three cases NSTVar We are given

n

nf

Y X By sub ject reduction U K and by lemma

nf K

it follows that U By weakening it follows

n

nf nf K

that X U U and by the induction hyp o

n

nf K nf

thesis it follows that X U AX U

n

Finally the result follows by NSTrans

Y dom By the free variables lemma X FVY and

X Y By lemmas and it follows that

nf

X U Y Y and by the induction hyp othesis

n

nf nf

it follows that X U Y AX U Finally

n

the result follows by NSTrans

By the induction hyp othesis it follows that Y dom

nf nf nf

X U Y X U AX U

n

By lemmas and

nf nf

X U Y X U Y

n

nf nf

Furthermore X U Y Y X U Hence

the result follows by NSTrans

NSArrow We are given that T S and S T By the induc

n n  

nf nf

tion hyp othesis it follows that X U T X U

n

nf nf nf nf

S X U and X U S X U T X U

n  

nf nf

There are four cases to consider since T X U and S X U

 

may be intersections or not We shall consider only two of them to

illustrate the pro of metho d

EQUIVALENCE OF ORDINARYAND NORMAL SUBTYPING

nf nf

T X U and S X U are not intersections Then the

 

result follows by applying NSArrow

V

nf nf

S X U C C and T X U is not an intersec

 n 

tion Then we have that

nf nf nf

T T X U T X U T X U and

 

nf

S S X U



V

nf nf

S X U C S X U C

n

nf

By lemma it follows that for some i X U

n

nf nf

X U C T X U Applying NSArrow

n i 

nf nf nf

S X U C T X U T X U Finally the

i 

result follows by NS

Other cases NSOApp is similar to the case for NSTVar using lemma

and uniqueness of normal forms All other cases are similar to that

of NSArrow

This substitution lemma is the key result we use in proving that SOApp has a

corresp onding admissible rule in NF

nf nf

Lemma SU K Then S T implies SU TU

n n

Proof By induction on the derivation of S T assuming a derivation in

n

normal form The cases for NSArrow and NSAll are imp ossible b ecause of the

assumption SU K

NSRefl By sub ject reduction for kinds and NSRefl

NSTVar We are given X A By the induction hyp othesis

n n

nf nf

AU We have to consider two cases X U

nf

AU B By NSOApp

V

K nf nf

A A By lemma X U A for AU

n n k

each k in fng By NSOApp XU

n

A for each k which by NS implies

k n

nf

XU AU

NSOAbs We are given X K S T By the substitution lemma it

n

nf nf

follows that S X U T X U On the other hand

n

we have that X KS U S X U and X KT U

T X U Finally the result follows by the uniqueness of normal

forms

NSOApp Similar to case NSTVar

NS By the induction hyp othesis and NS using generation for subtyp ing

CHAPTER DECIDABILITY OF SUBTYPING IN F

NS and NS By the induction hyp othesis using lemma

nf nf nf

Proposition Completeness If S T then S T

n

Proof By induction on the derivation of S T using lemma for the

case of SOApp

Theorem Equivalence of ordinary and normal subtyping Let S

nf nf nf

K and T K Then S T if and only if S T

n

Proof

By completeness

nf nf nf

By soundness it follows that S T and by lemma

it follows that S T

Least strict upp er b ound

So far we only used that lub S is an upp er bound of S in the context See



lemma We can now give the nal motivation of the name we chose

S T implies showing that if lub S is dened and T S then



lub S T We rst show that the corresp onding prop erty holds for the



normalized system

Lemma Let lub S be dened Then





S If S  S and  then lub S  lub

 

nf

If S T then lub S T or S T

n n 

Proof

By induction on the structure of S observing that if lub S is dened so





is lub S



By induction on the derivation of S T It is immediate for the case

n

NSReflforNSArrow NSAlland NSOAbs lub S is not dened for



the other rules the result follows using the induction hyp othesis

Corollary Let lub S be dened Then S T and T S



T implies lub S



nf nf nf

Proof By completeness it follows that S T By lemma

n

nf

nf nf nf nf nf

lub S T b ecause S T By soundness it follows that

nf

n



nf

nf nf nf nf

lub S T which is equivalent to lub S T by lemma

nf nf

 

Finally using lemmas and and prop osition to get

the corresp onding kinding judgements it follows that lub S T by lemma



SConv and STrans

EQUIVALENCE OF ORDINARYAND NORMAL SUBTYPING

Example

In this section we give the derivations in F and in NF of the example

mentioned in the intro duction and in section

Let X Y KY K K Z X K K We present a pro of of

X ZW W

and a pro of of its translation in the normal system

nf nf nf

X ZW W

n

nf

Observe that

nf

ZW X ZW and X

nf

W W

For the sake of readabilitywe omit kinding judgements

We have the following derivation in F

ok

STVar

X Y KY Y KY ZW ZW

SOApp SConv

X ZW Y KY ZW Y KY ZW ZW

STrans

X ZW ZW

ok ok

STVar STVar

Z X X Y KY

STrans

Z Y KY Y KY W W

SOApp SConv

ZW Y KY W Y KY W W

STrans

ZW W

X ZW ZW ZW W

STrans

X ZW W

The corresp onding derivation in normal form in NF is substantially shorter

W K

NSRefl

nf

Y KY W W

n

NSOApp

XW W

n

NSOApp

nf

Y KY ZW W

n

NSOApp

X ZW W n

CHAPTER DECIDABILITY OF SUBTYPING IN F

A subtyp e checking algorithm AlgF

As it stands NF as dened in section is not a deterministic algorithm be

cause its rules are not syntax directed Fortunately we are not far away from an

algorithmic presentation In fact corollary is the bridge to the algorithmic

presentation of the subtyping relation AlgF which states that transitivity steps

is obtained from can b e eliminated and reexivity steps can b e simplied AlgF

y removing NSTrans and restricting NSRefl to typ e variables and typ e NF b

applications

Definition AlgF subtyping rules

X K

AlgSTVarRefl

X X

Alg

TS K

AlgSOAppRefl

TS TS

Alg

X A X A

Alg

AlgSTVar

X A

Alg

T S S T S S

Alg Alg   

AlgSArrow

S S T T

Alg  

XU K S T X U KS

Alg

AlgSAll

X U KS X U KT

Alg

K

X K S T

Alg

AlgSOAbs

X KS X KT

Alg

nf

lub TS A TS K TS A

Alg 

AlgSOApp

TS A

Alg

if mg A T A K

Alg i

AlgS

V

K

A T T

Alg m

j f ng S A k f ng S K

Alg j k

AlgS

V

K

S S A

Alg n

if mgj f ng S T k f ng S K

Alg j i k

AlgS

V V

K K

S S T T

Alg n m

Lemma Equivalence of normal and algorithmic subtyping

Let S T K Then S T if and only if S T

n Al g

Proof By corollary Immediate

Wehave therebyproved that AlgF is indeed a sound and complete algorithm

to compute F s subtyping relation We conclude the pro of of decidability of

subtyping in F b y establishing in section that AlgF always terminates

TERMINATION OF SUBTYPE CHECKING

Proposition Equivalence of ordinary and algorithmic subtyping

nf nf

Let S K and T K Then S T if and only if S

Al g

nf

T

Proof By the equivalence of ordinary and normal subtyping theorem

and the equivalence of normal and algorithmic subtyping lemma

Termination of subtyp e checking

The last step in proving the decidability of the subtyping relation of F is proving

the termination or wellfoundedness of the relation dened by the AlgF subtyping

to the strong e show this by reducing the wellfoundedness of AlgF rules W

normalization prop erty of the relation



We b egin by extending the language of typ es with the constructor as follows



T X typ e variable

 

j T T function typ e

 

j X T K T p olymorphic typ e



j X K T op erator abstraction

 

j T T op erator application

V

K  

j T T intersection at kind K

 

j T T choice

Since wehave enriched the language of typ es with a new typ e constructor we

need to extend our kinding judgements section with the following kinding

rule

S K T K

 

KPlus

S T K



is obtained from by adding the reductions asso ciated with the choice



op erator S T S and S T T We also need the corresp onding

 

kinding rule saying that S T K whenever S T K As far as we are

aware choice op erators have not b een used b efore to analyze subtyping

Notation We write mo dulo commutativityand asso ciativity

We now dene a new reduction



Definition The reduction on typ es is obtained from

 

denition by adding the following two rules

S T S and



S T T



We also write to refer to these two new reduction rules 

CHAPTER DECIDABILITY OF SUBTYPING IN F

As usual is extended to b ecome a compatible relation with resp ect to typ e



formation  is the reexive transitive closure of and is the

  

reexive symmetric and transitive closure of



Proposition Strong normalization for If T K thenevery

 

reduction sequence starting from T is nite

Proof The result follows using the strategy used to prove that the reduction

is strongly normalizing on well kinded typ es see theorem We only

need to mo dify the denition of saturated sets by adding the following closure

condition



if T U R R SN thenTR R S and UR R S imply T U R R S

n n n n

Next we dene a measure for subtyping statements such that given a subtyp

ing rule the measure of each hyp othesis is smaller than that of the conclusion

relation dened by a set Most measures for showing the wellfoundedness of a

of inference rules involve a clever assignment of weights to judgements often in

volving the number of symbols We need a more sophisticated measure since in

AlgSOApp it is not necessarily the case that the size of the hyp othesis is smaller

than the size of the conclusion

Weintro duce a new mapping from typ es to typ es in the extended language in

order to dene a new measure on subtyping statements To motivate the denition

of this new measure we analyze the behavior of typ e variables during subtyp e

S T where S is a variable checking Assume that we want to check if

Al g

or a typ e application It can be the case that the judgement is obtained with an

application of AlgSTVar or AlgSOApp in which case we have to consider a

new statement S T where S is obtained from S by replacing a variable

Al g

by its bound and eventually normalizing However we do not replace every

variable by its b ound as this would constitute an unsound op eration with resp ect

to subtyping

Example Two unrelated variables may have the same bound

X Y X Y but

X Y

Our new mapping plus includes in eachtyp e expression this nondeterministic

behavior of its typ e variables

Definition plus



The mapping plus T T is dened as follows



T X X plus plus

  XT K 

 

S plus T S plus T plus

  

plus X T KS X plus T Kplus S

  X T K

TERMINATION OF SUBTYPE CHECKING

plus X KS X Kplus S

 X K

plus STplus S plus T

  

V V

K K

plus S S plus S plus S

n n

  

Example plus Z Z Y X

X Y X Z Y

We need to show that plus is well dened on well kinded arguments

Lemma Wellfoundedness of plus

If T K then plus T is dened



Proof Observe that the size of the kinding judgements of the arguments strictly

j

decreases in each recursive call Consider

rankplus S size S kindS

j



where size S K is the size of the derivation of the kinding judgement

j

using see denition The function kind can be dened straightforwardly

prop osition such that kindS K if S K and gives a constant

NoKind otherwise Moreover lemma implies that the function kind is total

Given that S K by lemmas and the rank decreases in each

K

recursive call and the least value is that of size K

Lemma If T K then plus T K





Proof By induction on the derivation of T K observing that T K

implies T K It is straightforward to verify that satises weakening

 

see lemma We consider here the case for KTVar the rest follows by

straightforward induction We are given X T K ok By lemma



there is a prop er sub derivation of T K Finally the result follows by the

induction hyp othesis weakening and KPlus

Lemma Strengthening for plus

Let X FTV FTV S Then



S S plus X T K S K implies plus

X X 

   XT K 

 

X X

S S plus xT S K implies plus



   x T 

 

Proof

S is dened therefore we can reason by By lemma plus

K   XT



X X

induction on the number of unfolding steps of plus We pro ceed by case

analysis on the form of S

S Y We have to consider two cases

CHAPTER DECIDABILITY OF SUBTYPING IN F

a Y T K Then by denition



plus Y Y plus T

 XT K 

 

X X

On the other hand also by the denition of plus

plus Y Y plus T

 

 

b Y T K By the denition of plus

 

plus Y Y plus T

 XT K   XT K

  

X X X X

By lemma

X T K ok

X X 

and by lemma

X T K T K

X X

FTV Moreover since X FTV it follows that X



FTVT Then applying the induction hyp othesis we obtain

Y plus T Y plus T

 XT K 

   

X X

and the result follows by the denition of plus

S Y T K T By the denition of plus



plus Y T K T



 XT K 



X X

Y plus T K plus T



 T K   X T K  Y T K

   

X X X X

By generation for kinding prop osition

X T K Y T K T

X X  

y and since X FTV Y T K FTVT by the induction h

 

p othesis

Y plus T K plus T



 T K   XT K  Y T K

   

X X X X

Y plus T K plus T



 T K    Y T K

   

X X

By lemma

X T K Y T K ok

X X 

by syntax directedness of context judgements lemma

X T K T K

X X 

Since X FTV FTVT by the induction hyp othesis



Y plus T K plus T



 T K    Y T K

   

X X

Y plus T K plus T



    Y T K

   

plus Y T K T



 



For all the other cases the result follows by straightforward application of

the induction hyp othesis using generation for kinding prop osition

TERMINATION OF SUBTYPE CHECKING

the denition of plus do es not dep end on the assumptions of term variables

Lemma Weakening for plus If ok and S K then

S plus S plus



 

Proof The assumptions ensure that plus S is dened so we can pro ceed by



induction on the number of unfolding steps of the denition of plus We pro ceed

by case analysis on the form of S

By generation for kinding prop osition and the fact that S X

X T K and



X T K



There are two cases to consider

then the result follows by the denition of plus If

If then



By the denition of plus

plus T plus X X

 



By lemmas and it follows that T K Hence by

the induction hyp othesis

T X plus T X plus



 



Since ok from lemma it follows that T K Con

sequently fX g FTV FTVT by the free variables lemma



lemma Hence starting from the last declaration in w e can



iterate the strengthening lemma for plus lemma items and to

obtain

X T X plus  T plus X plus

 

  



S X T K T By the denition of plus



T plus X T K T X plus T K plus

 

  XT K

 

By generation for kinding prop osition and lemmas and

the induction hyp othesis it follows that T K Then by

X plus T K plus T X plus T K plus T



 

 XT K  XT K

   

By generation for kinding By weakening for kinding

X T K T



Apply lemma and by CTVar X T K ok

T K

ing again the induction hyp othesis it follows that

T K plus X plus T





XT K 

 

X plus T K plus T

 



  X T K

 

X T K T plus



 

CHAPTER DECIDABILITY OF SUBTYPING IN F

S X KT By the denition of plus

plus X KT X Kplus T

 X K

K K

By KMeetitfollows that K and by CTVar X K

ok Finallythe result follows by the induction hyp othesis

In all other cases the pro of follows bystraightforward application of the induction

hyp othesis

The op eration plus do es not have the usual prop erties under substitution as

wing example shows the equality follo

plus T X plus T plus T X T

 

 XS K     X T

    

do es not hold in general

Example Consider the case where

Y S Y T Y and T X

 

Then

Y X Y X Y plus X X plus

Y Y X Y

Y Y

On the other hand

plus X X Y plus Y Y

Y Y

We therefore need a lemma which says that the welltyp ed typ es are well

behaved under substitution with resp ect to the plus op eration

Lemma Substitution for plus If X S K T K and

  

T K then

plus T X plus T  plus T X T

  

 XS K     X T

    

Proof By induction on the size of the derivation of X S K T K

  

We pro ceed by case analysis on the form of T



T Y By the free variables lemma lemma Y dom X S K

 

Then there are three cases to consider

Y dom Let Y U K Then



plus Y X plus T

 XS K  

  

by the denitions of plus and substitution

Y plus U X plus T



 

since X FTV U FTV X FTVplus U



U Y plus



plus Y X T

  X T

 

TERMINATION OF SUBTYPE CHECKING

Y X Then

plus X X plus T

 XS K  

  

by the denitions of plus and substitution

plus T plus U X plus T

 

  

plus T







On the other hand

plus X X T

  X T

 

plus T

  X T

 

since FTVT dom by strengthening for plus

plus T





Y dom Let Y U K Then

  

plus Y X plus T

 XS K  

  

by the denitions of plus and substitution

U X plus T Y plus

 XS K 

   

by generation and the induction hyp othesis

Y plus U X T



 X T

  

plus Y X T

  X T

 

T Y S KS Let X S K Then

  

plus Y S KS X plus T



 



by the denitions of plus and substitution

Y plus S X plus T Kplus S X plus T



  Y S K 

  

by generation prop osition and the induction hyp othesis

Y plus S X T K



  X T

 

plus S X T



  X T Y S X T K

   

by the denitions of plus and substitution

plus Y S KS X T



  X T

 

Other cases All the other cases are similar to the case T Y S KS

 

Lemma Monotonicityofplus with resp ect to If T K then

implies plus T  plus T





 

T T implies plus T  plus T



 

Proof By simultaneous induction on the size of the derivation of T K

We pro ceed by case analysis on the form of T

T X Let X S K Then we have to consider three cases



CHAPTER DECIDABILITY OF SUBTYPING IN F

a Then

plus X X plus S

 



by lemma and part of the induction hyp othesis

 X plus  S plus  X

 

 

b S S By lemma and part of the induction hyp o

thesis

By the denition of plus c





K T By generation for kinds prop osition there are T X T



prop er sub derivations of T K and X T K T



Then by part of the induction hyp othesis it follows that

T and plus T  plus



 

plus T  plus T



 

XT K  XT K

   

The result follows by the denitions of plus and 

Other cases The rest of the cases are similar to the case T X T K T



using generation for kinding prop osition and part of the in

duction hyp othesis

T T

T X T K T We have to consider three cases



a T T By generation for kinding prop osition there are

prop er sub derivations of T K and X T K T



Then by parts and of the induction hyp othesis resp ectively

it follows that

plus T  plus T and

 



plus T  plus T

 

XT K XT K

   

The result follows by the denitions of plus and 

b T T By part of the induction hyp othesis





V V

X T K S X T K S S S c X T K

n n

V

S S plus X T K

n



V

plus X plus T K S plus S

n

XT K  XT K

   

V

X plus T Kplus S



 XT K

 

X plus T Kplus S

n

 XT K

 

V

X T K S X T K S plus

n



T T T We have to consider four cases



a T T

b T T





V V V

c T S S and S S T S T S T

n n   n 

d T X KS and X KS T S X T

 

TERMINATION OF SUBTYPE CHECKING

Cases a b and c follow using similar arguments to those used for

the case T X T K T Consider case d



plus X KS T





X Kplus S plus T

K





X K

plus S X plus T

K





X K

by lemma

plus S X T 

 



Other cases The rest of the cases follows using a similar argument to the

one used in the case T X T K T



Lemma Let lub S be dened and S K Then plus S 

 



plus lub S





Proof By induction on the structure of S Since lub S is dened it is enough



to consider the following two cases

S X Let X T K



plus X X plus T

 



X plus T by weakening lemma



plus T





plus lub X





S AT By the induction hyp othesis

Our measure to show the wellfoundedness of AlgF considers the reduction

e men paths of the plus versions of the typ es in the subtyping judgements As w

tioned b efore in AlgSTVar and AlgSOApp the typ es app earing in the hyp o

thesis may be larger than those in their conclusions Therefore the well foun

dedness of the AlgF relation is not immediate The next corollary gathers the

previous results to serve our purp oses

Corollary

If X K then plus X  plus X



 

nf

If AT K then plus AT  plus lub AT

 

 

Proof Item is a particular case of the previous lemma lemma and

item is a consequence of lemma and the monotonicityof plus with resp ect

to



Finallywe can dene our measure

Definition Weight

CHAPTER DECIDABILITY OF SUBTYPING IN F

weight S T maxredplus S maxredplus T size

Al g j

 

S T

weight T K

where maxredS is the length of a maximal reduction path starting from

S and size is dened in denition

j

Pairs are ordered lexicographically Note that is the least weight

J J

n

If AlgF rule Proposition Wellfoundedness of is an AlgF

J

then weightJ weightJ for each ifng

i

Proof By insp ection of the rules of AlgF

Finallywe can state our main result

Theorem Decidabilityofsubtyping in F

For any context and for any two typ es S and T it is decidable whether

S T

Our decidability pro of and full F

tro duction to chapter we mentioned that subtyping in F a second In the in

order calculus with b ounded quantication dened by Curien and Ghelli in

is undecidable A question that comes to mind is if we try to apply our pro of of

the decidabilityofsubtyping in F to F where will it fail

If we consider the algorithm for the subtyping relation in Ghe the place

where our pro of do es not go through is when we try to prove that the algorithm

terminates by calculating the maximal length of the plus versions of the typ es in

subtyping quantied typ es Remember that the subtyping rule for the rule for

quantied typ es in full F is

T S X T S T

 

F SAll

X S S X T T

 

Consider now the following case

Y Y Y Y Y Y Y

     

T Y

S

T X X and



S X X 

OUR DECIDABILITY PROOF AND FULL F

The plus versions of the typ es in the subtyping statements of this example are as

follows

plus S X Y Y Y Y X Y Y Y Y

      

XY



plus T X Y Y Y Y X Y Y Y Y

      

XY



plus X S S X X X





plus X T T X Y Y Y Y

   



X Y Y Y Y X Y Y Y Y

     

The length of a maximal reduction in each case is

maxredplus S



XY



maxredplus T



XY



maxredplus X S S





maxredplus X T T





as dened in den The weight of the conclusion X S S X T T

 

ition is smaller than the weight of the hyp othesis X T S T

 

b ecause the maximal length of a reduction starting from the plus version of the

conclusion is shorter than the maximal length of a reduction starting from the

plus version of that hyp othesis Tobe more precise

maxredplus X S S maxredplus X T T

 

 

maxredplus S maxredplus T

 

XY XY

 

CHAPTER DECIDABILITY OF SUBTYPING IN F

Chapter

Typing in F

Typ e checking and typ e inference

Given a context a term e and a typ e T typ e checking consists of analyzing

whether the judgement e T is derivable from a given set of inference

rules Typ e checking algorithms for lambda calculi unless they are formulated

using Gentzens sequent calculus style involve guessing the typ e of subterms For

yp e of e is not necessarily a sub expression of T example when e is e e the t

 

and in order to corrob orate or to refute the assertion e T we need to infer

a typ e for e



In this section we present an algorithm for inferring minimal typ es in F

Given and e the typ e S constructed by the algorithm is a subtyp e of every T

T to such that e T In this way we reduce the problem of whether e

that of inferring a typ e S such that e S and S T Solving this problem

involves not only the typing rules but all the inference rules of F the rule T

Subsumption dep ends on a subtyping judgement the rule TVar dependsonanok

judgement and the ok judgements dep end on kinding judgements Consequently

typ e checking uses the full p ower of the F system

As an example consider typ e checking the following judgement

X T T f X aT fa T

 

The application fa can only be formed if f has an arrow typ e Using TVar we

can assign typ e X to f which means that in order to obtain an arrow typ e for f

we have to replace X by its bound which has the right form Observe how this

replacement is p erformed by TSubsumption in the following derivation

X T T fX aT ok X T T fX aT ok

 

X T T fX aT f X X T T fX aT X T T

  

TSub

X T T fX aT f T T

 

X T T fX aT ok



X T T fX a T f T T X T T fX aT a T

  

TApp

X T T fX aT fa T

 

CHAPTER TYPING IN F

Note that in the presence of TSubsumption we may actually p erform the ap

plication when the typ e of a isasubtyp e of T Namelyif

X T T fX aU a U X T T fX aU U T

 

TSub

X T T fX aU a T



Moreover we maywant to check whether

X T T f X aU fa U

 

where T isasubtyp e of U

 

The situation gets more complicated if f has an intersection typ e Supp ose

that

X T T Y S S f X Y Z V KV aU fa U

   

where U is a subtyp e of T and S An algorithm should not consider the typ e

Z V KV for f since in this case f is applied to a term and not to a typ e



Then it has to replace X and Y by their b ounds T T and S S Moreover

 

should check given that the typ e of a U is a subtyp e of b oth S and T it

whether S T is a subtyp e of U

  

Another source of problems in the search for an algorithmic presentation of

the typing rules is that typ es may not b e in normal form Consider the judgement

X T T Z Y Y f ZX aT fa T

 

In order to typ e the application f should b e assigned typ e T T Todothat Z



should b e replaced by its b ound in ZX This replacement pro duces a typ e which

is not in normal form so Y Y X has to be normalised to obtain X Finally

X is replaced by its b ound and then the application can be typ ed

The main new source of dicultyis the interaction b etween the need for nor

malization and the presence of intersection typ es

An algorithm to infer typ es should pro ceed structurally on the form of the term

whose typ e is to b e inferred This requires us to remove the rules whichmake our

typing rules nondeterministic we should eliminate TSubsumption and TMeet

from the original presentation and mo dify the other rules in such a way that we

can still typ e the same set of terms

We give some preliminary denitions and results b efore presenting the rules of

our new system

replacements which we We dene the mapping ub which p erforms the

motivated with the previous examples

We dene the function arrows to lter arrow typ es in order to deal with

term application

We dene the function al ls to lter p olymorphic typ es to deal with typ e application

TYPE CHECKING AND TYPE INFERENCE

The function lub denition is a partial function which is only dened

for typ e variables and typ e applications Here we extend the denition of lub

to intersection typ es in such a way that it is dened if the least upp er bound is

dened for at least one of the typ es in the intersection

Definition Homomorphic extension of lub to intersections lub

lub X X



S T lub ST lub

 

V V

K K

lub T T T T if ifng such that lub T

n i

 n 

T and T otherwise T if lub is lub where T

i i i

  i

Lemma If lub T is dened then T lub T

 

Proof By induction on the complexit y of T using corollary

T be dened and T K Then plus T  Lemma Let lub







plus lub T





Proof The pro of follows by induction on the structure of T If T X or

T ST then the argument is the same as in lemma The case remaining

V

K

to b e checked is when T T T Then

n

V V

K K

T T plus T plus T plus

n n

  

V V

K K

plus T plus T T T plus lub

n

  

n 

where T T or T lub T Since lub T is dened there exists j fng

i i

i  

such that lub T is dened Now for every k such that lub T is dened by

j k

 

the induction hyp othesis we have that

T plus lub plus T 

k k 

 



Hence

V V

K K

plus T T  plus lub T T

n  n

 



We dene the mapping ub whic h given a typ e T and a context nds the

smallest typ e larger than T with resp ect to the subtyp e relation having structural

information to p erform an application

Definition Functional Least Upp er Bound The functional least upp er

bound of a typ e T inacontext ub T is dened as follows



nf nf

ub lub T if lub T



 

ub T



nf

T otherwise

CHAPTER TYPING IN F

The intuition b ehind the denition of the function ub is to nd T T starting



form ZX in the example ab ove In other words ub ZX T T For





simplicitywe assume T T in normal form Step by step



ub ZX ub lub ZX

 



ub Y KY X



nf

ub lub Y KY X





ub lub X





ub T T





T T



More generally ub climbs the subtyping hierarchy until it nds an arrow a

quantieroranintersection of these two To show that ub is welldened we use

a similar argument to that used in section to show that the relation dened by

AlgF is wellfounded We show in lemma that a maximal reduction

of ub is strictly longer than a maximal path of the plus version of the argument

reduction path of the plus version of the argument of its recursive call

Lemma Wellfoundedness of ub

If T K then ub T is dened



nf

Proof If lub T is undened ub terminates b ecause is strongly nor



malizing on well kinded typ es Otherwise dene

weightub T maxredplus T

 

where maxredS is the length of a maximal reduction path starting from S

Lemma and the strong normalization prop ertyof imply that weight is



nf

well dened and always p ositiveonwell kinded typ es Since lub T isdened



nf

plus T  plus T by lemma



 

nf

 plus lub T by lemma







Then the weight of the arguments of ub reduces in each recursive call which

proves that ub is wellfounded

Lemma Let S T and S T Then ub S ub T

 

Definition arrows and al ls

arrows T T fT T g

 

V

arrows T T arrows T

n ifng i

V

arrows T if T T T and T T T

 n

This step can b e optimised in an implementation of the typ e checking algorithm allowing

us to avoid the normalization of T when T is either an arrowtyp e or a quantied typ e

TYPE CHECKING AND TYPE INFERENCE

al ls X T KT fX T KT g

 

V

al ls T T al ls T

n i

ifng

V

al ls T if T X T KT and T T T

 n

The situation here is signicantly more complex than in Pie for F an

calculus There it is enough to recursively search extension of the second order

for arrows or p olymorphic typ es in the context b ecause in F there is no reduction

on typ es The information to b e searched for is explicit in the context so the job

done here by ub is simply an extra case in the denition of arrows and al ls

Namely

arrows X arrows X and

al ls X al ls X

Moreover to prove that ub is wellfounded is similar for us in complexity to

proving termination of subtyp e checking The similarity comes from the fact that

computing ub involves replacing variables by their b ounds in a given con text and

normalizing with resp ect to as in lemma In contrast in Pie it

is enough to observe that wellformed contexts cannot contain cycles of variable

references

Notation We intro duce a new notation for intersection typ es We write

V

K

T j T meaning the intersection of all typ es T such that T holds Note

V

K

that this is an alternative notation to T T suchthatT holds if and only

n i

fng if i

We can now dene a typ e inference algorithm for F

Definition A typ e inference algorithm inf

xT ok



ATVar

xT x T

 inf

xT e T

inf 

ATAbs

xT e T T

inf 

f T a S

inf inf

ATApp

V

fa T j S T arrows ub T and S S

inf i i i i



X T K e T

inf 

ATTAbs

X T K e X T K T

inf 

f T

inf

V

T X S jX S KT al ls ub T and S S fS

i i i i inf



ATTApp

for all ifng eX S T

inf i i

ATFor

V

forX S S e T T

inf n n

CHAPTER TYPING IN F

The algorithmic information of rule ATApp is that in order to nd a typ e for

fa in we need to infer a typ e S for a and a typ e T for f and to take the

intersection of all the T s such that T S arrows ub T and S S

i i i



i

Minimal typing

satises the minimal typing prop erty theorem In this section we show that F

We rst prove that the algorithm inf is sound with resp ect to F if

e T then e T prop osition We then prove that every closed

inf

term is typ eable using either set of typing rules lemma Finallywe prove

that inf computes minimal typ es for F terms prop osition

Lemma Let T Then T ub T



Proof Since ub is wellfounded we can pro ceed by induction on the number

nf

steps in ub T If ub T T the result follows by SConv of unfolding

 

nf

Otherwise ub T ub lub T By SConv

 



nf

T T

By lemma

nf nf

T lub T



By the induction hyp othesis

nf nf

lub T ub lub T



 

Finallyby STransthe result follows

Lemma Let T Then

V

T S j S arrows ub T



V

T S j S al ls ub T



Proof

Using lemma we reduce our problem to proving that

V

T S j S arrows T

which follows by induction on the structure of T

Similar to

Lemma

V

If T T T S j S arrows ub T T T then

 



V

If T X T KT then S j S al ls ub T X T KT

  

MINIMAL TYPING

Proof

By induction on the derivation of T T T The last rule of a



derivation of this subtyping statement can only be SConv STVar S

TransorSMeetLB The rst three cases use similar arguments therefore

we consider here only the cases for SConv and SMeetLB

SConv We are given that T T T By lemma and the denition



of ub we have that

T T arrows ub T arrows ub



 

nf

arrows T T



We now have two cases to analyze

nf nf

nf

a If T T T T then the result follows by SMeetLB





and SConv

V

nf nf nf

nf

b Otherwise let T T T U T U where T

 n



V

U U Then

n

nf nf

U T U g fT arrows ub T

n



Consequently

V

nf

S j S arrows ub T T T





and the result follows by SConv

SMeetLB We are given that

V

S T T S T T

 n 

By the denition of ub

V

ub S T T S

 n



V

nf nf

A A T T

m

V

nf

A A or where T

m



nf

A T



Now

V

S T T S arrows ub

 n



nf nf

fT A T A g

m

V

nf nf

Then if T A A by lemma and if T A by

m

 

SMeetLB we have that

V V

nf

S j S arrows ub S T T S T T

 n 



Finallythe result follows by SConv

Similar to previous item

Proposition Soundness of inf If e T then e T inf

CHAPTER TYPING IN F

Proof By induction on the derivation of e T The interesting cases are

inf

when the last applied rule is either ATApp and ATTApp

ATApp We are given that

V

fa T j S T arrows ub T and S S

inf i i i i



is derived from

f T and a S

inf inf

V

If T j S T arrows ub T and S S then the

i i i i



result follows immediately using TMeet Otherwise by the induction

hyp othesis we have that f T By lemma SMeet

LB STrans and TSubsumption f S T By the induction

i i

hyp othesis and TSubsumption a S By TApp fa T

i i

Finallyby TMeet

V

fa T j S T arrows ub T and S S

i i i i



ATTApp We are given that

fS

inf

V

T X S jX S KT al ls ub T and S S

i i i i



is derived from f T

inf

V

then the If T j S T arrows ub T and S S

i i i i



result follows immediatelyusing TMeet Otherwise assume

V

al ls ub T X S KT X S KT

n n



By the induction hyp othesis we have that f T By lemma

SMeetLB STrans and TSubsumption it follows that

f X S KT Since S S by TApp fS

i i i

T X S Finallyby TMeet

i

fS

V

T X S jX S KT al ls ub T and S S

i i i i



Lemma Term application

V

If S T S T S T and U S

n n

V

then T j U S T

j j

Proof There are two cases to be considered according to the normal form of

nf nf nf

S T The case when S T S T is similar to but simpler than the one

we consider here Assume

V V

nf nf nf nf

S T S A S A where T A A

m m

By the equivalence of ordinary and normal subtyping theorem

V V

nf

nf nf

nf nf k nf nf k

n



S A S A B S B S B B S S

m n

n n n n

B if it is not an intersection

nf

i

V

where T

k

i

i

B B otherwise

i i

MINIMAL TYPING

By generation for normal subtyping prop osition for each ifmg there

exist j fng and l fik g such that

j j

l

nf nf nf

j

S B S A

n i

j

j

Again by generation for normal subtyping prop osition for each ifmg

there exist j fng and l fik g such that

j j

nf

nf nf

and S S

n

j

l

nf j

B A

n i

j

alence of ordinary and normal subtyping theorem By NSTrans and the equiv

for each ifmg there exist j fng and l fi k g such that

j j

U S and

j

l

j

nf

B A

n i

j

By NS for each ifmg there exist j fng such that

U S and

j

V

k

j

nf

A B B

i n

j

j

alence of ordinary and normal subtyping theorem for each By the equiv

ifmg there exist j fng such that

U S and

j

T A

j i

By lemma SConv and STrans

V

T j U S T

j j

Lemma Substitution for subtyping

If S T and X T K S T then X S

   

S X S T X S

 

Proof By straightforward induction on the derivation of X T K



S T using the weakening lemma lemma the typ e substitution lemma

 

lemma and lemma

Lemma Typ e application

V

X S K T X S KT and U S If X S K T

n n n

V

then T X U j U S T X U

j j

CHAPTER TYPING IN F

Proof There are two cases to be considered according to the normal form of

nf nf nf

X S KT The case when X S KT X S KT is similar to but

simpler than the one we consider here Assume

V

nf

nf nf

X S KT X S KA X S KA

m

V

nf

where T A A By the equivalence of ordinary and normal subtyping

m

theorem

V

nf nf

k nf nf k nf

n



K B X S X S X S K B K B K B X S

n n n

n n n n

V

nf nf

X S KA X S KA

m

if it is not an intersection B

nf

i

V

where T

k i

i

B B otherwise

i i

By generation for normal subtyping prop osition for each ifmg there

exist j fng and l fik g such that

j j

l

nf nf nf

j

X S KA K B X S

i j n

j

j

Again by generation for normal subtyping prop osition for each ifmg

h that there exist j fng and l fik g suc

j j

K K

j

nf

nf

S S and

j

l

nf

j

nf

X S K B A

n i

j j

By NS for each ifmg there exist j fng such that

nf nf nf

X S K T A

n i

j j

By the equivalence of ordinary and normal subtyping theorem for each

g such that ifmg there exist j fn

X S K T A

j j i

Furthermore by SConv and STrans

U S

j

Then by the substitution lemma for subtyping lemma for each ifmg

there exist j fng such that

T X U A X U

j i

By NS

V V

T X U j U S A X U A X U

j j m

By the denition of substitution

V

nf

T X U j U S T X U

j j

MINIMAL TYPING

Finallyby lemma SConv and STrans

V

T X U j U S T X U

j j

Usually the next step to prove the accuracy of an algorithm inf in our case

would b e to prove a completeness result if the term e has typ e T with resp ect to

then the algorithm inf nds a typ e T the context in the the typing system F

t situation this result is not strong enough since every for e in In the presen

closed term is typ eable in b oth systems One easily proves that

Lemma

If e is closed in then there exists T such that e T

If e is closed in then there exists T such that e T

inf

We use the fact that inf is deterministic which means that the rules are in

vertible to prove the imp ortant prop erty that it nds a minimal typ e

Proposition Generation for inf

T then T x If x

inf

If xT e T then T T T where xT e T as a sub

inf  inf 

derivation

If fa T then

inf

V

T j S T arrows ub U and S S T

i i i i



where f U and a S as sub derivations

inf inf

If X T K e T then

inf

T X T K T



where X T K e T as a sub derivation

inf 

If fS T then

inf

V

T X S jX S KT al ls ub U and S S T

i i i i



where f U as a sub deriv ation

inf

V

If forX S S e T then T T T where eX S

inf n n inf i

T for each ifng as sub derivations

i

Proof The form of the term in the antecedent uniquely determines the last rule

of its derivation

Proposition Minimal typing

If e T and e T then T T inf

CHAPTER TYPING IN F

Proof By induction on the derivation of e T

TVar By generation for inf prop osition T T and then the result

follows by SConv

TAbs We are given that

e xT e



T T T and



x T e T

 

By generation for inf prop osition

and T T T



x T e T

inf 



T and by strengthening By the induction hyp othesis xT T





T from which it follows that T T lemma T





TApp We are given that

e fa

f V T and

a V

By generation for inf prop osition

f U

inf

a S and

inf

V

T T j S T arrows ub U and S S

i i i i



By the induction hyp othesis

U V T and

S V

By lemma

V

S T j S T arrows ub U V T

i i i i



Finallyby the term application lemma lemma it follows that

V

T j S S T

i i

ub U where S T arrows

i i



In other words

V

T j S T arrows ub U T

i i i 

MINIMAL TYPING

TTAbs We are given that

e X T K e



T X T K T and



X T K e T



By generation for inf prop osition

T X T K T and



X T K e T

inf 



T Then by SAll T By the induction hyp othesis X T K





it follows that T T

TTApp We are given that

e fS

T T X S f X T KT and

 

S T

By generation for inf prop osition

V

T U jX S K U al ls ub U and S S and

i i i i i



f U

inf

By the induction hyp othesis U X T KT By lemma



V

X S K U jX S K U al ls ub U X T KT

i i i i i i 



Then by the typ e application lemma lemma it follows that

V

U X S j S S T X S

i i 

S K U al ls ub U where X

i i i



Namely

V

U X S jX S K U al ls ub U and S S

i i i i i



T X S



TFor We are given that

e forX S S e

n 

e X S T and



S fS S g

n

CHAPTER TYPING IN F

By generation for inf prop osition

V

T T and T

m

e X S T for each iinfmg

inf  i

By the induction hyp othesis SMeetLB and STrans we have that

V

T T T

m

TMeet By the induction hyp othesis and SMeetG

TSub By the induction hyp othesis and STrans

following result Finallywe have proved the

Theorem Minimal typing for F Given a term e and a context there

exists T such that for every T if e T then T T

Decidability of typ e checking and typ e infer

ence

In the previous section we proved that the algorithm inf is sound and computes

minimal typ es for the F typing system The next step is to prove that the

algorithm inf always terminates This result completes the pro of of decidability

of typ e checking and typ e inference in F

We rst dene a measure for terms such that the typ e information inside the

terms is considered to have constant value The intuition b ehind the denition is

to nd a measure on terms whichisinvariant under typ e substitution see lemma

Definition size kk

kxk

kx Tek kek

ke e k ke k ke k

 

k kek kX T Ke

keTk kek

kforX T T ek kek

n

Lemma kek keX T k

Proposition Wellfoundedness of inf

The inference rules for inf dene a terminating algorithm

SUBJECT REDUCTION

Proof In the case of ATVar the termination follows from the decidability of

ok judgements see corollary Furthermore for each rule R of inf if

e T is a hyp othesis and e T is the conclusion of R then kek ke k

Moreover in the cases for ATApp and ATTApp f T by the soundness

of inf prop osition T by w ellkindedness of typing prop osition

Hence ub T is dened by lemma Furthermore arrows and al ls



dene nite sets and as we proved in section subtyping is decidable Hence

the algorithm inf always terminates

We can now state and prove that typ e checking in F is decidable

Theorem Decidabilityoftyp e checking in F

For any context and for any term e and typ e T closed in it is decidable

e T whether

Proof Infer a minimal typ e T for e in using inf which is decidable by

T which is also decidable by prop osition and check whether T

theorem

Every term e closed in a context has typ e We are interested in nding

typ es other than namely nontrivial typ es Since inf computes minimal typ es

term has a non trivial typ e in a and is the largest typ e mo dulo if a

given context then the algorithm inf nds it

Theorem Decidabilityoftyp e inference in F

For any context and for an y term e closed in it is decidable whether there

exists a typ e T such that e T and T

Proof Infer a minimal typ e T for e in using inf which is decidable by pro

p osition and reduce T to normal form which is decidable because is

nf

strongly normalising see theorem Finally check whether T

Sub ject reduction

The F system is layered in three syntactic categories kinds typ es and terms

Since terms do not app ear in either typ es or kinds reductions in typ e expressions

can be studied indep endently from the reductions of terms In section we

proved that reduction on typ es preserves kinding prop erties the sublanguage of

typ es and kinds satises the sub ject reduction prop erty lemma

if S K and S  T then T K

w the sub ject reduction prop erty for typing judgements In this section we sho

prop osition

if e T and e  e then e T

for

In other words reductions on terms are also safe

CHAPTER TYPING IN F

Lemma If Y FVS then

eY T X S eX S Y T X S

U Y T X S U X S Y T X S

Proof By induction on the structure of e and U resp ectively

Lemma Substitution for typing

xS e S then e xe S If e S and

     

If S S and X S K e S then X S

   

e X S S X S

 

Proof

xS e S By induction on the derivation of

  

By induction on the derivation of X S K e S using the

  

typ e substitution lemma lemma in the TVar and TMeet cases

the substitution lemma for subtyping lemma and lemma in the

case for TTApp lemma in the TFor case and the substitution lemma

for subtyping lemma in the TSubsumption case

Lemma T if and only if T and T

Proof If T then the result follows by SConv Otherwise if

T by the wellkindedness of subtyping prop osition TMeet and

uniqueness of kinds lemma T By the equivalence of ordinary and

nf nf

algorithmic subtyping prop osition T which can only be

Al g

nf

derived using AlgS where T is the empty intersection

Given S T generation for normal subtyping prop osition and the

equivalence of ordinary and normal subtyping theorem provide subtyping

T We can also show that subtyping information ab out the normal forms of S and

is structural for arrow typ es quantied typ es and typ e op erators without redu

cing the terms in the subtyping relation to normal form An implementation of a

subtyping algorithm for F could take advantage of this fact by delaying normal

izing steps which might result in having to consider fewer recursive calls or calls

with smaller arguments

Lemma Generation for ordinary subtyping

T T S S and S if and only if S T and

  

T S

 

X T K T X S K S and S if and only if K K

T  S   S T

T S and X T K T S

T  

SUBJECT REDUCTION

X K T X K S and S if and only if X K T

T  S   S 

S and K K

 T S

Proof The three statements are proved using a similar argument We consider

T S here the pro of of part If K K T S and X T K

  S T T

then by SAll and SConvX T K T X S K S Conversely let

T  S 

X T K T X S K S and S

T  S  

nf

Lemma implies that T Then we have to consider four cases



nf nf

according to whether S are intersection typ es or not We illustrate the and T

 

pro of argument considering just one case Let

nf

nf nf

K T X T K T X T and

T T 



V

nf nf nf

K A K A X S X S X S K S

S n S S 

V

nf

By the equivalence of ordinary and normal subtyping A A where S

n



theorem and generation for normal subtyping prop osition for each

ifng

nf nf nf nf

X T K T X S K A

n T S i



and again generation for normal subtyping prop osition implies that

nf nf

nf

A and K T X T

i T n



nf nf

T S

By NS

nf nf nf nf

X T K T S

T n

 

and by the equivalence of ordinary and normal subtyping theorem

X T K T S

T  

Lemma Generation for typing

If xS e S then there exists S such that xS e S and

 

S S S



If X S K e S then there exists S such that X S K e S

 

and X S K S S



If forX fU U ge T then there exist T T such that for each i

n n

V

in fng eX U T and T T T

i i n

of the typing Proof Each statement is proved by induction on the derivation

statement in the antecedent We exhibit here the pro of of part We pro ceed by

case analysis on the last rule of the derivation of forX fU U ge T

n

CHAPTER TYPING IN F

TFor We are given that eX U T for some U fU U g Since every

n

closed term has a typ e wehave that for each i in fng eX U

i

V

T and by SMeetLB T T T T

i n

V

TMeet Let T S S We are given that

k

ok and

forX fU U ge S for each j in fk g

n j

By the induction hyp othesis for each j fk g and each ifng there

exist T such that

j

i

eX U T and

i j

i

V

T T S

j j j

n



and by the minimal typ e prop erty theorem there exist T T

n

such that

e X U T and

i i

T T

i j

i

V V

by lemma it follows that T T T T and by

n j j

n



V

STrans T T S Finally by SMeetG it follows that

n j

V V

T T S S

n k

TSub W e are given that

forX fU U ge S and

n

S T

The result follows by the induction hyp othesis and STrans

Since terms cannot o ccur in typ es sub ject reduction for terms do es not need

to consider reductions in contexts

Proposition One step sub ject reduction for typing judgements

If e T and e e then e T

for

the interesting case is when T Proof Since every term has typ e

This prop osition follows by induction on the derivation of e T We consider

the cases where e is a redex the other cases followby straightforward application

of the induction hyp othesis

TApp There are two p ossibilities for e to b e a redex

SUBJECT REDUCTION

e xS e e e e x e and T T We are given that

  

xS e T T and e T

 

By the generation lemma for typing lemma there exists S



such that

T x S e S and S S T

  

Since T by the generation lemma for ordinary subtyping



lemma

T S and S T

 

Then by TSubsumption it follows that

x S e T and e S

 

Finallyby the substitution lemma for typing lemma

e xe T

 

e e forX U U e e and T T e forX U U e

n   n 

We are given that

forX U U e T T and e T

n  

By the generation lemma for typing lemma there exist V V

n

such that

e X U V for each ifng and

 i i

V

V V T T

n 

nf

A if it is not an intersection We write V

i

i



V

nf

A A otherwise V

i i

i



k

i

V V

nf

A A A A By the equival V V Note that

n n n

 

k k

n



ence of ordinary and normal subtyping theorem

V

nf nf

T T A A A A

 n n n

 

k k

n



nf

We have to consider two cases according to the form of T T



nf nf

nf

a T T T T By generation for normal subtyping





prop osition there exist l fng and j fk g such

l

that

nf nf nf

A T T

n l

j 

and by NS or NSRefl

nf nf

V A

n l

l j

by NSTrans

nf nf nf nf

V T T

n

l 

CHAPTER TYPING IN F

and by the equivalence of ordinary and normal subtyping the

orem

V T T

l 

Then by TSubsumption

e X U T T

 l 

By TApp

e X U e T

 l 

and since X is not afree variable of e we have that

e e X U T

 l 

Finally applying TF or we have that

forX U U e e T

n  

V V

nf nf nf

nf

B B B where T B T b T T T

r r 



By generation for normal subtyping prop osition for

every sfr g there exist l fng and j fk g such that

l

nf nf

B A T

s n l

j

and by NS or NSRefl

nf nf

V A

n l

l j

by NSTrans for every sfr g there exists l fng such

that

nf nf nf

V T B

n s

l

and by the equivalence of ordinary and normal subtyping the

orem

V T B

l s

By TSubsumptionforevery sfr g there exists l fn g

e X S T B

 l s

By TApp for every sfr g there exists l fng

e X S e B

 l s

and since X is not a free variable of e we have that for every

sfr g there exists l fng

e e X S B

 l s

Applying TF or we get that for every sfr g

forX U U e e B

n  s

by TMeet

nf

forX U U e e T

n 



Finallyby SConv and TSubsumption

forX U U e e T

n  

TTApp There are two p ossibilities for e to be a redex The case when e

U U e S follows a similar argument to the one used for the forX

n 

case e forX U U e e in TApp

n 

SUBJECT REDUCTION

If wehave

e X S K e S e e X S and T T X S

S   

that X S K e X T K T and S T By the gener

S T 

ation lemma for typing lemma there exists S such that



S and X S K e

 S 

X S K S X T K T

S  T 

Since T X S lemma implies that T Then

 

by the generation lemma for ordinary subtyping lemma

X S K S T S T and K K

S   S T

By TSubsumption X S K e T and by STrans and S

S  

Conv S S Finallyby the substitution lemma for typing lemma

e X S T X S

 

TFor Let e forX U U e whereX FTVe and e e We are given

n

that e X U T with U fU U g Since e e X U the

n

result holds

We now have all the results needed in order to prove that reduction on terms

preserves typing The following prop osition the sub ject reduction prop erty for

F terms is a consequence of the previous one

Proposition Sub ject reduction for typing judgements

If e T and e  e then e T

for

Proof By induction on the derivation of e  e using prop osition

for

CHAPTER TYPING IN F

Chapter

A PER Mo del for F

Intro duction

This chapter is based on CP The dierences come from having replaced the

distributivitysubtyping rules by reduction rules Among simplest mo dels for typ ed

calculi are those based on partial equivalence relations PERs A mo del in this

style is essentially untyp ed terms are interpreted by erasing all typ e information

and interpreting the resulting pure term as an element of the mo del A typ e

in this setting is just a subset of the mo del along with an appropriate notion of

inclusion of equivalence of elements Co ercions between typ es are interpreted as

PERs

Our PER mo del for F extends the mo del of F given in Pie which is

based on Bruce and Longos mo del for F BL The usual interpretation of a

quantied typ e XT in a PER mo del is the PERindexed intersection of all p os

sible instances of T Bruce and Longo rened this denition to interpret X ST

as the intersection of all the instances of T where X is interpreted as a subPER of

V

the interpretation of S This intuition also serves for intersection typ es T T

n

is interpreted as the intersection of the PERs interpreting each of the T s We

i

generalize this mo del to order p olymorphism and subtyping by interpreting

typ e op erators as functions over PERs

To deal correctly with intersection typ es we need to make one signicant

technical departure here from PER mo dels of ordinary b ounded quantication

instead of allowing the elements of our PERs to be drawn from the carrier of an

arbitrary partial combinatory algebra D we require that D be a total combinatory

algebra This restriction is needed to validate instances of SConv which have

the form S For example let S The emptyintersection

is interpreted by the everywheredened PER ie relates every m to itself

To validate the distributivity law it must therefore be the case that

relates every element to itself But this will only b e true if the application of any

elementtoany other elementisdened This observation is due to QingMing Ma

Cardelli and Longo CL and Bruce and Mitchell BM have given related

without intersections mo dels for variants of F including subtyping but

The notation and fundamental denitions used here are based on pap ers of

CHAPTER A PER MODEL FOR F

Bruce and Longo BL Freyd Mulry Rosolini and Scott FMRS and others

A helpful basic reference for PER mo dels of secondorder calculi is Mitb also

see BMM for more general discussion of secondorder mo dels and Bar HS

for general discussion of combinatory mo dels

Total combinatory algebras



A total combinatory algebra is a D hD k si comprising a set D of



elements an application function with typ e D D D and distinguished

elements k s D such that for all d d d D

 

 

d d k d



     

d d d d d d s d

    

Throughout this section wework with a xed but unsp ecied total combinatory

algebra D Cf Sco for examples

The set of pure terms is dened by the following grammar

M x j xM j M M



The set of combinator terms is

C x j C C j K j S



The bracket abstraction of a combinator term C with resp ect to a variable x

written fun x C is dened as follows

fun x C KC when x FVC

fun x x SK K

fun x C C S fun x C fun x C when x FVC C

  

The combinator translation of a pure term M written jM j is dened as follows

jxj x

jM j jxM j fun x

jM M j jM jjM j

 

A term environment is a nite function from term variables to elements of

D When x dom we write xd for the environment that maps x to d and

agrees with everywhere else We write nx for the environment like except

that xisundened nis lik e but undened on all the variables in dom

We say that extends when dom dom and and agree on dom

Let C be a combinator term and a term environment such that FVC

dom Then the interpretation of C under written C is dened as follows

x x



C C C C

 

k K

S s

HIGHERORDER PARTIAL EQUIVALENCE RELATIONS

Lemma

If extends and FVC dom then C C





fun x C m C

xm

Proof Standard

Higherorder partial equivalence relations

A partial equivalence relation PER on a combinatory algebra D is a symmetric

m fAg n when A relates m and n The and transitive relation A on D We write

domain of A written domA is the set fn j n fAg ng Note that m fAg n

implies m domA We write PER for the class of all PERs

If A and B are relations then A B is the relation where m fA B g n i

 

for all p q D p fAg q m p fB g n q It is not hard to show that A B is a PER

A and B are PERs and that the intersection of any set of PERs is a PER when

To interpret typ e op erators we need to consider not only PERs but arbitrary

function spaces built on PER An element of such a function space including as

a sp ecial case an element of PER itself is called a higherorder PER HOPER

The interpretation of a kind K is a suitable space of HOPERs

PER and

K K K K

 

We generalize the familiar graphinclusion of relations to HOPERs as follows

A B i A B and

m fAg n implies m fB g n for all m n D

K K



B i A B K K and A



K

BP for all P K AP

T

K

Let fA K g be a set of HOPERs indexed by a set I Then A is

i iI i

iI

the HOPER dened by

T

A g n i for every i m fA g n m f

i i

I i

T T

K K K



A P K A P

i i

iI iI

Lemma

K

Each is transitive

T

K

A K If A K for each j I then

i j

iI

T

K K K

B If A B for each j then A

i j

iI

CHAPTER A PER MODEL FOR F

T

K K

A A for each j

i j

iI

T T

AB A B

i i

iI iI

T T T T

B B

K K

i i

iI P A P A iI

T T

K K K K K

 

P K B P P K B P if each B K K

i i i 

iI iI

T T

K K K K



B A B A where A K

i i

iI iI

Indeed in cases through the inclusions are equalities

Proof Straightforward

the ordering Each collection K of HOPERs has a maximal element under

K K

This element is written and can b e calculated as follows is the total

K K K



relation on D and P K

Fact Let A K Then

K K

A

K K K

A implies A

HOPER interpretation of F

An environment is a nite function from typ e variables to HOPERs and from

term variables to elements of D The notations for environment extension re

striction and agreement are carried over from term environments By an abuse

of notation typ e environments are used in place of term environments from now

on

The erasure of an F term e written erase e is the pure term dened as

follows

erase x x

erase xTe xerase e

erase e e erase e erase e

 

erase X T Ke erase e

erase eT erase e

erase forX T T e erase e

n

Let be a term environment and e an expression such that FVe dom

Then the interpretation of e under written e is jeraseej

HOPER INTERPRETATION OF F

If is an environment and T a typ e expression such that FTVT dom

then the interpretation of T under written T is the HOPER dened as

follows

X X

T T T T

 

T

T X T K T

K

 



T P

X P



V T

K K

T T T

n i

in

ST S T

X KT P K T

X P

Wesay that an environment satises acontext written j if dom

dom and

or

x T where nx satises and either T or x domT

nx nx

or

K

or X X T K where nX satises and either T

nX

T

nX

Iterating the denition immediately yields that either T or T

n nX n nX

K whenever j X T K Also note that if extends and FTVT



dom then either T and T or else b oth are dened and T T

 

Lemma Let T be a typ e a context and an environment such that

FTVT dom and jIf T K then T and T K

Proof We need to check the desired result together with an additional fact

T is dened and T K If T K then

If X T K ok then T and T K



n nX n nX

The two are proved by simultaneous induction on derivations We give only the

interesting cases the rest followby straightforward use of the induction hyp othesis

and simple prop erties of HOPERs

KTVar We are giv en that X T K and that ok By part



of the induction hyp othesis T By the denition of

n nX

K

satisfaction n X T which implies in particular



n nX

that X K By the denition of interpretation X

K X

CHAPTER A PER MODEL FOR F

KAll We are given that K and T X T K T and that



X T K T By lemma there exists a shorter



derivation of X T K ok and by part of the induc

tion hyp othesis we have that T and T K Now

K



supp ose P T Then the denition of satisfaction yields

X P j X T K By part of the induction hyp othesis

T and T Since PER is closed under

 

X P X P

T

intersections T T

K





P T

X P



KOAbs Similar

and CVar We are given that xS where X T K



that S By lemma ok By the denition of

satisfaction nx j By the induction hyp othesis the result

follows

CTVar The case where is similar to the previous case When



we are given that X T K and that T K



By the denition of satisfaction nX j By the induction

hyp othesis the result follows

In order to prove the soundness of subtyping we need some technical results

ab out substitution and conversion

Lemma

be an environment with X dom and such that FTV S X T Let

dom and S Then S X T S

X T X T

Proof By induction on the structure of S

T Lemma Let b e an environmentsuchthatFTVS dom If S

and S then S T

Proof By induction on the denition of conversion it is easy to see that it

suces to show the statement for a onestep reduction S T This is proved

by induction on the structure of S The only interesting cases are when S is a

redex and T its reduct Let S X KT T and T T X T Then

 

X KT T P K T T by denition of

 

X P

T



X T



T X T by lemma



For the other redexes the result follows from lemma given that for items

through the inclusions are equalities

Our main semantic results are the soundness of subtyping and typing

Theorem Soundness of subtyping If S T and S K and if

K

jthen S T

HOPER INTERPRETATION OF F

Proof The pro of pro ceeds by induction on the structure of a derivation of

S T For the sake of readabilitywe often make implicit use of the fact that

if S T and S K then by prop osition and lemmas and

S and T are dened and b elong to K whenever j

lemma S T SConv We are given that S T Then by

K

STrans By the induction hyp othesis and the transitivityof lemma

K

STVar We are given X T K ok Now X X T



nX

b ecause j and as extends nX T T

nX

SArrow We are given T S and S T with S S

  

By the uniqueness of kinds lemma K Now by the well

kindedness of subtyping prop osition and syntax directedness

of kinds prop osition we have S T By the induc



tion hyp othesis S T and T S Hence by the

 

covariance on the right and contravariance on the left of the func

tion space constructor on PERs easily veried from its denition

S S T T ie S S T T

   

SAll We are given that

K S T and X U

 

X U K S



By prop osition X U K S Let P U Then



by the denition of satisfaction X P j X U K Now by

the induction hyp othesis it follows that S T

 

X P X P

T T

S T Consequently Hence

K K

 

 

P U P U

X P X P

X U K S X U K T

 

K



X K S T and X K S K By the SOAbs We are given

K



K syntaxdirectedness of kinding K K K and X



K



S K for some K If P K then X P j X K

 

K

Now by the induction hyp othesis S T Then

X P X P

K K



P K S P K T Consequently

X P X P

K K



X K T X K S

syntaxdirectedness of kinding SOApp By induction hyp othesis using the

SMeetG By the induction hyp othesis and lemma

SMeetLB By lemma

The type context T V obtained from a context is dened in the obvious

way

T V

x T T V T V

X T K T V T V X T K

CHAPTER A PER MODEL FOR F

Theorem Let jand j such that T V T V and for all

 

x dom x fx g x where T V Then e fT g e





Proof From e T it follows by the wellkindedness of typing prop osition

that T Note that FTVT domT V then by strengthen

ing lemma T V T Note also that j T V by lemma

T is dened and in We often use this fact implicitly in the following The

pro of now pro ceeds by induction on a derivation of e T

x TVar From the assumption that for every x in dom x fx g



TAbs We are given that x T e T Supp ose that p D and q D



are such that p f T g q Then xp j xT and xq j



xT By the induction hyp othesis e fT g e that



xp xq



is jeraseej f T g jeraseej From lemma



xp xq



it follows that fun x jerase ej p f T g fun x jerase ej q





that is x T e p f T g xT e q Since p and q were chosen





freelywehave thatx T e fT T g xT e in other words





x T e fT T g xT e





TApp By the induction hyp othesis using the fact that fa f a

K



TTAbs We are given that X T K e T Supp ose that P T



Then it follo ws that X P j X T K and that X P j



e we have by the induction hyp o X T K Since e

X P

 

Since P was chosen freely we have thesis that e fT g e



X P



T

T g e Now the result follows from the that e f

K





T P

X P





denition and the fact that X T K e e e

X P

TTApp We are given that f X T K T and that S T By



which means fX T K g f the induction hyp othesis f



T

By the wellkindedness of typ that f f T g f

K





P

X P



ing syntax directedness of kinds and wellkindedness of subtyping

K



T So we have S K and by soundness of subtyping S

which by lemma and the fact that fT g f f



X S



f fS for any is fS fT X S g fS





TFor Immediate from the induction hyp othesis because erase eX S

erase e

TMeet We are given that ok and also e T for each i in f ng The

i

T

denitions of and result follows by the induction hyp othesis and the

TSub We are given that e S and that S T By the induction

By wellkindedness of typing S fS g e hyp othesis e



and by the soundness of subtyping S T Hence e f T g



e

HOPER INTERPRETATION OF F

Corollary Soundness of typing Let b e an environment such that j

Then e T implies e domT

Proof Take



CHAPTER A PER MODEL FOR F

Chapter

Multiple Inheritance

Intro duction

This chapter is extracted from CP The reader is invited to read Bru FM

PT for a complete account on the foundations of ob jectoriented programming

Here we intend to illustrate how the concept of multiple inheritance is captured

by intersection typ es We use records and record typ es which are not part of the

syntax of F The reader is referred to Car for an implementation of records

in a typ ed lamb da calculus

Informally in class based ob jectoriented programming there are entities called

ob jects which are organised in classes and each class of ob jects is asso ciated with

a set of functions or metho ds This set of metho ds is known as the interface of

ob jects of a class The use of a metho d of the interface to access an ob ject is the

called message passing A suitable typ e theory for an ob jectoriented programming

discipline should prevent access to ob jects other than through the corresp onding

interface This protection against illegal access is known as encapsulation

Existing classes may b e used to create new ones In this way classes are organ

ised in a hierarchy or genealogy where ancestors are sup erclasses and descendants

are sub classes The mechanism through which a sub class of ob jects use metho ds

of a sup erclass is known as inheritance So far we used the word class as a syn

onym of collection In the sequel the word class is used formally to refer to a term

of the F language

The common goal of studies in this area is to prove the safetyofatyp e system

describing a set of highlevel syntactic constructs for ob ject encapsulation mes

sage passing and inheritance Our approach consists of translating the highlevel

syntax into a more conventional calculus whose own typ esafety is established

separately the soundness of the typing rules for the ob ject features then follows

from the soundness of the target system So for example the keyword newwhich

by itself do es not represent any entity in an ob jectoriented programming lan

guage is interpreted as a term which given suitable arguments creates an ob ject

can verify typ e safety automatically using One advantage of this style is that we

a typ e checker for the underlying calculus

The ob ject mo del of Pierce and Turner PT on which the present study

CHAPTER MULTIPLE INHERITANCE

is based enco des ob jects as expressions of F an extension of Girards System

F Gir with b ounded quantication Given a description M of a public inter

face the names and typ es of a set of metho ds the typ e ObjectM denotes

the typ e of ob jects satisfying this description Technically ob ject interfaces are

typ e op erators of kind maps from typ es to typ es and Object

is a higherorder constructor See sections and for details and PT for a

longer discussion For example if PointM describ es the interface of onedimensional

point ob jects resp onding to the messages setX and getX then ObjectPointM is

the typ e of p oints Asso ciated with each such collection of ob jects is a group of

functions for sending messages with typ es like

PointsetX AllM PointM

ObjectM Int ObjectM

The b ounded quantier AllMPointM expresses the fact that the message setX

can actually be sent to any ob ject whose interface renes the interface of p oints

Given such an ob ject and an integer representing its new xco ordinate PointsetX

returns a new ob ject with an appropriately up dated p osition The foregoing ac

counts for the fundamental features of ob ject encapsulation and interface rene

ment and correctly handles their interaction this is the dicult part But it

omits some characteristic features of popular ob jectoriented languages notably

inheritance

In general terms inheritance is a mechanism allowing the implementations of

dierent sorts of ob jects that share some of their behavior to be factored so that

the common b ehavior is written just once

In our framework a class is a term containing the typ e of the internal rep

ob jects a default value for the private data and an resentation of ob jects its

implementation of the metho ds Therefore a class can be used in two ways as

a for creating new ob jects b ecause it has a default value and the set of

metho ds which is all that is needed to create an ob ject and as the basis for de

ning sub classes by incremental extension of the set of metho ds If M is an ob ject

interface then ClassM is the typ e of classes that can be used to create ob jects

of typ e ObjectM The p olymorphic function new maps a class into a new ob ject

new AllM ClassM ObjectM

Another function extend takes an existing class and a description of some new

metho ds and constructs a new class combining the old and new behaviors

extend AllSuperM

AllSelfMSuperM

ClassSuperM ClassSelfM

The details hidden here by are revealed in the following section and sec

tion The b ounded quantier AllSelfMSuperM ensures that the inter

face of the new class renes that of the old ObjectSelfM is a subtyp e of

ObjectSuperM

AN EXAMPLE OF MULTIPLE INHERITANCE

To handle multiple inheritance in this setting wemust enrichthe extend func

tion to take two or more sup erclasses as arguments Consider the case where the

new class inherits metho ds from two sup erclasses This version callitextend

there will b e an analogous one for each n should have atyp e like

extend AllSuperM the interface of one superclass

AllSuperM the interface of the other superclass

AllSelfM the interface of the class being built

ClassSuperM the first superclass

ClassSuperM the second superclass

build the new class how to

ClassSelfM the new class itself

But the upp er bound of SelfM presents a problem we must ensure that SelfM is

a subtyp e of both SuperM and SuperMwhich falls outside the expressivescopeof

our target calculus F

Intuitivelywhat we want to write is

extend AllSuperM AllSuperM

AllSelfM SuperM and SuperM

ClassSuperM

ClassSuperM

ClassSelfM

where informally and forms the conjunction of the two sup erclass sp ecications

Fortunately a typ e constructor with exactly this meaning has already app eared

in the literature Firstorder typ e systems with intersection typ es have b een in

vestigated by the group in Torino CDC BCD and elsewhere See CC

for background and further references A secondorder calculus with intersec

tion typ es was studied by Pierce Pie The calculus needed here is the order

extension of this system

A typ e system combining intersection typ es with a powerful form of poly

morphism is of indep endent interest Reynolds Rey has argued that inter

section typ es can form the basis of elegant language designs But his Forsythe

language has only a rstorder typ e system and thus lacks some of the expressive

p ossibilities of p olymorphic languages like ML Our work represents a step toward

a synthesis of these styles of language design

The following section shows some examples of multiple inheritance using a

simple highlevel syntax and section develops an implementation of inheritance

in this setting

An example of multiple inheritance

We b egin by recalling the enco dings of some basic concepts of ob jectoriented

programming in F and showing a simple example of multiple inheritance in this

setting

CHAPTER MULTIPLE INHERITANCE

In this setting an ob ject interface specication is mo delled as a function from

typ es to typ es describing the b ehaviors of a collection of metho ds as transforma

tions on the ob jects internal state For example the interface of onedimensional

point ob jects supp orting the messages getX setX and bump is captured by the

typ e op erator

PointM FunRep setX RepIntRep

getX RepInt

bump RepRep

PointM

which expresses the fact that the getX metho d of a p ointinterrogates its internal

state and returns an integer that the set metho d transforms the internal state

and a new p osition into an up dated internal state and that bump which increases

maps one internal state to another The in the lefthand the p osition by one

margin indicates that this expression has b een checked by our implementation the

typ echeckers resp onse follows The abstraction over the typ e Rep of the internal

state hides the actual internal state from outside view Concretely a p ointwhose

internal state typ e is xInt a oneeld record containing an integer will

contain a record of metho ds with typ es

setX xInt Int xInt

getX xInt Int

bump xInt xInt

while a point whose internal state typ e is richer say xIntyInt will have

corresp ondingly richer concrete typ es for its metho ds

setX xIntyInt Int xIntyInt

getX xIntyInt Int

bump xIntyInt xIntyInt

Externally we exp ect the dierence between these two to be invisible thus the

public interface to the metho ds PointM abstracts away from any particular rep

resentation typ e Both point ob jects are elements of the typ e ObjectPointM

For present purp oses it is not imp ortanthow Object itself is dened Cf PT

HP

New ob jects are created by applying the p olymorphic function new to a class

Given an interface M and a class for this interface that is a class whose instances

new creates and returns suchanobject New classes are ob jects with interface M

in turn are created by applying the p olymorphic function extend to an existing

class along with a sp ecication of an incremental change to its b ehavior

extend val

AllSuperM

AllSelfMSuperM

AllSelfDiffR

Class SuperM

SelfDiffR

AN EXAMPLE OF MULTIPLE INHERITANCE

AllFinalR

Extractor FinalR SelfDiffR

SuperM FinalR

SelfM FinalR

SelfM FinalR

Class SelfM

In detail the arguments exp ected by extend comprise

The interface SuperM of the existing class

The interface SelfM of the new class that will b e returned by extend

The typ e SelfDiffR which describ es the dierence between the representa

tion of the sup erclass whatever it may b e and the representation of the new

class In conventional terminology this is the set of new instance variables

intro duced by the sub class

er prints The sup erclass itself an elementof ClassSuperM our typ echeck

it as Class SuperM

An initial value an elementofSelfDiffR for the new part of the state

A p olymorphic metho d builder function

Given all these extend returns a class for the interface SelfM

The metho d builder function which do es the work of constructing the vec

tor of metho ds to be used in instances of the new class must itself take several

parameters

The nal representation typ e FinalRwhich is xed at the moment when

new is applied to a class

An extractor whichprovides a mapping back and forth b etween the nal

representation typ e and the lo cal representation typ e allowing the lo cal

metho ds to access the part of the state that interests them

The sup er metho ds of the existing class

The self metho ds of the new class which are used to mo del the charac

teristic ob jectoriented feature of sending a message to self

Given these the metho d builder must return a collection of metho ds for the new

ob ject

For uniformity let us assume that there is just one base class the class of

things whose instances are ob jects with no b ehavior at all

ThingM FunRep

ThingM

thingClass val Class ThingM

CHAPTER MULTIPLE INHERITANCE

To build a class of p oints extending thingClass we rst cho ose the lo cal part

of the representation of points

PointDiffR xInt

PointDiffR

Nowwe create pointClass by applying extend as follows see section for more

details

pointClass

extend

ThingM superclass interface

PointM interface for new class

PointDiffR local state type

thingClass the superclass itself

x initial value for local state

funFinalR method builder function

FinalR PointDiffR mapping a state extractor fune Extractor

funsuper ThingM FinalR and the super methods

funself PointM FinalR and the self methods

getX funsFinalR to a getX method

eget sx that returns

the local x field

setX funsFinalR and a setX method

funiInt eput s xi that overwrites

the x field

bump funsFinalR and a bump method

selfsetX s that calls setX on self

plus selfgetX s to set x to one more

than selfgetX

pointClass val Class PointM

Of course this denition of pointClass is quite verb ose It is not hard to design

higherlevel syntax for ob jects message passing and class extension that lo oks

like ordinary ob jectoriented source co de but since we are building a foundational

mo del here we prefer the lowlevel notation

Similarlywe can dene the interface for colored ob jects ob jects supp ort

ing the messages setC and getC asfollows

ColoredM FunRep setC RepColorRep getC RepColor

ColoredM

Again one instance variable suces to represent the color of a colored ob ject

ColoredDiffR cColor

ColoredDiffR

y extending thingClass as we did A class of colored ob jects can now be created b

to build pointClass

AN EXAMPLE OF MULTIPLE INHERITANCE

coloredClass

extend ThingM ColoredM ColoredDiffR thingClass

c black

funFinalR

fune Extractor FinalR ColoredDiffR

funsuper ThingM FinalR

funself ColoredM FinalR

getC funsFinalR eget sc

setC funsFinalR funnewcColor eput s cnewc

coloredClass val Class ColoredM

Nowwehavereached the p oint where we can use multiple inheritance to com

bine the classes of p oint ob jects and colored ob jects yielding a new class of colored

points The interface of colored points contains all the messages of b oth sup er

classes

CPointM FunRep setX RepIntRep

getX RepInt

bump RepRep

setC RepColorRep

getC RepColor

CPointM

For this simple implementation no additional instance variables are needed we

can set CPointDiffR

To make the example more interesting we take the metho ds getX setC and

getC unchanged from the sup erclasses while overriding the denition of setX so

that in addition to setting the x co ordinate as usual it also sets the color to say

blue

cpointClass

extend PointM ColoredM CPointM

CPointDiffR pointClass coloredClass

funFinalR

fune Extractor FinalR CPointDiffR

funsuper PointM FinalR

funsuper ColoredM FinalR

funself CPointM FinalR

setX funsFinalR funiInt the new setX method

let s supersetX s i in use pointClasss setX

to set position

let s supersetC s blue and coloredClasss setC

to set color

in s end end

getX supergetX copy all the remaining

bump superbump methods from the

setC supersetC appropriate superclass

getC supergetC

CHAPTER MULTIPLE INHERITANCE

cpointClass val Class CPointM

Here the low level at which we are working is reected in the fact that the old

metho ds getX bump setCand getC must b e copied explicitly from the sup erclasses

to the new class Intro ducing highlevel syntax for multiple inheritance would of

course raise all the usual questions must each inherited metho d app ear in only

one of the sup erclasses if it app ears in more than one which should be copied

to the sub class etc for which the usual solutions will apply

To test what we have done lets build a colored point and send it some mes

sages

p new CPointM cpointClass

p val Object CPointM

ColoredgetC CPointM p

black Color

p Pointbump CPointM p

p val Object CPointM

PointgetX CPointM p

Int

ColoredgetC CPointM p

blue Color

Note that sending our colored point the bump metho d has the eect of changing

its color to blue the overridden behavior of the setX metho d is observable in the

in the sub class behavior of bump metho d even though bump was not redened

Enco ding multiple inheritance

We close with a full implementation of the extend function generalising the

extend function in section of PT As we suggested in the intro duction

an intersection typ e must be used at one point marked in the denition of

extend to obtain asound typing the rest is straightforward

This implementation of classes and inheritance makes the lo cal state of each

class inaccessible b oth to clients of ob jects and to metho ds dened in sub classes

Other variations are p ossible w e chose this one to simplify the presentation of

section

If M is an ob ject interface an op erator of kind then ClassM is the set

of classes whose instances havetyp e ObjectMEachsuch class consists of a lo cal

representation typ e MyR whose identityishiddenby an existential quantier an

element initstateMyR that is used as the initial value of the state in new ob jects

created from this class and a function buildM that can be used to construct the

metho ds of the new ob jects

Class

FunM

SomeR

ENCODING MULTIPLE INHERITANCE

initstate R

buildM ClassMethods M R

Class

To cop e with dierent representations of lo cal state in sub classes the metho d

building function is abstracted on two parameters a typ e FinalR representing

the full state of an eventual sub class and an extractor giving access to the

comp onents of interest to the metho ds b eing built The metho d builder is also

abstracted on a collection of selfmetho ds of the same typ es as its own metho ds

Given these it yields a concrete collection of metho ds sp ecialized to work prop erly

in an ob ject with representation typ e FinalR

ClassMethods

FunMyM

FunMyR

AllFinalR

Extractor FinalR MyR

MyMFinalR

MyMFinalR

ClassMethods

Finally an extractor is just a pair of maps get and put

Extractor FunSS FunTT get SSTT put SSTTSS

Extractor

Intuitively get extracts the sup erclass part of an element of a sub classs state

while put overwrites the sup erclass part yielding a new sub class state

For example the p oint class of section can b e dened directly rather than

as an extension of thingClassas follows

pointClass

xInt

initstate x

buildM funFinalR

fune Extractor FinalR xInt

funself PointM FinalR

getX funsFinalR eget sx

setX funsFinalR funiInt eput s xi

bump funsFinalR eput s x plus eget sx

Class PointM

pointClass val Class PointM

We use the ascii syntax RbTforintro ducing elements of existential typ es R

is the hidden witness typ e b is the b o dyandT is the existential typ e where the res

ultistolive The corresp onding elimination form is written open e as Rx in

b

A class with two sup erclasses generates ob jects whose internal states have

three parts one for each sup erclass and one for the new comp onents lo cal to

CHAPTER MULTIPLE INHERITANCE

the class itself For example an instance of cpointClass contains a point state

of typ e xInt a coloredob ject state of typ e cColor and an empty

lo cal state The extend function takes two classes an initial lo cal state and a

function for incrementally building a collection of new metho ds from the old ones

and constructs a sub class of this form

extend Arguments

funSuperM first superclass interface

funSuperM second superclass interface

funMyM SuperMSuperM new subclass interface

funMyLocalR local state type

funsuperClass Class SuperM first superclass

funsuperClass Class SuperM second superclass

funmyinitstate MyLocalR initial local state

funmymethods incr method extension fun

with arguments

AllFinalR a final rep type

Extractor FinalR MyLocalR an extractor

for the local state

SuperMFinalR the first superclasss

methods

SuperMFinalR the second superclasss

methods

MyMFinalR the selfmethods

MyMFinalR returning the new methods

open superClass

as SuperRsuperData in open the first superclass

open superClass

as SuperRsuperData in open the second superclass

let MyR define the new state type

Triple SuperR SuperR MyLocalR in a triple

Result a new class

MyR with state type MyR

initstate and initial state

triple SuperR SuperR MyLocalR a triple of

superDatainitstate first supers

initial state

superDatainitstate second supers

initial state

myinitstate local initial state

buildM and methodbuilder

a fun with args

funFinalR a final rep type

fune Extractor FinalR MyR an extractor

funself MyMFinalR and a collection

let eself of selfmethods

composeExtractors

FinalR MyR MyLocalR e

ENCODING MULTIPLE INHERITANCE

extractof SuperR SuperR MyLocalR in

let esuper

composeExtractors

FinalR MyR SuperR e

extractof SuperR SuperR MyLocalR in

let esuper

composeExtractors

FinalR MyR SuperR e

extractof SuperR SuperR MyLocalR in

mymethods FinalR eself returning

methods built by mymethods

superDatabuildM FinalR esuper selfwhen applied to

concrete methods

superDatabuildM FinalR esuper selfof the superclasses

self and the selfmethods

end end end

Class MyM

end end end

extend val

AllSuperM

AllSuperM

AllMyMSuperMSuperM

AllMyLocalR

Class SuperM

Class SuperM

MyLocalR

AllFinalR

Extractor FinalR MyLocalR

SuperM FinalR

SuperM FinalR

MyM FinalR

MyM FinalR

Class MyM

This denition uses a utility function for comp osing extractors in the obvious way

composeExtractors

funT funT funT

fune Extractor T T

fune Extractor T T

get funtT eget eget t

put funtT funtT

eput t eput eget t t

composeExtractors val

AllT

AllT

AllT

Extractor T T

Extractor T T

CHAPTER MULTIPLE INHERITANCE

getTT putTTT

For forming triples we use the typ e abbreviation

Triple FunT FunT FunT fstT sndT thdT

Triple

with the constructor

triple

funT funT funT

funtT funtT funtT

fstt sndt thdt

triple val

AllT

AllT

AllT

T T T fstT sndT thdT

and the pro jections

extractof

funT funT funT

get funp Triple T T T pfst

put funp Triple T T T

funtT

fstt sndpsnd thdpthd

val extractof

AllT

AllT

AllT

get Triple T T TT

put Triple T T TTfstT sndT thdT

and extractof and extractof which are dened similarly

Aslightly dierentformulation of the extend function provides an alternative

p ersp ective on its b ehavior The original extend is parametric on three class

interfaces SuperM SuperM and MyM where MyM is constrained to rene b oth

SuperM and SuperM The typ e of the following function extend emphasizes

the fact that MyM is typically formed by adding some new metho ds to those given

by SuperM and SuperM it is parameterized on SuperM SuperM and a partial

interface MyOwnM which is conjoined with the other two to form MyM

extend

funSuperM first superclass interface

funSuperM second superclass interface

funMyOwnM new methods specification

let MyM SuperMSuperMMyOwnM in new class interface

the rest as before

extend val

ENCODING MULTIPLE INHERITANCE

AllSuperM

AllSuperM

AllMyOwnM

AllMyLocalR

Class SuperM

Class SuperM

MyLocalR

AllFinalR

Extractor FinalR MyLocalR

SuperM FinalR

SuperM FinalR

SuperMSuperMMyOwnM FinalR

SuperMSuperMMyOwnM FinalR

Class SuperMSuperMMyOwnM

Note that all of the quantiers in this version are unb ounded b ounded quantic

ation has b een replaced by unb ounded quantication and intersection

CHAPTER MULTIPLE INHERITANCE

Part II

FirstOrder Subtyping

Chapter

Implicit and Explicit Subtyping

Intro duction

In the analysis of calculi we can distinguish b etween two main groups of systems

namely explicitly typ ed systems usually called a la Church and implicitly typ ed

systems also called a la Curry In the implicitly typ ed systems typ e free lambda

terms are assigned a typ e and this is why these calculi a la Curry are sometimes

called systems of typ e assignment On the other hand in the explicitly typ ed

systems the terms are not terms of the typ efree calculus but terms themselves

o illustrate the dierence we write the canonical containing typ e information T

example of typing the corresp onding identity term in b oth styles

xx

Curry

x x

C hur ch

Observe that the Church style term has extra typing information namely

This explicit mention of typ es in a term makes it easier to decide whether a term

has a certain typ e For some systems a la Curry this question is undecidable See

Bar for some examples In these systems the problem of nding a typ e for

a given term involves solving sets of equations See Wan for an elegant and

concise algorithm of typ e inference for simply typ ed calculus a la Curry

The idea of subtyp e app ears quite naturally in programming languages If we

think of typ es as sets we can easily picture what a subtyp e could be Informally

we can say that a typ e is a subtyp e of if any element of can be

seen as an element of We say can be seen as and not directly is b ecause the

t of typ e might hide some act of considering an element of typ e as an elemen

transformation Consider for example the typ es Int and Real of integers and real

numb ers resp ectively Usually on a computer integer numb ers are represented in

a dierent way than real numb ers are even if we might think of the integers as a

subset of the real numb ers there is a translation going on The act of considering

an element of typ e as an element of typ e will be called coercion In other

words we say that an element of typ e is co erced into an element of typ e

Somehow an element of typ e has enough information to be seen as an element

of typ e

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

While dealing with co ercions we can again distinguish between an explicit

style and an implicit style A style with explicit co ercions means that co ercions

are explicitly indicated and in an implicit style as the name suggests co ercions

are left implicit In systems including subtyping there is usually a rule for typing

co erced terms Then in an explicit style the co ercion rule mightlookas follows

M

Coercion

c M

Similarly in an implicit style the corresp onding rule is as follows

M

Subsumption

M

From the previous discussion it follows that we can split subtyping systems into

four main groups combining implicit or explicit typing with implicit or explicit

co ercions Explicit co ercions have been used as a way of giving semantics to

systems with implicit co ercions in CG In CL PER mo dels for Quest

a higher order lambda calculus with subsumption and Quest a higher order

C

lamb da calculus with co ercion are studied

An implicit co ercion is motivated by the fact that the same term can be con

without p erforming any change in the sidered as b elonging to two dierent typ es

term as for example is the case when one of the typ es is included in the other

with the intuitive idea of set inclusion while an explicit co ercion wishes to state

explicitly that there is a transformation going on We can think for example of

a function f with the real numb ers as domain and a subset A of real numb ers

If x is a variable of typ e A then we would like to use f on x as well without

p erforming any extra calculation to apply f to x

The system lambda sub an extension of the simply typ ed calculus

a la Church with subtyping is presented in section The extension consists of

adding the previously mentioned Subsumption rule in other words co ercions are

left implicit The subtyping relation mentioned in the rule is based on a nite set of

subtyping axioms closed under reexivity and transitivity and extended to arrow

typ es in the standard way Weshowthat satises the minimal typ es prop erty

and we exhibit an algorithm to compute minimal typ es Alg Moreover we

show that typ e checking and typ e inference are decidable

The subtyping relation is studied in section where a metho d to establish

o typ es are in the subtyp e relation is given and proven sound and whether tw

complete with resp ect to the denition of the subtyping relation The decidability

of the predicate was already stated in Mit Typ es in the subtyp e relation

are lo oked at through the magnifying glass to establish the relation between the

structure of and when is less than or equal to

In section lambda co erce another extension of the simply typ ed

C

lambda calculus with subtyping is intro duced This time the rule added is the

previously mentioned Coercion rule Basic prop erties of this system are estab

lished and in section we show that the invisible co ercions in a typing

statement can b e uniformly reconstructed pro ducing a legal statementof The

C

fact that we translate typing statements instead of typing derivations as in CG

THE SUBTYPING RELATION

and BCGS avoids coherence problems

Finally in section a translation of into the simply typ ed lambda cal

C

culus is develop ed This means that a system with two dierent kinds of

judgements typing judgements and subtyping judgements is translated into a

system without subtyping The idea is to mimic inside The translation of

C

the typing system is straightforward the Coercion rule is omitted The transla

tion of the subtyping statements is as follows the subtyping axioms are collected

called environment like a signature in ELF AH and the subtyping as a so

rules are p erfectly captured by computational prop erties of the calculus A pro of

of a subtyping statement is then a term containing constants of the environ

ment This translation together with the translation from into imply that

C

subtyping can be co ded into a system without subtyping

The that we dene in section is not exactly the one presented in

Bar We prefer a formulation in which constants are syntactically dierent

from variables the rules prevent abstraction ov er constants and there is a typing

rule for constants so that nothing that is ilegal can be derived from the rules

without extra proviso in the metalanguage

Convention Throughout this chapter the metavariables and will

range over typ e variables and will range over typ es M N and P will range

over terms x y and z over term variables will range o ver contexts and over

environments

The subtyping relation

The relation 

C

In the present section we dene the subtyping relation  and an algorithm

C

to check whether two typ es are in the subtyping relation In prop osi Subtype

tion we prove the correctness of the algorithm Subtype with resp ect to the

denition of 

C

Let V be a set of typ e variables T aset of typ es dened by

T V j T T

and let C V V a nite set of subtyping axioms where if C then

are dierent variables

particular case when C V V given We will restrict our attention to the

that the more general case when C T T could allow typing nonterminating

terms like for example x xxx xx

Definition Subtyping The relation  T T is the smallest relation

C

closed under the following rules

C  SIncl

C

 SRefl

C

   STrans

C C C

   SArrow

C C C

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

The SArrow rule deserves aclose lo ok If we consider the relation  as an

C

ordering then is monotonic in the second argument and antimonotonic in the

rst argument Intuitively if every value of typ e can be treated as a value of

typ e then every function which maps to also maps to

In what follows we dene the algorithm Subtype which is a decision pro cedure

for the  relation as it is shown in prop osition But rst we need the

C

following denition

Definition Transitive Closure of C

C TransC

and TransC TransC

transC true if and only if b elongs to TransC

We can now write down the algorithm

VV

Definition Subtype Subtype T T Bool

SubtypeC

if

then true

else if and are variables

then transC

else if and

 

then Subtype C and Subtype C

 

else false

Where is the syntactic equality

Since C is nite TransC isalso nite Consequently transC is decid

the recursive calls have arguments of strictly smaller size Hence able Moreover

the algorithm Subtype always terminates

Definition The Shadow ofatyp e is dened as follows

Shadow if V

Shadow

Shadow Shadow

The dierence between the usual underlying tree structure and the shadow of

a typ e expression is that dierent typ e variables have dierent underlying trees

but the same shadow Then two typ e expressions that only dier in their atomic

sub expressions typ e variables have the same shadow

Lemma Let V and T Then

THE SUBTYPING RELATION

If trans C then 

C

If  then Shadow Shadow

C

If  then trans C or

C

Proof

By induction on the denition of TransC

By induction on the derivation of 

C

By induction on the derivation of 

C

Lemma Let and T Then

 

 if and only if  and 

 C  C  C 

Proof From right to left it is just the SArrow rule From left to right the

pro of follows by induction on the derivation of  Since C only

 C 

contains pairs of typ e variables the SIncl rule could not have b een the last rule

of the derivation

and this means that and Hence by SRefl

   

SRefl it follows that  and 

C  C 

STrans We are in the case that for some  and 

 C C 

By lemma is of the form Then by the induction



hyp othesis    and  Then by

C  C  C  C 

STranswe conclude that  and 

C  C 

SArrow If the last rule was SArrow then the only p ossibility for the hyp othesis

is  and 

C  C 

We can now show that the algorithm Subtype is correct with resp ect to den

ition The correctness is split into two parts usually called soundness and

completeness Soundness means that if Subtyp eCtrue then it is the case

that  Conversely completeness means that if  then the algorithm

C C

outputs true when called with arguments and C

Proposition Soundness and completeness of Subtype

SubtypeCtrue if and only if 

C

Proof

By induction on the complexity of and

Case V Then we have to consider the following two cases

If then by SRefl  Case a C

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

Case b If transC true then by lemma we know

that 

C

Case If either or is a variable and the other one is not then the al

gorithm never yields true

Case Neither nor isavariable Again we have to consider two cases

Then by SRefl 

C

Say and Then it is the case that

 

Subtype Ctrue and Subtype C true

 

By the induction hyp othesis  and  Hence

C  C 

due to the SArrow rule 

 C 

By induction on the complexity of

V Then by lemma is also a variable By lemma

that transC true or and in b oth cases it follows

we have that Subtype C true

Then by lemma is of the form and b ecause

 

of lemma we know that  and  Then

C  C 

Subtype C true and Subtype C true by the

 

induction hyp othesis Hence SubtypeC true

A closer lo ok at the algorithm uncovers some pro of theoretic prop erties of

the subtyping relation  Observe that in the algorithm the STrans rule is

C

considered only at the level of variables in other words the STrans rule is never

used as the last rule of a pro of of a statement of the form 

 C 

there exists The corollary is then that if there exists a pro of of  then

C

also a pro of of  in which the applications of the STrans rule are only on

C

statements of the form  and  where and are typ e variables

C C

This fact can b e read as follows the system in which the STrans rule is replaced

by

STrans    where V

C C C

can prove the same subtyping statements as the original system dened in

From the pro of theoretic point of view there is another p ossible renement

that consists of restricting the application of the SRefl rule to typ e variables In

other words we could replace SRefl by

SRefl  for all V

C

But from an algorithmic p oint of view this is not a very satisfactory choice

b ecause the pro ofs with the SRefl rule can be shorter The use of the SRefl

rule instead of the SRefl rule avoids sup eruous recursive calls For example

to prove  requires two applications of the SArrow rule

C

and three applications of the SRefl rule while it can b e proved in one step with

original SRefl rule the

THE SUBTYPING RELATION

Ab out the sets f T j  g and f T j  g

C C

In this section we fo cus our attention on the sets of typ es which are smaller

and bigger than a given typ e with resp ect to  We dene simultaneously the

C

functions after and before that given a typ e retrieve the set of bigger and smaller

typ es resp ectively as we prove in lemma

T

Definition afterbefore T

before f gf V j trans C true g if V

after f gf V j trans C true g if V

before f T j after and before g

after f T j before and after g

Lemma Let T

before and after

before after

 before

C

nite sets f T j  g and f T j  g are

C C

Proof

Straightforward

By induction on the structure of

By induction on the structure of using prop osition and

By induction on the structure of

By items and we know that

f T j  g before and

C

f T j  g after

C

Since C is nite before and after are nite sets

Ab out the form of typ es in the  relation

C

In order to study typ es in the subtyping relation it is useful fo have a language

which enables us to refer to a sp ecic sub expresion of a given typ e Having in mind

the underlying tree structure of a typ e say we dene the notion of binary code

Each binary co de uniquely determines a subtree of the underlying tree of which

in its turn is linked to a sub expresion of

Definition

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

A binary code is a p ossibly empty sequence of zeros and ones

A positive binary code isabinary co de with an even number of ones

A negative binary code isabinary co de with an o dd number of ones

Definition The sub expression of co de b in the typ e expression nota

tion Subb is dened as follows Sub f g T T

Sub

Subb Subb

Subb Subb

Observe that Sub is a partial function not every binary co de indicates a sub ex

pression of a given typ e For example Sub with V is undened

Notation we will write b insteadofSubb We frequently use code

instead of binary code

Definition

b is called a code in if b is dened

b is called a binary leaf code in if b V

Intuitively a binary code in a typ e is a path starting from the ro ot of the

underlying tree of where left is indicated with and right with Note that

each co de in uniquely determines a sub expression of Then we can say that

a sub expression is p ositive if it has a p ositivecode and negative otherwise Note

that then in the path from the ro ot of the underlying tree of to the ro ot of a

p ositive resp ectively negative sub expression wehavechosen an even resp ectively

odd n umber of times the left branch of an arrow no de

Lemma Shadow Shadow if and only if every leaf co de of is a

leaf co de of

Proof From left to right the result follows by straightforward induction on the

structure of From right to left By induction on the structure of

V is the only leaf co de of Since is also a leaf co de of it

follows that isavariable Hence Shadow Shadow

The leaf co des of are of the form b and b for every leaf co de

 

b of and for every leaf co de b of Since b and b are also

  

leaf co des of is of the form Then b is a leaf co de of



and b of Then by the induction hyp othesis and the denition of

 

Shadow Shadow it follows that Shadow

THE SUBTYPING RELATION

Proposition

If for every p ositive leaf co de b in b  b

C

and for every negative leaf co de b in b  b then 

C C

Proof By induction on the complexity of

V Then the only co de in is the empty co de and as the empty co de

is p ositive we have that  but is and is

C

Then the co des in are of the form b and b where b is a co de

 

in and b is acode in

 

Since b is a leaf co de of b isavariable and since b  b

C

by the correctness of the algorithm Subtype b is also a vari

By lemma it follows that able Hence b is a leaf co de of

Shadow Shadow Then weknowthat is of the form



Our goal is to prove that  For that it is enough

 C 

to show that  and 

C  C 

Let b be a negative leaf co de in By the denition of co de

b b and by assumption since b is a p ositivecode

also b  b and as b b we conclude

C

b  b

C

Let b be a p ositive leaf co de in By denition of co de

b b then as b is a negative co de in b 

C

b and as b b we have that b  b

C

We conclude that  by the induction hyp othesis Similarly

C

it follows that 

 C 

Lemma Supp ose  Then

C

b is a co de in if and only if b is acode in

Proof By induction on the complexity of

V Then by lemma is also a variable Then the only p ossible

co de is the empty co de and is acode in every element of T

Then by lemma is of the form By lemma

 

 and 

C  C 

Let b be a co de in then the following cases have to be con

sidered

in Then by the induction Case b b with b a co de



hyp othesis b is also a co de in but this means that



b isacode in

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

Case b b with b acodein By the induction hyp othesis

b is a co de in Hence b is a co de in

Similar to the pro of of interchanging the roles of and

Proposition Supp ose  Then for every leaf co de b in

C

if b is p ositive then b  b and

C

if b is negative then b  b

C

Proof By induction on the complexity of

V This means that the only co de in is the empty co de Hence b

is and b is The empty co de is p ositive and by hyp othesis

 so there is nothing else to prove

C

Then by lemma is of the form By lemma

 

and  We are in the case that b db with d 

 C  C

and b a co de in or d and b a co de in According to the



p ossible co des in we have to consider the following two cases

b is p ositive

Case d andb p ositive As b is a co de in by the induc



y denition of tion hyp othesis b  b what b

 C 

co de can b e read as b  b

C

Case d and b negative By lemma b is also a co de

in and by the induction hyp othesis b  b

C

what by denition of co de is b  b

C

b is negative

Case d and b negative By the induction hyp othesis

b  b what means that b  b

 C  C

Case d and b p ositive Lemma implies that b is

also a co de in Then by the induction hyp othesis

b  b Finally by the denition of co de it

C

follows that b  b

C

Theorem

in  if and only if for every leaf co de b

C

if b is p ositive then b  b and

C

if b is negative then b  b

C

Proof By prop ositions and

This theorem suggests yet another way to check whether  by only

C

lo oking at the leaves of the underlying trees of and

SIMPLY TYPED CALCULUS

Simply typ ed calculus

We use a slightly dierent version of than the one in Bar the dierence

b eing that our version contains constants as pseudoterms that are syntactically

dierent from variables Constants are assigned a typ e in an environment as in

AH and there is a rule for typing constants

Definition The typ ed calculus is dened as follows

The set of pseudoterms is dened by the follo wing syntax

V j K j V T j

where V is a set of term variables and K is a set of constants suchthatV

and K are disjoint sets

An environment is a set of statements with only distinct constants as sub

jects The symbol is used for environments

The set of typ es T and the concepts of statement typing assumption sub

ject context derivable statement and legal term are as in denition

Definition Typing rules

k if k T



x if x TVar



x M



TAbs

x M



M N

 

TApp

MN



Basic prop erties of

Let us mention the following prop erties of without giving their pro ofs The

interested reader can nd more ab out these results in Bar There the

system presented do es not have constants but the pro ofs of the prop ositions b elow

are straightforward extensions of the pro ofs given in Bar nevertheless the

strong normalization prop erty deserves a more careful examination Let us use

for derivability in the system presented in Bar where we consider K V

as the set of variables Note that if M then M Then the



strong normalization prop erty for the system in which our constants are treated

as free variables implies the corresp onding result for the system with constants

Let FV M denote as usual the set of free variables of M and Dom the

domain of ie the set of sub jects of

Lemma

Free Variable If M then FV M Dom 

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

Weakening for

Let and be contexts such that Then if M then



M



Proposition

Generation for

a If k then k



b If x then x



c If MN then there exists such that M and

 

N



d If x M then there exists such that and



x M



Unicityoftyp es for If M and M then

 

Strong normalization for If M then M is strongly normal



izing

a system with implicit co ercions

We dene the system lamb da sub an extension of the simply typ ed calculus

with subtyping The dierence b etween the simply typ ed calculus and is the

following rule

M 

C

TSubsumption

M

An immediate consequence of the addition of this rule is the loss of the unicity

of typ es prop erty Fortunately the system has instead the minimal typ e prop erty

Namelyif M then there exists such that M and  That

C

prop erty is relevant in the design of typ e checking and typ e inference algorithms

Consider the case of typ e checking Knowing that if a term is typ eable then it

has a minimal typ e we try to identify a fragment of the system in which every

typ eable term is assigned a minimal typ e In our case we dene a subsystem of

Alg which has the unicityoftyp es prop erty and is syntax directed in such

a way that that Alg denes an algorithm to compute minimal typ es in

We give now the formal denition of a simply typ ed calculus with implicit

co ercions

Definition The typ ed calculus with implicit co ercions is dened

as follows

The set of typ es T Typ e is dened by

T V j T T

where V is aset of typ e variables

A SYSTEM WITH IMPLICIT COERCIONS

The set of pseudoterms is dened as follows

V j V T j

where V is a set of term variables

A statement is of the form M M is of typ e with M and T

The term M is called the subject of the statement A typing assumption is

the an expression of the form x The variable x is called the sub ject of

typing assumption

A context is a set of typing assumptions with distinct variables as sub jects

Definition Typing rules

x if x TVar

x M

TAbs

xM

M N

TApp

MN

M 

C

TSubsumption

M

Definition A statement M is derivable from the context we write

M yields M of typ e if M can b e obtained using the

rules TVar TAbs TApp and TSubsumption in denition A term

M is legal if there exist and such that M In other words a legal

term is the sub ject of a derivable statements

The typing rules in denition are not syntax directed In order to describ e

atyp e inference algorithm we need an alternative presen tation of the typing rules

in which the term to b e typ ed uniquely determines the last rule of the derivation

of its typing statement In the next section we dene Alg an algorithmic

presentation of This presentation has the prop erty of nding a minimal typ e

for

Definition A typ e inference algorithm

x if x TVar

Alg

x M

Alg

TAbs

xM

Alg

M N 

Alg Alg C

TApp

MN

Alg

Proposition Generation for Alg

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

If x then x

Alg

If x M then there exists such that x M and

Alg Alg

M MN then there exist and such that If

Alg Alg

N  and

C Alg

Proof Since the system is syntax directed the form of each judgement uniquely

determines the last rule of its derivation

Proposition Unicity of typ es for Alg If M and

Alg

M then

Alg

Proof By induction on the complexity of M using that the system is syntax

directed

Lemma Wellfoundedness of Alg The Alg rules dene a ter

minating algorithm

cannot cause nontermination In rules TApp Proof Since is nite TVar

and TAbs the size of the sub ject in each of the premises is strictly smaller than

the size of the sub ject in the corresp onding conclusion Moreover the relation

 is decidable b ecause the algorithm Subtype which is sound and complete with

C

resp ect to  always terminates

C

Proposition Decidabilityoftyp e inference for Alg For any and M

it is decidable whether there exists such that M

Alg

Proof By the wellfoundedness of Alg

Proposition Decidabilityoftyp e checking for Alg

Given M and it is decidable whether M

Alg

Proof Because of the unicity of typ es prop erty of Alg the decidability of

typ e inference implies the decidabilityoftyp e checking

To prove the strong normalization prop ertyforAlg we use the corresp onding

result for in section We rst provide some denitions that allow us to

relate Alg terms to terms in such a way that reductions are preserved



TAlg T Definition Let T



if V

  

The homomorphic extension to contexts is dened as follows



fg fg

  

g fx fx g

A SYSTEM WITH IMPLICIT COERCIONS

Definition Alg

x x



xM x M

MN M N

The choice of is irrelevant the essential feature is that it is xed

 

Lemma Let T If Shadow Shadow then

 

M Lemma Let M Alg If M then

 Alg

M Consider the case of Proof By induction on the derivation of

Alg

the TApp rule Then the situation is as follows

P N 

Alg Alg C

PN

Alg



where M PN By the induction hyp othesis the denition of and lemmas

and we have that

    

N and P

 

 

PN it follows that Finallyby TApp and the denition of



Lemma Substitution Let M N in Alg Then

M x N M xN

Proof By induction on the complexity of M

Lemma Reduction preservation

Let M N in Alg If M N then M N

Proof The pro of is by straightforward induction on the complexity of M In

particular the case when M is a redex is a consequence of lemma

Proposition Strong normalization for Alg If M then

Alg

M is strongly normalizing

Proof The result follows from lemmas and using a similar argu

ment as in theorem

The system Alg do es not satisfy the sub ject reduction prop erty Consider

the following simple example

Let  then z x xz and x xz z butz z

C Alg Alg

This example illustrates the fact that a step of reduction of a term may reduce

its original typ e The reason being that in order to typ e xxz  is

C

used and such information can only be used by the application rule TApp

The reduction step erases the application Therefore the subtyping inform

ation cannot be used any longer Nevertheless the following monotonic sub ject

reduction prop erty holds The following result is necessary to prove prop osition

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

Lemma If x M N and  then there

Alg Alg C

exists such that M x N and 

Alg C

Proof By induction on the complexity of M

Proposition Monotonic sub ject reduction

then there exists suchthat M If M and M M

Alg Alg

and 

C

Proof By straightforward induction on the complexity of M using lemma

Let us consider the case when M is a redex and M its reduct that is

the only case that demands some work Then the situation is as follows

xM M

Alg 

By generation prop osition we have that

x M M and 

Alg Alg  C

By lemma there exists such that M xM and 

Alg  C

Observe that b ecause of the unicity of typ es prop erty M cannot have typ e

as well

Here weshow that Alg describ es an eective pro cedure to compute minimal

typ es in

Proposition

M then M Soundness If

Alg

Completeness and minimal typing If M then there exists

such that  and M

C Alg

Proof

By induction on the derivation of M

Alg

By induction on the derivation of M We consider the case for

TApp the other cases follow with similar or simpler arguments We are

given that

M M M



M and

M



By the induction hyp othesis there exist and such that



M where  and

Alg C

M where 

   C Alg

By the correctness of the algorithm Subtype we know that

and by lemma we have that  and  By S

C C

Trans  Finally by TApp we have that M M

 C Alg 

A SYSTEM WITH EXPLICIT COERCIONS

C

The last prop osition says that and Alg typ e the same set of terms in

other words the set of legal terms of is equal to that of Alg The dierence

is that they may assign dierent typ es to the same term Note that item says

that Alg typing statements are also typing statements the converse is not

true Consider the following simple example

Let C f g Then x x butx x is not a derivable

Alg

statement

Observe that the typing information present in the terms is essential for the

minimal typ e prop erty Consider the following example in the system presented

in Mit ie a simply typ ed calculus a la Curry with implicit co ercions to

see that it might be the case that neither the unicity of typ es prop erty nor the

minimal typ e prop erty are satised

let and be dierent typ e variables Then the Let C f g and

identity function xx has as typ e scheme where  In particular we

C

have that

xx and xx

but there is no typ e such that

xx  and 

C C

Unlike the system Alg the system satises the sub ject reduction prop erty

Sub ject reduction for Proposition

If M and M M then M

Proof By lemma weknow that there exists such that M

Alg

and  By monotonic sub ject reduction M for some

C Alg

 Then by lemma it follows that M Finally since

C

 by TSubsumptionitfollows that M

C

a system with explicit co ercions

C

Definition The typ ed calculus with explicit co ercions is dened as

C

follows The set of pseudoterms is dened by the following grammar

C

V j V T j j Q

where V is a set of term variables and Q is a set of constants with c Q if and

only if 

C

The set of typ es T and the concepts of statement typing assumption sub ject

context derivable statement and legal term are as in denition

Definition

if x TVar x

C

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

x M

C

TAbs

x M

C

M N

C C

TApp

MN

C

M 

C

C

TCoerce

c M

C

The standard notions of reduction and substitution are extended with the

following rules

If M M then c M c M

c N xM c N x M

Lemma Weakening for Let and be contexts such that

C

M then M Then if

C C

Proof By induction on the derivation of M

C

Proposition Generation for

C

If x x

C

If MN there exists suchthat M and N

C C C

If xM there exists suchthat and x M

C C

If c M M and 

C

C C

Proof Since the system is syntax directed the form of the sub ject uniquely

determines the last rule of the derivation of the typing statement

Lemma Let M

C

N If x M and N then M x

C C C

Proof By induction on the complexity of M

Proposition Sub ject reduction for

C

If M and M M then M

C C

Proof By induction on the complexity of M Let us consider the case when

M is an application and in particular a redex Then M x P N and

M P xN By generation we hav N P and e that x

C C

Finally due to lemma we get P xN

C

It is instructive to compare these last two results with lemma and pro

p osition the corresp onding results for Alg

THE RELATION BETWEEN AND

C

The relation between and

C

If we go back to the discussion in the intro duction the TCoerce rule is more

than what we actually need to be able to apply a function to an argument of a

typ e smaller than its domain typ e On the other hand the TApp rule ts p er

fectly in our requirements In the intro duction we mention the dierence b etween

implicit and explicit co ercions in this framework Alg has implicit co ercions and

lg there is a uniform way to nd furthermore given a legal term M in A

a term M in that is the explicitly co erced version of M as will b e dened

C

in denition This can be read as there is no need to write the co ercions

b ecause they can be automatically recovered

Definition The implicitly co erced version of M

j j Alg

C

jxj x

jxM j x jM j

jMNj jM jjN j

jc M j jM j

Lemma If M then there exists such that jM j

Alg

C

and 

C

Proof By induction on the derivation of M

C

This lemma says that the implicitly co erced version of a legal term is a

C

legal Alg term

Definition dec ContextAlg

C

dec x x



dec x M xdec M

 x

dec MN dec M c dec N

  

where N

Alg

and M

Alg

dec stands for decoration

Observe that dec is a partial mapping given that there may not b e such or

in the application case and that it may not be the case that  in which

C

case there is no c constant On the other hand the next result shows that dec

is total on the subset of legal Alg terms

Lemma Let M Alg then

M if and only if dec M

Alg 

C

Proof

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

By induction on the complexity of M

M Let us consider the case of By induction on the derivation of

Alg

the TApp rule Then the situation is as follows

P N 

Alg Alg C

PN

Alg

where M PN

By the induction hyp othesis dec P and dec N

 

C C

and by the TCoerce rule c dec N Finallyby the TApp



C

rule it follows that dec P c dec N and by prop osition

 

C

we conclude dec PN dec P c dec N

  

Lemma

M if and only if there exists such that c dec M



C

Proof

If c dec M by generation for prop osition

 C

C

 dec M and

C 

C

By lemma M and by the soundness of Alg

Alg

M Finallythe result follows by TSubsumption

If M then by the completeness of Alg there exists

such that

M and

Alg



C

Then by lemma it follows that dec M Finally the result



C

follows b y TCoerce

This last lemma says that can be translated into Moreover together

C

with theorem implies that can b e translated into

We could compare the systems Alg and using an auxiliary system call it

C

obtainedby replacing the rules TApp and TCoerce of by the following

C C

rule

M N 

C

C C

TApp

C

Mc N

C

It is easy to check that satises the unicity of typ es prop erty Unfortu

C

is not suciently wellb ehaved to be of indep endent in nately the system

C

terest For example it do es not satisfy the sub ject reduction prop erty The failure

is such that reductions on well typ ed terms can yield illegal terms For example

if C f g then y x xc y but y c y

C C

is not aderivable statement

The following lemma shows that is an intermediate step between Alg

C

and C

SIMPLY TYPED CALCULUS AND

C

Lemma If M then M

C

C

M then there exists such that  and M If

C

C

C

If M then jM j

Alg

C

If M then dec M

Alg 

C

If M then dec jM jM



C

M then jdec M j M If

 Alg

Observe that lemma is now a consequence of items and of the last

are equivalent in the sense lemma and the last four items saythatAlg and

C

that every term that can be typ ed in Alg has its explicitly co erced version in

has its implicitly co erced version and every term that can be typ ed in

C C

in Alg Furthermore the translation between the two systems do es not cause

any loss of information as long as no reduction is involved

Simply typ ed calculus and

C

In this section we show how to translate into The rst step of this

C

translation consists of representing subtyping statements as terms For this

we dene an environmen t in which there is a typing statementforeachsubtyping

axiom of C as follows

Definition The environment Let k k b e dierent constants

C

n n

 

of K Then

fk k g

C n n

n n

 

such that C if and only if k

i i C

i i

is just a mnemonic name for a constant Next we dene the Observe that k

i i

function nd that nds a term that p erforms the co ercion from to if 

C

For that we use the following auxiliary denition

Definition Typ ed comp osition

f g xf gx

For the sake of readabilitywe use simply

If welookatC as a directed graph there may b e more than one path b etween

two variables of C and that is the reason why a choice is involved An example

of such situation is the following

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

Example Sup osse C f g Then there are two

nonconvertible terms that p erform the co ercion from to namely

k k and k k

Therefore we assume a choice function choose so that nd is well dened

VV

Definition nd T T

ndC xx

nd C choosefk k j

n

n  

and C for every ifngg

n i i

nd C x nd C x nd C

    

where and

 

Observe that nd is a partial mapping given that choose may fail and it is

only dened when the rst and second arguments are in the  relation Note

C

that if we imp ose the restriction that C must be transitively closed then we can

simplify the function nd and redene the second clause as

nd C k

The next lemma states that if  then ndC is a co dication of a

C

simply typ ed lamb da calculus without subtyping subtyping statement in the

Lemma If  then findC

C 

C

Proof By induction on the complexity of

Case is a variable Then by lemma is also a variable According

to lemma we can distinguish two cases

Case a Then by denition ndC x x Further

more it holds that xx



C

ansC Then as k by the denition of Case b tr

i j

i j

C

we have that k k

C 

n

n  

C

Case Then by lemma If the result

 

follows as in Case a Otherwise by lemma  and 

C  C 

By the induction hyp othesis we have

nd C and



C

nd C

    

C

Hence x nd C x nd C

   

C

two Observe that actually this result could be obtained as a corollary of these facts

SIMPLY TYPED CALCULUS AND

C

If  then ndC is dened and

C

ndC If ndCisdened then



C

With the following denition we can relate the system with

C

Definition M The version of M

C

x x

xM x M

MN M N

c M ndCM

Then M is obtained from M by replacing the co ercion constants by terms that

will p erform the corresp onding co ercion

Note that is a total function b ecause the existence of c implies 

C

Proposition Let M in Then

C

If M then M



C C

Proof By induction on the derivation of M

C

TVar Let M x Then x As x x using the TVar rule in we

get x



C

TAbs Let M xN and Then x N follows

C

from x N By the induction hyp othesis it follows that

C

x N By TAbs we conclude x N

 

C C

and by the denition of xN



C

TApp M NP and NP follows from the statements P

C C

and N for some By the induction hyp othesis the T

C

the result follows App rule of and the denition of

TCoerce Misc N and c N follows from N and

C C

 By the induction hyp othesis N and due to

C 

C

lemma nd C Finally using the weakening



C

lemma together with the TApp rule of and the denition of

we get c N



C

Proposition Let M in Then

C

M M



C C

Proof By induction on the complexity of M

Case M xV Then M x By the free variable lemma of we can

conclude that x and by TVar x

C

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

Case M PQ Then M P Q By generation there exists such that

P and Q

 

C C

By the induction hyp othesis

P and Q

C C

By TApp we get PQ

C

Case M x N Then M x N By generation it follows that

x N and We now can apply the induction hy



C

p othesis and we get x N and to that we only have to apply

C

the TAbs rule to get xN

C

Case M c N Then M nd CN Recall that the constant

c exists if and only if  Then using lemma we know that

C

nd C By generation the unicityoftyp es lemma and



C

the weakening lemma it follows that N By the induction



C

hyp othesis we have N Finally by applying TCoerce it

C

follows that c N

C

Putting together the last two prop ositions we can state the following theorem

Theorem Let M in Then

C

M M



C C

This last result can be read as follows The simply typ ed calculus without

subtyping is an appropriate mo del for the simply typ ed calculus with subtyping

We can also extract from it the conclusion that the simply typ ed calculus with

explicit subtyping is a conservative extension of the calculus without subtyping

b ecause if M is a term then M M

Metatheory of

C

The system is of indep endentinterest The previous theorem and the fact that

C

M is always dened imply that the typ e checking and typ e inference problems

are decidable given that the corresp onding problems in are see Bar

Recall that a pseudoterm M is called strongly normalizing if there is no innite

reduction chain starting from M We reduce the strong normalization prop ertyof

to the strong normalization result for see Bar

C

First we prove some auxiliary results rst

Lemma Substitution lemma Let M N in and x V Then

C

M x N M x N

SIMPLY TYPED CALCULUS AND

C

Proof By induction on the complexityofM We consider here only two cases

the missing ones are proven in a similar way

M x

xx N N by denition of

xxN by denition of

x xN by denition of

M c P

c P xN c P xN by denition of

ndCP xN by denition of

ndCP xN by the induction

hyp othesis

ndCP xN

c P xN by denition of

Observe that ndCis a closed term

Lemma Reduction preservation Let M N in Then if M N

C

then M N

Proof By induction on the complexity of M

Case If M V the result is vacuously true

Case M xM Then N is of the form x N where M N By the

induction hyp othesis and the denition of the result holds

Case M M M Then we have the following three cases



N N M where M N



N M N where M N and

  

M x P M and N P xM

 

The rst two cases follow from the induction hyp othesis and the denition

and the third is a consequence of the substitution lemma of

Case M c M The result follows from the induction hyp othesis and

the denition of

Theorem Strong normalization for

C

If M then M is strongly normalizing

C

CHAPTER IMPLICIT AND EXPLICIT SUBTYPING

Proof Supp ose towards a contradiction that M is not strongly normalizing

This means that there exists an innite reduction chain starting from M By the

reduction preservation lemma weknow that there is also an innite chain

of reductions starting from M and using prop osition we know that

M



C

But we also know that has the strong normalization prop erty whichyieldsa

contradiction Hence M is strongly normalizing

Conuence

Using the results ab out conuence of orthogonal combinatory reduction systems

conuent as a CRSs in vR we can state that the systems and are

C

consequence of what we have already proven in this chapter The sets of pseudo

terms with the reduction rule and with the reduction rule are

C

according to the denitions of vR two orthogonal CRSs The sub ject reduction

prop ertyof and of imply that the corresp onding sets of legal

C

terms are two substructures of and resp ectively Since substructures

C

of orthogonal CRSs are also orthogonal it follows that the systems and are

C

conuent

Conclusions

In this chapter we analyze two dierent styles of subtyping subtyping with im

plicit co ercions and subtyping with explicit co ercions We dene and study two

alternative presentations of subtyping for simply typ ed lamb da calculus The rst

one a system with implicit co ercions and the second one a system with

C

explicit co ercions We show that the system can be translated into and

C

in its turn can b e translated into In other words b oth disciplines can b e

C

compiled into the simply typ ed lamb da calculus without subtyping

Chapter

Future research

has The study of the metatheory of a rich typ ed lamb dacalculus such as F

drawn our attention towards op en and challenging problems such as the ones

listed b elow

A normalizing fragment of F

Although the reduction on typ es is strongly normalizing on wellkinded typ es

the reductions on terms are not strongly normalizing for the simple reason that

every closed term can b e assigned a typ e Therefore wewould liketocharacterize

a normalizing subset of the language of terms A similar situation arises for the

yp e discipline a la Curry studied by the group in Torino In their intersection t

framework a term e is strongly normalizing if and only if there exists a derivation

of a typing statement with e as sub ject which do es not contain the maximal typ e

In our case this statement is not true A very simple counterexample is the

term

x x

which is in normal form and all whose derivations contain the maximal typ e

This problem is sub ject of current researchby Mariangiola Dezani and the author

of this thesis

Bounded op erator abstraction

KT and its asso In F and also in F abstraction on typ es is of the form X

ciated formation rule is

K



X K T K

 

KOAbs

X K T K K

 

A natural enrichment of the theory would replace X KT by X S KT

using the following formation rule

X S K T K

 

KBoundedOAbs

X S KT X S K K



CHAPTER FUTURE RESEARCH

This means that we need to mo dify the language of kinds b ecause S the b ound

of X is required in order to assign a kind to a typ e op erator application Then

the formation rule is as follows

T X S K K S S



KBoundedOApp

TS K X S



Consequently the kind inference and kind checking algorithms become more

complicated since they involvechecking a subtyping judgement The metatheory

and applications of this extension are the sub ject of current research by Paula

Severi and the author of this thesis

Subtyping dep endent typ es

The typ etheoretic foundations of pro of development systems include the Auto

math family of calculi dB the Edinburgh Logical Framework HHP the

CH Extended Calculus of Constructions Luo Calculus of Constructions

and MartinLof typ e theory SNP Implementations of these theories have b een

used in a numb er of pro of development systems Automath at Eindhoven Coq

at INRIA Lego at the LFCS and Alf at Goteb org A common feature of these

systems is their heavy reliance on dep endent typ es and the consequent diculty

of their metatheoretic analysis

The interaction of subtyping with dep endent typ es seems to be the principal

obstacle to its integration in pro of development systems Some work in this area

has b een started by Cardelli Car Carb who gives basic denitions and some

ideas ab out typ e checking algorithms and by Pfenning Pfe who has prop osed

variants of the Logical Framework and the Calculus of Constructions with re

nement typ es a simple form of intersection typ es whose interaction with typ e

conversion and dep endency is strictly controlled Aside from these preliminary

eorts the area remains unexplored

The system P is an extension of the simply typ ed lambda calculus with

dep endent typ es Bar The metatheory of P an extension of P with sub

typing is b eing develop ed by David Aspinall and the author of this thesis

App endix A

Summary of Denitions

A F

A Reduction rules for typ es

X KT T T X T

 

V V

T T S T S T S

n n

V V

X S K T T X S KT X S KT

n n

V V

K K K



K T T X K T X K T X

n n

V V

K K K



T U T U T T U

n n

V V V

K K K

T S S T T S S T

n m n m

A Reduction rules for terms

xT e e e x e

 fors 

X T K eT eX T

fors

forX T T e e forX T T e e

n  fors n 

forX T T e e if X FVe

n fors

formation rules A Context

ok CEmpty

T x dom

CVar

x T ok

T K X dom

CTVar

X T K ok

APPENDIX A SUMMARY OF DEFINITIONS

A Kinding rules

X T K ok



KTVar

X T K X K



T T



KArrow

T T



X T K T



KAll

X T K T



K



X K T K

 

KOAbs

X K T K K

 

S K K T K



KOApp

ST K



ok for each i T K

i

KMeet

V

K

T T K

n

A Subtyping rules

S K T K S T

SConv

S T

S T T U

STrans

S U

X T K ok



STVar

X T K X T



T S S T S S

  

SArrow

S S T T

 

X U K S T X U KS

SAll

X U KS X U KT

K

X K S T

SOAbs

X KS X KT

S T SU K

SOApp

SU TU

for each i S T S K

i

SMeetG

V

K

S T T

n

V

K

T T K

n

SMeetLB

V

K

T T T

n i

A F

A Typing rules

xT ok



TVar

xT x T



xT e T



TAbs

xT e T T



f T T a T



TApp

fa T



X T K e T



TTAbs

X T K e X T K T



f X T K T S T



TTApp

fS T X S



eX S T S fS S g

n

TFor

forX S S e T

n

ok for each i e T

i

TMeet

V

e T T

n

e S S T

TSubsumption

e T

A Subtyp e checking AlgF subtyping rules

X K

AlgSTVarRefl

X X

Al g

TS K

AlgSOAppRefl

TS TS

Al g

X A X A

Al g

AlgSTVar

X A

Al g

T S S T S S

Al g Al g   

AlgSArrow

S S T T

Al g  

X U K S T X U KS

Al g

AlgSAll

X U KS X U KT

Al g

K

X K S T

Al g

AlgSOAbs

X KS X KT

Al g

nf

lub TS A TS K TS A

Al g 

AlgSOApp

TS A

Al g

ifmg A T A K

Al g i

AlgS

V

K

T T A

m Al g

j fng S A k fng S K

Al g j k

AlgS

V

K

S S A

n Al g

APPENDIX A SUMMARY OF DEFINITIONS

ifmgj fng S T k fng S K

Al g j i k

AlgS

V V

K K

T T S S

m n Al g

A Typ e inference

Definition Homomorphic extension of lub to intersections lub

lub X X



lub ST lub S T

 

V V

K K

lub T T T T if ifng such that lub T

n i

 n 

where T is lub T if lub T and T otherwise

i i i

i  

Definition Functional Least Upp er Bound The functional least upp er b ound

ofatyp e T inacontext ub T is dened as follows



nf nf

T T if lub ub lub



 

ub T



nf

T otherwise

Definition arrows and al ls

T T fT T g arrows

 

V

T T arrows T arrows

n i

ifng

arrows T

V

if T T T and T T T

 n

al ls X T KT fX T KT g

 

V

T T al ls al ls T

n ifng i

al ls T

V

T T if T X T KT and T

n 

Typ e inference rules

xT ok



ATVar

xT x T

 inf

xT e T

inf 

ATAbs

xT e T T

inf 

f T a S

inf inf

ATApp

V

fa T j S T arrows ub T and S S

inf i i i i



X T K e T

inf 

ATTAbs

X T K e X T K T

inf 

f T

inf

V

fS T X S jX S KT al ls ub T and S S

inf i i i i



ATTApp

for all ifng eX S T

inf i i

ATFor

V

T T forX S S e

n inf n

A FIRST ORDER SUBTYPING

A First order subtyping

A

x if x TVar

x M

TAbs

xM

M N

TApp

MN

M 

C

TSubsumption

M

A

C

x if x TVar

C

x M

C

TAbs

x M

C

M N

C C

TApp

MN

C

M 

C

C

TCoerce

c M

C

APPENDIX A SUMMARY OF DEFINITIONS

Bibliography

AC Rob erto M Amadio and Luca Cardelli Subtyping recursive typ es ACM

Transactions on Programming Languages and Systems

A preliminary version app eared in POPL pp and as DEC

Systems ResearchCenter Research Rep ort numb er August

AH A Avron and I Honsell Fand Mason Using typ ed lambda calculus to

implement formal systems on a machine Technical Rep ort ECSLFCS

LFCS University of Edinburgh July

Bar H P Barendregt The Lambda Calculus North Holland revised edition

Functional programming and lambda calculus In Bar Henk P Barendregt

J van Leeuwen editor Handbook of Theoretical Computer Science volume

B Formal Mo dels and Semantics chapter pages Elsevier Science

Publishers B V NorthHolland

Bar H Barendregt Lamb da calculi with typ es In T S E Maibaum S Abramsky

Dov M Gabbay editor Handbook of Login in Computer Sciencevolume

pages Clarendon Press Oxford

BCD H P Barendregt M Copp o and M DezaniCiancaglini A lter lambda

mo del and the completeness of typ e assignment Journal of Symbolic Logic

BCGS Val BreazuTannen Thierry Co quand Carl Gunter and Andre Scedrov

Inheritance as implicit co ercion Information and Computation

BL Kim B Bruce and Giusepp e Longo A mo dest mo del of records inherit

ance and b ounded quantication Information and Computation

Also in GM An earlier version app eared in the pro ceedings

of the IEEE Symp osium on Logic in Computer Science

BM Kim Bruce and John Mitchell PER mo dels of subtyping recursive typ es

and higherorder p olymorphism In Proceedings of the Nineteenth ACM Sym

posium on Principles of Programming Languages Alb equerque NM January

BMM Kim B Bruce Alb ert R Meyer and John C Mitchell The semantics of

secondorder lambda calculus In Huet Hue pages Also ap

uary p eared in Information and Computation Jan

BIBLIOGRAPHY

Bru Kim B Bruce A paradigmatic ob jectoriented programming language

Design static typing and semantics Journal of Functional Programming

April A preliminary version app eared in POPL under the

title Safe Typ e Checking in a Statically Typ ed Ob jectOriented Program

ming Language

Car Luca Cardelli Typ echecking dep endenttyp es and subtyp es In Proc of the

Workshop on Foundations of Logic and Functional Programming Trento

Italy Decemb er

Cara Luca Cardelli A semantics of multiple inheritance Information and Com

putation Preliminary version in Semantics of Data Types

Kahn MacQueen and Plotkin eds SpringerVerlag LNCS

Carb Luca Cardelli Structural subtyping and the notion of p ower typ e In Proceed

ings of the th ACM Symposium on Principles of Programming Languages

pages San Diego CA January

Car Luca Cardelli Notes ab out F Unpublished manuscript Octob er

Car Luca Cardelli Extensible records in a pure calculus of subtyping Research

rep ort DEC Systems ResearchCenter January Also in GM

CC Felice Cardone and Mario Copp o Two extensions of Currys typ e inference

system In Piergiorgio Odifreddi editor Logic and Computer Sciencenum

ber in APIC Studies in Data Pro cessing pages Academic Press



CCH Peter Canning William Co ok Walter Hill Walter Oltho and John

Mitchell Fb ounded quantication for ob jectoriented programming In

Fourth International ConferenceonFunctional Programming Languages and

Computer Architecture pages Septemb er

CD M Copp o and M DezaniCiancaglini An extension of the basic functionality

theory for the calculus NotreDame Journal of Formal Logic

Octob er

CDC M Copp o and M DezaniCiancaglini A new typ eassignment for terms

Archiv Math Logik

CDdL Felice Cardone Mariangiola DezaniCiancaglini and Ugo de Liguoro Com

bining typ e disciplines Manuscript

CG PierreLouis Curien and Giorgio Ghelli Coherence of subsumption Min

imum typing and typ echecking in F Mathematical Structures in Computer

Science Also in GM

CH Thierry Co quand and Gerard Huet The Calculus of Constructions Inform

ation and Computation FebruaryMarch

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

subtyping In Seventeenth Annual ACM Symposium on Principles of Pro

gramming Languages pages San Francisco CA January Also in GM

BIBLIOGRAPHY

Chu Alonzo Church An unsolvable problem of elementary numb er theory Amer

J Math

Chu Alonzo Church The Calculi of Lambda Conversion Princeton University

Press

CL Luca Cardelli and Giusepp e Longo A semantic basis for Quest Journal of

Functional Programming Octob er Preliminary version

in ACM Conference on Lisp and Functional Programming June Also

available as DEC SRC Research Rep ort Feb

CM Luca Cardelli and John Mitchell Op erations on records Mathematical Struc

tures in Computer Science Also in GM and available as

DEC Systems ResearchCenter Research Rep ort August and in

the pro ceedings of MFPS Springer LNCS volume

Com Adriana B Compagnoni Subtyping in F is decidable Technical Rep ort

ECSLFCS LFCS University of Edinburgh January Presented

at Computer Science Logic Septemb er under the title Decidabilityof

HigherOrder Subtyping with Intersection Typ es

CP Adriana B Compagnoni and Benjamin C Pierce Multiple inheritance via

intersection typ es Technical Rep ort ECSLFCS LFCS University

of Edinburgh August Also available as Catholic University Nijmegen

computer science technical rep ort

CP Giusepp e Castagna and Benjamin Pierce Decidable b ounded quantication

In Proceedings of TwentyFirst Annual ACM Symposium on Principles of

Programming Languages Portland ORACM January

CW Luca Cardelli and Peter Wegner On understanding t yp es data abstraction

and p olymorphism Computing Surveys Decemb er

dB Nicolas G de Bruijn Lamb dacalculus notation with nameless dummies

a to ol for automatic formula manipulation with application to the Church

Rosser theorem Indag Math

dB Nicolas G de Bruijn A survey of the pro ject AUTOMATH In J P Seldin

and J R Hindley editors To H B Curry Essays in Combinatory Logic

Lambda Calculus and Formalism pages Academic Press

FM Kathleen Fisher and John Mitchell Notes on typ ed ob jectoriented program

ming In Proceedings of Theoretical Aspects of Computer Software Sendai

Japan pages SpringerVerlag April LNCS

FMRS P Freyd P Mulry G Rosolini and D Scott Extensional PERs In Fifth

Annual Symposium on Logic in Computer Science Philadelphia PA pages

IEEE Computer So ciety Press June

Ghe Giorgio Ghelli Proof Theoretic Studies about a Minimal Integ

rating Inclusion and PhD thesis Universita di

Pisa March Technical rep ort TD Dipartimento di Informatica

UniversitadiPisa

BIBLIOGRAPHY

Ghe Giorgio Ghelli January Message to the Types mailing list

Gir JeanYves Girard Interpretation fonctionel le et elimination des coupures de

larithmetique dordre superieur PhD thesis UniversiteParis VI I

GM Carl A Gunter and John C Mitchell Theoretical Aspects of ObjectOriented

Programming Types Semantics and Language Design The MIT Press

HHP Rob ert Harp er Furio Honsell and Gordon Plotkin A framework for dening

logics Journal of the ACM Preliminary version in

LICS

HP Martin Hofmann and Benjamin Pierce A unifying typ etheoretic framework

for ob jects Journal of Functional Programming To app ear Previous

in the Symp osium on Theoretical Asp ects of Computer versions app eared

Science and under the title An Abstract View of Ob jects and Sub

typing Preliminary Rep ort as University of Edinburgh LFCS technical

rep ort ECSLFCS

HS J Roger Hindley and Jonathan P Seldin Introduction to Combinators and

Calculusvolume of London Mathematical Society Student Texts Cam

bridge University Press

Hue Gerard Huet editor Logical Foundations of Functional Programming Uni

versity of Texas at Austin Year of Programming Series AddisonWesley

Lan P J Landin A corresp ondence between ALGOL and Churchs lambda

notation Communications of the ACM

Luo Zhaohui Luo An Extended Calculus of Constructions PhD thesis University

of Edinburgh

Mar Per MartinLof An intuitionistic theory of typ es predicative part In H E

Rose and J C Shepherdson editors Logic Col loquium pages

Amsterdam North Holland

Mit J C Mitchell Lambda Calculus Models of Typed Programming Languages

PhD thesis Massachusetts Institute of Technology

Mita John C Mitchell Towardatyp ed foundation for metho d sp ecialization and

oceedings of the th ACM Symposium on Principles of inheritance In Pr

Programming Languages pages January Also in in GM

Mitb John C Mitchell A typ einference approach to reduction prop erties and

semantics of p olymorphic expressions In Huet Hue pages

Mitc John C Mitchell Typ e systems for programming languages In J van

Leeuwen editor Handbook of Theoretical Computer Science volume B

Formal Mo dels and Semantics chapter pages Elsevier Science Publishers B V NorthHolland

BIBLIOGRAPHY

Pfe Frank Pfenning Renementtyp es for logical frameworks In Informal Pro

ceedings of the Workshop on Types for Proofs and Programs pages

May

Pie Benjamin C Pierce Programming with Intersection Types and Bounded

Polymorphism PhD thesis Carnegie Mellon University December

Available as Scho ol of Computer Science technical rep ort CMUCS

PT Benjamin C Pierce and David N Turner Simple typ etheoretic founda

tions for ob jectoriented programming Journal of Functional Programming

version app eared in Principles of April A preliminary

Programming Languages and as University of Edinburgh technical

rep ort ECSLFCS under the title Ob jectOriented Programming

Without RecursiveTyp es

Rey John Reynolds Using category theory to design implicit conversions and

generic op erators In N D Jones editor Proceedings of the Aarhus Workshop

on SemanticsDirected Compiler Generationnumb er in Lecture Notes in

Computer Science SpringerVerlag January Also in GM

Rey John C Reynolds Preliminary design of the programming language For

sythe Technical Rep ort CMUCS Carnegie Mellon University June

Sco Dana Scott Data typ es as lattices SIAM Journal on Computing

SNP Jan Smith Bengt Nordstrom and KentPetersson Progr amming in Martin

Lof s An Introduction Oxford University Press

SP Martin Steen and Benjamin Pierce Higherorder subtyping In IFIP

Working ConferenceonProgramming Concepts Methods and Calculi PRO

COMET June An earlier version app eared as University of Edinburgh

technical rep ort ECSLFCS and Universitat ErlangenNurn berg In

terner Bericht IMMD February

vR Femke van Raamsdonk A simple pro of of conuence for weakly orthogonal

combinatory reduction systems Technical Rep ort CSR CWI Amster

dam August

Wan Mitchell Wand A simple algorithm and pro of for typ e inference Fundamenta

Informaticae X pages North Holland

BIBLIOGRAPHY

Curriculum Vit

July December Courses of the Licenciatura en Ciencias Matematicas

at Universidad de Buenos Aires Argentina

March December Licenciatura en Informatica

at Escuela Sup erior LatinoAmericana de Informatica

Buenos Aires Argentina

January Undergraduate degree Licenciada en Informatica

Escuela Sup erior LatinoAmericana de Informatica

June May PhD program at the Katholieke Universiteit Nijmegen

Nijmegen The Netherlands

NWOSION pro ject Typed lambda calculus

Octob er April Visited the Lab oratory for Foundations of Computer Science

UniversityofEdinburgh Scotland

From June ResearchFellow

at the Lab oratory for Foundations of Computer Science

UniversityofEdinburgh Scotland

EPSRC GRANT GRG

Constructive logic as a basis for program development

Samenvatting

Subtyp ering is een primitieve relatie waarmee op een uniforme wijze b egripp en uit di

verse gebieden van de informatica kunnen worden beschreven In het geval dat S en

T verzamelingen zijn betekent S T S is een subtype van T elementen van S zijn

ook elementen van T Als S en T sp ecicaties zijn dan voldo en elementen die aan

de sp ecicatie S voldo en ook aan T Als S en T ob jectb eschrijvingen zijn in ob ject

enteerd programmeren dan betekent S T dat het op plaatsen waar een ob ject geori

met interface T wordt verwacht ook een ob ject met interface S gebruikt mag worden

Wanneer S en T mo dule interfaces zijn in een software systeem dan is een implementatie

van S ook een implementatie van T Als S en T stellingen zijn daniseen bewijs van

S o ok een b ewijs van T Het b egrijp en van de essentie de subtiliteiten en de algemene

eigenschap en van subtyp ering werpt lichtopeenomvangrijk gebied

Dit pro efschift b evat twee delen Het eerste deel geeft een gedetailleerde analyse van

de metatheorie van een getyp eerde lamb da calculus waarin intersectietyp es en hogere

Ons onderzo ek b etreft syntactische orde b egrensde quanticatie worden gecombineerd

semantische en pragmatische asp ecten

In ho ofdstuk denieren we het systeem F en b ewijzen we een aantal ele

mentaire eigenschapp en

We deneren de getyp eerde lamb da calculus F een natuurlijke generalisa

tie van Girards systeem F met intersectietyp es en b egrensd p olymorsme

Een nieuw asp ect van onze presentatie is het gebruik van termherschrijf

technieken om intersectietyp es te denieren waardo or de computationele

semantiek reductieregels duidelijk van de syntax inferentieregels van het

systeem wordt gescheiden

De reductieregels van F kunnen gesplitst worden in twee ho ofdgro ep en

reductie van typ es en reductie van termen Ho ewel conu

fors

entie in het algemeen niet een mo dulaire eigenschap is is het in ons geval

wel mogelijk om een mo dulair b ewijs te geven In sectie combineren

we de onafhankelijke b ewijzen van conuentie voor reductie van typ es en

conuentie vo or reductie van termen tot een b ewijs van conuentie van de

reductierelatie van het gehele systeem

eigenschap van op go edgevormde We b ewijzen de sterke normalisatie

typ es

Ho ofdstuk bevat het meest b elangrijke resultaat van dit pro efschrift Onze

vo ornaamste bijdrage is het b ewijs van het feit dat subtyp ering in F b eslisbaar

is Dit resultaat heeft een oplossing tot gevolg voor het tot nu to e op en prob

leem van de b eslisbaarheid van subtyp ering in F het intersectievrije deel van

F omdat het subtyp erinssysteem van F een conservatieve uitbreiding van de

subtyp eringsrelatie van F is Verder is de b eslisbaarheid van subtyp ering es

sentieel voor de b eslisbaarheid van typ e checking en typ e inferentie Een andere

o orspronkelijke bijdrage is het gebruik van een keuzeop erator om het gedrag van

variab elen tijdens subtyp e checking te mo delleren Het b ewijs van de b eslisbaar

heid wordt opgesplitst in de volgende stapp en

We denieren een algoritmische presentatie van de subtyp eringsrelatie waar

bij wealleentyp es in normaalvorm b eschouwen

presentatie sound en complete is met We b ewijzen dat deze algoritmische

b etrekking tot de denitie van subtyp ering dat wil zeggen dat hij een de

terministische pro cedure b epaalt voor het checken van subtyp ering in F

Tenslotte b ewijzen we dat de do or de algoritmische presentatie beschreven

pro cedure termineert Het b ewijs van terminatie wordt herleid tot de sterke

normalisatieeigenschap van reductie op typ es uitgebreid met een keuze

reductie die het gedrag van variab elen tijdens het checken van subtyp ering

mo delleert

yp eeigenschap heeft en we In ho ofdstuk b ewijzen we dat F de minimale t

b eschrijven een algoritme voor het b erekenen van de minimale typ es Bovendien

b ewijzen we dat typ e inferentie en typ e checking in F b eslisbaar zijn De mini

male typ eeigenschap wordt gebruikt om te b ewijzen dat F de sub ject reductie

eigenschap heeft

In ho ofdstuk denieren we een mo del gebaseerd op partiele equivalentierelaties

en we b ewijzen dat de subtyp eringsrelatie sound is met b etrekking tot dit mo del

gedenieerd was om een mo del te creeren voor ob ject georienteerd Ho ewel F

programmeren met multiple inheritance is het niet de b edo eling van dit pro ef

schrift om de grondslagen van ob ject georien teerd programmeren te b ehandelen

In ho ofdstuk laten we zien ho e multiple inheritance met b ehulp van subtyp er

ing gemo delleerd kan worden Dit is een vo ortzetting van het onderzo ek naar de

typ etheoretische grondslagen van ob ject georienteerd programmeren do or Pierce

en Turner PT die multiple inheritance buiten b eschouwing laten

In het tweede deel van dit pro efschrift worden twee verschillende stijlen van sub

typ ering b estudeerd subtyp ering met impliciete coercies en subtyp ering met expliciete

coercies We denieren en b estuderen twee alternatieve presentaties van subtyp ering

voordesimpelgetyp eerde lamb da calculus De eerste is een systeem met implici

tweede is een systeem met expliciete coercies We laten zien ete coercies en de

C

dat het systeem vertaald kan worden in endat vertaald kan worden in

C C

Vanuit een pragmatische invalsho ek b etekent dat dat impliciete of expliciete coercies

slechts een kwestie van smaak zijn en dat b eide b enaderingen vertaald kunnen worden

in de simp el getyp eerde lamb da calculus zonder subtyp ering

Translated byJanKuper