-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute_with_cache.v
More file actions
533 lines (506 loc) · 23.7 KB
/
compute_with_cache.v
File metadata and controls
533 lines (506 loc) · 23.7 KB
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
From iris.algebra Require Import list.
From iris.proofmode Require Import tactics.
From iris.base_logic.lib Require Export invariants.
From mwp.mwp_modalities Require Import mwp_step_fupd.
From mwp.mwp_modalities.ni_logrel Require Import mwp_left mwp_right ni_logrel_lemmas.
From logrel_ifc.lambda_sec Require Export lattice fundamental_binary notation.
Local Instance tpSecurityLattice : SecurityLattice tplabel := { ζ := L }.
Notation H := (LLabel H).
Notation L := (LLabel L).
(*
λ f, default.
let cache = ref (default, f default) in
let compute = λ v,
let result = f v in
cache <- (v, result); result in
(λ v,
match !cache with
| (w, result) =>
if w = v then result else (compute v)
| _ => compute v
end)
*)
Definition and : expr := λ: λ: if: $0 then $1 else #false.
Definition or : expr := λ: λ: if: $0 then #true else $1.
Definition notb : expr := λ: if: $0 then #false else #true.
(* λ cache, f, v. let res = f v in cache <- (false, v, res); res *)
Definition compute_aux : expr :=
λ: λ: λ: let: $1 $0 in $3 <- ($1, $0);; $0.
(* λ cache, f, v. let (w, res) = !cache in
if v = w
then res
else compute_L_aux cache f v *)
Definition compute : expr :=
λ: λ: λ: let: !$2 in
if: (Proj1 $0 = $1)
then (Proj2 $0)
else (compute_aux $3 $2 $1).
Definition mkComputeWithCache : expr :=
λ: λ:
let: ref ($0, $1 $0) in
compute $0 $2.
Definition mkComputeWithCache_aux : expr := App (App mkComputeWithCache $1) $0.
Definition fun_in : sectype :=
(TNat @ LVar 0 →[H] TNat @ LVar 0) @ L.
Definition fun_out : sectype :=
fun_in.
Definition init_arg : sectype :=
TNat @ LVar 0.
Definition mkComputeWithCache_typ : sectype :=
(fun_in →[L] (init_arg →[L] fun_out ) @L) @ L.
(************************ Proofs ************************)
Definition Ncache : namespace := nroot.@"computeWithCache".
Section bi_defs.
Context `{!secG Σ}.
Notation projl Θ := Θ.*2.*1.
Notation projr Θ := Θ.*2.*2.
Definition cache_inv
(Θ : list ((val * val -d> iPropO Σ) * ((val -d> iPropO Σ) * (val -d> iPropO Σ))))
ρ l1 l2 f τ1 τ2 :=
(∃ d1 d2 w1 w2, l1 ↦ₗ (d1, w1) ∗ l2 ↦ (d2, w2) ∗
⌜ w1 = f d1 ⌝ ∗ ⌜ w2 = f d2 ⌝ ∗
⌊ τ1 ₗ⌋ₛ (projl Θ) ρ d1 ∗ ⌊ τ2 ᵣ⌋ₛ (projr Θ) ρ d2 ∗
⌊ τ1 ₗ⌋ₛ (projl Θ) ρ w1 ∗ ⌊ τ2 ᵣ⌋ₛ (projr Θ) ρ w2
)%I.
Definition is_cache_ref Θ ρ f τ1 τ2 : val * val -d> iPropO Σ := λ vv,
(∃ l1 l2, ⌜vv.1 = LocV l1⌝ ∗ ⌜vv.2 = LocV l2⌝ ∗ inv Ncache (cache_inv Θ ρ l1 l2 f τ1 τ2))%I.
Arguments of_val : simpl never.
Definition isWellBehaved Θ ρ (f1 f2 : expr) (f : val -> val) (τ1 τ2 : sectype) :=
(<pers> ((∀ v1, ⌊τ1 ₗ⌋ₛ (projl Θ) ρ v1 -∗ MWP@{mwpd_step_fupd SI_left} (f1) (#v1) {{w1, ⌜ w1 = (f v1) ⌝ ∗ ⌊τ2 ₗ⌋ₛ (projl Θ)ρ w1 }}) ∗
(∀ v2, ⌊τ1 ᵣ⌋ₛ (projr Θ) ρ v2 -∗ MWP@{mwpd_step_fupd SI_right} (f2) (#v2) {{w2, ⌜ w2 = (f v2) ⌝ ∗ ⌊τ2 ᵣ⌋ₛ (projr Θ)ρ w2 }}) ∗
(∀ v1 v2, ⟦τ1⟧ₛ Θ ρ (v1, v2) -∗ ⟦τ2⟧ₛ Θ ρ (f v1, f v2)))
)%I.
Lemma compute_aux_typed Θ ρ f1 f2 f1v f2v f cache1 cache2 :
envs_Persistent Θ → IntoVal f1 f1v → IntoVal f2 f2v → env_coherent Θ -∗
is_cache_ref Θ ρ f (TNat @ LVar 0) (TNat @ LVar 0) (cache1, cache2) -∗
isWellBehaved Θ ρ f1 f2 f (TNat @ LVar 0) (TNat @ LVar 0) -∗
isWellBehaved Θ ρ (compute_aux (#cache1) f1) (compute_aux (#cache2) f2) f (TNat @ LVar 0) (TNat @ LVar 0).
Proof.
iIntros (HPers <- <-) "#HCoh #HisCache (#Hf_typed_l & #Hf_typed_r & #Hf_typed)".
rewrite /compute_aux !lam_to_val.
iSplitL; [| iSplitL ].
- iIntros "!>" (v) "#Hv".
iApply (mwp_step_fupd_bind _ (fill [AppLCtx _; AppLCtx _])).
iApply mwp_step_fupd_pure_step; [done|].
iModIntro; simpl. asimpl. rewrite lam_to_val.
iApply (mwp_value (mwpd_step_fupd _)); umods. iModIntro.
iApply (mwp_step_fupd_bind _ (fill [AppLCtx _])).
iApply mwp_step_fupd_pure_step; [done|]; simpl.
iModIntro; asimpl.
iApply (mwp_value (mwpd_step_fupd _)); umods; iModIntro.
iApply mwp_step_fupd_pure_step; [done|]; simpl.
iModIntro; asimpl.
iApply (mwp_step_fupd_bind _ (fill [LetInCtx _])).
iApply mwp_wand_r; iSplitL.
{ by iApply "Hf_typed_l". }
iIntros (???) "(-> & #Hf_typed_l')".
iApply mwp_step_fupd_pure_step; [done|]. simpl.
iModIntro; asimpl.
iDestruct "HisCache" as (l1 l2) "(%Hl1 & %Hl2 & HcacheInv)".
simpl in Hl1. rewrite Hl1.
iApply (mwp_step_fupd_bind _ (fill [SeqCtx _])).
iApply (mwp_atomic (mwpd_step_fupd _) _ StronglyAtomic); try done.
iInv (Ncache) as "H" "Hclose". iModIntro.
iDestruct "H" as (????) "(Hl1 & Hl2 & #Hc1 & #Hc2 & #H_typedl & #H_typedr & #H_typedl' & #H_typedr')".
rewrite !pair_to_val.
iApply ((@mwp_step_fupd_store _ secG_un_left) with "[//]").
iFrame. iIntros "!> Hl1".
rewrite /cache_inv.
iMod ("Hclose" with "[Hl1 Hl2]") as "_".
{
iModIntro; iExists _, _, _, _. iFrame.
iFrame "#"; eauto.
}
iApply mwp_step_fupd_pure_step; first done.
do 2 iModIntro.
iApply mwp_value; umods. iModIntro.
iFrame "#"; eauto.
- iIntros "!>" (v) "#Hv".
iApply (mwp_step_fupd_bind _ (fill [AppLCtx _; AppLCtx _])).
iApply mwp_step_fupd_pure_step; [done|].
iModIntro; simpl. asimpl. rewrite lam_to_val.
iApply (mwp_value (mwpd_step_fupd _)); umods. iModIntro.
iApply (mwp_step_fupd_bind _ (fill [AppLCtx _])).
iApply mwp_step_fupd_pure_step; [done|]; simpl.
iModIntro; asimpl.
iApply (mwp_value (mwpd_step_fupd _)); umods; iModIntro.
iApply mwp_step_fupd_pure_step; [done|]; simpl.
iModIntro; asimpl.
iApply (mwp_step_fupd_bind _ (fill [LetInCtx _])).
iApply mwp_wand_r; iSplitL.
{ by iApply "Hf_typed_r". }
iIntros (???) "(-> & #Hf_typed_r')".
iApply mwp_step_fupd_pure_step; [done|]; simpl.
iModIntro; asimpl.
iDestruct "HisCache" as (l1 l2) "(%Hl1 & %Hl2 & HcacheInv)".
simpl in Hl2. rewrite Hl2.
iApply (mwp_step_fupd_bind _ (fill [SeqCtx _])).
iApply (mwp_atomic (mwpd_step_fupd _) _ StronglyAtomic); try done.
iInv (Ncache) as "H" "Hclose". iModIntro.
iDestruct "H" as (????) "(Hl1 & Hl2 & #Hc1 & #Hc2 & #H_typedl & #H_typedr & #H_typedl' & #H_typedr')".
rewrite !pair_to_val.
iApply ((@mwp_step_fupd_store _ secG_un_right) with "[//]").
iFrame. iIntros "!> Hl2".
rewrite /cache_inv.
iMod ("Hclose" with "[Hl1 Hl2]") as "_".
{
iModIntro; iExists _, _, _, _. iFrame.
iFrame "#"; eauto.
}
iApply mwp_step_fupd_pure_step; first done.
do 2 iModIntro.
iApply mwp_value; umods. iModIntro.
iFrame "#"; eauto.
- iIntros "!>" (v1 v2) "#Hvv".
by iApply "Hf_typed".
Qed.
Definition is_closed (f : expr) := ∀ σ, f.[σ] = f.
Lemma compute_aux_closed :
is_closed compute_aux.
Proof. rewrite /is_closed. autosubst. Qed.
Lemma loc_closed (ι : loc) :
is_closed (# ι).
Proof. rewrite /is_closed. autosubst. Qed.
Hint Rewrite compute_aux_closed : autosubst.
Hint Rewrite loc_closed : autosubst.
Opaque compute_aux.
Lemma compute_typed Θ ρ f1 f2 f1v f2v f cache1 cache2 :
envs_Persistent Θ → IntoVal f1 f1v → IntoVal f2 f2v → is_closed (#f1v) → is_closed (#f2v) →
env_coherent Θ -∗
is_cache_ref Θ ρ f (TNat @ LVar 0) (TNat @ LVar 0) (cache1, cache2) -∗
isWellBehaved Θ ρ f1 f2 f (TNat @ LVar 0) (TNat @ LVar 0) -∗
⟦ fun_out ⟧ₑ Θ ρ (compute (#cache1) (#f1v)
, compute (#cache2) (#f2v)).
Proof.
iIntros (HPers Hv1 Hv2 Hcl1 Hcl2) "#HCoh #isCache #isWellBehaved".
iPoseProof (compute_aux_typed with "HCoh isCache isWellBehaved") as "(HrecomputeL & HrecomputeR & Hrecompute)".
iDestruct "isCache" as (l1 l2) "/= (-> & -> & HcacheInv)".
rewrite /compute !lam_to_val.
iApply (mwp_left_strong_bind _ _ (fill [ AppLCtx _ ]) (fill [ AppLCtx _ ])).
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. simpl.
rewrite !compute_aux_closed. asimpl.
iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods.
iModIntro. asimpl.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. simpl. asimpl.
iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods.
iModIntro; asimpl.
rewrite [⟦ fun_out ⟧ₛ _ _ ((λ: _ )%V, (λ: _)%V)] interp_sec_def /= bool_decide_eq_true_2 //.
rewrite interp_arrow_def.
iSplitL.
- iIntros "!>" (vv) "#Hvv".
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. asimpl.
iApply (mwp_left_strong_bind _ _ (fill [ LetInCtx _ ]) (fill [ LetInCtx _ ])).
iApply (mwp_double_atomic_lr _ _ StronglyAtomic).
iInv (Ncache) as "H" "Hclose". iModIntro. rewrite /cache_inv.
iDestruct "H" as (????) "(Hl1 & Hl2 & #Hc1 & #Hc2 & #H_typedl & #H_typedr & #H_typedl' & #H_typedr')".
iApply ((@mwp_step_fupd_load _ secG_un_left)); [done|].
iFrame. iIntros "!> Hl1".
iApply ((@mwp_fupd_load _ secG_un_right)); [done|].
iFrame. iIntros "Hl2 /=". rewrite !Hcl1 !Hcl2.
iMod ("Hclose" with "[-]") as "_".
{ iNext. iExists _,_,_,_. iFrame. iFrame "#". }
iModIntro. cbn.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. rewrite /= !compute_aux_closed !Hcl1 !Hcl2. asimpl.
iApply (mwp_left_strong_bind _ _ (fill [ BinOpLCtx _ _; IfCtx _ _ ]) (fill [ BinOpLCtx _ _; IfCtx _ _ ])).
rewrite [⟦ TNat @ _ ⟧ₛ _ _ _]interp_sec_def interp_nat_def !interp_un_sec_def !interp_un_nat_def /=.
case_bool_decide as Hflows.
{
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. iApply mwp_value; umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply (mwp_left_strong_bind _ _ (fill [ IfCtx _ _ ]) (fill [ IfCtx _ _ ])).
iDestruct "Hvv" as (n1 n2) "(-> & -> & ->)".
iDestruct "H_typedl" as (H') "->".
iDestruct "H_typedr" as (H0') "->".
iDestruct "H_typedl'" as (H1') "->".
iDestruct "H_typedr'" as (H2') "->".
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. simpl.
case_bool_decide as Heql; case_bool_decide as Heqr.
- iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. cbn.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iApply mwp_value; umods.
iApply (mwp_value mwp_right); umods.
do 2 iModIntro.
iDestruct "Hc1" as "->"; iDestruct "Hc2" as "->".
iApply "Hrecompute"; subst.
rewrite [⟦ TNat @ §0 ⟧ₛ _ _ _] interp_sec_def /=. case_bool_decide.
+ rewrite interp_nat_def. by iExists _, _.
+ iSplitL; (rewrite interp_un_sec_def interp_un_nat_def; by iExists _).
- iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. cbn.
iApply mwp_un_bi_lr.
iApply mwp_wand_r; iSplitL.
{ rewrite Hv1. iApply "HrecomputeL". by iExists _. }
iIntros (???) "(-> & #Hrecomputed)".
iApply mwp_step_fupd_pure_step; first done.
iApply mwp_value; umods.
do 2 iModIntro. iDestruct "Hc2" as "->".
iApply "Hrecompute".
rewrite [⟦ TNat @ §0 ⟧ₛ _ _ _] interp_sec_def /=. case_bool_decide.
+ rewrite interp_nat_def. by iExists _, _.
+ iSplitL; (rewrite interp_un_sec_def interp_un_nat_def; by iExists _).
- iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro; cbn.
iApply mwp_un_bi_lr.
iApply mwp_step_fupd_pure_step; first done.
iModIntro. iApply mwp_value; umods. iModIntro.
iApply (mwp_wand_r (mwpd_step_fupd _)); iSplitL.
{ rewrite Hv2. iApply "HrecomputeR". by iExists _. }
iIntros (???) "(-> & #Hrecomputed)".
iDestruct "Hc1" as "->".
iApply "Hrecompute".
rewrite [⟦ TNat @ §0 ⟧ₛ _ _ _] interp_sec_def /=. case_bool_decide.
+ rewrite interp_nat_def. by iExists _, _.
+ iSplitL; (rewrite interp_un_sec_def interp_un_nat_def; by iExists _).
- iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro; cbn.
iApply mwp_un_bi_lr.
iApply mwp_wand_r; iSplitL.
{ rewrite Hv1. iApply "HrecomputeL". by iExists _. }
iIntros (???) "(-> & #HrecomputedL)".
iApply (mwp_wand_r (mwpd_step_fupd _)); iSplitL.
{ rewrite Hv2. iApply "HrecomputeR". by iExists _. }
iIntros (???) "(-> & #HrecomputedR) /=".
iApply "Hrecompute".
rewrite [⟦ TNat @ §0 ⟧ₛ _ _ _] interp_sec_def /=. case_bool_decide.
+ rewrite interp_nat_def. by iExists _, _.
+ iSplitL; (rewrite interp_un_sec_def interp_un_nat_def; by iExists _).
}
{
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. iApply mwp_value; umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply (mwp_left_strong_bind _ _ (fill [ IfCtx _ _ ]) (fill [ IfCtx _ _ ])).
iDestruct "Hvv" as "(Hv1 & Hv2)".
iDestruct "Hv1" as (v1) "->".
iDestruct "Hv2" as (v2) "->".
iDestruct "H_typedl" as (H') "->".
iDestruct "H_typedr" as (H0') "->".
iDestruct "H_typedl'" as (H1') "->".
iDestruct "H_typedr'" as (H2') "->".
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. simpl.
case_bool_decide as Heql; case_bool_decide as Heqr.
- iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. cbn.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iApply mwp_value; umods.
iApply (mwp_value mwp_right); umods.
do 2 iModIntro.
iDestruct "Hc1" as "->"; iDestruct "Hc2" as "->".
iApply "Hrecompute"; subst.
rewrite [⟦ TNat @ §0 ⟧ₛ _ _ _] interp_sec_def /=. case_bool_decide.
+ rewrite interp_nat_def. by iExists v1, v2.
+ iSplitL; (rewrite interp_un_sec_def interp_un_nat_def; by iExists _).
- iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. cbn.
iApply mwp_un_bi_lr.
iApply mwp_wand_r; iSplitL.
{ rewrite Hv1. iApply "HrecomputeL". by iExists _. }
iIntros (???) "(-> & #Hrecomputed)".
iApply mwp_step_fupd_pure_step; first done.
iApply mwp_value; umods.
do 2 iModIntro. iDestruct "Hc2" as "->".
iApply "Hrecompute".
rewrite [⟦ TNat @ §0 ⟧ₛ _ _ _] interp_sec_def /=. case_bool_decide.
+ rewrite interp_nat_def. by iExists v1, v2.
+ iSplitL; (rewrite interp_un_sec_def interp_un_nat_def; by iExists _).
- iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro; cbn.
iApply mwp_un_bi_lr.
iApply mwp_step_fupd_pure_step; first done.
iModIntro. iApply mwp_value; umods. iModIntro.
iApply (mwp_wand_r (mwpd_step_fupd _)); iSplitL.
{ rewrite Hv2. iApply "HrecomputeR". by iExists _. }
iIntros (???) "(-> & #Hrecomputed)".
iDestruct "Hc1" as "->".
iApply "Hrecompute".
rewrite [⟦ TNat @ §0 ⟧ₛ _ _ _] interp_sec_def /=. case_bool_decide.
+ rewrite interp_nat_def. by iExists v1, v2.
+ iSplitL; (rewrite interp_un_sec_def interp_un_nat_def; by iExists _).
- iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods. iModIntro.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro; cbn.
iApply mwp_un_bi_lr.
iApply mwp_wand_r; iSplitL.
{ rewrite Hv1. iApply "HrecomputeL". by iExists _. }
iIntros (???) "(-> & #HrecomputedL)".
iApply (mwp_wand_r (mwpd_step_fupd _)); iSplitL.
{ rewrite Hv2. iApply "HrecomputeR". by iExists _. }
iIntros (???) "(-> & #HrecomputedR) /=".
iApply "Hrecompute".
rewrite [⟦ TNat @ §0 ⟧ₛ _ _ _] interp_sec_def /=. case_bool_decide.
* rewrite interp_nat_def. by iExists v1, v2.
* iSplitL; (rewrite interp_un_sec_def interp_un_nat_def; by iExists _).
}
- iSplitL; simpl; rewrite interp_un_arrow_def.
+ iIntros "!>" (v1) "#Hv1 %".
iApply mwp_step_fupd_pure_step; first done.
rewrite !Hcl1. iModIntro; asimpl.
iApply (mwp_step_fupd_bind _ (fill [LetInCtx _])).
iApply (mwp_atomic (mwpd_step_fupd _) _ StronglyAtomic); try done.
iInv (Ncache) as "H" "Hclose". iModIntro.
iDestruct "H" as (????) "(Hl1 & Hl2 & #Hc1 & #Hc2 & #H_typedl & #H_typedr & #H_typedl' & #H_typedr')".
iApply ((@mwp_step_fupd_load _ secG_un_left) with "[//]").
iFrame. iIntros "!> Hl1".
rewrite /cache_inv.
iMod ("Hclose" with "[Hl1 Hl2]") as "_".
{
iModIntro; iExists _, _, _, _. iFrame.
by iFrame "#".
}
iApply mwp_step_fupd_pure_step; first done.
do 2 iModIntro. rewrite !Hcl1. asimpl.
iApply (mwp_step_fupd_bind _ (fill [ IfCtx _ _ ])).
rewrite !interp_un_sec_def !interp_un_nat_def.
iDestruct "H_typedl" as (H') "->".
iDestruct "H_typedl'" as (H1') "->".
iDestruct "Hv1" as (v1') "->".
iApply (mwp_step_fupd_bind _ (fill [ BinOpLCtx _ _ ])).
iApply mwp_step_fupd_pure_step; first done.
iApply mwp_value; umods. do 2 iModIntro.
iApply mwp_step_fupd_pure_step; first done.
iApply mwp_value; umods. do 2 iModIntro.
case_bool_decide.
* iApply mwp_step_fupd_pure_step; first done.
iApply mwp_step_fupd_pure_step; first done.
iApply mwp_value; umods. do 3 iModIntro; subst.
by iExists _.
* iApply mwp_step_fupd_pure_step; first done.
rewrite !Hcl1 Hv1.
iApply mwp_wand_r; iSplitL.
{ iApply "HrecomputeL". iModIntro. by iExists _. }
iIntros "!>" (???) "(-> & #Hrecomputed') //".
+ iIntros "!>" (v2) "#Hv2 %".
iApply mwp_step_fupd_pure_step; first done.
rewrite !Hcl2. iModIntro; asimpl.
iApply (mwp_step_fupd_bind _ (fill [LetInCtx _])).
iApply (mwp_atomic (mwpd_step_fupd _) _ StronglyAtomic); try done.
iInv (Ncache) as "H" "Hclose". iModIntro.
iDestruct "H" as (????) "(Hl1 & Hl2 & #Hc1 & #Hc2 & #H_typedl & #H_typedr & #H_typedl' & #H_typedr')".
iApply ((@mwp_step_fupd_load _ secG_un_right) with "[//]").
iFrame. iIntros "!> Hl2".
rewrite /cache_inv.
iMod ("Hclose" with "[Hl1 Hl2]") as "_".
{
iModIntro; iExists _, _, _, _. iFrame.
by iFrame "#".
}
iApply mwp_step_fupd_pure_step; first done.
do 2 iModIntro. rewrite !Hcl2. asimpl.
iApply (mwp_step_fupd_bind _ (fill [ IfCtx _ _ ])).
rewrite !interp_un_sec_def !interp_un_nat_def.
iDestruct "H_typedr" as (H') "->".
iDestruct "H_typedr'" as (H1') "->".
iDestruct "Hv2" as (v2') "->".
iApply (mwp_step_fupd_bind _ (fill [ BinOpLCtx _ _ ])).
iApply mwp_step_fupd_pure_step; first done.
iApply mwp_value; umods. do 2 iModIntro.
iApply mwp_step_fupd_pure_step; first done.
iApply mwp_value; umods. do 2 iModIntro.
case_bool_decide.
* iApply mwp_step_fupd_pure_step; first done.
iApply mwp_step_fupd_pure_step; first done.
iApply mwp_value; umods. do 3 iModIntro; subst.
by iExists _.
* iApply mwp_step_fupd_pure_step; first done.
rewrite !Hcl2 Hv2.
iApply mwp_wand_r; iSplitL.
{ iApply "HrecomputeR". iModIntro. by iExists _. }
iIntros "!>" (???) "(-> & #Hrecomputed') //".
Qed.
Lemma compute_with_cache_typed Θ ρ f1 f2 f1v f2v f :
envs_Persistent Θ → IntoVal f1 f1v → IntoVal f2 f2v →is_closed (#f1v) → is_closed (#f2v) →
env_coherent Θ -∗
isWellBehaved Θ ρ f1 f2 f (TNat @ LVar 0) (TNat @ LVar 0) -∗
⟦ fun_out ⟧ₑ Θ ρ (mkComputeWithCache (#f1v) 0
, mkComputeWithCache (#f2v) 0).
Proof.
iIntros (Hpers Hv1 Hv2 Hcl1 Hcl2) "#HCoh #HisWellbehaved".
rewrite /mkComputeWithCache /mkComputeWithCache !lam_to_val.
iApply (mwp_left_strong_bind _ _ (fill [ AppLCtx _ ]) (fill [ AppLCtx _ ])).
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. simpl. asimpl.
iApply (mwp_value mwp_binary); umods.
iApply (mwp_value mwp_right); umods.
iModIntro.
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. rewrite !Hcl1 !Hcl2. asimpl.
iApply (mwp_left_strong_bind _ _ (fill [ LetInCtx _ ]) (fill [ LetInCtx _ ])).
rewrite !nat_to_val.
iApply (mwp_left_strong_bind _ _ (fill [ PairRCtx _; AllocCtx ]) (fill [ PairRCtx _; AllocCtx ])).
iDestruct "HisWellbehaved" as "(#Hf_typed_l & #Hf_typed_r & #Hf_typed)".
rewrite !Hcl1 !Hcl2 Hv1 Hv2.
iApply mwp_un_bi_lr.
iApply mwp_wand_r; iSplitL.
{ iApply "Hf_typed_l". by iExists _. }
iIntros (???) "( -> & #Hf_typed_l' )".
iApply mwp_wand_r; iSplitL.
{ iApply "Hf_typed_r". by iExists _. }
iIntros (???) "( -> & #Hf_typed_r' ) /=".
iApply mwp_un_bi_lr. rewrite !pair_to_val.
iApply ((@mwp_step_fupd_alloc _ secG_un_left) with "[//]").
iNext. iIntros (ι1) "Hι1".
iApply mwp_fupd.
iApply ((@mwp_step_fupd_alloc _ secG_un_right) with "[//]").
iNext. iIntros (ι2) "Hι2"; cbn.
iMod (inv_alloc Ncache _ (cache_inv _ _ ι1 ι2 f _ _) with "[-]") as "#HisCache".
{
iModIntro. rewrite /cache_inv.
iExists _, _, _, _. iFrame. iFrame "#". repeat iSplitL; try done.
all: by iExists _.
}
iApply mwp_left_pure_step; first done.
iApply mwp_left_pure_step_index; first done.
iModIntro. asimpl.
iPoseProof ((compute_typed _ _ f1 f2) with "HCoh") as "compute_typed"; [done|done|].
rewrite -!Hv1 !Hcl1 -!Hv2 !Hcl2 Hv1 Hv2.
iApply "compute_typed".
- rewrite /is_cache_ref.
iExists _, _; eauto.
- rewrite /isWellBehaved. do 2 iModIntro. iFrame "#".
Qed.
End bi_defs.