Debian Codegraph @

src/Util/ZUtil/Tactics/ReplaceNegWithPos.v

raw
1 From Coq Require Import Lia ZArith.
2 Local Open Scope Z_scope.
3
4 Module Z.
5 Lemma push_minus_add a b : -(-a + b) = a + -b. Proof. lia. Qed.
6
7 Ltac clean_neg_step _ :=
8 match goal with
9 | [ H : (-?x) < 0 |- _ ] => assert (0 < x) by lia; clear H
10 | [ H : 0 > (-?x) |- _ ] => assert (0 < x) by lia; clear H
11 | [ H : 0 <> -?x |- _ ] => assert (0 <> x) by lia; clear H
12 | [ H : -?x <> 0 |- _ ] => assert (x <> 0) by lia; clear H
13 | [ H : (-?x) <= 0 |- _ ] => assert (0 <= x) by lia; clear H
14 | [ H : 0 >= (-?x) |- _ ] => assert (0 <= x) by lia; clear H
15 | [ H : -?x <= -?y |- _ ] => rewrite <- Z.opp_le_mono in H
16 | [ |- -?x <= -?y ] => rewrite <- Z.opp_le_mono
17 | [ H : -?x < -?y |- _ ] => rewrite <- Z.opp_lt_mono in H
18 | [ |- -?x < -?y ] => rewrite <- Z.opp_lt_mono
19 | _ => progress rewrite <- Z.opp_le_mono in *
20 | [ H : context[_ * _] |- _ ]
21 => first [ rewrite !Z.mul_opp_opp in H
22 | rewrite !Z.mul_opp_l in H
23 | rewrite !Z.mul_opp_r in H ]
24 | [ |- context[_ * _] ]
25 => first [ rewrite !Z.mul_opp_opp
26 | rewrite !Z.mul_opp_l
27 | rewrite !Z.mul_opp_r ]
28 | [ H : context[- - _] |- _ ] => rewrite !Z.opp_involutive in H
29 | [ |- context[- - _] ] => rewrite !Z.opp_involutive
30 | [ H : context[-_ + -_] |- _ ] => rewrite <- !Z.opp_add_distr in H
31 | [ |- context[-_ + -_] ] => rewrite <- !Z.opp_add_distr
32 | [ H : context[-(-?a + ?b)] |- _ ] => rewrite !push_minus_add in H
33 | [ |- context[-(-?a + ?b)] ] => rewrite !push_minus_add
34 | [ H : 0 <= ?x, H' : 0 <= ?y, H'' : -?x <= ?y |- _ ] => clear H''
35 | [ H : 0 < ?x, H' : 0 <= ?y, H'' : -?x <= ?y |- _ ] => clear H''
36 | [ H : 0 <= ?x, H' : 0 < ?y, H'' : -?x <= ?y |- _ ] => clear H''
37 | [ H : 0 < ?x, H' : 0 < ?y, H'' : -?x <= ?y |- _ ] => clear H''
38 | [ H : 0 < ?x, H' : 0 <= ?y, H'' : -?x < ?y |- _ ] => clear H''
39 | [ H : 0 <= ?x, H' : 0 < ?y, H'' : -?x < ?y |- _ ] => clear H''
40 | [ H : 0 < ?x, H' : 0 < ?y, H'' : -?x < ?y |- _ ] => clear H''
41 | [ |- -_ = -_ ] => rewrite Z.opp_inj_wd
42 | [ H : -_ = -_ |- _ ] => rewrite Z.opp_inj_wd in H
43 end.
44 Ltac clean_neg := repeat clean_neg_step ().
45 Ltac replace_with_neg x :=
46 assert (x = -(-x)) by (symmetry; apply Z.opp_involutive); generalize dependent (-x);
47 let x' := fresh in
48 rename x into x'; intro x; intros; subst x';
49 clean_neg.
50 Ltac replace_all_neg_with_pos :=
51 repeat match goal with
52 | [ H : ?x < 0 |- _ ] => replace_with_neg x
53 | [ H : 0 > ?x |- _ ] => replace_with_neg x
54 | [ H : ?x <= 0 |- _ ] => replace_with_neg x
55 | [ H : 0 >= ?x |- _ ] => replace_with_neg x
56 end.
57 End Z.