tactics.v 14.6 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1
(* Copyright (c) 2012-2014, Robbert Krebbers. *)
2
(* This file is distributed under the terms of the BSD license. *)
3
(** This file collects general purpose tactics that are used throughout
4
the development. *)
5
Require Export Psatz.
6 7
Require Export base.

8 9 10 11 12
(** We declare hint databases [f_equal], [congruence] and [lia] and containing
solely the tactic corresponding to its name. These hint database are useful in
to be combined in combination with other hint database. *)
Hint Extern 998 (_ = _) => f_equal : f_equal.
Hint Extern 999 => congruence : congruence.
13 14 15 16 17 18 19 20
Hint Extern 1000 => lia : lia.

(** The tactic [intuition] expands to [intuition auto with *] by default. This
is rather efficient when having big hint databases, or expensive [Hint Extern]
declarations as the above. *)
Tactic Notation "intuition" := intuition auto.

(** A slightly modified version of Ssreflect's finishing tactic [done]. It
21 22 23 24
also performs [reflexivity] and uses symmetry of negated equalities. Compared
to Ssreflect's [done], it does not compute the goal's [hnf] so as to avoid
unfolding setoid equalities. Note that this tactic performs much better than
Coq's [easy] tactic as it does not perform [inversion]. *)
25 26
Ltac done :=
  trivial; intros; solve
27 28 29 30 31 32 33 34 35
  [ repeat first
    [ solve [trivial]
    | solve [symmetry; trivial]
    | reflexivity
    | discriminate
    | contradiction
    | solve [apply not_symmetry; trivial]
    | split ]
  | match goal with H : ¬_ |- _ => solve [destruct H; trivial] end ].
36 37 38
Tactic Notation "by" tactic(tac) :=
  tac; done.

39 40
(** Whereas the [split] tactic splits any inductive with one constructor, the
tactic [split_and] only splits a conjunction. *)
41
Ltac split_and := match goal with |- _  _ => split end.
42
Ltac split_ands := repeat split_and.
43
Ltac split_ands' := repeat (hnf; split_and).
44 45 46 47

(** The tactic [case_match] destructs an arbitrary match in the conclusion or
assumptions, and generates a corresponding equality. This tactic is best used
together with the [repeat] tactical. *)
48 49 50 51 52 53
Ltac case_match :=
  match goal with
  | H : context [ match ?x with _ => _ end ] |- _ => destruct x eqn:?
  | |- context [ match ?x with _ => _ end ] => destruct x eqn:?
  end.

54 55 56 57
(** The tactic [unless T by tac_fail] succeeds if [T] is not provable by
the tactic [tac_fail]. *)
Tactic Notation "unless" constr(T) "by" tactic3(tac_fail) :=
  first [assert T by tac_fail; fail 1 | idtac].
58 59 60 61 62 63

(** The tactic [repeat_on_hyps tac] repeatedly applies [tac] in unspecified
order on all hypotheses until it cannot be applied to any hypothesis anymore. *)
Tactic Notation "repeat_on_hyps" tactic3(tac) :=
  repeat match goal with H : _ |- _ => progress tac H end.

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
(** The tactic [clear dependent H1 ... Hn] clears the hypotheses [Hi] and
their dependencies. *)
Tactic Notation "clear" "dependent" hyp(H1) hyp(H2) :=
  clear dependent H1; clear dependent H2.
Tactic Notation "clear" "dependent" hyp(H1) hyp(H2) hyp(H3) :=
  clear dependent H1 H2; clear dependent H3.
Tactic Notation "clear" "dependent" hyp(H1) hyp(H2) hyp(H3) hyp(H4) :=
  clear dependent H1 H2 H3; clear dependent H4.
Tactic Notation "clear" "dependent" hyp(H1) hyp(H2) hyp(H3) hyp(H4)
  hyp(H5) := clear dependent H1 H2 H3 H4; clear dependent H5.
Tactic Notation "clear" "dependent" hyp(H1) hyp(H2) hyp(H3) hyp(H4) hyp(H5)
  hyp (H6) := clear dependent H1 H2 H3 H4 H5; clear dependent H6.
Tactic Notation "clear" "dependent" hyp(H1) hyp(H2) hyp(H3) hyp(H4) hyp(H5)
  hyp (H6) hyp(H7) := clear dependent H1 H2 H3 H4 H5 H6; clear dependent H7.
Tactic Notation "clear" "dependent" hyp(H1) hyp(H2) hyp(H3) hyp(H4) hyp(H5)
  hyp (H6) hyp(H7) hyp(H8) :=
  clear dependent H1 H2 H3 H4 H5 H6 H7; clear dependent H8.
Tactic Notation "clear" "dependent" hyp(H1) hyp(H2) hyp(H3) hyp(H4) hyp(H5)
  hyp (H6) hyp(H7) hyp(H8) hyp(H9) :=
  clear dependent H1 H2 H3 H4 H5 H6 H7 H8; clear dependent H9.
Tactic Notation "clear" "dependent" hyp(H1) hyp(H2) hyp(H3) hyp(H4) hyp(H5)
  hyp (H6) hyp(H7) hyp(H8) hyp(H9) hyp(H10) :=
  clear dependent H1 H2 H3 H4 H5 H6 H7 H8 H9; clear dependent H10.

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
(** The tactic [first_of t ft ot] calls [t] and then calls [ft] on the first
subgoal generated by [t], and [ot] on the other subgoals. *)
Ltac first_of t ft ot :=
     solve [t]
  || (t; [ ft ])
  || (t; [ ft | ot ])
  || (t; [ ft | ot | ot ])
  || (t; [ ft | ot | ot | ot ])
  || (t; [ ft | ot | ot | ot | ot ])
  || (t; [ ft | ot | ot | ot | ot | ot ])
  || (t; [ ft | ot | ot | ot | ot | ot | ot ])
  || (t; [ ft | ot | ot | ot | ot | ot | ot | ot ])
  || (t; [ ft | ot | ot | ot | ot | ot | ot | ot | ot ])
  || (t; [ ft | ot | ot | ot | ot | ot | ot | ot | ot | ot ])
  || (t; [ ft | ot | ot | ot | ot | ot | ot | ot | ot | ot | ot ])
  || (t; [ ft | ot | ot | ot | ot | ot | ot | ot | ot | ot | ot | ot ])
  || (t; [ ft | ot | ot | ot | ot | ot | ot | ot | ot | ot | ot | ot | ot ]).

Ltac last_of t ot lt :=
     solve [t]
  || (t; [ lt ])
  || (t; [ ot | lt ])
  || (t; [ ot | ot | lt ])
  || (t; [ ot | ot | ot | lt ])
  || (t; [ ot | ot | ot | ot | lt ])
  || (t; [ ot | ot | ot | ot | ot | lt ])
  || (t; [ ot | ot | ot | ot | ot | ot | lt ])
  || (t; [ ot | ot | ot | ot | ot | ot | ot | lt ])
  || (t; [ ot | ot | ot | ot | ot | ot | ot | ot | lt ])
  || (t; [ ot | ot | ot | ot | ot | ot | ot | ot | ot | lt ])
  || (t; [ ot | ot | ot | ot | ot | ot | ot | ot | ot | ot | lt ])
  || (t; [ ot | ot | ot | ot | ot | ot | ot | ot | ot | ot | ot | lt ])
  || (t; [ ot | ot | ot | ot | ot | ot | ot | ot | ot | ot | ot | ot | lt ]).
121 122

(** The tactic [is_non_dependent H] determines whether the goal's conclusion or
123
hypotheses depend on [H]. *)
124 125 126 127 128 129 130
Tactic Notation "is_non_dependent" constr(H) :=
  match goal with
  | _ : context [ H ] |- _ => fail 1
  | |- context [ H ] => fail 1
  | _ => idtac
  end.

131 132
(** The tactic [var_eq x y] fails if [x] and [y] are unequal, and [var_neq]
does the converse. *)
133 134 135
Ltac var_eq x1 x2 := match x1 with x2 => idtac | _ => fail 1 end.
Ltac var_neq x1 x2 := match x1 with x2 => fail 1 | _ => idtac end.

136 137 138 139 140
(** The tactics [block_hyps] and [unblock_hyps] can be used to temporarily mark
certain hypothesis as being blocked. The tactic changes all hypothesis [H: T]
into [H: blocked T], where [blocked] is the identity function. If a hypothesis
is already blocked, it will not be blocked again. The tactic [unblock_hyps]
removes [blocked] everywhere. *)
141

142 143 144 145
Ltac block_hyp H :=
  lazymatch type of H with
  | block _ => idtac | ?T => change T with (block T) in H
  end.
146
Ltac block_hyps := repeat_on_hyps (fun H =>
147
  match type of H with block _ => idtac | ?T => change (block T) in H end).
148 149 150 151
Ltac unblock_hyps := unfold block in * |-.

(** The tactic [injection' H] is a variant of injection that introduces the
generated equalities. *)
152
Ltac injection' H := block_goal; injection H; clear H; intros; unblock_goal.
153

154 155
(** The tactic [simplify_equality] repeatedly substitutes, discriminates,
and injects equalities, and tries to contradict impossible inequalities. *)
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
Ltac fold_classes :=
  repeat match goal with
  | |- appcontext [ ?F ] =>
    progress match type of F with
    | FMap _ =>
       change F with (@fmap _ F);
       repeat change (@fmap _ (@fmap _ F)) with (@fmap _ F)
    | MBind _ =>
       change F with (@mbind _ F);
       repeat change (@mbind _ (@mbind _ F)) with (@mbind _ F)
    | OMap _ =>
       change F with (@omap _ F);
       repeat change (@omap _ (@omap _ F)) with (@omap _ F)
    | Alter _ _ _ =>
       change F with (@alter _ _ _ F);
       repeat change (@alter _ _ _ (@alter _ _ _ F)) with (@alter _ _ _ F)
    end
  end.
Ltac fold_classes_hyps :=
  repeat match goal with
  | _ : appcontext [ ?F ] |- _ =>
    progress match type of F with
    | FMap _ =>
       change F with (@fmap _ F) in *;
       repeat change (@fmap _ (@fmap _ F)) with (@fmap _ F) in *
    | MBind _ =>
       change F with (@mbind _ F) in *;
       repeat change (@mbind _ (@mbind _ F)) with (@mbind _ F) in *
    | OMap _ =>
       change F with (@omap _ F) in *;
       repeat change (@omap _ (@omap _ F)) with (@omap _ F) in *
    | Alter _ _ _ =>
       change F with (@alter _ _ _ F) in *;
       repeat change (@alter _ _ _ (@alter _ _ _ F)) with (@alter _ _ _ F) in *
    end
  end.
Tactic Notation "csimpl" "in" "*" :=
  try (progress simpl in *; fold_classes; fold_classes_hyps).
Tactic Notation "csimpl" := try (progress simpl; fold_classes).

196 197
Ltac simplify_equality := repeat
  match goal with
198 199 200 201
  | H : _  _ |- _ => by destruct H
  | H : _ = _  False |- _ => by destruct H
  | H : ?x = _ |- _ => subst x
  | H : _ = ?x |- _ => subst x
202
  | H : _ = _ |- _ => discriminate H
203
  | H : ?f _ = ?f _ |- _ => apply (injective f) in H
204
  | H : ?f _ _ = ?f _ _ |- _ => apply (injective2 f) in H; destruct H
205 206 207
    (* before [injection'] to circumvent bug #2939 in some situations *)
  | H : _ = _ |- _ => injection' H
  | H : ?x = ?x |- _ => clear H
208 209 210 211
    (* unclear how to generalize the below *)
  | H1 : ?o = Some ?x, H2 : ?o = Some ?y |- _ =>
    assert (y = x) by congruence; clear H2
  | H1 : ?o = Some ?x, H2 : ?o = None |- _ => congruence
212
  end.
213 214
Ltac simplify_equality' := repeat (progress csimpl in * || simplify_equality).
Ltac f_equal' := csimpl in *; f_equal.
215

216 217 218 219
(** Given a tactic [tac2] generating a list of terms, [iter tac1 tac2]
runs [tac x] for each element [x] until [tac x] succeeds. If it does not
suceed for any element of the generated list, the whole tactic wil fail. *)
Tactic Notation "iter" tactic(tac) tactic(l) :=
220
  let rec go l :=
221
  match l with ?x :: ?l => tac x || go l end in go l.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

(** Given H : [A_1 → ... → A_n → B] (where each [A_i] is non-dependent), the
tactic [feed tac H tac_by] creates a subgoal for each [A_i] and calls [tac p]
with the generated proof [p] of [B]. *)
Tactic Notation "feed" tactic(tac) constr(H) :=
  let rec go H :=
  let T := type of H in
  lazymatch eval hnf in T with
  | ?T1  ?T2 =>
    (* Use a separate counter for fresh names to make it more likely that
    the generated name is "fresh" with respect to those generated before
    calling the [feed] tactic. In particular, this hack makes sure that
    tactics like [let H' := fresh in feed (fun p => pose proof p as H') H] do
    not break. *)
    let HT1 := fresh "feed" in assert T1 as HT1;
      [| go (H HT1); clear HT1 ]
  | ?T1 => tac H
  end in go H.

(** The tactic [efeed tac H] is similar to [feed], but it also instantiates
dependent premises of [H] with evars. *)
243
Tactic Notation "efeed" constr(H) "using" tactic3(tac) "by" tactic3 (bytac) :=
244 245 246 247 248
  let rec go H :=
  let T := type of H in
  lazymatch eval hnf in T with
  | ?T1  ?T2 =>
    let HT1 := fresh "feed" in assert T1 as HT1;
249
      [bytac | go (H HT1); clear HT1 ]
250 251 252 253 254 255
  | ?T1  _ =>
    let e := fresh "feed" in evar (e:T1);
    let e' := eval unfold e in e in
    clear e; go (H e')
  | ?T1 => tac H
  end in go H.
256 257
Tactic Notation "efeed" constr(H) "using" tactic3(tac) :=
  efeed H using tac by idtac.
258 259 260 261 262 263 264 265 266

(** The following variants of [pose proof], [specialize], [inversion], and
[destruct], use the [feed] tactic before invoking the actual tactic. *)
Tactic Notation "feed" "pose" "proof" constr(H) "as" ident(H') :=
  feed (fun p => pose proof p as H') H.
Tactic Notation "feed" "pose" "proof" constr(H) :=
  feed (fun p => pose proof p) H.

Tactic Notation "efeed" "pose" "proof" constr(H) "as" ident(H') :=
267
  efeed H using (fun p => pose proof p as H').
268
Tactic Notation "efeed" "pose" "proof" constr(H) :=
269
  efeed H using (fun p => pose proof p).
270 271 272 273

Tactic Notation "feed" "specialize" hyp(H) :=
  feed (fun p => specialize p) H.
Tactic Notation "efeed" "specialize" hyp(H) :=
274
  efeed H using (fun p => specialize p).
275 276 277 278 279 280 281 282 283 284 285 286

Tactic Notation "feed" "inversion" constr(H) :=
  feed (fun p => let H':=fresh in pose proof p as H'; inversion H') H.
Tactic Notation "feed" "inversion" constr(H) "as" simple_intropattern(IP) :=
  feed (fun p => let H':=fresh in pose proof p as H'; inversion H' as IP) H.

Tactic Notation "feed" "destruct" constr(H) :=
  feed (fun p => let H':=fresh in pose proof p as H'; destruct H') H.
Tactic Notation "feed" "destruct" constr(H) "as" simple_intropattern(IP) :=
  feed (fun p => let H':=fresh in pose proof p as H'; destruct H' as IP) H.

(** Coq's [firstorder] tactic fails or loops on rather small goals already. In 
287 288 289 290
particular, on those generated by the tactic [unfold_elem_ofs] which is used
to solve propositions on collections. The [naive_solver] tactic implements an
ad-hoc and incomplete [firstorder]-like solver using Ltac's backtracking
mechanism. The tactic suffers from the following limitations:
291
- It might leave unresolved evars as Ltac provides no way to detect that.
292 293
- To avoid the tactic becoming too slow, we allow a universally quantified
  hypothesis to be instantiated only once during each search path.
294 295 296
- It does not perform backtracking on instantiation of universally quantified
  assumptions.

297 298 299 300
We use a counter to make the search breath first. Breath first search ensures
that a minimal number of hypotheses is instantiated, and thus reduced the
posibility that an evar remains unresolved.

301 302 303
Despite these limitations, it works much better than Coq's [firstorder] tactic
for the purposes of this development. This tactic either fails or proves the
goal. *)
304 305 306 307
Lemma forall_and_distr (A : Type) (P Q : A  Prop) :
  ( x, P x  Q x)  ( x, P x)  ( x, Q x).
Proof. firstorder. Qed.

308 309
Tactic Notation "naive_solver" tactic(tac) :=
  unfold iff, not in *;
310 311
  repeat match goal with
  | H : context [ _, _  _ ] |- _ =>
312
    repeat setoid_rewrite forall_and_distr in H; revert H
313
  end;
314
  let rec go n :=
315 316 317 318 319 320 321
  repeat match goal with
  (**i intros *)
  | |-  _, _ => intro
  (**i simplification of assumptions *)
  | H : False |- _ => destruct H
  | H : _  _ |- _ => destruct H
  | H :  _, _  |- _ => destruct H
322
  | H : ?P  ?Q, H2 : ?Q |- _ => specialize (H H2)
323
  (**i simplify and solve equalities *)
324
  | |- _ => progress simplify_equality'
325
  (**i solve the goal *)
326 327 328 329 330 331
  | |- _ =>
    solve
    [ eassumption
    | symmetry; eassumption
    | apply not_symmetry; eassumption
    | reflexivity ]
332 333 334 335 336 337
  (**i operations that generate more subgoals *)
  | |- _  _ => split
  | H : _  _ |- _ => destruct H
  (**i solve the goal using the user supplied tactic *)
  | |- _ => solve [tac]
  end;
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  (**i use recursion to enable backtracking on the following clauses. *)
  match goal with
  (**i instantiation of the conclusion *)
  | |-  x, _ => eexists; go n
  | |- _  _ => first [left; go n | right; go n]
  | _ =>
    (**i instantiations of assumptions. *)
    lazymatch n with
    | S ?n' =>
      (**i we give priority to assumptions that fit on the conclusion. *)
      match goal with 
      | H : _  _ |- _ =>
        is_non_dependent H;
        eapply H; clear H; go n'
      | H : _  _ |- _ =>
        is_non_dependent H;
        try (eapply H; fail 2);
        efeed pose proof H; clear H; go n'
      end
    end
  end
  in iter (fun n' => go n') (eval compute in (seq 0 6)).
360
Tactic Notation "naive_solver" := naive_solver eauto.