forked from mit-plv/fiat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TupleList.v
617 lines (554 loc) · 28 KB
/
TupleList.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
Require Import CertifiedExtraction.Extraction.QueryStructures.CallRules.Core.
Require Import CertifiedExtraction.Extraction.External.Loops.
Require Import CertifiedExtraction.Extraction.External.FacadeLoops.
Require Import CertifiedExtraction.Extraction.External.Lists.
Lemma map_inj {A B}:
forall (f: A -> B),
(forall x y, f x = f y -> x = y) ->
(forall x y, (map f) x = (map f) y -> x = y).
Proof.
induction x; destruct y; simpl; intros HH; try congruence.
inversion' HH; f_equal; eauto.
Qed.
Instance WrapList {A B} {Wrp: FacadeWrapper A B} : FacadeWrapper (list A) (list B).
Proof.
refine {| wrap x := map wrap x |}.
eauto using map_inj, wrap_inj.
Qed.
Hint Resolve map_inj : inj_db.
Module Type ADTListCompilationParams (Import Params: ADTListADTSpecParams).
Parameter TypeIndex : Type.
Parameter RealElemType : TypeIndex -> Type.
Parameter RealElemToElem : forall index, (RealElemType index) -> ElemType.
Axiom RealElemToElem_inj : forall idx, Injective (@RealElemToElem idx).
End ADTListCompilationParams.
Module ADTListCompilation
(Import Params: ADTListADTSpecParams)
(Import CompilationParams: ADTListCompilationParams Params).
Module Import Specs := (ADTListADTSpec Params).
Hint Resolve map_inj : inj_db.
Hint Resolve ListConstructor_inj : inj_db.
Hint Resolve ElemConstructor_inj : inj_db.
Hint Extern 1 => eapply RealElemToElem_inj : inj_db.
Instance WrapADTList {index} : FacadeWrapper ADTValue (list (RealElemType index)).
Proof.
refine {| wrap ls := ListConstructor (map (RealElemToElem (index := index)) ls);
wrap_inj := _ |}; eauto with inj_db.
Defined.
Instance WrapADTListElement {idx} : FacadeWrapper ADTValue (RealElemType idx).
Proof.
refine {| wrap x := ElemConstructor (RealElemToElem x);
wrap_inj := _ |}; eauto with inj_db.
Defined.
Lemma CompileNew {idx} :
forall vret fpointer (env: Env ADTValue) ext tenv,
GLabelMap.find fpointer env = Some (Axiomatic New) ->
vret ∉ ext ->
Lifted_not_mapsto_adt ext tenv vret ->
{{ tenv }}
Call vret fpointer nil
{{ [[ `vret ->> @nil (RealElemType idx) as _ ]] :: DropName vret tenv }} ∪ {{ ext }} // env.
Proof.
repeat (SameValues_Facade_t_step || facade_cleanup_call || LiftPropertyToTelescope_t).
change (ADT (ListConstructor [])) with (wrap (FacadeWrapper := WrapInstance (H := @WrapADTList idx)) []).
facade_eauto.
Qed.
Ltac injections :=
match goal with
| [ H: ListConstructor _ = ListConstructor _ |- _ ] => apply ListConstructor_inj in H
| [ H: ElemConstructor _ = ElemConstructor _ |- _ ] => apply ElemConstructor_inj in H
end.
Lemma CompilePop {idx} :
forall vret vlst fpointer (env: Env ADTValue) ext tenv
h (t: list (RealElemType idx)),
GLabelMap.find fpointer env = Some (Axiomatic Pop) ->
Lifted_MapsTo ext tenv vlst (wrap (h :: t)) ->
Lifted_not_mapsto_adt ext tenv vret ->
vret <> vlst ->
vlst ∉ ext ->
vret ∉ ext ->
{{ tenv }}
Call vret fpointer (vlst :: nil)
{{ [[ `vret ->> h as _ ]] :: [[ `vlst ->> t as _ ]] :: DropName vlst (DropName vret tenv) }} ∪ {{ ext }} // env.
Proof.
repeat (SameValues_Facade_t_step || facade_cleanup_call || LiftPropertyToTelescope_t || injections);
facade_eauto.
Qed.
Lemma CompilePush {idx} :
forall vret vhd vlst fpointer (env: Env ADTValue) ext tenv
h (t: list (RealElemType idx)),
GLabelMap.find fpointer env = Some (Axiomatic Push) ->
Lifted_MapsTo ext tenv vhd (wrap h) ->
Lifted_MapsTo ext tenv vlst (wrap t) ->
Lifted_not_mapsto_adt ext tenv vret ->
vret <> vlst ->
vret <> vhd ->
vhd <> vlst ->
vhd ∉ ext ->
vlst ∉ ext ->
vret ∉ ext ->
{{ tenv }}
Call vret fpointer (vlst :: vhd :: nil)
{{ [[ `vret ->> Word.wzero 32 as _ ]]
:: [[ `vlst ->> h :: t as _ ]]
:: DropName vlst (DropName vret (DropName vhd tenv)) }} ∪ {{ ext }} // env.
Proof.
repeat (SameValues_Facade_t_step || facade_cleanup_call || LiftPropertyToTelescope_t || injections).
facade_eauto.
facade_eauto.
facade_eauto.
repeat apply DropName_remove; congruence.
Qed.
Lemma CompileEmpty {idx}:
forall (vtest vlst : StringMap.key) (env : GLabelMap.t (FuncSpec QsADTs.ADTValue))
(tenv: Telescope QsADTs.ADTValue) ext
(fpointer : GLabelMap.key) (lst : list (RealElemType idx)),
vlst <> vtest ->
vtest ∉ ext ->
Lifted_MapsTo ext tenv vlst (wrap lst) ->
Lifted_not_mapsto_adt ext tenv vtest ->
GLabelMap.MapsTo fpointer (Axiomatic Empty) env ->
{{ tenv }}
Call vtest fpointer (vlst :: nil)
{{ [[`vtest ->> (bool2w (Nat.eqb (Datatypes.length lst) 0)) as _]] :: (DropName vtest tenv) }} ∪ {{ ext }} // env.
Proof.
repeat (SameValues_Facade_t_step || facade_cleanup_call || LiftPropertyToTelescope_t || PreconditionSet_t || injections).
facade_eauto.
rewrite add_remove_comm by congruence.
rewrite <- add_redundant_cancel by assumption.
facade_eauto.
Qed.
Lemma CompileEmpty_spec {idx}:
forall (vtest vlst : StringMap.key) (env : GLabelMap.t (FuncSpec QsADTs.ADTValue))
(tenv: Telescope QsADTs.ADTValue) ext
(fpointer : GLabelMap.key) (lst : list (RealElemType idx)),
vlst <> vtest ->
vtest ∉ ext ->
NotInTelescope vtest tenv ->
StringMap.MapsTo vlst (wrap lst) ext ->
GLabelMap.MapsTo fpointer (Axiomatic Empty) env ->
{{ tenv }}
Call vtest fpointer (vlst :: nil)
{{ [[`vtest ->> (bool2w (Nat.eqb (Datatypes.length (rev lst)) 0)) as _]] :: tenv }} ∪ {{ ext }} // env.
Proof.
intros.
setoid_rewrite rev_length.
apply generalized @CompileEmpty; repeat (compile_do_side_conditions || Lifted_t).
Qed.
Lemma CompileEmpty_alt {idx}:
forall (vtest vlst : StringMap.key) (env : GLabelMap.t (FuncSpec QsADTs.ADTValue))
(tenv: Telescope QsADTs.ADTValue) ext
(fempty : GLabelMap.key) (lst : Comp (list (RealElemType idx))),
vlst <> vtest ->
vtest ∉ ext ->
Lifted_not_mapsto_adt ext tenv vtest ->
GLabelMap.MapsTo fempty (Axiomatic Empty) env ->
{{ [[(NTSome vlst) ~~> lst as _]] :: tenv }}
Call vtest fempty (vlst :: nil)
{{ [[(NTSome vlst) ~~> lst as ls]]
:: [[`vtest ->> (bool2w match ls with
| nil => true
| _ :: _ => false
end) as _]]
:: (DropName vtest tenv) }} ∪ {{ ext }} // env.
Proof.
repeat (SameValues_Facade_t_step || facade_cleanup_call || LiftPropertyToTelescope_t || injections);
facade_eauto.
Qed.
Lemma CompileDelete {idx}:
forall (vtmp vlst : StringMap.key) (env : GLabelMap.t (FuncSpec QsADTs.ADTValue))
(tenv: Telescope QsADTs.ADTValue) ext
(fpointer : GLabelMap.key),
vlst <> vtmp ->
vtmp ∉ ext ->
vlst ∉ ext ->
Lifted_MapsTo ext tenv vlst (wrap (@nil (RealElemType idx))) ->
Lifted_not_mapsto_adt ext tenv vtmp ->
GLabelMap.MapsTo fpointer (Axiomatic Delete) env ->
{{ tenv }}
Call vtmp fpointer (vlst :: nil)
{{ [[`vtmp ->> (Word.natToWord 32 0) as _]] :: (DropName vtmp (DropName vlst tenv)) }} ∪ {{ ext }} // env.
Proof.
repeat (SameValues_Facade_t_step || facade_cleanup_call || LiftPropertyToTelescope_t || PreconditionSet_t || injections);
facade_eauto.
Qed.
Lemma CompileDelete_spec {idx}:
forall (vtmp vlst : StringMap.key) (env : GLabelMap.t (FuncSpec QsADTs.ADTValue))
(tenv: Telescope QsADTs.ADTValue) ext
(fpointer : GLabelMap.key),
vlst <> vtmp ->
vtmp ∉ ext ->
vlst ∉ ext ->
NotInTelescope vtmp tenv ->
NotInTelescope vlst tenv ->
GLabelMap.MapsTo fpointer (Axiomatic Delete) env ->
{{ [[ (NTSome vlst) ->> @nil (RealElemType idx) as _]] :: tenv }}
(Call vtmp fpointer (vlst :: nil))
{{ tenv }} ∪ {{ ext }} // env.
Proof.
intros.
apply ProgOk_Remove_Skip_R; hoare.
apply generalized @CompileDelete; try (compile_do_side_conditions || Lifted_t).
repeat match goal with
| [ H: NotInTelescope _ _ |- _ ] => setoid_rewrite (DropName_NotInTelescope _ _ H)
| _ => setoid_rewrite Propagate_anonymous_ret
end.
apply CompileDeallocW_discretely; try compile_do_side_conditions.
apply CompileSkip.
Qed.
Ltac loop_t :=
repeat (intros || unfold Fold || solve [PreconditionSet_t; Lifted_t] || compile_do_side_conditions || clean_DropName_in_ProgOk || rewrite Propagate_ret || eapply CompileSeq || eauto 2).
Lemma CompileLoopBase__many {idx} :
forall {A}
(lst: list (RealElemType idx)) init vhead vtest vlst
fpop fempty fdealloc facadeBody env (ext: StringMap.t (Value QsADTs.ADTValue)) tenv
(f: A -> (RealElemType idx) -> A) tenvF,
GLabelMap.MapsTo fpop (Axiomatic Pop) env ->
GLabelMap.MapsTo fempty (Axiomatic Empty) env ->
GLabelMap.MapsTo fdealloc (Axiomatic Delete) env ->
(* (forall tenv a1 a2 b, tenvF tenv (a1, b) = tenvF tenv (a2, b)) -> *)
PreconditionSet tenv ext [[[vhead; vtest; vlst]]] ->
(forall v, NotInTelescope vtest (tenvF tenv v)) ->
(forall v, NotInTelescope vhead (tenvF tenv v)) ->
(forall v, NotInTelescope vlst (tenvF tenv v)) ->
(forall v (h: (RealElemType idx)), TelEq ext ([[ ` vhead ->> h as _]]::tenvF tenv v) (tenvF ([[ ` vhead ->> h as _]]::tenv) v)) ->
(forall head (acc: A) (s: list (RealElemType idx)),
{{ tenvF ([[`vhead ->> head as _]] :: tenv) acc }}
facadeBody
{{ [[ ret (f acc head) as facc ]] :: tenvF tenv facc }} ∪
{{ [vtest |> wrap (bool2w false)] :: [vlst |> wrap s] :: ext }} // env) ->
{{ [[`vlst ->> lst as _]] :: tenvF tenv init }}
(Seq (Fold vhead vtest vlst fpop fempty facadeBody) (Call (DummyArgument vtest) fdealloc (vlst :: nil)))
{{ tenvF tenv (fold_left f lst init) }} ∪ {{ ext }} // env.
Proof.
Transparent DummyArgument.
unfold DummyArgument; loop_t.
eapply (CompileEmpty_alt); loop_t.
apply Lifted_not_In_Telescope_not_in_Ext_not_mapsto_adt; loop_t.
2:eapply (CompileDelete_spec); loop_t.
loop_t.
generalize dependent init;
induction lst; loop_t.
move_to_front vtest;
apply CompileWhileFalse_Loop; loop_t.
simpl.
eapply CompileWhileTrue; [ loop_t.. | ].
apply generalized @CompilePop; loop_t.
rewrite <- GLabelMapFacts.find_mapsto_iff; try assumption.
apply Lifted_not_In_Telescope_not_in_Ext_not_mapsto_adt; loop_t.
move_to_front vtest. (* apply ProgOk_Chomp_Some; loop_t. *)
move_to_front vlst. (* apply ProgOk_Chomp_Some; loop_t. *)
match goal with
| [ H: forall _ _, TelEq _ _ _ |- _ ] => setoid_rewrite H
end.
apply ProgOk_Chomp_Some; loop_t.
apply ProgOk_Chomp_Some; loop_t.
computes_to_inv; subst; defunctionalize_evar; solve [eauto].
move_to_front vtest.
apply ProgOk_Remove_Skip_L; hoare.
apply CompileDeallocSCA_discretely; try compile_do_side_conditions.
apply ProgOk_Chomp_Some; try compile_do_side_conditions; intros.
apply CompileSkip.
apply CompileEmpty_alt; loop_t.
apply Lifted_not_In_Telescope_not_in_Ext_not_mapsto_adt; loop_t.
loop_t.
setoid_replace (DropName vtest ([[ ret (f init a) as facc ]] :: tenvF tenv facc))
with (tenvF tenv (f init a)) using relation (TelEq ext); simpl; loop_t.
Qed.
Lemma CompileLoop__many {idx}:
forall {A}
vhead vtest vlst (tenvF: A -> Telescope ADTValue) tenv'
(lst: list (RealElemType idx)) init (f: A -> (RealElemType idx) -> A) tenv0 tenv
env (ext: StringMap.t (Value QsADTs.ADTValue))
fpop fempty fdealloc facadeBody facadeConclude,
GLabelMap.MapsTo fpop (Axiomatic Pop) env ->
GLabelMap.MapsTo fempty (Axiomatic Empty) env ->
GLabelMap.MapsTo fdealloc (Axiomatic Delete) env ->
PreconditionSet tenv ext [[[vhead; vtest; vlst]]] ->
TelEq ext tenv0 ([[`vlst ->> lst as _]] :: TelAppend (tenvF init) tenv) ->
(forall v, NotInTelescope vtest (TelAppend (tenvF v) tenv)) ->
(forall v, NotInTelescope vhead (TelAppend (tenvF v) tenv)) ->
(forall v, NotInTelescope vlst (TelAppend (tenvF v) tenv)) ->
{{ TelAppend (tenvF (fold_left f lst init)) tenv }}
facadeConclude
{{ TelAppend (tenvF (fold_left f lst init)) tenv' }}
∪ {{ ext }} // env ->
(forall head (acc: A) (s: list (RealElemType idx)),
{{ TelAppend (tenvF acc) ([[`vhead ->> head as _]] :: tenv) }}
facadeBody
{{ TelAppend ([[ ret (f acc head) as facc ]] :: tenvF facc) tenv }} ∪
{{ [vtest |> wrap (bool2w false)] :: [vlst |> wrap s] :: ext }} // env) ->
{{ tenv0 }}
(Seq (Seq (Fold vhead vtest vlst fpop fempty facadeBody)
(Call (DummyArgument vtest) fdealloc (vlst :: nil)))
facadeConclude)
{{ TelAppend ([[ ret (fold_left f lst init) as folded ]] :: tenvF folded) tenv' }} ∪ {{ ext }} // env.
Proof.
intros.
rewrite H3.
setoid_rewrite Propagate_anonymous_ret.
hoare.
pose (fun tenv init => TelAppend (tenvF init) tenv) as tenvF'.
change (TelAppend (tenvF init) tenv) with (tenvF' tenv init).
change (TelAppend (tenvF (fold_left f lst init)) tenv) with (tenvF' tenv (fold_left f lst init)).
eapply @CompileLoopBase__many; eauto using TelEq_TelAppend_Cons_Second.
Qed.
End ADTListCompilation.
Require Import CertifiedExtraction.Extraction.QueryStructures.CallRules.Tuple.
Module WTupleListCompilation.
Include (TupleListADTSpec WTupleListADTSpecParams).
Module Import PR := (ADTListADTSpecParamsFromTupleOnes WTupleListADTSpecParams).
Module WTupleListADTCompilationParams <: (ADTListCompilationParams PR).
Definition TypeIndex := nat.
Definition RealElemType N := FiatWTuple N.
Definition RealElemToElem N := TupleToListW (N := N).
Lemma RealElemToElem_inj : forall idx, Injective (@RealElemToElem idx).
Proof.
unfold RealElemToElem, Injective.
eauto using TupleToListW_inj.
Qed.
End WTupleListADTCompilationParams.
(* FIXME make injective a notation *)
(* This makes no sense: we don't have a bedrock implementation of this! *)
(* Module FiatWTupleListADTSpecParams <: ADTListADTSpecParams. *)
(* Definition N := 1. *)
(* Definition ElemType := FiatWTuple N. *)
(* Definition ElemConstructor x := WTuple (TupleToListW (N := N) x). *)
(* Lemma ElemConstructor_inj : (Injective ElemConstructor). *)
(* Admitted. *)
(* Definition ListConstructor ls := WTupleList (map (TupleToListW (N := N)) ls). *)
(* Lemma ListConstructor_inj : (Injective ListConstructor). *)
(* Admitted. *)
(* End FiatWTupleListADTSpecParams. *)
Include (ADTListCompilation PR WTupleListADTCompilationParams).
(* Module Import PR := (ADTListADTSpecParamsFromTupleOnes WTupleListADTSpecParams). *)
(* Include (ADTListCompilation PR). *)
(* Instance QS_WrapWTupleList {N} : FacadeWrapper ADTValue (list (FiatWTuple N)). *)
(* Proof. *)
(* refine {| wrap tl := wrap (List.map TupleToListW tl); *)
(* wrap_inj := _ |}; Wrapper_t. *)
(* Defined. *)
Require Import GLabelMapFacts.
(* Lemma CompileLoopbase : *)
(* forall {N A} `{FacadeWrapper (Value QsADTs.ADTValue) A} (lst: list (FiatWTuple N)) init vhead vtest vlst vret fpop fempty fdealloc facadeBody env (ext: StringMap.t (Value QsADTs.ADTValue)) tenv (f: Comp A -> FiatWTuple N -> Comp A), *)
(* GLabelMap.MapsTo fpop (Axiomatic Pop) env -> *)
(* GLabelMap.MapsTo fempty (Axiomatic Empty) env -> *)
(* GLabelMap.MapsTo fdealloc (Axiomatic Delete) env -> *)
(* PreconditionSet tenv ext [[[vhead; vtest; vlst; vret]]] -> *)
(* BinNat.N.lt (BinNat.N.of_nat N) (Word.Npow2 32) -> *)
(* (forall head (acc: Comp A) (s: list (FiatWTuple N)), *)
(* {{ [[`vret ~~> acc as _]] :: [[`vhead ->> head as _]] :: tenv }} *)
(* facadeBody *)
(* {{ [[`vret ~~> (f acc head) as _]] :: tenv }} ∪ *)
(* {{ [vtest |> wrap (bool2w false)] :: [vlst |> wrap s] :: ext }} // env) -> *)
(* {{ [[`vret ~~> init as _]] :: [[`vlst ->> lst as _]] :: tenv }} *)
(* (Seq (Fold vhead vtest vlst fpop fempty facadeBody) (Call (DummyArgument vtest) fdealloc (vlst :: nil))) *)
(* {{ [[`vret ~~> (fold_left f lst init) as _]] :: tenv }} ∪ {{ ext }} // env. *)
(* Proof. *)
(* unfold DummyArgument; loop_t. *)
(* rewrite TelEq_swap by loop_t. *)
(* unfold *)
(* eapply (CompileEmpty_alt); loop_t. *)
(* 2:eapply (CompileDelete_spec (N := N)); loop_t. *)
(* loop_t. *)
(* generalize dependent init; *)
(* induction lst; loop_t. *)
(* move_to_front vtest; *)
(* apply CompileWhileFalse_Loop; loop_t. *)
(* simpl. *)
(* eapply CompileWhileTrue; [ loop_t.. | ]. *)
(* apply generalized @CompilePop; loop_t. *)
(* rewrite <- GLabelMapFacts.find_mapsto_iff; assumption. *)
(* move_to_front vlst; apply ProgOk_Chomp_Some; loop_t. *)
(* move_to_front vtest; apply ProgOk_Chomp_Some; loop_t. *)
(* move_to_front vret; loop_t. *)
(* computes_to_inv; subst; defunctionalize_evar; eauto. *)
(* rewrite TelEq_swap. *)
(* apply ProgOk_Remove_Skip_L; hoare. *)
(* apply CompileDeallocW_discretely; try compile_do_side_conditions. *)
(* apply ProgOk_Chomp_Some; try compile_do_side_conditions; intros. *)
(* apply CompileSkip. *)
(* apply CompileEmpty_alt; loop_t. *)
(* loop_t. *)
(* Qed. *)
Lemma CompileLoop {N} :
forall {A} `{FacadeWrapper (Value QsADTs.ADTValue) A}
lst init vhead vtest vlst vret fpop fempty fdealloc facadeBody facadeConclude
env (ext: StringMap.t (Value QsADTs.ADTValue)) tenv tenv' (f: Comp A -> FiatWTuple N -> Comp A),
GLabelMap.MapsTo fpop (Axiomatic (Pop)) env ->
GLabelMap.MapsTo fempty (Axiomatic (Empty)) env ->
GLabelMap.MapsTo fdealloc (Axiomatic (Delete)) env ->
PreconditionSet tenv ext [[[vhead; vtest; vlst; vret]]] ->
{{ [[`vret ~~> (fold_left f lst init) as _]] :: tenv }}
facadeConclude
{{ [[`vret ~~> (fold_left f lst init) as _]] :: tenv' }} ∪ {{ ext }} // env ->
(* BinNat.N.lt (BinNat.N.of_nat 1) (Word.Npow2 32) -> *)
(forall head (acc: Comp A) (s: list (FiatWTuple N)),
{{ [[`vret ~~> acc as _]] :: [[`vhead ->> head as _]] :: tenv }}
facadeBody
{{ [[`vret ~~> (f acc head) as _]] :: tenv }} ∪
{{ [vtest |> wrap (bool2w false)] :: [vlst |> wrap s] :: ext }} // env) ->
{{ [[`vret ~~> init as _]] :: [[`vlst ->> lst as _]] :: tenv }}
(Seq (Seq (Fold vhead vtest vlst fpop fempty facadeBody)
(Call (DummyArgument vtest) fdealloc (vlst :: nil))) facadeConclude)
{{ [[`vret ~~> (fold_left f lst init) as _]] :: tenv' }} ∪ {{ ext }} // env.
Proof.
hoare. hoare.
rewrite TelEq_swap.
set (tenvF := fun tenv init => [[ `vret ~~> init as _ ]] :: tenv).
change ([[ `vlst ->> lst as _ ]] :: [[ `vret ~~> init as _ ]] :: tenv) with ([[ `vlst ->> lst as _ ]] :: tenvF tenv init).
change ([[ `vret ~~> fold_left f lst init as _ ]] :: tenv) with (tenvF tenv (fold_left f lst init)).
eapply CompileLoopBase__many; unfold tenvF in *; loop_t.
setoid_rewrite Propagate_anonymous_ret; apply H5.
Qed.
Lemma CompileLoopalloc :
forall {N A} `{FacadeWrapper (Value QsADTs.ADTValue) A} lst init vhead vtest vlst vret fpop fempty fdealloc facadeInit facadeBody facadeConclude
env (ext: StringMap.t (Value QsADTs.ADTValue)) tenv tenv' (f: Comp A -> (FiatWTuple N) -> Comp A),
GLabelMap.MapsTo fpop (Axiomatic (Pop)) env ->
GLabelMap.MapsTo fempty (Axiomatic (Empty)) env ->
GLabelMap.MapsTo fdealloc (Axiomatic (Delete)) env ->
PreconditionSet tenv ext [[[vhead; vtest; vlst; vret]]] ->
BinNat.N.lt (BinNat.N.of_nat N) (Word.Npow2 32) ->
{{ [[`vlst ->> lst as _]] :: tenv }}
facadeInit
{{ [[`vret ~~> init as _]] :: [[`vlst ->> lst as _]] :: tenv }} ∪ {{ ext }} // env ->
{{ [[`vret ~~> (fold_left f lst init) as _]] :: tenv }}
facadeConclude
{{ [[`vret ~~> (fold_left f lst init) as _]] :: tenv' }} ∪ {{ ext }} // env ->
(forall head (acc: Comp A) (s: list (FiatWTuple N)),
{{ [[`vret ~~> acc as _]] :: [[`vhead ->> head as _]] :: tenv }}
facadeBody
{{ [[`vret ~~> (f acc head) as _]] :: tenv }} ∪
{{ [vtest |> wrap (bool2w false)] :: [vlst |> wrap s] :: ext }} // env) ->
{{ [[`vlst ->> lst as _]] :: tenv }}
(Seq facadeInit (Seq (Seq (Fold vhead vtest vlst fpop fempty facadeBody) (Call (DummyArgument vtest) fdealloc (vlst :: nil))) facadeConclude))
{{ [[`vret ~~> (fold_left f lst init) as _]] :: tenv' }} ∪ {{ ext }} // env.
Proof.
hoare. hoare.
apply @CompileLoop; try eassumption.
Qed.
Require Import Fiat.CertifiedExtraction.Extraction.QueryStructures.CallRules.WordList.
Lemma CompileMap_TuplesToWords :
forall {N} (lst: list (FiatWTuple N)) vhead vhead' vtest vlst vret vtmp fpop fempty falloc fdealloc fcons facadeBody facadeCoda env (ext: StringMap.t (Value QsADTs.ADTValue)) tenv tenv' (f: FiatWTuple N -> W),
GLabelMap.MapsTo fpop (Axiomatic (Pop)) env ->
GLabelMap.MapsTo fempty (Axiomatic (Empty)) env ->
GLabelMap.MapsTo falloc (Axiomatic (QsADTs.WordListADTSpec.New)) env ->
GLabelMap.MapsTo fdealloc (Axiomatic (Delete)) env ->
GLabelMap.MapsTo fcons (Axiomatic (QsADTs.WordListADTSpec.Push)) env ->
PreconditionSet tenv ext [[[vhead; vhead'; vtest; vlst; vret; vtmp]]] ->
BinNat.N.lt (BinNat.N.of_nat N) (Word.Npow2 32) ->
{{ [[NTSome (H := WrapInstance (H := QS_WrapWordList)) vret ->> (revmap f lst) as _]] :: tenv }}
facadeCoda
{{ [[NTSome (H := WrapInstance (H := QS_WrapWordList)) vret ->> (revmap f lst) as _]] :: tenv' }} ∪ {{ ext }} // env ->
(forall head (s: list (FiatWTuple N)) (s': list W),
{{ [[`vhead ->> head as _]] :: tenv }}
facadeBody
{{ [[`vhead' ->> f head as _]] :: tenv }}
∪ {{ [vret |> wrap (FacadeWrapper := WrapInstance (H := QS_WrapWordList)) s']
:: [vtest |> wrap (bool2w false)] :: [vlst |> wrap s] :: ext }} // env) ->
{{ [[`vlst ->> lst as _]] :: tenv }}
(Seq
(Call vret falloc nil)
(Seq
(Seq
(Fold vhead vtest vlst fpop fempty
(Seq facadeBody
(Call vtmp fcons (vret :: vhead' :: nil))))
(Call vtest fdealloc (vlst :: nil)))
facadeCoda))
{{ [[NTSome (H := WrapInstance (H := QS_WrapWordList)) vret ->> (revmap f lst) as _]] :: tenv' }} ∪ {{ ext }} // env.
Proof.
intros.
setoid_rewrite <- revmap_fold_comp.
apply CompileLoopalloc; eauto.
PreconditionSet_t; eauto.
eapply CompileWordList_new; compile_do_side_conditions.
setoid_rewrite revmap_fold_comp; eassumption.
intros.
rewrite SameValues_Fiat_Bind_TelEq.
move_to_front vret.
apply miniChomp'; intros.
hoare.
apply ProgOk_Chomp_Some; loop_t; defunctionalize_evar; eauto.
apply CompileWordList_push_spec; try compile_do_side_conditions.
Qed.
Lemma CompileLoop_ret :
forall {N A} `{FacadeWrapper (Value QsADTs.ADTValue) A}
lst init facadeBody facadeConclude vhead vtest vlst vret env (ext: StringMap.t (Value QsADTs.ADTValue)) tenv tenv' fpop fempty fdealloc (f: A -> (FiatWTuple N) -> A),
GLabelMap.MapsTo fpop (Axiomatic Pop) env ->
GLabelMap.MapsTo fempty (Axiomatic Empty) env ->
GLabelMap.MapsTo fdealloc (Axiomatic Delete) env ->
PreconditionSet tenv ext [[[vhead; vtest; vlst; vret]]] ->
BinNat.N.lt (BinNat.N.of_nat N) (Word.Npow2 32) ->
(forall head acc (s: list (FiatWTuple N)),
{{ [[`vret ->> acc as _]] :: [[`vhead ->> head as _]] :: tenv }}
facadeBody
{{ [[`vret ->> (f acc head) as _]] :: tenv }} ∪ {{ [vtest |> wrap (bool2w false)] :: [vlst |> wrap s] :: ext }} // env) ->
{{ [[`vret ->> (fold_left f lst init) as _]] :: tenv }}
facadeConclude
{{ [[`vret ->> (fold_left f lst init) as _]] :: tenv' }} ∪ {{ ext }} // env ->
{{ [[`vret ->> init as _]] :: [[(NTSome vlst) ->> lst as _]] :: tenv }}
(Seq (Seq (Fold vhead vtest vlst fpop fempty facadeBody) (Call (DummyArgument vtest) fdealloc (vlst :: nil))) facadeConclude)
{{ [[`vret ->> (fold_left f lst init) as _]] :: tenv' }} ∪ {{ ext }} // env.
Proof.
intros.
setoid_replace ([[ `vret ->> fold_left f lst init as _ ]] :: tenv')
with (TelAppend ([[ ret (fold_left f lst init) as r ]] :: [[ `vret ->> r as _ ]] :: Nil) tenv')
using relation (TelEq ext) by (rewrite Propagate_anonymous_ret; reflexivity).
eapply CompileLoop__many; unfold TelAppend; PreconditionSet_t; loop_t.
rewrite Propagate_anonymous_ret. eauto.
Qed.
Lemma CompileLoopalloc_ret :
forall {N A} `{FacadeWrapper (Value QsADTs.ADTValue) A}
lst init facadeInit facadeBody facadeConclude vhead vtest vlst vret env (ext: StringMap.t (Value QsADTs.ADTValue)) tenv tenv' fpop fempty fdealloc (f: A -> (FiatWTuple N) -> A),
GLabelMap.MapsTo fpop (Axiomatic (Pop)) env ->
GLabelMap.MapsTo fempty (Axiomatic (Empty)) env ->
GLabelMap.MapsTo fdealloc (Axiomatic (Delete)) env ->
PreconditionSet tenv ext [[[vhead; vtest; vlst; vret]]] ->
BinNat.N.lt (BinNat.N.of_nat N) (Word.Npow2 32) ->
{{ [[`vlst ->> lst as _]] :: tenv }}
facadeInit
{{ [[`vret ->> init as _]] :: [[`vlst ->> lst as _]] :: tenv }} ∪ {{ ext }} // env ->
(forall head acc (s: list (FiatWTuple N)),
{{ [[`vret ->> acc as _]] :: [[`vhead ->> head as _]] :: tenv }}
facadeBody
{{ [[`vret ->> (f acc head) as _]] :: tenv }} ∪ {{ [vtest |> wrap (bool2w false)] :: [vlst |> wrap s] :: ext }} // env) ->
{{ [[`vret ->> (fold_left f lst init) as _]] :: tenv }}
facadeConclude
{{ [[`vret ->> (fold_left f lst init) as _]] :: tenv' }} ∪ {{ ext }} // env ->
{{ [[`vlst ->> lst as _]] :: tenv }}
(Seq facadeInit (Seq (Seq (Fold vhead vtest vlst fpop fempty facadeBody) (Call (DummyArgument vtest) fdealloc (vlst :: nil))) facadeConclude))
{{ [[`vret ->> (fold_left f lst init) as _]] :: tenv' }} ∪ {{ ext }} // env.
Proof.
eauto using @CompileSeq, @CompileLoop_ret.
Qed.
Require Import CertifiedExtraction.Extraction.QueryStructures.CallRules.Tuple.
Lemma CompileDeleteAny_spec:
forall {N} (vtmp vtmp2 vsize vtest vlst vhead : StringMap.key) (env : GLabelMap.t (FuncSpec QsADTs.ADTValue)) (tenv: Telescope QsADTs.ADTValue) ext
(fpop fempty fdealloc ftdealloc : GLabelMap.key) (seq: (list (FiatWTuple N))),
PreconditionSet tenv ext [[[vtmp; vtmp2; vsize; vhead; vtest; vlst]]] ->
BinNat.N.lt (BinNat.N.of_nat N) (Word.Npow2 32) ->
GLabelMap.MapsTo fpop (Axiomatic (Pop)) env ->
GLabelMap.MapsTo fempty (Axiomatic (Empty)) env ->
GLabelMap.MapsTo fdealloc (Axiomatic (Delete)) env ->
GLabelMap.MapsTo ftdealloc (Axiomatic (WTupleADTSpec.Delete)) env ->
{{ [[ `vlst ->> seq as _]] :: tenv }}
(Seq (Assign vtmp (Const (Word.natToWord 32 0)))
(Seq (Seq (Fold vhead vtest vlst fpop fempty (Seq (Assign vsize (Const (Word.natToWord 32 N)))
(Call vtmp2 ftdealloc [[[vhead; vsize]]])))
(Call (DummyArgument vtest) fdealloc [[[vlst]]]))
Skip))
{{ tenv }} ∪ {{ ext }} // env.
Proof.
intros.
apply ProgOk_Remove_Skip_R.
apply CompileSeq with ([[ ` vtmp ->> fold_left (fun acc x => acc) seq (Word.natToWord 32 0) as _]]::tenv).
eapply (CompileLoopalloc_ret (vhead := vhead) (vtest := vtest)); try compile_do_side_conditions.
apply CompileConstant; try compile_do_side_conditions.
intros. apply ProgOk_Chomp_Some; try compile_do_side_conditions; intros.
apply (WTupleCompilation.CompileDelete_spec (vtmp := vtmp2) (vsize := vsize)); try compile_do_side_conditions.
apply CompileSkip.
apply CompileDeallocW_discretely; try compile_do_side_conditions.
apply CompileSkip.
Defined.
End WTupleListCompilation.