Type Theory & Functional Programming
Total Page:16
File Type:pdf, Size:1020Kb
Type Theory & Functional Programming Simon Thompson Computing Laboratory, University of Kent March 1999 c Simon Thompson, 1999 Not to be reproduced i ii To my parents Preface Constructive Type theory has been a topic of research interest to computer scientists, mathematicians, logicians and philosophers for a number of years. For computer scientists it provides a framework which brings together logic and programming languages in a most elegant and fertile way: program development and verification can proceed within a single system. Viewed in a different way, type theory is a functional programming language with some novel features, such as the totality of all its functions, its expressive type system allowing functions whose result type depends upon the value of its input, and sophisticated modules and abstract types whose interfaces can contain logical assertions as well as signature information. A third point of view emphasizes that programs (or functions) can be extracted from proofs in the logic. Up until now most of the material on type theory has only appeared in proceedings of conferences and in research papers, so it seems appropriate to try to set down the current state of development in a form accessible to interested final-year undergraduates, graduate students, research workers and teachers in computer science and related fields – hence this book. The book can be thought of as giving both a first and a second course in type theory. We begin with introductory material on logic and functional programming, and follow this by presenting the system of type theory itself, together with many examples. As well as this we go further, looking at the system from a mathematical perspective, thus elucidating a number of its important properties. Then we take a critical look at the profusion of suggestions in the literature about why and how type theory could be augmented. In doing this we are aiming at a moving target; it must be the case that further developments will have been made before the book reaches the press. Nonetheless, such an survey can give the reader a much more developed sense of the potential of type theory, as well as giving the background of what is to come. iii iv PREFACE Outline It seems in order to give an overview of the book. Each chapter begins with a more detailed introduction, so we shall be brief here. We follow this with a guide on how the book might be approached. The first three chapters survey the three fields upon which type theory depends: logic, the λ-calculus and functional programming and construc- tive mathematics. The surveys are short, establishing terminology, notation and a general context for the discussion; pointers to the relevant literature and in particular to more detailed introductions are provided. In the second chapter we discuss some issues in the λ-calculus and functional program- ming which suggest analogous questions in type theory. The fourth chapter forms the focus of the book. We give the formal system for type theory, developing examples of both programs and proofs as we go along. These tend to be short, illustrating the construct just introduced – chapter 6 contains many more examples. The system of type theory is complex, and in chapter which follows we explore a number of different aspects of the theory. We prove certain results about it (rather than using it) including the important facts that programs are terminating and that evaluation is deterministic. Other topics examined include the variety of equality relations in the system, the addition of types (or ‘universes’) of types and some more technical points. Much of our understanding of a complex formal system must derive from out using it. Chapter six covers a variety of examples and larger case studies. From the functional programming point of view, we choose to stress the differences between the system and more traditional languages. After a lengthy discussion of recursion, we look at the impact of the quan- tified types, especially in the light of the universes added above. We also take the opportunity to demonstrate how programs can be extracted from constructive proofs, and one way that imperative programs can be seen as arising. We conclude with a survey of examples in the relevant literature. As an aside it is worth saying that for any formal system, we can really only understand its precise details after attempting to implement it. The combination of symbolic and natural language used by mathematicians is surprisingly suggestive, yet ambiguous, and it is only the discipline of having to implement a system which makes us look at some aspects of it. In the case of TT , it was only through writing an implementation in the functional programming language Miranda1 that the author came to understand the distinctive role of assumptions in TT , for instance. The system is expressive, as witnessed by the previous chapter, but are programs given in their most natural or efficient form? There is a 1Miranda is a trade mark of Research Software Limited v host of proposals of how to augment the system, and we look at these in chapter 7. Crucial to them is the incorporation of a class of subset types, in which the witnessing information contained in a type like (∃x:A) .B(x) is suppressed. As well as describing the subset type, we lay out the arguments for its addition to type theory, and conclude that it is not as necessary as has been thought. Other proposals include quotient (or equivalence class) types, and ways in which general recursion can be added to the system without its losing its properties like termination. A particularly elegant proposal for the addition of co-inductive types, such as infinite streams, without losing these properties, is examined. Chapter eight examines the foundations of the system: how it compares with other systems for constructive mathematics, how models of it are formed and used and how certain of the rules, the closure rules, may be seen as being generated from the introduction rules, which state what are the canonical members of each type. We end the book with a survey of related systems, implemented or not, and some concluding remarks. Bibliographic information is collected at the end of the book, together with a table of the rules of the various systems. We have used standard terminology whenever were able, but when a subject is of current research interest this is not always possible. Using the book In the hope of making this a self-contained introduction, we have included chapters one and two, which discuss natural deduction logic and the λ- calculus – these chapters survey the fields and provide an introduction to the notation and terminology we shall use later. The core of the text is chapter four, which is the introduction to type theory. Readers who are familiar with natural deduction logic and the λ-calculus could begin with the brief introduction to constructive mathematics pro- vided by chapter three, and then turn to chapter four. This is the core of the book, where we lay out type theory as both a logic and an functional programming system, giving small examples as we go. The chapters which follow are more or less loosely coupled. Someone keen to see applications of type theory can turn to chapter six, which contains examples and larger case studies; only occasionally will readers need to need to refer back to topics in chapter five. Another option on concluding chapter four is to move straight on to chapter five, where the system is examined from various mathematical per- spectives, and an number of important results on the consistency, express- ibility and determinacy are proved. Chapter eight should be seen as a continuation of this, as it explores topics of a foundational nature. vi PREFACE Chapter seven is perhaps best read after the examples of chapter six, and digesting the deliberations of chapter five. In each chapter exercises are included. These range from the routine to the challenging. Not many programming projects are included as it is expected that readers will to be able to think of suitable projects for themselves – the world is full of potential applications, after all. Acknowledgements The genesis of this book was a set of notes prepared for a lecture series on type theory given to the Theoretical Computer Science seminar at the Uni- versity of Kent, and subsequently at the Federal University of Pernambuco, Recife, Brazil. Thanks are due to colleagues from both institutions; I am especially grateful to David Turner and Allan Grimley for both encourage- ment and stimulating discussions on the topic of type theory. I should also thank colleagues at UFPE, and the Brazilian National Research Council, CNPq, for making my visit to Brazil possible. In its various forms the text has received detailed commment and criti- cism from a number of people, including Martin Henson, John Hughes, Nic McPhee, Jerry Mead and various anonymous reviewers. Thanks to them the manuscript has been much improved, though needless to say, I alone will accept responsibility for any infelicities or errors which remain. The text itself was prepared using the LaTeX document preparation system; in this respect Tim Hopkins and Ian Utting have put up with nu- merous queries of varying complexity with unfailing good humour – thanks to both of them. Duncan Langford and Richard Jones have given me much appreciated advice on using the Macintosh. The editorial and production staff at Addison-Wesley have been most helpful; in particular Simon Plumtree has given me exactly the right mix- ture of assistance and direction. The most important acknowledgements are to Jane and Alice: Jane has supported me through all stages of the book, giving me encouragement when it was needed and coping so well with having to share me with this enterprise over the last year; without her I am sure the book would not have been completed.