forked from sven--/Software-Foundations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoreInd.html
1978 lines (1503 loc) · 158 KB
/
MoreInd.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>MoreInd: More on Induction</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">MoreInd<span class="subtitle">More on Induction</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> "ProofObjects".<br/>
<br/>
</div>
<div class="doc">
<a name="lab326"></a><h1 class="section">Induction Principles</h1>
<div class="paragraph"> </div>
This is a good point to pause and take a deeper look at induction
principles.
<div class="paragraph"> </div>
Every time we declare a new <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> datatype, Coq
automatically generates and proves an <i>induction principle</i>
for this type.
<div class="paragraph"> </div>
The induction principle for a type <span class="inlinecode"><span class="id" type="var">t</span></span> is called <span class="inlinecode"><span class="id" type="var">t_ind</span></span>. Here is
the one for natural numbers:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">nat_ind</span>.<br/>
<span class="comment">(* ===> nat_ind : <br/>
forall P : nat -> Prop,<br/>
P 0 -><br/>
(forall n : nat, P n -> P (S n)) -><br/>
forall n : nat, P n *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab327"></a><h3 class="section"> </h3>
The <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic is a straightforward wrapper that, at
its core, simply performs <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">t_ind</span></span>. To see this more
clearly, let's experiment a little with using <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">nat_ind</span></span>
directly, instead of the <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic, to carry out some
proofs. Here, for example, is an alternate proof of a theorem
that we saw in the <span class="inlinecode"><span class="id" type="var">Basics</span></span> chapter.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">mult_0_r'</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>:<span class="id" type="var">nat</span>, <br/>
<span class="id" type="var">n</span> × 0 = 0.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">nat_ind</span>.<br/>
<span class="id" type="var">Case</span> "O". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "S". <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">IHn</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">IHn</span>.<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
This proof is basically the same as the earlier one, but a
few minor differences are worth noting. First, in the induction
step of the proof (the <span class="inlinecode">"<span class="id" type="var">S</span>"</span> case), we have to do a little
bookkeeping manually (the <span class="inlinecode"><span class="id" type="tactic">intros</span></span>) that <span class="inlinecode"><span class="id" type="tactic">induction</span></span> does
automatically.
<div class="paragraph"> </div>
Second, we do not introduce <span class="inlinecode"><span class="id" type="var">n</span></span> into the context before applying
<span class="inlinecode"><span class="id" type="var">nat_ind</span></span> — the conclusion of <span class="inlinecode"><span class="id" type="var">nat_ind</span></span> is a quantified formula,
and <span class="inlinecode"><span class="id" type="tactic">apply</span></span> needs this conclusion to exactly match the shape of
the goal state, including the quantifier. The <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic
works either with a variable in the context or a quantified
variable in the goal.
<div class="paragraph"> </div>
Third, the <span class="inlinecode"><span class="id" type="tactic">apply</span></span> tactic automatically chooses variable names for
us (in the second subgoal, here), whereas <span class="inlinecode"><span class="id" type="tactic">induction</span></span> lets us
specify (with the <span class="inlinecode"><span class="id" type="keyword">as</span>...</span> clause) what names should be used. The
automatic choice is actually a little unfortunate, since it
re-uses the name <span class="inlinecode"><span class="id" type="var">n</span></span> for a variable that is different from the <span class="inlinecode"><span class="id" type="var">n</span></span>
in the original theorem. This is why the <span class="inlinecode"><span class="id" type="var">Case</span></span> annotation is
just <span class="inlinecode"><span class="id" type="var">S</span></span> — if we tried to write it out in the more explicit form
that we've been using for most proofs, we'd have to write <span class="inlinecode"><span class="id" type="var">n</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">n</span></span>, which doesn't make a lot of sense! All of these conveniences
make <span class="inlinecode"><span class="id" type="tactic">induction</span></span> nicer to use in practice than applying induction
principles like <span class="inlinecode"><span class="id" type="var">nat_ind</span></span> directly. But it is important to
realize that, modulo this little bit of bookkeeping, applying
<span class="inlinecode"><span class="id" type="var">nat_ind</span></span> is what we are really doing.
<div class="paragraph"> </div>
<a name="lab328"></a><h4 class="section">Exercise: 2 stars, optional (plus_one_r')</h4>
Complete this proof as we did <span class="inlinecode"><span class="id" type="var">mult_0_r'</span></span> above, without using
the <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">plus_one_r'</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>:<span class="id" type="var">nat</span>, <br/>
<span class="id" type="var">n</span> + 1 = <span class="id" type="var">S</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>
Coq generates induction principles for every datatype defined with
<span class="inlinecode"><span class="id" type="keyword">Inductive</span></span>, including those that aren't recursive. (Although
we don't need induction to prove properties of non-recursive
datatypes, the idea of an induction principle still makes sense
for them: it gives a way to prove that a property holds for all
values of the type.)
<div class="paragraph"> </div>
These generated principles follow a similar pattern. If we define a
type <span class="inlinecode"><span class="id" type="var">t</span></span> with constructors <span class="inlinecode"><span class="id" type="var">c1</span></span> ... <span class="inlinecode"><span class="id" type="var">cn</span></span>, Coq generates a theorem
with this shape:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">t_ind</span> :<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">P</span> : <span class="id" type="var">t</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>,<br/>
... <span class="id" type="tactic">case</span> <span class="id" type="keyword">for</span> <span class="id" type="var">c1</span> ... <span style="font-family: arial;">→</span><br/>
... <span class="id" type="tactic">case</span> <span class="id" type="keyword">for</span> <span class="id" type="var">c2</span> ... <span style="font-family: arial;">→</span><br/>
...<br/>
... <span class="id" type="tactic">case</span> <span class="id" type="keyword">for</span> <span class="id" type="var">cn</span> ... <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">n</span> : <span class="id" type="var">t</span>, <span class="id" type="var">P</span> <span class="id" type="var">n</span>
<div class="paragraph"> </div>
</div>
The specific shape of each case depends on the arguments to the
corresponding constructor. Before trying to write down a general
rule, let's look at some more examples. First, an example where
the constructors take no arguments:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">yesno</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">yes</span> : <span class="id" type="var">yesno</span><br/>
| <span class="id" type="var">no</span> : <span class="id" type="var">yesno</span>.<br/>
<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">yesno_ind</span>.<br/>
<span class="comment">(* ===> yesno_ind : forall P : yesno -> Prop, <br/>
P yes -><br/>
P no -><br/>
forall y : yesno, P y *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab329"></a><h4 class="section">Exercise: 1 star, optional (rgb)</h4>
Write out the induction principle that Coq will generate for the
following datatype. Write down your answer on paper or type it
into a comment, and then compare it with what Coq prints.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">rgb</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="tactic">red</span> : <span class="id" type="var">rgb</span><br/>
| <span class="id" type="var">green</span> : <span class="id" type="var">rgb</span><br/>
| <span class="id" type="var">blue</span> : <span class="id" type="var">rgb</span>.<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">rgb_ind</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
Here's another example, this time with one of the constructors
taking some arguments.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">natlist</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">nnil</span> : <span class="id" type="var">natlist</span><br/>
| <span class="id" type="var">ncons</span> : <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="var">natlist</span> <span style="font-family: arial;">→</span> <span class="id" type="var">natlist</span>.<br/>
<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">natlist_ind</span>.<br/>
<span class="comment">(* ===> (modulo a little variable renaming for clarity)<br/>
natlist_ind :<br/>
forall P : natlist -> Prop,<br/>
P nnil -><br/>
(forall (n : nat) (l : natlist), P l -> P (ncons n l)) -><br/>
forall n : natlist, P n *)</span><br/>
<br/>
</div>
<div class="doc">
<a name="lab330"></a><h4 class="section">Exercise: 1 star, optional (natlist1)</h4>
Suppose we had written the above definition a little
differently:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">natlist1</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">nnil1</span> : <span class="id" type="var">natlist1</span><br/>
| <span class="id" type="var">nsnoc1</span> : <span class="id" type="var">natlist1</span> <span style="font-family: arial;">→</span> <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="var">natlist1</span>.<br/>
<br/>
</div>
<div class="doc">
Now what will the induction principle look like? <font size=-2>☐</font>
<div class="paragraph"> </div>
From these examples, we can extract this general rule:
<div class="paragraph"> </div>
<ul class="doclist">
<li> The type declaration gives several constructors; each
corresponds to one clause of the induction principle.
</li>
<li> Each constructor <span class="inlinecode"><span class="id" type="var">c</span></span> takes argument types <span class="inlinecode"><span class="id" type="var">a1</span></span>...<span class="inlinecode"><span class="id" type="var">an</span></span>.
</li>
<li> Each <span class="inlinecode"><span class="id" type="var">ai</span></span> can be either <span class="inlinecode"><span class="id" type="var">t</span></span> (the datatype we are defining) or
some other type <span class="inlinecode"><span class="id" type="var">s</span></span>.
</li>
<li> The corresponding case of the induction principle
says (in English):
<ul class="doclist">
<li> "for all values <span class="inlinecode"><span class="id" type="var">x1</span></span>...<span class="inlinecode"><span class="id" type="var">xn</span></span> of types <span class="inlinecode"><span class="id" type="var">a1</span></span>...<span class="inlinecode"><span class="id" type="var">an</span></span>, if <span class="inlinecode"><span class="id" type="var">P</span></span>
holds for each of the inductive arguments (each <span class="inlinecode"><span class="id" type="var">xi</span></span> of
type <span class="inlinecode"><span class="id" type="var">t</span></span>), then <span class="inlinecode"><span class="id" type="var">P</span></span> holds for <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">x1</span></span> <span class="inlinecode">...</span> <span class="inlinecode"><span class="id" type="var">xn</span></span>".
</li>
</ul>
</li>
</ul>
<div class="paragraph"> </div>
<div class="paragraph"> </div>
<a name="lab331"></a><h4 class="section">Exercise: 1 star, optional (byntree_ind)</h4>
Write out the induction principle that Coq will generate for the
following datatype. Write down your answer on paper or type it
into a comment, and then compare it with what Coq prints.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">byntree</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">bempty</span> : <span class="id" type="var">byntree</span> <br/>
| <span class="id" type="var">bleaf</span> : <span class="id" type="var">yesno</span> <span style="font-family: arial;">→</span> <span class="id" type="var">byntree</span><br/>
| <span class="id" type="var">nbranch</span> : <span class="id" type="var">yesno</span> <span style="font-family: arial;">→</span> <span class="id" type="var">byntree</span> <span style="font-family: arial;">→</span> <span class="id" type="var">byntree</span> <span style="font-family: arial;">→</span> <span class="id" type="var">byntree</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab332"></a><h4 class="section">Exercise: 1 star, optional (ex_set)</h4>
Here is an induction principle for an inductively defined
set.
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">ExSet_ind</span> :<br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">P</span> : <span class="id" type="var">ExSet</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>,<br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">b</span> : <span class="id" type="var">bool</span>, <span class="id" type="var">P</span> (<span class="id" type="var">con1</span> <span class="id" type="var">b</span>)) <span style="font-family: arial;">→</span><br/>
(<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">e</span> : <span class="id" type="var">ExSet</span>), <span class="id" type="var">P</span> <span class="id" type="var">e</span> <span style="font-family: arial;">→</span> <span class="id" type="var">P</span> (<span class="id" type="var">con2</span> <span class="id" type="var">n</span> <span class="id" type="var">e</span>)) <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">e</span> : <span class="id" type="var">ExSet</span>, <span class="id" type="var">P</span> <span class="id" type="var">e</span>
<div class="paragraph"> </div>
</div>
Give an <span class="inlinecode"><span class="id" type="keyword">Inductive</span></span> definition of <span class="inlinecode"><span class="id" type="var">ExSet</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">ExSet</span> : <span class="id" type="keyword">Type</span> :=<br/>
<span class="comment">(* FILL IN HERE *)</span><br/>
.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
What about polymorphic datatypes?
<div class="paragraph"> </div>
The inductive definition of polymorphic lists
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">list</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">nil</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span><br/>
| <span class="id" type="var">cons</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</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="var">list</span> <span class="id" type="var">X</span>.
<div class="paragraph"> </div>
</div>
is very similar to that of <span class="inlinecode"><span class="id" type="var">natlist</span></span>. The main difference is
that, here, the whole definition is <i>parameterized</i> on a set <span class="inlinecode"><span class="id" type="var">X</span></span>:
that is, we are defining a <i>family</i> of inductive types <span class="inlinecode"><span class="id" type="var">list</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span>,
one for each <span class="inlinecode"><span class="id" type="var">X</span></span>. (Note that, wherever <span class="inlinecode"><span class="id" type="var">list</span></span> appears in the body
of the declaration, it is always applied to the parameter <span class="inlinecode"><span class="id" type="var">X</span></span>.)
The induction principle is likewise parameterized on <span class="inlinecode"><span class="id" type="var">X</span></span>:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">list_ind</span> :<br/>
<span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>) (<span class="id" type="var">P</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">P</span> [] <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span>(<span class="id" type="var">x</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">P</span> <span class="id" type="var">l</span> <span style="font-family: arial;">→</span> <span class="id" type="var">P</span> (<span class="id" type="var">x</span> :: <span class="id" type="var">l</span>)) <span style="font-family: arial;">→</span><br/>
<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">P</span> <span class="id" type="var">l</span>
<div class="paragraph"> </div>
</div>
Note the wording here (and, accordingly, the form of <span class="inlinecode"><span class="id" type="var">list_ind</span></span>):
The <i>whole</i> induction principle is parameterized on <span class="inlinecode"><span class="id" type="var">X</span></span>. That is,
<span class="inlinecode"><span class="id" type="var">list_ind</span></span> can be thought of as a polymorphic function that, when
applied to a type <span class="inlinecode"><span class="id" type="var">X</span></span>, gives us back an induction principle
specialized to the type <span class="inlinecode"><span class="id" type="var">list</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span>.
<div class="paragraph"> </div>
<a name="lab333"></a><h4 class="section">Exercise: 1 star, optional (tree)</h4>
Write out the induction principle that Coq will generate for
the following datatype. Compare your answer with what Coq
prints.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">tree</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">leaf</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tree</span> <span class="id" type="var">X</span><br/>
| <span class="id" type="var">node</span> : <span class="id" type="var">tree</span> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tree</span> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">tree</span> <span class="id" type="var">X</span>.<br/>
<span class="id" type="keyword">Check</span> <span class="id" type="var">tree_ind</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab334"></a><h4 class="section">Exercise: 1 star, optional (mytype)</h4>
Find an inductive definition that gives rise to the
following induction principle:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">mytype_ind</span> :<br/>
<span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>) (<span class="id" type="var">P</span> : <span class="id" type="var">mytype</span> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>),<br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">x</span> : <span class="id" type="var">X</span>, <span class="id" type="var">P</span> (<span class="id" type="var">constr1</span> <span class="id" type="var">X</span> <span class="id" type="var">x</span>)) <span style="font-family: arial;">→</span><br/>
(<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">P</span> (<span class="id" type="var">constr2</span> <span class="id" type="var">X</span> <span class="id" type="var">n</span>)) <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">m</span> : <span class="id" type="var">mytype</span> <span class="id" type="var">X</span>, <span class="id" type="var">P</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span> <br/>
<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">P</span> (<span class="id" type="var">constr3</span> <span class="id" type="var">X</span> <span class="id" type="var">m</span> <span class="id" type="var">n</span>)) <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">m</span> : <span class="id" type="var">mytype</span> <span class="id" type="var">X</span>, <span class="id" type="var">P</span> <span class="id" type="var">m</span>
<div class="paragraph"> </div>
</div>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab335"></a><h4 class="section">Exercise: 1 star, optional (foo)</h4>
Find an inductive definition that gives rise to the
following induction principle:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">foo_ind</span> :<br/>
<span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span> <span class="id" type="var">Y</span> : <span class="id" type="keyword">Type</span>) (<span class="id" type="var">P</span> : <span class="id" type="var">foo</span> <span class="id" type="var">X</span> <span class="id" type="var">Y</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>),<br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">x</span> : <span class="id" type="var">X</span>, <span class="id" type="var">P</span> (<span class="id" type="var">bar</span> <span class="id" type="var">X</span> <span class="id" type="var">Y</span> <span class="id" type="var">x</span>)) <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">y</span> : <span class="id" type="var">Y</span>, <span class="id" type="var">P</span> (<span class="id" type="var">baz</span> <span class="id" type="var">X</span> <span class="id" type="var">Y</span> <span class="id" type="var">y</span>)) <span style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">f1</span> : <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="var">foo</span> <span class="id" type="var">X</span> <span class="id" type="var">Y</span>,<br/>
(<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">P</span> (<span class="id" type="var">f1</span> <span class="id" type="var">n</span>)) <span style="font-family: arial;">→</span> <span class="id" type="var">P</span> (<span class="id" type="var">quux</span> <span class="id" type="var">X</span> <span class="id" type="var">Y</span> <span class="id" type="var">f1</span>)) <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">f2</span> : <span class="id" type="var">foo</span> <span class="id" type="var">X</span> <span class="id" type="var">Y</span>, <span class="id" type="var">P</span> <span class="id" type="var">f2</span>
<div class="paragraph"> </div>
</div>
<font size=-2>☐</font>
<div class="paragraph"> </div>
<a name="lab336"></a><h4 class="section">Exercise: 1 star, optional (foo')</h4>
Consider the following inductive definition:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">foo'</span> (<span class="id" type="var">X</span>:<span class="id" type="keyword">Type</span>) : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">C1</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="var">foo'</span> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="var">foo'</span> <span class="id" type="var">X</span><br/>
| <span class="id" type="var">C2</span> : <span class="id" type="var">foo'</span> <span class="id" type="var">X</span>.<br/>
<br/>
</div>
<div class="doc">
What induction principle will Coq generate for <span class="inlinecode"><span class="id" type="var">foo'</span></span>? Fill
in the blanks, then check your answer with Coq.)
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">foo'_ind</span> :<br/>
<span style="font-family: arial;">∀</span>(<span class="id" type="var">X</span> : <span class="id" type="keyword">Type</span>) (<span class="id" type="var">P</span> : <span class="id" type="var">foo'</span> <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>),<br/>
(<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">f</span> : <span class="id" type="var">foo'</span> <span class="id" type="var">X</span>),<br/>
<span class="id" type="var">_______________________</span> <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">_______________________</span> ) <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">___________________________________________</span> <span style="font-family: arial;">→</span><br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">f</span> : <span class="id" type="var">foo'</span> <span class="id" type="var">X</span>, <span class="id" type="var">________________________</span>
<div class="paragraph"> </div>
</div>
<div class="paragraph"> </div>
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab337"></a><h2 class="section">Induction Hypotheses</h2>
<div class="paragraph"> </div>
Where does the phrase "induction hypothesis" fit into this story?
<div class="paragraph"> </div>
The induction principle for numbers
<div class="paragraph"> </div>
<div class="code code-tight">
<span style="font-family: arial;">∀</span><span class="id" type="var">P</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">P</span> 0 <span style="font-family: arial;">→</span><br/>
(<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">P</span> <span class="id" type="var">n</span> <span style="font-family: arial;">→</span> <span class="id" type="var">P</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>)) <span style="font-family: arial;">→</span><br/>
<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">P</span> <span class="id" type="var">n</span>
<div class="paragraph"> </div>
</div>
is a generic statement that holds for all propositions
<span class="inlinecode"><span class="id" type="var">P</span></span> (strictly speaking, for all families of propositions <span class="inlinecode"><span class="id" type="var">P</span></span>
indexed by a number <span class="inlinecode"><span class="id" type="var">n</span></span>). Each time we use this principle, we
are choosing <span class="inlinecode"><span class="id" type="var">P</span></span> to be a particular expression of type
<span class="inlinecode"><span class="id" type="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="keyword">Prop</span></span>.
<div class="paragraph"> </div>
We can make the proof more explicit by giving this expression a
name. For example, instead of stating the theorem <span class="inlinecode"><span class="id" type="var">mult_0_r</span></span> as
"<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">n</span></span> <span class="inlinecode">×</span> <span class="inlinecode">0</span> <span class="inlinecode">=</span> <span class="inlinecode">0</span>," we can write it as "<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">P_m0r</span></span>
<span class="inlinecode"><span class="id" type="var">n</span></span>", where <span class="inlinecode"><span class="id" type="var">P_m0r</span></span> is defined as...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">P_m0r</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">n</span> × 0 = 0.<br/>
<br/>
</div>
<div class="doc">
... or equivalently...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Definition</span> <span class="id" type="var">P_m0r'</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="keyword">fun</span> <span class="id" type="var">n</span> ⇒ <span class="id" type="var">n</span> × 0 = 0.<br/>
<br/>
</div>
<div class="doc">
Now when we do the proof it is easier to see where <span class="inlinecode"><span class="id" type="var">P_m0r</span></span>
appears.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">mult_0_r''</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>:<span class="id" type="var">nat</span>, <br/>
<span class="id" type="var">P_m0r</span> <span class="id" type="var">n</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">nat_ind</span>.<br/>
<span class="id" type="var">Case</span> "n = O". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "n = S n'".<br/>
<span class="comment">(* Note the proof state at this point! *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">IHn</span>.<br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">P_m0r</span> <span class="id" type="keyword">in</span> <span class="id" type="var">IHn</span>. <span class="id" type="tactic">unfold</span> <span class="id" type="var">P_m0r</span>. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">IHn</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
This extra naming step isn't something that we'll do in
normal proofs, but it is useful to do it explicitly for an example
or two, because it allows us to see exactly what the induction
hypothesis is. If we prove <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">P_m0r</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> by induction on
<span class="inlinecode"><span class="id" type="var">n</span></span> (using either <span class="inlinecode"><span class="id" type="tactic">induction</span></span> or <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">nat_ind</span></span>), we see that the
first subgoal requires us to prove <span class="inlinecode"><span class="id" type="var">P_m0r</span></span> <span class="inlinecode">0</span> ("<span class="inlinecode"><span class="id" type="var">P</span></span> holds for
zero"), while the second subgoal requires us to prove <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">P_m0r</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">P_m0r</span></span> <span class="inlinecode"><span class="id" type="var">n'</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n'</span>)</span> (that is "<span class="inlinecode"><span class="id" type="var">P</span></span> holds of <span class="inlinecode"><span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n'</span></span> if it
holds of <span class="inlinecode"><span class="id" type="var">n'</span></span>" or, more elegantly, "<span class="inlinecode"><span class="id" type="var">P</span></span> is preserved by <span class="inlinecode"><span class="id" type="var">S</span></span>").
The <i>induction hypothesis</i> is the premise of this latter
implication — the assumption that <span class="inlinecode"><span class="id" type="var">P</span></span> holds of <span class="inlinecode"><span class="id" type="var">n'</span></span>, which we are
allowed to use in proving that <span class="inlinecode"><span class="id" type="var">P</span></span> holds for <span class="inlinecode"><span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n'</span></span>.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab338"></a><h2 class="section">More on the <span class="inlinecode"><span class="id" type="tactic">induction</span></span> Tactic</h2>
<div class="paragraph"> </div>
The <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic actually does even more low-level
bookkeeping for us than we discussed above.
<div class="paragraph"> </div>
Recall the informal statement of the induction principle for
natural numbers:
<div class="paragraph"> </div>
<ul class="doclist">
<li> If <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> is some proposition involving a natural number n, and
we want to show that P holds for <i>all</i> numbers n, we can
reason like this:
<ul class="doclist">
<li> show that <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">O</span></span> holds
</li>
<li> show that, if <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">n'</span></span> holds, then so does <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n'</span>)</span>
</li>
<li> conclude that <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> holds for all n.
</li>
</ul>
</li>
</ul>
So, when we begin a proof with <span class="inlinecode"><span class="id" type="tactic">intros</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> and then <span class="inlinecode"><span class="id" type="tactic">induction</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span>,
we are first telling Coq to consider a <i>particular</i> <span class="inlinecode"><span class="id" type="var">n</span></span> (by
introducing it into the context) and then telling it to prove
something about <i>all</i> numbers (by using induction).
<div class="paragraph"> </div>
What Coq actually does in this situation, internally, is to
"re-generalize" the variable we perform induction on. For
example, in our original proof that <span class="inlinecode"><span class="id" type="var">plus</span></span> is associative...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">plus_assoc'</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> : <span class="id" type="var">nat</span>, <br/>
<span class="id" type="var">n</span> + (<span class="id" type="var">m</span> + <span class="id" type="var">p</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="keyword">Proof</span>.<br/>
<span class="comment">(* ...we first introduce all 3 variables into the context,<br/>
which amounts to saying "Consider an arbitrary <span class="inlinecode"><span class="id" type="var">n</span></span>, <span class="inlinecode"><span class="id" type="var">m</span></span>, and<br/>
<span class="inlinecode"><span class="id" type="var">p</span></span>..." *)</span><br/>
<span class="id" type="tactic">intros</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="comment">(* ...We now use the <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic to prove <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> (that<br/>
is, <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">+</span> <span class="inlinecode">(<span class="id" type="var">m</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">p</span>)</span> <span class="inlinecode">=</span> <span class="inlinecode">(<span class="id" type="var">n</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">m</span>)</span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">p</span></span>) for _all_ <span class="inlinecode"><span class="id" type="var">n</span></span>,<br/>
and hence also for the particular <span class="inlinecode"><span class="id" type="var">n</span></span> that is in the context<br/>
at the moment. *)</span><br/>
<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 = O". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "n = S n'".<br/>
<span class="comment">(* In the second subgoal generated by <span class="inlinecode"><span class="id" type="tactic">induction</span></span> -- the<br/>
"inductive step" -- we must prove that <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">n'</span></span> implies <br/>
<span class="inlinecode"><span class="id" type="var">P</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 all <span class="inlinecode"><span class="id" type="var">n'</span></span>. The <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic <br/>
automatically introduces <span class="inlinecode"><span class="id" type="var">n'</span></span> and <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">n'</span></span> into the context<br/>
for us, leaving just <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n'</span>)</span> as the goal. *)</span><br/>
<span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">IHn'</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
It also works to apply <span class="inlinecode"><span class="id" type="tactic">induction</span></span> to a variable that is
quantified in the goal.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">plus_comm'</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">nat</span>, <br/>
<span class="id" type="var">n</span> + <span class="id" type="var">m</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="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 = O". <span class="id" type="tactic">intros</span> <span class="id" type="var">m</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">plus_0_r</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "n = S n'". <span class="id" type="tactic">intros</span> <span class="id" type="var">m</span>. <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">IHn'</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">plus_n_Sm</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Note that <span class="inlinecode"><span class="id" type="tactic">induction</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> leaves <span class="inlinecode"><span class="id" type="var">m</span></span> still bound in the goal —
i.e., what we are proving inductively is a statement beginning
with <span class="inlinecode"><span style="font-family: arial;">∀</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span>.
<div class="paragraph"> </div>
If we do <span class="inlinecode"><span class="id" type="tactic">induction</span></span> on a variable that is quantified in the goal
<i>after</i> some other quantifiers, the <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic will
automatically introduce the variables bound by these quantifiers
into the context.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">plus_comm''</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">nat</span>, <br/>
<span class="id" type="var">n</span> + <span class="id" type="var">m</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">(* Let's do induction on <span class="inlinecode"><span class="id" type="var">m</span></span> this time, instead of <span class="inlinecode"><span class="id" type="var">n</span></span>... *)</span><br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">m</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">m'</span>].<br/>
<span class="id" type="var">Case</span> "m = O". <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">plus_0_r</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "m = S m'". <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">IHm'</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">plus_n_Sm</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab339"></a><h4 class="section">Exercise: 1 star, optional (plus_explicit_prop)</h4>
Rewrite both <span class="inlinecode"><span class="id" type="var">plus_assoc'</span></span> and <span class="inlinecode"><span class="id" type="var">plus_comm'</span></span> and their proofs in
the same style as <span class="inlinecode"><span class="id" type="var">mult_0_r''</span></span> above — that is, for each theorem,
give an explicit <span class="inlinecode"><span class="id" type="keyword">Definition</span></span> of the proposition being proved by
induction, and state the theorem and proof in terms of this
defined proposition.
</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="lab340"></a><h2 class="section">Generalizing Inductions.</h2>
<div class="paragraph"> </div>
One potentially confusing feature of the <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic is
that it happily lets you try to set up an induction over a term
that isn't sufficiently general. The net effect of this will be
to lose information (much as <span class="inlinecode"><span class="id" type="tactic">destruct</span></span> can do), and leave
you unable to complete the proof. Here's an example:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">one_not_beautiful_FAILED</span>: ¬ <span class="id" type="var">beautiful</span> 1.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">H</span>.<br/>
<span class="comment">(* Just doing an <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> on <span class="inlinecode"><span class="id" type="var">H</span></span> won't get us very far in the <span class="inlinecode"><span class="id" type="var">b_sum</span></span><br/>
case. (Try it!). So we'll need induction. A naive first attempt: *)</span><br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">H</span>.<br/>
<span class="comment">(* But now, although we get four cases, as we would expect from<br/>
the definition of <span class="inlinecode"><span class="id" type="var">beautiful</span></span>, we lose all information about <span class="inlinecode"><span class="id" type="var">H</span></span> ! *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
The problem is that <span class="inlinecode"><span class="id" type="tactic">induction</span></span> over a Prop only works properly over
completely general instances of the Prop, i.e. one in which all
the arguments are free (unconstrained) variables.
In this respect it behaves more
like <span class="inlinecode"><span class="id" type="tactic">destruct</span></span> than like <span class="inlinecode"><span class="id" type="tactic">inversion</span></span>.
<div class="paragraph"> </div>
When you're tempted to do use <span class="inlinecode"><span class="id" type="tactic">induction</span></span> like this, it is generally
an indication that you need to be proving something more general.
But in some cases, it suffices to pull out any concrete arguments
into separate equations, like this:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">one_not_beautiful</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">n</span> = 1 <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">E</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">p</span> <span class="id" type="var">q</span> <span class="id" type="var">Hp</span> <span class="id" type="var">IHp</span> <span class="id" type="var">Hq</span> <span class="id" type="var">IHq</span>].<br/>
<span class="id" type="var">Case</span> "b_0".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">E</span>.<br/>
<span class="id" type="var">Case</span> "b_3".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">E</span>.<br/>
<span class="id" type="var">Case</span> "b_5".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">E</span>.<br/>
<span class="id" type="var">Case</span> "b_sum".<br/>
<span class="comment">(* the rest is a tedious case analysis *)</span><br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">p</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">p'</span>].<br/>
<span class="id" type="var">SCase</span> "p = 0".<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">q</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">q'</span>].<br/>
<span class="id" type="var">SSCase</span> "q = 0".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">E</span>.<br/>
<span class="id" type="var">SSCase</span> "q = S q'".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHq</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">E</span>.<br/>
<span class="id" type="var">SCase</span> "p = S p'".<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">q</span> <span class="id" type="keyword">as</span> [|<span class="id" type="var">q'</span>].<br/>
<span class="id" type="var">SSCase</span> "q = 0".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHp</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">plus_0_r</span> <span class="id" type="keyword">in</span> <span class="id" type="var">E</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">E</span>.<br/>
<span class="id" type="var">SSCase</span> "q = S q'".<br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">E</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">E</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">p'</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H0</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H0</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
There's a handy <span class="inlinecode"><span class="id" type="var">remember</span></span> tactic that can generate the second
proof state out of the original one.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">one_not_beautiful'</span>: ¬ <span class="id" type="var">beautiful</span> 1.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">remember</span> 1 <span class="id" type="keyword">as</span> <span class="id" type="var">n</span> <span class="id" type="var">eqn</span>:<span class="id" type="var">E</span>.<br/>
<span class="comment">(* now carry on as above *)</span><br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">Admitted</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab341"></a><h1 class="section">Informal Proofs (Advanced)</h1>
<div class="paragraph"> </div>
Q: What is the relation between a formal proof of a proposition
<span class="inlinecode"><span class="id" type="var">P</span></span> and an informal proof of the same proposition <span class="inlinecode"><span class="id" type="var">P</span></span>?
<div class="paragraph"> </div>
A: The latter should <i>teach</i> the reader how to produce the
former.
<div class="paragraph"> </div>
Q: How much detail is needed??
<div class="paragraph"> </div>
Unfortunately, There is no single right answer; rather, there is a
range of choices.
<div class="paragraph"> </div>
At one end of the spectrum, we can essentially give the reader the
whole formal proof (i.e., the informal proof amounts to just
transcribing the formal one into words). This gives the reader
the <i>ability</i> to reproduce the formal one for themselves, but it
doesn't <i>teach</i> them anything.
<div class="paragraph"> </div>
At the other end of the spectrum, we can say "The theorem is true
and you can figure out why for yourself if you think about it hard
enough." This is also not a good teaching strategy, because
usually writing the proof requires some deep insights into the
thing we're proving, and most readers will give up before they
rediscover all the same insights as we did.
<div class="paragraph"> </div>
In the middle is the golden mean — a proof that includes all of
the essential insights (saving the reader the hard part of work
that we went through to find the proof in the first place) and
clear high-level suggestions for the more routine parts to save the
reader from spending too much time reconstructing these
parts (e.g., what the IH says and what must be shown in each case
of an inductive proof), but not so much detail that the main ideas
are obscured.
<div class="paragraph"> </div>
Another key point: if we're comparing a formal proof of a
proposition <span class="inlinecode"><span class="id" type="var">P</span></span> and an informal proof of <span class="inlinecode"><span class="id" type="var">P</span></span>, the proposition <span class="inlinecode"><span class="id" type="var">P</span></span>
doesn't change. That is, formal and informal proofs are <i>talking
about the same world</i> and they <i>must play by the same rules</i>. <a name="lab342"></a><h2 class="section">Informal Proofs by Induction</h2>
<div class="paragraph"> </div>
Since we've spent much of this chapter looking "under the hood" at
formal proofs by induction, now is a good moment to talk a little
about <i>informal</i> proofs by induction.
<div class="paragraph"> </div>
In the real world of mathematical communication, written proofs
range from extremely longwinded and pedantic to extremely brief
and telegraphic. The ideal is somewhere in between, of course,
but while you are getting used to the style it is better to start
out at the pedantic end. Also, during the learning phase, it is
probably helpful to have a clear standard to compare against.
With this in mind, we offer two templates below — one for proofs
by induction over <i>data</i> (i.e., where the thing we're doing
induction on lives in <span class="inlinecode"><span class="id" type="keyword">Type</span></span>) and one for proofs by induction over
<i>evidence</i> (i.e., where the inductively defined thing lives in
<span class="inlinecode"><span class="id" type="keyword">Prop</span></span>). In the rest of this course, please follow one of the two
for <i>all</i> of your inductive proofs.
<div class="paragraph"> </div>
<a name="lab343"></a><h3 class="section">Induction Over an Inductively Defined Set</h3>
<div class="paragraph"> </div>
<i>Template</i>:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <i>Theorem</i>: <Universally quantified proposition of the form
"For all <span class="inlinecode"><span class="id" type="var">n</span>:<span class="id" type="var">S</span></span>, <span class="inlinecode"><span class="id" type="var">P</span>(<span class="id" type="var">n</span>)</span>," where <span class="inlinecode"><span class="id" type="var">S</span></span> is some inductively defined
set.>
<div class="paragraph"> </div>
<i>Proof</i>: By induction on <span class="inlinecode"><span class="id" type="var">n</span></span>.
<div class="paragraph"> </div>
<one case for each constructor <span class="inlinecode"><span class="id" type="var">c</span></span> of <span class="inlinecode"><span class="id" type="var">S</span></span>...>
<div class="paragraph"> </div>
<ul class="doclist">
<li> Suppose <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">a1</span></span> <span class="inlinecode">...</span> <span class="inlinecode"><span class="id" type="var">ak</span></span>, where <...and here we state
the IH for each of the <span class="inlinecode"><span class="id" type="var">a</span></span>'s that has type <span class="inlinecode"><span class="id" type="var">S</span></span>, if any>.
We must show <...and here we restate <span class="inlinecode"><span class="id" type="var">P</span>(<span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">a1</span></span> <span class="inlinecode">...</span> <span class="inlinecode"><span class="id" type="var">ak</span>)</span>>.
<div class="paragraph"> </div>
<go on and prove <span class="inlinecode"><span class="id" type="var">P</span>(<span class="id" type="var">n</span>)</span> to finish the case...>
<div class="paragraph"> </div>
</li>
<li> <other cases similarly...> <font size=-2>☐</font>
</li>
</ul>
</li>
</ul>
<div class="paragraph"> </div>
<i>Example</i>:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <i>Theorem</i>: For all sets <span class="inlinecode"><span class="id" type="var">X</span></span>, lists <span class="inlinecode"><span class="id" type="var">l</span></span> <span class="inlinecode">:</span> <span class="inlinecode"><span class="id" type="var">list</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span>, and numbers
<span class="inlinecode"><span class="id" type="var">n</span></span>, if <span class="inlinecode"><span class="id" type="var">length</span></span> <span class="inlinecode"><span class="id" type="var">l</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">n</span></span> then <span class="inlinecode"><span class="id" type="var">index</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n</span>)</span> <span class="inlinecode"><span class="id" type="var">l</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">None</span></span>.
<div class="paragraph"> </div>
<i>Proof</i>: By induction on <span class="inlinecode"><span class="id" type="var">l</span></span>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> Suppose <span class="inlinecode"><span class="id" type="var">l</span></span> <span class="inlinecode">=</span> <span class="inlinecode">[]</span>. We must show, for all numbers <span class="inlinecode"><span class="id" type="var">n</span></span>,
that, if length <span class="inlinecode">[]</span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">n</span></span>, then <span class="inlinecode"><span class="id" type="var">index</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n</span>)</span> <span class="inlinecode">[]</span> <span class="inlinecode">=</span>
<span class="inlinecode"><span class="id" type="var">None</span></span>.
<div class="paragraph"> </div>
This follows immediately from the definition of index.
<div class="paragraph"> </div>
</li>
<li> Suppose <span class="inlinecode"><span class="id" type="var">l</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">::</span> <span class="inlinecode"><span class="id" type="var">l'</span></span> for some <span class="inlinecode"><span class="id" type="var">x</span></span> and <span class="inlinecode"><span class="id" type="var">l'</span></span>, where
<span class="inlinecode"><span class="id" type="var">length</span></span> <span class="inlinecode"><span class="id" type="var">l'</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">n'</span></span> implies <span class="inlinecode"><span class="id" type="var">index</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n'</span>)</span> <span class="inlinecode"><span class="id" type="var">l'</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">None</span></span>, for
any number <span class="inlinecode"><span class="id" type="var">n'</span></span>. We must show, for all <span class="inlinecode"><span class="id" type="var">n</span></span>, that, if
<span class="inlinecode"><span class="id" type="var">length</span></span> <span class="inlinecode">(<span class="id" type="var">x</span>::<span class="id" type="var">l'</span>)</span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">n</span></span> then <span class="inlinecode"><span class="id" type="var">index</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n</span>)</span> <span class="inlinecode">(<span class="id" type="var">x</span>::<span class="id" type="var">l'</span>)</span> <span class="inlinecode">=</span>
<span class="inlinecode"><span class="id" type="var">None</span></span>.
<div class="paragraph"> </div>
Let <span class="inlinecode"><span class="id" type="var">n</span></span> be a number with <span class="inlinecode"><span class="id" type="var">length</span></span> <span class="inlinecode"><span class="id" type="var">l</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">n</span></span>. Since
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">length</span> <span class="id" type="var">l</span> = <span class="id" type="var">length</span> (<span class="id" type="var">x</span>::<span class="id" type="var">l'</span>) = <span class="id" type="var">S</span> (<span class="id" type="var">length</span> <span class="id" type="var">l'</span>),
<div class="paragraph"> </div>
</div>
it suffices to show that
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">index</span> (<span class="id" type="var">S</span> (<span class="id" type="var">length</span> <span class="id" type="var">l'</span>)) <span class="id" type="var">l'</span> = <span class="id" type="var">None</span>.
<div class="paragraph"> </div>
</div>
But this follows directly from the induction hypothesis,
picking <span class="inlinecode"><span class="id" type="var">n'</span></span> to be length <span class="inlinecode"><span class="id" type="var">l'</span></span>. <font size=-2>☐</font>
</li>
</ul>
</li>
</ul>
<div class="paragraph"> </div>
<a name="lab344"></a><h3 class="section">Induction Over an Inductively Defined Proposition</h3>
<div class="paragraph"> </div>
Since inductively defined proof objects are often called
"derivation trees," this form of proof is also known as <i>induction
on derivations</i>.
<div class="paragraph"> </div>
<i>Template</i>:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <i>Theorem</i>: <Proposition of the form "<span class="inlinecode"><span class="id" type="var">Q</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span>," where <span class="inlinecode"><span class="id" type="var">Q</span></span> is
some inductively defined proposition (more generally,
"For all <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span> <span class="inlinecode"><span class="id" type="var">z</span></span>, <span class="inlinecode"><span class="id" type="var">Q</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span> <span class="inlinecode"><span class="id" type="var">z</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span> <span class="inlinecode"><span class="id" type="var">z</span></span>")>
<div class="paragraph"> </div>
<i>Proof</i>: By induction on a derivation of <span class="inlinecode"><span class="id" type="var">Q</span></span>. <Or, more
generally, "Suppose we are given <span class="inlinecode"><span class="id" type="var">x</span></span>, <span class="inlinecode"><span class="id" type="var">y</span></span>, and <span class="inlinecode"><span class="id" type="var">z</span></span>. We