Debian Codegraph @

src/Bedrock/Field/Translation/Proofs/Func.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.Syntax.
6 Require Import bedrock2.ProgramLogic.
7 Require Import bedrock2.Map.Separation.
8 Require Import bedrock2.Map.SeparationLogic.
9 Require Import bedrock2.WeakestPreconditionProperties.
10 Require Import coqutil.Word.Interface.
11 Require Import coqutil.Word.Properties.
12 Require Import coqutil.Map.Interface.
13 Require Import coqutil.Map.Properties.
14 Require Import coqutil.Datatypes.List.
15 Require Import coqutil.Datatypes.PropSet.
16 Require Import Crypto.Bedrock.Field.Common.Types.
17 Require Import Crypto.Bedrock.Field.Common.Tactics.
18 Require Import Crypto.Bedrock.Field.Common.Util.
19 Require Import Crypto.Bedrock.Field.Translation.Proofs.Cmd.
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.Proofs.LoadStoreList.
26 Require Import Crypto.Bedrock.Field.Translation.Func.
27 Require Import Crypto.Bedrock.Field.Translation.Flatten.
28 Require Import Crypto.Bedrock.Field.Translation.LoadStoreList.
29 Require Import Crypto.Language.API.
30 Require Import Crypto.Util.ListUtil.
31 Require Import Crypto.Util.Option.
32 Import ListNotations. Local Open Scope Z_scope.
33
34 Import API.Compilers.
35 Import Wf.Compilers.expr.
36 Import Types.Notations.
37
38 Section Func.
39 Context
40 {width BW word mem locals ext_spec varname_gen error}
41 `{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}.
42 Context {ok : ok}.
43
44 Local Existing Instance rep.Z.
45
46 Inductive valid_func : forall {t}, @API.expr (fun _ => unit) t -> Prop :=
47 | validf_Abs :
48 forall {s d} f, valid_func (f tt) ->
49 valid_func (expr.Abs (s:=type.base s) (d:=d) f)
50 | validf_base :
51 forall {b} e, valid_cmd e -> valid_func (t:=type.base b) e
52 .
53
54 Lemma translate_func'_correct {t}
55 (* three exprs, representing the same Expr with different vars *)
56 (e0 : @API.expr (fun _ => unit) t)
57 (e1 : @API.expr API.interp_type t)
58 (e2 : @API.expr ltype t)
59 (* expressions are valid input to translate_func' *)
60 (e0_valid : valid_func e0)
61 (* context list (consists only of arguments) *)
62 (G : list _) :
63 (* exprs are all related *)
64 wf3 G e0 e1 e2 ->
65 forall (argnames : type.for_each_lhs_of_arrow ltype t)
66 (args : type.for_each_lhs_of_arrow API.interp_type t)
67 (nextn : nat),
68 (* ret1 := fiat-crypto interpretation of e1 applied to args1 *)
69 let ret1 : base.interp (type.final_codomain t) :=
70 type.app_curried (API.interp e1) args in
71 (* out := translation output for e2; triple of
72 (# varnames used, return values, cmd) *)
73 let out := translate_func' e2 nextn argnames in
74 let nvars := fst (fst out) in
75 let ret2 := base_rtype_of_ltype (snd (fst out)) in
76 let body := snd out in
77 (* look up variables in argnames *)
78 let argvalues :=
79 type.map_for_each_lhs_of_arrow rtype_of_ltype argnames in
80 (* argnames don't contain variables we could later overwrite *)
81 (forall n,
82 (nextn <= n)%nat ->
83 ~ varname_set_args argnames (varname_gen n)) ->
84 (* G doesn't contain variables we could overwrite *)
85 (forall n,
86 (nextn <= n)%nat ->
87 ~ context_varname_set G (varname_gen n)) ->
88 forall (tr : Semantics.trace)
89 (locals : locals)
90 (mem : mem)
91 functions,
92 (* locals doesn't contain variables we could overwrite *)
93 (forall n nvars,
94 (nextn <= n)%nat ->
95 map.undef_on locals (used_varnames (varname_gen:=varname_gen) n nvars)) ->
96 (* argument values are equivalent *)
97 locally_equivalent_args args argvalues locals ->
98 (* contexts are equivalent; for every variable in the context list G,
99 the fiat-crypto and bedrock2 results match *)
100 context_equiv G locals ->
101 (* executing translation output is equivalent to interpreting e *)
102 WeakestPrecondition.cmd
103 functions
104 body tr mem locals
105 (fun tr' mem' locals' =>
106 tr = tr' /\
107 mem = mem' /\
108 subset (varname_set_base (snd (fst out)))
109 (used_varnames (varname_gen:=varname_gen) nextn nvars) /\
110 Interface.map.only_differ
111 locals (used_varnames (varname_gen:=varname_gen) nextn nvars) locals' /\
112 locally_equivalent_base
113 (listZ:=rep.listZ_local) ret1 ret2 locals').
114 Proof.
115 revert G. cbv zeta.
116 induction e0_valid; intros *.
117 { (* Abs *)
118 inversion 1; cleanup_wf.
119 cbn [translate_func']; intros.
120 match goal with
121 | H : context [varname_set_args] |- _ =>
122 cbn [varname_set_args] in H;
123 setoid_rewrite not_union_iff in H;
124 match type of H with
125 forall x y, ?P /\ ?Q =>
126 assert (forall x y, P) by (apply H);
127 assert (forall x y, Q) by (apply H);
128 clear H
129 end
130 end.
131 destruct argnames.
132 cbv [locally_equivalent_args] in *.
133 cbn [fst snd equivalent_args
134 type.map_for_each_lhs_of_arrow] in *.
135 cleanup. sepsimpl.
136 eapply IHe0_valid; eauto; [ | ].
137 { destruct args; cbn [fst snd].
138 cbn [context_varname_set]; intros.
139 apply not_union_iff; eauto with lia. }
140 { eapply Forall_cons; eauto. } }
141 { (* base case *)
142 inversion 1; cleanup_wf;
143 cbv [translate_func']; intros.
144 all:eapply Proper_cmd;
145 [ repeat intro
146 | eapply (translate_cmd_correct (t:=type.base _));
147 solve [eauto] ];
148 cbv beta in *; cleanup; subst; tauto. }
149 Qed.
150
151 Lemma look_up_return_values {t} :
152 forall (ret : base.interp t)
153 (retnames : listexcl_base_ltype (listZ:=rep.listZ_mem) t)
154 (retsizes : base_access_sizes t)
155 (locals : locals)
156 (mem : mem)
157 (R : _ -> Prop),
158 sep (equivalent_listexcl
159 ret (map_listexcl (fun t => base_rtype_of_ltype (t:=t)) retnames)
160 retsizes locals) R mem ->
161 WeakestPrecondition.list_map
162 (WeakestPrecondition.get locals)
163 (flatten_listexcl_base_ltype retnames)
164 (fun flat_rets =>
165 sep (equivalent_listexcl_flat_base ret flat_rets retsizes)
166 R mem).
167 Proof.
168 cbv [flatten_retnames].
169 induction t;
170 cbn [flatten_base_ltype
171 flatten_listexcl_base_ltype
172 equivalent_listexcl_flat_base
173 equivalent_listexcl
174 flatten_rets base_rtype_of_ltype rep.rtype_of_ltype
175 flatten_base_rtype equivalent_flat_base
176 rep.listZ_mem rep.Z equivalent]; break_match;
177 repeat match goal with
178 | _ => progress (intros; cleanup)
179 | _ => progress subst
180 | _ => progress sepsimpl
181 | _ => progress cbn [rep.equiv rep.listZ_mem rep.Z] in *
182 | _ => progress cbn [List.hd
183 WeakestPrecondition.list_map
184 WeakestPrecondition.list_map_body
185 WeakestPrecondition.literal
186 WeakestPrecondition.dexpr
187 WeakestPrecondition.dexprs
188 WeakestPrecondition.expr
189 WeakestPrecondition.expr_body]
190 | _ => rewrite word.of_Z_unsigned
191 | H : WeakestPrecondition.dexpr _ _ _ _ |- _ => destruct H
192 | |- WeakestPrecondition.get _ _ _ => eexists; split; [ eassumption | ]
193 | |- WeakestPrecondition.literal _ _ =>
194 cbv [WeakestPrecondition.literal dlet.dlet]
195 | |- Lift1Prop.ex1 _ _ => eexists; sepsimpl; eauto; [ ]
196 | H : False |- _ => tauto
197 | _ => reflexivity
198 | _ => solve [eauto]
199 end.
200 { apply list_map_app_iff;
201 [ split; intros;
202 (eapply Proper_get; [|eassumption]); repeat intro;
203 match goal with H : _ |- _ => apply H; solve [eauto] end | ].
204 fold (@Language.Compilers.base.interp) in *.
205 eapply Proper_list_map; [ solve [apply Proper_get] | repeat intro | ].
206 2:{ eapply IHt1;
207 (* TODO: why does ecancel_assumption not work here? *)
208 use_sep_assumption; rewrite sep_assoc; reflexivity. }
209 cbv beta in *.
210 eapply Proper_list_map; [ solve [apply Proper_get] | repeat intro | ].
211 2:{ eapply IHt2.
212 (* TODO: why does ecancel_assumption not work here? *)
213 use_sep_assumption; rewrite sep_comm,sep_assoc; reflexivity. }
214 cbv beta in *.
215 apply sep_ex1_l.
216 match goal with |- context[List.firstn _ (?x ++ ?y)] =>
217 exists (length x) end.
218 rewrite firstn_app_sharp, skipn_app_sharp by reflexivity.
219 ecancel_assumption. }
220 Qed.
221
222 Lemma equivalent_flat_base_iff1 {t} :
223 forall (names : base_ltype t)
224 (values : base.interp t)
225 (sizes : base_access_sizes t)
226 (flat_values : list word)
227 (locals locals' : locals),
228 NoDup (flatten_base_ltype names) ->
229 map.putmany_of_list_zip
230 (flatten_base_ltype names) flat_values locals = Some locals' ->
231 Lift1Prop.iff1
232 (equivalent_flat_base values flat_values sizes)
233 (equivalent_base values (base_rtype_of_ltype names) sizes locals').
234 Proof.
235 induction t;
236 cbn [rep.Z rep.equiv base_rtype_of_ltype
237 equivalent_base equivalent_flat_base
238 flatten_base_ltype];
239 break_match; cbn [fst snd]; intros; try reflexivity; [ | | ].
240 all:match goal with
241 H : _ |- _ =>
242 pose proof H;
243 eapply map.putmany_of_list_zip_sameLength in H;
244 cbn [length] in H
245 end.
246 { repeat intro. rewrite sep_emp_l.
247 destruct flat_values as [| ? [|? ?] ]; cbn [length] in *; try lia.
248 cbv [emp List.hd
249 WeakestPrecondition.literal dlet.dlet
250 WeakestPrecondition.get WeakestPrecondition.dexpr
251 WeakestPrecondition.expr WeakestPrecondition.expr_body].
252 repeat match goal with
253 | _ => progress sepsimpl
254 | _ => progress subst
255 | _ => progress cbn [map.putmany_of_list_zip] in *
256 | H : Some _ = Some _ |- _ => inversion H; clear H; subst
257 | _ => rewrite map.get_put_same, word.of_Z_unsigned
258 | _ => split; intros; cleanup; subst
259 | _ => eexists; sepsimpl; [ reflexivity .. | ]
260 | _ => solve [eauto]
261 end. }
262 { match goal with
263 | H : _ |- _ =>
264 rewrite putmany_of_list_zip_app_l in H;
265 pose proof H;
266 rewrite putmany_of_list_zip_bind_comm in H by auto
267 end.
268 match goal with
269 H : NoDup (_ ++ _) |- _ =>
270 pose proof H;
271 apply NoDup_app_iff in H; cleanup
272 end.
273 cbv [Option.bind] in *.
274 break_match_hyps; try congruence.
275 repeat intro; split; intros.
276 { match goal with
277 | H : Lift1Prop.ex1 _ _ |- _ => destruct H end.
278 eapply Proper_sep_iff1;
279 [ symmetry; eapply IHt1; eassumption
280 | symmetry; eapply IHt2; eassumption | ].
281 erewrite <-flatten_base_samelength by ecancel_assumption.
282 rewrite firstn_length_firstn, skipn_length_firstn.
283 assumption. }
284 { eexists.
285 eapply Proper_sep_iff1;
286 [ eapply IHt1; eassumption
287 | eapply IHt2; eassumption | ].
288 eapply Proper_sep_iff1; [ | reflexivity | ].
289 { eapply (equivalent_only_differ_iff1
290 ltac:(eapply equiv_listZ_only_differ_mem)
291 _ locals');
292 eauto using @only_differ_sym, @map.only_differ_putmany with typeclass_instances.
293 symmetry.
294 eapply disjoint_sameset; eauto using varname_set_flatten.
295 apply NoDup_disjoint; eauto using string_dec. }
296 { eauto. } } }
297 { repeat intro. rewrite sep_emp_l.
298 destruct flat_values as [| ? [|? ?] ]; cbn [length] in *; try lia.
299 cbv [List.hd
300 rep.equiv rep.rtype_of_ltype rep.listZ_mem rep.Z
301 WeakestPrecondition.literal dlet.dlet
302 WeakestPrecondition.get WeakestPrecondition.dexpr
303 WeakestPrecondition.expr WeakestPrecondition.expr_body].
304 match goal with
305 H : map.putmany_of_list_zip _ _ _ = _ |- _ =>
306 cbn [map.putmany_of_list_zip] in H; inversion H; clear H; subst
307 end.
308 split; intros;
309 repeat match goal with
310 | _ => progress sepsimpl
311 | _ => progress subst
312 | H : Some _ = Some _ |- _ =>
313 inversion H; clear H; subst
314 | H : _ |- _ => rewrite word.of_Z_unsigned in H
315 | H : _ |- _ => rewrite map.get_put_same in H
316 | _ => rewrite map.get_put_same
317 | _ => rewrite word.of_Z_unsigned
318 | |- Lift1Prop.ex1 _ _ => eexists
319 | |- exists _, _ => eexists
320 | |- _ /\ _ => split
321 | _ => solve [eauto]
322 | _ => congruence
323 end. }
324 Qed.
325
326 (* When arguments are loaded into initial locals, the new argument names map
327 to the correct values *)
328 Lemma equivalent_flat_args_iff1 {t} :
329 forall (argnames : type.for_each_lhs_of_arrow ltype t)
330 (args : type.for_each_lhs_of_arrow API.interp_type t)
331 (argsizes : type.for_each_lhs_of_arrow access_sizes t)
332 (flat_args : list word)
333 (locals locals' : locals),
334 NoDup (flatten_argnames argnames) ->
335 map.putmany_of_list_zip
336 (flatten_argnames argnames) flat_args locals = Some locals' ->
337 let argvalues :=
338 type.map_for_each_lhs_of_arrow rtype_of_ltype argnames in
339 Lift1Prop.iff1
340 (equivalent_flat_args args flat_args argsizes)
341 (equivalent_args args argvalues argsizes locals').
342 Proof.
343 induction t;
344 cbn [equivalent_args
345 equivalent_flat_args
346 flatten_argnames type.map_for_each_lhs_of_arrow];
347 break_match; intros; try reflexivity; [ | ].
348 all:match goal with
349 H : _ |- _ =>
350 pose proof H;
351 eapply map.putmany_of_list_zip_sameLength in H
352 end.
353 { destruct flat_args; cbn [length] in *; try lia; [ ].
354 repeat intro; cbv [emp]; tauto. }
355 { destruct argnames. cbn [fst snd rtype_of_ltype] in *.
356 match goal with
357 | H : _ |- _ =>
358 pose proof H; rewrite putmany_of_list_zip_app_l in H
359 end.
360 match goal with
361 | H : NoDup (_ ++ _) |- _ =>
362 pose proof H; apply NoDup_app_iff in H; cleanup
363 end.
364 cbv [Option.bind] in *. break_match_hyps; try congruence.
365 repeat intro; split; intros.
366 { repeat match goal with
367 | _ => progress cleanup
368 | H : Lift1Prop.ex1 _ _ |- _ => destruct H
369 | H: _ |- _ =>
370 erewrite <-flatten_base_samelength in H by eauto;
371 rewrite ?firstn_length_firstn, ?skipn_length_firstn in H
372 end.
373 split; eauto.
374 { eexists.
375 eapply Proper_sep_iff1;
376 [ | reflexivity | eassumption ].
377 rewrite equivalent_flat_base_iff1 by eauto.
378 eapply equivalent_only_differ_iff1;
379 eauto using @only_differ_sym, @map.only_differ_putmany
380 with typeclass_instances equiv; [ ].
381 rewrite varname_set_flatten; symmetry;
382 apply NoDup_disjoint; eauto using string_dec. }
383 { eapply IHt2; eassumption. } }
384 { cleanup; subst.
385 eexists; split; eauto.
386 { eexists.
387 eapply Proper_sep_iff1;
388 [ | reflexivity | eassumption ].
389 rewrite equivalent_flat_base_iff1 by eauto.
390 eapply equivalent_only_differ_iff1;
391 eauto using @only_differ_sym, @map.only_differ_putmany
392 with typeclass_instances equiv; [ ].
393 rewrite varname_set_flatten; symmetry;
394 apply NoDup_disjoint; eauto using string_dec. }
395 { eapply IHt2; eassumption. } } }
396 Qed.
397
398 Lemma equivalent_listonly_flat_iff1 {t} :
399 forall (names : listonly_base_ltype t) (values : list word)
400 (sizes : base_access_sizes t)
401 (l locals init_locals : locals)
402 (vset : set string) (x : base.interp t),
403 NoDup (flatten_listonly_base_ltype names) ->
404 map.putmany_of_list_zip (flatten_listonly_base_ltype names) values l = Some init_locals ->
405 map.only_differ init_locals vset locals ->
406 disjoint vset (of_list (flatten_listonly_base_ltype names)) ->
407 Lift1Prop.iff1
408 (equivalent_listonly
409 x (map_listonly base_rtype_of_ltype names) sizes locals)
410 (equivalent_listonly_flat_base x values sizes).
411 Proof.
412 induction t;
413 cbn [fst snd equivalent_listonly equivalent_listonly_flat_base
414 flatten_listonly_base_ltype
415 equivalent_flat_base map_listonly];
416 intros; break_match; try reflexivity; [ | | ].
417 { cbn [map.putmany_of_list_zip] in *.
418 break_match_hyps; try congruence; [ ].
419 split; intros; sepsimpl; tauto. }
420 { destruct names. cbn [fst snd] in *.
421 match goal with
422 | H : _ |- _ =>
423 pose proof H; rewrite putmany_of_list_zip_app_l in H
424 end.
425 match goal with
426 | H : NoDup (_ ++ _) |- _ =>
427 pose proof H; apply NoDup_app_iff in H; cleanup
428 end.
429 cbv [Option.bind] in *. break_match_hyps; try congruence; [ ].
430 match goal with
431 | H : _ |- _ => rewrite of_list_app in H;
432 rewrite disjoint_union_r_iff in H
433 end.
434 cleanup.
435
436 match goal with
437 | H : map.putmany_of_list_zip _ (List.firstn _ _) _ = Some _ |- _ =>
438 pose proof H; apply map.only_differ_putmany in H
439 end.
440 match goal with
441 | H : map.putmany_of_list_zip _ (List.skipn _ _) _ = Some _ |- _ =>
442 pose proof H; apply map.only_differ_putmany in H
443 end.
444
445 erewrite IHt1; pose only_differ_trans; eauto ; [ |
446 apply disjoint_union_l_iff; intuition trivial;
447 symmetry; eapply NoDup_disjoint; eauto ].
448 erewrite IHt2 by eauto using only_differ_trans.
449 split; intros; [ eexists; eassumption | ].
450 sepsimpl.
451 erewrite <-flatten_listonly_samelength by ecancel_assumption.
452 rewrite firstn_length_firstn, skipn_length_firstn.
453 fold (@Language.Compilers.base.interp) in *.
454 ecancel_assumption. }
455 { cbn [map.putmany_of_list_zip] in *.
456 break_match_hyps; try congruence; [ ].
457 match goal with H : Some _ = Some _ |- _ =>
458 inversion H; subst; clear H end.
459 cbn [rep.Z rep.listZ_mem base_rtype_of_ltype rep.rtype_of_ltype
460 rep.equiv length List.hd].
461 cbn [WeakestPrecondition.dexpr
462 WeakestPrecondition.expr WeakestPrecondition.expr_body].
463 cbv [WeakestPrecondition.literal dlet.dlet].
464 repeat match goal with
465 | H : _ |- _ => rewrite map.get_put_same in H
466 | H : context [of_list [_] ] |- _ =>
467 rewrite of_list_singleton in H
468 | H : map.only_differ (map.put _ ?k ?v) _ ?m' |- _ =>
469 eapply only_differ_notin in H;
470 [ | eapply disjoint_singleton_r_iff; eassumption ]
471 end.
472 cbv [WeakestPrecondition.get].
473 split; intros; sepsimpl; subst; try reflexivity.
474 { eexists. sepsimpl; eauto; [ ].
475 rewrite !word.of_Z_unsigned in *.
476 eexists; sepsimpl; eauto; [ ].
477 eexists; sepsimpl; eauto; [ ].
478 congruence. }
479 { eexists; sepsimpl; eauto; [ ].
480 eexists; sepsimpl; eauto; [ ].
481 eexists; sepsimpl; eauto; [ ].
482 eexists; split; eauto; [ ].
483 apply word.of_Z_unsigned. } }
484 Qed.
485
486 Lemma translate_func_correct {t}
487 (e : API.Expr t)
488 (* expressions are valid input to translate_func *)
489 (e_valid : valid_func (e _)) :
490 Wf e ->
491 forall (fname : string)
492 (retnames : base_ltype (type.final_codomain t))
493 (retsizes : base_access_sizes (type.final_codomain t))
494 (argnames : type.for_each_lhs_of_arrow ltype t)
495 (arglengths : type.for_each_lhs_of_arrow list_lengths t)
496 (argsizes : type.for_each_lhs_of_arrow access_sizes t)
497 (args : type.for_each_lhs_of_arrow API.interp_type t),
498 (* rets := fiat-crypto interpretation of e1 applied to args *)
499 let rets : base.interp (type.final_codomain t) :=
500 type.app_curried (API.interp (e _)) args in
501 (* extract list lengths from fiat-crypto arguments/return values *)
502 arglengths = list_lengths_from_args args ->
503 let retlengths := list_lengths_from_value rets in
504 (* out := translation output for e2; triple of
505 (function arguments, function return variable names, body) *)
506 let out := translate_func
507 e argnames arglengths argsizes retnames retsizes in
508 forall tr
509 (mem : mem)
510 (flat_args : list word)
511 (out_ptrs : list word)
512 (argvalues : list word)
513 (functions : Semantics.env)
514 (R : _ -> Prop),
515 (* argument values are the concatenation of true argument values
516 and output pointer values *)
517 argvalues = out_ptrs ++ flat_args ->
518 length out_ptrs =
519 length (flatten_listonly_base_ltype
520 (fst (extract_listnames retnames))) ->
521 (* argnames don't contain variables we could later overwrite *)
522 (forall n, ~ varname_set_args argnames (varname_gen n)) ->
523 (* argument values are equivalent *)
524 equivalent_flat_args args flat_args argsizes mem ->
525 (* argnames don't have duplicates *)
526 NoDup (flatten_argnames argnames) ->
527 (* argument bounds are within allowed integer size *)
528 access_sizes_good_args argsizes ->
529 (* argument bounds are obeyed *)
530 within_access_sizes_args args argsizes ->
531 (* retnames don't contain variables we could later overwrite *)
532 (forall n, ~ varname_set_base retnames (varname_gen n)) ->
533 (* retnames don't have duplicates *)
534 NoDup (flatten_base_ltype retnames) ->
535 (* return value bounds are within allowed integer size *)
536 base_access_sizes_good retsizes ->
537 (* return value bounds are obeyed *)
538 within_base_access_sizes rets retsizes ->
539 (* argnames and retnames are disjoint *)
540 disjoint (varname_set_args argnames)
541 (varname_set_base retnames) ->
542 (* seplogic frame for return values *)
543 sep (lists_reserved_with_initial_context
544 retlengths argnames retnames retsizes argvalues) R mem ->
545 map.get functions fname = Some (fst out) ->
546 (* translated function produces equivalent results *)
547 WeakestPrecondition.call
548 functions fname tr mem argvalues
549 (fun tr' mem' flat_rets =>
550 tr = tr' /\
551 (* lengths of output lists match *)
552 retlengths = snd out /\
553 (* return values are equivalent *)
554 sep (sep (equivalent_listexcl_flat_base
555 rets flat_rets retsizes)
556 (equivalent_listonly_flat_base
557 rets out_ptrs retsizes))
558 R mem').
559 Proof.
560 cbv [translate_func]; intros. subst.
561 eapply start_func. 1: eassumption.
562 cbn [fst snd WeakestPrecondition.func].
563 match goal with
564 |- exists l, map.of_list_zip ?ks ?vs = Some l /\ _ =>
565 assert (NoDup ks);
566 [ | assert (exists m, map.of_list_zip ks vs = Some m);
567 [ | cleanup; eexists; split; [ eassumption | ] ] ]
568 end.
569 { apply disjoint_NoDup; eauto using flatten_listonly_NoDup.
570 eapply subset_disjoint_l;
571 [ | symmetry; rewrite <-varname_set_args_flatten; solve [eauto] ].
572 eapply flatten_listonly_subset. }
573 { eapply of_list_zip_app; try lia; [ ].
574 erewrite flatten_args_samelength; eauto. }
575 match goal with H : _ |- _ =>
576 pose proof H;
577 eapply (of_list_zip_flatten_argnames argnames) in H;
578 cleanup
579 end.
580 match goal with
581 | H : map.of_list_zip _ _ = Some _ |- _ =>
582 pose proof H;
583 cbv [map.of_list_zip] in H;
584 rewrite putmany_of_list_zip_app_l in H;
585 rewrite firstn_app_sharp, skipn_app_sharp in H
586 by eauto using flatten_args_samelength;
587 pose proof H; (* preserve original ordering *)
588 erewrite putmany_of_list_zip_bind_comm in H by eauto;
589 cbv [Option.bind] in *; repeat break_match_hyps; try congruence; [ ]
590 end.
591 cbn [WeakestPrecondition.cmd WeakestPrecondition.cmd_body].
592 eapply Proper_cmd; [ repeat intro | ].
593 2 : { eapply load_arguments_correct; try eassumption; eauto.
594 eapply equivalent_flat_args_iff1; eauto. }
595 cbv beta in *. cleanup; subst.
596 eapply Proper_cmd; [ repeat intro | ].
597 2 : { eapply @translate_func'_correct with (args:=args);
598 cbv [context_equiv]; intros; try apply Wf3_of_Wf; eauto; [ ].
599 eapply only_differ_disjoint_undef_on; eauto;
600 [ eapply used_varnames_disjoint; lia | ].
601 eapply putmany_of_list_zip_undef_on
602 with (ks:=flatten_argnames _); eauto;
603 [ rewrite <-varname_set_args_flatten;
604 symmetry; eapply disjoint_used_varnames_lt;
605 eauto with lia | ].
606 eapply putmany_of_list_zip_undef_on;
607 eauto using @undef_on_empty with typeclass_instances.
608 eapply subset_disjoint_l;
609 eauto using flatten_listonly_subset; [ ].
610 eapply disjoint_sym.
611 eapply disjoint_used_varnames_lt; eauto with lia. }
612 cbv beta in *. cleanup; subst.
613 eapply Proper_cmd; [ repeat intro | ].
614 2 : {
615 cbv [lists_reserved_with_initial_context] in *.
616 break_match_hyps; try congruence; [ ].
617 match goal with H : Some _ = Some _ |- _ =>
618 inversion H; clear H; subst end.
619 eapply store_return_values_correct;
620 eauto using @only_differ_trans with typeclass_instances.
621 { eapply of_list_zip_undef_on; eauto.
622 rewrite of_list_app.
623 rewrite <-varname_set_args_flatten.
624 repeat match goal with
625 | |- disjoint (union _ _) _ =>
626 apply disjoint_union_l_iff; split; auto
627 | |- disjoint _ (union _ _) =>
628 apply disjoint_union_r_iff; split; auto
629 | |- disjoint
630 (of_list
631 (flatten_listonly_base_ltype _))
632 (used_varnames _ _) =>
633 symmetry;
634 eapply subset_disjoint_r;
635 [ solve [apply flatten_listonly_subset] | ]
636 end;
637 (* solvers *)
638 try match goal with
639 | |- disjoint _ (varname_set_listexcl _) =>
640 eapply subset_disjoint_r;
641 solve [eauto using varname_set_listexcl_subset]
642 | |- disjoint (used_varnames _ _) _ =>
643 apply disjoint_used_varnames_lt; intros;
644 solve [eauto with lia]
645 | |- disjoint _ (used_varnames _ _) =>
646 symmetry; apply disjoint_used_varnames_lt; intros;
647 solve [eauto with lia]
648 | _ => solve [eauto using flatten_listonly_disjoint]
649 | _ => solve [symmetry; eauto using flatten_listonly_disjoint]
650 end. }
651 { eapply subset_disjoint_l; try eassumption.
652 eauto using disjoint_used_varnames_lt. } }
653
654 cbv beta in *. cleanup; subst.
655 match goal with H : list_lengths_from_value _ = _ |- _ =>
656 rewrite H end.
657 eapply Proper_list_map;
658 [ solve [apply Proper_get]
659 | | eapply look_up_return_values; eauto;
660 apply equivalent_extract_listnames; eauto ].
661 repeat intro; cleanup; subst; eauto.
662 split; eauto.
663 split; eauto.
664
665 use_sep_assumption.
666 cancel.
667
668 repeat match goal with H : NoDup (_ ++ _) |- _ =>
669 apply NoDup_app_iff in H end.
670 cleanup.
671
672 eapply equivalent_listonly_flat_iff1;
673 eauto 10 using @only_differ_sym, @map.only_differ_putmany,
674 @only_differ_trans with typeclass_instances; [ ].
675 repeat match goal with
676 |- disjoint (union _ _) _ =>
677 eapply disjoint_union_l_iff; split
678 | _ => rewrite <-varname_set_args_flatten
679 | _ =>
680 solve[eauto using flatten_listonly_disjoint,
681 subset_disjoint_r, flatten_listonly_subset,
682 disjoint_used_varnames_lt]
683 | _ =>
684 symmetry;
685 solve[eauto using flatten_listonly_disjoint,
686 subset_disjoint_r, flatten_listonly_subset,
687 disjoint_used_varnames_lt]
688 end.
689 Qed.
690 End Func.