Debian Codegraph @

src/Util/ZUtil/Quot.v

raw
1 From Coq Require Import ZArith Lia.
2 Require Import Crypto.Util.ZUtil.Hints.Core.
3 Require Import Crypto.Util.ZUtil.Sgn.
4 Require Import Crypto.Util.ZUtil.Modulo.
5 Require Import Crypto.Util.ZUtil.Div.
6 Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos.
7 Local Open Scope Z_scope.
8
9 Module Z.
10 Lemma quot_div_full a b : Z.quot a b = Z.sgn a * Z.sgn b * (Z.abs a / Z.abs b).
11 Proof.
12 destruct (Z_zerop b); [ subst | apply Z.quot_div; assumption ].
13 destruct a; simpl; reflexivity.
14 Qed.
15
16 Local Arguments Z.mul !_ !_.
17
18 Lemma quot_sgn_nonneg a b : 0 <= Z.sgn (Z.quot a b) * Z.sgn a * Z.sgn b.
19 Proof.
20 rewrite quot_div_full, !Z.sgn_mul, !Z.sgn_sgn.
21 set (d := Z.abs a / Z.abs b).
22 destruct a, b; simpl; try (subst d; simpl; lia);
23 try rewrite (Z.mul_opp_l 1);
24 do 2 try rewrite (Z.mul_opp_r _ 1);
25 rewrite ?Z.mul_1_l, ?Z.mul_1_r, ?Z.opp_involutive;
26 apply Z.div_abs_sgn_nonneg.
27 Qed.
28
29 Lemma quot_nonneg_same_sgn a b : Z.sgn a = Z.sgn b -> 0 <= Z.quot a b.
30 Proof.
31 intro H.
32 generalize (quot_sgn_nonneg a b); rewrite H.
33 rewrite <- Z.mul_assoc, <- Z.sgn_mul.
34 destruct (Z_zerop b); [ subst; destruct a; unfold Z.quot; simpl in *; congruence | ].
35 rewrite (Z.sgn_pos (_ * _)) by nia.
36 intro; apply Z.sgn_nonneg; lia.
37 Qed.
38
39 Lemma mul_quot_eq_full a m : m <> 0 -> m * (Z.quot a m) = a - a mod (Z.abs m * Z.sgn a).
40 Proof.
41 intro Hm.
42 assert (0 <> m * m) by (intro; apply Hm; nia).
43 assert (0 < m * m) by nia.
44 assert (0 <> Z.abs m) by (destruct m; simpl in *; try congruence).
45 rewrite quot_div_full.
46 rewrite <- (Z.abs_sgn m) at 1.
47 transitivity ((Z.sgn m * Z.sgn m) * Z.sgn a * (Z.abs m * (Z.abs a / Z.abs m))); [ nia | ].
48 rewrite <- Z.sgn_mul, Z.sgn_pos, Z.mul_1_l, Z.mul_div_eq_full by lia.
49 rewrite Z.mul_sub_distr_l.
50 rewrite Z.mul_comm, Z.abs_sgn.
51 destruct a; simpl Z.sgn; simpl Z.abs; autorewrite with zsimplify_const; [ reflexivity | reflexivity | ].
52 repeat match goal with |- context[-1 * ?x] => replace (-1 * x) with (-x) by lia end.
53 repeat match goal with |- context[?x * -1] => replace (x * -1) with (-x) by lia end.
54 rewrite <- Zmod_opp_opp; simpl Z.opp.
55 reflexivity.
56 Qed.
57
58 Lemma quot_sub_sgn a : Z.quot (a - Z.sgn a) a = 0.
59 Proof.
60 rewrite quot_div_full.
61 destruct (Z_zerop a); subst; [ lia | ].
62 rewrite Z.div_small; lia.
63 Qed.
64
65 Lemma quot_small_abs a b : 0 <= Z.abs a < Z.abs b -> Z.quot a b = 0.
66 Proof.
67 intros; rewrite Z.quot_small_iff by lia; lia.
68 Qed.
69
70 Lemma quot_add_sub_sgn_small a b : b <> 0 -> Z.sgn a = Z.sgn b -> Z.quot (a + b - Z.sgn b) b = Z.quot (a - Z.sgn b) b + 1.
71 Proof.
72 destruct (Z_zerop a), (Z_zerop b), (Z_lt_le_dec a 0), (Z_lt_le_dec b 0), (Z_lt_le_dec 1 (Z.abs a));
73 subst;
74 try lia;
75 rewrite !Z.quot_div_full;
76 try rewrite (Z.sgn_neg a) by lia;
77 try rewrite (Z.sgn_neg b) by lia;
78 repeat first [ reflexivity
79 | rewrite Z.sgn_neg by lia
80 | rewrite Z.sgn_pos by lia
81 | rewrite Z.abs_eq by lia
82 | rewrite Z.abs_neq by lia
83 | rewrite !Z.mul_opp_l
84 | rewrite Z.abs_opp in *
85 | rewrite Z.abs_eq in * by lia
86 | match goal with
87 | [ |- context[-1 * ?x] ]
88 => replace (-1 * x) with (-x) by lia
89 | [ |- context[?x * -1] ]
90 => replace (x * -1) with (-x) by lia
91 | [ |- context[-?x - ?y] ]
92 => replace (-x - y) with (-(x + y)) by lia
93 | [ |- context[-?x + - ?y] ]
94 => replace (-x + - y) with (-(x + y)) by lia
95 | [ |- context[(?a + ?b + ?c) / ?b] ]
96 => replace (a + b + c) with (((a + c) + b * 1)) by lia; rewrite Z.div_add' by lia
97 | [ |- context[(?a + ?b - ?c) / ?b] ]
98 => replace (a + b - c) with (((a - c) + b * 1)) by lia; rewrite Z.div_add' by lia
99 end
100 | progress intros
101 | progress Z.replace_all_neg_with_pos
102 | progress autorewrite with zsimplify ].
103 Qed.
104 End Z.