Debian Codegraph @

src/Bedrock/Field/Common/Util.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import String.
3 From Coq Require Import Lia.
4 From Coq Require Import Morphisms.
5 From Coq Require Import RelationClasses.
6 Require Import bedrock2.Array.
7 Require Import bedrock2.Scalars.
8 Require Import bedrock2.Syntax.
9 Require Import bedrock2.Map.Separation.
10 Require Import bedrock2.Map.SeparationLogic.
11 Require Import bedrock2.WeakestPrecondition.
12 Require Import bedrock2.WeakestPreconditionProperties.
13 Require Import coqutil.Tactics.destr.
14 Require Import coqutil.Map.Interface coqutil.Map.Properties.
15 Require Import coqutil.Word.Interface coqutil.Word.Properties.
16 Require Import coqutil.Word.LittleEndianList.
17 Require Import coqutil.Datatypes.PropSet.
18 Require Import coqutil.Datatypes.List.
19 Require Import Crypto.AbstractInterpretation.AbstractInterpretation.
20 Require Import Crypto.COperationSpecifications.
21 Require Import Crypto.Util.Option.
22 Require Import Crypto.Util.ListUtil.
23 Require Import Crypto.Util.ZUtil.Modulo.
24 Require Import Crypto.Util.ZUtil.Tactics.LtbToLt.
25 From Coq Require Import List. (* after SeparationLogic *)
26 Import ListNotations.
27
28 Import AbstractInterpretation.Compilers.
29
30 Section Maps.
31 Local Hint Mode map.map - - : typeclass_instances.
32 Context {key value} {key_eqb}
33 {map : map.map key value}
34 {key_eq_dec :
35 forall x y : key,
36 BoolSpec (x = y) (x <> y) (key_eqb x y)}
37 {map_ok : map.ok map}.
38
39 Lemma only_differ_empty m :
40 map.only_differ m PropSet.empty_set m.
41 Proof. right; reflexivity. Qed.
42
43 Lemma only_differ_sym m1 m2 ks :
44 map.only_differ m1 ks m2 ->
45 map.only_differ m2 ks m1.
46 Proof. firstorder congruence. Qed.
47
48 Lemma only_differ_trans m1 m2 m3 ks1 ks2 :
49 map.only_differ m2 ks1 m1 ->
50 map.only_differ m3 ks2 m2 ->
51 map.only_differ m3 (union ks1 ks2) m1.
52 Proof. firstorder congruence. Qed.
53
54 Global Instance Proper_only_differ :
55 Proper (eq ==> sameset ==> eq ==> iff) map.only_differ.
56 Proof. firstorder congruence. Qed.
57
58 Global Instance Proper_undef_on :
59 Proper (eq ==> sameset ==> iff) map.undef_on.
60 Proof. firstorder congruence. Qed.
61
62 Lemma only_differ_put m k v :
63 map.only_differ (map.put m k v) (singleton_set k) m.
64 Proof.
65 cbv [map.only_differ elem_of singleton_set].
66 intro x; destr (key_eqb k x); subst;
67 rewrite ?map.get_put_same, ?map.get_put_diff by congruence;
68 tauto.
69 Qed.
70
71 Lemma putmany_of_list_zip_app_l m ks1 ks2 vs :
72 map.putmany_of_list_zip (ks1 ++ ks2) vs m =
73 Option.bind
74 (map.putmany_of_list_zip
75 ks1 (List.firstn (Datatypes.length ks1) vs) m)
76 (map.putmany_of_list_zip
77 ks2 (List.skipn (Datatypes.length ks1) vs)).
78 Proof.
79 revert m ks2 vs; induction ks1; destruct vs;
80 rewrite ?app_nil_l, <-?app_comm_cons;
81 try reflexivity; [ ].
82 apply IHks1.
83 Qed.
84
85 Lemma putmany_of_list_zip_None ks vs m :
86 map.putmany_of_list_zip ks vs m = None <->
87 length ks <> length vs.
88 Proof.
89 revert m vs; induction ks; destruct vs;
90 cbn [map.putmany_of_list_zip length];
91 split; intros; try congruence;
92 match goal with
93 | H : _ |- _ => apply IHks in H; lia
94 | _ => apply IHks; lia
95 end.
96 Qed.
97
98 Lemma putmany_of_list_zip_combine m ks vs :
99 length ks = length vs ->
100 map.putmany_of_list_zip ks vs m =
101 Some (map.putmany_of_list (combine ks vs) m).
102 Proof.
103 revert m vs; induction ks; destruct vs; intros;
104 cbn [length] in *; try lia; try reflexivity; [ ].
105 cbn [map.putmany_of_list_zip map.putmany_of_list combine].
106 rewrite IHks; auto.
107 Qed.
108
109 Lemma put_put_comm k1 k2 v1 v2 m :
110 k1 <> k2 ->
111 map.put (map.put m k1 v1) k2 v2 =
112 map.put (map.put m k2 v2) k1 v1.
113 Proof.
114 intros.
115 apply map.map_ext; intro k;
116 destruct (key_eq_dec k1 k);
117 destruct (key_eq_dec k2 k);
118 subst; try congruence;
119 repeat first [ rewrite map.get_put_same
120 | rewrite map.get_put_diff by congruence
121 | reflexivity ].
122 Qed.
123
124 Lemma putmany_of_list_put_comm kv k v m :
125 ~ In k (List.map fst kv) ->
126 map.putmany_of_list kv (map.put m k v) =
127 map.put (map.putmany_of_list kv m) k v.
128 Proof.
129 revert m k v; induction kv; intros;
130 [ reflexivity | ].
131 cbn [map.putmany_of_list]. break_match.
132 cbn [List.map In fst] in *.
133 match goal with H : _ |- _ =>
134 apply Decidable.not_or in H; destruct H end.
135 rewrite <-IHkv by auto.
136 rewrite put_put_comm by congruence.
137 reflexivity.
138 Qed.
139
140 Lemma putmany_of_list_comm kv1 kv2 m :
141 NoDup (List.map fst (kv1 ++ kv2)) ->
142 map.putmany_of_list kv1 (map.putmany_of_list kv2 m) =
143 map.putmany_of_list kv2 (map.putmany_of_list kv1 m).
144 Proof.
145 revert kv2 m; induction kv1; destruct kv2; intros;
146 try reflexivity; [ ].
147 rewrite map_app in *. cbn [List.map] in *.
148 cbn [map.putmany_of_list]. break_match.
149 repeat match goal with
150 | H : NoDup (_ ++ _) |- _ =>
151 apply NoDup_app_iff in H
152 | H : _ /\ _ |- _ => destruct H
153 | H : NoDup (_ :: _) |- _ => inversion H; subst; clear H
154 end.
155 cbn [In fst] in *.
156 rewrite !putmany_of_list_put_comm by firstorder idtac.
157 rewrite put_put_comm by firstorder idtac.
158 rewrite IHkv1; [ reflexivity | ].
159 rewrite map_app. apply NoDup_app_iff.
160 firstorder idtac.
161 Qed.
162
163 Lemma putmany_of_list_zip_bind_comm ks1 ks2 vs1 vs2 m :
164 NoDup (ks1 ++ ks2) ->
165 Option.bind
166 (map.putmany_of_list_zip ks1 vs1 m)
167 (map.putmany_of_list_zip ks2 vs2) =
168 Option.bind
169 (map.putmany_of_list_zip ks2 vs2 m)
170 (map.putmany_of_list_zip ks1 vs1).
171 Proof.
172 intros.
173 destruct (Nat.eq_dec (length ks1) (length vs1));
174 destruct (Nat.eq_dec (length ks2) (length vs2));
175 try solve [
176 cbv [Option.bind]; break_match; try reflexivity;
177 repeat match goal with
178 | H : _ = Some _ |- _ =>
179 apply map.putmany_of_list_zip_sameLength in H
180 | H : _ = None |- _ =>
181 apply putmany_of_list_zip_None in H
182 | _ => apply putmany_of_list_zip_None
183 | _ => symmetry; apply putmany_of_list_zip_None
184 | _ => congruence
185 end ]; [ ].
186 repeat match goal with
187 | _ => rewrite putmany_of_list_zip_combine by auto
188 | _ => progress cbn [Option.bind]
189 end.
190 rewrite <-putmany_of_list_comm; [ reflexivity | ].
191 rewrite <-combine_app_samelength by congruence.
192 rewrite map_fst_combine.
193 rewrite firstn_all2 by (rewrite !app_length; lia).
194 assumption.
195 Qed.
196
197 Lemma getmany_of_tuple_empty sz keys :
198 sz <> 0%nat ->
199 map.getmany_of_tuple (sz:=sz) map.empty keys = None.
200 Proof.
201 destruct sz; try congruence.
202 cbn; intros. rewrite map.get_empty. reflexivity.
203 Qed.
204
205 Lemma undef_on_None m k ks :
206 map.undef_on m ks ->
207 elem_of k ks ->
208 map.get m k = None.
209 Proof.
210 intros.
211 match goal with H : map.undef_on _ _ |- _ =>
212 specialize (H _ ltac:(eassumption));
213 rewrite map.get_empty in H
214 end.
215 congruence.
216 Qed.
217
218 Lemma undef_on_empty ks :
219 map.undef_on map.empty ks.
220 Proof. firstorder idtac. Qed.
221
222 Lemma get_only_differ_undef m1 m2 ks k v :
223 map.only_differ m1 ks m2 ->
224 map.undef_on m1 ks ->
225 map.get m1 k = Some v ->
226 map.get m2 k = Some v.
227 Proof.
228 repeat match goal with
229 | _ => progress intros
230 | H : map.only_differ _ _ _ |- _ =>
231 specialize (H k); destruct H
232 | H1 : map.undef_on _ ?ks, H2 : elem_of ?k ?ks |- _ =>
233 eapply undef_on_None in H2; [ | eassumption .. ]
234 | _ => congruence
235 end.
236 Qed.
237
238 Lemma only_differ_union_undef_l m1 m2 k1 k2 :
239 map.only_differ m1 (union k1 k2) m2 ->
240 map.undef_on m1 k2 ->
241 map.undef_on m2 k2 ->
242 map.only_differ m1 k1 m2.
243 Proof.
244 intros.
245 match goal with H : map.only_differ _ _ _ |- map.only_differ _ _ _ =>
246 let x := fresh "x" in intro x; specialize (H x);
247 destruct H
248 end; [ | tauto ].
249 match goal with H : elem_of _ (union _ _) |- _ =>
250 destruct H end;
251 erewrite ?undef_on_None by eauto; tauto.
252 Qed.
253
254 Lemma undef_on_subset m k1 k2 :
255 subset k1 k2 ->
256 map.undef_on m k2 ->
257 map.undef_on m k1.
258 Proof. firstorder congruence. Qed.
259
260 Lemma only_differ_disjoint_undef_on m1 m2 ks s :
261 map.only_differ m1 ks m2 ->
262 disjoint ks s ->
263 map.undef_on m1 s ->
264 map.undef_on m2 s.
265 Proof. firstorder congruence. Qed.
266
267 Lemma undef_on_union_iff m k1 k2 :
268 map.undef_on m (union k1 k2) <->
269 (map.undef_on m k1 /\ map.undef_on m k2).
270 Proof.
271 cbv [map.undef_on map.agree_on union elem_of].
272 repeat split; intros; destruct_head'_or; destruct_head'_and; eauto.
273 Qed.
274
275 Lemma put_undef_on k v m s :
276 map.undef_on m s ->
277 ~ s k ->
278 map.undef_on (map.put m k v) s.
279 Proof.
280 cbv [map.undef_on map.agree_on elem_of].
281 intros. rewrite map.get_empty.
282 match goal with H : context [map.empty] |- _ =>
283 setoid_rewrite map.get_empty in H end.
284 match goal with H1 : ~ ?s ?k1, H2 : ?s ?k2 |- _ =>
285 destruct (key_eq_dec k1 k2); subst;
286 [ tauto | ]
287 end.
288 rewrite map.get_put_diff by congruence. eauto.
289 Qed.
290
291 Lemma of_list_zip_app ks1 ks2 vs1 vs2 :
292 length ks1 = length vs1 ->
293 length ks2 = length vs2 ->
294 (exists l,
295 map.of_list_zip (ks1 ++ ks2) (vs1 ++ vs2) = Some l).
296 Proof.
297 intros. apply map.sameLength_putmany_of_list.
298 rewrite !app_length; lia.
299 Qed.
300
301 Lemma putmany_of_list_zip_undef_on ks vs m1 m2 s :
302 map.putmany_of_list_zip ks vs m1 = Some m2 ->
303 disjoint (of_list ks) s ->
304 map.undef_on m1 s ->
305 map.undef_on m2 s.
306 Proof.
307 revert vs m1 m2 s; induction ks; destruct vs;
308 cbn [map.putmany_of_list_zip]; intros;
309 try match goal with H : Some _ = Some _ |- _ =>
310 inversion H; subst; clear H
311 end; try congruence; eauto; [ ].
312 match goal with H : _ |- _ =>
313 symmetry in H; apply disjoint_cons in H
314 end.
315 destruct_head'_and.
316 eapply IHks; eauto.
317 { symmetry; eauto. }
318 { apply put_undef_on; eauto.
319 eapply disjoint_singleton_r_iff; eauto. }
320 Qed.
321
322 Lemma of_list_zip_undef_on ks vs m s :
323 map.of_list_zip ks vs = Some m ->
324 disjoint (of_list ks) s ->
325 map.undef_on m s.
326 Proof.
327 cbv [map.of_list_zip]; intros.
328 eapply putmany_of_list_zip_undef_on; eauto.
329 cbv [map.undef_on map.agree_on]; reflexivity.
330 Qed.
331
332 Lemma only_differ_notin k ks m m' :
333 map.only_differ m ks m' ->
334 ~ ks k ->
335 map.get m' k = map.get m k.
336 Proof.
337 cbv [map.only_differ PropSet.elem_of].
338 let H := fresh in
339 intro H; specialize (H k); destruct H.
340 { tauto. }
341 { auto. }
342 Qed.
343 End Maps.
344
345 (* proofs about list_Z_bounded_by *)
346 Section ListZBoundedBy.
347 Lemma relax_list_Z_bounded_by r1 r2 x :
348 ZRange.type.option.is_tighter_than
349 (t:=type.base (base.type.list (base.type.type_base base.type.Z)))
350 (Some r1) (Some r2) = true ->
351 list_Z_bounded_by r1 x ->
352 list_Z_bounded_by r2 x.
353 Proof.
354 cbn in r1, r2 |- *. intros.
355 pose proof length_list_Z_bounded_by _ x ltac:(eassumption).
356 match goal with H : FoldBool.fold_andb_map _ _ _ = true |- _ =>
357 pose proof H;
358 apply FoldBool.fold_andb_map_length in H
359 end.
360 generalize dependent r1; generalize dependent r2.
361 generalize dependent x; induction x; cbn [length].
362 { destruct r2; cbn [length]; intros; [ | lia].
363 reflexivity. }
364 { destruct r1, r2; cbn [length]; intros; try lia; [ ].
365 cbv [list_Z_bounded_by] in *. cbn [FoldBool.fold_andb_map] in *.
366 repeat match goal with
367 | H : _ /\ _ |- _ => destruct H
368 | H : (_ && _)%bool = true |- _ =>
369 apply Bool.andb_true_iff in H
370 end.
371 apply Bool.andb_true_iff; split.
372 { break_innermost_match; [ | reflexivity].
373 break_innermost_match_hyps; [ | congruence ].
374 cbv [ZRange.is_tighter_than_bool] in *.
375 repeat match goal with
376 | H : _ /\ _ |- _ => destruct H
377 | H : (_ && _)%bool = true |- _ =>
378 apply Bool.andb_true_iff in H
379 end.
380 apply Bool.andb_true_iff; split; Z.ltb_to_lt; lia. }
381 { eapply IHx;
382 match goal with
383 | |- length _ = length _ =>
384 idtac (* no eassumption on length goals *)
385 | _ => try eassumption
386 end; lia. } }
387 Qed.
388
389 Lemma list_Z_bounded_by_Forall x r m :
390 list_Z_bounded_by (repeat (Some r) m) x ->
391 Forall (fun z : Z => ZRange.lower r <= z <= ZRange.upper r)%Z x.
392 Proof.
393 intros.
394 pose proof length_list_Z_bounded_by _ x ltac:(eassumption).
395 cbv [list_Z_bounded_by] in *.
396 rewrite repeat_length in *.
397 generalize dependent x.
398 generalize dependent m.
399 induction m; intros;
400 destruct x; intros; cbn [length] in *; subst;
401 try lia; [ | ]; constructor;
402 [ | apply IHm; [ | lia] ].
403 all: cbn [repeat FoldBool.fold_andb_map] in *.
404 all: repeat match goal with
405 | H : _ /\ _ |- _ => destruct H
406 | H : (_ && _)%bool = true |- _ =>
407 apply Bool.andb_true_iff in H
408 | _ => progress Z.ltb_to_lt
409 | _ => solve [auto]
410 | _ => lia
411 end.
412 Qed.
413
414 Lemma list_Z_bounded_by_cons b0 bs x0 xs:
415 list_Z_bounded_by (b0 :: bs) (x0 :: xs) <->
416 (match b0 with
417 | Some r =>
418 ZRange.is_bounded_by_bool x0 r = true
419 | None => True
420 end /\ list_Z_bounded_by bs xs).
421 Proof.
422 cbv [list_Z_bounded_by]. cbn [FoldBool.fold_andb_map].
423 cbv [ZRange.is_bounded_by_bool].
424 split; destruct b0; intros;
425 repeat match goal with
426 | H : (_ && _)%bool = true |- _ =>
427 apply Bool.andb_true_iff in H
428 | |- (_ && _)%bool = true => apply Bool.andb_true_iff
429 | H : _ /\ _ |- _ => destruct H
430 | |- _ /\ _ => split
431 | _ => assumption
432 | _ => tauto
433 end.
434 Qed.
435
436 Lemma list_Z_tighter_than_cons b1 b2 b1s b2s :
437 list_Z_tighter_than (b1 :: b1s) (b2 :: b2s) <->
438 (match b1 with
439 | Some r1 =>
440 match b2 with
441 | Some r2 =>
442 (ZRange.lower r2 <= ZRange.lower r1
443 /\ ZRange.upper r1 <= ZRange.upper r2)%Z
444 | None => True
445 end
446 | None => match b2 with
447 | Some _ => False
448 | None => True
449 end
450 end /\ list_Z_tighter_than b1s b2s).
451 Proof.
452 cbv [list_Z_tighter_than]. cbn [FoldBool.fold_andb_map].
453 split; destruct b1, b2; intros;
454 repeat match goal with
455 | H : (_ && _)%bool = true |- _ =>
456 apply Bool.andb_true_iff in H
457 | |- (_ && _)%bool = true => apply Bool.andb_true_iff
458 | H : _ /\ _ |- _ => destruct H
459 | |- _ /\ _ => split
460 | _ => progress Z.ltb_to_lt
461 | _ => assumption
462 | _ => tauto
463 | _ => congruence
464 end.
465 Qed.
466
467 Lemma list_Z_bounded_by_snoc b0 bs x0 xs:
468 list_Z_bounded_by (bs ++ [b0]) (xs ++ [x0]) <->
469 (match b0 with
470 | Some r =>
471 ZRange.is_bounded_by_bool x0 r = true
472 | None => True
473 end /\ list_Z_bounded_by bs xs).
474 Proof.
475 cbv [list_Z_bounded_by].
476 rewrite FoldBool.fold_andb_map_snoc.
477 cbv [ZRange.is_bounded_by_bool].
478 split; destruct b0; intros;
479 repeat match goal with
480 | H : (_ && _)%bool = true |- _ =>
481 apply Bool.andb_true_iff in H
482 | |- (_ && _)%bool = true => apply Bool.andb_true_iff
483 | H : _ /\ _ |- _ => destruct H
484 | |- _ /\ _ => split
485 | _ => assumption
486 | _ => tauto
487 end.
488 Qed.
489
490 Lemma relax_to_bounded_upperbounds bs upperbounds x :
491 list_Z_bounded_by
492 (map (fun v : Z => Some {| ZRange.lower := 0; ZRange.upper := v |})
493 upperbounds) x ->
494 Forall (fun r =>
495 match r with
496 | Some r => ZRange.lower r = 0%Z
497 | None => True end) bs ->
498 list_Z_bounded_by bs upperbounds ->
499 list_Z_bounded_by bs x.
500 Proof.
501 intros.
502 pose proof length_list_Z_bounded_by bs _ ltac:(eassumption).
503 pose proof length_list_Z_bounded_by _ x ltac:(eassumption).
504 rewrite map_length in *.
505 generalize dependent bs. generalize dependent upperbounds.
506 generalize dependent x.
507 induction x; destruct upperbounds; destruct bs;
508 cbn [map length] in *; try congruence; intros; [ ].
509 repeat match goal with
510 | _ => progress cbv [ZRange.is_bounded_by_bool] in *
511 | _ => progress cbn [ZRange.lower ZRange.upper] in *
512 | H : _ = 0 |- _ => rewrite H in *
513 | H : Forall _ (_ :: _) |- _ =>
514 inversion H; subst; clear H
515 | H : (_ && _)%bool = true |- _ =>
516 apply Bool.andb_true_iff in H
517 | H : list_Z_bounded_by (_ :: _) (_ :: _) |- _ =>
518 apply list_Z_bounded_by_cons in H; destruct H
519 | |- list_Z_bounded_by (_ :: _) (_ :: _) =>
520 apply list_Z_bounded_by_cons; split
521 | |- (_ && _)%bool = true => apply Bool.andb_true_iff
522 | H : _ /\ _ |- _ => destruct H
523 | |- _ /\ _ => split
524 | _ => progress break_match
525 | _ => solve [auto]
526 | _ => Z.ltb_to_lt; lia
527 | H : _ |- list_Z_bounded_by _ _ => eapply H; solve [eauto]
528 end.
529 Qed.
530
531 Lemma tighter_than_if_upper_bounded_by lo uppers bs :
532 list_Z_bounded_by bs uppers ->
533 Forall (fun b =>
534 exists r,
535 b = Some r /\ ZRange.lower r = lo) bs ->
536 list_Z_tighter_than
537 (map (fun v : Z => Some {| ZRange.lower := lo; ZRange.upper := v |})
538 uppers) bs.
539 Proof.
540 clear; revert bs; induction uppers; intros;
541 lazymatch goal with
542 | H : list_Z_bounded_by _ _ |- _ =>
543 pose proof H; apply length_list_Z_bounded_by in H
544 end; (destruct bs; cbn [length] in *; try auto with lia); [ ].
545 repeat lazymatch goal with
546 | H : list_Z_bounded_by (_::_) (_::_) |- _ =>
547 rewrite list_Z_bounded_by_cons in H
548 | |- list_Z_tighter_than (map _ (_ :: _)) (_ :: _) =>
549 cbn [map]; rewrite list_Z_tighter_than_cons
550 | H : ZRange.is_bounded_by_bool _ _ = true |- _ =>
551 apply Bool.andb_true_iff in H; destruct H;
552 Z.ltb_to_lt
553 | H : Forall _ (_ :: _) |- _ => inversion H; clear H; subst
554 | H : exists _, _ |- _ => destruct H; subst
555 | H : _ /\ _ |- _ => destruct H; subst
556 end.
557 progress cbn [ZRange.lower ZRange.upper].
558 split; auto with lia.
559 Qed.
560
561 Lemma partition_bounded_by_partition_ones x lg2s nb :
562 (0 <= x < 2 ^ lg2s)%Z ->
563 list_Z_bounded_by
564 (map (fun v : Z => Some {| ZRange.lower := 0; ZRange.upper := v |})
565 (Partition.Partition.partition
566 (ModOps.weight 8 1) nb (2 ^ lg2s - 1)))
567 (Partition.Partition.partition (ModOps.weight 8 1) nb x).
568 Proof.
569 clear.
570 induction nb; [ reflexivity | ].
571 rewrite !Partition.partition_step.
572 rewrite map_last, list_Z_bounded_by_snoc.
573 split; [ | solve [auto] ]. cbv [ZRange.is_bounded_by_bool].
574 cbn [ZRange.lower ZRange.upper].
575 apply Bool.andb_true_iff; split; Z.ltb_to_lt;
576 [ ZeroBounds.Z.zero_bounds;
577 solve [auto using Core.weight_positive, ModOps.wprops
578 with zarith] | ].
579 apply Z.div_le_mono;
580 [ solve [auto using Core.weight_positive, ModOps.wprops
581 with zarith] | ].
582 cbv [ModOps.weight]. rewrite Nat2Z.inj_succ.
583 autorewrite with zsimplify_fast. clear IHnb.
584 destruct (Z_lt_dec lg2s ((8 * Z.of_nat nb + 8)%Z));
585 [ assert (2 ^ lg2s < 2 ^ (8 * Z.of_nat nb + 8))%Z | ].
586 1:auto with zarith.
587 1:rewrite !Z.mod_small; auto with zarith.
588 replace (2 ^ lg2s - 1)%Z with (Z.ones lg2s)
589 by (rewrite Z.ones_equiv; auto with zarith).
590 rewrite Z.ones_mod_pow2 by auto with zarith.
591 lazymatch goal with
592 |- context [(x mod ?m)%Z] =>
593 pose proof Z.mod_pos_bound x m ltac:(auto with zarith)
594 end.
595 rewrite Z.ones_equiv; auto with zarith.
596 Qed.
597 End ListZBoundedBy.
598
599 Section Bytes.
600 Context {width : Z} {word : word width} {word_ok : word.ok word}.
601 Local Notation bytes_per_word := (Z.to_nat (Memory.bytes_per_word width)).
602
603 Definition eval_bytes bs := map le_combine (chunk bytes_per_word bs).
604
605 Definition encode_bytes (xs : list word) : list Byte.byte :=
606 flat_map (fun w => le_split bytes_per_word (word.unsigned w)) xs.
607
608 Lemma bytes_per_word_nz : bytes_per_word <> 0%nat.
609 Proof.
610 pose proof word.width_pos.
611 cbv [Memory.bytes_per]. change 0%nat with (Z.to_nat 0).
612 rewrite Z2Nat.inj_iff by (try apply Z.div_pos; lia).
613 apply not_eq_sym. apply Z.lt_neq, Z.div_str_pos.
614 lia.
615 Qed.
616
617 Lemma eval_bytes_length bs n :
618 length bs = (n * bytes_per_word)%nat ->
619 length (eval_bytes bs) = n.
620 Proof.
621 intro Hlength.
622 pose proof bytes_per_word_nz.
623 rewrite <-(Nat.div_mul n bytes_per_word) by trivial.
624 cbv [eval_bytes]; rewrite map_length, length_chunk by trivial.
625 rewrite Hlength, Nat.div_up_exact, Nat.div_mul; trivial.
626 Qed.
627
628 Lemma Forall_map_byte_unsigned x :
629 Forall (fun z : Z => 0 <= z < 2 ^ 8)%Z (map Byte.byte.unsigned x).
630 Proof.
631 induction x; cbn [length] in *; constructor; eauto; [ ].
632 apply Byte.byte.unsigned_range.
633 Qed.
634
635 Context {bits_per_word_eq_width : (Z.of_nat bytes_per_word * 8 = width)%Z}.
636
637 Lemma eval_bytes_range bs :
638 Forall (fun z : Z => (0 <= z < 2 ^ width)%Z) (eval_bytes bs).
639 Proof.
640 pose proof bytes_per_word_nz as Hnz.
641 apply Forall_map_iff, Forall_nth; intros.
642 split; try eapply Z.lt_le_trans; try apply le_combine_bound.
643 eapply Z.pow_le_mono_r; [cbv;trivial|].
644 rewrite length_chunk in H by trivial.
645 erewrite nth_error_nth in *; [|eapply nth_error_chunk; trivial].
646 rewrite firstn_length, skipn_length.
647
648 change 8%Z with (Z.of_nat 8).
649 rewrite <-Nat2Z.inj_mul, <-Nat.mul_min_distr_l, Nat2Z.inj_min, Nat.mul_comm.
650 rewrite Nat2Z.inj_mul.
651 setoid_rewrite bits_per_word_eq_width.
652 eapply Z.le_min_l.
653 Qed.
654 End Bytes.
655
656 Section Scalars.
657 Context
658 {width: Z} {BW: Bitwidth.Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}
659 {locals: map.map String.string word}
660 {env: map.map String.string (list String.string * list String.string * Syntax.cmd)}
661 {ext_spec: bedrock2.Semantics.ExtSpec}
662 {varname_gen : nat -> String.string}
663 {word_ok : word.ok word} {mem_ok : map.ok mem}
664 {locals_ok : map.ok locals}
665 {env_ok : map.ok env}
666 {ext_spec_ok : Semantics.ext_spec.ok ext_spec}.
667
668 Local Notation bytes_per_word :=
669 (@Memory.bytes_per_word width).
670
671 Lemma scalar_to_bytes (a x : word) :
672 Lift1Prop.iff1
673 (scalar a x)
674 (array ptsto (word.of_Z 1) a
675 (LittleEndianList.le_split (Z.to_nat bytes_per_word)
676 (word.unsigned x))).
677 Proof.
678 unfold scalar, truncated_word, truncated_scalar.
679 symmetry; eapply array1_iff_eq_of_list_word_at; trivial.
680 rewrite length_le_split.
681 case BW as [ [ -> | -> ] ]; cbv; discriminate.
682 Qed.
683
684 Lemma truncated_scalar_one_ptsto_iff1 :
685 forall (addr : word) x,
686 Lift1Prop.iff1
687 (truncated_scalar access_size.one addr x)
688 (ptsto addr (Byte.byte.of_Z x)).
689 Proof. intros.
690 unfold scalar, truncated_word, truncated_scalar.
691 etransitivity. {
692 symmetry; eapply array1_iff_eq_of_list_word_at; trivial.
693 rewrite length_le_split.
694 case BW as [ [ -> | -> ] ]; cbv; discriminate. }
695 unfold Memory.bytes_per, le_split, array.
696 cancel.
697 Qed.
698
699 Lemma array_truncated_scalar_scalar_iff1 :
700 forall xs (start : word) size,
701 Lift1Prop.iff1
702 (array (truncated_scalar access_size.word)
703 size start (map word.unsigned xs))
704 (array scalar size start xs).
705 Proof.
706 induction xs; cbn [array map]; intros; [ reflexivity | ].
707 rewrite IHxs by auto. reflexivity.
708 Qed.
709
710 Lemma array_truncated_scalar_ptsto_iff1 :
711 forall xs (start : word) size,
712 Lift1Prop.iff1
713 (array (truncated_scalar access_size.one)
714 size start xs)
715 (array ptsto size start (map Byte.byte.of_Z xs)).
716 Proof.
717 induction xs; cbn [array map]; intros; [ reflexivity | ].
718 rewrite IHxs by auto. rewrite truncated_scalar_one_ptsto_iff1.
719 cancel.
720 Qed.
721 End Scalars.
722
723 Section Words.
724 Local Hint Mode word.word - : typeclass_instances.
725 Context {width} {word : word.word width} {ok : word.ok word}.
726
727 Lemma map_of_Z_unsigned x :
728 map word.of_Z (map word.unsigned x) = x.
729 Proof.
730 rewrite map_map.
731 rewrite map_ext with (g:=id);
732 [ solve [apply map_id] | ].
733 intros. rewrite word.of_Z_unsigned.
734 reflexivity.
735 Qed.
736
737 Lemma map_unsigned_of_Z x :
738 map word.unsigned (map word.of_Z x) = map word.wrap x.
739 Proof.
740 rewrite map_map. apply map_ext.
741 exact word.unsigned_of_Z.
742 Qed.
743
744 Lemma Forall_map_unsigned x :
745 Forall (fun z : Z => (0 <= z < 2 ^ width)%Z)
746 (map word.unsigned x).
747 Proof.
748 induction x; intros; cbn [map]; constructor;
749 auto using word.unsigned_range.
750 Qed.
751
752 Local Notation bytes_per_word := (Z.to_nat (Memory.bytes_per_word width)).
753 Lemma Forall_word_unsigned_within_access_size
754 {bits_per_word_eq_width : (Z.of_nat bytes_per_word * 8 = width)%Z} x :
755 Forall
756 (fun z : Z =>
757 0 <= z < 2 ^ (Z.of_nat (Memory.bytes_per (width:=width) access_size.word) * 8))%Z
758 (map word.unsigned x).
759 Proof.
760 intros.
761 eapply Forall_impl; [ | apply Forall_map_unsigned ].
762 cbv beta; intros.
763 setoid_rewrite bits_per_word_eq_width; assumption.
764 Qed.
765 End Words.
766
767 (* These lemmas should be moved to bedrock2, not coqutil *)
768 Section Separation.
769 Local Hint Mode map.map - - : typeclass_instances.
770 Context `{map_ok : map.ok}
771 {key_eqb : forall H H0 : key, bool}
772 {key_eq_dec :
773 forall x y : key,
774 BoolSpec (x = y) (x <> y) (key_eqb x y)}.
775 Lemma sep_empty_iff (q r : map.rep -> Prop) :
776 sep q r map.empty <-> q map.empty /\ r map.empty.
777 Proof.
778 cbv [sep].
779 repeat match goal with
780 | _ => progress (intros; subst)
781 | H : _ /\ _ |- _ => destruct H
782 | H : exists _, _ |- _ => destruct H
783 | H : _ |- _ => apply map.split_empty in H
784 | _ => rewrite map.split_empty
785 | _ => exists map.empty
786 | _ => tauto
787 | _ => split
788 end.
789 Qed.
790
791 Lemma iff1_sep_cancel_both p1 p2 q1 q2 :
792 Lift1Prop.iff1 p1 p2 ->
793 Lift1Prop.iff1 q1 q2 ->
794 Lift1Prop.iff1 (sep p1 q1) (sep p2 q2).
795 Proof.
796 intros Hp Hq. rewrite Hp, Hq. reflexivity.
797 Qed.
798 End Separation.
799
800 (* These lemmas should be moved to bedrock2, not coqutil *)
801 Section WeakestPrecondition.
802 Import Bitwidth bedrock2.WeakestPrecondition.
803 Context {width: Z} {BW: Bitwidth width} {word: word.word width} {mem: map.map word Byte.byte}.
804 Context {locals: map.map String.string word}.
805 Context {ext_spec: bedrock2.Semantics.ExtSpec}.
806 Context {word_ok : word.ok word} {mem_ok : map.ok mem}.
807 Context {locals_ok : map.ok locals}.
808 Context {ext_spec_ok : Semantics.ext_spec.ok ext_spec}.
809
810 (* "expr" should not be Compilers.expr; can remove once upstreamed *)
811 Local Notation expr := WeakestPrecondition.expr.
812
813 Section Load.
814 Lemma load_empty :
815 forall s (m:mem) a post,
816 load s map.empty a post -> load s m a post.
817 Proof.
818 cbv [load Memory.load Memory.load_Z Map.Memory.load_bytes]; intros *.
819 destruct option_all eqn:E; [exfalso|intros (?&?&?); discriminate].
820 destruct BW as [ [-> | ->] ], s in E;
821 simpl map in E; rewrite ?map.get_empty in E; discriminate.
822 Qed.
823 End Load.
824
825 Section Get.
826 Lemma get_put_same (l : locals) x y (post:_->Prop) :
827 post y -> get (map.put l x y) x post.
828 Proof.
829 cbv [get]; intros.
830 exists y; rewrite map.get_put_same; tauto.
831 Qed.
832
833 Lemma get_put_diff (l : locals) x1 x2 y (post:_->Prop) :
834 x1 <> x2 ->
835 get l x1 post ->
836 get (map.put l x2 y) x1 post.
837 Proof.
838 cbv [get]; intros.
839 match goal with H : exists _, _ |- _ =>
840 destruct H end.
841 eexists; rewrite map.get_put_diff; eauto.
842 Qed.
843
844 (* TODO: make module around Maps to stop name collision *)
845 Lemma WP_get_only_differ_undef :
846 forall e vset (locals locals' : locals) post,
847 map.only_differ locals vset locals' ->
848 map.undef_on locals vset ->
849 get locals e post ->
850 get locals' e post.
851 Proof.
852 cbv [get]; intros.
853 match goal with H : exists _, _ |- _ => destruct H end.
854 destruct_head'_and. eexists; split; try eassumption; eapply get_only_differ_undef; eauto.
855 Qed.
856 End Get.
857
858 Section Expr.
859 Lemma expr_empty :
860 forall e m (locals : locals) post,
861 expr map.empty locals e post ->
862 expr m locals e post.
863 Proof.
864 induction e;
865 cbn [expr expr_body];
866 cbv [dlet.dlet literal get];
867 intros; eauto.
868 { eapply IHe; eauto.
869 eapply Proper_expr; [ repeat intro | eassumption ].
870 eauto using load_empty. }
871 { eapply IHe1; eauto.
872 eapply Proper_expr; [ repeat intro | eassumption ].
873 cbv beta in *. eapply IHe2; eauto. }
874 { eapply IHe1; eauto.
875 eapply Proper_expr; [ repeat intro | eassumption ].
876 cbv beta in *.
877 destr (word.eqb a (word.of_Z 0)); eauto. }
878 Qed.
879
880 Lemma expr_untouched mem1 mem2 (l1 l2 : locals) vars v P :
881 map.only_differ l2 vars l1 ->
882 ~ vars v ->
883 expr mem1 l1 (expr.var v) P <->
884 expr mem2 l2 (expr.var v) P.
885 Proof.
886 cbv [map.only_differ
887 elem_of
888 expr
889 expr_body
890 get].
891 let H := fresh in
892 intro H; specialize (H v).
893 split; intros;
894 repeat match goal with
895 | H : exists _, _ |- _ => destruct H
896 | H : _ \/ _ |- _ => destruct H
897 | H : _ /\ _ |- _ => destruct H
898 | H : map.get _ _ = map.get _ _ |- _ =>
899 rewrite H in *
900 | |- exists _, _ => eexists
901 | |- _ /\ _ => split
902 | _ => eassumption
903 | _ => tauto
904 end.
905 Qed.
906
907 Lemma expr_only_differ_undef :
908 forall e m vset (locals locals' : locals) post,
909 map.only_differ locals vset locals' ->
910 map.undef_on locals vset ->
911 expr m locals e post ->
912 expr m locals' e post.
913 Proof.
914 induction e;
915 cbn [expr expr_body]; cbv [dlet.dlet literal];
916 intros; eauto using WP_get_only_differ_undef.
917 { eapply IHe1; eauto.
918 eapply Proper_expr; [ repeat intro | eassumption ].
919 cbv beta in *. eapply IHe2; eauto. }
920 { eapply IHe1; eauto.
921 eapply Proper_expr; [ repeat intro | eassumption ].
922 cbv beta in *. destr (word.eqb a (word.of_Z 0)); eauto. }
923 Qed.
924 End Expr.
925
926 Section ListMap.
927 Context {A B} (f : A -> (B -> Prop) -> Prop)
928 (f_ext :
929 forall a H1 H2,
930 (forall b, H1 b <-> H2 b) ->
931 f a H1 <-> f a H2).
932 Lemma list_map_app_iff xs ys post :
933 list_map f (xs ++ ys) post <->
934 list_map
935 f xs (fun xx =>
936 list_map
937 f ys (fun yy => post (xx ++ yy))).
938 Proof.
939 revert ys post; induction xs;
940 repeat match goal with
941 | _ => progress intros
942 | _ => progress cbn [list_map
943 list_map_body] in *
944 | _ => rewrite app_nil_l
945 | _ => rewrite <-app_comm_cons
946 | |- f _ _ <-> f _ _ => apply f_ext
947 | _ => reflexivity
948 end.
949 apply IHxs.
950 Qed.
951
952 Lemma list_map_cons_iff x xs post :
953 list_map f (x :: xs) post <->
954 f x (fun y => list_map f xs (fun ys => post (y :: ys))).
955 Proof. reflexivity. Qed.
956 End ListMap.
957
958 Section Dexpr.
959 Lemma dexpr_equiv m (l : locals) n x1 x2 :
960 dexpr m l (expr.var n) x1 ->
961 dexpr m l (expr.var n) x2 ->
962 x1 = x2.
963 Proof.
964 destruct 1; destruct 1; destruct_head'_and; congruence.
965 Qed.
966
967 Lemma dexpr_put_same m (l : locals) n x :
968 dexpr m (map.put l n x) (expr.var n) x.
969 Proof. eexists; rewrite map.get_put_same; tauto. Qed.
970
971 Lemma dexpr_put_diff m (l : locals) n1 n2 x y :
972 n1 <> n2 ->
973 dexpr m l (expr.var n1) x ->
974 dexpr m (map.put l n2 y) (expr.var n1) x.
975 Proof.
976 destruct 2; intros; eexists; rewrite map.get_put_diff; eauto.
977 Qed.
978 End Dexpr.
979
980 (* TODO: some of these may be unused *)
981 Section Dexprs.
982 Local Ltac peel_expr :=
983 progress (
984 repeat
985 progress match goal with
986 | H : expr ?m ?l ?e _ |- _ =>
987 match goal with
988 | |- expr m l e _ => idtac
989 | _ =>
990 apply expr_sound in H;
991 destruct H as [? [_ H] ]
992 end
993 end).
994
995 Local Ltac propers_step :=
996 match goal with
997 | H : get ?l ?x _
998 |- get ?l ?x _ =>
999 eapply Proper_get
1000 | H : expr ?m ?l ?e _
1001 |- expr ?m ?l ?e _ =>
1002 eapply Proper_expr
1003 | H : list_map ?f ?xs _
1004 |- list_map ?f ?xs _ =>
1005 eapply Proper_list_map
1006 end; [ repeat intro .. | eassumption ]; cbv beta in *.
1007
1008 Local Ltac propers :=
1009 propers_step;
1010 match goal with
1011 | _ => solve [propers]
1012 | H : _ |- _ => apply H; solve [eauto]
1013 | _ => congruence
1014 end.
1015
1016 Lemma dexprs_cons_iff m (l : locals) e es v vs :
1017 dexprs m l (e :: es) (v :: vs) <->
1018 (expr m l e (eq v)
1019 /\ dexprs m l es vs).
1020 Proof.
1021 cbv [dexprs].
1022 revert es v vs; induction es; split; intros;
1023 repeat match goal with
1024 | _ => progress cbn [list_map
1025 list_map_body] in *
1026 | _ => progress cbv beta in *
1027 | H : _ :: _ = _ :: _ |- _ => inversion H; clear H; subst
1028 | |- _ /\ _ => split
1029 | _ => progress destruct_head'_and
1030 | _ => solve [propers]
1031 | _ => reflexivity
1032 | _ => peel_expr
1033 end.
1034 eapply IHes with (vs := tl vs).
1035 propers_step. peel_expr.
1036 destruct vs; cbn [tl]; propers.
1037 Qed.
1038
1039 Lemma dexprs_cons_nil m (l : locals) e es :
1040 dexprs m l (e :: es) [] -> False.
1041 Proof.
1042 cbv [dexprs].
1043 induction es; intros;
1044 repeat match goal with
1045 | _ => progress cbn [list_map
1046 list_map_body] in *
1047 | _ => congruence
1048 | _ => solve [propers]
1049 | _ => apply IHes
1050 | _ => peel_expr
1051 end.
1052 propers_step. peel_expr. propers.
1053 Qed.
1054
1055 Lemma dexprs_app_l m (l : locals) es1 :
1056 forall es2 vs,
1057 dexprs m l (es1 ++ es2) vs ->
1058 (dexprs m l es1 (firstn (length es1) vs)) /\
1059 (dexprs m l es2 (skipn (length es1) vs)).
1060 Proof.
1061 induction es1; intros.
1062 { cbn [Datatypes.length skipn firstn]; rewrite app_nil_l in *.
1063 split; eauto; reflexivity. }
1064 { destruct vs; rewrite <-app_comm_cons in *;
1065 [ match goal with H : _ |- _ => apply dexprs_cons_nil in H; tauto end | ].
1066 cbn [Datatypes.length skipn firstn].
1067 rewrite !dexprs_cons_iff in *.
1068 destruct_head'_and.
1069 repeat split; try eapply IHes1; eauto. }
1070 Qed.
1071
1072 Lemma dexprs_length m (l : locals) :
1073 forall vs es,
1074 dexprs m l es vs ->
1075 length es = length vs.
1076 Proof.
1077 induction vs; destruct es; intros;
1078 repeat match goal with
1079 | _ => progress cbn [Datatypes.length]
1080 | _ => progress destruct_head'_and
1081 | H : _ |- _ => apply dexprs_cons_nil in H; tauto
1082 | H : _ |- _ => apply dexprs_cons_iff in H
1083 | _ => reflexivity
1084 | _ => congruence
1085 end.
1086 rewrite IHvs; auto.
1087 Qed.
1088 End Dexprs.
1089 End WeakestPrecondition.