forked from TorosFanny/software-foundations
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LibTactics.v
4194 lines (3433 loc) · 149 KB
/
LibTactics.v
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
(** * LibTactics: A Collection of Handy General-Purpose Tactics *)
(* $Date: 2013-01-16 22:29:57 -0500 (Wed, 16 Jan 2013) $ *)
(* Chapter maintained by Arthur Chargueraud *)
(** This file contains a set of tactics that extends the set of builtin
tactics provided with the standard distribution of Coq. It intends
to overcome a number of limitations of the standard set of tactics,
and thereby to help user to write shorter and more robust scripts.
Hopefully, Coq tactics will be improved as time goes by, and this
file should ultimately be useless. In the meanwhile, serious Coq
users will probably find it very useful.
The present file contains the implementation and the detailed
documentation of those tactics. The SF reader need not read this
file; instead, he/she is encouraged to read the chapter named
UseTactics.v, which is gentle introduction to the most useful
tactics from the LibTactic library. *)
(** The main features offered are:
- More convenient syntax for naming hypotheses, with tactics for
introduction and inversion that take as input only the name of
hypotheses of type [Prop], rather than the name of all variables.
- Tactics providing true support for manipulating N-ary conjunctions,
disjunctions and existentials, hidding the fact that the underlying
implementation is based on binary predicates.
- Convenient support for automation: tactic followed with the symbol
"~" or "*" will call automation on the generated subgoals.
Symbol "~" stands for [auto] and "*" for [intuition eauto].
These bindings can be customized.
- Forward-chaining tactics are provided to instantiate lemmas
either with variable or hypotheses or a mix of both.
- A more powerful implementation of [apply] is provided (it is based
on [refine] and thus behaves better with respect to conversion).
- An improved inversion tactic which substitutes equalities on variables
generated by the standard inversion mecanism. Moreover, it supports
the elimination of dependently-typed equalities (requires axiom [K],
which is a weak form of Proof Irrelevance).
- Tactics for saving time when writing proofs, with tactics to
asserts hypotheses or sub-goals, and improved tactics for
clearing, renaming, and sorting hypotheses.
*)
(** External credits:
- thanks to Xavier Leroy for providing the idea of tactic [forward],
- thanks to Georges Gonthier for the implementation trick in [rapply],
*)
Set Implicit Arguments.
(* ********************************************************************** *)
(** * Additional notations for Coq *)
(* ---------------------------------------------------------------------- *)
(** ** N-ary Existentials *)
(** [exists T1 ... TN, P] is a shorthand for
[exists T1, ..., exists TN, P]. Note that
[Coq.Program.Syntax] already defines exists
for arity up to 4. *)
Notation "'exists' x1 ',' P" :=
(exists x1, P)
(at level 200, x1 ident,
right associativity) : type_scope.
Notation "'exists' x1 x2 ',' P" :=
(exists x1, exists x2, P)
(at level 200, x1 ident, x2 ident,
right associativity) : type_scope.
Notation "'exists' x1 x2 x3 ',' P" :=
(exists x1, exists x2, exists x3, P)
(at level 200, x1 ident, x2 ident, x3 ident,
right associativity) : type_scope.
Notation "'exists' x1 x2 x3 x4 ',' P" :=
(exists x1, exists x2, exists x3, exists x4, P)
(at level 200, x1 ident, x2 ident, x3 ident, x4 ident,
right associativity) : type_scope.
Notation "'exists' x1 x2 x3 x4 x5 ',' P" :=
(exists x1, exists x2, exists x3, exists x4, exists x5, P)
(at level 200, x1 ident, x2 ident, x3 ident, x4 ident, x5 ident,
right associativity) : type_scope.
Notation "'exists' x1 x2 x3 x4 x5 x6 ',' P" :=
(exists x1, exists x2, exists x3, exists x4, exists x5, exists x6, P)
(at level 200, x1 ident, x2 ident, x3 ident, x4 ident, x5 ident,
x6 ident,
right associativity) : type_scope.
Notation "'exists' x1 x2 x3 x4 x5 x6 x7 ',' P" :=
(exists x1, exists x2, exists x3, exists x4, exists x5, exists x6,
exists x7, P)
(at level 200, x1 ident, x2 ident, x3 ident, x4 ident, x5 ident,
x6 ident, x7 ident,
right associativity) : type_scope.
Notation "'exists' x1 x2 x3 x4 x5 x6 x7 x8 ',' P" :=
(exists x1, exists x2, exists x3, exists x4, exists x5, exists x6,
exists x7, exists x8, P)
(at level 200, x1 ident, x2 ident, x3 ident, x4 ident, x5 ident,
x6 ident, x7 ident, x8 ident,
right associativity) : type_scope.
Notation "'exists' x1 x2 x3 x4 x5 x6 x7 x8 x9 ',' P" :=
(exists x1, exists x2, exists x3, exists x4, exists x5, exists x6,
exists x7, exists x8, exists x9, P)
(at level 200, x1 ident, x2 ident, x3 ident, x4 ident, x5 ident,
x6 ident, x7 ident, x8 ident, x9 ident,
right associativity) : type_scope.
Notation "'exists' x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 ',' P" :=
(exists x1, exists x2, exists x3, exists x4, exists x5, exists x6,
exists x7, exists x8, exists x9, exists x10, P)
(at level 200, x1 ident, x2 ident, x3 ident, x4 ident, x5 ident,
x6 ident, x7 ident, x8 ident, x9 ident, x10 ident,
right associativity) : type_scope.
(* ********************************************************************** *)
(** * Tools for programming with Ltac *)
(* ---------------------------------------------------------------------- *)
(** ** Identity continuation *)
Ltac idcont tt :=
idtac.
(* ---------------------------------------------------------------------- *)
(** ** Untyped arguments for tactics *)
(** Any Coq value can be boxed into the type [Boxer]. This is
useful to use Coq computations for implementing tactics. *)
Inductive Boxer : Type :=
| boxer : forall (A:Type), A -> Boxer.
(* ---------------------------------------------------------------------- *)
(** ** Optional arguments for tactics *)
(** [ltac_no_arg] is a constant that can be used to simulate
optional arguments in tactic definitions.
Use [mytactic ltac_no_arg] on the tactic invokation,
and use [match arg with ltac_no_arg => ..] or
[match type of arg with ltac_No_arg => ..] to
test whether an argument was provided. *)
Inductive ltac_No_arg : Set :=
| ltac_no_arg : ltac_No_arg.
(* ---------------------------------------------------------------------- *)
(** ** Wildcard arguments for tactics *)
(** [ltac_wild] is a constant that can be used to simulate
wildcard arguments in tactic definitions. Notation is [__]. *)
Inductive ltac_Wild : Set :=
| ltac_wild : ltac_Wild.
Notation "'__'" := ltac_wild : ltac_scope.
(** [ltac_wilds] is another constant that is typically used to
simulate a sequence of [N] wildcards, with [N] chosen
appropriately depending on the context. Notation is [___]. *)
Inductive ltac_Wilds : Set :=
| ltac_wilds : ltac_Wilds.
Notation "'___'" := ltac_wilds : ltac_scope.
Open Scope ltac_scope.
(* ---------------------------------------------------------------------- *)
(** ** Position markers *)
(** [ltac_Mark] and [ltac_mark] are dummy definitions used as sentinel
by tactics, to mark a certain position in the context or in the goal. *)
Inductive ltac_Mark : Type :=
| ltac_mark : ltac_Mark.
(** [gen_until_mark] repeats [generalize] on hypotheses from the
context, starting from the bottom and stopping as soon as reaching
an hypothesis of type [Mark]. If fails if [Mark] does not
appear in the context. *)
Ltac gen_until_mark :=
match goal with H: ?T |- _ =>
match T with
| ltac_Mark => clear H
| _ => generalize H; clear H; gen_until_mark
end end.
(** [intro_until_mark] repeats [intro] until reaching an hypothesis of
type [Mark]. It throws away the hypothesis [Mark].
It fails if [Mark] does not appear as an hypothesis in the
goal. *)
Ltac intro_until_mark :=
match goal with
| |- (ltac_Mark -> _) => intros _
| _ => intro; intro_until_mark
end.
(* ---------------------------------------------------------------------- *)
(** ** List of arguments for tactics *)
(** A datatype of type [list Boxer] is used to manipulate list of
Coq values in ltac. Notation is [>> v1 v2 ... vN] for building
a list containing the values [v1] through [vN]. *)
Require Import List.
Notation "'>>'" :=
(@nil Boxer)
(at level 0)
: ltac_scope.
Notation "'>>' v1" :=
((boxer v1)::nil)
(at level 0, v1 at level 0)
: ltac_scope.
Notation "'>>' v1 v2" :=
((boxer v1)::(boxer v2)::nil)
(at level 0, v1 at level 0, v2 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3" :=
((boxer v1)::(boxer v2)::(boxer v3)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4 v5" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::(boxer v5)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0, v5 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4 v5 v6" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::(boxer v5)
::(boxer v6)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0, v5 at level 0, v6 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4 v5 v6 v7" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::(boxer v5)
::(boxer v6)::(boxer v7)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0, v5 at level 0, v6 at level 0, v7 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4 v5 v6 v7 v8" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::(boxer v5)
::(boxer v6)::(boxer v7)::(boxer v8)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0, v5 at level 0, v6 at level 0, v7 at level 0,
v8 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4 v5 v6 v7 v8 v9" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::(boxer v5)
::(boxer v6)::(boxer v7)::(boxer v8)::(boxer v9)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0, v5 at level 0, v6 at level 0, v7 at level 0,
v8 at level 0, v9 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4 v5 v6 v7 v8 v9 v10" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::(boxer v5)
::(boxer v6)::(boxer v7)::(boxer v8)::(boxer v9)::(boxer v10)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0, v5 at level 0, v6 at level 0, v7 at level 0,
v8 at level 0, v9 at level 0, v10 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::(boxer v5)
::(boxer v6)::(boxer v7)::(boxer v8)::(boxer v9)::(boxer v10)
::(boxer v11)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0, v5 at level 0, v6 at level 0, v7 at level 0,
v8 at level 0, v9 at level 0, v10 at level 0, v11 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::(boxer v5)
::(boxer v6)::(boxer v7)::(boxer v8)::(boxer v9)::(boxer v10)
::(boxer v11)::(boxer v12)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0, v5 at level 0, v6 at level 0, v7 at level 0,
v8 at level 0, v9 at level 0, v10 at level 0, v11 at level 0,
v12 at level 0)
: ltac_scope.
Notation "'>>' v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13" :=
((boxer v1)::(boxer v2)::(boxer v3)::(boxer v4)::(boxer v5)
::(boxer v6)::(boxer v7)::(boxer v8)::(boxer v9)::(boxer v10)
::(boxer v11)::(boxer v12)::(boxer v13)::nil)
(at level 0, v1 at level 0, v2 at level 0, v3 at level 0,
v4 at level 0, v5 at level 0, v6 at level 0, v7 at level 0,
v8 at level 0, v9 at level 0, v10 at level 0, v11 at level 0,
v12 at level 0, v13 at level 0)
: ltac_scope.
(** The tactic [list_boxer_of] inputs a term [E] and returns a term
of type "list boxer", according to the following rules:
- if [E] is already of type "list Boxer", then it returns [E];
- otherwise, it returns the list [(boxer E)::nil]. *)
Ltac list_boxer_of E :=
match type of E with
| List.list Boxer => constr:(E)
| _ => constr:((boxer E)::nil)
end.
(* ---------------------------------------------------------------------- *)
(** ** Databases of lemmas *)
(** Use the hint facility to implement a database mapping
terms to terms. To declare a new database, use a definition:
[Definition mydatabase := True.]
Then, to map [mykey] to [myvalue], write the hint:
[Hint Extern 1 (Register mydatabase mykey) => Provide myvalue.]
Finally, to query the value associated with a key, run the
tactic [ltac_database_get mydatabase mykey]. This will leave
at the head of the goal the term [myvalue]. It can then be
named and exploited using [intro]. *)
Definition ltac_database (D:Boxer) (T:Boxer) (A:Boxer) := True.
Notation "'Register' D T" := (ltac_database (boxer D) (boxer T) _)
(at level 69, D at level 0, T at level 0).
Lemma ltac_database_provide : forall (A:Boxer) (D:Boxer) (T:Boxer),
ltac_database D T A.
Proof. split. Qed.
Ltac Provide T := apply (@ltac_database_provide (boxer T)).
Ltac ltac_database_get D T :=
let A := fresh "TEMP" in evar (A:Boxer);
let H := fresh "TEMP" in
assert (H : ltac_database (boxer D) (boxer T) A);
[ subst A; auto
| subst A; match type of H with ltac_database _ _ (boxer ?L) =>
generalize L end; clear H ].
(* ---------------------------------------------------------------------- *)
(** ** On-the-fly removal of hypotheses *)
(** In a list of arguments [>> H1 H2 .. HN] passed to a tactic
such as [lets] or [applys] or [forwards] or [specializes],
the term [rm], an identity function, can be placed in front
of the name of an hypothesis to be deleted. *)
Definition rm (A:Type) (X:A) := X.
(** [rm_term E] removes one hypothesis that admits the same
type as [E]. *)
Ltac rm_term E :=
let T := type of E in
match goal with H: T |- _ => try clear H end.
(** [rm_inside E] calls [rm_term Ei] for any subterm
of the form [rm Ei] found in E *)
Ltac rm_inside E :=
let go E := rm_inside E in
match E with
| rm ?X => rm_term X
| ?X1 ?X2 =>
go X1; go X2
| ?X1 ?X2 ?X3 =>
go X1; go X2; go X3
| ?X1 ?X2 ?X3 ?X4 =>
go X1; go X2; go X3; go X4
| ?X1 ?X2 ?X3 ?X4 ?X5 =>
go X1; go X2; go X3; go X4; go X5
| ?X1 ?X2 ?X3 ?X4 ?X5 ?X6 =>
go X1; go X2; go X3; go X4; go X5; go X6
| ?X1 ?X2 ?X3 ?X4 ?X5 ?X6 ?X7 =>
go X1; go X2; go X3; go X4; go X5; go X6; go X7
| ?X1 ?X2 ?X3 ?X4 ?X5 ?X6 ?X7 ?X8 =>
go X1; go X2; go X3; go X4; go X5; go X6; go X7; go X8
| ?X1 ?X2 ?X3 ?X4 ?X5 ?X6 ?X7 ?X8 ?X9 =>
go X1; go X2; go X3; go X4; go X5; go X6; go X7; go X8; go X9
| ?X1 ?X2 ?X3 ?X4 ?X5 ?X6 ?X7 ?X8 ?X9 ?X10 =>
go X1; go X2; go X3; go X4; go X5; go X6; go X7; go X8; go X9; go X10
| _ => idtac
end.
(** For faster performance, one may deactivate [rm_inside] by
replacing the body of this definition with [idtac]. *)
Ltac fast_rm_inside E :=
rm_inside E.
(* ---------------------------------------------------------------------- *)
(** ** Numbers as arguments *)
(** When tactic takes a natural number as argument, it may be
parsed either as a natural number or as a relative number.
In order for tactics to convert their arguments into natural numbers,
we provide a conversion tactic. *)
Require BinPos Coq.ZArith.BinInt.
Definition ltac_nat_from_int (x:BinInt.Z) : nat :=
match x with
| BinInt.Z0 => 0%nat
| BinInt.Zpos p => BinPos.nat_of_P p
| BinInt.Zneg p => 0%nat
end.
Ltac nat_from_number N :=
match type of N with
| nat => constr:(N)
| BinInt.Z => let N' := constr:(ltac_nat_from_int N) in eval compute in N'
end.
(** [ltac_pattern E at K] is the same as [pattern E at K] except that
[K] is a Coq natural rather than a Ltac integer. Syntax
[ltac_pattern E as K in H] is also available. *)
Tactic Notation "ltac_pattern" constr(E) "at" constr(K) :=
match nat_from_number K with
| 1 => pattern E at 1
| 2 => pattern E at 2
| 3 => pattern E at 3
| 4 => pattern E at 4
| 5 => pattern E at 5
| 6 => pattern E at 6
| 7 => pattern E at 7
| 8 => pattern E at 8
end.
Tactic Notation "ltac_pattern" constr(E) "at" constr(K) "in" hyp(H) :=
match nat_from_number K with
| 1 => pattern E at 1 in H
| 2 => pattern E at 2 in H
| 3 => pattern E at 3 in H
| 4 => pattern E at 4 in H
| 5 => pattern E at 5 in H
| 6 => pattern E at 6 in H
| 7 => pattern E at 7 in H
| 8 => pattern E at 8 in H
end.
(* ---------------------------------------------------------------------- *)
(** ** Testing tactics *)
(** [show tac] executes a tactic [tac] that produces a result,
and then display its result. *)
Tactic Notation "show" tactic(tac) :=
let R := tac in pose R.
(** [dup N] produces [N] copies of the current goal. It is useful
for building examples on which to illustrate behaviour of tactics.
[dup] is short for [dup 2]. *)
Lemma dup_lemma : forall P, P -> P -> P.
Proof. auto. Qed.
Ltac dup_tactic N :=
match nat_from_number N with
| 0 => idtac
| S 0 => idtac
| S ?N' => apply dup_lemma; [ | dup_tactic N' ]
end.
Tactic Notation "dup" constr(N) :=
dup_tactic N.
Tactic Notation "dup" :=
dup 2.
(* ---------------------------------------------------------------------- *)
(** ** Check no evar in goal *)
Ltac check_noevar M :=
match M with M => idtac end.
Ltac check_noevar_hyp H := (* todo: imlement using check_noevar *)
let T := type of H in
match type of H with T => idtac end.
Ltac check_noevar_goal := (* todo: imlement using check_noevar *)
match goal with |- ?G => match G with G => idtac end end.
(* ---------------------------------------------------------------------- *)
(** ** Tagging of hypotheses *)
(** [get_last_hyp tt] is a function that returns the last hypothesis
at the bottom of the context. It is useful to obtain the default
name associated with the hypothesis, e.g.
[intro; let H := get_last_hyp tt in let H' := fresh "P" H in ...] *)
Ltac get_last_hyp tt :=
match goal with H: _ |- _ => constr:(H) end.
(* ---------------------------------------------------------------------- *)
(** ** Tagging of hypotheses *)
(** [ltac_tag_subst] is a specific marker for hypotheses
which is used to tag hypotheses that are equalities to
be substituted. *)
Definition ltac_tag_subst (A:Type) (x:A) := x.
(** [ltac_to_generalize] is a specific marker for hypotheses
to be generalized. *)
Definition ltac_to_generalize (A:Type) (x:A) := x.
Ltac gen_to_generalize :=
repeat match goal with
H: ltac_to_generalize _ |- _ => generalize H; clear H end.
Ltac mark_to_generalize H :=
let T := type of H in
change T with (ltac_to_generalize T) in H.
(* ---------------------------------------------------------------------- *)
(** ** Deconstructing terms *)
(** [get_head E] is a tactic that returns the head constant of the
term [E], ie, when applied to a term of the form [P x1 ... xN]
it returns [P]. If [E] is not an application, it returns [E].
Warning: the tactic seems to loop in some cases when the goal is
a product and one uses the result of this function. *)
Ltac get_head E :=
match E with
| ?P _ _ _ _ _ _ _ _ _ _ _ _ => constr:(P)
| ?P _ _ _ _ _ _ _ _ _ _ _ => constr:(P)
| ?P _ _ _ _ _ _ _ _ _ _ => constr:(P)
| ?P _ _ _ _ _ _ _ _ _ => constr:(P)
| ?P _ _ _ _ _ _ _ _ => constr:(P)
| ?P _ _ _ _ _ _ _ => constr:(P)
| ?P _ _ _ _ _ _ => constr:(P)
| ?P _ _ _ _ _ => constr:(P)
| ?P _ _ _ _ => constr:(P)
| ?P _ _ _ => constr:(P)
| ?P _ _ => constr:(P)
| ?P _ => constr:(P)
| ?P => constr:(P)
end.
(** [get_fun_arg E] is a tactic that decomposes an application
term [E], ie, when applied to a term of the form [X1 ... XN]
it returns a pair made of [X1 .. X(N-1)] and [XN]. *)
Ltac get_fun_arg E :=
match E with
| ?X1 ?X2 ?X3 ?X4 ?X5 ?X6 ?X7 ?X => constr:((X1 X2 X3 X4 X5 X6,X))
| ?X1 ?X2 ?X3 ?X4 ?X5 ?X6 ?X => constr:((X1 X2 X3 X4 X5,X))
| ?X1 ?X2 ?X3 ?X4 ?X5 ?X => constr:((X1 X2 X3 X4,X))
| ?X1 ?X2 ?X3 ?X4 ?X => constr:((X1 X2 X3,X))
| ?X1 ?X2 ?X3 ?X => constr:((X1 X2,X))
| ?X1 ?X2 ?X => constr:((X1,X))
| ?X1 ?X => constr:((X1,X))
end.
(* ---------------------------------------------------------------------- *)
(** ** Action at occurence and action not at occurence *)
(** [ltac_action_at K of E do Tac] isolates the [K]-th occurence of [E] in the
goal, setting it in the form [P E] for some named pattern [P],
then calls tactic [Tac], and finally unfolds [P]. Syntax
[ltac_action_at K of E in H do Tac] is also available. *)
Tactic Notation "ltac_action_at" constr(K) "of" constr(E) "do" tactic(Tac) :=
let p := fresh in ltac_pattern E at K;
match goal with |- ?P _ => set (p:=P) end;
Tac; unfold p; clear p.
Tactic Notation "ltac_action_at" constr(K) "of" constr(E) "in" hyp(H) "do" tactic(Tac) :=
let p := fresh in ltac_pattern E at K in H;
match type of H with ?P _ => set (p:=P) in H end;
Tac; unfold p in H; clear p.
(** [protects E do Tac] temporarily assigns a name to the expression [E]
so that the execution of tactic [Tac] will not modify [E]. This is
useful for instance to restrict the action of [simpl]. *)
Tactic Notation "protects" constr(E) "do" tactic(Tac) :=
(* let x := fresh "TEMP" in sets_eq x: E; T; subst x. *)
let x := fresh "TEMP" in let H := fresh "TEMP" in
set (X := E) in *; assert (H : X = E) by reflexivity;
clearbody X; Tac; subst x.
Tactic Notation "protects" constr(E) "do" tactic(Tac) "/" :=
protects E do Tac.
(* ---------------------------------------------------------------------- *)
(** ** An alias for [eq] *)
(** [eq'] is an alias for [eq] to be used for equalities in
inductive definitions, so that they don't get mixed with
equalities generated by [inversion]. *)
Definition eq' := @eq.
Hint Unfold eq'.
Notation "x '='' y" := (@eq' _ x y)
(at level 70, arguments at next level).
(* ********************************************************************** *)
(** * Backward and forward chaining *)
(* ---------------------------------------------------------------------- *)
(** ** Application *)
(** [rapply] is a tactic similar to [eapply] except that it is
based on the [refine] tactics, and thus is strictly more
powerful (at least in theory :). In short, it is able to perform
on-the-fly conversions when required for arguments to match,
and it is able to instantiate existentials when required. *)
Tactic Notation "rapply" constr(t) :=
first (* todo: les @ sont inutiles *)
[ eexact (@t)
| refine (@t)
| refine (@t _)
| refine (@t _ _)
| refine (@t _ _ _)
| refine (@t _ _ _ _)
| refine (@t _ _ _ _ _)
| refine (@t _ _ _ _ _ _)
| refine (@t _ _ _ _ _ _ _)
| refine (@t _ _ _ _ _ _ _ _)
| refine (@t _ _ _ _ _ _ _ _ _)
| refine (@t _ _ _ _ _ _ _ _ _ _)
| refine (@t _ _ _ _ _ _ _ _ _ _ _)
| refine (@t _ _ _ _ _ _ _ _ _ _ _ _)
| refine (@t _ _ _ _ _ _ _ _ _ _ _ _ _)
| refine (@t _ _ _ _ _ _ _ _ _ _ _ _ _ _)
| refine (@t _ _ _ _ _ _ _ _ _ _ _ _ _ _ _)
].
(** The tactics [applys_N T], where [N] is a natural number,
provides a more efficient way of using [applys T]. It avoids
trying out all possible arities, by specifying explicitely
the arity of function [T]. *)
Tactic Notation "rapply_0" constr(t) :=
refine (@t).
Tactic Notation "rapply_1" constr(t) :=
refine (@t _).
Tactic Notation "rapply_2" constr(t) :=
refine (@t _ _).
Tactic Notation "rapply_3" constr(t) :=
refine (@t _ _ _).
Tactic Notation "rapply_4" constr(t) :=
refine (@t _ _ _ _).
Tactic Notation "rapply_5" constr(t) :=
refine (@t _ _ _ _ _).
Tactic Notation "rapply_6" constr(t) :=
refine (@t _ _ _ _ _ _).
Tactic Notation "rapply_7" constr(t) :=
refine (@t _ _ _ _ _ _ _).
Tactic Notation "rapply_8" constr(t) :=
refine (@t _ _ _ _ _ _ _ _).
Tactic Notation "rapply_9" constr(t) :=
refine (@t _ _ _ _ _ _ _ _ _).
Tactic Notation "rapply_10" constr(t) :=
refine (@t _ _ _ _ _ _ _ _ _ _).
(** [lets_base H E] adds an hypothesis [H : T] to the context, where [T] is
the type of term [E]. If [H] is an introduction pattern, it will
destruct [H] according to the pattern. *)
Ltac lets_base I E := generalize E; intros I.
(** [applys_to H E] transform the type of hypothesis [H] by
replacing it by the result of the application of the term
[E] to [H]. Intuitively, it is equivalent to [lets H: (E H)]. *)
Tactic Notation "applys_to" hyp(H) constr(E) :=
let H' := fresh in rename H into H';
(first [ lets_base H (E H')
| lets_base H (E _ H')
| lets_base H (E _ _ H')
| lets_base H (E _ _ _ H')
| lets_base H (E _ _ _ _ H')
| lets_base H (E _ _ _ _ _ H')
| lets_base H (E _ _ _ _ _ _ H')
| lets_base H (E _ _ _ _ _ _ _ H')
| lets_base H (E _ _ _ _ _ _ _ _ H')
| lets_base H (E _ _ _ _ _ _ _ _ _ H') ]
); clear H'.
(** [constructors] calls [constructor] or [econstructor]. *)
Tactic Notation "constructors" :=
first [ constructor | econstructor ]; unfold eq'.
(* ---------------------------------------------------------------------- *)
(** ** Assertions *)
(** [false_goal] replaces any goal by the goal [False].
Contrary to the tactic [false] (below), it does not try to do
anything else *)
Tactic Notation "false_goal" :=
elimtype False.
(** [false_post] is the underlying tactic used to prove goals
of the form [False]. In the default implementation, it proves
the goal if the context contains [False] or an hypothesis of the
form [C x1 .. xN = D y1 .. yM], or if the [congruence] tactic
finds a proof of [x <> x] for some [x]. *)
Ltac false_post :=
solve [ assumption | discriminate | congruence ].
(** [false] replaces any goal by the goal [False], and calls [false_post] *)
Tactic Notation "false" :=
false_goal; try false_post.
(** [tryfalse] tries to solve a goal by contradiction, and leaves
the goal unchanged if it cannot solve it.
It is equivalent to [try solve \[ false \]]. *)
Tactic Notation "tryfalse" :=
try solve [ false ].
(** [tryfalse by tac /] is that same as [tryfalse] except that
it tries to solve the goal using tactic [tac] if [assumption]
and [discriminate] do not apply.
It is equivalent to [try solve \[ false; tac \]].
Example: [tryfalse by congruence/] *)
Tactic Notation "tryfalse" "by" tactic(tac) "/" :=
try solve [ false; instantiate; tac ].
(** [false T] tries [false; apply T], or otherwise adds [T] as
an assumption and calls [false]. *)
Tactic Notation "false" constr(T) "by" tactic(tac) "/" :=
false_goal; first
[ first [ apply T | eapply T | rapply T]; instantiate; tac (* todo: sapply?*)
| let H := fresh in lets_base H T;
first [ discriminate H (* optimization *)
| false; instantiate; tac ] ].
(* todo: false (>> H X1 X2)... *)
Tactic Notation "false" constr(T) :=
false T by idtac/.
(** [false_invert] proves any goal provided there is at least
one hypothesis [H] in the context that can be proved absurd
by calling [inversion H]. *)
Ltac false_invert_tactic :=
match goal with H:_ |- _ =>
solve [ inversion H
| clear H; false_invert_tactic
| fail 2 ] end.
Tactic Notation "false_invert" :=
false_invert_tactic.
(** [tryfalse_invert] tries to prove the goal using
[false] or [false_invert], and leaves the goal
unchanged if it does not succeed. *)
Tactic Notation "tryfalse_invert" :=
try solve [ false | false_invert ].
(** [asserts H: T] is another syntax for [assert (H : T)], which
also works with introduction patterns. For instance, one can write:
[asserts \[x P\] (exists n, n = 3)], or
[asserts \[H|H\] (n = 0 \/ n = 1). *)
Tactic Notation "asserts" simple_intropattern(I) ":" constr(T) :=
let H := fresh in assert (H : T);
[ | generalize H; clear H; intros I ].
(** [asserts H1 .. HN: T] is a shorthand for
[asserts \[H1 \[H2 \[.. HN\]\]\]\]: T]. *)
Tactic Notation "asserts" simple_intropattern(I1)
simple_intropattern(I2) ":" constr(T) :=
asserts [I1 I2]: T.
Tactic Notation "asserts" simple_intropattern(I1)
simple_intropattern(I2) simple_intropattern(I3) ":" constr(T) :=
asserts [I1 [I2 I3]]: T.
Tactic Notation "asserts" simple_intropattern(I1)
simple_intropattern(I2) simple_intropattern(I3)
simple_intropattern(I4) ":" constr(T) :=
asserts [I1 [I2 [I3 I4]]]: T.
Tactic Notation "asserts" simple_intropattern(I1)
simple_intropattern(I2) simple_intropattern(I3)
simple_intropattern(I4) simple_intropattern(I5) ":" constr(T) :=
asserts [I1 [I2 [I3 [I4 I5]]]]: T.
Tactic Notation "asserts" simple_intropattern(I1)
simple_intropattern(I2) simple_intropattern(I3)
simple_intropattern(I4) simple_intropattern(I5)
simple_intropattern(I6) ":" constr(T) :=
asserts [I1 [I2 [I3 [I4 [I5 I6]]]]]: T.
(** [asserts: T] is [asserts H: T] with [H] being chosen automatically. *)
Tactic Notation "asserts" ":" constr(T) :=
let H := fresh in asserts H : T.
(** [cuts H: T] is the same as [asserts H: T] except that the two subgoals
generated are swapped: the subgoal [T] comes second. Note that contrary
to [cut], it introduces the hypothesis. *)
Tactic Notation "cuts" simple_intropattern(I) ":" constr(T) :=
cut (T); [ intros I | idtac ].
(** [cuts: T] is [cuts H: T] with [H] being chosen automatically. *)
Tactic Notation "cuts" ":" constr(T) :=
let H := fresh in cuts H: T.
(** [cuts H1 .. HN: T] is a shorthand for
[cuts \[H1 \[H2 \[.. HN\]\]\]\]: T]. *)
Tactic Notation "cuts" simple_intropattern(I1)
simple_intropattern(I2) ":" constr(T) :=
cuts [I1 I2]: T.
Tactic Notation "cuts" simple_intropattern(I1)
simple_intropattern(I2) simple_intropattern(I3) ":" constr(T) :=
cuts [I1 [I2 I3]]: T.
Tactic Notation "cuts" simple_intropattern(I1)
simple_intropattern(I2) simple_intropattern(I3)
simple_intropattern(I4) ":" constr(T) :=
cuts [I1 [I2 [I3 I4]]]: T.
Tactic Notation "cuts" simple_intropattern(I1)
simple_intropattern(I2) simple_intropattern(I3)
simple_intropattern(I4) simple_intropattern(I5) ":" constr(T) :=
cuts [I1 [I2 [I3 [I4 I5]]]]: T.
Tactic Notation "cuts" simple_intropattern(I1)
simple_intropattern(I2) simple_intropattern(I3)
simple_intropattern(I4) simple_intropattern(I5)
simple_intropattern(I6) ":" constr(T) :=
cuts [I1 [I2 [I3 [I4 [I5 I6]]]]]: T.
(* ---------------------------------------------------------------------- *)
(** ** Instantiation and forward-chaining *)
(** The instantiation tactics are used to instantiate a lemma [E]
(whose type is a product) on some arguments. The type of [E] is
made of implications and universal quantifications, e.g.
[forall x, P x -> forall y z, Q x y z -> R z].
The first possibility is to provide arguments in order: first [x],
then a proof of [P x], then [y] etc... In this mode, called "Args",
all the arguments are to be provided. If a wildcard is provided
(written [__]), then an existential variable will be introduced in
place of the argument.
It often saves a lot of time to give only the dependent variables,
(here [x], [y] and [z]), and have the hypotheses generated as
subgoals. In this "Vars" mode, only variables are to be provided.
For instance, lemma [E] applied to [3] and [4] is a term
of type [forall z, Q 3 4 z -> R z], and [P 3] is a new subgoal.
It is possible to use wildcards to introduce existential variables.
However, there are situations where some of the hypotheses already
exists, and it saves time to instantiate the lemma [E] using the
hypotheses. For instance, suppose [F] is a term of type [P 2].
Then the application of [E] to [F] in this "Hyps" mode is a term of type
[forall y z, Q 2 y z -> R z]. Each wildcard use
will generate an assertion instead, for instance if [G] has type
[Q 2 3 4], then the application of [E] to a wildcard and to [G]
in mode-h is a term of type [R 4], and [P 2] is a new subgoal.
It is very convenient to give some arguments the lemma should be
instantiated on, and let the tactic find out automatically where
underscores should be insterted. Underscore arguments [__] are
interpret as follows: an underscore means that we want to skip the
argument that has the same type as the next real argument provided
(real means not an underscore). If there is no real argument after
underscore, then the underscore is used for the first possible argument.
The general syntax is [tactic (>> E1 .. EN)] where [tactic] is
the name of the tactic (possibly with some arguments) and [Ei]
are the arguments. Moreover, some tactics accept the syntax
[tactic E1 .. EN] as short for [tactic (>>Hnts E1 .. EN)] for
values of [N] up to 5.
Finally, if the argument [EN] given is a triple-underscore [___],
then it is equivalent to providing a list of wildcards, with
the appropriate number of wildcards. This means that all
the remaining arguments of the lemma will be instantiated. *)
(* Underlying implementation *)
Ltac app_assert t P cont :=
let H := fresh "TEMP" in
assert (H : P); [ | cont(t H); clear H ].
Ltac app_evar t A cont :=
let x := fresh "TEMP" in
evar (x:A);
let t' := constr:(t x) in
let t'' := (eval unfold x in t') in
subst x; cont t''.
Ltac app_arg t P v cont :=
let H := fresh "TEMP" in
assert (H : P); [ apply v | cont(t H); try clear H ].
Ltac build_app_alls t final :=
let rec go t :=
match type of t with
| ?P -> ?Q => app_assert t P go
| forall _:?A, _ => app_evar t A go
| _ => final t
end in
go t.
Ltac boxerlist_next_type vs :=
match vs with
| nil => constr:(ltac_wild)
| (boxer ltac_wild)::?vs' => boxerlist_next_type vs'
| (boxer ltac_wilds)::_ => constr:(ltac_wild)
| (@boxer ?T _)::_ => constr:(T)
end.
Ltac build_app_hnts t vs final :=
let rec go t vs :=
match vs with
| nil => first [ final t | fail 1 ]
| (boxer ltac_wilds)::_ => first [ build_app_alls t final | fail 1 ]
| (boxer ?v)::?vs' =>
let cont t' := go t' vs in
let cont' t' := go t' vs' in
let T := type of t in
let T := eval hnf in T in
match v with
| ltac_wild =>
first [ let U := boxerlist_next_type vs' in
match U with
| ltac_wild =>
match T with
| ?P -> ?Q => first [ app_assert t P cont' | fail 3 ]
| forall _:?A, _ => first [ app_evar t A cont' | fail 3 ]
end
| _ =>
match T with (* should test T for unifiability *)
| U -> ?Q => first [ app_assert t U cont' | fail 3 ]
| forall _:U, _ => first [ app_evar t U cont' | fail 3 ]
| ?P -> ?Q => first [ app_assert t P cont | fail 3 ]
| forall _:?A, _ => first [ app_evar t A cont | fail 3 ]
end
end
| fail 2 ]
| _ =>
match T with
| ?P -> ?Q => first [ app_arg t P v cont'
| app_assert t P cont
| fail 3 ]
| forall _:?A, _ => first [ cont' (t v)
| app_evar t A cont
| fail 3 ]
end
end
end in
go t vs.
Ltac build_app args final :=
first [
match args with (@boxer ?T ?t)::?vs =>
let t := constr:(t:T) in
build_app_hnts t vs final
end
| fail 1 "Instantiation fails for:" args].
Ltac unfold_head_until_product T :=
eval hnf in T.
Ltac args_unfold_head_if_not_product args :=
match args with (@boxer ?T ?t)::?vs =>
let T' := unfold_head_until_product T in
constr:((@boxer T' t)::vs)
end.
Ltac args_unfold_head_if_not_product_but_params args :=
match args with
| (boxer ?t)::(boxer ?v)::?vs =>