Reduction Types and Intensionality in the Lambda-Calculus
Total Page:16
File Type:pdf, Size:1020Kb
Reduction Types and Intensionality in the Lambda-Calculus by David Amson Wright, BSc(Hons) MSc (Rhodes) Submitted in fulfilment of the requirements for the Degree of Doctor of Philosophy UNIVERSITY OF TASMANIA September 1992 Declaration This thesis contains no material which has been accepted for the award of any other higher degree or graduate diploma in any tertiary institution. To the best of my knowledge and belief, this thesis contains no material previously published or written by another person, except where due reference has been made in the text of the thesis. David A. Wright ii To Merridy 111 Abstract In this thesis I introduce a new approach to the automated analysis of the reduction behaviour of A-calculus terms. This new approach improves on earlier analysers in several ways, not least in its treatment of higher-order terms and polymorphism, two notably troublesome issues. In addition, this thesis introduces a stronger notion of reduction behaviour than strictness. This concept, called strong head neededness, forms the basis for a new notation for describing the reduction behaviour of terms. This notation is a kind of type, elements of which are built using a Boolean algebra of function type constructors. Thus the form of the methodology proposed is that of a type system. Consideration is given to a variety of type assignment systems for the new type system. This supports the hypothesis that the approach proposed is suit- able as a framework for building a range of analyses. Having established this framework it is then a matter of engineering to determine the appropriate trade off between information derived and performance achieved. An investigation is conducted into the formal semantics of all the constructs introduced. In particular, the investigation proves a range of soundness and completeness results. Also examined is the semantics of the new notion of type and the development of a model for reduction types. The model is of interest in its own right, as it gives further insight into the reduction behaviour of A-terms. The thesis includes detailed implementations of all the type assignment systems and ascertains the correctness of these implementations. iv Acknowledgements I would like to thank my supervisor, Clem Baker-Finch, for listening patiently to many partially developed ideas, reading many drafts and lots of discussion about this work. Thank you Clem for all your encouragement. I thank Geoffrey Burn for always being prepared to give of his especially valuable time. Geoffrey has read many drafts of my work and we have had many discussions about the ideas as described in this thesis. In lots of ways Geoffrey was like a supervisor, even though we were separated by many thousands of miles. Phil Collier has been a source of much wisdom, especially during some of the darker days. Thank you Phil for stepping in as supervisor when Clem left to take up his new post. Andrew Partridge has been, and is, an excellent colleague. Andrew, I have enjoyed discussing with you, and learnt much about, dynamic and static tech- niques for detecting parallelism. Tony Dekker and I had many useful and enlightening conversations about all manner of things to do with functional programming, types and theory in the earlier days of this project. Also much involved at this time was Ed Kazmierczak whose enthusiasm for the "great works" was an inspiration. I would like to thank Arthur Sale for providing time to allow me to finish and for good advice. Thanks to Bernard Gunther for opposing functional programming—for a while! Without a doubt, I owe the greatest thanks to Merridy for her never ending support and love. I also thank my Mom and Dad for their constant faith in me and their encouragement, even when separated from me by a great distance. Contents Abstract iv Acknowledgements 1 Introduction 1 1.1 Functional Languages and Static Analysis 1 1.2 A-Calculus 3 1.2.1 A-terms 3 1.2.2 The Theory A 5 1.2.3 Head Reduction 6 1.2.4 Labelled Reduction and Descendants 6 1.2.5 Some Common Combinators 7 1.3 Curry Types and Type Deduction 8 1.3.1 The Curry Type System 8 1.4 A Guide to Reading this Thesis 9 1.4.1 Plan of this Thesis 10 2 Strong Head Neededness and Irrelevance 13 2.1 Strong Head Neededness 14 2.2 Variations 15 2.3 Irrelevance 16 2.4 Properties 17 2.5 Discussion 20 3 Reduction Types and Type Assignment 22 3.1 Introduction to Reduction Types 22 3.1.1 Boolean Algebras 25 3.2 Curry-style Type Assignment 26 3.2.1 Preliminaries 26 3.2.2 The Type Assignment Rules 28 3.2.3 Examples 30 3.2.4 An Extension of the Curry-style System 35 3.2.5 Multiple Occurrences of Term Variables 37 vi 3.2.6 Properties of the Curry-style System 39 3.2.7 Syntactic Variations 47 3.2.8 Discussion 48 3.3 LET-Polymorphic Type Assignment 49 3.3.1 Generic Types 49 3.3.2 The Type Assignment Rules 50 3.3.3 Examples 53 3.3.4 Properties of the LET-Polymorphic System 53 3.3.5 Discussion 61 3.4 Intersection-style Type Assignment 61 3.4.1 Intersection Boolean Reduction Types 63 3.4.2 The Type Assignment Rules 65 3.4.3 Extending Intersection-style Inference 67 3.4.4 Examples 69 3.4.5 Properties of the Intersection-style System 70 3.4.6 Discussion 79 4 The Semantics of Reduction Types 80 4.1 Models of Terms 81 4.1.1 A Model of the A-calculus 81 4.1.2 A Semi-Model of the A-calculus 83 4.2 Models of Boolean Reduction Types 88 4.2.1 What is a Type Interpretation? 89 4.2.2 The Semantics of Simple Reduction Types 90 4.2.3 The Semantics of LET-Polymorphic Reduction Types 92 4.2.4 The Semantics of Intersection Reduction Types 92 4.3 The Intensional Applicative Behaviour of A-terms 93 4.4 Type Assignment is Sound 97 4.4.1 Soundness of 1- c 97 4.4.2 Soundness of EL 98 4.4.3 Soundness of 1- / 98 4.5 Type Assignment is Complete 98 4.5.1 Completeness of I- c 99 4.5.2 Completeness of EL. 100 4.5.3 Completeness of 1-1 101 4.6 Discussion 102 5 Implementation 104 5.1 Preliminaries 105 5.1.1 Substitutions 105 5.1.2 Triples 106 5.1.3 Unification of Boolean Rings 106 vii 5.2 Implementation of the Curry-style System 108 5.2.1 Preliminaries 109 5.2.2 Unification of Types 109 5.2.3 Type Inference 112 5.2.4 Decidability 115 5.3 Implementation of the LET-Polymorphic System 115 5.3.1 Substitution 115 5.3.2 Unification of Types 116 5.3.3 Type Inference 117 5.3.4 Decidability 121 5.4 Implementation of the Intersection-style System 121 5.4.1 Substitution 121 5.4.2 Normal Forms 122 5.4.3 Expansion of a Deduction 122 5.4.4 Principal Triples 129 5.4.5 Unification of Intersection Types 130 5.4.6 The Type Inference Algorithm 133 5.4.7 Decidability 136 6 Extensions and Future Work 137 6.1 Constants 137 6.1.1 Term Constants 138 6.1.2 Type Constants 138 6.1.3 Type Assignment 138 6.1.4 An Example: The Conditional 139 6.2 Adding a Fixpoint Constant 140 6.3 Data Structures 143 6.3.1 A Detailed Example 145 6.4 Second-Order Polymorphism 147 6.5 Algebraic Reduction Types 149 6.5.1 Constants 152 6.5.2 Fixpoints and Algebraic Reduction Types 153 6.5.3 Data Structures 153 6.5.4 Implementation 155 6.6 Non-termination 156 6.7 A Two Step Strong Head Neededness Algorithm 158 7 Conclusion 160 7.1 Related Work 161 7.1.1 Analyses based on Types 161 7.1.2 Abstract Interpretations not based on Types 163 7.1.3 Other Techniques 165 viii 7.2 Summary 166 7.2.1 Strong Head Neededness 166 7.2.2 Boolean Reduction Types and Type Deduction 166 7.2.3 Intensional Semantics for Reduction Types 167 7.2.4 Implementation of Reduction Type Inference 168 7.2.5 Extensions 168 A An Implementation in Orwell 170 A.1 Orwell 170 A.2 Preliminaries 171 A.2.1 Auxiliary functions 171 A.2.2 Parsing 173 A.2.3 Printing 177 A.2.4 Association Lists 179 A.3 An Implementation of Boolean Algebra 181 A.3.1 Parsing and Printing 182 A.3.2 Sum-of-Products Normal Form 183 A.3.3 Unification of Boolean Rings 184 A.4 Lambda Terms 186 A.4.1 Parsing A-terms 186 A.4.2 Printing A-terms 187 A.5 Intersection Reduction Types 187 A.5.1 Parsing Reduction Types 188 A.5.2 Simplification of Reduction Types 189 A.5.3 Printing Reduction Types 190 A.5.4 Substitution for types 191 A.5.5 State-based functions 191 A.5.6 Expansion 193 A.6 Unification 195 A.6.1 Guaranteeing Termination 196 A.7 Constants 197 A.8 Type Inference 198 A.9 The Top-level of the Type Inference System 199 A.10 Examples 200 ix List of Figures 1 The Theory A 5 2 Curry's system for Type Deduction 9 3 A Diagram of the Structure of the Thesis 11 4 The Axioms of a Boolean Algebra 25 5 The Curry-style Rules for deducing Reduction Types 29 6 Extended Curry-style Rules for deducing Reduction Types .