Debian Codegraph @

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

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import List.
3 From Coq Require Import Lia.
4 From Coq Require Import String.
5 Require Import bedrock2.ProgramLogic.
6 Require Import bedrock2.Map.Separation.
7 Require Import bedrock2.Map.SeparationLogic.
8 Require Import bedrock2.WeakestPreconditionProperties.
9 Require Import coqutil.Map.Interface.
10 Require Import coqutil.Word.Interface.
11 Require Import coqutil.Datatypes.List.
12 Require Import coqutil.Datatypes.PropSet.
13 Require Import Crypto.Bedrock.Field.Common.Types.
14 Require Import Crypto.Bedrock.Field.Common.Tactics.
15 Require Import Crypto.Bedrock.Field.Common.Util.
16 Require Import Crypto.Bedrock.Field.Translation.Proofs.Expr.
17 Require Import Crypto.Bedrock.Field.Translation.Proofs.Equivalence.
18 Require Import Crypto.Bedrock.Field.Translation.Proofs.EquivalenceProperties.
19 Require Import Crypto.Bedrock.Field.Translation.Proofs.UsedVarnames.
20 Require Import Crypto.Bedrock.Field.Translation.Proofs.VarnameSet.
21 Require Import Crypto.Bedrock.Field.Translation.Cmd.
22 Require Import Crypto.Bedrock.Field.Translation.Expr.
23 Require Import Crypto.Language.API.
24 Require Import Crypto.Util.ListUtil.
25 Require Import Crypto.Util.Notations.
26 Import ListNotations. Local Open Scope Z_scope.
27
28 (* for [eauto with lia] *)
29 Require Import Crypto.BoundsPipeline.
30
31 Import API.Compilers.
32 Import Wf.Compilers.expr.
33 Import Types.Notations.
34
35 Section Cmd.
36 Context
37 {width BW word mem locals ext_spec varname_gen error}
38 `{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}.
39 Context {ok : ok}.
40
41 Local Existing Instance Types.rep.Z.
42 Local Existing Instance Types.rep.listZ_local.
43
44 Inductive valid_cmd :
45 forall {t}, @API.expr (fun _ => unit) t -> Prop :=
46 (* N.B. LetIn is split into cases so that only pairs of type_base and type_base are
47 allowed; this is primarily because we don't want lists on the LHS *)
48 | valid_LetIn_prod :
49 forall {a b c} x f,
50 valid_expr true x -> valid_cmd (f tt) ->
51 valid_cmd (expr.LetIn
52 (A:=type.base (base.type.prod
53 (base.type.type_base a) (base.type.type_base b)))
54 (B:=type.base c) x f)
55 | valid_LetIn_base :
56 forall {a b} x f,
57 valid_expr true x -> valid_cmd (f tt) ->
58 valid_cmd (expr.LetIn (A:=type.base (base.type.type_base a)) (B:=type.base b) x f)
59 | valid_cons :
60 forall x l,
61 valid_expr true x ->
62 valid_cmd l ->
63 valid_cmd
64 (expr.App
65 (expr.App
66 (expr.Ident
67 (ident.cons (t:=base.type.type_base base.type.Z))) x) l)
68 | valid_nil :
69 valid_cmd (expr.Ident (ident.nil (t:=base.type.type_base base.type.Z)))
70 | valid_inner :
71 forall {t} e,
72 valid_expr (t:=type.base t) true e ->
73 valid_cmd e
74 .
75
76 Lemma assign_list_correct :
77 forall (rhs : base_rtype base_listZ)
78 (xs : base.interp base_listZ)
79 (nextn : nat)
80 tr
81 (mem : mem)
82 (locals : locals)
83 functions,
84 (* rhs == x *)
85 locally_equivalent_base xs rhs locals ->
86 (* locals don't contain any variables we can overwrite *)
87 (forall n nvars,
88 (nextn <= n)%nat ->
89 map.undef_on locals (used_varnames (varname_gen:=varname_gen) n nvars)) ->
90 let out := assign_list nextn rhs in
91 let nvars := fst (fst out) in
92 let lhs := snd (fst out) in
93 WeakestPrecondition.cmd
94 functions
95 (snd out)
96 tr mem locals
97 (fun tr' mem' locals' =>
98 tr = tr'
99 /\ mem = mem'
100 /\ PropSet.sameset (varname_set_base lhs)
101 (used_varnames (varname_gen:=varname_gen) nextn nvars)
102 /\ map.only_differ locals (varname_set_base lhs) locals'
103 /\ locally_equivalent_base xs (base_rtype_of_ltype lhs) locals').
104 Proof.
105 cbn [locally_equivalent_base equivalent_base rep.equiv rep.listZ_local].
106 induction rhs; intros; cbn [assign_list fst snd].
107 { repeat straightline.
108 repeat match goal with
109 | |- _ /\ _ => split
110 | _ => eassumption
111 | _ => apply only_differ_empty
112 | _ => reflexivity
113 end. }
114 { match goal with
115 H : Forall2 _ ?x (_ :: _) |- _ =>
116 destruct x; inversion H; subst; clear H; [ ]
117 end.
118 cbn [rep.equiv rep.Z] in *. sepsimpl.
119 repeat straightline.
120 eexists; split; [ eapply expr_empty; eassumption | ].
121 eapply Proper_cmd; [ repeat intro | ].
122 2:{
123 eapply IHrhs; eauto.
124 { eapply Forall2_impl_strong; eauto.
125 intros; sepsimpl; [ lia .. | ].
126 eexists; sepsimpl; [ eassumption | ].
127 eapply expr_only_differ_undef; eauto.
128 rewrite used_varnames_1.
129 eauto using @only_differ_sym, @only_differ_put with typeclass_instances. }
130 { intros. apply put_undef_on; eauto with lia.
131 rewrite used_varnames_iff; intro; cleanup.
132 match goal with H : varname_gen _ = varname_gen _ |- _ =>
133 apply varname_gen_unique in H end.
134 lia. } }
135 cbv beta in *. cleanup; subst.
136 repeat match goal with |- _ /\ _ => split end;
137 try reflexivity.
138 { cbn [varname_set_base
139 rep.varname_set rep.listZ_local rep.Z fold_right] in *.
140 rewrite used_varnames_succ_low, add_union_singleton.
141 match goal with H : PropSet.sameset _ _ |- _ =>
142 rewrite H end.
143 reflexivity. }
144 { cbn [varname_set_base
145 rep.varname_set rep.listZ_local rep.Z fold_right] in *.
146 eauto 10 using @only_differ_sym, @only_differ_put, @only_differ_trans with typeclass_instances. }
147 { constructor; eauto; [ ].
148 sepsimpl; [ lia .. | ].
149 cbn [rep.rtype_of_ltype rep.Z].
150 eexists; sepsimpl; [ reflexivity | ].
151 eapply expr_untouched with (mem2:=map.empty); eauto;
152 [ | solve [apply dexpr_put_same] ].
153 match goal with H : PropSet.sameset _ _ |- _ =>
154 rewrite sameset_iff in H;
155 rewrite H
156 end.
157 rewrite used_varnames_iff; intro; cleanup.
158 match goal with H : varname_gen _ = varname_gen _ |- _ =>
159 apply varname_gen_unique in H end.
160 lia. } }
161 Qed.
162
163 Lemma assign_base_correct {t} :
164 forall (x : base.interp t)
165 (rhs : base_rtype t)
166 (nextn : nat)
167 tr
168 (mem : mem)
169 (locals : locals)
170 functions,
171 (* rhs == x *)
172 locally_equivalent_base x rhs locals ->
173 (* locals don't contain any variables we can overwrite *)
174 (forall n nvars,
175 (nextn <= n)%nat ->
176 map.undef_on locals (used_varnames (varname_gen:=varname_gen) n nvars)) ->
177 let out := assign_base nextn rhs in
178 let nvars := fst (fst out) in
179 let lhs := snd (fst out) in
180 WeakestPrecondition.cmd
181 functions
182 (snd out) tr mem locals
183 (fun tr' mem' locals' =>
184 tr = tr'
185 (* assign never stores anything -- mem unchanged *)
186 /\ mem = mem'
187 (* return values match the number of variables used *)
188 /\ PropSet.sameset (varname_set_base lhs)
189 (used_varnames (varname_gen:=varname_gen) nextn nvars)
190 (* new locals only differ in the values of LHS variables *)
191 /\ map.only_differ locals (varname_set_base lhs) locals'
192 (* evaluating lhs == x *)
193 /\ locally_equivalent_base x (base_rtype_of_ltype lhs) locals').
194 Proof.
195 cbv zeta. cbv [locally_equivalent_base].
196 induction t; cbn [assign_base equivalent_base fst snd];
197 break_match; intros; sepsimpl; [ | | ].
198 { (* base_Z *)
199 cbn [rep.Z rep.equiv rep.varname_set
200 rep.rtype_of_ltype
201 varname_set_base
202 base_rtype_of_ltype] in *.
203 sepsimpl. repeat straightline.
204 eexists; split;
205 [ apply expr_empty; eassumption | ].
206 repeat (split; [reflexivity | ]).
207 repeat match goal with |- _ /\ _ => split end;
208 try eexists; sepsimpl;
209 eauto using @dexpr_put_same, @only_differ_sym, @only_differ_put with typeclass_instances.
210 all:cbv [PropSet.singleton_set]; apply sameset_iff; intros.
211 all:rewrite used_varnames_iff; split; intros; cleanup; subst; eauto;
212 first [ eexists; eauto with lia
213 | f_equal; lia ]. }
214 { (* prod *)
215 repeat straightline.
216 eapply Proper_cmd; [ repeat intro | eapply IHt1; solve [eauto] ].
217 cbv beta in *; cleanup; subst.
218 eapply Proper_cmd; [ repeat intro | ].
219 2:{
220 eapply IHt2; eauto; [ | ].
221 { eapply equivalent_only_differ_undef; eauto;
222 try exact equiv_listZ_only_differ_undef_local.
223 match goal with H : PropSet.sameset _ _ |- _ =>
224 rewrite H end.
225 eauto. }
226 { intros.
227 eapply only_differ_disjoint_undef_on;
228 try eapply undef_on_subset;
229 eauto using used_varnames_subset with lia.
230 match goal with H : PropSet.sameset _ _ |- _ =>
231 rewrite H end.
232 apply used_varnames_disjoint; lia. } }
233 cbv beta in *; cleanup; subst.
234 cbn [varname_set_base base_rtype_of_ltype fst snd].
235 repeat match goal with |- _ /\ _ => split end;
236 eauto 10 using @only_differ_sym, @only_differ_trans with typeclass_instances; [ | ].
237 { rewrite used_varnames_union.
238 repeat match goal with H : PropSet.sameset _ _ |- _ =>
239 rewrite H end.
240 reflexivity. }
241 { apply sep_empty_iff; split; eauto; [ ].
242 eapply equivalent_only_differ; eauto with equiv.
243 repeat match goal with H : PropSet.sameset _ _ |- _ =>
244 rewrite H end.
245 symmetry.
246 apply used_varnames_disjoint; lia. } }
247 { (* base_listZ *)
248 eapply assign_list_correct; eauto. }
249 Qed.
250
251 Lemma assign_correct {t} :
252 forall (x : API.interp_type t)
253 (rhs : rtype t)
254 (nextn : nat)
255 tr
256 (mem : mem)
257 (locals : locals)
258 functions,
259 (* rhs == x *)
260 locally_equivalent x rhs locals ->
261 (* locals don't contain any variables we can overwrite *)
262 (forall n nvars,
263 (nextn <= n)%nat ->
264 map.undef_on locals (used_varnames (varname_gen:=varname_gen) n nvars)) ->
265 let out := assign nextn rhs in
266 let nvars := fst (fst out) in
267 let lhs := snd (fst out) in
268 WeakestPrecondition.cmd
269 functions
270 (snd out) tr mem locals
271 (fun tr' mem' locals' =>
272 tr = tr'
273 (* assign never stores anything -- mem unchanged *)
274 /\ mem = mem'
275 (* return values match the number of variables used *)
276 /\ PropSet.sameset (varname_set lhs)
277 (used_varnames (varname_gen:=varname_gen) nextn nvars)
278 (* new locals only differ in the values of LHS variables *)
279 /\ map.only_differ locals (varname_set lhs) locals'
280 (* evaluating lhs == x *)
281 /\ locally_equivalent x (rtype_of_ltype _ lhs) locals').
282 Proof.
283 destruct t;
284 cbn [locally_equivalent equivalent varname_set];
285 [ apply assign_base_correct | tauto ].
286 Qed.
287
288 (* if e is a valid_expr, it will hit the cases that call translate_expr *)
289 Lemma translate_cmd_valid_expr {t}
290 (e1 : @API.expr (fun _ => unit) t)
291 (e2 : @API.expr API.interp_type t)
292 (e3 : @API.expr ltype t)
293 G nextn :
294 valid_expr true e1 ->
295 wf3 G e1 e2 e3 ->
296 translate_cmd e3 nextn = assign nextn (translate_expr true e3).
297 Proof.
298 inversion 1; cleanup_wf; try reflexivity; intros.
299 all: repeat first [ reflexivity
300 | match goal with
301 | [ H : wf3 _ ?x _ _ |- _ ]
302 => assert_fails is_var x; inversion H; clear H; cleanup_wf
303 end ].
304 Qed.
305
306 Local Ltac simplify :=
307 repeat
308 first [ progress (intros; cleanup)
309 | progress
310 cbn [fst snd assign assign_base context_varname_set
311 varname_set varname_set_base
312 ltype rtype base_ltype base_rtype rtype_of_ltype
313 base_rtype_of_ltype rep.rtype_of_ltype
314 rep.equiv rep.listZ_local rep.Z
315 locally_equivalent equivalent
316 locally_equivalent_base equivalent_base
317 map Datatypes.length Compilers.ident_interp] in *
318 | match goal with |- _ /\ _ => split end ].
319
320 Local Ltac setsimplify :=
321 repeat match goal with
322 | _ => progress cbv [PropSet.union PropSet.of_list
323 PropSet.singleton_set PropSet.elem_of] in *
324 | H : PropSet.sameset _ _ |- _ => rewrite sameset_iff in H; rewrite H
325 end.
326
327 (* prove that context doesn't include overwritable variables *)
328 Local Ltac context_not_overwritable :=
329 repeat match goal with
330 | _ => progress (intros; cleanup)
331 | _ => progress
332 cbn [ltype base_ltype assign_base context_varname_set
333 varname_set_base varname_set fst snd] in *
334 | _ => progress setsimplify
335 | _ => apply Forall_cons; [ | solve [eauto with lia] ]
336 | _ => rewrite used_varnames_iff
337 | H : varname_gen _ = varname_gen _ |- _ =>
338 apply varname_gen_unique in H; subst
339 | |- ~ (_ \/ _) =>
340 let X := fresh in intro X; destruct X; cleanup
341 | H : context [context_varname_set] |- _ =>
342 eapply H; try eassumption; lia
343 | _ => lia
344 end.
345
346 (* prove that paired variable values in the context are equivalent *)
347 Local Ltac context_equiv_ok :=
348 repeat match goal with
349 | _ => progress (intros; cleanup)
350 | |- context_equiv (_ :: _) _ =>
351 apply Forall_cons; [ solve [eauto] | ]
352 | _ =>
353 eapply equivalent_not_in_context_forall;
354 eauto using disjoint_sameset, disjoint_used_varnames_lt
355 | _ => solve [subst; eauto]
356 end.
357
358 Local Ltac new_context_ok :=
359 match goal with
360 | |- context_equiv _ _ => context_equiv_ok
361 | _ => context_not_overwritable
362 end.
363
364 Local Ltac only_differ_ok :=
365 repeat match goal with
366 | _ => eapply only_differ_step; try eassumption; [ ]
367 | _ => eapply Proper_only_differ; eauto;
368 solve[symmetry; eauto]
369 end.
370
371 Lemma translate_cmd_correct {t}
372 (* three exprs, representing the same Expr with different vars *)
373 (e1 : @API.expr (fun _ => unit) t)
374 (e2 : @API.expr API.interp_type t)
375 (e3 : @API.expr ltype t)
376 (* e1 is valid input to translate_cmd *)
377 (e1_valid : valid_cmd e1)
378 (* context list *)
379 (G : list _) :
380 (* exprs are all related *)
381 wf3 G e1 e2 e3 ->
382 forall functions
383 (locals : locals)
384 (nextn : nat),
385 (* ret := fiat-crypto interpretation of e2 *)
386 let ret1 : API.interp_type t := API.interp e2 in
387 (* out := translation output for e3 *)
388 let out := translate_cmd e3 nextn in
389 let nvars := fst (fst out) in
390 let ret2 := rtype_of_ltype _ (snd (fst out)) in
391 let body := snd out in
392 (* G doesn't contain variables we could accidentally overwrite *)
393 (forall n,
394 (nextn <= n)%nat ->
395 ~ context_varname_set G (varname_gen n)) ->
396 (* locals don't contain any variables we can overwrite *)
397 (forall n nvars,
398 (nextn <= n)%nat ->
399 map.undef_on locals (used_varnames(varname_gen:=varname_gen) n nvars)) ->
400 forall tr
401 (mem : mem),
402 (* contexts are equivalent; for every variable in the context list G,
403 the fiat-crypto and bedrock2 results match *)
404 context_equiv G locals ->
405 (* executing translation output is equivalent to interpreting e *)
406 WeakestPrecondition.cmd
407 functions
408 body tr mem locals
409 (fun tr' mem' locals' =>
410 tr = tr' /\
411 mem = mem' /\
412 PropSet.subset
413 (varname_set (snd (fst out)))
414 (used_varnames(varname_gen:=varname_gen) nextn nvars) /\
415 map.only_differ
416 locals (used_varnames(varname_gen:=varname_gen) nextn nvars) locals' /\
417 locally_equivalent ret1 ret2 locals').
418 Proof.
419 revert e2 e3 G. cbv zeta.
420 induction e1_valid; try (inversion 1; [ ]).
421
422 (* inversion on wf3 leaves a mess; clean up hypotheses *)
423 all:repeat match goal with
424 | _ => progress cleanup_wf
425 | _ => progress cbn [varname_set]
426 | H : wf3 _ ?x ?y _ |- _ =>
427 (* for the cons case, repeatedly do inversion until the cons is exposed *)
428 progress match x with
429 context [Compilers.ident.cons] =>
430 progress match y with
431 | expr.App _ _ => idtac (* don't invert original, already-inverted one *)
432 | _ => inversion H; clear H
433 end
434 end
435 end.
436
437 (* simplify goals *)
438 all:repeat match goal with
439 | _ => progress (intros; cleanup)
440 | _ => progress cbv [Rewriter.Util.LetIn.Let_In] in *
441 | _ => erewrite translate_cmd_valid_expr by eauto
442 | _ => progress cbn [translate_cmd expr.interp type.app_curried
443 WeakestPrecondition.cmd
444 WeakestPrecondition.cmd_body] in *
445 | _ => eapply Proper_cmd;
446 [ repeat intro
447 | eapply assign_correct; eauto;
448 eapply translate_expr_correct; solve [eauto] ]
449 | _ => progress cbn [invert_expr.invert_pair_cps invert_expr.invert_AppIdent2_cps Option.bind invert_expr.invert_App2_cps invert_expr.invert_App_cps invert_expr.invert_Ident invert_expr.is_pair Compilers.invertIdent Option.bind translate_ident2_for_cmd Crypto.Util.Option.bind]
450 end.
451
452 { (* let-in (product of base types) *)
453 eapply Proper_cmd; [ repeat intro | ].
454 2: {
455 eapply IHe1_valid; clear IHe1_valid;
456 repeat match goal with
457 | _ => progress (intros; cleanup)
458 | _ => solve [new_context_ok]
459 | H : _ |- _ => solve [apply H]
460 | _ => congruence
461 end; [ ].
462 eapply only_differ_disjoint_undef_on; eauto with lia; [ ].
463 match goal with H : PropSet.sameset _ _ |- _ =>
464 rewrite H end.
465 apply used_varnames_disjoint; lia. }
466 { simplify; subst; eauto; only_differ_ok.
467 etransitivity; [ eassumption | ].
468 apply used_varnames_shift. } }
469 { (* let-in (base type) *)
470 eapply Proper_cmd; [ repeat intro | ].
471 2: {
472 eapply IHe1_valid; clear IHe1_valid;
473 repeat match goal with
474 | _ => progress (intros; cleanup)
475 | _ => solve [new_context_ok]
476 | H : _ |- _ => solve [apply H]
477 | _ => congruence
478 end; [ ].
479 eapply only_differ_disjoint_undef_on; eauto with lia; [ ].
480 match goal with H : PropSet.sameset _ _ |- _ =>
481 rewrite H end.
482 apply used_varnames_disjoint; lia. }
483 { simplify; subst; eauto; only_differ_ok.
484 etransitivity; [ eassumption | ].
485 apply used_varnames_shift. } }
486 { (* cons *)
487 eapply Proper_cmd; [ repeat intro | ].
488 2: {
489 eapply IHe1_valid with (G:=G); clear IHe1_valid;
490 repeat match goal with
491 | _ => progress (intros; cleanup)
492 | H : _ |- _ => solve [apply H]
493 | _ => solve [new_context_ok]
494 | _ => congruence
495 end; [ ].
496 eapply only_differ_disjoint_undef_on; eauto with lia; [ ].
497 match goal with H : PropSet.sameset _ _ |- _ =>
498 rewrite H end.
499 apply used_varnames_disjoint; lia. }
500 { simplify; subst; eauto; [ | | ].
501 { (* varnames subset *)
502 rewrite varname_set_local.
503 rewrite PropSet.of_list_cons.
504 rewrite add_union_singleton.
505 apply subset_union_l;
506 [ apply used_varnames_subset_singleton; lia| ].
507 rewrite <-varname_set_local.
508 etransitivity; [eassumption|].
509 rewrite <-Nat.add_1_r.
510 apply used_varnames_shift. }
511 { (* only_differ *)
512 rewrite <-(Nat.add_comm nextn 1) in *.
513 only_differ_ok. }
514 { (* equivalence of output holds *)
515 clear IHe1_valid.
516 simplify. cbv [WeakestPrecondition.dexpr] in *.
517 apply Forall2_cons; [intros | eassumption].
518 sepsimpl.
519 eexists; sepsimpl; [ eassumption | ].
520 eapply (expr_untouched ltac:(eassumption)
521 ltac:(eassumption)); eauto; [ ].
522 cbv [used_varnames]. setsimplify.
523 rewrite in_map_iff. intro; cleanup.
524 match goal with H : varname_gen ?x = varname_gen _ |- _ =>
525 apply varname_gen_unique in H; subst x end.
526 match goal with H : In _ (seq _ _) |- _ =>
527 apply in_seq in H end.
528 lia. } } }
529 { (* nil *)
530 cbv [locally_equivalent equivalent]; simplify; eauto;
531 try reflexivity.
532 right; reflexivity. }
533 { (* valid expr *)
534 simplify; subst; eauto; only_differ_ok.
535 match goal with H : PropSet.sameset _ _ |- _ =>
536 rewrite H end; reflexivity. }
537 Qed.
538 End Cmd.