-
Notifications
You must be signed in to change notification settings - Fork 1
/
weakestpre_at.v
1871 lines (1695 loc) · 70.1 KB
/
weakestpre_at.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
(* Weakest precondition rules for atomic locations. *)
From stdpp Require Import gmap.
From iris.program_logic Require weakestpre.
From stdpp Require Import countable numbers gmap.
From iris Require Import invariants.
From iris.proofmode Require Import tactics monpred.
From iris.algebra Require Import gmap gset excl auth.
From iris.program_logic Require weakestpre.
From iris.heap_lang Require Import locations.
From iris_named_props Require Import named_props.
From self.algebra Require Export ghost_map ghost_map_map.
From self Require Export extra ipm_tactics encode_relation view view_slice.
From self.lang Require Export lang lemmas tactics syntax.
From self.base Require Import primitive_laws.
From self Require Import solve_view_le.
From self.high Require Export dprop resources crash_weakestpre weakestpre
lifted_modalities monpred_simpl modalities protocol locations.
From self.high Require Import locations protocol.
From self.high.modalities Require Import fence no_buffer.
Section wp_at_rules.
Context `{AbstractState ST}.
Context `{!nvmG Σ}.
Implicit Types (ℓ : loc) (s : ST) (prot : LocationProtocol ST).
(** * Shared points-to predicate *)
Lemma msg_persisted_views_eq
(ℓ : loc) (hists : gmap loc (gmap time (message * positive)))
(hist : gmap time (message * positive)) (msg : message)
(atLocs : gset loc) (t : time) (s' : positive) γ :
map_Forall
(λ _ : loc,
map_Forall
(λ _ '(msg, _), msg_persist_view msg = msg_persisted_after_view msg))
(restrict atLocs hists) →
hists !! ℓ = Some hist →
hist !! t = Some (msg, s') →
own γ (● (atLocs : gsetUR loc)) -∗
own γ (◯ {[ℓ]}) -∗
⌜msg.(msg_persist_view) = msg.(msg_persisted_after_view)⌝.
Proof.
iIntros (m look look') "A B".
iDestruct (location_sets_singleton_included with "A B") as %V.
iPureIntro.
assert (restrict atLocs hists !! ℓ = Some hist) as look2.
- apply restrict_lookup_Some. done.
- setoid_rewrite map_Forall_lookup in m.
specialize (m ℓ hist look2).
setoid_rewrite map_Forall_lookup in m.
specialize (m t (msg, s') look').
simpl in m.
done.
Qed.
Lemma interp_insert_loc_at ℓ prot `{!ProtocolConditions prot} s SV PV BV nG v :
SV !!0 ℓ = 0 →
interp -∗
p_inv prot s v (SV, PV, BV, nG) -∗
ℓ ↦h initial_history AT SV PV v ==∗
(ℓ ↦_AT^{prot} [s]) (SV, PV, BV, nG) ∗ interp.
Proof.
iIntros (svLook).
iNamed 1.
iIntros "pred pts".
iDestruct (big_sepM2_dom with "oldViewsDiscarded") as %offsetsDom.
(* The new location is not in the existing [phys_hist]. *)
destruct (phys_hists !! ℓ) eqn:physHistsLook.
{ assert (is_Some (offsets !! ℓ)) as (? & ?).
{ apply elem_of_dom. rewrite -offsetsDom. apply elem_of_dom. done. }
iDestruct (big_sepM_lookup with "ptsMap") as "pts'".
{ apply map_lookup_zip_with_Some. naive_solver. }
iDestruct (mapsto_valid_2 with "pts pts'") as (?) "_".
done. }
iDestruct (big_sepM2_dom with "predsHold") as %domEq.
iDestruct (big_sepM2_dom with "bumperSome") as %domEq2.
iDestruct (big_sepM2_dom with "predPostCrash") as %domEq3.
iDestruct (big_sepM2_dom with "bumpMono") as %domEq4.
assert (offsets !! ℓ = None) as offsetsLook.
{ apply not_elem_of_dom. rewrite -offsetsDom. apply not_elem_of_dom. done. }
assert (abs_hists !! ℓ = None) as absHistsLook.
{ apply not_elem_of_dom. rewrite -domEq. apply not_elem_of_dom.
assumption. }
assert (ℓ ∉ dom abs_hists) as absHistsDomElem.
{ apply not_elem_of_dom. done. }
(* We update ghost state. *)
(* Update ghost state for physical history. *)
iMod (auth_map_map_insert_top with "physHist") as "[physHist #physHistFrag]".
{ done. }
(* Add the predicate to the ghost state of predicates. *)
iMod (own_all_preds_insert with "predicates") as "[predicates knowPred]".
{ eapply map_dom_eq_lookup_None; last apply physHistsLook.
rewrite domEq3. congruence. }
(* Add a new offset to the ghost state of offfsets. *)
iMod (ghost_map_insert_persist _ 0 with "offsets") as "[offsets #offset]".
{ eapply map_dom_eq_lookup_None; last apply physHistsLook. congruence. }
(* Allocate the abstract history for the location. *)
iMod (full_map_insert _ _ _ {[0 := encode s]} with "history")
as "(history & ownHist & #fragHist)".
{ eapply map_dom_eq_lookup_None; last apply physHistsLook. congruence. }
iEval (rewrite big_sepM_singleton) in "fragHist".
(* Add the bumper to the ghost state of bumper. *)
iMod (own_all_bumpers_insert _ _ _ (prot.(p_bumper)) with "allBumpers")
as "[allBumpers knowBumper]".
{ eapply map_dom_eq_lookup_None; last apply physHistsLook. congruence. }
(* Add the preorder to the ghost state of bumper. *)
iMod (ghost_map_insert_persist with "allOrders") as "[allOrders #knowOrder]".
{ eapply map_dom_eq_lookup_None; last apply physHistsLook.
rewrite /relation2. congruence. }
(* Add the allocated location to the set of atomic locations. *)
iMod (own_update with "atLocs") as "[atLocs fragSharedLocs]".
{ apply auth_update_alloc. apply gset_local_update.
apply (union_subseteq_r {[ ℓ ]}). }
iEval (rewrite -gset_op) in "fragSharedLocs". iDestruct "fragSharedLocs" as "[#isShared _]".
iAssert (know_protocol ℓ prot (SV, PV, BV, nG)) as "#prot".
{ rewrite /know_protocol.
monPred_simpl.
simpl.
rewrite !monPred_at_embed.
iFrame "knowPred knowBumper knowOrder". }
iModIntro.
iSplitL "knowPred knowBumper".
{ rewrite /mapsto_at.
iExists _, {[ 0 := (Msg v SV PV PV) ]}, 0, 0, 0, s, _.
iSplitPure; first done.
iSplitPure; first apply map_sequence_singleton.
iSplitPure; first apply map_sequence_singleton.
iSplitPure. { apply map_no_later_singleton. }
iSplitPure. { set_solver+. }
simpl. rewrite monPred_at_embed.
iFrame "prot offset isShared".
iSplitPure. { apply increasing_map_singleton. }
iEval (rewrite 2!big_sepM_singleton).
simpl.
rewrite monPred_at_sep.
simpl.
rewrite !monPred_at_embed.
iDestruct (frag_history_equiv with "fragHist") as "$".
iFrame "physHistFrag".
iPureIntro. solve_view_le. }
simpl.
repeat iExists _.
iSplitL "ptsMap pts".
{ erewrite <- map_insert_zip_with. erewrite big_sepM_insert.
- iFrame "ptsMap". erewrite view_slice.drop_prefix_zero. iFrame "pts".
- apply map_lookup_zip_with_None. naive_solver. }
iFrameF "offsets". iFrameF "physHist".
iFrame "crashedAt history predicates allOrders naLocs atLocs".
(* oldViewsDiscarded *)
iSplit.
{ iApply big_sepM2_insert; [done|done|].
iFrame "oldViewsDiscarded".
iPureIntro. rewrite /initial_history.
intros ??? [<- ?]%lookup_singleton_Some. lia. }
(* historyFragments *)
iSplit.
{ iApply (big_sepM_insert_2 with "[] historyFragments");
simpl; rewrite big_sepM_singleton; iFrame "fragHist". }
(* locsDisjoint *)
iSplitPure. {
assert (ℓ ∉ dom abs_hists).
{ rewrite -domEq. apply not_elem_of_dom. done. }
set_solver. }
(* histDomLocs *)
iSplitPure. { rewrite dom_insert_L. set_solver+ histDomLocs. }
(* naViewsDom *)
iSplitPure; first done.
iFrame "naView".
(* mapShared *)
iSplitPure.
{ rewrite -map_insert_zip_with.
rewrite restrict_insert_union.
rewrite /shared_locs_inv.
rewrite /map_map_Forall.
apply map_Forall_insert_2; last done.
rewrite /initial_history.
simpl.
rewrite drop_prefix_zero.
rewrite map_Forall_singleton.
done. }
iSplitL "atLocsHistories ownHist".
{ rewrite restrict_insert_union big_sepM_insert.
2: { apply restrict_lookup_None_lookup. assumption. }
iFrame "ownHist atLocsHistories". }
(* "ordered" *)
iSplit.
{ iApply (big_sepM2_insert_2); last done.
iPureIntro. apply increasing_map_singleton. }
(* predsHold *)
iSplitL "predsHold pred".
{ iApply (big_sepM2_insert_2 with "[pred] [predsHold]").
- iExists _. rewrite lookup_insert.
iSplit; first done.
rewrite /initial_history.
simpl.
rewrite big_sepM2_singleton /=.
assert (na_views !! ℓ = None) as ->.
{ apply not_elem_of_dom in physHistsLook.
apply not_elem_of_dom.
rewrite naViewsDom.
eapply not_elem_of_weaken; first apply physHistsLook.
rewrite domEq histDomLocs.
set_solver. }
simpl.
iDestruct (predicate_holds_phi with "[]") as "HH".
{ done. }
{ done. }
iApply "HH".
(* destruct TV as [[??]?]. *)
iApply (into_no_buffer_at with "pred").
- iApply (big_sepM2_impl with "predsHold").
iModIntro. iIntros (ℓ' ????) "(%pred & %look & ?)".
iExists (pred).
assert (ℓ ≠ ℓ') by congruence.
rewrite lookup_insert_ne; last done.
iSplit; first done.
iFrame. }
iFrame "allBumpers".
(* bumpMono *)
iSplit.
{ iApply (big_sepM2_insert_2 with "[] bumpMono").
iPureIntro. simpl.
apply encode_bumper_bump_mono. apply bumper_mono. }
(* predPostCrash *)
iSplit.
{ iApply (big_sepM2_insert_2 with "[] predPostCrash").
iModIntro. iIntros (??????) "(%eq & eq2 & P)".
iEval (rewrite /encode_predicate).
apply encode_bumper_Some_decode in eq.
destruct eq as (s3 & eq' & eq2').
rewrite -eq2'.
rewrite decode_encode.
iExists _.
iSplit. { iPureIntro. simpl. reflexivity. }
iDestruct (encode_predicate_extract with "eq2 P") as "pred".
{ done. }
iApply (pred_condition with "pred"). }
(* bumperBumpToValid *)
iSplitPure.
{ rewrite map_Forall_insert.
2: { eapply map_dom_eq_lookup_None; last apply physHistsLook. congruence. }
split; last done.
apply encode_bumper_bump_to_valid. }
iApply (big_sepM2_insert_2 with "[] bumperSome").
iPureIntro.
apply map_Forall_singleton.
rewrite encode_bumper_encode.
done.
Qed.
Lemma wp_alloc_at v s prot `{!ProtocolConditions prot} st E :
{{{ prot.(p_inv) s v }}}
ref_AT v @ st; E
{{{ ℓ, RET #ℓ; ℓ ↦_AT^{prot} [s] }}}.
Proof.
intros Φ.
iModel.
iIntros "phi".
iIntros ([TV' ?] [incl [= <-]]) "Φpost".
iApply wp_unfold_at.
iIntros ([[SV PV] BV] incl2) "#val".
iApply wp_extra_state_interp. { done. } { by apply prim_step_ref_no_fork. }
iIntros "interp".
(* We add this to prevent Coq from trying to use [highExtraStateInterp]. *)
set (extra := (Build_extraStateInterp _ _)).
iApply wp_fupd.
iApply (wp_alloc (extra := {| extra_state_interp := True |})); first done.
iNext.
iIntros (ℓ CV') "(crashedAt' & % & % & pts)".
simpl.
iFrame "val".
destruct TV as [[??]?].
iMod (interp_insert_loc_at ℓ prot with "interp [phi] pts")
as "(pts & interp)"; first done.
{ iApply monPred_mono; last iApply "phi". split; last done. etrans; done. }
iModIntro.
rewrite -assoc. iSplit; first done.
iFrame "interp".
iSpecialize ("Φpost" $! ℓ).
monPred_simpl.
iApply ("Φpost" with "[] pts").
{ done. }
Qed.
Definition encoded_predicate_hold nG physHist (absHist : gmap nat positive) pred : iProp Σ :=
([∗ map] msg;encS ∈ physHist;absHist,
encoded_predicate_holds pred encS
(msg_val msg)
(msg_store_view msg,
msg_persisted_after_view msg, ∅, nG)).
Definition loc_info nG ℓ prot (pred : enc_predicateO) physHists physHist absHist offset : iProp Σ :=
"physHists" ∷ auth_map_map_auth phys_history_name physHists ∗
"%physHistsLook" ∷ ⌜ physHists !! ℓ = Some physHist ⌝ ∗
"%domEq" ∷ ⌜ dom physHist = dom absHist ⌝ ∗
"%increasing" ∷ ⌜ increasing_map (encode_relation (⊑@{ST})) absHist ⌝ ∗
"%atInvs" ∷
⌜ map_Forall (λ t msg, atomic_loc_inv ℓ t msg) (drop_prefix physHist offset) ⌝ ∗
"#predEquiv" ∷ ▷ (pred ≡ encode_predicate (p_inv prot)) ∗
"#frags" ∷ ([∗ map] k2 ↦ v ∈ absHist, frag_entry abs_history_name ℓ k2 v) ∗
"predHolds" ∷ encoded_predicate_hold nG physHist absHist pred ∗
"fullHist" ∷ know_full_encoded_history_loc ℓ 1 absHist ∗
"pts" ∷ ℓ ↦h drop_prefix physHist offset.
Definition insert_impl nG ℓ prot pred physHists physHist absHist offset : iProp Σ :=
∀ t s es msg,
⌜ offset ≤ t ⌝ -∗
⌜ encode s = es ⌝ -∗
⌜ physHist !! t = None ⌝ -∗
⌜ msg_store_view msg !!0 ℓ = t - offset ⌝ -∗
⌜ msg_persist_view msg = msg_persisted_after_view msg ⌝ -∗
auth_map_map_auth phys_history_name physHists -∗
encoded_predicate_hold nG (<[ t := msg ]>physHist) (<[ t := es ]>absHist) pred -∗
know_full_encoded_history_loc ℓ 1 absHist -∗
⌜ increasing_map (encode_relation (⊑@{ST})) (<[ t := es ]>absHist) ⌝ -∗
ℓ ↦h (drop_prefix (<[t := msg ]> physHist) offset) ==∗
know_frag_history_loc ℓ t s ∗
auth_map_map_frag_singleton phys_history_name ℓ t msg ∗
interp.
Definition lookup_impl nG ℓ prot pred physHists physHist absHist offset : iProp Σ :=
auth_map_map_auth phys_history_name physHists -∗
encoded_predicate_hold nG physHist absHist pred -∗
know_full_encoded_history_loc ℓ 1 absHist -∗
ℓ ↦h drop_prefix physHist offset -∗
interp.
(* Get all information inside [interp] related to the location [ℓ]. *)
Lemma interp_get_at_loc nG ℓ prot offset TV :
interp -∗
own (get_at_locs_name nG) (◯ {[ ℓ ]}) -∗
know_protocol ℓ prot (TV, nG) -∗
ℓ ↪[ offset_name ]□ offset -∗
∃ physHists physHist (absHist : gmap nat positive) pred,
loc_info nG ℓ prot pred physHists physHist absHist offset ∗
(insert_impl nG ℓ prot pred physHists physHist absHist offset ∧
lookup_impl nG ℓ prot pred physHists physHist absHist offset).
Proof.
iNamed 1.
iIntros "isAt".
rewrite /know_protocol.
iDestruct 1 as "(#knowPred & #knowPreorder & #knowBumper)".
rewrite /know_pred_d /know_preorder_loc_d /know_bumper.
rewrite !lift_d_at.
iIntros "offset".
iDestruct (own_all_preds_pred with "predicates knowPred")
as (pred predsLook) "#predsEquiv".
(* iDestruct (full_map_frag_singleton_agreee with "history hist") *)
(* as %(absHist & enc & absHistLook & lookTS & decodeEnc). *)
iDestruct (big_sepM2_dom with "oldViewsDiscarded") as %offsetsDom.
iDestruct (ghost_map_lookup with "offsets offset") as %?.
iDestruct (location_sets_singleton_included with "atLocs isAt") as %ℓSh.
iDestruct (big_sepM2_dom with "predsHold") as %domPhysHistEqAbsHist.
assert (is_Some (abs_hists !! ℓ)) as [absHist absHistLook].
{ rewrite -elem_of_dom. set_solver. }
assert (is_Some (phys_hists !! ℓ)) as [physHist physHistsLook].
{ rewrite -elem_of_dom domPhysHistEqAbsHist elem_of_dom. done. }
iDestruct (big_sepM2_delete with "predsHold") as "[predHolds allPredsHold]";
[done|done|].
iDestruct "predHolds" as (pred' predsLook') "predHolds".
assert (pred = pred') as <-. { apply (inj Some). rewrite -predsLook. done. }
clear predsLook'.
assert (na_views !! ℓ = None) as naViewLook. { apply not_elem_of_dom. set_solver. }
iEval (rewrite naViewLook; simpl) in "predHolds".
iDestruct (big_sepM2_delete_l with "ordered")
as (order) "(%ordersLook & %increasingMap & #ordered2)";
first apply absHistLook.
iDestruct (orders_lookup with "allOrders knowPreorder") as %orderEq;
first apply ordersLook.
rewrite orderEq in increasingMap.
iDestruct (big_sepM2_dom with "predHolds") as %domEq.
(* We can now get the points-to predicate and execute the load. *)
iDestruct (big_sepM_delete with "ptsMap") as "[pts ptsMap]".
{ apply map_lookup_zip_with_Some. naive_solver. }
eassert _ as invs.
{ eapply map_Forall_lookup_1; first apply mapShared.
apply restrict_lookup_Some_2; last done.
apply map_lookup_zip_with_Some.
eexists _, _. split_and!; done. }
simpl in invs.
iDestruct (big_sepM_delete with "atLocsHistories") as
"(fullHist & atLocsHistories)".
{ apply restrict_lookup_Some_2; done. }
iDestruct (big_sepM_lookup with "historyFragments") as "#histFrags"; first done.
iExists phys_hists, physHist, absHist, pred.
(* Give resources. *)
iFrame (domEq increasingMap). iFrame (invs).
iFrame "predsEquiv".
iFrame "predHolds".
iFrame "histFrags".
iFrame "pts".
iFrame "fullHist".
iFrame "physHist".
iSplitPure; first apply physHistsLook.
(* We show the two different implications. *)
iSplit.
{ (* Get back resources. *)
iIntros (t_i s2 es msg ? encEs lookNone viewLook ?)
"physHists predHolds fullHist order pts".
assert (absHist !! t_i = None)%I as absHistLookNone.
{ apply not_elem_of_dom. rewrite -domEq. apply not_elem_of_dom. done. }
iMod (full_map_full_entry_insert _ _ _ _ es with "history fullHist")
as "(history & fullHist & #histFrag)"; first done.
iDestruct (big_sepM_insert_delete with "[$atLocsHistories $fullHist]")
as "atLocsHistories".
iDestruct (big_sepM_insert_delete with "[$ptsMap $pts]") as "ptsMap".
iEval (rewrite map_insert_zip_with) in "ptsMap".
iEval (rewrite (insert_id offsets); last done) in "ptsMap".
iDestruct (big_sepM2_insert_delete with "[$allPredsHold predHolds]")
as "allPredsHold".
{ iExists pred. rewrite naViewLook. iFrame "predHolds". done. }
iMod (auth_map_map_insert with "physHists") as "(physHists & _ & physHistFrag)"; [try done|try done|].
iDestruct (big_sepM2_insert_delete with "[$ordered2 $order]") as "ordered3".
rewrite (insert_id orders). (* last congruence. *)
2: { rewrite ordersLook. f_equal.
rewrite orderEq.
reflexivity. }
iDestruct (bumpers_lookup with "allBumpers knowBumper") as %bumpersLook.
iModIntro.
iSplit; first (iApply frag_history_equiv; rewrite -encEs; iFrame "histFrag").
iFrameF "physHistFrag".
(* We re-establish [interp]. *)
repeat iExists _.
iFrameF "ptsMap".
iFrameF "offsets".
iFrameF "physHists".
iFrame "allOrders".
iFrame "ordered3".
iFrame "allPredsHold".
iFrame "history predicates".
iFrame "crashedAt".
iFrame "atLocs".
iFrame "naView naLocs allBumpers bumpMono".
(* oldViewsDiscarded *)
iSplit.
{ iEval (erewrite <- (insert_id offsets); last done).
iApply (big_sepM2_insert_2 with "[] oldViewsDiscarded").
iIntros (t2 ?).
iDestruct (big_sepM2_lookup _ _ _ ℓ with "oldViewsDiscarded") as %hi;
[done|done|].
destruct (decide (t_i = t2)) as [->|neq].
- iIntros (?). lia.
- rewrite lookup_insert_ne; last done.
iPureIntro. apply hi. }
(* historyFragments *)
iSplit.
{ iApply (big_sepM_insert_2 with "[] historyFragments").
iApply (big_sepM_insert_2 with "histFrag []").
iApply (big_sepM_lookup with "historyFragments").
done. }
iSplitPure; first apply locsDisjoint.
(* [histDomLocs] *)
iSplit. { iPureIntro. set_solver. }
(* [naViewsDom] *)
iSplitPure; first done.
(* [mapShared] - We need to show that the newly inserted message satisfied *)
(* the restriction on shared locations that their persist view and their *)
(* persisted after view is equal. *)
iSplit.
{ iPureIntro.
erewrite <- (insert_id offsets); last done.
rewrite -map_insert_zip_with.
setoid_rewrite (restrict_insert ℓ); last done.
rewrite /shared_locs_inv.
apply Nat.le_exists_sub in H2 as (tE & -> & ?).
rewrite -drop_prefix_insert.
apply map_map_Forall_insert_2.
- apply restrict_lookup_Some_2; last done.
apply map_lookup_zip_with_Some. naive_solver.
- simpl.
rewrite /atomic_loc_inv.
split; last done.
rewrite viewLook. lia.
- done. }
iSplitL "atLocsHistories".
{
rewrite /know_full_encoded_history_loc.
(* NOTE: This rewrite is mega-slow. *)
iEval (setoid_rewrite (restrict_insert ℓ at_locs (<[t_i:=es]> absHist) abs_hists ℓSh)).
iFrame. }
iFrameF "predPostCrash".
iFrame (bumperBumpToValid).
(* "bumperSome" *)
iApply (big_sepM2_update_left with "bumperSome"); eauto.
iPureIntro. intros bumperSome.
apply map_Forall_insert_2; eauto.
rewrite /encode_bumper. rewrite -encEs decode_encode. done. }
{ iIntros "physHists predHolds fullHist pts".
iDestruct (big_sepM_insert_delete with "[$atLocsHistories $fullHist]")
as "atLocsHistories".
iDestruct (big_sepM_insert_delete with "[$ptsMap $pts]") as "ptsMap".
iDestruct (big_sepM2_insert_delete with "[$ordered2]") as "ordered3";
first done.
iDestruct (big_sepM2_insert_delete with "[$allPredsHold predHolds]")
as "allPredsHold".
{ iExists pred. rewrite naViewLook. iFrame "predHolds". done. }
rewrite (insert_id orders); last congruence.
iEval (rewrite map_insert_zip_with) in "ptsMap".
iEval (rewrite (insert_id offsets); last done) in "ptsMap".
rewrite (insert_id phys_hists); last congruence.
rewrite (insert_id abs_hists); last congruence.
rewrite (insert_id (restrict at_locs abs_hists)).
2: { apply restrict_lookup_Some_2; done. }
(* We re-establish [interp]. *)
repeat iExists _.
iFrameF "ptsMap".
iFrameF "offsets".
iFrameF "physHists".
iFrameF "oldViewsDiscarded".
iFrame "allOrders".
iFrame "ordered".
iFrame "allPredsHold".
iFrame "history predicates".
iFrame "crashedAt".
iFrame "atLocs".
iFrame "naView naLocs allBumpers bumpMono predPostCrash atLocsHistories".
iFrame "bumperSome".
iFrame "historyFragments".
iFrame "%". }
Qed.
Lemma read_atomic_location_no_inv t_i t_l (physHist : history) absHist vm SVm FVm
PVm ℓ (e_i : positive) (s_i : ST) :
t_i ≤ t_l →
dom physHist = dom absHist →
absHist !! t_i = Some e_i →
decode e_i = Some s_i →
increasing_map (encode_relation (⊑@{ST})) absHist →
physHist !! t_l = Some (Msg vm SVm FVm PVm) →
∃ s_l e_l,
absHist !! t_l = Some e_l ∧
decode e_l = Some s_l ∧
(* SVm !!0 ℓ = t_l ∧ *)
(* FVm = PVm ∧ *)
s_i ⊑ s_l.
Proof.
intros le domEq ? decodeEnc ? ?.
assert (is_Some (absHist !! t_l)) as (e_l & ?).
{ apply elem_of_dom. rewrite <- domEq. apply elem_of_dom. naive_solver. }
(* The loaded state must be greater than [s_i]. *)
assert (encode_relation (⊑@{ST}) e_i e_l) as orderRelated.
{ eapply increasing_map_increasing_base; try done.
rewrite /encode_relation. rewrite decodeEnc. simpl. done. }
epose proof (encode_relation_inv _ _ _ orderRelated)
as (? & s_l & eqX & decodeS' & s3InclS').
assert (x = s_i) as -> by congruence.
exists s_l, e_l. done.
Qed.
Lemma read_atomic_location t_i t_l offset (physHist : history) absHist vm SVm FVm
PVm ℓ (e_i : positive) (s_i : ST) :
t_i ≤ t_l + offset →
dom physHist = dom absHist →
absHist !! t_i = Some e_i →
decode e_i = Some s_i →
increasing_map (encode_relation (⊑@{ST})) absHist →
map_Forall (λ (t : nat) (msg : message), atomic_loc_inv ℓ t msg)
(drop_prefix physHist offset) →
physHist !! (t_l + offset) = Some (Msg vm SVm FVm PVm) →
∃ s_l e_l,
absHist !! (t_l + offset) = Some e_l ∧
decode e_l = Some s_l ∧
SVm !!0 ℓ = t_l ∧
FVm = PVm ∧
s_i ⊑ s_l.
Proof.
intros le domEq ? decodeEnc ? atInvs ?.
eassert _ as temp.
{ eapply map_Forall_lookup_1; first apply atInvs.
rewrite drop_prefix_lookup. done. }
rewrite /atomic_loc_inv /= in temp. destruct temp as [SV'lookup <-].
assert (is_Some (absHist !! (t_l + offset))) as (e_l & ?).
{ apply elem_of_dom. rewrite <- domEq. apply elem_of_dom. naive_solver. }
(* The loaded state must be greater than [s_i]. *)
assert (encode_relation (⊑@{ST}) e_i e_l) as orderRelated.
{ eapply increasing_map_increasing_base; try done.
rewrite /encode_relation. rewrite decodeEnc. simpl. done. }
epose proof (encode_relation_inv _ _ _ orderRelated)
as (? & s_l & eqX & decodeS' & s3InclS').
assert (x = s_i) as -> by congruence.
exists s_l, e_l. done.
Qed.
Lemma map_sequence_big_sepM_sepL {A} (m : gmap nat A) lo hi ss (ϕ : A → iProp Σ) :
map_sequence m lo hi ss →
([∗ map] k ↦ y ∈ m, ϕ y) -∗
([∗ list] y ∈ ss, ϕ y) ∗ (([∗ list] y ∈ ss, ϕ y) -∗ [∗ map] k ↦ y ∈ m, ϕ y).
Proof.
generalize dependent m.
generalize dependent lo.
induction ss as [|x ss' IH]; first done.
iIntros (lo m seq) "M".
destruct ss' as [|x2 ss''].
{ destruct seq as [look ->]. rewrite /= right_id.
iApply (big_sepM_lookup_acc with "M"). done. }
simpl.
destruct seq as (look & lo2 & ? & ? & ?).
iDestruct (big_sepM_delete with "M") as "[$ M]"; first done.
iDestruct (IH lo2 with "M") as "[horse bing]".
{ apply map_sequence_delete_below; done. }
simpl.
iFrame "horse".
iIntros "[Hx Hx2]".
iDestruct ("bing" with "Hx2") as "H2".
iApply big_sepM_delete; first done.
iFrame.
Qed.
Lemma map_sequence_big_sepM2_sepL2 {A B}
(m1 : gmap nat A) (m2 : gmap nat B) tLo tHi ss ms
(ϕ : A → B → iProp Σ) :
map_sequence m1 tLo tHi ss →
map_sequence m2 tLo tHi ms →
([∗ map] k ↦ y1;y2 ∈ m1;m2, ϕ y1 y2) -∗
([∗ list] y1;y2 ∈ ss;ms, ϕ y1 y2) ∗
(([∗ list] y1;y2 ∈ ss;ms, ϕ y1 y2) -∗ [∗ map] k ↦ y1;y2 ∈ m1;m2, ϕ y1 y2).
Proof.
iIntros (seq1 seq2).
rewrite big_sepM2_alt.
rewrite big_sepL2_alt.
iIntros "(%domEq & M)".
assert (length ss = length ms) as lenEq.
{ eapply map_sequence_dom_length; done. }
iFrame (lenEq).
iDestruct (map_sequence_big_sepM_sepL with "M") as "[L Lreins]".
{ apply map_sequence_zip; done. }
iFrame "L".
iIntros "[_ L]".
iDestruct ("Lreins" with "L") as "$".
iPureIntro. done.
Qed.
(* Lemma bingo {A B} (m1 : gmap nat A) (m2 : gmap nat B) tLo tHi ss ms *)
(* (ϕ : A → B → dProp Σ) : *)
(* map_sequence m1 tLo tHi ss → *)
(* map_sequence m2 tLo tHi ms → *)
(* ⊢ ([∗ list] y1;y2 ∈ ss;ms, ϕ y1 y2) ∗-∗ ([∗ map] k ↦ y1;y2 ∈ m1;m2, ϕ y1 y2). *)
(* Proof. *)
(* iIntros (seq1 seq2). *)
(* Qed. *)
(* Lemma big_sepM2_union {A B} (Φ : nat → A → B → iProp Σ) (m1 m2 : gmap nat A) *)
(* (n1 n2 : gmap nat B) : *)
(* m1 ##ₘ m2 → *)
(* n1 ##ₘ n2 → *)
(* ([∗ map] k↦y1;y2 ∈ (m1 ∪ m2);(n1 ∪ n2), Φ k y1 y2) *)
(* ⊣⊢ ([∗ map] k↦y1;y2 ∈ m1;n1, Φ k y1 y2) ∗ ([∗ map] k↦y1;y2 ∈ m2;n2, Φ k y1 y2). *)
(* Proof. *)
(* intros disj1 disj2. *)
(* rewrite !big_sepM2_alt. *)
(* rewrite big_sepM_union. *)
(* apply big_opM_union. *)
(* Qed. *)
(* Lemma bingobongo (ϕ : ST → _) (pred : enc_predicateO) physHist absHist *)
(* encAbsHist fullEncAbsHist : *)
(* absHist = omap decode encAbsHist → *)
(* encAbsHist ⊆ fullEncAbsHist → *)
(* (pred ≡ encode_predicate ϕ) -∗ *)
(* encoded_predicate_hold physHist fullEncAbsHist pred -∗ *)
(* ([∗ map] msg;s ∈ physHist;absHist, *)
(* ϕ s (msg_val msg) _ (msg_store_view msg, msg_persisted_after_view msg, ∅)). *)
Definition fold_views (m : history) :=
map_fold
(λ _ msg (TV : thread_view),
(msg_store_view msg, msg_persisted_after_view msg, ∅) ⊔ TV) (∅, ∅, ∅) m.
Lemma fold_views_in_phys_hist phys_hist t msg :
phys_hist !! t = Some msg →
(msg_store_view msg, msg_persisted_after_view msg, ∅) ⊑ fold_views phys_hist.
Proof.
rewrite /fold_views. simpl.
apply (map_fold_ind (λ res phys_hist, phys_hist !! t = Some msg → _ ⊑ res)).
- inversion 1.
- intros t2 ???? IH.
destruct (decide (t = t2)) as [->|ne].
* rewrite lookup_insert.
inversion 1.
apply thread_view_le_l.
* rewrite lookup_insert_ne; last done.
rewrite -thread_view_le_r.
apply IH.
Qed.
Lemma extract_list_of_preds hG abs_hist (phys_hist : history) tLo tS ss ms prot
ℓ encAbsHist (pred : enc_predicateO) :
abs_hist = omap decode encAbsHist →
map_sequence abs_hist tLo tS ss →
map_sequence phys_hist tLo tS ms →
dom abs_hist = dom phys_hist →
(pred ≡ encode_predicate (p_inv prot)) -∗
encoded_predicate_hold hG phys_hist encAbsHist pred -∗
([∗ list] s;v ∈ ss;(msg_val <$> ms), p_inv prot s v) (fold_views phys_hist, hG).
Proof.
iIntros (-> seqAbs seqPhys domEq) "#equiv P".
rewrite /encoded_predicate_hold.
iDestruct (big_sepM2_impl_dom_subseteq _ _ _ _ phys_hist (omap decode encAbsHist) with "P []") as "P1".
{ done. }
{ done. }
{ iIntros "!>" (t m es m2 s ? encLook ? look) "H".
apply lookup_omap_Some in look as (? & ? & ?).
assert (m = m2) as <- by congruence.
assert (es = x) as <- by congruence.
iDestruct (predicate_holds_phi_decode_1 with "equiv H") as "H"; first done.
iApply "H". }
iDestruct (map_sequence_big_sepM2_sepL2 with "P1") as "[P1 Lreins]"; first done.
{ done. }
rewrite big_sepL2_flip.
rewrite monPred_at_big_sepL2.
rewrite big_sepL2_fmap_r.
iApply (big_sepL2_impl with "P1").
iIntros "!>" (idx s msg ? msLook) "pred".
iApply monPred_mono; last iApply "pred".
split; last done.
eapply map_sequence_list_lookup in msLook as (? & ? & look); last apply seqPhys.
eapply fold_views_in_phys_hist.
done.
Qed.
Lemma wp_load_at ℓ ss s Q1 Q2 prot st E :
{{{
ℓ ↦_AT^{prot} (ss ++ [s]) ∗
(* The case where we read an already known write. *)
((∀ vs vL, ∃ P (_ : Objective P) (_ : Persistent P),
⌜ last vs = Some vL ⌝ -∗
(* Extract knowledge from all the predicates. *)
(([∗ list] s; v ∈ ss ++ [s];vs, prot.(p_inv) s v) -∗ P) ∗
(* Using the [P] and the predicate for the loaded location show [Q1]. *)
(P -∗ <obj> (prot.(p_inv) s vL -∗ Q1 vL ∗ prot.(p_inv) s vL))) ∧
(* The case where we read a new write. *)
(∀ vs vL sL, ∃ P (_ : Objective P) (_ : Persistent P),
⌜ s ⊑ sL ⌝ -∗
(* Extract knowledge from all the predicates. *)
(([∗ list] s; v ∈ (ss ++ [s]) ++ [sL];vs ++ [vL], prot.(p_inv) s v) -∗ P) ∗
(P -∗ <obj> (prot.(p_inv) sL vL -∗ Q2 sL vL ∗ prot.(p_inv) sL vL))))
}}}
!_AT #ℓ @ st; E
{{{ vL, RET vL;
(∃ sL, ℓ ↦_AT^{prot} ((ss ++ [s]) ++ [sL]) ∗ <fence> Q2 sL vL) ∨
(* We didn't have to give the points-to predicate back here, but doing
* that is useful for users of the lemma. *)
(ℓ ↦_AT^{prot} (ss ++ [s]) ∗ <fence> Q1 vL)
}}}.
Proof.
intros Φ.
iModel.
iDestruct 1 as "(#pts & pToQ)".
iAssert (_) as "ptsCopy". { iApply "pts". }
iDestruct "pts" as (abs_hist phys_hist tLo tS offset s' ms) "H". iNamed "H".
assert (s' = s) as ->. { apply (inj Some). rewrite -lastEq. apply last_snoc. }
iDestruct "tSLe" as %tSLe.
(* iDestruct (store_lb_protocol with "storeLb") as "#knowProt". *)
iDestruct (know_protocol_extract with "locationProtocol")
as "(#knowPred & #knowPreorder & #knowBumper)".
(* rewrite /store_lb. *)
(* iDestruct "storeLb" as (tS offset) "(#prot & #hist & #offset & %tSLe)". *)
(* We unfold the WP. *)
iIntros ([TV' ?] [incl [= <-]]) "Φpost".
iApply wp_unfold_at.
iIntros ([[SV PV] BV] incl2) "#val".
iApply wp_extra_state_interp. { done. } { by apply prim_step_load_acq_no_fork. }
(* We open [interp]. *)
iIntros "interp".
simpl.
setoid_rewrite monPred_at_embed.
iDestruct (interp_get_at_loc gnames ℓ with "interp isAtLoc [locationProtocol] offset")
as (physHists physHist absHist pred) "(R & [_ reins])".
{ rewrite /know_protocol.
iEval (monPred_simpl).
simpl.
rewrite !monPred_at_embed.
iFrame "locationProtocol". }
iNamed "R".
iEval (rewrite monPred_at_big_sepM) in "physHist".
iEval (rewrite monPred_at_big_sepM) in "absHist".
iEval (setoid_rewrite monPred_at_sep) in "physHist".
simpl.
setoid_rewrite monPred_at_embed.
iAssert (⌜ phys_hist ⊆ physHist ⌝)%I as %sub.
{ rewrite map_subseteq_spec.
iIntros (?? physHistLook).
iDestruct (big_sepM_lookup with "physHist") as "[hi frag]"; first done.
iDestruct (auth_map_map_auth_frag with "physHists frag") as %(physHist' & ? & ?).
assert (physHist = physHist') as <-.
{ apply (inj Some). rewrite -physHistsLook. done. }
done. }
iDestruct (history_full_entry_frag_lookup_big with "fullHist absHist")
as %(encAbsHist & subset & domEq2 & eqeq & map).
(* We add this to prevent Coq from trying to use [highExtraStateInterp]. *)
set (extra := (Build_extraStateInterp _ _)).
iApply wp_fupd.
iApply (wp_load_acquire (extra := {| extra_state_interp := True |})
with "[$pts $val]").
iIntros "!>" (tL vL SV' PV' _PV') "(%look & %gt & #val' & pts)".
iFrame "val'".
assert (tS - offset ≤ tL) as lte.
{ etrans; first done. etrans; last done. f_equiv. solve_view_le. }
assert (tS ≤ tL + offset) as lte2 by lia.
rewrite drop_prefix_lookup in look.
assert (abs_hist !! tS = Some s).
{ rewrite -lastEq. eapply map_sequence_lookup_hi; done. }
iDestruct (big_sepM_lookup with "absHist") as "hist"; first done.
iDestruct (history_full_entry_frag_lookup with "fullHist hist")
as %(enc & lookTS & decodeEnc).
rewrite <- pure_sep_l; last solve_view_le.
(* The final view under the post fence modality where we will have to show
* [Q]. *)
set (TVfinal := (SV ⊔ SV', PV ⊔ (BV ⊔ PV'), BV ⊔ PV')).
assert (TV ⊑ TVfinal).
{ etrans; first apply incl. etrans; first apply incl2.
rewrite /TVfinal.
solve_view_le. }
(*
(* All the messages in the physical history are included in [TVfinal]. *)
iAssert (
⌜ map_Forall
(λ (_ : nat) (msg : message),
msg_store_view msg ⊑ store_view TVfinal
∧ msg_persisted_after_view msg ⊑ flush_view TVfinal) phys_hist ⌝
)%I as %inTVFinal.
{ iIntros (? msg looki).
iDestruct (big_sepM_lookup with "physHist") as "[[%have %have2] _]"; first done.
iPureIntro.
split.
- solve_view_le.
- etrans; first apply have2. rewrite /TVfinal.
simpl.
destruct TV as [[??]?].
destruct TV' as [[??]?].
simpl.
apply view_lub_le.
* solve_view_le.
* solve_view_le. }
*)
destruct (decide (tS = tL + offset)) as [eq|neq].
- iDestruct "pToQ" as "[pToQ _]".
subst.
assert (last (msg_val <$> ms) = Some vL).
{ rewrite fmap_last.
apply map_sequence_lookup_hi_alt in slicePhys
as (msg & physHistLook & ->).
eapply map_subseteq_spec in sub; last done.
rewrite -sub. rewrite look. done. }
iDestruct ("pToQ" $! (msg_val <$> ms) vL) as (P ? ?) "pToQ".
iDestruct ("pToQ" with "[]") as "[getP getQ]".
{ iPureIntro. done. }
iEval (monPred_simpl) in "getP".
iDestruct ("getP" $! (TV ⊔ _, gnames) with "[%] [-]") as "#P".
{ split; last done. apply thread_view_le_l. }
{ iDestruct (extract_list_of_preds _ _ _ _ _ _ _ _ _ _ _
with "predEquiv [predHolds]") as "L"; try done.
- iApply big_sepM2_impl_subseteq; try done.
rewrite -domEq2.
rewrite absPhysHistDomEq.
done.
- iApply monPred_mono; last done.
split; last done. apply thread_view_le_r.
}
iEval (monPred_simpl) in "getQ".
iDestruct ("getQ" with "[%] [P]") as "bing"; first done.
{ iApply objective_at. iApply "P". }
rewrite monPred_at_objectively.
eassert _ as temp.
{ eapply map_Forall_lookup_1; first apply atInvs.
rewrite drop_prefix_lookup. done. }
simpl.
destruct temp as (? & pvEq).
simpl in pvEq.
iDestruct (big_sepM2_lookup_acc with "predHolds") as "[predHolds predMap]";
[done|apply lookTS|].
simpl.
iDestruct (predicate_holds_phi_decode with "predEquiv predHolds") as "PH";
first done.
iDestruct ("bing" $! _ with "PH") as "[Q predHolds]".
iDestruct ("predMap" with "[predHolds]") as "predMap".
{ iApply (predicate_holds_phi_decode with "predEquiv predHolds").
assumption. }
iModIntro.
(* We re-establish [interp]. *)
iDestruct ("reins" with "[$] [$] [$] [$]") as "$".
iSpecialize ("Φpost" $! vL).
monPred_simpl.
iApply "Φpost".
{ iPureIntro.
split; last done.
etrans. eassumption.
repeat split; try done; try apply view_le_l. }
(* The thread view we started with [TV] is smaller than the view we ended
with. *)
(*
assert (TV ⊑ (SV ⊔ SV', PV, BV ⊔ PV')).
{ clear H4. do 2 (etrans; first done). repeat split; auto using view_le_l. }
*)
iRight. simpl.
iSplit. {
(* We show what is, essentially, the points-to predicate that we started with. *)
iExistsN.
iFrameF (lastEq).
iFrameF (slice).
iFrameF (slicePhys).
iFrameF (nolater).
iFrameF (absPhysHistDomEq).
iFrameF "isAtLoc".
rewrite 3!monPred_at_embed.
iFrameF "locationProtocol".
iSplitPure; first done.
rewrite /know_frag_history_loc_d.
rewrite monPred_at_big_sepM.
iEval (setoid_rewrite lift_d_at).
iFrameF "absHist".
iSplit.
{ iApply (monPred_mono _ (TV, _)).
{ split; last done.
etrans; first apply incl.
etrans; first apply incl2.
solve_view_le. }
rewrite monPred_at_big_sepM.
setoid_rewrite monPred_at_sep.
simpl.
setoid_rewrite monPred_at_embed.
iApply "physHist". }
iFrameF "offset".
iPureIntro.
etrans; first apply tSLe.
f_equiv.
solve_view_le. }
iApply monPred_mono; last iFrame "Q".
rewrite -pvEq.
split; last done.
solve_view_le.
- iDestruct "pToQ" as "[_ pToQ]".