tactics.v 21.4 KB
Newer Older
Robbert Krebbers's avatar
Robbert Krebbers committed
1 2 3 4
(* Copyright (c) 2012-2015, Robbert Krebbers. *)
(* This file is distributed under the terms of the BSD license. *)
(** This file collects general purpose tactics that are used throughout
the development. *)
5
From Coq Require Import Omega.
6
From Coq Require Export Lia.
7
From iris.prelude Require Export decidable.
Robbert Krebbers's avatar
Robbert Krebbers committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

Lemma f_equal_dep {A B} (f g :  x : A, B x) x : f = g  f x = g x.
Proof. intros ->; reflexivity. Qed.
Lemma f_equal_help {A B} (f g : A  B) x y : f = g  x = y  f x = g y.
Proof. intros -> ->; reflexivity. Qed.
Ltac f_equal :=
  let rec go :=
    match goal with
    | _ => reflexivity
    | _ => apply f_equal_help; [go|try reflexivity]
    | |- ?f ?x = ?g ?x => apply (f_equal_dep f g); go
    end in
  try go.

(** 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.
Hint Extern 1000 => lia : lia.
Ralf Jung's avatar
Ralf Jung committed
28
Hint Extern 1000 => omega : omega.
Robbert Krebbers's avatar
Robbert Krebbers committed
29 30
Hint Extern 1001 => progress subst : subst. (** backtracking on this one will
be very bad, so use with care! *)
Robbert Krebbers's avatar
Robbert Krebbers committed
31 32 33

(** The tactic [intuition] expands to [intuition auto with *] by default. This
is rather efficient when having big hint databases, or expensive [Hint Extern]
Robbert Krebbers's avatar
Robbert Krebbers committed
34
declarations as the ones above. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
35 36
Tactic Notation "intuition" := intuition auto.

37
(* [done] can get slow as it calls "trivial". [fast_done] can solve way less
38 39 40 41
   goals, but it will also always finish quickly.
   We do 'reflexivity' last because for goals of the form ?x = y, if
   we have x = y in the context, we will typically want to use the
   assumption and not reflexivity *)
42
Ltac fast_done :=
43 44 45 46 47
  solve
    [ eassumption
    | symmetry; eassumption
    | apply not_symmetry; eassumption
    | reflexivity ].
48 49 50
Tactic Notation "fast_by" tactic(tac) :=
  tac; fast_done.

Robbert Krebbers's avatar
Robbert Krebbers committed
51 52 53 54 55 56
(** A slightly modified version of Ssreflect's finishing tactic [done]. It
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]. *)
Ltac done :=
57
  solve
Robbert Krebbers's avatar
Robbert Krebbers committed
58
  [ repeat first
59 60
    [ fast_done
    | solve [trivial]
61 62 63
    (* All the tactics below will introduce themselves anyway, or make no sense
       for goals of product type. So this is a good place for us to do it. *)
    | progress intros
Robbert Krebbers's avatar
Robbert Krebbers committed
64
    | solve [symmetry; trivial]
65
    | solve [apply not_symmetry; trivial]
Robbert Krebbers's avatar
Robbert Krebbers committed
66 67
    | discriminate
    | contradiction
68
    | split
Robbert Krebbers's avatar
Robbert Krebbers committed
69
    | match goal with H : ¬_ |- _ => case H; clear H; fast_done end ]
70
  ].
Robbert Krebbers's avatar
Robbert Krebbers committed
71 72 73
Tactic Notation "by" tactic(tac) :=
  tac; done.

74 75 76 77
(** Aliases for trans and etrans that are easier to type *)
Tactic Notation "trans" constr(A) := transitivity A.
Tactic Notation "etrans" := etransitivity.

78 79 80 81 82 83 84 85 86 87 88
(** Tactics for splitting conjunctions:

- [split_and] : split the goal if is syntactically of the shape [_ ∧ _]
- [split_ands?] : split the goal repeatedly (perhaps zero times) while it is
  of the shape [_ ∧ _].
- [split_ands!] : works similarly, but at least one split should succeed. In
  order to do so, it will head normalize the goal first to possibly expose a
  conjunction.

Note that [split_and] differs from [split] by only splitting conjunctions. The
[split] tactic splits any inductive with one constructor. *)
89 90 91 92 93
Tactic Notation "split_and" :=
  match goal with
  | |- _  _ => split
  | |- Is_true (_ && _) => apply andb_True; split
  end.
94 95
Tactic Notation "split_and" "?" := repeat split_and.
Tactic Notation "split_and" "!" := hnf; split_and; split_and?.
Robbert Krebbers's avatar
Robbert Krebbers committed
96

97 98 99 100 101 102 103 104 105
Tactic Notation "destruct_and" "?" :=
  repeat match goal with
  | H : False |- _ => destruct H
  | H : _  _ |- _ => destruct H
  | H : Is_true (bool_decide _) |- _ => apply (bool_decide_unpack _) in H
  | H : Is_true (_ && _) |- _ => apply andb_True in H; destruct H
  end.
Tactic Notation "destruct_and" "!" := progress (destruct_and?).

Robbert Krebbers's avatar
Robbert Krebbers committed
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
(** 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. *)
Ltac case_match :=
  match goal with
  | H : context [ match ?x with _ => _ end ] |- _ => destruct x eqn:?
  | |- context [ match ?x with _ => _ end ] => destruct x eqn:?
  end.

(** 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].

(** 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.

(** 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.

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

(** The tactic [var_eq x y] fails if [x] and [y] are unequal, and [var_neq]
does the converse. *)
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.

Robbert Krebbers's avatar
Robbert Krebbers committed
163 164 165 166 167 168 169
(** Operational type class projections in recursive calls are not folded back
appropriately by [simpl]. The tactic [csimpl] uses the [fold_classes] tactics
to refold recursive calls of [fmap], [mbind], [omap] and [alter]. A
self-contained example explaining the problem can be found in the following
Coq-club message:

https://sympa.inria.fr/sympa/arc/coq-club/2012-10/msg00147.html *)
Robbert Krebbers's avatar
Robbert Krebbers committed
170 171
Ltac fold_classes :=
  repeat match goal with
172
  | |- context [ ?F ] =>
Robbert Krebbers's avatar
Robbert Krebbers committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    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 H :=
  repeat match type of H with
190
  | context [ ?F ] =>
Robbert Krebbers's avatar
Robbert Krebbers committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    progress match type of F with
    | FMap _ =>
       change F with (@fmap _ F) in H;
       repeat change (@fmap _ (@fmap _ F)) with (@fmap _ F) in H
    | MBind _ =>
       change F with (@mbind _ F) in H;
       repeat change (@mbind _ (@mbind _ F)) with (@mbind _ F) in H
    | OMap _ =>
       change F with (@omap _ F) in H;
       repeat change (@omap _ (@omap _ F)) with (@omap _ F) in H
    | Alter _ _ _ =>
       change F with (@alter _ _ _ F) in H;
       repeat change (@alter _ _ _ (@alter _ _ _ F)) with (@alter _ _ _ F) in H
    end
  end.
Tactic Notation "csimpl" "in" hyp(H) :=
  try (progress simpl in H; fold_classes_hyps H).
Tactic Notation "csimpl" := try (progress simpl; fold_classes).
Tactic Notation "csimpl" "in" "*" :=
  repeat_on_hyps (fun H => csimpl in H); csimpl.

Robbert Krebbers's avatar
Robbert Krebbers committed
212
(** The tactic [simplify_eq] repeatedly substitutes, discriminates,
213 214
and injects equalities, and tries to contradict impossible inequalities. *)
Tactic Notation "simplify_eq" := repeat
Robbert Krebbers's avatar
Robbert Krebbers committed
215
  match goal with
Robbert Krebbers's avatar
Robbert Krebbers committed
216 217
  | H : _  _ |- _ => by case H; try clear H
  | H : _ = _  False |- _ => by case H; try clear H
Robbert Krebbers's avatar
Robbert Krebbers committed
218 219 220
  | H : ?x = _ |- _ => subst x
  | H : _ = ?x |- _ => subst x
  | H : _ = _ |- _ => discriminate H
221
  | H : _  _ |- _ => apply leibniz_equiv in H
222 223
  | H : ?f _ = ?f _ |- _ => apply (inj f) in H
  | H : ?f _ _ = ?f _ _ |- _ => apply (inj2 f) in H; destruct H
Robbert Krebbers's avatar
Robbert Krebbers committed
224
    (* before [injection] to circumvent bug #2939 in some situations *)
225
  | H : ?f _ = ?f _ |- _ => progress injection H as H
Robbert Krebbers's avatar
Robbert Krebbers committed
226
    (* first hyp will be named [H], subsequent hyps will be given fresh names *)
227 228 229 230 231
  | H : ?f _ _ = ?f _ _ |- _ => progress injection H as H
  | H : ?f _ _ _ = ?f _ _ _ |- _ => progress injection H as H
  | H : ?f _ _ _ _ = ?f _ _ _ _ |- _ => progress injection H as H
  | H : ?f _ _ _ _ _ = ?f _ _ _ _ _ |- _ => progress injection H as H
  | H : ?f _ _ _ _ _ _ = ?f _ _ _ _ _ _ |- _ => progress injection H as H
Robbert Krebbers's avatar
Robbert Krebbers committed
232 233 234 235 236
  | H : ?x = ?x |- _ => clear H
    (* 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
237 238
  | H : @existT ?A _ _ _ = existT _ _ |- _ =>
     apply (Eqdep_dec.inj_pair2_eq_dec _ (decide_rel (@eq A))) in H
Robbert Krebbers's avatar
Robbert Krebbers committed
239
  end.
240 241 242
Tactic Notation "simplify_eq" "/=" :=
  repeat (progress csimpl in * || simplify_eq).
Tactic Notation "f_equal" "/=" := csimpl in *; f_equal.
Robbert Krebbers's avatar
Robbert Krebbers committed
243

Robbert Krebbers's avatar
Robbert Krebbers committed
244
Ltac setoid_subst_aux R x :=
Robbert Krebbers's avatar
Robbert Krebbers committed
245
  match goal with
Robbert Krebbers's avatar
Robbert Krebbers committed
246
  | H : R x ?y |- _ =>
Robbert Krebbers's avatar
Robbert Krebbers committed
247 248 249 250 251 252 253 254
     is_var x;
     try match y with x _ => fail 2 end;
     repeat match goal with
     | |- context [ x ] => setoid_rewrite H
     | H' : context [ x ] |- _ =>
        try match H' with H => fail 2 end;
        setoid_rewrite H in H'
     end;
255
     clear x H
Robbert Krebbers's avatar
Robbert Krebbers committed
256 257 258
  end.
Ltac setoid_subst :=
  repeat match goal with
259
  | _ => progress simplify_eq/=
Robbert Krebbers's avatar
Robbert Krebbers committed
260 261
  | H : @equiv ?A ?e ?x _ |- _ => setoid_subst_aux (@equiv A e) x
  | H : @equiv ?A ?e _ ?x |- _ => symmetry in H; setoid_subst_aux (@equiv A e) x
Robbert Krebbers's avatar
Robbert Krebbers committed
262 263
  end.

264 265
(** f_equiv works on goals of the form [f _ = f _], for any relation and any
number of arguments. It looks for an appropriate [Proper] instance, and applies
266
it. The tactic is somewhat limited, since it cannot be used to backtrack on
Ralf Jung's avatar
Ralf Jung committed
267
the Proper instances that has been found. To that end, we try to avoid the
268
trivial instance in which the resulting goals have an [eq]. More generally,
Ralf Jung's avatar
Ralf Jung committed
269
we try to "maintain" the relation of the current goal. For example,
270
when having [Proper (equiv ==> dist) f] and [Proper (dist ==> dist) f], it will
Ralf Jung's avatar
Ralf Jung committed
271
favor the second because the relation (dist) stays the same. *)
272
Ltac f_equiv :=
273 274
  match goal with
  | _ => reflexivity
275
  | |- pointwise_relation _ _ _ _ => intros ?
Robbert Krebbers's avatar
Robbert Krebbers committed
276 277
  (* We support matches on both sides, *if* they concern the same variable, or
     variables in some relation. *)
278
  | |- ?R (match ?x with _ => _ end) (match ?x with _ => _ end) =>
279
    destruct x
Robbert Krebbers's avatar
Robbert Krebbers committed
280 281
  | H : ?R ?x ?y |- ?R2 (match ?x with _ => _ end) (match ?y with _ => _ end) =>
     destruct H
282
  (* First assume that the arguments need the same relation as the result *)
283 284 285 286 287 288 289 290 291 292
  | |- ?R (?f ?x) (?f _) => apply (_ : Proper (R ==> R) f)
  (* For the case in which R is polymorphic, or an operational type class,
  like equiv. *)
  | |- (?R _) (?f ?x) (?f _) => apply (_ : Proper (R _ ==> _) f)
  | |- (?R _ _) (?f ?x) (?f _) => apply (_ : Proper (R _ _ ==> _) f)
  | |- (?R _ _ _) (?f ?x) (?f _) => apply (_ : Proper (R _ _ _ ==> _) f)
  | |- (?R _) (?f ?x ?y) (?f _ _) => apply (_ : Proper (R _ ==> R _ ==> _) f)
  | |- (?R _ _) (?f ?x ?y) (?f _ _) => apply (_ : Proper (R _ _ ==> R _ _ ==> _) f)
  | |- (?R _ _ _) (?f ?x ?y) (?f _ _) => apply (_ : Proper (R _ _ _ ==> R _ _ _ ==> _) f)
  | |- (?R _ _ _ _) (?f ?x ?y) (?f _ _) => apply (_ : Proper (R _ _ _ _ ==> R _ _ _ _ ==> _) f)
293 294 295 296 297 298
  (* Next, try to infer the relation. Unfortunately, there is an instance
     of Proper for (eq ==> _), which will always be matched. *)
  (* TODO: Can we exclude that instance? *)
  (* TODO: If some of the arguments are the same, we could also
     query for "pointwise_relation"'s. But that leads to a combinatorial
     explosion about which arguments are and which are not the same. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
299 300
  | |- ?R (?f ?x) (?f _) => apply (_ : Proper (_ ==> R) f)
  | |- ?R (?f ?x ?y) (?f _ _) => apply (_ : Proper (_ ==> _ ==> R) f)
301 302
   (* In case the function symbol differs, but the arguments are the same,
      maybe we have a pointwise_relation in our context. *)
Robbert Krebbers's avatar
Robbert Krebbers committed
303
  | H : pointwise_relation _ ?R ?f ?g |- ?R (?f ?x) (?g ?x) => apply H
304
  end;
305
  try reflexivity.
306

307 308 309
(* The tactic [preprocess_solve_proper] unfolds the first head symbol, so that
we proceed by repeatedly using [f_equiv]. *)
Ltac preprocess_solve_proper :=
310 311 312
  (* Introduce everything *)
  intros;
  repeat lazymatch goal with
313 314
  | |- Proper _ _ => intros ???
  | |- (_ ==> _)%signature _ _ => intros ???
315
  | |- pointwise_relation _ _ _ _ => intros ?
Robbert Krebbers's avatar
Robbert Krebbers committed
316 317
  | |- ?R ?f _ => try let f' := constr:(λ x, f x) in intros ?
  end; simpl;
318 319
  (* Unfold the head symbol, which is the one we are proving a new property about *)
  lazymatch goal with
320 321 322 323
  | |- ?R (?f _ _ _ _ _ _ _ _) (?f _ _ _ _ _ _ _ _) => unfold f
  | |- ?R (?f _ _ _ _ _ _ _) (?f _ _ _ _ _ _ _) => unfold f
  | |- ?R (?f _ _ _ _ _ _) (?f _ _ _ _ _ _) => unfold f
  | |- ?R (?f _ _ _ _ _) (?f _ _ _ _ _) => unfold f
324 325 326 327 328
  | |- ?R (?f _ _ _ _) (?f _ _ _ _) => unfold f
  | |- ?R (?f _ _ _) (?f _ _ _) => unfold f
  | |- ?R (?f _ _) (?f _ _) => unfold f
  | |- ?R (?f _) (?f _) => unfold f
  end;
329 330 331 332 333 334 335 336
  simplify_eq.

(** The tactic [solve_proper] solves goals of the form "Proper (R1 ==> R2)", for
any number of relations. The actual work is done by repeatedly applying
[f_equiv]. *)
Ltac solve_proper :=
  preprocess_solve_proper;
  solve [repeat (f_equiv; try eassumption)].
337

338 339 340 341 342 343 344
(** The tactic [intros_revert tac] introduces all foralls/arrows, performs tac,
and then reverts them. *)
Ltac intros_revert tac :=
  lazymatch goal with
  | |-  _, _ => let H := fresh in intro H; intros_revert tac; revert H
  | |- _ => tac
  end.
345

Robbert Krebbers's avatar
Robbert Krebbers committed
346 347 348 349 350 351 352
(** 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) :=
  let rec go l :=
  match l with ?x :: ?l => tac x || go l end in go l.

Robbert Krebbers's avatar
Robbert Krebbers committed
353
(** Given [H : A_1 → ... → A_n → B] (where each [A_i] is non-dependent), the
Robbert Krebbers's avatar
Robbert Krebbers committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
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. *)
Tactic Notation "efeed" constr(H) "using" tactic3(tac) "by" tactic3 (bytac) :=
  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;
      [bytac | go (H HT1); clear HT1 ]
  | ?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.
Tactic Notation "efeed" constr(H) "using" tactic3(tac) :=
  efeed H using tac by idtac.

(** 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') :=
  efeed H using (fun p => pose proof p as H').
Tactic Notation "efeed" "pose" "proof" constr(H) :=
  efeed H using (fun p => pose proof p).

Tactic Notation "feed" "specialize" hyp(H) :=
  feed (fun p => specialize p) H.
Tactic Notation "efeed" "specialize" hyp(H) :=
  efeed H using (fun p => specialize p).

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.

416 417 418 419 420 421 422
(** The block definitions are taken from [Coq.Program.Equality] and can be used
by tactics to separate their goal from hypotheses they generalize over. *)
Definition block {A : Type} (a : A) := a.

Ltac block_goal := match goal with [ |- ?T ] => change (block T) end.
Ltac unblock_goal := unfold block in *.

423 424 425 426 427

(** The following tactic can be used to add support for patterns to tactic notation:
It will search for the first subterm of the goal matching [pat], and then call [tac]
with that subterm. *)
Ltac find_pat pat tac :=
428 429 430 431 432
  match goal with
  |- context [?x] =>
      unify pat x with typeclass_instances;
      tryif tac x then idtac else fail 2
  end.
433

Robbert Krebbers's avatar
Robbert Krebbers committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
(** Coq's [firstorder] tactic fails or loops on rather small goals already. In 
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:
- It might leave unresolved evars as Ltac provides no way to detect that.
- To avoid the tactic becoming too slow, we allow a universally quantified
  hypothesis to be instantiated only once during each search path.
- It does not perform backtracking on instantiation of universally quantified
  assumptions.

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.

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. *)
Lemma forall_and_distr (A : Type) (P Q : A  Prop) :
  ( x, P x  Q x)  ( x, P x)  ( x, Q x).
Proof. firstorder. Qed.

456 457 458 459 460 461
(** The tactic [no_new_unsolved_evars tac] executes [tac] and fails if it
creates any new evars. This trick is by Jonathan Leivent, see:
https://coq.inria.fr/bugs/show_bug.cgi?id=3872 *)

Ltac no_new_unsolved_evars tac := exact ltac:(tac).

Robbert Krebbers's avatar
Robbert Krebbers committed
462 463 464 465 466 467 468 469 470 471 472 473
Tactic Notation "naive_solver" tactic(tac) :=
  unfold iff, not in *;
  repeat match goal with
  | H : context [ _, _  _ ] |- _ =>
    repeat setoid_rewrite forall_and_distr in H; revert H
  end;
  let rec go n :=
  repeat match goal with
  (**i intros *)
  | |-  _, _ => intro
  (**i simplification of assumptions *)
  | H : False |- _ => destruct H
474
  | H : _  _ |- _ =>
Robbert Krebbers's avatar
Robbert Krebbers committed
475
     (* Work around bug https://coq.inria.fr/bugs/show_bug.cgi?id=2901 *)
476 477 478 479 480
     let H1 := fresh in let H2 := fresh in
     destruct H as [H1 H2]; try clear H
  | H :  _, _  |- _ =>
     let x := fresh in let Hx := fresh in
     destruct H as [x Hx]; try clear H
Robbert Krebbers's avatar
Robbert Krebbers committed
481
  | H : ?P  ?Q, H2 : ?P |- _ => specialize (H H2)
482 483
  | H : Is_true (bool_decide _) |- _ => apply (bool_decide_unpack _) in H
  | H : Is_true (_ && _) |- _ => apply andb_True in H; destruct H
Robbert Krebbers's avatar
Robbert Krebbers committed
484
  (**i simplify and solve equalities *)
485
  | |- _ => progress simplify_eq/=
Robbert Krebbers's avatar
Robbert Krebbers committed
486
  (**i solve the goal *)
487
  | |- _ => fast_done
Robbert Krebbers's avatar
Robbert Krebbers committed
488 489
  (**i operations that generate more subgoals *)
  | |- _  _ => split
490 491
  | |- Is_true (bool_decide _) => apply (bool_decide_pack _)
  | |- Is_true (_ && _) => apply andb_True; split
492 493
  | H : _  _ |- _ =>
     let H1 := fresh in destruct H as [H1|H1]; try clear H
Robbert Krebbers's avatar
Robbert Krebbers committed
494 495 496 497 498 499
  (**i solve the goal using the user supplied tactic *)
  | |- _ => solve [tac]
  end;
  (**i use recursion to enable backtracking on the following clauses. *)
  match goal with
  (**i instantiation of the conclusion *)
500
  | |-  x, _ => no_new_unsolved_evars ltac:(eexists; go n)
Robbert Krebbers's avatar
Robbert Krebbers committed
501 502 503 504 505 506
  | |- _  _ => 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. *)
507
      match goal with
Robbert Krebbers's avatar
Robbert Krebbers committed
508 509
      | H : _  _ |- _ =>
        is_non_dependent H;
510 511
        no_new_unsolved_evars
          ltac:(first [eapply H | efeed pose proof H]; clear H; go n')
Robbert Krebbers's avatar
Robbert Krebbers committed
512 513 514
      end
    end
  end
515
  in iter (fun n' => go n') (eval compute in (seq 1 6)).
Robbert Krebbers's avatar
Robbert Krebbers committed
516
Tactic Notation "naive_solver" := naive_solver eauto.