Debian Codegraph @

theories/Corelib/Program/Tactics.v

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (** This module implements various tactics used to simplify the goals produced by Program,
12 which are also generally useful. *)
13
14 (** Debugging tactics to show the goal during evaluation. *)
15
16 Ltac show_goal := match goal with [ |- ?T ] => idtac T end.
17
18 Ltac show_hyp id :=
19 match goal with
20 | [ H := ?b : ?T |- _ ] =>
21 match H with
22 | id => idtac id ":=" b ":" T
23 end
24 | [ H : ?T |- _ ] =>
25 match H with
26 | id => idtac id ":" T
27 end
28 end.
29
30 Ltac show_hyps :=
31 try match reverse goal with
32 | [ H : ?T |- _ ] => show_hyp H ; fail
33 end.
34
35 (** The [do] tactic but using a Rocq-side nat. *)
36
37 Ltac do_nat n tac :=
38 match n with
39 | 0 => idtac
40 | S ?n' => tac ; do_nat n' tac
41 end.
42
43 (** Do something on the last hypothesis, or fail *)
44
45 Ltac on_last_hyp tac :=
46 lazymatch goal with [ H : _ |- _ ] => tac H end.
47
48 (** Destructs one pair, without care regarding naming. *)
49
50 Ltac destruct_one_pair :=
51 match goal with
52 | [H : (_ /\ _) |- _] => destruct H
53 | [H : prod _ _ |- _] => destruct H
54 end.
55
56 (** Repeateadly destruct pairs. *)
57
58 Ltac destruct_pairs := repeat (destruct_one_pair).
59
60 (** Destruct one existential package, keeping the name of the hypothesis for the first component. *)
61
62 Ltac destruct_one_ex :=
63 let tac H := let ph := fresh "H" in (destruct H as [H ph]) in
64 let tac2 H := let ph := fresh "H" in let ph' := fresh "H" in
65 (destruct H as [H ph ph'])
66 in
67 let tacT H := let ph := fresh "X" in (destruct H as [H ph]) in
68 let tacT2 H := let ph := fresh "X" in let ph' := fresh "X" in
69 (destruct H as [H ph ph'])
70 in
71 match goal with
72 | [H : (ex _) |- _] => tac H
73 | [H : (sig ?P) |- _ ] => tac H
74 | [H : (sigT ?P) |- _ ] => tacT H
75 | [H : (ex2 _ _) |- _] => tac2 H
76 | [H : (sig2 ?P _) |- _ ] => tac2 H
77 | [H : (sigT2 ?P _) |- _ ] => tacT2 H
78 end.
79
80 (** Repeateadly destruct existentials. *)
81
82 Ltac destruct_exists := repeat (destruct_one_ex).
83
84 (** Repeateadly destruct conjunctions and existentials. *)
85
86 Ltac destruct_conjs := repeat (destruct_one_pair || destruct_one_ex).
87
88 (** Destruct an existential hypothesis [t] keeping its name for the first component
89 and using [Ht] for the second *)
90
91 Tactic Notation "destruct" "exist" ident(t) ident(Ht) := destruct t as [t Ht].
92
93 (** Destruct a disjunction keeping its name in both subgoals. *)
94
95 Tactic Notation "destruct" "or" ident(H) := destruct H as [H|H].
96
97 (** Discriminate that also work on a [x <> x] hypothesis. *)
98
99 Ltac discriminates :=
100 match goal with
101 | [ H : ?x <> ?x |- _ ] => elim H ; reflexivity
102 | _ => discriminate
103 end.
104
105 (** Revert the last hypothesis. *)
106
107 Ltac revert_last :=
108 match goal with
109 [ H : _ |- _ ] => revert H
110 end.
111
112 (** Repeatedly reverse the last hypothesis, putting everything in the goal. *)
113
114 Ltac reverse := repeat revert_last.
115
116 (** Reverse everything up to hypothesis id (not included). *)
117
118 Ltac revert_until id :=
119 on_last_hyp ltac:(fun id' =>
120 match id' with
121 | id => idtac
122 | _ => revert id' ; revert_until id
123 end).
124
125 (** Clear duplicated hypotheses *)
126
127 Ltac clear_dup :=
128 match goal with
129 | [ H : ?X |- _ ] =>
130 match goal with
131 | [ H' : ?Y |- _ ] =>
132 match H with
133 | H' => fail 2
134 | _ => unify X Y ; (clear H' || clear H)
135 end
136 end
137 end.
138
139 Ltac clear_dups := repeat clear_dup.
140
141 (** Try to clear everything except some hyp *)
142
143 Ltac clear_except hyp :=
144 repeat match goal with [ H : _ |- _ ] =>
145 match H with
146 | hyp => fail 1
147 | _ => clear H
148 end
149 end.
150
151 (** A non-failing subst that substitutes as much as possible. *)
152
153 Ltac subst_no_fail :=
154 repeat (match goal with
155 [ H : ?X = ?Y |- _ ] => subst X || subst Y
156 end).
157
158 Tactic Notation "subst" "*" := subst_no_fail.
159
160 Ltac on_application f tac T :=
161 match T with
162 | context [f ?x ?y ?z ?w ?v ?u ?a ?b ?c] => tac (f x y z w v u a b c)
163 | context [f ?x ?y ?z ?w ?v ?u ?a ?b] => tac (f x y z w v u a b)
164 | context [f ?x ?y ?z ?w ?v ?u ?a] => tac (f x y z w v u a)
165 | context [f ?x ?y ?z ?w ?v ?u] => tac (f x y z w v u)
166 | context [f ?x ?y ?z ?w ?v] => tac (f x y z w v)
167 | context [f ?x ?y ?z ?w] => tac (f x y z w)
168 | context [f ?x ?y ?z] => tac (f x y z)
169 | context [f ?x ?y] => tac (f x y)
170 | context [f ?x] => tac (f x)
171 end.
172
173 (** Tactical [on_call f tac] applies [tac] on any application of [f] in the hypothesis or goal. *)
174
175 Ltac on_call f tac :=
176 match goal with
177 | |- ?T => on_application f tac T
178 | H : ?T |- _ => on_application f tac T
179 end.
180
181 (* Destructs calls to f in hypothesis or conclusion, useful if f creates a subset object. *)
182
183 Ltac destruct_call f :=
184 let tac t := (destruct t) in on_call f tac.
185
186 Ltac destruct_calls f := repeat destruct_call f.
187
188 Ltac destruct_call_in f H :=
189 let tac t := (destruct t) in
190 let T := type of H in
191 on_application f tac T.
192
193 Ltac destruct_call_as f l :=
194 let tac t := (destruct t as l) in on_call f tac.
195
196 Ltac destruct_call_as_in f l H :=
197 let tac t := (destruct t as l) in
198 let T := type of H in
199 on_application f tac T.
200
201 Tactic Notation "destruct_call" constr(f) := destruct_call f.
202
203 (** Permit to name the results of destructing the call to [f]. *)
204
205 Tactic Notation "destruct_call" constr(f) "as" simple_intropattern(l) :=
206 destruct_call_as f l.
207
208 (** Specify the hypothesis in which the call occurs as well. *)
209
210 Tactic Notation "destruct_call" constr(f) "in" hyp(id) :=
211 destruct_call_in f id.
212
213 Tactic Notation "destruct_call" constr(f) "as" simple_intropattern(l) "in" hyp(id) :=
214 destruct_call_as_in f l id.
215
216 (** A marker for prototypes to destruct. *)
217
218 Definition fix_proto {A : Type} (a : A) := a.
219
220 Register fix_proto as program.tactic.fix_proto.
221
222 Ltac destruct_rec_calls :=
223 match goal with
224 | [ H : fix_proto _ |- _ ] => destruct_calls H ; clear H
225 end.
226
227 Ltac destruct_all_rec_calls :=
228 repeat destruct_rec_calls ; unfold fix_proto in *.
229
230 (** Try to inject any potential constructor equality hypothesis. *)
231
232 Ltac autoinjection tac :=
233 match goal with
234 | [ H : ?f ?a = ?f' ?a' |- _ ] => tac H
235 end.
236
237 Ltac inject H := progress (inversion H ; subst*; clear_dups) ; clear H.
238
239 Ltac autoinjections := repeat (clear_dups ; autoinjection ltac:(inject)).
240
241 (** Destruct an hypothesis by first copying it to avoid dependencies. *)
242
243 Ltac destruct_nondep H := let H0 := fresh "H" in assert(H0 := H); destruct H0.
244
245 (** If bang appears in the goal, it means that we have a proof of False and the goal is solved. *)
246
247 Ltac bang :=
248 match goal with
249 | |- ?x =>
250 match x with
251 | context [False_rect _ ?p] => elim p
252 end
253 end.
254
255 (** A tactic to show contradiction by first asserting an automatically provable hypothesis. *)
256 Tactic Notation "contradiction" "by" constr(t) :=
257 let H := fresh in assert t as H by auto with * ; contradiction.
258
259 (** A tactic that adds [H:=p:typeof(p)] to the context if no hypothesis of the same type appears in the goal.
260 Useful to do saturation using tactics. *)
261
262 Ltac add_hypothesis H' p :=
263 match type of p with
264 ?X =>
265 match goal with
266 | [ H : X |- _ ] => fail 1
267 | _ => set (H':=p) ; try (change p with H') ; clearbody H'
268 end
269 end.
270
271 (** A tactic to replace an hypothesis by another term. *)
272
273 Ltac replace_hyp H c :=
274 let H' := fresh "H" in
275 assert(H' := c) ; clear H ; rename H' into H.
276
277 (** A tactic to refine an hypothesis by supplying some of its arguments. *)
278
279 Ltac refine_hyp c :=
280 let tac H := replace_hyp H c in
281 match c with
282 | ?H _ => tac H
283 | ?H _ _ => tac H
284 | ?H _ _ _ => tac H
285 | ?H _ _ _ _ => tac H
286 | ?H _ _ _ _ _ => tac H
287 | ?H _ _ _ _ _ _ => tac H
288 | ?H _ _ _ _ _ _ _ => tac H
289 | ?H _ _ _ _ _ _ _ _ => tac H
290 end.
291
292 (** The default simplification tactic used by Program is defined by [program_simpl], sometimes [auto]
293 is not enough, better rebind using [Obligation Tactic := tac] in this case,
294 possibly using [program_simplify] to use standard goal-cleaning tactics. *)
295
296 Ltac program_simplify :=
297 simpl; intros ; destruct_all_rec_calls ; repeat (destruct_conjs; simpl proj1_sig in * );
298 subst*; autoinjections ; try discriminates ;
299 try (solve [ red ; intros ; destruct_conjs ; autoinjections ; discriminates ]).
300
301 (** Restrict automation to propositional obligations. *)
302
303 Ltac program_solve_wf :=
304 match goal with
305 | |- well_founded _ => auto with *
306 | |- ?T => match type of T with Prop => auto end
307 end.
308
309 Create HintDb program discriminated.
310
311 Ltac program_simpl := program_simplify ; try typeclasses eauto 10 with program ; try program_solve_wf.
312
313 #[global] Obligation Tactic := program_simpl.
314 #[export] Obligation Tactic := program_simpl.