-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tactics.v
294 lines (233 loc) · 7.15 KB
/
Tactics.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
Require Import Equations.Equations.
Require Import PeanoNat.
Require Import Coq.Strings.String.
Require Import Coq.Lists.List.
Import ListNotations.
Require Export Psatz. (* lia tactic for linear integer arithmetic *)
Hint Extern 1 => exfalso: exfalso.
Hint Extern 1 => lia: lia.
Hint Extern 1 => congruence: congruence.
(** Light tactic which is fast to execute **)
Ltac destruct_pairs :=
match goal with
| H: _ * _ |- _ => destruct H
end.
Ltac destruct_exists :=
match goal with
| H: exists x, _ |- _ => let freshX := fresh x in destruct H as [ freshX ]
end.
Ltac destruct_refinements :=
match goal with
| H: { x: _ | _ } |- _ => let freshX := fresh x in destruct H as [ freshX ]
end.
Ltac destruct_and :=
match goal with
| H: _ /\ _ |- _ => destruct H
end.
Ltac destruct_or :=
match goal with
| H: _ \/ _ |- _ => destruct H
end.
Ltac destruct_unit :=
match goal with
| u: unit |- _ => destruct u
end.
Ltac not_unify t1 t2 := tryif unify t1 t2 then fail else idtac.
Ltac invert_constructor_equalities :=
match goal with
| H: ?F _ = ?F _ |- _ => not_unify existT F; is_constructor F; inversion H; clear H
| H: ?F _ _ = ?F _ _ |- _ => not_unify existT F; is_constructor F; inversion H; clear H
| H: ?F _ _ _ = ?F _ _ _ |- _ => not_unify existT F; is_constructor F; inversion H; clear H
| H: ?F _ _ _ _ = ?F _ _ _ _ |- _ => not_unify existT F; is_constructor F; inversion H; clear H
| H: ?F _ _ _ _ _ = ?F _ _ _ _ _ |- _ => not_unify existT F; is_constructor F; inversion H; clear H
| H: ?F _ _ _ _ _ _ = ?F _ _ _ _ _ _ |- _ => not_unify existT F; is_constructor F; inversion H; clear H
end.
Ltac f_equal_constructors :=
match goal with
| |- ?F _ = ?F _ => is_constructor F; f_equal
| |- ?F _ _ = ?F _ _ => is_constructor F; f_equal
| |- ?F _ _ _ = ?F _ _ _ => is_constructor F; f_equal
| |- ?F _ _ _ _ = ?F _ _ _ _ => is_constructor F; f_equal
| |- ?F _ _ _ _ _ = ?F _ _ _ _ _ => is_constructor F; f_equal
| |- ?F _ _ _ _ _ _ = ?F _ _ _ _ _ _ => is_constructor F; f_equal
end.
Ltac light :=
(intuition auto) ||
congruence ||
subst ||
(cbn in *) ||
destruct_exists ||
destruct_pairs ||
destruct_refinements
.
Ltac lights := repeat light.
Hint Extern 1 => lights: lights.
(** Some extra tactics used in particular cases **)
Ltac inversion_solve :=
match goal with
| H: _ |- _ => solve [ inversion H; lights ]
end.
Ltac invert_hyp H :=
(inversion H; [ idtac ]; clear H) ||
(inversion H; fail).
Ltac invert_pred P :=
match goal with
| H: ?F _ |- _ => unify F P; invert_hyp H
| H: ?F _ _ |- _ => unify F P; invert_hyp H
| H: ?F _ _ _ |- _ => unify F P; invert_hyp H
| H: ?F _ _ _ _ |- _ => unify F P; invert_hyp H
| H: ?F _ _ _ _ _ |- _ => unify F P; invert_hyp H
| H: ?F _ _ _ _ _ _ |- _ => unify F P; invert_hyp H
end.
Ltac destruct_match :=
match goal with
| [ |- context[match ?t with _ => _ end]] =>
let matched := fresh "matched" in
destruct t eqn:matched
| [ H: context[match ?t with _ => _ end] |- _ ] =>
let matched := fresh "matched" in
destruct t eqn:matched
end.
(** Tactics on Coq datastructures, need to be enriched **)
Ltac strings := rewrite String.eqb_eq in * || rewrite eqb_neq in *.
Hint Extern 1 => strings: strings.
Ltac nats := rewrite Nat.eqb_eq in * || rewrite Nat.eqb_neq in *.
Hint Extern 1 => nats: nats.
Ltac bools :=
rewrite Bool.orb_false_iff in * ||
rewrite Bool.orb_true_iff in * ||
rewrite Bool.andb_false_iff in * ||
rewrite Bool.andb_true_iff in * ||
rewrite Bool.negb_true_iff in * ||
rewrite Bool.negb_false_iff in *
.
Hint Extern 1 => bools: bools.
(** Tactics used to mark contexts to avoid applying the same tactics twice **)
Ltac isThere P := match goal with
| H: ?Q |- _ => unify P Q
end.
Ltac termNotThere p :=
let P := type of p in
tryif (isThere P) then fail else idtac.
Ltac poseNew E := termNotThere E; pose proof E.
Inductive Marked {T}: T -> string -> Type :=
| Mark: forall t s, Marked t s
.
Ltac clear_marked :=
repeat match goal with
| H: Marked _ _ |- _ => clear H
end.
Notation "'internal'" := (Marked _ _) (at level 50).
(** Useful shorthands that work on any hypothesis in the the context *)
Ltac apply_any :=
match goal with
| H: _ |- _ => apply H
end.
Ltac rewrite_any :=
match goal with
| H: _ |- _ => rewrite H in *
end.
Ltac erewrite_any :=
match goal with
| H: _ |- _ => erewrite H in *
end.
Ltac rewrite_back_any :=
match goal with
| H: _ |- _ => rewrite <- H in *
end.
Ltac eapply_any :=
match goal with
| H: _ |- _ => eapply H
end.
Ltac apply_anywhere f :=
match goal with
| H: _ |- _ => apply f in H
end.
Ltac eapply_anywhere f :=
match goal with
| H: _ |- _ => eapply f in H
end.
Ltac rewrite_anywhere f :=
match goal with
| H: _ |- _ => rewrite f in H
end.
Ltac erewrite_anywhere f :=
match goal with
| H: _ |- _ => erewrite f in H
end.
Ltac instantiate_any :=
match goal with
| H1: forall x1, _ -> _, H2: _ |- _ =>
poseNew (Mark (H1,H2) "instantiation");
pose proof (H1 _ H2)
| H1: forall x1 x2, _ -> _, H2: _ |- _ =>
poseNew (Mark (H1, H2) "instantiation");
pose proof (H1 _ _ H2)
| H1: forall x1 x2 x3, _ -> _, H2: _ |- _ =>
poseNew (Mark (H1,H2) "instantiation");
pose proof (H1 _ _ _ H2)
| H1: forall x1 x2 x3 x4, _ -> _, H2: _ |- _ =>
poseNew (Mark (H1,H2) "instantiation");
pose proof (H1 _ _ _ _ H2)
| H1: forall x1 x2 x3 x4 x5, _ -> _, H2: _ |- _ =>
poseNew (Mark (H1,H2) "instantiation");
pose proof (H1 _ _ _ _ _ H2)
| H1: forall x1 x2 x3 x4 x5 x6, _ -> _, H2: _ |- _ =>
poseNew (Mark (H1,H2) "instantiation");
pose proof (H1 _ _ _ _ _ _ H2)
end.
Ltac instantiate_forall :=
match goal with
| H: forall x, _, x: _ |- _ =>
poseNew (Mark (H, x) "instantiate_forall");
pose proof (H x)
end.
Hint Extern 1 => apply_any: apply_any.
Hint Extern 1 => eapply_any: eapply_any.
Hint Extern 1 => rewrite_any: rewrite_any.
Hint Extern 1 => erewrite_any: erewrite_any.
(** Remove duplicate propositions **)
Ltac removeDuplicateProps :=
repeat match goal with
| [ H1: ?P, H2: ?P |- _ ] =>
match type of P with
| Prop => idtac
end; clear H2
end.
(** An another way of getting postcondition of function calls **)
Ltac has_proj T :=
match T with
| context[proj1_sig] => idtac
end.
Ltac no_proj T := tryif has_proj T then fail else idtac.
(* We use no_proj to first destruct innermost calls *)
Ltac destruct_proj1_sigs :=
match goal with
| |- context[proj1_sig ?T] => no_proj T; destruct T
| H: context[proj1_sig ?T] |- _ => no_proj T; destruct T
| |- context[proj1_sig ?T] => destruct T
| H: context[proj1_sig ?T] |- _ => destruct T
end.
Ltac is_cons e :=
match e with
| _ :: _ => idtac
end.
Ltac find_false :=
match goal with
| H: context[?e] |- _ =>
let T := type of e in
unify T False; destruct e
| |- context[?e] =>
let T := type of e in
unify T False; destruct e
end.
Lemma smaller_greater:
forall a b, a <= b -> b < a + 1 -> a = b.
Proof.
intros; lia.
Qed.
Ltac smaller_greater :=
match goal with
| H1: ?a <= ?b, H2: ?b < ?a + 1 |- _ =>
pose proof (smaller_greater a b H1 H2); clear H1; clear H2
end.