Debian Codegraph @

src/Util/ZRange/BasicLemmas.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import RelationClasses.
3 From Coq Require Import Lia.
4 Require Import Crypto.Util.ZRange.
5 Require Import Crypto.Util.ZRange.Operations.
6 Require Import Crypto.Util.ZUtil.Hints.ZArith.
7 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
8 Require Import Crypto.Util.ZUtil.Tactics.SplitMinMax.
9 Require Import Crypto.Util.Tactics.SpecializeBy.
10 Require Import Crypto.Util.Tactics.SpecializeAllWays.
11 Require Import Crypto.Util.Tactics.BreakMatch.
12 Require Import Crypto.Util.Notations.
13 Require Import Crypto.Util.Tactics.DestructHead.
14
15 Module ZRange.
16 Import Operations.ZRange.
17 Local Open Scope zrange_scope.
18 Local Coercion is_true : bool >-> Sortclass.
19
20 Local Notation eta v := r[ lower v ~> upper v ].
21
22 Local Ltac t_step :=
23 first [ reflexivity
24 | progress destruct_head' zrange
25 | progress cbv -[Z.min Z.max Z.le Z.lt Z.ge Z.gt andb Z.leb Z.geb] in *
26 | progress split_min_max
27 | match goal with
28 | [ |- _ = _ :> zrange ] => apply f_equal2
29 end
30 | lia
31 | solve [ auto ] ].
32 Local Ltac t := repeat t_step.
33
34 Lemma flip_flip v : flip (flip v) = v.
35 Proof. destruct v; reflexivity. Qed.
36
37 Lemma normalize_flip v : normalize (flip v) = normalize v.
38 Proof. t. Qed.
39
40 Lemma normalize_idempotent v : normalize (normalize v) = normalize v.
41 Proof. t. Qed.
42
43 Definition normalize_or v : normalize v = v \/ normalize v = flip v.
44 Proof. t. Qed.
45
46 Lemma union_comm r1 r2 : union r1 r2 = union r2 r1.
47 Proof. t. Qed.
48
49 Lemma normalize_union_normalize r1 r2 : normalize (union (normalize r1) (normalize r2)) = union (normalize r1) (normalize r2).
50 Proof. t. Qed.
51
52 Local Ltac t2_step :=
53 first [ t_step
54 | rewrite !Bool.andb_true_iff
55 | rewrite !Z.leb_le
56 | rewrite !Z.leb_gt
57 | rewrite Bool.andb_true_iff in *
58 | rewrite Z.leb_le in *
59 | rewrite Z.leb_gt in *
60 | progress intros
61 | match goal with
62 | [ H : context[andb _ _ = true] |- _ ] => setoid_rewrite Bool.andb_true_iff in H
63 | [ |- context[andb _ _ = true] ] => setoid_rewrite Bool.andb_true_iff
64 | [ H : context[Z.leb _ _ = true] |- _ ] => setoid_rewrite Z.leb_le in H
65 | [ |- context[Z.leb _ _ = true] ] => setoid_rewrite Z.leb_le
66 | [ H : context[Z.leb _ _ = false] |- _ ] => setoid_rewrite Z.leb_gt in H
67 | [ |- context[Z.leb _ _ = false] ] => setoid_rewrite Z.leb_gt
68 | [ |- and _ _ ] => apply conj
69 | [ |- or _ (true = false) ] => left
70 | [ |- or _ (false = true) ] => left
71 | [ |- or _ (?x = ?x) ] => right
72 | [ H : true = false |- _ ] => exfalso; clear -H; discriminate
73 | [ H : Build_zrange _ _ = Build_zrange _ _ |- _ ] => inversion H; clear H
74 end
75 | progress specialize_by lia
76 | progress destruct_head'_or ].
77
78 Lemma goodb_normalize r : goodb (normalize r) = true.
79 Proof. repeat t2_step. Qed.
80
81 Lemma normalize_id_iff_goodb {r} : goodb r = true <-> normalize r = r.
82 Proof. repeat t2_step. Qed.
83
84 Lemma normalize_id_pow2 (k : Z) (k_nonneg : (0 <= k)%Z)
85 : normalize r[0 ~> 2 ^ k - 1] = r[0 ~> 2 ^ k - 1].
86 Proof.
87 assert (0 < 2 ^ k)%Z by auto with zarith.
88 assert (0 <= 2 ^ k - 1)%Z by lia.
89 repeat t2_step.
90 Qed.
91
92 Lemma is_tighter_than_bool_extend_land_lor_bounds r : is_tighter_than_bool r (extend_land_lor_bounds r) = true.
93 Proof. repeat t2_step. Qed.
94
95 Lemma is_bounded_by_iff_is_tighter_than r1 r2
96 : (forall v, is_bounded_by_bool v r1 = true -> is_bounded_by_bool v r2 = true)
97 <-> (is_tighter_than_bool r1 r2 = true \/ goodb r1 = false).
98 Proof. repeat t2_step; specialize_all_ways; repeat t2_step. Qed.
99
100 Lemma is_bounded_by_iff_is_tighter_than_good r1 r2 (Hgood : goodb r1 = true)
101 : (forall v, is_bounded_by_bool v r1 = true -> is_bounded_by_bool v r2 = true)
102 <-> (is_tighter_than_bool r1 r2 = true).
103 Proof. rewrite is_bounded_by_iff_is_tighter_than; intuition (congruence || auto). Qed.
104
105 Global Instance is_tighter_than_bool_Reflexive : Reflexive is_tighter_than_bool.
106 Proof.
107 cbv [is_tighter_than_bool is_true]; cbn [lower upper]; repeat intro.
108 rewrite ?Bool.andb_true_iff, ?Z.leb_le_iff in *; repeat apply conj; reflexivity.
109 Qed.
110 Global Instance is_tighter_than_bool_Transitive : Transitive is_tighter_than_bool.
111 Proof.
112 cbv [is_tighter_than_bool is_true]; cbn [lower upper]; repeat intro.
113 rewrite ?Bool.andb_true_iff, ?Z.leb_le_iff in *; repeat apply conj; etransitivity; destruct_head'_and; eassumption.
114 Qed.
115
116 Lemma is_bounded_by_normalize r
117 : forall v, is_bounded_by_bool v r = true -> is_bounded_by_bool v (normalize r) = true.
118 Proof.
119 apply <- is_bounded_by_iff_is_tighter_than; destruct (goodb r) eqn:?; [ left | right; reflexivity ].
120 rewrite (proj1 normalize_id_iff_goodb) by assumption; change (is_true (is_tighter_than_bool r r)); reflexivity.
121 Qed.
122
123 Lemma goodb_of_is_bounded_by_bool v r : is_bounded_by_bool v r = true -> goodb r = true.
124 Proof. repeat t2_step. Qed.
125
126 Lemma goodb_of_is_tighter_than_bool_goodb r1 r2 : is_tighter_than_bool r1 r2 = true -> goodb r1 = true -> goodb r2 = true.
127 Proof. repeat t2_step. Qed.
128
129 Lemma goodb_of_is_tighter_than_bool_normalize r1 r2 : is_tighter_than_bool (normalize r1) r2 = true -> goodb r2 = true.
130 Proof. repeat t2_step. Qed.
131
132 Lemma is_tighter_than_bool_normalize_of_goodb r : goodb r = true -> is_tighter_than_bool r (normalize r) = true.
133 Proof. repeat t2_step. Qed.
134
135 Lemma normalize_is_tighter_than_bool_of_goodb r : goodb r = true -> is_tighter_than_bool (normalize r) r = true.
136 Proof. repeat t2_step. Qed.
137
138 Lemma is_bounded_by_of_is_tighter_than r1 r2 (H : is_tighter_than_bool r1 r2 = true)
139 : (forall v, is_bounded_by_bool v r1 = true -> is_bounded_by_bool v r2 = true).
140 Proof. apply is_bounded_by_iff_is_tighter_than; auto. Qed.
141
142 Lemma is_bounded_by_bool_Proper_if_sumbool_union_dep {A B} (b : sumbool A B) x y rx ry
143 : (A -> is_bounded_by_bool x rx = true)
144 -> (B -> is_bounded_by_bool y ry = true)
145 -> is_bounded_by_bool (if b then x else y) (Operations.ZRange.union rx ry) = true.
146 Proof.
147 destruct b; cbv [Operations.ZRange.union is_bounded_by_bool];
148 intros; specialize_by_assumption; Bool.split_andb; rewrite Bool.andb_true_iff; split; Z.ltb_to_lt; cbn [lower upper] in *; split_min_max.
149 all: lia.
150 Qed.
151
152 Lemma is_bounded_by_bool_Proper_if_sumbool_union {A B} (b : sumbool A B) x y rx ry
153 : is_bounded_by_bool x rx = true
154 -> is_bounded_by_bool y ry = true
155 -> is_bounded_by_bool (if b then x else y) (Operations.ZRange.union rx ry) = true.
156 Proof. intros; apply is_bounded_by_bool_Proper_if_sumbool_union_dep; auto. Qed.
157
158 Lemma is_bounded_by_bool_Proper_if_bool_union_dep (b : bool) x y rx ry
159 : (b = true -> is_bounded_by_bool x rx = true)
160 -> (b = false -> is_bounded_by_bool y ry = true)
161 -> is_bounded_by_bool (if b then x else y) (Operations.ZRange.union rx ry) = true.
162 Proof. pose proof (is_bounded_by_bool_Proper_if_sumbool_union_dep (Sumbool.sumbool_of_bool b) x y rx ry); destruct b; assumption. Qed.
163
164 Lemma is_bounded_by_bool_Proper_if_bool_union (b : bool) x y rx ry
165 : is_bounded_by_bool x rx = true
166 -> is_bounded_by_bool y ry = true
167 -> is_bounded_by_bool (if b then x else y) (Operations.ZRange.union rx ry) = true.
168 Proof. intros; apply is_bounded_by_bool_Proper_if_bool_union_dep; auto. Qed.
169
170 Lemma is_bounded_by_bool_opp x r : is_bounded_by_bool (Z.opp x) (ZRange.opp r) = is_bounded_by_bool x r.
171 Proof.
172 cbv [is_bounded_by_bool andb opp]; cbn [lower upper]; break_match; Z.ltb_to_lt; break_match; Z.ltb_to_lt;
173 try (symmetry; progress Z.ltb_to_lt);
174 try reflexivity;
175 try lia.
176 Qed.
177
178 Lemma normalize_opp r : normalize (-r) = -(normalize r).
179 Proof.
180 cbv [normalize ZRange.opp]; cbn [lower upper].
181 rewrite <- !Z.opp_min_distr, <- !Z.opp_max_distr.
182 reflexivity.
183 Qed.
184
185 Lemma opp_involutive r : - - r = r.
186 Proof.
187 cbv [opp upper lower]; destruct r; rewrite !Z.opp_involutive; reflexivity.
188 Qed.
189
190 Lemma normalize_constant v : normalize (constant v) = constant v.
191 Proof. repeat t2_step. Qed.
192
193 Lemma is_bounded_by_bool_move_opp_normalize r v
194 : is_bounded_by_bool v (ZRange.normalize (-r))
195 = is_bounded_by_bool (-v) (ZRange.normalize r).
196 Proof.
197 rewrite <- is_bounded_by_bool_opp at 1.
198 rewrite normalize_opp, opp_involutive; reflexivity.
199 Qed.
200
201 Lemma is_tighter_than_bool_constant r v
202 : is_tighter_than_bool (ZRange.constant v) r
203 = is_bounded_by_bool v r.
204 Proof. reflexivity. Qed.
205
206 Lemma is_bounded_by_bool_constant v
207 : is_bounded_by_bool v (ZRange.constant v) = true.
208 Proof. repeat t2_step. Qed.
209
210 Lemma is_bounded_by_bool_normalize_constant v
211 : is_bounded_by_bool v (ZRange.normalize (ZRange.constant v)) = true.
212 Proof. repeat t2_step. Qed.
213
214 Lemma is_bounded_by_bool_constant_iff v1 v2
215 : is_bounded_by_bool v1 (ZRange.constant v2) = true <-> v1 = v2.
216 Proof. repeat t2_step. Qed.
217
218 Lemma is_bounded_by_bool_normalize_constant_iff v1 v2
219 : is_bounded_by_bool v1 (ZRange.normalize (ZRange.constant v2)) = true <-> v1 = v2.
220 Proof. repeat t2_step. Qed.
221
222 Lemma is_tighter_than_bool_union_l r1 r2 : is_tighter_than_bool r1 (union r1 r2).
223 Proof. repeat t2_step. Qed.
224
225 Lemma is_tighter_than_bool_union_r r1 r2 : is_tighter_than_bool r2 (union r1 r2).
226 Proof. repeat t2_step. Qed.
227
228 Lemma is_bounded_by_bool_union_l r1 r2 z : is_bounded_by_bool z r1 -> is_bounded_by_bool z (union r1 r2).
229 Proof. repeat t2_step. Qed.
230
231 Lemma is_bounded_by_bool_union_r r1 r2 z : is_bounded_by_bool z r2 -> is_bounded_by_bool z (union r1 r2).
232 Proof. repeat t2_step. Qed.
233
234 Section goodb.
235 Local Open Scope Z_scope.
236 Local Open Scope zrange_scope.
237
238 Lemma goodb_constant v : goodb (constant v) = true.
239 Proof. repeat t2_step. Qed.
240
241 Lemma goodb_apply_to_range f r
242 : goodb (f (lower r)) = true -> goodb (f (upper r)) = true -> goodb (apply_to_range f r) = true.
243 Proof. repeat t2_step. Qed.
244
245 Lemma goodb_union r1 r2
246 : goodb r1 = true -> goodb r2 = true -> goodb (union r1 r2) = true.
247 Proof. repeat t2_step. Qed.
248
249 Lemma goodb_apply_to_split_range f r
250 : (lower r < 0 -> upper r <= -1 -> goodb (f r) = true)
251 -> (lower r < 0 -> -1 < upper r -> goodb (f r[lower r ~> -1]) = true)
252 -> (lower r <= 0 -> 0 <= upper r -> goodb (f r[0 ~> 0]) = true)
253 -> (lower r < 1 -> 0 < upper r -> goodb (f r[1 ~> upper r]) = true)
254 -> (1 <= lower r -> 0 < upper r -> goodb (f r) = true)
255 -> goodb r = true \/ goodb (f r) = true
256 -> goodb (apply_to_split_range f r) = true.
257 Proof.
258 cbv [apply_to_split_range split_range_at_0 lower upper orb andb]; break_innermost_match; Z.ltb_to_lt; intros.
259 all: repeat first [ assumption
260 | match goal with
261 | [ H : goodb r[?l ~> ?u] = true |- _ ]
262 => let H' := fresh in
263 assert (H' : u < l) by lia; exfalso; clear -H H'; cbv [goodb ZRange.lower ZRange.upper] in H
264 end
265 | lia
266 | apply goodb_union
267 | progress specialize_by lia
268 | progress split_min_max
269 | progress destruct_head'_or
270 | progress Z.ltb_to_lt ].
271 Qed.
272
273 Local Ltac corners_t :=
274 repeat ((apply goodb_apply_to_split_range + apply goodb_apply_to_range + apply goodb_constant); intros);
275 try (constructor; solve [ corners_t ]).
276
277 Lemma goodb_two_corners f r : goodb (two_corners f r) = true.
278 Proof. corners_t. Qed.
279
280 Lemma goodb_four_corners f r1 r2 : goodb (four_corners f r1 r2) = true.
281 Proof. corners_t. Qed.
282
283 Lemma goodb_eight_corners f r1 r2 r3 : goodb (eight_corners f r1 r2 r3) = true.
284 Proof. corners_t. Qed.
285
286 Lemma goodb_two_corners_and_zero f r1 : goodb (two_corners_and_zero f r1) = true.
287 Proof. corners_t. Qed.
288
289 Lemma goodb_four_corners_and_zero f r1 r2 : goodb (four_corners_and_zero f r1 r2) = true.
290 Proof. corners_t. Qed.
291
292 Lemma goodb_eight_corners_and_zero f r1 r2 r3 : goodb (eight_corners_and_zero f r1 r2 r3) = true.
293 Proof. corners_t. Qed.
294
295 Lemma goodb_log2 r : goodb (ZRange.log2 r) = true.
296 Proof. apply goodb_two_corners. Qed.
297 Lemma goodb_log2_up r : goodb (ZRange.log2_up r) = true.
298 Proof. apply goodb_two_corners. Qed.
299 Lemma goodb_add r1 r2 : goodb (ZRange.add r1 r2) = true.
300 Proof. apply goodb_four_corners. Qed.
301 Lemma goodb_sub r1 r2 : goodb (ZRange.sub r1 r2) = true.
302 Proof. apply goodb_four_corners. Qed.
303 Lemma goodb_mul r1 r2 : goodb (ZRange.mul r1 r2) = true.
304 Proof. apply goodb_four_corners. Qed.
305 Lemma goodb_div r1 r2 : goodb (ZRange.div r1 r2) = true.
306 Proof. apply goodb_four_corners_and_zero. Qed.
307 Lemma goodb_shiftr r1 r2 : goodb (ZRange.shiftr r1 r2) = true.
308 Proof. apply goodb_four_corners_and_zero. Qed.
309 Lemma goodb_shiftl r1 r2 : goodb (ZRange.shiftl r1 r2) = true.
310 Proof. apply goodb_four_corners_and_zero. Qed.
311 Lemma goodb_land r1 r2 : goodb (ZRange.land r1 r2) = true.
312 Proof.
313 cbv [goodb land].
314 match goal with
315 |- context [2 ^ ?n] =>
316 assert (0 <= 2 ^ n) by (apply Z.pow_nonneg; lia)
317 end.
318 break_innermost_match;
319 repeat match goal with
320 | H : (_ && _)%bool = true |- _ =>
321 apply Bool.andb_true_iff in H; destruct H
322 | H : (_ && _)%bool = false |- _ =>
323 apply Bool.andb_false_iff in H; destruct H
324 end;
325 Z.ltb_to_lt; cbn [upper lower].
326 all:lia.
327 Qed.
328 Lemma goodb_lor r1 r2 : goodb (ZRange.lor r1 r2) = true.
329 Proof. apply goodb_four_corners_and_zero. Qed.
330 Lemma goodb_cc_m s r : goodb (ZRange.cc_m s r) = true.
331 Proof. apply goodb_two_corners. Qed.
332 End goodb.
333
334 Section normalize.
335 Lemma normalize_two_corners f r : normalize (two_corners f r) = two_corners f r.
336 Proof. now rewrite (proj1 normalize_id_iff_goodb) by apply goodb_two_corners. Qed.
337
338 Lemma normalize_four_corners f r1 r2 : normalize (four_corners f r1 r2) = four_corners f r1 r2.
339 Proof. now rewrite (proj1 normalize_id_iff_goodb) by apply goodb_four_corners. Qed.
340
341 Lemma normalize_eight_corners f r1 r2 r3 : normalize (eight_corners f r1 r2 r3) = eight_corners f r1 r2 r3.
342 Proof. now rewrite (proj1 normalize_id_iff_goodb) by apply goodb_eight_corners. Qed.
343
344 Lemma normalize_two_corners_and_zero f r1 : normalize (two_corners_and_zero f r1) = two_corners_and_zero f r1.
345 Proof. now rewrite (proj1 normalize_id_iff_goodb) by apply goodb_two_corners_and_zero. Qed.
346
347 Lemma normalize_four_corners_and_zero f r1 r2 : normalize (four_corners_and_zero f r1 r2) = four_corners_and_zero f r1 r2.
348 Proof. now rewrite (proj1 normalize_id_iff_goodb) by apply goodb_four_corners_and_zero. Qed.
349
350 Lemma normalize_eight_corners_and_zero f r1 r2 r3 : normalize (eight_corners_and_zero f r1 r2 r3) = eight_corners_and_zero f r1 r2 r3.
351 Proof. now rewrite (proj1 normalize_id_iff_goodb) by apply goodb_eight_corners_and_zero. Qed.
352
353 Lemma normalize_log2 r : normalize (ZRange.log2 r) = ZRange.log2 r.
354 Proof. apply normalize_two_corners. Qed.
355 Lemma normalize_log2_up r : normalize (ZRange.log2_up r) = ZRange.log2_up r.
356 Proof. apply normalize_two_corners. Qed.
357 Lemma normalize_add r1 r2 : normalize (ZRange.add r1 r2) = ZRange.add r1 r2.
358 Proof. apply normalize_four_corners. Qed.
359 Lemma normalize_sub r1 r2 : normalize (ZRange.sub r1 r2) = ZRange.sub r1 r2.
360 Proof. apply normalize_four_corners. Qed.
361 Lemma normalize_mul r1 r2 : normalize (ZRange.mul r1 r2) = ZRange.mul r1 r2.
362 Proof. apply normalize_four_corners. Qed.
363 Lemma normalize_div r1 r2 : normalize (ZRange.div r1 r2) = ZRange.div r1 r2.
364 Proof. apply normalize_four_corners_and_zero. Qed.
365 Lemma normalize_shiftr r1 r2 : normalize (ZRange.shiftr r1 r2) = ZRange.shiftr r1 r2.
366 Proof. apply normalize_four_corners_and_zero. Qed.
367 Lemma normalize_shiftl r1 r2 : normalize (ZRange.shiftl r1 r2) = ZRange.shiftl r1 r2.
368 Proof. apply normalize_four_corners_and_zero. Qed.
369 Lemma normalize_land r1 r2 : normalize (ZRange.land r1 r2) = ZRange.land r1 r2.
370 Proof. apply normalize_id_iff_goodb, goodb_land. Qed.
371 Lemma normalize_lor r1 r2 : normalize (ZRange.lor r1 r2) = ZRange.lor r1 r2.
372 Proof. apply normalize_four_corners_and_zero. Qed.
373 Lemma normalize_cc_m s r : normalize (ZRange.cc_m s r) = ZRange.cc_m s r.
374 Proof. apply normalize_two_corners. Qed.
375 End normalize.
376 End ZRange.