-
Notifications
You must be signed in to change notification settings - Fork 1
/
UseTactics.html
1669 lines (1326 loc) · 157 KB
/
UseTactics.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>UseTactics: Tactic Library for Coq: A Gentle Introduction</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">UseTactics<span class="subtitle">Tactic Library for Coq: A Gentle Introduction</span></h1>
<div class="code code-tight">
</div>
<div class="doc">
</div>
<div class="code code-tight">
<br/>
<span class="comment">(* $Date: 2013-07-17 16:19:11 -0400 (Wed, 17 Jul 2013) $ *)</span><br/>
<span class="comment">(* Chapter maintained by Arthur Chargueraud *)</span><br/>
<br/>
</div>
<div class="doc">
Coq comes with a set of builtin tactics, such as <span class="inlinecode"><span class="id" type="tactic">reflexivity</span></span>,
<span class="inlinecode"><span class="id" type="tactic">intros</span></span>, <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> and so on. While it is possible to conduct
proofs using only those tactics, you can significantly increase
your productivity by working with a set of more powerful tactics.
This chapter describes a number of such very useful tactics, which,
for various reasons, are not yet available by default in Coq.
These tactics are defined in the <span class="inlinecode"><span class="id" type="var">LibTactics.v</span></span> file.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <span class="id" type="var">LibTactics</span>.<br/>
<br/>
</div>
<div class="doc">
Remark: SSReflect is another package providing powerful tactics.
The library "LibTactics" differs from "SSReflect" in two respects:
<div class="paragraph"> </div>
<ul class="doclist">
<li> "SSReflect" was primarily developed for proving mathematical
theorems, whereas "LibTactics" was primarily developed for proving
theorems on programming languages. In particular, "LibTactics"
provides a number of useful tactics that have no counterpart in the
"SSReflect" package.
</li>
<li> "SSReflect" entirely rethinks the presentation of tactics,
whereas "LibTactics" mostly stick to the traditional
presentation of Coq tactics, simply providing a number of
additional tactics. For this reason, "LibTactics" is
probably easier to get started with than "SSReflect".
</li>
</ul>
<div class="paragraph"> </div>
This chapter is a tutorial focusing on the most useful features
from the "LibTactics" library. It does not aim at presenting all
the features of "LibTactics". The detailed specification of tactics
can be found in the source file <span class="inlinecode"><span class="id" type="var">LibTactics.v</span></span>. Further documentation
as well as demos can be found at http://www.chargueraud.org/softs/tlc/ .
<div class="paragraph"> </div>
In this tutorial, tactics are presented using examples taken from
the core chapters of the "Software Foundations" course. To illustrate
the various ways in which a given tactic can be used, we use a
tactic that duplicates a given goal. More precisely, <span class="inlinecode"><span class="id" type="var">dup</span></span> produces
two copies of the current goal, and <span class="inlinecode"><span class="id" type="var">dup</span></span> <span class="inlinecode"><span class="id" type="var">n</span></span> produces <span class="inlinecode"><span class="id" type="var">n</span></span> copies of it.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab971"></a><h1 class="section">Tactics for introduction and case analysis</h1>
<div class="paragraph"> </div>
This section presents the following tactics:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">introv</span></span>, for naming hypotheses more efficiently,
</li>
<li> <span class="inlinecode"><span class="id" type="var">inverts</span></span>, for improving the <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic,
</li>
<li> <span class="inlinecode"><span class="id" type="var">cases</span></span>, for performing a case analysis without losing information,
</li>
<li> <span class="inlinecode"><span class="id" type="var">cases_if</span></span>, for automating case analysis on the argument of <span class="inlinecode"><span class="id" type="keyword">if</span></span>.
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab972"></a><h2 class="section">The tactic <span class="inlinecode"><span class="id" type="var">introv</span></span></h2>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">IntrovExamples</span>.<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <span class="id" type="var">Stlc</span>.<br/>
<span class="id" type="keyword">Import</span> <span class="id" type="var">Imp</span> <span class="id" type="var">STLC</span>.<br/>
<br/>
</div>
<div class="doc">
The tactic <span class="inlinecode"><span class="id" type="var">introv</span></span> allows to automatically introduce the
variables of a theorem and explicitly name the hypotheses
involved. In the example shown next, the variables <span class="inlinecode"><span class="id" type="var">c</span></span>,
<span class="inlinecode"><span class="id" type="var">st</span></span>, <span class="inlinecode"><span class="id" type="var">st1</span></span> and <span class="inlinecode"><span class="id" type="var">st2</span></span> involved in the statement of determinism
need not be named explicitly, because their name where already
given in the statement of the lemma. On the contrary, it is
useful to provide names for the two hypotheses, which we
name <span class="inlinecode"><span class="id" type="var">E1</span></span> and <span class="inlinecode"><span class="id" type="var">E2</span></span>, respectively.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ceval_deterministic</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st1</span> <span class="id" type="var">st2</span>,<br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st1</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st2</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">st1</span> = <span class="id" type="var">st2</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span> <span class="id" type="var">E1</span> <span class="id" type="var">E2</span>. <span class="comment">(* was <span class="inlinecode"><span class="id" type="tactic">intros</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">st1</span></span> <span class="inlinecode"><span class="id" type="var">st2</span></span> <span class="inlinecode"><span class="id" type="var">E1</span></span> <span class="inlinecode"><span class="id" type="var">E2</span></span> *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
When there is no hypothesis to be named, one can call
<span class="inlinecode"><span class="id" type="var">introv</span></span> without any argument.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">dist_exists_or</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">P</span> <span class="id" type="var">Q</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">P</span> <span class="id" type="var">x</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">Q</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">↔</span> (<span style="font-family: arial;">∃</span><span class="id" type="var">x</span>, <span class="id" type="var">P</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">∨</span> (<span style="font-family: arial;">∃</span><span class="id" type="var">x</span>, <span class="id" type="var">Q</span> <span class="id" type="var">x</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span>. <span class="comment">(* was <span class="inlinecode"><span class="id" type="tactic">intros</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
The tactic <span class="inlinecode"><span class="id" type="var">introv</span></span> also applies to statements in which
<span class="inlinecode"><span style="font-family: arial;">∀</span></span> and <span class="inlinecode"><span style="font-family: arial;">→</span></span> are interleaved.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ceval_deterministic'</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st1</span>,<br/>
(<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st1</span>) <span style="font-family: arial;">→</span> <span style="font-family: arial;">∀</span><span class="id" type="var">st2</span>, (<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st2</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">st1</span> = <span class="id" type="var">st2</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span> <span class="id" type="var">E1</span> <span class="id" type="var">E2</span>. <span class="comment">(* was <span class="inlinecode"><span class="id" type="tactic">intros</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span class="id" type="var">st1</span></span> <span class="inlinecode"><span class="id" type="var">E1</span></span> <span class="inlinecode"><span class="id" type="var">st2</span></span> <span class="inlinecode"><span class="id" type="var">E2</span></span> *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
Like the arguments of <span class="inlinecode"><span class="id" type="tactic">intros</span></span>, the arguments of <span class="inlinecode"><span class="id" type="var">introv</span></span>
can be structured patterns.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">exists_impl</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">X</span> (<span class="id" type="var">P</span> : <span class="id" type="var">X</span> <span style="font-family: arial;">→</span> <span class="id" type="keyword">Prop</span>) (<span class="id" type="var">Q</span> : <span class="id" type="keyword">Prop</span>) (<span class="id" type="var">R</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">P</span> <span class="id" type="var">x</span> <span style="font-family: arial;">→</span> <span class="id" type="var">Q</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">P</span> <span class="id" type="var">x</span>) <span style="font-family: arial;">→</span> <span class="id" type="var">Q</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span> [<span class="id" type="var">x</span> <span class="id" type="var">H2</span>]. <span class="id" type="tactic">eauto</span>.<br/>
<span class="comment">(* same as <span class="inlinecode"><span class="id" type="tactic">intros</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> <span class="inlinecode"><span class="id" type="var">R</span></span> <span class="inlinecode"><span class="id" type="var">H1</span></span> <span class="inlinecode">[<span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">H2</span>].</span>, which is itself short <br/>
for <span class="inlinecode"><span class="id" type="tactic">intros</span></span> <span class="inlinecode"><span class="id" type="var">X</span></span> <span class="inlinecode"><span class="id" type="var">P</span></span> <span class="inlinecode"><span class="id" type="var">Q</span></span> <span class="inlinecode"><span class="id" type="var">R</span></span> <span class="inlinecode"><span class="id" type="var">H1</span></span> <span class="inlinecode"><span class="id" type="var">H2</span>.</span> <span class="inlinecode"><span class="id" type="tactic">destruct</span></span> <span class="inlinecode"><span class="id" type="var">H2</span></span> <span class="inlinecode"><span class="id" type="keyword">as</span></span> <span class="inlinecode">[<span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">H2</span>].</span> *)</span><br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Remark: the tactic <span class="inlinecode"><span class="id" type="var">introv</span></span> works even when definitions
need to be unfolded in order to reveal hypotheses.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">End</span> <span class="id" type="var">IntrovExamples</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab973"></a><h2 class="section">The tactic <span class="inlinecode"><span class="id" type="var">inverts</span></span></h2>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">InvertsExamples</span>.<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <span class="id" type="var">Stlc</span> <span class="id" type="var">Equiv</span> <span class="id" type="var">Imp</span>.<br/>
<span class="id" type="keyword">Import</span> <span class="id" type="var">STLC</span>.<br/>
<br/>
</div>
<div class="doc">
The <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> tactic of Coq is not very satisfying for
three reasons. First, it produces a bunch of equalities
which one typically wants to substitute away, using <span class="inlinecode"><span class="id" type="tactic">subst</span></span>.
Second, it introduces meaningless names for hypotheses.
Third, a call to <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> does not remove <span class="inlinecode"><span class="id" type="var">H</span></span> from the
context, even though in most cases an hypothesis is no longer
needed after being inverted. The tactic <span class="inlinecode"><span class="id" type="var">inverts</span></span> address all
of these three issues. It is intented to be used in place of
the tactic <span class="inlinecode"><span class="id" type="tactic">inversion</span></span>.
<div class="paragraph"> </div>
The following example illustrates how the tactic <span class="inlinecode"><span class="id" type="var">inverts</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span>
behaves mostly like <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> except that it performs
some substitutions in order to eliminate the trivial equalities
that are being produced by <span class="inlinecode"><span class="id" type="tactic">inversion</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">skip_left</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span>,<br/>
<span class="id" type="var">cequiv</span> (<span class="id" type="var">SKIP</span>;; <span class="id" type="var">c</span>) <span class="id" type="var">c</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span>. <span class="id" type="tactic">split</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">dup</span>. <span class="comment">(* duplicate the goal for comparison *)</span><br/>
<span class="comment">(* was: *)</span><br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="comment">(* now: *)</span><br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H</span>. <span class="id" type="var">inverts</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
A slightly more interesting example appears next.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ceval_deterministic</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st1</span> <span class="id" type="var">st2</span>,<br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st1</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st2</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">st1</span> = <span class="id" type="var">st2</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span> <span class="id" type="var">E1</span> <span class="id" type="var">E2</span>. <span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">st2</span>.<br/>
(<span class="id" type="var">ceval_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">E1</span>) <span class="id" type="var">Case</span>); <span class="id" type="tactic">intros</span> <span class="id" type="var">st2</span> <span class="id" type="var">E2</span>.<br/>
<span class="id" type="var">admit</span>. <span class="id" type="var">admit</span>. <span class="comment">(* skip some basic cases *)</span><br/>
<span class="id" type="var">dup</span>. <span class="comment">(* duplicate the goal for comparison *)</span><br/>
<span class="comment">(* was: *)</span> <span class="id" type="tactic">inversion</span> <span class="id" type="var">E2</span>. <span class="id" type="tactic">subst</span>. <span class="id" type="var">admit</span>.<br/>
<span class="comment">(* now: *)</span> <span class="id" type="var">inverts</span> <span class="id" type="var">E2</span>. <span class="id" type="var">admit</span>.<br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
The tactic <span class="inlinecode"><span class="id" type="var">inverts</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode"><span class="id" type="keyword">as</span>.</span> is like <span class="inlinecode"><span class="id" type="var">inverts</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> except that the
variables and hypotheses being produced are placed in the goal
rather than in the context. This strategy allows naming those
new variables and hypotheses explicitly, using either <span class="inlinecode"><span class="id" type="tactic">intros</span></span>
or <span class="inlinecode"><span class="id" type="var">introv</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">ceval_deterministic'</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span> <span class="id" type="var">st</span> <span class="id" type="var">st1</span> <span class="id" type="var">st2</span>,<br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st1</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">c</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇓</span> <span class="id" type="var">st2</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">st1</span> = <span class="id" type="var">st2</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span> <span class="id" type="var">E1</span> <span class="id" type="var">E2</span>. <span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">st2</span>.<br/>
(<span class="id" type="var">ceval_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">E1</span>) <span class="id" type="var">Case</span>); <span class="id" type="tactic">intros</span> <span class="id" type="var">st2</span> <span class="id" type="var">E2</span>; <br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">E2</span> <span class="id" type="keyword">as</span>.<br/>
<span class="id" type="var">Case</span> "E_Skip". <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "E_Ass".<br/>
<span class="comment">(* Observe that the variable <span class="inlinecode"><span class="id" type="var">n</span></span> is not automatically <br/>
substituted because, contrary to <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> <span class="inlinecode"><span class="id" type="var">E2</span>;</span> <span class="inlinecode"><span class="id" type="tactic">subst</span></span>,<br/>
the tactic <span class="inlinecode"><span class="id" type="var">inverts</span></span> <span class="inlinecode"><span class="id" type="var">E2</span></span> does not substitute the equalities<br/>
that exist before running the inversion. *)</span><br/>
<span class="comment">(* new: *)</span> <span class="id" type="tactic">subst</span> <span class="id" type="var">n</span>.<br/>
<span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="var">Case</span> "E_Seq".<br/>
<span class="comment">(* Here, the newly created variables can be introduced<br/>
using intros, so they can be assigned meaningful names,<br/>
for example <span class="inlinecode"><span class="id" type="var">st3</span></span> instead of <span class="inlinecode"><span class="id" type="var">st'0</span></span>. *)</span><br/>
<span class="comment">(* new: *)</span> <span class="id" type="tactic">intros</span> <span class="id" type="var">st3</span> <span class="id" type="var">Red1</span> <span class="id" type="var">Red2</span>.<br/>
<span class="id" type="tactic">assert</span> (<span class="id" type="var">st'</span> = <span class="id" type="var">st3</span>) <span class="id" type="keyword">as</span> <span class="id" type="var">EQ1</span>.<br/>
<span class="id" type="var">SCase</span> "Proof of assertion". <span class="id" type="tactic">apply</span> <span class="id" type="var">IHE1_1</span>; <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="tactic">subst</span> <span class="id" type="var">st3</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHE1_2</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="var">Case</span> "E_IfTrue".<br/>
<span class="id" type="var">SCase</span> "b1 evaluates to true".<br/>
<span class="comment">(* In an easy case like this one, there is no need to<br/>
provide meaningful names, so we can just use <span class="inlinecode"><span class="id" type="tactic">intros</span></span> *)</span><br/>
<span class="comment">(* new: *)</span> <span class="id" type="tactic">intros</span>.<br/>
<span class="id" type="tactic">apply</span> <span class="id" type="var">IHE1</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="var">SCase</span> "b1 evaluates to false (contradiction)".<br/>
<span class="comment">(* new: *)</span> <span class="id" type="tactic">intros</span>.<br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">H</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H5</span>. <span class="id" type="tactic">inversion</span> <span class="id" type="var">H5</span>.<br/>
<span class="comment">(* The other cases are similiar *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
In the particular case where a call to <span class="inlinecode"><span class="id" type="tactic">inversion</span></span> produces
a single subgoal, one can use the syntax <span class="inlinecode"><span class="id" type="var">inverts</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode"><span class="id" type="keyword">as</span></span> <span class="inlinecode"><span class="id" type="var">H1</span></span> <span class="inlinecode"><span class="id" type="var">H2</span></span> <span class="inlinecode"><span class="id" type="var">H3</span></span>
for calling <span class="inlinecode"><span class="id" type="var">inverts</span></span> and naming the new hypotheses <span class="inlinecode"><span class="id" type="var">H1</span></span>, <span class="inlinecode"><span class="id" type="var">H2</span></span>
and <span class="inlinecode"><span class="id" type="var">H3</span></span>. In other words, the tactic <span class="inlinecode"><span class="id" type="var">inverts</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode"><span class="id" type="keyword">as</span></span> <span class="inlinecode"><span class="id" type="var">H1</span></span> <span class="inlinecode"><span class="id" type="var">H2</span></span> <span class="inlinecode"><span class="id" type="var">H3</span></span> is
equivalent to <span class="inlinecode"><span class="id" type="var">inverts</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode"><span class="id" type="keyword">as</span>;</span> <span class="inlinecode"><span class="id" type="var">introv</span></span> <span class="inlinecode"><span class="id" type="var">H1</span></span> <span class="inlinecode"><span class="id" type="var">H2</span></span> <span class="inlinecode"><span class="id" type="var">H3</span></span>. An example follows.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">skip_left'</span>: <span style="font-family: arial;">∀</span><span class="id" type="var">c</span>,<br/>
<span class="id" type="var">cequiv</span> (<span class="id" type="var">SKIP</span>;; <span class="id" type="var">c</span>) <span class="id" type="var">c</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span>. <span class="id" type="tactic">split</span>; <span class="id" type="tactic">intros</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> <span class="id" type="var">U</span> <span class="id" type="var">V</span>. <span class="comment">(* new hypotheses are named <span class="inlinecode"><span class="id" type="var">U</span></span> and <span class="inlinecode"><span class="id" type="var">V</span></span> *)</span><br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">U</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
A more involved example appears next. In particular, this example
shows that the name of the hypothesis being inverted can be reused.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Example</span> <span class="id" type="var">typing_nonexample_1</span> :<br/>
¬ <span style="font-family: arial;">∃</span><span class="id" type="var">T</span>,<br/>
<span class="id" type="var">has_type</span> <span class="id" type="var">empty</span> <br/>
(<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">TBool</span><br/>
(<span class="id" type="var">tabs</span> <span class="id" type="var">y</span> <span class="id" type="var">TBool</span><br/>
(<span class="id" type="var">tapp</span> (<span class="id" type="var">tvar</span> <span class="id" type="var">x</span>) (<span class="id" type="var">tvar</span> <span class="id" type="var">y</span>))))<br/>
<span class="id" type="var">T</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">dup</span> 3.<br/>
<br/>
<span class="comment">(* The old proof: *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">C</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">C</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">clear</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H5</span>. <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">clear</span> <span class="id" type="var">H5</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H4</span>. <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">clear</span> <span class="id" type="var">H4</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H2</span>. <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">clear</span> <span class="id" type="var">H2</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H5</span>. <span class="id" type="tactic">subst</span>. <span class="id" type="tactic">clear</span> <span class="id" type="var">H5</span>.<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">H1</span>.<br/>
<br/>
<span class="comment">(* The new proof: *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">C</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">C</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> <span class="id" type="var">H1</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H1</span> <span class="id" type="keyword">as</span> <span class="id" type="var">H2</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H2</span> <span class="id" type="keyword">as</span> <span class="id" type="var">H3</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H3</span> <span class="id" type="keyword">as</span> <span class="id" type="var">H4</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H4</span>.<br/>
<br/>
<span class="comment">(* The new proof, alternative: *)</span><br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">C</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">C</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H</span> <span class="id" type="keyword">as</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="var">inverts</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="id" type="keyword">End</span> <span class="id" type="var">InvertsExamples</span>.<br/>
<br/>
</div>
<div class="doc">
Note: in the rare cases where one needs to perform an inversion
on an hypothesis <span class="inlinecode"><span class="id" type="var">H</span></span> without clearing <span class="inlinecode"><span class="id" type="var">H</span></span> from the context,
one can use the tactic <span class="inlinecode"><span class="id" type="var">inverts</span></span> <span class="inlinecode"><span class="id" type="var">keep</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span>, where the keyword <span class="inlinecode"><span class="id" type="var">keep</span></span>
indicates that the hypothesis should be kept in the context.
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab974"></a><h1 class="section">Tactics for n-ary connectives</h1>
<div class="paragraph"> </div>
Because Coq encodes conjunctions and disjunctions using binary
constructors <span class="inlinecode"><span style="font-family: arial;">∧</span></span> and <span class="inlinecode"><span style="font-family: arial;">∨</span></span>, working with a conjunction or a
disjunction of <span class="inlinecode"><span class="id" type="var">N</span></span> facts can sometimes be quite cumbursome.
For this reason, "LibTactics" provides tactics offering direct
support for n-ary conjunctions and disjunctions. It also provides
direct support for n-ary existententials.
<div class="paragraph"> </div>
This section presents the following tactics:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">splits</span></span> for decomposing n-ary conjunctions,
</li>
<li> <span class="inlinecode"><span class="id" type="var">branch</span></span> for decomposing n-ary disjunctions,
</li>
<li> <span class="inlinecode"><span style="font-family: arial;">∃</span></span> for proving n-ary existentials.
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">NaryExamples</span>.<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <span class="id" type="var">References</span> <span class="id" type="var">SfLib</span>.<br/>
<span class="id" type="keyword">Import</span> <span class="id" type="var">STLCRef</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab975"></a><h2 class="section">The tactic <span class="inlinecode"><span class="id" type="var">splits</span></span></h2>
<div class="paragraph"> </div>
The tactic <span class="inlinecode"><span class="id" type="var">splits</span></span> applies to a goal made of a conjunction
of <span class="inlinecode"><span class="id" type="var">n</span></span> propositions and it produces <span class="inlinecode"><span class="id" type="var">n</span></span> subgoals. For example,
it decomposes the goal <span class="inlinecode"><span class="id" type="var">G1</span></span> <span class="inlinecode"><span style="font-family: arial;">∧</span></span> <span class="inlinecode"><span class="id" type="var">G2</span></span> <span class="inlinecode"><span style="font-family: arial;">∧</span></span> <span class="inlinecode"><span class="id" type="var">G3</span></span> into the three subgoals
<span class="inlinecode"><span class="id" type="var">G1</span></span>, <span class="inlinecode"><span class="id" type="var">G2</span></span> and <span class="inlinecode"><span class="id" type="var">G3</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_splits</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> > 0 <span style="font-family: arial;">∧</span> <span class="id" type="var">n</span> < <span class="id" type="var">m</span> <span style="font-family: arial;">∧</span> <span class="id" type="var">m</span> < <span class="id" type="var">n</span>+10 <span style="font-family: arial;">∧</span> <span class="id" type="var">m</span> ≠ 3.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="var">splits</span>.<br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab976"></a><h2 class="section">The tactic <span class="inlinecode"><span class="id" type="var">branch</span></span></h2>
<div class="paragraph"> </div>
The tactic <span class="inlinecode"><span class="id" type="var">branch</span></span> <span class="inlinecode"><span class="id" type="var">k</span></span> can be used to prove a n-ary disjunction.
For example, if the goal takes the form <span class="inlinecode"><span class="id" type="var">G1</span></span> <span class="inlinecode"><span style="font-family: arial;">∨</span></span> <span class="inlinecode"><span class="id" type="var">G2</span></span> <span class="inlinecode"><span style="font-family: arial;">∨</span></span> <span class="inlinecode"><span class="id" type="var">G3</span></span>,
the tactic <span class="inlinecode"><span class="id" type="var">branch</span></span> <span class="inlinecode">2</span> leaves only <span class="inlinecode"><span class="id" type="var">G2</span></span> as subgoal. The following
example illustrates the behavior of the <span class="inlinecode"><span class="id" type="var">branch</span></span> tactic.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_branch</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">m</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">n</span> = <span class="id" type="var">m</span> <span style="font-family: arial;">∨</span> <span class="id" type="var">m</span> < <span class="id" type="var">n</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span>.<br/>
<span class="id" type="tactic">destruct</span> (<span class="id" type="var">lt_eq_lt_dec</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span>) <span class="id" type="keyword">as</span> [[<span class="id" type="var">H1</span>|<span class="id" type="var">H2</span>]|<span class="id" type="var">H3</span>].<br/>
<span class="id" type="var">branch</span> 1. <span class="id" type="tactic">apply</span> <span class="id" type="var">H1</span>.<br/>
<span class="id" type="var">branch</span> 2. <span class="id" type="tactic">apply</span> <span class="id" type="var">H2</span>.<br/>
<span class="id" type="var">branch</span> 3. <span class="id" type="tactic">apply</span> <span class="id" type="var">H3</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab977"></a><h2 class="section">The tactic <span class="inlinecode"><span style="font-family: arial;">∃</span></span></h2>
<div class="paragraph"> </div>
The library "LibTactics" introduces a notation for n-ary
existentials. For example, one can write <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">y</span></span> <span class="inlinecode"><span class="id" type="var">z</span>,</span> <span class="inlinecode"><span class="id" type="var">H</span></span>
instead of <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">x</span>,</span> <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">y</span>,</span> <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">z</span>,</span> <span class="inlinecode"><span class="id" type="var">H</span></span>. Similarly,
the library provides a n-ary tactic <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">a</span></span> <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span>, which is a
shorthand for <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">a</span>;</span> <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">b</span>;</span> <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">c</span></span>. The following
example illustrates both the notation and the tactic for
dealing with n-ary existentials.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="tactic">progress</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">ST</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span class="id" type="var">st</span>,<br/>
<span class="id" type="var">has_type</span> <span class="id" type="var">empty</span> <span class="id" type="var">ST</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">store_well_typed</span> <span class="id" type="var">ST</span> <span class="id" type="var">st</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">value</span> <span class="id" type="var">t</span> <span style="font-family: arial;">∨</span> <span style="font-family: arial;">∃</span><span class="id" type="var">t'</span> <span class="id" type="var">st'</span>, <span class="id" type="var">t</span> / <span class="id" type="var">st</span> <span style="font-family: arial;">⇒</span> <span class="id" type="var">t'</span> / <span class="id" type="var">st'</span>.<br/>
<span class="comment">(* was: <span class="inlinecode"><span class="id" type="var">value</span></span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode"><span style="font-family: arial;">∨</span></span> <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">t'</span>,</span> <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">st'</span>,</span> <span class="inlinecode"><span class="id" type="var">t</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st</span></span> <span class="inlinecode"><span style="font-family: arial;">⇒</span></span> <span class="inlinecode"><span class="id" type="var">t'</span></span> <span class="inlinecode">/</span> <span class="inlinecode"><span class="id" type="var">st'</span></span> *)</span><br/>
<span class="id" type="keyword">Proof</span> <span class="id" type="keyword">with</span> <span class="id" type="tactic">eauto</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">ST</span> <span class="id" type="var">t</span> <span class="id" type="var">T</span> <span class="id" type="var">st</span> <span class="id" type="var">Ht</span> <span class="id" type="var">HST</span>. <span class="id" type="var">remember</span> (@<span class="id" type="var">empty</span> <span class="id" type="var">ty</span>) <span class="id" type="keyword">as</span> <span style="font-family: serif; font-size:85%;">Γ</span>.<br/>
(<span class="id" type="var">has_type_cases</span> (<span class="id" type="tactic">induction</span> <span class="id" type="var">Ht</span>) <span class="id" type="var">Case</span>); <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">try</span> <span class="id" type="var">solve</span> <span class="id" type="tactic">by</span> <span class="id" type="tactic">inversion</span>...<br/>
<span class="id" type="var">Case</span> "T_App".<br/>
<span class="id" type="var">right</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHt1</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">Ht1p</span> | <span class="id" type="var">Ht1p</span>]...<br/>
<span class="id" type="var">SCase</span> "t<sub>1</sub> is a value".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">Ht1p</span>; <span class="id" type="tactic">subst</span>; <span class="id" type="tactic">try</span> <span class="id" type="var">solve</span> <span class="id" type="tactic">by</span> <span class="id" type="tactic">inversion</span>.<br/>
<span class="id" type="tactic">destruct</span> <span class="id" type="var">IHHt2</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">Ht2p</span> | <span class="id" type="var">Ht2p</span>]...<br/>
<span class="id" type="var">SSCase</span> "t<sub>2</sub> steps".<br/>
<span class="id" type="tactic">inversion</span> <span class="id" type="var">Ht2p</span> <span class="id" type="keyword">as</span> [<span class="id" type="var">t<sub>2</sub>'</span> [<span class="id" type="var">st'</span> <span class="id" type="var">Hstep</span>]].<br/>
<span style="font-family: arial;">∃</span>(<span class="id" type="var">tapp</span> (<span class="id" type="var">tabs</span> <span class="id" type="var">x</span> <span class="id" type="var">T</span> <span class="id" type="var">t</span>) <span class="id" type="var">t<sub>2</sub>'</span>) <span class="id" type="var">st'</span>...<br/>
<span class="comment">(* was: <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode">(<span class="id" type="var">tapp</span></span> <span class="inlinecode">(<span class="id" type="var">tabs</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode"><span class="id" type="var">T</span></span> <span class="inlinecode"><span class="id" type="var">t</span>)</span> <span class="inlinecode"><span class="id" type="var">t<sub>2</sub>'</span>).</span> <span class="inlinecode"><span style="font-family: arial;">∃</span></span> <span class="inlinecode"><span class="id" type="var">st'</span>...</span> *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
Remark: a similar facility for n-ary existentials is provided
by the module <span class="inlinecode"><span class="id" type="var">Coq.Program.Syntax</span></span> from the standard library.
(<span class="inlinecode"><span class="id" type="var">Coq.Program.Syntax</span></span> supports existentials up to arity 4;
<span class="inlinecode"><span class="id" type="var">LibTactics</span></span> supports them up to arity 10.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">End</span> <span class="id" type="var">NaryExamples</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab978"></a><h1 class="section">Tactics for working with equality</h1>
<div class="paragraph"> </div>
One of the major weakness of Coq compared with other interactive
proof assistants is its relatively poor support for reasoning
with equalities. The tactics described next aims at simplifying
pieces of proof scripts manipulating equalities.
<div class="paragraph"> </div>
This section presents the following tactics:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">asserts_rewrite</span></span> for introducing an equality to rewrite with,
</li>
<li> <span class="inlinecode"><span class="id" type="var">cuts_rewrite</span></span>, which is similar except that its subgoals are swapped,
</li>
<li> <span class="inlinecode"><span class="id" type="var">substs</span></span> for improving the <span class="inlinecode"><span class="id" type="tactic">subst</span></span> tactic,
</li>
<li> <span class="inlinecode"><span class="id" type="var">fequals</span></span> for improving the <span class="inlinecode"><span class="id" type="tactic">f_equal</span></span> tactic,
</li>
<li> <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> for proving <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> using an hypothesis <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">z</span></span>,
automatically producing an equality <span class="inlinecode"><span class="id" type="var">y</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">z</span></span> as subgoal.
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">EqualityExamples</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab979"></a><h2 class="section">The tactics <span class="inlinecode"><span class="id" type="var">asserts_rewrite</span></span> and <span class="inlinecode"><span class="id" type="var">cuts_rewrite</span></span></h2>
<div class="paragraph"> </div>
The tactic <span class="inlinecode"><span class="id" type="var">asserts_rewrite</span></span> <span class="inlinecode">(<span class="id" type="var">E1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">E2</span>)</span> replaces <span class="inlinecode"><span class="id" type="var">E1</span></span> with <span class="inlinecode"><span class="id" type="var">E2</span></span> in
the goal, and produces the goal <span class="inlinecode"><span class="id" type="var">E1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">E2</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">mult_0_plus</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span> <span class="id" type="var">m</span> : <span class="id" type="var">nat</span>,<br/>
(0 + <span class="id" type="var">n</span>) × <span class="id" type="var">m</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="id" type="var">dup</span>.<br/>
<span class="comment">(* The old 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="id" type="tactic">assert</span> (<span class="id" type="var">H</span>: 0 + <span class="id" type="var">n</span> = <span class="id" type="var">n</span>). <span class="id" type="tactic">reflexivity</span>. <span class="id" type="tactic">rewrite</span> <span style="font-family: arial;">→</span> <span class="id" type="var">H</span>.<br/>
<span class="id" type="tactic">reflexivity</span>.<br/>
<br/>
<span class="comment">(* The new 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="id" type="var">asserts_rewrite</span> (0 + <span class="id" type="var">n</span> = <span class="id" type="var">n</span>).<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="comment">(* subgoal <span class="inlinecode">0+<span class="id" type="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">n</span></span> *)</span><br/>
<span class="id" type="tactic">reflexivity</span>. <span class="comment">(* subgoal <span class="inlinecode"><span class="id" type="var">n</span>×<span class="id" type="var">m</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">m</span>×<span class="id" type="var">n</span></span> *)</span><br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
<span class="comment">(*** Remark: the syntax <span class="inlinecode"><span class="id" type="var">asserts_rewrite</span></span> <span class="inlinecode">(<span class="id" type="var">E1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">E2</span>)</span> <span class="inlinecode"><span class="id" type="keyword">in</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> allows<br/>
rewriting in the hypothesis <span class="inlinecode"><span class="id" type="var">H</span></span> rather than in the goal. *)</span><br/>
<br/>
</div>
<div class="doc">
The tactic <span class="inlinecode"><span class="id" type="var">cuts_rewrite</span></span> <span class="inlinecode">(<span class="id" type="var">E1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">E2</span>)</span> is like
<span class="inlinecode"><span class="id" type="var">asserts_rewrite</span></span> <span class="inlinecode">(<span class="id" type="var">E1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">E2</span>)</span>, except that the equality <span class="inlinecode"><span class="id" type="var">E1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">E2</span></span>
appears as first subgoal.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">mult_0_plus'</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">n</span> <span class="id" type="var">m</span> : <span class="id" type="var">nat</span>,<br/>
(0 + <span class="id" type="var">n</span>) × <span class="id" type="var">m</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="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">m</span>.<br/>
<span class="id" type="var">cuts_rewrite</span> (0 + <span class="id" type="var">n</span> = <span class="id" type="var">n</span>).<br/>
<span class="id" type="tactic">reflexivity</span>. <span class="comment">(* subgoal <span class="inlinecode"><span class="id" type="var">n</span>×<span class="id" type="var">m</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">m</span>×<span class="id" type="var">n</span></span> *)</span><br/>
<span class="id" type="tactic">reflexivity</span>. <span class="comment">(* subgoal <span class="inlinecode">0+<span class="id" type="var">n</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">n</span></span> *)</span><br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
More generally, the tactics <span class="inlinecode"><span class="id" type="var">asserts_rewrite</span></span> and <span class="inlinecode"><span class="id" type="var">cuts_rewrite</span></span>
can be provided a lemma as argument. For example, one can write
<span class="inlinecode"><span class="id" type="var">asserts_rewrite</span></span> <span class="inlinecode">(<span style="font-family: arial;">∀</span></span> <span class="inlinecode"><span class="id" type="var">a</span></span> <span class="inlinecode"><span class="id" type="var">b</span>,</span> <span class="inlinecode"><span class="id" type="var">a</span>*(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">b</span>)</span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">a</span>×<span class="id" type="var">b</span>+<span class="id" type="var">a</span>)</span>.
This formulation is useful when <span class="inlinecode"><span class="id" type="var">a</span></span> and <span class="inlinecode"><span class="id" type="var">b</span></span> are big terms,
since there is no need to repeat their statements.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Theorem</span> <span class="id" type="var">mult_0_plus''</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">u</span> <span class="id" type="var">v</span> <span class="id" type="var">w</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">nat</span>,<br/>
(<span class="id" type="var">u</span> + <span class="id" type="var">v</span>) × (<span class="id" type="var">S</span> (<span class="id" type="var">w</span> × <span class="id" type="var">x</span> + <span class="id" type="var">y</span>)) = <span class="id" type="var">z</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="var">asserts_rewrite</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">a</span>*(<span class="id" type="var">S</span> <span class="id" type="var">b</span>) = <span class="id" type="var">a</span>×<span class="id" type="var">b</span>+<span class="id" type="var">a</span>).<br/>
<span class="comment">(* first subgoal: <span class="inlinecode"><span style="font-family: arial;">∀</span></span> <span class="inlinecode"><span class="id" type="var">a</span></span> <span class="inlinecode"><span class="id" type="var">b</span>,</span> <span class="inlinecode"><span class="id" type="var">a</span>*(<span class="id" type="var">S</span></span> <span class="inlinecode"><span class="id" type="var">b</span>)</span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">a</span>×<span class="id" type="var">b</span>+<span class="id" type="var">a</span></span> *)</span><br/>
<span class="comment">(* second subgoal: <span class="inlinecode">(<span class="id" type="var">u</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">v</span>)</span> <span class="inlinecode">×</span> <span class="inlinecode">(<span class="id" type="var">w</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">y</span>)</span> <span class="inlinecode">+</span> <span class="inlinecode">(<span class="id" type="var">u</span></span> <span class="inlinecode">+</span> <span class="inlinecode"><span class="id" type="var">v</span>)</span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">z</span></span> *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab980"></a><h2 class="section">The tactic <span class="inlinecode"><span class="id" type="var">substs</span></span></h2>
<div class="paragraph"> </div>
The tactic <span class="inlinecode"><span class="id" type="var">substs</span></span> is similar to <span class="inlinecode"><span class="id" type="tactic">subst</span></span> except that it
does not fail when the goal contains "circular equalities",
such as <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">f</span></span> <span class="inlinecode"><span class="id" type="var">x</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_substs</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">x</span> <span class="id" type="var">y</span> (<span class="id" type="var">f</span>:<span class="id" type="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="var">nat</span>), <br/>
<span class="id" type="var">x</span> = <span class="id" type="var">f</span> <span class="id" type="var">x</span> <span style="font-family: arial;">→</span> <span class="id" type="var">y</span> = <span class="id" type="var">x</span> <span style="font-family: arial;">→</span> <span class="id" type="var">y</span> = <span class="id" type="var">f</span> <span class="id" type="var">x</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="var">substs</span>. <span class="comment">(* the tactic <span class="inlinecode"><span class="id" type="tactic">subst</span></span> would fail here *)</span><br/>
<span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab981"></a><h2 class="section">The tactic <span class="inlinecode"><span class="id" type="var">fequals</span></span></h2>
<div class="paragraph"> </div>
The tactic <span class="inlinecode"><span class="id" type="var">fequals</span></span> is similar to <span class="inlinecode"><span class="id" type="tactic">f_equal</span></span> except that it
directly discharges all the trivial subgoals produced. Moreover,
the tactic <span class="inlinecode"><span class="id" type="var">fequals</span></span> features an enhanced treatment of equalities
between tuples.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_fequals</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">nat</span>) (<span class="id" type="var">f</span> : <span class="id" type="var">nat</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">nat</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">nat</span>),<br/>
<span class="id" type="var">a</span> = 1 <span style="font-family: arial;">→</span> <span class="id" type="var">b</span> = <span class="id" type="var">e</span> <span style="font-family: arial;">→</span> <span class="id" type="var">e</span> = 2 <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">f</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">f</span> 1 2 <span class="id" type="var">c</span> 4.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span>. <span class="id" type="var">fequals</span>.<br/>
<span class="comment">(* subgoals <span class="inlinecode"><span class="id" type="var">a</span></span> <span class="inlinecode">=</span> <span class="inlinecode">1</span>, <span class="inlinecode"><span class="id" type="var">b</span></span> <span class="inlinecode">=</span> <span class="inlinecode">2</span> and <span class="inlinecode"><span class="id" type="var">c</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">c</span></span> are proved, <span class="inlinecode"><span class="id" type="var">d</span></span> <span class="inlinecode">=</span> <span class="inlinecode">4</span> remains *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab982"></a><h2 class="section">The tactic <span class="inlinecode"><span class="id" type="var">applys_eq</span></span></h2>
<div class="paragraph"> </div>
The tactic <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> is a variant of <span class="inlinecode"><span class="id" type="tactic">eapply</span></span> that introduces
equalities for subterms that do not unify. For example, assume
the goal is the proposition <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> and assume we have the
assumption <span class="inlinecode"><span class="id" type="var">H</span></span> asserting that <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">z</span></span> holds. We know that we can
prove <span class="inlinecode"><span class="id" type="var">y</span></span> to be equal to <span class="inlinecode"><span class="id" type="var">z</span></span>. So, we could call the tactic
<span class="inlinecode"><span class="id" type="var">assert_rewrite</span></span> <span class="inlinecode">(<span class="id" type="var">y</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">z</span>)</span> and change the goal to <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">z</span></span>, but
this would require copy-pasting the values of <span class="inlinecode"><span class="id" type="var">y</span></span> and <span class="inlinecode"><span class="id" type="var">z</span></span>.
With the tactic <span class="inlinecode"><span class="id" type="var">applys_eq</span></span>, we can call <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode">1</span>, which
proves the goal and leaves only the subgoal <span class="inlinecode"><span class="id" type="var">y</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">z</span></span>. The value <span class="inlinecode">1</span>
given as argument to <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> indicates that we want an equality
to be introduced for the first argument of <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> counting from
the right. The three following examples illustrate the behavior
of a call to <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode">1</span>, a call to <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode">2</span>, and a
call to <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode">1</span> <span class="inlinecode">2</span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Axiom</span> <span class="id" type="var">big_expression_using</span> : <span class="id" type="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="var">nat</span>. <span class="comment">(* Used in the example *)</span><br/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_applys_eq_1</span> : <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="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="keyword">Prop</span>) <span class="id" type="var">x</span> <span class="id" type="var">y</span> <span class="id" type="var">z</span>, <br/>
<span class="id" type="var">P</span> <span class="id" type="var">x</span> (<span class="id" type="var">big_expression_using</span> <span class="id" type="var">z</span>) <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">P</span> <span class="id" type="var">x</span> (<span class="id" type="var">big_expression_using</span> <span class="id" type="var">y</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span> <span class="id" type="var">H</span>. <span class="id" type="var">dup</span>.<br/>
<br/>
<span class="comment">(* The old proof: *)</span><br/>
<span class="id" type="tactic">assert</span> (<span class="id" type="var">Eq</span>: <span class="id" type="var">big_expression_using</span> <span class="id" type="var">y</span> = <span class="id" type="var">big_expression_using</span> <span class="id" type="var">z</span>).<br/>
<span class="id" type="var">admit</span>. <span class="comment">(* Assume we can prove this equality somehow. *)</span><br/>
<span class="id" type="tactic">rewrite</span> <span class="id" type="var">Eq</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">H</span>.<br/>
<br/>
<span class="comment">(* The new proof: *)</span><br/>
<span class="id" type="var">applys_eq</span> <span class="id" type="var">H</span> 1.<br/>
<span class="id" type="var">admit</span>. <span class="comment">(* Assume we can prove this equality somehow. *)</span><br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
If the mismatch was on the first argument of <span class="inlinecode"><span class="id" type="var">P</span></span> instead of
the second, we would have written <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode">2</span>. Recall
that the occurences are counted from the right.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_applys_eq_2</span> : <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="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="keyword">Prop</span>) <span class="id" type="var">x</span> <span class="id" type="var">y</span> <span class="id" type="var">z</span>, <br/>
<span class="id" type="var">P</span> (<span class="id" type="var">big_expression_using</span> <span class="id" type="var">z</span>) <span class="id" type="var">x</span> <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">P</span> (<span class="id" type="var">big_expression_using</span> <span class="id" type="var">y</span>) <span class="id" type="var">x</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span> <span class="id" type="var">H</span>. <span class="id" type="var">applys_eq</span> <span class="id" type="var">H</span> 2.<br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
</div>
<div class="doc">
When we have a mismatch on two arguments, we want to produce
two equalities. To achieve this, we may call <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> <span class="inlinecode">1</span> <span class="inlinecode">2</span>.
More generally, the tactic <span class="inlinecode"><span class="id" type="var">applys_eq</span></span> expects a lemma and a
sequence of natural numbers as arguments.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_applys_eq_3</span> : <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="var">nat</span><span style="font-family: arial;">→</span><span class="id" type="keyword">Prop</span>) <span class="id" type="var">x1</span> <span class="id" type="var">x2</span> <span class="id" type="var">y1</span> <span class="id" type="var">y2</span>, <br/>
<span class="id" type="var">P</span> (<span class="id" type="var">big_expression_using</span> <span class="id" type="var">x2</span>) (<span class="id" type="var">big_expression_using</span> <span class="id" type="var">y2</span>) <span style="font-family: arial;">→</span> <br/>
<span class="id" type="var">P</span> (<span class="id" type="var">big_expression_using</span> <span class="id" type="var">x1</span>) (<span class="id" type="var">big_expression_using</span> <span class="id" type="var">y1</span>).<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">introv</span> <span class="id" type="var">H</span>. <span class="id" type="var">applys_eq</span> <span class="id" type="var">H</span> 1 2.<br/>
<span class="comment">(* produces two subgoals:<br/>
<span class="inlinecode"><span class="id" type="var">big_expression_using</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">big_expression_using</span></span> <span class="inlinecode"><span class="id" type="var">x2</span></span><br/>
<span class="inlinecode"><span class="id" type="var">big_expression_using</span></span> <span class="inlinecode"><span class="id" type="var">y1</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">big_expression_using</span></span> <span class="inlinecode"><span class="id" type="var">y2</span></span> *)</span><br/>
<span class="id" type="keyword">Abort</span>.<br/>
<br/>
<span class="id" type="keyword">End</span> <span class="id" type="var">EqualityExamples</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab983"></a><h1 class="section">Some convenient shorthands</h1>
<div class="paragraph"> </div>
This section of the tutorial introduces a few tactics
that help make proof scripts shorter and more readable:
<div class="paragraph"> </div>
<ul class="doclist">
<li> <span class="inlinecode"><span class="id" type="var">unfolds</span></span> (without argument) for unfolding the head definition,
</li>
<li> <span class="inlinecode"><span class="id" type="var">false</span></span> for replacing the goal with <span class="inlinecode"><span class="id" type="var">False</span></span>,
</li>
<li> <span class="inlinecode"><span class="id" type="var">gen</span></span> as a shorthand for <span class="inlinecode"><span class="id" type="tactic">dependent</span></span> <span class="inlinecode"><span class="id" type="tactic">generalize</span></span>,
</li>
<li> <span class="inlinecode"><span class="id" type="var">skip</span></span> for skipping a subgoal even if it contains existential variables,
</li>
<li> <span class="inlinecode"><span class="id" type="var">sort</span></span> for re-ordering the proof context by moving moving all
propositions at the bottom.
</li>
</ul>
</div>
<div class="code code-tight">
<br/>
</div>
<div class="doc">
<a name="lab984"></a><h2 class="section">The tactic <span class="inlinecode"><span class="id" type="var">unfolds</span></span></h2>
</div>
<div class="code code-space">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">UnfoldsExample</span>.<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <span class="id" type="var">Hoare</span>.<br/>
<br/>
</div>
<div class="doc">
The tactic <span class="inlinecode"><span class="id" type="var">unfolds</span></span> (without any argument) unfolds the
head constant of the goal. This tactic saves the need to
name the constant explicitly.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">bexp_eval_true</span> : <span style="font-family: arial;">∀</span><span class="id" type="var">b</span> <span class="id" type="var">st</span>,<br/>
<span class="id" type="var">beval</span> <span class="id" type="var">st</span> <span class="id" type="var">b</span> = <span class="id" type="var">true</span> <span style="font-family: arial;">→</span> (<span class="id" type="var">bassn</span> <span class="id" type="var">b</span>) <span class="id" type="var">st</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">b</span> <span class="id" type="var">st</span> <span class="id" type="var">Hbe</span>. <span class="id" type="var">dup</span>.<br/>
<br/>
<span class="comment">(* The old proof: *)</span><br/>
<span class="id" type="tactic">unfold</span> <span class="id" type="var">bassn</span>. <span class="id" type="tactic">assumption</span>.<br/>
<br/>
<span class="comment">(* The new proof: *)</span><br/>
<span class="id" type="var">unfolds</span>. <span class="id" type="tactic">assumption</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
Remark: contrary to the tactic <span class="inlinecode"><span class="id" type="var">hnf</span></span>, which may unfold several
constants, <span class="inlinecode"><span class="id" type="var">unfolds</span></span> performs only a single step of unfolding.
<div class="paragraph"> </div>
Remark: the tactic <span class="inlinecode"><span class="id" type="var">unfolds</span></span> <span class="inlinecode"><span class="id" type="keyword">in</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> can be used to unfold the
head definition of the hypothesis <span class="inlinecode"><span class="id" type="var">H</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">End</span> <span class="id" type="var">UnfoldsExample</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab985"></a><h2 class="section">The tactics <span class="inlinecode"><span class="id" type="var">false</span></span> and <span class="inlinecode"><span class="id" type="var">tryfalse</span></span></h2>
<div class="paragraph"> </div>
The tactic <span class="inlinecode"><span class="id" type="var">false</span></span> can be used to replace any goal with <span class="inlinecode"><span class="id" type="var">False</span></span>.
In short, it is a shorthand for <span class="inlinecode"><span class="id" type="tactic">apply</span></span> <span class="inlinecode"><span class="id" type="var">ex_falso_quodlibet</span></span>.
Moreover, <span class="inlinecode"><span class="id" type="var">false</span></span> proves the goal if it contains an absurd
assumption, such as <span class="inlinecode"><span class="id" type="var">False</span></span> or <span class="inlinecode">0</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>, or if it contains
contradictory assumptions, such as <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">true</span></span> and <span class="inlinecode"><span class="id" type="var">x</span></span> <span class="inlinecode">=</span> <span class="inlinecode"><span class="id" type="var">false</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_false</span> : <br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">S</span> <span class="id" type="var">n</span> = 1 <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="id" type="tactic">intros</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">n</span>. <span class="id" type="tactic">reflexivity</span>. <span class="id" type="var">false</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
The tactic <span class="inlinecode"><span class="id" type="var">false</span></span> can be given an argument: <span class="inlinecode"><span class="id" type="var">false</span></span> <span class="inlinecode"><span class="id" type="var">H</span></span> replace
the goals with <span class="inlinecode"><span class="id" type="var">False</span></span> and then applies <span class="inlinecode"><span class="id" type="var">H</span></span>.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_false_arg</span> : <br/>
(<span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">n</span> < 0 <span style="font-family: arial;">→</span> <span class="id" type="var">False</span>) <span style="font-family: arial;">→</span> (3 < 0) <span style="font-family: arial;">→</span> 4 < 0.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">H</span> <span class="id" type="var">L</span>. <span class="id" type="var">false</span> <span class="id" type="var">H</span>. <span class="id" type="tactic">apply</span> <span class="id" type="var">L</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
The tactic <span class="inlinecode"><span class="id" type="var">tryfalse</span></span> is a shorthand for <span class="inlinecode"><span class="id" type="tactic">try</span></span> <span class="inlinecode"><span class="id" type="var">solve</span></span> <span class="inlinecode">[<span class="id" type="var">false</span>]</span>:
it tries to find a contradiction in the goal. The tactic
<span class="inlinecode"><span class="id" type="var">tryfalse</span></span> is generally called after a case analysis.
</div>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">demo_tryfalse</span> : <br/>
<span style="font-family: arial;">∀</span><span class="id" type="var">n</span>, <span class="id" type="var">S</span> <span class="id" type="var">n</span> = 1 <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="id" type="tactic">intros</span>. <span class="id" type="tactic">destruct</span> <span class="id" type="var">n</span>; <span class="id" type="var">tryfalse</span>. <span class="id" type="tactic">reflexivity</span>.<br/>
<span class="id" type="keyword">Qed</span>.<br/>
<br/>
</div>
<div class="doc">
<a name="lab986"></a><h2 class="section">The tactic <span class="inlinecode"><span class="id" type="var">gen</span></span></h2>
<div class="paragraph"> </div>
The tactic <span class="inlinecode"><span class="id" type="var">gen</span></span> is a shortand for <span class="inlinecode"><span class="id" type="tactic">generalize</span></span> <span class="inlinecode"><span class="id" type="tactic">dependent</span></span>
that accepts several arguments at once. An invokation of
this tactic takes the form <span class="inlinecode"><span class="id" type="var">gen</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>
<div class="code code-tight">
<br/>
<span class="id" type="keyword">Module</span> <span class="id" type="var">GenExample</span>.<br/>
<span class="id" type="keyword">Require</span> <span class="id" type="keyword">Import</span> <span class="id" type="var">Stlc</span>.<br/>
<span class="id" type="keyword">Import</span> <span class="id" type="var">STLC</span>.<br/>
<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">substitution_preserves_typing</span> : <span style="font-family: arial;">∀</span><span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> <span class="id" type="var">U</span> <span class="id" type="var">v</span> <span class="id" type="var">t</span> <span class="id" type="var">S</span>,<br/>
<span class="id" type="var">has_type</span> (<span class="id" type="var">extend</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> <span class="id" type="var">U</span>) <span class="id" type="var">t</span> <span class="id" type="var">S</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">has_type</span> <span class="id" type="var">empty</span> <span class="id" type="var">v</span> <span class="id" type="var">U</span> <span style="font-family: arial;">→</span><br/>
<span class="id" type="var">has_type</span> <span style="font-family: serif; font-size:85%;">Γ</span> ([<span class="id" type="var">x</span>:=<span class="id" type="var">v</span>]<span class="id" type="var">t</span>) <span class="id" type="var">S</span>.<br/>
<span class="id" type="keyword">Proof</span>.<br/>
<span class="id" type="var">dup</span>.<br/>
<br/>
<span class="comment">(* The old proof: *)</span><br/>
<span class="id" type="tactic">intros</span> <span style="font-family: serif; font-size:85%;">Γ</span> <span class="id" type="var">x</span> <span class="id" type="var">U</span> <span class="id" type="var">v</span> <span class="id" type="var">t</span> <span class="id" type="var">S</span> <span class="id" type="var">Htypt</span> <span class="id" type="var">Htypv</span>.<br/>
<span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span class="id" type="var">S</span>. <span class="id" type="tactic">generalize</span> <span class="id" type="tactic">dependent</span> <span style="font-family: serif; font-size:85%;">Γ</span>.<br/>
<span class="id" type="tactic">induction</span> <span class="id" type="var">t</span>; <span class="id" type="tactic">intros</span>; <span class="id" type="tactic">simpl</span>.<br/>
<span class="id" type="var">admit</span>. <span class="id" type="var">admit</span>. <span class="id" type="var">admit</span>. <span class="id" type="var">admit</span>. <span class="id" type="var">admit</span>. <span class="id" type="var">admit</span>.<br/>
<br/>
<span class="comment">(* The new proof: *)</span><br/>