-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prop.html
1850 lines (1435 loc) · 117 KB
/
Prop.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>Prop: Propositions and Evidence</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">Prop<span class="subtitle">Propositions and Evidence</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">Logic</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab230"></a><h1 class="section">Inductively Defined Propositions</h1>
<div class="paragraph"> </div>
In chapter <span class="inlinecode"><span class="id" type="var">Basics</span></span> we defined a <i>function</i> <span class="inlinecode"><span class="id" type="var">evenb</span></span> that tests a
number for evenness, yielding <span class="inlinecode"><span class="id" type="var">true</span></span> if so. We can use this
function to define the <i>proposition</i> that some number <span class="inlinecode"><span class="id" type="var">n</span></span> is
even:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">even</span> (<span class="id" type="var">n</span>:<span class="id" type="var">nat</span>) : <span class="id" type="keyword">Prop</span> := <br/>
<span class="id" type="var">evenb</span> <span class="id" type="var">n</span> = <span class="id" type="var">true</span>.<br/>
<br/>
</div>
<div class="doc">
That is, we can define "<span class="inlinecode"><span class="id" type="var">n</span></span> is even" to mean "the function <span class="inlinecode"><span class="id" type="var">evenb</span></span>
returns <span class="inlinecode"><span class="id" type="var">true</span></span> when applied to <span class="inlinecode"><span class="id" type="var">n</span></span>."
<div class="paragraph"> </div>
Note that here we have given a name
to a proposition using a <span class="inlinecode"><span class="id" type="keyword">Definition</span></span>, just as we have
given names to expressions of other sorts. This isn't a fundamentally
new kind of proposition; it is still just an equality.
<div class="paragraph"> </div>
Another alternative is to define the concept of evenness
directly. Instead of going via the <span class="inlinecode"><span class="id" type="var">evenb</span></span> function ("a number is
even if a certain computation yields <span class="inlinecode"><span class="id" type="var">true</span></span>"), we can say what the
concept of evenness means by giving two different ways of
presenting <i>evidence</i> that a number is even.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">ev</span> : <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">ev_0</span> : <span class="id" type="var">ev</span> <span class="id" type="var">O</span><br/>
| <span class="id" type="var">ev_SS</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">ev</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> (<span class="id" type="var">S</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>)).<br/>
<br/>
</div>
<div class="doc">
The first line declares that <span class="inlinecode"><span class="id" type="var">ev</span></span> is a proposition — or,
more formally, a family of propositions "indexed by" natural
numbers. (That is, for each number <span class="inlinecode"><span class="id" type="var">n</span></span>, the claim that "<span class="inlinecode"><span class="id" type="var">n</span></span> is
even" is a proposition.) Such a family of propositions is
often called a <i>property</i> of numbers.
<div class="paragraph"> </div>
The last two lines declare the two ways to give evidence that a
number <span class="inlinecode"><span class="id" type="var">m</span></span> is even. First, <span class="inlinecode">0</span> is even, and <span class="inlinecode"><span class="id" type="var">ev_0</span></span> is evidence
for this. Second, if <span class="inlinecode"><span class="id" type="var">m</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">S</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n</span>)</span> for some <span class="inlinecode"><span class="id" type="var">n</span></span> and we can give
evidence <span class="inlinecode"><span class="id" type="var">e</span></span> that <span class="inlinecode"><span class="id" type="var">n</span></span> is even, then <span class="inlinecode"><span class="id" type="var">m</span></span> is also even, and <span class="inlinecode"><span class="id" type="var">ev_SS</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span>
<span class="inlinecode"><span class="id" type="var">e</span></span> is the evidence.
<div class="paragraph"> </div>
<a name="lab231"></a><h4 class="section">Exercise: 1 star (double_even)</h4>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">double_even</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>,<br/>
<span class="id" type="var">ev</span> (<span class="id" type="var">double</span> <span class="id" type="var">n</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/>
<br/>
</div>
<div class="doc">
For <span class="inlinecode"><span class="id" type="var">ev</span></span>, we had already defined <span class="inlinecode"><span class="id" type="var">even</span></span> as a function (returning a
boolean), and then defined an inductive relation that agreed with
it. However, we don't necessarily need to think about propositions
first as boolean functions, we can start off with the inductive
definition.
<div class="paragraph"> </div>
As another example of an inductively defined proposition, let's
define a simple property of natural numbers — we'll call it
"<span class="inlinecode"><span class="id" type="var">beautiful</span></span>."
<div class="paragraph"> </div>
Informally, a number is <span class="inlinecode"><span class="id" type="var">beautiful</span></span> if it is <span class="inlinecode">0</span>, <span class="inlinecode">3</span>, <span class="inlinecode">5</span>, or the
sum of two <span class="inlinecode"><span class="id" type="var">beautiful</span></span> numbers.
<div class="paragraph"> </div>
More pedantically, we can define <span class="inlinecode"><span class="id" type="var">beautiful</span></span> numbers by giving four
rules:
<div class="paragraph"> </div>
<ul class="doclist">
<li> Rule <span class="inlinecode"><span class="id" type="var">b_0</span></span>: The number <span class="inlinecode">0</span> is <span class="inlinecode"><span class="id" type="var">beautiful</span></span>.
</li>
<li> Rule <span class="inlinecode"><span class="id" type="var">b_3</span></span>: The number <span class="inlinecode">3</span> is <span class="inlinecode"><span class="id" type="var">beautiful</span></span>.
</li>
<li> Rule <span class="inlinecode"><span class="id" type="var">b_5</span></span>: The number <span class="inlinecode">5</span> is <span class="inlinecode"><span class="id" type="var">beautiful</span></span>.
</li>
<li> Rule <span class="inlinecode"><span class="id" type="var">b_sum</span></span>: If <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">m</span></span> are both <span class="inlinecode"><span class="id" type="var">beautiful</span></span>, then so is
their sum.
</li>
</ul>
<div class="paragraph"> </div>
We will see many definitions like this one during the rest
of the course, and for purposes of informal discussions, it is
helpful to have a lightweight notation that makes them easy to
read and write. <i>Inference rules</i> are one such notation:
<div class="paragraph"> </div>
<center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(b_0)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">beautiful 0</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(b_3)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">beautiful 3</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule"> </td>
<td class="infrulenamecol" rowspan="3">
(b_5)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">beautiful 5</td>
<td></td>
</td>
</table></center><center><table class="infrule">
<tr class="infruleassumption">
<td class="infrule">beautiful n beautiful m</td>
<td class="infrulenamecol" rowspan="3">
(b_sum)
</td></tr>
<tr class="infrulemiddle">
<td class="infrule"><hr /></td>
</tr>
<tr class="infruleassumption">
<td class="infrule">beautiful (n+m)</td>
<td></td>
</td>
</table></center>
<div class="paragraph"> </div>
<a name="lab232"></a><h3 class="section"> </h3>
Each of the textual rules above is reformatted here as an
inference rule; the intended reading is that, if the <i>premises</i>
above the line all hold, then the <i>conclusion</i> below the line
follows. For example, the rule <span class="inlinecode"><span class="id" type="var">b_sum</span></span> says that, if <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">m</span></span>
are both <span class="inlinecode"><span class="id" type="var">beautiful</span></span> numbers, then it follows that <span class="inlinecode"><span class="id" type="var">n</span>+<span class="id" type="var">m</span></span> is
<span class="inlinecode"><span class="id" type="var">beautiful</span></span> too. If a rule has no premises above the line, then
its conclusion holds unconditionally.
<div class="paragraph"> </div>
These rules <i>define</i> the property <span class="inlinecode"><span class="id" type="var">beautiful</span></span>. That is, if we
want to convince someone that some particular number is <span class="inlinecode"><span class="id" type="var">beautiful</span></span>,
our argument must be based on these rules. For a simple example,
suppose we claim that the number <span class="inlinecode">5</span> is <span class="inlinecode"><span class="id" type="var">beautiful</span></span>. To support
this claim, we just need to point out that rule <span class="inlinecode"><span class="id" type="var">b_5</span></span> says so.
Or, if we want to claim that <span class="inlinecode">8</span> is <span class="inlinecode"><span class="id" type="var">beautiful</span></span>, we can support our
claim by first observing that <span class="inlinecode">3</span> and <span class="inlinecode">5</span> are both <span class="inlinecode"><span class="id" type="var">beautiful</span></span> (by
rules <span class="inlinecode"><span class="id" type="var">b_3</span></span> and <span class="inlinecode"><span class="id" type="var">b_5</span></span>) and then pointing out that their sum, <span class="inlinecode">8</span>,
is therefore <span class="inlinecode"><span class="id" type="var">beautiful</span></span> by rule <span class="inlinecode"><span class="id" type="var">b_sum</span></span>. This argument can be
expressed graphically with the following <i>proof tree</i>:
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<div class="code code-tight">
----------- (<span class="id" type="var">b_3</span>) ----------- (<span class="id" type="var">b_5</span>)<br/>
<span class="id" type="var">beautiful</span> 3 <span class="id" type="var">beautiful</span> 5<br/>
------------------------------- (<span class="id" type="var">b_sum</span>)<br/>
<span class="id" type="var">beautiful</span> 8
<div class="paragraph"> </div>
</div>
<a name="lab233"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
Of course, there are other ways of using these rules to argue that
<span class="inlinecode">8</span> is <span class="inlinecode"><span class="id" type="var">beautiful</span></span>, for instance:
<div class="paragraph"> </div>
<div class="code code-tight">
----------- (<span class="id" type="var">b_5</span>) ----------- (<span class="id" type="var">b_3</span>)<br/>
<span class="id" type="var">beautiful</span> 5 <span class="id" type="var">beautiful</span> 3<br/>
------------------------------- (<span class="id" type="var">b_sum</span>)<br/>
<span class="id" type="var">beautiful</span> 8
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
<a name="lab234"></a><h4 class="section">Exercise: 1 star (varieties_of_beauty)</h4>
How many different ways are there to show that <span class="inlinecode">8</span> is <span class="inlinecode"><span class="id" type="var">beautiful</span></span>?
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab235"></a><h2 class="section">Constructing Evidence</h2>
<div class="paragraph"> </div>
In Coq, we can express the definition of <span class="inlinecode"><span class="id" type="var">beautiful</span></span> as
follows:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">beautiful</span> : <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
<span class="id" type="var">b_0</span> : <span class="id" type="var">beautiful</span> 0<br/>
| <span class="id" type="var">b_3</span> : <span class="id" type="var">beautiful</span> 3<br/>
| <span class="id" type="var">b_5</span> : <span class="id" type="var">beautiful</span> 5<br/>
| <span class="id" type="var">b_sum</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span> <span class="id" type="var">m</span>, <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">beautiful</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span> <span class="id" type="var">beautiful</span> (<span class="id" type="var">n</span>+<span class="id" type="var">m</span>).<br/>
<br/>
</div>
<div class="doc">
<a name="lab236"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
The rules introduced this way have the same status as proven
theorems; that is, they are true axiomatically.
So we can use Coq's <span class="inlinecode"><span class="id" type="tactic">apply</span></span> tactic with the rule names to prove
that particular numbers are <span class="inlinecode"><span class="id" type="var">beautiful</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">three_is_beautiful</span>: <span class="id" type="var">beautiful</span> 3.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* This simply follows from the rule <span class="inlinecode"><span class="id" type="var">b_3</span></span>. *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_3</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">eight_is_beautiful</span>: <span class="id" type="var">beautiful</span> 8.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* First we use the rule <span class="inlinecode"><span class="id" type="var">b_sum</span></span>, telling Coq how to<br/>
instantiate <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">m</span></span>. *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_sum</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">n</span>:=3) (<span class="id" type="var">m</span>:=5).<br/>
<span class="comment">(* To solve the subgoals generated by <span class="inlinecode"><span class="id" type="var">b_sum</span></span>, we must provide<br/>
evidence of <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode">3</span> and <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode">5</span>. Fortunately we<br/>
have rules for both. *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_3</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_5</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab237"></a><h3 class="section"> </h3>
As you would expect, we can also prove theorems that have
hypotheses about <span class="inlinecode"><span class="id" type="var">beautiful</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">beautiful_plus_eight</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">beautiful</span> (8+<span class="id" type="var">n</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">B</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_sum</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">n</span>:=8) (<span class="id" type="var">m</span>:=<span class="id" type="var">n</span>).<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">eight_is_beautiful</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">B</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab238"></a><h4 class="section">Exercise: 2 stars (b_times2)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">b_times2</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">beautiful</span> (2×<span class="id" type="var">n</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="lab239"></a><h4 class="section">Exercise: 3 stars (b_timesm)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">b_timesm</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">n</span> <span class="id" type="var">m</span>, <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">beautiful</span> (<span class="id" type="var">m</span>×<span class="id" type="var">n</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="lab240"></a><h1 class="section">Using Evidence in Proofs</h1>
<a name="lab241"></a><h2 class="section">Induction over Evidence</h2>
<div class="paragraph"> </div>
Besides <i>constructing</i> evidence that numbers are beautiful, we can
also <i>reason about</i> such evidence.
<div class="paragraph"> </div>
The fact that we introduced <span class="inlinecode"><span class="id" type="var">beautiful</span></span> with an <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span>
declaration tells Coq not only that the constructors <span class="inlinecode"><span class="id" type="var">b_0</span></span>, <span class="inlinecode"><span class="id" type="var">b_3</span></span>,
<span class="inlinecode"><span class="id" type="var">b_5</span></span> and <span class="inlinecode"><span class="id" type="var">b_sum</span></span> are ways to build evidence, but also that these
four constructors are the <i>only</i> ways to build evidence that
numbers are beautiful.
<div class="paragraph"> </div>
In other words, if someone gives us evidence <span class="inlinecode"><span class="id" type="var">E</span></span> for the assertion
<span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span>, then we know that <span class="inlinecode"><span class="id" type="var">E</span></span> must have one of four shapes:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">E</span></span> is <span class="inlinecode"><span class="id" type="var">b_0</span></span> (and <span class="inlinecode"><span class="id" type="var">n</span></span> is <span class="inlinecode"><span class="id" type="var">O</span></span>),
</li>
<li> <span class="inlinecode"><span class="id" type="var">E</span></span> is <span class="inlinecode"><span class="id" type="var">b_3</span></span> (and <span class="inlinecode"><span class="id" type="var">n</span></span> is <span class="inlinecode">3</span>),
</li>
<li> <span class="inlinecode"><span class="id" type="var">E</span></span> is <span class="inlinecode"><span class="id" type="var">b_5</span></span> (and <span class="inlinecode"><span class="id" type="var">n</span></span> is <span class="inlinecode">5</span>), or
</li>
<li> <span class="inlinecode"><span class="id" type="var">E</span></span> is <span class="inlinecode"><span class="id" type="var">b_sum</span></span> <span class="inlinecode"><span class="id" type="var">n1</span></span> <span class="inlinecode"><span class="id" type="var">n2</span></span> <span class="inlinecode"><span class="id" type="var">E1</span></span> <span class="inlinecode"><span class="id" type="var">E2</span></span> (and <span class="inlinecode"><span class="id" type="var">n</span></span> is <span class="inlinecode"><span class="id" type="var">n1</span>+<span class="id" type="var">n2</span></span>, where <span class="inlinecode"><span class="id" type="var">E1</span></span> is
evidence that <span class="inlinecode"><span class="id" type="var">n1</span></span> is beautiful and <span class="inlinecode"><span class="id" type="var">E2</span></span> is evidence that <span class="inlinecode"><span class="id" type="var">n2</span></span>
is beautiful).
</li>
</ul>
<div class="paragraph"> </div>
<a name="lab242"></a><h3 class="section"> </h3>
This permits us to <i>analyze</i> any hypothesis of the form <span class="inlinecode"><span class="id" type="var">beautiful</span></span>
<span class="inlinecode"><span class="id" type="var">n</span></span> to see how it was constructed, using the tactics we already
know. In particular, we can use the <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic that we
have already seen for reasoning about inductively defined <i>data</i>
to reason about inductively defined <i>evidence</i>.
<div class="paragraph"> </div>
To illustrate this, let's define another property of numbers:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">gorgeous</span> : <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
<span class="id" type="var">g_0</span> : <span class="id" type="var">gorgeous</span> 0<br/>
| <span class="id" type="var">g_plus3</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">gorgeous</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">gorgeous</span> (3+<span class="id" type="var">n</span>)<br/>
| <span class="id" type="var">g_plus5</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">gorgeous</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">gorgeous</span> (5+<span class="id" type="var">n</span>).<br/>
<br/>
</div>
<div class="doc">
<a name="lab243"></a><h4 class="section">Exercise: 1 star (gorgeous_tree)</h4>
Write out the definition of <span class="inlinecode"><span class="id" type="var">gorgeous</span></span> numbers using inference rule
notation.
<div class="paragraph"> </div>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab244"></a><h4 class="section">Exercise: 1 star (gorgeous_plus13)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">gorgeous_plus13</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <br/>
<span class="id" type="var">gorgeous</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">gorgeous</span> (13+<span class="id" type="var">n</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="lab245"></a><h3 class="section"> </h3>
It seems intuitively obvious that, although <span class="inlinecode"><span class="id" type="var">gorgeous</span></span> and
<span class="inlinecode"><span class="id" type="var">beautiful</span></span> are presented using slightly different rules, they are
actually the same property in the sense that they are true of the
same numbers. Indeed, we can prove this.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">gorgeous__beautiful_FAILED</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <br/>
<span class="id" type="var">gorgeous</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="tactic">induction</span> <span class="id" type="var">n</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">n'</span>].<br/>
- <span class="comment">(* n = 0 *)</span> <span class="id" type="tactic">apply</span> <span class="id" type="var">b_0</span>.<br/>
- <span class="comment">(* n = S n' *)</span> <span class="comment">(* We are stuck! *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
The problem here is that doing induction on <span class="inlinecode"><span class="id" type="var">n</span></span> doesn't yield a
useful induction hypothesis. Knowing how the property we are
interested in behaves on the predecessor of <span class="inlinecode"><span class="id" type="var">n</span></span> doesn't help us
prove that it holds for <span class="inlinecode"><span class="id" type="var">n</span></span>. Instead, we would like to be able to
have induction hypotheses that mention other numbers, such as <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">-</span>
<span class="inlinecode">3</span> and <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">-</span> <span class="inlinecode">5</span>. This is given precisely by the shape of the
constructors for <span class="inlinecode"><span class="id" type="var">gorgeous</span></span>.
<div class="paragraph"> </div>
<a name="lab246"></a><h3 class="section"> </h3>
<div class="paragraph"> </div>
Let's see what happens if we try to prove this by induction on the evidence <span class="inlinecode"><span class="id" type="var">H</span></span>
instead of on <span class="inlinecode"><span class="id" type="var">n</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">gorgeous__beautiful</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <br/>
<span class="id" type="var">gorgeous</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">n'</span>|<span class="id" type="var">n'</span>].<br/>
- <span class="comment">(* g_0 *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_0</span>.<br/>
- <span class="comment">(* g_plus3 *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_sum</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">b_3</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHgorgeous</span>.<br/>
- <span class="comment">(* g_plus5 *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_sum</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">b_5</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">IHgorgeous</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="comment">(* These exercises also require the use of induction on the evidence. *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab247"></a><h4 class="section">Exercise: 2 stars (gorgeous_sum)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">gorgeous_sum</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span> <span class="id" type="var">m</span>,<br/>
<span class="id" type="var">gorgeous</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">gorgeous</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span> <span class="id" type="var">gorgeous</span> (<span class="id" type="var">n</span> + <span class="id" type="var">m</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="lab248"></a><h4 class="section">Exercise: 3 stars, advanced (beautiful__gorgeous)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">beautiful__gorgeous</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">gorgeous</span> <span class="id" type="var">n</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="lab249"></a><h4 class="section">Exercise: 3 stars, optional (g_times2)</h4>
Prove the <span class="inlinecode"><span class="id" type="var">g_times2</span></span> theorem below without using <span class="inlinecode"><span class="id" type="var">gorgeous__beautiful</span></span>.
You might find the following helper lemma useful.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">helper_g_times2</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">y</span> <span class="id" type="var">z</span>, <span class="id" type="var">x</span> + (<span class="id" type="var">z</span> + <span class="id" type="var">y</span>) = <span class="id" type="var">z</span> + <span class="id" type="var">x</span> + <span class="id" type="var">y</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">g_times2</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">gorgeous</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">gorgeous</span> (2×<span class="id" type="var">n</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">simpl</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">H</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>
Here is a proof that the inductive definition of evenness implies
the computational one.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ev__even</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>,<br/>
<span class="id" type="var">ev</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">even</span> <span class="id" type="var">n</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">E</span>. <span class="id" type="tactic">induction</span> <span class="id" type="var">E</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">n'</span> <span class="id" type="var">E'</span>].<br/>
- <span class="comment">(* E = ev_0 *)</span><br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">even</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
- <span class="comment">(* E = ev_SS n' E' *)</span><br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">even</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">IHE'</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab250"></a><h4 class="section">Exercise: 1 star (ev__even)</h4>
Could this proof also be carried out by induction on <span class="inlinecode"><span class="id" type="var">n</span></span> instead
of <span class="inlinecode"><span class="id" type="var">E</span></span>? If not, why not?
</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>
Intuitively, the induction principle for evidence <span class="inlinecode"><span class="id" type="var">ev</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> is
similar to induction on <span class="inlinecode"><span class="id" type="var">n</span></span>, but restricts our attention to only
those numbers for which evidence <span class="inlinecode"><span class="id" type="var">ev</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> could be generated.
<div class="paragraph"> </div>
<a name="lab251"></a><h4 class="section">Exercise: 1 star (l_fails)</h4>
The following proof attempt will not succeed.
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">l</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>,<br/>
<span class="id" type="var">ev</span> <span class="id" type="var">n</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span>. <span class="id" type="tactic">induction</span> <span class="id" type="var">n</span>.<br/>
- <span class="comment">(* O *)</span> <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">ev_0</span>.<br/>
- <span class="comment">(* S *)</span><br/>
...
<div class="paragraph"> </div>
</div>
Intuitively, we expect the proof to fail because not every
number is even. However, what exactly causes the proof to fail?
<div class="paragraph"> </div>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
<div class="paragraph"> </div>
Here's another exercise requiring induction on evidence. <a name="lab252"></a><h4 class="section">Exercise: 2 stars (ev_sum)</h4>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ev_sum</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span> <span class="id" type="var">m</span>,<br/>
<span class="id" type="var">ev</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> (<span class="id" type="var">n</span>+<span class="id" type="var">m</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="lab253"></a><h2 class="section">Inversion on Evidence</h2>
<div class="paragraph"> </div>
Having evidence for a proposition is useful while proving, because we
can <i>look</i> at that evidence for more information. For example, consider
proving that, if <span class="inlinecode"><span class="id" type="var">n</span></span> is even, then <span class="inlinecode"><span class="id" type="var">pred</span></span> <span class="inlinecode">(<span class="id" type="var">pred</span></span> <span class="inlinecode"><span class="id" type="var">n</span>)</span> is
too. In this case, we don't need to do an inductive proof. Instead
the <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic provides all of the information that we need.
<div class="paragraph"> </div>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ev_minus2</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">ev</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> (<span class="id" type="var">pred</span> (<span class="id" type="var">pred</span> <span class="id" type="var">n</span>)).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">E</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">E</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">n'</span> <span class="id" type="var">E'</span>].<br/>
- <span class="comment">(* E = ev_0 *)</span> <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">ev_0</span>.<br/>
- <span class="comment">(* E = ev_SS n' E' *)</span> <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">E'</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab254"></a><h4 class="section">Exercise: 1 star, optional (ev_minus2_n)</h4>
What happens if we try to use <span class="inlinecode"><span class="id" type="tactic">destruct</span></span> on <span class="inlinecode"><span class="id" type="var">n</span></span> instead of <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> on <span class="inlinecode"><span class="id" type="var">E</span></span>?
</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>
<a name="lab255"></a><h3 class="section"> </h3>
Here is another example, in which <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> helps narrow down to
the relevant cases.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">SSev__even</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>,<br/>
<span class="id" type="var">ev</span> (<span class="id" type="var">S</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>)) <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> <span class="id" type="var">n</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">E</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">E</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">n'</span> <span class="id" type="var">E'</span>].<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">E'</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab256"></a><h2 class="section">The Inversion Tactic Revisited</h2>
<div class="paragraph"> </div>
These uses of <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> may seem a bit mysterious at first.
Until now, we've only used <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> on equality
propositions, to utilize injectivity of constructors or to
discriminate between different constructors. But we see here
that <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> can also be applied to analyzing evidence
for inductively defined propositions.
<div class="paragraph"> </div>
(You might also expect that <span class="inlinecode"><span class="id" type="tactic">destruct</span></span> would be a more suitable
tactic to use here. Indeed, it is possible to use <span class="inlinecode"><span class="id" type="tactic">destruct</span></span>, but
it often throws away useful information, and the <span class="inlinecode"><span class="id" type="var">eqn</span>:</span> qualifier
doesn't help much in this case.)
<div class="paragraph"> </div>
Here's how <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> works in general. Suppose the name
<span class="inlinecode"><span class="id" type="var">I</span></span> refers to an assumption <span class="inlinecode"><span class="id" type="var">P</span></span> in the current context, where
<span class="inlinecode"><span class="id" type="var">P</span></span> has been defined by an <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> declaration. Then,
for each of the constructors of <span class="inlinecode"><span class="id" type="var">P</span></span>, <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode"><span class="id" type="var">I</span></span> generates
a subgoal in which <span class="inlinecode"><span class="id" type="var">I</span></span> has been replaced by the exact,
specific conditions under which this constructor could have
been used to prove <span class="inlinecode"><span class="id" type="var">P</span></span>. Some of these subgoals will be
self-contradictory; <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> throws these away. The ones
that are left represent the cases that must be proved to
establish the original goal.
<div class="paragraph"> </div>
In this particular case, the <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> analyzed the construction
<span class="inlinecode"><span class="id" type="var">ev</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n</span>))</span>, determined that this could only have been
constructed using <span class="inlinecode"><span class="id" type="var">ev_SS</span></span>, and generated a new subgoal with the
arguments of that constructor as new hypotheses. (It also
produced an auxiliary equality, which happens to be useless here.)
We'll begin exploring this more general behavior of inversion in
what follows.
<div class="paragraph"> </div>
<a name="lab257"></a><h4 class="section">Exercise: 1 star (inversion_practice)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">SSSSev__even</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>,<br/>
<span class="id" type="var">ev</span> (<span class="id" type="var">S</span> (<span class="id" type="var">S</span> (<span class="id" type="var">S</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>)))) <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> <span class="id" type="var">n</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">
The <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic can also be used to derive goals by showing
the absurdity of a hypothesis.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">even5_nonsense</span> : <br/>
<span class="id" type="var">ev</span> 5 <span style="font-family: arial;">→</span> 2 + 2 = 9.<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="lab258"></a><h4 class="section">Exercise: 3 stars, advanced (ev_ev__ev)</h4>
Finding the appropriate thing to do induction on is a
bit tricky here:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ev_ev__ev</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span> <span class="id" type="var">m</span>,<br/>
<span class="id" type="var">ev</span> (<span class="id" type="var">n</span>+<span class="id" type="var">m</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> <span class="id" type="var">m</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="lab259"></a><h4 class="section">Exercise: 3 stars, optional (ev_plus_plus)</h4>
Here's an exercise that just requires applying existing lemmas. No
induction or even case analysis is needed, but some of the rewriting
may be tedious.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ev_plus_plus</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span> <span class="id" type="var">m</span> <span class="id" type="var">p</span>,<br/>
<span class="id" type="var">ev</span> (<span class="id" type="var">n</span>+<span class="id" type="var">m</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> (<span class="id" type="var">n</span>+<span class="id" type="var">p</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> (<span class="id" type="var">m</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>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab260"></a><h1 class="section">Discussion and Variations</h1>
<a name="lab261"></a><h2 class="section">Computational vs. Inductive Definitions</h2>
<div class="paragraph"> </div>
We have seen that the proposition "<span class="inlinecode"><span class="id" type="var">n</span></span> is even" can be
phrased in two different ways — indirectly, via a boolean testing
function <span class="inlinecode"><span class="id" type="var">evenb</span></span>, or directly, by inductively describing what
constitutes evidence for evenness. These two ways of defining
evenness are about equally easy to state and work with. Which we
choose is basically a question of taste.
<div class="paragraph"> </div>
However, for many other properties of interest, the direct
inductive definition is preferable, since writing a testing
function may be awkward or even impossible.
<div class="paragraph"> </div>
One such property is <span class="inlinecode"><span class="id" type="var">beautiful</span></span>. This is a perfectly sensible
definition of a set of numbers, but we cannot translate its
definition directly into a Coq Fixpoint (or into a recursive
function in any other common programming language). We might be
able to find a clever way of testing this property using a
<span class="inlinecode"><span class="id" type="keyword">Fixpoint</span></span> (indeed, it is not too hard to find one in this case),
but in general this could require arbitrarily deep thinking. In
fact, if the property we are interested in is uncomputable, then
we cannot define it as a <span class="inlinecode"><span class="id" type="keyword">Fixpoint</span></span> no matter how hard we try,
because Coq requires that all <span class="inlinecode"><span class="id" type="keyword">Fixpoint</span></span>s correspond to
terminating computations.
<div class="paragraph"> </div>
On the other hand, writing an inductive definition of what it
means to give evidence for the property <span class="inlinecode"><span class="id" type="var">beautiful</span></span> is
straightforward.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab262"></a><h2 class="section">Parameterized Data Structures</h2>
<div class="paragraph"> </div>
So far, we have only looked at propositions about natural numbers. However,
we can define inductive predicates about any type of data. For example,
suppose we would like to characterize lists of <i>even</i> length. We can
do that with the following definition.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">ev_list</span> {<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>} : <span class="id" type="var">list</span> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span> :=<br/>
| <span class="id" type="var">el_nil</span> : <span class="id" type="var">ev_list</span> []<br/>
| <span class="id" type="var">el_cc</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">y</span> <span class="id" type="var">l</span>, <span class="id" type="var">ev_list</span> <span class="id" type="var">l</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ev_list</span> (<span class="id" type="var">x</span> :: <span class="id" type="var">y</span> :: <span class="id" type="var">l</span>).<br/>
<br/>
</div>
<div class="doc">
Of course, this proposition is equivalent to just saying that the
length of the list is even.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">ev_list__ev_length</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">X</span> (<span class="id" type="var">l</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>), <span class="id" type="var">ev_list</span> <span class="id" type="var">l</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ev</span> (<span class="id" type="var">length</span> <span class="id" type="var">l</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">X</span> <span class="id" type="var">l</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">induction</span> <span class="id" type="var">H</span>.<br/>
- <span class="comment">(* el_nil *)</span> <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">ev_0</span>.<br/>
- <span class="comment">(* el_cc *)</span> <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">ev_SS</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">IHev_list</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
However, because evidence for <span class="inlinecode"><span class="id" type="var">ev</span></span> contains less information than
evidence for <span class="inlinecode"><span class="id" type="var">ev_list</span></span>, the converse direction must be stated very
carefully.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">ev_length__ev_list</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">X</span> <span class="id" type="var">n</span>, <span class="id" type="var">ev</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span style="font-family: arial;">∀</span>(<span class="id" type="var">l</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>), <span class="id" type="var">n</span> = <span class="id" type="var">length</span> <span class="id" type="var">l</span> <span style="font-family: arial;">→</span> <span class="id" type="var">ev_list</span> <span class="id" type="var">l</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">X</span> <span class="id" type="var">n</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">H</span>.<br/>
- <span class="comment">(* ev_0 *)</span> <span class="id" type="tactic">intros</span> <span class="id" type="var">l</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">l</span>.<br/>
+ <span class="comment">(* <span class="inlinecode"></span> *)</span> <span class="id" type="tactic">apply</span> <span class="id" type="var">el_nil</span>.<br/>
+ <span class="comment">(* x::l *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>.<br/>
- <span class="comment">(* ev_SS *)</span> <span class="id" type="tactic">intros</span> <span class="id" type="var">l</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">l</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">x</span> [| <span class="id" type="var">x0</span> <span class="id" type="var">l</span>]].<br/>
+ <span class="comment">(* <span class="inlinecode"></span> *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>.<br/>
+ <span class="comment">(* <span class="inlinecode"><span class="id" type="var">x</span></span> *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>.<br/>
+ <span class="comment">(* x :: x0 :: l *)</span> <span class="id" type="tactic">apply</span> <span class="id" type="var">el_cc</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">IHev</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab263"></a><h4 class="section">Exercise: 4 stars (palindromes)</h4>
A palindrome is a sequence that reads the same backwards as
forwards.
<div class="paragraph"> </div>
<ul class="doclist">
<li> Define an inductive proposition <span class="inlinecode"><span class="id" type="var">pal</span></span> on <span class="inlinecode"><span class="id" type="var">list</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> that
captures what it means to be a palindrome. (Hint: You'll need
three cases. Your definition should be based on the structure
of the list; just having a single constructor
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">c</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">l</span>, <span class="id" type="var">l</span> = <span class="id" type="var">rev</span> <span class="id" type="var">l</span> <span style="font-family: arial;">→</span> <span class="id" type="var">pal</span> <span class="id" type="var">l</span>
<div class="paragraph"> </div>
</div>
may seem obvious, but will not work very well.)
<div class="paragraph"> </div>
</li>
<li> Prove <span class="inlinecode"><span class="id" type="var">pal_app_rev</span></span> that
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="font-family: arial;">∀</span><span class="id" type="var">l</span>, <span class="id" type="var">pal</span> (<span class="id" type="var">l</span> ++ <span class="id" type="var">rev</span> <span class="id" type="var">l</span>).
<div class="paragraph"> </div>
</div>
</li>
<li> Prove <span class="inlinecode"><span class="id" type="var">pal_rev</span></span> that
<div class="paragraph"> </div>
<div class="code code-tight">