Mercurial > cpdt > repo
comparison src/Predicates.v @ 442:89c67796754e
Undo some overzealous vspace tweaks
author | Adam Chlipala <adam@chlipala.net> |
---|---|
date | Wed, 01 Aug 2012 16:09:37 -0400 |
parents | f923024bd284 |
children | 2740b8a23cce |
comparison
equal
deleted
inserted
replaced
441:cbfd23b4364d | 442:89c67796754e |
---|---|
43 Print True. | 43 Print True. |
44 (** %\vspace{-.15in}%[[ | 44 (** %\vspace{-.15in}%[[ |
45 Inductive True : Prop := I : True | 45 Inductive True : Prop := I : True |
46 ]] | 46 ]] |
47 | 47 |
48 %\smallskip{}%Recall that [unit] is the type with only one value, and [True] is the proposition that always holds. Despite this superficial difference between the two concepts, in both cases we can use the same inductive definition mechanism. The connection goes further than this. We see that we arrive at the definition of [True] by replacing [unit] by [True], [tt] by [I], and [Set] by [Prop]. The first two of these differences are superficial changes of names, while the third difference is the crucial one for separating programs from proofs. A term [T] of type [Set] is a type of programs, and a term of type [T] is a program. A term [T] of type [Prop] is a logical proposition, and its proofs are of type [T]. Chapter 12 goes into more detail about the theoretical differences between [Prop] and [Set]. For now, we will simply follow common intuitions about what a proof is. | 48 Recall that [unit] is the type with only one value, and [True] is the proposition that always holds. Despite this superficial difference between the two concepts, in both cases we can use the same inductive definition mechanism. The connection goes further than this. We see that we arrive at the definition of [True] by replacing [unit] by [True], [tt] by [I], and [Set] by [Prop]. The first two of these differences are superficial changes of names, while the third difference is the crucial one for separating programs from proofs. A term [T] of type [Set] is a type of programs, and a term of type [T] is a program. A term [T] of type [Prop] is a logical proposition, and its proofs are of type [T]. Chapter 12 goes into more detail about the theoretical differences between [Prop] and [Set]. For now, we will simply follow common intuitions about what a proof is. |
49 | 49 |
50 The type [unit] has one value, [tt]. The type [True] has one proof, [I]. Why distinguish between these two types? Many people who have read about Curry-Howard in an abstract context and not put it to use in proof engineering answer that the two types in fact _should not_ be distinguished. There is a certain aesthetic appeal to this point of view, but I want to argue that it is best to treat Curry-Howard very loosely in practical proving. There are Coq-specific reasons for preferring the distinction, involving efficient compilation and avoidance of paradoxes in the presence of classical math, but I will argue that there is a more general principle that should lead us to avoid conflating programming and proving. | 50 The type [unit] has one value, [tt]. The type [True] has one proof, [I]. Why distinguish between these two types? Many people who have read about Curry-Howard in an abstract context and not put it to use in proof engineering answer that the two types in fact _should not_ be distinguished. There is a certain aesthetic appeal to this point of view, but I want to argue that it is best to treat Curry-Howard very loosely in practical proving. There are Coq-specific reasons for preferring the distinction, involving efficient compilation and avoidance of paradoxes in the presence of classical math, but I will argue that there is a more general principle that should lead us to avoid conflating programming and proving. |
51 | 51 |
52 The essence of the argument is roughly this: to an engineer, not all functions of type [A -> B] are created equal, but all proofs of a proposition [P -> Q] are. This idea is known as%\index{proof irrelevance}% _proof irrelevance_, and its formalizations in logics prevent us from distinguishing between alternate proofs of the same proposition. Proof irrelevance is compatible with, but not derivable in, Gallina. Apart from this theoretical concern, I will argue that it is most effective to do engineering with Coq by employing different techniques for programs versus proofs. Most of this book is organized around that distinction, describing how to program, by applying standard functional programming techniques in the presence of dependent types; and how to prove, by writing custom Ltac decision procedures. | 52 The essence of the argument is roughly this: to an engineer, not all functions of type [A -> B] are created equal, but all proofs of a proposition [P -> Q] are. This idea is known as%\index{proof irrelevance}% _proof irrelevance_, and its formalizations in logics prevent us from distinguishing between alternate proofs of the same proposition. Proof irrelevance is compatible with, but not derivable in, Gallina. Apart from this theoretical concern, I will argue that it is most effective to do engineering with Coq by employing different techniques for programs versus proofs. Most of this book is organized around that distinction, describing how to program, by applying standard functional programming techniques in the presence of dependent types; and how to prove, by writing custom Ltac decision procedures. |
53 | 53 |
85 Print False. | 85 Print False. |
86 (** %\vspace{-.15in}%[[ | 86 (** %\vspace{-.15in}%[[ |
87 Inductive False : Prop := | 87 Inductive False : Prop := |
88 ]] | 88 ]] |
89 | 89 |
90 %\smallskip{}%We can conclude anything from [False], doing case analysis on a proof of [False] in the same way we might do case analysis on, say, a natural number. Since there are no cases to consider, any such case analysis succeeds immediately in proving the goal. *) | 90 We can conclude anything from [False], doing case analysis on a proof of [False] in the same way we might do case analysis on, say, a natural number. Since there are no cases to consider, any such case analysis succeeds immediately in proving the goal. *) |
91 | 91 |
92 Theorem False_imp : False -> 2 + 2 = 5. | 92 Theorem False_imp : False -> 2 + 2 = 5. |
93 (* begin thide *) | 93 (* begin thide *) |
94 destruct 1. | 94 destruct 1. |
95 (* end thide *) | 95 (* end thide *) |
127 (** %\vspace{-.15in}% [[ | 127 (** %\vspace{-.15in}% [[ |
128 not = fun A : Prop => A -> False | 128 not = fun A : Prop => A -> False |
129 : Prop -> Prop | 129 : Prop -> Prop |
130 ]] | 130 ]] |
131 | 131 |
132 %\smallskip{}%We see that [not] is just shorthand for implication of [False]. We can use that fact explicitly in proofs. The syntax [~ P] expands to [not P]. *) | 132 We see that [not] is just shorthand for implication of [False]. We can use that fact explicitly in proofs. The syntax [~ P] expands to [not P]. *) |
133 | 133 |
134 Theorem arith_neq' : ~ (2 + 2 = 5). | 134 Theorem arith_neq' : ~ (2 + 2 = 5). |
135 (* begin thide *) | 135 (* begin thide *) |
136 unfold not. | 136 unfold not. |
137 (** [[ | 137 (** [[ |
149 Print and. | 149 Print and. |
150 (** %\vspace{-.15in}%[[ | 150 (** %\vspace{-.15in}%[[ |
151 Inductive and (A : Prop) (B : Prop) : Prop := conj : A -> B -> A /\ B | 151 Inductive and (A : Prop) (B : Prop) : Prop := conj : A -> B -> A /\ B |
152 ]] | 152 ]] |
153 | 153 |
154 %\smallskip{}%The interested reader can check that [and] has a Curry-Howard equivalent called %\index{Gallina terms!prod}%[prod], the type of pairs. However, it is generally most convenient to reason about conjunction using tactics. An explicit proof of commutativity of [and] illustrates the usual suspects for such tasks. The operator [/\] is an infix shorthand for [and]. *) | 154 The interested reader can check that [and] has a Curry-Howard equivalent called %\index{Gallina terms!prod}%[prod], the type of pairs. However, it is generally most convenient to reason about conjunction using tactics. An explicit proof of commutativity of [and] illustrates the usual suspects for such tasks. The operator [/\] is an infix shorthand for [and]. *) |
155 | 155 |
156 Theorem and_comm : P /\ Q -> Q /\ P. | 156 Theorem and_comm : P /\ Q -> Q /\ P. |
157 | 157 |
158 (* begin thide *) | 158 (* begin thide *) |
159 (** We start by case analysis on the proof of [P /\ Q]. *) | 159 (** We start by case analysis on the proof of [P /\ Q]. *) |
197 (** %\vspace{-.15in}%[[ | 197 (** %\vspace{-.15in}%[[ |
198 Inductive or (A : Prop) (B : Prop) : Prop := | 198 Inductive or (A : Prop) (B : Prop) : Prop := |
199 or_introl : A -> A \/ B | or_intror : B -> A \/ B | 199 or_introl : A -> A \/ B | or_intror : B -> A \/ B |
200 ]] | 200 ]] |
201 | 201 |
202 %\smallskip{}%We see that there are two ways to prov a disjunction: prove the first disjunct or prove the second. The Curry-Howard analogue of this is the Coq %\index{Gallina terms!sum}%[sum] type. We can demonstrate the main tactics here with another proof of commutativity. *) | 202 We see that there are two ways to prov a disjunction: prove the first disjunct or prove the second. The Curry-Howard analogue of this is the Coq %\index{Gallina terms!sum}%[sum] type. We can demonstrate the main tactics here with another proof of commutativity. *) |
203 | 203 |
204 Theorem or_comm : P \/ Q -> Q \/ P. | 204 Theorem or_comm : P \/ Q -> Q \/ P. |
205 | 205 |
206 (* begin thide *) | 206 (* begin thide *) |
207 (** As in the proof for [and], we begin with case analysis, though this time we are met by two cases instead of one. *) | 207 (** As in the proof for [and], we begin with case analysis, though this time we are met by two cases instead of one. *) |
371 (** %\vspace{-.15in}%[[ | 371 (** %\vspace{-.15in}%[[ |
372 Inductive ex (A : Type) (P : A -> Prop) : Prop := | 372 Inductive ex (A : Type) (P : A -> Prop) : Prop := |
373 ex_intro : forall x : A, P x -> ex P | 373 ex_intro : forall x : A, P x -> ex P |
374 ]] | 374 ]] |
375 | 375 |
376 %\smallskip{}%The family [ex] is parameterized by the type [A] that we quantify over, and by a predicate [P] over [A]s. We prove an existential by exhibiting some [x] of type [A], along with a proof of [P x]. As usual, there are tactics that save us from worrying about the low-level details most of the time. We use the equality operator [=], which, depending on the settings in which they learned logic, different people will say either is or is not part of first-order logic. For our purposes, it is. *) | 376 The family [ex] is parameterized by the type [A] that we quantify over, and by a predicate [P] over [A]s. We prove an existential by exhibiting some [x] of type [A], along with a proof of [P x]. As usual, there are tactics that save us from worrying about the low-level details most of the time. We use the equality operator [=], which, depending on the settings in which they learned logic, different people will say either is or is not part of first-order logic. For our purposes, it is. *) |
377 | 377 |
378 Theorem exist1 : exists x : nat, x + 1 = 2. | 378 Theorem exist1 : exists x : nat, x + 1 = 2. |
379 (* begin thide *) | 379 (* begin thide *) |
380 (** remove printing exists *) | 380 (** remove printing exists *) |
381 (** We can start this proof with a tactic %\index{tactics!exists}%[exists], which should not be confused with the formula constructor shorthand of the same name. (In the PDF version of this document, the reverse %`%#'#E#'#%'% appears instead of the text "exists" in formulas.) *) | 381 (** We can start this proof with a tactic %\index{tactics!exists}%[exists], which should not be confused with the formula constructor shorthand of the same name. (In the PDF version of this document, the reverse %`%#'#E#'#%'% appears instead of the text "exists" in formulas.) *) |
461 Print eq. | 461 Print eq. |
462 (** %\vspace{-.15in}%[[ | 462 (** %\vspace{-.15in}%[[ |
463 Inductive eq (A : Type) (x : A) : A -> Prop := eq_refl : x = x | 463 Inductive eq (A : Type) (x : A) : A -> Prop := eq_refl : x = x |
464 ]] | 464 ]] |
465 | 465 |
466 %\smallskip{}%Behind the scenes, uses of infix [=] are expanded to instances of [eq]. We see that [eq] has both a parameter [x] that is fixed and an extra unnamed argument of the same type. The type of [eq] allows us to state any equalities, even those that are provably false. However, examining the type of equality's sole constructor [eq_refl], we see that we can only _prove_ equality when its two arguments are syntactically equal. This definition turns out to capture all of the basic properties of equality, and the equality-manipulating tactics that we have seen so far, like [reflexivity] and [rewrite], are implemented treating [eq] as just another inductive type with a well-chosen definition. Another way of stating that definition is: equality is defined as the least reflexive relation. | 466 Behind the scenes, uses of infix [=] are expanded to instances of [eq]. We see that [eq] has both a parameter [x] that is fixed and an extra unnamed argument of the same type. The type of [eq] allows us to state any equalities, even those that are provably false. However, examining the type of equality's sole constructor [eq_refl], we see that we can only _prove_ equality when its two arguments are syntactically equal. This definition turns out to capture all of the basic properties of equality, and the equality-manipulating tactics that we have seen so far, like [reflexivity] and [rewrite], are implemented treating [eq] as just another inductive type with a well-chosen definition. Another way of stating that definition is: equality is defined as the least reflexive relation. |
467 | 467 |
468 Returning to the example of [isZero], we can see how to work with hypotheses that use this predicate. *) | 468 Returning to the example of [isZero], we can see how to work with hypotheses that use this predicate. *) |
469 | 469 |
470 Theorem isZero_plus : forall n m : nat, isZero m -> n + m = n. | 470 Theorem isZero_plus : forall n m : nat, isZero m -> n + m = n. |
471 (* begin thide *) | 471 (* begin thide *) |
529 (** %\vspace{-.15in}% [[ | 529 (** %\vspace{-.15in}% [[ |
530 isZero_ind | 530 isZero_ind |
531 : forall P : nat -> Prop, P 0 -> forall n : nat, isZero n -> P n | 531 : forall P : nat -> Prop, P 0 -> forall n : nat, isZero n -> P n |
532 ]] | 532 ]] |
533 | 533 |
534 %\smallskip{}%In our last proof script, [destruct] chose to instantiate [P] as [fun n => S n + S n = S (S (S (S n)))]. You can verify for yourself that this specialization of the principle applies to the goal and that the hypothesis [P 0] then matches the subgoal we saw generated. If you are doing a proof and encounter a strange transmutation like this, there is a good chance that you should go back and replace a use of [destruct] with [inversion]. *) | 534 In our last proof script, [destruct] chose to instantiate [P] as [fun n => S n + S n = S (S (S (S n)))]. You can verify for yourself that this specialization of the principle applies to the goal and that the hypothesis [P 0] then matches the subgoal we saw generated. If you are doing a proof and encounter a strange transmutation like this, there is a good chance that you should go back and replace a use of [destruct] with [inversion]. *) |
535 | 535 |
536 | 536 |
537 (* begin hide *) | 537 (* begin hide *) |
538 (* In-class exercises *) | 538 (* In-class exercises *) |
539 | 539 |