-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTactics.v
326 lines (283 loc) · 8.58 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
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
(* Celsius project *)
(* Clément Blaudeau - Lamp@EPFL & Inria 2020-2022 *)
(* ------------------------------------------------------------------------ *)
(* Adapted from SystemFR project : https://github.com/epfl-lara/SystemFR *)
Require Export String List Psatz Coq.Program.Tactics Arith.
Import ListNotations.
Global Hint Extern 50 => lia: lia.
Global Hint Extern 50 => cbn: cbn.
Global Hint Extern 50 => cbn; intuition auto: cbn_intuition.
Ltac options :=
unfold option_map in *.
Ltac invert_constructor_equalities :=
match goal with
| H: ?F _ = ?F _ |- _ => is_constructor F; inversion H; clear H
| H: ?F _ _ = ?F _ _ |- _ => is_constructor F; inversion H; clear H
| H: ?F _ _ _ = ?F _ _ _ |- _ => is_constructor F; inversion H; clear H
| H: ?F _ _ _ _ = ?F _ _ _ _ |- _ => is_constructor F; inversion H; clear H
| H: ?F _ _ _ _ _ = ?F _ _ _ _ _ |- _ => is_constructor F; inversion H; clear H
| H: ?F _ _ _ _ _ _ = ?F _ _ _ _ _ _ |- _ => is_constructor F; inversion H; clear H
end.
Ltac destruct_exists :=
match goal with
| H: exists x, _ |- _ => let freshX := fresh x in
let matched := fresh "matched_exists" in
destruct H as [ freshX ] eqn:matched
end.
Ltac destruct_and :=
match goal with
| H: _ /\ _ |- _ => destruct H
end.
Global Hint Rewrite Bool.andb_true_iff: bools.
Global Hint Rewrite Bool.andb_false_iff: bools.
Global Hint Rewrite Bool.orb_true_iff: bools.
Global Hint Rewrite Bool.orb_false_iff: bools.
Global Hint Rewrite Bool.negb_true_iff: bools.
Global Hint Rewrite Bool.negb_false_iff: bools.
Ltac bools := autorewrite with bools in *.
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.
Ltac flatten :=
repeat subst ||
match goal with
| H : _ \/ _ |- _ => let fresh1 := fresh H in
let fresh2 := fresh H in destruct H as [fresh1 | fresh2]
| H : _ /\ _ |- _ => let fresh1 := fresh H in
let fresh2 := fresh H in destruct H as [fresh1 fresh2]
| H : exists a, _ |- _ => let fresh_a := fresh a in destruct H as [fresh_a H]
end || invert_constructor_equalities.
Ltac ground :=
repeat destruct_match || flatten.
Ltac light :=
(intros) ||
(intuition auto) ||
(congruence) ||
(subst) ||
(cbn in *) ||
(autounfold in *)
.
(** Taken from Cpdt **)
(** Succeed iff [x] is in the list [ls], represented with left-associated nested tuples. *)
Ltac inList x ls :=
match ls with
| x => idtac
| (_, x) => idtac
| (?LS, _) => inList x LS
end.
(** Taken from Cpdt **)
Ltac step_inversion predicates :=
let invert H F :=
inList F predicates;
(inversion H; fail) ||
(inversion H; [ idtac ]; clear H)
in
match goal with
| [ H: ?F _ |- _ ] => invert H F
| [ H: ?F _ _ |- _ ] => invert H F
| [ H: ?F _ _ _ |- _ ] => invert H F
| [ H: ?F _ _ _ _ |- _ ] => invert H F
| [ H: ?F _ _ _ _ _ |- _ ] => invert H F
| [ H: ?F _ _ _ _ _ _ |- _ ] => invert H F
end.
Ltac containsExistential := match goal with
| [ |- ?G ] => has_evar G
end.
Ltac noExistential := tryif containsExistential then fail else idtac.
Ltac removeDuplicateProps := match goal with
| [ H1: ?P, H2: ?P |- _ ] =>
match type of P with
| Prop => idtac
end; clear H2
end.
Ltac isThere P := match goal with
| H: ?Q |- _ => unify P Q
(* | |- ?Q => unify P Q *)
end.
Ltac notThere P := tryif (isThere P) then fail else idtac.
Ltac not_var P := tryif (is_var P) then fail else idtac.
Ltac noUnify P Q := tryif (unify P Q) then fail else idtac.
Lemma strong_and:
forall (A B: Prop), A -> (A -> B) -> (exists _: A, B).
Proof.
eauto.
Qed.
Ltac step_gen := match goal with
| _ => progress light
| _ => apply strong_and
| H: exists x, _ |- _ =>
let x' := fresh x in
destruct H as [ x' ]
| [ p: ?A*?B |- _ ] => destruct p
| [ H: (_,_) = (_,_) |- _ ] => inversion H; clear H
| H: _ |- _ => injection H; clear H
| |- NoDup _ => constructor
| H: forall a, _ -> _ |- _ => pose proof (H _ eq_refl); clear H
| H: forall a b, _ -> _ |- _ => pose proof (H _ _ eq_refl); clear H
| H: forall a b c, _ -> _ |- _ => pose proof (H _ _ _ eq_refl); clear H
| H: forall a b c d, _ -> _ |- _ => pose proof (H _ _ _ _ eq_refl); clear H
| H: forall a b c d e, _ -> _ |- _ => pose proof (H _ _ _ _ _ eq_refl); clear H
| [ |- 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
| _ => removeDuplicateProps
| H := _: ?T |- _ => noUnify T string; clearbody H
| _ => noExistential; solve [ constructor ]
| _ => noExistential; solve [ constructor; constructor ]
end.
Ltac step := step_gen || step_inversion (List.Forall, List.In).
Ltac steps := repeat step.
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 destruct_eq H :=
match H with
| ?a = ?b =>
let fresh_H := fresh "Heq" in pose proof (PeanoNat.Nat.eq_dec a b) as [fresh_H | fresh_H]
end.
Ltac modus_ponens :=
repeat match goal with
| H1 : ?P -> ?Q, H2: ?P |- _ => pose proof (H1 H2) ; clear H1 end.
Ltac nat_le_trans :=
repeat match goal with
|H1: ?a <= ?b, H2: ?b <= ?c |- _ =>
match goal with
| H3 : a <= c |- _ => fail 1
| _ => assert (a <= c) by lia
end
end.
Ltac modus :=
repeat match goal with
| H: ?A -> ?B, H': ?A |- _ => specialize (H H')
end.
Ltac move_top t :=
try match goal with
| H1:t, H2:t, H3:t, H4:t, H5:t, H6:t, H7:t, H8:t, H9:t, H10:t |- _ =>
move H1 at top;
move H2 at top;
move H3 at top;
move H4 at top;
move H5 at top;
move H6 at top;
move H7 at top;
move H8 at top;
move H9 at top;
move H10 at top
| H1:t, H2:t, H3:t, H4:t, H5:t, H6:t, H7:t, H8:t, H9:t |- _ =>
move H1 at top;
move H2 at top;
move H3 at top;
move H4 at top;
move H5 at top;
move H6 at top;
move H7 at top;
move H8 at top;
move H9 at top
| H1:t, H2:t, H3:t, H4:t, H5:t, H6:t, H7:t, H8:t |- _ =>
move H1 at top;
move H2 at top;
move H3 at top;
move H4 at top;
move H5 at top;
move H6 at top;
move H7 at top;
move H8 at top
| H1:t, H2:t, H3:t, H4:t, H5:t, H6:t, H7:t |- _ =>
move H1 at top;
move H2 at top;
move H3 at top;
move H4 at top;
move H5 at top;
move H6 at top;
move H7 at top
| H1:t, H2:t, H3:t, H4:t, H5:t, H6:t |- _ =>
move H1 at top;
move H2 at top;
move H3 at top;
move H4 at top;
move H5 at top;
move H6 at top
| H1:t, H2:t, H3:t, H4:t, H5:t |- _ =>
move H1 at top;
move H2 at top;
move H3 at top;
move H4 at top;
move H5 at top
| H1:t, H2:t, H3:t, H4:t |- _ =>
move H1 at top;
move H2 at top;
move H3 at top;
move H4 at top
| H1:t, H2:t, H3:t |- _ =>
move H1 at top;
move H2 at top;
move H3 at top
| H1:t, H2:t |- _ =>
move H1 at top;
move H2 at top
| H1:t |- _ =>
move H1 at top
end.
Ltac app_eq_nil :=
repeat match goal with
| H: nil = _ ++ (_ :: _) |- _ => symmetry in H
| H: _ ++ (_ :: _) = nil |- _ =>
exfalso; apply app_eq_nil in H as [_ H];
inversion H
end.
Global Hint Extern 1 => app_eq_nil: core.
From Celsius Require Export LibTactics.
Ltac cross_rewrites :=
repeat match goal with
| H: ?A = ?B, H': ?A = ?C |- _ => rewrite H in H'; inverts H'
end.
Global Hint Extern 1 => cross_rewrites: core.
Ltac destruct_if_eqb:=
match goal with
| H : context [if Nat.eqb ?a ?b then _ else _ ] |- _ =>
let Heq := fresh "Heq" in
destruct (Nat.eqb a b) eqn:Heq;
[apply PeanoNat.Nat.eqb_eq in Heq ; rewrites Heq in * ; clear Heq | apply PeanoNat.Nat.eqb_neq in Heq ]
end.