| 1 |
Require Import Crypto.Util.Tactics.Contains. |
| 2 |
|
| 3 |
Ltac replace_with_rewrite_or_setoid_rewrite_in_all fwd x H := |
| 4 |
move H at top; |
| 5 |
lazymatch goal with |
| 6 |
| [ |- context[x] ] |
| 7 |
=> lazymatch fwd with |
| 8 |
| true => first [ rewrite !H | setoid_rewrite H ] |
| 9 |
| false => first [ rewrite <- !H | setoid_rewrite <- H ] |
| 10 |
end; |
| 11 |
replace_with_rewrite_or_setoid_rewrite_in_all fwd x H |
| 12 |
| [ H' : context[x] |- _ ] |
| 13 |
=> tryif constr_eq H H' |
| 14 |
then clear x H |
| 15 |
else (lazymatch fwd with |
| 16 |
| true => first [ rewrite !H in H' | setoid_rewrite H in H' ] |
| 17 |
| false => first [ rewrite <- !H in H' | setoid_rewrite <- H in H' ] |
| 18 |
end; |
| 19 |
replace_with_rewrite_or_setoid_rewrite_in_all fwd x H) |
| 20 |
end. |
| 21 |
|
| 22 |
Ltac setoid_subst'' R x := |
| 23 |
is_var x; |
| 24 |
match goal with |
| 25 |
| [ H : R x ?y |- _ ] |
| 26 |
=> free_in x y; replace_with_rewrite_or_setoid_rewrite_in_all true x H |
| 27 |
| [ H : R ?y x |- _ ] |
| 28 |
=> free_in x y; replace_with_rewrite_or_setoid_rewrite_in_all false x H |
| 29 |
| [ H : is_true (R x ?y) |- _ ] |
| 30 |
=> free_in x y; replace_with_rewrite_or_setoid_rewrite_in_all true x H |
| 31 |
| [ H : is_true (R ?y x) |- _ ] |
| 32 |
=> free_in x y; replace_with_rewrite_or_setoid_rewrite_in_all false x H |
| 33 |
| [ H : R x ?y = true |- _ ] |
| 34 |
=> free_in x y; change (is_true (R x y)) in H; replace_with_rewrite_or_setoid_rewrite_in_all true x H |
| 35 |
| [ H : R ?y x = true |- _ ] |
| 36 |
=> free_in x y; change (is_true (R y x)) in H; replace_with_rewrite_or_setoid_rewrite_in_all false x H |
| 37 |
end. |
| 38 |
|
| 39 |
Ltac setoid_subst' x := |
| 40 |
is_var x; |
| 41 |
match goal with |
| 42 |
| [ H : ?R x _ |- _ ] => setoid_subst'' R x |
| 43 |
| [ H : ?R _ x |- _ ] => setoid_subst'' R x |
| 44 |
| [ H : is_true (?R x _) |- _ ] => setoid_subst'' R x |
| 45 |
| [ H : is_true (?R _ x) |- _ ] => setoid_subst'' R x |
| 46 |
| [ H : ?R x _ = true |- _ ] => setoid_subst'' R x |
| 47 |
| [ H : ?R _ x = true |- _ ] => setoid_subst'' R x |
| 48 |
end. |
| 49 |
|
| 50 |
Ltac setoid_subst_rel' R := |
| 51 |
idtac; |
| 52 |
match goal with |
| 53 |
| [ H : R ?x _ |- _ ] => setoid_subst'' R x |
| 54 |
| [ H : R _ ?x |- _ ] => setoid_subst'' R x |
| 55 |
| [ H : is_true (R ?x _) |- _ ] => setoid_subst'' R x |
| 56 |
| [ H : is_true (R _ ?x) |- _ ] => setoid_subst'' R x |
| 57 |
| [ H : R ?x _ = true |- _ ] => setoid_subst'' R x |
| 58 |
| [ H : R _ ?x = true |- _ ] => setoid_subst'' R x |
| 59 |
end. |
| 60 |
|
| 61 |
Ltac setoid_subst_rel R := repeat setoid_subst_rel' R. |
| 62 |
|
| 63 |
Ltac setoid_subst_all := |
| 64 |
repeat match goal with |
| 65 |
| [ H : ?R ?x ?y |- _ ] => is_var x; setoid_subst'' R x |
| 66 |
| [ H : ?R ?x ?y |- _ ] => is_var y; setoid_subst'' R y |
| 67 |
| [ H : is_true (?R ?x ?y) |- _ ] => is_var x; setoid_subst'' R x |
| 68 |
| [ H : is_true (?R ?x ?y) |- _ ] => is_var y; setoid_subst'' R y |
| 69 |
| [ H : ?R ?x ?y = true |- _ ] => is_var x; setoid_subst'' R x |
| 70 |
| [ H : ?R ?x ?y = true |- _ ] => is_var y; setoid_subst'' R y |
| 71 |
end. |
| 72 |
|
| 73 |
Tactic Notation "setoid_subst" ident(x) := setoid_subst' x. |
| 74 |
Tactic Notation "setoid_subst" := setoid_subst_all. |