| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import Lia. |
| 3 |
Require Import Crypto.Util.ZRange. |
| 4 |
Require Import Crypto.Util.ZRange.BasicLemmas. |
| 5 |
Require Import Crypto.Util.ZRange.Operations. |
| 6 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 7 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 8 |
Require Import Crypto.Util.ZUtil.Tactics.PeelLe. |
| 9 |
Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos. |
| 10 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 11 |
Require Import Crypto.Util.ZUtil.ZSimplify. |
| 12 |
Require Import Crypto.Util.ZUtil.ZSimplify.Core. |
| 13 |
Require Import Crypto.Util.ZUtil.Modulo. |
| 14 |
Require Import Crypto.Util.ZUtil.Div. |
| 15 |
Require Import Crypto.Util.Prod. |
| 16 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 17 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 18 |
Local Open Scope Z_scope. |
| 19 |
|
| 20 |
Module ZRange. |
| 21 |
Lemma is_bounded_by_bool_split_bounds_pos x r m (Hm : 0 < m) |
| 22 |
: is_bounded_by_bool x r = true |
| 23 |
-> andb (is_bounded_by_bool (x mod m) (fst (Operations.ZRange.split_bounds_pos r m))) |
| 24 |
(is_bounded_by_bool (x / m) (snd (Operations.ZRange.split_bounds_pos r m))) = true. |
| 25 |
Proof. |
| 26 |
cbv [is_bounded_by_bool Operations.ZRange.split_bounds_pos andb]. |
| 27 |
repeat first [ progress intros |
| 28 |
| break_innermost_match_step |
| 29 |
| break_innermost_match_hyps_step |
| 30 |
| progress subst |
| 31 |
| progress cbn [fst snd lower upper] in * |
| 32 |
| reflexivity |
| 33 |
| discriminate |
| 34 |
| lia |
| 35 |
| progress Z.ltb_to_lt |
| 36 |
| progress Z.peel_le |
| 37 |
| match goal with |
| 38 |
| [ r : zrange |- _ ] => let l := fresh "l" in let u := fresh "u" in destruct r as [l u] |
| 39 |
| [ H : (?x < ?m)%Z, H' : (?m * ?q + _ <= ?x)%Z |- _ ] |
| 40 |
=> assert (q < 0 \/ q = 0 \/ 0 < q)%Z by nia; destruct_head'_or; |
| 41 |
[ assert (m * q < 0)%Z by nia; nia | progress subst | assert (0 < m * q)%Z by nia; nia ] |
| 42 |
end |
| 43 |
| progress autorewrite with zsimplify_const in * |
| 44 |
| progress Z.div_mod_to_quot_rem |
| 45 |
| nia ]. |
| 46 |
Qed. |
| 47 |
|
| 48 |
Lemma is_bounded_by_bool_fst_split_bounds_pos x r m (Hm : 0 < m) |
| 49 |
: is_bounded_by_bool x r = true |
| 50 |
-> (is_bounded_by_bool (x mod m) (fst (Operations.ZRange.split_bounds_pos r m))) = true. |
| 51 |
Proof. intro H; pose proof (@is_bounded_by_bool_split_bounds_pos x r m Hm H); Bool.split_andb; assumption. Qed. |
| 52 |
|
| 53 |
Lemma is_bounded_by_bool_snd_split_bounds_pos x r m (Hm : 0 < m) |
| 54 |
: is_bounded_by_bool x r = true |
| 55 |
-> (is_bounded_by_bool (x / m) (snd (Operations.ZRange.split_bounds_pos r m))) = true. |
| 56 |
Proof. intro H; pose proof (@is_bounded_by_bool_split_bounds_pos x r m Hm H); Bool.split_andb; assumption. Qed. |
| 57 |
|
| 58 |
Lemma is_bounded_by_bool_split_bounds x r m |
| 59 |
: is_bounded_by_bool x r = true |
| 60 |
-> andb (is_bounded_by_bool (x mod m) (fst (Operations.ZRange.split_bounds r m))) |
| 61 |
(is_bounded_by_bool (x / m) (snd (Operations.ZRange.split_bounds r m))) = true. |
| 62 |
Proof. |
| 63 |
intro; cbv [ZRange.split_bounds]; eta_expand; break_match; cbn [fst snd] in *. |
| 64 |
all: Z.ltb_to_lt. |
| 65 |
all: Z.replace_all_neg_with_pos. |
| 66 |
- now apply is_bounded_by_bool_split_bounds_pos. |
| 67 |
- autorewrite with zsimplify_const. now rewrite 1?Bool.andb_comm. |
| 68 |
- rewrite Z.div_opp_r, Z.mod_opp_r, ZRange.is_bounded_by_bool_opp. |
| 69 |
now apply is_bounded_by_bool_split_bounds_pos; [lia|rewrite ZRange.is_bounded_by_bool_opp]. |
| 70 |
Qed. |
| 71 |
|
| 72 |
Lemma is_bounded_by_bool_split_bounds_and x r m |
| 73 |
: is_bounded_by_bool x r = true |
| 74 |
-> and (is_bounded_by_bool (x mod m) (fst (Operations.ZRange.split_bounds r m)) = true) |
| 75 |
(is_bounded_by_bool (x / m) (snd (Operations.ZRange.split_bounds r m)) = true). |
| 76 |
Proof. |
| 77 |
intro H; pose proof (@is_bounded_by_bool_split_bounds x r m H). |
| 78 |
Bool.split_andb; split; assumption. |
| 79 |
Qed. |
| 80 |
End ZRange. |