-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitive_laws.v
1242 lines (1107 loc) · 49.6 KB
/
primitive_laws.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** This file proves the basic laws of the HeapLang program logic by applying
the Iris lifting lemmas. *)
From iris.proofmode Require Import proofmode.
From iris.algebra Require Import big_op gmap dfrac numbers.
From iris.algebra Require Import csum excl auth cmra_big_op.
From iris.bi.lib Require Import fractional.
From iris.base_logic.lib Require Import ghost_map invariants mono_nat.
From iris.base_logic.lib Require Export proph_map.
From iris.program_logic Require Export weakestpre total_weakestpre.
From iris.program_logic Require Import ectx_lifting total_ectx_lifting.
From smr.lang Require Export class_instances.
From smr.lang Require Import tactics notation.
From iris.prelude Require Import options.
Definition heap_freeableUR : ucmra :=
gmapUR blk (prodR fracR (gmapR Z (exclR unitO))).
(* we roll our own version of inv_heap stuff, because the original one depends
on iris's [gen_heapGS]. *)
Definition inv_heap_mapUR : ucmra := gmapUR loc $ prodR
(optionR $ exclR $ valO)
(agreeR (val -d> PropO)).
Class heapGS_gen hlc Σ := HeapGS {
heapGS_invGS : invGS_gen hlc Σ;
heap_name : gname;
heapGS_inG :> ghost_mapG Σ loc val;
inv_heap_name : gname;
heapGS_inv_heapGS :> inG Σ (authR inv_heap_mapUR);
heap_freeable_name : gname;
heap_freeable_inG :> inG Σ (authR heap_freeableUR); (* freeable predicate to use in Alloc/Free *)
heapGS_proph_mapGS :> proph_mapGS proph_id (val * val) Σ;
heapGS_step_name : gname;
heapGS_step_cnt : mono_natG Σ;
}.
Local Existing Instance heapGS_step_cnt.
Notation heapGS := (heapGS_gen HasLc).
Section steps.
Context `{!heapGS_gen hlc Σ}.
Local Definition steps_auth (n : nat) : iProp Σ :=
mono_nat_auth_own heapGS_step_name 1 n.
Definition steps_lb (n : nat) : iProp Σ :=
mono_nat_lb_own heapGS_step_name n.
Local Lemma steps_lb_valid n m :
steps_auth n -∗ steps_lb m -∗ ⌜m ≤ n⌝.
Proof.
iIntros "Hauth Hlb".
by iDestruct (mono_nat_lb_own_valid with "Hauth Hlb") as %[_ Hle].
Qed.
Local Lemma steps_lb_get n :
steps_auth n -∗ steps_lb n.
Proof. apply mono_nat_lb_own_get. Qed.
Local Lemma steps_auth_update n n' :
n ≤ n' → steps_auth n ==∗ steps_auth n' ∗ steps_lb n'.
Proof. intros Hle. by apply mono_nat_own_update. Qed.
Local Lemma steps_auth_update_S n :
steps_auth n ==∗ steps_auth (S n).
Proof.
iIntros "Hauth".
iMod (mono_nat_own_update with "Hauth") as "[$ _]"; [lia|done].
Qed.
Lemma steps_lb_le n n' :
n' ≤ n → steps_lb n -∗ steps_lb n'.
Proof. intros Hle. by apply mono_nat_lb_own_le. Qed.
End steps.
Definition heap_freeable_rel (m : memory) (hF : heap_freeableUR) : Prop :=
∀ b qs, hF !! b = Some qs →
qs.2 ≠ ∅ ∧ ∀ i, is_Some (m !! (b, i)) ↔ is_Some (qs.2 !! i).
Section heap_definitions.
Context `{!heapGS_gen hlc Σ}.
Definition mapsto_st
(l : loc) (dq : dfrac) (v: val) : iProp Σ :=
l ↪[heap_name]{dq} v.
Definition mapsto_def (l : loc) (d : dfrac) (v: val) : iProp Σ :=
mapsto_st l d v.
Definition mapsto_aux : seal (@mapsto_def). Proof. by eexists. Qed.
Definition mapsto := unseal mapsto_aux.
Definition mapsto_unseal : @mapsto = @mapsto_def :=
mapsto_aux.(seal_eq).
Definition array (l : loc) (d : dfrac) (vl : list val) : iProp Σ :=
([∗ list] i ↦ v ∈ vl, mapsto (l +ₗ i) d v)%I.
Fixpoint inter (i0 : Z) (n : nat) : gmapR Z (exclR unitO) :=
match n with O => ∅ | S n => <[i0 := Excl ()]>(inter (i0+1) n) end.
Definition heap_freeable_def (l : loc) (q : frac) (n: nat) : iProp Σ :=
own heap_freeable_name (◯ {[ l.1 := (q, inter (l.2) n) ]}).
Definition heap_freeable_aux : seal (@heap_freeable_def). Proof. by eexists. Qed.
Definition heap_freeable := unseal heap_freeable_aux.
Definition heap_freeable_unseal : @heap_freeable = @heap_freeable_def :=
heap_freeable_aux.(seal_eq).
Definition heap_ctx (mem:memory) : iProp Σ :=
(∃ hF, ghost_map_auth heap_name 1 mem
∗ own heap_freeable_name (● hF)
∗ ⌜heap_freeable_rel mem hF⌝)%I.
End heap_definitions.
Notation "l ↦ dq v" := (mapsto l dq v)
(at level 20, dq custom dfrac at level 1, format "l ↦ dq v") : bi_scope.
Notation "l ↦∗ dq vs" := (array l dq vs)
(at level 20, dq custom dfrac at level 1, format "l ↦∗ dq vs") : bi_scope.
Notation "†{ q } l … n" := (heap_freeable l q n)
(at level 20, q at level 50, format "†{ q } l … n") : bi_scope.
Notation "† l … n" := (heap_freeable l 1 n) (at level 20) : bi_scope.
Section heap.
Context `{!heapGS_gen hlc Σ}.
Implicit Types P Q : iProp Σ.
Implicit Types σ : memory.
Implicit Types E : coPset.
Local Open Scope Z.
(** General properties of mapsto and freeable *)
Global Instance mapsto_timeless l d v : Timeless (mapsto l d v).
Proof. rewrite mapsto_unseal /mapsto_def. apply _. Qed.
Global Instance mapsto_fractional l v: Fractional (λ q, l ↦{# q} v)%I.
Proof.
rewrite mapsto_unseal. unfold mapsto_def. unfold mapsto_st.
apply ghost_map_elem_fractional.
Qed.
Global Instance mapsto_as_fractional l q v:
AsFractional (l ↦{# q} v) (λ q, l ↦{# q} v)%I q.
Proof. split; first done. apply _. Qed.
Global Instance mapsto_persistent l v : Persistent (l ↦□ v).
Proof. rewrite mapsto_unseal. apply _. Qed.
Global Instance frame_mapsto p l v q1 q2 q :
FrameFractionalQp q1 q2 q →
Frame p (l ↦{# q1} v) (l ↦{# q2} v) (l ↦{# q} v) | 5.
Proof. apply: frame_fractional. Qed.
Global Instance array_timeless l q vl : Timeless (l ↦∗{q} vl).
Proof. rewrite /array. apply _. Qed.
Global Instance array_fractional l vl: Fractional (λ q, l ↦∗{# q} vl)%I.
Proof.
intros p q. rewrite /array -big_sepL_sep.
by setoid_rewrite (fractional (Φ := λ q, _ ↦{# q} _)%I).
Qed.
Global Instance array_as_fractional l q vl:
AsFractional (l ↦∗{# q} vl) (λ q, l ↦∗{# q} vl)%I q.
Proof. split; first done. apply _. Qed.
Global Instance heap_freeable_timeless q l n : Timeless (†{q}l…n).
Proof. rewrite heap_freeable_unseal /heap_freeable_def. apply _. Qed.
Lemma mapsto_valid l dq v : l ↦{dq} v -∗ ⌜✓ dq⌝.
Proof. rewrite mapsto_unseal. apply ghost_map_elem_valid. Qed.
Lemma mapsto_valid_2 l dq1 dq2 v1 v2 : l ↦{dq1} v1 -∗ l ↦{dq2} v2 -∗ ⌜✓ (dq1 ⋅ dq2) ∧ v1 = v2⌝.
Proof. rewrite mapsto_unseal. apply ghost_map_elem_valid_2. Qed.
Lemma mapsto_agree l d1 d2 v1 v2 : l ↦{d1} v1 -∗ l ↦{d2} v2 -∗ ⌜v1 = v2⌝.
Proof. iIntros "l↦v1 l↦v2". iDestruct (mapsto_valid_2 with "l↦v1 l↦v2") as "[_ $]". Qed.
Lemma mapsto_combine l dq1 dq2 v1 v2 :
l ↦{dq1} v1 -∗ l ↦{dq2} v2 -∗ l ↦{dq1 ⋅ dq2} v1 ∗ ⌜v1 = v2⌝.
Proof. rewrite mapsto_unseal. apply ghost_map_elem_combine. Qed.
Lemma mapsto_frac_ne l1 l2 dq1 dq2 v1 v2 :
¬ ✓(dq1 ⋅ dq2) → l1 ↦{dq1} v1 -∗ l2 ↦{dq2} v2 -∗ ⌜l1 ≠ l2⌝.
Proof. rewrite mapsto_unseal. apply ghost_map_elem_frac_ne. Qed.
Lemma mapsto_ne l1 l2 dq2 v1 v2 : l1 ↦ v1 -∗ l2 ↦{dq2} v2 -∗ ⌜l1 ≠ l2⌝.
Proof. rewrite mapsto_unseal. apply ghost_map_elem_ne. Qed.
(** Permanently turn any points-to predicate into a persistent
points-to predicate. *)
Lemma mapsto_persist l dq v : l ↦{dq} v ==∗ l ↦□ v.
Proof. rewrite mapsto_unseal. apply ghost_map_elem_persist. Qed.
Lemma array_nil l q : l ↦∗{q} [] ⊣⊢ True.
Proof. by rewrite /array. Qed.
Lemma array_app l q vl1 vl2 :
l ↦∗{q} (vl1 ++ vl2) ⊣⊢ l ↦∗{q} vl1 ∗ (l +ₗ length vl1) ↦∗{q} vl2.
Proof.
rewrite /array big_sepL_app.
do 2 f_equiv. intros k v. by rewrite loc_add_assoc_nat.
Qed.
Lemma array_singleton l q v : l ↦∗{q} [v] ⊣⊢ l ↦{q} v.
Proof. by rewrite /array /= loc_add_0 right_id. Qed.
Lemma array_cons l q v vl:
l ↦∗{q} (v :: vl) ⊣⊢ l ↦{q} v ∗ (l +ₗ 1) ↦∗{q} vl.
Proof.
by rewrite (array_app l q [v] vl) array_singleton.
Qed.
Global Instance array_cons_frame l dq v vs R Q :
Frame false R (l ↦{dq} v ∗ (l +ₗ 1) ↦∗{dq} vs) Q →
Frame false R (l ↦∗{dq} (v :: vs)) Q | 2.
Proof. by rewrite /Frame array_cons. Qed.
Lemma array_agree l dq1 dq2 vs1 vs2 :
length vs1 = length vs2 →
l ↦∗{dq1} vs1 -∗ l ↦∗{dq2} vs2 -∗ ⌜vs1 = vs2⌝.
Proof.
revert l vs2. iInduction (vs1) as [|v1 vs1] "IH"; iIntros (l vs2 EQ); destruct vs2; auto.
iIntros "l↦1 l↦2". rewrite !array_cons.
iDestruct "l↦1" as "[l↦1 l+ₗ1↦1]". iDestruct "l↦2" as "[l↦2 l+ₗ1↦2]".
iDestruct (mapsto_agree with "l↦1 l↦2") as %[= <-].
iDestruct ("IH" with "[] [$l+ₗ1↦1] [$l+ₗ1↦2]") as %[= <-]; last done.
iPureIntro. rewrite !cons_length in EQ. by injection EQ.
Qed.
Lemma array_op l q1 q2 vl1 vl2 :
length vl1 = length vl2 →
l ↦∗{# q1} vl1 ∗ l ↦∗{# q2} vl2 ⊣⊢ ⌜vl1 = vl2⌝ ∧ l ↦∗{# q1+q2} vl1.
Proof.
intros Hlen%Forall2_same_length. apply (anti_symm (⊢)).
- revert l. induction Hlen as [|v1 v2 vl1 vl2 _ _ IH]=> l.
{ rewrite !array_nil. iIntros "_"; auto. }
rewrite !array_cons. iIntros "[[Hv1 Hvl1] [Hv2 Hvl2]]".
iDestruct (IH (l +ₗ 1) with "[$Hvl1 $Hvl2]") as "[% $]"; subst.
rewrite (inj_iff (.:: vl2)).
iDestruct (mapsto_agree with "Hv1 Hv2") as %<-.
iSplit; first done. iFrame.
- by iIntros "[% [$ Hl2]]"; subst.
Qed.
Lemma array_combine l q vl :
vl ≠ [] →
l ↦∗{q} vl ⊣⊢ [∗ list] i ↦ v ∈ vl, (l +ₗ i) ↦{q} v.
Proof.
by rewrite /array mapsto_unseal /mapsto_def /mapsto_st=>?.
Qed.
Lemma array_persist l dq vs : l ↦∗{dq} vs ==∗ l ↦∗□ vs.
Proof.
revert l. iInduction (vs) as [|v vs'] "IH"; [naive_solver|].
iIntros (l) "l↦". rewrite !array_cons. iDestruct "l↦" as "[l↦ l+ₗ1↦]".
iMod (mapsto_persist with "l↦") as "$". iApply ("IH" with "l+ₗ1↦").
Qed.
Global Instance array_persistent l vs : Persistent (l ↦∗□ vs).
Proof. destruct vs; apply _. Qed.
Lemma inter_lookup_Some i j (n : nat):
i ≤ j < i+n → inter i n !! j = Excl' ().
Proof.
revert i. induction n as [|n IH]=>/= i; first lia.
rewrite lookup_insert_Some. destruct (decide (i = j)); naive_solver lia.
Qed.
Lemma inter_lookup_None i j (n : nat):
j < i ∨ i+n ≤ j → inter i n !! j = None.
Proof.
revert i. induction n as [|n IH]=>/= i; first by rewrite lookup_empty.
rewrite lookup_insert_None. naive_solver lia.
Qed.
Lemma inter_op i n n' : inter i n ⋅ inter (i+n) n' ≡ inter i (n+n').
Proof.
intros j. rewrite lookup_op.
destruct (decide (i ≤ j < i+n)); last destruct (decide (i+n ≤ j < i+n+n')).
- by rewrite (inter_lookup_None (i+n) j n') ?inter_lookup_Some; try lia.
- by rewrite (inter_lookup_None i j n) ?inter_lookup_Some; try lia.
- by rewrite !inter_lookup_None; try lia.
Qed.
Lemma inter_valid i n : ✓ inter i n.
Proof. revert i. induction n as [|n IH]=>i; first done. by apply insert_valid. Qed.
Lemma heap_freeable_op_eq l q1 q2 n n' :
†{q1}l…n ∗ †{q2}(l +ₗ n) … n' ⊣⊢ †{q1+q2}l…(n+n').
Proof.
by rewrite heap_freeable_unseal /heap_freeable_def -own_op -auth_frag_op
singleton_op -pair_op inter_op.
Qed.
Lemma heap_freeable_valid l n n' :
†l…n -∗ †l…n' -∗ False.
Proof.
iIntros "†l †l'".
iCombine "†l †l'" as "†".
rewrite heap_freeable_unseal /heap_freeable_def -own_op -auth_frag_op.
rewrite singleton_op -pair_op own_valid.
iDestruct "†" as "%H". iPureIntro.
rewrite auth_frag_valid singleton_valid pair_valid in H.
destruct H as [H _].
rewrite frac_op frac_valid in H. done.
Qed.
(** Properties about heap_freeable_rel and heap_freeable *)
Lemma heap_freeable_rel_None σ (l:blk) hF :
(∀ m : Z, σ !! (l,m) = None) → heap_freeable_rel σ hF →
hF !! l = None.
Proof.
intros FRESH REL. apply eq_None_not_Some. intros [[q s] [Hsne REL']%REL].
destruct (map_choose s) as [i []%REL'%not_eq_None_Some]; first done.
move: (FRESH i). by rewrite /loc_add.
Qed.
Lemma heap_freeable_is_Some σ hF l n i :
heap_freeable_rel σ hF →
hF !! l.1 = Some (1%Qp, inter (l.2) n) →
is_Some (σ !! (l +ₗ i)) ↔ 0 ≤ i ∧ i < n.
Proof.
destruct l as [b j]; rewrite /loc_add /=. intros REL Hl.
destruct (REL b (1%Qp, inter j n)) as [_ ->]; simpl in *; auto.
destruct (decide (0 ≤ i ∧ i < n)).
- rewrite is_Some_alt inter_lookup_Some; lia.
- rewrite is_Some_alt inter_lookup_None; lia.
Qed.
Lemma heap_freeable_rel_init_mem (l:blk) h n v σ:
n ≠ O →
(∀ m : Z, σ !! (l,m) = None) →
heap_freeable_rel σ h →
heap_freeable_rel (init_mem l n v σ)
(<[l := (1%Qp, inter 0%Z n)]> h).
Proof.
move=> Hvlnil FRESH REL b qs /lookup_insert_Some [[<- <-]|[??]].
- split.
{ destruct n as [|n]; simpl in *; [done|]. apply: insert_non_empty. }
intros i. destruct (decide (0 ≤ i ∧ i < n)).
+ rewrite inter_lookup_Some // lookup_init_mem; naive_solver.
+ rewrite inter_lookup_None; last lia. rewrite lookup_init_mem_ne /=; last lia.
replace (l,i) with (l +ₗ i) by (rewrite /loc_add; f_equal; lia).
by rewrite FRESH !is_Some_alt.
- destruct (REL b qs) as [? Hs]; auto.
split; [done|]=> i. by rewrite -Hs lookup_init_mem_ne; last auto.
Qed.
Lemma heap_freeable_rel_free_mem σ hF n l :
hF !! l.1 = Some (1%Qp, inter (l.2) n) →
heap_freeable_rel σ hF →
heap_freeable_rel (free_mem l n σ) (delete (l.1) hF).
Proof.
intros Hl REL b qs; rewrite lookup_delete_Some=> -[NEQ ?].
destruct (REL b qs) as [? REL']; auto.
split; [done|]=> i. by rewrite -REL' lookup_free_mem_ne.
Qed.
Lemma heap_freeable_rel_stable σ h l p :
heap_freeable_rel σ h → is_Some (σ !! l) →
heap_freeable_rel (<[l := p]>σ) h.
Proof.
intros REL Hσ b qs Hqs. destruct (REL b qs) as [? REL']; first done.
split; [done|]=> i. rewrite -REL' lookup_insert_is_Some.
destruct (decide (l = (b, i))); naive_solver.
Qed.
Lemma difference_insert' (m1 m2 : memory) i x :
m1 !! i = None →
m1 ∖ <[i:=x]> m2 = m1 ∖ m2.
Proof.
intro None.
apply map_eq. intros i'. apply option_eq. intros x'.
rewrite !lookup_difference_Some !lookup_insert_None.
split.
- intros (Hm1i' & Hm2i' & Neq). done.
- intros (Hm1i' & Hm2i'). split_and!; [done..|].
apply (lookup_ne m1 i i'). rewrite None Hm1i'. done.
Qed.
(** Weakest precondition *)
Lemma heap_alloc_vs σ l n v :
(∀ m : Z, σ !! (l +ₗ m) = None) →
ghost_map_auth heap_name 1 σ
==∗ ghost_map_auth heap_name 1 (init_mem l n v σ)
∗ ([∗ list] i ↦ v ∈ (replicate n v), (l +ₗ i) ↦ v).
Proof.
revert l n.
assert (∀ l n, (∀ m : Z, σ !! (l +ₗ m) = None) → σ ⊆ init_mem l n v σ) as Subσ.
{ intros l n FREE. revert l FREE. induction n as [|n IH] => l FRESH.
{ simpl. set_solver. }
simpl. specialize IH with (l +ₗ 1). rewrite map_subseteq_spec.
intros l' v' Hσ. destruct (decide (l' = l)) as [->|NE].
- specialize FRESH with 0. rewrite loc_add_0 in FRESH.
rewrite Hσ in FRESH. inversion FRESH.
- rewrite lookup_insert_ne; [|done]. rewrite map_subseteq_spec in IH.
apply IH; [|done]. intro m. specialize FRESH with (1 + m).
rewrite -loc_add_assoc in FRESH. apply FRESH.
}
assert (∀ l n, (∀ m : Z, σ !! (l +ₗ m) = None) → init_mem l n v σ ∖ σ ##ₘ σ) as Disjσ.
{ intros l n FREE. apply map_disjoint_difference_l. by apply Subσ. }
iIntros (l n FREE) "Heap".
iDestruct ((ghost_map_insert_big ((init_mem l n v σ) ∖ σ)) with "Heap") as "HeapCond"; [by apply Disjσ|].
rewrite (_ :(init_mem l n v σ) ∖ σ ∪ σ = (init_mem l n v σ)); last first.
{ rewrite map_union_comm; [|by apply Disjσ]. apply map_difference_union. by apply Subσ. }
iMod "HeapCond" as "[$ HeapCond]".
iModIntro.
rename FREE into FRESH.
iInduction n as [|n] "IH" forall (l FRESH); [done|].
simpl.
iSpecialize ("IH" $! (l +ₗ 1)).
rewrite loc_add_0.
iDestruct (big_sepM_delete (λ k v, (k ↪[heap_name] v)%I) (<[l:=v]> (init_mem (l +ₗ 1) n v σ) ∖ σ) l v) as "[Hl _]".
{ rewrite lookup_difference_Some. split; [simplify_map_eq|].
- rewrite lookup_insert. done.
- specialize FRESH with 0. by rewrite loc_add_0 in FRESH.
}
iSpecialize ("Hl" with "HeapCond").
iDestruct "Hl" as "[Hl HCons]".
iSplitL "Hl".
{ rewrite mapsto_unseal. unfold mapsto_def. by unfold mapsto_st. }
rewrite (_ : delete l (<[l:=v]> (init_mem (l +ₗ 1) n v σ) ∖ σ) = (init_mem (l +ₗ 1) n v σ ∖ σ)); last first.
{ rewrite (delete_difference _ _ l v).
rewrite (difference_insert _ _ _ _ _ v).
apply difference_insert'.
rewrite lookup_init_mem_ne; [specialize FRESH with 0; by rewrite loc_add_0 in FRESH|].
right. left. destruct l. simpl. lia.
}
iSpecialize ("IH" with "[%] HCons").
{ intros. rewrite loc_add_assoc. done. }
iApply (big_sepL_mono with "IH").
iIntros (i ? Hi) "l↦".
rewrite loc_add_assoc. assert (1 + i = S i) as -> by lia.
done.
Qed.
Lemma heap_alloc σ (l:blk) n v :
0 < n →
(∀ m, σ !! (l,m) = None) →
heap_ctx σ ==∗
heap_ctx (init_mem l (Z.to_nat n) v σ) ∗ †l…(Z.to_nat n) ∗
l ↦∗ replicate (Z.to_nat n) v.
Proof.
intros ??; iDestruct 1 as (hF) "(Hvalσ & HhF & %)".
assert (Z.to_nat n ≠ O) as Not0 by lia.
iMod (heap_alloc_vs _ (l,0%Z) (Z.to_nat n) with "[$Hvalσ]") as "[Hvalσ Hmapsto]"; first done.
iMod (own_update _ (● hF) with "HhF") as "[HhF Hfreeable]".
{ apply auth_update_alloc,
(alloc_singleton_local_update _ l (1%Qp, inter 0%Z (Z.to_nat n))).
- eauto using heap_freeable_rel_None.
- split; first done. apply inter_valid. }
iModIntro. iSplitL "Hvalσ HhF".
{ iExists _. iFrame. iPureIntro.
auto using heap_freeable_rel_init_mem. }
rewrite heap_freeable_unseal /heap_freeable_def. iFrame.
Qed.
Definition heap_seq (vl : list val) (l: loc) : memory :=
let f := λ i v, (l +ₗ i, v) in
let heap_list := imap f vl in
list_to_map heap_list.
Lemma heap_free_vs σ l vl :
ghost_map_auth heap_name 1 σ ∗ ([∗ list] i ↦ v ∈ vl, (l +ₗ i) ↦ v)
==∗ ghost_map_auth heap_name 1 (free_mem l (length vl) σ).
Proof.
iIntros "[Heap List]".
iDestruct ((ghost_map_delete_big (heap_seq vl l)) with "Heap") as "Heap".
assert (∀ l vl, imap ((λ (i : nat) (v : val), (l +ₗ i, v)) ∘ S) vl =
imap (λ (i : nat) (v : val), (l +ₗ 1 +ₗ i, v)) vl) as Himap.
{ intros. apply imap_ext. intros. simpl. f_equal.
rewrite loc_add_assoc. f_equal. lia. }
rewrite (_ : σ ∖ heap_seq vl l = free_mem l (length vl) σ); last first.
{ revert l. induction vl as [|v vl IH] => l.
{ by rewrite map_difference_empty. }
unfold heap_seq. simpl. rewrite loc_add_0.
rewrite -delete_difference. simpl. f_equal.
specialize IH with (l +ₗ 1). unfold heap_seq in IH.
rewrite -IH. do 2 f_equal.
apply Himap.
}
iApply "Heap".
iInduction (vl) as [|v vl] "IH" forall (l); [done|].
iSpecialize ("IH" $! (l +ₗ 1)).
unfold heap_seq. simpl. rewrite loc_add_0.
rewrite big_sepM_insert_delete.
iDestruct "List" as "[l↦ List]".
iSplitL "l↦"; [rewrite mapsto_unseal; iFrame|].
assert (delete l
(list_to_map
(imap ((λ (i : nat) (v0 : val), (l +ₗ i, v0)) ∘ S) vl)) =
list_to_map
(imap ((λ (i : nat) (v0 : val), (l +ₗ i, v0)) ∘ S) vl)) as ->.
{ apply delete_notin. apply not_elem_of_list_to_map_1.
rewrite fmap_imap. intro Hl.
apply elem_of_lookup_imap_1 in Hl.
destruct Hl as (i & v' & Hl & _).
destruct l. unfold loc_add in Hl. simpl in Hl.
inversion Hl. lia.
}
rewrite Himap. iApply "IH".
iApply (big_sepL_mono with "List").
iIntros (i v' Hi) "l↦".
rewrite loc_add_assoc. assert (1 + i = S i) as -> by lia.
iFrame.
Qed.
Lemma heap_free σ l vl (n : Z) :
n = length vl →
heap_ctx σ -∗ l ↦∗ vl -∗ †l…(length vl)
==∗ ⌜0 < n⌝ ∗ ⌜∀ m, is_Some (σ !! (l +ₗ m)) ↔ (0 ≤ m < n)⌝ ∗
heap_ctx (free_mem l (Z.to_nat n) σ).
Proof.
iDestruct 1 as (hF) "(Hvalσ & HhF & REL)"; iDestruct "REL" as %REL.
iIntros "Hmt Hf". rewrite heap_freeable_unseal /heap_freeable_def.
iCombine "HhF Hf" gives % [Hl Hv]%auth_both_valid_discrete.
move: Hl=> /singleton_included_l [[q qs] [/leibniz_equiv_iff Hl Hq]].
apply (Some_included_exclusive _ _) in Hq as [=<-<-]%leibniz_equiv; last first.
{ move: (Hv (l.1)). rewrite Hl. by intros [??]. }
assert (vl ≠ []).
{ intros ->. by destruct (REL (l.1) (1%Qp, ∅)) as [[] ?]. }
assert (0 < n) by (subst n; by destruct vl).
iMod (heap_free_vs with "[Hmt Hvalσ]") as "Hvalσ".
{ rewrite array_combine //. iFrame. }
iMod (own_update_2 with "HhF Hf") as "HhF".
{ apply auth_update_dealloc, (delete_singleton_local_update _ _ _). }
iModIntro; subst. repeat iSplit; eauto using heap_freeable_is_Some.
iExists _. subst. rewrite Nat2Z.id. iFrame.
eauto using heap_freeable_rel_free_mem.
Qed.
Lemma mapsto_lookup σ l q v :
ghost_map_auth heap_name 1 σ -∗ l ↦{q} v -∗
⌜σ !! l = Some v⌝.
Proof.
iIntros "Heap l↦".
rewrite mapsto_unseal.
iApply (ghost_map_lookup with "Heap l↦").
Qed.
Lemma mapsto_lookup_1 σ l v :
ghost_map_auth heap_name 1 σ -∗ l ↦ v -∗
⌜σ !! l = Some v⌝.
Proof.
apply mapsto_lookup.
Qed.
Lemma heap_read σ l q v :
heap_ctx σ -∗ l ↦{q} v -∗ ⌜σ !! l = Some v⌝.
Proof.
iDestruct 1 as (hF) "(Hσ & HhF & REL)". iIntros "Hmt".
iDestruct (mapsto_lookup with "Hσ Hmt") as %Hσl. done.
Qed.
Lemma heap_read_1 σ l v :
heap_ctx σ -∗ l ↦ v -∗ ⌜σ !! l = Some v⌝.
Proof.
iDestruct 1 as (hF) "(Hσ & HhF & REL)". iIntros "Hmt".
iDestruct (mapsto_lookup_1 with "Hσ Hmt") as %Hσl. done.
Qed.
Lemma heap_write_vs σ l v v':
σ !! l = Some v →
ghost_map_auth heap_name 1 σ -∗ l ↦ v
==∗ ghost_map_auth heap_name 1 (<[l:= v']> σ)
∗ l ↦ v'.
Proof.
iIntros (Hσv) "Heap l↦".
rewrite mapsto_unseal.
iDestruct ((ghost_map_update v') with "Heap") as "Heap".
iSpecialize ("Heap" with "l↦").
iFrame.
Qed.
Lemma heap_write σ l v v' :
heap_ctx σ -∗ l ↦ v ==∗ heap_ctx (<[l:=v']> σ) ∗ l ↦ v'.
Proof.
iDestruct 1 as (hF) "(Hσ & HhF & %)". iIntros "Hmt".
iDestruct (mapsto_lookup_1 with "Hσ Hmt") as %?; auto.
iMod (heap_write_vs with "Hσ Hmt") as "[Hσ $]"; first done.
iModIntro. iExists _. iFrame. eauto using heap_freeable_rel_stable.
Qed.
End heap.
#[export] Typeclasses Opaque array.
Global Program Instance heapGS_irisGS `{!heapGS_gen hlc Σ} : irisGS_gen hlc heap_lang Σ := {
iris_invGS := heapGS_invGS;
state_interp σ step_cnt κs _ :=
(heap_ctx σ.(heap) ∗ proph_map_interp κs σ.(used_proph_id) ∗ steps_auth step_cnt)%I;
fork_post _ := True%I;
num_laters_per_step n := n;
}.
Next Obligation.
iIntros (??? σ ns κs nt) "/= ($ & $ & H)".
by iMod (steps_auth_update_S with "H") as "$".
Qed.
(* our version of gen_inv_heap *)
Definition inv_heapN: namespace := nroot .@ "inv_heap".
Definition to_inv_heap (h: gmap loc (val * (val -d> PropO))) : inv_heap_mapUR :=
prod_map (λ x, Excl' x) to_agree <$> h.
Section inv_heap_definitions.
Context `{gG: !heapGS_gen hlc Σ}.
Definition inv_heap_inv_P : iProp Σ :=
∃ h : gmap loc (val * (val -d> PropO)),
own inv_heap_name (● to_inv_heap h) ∗
[∗ map] l ↦ p ∈ h, ⌜p.2 p.1⌝ ∗ l ↦ p.1.
Definition inv_heap_inv : iProp Σ := inv inv_heapN inv_heap_inv_P.
Definition inv_mapsto_own (l : loc) (v : val) (I : val → Prop) : iProp Σ :=
own inv_heap_name (◯ {[l := (Excl' v, to_agree I) ]}).
Definition inv_mapsto (l : loc) (I : val → Prop) : iProp Σ :=
own inv_heap_name (◯ {[l := (None, to_agree I)]}).
End inv_heap_definitions.
Global Instance: Params (@inv_mapsto_own) 4 := {}.
Global Instance: Params (@inv_mapsto) 3 := {}.
Notation "l '↦_' I □" := (inv_mapsto l I%stdpp%type)
(at level 20, I at level 9, format "l '↦_' I '□'") : bi_scope.
Notation "l ↦_ I v" := (inv_mapsto_own l v I%stdpp%type)
(at level 20, I at level 9, format "l ↦_ I v") : bi_scope.
Section to_inv_heap.
Implicit Types (h : gmap loc (val * (val -d> PropO))).
Lemma to_inv_heap_valid h : ✓ to_inv_heap h.
Proof. intros l. rewrite lookup_fmap. by case (h !! l). Qed.
Lemma to_inv_heap_singleton l v I :
to_inv_heap {[l := (v, I)]} =@{inv_heap_mapUR} {[l := (Excl' v, to_agree I)]}.
Proof. by rewrite /to_inv_heap fmap_insert fmap_empty. Qed.
Lemma to_inv_heap_insert l v I h :
to_inv_heap (<[l := (v, I)]> h) = <[l := (Excl' v, to_agree I)]> (to_inv_heap h).
Proof. by rewrite /to_inv_heap fmap_insert. Qed.
Lemma lookup_to_inv_heap_None h l :
h !! l = None → to_inv_heap h !! l = None.
Proof. by rewrite /to_inv_heap lookup_fmap=> ->. Qed.
Lemma lookup_to_inv_heap_Some h l v I :
h !! l = Some (v, I) → to_inv_heap h !! l = Some (Excl' v, to_agree I).
Proof. by rewrite /to_inv_heap lookup_fmap=> ->. Qed.
Lemma lookup_to_inv_heap_Some_2 h l v' I' :
to_inv_heap h !! l ≡ Some (v', I') →
∃ v I, v' = Excl' v ∧ I' ≡ to_agree I ∧ h !! l = Some (v, I).
Proof.
rewrite /to_inv_heap /prod_map lookup_fmap. rewrite fmap_Some_equiv.
intros ([] & Hsome & [Heqv HeqI]); simplify_eq/=; eauto.
Qed.
End to_inv_heap.
Section inv_heap.
Context `{!heapGS_gen hlc Σ}.
Implicit Types (l : loc) (v : val) (I : val → Prop).
Implicit Types (h : gmap loc (val * (val -d> PropO))).
(** * Helpers *)
Lemma inv_mapsto_lookup_Some l h I :
l ↦_I □ -∗ own inv_heap_name (● to_inv_heap h) -∗
⌜∃ v I', h !! l = Some (v, I') ∧ ∀ w, I w ↔ I' w ⌝.
Proof.
iIntros "Hl_inv H◯".
iCombine "H◯ Hl_inv" gives %[Hincl Hvalid]%auth_both_valid_discrete.
iPureIntro.
move: Hincl; rewrite singleton_included_l; intros ([v' I'] & Hsome & Hincl).
apply lookup_to_inv_heap_Some_2 in Hsome as (v'' & I'' & _ & HI & Hh).
move: Hincl; rewrite HI Some_included_total pair_included
to_agree_included; intros [??]; eauto.
Qed.
Lemma inv_mapsto_own_lookup_Some l v h I :
l ↦_I v -∗ own inv_heap_name (● to_inv_heap h) -∗
⌜ ∃ I', h !! l = Some (v, I') ∧ ∀ w, I w ↔ I' w ⌝.
Proof.
iIntros "Hl_inv H●".
iCombine "H● Hl_inv" gives %[Hincl Hvalid]%auth_both_valid_discrete.
iPureIntro.
move: Hincl; rewrite singleton_included_l; intros ([v' I'] & Hsome & Hincl).
apply lookup_to_inv_heap_Some_2 in Hsome as (v'' & I'' & -> & HI & Hh).
move: Hincl; rewrite HI Some_included_total pair_included
Excl_included to_agree_included; intros [-> ?]; eauto.
Qed.
(** * Typeclass instances *)
(* FIXME(Coq #6294): needs new unification
The uses of [apply:] and [move: ..; rewrite ..] (by lack of [apply: .. in ..])
in this file are needed because Coq's default unification algorithm fails. *)
Global Instance inv_mapsto_own_proper l v :
Proper (pointwise_relation _ iff ==> (≡)) (inv_mapsto_own l v).
Proof.
intros I1 I2 ?. rewrite /inv_mapsto_own. do 2 f_equiv.
apply: singletonM_proper. f_equiv. by apply: to_agree_proper.
Qed.
Global Instance inv_mapsto_proper l :
Proper (pointwise_relation _ iff ==> (≡)) (inv_mapsto l).
Proof.
intros I1 I2 ?. rewrite /inv_mapsto. do 2 f_equiv.
apply: singletonM_proper. f_equiv. by apply: to_agree_proper.
Qed.
Global Instance inv_heap_inv_persistent : Persistent (inv_heap_inv).
Proof. apply _. Qed.
Global Instance inv_mapsto_persistent l I : Persistent (l ↦_I □).
Proof. apply _. Qed.
Global Instance inv_mapsto_timeless l I : Timeless (l ↦_I □).
Proof. apply _. Qed.
Global Instance inv_mapsto_own_timeless l v I : Timeless (l ↦_I v).
Proof. apply _. Qed.
(** * Public lemmas *)
Lemma make_inv_mapsto l v I E :
↑inv_heapN ⊆ E →
I v →
inv_heap_inv -∗ l ↦ v ={E}=∗ l ↦_I v.
Proof.
iIntros (HN HI) "#Hinv Hl".
iMod (inv_acc_timeless _ inv_heapN with "Hinv") as "[HP Hclose]"; first done.
iDestruct "HP" as (h) "[H● HsepM]".
destruct (h !! l) as [v'| ] eqn: Hlookup.
- (* auth map contains l --> contradiction *)
iDestruct (big_sepM_lookup with "HsepM") as "[_ Hl']"; first done.
by iDestruct (mapsto_valid_2 with "Hl Hl'") as %[??].
- iMod (own_update with "H●") as "[H● H◯]".
{ apply lookup_to_inv_heap_None in Hlookup.
apply (auth_update_alloc _
(to_inv_heap (<[l:=(v,I)]> h)) (to_inv_heap ({[l:=(v,I)]}))).
rewrite to_inv_heap_insert to_inv_heap_singleton.
by apply: alloc_singleton_local_update. }
iMod ("Hclose" with "[H● HsepM Hl]").
+ iExists _.
iDestruct (big_sepM_insert _ _ _ (_,_) with "[$HsepM $Hl]")
as "HsepM"; auto with iFrame.
+ iModIntro. by rewrite /inv_mapsto_own to_inv_heap_singleton.
Qed.
Lemma inv_mapsto_own_inv l v I : l ↦_I v -∗ l ↦_I □.
Proof.
iApply own_mono; apply auth_frag_mono. rewrite singleton_included pair_included.
right. split; [apply: ucmra_unit_least|done].
Qed.
(** An accessor to make use of [inv_mapsto_own].
This opens the invariant *before* consuming [inv_mapsto_own] so that you can use
this before opening an atomic update that provides [inv_mapsto_own]!. *)
Lemma inv_mapsto_own_acc_strong' E :
↑inv_heapN ⊆ E →
inv_heap_inv ={E, E ∖ ↑inv_heapN}=∗ ∀ l v I, l ↦_I v -∗
(⌜I v⌝ ∗ l ↦ v ∗ (∀ w, ⌜I w ⌝ -∗ l ↦ w ==∗
inv_mapsto_own l w I ∗ |={E ∖ ↑inv_heapN, E}=> True)).
Proof.
iIntros (HN) "#Hinv".
iMod (inv_acc_timeless _ inv_heapN _ with "Hinv") as "[HP Hclose]"; first done.
iIntros "!>" (l v I) "Hl_inv".
iDestruct "HP" as (h) "[H● HsepM]".
iDestruct (inv_mapsto_own_lookup_Some with "Hl_inv H●") as %(I'&?&HI').
setoid_rewrite HI'.
iDestruct (big_sepM_delete with "HsepM") as "[[HI Hl] HsepM]"; first done.
iIntros "{$HI $Hl}" (w ?) "Hl".
iMod (own_update_2 with "H● Hl_inv") as "[H● H◯]".
{ apply (auth_update _ _ (<[l := (Excl' w, to_agree I')]> (to_inv_heap h))
{[l := (Excl' w, to_agree I)]}).
apply: singleton_local_update.
{ by apply lookup_to_inv_heap_Some. }
apply: prod_local_update_1. apply: option_local_update.
apply: exclusive_local_update. done. }
iDestruct (big_sepM_insert _ _ _ (w, I') with "[$HsepM $Hl //]") as "HsepM".
{ apply lookup_delete. }
rewrite insert_delete_insert -to_inv_heap_insert. iIntros "!> {$H◯}".
iApply ("Hclose" with "[H● HsepM]"). iExists _; by iFrame.
Qed.
(** Derive a more standard accessor. *)
Lemma inv_mapsto_own_acc' E l v I:
↑inv_heapN ⊆ E →
inv_heap_inv -∗ l ↦_I v ={E, E ∖ ↑inv_heapN}=∗
(⌜I v⌝ ∗ l ↦ v ∗ (∀ w, ⌜I w ⌝ -∗ l ↦ w ={E ∖ ↑inv_heapN, E}=∗ l ↦_I w)).
Proof.
iIntros (?) "#Hinv Hl".
iMod (inv_mapsto_own_acc_strong' with "Hinv") as "Hacc"; first done.
iDestruct ("Hacc" with "Hl") as "(HI & Hl & Hclose)".
iIntros "!> {$HI $Hl}" (w) "HI Hl".
iMod ("Hclose" with "HI Hl") as "[$ $]".
Qed.
Lemma inv_mapsto_acc' l I E :
↑inv_heapN ⊆ E →
inv_heap_inv -∗ l ↦_I □ ={E, E ∖ ↑inv_heapN}=∗
∃ v, ⌜I v⌝ ∗ l ↦ v ∗ (l ↦ v ={E ∖ ↑inv_heapN, E}=∗ ⌜True⌝).
Proof.
iIntros (HN) "#Hinv Hl_inv".
iMod (inv_acc_timeless _ inv_heapN _ with "Hinv") as "[HP Hclose]"; first done.
iModIntro.
iDestruct "HP" as (h) "[H● HsepM]".
iDestruct (inv_mapsto_lookup_Some with "Hl_inv H●") as %(v&I'&?&HI').
iDestruct (big_sepM_lookup_acc with "HsepM") as "[[#HI Hl] HsepM]"; first done.
setoid_rewrite HI'.
iExists _. iIntros "{$HI $Hl} Hl".
iMod ("Hclose" with "[H● HsepM Hl]"); last done.
iExists _. iFrame "H●". iApply ("HsepM" with "[$Hl //]").
Qed.
End inv_heap.
#[export] Typeclasses Opaque inv_heap_inv inv_mapsto inv_mapsto_own.
Section lifting.
Context `{!heapGS_gen hlc Σ}.
Implicit Types P Q : iProp Σ.
Implicit Types Φ Ψ : val → iProp Σ.
Implicit Types efs : list expr.
Implicit Types σ : state.
Implicit Types v : val.
Implicit Types l : loc.
Lemma wp_lb_init s E e Φ :
TCEq (to_val e) None →
(steps_lb 0 -∗ WP e @ s; E {{ v, Φ v }}) -∗
WP e @ s; E {{ Φ }}.
Proof.
(** TODO: We should try to use a generic lifting lemma (and avoid [wp_unfold])
here, since this breaks the WP abstraction. *)
rewrite !wp_unfold /wp_pre /=. iIntros (->) "Hwp".
iIntros (σ1 ns κ κs m) "(Hσ & Hκ & Hsteps)".
iDestruct (steps_lb_get with "Hsteps") as "#Hlb".
iDestruct (steps_lb_le _ 0 with "Hlb") as "Hlb0"; [lia|].
iSpecialize ("Hwp" with "Hlb0"). iApply ("Hwp" $! σ1 ns κ κs m). iFrame "∗#%".
Qed.
Lemma wp_lb_update s n E e Φ :
TCEq (to_val e) None →
steps_lb n -∗
WP e @ s; E {{ v, steps_lb (S n) -∗ Φ v }} -∗
WP e @ s; E {{ Φ }}.
Proof.
(** TODO: We should try to use a generic lifting lemma (and avoid [wp_unfold])
here, since this breaks the WP abstraction. *)
rewrite !wp_unfold /wp_pre /=. iIntros (->) "Hlb Hwp".
iIntros (σ1 ns κ κs m) "(Hσ & Hκ & Hsteps)".
iDestruct (steps_lb_valid with "Hsteps Hlb") as %?.
iMod ("Hwp" $! σ1 ns κ κs m with "[$Hσ $Hκ $Hsteps]") as "[%Hs Hwp]".
iModIntro. iSplit; [done|].
iIntros (e2 σ2 efs Hstep) "Hcred".
iMod ("Hwp" with "[//] Hcred") as "Hwp".
iIntros "!> !>". iMod "Hwp" as "Hwp". iIntros "!>".
iApply (step_fupdN_wand with "Hwp").
iIntros "Hwp". iMod "Hwp" as "(H & Hwp & $)".
iDestruct "H" as "($ & $ & Hsteps)".
iDestruct (steps_lb_get with "Hsteps") as "#HlbS".
iDestruct (steps_lb_le _ (S n) with "HlbS") as "#HlbS'"; [lia|].
iModIntro. iFrame "Hsteps".
iApply (wp_wand with "Hwp"). iIntros (v) "HΦ". by iApply "HΦ".
Qed.
Lemma wp_step_fupdN_lb s n E1 E2 e P Φ :
TCEq (to_val e) None →
E2 ⊆ E1 →
steps_lb n -∗
(|={E1∖E2,∅}=> |={∅}▷=>^(S n) |={∅,E1∖E2}=> P) -∗
WP e @ s; E2 {{ v, P ={E1}=∗ Φ v }} -∗
WP e @ s; E1 {{ Φ }}.
Proof.
iIntros (He HE) "Hlb HP Hwp".
iApply wp_step_fupdN; [done|].
iSplit; [|by iFrame].
iIntros (σ ns κs nt) "(? & ? & Hsteps)".
iDestruct (steps_lb_valid with "Hsteps Hlb") as %Hle.
iApply fupd_mask_intro; [set_solver|].
iIntros "_". iPureIntro. rewrite /num_laters_per_step /=. lia.
Qed.
(** Recursive functions: we do not use this lemmas as it is easier to use Löb
induction directly, but this demonstrates that we can state the expected
reasoning principle for recursive functions, without any visible ▷. *)
Lemma wp_rec_löb s E f x e Φ Ψ :
□ ( □ (∀ v, Ψ v -∗ WP (rec: f x := e)%V v @ s; E {{ Φ }}) -∗
∀ v, Ψ v -∗ WP (subst' x v (subst' f (rec: f x := e) e)) @ s; E {{ Φ }}) -∗
∀ v, Ψ v -∗ WP (rec: f x := e)%V v @ s; E {{ Φ }}.
Proof.
iIntros "#Hrec". iLöb as "IH". iIntros (v) "HΨ".
iApply lifting.wp_pure_step_later; first done.
iIntros "!> _". iApply ("Hrec" with "[] HΨ"). iIntros "!>" (w) "HΨ".
iApply ("IH" with "HΨ").
Qed.
(** Fork: Not using Texan triples to avoid some unnecessary [True] *)
Lemma wp_fork s E e Φ :
▷ WP e @ s; ⊤ {{ _, True }} -∗ ▷ Φ (LitV LitUnit) -∗ WP Fork e @ s; E {{ Φ }}.
Proof.
iIntros "He HΦ". iApply wp_lift_atomic_head_step; [done|].
iIntros (σ1 ns κ κs nt) "(? & ? & Hsteps)".
iModIntro. iSplit; first by eauto with head_step.
iIntros "!>" (v2 σ2 efs Hstep) "_"; inv_head_step.
iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
iFrame. by iFrame "∗#%".
Qed.
Lemma twp_fork s E e Φ :
WP e @ s; ⊤ [{ _, True }] -∗ Φ (LitV LitUnit) -∗ WP Fork e @ s; E [{ Φ }].
Proof.
iIntros "He HΦ". iApply twp_lift_atomic_head_step; [done|].
iIntros (σ1 ns κs nt) "(? & ? & Hsteps)".
iModIntro. iSplit; first by eauto with head_step.
iIntros (κ v2 σ2 efs Hstep); inv_head_step.
iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
iFrame. done.
Qed.
(** Heap *)
Lemma inv_mapsto_own_acc_strong E :
↑inv_heapN ⊆ E →
inv_heap_inv ={E, E ∖ ↑inv_heapN}=∗ ∀ l v I, l ↦_I v -∗
(⌜I v⌝ ∗ l ↦ v ∗ (∀ w, ⌜I w ⌝ -∗ l ↦ w ==∗
inv_mapsto_own l w I ∗ |={E ∖ ↑inv_heapN, E}=> True)).
Proof.
iIntros (?) "#Hinv".
iMod (inv_mapsto_own_acc_strong' with "Hinv") as "Hacc"; first done.
iIntros "!>" (l v I) "Hl". iDestruct ("Hacc" with "Hl") as "(% & Hl & Hclose)".
iFrame "%∗".
Qed.
Lemma inv_mapsto_own_acc E l v I:
↑inv_heapN ⊆ E →
inv_heap_inv -∗ l ↦_I v ={E, E ∖ ↑inv_heapN}=∗
(⌜I v⌝ ∗ l ↦ v ∗ (∀ w, ⌜I w ⌝ -∗ l ↦ w ={E ∖ ↑inv_heapN, E}=∗ l ↦_I w)).
Proof.
iIntros (?) "#Hinv Hl".
iMod (inv_mapsto_own_acc' with "Hinv Hl") as "(% & Hl & Hclose)"; first done.
iFrame "%∗". done.
Qed.
Lemma inv_mapsto_acc l I E :
↑inv_heapN ⊆ E →
inv_heap_inv -∗ l ↦_I □ ={E, E ∖ ↑inv_heapN}=∗
∃ v, ⌜I v⌝ ∗ l ↦ v ∗ (l ↦ v ={E ∖ ↑inv_heapN, E}=∗ ⌜True⌝).
Proof.
iIntros (?) "#Hinv Hl".
iMod (inv_mapsto_acc' with "Hinv Hl") as (v) "(% & Hl & Hclose)"; [done|].
iIntros "!>". iExists (v). iFrame "%∗".
Qed.
(** Useful rules for heap freeable predicates *)
Lemma twp_allocN s E v n :
(0 < n)%Z →
[[{ True }]] AllocN (Val $ LitV $ LitInt $ FinInt $ n) (Val v) @ s; E
[[{ (l : blk), RET LitV (LitLoc l); †l…(Z.to_nat n) ∗ l ↦∗ replicate (Z.to_nat n) v }]].
Proof.
iIntros (Hn Φ) "_ HΦ". iApply twp_lift_atomic_head_step_no_fork; first done.
iIntros (σ1 ns κs nt) "(Hσ & Hκs & Hsteps)". iModIntro.
iSplit; first by destruct n; auto with lia head_step.
iIntros (κ v2 σ2 efs Hstep); inv_head_step.
iMod (heap_alloc with "Hσ") as "[Hσ Hl]"; [try done..|].
iMod (steps_auth_update_S with "Hsteps") as "Hsteps".
iModIntro. do 2 (iSplit; first done). iFrame "∗#%". iApply ("HΦ" with "Hl").
Qed.
Lemma wp_allocN s E v n :
(0 < n)%Z →
{{{ True }}} AllocN (Val $ LitV $ LitInt $ FinInt $ n) (Val v) @ s; E
{{{ (l : blk), RET LitV (LitLoc l); †l…(Z.to_nat n) ∗ l ↦∗ replicate (Z.to_nat n) v }}}.
Proof.
iIntros (Hn Φ) "_ HΦ". iApply (twp_wp_step with "HΦ").
iApply twp_allocN; [by auto..|]; iIntros (l) "H HΦ". by iApply "HΦ".
Qed.
Lemma twp_alloc s E v :
[[{ True }]] Alloc (Val v) @ s; E [[{ (l : blk), RET LitV (LitLoc l); l ↦ v ∗ †l…1}]].
Proof.
iIntros (Φ) "_ HΦ". iApply twp_allocN; [auto with lia..|].
iIntros (l) "/= [†l ?]". rewrite array_singleton. iApply "HΦ"; iFrame.
Qed.
Lemma wp_alloc s E v :
{{{ True }}} Alloc (Val v) @ s; E {{{ (l : blk), RET LitV (LitLoc l); l ↦ v ∗ †l…1 }}}.
Proof.
iIntros (Φ) "_ HΦ". iApply (twp_wp_step with "HΦ").
iApply twp_alloc; [by auto..|]; iIntros (l) "H HΦ". by iApply "HΦ".
Qed.
Lemma twp_free s E (n:Z) l vl :
n = length vl →
[[{ l ↦∗ vl ∗ †l…(length vl) }]]
Free (Val $ LitV $ LitInt $ FinInt n) (Val $ LitV $ LitLoc l) @ s; E
[[{ RET LitV LitUnit; True }]].
Proof.
iIntros (? Φ) "[Hl †l] HΦ". iApply twp_lift_atomic_head_step_no_fork; first done.
iIntros (σ1 ns κs nt) "(Hσ & Hκs & Hsteps)".
iMod (heap_free _ _ _ n with "Hσ Hl †l") as "(% & % & Hσ)"=>//.