annotate src/Intro.v @ 305:690796f4690d

Further emphasize necessity and purpose of Set Implicit Arguments; tweak Makefile to support parallel builds
author Adam Chlipala <adam@chlipala.net>
date Wed, 03 Aug 2011 10:34:53 -0400
parents 25ec92be5ad2
children a419a60e5ff6
rev   line source
adam@300 1 (* Copyright (c) 2008-2011, Adam Chlipala
adamc@4 2 *
adamc@4 3 * This work is licensed under a
adamc@4 4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
adamc@4 5 * Unported License.
adamc@4 6 * The license text is available at:
adamc@4 7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
adamc@4 8 *)
adamc@4 9
adamc@21 10 (** %\fi
adamc@21 11
adamc@269 12 \usepackage{hyperref}
adamc@269 13
adamc@21 14 \begin{document}
adamc@21 15
adamc@21 16 \maketitle
adamc@21 17
adamc@21 18 \thispagestyle{empty}
adamc@21 19 \mbox{}\vfill
adamc@21 20 \begin{center}% *)
adamc@21 21
adamc@4 22 (**
adamc@4 23
adam@300 24 Copyright Adam Chlipala 2008-2011.
adamc@21 25
adamc@4 26 This work is licensed under a
adamc@4 27 Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
adamc@4 28 Unported License.
adamc@4 29 The license text is available at:
adamc@4 30 %\begin{center} \url{http://creativecommons.org/licenses/by-nc-nd/3.0/} \end{center}%
adamc@4 31 #<a href="http://creativecommons.org/licenses/by-nc-nd/3.0/">http://creativecommons.org/licenses/by-nc-nd/3.0/</a>#
adamc@4 32
adamc@4 33 *)
adamc@4 34
adamc@21 35 (** %\vfill\mbox{}
adamc@21 36 \end{center}
adamc@21 37
adamc@269 38 \phantomsection
adamc@21 39 \tableofcontents
adamc@21 40
adamc@21 41 \chapter{Introduction}% *)
adamc@21 42
adamc@21 43
adamc@21 44
adamc@4 45
adamc@4 46 (** * Whence This Book? *)
adamc@4 47
adamc@4 48 (**
adamc@4 49
adamc@13 50 We would all like to have programs check that our programs are correct. Due in no small part to some bold but unfulfilled promises in the history of computer science, today most people who write software, practitioners and academics alike, assume that the costs of formal program verification outweigh the benefits. The purpose of this book is to convince you that the technology of program verification is mature enough today that it makes sense to use it in a support role in many kinds of research projects in computer science. Beyond the convincing, I also want to provide a handbook on practical engineering of certified programs with the Coq proof assistant.
adamc@4 51
adam@292 52 There are a good number of (though definitely not %``%#"#many#"#%''%) tools that are in wide use today for building machine-checked mathematical proofs and machine-certified programs. This is my attempt at an exhaustive list of interactive %``%#"#proof assistants#"#%''% satisfying a few criteria. First, the authors of each tool must intend for it to be put to use for software-related applications. Second, there must have been enough engineering effort put into the tool that someone not doing research on the tool itself would feel his time was well spent using it. A third criterion is more of an empirical validation of the second: the tool must have a significant user community outside of its own development team.
adamc@4 53
adamc@4 54 %
adamc@244 55 \medskip
adamc@244 56
adamc@4 57 \begin{tabular}{rl}
adamc@4 58 \textbf{ACL2} & \url{http://www.cs.utexas.edu/users/moore/acl2/} \\
adamc@4 59 \textbf{Coq} & \url{http://coq.inria.fr/} \\
adamc@4 60 \textbf{Isabelle/HOL} & \url{http://isabelle.in.tum.de/} \\
adamc@4 61 \textbf{PVS} & \url{http://pvs.csl.sri.com/} \\
adamc@4 62 \textbf{Twelf} & \url{http://www.twelf.org/} \\
adamc@4 63 \end{tabular}
adamc@244 64
adamc@244 65 \medskip
adamc@4 66 %
adamc@4 67 #
adamc@4 68 <table align="center">
adamc@4 69 <tr><td align="right"><b>ACL2</b></td> <td><a href="http://www.cs.utexas.edu/users/moore/acl2/">http://www.cs.utexas.edu/users/moore/acl2/</a></td></tr>
adamc@4 70 <tr><td align="right"><b>Coq</b></td> <td><a href="http://coq.inria.fr/">http://coq.inria.fr/</a></td></tr>
adamc@4 71 <tr><td align="right"><b>Isabelle/HOL</b></td> <td><a href="http://isabelle.in.tum.de/">http://isabelle.in.tum.de/</a></td></tr>
adamc@4 72 <tr><td align="right"><b>PVS</b></td> <td><a href="http://pvs.csl.sri.com/">http://pvs.csl.sri.com/</a></td></tr>
adamc@4 73 <tr><td align="right"><b>Twelf</b></td> <td><a href="http://www.twelf.org/">http://www.twelf.org/</a></td></tr>
adamc@4 74 </table>
adamc@4 75 #
adamc@4 76
adam@292 77 Isabelle/HOL, implemented with the %``%#"#proof assistant development framework#"#%''% Isabelle, is the most popular proof assistant for the HOL logic. The other implementations of HOL can be considered equivalent for purposes of the discussion here.
adamc@4 78
adamc@4 79 *)
adamc@4 80
adamc@4 81
adamc@4 82 (** * Why Coq? *)
adamc@4 83
adamc@4 84 (**
adamc@4 85 This book is going to be about certified programming using Coq, and I am convinced that it is the best tool for the job. Coq has a number of very attractive properties, which I will summarize here, mentioning which of the other candidate tools lack each property.
adamc@4 86 *)
adamc@4 87
adamc@4 88
adamc@4 89 (** ** Based on a Higher-Order Functional Programming Language *)
adamc@4 90
adamc@4 91 (**
adamc@4 92 There is no reason to give up the familiar comforts of functional programming when you start writing certified programs. All of the tools I listed are based on functional programming languages, which means you can use them without their proof-related aspects to write and run regular programs.
adamc@4 93
adamc@13 94 ACL2 is notable in this field for having only a %\textit{%#<i>#first-order#</i>#%}% language at its foundation. That is, you cannot work with functions over functions and all those other treats of functional programming. By giving up this facility, ACL2 can make broader assumptions about how well its proof automation will work, but we can generally recover the same advantages in other proof assistants when we happen to be programming in first-order fragments.
adamc@4 95 *)
adamc@4 96
adamc@4 97
adamc@4 98 (** ** Dependent Types *)
adamc@4 99
adamc@4 100 (**
adamc@210 101 A language of %\textit{%#<i>#dependent types#</i>#%}% may include references to programs inside of types. For instance, the type of an array might include a program expression giving the size of the array, making it possible to verify absence of out-of-bounds accesses statically. Dependent types can go even further than this, effectively capturing any correctness property in a type. For instance, later in this book, we will see how to give a Mini-ML compiler a type that guarantees that it maps well-typed source programs to well-typed target programs.
adamc@4 102
adamc@4 103 ACL2 and HOL lack dependent types outright. PVS and Twelf each supports a different strict subset of Coq's dependent type language. Twelf's type language is restricted to a bare-bones, monomorphic lambda calculus, which places serious restrictions on how complicated %\textit{%#<i>#computations inside types#</i>#%}% can be. This restriction is important for the soundness argument behind Twelf's approach to representing and checking proofs.
adamc@4 104
adamc@4 105 In contrast, PVS's dependent types are much more general, but they are squeezed inside the single mechanism of %\textit{%#<i>#subset types#</i>#%}%, where a normal type is refined by attaching a predicate over its elements. Each member of the subset type is an element of the base type that satisfies the predicate.
adamc@4 106
adamc@4 107 Dependent types are not just useful because they help you express correctness properties in types. Dependent types also often let you write certified programs %\textit{%#<i>#without writing anything that looks like a proof#</i>#%}%. Even with subset types, which for many contexts can be used to express any relevant property with enough acrobatics, the human driving the proof assistant usually has to build some proofs explicitly. Writing formal proofs is hard, so we want to avoid it as far as possible, so dependent types are invaluable.
adamc@4 108
adamc@4 109 *)
adamc@4 110
adamc@4 111 (** ** An Easy-to-Check Kernel Proof Language *)
adamc@4 112
adamc@4 113 (**
adam@292 114 Scores of automated decision procedures are useful in practical theorem proving, but it is unfortunate to have to trust in the correct implementation of each procedure. Proof assistants satisfying the %``%#"#de Bruijn criterion#"#%''% may use complicated and extensible procedures to seek out proofs, but in the end they produce %\textit{%#<i>#proof terms#</i>#%}% in kernel languages. These core languages have feature complexity on par with what you find in proposals for formal foundations for mathematics. To believe a proof, we can ignore the possibility of bugs during %\textit{%#<i>#search#</i>#%}% and just rely on a (relatively small) proof-checking kernel that we apply to the %\textit{%#<i>#result#</i>#%}% of the search.
adamc@4 115
adam@292 116 ACL2 and PVS do not meet the de Bruijn criterion, employing fancy decision procedures that produce no %``%#"#evidence trails#"#%''% justifying their results.
adamc@4 117 *)
adamc@4 118
adamc@4 119 (** ** Convenient Programmable Proof Automation *)
adamc@4 120
adamc@4 121 (**
adamc@4 122 A commitment to a kernel proof language opens up wide possibilities for user extension of proof automation systems, without allowing user mistakes to trick the overall system into accepting invalid proofs. Almost any interesting verification problem is undecidable, so it is important to help users build their own procedures for solving the restricted problems that they encounter in particular implementations.
adamc@4 123
adamc@7 124 Twelf features no proof automation marked as a bonafide part of the latest release; there is some automation code included for testing purposes. The Twelf style is based on writing out all proofs in full detail. Because Twelf is specialized to the domain of syntactic metatheory proofs about programming languages and logics, it is feasible to use it to write those kinds of proofs manually. Outside that domain, the lack of automation can be a serious obstacle to productivity. Most kinds of program verification fall outside Twelf's forte.
adamc@4 125
adamc@4 126 Of the remaining tools, all can support user extension with new decision procedures by hacking directly in the tool's implementation language (such as OCaml for Coq). Since ACL2 and PVS do not satisfy the de Bruijn criterion, overall correctness is at the mercy of the authors of new procedures.
adamc@4 127
adam@292 128 Isabelle/HOL and Coq both support coding new proof manipulations in ML in ways that cannot lead to the acceptance of invalid proofs. Additionally, Coq includes a domain-specific language for coding decision procedures in normal Coq source code, with no need to break out into ML. This language is called Ltac, and I think of it as the unsung hero of the proof assistant world. Not only does Ltac prevent you from making fatal mistakes, it also includes a number of novel programming constructs which combine to make a %``%#"#proof by decision procedure#"#%''% style very pleasant. We will meet these features in the chapters to come.
adamc@4 129 *)
adamc@4 130
adamc@4 131 (** ** Proof by Reflection *)
adamc@4 132
adamc@4 133 (**
adam@292 134 A surprising wealth of benefits follow from choosing a proof language that integrates a rich notion of computation. Coq includes programs and proof terms in the same syntactic class. This makes it easy to write programs that compute proofs. With rich enough dependent types, such programs are %\textit{%#<i>#certified decision procedures#</i>#%}%. In such cases, these certified procedures can be put to good use %\textit{%#<i>#without ever running them#</i>#%}%! Their types guarantee that, if we did bother to run them, we would receive proper %``%#"#ground#"#%''% proofs.
adamc@4 135
adamc@4 136 The critical ingredient for this technique, many of whose instances are referred to as %\textit{%#<i>#proof by reflection#</i>#%}%, is a way of inducing non-trivial computation inside of logical propositions during proof checking. Further, most of these instances require dependent types to make it possible to state the appropriate theorems. Of the proof assistants I listed, only Coq really provides this support.
adamc@4 137 *)
adamc@5 138
adamc@5 139
adamc@7 140 (** * Why Not a Different Dependently-Typed Language? *)
adamc@7 141
adamc@7 142 (**
adamc@7 143 The logic and programming language behind Coq belongs to a type-theory ecosystem with a good number of other thriving members. %Agda\footnote{\url{http://appserv.cs.chalmers.se/users/ulfn/wiki/agda.php}}%#<a href="http://appserv.cs.chalmers.se/users/ulfn/wiki/agda.php">Agda</a># and %Epigram\footnote{\url{http://www.e-pig.org/}}%#<a href="http://www.e-pig.org/">Epigram</a># are the most developed tools among the alternatives to Coq, and there are others that are earlier in their lifecycles. All of the languages in this family feel sort of like different historical offshoots of Latin. The hardest conceptual epiphanies are, for the most part, portable among all the languages. Given this, why choose Coq for certified programming?
adamc@7 144
adamc@7 145 I think the answer is simple. None of the competition has well-developed systems for tactic-based theorem proving. Agda and Epigram are designed and marketed more as programming languages than proof assistants. Dependent types are great, because they often help you prove deep theorems without doing anything that feels like proving. Nonetheless, almost any interesting certified programming project will benefit from some activity that deserves to be called proving, and many interesting projects absolutely require semi-automated proving, if the sanity of the programmer is to be safeguarded. Informally, proving is unavoidable when any correctness proof for a program has a structure that does not mirror the structure of the program itself. An example is a compiler correctness proof, which probably proceeds by induction on program execution traces, which have no simple relationship with the structure of the compiler or the structure of the programs it compiles. In building such proofs, a mature system for scripted proof automation is invaluable.
adamc@7 146
adam@292 147 On the other hand, Agda, Epigram, and similar tools have less implementation baggage associated with them, and so they tend to be the default first homes of innovations in practical type theory. Some significant kinds of dependently-typed programs are much easier to write in Agda and Epigram than in Coq. The former tools may very well be superior choices for projects that do not involve any %``%#"#proving.#"#%''% Anecdotally, I have gotten the impression that manual proving is orders of magnitudes more costly than manual coping with Coq's lack of programming bells and whistles. In this book, I will devote significant time to patterns for programming with dependent types in Coq as it is today. We can hope that the type theory community is tending towards convergence on the right set of features for practical programming with dependent types, and that we will eventually have a single tool embodying those features.
adamc@7 148 *)
adamc@7 149
adamc@7 150
adamc@5 151 (** * Engineering with a Proof Assistant *)
adamc@5 152
adamc@5 153 (**
adam@292 154 In comparisons with its competitors, Coq is often derided for promoting unreadable proofs. It is very easy to write proof scripts that manipulate proof goals imperatively, with no structure to aid readers. Such developments are nightmares to maintain, and they certainly do not manage to convey %``%#"#why the theorem is true#"#%''% to anyone but the original author. One additional (and not insignificant) purpose of this book is to show why it is unfair and unproductive to dismiss Coq based on the existence of such developments.
adamc@5 155
adamc@5 156 I will go out on a limb and guess that the reader is a dedicated fan of some functional programming language or another, and that he may even have been involved in teaching that language to undergraduates. I want to propose an analogy between two attitudes: coming to a negative conclusion about Coq after reading common Coq developments in the wild, and coming to a negative conclusion about Your Favorite Language after looking at the programs undergraduates write in it in the first week of class. The pragmatics of mechanized proving and program verification have been under serious study for much less time than the pragmatics of programming have been. The computer theorem proving community is still developing the key insights that correspond to those that functional programming texts and instructors impart to their students, to help those students get over that critical hump where using the language stops being more trouble than it is worth. Most of the insights for Coq are barely even disseminated among the experts, let alone set down in a tutorial form. I hope to use this book to go a long way towards remedying that.
adamc@5 157
adam@292 158 If I do that job well, then this book should be of interest even to people who have participated in classes or tutorials specifically about Coq. The book should even be useful to people who have been using Coq for years but who are mystified when their Coq developments prove impenetrable by colleagues. The crucial angle in this book is that there are %``%#"#design patterns#"#%''% for reliably avoiding the really grungy parts of theorem proving, and consistent use of these patterns can get you over the hump to the point where it is worth your while to use Coq to prove your theorems and certify your programs, even if formal verification is not your main concern in a project. We will follow this theme by pursuing two main methods for replacing manual proofs with more understandable artifacts: dependently-typed functions and custom Ltac decision procedures.
adamc@5 159 *)
adamc@5 160
adamc@5 161
adamc@20 162 (** * Prerequisites *)
adamc@5 163
adamc@5 164 (**
adamc@5 165 I try to keep the required background knowledge to a minimum in this book. I will assume familiarity with the material from usual discrete math and logic courses taken by all undergraduate computer science majors, and I will assume that readers have significant experience programming in one of the ML dialects, in Haskell, or in some other, closely-related language. Experience with only dynamically-typed functional languages might lead to befuddlement in some places, but a reader who has come to grok Scheme deeply will probably be fine.
adamc@5 166
adamc@206 167 A good portion of the book is about how to formalize programming languages, compilers, and proofs about them. To understand those parts, it will be helpful to have a basic knowledge of formal type systems, operational semantics, and the theorems usually proved about such systems. As a reference on these topics, I recommend %\emph{%#<i>#Types and Programming Languages#</i>#%}%, by Benjamin C. Pierce.
adamc@5 168 *)
adamc@8 169
adamc@8 170
adamc@8 171 (** * Using This Book *)
adamc@8 172
adamc@8 173 (**
adamc@8 174 This book is generated automatically from Coq source files using the wonderful coqdoc program. The latest PDF version is available at:
adamc@8 175 %\begin{center}\url{http://adam.chlipala.net/cpdt/cpdt.pdf}\end{center}%#<blockquote><tt><a href="http://adam.chlipala.net/cpdt/cpdt.pdf">http://adam.chlipala.net/cpdt/cpdt.pdf</a></tt></blockquote>#
adamc@8 176 There is also an online HTML version available, with a hyperlink from each use of an identifier to that identifier's definition:
adamc@39 177 %\begin{center}\url{http://adam.chlipala.net/cpdt/html/toc.html}\end{center}%#<blockquote><tt><a href="http://adam.chlipala.net/cpdt/html/toc.html">http://adam.chlipala.net/cpdt/html/toc.html</a></tt></blockquote>#
adamc@8 178 The source code to the book is also freely available at:
adamc@8 179 %\begin{center}\url{http://adam.chlipala.net/cpdt/cpdt.tgz}\end{center}%#<blockquote><tt><a href="http://adam.chlipala.net/cpdt/cpdt.tgz">http://adam.chlipala.net/cpdt/cpdt.tgz</a></tt></blockquote>#
adamc@8 180
adamc@8 181 There, you can find all of the code appearing in this book, with prose interspersed in comments, in exactly the order that you find in this document. You can step through the code interactively with your chosen graphical Coq interface. The code also has special comments indicating which parts of the chapters make suitable starting points for interactive class sessions, where the class works together to construct the programs and proofs. The included Makefile has a target %\texttt{%#<tt>#templates#</tt>#%}% for building a fresh set of class template files automatically from the book source.
adamc@8 182
adam@279 183 I believe that a good graphical interface to Coq is crucial for using it productively. I use the %Proof General\footnote{\url{http://proofgeneral.inf.ed.ac.uk/}}%#<a href="http://proofgeneral.inf.ed.ac.uk/">Proof General</a># mode for Emacs, which supports a number of other proof assistants besides Coq. There is also the standalone CoqIDE program developed by the Coq team. I like being able to combine certified programming and proving with other kinds of work inside the same full-featured editor, and CoqIDE has had a good number of crashes and other annoying bugs in recent history, though I hear that it is improving. In the initial part of this book, I will reference Proof General procedures explicitly, in introducing how to use Coq, but most of the book will be interface-agnostic, so feel free to use CoqIDE if you prefer it. The one issue with CoqIDE, regarding running through the book source, is that I will sometimes begin a proof attempt but cancel it with the Coq [Abort] or [Restart] commands, which CoqIDE does not support. It would be bad form to leave such commands lying around in a real, finished development, but I find these commands helpful in writing single source files that trace a user's thought process in designing a proof.
adam@278 184
adam@278 185 The next chapter will introduce Coq with some simple examples, and the chapter starts with step-by-step instructions on getting set up to run the book chapters interactively, assuming that you have Coq and Proof General installed.
adamc@8 186 *)
adamc@25 187
adamc@25 188
adamc@25 189 (** %\section{Chapter Source Files}
adamc@25 190
adamc@25 191 \begin{center} \begin{tabular}{|r|l|}
adamc@25 192 \hline
adamc@25 193 \textbf{Chapter} & \textbf{Source} \\
adamc@25 194 \hline
adamc@25 195 Some Quick Examples & \texttt{StackMachine.v} \\
adamc@25 196 \hline
adamc@43 197 Introducing Inductive Types & \texttt{InductiveTypes.v} \\
adamc@26 198 \hline
adamc@49 199 Inductive Predicates & \texttt{Predicates.v} \\
adamc@49 200 \hline
adamc@62 201 Infinite Data and Proofs & \texttt{Coinductive.v} \\
adamc@62 202 \hline
adamc@70 203 Subset Types and Variations & \texttt{Subset.v} \\
adamc@70 204 \hline
adamc@83 205 More Dependent Types & \texttt{MoreDep.v} \\
adamc@83 206 \hline
adamc@105 207 Dependent Data Structures & \texttt{DataStruct.v} \\
adamc@105 208 \hline
adamc@118 209 Reasoning About Equality Proofs & \texttt{Equality.v} \\
adamc@118 210 \hline
adamc@219 211 Generic Programming & \texttt{Generic.v} \\
adamc@219 212 \hline
adamc@227 213 Universes and Axioms & \texttt{Universes.v} \\
adamc@227 214 \hline
adamc@132 215 Proof Search in Ltac & \texttt{Match.v} \\
adamc@132 216 \hline
adamc@142 217 Proof by Reflection & \texttt{Reflection.v} \\
adamc@142 218 \hline
adamc@235 219 Proving in the Large & \texttt{Large.v} \\
adamc@235 220 \hline
adamc@153 221 First-Order Abstract Syntax & \texttt{Firstorder.v} \\
adamc@152 222 \hline
adamc@265 223 Dependent De Bruijn Indices & \texttt{DeBruijn.v} \\
adamc@265 224 \hline
adamc@158 225 Higher-Order Abstract Syntax & \texttt{Hoas.v} \\
adamc@158 226 \hline
adamc@170 227 Type-Theoretic Interpreters & \texttt{Interps.v} \\
adamc@170 228 \hline
adamc@181 229 Extensional Transformations & \texttt{Extensional.v} \\
adamc@175 230 \hline
adamc@182 231 Intensional Transformations & \texttt{Intensional.v} \\
adamc@182 232 \hline
adamc@262 233 Higher-Order Operational Semantics & \texttt{OpSem.v} \\
adamc@190 234 \hline
adamc@25 235 \end{tabular} \end{center}
adamc@25 236
adamc@25 237 % *)