| 1 |
From Coq Require Import Morphisms. |
| 2 |
From Coq Require Import ZArith. |
| 3 |
From Coq Require Import Lia. |
| 4 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 5 |
Require Import Crypto.Util.ZUtil.Hints.ZArith. |
| 6 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 7 |
Require Import Crypto.Util.ZUtil.N2Z. |
| 8 |
Require Import Crypto.Util.ZUtil.Pow. |
| 9 |
Require Import Crypto.Util.ZUtil.Pow2. |
| 10 |
Require Import Crypto.Util.ZUtil.Div. |
| 11 |
Require Import Crypto.Util.ZUtil.Testbit. |
| 12 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 13 |
Require Import Crypto.Util.ZUtil.Tactics.ReplaceNegWithPos. |
| 14 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 15 |
Require Import Crypto.Util.ZUtil.Tactics.PeelLe. |
| 16 |
Require Import Crypto.Util.NUtil.WithoutReferenceToZ. |
| 17 |
Local Open Scope Z_scope. |
| 18 |
|
| 19 |
Module Z. |
| 20 |
Lemma lor_range : forall x y n, 0 <= x < 2 ^ n -> 0 <= y < 2 ^ n -> |
| 21 |
0 <= Z.lor x y < 2 ^ n. |
| 22 |
Proof. |
| 23 |
intros x y n H H0; assert (0 <= n) by auto with zarith lia. |
| 24 |
repeat match goal with |
| 25 |
| |- _ => progress intros |
| 26 |
| |- _ => rewrite Z.lor_spec |
| 27 |
| |- _ => rewrite Z.testbit_eqb by auto with zarith lia |
| 28 |
| |- _ => rewrite !Z.div_small by (split; try lia; eapply Z.lt_le_trans; |
| 29 |
[ intuition eassumption | apply Z.pow_le_mono_r; lia]) |
| 30 |
| |- _ => split |
| 31 |
| |- _ => apply Z.testbit_false_bound |
| 32 |
| |- _ => solve [auto with zarith] |
| 33 |
| |- _ => solve [apply Z.lor_nonneg; intuition auto] |
| 34 |
end. |
| 35 |
Qed. |
| 36 |
#[global] |
| 37 |
Hint Resolve lor_range : zarith. |
| 38 |
|
| 39 |
Lemma lor_shiftl_bounds : forall x y n m, |
| 40 |
(0 <= n)%Z -> (0 <= m)%Z -> |
| 41 |
(0 <= x < 2 ^ m)%Z -> |
| 42 |
(0 <= y < 2 ^ n)%Z -> |
| 43 |
(0 <= Z.lor y (Z.shiftl x n) < 2 ^ (n + m))%Z. |
| 44 |
Proof. |
| 45 |
intros x y n m H H0 H1 H2. |
| 46 |
apply Z.lor_range. |
| 47 |
{ split; try lia. |
| 48 |
apply Z.lt_le_trans with (m := (2 ^ n)%Z); try lia. |
| 49 |
apply Z.pow_le_mono_r; lia. } |
| 50 |
{ rewrite Z.shiftl_mul_pow2 by lia. |
| 51 |
rewrite Z.pow_add_r by lia. |
| 52 |
split; Z.zero_bounds. |
| 53 |
rewrite Z.mul_comm. |
| 54 |
apply Z.mul_lt_mono_pos_l; lia. } |
| 55 |
Qed. |
| 56 |
|
| 57 |
Lemma land_upper_bound_l : forall a b, (0 <= a) -> (0 <= b) -> |
| 58 |
Z.land a b <= a. |
| 59 |
Proof. |
| 60 |
intros a b H H0. |
| 61 |
destruct a, b; try solve [exfalso; auto]; try solve [cbv; congruence]. |
| 62 |
cbv [Z.land]. |
| 63 |
rewrite <-N2Z.inj_pos, <-N2Z.inj_le. |
| 64 |
auto using N.Pos_land_upper_bound_l. |
| 65 |
Qed. |
| 66 |
|
| 67 |
Lemma land_upper_bound_r : forall a b, (0 <= a) -> (0 <= b) -> |
| 68 |
Z.land a b <= b. |
| 69 |
Proof. |
| 70 |
intros. |
| 71 |
rewrite Z.land_comm. |
| 72 |
auto using Z.land_upper_bound_l. |
| 73 |
Qed. |
| 74 |
|
| 75 |
Section ZInequalities. |
| 76 |
Lemma land_le' : forall x y, (0 <= x)%Z -> (Z.land x y <= x)%Z. |
| 77 |
Proof. |
| 78 |
intros x y H; apply Z.ldiff_le; [assumption|]. |
| 79 |
rewrite Z.ldiff_land, Z.land_comm, Z.land_assoc. |
| 80 |
rewrite <- Z.land_0_l with (a := y); f_equal. |
| 81 |
rewrite Z.land_comm, Z.land_lnot_diag. |
| 82 |
reflexivity. |
| 83 |
Qed. |
| 84 |
|
| 85 |
Lemma lor_lower : forall x y, (0 <= x -> 0 <= y)%Z -> (x <= Z.lor x y)%Z. |
| 86 |
Proof. |
| 87 |
intros x y H. |
| 88 |
destruct (Z_lt_le_dec x 0). |
| 89 |
{ Z.replace_all_neg_with_pos. |
| 90 |
replace (-x) with (Z.lnot (x - 1)) by (cbv [Z.pred Z.lnot]; lia). |
| 91 |
rewrite <- (Z.lnot_involutive y). |
| 92 |
rewrite <- Z.lnot_land. |
| 93 |
cbv [Z.lnot]. |
| 94 |
rewrite <- !Z.sub_1_r. |
| 95 |
Z.peel_le. |
| 96 |
apply land_le'; lia. } |
| 97 |
{ apply Z.ldiff_le; [apply Z.lor_nonneg; auto|]. |
| 98 |
rewrite Z.ldiff_land. |
| 99 |
apply Z.bits_inj_iff'; intros k Hpos; apply Z.le_ge in Hpos. |
| 100 |
rewrite Z.testbit_0_l, Z.land_spec, Z.lnot_spec, Z.lor_spec; |
| 101 |
[|apply Z.ge_le; assumption]. |
| 102 |
induction (Z.testbit x k), (Z.testbit y k); cbv; reflexivity. } |
| 103 |
Qed. |
| 104 |
|
| 105 |
Lemma land_le : forall x y, (0 <= y -> 0 <= x)%Z -> (Z.land x y <= x)%Z. |
| 106 |
Proof. |
| 107 |
intros x y H. |
| 108 |
destruct (Z_lt_le_dec y 0), (Z_lt_le_dec x 0); auto using land_le' with lia. |
| 109 |
Z.replace_all_neg_with_pos. |
| 110 |
replace (-x) with (Z.lnot (x - 1)) by (cbv [Z.pred Z.lnot]; lia). |
| 111 |
replace (-y) with (Z.lnot (y - 1)) by (cbv [Z.pred Z.lnot]; lia). |
| 112 |
rewrite <- Z.lnot_lor. |
| 113 |
cbv [Z.lnot]. |
| 114 |
rewrite <- !Z.sub_1_r. |
| 115 |
Z.peel_le. |
| 116 |
apply lor_lower; lia. |
| 117 |
Qed. |
| 118 |
|
| 119 |
Lemma land_le_neg_l_lower : |
| 120 |
forall x y, (x < 0)%Z -> (0 <= y) -> (x <= Z.land x y)%Z. |
| 121 |
Proof. |
| 122 |
intros x y ? ?. destruct x, y; cbn [Z.land]; lia. |
| 123 |
Qed. |
| 124 |
|
| 125 |
Lemma land_le_neg_l_upper : |
| 126 |
forall x y, (x < 0)%Z -> (0 <= y) -> (Z.land x y <= y)%Z. |
| 127 |
Proof. |
| 128 |
intros; rewrite Z.land_comm. auto using land_le'. |
| 129 |
Qed. |
| 130 |
|
| 131 |
Lemma land_neg_l_range : |
| 132 |
forall x y, x < 0 -> 0 <= y -> x <= Z.land x y <= y. |
| 133 |
Proof. |
| 134 |
auto using land_le_neg_l_lower, land_le_neg_l_upper. |
| 135 |
Qed. |
| 136 |
Lemma land_neg_r_range : |
| 137 |
forall x y, 0 <= x -> y < 0 -> y <= Z.land x y <= x. |
| 138 |
Proof. |
| 139 |
intros; rewrite Z.land_comm. auto using land_neg_l_range. |
| 140 |
Qed. |
| 141 |
|
| 142 |
Lemma lor_neg_lower : |
| 143 |
forall x y, x < 0 -> y < 0 -> (Z.max x y <= Z.lor x y)%Z. |
| 144 |
Proof. |
| 145 |
intros x y ? ?. destruct x, y; cbn [Z.lor]; try lia. |
| 146 |
apply Z.opp_le_mono. |
| 147 |
repeat first [ rewrite N.pos_pred_spec |
| 148 |
| rewrite N.succ_pos_spec |
| 149 |
| rewrite Z.opp_max_distr |
| 150 |
| rewrite Pos2Z.opp_neg |
| 151 |
| rewrite <-!positive_N_Z |
| 152 |
| rewrite N.succ_pos_spec ]. |
| 153 |
rewrite !N2Z.inj_succ, !N2Z.inj_land, |
| 154 |
!N2Z.inj_pred, !N2Z.inj_pos by lia. |
| 155 |
match goal with |
| 156 |
| |- context [Z.land ?a ?b] => |
| 157 |
let Hab := fresh in |
| 158 |
let Hba := fresh in |
| 159 |
pose proof (land_le a b ltac:(lia)) as Hab; |
| 160 |
pose proof (land_le b a ltac:(lia)) as Hba; |
| 161 |
rewrite Z.land_comm in Hba |
| 162 |
end. |
| 163 |
lia. |
| 164 |
Qed. |
| 165 |
|
| 166 |
Lemma land_neg_lower' : |
| 167 |
forall x y n, x < 0 -> y < 0 -> |
| 168 |
- 2 ^ n <= x -> |
| 169 |
- 2 ^ n <= y -> |
| 170 |
- 2 ^ n <= Z.land x y. |
| 171 |
Proof. |
| 172 |
intros x y n. intros. |
| 173 |
destruct x, y; cbn [Z.land]; try lia; [ ]. |
| 174 |
apply Z.opp_le_mono. |
| 175 |
repeat first [ rewrite N.pos_pred_spec |
| 176 |
| rewrite N.succ_pos_spec |
| 177 |
| rewrite Z.opp_max_distr |
| 178 |
| rewrite Pos2Z.opp_neg |
| 179 |
| rewrite <-!positive_N_Z |
| 180 |
| rewrite N.succ_pos_spec |
| 181 |
| rewrite Z.opp_involutive |
| 182 |
| rewrite Z.opp_succ ]. |
| 183 |
rewrite !N2Z.inj_succ, !N2Z.inj_lor, |
| 184 |
!N2Z.inj_pred, !N2Z.inj_pos by lia. |
| 185 |
match goal with |
| 186 |
| |- context [Z.lor ?a ?b] => |
| 187 |
pose proof (lor_range a b n ltac:(lia) ltac:(lia)) |
| 188 |
end. |
| 189 |
lia. |
| 190 |
Qed. |
| 191 |
|
| 192 |
Lemma land_neg_lower : |
| 193 |
forall x y, |
| 194 |
x < 0 -> y < 0 -> |
| 195 |
- 2 ^ (Z.max (Z.log2_up (-x)) (Z.log2_up (-y))) <= Z.land x y. |
| 196 |
Proof. |
| 197 |
intros x y. intros. |
| 198 |
pose proof (Z.log2_log2_up_spec (-x) ltac:(lia)). |
| 199 |
pose proof (Z.log2_log2_up_spec (-y) ltac:(lia)). |
| 200 |
pose proof (Z.pow_le_mono 2 (Z.log2_up (-x)) |
| 201 |
2 (Z.log2_up (-y)) ltac:(lia)). |
| 202 |
pose proof (Z.pow_le_mono 2 (Z.log2_up (-y)) |
| 203 |
2 (Z.log2_up (-x)) ltac:(lia)). |
| 204 |
apply land_neg_lower'; auto; [ | ]. |
| 205 |
{ apply Z.opp_le_mono. |
| 206 |
rewrite Z.opp_involutive. |
| 207 |
apply Z.max_case_strong; lia. } |
| 208 |
{ apply Z.opp_le_mono. |
| 209 |
rewrite Z.opp_involutive. |
| 210 |
apply Z.max_case_strong; lia. } |
| 211 |
Qed. |
| 212 |
|
| 213 |
Lemma lor_le : forall x y z, |
| 214 |
(0 <= x)%Z |
| 215 |
-> (x <= y)%Z |
| 216 |
-> (y <= z)%Z |
| 217 |
-> (Z.lor x y <= (2 ^ Z.log2_up (z+1)) - 1)%Z. |
| 218 |
Proof. |
| 219 |
intros x y z H H0 H1; apply Z.ldiff_le. |
| 220 |
|
| 221 |
- apply Z.le_add_le_sub_r. |
| 222 |
replace 1%Z with (2 ^ 0)%Z by (cbv; reflexivity). |
| 223 |
rewrite Z.add_0_l. |
| 224 |
apply Z.pow_le_mono_r; [cbv; reflexivity|]. |
| 225 |
apply Z.log2_up_nonneg. |
| 226 |
|
| 227 |
- destruct (Z_lt_dec 0 z). |
| 228 |
|
| 229 |
+ assert (forall a, a - 1 = Z.pred a)%Z as HP by (intro; lia); |
| 230 |
rewrite HP, <- Z.ones_equiv; clear HP. |
| 231 |
apply Z.ldiff_ones_r_low; [apply Z.lor_nonneg; split; lia|]. |
| 232 |
rewrite Z.log2_up_eqn, Z.log2_lor; try lia. |
| 233 |
apply Z.lt_succ_r. |
| 234 |
apply Z.max_case_strong; intros; apply Z.log2_le_mono; lia. |
| 235 |
|
| 236 |
+ replace z with 0%Z by lia. |
| 237 |
replace y with 0%Z by lia. |
| 238 |
replace x with 0%Z by lia. |
| 239 |
cbv; reflexivity. |
| 240 |
Qed. |
| 241 |
|
| 242 |
Local Ltac solve_pow2 := |
| 243 |
repeat match goal with |
| 244 |
| [|- _ /\ _] => split |
| 245 |
| [|- (0 < 2 ^ _)%Z] => apply Z.pow2_gt_0 |
| 246 |
| [|- (0 <= 2 ^ _)%Z] => apply Z.pow2_ge_0 |
| 247 |
| [|- (2 ^ _ <= 2 ^ _)%Z] => apply Z.pow_le_mono_r |
| 248 |
| [|- (_ <= _)%Z] => lia |
| 249 |
| [|- (_ < _)%Z] => lia |
| 250 |
end. |
| 251 |
|
| 252 |
Lemma pow2_mod_range : forall a n m, |
| 253 |
(0 <= n) -> |
| 254 |
(n <= m) -> |
| 255 |
(0 <= Z.pow2_mod a n < 2 ^ m). |
| 256 |
Proof. |
| 257 |
intros; unfold Z.pow2_mod. |
| 258 |
rewrite Z.land_ones; [|assumption]. |
| 259 |
split; [apply Z.mod_pos_bound, Z.pow2_gt_0; assumption|]. |
| 260 |
eapply Z.lt_le_trans; [apply Z.mod_pos_bound, Z.pow2_gt_0; assumption|]. |
| 261 |
apply Z.pow_le_mono; [|assumption]. |
| 262 |
split; simpl; lia. |
| 263 |
Qed. |
| 264 |
|
| 265 |
Lemma shiftr_range : forall a n m, |
| 266 |
(0 <= n)%Z -> |
| 267 |
(0 <= m)%Z -> |
| 268 |
(0 <= a < 2 ^ (n + m))%Z -> |
| 269 |
(0 <= Z.shiftr a n < 2 ^ m)%Z. |
| 270 |
Proof. |
| 271 |
intros a n m H0 H1 H2; destruct H2. |
| 272 |
split; [apply Z.shiftr_nonneg; assumption|]. |
| 273 |
rewrite Z.shiftr_div_pow2; [|assumption]. |
| 274 |
apply Z.div_lt_upper_bound; [apply Z.pow2_gt_0; assumption|]. |
| 275 |
eapply Z.lt_le_trans; [eassumption|apply Z.eq_le_incl]. |
| 276 |
apply Z.pow_add_r; lia. |
| 277 |
Qed. |
| 278 |
|
| 279 |
|
| 280 |
Lemma shiftr_le_mono: forall a b c d, |
| 281 |
(0 <= a)%Z |
| 282 |
-> (0 <= d)%Z |
| 283 |
-> (a <= c)%Z |
| 284 |
-> (d <= b)%Z |
| 285 |
-> (Z.shiftr a b <= Z.shiftr c d)%Z. |
| 286 |
Proof. |
| 287 |
intros. |
| 288 |
repeat rewrite Z.shiftr_div_pow2; [|lia|lia]. |
| 289 |
etransitivity; [apply Z.div_le_compat_l | apply Z.div_le_mono]; solve_pow2. |
| 290 |
Qed. |
| 291 |
|
| 292 |
Lemma shiftl_le_mono: forall a b c d, |
| 293 |
(0 <= a)%Z |
| 294 |
-> (0 <= b)%Z |
| 295 |
-> (a <= c)%Z |
| 296 |
-> (b <= d)%Z |
| 297 |
-> (Z.shiftl a b <= Z.shiftl c d)%Z. |
| 298 |
Proof. |
| 299 |
intros. |
| 300 |
repeat rewrite Z.shiftl_mul_pow2; [|lia|lia]. |
| 301 |
etransitivity; [apply Z.mul_le_mono_nonneg_l|apply Z.mul_le_mono_nonneg_r]; solve_pow2. |
| 302 |
Qed. |
| 303 |
End ZInequalities. |
| 304 |
|
| 305 |
Lemma lor_bounds x y : 0 <= x -> 0 <= y |
| 306 |
-> Z.max x y <= Z.lor x y <= 2^Z.log2_up (Z.max x y + 1) - 1. |
| 307 |
Proof. |
| 308 |
apply Z.max_case_strong; intros; split; |
| 309 |
try solve [ eauto using lor_lower, Z.le_trans, lor_le with lia |
| 310 |
| rewrite Z.lor_comm; eauto using lor_lower, Z.le_trans, lor_le with lia ]. |
| 311 |
Qed. |
| 312 |
Lemma lor_bounds_lower x y : 0 <= x -> 0 <= y |
| 313 |
-> Z.max x y <= Z.lor x y. |
| 314 |
Proof. intros; apply lor_bounds; assumption. Qed. |
| 315 |
Lemma lor_bounds_upper x y : Z.lor x y <= 2^Z.log2_up (Z.max x y + 1) - 1. |
| 316 |
Proof. |
| 317 |
pose proof (proj2 (Z.lor_neg x y)). |
| 318 |
destruct (Z_lt_le_dec x 0), (Z_lt_le_dec y 0); |
| 319 |
try solve [ intros; apply lor_bounds; assumption ]; |
| 320 |
transitivity (2^0-1); |
| 321 |
try apply Z.sub_le_mono_r, Z.pow_le_mono_r, Z.log2_up_nonneg; |
| 322 |
simpl; lia. |
| 323 |
Qed. |
| 324 |
Lemma lor_bounds_gen_lower x y l : 0 <= x -> 0 <= y -> l <= Z.max x y |
| 325 |
-> l <= Z.lor x y. |
| 326 |
Proof. |
| 327 |
intros; etransitivity; |
| 328 |
solve [ apply lor_bounds; auto |
| 329 |
| eauto ]. |
| 330 |
Qed. |
| 331 |
Lemma lor_bounds_gen_upper x y u : x <= u -> y <= u |
| 332 |
-> Z.lor x y <= 2^Z.log2_up (u + 1) - 1. |
| 333 |
Proof. |
| 334 |
intros; etransitivity; [ apply lor_bounds_upper | ]. |
| 335 |
apply Z.sub_le_mono_r, Z.pow_le_mono_r, Z.log2_up_le_mono, Z.max_case_strong; |
| 336 |
lia. |
| 337 |
Qed. |
| 338 |
Lemma lor_bounds_gen x y l u : 0 <= x -> 0 <= y -> l <= Z.max x y -> x <= u -> y <= u |
| 339 |
-> l <= Z.lor x y <= 2^Z.log2_up (u + 1) - 1. |
| 340 |
Proof. auto using lor_bounds_gen_lower, lor_bounds_gen_upper. Qed. |
| 341 |
|
| 342 |
Lemma shiftl_le_Proper2 y |
| 343 |
: Proper (Z.le ==> Z.le) (fun x => Z.shiftl x y). |
| 344 |
Proof. |
| 345 |
unfold Basics.flip in *. |
| 346 |
pose proof (Zle_cases 0 y) as Hx. |
| 347 |
intros x x' H. |
| 348 |
pose proof (Zle_cases 0 x) as Hy. |
| 349 |
pose proof (Zle_cases 0 x') as Hy'. |
| 350 |
destruct (0 <=? y), (0 <=? x), (0 <=? x'); |
| 351 |
autorewrite with Zshift_to_pow; |
| 352 |
Z.replace_all_neg_with_pos; |
| 353 |
autorewrite with pull_Zopp; |
| 354 |
rewrite ?Z.div_opp_l_complete; |
| 355 |
repeat destruct (Z_zerop _); |
| 356 |
autorewrite with zsimplify_const pull_Zopp; |
| 357 |
auto with zarith; |
| 358 |
repeat match goal with |
| 359 |
| [ |- context[-?x - ?y] ] |
| 360 |
=> replace (-x - y) with (-(x + y)) by lia |
| 361 |
| _ => rewrite <- Z.opp_le_mono |
| 362 |
| _ => rewrite <- Z.add_le_mono_r |
| 363 |
| _ => solve [ auto with zarith ] |
| 364 |
| [ |- ?x <= ?y + 1 ] |
| 365 |
=> cut (x <= y); [ lia | solve [ auto with zarith ] ] |
| 366 |
| [ |- -_ <= _ ] |
| 367 |
=> solve [ transitivity (-0); auto with zarith ] |
| 368 |
end. |
| 369 |
{ repeat match goal with H : context[_ mod _] |- _ => revert H end; |
| 370 |
Z.div_mod_to_quot_rem_in_goal; nia. } |
| 371 |
Qed. |
| 372 |
|
| 373 |
Lemma shiftl_le_Proper1 x |
| 374 |
(R := fun b : bool => if b then Z.le else Basics.flip Z.le) |
| 375 |
: Proper (R (0 <=? x) ==> Z.le) (Z.shiftl x). |
| 376 |
Proof. |
| 377 |
unfold Basics.flip in *. |
| 378 |
pose proof (Zle_cases 0 x) as Hx. |
| 379 |
intros y y' H. |
| 380 |
pose proof (Zle_cases 0 y) as Hy. |
| 381 |
pose proof (Zle_cases 0 y') as Hy'. |
| 382 |
destruct (0 <=? x), (0 <=? y), (0 <=? y'); subst R; cbv beta iota in *; |
| 383 |
autorewrite with Zshift_to_pow; |
| 384 |
Z.replace_all_neg_with_pos; |
| 385 |
autorewrite with pull_Zopp; |
| 386 |
rewrite ?Z.div_opp_l_complete; |
| 387 |
repeat destruct (Z_zerop _); |
| 388 |
autorewrite with zsimplify_const pull_Zopp; |
| 389 |
auto with zarith; |
| 390 |
repeat match goal with |
| 391 |
| [ |- context[-?x - ?y] ] |
| 392 |
=> replace (-x - y) with (-(x + y)) by lia |
| 393 |
| _ => rewrite <- Z.opp_le_mono |
| 394 |
| _ => rewrite <- Z.add_le_mono_r |
| 395 |
| _ => solve [ auto with zarith ] |
| 396 |
| [ |- ?x <= ?y + 1 ] |
| 397 |
=> cut (x <= y); [ lia | solve [ auto with zarith ] ] |
| 398 |
| [ |- context[2^?x] ] |
| 399 |
=> lazymatch goal with |
| 400 |
| [ H : 1 < 2^x |- _ ] => fail |
| 401 |
| [ H : 0 < 2^x |- _ ] => fail |
| 402 |
| [ H : 0 <= 2^x |- _ ] => fail |
| 403 |
| _ => first [ assert (1 < 2^x) by auto with zarith |
| 404 |
| assert (0 < 2^x) by auto with zarith |
| 405 |
| assert (0 <= 2^x) by auto with zarith ] |
| 406 |
end |
| 407 |
| [ H : ?x <= ?y |- _ ] |
| 408 |
=> is_var x; is_var y; |
| 409 |
lazymatch goal with |
| 410 |
| [ H : 2^x <= 2^y |- _ ] => fail |
| 411 |
| [ H : 2^x < 2^y |- _ ] => fail |
| 412 |
| _ => assert (2^x <= 2^y) by auto with zarith |
| 413 |
end |
| 414 |
| [ H : ?x <= ?y, H' : ?f ?x = ?k, H'' : ?f ?y <> ?k |- _ ] |
| 415 |
=> let Hn := fresh in |
| 416 |
assert (Hn : x <> y) by congruence; |
| 417 |
assert (x < y) by lia; clear H Hn |
| 418 |
| [ H : ?x <= ?y, H' : ?f ?x <> ?k, H'' : ?f ?y = ?k |- _ ] |
| 419 |
=> let Hn := fresh in |
| 420 |
assert (Hn : x <> y) by congruence; |
| 421 |
assert (x < y) by lia; clear H Hn |
| 422 |
| _ => solve [ repeat match goal with H : context[_ mod _] |- _ => revert H end; |
| 423 |
Z.div_mod_to_quot_rem_in_goal; subst; |
| 424 |
lazymatch goal with |
| 425 |
| [ |- _ <= (?a * ?q + ?r) * ?q' ] |
| 426 |
=> transitivity (q * (a * q') + r * q'); |
| 427 |
[ assert (0 < a * q') by nia; nia |
| 428 |
| nia ] |
| 429 |
end ] |
| 430 |
end. |
| 431 |
{ replace y' with (y + (y' - y)) by lia. |
| 432 |
rewrite Z.pow_add_r, <- Zdiv_Zdiv by auto with zarith. |
| 433 |
assert (y < y') by (assert (y <> y') by congruence; lia). |
| 434 |
assert (1 < 2^(y'-y)) by auto with zarith. |
| 435 |
assert (0 < x / 2^y) |
| 436 |
by (repeat match goal with H : context[_ mod _] |- _ => revert H end; |
| 437 |
Z.div_mod_to_quot_rem_in_goal; nia). |
| 438 |
assert (2^y <= x) |
| 439 |
by (repeat match goal with H : context[_ / _] |- _ => revert H end; |
| 440 |
Z.div_mod_to_quot_rem_in_goal; nia). |
| 441 |
match goal with |
| 442 |
| [ |- ?x + 1 <= ?y ] => cut (x < y); [ lia | ] |
| 443 |
end. |
| 444 |
auto with zarith. } |
| 445 |
Qed. |
| 446 |
|
| 447 |
Lemma shiftr_le_Proper2 y |
| 448 |
: Proper (Z.le ==> Z.le) (fun x => Z.shiftr x y). |
| 449 |
Proof. apply shiftl_le_Proper2. Qed. |
| 450 |
|
| 451 |
Lemma shiftr_le_Proper1 x |
| 452 |
(R := fun b : bool => if b then Z.le else Basics.flip Z.le) |
| 453 |
: Proper (R (x <? 0) ==> Z.le) (Z.shiftr x). |
| 454 |
Proof. |
| 455 |
subst R; intros y y' H'; unfold Z.shiftr; apply shiftl_le_Proper1. |
| 456 |
unfold Basics.flip in *. |
| 457 |
pose proof (Zle_cases 0 x). |
| 458 |
pose proof (Zlt_cases x 0). |
| 459 |
destruct (0 <=? x), (x <? 0); try lia. |
| 460 |
Qed. |
| 461 |
End Z. |