-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiset.v
602 lines (532 loc) · 20.6 KB
/
multiset.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
From iris.base_logic Require Import invariants.
From iris_ni.logrel Require Import types.
From iris_ni.program_logic Require Import dwp heap_lang_lifting.
From iris.proofmode Require Import proofmode.
From iris.heap_lang Require Import lang proofmode.
From iris_ni.proofmode Require Import dwp_tactics.
From iris_ni.logrel Require Import interp.
From iris_ni.examples Require Import lock.
(*
type node = NONE | SOME int*list
and type list = ref node
*)
Definition cons : val := λ: "v" "tl",
ref (SOME ("v", "tl")).
Definition nil : expr := ref NONEV.
Definition insert_unordered : val := λ: "hd" "v",
"hd" <- SOME ("v", ref !"hd").
Definition insert_loop : val := rec: "loop" "hd" "v" :=
match: !"hd" with
NONE => "hd" <- SOME ("v", nil)
| SOME "x" =>
let: "w" := Fst "x" in
let: "tl" := Snd "x" in
if: "w" ≤ "v"
then "hd" <- SOME ("v", ref (SOME ("w", "tl")))
else "loop" "tl" "v"
end.
Definition insert_ordered : val := λ: "hd" "v",
insert_loop "hd" "v".
Definition lookup_loop : val := rec: "loop" "hd" "v" :=
match: !"hd" with
NONE => #0
| SOME "x" =>
let: "w" := Fst "x" in
let: "tl" := Snd "x" in
if: "w" = "v"
then #1 + "loop" "tl" "v"
else #0 + "loop" "tl" "v"
end.
Definition lookup_loop_ordered : val := rec: "loop" "hd" "v" :=
match: !"hd" with
NONE => #0
| SOME "x" =>
let: "w" := Fst "x" in
let: "tl" := Snd "x" in
if: "w" = "v"
then #1 + "loop" "tl" "v"
else if: "w" < "v"
then #0
else "loop" "tl" "v"
end.
Definition lookup_low : val := λ: "l" "v",
let: "x" := !"l" in
let: "l-high" := Fst "x" in
let: "l-low" := Snd "x" in
lookup_loop_ordered "l-low" "v".
Definition lookup : val := λ: "l" "v",
let: "x" := !"l" in
let: "l-high" := Fst "x" in
let: "l-low" := Snd "x" in
lookup_loop "l-high" "v".
Definition insert : val := λ: "l" "v" "f",
let: "x" := !"l" in
let: "l-high" := Fst "x" in
let: "l-low" := Snd "x" in
if: "f"
then insert_unordered "l-high" "v"
else insert_ordered "l-low" "v".
Definition size_loop : val := rec: "loop" "hd" "n" :=
match: !"hd" with
NONE => "n"
| SOME "x" =>
let: "tl" := Snd "x" in
"loop" "tl" ("n"+#1)
end.
Definition size : val := λ: "l",
let: "x" := !"l" in
let: "l-high" := Fst "x" in
let: "l-low" := Snd "x" in
size_loop "l-low" #0 + size_loop "l-high" #0.
Definition new_ms : val := λ: <>,
let: "hs" := ref NONE in
let: "ls" := ref NONE in
let: "ms" := ref ("hs", "ls") in
let: "lk" := newlock #() in
let: "insert" := λ: "v" "b",
acquire "lk";; insert "ms" "v" "b";; release "lk"
in
let: "size" := λ: <>,
acquire "lk";; let: "n" := size "ms" in release "lk";; "n"
in ("insert", "size").
Section proof.
Context `{!heapDG Σ, !lockG Σ}.
Definition joint_list_pre (P : val → val → iPropO Σ)
(R : locO -n> locO -n> iPropO Σ) : (locO -n> locO -n> iPropO Σ) := (λne hd1 hd2,
(hd1 ↦ₗ NONEV ∗ hd2 ↦ᵣ NONEV)
∨ (∃ v1 v2 (tl1 tl2 : loc), hd1 ↦ₗ SOMEV (v1, #tl1) ∗
hd2 ↦ᵣ SOMEV (v2, #tl2) ∗
P v1 v2 ∗
▷ (R tl1 tl2)))%I.
Instance joint_list_pre_contractive P : Contractive (joint_list_pre P).
Proof. solve_contractive. Qed.
Definition joint_list P := fixpoint (joint_list_pre P).
Lemma joint_list_unfold P hd1 hd2 :
joint_list P hd1 hd2 ≡
((hd1 ↦ₗ NONEV ∗ hd2 ↦ᵣ NONEV)
∨ (∃ v1 v2 (tl1 tl2 : loc), hd1 ↦ₗ SOMEV (v1, #tl1) ∗
hd2 ↦ᵣ SOMEV (v2, #tl2) ∗
P v1 v2 ∗
▷ (joint_list P tl1 tl2)))%I.
Proof.
rewrite {1}/joint_list.
transitivity (joint_list_pre P (fixpoint (joint_list_pre P)) hd1 hd2).
{ f_equiv. f_equiv. apply fixpoint_unfold. }
reflexivity.
Qed.
Definition sec_list (hd1 hd2 : loc) (l ξ : slevel) :=
(joint_list (⟦ tint l ⟧ ξ) hd1 hd2)%I.
Definition ls_inv (l1 l2 : loc) (ξ : slevel) :=
(∃ (hdh1 hdh2 hdl1 hdl2 : loc),
l1 ↦ₗ (#hdh1, #hdl1) ∗ l2 ↦ᵣ (#hdh2, #hdl2) ∗
sec_list hdh1 hdh2 High ξ ∗
sec_list hdl1 hdl2 Low ξ)%I.
Lemma lookup_unordered_spec (hd1 hd2 : loc) v1 v2 ξ Φ :
⟦ tint High ⟧ ξ v1 v2 -∗
sec_list hd1 hd2 High ξ -∗
▷ (∀ i1 i2, ⟦ tint High ⟧ ξ i1 i2 -∗
sec_list hd1 hd2 High ξ -∗ Φ i1 i2) -∗
DWP lookup_loop #hd1 v1
& lookup_loop #hd2 v2 : Φ.
Proof.
iIntros "#Hvv Hls HΦ".
iLöb as "IH" forall (hd1 hd2 Φ).
dwp_rec. dwp_pures.
dwp_bind (! _)%E (! _)%E.
rewrite {1}/(sec_list hd1 hd2) joint_list_unfold.
iDestruct "Hls" as "[[Hhd1 Hhd2]|Hls]".
- iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures. iApply dwp_value.
iModIntro. iApply ("HΦ" with "[] [-]").
+ rewrite interp_eq.
iExists 0,0. eauto with iFrame.
+ rewrite /sec_list joint_list_unfold.
iLeft. iFrame.
- iDestruct "Hls" as (w1 w2 tl1 tl2) "(Hhd1 & Hhd2 & #Hww & Hls)".
iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures.
(* w1 w2, v1 v2 are integers *)
iDestruct "Hww" as (i1 i2 -> ->) "%".
iDestruct "Hvv" as (j1 j2 -> ->) "%".
dwp_pures.
case_bool_decide; case_bool_decide; dwp_pures.
+ dwp_bind (lookup_loop _ _) (lookup_loop _ _).
iApply ("IH" with "Hls [-]").
iNext. iIntros (m1' m2') "#Hm Hls".
iDestruct "Hm" as (m1 m2 -> ->) "Hm".
iDestruct "Hm" as %?.
dwp_pures. iApply dwp_value. iApply ("HΦ" with "[] [-]").
* iExists _,_. repeat iSplit; eauto with iFrame.
iPureIntro. destruct ξ; naive_solver.
* rewrite /(sec_list hd1 hd2) joint_list_unfold.
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hls".
iExists _,_. repeat iSplit; eauto with iFrame.
+ dwp_bind (lookup_loop _ _) (lookup_loop _ _).
iApply ("IH" with "Hls [-]").
iNext. iIntros (m1' m2') "#Hm Hls".
iDestruct "Hm" as (m1 m2 -> ->) "Hm".
iDestruct "Hm" as %?.
dwp_pures. iApply dwp_value. iApply ("HΦ" with "[] [-]").
* iExists _,_. repeat iSplit; eauto with iFrame.
iPureIntro. destruct ξ; naive_solver.
* rewrite /(sec_list hd1 hd2) joint_list_unfold.
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hls".
iExists _,_. repeat iSplit; eauto with iFrame.
iPureIntro. destruct ξ; naive_solver.
+ dwp_bind (lookup_loop _ _) (lookup_loop _ _).
iApply ("IH" with "Hls [-]").
iNext. iIntros (m1' m2') "#Hm Hls".
iDestruct "Hm" as (m1 m2 -> ->) "Hm".
iDestruct "Hm" as %?.
dwp_pures. iApply dwp_value. iApply ("HΦ" with "[] [-]").
* iExists _,_. repeat iSplit; eauto with iFrame.
iPureIntro. destruct ξ; naive_solver.
* rewrite /(sec_list hd1 hd2) joint_list_unfold.
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hls".
iExists _,_. repeat iSplit; eauto with iFrame.
iPureIntro. destruct ξ; naive_solver.
+ dwp_bind (lookup_loop _ _) (lookup_loop _ _).
iApply ("IH" with "Hls [-]").
iNext. iIntros (m1' m2') "#Hm Hls".
iDestruct "Hm" as (m1 m2 -> ->) "Hm".
iDestruct "Hm" as %?.
dwp_pures. iApply dwp_value. iApply ("HΦ" with "[] [-]").
* iExists _,_. repeat iSplit; eauto with iFrame.
* rewrite /(sec_list hd1 hd2) joint_list_unfold.
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hls".
iExists _,_. repeat iSplit; eauto with iFrame.
Qed.
Lemma lookup_ordered_spec (hd1 hd2 : loc) v1 v2 ξ Φ :
⟦ tint Low ⟧ ξ v1 v2 -∗
sec_list hd1 hd2 Low ξ -∗
▷ (∀ i1 i2, ⟦ tint Low ⟧ ξ i1 i2 -∗
sec_list hd1 hd2 Low ξ -∗ Φ i1 i2) -∗
DWP lookup_loop_ordered #hd1 v1
& lookup_loop_ordered #hd2 v2 : Φ.
Proof.
iIntros "#Hvv Hls HΦ".
iLöb as "IH" forall (hd1 hd2 Φ).
dwp_rec. dwp_pures.
dwp_bind (! _)%E (! _)%E.
rewrite {1}/(sec_list hd1 hd2) joint_list_unfold.
iDestruct "Hls" as "[[Hhd1 Hhd2]|Hls]".
- iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures. iApply dwp_value.
iModIntro. iApply ("HΦ" with "[] [-]").
+ rewrite interp_eq.
iExists 0,0. eauto with iFrame.
+ rewrite /sec_list joint_list_unfold.
iLeft. iFrame.
- iDestruct "Hls" as (w1 w2 tl1 tl2) "(Hhd1 & Hhd2 & #Hww & Hls)".
iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures.
(* w1 w2, v1 v2 are integers *)
iDestruct "Hww" as (? w -> ->) "H".
iDestruct "H" as %Hw.
assert (Low ⊑ ξ) as Hξ.
{ by destruct ξ. }
rewrite (Hw Hξ).
iDestruct "Hvv" as (? v -> ->) "H".
iDestruct "H" as %Hv.
rewrite (Hv Hξ).
(* now we can continue symbolic execution *)
dwp_pures. case_bool_decide; dwp_pures.
+ (* Found another occcurence *)
dwp_bind (lookup_loop_ordered _ _) (lookup_loop_ordered _ _).
iApply ("IH" with "Hls [-]").
iNext. iIntros (i1 i2) "#Hi Hls".
iDestruct "Hi" as (? i -> ->) "H".
iDestruct "H" as %Hi.
rewrite (Hi Hξ).
dwp_pures. iApply dwp_value. iApply ("HΦ" with "[] [-]").
* iExists _,_. eauto with iFrame.
* rewrite /(sec_list hd1 hd2) joint_list_unfold.
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hls".
iExists w. eauto with iFrame.
+ case_bool_decide; dwp_pures.
++ iApply dwp_value. iApply ("HΦ" with "[] [-]").
* iExists _,_. eauto with iFrame.
* rewrite /(sec_list hd1 hd2) (joint_list_unfold _ hd1 hd2).
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hls".
iExists w. eauto with iFrame.
++ iApply ("IH" with "Hls [-]").
iNext. iIntros (i1 i2) "#Hi Hls".
iDestruct "Hi" as (? i -> ->) "H".
iDestruct "H" as %Hi.
rewrite (Hi Hξ). iApply ("HΦ" with "[] [-]").
{ iExists i. eauto with iFrame. }
{ rewrite /(sec_list hd1 hd2) (joint_list_unfold _ hd1 hd2).
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hls".
iExists w. eauto with iFrame. }
Qed.
Lemma insert_unordered_spec (hd1 hd2 : loc) v1 v2 ξ Φ :
⟦ tint High ⟧ ξ v1 v2 -∗
sec_list hd1 hd2 High ξ -∗
▷ (sec_list hd1 hd2 High ξ -∗ Φ #() #()) -∗
DWP insert_unordered #hd1 v1
& insert_unordered #hd2 v2 : Φ.
Proof.
iIntros "#Hvv Hls HΦ".
dwp_rec. dwp_pures.
dwp_pures. rewrite {1}/sec_list joint_list_unfold.
dwp_bind (! _)%E (! _)%E.
iDestruct "Hls" as "[[Hhd1 Hhd2]|Hls]".
- iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures. dwp_bind (ref _)%E (ref _)%E.
iApply dwp_alloc.
iIntros (nil1 nil2) "Hnil1 Hnil2". iNext.
dwp_pures. iApply (heap_lang_lifting.dwp_store with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
iApply "HΦ". rewrite /sec_list.
rewrite joint_list_unfold.
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hvv".
iNext. rewrite joint_list_unfold.
iLeft; by iFrame.
- iDestruct "Hls" as (w1 w2 tl1 tl2) "(Hhd1 & Hhd2 & #Hww & Hls)".
iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures. dwp_bind (ref _)%E (ref _)%E.
iApply heap_lang_lifting.dwp_alloc.
iIntros (hd1' hd2') "Hhd1' Hhd2'". iNext.
dwp_pures. iApply (heap_lang_lifting.dwp_store with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
iApply "HΦ". rewrite /sec_list.
rewrite (joint_list_unfold _ hd1 hd2).
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hvv".
iNext. rewrite (joint_list_unfold _ hd1').
iRight. iExists _,_,_,_. by iFrame.
Qed.
Lemma insert_ordered_spec (hd1 hd2 : loc) v1 v2 ξ Φ :
⟦ tint Low ⟧ ξ v1 v2 -∗
sec_list hd1 hd2 Low ξ -∗
▷ (sec_list hd1 hd2 Low ξ -∗ Φ #() #()) -∗
DWP insert_ordered #hd1 v1
& insert_ordered #hd2 v2 : Φ.
Proof.
iIntros "#Hvv Hls HΦ".
dwp_rec. dwp_pures.
iLöb as "IH" forall (hd1 hd2).
dwp_rec. dwp_pures.
dwp_bind (! _)%E (! _)%E.
rewrite {1}/(sec_list hd1 hd2) joint_list_unfold.
iDestruct "Hls" as "[[Hhd1 Hhd2]|Hls]".
- iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures. dwp_bind (ref (InjLV #()))%E (ref (InjLV #()))%E.
iApply heap_lang_lifting.dwp_alloc.
iIntros (nil1 nil2) "Hnil1 Hnil2". iNext.
dwp_pures. iApply (heap_lang_lifting.dwp_store with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
iApply "HΦ". rewrite /sec_list.
rewrite joint_list_unfold.
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2 Hvv".
iNext. rewrite joint_list_unfold.
iLeft; by iFrame.
- iDestruct "Hls" as (w1 w2 tl1 tl2) "(Hhd1 & Hhd2 & #Hww & Hls)".
iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures.
(* w1 w2, v1 v2 are integers *)
iDestruct "Hww" as (? w -> ->) "H".
iDestruct "H" as %Hw.
assert (Low ⊑ ξ) as Hξ.
{ by destruct ξ. }
rewrite (Hw Hξ).
iDestruct "Hvv" as (? v -> ->) "H".
iDestruct "H" as %Hv.
rewrite (Hv Hξ).
(* now we can continue symbolic execution *)
dwp_pures. case_bool_decide; dwp_pures.
+ (* Insert the element here *)
dwp_bind (ref _)%E (ref _)%E.
iApply heap_lang_lifting.dwp_alloc.
iIntros (hd1' hd2') "Hhd1' Hhd2'". iNext.
dwp_pures. iApply (heap_lang_lifting.dwp_store with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
iApply "HΦ". rewrite /sec_list.
rewrite (joint_list_unfold _ hd1 hd2).
iRight. iExists _,_,_,_. iFrame "Hhd1 Hhd2".
iSplitR.
{ iExists _,_. eauto with iFrame. }
iNext. rewrite (joint_list_unfold _ hd1' hd2').
iRight. iExists _,_,_,_. iFrame "Hhd1' Hhd2' Hls".
iExists _,_. eauto with iFrame.
+ (* Continue with the recursion. *)
iApply ("IH" with "Hls [-]").
iIntros "Htls". iApply "HΦ".
rewrite /sec_list (joint_list_unfold _ hd1 hd2).
iRight. iExists _,_,_,_. iFrame.
iExists _. eauto with iFrame.
Qed.
Lemma size_loop_spec (hd1 hd2 : loc) n1 n2 l ξ Φ :
⟦ tint Low ⟧ ξ n1 n2 -∗
sec_list hd1 hd2 l ξ -∗
(∀ v1 v2, sec_list hd1 hd2 l ξ -∗ ⟦ tint Low ⟧ ξ v1 v2 -∗ Φ v1 v2) -∗
DWP size_loop #hd1 n1 & size_loop #hd2 n2 : Φ.
Proof.
iIntros "Hns Hls HΦ".
iLöb as "IH" forall (n1 n2 hd1 hd2).
iDestruct "Hns" as "#Hns". dwp_rec. dwp_pures.
dwp_bind (! _)%E (! _)%E.
rewrite {1}/(sec_list hd1 hd2) (joint_list_unfold _ hd1 hd2).
iDestruct "Hls" as "[[Hhd1 Hhd2]|Hls]".
- iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures. iApply dwp_value.
iModIntro. iApply ("HΦ" with "[-Hns] Hns").
iClear "IH". rewrite /sec_list joint_list_unfold.
iLeft. by iFrame.
- iDestruct "Hls" as (w1 w2 tl1 tl2) "(Hhd1 & Hhd2 & #Hww & Hls)".
iApply (dwp_load with "Hhd1 Hhd2").
iIntros "Hhd1 Hhd2". iNext.
dwp_pures.
dwp_bind (_ + _)%E (_ + _)%E.
iApply dwp_wand.
{ (iApply logrel_binop_int; first by constructor);
iApply dwp_value; first by iFrame.
iModIntro. iExists _; eauto with iFrame. }
iIntros (z1 z2) "Hz".
rewrite (left_id Low).
iApply ("IH" with "Hz Hls [-]").
iClear "IH". iIntros (v1 v2) "Hls Hvv".
iApply ("HΦ" with "[-Hvv] Hvv").
rewrite /sec_list (joint_list_unfold _ hd1 hd2).
iRight. iExists _,_,_,_. by iFrame.
Qed.
Lemma lookup_spec l1 l2 v1 v2 ξ Φ :
ls_inv l1 l2 ξ -∗
⟦ tint High ⟧ ξ v1 v2 -∗
▷ (∀ i1 i2, ⟦ tint High ⟧ ξ i1 i2 -∗ ls_inv l1 l2 ξ -∗ Φ i1 i2) -∗
DWP lookup #l1 v1 & lookup #l2 v2 : Φ.
Proof.
iDestruct 1 as (hdh1 hdh2 hdl1 hdl2) "(Hl1 & Hl2 & Hhs & Hls)".
iIntros "#Hvv HΦ".
dwp_rec. dwp_pures.
dwp_bind (! _)%E (! _)%E. iApply (dwp_load with "Hl1 Hl2").
iIntros "Hl1 Hl2". iNext.
dwp_pures.
iApply (lookup_unordered_spec with "Hvv Hhs").
iNext. iIntros (i1 i2) "Hi Hhs". iApply ("HΦ" with "Hi [-]").
iExists _. eauto with iFrame.
Qed.
Lemma lookup_low_spec l1 l2 v1 v2 ξ Φ :
ls_inv l1 l2 ξ -∗
⟦ tint Low ⟧ ξ v1 v2 -∗
▷ (∀ i1 i2, ⟦ tint Low ⟧ ξ i1 i2 -∗ ls_inv l1 l2 ξ -∗ Φ i1 i2) -∗
DWP lookup_low #l1 v1 & lookup_low #l2 v2 : Φ.
Proof.
iDestruct 1 as (hdh1 hdh2 hdl1 hdl2) "(Hl1 & Hl2 & Hhs & Hls)".
iIntros "#Hvv HΦ".
dwp_rec. dwp_pures.
dwp_bind (! _)%E (! _)%E. iApply (dwp_load with "Hl1 Hl2").
iIntros "Hl1 Hl2". iNext.
dwp_pures.
iApply (lookup_ordered_spec with "Hvv Hls").
iNext. iIntros (i1 i2) "Hi Hls". iApply ("HΦ" with "Hi [-]").
iExists _. eauto with iFrame.
Qed.
(* TODO: Question: can the b's here be different? *)
Lemma insert_spec l1 l2 v1 v2 (b : bool) ξ Φ :
ls_inv l1 l2 ξ -∗
⟦ tint (if b then High else Low) ⟧ ξ v1 v2 -∗
▷ (ls_inv l1 l2 ξ -∗ Φ #() #()) -∗
DWP insert #l1 v1 #b & insert #l2 v2 #b : Φ.
Proof.
iDestruct 1 as (hdh1 hdh2 hdl1 hdl2) "(Hl1 & Hl2 & Hhs & Hls)".
iIntros "#Hvv HΦ".
dwp_rec. dwp_pures.
dwp_bind (! _)%E (! _)%E. iApply (dwp_load with "Hl1 Hl2").
iIntros "Hl1 Hl2". iNext.
dwp_pures. destruct b; dwp_pures.
- (* High-security *)
iApply (insert_unordered_spec with "Hvv Hhs").
iNext. iIntros "Hhs". iApply "HΦ".
iExists _,_,_,_. iFrame.
- (* Low-security *)
iApply (insert_ordered_spec with "Hvv Hls").
iNext. iIntros "Hls". iApply "HΦ".
iExists _,_,_,_. iFrame.
Qed.
Lemma size_spec l1 l2 ξ Φ :
ls_inv l1 l2 ξ -∗
▷ (∀ v1 v2, ⟦ tint Low ⟧ ξ v1 v2 -∗ ls_inv l1 l2 ξ -∗ Φ v1 v2) -∗
DWP size #l1 & size #l2 : Φ.
Proof.
iDestruct 1 as (hdh1 hdh2 hdl1 hdl2) "(Hl1 & Hl2 & Hhs & Hls)".
iIntros "HΦ".
dwp_rec.
dwp_bind (! _)%E (! _)%E. iApply (dwp_load with "Hl1 Hl2").
iIntros "Hl1 Hl2". iNext.
dwp_pures. dwp_bind (size_loop _ _) (size_loop _ _).
iApply (size_loop_spec with "[] Hhs").
{ iExists _; eauto with iFrame. }
iIntros (n1 n2) "Hhs #Hn".
dwp_bind (size_loop _ _) (size_loop _ _).
iApply (size_loop_spec with "[] Hls").
{ iExists _; eauto with iFrame. }
iIntros (m1 m2) "Hls #Hm".
iApply dwp_wand.
{ (iApply logrel_binop_int; try by constructor);
iApply dwp_value; eauto with iFrame. }
iIntros (z1 z2) "Hz".
rewrite (left_id Low).
iApply ("HΦ" with "Hz [-]").
iExists _,_,_,_. by iFrame "Hl1 Hl2 Hhs Hls".
Qed.
Lemma multiset_spec ξ Φ :
(∀ (ins1 ins2 siz1 siz2 : val),
□ (∀ (b: bool) v1 v2,
⟦ tint (if b then High else Low) ⟧ ξ v1 v2 -∗
DWP ins1 v1 #b & ins2 v2 #b : ⟦ tunit ⟧ ξ) -∗
⟦ tarrow tunit (tint Low) Low ⟧ ξ siz1 siz2 -∗
Φ (ins1, siz1)%V (ins2, siz2)%V) -∗
DWP new_ms #() & new_ms #() : Φ.
Proof.
iIntros "HΦ".
dwp_rec. dwp_pures.
dwp_bind (ref _)%E (ref _)%E.
iApply heap_lang_lifting.dwp_alloc.
iIntros (hs1 hs2) "Hhs1 Hhs2". iNext.
dwp_pures. dwp_bind (ref _)%E (ref _)%E.
iApply heap_lang_lifting.dwp_alloc.
iIntros (ls1 ls2) "Hls1 Hls2". iNext.
dwp_pures. dwp_bind (ref _)%E (ref _)%E.
iApply heap_lang_lifting.dwp_alloc.
iIntros (ms1 ms2) "Hms1 Hms2". iNext.
dwp_pures. dwp_bind (newlock _) (newlock _).
pose (N:=nroot.@"あ").
iApply (newlock_spec N (ls_inv ms1 ms2 ξ) with "[-HΦ]").
{ iExists _,_,_,_. iFrame.
iSplitL "Hhs1 Hhs2";
rewrite /sec_list joint_list_unfold; iLeft; by iFrame. }
iIntros (γ lk1 lk2) "#Hlock".
dwp_pures. iApply dwp_value. iModIntro.
iApply "HΦ".
- iModIntro. iIntros (b v1 v2) "Hv".
dwp_pures. dwp_bind (acquire _) (acquire _).
iApply (acquire_spec with "Hlock").
iIntros "Hlk Hls".
dwp_pures. dwp_bind (insert _ _ _) (insert _ _ _).
iApply (insert_spec with "Hls Hv").
iNext. iIntros "Hls".
dwp_pures. iApply (release_spec with "Hlock Hlk Hls").
eauto with iFrame.
- rewrite interp_eq /= /lrel_car /=. iModIntro.
iIntros (z1 z2) "_". dwp_rec.
dwp_bind (acquire _) (acquire _).
iApply (acquire_spec with "Hlock").
iIntros "Hlk Hls".
dwp_pures. dwp_bind (size _) (size _).
iApply (size_spec with "Hls").
iNext. iIntros (v1 v2) "Hv Hls".
dwp_pures. dwp_bind (release _) (release _).
iApply (release_spec with "Hlock Hlk Hls").
dwp_pures. iApply dwp_value. iModIntro.
iSimpl. iFrame "Hv".
Qed.
End proof.