-
Notifications
You must be signed in to change notification settings - Fork 1
/
MoreCoq.html
1772 lines (1373 loc) · 153 KB
/
MoreCoq.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>MoreCoq: More About Coq</title>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
<div id="page">
<div id="header">
</div>
<div id="main">
<h1 class="libtitle">MoreCoq<span class="subtitle">More About Coq</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Export</span> <span class="id" type="var">Poly</span>.<br/>
<br/>
</div>
<div class="doc">
This chapter introduces several more Coq tactics that,
together, allow us to prove many more theorems about the
functional programs we are writing.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab156"></a><h1 class="section">The <span class="inlinecode"><span class="id" type="tactic">apply</span></span> Tactic</h1>
<div class="paragraph"> </div>
We often encounter situations where the goal to be proved is
exactly the same as some hypothesis in the context or some
previously proved lemma.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly1</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">o</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 style="font-family: arial;">→</span><br/>
[<span class="id" type="var">n</span>;<span class="id" type="var">o</span>] = [<span class="id" type="var">n</span>;<span class="id" type="var">p</span>] <span style="font-family: arial;">→</span><br/>
[<span class="id" type="var">n</span>;<span class="id" type="var">o</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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span> <span class="id" type="var">o</span> <span class="id" type="var">p</span> <span class="id" type="var">eq1</span> <span class="id" type="var">eq2</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">←</span> <span class="id" type="var">eq1</span>.<br/>
<span class="comment">(* At this point, we could finish with <br/>
"<span class="inlinecode"><span class="id" type="tactic">rewrite</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">eq2</span>.</span> <span class="inlinecode"><span class="id" type="tactic">reflexivity</span>.</span>" as we have <br/>
done several times above. But we can achieve the<br/>
same effect in a single step by using the <br/>
<span class="inlinecode"><span class="id" type="tactic">apply</span></span> tactic instead: *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">eq2</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
The <span class="inlinecode"><span class="id" type="tactic">apply</span></span> tactic also works with <i>conditional</i> hypotheses
and lemmas: if the statement being applied is an implication, then
the premises of this implication will be added to the list of
subgoals needing to be proved.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly2</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">o</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 style="font-family: arial;">→</span><br/>
(<span style="font-family: arial;">∀</span>(<span class="id" type="var">q</span> <span class="id" type="var">r</span> : <span class="id" type="var">nat</span>), <span class="id" type="var">q</span> = <span class="id" type="var">r</span> <span style="font-family: arial;">→</span> [<span class="id" type="var">q</span>;<span class="id" type="var">o</span>] = [<span class="id" type="var">r</span>;<span class="id" type="var">p</span>]) <span style="font-family: arial;">→</span><br/>
[<span class="id" type="var">n</span>;<span class="id" type="var">o</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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span> <span class="id" type="var">o</span> <span class="id" type="var">p</span> <span class="id" type="var">eq1</span> <span class="id" type="var">eq2</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">eq2</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">eq1</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
You may find it instructive to experiment with this proof
and see if there is a way to complete it using just <span class="inlinecode"><span class="id" type="tactic">rewrite</span></span>
instead of <span class="inlinecode"><span class="id" type="tactic">apply</span></span>.
<div class="paragraph"> </div>
Typically, when we use <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span>, the statement <span class="inlinecode"><span class="id" type="var">H</span></span> will
begin with a <span class="inlinecode"><span style="font-family: arial;">∀</span></span> binding some <i>universal variables</i>. When
Coq matches the current goal against the conclusion of <span class="inlinecode"><span class="id" type="var">H</span></span>, it
will try to find appropriate values for these variables. For
example, when we do <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">eq2</span></span> in the following proof, the
universal variable <span class="inlinecode"><span class="id" type="var">q</span></span> in <span class="inlinecode"><span class="id" type="var">eq2</span></span> gets instantiated with <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">r</span></span>
gets instantiated with <span class="inlinecode"><span class="id" type="var">m</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly2a</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">n</span>) = (<span class="id" type="var">m</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">q</span> <span class="id" type="var">r</span> : <span class="id" type="var">nat</span>), (<span class="id" type="var">q</span>,<span class="id" type="var">q</span>) = (<span class="id" type="var">r</span>,<span class="id" type="var">r</span>) <span style="font-family: arial;">→</span> [<span class="id" type="var">q</span>] = [<span class="id" type="var">r</span>]) <span style="font-family: arial;">→</span><br/>
[<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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span> <span class="id" type="var">eq1</span> <span class="id" type="var">eq2</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">eq2</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">eq1</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab157"></a><h4 class="section">Exercise: 2 stars, optional (silly_ex)</h4>
Complete the following proof without using <span class="inlinecode"><span class="id" type="tactic">simpl</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly_ex</span> : <br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">evenb</span> <span class="id" type="var">n</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span> <span class="id" type="var">oddb</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>) = <span class="id" type="var">true</span>) <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">evenb</span> 3 = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">oddb</span> 4 = <span class="id" type="var">true</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="comment">(* FILL IN HERE *)</span> <span class="id" type="var">Admitted</span>.<br/>
</div>
<div class="doc">
<font size=-2>☐</font>
<div class="paragraph"> </div>
To use the <span class="inlinecode"><span class="id" type="tactic">apply</span></span> tactic, the (conclusion of the) fact
being applied must match the goal <i>exactly</i> — for example, <span class="inlinecode"><span class="id" type="tactic">apply</span></span>
will not work if the left and right sides of the equality are
swapped.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly3_firsttry</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">true</span> = <span class="id" type="var">beq_nat</span> <span class="id" type="var">n</span> 5 <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">beq_nat</span> (<span class="id" type="var">S</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>)) 7 = <span class="id" type="var">true</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">simpl</span>.<br/>
<span class="comment">(* Here we cannot use <span class="inlinecode"><span class="id" type="tactic">apply</span></span> directly *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
In this case we can use the <span class="inlinecode"><span class="id" type="tactic">symmetry</span></span> tactic, which switches the
left and right sides of an equality in the goal.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly3</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">true</span> = <span class="id" type="var">beq_nat</span> <span class="id" type="var">n</span> 5 <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">beq_nat</span> (<span class="id" type="var">S</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>)) 7 = <span class="id" type="var">true</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">symmetry</span>.<br/>
<span class="id" type="tactic">simpl</span>. <span class="comment">(* Actually, this <span class="inlinecode"><span class="id" type="tactic">simpl</span></span> is unnecessary, since <br/>
<span class="inlinecode"><span class="id" type="tactic">apply</span></span> will perform simplification first. *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab158"></a><h4 class="section">Exercise: 3 stars (apply_exercise1)</h4>
Hint: you can use <span class="inlinecode"><span class="id" type="tactic">apply</span></span> with previously defined lemmas, not
just hypotheses in the context. Remember that <span class="inlinecode"><span class="id" type="var">SearchAbout</span></span> is
your friend.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">rev_exercise1</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">l</span> <span class="id" type="var">l'</span> : <span class="id" type="var">list</span> <span class="id" type="var">nat</span>),<br/>
<span class="id" type="var">l</span> = <span class="id" type="var">rev</span> <span class="id" type="var">l'</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">l'</span> = <span class="id" type="var">rev</span> <span class="id" type="var">l</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="lab159"></a><h4 class="section">Exercise: 1 star, optional (apply_rewrite)</h4>
Briefly explain the difference between the tactics <span class="inlinecode"><span class="id" type="tactic">apply</span></span> and
<span class="inlinecode"><span class="id" type="tactic">rewrite</span></span>. Are there situations where both can usefully be
applied?
<span class="comment">(* FILL IN HERE *)</span><br/>
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab160"></a><h1 class="section">The <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode">...</span> <span class="inlinecode"><span class="id" type="keyword">with</span></span> <span class="inlinecode">...</span> Tactic</h1>
<div class="paragraph"> </div>
The following silly example uses two rewrites in a row to
get from <span class="inlinecode">[<span class="id" type="var">a</span>,<span class="id" type="var">b</span>]</span> to <span class="inlinecode">[<span class="id" type="var">e</span>,<span class="id" type="var">f</span>]</span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">trans_eq_example</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">a</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">d</span> <span class="id" type="var">e</span> <span class="id" type="var">f</span> : <span class="id" type="var">nat</span>),<br/>
[<span class="id" type="var">a</span>;<span class="id" type="var">b</span>] = [<span class="id" type="var">c</span>;<span class="id" type="var">d</span>] <span style="font-family: arial;">→</span><br/>
[<span class="id" type="var">c</span>;<span class="id" type="var">d</span>] = [<span class="id" type="var">e</span>;<span class="id" type="var">f</span>] <span style="font-family: arial;">→</span><br/>
[<span class="id" type="var">a</span>;<span class="id" type="var">b</span>] = [<span class="id" type="var">e</span>;<span class="id" type="var">f</span>].<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">a</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">d</span> <span class="id" type="var">e</span> <span class="id" type="var">f</span> <span class="id" type="var">eq1</span> <span class="id" type="var">eq2</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">eq1</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">eq2</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Since this is a common pattern, we might
abstract it out as a lemma recording once and for all
the fact that equality is transitive.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">trans_eq</span> : <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">n</span> <span class="id" type="var">m</span> <span class="id" type="var">o</span> : <span class="id" type="var">X</span>),<br/>
<span class="id" type="var">n</span> = <span class="id" type="var">m</span> <span style="font-family: arial;">→</span> <span class="id" type="var">m</span> = <span class="id" type="var">o</span> <span style="font-family: arial;">→</span> <span class="id" type="var">n</span> = <span class="id" type="var">o</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">X</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span> <span class="id" type="var">o</span> <span class="id" type="var">eq1</span> <span class="id" type="var">eq2</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">eq1</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">eq2</span>.<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Now, we should be able to use <span class="inlinecode"><span class="id" type="var">trans_eq</span></span> to
prove the above example. However, to do this we need
a slight refinement of the <span class="inlinecode"><span class="id" type="tactic">apply</span></span> tactic.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">trans_eq_example'</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">a</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">d</span> <span class="id" type="var">e</span> <span class="id" type="var">f</span> : <span class="id" type="var">nat</span>),<br/>
[<span class="id" type="var">a</span>;<span class="id" type="var">b</span>] = [<span class="id" type="var">c</span>;<span class="id" type="var">d</span>] <span style="font-family: arial;">→</span><br/>
[<span class="id" type="var">c</span>;<span class="id" type="var">d</span>] = [<span class="id" type="var">e</span>;<span class="id" type="var">f</span>] <span style="font-family: arial;">→</span><br/>
[<span class="id" type="var">a</span>;<span class="id" type="var">b</span>] = [<span class="id" type="var">e</span>;<span class="id" type="var">f</span>].<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">a</span> <span class="id" type="var">b</span> <span class="id" type="var">c</span> <span class="id" type="var">d</span> <span class="id" type="var">e</span> <span class="id" type="var">f</span> <span class="id" type="var">eq1</span> <span class="id" type="var">eq2</span>.<br/>
<span class="comment">(* If we simply tell Coq <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">trans_eq</span></span> at this point,<br/>
it can tell (by matching the goal against the<br/>
conclusion of the lemma) that it should instantiate <span class="inlinecode"><span class="id" type="var">X</span></span><br/>
with <span class="inlinecode">[<span class="id" type="var">nat</span>]</span>, <span class="inlinecode"><span class="id" type="var">n</span></span> with <span class="inlinecode">[<span class="id" type="var">a</span>,<span class="id" type="var">b</span>]</span>, and <span class="inlinecode"><span class="id" type="var">o</span></span> with <span class="inlinecode">[<span class="id" type="var">e</span>,<span class="id" type="var">f</span>]</span>.<br/>
However, the matching process doesn't determine an<br/>
instantiation for <span class="inlinecode"><span class="id" type="var">m</span></span>: we have to supply one explicitly<br/>
by adding <span class="inlinecode"><span class="id" type="keyword">with</span></span> <span class="inlinecode">(<span class="id" type="var">m</span>:=[<span class="id" type="var">c</span>,<span class="id" type="var">d</span>])</span> to the invocation of<br/>
<span class="inlinecode"><span class="id" type="tactic">apply</span></span>. *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">trans_eq</span> <span class="id" type="keyword">with</span> (<span class="id" type="var">m</span>:=[<span class="id" type="var">c</span>;<span class="id" type="var">d</span>]). <span class="id" type="tactic">apply</span> <span class="id" type="var">eq1</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">eq2</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Actually, we usually don't have to include the name <span class="inlinecode"><span class="id" type="var">m</span></span>
in the <span class="inlinecode"><span class="id" type="keyword">with</span></span> clause; Coq is often smart enough to
figure out which instantiation we're giving. We could
instead write: <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">trans_eq</span></span> <span class="inlinecode"><span class="id" type="keyword">with</span></span> <span class="inlinecode">[<span class="id" type="var">c</span>,<span class="id" type="var">d</span>]</span>.
<div class="paragraph"> </div>
<a name="lab161"></a><h4 class="section">Exercise: 3 stars, optional (apply_with_exercise)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Example</span> <span class="id" type="var">trans_eq_exercise</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">o</span> <span class="id" type="var">p</span> : <span class="id" type="var">nat</span>),<br/>
<span class="id" type="var">m</span> = (<span class="id" type="var">minustwo</span> <span class="id" type="var">o</span>) <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">n</span> + <span class="id" type="var">p</span>) = <span class="id" type="var">m</span> <span style="font-family: arial;">→</span><br/>
(<span class="id" type="var">n</span> + <span class="id" type="var">p</span>) = (<span class="id" type="var">minustwo</span> <span class="id" type="var">o</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="lab162"></a><h1 class="section">The <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic</h1>
<div class="paragraph"> </div>
Recall the definition of natural numbers:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">nat</span> : <span class="id" type="keyword">Type</span> :=<br/>
| <span class="id" type="var">O</span> : <span class="id" type="var">nat</span><br/>
| <span class="id" type="var">S</span> : <span class="id" type="var">nat</span> <span style="font-family: arial;">→</span> <span class="id" type="var">nat</span>.
<div class="paragraph"> </div>
</div>
It is clear from this definition that every number has one of two
forms: either it is the constructor <span class="inlinecode"><span class="id" type="var">O</span></span> or it is built by applying
the constructor <span class="inlinecode"><span class="id" type="var">S</span></span> to another number. But there is more here than
meets the eye: implicit in the definition (and in our informal
understanding of how datatype declarations work in other
programming languages) are two other facts:
<div class="paragraph"> </div>
<ul class="doclist">
<li> The constructor <span class="inlinecode"><span class="id" type="var">S</span></span> is <i>injective</i>. That is, the only way we can
have <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 class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> is if <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>.
<div class="paragraph"> </div>
</li>
<li> The constructors <span class="inlinecode"><span class="id" type="var">O</span></span> and <span class="inlinecode"><span class="id" type="var">S</span></span> are <i>disjoint</i>. That is, <span class="inlinecode"><span class="id" type="var">O</span></span> is not
equal to <span class="inlinecode"><span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> for any <span class="inlinecode"><span class="id" type="var">n</span></span>.
</li>
</ul>
<div class="paragraph"> </div>
Similar principles apply to all inductively defined types: all
constructors are injective, and the values built from distinct
constructors are never equal. For lists, the <span class="inlinecode"><span class="id" type="var">cons</span></span> constructor is
injective and <span class="inlinecode"><span class="id" type="var">nil</span></span> is different from every non-empty list. For
booleans, <span class="inlinecode"><span class="id" type="var">true</span></span> and <span class="inlinecode"><span class="id" type="var">false</span></span> are unequal. (Since neither <span class="inlinecode"><span class="id" type="var">true</span></span>
nor <span class="inlinecode"><span class="id" type="var">false</span></span> take any arguments, their injectivity is not an issue.)
<div class="paragraph"> </div>
Coq provides a tactic called <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> that allows us to exploit
these principles in proofs.
<div class="paragraph"> </div>
The <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic is used like this. Suppose <span class="inlinecode"><span class="id" type="var">H</span></span> is a
hypothesis in the context (or a previously proven lemma) of the
form
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="var">c</span> <span class="id" type="var">a1</span> <span class="id" type="var">a2</span> ... <span class="id" type="var">an</span> = <span class="id" type="var">d</span> <span class="id" type="var">b1</span> <span class="id" type="var">b2</span> ... <span class="id" type="var">bm</span>
<div class="paragraph"> </div>
</div>
for some constructors <span class="inlinecode"><span class="id" type="var">c</span></span> and <span class="inlinecode"><span class="id" type="var">d</span></span> and arguments <span class="inlinecode"><span class="id" type="var">a1</span></span> <span class="inlinecode">...</span> <span class="inlinecode"><span class="id" type="var">an</span></span> and
<span class="inlinecode"><span class="id" type="var">b1</span></span> <span class="inlinecode">...</span> <span class="inlinecode"><span class="id" type="var">bm</span></span>. Then <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> instructs Coq to "invert" this
equality to extract the information it contains about these terms:
<div class="paragraph"> </div>
<ul class="doclist">
<li> If <span class="inlinecode"><span class="id" type="var">c</span></span> and <span class="inlinecode"><span class="id" type="var">d</span></span> are the same constructor, then we know, by the
injectivity of this constructor, that <span class="inlinecode"><span class="id" type="var">a1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">b1</span></span>, <span class="inlinecode"><span class="id" type="var">a2</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">b2</span></span>,
etc.; <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> adds these facts to the context, and tries
to use them to rewrite the goal.
<div class="paragraph"> </div>
</li>
<li> If <span class="inlinecode"><span class="id" type="var">c</span></span> and <span class="inlinecode"><span class="id" type="var">d</span></span> are different constructors, then the hypothesis
<span class="inlinecode"><span class="id" type="var">H</span></span> is contradictory. That is, a false assumption has crept
into the context, and this means that any goal whatsoever is
provable! In this case, <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> marks the current goal as
completed and pops it off the goal stack.
</li>
</ul>
<div class="paragraph"> </div>
The <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic is probably easier to understand by
seeing it in action than from general descriptions like the above.
Below you will find example theorems that demonstrate the use of
<span class="inlinecode"><span class="id" type="tactic">inversion</span></span> and exercises to test your understanding.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">eq_add_S</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">S</span> <span class="id" type="var">n</span> = <span class="id" type="var">S</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span><br/>
<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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly4</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 style="font-family: arial;">→</span><br/>
<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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">o</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
As a convenience, the <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic can also
destruct equalities between complex values, binding
multiple variables as it goes.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly5</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">o</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">o</span>;<span class="id" type="var">o</span>] <span style="font-family: arial;">→</span><br/>
[<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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span> <span class="id" type="var">o</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab163"></a><h4 class="section">Exercise: 1 star (sillyex1)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Example</span> <span class="id" type="var">sillyex1</span> : <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">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">l</span> <span class="id" type="var">j</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>),<br/>
<span class="id" type="var">x</span> :: <span class="id" type="var">y</span> :: <span class="id" type="var">l</span> = <span class="id" type="var">z</span> :: <span class="id" type="var">j</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">y</span> :: <span class="id" type="var">l</span> = <span class="id" type="var">x</span> :: <span class="id" type="var">j</span> <span style="font-family: arial;">→</span><br/>
<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/>
</div>
<div class="doc">
<font size=-2>☐</font>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly6</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">S</span> <span class="id" type="var">n</span> = <span class="id" type="var">O</span> <span style="font-family: arial;">→</span><br/>
2 + 2 = 5.<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">contra</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">contra</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly7</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">false</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span><br/>
[<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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span> <span class="id" type="var">contra</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">contra</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab164"></a><h4 class="section">Exercise: 1 star (sillyex2)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Example</span> <span class="id" type="var">sillyex2</span> : <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">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">l</span> <span class="id" type="var">j</span> : <span class="id" type="var">list</span> <span class="id" type="var">X</span>),<br/>
<span class="id" type="var">x</span> :: <span class="id" type="var">y</span> :: <span class="id" type="var">l</span> = [] <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">y</span> :: <span class="id" type="var">l</span> = <span class="id" type="var">z</span> :: <span class="id" type="var">j</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">x</span> = <span class="id" type="var">z</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>
While the injectivity of constructors allows us to reason
<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> <span class="inlinecode"><span class="id" type="var">nat</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 class="id" type="var">S</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">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">m</span></span>, the reverse direction of
the implication is an instance of a more general fact about
constructors and functions, which we will often find useful:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="tactic">f_equal</span> : <span style="font-family: arial;">∀</span>(<span class="id" type="var">A</span> <span class="id" type="var">B</span> : <span class="id" type="keyword">Type</span>) (<span class="id" type="var">f</span>: <span class="id" type="var">A</span> <span style="font-family: arial;">→</span> <span class="id" type="var">B</span>) (<span class="id" type="var">x</span> <span class="id" type="var">y</span>: <span class="id" type="var">A</span>), <br/>
<span class="id" type="var">x</span> = <span class="id" type="var">y</span> <span style="font-family: arial;">→</span> <span class="id" type="var">f</span> <span class="id" type="var">x</span> = <span class="id" type="var">f</span> <span class="id" type="var">y</span>.<br/>
<span class="id" type="keyword">Proof</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">A</span> <span class="id" type="var">B</span> <span class="id" type="var">f</span> <span class="id" type="var">x</span> <span class="id" type="var">y</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">rewrite</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab165"></a><h4 class="section">Exercise: 2 stars, optional (practice)</h4>
A couple more nontrivial but not-too-complicated proofs to work
together in class, or for you to work as exercises.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">beq_nat_0_l</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>,<br/>
<span class="id" type="var">beq_nat</span> 0 <span class="id" type="var">n</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span> <span class="id" type="var">n</span> = 0.<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">beq_nat_0_r</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span>,<br/>
<span class="id" type="var">beq_nat</span> <span class="id" type="var">n</span> 0 = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span> <span class="id" type="var">n</span> = 0.<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="lab166"></a><h1 class="section">Using Tactics on Hypotheses</h1>
<div class="paragraph"> </div>
By default, most tactics work on the goal formula and leave
the context unchanged. However, most tactics also have a variant
that performs a similar operation on a statement in the context.
<div class="paragraph"> </div>
For example, the tactic <span class="inlinecode"><span class="id" type="tactic">simpl</span></span> <span class="inlinecode"><span class="id" type="keyword">in</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> performs simplification in
the hypothesis named <span class="inlinecode"><span class="id" type="var">H</span></span> in the context.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">S_inj</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>) (<span class="id" type="var">b</span> : <span class="id" type="var">bool</span>),<br/>
<span class="id" type="var">beq_nat</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>) (<span class="id" type="var">S</span> <span class="id" type="var">m</span>) = <span class="id" type="var">b</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">beq_nat</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span> = <span class="id" type="var">b</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">m</span> <span class="id" type="var">b</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Similarly, the tactic <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">L</span></span> <span class="inlinecode"><span class="id" type="keyword">in</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> matches some
conditional statement <span class="inlinecode"><span class="id" type="var">L</span></span> (of the form <span class="inlinecode"><span class="id" type="var">L1</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">L2</span></span>, say) against a
hypothesis <span class="inlinecode"><span class="id" type="var">H</span></span> in the context. However, unlike ordinary
<span class="inlinecode"><span class="id" type="tactic">apply</span></span> (which rewrites a goal matching <span class="inlinecode"><span class="id" type="var">L2</span></span> into a subgoal <span class="inlinecode"><span class="id" type="var">L1</span></span>),
<span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">L</span></span> <span class="inlinecode"><span class="id" type="keyword">in</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> matches <span class="inlinecode"><span class="id" type="var">H</span></span> against <span class="inlinecode"><span class="id" type="var">L1</span></span> and, if successful,
replaces it with <span class="inlinecode"><span class="id" type="var">L2</span></span>.
<div class="paragraph"> </div>
In other words, <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">L</span></span> <span class="inlinecode"><span class="id" type="keyword">in</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> gives us a form of "forward
reasoning" — from <span class="inlinecode"><span class="id" type="var">L1</span></span> <span class="inlinecode"><span style="font-family: arial;">→</span></span> <span class="inlinecode"><span class="id" type="var">L2</span></span> and a hypothesis matching <span class="inlinecode"><span class="id" type="var">L1</span></span>, it
gives us a hypothesis matching <span class="inlinecode"><span class="id" type="var">L2</span></span>. By contrast, <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">L</span></span> is
"backward reasoning" — it says that if we know <span class="inlinecode"><span class="id" type="var">L1</span><span style="font-family: arial;">→</span><span class="id" type="var">L2</span></span> and we
are trying to prove <span class="inlinecode"><span class="id" type="var">L2</span></span>, it suffices to prove <span class="inlinecode"><span class="id" type="var">L1</span></span>.
<div class="paragraph"> </div>
Here is a variant of a proof from above, using forward reasoning
throughout instead of backward reasoning.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">silly3'</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">beq_nat</span> <span class="id" type="var">n</span> 5 = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span> <span class="id" type="var">beq_nat</span> (<span class="id" type="var">S</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>)) 7 = <span class="id" type="var">true</span>) <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">true</span> = <span class="id" type="var">beq_nat</span> <span class="id" type="var">n</span> 5 <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">true</span> = <span class="id" type="var">beq_nat</span> (<span class="id" type="var">S</span> (<span class="id" type="var">S</span> <span class="id" type="var">n</span>)) 7.<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">eq</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">symmetry</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">eq</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">symmetry</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Forward reasoning starts from what is <i>given</i> (premises,
previously proven theorems) and iteratively draws conclusions from
them until the goal is reached. Backward reasoning starts from
the <i>goal</i>, and iteratively reasons about what would imply the
goal, until premises or previously proven theorems are reached.
If you've seen informal proofs before (for example, in a math or
computer science class), they probably used forward reasoning. In
general, Coq tends to favor backward reasoning, but in some
situations the forward style can be easier to use or to think
about.
<div class="paragraph"> </div>
<a name="lab167"></a><h4 class="section">Exercise: 3 stars (plus_n_n_injective)</h4>
Practice using "in" variants in this exercise.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">plus_n_n_injective</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">n</span> + <span class="id" type="var">n</span> = <span class="id" type="var">m</span> + <span class="id" type="var">m</span> <span style="font-family: arial;">→</span><br/>
<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="id" type="tactic">intros</span> <span class="id" type="var">n</span>. <span class="id" type="tactic">induction</span> <span class="id" type="var">n</span> <span class="id" type="keyword">as</span> [| <span class="id" type="var">n'</span>].<br/>
<span class="comment">(* Hint: use the plus_n_Sm lemma *)</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="lab168"></a><h1 class="section">Varying the Induction Hypothesis</h1>
<div class="paragraph"> </div>
Sometimes it is important to control the exact form of the
induction hypothesis when carrying out inductive proofs in Coq.
In particular, we need to be careful about which of the
assumptions we move (using <span class="inlinecode"><span class="id" type="tactic">intros</span></span>) from the goal to the context
before invoking the <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic. For example, suppose
we want to show that the <span class="inlinecode"><span class="id" type="var">double</span></span> function is injective — i.e.,
that it always maps different arguments to different results:
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">double_injective</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">double</span> <span class="id" type="var">n</span> = <span class="id" type="var">double</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span> <span class="id" type="var">n</span> = <span class="id" type="var">m</span>.
<div class="paragraph"> </div>
</div>
The way we <i>start</i> this proof is a little bit delicate: if we
begin it with
<div class="paragraph"> </div>
<div class="code code-tight">
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span>. <span class="id" type="tactic">induction</span> <span class="id" type="var">n</span>.
<div class="paragraph"> </div>
</div>
all is well. But if we begin it with
<div class="paragraph"> </div>
<div class="code code-tight">
<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="tactic">induction</span> <span class="id" type="var">n</span>.
<div class="paragraph"> </div>
</div>
we get stuck in the middle of the inductive case...
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">double_injective_FAILED</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">double</span> <span class="id" type="var">n</span> = <span class="id" type="var">double</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span><br/>
<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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</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 = O". <span class="id" type="tactic">simpl</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">destruct</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">SCase</span> "m = O". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">SCase</span> "m = S m'". <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>.<br/>
<span class="id" type="var">Case</span> "n = S n'". <span class="id" type="tactic">intros</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">destruct</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">SCase</span> "m = O". <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>.<br/>
<span class="id" type="var">SCase</span> "m = S m'". <span class="id" type="tactic">apply</span> <span class="id" type="tactic">f_equal</span>.<br/>
<span class="comment">(* Here we are stuck. The induction hypothesis, <span class="inlinecode"><span class="id" type="var">IHn'</span></span>, does<br/>
not give us <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> -- there is an extra <span class="inlinecode"><span class="id" type="var">S</span></span> in the<br/>
way -- so the goal is not provable. *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
What went wrong?
<div class="paragraph"> </div>
The problem is that, at the point we invoke the induction
hypothesis, we have already introduced <span class="inlinecode"><span class="id" type="var">m</span></span> into the context —
intuitively, we have told Coq, "Let's consider some particular
<span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">m</span></span>..." and we now have to prove that, if <span class="inlinecode"><span class="id" type="var">double</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">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> for <i>this particular</i> <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">m</span></span>, then <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>.
<div class="paragraph"> </div>
The next tactic, <span class="inlinecode"><span class="id" type="tactic">induction</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> says to Coq: We are going to show
the goal by induction on <span class="inlinecode"><span class="id" type="var">n</span></span>. That is, we are going to prove that
the proposition
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> = "if <span class="inlinecode"><span class="id" type="var">double</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">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span>, then <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>"
</li>
</ul>
<div class="paragraph"> </div>
holds for all <span class="inlinecode"><span class="id" type="var">n</span></span> by showing
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">O</span></span>
<div class="paragraph"> </div>
(i.e., "if <span class="inlinecode"><span class="id" type="var">double</span></span> <span class="inlinecode"><span class="id" type="var">O</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> then <span class="inlinecode"><span class="id" type="var">O</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">m</span></span>")
<div class="paragraph"> </div>
</li>
<li> <span class="inlinecode"><span class="id" type="var">P</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</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n</span>)</span>
<div class="paragraph"> </div>
(i.e., "if <span class="inlinecode"><span class="id" type="var">double</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">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> then <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>" implies "if
<span class="inlinecode"><span class="id" type="var">double</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 class="id" type="var">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> then <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 class="id" type="var">m</span></span>").
</li>
</ul>
<div class="paragraph"> </div>
If we look closely at the second statement, it is saying something
rather strange: it says that, for a <i>particular</i> <span class="inlinecode"><span class="id" type="var">m</span></span>, if we know
<div class="paragraph"> </div>
<ul class="doclist">
<li> "if <span class="inlinecode"><span class="id" type="var">double</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">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> then <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>"
</li>
</ul>
<div class="paragraph"> </div>
then we can prove
<div class="paragraph"> </div>
<ul class="doclist">
<li> "if <span class="inlinecode"><span class="id" type="var">double</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 class="id" type="var">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> then <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 class="id" type="var">m</span></span>".
</li>
</ul>
<div class="paragraph"> </div>
To see why this is strange, let's think of a particular <span class="inlinecode"><span class="id" type="var">m</span></span> —
say, <span class="inlinecode">5</span>. The statement is then saying that, if we know
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">Q</span></span> = "if <span class="inlinecode"><span class="id" type="var">double</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode">10</span> then <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode">5</span>"
</li>
</ul>
<div class="paragraph"> </div>
then we can prove
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">R</span></span> = "if <span class="inlinecode"><span class="id" type="var">double</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">10</span> then <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">5</span>".
</li>
</ul>
<div class="paragraph"> </div>
But knowing <span class="inlinecode"><span class="id" type="var">Q</span></span> doesn't give us any help with proving <span class="inlinecode"><span class="id" type="var">R</span></span>! (If we
tried to prove <span class="inlinecode"><span class="id" type="var">R</span></span> from <span class="inlinecode"><span class="id" type="var">Q</span></span>, we would say something like "Suppose
<span class="inlinecode"><span class="id" type="var">double</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">10</span>..." but then we'd be stuck: knowing that
<span class="inlinecode"><span class="id" type="var">double</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">n</span>)</span> is <span class="inlinecode">10</span> tells us nothing about whether <span class="inlinecode"><span class="id" type="var">double</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span>
is <span class="inlinecode">10</span>, so <span class="inlinecode"><span class="id" type="var">Q</span></span> is useless at this point.)
<div class="paragraph"> </div>
To summarize: Trying to carry out this proof by induction on <span class="inlinecode"><span class="id" type="var">n</span></span>
when <span class="inlinecode"><span class="id" type="var">m</span></span> is already in the context doesn't work because we are
trying to prove a relation involving <i>every</i> <span class="inlinecode"><span class="id" type="var">n</span></span> but just a
<i>single</i> <span class="inlinecode"><span class="id" type="var">m</span></span>.
<div class="paragraph"> </div>
The good proof of <span class="inlinecode"><span class="id" type="var">double_injective</span></span> leaves <span class="inlinecode"><span class="id" type="var">m</span></span> in the goal
statement at the point where the <span class="inlinecode"><span class="id" type="tactic">induction</span></span> tactic is invoked on
<span class="inlinecode"><span class="id" type="var">n</span></span>:
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">double_injective</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">double</span> <span class="id" type="var">n</span> = <span class="id" type="var">double</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span><br/>
<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="id" type="tactic">intros</span> <span class="id" type="var">n</span>. <span class="id" type="tactic">induction</span> <span class="id" type="var">n</span> <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">simpl</span>. <span class="id" type="tactic">intros</span> <span class="id" type="var">m</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">destruct</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">SCase</span> "m = O". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">SCase</span> "m = S m'". <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>.<br/>
<span class="id" type="var">Case</span> "n = S n'".<br/>
<span class="comment">(* Notice that both the goal and the induction<br/>
hypothesis have changed: the goal asks us to prove<br/>
something more general (i.e., to prove the<br/>
statement for _every_ <span class="inlinecode"><span class="id" type="var">m</span></span>), but the IH is<br/>
correspondingly more flexible, allowing us to<br/>
choose any <span class="inlinecode"><span class="id" type="var">m</span></span> we like when we apply the IH. *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">m</span> <span class="id" type="var">eq</span>.<br/>
<span class="comment">(* Now we choose a particular <span class="inlinecode"><span class="id" type="var">m</span></span> and introduce the<br/>
assumption that <span class="inlinecode"><span class="id" type="var">double</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">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span>. Since we<br/>
are doing a case analysis on <span class="inlinecode"><span class="id" type="var">n</span></span>, we need a case<br/>
analysis on <span class="inlinecode"><span class="id" type="var">m</span></span> to keep the two "in sync." *)</span><br/>
<span class="id" type="tactic">destruct</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">SCase</span> "m = O".<br/>
<span class="comment">(* The 0 case is trivial *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>.<br/>
<span class="id" type="var">SCase</span> "m = S m'".<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="tactic">f_equal</span>.<br/>
<span class="comment">(* At this point, since we are in the second<br/>
branch of the <span class="inlinecode"><span class="id" type="tactic">destruct</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span>, the <span class="inlinecode"><span class="id" type="var">m'</span></span> mentioned<br/>
in the context at this point is actually the<br/>
predecessor of the one we started out talking<br/>
about. Since we are also in the <span class="inlinecode"><span class="id" type="var">S</span></span> branch of<br/>
the induction, this is perfect: if we<br/>
instantiate the generic <span class="inlinecode"><span class="id" type="var">m</span></span> in the IH with the<br/>
<span class="inlinecode"><span class="id" type="var">m'</span></span> that we are talking about right now (this<br/>
instantiation is performed automatically by<br/>
<span class="inlinecode"><span class="id" type="tactic">apply</span></span>), then <span class="inlinecode"><span class="id" type="var">IHn'</span></span> gives us exactly what we<br/>
need to finish the proof. *)</span><br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHn'</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
What this teaches us is that we need to be careful about using
induction to try to prove something too specific: If we're proving
a property of <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">m</span></span> by induction on <span class="inlinecode"><span class="id" type="var">n</span></span>, we may need to
leave <span class="inlinecode"><span class="id" type="var">m</span></span> generic.
<div class="paragraph"> </div>
The proof of this theorem (left as an exercise) has to be treated similarly:
<div class="paragraph"> </div>
<a name="lab169"></a><h4 class="section">Exercise: 2 stars (beq_nat_true)</h4>
</div>
<div class="code code-space">
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">beq_nat_true</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">beq_nat</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span> = <span class="id" type="var">true</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="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="lab170"></a><h4 class="section">Exercise: 2 stars, advanced (beq_nat_true_informal)</h4>
Give a careful informal proof of <span class="inlinecode"><span class="id" type="var">beq_nat_true</span></span>, being as explicit
as possible about quantifiers.
</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>
The strategy of doing fewer <span class="inlinecode"><span class="id" type="tactic">intros</span></span> before an <span class="inlinecode"><span class="id" type="tactic">induction</span></span> doesn't
always work directly; sometimes a little <i>rearrangement</i> of
quantified variables is needed. Suppose, for example, that we
wanted to prove <span class="inlinecode"><span class="id" type="var">double_injective</span></span> by induction on <span class="inlinecode"><span class="id" type="var">m</span></span> instead of
<span class="inlinecode"><span class="id" type="var">n</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">double_injective_take2_FAILED</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">double</span> <span class="id" type="var">n</span> = <span class="id" type="var">double</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span><br/>
<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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span>. <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">intros</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">destruct</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">SCase</span> "n = O". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">SCase</span> "n = S n'". <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>.<br/>
<span class="id" type="var">Case</span> "m = S m'". <span class="id" type="tactic">intros</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">destruct</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">SCase</span> "n = O". <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>.<br/>
<span class="id" type="var">SCase</span> "n = S n'". <span class="id" type="tactic">apply</span> <span class="id" type="tactic">f_equal</span>.<br/>
<span class="comment">(* Stuck again here, just like before. *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
The problem is that, to do induction on <span class="inlinecode"><span class="id" type="var">m</span></span>, we must first
introduce <span class="inlinecode"><span class="id" type="var">n</span></span>. (If we simply say <span class="inlinecode"><span class="id" type="tactic">induction</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> without
introducing anything first, Coq will automatically introduce
<span class="inlinecode"><span class="id" type="var">n</span></span> for us!)
<div class="paragraph"> </div>
What can we do about this? One possibility is to rewrite the
statement of the lemma so that <span class="inlinecode"><span class="id" type="var">m</span></span> is quantified before <span class="inlinecode"><span class="id" type="var">n</span></span>. This
will work, but it's not nice: We don't want to have to mangle the
statements of lemmas to fit the needs of a particular strategy for
proving them — we want to state them in the most clear and
natural way.
<div class="paragraph"> </div>
What we can do instead is to first introduce all the
quantified variables and then <i>re-generalize</i> one or more of
them, taking them out of the context and putting them back at
the beginning of the goal. The <span class="inlinecode"><span class="id" type="tactic">generalize</span></span> <span class="inlinecode"><span class="id" type="tactic">dependent</span></span> tactic
does this.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">double_injective_take2</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">double</span> <span class="id" type="var">n</span> = <span class="id" type="var">double</span> <span class="id" type="var">m</span> <span style="font-family: arial;">→</span><br/>
<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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span>.<br/>
<span class="comment">(* <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 in the context *)</span><br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">n</span>.<br/>
<span class="comment">(* Now <span class="inlinecode"><span class="id" type="var">n</span></span> is back in the goal and we can do induction on<br/>
<span class="inlinecode"><span class="id" type="var">m</span></span> and get a sufficiently general IH. *)</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">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">destruct</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">SCase</span> "n = O". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">SCase</span> "n = S n'". <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>.<br/>
<span class="id" type="var">Case</span> "m = S m'". <span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">destruct</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">SCase</span> "n = O". <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>.<br/>
<span class="id" type="var">SCase</span> "n = S n'". <span class="id" type="tactic">apply</span> <span class="id" type="tactic">f_equal</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHm'</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">eq</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Let's look at an informal proof of this theorem. Note that
the proposition we prove by induction leaves <span class="inlinecode"><span class="id" type="var">n</span></span> quantified,
corresponding to the use of generalize dependent in our formal
proof.
<div class="paragraph"> </div>
<i>Theorem</i>: For any nats <span class="inlinecode"><span class="id" type="var">n</span></span> and <span class="inlinecode"><span class="id" type="var">m</span></span>, if <span class="inlinecode"><span class="id" type="var">double</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">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span>, then
<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>.
<div class="paragraph"> </div>
<i>Proof</i>: Let <span class="inlinecode"><span class="id" type="var">m</span></span> be a <span class="inlinecode"><span class="id" type="var">nat</span></span>. We prove by induction on <span class="inlinecode"><span class="id" type="var">m</span></span> that, for
any <span class="inlinecode"><span class="id" type="var">n</span></span>, if <span class="inlinecode"><span class="id" type="var">double</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">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span> then <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>.
<div class="paragraph"> </div>
<ul class="doclist">
<li> First, suppose <span class="inlinecode"><span class="id" type="var">m</span></span> <span class="inlinecode">=</span> <span class="inlinecode">0</span>, and suppose <span class="inlinecode"><span class="id" type="var">n</span></span> is a number such
that <span class="inlinecode"><span class="id" type="var">double</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">double</span></span> <span class="inlinecode"><span class="id" type="var">m</span></span>. We must show that <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode">0</span>.
<div class="paragraph"> </div>
Since <span class="inlinecode"><span class="id" type="var">m</span></span> <span class="inlinecode">=</span> <span class="inlinecode">0</span>, by the definition of <span class="inlinecode"><span class="id" type="var">double</span></span> we have <span class="inlinecode"><span class="id" type="var">double</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">=</span>
<span class="inlinecode">0</span>. There are two cases to consider for <span class="inlinecode"><span class="id" type="var">n</span></span>. If <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode">0</span> we are
done, since this is what we wanted to show. Otherwise, if <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> for some <span class="inlinecode"><span class="id" type="var">n'</span></span>, we derive a contradiction: by the definition of
<span class="inlinecode"><span class="id" type="var">double</span></span> we would have <span class="inlinecode"><span class="id" type="var">double</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">S</span></span> <span class="inlinecode">(<span class="id" type="var">S</span></span> <span class="inlinecode">(<span class="id" type="var">double</span></span> <span class="inlinecode"><span class="id" type="var">n'</span>))</span>, but this
contradicts the assumption that <span class="inlinecode"><span class="id" type="var">double</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode">0</span>.
<div class="paragraph"> </div>