-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logic.html
1194 lines (912 loc) · 78.8 KB
/
Logic.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="coqdoc.css" rel="stylesheet" type="text/css"/>
<title>Logic: Logic in Coq</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="page">
<div id="header">
</div>
<div id="main">
<h1 class="libtitle">Logic<span class="subtitle">Logic in Coq</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <span class="id" type="var">MoreCoq</span>.<br/>
<br/>
</div>
<div class="doc">
Coq's built-in logic is very small: the only primitives are
<span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> definitions, universal quantification (<span class="inlinecode"><span style="font-family: arial;">∀</span></span>), and
implication (<span class="inlinecode"><span style="font-family: arial;">→</span></span>), while all the other familiar logical
connectives — conjunction, disjunction, negation, existential
quantification, even equality — can be encoded using just these.
<div class="paragraph"> </div>
This chapter explains the encodings and shows how the tactics
we've seen can be used to carry out standard forms of logical
reasoning involving these connectives.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab191"></a><h1 class="section">Propositions</h1>
<div class="paragraph"> </div>
In previous chapters, we have seen many examples of factual
claims (<i>propositions</i>) and ways of presenting evidence of their
truth (<i>proofs</i>). In particular, we have worked extensively with
<i>equality propositions</i> of the form <span class="inlinecode"><span class="id" type="var">e1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">e2</span></span>, with
implications (<span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span>), and with quantified propositions
(<span class="inlinecode"><span style="font-family: arial;">∀</span></span> <span class="inlinecode"><span class="id" type="var">x</span>,</span> <span class="inlinecode"><span class="id" type="var">P</span></span>).
<div class="paragraph"> </div>
In Coq, the type of things that can (potentially)
be proven is <span class="inlinecode"><span class="id" type="keyword">Prop</span></span>.
<div class="paragraph"> </div>
Here is an example of a provable proposition:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> (3 = 3).<br/>
<span class="comment">(* ===> Prop *)</span><br/>
<br/>
</div>
<div class="doc">
Here is an example of an unprovable proposition:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> (<span style="font-family: arial;">∀</span>(<span class="id" type="var">n</span>:<span class="id" type="var">nat</span>), <span class="id" type="var">n</span> = 2).<br/>
<span class="comment">(* ===> Prop *)</span><br/>
<br/>
</div>
<div class="doc">
Recall that <span class="inlinecode"><span class="id" type="keyword">Check</span></span> asks Coq to tell us the type of the indicated
expression.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab192"></a><h1 class="section">Proofs and Evidence</h1>
<div class="paragraph"> </div>
In Coq, propositions have the same status as other types, such as
<span class="inlinecode"><span class="id" type="var">nat</span></span>. Just as the natural numbers <span class="inlinecode">0</span>, <span class="inlinecode">1</span>, <span class="inlinecode">2</span>, etc. inhabit
the type <span class="inlinecode"><span class="id" type="var">nat</span></span>, a Coq proposition <span class="inlinecode"><span class="id" type="var">P</span></span> is inhabited by its
<i>proofs</i>. We will refer to such inhabitants as <i>proof term</i> or
<i>proof object</i> or <i>evidence</i> for the truth of <span class="inlinecode"><span class="id" type="var">P</span></span>.
<div class="paragraph"> </div>
In Coq, when we state and then prove a lemma such as:
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">silly</span> : 0 × 3 = 0.<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
the tactics we use within the <span class="inlinecode"><span class="id" type="keyword">Proof</span></span>...<span class="inlinecode"><span class="id" type="keyword">Qed</span></span> keywords tell Coq
how to construct a proof term that inhabits the proposition. In
this case, the proposition <span class="inlinecode">0</span> <span class="inlinecode">×</span> <span class="inlinecode">3</span> <span class="inlinecode">=</span> <span class="inlinecode">0</span> is justified by a
combination of the <i>definition</i> of <span class="inlinecode"><span class="id" type="var">mult</span></span>, which says that <span class="inlinecode">0</span> <span class="inlinecode">×</span> <span class="inlinecode">3</span>
<i>simplifies</i> to just <span class="inlinecode">0</span>, and the <i>reflexive</i> principle of
equality, which says that <span class="inlinecode">0</span> <span class="inlinecode">=</span> <span class="inlinecode">0</span>.
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<a name="lab193"></a><h3 class="section"> </h3>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">silly</span> : 0 × 3 = 0.<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
We can see which proof term Coq constructs for a given Lemma by
using the <span class="inlinecode"><span class="id" type="keyword">Print</span></span> directive:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Print</span> <span class="id" type="var">silly</span>.<br/>
<span class="comment">(* ===> silly = eq_refl : 0 * 3 = 0 *)</span><br/>
<br/>
</div>
<div class="doc">
Here, the <span class="inlinecode"><span class="id" type="var">eq_refl</span></span> proof term witnesses the equality. (More on equality later!)
<div class="paragraph"> </div>
<a name="lab194"></a><h2 class="section">Implications <i>are</i> functions</h2>
<div class="paragraph"> </div>
Just as we can implement natural number multiplication as a
function:
<div class="paragraph"> </div>
<span class="inlinecode"></span>
<span class="inlinecode"><span class="id" type="var">mult</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">nat</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">nat</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">nat</span></span>
<span class="inlinecode"></span>
<div class="paragraph"> </div>
The <i>proof term</i> for an implication <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> is a <i>function</i> that takes evidence for <span class="inlinecode"><span class="id" type="var">P</span></span> as input and produces evidence for <span class="inlinecode"><span class="id" type="var">Q</span></span> as its output.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">silly_implication</span> : (1 + 1) = 2 <span style="font-family: arial;">→</span> 0 × 3 = 0.<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
We can see that the proof term for the above lemma is indeed a
function:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Print</span> <span class="id" type="var">silly_implication</span>.<br/>
<span class="comment">(* ===> silly_implication = fun _ : 1 + 1 = 2 => eq_refl<br/>
: 1 + 1 = 2 -> 0 * 3 = 0 *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab195"></a><h2 class="section">Defining Propositions</h2>
<div class="paragraph"> </div>
Just as we can create user-defined inductive types (like the
lists, binary representations of natural numbers, etc., that we
seen before), we can also create <i>user-defined</i> propositions.
<div class="paragraph"> </div>
Question: How do you define the meaning of a proposition?
<div class="paragraph"> </div>
<a name="lab196"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
The meaning of a proposition is given by <i>rules</i> and <i>definitions</i>
that say how to construct <i>evidence</i> for the truth of the
proposition from other evidence.
<div class="paragraph"> </div>
<ul class="doclist">
<li> Typically, rules are defined <i>inductively</i>, just like any other datatype.
<div class="paragraph"> </div>
</li>
<li> Sometimes a proposition is declared to be true without substantiating evidence. Such propositions are called <i>axioms</i>.
</li>
</ul>
<div class="paragraph"> </div>
<div class="paragraph"> </div>
In this, and subsequence chapters, we'll see more about how these
proof terms work in more detail.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab197"></a><h1 class="section">Conjunction (Logical "and")</h1>
<div class="paragraph"> </div>
The logical conjunction of propositions <span class="inlinecode"><span class="id" type="var">P</span></span> and <span class="inlinecode"><span class="id" type="var">Q</span></span> can be
represented using an <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> definition with one
constructor.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">and</span> (<span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
<span class="id" type="var">conj</span> : <span class="id" type="var">P</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> (<span class="id" type="var">and</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span>).<br/>
<br/>
</div>
<div class="doc">
The intuition behind this definition is simple: to
construct evidence for <span class="inlinecode"><span class="id" type="var">and</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span>, we must provide evidence
for <span class="inlinecode"><span class="id" type="var">P</span></span> and evidence for <span class="inlinecode"><span class="id" type="var">Q</span></span>. More precisely:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">conj</span></span> <span class="inlinecode"><span class="id" type="var">p</span></span> <span class="inlinecode"><span class="id" type="var">q</span></span> can be taken as evidence for <span class="inlinecode"><span class="id" type="var">and</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> if <span class="inlinecode"><span class="id" type="var">p</span></span>
is evidence for <span class="inlinecode"><span class="id" type="var">P</span></span> and <span class="inlinecode"><span class="id" type="var">q</span></span> is evidence for <span class="inlinecode"><span class="id" type="var">Q</span></span>; and
<div class="paragraph"> </div>
</li>
<li> this is the <i>only</i> way to give evidence for <span class="inlinecode"><span class="id" type="var">and</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> —
that is, if someone gives us evidence for <span class="inlinecode"><span class="id" type="var">and</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span>, we
know it must have the form <span class="inlinecode"><span class="id" type="var">conj</span></span> <span class="inlinecode"><span class="id" type="var">p</span></span> <span class="inlinecode"><span class="id" type="var">q</span></span>, where <span class="inlinecode"><span class="id" type="var">p</span></span> is
evidence for <span class="inlinecode"><span class="id" type="var">P</span></span> and <span class="inlinecode"><span class="id" type="var">q</span></span> is evidence for <span class="inlinecode"><span class="id" type="var">Q</span></span>.
</li>
</ul>
<div class="paragraph"> </div>
Since we'll be using conjunction a lot, let's introduce a more
familiar-looking infix notation for it.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "P <span style="font-family: arial;">∧</span> Q" := (<span class="id" type="var">and</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span>) : <span class="id" type="var">type_scope</span>.<br/>
<br/>
</div>
<div class="doc">
(The <span class="inlinecode"><span class="id" type="var">type_scope</span></span> annotation tells Coq that this notation
will be appearing in propositions, not values.)
<div class="paragraph"> </div>
Consider the "type" of the constructor <span class="inlinecode"><span class="id" type="var">conj</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">conj</span>.<br/>
<span class="comment">(* ===> forall P Q : Prop, P -> Q -> P /\ Q *)</span><br/>
<br/>
</div>
<div class="doc">
Notice that it takes 4 inputs — namely the propositions <span class="inlinecode"><span class="id" type="var">P</span></span>
and <span class="inlinecode"><span class="id" type="var">Q</span></span> and evidence for <span class="inlinecode"><span class="id" type="var">P</span></span> and <span class="inlinecode"><span class="id" type="var">Q</span></span> — and returns as output the
evidence of <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span style="font-family: arial;">∧</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span>.
<div class="paragraph"> </div>
<a name="lab198"></a><h2 class="section">"Introducing" Conjuctions</h2>
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 <span class="inlinecode"><span class="id" type="var">conj</span></span>, which (as can be seen from the type of <span class="inlinecode"><span class="id" type="var">conj</span></span>)
solves the current goal and leaves the two parts of the
conjunction as subgoals to be proved separately.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">and_example</span> : <br/>
(0 = 0) <span style="font-family: arial;">∧</span> (4 = <span class="id" type="var">mult</span> 2 2).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">conj</span>.<br/>
<span class="id" type="var">Case</span> "left". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "right". <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Just for convenience, we can use the tactic <span class="inlinecode"><span class="id" type="tactic">split</span></span> as a shorthand for
<span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">conj</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">and_example'</span> : <br/>
(0 = 0) <span style="font-family: arial;">∧</span> (4 = <span class="id" type="var">mult</span> 2 2).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">split</span>.<br/>
<span class="id" type="var">Case</span> "left". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "right". <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab199"></a><h2 class="section">"Eliminating" conjunctions</h2>
Conversely, the <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic can be used to take a
conjunction hypothesis in the context, calculate what evidence
must have been used to build it, and add variables representing
this evidence to the proof context.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">proj1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>, <br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> <span class="id" type="var">P</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">HP</span> <span class="id" type="var">HQ</span>].<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">HP</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab200"></a><h4 class="section">Exercise: 1 star, optional (proj2)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">proj2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>, <br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">and_commut</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>, <br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">P</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">HP</span> <span class="id" type="var">HQ</span>].<br/>
<span class="id" type="tactic">split</span>.<br/>
<span class="id" type="var">Case</span> "left". <span class="id" type="tactic">apply</span> <span class="id" type="var">HQ</span>.<br/>
<span class="id" type="var">Case</span> "right". <span class="id" type="tactic">apply</span> <span class="id" type="var">HP</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab201"></a><h4 class="section">Exercise: 2 stars (and_assoc)</h4>
In the following proof, notice how the <i>nested pattern</i> in the
<span class="inlinecode"><span class="id" type="tactic">inversion</span></span> breaks the hypothesis <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span style="font-family: arial;">∧</span></span> <span class="inlinecode">(<span class="id" type="var">Q</span></span> <span class="inlinecode"><span style="font-family: arial;">∧</span></span> <span class="inlinecode"><span class="id" type="var">R</span>)</span> down into
<span class="inlinecode"><span class="id" type="var">HP</span>:</span> <span class="inlinecode"><span class="id" type="var">P</span></span>, <span class="inlinecode"><span class="id" type="var">HQ</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">Q</span></span>, and <span class="inlinecode"><span class="id" type="var">HR</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">R</span></span>. Finish the proof from there:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">and_assoc</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">R</span> : <span class="id" type="keyword">Prop</span>, <br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">∧</span> (<span class="id" type="var">Q</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">R</span>) <span style="font-family: arial;">→</span> (<span class="id" type="var">P</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">Q</span>) <span style="font-family: arial;">∧</span> <span class="id" type="var">R</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">R</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">HP</span> [<span class="id" type="var">HQ</span> <span class="id" type="var">HR</span>]].<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab202"></a><h1 class="section">Iff</h1>
<div class="paragraph"> </div>
The handy "if and only if" connective is just the conjunction of
two implications.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">iff</span> (<span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>) := (<span class="id" type="var">P</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span>) <span style="font-family: arial;">∧</span> (<span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> <span class="id" type="var">P</span>).<br/>
<br/>
<span class="id" type="keyword">Notation</span> "P <span style="font-family: arial;">↔</span> Q" := (<span class="id" type="var">iff</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span>) <br/>
(<span class="id" type="tactic">at</span> <span class="id" type="var">level</span> 95, <span class="id" type="var">no</span> <span class="id" type="var">associativity</span>) <br/>
: <span class="id" type="var">type_scope</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">iff_implies</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>, <br/>
(<span class="id" type="var">P</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">Q</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">P</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">HAB</span> <span class="id" type="var">HBA</span>]. <span class="id" type="tactic">apply</span> <span class="id" type="var">HAB</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">iff_sym</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>, <br/>
(<span class="id" type="var">P</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">Q</span>) <span style="font-family: arial;">→</span> (<span class="id" type="var">Q</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">P</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">HAB</span> <span class="id" type="var">HBA</span>].<br/>
<span class="id" type="tactic">split</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">→</span>". <span class="id" type="tactic">apply</span> <span class="id" type="var">HBA</span>.<br/>
<span class="id" type="var">Case</span> "<span style="font-family: arial;">←</span>". <span class="id" type="tactic">apply</span> <span class="id" type="var">HAB</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab203"></a><h4 class="section">Exercise: 1 star, optional (iff_properties)</h4>
Using the above proof that <span class="inlinecode"><span style="font-family: arial;">↔</span></span> is symmetric (<span class="inlinecode"><span class="id" type="var">iff_sym</span></span>) as
a guide, prove that it is also reflexive and transitive.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">iff_refl</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> : <span class="id" type="keyword">Prop</span>, <br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">P</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">iff_trans</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">R</span> : <span class="id" type="keyword">Prop</span>, <br/>
(<span class="id" type="var">P</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">Q</span>) <span style="font-family: arial;">→</span> (<span class="id" type="var">Q</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">R</span>) <span style="font-family: arial;">→</span> (<span class="id" type="var">P</span> <span style="font-family: arial;">↔</span> <span class="id" type="var">R</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
Hint: If you have an iff hypothesis in the context, you can use
<span class="inlinecode"><span class="id" type="tactic">inversion</span></span> to break it into two separate implications. (Think
about why this works.) <font size=-2>☐</font>
<div class="paragraph"> </div>
Some of Coq's tactics treat <span class="inlinecode"><span class="id" type="var">iff</span></span> statements specially, thus
avoiding the need for some low-level manipulation when reasoning
with them. In particular, <span class="inlinecode"><span class="id" type="tactic">rewrite</span></span> can be used with <span class="inlinecode"><span class="id" type="var">iff</span></span>
statements, not just equalities.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab204"></a><h1 class="section">Disjunction (Logical "or")</h1>
<div class="paragraph"> </div>
<a name="lab205"></a><h2 class="section">Implementing Disjunction</h2>
<div class="paragraph"> </div>
Disjunction ("logical or") can also be defined as an
inductive proposition.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">or</span> (<span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>) : <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">or_introl</span> : <span class="id" type="var">P</span> <span style="font-family: arial;">→</span> <span class="id" type="var">or</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span><br/>
| <span class="id" type="var">or_intror</span> : <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> <span class="id" type="var">or</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span>.<br/>
<br/>
<span class="id" type="keyword">Notation</span> "P <span style="font-family: arial;">∨</span> Q" := (<span class="id" type="var">or</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span>) : <span class="id" type="var">type_scope</span>.<br/>
<br/>
</div>
<div class="doc">
Consider the "type" of the constructor <span class="inlinecode"><span class="id" type="var">or_introl</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">or_introl</span>.<br/>
<span class="comment">(* ===> forall P Q : Prop, P -> P \/ Q *)</span><br/>
<br/>
</div>
<div class="doc">
It takes 3 inputs, namely the propositions <span class="inlinecode"><span class="id" type="var">P</span></span>, <span class="inlinecode"><span class="id" type="var">Q</span></span> and
evidence of <span class="inlinecode"><span class="id" type="var">P</span></span>, and returns, as output, the evidence of <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span style="font-family: arial;">∨</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span>.
Next, look at the type of <span class="inlinecode"><span class="id" type="var">or_intror</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">or_intror</span>.<br/>
<span class="comment">(* ===> forall P Q : Prop, Q -> P \/ Q *)</span><br/>
<br/>
</div>
<div class="doc">
It is like <span class="inlinecode"><span class="id" type="var">or_introl</span></span> but it requires evidence of <span class="inlinecode"><span class="id" type="var">Q</span></span>
instead of evidence of <span class="inlinecode"><span class="id" type="var">P</span></span>.
<div class="paragraph"> </div>
Intuitively, there are two ways of giving evidence for <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span style="font-family: arial;">∨</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span>:
<div class="paragraph"> </div>
<ul class="doclist">
<li> give evidence for <span class="inlinecode"><span class="id" type="var">P</span></span> (and say that it is <span class="inlinecode"><span class="id" type="var">P</span></span> you are giving
evidence for — this is the function of the <span class="inlinecode"><span class="id" type="var">or_introl</span></span>
constructor), or
<div class="paragraph"> </div>
</li>
<li> give evidence for <span class="inlinecode"><span class="id" type="var">Q</span></span>, tagged with the <span class="inlinecode"><span class="id" type="var">or_intror</span></span>
constructor.
</li>
</ul>
<div class="paragraph"> </div>
<a name="lab206"></a><h3 class="section"> </h3>
Since <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span style="font-family: arial;">∨</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> has two constructors, doing <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> on a
hypothesis of type <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span style="font-family: arial;">∨</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> yields two subgoals.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">or_commut</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>,<br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">P</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">HP</span> | <span class="id" type="var">HQ</span>].<br/>
<span class="id" type="var">Case</span> "left". <span class="id" type="tactic">apply</span> <span class="id" type="var">or_intror</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HP</span>.<br/>
<span class="id" type="var">Case</span> "right". <span class="id" type="tactic">apply</span> <span class="id" type="var">or_introl</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HQ</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
From here on, we'll use the shorthand tactics <span class="inlinecode"><span class="id" type="var">left</span></span> and <span class="inlinecode"><span class="id" type="var">right</span></span>
in place of <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">or_introl</span></span> and <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">or_intror</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">or_commut'</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>,<br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">P</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">HP</span> | <span class="id" type="var">HQ</span>].<br/>
<span class="id" type="var">Case</span> "left". <span class="id" type="var">right</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HP</span>.<br/>
<span class="id" type="var">Case</span> "right". <span class="id" type="var">left</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HQ</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">or_distributes_over_and_1</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">R</span> : <span class="id" type="keyword">Prop</span>,<br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> (<span class="id" type="var">Q</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">R</span>) <span style="font-family: arial;">→</span> (<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">Q</span>) <span style="font-family: arial;">∧</span> (<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">R</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">R</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">HP</span> | [<span class="id" type="var">HQ</span> <span class="id" type="var">HR</span>]].<br/>
<span class="id" type="var">Case</span> "left". <span class="id" type="tactic">split</span>.<br/>
<span class="id" type="var">SCase</span> "left". <span class="id" type="var">left</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HP</span>.<br/>
<span class="id" type="var">SCase</span> "right". <span class="id" type="var">left</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HP</span>.<br/>
<span class="id" type="var">Case</span> "right". <span class="id" type="tactic">split</span>.<br/>
<span class="id" type="var">SCase</span> "left". <span class="id" type="var">right</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HQ</span>.<br/>
<span class="id" type="var">SCase</span> "right". <span class="id" type="var">right</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">HR</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab207"></a><h4 class="section">Exercise: 2 stars (or_distributes_over_and_2)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">or_distributes_over_and_2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">R</span> : <span class="id" type="keyword">Prop</span>,<br/>
(<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">Q</span>) <span style="font-family: arial;">∧</span> (<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">R</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> (<span class="id" type="var">Q</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">R</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab208"></a><h4 class="section">Exercise: 1 star, optional (or_distributes_over_and)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">or_distributes_over_and</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">R</span> : <span class="id" type="keyword">Prop</span>,<br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> (<span class="id" type="var">Q</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">R</span>) <span style="font-family: arial;">↔</span> (<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">Q</span>) <span style="font-family: arial;">∧</span> (<span class="id" type="var">P</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">R</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab209"></a><h2 class="section">Relating <span class="inlinecode"><span style="font-family: arial;">∧</span></span> and <span class="inlinecode"><span style="font-family: arial;">∨</span></span> with <span class="inlinecode"><span class="id" type="var">andb</span></span> and <span class="inlinecode"><span class="id" type="var">orb</span></span> (advanced)</h2>
<div class="paragraph"> </div>
We've already seen several places where analogous structures
can be found in Coq's computational (<span class="inlinecode"><span class="id" type="keyword">Type</span></span>) and logical (<span class="inlinecode"><span class="id" type="keyword">Prop</span></span>)
worlds. Here is one more: the boolean operators <span class="inlinecode"><span class="id" type="var">andb</span></span> and <span class="inlinecode"><span class="id" type="var">orb</span></span>
are clearly analogs of the logical connectives <span class="inlinecode"><span style="font-family: arial;">∧</span></span> and <span class="inlinecode"><span style="font-family: arial;">∨</span></span>.
This analogy can be made more precise by the following theorems,
which show how to translate knowledge about <span class="inlinecode"><span class="id" type="var">andb</span></span> and <span class="inlinecode"><span class="id" type="var">orb</span></span>'s
behaviors on certain inputs into propositional facts about those
inputs.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">andb_prop</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">andb</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span> <span class="id" type="var">b</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">c</span> = <span class="id" type="var">true</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">b</span>.<br/>
<span class="id" type="var">Case</span> "b = true". <span class="id" type="tactic">destruct</span> <span class="id" type="var">c</span>.<br/>
<span class="id" type="var">SCase</span> "c = true". <span class="id" type="tactic">apply</span> <span class="id" type="var">conj</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">SCase</span> "c = false". <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Case</span> "b = false". <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">andb_true_intro</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">b</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">c</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span> <span class="id" type="var">andb</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> = <span class="id" type="var">true</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">H0</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">H1</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab210"></a><h4 class="section">Exercise: 2 stars, optional (bool_prop)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">andb_false</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">andb</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> = <span class="id" type="var">false</span> <span style="font-family: arial;">→</span> <span class="id" type="var">b</span> = <span class="id" type="var">false</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">c</span> = <span class="id" type="var">false</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">orb_prop</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">orb</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span> <span class="id" type="var">b</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">c</span> = <span class="id" type="var">true</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">orb_false_elim</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">c</span>,<br/>
<span class="id" type="var">orb</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> = <span class="id" type="var">false</span> <span style="font-family: arial;">→</span> <span class="id" type="var">b</span> = <span class="id" type="var">false</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">c</span> = <span class="id" type="var">false</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab211"></a><h1 class="section">Falsehood</h1>
<div class="paragraph"> </div>
Logical falsehood can be represented in Coq as an inductively
defined proposition with no constructors.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">False</span> : <span class="id" type="keyword">Prop</span> := .<br/>
<br/>
</div>
<div class="doc">
Intuition: <span class="inlinecode"><span class="id" type="var">False</span></span> is a proposition for which there is no way
to give evidence.
<div class="paragraph"> </div>
Since <span class="inlinecode"><span class="id" type="var">False</span></span> has no constructors, inverting an assumption
of type <span class="inlinecode"><span class="id" type="var">False</span></span> always yields zero subgoals, allowing us to
immediately prove any goal.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">False_implies_nonsense</span> :<br/>
<span class="id" type="var">False</span> <span style="font-family: arial;">→</span> 2 + 2 = 5.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">contra</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">contra</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
How does this work? The <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic breaks <span class="inlinecode"><span class="id" type="var">contra</span></span> into
each of its possible cases, and yields a subgoal for each case.
As <span class="inlinecode"><span class="id" type="var">contra</span></span> is evidence for <span class="inlinecode"><span class="id" type="var">False</span></span>, it has <i>no</i> possible cases,
hence, there are no possible subgoals and the proof is done.
<div class="paragraph"> </div>
<a name="lab212"></a><h3 class="section"> </h3>
Conversely, the only way to prove <span class="inlinecode"><span class="id" type="var">False</span></span> is if there is already
something nonsensical or contradictory in the context:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">nonsense_implies_False</span> :<br/>
2 + 2 = 5 <span style="font-family: arial;">→</span> <span class="id" type="var">False</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">contra</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">contra</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Actually, since the proof of <span class="inlinecode"><span class="id" type="var">False_implies_nonsense</span></span>
doesn't actually have anything to do with the specific nonsensical
thing being proved; it can easily be generalized to work for an
arbitrary <span class="inlinecode"><span class="id" type="var">P</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ex_falso_quodlibet</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">P</span>:<span class="id" type="keyword">Prop</span>),<br/>
<span class="id" type="var">False</span> <span style="font-family: arial;">→</span> <span class="id" type="var">P</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">contra</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">contra</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
The Latin <i>ex falso quodlibet</i> means, literally, "from
falsehood follows whatever you please." This theorem is also
known as the <i>principle of explosion</i>.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab213"></a><h2 class="section">Truth</h2>
<div class="paragraph"> </div>
Since we have defined falsehood in Coq, one might wonder whether
it is possible to define truth in the same way. We can.
<div class="paragraph"> </div>
<a name="lab214"></a><h4 class="section">Exercise: 2 stars, advanced (True)</h4>
Define <span class="inlinecode"><span class="id" type="var">True</span></span> as another inductively defined proposition. (The
intution is that <span class="inlinecode"><span class="id" type="var">True</span></span> should be a proposition for which it is
trivial to give evidence.)
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
However, unlike <span class="inlinecode"><span class="id" type="var">False</span></span>, which we'll use extensively, <span class="inlinecode"><span class="id" type="var">True</span></span> is
used fairly rarely. By itself, it is trivial (and therefore
uninteresting) to prove as a goal, and it carries no useful
information as a hypothesis. But it can be useful when defining
complex <span class="inlinecode"><span class="id" type="keyword">Prop</span></span>s using conditionals, or as a parameter to
higher-order <span class="inlinecode"><span class="id" type="keyword">Prop</span></span>s.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab215"></a><h1 class="section">Negation</h1>
<div class="paragraph"> </div>
The logical complement of a proposition <span class="inlinecode"><span class="id" type="var">P</span></span> is written <span class="inlinecode"><span class="id" type="var">not</span></span>
<span class="inlinecode"><span class="id" type="var">P</span></span> or, for shorthand, <span class="inlinecode">¬<span class="id" type="var">P</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">not</span> (<span class="id" type="var">P</span>:<span class="id" type="keyword">Prop</span>) := <span class="id" type="var">P</span> <span style="font-family: arial;">→</span> <span class="id" type="var">False</span>.<br/>
<br/>
</div>
<div class="doc">
The intuition is that, if <span class="inlinecode"><span class="id" type="var">P</span></span> is not true, then anything at
all (even <span class="inlinecode"><span class="id" type="var">False</span></span>) follows from assuming <span class="inlinecode"><span class="id" type="var">P</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Notation</span> "¬ x" := (<span class="id" type="var">not</span> <span class="id" type="var">x</span>) : <span class="id" type="var">type_scope</span>.<br/>
<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">not</span>.<br/>
<span class="comment">(* ===> Prop -> Prop *)</span><br/>
<br/>
</div>
<div class="doc">
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.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">not_False</span> : <br/>
¬ <span class="id" type="var">False</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">not</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab216"></a><h3 class="section"> </h3>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">contradiction_implies_anything</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>,<br/>
(<span class="id" type="var">P</span> <span style="font-family: arial;">∧</span> ¬<span class="id" type="var">P</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">Q</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">HP</span> <span class="id" type="var">HNA</span>]. <span class="id" type="tactic">unfold</span> <span class="id" type="var">not</span> <span class="id" type="keyword">in</span> <span class="id" type="var">HNA</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">HNA</span> <span class="id" type="keyword">in</span> <span class="id" type="var">HP</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">HP</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">double_neg</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> : <span class="id" type="keyword">Prop</span>,<br/>
<span class="id" type="var">P</span> <span style="font-family: arial;">→</span> ~~<span class="id" type="var">P</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* WORKED IN CLASS *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">P</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">not</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">G</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">G</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab217"></a><h4 class="section">Exercise: 2 stars, advanced (double_neg_inf)</h4>
Write an informal proof of <span class="inlinecode"><span class="id" type="var">double_neg</span></span>:
<div class="paragraph"> </div>
<i>Theorem</i>: <span class="inlinecode"><span class="id" type="var">P</span></span> implies <span class="inlinecode">~~<span class="id" type="var">P</span></span>, for any proposition <span class="inlinecode"><span class="id" type="var">P</span></span>.
<div class="paragraph"> </div>
<i>Proof</i>:
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab218"></a><h4 class="section">Exercise: 2 stars (contrapositive)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">contrapositive</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> <span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>,<br/>
(<span class="id" type="var">P</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span>) <span style="font-family: arial;">→</span> (¬<span class="id" type="var">Q</span> <span style="font-family: arial;">→</span> ¬<span class="id" type="var">P</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab219"></a><h4 class="section">Exercise: 1 star (not_both_true_and_false)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">not_both_true_and_false</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">P</span> : <span class="id" type="keyword">Prop</span>,<br/>
¬ (<span class="id" type="var">P</span> <span style="font-family: arial;">∧</span> ¬<span class="id" type="var">P</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>