| 1 |
From Coq Require Import Lia. |
| 2 |
From Coq Require Import ZArith Znumtheory. |
| 3 |
Require Import Crypto.Util.ListUtil Coq.Lists.List Crypto.Util.ListUtil.FoldBool. |
| 4 |
Require Import Crypto.Util.ZRange. |
| 5 |
Require Import Crypto.Util.ZRange.Operations. |
| 6 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 7 |
Require Import Crypto.Util.ZUtil.Notations. |
| 8 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 9 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 10 |
Require Import Crypto.Util.ZUtil.Tactics.PullPush.Modulo. |
| 11 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. |
| 12 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModDivide. |
| 13 |
Require Import Crypto.Util.ZUtil.Tactics.ZeroBounds. |
| 14 |
Require Import Crypto.Util.ZUtil.Tactics.LinearSubstitute. |
| 15 |
Require Import Crypto.Util.ZUtil.Testbit. |
| 16 |
Require Import Crypto.Util.ZUtil.Tactics.Ztestbit. |
| 17 |
Require Import Crypto.Util.ZUtil.Hints. |
| 18 |
Require Import Crypto.Util.ZUtil.Hints.Core. |
| 19 |
Require Import Crypto.Util.ZUtil.ZSimplify.Core. |
| 20 |
Require Import Crypto.Util.ZUtil.ZSimplify. |
| 21 |
Require Import Crypto.Util.ZUtil.ZSimplify.Simple. |
| 22 |
Require Import Crypto.Util.ZUtil.Definitions. |
| 23 |
Require Import Crypto.Util.ZUtil.AddGetCarry. |
| 24 |
Require Import Crypto.Util.ZUtil.MulSplit. |
| 25 |
Require Import Crypto.Util.ZUtil.Ltz. |
| 26 |
Require Import Crypto.Util.ZUtil.TruncatingShiftl. |
| 27 |
Require Import Crypto.Util.ZUtil.Zselect. |
| 28 |
Require Import Crypto.Util.ZUtil.Div. |
| 29 |
Require Import Crypto.Util.ZUtil.Divide. |
| 30 |
Require Import Crypto.Util.ZUtil.Modulo. |
| 31 |
Require Import Crypto.Util.ZUtil.Opp. |
| 32 |
Require Import Crypto.Util.ZUtil.Pow. |
| 33 |
Require Import Crypto.Util.ZUtil.Land. |
| 34 |
Require Import Crypto.Util.ZUtil.Lor. |
| 35 |
Require Import Crypto.Util.ZUtil.LandLorShiftBounds. |
| 36 |
Require Import Crypto.Util.ZUtil.Shift. |
| 37 |
Require Import Crypto.Util.ZUtil.Divide.Bool. |
| 38 |
Require Import Crypto.Util.ZRange. |
| 39 |
Require Import Crypto.Util.ZRange.Operations. |
| 40 |
Require Import Crypto.Util.ZRange.BasicLemmas. |
| 41 |
Require Import Crypto.Util.ZRange.OperationsBounds. |
| 42 |
Require Import Crypto.Util.Tactics.NormalizeCommutativeIdentifier. |
| 43 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 44 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 45 |
Require Import Crypto.Util.Tactics.SpecializeAllWays. |
| 46 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 47 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 48 |
Require Import Crypto.Util.Tactics.RewriteHyp. |
| 49 |
Require Import Crypto.Util.Tactics.Head. |
| 50 |
Require Import Crypto.Util.Tactics.SetEvars. |
| 51 |
Require Import Crypto.Util.Tactics.SubstEvars. |
| 52 |
Require Import Crypto.Util.Prod. |
| 53 |
Require Import Crypto.Util.Bool. |
| 54 |
Require Import Crypto.Util.ListUtil. |
| 55 |
Require Import Crypto.Util.ListUtil.Forall. |
| 56 |
Require Import Crypto.Util.ListUtil.ForallIn. |
| 57 |
Require Import Crypto.Util.NatUtil. |
| 58 |
Require Import Crypto.Util.Option. |
| 59 |
Require Import Crypto.Util.CPSNotations. |
| 60 |
Require Import Crypto.Util.HProp. |
| 61 |
Require Import Crypto.Util.Decidable. |
| 62 |
Require Import Crypto.Util.Bool.Reflect. |
| 63 |
Require Crypto.Util.PrimitiveProd. |
| 64 |
Require Crypto.Util.PrimitiveHList. |
| 65 |
Require Import Crypto.Language.PreExtra. |
| 66 |
Require Import Crypto.CastLemmas. |
| 67 |
Require Import Crypto.Rewriter.Rules. |
| 68 |
Require Import Rewriter.Util.Prod. |
| 69 |
Require Import Crypto.Util.LetIn. |
| 70 |
Require Import Crypto.Util.Tactics.Head. |
| 71 |
Require Import Crypto.Util.Notations. |
| 72 |
Import ListNotations. Local Open Scope bool_scope. Local Open Scope Z_scope. |
| 73 |
|
| 74 |
Local Definition mymap {A B} := Eval cbv in @List.map A B. |
| 75 |
Local Definition myapp {A} := Eval cbv in @List.app A. |
| 76 |
Local Definition myflatten {A} := Eval cbv in List.fold_right myapp (@nil A). |
| 77 |
Local Notation dont_do_again := (pair false) (only parsing). |
| 78 |
Local Notation do_again := (pair true) (only parsing). |
| 79 |
|
| 80 |
Local Ltac start_proof := |
| 81 |
cbv [snd]; hnf; cbv [id |
| 82 |
PrimitiveHList.hlist ident.eagerly ident.literal Let_In ident.cast2]; ident.fancy.cbv_fancy; |
| 83 |
repeat apply PrimitiveProd.Primitive.pair; try exact tt. |
| 84 |
|
| 85 |
Local Hint Resolve |
| 86 |
prod_rect_nodep_eta |
| 87 |
eq_repeat_nat_rect |
| 88 |
eq_app_list_rect |
| 89 |
eq_combine_list_rect |
| 90 |
eq_firstn_nat_rect |
| 91 |
eq_skipn_nat_rect |
| 92 |
eq_update_nth_nat_rect |
| 93 |
: core. |
| 94 |
|
| 95 |
(* to catch [prod_rect] and not just [prod_rect_nodep] *) |
| 96 |
Local Hint Extern 0 (prod_rect _ _ _ = _) => solve [ apply prod_rect_nodep_eta ] : core. |
| 97 |
|
| 98 |
Lemma nbe_rewrite_rules_proofs |
| 99 |
: PrimitiveHList.hlist (@snd bool Prop) nbe_rewrite_rulesT. |
| 100 |
Proof using Type. start_proof; auto. Qed. |
| 101 |
|
| 102 |
Definition nbe_rewrite_rules_with_proofs |
| 103 |
:= Eval cbv [PrimitiveHList.combine_hlist nbe_rewrite_rulesT] in |
| 104 |
PrimitiveHList.combine_hlist _ nbe_rewrite_rules_proofs. |
| 105 |
|
| 106 |
Local Ltac interp_good_t_step_related := |
| 107 |
first [ lazymatch goal with |
| 108 |
| [ |- ?x = ?x ] => reflexivity |
| 109 |
| [ |- True ] => exact I |
| 110 |
| [ H : ?x = true, H' : ?x = false |- _ ] => exfalso; clear -H H'; congruence |
| 111 |
| [ |- ?G ] => has_evar G; reflexivity |
| 112 |
(*| [ |- context[_ == _] ] => reflexivity*) |
| 113 |
(*| [ |- context[(fst ?x, snd ?x)] ] => progress eta_expand |
| 114 |
| [ |- context[match ?x with pair a b => _ end] ] => progress eta_expand*) |
| 115 |
end |
| 116 |
| progress cbn [fst snd] in * |
| 117 |
| match goal with |
| 118 |
| [ H : zrange * zrange |- _ ] => destruct H |
| 119 |
end |
| 120 |
| progress intros |
| 121 |
| progress subst |
| 122 |
| assumption |
| 123 |
| progress inversion_option |
| 124 |
| progress destruct_head'_and |
| 125 |
| progress destruct_head'_unit |
| 126 |
| progress split_andb |
| 127 |
| match goal with |
| 128 |
| [ H : ?x = ?x -> _ |- _ ] => specialize (H eq_refl) |
| 129 |
| [ H : forall v : unit, _ |- _ ] => specialize (H tt) |
| 130 |
| [ H : negb ?b = true |- _ ] => rewrite (@Bool.negb_true_iff b) in H |
| 131 |
| [ |- context[Z.mul_split ?a ?b ?c] ] |
| 132 |
=> rewrite (surjective_pairing (Z.mul_split a b c)), Z.mul_split_div, Z.mul_split_mod |
| 133 |
| [ |- context[Z.zselect] ] => rewrite Z.zselect_correct |
| 134 |
| [ |- context[Z.truncating_shiftl] ] => rewrite Z.truncating_shiftl_correct |
| 135 |
| [ |- context[Z.sub_get_borrow_full ?a ?b ?c] ] |
| 136 |
=> rewrite (surjective_pairing (Z.sub_get_borrow_full a b c)), Z.sub_get_borrow_full_div, Z.sub_get_borrow_full_mod |
| 137 |
| [ |- context[Z.sub_with_get_borrow_full ?a ?b ?c ?d] ] |
| 138 |
=> rewrite (surjective_pairing (Z.sub_with_get_borrow_full a b c d)), Z.sub_with_get_borrow_full_div, Z.sub_with_get_borrow_full_mod |
| 139 |
| [ |- context[Z.add_get_carry_full ?a ?b ?c] ] |
| 140 |
=> rewrite (surjective_pairing (Z.add_get_carry_full a b c)), Z.add_get_carry_full_div, Z.add_get_carry_full_mod |
| 141 |
| [ |- context[Z.add_with_get_carry_full ?a ?b ?c ?d] ] |
| 142 |
=> rewrite (surjective_pairing (Z.add_with_get_carry_full a b c d)), Z.add_with_get_carry_full_div, Z.add_with_get_carry_full_mod |
| 143 |
| [ |- context[Z.mul_high ?a ?b ?c] ] |
| 144 |
=> rewrite Z.mul_high_div |
| 145 |
| [ |- pair _ _ = pair _ _ ] => apply f_equal2 |
| 146 |
| [ |- ?a mod ?b = ?a' mod ?b ] => apply f_equal2; lia |
| 147 |
| [ |- ?a / ?b = ?a' / ?b ] => apply f_equal2; lia |
| 148 |
| [ |- Z.opp _ = Z.opp _ ] => apply f_equal |
| 149 |
end |
| 150 |
| break_innermost_match_step |
| 151 |
| break_innermost_match_hyps_step |
| 152 |
| progress destruct_head'_or ]. |
| 153 |
|
| 154 |
Lemma arith_rewrite_rules_proofs (max_const_val : Z) |
| 155 |
: PrimitiveHList.hlist (@snd bool Prop) (arith_rewrite_rulesT max_const_val). |
| 156 |
Proof using Type. |
| 157 |
start_proof; auto; intros; try lia. |
| 158 |
all: autorewrite with zsimplify_const; try reflexivity. |
| 159 |
all: repeat first [ reflexivity |
| 160 |
| match goal with |
| 161 |
| [ |- context[Z.shiftl] ] => rewrite Z.shiftl_mul_pow2 by auto with zarith |
| 162 |
| [ |- context[Z.shiftr] ] => rewrite Z.shiftr_div_pow2 by auto with zarith |
| 163 |
| [ H : ?x = 2^?n |- context[Z.land _ (?x - 1)] ] |
| 164 |
=> rewrite !Z.sub_1_r, H, <- Z.ones_equiv, Z.land_ones by auto with zarith |
| 165 |
end |
| 166 |
| Z.div_mod_to_quot_rem; nia |
| 167 |
| interp_good_t_step_related ]. |
| 168 |
Qed. |
| 169 |
|
| 170 |
Lemma unfold_value_barrier_rewrite_rules_proofs |
| 171 |
: PrimitiveHList.hlist (@snd bool Prop) unfold_value_barrier_rewrite_rulesT. |
| 172 |
Proof using Type. now start_proof. Qed. |
| 173 |
|
| 174 |
Local Lemma unfold_is_bounded_by_bool v r |
| 175 |
: is_bounded_by_bool v r = true -> lower r <= v <= upper r. |
| 176 |
Proof using Type. |
| 177 |
cbv [is_bounded_by_bool]; intro; split_andb; Z.ltb_to_lt; split; assumption. |
| 178 |
Qed. |
| 179 |
|
| 180 |
Local Lemma unfold_is_tighter_than_bool r1 r2 |
| 181 |
: is_tighter_than_bool r1 r2 = true -> lower r2 <= lower r1 /\ upper r1 <= upper r2. |
| 182 |
Proof using Type. |
| 183 |
cbv [is_tighter_than_bool]; intro; split_andb; Z.ltb_to_lt; split; assumption. |
| 184 |
Qed. |
| 185 |
|
| 186 |
Local Ltac saturate_add_sub_bounds_step := |
| 187 |
match goal with |
| 188 |
| [ H : (ZRange.normalize ?r <=? r[0~>?s-1])%zrange = true |- context[?v mod ?s] ] |
| 189 |
=> unique assert (is_bounded_by_bool v (ZRange.normalize r) = true -> 0 <= v <= s - 1) |
| 190 |
by (let H' := fresh in |
| 191 |
intros H'; |
| 192 |
eapply ZRange.is_bounded_by_of_is_tighter_than in H; [ apply unfold_is_bounded_by_bool in H; exact H | exact H' ]) |
| 193 |
| [ H : is_bounded_by_bool (?x + ?y) (ZRange.normalize (ZRange.add ?rx ?ry)) = true -> ?T |- _ ] |
| 194 |
=> unique assert (is_bounded_by_bool x rx = true -> is_bounded_by_bool y ry = true -> T) |
| 195 |
by (let Hx := fresh in |
| 196 |
let Hy := fresh in |
| 197 |
intros Hx Hy; apply H; clear -Hx Hy; |
| 198 |
rewrite !ZRange.normalize_add in *; now apply ZRange.is_bounded_by_bool_add) |
| 199 |
| [ H : is_bounded_by_bool (?x + ?y) (ZRange.add ?rx ?ry) = true -> ?T |- _ ] |
| 200 |
=> unique assert (is_bounded_by_bool x rx = true -> is_bounded_by_bool y ry = true -> T) |
| 201 |
by (let Hx := fresh in |
| 202 |
let Hy := fresh in |
| 203 |
intros Hx Hy; apply H; clear -Hx Hy; |
| 204 |
now apply ZRange.is_bounded_by_bool_add) |
| 205 |
| [ H : is_bounded_by_bool (?x - ?y) (ZRange.normalize (ZRange.sub ?rx ?ry)) = true -> ?T |- _ ] |
| 206 |
=> unique assert (is_bounded_by_bool x rx = true -> is_bounded_by_bool y ry = true -> T) |
| 207 |
by (let Hx := fresh in |
| 208 |
let Hy := fresh in |
| 209 |
intros Hx Hy; apply H; clear -Hx Hy; |
| 210 |
rewrite !ZRange.normalize_sub in *; now apply ZRange.is_bounded_by_bool_sub) |
| 211 |
| [ H : is_bounded_by_bool (?x - ?y) (ZRange.sub ?rx ?ry) = true -> ?T |- _ ] |
| 212 |
=> unique assert (is_bounded_by_bool x rx = true -> is_bounded_by_bool y ry = true -> T) |
| 213 |
by (let Hx := fresh in |
| 214 |
let Hy := fresh in |
| 215 |
intros Hx Hy; apply H; clear -Hx Hy; |
| 216 |
now apply ZRange.is_bounded_by_bool_sub) |
| 217 |
| [ H : is_bounded_by_bool ?x ?r = true, H' : is_bounded_by_bool ?x ?r = true -> _ |- _ ] |
| 218 |
=> unique pose proof (H' H) |
| 219 |
end. |
| 220 |
|
| 221 |
Local Ltac interp_good_t_step_arith := |
| 222 |
first [ lazymatch goal with |
| 223 |
| [ |- ?x = ?x ] => reflexivity |
| 224 |
| [ |- True ] => exact I |
| 225 |
| [ H : ?x = true, H' : ?x = false |- _ ] => exfalso; clear -H H'; congruence |
| 226 |
| [ H : true = false |- _ ]=> exfalso; clear -H; congruence |
| 227 |
| [ H : false = true |- _ ]=> exfalso; clear -H; congruence |
| 228 |
end |
| 229 |
| progress cbv [option_beq] in * |
| 230 |
| match goal with |
| 231 |
| [ H : context[ZRange.normalize (ZRange.normalize _)] |- _ ] |
| 232 |
=> rewrite ZRange.normalize_idempotent in H |
| 233 |
| [ |- context[ZRange.normalize (ZRange.normalize _)] ] |
| 234 |
=> rewrite ZRange.normalize_idempotent |
| 235 |
| [ |- context[ident.cast (ZRange.normalize ?r)] ] |
| 236 |
=> rewrite ident.cast_normalize |
| 237 |
| [ H : context[ident.cast (ZRange.normalize ?r)] |- _ ] |
| 238 |
=> rewrite ident.cast_normalize in H |
| 239 |
| [ H : ?T, H' : ?T |- _ ] => clear H' |
| 240 |
| [ H : context[is_bounded_by_bool _ (ZRange.normalize (-_))] |- _ ] |
| 241 |
=> rewrite ZRange.is_bounded_by_bool_move_opp_normalize in H |
| 242 |
| [ |- context[is_bounded_by_bool _ (ZRange.normalize (-_))] ] |
| 243 |
=> rewrite ZRange.is_bounded_by_bool_move_opp_normalize |
| 244 |
| [ H : is_bounded_by_bool ?v (ZRange.normalize ?r) = true |- context[ident.cast ?r ?v] ] |
| 245 |
=> rewrite (@ident.cast_in_normalized_bounds r v) by exact H |
| 246 |
| [ H : is_bounded_by_bool ?v (ZRange.normalize ?r) = true |- context[ident.cast (-?r) (-?v)] ] |
| 247 |
=> rewrite (@ident.cast_in_normalized_bounds (-r) (-v)); |
| 248 |
[ | clear -H ] |
| 249 |
| [ |- context[ident.cast ?r (-ident.cast (-?r) ?v)] ] |
| 250 |
=> rewrite (ident.cast_in_normalized_bounds r (-ident.cast (-r) v)) |
| 251 |
by (rewrite <- ZRange.is_bounded_by_bool_move_opp_normalize; apply ident.cast_always_bounded) |
| 252 |
| [ |- context[ident.cast ?r (ident.cast ?r _)] ] |
| 253 |
=> rewrite (@ident.cast_idempotent r) |
| 254 |
| [ H : is_bounded_by_bool _ ?r = true |- _] |
| 255 |
=> is_var r; unique pose proof (ZRange.is_bounded_by_normalize _ _ H) |
| 256 |
| [ H : lower ?x = upper ?x |- _ ] => is_var x; destruct x; cbn [upper lower] in * |
| 257 |
| [ H : is_bounded_by_bool ?x (ZRange.normalize r[?y~>?y]) = true |- _ ] |
| 258 |
=> apply ZRange.is_bounded_by_bool_normalize_constant_iff in H |
| 259 |
| [ H : is_bounded_by_bool ?x r[?y~>?y] = true |- _ ] |
| 260 |
=> apply ZRange.is_bounded_by_bool_constant_iff in H |
| 261 |
| [ Hx : is_bounded_by_bool ?x _ = true, Hy : is_bounded_by_bool ?y _ = true |
| 262 |
|- Z.ltz ?x ?y = _ ] |
| 263 |
=> cbv [Z.ltz]; |
| 264 |
apply unfold_is_bounded_by_bool in Hx; |
| 265 |
apply unfold_is_bounded_by_bool in Hy |
| 266 |
| [ H : is_bounded_by_bool _ (ZRange.normalize r[0 ~> 1]) = true |- _ ] |
| 267 |
=> change (ZRange.normalize r[0 ~> 1]) with r[0 ~> 1]%zrange in *; |
| 268 |
cbv [is_bounded_by_bool] in H; cbn [upper lower] in H |
| 269 |
| [ H : ?x <> ?a, H' : ?a <= ?x |- _ ] => assert (a < x) by lia; clear H H' |
| 270 |
| [ H : 0 < ?x, H' : ?x <= 1 |- _ ] => assert (x = 1) by lia; clear H H' |
| 271 |
| [ H : ?x <= 1, H' : 0 <= ?x |- _ ] => assert (x = 0 \/ x = 1) by lia; clear H H' |
| 272 |
| [ H : ?x = Z.neg _ |- context[?x] ] => rewrite H |
| 273 |
| [ |- context[ident.cast r[0~>0] ?v] ] |
| 274 |
=> rewrite (ident.platform_specific_cast_0_is_mod 0 v) by reflexivity |
| 275 |
| [ H : ?x = Z.ones _ |- context [Z.land _ ?x] ] => rewrite H |
| 276 |
| [ H : ?x = Z.ones _ |- context [Z.land ?x _] ] => rewrite H |
| 277 |
| [ |- context [Z.land (Z.ones ?n) ?x] ] => |
| 278 |
lazymatch x with |
| 279 |
| Z.ones _ => fail |
| 280 |
| _ => rewrite (Z.land_comm (Z.ones n) x) |
| 281 |
end |
| 282 |
| [ |- context [Z.land ?x (Z.ones (Z.succ (Z.log2 _)))] ] => |
| 283 |
rewrite (Z.land_ones_low x) |
| 284 |
by (repeat match goal with |
| 285 |
| H : is_bounded_by_bool _ _ = true |- _ => |
| 286 |
apply unfold_is_bounded_by_bool in H; |
| 287 |
cbn [upper lower] in H |
| 288 |
end; |
| 289 |
try apply Z.lt_succ_r; |
| 290 |
eauto using Z.log2_le_mono with lia) |
| 291 |
end |
| 292 |
| progress intros |
| 293 |
| progress subst |
| 294 |
| assumption |
| 295 |
| progress destruct_head'_and |
| 296 |
| progress Z.ltb_to_lt |
| 297 |
| progress split_andb |
| 298 |
| progress change (0 - 1) with (-1) in * |
| 299 |
| match goal with |
| 300 |
| [ |- ?a mod ?b = ?a' mod ?b ] => apply f_equal2; lia |
| 301 |
| [ |- ?a / ?b = ?a' / ?b ] => apply f_equal2; lia |
| 302 |
| [ |- Z.opp _ = Z.opp _ ] => apply f_equal |
| 303 |
end |
| 304 |
| break_innermost_match_step |
| 305 |
| break_innermost_match_hyps_step |
| 306 |
| progress destruct_head'_or |
| 307 |
| match goal with |
| 308 |
| [ |- context[-ident.cast (-?r) (-?v)] ] => rewrite (ident.cast_opp' r v) |
| 309 |
| [ |- context[ident.cast ?r ?v] ] |
| 310 |
=> is_var v; |
| 311 |
pose proof (@ident.cast_always_bounded r v); |
| 312 |
generalize dependent (ident.cast r v); clear v; intro v; intros |
| 313 |
| [ |- context[ident.cast ?r ?v] ] |
| 314 |
=> is_var v; |
| 315 |
pose proof (@ident.cast_cases r v); |
| 316 |
generalize dependent (ident.cast r v); intros |
| 317 |
| [ H : is_bounded_by_bool ?v ?r = true, H' : is_tighter_than_bool ?r ?r' = true |- _ ] |
| 318 |
=> unique assert (is_bounded_by_bool v r' = true) by (eauto 2 using ZRange.is_bounded_by_of_is_tighter_than) |
| 319 |
| [ H : is_bounded_by_bool (-?v) ?r = true, H' : (-?r <=? ?r')%zrange = true |- _ ] |
| 320 |
=> unique assert (is_bounded_by_bool v r' = true) |
| 321 |
by (apply (@ZRange.is_bounded_by_of_is_tighter_than _ _ H'); |
| 322 |
rewrite <- ZRange.is_bounded_by_bool_opp, ZRange.opp_involutive; exact H) |
| 323 |
| [ H : is_bounded_by_bool ?v (-?r) = true |- _ ] |
| 324 |
=> is_var v; |
| 325 |
unique assert (is_bounded_by_bool (-v) r = true) |
| 326 |
by now rewrite <- ZRange.is_bounded_by_bool_move_opp_normalize, ZRange.normalize_opp |
| 327 |
| [ H : is_bounded_by_bool ?x r[0~>?v-1] = true |- _ ] |
| 328 |
=> progress (try unique assert (0 <= x); try unique assert (x <= v - 1)); |
| 329 |
[ clear -H; cbv [is_bounded_by_bool] in H; cbn [lower upper] in H; Bool.split_andb; Z.ltb_to_lt; lia.. |
| 330 |
| ] |
| 331 |
end |
| 332 |
| match goal with |
| 333 |
| [ |- context[Z.shiftl] ] => rewrite Z.shiftl_mul_pow2 by auto with zarith |
| 334 |
| [ |- context[Z.shiftr] ] => rewrite Z.shiftr_div_pow2 by auto with zarith |
| 335 |
| [ |- context[Z.shiftl _ (-_)] ] => rewrite Z.shiftl_opp_r |
| 336 |
| [ |- context[Z.land _ (Z.ones _)] ] => rewrite Z.land_ones by auto using Z.log2_nonneg |
| 337 |
| [ |- context[- - _] ] => rewrite Z.opp_involutive |
| 338 |
| [ H : ?x = 2^Z.log2 ?x |- context[2^Z.log2 ?x] ] => rewrite <- H |
| 339 |
| [ H : ?x = 2^?n |- context[Z.land _ (?x - 1)] ] |
| 340 |
=> rewrite !Z.sub_1_r, H, <- Z.ones_equiv, Z.land_ones by auto with zarith |
| 341 |
| [ |- _ = _ :> BinInt.Z ] => progress normalize_commutative_identifier Z.land Z.land_comm |
| 342 |
| [ H : ?x = ?y, H' : ?x <> ?y |- _ ] => exfalso; apply H', H |
| 343 |
| [ H : ?x = 2^Z.log2_up ?x - 1 |- context[2^Z.log2_up ?x - 1] ] => rewrite <- H |
| 344 |
| [ H : ?x = 2^Z.log2 ?x, H' : context[2^Z.log2 ?x] |- _ = _ :> BinInt.Z ] |
| 345 |
=> rewrite <- H in H' |
| 346 |
| [ |- _ = _ :> BinInt.Z ] => progress autorewrite with zsimplify_const |
| 347 |
| [ H : 0 <= ?x, H' : ?x <= ?r - 1 |- context[?x mod ?r] ] |
| 348 |
=> rewrite (Z.mod_small x r) by (clear -H H'; lia) |
| 349 |
| [ H : 0 <= ?x, H' : ?x <= ?y - 1 |- context[?x / ?y] ] |
| 350 |
=> rewrite (Z.div_small x y) by (clear -H H'; lia) |
| 351 |
| [ H : ?x = 2^Z.log2 ?x |- _ ] |
| 352 |
=> unique assert (0 <= x) by (rewrite H; auto with zarith) |
| 353 |
| [ |- _ mod ?x = _ mod ?x ] |
| 354 |
=> progress (push_Zmod; pull_Zmod) |
| 355 |
| [ |- ?f (_ mod ?x) = ?f (_ mod ?x) ] |
| 356 |
=> progress (push_Zmod; pull_Zmod) |
| 357 |
| [ |- _ mod ?x = _ mod ?x ] |
| 358 |
=> apply f_equal2; (lia + nia) |
| 359 |
| _ => rewrite !Z.shiftl_mul_pow2 in * by auto using Z.log2_nonneg |
| 360 |
| _ => rewrite !Z.land_ones in * by auto using Z.log2_nonneg |
| 361 |
| H : ?x mod ?b * ?y <= _ |
| 362 |
|- context [ (?x * ?y) mod ?b ] => |
| 363 |
rewrite (PullPush.Z.mul_mod_l x y b); |
| 364 |
rewrite (Z.mod_small (x mod b * y) b) by lia |
| 365 |
| [ |- context[_ - ?x + ?x] ] => rewrite !Z.sub_add |
| 366 |
| [ |- context[_ mod (2^_) * 2^_] ] => rewrite <- !Z.mul_mod_distr_r_full |
| 367 |
| [ |- context[Z.land _ (Z.ones _)] ] => rewrite !Z.land_ones by lia |
| 368 |
| [ |- context[2^?a * 2^?b] ] => rewrite <- !Z.pow_add_r by lia |
| 369 |
| [ |- context[-?x + ?y] ] => rewrite !Z.add_opp_l |
| 370 |
| [ |- context[?n + - ?m] ] => rewrite !Z.add_opp_r |
| 371 |
| [ |- context[?n - - ?m] ] => rewrite !Z.sub_opp_r |
| 372 |
| [ |- context[Zpos ?p * ?x / Zpos ?p] ] |
| 373 |
=> rewrite (@Z.div_mul' x (Zpos p)) in * by (clear; lia) |
| 374 |
| [ H : context[Zpos ?p * ?x / Zpos ?p] |- _ ] |
| 375 |
=> rewrite (@Z.div_mul' x (Zpos p)) in * by (clear; lia) |
| 376 |
| [ |- ?f (?a mod ?r) = ?f (?b mod ?r) ] => apply f_equal; apply f_equal2; lia |
| 377 |
| [ |- context[-?a - ?b + ?c] ] => replace (-a - b + c) with (c - a - b) by (clear; lia) |
| 378 |
| [ |- context[?x - ?y + ?z] ] |
| 379 |
=> lazymatch goal with |
| 380 |
| [ |- context[z - y + x] ] |
| 381 |
=> progress replace (z - y + x) with (x - y + z) by (clear; lia) |
| 382 |
end |
| 383 |
| [ |- context[?x - ?y - ?z] ] |
| 384 |
=> lazymatch goal with |
| 385 |
| [ |- context[x - z - y] ] |
| 386 |
=> progress replace (x - z - y) with (x - y - z) by (clear; lia) |
| 387 |
end |
| 388 |
| [ |- context[?x + ?y] ] |
| 389 |
=> lazymatch goal with |
| 390 |
| [ |- context[y + x] ] |
| 391 |
=> progress replace (y + x) with (x + y) by (clear; lia) |
| 392 |
end |
| 393 |
| [ |- context[?x + ?y + ?z] ] |
| 394 |
=> lazymatch goal with |
| 395 |
| [ |- context[x + z + y] ] |
| 396 |
=> progress replace (x + z + y) with (x + y + z) by (clear; lia) |
| 397 |
| [ |- context[z + x + y] ] |
| 398 |
=> progress replace (z + x + y) with (x + y + z) by (clear; lia) |
| 399 |
| [ |- context[z + y + x] ] |
| 400 |
=> progress replace (z + y + x) with (x + y + z) by (clear; lia) |
| 401 |
| [ |- context[y + x + z] ] |
| 402 |
=> progress replace (y + x + z) with (x + y + z) by (clear; lia) |
| 403 |
| [ |- context[y + z + x] ] |
| 404 |
=> progress replace (y + z + x) with (x + y + z) by (clear; lia) |
| 405 |
end |
| 406 |
| [ |- - ident.cast (-?r) (- (?x / ?y)) = ident.cast ?r (?x' / ?y) ] |
| 407 |
=> tryif constr_eq x x' then fail else replace x with x' by lia |
| 408 |
| [ |- _ = _ :> BinInt.Z ] => progress autorewrite with zsimplify_fast |
| 409 |
end |
| 410 |
| match goal with |
| 411 |
| [ |- context[(- _) mod _] ] => rewrite Z.mod_opp_small by lia |
| 412 |
end |
| 413 |
| saturate_add_sub_bounds_step ]. |
| 414 |
|
| 415 |
Local Ltac remove_casts := |
| 416 |
repeat match goal with |
| 417 |
| [ |- context[ident.cast r[?x~>?x] ?x] ] |
| 418 |
=> rewrite (ident.cast_in_bounds r[x~>x] x) by apply ZRange.is_bounded_by_bool_constant |
| 419 |
| [ |- context[ident.cast ?r (ident.cast ?r _)] ] |
| 420 |
=> rewrite ident.cast_idempotent |
| 421 |
| [ H : context[ident.cast ?r (ident.cast ?r _)] |- _ ] |
| 422 |
=> rewrite ident.cast_idempotent in H |
| 423 |
| [ |- context[ident.cast ?r ?v] ] |
| 424 |
=> is_var v; |
| 425 |
pose proof (@ident.cast_always_bounded r v); |
| 426 |
generalize dependent (ident.cast r v); |
| 427 |
clear v; intro v; intros |
| 428 |
| [ H : context[ident.cast ?r ?v] |- _ ] |
| 429 |
=> is_var v; |
| 430 |
pose proof (@ident.cast_always_bounded r v); |
| 431 |
generalize dependent (ident.cast r v); |
| 432 |
clear v; intro v; intros |
| 433 |
| [ H : context[ZRange.constant ?v] |- _ ] => unique pose proof (ZRange.is_bounded_by_bool_normalize_constant v) |
| 434 |
| [ H : is_tighter_than_bool (?ZRf ?r1 ?r2) (ZRange.normalize ?rs) = true, |
| 435 |
H1 : is_bounded_by_bool ?v1 ?r1 = true, |
| 436 |
H2 : is_bounded_by_bool ?v2 ?r2 = true |
| 437 |
|- _ ] |
| 438 |
=> let cst := multimatch goal with |
| 439 |
| [ |- context[ident.cast rs (?Zf v1 v2)] ] => constr:(ident.cast rs (Zf v1 v2)) |
| 440 |
| [ H : context[ident.cast rs (?Zf v1 v2)] |- _ ] => constr:(ident.cast rs (Zf v1 v2)) |
| 441 |
end in |
| 442 |
lazymatch cst with |
| 443 |
| ident.cast rs (?Zf v1 v2) |
| 444 |
=> let lem := lazymatch constr:((ZRf, Zf)%core) with |
| 445 |
| (ZRange.shiftl, Z.shiftl)%core => constr:(@ZRange.is_bounded_by_bool_shiftl v1 r1 v2 r2 H1 H2) |
| 446 |
| (ZRange.shiftr, Z.shiftr)%core => constr:(@ZRange.is_bounded_by_bool_shiftr v1 r1 v2 r2 H1 H2) |
| 447 |
| (ZRange.land, Z.land)%core => constr:(@ZRange.is_bounded_by_bool_land v1 r1 v2 r2 H1 H2) |
| 448 |
end in |
| 449 |
try unique pose proof (@ZRange.is_bounded_by_of_is_tighter_than _ _ H _ lem); |
| 450 |
clear H; |
| 451 |
rewrite (@ident.cast_in_normalized_bounds rs (Zf v1 v2)) in * by assumption |
| 452 |
end |
| 453 |
| [ H : is_tighter_than_bool (?ZRf ?r1) (ZRange.normalize ?rs) = true, |
| 454 |
H1 : is_bounded_by_bool ?v1 ?r1 = true |
| 455 |
|- _ ] |
| 456 |
=> let cst := multimatch goal with |
| 457 |
| [ |- context[ident.cast rs (?Zf v1)] ] => constr:(ident.cast rs (Zf v1)) |
| 458 |
| [ H : context[ident.cast rs (?Zf v1)] |- _ ] => constr:(ident.cast rs (Zf v1)) |
| 459 |
end in |
| 460 |
lazymatch cst with |
| 461 |
| ident.cast rs (?Zf v1) |
| 462 |
=> let lem := lazymatch constr:((ZRf, Zf)%core) with |
| 463 |
| (ZRange.cc_m ?s, Z.cc_m ?s)%core => constr:(@ZRange.is_bounded_by_bool_cc_m s v1 r1 H1) |
| 464 |
end in |
| 465 |
try unique pose proof (@ZRange.is_bounded_by_of_is_tighter_than _ _ H _ lem); |
| 466 |
clear H; |
| 467 |
rewrite (@ident.cast_in_normalized_bounds rs (Zf v1)) in * by assumption |
| 468 |
end |
| 469 |
| [ H : is_bounded_by_bool ?v (ZRange.normalize ?r) = true |- context[ident.cast ?r ?v] ] |
| 470 |
=> rewrite (@ident.cast_in_normalized_bounds r v) in * by assumption |
| 471 |
| [ H : is_bounded_by_bool ?v (ZRange.normalize ?r) = true, H' : context[ident.cast ?r ?v] |- _ ] |
| 472 |
=> rewrite (@ident.cast_in_normalized_bounds r v) in * by assumption |
| 473 |
| [ H : is_bounded_by_bool ?v ?r = true, |
| 474 |
H' : is_tighter_than_bool ?r r[0~>?x-1]%zrange = true, |
| 475 |
H'' : Z.eqb ?x ?m = true |
| 476 |
|- context[?v mod ?m] ] |
| 477 |
=> unique assert (is_bounded_by_bool v r[0~>x-1] = true) |
| 478 |
by (eapply ZRange.is_bounded_by_of_is_tighter_than; eassumption) |
| 479 |
| _ => progress Z.ltb_to_lt |
| 480 |
| _ => progress subst |
| 481 |
end. |
| 482 |
|
| 483 |
Local Ltac unfold_cast_lemmas := |
| 484 |
repeat match goal with |
| 485 |
| [ H : context[ZRange.normalize (ZRange.constant _)] |- _ ] |
| 486 |
=> rewrite ZRange.normalize_constant in H |
| 487 |
| [ H : is_bounded_by_bool _ (ZRange.normalize ?r) = true |- _ ] |
| 488 |
=> is_var r; generalize dependent (ZRange.normalize r); clear r; intro r; intros |
| 489 |
| [ H : is_bounded_by_bool _ (ZRange.normalize r[0 ~> 2 ^ _ - 1]) = true |- _ ] |
| 490 |
=> rewrite ZRange.normalize_id_pow2 in H by lia |
| 491 |
| [ H : is_bounded_by_bool ?x (ZRange.constant ?x) = true |- _ ] |
| 492 |
=> clear H |
| 493 |
| [ H : is_bounded_by_bool ?x ?r = true |- _ ] |
| 494 |
=> is_var r; apply unfold_is_bounded_by_bool in H |
| 495 |
| [ H : is_bounded_by_bool ?x r[_~>_] = true |- _ ] |
| 496 |
=> apply unfold_is_bounded_by_bool in H |
| 497 |
| [ H : is_tighter_than_bool r[_~>_] r[_~>_] = true |- _ ] |
| 498 |
=> apply unfold_is_tighter_than_bool in H |
| 499 |
| _ => progress cbn [lower upper] in * |
| 500 |
| [ H : context[lower ?r] |- _ ] |
| 501 |
=> is_var r; let l := fresh "l" in let u := fresh "u" in destruct r as [l u] |
| 502 |
| [ H : context[upper ?r] |- _ ] |
| 503 |
=> is_var r; let l := fresh "l" in let u := fresh "u" in destruct r as [l u] |
| 504 |
| _ => progress Z.ltb_to_lt |
| 505 |
end. |
| 506 |
|
| 507 |
Local Ltac clear_useless_hyps := |
| 508 |
repeat match goal with |
| 509 |
| [ H : True |- _ ] => clear H |
| 510 |
| [ H : unit |- _ ] => clear H |
| 511 |
| [ H : nat |- _ ] => clear H |
| 512 |
| [ H : Z |- _ ] => clear H |
| 513 |
| [ H : zrange |- _ ] => clear H |
| 514 |
| [ H : ?x = ?x |- _ ] => clear H |
| 515 |
| [ H : ?x <= ?y <= ?z |- _ ] |
| 516 |
=> is_var x; is_var z; clear x z H |
| 517 |
| [ H : ?x <= ?x' /\ ?y' <= ?y, H' : ?x' <= ?z <= ?y' |- _ ] |
| 518 |
=> is_var x'; is_var y'; |
| 519 |
let H'' := fresh in |
| 520 |
assert (H'' : x <= z <= y) by (clear -H H'; lia); |
| 521 |
clear x' y' H H' |
| 522 |
end. |
| 523 |
|
| 524 |
Local Ltac systematically_handle_casts := |
| 525 |
remove_casts; unfold_cast_lemmas; clear_useless_hyps. |
| 526 |
|
| 527 |
Local Ltac post_testbit_step := |
| 528 |
first [ reflexivity |
| 529 |
| progress Z.ltb_to_lt |
| 530 |
| progress rewrite ?Bool.andb_true_l, ?Bool.andb_true_r, ?Bool.andb_false_l, ?Bool.andb_false_r, ?Bool.orb_true_l, ?Bool.orb_true_r, ?Bool.orb_false_l, ?Bool.orb_false_r |
| 531 |
| exfalso; lia |
| 532 |
| break_innermost_match_step |
| 533 |
| apply (f_equal2 Z.testbit); lia ]. |
| 534 |
|
| 535 |
Local Ltac handle_via_testbit := |
| 536 |
apply Z.bits_inj; intro; Ztestbit; |
| 537 |
repeat post_testbit_step. |
| 538 |
|
| 539 |
|
| 540 |
Local Ltac fin_with_nia := |
| 541 |
lazymatch goal with |
| 542 |
| [ |- ident.cast ?r _ = ident.cast ?r _ ] => apply f_equal; Z.div_mod_to_quot_rem; nia |
| 543 |
| _ => reflexivity || (Z.div_mod_to_quot_rem; (lia + nia)) |
| 544 |
end. |
| 545 |
|
| 546 |
Local Ltac do_clear_nia x y r H H' := |
| 547 |
let rec find_H x := |
| 548 |
lazymatch goal with |
| 549 |
| [ H : _ <= x < _ |- _ ] => H |
| 550 |
| [ H : _ <= x <= _ |- _ ] => H |
| 551 |
| _ => lazymatch x with |
| 552 |
| ?x0 + ?x1 |
| 553 |
=> let H0 := find_H x0 in |
| 554 |
let H1 := find_H x1 in |
| 555 |
let m0 := lazymatch type of H0 with 0 <= _ <= ?m => m end in |
| 556 |
let m1 := lazymatch type of H1 with 0 <= _ <= ?m => m end in |
| 557 |
let H := fresh in |
| 558 |
let __ := lazymatch goal with |
| 559 |
| _ => assert (H : 0 <= x <= m0 + m1) by (clear -H0 H1; lia) |
| 560 |
end in |
| 561 |
H |
| 562 |
| ?x0 - ?x1 |
| 563 |
=> let H0 := find_H x0 in |
| 564 |
let H1 := find_H x1 in |
| 565 |
let m0 := lazymatch type of H0 with 0 <= _ <= ?m => m end in |
| 566 |
let m1 := lazymatch type of H1 with 0 <= _ <= ?m => m end in |
| 567 |
let H := fresh in |
| 568 |
let __ := lazymatch goal with |
| 569 |
| _ => assert (H : -m1 <= x <= m0) by (clear -H0 H1; lia) |
| 570 |
end in |
| 571 |
H |
| 572 |
end |
| 573 |
end in |
| 574 |
idtac; |
| 575 |
let Hx := find_H x in |
| 576 |
let Hy := find_H y in |
| 577 |
lazymatch goal with |
| 578 |
| [ Hm : 0 < 2^_ <= 2^_, Hr : 0 <= r < _ |- _ ] |
| 579 |
=> clear -Hx Hy Hm Hr H' H; nia |
| 580 |
end. |
| 581 |
|
| 582 |
Lemma arith_with_casts_rewrite_rules_proofs (adc_no_carry_to_add : bool) |
| 583 |
: PrimitiveHList.hlist (@snd bool Prop) (arith_with_casts_rewrite_rulesT adc_no_carry_to_add). |
| 584 |
Proof using Type. |
| 585 |
start_proof; auto; intros; try lia. |
| 586 |
all: repeat interp_good_t_step_related. |
| 587 |
all: repeat interp_good_t_step_arith. |
| 588 |
all: remove_casts; try fin_with_nia. |
| 589 |
all: try (reflect_hyps; lia). |
| 590 |
Qed. |
| 591 |
|
| 592 |
Lemma relaxed_rules_work rland rm1 rv v : |
| 593 |
is_bounded_by_bool (upper rland) (ZRange.normalize rm1) = true -> |
| 594 |
upper rland = Z.ones (Z.succ (Z.log2 (upper rland))) -> |
| 595 |
0 = lower rland -> |
| 596 |
0 = lower rv -> |
| 597 |
0 <= upper rv -> |
| 598 |
(upper rv + 1) mod (upper rland + 1) = 0 -> |
| 599 |
ident.cast rland (ident.cast rv v &' ident.cast rm1 (upper rland)) = ident.cast rland v. |
| 600 |
Proof. |
| 601 |
intros H1 H2 H3 H4 H5 H6. |
| 602 |
replace (ident.cast rland v) with (ident.cast rland (ident.cast rv v)). |
| 603 |
- do 3 interp_good_t_step_arith. rewrite Z.land_ones. |
| 604 |
+ replace (2 ^ Z.succ (Z.log2 (upper rland))) with (upper rland + 1). |
| 605 |
-- rewrite <- ident.cast_out_of_bounds_simple_0_mod. |
| 606 |
++ destruct rland. simpl in *. subst. apply ident.cast_idempotent. |
| 607 |
++ rewrite H2. apply Ones.Z.ones_nonneg. remember (Z.log2_nonneg (upper rland)). lia. |
| 608 |
-- remember (Z.log2 _) as x. rewrite H2. subst. rewrite Z.ones_equiv. lia. |
| 609 |
+ remember (Z.log2_nonneg (upper rland)). lia. |
| 610 |
- destruct rland. destruct rv. simpl in *. subst. |
| 611 |
repeat rewrite ident.cast_out_of_bounds_simple_0_mod. |
| 612 |
+ rewrite <- Z.mod_div_mod_full. |
| 613 |
-- reflexivity. |
| 614 |
-- rewrite <- Z.mod_divide_full. assumption. |
| 615 |
+ rewrite H2. apply Ones.Z.ones_nonneg. remember (Z.log2_nonneg (upper)). lia. |
| 616 |
+ lia. |
| 617 |
+ rewrite H2. apply Ones.Z.ones_nonneg. remember (Z.log2_nonneg (upper)). lia. |
| 618 |
Qed. |
| 619 |
|
| 620 |
Lemma arith_with_relaxed_casts_rewrite_rules_proofs |
| 621 |
: PrimitiveHList.hlist (@snd bool Prop) arith_with_relaxed_casts_rewrite_rulesT. |
| 622 |
Proof using Type. |
| 623 |
start_proof; auto; intros; try lia. |
| 624 |
- apply relaxed_rules_work; assumption. |
| 625 |
- rewrite Z.land_comm. apply relaxed_rules_work; assumption. |
| 626 |
Qed. |
| 627 |
|
| 628 |
Lemma strip_literal_casts_rewrite_rules_proofs |
| 629 |
: PrimitiveHList.hlist (@snd bool Prop) strip_literal_casts_rewrite_rulesT. |
| 630 |
Proof using Type. |
| 631 |
start_proof; auto; intros; remove_casts; reflexivity. |
| 632 |
Qed. |
| 633 |
|
| 634 |
Lemma add_assoc_left_rewrite_rules_proofs |
| 635 |
: PrimitiveHList.hlist (@snd bool Prop) add_assoc_left_rewrite_rulesT. |
| 636 |
Proof using Type. start_proof; intros; lia. Qed. |
| 637 |
|
| 638 |
Lemma flatten_thunked_rects_rewrite_rules_proofs |
| 639 |
: PrimitiveHList.hlist (@snd bool Prop) flatten_thunked_rects_rewrite_rulesT. |
| 640 |
Proof using Type. start_proof; intros; reflexivity. Qed. |
| 641 |
|
| 642 |
Local Ltac saturate_goodb_step := |
| 643 |
first [ match goal with |
| 644 |
| [ H : (ZRange.normalize ?r <=? _)%zrange = true |- _ ] |
| 645 |
=> unique pose proof (ZRange.goodb_of_is_tighter_than_bool_normalize _ _ H) |
| 646 |
end ]. |
| 647 |
|
| 648 |
Local Ltac unnormalize_step := |
| 649 |
first [ match goal with |
| 650 |
| [ H : ZRange.goodb ?r = true |- _ ] |
| 651 |
=> rewrite (proj1 ZRange.normalize_id_iff_goodb H) in * |
| 652 |
end |
| 653 |
| progress rewrite ?ZRange.normalize_flip, ?ZRange.normalize_idempotent, ?ZRange.normalize_union_normalize, ?ZRange.normalize_id_iff_goodb, ?ZRange.normalize_id_pow2, ?ZRange.normalize_is_tighter_than_bool_of_goodb, ?ZRange.normalize_opp, ?ZRange.normalize_constant, ?ZRange.normalize_two_corners, ?ZRange.normalize_four_corners, ?ZRange.normalize_eight_corners, ?ZRange.normalize_two_corners_and_zero, ?ZRange.normalize_four_corners_and_zero, ?ZRange.normalize_eight_corners_and_zero, ?ZRange.normalize_log2, ?ZRange.normalize_log2_up, ?ZRange.normalize_add, ?ZRange.normalize_sub, ?ZRange.normalize_mul, ?ZRange.normalize_div, ?ZRange.normalize_shiftr, ?ZRange.normalize_shiftl, ?ZRange.normalize_land, ?ZRange.normalize_lor, ?ZRange.normalize_cc_m in * ]. |
| 654 |
|
| 655 |
|
| 656 |
Local Ltac is_bounded_by_bool_step := |
| 657 |
first [ match goal with |
| 658 |
| [ |- is_bounded_by_bool (_ / _) (_ / _) = true ] => apply ZRange.is_bounded_by_bool_div |
| 659 |
| [ |- is_bounded_by_bool (_ * _) (_ * _) = true ] => apply ZRange.is_bounded_by_bool_mul |
| 660 |
| [ |- is_bounded_by_bool (_ + _) (_ + _) = true ] => apply ZRange.is_bounded_by_bool_add |
| 661 |
| [ |- is_bounded_by_bool (_ - _) (_ - _) = true ] => apply ZRange.is_bounded_by_bool_sub |
| 662 |
| [ |- is_bounded_by_bool (- _) (- _) = true ] => rewrite ZRange.is_bounded_by_bool_opp |
| 663 |
| [ |- is_bounded_by_bool (ident.cast ?r _) (ZRange.normalize ?r) = true ] => apply ident.cast_always_bounded |
| 664 |
end ]. |
| 665 |
|
| 666 |
Lemma relax_bitwidth_adc_sbb_rewrite_rules_proofs which_bitwidths |
| 667 |
: PrimitiveHList.hlist (@snd bool Prop) (relax_bitwidth_adc_sbb_rewrite_rulesT which_bitwidths). |
| 668 |
Proof using Type. |
| 669 |
start_proof; intros; repeat interp_good_t_step_related; repeat saturate_goodb_step; repeat unnormalize_step. |
| 670 |
all: match goal with |
| 671 |
| [ H0 : (_ <=? ?sm)%zrange = true, H1 : (?sm <=? ?lg)%zrange = true |
| 672 |
|- ident.cast ?sm ?v = ident.cast ?lg ?v ] |
| 673 |
=> rewrite (ident.cast_in_bounds sm v), (ident.cast_in_bounds lg v); |
| 674 |
[ reflexivity |
| 675 |
| eapply ZRange.is_bounded_by_of_is_tighter_than; [ eassumption | ]; |
| 676 |
eapply ZRange.is_bounded_by_of_is_tighter_than; [ eassumption | ] |
| 677 |
| eapply ZRange.is_bounded_by_of_is_tighter_than; [ eassumption | ] ] |
| 678 |
end. |
| 679 |
all: repeat is_bounded_by_bool_step. |
| 680 |
Qed. |
| 681 |
|
| 682 |
Section fancy. |
| 683 |
Context (invert_low invert_high : Z (*log2wordmax*) -> Z -> option Z) |
| 684 |
(value_range flag_range : zrange). |
| 685 |
|
| 686 |
Lemma fancy_rewrite_rules_proofs |
| 687 |
: PrimitiveHList.hlist (@snd bool Prop) fancy_rewrite_rulesT. |
| 688 |
Proof using Type. start_proof. Qed. |
| 689 |
|
| 690 |
Local Ltac fancy_local_t := |
| 691 |
repeat match goal with |
| 692 |
| [ H : forall s v v', ?invert_low s v = Some v' -> v = _, |
| 693 |
H' : ?invert_low _ _ = Some _ |- _ ] => apply H in H' |
| 694 |
| [ H : forall s v v', ?invert_low s v = Some v' -> v = _ |- _ ] |
| 695 |
=> clear invert_low H |
| 696 |
| [ H : None <> None |- _ ] => exfalso; exact (H eq_refl) |
| 697 |
end. |
| 698 |
Local Ltac more_fancy_arith_t := repeat autorewrite with zsimplify in *. |
| 699 |
|
| 700 |
Lemma fancy_with_casts_rewrite_rules_proofs |
| 701 |
(Hlow : forall s v v', invert_low s v = Some v' -> v = Z.land v' (2^(s/2)-1)) |
| 702 |
(Hhigh : forall s v v', invert_high s v = Some v' -> v = Z.shiftr v' (s/2)) |
| 703 |
: PrimitiveHList.hlist (@snd bool Prop) (@fancy_with_casts_rewrite_rulesT invert_low invert_high value_range flag_range). |
| 704 |
Proof using Type. |
| 705 |
start_proof; auto; intros; try lia. |
| 706 |
Time all: repeat interp_good_t_step_related. |
| 707 |
Time all: fancy_local_t. |
| 708 |
Time all: try solve [ systematically_handle_casts; repeat interp_good_t_step_arith ]. |
| 709 |
Qed. |
| 710 |
End fancy. |
| 711 |
|
| 712 |
Local Ltac cast_to_arith r v := |
| 713 |
match goal with |
| 714 |
| [ |- context[ident.cast r v] ] |
| 715 |
=> let H := fresh in |
| 716 |
eassert (H : _); |
| 717 |
[ | rewrite (ident.cast_in_bounds r v) by exact H ] |
| 718 |
end; |
| 719 |
repeat |
| 720 |
first [ match goal with |
| 721 |
| [ |- context[andb _ _ = true] ] => rewrite Bool.andb_true_iff |
| 722 |
| [ |- and _ _ ] => split |
| 723 |
end |
| 724 |
| progress unfold is_bounded_by_bool in * |
| 725 |
| progress cbn [lower upper] in * |
| 726 |
| progress Bool.split_andb |
| 727 |
| progress Z.ltb_to_lt ]. |
| 728 |
|
| 729 |
Lemma to_double_bounds xl xh bitwidth : |
| 730 |
0 <= xl <= 2 ^ bitwidth - 1 -> |
| 731 |
0 <= xh <= 2 ^ bitwidth - 1 -> |
| 732 |
xl + xh * 2 ^ bitwidth <= 2 ^ bitwidth * 2 ^ bitwidth - 1. |
| 733 |
Proof. nia. Qed. |
| 734 |
|
| 735 |
Local Ltac pose_mod_bounds := |
| 736 |
repeat match goal with |
| 737 |
| |- context [?a mod ?b] => |
| 738 |
unique pose proof (Z.mod_pos_bound a b ltac:(auto with zarith)) |
| 739 |
end. |
| 740 |
|
| 741 |
Local Ltac pose_shiftr_bounds := |
| 742 |
repeat match goal with |
| 743 |
| |- context [?a >> ?b] => |
| 744 |
unique pose proof |
| 745 |
(Z.shiftr_nonneg_le |
| 746 |
a b ltac:(auto with zarith) ltac:(auto with zarith)); |
| 747 |
unique pose proof (proj2 (Z.shiftr_nonneg a b) ltac:(auto with zarith)) |
| 748 |
end. |
| 749 |
|
| 750 |
Local Ltac bounds_arith_hammer := |
| 751 |
try match goal with |
| 752 |
| [ H0 : 0 < ?z, H : 0 <= ?x <= ?z - 1, H' : 0 <= ?y <= ?z - 1 |- ?x * ?y / ?z <= ?z - 1 ] |
| 753 |
=> clear -H H' H0 (* fast path so we don't time out on 8.9 *) |
| 754 |
end; |
| 755 |
repeat match goal with |
| 756 |
| _ => progress pose_mod_bounds |
| 757 |
| _ => progress pose_shiftr_bounds |
| 758 |
| |- _ / _ <= _ - 1 => |
| 759 |
apply Le.Z.le_sub_1_iff; |
| 760 |
apply Z.div_lt_upper_bound; solve [auto with zarith nia] |
| 761 |
| _ => apply to_double_bounds; lia |
| 762 |
| _ => solve [Z.zero_bounds] |
| 763 |
| _ => lia |
| 764 |
end. |
| 765 |
|
| 766 |
(* TODO: can this be replaced by just adding Z.shiftr_range to zarith? *) |
| 767 |
Local Ltac use_shiftr_range := |
| 768 |
try split; Z.zero_bounds; apply Z.shiftr_range; |
| 769 |
autorewrite with zsimplify; auto with zarith. |
| 770 |
|
| 771 |
Lemma mul_split_rewrite_rules_proofs (bitwidth : Z) (lgcarrymax : Z) |
| 772 |
: PrimitiveHList.hlist (@snd bool Prop) (mul_split_rewrite_rulesT bitwidth lgcarrymax). |
| 773 |
Proof using Type. |
| 774 |
start_proof; auto; intros; try lia. |
| 775 |
(* assert bounds to help out [lia] *) |
| 776 |
all: try (assert (0 < 2 ^ bitwidth) by Z.zero_bounds; |
| 777 |
assert (0 < 2 ^ bitwidth * 2 ^ bitwidth) by Z.zero_bounds; |
| 778 |
assert ((2 ^ bitwidth - 1) * (2 ^ bitwidth - 1) <= 2 ^ bitwidth * 2 ^ bitwidth - 1) as Hle_double |
| 779 |
by (autorewrite with push_Zmul zsimplify; lia)). |
| 780 |
all: try (assert (2 <= 2 ^ lgcarrymax) by auto with zarith). |
| 781 |
all: try (assert (0 <= bitwidth) by (apply Z.nonneg_pow_pos with (a:=2); lia)). |
| 782 |
all: try (assert (bitwidth < 2 ^ bitwidth) by (apply Z.pow_gt_lin_r; lia)). |
| 783 |
all: repeat interp_good_t_step_related. |
| 784 |
all: systematically_handle_casts. |
| 785 |
all: try reflexivity. |
| 786 |
all:unfold Z.combine_at_bitwidth. |
| 787 |
all:rewrite ?Z.shiftl_mul_pow2, ?Z.shiftr_div_pow2, ?Z.pow_twice_r in * by lia. |
| 788 |
|
| 789 |
(* special case to match on a particular rule that is way easier to prove this |
| 790 |
way; this is hacky and should ideally be removed eventually *) |
| 791 |
all: try match goal with |
| 792 |
| |- ident.cast r[0 ~> 2 ^ ?b * 2 ^ ?b - 1] (_ >> _) = _ => |
| 793 |
f_equal |
| 794 |
end. |
| 795 |
|
| 796 |
(* remove whatever bounds [bounds_arith_hammer] solves *) |
| 797 |
all:repeat match goal with |
| 798 |
| [ |- context[ident.cast ?r ?v] ] |
| 799 |
=> cast_to_arith r v; [ solve [bounds_arith_hammer] .. | ] |
| 800 |
end. |
| 801 |
|
| 802 |
(* change the rest of the casts to mods *) |
| 803 |
all:rewrite ?ident.platform_specific_cast_0_is_mod by lia; |
| 804 |
autorewrite with zsimplify_fast; Z.rewrite_mod_small. |
| 805 |
|
| 806 |
all: |
| 807 |
repeat match goal with |
| 808 |
| |- context [(_ * _) mod _] => |
| 809 |
(* multiplication case *) |
| 810 |
rewrite Z.mul_div_eq' by lia; lia |
| 811 |
| |- ((_ + _) * ?y) mod _ = _ => |
| 812 |
(* double * single multiplication case *) |
| 813 |
rewrite Z.mul_add_distr_r, <- Z.mul_assoc, (Z.mul_comm (2^_) y), Z.mul_assoc |
| 814 |
| |- (?x * (_ + _)) mod _ = _ => |
| 815 |
(* single * double multiplication case *) |
| 816 |
rewrite Z.mul_add_distr_l, Z.mul_assoc |
| 817 |
| |- (_ + _) mod (_ * _) = _ => |
| 818 |
(* addition cases *) |
| 819 |
rewrite !Z.rem_mul_r by lia; |
| 820 |
push_Zmod; pull_Zmod; autorewrite with zsimplify; |
| 821 |
solve [repeat (ring_simplify; f_equal; try lia)] |
| 822 |
| |- (?m &' (_ + _)) mod _ = _ => |
| 823 |
(* and-addition case *) |
| 824 |
rewrite !(Z.land_comm m) |
| 825 |
| |- ((_ + _) &' _) mod _ = _ => |
| 826 |
(* addition-and case *) |
| 827 |
rewrite Z.land_add_high by (try apply Z.log2_lt_pow2; lia); lia |
| 828 |
| |- (_ + _) >> _ mod _ = _ => |
| 829 |
(* take only high bits *) |
| 830 |
rewrite ?Z.shiftr_div_pow2 by lia; autorewrite with zsimplify; lia |
| 831 |
| |- (_ + _) / _ = _ => |
| 832 |
(* take only high bits *) |
| 833 |
autorewrite with zsimplify; lia |
| 834 |
end. |
| 835 |
|
| 836 |
all:rewrite <-?Z.pow_twice_r,<-?Z.land_ones, <-?Z.shiftl_mul_pow2 in * by lia. |
| 837 |
all: try solve [ handle_via_testbit ]. |
| 838 |
(* should have only Z.lor cases now *) |
| 839 |
all:match goal with |
| 840 |
| |- context [Z.lor (?a >> ?b) (_ &' Z.ones ?c)] => |
| 841 |
rewrite <-(Z.mod_small (a >> b) (2 ^ c)) by auto with zarith; |
| 842 |
rewrite <-Z.land_ones, <-Z.land_lor_distr_l by auto with zarith |
| 843 |
| _ => shelve |
| 844 |
end. |
| 845 |
all:rewrite Z.lor_shiftl by use_shiftr_range. |
| 846 |
all:rewrite Z.shiftr_add_shiftl_low by lia. |
| 847 |
all:match goal with |
| 848 |
| H : ?x < ?y |- context [Z.shiftr ?a (?x - ?y)] => |
| 849 |
replace (Z.shiftr a (x - y)) with (Z.shiftl a (y - x)) |
| 850 |
by (rewrite <-Z.shiftl_opp_r; f_equal; lia) |
| 851 |
end. |
| 852 |
all:try reflexivity. |
| 853 |
|
| 854 |
all:rewrite ?Z.land_ones, Z.mod_eq by auto with zarith. |
| 855 |
all:rewrite ?(Z.mul_comm (2 ^ _)), <-?Z.shiftl_mul_pow2, <-?Z.shiftr_div_pow2 by auto with zarith. |
| 856 |
all:rewrite Z.shiftr_add_shiftl_high by use_shiftr_range. |
| 857 |
all:autorewrite with zsimplify; lia. |
| 858 |
Qed. |
| 859 |
|
| 860 |
Lemma multiret_split_rewrite_rules_proofs (bitwidth : Z) (lgcarrymax : Z) |
| 861 |
: PrimitiveHList.hlist (@snd bool Prop) (multiret_split_rewrite_rulesT bitwidth lgcarrymax). |
| 862 |
Proof using Type. |
| 863 |
assert (0 <= lgcarrymax <= bitwidth -> 0 < 2^lgcarrymax <= 2^bitwidth) by auto with zarith. |
| 864 |
assert (0 <= bitwidth -> bitwidth < 2^bitwidth) |
| 865 |
by (intros; apply Z.pow_gt_lin_r; auto with zarith). |
| 866 |
|
| 867 |
start_proof; auto; intros; try lia. |
| 868 |
all: repeat interp_good_t_step_related. |
| 869 |
all: systematically_handle_casts; autorewrite with zsimplify_fast; try reflexivity. |
| 870 |
all: subst; rewrite !ident.platform_specific_cast_0_is_mod, ?Z.sub_add, ?Z.mod_mod by lia; try reflexivity. |
| 871 |
all: autorewrite with zsimplify_fast in *; Z.rewrite_mod_divide_in_all; try reflexivity. |
| 872 |
all: progress specialize_by lia. |
| 873 |
all: try match goal with |
| 874 |
| H : ?x = 2 ^ Z.log2 ?x |- _ => |
| 875 |
rewrite H |
| 876 |
end. |
| 877 |
all: |
| 878 |
try match goal with |
| 879 |
| |- context [(2^?n - 1) mod 2^?bw] => |
| 880 |
assert (0 < 2^n < 2^bw) by auto with zarith; |
| 881 |
Z.rewrite_mod_small |
| 882 |
end. |
| 883 |
all: rewrite ?Z.land_pow2 by auto with zarith. |
| 884 |
all: push_Zmod; pull_Zmod; try reflexivity. |
| 885 |
all: Z.rewrite_mod_small. |
| 886 |
all: rewrite ?Z.shiftr_div_pow2, ?Z.shiftl_mul_pow2 by lia. |
| 887 |
all: rewrite ?Z.log2_pow2 by lia. |
| 888 |
all: Z.rewrite_mod_small. |
| 889 |
all: rewrite ?Z.shiftr_div_pow2, ?Z.shiftl_mul_pow2 by lia. |
| 890 |
all: rewrite ?Z.mod_pow_same_base_larger, |
| 891 |
?Z.mod_pow_same_base_smaller by lia. |
| 892 |
all: rewrite ?Z.ltz_mod_pow2_small by lia. |
| 893 |
all: rewrite <-?Z.add_div_ltz_1 by lia. |
| 894 |
all: try reflexivity. |
| 895 |
all: push_Zmod; pull_Zmod; try reflexivity. |
| 896 |
all: rewrite ?Z.mod_pull_div, <-?Z.pow_add_r by auto with zarith. |
| 897 |
all: rewrite <-?Z.sub_sub_div_ltz by lia. |
| 898 |
all: rewrite <-?Z.sub_div_ltz by lia. |
| 899 |
all: try rewrite Z.mul_opp_l, Z.sub_opp_l, Opp.Z.opp_sub. |
| 900 |
all: |
| 901 |
repeat match goal with |
| 902 |
| Hx : 0 <= ?x <= 2^?n - 1, |
| 903 |
Hy : 0 <= ?y <= 2^?n - 1 |- context [?x + ?y] => |
| 904 |
unique assert (0 <= x + y < 2^(n+1)) |
| 905 |
by (rewrite Z.pow_add_r, Z.pow_1_r; auto with zarith) |
| 906 |
| Hx : 0 <= ?x <= 2^?n - 1, |
| 907 |
Hy : 0 <= ?y <= 2^?n - 1 |- context [?c + ?x + ?y] => |
| 908 |
unique assert (0 <= c + x + y < 2^(n+2)) |
| 909 |
by (rewrite Z.pow_add_r, Z.pow_2_r; auto with zarith) |
| 910 |
| H : 0 < ?n |- _ => |
| 911 |
unique assert (2^(bitwidth+1) <= 2^(bitwidth+n)) |
| 912 |
by auto with zarith |
| 913 |
| H : 0 <= ?x < 2 ^ ?z |- context [?x mod 2 ^ ?y] => |
| 914 |
unique assert (2 ^ z <= 2 ^ y) by auto with zarith; |
| 915 |
unique assert (0 <= x <= 2 ^ y) by auto with zarith |
| 916 |
| _ => rewrite Z.ltz_mod_pow2_small by lia |
| 917 |
| _ => rewrite Z.add_div_pow2 by auto with zarith |
| 918 |
| _ => rewrite <-Z.add_add_div_ltz_2 by lia |
| 919 |
| _ => rewrite Z.mod_pull_div, <-?Z.pow_add_r |
| 920 |
by auto with zarith |
| 921 |
| _ => progress Z.rewrite_mod_small |
| 922 |
end. |
| 923 |
all: try solve[ |
| 924 |
try lazymatch goal with |
| 925 |
| [ |- ?x mod ?y = _ mod ?y ] |
| 926 |
=> apply f_equal2; [ | reflexivity ]; |
| 927 |
cbv [Z.ltz]; break_innermost_match; Z.ltb_to_lt |
| 928 |
end; Z.div_mod_to_quot_rem; |
| 929 |
repeat match goal with |
| 930 |
| [ H : ?x + ?y = ?d * ?q + ?r, H' : ?r < ?y |- _ ] |
| 931 |
=> is_var q; unique assert (q = 1) by do_clear_nia x y r H H'; subst |
| 932 |
| [ H : ?x + ?y = ?d * ?q + ?r, H' : ?r >= ?y |- _ ] |
| 933 |
=> is_var q; unique assert (q = 0) by do_clear_nia x y r H H'; subst |
| 934 |
| [ H : ?x - ?y = ?d * ?q + ?r, H' : ?x < ?r |- _ ] |
| 935 |
=> is_var q; unique assert (q = -1) by do_clear_nia x y r H H'; subst |
| 936 |
| [ H : ?x - ?y = ?d * ?q + ?r, H' : ?x >= ?r |- _ ] |
| 937 |
=> is_var q; unique assert (q = 0) by do_clear_nia x y r H H'; subst |
| 938 |
| [ H0 : ?x = ?d * ?q0 + ?r0, H : ?x - ?y = ?d * ?q + ?r, H' : ?r0 < ?r |- _ ] |
| 939 |
=> (* ugh, this is kind-of complicated; we could just let [nia] handle it, but it runs out of memory on 8.9 *) |
| 940 |
let x' := fresh "x" in |
| 941 |
(remember x as x' eqn:?); |
| 942 |
symmetry in H0; destruct H0; |
| 943 |
assert (r0 - y = d * (q - q0) + r) by (clear -H; nia); clear H; |
| 944 |
let q' := fresh "q" in |
| 945 |
remember (q - q0) as q' eqn:?; (tryif is_var q then Z.linear_substitute q else idtac) |
| 946 |
| _ => nia |
| 947 |
end]. |
| 948 |
Qed. |
| 949 |
|
| 950 |
Lemma noselect_rewrite_rules_proofs (bitwidth : Z) |
| 951 |
: PrimitiveHList.hlist (@snd bool Prop) (noselect_rewrite_rulesT bitwidth). |
| 952 |
Proof. |
| 953 |
assert (0 <= bitwidth -> 0 < 2^bitwidth) by auto with zarith. |
| 954 |
start_proof; auto; intros; try lia. |
| 955 |
all: repeat interp_good_t_step_related. |
| 956 |
all: systematically_handle_casts; try reflexivity. |
| 957 |
all: specialize_by auto. |
| 958 |
all: rewrite !ident.platform_specific_cast_0_is_mod by lia. |
| 959 |
all: rewrite ?Z.sub_simpl_r by auto. |
| 960 |
all: autorewrite with zsimplify_fast. |
| 961 |
Time |
| 962 |
all: repeat match goal with |
| 963 |
| _ => progress rewrite ?Z.lxor_0_l, ?Z.lxor_0_r |
| 964 |
| _ => progress rewrite ?Z.lor_0_l, ?Z.lor_0_r |
| 965 |
| _ => progress rewrite ?Z.land_0_l, ?Z.land_0_r |
| 966 |
| _ => rewrite Z.lxor_nilpotent |
| 967 |
| _ => rewrite Z.land_pow2 by auto with zarith |
| 968 |
| _ => progress Z.rewrite_mod_small |
| 969 |
| _ => progress (push_Zmod; pull_Zmod) |
| 970 |
| _ => reflexivity |
| 971 |
end. |
| 972 |
Qed. |