-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logic.v
1561 lines (1287 loc) · 50.5 KB
/
Logic.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** * Logic: Logic in Coq *)
Set Warnings "-notation-overridden,-parsing".
From COC Require Export Tactics.
(** We have seen many examples of factual claims (_propositions_)
and ways of presenting evidence of their truth (_proofs_). In
particular, we have worked extensively with _equality
propositions_ ([e1 = e2]), implications ([P -> Q]), and quantified
propositions ([forall x, P]). In this chapter, we will see how
Coq can be used to carry out other familiar forms of logical
reasoning.
Before diving into details, let's talk a bit about the status of
mathematical statements in Coq. Recall that Coq is a _typed_
language, which means that every sensible expression in its world
has an associated type. Logical claims are no exception: any
statement we might try to prove in Coq has a type, namely [Prop],
the type of _propositions_. We can see this with the [Check]
command: *)
Check 3 = 3 : Prop.
Check forall n m : nat, n + m = m + n : Prop.
(** Note that _all_ syntactically well-formed propositions have type
[Prop] in Coq, regardless of whether they are true. *)
(** Simply _being_ a proposition is one thing; being _provable_ is
something else! *)
Check 2 = 2 : Prop.
Check 3 = 2 : Prop.
Check forall n : nat, n = 2 : Prop.
(** Indeed, propositions not only have types: they are
_first-class_ entities that can be manipulated in all the same
ways as any of the other things in Coq's world. *)
(** So far, we've seen one primary place that propositions can appear:
in [Theorem] (and [Lemma] and [Example]) declarations. *)
Theorem plus_2_2_is_4 :
2 + 2 = 4.
Proof. reflexivity. Qed.
(** But propositions can be used in many other ways. For example, we
can give a name to a proposition using a [Definition], just as we
have given names to other kinds of expressions. *)
Definition plus_claim : Prop := 2 + 2 = 4.
Check plus_claim : Prop.
(** We can later use this name in any situation where a proposition is
expected -- for example, as the claim in a [Theorem] declaration. *)
Theorem plus_claim_is_true :
plus_claim.
Proof. reflexivity. Qed.
(** We can also write _parameterized_ propositions -- that is,
functions that take arguments of some type and return a
proposition. *)
(** For instance, the following function takes a number
and returns a proposition asserting that this number is equal to
three: *)
Definition is_three (n : nat) : Prop :=
n = 3.
Check is_three : nat -> Prop.
(** In Coq, functions that return propositions are said to define
_properties_ of their arguments. For instance, here's a
(polymorphic) property defining the familiar notion of an
_injective function_. *)
Definition injective {A B} (f : A -> B) :=
forall x y : A, f x = f y -> x = y.
Lemma succ_inj : injective S.
Proof.
intros n m H. injection H as H1. apply H1.
Qed.
(** The equality operator [=] is also a function that returns a
[Prop].
The expression [n = m] is syntactic sugar for [eq n m] (defined in
Coq's standard library using the [Notation] mechanism). Because
[eq] can be used with elements of any type, it is also
polymorphic: *)
Check @eq : forall A : Type, A -> A -> Prop.
(** (Notice that we wrote [@eq] instead of [eq]: The type
argument [A] to [eq] is declared as implicit, and we need to turn
off the inference of this implicit argument to see the full type
of [eq].) *)
(* ################################################################# *)
(** * Logical Connectives *)
(* ================================================================= *)
(** ** Conjunction *)
(** The _conjunction_, or _logical and_, of propositions [A] and [B]
is written [A /\ B], representing the claim that both [A] and [B]
are true. *)
Example and_example : 3 + 4 = 7 /\ 2 * 2 = 4.
(** To prove a conjunction, use the [split] tactic. It will generate
two subgoals, one for each part of the statement: *)
Proof.
split.
- (* 3 + 4 = 7 *) reflexivity.
- (* 2 * 2 = 4 *) reflexivity.
Qed.
(** For any propositions [A] and [B], if we assume that [A] is true
and that [B] is true, we can conclude that [A /\ B] is also
true. *)
Lemma and_intro : forall A B : Prop, A -> B -> A /\ B.
Proof.
intros A B HA HB. split.
- apply HA.
- apply HB.
Qed.
(** Since applying a theorem with hypotheses to some goal has the
effect of generating as many subgoals as there are hypotheses for
that theorem, we can apply [and_intro] to achieve the same effect
as [split]. *)
Example and_example' : 3 + 4 = 7 /\ 2 * 2 = 4.
Proof.
apply and_intro.
- (* 3 + 4 = 7 *) reflexivity.
- (* 2 + 2 = 4 *) reflexivity.
Qed.
(** **** Exercise: 2 stars (and_exercise) *)
Example and_exercise :
forall n m : nat, n + m = 0
-> n = 0 /\ m = 0.
Proof.
intros n m H.
apply and_intro.
- (* n = 0 *) induction n as [| n'].
+ reflexivity.
+ discriminate H.
- (* m = 0 *) induction m as [| m'].
+ reflexivity.
+ rewrite plus_comm in H. discriminate H.
Qed.
Lemma and_example2 :
forall n m : nat, n = 0 /\ m = 0 -> n + m = 0.
Proof.
intros n m H.
destruct H as [Hn Hm].
rewrite -> Hn. rewrite -> Hm. simpl.
reflexivity.
Qed.
(** As usual, we can also destruct [H] right when we introduce it,
instead of introducing and then destructing it: *)
Lemma and_example2' :
forall n m : nat, n = 0 /\ m = 0 -> n + m = 0.
Proof.
intros n m [Hn Hm].
rewrite Hn. rewrite Hm.
reflexivity.
Qed.
(** You may wonder why we bothered packing the two hypotheses [n = 0]
and [m = 0] into a single conjunction, since we could have also
stated the theorem with two separate premises: *)
Lemma and_example2'' :
forall n m : nat, n = 0 -> m = 0 -> n + m = 0.
Proof.
intros n m Hn Hm.
rewrite Hn. rewrite Hm.
reflexivity.
Qed.
(** For this specific theorem, both formulations are fine. But
it's important to understand how to work with conjunctive
hypotheses because conjunctions often arise from intermediate
steps in proofs, especially in larger developments. Here's a
simple example: *)
Lemma and_example3 :
forall n m : nat, n + m = 0 -> n * m = 0.
Proof.
(* WORKED IN CLASS *)
intros n m H.
apply and_exercise in H.
destruct H as [Hn Hm].
rewrite -> Hn. simpl. reflexivity.
Qed.
(** Another common situation with conjunctions is that we know
[A /\ B] but in some context we need just [A] or just [B].
In such cases we can do a [destruct] (possibly as part of
an [intros]) and use an underscore pattern [_] to indicate
that the unneeded conjunct should just be thrown away. *)
Lemma proj1 : forall P Q : Prop,
P /\ Q -> P.
Proof.
intros P Q HPQ.
destruct HPQ as [HP _].
(** destruct HPQ as [_ HQ]. to return Q instead of P **)
apply HP.
Qed.
(** **** Exercise: 1 star, standard, optional (proj2) *)
Lemma proj2 : forall P Q : Prop,
P /\ Q -> Q.
Proof.
intros P Q HPQ.
destruct HPQ as [_ HQ].
apply HQ.
Qed.
(** Finally, we sometimes need to rearrange the order of conjunctions
and/or the grouping of multi-way conjunctions. The following
commutativity and associativity theorems are handy in such
cases. *)
Theorem and_commut : forall P Q : Prop,
P /\ Q -> Q /\ P.
Proof.
intros P Q [HP HQ].
split.
- (* left *) apply HQ.
- (* right *) apply HP.
Qed.
(** **** Exercise: 2 stars, standard (and_assoc)
(In the following proof of associativity, notice how the _nested_
[intros] pattern breaks the hypothesis [H : P /\ (Q /\ R)] down into
[HP : P], [HQ : Q], and [HR : R]. Finish the proof from
there.) *)
Theorem and_assoc : forall P Q R : Prop,
P /\ (Q /\ R) -> (P /\ Q) /\ R.
Proof.
intros P Q R [HP [HQ HR]].
split.
- (* inner *) split.
+ apply HP.
+ apply HQ.
- (* outer *) apply HR.
Qed.
(** By the way, the infix notation [/\] is actually just syntactic
sugar for [and A B]. That is, [and] is a Coq operator that takes
two propositions as arguments and yields a proposition. *)
Check and : Prop -> Prop -> Prop.
(* ===> and : Prop -> Prop -> Prop *)
(* ================================================================= *)
(** ** Disjunction *)
(** Another important connective is the _disjunction_, or _logical or_,
of two propositions: [A \/ B] is true when either [A] or [B]
is. (This infix notation stands for [or A B], where [or : Prop ->
Prop -> Prop].) *)
(** To use a disjunctive hypothesis in a proof, we proceed by case
analysis (which, as with other data types like [nat], can be done
explicitly with [destruct] or implicitly with an [intros]
pattern): *)
Lemma eq_mult_0 :
forall n m : nat, n = 0 \/ m = 0 -> n * m = 0.
Proof.
(* This pattern implicitly does case analysis on
[n = 0 \/ m = 0] *)
intros n m [Hn | Hm].
- (* Here, [n = 0] *)
rewrite -> Hn. reflexivity.
- (* Here, [m = 0] *)
rewrite -> Hm. rewrite -> mult_n_O. reflexivity.
Qed.
(** Conversely, to show that a disjunction holds, it suffices to show
that one of its sides holds. This is done via two tactics, [left]
and [right]. As their names imply, the first one requires proving
the left side of the disjunction, while the second requires
proving its right side. Here is a trivial use... *)
Lemma or_intro_l : forall A B : Prop, A -> A \/ B.
Proof.
intros A B HA.
left.
apply HA.
Qed.
(** ... and here is a slightly more interesting example requiring both
[left] and [right]: *)
Lemma zero_or_succ :
forall n : nat, n = 0 \/ n = S (pred n).
Proof.
(* WORKED IN CLASS *)
intros n. induction n as [|n'].
- (* left *)
left. reflexivity.
- (* right *)
right. simpl. reflexivity.
Qed.
Search ( _ * _ = _ ).
(** **** Exercise: 1 star (mult_eq_0) *)
Lemma mult_eq_0 : forall n m,
n * m = 0 -> n = 0 \/ m = 0.
Proof.
intros n. induction n as [|n'].
- (* n = O *)
intros m H. rewrite mult_O_1 in H. left. apply H.
- (* n = S n' *) intros m. induction m as [| m].
+ intros H. simpl in H. rewrite mult_n_O in H. right. apply H.
+ intros H. discriminate H.
Qed.
(** **** Exercise: 1 star (or_commut) *)
Theorem or_commut : forall P Q : Prop,
P \/ Q -> Q \/ P.
Proof.
intros P Q [HP|HQ].
- right. apply HP.
- left. apply HQ.
Qed.
(* ================================================================= *)
(** ** Falsehood and Negation
So far, we have mostly been concerned with proving that certain
things are _true_ -- addition is commutative, appending lists is
associative, etc. Of course, we may also be interested in
negative results, demonstrating that some given proposition is
_not_ true. Such statements are expressed with the logical
negation operator [~]. *)
(** To see how negation works, recall the _principle of explosion_
from the [Tactics] chapter, which asserts that, if we assume a
contradiction, then any other proposition can be derived.
Following this intuition, we could define [~ P] ("not [P]") as
[forall Q, P -> Q].
Coq actually makes a slightly different (but equivalent) choice,
defining [~ P] as [P -> False], where [False] is a specific
contradictory proposition defined in the standard library. *)
Module MyNot.
Definition not (P:Prop) := P -> False.
Notation "~ x" := (not x) : type_scope.
Check not : Prop -> Prop.
End MyNot.
(** Since [False] is a contradictory proposition, the principle of
explosion also applies to it. If we get [False] into the proof
context, we can use [destruct] on it to complete any goal: *)
Theorem ex_falso_quodlibet : forall (P:Prop),
False -> P.
Proof.
(* WORKED IN CLASS *)
intros P contra.
destruct contra.
Qed.
(** **** Exercise: 2 stars, standard, optional (not_implies_our_not)
Show that Coq's definition of negation implies the intuitive one
mentioned above: *)
Fact not_implies_our_not : forall (P:Prop),
~ P -> (forall (Q:Prop), P -> Q).
Proof.
intros P HPnot Q HP.
contradiction.
Qed.
(** Inequality is a frequent enough example of negated statement
that there is a special notation for it, [x <> y]: **)
Notation "x <> y" := (~(x = y)).
(** We can use [not] to state that [0] and [1] are different elements
of [nat]: *)
Theorem zero_not_one : 0 <> 1.
Proof.
(** The proposition [0 <> 1] is exactly the same as
[~(0 = 1)], that is [not (0 = 1)], which unfolds to
[(0 = 1) -> False]. (We use [unfold not] explicitly here
to illustrate that point, but generally it can be omitted.) *)
unfold not.
(** To prove an inequality, we may assume the opposite
equality... *)
intros contra.
(** ... and deduce a contradiction from it. Here, the
equality [O = S O] contradicts the disjointness of
constructors [O] and [S], so [discriminate] takes care
of it. *)
discriminate contra.
Qed.
(** It takes a little practice to get used to working with negation in
Coq. Even though you can see perfectly well why a statement
involving negation is true, it can be a little tricky at first to
make Coq understand it! Here are proofs of a few familiar facts
to get you warmed up. *)
Theorem not_False :
~ False.
Proof.
unfold not. intros H. destruct H.
Qed.
Theorem contradiction_implies_anything : forall P Q : Prop,
(P /\ ~P) -> Q.
Proof.
(* WORKED IN CLASS *)
intros P Q [HP HNA]. unfold not in HNA.
apply HNA in HP. destruct HP.
Qed.
Theorem double_neg : forall P : Prop,
P -> ~~P.
Proof.
(* WORKED IN CLASS *)
intros P H. unfold not. intros G. apply G. apply H.
Qed.
(** **** Exercise: 2 stars, recommended (contrapositive) *)
Theorem contrapositive : forall P Q : Prop,
(P -> Q) -> (~Q -> ~P).
Proof.
intros P Q H HQnot.
unfold not. unfold not in HQnot. intros P'. apply HQnot. apply H. apply P'.
Qed.
(** **** Exercise: 1 star (not_both_true_and_false) *)
Theorem not_both_true_and_false : forall P : Prop,
~ (P /\ ~P).
Proof.
intros P. unfold not. intros [HP HPnot].
apply HPnot. apply HP.
Qed.
(** Since inequality involves a negation, it also requires a little
practice to be able to work with it fluently. Here is one useful
trick. If you are trying to prove a goal that is
nonsensical (e.g., the goal state is [false = true]), apply
[ex_falso_quodlibet] to change the goal to [False]. This makes it
easier to use assumptions of the form [~P] that may be available
in the context -- in particular, assumptions of the form
[x<>y]. *)
Theorem not_true_is_false : forall b : bool,
b <> true -> b = false.
Proof.
intros b H.
destruct b eqn:HE.
- (* b = true *)
unfold not in H.
apply ex_falso_quodlibet.
apply H. reflexivity.
- (* b = false *)
reflexivity.
Qed.
(** Since reasoning with [ex_falso_quodlibet] is quite common, Coq
provides a built-in tactic, [exfalso], for applying it. *)
Theorem not_true_is_false' : forall b : bool,
b <> true -> b = false.
Proof.
intros [] H. (* note implicit [destruct b] here *)
- (* b = true *)
unfold not in H.
exfalso. (* <=== *)
apply H. reflexivity.
- (* b = false *) reflexivity.
Qed.
(* ================================================================= *)
(** ** Truth *)
(** Besides [False], Coq's standard library also defines [True], a
proposition that is trivially true. To prove it, we use the
predefined constant [I : True]: *)
Lemma True_is_true : True.
Proof. apply I. Qed.
(** Unlike [False], which is used extensively, [True] is used quite
rarely, since it is trivial (and therefore uninteresting) to prove
as a goal, and it carries no useful information as a hypothesis.
But it can be quite useful when defining complex [Prop]s using
conditionals or as a parameter to higher-order [Prop]s. We will
see examples later on. *)
(* ================================================================= *)
(** ** Logical Equivalence *)
(** The handy "if and only if" connective, which asserts that two
propositions have the same truth value, is simply the conjunction
of two implications. *)
Module MyIff.
Definition iff (P Q : Prop) := (P -> Q) /\ (Q -> P).
Notation "P <-> Q" := (iff P Q)
(at level 95, no associativity)
: type_scope.
End MyIff.
Theorem iff_sym : forall P Q : Prop,
(P <-> Q) -> (Q <-> P).
Proof.
(* WORKED IN CLASS *)
intros P Q [HAB HBA].
split.
- (* -> *) apply HBA.
- (* <- *) apply HAB.
Qed.
Lemma not_true_iff_false : forall b,
b <> true <-> b = false.
Proof.
(* WORKED IN CLASS *)
intros b. split.
- (* -> *) apply not_true_is_false.
- (* <- *)
intros H. rewrite H. intros H'. discriminate H'.
Qed.
Theorem iff_refl : forall P : Prop,
P <-> P.
Proof.
intros P. split.
- (* -> *) intros P'. apply P'.
- (* <- *) intros P'. apply P'.
Qed.
Theorem iff_trans : forall P Q R : Prop,
(P <-> Q) -> (Q <-> R) -> (P <-> R).
Proof.
intros P Q R [HPQ HQP] [HQR HRQ]. split.
- (* -> *) intros P'. apply HPQ in P'. apply HQR in P'. apply P'.
- (* <- *) intros R'. apply HRQ in R'. apply HQP in R'. apply R'.
Qed.
(** **** Exercise: 3 stars (or_distributes_over_and) *)
Theorem or_distributes_over_and : forall P Q R : Prop,
P \/ (Q /\ R) <-> (P \/ Q) /\ (P \/ R).
Proof.
intros P Q R. split.
- (* -> *) intros H. inversion H.
+ split.
{ left. apply H0. }
{ left. apply H0. }
+ split.
{ apply proj1 in H0. right. apply H0. }
{ apply proj2 in H0. right. apply H0. }
- (* <- *) intros H. inversion H.
+ destruct H1.
{ left. apply H1. }
{ destruct H0.
{ left. apply H0. }
{ right. split.
{ apply H0. }
{ apply H1. } } }
Qed.
(* ================================================================= *)
(** ** Setoids and Logical Equivalence *)
(** Some of Coq's tactics treat [iff] statements specially, avoiding
the need for some low-level proof-state manipulation. In
particular, [rewrite] and [reflexivity] can be used with [iff]
statements, not just equalities. To enable this behavior, we have
to import the Coq library that supports it: *)
From Coq Require Import Setoids.Setoid.
(** A "setoid" is a set equipped with an equivalence relation,
that is, a relation that is reflexive, symmetric, and transitive.
When two elements of a set are equivalent according to the
relation, [rewrite] can be used to replace one element with the
other. We've seen that already with the equality relation [=] in
Coq: when [x = y], we can use [rewrite] to replace [x] with [y],
or vice-versa.
Similarly, the logical equivalence relation [<->] is reflexive,
symmetric, and transitive, so we can use it to replace one part of
a proposition with another: if [P <-> Q], then we can use
[rewrite] to replace [P] with [Q], or vice-versa. *)
(** Here is a simple example demonstrating how these tactics work with
[iff]. First, let's prove a couple of basic iff equivalences. *)
Lemma mult_0 : forall n m, n * m = 0 <-> n = 0 \/ m = 0.
Proof.
split.
- (* -> *) apply mult_eq_0.
- (* <- *) apply eq_mult_0.
Qed.
Theorem or_assoc :
forall P Q R : Prop, P \/ (Q \/ R) <-> (P \/ Q) \/ R.
Proof.
intros P Q R. split.
- (* -> *) intros [H | [H | H]].
+ left. left. apply H.
+ left. right. apply H.
+ right. apply H.
- (* <- *) intros [[H | H] | H].
+ left. apply H.
+ right. left. apply H.
+ right. right. apply H.
Qed.
(** We can now use these facts with [rewrite] and [reflexivity]
to give smooth proofs of statements involving equivalences. For
example, here is a ternary version of the previous [mult_0]
result: *)
Lemma mult_0_3 :
forall n m p, n * m * p = 0 <-> n = 0 \/ m = 0 \/ p = 0.
Proof.
intros n m p.
rewrite -> mult_0. rewrite -> mult_0. rewrite -> or_assoc.
reflexivity.
Qed.
(** The [apply] tactic can also be used with [<->]. When given an
equivalence as its argument, [apply] tries to guess which direction of
the equivalence will be useful. *)
Lemma apply_iff_example :
forall n m : nat, n * m = 0 -> n = 0 \/ m = 0.
Proof.
intros n m H. apply mult_0. apply H.
Qed.
(* ================================================================= *)
(** ** Existential Quantification *)
(** Another important logical connective is _existential
quantification_. To say that there is some [x] of type [T] such
that some property [P] holds of [x], we write [exists x : T,
P]. As with [forall], the type annotation [: T] can be omitted if
Coq is able to infer from the context what the type of [x] should
be. *)
(** To prove a statement of the form [exists x, P], we must show that
[P] holds for some specific choice of value for [x], known as the
_witness_ of the existential. This is done in two steps: First,
we explicitly tell Coq which witness [t] we have in mind by
invoking the tactic [exists t]. Then we prove that [P] holds after
all occurrences of [x] are replaced by [t]. *)
Definition even x := exists n : nat, x = double n.
Lemma four_is_even : even 4.
Proof.
(* witness_ of the existential is given as 2 in this context *)
unfold even. exists 2. reflexivity.
Qed.
(** Conversely, if we have an existential hypothesis [exists x, P] in
the context, we can destruct it to obtain a witness [x] and a
hypothesis stating that [P] holds of [x]. *)
Theorem exists_example_2 : forall n,
(exists m, n = 4 + m) ->
(exists o, n = 2 + o).
Proof.
(* WORKED IN CLASS *)
intros n [m Hm]. (* note implicit [destruct] here *)
exists (2 + m).
apply Hm.
Qed.
(** **** Exercise: 1 star, standard, especially useful (dist_not_exists)
Prove that "[P] holds for all [x]" implies "there is no [x] for
which [P] does not hold." (Hint: [destruct H as [x E]] works on
existential assumptions!) *)
Theorem dist_not_exists : forall (X:Type) (P : X -> Prop),
(forall x, P x) -> ~ (exists x, ~ P x).
Proof.
intros X P Hall Hexist.
destruct Hexist as [x HPnot].
unfold not in HPnot. apply HPnot. apply Hall.
Qed.
(** **** Exercise: 2 stars (dist_exists_or) *)
(** Prove that existential quantification distributes over
disjunction. *)
Theorem dist_exists_or : forall (X:Type) (P Q : X -> Prop),
(exists x, P x \/ Q x) <-> (exists x, P x) \/ (exists x, Q x).
Proof.
intros X P Q. split.
- (* -> *) intros H. destruct H as [X' H]. destruct H as [HP | HQ].
+ left. exists X'. apply HP.
+ right. exists X'. apply HQ.
- (* <- *) intros H. destruct H as [HP | HQ].
+ destruct HP as [X' HP]. exists X'. left. apply HP.
+ destruct HQ as [X' HQ]. exists X'. right. apply HQ.
Qed.
(* ################################################################# *)
(** * Programming with Propositions *)
(** The logical connectives that we have seen provide a rich
vocabulary for defining complex propositions from simpler ones.
To illustrate, let's look at how to express the claim that an
element [x] occurs in a list [l]. Notice that this property has a
simple recursive structure:
- If [l] is the empty list, then [x] cannot occur in it, so the
property "[x] appears in [l]" is simply false.
- Otherwise, [l] has the form [x' :: l']. In this case, [x]
occurs in [l] if either it is equal to [x'] or it occurs in
[l']. *)
(** We can translate this directly into a straightforward recursive
function taking an element and a list and returning a proposition (!): *)
Fixpoint In {A : Type} (x : A) (l : list A) : Prop :=
match l with
| [] => False
| x' :: l' => x' = x \/ In x l'
end.
(** When [In] is applied to a concrete list, it expands into a
concrete sequence of nested disjunctions. *)
Example In_example_1 : In 4 [1; 2; 3; 4; 5].
Proof.
(* WORKED IN CLASS *)
simpl. right. right. right. left. reflexivity.
Qed.
Example In_example_2 :
forall n, In n [2; 4] ->
exists n', n = 2 * n'.
Proof.
(* WORKED IN CLASS *)
simpl.
intros n [H | [H | []]].
- exists 1. rewrite <- H. reflexivity.
- exists 2. rewrite <- H. reflexivity.
Qed.
(** (Notice the use of the empty pattern to discharge the last case
_en passant_.) *)
(** We can also prove more generic, higher-level lemmas about [In].
Note, in the first, how [In] starts out applied to a variable and
only gets expanded when we do case analysis on this variable: *)
Theorem In_map :
forall (A B : Type) (f : A -> B) (l : list A) (x : A),
In x l ->
In (f x) (map f l).
Proof.
intros A B f l x.
induction l as [|x' l' IHl'].
- (* l = nil, contradiction *)
simpl. intros [].
- (* l = x' :: l' *)
simpl. intros [H | H].
+ rewrite H. left. reflexivity.
+ right. apply IHl'. apply H.
Qed.
(** This way of defining propositions recursively, though convenient
in some cases, also has some drawbacks. In particular, it is
subject to Coq's usual restrictions regarding the definition of
recursive functions, e.g., the requirement that they be "obviously
terminating." In the next chapter, we will see how to define
propositions _inductively_, a different technique with its own set
of strengths and limitations. *)
(** **** Exercise: 2 stars (In_map_iff) *)
Lemma In_map_iff :
forall (A B : Type) (f : A -> B) (l : list A) (y : B),
In y (map f l) <->
exists x, f x = y /\ In x l.
Proof.
intros A B f l y. split.
- (* -> *) intros H. induction l as [| x' l' IHl'].
+ (* l = [] *)
simpl in H. contradiction.
+ (* l = x' :: l' *)
simpl in H. destruct H as [H1 | H2].
{ exists x'. split.
- apply H1.
- simpl. left. reflexivity. }
{ apply IHl' in H2. destruct H2 as [x2 H2]. exists x2. split.
- apply proj1 in H2. apply H2.
- simpl. right. apply proj2 in H2. apply H2. }
- (* <- *) intros H. induction l as [| x' l' IHl'].
+ (* l = [] *)
simpl in H. destruct H as [x' H]. apply proj2 in H. contradiction.
+ (* l = x' :: l' *)
simpl. simpl in H. destruct H as [x'' H]. inversion H. destruct H1 as [H2 | H3].
{ left. rewrite H2. apply H0. }
{ right. apply IHl'. exists x''. split.
- apply H0.
- apply H3. }
Qed.
(** **** Exercise: 2 stars (in_app_iff) *)
Lemma in_app_iff : forall A l l' (a:A),
In a (l++l') <-> In a l \/ In a l'.
Proof.
intros A l l' x. split.
- intros H. induction l.
+ simpl. simpl in H. right. apply H.
+ simpl. simpl in H. destruct H.
{ left. left. apply H. }
{ apply IHl in H. apply or_assoc. right. apply H. }
- intros H. induction l.
+ simpl. simpl in H. destruct H.
{ contradiction. }
{ apply H. }
+ simpl. simpl in H. apply or_assoc in H. destruct H as [H1 | [H2 | H3]].
{ left. apply H1. }
{ right. apply IHl. left. apply H2. }
{ right. apply IHl. right. apply H3. }
Qed.
(** **** Exercise: 3 stars (All) *)
(** Recall that functions returning propositions can be seen as
_properties_ of their arguments. For instance, if [P] has type
[nat -> Prop], then [P n] states that property [P] holds of [n].
Drawing inspiration from [In], write a recursive function [All]
stating that some property [P] holds of all elements of a list
[l]. To make sure your definition is correct, prove the [All_In]
lemma below. (Of course, your definition should _not_ just
restate the left-hand side of [All_In].) *)
Fixpoint All {T} (P : T -> Prop) (l : list T) : Prop :=
match l with
| [] => True
| h :: t => P h /\ All P t
end.
Lemma All_In : forall T (P : T -> Prop) (l : list T),
(forall x, In x l -> P x) <->
All P l.
Proof.
intros T P l. split.
- (* -> *) intros H. induction l as [| n l' IHl'].
+ (* l = nil *) simpl. auto.
+ (* l = n :: l' *) simpl. split.
(* left *) { apply H. simpl. left. reflexivity. }
(* right *) { apply IHl'. intros x0 H0. apply H. simpl. right. apply H0. }
- (* <- *) intros H. induction l.
+ (* l = nil *) simpl. intros x0 H0. contradiction.
+ (* l = cons n l' *) simpl. intros x0 H0. destruct H0 as [|H1 H2].
(* left *) { simpl in H. apply proj1 in H. rewrite H0 in H. apply H. }
(* right *) { simpl in H. apply proj2 in H. apply IHl with x0 in H. apply H. apply H1. }
Qed.
(** **** Exercise: 3 stars (combine_odd_even) *)
(** Complete the definition of the [combine_odd_even] function below.
It takes as arguments two properties of numbers, [Podd] and
[Peven], and it should return a property [P] such that [P n] is
equivalent to [Podd n] when [n] is odd and equivalent to [Peven n]
otherwise. *)
Definition combine_odd_even (Podd Peven : nat -> Prop) : nat -> Prop :=
fun (n : nat) => if oddb n then Podd n else Peven n.
(** To test your definition, prove the following facts: *)
Theorem combine_odd_even_intro :
forall (Podd Peven : nat -> Prop) (n : nat),
(oddb n = true -> Podd n) ->
(oddb n = false -> Peven n) ->
combine_odd_even Podd Peven n.
Proof.
intros Podd Peven n Hodd Heven.
unfold combine_odd_even. destruct (oddb n) eqn: H.
- apply Hodd. reflexivity.
- apply Heven. reflexivity.
Qed.
Theorem combine_odd_even_elim_odd :
forall (Podd Peven : nat -> Prop) (n : nat),
combine_odd_even Podd Peven n ->
oddb n = true ->
Podd n.
Proof.
intros Podd Peven n Hcomb Hodd.
unfold combine_odd_even in Hcomb.
rewrite Hodd in Hcomb. assumption.
Qed.
Theorem combine_odd_even_elim_even :
forall (Podd Peven : nat -> Prop) (n : nat),
combine_odd_even Podd Peven n ->
oddb n = false ->
Peven n.
Proof.
intros Podd Peven n Hcomb Heven.
unfold combine_odd_even in Hcomb.
rewrite Heven in Hcomb. assumption.
Qed.
(* ################################################################# *)
(** * Applying Theorems to Arguments *)
(** One feature that distinguishes Coq from some other popular
proof assistants (e.g., ACL2 and Isabelle) is that it treats
_proofs_ as first-class objects.
There is a great deal to be said about this, but it is not
necessary to understand it all in detail in order to use Coq.
This section gives just a taste, while a deeper exploration can be
found in the optional chapters [ProofObjects] and
[IndPrinciples]. *)
(** We have seen that we can use [Check] to ask Coq to print the type
of an expression. We can also use it to ask what theorem a
particular identifier refers to. *)
Check plus_comm.
(** Coq checks the _statement_ of the [plus_comm] theorem (or prints
it for us, if we leave off the part beginning with the colon) in
the same way that it checks the _type_ of any term that we ask it
to [Check]. Why? *)
(** The reason is that the identifier [plus_comm] actually refers to a
_proof object_, which represents a logical derivation establishing
of the truth of the statement [forall n m : nat, n + m = m + n]. The
type of this object is the proposition which it is a proof of. *)
(** Intuitively, this makes sense because the statement of a
theorem tells us what we can use that theorem for, just as the
type of a "computational" object tells us what we can do with that
object -- e.g., if we have a term of type [nat -> nat -> nat], we
can give it two [nat]s as arguments and get a [nat] back.
Similarly, if we have an object of type [n = m -> n + n = m + m]
and we provide it an "argument" of type [n = m], we can derive
[n + n = m + m]. *)
(** Operationally, this analogy goes even further: by applying a
theorem as if it were a function, i.e., applying it to hypotheses
with matching types, we can specialize its result without having
to resort to intermediate assertions. For example, suppose we
wanted to prove the following result: *)
Lemma plus_comm3 :
forall x y z, x + (y + z) = (z + y) + x.
(** It appears at first sight that we ought to be able to prove this
by rewriting with [plus_comm] twice to make the two sides match.
The problem, however, is that the second [rewrite] will undo the
effect of the first. *)
Proof.
(* WORKED IN CLASS *)
intros x y z.
rewrite plus_comm.
rewrite plus_comm.