Linearisation of the Lambda-Calculus

2 downloads 0 Views 1MB Size Report
O λ-calculus linear é um subconjunto do λ-calculus para o qual, nenhuma ... Identificamos uma classe de λ-termos, `a qual chamamos λ-calculus afim ...... let 〈z1,z2〉 = 〈M,M〉 in G′z1((iter (Sk 0) F S)z2) ...... of Mathematics, 36(3):630–636.
Sandra Maria Mendes Alves

Linearisation of the Lambda-Calculus

Departamento de Ciˆencia de Computadores Faculdade de Ciˆencias da Universidade do Porto 2007

Sandra Maria Mendes Alves

Linearisation of the Lambda-Calculus

Tese submetida ` a Faculdade de Ciˆencias da Universidade do Porto para obten¸c˜ao do grau de Doutor em Ciˆencia de Computadores Supervisor: Prof. M´ ario Florido

Departamento de Ciˆencia de Computadores Faculdade de Ciˆencias da Universidade do Porto 2007

Aos meus pais, ao Paulo, ao Manuel, ao Rui e ` a Frida...

–3–

Acknowledgements First of all, I would like to thank my supervisor, M´ ario Florido, for all the support he gave me throughout these years. This thesis would not have been possible without his valuable suggestions and constant availability. To my colleagues at the Computer Science Department, I am thankful for some very good years of great environment, and enjoyable lunches. In particular, I would like to thank Sabine Broda for providing me her LATEX macros, and for making meetings and talks more “sweet”, and to Ana Paula Tom´as, for inviting me to “Sexta Napolitana”, which really helped me in these last months. I also thank Rog´erio Reis and Nelma Moreira for their legal advises. To Lu´ıs Antunes, who shared an office with me for so many years, I thank all his friendship, good company, and also all the cookies and bottles of water! To Laurent Regnier for his helpful comments on some aspects of legal paths, and for answering all my questions so rapidly. I thank Joe Wells from Heriot-Watt, for the friendly way I was always received at the ULTRA Group, and for everything he taught me. To Ian Mackie and Maribel Fern´ andez, with whom I have learned so very much, thank you for all the time I have spent at King’s (both the College and the Road). To Fran¸cois-R´egis Sinot, for helping with the translation of the abstract. “Je suis tr`es reconnaissant!” For financial support, I would like to thank LIACC, the “Funda¸c˜ao para a Ciˆencia e Tecnologia”, CRUP, the British Council, and the “Funda¸c˜ao Calouste Gulbenkian”. To all my friends, for the life they give me beyond science, and without whom I would be a complete “geek” (at least, that is what they say). I would particularly like to thank Ana Dinger, Carla Gomes, Patr´ıcia Cardoso (my swan sister), Pedro Matos, and Raquel Santos. (I have put your names in alphabetic order!) Last but not least, to my amazing family. My parents who sacrificed so much, so I could have the opportunities they did not have. To my brothers, Manuel and Paulo, for making me laugh with so much nonsense. And finally, to Rui, for all his love, and for sharing a life with me... –5–

Abstract The goal of this thesis is to study the use of linearity in the definition of efficient simulations of the λ-calculus. In particular, our aim is to focus on the following issues: • The definition of linearisation algorithms from the λ-calculus into more linear calculi; • Extending the linear λ-calculus to more expressive “almost linear calculi”, without loosing the linear λ-calculus good computational properties. The linear λ-calculus is a subset of the λ-calculus, for which, no variable occurs more than once in any subterm of any term, and for any function, the argument of the function occurs in the body of the function. This subset has good computational properties, but has very little computational power. The contributions of this work, are the following: • We use linearisation to establish a relation between Rank 2 Intersection Types and Simple Types. We define a transformation in the λ-calculus that transforms a term typable in the Rank 2 Intersection Types System with a simple type, into a term typable in the Simple Types System, with the same type. • We identify a restricted class of λ-terms, which we call weak affine λ-calculus, and define a transformation from terms in the λ-calculus to terms in that class. Weak affine λ-terms are strongly normalisable, and typable in polynomial time. We prove that the transformation is correct, in the sense that it preserves the normal forms. • We extend the linear λ-calculus with pairs, booleans, natural numbers, and an iterator. We define a new notion of polymorphic iteration, which we call iterative types, and a reduction relation for the calculus, based on closed reduction. We define a compilation from terms in G¨ odel’s System T , to terms in our system. This allows us to study the computational power of our system: it has the same computational power of System T.

–7–

Resumo O objectivo desta tese ´e estudar o uso de linearidade na defini¸c˜ao de simula¸c˜oes eficientes do λ-calculus. Em particular, pretendemos focar nos seguintes pontos: • A defini¸c˜ao de algoritmos de lineariza¸c˜ao do λ-calculus para c´alculos mais lineares; • Extender o λ-calculus linear, a “c´alculos quase lineares” mais expressivos, sem perder as boas propriedades computacionais do λ-calculus. O λ-calculus linear ´e um subconjunto do λ-calculus para o qual, nenhuma vari´avel ocorre mais do que uma vez em qualquer subtermo de qualquer termo e, para qualquer fun¸c˜ao, o argumento ocorre no corpo da fun¸c˜ao. Este subconjunto tem boas propriedades computacionais, mas muito pouco poder computacional. As contribui¸c˜oes deste trabalho s˜ ao as seguintes: • Usamos lineariza¸c˜ao para estabelecer uma rela¸c˜ao entre Tipos com Interse¸c˜oes de Rank 2 e Tipos Simples. Definimos uma transforma¸c˜ao no λ-calculus, que transforma um termo tipific´ avel no Sistema de Tipos com Intersec¸c˜oes de Rank 2 com um tipo simples, num termo tipific´ avel no Sistema de Tipos Simples, com o mesmo tipo. • Identificamos uma classe de λ-termos, `a qual chamamos λ-calculus afim fraco, e definimos uma transforma¸c˜ao de termos no λ-calculus para termos nessa classe. Os termos afins fracos s˜ ao fortemente normaliz´ aveis, e tipific´ aveis em tempo polinomial. Provamos a correc¸c˜ao da transforma¸c˜ao, no sentido em que preserva a forma normal dos termos. • Extendemos o λ-calculus linear com pares, boleanos, n´ umeros naturais e um iterador. Definimos uma nova no¸c˜ao de itera¸c˜ao polim´orfica, `a qual chamamos tipos iterativos, e uma rela¸c˜ao de redu¸c˜ao para o novo c´alculo, baseada em redu¸c˜ao fechada. Definimos uma compila¸c˜ao de termos no Sistema T de G¨ odel, para termos no nosso sistema. Isso permite-nos estudar o poder computacional do nosso sistema: tem o mesmo poder computacional do Sistema T .

–9–

R´esum´e Le but de cette th`ese est d’´etudier l’utilisation de la lin´earit´e dans la d´efinition de simulations efficaces du λ-calcul. En particulier, notre but est de nous concentrer sur les questions suivantes : • La d´efinition d’algorithmes de lin´earisation du λ-calcul vers des calculs plus lin´eaires ; ´ • Etendre le λ-calcul lin´eaire des calculs presque lin´eaires plus expressifs, sans perdre les bonnes propri´et´es calculatoires du λ-calcul lin´eaire. Le λ-calcul lin´eaire est un sous-ensemble du λ-calcul, dans lequel aucune variable n’apparat plus d’une fois dans n’importe quel sous-terme de n’importe quel terme, et pour n’importe quelle fonction, l’argument de la fonction apparat dans le corps de la fonction. Ce sousensemble a de bonnes propri´et´es calculatoire, mais a tr`es peu de puissance calculatoire. Les contributions de ce travail sont les suivantes : • Nous utilisons la lin´earisation pour ´etablir une relation entre les Types Intersection de Rang 2 et les Types Simples. Nous d´efinissons une transformation du λ-calcul qui transforme un terme typable dans le Syst`eme de Types Intersection de Rang 2, avec un type simple, en un terme typable dans le Syst`eme de Types Simples, avec le mˆeme type. • Nous identifions une classe restreinte de λ-termes, que nous appelons le λ-calcul affine faible, et d´efinissons une transformation des termes du λ-calcul vers les termes de cette classe. Les λ-termes affines faibles sont fortement normalisables, et typables en temps polynomial. Nous prouvons que la transformation est correcte, dans le sens qu’elle pr´eserve les formes normales des termes. • Nous ´etendons le λ-calcul lin´eaire avec des paires, des bool´eens, des entiers naturels et un iterateur. Nous d´efinissons une nouvelle notion d’it´eration polymorphe, que nous appelons types it´eratifs, et une relation de r´eduction pour ce calcul, bas´ee sur la r´eduction close. Nous d´efinissons une compilation des termes du Syst`eme T de G¨ odel vers les termes de notre syst`eme. Ceci nous permet d’´etudier la puissance calculatoire de notre syst`eme : il a la mˆeme puissance calculatoire que le Syst`eme T . – 11 –

Contents

Acknowledgements

5

Abstract

7

Resumo

9

R´ esum´ e

11

List of Figures

20

1 Introduction

21

1.1

Lambda Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

1.2

Linear Calculi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

1.3

Linearisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

1.4

Almost Linear Calculi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

25

1.5

Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26

2 Background

29

2.1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

29

2.2

Reduction Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

30

2.3

The λ-calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

2.3.1

Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

2.3.2

Variables and Substitutions . . . . . . . . . . . . . . . . . . . . . . . .

34

14

CONTENTS

2.4

2.5

2.3.3

The hΛ, βi Reduction System . . . . . . . . . . . . . . . . . . . . . . .

35

2.3.4

Subsystems of the lambda calculus . . . . . . . . . . . . . . . . . . . .

38

Typed Lambda Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

39

2.4.1

Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

39

2.4.1.1

Type-checking and Typability . . . . . . . . . . . . . . . . .

42

2.4.2

Intersection Type Systems . . . . . . . . . . . . . . . . . . . . . . . . .

44

2.4.3

System T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

49

3 Simple Linearisation

51

3.1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

51

3.2

Simple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

52

3.3

Rank 2 Intersection Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

53

3.4

T -Reductions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

54

3.4.1

Multisets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

55

3.4.2

Confluence and normalisation for T -reduction . . . . . . . . . . . . . .

55

3.5

The IT Type System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

61

3.6

Simple Linearisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

64

3.6.1

Correctness of T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

70

3.7

4 Paths and Labels

71

4.1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

71

4.2

The Labeled Lambda Calculus . . . . . . . . . . . . . . . . . . . . . . . . . .

72

4.3

Labels as Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

76

4.4

Legal Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

77

4.4.1

78

Well Balanced Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CONTENTS

4.4.2 4.5

15

Legal Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

83

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

84

5 Weak Linearisation

85

5.1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

85

5.2

The Weak Affine Lambda Calculus . . . . . . . . . . . . . . . . . . . . . . . .

86

5.3

System TW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

88

5.3.1

A Type System for Weak Affine Terms . . . . . . . . . . . . . . . . . .

89

5.3.2

A Type Inference Algorithm . . . . . . . . . . . . . . . . . . . . . . . .

92

Weak Linearisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

96

5.4.1

Term transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . .

96

5.4.2

Correctness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

5.4

5.5

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

6 Linear Lambda Calculus with Iterators

109

6.1

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

6.2

Recursors versus Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

6.3

Linear λ-calculus with Iterator: System L . . . . . . . . . . . . . . . . . . . . 112

6.4

6.3.1

Correctness of −→ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

6.3.2

System L . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

6.3.3

Subject Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

6.3.4

Strong Normalisation . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

6.3.5

Confluence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

6.3.6

Adequacy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

7 Expressiveness of System L 7.1

133

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

16

CONTENTS

7.2

7.3

7.4

7.5

Primitive Recursion and Beyond . . . . . . . . . . . . . . . . . . . . . . . . . 133 7.2.1

Primitive Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . 134

7.2.2

Projecting linearly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

7.2.3

Copying linearly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

7.2.4

Primitive Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . 136

7.2.5

Beyond Primitive Recursion . . . . . . . . . . . . . . . . . . . . . . . . 138

System T Linearly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 7.3.1

Explicit Erasing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

7.3.2

Explicit Copying. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

7.3.3

Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

7.3.4

Simulating System T in System L . . . . . . . . . . . . . . . . . . . . 149

The Role of Closed Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 7.4.1

System LNat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

7.4.2

System LNat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 0

7.4.3

Primitive Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . 156

7.4.4

Beyond Primitive Recursion . . . . . . . . . . . . . . . . . . . . . . . . 157

Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

8 Conclusions 8.1

8.2

8.3

161

Linearisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 8.1.1

Simple Linearisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

8.1.2

Weak Linearisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

8.1.3

Almost linear calculi . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

Future Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 8.2.1

Linearisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

8.2.2

Almost linear calculi . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165

Final Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

CONTENTS

17

A Church-Rosser for ΛL

167

B Types for some primitive recursive functions

173

C Proofs of Chapter 7

177

D Correctness of some System L arithmetic functions

187

References

190

18

CONTENTS

List of Figures 2.1

Diamond property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32

2.2

Reduction-graph for (λx.(λy.xy)(λx.x))(λz.w) . . . . . . . . . . . . . . . . . .

36

2.3

Reduction graph for Ω . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

37

3.1

The notion of T -reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

54

3.2

Simple linearisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

64

4.1

Labeled λ-term . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

72

4.2

Labeled reduction

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

74

4.3

Reduction-graph of the term (λy.(λz.w)y)(λx.x)I . . . . . . . . . . . . . . . .

75

4.4

Reduction-graph of the term ΩI . . . . . . . . . . . . . . . . . . . . . . . . . .

75

4.5

Path hfbdebf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

77

4.6

Well balanced paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

78

4.7

Well balanced paths ϕ1 = d e b l m · l b e · f , and ϕ2 = d e b l m · l b i · j . . . . .

80

4.8

An elementary cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

81

4.9

Some cycles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

82

4.10 Legal path d e b k l . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

84

5.1

Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

99

5.2

One step transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

6.1

Typing rec 0 N (λxy.P x) in System T . . . . . . . . . . . . . . . . . . . . . . 111

20

LIST OF FIGURES

6.2

Typing π1 (iter M hN, 0i λx.(hP (π1 x)(π2 x), S(π2 x)i)) in System T . . . . . . 112

7.1

Typing of fst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

7.2

Typing of C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

7.3

Typing of functions defined by primitive recursion . . . . . . . . . . . . . . . 158

7.4

Typing of Ackermann’s function . . . . . . . . . . . . . . . . . . . . . . . . . 159

8.1

Linearisation, Intersection Types, and Strong Normalisation . . . . . . . . . . 162

A.1 The −→∗1 reduction relation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

Chapter 1

Introduction The λ-calculus, a Turing-complete computational model, has a very simple syntax (variables, function abstraction, and function application), and a unique rewrite rule (β-reduction), based on the notion of substitution. Although the definition of substitution in the λ-calculus is simple, it involves copying of arguments on function evaluation, which can lead to very complex reductions. Consider, for example, the function f x = (x x) x The application f f leads to the following: f f = (f f )f = ((f f )f )f = (((f f )f )f )f = · · · Efficient implementations of β-reduction, try to minimise the number of copying. The purpose is to duplicate as less work as possible. Theoretically, this notion of doing as less duplication as possible, was formalised by Jean Jacques L´evy (L´evy, 1978), and corresponds to optimal reductions in the λ-calculus. A linear function is a function where the formal parameter occurs exactly once in its body. Linear functions do not copy arguments, therefore sharing is not an issue. By L´evy’s definition, all the reductions in the linear λ-calculus, are optimal. However, since it is not possible to duplicate arguments, all the necessary copies must be explicitly in the term before evaluation. Two immediate consequences are: it is difficult to code programs in a linear calculus, and even simple programs may need very big linear terms to represent them. In 2000, Assaf Kfoury published a paper entitled “A Linearisation of the Lambda-Calculus and consequences” (Kfoury, 2000). In this paper Kfoury embedded the λ-calculus into a

22

Chapter 1. Introduction

new calculus, denoted Λ∧ , with a new notion of reduction, denoted β ∧ , and satisfying the following linearity condition on function evaluation: “ If the formal parameter x of an abstraction (λx.N ), is not dummy, then the free occurrences of x in the body N of the abstraction are in a one-one correspondence with the arguments to which the function is applied.” Two questions were made regarding linearity: 1. Can the standard λ-calculus be simulated by this non-standard λ-calculus, with a linearity condition on function evaluation? 2. What are the advantages of compiling the standard λ-calculus into this linear calculus, if such compilation is possible? Kfoury defined a notion of contraction of a term in the new calculus, which gives a λ-term. Well-formed terms of the new calculus are those for which there is a contracted term in the λ-calculus. Kfoury proved that terms in the λ-calculus that are contractions of terms in Λ∧ are strongly normalisable, and conjectured that for any strongly normalisable λ-term M , there exists a term in Λ∧ , which contraction is M . In 2004, in a paper entitled “Linearisation of the lambda-calculus and its relation with intersection type systems” (Florido and Damas, 2004), Florido and Damas presented a notion of expansion of terms in the λ-calculus into linear terms. One of the implications of the expansion relation is that, any term typable using intersection types (Coppo and DezaniCiancaglini, 1980), can be expanded to a term typable using the Simple type system (Curry, 1934). Expansion is preserved by weak-head reduction (a notion of reduction used in functional programming languages implementation). This expansion is defined using the typing of a term in the full intersection type system, and therefore it is undecidable. The authors conclude by saying: “Despite not being decidable, expansion can be used as the basis for new program transformation techniques for linearisation. Our work suggests that these transformations should be related to decidable restrictions of intersection types” The aim of this thesis is to further provide answers to the questions regarding linearity posted above. In particular: • To what extent can one define algorithms that allow us to approximate computations on a Turing-complete computational model, such as the λ-calculus, using linear calculi? • To what extent can one extend the linear λ-calculus, keeping its good computational properties, and how expressive are those extensions?

1.1. Lambda Calculus

23

• What type of term information must be used in the definition of linearisation algorithms? To reach the goals describe above we will use techniques from work on type theory, legal paths, rewrite systems, and λ-calculus, among others.

1.1

Lambda Calculus

We now briefly discuss the λ-calculus, and motivate its use in this thesis. The λ-calculus was originally defined by Church (Church, 1933) as a theory, based on the notion of function, which was part of a more general foundational theory for mathematics. Although the full system proved to be inconsistent (Kleene and Rosser, 1935), Church isolated the subsystem dealing only with functions (Church, 1936), which was proved to be consistent (Church and Rosser, 1936), and turnout to be a model for computable functions. This system is the λ-calculus as we known it today. Around the same time that Church and Kleene used the λ-calculus to define the notion of computability, Turing defined an alternative notion using Turing Machines (Turing, 1937b), and proved the equivalence of the two models (Turing, 1937a). While Turing Machines were used basically for the study of computability matters, the λcalculus is today the basis of functional programming languages such as Haskell, ML and LISP (see (Barendregt, 1997), for more on the impact of the λ-calculus in logic and computer science). The λ-calculus models the definition and application of functions, based on the notion of substitution. In the λ-calculus, a term of the form (M N ) is called an application and represents applying the function represented by the term M to the argument represented by N . A λ-term of the form (λx.M ), is called abstraction, and represents a function f such that f (x) = M , that is, the function that assigns to x the value M . Applying the function f to an argument N results in f (N ). The λ-calculus has been widely used in computer science. For example, in design, implementation, and also in the definition of semantic models for programming languages. As stated by Hindley and Seldin in (Hindley and Seldin, 1986): “λ-calculus and combinatory logic are regarded as ’test-beds’ in the study of higher-order programming languages: techniques are tried out on these two simple languages, developed, and then applied to other more ’practical’ languages.” We will present some basic notions, properties and subsets of the λ-calculus, which will be

24

Chapter 1. Introduction

useful for this thesis, in chapter 2.

1.2

Linear Calculi

Before discussing the contributions of this thesis, we will give some motivation to linear calculi. Linear terms have some nice computational properties: there are no infinite reduction sequences starting from linear terms, there is no duplication of terms upon function evaluation, and every linear term is typable in the simple type system (Hindley, 1989), thus it is typable in polynomial time. Regarding implementation issues of linear programs, both inline expansion and updating of structures in place, can be done safely. However, since no duplication and erasing is allowed, reduction in done in time linear to the size of the term, therefore the linear λ calculus has limited computational power (Hindley, 1989). Also, as mentioned before, representation of functions as linear terms (when possible) can be very space consuming. Nevertheless there exist several works that explore linearity. Many of these works derived from Girard’s Linear Logic (Girard, 1987) (a logic where hypothesis are looked as resources, which are consumed by proofs, contrary to classical logic where hypothesis can be used as many times as needed). In linear logic, copying of hypothesis can be done explicitly using the exponential operator (!). Based on the linear logic there have been defined several calculi for capturing specific complexity classes (Asperti, 1998; Girard, 1998; Girard et al., 1992; Asperti and Roversi, 2002; Lafont, 2004; Terui, 2001; Baillot and Mogbil, 2004). There is also previous work that uses linear types to characterise computations with time bounds (Hofmann, 1999). Also based on the linear logic, there is the area of linear functional programming (see for instance (Abramsky, 1993; Mackie, 1994; Holmstr¨ om, 1988)). These languages are based on a version of the λ-calculus with a type system corresponding to intuitionistic linear logic. One of the main features of the calculus is that it provides explicit syntactical constructs for copying and erasing terms (corresponding to the exponentials in linear logic).

1.3

Linearisation

We now discuss how we address approximating the λ-calculus by more linear calculi . We start by seeing what we mean by simulating a general term by a linear term, thus what we mean by linearisation of a λ-term.

1.4. Almost Linear Calculi

25

Consider the following function application (λz.zz)(λw.w). The argument z of the applied function, occurs twice in the body of the function zz. We can linearise (λz.zz) to get the term (λz1 z2 .z1 z2 ), but since we replaced a single argument function by a function with two arguments, we must provide two copies of the argument λw.w, thus obtaining the linear term: (λz1 z2 .z1 z2 )(λw.w)(λw.w). Now consider the following term (λxy.xy)(λz.zz)(λw.w). The only non-linear abstraction is λz.zz, which will have y as an argument after one step of function evaluation. Therefore, linearising the abstraction and making the necessary copies of the argument y, will result in the term: (λxy.xyy)(λz1 z2 .z1 z2 )(λw.w). Now the variable y, which occurred once in the initial term, occurs twice in the expanded term, and will have λw.w as argument after one step of function evaluation. Applying the same technique as before will finally result in the linear term : (λxy1 y2 .xy1 y2 )(λz1 z2 .z1 z2 )(λw.w)(λw.w). Notice that, in the first example the non-linear abstraction was near its argument, whereas in the second example we needed some look-ahead to decide which terms would be the arguments of linearised abstractions. In this thesis, this two different perspectives will result in two different contributions: 1. In Chapter 3 we define the notion of simple linearisation, which only expands nonlinear applications on the term, that is, terms of the form (λx.M )N where x occurs more than once in M (after a step of transformation, which makes implicit applications on the term explicit). An interesting implication of this transformation is that it is possible to transform any term typable with a simple type in the Rank 2 Intersection Type System (van Bakel, 1993; Jim, 1995), into a new term typable in the Simple Types System with the same type. 2. In Chapter 5 we define a restricted class of λ-terms, which we call weak affine λcalculus, with the good computational properties of the linear λ-calculus. We define a transformation from general λ-terms into terms in this restricted class. This notion of linearisation, which we call weak linearisation, makes use of term information to identify non-linear applications on the term that will be created by function evaluation.

1.4

Almost Linear Calculi

The transformations discussed in the previous section allow us to transform terms in the λ-calculus into terms in more linear calculi. In this thesis, we also address linearity from the

26

Chapter 1. Introduction

point of view of extending the linear λ-calculus, by “almost linear calculi”, that is, extensions of the linear λ-calculus, where duplication is only allowed in a controlled way. With this perspective in mind, we make the following contributions: • In Chapter 6 we define a typed linear calculus with natural numbers, booleans and an iterator (which only allows iteration of linear functions), which we call System L; • In Chapter 7 we show that we can define the whole class of primitive recursive functions (see (Phillips, 1992) for a detailed reference) in System L, and more general functions such as Ackermann’s function; • In the same chapter, we also demonstrate that System L has the same computational power as G¨ odel’s System T (G¨odel, 1958) (a very simple system, but with enormous expressive power). Notice that, if we take the λ-calculus and add it numbers, and a corresponding iterator, duplication becomes redundant in some cases. Consider, for example, the Church’s notation for numbers: n = λf x. f (· · · f (x) · · · ). | {z } n

We can represent Church’s numerals using iterators in the following way: n = λf x.iter n x f which leads to a more compact representation, for most of the numerals. With our study of “almost linear calculi”, we aim to discover to what extend can duplication in the λ-calculus be simulated in a linear system, with one specific mechanism of duplication (in this case an iterator). There are previous definitions of linear systems with iterators, which proved to be strictly less powerful that System T (Lago, 2005; Hofmann, 1999). Our study of the computational power of System L, will also allow us to identify some design choices, which affect the expressive power of linear systems: System L has a more permissive type system, and uses a closed evaluation strategy (Fern´ andez and Mackie, 1999; Fern´ andez et al., 2005).

1.5

Outline

The rest of the thesis is organised in the following way:

1.5. Outline

27

Chapter 2: Background In this chapter we present some notions on reduction systems, and the untyped λ-calculus, as a rewrite system with a notion of reduction. We also present three type systems: the Curry Simple Type System, the Coppo-Dezani Intersection Type System, and the G¨ odel System T Type System.

Chapter 3: Simple Linearisation Here we present a term transformation function, from terms typable in the rank 2 ITS with a simple type τ , to terms typable in the Simple type system with the same type.

Chapter 4: Paths and Labels In this chapter we give a general overview of the labeled λ-calculus and of legal paths, which will be used in the following chapter.

Chapter 5: Weak Linearisation Here we identify a restricted class of terms of the λ-calculus, called weak affine, that includes the linear λ-terms keeping their good properties. We present a transformation from general terms into weak affine terms, and prove its correctness by showing that reduction preserves normal forms.

Chapter 6: Linear Lambda Calculus with Iterators In this chapter we present a simple extension to the linear λ-calculus, with booleans, natural numbers, and a restricted iterator (only closed linear functions can be iterated), and present a type system for this calculus (we call this system System L). We prove System L’s main properties: subject reduction, strong normalisation and confluence.

Chapter 7: Expressiveness of System L In this chapter we study the computational power of System L, which we defined in the previous chapter. In particular we compare System L with G¨odel’s System T , and with previous extensions of linear calculus, with the same computational structures.

28

Chapter 1. Introduction

Chapter 8: Conclusions In this final chapter, we outline some future work, and make some final considerations. The original work presented in this thesis was partially exposed in (Alves and Florido, 2002; Alves and Florido, 2004; Alves and Florido, 2005; Alves et al., 2006b; Alves et al., 2006a).

Chapter 2

Background 2.1

Introduction

In this chapter we give a general overview of background necessary to follow this thesis. We present the λ-calculus and several standard type systems for the λ-calculus. As we mentioned in the introduction, the (type-free) λ-calculus was defined by Church as a model for computable functions, which was proved to be computationally equivalent to Turing Machines. In (Church, 1940), Church presented a typed version of the λ-calculus. Before that, in (Curry, 1934), Curry presented types in a different way in the context of combinatory logic. In both systems, types are syntactic objects that may be assigned to λ-terms, to represent their functional character. If M is a term with type α → β, them M represents a function from α to β. The difference between the two approaches is that in the Church system terms are explicitly annotated with their types, whereas in the Curry system, terms are untyped objects, and the rules of the system are used to assign types to the terms. This two different notions correspond to two different type paradigms in programming languages. The Church system models explicitly typed languages where programs are written with their corresponding types, and compilers check if the types are well assigned. Examples of these languages are C and Java. The other system corresponds to implicitly typed languages where programs may be written with no types, and compilers check if a type can be assigned to the program. ML and Haskell are examples of such languages. The Curry approach is the approach we will follow in this thesis.

30

Chapter 2. Background

In the following sections we will present some notions on reduction systems, and the untyped λ-calculus, as a rewrite system with a notion of reduction. We also present three type systems used in the rest of the thesis: • The Curry Simple Type System; • The Coppo-Dezani Intersection Type System; • The G¨ odel System T Type System.

2.2

Reduction Systems

In this section we present basic notions on reduction systems. For a more detailed study see (Klop, 1992; Dershowitz and Jouannaud, 1990). Definition 2.2.1 A reduction system is a pair hA, →R i, where A is a set of terms and R is a relation on A. We call R a notion of reduction. A notion of reduction can be introduced as a set of contraction rules: R : M → N if · · · This corresponds to the following relation on A: R = {(M, N ) | · · · } For the remainder of this thesis we will present notions of reduction as contraction rules. Definition 2.2.2 Let A be a set of terms. A context (denoted by C[ ]) is a ’term’ containing one or more occurrences of [ ], denoting holes, and such that if M ∈ A, then replacing the holes in C[ ] by M , is the term C[M ] ∈ A. A relation R is compatible if it can be lifted upon contexts. Definition 2.2.3

1. A binary relation R on a set of terms A is compatible if (M, N ) ∈ R ⇒ (C[M ], C[N ]) ∈ R

for all M, N ∈ A and all contexts C[ ] with one hole. 2. A compatible, reflexive and transitive relation on A is called a reduction relation on A.

2.2. Reduction Systems

31

Definition 2.2.4 Let R be a notion of reduction on A. Then R induces the following binary relations on A: • The one step R-reduction denoted by →R . The →R relation is the compatible closure of R, and is inductively defined as follows: (M, N ) ∈ R ⇒ M →R N M →R N ⇒ C[M ] →R C[N ] • The R-reduction denoted by −→∗R . The −→∗R relation is the reflexive, transitive closure of →R , and is inductively defined as follows: M →R N M −→∗R M M −→∗R N, N −→∗R P

⇒ M −→∗R N ⇒ M −→∗R P

The relation →R is, by definition, a compatible relation. The relation −→∗R is the reflexive transitive closure of →R and therefore a reduction relation. We will sometimes omit R, when it is clear from the context which notion of reduction R represents. Definition 2.2.5 Let hA, Ri be a notion of reduction. • A term M in A is called an R-redex, if (M, N ) ∈ R for some N in A. The term N is called an R-contractum of M . • A term M is said to be in R-normal form (R-nf) if M does not contain (as a subterm) any R-redex. • A term M has a (R-nf), if M R-reduces to N , and N is a (R-nf). • We write NFR to denote the set of terms in R-normal form. We now discuss the property of confluence, which will be required in the reduction systems we will consider. Definition 2.2.6 Let R be a notion of reduction. • R satisfies the diamond property (see Figure 2.1) if: ∀M, N1 , N2 .(M R N1 ∧ M R N2 ⇒ ∃N.(N1 R N ∧ N2 R N ))

32

Chapter 2. Background

M R

>




R


· · · > aj > · · · Definition 3.4.4 Let Z∗ be the set of non-negative integers. A multiset M over a set A is a function M : A → Z∗ . Let M (x) be the number of copies of x ∈ A in M . We use the standard set notation (e.g. {a, a, b} for the function {a → 2, b → 1, c → 0}) and: Element: Inclusion: Union: Difference:

x ∈ M ⇔ M (x) > 0 M ⊆ N ⇔ ∀x ∈ A.M (x) ≤ N (x) (M ∪ N )(x) = M (x) + N (x) ˙ N (x) ˙ n = max(0, m − n) (M − N )(x) = M (x) − where m −

The following relation was defined in (Baader and Nipkow, 1998). Definition 3.4.5 Let > be the usual order on the non-negative integers Z∗ . Let >mul be the relation defined on M(Λ) as: M >mul N

iff ∃X, Y ∈ M(Λ) such that ∅ 6= X ⊆ M and N = (M − X) ∪ Y and ∀y ∈ Y.∃x ∈ X.x > y.

In (Baader and Nipkow, 1998) it was proved that >mul is well-founded if and only if > is.

3.4.2

Confluence and normalisation for T -reduction

Definition 3.4.6 For any term M , let depthL (M ) be the left depth of the term, defined in the following way: depthL (x) = 0 depthL (λx.N ) = depthL (N ) + 1 depthL (P Q) = depthL (P ) + 1 Definition 3.4.7 For any term M , the multiset depapp(M ) is defined in the following way: depapp(x) = ∅ depapp(λx.M ) = depapp(M ) depapp(P Q) = depapp(P ) ∪ depapp(Q) ∪ {depthL (P Q)}

56

Chapter 3. Simple Linearisation

The multiset depapp(M ) contains the left depth of all the application subterms in M . Notation 3.4.8 For the remainder of this thesis we will use (I.H.) to denote “by induction hypothesis”. Lemma 3.4.9 If M →T M ′ then 1. depthL (M ) = depthL (M ′ ); 2. depapp(M ) >mul depapp(M ′ ). Proof: By induction on the definition of →T . We have two cases: • (λx.N )OP → (λx.N P )O. In this case: 1. depthL ((λx.N )OP ) = depthL (N ) + 3 = depthL ((λx.N P )O). 2. depapp((λx.N )OP )

=

depapp(N ) ∪ depapp(O) ∪ depapp(P ) ∪{depthL (N ) + 3, depthL (N ) + 2}

(I.H.)

>mul depapp(N ) ∪ depapp(O) ∪ depapp(P ) ∪{depthL (N ) + 3, depthL (N ) + 1} = depapp((λx.N P )O) • C[N ] →T C[N ′ ], because N →T N ′ . By induction hypothesis: 1. depthL (N ) = depthL (N ′ ); 2. depapp(N ) >mul depapp(N ′ ). We prove this case by induction on the context C[ ]. – C[ ] ≡ [ ]. Then: (I.H.)

1. depthL (C[N ]) = depthL (N ) = depthL (N ′ ) = depthL (C[N ′ ]). (I.H.)

2. depapp(C[N ]) = depapp(N ) >mul depapp(N ′ ) = depapp(C[N ′ ]). – C[ ] ≡ λx.C1 [ ]. Then: 1. depthL (C[N ])

=

depthL (λx.C1 [N ]) = depthL (C1 [N ]) = depthL (C1 [N ′ ])

(I.H.)

=

depthL (λx.C1 [N ′ ]) = depthL (C[N ′ ]).

=

depapp(λx.C1 [N ]) = depapp(C1 [N ])

2. depapp(C[N ])

(I.H.) >mul

depapp(C1 [N ′ ]) = depapp(λx.C1 [N ′ ]) = depapp(C[N ′ ]).

3.4. T -Reductions

57

– C[ ] ≡ C1 [ ]P . Then: 1. (I.H.)

depthL (C[N ]) = depthL (C1 [N ]P ) = depthL (C1 [N ]) = depthL (C1 [N ′ ]) = depthL (C1 [N ′ ]P ) = depthL (C[N ′ ]). 2. depapp(C[N ])

= = =

depapp(C1 [N ]P ) depapp(C1 [N ]) ∪ depapp(P ) ∪ {depthL (C1 [N ]) + 1} depapp(C1 [N ]) ∪ depapp(P ) ∪ {depthL (C1 [N ′ ]) + 1}

(I.H.)

>mul depapp(C1 [N ′ ]) ∪ depapp(P ) ∪ {depthL (C1 [N ′ ]) + 1} = depapp(C1 [N ′ ]P ) = depapp(C[N ′ ]). – C[ ] ≡ P C2 [ ]. Then: 1. depthL (C[N ]) = depthL (P C2 [N ]) = depthL (P ) = depthL (P C2 [N ′ ]) = depthL (C[N ′ ]). 2. depapp(C[N ])

= =

depapp(P C2 [N ]) depapp(P ) ∪ depapp(C2 [N ]) ∪ {depthL (P ) + 1}

(I.H.)

>mul depapp(P ) ∪ depapp(C2 [N ′ ]) ∪ {depthL (P ) + 1} = depapp(P C2 [N ′ ]) = depapp(C[N ′ ]). 

Theorem 3.4.10 The reduction system hΛ, T i is strongly normalising.

Proof: By contradiction. Suppose there exists an infinite T -reduction sequence M1 →T M2 →T · · · By Lemma 3.4.9, there exist an infinite descending chain depapp(M1 ) >mul depapp(M2 ) >mul · · · which is absurd, since >mul is well-founded. Therefore hΛ, T i is strongly normalising.

Theorem 3.4.11 The T reduction is Church-Rosser.



58

Chapter 3. Simple Linearisation

Proof: We first prove that T -reduction is weakly confluent. That is: M

∆1 T

>

∆2 T

∨ ∨



O

N

T

>>

T

P

Let ∆1 ≡ (λx.N )OP and let M ≡ C1 [∆1 ]. Let ∆2 ≡ (λy.Q)RS. We now consider the three possible relative positions of the T -redexes ∆1 and ∆2 in M . • ∆1 ≡ ∆2 . Follows trivially. • ∆1 ⊂ ∆2 or ∆2 ⊂ ∆1 . If this is the case then one of the following holds: ∆1 ⊂ Q or ∆1 ⊂ R or ∆1 ⊂ S or ∆2 ⊂ N or ∆2 ⊂ O or ∆2 ⊂ P . All the cases are handled in a similar way, so we will only show the case ∆1 ⊂ Q. Let Q ≡ C2 [∆2 ], and let ∆2 →T ∆′2 . Then: C1 [(λx.C2 [∆2 ])OP ]

∆1 T

>

C1 [(λx.C2 [∆2 ]P )O]

∆2 T

∨ ∨



C1 [(λx.C2 [∆′2 ])OP ]

T

>>

T

C1 [(λx.C2 [∆′2 ]P )O]

• ∆1 6⊂ ∆2 and ∆2 6⊂ ∆1 . Let M ≡ C[∆1 , ∆2 ] where C[ , ] is a context with two holes. Let ∆1 →T ∆′1 , and ∆2 →T ∆′2 . Then: C[∆1 , ∆2 ]

∆1 T

∆2 T

C[∆′1 , ∆2 ] T



C[∆1 , ∆′2 ]

>

T

∨ ∨ ′ >> C[∆1 , ∆′2 ]

The result follows noticing that, by Newman’s result (Newman, 1942), strong normalisation and weak confluence of T -reduction, implies confluence of T -reduction.  The first step in the term transformation defined in this chapter, reduces terms to normal form with respect to T -reduction. Definition 3.4.12 If M is a term in Λ, then normalizeT (M ) is the T -nf of M . We now give an inductive characterisation of the λ-terms which are in T -normal form (T -nf). Definition 3.4.13 The set of terms in T normal forms, ΛT is inductively defined as follows:

3.4. T -Reductions

59

1. x ∈ ΛT . 2. λx.M ∈ ΛT , if M ∈ ΛT . 3. xM1 . . . Mk ∈ ΛT , if M1 . . . Mk ∈ ΛT . 4. (λx.M )N ∈ ΛT , if M ∈ ΛT and N ∈ ΛT . Lemma 3.4.14 A λ-term M is in T -normal form if and only if M ∈ ΛT .

Proof: We prove the two directions separately. (⇐) If M ∈ ΛT , then it is easy to prove, by induction on the definition of M , that M does not contain a T -redex, thus M is in T -nf.

(⇒) We now prove by structural induction that, for every λ-term M , if M is in T -nf then M ∈ ΛT . 1. If M = x, it follows by part 1 of the definition of ΛT . 2. If M = λx.N , then since M is in T -nf, N is in T -nf. Then, by induction hypothesis, N ∈ ΛT and by part 2 of the definition of ΛT , λx.M ∈ ΛT . 3. If M = M1 M2 , since M is in T -nf, then M1 and M2 are both in T -nf, thus by induction hypothesis M1 , M2 ∈ ΛT . Then we have three cases: (a) If M1 = x, since M2 ∈ ΛT , it follows from part 3 of the definition of ΛT , xM2 ∈ ΛT . (b) If M1 = λx.N , by part 2 of the definition of ΛT , N ∈ ΛT and since M2 ∈ ΛT , it follows from part 4 of the definition of ΛT , (λx.N )M2 ∈ ΛT (c) If M1 = xN1 . . . Nk , by part 3 of the definition of ΛT , N1 , . . . , Nk ∈ ΛT and since M2 ∈ ΛT , it follows from part 3 of the definition of ΛT , (xN1 . . . Nk )M2 ∈ ΛT Note that M1 cannot be of the form (λx.N )P because otherwise M1 M2 would have a T -redex and thus would not be in T -nf.

 We now prove some useful properties of T -reduction and T -normal forms. Lemma 3.4.15 If Γ ⊢2 M : σ and M →T N , then Γ ⊢2 N : σ.

60

Chapter 3. Simple Linearisation

Proof: The cases where M is not of the form ((λx.P )Q)R are easy by structural induction. We just present the case where M is a term of the form ((λx.P )Q)R and x ∈ fv(P ) (the case where x ∈ / fv(P ) is proved in a similar way). Γ ⊢2 ((λx.P )Q)R : σ if Γ1 ⊢2 (λx.P )Q : τ11 ∩ · · · ∩ τ1n → σ and Γ1i ⊢2 R : τ1i where i ∈ {1, . . . , n}, and Γ = Γ1 ∪ Γ11 ∪ · · · ∪ Γ1n , thus Γ2 ⊢2 λx.P : τ21 ∩ · · · ∩ τ2m → (τ11 ∩ · · · ∩ τ1n → σ) and Γ2j ⊢2 Q : τ2j and Γ1i ⊢2 R : τ1i where i ∈ {1, . . . , n}, j ∈ {1, . . . , m}, and Γ1 = Γ2 ∪ Γ21 ∪ · · · ∪ Γ2m , then Γ2 ∪ {x : τ21 , . . . , x : τ2m } ⊢2 P : τ11 ∩ · · · ∩ τ1n → σ. By the (→ Elim) rule Γ2 ∪ {x : τ21 , . . . , x : τ2m } ⊢2 P R : σ. By the (→ IntroI ) rule Γ2 ∪ Γ11 ∪ · · · ∪ Γ1n ⊢2 λx.P R : τ21 ∩ · · · ∩ τ2k → σ finally applying the (→ Elim) rule again Γ ⊢2 (λx.P R)Q : σ. 

Lemma 3.4.16 Let M N be a term in ΛT , of the form xM1 . . . Mk . If M N is typable in rank 2, then both M and N have simple types and the last rule used in its type derivation is of the form Γ1 ⊢2 M : τ ′ → τ Γ2 ⊢2 N : τ ′ Γ1 ∪ Γ2 ⊢2 M N : τ Proof: By induction on k. 1. For k = 1, M N = xM1 . Then we have the following derivation {x : τ ′ → τ } ⊢2 x : τ ′ → τ

Γ ⊢ 2 M1 : τ ′

{x : τ ′ → τ } ∪ Γ ⊢2 xM1 : τ For xM1 to be typable, x must be of functional type τ ′ → τ .

3.5. The IT Type System

61

2. For k > 1, we have Γ0 ⊢2 xM1 . . . Mk−1 : τ1 ∩ · · · ∩ τn → σ

Γ i ⊢ 2 Mk : τi

Γ0 ∪ Γ1 ∪ · · · ∪ Γn ⊢2 xM1 . . . Mk : σ by the induction hypothesis τ1 ∩ · · · ∩ τn → σ must be a simple type. Thus, the derivation for xM1 . . . Mk becomes Γ0 ⊢2 xM1 . . . Mk−1 : τ ′ → τ

Γ 1 ⊢ 2 Mk : τ ′

Γ0 ∪ Γ1 ⊢2 xM1 . . . Mk : τ 

3.5

The IT Type System

In this section, we define the IT Type System, and present some properties of the system. This type system is going to be used solely in the proof of our main result, and not in the transformation itself. The set of types is the same defined for the Rank 2 Type System. The IT Type System differs from Rank 2 by typing an application only when the argument has a simple type. The definition of basis for the IT System is the same of rank 2 basis. Definition 3.5.1 In the IT Type System, we say that M has type σ given the basis Γ, and write Γ ⊢IT M : σ, if Γ ⊢IT M : σ can be obtained from the following derivation rules: (Axiom)

(→ IntroI )

(→ IntroK )

(→ Elim)

{x : τ } ⊢IT x : τ Γ ∪ {x : τ1 , . . . , x : τn } ⊢IT M : σ Γ ⊢IT λx.M : (τ1 ∩ · · · ∩ τn ) → σ

(a)

Γ ⊢IT M : σ Γ ⊢IT λx.M : τ → σ Γ1 ⊢IT M : τ → σ

if x ∈ fv(M )

if x ∈ / fv(M )

Γ2 ⊢IT N : τ

Γ1 ∪ Γ2 ⊢IT M N : σ

(a) And x : σ1 , . . . , x : σn are all and nothing but statements about x on which Γ ∪ {x : σ1 , . . . , x : σn } ⊢IT M : σ depends. In IT intersections only appear in the (→ IntroI ) rule, thus, in type derivations, intersection types can only appear in the types of abstractions which are not applied.

62

Chapter 3. Simple Linearisation

We now present some properties of the IT type system. Lemma 3.5.2 If Γ ∪ {x : τ1 , . . . , x : τk } ⊢IT M : σ, then x occurs n times in M , with n ≥ k. Proof: By structural induction. The cases where M is a variable or an abstraction are easy by structural induction. We just present the case where M is an application M1 M2 . Γ1 ∪ Γ2 ∪ {x : τ1 , . . . , x : τk } ⊢IT M1 M2 : σ then Γ1 ∪ {x : τ11 , . . . , x : τ1k1 } ⊢IT Γ2 ∪ {x : τ21 , . . . , x : τ2k2 } ⊢IT

M1 : τ → σ M2 : τ

with {x : τ1 , . . . , x : τk } = {x : τ11 , . . . , x : τ1k1 } ∪ {x : τ21 , . . . , x : τ2k2 }. Let n1 , n2 respectively be the number of occurrences of x in M1 , M2 then, by the induction hypothesis we have k1 ≤ n1 , k2 ≤ n2 . As ∪ is set union k ≤ k1 + k2 . Then n, the number of occurrences of x in M1 M2 , satisfies n = n1 + n2 ≥ k1 + k2 ≥ k.  Note that this last property does not hold for rank 2 ITS. For example {y : α → β, y : α} ⊢2 (λx.xx)y : β, y occurs free in (λx.xx)y once, and it has two type declarations in the basis. Lemma 3.5.3 If Γ ⊢IT M : σ, then dom(Γ) = fv(M ). Proof: Easy by structural induction.



Lemma 3.5.4 Let M be a λ-term and x a variable which occurs free in M , n times. Suppose that, for some Γ and σ there is a derivation of Γ ∪ {x : τ1 } ∪ · · · ∪ {x : τn } ⊢IT M : σ where {x : τi } ⊢IT x : τi (i = 1, . . . , n), are the steps in that derivation pertaining to each distinguished occurrence of x in M . (Note that τ1 . . . τn are not necessarily distinct.) Let xi be a fresh variable replacing the ith occurrence of x in M (i = 1, . . . , n), and M ⋆ be the result of the n replacements. Then Γ ∪ {x1 : τ1 } ∪ · · · ∪ {xn : τn } ⊢IT M ⋆ : σ.

3.5. The IT Type System

63

Proof: By structural induction on M : 1. If M = x then {x : τ } ⊢IT x : τ , x⋆ = x1 and {x1 : τ } ⊢IT x1 : τ . 2. If M = λy.N we consider two cases: (a) If y ∈ fv(N ) then Γ ∪ {x : τ1 } ∪ · · · ∪ {x : τn } ⊢IT λy.N : τ11 ∩ · · · ∩ τ1k → σ if Γ ∪ {x : τ1 } ∪ · · · ∪ {x : τn } ∪ {y : τ11 , . . . , y : τ1k } ⊢IT N : σ. By the induction hypothesis Γ ∪ {x1 : τ1 } ∪ · · · ∪ {xn : τn } ∪ {y : τ11 , . . . , y : τ1k } ⊢IT N ⋆ : σ and by (→ IntroI ): Γ ∪ {x1 : τ1 } ∪ · · · ∪ {xn : τn } ⊢IT λy.N ⋆ : τ11 ∩ · · · ∩ τ1k → σ. (b) The case where y ∈ / fv(N ) is proved in a similar way, using (→ Introk ). 3. If M = M1 M2 then Γ ∪ {x : τ1 } ∪ · · · ∪ {x : τn } ⊢IT M1 M2 : σ and for k ∈ {0, . . . , n}, the first k occurrences of x are in M1 thus Γ1 ∪ {x : τ1 } ∪ · · · ∪ {x : τk } ⊢IT Γ2 ∪ {x : τk+1 } ∪ · · · ∪ {x : τn } ⊢IT

M1 : τ → σ M2 : τ

with Γ = Γ1 ∪ Γ2 . By the induction hypothesis Γ1 ∪ {x1 : τ1 } ∪ · · · ∪ {xk : τk } ⊢IT Γ2 ∪ {xk+1 : τk+1 } ∪ · · · ∪ {xn : τn } ⊢IT

M1⋆ : τ → σ M2⋆ : τ

and by (→ Elim): Γ ∪ {x1 : τ1 } ∪ · · · ∪ {xn : τn } ⊢IT M1⋆ M2⋆ : σ. 

Lemma 3.5.5 Let Γ ⊢IT M : σ and M ⋆ be the result of replacing each occurrence of every free variable x in M by a new variable xi . Then, there is a monovalent basis Γ′ such that Γ′ ⊢IT M ⋆ : σ.

64

Chapter 3. Simple Linearisation

Proof: For every free variable in M , apply lemma 3.5.4. Note that each application of lemma 3.5.4 produces a monovalent subset of the new basis. By lemma 3.5.3 the term variables in the basis are exactly the free variables of M . Thus, when every free variable of M is replaced, the final basis is monovalent. 

Example 3.5.6 For the term M = (λx1 x2 .x1 x2 )zz we have {z : α → β, z : α} ⊢IT (λx1 x2 .x1 x2 )zz : β and for the term (λx1 x2 .x1 x2 )z1 z2 we have {z1 : α → β, z2 : α} ⊢IT (λx1 x2 .x1 x2 )z1 z2 : β.

3.6

Simple Linearisation

In this section we present the translation from terms typable in the Rank 2 ITS with a Simple type, into terms typable in the Simple Type System with the same type. In Figure 3.2 we show an overview of the transformation. M ∨

Γ ⊢2 M : τ

normalizeT Γ ⊢ 2 M1 : τ

M1 ∨

expand

M2 ∨

Γ ⊢IT M2 : τ

rename free vars

M′

Γ′ ⊢IT M ′ : τ Figure 3.2: Simple linearisation

Definition 3.6.1 Let M be a term in Λ of the form (λx.P )Q with x ∈ fv(P ), and let k be the number of free occurrences of x in P . Then L((λx.P )Q) = (λx1 . . . xk .P ⋆ ) Q . . . Q | {z } k times

where P ⋆ results from P by replacing the ith occurrence of x in P by a fresh variable xi , for i = 1, . . . , n.

3.6. Simple Linearisation

65

Definition 3.6.2 Let M be a way: expand(x) expand(λx.M ) expand((λx.M )N ) expand(M N )

term in T -nf, the term expand(M ) is defined in the following = = = =

x λx.expand(M ) L((λx.expand(M ))expand(N )) if x ∈ fv(M ) expand(M )expand(N ) otherwise

Example 3.6.3 The term (λz.(xz(yz))(λxyz.xz(yz)) expands to (λz1 z2 .(xz1 (yz2 )))(λxyz.xz(yz))(λxyz.xz(yz)). Note that in (λz.(xz(yz))(λxyz.xz(yz)) the variable z occurs free in (xz(yz)) twice, thus the term (λxyz.xz(yz)) is duplicated. Definition 3.6.4 If M is a λ-term, then rename free vars, is a function that replaces each occurrence of a free variable in M by a fresh variable. Example 3.6.5 For the term λy.zyzxy, rename free vars(λy.zyzxy) = λy.z1 yz2 x1 y. Definition 3.6.6 We now define the main transformation, T (M ) as T = rename free vars ◦ expand ◦ normalizeT where ◦ stands for function composition. Example 3.6.7 Consider the term M ≡ (λz.(wz(yz))(λxyz.xz(yz)). Since M is already in T normal form, then normalizeT (M ) = (λz.(wz(yz))(λxyz.xz(yz)). Applying expand we get expand(normalizeT (M )) = (λz1 z2 .(wz1 (yz2 )))(λxyz.xz(yz))(λxyz.xz(yz)). Finally renaming the only free variable w, we obtain rename free vars(expand(normalizeT (M )) = (λz1 z2 .(w1 z1 (yz2 )))(λxyz.xz(yz))(λxyz.xz(yz)). Therefore rename free vars(expand(normalizeT (M )) = (λz1 z2 .(w1 z1 (yz2 )))(λxyz.xz(yz))(λxyz.xz(yz)).

66

Chapter 3. Simple Linearisation

3.6.1

Correctness of T

Lemma 3.6.8 Let M be a term in ΛT . If Γ ⊢2 M : σ, then Γ ⊢IT expand(M ) : σ. Proof: By structural induction on M in T -nf. By lemma 3.4.14 we consider four cases: 1. If M = x, then {x : τ } ⊢2 x : τ , expand(x) = x, and {x : τ } ⊢C x : τ . 2. If M = λx.N , expand(λx.N ) = λx.expand(N ) then: (a) If x ∈ fv(N ), then Γ ⊢2 λx.N : τ1 ∩ · · · ∩ τn → σ if Γ ∪ {x : τ1 , . . . , x : τn } ⊢2 N : σ. By induction hypothesis Γ ∪ {x : τ1 , . . . , x : τn } ⊢IT expand(N ) : σ thus by (→ IntroI ) Γ ⊢IT λx.expand(N ) : τ1 ∩ · · · ∩ τn → σ. (b) If x ∈ / fv(N ) the proof is similar, using (→ Introk ). 3. If M = (λx.N )P , we consider two cases: (a) If x ∈ fv(N ), then Γ ⊢2 (λx.P )Q : σ if Γ0 ∪ {x : τ1 , . . . , x : τk } ⊢2 P : σ and Γi ⊢2 Q : τi with Γ = Γ0 ∪ Γ1 ∪ · · · ∪ Γk . Then by the induction hypothesis Γ0 ∪ {x : τ1 , . . . , x : τk } ⊢IT Γi ⊢IT

expand(P ) : σ expand(Q) : τi .

But x occurs in expand(P ) n times and, by lemma 3.5.2, n ≥ k. Each occurrence of x in expand(N ) has one type τi (i ∈ {1, . . . , k}) in the derivation of Γ0 ∪ {x : τ1 , . . . , x : τk } ⊢IT expand(N ) : σ. Thus if we replace the ith occurrence of x by a fresh variable xi , and add the respective declarations to the basis we have Γ0 ∪ {x1 : τi1 , . . . , xn : τin } ⊢IT expand(N )⋆ : σ with τij ∈ {τ1 , . . . , τk } for j ∈ {1, . . . , n} and n ≥ k. applications of the (→ IntroI ) rule

Thus by successive

Γ0 ⊢IT λx1 . . . xn .expand(P )⋆ : τi1 → · · · → τin → σ and by successive applications of the (→ Elim) rule and noting that

3.6. Simple Linearisation

67

• Γi ⊢IT expand(Q) : τi for i ∈ {1, . . . , n}, • τij ∈ {τ1 , . . . , τk } for j ∈ {1, . . . , n}, thus Γij ⊢IT expand(Q) : τij with Γij ∈ {Γ1 , . . . , Γk } for j ∈ {1, . . . , n}, • Γ = Γ0 ∪ Γi1 ∪ · · · ∪ Γin , we have: Γ ⊢IT (λx1 . . . xn .expand(P )⋆ ) expand(Q) . . . expand(Q) : σ. | {z } n times

(b) If x ∈ / fv(N ), then Γ ⊢2 (λx.P )Q : σ if Γ1 ⊢2 P : σ and Γ2 ⊢2 Q : τ with Γ = Γ1 ∪ Γ2 . Then by the induction hypothesis Γ1 ⊢IT Γ2 ⊢IT

expand(P ) : σ expand(Q) : τ

thus by the (→ IntroK ) rule Γ1 ⊢IT λx.expand(P ) : τ → σ and by (→ Elim) Γ ⊢IT (λx.expand(P ))expand(Q) : σ. 4. Finally for M = xM1 . . . Mk , expand(M ) = expand(xM1 . . . Mk−1 )expand(Mk ). By lemma 3.4.16 we have the following type derivation for M Γ1 ⊢2 xM1 . . . Mk−1 : τ ′ → τ

Γ 2 ⊢ 2 Mk : τ ′

Γ1 ∪ Γ2 ⊢2 xM1 . . . Mk : τ with τ ′ → τ ∈ TC . By induction hypothesis we have Γ1 ⊢IT Γ2 ⊢IT

expand(xM1 . . . Mk−1 ) : τ ′ → τ expand(Mk ) : τ ′

thus, by the (→ Elim) rule Γ1 ∪ Γ2 ⊢IT expand(xM1 . . . Mk ) : τ. 

Example 3.6.9 For the term M = (λxy.x)((λx.xx)z) we have {z : α → β, z : α} ⊢2 (λxy.x)((λx.xx)z) : δ → β and for the term expand(M ) = (λxy.x)((λx1 x2 .x1 x2 )zz) we have {z : α → β, z : α} ⊢IT (λxy.x)((λx1 x2 .x1 x2 )zz) : δ → β.

68

Chapter 3. Simple Linearisation

Lemma 3.6.10 If Γ ⊢IT M : τ and Γ is monovalent then Γ ⊢C M : τ . Proof: By structural induction on M . 1. If M = x then {x : τ } ⊢IT x : τ , {x : τ } is monovalent and {x : τ } ⊢C x : τ . 2. If M = λx.N then we have two cases: (a) If x ∈ fv(N ), then Γ ⊢IT λx.N : τ ′ → τ if Γ ∪ {x : τ ′ } ⊢IT N : τ and Γ∪{x : τ ′ } is monovalent because x ∈ / fv(λx.N ) so by lemma 3.5.3 x ∈ / dom(Γ). Then by induction hypothesis Γ ∪ {x : τ ′ } ⊢C N : τ thus by (→ IntroI ) Γ ⊢C λx.N : τ ′ → τ. (b) If x ∈ / fv(N ) the proof is similar using (→ IntroK ). 3. If M = M1 M2 , then Γ ⊢IT M1 M2 : τ if Γ1 ⊢IT M1 : τ ′ → τ and Γ2 ⊢IT M2 : τ ′ with Γ = Γ1 ∪Γ2 is monovalent so both Γ1 , Γ2 are monovalent. By induction hypothesis Γ 1 ⊢ C M1 : τ ′ → τ Γ 2 ⊢ C M2 : τ ′ thus Γ1 ∪ Γ2 ⊢C M1 M2 : τ. 

Theorem 3.6.11 If Γ ⊢2 M : τ and τ ∈ TC , then ∃Γ′ .Γ′ ⊢C T (M ) : τ . Proof: If Γ ⊢2 M : τ, with τ ∈ TC by lemma 3.4.15 and the fact that T is strongly normalisable Γ ⊢2 normalizeT (M ) : τ.

3.6. Simple Linearisation

69

Then, by lemma 3.6.8 Γ ⊢IT expand(normalizeT (M )) : τ and by lemma 3.5.5, there is a monovalent basis Γ′ such that Γ′ ⊢IT rename free vars(expand(normalizeT (M ))) : τ. Finally, by lemma 3.6.10 Γ′ ⊢C rename free vars(expand(normalizeT (M ))) : τ that is Γ′ ⊢C T (M ) : τ.  Note that, if the type σ in Γ ⊢2 M : σ is not a simple type, the previous result does not hold. As an example consider the term M = λx.xx: T (M ) = λx.xx and λx.xx is not typable in the Simple Type system. Example 3.6.12 For the term in T -nf M = (λxy.x)((λx.xx)z) we have {z : α → β, z : α} ⊢2 (λxy.x)((λx.xx)z) : δ → β expanding the term we get (λxy.x)((λx1 x2 .x1 x2 )zz) after applying the function rename free vars the resulting term is: (λxy.x)((λx1 x2 .x1 x2 )z1 z2 ). Note that there is a basis for which this term has the same type in the Simple type system: {z1 : α → β, z1 : α} ⊢C (λxy.x)((λx1 x2 .x1 x2 )z1 z2 ) : δ → β. Example 3.6.13 Let S = (λxyz.xz(yz)), K = (λxy.x) and I = (λx.x) in M = SKSI. M is typable in the rank 2 ITS. One typing of M is: ⊢2 M : α → α. Now we have normalizeT (M ) = λx.(λy.(λz.(xz(yz))I)S)K and expand(normalizeT (M )) = λx.(λy.(λz1 z2 .(xz1 (yz2 ))II)S)K. Note that this term has the same type in the Simple type system: ⊢C λx.(λy.(λz1 z2 .(xz1 (yz2 ))II)S)K : α → α.

70

3.7

Chapter 3. Simple Linearisation

Discussion

In this chapter we defined a transformation from terms, typable in rank 2 intersection type system with a simple type, to terms typable in Curry type system. Our transformation is independent of type information, and we prove that it preserves simple types. Type inference for finite rank ITS as term transformation followed by type inference in a more simple type system was studied in (Kfoury et al., 1999). In this work the transformation used was to β-reduce the redexes (λx.M )N where x occurs in M (after T transformation that makes all the redexes explicit), iterated j times, with j > k, and k being the rank of the term. The resulting term is then typable in another type system, which is simpler than rank 2. Notice for example that, if (λx.M )N : τ1 ∩ · · · ∩ τn → τ , after one β-reduction of (λx.M )N , we get a term where the occurrences of x in M disappeared and were replaced by copies of N , which must be typable with Simple types. In the transformation described in this chapter, instead of eliminating x by using β-reduction we remove the “problematic” dependencies between different occurrences of x by replacing each occurrence by a fresh variable. Therefore, by replacing λx.M , by a function that is linear on x. For the rank 2 intersection type system, one can easily remove these dependencies between different occurrences of bound variables, because they only occur in redexes already implicitly or explicitly in the term. In general intersection type systems, these dependencies can be caused by redexes that are going to be created by the reduction of the term. Therefore, in order to replace the function on those redexes by a linear function, one must be able to identify “virtual redexes”. In the next Chapter we will discuss the labeled λ-calculus, and paths in labelled λ-terms, which allow us to identify redexes on the term that will be created by β-reduction. These paths will be used in Chapter 5, to define the new notion of “weak linearisation”.

Chapter 4

Paths and Labels 4.1

Introduction

In this chapter we give a general overview of the labeled λ-calculus and legal paths along the lines presented in (Asperti and Laneve, 1995). For a more detailed reference on the labeled λ-calculus and legal paths see (Asperti and Guerrini, 1998). In (L´evy, 1978), L´evy introduced the notion of families of redexes, in the context of optimal reductions. An optimal reduction must avoid the duplication of redexes, either redexes which are already explicitly in the term, or redexes that are going to be created by reduction (virtual redexes). L´evy proposed several definitions of the notion of redex families, such as the zig-zag relation and the extraction relation, and proved their equivalence (L´evy, 1980). He also defined an operational formalisation of redex families based on the labeled λ-calculus. In this approach, terms are marked with labels, that grow along the reduction, allowing to keep a trace of the reduction history. The proof of equivalence between extraction and labeling was also presented in (L´evy, 1978). From labels created along the reduction, one can extract a path in the graphical representation of the initial term. In particular, for labels of redexes, we obtain a path connecting the function edge of an application node, to an abstraction node, therefore identifying a future redex of the term. In (Asperti and Laneve, 1995), legal paths were presented as a characterisation of paths yielded by labels of redexes, therefore proving a characterisation of redex families (virtual redexes).

72

4.2

Chapter 4. Paths and Labels

The Labeled Lambda Calculus

The labeled λ-calculus is an extension of the λ-calculus, proposed by L´evy in (L´evy, 1978). For the remainder of this chapter, we will use x, y, z, . . . to range over term variables, a, b, c, . . . to range over atomic labels, l, l1 , l2 , . . . to range over labels, and ϕ, ψ, φ, . . . to range over paths. Definition 4.2.1 Let L be an infinite set of atomic labels. The set of labels, L, is inductively defined from L in the following way: a∈L l 1 , l2 ∈ L l∈L l∈L

⇒ ⇒ ⇒ ⇒

a∈L l1 l2 ∈ L l∈L l∈L

We assume the operation of concatenation of labels l1 l2 to be associative. Definition 4.2.2 Let V be an infinite set of term variables V, and L be an infinite set of labels. The set of labeled λ-terms, ΛL V is inductively defined from V, and L in the following way: x ∈ V, l ∈ L ⇒ xl ∈ ΛL V L l M, N ∈ ΛV , l ∈ L ⇒ (M N ) ∈ ΛL V L l M ∈ ΛV , x ∈ V, l ∈ L ⇒ (λx.M ) ∈ ΛL V

a @

b

d e

@

j v

f

x h y

λx c @

λy g @

n

k λw l λz m @

w

p

o @

w

i y

Figure 4.1: Labeled λ-term

q z

4.2. The Labeled Lambda Calculus

73

Example 4.2.3 The term M ≡ ((λx.((xe (λy.(y h y i )g )f )d v j )c )b (λw.(λz.(wn (wp z q )o )m )l )k )a has the graph representation given by Figure 4.1. In the graphical representation of terms, to add a label to each subterm, corresponds to mark each edge in the graph by a label. Remark 4.2.4 In the graph presentation of terms it is assume that bound variables are explicitly connected to their binders. For example in the graph represented in Figure 4.1, the edge marked with label e is assumed to be connected to the lambda node marked by label b. For a matter of simplicity we will not draw those connections, but instead show terms as trees. The notions of free and bound variables for ΛL V are defined in an analogous way as for Λ. Also, we assume the same variable convention, and we identify terms up to α-conversion. Definition 4.2.5 (α-congruence) The labeled λ-terms M and N are α-congruent, (notation M ≡α N ), if N can be obtained from M by a series of change of bound variables. That is, replacing a subterm of the form (λx.P )l with (λy.P [y/x])l , where y ∈ / fv(M ). Definition 4.2.6 (Initial labeling) • Consider M in ΛL V . The predicate INIT(M ) is true if and only if the labels of all the subterms of M are atomic and pairwise distinct. • Let M be a term in Λ. The map (·)I is a standard initial labeling function associating a labeled λ-term M I to each term M , such that INIT(M I ) is equal to true. Definition 4.2.7 The concatenation of a label l in L with a labeled λ-term M in ΛL V , denoted l · M , is defined as follows: l1 · xl2 l1 · (M N )l2 l1 · (λx.M )l2

= xl1 l2 = (M N )l1 l2 = (λx.M )l1 l2

Definition 4.2.8 The result of substituting the free occurrences of x for a labeled λ-term P in a labeled λ-term M , denoted by M [P/x], is inductively defined as follows: xl [P/x] y l [P/x] (M N )l [P/x] (λx.M )l [P/x] (λy.M )l [P/x]

≡ ≡ ≡ ≡ ≡

l·P yl ((M [P/x])(N [P/x]))l (λx.M )l (λy.(M [P/x]))l

if x 6≡ y

if x 6≡ y

74

Chapter 4. Paths and Labels

Figure 4.2: Labeled reduction

Definition 4.2.9 (labeled β-reduction) The notion of labeled β-reduction on ΛL V is defined by the following contraction rule (see Figure 4.2 for a pictorial representation of the labeled β-reduction): β : ((λx.M )l0 N )l1 → l1 · l0 · M [l0 · N/x] When a redex R is contracted a label is captured between the label of the application of R, and the label of the body of the abstraction in R. This captured label is called the degree of the redex R. l1 l0 Definition 4.2.10 (Degree) Let M be a labeled λ-term in ΛL V , and R ≡ ((λx.P ) Q) be a redex in M . The degree of the redex R is the label l0 of the function part of the redex (notation degree(R) = l0 ).

In the contractum of a redex R, every new interaction created by the reduction rule must be marked with degree(R), in order to keep a trace of the history of creation. In the λ-calculus, there are three ways of creating new redexes (see (L´evy, 1978)): 1. (λxy.M )N P −→ (λy.M [N/x])P 2. (λx.x)(λy.M )P −→ (λy.M )P 3. (λx.C[xN ])(λy.M ) −→ C ′ [(λy.M )N ′ ], where C ′ [ ] and N ′ , are obtained from C[ ] and N , by replacing all the free occurrences of x by (λy.M ). In the first two ways, redexes are created upwards (that is, redexes where the outermost symbol of the body of the abstraction in R is involved), whereas in the last way the redexes

4.2. The Labeled Lambda Calculus

75

are created downwards (that is, redexes in which the outermost symbol of the argument in R is involved). Overlining and underlining respectively represent this two possibilities.

Example 4.2.11 Consider M ≡ (λy.(λz.w)y)(λx.x). In Figure 4.3, we show the reductiongraph of the term M I . a @

b

d λz e w d λz e w

abc @ f bg λx h x

λy c @

g λx h x

f y

>




a @

g λx h x


length(N ). That is, contracting a redex reduces the length of a term. Proposition 5.2.5 (Strong normalization) If M is a weak affine λ-term, then a reduction path starting at M cannot have more reduction steps than length(M ). Hence, every weak affine λ-term has a normal form.

5.2. The Weak Affine Lambda Calculus

87

ΛWA is a subset of Λ closed under β-reduction. Therefore, ΛWA is confluent. Note that deciding if a term is linear is easy by inspecting the term syntactic tree. However, the definition of weak affine terms relies on the reduction graph of the term, thus it is not direct to decide if a λ-term is weak affine or not. We show that this is still possible in time polynomial in the size of the term. The proof of this result relies on Proposition 5.2.5, and on the next lemma. The next lemma guarantees that we do not need to check every redex in the reduction graph of M , but only redexes in one particular path: the path corresponding to a maximal reduction strategy from M . We recall the maximal reduction strategy F∞ , defined in Chapter 2 for the λ-calculus. Definition 5.2.6 Let n be an integer, and σ be the following reduction path: σ : M0 −→ M1 −→ · · · We define σn (σ bounded by n) in the following way:  σ σn = M −→ M −→ · · · −→ M 0

1

n

if length(σ) ≤ n otherwise.

Definition 5.2.7 Given a λ-term M , the number of reductions steps in F∞ (M ), until the first non-affine redex is reached is called the non-affine depth of M , and denoted by nadepth(M ). Note that, if M is a weak affine λ-term, then nadepth(M ) is not defined (it is a partial function). Example 5.2.8 Consider the term M1 ≡ (λx.x(λz.zz)y)(λw.w), and M2 ≡ (λx.x)(λy.yy). Then nadepth(M1 ) = 2. Note that, because M2 is weak affine, nadepth(M2 ) is not defined. Proposition 5.2.9 The non-affine depth of any λ-term M , when defined, is finite. Note that the non-affine depth is defined for any non-weak affine λ-term. Lemma 5.2.10 Let M be λ-term and σ be the F∞ -reduction path from M . Then M is weak affine if and only if every redex in σ is affine. Proof: We prove the two directions separately. (⇒) The left to right implication is trivial by the definition of weak affine λ-term.

88

Chapter 5. Weak Linearisation

(⇐) We prove the right to left implication by contradiction. Suppose that M is not weak affine, that is, there exists a non-affine redex in σ, then there exists a non-affine redex in σ. We prove the previous implication by induction on the pair (nadepth(M ), length(M )). ~ Let the first non-affine reduction from M be in the reduction path 1. M ≡ xP~ QR. of Q. Then in σ, P~ are first reduced (and are all affine), and then Q is reduced. The result follows by induction hypothesis for Q. 2. M ≡ λx.P . The result follows by induction. ~ with x ∈ fv(P ) or Q ∈ NFβ . If x occurs more than once in P the 3. M ≡ (λx.P )QR result holds trivially. Otherwise, by induction on the non-affine depth of M the ~ Thus, by the definition of F∞ , it also holds for M . result holds for P [Q/x]R. ~ with x 6∈ fv(P ) and Q ∈ 4. M ≡ (λx.P )QR / NFβ . If the first non-affine redex appears in the reduction of Q, then the result follows by induction on the size of the term. Otherwise notice that in σ, Q will be normalised first (since Q has no non-affine redexes, then Q is weak affine, therefore strongly normalisable) and then erased ~ Now the result follows by induction on the non-affine depth of M . leading to P R.

 We now show that deciding if a term is weak affine is as easy as deciding if a term is affine, that is, it is decidable in polynomial time. Theorem 5.2.11 Given a λ-term M , one can decide if M is weak affine in time polynomial in the length of M . Proof: Let n = length(M ) and σ be the F∞ -reduction path from M . Let N be the last term in σn . If N is in normal form then by the previous lemma, just check if σn has a non-affine redex. If N is not in normal form then N is not weak affine because, by Proposition 5.2.5, every weak affine term is normalisable with a number of steps less or equal to length(M ). 

5.3

System TW

We now present a type system that types every weak affine term. Note that closed linear/affine terms are typed in the Simple Type System (Hindley, 1997), but the same does not happen with weak affine terms. In this case functions that are never applied may not be affine. For example, the term λx.xx is weak affine and it is not typable in the Simple Type System.

5.3. System TW

89

In this section, we present a type system here called TW type system, based on intersection types (Coppo and Dezani-Ciancaglini, 1980). Note that the previous term, λx.xx, is typed by intersection types with type ((α → β) ∩ α) → β. We will use intersections only to type abstractions, and when typing applications the function part cannot have a domain denoted by an intersection. This is enough to type every weak affine term keeping the decidability of the type inference problem.

5.3.1

A Type System for Weak Affine Terms

The type TW was used (with minor differences) before in (Kfoury et al., 1999) and (Alves and Florido, 2002), as a restricted form of intersection type inference for terms resulting from a simplification process of arbitrary terms. Recall the definition of the set of intersection types T∩ , given in Chapter 2. Definition 5.3.1 A basis Γ is a finite set of pairs of the form x : σ, where x is a term variable (called the subject) and σ is a type in T∩ . As before, we will denote the set of subjects of a basis Γ by dom(Γ).

Definition 5.3.2 In the TW type system, we say that M has type σ give the basis Γ, and write A ⊢W M : σ, if A ⊢W M : σ can be obtained from the following derivation rules: (Axiom)

(→ IntroI )

(→ IntroK )

(→ Elim)

{x : σ} ⊢W x : σ A ∪ {x : τ1 , . . . , x : τn } ⊢W M : σ A ⊢W λx.M : τ1 ∩ · · · ∩ τn → σ A ⊢W M : σ A ⊢W λx.M : τ → σ A1 ⊢W M : τ → σ

if x ∈ fv(M ) (a)

if x ∈ / fv(M )

A2 ⊢W N : τ

A1 ∪ A2 ⊢W M N : σ

(a) If x : τ1 , . . . , x : τn are all and nothing but statements about x on which A∪{x : τ1 , . . . , x : τn } ⊢W M : σ depends. Note that in TW intersections only appear in the (→ IntroI ) rule, thus in type derivations, intersection types can only appear in the types of abstractions which are not applied.

90

Chapter 5. Weak Linearisation

Example 5.3.3 In system TW we have ⊢W (λx.xx) : (α ∩ (α → β)) → β but (λx.xx)(λx.x) is not typable. Lemma 5.3.4 For any λ-term M , if Γ ⊢W M : σ, then dom(Γ) = fv(M ). Proof: By a straightforward structural induction on M .



Lemma 5.3.5 Let M be in ΛWA of the form M ≡ (λx.P )Q and N ≡ P [Q/x]. Let Γ ⊢W N : σ. Then: 1. If x occurs free in P , then there exists a basis Γ′ , such that Γ′ ⊢W M : σ; 2. If x does not occur free in P , and there exists a basis Γ′′ and a type σ ′′ , such that Γ′′ ⊢W Q : σ ′′ , then there exists a basis Γ′ , such that Γ′ ⊢W M : σ. Proof: 1. By lemma 5.3.4, Γ = Γ1 ∪ Γ2 and Γ1 ∪ Γ2 ⊢W P [Q/x] : σ (1) where dom(Γ1 ) = fv(P ) \ {x} and dom(Γ2 ) = fv(Q). This derivation contains a subderivation of the form: Γ2 ⊢W Q : θ (2) In (1), the assumption Q : θ is only used once, since P only contains one x. If we replace Q by x through (1), we get the deduction Γ1 ∪ {x : θ} ⊢W P : σ

(3)

Rule (→ IntroI ) can be applied to (3) to get Γ1 ⊢W λx.P : θ → σ

(4)

and then by (4) and (2) and rule (→ Elim) we have Γ ⊢W M : σ. 2. Argue as in 1, but use the assumed-to-be-given type derivation Γ′′ ⊢W Q : σ ′′ instead of (2). 

Theorem 5.3.6 If M is a term in ΛWA , then M is typable in system TW .

5.3. System TW

91

Proof: We will use induction on (depth(M ), length(M )). Note that any λ-term has exactly one of the following forms: 1. λx1 . . . xn .xM1 . . . Mp , (n ≥ 0, p ≥ 0); 2. λx1 . . . xn .(λx.M )M1 . . . Mp , (n ≥ 0, p ≥ 1). We now proceed by cases: 1. M ≡ λx1 . . . xn .xM1 . . . Mp , with x 6= x1 , . . . , xn . By induction hypothesis we have: Γ 1 ⊢ W M1 : σ 1 , . . . , Γ p ⊢ W Mp : σ p . By successive applications of the (→ Elim) rule, we have: Γ1 ∪ · · · ∪ Γp ∪ {x : σ1 → · · · → σp → σ} ⊢W xM1 . . . Mp : σ. Let Γ be Γ1 ∪· · ·∪Γp ∪{x : σ1 → · · · → σp → σ} where we remove the type assignments for xi , (1 ≤ i ≤ n). Then, by successive applications of (→ IntroI ) and (→ IntroK ) rules we have: Γ ⊢W λx1 . . . xn .xM1 . . . Mp : τ1 → · · · → τn → σ where τi is the intersection of all types declared for xi in Γ1 ∪· · ·∪Γp if such declarations exist, or a fresh type variable if it does not exist. 2. M ≡ λx1 . . . xn .xi M1 . . . Mp . Similar to the previous case. 3. M ≡ λx1 . . . xm .(λx.N )P N1 . . . Nn . By induction hypothesis: Γ1 ⊢W P : σ

and

Γ2 ⊢W N [P/x]N1 . . . Nn : τ.

We claim there exists a type environment Γ′ such that Γ′ ⊢W (λx.N )P N1 . . . Nn : τ . Let Γ be Γ′ where we remove the type assignments for xi , (1 ≤ i ≤ m). By successive applications of the (→ IntroI ) rule we have: Γ ⊢ W M : τ1 → · · · → τm → τ where each τi is the intersection of all types declared for xi in Γ′ if such declarations exist, or a fresh type variable if it does not exist. Our claim is proved by induction on n where the base case is lemma 5.3.5.  The right to left implication of theorem 5.3.6 does not hold. That is, terms typable in TW , are not necessarily weak affine terms. For example, the term (λf x.f (f x))(λy.y) is not weak affine and it has type α → α in TW . Note that, in particular, all terms typable in the Simple Type System, are typable in TW .

92

Chapter 5. Weak Linearisation

5.3.2

A Type Inference Algorithm

The type inference algorithm presented here is a generalisation of Hindley’s algorithm for Simple Types (Hindley, 1969). The main difference to the Simple Type System is that type declarations for the same variable in the environment may not be unique, and abstractions are typable even when types of their formal parameter do not unify.

Definition 5.3.7 (Context) We define the following notions: • A context Π, is a list of pairs of the form x : τ , where x is a variable and τ a type. • If Π is a context, then types(x, Π) is the list of types associated to x in Π. • The concatenation of two contexts, Π1 and Π2 , is denoted by Π1 ++ Π2 .

Definition 5.3.8 Let Π = [x1 : τ1 , . . . , xn : τn ]: • The set of subjects of Π, is denoted by dom(Π) = {x1 , . . . , xn }. • The context Πx is obtained from Π, by removing all the pairs having x as subject. • If S is a substitution on types, then S(Π) = [x1 : S(τ1 ), . . . , xn : S(τn )]

Lemma 5.3.9 If Γ ⊢W M : σ, and S is a type substitution, then: S(Γ) ⊢W M : S(σ)

Proof: Easy by structural induction on M .



Definition 5.3.10 (Nested permutations) Let [σ1 , . . . , σn ], be a list of types. We call p a nested permutation of [σ1 , . . . , σn ] if p is defined as follows: p(α) = α p([τ1 , . . . , τn ]) = [σ1 , . . . , σn ] if ∃p1 . . . pn .pi (τi ) = τi′ and permut([τ1′ , . . . , τn′ ]) = [σ1 , . . . , σn ] where permut([σ1 , . . . , σn ]) gives a permutation of [σ1 , . . . , σn ] if ∃p1 . . . pn .pi (σi ) = σi′ p(σ1 ∩ · · · ∩ σn ) = σ1′ ∩ · · · ∩ σn′ p(σ → τ ) = σ ′ → τ ′ if ∃p1 , p2 .p1 (σ) = σ ′ and p2 (τ ) = τ ′

5.3. System TW

93

Example 5.3.11 Consider Π = [α ∩ β → α, (β ∩ (α ∩ β)) → β]. Both p1 , p2 , and p3 defined below are nested permutations of Π. p1 (Π) = [β ∩ α → α, (β ∩ (β ∩ α)) → β] p2 (Π) = [(β ∩ (β ∩ α)) → β, β ∩ α → α] p3 (Π) = [((α ∩ β) ∩ β) → β, β ∩ α → α] Remark 5.3.12 By abuse of notation we will sometimes write Π = Γ where Π is a list and Γ is a set. This corresponds formally to toSet(Π) = Γ, where toSet is a function that makes a set from a list of elements, that is, in toSet(Π) the order of the elements becomes irrelevant and we do not distinguish repetitions. Definition 5.3.13 Let UNIFY be Robinson’s unification algorithm (Robinson, 1965). Given a λ term M , we define the function I(M ) = (Π, τ ), where Π is a context, and τ is a type, thus: 1. If M = x then I(M ) = ([x : α], α) where α is a type variable. 2. If M = λx.N then: • If I(N ) = (Π′ , τ ) and types(x, Π′ ) = [ ], then I(λx.N ) = (Π′ , α → τ ) where α is a new type variable. • If I(N ) = (Π′ , τ ) and types(x, Π′ ) = [τ1 , . . . , τn ] , then I(λx.N ) = (Π′x , τ1 ∩ · · · ∩ τn → τ ). 3. If M = M1 M2 then I(M ) = (S(Π1 ++ Π2 ), S(α)) where: • I(M1 ) = (Π1 , τ1 ); • I(M2 ) = (Π2 , τ2 ); • S = UNIFY(τ1 , τ2 → α) (α is a new type variable). Lemma 5.3.14 If I(M ) = (Π, σ), then dom(Π) = fv(M ). Proof: By a straightforward structural induction on M .

Theorem 5.3.15 (1) I(M ) = (Π, σ) if and only if M is typable in system TW . (2) I(M ) terminates in time polynomial in the size of M . Proof:



94

Chapter 5. Weak Linearisation

1. We prove the two directions separately. (⇒) If I(M ) = (Π, σ), then toSet(Π) ⊢ M : σ. The proof follows by induction on M . – If M = x, then I(M ) = ([x : α], α), and {x : α} ⊢W x : α. – If M = λx.N and x ∈ / fv(N ), then I(N ) = (Π′ , σ), types(x, Π′ ) = [ ], and I(λx.N ) = (Π′ , α → σ), where α is a new type variable. By induction hypothesis toSet(Π′ ) ⊢W N : σ. Thus, by (→ IntroK ), toSet(Π′ ) ⊢W λx.N : α → σ. – If M = λx.N and x ∈ fv(N ), then I(N ) = (Π′ , σ), types(x, Π′ ) = [τ1 , . . . , τn ], and I(λx.N ) = (Π′x , τ1 ∩ · · · ∩ τn → σ). There exists a permutation p, such that Π′ = p(Π′x ++ [x : τ1 , . . . , x : τn ]), and toSet(Π′ ) = toSet(Π′x ) ∪ {[x : τ1 , . . . , x : τn }. Then, by induction hypothesis toSet(Π′x ) ∪ {x : τ1 , . . . , x : τn } ⊢W N : σ. Thus, by (→ IntroI ), toSet(Π′x ) ⊢W λx.N : τ1 ∩ · · · ∩ τn → σ. – If M = M1 M2 , then I(M1 ) = (Π1 , σ1 ), I(M2 ) = (Π2 , σ2 ), S = UNIFY(σ1 , σ2 → β), where β is a new type variable, and I(M1 M2 ) = (S(Π1 ++ Π2 ), S(β)). By induction hypothesis toSet(Π1 ) ⊢W M1 : τ1

toSet(Π2 ) ⊢W M2 : τ2 .

By lemma 5.3.9 S(toSet(Π1 )) ⊢W M1 : S(τ2 ) → S(β)

S(toSet(Π2 )) ⊢W M2 : S(τ2 ).

Thus, by (→ Elim) S(toSet(Π1 )) ∪ S(toSet(Π2 )) ⊢W M1 M2 : S(β). (⇐) If Γ ⊢W M : τ , then I(M ) = (Π, σ) and there exist nested permutations p1 , p2 and a substitution S, such that p1 (Π) = Π′ , p2 (σ) = σ ′ and (S(Π′ ), S(σ ′ )) = (Γ, τ ). The proof follows by induction on the derivation tree of Γ ⊢W M : τ . (a) M = x. Let p1 = p2 = id and S = {τ /α}.

5.3. System TW

95

(b) M = λx.N and x ∈ fv(N ). In this case τ = τ1 ∩ · · · ∩ τn → δ and the previous step of the type derivation is: Γ ∪ {x : τ1 , . . . , x : τn } ⊢ N : δ. By induction hypothesis I(M ) ∃p3 , p4 , S. p3 (Π) p4 (θ) S(Π′ , θ′ )

= = = =

(Π, θ) and Π′ , θ′ and (Γ ∪ {x : τ1 , . . . , x : τn }, δ).

By rule 2, of the type inference algorithm: I(λx.N ) = (Πx , θ1 ∩ · · · ∩ θn → θ) where types(x, Π) = [θ1 , . . . , θn ]. Now let p1 (θ1 ∩ · · · ∩ θn → θ) = p3 (θ1 ∩ · · · ∩ θn ) → p4 (θ) and p2 (Π) = p3 (Π). With this choice of p1 , p2 and S the result follows. (c) M = λx.N and x ∈ fv(N ). Similar to the previous case. (d) M = M1 M2 . In this case we have Γ1 ∪ Γ2 ⊢W M1 M2 : τ , and previous steps in the type derivation are: Γ1 ⊢W M1 : τ ′ → τ and Γ2 ⊢W M2 : τ ′ . By induction hypothesis we have: I(M1 ) = (Π1 , θ1 ) (3) p3 (Π1 ) = Π′1 (5) p4 (θ1 ) = θ1′ (1) S1 (Π′1 , θ1′ ) = (Γ1 , τ ′ → τ )

I(M2 ) = (Π2 , θ2 ) p5 (Π2 ) = Π′2 (4) ′ p6 (θ2 ) = θ2 S2 (Π′2 , θ2′ ) = (Γ2 , τ ′ ) (2).

Equalities (1) and (2) imply that the left hand side of θ1′ , and θ2′ unify with the m.g.u. S′′ . Thus it is also true that S′′ = UNIFY(θ1′ , θ2′ → α) (7) where α is a new type variable. Note that S1 ◦ S2 is also a unifier but less general (or equal) than S′′ . Now by rule 3 of the type inference algorithm we have I(M1 M2 ) = (S′ (Π1 ++ Π2 ), S′ (α)) where S′ = UNIFY(θ1 , θ2 → α) The result follows by choosing:

(9)

96

Chapter 5. Weak Linearisation

– p1 (S′ (Π1 ++ Π2 )) = p3 (S′ Π1 ) ++ p5 (S′ Π2 ) = S′ Π′1 ++ S′ Π′2 by (3), (4) and noting that p(Π) = Γ implies p(SΠ) = SΓ. – p2 = p4 . In this case we have by (5): p4 (θ1 ) = θ1′ ⇒ p4 (S′ θ1 ) = S′ θ1′ (8) and by (8), p4 (S′ α) = S′′ α. This happens because by (7) S′′ = UNIFY(θ1′ , θ2′ → α), thus S′′ (α) is equal to the right hand side of S′ θ1′ and noting that, by (9), S′ α is the right hand side of S′ θ1 . Now by (1) we have S1 (S′′ α) = τ , thus just choose S = S1 ◦ S2 . Now notice that S1 ◦ S2 (p1 (S′ (Π1 ++ Π2 )), p4 (S′ α)) = S1 ◦ S2 (S′ Π′1 ++ S′ Π′2 , S′′ α) = (Γ1 ∪ Γ2 , τ ) because type variables in Π′1 and Π′2 are disjoint and S1 has effect only in variables in Π′1 and S2 in variables in Π′2 . 2. The second part of the theorem follows noticing that unification is PTIME-complete and that the inference algorithm analyses each subterm at most once.  Theorem 5.3.15 shows that weak affine terms are easy to analyse using intersection types. Note that general intersection type systems are not decidable (Coppo and Dezani-Ciancaglini, 1980) and known restrictions of these systems have exponential type inference (Kfoury et al., 1999). Weak affine terms are a class where the type inference problem, using intersection types, is decidable in polynomial time.

5.4

Weak Linearisation

In this section we present a transformation from arbitrary λ-terms into weak affine terms. As we mentioned before, the transformation from λ-terms into weak affine terms, has to know in advance that subterms of the form (λx.M ) and N are going to be the function and argument part of a redex in the future, i.e. (λx.M )N is a virtual redex. To deal with this complicated issue we make use of the notion of legal paths, presented in the previous chapter.

5.4.1

Term transformation

We start by proving some lemmas, which will be used in the proof of the correctness of the transformation.

5.4. Weak Linearisation

97

Lemma 5.4.1 M is a strongly normalisable term if an only if the set LP of legal paths of type @-λ in M is finite. Proof: We prove the two directions separately, both by contradiction. (⇐) Suppose that the set of legal paths of M is not finite. By theorem 4.4.16 there is an infinite number of paths yielded by degrees of redexes. Thus, there is an infinite reduction of M , which means that M is not strongly normalisable. (⇒) Suppose that M is not strongly normalisable. Let us choose a reduction of M that does not stop. That reduction will cause an infinite number of paths yielded by degrees of redexes. Thus, by theorem 4.4.15, an infinite number of legal paths. 

Definition 5.4.2 Let M be a λ-term and LP be the set of legal paths of M . Then: • We call legal paths of the form a, where a is an atomic label, the initial paths of M (corresponding to explicit redexes in M ). • The set S0 is the set of initial paths, plus paths obtained from those by @-composition (corresponding to explicit and implicit redexes in M ). • The set Si if build by λ-composition from the paths in the sets S1 , . . . , Si−1 and @composition from the paths in Si . Definition 5.4.3 Let LP be the set of legal paths of a term M . We define the chain of dependencies between legal paths of type @-λ in LP as CM = S1 ≫ S2 ≫ · · · ≫ Sn ≫ · · · Note that, the chain of dependencies defined above is finite if an only if the set LP of legal paths of M is finite, therefore (by Lemma 5.4.1) if and only if M is strongly normalisable. Example 5.4.4 Recall the λ-term M in Figure 4.1, with the set LP, of legal paths of type @-λ, given in Example 4.4.4, then: CM = {b} ≫ {e · b · k, d · e b k · l} ≫ {n · k b e · f, p · k b e · f }. Definition 5.4.5 (Affine legal path) A legal path ϕ of type @-λ is called an affine legal path, if ϕ ends in an abstraction λx.P , and x occurs at most once in P .

98

Chapter 5. Weak Linearisation

Definition 5.4.6 Let M be a λ-term, CM be the chain dependencies of M , and ϕ be the first non affine legal path in CM . The function next non affine(M ) returns a pair of labels (l, k), where l is the last label of ϕ, and k is the last. That is, l is the label of the abstraction node where ϕ ends, and k is the label of the function edge of the application node where ϕ starts. If every legal path is affine, then next non affine(M ) =⊥ . Basically next non affine(M ) returns the labels which identify the next non-affine virtual redex. Example 5.4.7 Consider the chain CM in Example 5.4.4. The result of next non affine(M ) is (k, e), and corresponds to the legal path represented in the right hand side of Figure 4.10. Lemma 5.4.8 If M is a λ-term and (l, k) = next non affine(M ), then the only application node such that there is a legal path ϕ of type @-λ ending in an abstraction labelled l, is the application with function edge labeled k. Proof: Suppose there are legal paths ϕ1 and ϕ2 both finishing in the abstraction (λx.P )l and starting in two different application nodes. Thus, there are two different degrees l1 , l2 of redexes (having the abstraction labeled l as left side) originated along some reduction of M such that path(li ) = ϕi (i = 1, 2). If (λx.P )l appears as the left side of two different redexes, then (λx.P )l was duplicated by some reduction previous to l1 and l2 . Thus, there is a non affine legal path previous to ϕ1 and ϕ2 , in which case (l, k) would not be the result of next non affine(M ). 

Example 5.4.9 Consider the chain CM in Example 5.4.4. We have next non affine(M ) = (k, e), and the application node whose function edge is e is the only application node for which there is a legal path ϕ ending in k. Definition 5.4.10 (one step of the transformation) Let M be a λ-term, and (d, b) = next non affine(M ). Suppose that (λx.N e )d and (P b Qc )a occur in M and xf1 , . . . , xfn where n ≥ 2, are the free occurrences of x in N e , from left to right. Suppose that neither (λx.N e )d occurs in Qc , nor Qc occurs in (λx.N e )d . Then L(M ), is the term that results by simultaneously replacing in M : 1. (λx.N e )d by (λx1 .(. . . (λxn .(N ∗ )en )en−1 · · · )e1 )d where N ∗ results from replacing xfi by the fresh variable xfi i for i ∈ {1, . . . , n}, and

5.4. Weak Linearisation

99

2. replacing (P b Qc )a by ((· · · (P b Qc11 )a1 · · · )Qcnn )an where Qi results from Q by replacing any label l in Q by the new label li , for all the labels of all the subterms in Q, for i ∈ {1, . . . , n}. See Figure 5.1 for a pictorial representation of transformation L.

Figure 5.1: Expansion

Example 5.4.11 Let M be the λ-term in the left hand side of Figure 5.2. The term that results from expanding the first non affine path (e · b · k) in M , L(M ) is the term in the right hand side of Figure 5.2.

Definition 5.4.12 (Transformation into weak affine terms) Let M be a λ-term. We define the following function:  M if next non affine 6=⊥ T (M ) = T (L(M )) otherwise. Example 5.4.13 Let ∆ = λy.yy and D = λy1 y2 .y1 y2 . Let M = (λx.x∆v) (λwz.w (wz)) be the term represented in Figure in the left hand side of Figure 5.2. Let us follow the transformation in some detail. We will omit most of the labels of the terms. We first linearise the abstraction (λw.(λz.w(wz))l )k and duplicate the argument of the application node (xe ∆f )d corresponding to the first non affine abstraction on the chain CM in Example 5.4.4, (e · b · k), to get the term in the right hand side of Figure 5.2. T (M ) = T ((λx.((xe ∆f1 )d1 ∆f2 )d2 v)(λw1 .(λw2 (λz.w1 (w2 z))l2 )l1 )k

100

Chapter 5. Weak Linearisation

Figure 5.2: One step transformation

After one more step, linearising ∆f1 , we get: T ((λx.x(λy1 .(λy2 .(y1 y2 )g2 )g1 )f1 ∆v)(λw1 w2 z.((w1 (w2 z)o1 )m1 (w2 z)o2 )m2 )) Now the abstractions labeled by l1 and l2 in (λw2 .(λz.w1 (w2 z)(w2 z))l2 )l1 which were affine, became non affine thus, after one more step we get: T ((λx.xD∆f21 ∆f22 v)(λw1 w21 w22 z.w1 (w21 z)(w22 z))) Linearising (λz.w1 (w21 z)(w22 z))l2 we get T ((λx.xD∆∆v j1 v j2 )(λw1 w2 w3 z1 z2 .w1 (w21 z1 )(w22 z2 ))) The next non affine abstraction is the copy of ∆ labeled by f21 , thus we get: q1

q1

T ((λx.xDD∆vv)(λw1 w2 w3 z1 z2 .w1 (w21 z1 1 z1 2 )(w22 z2 ))) The abstraction (λz1 z2 .w1 (w21 z1 z1 )(w22 z2 ))) becomes non affine, thus, after a few more steps, we get: = = = =

T ((λx.xDD∆vvv)(λw1 w21 w22 z11 z12 z2 .w1 (w21 z11 z12 )(w21 z2 ))) T ((λx.xDDDvvv)(λw1 w21 w22 z11 z12 z2 .w1 (w21 z11 z12 )(w21 z2 z2 ))) T ((λx.xDDDvvvv)(λw1 w21 w22 z11 z12 z21 z22 .w1 (w21 z11 z12 )(w21 z21 z22 ))) (λx.xDDDvvvv)(λw1 w21 w22 z11 z12 z21 z22 .w1 (w21 z11 z12 )(w21 z21 z22 ))

Now all the legal paths of type @-λ are affine, thus the transformation terminates. Note that nf(M ) = (vv)(vv) = nf(T (M )). As we shall prove later, preservation of normal forms holds for every term.

5.4. Weak Linearisation

5.4.2

101

Correctness

Here we show the correctness of the T transformation, in the sense that it preserves normal forms, and transformed terms are weak affine. The proof relies on the next lemma, which basically, relates the legal paths of the original term and of the transformed term. Lemma 5.4.14 Let (d, b) = next non affine(M ), and L(M ) = ML . Suppose that (λx.P e )d and (Qb N c )a occur in M . Then the set LP ML of legal paths of ML is such that: 1. If ϕ is a legal path internal to N , then ϕi (i = 1 . . . n) is a legal path in the ith copy of N in ML (the path ϕi is obtained from ϕ, by replacing every label l in ϕ, by li ); 2. If ϕ is a legal path with a @-cycle in N (starting and ending outside N ), then ϕ′ is a legal path with a cycle in a copy of N in ML ; 3. If ϕ is a legal path not internal but starting and ending in N , then ϕ′ is a legal path starting and ending in a copy of N in ML ; 4. If ϕ is a legal path starting/ending in N , then ϕ′ is a legal path starting/ending in a copy of N in ML , and ending/starting in the same edge; 5. If ϕ = bψd is the legal path in M of type @-λ ending in (λx.P e )d , then in ML , there are n paths of type @-λ ending respectively in (λx1 . . . xn .P ∗ )d , (λx2 . . . xn .P ∗ )e1 , . . . , (λxn .P ∗ )en−1 , and starting respectively in b, a1 , . . . , an−1 , (n ≥ 0); 6. If ϕ is a legal path in M of any type, external to N , then there is a legal path of the same type in ML , starting and ending in the same edges. Proof: The proof follows by induction on the number of recursive calls of the algorithm for building legal paths. • (base case) We consider the paths marked at the beginning of the algorithm, which are single edges from application nodes to variables, abstractions or other application nodes. 1. For every label a in N in M , there is a label ai (i = 1 . . . n) in the ith copy of N in ML . Thus for every initial legal path ϕ in N in M there is legal path ϕi (i = 1 . . . n) in the ith copy of N in ML ; 2. There are no initial paths with cycles; 3. An initial path starting and ending in N is an internal path in N ;

102

Chapter 5. Weak Linearisation

4. There are no initial paths starting/ending in N and ending/starting outside N ; 5. If b is an initial path of type @-λ then it corresponds to the path in ((λx.P e )b N c )a . Then in ML we have a term of the form (· · · (((λx1 .(λx2 . . . (λxn .P ∗ en )en−1 · · · )e1 )b N c1 )a1 N c2 ) · · · N cn )bn In ML we have the legal path b of type @-λ and, after applying the @-composition rule (n − 1) times, we get the paths a1 · b · e1 , . . . , an−1 · · · · · a1 · b · e1 · · · · · en−1 . 6. For any other initial path in M we have the same initial path in ML . • (@-composition) Let φ be a legal path from M obtained by applying the @-composition rule. Then φ = ψ · ϕ · a where ψ is a @-@ legal path, ϕ is a @-λ legal type, and a is a label. 1. If φ is internal to N then ψ, ϕ and a are all internal to N . Thus, by induction hypothesis, ψi and ϕi (i = 1 . . . n) are paths of the same type, and ai is a label in the ith copy of N in ML . Thus, by the (@-composition) rule φi = ψi · ϕi · ai (i = 1 . . . n), is a path in the ith copy of N in ML ; 2. If φ has a cycle over N , the only case that needs some care is the case where ψ ends inside N , and ϕ starts inside N (the other cases are easily proved by induction). By Proposition 4.4.11 and Definition 4.4.13, φ is uniquely decomposed into ζ1 aλ(ζ2 )r @ψ ′ @ζ2 λaζ4 , thus ψ = ζ1 aλ(ζ2 )r @ψ1 and ϕ = ψ2 @ζ2 λaζ4′ . We then have two possibilities: (a) Both ψ1 and ψ2 are contained in N . In this case, by the induction hypothesis, ψ ′ ends in a copy j of N and ϕ′ starts in the same copy j of N . These copies have to be the same, otherwise in the original path the discriminants would be binding edges of different variable occurrences, thus the path would not be legal. (b) ψ is composed of subpaths internal to N , and v-cycles over free variables of N . Since λ abstractions binding edges in N in M , also bind any copy of N in ML then it is possible to have those v-cycles in any copy of N . 3. If φ is a legal path starting and ending in N , then: (a) If φ is a legal path starting and ending in N , with no cycles, we have three possibilities: i. ψ is internal to N , ϕ starts and ends in N and a is a label in N . By the induction hypothesis, there is a legal path ψi in each copy of N , ϕ′ starts and ends in a copy of N , and there is a ai in each copy of N . Thus ψj · ϕ′ · ak is a legal path starting and ending in a copy of N ; ii. ψ starts and ends in N , ϕ is internal to N and a is a label in N . By the induction hypothesis, there is a legal path ϕi in each copy of N , ψ ′

5.4. Weak Linearisation

103

starts and ends in a copy j of N , and there is a label ai in each copy of N , therefore there exists aj in Nj . Thus ψ ′ · ϕj · aj is a legal path starting and ending in a copy of N ; iii. ψ starts in N , ϕ ends in N and a is a label in N . By the induction hypothesis, there is a legal path ψ ′ starting in a copy of N and ending in the same edge outside N , ϕ′ starts in the same edge outside N and ends in a copy j of N , and there is a label aj in Nj . Thus ψ ′ · ϕ′ · aj is a legal path starting and ending in a copy of N ; (b) If φ contains a @-cycle the proof follows by the induction except for the case where the two subpaths join inside N , which is proved as in (2). 4. If φ is a legal path ending in N then we have two cases: (a) If φ does not contain a cycle then we have two possibilities: i. ψ is a legal path ending in N and ϕ is a path internal to N . By the induction hypothesis ψ ′ is a legal path ending in a copy j of N , ϕj (i = 1 . . . n) is a legal path in the j th copy of N , and aj is a label in Nj . Thus ψ ′ · ϕj · aj is a legal path ending in the j copy of N ; ii. ψ is a legal path external to N and ϕ is a legal path ending in N . By the induction hypothesis ψ ′ is a legal path starting and ending in the same edges, and ϕ′ is a legal path starting in the same edge and ending in a copy j of N . Thus ψ ′ · ϕ′ · aj is a legal path ending in a copy j of N . (b) If φ contains a @-cycle it is proved by induction except for the case where the two subpaths join inside N , which is proved as in (2). If φ is a legal path starting in N it is proved in a similar way. 5. If φ is the legal path expanded then, by the induction hypothesis, ψ ′ and ϕ′ are legal paths in ML starting and ending in the same edges (because φ and ϕ are both external to N ), and there exist (n − 1) new paths of type @-@, a1 , . . . , an−1 . Thus ψ ′ · ϕ′ · a is a legal path of type @-λ in ML ending in (λx1 . . . xn .P ∗ ), and by (n − 1) applications of the (@-composition) rule we get (n − 1) paths of type @-λ ending respectively in (λx2 . . . xn .P ∗ ), . . . , (λxn .P ∗ ), (n ≥ 1) (and starting respectively in a1 , a2 , . . . , an−1 . 6. If φ is a legal path external to N , then we have two cases: (a) If ϕ is the expanded path then, by the induction hypothesis, we have a path ϕ′ starting in the same application node and ending in (λxn .P ∗ en−1 ) (note that ϕ ends in (λx.P e )d ), and ψ ′ starting and ending in the same edges. Thus ψ ′ .ϕ′ .a starts and ends in the same edges. (b) If ϕ isn’t the expanded path, then it is easily proved by induction. • (λ-composition) Let φ be a legal path from M obtained by the @-composition rule. Then φ = ψ.ϕr .a where ψ is a @-v legal path, ϕ is a @-λ legal type, and a is a label.

104

Chapter 5. Weak Linearisation

1. If φ is internal to N then, as in the (@-composition) rule φi = ψi .ϕri .ai (i = 1 . . . n), is a path in the ith copy of N in ML ; 2. If φ contains a @-cycle it is proved as in the (@-composition) rule; 3. If φ is a legal path starting and ending in N beside the cases in the (@-composition) rule, we also have the case where ψ ends outside N and ϕ starts in the application node of N . By the induction hypothesis, ψ ′ starts in a copy of N and ends in the same edge, and there exists ϕ′ beginning in the application node of the j th copy of N . Thus ψ ′ .ϕ′ r .aj is a legal path starting and ending in a copy of N ; 4. If φ is a legal path ending in N then we have two cases: (a) If φ does not contain a cycle, beside the cases in the (@-composition) rule, we also have the case where ψ is external to N and ϕ starts in the application node of N . By the induction hypothesis, ψ ′ starts and ends in the same edges, and there exists ϕ′ beginning in the application node of the j th copy of N . Thus ψ ′ .ϕrj .aj is a legal path ending in a copy of N ; (b) φ contains a @-cycle. Proved as before. If φ is a legal path starting in N it is proved in a similar way. 5. Proved by induction as in the (@-composition) rule. 6. Proved by induction as in the (@-composition) rule. 

Example 5.4.15 Let M be the labeled term in the left hand side of Figure 5.2, and ML , the term obtained from M after one step of transformation, in the right hand side of Figure 5.2. Note that, for the legal path h in M , internal to the duplicate argument ∆f , we have the corresponding legal paths h1 and h2 , respectively internal to the copies ∆f1 and ∆f2 , in ML . This illustrates point 1 of Lemma 5.4.14. For point 5 of the same lemma, notice that, for the expanded legal path e b k of M , we have two legal paths e b k and e1 e b k k1 is ML . The next theorem guarantees that the transformation maps terms into the weak affine λcalculus. Theorem 5.4.16 Let M be in ΛWA . If T (M ) = N , then N is weak affine. Proof: If T (M ) terminates then the set of legal paths (of type @-λ) in N , is a finite set of affine legal paths. Since only the abstraction nodes which are the end of a legal path appear

5.4. Weak Linearisation

105

as the left side of a redex along a reduction of N (that follows from theorem 4.4.15), and being all those abstractions affine, then no reduction starting from N duplicates arguments. Thus N is weak affine.  The following lemma will be used in the proof of correctness of the transformation. Lemma 5.4.17 Let σ be a reduction sequence from a term M using the normal order reduction strategy. Let ∆ be a β-redex reduced in another reduction sequence of M and such that ∆ is not reduced in σ. Then there exists a β-redex ∆′ reduced in σ such that ∆′ ≡ (λx.P )Q and x 6∈ fv(P ) and ∆ occurs in Q. Proof: Suppose that ∆ is not reduced in σ and that ∆ is not erased in σ (by a redex of the form ∆′ ). Then ∆ must be a subterm of the normal form of M , which is absurd, because it is a β-redex, thus it cannot be a subterm of a term in normal form.  The next lemma shows that the transformation preserves normal forms. Note that this is not possible if the mapping was into the linear/affine λ-calculus (as it was shown in the introduction of the chapter). Lemma 5.4.18 If L(M ) = N , and both N and M have a normal form, then nf(M ) = nf(N ). Proof: Notice that the normal order reduction strategy is normalising, that is, if there is a normal form of M then it can be reached using the normal order strategy of reduction. Now let (λx.P ) be the abstraction labeled d, where (d, b) = next non affine(M ), and choose the normal order reduction of M . We then have two cases:

1. The expanded abstraction appears in the normal reduction sequence: M → · · · → (λx.P )Q → [Q/x]P → · · · → nf(M ). By lemma 5.4.14, we have N → · · · → (λx1 . . . xn .P ∗ ) Q . . . Q →∗ [Q/x1 , . . . , Q/xn ]P ∗ . | {z } n times

But [Q/x1 , . . . , Q/xn ]P ∗ = [Q/x]P , thus [Q/x1 , . . . , Q/xn ]P ∗ → · · · → nf(M ). Thus nf(N ) = nf(M ).

106

Chapter 5. Weak Linearisation

2. If the expanded abstraction does not appear in the normal reduction sequence of M then, use lemma 5.4.17 to get a similar result. 

Theorem 5.4.19 If T (M ) = N , then nf(M ) = nf(N ). Proof: Easy by induction on the number of recursive calls of T , and using lemma 5.4.18.  Remark 5.4.20 Note that weak affine terms normalise in time polynomial in the size of the term. This means that if the original term M normalises in exponential time, our transformation will increase the size of M exponentially (because the result term will be normalisable in polynomial time). It is interesting to note that weak linearisation transfers the complexity of the term from the reduction level to the size level. We now show that if the transformation halts then the original term is strongly normalisable. Theorem 5.4.21 If T (M ) = N , then M is strongly normalisable. Proof: If T (M ) terminates then the set of legal paths of N is finite. By lemma 5.4.14, it is easy to verify that the number of legal paths in T (M ) is greater or equal to the number of legal paths in M , thus the set of legal paths of M is finite, which means, by lemma 5.4.1, that M is strongly normalisable.  The converse implication remains an open problem. Note that the transformation may not terminate. In every example tested, the non-termination of T (M ) arises when the reduction of M itself may not terminate. Example 5.4.22 Let ∆ = λx.xx, D = λx1 x2 .x1 x2 , and Ω = ∆∆. We have: T (Ω) = = = = = =

T (D∆∆) T (λx1 x2 .x1 x2 x2 )D∆) T (λx1 x21 x22 .x1 x21 x22 )D∆∆) T (λx1 x21 x22 .x1 x21 x22 x22 )DD∆) T (λx1 x21 x221 x222 .x1 x21 x221 x222 )DD∆∆) ···

Since the set of legal paths of Ω is not finite, T (Ω) never terminates. This is somehow expected since, the cause of non-termination of the transformation process is the existence of an infinite number of legal paths (which can only happen when the term is not strongly normalisable).

5.5. Discussion

5.5

107

Discussion

We introduced the notion of weak linearisation, developed its basic properties, established its relationship with legal paths, and used it to show that it is possible to transform λterms into a class of terms where every applied function is affine, while still preserving normal forms. We conjecture that our transformation terminates if and only if the term is strongly normalisable. The left-to-right implication is proved in Theorem 5.4.21. The rightto-left implication would probably require a non-trivial analysis of the interaction between reductions (legal paths) of the initial term and of the transformed term. Another approach to the linearisation of the λ-calculus was made by Kfoury in (Kfoury, 2000). Kfoury embedded the λ-calculus into a larger calculus, denoted Λ∧ with a new notion of reduction, denoted β ∧ . In the new calculus Λ∧ , in every function application, an argument is used at most once. He also defined the notion of contraction of a term in the new calculus, giving a λ-term. The relation between Λ∧ and Λ, was made indirectly saying that the well-formed terms of the new calculus were the ones for which there is a contraction in the λ-calculus. However, no direct definition of a transformation of λ-terms into terms of the new calculus was presented. In (Girard, 1987), Girard presented a translation from intuitionistic logic into linear logic. This work was extended for programming languages in several other works (Abramsky, 1993; Lincoln and Mitchell, 1992; Wadler, 1990; Benton et al., 1992; Benton and Wadler, 1996; Maraist et al., 1995). There is a fundamental difference between these previous papers on linear logic and our work. These previous translations were all made into a language that allows duplication to some extent (using the standard exponential modality of linear logic !), while our transformation is made into the weak affine λ-calculus where duplication is forbidden.

108

Chapter 5. Weak Linearisation

Chapter 6

Linear Lambda Calculus with Iterators 6.1

Introduction

In previous chapters, linearisation was defined for:

• terms typable in rank 2 intersection type system with a simple type (Chapter 3); • the λ-calculus (Chapter 5).

In both cases, the perspective was to go from subsets of the full λ-calculus to “more linear” or affine calculus, preserving types in the first case, and normal forms in the second. In this and the following chapter, linearity will be studied from a different point of view. Starting from the linear λ-calculus, we consider a simple extension with booleans, natural numbers, and a restricted iterator (only closed linear functions can be iterated), and study the computational power of this calculus. Our system can be seen as a linear version of G¨ odel’s System T , presented in Chapter 2, with a closed reduction strategy. In the following sections we present this linear System T , which we call System L, and prove its main properties: subject reduction, strong normalisation, confluence and adequacy of the closed reduction strategy. This chapter is based on results presented in (Alves et al., 2006b).

110

6.2

Chapter 6. Linear Lambda Calculus with Iterators

Recursors versus Iterators

We start by showing that, in System T , bounded recursion can be easily encoded by iteration. The definition, reduction rules and typing rule for an iterator follow: If M is a term of type Nat, and N, P have types τ and τ → τ respectively, then iter M N P is a term of type τ . The reduction rules for the iterator are: iter 0 N P iter S(M ) N P

−→ N −→ P (iter M N P )

with the following typing rule: Γ ⊢T M : Nat

Θ ⊢T N : τ

∆ ⊢T P : τ → τ

Γ, Θ, ∆ ⊢T iter M N P : τ There are standard results that show that the more constrained iterator has all the computational power of the recursor. These results are not new (Girard et al., 1989), but they are an important part of our construction, therefore we recall the encodings in both directions. Lemma 6.2.1 An iterator can be encoded with a recursor, preserving types and reductions. Proof: Define: def

iter M N P = rec M N (λxy.P x). We proceed by induction on M . iter 0 N P

= −→

iter S(M ) N P

= rec S(M ) N (λxy.P x) −→ (λxy.P x)(rec M N (λxy.P x))M −→∗ P (rec M N (λxy.P x)) (I.H.)

=

rec 0 N (λxy.P x) N

P (iter M N P )

For types we need to show that: Γ, Θ, ∆ ⊢T iter M N P : τ ⇒ Γ, Θ, ∆ ⊢T rec M N (λxy.P x) : τ If Γ, Θ, ∆ ⊢T iter M N P : τ then we have: Γ ⊢T M : Nat, Θ ⊢T N : τ , and ∆ ⊢T P : τ → τ . Therefore Γ, Θ, ∆ ⊢T rec M N (λxy.P x) : τ (see Figure 6.1). 

6.2. Recursors versus Iterators

111 · · ·

∆ ⊢T P : τ → τ

x : τ ⊢T x : τ

∆, x : τ ⊢T P x : τ · · ·

· · · Γ ⊢T M : Nat

Θ ⊢T N : τ

∆, x : τ, y : Nat ⊢T P x : τ ∆ ⊢T λxy.P x : τ → Nat → τ

Γ, Θ, ∆ ⊢T rec M N (λxy.P x) : τ Figure 6.1: Typing rec 0 N (λxy.P x) in System T

Lemma 6.2.2 A recursor can be encoded with an iterator, preserving types and reductions. Proof: Define: def

rec M N P = π1 (iter M hN, 0i (λx.hP (π1 x)(π2 x), S(π2 x)i)). We prove the slightly stronger result:

hrec M N P , M i = iter M hN, 0i (λx.hP (π1 x)(π2 x), S(π2 x)i) by induction on M . hrec 0 N P , 0i

= iter 0 hN, 0i (λx.hP (π1 x)(π2 x), S(π2 x)i) −→ hN, 0i

hrec S(M ) N P , S(M )i

= iter S(M ) hN, 0i (λx.hP (π1 x)(π2 x), S(π2 x)i) −→ (λx.hP (π1 x)(π2 x), S(π2 x)i) (iter M hN, 0i (λx.hP (π1 x)(π2 x), S(π2 x)i)) −→ (λx.hP (π1 x)(π2 x), S(π2 x)i)hrec M N P , M i −→ hP (π1 hrec M N P , M i)(π2 hrec M N P , M i), S(π2 hrec M N P , M i)i −→ hP (rec M N P )M , S(M )i

Now taking the first projection of each side gives the required result. For types we need to show that: Γ, Θ, ∆ ⊢T rec M N P : τ implies:

Γ, Θ, ∆ ⊢T π1 (iter M hN, 0i (λx.hP (π1 x)(π2 x), S(π2 x)i)) : τ This is easy (see Figure 6.2) observing that if Γ, Θ, ∆ ⊢T rec M N P : τ , then we have:

112

Chapter 6. Linear Lambda Calculus with Iterators

• Γ ⊢T M : Nat • Θ ⊢T N : τ • ∆ ⊢T P : τ → Nat → τ

Let Σ = {x : τ × Nat}, we have: · · · ∆ ⊢T P : τ → Nat → τ

Σ ⊢T x : τ × Nat

Σ ⊢T x : τ × Nat

Σ ⊢ T π1 x : τ

Σ ⊢T π2 x : Nat

∆, Σ ⊢T P (π1 x)(π2 x) : τ

· · · Σ ⊢T S(π2 x) : Nat

∆, Σ ⊢T hP (π1 x)(π2 x), S(π2 x)i : τ × Nat ∆ ⊢T λx.(hv(π1 x)(π2 x), S(π2 x)i) : (τ × Nat) → (τ × Nat) · · ·

· · · Γ ⊢T M : Nat

Θ ⊢T N : τ · · · Θ ⊢T hN, 0i : τ × Nat

· · ·

∆ ⊢T iter M hN, 0i λx.(hP (π1 x)(π2 x), S(π2 x)i) : τ × Nat

∆ ⊢T π1 (iter M hN, 0i λx.(hP (π1 x)(π2 x), S(π2 x)i)) : τ Figure 6.2: Typing π1 (iter M hN, 0i λx.(hP (π1 x)(π2 x), S(π2 x)i)) in System T

 In the rest of this thesis, when we refer to System T , it will be the system with iterators rather than recursors (this system is also confluent, strongly normalising, and type preserving, as the proofs for System T in (Girard et al., 1989), can be adapted for the system using an iterator).

6.3

Linear λ-calculus with Iterator: System L

In this section we extend the linear λ-calculus, ΛL , defined in Chapter 2, with numbers, booleans, pairs, and an iterator, we define a reduction strategy inspired by closed reduction (Fern´ andez et al., 2005; Girard, 1989), and we also define a type system for this calculus. We call this typed calculus, System L.

Definition 6.3.1 Let V be an infinite set of variables. The set of linear terms ΛL , is

6.3. Linear λ-calculus with Iterator: System L

113

inductively defined from V in the following way: (Variables) (Application) (Abstraction) (Pairs) (Splitter) (Booleans) (Conditional) (Numbers) (Iterator)

x∈V M, N ∈ ΛL , fv(M ) ∩ fv(M ) = ∅ M ∈ ΛL , x ∈ fv(M ) M, N ∈ ΛL , fv(M ) ∩ fv(M ) = ∅ M, N ∈ ΛL , fv(M ) ∩ fv(N ) = ∅, x, y ∈ fv(N )

⇒ ⇒ ⇒ ⇒

x ∈ ΛL (M N ) ∈ ΛL (λx.M ) ∈ ΛL hM, N i ∈ ΛL

⇒ let hx, yi = M in N ∈ ΛL true, false ∈ ΛL

M, N, P ∈ ΛL , fv(N ) = fv(P ), fv(M ) ∩ fv(N ) = ∅ ⇒ cond M N P ∈ ΛL 0 ∈ ΛL M ∈ ΛL ⇒ S(M ) ∈ ΛL M, N, P ∈ ΛL , fv(M ) ∩ fv(N ) = ∅, fv(N ) ∩ fv(P ) = fv(M ) ∩ fv(P ) = ∅ ⇒ iter M N P ∈ ΛL

Note that, the definition of ΛL differs from the definition of terms for System T , by imposing conditions on the free variables of the terms, which guarantee linearity. Also, projecting from a pair is done using a splitter, which forces both the components of the pair to be used. Example 6.3.2 Addition and multiplication can be defined in ΛL the following way: add = λmn.iter m n λx.S(x) mult = λmn.iter m 0 (add n) The notion of free variables in ΛL extends the notion for Λ as follows. Definition 6.3.3 Let M be in ΛL , the set fv(M ) of free variables of M is inductively defined as follows: fv(x) = {x} fv(M N ) = fv(M ) ∪ fv(N ) fv(λx.M ) = fv(M ) \ {x} fv(hM, N i) = fv(M ) ∪ fv(N ) fv(let hx, yi = M in N ) = fv(M ) ∪ (fv(N ) \ {x, y}) fv(true) = ∅ fv(false) = ∅ fv(cond M N P ) = fv(M ) ∪ fv(N ) fv(0) = ∅ fv(S(M )) = fv(M ) fv(iter M N P ) = fv(M ) ∪ fv(N ) ∪ fv(P )

114

Chapter 6. Linear Lambda Calculus with Iterators

The notion of bound variables is extended to ΛL in the obvious way. Definition 6.3.4 (Substitution) Let M be in ΛL , and z be in fv(M ). The result of substituting the free occurrences of z by L in M (denoted by M [L/z]) is defined as: z[L/z] S(M )[L/z] (M N )[L/z] (M N )[L/z] (λx.M )[L/z] hM, N i[L/z] hM, N i[L/z] (let hx, yi = M in N )[L/z] (let hx, yi = M in N )[L/z] (cond M N P )[L/z] (cond M N P )[L/z] (iter M N P )[L/z] (iter M N P )[L/z] (iter M N P )[L/z]

≡ ≡ ≡ ≡ ≡ ≡ ≡ ≡ ≡ ≡ ≡ ≡ ≡ ≡

L S(M [L/z]) (M [L/z])N M (N [L/z]) λx.M [L/z] hM [L/z], N i hM, N [L/z]i let hx, yi = M [L/z] in N let hx, yi = M in N [L/z] cond M [L/z] N P cond M N [L/z] P [L/z] iter M [L/z] N P iter M N [L/z] P iter M N P [L/z]

if z ∈ fv(M ) if z ∈ fv(N ) if if if if if if if if if

z z z z z z z z z

∈ fv(M ) ∈ fv(N ) ∈ fv(M ) ∈ fv(N ) ∈ fv(M ) ∈ fv(N ) ∈ fv(M ) ∈ fv(N ) ∈ fv(P )

As before, we do not distinguish terms that are α-congruent, and we assume the same variable convention, regarding free and bound variables in a term.

Definition 6.3.5 (Closed reduction) The notion of reduction on ΛL is defined by the following contraction rules: Beta: Let: Cond: Cond: Iter: Iter:

(λx.M )N let hx, yi = hN, P i in M cond true M N cond false M N iter 0 N P iter S(M ) N P

−→ −→ −→ −→ −→ −→

M [N/x], M [N/x][P/y] M N N P (iter M N P )

fv(N ) = ∅ fv(N ) = fv(P ) = ∅

fv(P ) = ∅ fv(P ) = ∅

As before, reductions can take place in any context. Reduction is closed, thus the set of normal forms is not the same as for the λ-calculus. For example, λx.(λy.y)x is a normal form. Note that all the substitutions created during reduction (rules Beta, Let) are closed; this corresponds to a closed-argument reduction strategy (called ca in (Fern´ andez et al., 2005)). Also note that Iter rules are only triggered when the function v is closed.

6.3. Linear λ-calculus with Iterator: System L

115

Lemma 6.3.6 (Correctness of Substitution) Let M and N be in ΛL , z ∈ fv(M ), and fv(N ) = ∅, then M [N/z] is in ΛL . More generally, if fv(M ) ∩ fv(N ) = ∅ then M [N/z] is in ΛL . Proof: By induction on the structure of M . • z[N/z] = N . Trivial. • (S(P ))[N/z] = S(P [N/z]). S(P [N/z]) is in ΛL .

P [N/z] is in ΛL by induction since z ∈ fv(P ), thus

• (P Q)[N/z] = (P [N/z])Q if z ∈ fv(P ). The term P [N/z] is in ΛL by induction, since z ∈ fv(P ) and fv(N ) = ∅. By assumption fv(P ) ∩ fv(Q) = ∅, thus ((fv(P ) − {z}) ∪ fv(N )) ∩ fv(Q) = ∅ and therefore (P [N/z])Q is in ΛL . • (P Q)[N/z] = P (Q[N/z]) if z ∈ fv(Q). Similar to the previous case. • (λx.P )[N/z] = λx.P [N/z]. The term P [N/z] is in ΛL since z ∈ fv(P ) and fv(N ) = ∅. Since x ∈ fv(P ), then x ∈ fv(P ) − {z}, thus λx.P [N/z] is in ΛL . • hP, Qi[N/z] = hP [N/z], Qi if z ∈ fv(P ). The term P [N/z] is in ΛL , since z ∈ fv(P ) and fv(N ) = ∅. By assumption fv(P ) ∩ fv(Q) = ∅, thus (fv(P ) − {z}) ∩ fv(Q) = ∅ and therefore hP [N/z], Qi is in ΛL . • hP, Qi[N/z] = hP, Q[N/z]i if z ∈ fv(Q). Similar to the previous case. • (let hx, yi = P in Q)[N/z] = let hx, yi = (P [N/z]) in Q if z ∈ fv(Q). The term P [N/z] is in ΛL , since z ∈ fv(P ) and fv(N ) = ∅. By assumption fv(P ) ∩ fv(Q) = ∅, thus (fv(P ) − {z}) ∩ fv(Q) = ∅. Since x, y ∈ fv(Q) then let hx, yi = P [N/z] in Q is in ΛL . • (let hx, yi = P in Q)[N/z] = let hx, yi = P in (Q[N/z]) if z ∈ fv(Q). Similar to the previous case, noticing that since x, y ∈ / fv(let hx, yi = P in Q), and z ∈ fv(let hx, yi = P in Q), then z 6= x, y, thus x, y ∈ (fv(Q) − {z}). • (cond P Q R)[N/z] = cond (P [N/z]) Q R, if z ∈ fv(P ). The term P [N/z] is in ΛL , since z ∈ fv(P ) and fv(N ) = ∅. By assumption fv(P ) ∩ fv(Q) = ∅, thus (fv(P ) − {z}) ∩ fv(Q) = ∅. Since fv(Q) = fv(R) by assumption, then cond (P [N/z]) Q R is in ΛL . • (cond P Q R)[N/z] = cond P (Q[N/z]) (R[N/z]), if z ∈ fv(Q). The terms Q[N/z] and R[N/z] are in ΛL , since z ∈ fv(Q) = fv(R) and fv(N ) = ∅. By assumption fv(P ) ∩ fv(Q) = ∅, thus fv(P ) ∩ (fv(Q) − {z}) = ∅. Since fv(Q) = fv(R) by assumption, then (fv(Q) − {z}) = (fv(R) − {z}), thus cond P (Q[N/z]) (R[N/z]) is in ΛL .

116

Chapter 6. Linear Lambda Calculus with Iterators

• (iter P Q R)[N/z] = iter (P [N/z]) Q R, if z ∈ fv(P ). The term P [N/z] is in ΛL , since z ∈ fv(P ) and fv(N ) = ∅. By assumption fv(P ) ∩ fv(Q) ∩ fv(R) = ∅, thus (fv(P ) − {z}) ∩ fv(Q) ∩ fv(R) = ∅. Then iter (P [N/z]) Q R is in ΛL . • (iter P Q R)[N/z] = iter P (Q[N/z]) R, if z ∈ fv(Q). Similar to the previous case. • (iter P Q R)[N/z] = iter P Q (R[N/z]), if z ∈ fv(R). Similar to the previous case. 

6.3.1

Correctness of −→

Lemma 6.3.7 (Correctness of −→) Let M be in ΛL , and M −→ N , then: 1. fv(M ) = fv(N ); 2. N is in ΛL . Proof: We prove these properties by simultaneous induction, showing that all the rewrite rules preserve the variable constraints for terms in Definition 6.3.1. • (λx.P )Q −→ P [Q/x] if fv(Q) = ∅. By assumption, x ∈ fv(P ), P , Q are in ΛL , and fv(Q) = ∅. Thus P [Q/x] is in ΛL by Lemma 6.3.6, and fv((λx.P )Q) = fv(P [Q/x]). • let hx, yi = hP, Qi in R → (R[P/x])[Q/y] follows by the application of Lemma 6.3.6 twice, observing that fv(P ) = fv(Q) = ∅. • cond true P Q → P . Straightforward considering that fv(P ) = fv(Q). • cond false P Q → Q. Similar to the previous case. • iter 0 Q R → Q. Straightforward since fv(R) = ∅. • iter S(P ) Q R → R(iter P Q R). Since fv(R) = ∅, we get fv(iter P Q R) ∩ fv(R) = ∅, therefore R(iter P Q R) is in ΛL . Also, fv(iter S(P ) Q R) = fv(R(iter P Q R)).  Note that reduction preserves the free variables of the term, but the free variables of a subterm may change. In particular, a subterm of the form iter P Q R may become closed after a reduction in a superterm, triggering in this way a reduction with an Iter rule. For example the term M ≡ (λx.iter S 2 (0) y x)(λy.y). The x variable occurs free in the subterm iter S 2 (0) y x, which becomes closed after a β-reduction, thus allowing the iterator to be reduced (note that x is not a free variable of M ).

6.3. Linear λ-calculus with Iterator: System L

6.3.2

117

System L

Unlike the linear λ-calculus, where all terms are strongly normalising, terms in ΛL , are not always normalising. For example, the term ΩL ≡ (λz.iter S2 (0) (λx1 x2 .x1 x2 ) (λx.xz))(λz.iter S2 (0) (λx1 x2 .x1 x2 ) (λx.xz)) is such that ΩL −→∗ ΩL −→∗ · · · . In this section we present a type system for ΛL , such that, typable ΛL terms are strongly normalising. We call this system System L. Definition 6.3.8 The set of System L types, TL , is defined from two basic types Nat and Bool in the following way:

τ1 , τ2 ∈ TL τ1 , τ2 ∈ TL

Nat, Bool ∈ TL ⇒ (τ1 −◦ τ2 ) ∈ TL ⇒ (τ1 ⊗ τ2 ) ∈ TL

Definition 6.3.9 Let τ0 , . . . , τn be a non-empty list of types. Then It(A0 , . . . , An ) denotes a non-empty set of types, here called iterative types, defined by induction on n in the following way: n = 0 : It(τ0 ) = {τ0 −◦ τ0 } n = 1 : It(τ0 , τ1 ) = {τ0 −◦ τ1 } n ≥ 2 : It(τ0 , . . . , τn ) = It(τ0 , . . . , τn−1 ) ∪ {τn−1 −◦ τn } Iterative types type functions used in iterators. Note that It(τ0 ) = It(τ0 , τ0 ) = It(τ0 , . . . , τ0 ). The notions of statement, declaration are similar to the notions defined for System T in Chapter 2. Definition 6.3.10 Let x1 , . . . , xn be term variables in V, and τ1 , . . . , τn be types in TL . • A basis Γ is a sequence of the form x1 : τ1 , . . . , xn : τn , such that all xi are distinct. • We denote by dom(Γ) the set of variables such that xi : τi ∈ Γ. Notation 6.3.11 We will use the following abbreviation: Γ ⊢L t : It(τ0 , . . . , τn ) if and only if Γ ⊢L t : τ for each τ ∈ It(τ0 , . . . , τn ). We now present the typing rules for System L.

118

Chapter 6. Linear Lambda Calculus with Iterators

Definition 6.3.12 We say that M has type τ given the basis Γ, and write Γ ⊢L M : τ, if Γ ⊢L M : τ can be obtained from the following derivation rules: Axiom and Structural Rule: x : τ ⊢L x : τ

(Axiom)

Γ, x : τ1 , y : τ2 , ∆ ⊢L M : τ Γ, y : τ2 , x : τ1 , ∆ ⊢L M : τ

(Exchange)

Logical Rules: Γ, x : τ1 ⊢L M : τ2 Γ ⊢L λx.M : τ1 −◦ τ2

Γ ⊢L M : τ1

∆ ⊢ L N : τ2

Γ, ∆ ⊢L hM, N i : τ1 ⊗ τ2

Γ ⊢L M : τ1 −◦ τ2

(−◦Intro)

(⊗Intro)

Γ, ∆ ⊢L M N : τ2

Γ ⊢ L M : τ1 ⊗ τ2

Γ ⊢L M : Nat

(−◦Elim)

x : A, y : B, ∆ ⊢L N : τ

Γ, ∆ ⊢L let hx, yi = M in N : τ

Numbers: ⊢L 0 : Nat

∆ ⊢ L N : τ1

(Zero)

Γ ⊢L M : Nat Γ ⊢L S(M ) : Nat

Θ ⊢L N : τ0

(⊗Elim)

(Succ)

∆ ⊢L P : It(τ0 , . . . , τn )

(⋆)

Γ, Θ, ∆ ⊢L iter M N P : τn

(Iter)

(⋆) where if M ≡ Sm (0) then n = m otherwise n = 0 Booleans: (True) (False) ⊢L true : Bool ⊢L false : Bool ∆ ⊢L M : Bool

Γ ⊢L N : τ

Γ ⊢L P : τ

Γ, ∆ ⊢L cond M N P : τ

(Cond)

Note that the only structural rule is Exchange. We are in a linear system, therefore we do not have Weakening and Contraction rules. For the same reason, the logical rules split the context between the premises. The rules for numbers are standard. In the case of a term of the form iter M N P , we check that M is a term of type Nat. Concerning N and P in iter M N P there are two cases: if M is Sn 0 then we require P to be a function that can be iterated n times on N . Otherwise, if M is not (yet) a number, we require P to have a type that allows it to be iterated any number of times (i.e. N has type τ and P : τ −◦ τ , for some type τ ). The typing of iterators is therefore more flexible than in System T , but we will see that this extra flexibility does not compromise the confluence and strong normalisation of the system. Since there are no Weakening and Contraction rules, we have:

6.3. Linear λ-calculus with Iterator: System L

119

Lemma 6.3.13 If Γ ⊢L M : τ then dom(Γ) = fv(M ). Proof: Straightforward induction on the type derivation for Γ ⊢L M : τ .

6.3.3



Subject Reduction

Here we show that System L has subject reduction, that is, the reduction relation in Definition 6.3.5, preserves type derivations. Lemma 6.3.14 (Substitution) If Γ, x : τ ′ ⊢L M : τ and ∆ ⊢L N : τ ′ , where fv(M ) ∩ fv(N ) = ∅, then Γ, ∆ ⊢L M [N/x] : τ . Proof: By induction on the length of the type derivation Γ, x : τ ′ ⊢L M : τ . Note that, by Lemma 6.3.13, x ∈ fv(M ). We distinguish cases depending on the last typing rule applied. • Exchange: the property follows by induction. • Axiom: M = x, then having x : τ ⊢L x : τ and ∆ ⊢L N : τ we have ∆ ⊢L x[N/x] : τ • Succ: M = S(P ), and Γ, x : τ ′ ⊢L S(P ) : Nat, with Γ, x : τ ′ ⊢L P : Nat. By induction hypothesis Γ, ∆ ⊢L P [N/x] : Nat. Thus, since S(P )[N/x] = S(P [N/x]) we have Γ, ∆ ⊢L S(P )[N/x] : Nat. • −◦Intro: M = λy.P , we have Γ, x : τ ′ ⊢L λy.P : τ1 −◦ τ2 , if Γ, x : τ ′ , y : τ1 ⊢L P : τ2 . By induction hypothesis: Γ, y : τ2 , ∆ ⊢L P [N/x] : τ2 Thus, since (λy.P )[N/x] = λy.(P [N/x]) we have Γ, ∆ ⊢L (λy.P )[N/x] : τ1 −◦ τ2 . • −◦Elim: M = P Q and x ∈ fv(s), we have Γ1 , Γ2 , x : τ ′ ⊢L P Q : τ2 if Γ1 , x : τ ′ ⊢L P : τ1 −◦ τ2 and Γ2 ⊢L Q : τ1 . By induction hypothesis Γ1 , ∆ ⊢L P [N/x] : τ1 −◦ τ2 Thus, since P Q[N/x] = (P [N/x])Q we have Γ1 , Γ2 , ∆ ⊢L (P Q)[N/x] : τ2 Analogous for P Q where x ∈ fv(Q) and P Q[N/x] = P (Q[N/x]).

120

Chapter 6. Linear Lambda Calculus with Iterators

• ⊗Intro: M = hP, Qi and x ∈ fv(P ), we have Γ1 , Γ2 , x : τ ′ ⊢L hP, Qi : τ1 ⊗ τ2 if Γ1 , x : τ ′ ⊢L P : τ1 and Γ2 ⊢L Q : τ2 . By induction hypothesis Γ1 , ∆ ⊢L P [N/x] : τ1 Thus, since hP, Qi[N/x] = hP [N/x], Qi we have Γ1 , Γ2 , ∆ ⊢L hP, Qi[N/x] : τ1 ⊗ τ2 Analogous for hP, Qi where x ∈ fv(Q) and hP, Qi[N/x] = hP, Q[N/x]i. • ⊗Elim: M = let hy, zi = P in Q and x ∈ fv(P ), we have Γ1 , Γ2 , x : τ ′ ⊢L let hy, zi = P in Q : τ , if Γ1 , x : τ ′ ⊢L s : τ1 ⊗ τ2 and Γ2 , y : τ1 , z : τ2 ⊢L Q : τ . By induction hypothesis Γ1 , ∆ ⊢L P [N/x] : τ1 ⊗ τ2 . Thus, since (let hy, zi = P in Q)[N/x] = let hy, zi = (P [N/x]) in Q we have Γ1 , Γ2 , ∆ ⊢L (let hy, zi = P in Q)[N/x] : τ Analogous for let hy, zi = P in Q where x ∈ fv(Q) and (let hy, zi = P in Q)[N/x] = let hy, zi = P in (Q[N/x]). • Cond: M = cond P Q R and x ∈ fv(P ), we have Γ1 , Γ2 , x : τ ′ ⊢L cond P Q R : τ , if Γ1 , x : τ ′ ⊢L P : Bool, Γ2 ⊢L Q : τ and Γ2 ⊢L R : τ . By induction hypothesis Γ1 , ∆ ⊢L P [N/x] : Bool. Thus, since (cond P Q R)[N/x] = cond (P [N/x]) Q R we have Γ1 , Γ2 , ∆ ⊢L (cond P Q R)[N/x] : τ . Analogous for cond P Q R where x ∈ fv(Q) = fv(R), and (cond P Q R)[N/x] = cond P (Q[N/x]) (R[N/x]). • Iter: M = iter P Q R and x ∈ fv(P ). Note that in this case P is not of the form Sm (0) since it has a free variable. Then we have Γ1 , Γ2 , Γ3 , x : τ ′ ⊢L iter P Q R : τ , if Γ1 , x : τ ′ ⊢L P : Nat, Γ2 ⊢L Q : τ and Γ3 ⊢L R : It(τ ). Then, (iter P Q R)[N/x] = iter (P [N/x]) Q R. By induction hypothesis Γ1 , ∆ ⊢L P [N/x] : Nat, and since It(τ ) = It(τ, . . . , τ ) we can deduce Γ1 , ∆, Γ2 , Γ3 ⊢L (iter P Q R)[N/x] : τ . Analogous for iter P Q R where x ∈ fv(Q) and (iter P Q R)[N/x] = iter P (Q[N/x]) R, and where x ∈ fv(R) and (iter P Q R)[N/x] = iter P Q (R[N/x]). 

Theorem 6.3.15 (Subject Reduction) If Γ ⊢L M : τ and M −→ N , then Γ ⊢L N : τ . Proof: By induction on the type derivation Γ ⊢L M : A. We distinguish cases according to the last rule applied. If the rule is Exchange the property follows directly by induction. Otherwise, since M cannot be a variable, true, false or 0, the only interesting cases are:

6.3. Linear λ-calculus with Iterator: System L

121

1. M = λx.P and the derivation ends in −◦Intro. Then reduction can only take place inside P , so the property holds by induction. 2. The cases M = hP, Qi or M = S(P ) are analogous. 3. M = let hx, yi = hP, Qi in R, and there is a derivation ending in: Γ ⊢L hP, Qi : τ1 ⊗ τ2

∆, x : τ1 , y : τ2 ⊢L R : τ

Γ, ∆ ⊢L let hx, yi = hP, Qi in R : τ If the reduction takes place inside P , Q or R, the property holds directly by induction. Otherwise, since Γ ⊢L hP, Qi : τ1 ⊗ τ2 implies Γ′ ⊢L P : τ1 and Γ′′ ⊢L Q : τ2 where Γ′ ∪ Γ′′ = Γ, we can obtain Γ, ∆ ⊢L R[P/x][Q/y] : τ using the Substitution Lemma 6.3.14 (note that fv(P ) = fv(Q) = ∅ to apply the Let rule). 4. If M = (λx.P )Q, then there is a derivation ending in: Γ ⊢L λx.P : τ1 −◦ τ2

∆ ⊢ L Q : τ1

Γ, ∆ ⊢L (λx.P )Q : τ2 If the reduction takes place inside P or Q, the property holds directly by induction. Otherwise, since Γ ⊢L λx.P : τ1 −◦ τ2 implies Γ, x : τ1 ⊢L P : τ2 , we can obtain Γ, ∆ ⊢L P [Q/x] : τ2 using the substitution Lemma 6.3.14 (note that fv(Q) = ∅ to apply Beta). 5. If M = cond P Q R, then there is a derivation ending in: Γ ⊢L P : Bool ∆ ⊢L Q : τ

∆ ⊢L R : τ

Γ, ∆ ⊢L cond P Q R : τ If the reduction takes place inside P , Q, or R the property holds directly by induction. Otherwise, if P ≡ true (similar if P ≡ false), then there is a derivation ending in ⊢L true : Bool ∆ ⊢L Q : τ

∆ ⊢L R : τ

∆ ⊢L cond P Q R : τ Thus M −→ Q, and ∆ ⊢L Q : τ (similar for M −→ R). 6. M = iter P Q R. If the reduction takes place inside Q or R the property follows directly by induction. If the reduction takes place inside P , then P cannot be a term of the form Sn (0) and therefore the typing derivation has the form: Γ ⊢L t : Nat

Θ ⊢ L Q : τ0

∆ ⊢L R : It(τ0 )

Γ, Θ, ∆ ⊢L M : τ0 P′

By induction, the reduct of P still has type Nat, and note that It(τ0 ) = It(τ0 , . . . , τ0 ), thus we can still deduce Γ, Θ, ∆ ⊢L iter P ′ Q R : τ0 . Finally, if the reduction takes place at the root, we have two cases:

122

Chapter 6. Linear Lambda Calculus with Iterators

(a) iter 0 Q R −→ Q if fv(R) = ∅. Then, by Lemma 6.3.13, Γ = fv(iter 0 Q R) = fv(Q), and the type derivation has conclusion Γ ⊢L iter 0 Q R : τ0 with premises: ⊢L 0 : Nat, Γ ⊢L Q : τ0 , ⊢L R : It(τ0 ). Therefore the property holds. (b) iter S(P ) Q R −→ R(iter P Q R) if fv(R) = ∅. Then there is a derivation ending in Γ ⊢L S(P ) : Nat Θ ⊢L Q : τ0 ⊢L R : It(τ0 , . . . , τn ) Γ, Θ ⊢L iter S(P ) Q R : τn Sm (0).

where n = 0 if P 6= Therefore, also ⊢L R : It(τ0 , . . . , τn−1 ), and we can obtain Γ, Θ ⊢L iter P Q R : τn−1 . From these we deduce Γ, Θ ⊢L R(iter P Q R) : τn as required. 

6.3.4

Strong Normalisation

In System L, every reduction sequence is finite (that is, typable terms are strongly normalisable). We show the strong normalisation property for System L, based on the strong normalisation property for System T (Girard et al., 1989). We first define a translation from System L into System T . Definition 6.3.16 We define the compilation of terms in System L into System T , denoted J·K, in the following way: J0K JtrueK JfalseK JS(M )K JxK Jλy.M K JM N K JhM, N iK Jlet hx, yi = M in N K Jcond M N P K Jiter M N P K

= = = = = = = = = =

0 true false S(JM K) x λy.JM K JM KJN K hJM K, JN Ki JN K[(π1 JM K)/x][(π2 JM K)/y] cond JM K JN K JP K  JP Km (JN K) if M = Sm (0), m > 0 = iter JM K JN K JP K otherwise

Note that the translation of an iterator where the number of times to iterate is known and positive, develops this iteration. If it is zero or not known we use System T ’s iterator. When

6.3. Linear λ-calculus with Iterator: System L

123

the argument number of the iterator is unknown or zero, then the iterated function must be of type τ −◦ τ , and therefore the translation can be typed by the iterator rule of System T . Otherwise, the iterated function can be typed using iterative types, thus the translation into System T may not be typed by the System T iterator rule. Definition 6.3.17 We define the following translation from types in System L into types in System T : JNatK = Nat JBoolK = Bool Jτ1 −◦ τ2 K = Jτ1 K → Jτ2 K Jτ1 ⊗ τ2 K = Jτ1 K × Jτ2 K Also, if Γ = x1 : τ1 , . . . xn : τn , then JΓK = x1 : Jτ1 K, . . . xn : Jτn K. Lemma 6.3.18 Let M, N be terms in System L, such that fv(M ) ∩ fv(N ) = ∅. Then JM K[JN K/x] −→∗ JM [N/x]K. Proof: By induction on the structure of M . • M = x, then JxK[JN K/x] = x[JN K/x] = JN K

and

Jx[N/x]K = JN K

• M = S(P ), then JS(P )K[JN K/x]

= =

S(JP K)[JN K/x] S(JP K[JN K/x])

(I.H.)

−→∗ S(JP [N/x]K) = JS(P [N/x])K = JS(P )[N/x]K • M = λy.P (w.l.g. y 6∈ fv(N )), then Jλy.P K[JN K/x]

= =

(λy.JP K)[JN K/x] λy.(JP K[JN K/x])

(I.H.)

−→∗ λy.JP [N/x]K = Jλy.(P [N/x])K = J(λy.P )[N/x]K • M = P Q and x ∈ fv(P ), then JP QK[JN K/x]

= = (I.H.)

(JP KJQK)[JN K/x] (JP K[JN K/x])JQK

−→∗ JP [N/x]KJQK = JP [N/x]QK = J(P Q)[N/x]K Analogous for P Q where x ∈ fv(Q).

124

Chapter 6. Linear Lambda Calculus with Iterators

• M = hP, Qi. Similar to the previous cases, considering the cases for x ∈ fv(P ) and x ∈ fv(Q). • M = let hy, zi = P in Q. Similar to the previous cases, considering the cases for x ∈ fv(P ) and x ∈ fv(Q). • M = cond P Q R. Similar to the previous cases, considering the cases for x ∈ fv(P ) and x ∈ fv(Q) = fv(R). • M = iter P Q R. – If P = Sm (0) and x ∈ fv(Q), then Jiter Sm (0) Q RK[JN K/x]

= = (I.H.)

= = = =

JRKm (JQK)[JN K/x] JRKm (JQK[JN K/x]) JRKm (JQ[N/x]K) iter Sm (0) JQ[N/x]K JRK Jiter Sm (0) Q[N/x] RK J(iter Sm (0) Q R)[N/x]K

Analogous for iter Sm 0 Q R where x ∈ fv(R). – If P 6= Sm (0) and x ∈ fv(P ), then we have two cases: ∗ P [N/x] = Sk (0), thus JP [N/x]K = Sk (0) Jiter P Q RK[JN K/x]

= =

iter JP K JQK JRK[JN K/x] iter JP K[JN K/x] JQK JRK

(I.H.)

−→∗ iter JP [N/x]K JQK JRK −→∗ JRKk (JQK) = Jiter P [N/x] Q RK = Jiter P Q R[N/x]K ∗ Otherwise Jiter P Q RK[JN K/x]

= (I.H.) −→∗

=

iter JP K[JN K/x] JQK JRK iter JP [N/x]K JQK JRK Jiter (P [N/x]) Q RK = J(iter P Q R)[N/x]K

Analogous for iter P Q R where x ∈ fv(Q), and where x ∈ fv(R). 

Lemma 6.3.19 If Γ ⊢L M : τ , then JΓK ⊢T JM K : Jτ K Proof: By induction on Γ ⊢L M : τ . We only consider the cases for pair elimination and iter, because the other cases are trivial by induction.

6.3. Linear λ-calculus with Iterator: System L

125

• If M = let hx, yi = P in Q and we have Γ ⊢L P : τ1 ⊗ τ2

x : τ1 , y : τ2 , ∆ ⊢L Q : τ

Γ, ∆ ⊢L let hx, yi = P in Q : τ By induction hypothesis we have: – JΓK ⊢T JP K : Jτ1 K × Jτ2 K – x : Jτ1 K, y : Jτ2 K, J∆K ⊢T JQK : Jτ K By ×Elim we have: JΓK ⊢T π1 (JP K) : Jτ1 K and JΓK ⊢T π2 (JP K) : Jτ2 K. Thus, by the substitution Lemma 6.3.14, and x : Jτ1 K, y : Jτ2 K, J∆K ⊢T JQK : Jτ K we have JΓK, J∆K ⊢T (JQK[π1 (JP K)/x])[π2 (JP K)/y] : Jτ K • If M = iter P Q R we have two cases: 1. If P = Sm (0), m > 0, we have: ⊢L Sm (0) : Nat

Θ ⊢ L Q : τ0

Γ ⊢L R : It(τ0 , . . . , τm )

Θ, Γ ⊢L iter Sm (0) Q R : τm By induction: – JΘK ⊢T JQK : Jτ0 K – JΓK ⊢T JRK : Jτi K → Jτi+1 K Then Jiter Sm (0) Q RK is JRKm (JQK) and we derive in System T :

JΓK ⊢T JRK : Jτm−1 K → Jτm K

JΓK ⊢T JRK : Jτ0 K → Jτ1 K JΘK ⊢T JQK : Jτ0 K · · · JΓK, . . . , JΓK, JΘK ⊢T JRKm−1 (JQK) : Jτm−1 K

JΓK, . . . , JΓK, JΘK ⊢T JRKm (JQK) : Jτm K JΓK, JΘK ⊢T JRKm (JQK) : Jτm K 2. Otherwise iter P Q R is typed in the following way: ∆ ⊢L P : Nat

Θ ⊢L Q : τ

Γ ⊢L R : τ −◦ τ

∆, Θ, Γ ⊢L iter P Q R : τ By induction: – J∆K ⊢T JP K : Nat – JΘK ⊢T JQK : Jτ K – JΓK ⊢T JRK : Jτ K → Jτ K

126

Chapter 6. Linear Lambda Calculus with Iterators

Then Jiter P Q RK is iter JP K JQK JRK and we derive in System T : J∆K ⊢T JP K : Nat

JΘK ⊢T JQK : Jτ K

JΓK ⊢T JRK : Jτ K → Jτ K

J∆, Θ, ΓK ⊢T iter JP K JQK JRK : Jτ K  We now show there is a relation between reduction steps in System L and reduction steps in System T . Notation 6.3.20 We use −→Iter to represent the reduction iter S m+1 0 Q R −→Iter R(iter S m 0 Q R). Lemma 6.3.21 Let M be a System L term. 1. If M −→Iter N , then JM K = JN K. 2. If M −→ N (by any rule other than −→Iter ), then JM K −→+ JN K. Proof: 1.

iter Sm+1 0 Q R

>

Iter

R(iter Sm 0 Q R)

J·K

∨ m+1

JRK

J·K (JQK)

∨ m

= JRK(JRK (JQK))

2. By inspection of the different reduction rules. • β-redexes: J(λx.P )QK

>

P [Q/x]

J·K

J·K ∨



(λx.JP K)JQK

>

JP K[JQK/x]

∗ > JP [Q/x]K (Lem 6.3.18)

• Pairs: let hx, yi = hP, Qi in R

>

J·K ∨

(JRK[π1 (JhP, QiK)/x])[π2 (JhP, QiK)/y]

+

>

(R[P/x])[Q/y]

J·K ∨ ∗ > JR[P/x][Q/y]K (JRK[JP K/x])[JQK/y] (Lem 6.3.18)

• Conditionals: cond true Q R

>

Q

J·K

J·K ∨

cond true JQK JRK

>

cond false Q R and

>

R J·K

J·K





JQK

cond false JQK JRK

∨ >

JRK

6.3. Linear λ-calculus with Iterator: System L

• Iterator: iter 0 Q R

>

Q

J·K

iter S(P ) Q R

J·K and ∨

iter 0 JQK JRK

>

127

>

R(iter P Q R)

J·K

J·K





JRK

iter S(JP K) JQK JRK

∨ >

JRK(iter JP K JQK JRK) 

Lemma 6.3.21 implies SN for every reduction sequence not involving −→Iter steps. We will now prove that −→Iter is also SN. In the proof, recall the notions on multisets, presented in Chapter 3. Also, consider the usual lexicographic order for pairs (Baader and Nipkow, 1998): Definition 6.3.22 Consider the set of non-negative integers Z∗ , and the set Ω = Z∗ ∪ {⊤}. Let > be the usual order relation on integers Z∗ . Let > on Ω be such that ∀x ∈ Z∗ , ⊤ > x. Let ≫ on Z∗ × Ω be defined as: (x1 , y1 ) ≫ (x2 , y2 ) if and only if x1 > x2 ∨ (x1 = x2 ∧ y1 > y2 ) Definition 6.3.23 For any M let iter depth(M ) be defined as: iter depth(0) = iter depth(true) = iter depth(false) = iter depth(x) = 0 iter depth(λy.M ) = iter depth(S(M )) = iter depth(M ) iter depth(M N ) = iter depth(hM, N i) = iter depth(let hx, yi = M in N ) = = max{iter depth(M ), iter depth(N )} iter depth(cond M N P ) = max{iter depth(M ), iter depth(N ), iter depth(P )} iter depth(iter M N P ) = max{iter depth(M ), iter depth(N ), iter depth(P )} + 1 Example 6.3.24 Consider the term M ≡ (iter (iter S 5 (0) 0 I) I I)(iter 0 true (iter S2 (0) I (iter 0 I I))) where I ≡ λx.x. Then iter depth(M ) = 3, which is the maximal nesting of iterators in M . Definition 6.3.25 For any M let |M | be a multiset over Z∗ × Ω defined as: |0| = |true| = |false| = |x| = ∅ |λy.M | = |S(M )| = |M | |M N | = |hM, N i| = |let hx, yi = M in N | = |M | ∪ |N | |cond M N P | =|M | ∪ |N | ∪ |P | |M | ∪ |N | ∪ {(iter depth(iter M N P ) + 1, m)} if n = Sm (0), m > 0 |iter M N P | = |M | ∪ |N | ∪ |P | ∪ {(iter depth(iter M N P ) + 1, ⊤)} otherwise

Given the set of terms Λ, let M(Λ) = {|M | | M ∈ Λ}.

128

Chapter 6. Linear Lambda Calculus with Iterators

The multiset |M | is such that, for any iterator N ≡ iter P Q R in M , there is a pair (d, n) in |M |, where d is iter depth(N ), and n is the number of times R will be iterated on Q, if that number is already known, or ⊤ otherwise. Example 6.3.26 For the term M in Example 6.3.24 we have |M | = {(1, 5), (2, ⊤), (1, 0), (2, 2), (3, 0)} Let >mul be the usual multiset extension to M(Λ) of the relation ≫ defined above. Since ≫ is well founded, so is >mul (see (Baader and Nipkow, 1998) for more details). Lemma 6.3.27 If M −→Iter M ′ then: 1. iter depth(M ) = iter depth(M ′ ) 2. |M | >mul |M ′ | Proof: By induction on the structure of M . • For M = 0, true, false, and x, it is trivial. • If M = λx.P , then M −→Iter λx.P ′ if P −→Iter P ′ . By induction hypothesis: 1. iter depth(P ) ≥ iter depth(P ′ ). Thus iter depth(λx.P ) = iter depth(P ) = iter depth(P ′ ) = iter depth(λx.P ′ ). 2. |P | >mul |P ′ |, thus |λx.P | = |P | >mul |P ′ | = |λx.P ′ | • M = S(P ). Similar to the previous case. • M = P Q, then we have two cases: – P Q −→Iter P ′ Q because P −→Iter P ′ . 1. By induction hypothesis: iter depth(P ) = iter depth(P ′ ). Then iter depth(P Q) = max{iter depth(P ), iter depth(Q)} = max{iter depth(P ′ ), iter depth(Q)} = iter depth(P ′ Q) 2. By induction hypothesis |P | >mul |P ′ |, thus ∃X, Y. |P ′ | = (|P | − X) ∪ Y . Consider X ′ = X, and Y ′ = Y , then (|P Q| − X ′ ) ∪ Y ′ = ((|P | ∪ |Q|) − X) ∪ Y (because X ⊆ |P |) = (|P | − X) ∪ |Q| ∪ Y = (|P | − X) ∪ Y ∪ |v| = |P ′ | ∪ |Q| = |P ′ Q| Thus |P Q| >mul |P ′ Q|.

6.3. Linear λ-calculus with Iterator: System L

129

– P Q −→Iter P Q′ because Q −→Iter Q′ . Similar to the previous case • M = hP, Qi. Similar to P Q. • M = let hx, yi = P in Q. Similar to the previous cases. • M = cond P Q R. Similar to the previous cases, but considering the three different reduction possibilities. • M = iter P Q R. Then we have: – iter Sm+1 (0) Q R −→Iter R(iter S m (0) Q R). 1. iter depth(iter Sm+1 (0) Q R) = max{iter depth(Q), iter depth(R)} + 1 = iter depth(R(iter S m (0) Q R)) 2. Let b = max{iter depth(Q), iter depth(R)}, and notice that for any (x, y) ∈ |R|, x < b + 1. |iter Sm+1 0 Q R| = |Q| ∪ |R| ∪ {(b + 1, m + 1)} m |R(iter S (0) Q R)| = |R| ∪ |Q| ∪ |R| ∪ {(b + 1, m)} Consider X = {(b + 1, m + 1)}, Y = |R| ∪ {(b + 1, m)} (|iter Sm+1 (0) Q R| − X) ∪ Y

= ((|Q| ∪ |R| ∪ {(b + 1, m + 1)})− {(b + 1, m + 1)}) ∪ (|R| ∪ {(b + 1, m)}) = |R| ∪ |Q| ∪ |R| ∪ {(b + 1, m)} = |R(iter S m−1 (0) Q R)|

– iter P Q R −→Iter iter P ′ Q R, because P −→Iter P ′ . 1. By induction iter depth(P ) = iter depth(P ′ ). iter depth(iter P Q R) = max{iter depth(P ), iter depth(Q), iter depth(R)} = max{iter depth(P ′ ), iter depth(Q), iter depth(R)} = iter depth(iter P ′ Q R) 2. Let b = max{iter depth(P ), iter depth(Q), iter depth(R)}, and notice that b = b′ , where b′ = max{iter depth(P ′ ), iter depth(Q), iter depth(R)}. Then |P | >mul |P ′ |, thus ∃X, Y. |P ′ | = (|P | − X) ∪ Y . |iter P Q R| = |P | ∪ |Q| ∪ |R| ∪ {(b + 1, ⊤)} |iter P ′ Q R| = |P ′ | ∪ |Q| ∪ |R| ∪ {(b + 1, ⊤)} Let X ′ = X and Y ′ = Y . Then (|iter P Q R| − X ′ ) ∪ Y ′ = |iter P ′ Q R|.

130

Chapter 6. Linear Lambda Calculus with Iterators

– iter P Q R −→Iter iter P Q′ R and iter P Q R −→Iter iter P Q R′ . Similar to the previous case. 

Example 6.3.28 Consider the term M of Example 6.3.24. There exist two different −→Iter reductions from M : M −→Iter M1 ≡ (iter (I(iter S 4 (0) 0 I)) I I)(iter 0 true (iter S2 (0) I (iter 0 I I))) M −→Iter M2 ≡ (iter (iter S 5 (0) 0 I) I I)(iter 0 true ((iter 0 I I)(iter S(0) I (iter 0 I I)))) iter depth(M1 ) = 3 = iter depth(M2 ), |M1 | = {(1, 4), (2, ⊤), (1, 0), (2, 2), (3, 0)} |M2 | = {(1, 5), (2, ⊤), (1, 0), (1, 0), (2, 1), (3, 0)} and |M | >mul |M1 |, and |M | >mul |M2 |. Corollary 6.3.29 For every term M in ΛL , every sequence of −→Iter starting from M is finite. Proof: By contradiction. Suppose there is an infinite sequence M1 −→Iter M2 −→Iter · · · By Lemma 6.3.27 there is an infinite descending chain |M1 | >mul |M2 | >mul · · · , which is absurd since >mul is well-founded.



Lemma 6.3.30 If JM K is strongly normalisable, then M is strongly normalisable. Proof: By contradiction. Assume that there is an infinite sequence starting from M in System L: M −→∗Iter M1 −→ t2 −→∗Iter M3 −→ M4 · · · Since −→Iter is terminating, an infinite derivation out of M must contain an infinite number of −→. By Lemma 6.3.21 we obtain an infinite sequence for JM K JM K = JM1 K −→+ JM2 K = JM3 K −→+ JM4 K · · · which gives a contradiction, because System T is strongly normalisable.



6.3. Linear λ-calculus with Iterator: System L

131

Theorem 6.3.31 (Strong Normalisation) If Γ ⊢L M : τ , then M is strongly normalisable. Proof: If Γ ⊢L M : τ then, by Lemma 6.3.19, Γ ⊢T JM K : Jτ K. Thus JM K is strongly normalisable which implies, by Lemma 6.3.30, that M is strongly normalisable. 

6.3.5

Confluence

System L is confluent, which implies that normal forms are unique. Confluence for System L is a direct consequence of strong normalisation and the fact that the rules are non-overlapping (using Newman’s Lemma (Newman, 1942)). In fact, even untyped terms (in ΛL ) are confluent. We prove confluence for hΛL , −→i in Appendix A, using the Tait-Martin-L¨ of method (note that we cannot apply directly Klop’s result (Klop et al., 1993) because we have conditional reductions). Theorem 6.3.32 (Confluence) Let M be in ΛL , if M −→∗ N1 and M −→∗ N2 , there exists a term N in ΛL , such that N1 −→∗ N and N2 −→∗ N . Proof: See Appendix A.

6.3.6



Adequacy

Theorem 6.3.33 (Adequacy) If M is closed System L term, then one of the following holds: • ⊢L M : Nat and t −→∗ Sn (0) • ⊢L M : Bool and t −→∗ true or t −→∗ false • ⊢L M : τ1 −◦ τ2 and t −→∗ λx.N for some term N . • ⊢L M : τ1 ⊗ τ2 and t −→∗ hP, Qi for some terms P, Q. Proof: By Lemma 6.3.13, typing judgements for M have the form ⊢L M : τ , and τ is either Nat, Bool, τ1 −◦ τ2 or τ1 ⊗ τ2 . By Subject Reduction, Strong Normalisation, and Lemma 6.3.7, we know that M has a closed, typable normal form N . We show the case when ⊢L N : Nat, the others follow with similar reasoning. Since N is a closed term of type Nat, it cannot be a variable, an

132

Chapter 6. Linear Lambda Calculus with Iterators

abstraction or a pair. Hence N is either an application, a pair spliter, a conditional, an iterator or a number. • Let N = N1 N2 . . . Nn , n ≥ 2, such that N1 is not an application. Then N1 is closed, and since N is typable, N1 must have an arrow type. But then by induction N1 is an abstraction, and then the Beta rule would apply, contradicting our assumptions. Thus N cannot be an application. • Let N = let hx, yi = P in Q. Then P is closed and fv(Q) = {x, y}. Since N is typable, P has type τ1 ⊗ τ2 , and by induction it should be a (closed) pair hP1 , P2 i. But then the Let rule would apply contradicting our assumptions. Thus N cannot be a let. • Let N = cond P Q R. Then P , Q, R are closed. Since N is typable, P must have a boolean type, and by induction it should be either true or false. But then the Cond rule would apply contradicting our assumptions. Thus N cannot be a cond. • Let N = iter P Q R. Since N is closed, so are P , Q and R. Since N is typable P must be a term of type Nat, and by induction, P is a number. But then the Iter rule would apply contradicting our assumptions. Thus u cannot be an iter. Thus, if ⊢L M : Nat then M reduces to a number.

6.4



Discussion

In this chapter we presented System L, a linear version of G¨ odel’s System T with closed reductions. System L has the subject reduction property, and it is both strong normalising and confluent. Our system is related to previous linear versions of System T (see for instance (Lago, 2005)). But in System L typing of iterators is more flexible, and we accept iterators on open linear functions (but only allow reduction of iterators when the functions becomes closed). In the next chapter we will study the expressive power of System L and further relations with System T .

Chapter 7

Expressiveness of System L 7.1

Introduction

In the previous chapter we present a linear version of G¨ odel’s System T , System L, which we proved to be confluent, strong normalising, and with the subject reduction property. The typing of iterators is more flexible than in other linear versions of System T : the function to be iterated can be typed by a set of types (which we called iterative types). We also defined a closed reduction strategy for System L. In this chapter we show how the design choices of System L influence the computational power of the system: it is possible to encode every System T term in System L. Previous definitions of linear systems were proved to be strictly less powerful than System T (Lago, 2005). For example, Dal Lago’s linear language H(∅) (Lago, 2005) exactly characterises primitive recursive functions. Closed reduction strategy is a key element to the power of System L. We will prove this claim, by analysing the interplay between linearity, iteration and closed reduction, and comparing the computational power of linear systems with and without closed reduction. This chapter is based on results presented in (Alves et al., 2006b) and (Alves et al., 2006a).

7.2

Primitive Recursion and Beyond

In this section we show how to define the primitive recursive functions in System L. We conclude the section indicating that we can encode more than primitive recursive functions.

134

Chapter 7. Expressiveness of System L

7.2.1

Primitive Recursive Functions

We start by recalling some notions on recursion theory. For a detailed reference see (Phillips, 1992). Primitive recursive functions are a class of total functions widely used in recursion theory. Definition 7.2.1 A function f : Natn → Nat is primitive recursive if it can be defined using: • the natural numbers built from 0 and the successor function S; • the projections: πi (x1 , . . . , xn ) = xi (1 ≤ i ≤ n); • composition; • and the primitive recursive scheme, which allows us to define a recursive function h using two auxiliary functions f , g: h(~x, 0) = f (~x) h(~x, S n) = g(~x, h(~x, n), n) Remark that in the last equation, the numbers ~x and n are copied.

7.2.2

Projecting linearly

Although System L is a linear calculus, numbers can be erased. We show how we can define projections over pairs. System L typings of the following terms are presented in Appendix B. Definition 7.2.2 We define the projection functions fst, snd : Nat ⊗ Nat −◦ Nat as follows: fst = λx.let hy, zi = x in iter z y (λw.w) snd = λx.let hy, zi = x in iter y z (λw.w) One can easily verify that for any pair of numbers these terms behave like the projection functions Lemma 7.2.3 For any numbers M and N , fsthM, N i −→∗ M and sndhM, N i −→∗ N. Proof: We show the case for fst (the case for snd is analogous). Let M = Sm (0), N = Sn (0). fsthM, N i

= −→ −→ −→∗

(λx.let hy, zi = x in iter z y (λw.w))hSm (0), Sm (0)i (let hy, zi = hSm (0), Sn (0)i in iter z y λw.w) iter Sn (0) Sm (0) λw.w Sm (0) = M 

7.2. Primitive Recursion and Beyond

7.2.3

135

Copying linearly

We can also copy natural numbers in this linear calculus. Definition 7.2.4 We define a function C : Nat −◦ Nat ⊗ Nat that given a number M returns the pair hN, N i: C = λx.iter x h0, 0i (λx.let hy, zi = x in hS(y), S(z)i). Lemma 7.2.5 For any number N ≡ Sn (0), CN −→∗ hN, N i. Proof: By induction on n. • Basis: C 0 −→ iter 0 h0, 0i (λx.let hy, zi = x in hS(y), S(z)i) −→ h0, 0i • Induction: n = t + 1 C(St+1 (0))

= −→

iter St+1 (0) h0, 0i (λx.let hy, zi = x in hS(y), S(z)i) (λx.let hy, zi = x in hS(y), S(z)i) (iter (St 0) h0, 0i (λx.let hy, zi = x in hS(y), S(z)i))

(I.H.)

−→∗ (λx.let hy, zi = x in hS(y), S(z)i)hSt (0), St (0)i −→∗ hS(St (0)), S(St (0))i = hSt+1 (0), St+1 (0)i  We can easily apply this technique to other inductively defined data structures (like any linear tuple or linear lists). Note that we do not need iterative types for this (the simple, monomorphic typing of iterators is sufficient). We will show in Section 7.3 that iterators will allow us to erase any closed term, and moreover copy any closed term. Example 7.2.6 Using the fact that we can copy and erase numbers, we are now able to define more examples of functions in System L. • Exponential: exp = λmn.iter n (S 0) (mult m) • Predecessor: pred = λn.fst(iter n h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)) • Is zero: is zero = λn.fst(iter n h0, S 0i (λx.C(snd x)))

136

Chapter 7. Expressiveness of System L

• Subtraction: sub = λmn.iter n m pred • Factorial: λn.snd(iter n hS(0), S(0)i (λx.let ht, ui = x in let ht1 , t2 i = C t in F )) where F = hS t1 , mult u (S t2 )i. It is easily proved by induction that the previous codifications are correct (see Appendix D, for some examples).

7.2.4

Primitive Recursive Functions

We have seen how one can encode projection in System L, and we trivially have composition. We now show how to encode the primitive recursive scheme using iter (see Appendix B for type derivations of primitive recursive functions in System L). First, assume h′ is defined by the following, simpler scheme, which uses n only once in the second equation: h′ (x, 0) = f (x) ′ h (x, S(n)) = g ′ (x, h′ (x, n)) Definition 7.2.7 Given the functions G′ : Nat −◦ Nat −◦ Nat −◦ Nat and F : Nat −◦ Nat, such that, for any numbers M ≡ Sm (0) and N ≡ Sucn (0): F M G′ M N

= f (M ) = g ′ (M, N )

let S be the term: λyz.let hz1 , z2 i = C z in Gz1 (yz2 ) : (Nat −◦ Nat) −◦ (Nat −◦ Nat) then H ′ M N = (iter N F S)M : Nat.

Lemma 7.2.8 For any M ≡ Sm (0) and N ≡ Sn (0), H ′ M N −→∗ h′ (M, N ).

Proof: By induction on n, using Lemma 7.2.5 to copy numbers: • Basis: (iter 0 F S)M

−→ F M = f (M ) = h′ (M, 0)

7.2. Primitive Recursion and Beyond

137

• Induction: n = t + 1 (iter Sk+1 (0) F S)M

−→∗ (let hz1 , z2 i = C z in G′ z1 (yz2 )) [(iter Sk (0) F S)/y, M/z] −→∗ let hz1 , z2 i = hM, M i in G′ z1 ((iter (Sk 0) F S)z2 ) −→ GM ((iter Sk (0) F S)M ) (I.H.)

=

g ′ (M, h′ (M, Sk (0))) = h′ (M, N ) 

To encode the standard primitive recursive scheme, which has an extra N in the last equation, we need to use Lemma 7.2.5 to copy N . Definition 7.2.9 Given the functions G : Nat −◦ Nat −◦ Nat −◦ Nat and F : Nat −◦ Nat, such that, for any numbers K, M and N : F M GKM N

= f (M ) = g(K, M, N )

Let

H M N = let hn1 , n2 i = C N in S (pred n1 ) M where S = iter n2 (λx1 .fsthF, x1 i) S ′ S ′ = λyx2 z.let hz1 , z2 i = C z in (let hw1 , w2 i = C x2 in G z1 (y (pred w1 ) z2 )w2 ) In Appendix D we prove that the encoding of predecessor function is correct, that is: pred 0 = 0 pred S(M ) = M Also, the predecessor function is a closed typable function in System L (see Appendix B). Lemma 7.2.10 For any M ≡ Sm (0) and N ≡ Sn (0), H M N −→∗ h(M, N ). Proof: Note that H M N −→∗ (iter N (λx1 .fsthF, x1 i) S ′ ) (pred N ) M. We now prove by induction on n that: iter N (λx1 .fsthF, x1 i) S ′ (pred N ) M −→∗ h(M, N ).

138

Chapter 7. Expressiveness of System L

• Basis: iter 0 (λx1 .fsthF, x1 i) S ′ (pred 0) M

−→ (λx1 .fsthF, x1 i) (pred 0) M −→∗ F M = f (M ) = h(M, 0)

• Induction: n = t + 1

−→ −→

iter St+1 (0) (λx1 .fsthF, x1 i) S ′ (pred St+1 (0)) M S ′ (iter St (0) (λx1 .fsthF, x1 i) S ′ )( St (0) M G M (iter St (0) (λx1 .fsthF, x1 i) S ′ pred(St (0)) M ) (St (0))

(I.H.)

−→∗ g(M, h(M, St (0)), St (0)) = h(M, Sn (0)) 

7.2.5

Beyond Primitive Recursion

System L encodes more than primitive recursive functions. We show it, by encoding in System L a standard example of a non primitive recursive function, the Ackermann’s function, defined as follows: ack(0, n) = Sn ack(S n, 0) = ack(n, S 0) ack(S n, S m) = ack(n, ack(S n, m)) In a higher-order functional language, we have an alternative definition: Let succ = λx.S x : Nat −◦ Nat, then ack(m, n) = a m n where a is the function defined by: a0 a (S n)

= succ = A (a n)

Ag0 = g(S 0) A g (S n) = g(A g n) Both definitions are equivalent: Lemma 7.2.11 If g = λy.ack(x, y) then A g n = ack(S x, n). Proof: By induction on n. • Basis: A g 0 = g(S 0) = ack(x, S 0) = ack(S x, 0)

7.2. Primitive Recursion and Beyond

139

• Induction n = k + 1: A g (S k) = = = =

g(A g k) g(ack(S x, k)) ack(x, ack(S x, k)) ack(S x, S k) 

Lemma 7.2.12 For any natural numbers m and n, a m n = ack(m, n). Proof: By induction on m • The case m = 0 is trivial. • Induction m = k + 1. By definition, a (S k) = A (a k), and by induction this is A(λy.ack(k, y)). Therefore by Lemma 7.2.11, a(S k)n = ack(S k, n) = ack(m, n).  We now define the Ackermann’s function in System L, by encoding the functions a and A. Definition 7.2.13 We define aL and AL in System L as follows: aL N AL G N

= iter N λx.S(x) AL : Nat −◦ Nat = iter S(N ) S(0) G : Nat

We show that this encoding is correct: Lemma 7.2.14 For any M ≡ Sm (0) and N ≡ Sn (0), then: aL N AL G N

= aN = AGN

Proof: By induction on n. • Basis: aL 0

= iter 0 succ AL = succ = a 0

AL G 0 = iter S(0) S(0) G = G S(0) = A G 0

140

Chapter 7. Expressiveness of System L

• Induction n = k + 1: aL Sk+1 (0)

= iter Sk+1 (0) succ AL = AL (iter Sk (0) succ AL ) = A(aSk (0))

AL G Sk+1 (0) = iter S(Sk+1 (0)) S(0) G = G(iter Sk+1 (0) S(0) G) = G(A G Sk (0)) = A G Sk+1 (0)  Based on aL and AL , we now define the Ackermann’s function in System L. Definition 7.2.15 ackL M N = (iter M succ (λgx.iter S(x) S(0) g)) N : Nat. The correctness of this encoding follows directly from Lemma 7.2.14. Note that iter S(N ) S(0) g cannot be typed in the system presented in (Lago, 2005), because g is a free variable. We allow building the term with the free variable g, but we do not allow reduction until it is closed.

7.3

System T Linearly

In this section we show how to compile System T programs into System L. This, together with the inverse compilation defined in Section 6.3.4, shows that System T and System L have the same computational power.

7.3.1

Explicit Erasing.

In the linear λ-calculus, it is not possible to erase arguments. However, terms are consumed by reduction. For example when a linear term (λx.x)N reduces to N , then we can see (λx.x) as being consumed in the reduction process. The idea of erasing by consuming is not new, it is known as Solvability (see (Barendregt, 1984)). In this section we give an analogous result that allows us to obtain a general form of erasing. We define the following mutually recursive operations which, respectively, erase and create a System L term. Definition 7.3.1 (Erasing) If Γ ⊢L M : τ , then E(M, τ ) is defined as follows (where

7.3. System T Linearly

141

I ≡ λx.x): E(M, Nat) E(M, Bool) E(M, τ1 ⊗ τ2 ) E(M, τ1 −◦ τ2 )

= = = =

iter M I I cond M I I let hx, yi = M in E(x, τ1 )E(y, τ2 ) E(M M(τ1 ), τ2 )

M(Nat) M(Bool) M(τ1 ⊗ τ2 ) M(τ1 −◦ τ2 )

= = = =

0 true hM(τ1 ), M(τ2 )i λx.E(x, τ1 )M(τ2 )

Example 7.3.2 Examples of erasing terms: E(hS(S 0), truei, Nat ⊗ Bool)

E(λx.x, Nat −◦ Nat)

= = −→ −→∗

let hS(S 0), truei = hx, yi in E(x, Nat)E(y, Bool) let hS(S 0), truei = hx, yi in (iter x I I)(cond y I I) (iter S2 (0) I I)(cond true I I) II −→ I

= E((λx.x)M(Nat), Nat) = iter ((λx.x)0) I I −→ iter 0 I I −→ I

Lemma 7.3.3 Let M be in ΛL . If Γ ⊢L M : τ then: 1. fv(E(M, τ )) = fv(M ) and ∀τ ′ .Γ ⊢L E(M, τ ) : τ ′ −◦ τ ′ . 2. M(τ ) is a closed System L term such that ⊢L M(τ ) : τ . Proof: Simultaneous induction on τ . • τ = Nat: 1. fv(E(M, Nat)) = fv(iter M I I) = fv(M ), and Γ ⊢L iter M I I : τ ′ −◦ τ ′ , for any τ ′ . 2. fv(M(Nat)) = fv(0) = ∅, and ⊢L 0 : Nat. • τ = Bool: 1. fv(E(M, Bool)) = fv(cond M I I) = fv(M ), and Γ ⊢L cond M I I : τ ′ −◦ τ ′ , for any τ ′ . 2. fv(M(Bool)) = fv(true) = ∅, and ⊢L true : Bool. • τ = τ1 ⊗ τ2 :

142

Chapter 7. Expressiveness of System L

1. fv(E(M, τ1 ⊗ τ2 )) = fv(let hx, yi = M in E(x, τ1 )E(y, τ2 )) = fv(M ). By induction: x : τ1 ⊢L E(x, τ1 ) : (τ ′ −◦ τ ′ ) −◦ (τ ′ −◦ τ ′ ) y : τ2 ⊢L E(y, τ2 ) : τ ′ −◦ τ ′ then x : τ1 , y : τ2 ⊢L E(x, τ1 )E(y, τ2 ) : τ ′ −◦ τ ′ . Then x : τ1 , y : τ2 ⊢L E(M, τ1 ⊗ τ2 ) : τ ′ −◦ τ ′ , for any τ ′ . 2. fv(M(τ1 ⊗ τ2 )) = fv(hM(τ1 ), M(τ2 )i) = fv(M(τ1 )) ∪ fv(M(τ2 )) = ∅ by induction hypothesis (2), and ⊢L hM(τ1 ), M(τ2 )i : τ1 ⊗ τ2 by induction hypothesis (2). • τ = τ1 −◦ τ2 : 1. fv(E(M, τ1 −◦ τ2 )) = fv(E(M M(τ1 ), τ2 )) = fv(M M(τ1 )) = fv(M ) by induction hypothesis (1 and 2). Also, by induction hypothesis (1) Γ ⊢L E(M M(τ1 ), τ2 ) : τ ′ −◦ τ ′ for any τ ′ , since ⊢L M(τ1 ) : τ1 by induction hypothesis (2). 2. fv(M(τ1 −◦ τ2 )) = fv(λx.E(x, τ1 )M(τ2 )) = ∅ by induction hypothesis (1 and 2). Also, ⊢L M(τ1 −◦ τ2 ) : τ1 −◦ τ2 because by induction hypothesis (1) x : τ1 ⊢L E(x, τ1 ) : τ2 −◦ τ2 and by induction hypothesis (2) ⊢L M(τ2 ) : τ2 .  Lemma 7.3.4 Consider M in ΛL . If x : τ ′ ⊢L M : τ and ⊢L N : τ ′ then: E(M, τ )[N/x] = E(M [N/x], τ ). Proof: By an easy structural induction on τ , using the fact that ⊢L M [N/x] : τ .



Lemma 7.3.5 (Erase Lemma) Let M be a closed term in ΛL (that is, fv(M ) = ∅). If ⊢L M : τ then E(M, τ ) −→∗ I. Proof: By induction on τ , using Theorem 6.3.33: • If τ = Nat, then E(M, Nat) = iter M I I −→∗ iter (Sn 0) I I −→∗ I. • If τ = Bool, then E(M, Bool) = cond M I I −→∗ I. • If τ = τ1 ⊗ τ2 , then M −→∗ hP, Qi and by Theorem 6.3.15 and Lemma 6.3.7: ⊢L P : τ and ⊢L Q : τ2 . By induction, E(P, τ1 ) −→∗ I and E(Q, τ2 ) −→∗ I, therefore let hx, yi = hP, Qi in E(x, τ1 )E(y, τ2 ) −→ E(P, τ1 )E(Q, τ2 ) −→∗ I. • If τ = τ1 −◦ τ2 then E(M, τ1 −◦ τ2 ) = E(M M(τ1 ), τ2 ). By Lemma 7.3.3 M(τ1 ) is a closed System L term of type τ1 , thus by induction E(M M(τ1 ), τ2 ) −→∗ I. 

7.3. System T Linearly

7.3.2

143

Explicit Copying.

We now show how to duplicate arbitrary terms in System L. The intuition is the following: suppose that we want to write λx.hx, xi. This term can be linearised as λxy.hx, yi. Now we just have to apply this term to two copies of the argument. Although we don’t have the argument yet, we can write a System L term, using a iterator, which will create these copies, when the argument becomes available: D = λz.iter (S2 0) (λxy.hx, yi) (λx.xz). Lemma 7.3.6 (Duplication Lemma) If M is a closed System L term, then there is a System L term D such that D M −→∗ hM, M i. Proof: Let D = λz.iter S2 (0) (λxy.hx, yi) (λx.xz). Then DM

−→ −→ −→ −→∗ −→

iter S2 (0) (λxy.hx, yi) (λx.xM ) (λx.xM )(iter S(0) (λxy.hx, yi) (λx.xM )) (λx.xM )((λx.xM )(iter 0 (λxy.hx, yi) (λx.xM ))) (λx.xM )((λx.xM )(λxy.hx, yi)) (λx.xM )((λxy.hx, yi)M ) −→ ((λxy.hx, yi)M )M −→∗ hM, M i

The term D is typable. In the following we will use N to denote Nat and τ2 to denote τ1 ⊗ τ1 . x : τ1 ⊢ L x : τ1 y : τ1 ⊢L y : τ1 ⊢L 0 : N

x : τ1 , y : τ1 ⊢L hx, yi : τ2

⊢L S(0) : N

x : τ1 ⊢L λy.hx, yi : τ1 → τ2

⊢L S2 (0) : N

⊢L λxy.hx, yi : τ1 → τ1 → τ2 z : τ1 ⊢L (λx.xz) : It(τ1 → τ1 → τ2 , τ1 → τ2 , τ2 ) z : τ1 ⊢L iter S2 (0) (λxy.hx, yi) (λx.xz) : τ2 ⊢L λz.iter S2 (0) (λxy.hx, yi) (λx.xz) : τ1 → τ2

Note that

x : τ1 → τ1 → τ2 ⊢L x : τ1 → τ1 → τ2

z : τ1 ⊢ L z : τ1

x : τ1 → τ1 → τ2 , z : τ1 ⊢L xz : τ1 → τ2 z : τ1 ⊢L (λx.xz) : (τ1 → τ1 → τ2 ) → (τ1 → τ2 ) and

x : τ1 → τ2 ⊢L x : τ1 → τ2

z : τ1 ⊢L z : τ1

x : τ1 → τ2 , z : τ1 ⊢L xz : τ2 z : τ1 ⊢L (λx.xz) : (τ1 → τ2 ) → τ2 Therefore z : τ1 ⊢L (λx.xz) : It(τ1 → τ1 → τ2 , τ1 → τ2 , τ2 )



This result also applies to numbers, so we defined two different ways of copying numbers in System L.

144

7.3.3

Chapter 7. Expressiveness of System L

Compilation

Having defined erasing and duplication of System L terms, we now give a formal definition of a compilation of System T into System L. Definition 7.3.7 System T types are translated into System L types using h ·ii defined by: h Natii h Boolii h τ1 → τ2 i h τ1 × τ2 i

= = = =

Nat Bool h τ1 i −◦ h τ2 i h τ1 i ⊗ h τ2 i

If Γ = x1 ; τ1 , . . . , xn : τn then h Γii = x1 : h τ1 i , . . . , xn : h τn i .

Notation 7.3.8 For the remainder of this section, we will use the following abbreviations: Cxx1 ,...,xn M Axy M

= iter Sn (0) (λx1 · · · xn .M ) (λz.zx) = ([x]M )[y/x]

Informally, Cxx1 ,...,xn M makes n copies x. Definition 7.3.9 Let M be a System T term. We define h M i in the following way: h xii hM N i h λx.M i

= = = =

h 0ii h S(M )ii h iter M N P i h trueii h falseii h cond M N P i h hM, N iii h π1 M i

= = = = = = = =

h π2 M i

=

x hM ihN i λx.[x]hhM i , if x ∈ fv(M ) λx.E(x, h τ1 i )hhM i , otherwise, where Γ ⊢T M : τ1 → τ2 = τ by Lemma 7.3.24 0 S(hhM i ) iter h M i h N i h P i true false cond h M i h N i h P i hhhM i , h N i i let hx, yi = h M i in E(y, h τ2 i )x, where Γ ⊢T M : τ1 × τ2 = τ by Lemma 7.3.24 let hx, yi = h M i in E(x, h τ1 i )y, where Γ ⊢T M : τ1 × τ2 = τ by Lemma 7.3.24

7.3. System T Linearly

145

where [·]· is defined as: [x](S(M )) = S([x]M ) [x]x = x [x](λy.M ) =  λy.[x]M   C x1 ,x2 (Axx1 M )(Axx2 N )   x [x](M N ) = ([x]M )N    M ([x]N )

[x]hM, N i =

[x](let hy, zi = M in N ) =

x ∈ fv(M ), x ∈ fv(N ) x ∈ fv(M ), x 6∈ fv(N ) x ∈ fv(N ), x 6∈ fv(M )

   C x1 ,x2 hAxx1 M , Axx2 N i, x ∈ fv(M ), x ∈ fv(N )   x h[x]M , N i, x ∈ fv(M ), x 6∈ fv(N )    hM, [x]N i, x ∈ fv(N ), x 6∈ fv(M )    let hy, zi = [x]M in N  

x ∈ fv(M ), x 6∈ fv(N )

let hy, zi = M in [x]N x 6∈ fv(M ), x ∈ fv(N )    Cxx1 ,x2 (let hy, zi = Ax M in Ax N ) x ∈ fv(M ), x ∈ fv(N ) x1 x2

   x ∈ fv(M ), x 6∈ fv(N P ) iter [x]M N P      iter M [x]N P x 6∈ fv(M P ), x ∈ fv(N )       iter M N [x]P x 6∈ fv(M N ), x ∈ fv(P )   x, x2 [x](iter M N P ) = Cx iter (Axx1 M ) (Axx2 N ) P x ∈ fv(M ) ∩ fv(N ), x 6∈ fv(P )    x ,x Cx 1 3 iter (Ax M ) N (Ax P ) x ∈ fv(M ) ∩ fv(P ), x 6∈ fv(N )  x3 x1     x ,x 2 3 x   Cx iter M (Ax2 N ) (Axx3 P ) x 6∈ fv(M ), x ∈ fv(N ) ∩ fv(P )     Cxx1 ,x2 ,x3 iter (Ax M ) (Ax N ) (Ax P ) x ∈ fv(M ) ∩ fv(N ) ∩ fv(P ) x1 x2 x3    x ∈ fv(M ), x 6∈ fv(N P ) cond [x]M N P      cond M [x]N [x]P x 6∈ fv(M ), x ∈ fv(N ) ∩ fv(P )       cond M [x]N (E(x, h τ i )P ) x 6∈ fv(M P ), x ∈ fv(N )   τ [x ](cond M N P ) = cond M (E(x, h τ i )N ) [x]P x 6∈ fv(M N ), x ∈ fv(P )    x x , 2 Cx cond (Ax M ) (Ax N ) (Ax P )  x ∈ fv(M ) ∩ fv(N ) ∩ fv(P ) x2 x2 x1     x ,x 1 2 x x   Cx cond (Ax1 M ) (Ax2 N ) (E(x2 , h τ i )P ) x ∈ fv(M ) ∩ fv(N ), x 6∈ fv(P )     Cxx1 ,x2 cond (Ax M ) (E(x2 , h τ i )N ) (Ax P ) x ∈ fv(M ) ∩ fv(P ), x ∈ / fv(N ) x1 x2 where the variables x1 , x2 and x3 above are assumed fresh.

146

Chapter 7. Expressiveness of System L

Definition 7.3.10 (Compilation) Let M be a System T term such that Γ, x1 : τ1 , . . . , xn : τn ⊢T M : τ . Its compilation into System L is defined as: [xτ11 ] . . . [xτnn ]hhM i 1 where fv(M ) = {x1 , . . . , xn }, n ≥ 0. Example 7.3.11 Let I ≡ λx.x, S ≡ λxyz.xz(yz), and K ≡ λxy.x. Then • h Iii = λx.x • h Sii = λxyz.iter S2 (0) (λz1 z2 .xz1 (yz2 )) λa.az • h Kii = λxy.E(y, τ )x Consider a finite set of variables X = {x1 , . . . , xn }. We say that a term M is in Λ+X L , if M is a term in ΛL , for which the variable conditions on the free variables hold, for variables in X. Note that, if X = fv(M ), then M is a term in ΛL . Definition 7.3.12 Let V be an infinite set of variables, and X a finite subset of V. The set of terms Λ+X L , is inductively defined from V in the following way: (Variables) (Application) (Abstraction) (Pairs) (Splitter) (Booleans) (Conditional) (Numbers) (Iterator)

x∈V +X M, N ∈ ΛL , fv(M ) ∩ fv(M ) ∩ X = ∅ M ∈ Λ+X L , x ∈ fv(M ) +X M, N ∈ ΛL , fv(M ) ∩ fv(M ) ∩ X = ∅ M, N ∈ Λ+X L , fv(M ) ∩ fv(N ) ∩ X = ∅, x, y ∈ fv(N )

⇒ ⇒ ⇒ ⇒

x ∈ Λ+X L (M N ) ∈ Λ+X L (λx.M ) ∈ Λ+X L hM, N i ∈ Λ+X L

⇒ let hx, yi = M in N ∈ Λ+X L true, false ∈ Λ+X L

M, N, P ∈ Λ+X L , fv(M ) ∩ fv(N ) ∩ X = ∅, (fv(N ) ∩ X) = (fv(P ) ∩ X) ⇒ cond M N P ∈ Λ+X L +X 0 ∈ ΛL +X M ∈ ΛL ⇒ S(M ) ∈ Λ+X L M, N, P ∈ Λ+X , fv(M ) ∩ fv(N ) ∩ X = ∅, L fv(N ) ∩ fv(P ) ∩ X = fv(M ) ∩ fv(P ) ∩ X = ∅ ⇒ iter M N P ∈ Λ+X L

Example 7.3.13 Consider the sets of variables X1 = {x, y, z} and X2 = {x, y}. The term +X2 1 2 1 S3 (0) is in both Λ+X and ΛL . The term cond x yz y is in Λ+X , but not in Λ+X . L L L +(X∪{x})

Proposition 7.3.14 If M is in Λ+X and x ∈ / fv(M ), then M is in ΛL L

.

Note also that any System L term M is a term in Λ+X L , for any X ⊆ fv(M ). 1

For the sake of readability we will omit types in the compilation when they are not necessary.

7.3. System T Linearly

147

Lemma 7.3.15 If M is a term in Λ+X L , and x is a free variable in M , then: 1. fv([x]M ) = fv(M ) +(X∪{x})

2. [x]M is a term in ΛL

Proof: By simultaneous induction on M . See Appendix C for details.



Lemma 7.3.16 Let M be a System T term, then fv(hhM i ) = fv(M ). Proof: By induction on M . See Appendix C.



Lemma 7.3.17 Let M be a System T term, then h M i is a term in Λ+∅ L . Proof: By induction on M using Lemma 7.3.15. See Appendix C.



Lemma 7.3.18 If M is a System T term, then: 1. fv([x1 ] · · · [xn ]hhM i ) = fv(M ). 2. If fv(M ) = {x1 , . . . , xn }, then [x1 ] · · · [xn ]hhM i is in ΛL . Proof: 1. By induction on the number of variables n using Lemma 7.3.15, and Lemma 7.3.16. 2. By Lemma 7.3.17, h M i is a term in Λ+∅ L . By induction on the number of variables n, +fv(M ) using point 2 of Lemma 7.3.15, [x1 ] · · · [xn ]hhM i is a term in ΛL , therefore a term in ΛL .  We will now prove that the compilation produces a typable term in System L. Definition 7.3.19 We say that M has type τ given the basis Γ, and write Γ ⊢L+X M : τ, if Γ ⊢L+X M : τ can be derived using the typing rules system for System L augmented with weakening and contraction for variables in X:

148

Chapter 7. Expressiveness of System L

Axiom and Structural Rule: x : τ ⊢L+X x : τ

Γ, x : τ1 , y : τ2 , ∆ ⊢L+X M : τ Γ, y : τ2 , x : τ1 , ∆ ⊢L+X M : τ

(Axiom)

(Exchange)

and X = {x} or X = ∅

Γ ⊢L+X M : τ

and x ∈ X

Γ, x : τ1 ⊢L+X M : τ

Γ, x : τ1 , x : τ1 ⊢L+X M : τ

and x ∈ X

Γ, x : τ1 ⊢L+X M : τ

(Weakening)

(Contraction)

Logical Rules: Γ, x : τ1 ⊢L+X M : τ2

(−◦Intro)

Γ ⊢L+X λx.M : τ1 −◦ τ2

Γ ⊢L+X1 M : τ1 −◦ τ2

Γ, ∆ ⊢L+(X1 ∪X2 ) M N : τ2

Γ ⊢L+X1 M : τ1

∆ ⊢L+X2 N : τ2

Γ, ∆ ⊢L+(X1 ∪X2 ) hM, N i : τ1 ⊗ τ2 Γ ⊢L+X1 M : τ1 ⊗ τ2

x : A, y : B, ∆ ⊢L+X1 N : τ

Numbers:

Γ ⊢L+X1 M : Nat

(Zero)

Γ ⊢L+X M : Nat Γ ⊢L+X S(M ) : Nat

Θ ⊢L+X2 N : τ0

(−◦Elim)

(⊗Intro)

Γ, ∆ ⊢L+(X1 ∪X2 ) let hx, yi = M in N : τ

⊢L+∅ 0 : Nat

∆ ⊢L+X1 N : τ1

(⊗Elim)

(Succ)

∆ ⊢L+X3 P : It(τ0 , . . . , τn )

(⋆)

Γ, Θ, ∆ ⊢L+(X1 ∪X2 ∪X3 ) iter M N P : τn

(Iter)

(⋆) where if M ≡ Sm (0) then n = m otherwise n = 0 Booleans: (True) (False) ⊢L+∅ true : Bool ⊢L+∅ false : Bool ∆ ⊢L+X1 M : Bool

Γ ⊢L+X2 N : τ

Γ ⊢L+X3 P : τ

Γ, ∆ ⊢L+(X1 ∪X2 ∪X3 ) cond M N P : τ

(Cond)

Definition 7.3.20 Consider a finite set of variables X = {x1 , . . . , xn } ⊆ fv(M ). If Γ = Γx ∪ {x1 : τ1 , . . . , xn : τn }, then the restriction of Γ to the variables in X, denoted by Γ|X , is {x1 : τ1 , . . . , xn : τn }. Lemma 7.3.21 Let Γ ⊢L+X M : τ , where x ∈ X ⊆ fv(M ) and X ′ = X \ {x}. Then Γ ⊢L+X ′ [x]M : τ .

7.3. System T Linearly

149

Proof: By induction on M . See Appendix C.



Lemma 7.3.22 If Γ ⊢T M : τ , then h Γ|fv(M ) i ⊢L+fv(M ) h M i : h τ i . Proof: By induction on the type derivation Γ ⊢T M : τ . See Appendix C.



Theorem 7.3.23 If Γ ⊢T M : τ and fv(M ) = {x1 , . . . , xn } then h Γ|fv(M ) i ⊢L [x1 ] . . . [xn ]hhM i : h τ i . Proof: By induction on the number of free variables of M , using Lemmas 7.3.21 and 7.3.22. 

Simulating System T in System L

7.3.4

In this subsection we show how System T can be simulated in System L. We first define a call-by-name evaluation strategy for System T , and recall the following properties: Lemma 7.3.24 • If Γ ⊢T λx.M : τ then τ = τ1 → τ2 and Γ, x : τ1 ⊢T M : τ2 for some τ1 , τ1 . • If Γ ⊢T π1 (M ) : τ then Γ ⊢T M : τ × τ2 for some τ2 . • If Γ ⊢T π2 (M ) : T then Γ ⊢T M : τ1 × τ for some τ1 . Definition 7.3.25 A System T term is called a closed value if it is a term of the form: Sn (0), true, false, hP, Qi or λx.P . Definition 7.3.26 Let M be a closed System T term, and N be a value. Let Rn (Q) stand for R(. . . R(Q) . . . ). We define ⇓ , where M ⇓ N means that M evaluates to the value N , | {z } n

in the following way:

N is a value

P ⇓ λx.P ′

N ⇓N M ⇓N S(M ) ⇓ S(N )

P ′ [Q/x] ⇓ N

PQ ⇓ N P ⇓ Sn (0)

Rn (Q) ⇓ N

iter P Q R ⇓ N

M ⇓ hP, Qi

P ⇓N

π1 (M ) ⇓ N P ⇓ true

Q⇓N

cond P Q R ⇓ N

M ⇓ hP, Qi

Q⇓N

π2 (M ) ⇓ N P ⇓ false

R⇓N

cond P Q R ⇓ N

150

Chapter 7. Expressiveness of System L

Lemma 7.3.27 (Adequacy of · ⇓ · for System T ) 1. If M ⇓ N then M −→∗ N .

2. If Γ ⊢ M : τ , and M is closed, then: τ τ τ τ

= Nat = Bool = τ1 × τ2 = τ1 → τ2

⇒ ⇒ ⇒ ⇒

M M M M

⇓ S(S . . . (S 0)) ⇓ true or M ⇓ false ⇓ hP, Qi ⇓ λx.P

Proof: The first part follows easily by induction on ⇓ , and the second is by induction over the maximal length of a derivation from M (which is well-defined since M is SN), using Part 1. 

Definition 7.3.28 A program in System T is a closed term of type Nat or Bool. We will now prove that we can simulate System T evaluations. First we prove a series of substitution lemmas, which we will use in the simulation theorem proof. Lemma 7.3.29 If M is in Λ+X L , x, y ∈ fv(M ), and fv(N ) = ∅, then ([y]M )[N/x] = [y](M [N/x]). Proof: By induction on M . See Appendix C.



Lemma 7.3.30 If M is a System T term, x ∈ fv(M ), and fv(N ) = ∅, then h M i [hhN i /x] = h M [N/x]ii. Proof: By induction on M . See Appendix C.



∗ Lemma 7.3.31 If M is in Λ+X L , x ∈ fv(M ), and fv(N ) = ∅, then ([x]M )[N/x] −→ M [N/x].

Proof: By induction on M . See Appendix C.



7.3. System T Linearly

151

Theorem 7.3.32 (Simulation) Let M be a System T program, then: M ⇓ N ⇒ h M i −→∗ h N i . Proof: By induction on M ⇓ N . • For values the result is trivial. • Application: h P Qii = h P i h Qii, and P Q ⇓ N if P ⇓ λx.P ′ and P ′ [Q/x] ⇓ N . By induction hypothesis h P i −→∗ h λx.P ′ i . There are two cases: – If x ∈ fv(P ′ ) then using Lemma 7.3.31: h λx.P ′ i h Qii

= (λx.[x]hhP ′ i )hhQii −→ ([x]hhP ′ i )[hhQii/x] −→∗ h P ′ i [hhQii/x] (I.H.)

=

h P ′ [Q/x]ii −→∗ h N i

– Otherwise, using Lemmas 7.3.4 and 7.3.5: h λx.P ′ i h Qii

= (λx.E(x, τ )hhP ′ i )hhQii −→∗ (E(hhQii, h τ i )hhP ′ i ) −→ h P ′ i (I.H.)

=

h P ′ [Q/x]ii −→∗ h N i

• Projection: π1 (M ) ⇓ N if M ⇓ hP, Qi, and P ⇓ N . Now using Lemmas 7.3.4 and 7.3.5: h π1 M i

= (I.H.) −→∗

=

let hx, yi = h M i in E(y, h τ2 i )x let hx, yi = h hP, Qiii in E(y, h τ2 i )x let hx, yi = hhhP i , h Qiii in E(y, h τ2 i )x (I.H.)

−→

E(hhQii, h τ2 i )hhP i −→∗ h P i −→∗ h N i

The case for π2 follows similarly. • Successor : follows trivially by induction hypothesis. • Iterator : iter P Q R ⇓ N if P ⇓ Sn (0), and Rn (Q) ⇓ N . Then, using Definition 7.3.10: h iter P Q Rii

= (I.H.) −→∗

iter h P i h Qii h Rii iter h Sn (0)ii h Qii h Rii (I.H.)

=

iter Sn (0) h Qii h Rii −→∗ h Riin (hhQii) −→∗ h N i

152

Chapter 7. Expressiveness of System L

• Conditional : cond P Q R ⇓ N if P ⇓ true, and Q ⇓ N . Then, using Definition 7.3.10: h cond P Q Rii

=

cond h P i h Qii h Rii

(I.H.) −→∗

cond h trueii h Qii h Rii (I.H.)

cond true h Qii h Rii −→ h Qii −→∗ h N i

= The case for false follows similarly.



7.4

The Role of Closed Reduction

In this section we define two linear systems: LNat and LNat 0 . Both systems are defined in the linear λ-calculus with numbers, booleans, pairs of natural numbers, and an iterator. System LNat has the same syntax as System L, and as System L, it uses a closed reduction strategy. However, it is a more restrictive type system than System L. System LNat is not restricted 0 to closed reduction strategies, but in order to be linear, it restricts the set of terms. is strictly less powerful than System LNat , because, although We will show that System LNat 0 can encode all the primitive recursive functions, more both System LNat and System LNat 0 general functions such as Ackermann, are definable in System LNat , but not in System LNat 0 .

7.4.1

System LNat

Here we define System LNat . System LNat types terms in ΛL (such as System L). System LNat ’s reduction rules are the same as for System L’s, therefore we inherit from System L the following properties, for the untyped calculus: correctness of Substitution (Lemma 6.3.6), correctness of −→ (Lemma 6.3.7), and confluence (Theorem 6.3.32). We now define the type system. The rules of System LNat differ from the rules for System L, by only allowing pairs of natural numbers. Definition 7.4.1 We say that M has type τ given the basis Γ, and write Γ ⊢LNat M : τ, if Γ ⊢LNat M : τ can be obtained from the following derivation rules: Axiom and Structural Rule: x : τ ⊢LNat x : τ

(Axiom)

7.4. The Role of Closed Reduction

153

Γ, x : τ1 , y : τ2 , ∆ ⊢LNat M : τ Γ, y : τ2 , x : τ1 , ∆ ⊢LNat M : τ

(Exchange)

Logical Rules: Γ, x : τ1 ⊢LNat M : τ2 Γ ⊢LNat λx.M : τ1 −◦ τ2

∆ ⊢LNat N : τ1

Γ ⊢LNat M : τ1 −◦ τ2

(−◦Intro)

Γ, ∆ ⊢LNat M N : τ2 ∆ ⊢LNat N : Nat

Γ ⊢LNat M : Nat

Γ, ∆ ⊢LNat hM, N i : Nat ⊗ Nat Γ ⊢LNat M : Nat ⊗ Nat

(⊗Intro)

x : Nat, y : Nat, ∆ ⊢LNat N : τ

Γ, ∆ ⊢LNat let hx, yi = M in N : τ Numbers: ⊢LNat 0 : Nat Γ ⊢LNat M : Nat

(Zero)

Γ ⊢LNat M : Nat Γ ⊢LNat S(M ) : Nat

Θ ⊢LNat N : τ

(−◦Elim)

(⊗Elim)

(Succ)

∆ ⊢LNat P : τ → τ

Γ, Θ, ∆ ⊢LNat iter M N P : τ

(Iter)

Booleans: ⊢LNat true : Bool ∆ ⊢LNat M : Bool

(True)

⊢LNat false : Bool

Γ ⊢LNat N : τ

(False)

Γ ⊢LNat P : τ

Γ, ∆ ⊢LNat cond M N P : τ

(Cond)

As in System L, we allow the typing of iter M N P even if P is open (in contrast with (Lago, 2005)), but we do not allow reduction until P becomes closed. We will show later that this feature gives our system more power. Subject Reduction for System LNat can be proved as for System L. Theorem 7.4.2 (Subject Reduction) If Γ ⊢LNat M : τ and M −→ N , then Γ ⊢LNat N : τ . Note that confluence of the untyped calculus, together with subject reduction, implies confluence of the typed calculus. Since terms typable in System LNat are also typable in System L, we inherit the strong normalisation property. Theorem 7.4.3 (Strong Normalisation) If Γ ⊢LNat M : τ , then M is strongly normalisable.

154

7.4.2

Chapter 7. Expressiveness of System L

System LNat 0

is built in the same way as for System L, except that, when The set of terms for System LNat 0 building an iterator, we don’t allow the iterated function to be an open term. Definition 7.4.4 Let V be an infinite set of variables. The set of terms ΛLNat , is inductively 0 defined from V as for ΛL , expect iterators, which are defined in the following way: (Iterator) M, N, P ∈ ΛLNat , fv(M ) ∩ fv(N ) = ∅, and fv(P ) = ∅ ⇒ iter M N P ∈ ΛLNat 0

0

We now define the reduction rules, and the typing rules, for System LNat 0 . Definition 7.4.5 (Reduction) The notion of reduction on ΛLNat is defined by the following 0 contraction rules: Beta: Let: Cond: Cond: Iter: Iter:

(λx.M )N let hx, yi = hN, P i in M cond true M N cond false M N iter 0 N P iter S(M ) N P

−→ −→ −→ −→ −→ −→

M [N/x] M [N/x][P/y] M N N P (iter M N P )

As before, reductions can take place in any context (see Definition 2.2.4). Correctness of Substitution is proved as for System L, but α-conversion must be used in substitution whenever necessary. Note that α-conversion was not needed in System L and therefore in System LNat (because all the substitutions are by closed terms, there is no capture of free variables). Lemma 7.4.6 (Correctness of Substitution) Let M and N be terms in ΛLNat and x ∈ 0 fv(M ). Then M [N/x] is in ΛLNat . 0

Proof: Straightforward induction on the structure of M .

Lemma 7.4.7 (Correctness of −→) Let M be in ΛLNat , and M −→ N , then: 0

1. fv(M ) = fv(N ); 2. M is in ΛLNat . 0



7.4. The Role of Closed Reduction

155

Proof: The only reduction rules that copy or erase terms, are the rules for iterators, which either copy or erase the iterated function. However, because of the condition that the iterated function must be closed when constructing the term iter M N P , then reducing an iterator will either copy or erase a closed term. Therefore the set of free variables is preserved and the term obtained is in ΛLNat .  0

Note that in System LNat we do not have the constraint on the iterator term, but we have a condition in the reduction rules for iterators, which also guarantees that reducing an iterator will either copy or erase a closed term. We now show how types are assigned to terms. The only difference between the rules for this system and for System LNat is in the (Iter) rule, where the context for the iterated function is always empty, since the iterated function is a closed term, and the system does not have weakening. Definition 7.4.8 We say that M has type τ given the basis Γ, and write Γ ⊢LNat M : τ, 0

if Γ ⊢LNat M : τ can be obtained from the following derivation rules: 0

Axiom and Structural Rule: (Axiom)

x : τ ⊢LNat x : τ 0

Γ, x : τ1 , y : τ2 , ∆ ⊢LNat M : τ 0

Γ, y : τ2 , x : τ1 , ∆ ⊢LNat M : τ

(Exchange)

0

Logical Rules: Γ, x : τ1 ⊢LNat M : τ2 0

Γ ⊢LNat λx.M : τ1 −◦ τ2

Γ ⊢LNat M : τ1 −◦ τ2

∆ ⊢LNat N : τ1

0

(−◦Intro)

0

Γ, ∆ ⊢LNat M N : τ2

0

0

∆ ⊢LNat N : Nat

Γ ⊢LNat M : Nat

0

0

Γ, ∆ ⊢LNat hM, N i : Nat ⊗ Nat

(⊗Intro)

0

Γ ⊢LNat M : Nat ⊗ Nat

x : Nat, y : Nat, ∆ ⊢LNat N : τ

0

0

Γ, ∆ ⊢LNat let hx, yi = M in N : τ

(⊗Elim)

0

Numbers: ⊢LNat 0 : Nat

Γ ⊢LNat M : Nat

(Zero)

0

Γ ⊢LNat S(M ) : Nat

0

Γ ⊢LNat M : Nat 0

(Succ)

0

Θ ⊢LNat N : τ 0

⊢LNat P : τ → τ 0

Γ, Θ ⊢LNat iter M N P : τ 0

(Iter)

(−◦Elim)

156

Chapter 7. Expressiveness of System L

Booleans: ⊢LNat true : Bool

(True)

0

⊢LNat false : Bool

(False)

0

∆ ⊢LNat M : Bool

Γ ⊢LNat N : τ

0

0

Γ ⊢LNat P : τ

Γ, ∆ ⊢LNat cond M N P : τ

0

(Cond)

0

As before, Subject Reduction for System LNat can be proved as for System L. 0 Theorem 7.4.9 (Subject Reduction) If Γ ⊢LNat M : τ and M −→ N , then Γ ⊢LNat N : τ . 0

0

Note that any term typable in System LNat is also typable in System LNat , therefore in 0 is strongly normalisable. System L. Thus, System LNat 0 Theorem 7.4.10 (Strong Normalisation) If Γ ⊢LNat M : τ , then M is strongly normal0 isable. Confluence for terms in System LNat is a direct consequence of strong normalisation and 0 the fact that the rules are non-overlapping (using Newman’s Lemma (Newman, 1942)). Moreover, we can apply directly Klop’s result (Klop et al., 1993) to the untyped calculus because the system is orthogonal (that is, left-linear and non-overlapping). Lemma 7.4.11 (Confluence) If M −→∗ N1 and M −→∗ N2 , then there is a term N such that N1 −→∗ N and N2 −→∗ N .

7.4.3

Primitive Recursive Functions

The encodings of the projections fst and snd, and of the copying function C, given for System L in Section 7.2, satisfy the term conditions of System LNat 0 , and therefore also those Nat of System L . The reductions are valid in both systems, so we just need to show that the terms encoding erasing and duplication are typable in the more restricted type systems of System LNat and System LNat . 0 We show a type derivation for fst in System LNat in Figure 7.1. The typing of snd is similar, 0 Nat as are the type derivations in System L . The function C : Nat −◦ Nat ⊗ Nat that given a number N ≡ Sn (0) returns a pair hN, N i is and System LNat , as shown in Figure 7.2. also typable in both System LNat 0 Finally, we consider the encoding of the primitive recursive scheme. functions G : Nat −◦ Nat −◦ Nat and F : Nat → Nat, let g ′ be the term:

Given the closed

λy.λz.let hz1 , z2 i = C z in Gz1 (yz2 ) : (Nat −◦ Nat) −◦ (Nat −◦ Nat)

7.4. The Role of Closed Reduction

157

z : Nat ⊢LNat z : Nat 0

v : Nat ⊢LNat v : Nat

u : Nat ⊢LNat u : Nat

0

x : Nat ⊗ Nat ⊢LNat x : Nat ⊗ Nat

0

⊢LNat λz.z : Nat −◦ Nat 0

u : Nat, v : Nat ⊢LNat iter v u λz.z : Nat

0

0

x : Nat ⊗ Nat ⊢LNat let hu, vi = x in iter v u λz.z : Nat 0

⊢LNat λx.let hu, vi = x in iter v u λz.z : (Nat ⊗ Nat) −◦ Nat 0

Figure 7.1: Typing of fst a : Nat ⊢LNat a : Nat

b : Nat ⊢LNat b : Nat

a : Nat ⊢LNat S a : Nat

b : Nat ⊢LNat S b : Nat

0

0

x : Nat ⊗ Nat ⊢LNat x : Nat ⊗ Nat

0

0

a : Nat, b : Nat ⊢LNat hS a, S bi : Nat ⊗ Nat

0

0

x : Nat ⊗ Nat ⊢LNat let ha, bi = x in hS a, S bi : Nat ⊗ Nat 0

⊢LNat F ≡ λx.let ha, bi = x in hS a, S bi : (Nat ⊗ Nat) −◦ (Nat ⊗ Nat) 0

⊢LNat 0 : Nat 0

x : Nat ⊢LNat x : Nat

⊢LNat 0 : Nat 0

⊢LNat h0, 0i : Nat ⊗ Nat

0

0

⊢LNat 0

· · · F : (Nat ⊗ Nat) −◦ (Nat ⊗ Nat)

x : Nat ⊢LNat iter x h0, 0i (λx.let ha, bi = x in hS a, S bi) : Nat ⊗ Nat 0

⊢LNat λx.iter x h0, 0i (λx.let ha, bi = x in hS a, S bi) : Nat −◦ (Nat ⊗ Nat) 0

Figure 7.2: Typing of C

then h(x, n) is defined by the term (iter n F G′ )x : Nat, see Figure 7.3. Note that the encoding of h is a closed term. This term is valid because F is closed (by assumption), and G′ is closed since G is a closed term (by assumption). Now to encode the standard primitive recursive scheme, which has an extra n in the last equation, all we need to do is to copy n, as shown in Section 7.2 (see Lemma 7.2.10).

7.4.4

Beyond Primitive Recursion

The encoding of Ackermann’s function given for System L using functions aL and AL is still valid in System LNat . Since the syntax of the terms and the reduction rules are the same, we just have to check that the terms defining aL and AL are typable in System LNat : see

158

⊢LNat 0

Chapter 7. Expressiveness of System L

· · · G : Nat −◦ Nat −◦ Nat

z1 : Nat ⊢LNat z1 : Nat 0

y : Nat −◦ Nat ⊢LNat y : Nat −◦ Nat

z2 : Nat ⊢LNat z2 : Nat

0

z1 : Nat ⊢LNat Gz1 : Nat −◦ Nat

0

y : Nat −◦ Nat, z2 : Nat ⊢LNat yz2 : Nat

0

0

y : Nat −◦ Nat, z1 : Nat, z2 : Nat ⊢LNat Gz1 (yz2 ) : Nat 0

z : Nat ⊢LNat 0

· · · C z : Nat ⊗ Nat

· · · y : Nat −◦ Nat, z1 : Nat, z2 : Nat ⊢LNat Gz1 (yz2 ) : Nat 0

y : Nat −◦ Nat, z : Nat ⊢LNat let hz1 , z2 i = C z in Gz1 (yz2 ) : Nat 0

y : Nat −◦ Nat ⊢LNat λz.let hz1 , z2 i = C z in Gz1 (yz2 ) : Nat −◦ Nat 0



⊢LNat g ≡ λyz.let hz1 , z2 i = C z in Gz1 (yz2 ) : (Nat −◦ Nat) −◦ (Nat −◦ Nat) 0

x : Nat ⊢LNat x : Nat 0

⊢LNat 0

· · · F : Nat −◦ Nat ′

⊢LNat

n : Nat ⊢LNat iter n F g : Nat −◦ Nat 0

0

· · · g ′ : (Nat −◦ Nat) −◦ (Nat −◦ Nat)

x : Nat ⊢LNat x : Nat 0



n : Nat, x : Nat ⊢LNat (iter n F g )x : Nat 0

Figure 7.3: Typing of functions defined by primitive recursion

Figure 7.4. Note that iter (S u) (S 0) g cannot be typed in System LNat 0 , because g is a free variable. Nat System L allows building the term with the free variable g, but does not allow reduction until it is closed. Actually every function in System LNat can be defined in system H(∅) in (Lago, 2005). 0 Nat Therefore System L0 is strictly less powerful than System LNat because we cannot define Ackermann’s function in H(∅) (see (Lago, 2005) for a proof of this result). Here we proved that the closed reduction strategy of System L is a key element for System L computational power. Closed reduction strategies impose strong constraints on the application of reduction rules, but they still can simulate both call-by-name and call-byvalue evaluations in the λ-calculus, and also more efficient evaluations (see (Fern´ andez et al., 2005)). To study the role of closed reduction, we defined two linear systems with iterators, and established their relation with System L and previous linear versions of System T . The results are summarised as follows:

7.5. Discussion

159

x : Nat ⊢LNat x : Nat 0

x : Nat ⊢LNat S x : Nat 0

⊢LNat succ ≡ λx.S x : Nat −◦ Nat 0

u : Nat ⊢LNat u : Nat

⊢LNat 0 : Nat

u : Nat ⊢LNat S u : Nat

⊢LNat S 0 : Nat

0

0

0

0

g : Nat −◦ Nat ⊢LNat g : Nat −◦ Nat 0

g : Nat −◦ Nat, u : Nat ⊢LNat iter (S u) (S 0) g : Nat 0

g : Nat −◦ Nat ⊢LNat λu.iter (S u) (S 0) g : (Nat −◦ Nat) 0

⊢LNat A ≡ λgu.iter (S u) (S 0) g : (Nat −◦ Nat) −◦ (Nat −◦ Nat) 0

m : Nat ⊢LNat m : Nat 0

⊢LNat 0

· · · succ : Nat −◦ Nat

⊢LNat 0

· · · A : (Nat −◦ Nat) −◦ (Nat −◦ Nat)

m : Nat ⊢LNat iter m succ A : Nat −◦ Nat 0

n : Nat ⊢LNat n : Nat 0

m : Nat, n : Nat ⊢LNat (iter m succ A) n : Nat 0

⊢LNat λm n.(iter m succ A) n : Nat −◦ Nat −◦ Nat 0

Figure 7.4: Typing of Ackermann’s function

System LNat ⊆ H(∅) ⊂ System LNat ⊂ System L 0

7.5

Discussion

In this chapter we studied the computational power of System L presented in the previous chapter. In particular, we proved that System L is as computationally powerful as System T . In this proof we define a compilation from terms in System T to terms in System L: this defines a partial linearisation of System T , where the target language is System L. Finally we showed the role of closed reduction in the expressiveness of the system.

160

Chapter 7. Expressiveness of System L

Chapter 8

Conclusions Duplications, that arise from substitution of function arguments during reduction, are a major problem when dealing with efficient implementation of programming languages. Optimal reduction, in the sense that no unnecessary duplications are made, for the λ-calculus, was precisely defined by L´evy in his PhD thesis (L´evy, 1978). In practice, implementation of optimal reductions in the sense defined by L´evy, are far from being efficient. As stated by John Lamping in (Lamping, 1990): “...both an informal argument and experience with an implementation of the algorithm indicate that the amount of bookkeeping work the algorithm requires for each beta reduction step is proportional to the cost of doing a substitution...” In the linear λ-calculus, duplication of arguments never takes place, therefore, every reduction is optimal. However, the calculus itself is very restrictive and has a small computational power. Still, we believe there is something in-between the λ-calculus and the linear λcalculus, for which the advantages of linearity can be useful. In this thesis we focused on the simulation of the λ-calculus by other (more restricted) calculus based on linearity. In particular, our aim was to provide some answers to the following questions: • can one define linearisation algorithms from the λ-calculus into more linear calculi, and what type of term information must be used in those definitions? • can one extend the linear λ-calculus, to more expressive “almost linear calculi”, without loosing the linear λ-calculus good computational properties? We will now reflect on the results obtained, and point out some future lines of work, following the work presented here.

162

8.1

Chapter 8. Conclusions

Linearisation

The expansion relation defined in (Florido and Damas, 2004), from λ-terms into linear terms, was based on the type derivation of terms in an intersection type system. Expansion was proved to be defined for the strongly normalisable λ-terms. Since it is not possible to decide if a term is strongly normalisable, it is also not possible to decide if a term can be linearised. Note that Kfoury’s conjecture in (Kfoury, 2000), states that, if and only if a term M is strongly normalisable term, it is possible to define a linear term in the Λ∧ , which contracts into M . We summarise the relations described above in Figure 8.1.

Figure 8.1: Linearisation, Intersection Types, and Strong Normalisation

8.1.1

Simple Linearisation

Damas and Florido (Florido and Damas, 2004) motivated the definition of transformation algorithms for linearisation based on decidable restrictions of intersection types. We defined the notion of simple linearisation, a transformation algorithm in which linearisation is used in a restricted way. We only linearise redexes which are already in the term, after a transformation step which makes all the implicit redexes in the term become explicit.

8.1. Linearisation

163

The transformation is a total function in the λ-calculus, using as only information the free occurrences of the abstracted variables in the body of the function. As a result, terms typable in the rank 2 intersection type system with a simple type, become typable in the simple type system, with the same type. Note that terms typable by simple types, include the set of basic values returned by programs in a functional programing language, such as ML or Haskell. Note also that rank 2 intersection types strictly include the ML type system, as shown in (van Bakel, 1993; Jim, 1995).

8.1.2

Weak Linearisation

While simple linearisation is easily defined based on local term information (occurrences of variable), generalisations of this notion to other ranks are not trivially defined. We have identified the weak affine λ-calculus, and introduced the notion of weak linearisation. The weak affine λ-calculus, is as a subset of the λ-calculus where there is no duplication of arguments during reduction. That is, non-linearity is allowed, but only on functions that will never be applied. This class of terms was proved to be decidable, strongly normalisable and typable in PTIME. We call weak linearisation to a transformation from terms in the λ-calculus into terms in the weak affine λ-calculus. Our transformation uses legal paths (which captures the notion of virtual redexes) to link abstractions to its possible future arguments, while preserving the normal forms of terms. Termination of weak linearisation, is an undecidable problem. Similarly to the result in (Kfoury, 2000), we conjecture that our transformation terminates for the strongly normalisable λ-terms. Note that, although Kfoury established a relation between general λ-terms, and terms in the linear calculus Λ∧ , there is not a direct definition of a transformation of λ-terms into terms in Λ∧ . We define directly a transformation from λ-terms to weak affine terms and simulate the λ-calculus by a subset of the λ-calculus and not by a non-standard calculus.

8.1.3

Almost linear calculi

Concerning extensions to the linear λ-calculus, we defined System L, and studied its expressiveness.

164

Chapter 8. Conclusions

System L extends the linear λ-calculus with numbers, booleans, pairs and an iterator. We proved that System L can express much more than primitive recursive functions, in particular, it has all the computational power of G¨ odel’s System T . System L computational power relies on two features: the use of a closed reduction strategy (which permits the construction of an iterator on an open function, but only iterates the function after it becomes closed, thus preserving linearity), and the use of a more general typing rule for iterators based on the new notion of iterative types. The study of the computational power of System L, shows that: • Closed reduction has a crucial influence on the computational power of System L; • System T is intrinsically redundant: it has several ways of expressing duplication and erasure.

8.2

Future Work

We now present some possible lines of work that can depart from the results presented in this thesis.

8.2.1

Linearisation

Linearisation and Intersection Types We have already mention the close relation between terms typable by intersection types and terms that can be linearised. We believe there exists, also, a very close relation between linearisation algorithms and type inference algorithms for intersection types. In particular, the analysis of type inference and linearisation examples, suggests an isomorphism between weak affine versions of terms, and final skeletons given by Carlier and Wells, System E type inference algorithm (Carlier et al., 2004; Carlier and Wells, 2004).

Time versus Space Another interesting question, that arises from linearisation, is the relation between the reductions of λ-terms and the size of their linear versions. Terms that take more advantage of sharing during reduction are the ones that have relatively bigger linear versions.

8.2. Future Work

165

Further investigation of this subject could lead to an interesting interplay between time and space in computation.

Partial linearisation Weak linearisation only terminates if the term is strongly normalisable. It would be interesting to explore partial weak linearisation, where the transformation halts after a certain depth. For a sufficiently large depth this could lead to decidable restrictions usable in practice. Applying our technique to a programming language could also be simplified if one considers definition of weak affine terms parametrised by a particular reduction strategy and not by the whole reduction graph of the term.

Sharing versus copying Linearity is a key element when looking at the problem of sharing versus copying of function arguments. This is an important issue when dealing with efficient implementations of programming languages. For example, for logic programming, there have been some works exploring the gain of copying of arguments instead of structure sharing (Bruynooghe, 1982; Mellish, 1982). It could be interesting to study in which extent can our linearisation be applied to a compilation from a sharing machine to a copying machine. Decidable restrictions of linearisation would be of great importance here.

8.2.2

Almost linear calculi

Iterative types Every syntactic term in the linear λ-calculus is strongly normalisable (reduction strictly reduces the size of terms). For System L, strong normalisation is only guaranteed for typable terms (recall that in Section 6.3.2 we defined an encoding of Ω in ΛL ). We defined iterative types, which allow the definition of iterators where in some cases the iterated function is used with different types each time. Iterative types have impact on the computational power of System L. Recent work on this subject, lead to the definition of a type inference algorithm for System L, and also established a relation between iterative types and rank 2 intersection types (Alves et al., 2007).

166

Chapter 8. Conclusions

It remains to be understood what is the exact computational power given only by iterative types.

Extensions to System L System L is not computationally complete (it is strongly normalising). Systems with explicit recursion combinators, like Plotkin’s PCF (Plotkin, 1977), are not normalising. PCF is a typed functional programming language, where types are used to ensure that programs are well-behaved but not necessarily terminating. It would be interesting to find if it is possible to define a linear version of PCF using closed reductions.

Implementation There exist efficient algorithms for implementing linear calculi (Abramsky, 1993; Mackie, 1994; Mackie, 2000). An interesting point that remains to be studied is the possibility of obtaining an efficient implementation of System L based on those algorithms.

8.3

Final Considerations

We have studied several aspects of linearity in the λ-calculus. There is a great deal of redundancy in the full λ-calculus, and linearity plays a very important role in understanding what computational objects are necessary to define certain computational classes. With this work, several lines of future work were opened. Particularly, concerning the definition of linearisation algorithms, we regret not having provided a deeper study on the relation between linearisation and intersection types typability, and on the definition of partial decidable restrictions of the weak linearisation transformation. Nevertheless, we believe that we have provided some relevant answers to our initial questions regarding linearity.

Appendix A

Church-Rosser for ΛL Let −→ be the closed reduction strategy, defined in Chapter 6. Recall from 2 that ։ is the reflexive transitive closure of −→. We also consider −→= , which denotes the zero or one step reduction induced by −→. We recall the following result from (Barendregt, 1984). Lemma A.0.1 If a binary relation satisfies the diamond property, then the transitive closure of that relation also satisfies the diamond property. Definition A.0.2 We define a binary relation −→∗1 on the set of System L terms inductively as indicated in Figure A.1. Lemma A.0.3 If M −→∗1 M ′ and N −→∗1 N ′ , where fv(M )∩fv(N ) = ∅, then M [N/x] −→∗1 M ′ [N ′ /x]. Proof: By induction on the definition of −→∗1 . • M −→∗1 M ′ is M −→∗1 M . Then one has to show that M [N/x] −→∗1 M [N ′ /x]. This follows easily by induction on the structure of M . • M −→∗1 M ′ is S(P ) −→∗1 S(P ′ ), and is a consequence of P −→∗1 P ′ . By induction hypothesis P [N/x] −→∗1 P ′ [N ′ /x]. Then S(P [N/x]) −→∗1 S(P ′ [N/x]) by the definition of A.0.2, thus (S(P ))[N/x] −→∗1 (S(P ′ ))[N ′ /x], by the definition of substitution. • M −→∗1 M ′ is λy.P −→∗1 λy.P ′ , and is a consequence of P −→∗1 P ′ . Similar to the previous case.

168

Appendix A. Church-Rosser for ΛL

M M M M M M M M M M

−→∗1 −→∗1 −→∗1 −→∗1 −→∗1 −→∗1 −→∗1 −→∗1 −→∗1 −→∗1

M M′ M′ M ′, N M ′, N M ′, N M ′, N M ′, N M ′, N M ′, N

−→∗1 −→∗1 −→∗1 −→∗1 −→∗1 −→∗1 −→∗1

N′ N′ N′ N′ N′ N ′ , P −→∗1 P ′ N ′ , P −→∗1 P ′

⇒ ⇒ ⇒ ⇒ ⇒ ⇒ ⇒ ⇒ ⇒

M M M M

−→∗1 −→∗1 −→∗1 −→∗1

M ′, N M ′, N M ′, N M ′, N

−→∗1 −→∗1 −→∗1 −→∗1

N′ N′ N ′ , P −→∗1 P ′ N ′ , P −→∗1 P ′

⇒ ⇒ ⇒ ⇒

S(M ) −→∗1 S(M ′ ) λx.M −→∗1 λx.M ′ hM, N i −→∗1 hM ′ , N ′ i M N −→∗1 M ′ N ′ (λx.M )N −→∗1 M ′ [N ′ /x] where fv(N ) = ∅ cond true M N −→∗1 M ′ cond false M N −→∗1 N ′ cond M N P −→∗1 cond M ′ N ′ P ′ let hx, yi = hM, N i in P −→∗1 P ′ [M ′ /x, N ′ /y] where fv(M ) = fv(N ) = ∅ let hx, yi = M in N −→∗1 let hx, yi = M ′ in N ′ iter 0 M N −→∗1 M ′ where fv(N ) = ∅ iter S(M ) N P −→∗1 P ′ (iter M ′ N ′ P ′ ) where fv(P ) = ∅ iter M N P −→∗1 iter M ′ N ′ P ′

Figure A.1: The −→∗1 reduction relation

• M −→∗1 M ′ is hP, Qi −→∗1 hP ′ , Q′ i, and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ . Then we have two subcases: – x ∈ fv(P ). By induction hypothesis P [N/x] −→∗1 P ′ [N ′ /x]. Then hP [N/x], Qi −→∗1 hP ′ [N ′ /x], Q′ i, thus hP, Qi[N/x] −→∗1 hP ′ , Q′ i[N ′ /x]. – x ∈ fv(Q). By induction hypothesis Q[N/x] −→∗1 Q′ [N ′ /x]. Then hP, Q[N/x]i −→∗1 hP ′ , Q′ [N ′ /x]i, thus hP, Qi[N/x] −→∗1 hP ′ , Q′ i[N ′ /x]. • M −→∗1 M ′ is P Q −→∗1 P ′ Q′ , and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ . Similar to the previous case. • M −→∗1 M ′ is (λy.P )Q −→∗1 P ′ [Q′ /y], and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ , and since fv(Q) = ∅, then x ∈ fv(P ). M [N/x]

= (λy.P [N/x])Q ∗ −→1 P ′ [N ′ /x][Q′ /y] (I.H.)

= =

(P ′ [Q′ /y])[N ′ /x] M ′ [N ′ /x]

• M −→∗1 M ′ is cond true Q R −→∗1 Q′ , and is a consequence of Q −→∗1 Q′ and R −→∗1 R′ . By induction hypothesis Q[N/x] −→∗1 Q′ [N ′ /x] and R[N/x] −→∗1 R′ [N ′ /x]. Thus cond true Q[N/x] R[N/x] −→∗1 Q′ [N ′ /x]. • M −→∗1 M ′ is cond false Q R −→∗1 R′ , and is a consequence of Q −→∗1 Q′ and R −→∗1 R′ . Similar to the previous case

169

• M −→∗1 M ′ is cond P Q R −→∗1 cond P ′ Q′ R′ , and is a consequence of P −→∗1 P ′ , Q −→∗1 Q′ and R −→∗1 R′ . By induction hypothesis P [N/x] −→∗1 P ′ [N ′ /x], Q[N/x] −→∗1 Q′ [N ′ /x] and R[N/x] −→∗1 R′ [N ′ /x]. We have two possible cases: – x ∈ fv(P ). Then cond P Q R[N/x]

= cond P [N/x] Q R ∗ −→1 cond P ′ [N ′ /x] Q′ R′ = cond P ′ Q′ R′ [N ′ /x]

– x ∈ fv(Q) = fv(R). Then cond P Q R[N/x]

= cond P Q[N/x] Q[N/x] ∗ −→1 cond P ′ Q′ [N ′ /x] R′ [N ′ /x] = cond P ′ Q′ R′ [R′ /x]

• M −→∗1 M ′ is let hy, zi = hP, Qi in R −→∗1 R′ [P ′ /y][Q′ /z] and is a consequence of P −→∗1 P ′ , Q −→∗1 Q′ and R −→∗1 R′ , and since fv(P ) = fv(Q) = ∅, then x ∈ fv(R). M [N/x]

= let hy, zi = hP, Qi in R[N/x] ∗ −→1 R[N ′ /x][P ′ /y][Q′ /z] (I.H.)

=

(R′ [P ′ /y][Q′ /z])[R′ /x] = M ′ [N ′ /x]

• M −→∗1 M ′ is let hy, zi = P in Q −→∗1 let hy, zi = P ′ in Q′ and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ . Then we have two subcases: – x ∈ fv(P ), then M [N/x]

= (I.H.) −→∗1

let hy, zi = P [N/x] in Q let hy, zi = P ′ [N ′ /x] in Q′ = M ′ [N ′ /x]

– x ∈ fv(Q), then M [N/x]

= (I.H.) −→∗1

let hy, zi = P in Q[N/x] let hy, zi = P ′ in Q′ [N/x] = M ′ [N ′ /x]

• M −→∗1 M ′ is iter 0 P Q −→∗1 Q′ and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ , and fv(Q) = ∅, thus x ∈ fv(P ): (iter 0 P Q)[N/x] = iter 0 P [N/x] Q −→∗1 P ′ [N ′ /x] = M ′ [N ′ /x] • M −→∗1 M ′ is iter S(P ) Q R −→∗1 R′ (iter P ′ Q′ R′ ) and is a consequence of P −→∗1 P ′ , Q −→∗1 Q′ , R −→∗1 R′ and fv(R) = ∅. Then we have two cases:

170

Appendix A. Church-Rosser for ΛL

– x ∈ fv(p). Then (iter S(P ) Q R)[N/x]

= iter S(P [N/x]) Q R −→∗1 R′ (iter S(P ′ [N ′ /x]) Q′ R′ ) = (R′ (iter S(P ′ ) Q′ R′ ))[N ′ /x]

– x ∈ fv(Q). Then (iter S(P ) Q R)[N/x]

= iter S(P ) Q[N/x] R ∗ −→1 R′ (iter S(P ′ ) Q′ [N ′ /x] R′ ) = (R′ (iter S(P ′ ) Q′ R′ ))[N ′ /x]

• M −→∗1 M ′ is iter P Q R −→∗1 iter P ′ Q′ R′ and is a consequence of P −→∗1 P ′ , Q −→∗1 Q′ and R −→∗1 R′ . Then we have three subcases: – x ∈ fv(P ). Then (iter P Q R)[N/x]

= iter P [N/x] Q R ∗ −→1 iter P ′ [N ′ /x] Q′ R′ = (iter P ′ Q′ R′ )[N ′ /x]

– x ∈ fv(Q) or x ∈ fv(R). Similar to the previous case. 

Lemma A.0.4 −→∗1 satisfies the diamond property. Proof: By induction on the definition of −→∗1 , one can show that if M −→∗1 M1 and M −→∗1 M2 , then there is a term M3 such that M1 −→∗1 M3 and M2 −→∗1 M3 . • M −→∗1 M1 is M −→∗1 M . Then take M3 ≡ M2 . • M −→∗1 M1 is S(P ) −→∗1 S(P ′ ) and is a consequence of P −→∗1 P ′ . Then M2 ≡ SP ′′ and P −→∗1 P ′′ . By induction hypothesis there is a term P ′′′ with P ′ −→∗1 P ′′′ and P ′′ −→∗1 P ′′′ , thus take M3 ≡ S(P ′′′ ). • M −→∗1 M1 is λx.P −→∗1 λx.P ′ and is a consequence of P −→∗1 P ′ . Then M2 ≡ λx.P ′′ . By induction hypothesis take M3 ≡ λx.P ′′′ (as in the previous case). • M −→∗1 M1 is hP, Qi −→∗1 hP ′ , Q′ i and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ . Then M2 ≡ hP ′′ , Q′′ i. By induction hypothesis there is a term P ′′′ with P ′ −→∗1 P ′′′ and P ′′ −→∗1 P ′′′ and the same for Q, thus take M3 ≡ hP ′′′ , Q′′′ i. • M −→∗1 M1 is N P −→∗1 P ′ Q′ and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ . Then we have two subcases:

171

– M2 ≡ P ′′ Q′′ . Then by induction hypothesis take M3 = P ′′′ Q′′′ (as in the case for pairs). – P ≡ λx.P1 , M2 ≡ P1′′ [Q′′ /x] with P1 −→∗1 P1′′ and Q −→∗1 Q′′ . Then one has P ′ ≡ λx.P1′ with P1 −→∗1 P1′ . By induction hypothesis and Lemma A.0.3 one can take M3 ≡ P1′′′ [P ′′′ /x]. • M −→∗1 M1 is (λx.P )Q −→∗1 P ′ [Q′ /x] and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ . Then we have two subcases: – M2 ≡ (λx.P ′′ )Q′′ . Then by induction hypothesis and Lemma A.0.3 one can take M3 = P ′′′ [Q′′′ /x]. – M2 ≡ P ′′ [Q′′ /x] with P −→∗1 P ′′ and Q −→∗1 Q′′ . By induction hypothesis and Lemma A.0.3 one can can take M3 ≡ P ′′′ [Q′′′ /x]. • M −→∗1 M1 is cond true P Q −→∗1 P ′ and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ . Then we have two subcases: – M2 ≡ P ′′ . Then using the induction hypothesis one can take M3 = P ′′′ . – M2 ≡ cond true P ′′ Q′′ . Then using the induction hypothesis one can take M3 ≡ P ′′′ . • M −→∗1 M1 is cond false P Q −→∗1 Q′ and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ . Analogous to the previous case with M3 ≡ Q′′′ . • M −→∗1 M1 is cond P Q R −→∗1 cond P ′ Q′ R′ (P is neither true nor false) and is a consequence of P −→∗1 P ′ , Q −→∗1 Q′ and R −→∗1 R′ . Then M2 ≡ cond P ′′ Q′′ R′′ , with P −→∗1 P ′′ , Q −→∗1 Q′′ and R −→∗1 R′′ . Then using the induction hypothesis one can take M3 ≡ cond P ′′′ Q′′′ R′′′ . • M −→∗1 M1 is let hx, yi = hP, Qi in R −→∗1 T ′ [P ′ /x, Q′ /y] and is a consequence of P −→∗1 P ′ , Q −→∗1 Q′ and R −→∗1 R′ . Then we have two subcases: – M2 ≡ let hx, yi = hP ′′ , Q′′ i in R′′ , with P −→∗1 P ′′ , Q −→∗1 Q′′ and R −→∗1 R′′ . Using the induction hypothesis and Lemma A.0.3 twice one can take M3 ≡ R′′′ [P ′′′ /x, Q′′′ /y]. – M2 ≡ R′′ [P ′′ /x, Q′′ /y], with P −→∗1 P ′′ , Q −→∗1 Q′′ and R −→∗1 R′′ . Using the induction hypothesis and Lemma A.0.3 one can take M3 ≡ R′′′ [P ′′′ /x, Q′′′ /y]. • M −→∗1 M1 is let hx, yi = P in Q −→∗1 let hx, yi = P in Q′ and is a consequence of P −→∗1 P ′ and Q −→∗1 Q′ . Then we have two subcases: – M2 ≡ let hx, yi = P ′′ in Q′′ , with P −→∗1 P ′′ and Q −→∗1 Q′′ . Using the induction hypothesis one can take M3 ≡ let hx, yi = P ′′′ in Q′′′ .

172

Appendix A. Church-Rosser for ΛL

– P ≡ hP1 , P2 i, therefore P ′ ≡ hP1′ , P2′ i and M2 ≡ Q′′ [P1′′ /x, P2′′ /y], with P1 −→∗1 P1′′ , P2 −→∗1 P2′′ and Q −→∗1 Q′′ . Using the induction hypothesis and Lemma A.0.3 one can take M3 ≡ Q′′′ [P1′′′ /x, P2′′′ /y]. • M −→∗1 M1 is iter 0 Q R −→∗1 Q′ and is a consequence of Q −→∗1 Q′ and R −→∗1 R′ . Then we have two subcases: – M2 ≡ Q′′ , with Q −→∗1 Q′′ and R −→∗1 R′′ . Then using the induction hypothesis take M3 ≡ Q′′′ . – t2 ≡ iter 0 Q′′ R′′ , with Q −→∗1 Q′′ and R −→∗1 R′′ . Then using the induction hypothesis take M3 ≡ R′′′ . • M −→∗1 M1 is iter S(P ) Q R −→∗1 R′ (iter P ′ Q′ R′ ) and is a consequence of P −→∗1 P ′ , Q −→∗1 Q′ and R −→∗1 R′ . Then we have two subcases: – M2 ≡ iter S(P ′′ ) Q′′ R′′ , with P −→∗1 P ′′ , Q −→∗1 Q′′ and R −→∗1 R′′ . Then using the induction hypothesis take M3 ≡ R′′′ (iter P ′′′ Q′′′ R′′′ ). – M2 ≡ R′′ (iter P ′′ Q′′ R′′ ), with P −→∗1 P ′′ , Q −→∗1 Q′′ and R −→∗1 R′′ . Then using the induction hypothesis take M3 ≡ R′′′ (iter P ′′′ Q′′′ R′′′ ). • M −→∗1 M1 is iter P Q R −→∗1 iter P ′ Q′ R′ (q is not of the form (S(P1 ))), and is a consequence of P −→∗1 P ′ , Q −→∗1 Q′ and R −→∗1 R′ . Then M2 ≡ iter P ′′ Q′′ R′′ , with P −→∗1 P ′′ , Q −→∗1 Q′′ and R −→∗1 R′′ . Then using the induction hypothesis take M3 ≡ iter P ′′′ Q′′′ R′′′ . 

Lemma A.0.5 −→∗ is the transitive closure of −→∗1 . Proof: If we represent reduction as a relation (thus as a set of pairs), we have: −→= ⊆−→∗1 ⊆−→∗ Since −→∗ is this transitive closure of →= , so it is of −→∗1 .



Theorem A.0.6 (Confluence) −→ is Church-Rosser. Proof: By Lemma A.0.4 −→∗1 satisfies the diamond property. By Lemma A.0.5 −→∗ is  the transitive closure of −→∗1 . The result follows by Lemma A.0.1.

Appendix B

Types for some primitive recursive functions In the following we present type derivations for terms defined in Chapter 7. • The projection function fst = λx.let hu, vi = x in iter v u λz.z. (Analogous for snd) z : Nat ⊢L z : Nat v : Nat ⊢L v : Nat x : Nat ⊗ Nat ⊢L x : Nat ⊗ Nat

u : Nat ⊢L u : Nat

⊢L λz.z : Nat −◦ Nat

u : Nat, v : Nat ⊢L iter v u λz.z : Nat

x : Nat ⊗ Nat ⊢L let hu, vi = x in iter v u λz.z : Nat ⊢L λx.let hu, vi = x in iter v u λz.z : (Nat ⊗ Nat) −◦ Nat

• The copy function C = λx.iter x h0, 0i (λx.let ha, bi = x in hSa, Sbi)

x : Nat ⊗ Nat ⊢L x : Nat ⊗ Nat

a : Nat ⊢L a : Nat

b : Nat ⊢L b : Nat

a : Nat ⊢L S a : Nat

b : Nat ⊢L S b : Nat

a : Nat, b : Nat ⊢L hS a, S bi : Nat ⊗ Nat

x : Nat ⊗ Nat ⊢L let ha, bi = x in hS a, S bi : Nat ⊗ Nat ⊢L F ≡ λx.let ha, bi = x in hS a, S bi : (Nat ⊗ Nat) −◦ (Nat ⊗ Nat)

⊢L 0 : Nat x : Nat ⊢L x : Nat

⊢L 0 : Nat

⊢L h0, 0i : Nat ⊗ Nat

· · · ⊢L F : (Nat ⊗ Nat) −◦ (Nat ⊗ Nat)

x : Nat ⊢L iter x h0, 0i (λx.let ha, bi = x in hS a, S bi) : Nat ⊗ Nat ⊢L λx.iter x h0, 0i (λx.let ha, bi = x in hS a, S bi) : Nat −◦ (Nat ⊗ Nat)

174

Appendix B. Types for some primitive recursive functions

• Primitive recursive scheme h(x, n) = (iter n F g ′ )x, given the closed functions G : Nat −◦ Nat −◦ Nat and F : Nat → Nat. · · · ⊢L G : Nat −◦ Nat −◦ Nat

z1 : Nat ⊢L z1 : Nat

y : Nat −◦ Nat ⊢L y : Nat −◦ Nat

z1 : Nat ⊢L Gz1 : Nat −◦ Nat

z2 : Nat ⊢L z2 : Nat

y : Nat −◦ Nat, z2 : Nat ⊢L yz2 : Nat

y : Nat −◦ Nat, z1 : Nat, z2 : Nat ⊢L Gz1 (yz2 ) : Nat

· · · z : Nat ⊢L C z : Nat ⊗ Nat

· · · y : Nat −◦ Nat, z1 : Nat, z2 : Nat ⊢L Gz1 (yz2 ) : Nat

y : Nat −◦ Nat, z : Nat ⊢L let hz1 , z2 i = C z in Gz1 (yz2 ) : Nat y : Nat −◦ Nat ⊢L λz.let hz1 , z2 i = C z in Gz1 (yz2 ) : Nat −◦ Nat ⊢L g ′ ≡ λyz.let hz1 , z2 i = C z in Gz1 (yz2 ) : (Nat −◦ Nat) −◦ (Nat −◦ Nat)

x : Nat ⊢L x : Nat

· · · ′ ⊢L g : (Nat −◦ Nat) −◦ (Nat −◦ Nat)

· · · ⊢L F : Nat −◦ Nat

n : Nat ⊢L iter n F g ′ : Nat −◦ Nat

x : Nat ⊢L x : Nat

n : Nat, x : Nat ⊢L (iter n F g ′ )x : Nat

• Predecessor function pred = λn.fst(iter n h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)). u : Nat ⊢L u : Nat · · · x : Nat ⊗ Nat ⊢L C(snd x) : Nat ⊗ Nat

t : Nat ⊢L t : Nat

u : Nat ⊢L S u : Nat

t : Nat, u : Nat ⊢L ht, S ui : Nat ⊗ Nat

x : Nat ⊗ Nat ⊢L let ht, ui = C(snd x) in ht, S ui : Nat ⊗ Nat ⊢L f ≡ λx.let ht, ui = C(snd x) in ht, S ui : (Nat ⊗ Nat) −◦ (Nat ⊗ Nat)

n : Nat ⊢L n : Nat

· · · ⊢L h0, 0i : Nat ⊗ Nat

· · · ⊢L f : (Nat ⊗ Nat) −◦ (Nat ⊗ Nat)

n : Nat ⊢L iter n h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui) : Nat ⊗ Nat n : Nat ⊢L fst(iter n h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)) : Nat ⊢L fst(iter n h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)) : Nat −◦ Nat

• The Ackermann’s function ack = (iter m succ A) n.

x : Nat ⊢L x : Nat x : Nat ⊢L S x : Nat ⊢L succ ≡ λx.S x : Nat −◦ Nat

175

u : Nat ⊢L u : Nat

⊢L 0 : Nat

u : Nat ⊢L S u : Nat

⊢L S 0 : Nat

g : Nat −◦ Nat ⊢L g : Nat −◦ Nat

g : Nat −◦ Nat, u : Nat ⊢L iter (S u) (S 0) g : Nat g : Nat −◦ Nat ⊢L λu.iter (S u) (S 0) g : (Nat −◦ Nat) ⊢L A ≡ λgu.iter (S u) (S 0) g : (Nat −◦ Nat) −◦ (Nat −◦ Nat)

m : Nat ⊢L m : Nat

· · · ⊢L succ : Nat −◦ Nat

· · · ⊢L A : (Nat −◦ Nat) −◦ (Nat −◦ Nat)

m : Nat ⊢L iter m succ A : Nat −◦ Nat

n : Nat ⊢L n : Nat

m : Nat, n : Nat ⊢L (iter m succ A) n : Nat ⊢L λm n.(iter m succ A) n : Nat −◦ Nat −◦ Nat

176

Appendix B. Types for some primitive recursive functions

Appendix C

Proofs of Chapter 7 Proof of Lemma 7.3.15

If M is a term in Λ+X L , and x is a free variable in M , then:

1. fv([x]M ) = fv(M ) +(X∪{x})

2. [x]M is a term in ΛL

Proof: By simultaneous induction on M . • M ≡ x, true, false and 0. The results are trivially true. • M ≡ S(N ). (I.H.)

1. fv([x]S(N )) = fv(S([x]N ) = fv([x]N ) = fv(N ) = fv(S(N )) +(X∪{x})

2. [x]S(N ) = S([x]N ). By induction hypothesis [x]N is in ΛL +(X∪{x}) . is in ΛL

, thus S([x]N )

• M ≡ λy.N . Similar to the previous case. • M ≡ P Q, and x ∈ fv(P ), x ∈ / fv(Q). (I.H.)

1. fv([x]P Q) = fv(([x]P )Q) = fv([x]P ) ∪ fv(Q) = fv(P ) ∪ fv(Q) = fv(P Q) +(X∪{x})

and Q is in 2. [x](P Q) = ([x]P )Q. By induction hypothesis [x]P is in ΛL +(X∪{x}) ΛL by Proposition 7.3.14. By hypothesis fv(P ) ∩ fv(Q) ∩ X = ∅. By induction hypothesis (1) fv([x]P ) = fv(P ), thus fv([x]P ) ∩ fv(Q) ∩ (X ∪ {x}) = ∅ +(X∪{x}) . (because x ∈ / fv(Q)). Therefore ([x]P )Q is in ΛL • M ≡ P Q, and x ∈ / fv(P ), x ∈ fv(Q). Similar to the previous case. • M ≡ P Q, and x ∈ fv(P ), x ∈ fv(Q). Then

178

Appendix C. Proofs of Chapter 7

1. In the following, note that if x ∈ M , then fv(M [y/x]) = (fv(M ) \ {x}) ∪ {y}. fv([x]P Q)

= = = = (I.H.)

= =

fv(iter S2 (0) (λx1 x2 .(([x]P )[x1 /x])(([x]Q)[x2 /x])) λz.zx (fv((([x]P )[x1 /x])(([x]Q)[x2 /x]))) \ {x1 , x2 }) ∪ {x} ((fv(([x]P )[x1 /x]) ∪ fv(([x]Q)[x2 /x])) \ {x1 , x2 }) ∪ {x} (((fv([x]P ) \ {x}) ∪ {x1 }) ∪ ((fv([x]Q) \ {x}) ∪ {x2 })) \ {x1 , x2 }) ∪ {x} (((fv(P ) \ {x}) ∪ ((fv(Q) \ {x}) ∪ {x1 , x2 })) \ {x1 , x2 }) ∪ {x} (((fv(P ) \ {x}) ∪ ((fv(Q) \ {x})) ∪ {x} = fv(P ) ∪ fv(Q) = fv(hhP Qii)

2. [x](P Q) = fv(iter S2 (0) (λx1 x2 .((([x]P )[x1 /x])(([x]Q)[x2 /x])) λz.zx Note that, by induction hypothesis (1): fv([x]P ) = fv(P ) = YP ∪ {x} fv([x]Q) = fv(Q) = YQ ∪ {x} By hypothesis, fv(P ) ∩ fv(Q) ∩ X = ∅, thus YP ∩ YQ ∩ X = ∅. Note that fv(([x]P )[x1 /x]) = YP ∪ {x1 } fv(([x]Q)[x2 /x]) = fv(Q) = YQ ∪ {x2 } Therefore fv(Axx1 ) ∩ fv(Axx2 ) ∩ (X ∪ {x}) = ∅, and since x1 , x2 ∈ fv((Axx1 )(Axx2 )), +(X∪{x}) . Finally, both S2 (0) and λz.zx are terms then (λx1 x2 .(Axx1 )(Axx2 )) is in ΛL +(X∪{x}) in ΛL , and fv(S2 (0)) ∩ fv((λx1 x2 .(Axx1 )(Axx2 ))) ∩ fv(λz.zx) ∩ (X ∪ {x}) = ∅, +(X∪{x}) therefore iter S2 (0) (λx1 x2 .(Axx1 )(Axx2 )) λz.zx is a term in ΛL . • M ≡ hP, Qi. Similar to P Q considering the three different possibilities. • M ≡ iter P Q R. Similar to the previous cases considering all different possibilities. • M ≡ let hy, zi = N in P . Similar to previous cases considering the three different possibilities. • M ≡ cond P Q R, and x ∈ / fv(P ), x ∈ fv(Q) ∩ fv(R). 1. fv([x]cond P Q R)

= = (I.H.)

=

fv(cond P ([x]Q) [x]R) fv(P ) ∪ fv([x]Q) ∪ fv(R) fv(P ) ∪ fv(Q)fv(R) = fv(cond P Q R)

2. [x]cond P Q R = cond P ([x]Q) [x]R. By induction hypothesis [x]Q and [x]R +(X∪{x}) +(X∪{x}) are in ΛL , and P is in ΛL by Proposition 7.3.14. By hypothesis, fv(P ) ∩ fv(Q) ∩ X = ∅, and (fv(Q) ∩ X) = (fv(R) ∩ X). Thus, by induction hypothesis (1), fv(P ) ∩ fv([x]Q) ∩ (X ∪ {x}) = ∅, and (fv([x]Q) ∩ (X ∪ {x})) = +(X∪{x}) (fv([x]R) ∩ (X ∪ {x})). Thus cond P ([x]Q) [x]R is in ΛL .

179

• M ≡ cond P Q R, and x ∈ / fv(P R), x ∈ fv(Q). 1. fv([x]cond P Q R)

= = (I.H.)

=

fv(cond P [x]Q (E(x, h τ1 i )R)) fv(P ) ∪ fv([x]Q) ∪ {x} ∪ fv(R) fv(P ) ∪ fv(Q) = fv(cond P Q R)

2. [x]cond P Q R = fv(cond P ([x]Q) (E(x, h τ1 i )R). By induction hypothesis [x]Q +(X∪{x}) +(X∪{x}) is in ΛL , and P and R are in ΛL by Proposition 7.3.14. Note also +(X∪{x}) that E(x, h τ1 i ) is in ΛL , therefore is in ΛL , and since fv(E(x, h τ1 i )) = {x}, +(X∪{x}) and {x} ∩ fv(R) = ∅, then (E(x, h τ1 i )R) is in ΛL . By hypothesis fv(P ) ∩ fv(Q) ∩ X = ∅, and (fv(Q) ∩ X) = (fv(R) ∩ X). Thus fv(P ) ∩ fv([x]Q) ∩ X = ∅, and (fv([x]Q) ∩ (X ∪ {x}) = (fv(E(x, h τ1 i )R) ∩ (X ∪ {x})), because x ∈ fv([x]Q) +(X∪{x}) and x ∈ fv(E(x, h τ1 i )R). Thus cond P ([x]Q) (E(x, h τ1 i )R) is in ΛL . All the other four cases for cond are proved using similar reasonings. 

Proof of Lemma 7.3.16

Let M be a System T term, then fv(hhM i ) = fv(M ).

Proof: By induction on M . • M ≡ true, false or 0. Then fv(hhM i ) = fv(M ) = ∅. • M ≡ x. Then fv(hhxii) = fv(x). • M ≡ S(N ). Then fv(hhS(N )ii) = fv(S(hhN i )) (I.H.)

= fv(hhN i ) = fv(N ) = fv(S(N )) • M ≡ λy.N , and y ∈ fv(N ). fv(hhλy.N i ) = fv(λy.[y]hhN i ) = fv([y]hhN i ) \ {y} = fv(hhN i ) \ {y}

(by Lemma 7.3.15)

• M ≡ λy.N , and y ∈ / fv(N ). Note that by induction hypothesis, y ∈ / fv(hhN i ). Then fv(hhλy.N i )

= = = (I.H.)

=

λy.E(y, h τ1 i )hhN i ) and Γ ⊢T N : τ1 → τ2 (fv(E(y, h τ1 i )) ∪ fv(hhN i )) \ {y} ({y} ∪ fv(hhN i )) \ {y} (by Lemma 7.3.3) ({y} ∪ fv(N )) \ {y} = fv(λy.N )

180

Appendix C. Proofs of Chapter 7

• M ≡ P Q. Then fv(hhP Qii) = fv(hhP i h Qii)) (I.H.)

= fv(hhP i ) ∪ fv(hhP i ) = fv(P ) ∪ fv(Q) = fv(P Q) • M ≡ hP, Qi. Similar to P Q. • M ≡ iter P Q R. Similar to the previous cases. • M ≡ cond P Q R. Similar to the previous cases. • M ≡ π1 N . Then fv(hhπ1 N i ) = let hy, zi = h N i in E(z, h τ2 i )y) = (fv(hhN i ) \ {y, z}) ∪ fv(E(z, h τ2 i )y)

and

Γ ⊢T N : τ1 × τ2

(I.H.)

= (fv(hhN i ) \ {y, z})) ∪ {z, y} = fv(N ) = fv(π1 (N )) • M ≡ π2 N . Similar to the previous case. 

Proof of Lemma 7.3.17

Let M be a System T term, then h M i is a term in Λ+∅ L .

Proof: The only non trivial cases are the cases where M ≡ λx.P , M ≡ π1 (P ), and M ≡ π2 (P ). • M ≡ λx.P , and x ∈ fv(P ). Then h M i = λx.[x]hhP i . By induction hypothesis h P i is a +∅ term in Λ+∅ L and by Lemma 7.3.15 x ∈ fv([x]hhP i ), therefore λx.[x]hhP i is in ΛL . • M ≡ λx.P , and x ∈ fv(P ). Then h M i = λx.(E(x, h τ i )hhP i ). By induction hypoth+∅ esis h P i is in Λ+∅ L , and since E(x, h τ i ) is a System L term, therefore is in ΛL . Since fv(E(x, h τ i )) ∩ fv(hhP i ∩ ∅ = ∅, then E(x, h τ i )hhM i is in Λ+∅ L , and since x ∈ +∅ fv(E(x, h τ i )hhM i ), then λx.(E(x, h τ i )hhP i ) is in ΛL . • M ≡ π1 (P ). Then h (iiM ) = let hx, yi = h P i in E(y, h τ2 i )x. By induction hypothesis +∅ h P i is in Λ+∅ L . The terms E(y, h τ2 i ) and x are both in ΛL , and since fv(E(y, h τ2 i )) ∩ fv(x) = ∅, then E(y, h τ2 i )x is in Λ+∅ L , and x, y ∈ fv(E(y, h τ2 i )x), therefore let hx, yi = +∅ h P i in E(y, h τ2 i )x is in ΛL . Similar to M ≡ π2 (N ). 

181

Proof of Lemma 7.3.21 then Γ ⊢L+X ′ [x]M : τ .

If Γ ⊢L+X M : τ , where x ∈ X ⊆ fv(M ), and let X ′ = X \ {x},

Proof: By induction on M . • If M ≡ 0, true or false, the result is trivial. • M ≡ x. Then [x]x = x, and by the axiom rule both x : τ ⊢L+{x} x : τ and x : τ ⊢L+∅ x : τ are valid derivations. • M ≡ S(N ). Then [x]S(N ) = S(x[N ]), and Γ ⊢L+X S(N ) : Nat if Γ ⊢L+X N : Nat. By induction hypothesis Γ ⊢L+X ′ [x]N : Nat, therefore Γ ⊢L+X ′ S([x]N ) : Nat. • M ≡ λy.N . Similar to the previous case. • M ≡ P Q, and x ∈ fv(P ), x ∈ / fv(Q). Then [x]P Q = ([x]P )Q, and Γ ⊢L+X P Q : τ ′ if Γ1 ⊢L+X1 P : τ −◦ τ , Γ2 ⊢L+X2 Q : τ ′ , with Γ = Γ1 , Γ2 , and X = X1 ∪ X2 . By induction hypothesis Γ1 ⊢L+X1′ [x]P : τ ′ −◦ τ , (X1′ = X1 \ {x}). Therefore Γ1 , Γ2 ⊢L+X ′ (x[P ])Q : τ . Note that X ′ = (X1 \ {x}) ∪ X2 = (X1 ∪ X2 ) \ {x}, since x ∈ / X2 . • M ≡ P Q, and x ∈ / fv(P ), x ∈ fv(Q). Similar to the previous case. • M ≡ P Q, and x ∈ fv(P ), x ∈ fv(Q). Then Γ1 , Γ2 , x : τ ′′ ⊢L+X P Q : τ if Γ1 , x : τ ′′ ⊢L+X1 P : τ ′ −◦ τ and Γ2 , x : τ ′′ ⊢L+X2 Q : τ ′ By induction hypothesis Γ1 , x : τ ′′ ⊢L+X1′ [x]P : τ ′ −◦ τ , and Γ2 , x : τ ′′ ⊢L+X2′ [x]Q : τ ′ (X1′ = X1 \ {x}, X2′ = X2 \ {x}). Thus Γ1 , x1 : τ ′′ ⊢L+X1′ ([x]P )[x1 /x] : τ ′ −◦ τ , and Γ2 , x2 : τ ′′ ⊢L+X2′ ([x]Q)[x2 /x] : τ ′ . Therefore Γ1 , x1 : τ ′′ , Γ2 , x2 : τ ′′ ⊢L+X ′ (Axx1 P )(Axx2 Q) : τ (X ′ = X1 ∪ X2 = X \ {x}). Then Γ1 , Γ2 ⊢L+X ′ (λx1 x2 .(Axx1 P )(Axx2 Q)) : τ ′′ −◦ τ ′′ −◦ τ . Since ⊢L+∅ S2 (0) : Nat, x : τ ′′ ⊢L+∅ λz.zx : (τ ′′ −◦ τ ′′ −◦ τ ) −◦ (τ ′′ −◦ τ ), and x : τ ′′ ⊢L+∅ λz.zx : (τ ′′ −◦ τ ) −◦ τ , then Γ1 , Γ2 , x : τ ′′ ⊢L+X ′ iter S2 (0) (λx1 x2 .(Axx1 P )(Axx2 Q)) λz.zx : τ . • M ≡ hP, Qi. Similar to P Q considering the three different possibilities. • M ≡ iter P Q R. Similar to the previous cases considering all different possibilities. • M ≡ let hy, zi = N in P . Similar to previous cases considering the three different possibilities. • M ≡ cond P Q R. Similar to the previous cases considering all different possibilities. 

182

Appendix C. Proofs of Chapter 7

Proof of Lemma 7.3.22

If Γ ⊢T M : τ , then h Γ|fv(M ) i ⊢L+fv(M ) h M i : h τ i .

Proof: By induction on the type derivation Γ ⊢T M : τ . We distinguish cases depending on the last typing rule applied. • For (Zero), (True) and (False), the result is trivial. • Axiom: x : τ ⊢T x : τ , and x : h τ i ⊢L+{x} x : h τ i . • Exchange: The result follows trivially by induction hypothesis. • Weakening: Γ, x : τ ′ ⊢T M : τ if Γ ⊢T M : τ . By induction hypothesis h Γ|fv(M ) i ⊢L+fv(M ) h M i : h τ i We consider two cases: – x ∈ fv(M ), in which case h (Γ, x : τ ′ )|fv(M ) i = h Γ|fv(M ) i , x : h τ ′ i , and by weakening (since x ∈ fv(M )) h Γ|fv(M ) i , x : h τ ′ i ⊢L+fv(M ) h M i : h τ i . – x ∈ / fv(M ), in which case h (Γ, x : τ ′ )|fv(M ) i = h Γ|fv(M ) i , and by induction we already have h Γ|fv(M ) i ⊢L+fv(M ) h M i : h τ i . • Contraction: Γ, x : τ ′ ⊢T M : τ if Γ, x : τ ′ , x : τ ′ ⊢T M : τ . By induction hypothesis h (Γ, x : τ ′ , x : τ ′ )|fv(M ) i ⊢L+fv(M ) h M i : h τ i Again, we consider two cases: – x ∈ fv(M ), in which case h (Γ, x : τ ′ , x : τ ′ )|fv(M ) i = h Γ|fv(M ) i , x : h τ ′ i , x : h τ ′ i , and by contraction (since x ∈ fv(M )) h Γ|fv(M ) i , x : h τ ′ i ⊢L+fv(M ) h M i : h τ i . – x∈ / fv(M ), in which case h (Γ, x : τ ′ , x : τ ′ )|fv(M ) i = h Γ|fv(M ) i , and by induction we already have h Γ|fv(M ) i ⊢L+fv(M ) h M i : h τ i . • For Succ, −◦Elim, ⊗Intro, Cond, and Iter, the result follows trivially by induction hypothesis. • −◦Intro: Γ ⊢T λx.N : τ1 → τ2 if Γ, x : τ1 ⊢T N : τ2 . By induction hypothesis h (Γ, x : τ1 )|fv(N ) i ⊢L+fv(N ) h N i : h τ2 i . We have two distinct cases: – If x ∈ fv(N ), then h λx.N i = (λx.[x]hhN i ), and (Γ, x : τ1 )|fv(N ) = Γ|fv(N ) , x : τ1 . Thus h Γ|fv(N ) i , x : h τ1 i ⊢L+fv(N ) h N i : h τ2 i . By Lemma 7.3.22 h Γ|fv(N ) i , x : h τ1 i ⊢L+fv(M ) [x]hhN i : h τ2 i , thus h Γ|fv(M ) i ⊢L+fv(M ) λx.[x]hhN i : h τ1 i −◦ h τ2 i . – x ∈ / fv(N ), thus fv(M ) = fv(N ). then h λx.N i = (λx.E(x, h τ1 i )hhN i ), (Γ, x : τ1 )|fv(N ) = Γ|fv(M ) . Thus h Γ|fv(M ) i ⊢L+fv(M ) h N i : h τ2 i . By Lemma 7.3.22 x : h τ1 i ⊢L∅ E(x, h τ1 i ) : h τ2 i −◦ h τ2 i , therefore h Γ|fv(M ) i , x : h τ1 i ⊢L+fv(M ) E(x, h τ1 i )hhN i : h τ2 i . Thus h Γ|fv(M ) i ⊢L+fv(M ) λx.E(x, h τ1 i )hhN i : h τ1 i −◦ h τ2 i . • ⊗Elim: Γ ⊢T π1 N : τ1 if Γ ⊢T N : τ1 × τ2 . By induction hypothesis h Γ|fv(N ) i ⊢L+fv(N ) h N i : h τ1 i ⊗ h τ2 i . By Lemma 7.3.22 y : h τ2 i ⊢L∅ E(y, h τ2 i ) : h τ1 i −◦ h τ1 i , and

183

x : h τ1 i ⊢L∅ x : h τ1 i , therefore y : h τ2 i , x : h τ1 i ⊢L∅ E(y, h τ2 i )x : h τ1 i . Thus h Γ|fv(M ) i ⊢L+fv(M ) let hx, yi = h N i in E(y, h τ2 i )x : h τ1 i . Similar to π2 (N ). 

Proof of Lemma 7.3.29 [y](M [N/x]).

If M is in Λ+X L , x, y ∈ fv(M ), and fv(N ) = ∅, then ([y]M )[N/x] =

Proof: By induction on M . • M ≡ x, true, false and 0. The result follows trivially. • M ≡ S(P ). Then ([y]S(P ))[N/x]

= = (I.H.)

=

S([y]P ))[N/x] S(([y]P )[N/x]) S([y](P [N/x])) = [y]S(P [N/x]) = [y](S(P )[N/x])

• M ≡ λy.P . Similar to the previous case. • M ≡ P Q, x, y ∈ fv(P ) and x, y ∈ fv(Q). Then ([y]P Q)[N/x]

= = =

iter S2 (0) (λy1 y2 .(([y]P )[y1 /y])(([y]Q)[y2 /y])) (λz.zy)[N/x] iter S2 (0) (λy1 y2 .(([y]P )[y1 /y][N/x])(([y]Q)[y2 /y])[N/x]) (λz.zy) iter S2 (0) (λy1 y2 .(([y]P )[N/x][y1 /y])(([y]Q)[N/x][y2 /y]) (λz.zy)

(I.H.)

iter S2 (0) (λy1 y2 .(([y]P [N/x])[y1 /y])(([y]Q[N/x])[y2 /y])) (λz.zy) [y]((P [N/x])(Q[N/x])) = [y]((P Q)[N/x])

= =

Other cases for P Q are proved in a similar way, or trivially using induction hypothesis. Other cases are proved in a similar way.

Proof of Lemma 7.3.30 h M i [hhN i /x] = h M [N/x]ii.



If M is a System T term, x ∈ fv(M ), and fv(N ) = ∅, then

Proof: By induction on M . • M ≡ x, true, false and 0. The result follows trivially.

184

Appendix C. Proofs of Chapter 7

• M ≡ S(P ). Then (hhS(P )ii)[hhN i /x] = S(hhP i ))[hhN i /x] (I.H.)

= S(hhP i [hhN i /x]) = S(hhP [N/x]ii) = h S(P )[N/x]ii • M ≡ λy.P , and y ∈ fv(P ). (hhλy.P i )[hhN i /x]

= = =

(λy.[y]hhP i )[hhN i /x] λy.([y]hhP i )[hhN i /x] λy.([y](hhP i [hhN i /x]))

(I.H.)

=

λy.([y](hhP [N/x]ii)) = h (λy.P [N/x])ii = h (λy.P )[N/x]ii

• M ≡ λy.P , and y ∈ / fv(P ). (hhλy.P i )[hhN i /x]

= = (I.H.)

=

(λy.E(y, h τ i )hhP i )[hhN i /x] λy.(E(y, h τ i )(hhP i [hhN i /x])) λy.(E(y, h τ i )(hhP [N/x]ii)) = h (λy.P [N/x])ii = h (λy.P )[N/x]ii

• M ≡ P Q, hP, Qi, iter P Q R and cond P Q R. The result follows easily by induction hypothesis. • M ≡ π1 N . Then h π1 P i [hhN i /x]

= = (I.H.)

= =

let hy, zi = h P i in E(z, h τ2 i )y)[hhN i /x] let hy, zi = h P i [hhN i /x] in E(z, h τ2 i )y) let hy, zi = h P [N/x]ii in E(z, h τ2 i )y) h π1 P [N/x]ii = h π1 P [N/x]ii

M ≡ π2 N is similar. 

Proof of Lemma 7.3.31 M [N/x].

∗ If M is in Λ+X L , x ∈ fv(M ), and fv(N ) = ∅, then ([x]M )[N/x] −→

Proof: We proceed by induction on M . • M ≡ x, true, false and 0. The result follows trivially. • M ≡ S(P ). Then ([x]S(P ))[N/x] = S([x]P ))[N/x] (I.H.)

= S(([x]P )[N/x]) = S(P [N/x]) = S(P )[N/x]

185

• M ≡ λy.P . Similar to the previous case. • M ≡ P Q, x ∈ fv(P ), x 6∈ fv(Q). ([x]P Q)[N/x] = (([x]P )Q)[N/x] (I.H.)

= (([x]P )[N/x])Q = (P [N/x])Q = (P Q)[N/x] • M ≡ P Q, x 6∈ fv(P ), x ∈ fv(Q). Similar to above. • M ≡ P Q, x ∈ fv(P ), x ∈ fv(Q). ([x]P Q)[N/x]

= = −→∗ −→∗ = (I.H.)

= =

iter S2 (0) (λx1 x2 .(([x]P )[x1 /x])(([x]Q)[x2 /x])) (λz.zx)[N/x] iter S2 (0) (λx1 x2 .(([x]P )[x1 /x])(([x]Q)[x2 /x])) (λz.zN ) (λz.zN )((λz.zN )(λx1 x2 .(([x]P )[x1 /x])(([x]Q)[x2 /x]))) (([x]hhP i )[x1 /x])(([x]hhQii)[x2 /x])[hhN i /x1 ][hhN i /x2 ] ((([x]P )[x1 /x])[N/x1 ])((([x]Q)[x1 /x])[N/x2 ]) (P [x1 /x][N/x1 ])(Q[x2 /x][N/x]) (P [N/x])(Q[n/x]) = (P Q)[N/x]

The other cases are proved in a similar way.



186

Appendix C. Proofs of Chapter 7

Appendix D

Correctness of some System L arithmetic functions Lemma D.0.7 Let add = λxy.iter x y λx.S(x) For any M ≡ Sm (0), N ≡ Sn (0), then add M N −→∗ Sm+n (0). Proof: By induction on m. • Basis: add 0 N

= (λxy.iter x y λx.S(x)) 0 N ∗ −→ iter 0 N λx.S(x)) −→ N

• Induction: m = t + 1 add M N

= (λxy.iter x y λx.S(x)) M N ∗ −→ iter St+1 (0) Sn (0) λx.S(x) −→∗ S(iter St (0) Sn (0) λx.S(x)) (I.H.)

−→∗ S(St+n (0)) = St+1+n (0) = Sm+n (0) 

Lemma D.0.8 Let mult = λxy.iter x 0 (add y) For any M ≡ Sm (0), N ≡ Sn (0), then add M N −→∗ Sm∗n (0). Proof: By induction on m.

188

Appendix D. Correctness of some System L arithmetic functions

• Basis: mult 0 N

= (λxy.iter x 0 (add y)) 0 N ∗ −→ iter 0 0 (add y) −→ 0

• Induction: m = t + 1 mult M N

= (λxy.iter x 0 (add y)) M N ∗ −→ iter St+1 (0) 0 (add Sn (0)) −→∗ add Sn (0) (iter St (0) 0 (add Sn (0))) (I.H.)

−→∗ add Sn (0) St∗n (0) −→∗ Sn+t∗n (0) = Sn∗(1+t) (0) = Sn∗m (0) 

Lemma D.0.9 Let pred = λn.fst(iter n h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)) Then pred 0 = 0 pred S(M ) = M Proof: Consider N ≡ Sn (0) • If n = 0, then pred 0

= (λn.fst(iter n h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)))0 −→ fst(iter 0 h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)) −→ fst(h0, 0i) −→ 0

• If n = t + 1, then pred N −→ fst(iter N h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)) We now prove by induction that for any n = t + 1, then iter St+1 (0) h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui) −→∗ hSt , St+1 (0)i – If n = 1, then

−→ −→ −→∗

iter S(0) h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui) (λx.let ht, ui = C(snd x) in ht, S ui) (iter 0 h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)) (λx.let ht, ui = C(snd x) in ht, S ui)h0, 0i h0, S 0i

189

– If n = t + 2, then

−→

iter St+2 (0) h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui) (λx.let ht, ui = C(snd x) in ht, S ui) (iter St+1 (0) h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui))

(I.H.)

−→∗ (λx.let ht, ui = C(snd x) in ht, S ui)hSt (0), St+1 (0)i −→∗ hSt+1 (0), S(St+1 (0))i = hSt+1 (0), St+2 (0)i Now we have pred St+1 (0)

−→ fst(iter St+1 (0) h0, 0i (λx.let ht, ui = C(snd x) in ht, S ui)) −→∗ fst(hSt (0), St+1 (0)i) −→ St (0) 

190

Appendix D. Correctness of some System L arithmetic functions

References Abramsky, S. (1993). Computational Interpretations of Linear Logic. Theoretical Computer Science, 111(1–2):3–57. Alves, S., Fern´ andez, M., Florido, M., and Mackie, I. (2006a). The Power of ClosedReduction Strategies. In Proceedings of the International Workshop on Rewriting Strategies (WRS’06), FLOC 2006, Seattle. Revised Selected Papers, Electronic Notes in Theoretical Computer Science. Elsevier Science. Alves, S., Fern´ andez, M., Florido, M., and Mackie, I. (2006b). The Power of Linear Functions. In Proceedings of Computer Science Logic, (CSL’06), volume 4207 of Lecture Notes in Computer Science, pages 119–134. Springer Verlag. Alves, S., Fern´ andez, M., Florido, M., and Mackie, I. (2007). Iterator Types. In Proceedings of Foundations of Software Science and Computation Structures, (FOSSACS’07). Accepted for publication. Alves, S. and Florido, M. (2002). On the relation between rank 2 intersection types and simple types. In Proceedings of APPIA-GULP-PRODE Joint Conference on Declarative Programming (AGP’02), pages 259–274. Alves, S. and Florido, M. (2004). Linearisation by Program Transformation. In Bruynooghe, M., editor, Proceedings of Logic Based Program Synthesis and Transformation (LOPSTR’03), Uppsala, Sweden, August 25-27, 2003, Revised Selected Papers, volume 3018 of Lecture Notes in Computer Science, pages 160–175. Springer Verlag. Alves, S. and Florido, M. (2005). Weak linearisation of the lambda calculus. Theoretical Computer Science, 342(1):79–103. Amadio, R. M. and Curien, P.-L. (1998). Domains and Lambda-Calculi. Number 46 in Cambridge Tracts in Theoretical Computer Science. Cambridge University Press, New York, NY, USA. Asperti, A. (1998). Light affine logic. In Proceedings of Logic in Computer Science (LICS’98), pages 300–308. IEEE Computer Society Press.

192

REFERENCES

Asperti, A. and Guerrini, S. (1998). The optimal implementation of functional programming languages, volume 5 of Cambridge Tracts in Theoretical Computer Science. Cambridge University Press, New York, NY, USA. Asperti, A. and Laneve, C. (1995). Paths, computations and labels in the lambda-calculus. Theoretical Computer Science, 142(2):277–297. Asperti, A. and Roversi, L. (2002). Intuitionistic light affine logic. ACM Transactions on Computational Logic, 3(1):137–175. Baader, F. and Nipkow, T. (1998). Term rewriting and all that. Cambridge University Press, New York, NY, USA. Baillot, P. and Mogbil, V. (2004). Soft lambda-calculus: a language for polynomial time computation. In Proceedings of Foundations of Software Science and Computation Structures (FOSSACS’04), Lecture Notes in Computer Science, pages 27–41. Springer Verlag. Barendregt, H. P. (1984). The Lambda Calculus: Its Syntax and Semantics, volume 103 of Studies in Logic and the Foundations of Mathematics. North-Holland, Amsterdam, second, revised edition. Barendregt, H. P. (1992). Lambda Calculi with Types. In Abramsky, S., Gabbay, D. M., and Maibaum, T., editors, Handbook of Logic in Computer Science, volume 2. Clarendon Press, Oxford. Barendregt, H. P. (1997). The Impact of the Lambda Calculus on Logic and Computer Science. Bulletin of Symbolic Logic, 3(3):181–215. Barendregt, H. P., Bergstra, J., Klop, J. W., and Volken, H. (1976). Degrees, reductions and representability in the lambda calculus. Technical Report Preprint no.22, University of Utrecht, Department of Mathematics. Barendregt, H. P., Coppo, M., and Dezani-Ciancaglini, M. (1983). A Filter Lambda Model and the Completeness of Type Assignment. Journal of Symbolic Logic, 48(4):931–940. Benton, N., Bierman, G., de Paiva, V., and Hyland, M. (1992). Term assignement for intuitionistic linear logic. Technical report, Computing Laboratory, University of Cambridge. Benton, N. and Wadler, P. (1996). Linear logic, monads, and the lambda calculus. In Proceedings of Logic in Computer Science (LICS’96), pages 420–431. IEEE Computer Society Press. Bruynooghe, M. (1982). The Memory Management of PROLOG Implementations. In Clark, K. and T¨ arnlund, S.-A., editors, Logic Programming, pages 83–98. Academic Press.

REFERENCES

193

Bucciarelli, A., Lorenzis, S. D., Piperno, A., and Salvo, I. (1999). Some Computational Properties of Intersection Types. In Proceedings of Logic in Computer Science (LICS’99), pages 109–118. IEEE Computer Society Press. Carlier, S., Polakow, J., Wells, J. B., and Kfoury, A. J. (2004). System E: Expansion Variables for Flexible Typing with Linear and Non-linear Types and Intersection Types. In Proceedings of (ESOP’04), Lecture Notes in Computer Science, pages 294–309. Springer Verlag. Carlier, S. and Wells, J. B. (2004). Type inference with expansion variables and intersection types in system E and an exact correspondence with beta-reduction. In Proceedings of Principles and Practice of Declarative Programming (PPDP’04), Lecture Notes in Computer Science, pages 132–143. Springer Verlag. Church, A. (1932 and 1933). A set of postulates for the foundation of logic. Annals of Mathematics, 33–34(2):346–366 and 839–864. Church, A. (1936). An unsolvable problem of elementary number theory. American Journal of Mathematics, 58(2):345–363. Church, A. (1940). A Formulation of the Simple Theory of Types. Journal of Symbolic Logic, 5(2):56–68. Church, A. and Rosser, J. B. (1936). Some Properties of Conversion. Transactions of the American Mathematical Society, 39(3):472–482. Coppo, M. and Dezani-Ciancaglini, M. (1980). An extension of the basic functionality theory for the λ-calculus. Notre Dame Journal of Formal Logic, 21(4):685–693. Coppo, M., Dezani-Ciancaglini, M., and Venneri, B. (1980). Principal Type Schemes and lambda-Calculus Semantics. In Seldin, J. P. and Hindley, J. R., editors, To H. B. Curry: Essays on Combinatory Logic, Lambda Calculus and Formalism, pages 535–560. Academic Press, Inc., New York, USA. Coppo, M., Dezani-Ciancaglini, M., and Venneri, B. (1981). Functional Characters of Solvable Terms. Zeitschrift f¨ ur Mathematische Logik und Grundlagen der Mathematik, 27:45–58. Coppo, M. and Giannini, P. (1995). Principal types and unification for simple intersection type systems. Information and Computation, 122(1):70–96. Curry, H. (1969). Modified basic functionality in combinatory logic. Dialectica, 23:83–92. Curry, H. and Feys, R. (1958). Combinatory Logic, volume 1. North-Holland, Amsterdam.

194

REFERENCES

Curry, H. B. (1934). Functionality in Combinatory Logic. In Proceedings of the National Academy of Science, U.S.A, volume 20, pages 584–590. National Academy of Sciences. Damiani, F. and Giannini, P. (1994). A decidable intersection type system based on relevance. In Proceedings of Theoretical Aspects of Computer Science (TACS’94), Lecture Notes in Computer Science, pages 707–725. Springer Verlag. Dershowitz, N. and Jouannaud, J.-P. (1990). Rewrite Systems. In van Leeuwen, J., editor, Handbook of Theoretical Computer Science, volume B: Formal Models and Semantics, chapter 6, pages 243–320. North-Holland, Amsterdam. Fern´ andez, M. and Mackie, I. (1999). Closed Reduction in the λ-calculus. In Flum, J. and Rodr´ıguez-Artalejo, M., editors, Proceedings of Computer Science Logic (CSL’99), volume 1683 of Lecture Notes in Computer Science, pages 220–234. Springer-Verlag. Fern´ andez, M., Mackie, I., and Sinot, F.-R. (2005). Closed Reduction: explicit substitutions without alpha conversion. Mathematical Structures in Computer Science, 15(2):343–381. Florido, M. and Damas, L. (2004). Linearisation of the lambda-calculus and its relation with intersection type systems. Journal of Functional Programming, 14(5):519–546. Girard, J. (1998). Light linear logic. Information and Computation, 143(2):175–204. Girard, J.-Y. (1987). Linear Logic. Theoretical Computer Science, 50(1):1–102. Girard, J.-Y. (1989). Towards a Geometry of Interaction. In Gray, J. W. and Scedrov, A., editors, Categories in Computer Science and Logic: Proceedings of the Joint Summer Research Conference, volume 92 of Contemporary Mathematics, pages 69–108. American Mathematical Society. Girard, J.-Y., Lafont, Y., and Taylor, P. (1989). Proofs and Types, volume 7 of Cambridge Tracts in Theoretical Computer Science. Cambridge University Press. Girard, J.-Y., Scedrov, A., and Scott, P. J. (1992). Bounded Linear Logic: A Modular Approach to Polynomial Time Computability. Theoretical Computer Science, 97:1–66. ¨ G¨ odel, K. (1958). Uber eine bisher noch nicht ben¨ utzte Erweiterung des finiten. Dialectica, 12:280–287. Hindley, J. R. (1969). The Principal Type-scheme of an Object in Combinatory Logic. Transactions of the American Mathematical Society, 146:29–60. Hindley, J. R. (1989). BCK-Combinators and linear lambda-terms have types. Theoretical Computer Science, 64(1):97–105. Hindley, J. R. (1997). Basic Simple Type Theory, volume 42 of Cambridge Tracts in Theoretical Computer Science. Cambridge University Press.

REFERENCES

195

Hindley, J. R. and Seldin, J. P. (1986). Introduction to combinators and λ-calculus. Cambridge University Press, New York, NY, USA. Hofmann, M. (1999). Linear types and non-size-increasing polynomial time computation. In Proceedings of Logic in Computer Science (LICS’99), pages 464–473. IEEE Computer Society Press. Holmstr¨ om, S. (1988). Linear Functional Programming. In Johnsson, T., Peyton Jones, S. L., and Karlsson, K., editors, Proceedings of the Workshop on Implementation of Lazy Functional Languages, pages 13–32. Jim, T. (1995). Rank 2 type systems and recursive definitions. Massachusetts Institute of Technology.

Technical report,

Jim, T. (1996). What are principal typings and are they good for? In Proceedings of the Symposium Principles of Programming Languages (POPL’96), pages 42–53. ACM Press. Kfoury and Wells (1995). New Notions of Reduction and Non-Semantic Proofs of Strong beta-Normalisation in Typed lambda-Calculi. In Proceedings of Logic in Computer Science (LICS’95), pages 311–321. IEEE Computer Society Press. Kfoury, A. J. (2000). A linearisation of the Lambda-Calculus and consequences. Journal of Logic and Computation, 10(3):411–436. Special issue on Type Theory and Term Rewriting. Kamareddine and Klop (editors). Kfoury, A. J., Mairson, H. G., Turbak, F. A., and Wells, J. B. (1999). Relating Typability and Expressiveness in Finite-Rank Intersection Type Systems (Extended Abstract). In Proceedings of the International Conference on Functional Programming (ICFP’99), pages 90–101. ACM Press. Kfoury, A. J. and Wells, J. B. (1994). A Direct Algorithm for Type Inference in the Rank-2 Fragment of the Second-Order lambda-Calculus. In Proceedings of LISP and Functional Programming, pages 196–207. ACM Press. Kfoury, A. J. and Wells, J. B. (1999). Principality and Decidable Type Inference for Finite-Rank Intersection Types. In Proceedings of Principles of Programming Languages (POPL’99), pages 161–174. ACM Press. Kleene, S. C. and Rosser, J. B. (1935). The inconsistency of certain formal logics. Annals of Mathematics, 36(3):630–636. Klop, J. W. (1992). Term Rewriting Systems. In Abramsky, S., Gabbay, D. M., and Maibaum, T. S. E., editors, Handbook of Logic in Computer Science: Background Computational Structures (Volume 2), pages 1–116. Clarendon Press, Oxford.

196

REFERENCES

Klop, J.-W., van Oostrom, V., and van Raamsdonk, F. (1993). Combinatory Reduction Systems, introduction and survey. Theoretical Computer Science, 121:279–308. Lafont, Y. (2004). Soft linear logic and polynomial time. Theoretical Computer Science, 318(1-2):163–180. Lago, U. D. (2005). The Geometry of Linear Higher-Order Recursion. In Panangaden, P., editor, Proceedings of Logic in Computer Science, (LICS’05), pages 366–375. IEEE Computer Society Press. Lamping, J. (1990). An algorithm for optimal lambda calculus reduction. In Proceedings of Principles of Programming Languages (POPL’90), pages 16–30. ACM Press. Leivant, D. (1983). Polymorphic type inference. In Proceedings of Principles of Programming Languages (POPL’83), pages 88–98, New York, NY, USA. ACM Press. L´evy, J. (1978). R´eductions correctes et optimales dans le lambda calcul. PhD thesis, Universit´e Paris VII. L´evy, J. (1980). Optimal reductions in the lambda-calculus. In Seldin, J. P. and Hindley, J. R., editors, To H. B. Curry: Essays on Combinatory Logic, Lambda-Calculus, and Formalism, pages 159–191. Academic Press, Inc., New York, NY. Lincoln, P. and Mitchell, J. (1992). Operational aspects of linear lambda calculus. In Proceedings of Logic in Computer Science (LICS’92), pages 235–246. IEEE Computer Society Press. Mackie, I. (1994). Lilac: A Functional Programming Language Based on Linear Logic. Journal of Functional Programming, 4(4):395–433. Mackie, I. (2000). Interaction nets for linear logic. Theoretical Computer Science, 247(12):83–140. Maraist, J., Odersky, M., Turner, D. N., and Wadler, P. (1995). Call-by-name, call-by-value, call-by-need, and the linear lambda calculus. In Proceedings of Mathematical Foundations of Programming Semantics (MFPS’95), volume 1 of Electronic Notes in Theoretical Computer Science. Mellish, C. (1982). An Alternative to Structure Sharing in the Implementation of a PROLOG Interpreter. In Clark, K. and T¨arnlund, S.-A., editors, Logic Programming, pages 99–106. Academic Press. Milner, R. (1978). A Theory of Type Polymorphism in Programming. Journal of Computer and System Sciences, 17(3):348–375.

REFERENCES

197

Newman, M. (1942). On theories with a combinatorial definition of “equivalence”. Annals of Mathematics, 43(2):223–243. Phillips, I. (1992). Recursion theory. In Abramsky, S., Gabbay, D., and Maibaum, T., editors, Handbook of Logic in Computer Science, volume 1, pages 79–187. Oxford University Press. Plotkin, G. D. (1977). LCF Considered as a Programming Language. Theoretical Computer Science, 5:223–255. Pottinger, G. (1980). A type assignement for the strongly normalisable terms. In Hindley, J. and Seldin, J., editors, To H.B. Curry, Essays in Combinatory Logic, Lambda-calculus and Formalism, pages 561–577. Academic Press, Inc. Robinson, J. A. (1965). A machine-oriented logic based on the resolution principle. Journal of the Association for Computing Machinery (ACM), 12:23–41. Tait, W. W. (1967). Intensional Interpretation of Functionals of Finite Type I. Journal of Symbolic Logic, 32(2):198–212. Terui, K. (2001). Light Affine Lambda-calculus and polytime strong normalisation. In Proceedings of Logic in Computer Science (LICS’01), pages 209–220. IEEE Computer Society Press. Turing, A. M. (1936 and 1937b). On Computable Numbers, with an Application to the Entscheidungsproblem. Proceedings of the London Mathematical Society, 42(2):230–265. Turing, A. M. (1937a). Computability and λ-definability. Journal of Symbolic Logic, 2(4):153–163. van Bakel, S. (1993). Intersection Type Disciplines in Lambda Calculus and Applicative Term Rewriting Systems. PhD thesis, Department of Computer Science, University of Nijmegen. van Bakel, S. (1996). Rank 2 Intersection Type Assignment in Term Rewriting Systems. Fundamenta Infomaticae, 2(26):141–166. van Raamsdonk, F., Severi, P., Sorensen, M. H. B., and Xi, H. (1999). Perpetual reductions in λ-calculus. Information and Computation, 149(2):173–225. Wadler, P. (1990). Linear types can change the world! In Broy, M. and Jones, C., editors, Proceedings of IFIP TC 2 Working Conference on Programming Concepts and Methods, pages 347–359. North Holland, Amsterdam. Wand, M. (1987). A Simple Algorithm and Proof for Type Inference. Infomaticae, 10:115–121.

Fundamenta

Suggest Documents