Debian Codegraph @

theories/Corelib/Init/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 Require Import Notations.
12 Require Import Ltac.
13 Require Import Logic.
14 Require Import Specif.
15
16 (** * Useful tactics *)
17
18 (** Ex falso quodlibet : a tactic for proving False instead of the current goal.
19 This is just a nicer name for tactics such as [cut False]. *)
20
21 Ltac exfalso := Corelib.Init.Ltac.exfalso.
22
23 (** A tactic for proof by contradiction. With contradict H,
24 - H:~A |- B gives |- A
25 - H:~A |- ~B gives H: B |- A
26 - H: A |- B gives |- ~A
27 - H: A |- ~B gives H: B |- ~A
28 - H:False leads to a resolved subgoal.
29 Moreover, negations may be in unfolded forms,
30 and A or B may live in Type *)
31
32 Ltac contradict H :=
33 let save tac H := let x:=fresh in intro x; tac H; rename x into H
34 in
35 let negpos H := case H; clear H
36 in
37 let negneg H := save negpos H
38 in
39 let pospos H :=
40 let A := type of H in (exfalso; revert H; try fold (~A))
41 in
42 let posneg H := save pospos H
43 in
44 let neg H := match goal with
45 | |- (~_) => negneg H
46 | |- (_->False) => negneg H
47 | |- _ => negpos H
48 end in
49 let pos H := match goal with
50 | |- (~_) => posneg H
51 | |- (_->False) => posneg H
52 | |- _ => pospos H
53 end in
54 match type of H with
55 | (~_) => neg H
56 | (_->False) => neg H
57 | _ => (elim H;fail) || pos H
58 end.
59
60 (* A useful complement to contradict. Here H:A while G allows concluding ~A *)
61
62 Ltac false_hyp H G :=
63 let T := type of H in absurd T; [ apply G | assumption ].
64
65 (* A case with no loss of information. *)
66
67 Ltac case_eq x := generalize (eq_refl x); pattern x at -1; case x.
68
69 (* use either discriminate or injection on a hypothesis *)
70
71 Ltac destr_eq H := discriminate H || (try (injection H as [= H])).
72
73 (* Similar variants of destruct *)
74
75 Tactic Notation "destruct_with_eqn" constr(x) :=
76 destruct x eqn:?.
77 Tactic Notation "destruct_with_eqn" ident(n) :=
78 try intros until n; destruct n eqn:?.
79 Tactic Notation "destruct_with_eqn" ":" ident(H) constr(x) :=
80 destruct x eqn:H.
81 Tactic Notation "destruct_with_eqn" ":" ident(H) ident(n) :=
82 try intros until n; destruct n eqn:H.
83
84 (** Break every hypothesis of a certain type *)
85
86 Ltac destruct_all t :=
87 match goal with
88 | x : t |- _ => destruct x; destruct_all t
89 | _ => idtac
90 end.
91
92 (* Rewriting in all hypothesis several times everywhere *)
93
94 Tactic Notation "rewrite_all" constr(eq) := repeat rewrite eq in *.
95 Tactic Notation "rewrite_all" "<-" constr(eq) := repeat rewrite <- eq in *.
96
97 (** Tactics for applying equivalences.
98
99 The following code provides tactics "apply -> t", "apply <- t",
100 "apply -> t in H" and "apply <- t in H". Here t is a term whose type
101 consists of nested dependent and nondependent products with an
102 equivalence A <-> B as the conclusion. The tactics with "->" in their
103 names apply A -> B while those with "<-" in the name apply B -> A. *)
104
105 (* The idea of the tactics is to first provide a term in the context
106 whose type is the implication (in one of the directions), and then
107 apply it. The first idea is to produce a statement "forall ..., A ->
108 B" (call this type T) and then do "assert (H : T)" for a fresh H.
109 Thus, T can be proved from the original equivalence and then used to
110 perform the application. However, currently in Ltac it is difficult
111 to produce such T from the original formula.
112
113 Therefore, we first pose the original equivalence as H. If the type of
114 H is a dependent product, we create an existential variable and apply
115 H to this variable. If the type of H has the form C -> D, then we do a
116 cut on C. Once we eliminate all products, we split (i.e., destruct)
117 the conjunction into two parts and apply the relevant one. *)
118
119 Ltac find_equiv H :=
120 let T := type of H in
121 lazymatch T with
122 | ?A -> ?B =>
123 let H1 := fresh in
124 let H2 := fresh in
125 cut A;
126 [intro H1; pose proof (H H1) as H2; clear H H1;
127 rename H2 into H; find_equiv H |
128 clear H]
129 | forall x : ?t, _ =>
130 let a := fresh "a" in
131 let H1 := fresh "H" in
132 evar (a : t); pose proof (H a) as H1; unfold a in H1;
133 clear a; clear H; rename H1 into H; find_equiv H
134 | ?A <-> ?B => idtac
135 | _ => fail "The given statement does not seem to end with an equivalence."
136 end.
137
138 Ltac bapply lemma todo :=
139 let H := fresh in
140 pose proof lemma as H;
141 find_equiv H; [todo H; clear H | .. ].
142
143 Tactic Notation "apply" "->" constr(lemma) :=
144 bapply lemma ltac:(fun H => destruct H as [H _]; apply H).
145
146 Tactic Notation "apply" "<-" constr(lemma) :=
147 bapply lemma ltac:(fun H => destruct H as [_ H]; apply H).
148
149 Tactic Notation "apply" "->" constr(lemma) "in" hyp(J) :=
150 bapply lemma ltac:(fun H => destruct H as [H _]; apply H in J).
151
152 Tactic Notation "apply" "<-" constr(lemma) "in" hyp(J) :=
153 bapply lemma ltac:(fun H => destruct H as [_ H]; apply H in J).
154
155 (** An experimental tactic simpler than auto that is useful for ending
156 proofs "in one step" *)
157
158 Ltac easy :=
159 let rec use_hyp H :=
160 match type of H with
161 | _ /\ _ => exact H || destruct_hyp H
162 | _ => try solve [inversion H]
163 end
164 with do_intro := let H := fresh in intro H; use_hyp H
165 with destruct_hyp H := case H; clear H; do_intro; do_intro in
166 let rec use_hyps :=
167 match goal with
168 | H : _ /\ _ |- _ => exact H || (destruct_hyp H; use_hyps)
169 | H : _ |- _ => solve [inversion H]
170 | _ => idtac
171 end in
172 let do_atom :=
173 solve [ trivial with eq_true | reflexivity | symmetry; trivial | contradiction ] in
174 let rec do_ccl :=
175 try do_atom;
176 repeat (do_intro; try do_atom);
177 solve [ split; do_ccl ] in
178 solve [ do_atom | use_hyps; do_ccl ] ||
179 fail "Cannot solve this goal".
180
181 Ltac Ltac.easy_forward_decl ::= easy.
182
183 Tactic Notation "now" tactic(t) := t; easy.
184
185 (** Slightly more than [easy]*)
186
187 Ltac easy' := repeat split; simpl; easy || now destruct 1.
188
189 (** A tactic to document or check what is proved at some point of a script *)
190
191 Ltac now_show c := change c.
192
193 (** Support for rewriting decidability statements *)
194
195 Set Implicit Arguments.
196
197 Lemma decide_left : forall (C:Prop) (decide:{C}+{~C}),
198 C -> forall P:{C}+{~C}->Prop, (forall H:C, P (left _ H)) -> P decide.
199 Proof.
200 intros C decide H P H0; destruct decide.
201 - apply H0.
202 - contradiction.
203 Qed.
204
205 Lemma decide_right : forall (C:Prop) (decide:{C}+{~C}),
206 ~C -> forall P:{C}+{~C}->Prop, (forall H:~C, P (right _ H)) -> P decide.
207 Proof.
208 intros C decide H P H0; destruct decide.
209 - contradiction.
210 - apply H0.
211 Qed.
212
213 Tactic Notation "decide" constr(lemma) "with" constr(H) :=
214 let try_to_merge_hyps H :=
215 try (clear H; intro H) ||
216 (let H' := fresh H "bis" in intro H'; try clear H') ||
217 (let H' := fresh in intro H'; try clear H') in
218 match type of H with
219 | ~ ?C => apply (decide_right lemma H); try_to_merge_hyps H
220 | ?C -> False => apply (decide_right lemma H); try_to_merge_hyps H
221 | _ => apply (decide_left lemma H); try_to_merge_hyps H
222 end.
223
224 (** Clear an hypothesis and its dependencies *)
225
226 Tactic Notation "clear" "dependent" hyp(h) :=
227 let rec depclear h :=
228 clear h ||
229 lazymatch goal with
230 | H : context [ h ] |- _ => depclear H; depclear h
231 | H := context [ h ] |- _ => depclear H; depclear h
232 end ||
233 fail "hypothesis to clear is used in the conclusion (maybe indirectly)"
234 in depclear h.
235
236 (** Revert an hypothesis and its dependencies :
237 this is actually generalize dependent... *)
238
239 #[deprecated(note="Use ""generalize dependent"" instead (""revert dependent"" is currently an alias)", since="8.18")]
240 Tactic Notation "revert" "dependent" hyp(h) :=
241 generalize dependent h.
242
243 (** Provide an error message for dependent induction/dependent destruction that
244 reports an import is required to use it. Importing Stdlib.Program.Equality will
245 shadow this notation with the actual tactics. *)
246
247 Tactic Notation "dependent" "induction" ident(H) :=
248 fail "To use dependent induction, first [Require Import Stdlib.Program.Equality.]".
249
250 Tactic Notation "dependent" "destruction" ident(H) :=
251 fail "To use dependent destruction, first [Require Import Stdlib.Program.Equality.]".
252
253 (** *** [inversion_sigma] *)
254 (** The built-in [inversion] will frequently leave equalities of
255 dependent pairs. When the first type in the pair is an hProp or
256 otherwise simplifies, [inversion_sigma] is useful; it will replace
257 the equality of pairs with a pair of equalities, one involving a
258 term casted along the other. This might also prove useful for
259 writing a version of [inversion] / [dependent destruction] which
260 does not lose information, i.e., does not turn a goal which is
261 provable into one which requires axiom K / UIP. *)
262
263 Ltac lookup_inversion_sigma_rect H :=
264 lazymatch type of H with
265 | ex_intro _ _ _ = ex_intro _ _ _
266 => uconstr:(eq_ex_rect_ex_intro)
267 | exist _ _ _ = exist _ _ _
268 => uconstr:(eq_sig_rect_exist)
269 | existT _ _ _ = existT _ _ _
270 => uconstr:(eq_sigT_rect_existT)
271 | _ = ex_intro _ _ _
272 => uconstr:(eq_ex_rect_ex_intro_r)
273 | _ = exist _ _ _
274 => uconstr:(eq_sig_rect_exist_r)
275 | _ = existT _ _ _
276 => uconstr:(eq_sigT_rect_existT_r)
277 | ex_intro _ _ _ = _
278 => uconstr:(eq_ex_rect_ex_intro_l)
279 | exist _ _ _ = _
280 => uconstr:(eq_sig_rect_exist_l)
281 | existT _ _ _ = _
282 => uconstr:(eq_sigT_rect_existT_l)
283 | ex_intro2 _ _ _ _ _ = ex_intro2 _ _ _ _ _
284 => uconstr:(eq_ex2_rect_ex_intro2)
285 | exist2 _ _ _ _ _ = exist2 _ _ _ _ _
286 => uconstr:(eq_sig2_rect_exist2)
287 | existT2 _ _ _ _ _ = existT2 _ _ _ _ _
288 => uconstr:(eq_sigT2_rect_existT2)
289 | _ = ex_intro2 _ _ _ _ _
290 => uconstr:(eq_ex2_rect_ex_intro2_r)
291 | _ = exist2 _ _ _ _ _
292 => uconstr:(eq_sig2_rect_exist2_r)
293 | _ = existT2 _ _ _ _ _
294 => uconstr:(eq_sigT2_rect_existT2_r)
295 | ex_intro2 _ _ _ _ _ = _
296 => uconstr:(eq_ex2_rect_ex_intro2_l)
297 | exist2 _ _ _ _ _ = _
298 => uconstr:(eq_sig2_rect_exist2_l)
299 | existT2 _ _ _ _ _ = _
300 => uconstr:(eq_sigT2_rect_existT2_l)
301 | _ = _ :> ?T
302 => let sig := uconstr:(@sig) in
303 let sig2 := uconstr:(@sig2) in
304 let sigT := uconstr:(@sigT) in
305 let sigT2 := uconstr:(@sigT2) in
306 let ex := uconstr:(@ex) in
307 let ex2 := uconstr:(@ex2) in
308 fail 0 "Type" "of" H "is" "not" "an" "equality" "of" "recognized" "Σ" "types:" "expected" "one" "of" sig sig2 sigT sigT2 ex "or" ex2 "but" "got" T
309 | _
310 => fail 0 H "is" "not" "an" "equality" "of" "Σ" "types"
311 end.
312 Ltac inversion_sigma_on_as H ip :=
313 let rect := lookup_inversion_sigma_rect H in
314 induction H as ip using rect.
315 Ltac inversion_sigma_on H := inversion_sigma_on_as H ipattern:([]).
316 Ltac inversion_sigma_step :=
317 match goal with
318 | [ H : _ |- _ ] => inversion_sigma_on H
319 end.
320 Ltac inversion_sigma := repeat inversion_sigma_step.
321
322 Tactic Notation "inversion_sigma" := inversion_sigma.
323 Tactic Notation "inversion_sigma" hyp(H) := inversion_sigma_on H.
324 Tactic Notation "inversion_sigma" hyp(H) "as" simple_intropattern(ip) := inversion_sigma_on_as H ip.
325
326 (** A version of [time] that works for constrs *)
327
328 Ltac time_constr tac :=
329 let eval_early := match goal with _ => restart_timer end in
330 let ret := tac () in
331 let eval_early := match goal with _ => finish_timing ( "Tactic evaluation" ) end in
332 ret.
333
334 (** Useful combinators *)
335
336 Ltac assert_fails tac :=
337 tryif (once tac) then gfail 0 tac "succeeds" else idtac.
338 Tactic Notation "assert_fails" tactic3(tac) :=
339 assert_fails tac.
340
341 Create HintDb rewrite discriminated.
342 #[global]
343 Hint Variables Opaque : rewrite.
344
345 (** A variant of [apply] using [refine], doing as much conversion as necessary. *)
346
347 Ltac rapply p :=
348 (** before we try to add more underscores, first ensure that adding such underscores is valid *)
349 (assert_succeeds (idtac; let __ := open_constr:(p _) in idtac);
350 rapply uconstr:(p _))
351 || refine p.