1. Trang chủ
  2. » Tất cả

Type Theory and Functional Programming

378 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

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 constructive 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 programming 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 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 quantified 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 T T , 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 T T , 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 Miranda 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 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 provided 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 perspectives, and an number of important results on the consistency, expressibility 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 University 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 encouragement 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 criticism 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 numerous 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 mixture 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 Alice is a joy, and makes me realise how much more there is to life than type theory Contents Preface iii Introduction 1 Introduction to Logic 1.1 Propositional Logic 1.2 Predicate Logic 1.2.1 Variables and substitution 1.2.2 Quantifier rules 1.2.3 Examples 16 18 21 24 Functional Programming and λ-Calculi 2.1 Functional Programming 2.2 The untyped λ-calculus 2.3 Evaluation 2.4 Convertibility 2.5 Expressiveness 2.6 Typed λ-calculus 2.7 Strong normalisation 2.8 Further type constructors: the product 2.9 Base Types: Natural Numbers 2.10 General Recursion 2.11 Evaluation revisited 29 30 32 36 40 41 42 45 50 53 55 56 Constructive Mathematics 59 Introduction to Type Theory 4.1 Propositional Logic: an Informal View 4.2 Judgements, Proofs and Derivations 4.3 The Rules for Propositional Calculus 67 69 71 73 vii viii CONTENTS 4.4 4.5 The Curry Howard Isomorphism Some examples 4.5.1 The identity function; A implies itself 4.5.2 The transitivity of implication; function composition 4.5.3 Different proofs 4.5.4 and different derivations 4.5.5 Conjunction and disjunction 4.6 Quantifiers 4.6.1 Some example proofs 4.7 Base Types 4.7.1 Booleans 4.7.2 Finite types 4.7.3 > and ⊥ 4.8 The natural numbers 4.9 Well-founded types — trees 4.10 Equality 4.10.1 Equality over base types 4.10.2 Inequalities 4.10.3 Dependent Types 4.10.4 Equality over the I-types 4.11 Convertibility 4.11.1 Definitions; convertibility and equality 4.11.2 An example – Adding one 4.11.3 An example – natural number equality Exploring Type Theory 5.1 Assumptions 5.2 Naming and abbreviations 5.2.1 Naming 5.2.2 Abbreviations 5.3 Revising the rules 5.3.1 Variable binding operators and disjunction 5.3.2 Generalising ∨ 5.3.3 The Existential Quantifier 5.4 Derivability 5.4.1 A is a type is derivable from a : A 5.4.2 Unique types 5.5 Computation 5.5.1 Reduction 5.5.2 The system T T0∗ 5.5.3 Combinators and the system T T0c 5.6 T T0c : Normalisation and its corollaries 78 83 83 83 84 85 86 88 92 96 96 98 99 100 105 109 113 114 114 116 117 117 119 121 125 126 130 131 132 133 133 135 136 139 140 142 144 144 146 148 153 CONTENTS ix 5.6.1 Polymorphism and Monomorphism Equalities and Identities 5.7.1 Definitional equality 5.7.2 Convertibility 5.7.3 Identity; the I type 5.7.4 Equality functions 5.7.5 Characterising equality 5.8 Different Equalities 5.8.1 A functional programming perspective 5.8.2 Extensional Equality 5.8.3 Defining Extensional Equality in T T0 5.9 Universes 5.9.1 Type families 5.9.2 Quantifying over universes 5.9.3 Closure axioms 5.9.4 Extensions 5.10 Well-founded types 5.10.1 Lists 5.10.2 The general case - the W type 5.10.3 Algebraic types in Miranda 5.11 Expressibility 5.12 The Curry Howard Isomorphism? 5.12.1 Assumptions 5.12.2 Normal Forms of Proofs 5.7 Applying Type Theory 6.1 Recursion 6.1.1 Numerical functions 6.1.2 Defining propositions and types by 6.1.3 Recursion over lists – 6.1.4 Recursion over lists – 6.2 A Case Study – Quicksort 6.2.1 Defining the function 6.2.2 Verifying the function 6.3 Dependent types and quantifiers 6.3.1 Dependent Types 6.3.2 The Existential Quantifier 6.3.3 The Universal Quantifier 6.3.4 Implementing a logic 6.3.5 Quantification and Universes – ∀ 6.3.6 Quantification and Universes – ∃ 6.4 A Case Study – Vectors 162 163 163 164 165 165 167 168 168 169 171 174 176 177 178 179 179 179 181 187 189 191 191 192 recursion 195 196 197 200 202 205 207 207 209 214 214 216 217 217 220 223 226 ... 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. .. and programming, then we can define a system which is simultaneously a logic and a programming language, and in which propositions and types are identical This is the system of constructive type. .. 360 xii CONTENTS Introduction Types are types and propositions are propositions; types come from programming languages, and propositions from logic, and they seem to have no relation to

Ngày đăng: 17/04/2017, 10:59

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN