| 1 |
From Coq Require Import ZArith QArith Qround. |
| 2 |
From Coq Require Import Lia. |
| 3 |
Require Import Crypto.Util.Decidable. |
| 4 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 5 |
Require Import Crypto.Util.ZUtil.Morphisms. |
| 6 |
Require Import Crypto.Util.Bool.Reflect. |
| 7 |
|
| 8 |
Local Open Scope Z_scope. |
| 9 |
|
| 10 |
Lemma Qfloor_le_add a b : Qfloor a + Qfloor b <= Qfloor (a + b). |
| 11 |
destruct a as [x y], b as [a b]; cbv [Qceiling Qfloor Qplus Qopp QArith_base.Qden QArith_base.Qnum]. |
| 12 |
Z.div_mod_to_quot_rem_in_goal; nia. |
| 13 |
Qed. |
| 14 |
|
| 15 |
Lemma Qceiling_le_add a b : Qceiling (a + b) <= Qceiling a + Qceiling b. |
| 16 |
destruct a as [x y], b as [a b]; cbv [Qceiling Qfloor Qplus Qopp QArith_base.Qden QArith_base.Qnum]. |
| 17 |
Z.div_mod_to_quot_rem_in_goal. nia. |
| 18 |
Qed. |
| 19 |
|
| 20 |
Lemma add_floor_l_le_ceiling a b : Qfloor a + Qceiling b <= Qceiling (a + b). |
| 21 |
destruct a as [x y], b as [a b]; cbv [Qceiling Qfloor Qplus Qopp QArith_base.Qden QArith_base.Qnum]. |
| 22 |
Z.div_mod_to_quot_rem_in_goal; nia. |
| 23 |
Qed. |
| 24 |
|
| 25 |
Lemma Zle_floor_ceiling a : Z.le (Qfloor a) (Qceiling a). |
| 26 |
pose proof Qle_floor_ceiling. |
| 27 |
destruct a as [x y]; cbv [Qceiling Qfloor Qplus Qopp QArith_base.Qden QArith_base.Qnum]. |
| 28 |
Z.div_mod_to_quot_rem_in_goal; nia. |
| 29 |
Qed. |
| 30 |
|
| 31 |
Section pow_ceil_mul_nat. |
| 32 |
Context b f (b_nz:0 <> b) (f_pos:(0 <= f)%Q). |
| 33 |
Local Notation wt i := (b^Qceiling (f * inject_Z (Z.of_nat i))) (only parsing). |
| 34 |
|
| 35 |
Lemma zero_le_ceil_mul_nat i : |
| 36 |
0 <= Qceiling (f * inject_Z (Z.of_nat i))%Q. |
| 37 |
Proof. |
| 38 |
rewrite Zle_Qle. |
| 39 |
eapply Qle_trans; [|eapply Qle_ceiling]. |
| 40 |
eapply Qle_trans; [|eapply (Qmult_le_compat_r 0)]; trivial. |
| 41 |
cbv; discriminate. |
| 42 |
apply (Qle_trans _ (inject_Z 0)); [vm_decide|]. |
| 43 |
change 0%Q with (inject_Z 0). |
| 44 |
rewrite <-Zle_Qle. |
| 45 |
eapply Zle_0_nat. |
| 46 |
Qed. |
| 47 |
|
| 48 |
Lemma pow_ceil_mul_nat_nonzero |
| 49 |
: forall i, wt i <> 0. |
| 50 |
Proof. |
| 51 |
intros. |
| 52 |
eapply Z.pow_nonzero; try lia. |
| 53 |
eapply zero_le_ceil_mul_nat. |
| 54 |
Qed. |
| 55 |
|
| 56 |
Lemma pow_ceil_mul_nat_nonneg (Hb : 0 <= b) |
| 57 |
: forall i, 0 <= wt i. |
| 58 |
Proof. |
| 59 |
intros. |
| 60 |
apply Z.pow_nonneg; assumption. |
| 61 |
Qed. |
| 62 |
|
| 63 |
Lemma pow_ceil_mul_S i : |
| 64 |
wt (S i) = |
| 65 |
(b ^ (Qceiling (f + f * inject_Z (Z.of_nat i)) - Qceiling (f * inject_Z (Z.of_nat i))) * wt i). |
| 66 |
Proof. |
| 67 |
rewrite Nat2Z.inj_succ. |
| 68 |
rewrite <-Z.add_1_l. |
| 69 |
rewrite inject_Z_plus. |
| 70 |
change (inject_Z 1) with 1%Q. |
| 71 |
rewrite Qmult_plus_distr_r, Qmult_1_r. |
| 72 |
let g := constr:((f * inject_Z (Z.of_nat i))%Q) in |
| 73 |
replace (Qceiling (f+g)) with ((Qceiling (f+g)-Qceiling g)+Qceiling g) at 1 by lia. |
| 74 |
rewrite Z.pow_add_r; [reflexivity|eapply Zle_minus_le_0|apply zero_le_ceil_mul_nat]. |
| 75 |
eapply Qceiling_resp_le. |
| 76 |
rewrite <-Qplus_0_l at 1. |
| 77 |
eapply Qplus_le_compat; |
| 78 |
auto with qarith. |
| 79 |
Qed. |
| 80 |
|
| 81 |
Lemma pow_ceil_mul_nat_multiples i : |
| 82 |
wt (S i) mod (wt i) = 0. |
| 83 |
Proof. |
| 84 |
rewrite pow_ceil_mul_S, Z_mod_mult; reflexivity. |
| 85 |
Qed. |
| 86 |
End pow_ceil_mul_nat. |
| 87 |
|
| 88 |
Section pow_ceil_mul_nat2. |
| 89 |
Context b f (b_pos:0 < b) (f_ge_1:(1 <= f)%Q). |
| 90 |
Local Notation wt i := (b^Qceiling (f * inject_Z (Z.of_nat i))) (only parsing). |
| 91 |
|
| 92 |
Lemma pow_ceil_mul_nat_pos |
| 93 |
: forall i, wt i > 0. |
| 94 |
Proof. |
| 95 |
intros. |
| 96 |
eapply Z.gt_lt_iff. |
| 97 |
eapply Z.pow_pos_nonneg; [assumption|]. |
| 98 |
eapply zero_le_ceil_mul_nat. |
| 99 |
eapply (Qle_trans _ 1 _); [vm_decide|assumption]. |
| 100 |
Qed. |
| 101 |
|
| 102 |
Lemma pow_ceil_mul_nat_divide i : |
| 103 |
wt (S i) / (wt i) > 0. |
| 104 |
Proof. |
| 105 |
rewrite pow_ceil_mul_S. |
| 106 |
rewrite Z_div_mult_full; [|apply pow_ceil_mul_nat_nonzero]. |
| 107 |
rewrite Z.gt_lt_iff. |
| 108 |
eapply Z.pow_pos_nonneg; [assumption|]. |
| 109 |
etransitivity; [|eapply Z.sub_le_ge_Proper]. |
| 110 |
2:eapply add_floor_l_le_ceiling. |
| 111 |
2:eapply Z.ge_le_iff; reflexivity. |
| 112 |
assert (1 <= Qfloor f); [|lia]. |
| 113 |
change 1%Z with (Qfloor 1). |
| 114 |
eapply Qfloor_resp_le. |
| 115 |
all: trivial; try lia; (eapply (Qle_trans _ 1 _); [vm_decide|assumption]). |
| 116 |
Qed. |
| 117 |
|
| 118 |
Lemma pow_ceil_mul_nat_divide_nonzero i : |
| 119 |
wt (S i) / (wt i) <> 0. |
| 120 |
Proof. |
| 121 |
pose proof (pow_ceil_mul_nat_divide i). |
| 122 |
lia. |
| 123 |
Qed. |
| 124 |
|
| 125 |
Lemma pow_ceil_mul_nat_divide_upperbound i : |
| 126 |
wt (S i) / (wt i) <= b^Qceiling f. |
| 127 |
Proof. |
| 128 |
rewrite pow_ceil_mul_S. |
| 129 |
rewrite Z_div_mult_full; [|apply pow_ceil_mul_nat_nonzero]. |
| 130 |
all:[ > | lia | eapply (Qle_trans _ 1 _); [vm_decide|assumption].. ]. |
| 131 |
apply Z.pow_le_mono_r; [ assumption | ]. |
| 132 |
rewrite Z.le_sub_le_add_l, Z.add_comm. |
| 133 |
etransitivity; [ | apply Qceiling_le_add ]. |
| 134 |
reflexivity. |
| 135 |
Qed. |
| 136 |
End pow_ceil_mul_nat2. |
| 137 |
|
| 138 |
Scheme Equality for Q. |
| 139 |
Scheme Induction for Q Sort Prop. |
| 140 |
Scheme Induction for Q Sort Set. |
| 141 |
Scheme Induction for Q Sort Type. |
| 142 |
Scheme Minimality for Q Sort Prop. |
| 143 |
Scheme Minimality for Q Sort Set. |
| 144 |
Scheme Minimality for Q Sort Type. |
| 145 |
|
| 146 |
Global Instance reflect_eq_Q : reflect_rel (@eq Q) Q_beq | 10 |
| 147 |
:= reflect_of_brel internal_Q_dec_bl internal_Q_dec_lb. |
| 148 |
|
| 149 |
Global Instance Q_rect_Proper {P} |
| 150 |
: Proper (forall_relation (fun _ => forall_relation (fun _ => eq)) ==> forall_relation (fun _ => eq)) (Q_rect P). |
| 151 |
Proof. |
| 152 |
intros f g Hfg [? ?]; cbn; apply Hfg. |
| 153 |
Qed. |
| 154 |
|
| 155 |
Global Instance Q_rect_Proper_nondep {T} |
| 156 |
: Proper (pointwise_relation _ (pointwise_relation _ eq) ==> eq ==> eq) (Q_rect (fun _ => T)). |
| 157 |
Proof. |
| 158 |
intros f g Hfg [? ?] y ?; subst y; cbn; apply Hfg. |
| 159 |
Qed. |
| 160 |
|
| 161 |
Global Instance Q_rect_Proper_nondep_gen {P} (R : relation P) : Proper ((eq ==> eq ==> R) ==> eq ==> R) (@Q_rect (fun _ => P)) | 100. |
| 162 |
Proof. |
| 163 |
intros f g Hfg [? ?] y ?; subst y; cbn; now apply Hfg. |
| 164 |
Qed. |