| 1 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 2 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 3 |
Require Import Crypto.Util.Tactics.GeneralizeOverHoles. |
| 4 |
|
| 5 |
Ltac guarded_specialize_term_under_binders_by' guard_tac H tac := |
| 6 |
lazymatch type of H with |
| 7 |
| forall a, _ |
| 8 |
=> match goal with |
| 9 |
| _ => let __ := lazymatch goal with _ => guard_tac H end in |
| 10 |
open_constr:(H ltac:(progress tac)) |
| 11 |
| _ => let H := open_constr:(H _) in |
| 12 |
guarded_specialize_term_under_binders_by' guard_tac H tac |
| 13 |
end |
| 14 |
end. |
| 15 |
|
| 16 |
Ltac guarded_specialize_term_under_binders_by guard_tac H tac := |
| 17 |
generalize_over_holes ltac:(fun _ => guarded_specialize_term_under_binders_by' guard_tac H tac). |
| 18 |
|
| 19 |
Ltac guarded_specialize_gen_hyp_under_binders_by all_ways guard_tac H tac := |
| 20 |
idtac; |
| 21 |
let is_transparent := match goal with |
| 22 |
| _ => let __ := (eval cbv delta [H] in H) in |
| 23 |
constr:(true) |
| 24 |
| _ => constr:(false) |
| 25 |
end in |
| 26 |
lazymatch constr:((all_ways, is_transparent)) with |
| 27 |
| (false, true) |
| 28 |
=> let H' := fresh in |
| 29 |
rename H into H'; |
| 30 |
let H'' := guarded_specialize_term_under_binders_by guard_tac H' tac in |
| 31 |
pose H'' as H; |
| 32 |
subst H' |
| 33 |
| (true, true) |
| 34 |
=> let H := guarded_specialize_term_under_binders_by guard_tac H tac in |
| 35 |
unique pose H |
| 36 |
| (false, false) |
| 37 |
=> let H := guarded_specialize_term_under_binders_by guard_tac H tac in |
| 38 |
specialize H |
| 39 |
| (true, false) |
| 40 |
=> let H := guarded_specialize_term_under_binders_by guard_tac H tac in |
| 41 |
unique pose proof H |
| 42 |
end. |
| 43 |
|
| 44 |
Ltac guarded_specialize_hyp_under_binders_by guard_tac H tac := |
| 45 |
guarded_specialize_gen_hyp_under_binders_by false guard_tac H tac. |
| 46 |
Ltac guarded_specialize_hyp_all_ways_under_binders_by guard_tac H tac := |
| 47 |
guarded_specialize_gen_hyp_under_binders_by true guard_tac H tac. |
| 48 |
|
| 49 |
Ltac guard_noop H := idtac. |
| 50 |
|
| 51 |
Ltac guard_nondep H := |
| 52 |
lazymatch type of H with |
| 53 |
| ?A -> ?B => idtac |
| 54 |
end. |
| 55 |
|
| 56 |
(** try to specialize all non-dependent hypotheses using [tac] at any point under their binders, maintaining transparency *) |
| 57 |
Ltac guarded_specialize_gen_under_binders_by' all_ways tac guard_tac := |
| 58 |
idtac; |
| 59 |
match goal with |
| 60 |
| [ H : _ |- _ ] |
| 61 |
=> guard_tac H; |
| 62 |
guarded_specialize_gen_hyp_under_binders_by all_ways guard_nondep H tac |
| 63 |
end. |
| 64 |
Ltac guarded_specialize_gen_dep_under_binders_by' all_ways tac guard_tac := |
| 65 |
idtac; |
| 66 |
match goal with |
| 67 |
| [ H : _ |- _ ] |
| 68 |
=> guard_tac H; |
| 69 |
guarded_specialize_gen_hyp_under_binders_by all_ways guard_noop H tac |
| 70 |
end. |
| 71 |
|
| 72 |
Ltac guarded_specialize_under_binders_by' tac guard_tac := |
| 73 |
guarded_specialize_gen_under_binders_by' false tac guard_tac. |
| 74 |
Ltac guarded_specialize_dep_under_binders_by' tac guard_tac := |
| 75 |
guarded_specialize_gen_dep_under_binders_by' false tac guard_tac. |
| 76 |
|
| 77 |
Ltac specialize_gen_under_binders_by' all_ways tac := guarded_specialize_gen_under_binders_by' all_ways tac ltac:(fun _ => idtac). |
| 78 |
Ltac specialize_gen_dep_under_binders_by' all_ways tac := guarded_specialize_gen_dep_under_binders_by' all_ways tac ltac:(fun _ => idtac). |
| 79 |
|
| 80 |
Ltac specialize_under_binders_by' tac := specialize_gen_under_binders_by' false tac. |
| 81 |
Ltac specialize_dep_under_binders_by' tac := specialize_gen_dep_under_binders_by' false tac. |
| 82 |
|
| 83 |
Ltac guarded_specialize_gen_under_binders_by all_ways tac guard_tac := repeat guarded_specialize_gen_under_binders_by' all_ways tac guard_tac. |
| 84 |
Ltac guarded_specialize_gen_dep_under_binders_by all_ways tac guard_tac := repeat guarded_specialize_gen_dep_under_binders_by' all_ways tac guard_tac. |
| 85 |
|
| 86 |
Ltac guarded_specialize_under_binders_by tac guard_tac := guarded_specialize_gen_under_binders_by false tac guard_tac. |
| 87 |
Ltac guarded_specialize_dep_under_binders_by tac guard_tac := guarded_specialize_gen_dep_under_binders_by false tac guard_tac. |
| 88 |
|
| 89 |
Ltac guarded_specialize_all_ways_under_binders_by tac guard_tac := guarded_specialize_gen_under_binders_by true tac guard_tac. |
| 90 |
Ltac guarded_specialize_all_ways_dep_under_binders_by tac guard_tac := guarded_specialize_gen_dep_under_binders_by true tac guard_tac. |
| 91 |
|
| 92 |
Ltac specialize_gen_under_binders_by all_ways tac := repeat specialize_gen_under_binders_by' all_ways tac. |
| 93 |
Ltac specialize_gen_dep_under_binders_by all_ways tac := repeat specialize_gen_dep_under_binders_by' all_ways tac. |
| 94 |
|
| 95 |
Ltac specialize_under_binders_by tac := specialize_gen_under_binders_by false tac. |
| 96 |
Ltac specialize_dep_under_binders_by tac := specialize_gen_dep_under_binders_by false tac. |
| 97 |
|
| 98 |
Ltac specialize_all_ways_under_binders_by tac := specialize_gen_under_binders_by true tac. |
| 99 |
Ltac specialize_all_ways_dep_under_binders_by tac := specialize_gen_dep_under_binders_by true tac. |
| 100 |
|
| 101 |
(** [specialize_under_binders_by auto] should not mean [specialize_under_binders_by ( auto |
| 102 |
with * )]!!!!!!! (see |
| 103 |
https://coq.inria.fr/bugs/show_bug.cgi?id=4966) We fix this design |
| 104 |
flaw. *) |
| 105 |
Tactic Notation "specialize_under_binders_by" tactic3(tac) := specialize_under_binders_by tac. |
| 106 |
Tactic Notation "specialize_dep_under_binders_by" tactic3(tac) := specialize_dep_under_binders_by tac. |
| 107 |
|
| 108 |
Tactic Notation "specialize_all_ways_under_binders_by" tactic3(tac) := specialize_all_ways_under_binders_by tac. |
| 109 |
Tactic Notation "specialize_all_ways_dep_under_binders_by" tactic3(tac) := specialize_all_ways_dep_under_binders_by tac. |