comparison src/Equality.v @ 124:57eaceb085f6

Templatize Equality
author Adam Chlipala <adamc@hcoop.net>
date Mon, 20 Oct 2008 09:11:54 -0400
parents 9ccd215e5112
children 0bfc75502498
comparison
equal deleted inserted replaced
123:9ccd215e5112 124:57eaceb085f6
32 | O => O 32 | O => O
33 | S n' => let y := n' in y 33 | S n' => let y := n' in y
34 end. 34 end.
35 35
36 Theorem reduce_me : pred' 1 = 0. 36 Theorem reduce_me : pred' 1 = 0.
37 (* begin thide *)
37 (** CIC follows the traditions of lambda calculus in associating reduction rules with Greek letters. Coq can certainly be said to support the familiar alpha reduction rule, which allows capture-avoiding renaming of bound variables, but we never need to apply alpha explicitly, since Coq uses a de Bruijn representation that encodes terms canonically. 38 (** CIC follows the traditions of lambda calculus in associating reduction rules with Greek letters. Coq can certainly be said to support the familiar alpha reduction rule, which allows capture-avoiding renaming of bound variables, but we never need to apply alpha explicitly, since Coq uses a de Bruijn representation that encodes terms canonically.
38 39
39 The delta rule is for unfolding global definitions. We can use it here to unfold the definition of [pred']. We do this with the [cbv] tactic, which takes a list of reduction rules and makes as many call-by-value reduction steps as possible, using only those rules. There is an analogous tactic [lazy] for call-by-name reduction. *) 40 The delta rule is for unfolding global definitions. We can use it here to unfold the definition of [pred']. We do this with the [cbv] tactic, which takes a list of reduction rules and makes as many call-by-value reduction steps as possible, using only those rules. There is an analogous tactic [lazy] for call-by-name reduction. *)
40 41
41 cbv delta. 42 cbv delta.
87 0 = 0 88 0 = 0
88 ]] *) 89 ]] *)
89 90
90 reflexivity. 91 reflexivity.
91 Qed. 92 Qed.
93 (* end thide *)
92 94
93 (** The standard [eq] relation is critically dependent on the definitional equality. [eq] is often called a %\textit{%#<i>#propositional equality#</i>#%}%, because it reifies definitional equality as a proposition that may or may not hold. Standard axiomatizations of an equality predicate in first-order logic define equality in terms of properties it has, like reflexivity, symmetry, and transitivity. In contrast, for [eq] in Coq, those properties are implicit in the properties of the definitional equality, which are built into CIC's metatheory and the implementation of Gallina. We could add new rules to the definitional equality, and [eq] would keep its definition and methods of use. 95 (** The standard [eq] relation is critically dependent on the definitional equality. [eq] is often called a %\textit{%#<i>#propositional equality#</i>#%}%, because it reifies definitional equality as a proposition that may or may not hold. Standard axiomatizations of an equality predicate in first-order logic define equality in terms of properties it has, like reflexivity, symmetry, and transitivity. In contrast, for [eq] in Coq, those properties are implicit in the properties of the definitional equality, which are built into CIC's metatheory and the implementation of Gallina. We could add new rules to the definitional equality, and [eq] would keep its definition and methods of use.
94 96
95 This all may make it sound like the choice of [eq]'s definition is unimportant. To the contrary, in this chapter, we will see examples where alternate definitions may simplify proofs. Before that point, we will introduce effective proof methods for goals that use proofs of the standard propositional equality "as data." *) 97 This all may make it sound like the choice of [eq]'s definition is unimportant. To the contrary, in this chapter, we will see examples where alternate definitions may simplify proofs. Before that point, we will introduce effective proof methods for goals that use proofs of the standard propositional equality "as data." *)
96 98
151 153
152 Variable elm : A. 154 Variable elm : A.
153 155
154 Theorem get_imap : forall ls (mem : fmember elm ls) (hls : fhlist B ls), 156 Theorem get_imap : forall ls (mem : fmember elm ls) (hls : fhlist B ls),
155 fhget (fhmap hls) mem = f (fhget hls mem). 157 fhget (fhmap hls) mem = f (fhget hls mem).
158 (* begin thide *)
156 induction ls; crush. 159 induction ls; crush.
157 160
158 (** Part of our single remaining subgoal is: 161 (** Part of our single remaining subgoal is:
159 162
160 [[ 163 [[
204 207
205 It seems that [destruct] was trying to be too smart for its own good. *) 208 It seems that [destruct] was trying to be too smart for its own good. *)
206 209
207 reflexivity. 210 reflexivity.
208 Qed. 211 Qed.
212 (* end thide *)
209 213
210 (** It will be helpful to examine the proof terms generated by this sort of strategy. A simpler example illustrates what is going on. *) 214 (** It will be helpful to examine the proof terms generated by this sort of strategy. A simpler example illustrates what is going on. *)
211 215
212 Lemma lemma1 : forall x (pf : x = elm), O = match pf with refl_equal => O end. 216 Lemma lemma1 : forall x (pf : x = elm), O = match pf with refl_equal => O end.
217 (* begin thide *)
213 simple destruct pf; reflexivity. 218 simple destruct pf; reflexivity.
214 Qed. 219 Qed.
220 (* end thide *)
215 221
216 (** [simple destruct pf] is a convenient form for applying [case]. It runs [intro] to bring into scope all quantified variables up to its argument. *) 222 (** [simple destruct pf] is a convenient form for applying [case]. It runs [intro] to bring into scope all quantified variables up to its argument. *)
217 223
218 Print lemma1. 224 Print lemma1.
219 225
231 end 237 end
232 ]] 238 ]]
233 239
234 Using what we know about shorthands for [match] annotations, we can write this proof in shorter form manually. *) 240 Using what we know about shorthands for [match] annotations, we can write this proof in shorter form manually. *)
235 241
242 (* begin thide *)
236 Definition lemma1' := 243 Definition lemma1' :=
237 fun (x : A) (pf : x = elm) => 244 fun (x : A) (pf : x = elm) =>
238 match pf return (0 = match pf with 245 match pf return (0 = match pf with
239 | refl_equal => 0 246 | refl_equal => 0
240 end) with 247 end) with
241 | refl_equal => refl_equal 0 248 | refl_equal => refl_equal 0
242 end. 249 end.
250 (* end thide *)
243 251
244 (** Surprisingly, what seems at first like a %\textit{%#<i>#simpler#</i>#%}% lemma is harder to prove. *) 252 (** Surprisingly, what seems at first like a %\textit{%#<i>#simpler#</i>#%}% lemma is harder to prove. *)
245 253
246 Lemma lemma2 : forall (x : A) (pf : x = x), O = match pf with refl_equal => O end. 254 Lemma lemma2 : forall (x : A) (pf : x = x), O = match pf with refl_equal => O end.
255 (* begin thide *)
247 (** [[ 256 (** [[
248 257
249 simple destruct pf. 258 simple destruct pf.
250 259
251 [[ 260 [[
254 ]] *) 263 ]] *)
255 Abort. 264 Abort.
256 265
257 (** Nonetheless, we can adapt the last manual proof to handle this theorem. *) 266 (** Nonetheless, we can adapt the last manual proof to handle this theorem. *)
258 267
259 Definition lemma2' := 268 (* begin thide *)
269 Definition lemma2 :=
260 fun (x : A) (pf : x = x) => 270 fun (x : A) (pf : x = x) =>
261 match pf return (0 = match pf with 271 match pf return (0 = match pf with
262 | refl_equal => 0 272 | refl_equal => 0
263 end) with 273 end) with
264 | refl_equal => refl_equal 0 274 | refl_equal => refl_equal 0
265 end. 275 end.
276 (* end thide *)
266 277
267 (** We can try to prove a lemma that would simplify proofs of many facts like [lemma2]: *) 278 (** We can try to prove a lemma that would simplify proofs of many facts like [lemma2]: *)
268 279
269 Lemma lemma3 : forall (x : A) (pf : x = x), pf = refl_equal x. 280 Lemma lemma3 : forall (x : A) (pf : x = x), pf = refl_equal x.
281 (* begin thide *)
270 (** [[ 282 (** [[
271 283
272 simple destruct pf. 284 simple destruct pf.
273 285
274 [[ 286 [[
324 Perhaps surprisingly, we cannot prove [eq_rect_eq] from within Coq. This proposition is introduced as an axiom; that is, a proposition asserted as true without proof. We cannot assert just any statement without proof. Adding [False] as an axiom would allow us to prove any proposition, for instance, defeating the point of using a proof assistant. In general, we need to be sure that we never assert %\textit{%#<i>#inconsistent#</i>#%}% sets of axioms. A set of axioms is inconsistent if its conjunction implies [False]. For the case of [eq_rect_eq], consistency has been verified outside of Coq via "informal" metatheory. 336 Perhaps surprisingly, we cannot prove [eq_rect_eq] from within Coq. This proposition is introduced as an axiom; that is, a proposition asserted as true without proof. We cannot assert just any statement without proof. Adding [False] as an axiom would allow us to prove any proposition, for instance, defeating the point of using a proof assistant. In general, we need to be sure that we never assert %\textit{%#<i>#inconsistent#</i>#%}% sets of axioms. A set of axioms is inconsistent if its conjunction implies [False]. For the case of [eq_rect_eq], consistency has been verified outside of Coq via "informal" metatheory.
325 337
326 This axiom is equivalent to another that is more commonly known and mentioned in type theory circles. *) 338 This axiom is equivalent to another that is more commonly known and mentioned in type theory circles. *)
327 339
328 Print Streicher_K. 340 Print Streicher_K.
341 (* end thide *)
329 (** [[ 342 (** [[
330 343
331 Streicher_K = 344 Streicher_K =
332 fun U : Type => UIP_refl__Streicher_K U (UIP_refl U) 345 fun U : Type => UIP_refl__Streicher_K U (UIP_refl U)
333 : forall (U : Type) (x : U) (P : x = x -> Prop), 346 : forall (U : Type) (x : U) (P : x = x -> Prop),
353 | nil => fun _ hls2 => hls2 366 | nil => fun _ hls2 => hls2
354 | _ :: _ => fun hls1 hls2 => (fst hls1, fhapp _ _ (snd hls1) hls2) 367 | _ :: _ => fun hls1 hls2 => (fst hls1, fhapp _ _ (snd hls1) hls2)
355 end. 368 end.
356 369
357 Implicit Arguments fhapp [ls1 ls2]. 370 Implicit Arguments fhapp [ls1 ls2].
371
372 (* EX: Prove that fhapp is associative. *)
373 (* begin thide *)
358 374
359 (** We might like to prove that [fhapp] is associative. 375 (** We might like to prove that [fhapp] is associative.
360 376
361 [[ 377 [[
362 378
541 intros. 557 intros.
542 rewrite (UIP_refl _ _ pf0). 558 rewrite (UIP_refl _ _ pf0).
543 rewrite (UIP_refl _ _ pf'0). 559 rewrite (UIP_refl _ _ pf'0).
544 reflexivity. 560 reflexivity.
545 Qed. 561 Qed.
562 (* end thide *)
546 End fhapp. 563 End fhapp.
547 564
548 Implicit Arguments fhapp [A B ls1 ls2]. 565 Implicit Arguments fhapp [A B ls1 ls2].
549 566
550 567
561 578
562 [JMeq] stands for "John Major equality," a name coined by Conor McBride as a sort of pun about British politics. [JMeq] starts out looking a lot like [eq]. The crucial difference is that we may use [JMeq] %\textit{%#<i>#on arguments of different types#</i>#%}%. For instance, a lemma that we failed to establish before is trivial with [JMeq]. It makes for prettier theorem statements to define some syntactic shorthand first. *) 579 [JMeq] stands for "John Major equality," a name coined by Conor McBride as a sort of pun about British politics. [JMeq] starts out looking a lot like [eq]. The crucial difference is that we may use [JMeq] %\textit{%#<i>#on arguments of different types#</i>#%}%. For instance, a lemma that we failed to establish before is trivial with [JMeq]. It makes for prettier theorem statements to define some syntactic shorthand first. *)
563 580
564 Infix "==" := JMeq (at level 70, no associativity). 581 Infix "==" := JMeq (at level 70, no associativity).
565 582
583 (* EX: Prove UIP_refl' : forall (A : Type) (x : A) (pf : x = x), pf == refl_equal x *)
584 (* begin thide *)
566 Definition UIP_refl' (A : Type) (x : A) (pf : x = x) : pf == refl_equal x := 585 Definition UIP_refl' (A : Type) (x : A) (pf : x = x) : pf == refl_equal x :=
567 match pf return (pf == refl_equal _) with 586 match pf return (pf == refl_equal _) with
568 | refl_equal => JMeq_refl _ 587 | refl_equal => JMeq_refl _
569 end. 588 end.
589 (* end thide *)
570 590
571 (** There is no quick way to write such a proof by tactics, but the underlying proof term that we want is trivial. 591 (** There is no quick way to write such a proof by tactics, but the underlying proof term that we want is trivial.
572 592
573 Suppose that we want to use [UIP_refl'] to establish another lemma of the kind of we have run into several times so far. *) 593 Suppose that we want to use [UIP_refl'] to establish another lemma of the kind of we have run into several times so far. *)
574 594
575 Lemma lemma4 : forall (A : Type) (x : A) (pf : x = x), 595 Lemma lemma4 : forall (A : Type) (x : A) (pf : x = x),
576 O = match pf with refl_equal => O end. 596 O = match pf with refl_equal => O end.
597 (* begin thide *)
577 intros; rewrite (UIP_refl' pf); reflexivity. 598 intros; rewrite (UIP_refl' pf); reflexivity.
578 Qed. 599 Qed.
600 (* end thide *)
579 601
580 (** All in all, refreshingly straightforward, but there really is no such thing as a free lunch. The use of [rewrite] is implemented in terms of an axiom: *) 602 (** All in all, refreshingly straightforward, but there really is no such thing as a free lunch. The use of [rewrite] is implemented in terms of an axiom: *)
581 603
582 Check JMeq_eq. 604 Check JMeq_eq.
583 (** [[ 605 (** [[
594 Variable A : Type. 616 Variable A : Type.
595 Variable B : A -> Type. 617 Variable B : A -> Type.
596 618
597 (** This time, the naive theorem statement type-checks. *) 619 (** This time, the naive theorem statement type-checks. *)
598 620
621 (* EX: Prove [fhapp] associativity using [JMeq]. *)
622
623 (* begin thide *)
599 Theorem fhapp_ass' : forall ls1 ls2 ls3 624 Theorem fhapp_ass' : forall ls1 ls2 ls3
600 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2) (hls3 : fhlist B ls3), 625 (hls1 : fhlist B ls1) (hls2 : fhlist B ls2) (hls3 : fhlist B ls3),
601 fhapp hls1 (fhapp hls2 hls3) == fhapp (fhapp hls1 hls2) hls3. 626 fhapp hls1 (fhapp hls2 hls3) == fhapp (fhapp hls1 hls2) hls3.
602 induction ls1; crush. 627 induction ls1; crush.
603 628
651 676
652 From this point, the goal is trivial. *) 677 From this point, the goal is trivial. *)
653 678
654 intros f f0 H; rewrite H; reflexivity. 679 intros f f0 H; rewrite H; reflexivity.
655 Qed. 680 Qed.
681 (* end thide *)
656 End fhapp'. 682 End fhapp'.
657 683
658 684
659 (** * Equivalence of Equality Axioms *) 685 (** * Equivalence of Equality Axioms *)
660 686
687 (* EX: Show that the approaches based on K and JMeq are equivalent logically. *)
688
689 (* begin thide *)
661 (** Assuming axioms (like axiom K and [JMeq_eq]) is a hazardous business. The due diligence associated with it is necessarily global in scope, since two axioms may be consistent alone but inconsistent together. It turns out that all of the major axioms proposed for reasoning about equality in Coq are logically equivalent, so that we only need to pick one to assert without proof. In this section, we demonstrate this by showing how each the previous two sections' approaches reduces to the other logically. 690 (** Assuming axioms (like axiom K and [JMeq_eq]) is a hazardous business. The due diligence associated with it is necessarily global in scope, since two axioms may be consistent alone but inconsistent together. It turns out that all of the major axioms proposed for reasoning about equality in Coq are logically equivalent, so that we only need to pick one to assert without proof. In this section, we demonstrate this by showing how each the previous two sections' approaches reduces to the other logically.
662 691
663 To show that [JMeq] and its axiom let us prove [UIP_refl], we start from the lemma [UIP_refl'] from the previous section. The rest of the proof is trivial. *) 692 To show that [JMeq] and its axiom let us prove [UIP_refl], we start from the lemma [UIP_refl'] from the previous section. The rest of the proof is trivial. *)
664 693
665 Lemma UIP_refl'' : forall (A : Type) (x : A) (pf : x = x), pf = refl_equal x. 694 Lemma UIP_refl'' : forall (A : Type) (x : A) (pf : x = x), pf = refl_equal x.
724 Qed. 753 Qed.
725 754
726 (** We see that, in a very formal sense, we are free to switch back and forth between the two styles of proofs about equality proofs. One style may be more convenient than the other for some proofs, but we can always intercovert between our results. The style that does not use heterogeneous equality may be preferable in cases where many results do not require the tricks of this chapter, since then the use of axioms is avoided altogether for the simple cases, and a wider audience will be able to follow those "simple" proofs. On the other hand, heterogeneous equality often makes for shorter and more readable theorem statements. 755 (** We see that, in a very formal sense, we are free to switch back and forth between the two styles of proofs about equality proofs. One style may be more convenient than the other for some proofs, but we can always intercovert between our results. The style that does not use heterogeneous equality may be preferable in cases where many results do not require the tricks of this chapter, since then the use of axioms is avoided altogether for the simple cases, and a wider audience will be able to follow those "simple" proofs. On the other hand, heterogeneous equality often makes for shorter and more readable theorem statements.
727 756
728 It is worth remarking that it is possible to avoid axioms altogether for equalities on types with decidable equality. The [Eqdep_dec] module of the standard library contains a parametric proof of [UIP_refl] for such cases. *) 757 It is worth remarking that it is possible to avoid axioms altogether for equalities on types with decidable equality. The [Eqdep_dec] module of the standard library contains a parametric proof of [UIP_refl] for such cases. *)
758 (* end thide *)
729 759
730 760
731 (** * Equality of Functions *) 761 (** * Equality of Functions *)
732 762
733 (** The following seems like a reasonable theorem to want to hold, and it does hold in set theory. [[ 763 (** The following seems like a reasonable theorem to want to hold, and it does hold in set theory. [[
734 764
735 Theorem S_eta : S = (fun n => S n). 765 Theorem S_eta : S = (fun n => S n).
736 766
737 Unfortunately, this theorem is not provable in CIC without additional axioms. None of the definitional equality rules force function equality to be %\textit{%#<i>#extensional#</i>#%}%. That is, the fact that two functions return equal results on equal inputs does not imply that the functions are equal. We %\textit{%#<i>#can#</i>#%}% assert function extensionality as an axiom. *) 767 Unfortunately, this theorem is not provable in CIC without additional axioms. None of the definitional equality rules force function equality to be %\textit{%#<i>#extensional#</i>#%}%. That is, the fact that two functions return equal results on equal inputs does not imply that the functions are equal. We %\textit{%#<i>#can#</i>#%}% assert function extensionality as an axiom. *)
738 768
769 (* begin thide *)
739 Axiom ext_eq : forall A B (f g : A -> B), 770 Axiom ext_eq : forall A B (f g : A -> B),
740 (forall x, f x = g x) 771 (forall x, f x = g x)
741 -> f = g. 772 -> f = g.
773 (* end thide *)
742 774
743 (** This axiom has been verified metatheoretically to be consistent with CIC and the two equality axioms we considered previously. With it, the proof of [S_eta] is trivial. *) 775 (** This axiom has been verified metatheoretically to be consistent with CIC and the two equality axioms we considered previously. With it, the proof of [S_eta] is trivial. *)
744 776
745 Theorem S_eta : S = (fun n => S n). 777 Theorem S_eta : S = (fun n => S n).
778 (* begin thide *)
746 apply ext_eq; reflexivity. 779 apply ext_eq; reflexivity.
747 Qed. 780 Qed.
781 (* end thide *)
748 782
749 (** The same axiom can help us prove equality of types, where we need to "reason under quantifiers." *) 783 (** The same axiom can help us prove equality of types, where we need to "reason under quantifiers." *)
750 784
751 Theorem forall_eq : (forall x : nat, match x with 785 Theorem forall_eq : (forall x : nat, match x with
752 | O => True 786 | O => True
754 end) 788 end)
755 = (forall _ : nat, True). 789 = (forall _ : nat, True).
756 790
757 (** There are no immediate opportunities to apply [ext_eq], but we can use [change] to fix that. *) 791 (** There are no immediate opportunities to apply [ext_eq], but we can use [change] to fix that. *)
758 792
793 (* begin thide *)
759 change ((forall x : nat, (fun x => match x with 794 change ((forall x : nat, (fun x => match x with
760 | 0 => True 795 | 0 => True
761 | S _ => True 796 | S _ => True
762 end) x) = (nat -> True)). 797 end) x) = (nat -> True)).
763 rewrite (ext_eq (fun x => match x with 798 rewrite (ext_eq (fun x => match x with
781 816
782 reflexivity. 817 reflexivity.
783 818
784 destruct x; constructor. 819 destruct x; constructor.
785 Qed. 820 Qed.
786 821 (* end thide *)