-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProp.html
2830 lines (2217 loc) · 201 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=iso-8859-1"/>
<link href="coqdoc.css" rel="stylesheet" type="text/css"/>
<title>Prop: Propositions and Evidence</title>
</head>
<script>
<!--
function toggleDisplay(id)
{
var elt = document.getElementById(id);
if (elt.style.display == 'none') {
elt.style.display = 'block';
} else {
elt.style.display = 'none';
}
}
function hideAll(cls)
{
var testClass = new RegExp("(^|s)" + cls + "(s|$)");
var tag = tag || "*";
var elements = document.getElementsByTagName("div");
var current;
var length = elements.length;
for(var i=0; i<length; i++){
current = elements[i];
if(testClass.test(current.className)) {
current.style.display = 'none';
}
}
}
// -->
</script>
<body onLoad="hideAll('proofscript')">
<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="comment">(* $Date: 2012-07-23 16:26:25 -0400 (Mon, 23 Jul 2012) $ *)</span><br/>
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <span class="id" type="var">Poly</span>.<br/>
<br/>
</div>
<div class="doc">
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 this chapter we take a deeper look at the way propositions are
expressed in Coq and at the structure of the logical evidence that
we construct when we carry out proofs.
<div class="paragraph"> </div>
Some of the concepts in this chapter may seem a bit abstract on a
first encounter. We've included a <i>lot</i> of exercises, most of
which should be quite approachable even if you're still working on
understanding the details of the text. Try to work as many of
them as you can, especially the one-starred exercises.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab149"></a><h1 class="section">Inductively Defined Propositions</h1>
<div class="paragraph"> </div>
As a running example for the first part of the chapter, let's
consider a simple property of natural numbers, and let's say that
the numbers possessing this property are "beautiful."
<div class="paragraph"> </div>
Informally, a number is <i>beautiful</i> if it is <span class="inlinecode">0</span>, <span class="inlinecode">3</span>, <span class="inlinecode">5</span>, or the
sum of two beautiful numbers. More pedantically, we can define
beautiful 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 beautiful.
</li>
<li> Rule <span class="inlinecode"><span class="id" type="var">b_3</span></span>: The number <span class="inlinecode">3</span> is beautiful.
</li>
<li> Rule <span class="inlinecode"><span class="id" type="var">b_5</span></span>: The number <span class="inlinecode">5</span> is beautiful.
</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 beautiful, 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>
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 beautiful numbers, then it follows that <span class="inlinecode"><span class="id" type="var">n</span>+<span class="id" type="var">m</span></span> is
beautiful too. The rules with no premises above the line are
called <i>axioms</i>.
<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 beautiful,
our argument must be based on these rules. For a simple example,
suppose we claim that the number <span class="inlinecode">5</span> is beautiful. 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 it is.
Or, if we want to claim that <span class="inlinecode">8</span> is beautiful, we can support our
claim by first observing that <span class="inlinecode">3</span> and <span class="inlinecode">5</span> are both beautiful (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 beautiful 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="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>
Of course, there are other ways of using these rules to argue that
<span class="inlinecode">8</span> is beautiful — 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="lab150"></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 beautiful?
</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>
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">
The first line declares that <span class="inlinecode"><span class="id" type="var">beautiful</span></span> is a proposition — or,
more formally, a family of propositions "indexed by" natural
numbers. (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
<span class="inlinecode"><span class="id" type="var">beautiful</span></span>" is a proposition.) Such a family of propositions is
often called a <i>property</i> of numbers.
<div class="paragraph"> </div>
Each of the remaining lines embodies one of the rules for
beautiful numbers.
<div class="paragraph"> </div>
We can use Coq's tactic scripting facility to assemble proofs that
particular numbers are beautiful.
</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 axiom <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 axioms 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="lab151"></a><h1 class="section">Proof Objects</h1>
<div class="paragraph"> </div>
Look again at the formal definition of the <span class="inlinecode"><span class="id" type="var">beautiful</span></span> property.
The opening keyword, <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span>, has been used up to this point
to declare new types of <i>data</i>, such as numbers and lists. Does
this interpretation also make sense for the Inductive definition
of <span class="inlinecode"><span class="id" type="var">beautiful</span></span>? That is, can we view evidence of beauty as some
kind of data structure? Yes, we can!
<div class="paragraph"> </div>
The trick is to introduce an alternative pronunciation of "<span class="inlinecode">:</span>".
Instead of "has type," we can also say "is a proof of." For
example, the second line in the definition of <span class="inlinecode"><span class="id" type="var">beautiful</span></span> declares
that <span class="inlinecode"><span class="id" type="var">b_0</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode">0</span>. Instead of "<span class="inlinecode"><span class="id" type="var">b_0</span></span> has type
<span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode">0</span>," we can say that "<span class="inlinecode"><span class="id" type="var">b_0</span></span> is a proof of <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode">0</span>."
Similarly for <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>.
<div class="paragraph"> </div>
This pun between "<span class="inlinecode">:</span>" as "has type" and <span class="inlinecode">:</span> as "is a proof of" is
called the <i>Curry-Howard correspondence</i> (or sometimes
<i>Curry-Howard isomorphism</i>). It proposes a deep connection
between the world of logic and the world of computation.
<pre>
propositions ~ types
evidence ~ data
</pre>
Many useful things follow from this connection. To begin with, it
gives us a natural interpretation of the <span class="inlinecode"><span class="id" type="var">b_sum</span></span> constructor:
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<div class="code code-tight">
<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>, <br/>
<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>).
<div class="paragraph"> </div>
</div>
If we read <span class="inlinecode">:</span> as "has type," this says that <span class="inlinecode"><span class="id" type="var">b_sum</span></span> is a data
constructor that takes four arguments: two numbers, <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">m</span></span>,
and two values of type <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span>. That is,
<span class="inlinecode"><span class="id" type="var">b_sum</span></span> can be viewed as a <i>function</i> that, given evidence for the
propositions <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span>, gives us evidence
for the proposition that <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode">(<span class="id" type="var">n</span>+<span class="id" type="var">m</span>)</span>.
<div class="paragraph"> </div>
In view of this, we might wonder whether we can write an
expression of type <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode">8</span> by applying <span class="inlinecode"><span class="id" type="var">b_sum</span></span> to
appropriate arguments. Indeed, we can:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> (<span class="id" type="var">b_sum</span> 3 5 <span class="id" type="var">b_3</span> <span class="id" type="var">b_5</span>).<br/>
<br/>
</div>
<div class="doc">
The expression <span class="inlinecode"><span class="id" type="var">b_sum</span></span> <span class="inlinecode">3</span> <span class="inlinecode">5</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> can be thought of as
instantiating the parameterized constructor <span class="inlinecode"><span class="id" type="var">b_sum</span></span> with the
specific arguments <span class="inlinecode">3</span> <span class="inlinecode">5</span> and the corresponding proof objects for
its premises <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> (Coq is smart enough
to figure out that 3+5=8). Alternatively, we can think of <span class="inlinecode"><span class="id" type="var">b_sum</span></span>
as a primitive "evidence constructor" that, when applied to two
particular numbers, wants to be further applied to evidence that
those two numbers are beautiful; its type,
<span class="inlinecode">[</span>
<span class="inlinecode"><span style="font-family: arial;">∀</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode"><span class="id" type="var">m</span>,</span> <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode">(<span class="id" type="var">n</span>+<span class="id" type="var">m</span>),</span>
<span class="inlinecode">]</span>
expresses this functionality, in the same way that the polymorphic
type <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">list</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> in the previous chapter expressed the fact
that the constructor <span class="inlinecode"><span class="id" type="var">nil</span></span> can be thought of as a function from
types to empty lists with elements of that type.
<div class="paragraph"> </div>
This gives us an alternative way to write the proof that <span class="inlinecode">8</span> is
beautiful:
</div>
<div class="code code-tight">
<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="id" type="tactic">apply</span> (<span class="id" type="var">b_sum</span> 3 5 <span class="id" type="var">b_3</span> <span class="id" type="var">b_5</span>).<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Notice that we're using <span class="inlinecode"><span class="id" type="tactic">apply</span></span> here in a new way: instead of just
supplying the <i>name</i> of a hypothesis or previously proved theorem
whose type matches the current goal, we are supplying an
<i>expression</i> that directly builds evidence with the required
type.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab152"></a><h2 class="section">Proof Scripts and Proof Objects</h2>
<div class="paragraph"> </div>
These proof objects lie at the core of how Coq operates.
<div class="paragraph"> </div>
When Coq is following a proof script, what is happening internally
is that it is gradually constructing a proof object — a term
whose type is the proposition being proved. The tactics between
the <span class="inlinecode"><span class="id" type="keyword">Proof</span></span> command and the <span class="inlinecode"><span class="id" type="keyword">Qed</span></span> instruct Coq how to build up a
term of the required type. To see this process in action, let's
use the <span class="inlinecode"><span class="id" type="keyword">Show</span></span> <span class="inlinecode"><span class="id" type="keyword">Proof</span></span> command to display the current state of the
proof tree at various points in the following tactic proof.
</div>
<div class="code code-tight">
<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="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="id" type="keyword">Show</span> <span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_3</span>.<br/>
<span class="id" type="keyword">Show</span> <span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_5</span>.<br/>
<span class="id" type="keyword">Show</span> <span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
At any given moment, Coq has constructed a term with some
"holes" (indicated by <span class="inlinecode">?1</span>, <span class="inlinecode">?2</span>, and so on), and it knows what
type of evidence is needed at each hole. In the <span class="inlinecode"><span class="id" type="keyword">Show</span></span> <span class="inlinecode"><span class="id" type="keyword">Proof</span></span>
output, lines of the form <span class="inlinecode">?1</span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> record these
requirements. (The <span class="inlinecode"><span style="font-family: arial;">→</span></span> here has nothing to do with either
implication or function types — it is just an unfortunate choice
of concrete syntax for the output!)
<div class="paragraph"> </div>
Each of the holes corresponds to a subgoal, and the proof is
finished when there are no more subgoals. At this point, the
<span class="inlinecode"><span class="id" type="keyword">Theorem</span></span> command gives a name to the evidence we've built and
stores it in the global context.
<div class="paragraph"> </div>
Tactic proofs are useful and convenient because they avoid
building proof trees by hand, but they are not essential: in
principle, we can always construct the required evidence by hand.
Indeed, we don't even need the <span class="inlinecode"><span class="id" type="keyword">Theorem</span></span> command: we can use
<span class="inlinecode"><span class="id" type="keyword">Definition</span></span> instead, to directly give a global name to a piece of
evidence.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">eight_is_beautiful'''</span> : <span class="id" type="var">beautiful</span> 8 :=<br/>
<span class="id" type="var">b_sum</span> 3 5 <span class="id" type="var">b_3</span> <span class="id" type="var">b_5</span>.<br/>
<br/>
</div>
<div class="doc">
All these different ways of building the proof lead to exactly the
same evidence being saved in the global environment.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Print</span> <span class="id" type="var">eight_is_beautiful</span>.<br/>
<span class="comment">(* ===> eight_is_beautiful = b_sum 3 5 b_3 b_5 : beautiful 8 *)</span><br/>
<span class="id" type="keyword">Print</span> <span class="id" type="var">eight_is_beautiful'</span>.<br/>
<span class="comment">(* ===> eight_is_beautiful' = b_sum 3 5 b_3 b_5 : beautiful 8 *)</span><br/>
<span class="id" type="keyword">Print</span> <span class="id" type="var">eight_is_beautiful''</span>.<br/>
<span class="comment">(* ===> eight_is_beautiful'' = b_sum 3 5 b_3 b_5 : beautiful 8 *)</span><br/>
<span class="id" type="keyword">Print</span> <span class="id" type="var">eight_is_beautiful'''</span>.<br/>
<span class="comment">(* ===> eight_is_beautiful''' = b_sum 3 5 b_3 b_5 : beautiful 8 *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab153"></a><h4 class="section">Exercise: 1 star (six_is_beautiful)</h4>
Give a tactic proof and a proof object showing that <span class="inlinecode">6</span> is <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">six_is_beautiful</span> :<br/>
<span class="id" type="var">beautiful</span> 6.<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">Definition</span> <span class="id" type="var">six_is_beautiful'</span> : <span class="id" type="var">beautiful</span> 6 :=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab154"></a><h4 class="section">Exercise: 1 star (nine_is_beautiful)</h4>
Give a tactic proof and a proof object showing that <span class="inlinecode">9</span> is <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">nine_is_beautiful</span> :<br/>
<span class="id" type="var">beautiful</span> 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/>
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">nine_is_beautiful'</span> : <span class="id" type="var">beautiful</span> 9 :=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab155"></a><h2 class="section">Implications and Functions</h2>
<div class="paragraph"> </div>
If we want to substantiate the claim that <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>, what sort of
proof object should count as evidence?
<div class="paragraph"> </div>
We've seen one case above: the <span class="inlinecode"><span class="id" type="var">b_sum</span></span> constructor, which is
<i>primitive</i> evidence for an implication proposition — it is part
of the very meaning of the word "beautiful" in this context. But
what about other implications that we might want to prove?
<div class="paragraph"> </div>
For example, consider this statement:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">b_plus3</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> (3+<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">apply</span> <span class="id" type="var">b_sum</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">H</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
What is the proof object corresponding to <span class="inlinecode"><span class="id" type="var">b_plus3</span></span>?
<div class="paragraph"> </div>
We've made a notational pun between <span class="inlinecode"><span style="font-family: arial;">→</span></span> as implication and <span class="inlinecode"><span style="font-family: arial;">→</span></span>
as the type of functions. If we take this pun seriously, then
what we're looking for is an expression whose <i>type</i> is
<span class="inlinecode"><span style="font-family: arial;">∀</span></span> <span class="inlinecode"><span class="id" type="var">n</span>,</span> <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">beautiful</span></span> <span class="inlinecode">(3+<span class="id" type="var">n</span>)</span> — that is, a
<i>function</i> that takes two arguments (one number and a piece of
evidence) and returns a piece of evidence! Here it is:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">b_plus3'</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> (3+<span class="id" type="var">n</span>) := <br/>
<span class="id" type="keyword">fun</span> <span class="id" type="var">n</span> => <span class="id" type="keyword">fun</span> <span class="id" type="var">H</span> : <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span> =><br/>
<span class="id" type="var">b_sum</span> 3 <span class="id" type="var">n</span> <span class="id" type="var">b_3</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">b_plus3'</span>.<br/>
<span class="comment">(* ===> b_plus3' : forall n, beautiful n -> beautiful (3+n) *)</span><br/>
<br/>
</div>
<div class="doc">
Recall that <span class="inlinecode"><span class="id" type="keyword">fun</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">=></span> <span class="inlinecode"><span class="id" type="var">blah</span></span> means "the function that, given <span class="inlinecode"><span class="id" type="var">n</span></span>,
yields <span class="inlinecode"><span class="id" type="var">blah</span></span>." Another equivalent way to write this definition is:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">b_plus3''</span> (<span class="id" type="var">n</span> : <span class="id" type="var">nat</span>) (<span class="id" type="var">H</span> : <span class="id" type="var">beautiful</span> <span class="id" type="var">n</span>) : <span class="id" type="var">beautiful</span> (3+<span class="id" type="var">n</span>) := <br/>
<span class="id" type="var">b_sum</span> 3 <span class="id" type="var">n</span> <span class="id" type="var">b_3</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">b_plus3''</span>.<br/>
<span class="comment">(* ===> b_plus3'' : forall n, beautiful n -> beautiful (3+n) *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab156"></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="lab157"></a><h4 class="section">Exercise: 3 stars, optional (b_times2')</h4>
Write a proof object corresponding to <span class="inlinecode"><span class="id" type="var">b_times2</span></span> above
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</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="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab158"></a><h4 class="section">Exercise: 2 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="lab159"></a><h2 class="section">Induction Over Proof Objects</h2>
<div class="paragraph"> </div>
Since we use the keyword <span class="inlinecode"><span class="id" type="keyword">Induction</span></span> to define primitive
propositions together with their evidence, we might wonder whether
there are some sort of induction principles associated with these
definitions. Indeed there are, and in this section we'll take a
look at how they can be used.
<div class="paragraph"> </div>
Besides <i>constructing</i> evidence that numbers are beautiful, we can
also <i>reason about</i> such evidence. 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 us 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 two 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> justifying 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> can only have one
of four forms: either <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>) or <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>), or <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 <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>, and <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 beauiful 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).
<div class="paragraph"> </div>
Thus, it makes sense to use the tactics that we have already seen
for inductively defined <i>data</i> to reason instead about inductively
defined <i>evidence</i>.
<div class="paragraph"> </div>
Let's introduce a new property of numbers to help illustrate the
role of induction.
</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="lab160"></a><h4 class="section">Exercise: 1 star (gorgeous_tree)</h4>
Write out the definition of gorgeous numbers using the <i>inference
rule</i> notation.
<div class="paragraph"> </div>
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
<div class="paragraph"> </div>
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</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>.<br/>
<span class="comment">(* The argument proceeds by induction on the evidence 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="id" type="var">Case</span> "g_0".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">b_0</span>.<br/>
<span class="id" type="var">Case</span> "g_plus3".<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="id" type="var">Case</span> "g_plus5".<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/>
</div>
<div class="doc">
Let's see what happens if we try to prove this by induction on <span class="inlinecode"><span class="id" type="var">n</span></span>
instead of induction on the evidence <span class="inlinecode"><span class="id" type="var">H</span></span>.
</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="id" type="var">Case</span> "n = 0". <span class="id" type="tactic">apply</span> <span class="id" type="var">b_0</span>.<br/>
<span class="id" type="var">Case</span> "n = S n'". <span class="comment">(* We are stuck! *)</span><br/>
<span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab161"></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="lab162"></a><h4 class="section">Exercise: 2 stars (gorgeous_plus13_po):</h4>
Give the proof object for theorem <span class="inlinecode"><span class="id" type="var">gorgeous_plus13</span></span> above.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">gorgeous_plus13_po</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> (13+<span class="id" type="var">n</span>):=<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">admit</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab163"></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="lab164"></a><h4 class="section">Exercise: 3 stars (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="lab165"></a><h4 class="section">Exercise: 3 stars, optional (b_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>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab166"></a><h2 class="section">Evenness</h2>
<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. This gives us an obvious way
of defining the <i>concept</i> of evenness:
</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>
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">
This definition says that there are 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="lab167"></a><h4 class="section">Exercise: 1 star (double_even)</h4>
Construct a tactic proof of the following proposition.
</div>
<div class="code code-tight">
<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 class="paragraph"> </div>
<a name="lab168"></a><h4 class="section">Exercise: 4 stars, optional (double_even_pfobj)</h4>
Try to predict what proof object is constructed by the above
tactic proof. (Before checking your answer, you'll want to
strip out any uses of <span class="inlinecode"><span class="id" type="var">Case</span></span>, as these will make the proof
object look a bit cluttered.) <font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab169"></a><h3 class="section">Discussion: Computational vs. Inductive Definitions</h3>
<div class="paragraph"> </div>
We have seen that the proposition "some number 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 as a Coq Fixpoint (or translate it directly
into a recursive function in any other 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="lab170"></a><h2 class="section">Inverting Evidence</h2>
<div class="paragraph"> </div>
Besides induction, we can use the other tactics in our toolkit
with evidence. For example, this proof uses <span class="inlinecode"><span class="id" type="tactic">destruct</span></span> on
evidence.
</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>,<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">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">destruct</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="var">Case</span> "E = ev_0". <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="id" type="var">Case</span> "E = ev_SS n' E'". <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="lab171"></a><h4 class="section">Exercise: 1 star, optional (ev_minus2_n)</h4>
What happens if we try to <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="var">E</span></span>? <font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab172"></a><h4 class="section">Exercise: 1 star, recommended (ev__even)</h4>
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="id" type="var">Case</span> "E = ev_0".<br/>