Debian Codegraph @

src/Bedrock/Field/Translation/Proofs/LoadStoreList.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import String.
3 From Coq Require Import List.
4 From Coq Require Import Lia.
5 Require Import bedrock2.Array.
6 Require Import bedrock2.Scalars.
7 Require Import bedrock2.Syntax.
8 Require Import bedrock2.ProgramLogic.
9 Require Import bedrock2.Map.Separation.
10 Require Import bedrock2.Map.SeparationLogic.
11 Require Import bedrock2.WeakestPreconditionProperties.
12 Require Import coqutil.Word.Interface coqutil.Word.Properties.
13 Require Import coqutil.Map.Interface coqutil.Map.Properties.
14 Require Import coqutil.Datatypes.List.
15 Require Import coqutil.Datatypes.PropSet.
16 Require Import Crypto.AbstractInterpretation.AbstractInterpretation.
17 Require Import Crypto.Bedrock.Field.Common.Types.
18 Require Import Crypto.Bedrock.Field.Common.Tactics.
19 Require Import Crypto.Bedrock.Field.Common.Util.
20 Require Import Crypto.Bedrock.Field.Translation.Proofs.Equivalence.
21 Require Import Crypto.Bedrock.Field.Translation.Proofs.EquivalenceProperties.
22 Require Import Crypto.Bedrock.Field.Translation.Proofs.Flatten.
23 Require Import Crypto.Bedrock.Field.Translation.Proofs.UsedVarnames.
24 Require Import Crypto.Bedrock.Field.Translation.Proofs.VarnameSet.
25 Require Import Crypto.Bedrock.Field.Translation.Flatten.
26 Require Import Crypto.Bedrock.Field.Translation.LoadStoreList.
27 Require Import Crypto.Language.API.
28 Require Import Crypto.Util.ListUtil.
29 Require Import Crypto.Util.ZUtil.Tactics.RewriteModSmall.
30 Import ListNotations.
31
32 Import AbstractInterpretation.Compilers.
33 Import API.Compilers.
34 Import Types.Notations.
35
36 Section LoadStoreList.
37 Context
38 {width BW word mem locals ext_spec varname_gen error}
39 `{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}.
40 Context {ok : ok}.
41
42 Local Existing Instance rep.Z.
43
44 (* states that access sizes are within machine integer width *)
45 Fixpoint base_access_sizes_good {t}
46 : base_access_sizes (listZ:=rep.listZ_mem) t -> Prop :=
47 match t with
48 | base.type.prod a b =>
49 fun s => base_access_sizes_good (fst s)
50 /\ base_access_sizes_good (snd s)
51 | base_listZ =>
52 fun s =>
53 (Z.of_nat (Memory.bytes_per
54 (width:=width) s) * 8 <= width)%Z
55 | _ => fun _ => True
56 end.
57 Definition access_sizes_good {t}
58 : access_sizes (listZ:=rep.listZ_mem) t -> Prop :=
59 match t with
60 | type.base _ => base_access_sizes_good
61 | _ => fun _ => True
62 end.
63 Fixpoint access_sizes_good_args {t}
64 : type.for_each_lhs_of_arrow
65 (access_sizes (listZ:=rep.listZ_mem)) t -> Prop :=
66 match t with
67 | type.base _ => fun _ : unit => True
68 | type.arrow _ _ =>
69 fun s => access_sizes_good (fst s) /\ access_sizes_good_args (snd s)
70 end.
71
72 Lemma access_sizes_good_in_range size :
73 base_access_sizes_good (t:=base_listZ) size ->
74 (2 ^ (Z.of_nat (Memory.bytes_per
75 (width:=width) size) * 8)
76 <= 2 ^ width)%Z.
77 Proof.
78 cbv [base_access_sizes_good]; intros.
79 apply Z.pow_le_mono_r; lia.
80 Qed.
81
82 (* states that each list Z is within its access size *)
83 Fixpoint within_base_access_sizes {t}
84 : base.interp t ->
85 base_access_sizes (listZ:=rep.listZ_mem) t -> Prop :=
86 match t with
87 | base.type.prod a b =>
88 fun x s =>
89 within_base_access_sizes (fst x) (fst s) /\
90 within_base_access_sizes (snd x) (snd s)
91 | base_listZ =>
92 fun x s =>
93 let bytes :=
94 Z.of_nat (Memory.bytes_per (width:=width) s) in
95 Forall (fun z => 0 <= z < 2 ^ (bytes * 8))%Z x
96 | _ => fun _ _ => True
97 end.
98
99 Fixpoint within_access_sizes_args {t}
100 : type.for_each_lhs_of_arrow API.interp_type t ->
101 type.for_each_lhs_of_arrow
102 (access_sizes (listZ:=rep.listZ_mem)) t -> Prop :=
103 match t with
104 | type.base _ => fun _ _ => True
105 | type.arrow (type.base s) _ =>
106 fun x s =>
107 within_base_access_sizes (fst x) (fst s) /\
108 within_access_sizes_args (snd x) (snd s)
109 | _ => fun _ _ => False
110 end.
111
112 (* separation-logic relation that says space exists in memory for lists
113 (other values are ignored) *)
114 Fixpoint lists_reserved {t}
115 : base_listonly nat t ->
116 base_rtype (listZ:=rep.listZ_mem) t ->
117 base_access_sizes (listZ:=rep.listZ_mem) t ->
118 locals ->
119 mem ->
120 Prop :=
121 match t as t0 return
122 base_listonly nat t0 -> base_rtype t0 ->
123 base_access_sizes (listZ:=rep.listZ_mem) t0 -> _ with
124 | base.type.prod a b =>
125 fun x y s locals =>
126 sep (lists_reserved (fst x) (fst y) (fst s) locals)
127 (lists_reserved (snd x) (snd y) (snd s) locals)
128 | base_listZ =>
129 fun n loc sz locals =>
130 (Lift1Prop.ex1
131 (fun oldvalues : list Z =>
132 sep (emp (length oldvalues = n))
133 (rep.equiv (rep:=rep.listZ_mem)
134 oldvalues loc sz locals)))
135 | base_Z => fun _ _ _ _ => emp True
136 | _ => fun _ _ _ _ => emp False
137 end.
138
139 Fixpoint list_lengths_from_value {t}
140 : base.interp t -> base_listonly nat t :=
141 match t as t0 return base.interp t0 -> base_listonly nat t0 with
142 | base.type.prod a b =>
143 fun x : base.interp a * base.interp b =>
144 (list_lengths_from_value (fst x),
145 list_lengths_from_value (snd x))
146 | base_listZ => fun x : list Z => length x
147 | _ => fun _ => tt
148 end.
149
150 Fixpoint list_lengths_from_args {t}
151 : type.for_each_lhs_of_arrow API.interp_type t ->
152 type.for_each_lhs_of_arrow list_lengths t :=
153 match t with
154 | type.base b => fun _ => tt
155 | type.arrow (type.base a) b =>
156 fun x =>
157 (list_lengths_from_value (fst x), list_lengths_from_args (snd x))
158 | type.arrow a b =>
159 fun x => (tt, list_lengths_from_args (snd x))
160 end.
161
162 Definition lists_reserved_with_initial_context {t}
163 (lengths : base_listonly nat (type.final_codomain t))
164 (argnames : type.for_each_lhs_of_arrow
165 (ltype (listZ:=rep.listZ_mem)) t)
166 (retnames : base_ltype (type.final_codomain t))
167 (retsizes : base_access_sizes (type.final_codomain t))
168 (flat_args : list word)
169 : mem -> Prop :=
170 let outptrs :=
171 flatten_listonly_base_ltype (fst (extract_listnames retnames)) in
172 match map.of_list_zip
173 (outptrs ++ (flatten_argnames argnames))
174 flat_args with
175 | Some init_locals =>
176 lists_reserved
177 lengths (base_rtype_of_ltype retnames) retsizes init_locals
178 | None => emp True
179 end.
180
181 Lemma lists_reserved_0 {listZ:rep.rep base_listZ} start sizes :
182 forall (locals:locals),
183 Lift1Prop.iff1
184 (lists_reserved (t:=base_listZ) 0%nat start sizes locals)
185 (emp (exists x, locally_equivalent_base (t:=base_Z) x start locals)).
186 Proof.
187 cbv [lists_reserved].
188 split; intros;
189 repeat match goal with
190 | _ => progress (sepsimpl; subst)
191 | _ => progress cbn [locally_equivalent_base
192 equivalent_base rep.equiv rep.Z] in *
193 | H : Datatypes.length _ = 0%nat |- _ =>
194 apply length0_nil in H; subst
195 | H : rep.equiv _ _ _ _ _ |- _ => apply equiv_nil_iff1 in H
196 | |- Lift1Prop.ex1 _ _ => eexists
197 | |- exists _, _ => eexists
198 | |- _ /\ _ => split
199 | |- rep.equiv [] _ _ _ _ => apply equiv_nil_iff1
200 | _ => solve [eauto using List.length_nil]
201 | _ => eexists; split; solve [eauto]
202 end.
203 Qed.
204
205 Lemma lists_reserved_only_differ_undef t :
206 forall lengths locs s locals locals' vset,
207 map.only_differ locals vset locals' ->
208 map.undef_on locals vset ->
209 Lift1Prop.impl1
210 (lists_reserved (t:=t) lengths locs s locals)
211 (lists_reserved lengths locs s locals').
212 Proof.
213 induction t; cbn [lists_reserved];
214 break_match; intros; try reflexivity; [ | ].
215 { rewrite IHt1, IHt2; try reflexivity; eauto. }
216 { repeat intro; sepsimpl; eexists; sepsimpl; eauto.
217 eapply equiv_listZ_only_differ_undef_mem; eauto. }
218 Qed.
219
220 Lemma equivalent_extract_listnames {t} :
221 forall (x : base.interp t)
222 (names : base_ltype t)
223 (locals : locals)
224 (sizes : base_access_sizes t)
225 (R : mem -> Prop),
226 Lift1Prop.iff1
227 (sep (equivalent_base x (base_rtype_of_ltype names) sizes locals) R)
228 (sep (equivalent_listexcl
229 x (map_listexcl (fun t => base_rtype_of_ltype (listZ:=rep.listZ_mem)(t:=t))
230 (snd (extract_listnames names))) sizes locals)
231 (sep (equivalent_listonly
232 x (map_listonly
233 (base_rtype_of_ltype)
234 (fst (extract_listnames names))) sizes locals) R)).
235 Proof.
236 induction t;
237 cbn [fst snd equivalent_base extract_listnames
238 base_rtype_of_ltype rep.rtype_of_ltype
239 equivalent_listexcl map_listexcl
240 equivalent_listonly map_listonly];
241 break_match; intros;
242 try match goal with
243 |- context [emp False] =>
244 split; intros; sepsimpl
245 end; [ solve [cancel] | | ].
246 { rewrite sep_assoc. (* needed for rewrites for some reason *)
247 rewrite IHt1. rewrite IHt2.
248 rewrite !sep_assoc.
249 cancel.
250 (* TODO : this should go by associativity/commutativity; is there a better
251 way than manually selecting indices? *)
252 cancel_seps_at_indices 0 0; [ reflexivity | ].
253 cancel_seps_at_indices 0 1; [ reflexivity | ].
254 cancel_seps_at_indices 0 0; [ reflexivity | ].
255 reflexivity. }
256 { cancel. reflexivity. }
257 Qed.
258
259 Lemma load_list_item_correct
260 (size : access_size)
261 (name : base_ltype (listZ:=rep.listZ_mem) base_listZ)
262 i l :
263 forall mem locals R,
264 (i < length l)%nat ->
265 base_access_sizes_good (t:=base_listZ) size ->
266 sep (rep.equiv (t:=base_listZ)
267 l (base_rtype_of_ltype name) size locals) R mem ->
268 WeakestPrecondition.dexpr
269 mem locals
270 (load_list_item (width:=width) size (expr.var name) i)
271 (word.of_Z (hd 0%Z (skipn i l))).
272 Proof.
273 cbv [load_list_item];
274 cbn [rep.equiv rep.listZ_mem rep.Z base_rtype_of_ltype
275 rep.rtype_of_ltype rep.listZ_mem ]; intros.
276 cbn [WeakestPrecondition.dexpr
277 WeakestPrecondition.expr WeakestPrecondition.expr_body] in *.
278 sepsimpl; subst.
279 match goal with
280 | H : context[array] |- _ =>
281 eapply Proper_sep_iff1 in H;
282 [ | symmetry; apply array_index_nat_inbounds
283 with (n:=i) (default:=0%Z);
284 rewrite !map_length in *; lia
285 | reflexivity]
286 end.
287 match goal with
288 | H : context[array] |- _ =>
289 rewrite !word.ring_morph_mul, !word.of_Z_unsigned in H;
290 rewrite <-!word.ring_morph_mul in H
291 end.
292 straightline.
293 eapply Proper_get; [ repeat intro | eassumption ].
294 match goal with H : word.unsigned _ = word.unsigned _ |- _ =>
295 apply word.unsigned_inj in H
296 end.
297 subst. rewrite Nat2Z.inj_mul.
298 eexists; split.
299 { cbv [Memory.load].
300 erewrite load_Z_of_sep; [ reflexivity | ].
301 ecancel_assumption. }
302 { unfold truncate_Z.
303 rewrite Z.land_ones by auto with zarith.
304 rewrite Z.mod_small; [ reflexivity | ].
305 rewrite <-hd_skipn_nth_default.
306 match goal with H : base_access_sizes_good _ |- _ =>
307 apply access_sizes_good_in_range in H end.
308 apply Forall_nth_default; auto with zarith. }
309 Qed.
310
311 Lemma load_list_correct rem l :
312 forall (i nextn : nat)
313 (name : base_ltype (listZ:=rep.listZ_mem) base_listZ)
314 tr
315 (sizes : base_access_sizes
316 (listZ:=rep.listZ_mem) base_listZ)
317 (locals : locals)
318 (mem : mem)
319 functions
320 (R : _ -> Prop),
321 (forall (n : nat) (H : nextn <= n), name <> varname_gen n) ->
322 base_access_sizes_good sizes ->
323 length l = i + rem ->
324 sep (rep.equiv (t:=base_listZ)
325 l (base_rtype_of_ltype name) sizes locals) R mem ->
326 (* load_list returns # vars used, variable names, cmd *)
327 let out := load_list (width:=width) (varname_gen:=varname_gen) sizes (expr.var name) i rem nextn in
328 let nvars := fst (fst out) in
329 let names' : base_ltype (listZ:=rep.listZ_local) base_listZ
330 := snd (fst out) in
331 WeakestPrecondition.cmd
332 functions
333 (snd out)
334 tr mem locals
335 (fun tr' mem' locals' =>
336 tr = tr' /\
337 mem = mem' /\
338 Interface.map.only_differ
339 locals (used_varnames (varname_gen:=varname_gen) nextn nvars) locals' /\
340 (forall n,
341 (nextn + nvars <= n)%nat ->
342 ~ varname_set_base names' (varname_gen n)) /\
343 locally_equivalent_base
344 (t:=base_listZ) (skipn i l)
345 (base_rtype_of_ltype names') locals').
346 Proof.
347 induction rem; cbn [fst snd load_list]; intros.
348 { straightline.
349 cbv [locally_equivalent_base].
350 cbn [rep.Z rep.listZ_local fold_right map
351 equivalent_base rep.equiv
352 base_rtype_of_ltype rep.rtype_of_ltype
353 varname_set rep.varname_set].
354 repeat split.
355 { eapply only_differ_zero. }
356 { intros. cbv [empty_set]. tauto. }
357 { rewrite skipn_all by lia. econstructor. } }
358 { cbn [WeakestPrecondition.cmd WeakestPrecondition.cmd_body].
359 eexists; split; cbv [dlet.dlet].
360 { eapply load_list_item_correct; try eassumption.
361 cbn [Datatypes.length]; lia. }
362 eapply Proper_cmd.
363 2:{
364 eapply IHrem; eauto with lia.
365 eapply Proper_sep_iff1; [ | reflexivity | eassumption ].
366 eapply equiv_listZ_only_differ_mem_iff1;
367 eauto using @only_differ_put with typeclass_instances.
368 cbn [varname_set rep.varname_set rep.listZ_mem rep.Z].
369 apply disjoint_singleton_singleton; eauto using string_dec. }
370 repeat intro.
371 cbv beta in *; cleanup; subst.
372 repeat match goal with |- _ /\ _ => split end;
373 eauto using only_differ_succ.
374 { cbn [varname_set_base
375 rep.varname_set rep.listZ_local rep.Z fold_right] in *.
376 intros.
377 apply not_union_iff; split; eauto with lia.
378 cbv [singleton_set].
379 rewrite varname_gen_unique; lia. }
380 { cbv [locally_equivalent] in *.
381 cbn [map base_rtype_of_ltype
382 Compilers.base_interp
383 rep.rtype_of_ltype rep.listZ_local
384 equivalent rep.equiv ] in *.
385 rewrite skipn_nth_default with (d:=0%Z) by lia.
386 eapply Forall2_cons; eauto.
387 eapply (equiv_Z_only_differ_iff1 (listZ:=rep.listZ_mem)); eauto.
388 { eapply only_differ_sym; eassumption. }
389 { eapply disjoint_used_varnames_singleton.
390 lia. }
391 { assert (2 ^ (Z.of_nat (Memory.bytes_per (width:=width) sizes) * 8)
392 <= 2 ^ width)%Z
393 by (cbv [base_access_sizes_good] in *;
394 apply Z.pow_le_mono_r; lia).
395 cbn [rep.equiv rep.listZ_mem rep.Z] in *.
396 sepsimpl_hyps.
397 match goal with H : word.unsigned _ = word.unsigned _ |- _ =>
398 apply word.unsigned_inj in H
399 end.
400 subst. eexists.
401 split;[reflexivity|].
402 split.
403 { rewrite <-word.unsigned_of_Z_0, map_nth_default_always.
404 reflexivity. }
405 { eexists; split; [ | reflexivity ].
406 rewrite map.get_put_same, hd_skipn_nth_default.
407 rewrite <-word.unsigned_of_Z_0.
408 rewrite skipn_map, hd_map.
409 rewrite !word.of_Z_unsigned.
410 reflexivity. } } } }
411 Qed.
412
413 Lemma load_all_lists_correct {t} :
414 forall (argnames : base_ltype t)
415 (argsizes : base_access_sizes t)
416 (args : base.interp t)
417 functions
418 tr
419 (locals : locals)
420 (mem : mem)
421 (nextn : nat)
422 (R : _ -> Prop),
423 (* lengths of any lists in the arguments *)
424 let arglengths := list_lengths_from_value args in
425 (* look up variables in argnames *)
426 let argvalues := base_rtype_of_ltype argnames in
427 (* argnames don't contain variables we could later overwrite *)
428 (forall n,
429 (nextn <= n)%nat ->
430 ~ varname_set_base argnames (varname_gen n)) ->
431 (* argument sizes are <= integer width *)
432 base_access_sizes_good argsizes ->
433 (* argument values are equivalent *)
434 sep (equivalent_base args argvalues argsizes locals) R mem ->
435 (* load_all_lists returns triple : # fresh variables used,
436 new argnames with local lists, and cmd *)
437 let out := load_all_lists nextn argnames arglengths argsizes in
438 let nvars := fst (fst out) in
439 (* extract loaded argument values *)
440 let argnames' := snd (fst out) in
441 let argvalues' := base_rtype_of_ltype argnames' in
442 (* translated function produces equivalent results *)
443 WeakestPrecondition.cmd
444 functions
445 (snd out)
446 tr mem locals
447 (fun tr' mem' locals' =>
448 tr = tr' /\
449 mem = mem' /\
450 Interface.map.only_differ
451 locals (used_varnames (varname_gen:=varname_gen) nextn nvars) locals' /\
452 (forall n,
453 (nextn + nvars <= n)%nat ->
454 ~ varname_set_base argnames' (varname_gen n)) /\
455 locally_equivalent_base args argvalues' locals').
456 Proof.
457 (* TODO: lots of repeated steps in this proof; automate *)
458 induction t;
459 cbn [fst snd map load_all_lists varname_set
460 locally_equivalent_base equivalent_base rep.equiv
461 flatten_base_ltype equivalent_flat_base
462 base_access_sizes_good
463 base_rtype_of_ltype rep.varname_set rep.Z
464 ]; break_match; intros; sepsimpl; [ | | ].
465 { (* base_Z *)
466 repeat straightline. cbv [emp].
467 repeat match goal with
468 | |- Lift1Prop.ex1 _ _ => eexists
469 | |- _ /\ _ => split end;
470 eauto using only_differ_zero with lia. }
471 { (* product *)
472 straightline.
473 eapply Proper_cmd.
474 2:{ eapply IHt1; eauto.
475 { intros.
476 match goal with
477 | H : _ |- _ =>
478 setoid_rewrite not_union_iff in H;
479 apply H; eauto
480 end. }
481 { use_sep_assumption; cancel.
482 cancel_seps_at_indices 0%nat 0%nat; trivial.
483 ecancel_done. } }
484 repeat intro. cleanup; subst.
485 eapply Proper_cmd.
486 2:{ eapply IHt2; eauto.
487 { intros.
488 match goal with
489 | H : _ |- _ =>
490 setoid_rewrite not_union_iff in H;
491 apply H; lia
492 end. }
493 { cbn [base_rtype_of_ltype fst snd] in *.
494 eapply Proper_sep_iff1; [ | reflexivity | ].
495 { eapply equivalent_only_differ_iff1;
496 eauto using equiv_listZ_only_differ_mem, @only_differ_sym with typeclass_instances.
497 eapply disjoint_used_varnames_lt.
498 match goal with
499 | H : _ |- _ =>
500 setoid_rewrite not_union_iff in H;
501 apply H; eauto
502 end. }
503 use_sep_assumption; cancel.
504 cancel_seps_at_indices 1%nat 0%nat; trivial.
505 ecancel_done. } }
506 cbn [list_lengths_from_value fst snd] in *.
507 repeat intro. cleanup; subst.
508 repeat match goal with |- _ /\ _ => split end;
509 eauto using only_differ_step.
510 { intros; apply not_union_iff; split;
511 match goal with H : _ |- _ => apply H; lia end. }
512 { cbv [locally_equivalent locally_equivalent_args] in *.
513 cbn [equivalent_args fst rtype_of_ltype].
514 apply sep_empty_iff; split; eauto.
515 eapply equivalent_only_differ_iff1;
516 eauto using equiv_listZ_only_differ_local, @only_differ_sym with typeclass_instances.
517 intros.
518 eapply disjoint_used_varnames_lt.
519 cleanup. subst.
520 match goal with H : _ |- _ => apply H; lia end. } }
521 { (* base_listZ *)
522 clear IHt.
523 cbn [flatten_base_rtype base_rtype_of_ltype] in *.
524 eapply Proper_cmd;
525 [ | eapply load_list_correct; eauto; reflexivity ].
526 repeat intro. cleanup; subst.
527 repeat match goal with |- _ /\ _ => split end;
528 eauto using only_differ_step. }
529 Qed.
530
531 Lemma load_arguments_correct {t} :
532 forall (argnames : type.for_each_lhs_of_arrow ltype t)
533 (argsizes : type.for_each_lhs_of_arrow access_sizes t)
534 (args : type.for_each_lhs_of_arrow API.interp_type t)
535 functions
536 tr
537 (locals : locals)
538 (mem : mem)
539 (nextn : nat),
540 (* lengths of any lists in the arguments *)
541 let arglengths := list_lengths_from_args args in
542 (* look up variables in argnames *)
543 let argvalues :=
544 type.map_for_each_lhs_of_arrow rtype_of_ltype argnames in
545 (* argnames don't contain variables we could later overwrite *)
546 (forall n,
547 (nextn <= n)%nat ->
548 ~ varname_set_args argnames (varname_gen n)) ->
549 (* argument sizes are <= width *)
550 access_sizes_good_args argsizes ->
551 (* argument values are equivalent *)
552 equivalent_args args argvalues argsizes locals mem ->
553 (* load_arguments returns triple : # fresh variables used,
554 new argnames with local lists, and cmd *)
555 let out := load_arguments nextn argnames arglengths argsizes in
556 let nvars := fst (fst out) in
557 (* extract loaded argument values *)
558 let argnames' := snd (fst out) in
559 let argvalues' :=
560 type.map_for_each_lhs_of_arrow rtype_of_ltype argnames' in
561 (* translated function produces equivalent results *)
562 WeakestPrecondition.cmd
563 functions
564 (snd out)
565 tr mem locals
566 (fun tr' mem' locals' =>
567 tr = tr' /\
568 mem = mem' /\
569 Interface.map.only_differ
570 locals (used_varnames (varname_gen:=varname_gen) nextn nvars) locals' /\
571 (forall n,
572 (nextn + nvars <= n)%nat ->
573 ~ varname_set_args argnames' (varname_gen n)) /\
574 locally_equivalent_args args argvalues' locals').
575 Proof.
576 cbv zeta.
577 induction t;
578 destruct argnames;
579 cbn [fst snd load_arguments access_sizes_good_args
580 list_lengths_from_args varname_set_args
581 type.for_each_lhs_of_arrow equivalent_args
582 type.map_for_each_lhs_of_arrow flatten_args
583 ]; break_match; cbn [fst snd]; intros.
584 { (* base type *)
585 repeat straightline.
586 repeat match goal with
587 | |- _ /\ _ => split end;
588 eauto using only_differ_zero; reflexivity. }
589 { (* arrow (base _) _ *)
590 cleanup. straightline.
591 eapply Proper_cmd.
592 2:{
593 eapply load_all_lists_correct; eauto; [ ].
594 intros.
595 match goal with
596 | H : _ |- _ =>
597 setoid_rewrite not_union_iff in H;
598 apply H; eauto
599 end. }
600 { repeat intro; cleanup; subst.
601 eapply Proper_cmd.
602 2:{
603 eapply IHt2; eauto.
604 { intros.
605 match goal with
606 | H : _ |- _ =>
607 setoid_rewrite not_union_iff in H;
608 apply H; lia
609 end. }
610 { eapply equivalent_args_only_differ;
611 eauto using only_differ_sym with equiv; [ ].
612 intros.
613 eapply disjoint_used_varnames_lt.
614 cleanup. subst.
615 match goal with
616 | H : _ |- _ =>
617 setoid_rewrite not_union_iff in H;
618 apply H; eauto
619 end. } }
620 { repeat intro. cleanup. subst.
621 repeat match goal with |- _ /\ _ => split end;
622 eauto using only_differ_step.
623 { intros; apply not_union_iff; split;
624 match goal with H : _ |- _ => apply H; lia end. }
625 { cbv [locally_equivalent locally_equivalent_args] in *.
626 cbn [equivalent_args fst rtype_of_ltype].
627 split; [ | solve [eauto] ].
628 exists (emp True). sepsimpl; eauto.
629 eapply equivalent_only_differ;
630 eauto using only_differ_sym with equiv; [ ].
631 eapply disjoint_used_varnames_lt.
632 intros; cleanup. subst.
633 eauto with lia. } } } }
634 { (* arrow (arrow _ _) _ *)
635 sepsimpl. }
636 Qed.
637
638 Lemma store_list_correct :
639 forall (size : access_size)
640 (start : string)
641 functions
642 (value_names2 value_names1 : list string)
643 (rets1 rets2 rets : base.interp base_listZ)
644 (i : nat)
645 tr
646 (locals : locals)
647 (m : mem)
648 (R : _ -> Prop),
649 let retlengths :=
650 list_lengths_from_value (t:=base_listZ) rets2 in
651 let loc := expr.var start in
652 let nbytes := Memory.bytes_per (width:=width) size in
653 let offset := expr.literal (Z.of_nat (nbytes * i)) in
654 let loc' := expr.op bopname.add loc offset in
655 let values1 := map expr.var value_names1 in
656 let values2 := map expr.var value_names2 in
657 rets = rets1 ++ rets2 ->
658 length rets1 = length value_names1 ->
659 length rets2 = length value_names2 ->
660 length value_names1 = i ->
661 (* bounds are OK *)
662 Forall (fun x =>
663 0 <= x < 2 ^ (Z.of_nat nbytes * 8))%Z rets2 ->
664 (* yet-to-be-stored values *)
665 locally_equivalent_base
666 (listZ:=rep.listZ_local) rets2 values2 locals ->
667 base_access_sizes_good (t:=base_listZ) size ->
668 (* already-stored values *)
669 sep (map:=mem)
670 (sep (map:=mem)
671 (rep.equiv (rep:=rep.listZ_mem)
672 rets1 (expr.var start) size locals)
673 (lists_reserved retlengths loc' size locals)) R m ->
674 WeakestPrecondition.cmd
675 functions
676 (store_list (width:=width) size (expr.var start) values2 i) tr m locals
677 (fun tr' mem' locals' =>
678 tr = tr' /\
679 locals = locals' /\
680 sep (map:=mem)
681 (rep.equiv (rep:=rep.listZ_mem)
682 rets (expr.var start) size locals') R mem').
683 Proof.
684 cbv zeta.
685 induction value_names2 as [|n0 ?]; destruct rets2 as [|r0 rets2];
686 cbn [store_list Datatypes.length base_access_sizes_good
687 locally_equivalent_base equivalent_base
688 rep.equiv rep.listZ_local rep.Z rep.listZ_mem];
689 intros; subst; try lia; sepsimpl.
690 { repeat split; try reflexivity.
691 repeat match goal with
692 | _ => progress sepsimpl
693 | H : _ |- _ =>
694 rewrite List.firstn_all, List.skipn_all_exact in H
695 | _ => rewrite app_nil_r
696 | |- Lift1Prop.ex1 _ _ => eexists
697 | |- _ /\ _ => split
698 | _ => eassumption
699 end.
700 match goal with
701 H : sep _ _ _ |- _ =>
702 eapply Proper_sep_iff1 in H;
703 [ | rewrite ?(lists_reserved_0 (listZ:=rep.listZ_local));
704 reflexivity .. ]
705 end.
706 sepsimpl.
707 ecancel_assumption. }
708 { repeat match goal with
709 | H : _ |- _ => rewrite word.of_Z_unsigned in H end.
710 subst; eexists; split.
711 { cbn [WeakestPrecondition.dexpr
712 WeakestPrecondition.expr
713 WeakestPrecondition.expr_body] in *.
714 eapply Proper_get; [ | eassumption ].
715 repeat intro; subst.
716 reflexivity. }
717 cbn [map] in *.
718 match goal with
719 | H : Forall2 _ (_ :: _) (_ :: _) |- _ => inversion H; subst; clear H
720 end.
721 sepsimpl.
722 eexists; split.
723 { cbn [WeakestPrecondition.dexpr
724 WeakestPrecondition.expr
725 WeakestPrecondition.expr_body] in *.
726 eassumption. }
727 cbn [lists_reserved
728 list_lengths_from_value
729 equivalent
730 rep.equiv rep.Z rep.listZ_mem] in *.
731 sepsimpl.
732 match goal with
733 H : Datatypes.length ?x = Datatypes.length (_ :: _) |- _ =>
734 destruct x; cbn [Datatypes.length] in H; [ lia | ]
735 end.
736 cbn [map array Semantics.interp_binop] in *.
737
738 (* we now have a Z in context that should be equivalent to the offsetted
739 location; destruct WeakestPrecondition.dexpr to expose that equivalence *)
740 lazymatch goal with
741 H : WeakestPrecondition.dexpr _ _ (expr.op _ _ _) ?v |- _ =>
742 cbn [WeakestPrecondition.dexpr
743 WeakestPrecondition.expr
744 WeakestPrecondition.expr_body
745 Semantics.interp_binop] in H;
746 cbv [dlet.dlet WeakestPrecondition.literal
747 WeakestPrecondition.get] in H;
748 rewrite ?word.of_Z_unsigned in H;
749 cleanup; subst
750 end.
751 match goal with
752 | H1 : (WeakestPrecondition.dexpr _ ?l (expr.var ?x) ?v1),
753 H2 : (map.get ?l ?x = Some ?v2) |- _ =>
754 replace v2 with v1 in *
755 by (cbn [WeakestPrecondition.dexpr
756 WeakestPrecondition.expr
757 WeakestPrecondition.expr_body] in H1;
758 cbv [WeakestPrecondition.get] in H1;
759 cleanup; congruence)
760 end.
761 repeat match goal with
762 | H: word.unsigned _ = word.unsigned _ |- _ =>
763 apply word.unsigned_inj in H; subst
764 end.
765 repeat match goal with
766 | H : map word.unsigned ?x = _ :: _ |- _ =>
767 destruct x; cbn [map] in H;
768 [ congruence | inversion H; clear H ]
769 end.
770 cbn [map array] in *.
771 eapply store_Z_of_sep; [ ecancel_assumption | ].
772 intros. sepsimpl.
773 repeat match goal with
774 | H : Forall _ (_ :: _) |- _ => inversion H; subst; clear H
775 end.
776
777 eapply Proper_cmd; [ repeat intro | ].
778 2:{
779 let s :=
780 lazymatch goal with
781 H : map.get locals0 start = Some ?x |- _ => x end in
782 let H := lazymatch goal with
783 | H : sep _ _ ?m |- context [?m] => H end in
784 let r1 :=
785 lazymatch type of H with
786 context [array _ _ s ?xs] => xs end in
787 let r2 :=
788 lazymatch type of H with
789 context [array _ _ (word.add _ _) ?xs] => xs end in
790 let r :=
791 lazymatch type of H with
792 context [truncated_scalar _ _ ?x] => x end in
793 apply IHvalue_names2 with
794 (rets1:= r1 ++ [r])
795 (rets2:=rets2) (R:=R)
796 (value_names1:=value_names1 ++ [n0]);
797 rewrite ?app_length; cbn [length]; eauto; try lia; [ ].
798 { intros.
799 (* look at goal and plug in evars very carefully *)
800 sepsimpl.
801 match goal with H: ?P (expr.var start) ?x |- _ =>
802 exists x end.
803 sepsimpl; [ solve [eauto using Forall_snoc] .. | ].
804 match goal with
805 |- context [map word.unsigned _ =
806 map word.unsigned ?xs ++ [word.unsigned ?x] ] =>
807 exists (xs ++ [x])
808 end.
809 sepsimpl; [ rewrite map_app; reflexivity
810 | solve [eauto using Forall_snoc] | ].
811 eexists; sepsimpl; [ reflexivity | solve [eauto] | ].
812 match goal with
813 H : sep _ _ ?m |- context [?m] =>
814 match type of H with
815 context [array _ _ (word.add ?a ?b) ?x] =>
816 exists x;
817 sepsimpl; [solve [auto] .. | ];
818 exists (word.add a b)
819 end
820 end.
821 sepsimpl; [ ].
822 eexists; sepsimpl; [ reflexivity | solve [eauto] | ].
823 eexists; sepsimpl; [ reflexivity | | ].
824 { eexists; split; [ eassumption | ].
825 cbn [WeakestPrecondition.dexpr
826 WeakestPrecondition.expr
827 WeakestPrecondition.expr_body
828 Semantics.interp_binop].
829 cbv [WeakestPrecondition.literal dlet.dlet].
830 match goal with
831 | |- context[word.add ?x (word.of_Z ?y)] =>
832 progress match x with
833 | word.of_Z _ => idtac
834 | word.add _ _ => idtac
835 | _ => rewrite <-(word.of_Z_unsigned x)
836 end
837 end.
838 rewrite <-!word.ring_morph_add.
839 f_equal. clear; lia. }
840 { match goal with
841 H : context [array _ _ (word.add (word.add _ _) _) _] |- _ =>
842 refine (Lift1Prop.subrelation_iff1_impl1 _ _ _ _ _ H); clear H
843 end.
844 rewrite map_app, array_append. cbn [map array].
845 cbn [Compilers.base_interp] in *. (* simplifies implicit types *)
846 match goal with
847 | |- Lift1Prop.iff1 ?L ?R =>
848 let la := match L with
849 context [truncated_scalar _ ?la] => la end in
850 let ra := match R with
851 context [truncated_scalar _ ?ra] => ra end in
852 replace ra with la
853 by (apply word.unsigned_inj;
854 rewrite !word.unsigned_add, !word.unsigned_of_Z;
855 cbv [word.wrap]; Modulo.pull_Zmod; f_equal; lia)
856 end.
857 cancel. } } }
858 cbv beta in *; cleanup; subst.
859 repeat match goal with |- _ /\ _ => split end;
860 eauto.
861 rewrite app_cons_app_app.
862 eassumption. }
863 Qed.
864
865 Lemma store_return_values_correct {t} init_locals :
866 forall (retnames_local : base_ltype t)
867 (retnames_mem : base_ltype t)
868 (retsizes : base_access_sizes t)
869 (vset : set string)
870 (rets : base.interp t)
871 functions
872 tr
873 (locals : locals)
874 (mem : mem)
875 (R : _ -> Prop),
876 let out := store_return_values retnames_local retnames_mem retsizes in
877 let retlengths := list_lengths_from_value rets in
878 sep (lists_reserved retlengths
879 (base_rtype_of_ltype retnames_mem)
880 retsizes init_locals) R mem ->
881 map.undef_on
882 init_locals
883 (union vset (varname_set_listexcl retnames_mem)) ->
884 map.only_differ init_locals vset locals ->
885 (* rets are stored in local retnames *)
886 locally_equivalent_base
887 rets (base_rtype_of_ltype retnames_local) locals ->
888 (* retnames are disjoint *)
889 disjoint (varname_set_base retnames_local)
890 (varname_set_base retnames_mem) ->
891 (* retnames don't contain duplicates *)
892 NoDup (flatten_base_ltype retnames_mem) ->
893 (* rets bounds obey access sizes *)
894 within_base_access_sizes rets retsizes ->
895 (* access sizes not larger than integer width *)
896 base_access_sizes_good retsizes ->
897 (* translated function produces equivalent results *)
898 WeakestPrecondition.cmd
899 functions
900 (snd out)
901 tr mem locals
902 (fun tr' mem' locals' =>
903 tr = tr' /\
904 retlengths = fst out /\
905 map.only_differ
906 locals (varname_set_listexcl retnames_mem) locals' /\
907 sep (equivalent_base
908 rets (base_rtype_of_ltype retnames_mem) retsizes locals')
909 R mem').
910 Proof.
911 cbv zeta.
912 induction t;
913 cbn [fst snd store_return_values lists_reserved
914 locally_equivalent_base equivalent_base
915 varname_set_base varname_set_listexcl
916 base_rtype_of_ltype rep.rtype_of_ltype
917 rep.equiv rep.varname_set rep.Z
918 base_access_sizes_good within_base_access_sizes
919 flatten_base_ltype list_lengths_from_value
920 ]; break_match; intros; sepsimpl; [ | | ].
921 { (* base_Z *)
922 repeat straightline.
923 eexists; split; [eassumption|].
924 cbv [dlet.dlet]; split; [reflexivity|].
925 split; [reflexivity|].
926 split;
927 [ solve [eauto using @only_differ_sym, @only_differ_put with typeclass_instances] | ].
928 sepsimpl. eexists; sepsimpl; try eassumption; [ ].
929 cbv [WeakestPrecondition.dexpr
930 WeakestPrecondition.expr
931 WeakestPrecondition.expr_body
932 WeakestPrecondition.get] in *.
933 cleanup; subst.
934 rewrite map.get_put_same.
935 eexists; split; reflexivity. }
936 { (* prod *)
937 repeat straightline.
938 match goal with H : _ |- _ =>
939 pose proof H;
940 apply NoDup_app_iff in H; cleanup end.
941 repeat match goal with
942 | _ => progress cleanup
943 | H : disjoint (union _ _) _ |- _ =>
944 apply disjoint_union_l_iff in H
945 | H : disjoint _ (union _ _) |- _ =>
946 apply disjoint_union_r_iff in H
947 | H : map.undef_on _ (union _ _) |- _ =>
948 apply undef_on_union_iff in H
949 end.
950 eapply Proper_cmd;
951 [ repeat intro
952 | eapply IHt1; try ecancel_assumption;
953 try (apply undef_on_union_iff; split);
954 solve [eauto] ].
955 cbv beta in *. cleanup; subst.
956 eapply Proper_cmd;
957 [ repeat intro | ].
958 2:{ eapply IHt2 with
959 (vset:=union vset (varname_set_listexcl (fst retnames_mem)));
960 try ecancel_assumption; eauto.
961 { apply undef_on_union_iff; split; eauto.
962 apply undef_on_union_iff; split; eauto. }
963 { rewrite union_comm.
964 eapply only_differ_trans; eauto. }
965 { cbv [locally_equivalent].
966 eapply equivalent_only_differ; eauto with equiv.
967 apply disjoint_sym.
968 eapply subset_disjoint_r;
969 eauto using varname_set_listexcl_subset. } }
970 cbv beta in *. cleanup; subst.
971 repeat match goal with H : list_lengths_from_value _ = _ |- _ =>
972 rewrite H end.
973 repeat match goal with |- _ /\ _ => split end;
974 eauto 10 using @only_differ_sym, @only_differ_trans with typeclass_instances.
975 use_sep_assumption. cancel.
976 (* TODO: why doesn't cancel handle the below? *)
977 cancel_seps_at_indices 0 1; [ reflexivity | ].
978 eapply equivalent_only_differ_iff1; eauto with equiv.
979 eapply disjoint_sym.
980 eapply subset_disjoint_r;
981 eauto using varname_set_listexcl_subset; [ ].
982 rewrite !varname_set_flatten.
983 eapply NoDup_disjoint; eauto using string_dec. }
984 { (* base_listZ *)
985 repeat straightline.
986 repeat match goal with p := _ |- _ => subst p end.
987 repeat match goal with
988 | H : map.undef_on _ (union _ _) |- _ =>
989 apply undef_on_union_iff in H
990 end.
991 cbn [rep.Z rep.listZ_mem rep.equiv rep.rtype_of_ltype] in *.
992 sepsimpl.
993 eapply Proper_cmd;
994 [ repeat intro | ].
995 2:{
996 eapply store_list_correct with
997 (rets:=rets) (rets1:=[]) (rets2:=rets)
998 (value_names1:=nil); auto using List.length_nil; [ | ].
999 { cbn [rep.equiv rep.rtype_of_ltype rep.listZ_local] in *.
1000 eapply Forall.eq_length_Forall2, Forall.Forall2_map_r_iff.
1001 eauto. }
1002 { apply sep_assoc.
1003 eapply Proper_sep_iff1;
1004 [ apply equiv_nil_iff1 | reflexivity | ].
1005 sepsimpl. eexists.
1006 apply sep_ex1_l.
1007 eexists; sepsimpl;
1008 [ | eapply expr_only_differ_undef; solve [eauto] | ];
1009 [ reflexivity | ].
1010 cbn [lists_reserved equivalent rep.equiv rep.listZ_mem rep.Z].
1011 sepsimpl. eexists.
1012 sepsimpl; eauto.
1013 eexists; sepsimpl; try ecancel_assumption; eauto.
1014 cbn [WeakestPrecondition.dexpr
1015 WeakestPrecondition.expr
1016 WeakestPrecondition.expr_body] in *.
1017 eexists; sepsimpl; try eassumption; [ ].
1018 eexists; sepsimpl; try eassumption; [ ].
1019 eapply WP_get_only_differ_undef; eauto.
1020 eapply Proper_get; [ repeat intro | eassumption ].
1021 cbn [Semantics.interp_binop].
1022 cbv [WeakestPrecondition.literal dlet.dlet].
1023 subst.
1024 apply word.unsigned_inj.
1025 rewrite word.unsigned_add, word.unsigned_of_Z.
1026 rewrite Nat2Z.inj_mul. change (Z.of_nat 0) with 0%Z.
1027 autorewrite with zsimplify_fast.
1028 cbv [word.wrap]. rewrite word.wrap_unsigned.
1029 reflexivity. } }
1030 cbv beta in *. cleanup; subst.
1031 cbn [rep.rtype_of_ltype rep.listZ_local rep.Z] in *.
1032 match goal with
1033 | H : rep.equiv ?x ?y _ _ _ |- _ =>
1034 assert (length x = length y)
1035 by (eapply Forall.eq_length_Forall2; apply H)
1036 end.
1037 rewrite !map_length in *.
1038 intuition trivial.
1039 eapply only_differ_sym, only_differ_empty. }
1040 Qed.
1041 End LoadStoreList.