| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import Lia. |
| 4 |
From Coq Require Import FMapPositive. |
| 5 |
From Coq Require Import Morphisms. |
| 6 |
From Coq Require Import Relations. |
| 7 |
Require Export Crypto.Language.PreExtra. |
| 8 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 9 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 10 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 11 |
Require Import Crypto.Util.Tactics.SpecializeAllWays. |
| 12 |
Require Import Crypto.Util.Tactics.RewriteHyp. |
| 13 |
Require Import Crypto.Util.Tactics.SplitInContext. |
| 14 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 15 |
Require Import Crypto.Util.ZUtil.Tactics.LtbToLt. |
| 16 |
Require Import Crypto.Util.ZUtil.Tactics.DivModToQuotRem. |
| 17 |
Require Import Crypto.Util.ZUtil.Tactics.SplitMinMax. |
| 18 |
Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall. |
| 19 |
Require Import Crypto.Util.Option. |
| 20 |
Require Import Crypto.Util.NatUtil. |
| 21 |
Require Import Crypto.Util.ZRange. |
| 22 |
Require Import Crypto.Util.ZRange.Operations. |
| 23 |
Require Import Crypto.Util.ZRange.BasicLemmas. |
| 24 |
Require Import Crypto.Util.Sigma. |
| 25 |
Require Import Crypto.Util.ListUtil. |
| 26 |
Require Import Crypto.Util.ListUtil.Forall. |
| 27 |
Require Import Crypto.Util.Bool. |
| 28 |
Require Import Crypto.Util.Prod. |
| 29 |
Require Import Crypto.Util.Logic.ProdForall. |
| 30 |
Require Import Crypto.Util.Decidable. |
| 31 |
Require Import Crypto.Util.HProp. |
| 32 |
Require Import Crypto.Util.CPSNotations. |
| 33 |
Require Import Crypto.Util.Notations. |
| 34 |
Import Coq.Lists.List ListNotations. Local Open Scope list_scope. |
| 35 |
|
| 36 |
Import EqNotations. |
| 37 |
Module ident. |
| 38 |
Local Transparent ident.cast. |
| 39 |
Section with_cast. |
| 40 |
Local Notation cast := ident.cast (only parsing). |
| 41 |
|
| 42 |
Lemma cast_opp' r v : (-cast (-r) (-v) = cast r v)%Z. |
| 43 |
Proof. |
| 44 |
pose proof (ZRange.goodb_normalize r); cbv [ZRange.goodb] in *. |
| 45 |
cbv [cast ident.is_more_pos_than_neg]; rewrite !ZRange.normalize_opp, !ZRange.opp_involutive, !Z.opp_involutive. |
| 46 |
repeat change (lower (-?r)) with (-upper r)%Z. |
| 47 |
repeat change (upper (-?r)) with (-lower r)%Z. |
| 48 |
destruct (ZRange.normalize r) as [l u]; clear r; cbn [lower upper] in *. |
| 49 |
rewrite !Z.abs_opp. |
| 50 |
repeat first [ progress split_andb |
| 51 |
| progress rewrite ?Bool.andb_false_iff in * |
| 52 |
| progress rewrite ?Bool.orb_true_iff in * |
| 53 |
| progress rewrite ?Bool.orb_false_iff in * |
| 54 |
| progress destruct_head'_and |
| 55 |
| progress Z.ltb_to_lt |
| 56 |
| progress subst |
| 57 |
| rewrite !Z.sub_opp_r |
| 58 |
| rewrite !Z.opp_involutive |
| 59 |
| rewrite !Z.add_0_r |
| 60 |
| rewrite !Z.sub_0_r |
| 61 |
| rewrite !Z.sub_diag |
| 62 |
| rewrite !Z.mod_1_r |
| 63 |
| progress change (- (-1))%Z with 1%Z |
| 64 |
| progress change (0 - 1)%Z with (-1)%Z |
| 65 |
| progress change (-0)%Z with 0%Z |
| 66 |
| lia |
| 67 |
| match goal with |
| 68 |
| [ H : ?x = ?x |- _ ] => clear H |
| 69 |
| [ H : (?x <= ?x)%Z |- _ ] => clear H |
| 70 |
| [ H : ?T, H' : ?T |- _ ] => clear H' |
| 71 |
| [ H : (-?x = -?y)%Z |- _ ] => assert (x = y) by lia; clear H |
| 72 |
| [ H : (0 <= - ?x)%Z |- _ ] => assert (x <= 0)%Z by lia; clear H |
| 73 |
| [ H : (?x > ?y)%Z |- _ ] => assert (y < x)%Z by lia; clear H |
| 74 |
| [ H : (?x >= ?y)%Z |- _ ] => assert (y <= x)%Z by lia; clear H |
| 75 |
| [ H : (- ?y < -?x)%Z |- _ ] => assert (x < y)%Z by lia; clear H |
| 76 |
| [ H : (?x <= ?y)%Z, H' : (?y <= ?x)%Z |- _ ] => assert (x = y) by lia; clear H H' |
| 77 |
(*| [ H : Z.abs ?l = Z.abs ?u |- _ ] => progress cbv [ZRange.opp]; cbn [lower upper]*) |
| 78 |
| [ H : (?l <= ?u)%Z, H' : (?u < 0)%Z, H'' : context[Z.abs ?l] |- _ ] |
| 79 |
=> rewrite (Z.abs_neq l), (Z.abs_neq u) in * by lia |
| 80 |
| [ H : (?l <= ?u)%Z, H' : (0 < ?l)%Z, H'' : context[Z.abs ?l] |- _ ] |
| 81 |
=> rewrite (Z.abs_eq l), (Z.abs_eq u) in * by lia |
| 82 |
| [ |- context[(?x mod (-?a + ?b + 1))%Z] ] |
| 83 |
=> replace (x mod (-a + b + 1))%Z with (-((-x) mod (- - (a - b - 1))))%Z |
| 84 |
by (rewrite !Zmod_opp_opp, !Z.opp_involutive; apply f_equal2; lia) |
| 85 |
| [ |- context[(?x mod (?a - ?b + 1))%Z] ] |
| 86 |
=> replace (x mod (a - b + 1))%Z with (-((-x) mod (- - (b - a - 1))))%Z |
| 87 |
by (rewrite !Zmod_opp_opp, !Z.opp_involutive; apply f_equal2; lia) |
| 88 |
| [ |- context[(?x mod (-1))%Z] ] |
| 89 |
=> replace ((x mod (-1)))%Z with (-((-x) mod (- - 1)))%Z |
| 90 |
by (rewrite !Zmod_opp_opp, !Z.opp_involutive; apply f_equal2; lia) |
| 91 |
end |
| 92 |
| progress destruct_head'_or |
| 93 |
| break_innermost_match_step ]. |
| 94 |
Qed. |
| 95 |
|
| 96 |
Lemma cast_in_normalized_bounds r v : is_bounded_by_bool v (ZRange.normalize r) = true -> cast r v = v. |
| 97 |
Proof. cbv [cast is_bounded_by_bool]; break_innermost_match; congruence. Qed. |
| 98 |
|
| 99 |
Lemma cast_in_bounds r v : is_bounded_by_bool v r = true -> cast r v = v. |
| 100 |
Proof. |
| 101 |
intro; apply cast_in_normalized_bounds, ZRange.is_bounded_by_normalize; assumption. |
| 102 |
Qed. |
| 103 |
|
| 104 |
Lemma cast_always_bounded r v : is_bounded_by_bool (cast r v) (ZRange.normalize r) = true. |
| 105 |
Proof. |
| 106 |
pose proof (ZRange.goodb_normalize r); cbv [ZRange.goodb] in *. |
| 107 |
cbv [cast]; break_innermost_match; Z.div_mod_to_quot_rem. |
| 108 |
all: destruct (ZRange.normalize r) as [l u]; clear r; cbn [lower upper ZRange.opp] in *. |
| 109 |
all: cbv [is_bounded_by_bool]; cbn [lower upper]. |
| 110 |
all: repeat first [ progress rewrite ?Bool.andb_true_iff, ?Bool.andb_false_iff in * |
| 111 |
| rewrite !Z.leb_le in * |
| 112 |
| progress destruct_head'_and |
| 113 |
| lia ]. |
| 114 |
Qed. |
| 115 |
|
| 116 |
Lemma cast_bounded r v : (lower r <= upper r)%Z -> is_bounded_by_bool (cast r v) r = true. |
| 117 |
Proof. |
| 118 |
intro H; replace r with (ZRange.normalize r) at 2; [ apply cast_always_bounded | ]. |
| 119 |
cbv [ZRange.normalize lower upper] in *; destruct r; split_min_max; reflexivity. |
| 120 |
Qed. |
| 121 |
|
| 122 |
Lemma cast_cases r v |
| 123 |
: is_bounded_by_bool (cast r v) (ZRange.normalize r) = true |
| 124 |
/\ ((is_bounded_by_bool v (ZRange.normalize r) = true /\ cast r v = v) |
| 125 |
\/ is_bounded_by_bool v (ZRange.normalize r) = false). |
| 126 |
Proof. |
| 127 |
split; [ apply cast_always_bounded | ]. |
| 128 |
pose proof (cast_in_normalized_bounds r v). |
| 129 |
edestruct is_bounded_by_bool; tauto. |
| 130 |
Qed. |
| 131 |
|
| 132 |
Lemma is_more_pos_then_neg_0_u u v |
| 133 |
: (0 <= u)%Z |
| 134 |
-> ident.is_more_pos_than_neg (ZRange.normalize r[0~>u]) v = true. |
| 135 |
Proof using Type. |
| 136 |
intro. |
| 137 |
cbv [ident.is_more_pos_than_neg]; cbn [upper lower]. |
| 138 |
rewrite (proj1 ZRange.normalize_id_iff_goodb) |
| 139 |
by (cbv [ZRange.goodb lower upper]; Z.ltb_to_lt; assumption). |
| 140 |
cbn [lower upper]. |
| 141 |
rewrite Z.abs_0, Z.abs_eq by assumption. |
| 142 |
cbv [andb orb]; break_innermost_match; Z.ltb_to_lt; try lia; reflexivity. |
| 143 |
Qed. |
| 144 |
|
| 145 |
Lemma cast_out_of_bounds_simple_0_mod u v |
| 146 |
: (0 <= u)%Z |
| 147 |
-> (cast r[0~>u] v = v mod (u + 1))%Z. |
| 148 |
Proof. |
| 149 |
intro H0. |
| 150 |
pose proof (is_more_pos_then_neg_0_u u v H0) as H1. |
| 151 |
cbv [cast]; rewrite H1. |
| 152 |
rewrite (proj1 ZRange.normalize_id_iff_goodb) |
| 153 |
by (cbv [ZRange.goodb lower upper]; Z.ltb_to_lt; assumption). |
| 154 |
cbn [lower upper]. |
| 155 |
rewrite !Z.sub_0_r, !Z.add_0_r. |
| 156 |
break_innermost_match; split_andb; Z.ltb_to_lt; |
| 157 |
Z.rewrite_mod_small; reflexivity. |
| 158 |
Qed. |
| 159 |
|
| 160 |
(* N.B. This lemma depends on the hard-coded behavior of casting |
| 161 |
out of range being modulo, and hence we label it |
| 162 |
"platform-specific". *) |
| 163 |
Lemma platform_specific_cast_0_is_mod u v |
| 164 |
: (0 <= u)%Z |
| 165 |
-> (cast r[0~>u] v = v mod (u + 1))%Z. |
| 166 |
Proof. |
| 167 |
intro H0. |
| 168 |
pose proof (is_more_pos_then_neg_0_u u v H0) as H1. |
| 169 |
cbv [cast]; rewrite H1. |
| 170 |
rewrite (proj1 ZRange.normalize_id_iff_goodb) |
| 171 |
by (cbv [ZRange.goodb lower upper]; Z.ltb_to_lt; assumption). |
| 172 |
cbn [lower upper]. |
| 173 |
rewrite !Z.sub_0_r, !Z.add_0_r. |
| 174 |
break_innermost_match; split_andb; Z.ltb_to_lt; Z.rewrite_mod_small; reflexivity. |
| 175 |
Qed. |
| 176 |
|
| 177 |
Lemma cast_normalize r v : cast (ZRange.normalize r) v = cast r v. |
| 178 |
Proof. |
| 179 |
cbv [cast]; rewrite ZRange.normalize_idempotent; reflexivity. |
| 180 |
Qed. |
| 181 |
End with_cast. |
| 182 |
|
| 183 |
Lemma cast_idempotent_gen |
| 184 |
r1 r2 v |
| 185 |
: is_tighter_than_bool (ZRange.normalize r1) (ZRange.normalize r2) = true |
| 186 |
-> ident.cast r2 (ident.cast r1 v) |
| 187 |
= ident.cast r1 v. |
| 188 |
Proof. |
| 189 |
intro H; apply (@cast_in_normalized_bounds r2). |
| 190 |
eapply ZRange.is_bounded_by_of_is_tighter_than, cast_always_bounded; assumption. |
| 191 |
Qed. |
| 192 |
|
| 193 |
Lemma cast_idempotent |
| 194 |
r v |
| 195 |
: ident.cast r (ident.cast r v) |
| 196 |
= ident.cast r v. |
| 197 |
Proof. |
| 198 |
apply cast_idempotent_gen; change (is_true (is_tighter_than_bool (ZRange.normalize r) (ZRange.normalize r))); reflexivity. |
| 199 |
Qed. |
| 200 |
End ident. |