-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logic.thy
1967 lines (1587 loc) · 59.2 KB
/
Logic.thy
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
theory Logic
imports Main Ind begin
(* (** * Logic: Logic in Coq *)
(* Version of 4/8/2010 *)
Require Export Ind.
(** Like its built-in programming language, Coq's built-in logic
is extremely small: universal quantification ([forall]) and
implication ([->]) are primitive, but all the other familiar
logical connectives -- conjunction, disjunction, negation,
existential quantification, even equality -- can be defined using
just these together with the [Inductive] definition facility. *)
(* ########################################################### *)
(** * Quantification and Implication *)
(** In fact, [->] and [forall] are the _same_ primitive! *)
(** Coq's [->] notation is actually just a shorthand for [forall].
The [forall] notation is more general, because it allows us to
_name_ the hypothesis. For example, consider this proposition: *)
*)
(* CH: Okay, so this is totally not true in Isabelle. Forall and the function
space are different constructions. Why? Because Isabelle is,
basically, interpreted in set theory. *)
(*
Definition funny_prop1 := forall n, forall (E : ev n), ev (n+4).
(** If we had a proof term inhabiting this proposition, it would be a
function with two arguments: a number [n] and some evidence that
[n] is even. But the name [E] for this evidence is not used in
the rest of the statement of [funny_prop1], so it's a bit silly to
bother making up a name. We could write it like this instead: *)
Definition funny_prop1' := forall n, forall (_ : ev n), ev (n+4).
(** Or we can write it in more familiar notation: *)
Definition funny_prop1'' := forall n, ev n -> ev (n+4).
(** This illustrates that "[P -> Q]" is just syntactic sugar for
"[forall (_:P), Q]". *)
(* ########################################################### *)
(** * Conjunction *)
(** The logical conjunction of propositions [P] and [Q] is
represented by the following inductively defined
proposition. *)
Inductive and (P Q : Prop) : Prop :=
conj : P -> Q -> (and P Q).
*)
(* CH: I believe this is, essentially, true in Isabelle given that the object
level "Prop" is bool. *)
(*
(** Note that, like the definition of [ev] in the previous
chapter, this definition is parameterized; however, in this case,
the parameters are themselves propositions. *)
(** The intuition behind this definition is simple: to
construct evidence for [and P Q], we must provide evidence
for [P] and evidence for [Q]. More precisely:
- [conj p q] can be taken as evidence for [and P Q] if [p]
is evidence for [P] and [q] is evidence for [Q]; and
- this is the _only_ way to give evidence for [and P Q] --
that is, if someone gives us evidence for [and P Q], we
know it must have the form [conj p q], where [p] is
evidence for [P] and [q] is evidence for [Q].
Since we'll be using conjunction a lot, let's introduce a more
familiar-looking infix notation for it. *)
Notation "P /\ Q" := (and P Q) : type_scope.
(** (The [type_scope] annotation tells Coq that this notation
will be appearing in propositions, not values.) *)
(** Consider the "type" of the constructor [conj]: *)
Check conj.
(* ====> forall P Q : Prop, P -> Q -> P /\ Q *)
(** Notice that it takes 4 inputs, namely the propositions [P],[Q] and
evidence of [P], [Q], and returns as output the evidence of [P /\ Q]. *)
(** Besides the elegance of building everything up from a tiny
foundation, what's nice about defining conjunction this way is
that we can prove statements involving conjunction using the
tactics that we already know. For example, if the goal statement
is a conjuction, we can prove it by applying the single
constructor [conj], which (as can be seen from the type of [conj])
solves the current goal and leaves the two parts of the
conjunction as subgoals to be proved separately. *)
Theorem and_example :
(ev 0) /\ (ev 4).
Proof.
apply conj.
(* Case "left". *) apply ev_0.
(* Case "right". *) apply ev_SS. apply ev_SS. apply ev_0. Qed.
*)
(* CH: Isabelle's emphasis is on introduction and elimination rules. *)
theorem "(ev 0) \<and> (ev 4)"
apply (rule conjI)
apply (rule ev_0)
apply (simp del: refl)
by (rule ev_SS, rule ev_SS, rule ev_0)
theorem "(ev 0) \<and> (ev 4)"
proof (rule conjI)
show "ev 0" by (rule ev_0)
next
show "ev 4" by auto
qed
(*
(** Let's take a look at the proof object for the above theorem. *)
Print and_example.
(* ====> conj (ev 0) (ev 4) ev_0 (ev_SS 2 (ev_SS 0 ev_0))
: ev 0 /\ ev 4 *)
(** Note that the proof is of the form
[[
conj (ev 0) (ev 4) (...pf of ev 0...) (...pf of ev 4...)
]]
which is what you'd expect, given the type of [conj]. *)
(** **** Exercise: 1 star, optional *)
(** The [Case] tactics were commented out in the proof of
[and_example] to avoid cluttering the proof object. What would
you guess the proof object will look like if we uncomment them?
Try it and see. *)
(** [] *)
(** Just for convenience, we can also use [split] as a shorthand for
[apply conj]. *)
Theorem and_example' :
(ev 0) /\ (ev 4).
Proof.
split.
Case "left". apply ev_0.
Case "right". apply ev_SS. apply ev_SS. apply ev_0. Qed.
*)
(* CH: So there isn't anything quite like split in Isabelle, but you do
have some fun options for automating the use of introduction rules!
You can give auto the keyword intro: <list of intro rules>
or you can specifically do apply (intro <list of intro rules>)
to apply as many as possible *)
(*
(** Conversely, the [inversion] tactic can be used to take a
conjunction hypothesis in the context, calculate what evidence
must have been used to build it, and put this evidence into the
proof context. *)
Theorem proj1 : forall P Q : Prop,
P /\ Q -> P.
Proof.
intros P Q H.
inversion H as [HP HQ].
apply HP. Qed.
*)
(* CH: this use of inversion is, essentially, the application of elim rules
in Isabelle. Just as we could use intro for introduction rules, we
can use elim for elimination rules*)
theorem "P \<and> Q \<Longrightarrow> P"
apply (elim conjE)
by assumption
(* CH: Equivalently I think you can use the "cases" rule to take apart
a hypothesis as we saw in the previous section. Inversion in Coq
doesn't seem to exactly correspond to a construct in Isabelle. *)
(*
(** **** Exercise: 1 star, optional *)
Theorem proj2 : forall P Q : Prop,
P /\ Q -> Q.
Proof.
intros P Q H.
inversion H as [HP HQ].
apply HQ.
Qed.
(** [] *)
Theorem and_commut : forall P Q : Prop,
P /\ Q -> Q /\ P.
Proof.
(* WORKED IN CLASS *)
intros P Q H.
split.
Case "left". apply proj2 with (P:=P). apply H.
Case "right". apply proj1 with (Q:=Q). apply H. Qed.
*)
theorem "P \<and> Q \<Longrightarrow> Q \<and> P"
apply (intro conjI)
by (elim conjE, simp)+
(* CH: Also, for simple things involving this first order logic manipulation
we can use a tactic called 'blast'. Essentially everything in this file
could be handled by blast outright. *)
theorem "P \<and> Q \<Longrightarrow> Q \<and> P"
by blast
(*
(** **** Exercise: 2 stars *)
(** In the following proof, notice how the _nested pattern_ in the
[inversion] breaks [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 H.
inversion H as [HP [HQ HR]].
split.
split. apply HP. apply HQ.
apply HR.
Qed.
(** [] *)
*)
theorem "P \<and> (Q \<and> R) \<Longrightarrow> (P \<and> Q) \<and> R"
apply (intro conjI)
apply (elim conjE,simp)+
done
theorem "P \<and> (Q \<and> R) \<Longrightarrow> (P \<and> Q) \<and> R"
by auto
(*
(** **** Exercise: 2 stars *)
(** Now we can prove the other direction of the equivalence of [even]
and [ev], which we left hanging in the last chapter. Notice that
the left-hand conjunct here is the statement we are actually
interested in; the right-hand conjunct is needed in order to make
the induction hypothesis strong enough that we can carry out the
reasoning in the inductive step. (To see why this is needed, try
proving the left conjunct by itself and observe where things get
stuck.) *)
Theorem even_ev : forall n : nat,
(even n -> ev n) /\ (even (S n) -> ev (S n)).
Proof.
(* Hint: Use induction on [n]. *)
intros n.
induction n.
split.
intros H.
apply ev_0.
intros H.
inversion H.
split.
inversion IHn.
apply H0.
intros H.
inversion H.
apply ev_SS.
inversion IHn.
apply H0.
unfold even. apply H1.
Qed.
(** [] *)
*)
primrec even :: "nat \<Rightarrow> bool" where
"even 0 = True" |
"even (Suc n) = (\<not> (even n))"
lemma ev_not_ev : "ev n \<Longrightarrow> \<not> (ev (Suc n))"
apply (induct n set: ev)
apply (rule notI)
apply (erule ev.cases)
apply simp+
apply (intro notI)
apply (erule notE)
apply (erule_tac a="(Suc (Suc (Suc n)))" in ev.cases)
by simp_all
lemma "ev n \<Longrightarrow> \<not> ev (Suc n)"
proof (induct n set: ev)
case ev_0
show ?case apply -
apply (rule notI)
apply (erule ev.cases)
by auto
case (ev_SS i)
assume H1 : "\<not> ev (Suc i)"
have L1:"ev (Suc (Suc (Suc i))) \<Longrightarrow> ev (Suc i)" apply -
apply (erule ev.cases)
by (auto intro: ev.intros)
show ?case using L1 H1 by (auto intro: ev.intros)
qed
(* CH: I think the above is a lot more idiomatic than the
apply script. It's a little verbose, but I think
the reasoning is pretty clear *)
thm contrapos_np [OF ev_not_ev]
(*
lemma "(even n \<longrightarrow> ev n) \<and> (even (Suc n) \<longrightarrow> ev (Suc n))"
proof (induct n)
case 0
show ?case proof (intro conjI impI)
show "even 0 \<Longrightarrow> ev 0" by (rule ev_0)
next
show "even (Suc 0) \<Longrightarrow> ev (Suc 0)" by auto
qed
next
case (Suc i)
show ?case using Suc(1)
proof (intro conjI impI)
qed
*)
theorem "(even n \<longrightarrow> ev n) \<and> (even (Suc n) \<longrightarrow> ev (Suc n))"
apply (induct_tac n)
apply (intro conjI impI)
apply (rule ev_0)
apply simp
apply (intro conjI impI)
apply simp
apply simp
apply (rule ev_SS)
apply simp
done
theorem "(even n \<longrightarrow> ev n) \<and> (even (Suc n) \<longrightarrow> ev (Suc n))"
apply (induct_tac n)
by auto
(*
(* ###################################################### *)
(** ** Iff *)
(** The familiar logical "if and only if" is just the
conjunction of two implications. *)
Definition iff (P Q : Prop) := (P -> Q) /\ (Q -> P).
Notation "P <-> Q" := (iff P Q) (at level 95, no associativity) : type_scope.
Theorem iff_implies : forall P Q : Prop,
(P <-> Q) -> P -> Q.
Proof.
intros P Q H.
inversion H as [HAB HBA]. apply HAB. Qed.
*)
theorem "P\<longleftrightarrow>Q \<Longrightarrow> P \<longrightarrow> Q"
apply (elim iffE)
by assumption
(* CH: IFF is a bit different in Isabelle as I don't believe it is fundamentally an inductive construction that is invertable the way it is in Coq *)
(*
Theorem iff_sym : forall P Q : Prop,
(P <-> Q) -> (Q <-> P).
Proof.
(* WORKED IN CLASS *)
intros P Q H.
inversion H as [HAB HBA].
split.
Case "->". apply HBA.
Case "<-". apply HAB. Qed.
*)
theorem "(P \<longleftrightarrow> Q) \<Longrightarrow> (Q \<longleftrightarrow> P)"
apply (intro iffI)
by (erule iffE, erule mp, assumption)+
(*
(** **** Exercise: 1 star (iff_properties) *)
(** Using the above proof that [<->] is symmetric ([iff_sym]) as
a guide, prove that it is also reflexive and transitive. *)
Theorem iff_refl : forall P : Prop,
P <-> P.
Proof.
intros P.
split; intros H; apply H.
Qed.
*)
lemma "P \<longleftrightarrow> P"
apply (rule iffI)
by assumption+
(*
(** Hint: If you have an iff hypothesis in the context, you
can use [inversion] to break it into two separate
implications. (Think about why this works.) *)
Theorem iff_trans : forall P Q R : Prop,
(P <-> Q) -> (Q <-> R) -> (P <-> R).
Proof.
intros P Q R H1 H2.
split; inversion H1 as [PQ QP] ; inversion H2 as [QR RQ].
intros HP.
apply QR. apply PQ. apply HP.
intros HR.
apply QP. apply RQ. apply HR.
Qed.
*)
theorem "\<lbrakk>(P \<longleftrightarrow> Q);(Q \<longleftrightarrow> R)\<rbrakk> \<Longrightarrow> (P \<longleftrightarrow> R)"
by auto
theorem "\<lbrakk>(P \<longleftrightarrow> Q);(Q \<longleftrightarrow> R)\<rbrakk> \<Longrightarrow> (P \<longleftrightarrow> R)"
apply (rule iffI)
apply (erule iffE)+
apply (erule mp)
apply (erule mp)
apply assumption
apply (erule iffE)+
apply (erule mp)
apply (erule (1) mp)
done
(*
(** [] *)
(** **** Exercise: 2 stars *)
(** We have seen that the families of propositions [MyProp] and [ev]
actually characterize the same set of numbers (the even ones).
Prove that [MyProp n <-> ev n] for all [n]. Just for fun, write
your proof as an explicit proof object, rather than using
tactics. (_Hint_: you should only need a single line!) *)
Check (fun n => conj (MyProp n -> ev n) (ev n -> MyProp n) (ev_MyProp n) (MyProp_ev n)).
Definition MyProp_iff_ev : forall n, MyProp n <-> ev n :=
(fun n => conj (MyProp n -> ev n) (ev n -> MyProp n) (ev_MyProp n) (MyProp_ev n)).
(** [] *)
(** In traditional Coq, propositions phrased with [<->] were a bit
inconvenient to use as hypotheses or lemmas, because they had to
be deconstructed into their two directed components in order to be
applied. Starting in Coq 8.2, using [apply] with an iff proposition
appears to do an implicit [inversion] first, so does the right thing
if _either_ direction is relevant. Unfortunately, this feature isn't
properly documented, though. In the rest of the book, we'll just avoid
using [iff] very much. *)
(* ############################################################ *)
(** * Disjunction *)
(** Disjunction ("logical or") can also be defined as an
inductive proposition. *)
Inductive or (P Q : Prop) : Prop :=
| or_introl : P -> or P Q
| or_intror : Q -> or P Q.
*)
(* CH: In Isabelle, the equivalent is *)
(*
Notation "P \/ Q" := (or P Q) : type_scope.
(** Consider the "type" of the constructor [or_introl]: *)
Check or_introl.
(* ===> forall P Q : Prop, P -> P \/ Q *)
(** It takes 3 inputs, namely the propositions [P],[Q] and
evidence of [P], and returns as output, the evidence of [P /\ Q].
Next, look at the type of [or_intror]: *)
Check or_intror.
(* ===> forall P Q : Prop, Q -> P \/ Q *)
(** It is like [or_introl] but it requires evidence of [Q]
instead of evidence of [P]. *)
(** Intuitively, there are two ways of giving evidence for [P \/ Q]:
- give evidence for [P] (and say that it is [P] you are giving
evidence for! -- this is the function of the [or_introl]
constructor), or
- give evidence for [Q], tagged with the [or_intror]
constructor.
Since [P \/ Q] has two constructors, doing [inversion] on a
hypothesis of type [P \/ Q] yields two subgoals. *)
Theorem or_commut : forall P Q : Prop,
P \/ Q -> Q \/ P.
Proof.
intros P Q H.
inversion H as [HP | HQ].
Case "left". apply or_intror. apply HP.
Case "right". apply or_introl. apply HQ. Qed.
*)
theorem "P \<or> Q \<Longrightarrow> Q \<or> P"
apply (erule disjE)
apply (rule disjI2, assumption)
apply (rule disjI1, assumption)
done
(*
(** From here on, we'll use the shorthand tactics [left] and [right]
in place of [apply or_introl] and [apply or_intror]. *)
Theorem or_commut' : forall P Q : Prop,
P \/ Q -> Q \/ P.
Proof.
intros P Q H.
inversion H as [HP | HQ].
Case "left". right. apply HP.
Case "right". left. apply HQ. Qed.
Theorem or_distributes_over_and_1 : forall P Q R : Prop,
P \/ (Q /\ R) -> (P \/ Q) /\ (P \/ R).
Proof.
intros P Q R. intros H. inversion H as [HP | [HQ HR]].
Case "left". split.
SCase "left". left. apply HP.
SCase "right". left. apply HP.
Case "right". split.
SCase "left". right. apply HQ.
SCase "right". right. apply HR. Qed.
(** **** Exercise: 2 stars *)
Theorem or_distributes_over_and_2 : forall P Q R : Prop,
(P \/ Q) /\ (P \/ R) -> P \/ (Q /\ R).
Proof.
intros P Q R H.
inversion H as [H1 H2]; inversion H1 as [HP1 | HQ]; inversion H2 as [HP2 | HR].
left. apply HP1.
left. apply HP1.
left. apply HP2.
right. split; apply (HQ , HR).
Qed.
(** [] *)
(** **** Exercise: 1 star *)
Theorem or_distributes_over_and : forall P Q R : Prop,
P \/ (Q /\ R) <-> (P \/ Q) /\ (P \/ R).
Proof.
(* FILL IN HERE *) Admitted.
(** [] *)
(* ################################################### *)
(** ** Digression: Induction Principles for [/\] and [\/] *)
(** The induction principles for conjunction and disjunction are a
good illustration of Coq's way of generating simplified induction
principles for [Inductive]ly defined propositions, which we
discussed in the last chapter. You try first: *)
(** **** Exercise: 1 star (and_ind_principle) *)
(** See if you can predict the induction principle for conjunction. *)
(* Check and_ind. *)
(** [] *)
(** **** Exercise: 1 star (or_ind_principle) *)
(** See if you can predict the induction principle for disjunction. *)
(* Check or_ind. *)
(** [] *)
Check and_ind.
(** From the inductive definition of the proposition [and P Q]
[[
Inductive and (P Q : Prop) : Prop :=
conj : P -> Q -> (and P Q).
]]
we might expect Coq to generate this induction principle
[[
and_ind_max :
forall (P Q : Prop) (P0 : P /\ Q -> Prop),
(forall (a : P) (b : Q), P0 (conj P Q a b)) ->
forall a : P /\ Q, P0 a
]]
but actually it generates this simpler and more useful one:
[[
and_ind :
forall P Q P0 : Prop,
(P -> Q -> P0) ->
P /\ Q -> P0
]]
In the same way, when given the inductive definition of [or P Q]
[[
Inductive or (P Q : Prop) : Prop :=
| or_introl : P -> or P Q
| or_intror : Q -> or P Q.
]]
instead of the "maximal induction principle"
[[
or_ind_max :
forall (P Q : Prop) (P0 : P \/ Q -> Prop),
(forall a : P, P0 (or_introl P Q a)) ->
(forall b : Q, P0 (or_intror P Q b)) ->
forall o : P \/ Q, P0 o
]]
what Coq actually generates is this:
[[
or_ind :
forall P Q P0 : Prop,
(P -> P0) ->
(Q -> P0) ->
P \/ Q -> P0
]]
*)
(* ################################################### *)
(** ** Relating /\ and \/ with andb and orb *)
(** We've already seen several places where analogous structures
can be found in Coq's computational ([Type]) and logical ([Prop])
worlds. Here is one more: the boolean operators [andb] and [orb]
are obviously analogs, in some sense, of the logical connectives
[/\] and [\/]. This analogy can be made more precise by the
following theorems, which show how to translate knowledge about
[andb] and [orb]'s behaviors on certain inputs into propositional
facts about those inputs. *)
Theorem andb_true__and : forall b c,
andb b c = true -> b = true /\ c = true.
Proof.
(* WORKED IN CLASS *)
intros b c H.
destruct b.
Case "b = true". destruct c.
SCase "c = true". apply conj. reflexivity. reflexivity.
SCase "c = false". inversion H.
Case "b = false". inversion H. Qed.
Theorem and__andb_true : forall b c,
b = true /\ c = true -> andb b c = true.
Proof.
(* WORKED IN CLASS *)
intros b c H.
inversion H.
rewrite H0. rewrite H1. reflexivity. Qed.
(** **** Exercise: 1 star (bool_prop) *)
Theorem andb_false : forall b c,
andb b c = false -> b = false \/ c = false.
Proof.
intros b c H.
destruct b; destruct c.
inversion H.
right ; reflexivity. left ; reflexivity. left ; reflexivity.
Qed.
Theorem orb_true : forall b c,
orb b c = true -> b = true \/ c = true.
Proof.
intros b c H.
destruct b; destruct c.
left ; reflexivity.
left ; reflexivity.
right ; reflexivity.
inversion H.
Qed.
Theorem orb_false : forall b c,
orb b c = false -> b = false /\ c = false.
Proof.
intros b c H.
destruct b ; destruct c.
inversion H.
inversion H.
inversion H.
split; reflexivity.
Qed.
(** [] *)
(* ################################################### *)
(** * Falsehood *)
(** Logical falsehood can be represented in Coq as an inductively
defined proposition with no constructors. *)
Inductive False : Prop := .
(** Intuition: [False] is a proposition for which there is no way
to give evidence. *)
(** **** Exercise: 1 star (False_ind_principle) *)
(** Can you predict the induction principle for falsehood? *)
(* Check False_ind. *)
(** [] *)
(** Since [False] has no constructors, inverting an assumption
of type [False] always yields zero subgoals, allowing us to
immediately prove any goal. *)
Theorem False_implies_nonsense :
False -> plus 2 2 = 5.
Proof.
intros contra.
inversion contra. Qed.
(** How does this work? The [inversion] tactic breaks [contra] into
each of its possible cases, and yields a subgoal for each case.
As [contra] is evidence for [False], it has _no_ possible cases,
hence, there are no possible subgoals and the proof is done. *)
(** Conversely, the only way to prove [False] is if there is already
something nonsensical or contradictory in the context: *)
Theorem nonsense_implies_False :
plus 2 2 = 5 -> False.
Proof.
intros contra.
inversion contra. Qed.
(** Actually, since the proof of [False_implies_nonsense]
doesn't actually have anything to do with the specific nonsensical
thing being proved; it can easily be generalized to work for an
arbitrary [P]: *)
Theorem ex_falso_quodlibet : forall (P:Prop),
False -> P.
Proof.
intros P contra.
inversion contra. Qed.
(** The Latin _ex falso quodlibet_ means, literally, "from
falsehood follows whatever you please." This theorem is also
known as the _principle of explosion_. *)
(* #################################################### *)
(** ** Truth *)
(** Since we have defined falsehood in Coq, we might wonder whether it
is possible to define truth in the same way. Naturally, the
answer is yes. *)
(** **** Exercise: 2 stars *)
(** Define [True] as another inductively defined proposition. What
induction principle will Coq generate for your definition? (The
intution is that [True] should be a proposition for which it is
trivial to give evidence. Alternatively, you may find it easiest
to start with the induction principle and work backwards to the
inductive definition.) *)
Inductive True : Prop := TT.
(** [] *)
(** However, unlike [False], which we'll use extensively, [True] is
just a theoretical curiosity: it is trivial (and therefore
uninteresting) to prove as a goal, and it carries no useful
information as a hypothesis. *)
(* #################################################### *)
(** * Negation *)
(** The logical complement of a proposition [P] is written [not
P] or, for shorthand, [~P]: *)
Definition not (P:Prop) := P -> False.
Notation "~ x" := (not x) : type_scope.
Check not.
(* ====> Prop -> Prop *)
(** The intuition is that, if [P] is not true, then anything at
all (even [False]) should follow from assuming [P]. *)
(** It takes a little practice to get used to working with
negation in Coq. Even though you can see perfectly well why
something is true, it can be a little hard at first to get things
into the right configuration so that Coq can see it! Here are
proofs of a few familiar facts about negation to get you warmed
up. *)
Theorem not_False :
~ False.
Proof.
unfold not. intros H. inversion H. Qed.
Theorem contradiction_implies_anything : forall P Q : Prop,
(P /\ ~P) -> Q.
Proof.
(* WORKED IN CLASS *)
intros P Q H. inversion H as [HP HNA]. unfold not in HNA.
apply HNA in HP. inversion 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 (double_neg_inf) *)
(** Write an informal proof of [double_neg]:
_Theorem_: [P] implies [~~P], for any proposition [P].
_Proof_:
(* FILL IN HERE *)
[]
*)
(** **** Exercise: 2 stars *)
Theorem contrapositive : forall P Q : Prop,
(P -> Q) -> (~Q -> ~P).
Proof.
intros P Q H.
unfold not.
intros QF HP.
apply QF. apply H. apply HP.
Qed.
(** [] *)
(** **** Exercise: 1 star *)
Theorem not_both_true_and_false : forall P : Prop,
~ (P /\ ~P).
Proof.
intros P.
unfold not.
intros H. inversion H.
apply H1; apply H0.
Qed.
(** [] *)
Theorem five_not_even :
~ ev 5.
Proof.
(* WORKED IN CLASS *)
unfold not. intros Hev5. inversion Hev5 as [|n Hev3 Heqn].
inversion Hev3 as [|n' Hev1 Heqn']. inversion Hev1. Qed.
(** **** Exercise: 1 star *)
(** Theorem [five_not_even] confirms the unsurprising fact that five
is not an even number. Prove this more interesting fact: *)
Theorem ev_not_ev_S : forall n,
ev n -> ~ ev (S n).
Proof.
unfold not. intros n H. induction H. (* not n! *)
intros. inversion H.
intros.
apply IHev.
inversion H0. apply H2.
Qed.
(** [] *)
(** **** Exercise: 1 star, optional (informal_not_PNP) *)
(** Write an informal proof (in English) of the proposition [forall P
: Prop, ~(P /\ ~P)]. *)
(* FILL IN HERE *)
(** [] *)
(** **** Exercise: 5 stars, optional (classical_axioms) *)
(** For those who like a challenge, here is an exercise
taken from the Coq'Art book (p. 123). The following five
statements are often considered as characterizations of
classical logic (as opposed to constructive logic, which is
what is "built in" to Coq). We can't prove them in Coq, but
we can consistently add any one of them as an unproven axiom
if we wish to work in classical logic. Prove that these five
propositions are equivalent. *)
Definition peirce := forall P Q: Prop,
((P->Q)->P)->P.
Definition classic := forall P:Prop,
~~P -> P.
Definition excluded_middle := forall P:Prop,
P \/ ~P.
Definition de_morgan_not_and_not := forall P Q:Prop,
~(~P/\~Q) -> P\/Q.
Definition implies_to_or := forall P Q:Prop,
(P->Q) -> (~P\/Q).
(* FILL IN HERE *)
(** [] *)
(* ########################################################## *)
(** ** Inequality *)
(** Saying [x <> y] is just the same as saying [~(x = y)]. *)
Notation "x <> y" := (~ (x = y)) : type_scope.
(** Since inequality involves a negation, it again requires
a little practice to be able to work with it fluently. Here
is one very useful trick. If you are trying to prove a goal
that is nonsensical (e.g., the goal state is [false = true]),
apply the lemma [ex_falso_quodlibet] to change the goal to
[False]. This makes it easier to use assumptions of the form
[~P] that are available in the context -- in particular,
assumptions of the form [x<>y]. *)
Theorem not_false_then_true : forall b : bool,
b <> false -> b = true.
Proof.
intros b H. destruct b.
Case "b = true". reflexivity.
Case "b = false".
unfold not in H.
apply ex_falso_quodlibet.
apply H. reflexivity. Qed.
(** **** Exercise: 2 stars *)
Theorem not_eq_beq_false : forall n n' : nat,
n <> n' ->
beq_nat n n' = false.
Proof.
intros n n' H.
Admitted.
(** [] *)
(** **** Exercise: 2 stars, optional *)
Theorem beq_false_not_eq : forall n m,
false = beq_nat n m -> n <> m.
Proof.
(* FILL IN HERE *) Admitted.
(** [] *)
(* ############################################################ *)
(** * Existential Quantification *)
(** Another critical logical connective is _existential
quantification_. Here is one way of defining it. *)
Module ExFirstTry.
Inductive ex : forall X:Type, (X->Prop) -> Prop :=
ex_intro : forall (X:Type) (P: X->Prop),
forall (witness:X),
P witness ->
ex X P.
(** That is, [ex] is a family of propositions indexed by a type [X]
and a property [P] over [X]. In order to give evidence for the
assertion "there exists an [x] for which the property [P] holds"
we must actually name a _witness_ -- a specific value [x] -- and
then give evidence for [P x], i.e., evidence that [x] has the
property [P].
For example, consider this existentially quantified proposition: *)
Definition some_nat_is_even : Prop :=
ex nat ev.
(** To prove this proposition, we need to choose a particular number
as witness -- say, 4 -- and give some evidence that that number is
even. *)
Definition snie : some_nat_is_even :=
ex_intro _ ev 4 (ev_SS 2 (ev_SS 0 ev_0)).
(** However, this is not actually the best way to define [ex]: if we
do it this way, Coq will generate the following rather complex
induction axiom: *)
Check ex_ind.
(* ===> forall Q : (forall X : Type, (X->Prop) -> Prop),
(forall (X : Type) (P : X->Prop) (witness : X),
P witness -> Q X P) ->
forall (X : Type) (P : X->Prop), ex X P -> Q X P
(modulo a little renaming...) *)
(** The reason this happened is that Coq uses the part of the
[Inductive] declaration between the [:] and the [:=] as the
argument type [T] for the proposition [P] in the induction
principle "for all properties [P] over things of type [T], if
...(some clause for each constructor)... then [P] holds for
everything of type [T]." *)
End ExFirstTry.
(** A much simpler induction axiom is generated if we rearrange the
definition so that the parameters [X] and [P] are introduced at
the very outside, rather than with the [ex_intro] constructor. *)