# normalisation in lambda calculus and its relation to type ... · we give an informal explanation of...

Embed Size (px)

TRANSCRIPT

Normalisation in lambda calculus and its relation to typeinferenceCitation for published version (APA):Severi, P. G. (1996). Normalisation in lambda calculus and its relation to type inference. Technische UniversiteitEindhoven. https://doi.org/10.6100/IR461540

DOI:10.6100/IR461540

Document status and date:Published: 01/01/1996

Document Version:Publisher’s PDF, also known as Version of Record (includes final page, issue and volume numbers)

Please check the document version of this publication:

• A submitted manuscript is the version of the article upon submission and before peer-review. There can beimportant differences between the submitted version and the official published version of record. Peopleinterested in the research are advised to contact the author for the final version of the publication, or visit theDOI to the publisher's website.• The final author version and the galley proof are versions of the publication after peer review.• The final published version features the final layout of the paper including the volume, issue and pagenumbers.Link to publication

General rightsCopyright and moral rights for the publications made accessible in the public portal are retained by the authors and/or other copyright ownersand it is a condition of accessing publications that users recognise and abide by the legal requirements associated with these rights.

• Users may download and print one copy of any publication from the public portal for the purpose of private study or research. • You may not further distribute the material or use it for any profit-making activity or commercial gain • You may freely distribute the URL identifying the publication in the public portal.

If the publication is distributed under the terms of Article 25fa of the Dutch Copyright Act, indicated by the “Taverne” license above, pleasefollow below link for the End User Agreement:www.tue.nl/taverne

Take down policyIf you believe that this document breaches copyright please contact us at:openac[email protected] details and we will investigate your claim.

Download date: 23. Feb. 2021

~AUlA ~tVt~ .

Normalisation in Lambda Calculus

and its relation to Type Inference

PROEFSCHRIFT

ter verkrijging van de graad van doctor aan de Technische Universiteit Eindhoven, op gezag van de Rector Magnificus, prof.dr. J.H. van Lint, voor een commissie aangewezen door het College van Dekanen in het openbaar te verdedigen op dinsdag 18 juni 1996 om 16.00 uur

door

PAULA GABRIELA SEVERI

geboren te Montevideo, Uruguay

Dit proefschrift is goedgekeurd door de promotoren: prof.dr. J.C.M. Baeten prof.dr. H.P. Barendregt en de copromotor: dr. J.H. Geuvers

Normalisation in Lambda Calculus

and its relation to Type Inference

PAULA GABRIELA SEVERI

CIP-GEGEVENS KONINKLIJKE BIBLIOTHEEK, DEN HAAG Severi, Paula Gabriela Normalisation in lambda calculus and its relation to type inference/ Paula Gabriela Severi Eindhoven: Eindhoven U niversity of Technology Thesis Technische Universiteit Eindhoven -With index, ref. With summary in Dutch ISBN 90-386-0218-9

Cover design: Laura Severi Druk: Universiteit Drukkerij TU Eindhoven

Acknowledgements. I would like to thank all the people who helped me during my career.

Special thanks to Herman Genvers for his patience, advice and help to organise my work, to Adriana Compagnoni and Rob Nederpelt for helping me to find a position in The Netherlands. I had the pleasure to work during this period with Walter Ferrer, Erik Poll and Femke van Raamsdonk. Thanks also to those who helped me to improve a rather raw initia! version of this thesis: Jos Baeten, Henk Barendregt, Herman Geuvers, Kees Hemerik, Jan Willem Klop and Rob Nederpelt. I want also tomention that Tijn Borghuis translated into Dutch the English summary of the thesis.

I would also like to thank the people in the group of type theory at the University of Eindhoven.

Finally I would like to thank Universidad de la Repûblica (Montevideo, Uruguay) and Technische Universiteit Eindhoven (The Netherlands) for providing the material means to finish my studies.

The work in this thesis has been cm·ried out under the auspices of the research school IPA (Institute for Programming research and Algorithmics).

Contents

1 Introduetion 1.1 Lambda Calculus . . . . . . 1.2 Lambda Calculi with Types

1.2.1 Pure Type Systems . 1.2.2 Definitions . . . 1.2.3 Normalisation . . . . 1.2.4 Type lnference ...

1.3 Summary of the Contents of this Thesis

I An Abstract Presentation of Rewriting and Typing

2 Abstract Rewriting Systems 2.1 Introduetion ........ . 2.2 Abstract Rewriting Systems 2.3 Morphisms . . . . . . . . . . 2.4 Properties of Abstract Rewriting Systems. 2.5 Strategies . . . . . . . . . . . . 2.6 Criteria . . . . . . . . . . . . . 2. 7 Conclusions and Related Work .

3 Topology 3.1 Introduetion . 3.2 Topology . , . 3.3 Equivalence 3.4 Topological Characterisations 3.5 Conclusions a.nd Related Work .

4 Abstract Typing Systems 4.1 Introduetion ................ . 4.2 Abstract Typing Systems ......... . 4.3 Abstract Rewriting Systems with Typing 4.4 Environments . . . . . . . . . . .

1 1 3 5 6 8 8 9

13

15 15 15 16 18 19 21 24

27 27 28 30 32 35

37 37 38 39 43

11

4.5 Semantics . . . . . . . . . . . . 4.6 Conclusions and Related Work .

11 Lambda Calculus

5 Strongly Normalising À-terms 5.1 Introduetion ... 5.2 Lambda Calculus . . . . . . . 5.3 The set SJV . . . . . . . . . . 5.4 Conclusions and Related work

6 Perpetual Strategies 6.1 Introduetion ........ . 6.2 The Strategies Fbk and Gbk .

6.3 The Strategies Foo and Goo . 6.4 Maximal Strategies . . . . . 6.5 Conclusions and Related Work .

7 Developments and Superdevelopments 7.1 Introduetion .............. . 7.2 Developments . . . . . . . . . . . . . . 7.3 First Proof of Finiteness of Developments . 7.4 Second Proof of Finiteness of Developments 7.5 Superdevelopments ............. . 7.6 First Proof of Finiteness of Superdevelopments . 7.7 Second Proof of Finiteness of Superdevelopments 7.8 Condusions and Related Work .......... .

8 Simply Typed Lambda Calculus 8.1 Introduetion . . . . . . . 8.2 Simply Typed À-calculus . . . . 8.3 Strong Normalisation ..... . 8.4 Conclusions and Related Work .

lil Pure Type Systems with Definitions

9 Pure Type Systems 9.1 Introduetion ... 9.2 Specifications . . 9.3 Pure Type Systems 9.4 Conclusions and Related Work .

CONTENTS

46 47

49

51 51 51 53 55

57 57 57 58 59 64

65 65 65 67 69 69 72 76 77

79 79 79 80 82

85

87 87 87 89 96

CONTENTS

10 Type Inference for Pure Type Systems 10.1 Introduetion ............... . 10.2 Pure Type Systems without the 11-condition 10.3 Basic Properties . . . . . . . . 10.4 Description of Toptypes .......... . 10.5 Normalisation for ,8-reduction ....... . 10.6 Weak and Strong Normalisation for ,8,-reduction . 10.7 Syntax Directed Rules .... . 10.8 Type lnference ........ . 10.9 Conclusions and Related Work.

11 Pure Type Systems with Definitions 11.1 Introduetion ............. . 11.2 Pure Type Systems with Definitions .

11.2.1 Pseudoterms. 11.2.2 Reductions ... . 11.2.3 Types ...... .

11.3 Properties of Pseudoterms 11.3.1 Basic Properties .. 11.3.2 Confluence for ,8, 8 and ;38-reductions . 11.3.3 Weak and Strong Normalisation for -+s .

11.4 Properties of Well-Typed Terms . 11.4.1 Basic Properties ............. . 11.4.2 Strengthening .............. . 11.4.3 Weak a.nd Strong Normalisa.tion for f'S-reduetion

11.5 Conclusions and Related Work .............. .

12 Type Inference for Definitions 12.1 Introduetion . . . . . . . . . . 12.2 The 8-start and S-weakening rules .. 12.3 Syntax Directed Rules for Definitioris 12.4 Type lnference for Definitions . 12.5 Conclusions and Related Work .

13 Conclusions 13.1 Abstract Presentation of Rewriting and Typing 13.2 Lambda Calculus . . . . . . . . . . ; 13.3 Pure Type Systems with Definitions .

13.3.1 Normalisation ........ . 13.3.2 Type Inferertce . . . . . . . . 13.3.3 Normalisation versus Type Inference

lil

97 97 98 99

102 110 115 118 121 131

135 135 136 136 138 140 143 143 145 149 153 153 158 161 167

171 171 171 172 176 182

185 185 185 186 186 188 189

Chapter 1

Introduetion

In this thesis we consider typed and untyped lambda calculi. In this introduction, firstly, we give an informal explanation of the untyped lambda calculus, of the concept of type and an overview of the lambda calculi with types. Finally, we summarise the contents of this thesis.

1.1 Lambda Calculus

The lambda calculus is a formal system basedon a function notation invented by A. Church [Chu4J.]. It captures the most basicaspectsof the manners in which operators are combined to form other operators.

We give some motivations for the syntax of the À-calculus. In mathematics, a function that given x produces x 2 is written as follows.

This notation is notadequate when higher-order functions are involved (functions which admit other functions as arguments). Church used a notation involving the special symbol À to construct functions in a systematic way. In Church's notation the example above is written as Àx.x2

•

The À-terms are formed with two constructors, namely the application ( ) and the abstraction À. We write (! a) to express that a function f is applied to the argument a. Let b(x) be an expression containing x. A function that assigns the value b(a) to the argument a is denoted by (Àx. b(x)). This justifies the definition of arelation on the set of À-terms called {3-reduction.

((h. b(x)) a) -+[J b(a) {3-reduction

In the example, we have that ((Àx.x 2) 3) {3-reduces to 32 • We usually omit a pair of

parenthesis when this does not cause confusion, for example we write (Àx.x 2) 3 for the

term above.

1

2 CHAPTER 1. INTRODUCTION

The equivalence relation generated by the ,8-reduction is called ,8-conversion and is denoted by *'"'*f3 and the transitive dosure of --"p is denoted by ~p.

An important property of ,8-reduction is confluence. Since a term may contain several subterros of the form (.h. b(x)) a ( called redexes), the ,8-reduction is not functional. This is not an obstacle for the theory of the lambda calculus due to the fact that it has the property of confluence. Confluence means that if we have the solid arrows of the diagram then we also have the dotted ones.

. ...-{3 ,a··..... /'' ' I' d

A term that can not he reduced any further is said to he a normal form ( or to be in normal form). For example (Àx.x) y --"f3 y and y is in normal form.

An important property of À-terms is normalisation. Normalisation and termination are synonymous. In À-calculus, termination of the reduction is not guàranteed, e.g. n (>.x.(x x)) (Àx.(x x)) reduces to itself. A term is said to he weakly normalising ( or normalising) if there is a reduction sequence that ends in a normal form. For example, the term ( Àx.x) y is weakly normalising. There may he À-terms that are normalising but they have some rednetion sequence that is not finite, e.g. the term (Àx.y)O reduces toy if we contract the leftroost redex and to itself if we contract n. A term is said to be strongly normalising if any reduction sequence starting from the term terminates. Clearly, (Àx.y)O is weakly normalising but not strongly normalising.

The trace of a redex can be foliowed by marking the corresponding abstraction. In the term (Àx.(x x)) ((_dx.x) y), we have marked the second redex that we want to trace. When this term is reduced to ((.dx.x) y)((_dx.x) y), two redexes are created which are called residuals of the initia! marked redex. The ,8-reduction restricted to the marked redexes is called ,8-reduction. Developments are reduction sequences where only residuals of redexes that a;ë present in the initial term are contracted. In other words, a development is a ~-rewrite sequence. For example, a development is the rewrite sequence

(_dx.(x x)) ((_dx.x) y) --"[!_ ((_dx.x) y) ((_dx.x) y)

--"f!.. y ((_dx.x) y)

--"f!.. (y y).

An important classica! result in the lambda calculus is that all developments are finite (finiteness of developments) [Bar85]. To prove that the developments are finite is to prove

1.2. LAMBDA CALCULI WITH TYPES 3

that the ~-reduction, the .8-reduction restricted to the marked redexes, is strongly normalising.

Even though the confiuence property of the lambda calculus guarantees that the system is 'functional', it is important from the practical point of view to find adequate reduction strategies, i.e. functions that determine 'a way of reducing'. A strategy is called normalising if it finds the normal form of a weakly normalising À-term. For example, the strategy that reduces only the leftmost redex of a >.-term is normalising. The importance. of the existence of normalising strategies is that it implies the decidability of .8-conversion restricted to the weakly normalising terms. In order to check if two weakly normalising terms are convertible, we compute their normal forms by applying the normalising strategy and we check whether the two normal forms are syntactically equal. Due to the fact that there exists a normalising strategy for the lambda calculus, .8-conversion restricted to the .8-weakly normalising À-terms is decidable.

We also consider perpetual strategies, i.e. strategies that preserve the property of nontermination. In the term ( >.x.y )0, a perpetual strategy has to reduce n and not the leftmost redex. To have a perpetual strategy is important because a term is strongly normalising if and only if the perpetual strategy yields a finite reduction sequence.

In this thesis, we present new proofs of finiteness of developments and superdevelopments (a generalisation of developments) and of the fact that some strategies are perpetual.

1.2 Lambda Calculi with Types

Types were introduced for the first time in the combinatory logic (a variant of the lambda calculus) in [Cur34] and in the lambda calculus itself in [Chu40]. Nowadays types are used for many purposes as wil! be explained later. In order to give a first insight of the notion of type, we give one simple motivation related to set theory. The >.-terms do not represent the mathematica! set-theoretic notion of function, with domain and range as part of the definition of the function. However, they can be modified to fit this notion by adding the notion of type.

We want to say that in the term Àx.x2 , the variabie x ranges over the set of natura! numbers. This could be expressed by consiclering a special symbol Nat which could be interpreted in set theory as the set of natura! numbers. The >.-term is now written as Àx: Nat.x2 • We read x: Nat as x has type Nat and the intended meaning of x: Nat is that the variabie x ranges over the set of natura! numbers.

We need another symbol -+ to express that this is a function from the natura! numbers into the natura! numbers.

Àx: Nat.x2: Nat-+ Nat

This is read as follows: the term >.x : Nat.x2 has type Nat -+ Nat and its intended meaning is that >.x : Nat.x2 is a function from thesetof natura! numbers into itself.

The types Nat and Nat -+ Nat should he syntactic expressions de:fined in a forma! language with the notion of set as a possible interpretation for them. The lambda calculus with this kind of types is called simply typed lambda calculus ( or À_.).

4 GHAPTER 1. INTRODUGTION

The two origina.l papers by Curry and Church introduced two ·different families of lambda calculi with types. In the systems à la Church, the variabie of the abstraction contains the information of its type like in the example .\x : Nat.x2

• The termsof these systems usually have the property of uniqueness of types sirree the types of the variables determine the type of the whole term.

In the systems à la Curry, the types for the variables of the abstractions are not declared and the types assigned to the À-terms are not unique. The term Àx.x may have the type Nat --+ Nat but also may have the type a --+ a for an arbitrary type a.

Types may become more complex if we want to enrich the expressiveness of our language. The identity function Àx:Nat.x is 'the same function' as Àx:Bool.x except for the type. The language of types may be extended to express that the identity function may he applied to any type. The language of terms is extended with a new abstraction A and a quantifier on types V. The identity is written as

Aa.Àx:a. x

The type of the identity is Va.( a-+ a)

The fact that Aa.Àx:a. x has type Va.(a-+ a) means that for any type a, the function À x : a. x has type (a --+ a) The abstraction A and the quantifier V are applied to type variables, i.e. a variabie that can he substituted by types. In this example, the type variabie a can be substituted by types like Nat and Bool.

(Aa.Àx:a. x)Nat -+p Àx:Nat.x

A function constructed with A is called polymorphic, i.e. the argument of the function is a type. For example, (Aa.Àx:a. x) is the polymorphic iden:tity.

The extension of the simply typed lambda calculus with polymorphism is called polymorphic typed lambda calculs (F or À2 or second order typed lambda calculus) and it was introduced independently by [Gir72] and [Rey74].

Implementations of Lambda calculi

The À-calculus is the foundation of functional programming languages. The À-calculus itself could be considered as an abstract programming language. It contains the concept of computation in full generality and strength but in a pure form with a very simple syntax. The first typed programming languages developed to avoid typing errors at compile-time, were ALGOL-60 and PASCAL where variables have to be declared in the programs (typing à la Church). More sophisticated typed languages appeared later, like ML [HMM86], Miranda {Tur85] and Haskell [HW88). The last mentioned languages are functional programming languages based on fragments of F à la Curry. In this approach, types are introduced in the programming language to ensure correctnessof programs. Types are a way of classifying the objects to use them in a correct way. Hence the type of a program

1.2. LAMBDA CALCULI WITH TYPES 5

gives a partial specification of the program. Terms are viewed as programs and types as specifications for the programs. Another approach to the notion of type is the so-called propositions-as-types interpretation [Bru70] [How80]. A type is viewed as a proposition and a term as its proof. The first systems of proof checking (type checking) based on this interpretation of propositions-as-types and proofs-as-terms were the systems of AUTOMATH [NGdV94]. Modern systems that also provide computer-assistance for the construction of proofs are Goq [Dow91], Lego [LP92], Gonstructor [HA91], Nuprl [Gon86] and Alf [Mag94]. Coq is based on the calculus of constructions [CH88] extended with inductive definitions [GP90]. Lego is a proof assistant for the extended calculus of constructions [Luo90] with inductive types. Gonstructor is a partly automated proof assistant for pure type systems. The first version of Alf [AGN90] is based on Martin-Löf's type theory [NPS90] and the actual version [Mag94] is based on the monomorphic type theory with explicit substitution [Tas93].

1.2.1 Pure Type Systems

In this thesis we work with pure type systems. They provide a framework to descri he a large dass of type systems à la Ghurch in a uniform way. They were introduced independently by S. Berardi [Ber88] (see also [Ber90]) and J. Terlouw [Ter89]. Many systems ca.n he described in this way, for instanee the simply and the polymorphic typed lambda calculus, the systems of the AUTOMATH family [NGdV94], the Calculus of Gonstructions (and all the systems of the À-cube [Bar92]) and the inconsistent system À* [Gir72].

They are called 'pure' because there is only one type constructor and only one rednetion rule, namely the type constructor I1 and the ,8-reduction.

If A is a type and B(x) is a family of types indexed overAthen 11x:A. B(x) is also a type. The term Àx:A. b(x) has type 11x:A. B(x) if for every a of type A, the term b(a) ha.s type B(a). .

In case the expression B(x) does notdepend on x, Ilx:A. Bis equivalent to the ordinary function type A -+ B.

In pure type systems we have only one rule for all possible 'functions types'. The typing rule for a. product depends on some parameters. By instantiating the parameters, we obtain different product rules that allow to have different kinds of functions. For example the abstractions A and À of the polymorphic typed lambda calculus are replaced by the unique abstraction À of pure type systems and the type constructars -+ and V are replaced by the unique type constructor 11. Since we have the same symbol for the abstraction of type variables and of term variables, we have to distinguish them in other way. When we write a: : *, we mean that a: is a type variable. The polymorphic identity is written in a pure type system style using the abstraction À instea.d of A and writing a: : * for a:.

The type of the polymorphic identity is written using the product 11 insteadof the quantif1er

6 GRAPTER 1. INTRODUCTION

V and writing a : * for a.

IIa:*.(IIx:a.a)

One instantiation of the product rule allows to type the product (IIx:a.a) that corresponds to a -+ a in the polymorphic typed lambda calculus. Another instantiation of the same rule allows to type IIa: *.(IIx: a.a) that corresponds to Va.( a-t a) in the polymorphic typed lambda calculus.

1.2.2 Definitions

Any programming language provides a mechanism to introduce definitions, i.e. an abbreviation or name for a larger term that can be used several times in a program. Definitions are usually considered in a meta-leveland notaspart of the language of the lambda calculus.

In the systems of the AUTOMATH family [NGdV94] definitions are considered as part of the formallanguage. The meta-theory of these systems is treated in detail in [Daa80]. Howewer, some of the proofs apply only to the particular type system that they consider and do not extend to other type systems.

We consider it important to include definitions in the syntax of the lambda calculus and to study the properties that are preserved by the extension. This study can he clone in a very general manner if we use the framework of pure type systems.

We suppose that we have a context or environment where wecan introduce definitions. The definitions of the context are called global definitions and they can be used anywhere in the program or term. A global definition is written as x= a: A. This means that the name x is an ahbreviation for the term a whose type is A. There are definitions that have a restricted scope and they are called local definitions. A local definition is written as x=a:A in b. This means that x is an abbreviation for the term a whose type is A but it can he used only inside the term b. In our opinion it is important for practical use to have hoth ways of introducing definitions: global and local.

The intended mearüng of a definition x=a:A is that the definiendum x can be unfolded by the definiens a in its scope, either globally or locally. The unfolding of a definition is not the substitution of all the occurrences of the definition at once like for the ,8-reduction. Instead one occurrence of the definition is unfolded at a time. The reduction that performs the unfolding of definitions is called fi-reduction.

The equality x = a can be used not only in the evaluation of a term but also in the typing of a term. We can use the fact that the definiendum and the definiens are equal in order to type terms that could not be typed otherwise. Suppose that the definition x=a:* is a type and x occurs in another type B of the expression b, the typing of b may depend on the fact that we can use that x is equal to a.

A definition x=a:A in b has a similar behaviour as (Àx:A. b)a. However the two facts mentioned above suggest that they are in fact different, both from the point of view of the evaluation and the typing.

We give an example in À2 that shows the importance and usage of definitions in the

1.2. LAMBDA CALCULI WITH TYPES 7

evaluation and in the typing. First we introduce the global definition 'Bool' as follows.

Bool = Ila : *·a --+ a --+ a : *

The elements of Bool are true and false and they are given by the following global definitions.

true = .Àa:* . .Àx:a . .Ày:a. x : Bool

false .Àa:* . .Àx:a . .Ày:a. y : Bool

Observe that in a definition we can make use of the definitions introduced previously, in the definitions of true and false, we make use of Bool that was defined before.

The if-then-else is also introduced as a global definition.

if-then-else = .Àa:* . .Àb:Bool. .Àx:a . .Ày:a. (ba x y)

: (Ila:*. Bool--+ a--+ a--+ a)

The function that yields the negation of a booleari is abbreviated by the global definition not.

not = .Àb: Bool. (if-then-else Bool b false true): Bool-t Bool

The term (not true) evaluates to false and the term (not false) evaluates to true. · In the evaluation of (not true) it is convenient to unfold one occurrence of true and

to leave the other without being unfolded. In the following /38-rewrite sequence, we first unfold the definition of not, then we perform one /3-reduction step, we unfold the definition of if-then-else and perform some /3-reduction steps.

(not true) --+s

( ( .Àb : Bool. if-then-else Bool b false true) true) --+ f3

(if-then-else Bool true false true) --+s

((.Àa:* . .Àb:Bool. .Àx:a . .Ày:a. (ba x y)) Bool true false true) -(3

(true Bool false true)

In order to get the result, the first occurrence of true in (true Bool false true) should be unfolded but the second occurrence need not be unfolded. This shows the convenience of unfolding one occurrence of the definition at a time.

The terms defined above are all typable because we can use the fact that the definiendum and the definiens are equal.

For example, true would not have type Bool if we were not allowed to use the fact that the defiendum Bool is equal to its definiens Ila: *.(a--+ a--+ a). The type of true is Ila:*.a --+ a -t a and by using the fact that Bool is equal to Ila : *·a --+ a --+ a, we deduce that true has type Bool.

The if-then-else would not be typable at all if we could not use the fact that the definiendum Bool is equal to its definiens Ila:*.(a--+ a--+ a). In the definition of ifthen-else, the type of the argument b is Bool. Sirree b is the operator of an application,

8 CHAPTER 1. INTRODUCTION

the type of b should be a. function type a.nd the symbol Bool is fa.r from being a. function type unless we ca.n unfold its definition.

The typing rules for pure type systems a.re extended to include definitions. The rules have to be extended in such a way that the fa.ct tha.t the definiendum and the definiens are equal can be used in type deriva.tion. This is a.chieved by a.dding the 6-conversion rule. In this thesis we study the beha.viour of the 6-reduction a.nd the combination of the 6 with the ,8-reduction in pure type systems with definitions. The extension is clone in such a wa.y that almost all the properties tha.t are va.lid for a. pure type system are a.lso valid for its extension with definitions.

1.2.3 Normalisation

We a.re interested in the property of normalisa.tion in type systems. If all the terms that are typa.ble in the type system are (strongly) normalising then the system itself is sa.id to be ( strongly) normalising .

Normalisa.tion is important due to the fa.ct that if the type system is norma.lising the conversion when restricted to the typa.ble terms is decidable. If we want to check if two typable terms are convertible, we apply a reduction strategy to find their normal forms and check if the normal forms are syntactically equa.l. The opera. ti on tha.t computes the normal form ca.n be exponentia.l (or even worse!) a.nd so in practical situations it is advisa.ble to find a good stra.tegy for computing a. common-reduct of two terms (not necessarily the normal form) in a. reasonable time.

Strong norma.lisa.tion is a.lso important. The normalising stra.tegies in lambda calculus, e.g. the leftmost reduction, are not a.lwa.ys eflicient. They ma.y even take longer pa.ths to the normal forms tha.n other strategies. We would like to have the freedom to choose the strategy to compute the normal form (or some pa.rtial va.lue). In this case, to ensure termina.tion we have to prove tha.t a.ny stra.tegy is norma.lising, i.e. strong norma.lisation.

Examples of pure type systems that are strongly norma.lising are the simply typed lambda calculus [Tai67] [Tro73], the systems of the AUTOMATH fa.mily [NGdV94], the polymorphic typed lambda calculus [Gir72], the Calculus of Constructions [GN91] (and all the systems of the À-cube) and the 'pure' part of the extended calculus of constructions [Luo90]. flowever the pure type systems that are inconsistent like À* are not weakly norma.lising [Gir72]. We do not know any pure type system that is weakly norma.lising but not strongly normalising.

In this thesis we consider two extensions of pure type systems ( one that is obtained by weakening the abstraction rule and the other by ad ding definitions) and prove that normalisation is preserved by these extensions. We a.lso prove that the 6-reduction, the unfolding of definitions, is strongly norma.lising.

1.2.4 Type Inference

Look at the following problems rela.ted to type systems.

1.3. SUMMARY OF THE CONTENTS OF THIS THESIS 9

1. Type checking. Given a and b, does a have type b?

2. Type inference. Given a, does a have any type?

3. Inhabitation. Given b, is b the type of some term?

They are important in the implementations of functional programming languages, proof checkers and proof assistants.

Type inference for systems à la Curry is not always decidable, not even for normalising ones. For example, type inference for the simply typed lambda calculus is decidabie [Cur69] [Mil78] but for Àw (or Fw) is undecidable [Urz93b]. An incomplete overview of the decidability of type inference in the systems à la Curry can be found in a table on page 183 in [Bar92]. This table can be filled in completely by now: type checking and inference in ,\2 ( or F) have been proved undecidable in [Wel94] and the inhabitation of ,\n has been proved undecidable in [Urz93a]. Type inference for·ML [DM82] is decidabie because polymorphism can be used only in a weaker form (the universal quantifiers can occur only in the outermost position of a type).

For systems à la Church, in all known cases the problems of type checking and type inference are equivalent. Moreover decidability of type inference is very closely related to normalisation. Decidability of type inference for normalising pure type systems whose set of sorts is finite is proved in [BJ93] and decidability for normalising pure type systems whose specification is recursive and singly sorted ( also semi-full) is proved in [Pol96]. U ndecidability of type inference for inconsistent impredicative pure type systems is proved in [CH94] generalising the result in [MR86].

The problem of inhabitation in systems à la Church presents the same complications as for systems à la Curry. For the inconsistent systems, inhabitation is trivial since all the types have at least one inhabitant. In the systems of the À-cube, inhabitation is decidabie only for the simply typed lambda calculus(,\_,) and for À~. Fortherest of the systems in the À-cube inhabitation is undecidable [Spr95].

In this thesis, we give solutions for the problem of type inference in pure type systems (also with definitions). We present a type inference procedure (it is not an algorithm, i.e. a program that always terminates) for pure type systems. It can be applied to any singly sorted pure type system (systems with the uniqueness of types property), including the non-normalising ones. In order to prove that it behaves correctly (it terminates and yields the type of a term if the term is typable and it may not terminate otherwise), we use the results proved in this same thesis concerning normalisation.

1.3 Summary of the Contents of this Thesis

This thesis is divided into three parts: 1) an abstract presentation of rewriting and typing, 2) lambda calculus and 3) pure type systems with definitions.

10 GRAPTER 1. INTRODUCTION

An abstract presentation of rewriting and typing

In this part, the concepts of computation and typing are formalised in an abstract way as binary relations on an arbitrary set. The word abstract is due to the fact that we do not fix a set of terms or expressions, instead we take an arbitrary set. In this way, we give a common framework for the systems presented in the rest of the thesis. We believe that this abstract presentation gives uniformity and darity to the exposition.

In chapter 2, we start by recalling the notion of abstract rewriting systems. The rewrite relation intends to model the concept of computation. This setting has been used to capture some properties of the concept of computation like confluence and normalisation in an abstract way.

In chapter 3, with the intention to clarify the basic abstract properties of confluence and normalisation we compare abstract rewriting systems with topological structures and find the relationship of the mentioned properties with well known topological concepts.

In chapter 4, as one of the main purposes of the thesis is to study the behaviour of confluence and normalisation under different kinds of extensions of pure type systems, we introduce the notion of abstract rewriting systems with typing which intends to model the interaction between the concepts of computation and typing.

Chapters 2 and 4 are partly joined work with Femke van Raamsdonk. We were both interested in an abstract notion for type systems in order to have a common framework for pure type systems and for higher order rewriting systems. For the use of these abstract notionsin the context of higher orderrewriting systems, we refer to [Raa96].

Chapter 3 is basedon a paper with Walter Ferrer [FS93].

Lambda Calculus

In this part, we give somé new characterisations of the set of weakly and strongly normalising .À-terms focusing on expansion rather than reduction. These characterisations of the set of strongly normalising .À-terms permit us to give new and simple proofs of classical results about À-calculus. In most cases the ·new proofs are essentially simpler than the already existing ones and help us to understand not only the mechanics of the proofs of the results but also the reasons for their validity.

In chapter 5, we give new characterisations of the set of weakly and strongly normalising .À-terms.

In chapter 6, wedefine two perpetual stra.tegies Gbk and Goo similar to Fbk [BK82] and F= [BBKV76]. In order to prove that a strategy F is perpetual, we prove tha.t if F(M) is strongly normalising then so is M and we use one of the characterisations of the set of strongly normalising >.-terms given before. We also prove that the strategies Gbk and Fbk

are maximal, i.e. the length of the Gbk and Fb"-reduction sequences are maxima!. In chapter 7, we give two proofs of finiteness of developments and superdevelopments.

Wedefine the ,8-reduction as the ,8-reduction restricted to marked red exes. In order to prove finiteness of d~velopments we have to prove that the ,8-reduction is strongly normalising. In the first proof we define by induction a set that ~incides with the set of ~-strongly

1.3. SUMMARY OF THE CONTENTS OF THIS THESIS 11

normalising terms, then we prove that this set is equal to the set of ,i-terms. In other words, we prove that all the ,i-terms are ,8-strongly normalising. In the second proof we write a fundion from the set of ,i-terms ~ the set of strongly normalising >.-terms that preserves the reduction.

In chapter 8, we prove strong normalisation for the simply typed lambda calculus. We use again the new characterisation of the strongly normalising >.-terms.

This part is based on a paper with Femke van Raamsdonk [RS95].

Pure Type Systems with Definitions

In this part, we study the meta-theory of pure type systems with definitions in detail. We also give semi-algorithms of type inference for singly sorted pure type systems with and without definitions. A semi-algorithm of type inference is a program that terminates and infers the type if the term is typable, otherwise it may not terminate.

In chapter 9 we reeall the notion of pure type systems. First we look at the the notion of specification ( the parameters of the typing rules for pure type systems.) We define the notion of morphism between specifications. Then we reeall the typing rules for pure type systems. Pure type systems are obtained from the typing rules by instantianting the specification. We give some examples of specifications, and show which lambda calculi with types correspond to the typing rules of pure type systems instantiated with these specifications.

In chapter 10 we define a fundion that infers the type for singly sorted pure type systems (systems with the uniqueness of types property). We weaken the rules of pure type systems by removing a premise from the abstraction rule. This premise, called the 11-condition, states that (11x:A. B) should be well-typed in order to be able to give the type (11x:A. B) to the abstraction >.x:A. b. We study the metatheory of the pure type systems without the 11-condition. We prove that if a singly sorted pure type system is normalising then so is the corresponding pure type system without the 11-condition. Using this result we define a set of rules for pure type systems that are syntax directed, i.e. the last rule in a type derivation is determined by the sha.pe of the term and the context. We prove the equivalence between the syntax directed set of rules a.nd the original ones. Finally, we define a function that infers the type in a singly sorted pure type system based on this syntax directed set of rules. This chapter is basedon the paper [Sev96].

In chapter 11, we study the metatheory for pure type systems extended with definitions. We prove properties like confluence and subject reduction for the combination of the ,8 and 8-reduction. We prove strong normalisation for the 8-reduction and define a perpetual a.nd maximal strategy for the 8-reduction similar to Fbk for ,8-reduction. Also, we prove that weak normalisation is preserved by the extension, i.e. if a pure type system is weakly normalising then so is its extension with definitions. Moreover, we prove for certain pure type systems, including the Calculus of Constructions, that strong normalisation is preserved by the extension. This chapter is basedon a paper with Erik Poll [SP93, SP94].

In chapter 12, we define a function that infers the type of a term in 1a singly sorted pure type system with definitions. Similarly to pure type systems, we define a set of rules

12 CHAPTER 1. INTRODUCTION

for pure type systems with definitions that is syntax directed and prove the equivalence between the syntax directed set of rules and the original ones. We define a function that infers the type in a singly sorted pure type system with definitions based on this syntax directed set of rules.

Part I

An Abstract Presentation of Rewriting and Typing

13

Chapter 2

Abstract Rewriting Systems

2.1 Introduetion

The concept of computation can be modelled in an abstract way as a binary relation on a set. This relation is called the rewrite relation, usually denoted by -+. A pair consisting of a set and a binary relation is called an abstract rewriting system. We are interested, for example, in computing the value of an element. A computation is represented by a

. sequence of elements a0 -+ a1 -+ a2 ••• -+ a,. whose last element an represents the value of the computation. Several properties can be stuclied in this abstract setting, like confluence, weak and strong normalisation. A final value of a computation can he represented by an element that cannot be reduced any further and is called normal form. If all the elements have a normal form then the system is said to be weakly normalising. The intuitive meaning of confiuence is that any procedure that computes the value of an element yields the same result, the intuitive meaning of strong normalisation is that any procedure is finite.

This setting allows us to give criteria to prove properties like confluence, weak and strong normalisation in a very general way.

We summarise the contentsof the sections of this chapter. Insection 2.2, we reeall the basic concepts concerning abstract rewriting systems. In section 2.3 we define the notions of morphism between abstract rewriting systems and of rewrite sequences. In section 2.4 we reeall the concepts of conftuence, weak and strong normalisation. We also give some general criteria to prove conftuence, weak and strong normalisation.

2.2 Abstract Rewriting Systems

In this section we reeall the definition of abstract rewriting system. The abstract notion of rewrite relation was first formalised by Newman (see [New42]) under the name of 'move' or indexed I-complex. They arealso called abstract rednetion systems in [Klo80]. We call them abstract rewriting systems as in [Oos94].

15

16 CHAPTER 2. ABSTRACT REWRITING SYSTEMS

Definition 2.2.1. An abstract rewriting system is a structure (A,-+} where A is a set of objects and-+ is a subset of A x A called a rewrite relation (or reduction).

Definition 2.2.2. We say that (a, b) E-'-+ is a rewrite step in the abstract rewriting system (A,-+). We write a-+ b insteadof (a, b) E-+.

The reflexive ciosure of -+ is denoted by -+=. The transitive dosure of -+ is denoted by -++. The tra.nsitive-reflexive dosure of-+ is written as-· The inverse relation of-+ is denoted as t-. The equivalence relation generated hy -+ is written as <Hl' and called conversion.

Example 2.2.3.

1. For each n E JN we define the abstract rewriting system,

In= ({i I 0:5 i :5 n},{(i,i+ 1) I 0:5 i :5 (n-1)}}

Diagrammatically,

2. I = (JN, { (i, i + 1) I i 2: 0}). Diagrammatically,

0-+1-+2-+ ...

2.3 Morphisms

In this section we introduce the notion of morphism for abstract rewriting systems (see also [Raa.96}). Wethink that the concept of morphism is the natura! one. It allows us to express several concepts like the notions of rewrite sequence and extension as morphisms.

A morphism should preserve the structure. For abstract rewriting systems a morphism is a function between sets that preserves the rewrite relation -+. These are the morphisms associated to the category of abstract rewriting systems. In case the function preserves other relations like -++, -, we ca.ll it refining and implementing morphism respectively. Morphisms that preserve conversion <Hl' are defined later.

Definition 2.3.1. Let (A, -+a) (B, -+p) he two abstract rewriting systems. A morphism from (A, -+a) to (B, -+p) is a function f : A -+ B such that for all a, a' E 'A if a -+" a' then f(a) -+p f(a').

2.3. MORPHISMS 17

The category whose objects are the abstract rewriting systems and the morphisms are as defined above is denoted as Ars.

We define different kinds of morphisms between abstract rewriting systems depending on the relation they preserve.

Definition 2.3.2. Let (A, -+a) and (B, -+p) be two abstract rewriting systems.

A refining morphism from (A, -+a) to (B, -+p) is a function f : A -+ B such that for all a, a' E A if a -+a a' then f(a) -+t f(a').

An implementing morphism from (A, -+a) to (B, -+p) is a function f : A -+ B such that for all a, a' E A if a -+a a' then f(a) -{3 f(a').

A forgetting morphism from (A, -+a) to (B, -+p) is a function f: A-+ B such that for all a, a' E A if a -+a a1 then f( a) f( a'). By =,we mean equality between elementsin a set.

Definition 2.3.3. Let (A, -+a) and (B, -+p) be two abstract rewriting systems.

We say that (B, -+p) is an extension of (A, -+a) if AC B and the inclusion mapping is a morphism from (A, -+a) to (B, -+p).

We say that (B, -+p) is a conservative extension of (A, -+a) if (B, -+p) is a.n extension of (A, -+a) and for all a, a' E A, if a -+pa' then a -+ 01 a'.

We say that (B, -+f3) is astrong conservative extension of (A, -+a) if (B, -+p) is a conservative extension of (A, -+a) and A is closed under -+p, i.e. if a E A and a -+p a' then a' E A.

A fini te computation is represented by a finite rewrite sequence of elements ao -+ a1 -+ a2 -+ ... an in an abstract rewriting system. We will formally define a finite rewrite sequence in an abstract rewriting system (A,-+) as a morphism from In to (A,-+).

A computation can also be infinite and it is represented by an infinite rewrite sequence a0 -+ a1 -+ . . .. We will formally define an infinite rewrite sequence in an abstract rewriting system (A, as a morphism from I to (A,-+).

Definition 2.3,4. Let (A,-+) he an abstract rewriting system, a E A and n E JN.

A rewrite sequence of length n starting at a is a triple (a, n, u) such that u is a morphism from Ir. to (A,-+) and u(O) =a. In a diagram:

0 -+ 1 -t 2

u(O) -+ u(l) -+ u(2) -+ u(n)

We denote a rewrite sequence (a,n,u) as u: u(O)-+ u(l)-+ ... -+ o-(n) or u: u(O)u(n).

18 GRAPTER 2. ABSTRACT REWRITING SYSTEMS

A rewrite sequence of infinite length starting at a is a pair (a, u) such that u is a morphism from I to (A, -7) and u(O) =a. In a diagram:

0 -7 1 ~ 2

u(O) ~ u(l) ~ u(2)

We denote an infinite rewrite sequence (a, u) as u : u( 0) -7 u( 1) ~ ....

. The lengthof u is denoted by !lul!. We have that Huil iseither a naturaJ number or oo. The domain of u is denoted hy dom( u). Note that a morphism f : A -7 B from (A, -7a) to (B, -713) 'preserves rewrite se

quences'. lf (a, n, u) is a finite rewrite sequence in (A,~.,) then (J( a), n, f o u) is a rewrite sequence in (B, -713). lf (a, u) is an infinite rewrite sequence in (A, -70 ) then (J(a), f o u) is an infinite rewrite sequence.

In the following definition, we introduce the notion of lifting which will he used to define the notion of development in chapter 7.

Definition 2.3.5. Let f : A~ B he a morphism from the abstract rewriting system (A, -7a) to (B, -713). A rewrite sequence u in (A, -7a) is an f-lifting of a rewrite sequence p if j 0 CT p.

The notion of lifting for rewrite sequence has been defined in [RS95] in the context of indexed abstract rewriting systems. Note that this is the categoricaJ notion of lifting for morphisms.

Definition 2.3.6. A rewrite sequence u : a '"""* b is maximal if for aJl p : a ._ b we have

llull 2: IIPII·

2.4 Properties of Abstract Rewriting Systems

In this section we define the basic properties of confluence, weak and strong normalisation in an abstract rewriting system.

An element that cannot he reduced any further is called normal form and it can he viewed as the final value of the computation. If all the elements have a computation that ends in a normal form then the system is said to he weakly normalising. Other important properties of abstract rewriting systems are confluence and strong normalisation.

Definition 2.4.1. Let (A, he an abstract rewriting system and a E A.

We say that a is confluent if for all b, c E A such that a - b and a ._ c, there exists an element d such that b ._ d and c - d.

We say that a is a normal form (or -7-normal form) if there is no b such that a -7 b.

2.5. STRATEGIES 19

We say that a has a normal form ( or has a -t-normal form) if there exists a normal form b such that a - b.

We say that a is weakly normalising ( or -t-weakly normalising) if a has a normal form.

We say that a is strongly normalising (or -t-strongly normalising) if there is no infinite rewrite sequence starting at a.

The notions of confluence, weak and strong normalisation can he extended to abstract rewriting systems.

Definition 2.4.2. Let (A, -+) he an abstract rewriting system.

We say that (A, -t) or -t is confluent if for all a E A, a is confluent.

We say that (A, -t) or -t is weakly normalising if for all a E A, a is -t-weakly normaJising.

We say that (A, -t) or -t is strongly normalising if for all a E A, a is -t~strongly normalising.

In the following we define the simple and transitive reductions graphs of an element which represent the set of values of all the computations starting from this element. We also define the simple and transitive expansion graphs of an element which represent the set of inputs whose computation yields the element.

Definition 2.4.3. Let (A, -t) he an abstract rewriting system and a E A. We define the following subsets of A.·

a) Q_,(a) = {b E A I a-t b} and Q_".(a) {b E A I a- b}. We call them the simple and the transitive reduction graphs of a.

b) &_,(a) {b E A I b -t a} and &-(a)= {b E A I b-a}. We call them the simple and the transitive expansion graphs of a.

Note that &_,(a)= Q,_(a) and that t:....,.(a) Q.,_(a).

Definition 2.4.4. Let (A, -t") and (B, -t13) he two abstract rewriting systems. The union of (A, -t..,) and (B, -t13) is defined by (A U B, -taU -tp). We write -tap insteadof -t", u -tp.

2.5 Strategies

In this section, we define the notion of strategy. A strategy is a procedure that determines the way we reduce an element.

Definition 2.5.1.

20 CHAPTER 2. ABSTRACT REWRITING SYSTEMS

1. A strategy for the rewrite relation -+ is a mapping f : A -+ A such that for all a E A a--* J(a).

2. A one-step strategy for the rewrite relation -+ is a mapping f : A -+ A such that for all a E A not in normal form, we have that a-+ f(a).

In chapter 6, we need to consider strategies that yield a set of reducts instead of only one. For that reason, we introduce the following definition.

Definition 2.5.2.

1. A non-deterministic strategy for the rewrite relation -+ is a mapping F: A-+ P(A) such that for all a E A, b E F(a) we have that a--* b.

2. A non-deterministic one-step strategy for the rewrite relation -+ is a mapping F : A-+ P(A) such that for all a E A not in normal form, we have that F(a) :f= 0 and for all b E F(a), a-+ b.

Por example, the function defined by F(a) == {a} for all a E A is a (trivial) nondeterministic strategy.

Let f be a one-step strategy. We define an f-rewrite sequence starting from a. Intuitively, an f-rewrite sequence is a sequence of the form

a-+ f(a)-+ Jl(a)-+ ...

possibly ending in the normal form of a.

Definition 2.5.3. Let F be a (non-deterministic) one-step strategy for -+. We say that a rewrite sequence u is an F-rewrite sequence if for all n E dom(u) such that n > 0, u(n) E F(u(n -1)).

Definition 2.5.4. A (non-deterministic) one-step strategy F is maximal if all the Frewrite sequence starting.are maximaL

Definition 2.5.5. A non-deterministic strategy F : A -+ P(A) is called normalising if for all a E A such that a is weakly normalising, there exists an F-rewrite sequence from a to a normal form.

The importance of the existence of normalising strategiesin weakly normalising abstract rewriting systems is that the decidability of the equality on normal forms implies the decidability of the conversion. In order to check if two elements are convertible, we compute their normal forms by applying the normalising strategy and we check that the two normal forms are equal.

Definition 2.5.6. A non-deterministic strategy F : A -+ 'P(A) is called perpetual if for all a E A such that a is not strongly normalising we have that all the elementsin F(a) are not strongly normalising.

2.6. CRITERIA 21

For a perpetual strategy F, if a is not strongly normalising then all the F-rewrite sequences starting at a are infinite.

Deflnition 2.5.7. A non-deterministic strategy F: A-+ P(A) is called confluent if for all a, b E A such that a - b implies that the F-rewrite sequences starting at a and b intersect.

If (A,-+) is confluent then the simple graph g_. is a confluent strategy.

Deflnition 2.5.8. A common-reduct strategy is a function F : A x A -+ P(A) if for all a, b E A such that a- b then for all c E F(a, b) we have that a- c and b- c.

The importance of the existence of a common-reduct strategy .F is that we can check if two elements are convertible in a confluent abstract rewriting system. In order to check if two elements a and b are convertible, we check if F( a, b) is not the empty set.

In the next lemma we prove that confluent strategies are partienlar cases of commonreduct strategies. This is evident since a confluent strategy has oq.ly one argument and a common-reduct strategy has two.

Lemma 2.5.9. If F ; A -+ P(A) is a confluent strategy then G : A x A -+ 'P(A) defined by G( a, b) = pn( a) n Fm( b) where n, m are the least natural numbers such that pn(a) n pm(b) =/: 0 is a common-reduct strategy.

An example of a common-reduct strategy is F : A x A -+ P( A) de:fined from the simple graph 9 ... by F(a, b) = Ç~(a) n 9:!(b) where n, mare the least natural numbers such that g~(a) n 9:!(b) =/: 0.

More examples of common-reduct strategies are given in chapters 10 and 12.

2. 6 Criteria

In this section we give some criteria to prove confinenee and normalisation that will he used in the following chapters of this thesis. They are expressed in terms of the notion of morphism. We study the manner in which the properties of confluence, weak and strong normalisation are transported from one abstract rewriting system (A, -+a) to the abstract rewriting system (B, .:....;3) depending on the class of morphism we can find from (A, -+0') to (B, -+;3).

Forgetting morphisms give rise to a criterion for confinenee provided they are strategies. This criterion is used in chapter 11 in the proof of confinenee for the ó-reduction.

Lemma 2.6.1. (Conftuence Criterion) Let (A, -+a) he a.n abstract rewriting system. If there is a forgetting morphism f : A -+ A which is a strategy for -+,., then --*<> is confluent.

Proof: The proof is illustra.ted by the following diagram.

22 CHAPTER 2. ABSTRACT REWRITING SYSTEMS

a

0

Implementing morphisms giverise to a criteria for confinenee provided they are strategies. This criterion is used in cha~ter 11 in the proof of confinenee for the ,88-reduction.

Lemma 2.6.2. (Confluence Criteria) Let (A, -+a) and (B, -+,e) be abstract rewriting systems and suppose that there are implementing morphisms f : A -t B and g : B -+ A such that f o g : B -t B is a strategy for -+,e. If -+.,. is confluent then so is -+p.

Proof: Suppose that b - 13 b1 and b -.e ~. Since gis an implementing morphism from (B, -+p) to (A, -+a) we have that g(b) -"' g(bt) and g(b) -,. g(b2). Since -+a is confluent we have that there exists a E A such that:

g(b)

;/~ g(bt).. ..~.(b2)

·. .· a··.. .··a ·... ,..·

" ~ a

Since f o g is a strategy for -+p and f is an implementing morphism from (A, -ta) to (B, -+p) we have the following picture:

. 2.6. CRITERIA 23

0

Strategies from one abstract rewriting system to a conservative extension give rise to a weak normalisation criterion.

Lemma 2.6.3. (Weak Normalisation Criterion) Let (B, -+p) be astrong conservative extension of the abstract rewriting system (A,--+.,.). Suppose there exists a strategy f : B--+ B for -+p such that /(B) CA. If -+.,. is weakly norma.lising then so is -+(3.

Proof: Suppose b E B. Then b -{3 f(b) a.nd f(b) E A. Since --+." is weakly normalising there exists an -+a-normal form c E A of /(b). Since --+aC-+(3 we have that f(b) -{3 c.

Since (B, -+p) is astrong conserva.tive extension of (A,--+.,.) and c E A is an --+ 01-normal form, we have that cis an -+(3-normal form. Suppose towards a contradiction that cis not an --+(3-normal form. Then there exists d such that c --+(3 d. Since A is closedunder -+p, we have that dE A. Since -+p n(A x A) C-+01 we have that c -+.,. d. This contradiets the fact that c is an -+01-norma.l form. 0

For proving preservation of weak norma.lisation from one abstract rewriting system (A, -+a) to another abstract rewriting system (B, -+p), the requirement that (B, -+p) is a strong conservative extension of (A, -+a) can he weakened. Assumewe can split the rewrite relation -+p into two relations -+p and -+s. We require that only (B, -+p) is a strong conservative extension of (A, -+a)· In this case we need -+s to be weakly normalising. This criterion is used in chapter 4 to prove a criterion that is applied in chapter 11 in the proof of weak normalisation for pure type systems with definitions.

Lemma 2.6.4. (Weak Normalisation Criterion) Let (B, -+f3) be astrong conservative extension of the abstract rewriting system (A, --+a) and let ( B,--+ 6) be a.n abstract rewriting system. Suppose the following conditions are verified.

a) The relation -+sis weakly normalising.

b) For all b E B such that b is a 6-norma.l form, we have that b E A.

c) If a is in 8-normal form and a -+a a' then a' is in ó-norma.l form.

If -+a is weakly normalising then so is -+ps.

Proof: Suppose b E B. By a) there exists d such that dis the ó-normal form of b. Then b -s d. It follows from b) that dE A. Since -+a is weakly norma.lising there exists c an -+01-normal form of d. Since -+aC-+p we have that d -{3 c. Since (B,-+p) is astrong conservative extension of (A, -+a), cis in --+(3-normal form. By c) we also have that cis in --+5-normal form. 0

Refining morphisms give rise to a criterion for strong normalisation since they map infinite rewrite sequences into infinite rewrite sequences. This criterion is used in chapter 7 in the second proof of finiteness of developments and superdevelopments.

24 GRAPTER 2. ABSTRACT REWRITING SYSTEMS

Lemma 2.6.5. (Strong Normalisation Criterion) Let (A,-+,.), (B, -+13 ) be two abstract rewriting systems a.nd suppose there is a refining morphism f : B -+ A.

If -+,. is strongly normalising then so is -+13.

In order to prove the preservation of strong normalisation from one abstract rewriting system (A, -+a) toanother abstract rewriting system (B, -+p), in the case it is not possible to find a refining morphism from (B, -+p) to (A, -+01), a method is to split the reduction -+p into two reductions -+13 and -+s and find a refining morphism from (B, -+13) to (A, -+a)· In the case that -+s is strongly norma.lising, we haveastrong normalisation criterion. This criterion is used to prove the criterion of chapter 4 that is used in the proof of strong norma.lisation for pure type systems with definitions in chapter 11.

Lemma 2.6.6. (Strong Normalisation Criterion) Let (A,-+,.), (B,-+136) he abstract rewriting systems. Suppose there is a mapping f : B -+ A such that:

a) fis an implementing morphism from (B, -+s) to (A,-+").

b) fis a refining morphism from (B,-+13) to (A,-+a)·

If -+a a.nd -+s are strongly normalising then -+135 is strongly normalising.

Proof: Suppose towards a contradiction that -+135 is not strongly normalising, i.e. there is an infinite -+13s-rewrite sequence starting at b E B.

Observe that the number of ,8-rewrite steps in this sequence is infinite, i.e. 'In E lN 3m > n : bm -+13 bm+t· Otherwise it would follow that there is no E IN such that Vm > n0 bm -+s bm+l· Herree the sequence bno+t -+s b110 +2 -+s ... would be infinite. As -+s is strongly normalising, this can not happen. Hence the number of ,8-rewrite steps in the sequence b -+135 b1 -+f36 b2 ... is infinite. Then this sequence is of the form

By hypothesis a) and b) there is an infinite a-rewrite sequence startingat f(b):

which contradiets the assumption that -+" is strongly normalising. D \

2. 7 Conclusions and Related Work

In this chapter, we have presented the notion of abstract rewriting system in a categorical way, by introducing different types of morphisms and we have proved some general Iemma.s concerning confluence and normalisa.tion.

In the litera.ture abstract rewriting systems are not usually presented in a categorical manner. We have introduced the notion of morphism and considered the category of abstract rewriting systems. In our opinion, the categorical presentation is more elegant. For

2. 7. CONCLUSIONS AND RELATED WORK 25

example the notion of rewrite sequence can he defined economically in terms of morphisms. The a.pplication of a function to a rewrite sequence is the composition of two morphisms. The notion of lifting of a development coincides in this wa.y with the categorical notion of lifting. ·

We have introduced the general notion of common-reduct strategy in order to use it later in the definitions of the type inference semi-a.lgorithms. A common-reduct strategy can he used to check conversion in confluent abstract rewriting systems, if F is a common-reduct strategy, the terms a and bare convertible if and only if F(a,b) =f:. 0.

The Church-Rosser strategy presented in [Bar85] is a particular case of a commonreduct strategy for .8-reduction. The main difference between a common-reduct strategy and a Church-Rosser strategy is that the former depends on two arguments and the latter on one. The normalising strategies are important for checking conversion but they ensure termination only on the set of weakly normalising elements.

The way in which conversion is checked in [Coq91] supposes implicitly the existence of a common-reduct strategy. In [Coq91], an algorithm for checking conversion is defined. For that purpose a computable binary relation is defined which is equivalent to the .811· reduction. This relation depending on two arguments can he considered as a commonreduct strategy. We define this strategy for f3 in chapter 10 and a similar one for .BS in cha.pter 12. Also in these chapters we give more examples of common-reduct strategies for f3 and (38-reduction.

The criteria to prove confluence, weak and strong norma.lisa.tion have been obta.ined generalising the original proofs done for pure type systems with definitions (see [SP94]). Many other criteria came up as generalisations of existing proofs (see [New42], [Klo90], [Oos94] and [GLM92]). The proofs of [SP94] are rewritten as applications of the new criteria in chapter 11. Moreover these criteria will also be applied to other particular cases.

26 CHAPTER 2. ABSTRACT REWRITING SYSTEMS

Chapter 3

Topology

3.1 Introduetion

In this chapter we dress many of the basic properties of abstract rewriting systems in a topological costume. It will not be formally used later and the readers. who are not interested can skip it.

We define a topology (A, T .... ) associated to an abstract rewriting system (A, --+). The dosed sets ofthis topology are those subsets of A that are dosed under the rewrite relation --+ and the open sets are those subsets of A that are dosed under the inverse relation +-.

The dosure operator applied to an element a E A is the transitive reduction gra.ph of the element, Q...,(a). This is the smallest dosed set that contains a. In the context of preorders, the topology associated to a preorder is eaJled the Alexandroff topology. This topology is very special in the sense that the open sets are dosed under arbitrary intersections (not only fini te ones) and there is another operator besides the dosure that yields the expansions of a set.

We give some topological characterisations of confluence. An abstract rewriting system (A,--+) is confluent if and only if the topology associated to (A,--+) verifies tha.t the intersection of any pair of closed sets tha.t are subsets of Q....,.(a) is non-empty or in informal language if the closed subsets of Q...,.(a) are 'large'. Moreover, we prove tha.t an abstract rewriting system (A, --+) is confluent if and only if the two operators in the topology associated to (A,--+) verify a subcommutation condition.

We also give charaêterisations of normal forms a.nd the properties of weak and strong normalisation for what we call irreflexive abstract rewriting systems.

A normal form in an irreflexive abstract rewriting system is a closed point in the topology associated to the abstract rewriting system.

An irreflexive abstract rewriting system is weakly normalising if and only if all the closed sets of the associated topology contain a closed point. Another characterisation states that an irreflexive abstract rewriting system is weakly normalising if and only if A is the result of a.pplying the expansion operator to the set of closed points.

An irreflexive abstract rewriting system is strongly normalising if and only if the fol-

27

28 CHAPTER 3. 'TOPOLOGY

lowing two conditions are verified:

1. The family of principal closed open sets is noetherian.

2. The dosure of two points is the same if and only if they are the same point.

This chapter is organised as follows. In section 3.2 we define the topology associated to an abstract rewriting system and the abstract rewriting system associated to a topology. In section 3.3, we show that there is an equivalence between the preorders and symmetrie topological spaces. In section 3.4, we give topological characterisations of confluence, weak and strong normalisation.

3.2 Topology

In this section we associate a topology T .... to an abstract rewriting system (A,---+). The dosed sets of this topology are the subsets of A that are closed under the rewrite relation ---+. The dosure operator is the extension of the transitive graph of an element of A to subsets of A. Conversely, we associate an abstract rewriting system to a topology. The rewrite relation is defined as follows: an element a rewrites to b if b is in the dosure of the element a. This rewrite relation is reflexive and transitive.

We reeall the definition and the basic properties of dosure operators on a set (see [Kel55]).

Definition 3.2.1. A map C : P(A)---+ P(A) is called a closure operatorifit verifies:

1. 0c 0,

2. xçxc,

3. Xe = (Xe)\

4. (X1 UX2t X1cUX2c for all X,X1,X2 cA.

Definition 3.2.2. IfC: P(A)---+ P(A) is a dosureoperator thenF {X ç A I x= xc} and T = {X Ç A I A- X E F} form the family of closed and open sets of a topology in A.

We associate a topology T_ to an abstract rewriting system (A,---+). The dosed sets of this topology are the subsets of A that are dosed under the rewrite relation -i-. The dosure operator is the extension of the transitive graph of an element of A to subsets of A.

Definition 3.2.3. Let (A,---+) be an abstract rewriting system. We define a mapping C: P(A)---+ P(A) defined for X Ç A by C(X) = {b E A I 3x EX x---+> b}.

3.2. TOPOLOGY 29

For each subset X of A, C(X) is thesetof all the elementsin A that are obtained by rewriting some element in X. It is easy to prove that this mapping is a dosure operator.

Lemma 3.2.4. Let (A,--+) he an abstract rewriting system. The operator C : P(A) --+ P(A) is a dosure operator.

We denote by :F.A ( or :F ..... ) and T.A (orT ..... ) the family of dosed and open sets with respect to the topology associated to an abstract rewriting system. This topology is sametimes called the Alexandroff topology associated to --+. It has been considered especially in the case in which --+ is a preorder on A, see [GLSH92].

In the case that X = {a} we write C( a) instead of C( {a}). The dosure of a point is the transitive reduction graph of the point and it is a dosed set. In other words, if a E A then C(a) ={x E A I a- x}= Ç_,.(a). Observe also that C(X) = Uaex C(a) = Uaex Ç_,.(a). Note also that a- b <=> b E C(a) <=> C(b) Ç C(a).

Also the transitive expansion graphof a point is an open set. In other words, t'-(a) = {x E A I x - a} is an open set. It is the smallest set that is open and contains the element a. Note also that b-a<=> b E t'_,.(a) <=> t'_,.(b) Ç t:_(a).

In the following lemma, we prove that the dosed sets are invariant under the rewrite relation and the open sets are invariant under the inverse relation.

Lemma 3.2.5.

1. A subset X Ç A is dosed if and only if it is invariant under the rewrite relation --+. In other words X is dosed if and only if x E X, x --+ y implies y E X for all x, y E A.

2. A subset X Ç A is open if and only if it is invariant under the rewrite relation +--.

In other words X is open if and only if x E X, y --+ x implies y E X for all x, y E A.

3. A subset of A is T ..... open if and only if it is T .... dosed, i.e. T ..... = :F .... and :F ..... = T .....

Definition 3.2.6.

The family of all sets of the form C(a) with a E A is called the family of principal closed sets and is denoted as 'P ..... Ç :F ......

The family of all sets of the form t:_(a) with a E A is called the family of principal open sets and is denoted as 0 ..... Ç T ......

Note that the family of open sets 0 ..... is a basis for the topology T ......

We associate to each abstract rewriting system (A, --+) the topological space (A, T_.) by means of a functor. It is easy to prove that if f: A--+ Bis a morphism from (A, -+a) to (B, --+p) then fis a continuous function from (A, T ..... "') into (B, T ..... ").

Definition 3.2.7. The functor 1i: Ars--+ Top is defined as follows:

1-l(A,--+) = (A, T ..... ) for CA,--+) an abstract rewriting system,

30 CHAPTER 3. TOPOLOGY

1i(g) = g for g a morphism between abstract rewriting systems.

We associate an abstract rewriting system to a topology. The rewrite rela.tion is defined as follows: an element a rewrites to b if b is in the dosure of the element a.

Definition 3.2.8. Let (A, T) he a topological space whose dosure operator is _c. We define a binary relation -+r on A as follows. ·

a -+r b if b E ac.

This bina.ry relation is the rewrite relation associated to T. The abstract rewriting system associated toT is (A, -+r).

Note that -+r is reflexive and tra.nsitive.

We associate toeach topological space (A, T) an abstract rewriting system (A, -+r) by means of a functor. It is easy to prove that if fis a continuons function from (A, T) into (B, T') then f: A-+ Bis a morphism from (A, -+r) to (B, -+r•).

Definition 3.2.9. Thè functor g : Top -+Ars is de:fined as follows:

Q(A, T) =(A, -+r) for (A, T) a topological space,

Q(f) = f for f: A-+ Ba continuons function of topological spaces.

3.3 Equivalence

The abstract rewriting systems whose rewrite relation verifies reflexivity and transitivity are called preorders and the topological spaces whose open sets are dosed under intersections are called symmetrie topological spaces. In thissection, we prove that there is an equiva.lence between the preorders and the symmetrie topological spaces.

We ca.n associate the reflexive-transitive dosure of an abstract rewriting system by mea.ns of a functor.

Definition 3.3.1. The functor transitive dosure for abstract rewriting systems is denoted a.s TC : Ars -+ Ars and defined as follows:

TC(A,-+) =(A,-) for (A,-+) an abstract rewriting system,

TC(f) = f for fa morphism between abstract rewriting systems.

Lemma 3.3.2. The functor TC verifies tha.t TC2 TC.

We define the notion of symmetrie topologica.l space.

Definition 3.3.3. Let (A, T) be a topology on A. We say that (A, T) is a symmetrie topology if the family of open sets is closed by intersections.

:}.3. EQUNALENCE 31

Definition 3.3.4. Let (A, T) be a topological space and 'Ió Ç 7 he a.n arbitrary subfa.mily of 7 and call Oro the set Oro = noero 0. We define the symmetrie topology associated to 7 ( denoted as T,.) as the topology whose basis is the set { Oro I 'Ió Ç 7}.

Note that T,. 2 7. The construction above can be expressedas a functor.

Definition 3.3.5. Define a functor R from the category of topological spaces into itself, R : Top -+ Top, as follows:

R(A, 7) = (A, T,.) for (A, T) a topological space,

R(f) = f for f a continuous function.

Lemma 3.3.6. The functor R verifies that R2 = R.

The symmetrie topology is very special in the sense that there is another operator that · yields the smallest open set that contains a given element. This set is just the transitive expansion graph e_(a).

Lemma 3.3. 7. The following statements are equivalent.

a) 7 is a symmetrie topology.

b) 7 is a topology and there exists another topology 7' in A , such that if :F' denotes the family of closed sets of 7', then :F' = 7.

c) 7 is a topology and the following condition is verified.

(M) For all a E A, there exists a unique set S( a) that is the smallest open set that contains the point a, i.e. S(a) Ç X for all X E 7 sueh that a EX.

Proof: To prove a) => c) we take a symmetrie topology (A, 7) and observe tha.t it verifies (M) beeause n U is the smallest open set containing a.

{UJaeuen Conversely, a topology (A, 7) tha.t verifies (M) is a symmetrie topology. Suppose tha.t

'Jó Ç 7 and X = nuE'To U f 0. Take a E X then for any U E 'Jó, S(a) Ç U and then S(a) Ç X. So that X UaexS(a} is an open set. 0

Lemma 3.3.8. Suppose that 7 is a topology on the set A and T,. the assoeiated symmetrie topology. For every point a E A we have that CT( a)= Cr,.(a).

Lemma 3.3.9. Let 7 he a symmetrie topology on the set A.

a) For any X Ç A there exists an open set S(X) that is the smallest open subset of A that eontains X. Moreover S(X) = Uaex S(a).

b) Let a, b E A. We have that b E ac #a E S(b) # S(a) Ç S(b).

c) The family of open sets S(a) with a E A forma basis for the topology 7.

32 GRAPTER 3. TOPOLOGY

Note that if (A, -t) is an abstract rewriting system, the topology T .... is a symmetrie topology.

In the following theorem we prove tha.t there is a.n equiva.lence between the preorders and the symmetrie topologies.

Theorem 3.3.10. Let Preord be the subeategory of Ars consisting of the preorders and Tops the subeategory of Top eonsisting of the topological spaces whose topology is a symmetrie topology. Let 'H, g, TC and 'R be the functors defined before.

a) The eomposition go 1{ satisfies go 1i =TC.

b) The composition 1i o g satisfies 1i o g 'R.

c) The functors 1i and gareinversesof each other when respeetively restricted to Preord and Tops.

d) The funetor 1{ : Preord -t Tops is an equivalence of eategories. lts inverse is the functor g : Tops -t Preord.

Proof: Parts a) and b) are easyto prove. Part c) follows immedia.tely from parts a), b) and the fact that 'R and TC are projection functors onto Tops and Preord respectively. Part d) follows immediately from the previous parts. 0

3.4 Topological Characterisations

In this section, we give topological characterisations of confluence, weak and strong normalisation.

In the following theorem, we give some topologieal eharacterisations of confluence. An abstract rewriting system (A, -t) is confluent if and only if in the topology associated to (A, -t ), the intersection of any pair of closed subsets of Ç_"( a) is non-empty. Moreover, we prove that an abstract rewriting system (A, -t) is confluent if and only if the two operators in the topology associated to (A, -t) verify some subcommutation condition.

Theorem 3.4.1. (Topological Characterisation of Confluence) The following statements are equivalent.

1. The abstract rewriting system (A, -t) is confluent.

2. For all a in A and for every pair C and Dof non empty T ..... closed subsets of C(a), a n D =f: 0.

3. For all X Ç A, C(S(X)) Ç S(C(X)).

Pro of:

3.4. TOPOLOGICAL CHARACTERISATIONS 33

(1 # 2). Suppose (A,---+) is confluent and C and D are as above. Take c E C and dE D. As c, d E C( a) we have that d - a - c. Th en there exists a.n x E A such that d - x - c. As d E D , C(d) Ç D and as d- x, x E C(d) Ç D. Similarly, x E C(c) Ç C. Hence xECnD.

Conversely, suppose that we have a, d, c E A such that d - a - c. Then C( d) Ç C( a) and C(c) Ç C(a). By hypothesis, there exists an element x E C(d) n C(c). That mea.ns that d - x - c and hence that (A, ---+) is confluent.

(1 # 3). Observe that

C(S(X))={z:3y,y-z & y-x & xEX}

S(C(X))={u:3v,u-v & x-v & xEX}

0

Next we give a topologica.l chara.cterisation of normal forms. The closed points in the topology associated to the abstract rewriting systems are either elements that rewrite to itself or normal forms.

First we reeall the notion of loop a.nd irreflexive abstract rewriting systems.

Definition 3.4.2. Let (A,---+) be an abstract rewriting system.

We say that a rewrite sequence starting at a is a loop if it is of the form a ---+ + a.

We sa.y that a. one-step loop is a loop of length 1.

For exa.mple, in the À-calculus < A, -+p> one-step loops are of the form C[n] ---+ C[n] with n (>.x.x x) (>.x.x x}. .

Definition 3.4.3. We say that an abstract rewriting system is irrefiexive if there is not a.ny one-step loop.

We define the notion of terminal loops as loops that ca.nnot rewrite to anything else than itself.

Definition 3.4.4. A terminal loop is a loop a ---+ + a such that there is no b -:f:. a with a-b.

Note that the only terminalloops are loops of one step.

Definition 3.4.5. An abstract rewriting system (A,---+) is weakly irrefiexive if there is not any terminal loop. ·

34 CHAPTER 3. TOPOLOGY

For example, the pure type systems defined in chapter 9 are weakly irreflexive. There are no terminal loops.

In order to give a topological characterisation of normal forms we require that the abstract rewriting system should be weakly irreflexive. In this case the normal forms are exactly the dosed points in the topology associated to the abstract rewriting system.

In the following lemma, we give a topological characterisation of normal forms for weakly irreflexive abstract rewriting systems: an element of a weakly reflexive abstract rewriting system is a normal form if it is a closed point in the associated topology.

Lemma 3.4.6. (Topological characterisation of normal forms) Let (A,-+) he a weakly irreflexive abstract rewriting system. An element a E A is a

normal form iff {a} is a T_, closed set.

In the following theorem, we give some topological characterisations of weak normalisation for weakly irreflexive abstract rewriting systems.

The first characterisation says that an abstract rewriting system is weakly normalising if and only if every closed set has a closed point.

Note that the weakly normalising elements are the expansion of some normal form. This give us the other characterisation: (A,-+) is weakly normalising if A can be obtained by a.pplying the operator S to the set of closed points.

Theorem 3.4.7. (Topological characterisations of weak normalisation) Let (A,-+) he a weakly irreflexive abstract rewriting system. The following statements

are equivalent.

1. (A, -+) is weakly normalising.

2. Every non empty dosed subset of A with respect to the topology associated to (A,-+) has a closed point.

3. If X is thesetof dosed points of A then A= S(X).

Pro of:

(1 # 2). Suppose (A,-+) is weakly normalising. Take C :f:. 0 a closed subset of A. Take c E C and oonsider a E A such that a is the normal form of c. Hence a E C because C is dosed and a is a closed point because of lemma 3.4.6. Conversely, suppose that every non empty closed set has a closed point. Then for any b E A the closed set C(b) contains a closed point c. Then b - c and c is in normal form.

(1 # 3). Easy. 0

To characterise strong normalisation we need the concept of noetherian family of subsets.

3.5. GONGLUS/ONS AND RELATED WORK 35

Definition 8.4.8. Let A be an arbitra.ry set and Sa familyof subsets of A, i.e. S Ç P(A). We say that S is noetherian if and only if all decreasing subfamilies of S sta.bilize, i.e. for a.n arbitrary family {S;: iE IN} Ç S such that St 2 S2 2 S3 2 .. 2 Sn 2 .. => 3m E 1N such that Sm Sm+l = ... Theorem 8.4.9. (Topological characterisation of strong normalisation)

Let (A,-+) he a wea.kly irreftexive abstract rewriting system. (A, -+) is strongly normalising if and only if the following two conditions are verified:

1. The family P _, Ç :F _. of principal closed sets of A is noetherian.

2. C(a) = C(b) Ç> a= b.

Proof: Suppose -+ is strongly normalising. We prove the two conditions:

L Suppose tha.t we have a decreasing fa.mily of sets in P _, i.e. a family of the form:

C(at) 2 C(a:~) 2 ... 2 C(a.,) 2 C(a,.+t) 2 ...

This fa.mily produces a sequence of reductions a1 ~· a2 ~ ... a,. ~ an+I .... There exists mE 1N such tha.t am = am+t = .... Then C(am) = C(am+t) = ... and hence the family stabilizes.

2. If C(a) = C(b) with a :f:. b, we would have a reduction of infinite length a-++ b -++ a-++ b ....

Conversely, any reduction a1 -+ a2 -+ ... a.. -+ a,.+t ... produces a family of principal closed sets, C(at) 2 C(a2) 2 ... 2 C(a,.) 2 C(a,.+I) 2 .... By the noetherian hypothesis we conclude that there exists an m E 1N such that C(am) = C(am+I) = .... Hence by hypothesis 2 we conclude that am = am+I = . . .. 0

3.5 Conclusions and Related Work

The topology associated to an abstract rewriting system is the well-known Alexandroff topology that bas been considered mainly for preorders. The comparison between abstract rewriting systems and topological structures has the novelty of finding topological characterisations for confluence and normalisation.

We see that in these topological characterisations of conftuence, weak and strong normaliaation expansion is as important as reduction. Expansion has also played a role in the characterisation of thesetof strongly normalising .À-terms in chapter 5.

This chapter is logically independent of the rest of the thesis. Although we did not apply these results later, wethink that it is relevant because to illuminate the sameobject with light from different angles can be sometimes very productive.

This topology does not look at the structure of the term (neither does the notion of abstract rewriting system) but only at the reduction graph of the term. In earlier work, other topologies associated to a rednetion relation have been considered. In all these cases the topology depends on the structure of the terms ( see [Bar85], [KKSdV91] and [KKS95]).

36 GRAPTER 3. TOPOLOGY

Chapter 4

Abstract Typing Systems

4.1 Introduetion

In computer science the notions of computation and typing are basit:, and play a.n essentia.l role in the theory and its applications. In cha.pter 2 we have forma.lised the notion of computation in an abstract way as a binary relation on a. set.

The concept of typing can be modelled also as a. binary relation. In this case the rela.tion is called the typing relation (usually denoted by : ). An element a is typa.ble if there exists b such that a : b and it is inhabited if there exists b such that b : a.

In the case that the binary relation represents the rewrite relation, we are interested in looking at properties associated to its tra.nsitive ciosure like confinenee a.nd norma.lisation. In the case of a binary relation that represents the typing relation, we are interested in other kinds of properties like for example uniqueness of types. A typing relation does not have much use by itself and we believe it makes sense in an abstract setting only when considered together with a rewrite rela.tion.

We consider a triple (A,-+,: ) consisting of a set and two binary relations, one representing the rewrite relation and the other representing the typing relation. There should he some interaction between these two relations. Now we discuss the kind of interaction we consider interesting.

We use the typing relation in order to restriet the domain of the rewrite relation to the set of elements that are typable or inhabited. This is useful when the abstract rewriting system does not verify weak or strong normalisation.

We want thesetof typable elements together with the rewrite relation to be a subsystem of the original. This is verified when the set of typable elements and the set of inhahited elements are closed under the rewrite relation. These properties are called subject reduction and type reduction respectively.

Hence, we need that the rewrite and typing relations verify the subject and the type rednetion properties. We say that the triple (A, -+ 1 : ) is an abstract rewriting system with typing if it verifies the subject and the type rednetion properties.

We want that the abstract rewriting system obtained by restricting the domain A to

37

38 CHAPTER 4. ABSTRACT TYPING SYSTEMS

the set of typable or inhabited elements verifies weak and strong normalisation. In this chapter we prove these properties for abstract rewriting systems with typing under certain hypothesis.

Finally, we introduce an abstract notion of environment (also called context). The typing relation and the rewrite relation may depend on environments. We consider indexed families of abstract rewriting systems with typing where the indices represent the environments. These families are called environmental abstract rewriting systems with typing.

We summarise the contents of the sections of this chapter: In section 4.2, we introduce the notions of abstract typing system and of type. In section 4.3, we introduce the abstract conceptsof subject reduction and type reduction. Also wedefine the notions of abstract rewriting system with typing. We define the properties of uniqueness of types and of weak and strong normalisation. Also we give some general criteria to prove these properties. In section 4.4, we introduce the notion of environment. We add this feature to all the abstract structures defined in the previous section. In section 4.5, we introduce a general notion of · semantics.

4.2 Abstract Typing Systems

In this section, we introduce the notion of abstract typing systems (see also [Raa96]) to formalise the notion óf typing. As we said before a typing relation is formalised in an abstract way as a binary relation.

Definition 4.2.1. An abstract typing system is a pair (A, : ) consisting of a set A and a relation : C A x A called typing relation.

Definition 4.2.2. Let (A, : ) be an abstract typing system.

We say that .a has type b if a: b.

We say that a is a term ( or typable) if there exists b such that a : b.

We say that b is a type ( or inhabited) if there exists a such that a : b.

We say that a is a toptype if a is a type that is not typable.

Definition 4.2.3. Wedefine a morphism from (A,:.,.) to (B, :IJ) as a function f: A-+ B such that if a :"' a1 then f( a) :IJ f( a') for all a, a1 E A.

The category whose objects are the abstract typing systems and whose morphisms are the ones defined above is denoted by Ats.

Definition 4.2.4. We say that (B, :IJ) is an extension of (A,:") if AC Band the indusion mapping is a morphism from (A,:") to (B, :IJ)·

Definition 4.2.5. We say that an extension (B, :IJ) of (A, :a) is conservative if a :IJ a' implies a :a a' for all a, a' E A.

4.3. ABSTRACT REWRITING SYSTEMS WITH TYPING 39

4.3 Abstract Rewriting Systems with Typing

In this section, we consider triples (A,-+,:) consisting of a set A and two binary relations, one representing the rewrite relation and the other repreaenting the typing relation. There should be some interaction between these two relations. We will require that the rewrite relation and the typing relation commute in different ways.

Definition 4.3.1. (Commutation of the rewrite and the typing relations) Let (A,-+,: ) be a set A and two binary relations -+ and :on the set A.

1. Reducing the term. If a has type b then we can rewrite the subject a to a' and a' should have a type b' related to b. According to the relation between band b', we classify the way in which these two relations interact as follows.

(a) We say that (A,-+, : ) satisfies very weak subject reduction iffor all a, b, a' such that a: band a-+ a', there exists b' such that a': b' and b *"* b'.

(b) We say that (A,-+, : ) satisfies weak subject reduction if for all a, b, a' such that a: band a-+ a', there exists b' such that a': b' and b- b'.

a b

J i a' b1

( c) We say that (A, -+, : ) satisfies subject reduction if for all a, b, a' such that a : b and a -+ a1 we have that a1

: b.

a b

11

a1 b

Note that (A,-+, : ) satisfies (weak) subject reduction if and only if (A,-, satisfies (weak) subject reduction.

2. Reducing the type. If a has type b then we can rewrite the type b to b' and b' should have an inhabitant a' related to a. According to the relation between a and a', we classify the way in which these two relations interact as weak type reduction and type reduction.

(a) We say that (A,-+, : ) satisfies weak type reduction if for all a, b, b' such that a: band b-+ b', there exists a' such that a': b' and a- a'.

40 CHAPTER 4. ABSTRACT TYPING SYSTEMS

a b

i 1 a' b'

(b) We say that (A, -t, : ) satisfies type reduction if for all a, b, b' such that a : b and b -t b' we have that a : b'.

a b

1 a b'

Note that (A, -t.> :) satisfies (weak) type reduction if and only if (A,-, : ) satisfies (weak) type reduction.

3. Expanding. If a has type b then we can expand a or b. We introduce the notions of subject expansion and type expansion.

(a) We say that (A, -t, : ) satisfies subject expansion if for all a, b, a1 such that a : b, a' is typable and a' -t a we have that a' : b.

(b) We say that (A, -t, : ) satisfies type expansion if for all a, b, b' such that a : b, b' is typable and b' -t b we have that a : b'.

An abstract rewriting system with ( very, weak) typing consists of a set, two binary relations which satisfy some commutation requirements.

Definition 4.3.2.

We say that (A, -t1 : ) is an abstract rewriting system with very weak typing if (A, -t, : )

satisfies very weak subject reduction.

We say that (A, -t, : ) is an abstract rewriting system with weak typing if (A, -t, : ) satisfies weak subject and weak type reduction.

We say that (A, -t, : ) is an abstract rewriting system with typing if (A, -t, : ) satisfies subject and type reduction.

A morphism between abstract rewriting systems with typing is a function between sets that preserves the rewrite and the typing relations.

Definition 4.3.3. Wedefine a morphism from (A, -t0 " :a ) to (B, -tp, :p ) as a function f : A -t B such that f is a morphism in Ars from (A, -t.") to (B, -tp) and a morphism in Ats from (A, :a ) to (B, :p ).

4.3. ABSTRACT REWRITING SYSTEMS WITH TYPING 41

The category whose objects are the abstract rewriting systems with (weak) typing and whose morphisms are the ones defined above is denoted by (Arstw) Arst.

In a similar way as above, we can define the notions of refining, implementing and forgetting morphism.

If (A,-+, :) is an abstract rewriting system with (weak) typing then the set At= {a I a is typahle or a is inhabited} is closedunder the rewrite relation-+ and the pair (A~,-+) is an abstract rewriting system.

Definition 4.3.4. Let {A,-+, : ) be an abstract rewriting system with (weak) typing. Wedefine the restrietion of (A,-+) by (A, : ) as the abstract rewriting system (At,-+).

The most commonly used abstract rewriting systems do not satisfy the necessary properties of weak or strong normalisation. Abstract rewriting systems are combined with abstract typing systems in order to restriet the original system (A,-+) to (At,-+) to have these properties. Intuitively, an abstract rewriting system with ( weak) typing (A, -+, :) verit1es some property if the restrietion of (A,-+) by (A, : ) verifies this property.

Definition 4.3.5. Let (A,-+,:) be an abstract rewriting system with (weak) typing. We sa.y that (A,-+,:) is ( weakly) strongly normalising if the restrietion of (A,-+) by (A, : ) is ( weakly) strongly normalising.

The restrietion of an abstract rewriting system by an abstract typing system defined above can be expressed by means of a. functor.

Definition 4.3.6. We define a functor C : Arst.., -+ Ars as follows.

C(A,-+, :) = (A~,-+) for(A,-+, :)EArstw,

where A1 = {a I a is typable or a is inhabited}. This functor is defined for morphisms in the obvious way.

Lemma 4.3.7. A (refining, implementing, forgetting) morphism from (A,-+,., :") to (B, -+p, :p ) is a (refining, implementing, forgetting) morphism from C(A, -+a, :a ) to C(B, -+p, :p ).

Notice that, a morphism from C(A, -+", :") to C(B, -+p, :p) is a function that preserves the rewrite relation on a restricted domain. This function may not preserve the rewrite relation on the whole set and hence it may not be a morphism between abstract rewriting systems with typing.

The criteria for weak and strong normalisation given for abstract rewriting systems can he adapted to abstract rewriting system with (weak) typing. The weak normalisation criteria are used in the proof of weak normalisation for pure type systems with definitions of chapter 11. The second strong normalisation criterion is used in the proof of strong normalisation for pure type systems with definitions in chapter 11.

42 GRAPTER 4. ABSTRACT TYPING SYSTEMS

Lemma 4.3.8. (Weak Normalisation Criterion) Let (A, -+a, :a) and (B, -+p, :11) he abstract rewriting systems with (weak) typing. Suppose the following conditions are verified.

a) (B, -+11) is astrong conservative extension of (A,-+.,).

b) There exists a function f : B -+ A that is a strategy for -+11 and a morphism from (B, :p) to (A,:.").

If (A, -+ 0 , :a) is weakly normalising then so is (B, -+11, :11).

Lemma 4.3.9. (Weak Normalisation Criterion) Let (A, -+a, :a) and (B, -+ps, :p) he abstract rewriting systems with (weak) typing. Suppose the following conditions are verified.

a) (B, -+IJ) is astrong conservative extension of (A, -ta).

b) If a is in 6-normal form and a -ta a' then a' is in 8-normal form.

c) The relation -ts is weakly normalising.

d) The 6-normal form is a morphism nfs: B-+ A from (B, :f3) to (A, :c;;).

If (A, -ta, :a) is weakly normalising then so is (B, -tps, :IJ)·

Lemma 4.3.10. (Strong Normalisation Criterion) Let (A, -t"'' :"), (B, -+f3, :p) he two abstract rewriting systems with typing and suppose there is a refining morphism f: B-+ A between them. If (A, -+a,:"'.) is. strongly normalising then so is (B, -tp, :f3)·

Lemma 4.3.11. (Strong Normalisation Criterion) Let (A, -+01 , :.") to (B, -+ps, :IJ) he abstract rewriting systems with (weak) typing. Suppose there is a mapping f: B -t A such that:

a) fis an implementing morphism from (B, -+ii, :p) to (A, -ta, :" ).

b) fis a refining morphism from (B, -tf3, :p ) to (A, -t0 " :"' ).

c) (B, -ts, :IJ) is strongly normalising.

If (A, -+a, :a ) is strongly normalising then so is (B, -tps, :p ).

Proof: This follows from lemma 2.6.6. We consider the abstract rewriting systems: L:(A, -t"' :a ), .C(B, -+13, :IJ ) and .C(B, -+5 , :IJ ). o

Wedefine uniqueness of types property (up to conversion).

4.4. ENVIRONMENTS 43

Definition 4.3.12. Let (A, -t, : ) he an abstract rewriting system with (weak) typing. We say that (A, -t, : ) verifies uniqueness of types if for all a such that a: band a: b', we have that b - b'.

We give a criterion to prove that the uniqueness of types property is preserved from one system to its extension. This criterion is used to prove uniqueness of types for singly sorted pure type systems with definitions in chapter 11.

Lemma 4.3.13. (Uniqueness of Types Criterion) Let (B, -tp) be an extension of (A, -t_,). Suppose there is a mapping f: B -t A that is a strategy for-tpand a morphism from (B, :p) to (A, :a)· If (A, -te.., :a) verifies uniqueness of types so does (B, -tp, :p ).

Proof: Let b E B be such that b :p c and b :13 d. Since f is a morphism, we have that f(b) :" f(c) and f(b) :,. f(c'). By uniqueness of types for (A,:,.) we have that f(c) - .. f(d). Since -t_,C-tp, we have that f(c) -/3 f(d). Since fis a strategy we have that c - 13 d. 0

4.4 Environments

Both the rewrite relation and the typing relation may depend on environments ( also called contexts). The typing relation for pure type systems defined in chapter 9 and the rednetion of global definitions defined in chapter 11 are examples of this dependency. In order to have an abstract picture of that situation for the reduction of global definitions we consider indexed families of abstract rewriting systems and for pure type systems we consider indexed families of abstract rewriting systems with typing where the indices represent the environments.

Defihition 4.4.1. Let A and C be sets. We say that the triple (A, C, -t) is an environmental abstract rewriting system if -t is a function from C to P(A x A).

We write -tr instead of -t (f). Note that (A, -tr) is an abstract rewriting system for r E C. The elementsof C might be called pseudoertvironments (or pseudocontexts). The defi

nition of environment appears later.

Definition 4.4.2. Let (A, C, -t) be as above.

A rewrite step in r is a -tr-rewriting step. We write r 1- a -t b instead of a -tr b.

A rewrite sequence in r is defined as a -tr-rewriting sequence. We write r 1- a1 -t a2 -t a3 ••. instead of a1 -tr a2 -tr a3 ....

Definition 4.4.3. Let (A, C, -ta) and (A, C, -tp) be two environmental abstract rewriting systems. The union of -ta and -tp in ris defined as the union of -t"'r and -tPr· We write r 1- a -l-_,p b instead of a -tarf3r b.

44 GRAPTER 4. ABSTRACT TYPING SYSTEMS

We define the notion of morphism for environmental abstract rewriting systems. A morphism is a pair of functions, one of the functions transforms the environments ( or indices) and the other transfarms the elements.

Definition 4.4.4. Let (A,C,~.,) and (B,D,~f3) as above. We say that the pair (f,g) with f: C-t D and g: C x A~ Bis a morphism from (A, C, ~a) to (B,D, ~f3) if for all a, b E A and f E C, if f 1- a--+., b then f(f) 1- g(f, a) ~13 g(f, b).

In a similar way, we can define the notions of refining, imptementing and forgetting morphism.

Definition 4.4.5. Let (A, C, ~) as above. We say that f : C x A ~ A is a strategy if f 1- a.- j(f,a) for all f E C and a E A. (Sometimes we write fr(a) insteadof j(f, a)).

Intuitively, an element a of A verifies a property in r if a verifies this property in the abstract rewriting system (A, ~r). Moreover, an environmental abstract rewriting system (A, C,--+) verifies a property if (A, ~r) verifies this property for all r E C.

Definition 4.4.6. Let (A, C,-+) as above and rE C.

We say that a is ( weakly) strongly normalising in r if a is ( weakly) strongly normalising in the abstract rewriting system (A, ~r ).

We say that (A, C, ~) is ( weakly) strongly normalising if (A, -+r ) is (weakly) strongly normalising for all f E C.

We say that (A, C,->) is confluent if (A, -+r ) is confluent for all rEC.

Definition 4.4. 7. Let A and C be sets. We say that the triple (A, C, : ) is an environmental abstract typing system if: is a function from C to P(A x A).

Note that (A, :r ) is an abstract typing system for rE C.

Definition 4.4.8. Let (A, C, : ) be as above and r E C. We say that a has type b in r if a :r b. We write r 1- a : b instead of a :r b.

Definition 4.4.9. We say that rE Cis an environment (or a context) if there are a and b such that r 1- a : b.

Definition 4.4.10. Let (A, C, : ) as above and r E C.

We say that a is a term (or typable) in r if a is a term in the abstract typing system (A, :r ) . We denote that a is not typable in r by r lf a : -.

We say that a is a term ( or typable) if there exists r such that a is a term in r.

We say that a is a type ( or inhabited) in r if a is a type in the abstract typing system (A, :r ).

4.4. ENVIRONMENTS

We say that a is a type ( or inhabited) if there exists r such that a is a type in r.

We say that a is a toptype in rif a is a toptype in the abstract typing system (A, :r ).

We say that a is a toptype if there exists r such that a is a toptype in r.

45

Definition 4.4.11. We say that the quadruple (A, a,-+, : ) is an environmental abstract rewriting system with ( very weak , weak) typing if-+ and : are functions from a to P( A x A) and for all r E a, (A, -+r, :r) is an abstract rewriting system with (very weak, weak) typing.

Most of the examples of environmental abstract rewriting systems have a rewrite relation-+ that does not depend on the set a. In these cases, a-+ a' can he considered as an a.bbreviation of r 1- a-+ a', for all rE a.

An environmental abstract rewriting system with (weak) typing (A, a,-+, : ) verifies a property if (A, -+r, :r ) verifies this property for all rE a. Definition 4.4.12. Let (A, a,-+, : ) as above a.nd rE a.

We sa.y that (A, a,-+, : ) is ( weakly ) strongly normalising if (A, -+r, :r ) is (weakly) strongly normalising for a.U rEC.

We say that (A, a,-+, : ) verifies uniqueness of types if (A, -+r, :r ) verifies uniqueness of types for all f E a.

Definition 4.4.13. Let A= (A, a,-+"" :,. ) a.nd B = (B,D,-+p, :p) as above. We say that B is a.n extension of A if the following conditions are verified.

1. AC B.

2. acD.

3. r 1- a-+,. a' then r 1- a -+p a' for all a, a' E A and r E a.

4. r 1- a :" a' then f 1- a :13 a' for all a, a' E A and f E a.

We define the notion of morphism for environmental abstract rewriting systems with typing as a pair of functions. One of the functions transforms the contexts and the other transforms the elements. These fundions preserve the rewrite relation and the typing relation. Note that they also preserve rewrite sequences.

Definition 4.4.14. Let A (A, a,-+,., :a) and B (B,D,-+p, :{3) as a.bove. We say that (j,g) is a morphism from A toB if j : a-+ D, g: a x A-+ Band the

following conditions are verified.

1. If r 1- a -+a b then j(r) 1- g(f,a) -+(3 g(f,b).

2. r 1- a :a b then f(f) 1- g(r, a) :{3 g(r, b).

46 GRAPTER 4. ABSTRACT TYPING SYSTEMS

If the rewrite relation -+[i in the definition above does not depend on D then the first clause is replaced by the following one: if r 1- a --+a b then g(r, a) --+ {3 g(f' b).

We denote the category whose objects are the environmental abstract rewriting systems with typing as Carst, the category whose objects are the environmental abstract rewriting systems with weak typing as Carstw and the category whose objects are the environmental abstract rewriting with very weak typing as Carst.,w) (in all these cases, the morphisms are the ones considered above).

In a similar way, we can define the notions of implementing, refining and forgetting morphisms for environmental abstract rewriting systems with (weak) typing.

4.5 Semantics

The notion of interpretation can be formalised in an abstract way by means of a morphism that preserves the conversion relation. The codomain of the interpretation can be seen as the semantics of the respective domain. For all the categories defined in the previous sections, we define the notions of interpretation and semantics. This section has been introduced to state and prove formally the corollary 11.4.16.

Definition 4.5.1. Let (A, -+a) (B, -+[i) be two abstract rewriting systems. Wedefine an interpretation (or a converting morphism) from (A, -+a) to (B, -+fi) as a

function f : A -+ B such that if a *"*a a' then f( a) *"-*(3 f( a') for all a, a' E A. If there exists an interpretation from (A, -+a) tb (B, -+f3), we say that (B, -+[i) is a. semantics for (A,--+").

Definition 4.5.2. Wedefine an interpretation (or converling morphism) from (A,-+"'' :a) to (B, -+[i, :fi ) as a fundion f : A -+ B such that f is an interpretation in Ars from (A, -+a) to (B, -+fi) and a morphism in Ats from (A, :." ) to (B, :fi ). If there exists an interpretation from (A, -+a, :a ) to (B, --+(3, :(i ), we say that (B, -+[i, :(i ) is a semantics for (A,--+", :a ).

Definition 4.5.3. Let A= (A, C, -+a, :a ) and B = (B, D, -+[i, :{3 ) be environmental abstract rewriting systems with typing.

We say that (f,g) is an interpretation (or converling morphism) from A to B if f : C-+ D, g: C x A--+ Band the following conditions are verified.

1. If r 1- a--+,. b then /(f) 1- g(f,a) *"-*(3 g(f, b).

2. r 1- a :a b then /(f) 1- g(f,a) :(i g(f,b).

If there exists an interpretation frorn (A, C, -+", :a ) to (B, D, -+f!, :(i ), we say that (B, D, -+(3, :13 ) is a semantics for (A, C, --->a, :" ).

In the following, wedefine the notion of weak converting morphism as a pair of functions. The function that transforma contexts depends on the context and on the element.

4.6. CONCLUSIONS AND RELATED WORK 47

Definition 4.5.4. Let A = (A, C, ~"'' :"' ) and B = (B, D, ~rh :p ) as a.bove. We say tha.t f : C x A ~ D x B is a weak converting morphism from A to B if the

following conditions are verified for all a,b E A and fEC. Suppose f(f,a) (Ll,c) a.nd f(f,b) = (Ll1,d).

1. If r 1- a *"*a b then Lll- c *"*13 d.

2. If r 1- a :a b then Lll- c :p d.

This special kind of morphisms is used in chapter 10. Note that the contexts Ll and Ll' have no relationship a.nd the first clause ca.nnot be replaced by 'f 1- a ~"' b then Lll- c *-*13 d'.

A wea.k converting morphism from A to B could be seen as a weak interpretation from A toB.

4.6 Conclusions and Related Work

In this chapter, we have introduced the notions of abstract typing systerri and abstract rewriting system with typing. The concepts will not surprise the specialists in the subject, firstly because they are extremely natura.l and secondly because even though they appear as 'new', they were already 'there' in a sort of ghostly manner. All these definitions and properties are used in the rest of the thesis.

We think it is necessary to have a forma! basis for type systems in the same fashion as the notion of abstract rewriting systems is a forma! basis for the lambda calculus. For example, the whole section 4.5 was introduced in order to be able to formally describe a property for models of pure type systems with definitions. Intuitively, the interpretation of a pure type system with definitions is obtained by computing first the 8-normal form and then applying the interpretation of the original system without definitions. In [Pol94], this result is stated in an informal way. Since we introduce the general setting of abstract typing systems, we can say formally what a.n interpretation is and the considered property can he formalised adequately.

Abstract formalisations of the notion of logic can be found in [Bar7 4], [Mes89], [HST89], [Avr92] ·and [Acz95]. These formalisa.tions are aimed to model different aspectsof logic. For example, the notion of a proof is modelled in [Mes89] but the rewrite relation between proofs is not considered important. The notion of abstract rewriting systems with typing intends to capture the notionsof terms (proofs), types (propositions) and the rednetion of terms (the simplification of proofs). Hence our formalisation is aimed to model the interaction between the typing relation and the rewriting relation. This is because we are interested in properties of the typing systems like weak and strong normalisation.

Wethink that it could be possible to define the abstract notion of derivation andrelate it with the abstract logies defined in [Avr92] and [Acz95]. In that case we should introduce an abstract notion of typing rules and of typing relation generated by these rul es. Also we should probably add more structure to the notion of environment.

48 GRAPTER 4. ABSTRACT TYPING SYSTEMS

Part 11

Lambda Calculus

49

Chapter 5

Strongly Normalising À-terms

5.1 Introduetion

In this chapter we give some characterisations of the set of ,8-strongly normalising À

terms. Weusethese characterisations (see chapters 6-8) to give new proofs of some results concerning normalisation in À-calculus.

This chapter is organised as follows. In section 5.2 we define the set of À-terms and the ,8-reduction. Insection 5.3, first wedefine a set SN' by induction that reflects the intuition of what should he the set of strongly normalising terms. Then we define another set SN that is equal to SN'. Finally we prove that SN is thesetof ,8-strongly normalising terms.

5.2 Lambda Calculus

In this section we reeall the definition of the untyped lambda calculus with ,8-reduction. Thesetof variables is denoted by V== {v, v', v", .. . } and arbitrary variables in V are

denoted by x,y,z, . ...

Definition 5.2.1. The set A of À-terms is defined as the smallest set satisfying the following clauses.

1. V cA,

2. if M E A then >.x.M E A,

3. if ME A and NE A then (M N) E A.

Definition 5.2.2. The mapping FV : A C P(V) is defined as follows.

FV(x) FV(>..x.M) FV(MN)

= {x} FV(M) {x}

= FV(M) U FV(N)

51

52 CHAPTER 5. STRONGLY NORMALISING >..-TERMS

A variabie x is said to occur free in M if x E FV(M).

Definition 5.2.3. The mapping BV : A C P(V) is defined as follows.

BV(x) = 0 BV(>..x.M) = BV(M) U {x} BV(MN) BV(M) u BV(N)

A variabie x is said to occur bound in M if x E BV(M). We define substitution as in [CF58].

Definition 5.2.4. The result of the substitution of N for x in M is defined as follows.

x[x := N] = N y[x := N] = y if x# y

(PQ)[x := N] = (P[x := N]Q[x := N]) (>..x.P)[x := N] (>..x.P) (>..y.P)[x := N] (>..z.P[y := z][x := N]) if y #x, y E FV(N) and z is fresh (>..y.P)[x := N] = (>..y.P[x := N]) otherwise

Definition 5.2.5. A change of a bound variabie in the term Mis the replacement of a subterm (>..x.N) by (>..y.N[x := y]) where y tJ. FV(N).

The relation of a-conversion between >..-terms is defined as follows.

Definition 5.2.6. The termMis a-convertible toN if Nis the resuit of applying to M a series of changes of bound variables or vice versa.

Convention 5.2. 7. Two terms are identified if they are a-convertible.

We define now the notion of context in the lambda calculus as a term with holes in it.

Definition 5.2.8. Wedefine the set P by induction as follows.

1. x EP,

2. []EP,

3. if C1[] EP and C2[] EP then (C1[] Cz[]) EP,

4. if Cl(] EP then (>..x.C1[]) EP.

A context is an element of Pand is denoted by Cl:]. If C[] E Pand ME A then G[M] denotes the result of placing Min the holes of C[ ].

The essential feature of a context C[ ] is that a free variabie in M may become bound in C[M].

Definition 5.2.9. The ,8-reduction (or ,8-rewrite relation) is defined as follows.

C[(>..x.M)N]-~o13 C[M[x := N]]

where C[ ] E P has only one occurrence of [ ].

We use the usual abbreviations: I= >..x.x, K = (..\x.>..y.x), w = >..x.(xx) and n = (ww). There aretermsin the >..-calculus that are not strongly normalising like n and (I< I fl).

5.3. THE SET SN 53

5.3 The set SN In this section we give two .definitions by induction of the set of strongly normalising À-terms. These definitions use ,8-expansion.

An easy observation is that the set that contàins all normal forms and that is closed under expansion is exactly thesetof all weakly normalising terms. So we have the following definition.

Definition 5.3.1. The set Wis the smallest set of À-terms satisfying the following:

1. all normal forms are in W,

2. if C[P[x := Q]] E W, then C[(.\x.P)Q] E W.

The first naive attempt to obtain the set of all strongly normalising terms, is to add the requirement that the argument of the redex introduced by the expansion is strongly normalising. The set S is the smallest set that satisfies

1. all normal forms are in S,

2. if C[P[x := Q]] ES and Q ES, then C[(.\x.P)Q] ES.

However, it is easy to see that there are terms that are not strongly normalising that belong toS. For example, take w = Ày.(yy) and the rewrite sequence

(.\x.(Ày.z)(xx))w -+13 (.\x.z)w -+(3 w

The last term in the sequence, w, is in normal form then wE S. If we go backwards in the sequence we get that (.\x.z)w ES and also (>.x.(.\y.z)(xx))w ES. But this term is not strongly normalising because

(.\x.(.\y.z)(xx))w -+p (.\y.z)(ww) -+p (ww)

The problem is that expansions cannot be allowed to take place just everywhere. The expansion as in the second dause of the definition of S above will be required to crea.te a spine redex, i.e. a head redex or if there is no head redex an outermost redex.

Definition 5.3.2. The set 0 of contexts with a hole at a spine position is defined as the minimal set that satisfies

1. if C[ ] E 0 then x M 1 ... C[ ] ... M,. E 0,

2. if C[] E 0 then Àx.C[] E 0,

3. [ ]Pt ... P,. E 0.

54 GRAPTER 5. STRONGLY NORMALISING >.-TERMS

The redex in C[(>.x.M)N] is called a spine redex if C[] E 0 [BKKS87].

Definition 5.3.3. The set S.N' is defined as the smallest set that satisfies

1. all normal forms are in S.N',

2. if C[P[x := Q]] E S.N', Q E SN'' and C[] E 0, then C[(>.x.P)Q] E S.N'.

In order to obtain another definition of the set of strongly normalising >.-terms, observe that the set of normal forms can he defined by induction in the following way.

Definition 5.3.4. The set .N:F is the smallest set of >.-terms satisfying the following:

1. if x is a variabie and Mh ... , Mn E .N:F forsomen;::: 0, then xM1 ••• Mn E .N:F,

2. if ME .N:F then >.x.M E .N:F,

Wè define the set S.N as follows.

Definition 5.3.5. The set S.N is the smallest set of >.-terms satisfying thè following:

1. if x is a variabie and M1, ••. , Mn E S.N for some n ;::: 0, then xMt ... Mn E S.N,

2. if M E S.N then >.x.M E S.N,

3. if M[x := N]Pt ... Pn E S.N and NE S.N, then (>.x.M)N Pt ... P., E S.N.

In the following theorem, we prove that the set S.N characterises the set of strongly normalising terms.

Theorem 5.3.6. (Characterisation of the strongly normalising >.-terms) M is strongly normalising if and only if ME S.N.

Pro of:

::::}. Let M be a strongly normalising term. The proof proceeds by induction on the pair (maxred(M),M), lexicographically ordered by the usual ordering on lN and the subterm ordering. Here we denote by maxred(M) the length of a maximal rewrite sequence from M to normal form.

The base case is trivia! sirree it is easy to see that all normal forms are in S.N. Suppose the maximal reduction of M to normal form takes k + 1 steps. Let M =

ÀXt .•. >.x.,.PQt ... Qm. There are two cases.

Case 1. P = y. Th en the normal form of M is of the form >.x1 .•. Àxn.yQ~ ... Q;", with Q; -{3 Qi for i = 1, ... , m. By induction hypothesis, Q1 E S.N, ... , Qm E S.N. By the first and second dause of the definition of S.N, we have M = >.x1 ••• Àxn.yQ1 ••• Qm E S.N.

5.4. CONCLUSIONS AND RELATED WORK 55

Case 2. P = Ày.Po. We have M = Àxt ... Àx.,.(Ày.Po)QtQ2 ... Qm---> ÀXt ... Àx ... Po[Y Q1]Q2 ... Qm. By induction hypothesis, ÀXt ... Àx,..Po[Y := Qt]Q2 ... Qm E SN. Also by induction hypothesis, Q1 E SN. By the last clause of the definition of SN, we have M = Àxt ... .Xx,..(.Xy.Po)Qt ... Qm E SN.

{:::. Suppose ME SN. We prove by induction on the derivation of ME SN that Mis strongly normalising.

0

1. If M = xM1 ••• M,. with Mt, . .. , M,. E SN, then the statement follows easily by induction hypothesis.

2. If M = Àx.M0 with M0 E SN, then by induction hypothesis M0 is strongly normalising. Then also M = Àx.M0 is strongly normalising.

3. Let M = (> .. x.Mo)MtMz ... Mn with Mo[x := Mt]M2 ... M". E SN and Mt E SN. Consider an arbitrary rewrite sequence p : M =Po --->(3 P1 ->f$ P2 -+f' ... startingin M. There are two possibilities: in p either the head redex of M is contracted or the head redex of M is not contracted.

In the first case, there is an i such that Pi = M~[x := M{]M~ ... M~, with M0 -{3

M~, ... , M". -fi M~. Then P; is aresult of rewriting the term M0 [x := M1]M2 ••. M ... The latter is by induction hypothesis strongly normalising. Hence P, is strongly normalising so p is finite.

In the second case, alltermsin pare of the form (.Xx.M~)M{M~ ... M~ with M0 -{3

Mó, ... , Mn - 13 M~. By induction hypothesis, the term M0 [x := M1]M2 ... M". is strongly normalising. Therefore M0 , M2 , ••• , M". are strongly norma.lising. Moreover, we have by induction hypothesis that M1 is strongly normalising. Hence all the terms in the rewrite sequence are strongly normalising a.nd hence p is finite.

Theorem 5.3.7. SN'= SN.

Proof: SN' C SN is proved by induction on SN'. For SN C SN', we prove that the set of strongly normalising .X-terms is a subset of SN' by induction on (maxred(M), M). 0

5.4 Conclusions and Related work

We have defined the sets SN and SN' and proved that they are equal to the set of (3-strongly normalising .X-terms. In the chapters 6-8, we use the set SN to give new proofs of classica! results in lambda calculus. The use of SN to prove normalisation properties is very convenient because its definition is by induction and furthermore it recalls the notion of saturated set.

A saturated set is a subset X of the set of strongly normalising .X-terms that satisfies the following properties.

56 CHAPTER 5. STRONGLY NORMALISING >..-TERMS

L if x is a variabie and M1, ••• , M.,. are strongly normalising terms then the term xM1 •.• M", EX,

2. if M[x := N]P1 ••• P.,. EX and Nis strongly normalising then the term (> .. x.M)NPt ... P" EX.

The first and second dauses in the definition of SN are also conditions in the definition of a saturated set. In the definition of SN, we have an additional clause for abstractions, and in the definition of saturated set it is necessary to add the requirement that the set should be a subset of the set of strongly normalising terms.

Our definition of SN first a.ppea.red in [RS95] and more or less simultaneously a similar definition a.ppea.red in [Loa95].

Chapter 6

Perpetual Strategies

6.1 Introduetion

In this chapter we define two strategies Gbk and Goo similar to Fbk (BK82] and F00

[BBKV76]. These strategies.are perpetual, which means that they yield an infinite rewrite sequence whenever possible. We prove that Gb1c a.nd Goo are perpetua.l by using the characterisation of the set of strongly norma.lising terms. As a. consequence, we deduce tha.t Fok and F 00 are perpetua.l.

This chapter is organised as follows. In section 6.2 we prove that the strategies G&k and H~o are perpetual. Then we prove that the strategies G00 and F00 are perpetual. For the strategies G00 and Foo, we prove in section 6.4 that they are not only perpetual but also maximaL That is, they yield the longest possible reduction to normal form whenever the initia} term is strongly normalising, and an infinite rewrite sequence otherwise. This is clone by computing the length of the rewrite sequence tó the normal form.

6.2 The Strategies Fbk and Gbk

First we consider the strategy Fok as introduced in (BK82].

Definition 6.2.1. Suppose that ME A is not in normal form. Let M = G[(>.x.P)Q] where (>..x.P)Q is the leftroost redex of M.

{ G[P[x := QJ] if Q is strongly normalising

Fbk(G[(>.x.P)Q]) = G[(>.x.P)F6~;(Q)] otherwise

We define the strategy Gbk as a variant of Fbk· We reduce any spine redex instead of just the leftroost redex. This yields a non-deterministic strategy.

57

58 CHAPTER 6. PERPETVAL STRATEGIES

Definition 6.2.2. Wedefine Gbk : A-+ P(A) as follows.

;;;: Lj,~~{(xMt ... N; ... Mn)IN; E Gbk(M;)}

{(.\x.N)IN E Gbk(M}}

= { {M[x := N]P1 .• • Pn} if Nis strongly normalising {(.\x.M)QP1 ... PniQ E Gb~o(N)} otherwise

Theorem 6.2.3. Gbk is a perpetual strategy.

Proof: We prove that if Gbk(M) C S.N then ME S.N by induction on the structure of M.

0

1. Suppose that the term is (xM1 ... Mn)· Since Gbk(xM1 ••• Mn) C S.N, we have that Gbk(M;} C S.N for all i 1 ... n. By induction hypothesis we have that M; E S.N. I;Ience (xM1 .•• Mn) E SJV.

2. Suppose that the term is. (>.x.M}. Since Gbk(>.x.M) C S.N, we have that Gbk(M) C SJV. Moreover, by induction hypothesis ME S.N. Therefore (>.x.M) E SJV.

3. Suppose that the term is C>..x.M)N P1 •.• Pn. We have two cases:

(a.) If Nis strongly normalising then NE S.N. Since M[x :;;;: N]Pt ... Pn E S.N we have that (>.x.M)NP1 •• • Pn.·

(b) If N is not strongly normalising then there exists non-strongly normalising term Q in Gb~c((>.x.M)NPt ... Pn)·

The strategy Fbk is contained in Gbk·

Lemma 6.2.4. Let ME A not in normal form. Then Fbk(M) E Gbk(M).

Theorem 6.2.5. H~c is a perpetual strategy.

This follows from lemma 6.2.4 and theorem 6.2.3.

6.3 The Strategies F00 and Goo

We now consider the strategy Fco that is defined in [BBKV76]. This strategy does not check whether the argument of the leftmost redex is strongly normalising or not. Instead, it is checked whether the leftmost redex is an /-redex. If it is, it is contracted. If it is not, contrading it could imply loosing the possibility of having an infinite reduction sequence. Therefore, in that case, the leftmost redex is only contracted if the argument is a normal form. If the argument is not a normal form, the stra.tegy is applied to the argument.

6.4. MAXIMAL STRATEGIES

Definition 6.3.1. Suppose that M E A is not in normal form. Let M = C[(.-\x.P)Q] where (.-\x.P)Q is the leftmost redex of M.

F oo( G[( .-\x.P)Q]) { C[P[x := Q]] if x E FV(P) or Q E N":F C[(.-\x.P)Foo(Q)] otherwise

The merit of F 00 is that it is decidable.

59

We define the strategy Goo as a variant of F00 • We do not only reduce the leftmost redex but also any spine redex. This yields a non-deterministic strategy.

Definition 6.3.2. We define Goo : A -+ 'P(A) as follows.

Goo(xMt ... M,..)

Goo(Àx.M)

Goo((Àx.M)N P1 ... P,..)

= Lf;~~{(xM1 ... N; ... M,..)IN• E Goo(M)}

{(Àx.N)IN E Goo(M)}

= { {M[x := N]P1 . .. P,.} if x E FVP or Q E N":F {(Àx.M)QPl ... P,.IQ E Goo(N)} otherwise

Theorem 6.3.3. Goo is a perpetual strategy.

We prove that if Goo(M) C SN" then ME SN" by induction on the structure of M.

Lemma 6.3.4. Let ME A not in normal form. Then Fc?>(M) E Goo(M).

As an immediate consequence of the previous lemma we have that F 00 is perpetual.

Theorem 6.3.5. Foo is a perpetual stra.tegy.

6.4 Maximal Strategies

In this section we prove that the strategies Foo and Goo are maxima[, which means that they compute for each term M the longest possible rewrite sequence. In pa.rticular, a maximal strategy is perpetua.l. The converse is not necessarily true, as witnessed by the strategy Hk defined in [BK82].

Example 6.4.1. The Fbk·rewrite sequence startingat (Àx.z)(II) has length 1.

(Àx.z)(II) -+ 13 z

However the length of the maximal rewrite sequence is 2.

(Àx.z)(JI) -+13 (.-\x.z)I -+/3 z

60 GRAPTER 6. PERPETUAL STRATEGIES

Our proof that Goo is a maximal strategy makes use of the characterisation of strongly normalising terms. We define a mapping h that computes the length of a G00-rewrite sequence of a term. Then it is proved that the mapping h computes the length of a maximal rewrite sequence to normal form.

We define a map h : A -+ 1N U { oo} that computes for each term the length of its F00-rewrite sequence.

Definition 6.4.2.

1. The map h : SN -+ 1N is defined by induction on the definition of SN.

{ t.h(Mo)

if n = 0 h(xM1 ••• M",) = if n tf 0

h(>.x.M) :;::: h(M)

{ h(M[x:=N]Pl···Pn)+l ifxEFV(M)

h((>.x.M)N p1 · · · P",) = h(M P1 ••. P",) + h(N) + 1 if x~ FV(M)

2. We extend h : SN-+ 1N to h :A -+ 1N U { oo} by defining h(M) = oo if M ~SN.

We prove that the map h has the following two properties:

• it computes the lengthof all the G00-rewrite sequences of a term M,

• it computes the lengthof a maximal rewrite sequence startingin M.

From these we coneinde that Goo and Foo are maximal strategies. First we pr9ve the following lemma.

Lemma 6.4.3. Let M E SN.

1. If ME N:F then h(M) = 0.

2. If M ~ N:F then h(M) h(N) + 1 for all NE Goo(M).

Pro of:

1. Trivial.

2. Suppose that M is not in normal form. We prove that h(M) = h(N) + 1 for all N E Goo(M) by induction on ME SN. We consider these two cases:

6.4. MAXIMAL STRATEGIES 61

0

(a.) The termMis of the form yQt ... Qm. By induction hypothesis we have h(Q;) h(N;) + 1 for all N; E Goo(Q;). Take i a.nd N; E Goo(Q;). Hence we have

m

h(M) = E h(Q~o) k=i

= h(Q,) + Eh<Q") k~i

= h(N;) + 1 + Eh(Qk) k-:Fi

= h('uQt ... N; . .. Q,.) + 1

(b) The termMis (..\y.Po)Ql ... Qm. Two cases are distinguished.

i. y E FV(Po). Then Goo(M) {Po[Y := Q1] Q2 ... Qm}. We have tha.t h(M) = h(Po[Y := QI] Q2 ... Qm) + 1.

ii. y f/. FV(P0 ). Aga.in two cases are distinguished. A. If Q1 is not in normal form then

By induction hypothesis, h(Qt) = h(N) + 1 for all NE G00 (Q1). Hence we have

h(M) = h(PoQ2···Qm)+h(Qt)+l

h(Po Q2 ... Qm) + h(N) + 1 + 1 h((Ay.Po)N Q2 ... Qm) + 1

B. If Q1 is in normal form then

h(M) = h((>.y.Po)QIQ2 ... Qm)

= h(Po Q2 ... Qm) + h(Qt) + 1 = h(Po Q2 ... Qm) +0+ 1

Theorem 6.4.4. The map h : A -t IN U { oo} computes the lengthof all the G00 -rewrite sequence of a term M.

Proof: If ME SN then a G00-rewrite sequence is of the form

M -tp M1 -tp ... -tp M,. with Min normal form.

It follows by induction on n that h(M) = n using lemma 6.4.3. If M f/. SN then a. G00-rewrite sequence of M is infinite a.nd indeed h(M) = oo. 0

62 GRAPTER 6. PERPETUAL STRATEGIES

Now we prove that h : A ----+ .IN U{ oo} computes the lengthof a maximal rewrite sequence starting at M. Hete maxred( M) denotes the length of a maximal rewrite sequence starting inM.

Theorem 6.4.5. Let M E A. We have

h(M) maxred(M)

Proof: If M ~ S.N, then h(M) = oo soit is clear that the statement holds. Suppose that M E S.N is not in normal form. We wil! prove that the length of an

arbitrary reduction to normal form is less than or equal to h(M). The proof proceeds by induction on the number of steps in the derivation of M E S.N. The term M is of the form ÀX1 ••• x"..PQt ..• Qm where P can be either a variabie y or an abstraction Ày.P0 • We consider these two cases:

1. P = y. An arbitrary reduction from M to normal form can he transformed into a reduction sequence of the same length such that:

ÀXt ... Àx"..yQt ... Q.,: ~!3 ÀXt ... Àx,...y nf( Qt)Q2 ... Qm

..!!'#13 Àx1 ... Àx",.y nf(Qt) nf(Q2) ... Qm -l'lïJ

~!3 ÀXt ... Àx",.y nf(Qt) nf(Q2) ... nf(Qm)

Here nf(M) denotes the normal form of M.

The number of steps of this sequence is n1 + ... + nm. By induction hypothesis, we have h(Qi) 2:: n, for i= 1, ... ,m. Hence we have

m

h(M) Lh(Qi) i=l m

> n;

2. P Ày.P0 • Two cases are distinguished.

(a) y E FV(Po). An arbitrary reduction sequence from M to normal form is of the form

M = ÀXt ... Àxn.(Ày.Po)QIQ2 ... Qm

413 ÀXt ... Àxn.(Ày.P~)Q~Q~ ... Q'.,.

----+p ÀXt •.. Àx ... P~[y Q~]Q~ ... Q'.,. I

-P nf(M)

6.4. MAXIMAL STRATEGIES 63

0

It can he transformed into a rewrite sequence of the form

M = ÀXt ... ÀXn.(Ày.Po)QtQz ... Qm ~(3 ÀXt ..• ÀXn.Po[Y := Qt]Qz ... Qm

~f3 Àxt . ~. Àxn.PMy := Q~]Q~. ·. Q;.. I

-f3 nf(M)

with k ~ p. By induction hypothesis, h(Po[Y := Qt]Qz ... Qm) ~ k + l. Hence

h(M) = h(Po[Y := Qt]Q2 ... Qm) + 1

~ k+l+l

~ p+l+l

(b) y tf. P0 • An arbitrary reduction sequence from M to normal form can be transformed into a reduction sequence of the same length of the form:

M = ÀXt ... Àxn.(Ày.Po)QtQz: .. Qm

4(3 ÀXt ••• ÀXn.(Ày.Po)Q;Qz · · · Qm ~(3 ÀXt •.• Àx,..Po Qz . .. Qm

I nf(M) -(3

By induction hypothesis we have that h(Ql) ~pand h(PoQz ... Qm) ~ l. Hence

h(M) = h(PoQz ... Qm) + h(Qt} + 1

~ l + p+ 1

Theorem 6.4.6. (Maximal Strategy) The strategy Goo is maximaL

Pro of:

1. By theorem 6.4.4 we have that h(M) is the length of all the G00-rewrite sequences of M.

2. By theorem 6.4.5 we have that h(M) maxred(M) is the maximumlengthof all reductions sequences starting at M.

Hence the strategy Gco is maximaL 0

As a consequence of the previous theorem we have that the strategy F 00 is also maximaL

64 CHAPTER 6. PERPETUAL STRATEGIES

6.5 Conclusions and Related Work

In this chapter the strategies Gbk and Goo are defined which are similar to Fbk [BK82] and Foo [BBKV76]. Instead of looking at the leftmost redex, the new strategies look at the spine redexes. As a consequence, these strategies are non-deterministic.

The original proofs of the facts that Fbk and F oo are perpetual praeeed by a case analysis [Bar85]. In order to prove that a strategy Fis perpetual it is proved that F(M) admits an infinite rewrite sequence if M does so. In order to prove that the strategies Gbk and Goo are perpetual, we use the set S.N. In order to prove that a strategy G is perpetual, we prove that G(M) c S.N => M E S.N. The deterministic strategies Fbk and Foo are partienlar cases of Gbk and Goo and so they are perpetual. Proving G(M) C S.N:::} M E S.N and using the definition of S.N make our proofs more perspicuous.

The fact that Foo is a maximal strategy has been proved by Régnier [Reg94] using a relation that permits the permutation of redexes. Much more in the spirit of the present workis a paper by S~rensen ([Sor94]), who gives a proof that is very similar to ours. His work was developed independently and simultaneously. In our case we have proved that Goo is maximaL Since Fco is a partienlar case of Goo, wededuce that Foo is also maximaL

Chapter 7

Developments and Superdevelopments

7.1 Introduetion

In this chapter we give two new and short proofs of the fact that in >.-ca.lculus a.ll {3-developments terminate. In order to prove that a.ll {3-developments are finite it is sufiicient to prove that the f3 is strongly norma.lising. For the first proof we define a set that characterises the {3-strong)y norma.lising terms. Then we prove that a.ny term belongs to this set. As a consequence of this we have that all the terros are {3-strongly norma.lising. For the secoud proof we define a mapping from the underlined >...'terms to the set SN. We prove that this is a morphism between abstract rewriting systems.

Applying similar methods, we give two new and short proofs of the fact that in À

calculus all {3-superdevelopments terminate.

This chapter is organised as follows. In section 7.2 we reeall the definition of development. In section 7.3 we give a short and simpleproof of finiteness of developments. In section 7.4 we give another proof of finiteness of developments that makes direct use of the set SN. In section 7.5 we reeall the definition of superdevelopments. In section 7.6 we prove that all superdevelopments are finite. In section 7.7 another proof of finiteness of superdevelopmentsthat makes direct use of the set SN and similar to the one in 7.4.

7.2 Developments

We shortly reeall some definitions, fora complete formal treatment see [Bar85]. A development is a rewrite sequence in which only descendants of redexes that are present in the initia! term may be contracted.

Usually, {3-developments are defined via a set of underlined À-terms and an underlined {3-reduction rule.

65

66 GHAPTER 7. DEVELOPMENTS AND SUPERDEVELOPMENTS

Definition 7.2.1. Thesetof underlined À-terms A is defined by induction as follows.

1. x E A for every variabie x,

2. if ME A, then Àx.M E A,

. 3. if M E A and N E A, then M N E A,

4. if ME A and NE A, then (~x.M)N E A.

The notion of context G[ ] with holes is defined similar to definition 5.2.8. The [i-reduction is defined as follows.

C[(~x.M)N] -+f!.. C[M[x := N]]

where C[ J is a context with only one occurrence of [ ]. Note that A is closed under /1-reduction. We define a mapping e that -;;rases underlinings.

Definition 7.2.2. The mapping e: A-+ A is defined as follows.

e(x) = x e(>.x.M) = Àx.e(M) e(MN) e(M)e(N)

e((lx.M)N) = (>.x.e(M))e(N)

Lemma7.2.3. The mappingeis a morphism from (A, -+f!..) to (A, -+p).

Definition 7.2.4. A rewrite sequence er : M -.a N in A is a development if there is a rewrite sequence pin A that is an e-lifting of er. Diagrammatically,

The abstract rewriting system.:! can be either In forsomenor I (see example 2.2.3).

Example 7.2.5. The following /1-rewrite sequence is a development.

Àx.(x x) >.x.(x x) -+p Àx.(x x) >.x.(x x)

The e-lifting of this development is the {ollowing é:-rewrite sequence.

~x.(x x) Àx.(x x) -+f!.. >.x.(x x) >.x.(x x)

The term >.x.(x x) Àx.(x x) is in é:-normal form.

7.3. FIRST PROOF OF FINITENESS OF DEVELOPMENTS

We write a function that computes the ~-normal form.

Definition 7.2.6. The mapping nff!.: A-+ A is defined as follows.

nf~(x) x

nf[!.(Àx.M) = ÀX.nff!.(M)

nff!.(MN) = nff!.(M)nf~(N)

nf[!_((J.x.M)N) = nff!.(M)[x := nf{!(N)]

Note that nf~(M) does not contain J.'s soit is in ~-normal form.

Lemma 7.2.7.

1. M -E!. nff!.(M).

2. If M -+[!. N then nf~(M) = nf~(N).

67

So we have that nfp(M) is the ,8-normal form of Mand is unique. The functions e and nf.a are used in [Bar92T to prove co'"ilfluence for ,8-reduction.

-In the following sections, we prove that ~ is strongly normalising.

7.3 First Proof of Finiteness of Developments

We give a new and short proof of finiteness of developments by consiclering another inductive definition V of thesetof all underlined À-terms. Like in the definition of the set S.N, we make use of the expansion. We prove that all the terms in V are ,8-strongly normalising. Finally, we prove that V IJ... -

Definition 7.3.1. The set V is the smallest set of À-terms satisfying

1. x E V for all variables x,

2. if M E V, then >.x.M E V,

3. if M E V and N E V, then M N E V.

4. if M[x := N] E V and N E V, then (J.x.M)N E V.

The proof of the following lemma is immediate.

Lemma 7.3.2. If Pin PQ is not of the form J.x.P0 , then all ~-reducts of PQ are of the form P'Q' with P -{! P' à.nd Q -~ Q'.

68 CHAPTER 7. DEVELOPMENTS AND SUPERDEVELOPMENTS

Theorem 7.3.3. If ME V, then all ~-rewrite sequences.starting in Mare finite.

Proof: The proof proceeds by induction on the derivation of M E V.

0

1. If M is a variabie then it is trivia!.

2. Let M ;;:: >.x.P with P E V. By induction hypothesis, we have that P is strongly ~-normalising. So M is strongly ~-normalising.

3. Let M = PQ with P E V .and Q EV. Note that Pis not of the form ~x.P0 • By lemma 7.3.2, every /3-reduct of M is of the form P'Q1 with P """*IJ P' and Q """*!3 Q'. By induction hypothesis there are no infinite /3-rewrite sequences-starting in Por in Q. Therefore Mis strongly ~-normalising.. -

4. Let M = (ll,x.P)Q with P[x := Q] E V and Q E V. Consicier an arbitrary j3-rewrite sequence p: M = M0 """*{3 M1 """*!3 M2 """*!3 .... There are two possibilities:-in p the head redex of M is contracted or the head redex of M is not contracted.

In the first case there is an i such that Mi = P'[x := Q1, with P """*!3 P' and Q ""*!3 Q1

• The term M; is a result of rewriting P[x := Q], and the latter is by induction hypothesis strongly ~-normalising. Hence p is finite.

In the second case all terms in p are of the form (llx.P')Q' with P """*13 P 1 and Q ""*!3 Q'. By induction hypothesis, P[x := Q] is strongly /3-normalisîng, which yields-that P is strongly /3-normalising, and moreover Q is strongly j3-normalising. Hence all terms in p are strongly normalising so p is finite. -

Lemma 7.3.4. If ME V and NE V then M[x N] EV.

This lemma is proved by induction on M E V.

Lemma 7.3.5. If M[x := N] E 11 then M E /l.

Theorem 7.3.6. 11 V.

Pro of:

C. Let M E A. We prove by induction on M that M E V. We prove the case that M = (ll,x.P)Q. By induction hypothesis, P E V and Q E V. By lemma 7.3.4 we have that P[x := Q] E V and by the definition of V we have that (ll,x.P)Q E V.

:::>. Let M E V. By induction on the derivation of M E V we prove that M E /l. We prove the case that M (ll,x.P)Q. By induction hypothesis, P[x := QJ E 11 and Q E /l. By lemma 7.3.5, P E /l. Hence (ll,x.P)Q E

0

Corollary 7.3.7. (Finiteness of Developments) All j3-developments are finite.

7.4. SECOND PROOF OF FINITENESS OF DEVELOPMENTS 69

7.4 Second Pro of of Finiteness of Developments

lt is possible to prove in a different way, also using the set SN, that all developments are finite. We define a morphism

(SN, -+p)

Let Abs denote a distinguished variable.

Definition 7.4.1. Wedefine 1: !1--+ SN as follows.

l(x) = x l(Àx.M) = Abs)..x.J(M) l(MN) = l(M)l(N)

l((J.x.M)N) = (>-.x.J(M))l(N)

Lemma 7.4.2. J(M[x := N]) = J(M)[x := l(N)].

Theorem 7.4.3.

1. if M E !1 then l(M) E SN,

2. if ME !1 and M -+f!.. N, then l(M) -+p l(N).

Corollary 7.4.4. The mapping 1 is a morphism from (!1, --+[!_) into (SN, -+p).

Theorem 7.4.5. The rewrite relation (!_is strongly normalising.

Proof: This fellows from lemma 2.6.5 and corollary 7.4.4. 0

Corollary 7.4.6. (Finiteness of Developments) All ,8-developments are finite.

7.5 Superdevelopments

In [R.aa93], superdevelopments were introduced and proved to he finite. Superdevelopments form an extension of the notion of development. In a superdevelopment not only redexes that deseend from the initia! term may he contra.cted, but also some redexes that are created during reduction.

There are three ways of creating new redexes (see [Lev78]):

1. ((>..x.Ày.M)N)P -+p (>-.y.M[x := N])P

2. (>-.x.x)(>..y.M)N -+p (>-.y.M)N

70 CHAPTER 7. DEVELOPMENTS AND SUPERDEVELOPMENTS

3. (Àx.C[xM])(>.y.N) -+fJ C'[(>.y.N)M'J where C' and M' are ohtainedfrom C and M hy replacing all free occurrences of x hy (Ay.N).

The first two kinds of created redexes are 'innocent' and they may he contracted in a superdevelopment. The result that all superdevelopments are finite shows that infinite ,8-reduction sequences are due to the presence of the third type of redexes.

In the following two sections we give two new proofs of the fact that in >.-calculus all ,8-superdevelopments terminate.

First we shortly repeat the definition of a superdevelopment. The definition makes use of a set of labelled À-terms and a notion of labelled ,8-reduction. Since application nocles will be labelled, we write them explicitly.

Definition 7.5.1. The set Aj of labelled À-terms is defined by induction as follows.

1. x E Aj for every variabie x,

2. if M E Af and i E IN, then À;x.M E Aj,

3. if M, NE Aj a.nd X c IN, then @X (M, N) E Af.

Sometimes we write i instea.d of {i} for iE IN. The notion of context C[ ] with holes is defined simHar to definition 5.2.8. On the set Af, the .81-reduction is defined as follows.

C[@x(>.;x.M,N)J-+ C[M[x := NJ] ifi EX

where C[] has only one occurrence of [ ]. We define a mapping from A1 to A tha.t erases the labels.

Definition 7.5.2. The mapping e1 : Af -+A is defined by induction on the definition of A1 as follows.

e1(x) = x

et(À;x.M) Àx.~(M)

e1(@x(M,N)) = e1(M)e1(N)

The proof of the following lemma is stra.ightforward.

Lemma 7.5.3. The mapping ec is a morphism from (Af, -+fJ1) to (A, -+fJ)·

The ,81-reduction is not strongly normalising on the set Al since any ,8-rewrite sequence can be lifted in a. .81-rewrite sequence. This is illustrated by the following example.

7.5. SUPERDEVELOPMENTS

Example 7.5.4. Let w = À1x.©!1(x,x).

©!1(w,w) -+p1 ©!1(w,w)

The term ©!1 (w,w) is not {J,-strongly normalising.

We restriet the set A'/ to a set A, of weli-labelled terms.

Definition 7.5.5.

71

1. A term M E At is said to be welt-labelled if the label X of an application node never contains the label i of a À outside the scope of the application node. The set of weli-labelled À-terms is denoted by Az.

2. A term M E A, is initially labelled if it is weli-labelled and all Ns have a different label.

The set A, of weli-labelled termsis closedunder fJ,-reduction.

Lemma 7.5.6. (fJz-closure) If ME A, and M -+p1 N then NE A,.

This is proved by induètion on M E Al.

Definition 7.5.7. A rewrite sequence a: M-P N in A is a superdevelopment ifthere is a rewrite sequence p in A, that starts in an initially labelled termand that is an e,-lifting of a. Diagrammatically,

(A, -+p)

The abstract rewriting system J is either In or I (see exa.mple 2.2.3 and definition 2.3.4).

Example 7.5.8. The following {J-rewrite sequence is a superdevelopment.

(>.x.>.y.xy)(>.z.z)u -+p (>.y.(>.z.z)y)u -+p (>.z.z)u

An e,-lifting for this {J-rewrite sequence is, for example, the following {J,-rewrite sequence.

©!2(©!1 (>qx.À2y.©!3(x, y ), À4z.z), u) -+p1 ©!2( À2y.©!3(À4z.z, y ), u) -+p1 ©!3(>.4z.z, u)

In the first step, the variabie x is replaced by >.4z.z. This corresponds to the third way of crea.ting a redex and so ©!3( À4 z.z, y) is not a fJrredex.

72 CHAPTER 7. DEVELOPMENTS AND SUPERDEVELOPMENTS

We define a function that computes the p,-normal form of a term.

Definition 7.5.9. The fundion nfp1 : A1 ....... A1 is defined by induction on the definition of A, as follows.

nfpp,,x.M) = À;x.nf131 (M)

Lemma 7.5.10. lf@X(M,N) E A1 then nfp,(M[x N]) = nfp1(M)[x := nfp,(N)].

Lemma 7.5.11. Let ME A1•

1. M -'Wf31 nf p1(M) and nf p,(M) is in 111-normal form.

2. If M ....... p, M' then nfp1(M) := nfp1(M').

So we have nf p1 ( M) is the Pz-normal form of M and it is unique. As a eonsequence of this we have that 111 is confluent.

In the following sections, we prove that p, is strongly normalising.

7.6 First Proof of Finiteness of Superdevelopments

We give a new proof of the fact that all superdevelopments are finite. It is similar to the proof of finite developments in section 7.3. We define a set SV of underlined À-terms by induction. In this case we make use of the expansion looking at the cases 1) and 2) of creating new redexes. We prove that the terms in SV are 11-strongly normalising. Finally, we write a morphism -

(At, -+131) __!!.., (SV, -+~)

Definition 7.6.1. The setSVis defined by induction as follows.

1. x E SV for all variables x,

2. if ME SV, then >.x.M E SV,

3. if ME SV and NE SV, then M N E SV,

4. if M[x N]P1 ... Pn E S'D and NE S'D, then (Àx.M)N H ... Pn E S1J,

5. if (~.M)N P1 ... Pn E S'D, then (Àx.x)(~.M)N P1 ... Pn E SV.

7.6. FIRST PROOF OF FINITENESS OF SUPERDEVELOPMENTS

The notion of context C[ ] with holes is defined simila.r to definitîon 5.2.8. The ,{!-rewrite relatîon is defined as follows.

C[(l!_x.M)N] C[M[x := N]]

where C[ ] has only one occurrence of [ ].

Lemma 7.6.2. If M, N E SV then M[x N] E SV.

Lemma 7.6.3. (~-Closure) Let ME SV. If M -+!!. M' then M' E SD.

73

Lemma 7.6.4. Let M = PQ with P E SD and Q E SV. If M-P M', then M' = P'Q' ~P~P~Q~~ -

Theorem 7.6.5. If ME SD, then all ~-rewrite sequences startingat M a.re finite.

Proof: The proof proceeds by induction on the derivation of ME SV.

1. If M is a variabie then it is trivial.

2. Let M = >.x.P with P E SV. By induction hypothesis, we have that Pis strongly ~-normalising. So M is strongly ~-norma.Hsing.

3. Let M = PQ with P E SD and Q E SV. By induction hypothesisPand Q are ,8-strongly normalising. It follows from lemma 7.6.4 that any ,8-sequence starting at M is finite. -

4. Let M = (à_x.P)QN1 •.. N .. with P[x := Q]N1 •.. N.,. E SD. Consider an arbitrary ,8-rewrite sequence p: M = Mo -(3 Mt -(3 M2 -{3 .. .. There are two possibilities: h>. p the head redex of M is contraëted or the headredex of M is not contracted.

In the first case, there is ani such that M; = P'[x := Q1]Ni ... N~ with P -{3 P', Q -!!.. Q',N1 -!!. Nf, ... ,N", -!!. N~. -

The term M; is obtained by rewriting P[x := Q]N1 ••• N,. and the latter term is by induction hypothesis strongly ~-normalising. Hence p is finite.

In the second case, all terms in p are of the form (à_x.P1)Q1 Ni ... N~ with P -+

-+p P',Q -(3 Q',N1 -{3 N:, ... ,Nn -/3 N~. Since P[x := Q]N1 ... N .. and Q are by -in duetion-hypothesis strongly ,8-normalising, we have that P, Q, N1 , ..• , Nn are strongly ,8-normalising. So all ter;s in p are strongly ,8-normalising and hence p is finite. - -

5. Let M = (à_x.x)(&J.N)PNt ... Nn with (&J.N)PNt···Nn E SV. Consider an arbitrary ,8-rewrite sequence p : M = M0 -{3 M1 -(3 M2 -/3 .... There are two possibiliÏies: in p the head redex of M is contracted Ör the heäd redex of M is not contracted.

74

0

CHAPTER 7. DEVELOPMENTS AND SUPERDEVELOPMENTS

In the first case, there is an i such that M; = (&;.N')P' N{ ... N~ with N -(3

N',P -(3 P',NI -(3 N{, ... ,Nn -(3 N~. The term M; is obtained by rewriting the term(&; .N)P NI-:- .. Nn and the Iätter term is by induction hypothesis strongly ~-normalising. So M; is strongly ~-normalising and herree p is finite.

In the second case, alltermsin p are of the form (!:,.x.x)(&;.N')P'N{ ... N~ with N -!!.. N',P -!!.. P',NI-f!_ N{, ... ,Nn -!!.. N~. By induction hypothesis, (&; .N)P NI ... Nn is strongly ~-normalising.

Herree N, P, NI, ... , Nn are all strongly ~-normalising. This yields that p is fini te.

We define a set llw of 'liberal' underlined À-terms.

Definition 7.6.6. The set llw is the smallest set satisfying the following.

1. x E llw for every variabie x,

3. if M E /1w then (!:,.x.M) E /1w,

4. if ME llw and NE /1w, then MN E /1w.

We define a mapping (-)* : SV -+ llw that underlines one special À of a term. lf M -(3 Àx.M0 then the value M* is obtained from M by underlining the À that descends to the-head lambda in Àx.M0 •

Definition 7.6.7. Wedefine (-)*: SV-+ llw by induction on the length ofthe maximal ~-rewrite sequence to normal form.

(Àx.M)* = !:,.x.M

((!:,.x.M)NPI·· .Pn)* = (!:,.x.M')N'P{ .. . P~ if (M[x := N]PI .. . Pn)* = M'[x := N']P{ ... P~

M* = M otherwise

Lemma 7.6.8. lf M,N E SV then M*N E SV.

Proof: Let M E SV. Then M is either of the form (xUI ... Un), or (Àx.P)UI ... Un or (!:,.x.P)QUI ... Un. We proceed by induction on the length of the maximal ~-rewrite sequence to normal form.

1. Let M = (Àx.P). Then (Àx.P)* = !:,.x.P. By lemma 7.6.2, we have that P[x := N] E SV. Herree (Àx.P)* N E SV.

7.6. FIRST PROOF OF FINITENESS OF SUPERDEVELOPMENTS

0

2. Let M = xUt···Un. Then M* =Mand MN E S'D.

3. Let M = (>..x.P)QU1 ••• Un. Then M* = M and M N E S'D.

4. Let M = (f1x.P)QU1 ••• Un. By induction hypothesis, we have that

(P[x := Q]U1 ... Un)* N E S'D. Herree M* N E S'D.

Lemma 7.6.9. Let ME S'D. If M =f:. (xN1 ... Nn) then (M[x :=PI)*= M*[x := P].

75

The proof proceeds by induction on the length of a maximal ,8-rewrite sequence to normal form. -

Definition 7.6.10. The mapping u: At-+ Aw is defined as follows.

u{x) = x

u(À;x.M) = Àx.u(M)

{

u(M)* u(N) if nfp1(M) = À;x.M0 and iE X u(@x (M, N)) =

u( M) u( N) otherwise

Theorem 7.6.11. Let ME At. Then u(M) E S'D.

The previous theorem is proved by induction on ME At and using lemma 7.6.8.

Lemma 7.6.12. Let M E A,. If u(M) = (xN1 ... Nn) then nf p1(M) is of the form (xP1 ••• Pn) forsome terms P1, ... , Pn.

This lemma is proved by induction on ME A,.

Lemma 7.6.13. Let @x (M, N) E A,. Then u(M[x := N]) = u(M)[x := u{N)].

Proof: The proof proceeds by induction on M E At. We prove only the case of the application M = @x(P,Q) with nfp1(P) = À;x.P0 and iE X.

It follows from lemma 7.5.10 and the fact that the terms are weli-labelled that

nfp1(P) = À;x.Po if and only if nfp1(P[x := N]) = À;x.Po[x := nfp1(N)].

Therefore

u(@x(P,Q))[x := u(N)] u(P)* u(Q)[x := N] by definition 7.6.10 = u(P[x := N])* u(Q[x := N]) by lemmas 7.6.9 and 7.6.12 = u(@x(P, Q)[x := N]) by definition 7.6.10

0

76 CHAPTER 7. DEVELOPMENTS AND SUPERDEVELOPMENTS

Theorem 7.6.14. Let ME At. If M -+p1 N in At then u(M) -+(!_ u(N) in S1J.

This theorem is proved by using lemma 7.6.13.

Theorem 7.6.15. The mapping u is a morphism from (At, -+p1) to (S1J, -+(!_)·

The proof follows from theorems 7.6.11 and 7.6.15.

Corollary 7.6.16. (Finiteness of Superdeveloments) All superdevelopments are finite.

7. 7 Second Proof of Finiteness of Superdevelopments

Another proof of the fact that all superdevelopments are finite can be given in a way similar to the one in section 7 .4. We define a morphism

Let App denote a distinguished variable.

Definition 7. 7 .1. The mapping J : At -+ SN is defined as follows.

J(x)

J(À;x.M)

x

Àx.J(M)

{

J(M)J(N) J(@x (M, N)) =

AppJ(M)J(N)

if nfp1(M) = À;x.M and iE X

otherwise

Lemma 7.7.2. Let ME At, J(M) E SN and NE S.N. Then J(M)[x := N] E S.N.

Proof: The proof proceeds by induction on the derivation of J(M) E S.N.

1. Suppose J(M) = yP1 ... Pn with P; E S.N for i = 1, ... , n. If n > 0 then y = App. By induction hypothesis, J(P;)[x := N] E SN for i = 1, ... , n. Hence J(M)[x := N] E S.N.

2. Suppose J(M) = .\y.P with P E SN. Using induction hypothesis we obtain that J(M)[x := N] E S.N.

3. Suppose J(M) = (.\y.P)Q1Q2 ... Qn with P[y := Q1]Q2 ... Qn E S.J.! and Qt E S.J.!. By induction hypothesis, we have (P[y := Q1]Q2 ... Qn)[x := N] E S.N and Q1 [x := N] E S.J.!. This yields J(M)[x := N] E SN.

7. 7. SEOOND PROOF OF FINITENESS OF SUPERDEVELOPMENTS 77

0

Lemma 7.7.3. Let ME A1, J(M) E SN and NE SN. Then J(M)N E SN.

Proof: The proof proceeds by induction on the derivation of J(M) E SN.

1. Suppose J(M) = xP1 ... P., with P; E SN for i = 1, ... , n. Then J{M)N E SN.

2. Suppose J(M) = >..x.P with P E SN. Then M >..;x.M0 and J(Mo) = P. By the

0

previous lemma we have P[x := N] E SN. Hence J(M)N E SN.

3. Suppose J(M) = (>..x.P)Q1Q2 ... Qn with P[x := Q1]Q2 ... Qn E SN and Q1 E SN. By induction hypothesis, we have P[x := Q1]Q2 ••• Q,.N E SN. Moreover Q1 E SN, henceJ(M)N E SN.

Lemma 7.7.4. Let @x(M,N) E A,. Then J(M)[x :=J(N)] =J(M[x := N]).

This lemma is proved by induction on M E A,.

Theorem 7.7.5. Let ME Az.

1. J(M) E SN.

2. If M --+p1 N in A1 then J(M) --+p J(N) in SN.

Proof: The proof of the first part proceeds by induction on M E Az and makes use of the lemmas 7 . .7.2 and 7.7.3. The second part uses lemma 7.7.4. 0

Corollary 7. 7 .6. The mapping J is a morphism from ( A1, --+ p1) to (SN, --+ p).

Theorem 7.7.7. --+ p1 is strongly normalising.

This theorem follows from lemma 2.6.5 and corollary 7.7.6.

Corollary 7.7.8. (Finiteness of Superdeveloments) All superdevelopments are finite.

78 CHAPTER 7. DEVELOPMENTS AND SUPERDEVELOPMENTS

7.8 Conclusions and Related Work

The result that all ;3-developments are finite is a classica! result in À-calculus and various proofs already exist. Church and Rosser proved finiteness of developments for the .U-calculus with ;3-reduction in [CR36]. The first proof for the full À-calculus is given by Schroer in [Sch65]. Other proofs have heen given in [Hyl73] and [Bar85]. In [Klo80], finiteness of developments is proved from strong normalisation for a ;3-reduction with 'memory' [Ned73] (see also section 11.5). There is a short a.nd elegant proof by de Vrijer [Vri85], in which an exact bound for the length of a development is computed. For proving that the bound is an exact bound, he makes in fact use of the strategy F00 • Another proof can he found in [Par90] (see also [Kri93]) that uses strong normalisation of the simply typed lambda calculus with intersection types. In this proof a morphism from the set of underlined lambda terms to the simply typed lambda calculus with intersection types is defined simila.r to our morphism used in the second proof of finiteness of developments (see definition 7.4.1). A similar proof using strong normalisatio~ of simply typed lambda calculus appears in [Ghi94]. In [Mel96] an axiomatic and general proof of finiteness of developments is given.

In [Raa93] the proof that the superdevelopments are finite uses the metbod of minimalisation.

Superdevelopments are related to the so-called 'generalised ,8-reduction'. The generalised ;3-reduction first appers in [Ned73] (see also [KN95]) as a natura! generalisation of the ;3-reduction intheitem notation. A ,8-redex in item notation is a 6-item (a.n application) foliowed by a À-item (a.n abstraction) like if they were a pair ()of parentheses. The notion of ;3-redex is generalised to include more complicated structures of parentheses like (()). For exa.mple, a generalised ;3-rewrite step (or ;39-rewrite step) in our notation is the following:

(>.x.>.y.M)N P _.,Pg (Àx.M[y := P])N

If we underline the ;39 -redexes, we get the underlined ;3-redexes of a superdevelopment. An way of labelling alternative to the one presented in [Raa93] (see definition 7.5.5) is to represent the term in item notation and to mark all the li and À-items that match as if they were parentheses.

Chapter 8

Simply Typed Lambda Calculus

8.1 Introduetion

In this section we give a new proof of the fact that the simply typed .X-calculus is /1-strongly normalising. In the proof we make use of the chara.cterisation of the strongly normalising .X-terms.

This chapter is organised as follows. In section 8.2 we reeall the definition of the simply typed lambda calculus à la Church. In section 8.3, we give a new proof of the fact tha.t the simply typed lambda calculus is strongly normalising.

8.2 Simply Typed À-calculus

In this section we shortly reeall the definition of simply typed .X-calculus.

Definition 8.2.1. The set Type is defined as follows.

1. 0 E Type,

2. if rE Type and a E Type then (r ~a) E Type.

Types are written as r, (]', . . .. We write r1 ~ r2 ~ r3 instead of ( r1 ~ ( r2 ~ r3 )).

Note that a type r is always of the form r1 ~ ••• ~ Tn ~ 0. We assume that we have an infinite number of variables, V.. = { v", v'" ... } for each

rE Type. The set V of variables is UreType V... A variabie that ranges on V.. is denoted by x,y, .. .. lf x E V.. and y E Vu with r i=(]'

then x::/: y.

Definition 8.2.2. Wedefine the family {A .. }re7ype of subsets of A as follows.

79

80 CHAPTER 8. SIMPLY TYPED LAMBDA CALCULUS

1. V.. C A-r,

2. if ME A-r_.." and NE A"' then (M N) EAu,

3. if M E Aa and x E V.. then >..x.M E A7 _.17 •

Definition 8.2.3. The simply typed lambda calculus>.."' (or >.. .... )is the abstract rewri.ting system with typing defined by

(A,-->13,{(M,r) I ME A"'})

Substitution and ,8-reduction are defined as in chapter 5.

Lemma 8.2.4. (Substitution Lemma) Let x E V... If M EAu and N E A,. then M[x := N] E A17 •

Definition 8.2.5. If A C A"' and B C Au, we define

A--> B {ME A-r .... u I VN E A: MN EB}

Note that if Ac A' then A'--> B cA-tB and that if B c B' then A--> B CA--> B'.

Proof: If M E A"'_,u and N E A7 then M N E Au. Conversely, if M E A"' -t Au, then (Mx) EAu for x EV... Therefore ME Ar-+u· D

8.3 Strong Normalisation

In this section we prove that the simply typed >.-calculus is ,8-strongly normalising.

Definition 8.3.1. The set S.N(r) is defined as follows.

S.N(r) =A_,. n s.N

Theorem 8.3.2. S.N(r--> a):> S.N(r) -t S.N(a).

Proof: Let ME S.N(r) -t S.N(a). We have that ME S.N, because MN E S.N(a). If (Mx) EAu for x EV.. then ME A,. ..... ". D

The converse inclusion is not so easy to prove. First we need the following lemma.

Lemma 8.3.3. Let NE S.Nh) -t ... -t S.N(r,.) -t S.N(O). lf P E S.N(a) and x E V..1 ........... _,." .... 0 then P[x := N] E S.N(a).

Proof: The proof proceeds by induction on the derivation of P E S.N.

8.3. STRONG NORMALISATION 81

0

1. Suppose P = yP1 ... Pk with Pb ... , Pk E S.N. By induction hypothesis, we have P;[x := N] E S.N for i= 1, ... , k. We write P;* for P;[x := N] for i = 1, ... , k.

If y =f:. x, then P[x := N] E S.N follows from the fact that P;* E S.N for i= 1, ... , k. Using lemma 8.2.4, we obtain P[x := N] E S.N(u).

If y = x, then we have to prove that N Pt ... P; E S.N(u). By the induction hypothesis and lemma 8.2.4, we have that P;* E S.N( r;) for i = 1, ... , k. Furthermore, NE S.N(r1)--+ ... --+ S.N(rn)--+ S.N(O) c S.N(ri)--+ ... --+ S.N(rk)--+ S.N(u), by theorem 8.3.2. Hence we have P[x := N] = N Pt ... Pk E S.N(u).

2. Suppose P = >.y.Po with PoE S.N. By induction hypothesis, we have P0 [x := N] E S.N. Therefore P[x := N] = (>.z.P0 )[x := N] E S.N(u).

3. Suppose P = {>.y.Po)P1P2 ... Pk with Po[Y := P1]P2 ... Pk E S.N and P1 E S.N. By in duetion hypothesis, we have (Po[Y := P1]P2 ... P~c)[x := N] E SN and P1 [x := N] E S.N. Hence P[x := N] = ((>.y.Po)P1P2 ... P~c)[x := N] E S.N(u).

Now we can prove the following theorem.

Theorem 8.3.4. S.N(r--+ u) C S.N(r)--+ S.N(u).

Proof: Let ME S.N(r--+ u). We prove that for all NE S.N(r), we have MN E S.N(u). Let NE S.N(r). Note that MN EAu. It remains to prove that MN E S.N. This is proven by induction on T and for each r by induction on the derivation of M E S.N.

T is 0. The proof of this ·part proceeds by induction on the derivation of M E S.N.

1. Suppose M = xM1 ... M~c with M1, ... , Mk E SN. We have N E SN because NE SN(r). This yields MN = xM1 ... M~cN E S.N.

2. Suppose M = >.x.P with P E S.N. We have that x E V,. and P E Au, so actually P E S.N(u). For proving (>.x.P)N E S.N, weneed to prove P[x := N] E SN. This follows from an application of lemma 8.3.3.

3. Suppose M = (>.x.Mo)M1M2 ... M~c with Mo[x := M1]M2 ... M~c E S.N and M1 E S.N. By induction hypothesis of the induction on the derivation of M E SN, we have M0 [x := M1]M2 •.. M~cN E S.N. Moreover M1 E S.N.

This yields (>.x.Mo)MtM2 ... MkN E S.N.

82 GRAPTER 8. SIMPLY TYPED LAMBDA CALCULUS

7 is a composed type. The proof of this part proce(lds as well by induction on the derivation of M E SN.

0

1. Suppose M = xM1 ••• M,. with M1 , ••• ,M,. E SN. Since NE SN, we have that MNeSN.

2. Suppose M = >.x.P with P E SN. For proving (>.x.P)N E SN, weneed to prove that P[x := N] E SN. We have 7 = 71 --+ ... --+ 7,. --+ 0. By the induction hypothesis of the induction on 7, we haveN E SN(71)--+ ... --+ SN(7.,)--+ SN(O). Lemma 8.3.3 yields tha.t P[x := N] E SN.

3. Suppose M = (>.x.Mo)MtM2 ... M,. with Mo[x := Mt]M2 ... M,. E SN a.nd Mt E SN. By induction hypothesis of the induction on the deriva.tion of M E SN, we have Mo[x := Mt]M2 ••• M,.N E SN. Moreover M1 E SN. This yields M N E SN.

Corollary 8.3.5. SN(7--+ u)= SN(7)--+ SN( u).

Theorem 8.3.6. (Strong Normalisatión for >. .... ) For all 7 E Type, if ME AT then ME SN(7).

Proof: The proof proceeds by induction on the deriva.tion of ME Ar.

0

1. Suppose x E Arthen x E SN(7).

2. Suppose M = >.x.P E A" .... "' with x E Vu a.nd P EAu'· By induction hypothesis, we have P E SN(u'). This yields (>.x.P) E SN( u--+ u').

3. Suppose M PQ E AT. Then P E Au-r and Q E Au. By induction hypothesis, P E SN( u--+ 7) and Q E SN(u).

By the previous theorem we have SN( u--+ 7) =SN( u)--+ SN(7).

Therefore PQ E SN(7).

8.4 Conclusions and Related Work

An interesting proof of the normalisation of the simply typed lambda calculus is the one given by Tait in [Tai67]. Tait defined the class of computable terms (or reducible terms). Using Tait's method one can also provestrong normalisation for the simply typed lambda calculus and some of its extensions, like Gödel's T [Tro73]. Girard [Gir72] introduced the concept of candidate of reducibility to generalise Tait's method to include polymorphism

8.4. CONCLUSIONS AND RELATED WORK 83

(for the systems F to Fw). For an explanation of the method and applications to prove confiuence see [Gal90].

The conditions in the definition of candida.te of reducibility in [Gir72] were modified in [Ta.i75] and [Mit86] and a new definition was introduced: a saturated set. The definitions of the set SN and of satura.ted sets are very similar (see chapter 5).

Our proof differs from the proof by Tait in the fact that in his method an iriterpretation for types is used. A type T is interpreted as a set of À-terms and denoted by [r]. Then, the interpretation of a type T-+ o- is defined to be [r)-+ [al So [r-+ u] = [r]-+ [o-] by definition. In our proof the equality SN(r-+ o-) SN(r)-+ SN(o-) needs to be proved. On the other hand, in Tait's method one has to prove that (r)-+ (o-J is a subset of the set of strongly normalising terms. In our proof, the set SN(r -+ o-) is a subset of thesetof strongly normalising terms by definition.

In [Vri87] strong normalisation for the simply typed lambda calculus is proved by giving a function that computes the length of the maximal rewrite sequence and by implictly following the strategy F 00 of maximal length. In our proof we make use of the set SN which also implicitly uses this strategy.

It seems that the metbod used here ca.nnot be extended to Gödel' s T nor to the combina.tory version of>._. Our metbod does not extend because it fails in theorem 8.3.4 which is proved by induction on the structure of the type. The type of the newly created redexes may be more complex and thus it is not possible to apply the induction hypothesis. This seems related to the proof of normalisation for the simply typed lambda calculus by Turing [Gan80]. In our opinion, this method, being too simple, will not be ea.sily extensible.

lt is possible to express the proof of strong normalisation of the simply typed lambda calculus in Peano Arithmetic (PA). On the other hand, strong normalisation for Gödel's T cannot be proved in PA and the system F cannot be proved in PA2•

A topic for further investigation is to implement our proof in a proof checker like Coq, Legoor Alf.

84 GRAPTER 8. SIMPLY TYPED LAMBDA GALGUL US

Part 111

Pure Type Systems with Definitions

85

Chapter 9

Pure Type Systems

9.1 Introduetion

Pure type systems provide a way to descri he a large class of type systems à la Church in a uniform way. They were introduced independently by S. Berardi [Ber88] (see also [Ber90]) and J. Terlouw [Ter89]. Important pure type systems are the systems of the À-cube [Bar92]. They are called 'pure' because there is only one type constructor and only one reduction rule, namely the type constructor II and the ,8-reduction.

This chapter is organised as follows. Insection 9.2 we reeall the notion of specification and of morphism between specifications. In section 9.3 we reeall the definition of pure type systems.

9.2 Specifications

In this section wedefine the notion of specification. The specifications are 'the parameters' in the definition of pure type systems.

Definition 9.2.1. A specification is a tripleS (S, A,R) such that

1. S is a set of syrilbols called sorts,

2. A ç; S x S called set of axioms,

3. R ç; S x S x S called set of rules.

Sorts are denoted by s, s', ... , St, s2, .... The set of axioms and the set of rules are used in the typing rules of pure type systems

(see section 9.3). The set A determines the axioms and the set R determines all 'the functions' we can form in the system.

87

88 GRAPTER 9. PURE TYPE SYSTEMS

Example 9.2.2. We give two examples of specifications. These specifications will make sense in section 9.3 after introducing the typing relation.

1. The specification P RED is defined as follows.

The sort *P is for propositions, the sort *8 is for sets and *' is for first order fundions between the sets in *·· The rule (*~',*P,*P) allows the formation of impHeation between propositions, the rule ( *8

, *P, *") allows quantification over sets, the rule ( *8

, OP, OP) allows the formation of first order predicates, the rule (*I, *8, *') allows

the formation of function spaces between the basic set *8 and the rule ( *•, *f, *f) allows the formation of curried functions of several arguments in the basic set.

2. The specification P is defined as follows.

S {*,0} P A {(*,0)}

R {(*,*•*),(*,o,o)}

The sort * is used for types and the sort 0 is used for kinds. The rule ( *• *• *) allows the formation of types and (*, o, 0) allows the formation of kinds.

Definition 9.2.3. Wedefine a morphism from the specification S = (S, A,R) toS' ( S', A', R') as a Junction f : S -+ S' that satisfies the following conditions.

1. If (s1,s2) E A then (f(st),J(s2)) E A'.

2. If (s17s2,s3) ER then (f(st),j(s2),f(s3)) ER'.

We denote the category whose objects are the specifications and morphisms the ones defined above by Spec.

Several examples of morphisms between specifications are given in [Geu93] and [Bar92].

Example 9.2.4. An important example of morphism between specifications is the following one used for the propositions-as-types interpretation.

Wedefine p: { *", *P, *!,os, OP}-+ { *• 0} as follows.

p( *•) * p(o•) o P(*P) = * p(OP) = 0

P(*f) = * It is easy to see that pis a morphism from PRED toP.

9.3. PURE TYPE SYSTEMS 89

Definition 9.2.5. Let S = (S, A, R) he a specification. A sort sin Sis called a topsort if there is no s0 E S such that (s, s0 ) E A.

Definition 9.2.6. Let S = (S, A, R) he a speci:fication. The specification S is called singly sorted if

1. (st, sz), (st, sa} E A implies sz =sa

2. (sbsz,sa),(st,Sz,s4) ER implies sa= s4.

Definition 9.2.7. LetS= (S,A,R) he a speci:fication.

The speci:fication S = (S, A, R) is called full if R = {(s1,s2 , s 2 ) I St,Sz E S}.

The speci:fication S = ( S, A, R) is called semi-full if for all (St. s2 , sa) E R a.nd s~ E S there exists s~ E S such that (St, s;, s~) E R.

Definition 9.2.8. Let S = (S, A, R) be a specification.

The specification S is called logica[ if it verifies the following conditions.

1. The set S contains two distinguished sorts * a.nd 0.

2. The set A contains the axiom (*, 0).

3. The set R contains the rule ( *• *• * ).

4. There is nosort s such that (s, *) E A.

The specifica.tion S is called non-dependent if it is logica.l a.nd the only rules concerning * are of the form (s, *• *) for some sort s.

The specification S is called impredicative if it is logica! and (0, *• *) E R.

9.3 Pure Type Systems

We present the notion of pure type systems in a slightly differènt way than usual. We first define a functor >. from the category of specifications to the category of abstract rewriting systems with typing. We think tha.t the presentation of pure type systems is more neat in this way.

A pure type system is a value >.(S) of À given by a 4-tuple:

1. a set T of pseudoterms,

2. a set C of pseudocontexts,

3. a reduction relation on pseudoterms, called ,8-reduction,

4. a typing relation denoted by 1-.

90 GRAPTER 9. PURE TYPE SYSTEMS

First we define the components of this 4-tuple. Pseudoterms are expressions formed with a constructor À for the abstractions, the brackets ( ) for the application and the constructor rr for the product (function space).

Definition 9.3.1. The set 7s (or 7 for short) of pseudotermsis defined as follows.

7 ::=V I sI (7 7) I (.\V:7. 7) I (ITV:T. 7)

where V is a set of variables and S is the set of sorts.

Variables will be denoted as x, y, z, ... , a, (3, Î.... Pseudoterms will he denoted as a, b, c, d, ... , A, B, C, . . .. The usual parenthesis conventions for abstraction, application and product will be used (see [Bar92]).

Definition 9.3.2. The mapping FV : 7 -+ 'P(V) is defined as follows.

FV(x) = {x} FV(c) = 0

FV(a b) = FV(a)UFV(b) FV(.\x:A. a) = FV(A) u (FV(a)- {x}) FV(ITx:A. a) = FV(A) U(FV(a)- {x})

We say that x is free in a if x E FV(a).

Definition 9.3.3. The mapping BV : 7 -+ P(V) is defined as follows.

BV(x) BV(c)

BV(a b) BV(.\x:A. a) BV(ITx:A. a)

= =

= =

0 0 BV(a) U BV(b) BV(A) U (BV(a) u {x}) BV(A) U (BV(a) u {x})

We say that x is bound in a if x E BV(a).

Definition 9.3.4. The result of substituting d for (the free occurrences of) x in e is denoted as e[x := d] and is defined as follows.

s[x := d] x[x dj y[x dj

(Àx:A. a)[x dJ (Ày:A. a)[x :=dJ (Ày:A. a)[x :=dJ

(a b)[x :=dJ (ITx:A. a)[x :=dj (ITy:A. a)[x := d] (ITy:A. a)[x := d]

=s =d =y = (Àx:A. a) = (.\y:A[x :=dj. a[x :=='dj)

(.\z:A[x :=dj. a[y := z][x :=dj) = (a[x :=dj b[x :=dj) = (Ilx:A. a) = (ITy:A[x dJ. a[x :=dj) = (ITz:A[x := d]. a[y := zl[x := d])

if x :f y and y f/. FV(d) if x :f y, y E FV(d) and z is fresh

if x :f y and y f/. FV( d) if x :f y, y E FV(d) and z is fresh

9.3. PURE TYPE SYSTEMS 91

The set of pseudoterros with holes in it is defined by the following grammar.

Definition 9.3.5. The set H is defined as follows.

H :::::: 1:11 V I sI ('H. H) I (ÀV:'H.. H) I (TIV:'H.. 1i)

where V is the set of variables and S is the set of sorts.

An element in 1i is denoted by C[ ]. Pseudocontexts are lists of pairs consisting of one variabie and one pseudoterm. The

pseudocontexts are used in the definition of the typing relation in order to assign types to the variables.

Definition 9.3.6. The set Cs (or C for short) of pseudocontextsis defined as follows.

c ::= f I < c, V:T >

Pseudocontexts will he denoted as r, I", ... , L.l, L.l', .... The expression r,x:A stands for < r,x:A >. Next we define a mapping Dom that gives the set of variables declared in a pseudocon

text.

Definition 9.3.7. The mapping Dom: C-+ 'P(V) is defined as follows.

Dom(e.) = 0 Dom(r, x:A) = Dom(r) u {x}

Definition 9.3.8. The result of substituting d for (the free occurrences of) a. variabie x in r such that x ~ Dom{r) is denoted as r[x := dj and is defined as follows.

e.[x :=dj e. < r,y:A >[x:= dj =< r[x := dj,y:A[x :=dj>

Definition 9.3.9. Let d E T. A change of a bound variabie in the term d is the reptacement of a subterm (Àx:A. b) or (Tix:A. b) by (-\y:A. b[x := y]) or (Tiy:A. b[x := y]), respectively, where y (; FV(b).

Definition 9.3.10. The pseudoterm b is a-convertible to b' if b' is the result of applying to b a series of changes of variables or vice versa.

Convention 9.3.11. Two terms are identified if they are a-convertible.

Definition 9.3.12. The (J-reduction is defined by the following rule:

C[(Àx:A. b)a]-+p C[b[x :=a]]

where C[ ] E 1i bas only one occurrence of IJ.

92 GRAPTER 9. PURE TYPE SYSTEMS

Definition 9.3.13. The typing relation 1-s (or 1- for short) is the smallest relation on C x T x T closed under the following rules.

(axiom)

(start)

( weakening)

(! ormation)

( abstraction)

( application)

(conversion)

r,x:AI-x:A

rl-b:B ri-A:s r,x:AI-b:B

r 1- A: St r,x: A 1- B: Sz

r 1- (IIx:A.B) : S3

r, x: A 1- b: B r 1- (IIx:A. B) : s r 1- (.-\x:A. b) : (IIx:A. B)

r 1- b : (Ilx:A. B) r 1- a : A r 1- (b a): B[x a]

rl-b:B B B'

for (st, s2) E A

where x is r ·fresh

where x is r-fresh

where s ranges over sorts, i.e. s E S.

Explanation of the typing rules. Variables are typable by means of the start rule, abstractions by means of the abstraction rule, a.pplications by means of the application rule and the product by means of the formation rule.

The start a.nd the weakening rules allows to erilarge the context. They ensure that all the components of a pseudocontext are typable. Besides they do not allow the repetition of variables in the context since a variabie is added to the context only if it is fresh. One consequence of this is that we cannot have two nested abstractions with the same bound variable.

Note that the typing rules have two 'parameters'. One parameter is the set A in the first rule which determines the set of axioms we have in the system and the other is the set R in the formation rule which determines the products and hence the abstractîons we can form in the system. When we fix the triple (S, A, R), we ohtain a partienlar typing system.

The conversion rule ensures that types are 'closed under rednetion and expansion'.

Definition 9.3.14. The functor >.: Spec-+ Carst is defined as follows .

.-\(S) = (T,C,-+13,1-) SE Spec

f : S -+ S' E Spec

9.3. PURE TYPE SYSTEMS 93

The functions ]0 and }1 are the extension of f to the set of pseudoterms and pseudocontexts respectively.

]o(8) = f(8) ] 0 (Àx:A. b) = >.x:}o(A). ]o(b) }o(IIx:A. B) = ITx:}o(A). ]o(B) ]o(F a) = (}o(F) ]o(a))

ft(f) = €

}t(r, x:A) = ft (r), x:]o(A)

There are several things that we need to verify. Amongst the properties of pure type systems below, we list subject and type reduction. This means that (T, C, -+f3, 1-) E Carst. Moreover the function >.(!) is a morphism in Carst sirree it preserves the rewrite and the typing relations.

Definition 9.3.15. (Pure Type Systems)

• A pure type 5y5tem (PTS) is an element of >.(Spec) = {)..(S) I SE Spec }.

• A 8ingly 5orted pure type 5y5tem is an element of { >.( S) I S E Spec & S is singly sorted}.

For example, >.(PRED) and >.(P) are pure type systems and >.(p) is a morphism from >.(P RED) to >.(P).

The À-cube consists of eight systems [Bar92] defined by the same set of sorts and the same set of axioms. They differ in the set of rules R.

Definition 9.3.16. Let 8 0 = { *• D} and A 0 = {( *, D)}. We will write So ® So for {(8t,82,83) I 82 = 83 & 81,82,83 E So}. The À-cube is a mapping from P(So ® 8 0 ) into Carst defined as follows.

Sy5tem .R

).._. (*,*) )..2 (*,*) (D,*) )..p (*,*) (*, D)

>.P2 (*,*) (D, *) (*, D) À!:!L (*, *) (D,D) Àw (*,*) (D, *) (D,D)

>.P!:!L (*, *) (*, D) (D,D) >.Pw=>.C (*,*) (D,*) (*, D) (D,D)

94 GRAPTER 9. PURE TYPE SYSTEMS

The systems of the .À-cube correspond to some known systems with some variations: .À__, is the simply typed làmbda calculus [Chu40), >.2 is the second order typed lambda calculus [Gir72] and [Rey74], >.Pis AUT-QE and LF [Bru70] and [RHP87]. The system Àw is POLYREC and Àw is Fw [Gir72]. The last element in the table, >.C, corresponds to the Calculus of Constructions.

All the systems in the .À-cube have only one topsort, narnely D.

Definition 9.1. The system of higher order logic can he described by the following specification (see [Geu93]).

s {*, o, .6.} HOL A {(*,D),(o,.6.)}

R {(*,*),(o,*),(D,D)}

There is only one topsort in >.(HOL) and that is .6..

Definition 9.2. The Calculus of Constructions extended with an infinite type hierarchy can he described by the following specification.

S IN 0 09 A { ( n, n + 1) I n E IN}

R {(m,O,O) I mE IN}U {(m,n,r) I m,n E IN & max(m,n):::; r}

The system .ÀÜ00 extended withstrong 2::-types· and cumulativity is the system ECC (see [Luo89]). We can see that >.Coo is an extension of >.C and of >.(HOL) writing * insteadof 0, D instead of 1 and .6. instead of 3. Note that there is no topsort in.ÀCw

Definition 9.3.17. LetS be a logical specification. The pure type system >.(S) is said to he inconsistent if for all A, there exists a such that a has type A in the context A : *·

For example, an inconsistent pure type system is À* determined by the specification (S,A,R) where S ={*},A={(*,*)} and R= {(*,*)} (see [Gir72] and [Bar92]).

Properties of Pure Type Systems

The advantage of pure type systems is that we can do the metatheory of several type systems at the same time. There are several properties that can he proved for all these systems.

9.3. PURE TYPE SYSTEMS 95

Theorem 9.3.18.

1. (T, -p) is confluent

2. Correctness of types, i.e. if r f- d : D then there exists a sort s such that r 1- D : s or D = s for all r E C, d, D E T.

3. Subject reduction, i.e. if r f- d: D. and d -+p d' then r 1- d': D for all d, d', D ET and rEC.

4. Type reduction, i.e. if r 1- d : D and D -+p D' then r 1- d : D' for all d, D, D' E T and rEC.

5. Strengthening, i.e. if r1tx:A,r2 1- b: Band x~ FV(r2 ) U FV(b) U FV(B) then r1,r2t- b: B.

6. Let S be a singly sorted specification. Then A(S) verifies uniqueness of types.

7. Let S be a logica! specification. If A(S) is inconsistent then A(S) is not normalising.

The proofs of these propertiescan he found in [GN91) and [Bar92) except for strengthening that is proved in [BJ93).

Theorem 9.3. (Strong Normalisation)

1. The system À000 is ,8-strongly normalising.

2. The systems of the À-cuhe are ,8-strongly normalising.

3. The system A(HOL) is ,8-strongly normalising.

Proof: The system ECC is strongly normalising (see [Luo89)) and contains À000 , the systems of the À-cuhe and >..(HOL). D

There are pure type systems that are not normalising, e.g. the system À* which is inconsistent (see theorem 9.3.18 part 7).

Theorem 9.3.19. (Decidability of Type loferenee and Type Checking) LetS (S,A,R).

1. If Sis finite then type inference and type checking in >..(S) are decidahle.

2. Suppose tha.t S is singly sorted or semi-full and that the sets S, A and R are recursive. Then type inference and type checking in A( S) are decidahle.

The proof of the first part can he found in [BJ93) and the second one in [BJMP93] and [Pol96].

96 CHAPTER 9. PURE TYPE SYSTEMS

Theorem 9.3.20. (Undecidability of Type Inference and Type Checking) Let S = ( S, A, R) he a singly sorted, non-dependent and irnpredicative specification.

If .\(S) is inconsistent then type inference and type checking in .\(S) are undecidable.

This is proved in [CH94] as a generalisation of the result of [MR86]. The problern of inhabita.tion in À .... and in À!& are decidabie but in the rest of the systerns

of the cube is undecidable [Spr95]. Fortheinconsistent systerns, inhabitation is trivial.

9.4 Conclusions and Related Work

As we said before, pure type systerns were introduced independently by S. Berardi [Ber88] and J. Terlouw [Ter89]. In the typing rules for pure type systerris, the axiom depends on the set A and the product rule depends on the set R. In other words, we have a family of axioms and product rules depending on A and R. The typing rules are parametrie and the parameters are the specifications, i.e. triples of theform (S, A, R). The dependency on the specification is expressed with the notation .\(S) for the pure type system conesponding to the specification S (S, A, R). We made a 'slight change' in the definition of pure type systems. We bav~ written the functor À : Spec-+ Carst instead of 'giving a set of values .\(S)'. We could think that now we have given 'a house' (or perha.ps 'a. type') for À

to live in a.nd this house is the category of functors from Spec to Carst. Extensions of pure type systems are also defined as functors from the category of

specifications Spec into the category of environmental abstract rewriting systems with typing Carst. The category Carstspec of functors from Spec to Carst is an adequate 'place' to put the pure type systems and all their extensions. Comparison of pure type systems and their extensions is made via morphisms in the category of functors frorn Spec to Carst, i.e. via natural transforrnations.

Chapter 10

Type Inference for Pure Type Systerns

10.1 Introduetion

In this chapter we define a pa.rtial function that infers the type of a term in a singly sorted pure type system. If the term has type in a singly sorted pure type system then this function terminates and yields the type of the term (up to ,8-conversion). This function can be constructed if we have a set of typing rules that is synta.x directed. A set of rules is called syntax directed if the last rule in the derivation of a term is determined by the structure of the term and of the context. The rules of pure type systems are not synta.x directed since the wea.kening a.nd the conversion rules ca.n be applied at any point in the deriva.tion. In order to make this set of rules synta.x directed, we should remove the non-structural rul es (like the wea.kening and the conversion ones} a.nd keep only the structural rules (the ones for term constructors). The system obta.ined by elimina.ting the non-structura.l rules should be equivalent to the original one. The proof of the equivalence ( soundness and completeness) between the original rules a.nd the corresponding syntax directed ones is probiernatie forsome pure type systems (see [BJMP93] and also [Pol93a.]). Here we present a syntax directed set of rules for singly sorted pure type systems similar to the one presented in [BJMP93]. As in [BJMP93], we use an auxiliary system to check for the II-condition, i.e. the premise that the product (Ilx:A. B) should be well-typed in the abstraction rule. The auxiliary system we use to define the syntax directed set of rult;s is the corresponding pure type system without the II-condition. The convenience of this system is that it permits us to prove soundness and completeness of the original rules for pure type systems with respect to the synta.x directed ones.

This chapter is orga.nised as follows. In section 10.2, we define a functor Àw from the category of specifications to the ca.tegory of contextual abstract rewriting systems with weak typing. Wedefine the class of pure type systems without the II-condition as the image of Àw. A pure type system without the II-condition can he considered as an extension of

97

98 GRAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

the corresponding system with the Il-condition. The new termsof the extension are the abstractions (Àx:A. b) whose type (IIx:A. B) is not typable and applica.tions of the form (Fa) whose operator F has a type that is not typable. Hence we are led to consider types that are not typable. We call them toptypes. In section 10.3, we prove the basic properties of pure type systems without the II-condition, e.g. weak subject rednetion theorem. In section 10.4, we analyse the shape of a toptype. We give a characterisation of the set of toptypes and prove weak type reduction. In the case of singly sorted specifications we give another characterisation of thesetof toptypes and prove that it is closedunder substitution and /9-reduction. In section 10.5, we consider those /9-redexes whose abstraction has a type that is not typable and we call them illegal redexes. We introduce a mapping <p tha.t contracts all the illegal redexes of a term. Using cp we prove that weak normalisation is preserved by the extension from .X(S) to Àw(S) in the case that Sis singly sorted. Moreover cp can be used to define a morphism from .xw(S) to .X(S) for each singly sorted specification S. In section 10.6, we define the notion of /9,-reduction as the contraction of the illegal redexes. We prove that all the terms typable in a singly sorted pure type system are /9,strongly normalising. In section 10. 7, we define a syntax directed set of rul es for singly sorted pure type systems. Insection 10.8, wedefine a partial function type that computes the type of a term in a singly sorted pure type system based upon the syntax directed set of rules defined in the previous section.

10.2 Pure Type Systems without the II-condition

In this section wedefine the notion of pure type systems without the II-condition. Por pure type systems the type of an abstraction (.Xx:A. b) is a dependent product (IIx:A. B) and the abstraction rule has the premise that the product (fix: A. B) should be well-typed. This premise is called the II-condition. The problem with the II-condition is that sametimes it is not well-adapted to induction. The idea is to remove this condition in order to define a syntax directed set of rules and prove the equivalence of the original rules with the syntax directed ones.

Definition 10.2.1. The IT-condition is the premise r I- (ITx:A. B) :sof the abstraction rule in a pure type system.

Definition 10.2.2. Wedefine a functor Àw : Spec-+ Carstw such that for S E Spec we have that

.xw(s) (T,C, -+fJ, 1-"').

We de:fine the components of this 4-tuple as follows. The sets T, C and the relation -+fJ are defined as in definition 9.3.14.

The typing relation 1--w ( or 1-~) is the smallest relation defined by the same rules as in definition 9.3.14 except for the abstraction rule that is replaced by the following one.

( b . ) f,x:AI-wb:B

a stract~on r I-w (Àx:A. b) : (ITx:A. B)

10.3. BASIC PROPERTIES 99

The functor ,\w : Spec ~ Carstw is defined for morphisms in the obvious way. There are two things that remain to he verified, i.e. ,\"'(S) E Carstw and >.w(f) E

Carstw. For ,\w(S) E Carst,.., we have to prove that a pure type system without the II-condition verifies weak subject and weak type reduction. This wiJl he proved in the next sections. For >.w(f) E Carst .... , we have to prove that >.w(f) preserves the rewrite and the typing relation. This is very easy to prove.

Definition 10.2.3. (Pure Type Systems without the II-condition)

A pure type system witkout tke 11-condition is an element of the set

A singly sorted pure type system without the IT-condition is an element of the set

{,\"'(S) I SE Spec & Sis singly sorted}.

Note that if a term is typable in >.(S) then it is typahle in >.w(S). Therefore >.w(S) is an extension of >.(S).

The converse is not true. There may be terms typable in >.w(S) which are not typable in >.(S). For example, the type (A ~ D) is not typable in the context A : * either in ,\(G) or in ,\"'(C). However in ,\w(G) we can derive that the term Àx:A.(* ~ *} has type (A~ D) in the context A: *·

10.3 Basic Properties

In this section we prove the generation lemma., the thinning lemma a.nd the wea.k subject reduction theorem for pure type systems without the II-condition. Since correctness of types does not hold, we prove some weaker lemma.s tha.t we call correctness of contexts and correctness of types for varia.bles. Finally we prove uniqueness of types for singly sorted specifica.tions.

Lemma 10.3.1. (Generation Lemma)

1. If r I-"' s : D then there exists S1 such tha.t (s, s1

) E A a.nd D -{3 s1•

2. If r I-"' x : D then there are s, band B such that r I-"' B: s, x : BE rand B -P D.

3. If r 1-W (ITx:A.B) : D then there is a rule (st,S2,s3) E R such that r I-W A: sl, f,x:A 1-w B: s2 and D -P s3.

4. If r 1-W (>.x: A. b) : D then there are s and B such that r, x: A 1-"' b : B a.nd D -P (ITx:A. B).

5. If r I-W (b a) : D then there are A, B such that r I-W b : (ITx:A. B), r I-W a : A and D -P B[x :=a].

100 CHAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

All parts of the generation lemma are proved by induction on the derivation of a term.

Lemma 10.3.2. ( Correctness of Contexts) If r' x:A, r' 1-W b : B then there exists an s such that r 1-W A: s.

This lemma is proved by induction on the derivation of r, x:A, f' !-"' b: B.

Lemma 10.3.3. (Correctness of Types for Variables) If r 1-W x : A then there exists s such that r 1-W A : s.

This lemma is proved by induction on the derivation of r 1-W x : A.

Lemma 10.3.4. Let r 1-w ( .Àx:A. b) : D. Th en there exists a B such that r, x : A !-"' b : B. Moreover either D = (ITx:A. B) or r 1- D : s and D *"*fJ (llx:A. B) for some sort s.

This lemma is proved by induction on the derivation of r 1-W (>.x:A. b) : D.

Lemma 10.3.5. If r 1-W A : s and r 1- a: A then r 1- A: s1 forsomesort s1•

Pro of: By the lemma of correctness of types for pure type systems, we have that r 1- A : s" or A s11 forsome s11

• In case A= s", we have that (s",s) is an axiom. 0

Lemma 10.3.6. (Thinning Lemma) Let r'l-"' b: B.

If r f-W a : A and r ç;; r' then r' f-W a : A.

This lemma is proved by induction on the derivation of r f-W a : A.

Lemma 10.3.7. (Substitution Lemma) If r 1-W a: A and r,x:A,r' 1-W b: B then r, f'[x := a]f-W b[x := a] : B[x :=a].

This lemma is proved by induction on the derivation of r, x:A, r' !-"' b: B.

Lemma 10.3.8 .. (Correctness of Domains) lf r f-W F: llx:A. B then r 1-W A: s for some sort s.

This lemma is proved by induction on the derivation of r 1-w F: llx:A. B.

Theorem 10.3.9. (Weak Subject Reduction Theorem) Let rf-We : E.

a) lf e -?p e1 then there exists E' such that E -(iE' and rf-We': E'.

b) If r -?p r' then r' !-"' e: E.

Proof: The two statements are proved by simultaneous induction on the derivation of r 1-w e: E. We only consider the statement a) for the case of the application rule.

10.3. BASIC PROPERTIES 101

r f-W b : (IIx:A. B) r j-W a : A (application) r j-W (b a): B[x :=a]

There are several cases when we reduce the application (b a).

1. Suppose (b a) -+p (ll a) with b -+p b'. By induction we have tha.t r f-W b' : (IIx: A'. B') forA-PA' and B -PB'. By lemma 10.3.8 we have that r 1-w A': 8 for some sort 8. Therefore we ca.n apply the conversion rule and we obta.in r 1-w a: A'. Then we apply the application rule and we have a derivation of r j-W (b a) : B'[x :=

a].

2. Suppose (b a) -+p (b a') with a -+pa'. By induction we have that r f-W a': A' for A - 13 A'. By lemma 10.3.8 we have that r 1-w A: 8. Therefore we can a.pply the conversion rule and we have that r 1-w a' : A. Then we apply the application rule and we obtain a derivation of r 1-W (b a') : B[x :=a'].

3. Suppose (>.x:A'. d}a -+p d{x :=a]. By lemma 10.3.4 we have that r,x:A11-W d: B'. Moreover by lemma 10.3.4 we know there are two possibilities, either (IIx:A. B) = (IIx:A'. B') or (IIx:A. B) -/3 (IIx:A'. B') and r 1-w (IIx:A. B) : 8.

(a) Suppose (IIx: A. B) = (IIx: A'. B'). Hence A = A1 and B = B'. By the substitution lemma we have that r j-W d[x :=a]: B[x :=a].

(b) Suppose (IIx:A. B) -f3 (IIx:A'. B') a.nd r r-w (IIx:A. B): 8 forsome 8. By lemma 10.3.8 we have that r 1-"' A' : s' for some 8

1• Therefore we can apply

the conversion rule and we have that r 1-w a : A'. By the substitution lemma we have that r 1-W d[x a] : B'[x :=a]. Since r j-W (IIx:A. B) : 8, it follows from the generation lemma that r, x: A 1-W B : 82 for some sort S2. By the substitution lemma we have that r r-w B[x := a] : 8 2 and by conversion rule we have that r j-W d[x a] : B[x :=a].

The rest of the cases are easy to prove. 0

The next example shows that subject reduction does not hold for pure type systems without the II-condition. If r j-W e : E and e -+p e' then e' may not have type E. This happens when the type E is not typable and hence we cannot apply conversion rule.

Example 10.3.10. Let r =< 1 : * >.

e = (>.a:*.Àx:a.*)((>.a:*.ah) -+p (>.a:*.Àx:a.*h = e1

In ).W(C), we have that r j-W e: (,.\a:*.ah- 0 and r 1-"' e1: 1- 0 but not r j-W e': (>.a:

*.a)1-+ 0 (the latter can he proved by some meta-theoretical reasoning).

Theorem 10.8.11. (Uniqueness of Types) Let S be a singly sorted specification. If r 1-W a: A and r l-"' a: B then A -{J B.

This theorem is proved by induction on the derivation of r 1-"' a : A.

102 GRAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

10.4 Description of Toptypes

Toptypes are types that are not typa.ble. In this section we study the form of a toptype for pure type systems without the 11-condition and prove weak type reduction. Also we prove for singly sorted specifications that the set of toptypes is closed under {J-reduction and under substitution.

Reeall that A is a toptype in r if there èxists a such that r 1-w a : A and r ~ A : -and that A is a toptype if there are a a.nd r such that r f-W a : A and r vw A : ( see definitions 4.2.2 and 4.4.10).

Note that for pure type systems the only toptypes are the topsorts that are inhabited. Next we give examples of toptypes in pure types systems without the 11-condition.

Example 10.4.1.

a) In _xw(C) we can derive

A:* 1-0 .\x:A.(*-> *):(A-> D)

We have that (A-> D) is a. toptype.

b) In .x::: we can derive 1-w ( Àa:*.Àx:o:.x) : llo::*.O:-> o:

We have that lla:*.o: -> a is a toptype.

Definition 10.4.2. We say that the product llx:A. B can be Jormed in the context r if there exists an s such that r f-W (llx:A. B) : s.

Note that the product llx:A. B cannot be formed in the context r iffor all sl> s2 such that r 1-w A : St and r' x:A 1-w B : 82 we have that there is no sort s with (St, Sz, s) E R.

We define the set Mr of 'potencial' toptypes in r. The set Mr is not exactly the set of toptypes. If an element is in the set Mr thèn it is not typable in r. However it mayor may not be inhabited.

Definition 10.4.3. Let r E C. We define the set Mr as the smallest subset of T satisfying the following clauses.

1. if s is a topsort then s E Mr,

2. if r, x:A 1-w B : s for some s and the product (llx:A. B) cannot be formed in f then (llx:A. B) E Mr.

3. if B E Mr,.,-.A then (llx:A. B) E Mr.

10.4. DESGRIPTION OF TOPTYPES

Definition 10.4.4. Let rE T. A mapping deptbr: Mr ~IN is defined as follows.

depthr( s) 0 if s is a topsort

depthr(Ilx:A. B) 1 if r, x:A 1-W B : s for some s and (Ilx:A. B) cannot be formed in r

depthr(Ilx:A. B) = depthr,z:A(B) + 1 otherwise

We extend the mapping depth to pseudoterrus as follows.

depthr(A) = 0 forA ET- Mr and rEC.

103

Next we prove that a pseudoterm in the set Mr is a sequence of products whose 'heart' is either a topsort or it is a product that cannot be formed.

Lemma 10.4.5. Let rE C and A ET. The following two statements are equivalent:

1. A E Mr, n = depthr(A),

2. there are Ah ... , A.,, B such that A= Ilx1:A1 ... Ilx",:A.,.B where

Proof:

either n ~ 0 and B is a topsort s0

or n ~ 1 and r, x1:At, ... , x",:A., 1-w B : s for some sort s and (Ilx.,:A.,. B) cannot be formed in r, Xt:At, ... 'Xn-t:An-1•

(1 =? 2). This is proved by induction on A E Mr.

1. Suppose that the pseudoterm is a topsort s. Take n = 0 and the second statement hol ds.

2. Suppose that the pseudoterm is (Ilx:A. B) with r,x:A 1-w B: s forsome s. We know that the product Ilx:A. B cannot he formed in r,x:A. Taken= 1 and the second statement holds.

3. Suppose that the pseudoterm is (Ilx:A. B) with B E Mr,r.A· By induction we have that B = Ilx1:A1 ... IIxn:A",.B' where

either n ~ 0 and B' is a topsort s0

or n ~ 1 and r, x:A, x 1 :A1, ... , x",:An 1-w B' : s for some sort s and (Ilxn:An. B') cannot beformed in r,x:A,x1 :A~, ... ,xn-t:A",_1 .

Therefore the second statement holds for (Ilx:A. B).

104 GRAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

(2 '* 1 ). There are two cases:

0

1. Suppose n ;::: 0 and B is a topsort .s0. We apply once the first clause and then n times the third clause.

2. Suppose that r' Xt: At, ... 'Xn: A" f-W B : .s for some sort .s and the product (IIx": A". B) cannot be formed in the context r, Xt:At, ... 'Xn-l:An-l·

We apply once the second clause and then n times the third clause.

Next we prove that the pseudotermsin the set Mr are not typable in the context r.

Lemma 10.4.6. If A E Mr then r lfw A : -.

Proof: This is proved by induction on A E Mr.

0

1. Suppose that the pseudoterm is a topsort .s. Suppose towards a contradiction that r f-W .s : D for some D. Using the generation lemma we deduce that there is a sort .s' such that ( .s, .s') E A. This is a contradiction. Hence f lf' .s : -.

2. Suppose that the pseudoterm is (IIx: A. B) and that there are A and .s such that f,x:A 1-w B: .s. Also, we know that the product IIx:A. B cannot be formed in r,x:A.

Suppose towards a contradiction that f 1-w (IIx: A. B) : D for some D. By the generation lemma, there are sorts .St, .s2, .53 such that r f-W A: .St, r, x:A 1- B: .52 and (.st, .s2, .s3) E R. This is a contradiction.

3. Suppose that the pseudoterm is (IIx:A. B) with B E Mr,r.A· By induction we have that r, x:A lfw B: -.

Suppose towards a contradiction that r 1-"' (IIx: A. B) : D for some D. By the generation lemma there is a sort .s2 such that r, x:A 1- B : .s2. This is a contradiction.

Lemma 10.4. 7. Let r f-W a : A. If there is no sort .s such that r 1-w B[x := a] : .s then there is no sort .s such that r f-W (IIx:A. B) : .5.

Proof: Suppose r f-W (IIx:A. B); .s'. By the generation lemma we have that r,x:A f-W

B: s. By the substitution lemma we have that r 1-w B[x :=a]: s. o

Next we prove that a type in r that does not have a sort as type is in the set Mr.

Lemma 10.4.8. Suppose that r f-W a : A.

10.4. DESCRIPTION OF TOPTYPES 105

If there is nosort s such that r f- A : s then A E Mr.

Pro of: This lemma is proved by induction on the derivation of r r-w a : A. We will prove the cases of the abstraction and the application.

( b 0 ) r' x:D f-W e : E

• a stractiOn r f-W (>o.x:D. e) : {llx:D. E) 0

1. Suppose that f, x:D f-w E: s. The product {llx:D. E) cannot be formed in r. Hence (llx:D. E) E Mr.

2. Suppose that there is no sort s such that r, x: D f-W E : s. By induction E E Mr,rD· Hence {llx:D. E) E Mr.

( 1. . ) r f- f : {llx:D. E) r f- d : D

• app teatiOn r f- (f d) : E[x := d]

If there is no s such that r f-W E[x := d]: s then by lemma 10.4.7 there isno s such that r f- {llx:D. E) : s.

By induction we have that {llx: D. E) E Mr. By lemma 10.4.5 we have that E = llxt:At 0 0 .ITxn:An.H where either H is a topsort So or r,x:D, Xt :At, 0 0 0 ,xn: An r-w H: s forsomesort s.

Then E[x := d] = llx1 :A1 [x := d] .. . ITxn:An[x := d].H[x := d] where H[x := d] is either a topsort So or r, Xt:At[x := d], 0 0 0 'Xn:An[X := d] f-W H[x := d] : s.

1. Suppose that H[x := d] is a topsort s0 • By lemma 10.4.5 we have that E[x := d] E Mr.

2. Suppose that r, Xt:At[X := d], 0 0 0 'Xn:An[x := d] r-w H[x := d] : s. There should be a natural number k with 1 S k S n and a sort s' such that

a) r, Xt:At[X := d] 0 0 0 Xk:Ak[x := d] r-w (ITxk+I:Ak+I 0 0 .ITxn:An· B)[x := d] : s' and

b) the product (ITxk:Ak ... ITxn:An. B)[x := d] cannot be formed in the context r, Xt:At[X := d] 0 0 0 Xk-t:Ak_t[x := d]. .

Therefore by lemma 10.4.5 we have that E[x := d] E Mr.

The rest of the cases are easy to prove. 0

The next theorem says that a type is not typable in a context r if and only if it is in the set Mr. Hence, A is a toptype in r if and only if A is a type in r and it is in the set Mr.

Theorem 10.4. 9. (Description of Toptypes) Let f f-w a : A. The following statements are equivalent:

106 CHAPTER 10. TYPE INFERENCE FORPURE TYPE SYSTEMS

1. r lf"' A:-,

2. There is no sort s such that r f- A :. s,

3. AEMr.

Proof: The proof of (1 => 2) is trivial. The impHeation (2 => 3) is lemma 10.4.8. The implication (3 => 1) is lemma 10.4.6. 0

Lemma 10.4.10. Let r f-"' e : E and r ~ E : - be such that depth(E) = n and E = (llx1: At ... llx,.: A,.. B). Then there exists e' (Àx1 : A1 ••• >.x,..: A,.. b) such that e -(Je' and rf-We': E.

Proof: This is proved by induction on the derivation of r f-"' a : A. Only one case is considered.

r f-"' b: (llx:A. B) r f-W a: A r f-W (b a): B[x :=a]

By induction hypothesis we have that b -(J b', b' = (,\.x:A . .Àx1:A1 •.. Àxm:Am· d) and r f-"' b' : (llx:A. B) for some At, . .. Am and d.

Notethat depth(B[x :=al)= n:::; m = depth(llx:A. B). Therefore (b a) -(J d and d = (Àx1:At!x :=a] ... Àxm:Am[x :=a]. d[x :=a]). By the

substitution lemma we have that r f-W d : B[x := a]. 0

Theorem 10.4.11. (Weak Type Reduction Theorem) If r f-W e: E and E -+(JE' then there exists e' such that e -(Je' and r f-W e' : E'.

Proof: There are two possibilities, either E' is a toptype or not.

0

1. Suppose E' is nota toptype. By theorem 10.4.9, r f-w E' : s. Applying the conversion rule, we have that r f- e: E'.

2. Suppose E' is a toptype. Then Eis a toptype and E = (llx1:A1 ..• llx,..:A,.. B) with n = depth(E).

By lemma 10.4.10 there exists e' such that e -(Je'= (>.x 1 :A1 ... >.x",:A,.. b) and r 1-W e': E. Hence r,xl:Al ... ,x,..:An f-W b: B.

(a) If A; -+(J A: then by the weak subject rednetion theorem r,x1 : A1 •.• x;: A~, ... , x",:An f-W b: B.

(b) If B -+(J B' then by lemma 10.4.5 and the weak subject rednetion theorem we have tha.t r,x1:A1 .. . ,x,.:An !-"' B': s. Applying the conversion rule we have that r, x1:At, ... , x,.:A,. f-w b: B'.

10.4. DESGRIFTION OF TOPTYPES 107

For singly sorted specifications, we give another characterisation of the set of toptypes. Using this characterisation we prove that for singly sorted specifications, the toptypes are closed under ,8-reduction and substitution.

Next wedefine the set .Nr similar to the set Mr. The elementsin .Nr are not typable and they may or may not he inhabited.

Definition 10.4.12. We define the set .Nr as the smallest subset of T satisfying the following clauses.

1. if s is a topsort then s E .Nr,

2. if there are sorts St' s2 such that r 1-w A : St' that r' x:A 1-W B : s2 and there is no S3

such that (s11 s2, s3) ER then (ITx:A. B) E .Nr.

3. if B E .Nr,x-.A then (ITx:A. B) E .Nr.

Note that definitions 10.4.3 and 10.4.12 differ only in the second clause. For the case of singly sorted specifications, to require that the product (ITx:A. B) cannot he formed in r is equivalent to require that there are sorts St' s2 such that r 1-W A : St' that r' x:A 1-w B : s2

and there is no sort s3 such that ( s11 s2, s3) E R. Note that the inclusion Mr C .Nr holds for any specification. If the specification is not

singly sorted, .Nr may have more elements than Mr.

Example 10.4.13. Let S = ( S, A, R) he the non-singly sorted specification such that S = {0,1}, A= {(0,1),(0,2)} and R = {(2,2,2)}. We have that (0 -t 0) E .Nr but (0 -t 0) ~Mr.

In the following lemma, we prove that Mr = .Nr for singly sorted specifications.

Lemma 10.4.14. LetS he a singly sorted specification. Then Mr =.Nr.

Proof: [Mr C .Nr]. We pro'ceed by induction on the definition of DE Mr. Only one case is considered. Suppose that the pseudoterm D is (ITx:A. B), there are A and s2 such that r, x:A 1-w B : s2 and the product (Ilx:A. B) cannot he formed in r, x:A. By lemma 10.3.2 we have that there is some St such that r 1-w A : St. Th ere is no s such that (St, s2, s) E R. Therefore (Ilx:A. B) E .Nr.

[.Nr C Mr]. We proceed by induction on the definition of D E .Nr. Only one case is considered. Suppose that the pseudotermDis (IIx:A. B) and that there are sorts St,s2 such that r 1-W A: St, r,x:A 1-W B: s2 and there is nosort S3 such that (st,S2,s3) ER.

Suppose towards a contradiction that (IIx:A. B) can he formed in r. Then there are sorts s~, s~ such that r 1-w A : s~, r, x: A 1-w B : s~. By uniqueness of types we have that St = s~ and s2 = s~. This is a contradiction. D

As a consequence of the previous lemma, the set .Nr has the same properties as the set Mr if the specification is singly sorted.

108 GRAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEM$

Lemma 10.4.15. Let S be a singly sorted specification.

1f A E Nr then r lf' A : -.

Proof: This follows from lemmas 10.4.6 a.nd 10.4.14. 0

The next theorem states that a type is in Nr if and only if this type is not typable. Hence A is a toptype in rif a.nd only if A is a type and it is in the set Nr.

Theorem 10.4.16. Let s be a singly sorted specification and r 1-W a : A. The following statements are equivalent:

1. r f(w A:-.

2. AENr.

Proof: This follows from theorem 10.4.9 and lemma 10.4.14. 0

In the following lemma we prove that the set Nr is closed under ,8-reduction.

Lemma 10.4.17. If DENrand D -{3 D' then D' E Nr.

Proof: The following two statements are proved by induction on the definition of D E Nr.

a) if D -+p D' then D' E Nr,

h) if f -+f3 f' then DE Nr'·

0

We only prove statement a).

1. Suppose the pseudoterm D is a topsort s. Note that s is in ,8-normal form.

2. Suppose that the pseudoterm D is (IIx: A. B) and that there are sorts St, s2 such that r 1-W A: SI, r,x:A 1- B: 82 and there is nosort 83 such that (sl,s2,s3) ER.

If A -+p A' then it follows from weak subject reduction theorem that r 1-W A' : St

and f,_x:AI-0: B: s2. Hence (ITx:A'. B) E Nr.

Similarly if B -+p B' we have that (ITx:A. B') E Nr.

3. Suppose that the pseudoterm D is (IIx:A. B) with B E Nr,w.A·

If A -+pA' then it follows from induction that B E Nr,w.A'· Therefore (IIx:A'. B) E Nr.

Similarly if B -+p B' we have that (IIx:A. B') E Nr.

As a consequence of the previous lemma, we have that the set of toptypes is closed under ,8-reduction.

10.4. DESCRIPTION OF TOPTYPES 109

Theorem 10.4.18. (/3-closure of toptypes) Let S be a singly sorted specification.

If A is a toptype and A -{3 A' then A' is a toptype.

Proof: If A is a toptype then there are rand a such that r 1-W a: A and r lfw A: -. By theorem 10.4.16 we have that A E Nr. By lemma 10.4.17 we have that A' E Nr.

By lemma 10.4.15 we have that r lfw A':-. By the weak type reduction theorem we have that there exists a' such that r 1- a': A'. Hence A' is a toptype. o

Corollary 10.4.19. Let S he a singly sorted specification and r 1-w b: B. If there exists a sort s such that r 1-W B': s and B' -{3 B then r 1-w B: s.

Proof: By Church-Rosser theorem, there exists a common reduct Do of B and B'. By weak subject reduction theorem we have that r 1-w Do : s. By theorem 10.4.18 we have that r 1-W B : s' forsome s'. By weak subject reduction r 1-w Do : s' and by uniqueness of types we have that s = s'. 0

In the following, we prove that the set of toptypes is closed under substitution.

Lemma 10.4.20. Let S he a singly sorted specification. Let r 1- d: D.

If E E Nr,vD,r' then E{y := d] E Nr,r'[y:=dJ·

Proof: This is proved by irtduetion on E E Nr,vD,r'· Only one case is considered. Suppose that Eis (flx:A. B) and that there are sorts St, s2 such that r, y:D, r' 1-W A: St

and r' y:D' r'' x:A 1- B : s2. Moreover there is no sort SJ such that (St, s2, SJ) E R. Substitution lemma yields r' r' 1-W A[y := d] : St and r' r'[y := d]' x:A[y := d] 1- B[y :=

d] : s2 • Hence (flx:A. B)[y := d] E Nr,r'[y:=dJ· 0

Theorem 10.4.21. (Substitution on Toptypes) LetS be a singly sorted specification. Suppose that r, y:D, f' 1- e : E and that r.l- d : D.

If r,y:D,f'lf' E:- then r,r'[y := d] lfw E[y := d]: -.

Proof: By theorem 10.4.16 we have that E E Nr,vD,f'· By lemma 10.4.20 we have that E[y := d] E Nr,P[y:=dJ· By lemma 10.4.15 we have that r, f'[y := d] lfw E[y := d] : -. o

Corollary 10.4.22. Let S be a singly sorted specification. Let r 1-w F : (fix: A. B), f 1-w B[x :=a]: s and r 1-w a: A. Then f,x:A 1-w B: s.

The next example shows that when the specification is not singly sorted, a toptype may become typable after substitution.

Example 10.4.23. The following specification is not singly sorted:

s 0,1,2 S A 0: 1, 0: 2,1: 2

R (2,2)

Given the context< x:1 >, the term (x -t x) is a toptype. Substituting x by 0 we obtain (0 -t 0) which has type 2.

110 CHAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

10.5 Normalisation for /3-reduction

In this section we define the notion of illegal redex a.nd a function r.p : C x T -7 T that contracts the illega.l redexes of a term (we write r.pr( a) instea.d of r.p(f, a)). We prove that <pr is a strategy for the ,8-reduction. Moreover we prove that for singly sorted specifications, if a termbis typahle in Àw(S) then r.pr(b} does not contain illegal redexes. For each singly sorted specification, wedefine a converting morphism from Àw(S) to .>.(S). Finally we prove that weak normalisation of .>.(S) implies weak normalisation of >.w(S) if Sis singly sorted.

Definition 10.5.1. LetS he a singly sorted specification.

We say that im abstraction Àx:A.b is illegal in the context r if there exists D such that r 1--W Àx:A.b: D and r lf' D : -.

An abstraction is legal if it is typable in r and it is not illegal in r.

We say that (.>.x:A.b)a is an illegal ,8-redex in rif the abstraction >.x:A.b is illegal in the context r.

Lemma 10.5.2. Let S he a singly sorted specification. An abstraction Àx:A.b is legal in the context rif and only if there exists D such that r 1--"' >.x:A.b: D and r f-W D : s.

Pro of:

(::}) Obvious.

C<=) Since r 1--w Àx:A.b: D, the abstraction >.x:A.b is typable in r. Suppose there exists D' such that r 1--"' Àx:A.b: D'. By the uniqueness of types theorem we have that D' *"'*PD. Corollary 10.4.19 yields r f-W D' : s.

0

We define the mapping r.p that contracts the illegal redexes of a term.

Definition 10.5.3. We define r.p : C x T -7 T as follows.

r.pr(x) x

r.pr(s). s

r.pr(a b)

lf'r(>.x:A. a) r.pr(ITx:A. B)

{

a0 [x := r.pr(b )] if r.pr( a)

( r.pr( a) r.pr( b)) otherwise

(.>.x:r.pr(A). <pr,x-.A(a))

= (Ilx:r.pr(A). <pr,x-.A(B))

Sometimes we write 1p(a) insteadof r.pr(a).

>.x:A.a0 is an illegal abstraction in r

10.5. NORMALISATION FOR /3-REDUCTION

Definition 10.5.4. Wedefine r.p : C -+ C as follows.

r.p(t:) = E

r.p(f,x:A) = r.p(f),x:r.pr(A)

The following lemma states that r.pr is the identity on ,\(S).

111

Lemma 10.5.5. Let S be a singly sorted specification. If r 1- a : A then r.p(f) = r, r.pr(a) =a a.nd r.pr(A) =A.

The following lemma states that cpr is a strategy for /3-reduction.

Lemma 10.5.6. For all r, a -*P r.pr(a).

Lemma 10.5.7. LetS he a singly sorted speci:fication. If r,x:A,f' 1-"' b: Band r r"' a: A then r.pr,.,..A,r·(b)[x := r.pr(a)] = r.pr,f'(b[x a]).

Proof: We prove the case of the application.

r.p(F b) = { ao[Y := r.p(b)] if r.p(F) Ày:A.a0 is illegal ( r.p( F) r.p( b)) otherwise

{ a1[y := r.p(b[x a])] if 1/)(F[x :=a]) = Ày:A'.at cp((F b)[x :=a]) = r (r.p(F[x :=a]) r.p(b[x :=a])) otherwise

By induction we have that r.p(F)[x r.p(a)] = r.p(F[x := a]) and r.p(b)[x r.p(a)] = r.p(b[x :=a]).

Note that F and r.p(F) are typable in r, x:A, f' and r.p(F)[x := r.p(a)] is typable in r, f'. There are several cases:

1. Suppose r.p(F) = Ày:E. a0 is an illegal abstraction.

Therefore r, x: A, f' r-w ,\y: E.a0 : D and r, x: A, f' lf" D : - for some D. It follows from the weak subject reduction theorem that r 1-w r.p(a) :A' with A -*PA'. Since r r-w A : s we have that r r r.p( a) : A. It follows from lemma 10.3. 7 that r, r'[x := r.p(a)] f-W r.p(F)[x := r.p(a)] : D(x r.p(a)]. By theorem 10.4.21, we have that r,r'[x := r.p(a)]lfw D[x := r.p(a)]: Therefore r.p(F)[x := r.p(a)] is an illegal abstraction too.

Moreover we have that

r.p(F[x :=a]) = r.p(F)[x r.p(a)] Ày:E[x := r.p(a)].a0 [x := r.p(a)] .

We condude that r.p(F b)[x := r.p(a)] r.p((F b)[x :=a]) as follows.

r.p(F b)[x := r.p(a)] = a0 [y := r.p(b)][x := r.p(a)] = a0 [x := r.p(a)][y := r.p(b)[x r.p(a)]] = r.p(F[x :=a] b[x :=a])

112 CHAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

2. Suppose cp(F) is not an illegal abstraction.

(a) Suppose that cp(F) is not an abstraction. If cp(F)[x := cp(a)] == cp(F[x :== a]) were an abstraction we would have that cp(F) = x and cp(a) >..y:E. a0 • We have that r f-W cp(a) : A and r f-W A : s. By lemma 10.5.2 we have thát cp(F)[x :== cp(a)] is alegal abstraction. Hence the value of the application can be computed as follows.

cp(F b)[x := cp(a)] (cp(F) cp(b))[x := cp(a)] (cp(F)[x :== cp(a)] cp(b)[x := cp(a)])

= (cp(F[x :=a]) cp(b[x :=a])) = cp(F[x :=a] b[x :=a])

Hence cp(F b)[x := cp(a)] = cp((F b)[x :=a]).

(b) Suppose that cp(F) is an abstraction but legal. By lemma 10.5.2 we have that cp(F[x := a]) is a legal abstraction. The equality cp(F b)[x := cp(a)] = cp((F b)[x :=a]) is provedas in case a).

The rest of the cases are easy to prove. 0

The next example shows that if the specification is not singly sorted cp(b[x :=a]) may not he syntactically equal to cp(b)[x := cp(a)].

Example 10.5.8. The following specification is not singly sorted:

s 0,1,2 SA 0:1,0:2,1:2

R (2,2)

We take< r x : l,z: x> and b (>..y:x.y)z. Note that b cont.ains illegal abstractions but b[x 0] does not. Hence cp(b[x := 0]) ::f. cp(b)[x := cp(O)].

In the following theorem, we prove that if b is typable in >..w(S) then cp(b) does not contain illegal redexes when S is a singly sorted specification. The value cp(b) is of the form >..x1 : A1 ••.• >..x,.: A".. b with b typable in >..(S) and the abstractions whose bound variables are x1 , •.. , Xn are illegal.

Theorem 10.5.9. (Preservation of the typing relation} Let S be a singly sorted specification. 1f f 1-w a: A, n depth(A) and A= flx 1:A1 ••• IIx".:A,..B then

cp(f,xt:At .. . xn:A".) 1- a': cp(B) with cp(a) Àxt:cp(At) ... Àxn:cp(An).a'.

Proof: This property is proved by induction on the derivation of r f-W a : A. We consider the cases of the abstraction, the application and the conversion rule.

10.5. NORMALISATION FOR {3-REDUCTION 113

( b . ) r, x : A r-w b : B

• a stractwn r r-w (Àx:A. b) : (IIx:A. B) .

There are two possibilities, either (IIx:A. · B) is a toptype or not.

1. Suppose r r-w (IIx:A. B) : s. By the generation lemma we have that r, x:A r-w B: s2, r r-w A: SJ and (sJ,s2,s) ER.

By induction we have that c,o(f, x: A) f-- c,o(b) : c,o(B). Using weak subject reduction theorem wededuce that c,o(f, x: A) r-w c,o(B) : s2. By lemma 10.3.5 we have that c,o(f,x: A) f-- c,o(B): s~ forsomesort s~.

Besides we have that c,o(f) f-- c,o(A) : s~ for some sort s~.

Since the specification is singly sorted, we have that SJ = s~ and s2 = s~ and we know that ( sl> s2 , s) E R. We obtain the following derivation:

(r x·A) f-- (b). (B) c,o(r) f-- c,o(A): SJ c,o(f,x:A) f-- c,o(B): s2 c,o ' . c,o . c,o c,o(f) f-- c,o(IIx:A. B) : s

c,o(f) f-- c,o(Àx:A. b) : c,o(IIx:A. B)

2. Suppose r lfw (IIx:A. B) : -. There are two possibilities, either B is a toptype or not.

(a) Suppose f,x:A r-w B: s. By induction we have that:

c,o(f,x:A) r-w c,o(b): c,o(B) where c,o(Àx:A. b) = Àx:c,o(A). c,o(b)

(b) Suppose f,x:A lfw B: -. It follows from induction that

c,o(f, x:A, XJ:AJ ... Xn:An) f-- b': c,o(B') where B = IIxJ:AJ ... IIxn:An.B',

c,o(b) = ÀXJ:c,o(AI) ... Àxn:.c,o(A.,).b'

( l. . ) r r-w b : (IIx:A. B) r r-w a : A

• app Icatwn r r-w (b a) : B[x :=a]

There are two possibilities: either B[x :=a] is a toptype in r or not.

1. Suppose r r-w B[x := a] : s.

(a) Suppose r r-w (IIx:A. B): s'. By induction we have that

c,o(r) f-- c,o(b) : c,o(IIx:A. B) By the generation lemma we have that r r-w A: SJ. By induction we have that c,o(f) f-- c,o.(a): c,o(A). Using the weak subject reduction theorem, wededuce that r r-w c,o(b) : D and r r-w D: s. Hence c,o(b) is not an illegal abstraction in rand c,o(b a)= (c,o(b) c,o(a)). Hence we have the following derivation:

114 CHAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

<,o(f) f- cp(b): <,o(IIx:A. B) <,o(f) f- cp(a): <,o(A) <,o(f) f- cp(b a): cp(B)[x := cp(a)]

By lemma 10.5.7 we have that cp(B)[x := cp(a)] = cp(B[x :=a]). (b) Suppose r lfw (IIx:A. B) : -.

By lemma 10.3.8 we have that r f-W A : St. By induction we have that <,o(f) f- cp(a): <,o(A). By corollary 10.4.22 we have that r' x:A f-W B : s2. By induction we have that <,o(f,x:A) f- b': <,o(B) with cp(b) = Àx:cp(A).b'. We have that f f-w Àx:cp(A).b': (IIx:cp(A). <,o(B)). By theorem 10.4.18 we have that r lfw (IIx:cp(A). cp(B)) : -. Therefore cp(b) = Àx:cp(A).b' is an illegal abstraction in the context r. Since cp(b) = Àx:A'.b' is an illegal abstraction, we have that cp(b a) = b'[x := <,o(a)]. By the substitution lemma,

cp(r) f- b'[x := cp(a)]: cp(B)[x := cp(a)]

By lemma 10.5.7, we have that cp(B)[x := cp(a)] = <,o(B[x :=a]).

2. Suppose r lfw B[x := a] : -.

It follows from lemma 10.4.7 that f lfw (IIx:A. B): -. By lemma 10.3.8 we have that r f-W A : St. By induction we have that <,o(f) fep( a): <,o(A) and that <,o(f,x:A,xt:At,···•xn:An) f- b': <,o(Bo) with cp(b) = Àx: <,o(A). ÀXt:<,o(At) ... Àxn:<,o(An).b' and B = Ilxt:At ... Ilxn:An· Bo. Weak subject reduction theorem yields r f-W <,o(b) : (IIx: A'. B'). By theorem 10.4.18 we have that r lfw (IIx: A'. B') : -. Therefore cp(b) is an illegal abstraction and then the value of cp(b a) is computed as follows.

cp(b a) = Àxt:cp(At)[x := cp(a)] .. . Àxn:cp(An)[x := cp(a)].b'[x := cp(a)]

By the substitution lemma we have that b'[x := cp(a)] has type cp(B0 )[x := cp(a)] in the context cp(f), Xt:cp(At)[x := cp(a)], ... , Xn:cp(An)[x := cp(a)]). By lemma 10.5.7 we have that: cp(B0 )[x := cp(a)] = cp(B0 [x :=a]). By theorem 10.4.21, we have that n = depthr(B[x :=a]) = depthr,:r.A(B).

. r f-W b : B r f-W A : s B """"* (3 A • (converston) rf-wb:A

By corollary 10.4.19, we have that r f-W B : s'. By induction we have that <,o(f) fcp(b) : cp(B) and cp(f) f- cp(A) : s. After applying the conversion rule, we obtain cp(f) f- cp(b): cp(A).

The rest of the cases are easy to prove. 0

10.6. WEAK AND STRONG NORMALISATION FOR p,-REDUCTION 115

Definition 10.5.10. Let S a singly sorted specifica.tion. We define 'Ps : C x T -t C x T as follows.

'Ps(r,a) =

( < cp(r), x1:A1 ... Xn:An >, b) if r 1-"' a: A forsome A, n = depth(A) and cp(a) = Àx1:A1 ... x":An· b

( < cp(r), Xt :At ... x .. :An >, b} if a is a toptype in r, n = depth( a) and cp(a) = 11xt:At·· .x,..:An. b

(r,a) otherwise

Sometimes we write <p instead of cp•.

Corollary 10.5.11. (Converting morphism from À"'(S) to >.(S)) LetS be a singly sorted specification. There is a weak converting morphism from >."'(S)

to À(S).

,Proof: By the previous theorem we have that <p$ preserves the typing relation. Since <p is a strategy, 'Ps preserves conversion. Hence <p$ is a weak converting morphism from >."'(S) to À(S). 0

Note that the previous theorem in fact shows that cp• is a natura! transformation from À"'

to À when these are considered as functors from the category of singly sorted specifications into a category analogous to Carst.., where morphisms only preserve /1-conversion.

Corollary 10.5.12. (Weak Normalisation) LetS be a singly. sorted specification. If À(S) is weakly normalising then so is À"'(S).

10.6 Weak and Strong Normalisation for f3t-reduction

The mapping r.p contracts the illegal redexes in a given order. Now we contract the illegal redexes in any order by means of a reduction called /1,-reduction. In this section we define the notion of p,-reduction and prove that for singly sorted specifications if r 1-"' a : A then a is p,-strongly normalising.

Definition 10.6.1. Wedefine the illegal /1-reduction in r (or /1,-reduction) by the following rules:

116 GRAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS.

r f- (.\x:A. b)a _"13, b[x :=a] if (.\x:A. b)a is an illegal redex in r.

r,x:A f- b _"13, b' r f- (h:A. b) _"13, (.\x:A. b') r f- (.\x:A. b) _" 13, (h:A'. b)

r,x:A f- b _"13, b' r f- (IIx:A. b) _"f3, (IIx:A. b') r f- (IIx:A. b) _"f3, (IIx:A'. b)

r f- (ba) _"{3, (b' a) r f- (b a) _"{3, (b a')

In the following lemma, we prove that <pis a strategy for ,8,-reduction.

Lemma 10.6.2. Let S be a singly sorted specification. If r r-w a : A then r f- a -f3.

rpr(a).

In the following lemma, we prove that <p is a forgetting morphism.

Lemma 10.6.3. Let S be a singly sorted specification. If r r-w a : À and r f- a _"f3, b then rpr(a) = rpr(b).

Proof: We prove only one case. Suppose that r f- (.\x:A. b)a _"{3, b[x :=a] and (h:A. b)a is an illegal redex in r. Note that rp(.\x:A. b) is illegal. We have that

rp((.\x:A. b)a) rp(b)[x := rp(a)] = rp(b[x :=a]) by lemma 10.5.7.

0

Theorem 10.6.4. (Weak Normalisation for ,8,-reduction) Let S be a singly sorted specification. If r r-w b: B then bis ,8,-weakly normalising.

Proof: Suppose r f-W a: A. Bylemma 10.6.2, rf-a -{3. <pr(a). By theorem 10.5.9, we have that rpr(a) does not contain illegal redexes and hence it is in ,8,-normal form in r. By lemma 10.6.3 the normal form is unique. 0

For singly sorted specifications, the illegal abstractions of a term b constitute an initial labelling for a superdevelopment ( see chapter 7).

We write a morphism,

.\w(S) ~ (A" _"pl)

The function L puts labells in the illegal abstractions of a pseudoterm.

10.6. WEAK AND STRONG NORMALISATION FOR {J.-REDUCTION 117

Deftnition 10.6.5. Wedefine the mapping L : C x T -t A1 as follows (we write L(b) instead of L(f, b)).

L(x) = x

{ @{0}(,\0z.À;x.L(b), L(A)) if -\x:A. bis illegal in r,

L(-\x:A. b) = take sorne i and z fresh @{0}(À0z.Àox.L(b), L{A)) otherwise

{ @{;l(L(b), L( a)) if nfp1(L(b)) = À;x. b0 and i> 0 L(b a) =

@0(L{b), L(a)) otherwise

In the case of the abstraction, the function L puts a fresh label (greater than 0) to an illegal abstraction and the label 0 to a legal one. The terrus in A, are untyped so the declaration x:A of an abstraction Àx:A. b in T is encoded as a fJ1-redex whose argument is the type A.

In the case of the application, the function L. puts the label i > 0 to the a.pplication whose operator reduces to >.,x. bo, an abstraction that corresponds to an illegal a.bstraction.

All the illegal abstractions should have different labels. In the case of the a.bstraction, we assurne that all the labels of the illegal abstractions in L(b) and in L(A) are different. Sirnilarly, in the case of the the applica.tion, we aasurne that all the labels of the iJlegal abstractionsin L(b) and in L(a) are different.

Lemma 10.6.6. Let S he a singly sorted specification. lf f 1-w b: B then L(b) E At.

Lemma 10.6. 7. Lets he a singly sorted specification and r, x:A 1-W b: Band r 1-W a: A. Suppose that all the abstractionsin L(b) and in L(a) have different labels except for 0.

L(b[x a])= L(b)[x L(a)]

Theorem 10.6.8. Let s be a singly sorted specification. 1f r 1-W b: B and r 1- b -tp, b'. then L(b) -t~ L(b').

Corollary 10.6.9. Let S be a. singly sorted specification. Then L is a rnorphism frorn >.w(S) equipped with fJ.-reduction to (A,, -tp1).

As a consequence, a fJ.-rewrite sequence can he rnapped into a superdeveloprnent and so it is strongly normalising.

Corollary 10.6.10. (Strong Normalisation for {J.-reduction) Let s he a singly sorted specification. If r f-W b : B then b is {J.-strongly norrnalising.

118 CHAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

10.7 Syntax Directed Rules

In this section we define a syntax directed set of rules for any singly sorted pure type system. This system will be used in the following section to define a function that infers the type.

When we try to infer the type of a term, we construct the derivation tree bottom-up, from the condusion to the premises. This tree is constructed by means of an analysis of the term and of the context. According to the structure of the term we try to deduce which rule may fit as the last rule in the derivation tree. For a term like an abstraction, the last rule in the derivation tree should be the abstraction rule. However the weakening and the conversion rules of a pure type system can always be applied at any point in the derivation. This means that in the case of the abstraction, the last rule in the derivation tree might be either the abstraction, or the weakening or the conversion rules. The last rule is not determined by the shape of the term. If the term constructor together with the context determine the last rule to be applied then we can build the derivation tree of the term. A set of rules is called syntax directed if it has this property: the last rule in the derivation of the type of a term is determined by the structure of the term and of' the context. The rules for pure type systems are not syntax directed since the last rule in the derivation can be the conversion or the weakening rules besides the corresponding structural rule. In order to make these sets of rules syntax directed, we should remove the non-structural rules (like the we!lkening and the conversion ones) and keep only the structural rules (the ones for term constructors). The system obtained by eliminating the non-structural rules should be equivalent to the original one. Therefore, the weakening rule is not removed but restricted to variables or constauts and eventhough the conversion rule is removed, reduction or conversion is needed in the premises of almost all the rules. The equivalence (soundness and complete~ess) between the syntax directed set of rules and the original one for pure type systems is not easy to be proved. In order to be able to prove soundness and completeness we do not check the IT-condition in the same system but in a weaker one. The weaker system is the pure type system without the IT-condition.

First we define the weak head ,8-reduction.

Definition 10.7 .1. The weak head ,8-reduction is defined by the following rules:

(Àx:A. b)a -t~h b[x :=a]

F -twh F' [3

(Fa) -tt (F' a)

Note that this reduction is not closed under the compatibility rules, i.e. it is not true that C[aJ-tt C[b] if a -tt b.

Wedefine a sytem À~d(S) whose rules are syntax directedandis proved to be 'equivalent' to Àw(S). We denote that r f- A : C and 0 --it B by r f- A:---* B for an arbitrary typing relation f- and a rewrite relation -+.

10. 7. SYNTAX DIRECTED RU LES 119

Definition 10.7 .2. The functor ).~d: Spec-+ Carstvw is defined as À~d(S) = (T,C, -+11,!-~d) forSE Spec.

The sets Tand C and the relation -+11 are as in definition 9.3.14. The typing relation 1-~d is defined as the smallest relation closed under the following rules.

(axiom)

(start)

( weakening)

(f ormation)

( abstraction)

( application)

where sE S.

r 8

r ~-~d A :.-11 St r,x: A ~--~dB :.-p 82

r ~-~d (ITx:A.B) : Sg

r, x : A 1-~d b : B r !-~d (Ax:A. b) : (IIx:A. B)

r 1-~ct b :.-';" (ITx:A. B) r !-~d a : A' r !-~d (b a) : B[x := aJ

for (c,s) E A

where x is r-fresh

where x is r-fresh and b E CU V

Since the conversion rule is removed, reduction or conversion is needed in the premises of almost all the rules. In the start, the weakening and the product rules, the types are reduced tosome sort. In the application rule, the type of F is reduced to (ITx:A. B) and the type A' of a should he convertible to A.

Note that these rules are syntax directed if the specification is singly sorted. If the specification is not singly sorted, given a context r and a term b, the type of b may not be unique and the last rule to he applied in the derivation of the type of b is not determined by the shape of band r.

Note that these systems verify only very weak subject reduction (see definition 4.3.1). We prove that the system À~d(S) is 'equivalent' to A"'(S) (soundness and completeness).

Lemma 10.7.3.- If r 1-w A: D and D .-11 s then r 1-w A: s.

Proof: Suppose that r 1-w D : s' forsome s'. By the weak subject reduction theorem, r 1- s : s' and applying the conversion rule, we obtain r !- A : s.

If D is a toptype, we have that D s. D

Theorem 10.7.4. (Soundness) If r ~-~d a: A then r !-W a: A.

Proof: The proof proceeds by induction on the derivation of r r~d a : A. Only two cases are considered.

120

0

GRAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

( d ) r t-~d A =-Pst r, x : A t-~d B =-P s2 .c ( ) R • pro u ct r 1-"' (II ·A B) . lOf St, s2, S3 E . sd X. • • S3

By induction hypothesis and lemma 10.7.3, we haver 1-"' A: Stand r,x: A 1-"' B: s 2• Hence r 1-"' (ITx:A.B) : sa.

( l. t' ) r t-~d b :-'J/' (ITx:A. B) r t-~rt a: A' 'th A A' • app !Ca IOn r ~-~d (b a): B[x :=a] Wl <+-*(3 •

By induction hypothesis, we have that r 1-"' b : D and D -'Ph (ITx: A. B). Also r 1-"' a: A'. There are two cases.

1. Suppose that T 1-"' D : s forsome s. By weak subject reduction, we have that r 1-"' (Ilx:A. B) : 8. Generation lemma yields r f-W A : 8

1 for some s'. Hence r 1-"' a: Aand so r 1-"' (b a): B[x :=a].

2. Suppose that D is a toptype in r. By the description of toptypes theorem, we have that DE Mr and so D = ITx:A". B" forsome A11 and B". The weak head normal form of D is D itself, so A = A" and B" = B.

The proof of completeness is straightforward.

Theorem 10.7.5. (Completeness) If r f-W a: A then there exists A' such that r ~-~d a: A' and A *"*PA'.

Next wedefine a system Àsd(S) whose rules are syntax directed. We prove that if Sis singly sorted then Àsd(S) is 'equivalent' to À(S). The systems Àsd(S) and À::'d(S) differ only in the abstraction rule. The abstraction nile in À~d(S) does not contain the II-condition and the one in Àsd(S) does. The II-condition of Àsrt(S) is not checked in the same system Àsd(S) but in À::'rt{S). The idea is to use an auxiliary system to check for the II-condition [BJMP93] so that we can prove the 'equivalence' between Àsd(S) and À(S). In our case, the auxiliary system is the corresponding pure type system without the IT-condition.

Definition 10. 7.6. The functor Àsd : Spec -t Carstvw is defined by Àsd(S) = (T, C, -+p, 1-sd) forS E Spec.

The setsTand C and the relation -tp are as in definition 9.3.14. The typing relation r-.d is the smallest relation closed under the same rules as in definition 10. 7.2 except that the abstraction rule is replaced by the following one.

( abstraction)

where sE S.

r,x: A 1-.d b: B r 1-~d (ITx:A. B) :-p s r f-sà (Àx:A. b): (ITx:A. B)

10.8. TYPE INFERENCE 121

Observe that in the abstraction rule we have the requirement that (IIx:A. B) should be typable in >.~d(S).

Note that these systems verify only very weak subject reduction (see definition 4.3.1).

Theorem 10.7. 7. (Soundness) Let S be a singly sorted specification. ~ If r 1-sd a : A then r 1- a : A.

Pro of: We prove only the case of the abstraction rule.

( b t t. ) r, x : A f-sd b : B r 1-~d (Ilx:A. B) :- s

a s ra.c 1on r 1-.d (>.x:A. b): (Ilx:A. B)

By induction hypothesis we have that r,x:A 1- b: B. By lemma 10.5.5, we have that cp(r) r, cp(A) = A, cp(b) = band cp(B) = B. By theorem 10.7.4 and lemma 10.7.3 we have that r 1-"' (Ilx:A. B) : s. By theorem 10.5.9 we have that r 1- (Ilx:A. B) : s. o

Theorem 10.7.8. (Completeness) If r 1- a : A then there exists A' such that r 1-.d a: A' and A *-*PA'.

Proof: We prove only the case of the abstraction rule.

( b t. ) r, x : A 1- b: B r 1- (IIx:A. B) : s

a strac Ion. r 1- (>.x:A. b) : (IIx:A. B)

By induction hypothesis we have that r, x:A 1-sd b : B' for some B' such that B *'"* p B'. By the previous theorem (soundness) we have that r, x:A 1- b: B'.

If r 1- (IIx:A. B) : s then there exists St and s2 such that (St' s2' s) E R, r 1- A : St and r, x:A 1- B : s2 • By correctness and unicity of types we have that r, x:A 1- B' : s2 • Hence r 1- (Ilx:A. B'): s. By theorem 10.7.5 we have that r f-~d (IIx:A. B') : D and D -13 s. o

10.8 Type Inference

In this section we define a semi-algorithm of type inference for the class of singly sorted pure type systems. A semi-algorithm of type inference is a partial function or program that terminates and yields the type of a term if the term is typable and it may not terminate otherwise.

We cannot expect to find a terminating algorithm for the class of singly sorted pure type systems since typability {and also conversion) forsome non-norma.lising pure type systems is not decidabie (see theorem 9.3.20). Hence wedefine a type inference semi-algorithm, for the class of singly sorted pure type systems, including the non-normalising ones.

In order to define such a semi-algorithm, we use the syntax directed set of rules presented in section 10.7. Although those rules are syntax directed when the specification is singly sorted, they are not yet deterministic. There are several conditions, called side conditions, tha.t should be solved. For these conditions we have to verify if some element

122 GRAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

belongs to some of the sets in ~· specification, or we have to perform ,8-reduction or check if two types are ,8-convertible.

If the sets S, A and R of the specification are not recursively enumerable (recursive) then the conditions sE S, (s1,s2 ) E A and (s1,s2,s3 ) ER are not semi-decidabie (decidable). In order to have a semi-algorithm we have to assume that these sets are recursively enumerable.

There are several ways of ,8-reducing or checking ,8-conversion. We should specify in which way we reduce and how we check if two types are convertible. We write semialgorithms or partia1 fundions to compute the weak head normal form and to check for ,8-conversion. The first one computes the weak head normal form if it exists and it may not terminate otherwise. The secoud one is a common-reduct strategy. The termination of the semi-algorithm of type inference depends on the termination of these two functions. Therefore, in order to reduce the cases of non-termination of the type inference semialgorithm, it is suflident to reduce the cases of non-termination in the semi-algorithm that computes the weak head normal form and in the common-reduct strategy.

Reducing to a sort or to a product. In the cases of the start, weakening and product rules the types are reduced to a sort. In case the term is typable, we know that the weak head normal form exists and it is a sort. In the application rule, the type of the operator is a product (IIx:A. B) also a weak head normal form.

A weak head normal form can be either au abstraction (.~x:A. b), or a product (IIx: A. B), or an application (b a1 ••• an) where bis a sort or a variabie or a product.

Now we write a function that computes the weak head ,8-normal form if it exists.

Definition 10.8.1. A function whnf: T __.. T is defined as follows .

whnf(a) = a if a is in weak head normal form

whnf((Àx:A. b) a d1 ••• dn) = whnf(b[x := a]d1 ••• dn)

This function is a semi-algorithm, i.e. it may not terminate only in case the term is not weak head normalising.

Lemma 10.8.2. Let a ET be weak head normalising. Then whnf(a) is the wea.k hea.d normal form of a.

This lemma is proved by in duetion on the number of steps of the leftmost reduction to normal form.

Checking ,8-conversion. In the application rule we have to check if two types are f:Jconvertible. It would be suflident to find a computable common-reduct strategy F : T x T __.. P(T) and then check if F(a, b) :j; 0. .

The strategy presented in [Coq91] (see also [Mag94]) reduces as less as possible by performing weak head reduction. The idea is to compute the weak head normal forms of

10.8. TYPE INFERENCE 123

the terms and compare their heads. In spite of being efficient, it gives a set of commonreducts only if both terms are normalising. It might happen that a *"'*fJ b and F 11 (a, b) does not terminate if a or b are not normalising.

Definition 10.8.3. We define F 11 : T x T -4 'P(T) as follows.

F11(d,d') = {d} if d = d'

F11 ((Àx:A. b), (Àx:A'. b')) = {(Àx:A11• b") I A" E F11(A, A') & b11 E F11 (b, b')}

F 11 ((IIx:A. B),(ITx:A'. B')) = {(ITx:A". B11) I A11 E F11(A,A') & B 11 E F 11 (B,B')}

F11 (d,d') F11(whnf(d), whnf(d')) if d or d' are not in weak head normal form

otherwise

The next lemma says that the function F11 is a common-reduct strategy only for normalising pseudoterms, i.e. for all a, bET that are normalising, for all c E F 11 (a, b) we have that a -fi c and b -P c.

Lemma 10.8.4. Let a and b be weakly normalising. Then

1. F 11 (a, b) terminates.

2. For all c E F 11 (a, b), we have that a-pc and b -Pc.

3. a *"'*P b if and only if F 11( a, b) =/:- 0.

All the parts ofthe previous lemma are proved by induction on (I( a) +l(b), n(a) + n(b)) where I computes the number of steps of the leftroost rednetion to normal form and n computes the number of symbols.

This strategy may not terminate when one of the terms is not weakly normalising. For example, suppose that n = (Àx:A. x x)(Àx:A. x x). Then F 11 (b, fl) does not terminate for any bET.

This common-reduct strategy is not satisfactory because we do not want to restriet our type inference procedure to normalising pure type systems.

Now wedefine three common-reduct strategies that work also for non-normalising pseudoterros and hence for non-normalising pure type systems. The three compute the bounded graphs of the terms (see also section 2.5) step by step. We compare these three strategies according to their order and the number of cases they do not terminate. The third strategy is the best one for having a best order and least cases of non-termination.

124 GRAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

The first strategy is called F and it always computes the bounded reduction graphs of the terros step by step. In each step, we have to check only if the 'new part' of the graph has a common element with the other set. This strategy terminates and gives at least one common reduct if both terros are ,8-convertible and it may not terminate otherwise.

Definition 10.8.5. We define F : T x T-+ P(T) as follows.

F(a,b) = If a= b then I {a} el se

H({a},{b}) end

Next wedefine the function H: P(T) x P(T)-+ P(T). We suppose that it is always applied to subsets X, Y of T that verify the following preconditions.

1. X and Y are the n and m-bounded reduction graphs of a and b, i.e. they are of the form

g_::~(a) ={dI a -(3 d in less than n steps}

and g_::;(b) ={dI a -(3 d in less than m steps}

2. The intersection of X and Y is empty.

3. 0 ~ n- m ~ 1.

We only have to check if Q .... ll(X) has elementsin common with Y. If itdoes, Q .... ll(X)nY are the common-reducts we are looking for.

H(X,Y) = If Q .... ll(X) n Y # 0 then I g_,ll(X) n Y el se

H(Y, X U Q .... 13 (X)) end

Note that Y takes the role of X in the else-part. In the following lemma we prove that the function F is a common-reduct strategy.

Lemma 10.8.6.

1. If a -(3 b then F(a, b) terminates and yields a non-empty set that verifies that for all c E F(a, b) we have that a -(3 c and b -(3 c.

10.8. TYPE INFERENCE 125

2. If F(a, b) :f: 0 then a *"'*IJ b.

Proof:

0

1. If a *"'*IJ b then there exists mand n such that 0:::; m-n:::; 1 and Q:5,~(a)nQ.S.;(b) :f: 0. Hence F(a,b) terminates and yields a non-empty set of common-reducts.

2. Suppose F(a,b) -::f. 0. Note that F(a,b) yields a set of common reducts fora and b. Hence a *"'*IJ b.

This strategy presents some drawbacks, the order and the cases of non-termination. Firstly, its order is clearly exponential since we compute the bounded rednetion graphs of the terms. Secondly, this fundion does not terminate in cases the terms are not convertible but normalising. For example, the terms (K x !l) and y are not convertible and F((K x !l),y) does not terminate.

The next two common-reduct strategies we present try to improve these two features, order and cases of non-termination, by avoiding the computation of the bounded graphs and by reducing the cases of non-termination.

A first improverneut in this direction can he done if we use a normalising strategy. In the case we find a normal form in one of the bounded graphs, we stop computing them. Supposing the terms were convertible a.nd one of them normalising then the other term should also he normalising. Here we could apply a normalising strategy to the second term. We first write a function nf that finds the normal form (if it exists) by reducing the spine redexes (see [BKKS87]).

Definition 10.8.7. A fundion nf: T-+ T is defined as follows.

nf(>.x:A. b) = (>.x:nf(A). nf(b))

nf(Ilx:A. B) = (Ilx:nf(A). nf(B))

nf(x a1 ... a,.) = (x nf(a1 ) ... nf(a,.))

nf((>.x:A. b) a1 ... an) = nf(b[x :=at]· .. an)

nf((Ilx:A. B) at ... an) = (Ilx:nf(A). nf(B))nf(at) ... nf(an)

The function nf reduce all the spine redexes at the same time. lt is a semi-algorithm, i.e. it computes the normal form if the term is weakly normalising and it may not terminate otherwise.

Lemma 10.8.8. Let a ET be weakly normalising. Then nf(a) is the normal form of a.

126 GRAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

The second common-reduct strategy is called F+ and applies the function nf to one of the terms when it finds that the other is wea.kly normalising.

Definition 10.8.9. We define F+ : T x T -+ P(T) as follows.

F+(a,b) = If a b then

I { a } el se

If a or b are in normal form then If nf(a) = nf(b) then

I {nf(a)} el se

I 0 end

else

I a+({a}, {b}) end

end

Next wedefine the function H+: P(T) x P(T)-+ P(T). We suppose that it is always applied to subsets X, Y of T that verify the following preconditions.

1. X and Y are the n and m-bounded reduction graphs of a and b.

2. The intersection of X and Y is empty.

3. 0:5 n- m :5 1.

4. X and Y do not contain any normal form.

If we find that Q .... (X) 'èontains a normal form then we do not go on èomputing the bounded graph. We choose one element of Y and reduce it to normal form.

H+(X,Y) If g .... ~'(X) n Y =J 0 then

I Ç .... ~'(X) nY el se

.If there exists a E 9--.,r;(X) in normal form then If forsome bEY, nf(b) =a then

I {a} else

I 0 end

el se H+(Y, X U Ç .... ~>(X))

end end

10.8. TYPE INFERENGE 127

In the following lemma we prove that F+ is a common-reduct strategy. The proof is similar to the one of lemma 10.8.6.

Lemma 10.8.10.

1. If a *-* 13 b then F+ (a, b) terminatea and yields a non-empty set that verifies that for all c E F+( a, b) we have that a -!3 c and b - 13 c.

2. If F+(a, b} ::/; 0 then a *-*!3 b.

In a sense the strategy F+ is 'worse' than F since in F+ the operation nf that computes the normal form can consume a great amount of time and space. One can also argue that F+ is 'better' than F for having a better order and less cases of non-termination.

On one hand, in some cases the order of F+ is less than the order of F. The function F always computes the bounded graph of the termsin a. silly way. While F+ stops computing the bounded reduction graph if one of the terms is weakly normalising. On the other hand, there are cases in which F does not terminate while F+ does. If the terms are not convertible and normalising then F+ (a, b) terminatea and yields the empty set. However F does not terminate in some of these cases, like we have shown before:

Wedefine a third common-reduct strategy that uses weak head reduction. This strategy is the best amongst the ones we presented for ha ving the best order a.nd least cases of nontermina.tion.

Definition 10.8.11. We define F++ : T x T-+ P(T) as follows.

F++(a,b) = If a b then

I {a} else

If a or b are in weak head normal form then

I L(whnf(a), whnf(b)) el se

H++( {a}, {b}) end

end

Note that whnf(a) and whnf(b) may not terminate. If they both terminate, the function L is applied only to weak head normal forms. We define the function L : T x T -+

'P(T) as follows. In case both terms are weak head normal forms and their hea.ds are equal, we try to find a common-reduct for their subterms. In any other case, this function yields the empty set.

128 CHAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

L((.\x:A1. b1),(.\x:Az. b2)) {(.\x:AJ. 6:3) I A3 E F++(AI.Az) & b3 E F++(bl,b2)}

L((IIx:A1. b1), (IIx:Az. bz)) = {(IIx:A3. b3) I A3 E F++(Al, Az) & b3 E F++(bl, bz)}

L((x a1 ... an), (x b1 ... bn)) = {{(x c1 ... en) I \f i= 1, n Ci E F++(ai, bi)}

L(a, b) = 0 otherwise

The fundion H++ : P(T) x 'P(7) -) P(T) is always applied to subsets X and Y of 7 that verify the following preconditions.

1. X and Y are the n and m-bounded reduction graphs of a and b.

2. The intersedion of X and Y is empty.

3. 0 :::; n - m :::; 1.

4. X and Y do not contain any weak head normal form.

If the bounded graphs X contains a wea.k head normal form, we choose a b of Y and reduce it to wea.k head normal form.

H++(X,Y) = If 9-~'(X) n Y # 0 then I 9-~'(X) n Y el se

If there exists a E 9-ll(X) in weak head normal form then I Choose bEY, L(a, whnf(b)} else

H++(Y, X U 9-~'(X)) end

end

In the following lemma we prove that the fundion F++ is a common-reduct strategy.

Lemma 10.8.12.

1. If a <+-*{3 b then F++(a,b) terminatea and yields a non-empty set that verifies that for all c E F++(a, b) we have that a -/3 c and b - 13 c.

2. IfF++(a,b)#0thena-13 b.

10.8. TYPE INFERENGE 129

The proof of lemma is similar to the proof of lemma 10.8.6. The stra.tegy F++ is better than F+ ( and better than F) again for ha ving a better

order and less cases of non-termination. On one hand, in some cases the order of F++ is less than the order of F. The function F++ avoids consttucting the bounded graphs in more cases than F+. This is very important since the bounded graphs grow exponentially. If we find that one of the terms is weak head normalising, we try to reduce the other to weak head normal form. On the other hand, the function F++ terminates in cases that F+ does not. More precisely, the stra.tegy F++ terminates and yields the empty set when the terms are not normalising but they are both weak head normalising and their heads are different. The strategy F+ does not terminate in some of these cases like in for example F+((Àx:A. !1), (IIx:B. x)). Many of these casescan appear when we have recursion.

In table 10.1, we compare the strategies defined before by answering the question: does the strategy term i na te?.

Reduction behaviour of the terms a and b F

a«-*b a,bareWN Yes Yes Yes Yes Otherwise May not Yes Yes Yes

Otherwise a,bareWN Yes May not Yes Yes

0 h . 1 a , b are WN with :fi heads

t erw1se Yes Ma.y not Ma.y not Yes

I Otherwise Ma.y not Ma.y not Ma.y Not May Not

Table 10.1: Termination of the Strategies

Semi-algorithm of type inference. Now wedefine a function type that computes the type of a term (up to ,8-conversion) in a singly sorted pure type system. If a is typable in r in a singly sorted pure type system then type(f,a) terminates and yields the type of a in r (up to ,8-conversion), i.e. if rf-a: A then type(f,a) -iJ A. If the term a is not typable in r then type(r' a) either yields .l or it does not terminate.

This function is obtained from the syntax directed set of rules defined insection 10.7 for pure type systems. For each rule, we write a case of 'pattern matching'. The first case in this definition is for the axiom rule. The following three cases correspond to.the start , the weakening and the product rules. The conditions that a.ppear in these rules that are of the form 'f f- A :-11 s' are replaced by 'whnf(type(r, A)) = s'. The fifth case corresponds to the abstraction rule. Here we need to define an auxiliary function typew to compute the type in a pure type system without the '11-condition. The condition that appears in this rule, 'f 1-w (IJx:A. B) :-p s' is replaced by 'whnf(typew(r, (IIx:A. B))) = s'. The last case corresponds to the application rule. The condition 'r f- b :-t'tp (IIx:A. B)' that appears in this ruleis repla.ced by 'whnf(type(r, b)) (IIx:A. B)'. The other condition in this same rule 'A -iJ A" is replaced by F++(A, A') i: 0.

Definition 10.8.13. The function type8 : C x 7 -+ T1. (or just type) is defined as follows.

130 CHAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

type(c::,s) = s' if (s,s') E A

type(<: r,x: A >,x) A ifwhnf(type(f,A)) =sE S and x is r-fresh

type(< r,x: A >,b) = type(f,b) if b E CU V, b =f x x is r~fresh and whnf(type(f,A)) =sE S

type(r, (llx:A.B)) SJ if whnf(type(r, A)) = sh whnf(type(< r,x: A >,B)) s2, and (s1,s2,s3) ER

type(r, (>.x:A. b)) = (llx:A. B) iftype(< r,x: A >,b) =Band whnf(typew(r,nx:A. B)) =sE S

type(r, (b a)) = B[x:=a] if whnf(type(r, b)) = (llx:A. B), type(r, a) = A' and F++ (A, A') =f 0

type(f,a) .L otherwise

The function types : C x T --+ Ti is defined exactly like type except for the case that corresponds to the abstraction rule. In that case, the condition whnf(typew(r, llx: A. B)) = s E S is removed. The function typew computes the type of a term in a pure type system without the TI-condition.

The first part of the following theorem sa.ys that if the term has a type in a singly sorted pure type system then this is computed by type (up to ,8-conversion). The second part says that the value type(f,a) =f l. is the type of a in r in a singly sorted pure type system.

Theorem 10.8.14. (Correctness of 'type') Let S = (S, A, R) he singly sorted such tha.t the sets S, A and R are recursively enumerable.

1. If r 1- a : A then type(r, a) termin at es a.nd type(f, a) *"""' f3 A.

2. If type(r, a) terminates and yields A then · r 1- a : A.

Three functions are used in the definition of type: typew, whnf and F++; The function typew computes the type of a term in a pure type system without the TI-condition. The other two fundions are used to solve the side-conditions, whnf computes the weak head normal form of a term and the other F++ is a common-reduct strategy.

The termination of typew and type depend on the termination of whnf and F++. If whnf or F++ do not terminate then neither do type and typew.

Type checking can be solved from type inference. Using the function type that infers the type of a term, we write another function check that checks if a term has certain type.

10.9. CONCLUSIONS AND RELATED WORK

Definition 10.8.15. Wedefine the function check: C x T x T-+ Bool as follows.

{

true

check(r,a,A) =

false

if whnf(type(r, A))= sE s, type( r, a) f:. .L a.nd

F++(type(r,a),A) f:. 0 otherwise

131

We have the following condusion which has been proved before in [BJMP93] and [Pol96J (see theorem 9.3.19).

Theorem 10.8.16. (Decidability of Type Inference and Type Checking) Let S = (S, A, R) be singly sorted such that the sets S, A and Rare recursive. If ..\(S) is ,8-weakly normalising then type inference and type checking in ..\(S) are

decidable.

Proof: Suppose that ..\(S) is norma.lising. Then whnf and F++ are applied to norma.lising terms and hence they terminate. Since the sets of the specification are recursive, we have that typew terminatea and so do type and check. Therefore type inference and type

. checking for ..\( S) are decidable. 0 ·

10.9 Conclusions and Related Work

Type Inference semi-algorithm. In order to solve the type inference problem for pure type systems we have first considered a syntax directed set of rul es and then we have written a function that infers the typebasedon this syntax directed set of rules. In table 10.2 we illustrate our methods.

11-condition Original Type system with syntax Type inference type system directed rules semi-algorithm

included .\(S) À.a(S) type removed Àw(S) À~a(S) typew

Table 10.2: Type Inference Semi-a.lgorithm

In the first column of the table we indicate if the 11-condition is included or removed, in the second one we give the narnes of the original type systems (with and without the 11-condition), in the third one we give the corresponding type systems with a syntax directed set of rules and finally the functions that infer the type in the original systems.

The definitions of type and typew are based on the systems that appear next to them in the preceding column.

The 11-condition of À(S) is checked in the same system, wherea.s the one of À.a(S) is checked in À~d(S). Therefore in the definition of type, the 11-condition is checked using the function typew and not type. In other words, we have to use typew to define type.

The 11-condition of "\ .. a(S) is checked in À~d(S) that is weaker than À.a(S).

132 GRAPTER 10. TYPE INFERENGE FOR PURE TYPE SYSTEMS

We have proved that if Sis a singly sorted specification then Àsd(S) is 'equivalent' to ,\(S) by using the relations shown in the diagram below. (see theorems 10.7.7 and 10.7.8).

[

>.(S) ~ À.a(S)

c.p n n

w(S) ~ ,\~d(S)

Related work. Several syntax directed sets of rules for pure type systems are stuclied in [BJMP93]. Our definition of a syntax directed set of rules follows the idea in [BJMP93] of using an auxiliary system to check for the 11-condition. In that paper, the auxiliary system is muchweaker than >.(S). In our case, the auxiliary system is >.w(S), which is very close to >.(S). Moreover, ,\w(S) preserves some properties of >.(S) like normalisation.

In [Pol93a] a syntax directed set of rules for bijective pure type systems is presented. The class of bijective pure type systems includes all systems of the >.-cube and is a proper subclass of the class we study here, the class of singly sorted pure type systems. The class of bijective pure type systems does not include any of the systems of the family of AUTOMATH as described on page 216 and 217 in [Bar85].

Decidability of type checking for normalising pure type systems whose set of sorts is finite is proved in [BJ93]. In that paper, a type inference algorithm is defined that computes the normal form in all the rules. A discussion on the side-conditions can he found in [Pol96]. In this paper, decidabilîty of type inference is proved for normalising pure type systems that are either singly sorted or semi-full under the assumption that the sets forming the specification are recursive. In theorem 10.8.16, we have given a new proof of the same result for the singly sorted but not for the semi-full pure type systems.

Concerning the problem with a-conversion, we define substitution for the set of pseudoterros as in [CF58]. Using this definition of substitution the variabie convention in [Bar85] is not necessary. The typing rules for pure type systems do not allow to type terms unless their nested variables are all different. In the start and the weakening rules a variabie is added to the context only if it is f-fresh. The term (>.x:A. >.x:B. x) is not typable because the second occurrence of x is not < A : *, x:A >-fresh. It is necessary to perform a-conversion to ensure subject reduction. In fact subject reduction holds up to aconversion (see [Pol93b]). In the context A:*, the term (>.y:(A--+ A). >.x:A. y)(>o.x:B. x) is typable in a pure type system. This term red u ces to (,\x: A. >.x: B. x) which is not typable. Subject reduction holds if we identify (>.x:A. >.x:B. x) with (>.z:A. >.x:B. x)

A solution to the implementation of a-conversion appeared in [Bru72]. Here, reference numbers to the positions of the abstractions are used instead of name variables. Another solution can he found in [Coq96]. In this case, a semantica! argument is used to prove the correctness of the type checking algorithm.

For pure type systems, the problem of uniikation does not arise when inferring the type. In typing à la Curry, since the types of the variables are unknown, we have to solve

10.9. CONCLUSIONS AND RELATED WORK 133

the uniikation problem when checking and inferring the type. If we are checking whether an application (! a) has a type B we do not know the type of the argument a. We could infer a typefora and a type for f. The terms f and a may have several types but we could infer the principal type, i.e. a type from which all others can be obtained by substitution. When we have inferred the principal types of f and a, we need to find a. unifier for the type of f and a type whose domain is the type of a. The unification and type inference problems for normalising systems à la Curry are not alwa.ys decida.ble.

134 CHAPTER 10. TYPE INFERENCE FOR PURE TYPE SYSTEMS

Chapter 11

Pure Type Systems with Definitions

11.1 Introduetion

A pure type system does not provide the possihility to introduce a. definition, i.e. a.n a.hhrevia.tion (name) for a larger term which can he used several times i~ a program or proof. A definition mechanism is essential for practical use, and. indeed implementations of pure type systems such as Coq [Dow91], Lego [LP92] or Constructor [HA91] do provide such a facility, even though the formal definition of the systems they implement do not. In this chapter, we metend the pure type system to include (non-recursive) de:finitions.

The extension of a pure type system with definitions looks very harmless and this may not seem a topic worthy of investigation. However the local definitions complicate matters and it is an open problem whether extending an arbitrary pure type system with de:finitions preserves strong normalisàtion or not. Worse still, proving strong normalisation for particular pure type systems extended with definitions is already a problem. The st rong normalisation proofs for partienlar type systems given in [Coq85], [Luo89], [GN91], [Bar92] cannot be extended in any obvious way.

In this chapter, we show how strong normalisation of a pure type system extended with definitions follows from strong normalisation of another (larger) pure type system. This enables us to prove that for àll strongly normalising pure type systems that we know the extensions with definitions are also strongly normàlising.

In the systems ofthe AUTOMATH family [NGdV94] definitions are considered as part of the formallanguage. The meta-theory of these systems -including strong normàlisation -is treated in detail in [Daa80]. However, the proofs of strong normalisation apply only to the partienlar type system that they consider and do not extend to other type systems.

This chapter is organised as follows. In section 11.2 we define the pseudoterros and the pseudocontexts extended with definitions, the ó-reduction and the typing rules for definitions. In section 11.3 we prove properties for all pseudoterros like confluence for {38-reduction and strong normalisation for 8-reduction. In section 11.4 we prove properties for typable terms. We prove that weak normalisation is preserved by the extension. This

135

136 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

property is easy to prove. We first unfold all the definitions and then we perform (Jreduction. The unfolding of definitions may be inefficient and we want to perform other strategies for (JS. Hence we prove that strong norma.lisation is preserved by the extension for a class of pure type systems.

11.2 Pure Type Systems with Definitions

We define a functor ).,/ from the category of specifications to the category of environmental abstract rewriting systems with typing similar to À. A pure type systems with definitions is a value ,\8(S) for SE Spec of ,\8 given by a 4-tuple:

1. a set 75 of pseudoterros

2. a set Cs of pseudocontexts,

3. two reduction relations on pseudoterros and pseudocontexts: one reduction is the (J-reduction and the other relation is called 6-reduction,

4. a typing relation denoted by f-Ó.

11.2.1 Pseudoterms

Definitions will he of the form x=a:A. A definition x=a:A introduces x as an abbreviation of the term a of type A.

Definitions are allowed both in pseudocontexts, e.g. r,x=a:A, and in pseudoterms, e.g. x=a:A in b. Definitions in pseudocontexts are called global definitions and definitions in pseudoterros are called local definitions.

Next we extend the set of pseudoterros to include local definitions, expressions of the form (x=a:A in b).

Definition 11.2.1. Let S = (S, A, R) he a specification. The set 75 of pseudotermsis given by

75 ::= V I sI (75 75) I (.\V:75. 75) I (IIV:Ts. 4) I (V=4:76 in 4)

where V is the set of variables and S is the set of sorts.

Definition 11.2.2. The mapping FV : 75 -+ P(V) is defined as in definition 9.3.2 by adding the following case.

FV(x=a:A in b) FV(A) U FV(a) U (FV(b)- {x})

We say that x is /ree in a if x E FV(a).

11.2. PURE TYPE SYSTEMS WITH DEFINITIONS 137

Definition 11.2.3. The mapping BV : Ts --+ P(V) is defined as in definition 9.3.3 by adding the following case.

BV(x=a:Ainb) = BV(A)UBV(a)U(BV(b)U{x})

We say that x is bound in a if x E BV(a).

Definition 11.2.4. The result of substituting d for (the free occurrences of) x in e is denoted as e[x := dJ and defined as in definition 9.3.4 by adding the following cases.

(x=a:A in b)!x :=dJ = (x=a:A in b) (y=a:A in b}[x :=dJ (y=a[x := d):A[x :=dJ in b[x :=dJ) if x=/= y and y (j_ FV(d) (y=a:A in b)[x :=dJ = (z=a[x := d):A[x :=dJ in b[y := z][x :=dJ) if x=/= y, y E FV(d)

and z is fresh

The set of pseudoterros with holes in it is defined as follows.

Definition 11.2.5. LetS= (S, A, R) be a specification. The set 'Hs is given by

'Hs ::::::: []I V I SI ('Hs 'Hs) I (.>.V:'Hs. 'Hs) I (IIV:'Hs. 'Hs) I (V='Hs:'Hs in 'Hs)

where V is the set of variables and S is the set of sorts.

An element in H.s is denoted by C[ ]. Next we extend the set of pseudocontexts to include global definitions, expressions of

the form r, x=a:A, f'.

Definition 11.2.6. Let S be a specification. The set Cs of pseudocontexts is given by

i) t E Cs

ii) < f,x:A > E Co if f E Cs, x E V,A ETs and x is f-fresh

iii) < f,x=a:A > E Cs if f E Co, x EVa ETs, A ETs, x is f-fresh and x (j. FV(a) u FV(A)

Note that the set of pseudocontexts is not given by

Cs::= tI Cs, V: Ts I Cs, V Ts : Ts .

We have additional requirements for the weli-formation of the pseudocontext r,x=a:A. We require that x should he f-fresh in order to avoid capture of bound variables in the definition of 6-reduction given below. Moreover we require that x (j_ FV(a) U FV(A) in order that definitions are not recursive.

The expression r, x=a:A stands for the pseudocontext < r, x=a:A >. Next we define a mapping Dom that gives the set of variables declared in a pseudocon

text.

138 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

Definition 11.2. 7. The mapping Dom : C5 -t 'P(V) is defined as definition 9.3. 7 by adding the case for definitions.

Dom(r,x=a:A) = Dom(f) U {x}

Next we define a mapping Def that gives the set of variables declared as definitions in a pseudocontext.

Definition 11.2.8. The mapping Dej : C5 -t P(V) is defined as follows.

Dej(E) Dej(r, x:A)

Def(f,x=a:A)

= 0 Def(f)

= Dej(f) U {x}

Definition 11.2.9. The result of substituting d for (the free occurrences of) a variabie x in r such that x rf. Dom(f) is denoted as f[x := d] and is defined as in definition 9.3.8 by adding the following case for definitions.

< f,y=a:A >[x:= d] =< f[x := d],y=a[x := d]:A[x := d] >

Definition 11;2.10. Let d E 'Fö. A change of a bound variable in the term d is the replacement of a subterm (x=a:A in b), (>.x:A. b) or (IIx:A. b) by (y=a:A in b[x := y]), (>.y:A. b[x := y]) or (ITy:A. b[x := y]), respectively, where y f/. FV(b).

Definition 11.2.11. The pseudotermbis a-convertible tob' if b' is the result of applying to b a series of changes of variables or vice versa.

Convention 11.2.12. Two terros are identified if they are a-convertible.

11.2.2 Reductions

In this section we define the {:J and the 6-reduction. The ,8-reduction is defined as usual.

Definition 11.2.13. The ,8-reduction is written as a -tp a' and is defined by the following rule.

C[(>.x:A. b)a] -tiJ C[b[x :=a]]

where C[] E 'Hs has only one occurrence of [ ].

The intended meaning of a definition (x= a: A in b) is that the definiendum x can be substituted by the definiens a in the expression b. A definition (x= a: A in b) can he considered as having a simHar behaviour to (>.x:A. b)a, i.e. the substitution of the variabie x by a in the expression b. In the {:J-reduction where (>.x:A. b)a reduces to b[x :=a], the opera.tion b[x :=a] is the substitution of all the occurrences of x by a in the expression b. In contrast to {:J-reduction, the expression (x= a: A in b) red u ces to the expression (x=a:A in b') where b' is obtained from b by unfolding one occurrence of x by a. This is dearly illustrated in the example we presented in section 1.2.2. In order to perfarm the unfolding of a definition, we introduce a new relation called 8-reduction.

11.2. PURE TYPE SYSTEMS WITH DEFINITIONS 139

Definition 11.2.14. We define the S-reduction (or -+s) as the smallest relation on Csx'Ts x'Ts closedunder the following rules (we write f 1- d -+0 d! insteadof (f, d, d') E-+6):

ft.x=a:A,f21- x -+6 a

r 1- (x=a:A in b) -+s b

r, x=a:A 1- b -+s b' f 1- (x=a:A in b) -+s (x=a:A in b')

r 1- a -6 a' r 1- (x=a:A in b) -+6 (x=a':A in b)

r 1- a-s a' r 1- (a b) -/i (a' b)

f, x:A 1- a-+& a' f 1- (Àx:A. a) -+s (Àx:A. a')

f,x:A 1- a -+6 a' r 1- (IIx:A. a) -+6 (ITx:A. a')

if x~ FV(b)

f 1- A -+sA' r 1- (x=a:A in b) -+6 (x=a:A' in b)

r 1- b -+s b' r 1- (a b) -·(a b')

f 1- A -+sA, f 1- (Àx:A. a) -+6 (Àx:A'. a)

f 1- A -+sA' f 1- (IIx:A. a) -+s (ITx:A'. a)

When r is the empty pseudocontext, a -+sa' is written instead of r 1- a -·a'.

Unfolding of definitions. The first rule allows to unfold definitions. The definiens x reduces to its definiendum a. This rule together with the compa.tibility rules perform the unfolding of global definitions. In a pseudocontext f, x=a:A, f' a termdis ó-reduced toa. term d' if d' is obtained replacing one occurrence of x by a in d.

The third rule allows to unfold local definitions. We consider a local definition as if it were global. The declara.tion x= a: A passes from the pseudoterm to the pseudocontext. Besides, we keep the bound variabie x in (x=a:A in b) until we unfold all the occurrences of x in b.

After unfolding all the occurrences of x in b, we can remove the definition x=a:A from the term. The second rule allows to remove definitions when the variabie x does not occur in b.

Reductions depending on pseudocontexts. The ó-reduction is an example of a rewrite relation depending on a pseudocontext. We have that (Ts,Cs, -t5) and (Ts,Cs, -tps) are environmental abstract rewriting systems. lf R E { -s, *-*s, -ps, *-*ps} a pseudocontext has to be specified, i.e. f 1- aR b fora, b E 'Ts and rE C0 •

Renaming of variables. The variables introduced as definitions are bound variables. Therefore a-conversion is necessary when rewriting terms. For example,

x=y:A 1- (y=z:u in x) *"*a (y'=z:u in x) -ts (y'=z:u in y)

140 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

The variabie y occurs in the term (y=z:u in x) and in the pseudocontext x=y:A. From now on, we assume that all the bound variables of the term and all the declared

variables of the pseudocontext are different. We extend the definition of 8-reduction to pseudocontexts.

Definition 11.2.15. Wedefine the 8-reduction on pseudocontexts (or -t6) as the smallest relation on c6 x c6 closed under the following rules:

r f-- E -t6 E' r,y:E,r' -6 r,y:E',r'

r f-- E -t6 E' f,y=e:E,f' -t6 f,y=e:E',f'

r f-- e -6 e' r, y=e:E, f' -t6 r, y=e':E, f'

We extend the definition of ,8-reduction to pseudocontexts.

Definition 11.2.16. Wedefine the ,8-reduction on pseudocontexts ( or -tf3) as the smallest relation on c6 x c6 closed under the following rules:

11.2.3 Types

r f-- E -tf3 E' r,y:E,r' -{3 r,y:E',r'

r f-- E -tf3 E' f,y=e:E,r' -tf3 f,y=e:E',f'

r f-- e -{3 e' f,y=e:E,f' -tf3 f,y=e':E,f'

We define the typing relation f--6 which allows to type definitions.

Definition 11.2.17. The typing relation f--68 (or f--6 for short) is the smallest relation on C x T x T closed under the following rules and the rules of definition 9.3.14 (we write r f--6 b: B insteadof (f, b, B) E f--6) :

(8- start)

( 8-weakening)

( 8-formation)

( 8-introduction)

( 8-conversion)

r f--6 a: A r,x=a:A P x: A

rf--6b:B rf--6a:A r,x=a:A Pb: B

f,x=a:A f--6 B: s r P ( x=a:A in B) : s

r,x=a:A f--6 b: B r f--6 (x=a:A in B): s r P (x=a:A in b) : (x=a:A in B)

r f--6 b: B r f--6 B' : s r f-- B *"'*6 B' rP b:B'

where x is r-fresh

where x is r-fresh

11.2. PURE TYPE SYSTEMS WITH DEFINITIONS 141

where s ranges over sorts, i.e. sE S.

Definition 11.2.18. The functor ,\6 : Spec ~ Carst is defined for SE Spec a.s follows.

The functor >.6 : Spec ~ Carst is defined for f E Spec a.s the extension of f to the set of pseudoterros and pseudocontexts.

We have to verify that .\6(S) E Carst and that .\6(!) E Carst. For .\6(S) E Carst, we have to prove the subject and type reduction properties. which are proved in the following sections. For the second one, we have to verify that >.6(!) preserves the rewrite and the typing relations. This is very ea.sy to prove.

Definition 11.2.19.

A pure type system with definitions (DPTS) is defined a.s an element of

A singly sorted pure type system with definitions is defined a.s an element of

p 6(S) I s E Spec & s is singly sorted }

Observe that the system ,\8(S) is an extension of >.(S).

Explanation of the typing rules, The 8-start and 8-weakening rules allow the typing of globa.l definitions. We cannot add x=a:A to the context r unless the term a ha.s type A in r. These rules ensure the correctness of what we abbreviate. Moreover they do not allow to abbreviate topsorts. For example in the systems of the À-cube it is not possible to abbreviate D.

The 8-formation and 8-introduction rules allow the typing of local definitions. These rules are similar to the abstraction and the II-formation rules. They differ in the fact that the 6-formation ruleis not restricted by a set R of rules like the II-formation rule.

We could have removed the 8-formation rule and the condition 'r f--6 (x=a:A in B) : s' of the 8-introduction rule. In that case correctnessof types and subject reduction would hold in a weaker form. The weaker form of correctness of types is stated as follows: if r t-.5 a : A then either A -ps s ot r f--6 A : B and B -ps s. The weaker form of subject reduction is stated a.s follows: if r t-.5 a : A and a -ps a1 then there exists A' such that r f--6 a': A' and A -ps A' (see also definition 4.3.1).

The 8-conversion rule plays an important role in typing definitions. This rule allows to use the fact that the definiens x and the definiendum a are ( 8-)equal for typing a term.

142 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

Definition vs abstraction and application. A definition ( x=a:A in b) is not another way of writing (.:\x:A. b)a. There are important differences between (x a:A in b) and (À x: A. b )a, both regarding their reduction behaviour and their typing. One reason for consiclering (x=a:A in b) and not (.:\x:A. b)a is tliat insome cases it is convenient to have the freedom of substituting only in some of the occurrences of an expression in a given formula.

Another reason for consiclering (x=a:A in b) and not (.:\x:A. b)a is that the first may he typable when the second is not. There are two situations where this happens:

1. The fact that x is a.n abbreviation for a can he used to type b. This is shown in the following example.

Example 11.2.20. The term .:\a:*· (X= a:* in Ày: X . .:\f: a -+ a. fy) is typable in the system .:\2 extended with definitions. But it is not possible to type the corresponding term expressed with an application and an abstraction in .:\2. As a matter of fact, the following term is not typable in any system of the À-cube.

In this term the application fy is not well-typed because the type X of the argument y doesnotmatch the type a-+ a of the function f. In the first term this application is well-typed because we know that X is an abbreviation of a. Note that here we apply the ó-conversion rule.

2. The abstraction (.:\x:A. b) may not be allowed in a given type system.

Example 11.2.21. The term (X=a-+ a:* in >.y:X. Àj:X-+ X. fy) is typable in the system .:\ ... extended with definitions. The corresponding term expressed with an application and an abstraction, i.e. (.:\X:*. Ày:X . .:\f:X-+ X. fy)a-+ a, is not typable in .:\_. because in .:\_. abstractions over type variables are not allowed.

Properties of pure type systems with definitions. In the following sections we will prove these properties:

• Confluence for -+s and for -+f35·

• Strong normalisation for -+6 •

• (16, Cs, -+f3ó, j-5) verifies subject reduction, i.e. if r f- b -+pó b' and r j-.5 b : B then rf-Sb':B.

• (16,Cs,-+ps,f-Ó) verifies type reduction, i.e. if r f- B -+ps B' and r f-Ó b: B then r f-6 b: B'.

11.3. PROPERTIES OF PSEUDOTERMS 143

• Uniqueness of types for singly sorted specifications, i.e. if f f-6 d : D and f f-6 d : D' then f 1- D *""*PS D' with sa singly sorted specification.

• Conservativity, i.e. for A E T, f E C 3a f 1- a : A iff 3a f f-5 a : A.

• Strengthening, i.e. if f 1 , x:A, f 2 f-6 b: B and x ~ FV(f2) U FV(b) u FV(B) then f1,f2f-6b:B.

• If a pure type system is ,86-weakly normalising then the corresponding pure type system with definitions is ,86-weakly normalising.

• An extension of a pure type system is ,86-strongly normalising if a 'slightly' larger pure type system is ,8-strongly normalising. In particular, the Calculus of Constructions extended with definitions is ,86-strongly normalising.

11.3 Properties of Pseudoterms

In this section, we prove properties of ,8 and 6-reductions for all pseudoterms. Amongst these properties, we will prove confluence for li and for ,8/i. Besides we will prove weak and strong normalisation for 6-reduction.

11.3.1 Basic Properties

In the following lemma we show that a 6-reduction step remains invariant if we enlarge the context. The proof is done by induction on the definition of -+s.

Lemma 11.3.1. Let <ft. f 2, f 3 >E Cs be such that ft. f 3 1- b -+5 b'. Then

Both implications from left to right of the following lemma are a. particula.r case of lemma 11.3.1. Both implica.tions from right to left allow to make the context shorter. The first part states that declarations of the form x: A can always be removed from the context. The second part states tha.t dedarations of the form x= a : A can be removed from the context only if x ~ FV(b). This a.llows to remove global definitions as the second rule in the definition of li-reduction does for local definitions. Both parts are proved by induction on the definition of ,8 and lJ.

Lemma 11.3.2.

1. Let < f, x:A, f' >E Cs and b E 'Tt;. f, f' f- b -+ ps b'if and only if f, x:A, f' 1- b -+ ps b'

2. Let< f,x=a:A,f' >E Cs and b E Tt; be such that x t/: FV(b). f, f' f- b -+ps b' if a.nd only if f, x=a:A, f' f- b -+ps b'.

144 GRAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

In the following lemma we show that the compatibility rule for ( x=a:A in b) when we reduce inside b is a derivable rule.

Lemma 11.3.3. The following ruleis derivable from the ones in the definition of -+ii.

r f- (x=a:A in b) -+s (x=a:A in b')

Proof: By lemma 11.3.1, it follows thatf,x=a:A f- b -+a b'. By definition of -+a, it follows that r f- (x=a:A in b) -+s (x=a:A in b'). o

We will prove that a definition (x=a:A in b) has the same behaviour as ().x:A. b)a in the sense that the definition (x=a:A in b) !5-reduces in several steps to b[x :=a]. Then the unfolding of definitions that is achieved via the S-reduction corresponds to the operatien of substitution b[x :=a]. The proof is clone by induction on the structure of b.

Theorem 11.3.4. Let r =< ft.x=a:A,f2 >. Then

r f- b-s b[x aJ.

Cor()llary 11.3.5. r f- (x=a:A in b) -+t b[x :=a].

Proof: By theorem 11.304, it follows that r, x=a:A f- b -s b[x :=a]. Then

r 1- (x=a:Ain b) -ó (x=a:A in b[x :=a]) -+s b[x :=a]

Note that x f/. FV(b[x :=a]). 0

The following lemma is proved by induction on the structure of a.

Lemma 11.3.6. (Substitution Lemma) Suppose x {y and x f/. FV(d). Then

a[x := b][y :=dJ= a[y := d][x := b[y :=dj]

The following lemma shows that f1 is substitutive. It is proved by induction on the generation of -+p.

Lemma 11.3.7. (Substitutivity for {3) If a-+(:! a' then a[x := b] -+p a'[x := b].

The following example shows that ti is not substitutive for those variables which are definitions.

11.3. PROPERTIES OF PSEUDOTERMS 145

Example 11.3.8. Let r be the context < A : *• id=( >.y:A. y ):(ITy:A. A) >. r r id -+s (>.y:A. y)

But it is not true that r r id[id := b]-+11 (>.y:A. y)[id := b] for all b E 78. In this case the variabie id is a definition, this means that it can be substituted only by the definiendum (>.y:A. y).

Lemma 11.3.9. If f, x=a:A, f' r b -<f3s b' then r, f 1 [x :=a] r b[x := a]-ps b'[x :=a].

In the following lemma we reduce inside the pseudoterm a of b[x := a]. This lemma holds for any variabie x, including those which are definitions. It is proved by induction on the structure of b.

Lemma 11.3.10. If r r a -"+{3/j a' then r r b[x := a] --'hf3S b[x := a'].

Lemma 11.3.11. If r ra -+136 a' and r, x=a:A, f' r b -+6 b' then r, x=a':A, r' r b -p5

b'.

The previous lemma is proved by induction on the definition of r, x=a:A, f' r b -+6 b'.

11.3.2 Confinenee for (3, 6 and (38-reductions

The proof of confluence for .8-reduction is very easy. It follows from the fact that the combinatorial reduction system < 78, -<p> is orthogonal(see [Klo90]),

Theorem 11.3.12. (Confluence for .8-reduction) Let a,aha2 E 78 such that a --'hf3 a 1

and a -f3 a2 • Then there exists a pseudoterm a3 such that a1 -11 a3 and a2 -f3 a3.

We prove confluence for ti and ,Bti-reductions using the criteria proved in chapter 4. The idea of the proof of confluence for 8-reduction is as follows. We define a 'projection mapping', 1-1-: Cli x 78-+ T. The 'projection' lalris a pseudoterm that is obtained from a by unfolding all the definitions occurring in r and in a. First we prove that this mapping is a strategy for ti-reduction. Then we prove that this mapping is a forgetting morphism from (78, Cs, -+s) to (7, -+f3)· Finally, the proof of confluence for ,88-reduction follows from the previous considerations and the fact that the projection mapping is an implementing morphism from (78, -<p) to (T, -+f3)·

Next wedefine the mapping I 1-· Definition 11.3.13. The mapping I 1- : cli x 78 -+ T is defined as follows.

lxlr if r = r 1, x=a:A, r 2

otherwise

!sir = s

la blr lalrlblr j>.x:A. air

jiix:A. Blr (>.x:jAir. lalr"" . .A) (IIx:IAir· IBir,,.-.A)

lx=a:A in blr = lblr[x := !air] where x is f-fresh.

146 GRAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

The value ldlr is obtained from d by unfolding all the global and local definitions. The unfolding of global definitions is performed in the first line lxlr = lalr,. The unfolding of local definitions is performed inthelast line !x=a:A in blr = lblr[x := lalr].

Note that ldk does not contain either local or global definitions, i.e. ldlr E T and Dej(f) n FV(Jdlr) = 0. Conversely, if d E T is such that Dej(f) n FV(d) = 0 then \dlr = d. This means that for those pseudoterms which contain neither global nor local definitions, the mapping 1-1- is the identity. Hence I~ 1- is a 'projection' from '4 and Cs toT. Later, we will prove that ldlr is the 8-normal form of the pseudoterm d.

The function FV: '4 --> 'P(V) is extended to Cs x 'Tr;. We say that FVr(b) is thesetof free variables of b with respect to r.

Definition 11.3.14. The mapping FV: Cs x '4-+ P(V) is defined as follows.

FV.(b) = FV(b) FKA,r(b) = FV(A)U(FVr(b)-{x})

FV.,=a:A,r(b) = FVr(A)UFV(a)U(FVr(b)-{x})

Lemma 11.3.15.

1. If x~ FV(b) and x is f-fresh then x ~ FV(Ib!r).

2. Let< f 11 f2,r3 >E Cs and b ETs be such that (FVr3 (b)) n Dej(f2) = 0. Then

lblr, ,r2,ra=lblr,,ra·

3. Let < fh x=a:A, f2 >E Cs and a E 'Tr;. Then ialr,,=:A,r2 = \air,.

Pro of:

0

1. lt is proved by induction on the number of symbols occurring in r and b.

2. It is proved by induction on the structure of b.

3. None of the variables in Def(x=a:A, f 2) can occur in a. Hence the result follows immediately from the previous part.

The following lemma states that 1-1- preserves substitution. Also it shows that I 1-yields the same value for global and local definitions and this value is given by substitution. It is proved by induction on the structure of b.

Lemma 11.3.16. Let < r, x=a:A >E Cs and b E 'Tr;. Then

lblr [x:= lalr] = lb[x :=a] Ir = lblr,x=a:A

The following lemma states that a pseudoterm reduces to its projection.

11.3. PROPERTIES OF PSEUDOTERMS 147

Lemma 11.3.17. The 'projection mapping' is a strategy for 5-reduction. In other words,

f 1- d -6 ldlr for all dE 4, f E C5.

Proof: It is proved by induction on the number of symbols occurring in f and in d. Only two cases are considered.

• Assume d =x. There are two possibilities, either x E De/(f) or not.

If x E De/(f) then f =< f1ox=b:B,f2 >.

r 1- x -+IJ b -5 lbl!'1 by induction hypothesis and lemma 11.3.1 = lxlr

If x tf. Def(f) then f 1- x -5 x= lxlr·

• Assume d = (x=a:A in b). By induction hypothesis, it follows that r 1- a -5 lak, f 1- A -s !Al!' and that f 1- b -s lblr·

r 1- (x=a:A in b) -• (x=lalr:IAir in lblr) by definition of 5 and lemma 11.3.3 -s lblr[x := lalr] by corollary 11.3.5 = lx=a:A in blr

The rest of the cases are easy to prove. 0

The following lemma states that the projectionsof two pseudoterms that are in -+s are equal.

Lemma 11.3.18. The projection mapping is a forgetting morphism from (4,Cs, -+s) to (T,-+p), i.e. if f 1- c -+s d then jcjr = ldlr for all c,d E 4 and f E C5.

Proof: It is proved by induction on the structure of c. Only some cases are considered.

• Suppose that c = x. This means that f f 1, x=a:A, f 2 and f 1- x -+5 a.

lxlr lalr1 = lalr by lemma 11.3.15

• Suppose that cis (x=a:A in b) and f 1- (x=a:A in b) -+s b with x tf. FV(b).

lx=a:A in blr lblr[x := lalr] = lblr by lemma 11.3.15 part 1

The rest of the cases are easy to prove. 0

148 GRAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

Theorem 11.3.19. (Confl.uence for 8-reduction) If r 1- a-s a1 and f f- a-s a2

then there exists a3 such that rf-al -8 a3 and r 1- a2 -s a3.

Proof: By theorem 11.3.17, the projection mapping is a strategy for 6-reduction. Moreover it follows from lemma 11.3.18 that this mapping is a forgetting morphism. By lemma 2.6.1, -+s is confluent. 0

The following lemma states that the projection preserves ,8-reduction.

Lemma 11.3.20. Let r E Cs. The projection mapping I-lr is an implementing morphism from (75, -+,e) to (T, -+,e), i.e. if a -+13 a' then !air -13 la'lr, for all a, a' E 75.

Proof: lt is proved by induction on the structure of a. Only the case a= (.:\x:B. b)d and (.:\x:B. b)d -+,e b[x := d] is considered.

lalr 1(.:\x:B. b)dlr = (.:\x:IBir· lblr)ldlr -+fJ lblr[x := ldlr]

lb[x dJ Ir. by lemma 11.3.16

The rest of the cases are easy to prove. 0

Theorem 11.3.21. (Confl.uence for ,88-reduction) If f f- a -.es b a.nd r f- a -.eó c then there exists dE 15 such that f f- b -fis d and f f- c -fló d. .

Proof: We apply lemma 2.6.2 to the abstract rewriting system (T, -+p) and the environmental abstract rewriting system (15,Cs, -+(Is). We fix a pseudocontext, say r E Cs and we consider the ,88-reductioil in r (we write -+(iö insteadof -+f18r ).

• The inclusion mapping is an implementing morphism from (T, -+fi) to (15, -+,es).

• By lemma 11.3.17, we have that the projection mapping I-lr is a strategy for ó and hence for ,86-reduction.

• By lemmas 11.3.18 and 11.3.20, the projection mapping is a.n implementing morphism from (75, -+13s) to (T, -+p)·

Since (T, -+fi) is confluent so is (15,Cs, -+fis). 0

Note that if the contexts f and f' are different then ialr may he different from lalr'·

Lemma 11.3.22. If f -ps f' then lalr -IJ ialr'·

This is proved by induction on the lengthof a and f.

11.3. PROPERTIES OF PSEUDOTERMS 149

11.3.3 Weak and Strong Normalisation for --+8

In this section an illustrative and intuitive proof of weak normalisation for 8-reduction is presented. In order to prove strong normalisation for -s, the well-known method of defining a function w_(-) :Cs x T6 --+IN which decreases with 8-reduction is used. This function computes the length of a maximal 8-rewrite sequence from a term to the 8-normal form.

Note that a pseudoterm can be in 8-normal form but not in ,8-normal form, for example (>.y:*. y )(.\y:A. y ).

According to the following theorem, a pseudoterm a is in 8-normal form in a context r if and only if a does not contain either global or local definitions. The pseudot'erms that do not contain local definitions are in T. The pseudoterros that do not contain global definitions are those pseudoterros whose free variables are not included in the set of definitions of the context. The proof follows easily by induction on the structure of a.

Theorem 11.3.23. Let a ET;;.

a is in 8-normal form in rif and only if a ET and FV(a) n De/(f) = 0.

The projection lalr does not contain either global or local definitions. Therefore lalris in 8-normal form. Since 8-reduction is confluent we have that the 8-normal form is unique. As a consequence of this we have the result that follows.

Corollary 11.3.24. (Weak Normalisation for 8-reduction) The pseudoterm lalris the 8-normal form of a in r.

Note that by corollary 11.3.24 the 8-normal form for an arbitrary pseudoterm exists, but it is not guaranteed that all 8-paths starting at the pseudoterm are finite.

Next wedefine a function w_(-) : Cs x T6 --+ IN that decreases with 8-reduction. We use this function in order to prove strong normalisation for 8. We also prove that wr(b) computes the length of a maximal 8-rewrite sequences starting at b.

Definition 11.3.25. If r E Cs and b E T;;, wr(b) is defined by induction on the number of symbols in r and in b as follows. .

Wft ,x=a:A,rz (X) = Wr1 (a)+1

wr(x) 0 if x i. De/(f) wr(s) = 0

wr(x=a:A in b) = wr(a) + wr(A) + wr,x=a:A(b) + 1

wr(a b) wr(a) + wr(b)

wr(ITx:A. a) wr,x:A(a) + wr(A) wr(Àx:A. a) = wr,x:A(a) + wr(A)

150 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

Lemma 11.3.26. If FV(b) n Dej(f2 ) = 0 then wr,,r2 ,r3 (b)=wr,,r3 (b):

The previous lemma is proved by induction on the number of symbols in < r I' r 2' r 3 > and in b.

Lemma 11.3.27. wr,,x=a:A,r2 (b) ~ wr,,r2 (b).

The previous lemma is proved by induction on the number of symbols in r I' x=a:A, r 2

and in b.

Lemma 11.3.28. If r f- d -+6 d' then wr(d) > wr(d').

Pro of: The following two properties are proved simultaneously by induction on the number of symbols in r and in d.

1. If r f- d -+s d' then wr(d) > wr(d').

2. If r -+s r' then wr(d) ~ wp(d).

We only consider the proof of the first property for the case that d =x. We have that r = r I' x = a : A, r 2 and r f- x -+ 6 a.

D

wr(x) = Wr1 ,x=a:A,r2 (x) = wr,(a) + 1 > wr,(a)

wr(a) by lemma 11.3.26

As an immediate consequence of lemma 11.3.28, we have the result that follows.

Theorem 11.3.29. (Strong Normalisation for ö) The reduction ö is strongly normalisihg.

Finiteness of developments can be deduced from strong normalisation of <5-reduction. This is our third proof of finiteness of developments (see chapter 7).

We write a function that maps a marked redex (,ix:A. b)a into a definition x=a:A in b. This function maps one step of ~-rewrite step into one or more steps of <5-reduction.

We suppose that the terms in /l are typed.

Definition 11.3.30. The mapping 1' : /l-+ Ts is defined as follows.

!(x) = x

!(Àx:A.b) = h:1'(A). !(b)

!(ba) = !(b) T(a) !((,ix:A.b)a) (x=!(a):!(A). !(b))

11.3. PROPERTIES OF PSEUDOTERMS 151

The last clause maps a marked redex (,ix:A.b)a into a definition x=a:A in b.

Lemma 11.3.31. l'(b[x :=a])= !(b)[x !(a)]

This function is a refining morphism, i.e. it maps one step of ,8-reduction to one or more steps of 8-reduction.

Lemma 11.3.32. If a-+(!_ b then !(a) -+t !(b).

This lemma is proved by induction on the structure of a. In the case of a marked redex, (,ix:A.b)a, we use corollary 11.3.5 and the previous lemma.

Theorem 11.3.33. (Finiteness of Developments) The ê_-reduction is strongly normalising.

Proof: By the criterion on strong normalisation 2.6.5, we have that the ,8-reduction is strongly normalising. 0 -

Note that wr(a) is an upper bound for the number of reductions steps in a 8-reduction sequence startingat a in r, i.e. for all n, al, ... ,an,

jf f j- a = a1 -+s az -+s ... -+s an-I -+s an then Wr( a) 2:: n.

We will show that wr( a) is the length of a maximal 6-rewrite sequence from a to its 6-normal form. In order to show that wr(a) is a maximum we will build a 6-reduction sequence of this length. We will define a strategy F!, : Cs x Ts -+ Ts for 8-reduction such that the F!,-reduction sequence of a has length wr(a).

The 8-reduction unfolds only one occurrence of a variabie at a time. We need to give an order to these occurrences in order to define a strategy of reduction. We choose to unfold them from left to right. We will define e[x0 :=dj as the substitution of the leftmost occurrence of the variabie x for d in the expression e.

Definition 11.3.34. Let x E FV(e). The result of substituting d for the leftmost occurrence of x in eis denoted as e[x0 :=dj and is defined as follows.

y[xo := dj = { d if x y y otherwise

(y=a:A in b)[x0 := dj

(IIy:A. B)[x0 := dj

(,\.y:A. a)[x0 :=dj

= { (a[x0 :=dj b) if x E FV(a) (a b[x0 dj) otherwise

{

y=a[x0 := dj:A in b if x E FV(a) = y=a:A[x0 :=dj in b if x rt FV(a) and x E FV(A)

y=a:A in b[x0 := dj if x rt FV(a) u FV(A)

= { (ITy:A[x0 :=dj. B) if x E FV(A) (IIy:A. B[x0 := dj) otherwise

= { (..\y:A[x0 :=dj. a) if x E FV(A) (..\y:A. a[x0 :=dj) otherwise

152 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

The strategy we define for 6-reduction is similar to the perpetual strategy Foo on Àterms and for ,8-reduction(see section 6). The leftmost 6-redex of a pseudoterm could be either a global or a local definition. If the leftmost redex is a variabie x such that x=a:A is in the context then the strategy gives the pseudoterm ohtained by unfolding this occurrence of x by its definiendum a. If the leftmost redex is a definition (x=a:A in b), we check whether the variabie x occurs as a free variabie in b or not. If it doe.s, then the strategy yields the pseudoterm obtained by unfolding the leftmost occurrence of x in b. If the variabie x does not occur as a free variabie in b then we do not remove the definition. We will apply the strategy first to the definiendum a and then to the type A. Only in the case that both a and A are in 6-normal form we remove the definition.

Definition 11.3.35. The mapping F!, : Cs x '18 -+ '18 is defined as follows {we write F!,(b) insteadof F!,(f,b)).

F!,(s)

= { ~ if r = f 11 x=a:A,f2

otherwise

= s

x=a:A in b[x0 := a] x=F!,(a):A in b

if x E FV(b) if x '$. FV(b) and a is not in 6-normal forrn in r

F!,(x=a:A in b) = x=a:F!,(A) in b if x'$. FV(b), a is in 6-norrnal form in f and A is not in 6-normal form in f

F!,(a b)

F!,(llx:A. B)

F!,(>..x:A. a)

b if x'$. FV(b) and a and A are in 6-norrnal form in r

= { ( F!,( a) b) if a is not in 8-norrnal form in r (a F!,(b)) otherwise

{ = {

(IIx:F!,(A). B) if A is not in 6-normal form in f (llx:A. F!,(B)) otherwise

(>..x:F!,(A). a) if A is not in 6-normal form in f (>..x:A. F!,(á)) otherwise

The following two lemmas are proved by induction on the structure of the term.

Lemma 11.3.36. Let x E FV(b). Then wr,,x=a:A,r2(b) wr,,=a:A,db[x0 :=a])+ 1.

Lemma 11.3.37.

1. If bis in ó-normal form inT then F!,(b) b.

11.4. PROPERTIES OF WELL·TYPED TERMS 153

2. If bis not in ó·normal form in r then rI-b -+0 F!,(b) and wr(b) = wr(F!,(b)) + 1

The next theorem states that the F!,~reduction sequence of a has length wr(a). The proof follows immediately from the previous lemma.

Theorem 11.3.38. Let n = wr(a). Then

rI-a -+o F!,(a) -+o ... (F!)n-1(a) -+6 (F!,)n(a) = lalr.

As a corollary we have that wr( a) is the length of a maximal ó~rewriting sequence starting at a and that the strategy F! is maximaL Here maxred( a) denotes the length of the maximal Ó·rewrite sequence startingat a.

Corollary 11.3.39. (Maxima! Strategy for ó)

wr( a) = max.red( a) and F! is maximaL

Proof: By theorem 11.3.28, wr(a) is an upper bound for the number of reductions steps in a 5-reduction sequence startingat a in r, i.e. for all n, al, ... ' an,

if rI-a al -+o az -+o ... -+o an-1 -+lî an then wr(a) 2: n.

By the previous theorem, there is a 5-rewrite sequence of length wr(a) that is the F!rewrite sequence. Hence wr(a) is the lengthof the maximal 5-rewrite sequence and F! is a maximal strategy. 0

11.4 Properties of Well-Typed Terms

The properties in this section are proved for all terms typable in a pure type system with definitions, i.e. for pseudoterms a such that r(r f-6 a : A]. Amongst these properties, we will prove that À(S) is weakly normalising if and only if À6(S) is weakly normalising. Also we wiJl prove strong normalisa.tion of Pó-reduction for a class of pure type systems with definitions.

11.4.1 Basic Properties

In the following lemma we will show that the structure of the term gives a.n idea. of the sha.pe of its type and its deriva.tion. For exa.mple, the type of a.n abstraction will be a product (up to Pó-conversion) a.nd the last part of the deriva.tion consistsof the a.pplica.tion of the abstraction rule and then 0 or more applications of the (3 or the 8-conversion rules.

The different cases of next lemma are all proved by induction on the derivation.

Lemma 11.4.1. (Generation Lemma)

1. If f f-6 s: D then there exists a sort s' such that r 1- D «-'1tf3ó s' and (s,s') E A.

154 GRAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

2. If r f-6 x : D then there exists 8 such tha.t r r B ""'"*fJó D, either r f-6 B : s a.nd x : B E r or f f-Ó b : B and x b : B E f for some b.

3. If r f-6 (IIx:A.B): D then there are sorts such that (st,82 ,83 ) ER, that r f-6 A: 81 ,

that r, x:A f-Ó B: Sz and r r D ""'"*f36 83·

4. If r f-Ó (>.x: A. b) : D then there are s and B such that r f-6 (IIx: A. B) : 8, that r, x: A f-6 b: Band r 1- D ""'"*ps (IIx:A. B).

5. If r f-6 (b a) : D then there are A and B such that r f-.6 b: (IIx:A. B), r f-8 a : A and r r D ""'"*iJë B[x := a].

6. If r f-.6 (x=a:A in b) : D then there exists B such that either r,x=a:A f-6 b: B, r f-6 (x=a:A in B) : 8 and r r D ""'"*f3ë (x=a:A in B) or r,x=a:A f-.6 b: s a.nd r 1- n ""'"*Pó s ·

Observe that in 6, the type of a term (x=a:A in b) can he a sort 8 or a.n expression of the form (x=a:A in B).

Lemma 11.4.2. (Correctness of Types) If r f-6 A : B then there exists 8 such that either B = s or r f-6 B : s.

The previous lemma is proved by induction on the deriva.tion of r f-.6 A: B.

Lemma 11.4.3. If f f-Ó b: (IIx:A.B) then there are sorts (st,sz,83) ER such that r f-Ó A: Stand r,x:A f-Ó B: s2.

Pro of: It follows from lemma 11.4.2 that r f-.6 (IIx:A.B) : s. By the generation lemma part 3, it follows that there are sorts (Si> Sz, s3) E R such that r f-Ó A : St and r, x:A f-Ó B : s2. 0

Reeall that r is a context if there are b and B such that r f-6 b : B.

Lemma 11.4.4. ( Correctness of Contexts)

1. If r, x:A, f' is a context then there exists a sort s such tha.t r r A: s.

2. If r, x=a:A, r' is a context then r f-Ó a : A.

Both parts of the previous lemma are proved by induction on the derivation.

Lemma 11.4.5. (Thinning Lemma) Let f' be a context.

If r f-6 a : A and r Ç f' then r' f-6 a : A.

The previous lemma is proved by induction on the derivation of r f-Ó a : A.

11.4. PROPERTIES OF WELL-TYPED TERMS 155

Lemma 11.4.6. If f f-8 a: A and f, x:A, f' f-8 b: B then r, x:;;:a:A, r' f-8 b: B.

The previous lemma is proved by induction on the derivation of f, x:A, f' f-8 b: B.

Example 11.4. 7. It is not true that if r, f' f-8 b: B then f, x:A, f' f-8 b: B. For instance, we can derive a:*,X:;;:a:*,y:X,J:(a--+ a) f-8 (f y): a. But there is no term B such that a:*,X:*,y:X,J:(a--+ a) f-8 (J y): B. This example

is similar to example 11.2.20.

Lemma 11.4.8. (Substitution Lemma)

1. If r, x:;;:a:A, f' j-.5 b : B then f, f'[x :=a] f-8 b[x a] : B[x ::;;: a]

2. If f j-.5 a: A and f, x:A, f' f-8 b: B then r, f'[x :=a] f-8 b[x :=a] : B[x ::;;: a].

Proof: The first part is proved by induction on the derivation of r,x=a:A,f' f-8 b: B. The second part follows by lemma 11.4.6 and the previous part. D

Theorem 11.4.9. (Subject Reduction Theorem)

If r f-8 d: D and f r d -+p6 d' then f f-8 d' : D.

Proof: The following properties are proved simultaneously by induction on the derivation of r f-6 d: D.

1. If r r d -+pti d' and r f-8 d : D then r f-8 d' : D

2. If r -+p6 f' and f f-6 d: D then f' f-8 d: D

We only give the proof for some cases of the first property. Suppose that the last rule in the derivation of f f-8 d : D is:

( l. . ) f f-6 b : (IIx:A. B) r f-8 a : A

• app teatiOn r p (b a): B[x :=a]

Only one case is considered.

Assume b (.\x:A1. bt) and f r (.\x:At. bt)a -+f3 bt[x :=a]. Generation lemma part 4 and confluence for {Jó, yield A 4.+-'hfJIJ A1 and by also using the conversion rule we have r, x : A1 f-8 b1 : B. By generation lemma part 3, it follows that r f-8 A1 : s and by using the conversion rule we have that r Pa: A1. By substitution lemma 11.4.8 part 2, it follows that f f-8 bt[x :=a] : B[x :=a].

156 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

(, d ) f, x::::a:A f-b b : B f f-b ( x=a:A in B) : s

• u -intro netion r P (x=a:A in b) : (x=a:A in B)

Only one case is considered.

Assume r 1- (x=a:A in b) -+s bunder the hypothesis x f:. FV(b).

By lemma 11.4.4, it follows that rf-ba: A. By the substitution lemina 11.4.8 part 1, it follows tha.t r f-b b[x :=a] : B[x :=a]. Since x f:. FV(b), the equa.lity b[x :=a] = b holds.

By corollary 11.3.5, it follows that r 1- (x=a:A in B) -• B[x :=a].

By conversion rule, r f-b b: (x=a:A in B).

The rest of the cases are easy to prove. 0

Theorem 11.4.10. (Type Reduction Theorem) .

If f f-b d: D and r 1- D -+ps D' then f f-b d: D'.

The previous theorem is proved using correctness of types lemma and subject rednetion theorem.

We extend the mapping I - 1- to contexts.

Definition 11.4.11. The mapping 1- I :Cs-+ Cis defined as follows.

lt:l = €

lf, x:AI = lfl, x:JAir jr, x=a:AI = lfl

Note that if r E C then lfl = r. This means that for contexts that do not conta.in definitions the mapping I - I is the identity. This mapping I - I is the projection from Cs to C.

In the following theorem we prove that the range of I I restricted to the set of typable termsin .V(S) is thesetof typa.ble termsin >.(S).

Theorem 11.4.12. If r f-b a : A then ifll- lalr : I Air.

Proof: Suppose that the last rule in the derivation of r 1-6 a : A is:

rf-ha:A • (6- start) r _ ·A p . A where x is f-fresh. ,x-a. x.

By induction Ir! 1- i air : I Air· By definition 11.3.13 we have that lxlr,=a:A = Ja Jr. By lemma 11.3.15 part 2, it follows that IAir,=a:A = lAk. Hence jr, x=a:AII- lxlr,=a:A : IAir,=a:A·

11.4. PROPERTIES OF WELL-TYPED TERMS 157

(c. t d t" ) f,x=a:A f-6 b: B f f-6 (x=a:A in B): s

• o-m ro uc IOn . . r !-" (x=a:A mb) : (x=a:A mB)

By induction, lf, x a: Al f- lblr,=a:A : IBir,:=a:A· By lemma 11.3.16 and definition 11.3.13 wehave that

lblr,x=a:A = lblr(x := ialr] lx=a:A in blr

IBir,"::a:A IBir[x iair] lx=a:A in Blr·

Hence lfl f- lx=a:A in blr : lx=a:A in Blr-

The rest of the cases are easy to prove. 0

The first part of the next corollary states that a term is typable in ..\(S) iff it is typa.ble in ..\6(S). The second part states that a type is inhabited in ..\(S) if and only if it is inhabited in V(S). If we interpret types as propositions and terros as proofs, this means that a proposition is provahle in ..\(S) iff it is provable in ..\6(S).

Corollary 11.4.13. (Conservativity) Let a ET and fEC. Then

1. 3A r 1- a : A iff 3A r f-6 a : A.

2. Let A ET. Then f 1- a: A iff f f-6 a: A.

3. 3A r f- A : a iff 3A f f-6 A : a.

As a consequence of conservativity, we have the following result.

Corollary 11.4.14. (Undecidability) Let S = (S, A,R) he a specification. The prohierna of type inference, type checking

and inhahitation are undecidable in ..\6(S) if they are undecidable in ..\(S).

Therefore, type inference and type checking is undecidable in inconsistent pure type system with definitions whose specification is impredicative and non-dependent (see theorem 9.3.20). The prohlem of inhahitation in the systems of the ,V-cube (the À-cube extended with definitions) except for .À~ and .À0f:!:1. is undecidable.

Theorem 11.4.15. (Implementing morphism from .X6(S) to ..\(S)) The pair (1-1, l-IJ of projections mappings is an implementing morphism from .X6(S) to .X(S).

Proof: By theorem 11.4.12, the pair (1-1, l-IJ preserves the typing from ..\8 (S) to ..\(S). lt follows from lemmas 11.3.18 and 11.3.20 that (1-1, l-IJ is an implementing morphism from ..\6(S) to .X(S). o

158 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

We write I I instea.d of (I .. J, l-IJ· Note that the previous theorem in fact shows that there is a natura! transformation

from )..6 to ).. when these are considered as functores from the category of specifications into a category analogous to Carst where morphisms are the implementing morphisms.

Corollary 11.4.16. (Semantics) Asemantics for )..(S) is asemantics for )..6(8).

Pro of: Suppose there is an intepretation f from )..( S) to some environmental abstract rewriting system with typing A. The proof is shown in the following diagram.

0

We apply the uniqueness of types criteria of cha.pter 4 to prove the following theorem.

Theorem 11.4.17. (Uniqueness of Types) Let S he a. singly sorted specifica.tion. Then )..6 ( S) verifies uniqueness of types.

Proof: By lemma 11.3.17, we know tha.t I I is a. strategy for s~reduction. Moreover by lemma 11.4.12, this mapping preserves the typing. Since S is singly sorted, we have that )..(8) verifies uniqueness of types. It follows from lèmma 4.3.13 that if )..(S) verifies uniqueness of types so does )..8(8). 0

11.4.2 Strengthening

In this section we wil! prove strengthening. This is a property we expect to hold: if in a derivation a. variabie x occurs only in the declaration x:A in the context then we should he able to construct a derivation omitting this variabie from the context. When the variabie has been declared as a definition x= a: A in the context, this property is an immediate consequence of the substitution lemma. We need to prove it for the case of variables which are not definitions. c

In order to prove strengthening, we use the conesponding result for pure type systems (see [BJ93]).

Lemma 11.4.18. Let f' he a context. If f' _.fiê rand r j-.8 a: A then f' j-.8 a: A.

Proof: By induction on the number of steps in the derivation of r j-.8 a : A. Only some cases are considered.

(" t' .. ) r j-.8 A : St r' x : A j-.8 B : 82 r ( ) R • lOrma IOn r f1î (ITx:A.B) : 8 . lOf St, s2, s E .

By induction we have that r' j-.8 A : St. Therefore r'' x:A is a context. By induction we have that f', x: A j-.8 B: s2 . Hence

r' j-.8 A : s1 r', x : A j-.8 B : s2

f' P (llx:A.B) : s

11.4. PROPERTIES OF WELL-TYPED TERMS 159

(" " . ) f, x=a:A f-.5 B : s • 0 - lOrmatJon f p ( A . B) . x=a: ln : s

There exists a deriva.tion of f f-.5 a : A that has less number of steps than this derivation of f, x= a: A f-.5 B : s. By in duetion we have that f' f-.5 a : A. Hence f', x=a:A is a context. By induction we have that f', x=a:A f-.5 B : s.

( . ) f f-.5 b : B f f-.5 B' : so f 1- B -P• B' • converston f p b: B'

By induction we have that f' f-.5 b: Band that f' f-.5 B': s0 . By lemma 11.3.22 we have that f' 1- B <.f-*ps B'.

The rest of the cases are easy to prove. 0

Lemma 11.4.19. If f 1 ,z:D, f 2 f-.5 e: E and z f. FV(f2) U FV(e) then there exists E' such that f 1 , f 2 1- E -ps E' and ft. f 2 f-.5 e: E'.

Proof: By induction on the number of steps in the derivation of ft,z:D,f2 f-.5 e: E. Only some cases are considered.

• (abstraction) Suppose that the last rule in the derivation is the a.bstraction:

f1,z:D,f2,x: A f-.5 b: B f1.z:D,f2 f-.5 (ITx:A. B): s ft,z:D,f:~ P (.\x:A. b): (ITx:A. B)

By induction, there exists B' such tha.t ft. f 2 1- B -ps B' a.nd tha.t:

f h f 2, x : A r-s b : B'

By generation lemma there exist Stand s2 such that (s1,s2,s) ER.

rt.z:D,f2 f-.5 A: st

rl>z:D,f:~,x:A f-.5 B: S:~

{i)

(ii)

(iii)

There exists a derivation of r 1> z:D, f 2 f-5 A : St that has less number of steps than the deriva.tion of fl>z:D,f2 f-.5 (ITx:A. B): s. It follows by induction that

(iv)

We can not apply the induction hypothesis to (iii) because the variabie z may occur in B. Since the mapping I -1- preserves the typing, in particular for (iii) we have that:

!ft. z:D, r2, x:AII- IBir,, .. n,r,,r.A : Sz

160 GRAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

It follows from subject reduction theorem for pure type systems that

The variabie z does not occur in JB'Jr,,r,,.,..A· By strengtherring for pure type systems,

(v)

By lemma 11.4.18,

(vi)

This is a derivation of f 1,f2 ~ (.\x:A. b): (ITx:A. IB'Jr,,r,):

fl, f2 1- B' *"*f3S IB'Irt,f'2 (i) (vi} (iv) (vi) f1,f2,x: A Pb: JB'Jr,,r2 ft.f2 P (ITx:A. JB'Irt.r2 ): s

(". d . ) ft,z:D,f2,x=a:A~b:B ft,z:D,f2 ~(x=a:AinB):s

• u -mtro uct10n ft,z:D,f2 f-11 (x=a:A in b): (x=a:A in B)

By induction, there exists B' such that ft,f2 ,x=a:A 1- B -ps B' a.nd tha.t:

(i)

We ca.n not a.pply the induction hypothesis to f 1 , z: D, f 2 ~ (x a: A in B) : s because the variabie z ma.y occur in B. We a.pply correctnessof types to (i) and we have that either ft. f 2 ,x=a:A ~ B': s' orB'= s'.

1. Suppose f 1> f 2 , x=a:A ~ B' : s'. This is a derivation of f 11 f 2 ~ ( x=a:A in b) : (x:;;;:a:A in B') with ft, f 2,x=a:A 1- B -+'>f3s B'.

r f A 'F.b B' f1,f2,x=a:A~ B':s' i, 2,x=a: r : f f L1î ( A. B') I 1> 2 r x=a: ~n : s

ft, f 2 P (x=a:A in b): (x=a:A in B')

Note that this case is not difficult like the case of the abstraction rule. The proof of the case for the abstraction rule is complicated because the 11-formation rule has more restrictions than the S-formation rule.

2. Suppose B' = s'. By generation lemma fl>z:D,f2,x=a:A ~ B: s. By subject reduction theorem we have that fbz:D,f 2 ,x=a:A ~ B': s. Then (s',s) is an axiom. Since ft. f 2 is a context we have that

(ii)

11.4. PROPERTIES OF WELL-TYPED TERMS 161

This is a derivation of ft. f 2 f-5 ( x=a:A in b) : ( x=a:A in s').

r r A '•b, ft,f2,x=a:Af-5s':s t. 2,x=a: r : s r r a ( A. ')

1, 2• x=a: ~ns :s f 1 , f 2 P (x=a:A in b) : (x=a:A ins')

The rest of the cases are easy to prove. 0

Strengthening for arbitrary specifications follows immediately from the previous lemma.

Theorem 11.4.20. (Strengthening for arbitrary specifications) If ft,x:A,f2 f-S b: Band x~ FV(f2) U FV(b) U FV(B) then rhr2 f-5 b: B.

11.4.3 Weak and Strong Normalisation for ,86-reduction

In this section we prove that if >.(S) is weakly normalising so is >.6(S). Also we prove that a pure type system with definitions >.8(S) is P6-strongly normalising if a slightly larger pure type system >.( 8') is P-strongly normalising. The idea of the proof of st rong normalisation is as foHows.

• Wedefine a mapping { -} _ : 16 x Cs -+ T similar to the projection I -1-· The value {a }r is a term that is obtained from a by unfolding all the global and local definitions. However {-L differs from [-I- in the value given to (x=a:A in b). lnstead of removing the local definition it is translated to a P-redex, i.e. an application and an abstraction.

• This function { -} _ rriaps an infinite Pli reduction sequence to an infinite P reduction sequence.

• The function {-} _ maps terms that are typable in a DPTS >.6(8) to. terms that are typable in a PTS slightly larger than >.(8).

Theorem 11.4.21. (Weak Normalisation for PS) Let 8 be a specification .

.\(8) is P-weakly norma.lising if a.nd only if >.6{8) is Pé-weakly normalising.

Proof: By theorem 11.3.23, the mapping 11 computes the 6-normal form. By 11.4.15 this mapping is an implementing morphism from >.6(8) to >.(8). It follows from lemma. 4.3.9 that if >.(8) is P-weakly normalising then >.6(8) is Pé-weakly normalising. 0

The function I _ 1- is not a refining morphism. lt may not map infinite /'1-reduction sequences to infinite P-reduction sequences as the following example shows.

162 GRAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

Example 11.4.22. Suppose there is an infinite ,8-reduction sequence starting at a and hence at (x=a:A in b). If x(/. FV(b) and bis a ,88 normal form then there is no ,8-reduction sequence starting at I x=a:A in b 1,= b.

The mapping I _ 1- era.ses the term a which could contain an infinite reduction sequence. We definè a new function {-L that is a refining morphism.

Definition 11.4.28. The mapping {-L: Cs x Ts -t T is defined a.s follows.

{x }r = { !a}r1 if r = rhx=a:A, f2 otherwise

{s}I' = 8

{a b }r = {a}r{b}I' P,x:A. b}r = Àx:{A}r. {b}l',.,:A {Ilx:A. b }r Ilx:{A}r. {b}r,.,:A

{ x=a:A in b }r = (Àx:{A}r. {b}r,.,=a:A){a}r

Like 1-l_, the, value {a}r is the unfolding of all the definitions occurring in the context r and in the term a. Global definitions are unfolded in the first line {x }r = {a }r1 • Local definitions are unfolded inthelast line since {b}r,.,=a:A = {b}r[x := {a}r]. However {-L differs from I ..,.. 1- in the value given to (x= a: A in b). Instead of removing the local definition, it is translated to a ,8-redex. The bound variabie x of the local definition is tra.nsformed into the bound variabie of an a.bstra.ction. The abbreviation a is transformed into the argument of a.n application.

Example 11.4.24. Reeall that in example 11.2.20 we show that (Àx:A. b)a may not be typable when (x=a:A in b) is. Let e = Àa:*. (x=a:* in >.y:x. Àj:a -t a. fy) be the term used in example 11.2.20. The corresponding term expressed as an application and an abstraction is not typable in any system of the À-cube. But

{e}r = Àa:*. (Àx:*. Ày:a. Àj:a -t a. fy)a

is typable in >.2. This is because the definition of x is unfolded by a and then x does not occur in the expression Ày:a. >.j:a -t a. fy.

The mapping {-}_is extended to contexts.

Definition 11.4.25. The mapping {-}:Cs-tC is defined as follows.

{e} = e

{r,x: A}

{f,x::::::a:A} = {f},x: {A}r

{r},x: {A}r

11.4. PROPERTIES OF WELL-TYPED TERMS

Similar properties proved for the projection I -1- hold for the function {-L Lemma 11.4.26.

1. If x is f-fresh and x f/. FV(b) then x f/. FV({b}r).

2. Let< f1of2,f3 >E Cs and b ETs be such tha.t (FVr3 (b)) n Def(f2) = 0. Then

{b}r1 ,r2r3 {b}r,,ra·

3. Let< f 1 ,y=a:A,r2 >E Cs. Then {a}r,,y=a:A,r2 = {a}r,.

The following lemma is proved by induction on the structure of the term b.

Lemma 11.4.27. {b}r[x := {a}r] = {b[x a]}r = {b}r,=a:A

163

Lemma 11.4.28. The mapping {-} is an implementing morphism from (Ts,Cs, ---+5 ) to (T, ---+p). More precisely, if r f- c ---+s d then {c}r ---+'; {d}r for all c, dE Ts and rE Cs.

Proof: This is proved by induction on the structure of c. Only some cases are considered.

• Suppose c =x. This means tha.t r = f1o x = d: D, f 2 and r 1- x ---+s d.

{x}r,,.,=d:D,r2 = {d}r, = {d}r1,.,=d:D,r2 by lemma.11.4.26 part 3

• Suppose c (x=a:A in b) and r 1- (x=a:A in b) ---+s b with x f/. FV(b).

{x=a:A in b} = (>.x:{A}r. {b}r,.,=a:A){a}r ---+r; { b }r ,$=a:A [x := {a }r] = {b}r,$=a:A by lemma.l1.4.26 = {b}r

The rest of the cases are easy to prove. 0

By the following lemma, {-L maps an infinite ,86-reduction sequence to an infinite ,8-reduction sequence.

Lemma 11.4.29. The mapping { _} is a refining morphism from (Ts, ---+p) to (T,---+ r; ), i.e. if c ---+p d then {c}r ---+t {d}r.

Proof:This is proved by induction on the structure of c. Only the case c = (>.x:A. b)a with (>.x:A. b)a ---+p b[x :=a] is considered.

0

{c}r = {Àx:A. b}r{a}r = (>.x:{A}r. {b}r){a}r

---+p { b }r[x := {a }r] {b[x := a]}r by lemma 11.4.27

164 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

Definition 11.4.30. The specification S = (S,A,R) is called quasi-full if for all s 11

Sz E S there exists SJ E S such that ( sh s2, SJ) E R.

Note that if a specification is full then it is quasi-full. But the converse is not true.

Definition 11.4.31. LetS= (S,A,R) and S' = (S',A',R') he such that

L S Ç S', A Ç A', and R Ç R'

2. S' is quasi-full

3. for all s E S there is a sort s' ES' such that (s : s') E A' (i.e. the sorts of S are not topsorts in S').

Then the specification S' is called a completion of S.

Example 11.4.32. The specification Coo is a completion of C, HOL and itself.

This definition is necessary in order to prove that { _} _ maps terms that are typable in .a pure type system with definitions ,V(S) to terms that are typable in a sligh-tly larger pure type system >.S' with S' a completion of S. Remember that {-L translates alocal definition toa À-ahstraction with an argument:

{x=a:A in b}r = (Àx:{A}r. {b}r,z=a:A){a}r

Condition 2 is necessary to ensure that all these À-abstractions introduced by { -} _ are allowed in S'. The typing of the abstraction is restricted by the set R of rules whereas the typing of (x=a:A in b) is not. Let e = (x=a:* in Ày:x. >.j:x ...-+x. fy) he the term in the example 11.2.21. This term is typable in the system >.~ but the term {e}, =(>.x:*. Ày:a. >.j:a ....-+a. fy)a is not typable in À_.

Condition 3 is necessary because we can nottype these abstra.ctions introduced by {.}_ if A is a topsort. For example the term (x=*: o in x) is typable in >."0 but

{x=*: 0 in x}e = (>.x:O. *)*

is not typable in >.C.

The next lemma is proved by induction on the derivation.

Lemma 11.4.33. LetS= (S, A, R). If s occurs either in A or in a or in rand r f-65 a: A then sE S.

The next lemma states that a >.S' type that is in the range of { _} _ cannot he one of the topsarts of >.S'.

Lemma 11.4.34. LetS (S,A,R) and S' = (S',A',R') be such that S' is a completion of S. Then If .ó. f-68 a: A and .ó. f-68 , {a}r: {A}r then .ó. f-68 , {A}r: s.

11.4. PROPERTIES OF WELL-TYPED TERMS 165

Pro of: Assume .6. f-58 , {a }r : {A }r. By correctness of types .6. f-58 , {A }r : s1 or {A }r = s. Suppose {A}r = s. Since .6. f-58 a: A and by lemma 11.4.33 we have that s E S. By the condition (3) of the definition of completion, there is a sort s' ES' such that (s,s') E A' and hence .6. j-Ö8 , s: s', i.e . .6. j-Ö8 , {A}r: s'. D

The next theorem states that { _} _ maps terms that are typable in .V ( S) to terms that are typable in the pure type system >.S' with S' a completion of S.

Theorem 11.4.35. Let S = (S,A,R) and S' = (S',A',R') be such that S' is a completion of S. Then r f-58 a: A :::} {f} j-Ö8 , {a}r: {A}r.

Proof: By induction on the derivation of r f-58 a : A. Only some cases are considered.

ff-~a:A • ( 6-start) r _ . A ïE • A ,x-a. r 8 x.

By in duetion {f} f-58 , {a }r : {A }r. By definition we have that {x }r,.,=a:A = {a }r. By lemma 11.4.26 part 2 we have that {A}r,.,=a:A = {A}r.

It follows from lemma 11.4.34 that {f} j-Ö8 , {A }r : s. By weakening rule, we have that {f},x: {A}r f-Ó8 , {x}r,z=a:A: {A}r.

(c k . ) ff-Ó8 b:B rr-s8 a:A

• u-wea enmg r _ . A îE b. B ,x-a. •s .

Byinduction, {r} j-Ö8 , {b}r: {B}rand {f} f-58 , {a}r: {A}r. Then bylemma11.4.34, we have that {f} j-Ö8 , {A}r: s. By weakening rule we have that {f},x: {A}r f-58 ,

{b}r: {B}r.

We have tha.t x f/. FV(b) and x f/. FV(B). By lemma 11.4.26, we have that {b}r,x=a:A = {b}r and that {B}r,x=a:A = {B}r.

( c .r • ) r, x=a:A f-58 B : s • u-1ormat10n -::::-,...,.;..-,..---:..::;--=-r f-58 (x=a:A in B) : s

By induction

{ r, x=a:A} f-58 , { B} r,=a:A : s (i)

The derivation of r, x=a:A f-58 B: s contains a (shorter) derivation of r j-Ss a: A, so also by induction

{r} j--68 , {a}r: {A}r

By lemma 11.4.34 it follows from (i) and (ii) that there are 8It8z ES' such that

{f, x=a:A} j-Ö8 , 8 : sz

{r} f-Ós, {A }r : s1

(ii)

(iii)

(iv)

166 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

The following is a derivation of {r} rD8 , {x=a:A in B}r: s.

and (.Xx:{A}r. {B}r,a:=a:A){a}r = {x=a:A in B}r.

(c. d . ) r,x=a:A f-~ b: B r rD8 (x=a:A in B): s

• v-mtro u ct IOn ___:.--;::,..,..."...--,,-'!------,,....,----;.,.-~'-:----:--:--~~-r 1-"'8 (x=a:A in b): (x'=a:A in B)

By induction

{f,x=a:A} rD8 , {b}r,x=a.:A: {B}r,x=a.:A {r} rD8 , { x=a:A in B}r : s

(i) (ii)

The derivation of r,x=a:A rD8 b: B contains a (shorter) derivation of r f-58 a: A, so also by the induction

{r} rDs, {a}r: {A}r (iii)

By lemma 11.4.34 it follows from (i) and (iii) that there are .St, s2 E S' such that

Th en

{r, x=a:A} rD8 , { B }r,x=a.:A : s2

{r} f-68 , {A}r : s1

(~) (~ ~ ~ {r} P8, (Tix:{A}r. {B}r,:c=a:A): sa pro (i) ( b)

{r} Ps, (.Xx:{A}r. {b}r,x-e:A): (Tix:{A}r. {B}r,a:=a:A) as (iii) ( ) {f} P8, (>.x:{A}r. {B}r,x=a:A){a}r: {B}r,a:=a:A(x := {a}r] app

{x=a:A in b}r = (.Xx:{A}r. {b}r,x=a:A){a}r and

{x=a:A in B}r = (.Xx:{A}r. {B}r,x=a:A){a}r -p {B}r,x=a:A[x := {a}r]

so using the conversion rule

{f} rD8 , {x=a:A in b}r: {B}r,x=a:A[x := {a}r] (ii) (vi) (fJ _ conv) {r} P8 , {x=a:A in b}r: {x=a:A in B}r

(c . ) r rD8 b: B f rD8 B': s r f- B -ó B'

• u-conversiOn r Ps b : B

(iv)

(v)

(vi)

It follows from in duetion that {r} rD8 , { b}r : { B}r and {f} rD8 , { B'}r : s. By lemma 11.4.28 it follows from r f- B *-*s B' that {B}r *"*P {B'}r. Then using the fJ-conversion rule {f} rD8 , {b}r: {B'}r.

11.5. GONGLUS/ONS AND RELATED WORK 167

The rest of the cases are easy to prove. 0

Theorem 11.4.36. (Implementing morphism from V(S) to >.(S')) Let S' be a completion of S. The pair ( { -}, { -} _) is an implementing morphism from

À5(S) to >.(S').

Proof: By theorem 11.4.35, the pair({_},{-}_) preserves the typing from >.6(S) to À(S'). It follows from lemmas 11.4.28 and 11.4.29 that this pair is an imptementing morphism from >.6(S') to >.(S). O

We write { } instead of ( { -}, { _ }_).

Theorem 11.4.37. (Strong Normalisation for (38) Let S = ( S, A, R) and S' = ( S', A', R') be such that S' is a completion of S.

If >.(S') is /3-strongly normalising, then >.6(S) is /38-strongly normalising.

Proof: By theorem 11.3.29, the ó-reduction is strongly normalising. lt follows from lemma 11.4.29 and theorem 11.4.35 that { } is a refining morphism from >.6(S') with only /3-reduction to >.(S). Moreover it follows from lemma 11.4.28 and theorem 11.4.35 that { } is an implementing morphism from ).S(S') with only b'-reduction to >.(S). By lemma 4.3.11, if >.(S') is /3-strongly normalising, then >.6(S) is f3b'-strongly normalising. 0

Corollary 11.4.38. The following systems are strongly normalising:

1. The system >.(Cco) extended with definitions, i.e. >.6(Goo)·

2. The calculus of constructions extended with definitions, i.e. >.6C.

3. The system of higher order logic extended with definitions, i.e. ).6(HOL) is strongly normalising.

Proof: The system >.(Coo) is strongly normalising. This specificaiion Goois a completion of itself. Hence it follows from the previous theorem that >.6(Coo) is strongly normalising. Since ).6(Coo) contains ).5C and >.6(HOL), the parts 2 and 3 follow from part 1. 0

Theorem 11.4.37 is somewhat unsatisfactory. It would be nicer to prove a stronger property, namely that V(S) is (38-strongly normalising if >.(S) is /3-strongly normalising. On the other hand, we do not know any strongly normalising pure type system ).S for which theorem 11.4.37 cannot be used to provestrong normalisation of ).6(S). In particular, all strongly normalising pure type systems given in (Bar92] have a completion that is >.(Cc,,).

11.5 Conclusions and Related Work

In this chapter we have considered definitions as part of the formal language. In our opinion, this extension has been done in a neat and general way (for pure type systems). The indusion of definitions in the formallanguage and its study have not been considered before except for the systems of the AUTOMATH family [NGdV94].

168 GRAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

Definitions vs local ,8-reduction. In the systems of the AUTOMATH family, de:finitions are written as ,8-redexes [Ned73] [Daa80]. A reduction called local ,8-reduction to unfold one occurrence of a variabie at a time is introduced in [Ned92]. In some later versionsof AUTOMATH, de:finitions are connected with local ,8-reduction. We prefer, as in the original AUTOMATH systems, to have a special constructor for definition and a reduction for the unfolding of de:finitions to make a clear distinction between definitions and ,8-redexes.

Global and Local Definitions. Our extension provides global and local definitions, i.e. definitions in the context and in the term. Coq [Dow91] provides only global definitions. In our opinion, it is important to have local de:finitions as well as global ones for practical use. The study of the meta-theory is rather simple if we have only global definitions and in this case, it is easy to prove that strong normalisation is preserved by the extension.

Definitions vs Abstraction and Application: the third proof of finiteness of developments. As we said before, a definition and a ,8-redex are very similar. Intuitively, we know that a ,8-redex (.\x: A. b )a is 'like' a definition x=a:A in b. However the 6-reduction is finer than the ,8-reduction since one step of ,8-reduction corresponds to several steps of 6-reduction.

The relation between definitions and .B·redexes is formalised by the morphism T. This morphism maps a marked redex (~x:A. b)a into a de:finition x=a:A in b. Moreover this function maps one step of ,8-reduction into one or more steps of 8-reduction, i.e. it is a refining morphism. -

A development is then mapped into a. 8-rewrite sequence via the morphism T. Since ti is strongly normalising, we conclude that all the developments are finite. This is our third proof of finiteness of developments ( see chapter 7) and it is similar to the proof of the same result in [Kló80J using .8-reduction with memory.

The length of a development is always smaller than the length of the corresponding ti-rewrite sequence. This can also be formalised. The function w_ (-) used to provestrong normalisation of 6 computes the length of a maximal8-rewrite sequence to the normal form. In [Vri85], a mapping h is defined that computes the lengthof a maximal development, i.e. the length of a maximal ,8-rewrite sequence. The mapping w _ (-) composed with T is an upper bound for the number of steps in a development. It is easy to verify that w0(T( a)) is greater than h(a). lt is clear that the difference between we(T(a)) and h(a) is the number of extra-steps needed in the 8-rewrite sequence.

Definitions vs Explicit Substitution. There is a common reason to introduce definitions x=a:A in band explicit sustitution is b{x :==a}: to delay the global substitution of x for a in b and perform the substitution for one occurrence of the variabie x at a time because the substitution for all the occurrences at once may not be desirable.

The substitution is said to be explicit if it is not a meta-operation on terms but part of the formallanguage with a special constructor b{ x : = a} and a special rewrite relation .Bx

11.5. CONCLUSIONS AND RELATED WORK 169

to define 'the substitution behaviour' [ACCL91] [KN93] [BG96J [Tas93]. The ,8.,-reduction and the 8-reduction are very similar, they both unfold one occurrence

of a variabie at a time. They differ in the way they perfarm this unfolding. In a ,Bw-rewrite step, the explicit substitution '{x:= a}' is pushed inside the structure

of the term b until a variabie is found. For example in the case of the application

(b c){x :=a} __.{3., (b{x a}c{x:=a})

In a 8-rewrite step, the definition x=a:A is always kept outside the term b (in a context). In the moment of unfolding the variable, the explicit susbtitution ' {x := a}' is inside

the term and the unfolding is performed as follows.

x{ x:= a} _.p., a

In the moment of unfolding the variable, the definition x=a:A is taken from the context r, x=a:A, f' and the unfolding is performed as follows.

r, x=a:A, r' !-x _.sa

The length of a 8-rewrite sequence depends on the number of occurrences of the variabie x in the term b. The ,Bw-reduction needs more steps than the 8-reduction to perfarm the unfolding of one occurrence of a variable. The extra-steps that ,8., needs depend on the structure of the term b. We do not see any reason to have these extra-steps if we just want to perfarm the unfolding of one occurrence at a time. Definitions achieve this purpose very well besides having all the good properties. On the other hand, it is not clear how to define a type system for explicit substitutions [Blo97] that verifies all the good properties like subject reduction.

The main difference, however, between definitions and èxplicit substitution is that the explicit substitution is defined tagether with a rule that creates a fJ.,-redex from a ,8-redex.

(..\x:A. b)a -"p~ b{x a}.

The crucial problem for explicit substitution is called preservation of strong normalisation and is the following.

If a term b is fJ-strongly normalising, is the term b also fJ.,,B~-strongly normalising?.

So fJ.,-reduction is in fact a refinement of fJ-reduction whereas S-reduction is not. One still has to consider the combination of the 8 with the fJ-reduction. The problem analogous to this for definitions is the following: if b E To is fJ-strongly normalising then b is ,88-strongly normalising. This is not true since there may be pseudoterros that are ,8-strongly normalising but not fJ8-strongly normalising. For example,

w (..\x:A. x x):B in (ww) -6 (..\x:A. x x)(..\x:A. x x)

The pseudoterm w (..\x:A. x x):B in (ww) is in ,8-normal form but it is not fJó-strongly normalising. Therefore in the case of definitions, we need to restriet the set of terms by using types. The problem for definitions is then the following.

If a pure type system ..\(S) is ,8-strongly normalising, is its extension with definitions ..\6(8) ,88-strongly normalising?.

170 CHAPTER 11. PURE TYPE SYSTEMS WITH DEFINITIONS

Normalisation for {36. In order to prove that weak and strong normalisation are preserved by the extension we have written two morphisms that are illustrated in the following diagram.

).8(8)

IV~ À(S) À(S')

The first one is I I and computes the Ö-normal form of a term. This is an implementing morphism from a pure type system with definitions to the corresponding pure type system without definitions. By the results of chapter 4, this morphism allows us to prove that weak normalisation is preserved by the extension.

The second one is { } and transforms a Ö-redex into a {3-redex. This is a refining morphism from À8(S) to À(S') where S' is a completion of S. By the results of chapter 4, this morphism allows us to prove that for a class of pure type systems, strong normalisation is preserved by the extension.

lt is still an open problem whether extending an arbitrary pure type system with definitions preserves strong normalisation or not.

Definitions with parameters. In [LSZ96], we consider definitions with parameters like in the systems of the AUTOMATH family [NGdV94]. We write x(y1 :Bt, ... , y11:B11):::: a: A to denote that x is an abbreviation for a and the variables Yt, ... Yn may occur free in a. Then x(bt, ... bn) reduces to a[y1 :==biJ ... Yn := b",]. We are presently investigating the pure type systems extended with this kind of definitions.

Chapter 12

Type Inference for Definitions

12.1 Introd netion

In this chapter we write a partial function that infers the type of a term in a singly sorted pure type system with definitions.

As we said in chapter 10, the type can be easily inferred if the last rule to he applied is determined by the shape of the term and the context. In other words, we have to define a set of typing rules that are syntax directed in order to write a function that infers the type. We present a syntax directed set of rules for singly sorted pure type systems with definitions simHar to the one presented in chapter 10.

This chapter is organised as follows. In section 12.2 we modify the 8-start and the 8-weakening rules in the definition of pure type systems with definitions. Insection 12.3, we define a syntax directed set of rules for singly sorted pure type systems with definitions. In section 12.4, we define a function that infers the type for a term in a singly sorted pure type system with definitions.

12.2 The 8-start and 8-weakening rules

In this section, we change the rules of pure type systems with definitions. These new rules define the same typing relation as before.

We split the the 6-start rule into two rules, 6-start1 and 8-start2 • The 8-start rule is equivalent to these two rules. Similarly, we split the 6-weakening rule into two rules, the 8-weakening1 and 8-weakening2 rules.

We give an intuitive explanation of why the split rules are equivalent to the original one. The 6-start rule has the premise f I-" a : A. We know that f I-" A : s or A = s by correctness of types. We could add the superfluous condition 'f 1-5 A : s or A = s' to the 8-start rule. This condition is an 'or' and herree we can split the rule in two as follows.

171

172

(6-startz)

GRAPTER 12. TYPE INFERENCE FOR DEFINITIONS

r 1-6' a : A r 1-6' A : s r, x=a:A !-6' x : A

r 1-5' a : s s is a topsort r, x=a:s !-5' x : s

x is r-fresh

x is r-fresh

Next we define the notion of a pure type systems with definitions whose 6-start and 6-weakening rules are split.

Definition 12.2.1. The functor >!' : Spec--+ Carstvw is defined as ,\"'(S) = (1/;,Cs,--+,(3s,l-6

') forSE Spec. The sets 1/; and C5 and the relation --+136 are as in definition 11.2.18. The typing relation 1-5' is defined as the smallest relation dosed under the same rules as in definition 11.2.18 éxcept for the 6-start and 6-weakening rules.

(8-8tartz)

( 6-weakening2 )

r 1-6' a : A r 1-6' A : s r, x=a:A !-8' x: A

r 1-6' a : s s is a topsort r, x=a:8 !-6' x: 8

r 1-5' b : B r 1-6' a : A r 1-6' A : 8

r,x=a:A !-6' b: B

r 1-5' b : B r 1-5' a : s s is a topsort r, x=a:s 1-6' b : B

x is r-fresh

x is r-fresh

x is r-fresh

x is r-fresh

In the following theorem we prove that the typing rules of definitions 11.2.18 and 12.2.1 generate the same typing relation.

Theorem 12.2.2. r !-6' a : A if and only if r I-" a : A.

From now on, we consider the rules for pure type systems with definitions as presented in this section.

12.3 Syntax Directed Rules for Definitions

In this section we define a syntax directed set of rules for any singly sorted pure type system with definitions. As for pure type systems, the main features are that the weakening rule is restricted to variables and constants and the conversion rule has heen removed. Moreover in the abstraction rule the ll-condition is checked in a weaker system. The weaker system is the pure type system with definitions and without the II-condition.

Now we remave the II-condition from the rul es for pure type system with definitions. In the introduetion rule, the premise that the type (x=a:A in B) of a definition is well-typed, is replaced by the condition 'Bis nota sort'.

12.3. SYNTAX DIRECTED RULES FOR DEFINITIONS 173

Definition 12.3.1. The functor >.fiw: Spec-t Carstvw is defined as >.5w(S) = (Ts,Cs,-tj3s,l-5w) forSE

Spec. The sets Ts and Có and the relation -tps are as in definition 11.2.18. The typing relation l-6

w is defined as the smallest relation closed under the same rules as in definition 12.2.1 except for the following ones.

( abstraction)

( 6-introduction)

r 1-sw (>.x:A. b): (ITx:A. B)

r, x=a:A I-8W b: B

r 1-5w (x=a:A in b) : (x=a:A in B) B is not a sort

Definition 12.3.2. (Pure type systems with definitions and without the IIcondition) A pure type system with definitions and without the IT-condition is an element of the set

Note that >.6w(S) is an extension of >.8(S) and >.w(S). Diagramatically,

n n

.X8(S) c Àliw(S)

We define the syntax directed set of rules for the pure type systems with definitions and without the Il-condition.

First we define the weak head ,65-reduction.

Definition 12.3.3. The weak head ,66-reduction is defined by the following rules:

r, r 1- (x=a:A in b) -t'tf; b[x :=a]

r 1- (.Xx:A. b)a -tiJ; b[x :=a] F wh F' r 1- -+riO

(Fa) -t'tfff (F' a)

Definition 12.3.4. The functor >.~d' : Spec-t Carst .... w is defined as À~d'(S) = {Ts,Có, ..... (;16,1-~d') forSE

Spec. The sets Ts and Có and the relation -tf3fi are as in definition 11.2.18. The typing relation 1-~d' is defined as the smallest relation closed under the following rules.

(axiom) for (s, s') E A

174 CHAPTER 12. TYPE INFERENCE FOR DEFINITIONS

(start)

( weakening)

(! ormation)

( abstraction)

( application)

(6-start1)

( 6-weakening1 )

( 6-weakening2)

( 6-formation)

( 6-introduction)

where sE S.

f 1-~d' A :-11s 81 f,x: A f-~:t B :-ps s2 r r-!:r (IIx:A.B) : s3

r,x:Af-~:fb:B r r!d' (,\x:A. b) : (IIx:A. B)

r 1-~:f b :-wt (ITx:A. B) r r::t a: A' r r!:t (b a): B[x :=a]

r 1-~d' a: A' r f-~d' A :-ps s r, x=a:A r!:t x : A'

r ~-~:r a :-ps s r, x=a:s 1-!:t x: s

r 1-~:t b : B r r-::r a : A' r 1-~:t A :-ps 8

r, x=a:A 1-::r b: B

r 1-~:t b: B r f-~d' a :-ps s r, x=a:s f-!:t b: B

r, x=a:A f-~d' B: s r f-!d' (x=a:A in B) : s

r, x=a:A f-~d' b: B r f-!d' (x=a:A in b) : (x=a:A in B)

where x is fcfresh

x is f-fresh and b E C U V

x is f-fresh and A *-*Pii A'

sis a topsort and x is f-fresh

x is f-fresh, b E CU V and . A *-*PS A'

8 is a topsort, b E CU V

and x is r-fresh

B is not a sort

Note that these systems verify only very weak subject reduction (see definition 4.3.1).

Lemma 12.3.5. If r f-~d' a : A then lfl f-~d lalr : IAir·

This is proved by induction on the derivation of r 1-~d' a : A.

Theorem 12.3.6. (Completeness) If r f-8w a: A then f 1-!d' a : A' with A *-*ps A'.

This is proved by induction on the derivation of r f-!d' a : A. Next wedefine a syntax directed set of rules for any singly sortf>.-<1 pure type system with

definitions. These rules are exactly the ones in definition 12.3.4 except for the abstraction rule that contains the 11-condition.

Definition 12.3.7. The functor ,\~d: Spec-+ Carstvw is defined as ,\~d(S) = (16,Cs,-+ps,l-sd) forSE

Spec. The sets 7(, and Ca and the relation --.Pli are as in definition 11.2.18. The typing

12.3. SYNTAX DIRECTED RULES FOR DEFINITIONS 175

relation ~d is the smallest relation closed under the same rules as in definition 12.3.4 except that the abstraction rule is replaced by the following ones.

( abstraction)

where 8 ES.

r,x: A ~db: B r f-~~ (llx:A. B) :-135 s f Psd (>..x:A. b) : (llx:A. B)

The relations between the systems with a syntax directed set of rules are shown in the following diagram.

>...d(S) c .X:'d(S)

n n .x:d(s) c >.::r(s)

Note that these systems verify only very weak subject reduction {see definition 4.3.1).

Lemma 12.3.8. If r f-5 A =-136 8 then r f-5 A : 8.

Theorem 12.3.9. (Soundness) LetS be a singly sorted specification. If r ~d a : A then r f-5 a : A.

Proof: This is proved by induction on the derivation of r ~d a : A. Suppose that the last rule in this derivation is the abstraction rule.

( b t. ) r, x : A ~d b : B r f-~~ (Ilx:A. B) :- 8 a strac wn ---""----;;,..,,.T---:-:-.,.--,~~,---:--="<"'"----r f-h.d ().x:A. b) : (Ilx:A. B)

By in duetion hypothesis we have that r, x:A f-5 b : B .. We have that r r-::r (Ilx:A. B) :- 8. By lemma. 12.3.5, lfl f-~d ITix:A. Blr =- 8. By

soundness for À~d(S), we have tha.t lfl f-"' IIIx:A. Blr =- 8. Hence lfl r-w ITix:A. Blr : 8.

By generation lemma., !f,x:AI f-"' IBir: 82, lfl r-w lAl: 81 and (8l,sz,s) ER. By correctness of types we have that either r, x:A f-5 B : 8 1 or B = s' is a topsort. The

second possibility is impossible. Hence r' x:A f-5 B ; 81• We have tha.t 81 = 8z because the

specification is singly sorted. By correctness of contexts r P A: s". Since the specifica.tion is singly sorted we have

that 811

81.

This is a deriva.tion of r P (>.x:A. b) : (llx:A. B).

rx·Af-hb·B rPA:81 r,x:Af-DB:82

' . . r P (llx:A. B) : s r P (>.x:A. b) : (Tix:A. B)

0

176 GRAPTER 12. TYPE INFERENCE FOR DEFINITIONS

Theorem 12.3.10. (Completeness) LetS be a singly sorted specification. If r \-Ó a: A then there exists A' such that r ~d a: A' and A *"*f3 A'.

Pro of: We prove only the case of the abstraction rule.

( abstraction) r, x :A \-Ó b: B r \-Ó (IIx:A. B) : s

r P (Àx:A. b): (IIx:A. B)

By induction hypothesis we have that f; x: A ~d b : B' forsome B' such that B *"*f3ö B'. By theorem 12.3.9 we have that r,x:A \-Ó b: B'.

If r j-5 (IIx:A. B): s then there exists Stand s2 such that (8t.82,8) ER, r j-5 A: St and r, x:A \-Ó B : 82. By correctness and unicity of types we have that r, x:A \-Ó B' : 82. Hence r \-Ó (IIx: A. B') : 8. By theorem 12.3.6 we have that r f-~d' (IIx: A. B') : D and D *"*f36 8. o

12.4 Type Inference for Definitions

In this section we write a semi-algorithm of type inference for all the class of singly sorted pure type systems with definitions.

As for pure type systems, we cannot expect to find a terminating algorithm for the class of singly sorted pure type systems since typability for some non-normalising pure type systems with definitions is not decidabie (see corollary 11.4.14). Hence we define a type inference semi-algorithm for all the class of singly sorted pure type systems with definitions, including the non-normalising ones.

In order to define such a semi-algorithm, we use the syntax directed set of rules presented in section 12.3. Although those rules are syntax directed when the specification is singly sorted, they are not yet deterministic. We have to solve the side conditions as in chapter 10. In this case we have to perfarm (3/i-weak head reduction and check (3/iconverswn.

First we write a function that computes the weak head (3/i-normal form if it exists.

Definition 12.4.1. A function whnff36 : C6 x 7/;-+ 7/; is defined as follows.

whnff35(r, a) = a if a is in weak head normal form in r

whnff3s(f, (x d1 ... dn)) = whnff3s(f, ad1 ... dn) if x=a:A E f

whnff3s(f, (x=a:A in b) d1 ... dn) whnf(3s(f, b[x := a]dt ... dn)

Lemma 12.4.2. Let a ET be weak head normalising. Then whnff3s(a) is the weak head (3/i-normal form of a.

12.4. TYPE INFERENGE FOR DEFINITIONS 177

This lemma is proved by in duetion on the numher of steps of the leftroost rednetion to the normal form.

In order to check conversion in the semi-algorithm we define a common-reduct strategy for ,88-reduction. We define two common-reduct strategies for ,86-reduction.

First we define the strategy F36 similar to F0•

Definition 12.4.3. Wedefine Fp5 : Co x T5 x T5 -t 'P(To) as follows.

Fp6(f,d,d) = {d} if d = d'

F_:Mr,(>.x:A. b),(>.x:A'. b')) {(>.x:A". b") I A" E Fp6(f,A,A') & b" E FMr, b, b')}

Fp8(f, (ITx:A. B), (ITx:A'. B')) = {(ITx:A". B") I A" E Fp6(f, A, A') & B" E Fp6(f,B,B')}

FMf,d,d') = Fp6(whnf.es(d),whnfps(d')) if d or d' are not in weak head normal form

Fp6( d, d') = 0 otherwise

The next lemma says that the function Fp6 is a common-reduct strategy only for normalising pseudoterms.

Lemma 12.4.4. Let a and b be ,86-weakly normalising. Then

1. Fps( a, b) terminates.

2. For all c E FiJs(a,b), we have that a -.66 c and b -ps c.

3. a -13s b if and only if Fp6(a, b) =f. 0.

We define a common-reduct strategy Ftt similar to the strategy F++. This strategy terminates if the terms are convertible, even if they are not normalising.

Definition 12.4.5. Wedefine FU :Cs x T5 x T5 -t 'P(To) as follows.

178 GRAPTER 12. TYPE INFERENCE FOR DEFINITIONS

FU(r,a,b) = If a= b then I {a} el se

end

If a or b are in weak head ,86-normal form in r then I L(r, whnfps(r, a), whnfps(r, b)) el se I HU(r,{a},{b}) end

We define the function L :Cs x Ts x Ts -f 'P(Ts) as follows.

L(r,(>.x:A1. bt),(>.x:A2. b2)) = {(>.x:Aa. b3) I Aa E Ftl(f,At,A2) & ba E Ftt(f,ht,b2)}

L(f,(llx:A1. bt),(llx:A2. ~)) = {(llx:A3. ba) I A3 E Ftt(f,At.A2) & ba E Ftt(r, bt, b2)}

L(r,a,b) = 0 otherwise

The function Htt :Cs x 'P(Ts) x 'P(Ts) -f 'P(Ts) is a.lways applied toa context r and subsets X a.nd Y of Ts that verify the following preconditions.

1. X and Y are the n and m-bounded reduction graphs of a and b in r.

x= 9.:S.';.(r,a) ={a 1 r 1- a -,(is a in Iess tha.n n steps}

Y g.;.;.(r, b) = {a 1 r r- b -,(is a in less than m steps}

2. The intersection of X and Y is empty.

3. 0 5 n- m :$1.

4. x a.nd y do not contain a.ny weak head ,86-normal form in r.

If the bounded graphs X contains a weak head normal form, we choose a b of Y and reduce it to weak hea.d normal form.

12.4. TYPE INFERENCE FOR DEFINITIONS 179

Htf(r,X,Y) ::;: If Ç .... 116(f,X) n Y # 0 then I !I-11Ar,X)nY el se

If 3a, a E !1-116 (f,X) in weak head /18-normal form then I Choose bEY, L(f, a, whnfp6(f, b)) el se 1 Httcr, v, x u !I-11Ar, xn end

end

In the following lemma we prove that the function Ftt is a common-reduct strategy.

Lemma 12.4.6.

1. If r 1- a *""*.86 b then Ftf(f,a, b) terminatea and yields a non-empty set that verifies that for all c E Ftf(r, a, b) we have that a -.as c and b -/36 c.

2. IfFti(f,a,b) # 0 then f 1- a *-*ps b.

Semi-algorithm of type inference. We define a function type6 that computes the type of a term (up to /16-conversion) in a singly sorted pure type system with definitions. If a is typable in r in a singly sorted pure type system with definitions then typ eh' ( r, a) terminates and yields the type of a in r (up to Po-conversion), i.e. if r f-6 a : A then typé (f, a) *""* ps A. If the term a is not typable in r then types (f, a) either yields l. or it does not terminate.

This function is obtained from the syntax directed set of rules defined insection 12.3 for pure type systems with definitions. For. each rule, we write a case of 'pattern matching'.

The conditions that appear in these rules that are of the form 'f f-6 A =-.es s' are replaced by 'whnfp8(type6(f, A)) ::;: s'.

The condition 'f f-6 b :-p6 (llx:A. B)' is replaced by

'whnfp6(type8(f, b))::;: (llx:A. B}'.

The other condition 'A *-*ps A" is replaced by Ftf(A, A') # 0. The condition 'f 1-6

"' (llx:A. B) =-.as s' is replaced by

'whnfps(typé'w(r, (llx:A. B))) ::;: s'.

We need to define an auxiliary function typé" to compute the type in a pure type system without the 11-condition.

The function typé" : Cs x 76 ~ 761. is defined as type5 except that we remove the condition whnfps(f, type6w(r, llx:A. B)) =sE S that corresponds to the IT-condition.

180 GHAPTER12. TYPE INFERENGE FOR DEFINITIONS

Definition 12.4. 7. The function type~ : Cs x Ts -t Ts .L . ( or just typé) is defined as follows.

typé(e::,s) = s' if (s,s') E A

typé(< r,x: A >,x) = A if whnfps(r, type6(f,A)) =sE S a.nd x is r-fresh

type6(< r,x: A >,b) = typé(f,b) if b E C U V, x is r-fresh, b ::/; x and whnff.l6(f,type6(f,A)) =sE S

type8(r, (IIx:A.B)) = 83 ifwhnf,aó{r,typé(r,A)) = s1, whnfps(f, type6( < r,x: A>, B)) s2, a.nd (shs2,s3) ER

typé(r, (.h:A. b)) = (ITx:A. B) if type6( < r,x: A>, b) = B a.nd whnfps(r, type6"'(r, ITx:A. B)) =sE S

typé(r, (b a)) = B[x:=a] if whnf,as(r, type5(r, b)) = (ITx:A. B), type"(r,a) =A' and Ftt(r,A,A') ::j; 0

types(< r,x=a:A >,x) = A' if x is f-fresh and whnf,as(f,type"(r,A)) = s, type6(f,a) =A' and Ftt(r,A,A1

) :F 0

type6(< f,x=a:s >,x) = s if 8 is a topsort, x is r -fresh and whnfp6(f, type6(f,a)) = s

type"(< f,x=a:A >,b) = B if b E CU V, x is f-fresh, b ::j; x type6(f,b) = B, type"(r,a) = A1

,

whnf,as(r, type6(r, A))= s a.nd Ftt(r,A,A') # 0

type6(< r,x=a:s >,b) = B if b E CU V, s is a topsort, x is f-fresh, type6(r, b) =Band whnf ,as(r, type6 (r, a)) = s

type"(r, (x=a:A in B)) s if type6( < r, x=a:A >, B) = s

type6(r, (x=a:A in b)) (x=a:A in B) iftype"(< r,x=a:A >,b) =Band B is not a sort

type" (r, a) = ..L otherwise

12.5. GONGLUS/ONS AND RELATED WORK 181

Theorem 12.4.8. ( Correctness of 'typè') Let S = ( S, A, R) be singly sorted such that the sets S, A and R a.re recursively enumerable.

1. If r f-Ó a: A then typé(r,a) terminates and type8(r,a) *-*PS A.

2. If type6(r, a) terminatea a.nd yields A then r f-5 a: A.

Like for pure type systems without definitions, type checking for pure type systems with definitions ca.n be solved from type inference (see definition 10.8.15). A function check6

is defined that checks if a term bas a given type in a singly sorted pure type system with definitions.

Definition 12.4.9. We define the function checks : Cs x T{; x T{; -+ Bool as follows.

{

true

check6(r,a,A) = false

ifwhnfp.s(typé{r,A)) =sE s, type8(r, a) :f: J.. and Ftt(type6(r, a), A) :1= 0 otherwise

Theorem 12.4.10. (Decidability of Type Inference and Type Checking) Let S = (S, A, R) be singly sorted such tha.t the sets S, A and Rare recursive. If >.6(S) is ,88-weakly norma.lising then type inference a.nd typechecking in >.ó(S) are

decidable.

Proof: Since the sets of the specification are recursive, we have that typé" a.lways terminatea a.nd so do type6 a.nd chèck8

• Therefore type inference a.nd type checking for >.6(S) are decidable. 0

Theorem 12.4.11. (Decidability of Inhabitation) Let S = (S,A,R) be singly sorted such that the sets S, A and Rare recursive.

Suppose that .:\(S) is ,8-weakly normalising. If the problem of inhabitation is decidabie in À(S) then it is decidabie in ,\6(S).

Proof: 1f >.(S) is ,8-norma.lising then >.6(S) is ,88-normalising. Let rE Cs and A E T{;. By theorem 12.4.10, we have that it is decidabie whether r f-5 A: s or not. In case r f-Ó A : s, we find a such that lfll- a : IAir· By applying the conversion rule and lemma 11.4.18, we obtain r f-Ó a: A. If IAir has no inhabitant then A ca.nnot have any inhabitant. 0

As a consequence of this, we have that the problems of inhabitation in À~ and in À0f!L

are decidable.

182 GRAPTER 12. TYPE INFERE:NCE FOR D~FINITIONS

II-condition Original Type system with syntax Type type system directed rules semi-al •. I. I

included .\b(S) .\~d(8) typ eb removed _\0W(8) .\:~(8) type""'

Table 12.1: Type lnference Semi-algorithm

12.5 Conclusions and Related Work

Type lnference Semi-algorithm. In order to solve the type inference problem for pure type systems with definitions we have first considered a· syntax directed set of rules and then we have written a function that infers the type based on this syntax directed set of rules. Table 12.1 illustrates our methods.

In the first column of the table we indi.cate if the Il-condition is included or removed, in the second one the original type systems with definitions (with and without the IIcondition), in the third one,.the corresponding type systems with a syntax directed set of rules a.nd finally the functions that infer the type in the original systems.

The definitions of type6 a.nd typéw a.re based on the system tha.t appea.r next tothem intheir preceding columns. ·

The II-condition of .\6(S) is checked in the same system, whereas the one of À:d(8) is checked in .\~~(8). Therefore in the definition of type", the condition that corresponds to the Il-condition is checked using the funcÜon type"w a.nd nót type5. In other words, we have to use typéw to define type".

The II-condition of >.!d(8) is checked in À::f(8) tha.t is weaker than À!d(8). We have proved that if Sis a singly sorted specification then À~d(8) is 'equivalent' to

and .\5(8) by using the relation shown in the diagram (see theorems 12.3.9 and 12.3.10).

Decidability. Table 12.2 summarises some results concerning decidability discussed in this chapter. In the first column, we write the conditions that a pure type system with definitions should verify. In the second and third columns, we say whether type inference a.nd type checking are decidabie or undecidable.

We deduce from the table that the problems of type inference and type checking in the systems of the À5-cube (the À-cube extended with definitions) are decidable.

For the systems of the cube, the problem of inhabitation is decidabie in À( S) if and only if it is decidabie in .\8 (8). The problems of inhabitation in À~ and in À'\!l. are decidabie and in the rest of the systems of the cube extended with definitions it is undecidable.

12.5. CONCLUSIONS AND RELATED WORK 183

Pure type systems Type inference Type checking 1

with definitions S = (S,A,R) singly sorted

S, A a.nd R recursive decidabie decidabie >.(S) is ,6-normalising

S is singly sorted, impredicative and non-dependent

>.5(S) is inconsistent undecidable undecida.ble

i

· Table 12.2: Decida.bility of type inference and type checking

In our opinion, the algorithm for normalising pure type systems whose set of sorts is finite defined in [BJ93] a.nd the syntax directed sets of rules defined in [BJMP93] a.nd in [Pol93a] can he adapted to include definitions by using the fundion I I to prove the equivalence between the syntax directed set of rules and the original system.

184 CHAPTER 12. TYPE INFERENCE FOR DEFINITIONS

Chapter 13

Conclusions

In this chapter we discuss the results of this thesis. We mark what we believe are the main contributions to the field, give a glohal overview and show the interconnections of the sub jects.

The discussion is divided in the same parts as the thesis: 1) an abstract presenta.tion of rewriting and typing, 2) lambda calculus and 3) pure type systems with definitions.

13.1 Abstract Presentation of Rewriting and Typing

In this part, the concepts of computation and typing have been formalised in an abstract way, as binary relations on a set.

The chapters 2 and 4 on abstract rewriting and typing systems give uniformity and clarity to the exposition of these subjects.

The main concepts are defined only once in this abstract setting. It is clea.r that this a.voids repetitions and provides us with a common definition that covers all the particular cases. Moreover it enables us to state properties in a very general way. All these definitions and properties are used in the chapters that follow.

13.2 Lambda Calculus

We have given a characterisation of the set of strongly norma.lising >.-terms that permits us to give new and simple proofs of classical results about À-calculus.

All the proofs of this part follow a comrnon line since they all use the definition of the set SN. Even though we did not prove any new result, the methods for proving them are new.

In most cases the new proofs are essentially simpler than already existing ones and they help us to understand not only the mechanics of the proofs of the results but also the reasons for their validity.

185

186 GRAPTER 13. CONCLUSIONS

13.3 Pure Type Systems with Definitions

In this part we have stuclied the meta-theory of pure type systems with definitions. Also we have written semi-algorithms of type inference for pure type systems with ( and without) definitions.

Pure Type Systems. We have presented the definition of pure type systems in a slightly different way (see chapter 9) from the usual one. The typing rules for pure type systems are parametrie in the specifications. Pure type systems are written as a functor .À that, given a specification S, produces a particular pure type system >.(S). The codomain of this functor is the category of environmental abstract rewriting systems with typing defined in chapter 4.

Definitions. We have considered definitions as part of the formalla.nguage, the language of purè type systems. In our opinion, this extension has been done in a neat and general way (for pure type systems). The inclusion of definitions in the formallanguage and its study have not heen considered before except for the systems of the AUTOMATH fa.mily [NGdV94], that can heseen as partienlar pure type systems.

13.3.1 Normalisation

The two extension of pure type systems, with~ut the II-condition and with definitions have similar properties that are compared in table 13.1. WN is an abbreviation for wea.k normalisation and SN is an abbreviation for strong norma.lisa.tion. In the first column, we write the properties of a pure type system without the II-condition and in the second one, the properties of a pure type system with definitions.

We have written two functions cp a.nd 11, one is a weak converting morphism from À"'(S) to ,\(S) and the Óther is an implementlng morphism from >.8(8) to >.(S). Diagrama.tically,

The function cp computes the illega.l /9-norma.l form of a termand 11 computes the o-normal form of a. term.

In the case of pure type systems without the II-condition, an illegal /9-rewrite sequence is a. superdevelopment. Since all superdevelopments are finite, we have that the illegal /9-reduction is strongly norma.lising. In the case of pure type systems with definitions

13.3. PURE TYPE SYSTEMS WITH DEFINITIONS 187

>."'(8) >.5(8)

<p computes the normal form of I I computes the normal form of the illegal ,8-reduction the .5-reduction

rp is a weak converting morphism I I is an imptementing morphism (8 is singly sorted)

An illegal ,8-rewrite sequence A development is a superdevelopment is a 6-rewrite sequence

The illegal ,8-reduction is SN The 6-reduction is SN

WN(,\(8)) implies WN(>."'(8)) WN(>.(8)) implies WN ( >.6(8) ) (8 is singly sorted)

I SN(..\(8')) implies SN( À"'(8)) SN(..\(8')) implies SN(..\6(8)) (8' is a completion of 8) ( 8' is a completion of 8)

Table 13.1: .\"'(8) vs. >.6(8)

a 8-rewrite sequence is a development. A definition x= a: A in b is like a. marked redex (.,lx: A. b )a. However a 8-rewrite sequence is not a partienlar case of a development and we cannot deduce strong normalisation of the 6-reduction from finiteness of developments. Instead we deduce finiteness of developments from the strong normalisation of the 8-reduction.

In both extensions we have that weak normalisation is preserved by the extension. For pure type systems without the II-condition, we used rp to prove that wea.k normalisation is preserved by the extension and for definitions, we used the mapping 11·

In both cases we have tha.t strong normalisation of the extension follows from the strong normalisation of another (larger) pure type system. In order to prove that strong normalisation is preserved by the extension with definitions, we have written a refinining morphism { } from >.8(8) to >.(8'), S' being a completion of S. The identity is a morphism from ..\"'(8) to .\(S') and it can he used to prove that if .\(S') is strongly normalising then so is ,\w(8) (this proof has notbeen included in this thesis). In the following diagram, we show how these morphisms are rela.ted.

188 CHAPTER 13. CONCLUSIONS

Strong normalisation is the most important property we have proved for definitions. The (3/i-strong norma.lisation of >.( S) follows from the {3-strong norma.lisa.tion of >.( S') with S' a. completion of S. This enables us to prove that for all pure type systems that are known to he (3-strongly normalising, their extensions with definitions are also (3/i-strongly normalising.

Systems that are (38-strongly normalising are, for example, the calculus of constructions extended with definitions and the system of higher order logic extended with definitions.

The question 'Given an arbitrary pure type system, is its extension with definitions strongly normalising?' remains open.

13.3.2 Type Inference

In order to solve the type inference problem for pure type systems with (and without) definitions we have first considered a syntax directed set of rules and then we have written a function that infers the type.

Syntax directed sets of rul es. The definitions of the semi-algorithms of type inference are ba.sed upon the definitions of a synta.x directed set of rules.

Table 13.2 illustrates the type systems we considered.

Original System Syntax-directed System PTS

PTS without 11-cqndition PTS with definitions

PTS with defs. without 11-cond.

>.(S) >."'(S) >.•(S) >.•"'(S)

Table 13.2: Type Systems Considered

Àsd(S) >.~d(S) .X~d(S) >.~d(S)

In the first column oftable 13.2 we find the original systems whose rules are not syntax directed. In the second column, we find the equivalent systems whose rules are syntax directed. Two systems that appear on the same line are equivalent.

In the second and fourth lines, the systems do not contain the II-condition. The systems of the first and third line do contain the II-condition. The II-condition of the systems .X( S) and >.6(S) is checked inside the system, whereas the II-condition of Àsd(S) a.nd À~d(S) is

13.3. PURE TYPE SYSTEMS WITH DEFINITIONS 189

checked outside them. The auxiliary systems used to check the II-condition of >..d(S) and .x:d(S) appear on the line helow in the tahle 13.2.

The following diagram shows some rela.tions between these type systems. We suppose tha.t S is a. singly sorted specifica.tion.

>.~d(S) ~ .xs(S) c >.&"'(S) c A!~(S)

111 <p 111 { 1

Àsd( S) ~ >.(S) c >."'(S) ~ >.~d(S)

In order to prove the equiva.lences >.6(S) ~ >.~d(S) a.nd A(S) ~ >..d(S), we have used the other relations that are depicted in the diagram (see theorems 10.7.7, 10.7.8, 12.3.9 and 12.3.10).

Semi-algorithms. In chapter 10.2 we have defined the semi-a.lgorithm 'type' of type inference for singly sorted pure type systems and in cha.pter 12 the other semi-a.lgorithm 'type6 ' of type inference for singly sorted pure type systems with definitions. These semialgorithms termina.te if the term is typahle and otherwise they may not terminate.

The semi-a.lgorithm type is ba.sed on the system Àsd(S) and type6 is ba.sed on .x:d(S). In order to define both semi-a.lgorithms we have followed the sa.me method. The methad consists of consirlering the corresponding type system without the II-condition to check for the II-condition.

13.3.3 Normalisation versus Type Inference

The norma.lisa.tion property and the type inference problem are related in two places: in the pro9f of the correctness of the semi-algorithms of type inference a.nd in the results on decidability of type inference and type checking for pure type systems with and without definitions.

The proof of correctness. We have proved the correctness of the type inference semialgorithms for singly sorted pure type systems with and without definitions. The correctness of the semi-algorithms is proved using the equivalence between the syntax directed sets of rules and the original systems.

The proof of the equivalence between the syntax directed sets of rules and the origina.l ones, use the relations shown in the diagram above. The morphisms that appear in this diagram are I I and <p which compute the 6-normal form and the iJlegal ,8-normal form, respectively.

190 GRAPTER 13. CONCLUSIONS

Decidability of Type Inferencet Type Checking Let S = ( S, A, R) he singly sorted such that the sets S, A and R are recursive. If A(S) is !9-weak:ly normalising then the following statements on decidability hold.

• Type inference and type checking in A(S) are decidabie (see theorems 9.3.19 and 10.8.16).

• Type inference a.nd type checking in A6(S) aré decida.ble (see theorem 12.4.10).

Undecidability of Type Inference, Type Checking and Inhabitation. LetS be a singly sorted, impredicative a.nd non-dependent specification. If A(S) is inconsistent then the following statements hold.

• Type inference a.nd type checking in >.(S) are undecidable (see theorem 9.3.20).

• Type inference a.nd type checking in >.6(S) are undecidable (see theorem 11.4.14).

Hence, for the systems of the À-cube extended with definitions, we have that type inference and type checking are decidable.

Bibliography

[ACCL91] M. Abadi, L. Cardelli, P. L. Curien, and J.J. Lévy. Explicit substitutions. Journalof Functional Programming, 1(4):375-416, 1991.

[ACN90] L. Augustsson, T. Coquand, and B. Nordström. A short description of another logical framework. In Proceedings of the First Workshop on Logica/ Frameworks, pages 39-42, 1990.

[Acz95] P. Aczel. Schematic consequence. Preprint, 1995.

[Avr92] A. Avron. Axiomatic Systems, Deduction and lmplication. Journalof Logic Computation, 2(1):51-98, 1992.

[Bar74] K. J. Barwise. Axioms for abstract model theory. Annals of Mathematica[ Logic, 7:221-265, 1974.

[Bar85] H. Barendregt. The Lambda Calculus. lts syntax and semantics, volume 103 of Studies in logic and the foundations of mathematica. North Holland. Amsterdam, 1985.

[Bar92] H. Barendregt. Lambda Calculi with Types. In D. M. Gabbai, S. Abramsky, and T. S. E. Maibaum, editors, Handhook of Logic in Computer Science, volume 1, pages 117-309. Oxford University Press, 1992.

[BBKV76] H. Barendregt, J. Bergstra, J. W. Klop, and H. Volken. Degrees, reductions and representability in the lambda calculus. Technica! Report 22, University of Utrecht, 1976.

[Ber88] S. Berardi. Towards a mathematica} analysis of the Coquand-Huet calculus of constructions and Other Systems in Barendregt's Cube. Technica! report, Carnegie Mellon University and Universitá di Torino, 1988.

[Ber90] S. Berardi. Type Dependency and Constructive Mathematics. PhD thesis, Carnegie Mellon University and Universitá di Torino, 1990.

[BG96] R. Bloo and H. Geuvers. Explicit substitution: On the edge of strong normalisation. Teehuical Report CS-9610, Eindhoven University of Technology, 1996.

191

192 BIBLIOGRAPHY

[BJ93] B. van Benthem Jutting. Typing in pure type systems. lnformation and Computation, 105:30-41, 1993.

[BJMP93] B. van Benthem Jutting, J. McKinna, and R. Pollack. Checking algorithms for pure type systems. In H. Barendregt and T. Nipkov, editors, Types for Proofs and Programs, volume 806, 1993.

[BK82] J. A. Bergstra and J. W. Klop. Strong normalization and perpetual reductions in the lambda calculus. Journal of Information Processing and Cybernetics, 18:403-417, 1982.

[BKKS87] H. P. Barendregt, J. R. Kennaway, J. W. Klop, and M. R. Sleep. Needed rednetion and spine strategies for the lambda calculus. Information and Computation, 75:191-231, 1987.

[Blo97] R. Bloo. In preparation. PhD thesis, Eindhoven University of Technology, 1997.

[Bru70] N. G. de Bruijn. The mathematicallanguage of AUTOMATH, its usage and some of its extensions. In Symposium on automatic demonstration, volume 125 of LNCS, pages 27-61. Springer, 1970.

[Bru72] N. G. de Bruijn. Lambda calculus notation with nameless dummies, a tooi for automatic formula manipulation. Indag. Math., pages 381-392, 1972.

[CF58] H. B. Curry and R. Feys. Combinatory Logic. North Holland. Amsterdam, 1958.

[CH88] T. Coquand and G. Huet. The Calculus of Constructions. Information and Computation, 76:95-120, 1988.

[CH94] T. Coquand and H. Herbelin. A-translation and looping combinatars in pure type systems. Journat of Functional Programming, 4(1):77-88, 1994.

[Chu40] A. Church. A formulation of the simply theory of types. Journalof Symbolic Logic, 5:56-68, 1940.

[Chu41] A. Church. The Calculi of Lambda-Conversion. Princeton University Press. Princeton., 1941.

(Con86] R. L. Constabie et al. Implementing rnathematics with the NuPRL Proof Development System. Prentice-Hall, Englewood Cliffs, NJ, 1986.

[Coq85] T. Coquand. Une Théorie des constructions. PhD thesis, {]niversité Paris VII, 1985.

[Coq91] T. Coquand. An algorithm for testing conversion in type theory. In Logical Frameworks, 1991.

BIBLIOGRAPHY 193

[Coq96]

[CP90]

[CR36]

[Cur34]

[Cur69]

[Daa80]

[DM82]

[Dow91]

[FS93]

[Gal90]

[Gan80]

[Geu93]

[Ghi94]

T. Coquand. An Algorithm for Type-Checking Dependent Types. In, B. Bjerner, M. Larsson, and B. Nordström, editors, Proceedings of the 7th Nordie Workshop on Programming Theory. Göteborg University and Chalmers University of Technology, 1996.

T. Coquand and C. Paulin. Inductively Defined Types. In P. Martin-Löf and G. Mints, editors, COLOG-88, volume 417 of LNCS, pages 50-66. Springer, 1990.

A. Church and J. B. Rosser. Some properties of conversion. Transactions of the Americal Society, 39:472-482, 1936.

H. B. Curry. Functionality in combinatory logic. Proc. nat. Acad. Science USA, 20:584-590, 1934.

H. B. Curry. Modified based functionality in combinatory logic. Dialectica, 23:83-92, 1969.

D. T. van Daalen. The Language Theory of Automath. PhD thesis, Eindhoven University of Technology, 1980.

L. Damas and R. Milner. Principal type-schernes for functional programs. In Principles of Programming Languages, pages 207-212. ACM, 1982.

G. Dowek et aL The Coq proof assistant version 5.6, users guide. Rapport de Recherche 134, INRIA, 1991.

W. Ferrer and P. Severi. Abstract reduction and topology. Technica! Report CS-9335, Eindhoven University of Technology, 1993.

J. H. Gallier. On Girard's 'candidats de reductibilité'. In P. Odifreddi, editor, Logic and Computer Science, volume 31 of APIC, pages 123-203. Academie Press, London, New York, 1990.

R.O. Gandy. An Early Proof of Normalisation by A. M. Turing. In J. R. Hindley and J. P. Seldin, editors, To H. B. Curry: Essays in Combinatory Logic, Lambda Calculus and Formalism, pages 453-455. Academie Press, 1980.

H. Geuvers. Logies and Type Systems. PhD thesis, University of Nijmegen, 1993.

S. Ghilezan. Applications of typed lambda calculi in the untyped lambda calculus. In A. Nerode and Yu. V. Matiyasevich, editors, Logical Foundations of Computer Science: Proceedings of the Third International Symposium, volume 813, pages 129-139. Springer-Verlag, 1994.

194 BIBLIOGRAPHY

[Gir72] J. Y. Girard. Interprétation fonctionelle et élimination des coupures dans l'arithmétique d'ordre supérieur. PhD thesis, Université Paris VII, 1972.

[GLM92) G. Gonthier, J. Lévy, and P.A. Melliès. An abstract standardisation theorem. In Seventh Annual IEEE Symposium on Logic In Computer Science, pages 72-81. IEEE, 1992.

[GLSH92) E. R. Griffor, I. Lindstroem, and V. Stoltenberg-Hansen. Mathematica[ Theory of Domains. Cambridge University Press, 1992.

[GN91] H. Geuvers and M. J. Nederhof. A modularproof of strong normalization for the calculus of constructions. Journat of Functional Programming, 1(2):155-189, 1991.

[HA91] L. Helmink and R. M. C. Ahn. Goal directed proof construction in type theory. In Procs. of the jirst Workshop on Logica! Frameworks. Cambridge University Press, 1991.

[HMM86) R. Harper, D. MacQueen, and R. Milner. Standard ML. LFCS Report Series ECS-LFCS-86-2, LFCS-University of Edinburgh, March 1986.

[How80] W. A. Howard. The formulae-as-types notion of construction. In J. R. Hindley and J. P. Seldin, editors, To H. B. Curry: Essays in Combinatory Logic, Lambda Calculus and Formalism, pages 479-490. Academie Press, 1980.

[HST89] R. Harper, D. Sanella, and A. Tarlecki. Structure and Representation in LF. In IEEE Fourth Annual Symposium On Logic in Computer Science, pages 226-237, 1989.

[HW88] P. Hudak and P.L. Wadler. Report of the functional programming language Haskell. Technica! Report YALEU/DCS/RR656, Yale University, 1988.

[Hyl73] J. M. E. Hyland. A simpleproof of the Church-Rosser theorem. Technica! report, Oxford University, 1973.

[Kel55] J. Kelley. General Topology. Van Norcistram Company, Inc., 1955.

[KKS95] R. Kennaway, J. W. Klop, and R. Sleep. Infinitary lambda calculus. Teehuical report, CWI, Amsterdam, 1995.

[KKSdV91] R. Kennaway, J. W. Klop, R. Sleep, and F. J. de Vries. Transfinite reductions in orthogonal term rewriting systems. Technica! report, Centre for Mathernaties and Computer Science Amsterdam, 1991.

[Klo80] J. W. Klop. Combinatory Reduction Systems, PhD thesis, Rijksuniversiteit Utrecht, 1980.

BIBLIOGRAPHY 195

[Klo90]

[KN93]

[KN95]

[Kri93]

[Lev78]

(Loa95]

(LP92]

[LSZ96]

[Luo89]

[Luo90]

[Mag94]

[Mel96]

[Mes89]

[Mil78]

[Mit86]

[MR86]

J. W. Klop. Term rewriting systems. Technica.l Report CS-R9073, Centre for Mathernaties and Computer Science Amsterdam, 1990.

F. Kamareddine and R. Nederpelt. On stepwise explicit substitution. Internation Journal of Foundation of Computer Science, 4(3):197-240, 1993.

F. Kamareddine and R. Nederpelt. Refining reduction in the lambda calculus. J. Functional Programming, 5(4):637-651, 1995.

J. L. Krivine. Lambda-Calculus, Types and Models. Masson, France, 1993.

J. J. Lévy. Réductions correctes et optimales dans le lambda-calcul. PhD thesis, Université de Paris VII, 1978.

R. Loader. Normalisation by translation. Note distributed on the 'types' mailing list, 1995.

Z. Luo and R. Pollack. LEGO proof development system: User's manual. Technica! Report ECS-LFCS-92-211, LFCS-University of Edinburgh, 1992.

T. Laan, P. Severi, and J. Zwanenburg. Pure type systems with parameters. Technica! Report To appear, Eindhoven University of Technology, 1996.

Z. Luo. ECC, the Extended Calculus of Constructions. In The fourth Annual Symposium on Logic in Computer Science, pages 386-395. IEEE, 1989.

Z. Luo. An Extended Calculus of Constructions. PhD thesis, University of Edinburgh, 1990.

L. Magnusson. The lmplementation of Alf-a proof editor based on Martin-Liif 's Monomorphic Type Theory with Explicit Substitution. PhD thesis, Chalmers University of Technology, 1994.

P. A. Melliès. Description abstraite de Systémes de Réécriture. PhD thesis, Université de Paris VII, 1996.

J. Meseguer. Relating modelsof polymorphism. In Principlesof Programming Languages, pages 228-241. ACM, 1989.

R. Milner. A theory of type polymorphism in programming. Journal of Computer and System Sciences, 17:348-375, 1978.

J. C. Mitchell. A type-inference approach to reduction properties and semantics of polymorphic expressions. In Proc. 1986 ACM Symposium on Lispand Functional Programming, pages 308-319. ACM, 1986.

A. R. Meyer and M. B. Reinhold. 'type' is not a type: Preliminary report. In Principles of Programming Languages, pages 287-295. ACM, 1986.

196 BIBLIOGRAPHY

[Ned73] R. P. Nederpelt. Strong Normalisation in a Typed Lambda-Calculus with Lambda-Structured Typed. PhD thesis, Eindhoven University of Technology, 1973.

[Ned92] R. P. Nederpelt. The fine-structure of lambda calculus. Computer science notes CS-9207, TU, 1992.

[N ew42] M. H. A. Newman. On theories with a combinatorial definition of 'equivalence'. Annals of Mathematics, 43(2):223-243, 1942.

[NGdV94] R. P. Nederpelt, J. H. Geuvers, and R. C. de Vrijer. Selected Papers on Automath. Studies in Logic and the Foundation of Mathematics, volume 133. North Holland Pub!. Company, 1994.

[NPS90] B. Nordström, K. Petersson, and J. M. Smith. Programming in Martin-Löf's Type Theory. An introduction. Oxford Science Publications, 1990.

[Oos94] V. van Oostrom. Confluence for Abstract and Higher-Order Rewriting. PhD thesis, Vrije Universiteit, Amsterdam, 1994.

[Par90] M. Parigot. Internallahelling in lambda calculus. In MFCS'90, number 452 in LNCS, pages 439-445. Springer, 1990.

[Pol93a] E. PolL A type checker for bijective pure type systems. Computing Science Note CS-9322, Eindhoven University of Technology, 1993.

[Pol93b] R. Pollack. Ciosure under Alpha Conversion. InH. Barendregtand T. Nipkov, editors, In Types for Proofs and Programs, LNCS, pages 313-332. SpringerVerlag, 1993.

[Pol94] E. Poll. A Programming Logic Basedon Type Theory. PhD thesis, Eindhoven University of Technology, 1994.

[Pol96J R. Pollack. A Verified Type Checker. In B. Bjerner, M. Larsson, and B. Nordström, editors, Proceedings of the 7th Nordie Workshop on Programming Theory, 86. Göteborg University and Chalmers University of Technology, 1996.

[Raa93J F. van Raamsdonk. Confiuence and superdevelopment. In Claude Kirchner, editor, Rewriting Techniques and Applications, volume 690 of LNCS, pages 168-182. Springer-Verlag, 1993.

[Raa96J F. van Raamsdonk. Confluence and Normalisation for Higher-Order Rewriting. PhD thesis, VUA, University of Amsterdam, 1996.

[Reg94] L. Régnier. Une équivalence sur les lambda-termes. Theoretica{ Computer Science, 126:281-292, 1994.

BIBLIOGRAPHY 197

[RS95]

[Rey74]

[RHP87]

[Sch65]

[Sev96]

[Sor94]

[SP93]

(SP94]

[Spr95]

[Tai67]

[Tai75]

[Tas93]

[Ter89]

F. van Raamsdonk and P. Severi. On normalisation. Teehuical Report CSR9545 and CS-9520, CWI-Amsterdam and TU-Eindhoven, 1995.

John C. Reynolds. Towards a theory of type structure. In Programming Symposium: Colloque sur la Programmation, volume 19 of LNCS, pages 408-425. Springer, 1974.

F. Honsell R. Harper and G. Plotkin. A Framework For Defining Logies. In Proc. Second Symposium of Logic in Computer Science, pages 194-204, 1987.

D. E. Schroer. The Church-Rosser Theorem. PhD thesis, Cornell University, 1965.

P. Severi. Pure type systems without the II-condition. In B. Bjerner, M. Larsson, and B. Nordström, editors, Proceedings of the 7th Nordie Workshop on Programming Theory, 86. Göteborg University and Chalmers University of Technology, 1996.

M. H. Sf/lrensen. Etfective longest reductions paths in untyped ,\-calculus. Draft, Katholieke Universiteit Nijmegen, 1994.

P. Severi and E. Poll. Pure type systems with definitions. Computing Science Note CS-9324, Eindhoven University of Technology, 1993.

P. Severi and E. Poll. Pure type systems with definitions. In A. Nerode and Yu. V. Matiyasevich, editors, Logical Foundations of Computer Science: Proceedings of the Third International Symposium, 813, pages 316-328. LFCS'94, St. Petersburg Russia, Springer-Verlag, Berlin, New Vork, 1994.

J. Springintveld. Algorithms for Type Theory. PhD thesis, Department of Philosophy - Utrecht University, 1995.

W. W. Tait. Intensional interpretation of functionals of finite type I. Journat of Symbolic Logic, 32:198-212, 1967.

W. W. Tait. A realizabilityinterpretation of the theory of species. In R. Parikh, editor, Proceedings of the Logic Colloquim, volume 453 of LNCS, pages 2'40-251. Springer, Berlin, 1975.

A. Tasistro. Formulation of Martin-Löf's Theory of Types with Explicit Substitution. Computing science note, Chalmers University of Technology and University of Göteborg, 1993.

J. Terlouw. Een nadere bewijstheoretische analyse van GSTT's. Manuscript, 1989.

198

[Tro73]

[Tur85]

[Urz93a]

[Urz93b]

[Vri85]

[Vri87]

[Wel94]

BIBLIOGRAPHY

A. S. Troelstra. Metamathematical Investigations of lntuitionistic Arithmetic and Analysis, volume 344 of LNM. Springer, 1973.

D. A. Turner. Miranda: A non-strict functional language with polymorphic types. In IFIP lnt'l Conf. Funct. Program. Comput. Arch., volume 201 of LNCS. Springer, 1985.

P. Urzyczyn. The emptiness problem for intersection types. Technica! report, Institute of Informaties University of Warsaw, 1993.

P. Urzyczyn. Type Reconstruction in F wis undecidable. In Int'l Conf. Typed Lambda Calculus and its Applications, volume 664 of LNCS, pages 418-432. Springer, 1993.

R. de Vrijer. A Direct Proof of the Finiteness of Developments Theorem. The Journal of Symbolic Logic, 50, 1985.

R. de Vrijer. Exactly estimating fundionals and strong normalization. In Proceedings ofthe Koninklijke Nederlandse Akademie van Wetenschappen, volume 90, pages 479-493, 1987.

J. B. Wells. Typablity and Type Checking in the Second Order À-calculus are Equivalent and Undecidable. In Ninth Anual IEEE Symposium on Logic in Computer Science, pages 176-185. IEEE, 1994.

Index

abstract rewriting system, 16 irreflexive, 33 weakly irreflexive, 33 with typing, 37, 40 with very weak typing, 40 with weak typing, 40

abstract rewriting systems In, 16 I, 16

abstract typing system, 38

bound variable, 52, 90, 137 BV, 52, 90, 137

categories Ars, 17 Arst, 41 Arstw, 41 Carst, 46 Carst""'' 46 Carstw, 46 Spec, 88 Ats, 38

common-reduct strategies F, 124 F++, 127 Ftt, 177 F+, 126 Fn, 123 Fib 177

condition II, 98

confluent, 18, 19, 44 context, 38, 44, 52 conversion

a:-conversion, 52, 91, 138

199

.8-conversion, 2 conversion *""*• 16

definition, 6 global, 6, 136 local, 6, 136

depth depth, 103 development, 2, 65, 66

environment, 38, 44 environmental

abstract rewriting system, 43 with typing, 38, 45 with very weak typing, 45 with weak typing, 45

abstract typing system, 44 extension, 17, 38, 45

conservative, 17, 38 strong, 17

free variable, 52, 90, 136 FV, 51, 90, 136

illegal .8-redex, 110 illegal .8-reduction, 115 illegal abstraction, 110 inhabitation, 8 inhabited, 37, 38, 45 interpretation, 46

lambda cube, 93 lambda terms, 1, 51

A, 51 legal abstraction, 110 lifting, 18 loop, 33

one-step, 33

200

terminal, 33

morphism, 16, 38, 40, 44, 45, 88 converting, 46 forgetting, 17, 41, 44, 46 implementing, 17, 41, 44, 46 refining, 17, 41, 44, 46 weak converting, 47

morphisms <p, 110, 115 I I, 145, 156, 157 { }, 162, 167

normal form, 2, 18 nf, 125

perpetual strategies F=, 58 Fbk, 57 F!,, 152 Gbk, 57 G=, 59

pseudocontexts c5, 137 c, 91

pseudoterms 4, 136 T, 90

pure type system, 5, 93 inconsistent, 94 with definitions, 141 with definitions and without the ll

condition, 173 without the II-condition, 99

redex, 2 creation of new redexes, 69 spine redex, 54

reduction, 16 ,8-reduction, 1, 52, 138, 140 ,81-reduction, 70 ,8,-reduction, 115 b'-reduction, 6, 139, 140 ~-reduction, 66

reduction graphs hounded 9~~., 178 bounded g~~. 124 simple 9-, 19

rewrite relation, 16 inverse rela.tion +-, 16

INDEX

reflexive dosure _,.=, 16 transitive dosure _,.+, 16 transitive-reflexive dosure -, 16

rewrite sequence, 17, 43 infinite, 18 maximal, 18

semantics, 46 sets of toptypes

Nr, 107 Mr, 102

simply typed lambda calculus, 3, 80, 94 specification, 87

completion, 164 full, 89 impredicative, 89 logical, 89 non-dependent, 89 quasi-full, 164 singly sorted, 89

strategy, 3, 19, 44 common-reduct, 21 maximal, 20, 59 non-deterministic, 20 normalising, 3 one-step, 20 perpetual, 20

strongly normalising, 2, 8, 19, 41, 44, 45 lambda terms SN, 54

subject reduction, 37, 39 very weak, 39 weak, 39

substitution, 52, 90, 91, 137, 138 superdevelopment, 71 symmetrie topology, 30

associated to a topology, 31 syntax directed rules, 97, 118

INDEX

term, 38, 44 topsort, 89 toptype, 38,45,102 transitive closure, 30 typable, 37, 38, 44 type, 3, 38, 45 type checking, 8 type checking semi-algorithms

check, 131 check'\ 181

type inference, 8 type inference semi-algorithm, 121, 176 type inference semi-algorithms

type, 129 typé, 180 typéw, 179 typew, 130

type reduction, 37, 40 weak, 39

type systems · >.(S), 92

Àad(S), 120 .\6(S), 141 >.~d(S), 174 ).6w(S), 173 >.~;r(S), 173 Àw(S), 98 >.::'d(S), 119

typing relation, 37, 38 typing relations

1-, 92 ~. 140 ~d> 175 ~-~:r' 173 1-.d, 120 1-W 1 98 l-5w, 173 1-::d, 119

union of rewrite relations, 19, 43 uniqueness of types, 43, 45

weak head ,86-reduction, 173

201

weak head ,8-reduction, 118 weak head normal form semi-algorithms

whnf, 122 whnfps, 176

weakly normalising, 2, 8, 19, 41, 44, 45

STELLINGEN

behorende hij bet proefschrift

N ormalisation in Lambda Calculus

and its relation to Type Inference

PAULA GABRIELA SEVERI

1. The function { } maps a ó-redex into a K-redex (see definition 11.4.23). Wc define N'(S) as the extension of the pure type system .\(S) with the combinator K (see [2]) with reduction -+~ and typing rule 1-" defined as follows.

(Ka b) -.,.a

fl-,.a:A fl-,.b:B fi-,.(Kab):A

The mapping { } can be viewed as a morphism from .\6(S) to A"(S). As a con.~equence, we have the following result.

A"(S) is )3~~:-strongly normalising if and only if .\6(S) is j3ó-strongly norrnalising.

2. If the specification S of a pure type system .\(S) is a completion of itself then the typing rela.tion 1-"' for .\(S) without the 11-condition is equal to the typing relation lof .\(S) (see [5]).

This means that the 11-condition is redundant for some pure type systems and we can use type"' instead of type to infer the type of a term in these systerns.

For example, the specification 0 00 is a completion of itself (see example 11.4.3) and so the 11-condition in .\000 is redundant. Hence, we can use type"' instead of type to infer the type in ÀC"".

3. What is use of the abstract typing systems? One of its uses is that they allow us to write pure type systems as {unctors with domain the category of specifications and codomain the category of abstract typing systems. The constructions we perform ca.n be viewed as natura! transformations (see [3]). For example, the extension with definitions and the weakening construction are natura.! transformations between functors as the diagram illustra.tes.

4. In the topological characterisation of confinenee and normalisation, the operators C a.nd S play an important role (see definition 3.2.3 and lemma. 3.3.9). These characterisa.tions could also be expressed in the a.lgebraic setting of boolean algebras { see (1]). This leadstoa complete algebra.ic description of abstract rewriting systems and their properties. .

1

5. Almost all the proofs in this thesis except for the ones in the first chapter are inductive proofs. This is because all definitions are inductive. Is it possible to a.void this situa.tion? I believe not.

6. Grothendieck's motto was 'simplify and genera.lise' (see [4]). He cla.ims that the best definitions are the ones that make our proofs of the theorems trivia!. Does he mean that the theory can he trivialised? I do not think so. He means that tbe really difficult job is to find the adequate definitions.

7. In the following phrase a taoïst a.uthor [9] wonders if we can free ourselves of form.

At first, form is needed. Then doubt and inhibition must he dispelled. Eventually, form is celebrated with joy, And expression becomes formless.

As soon as expression is accomplished, we ca.n free ourselves of form.

8. There are at least two w~ys of acquiring understanding. One is by means of logical reasoning and it is good for the forma! level. In order to understand our dreams, our emotions, our past, etc, we need to apply another kind of reasoning, called analogical reasoning. In my opinion, real creativity puts these two tools to work together.

9. Spiritual development is represented by the symbol of a mounta.in (see [7]). There are many paths to the summit and there are many ways to walk along these paths. There are peóple who climb at a regular and steady pace. Others are always taking shortcuts. The former advance on firm steps and the latter take many risks a.nd ca.n be hurt along their way. From an individual viewpoint both alternatives are valid. Nowadays, our society is following the second alternative and this could he dangerous.

10. The Chaldeans worshiped, as powers: Earth, Water, Wind and Fire. They ca.lled them the Elements (crro~xc:u:x). The system which recognizes theseelementsas the prime sourees of all things, is a creation of the Greeks (see [10], [8], [6]). There are tbree astrological signs associated to earth: Taurus, Virgo and Capricorn. The outstanding traits associated to Taurus are practicality, possesiveness and gentleness.

In my opinion The Netherlands has Taurus influence. There are severa.l reasons tha.t susta.in rny belief. One of the most important holidays is 'de koninginnedag', tbe 30th of April. Dutch people are polite, concrete a.nd practical (grounded). Moreover, Dutch society is well-organised a.nd it is a matter of nationa.l pride to have taken someland (earth) from the sea..

2

References

[1] G. Birkhoff. Lattice Theory. Americal Mathematica! Society, Publications. volume 25. Providence. 1967.

[2] J. Hindley, J. P. Seldin. Introduetion to Combinatars and >.-Calculus. Cambridge Universtity Press, Cambridge. 1986.

[3] S. MacLane. Categories for the Working Mathematician. Springer-Verlag. Berlin Heidelberg New York. 1971.

[4] J. Dieudonné. Les travau:e de Alexander Grothendieck. In Proceedings of the International Congress of mathematicians, Moscow. 1966. I. G. Petrovsky, ed. Ed. Mir. Moscow. 1966.

[5] P. Severi. A type inference algorithm for pure type systems. Technical report CS 9505. Eindhoven University of Technology.

[6] T. Barton. Ancient Astrology. Series Scienceof Antiquity. Routledge, London. 1994.

[7] U. Becker, ed. The continuurn Encyclopedia of symbols. The Continuurn Publishing Compa.ny, New York. 1994.

[8] F. Cumont. Astrology andReligion among the Greeks and Romans. Dover Publications, Inc. New Vork. 1960.

[9] Dend Ming-Dao. 365 TAO: Daily Meditations. HarperCollins Publishers, San Fran· cisco. 1992.

[10] A. S. Lyons. Predicting the juture. H. N. Abrams Inc., New York. 1990.

3