-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_base.v
More file actions
231 lines (211 loc) · 6.9 KB
/
Copy pathsimple_base.v
File metadata and controls
231 lines (211 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
From iris.proofmode Require Import tactics.
From Perennial.program_logic Require Import crash_weakestpre.
From self.base Require Export primitive_laws class_instances post_crash_modality.
From self.algebra Require Import view.
From self.base Require Import proofmode wpc_proofmode wpr_lifting.
From self.lang Require Import lang.
Definition assignment_prog (ℓ : loc) : expr :=
#ℓ <-_NA #1.
Definition init_hist : history := {[ 0 := Msg #0 ∅ ∅ ∅ ]}.
Section simple_assignment.
Context `{!nvmBaseFixedG Σ, extraStateInterp Σ, nvmBaseDeltaG}.
Lemma wpc_assignment ℓ st E :
{{{ validV ∅ ∗
persisted_loc ℓ 0 ∗
ℓ ↦h init_hist
}}}
assignment_prog ℓ `at` (∅, ∅, ∅) @ st; E
{{{ v t TV, RET ThreadVal v TV;
ℓ ↦h {[ t := Msg #1 ∅ ∅ ∅; 0 := Msg #0 ∅ ∅ ∅ ]}
}}}
{{{ <PC> _,
(ℓ ↦h {[ 0 := Msg #0 ∅ ∅ ∅ ]} ∨
(ℓ ↦h {[ 0 := Msg #1 ∅ ∅ ∅ ]}))
}}}.
Proof.
iIntros (Φ Φc) "(#val & #per & pts) post".
rewrite /assignment_prog /init_hist.
iApply wpc_atomic_no_mask.
iSplit.
{ crash_case.
iDestruct (post_crash_mapsto with "pts") as "pts".
iDestruct "per" as "-#per".
iCrash.
iDestruct "per" as "(per & (%CV & %t & (%look & %le) & crashed))".
rewrite /mapsto_post_crash.
iDestruct "pts" as (CV') "[crashed' [H | %]]";
iDestruct (crashed_at_agree with "crashed crashed'") as %<-.
2: { congruence. }
iDestruct "H" as (??? histLook ?) "pts".
apply lookup_singleton_Some in histLook as [<- <-].
iLeft. iFrame. }
iApply (wp_store with "[$pts $val]").
iNext. iIntros (t) "(%hlook & %gtT & val2 & pts)".
iSplit.
{ iModIntro. crash_case.
iDestruct (post_crash_mapsto with "pts") as "pts".
iDestruct "per" as "-#per".
iCrash.
iDestruct "per" as "(per & (%CV & % & (%look & %le) & crashed))".
rewrite /mapsto_post_crash.
iDestruct "pts" as (CV') "[crashed' [H | %]]";
iDestruct (crashed_at_agree with "crashed crashed'") as %<-.
2: { congruence. }
iDestruct "H" as (tn ?? histLook ?) "pts".
destruct (decide (tn = t)) as [->|neq].
- rewrite lookup_insert in histLook.
inversion histLook.
simpl.
iRight. iFrame.
- rewrite lookup_insert_ne in histLook; last done.
apply lookup_singleton_Some in histLook as [? <-].
iLeft. iFrame. }
iModIntro.
iApply "post".
iFrame "pts".
Qed.
End simple_assignment.
Section simple_increment.
Context `{!nvmBaseFixedG Σ, extraStateInterp Σ, nvmBaseDeltaG}.
Definition pure : expr :=
let: "a" := #1 in
let: "b" := #7 in
"a" + "b".
Lemma wp_with_let TV :
{{{ True }}} pure `at` TV {{{ RET ThreadVal (#8) TV; True }}}.
Proof.
iIntros (Φ) "_ Post".
rewrite /pure.
wp_pures.
iModIntro.
by iApply "Post".
Qed.
Lemma wpc_with_let TV E1 :
{{{ True }}} pure `at` TV @ E1 {{{ RET ThreadVal (#8) TV; True }}}{{{ True }}}.
Proof.
iIntros (Φ Φc) "_ Post".
rewrite /pure.
iCache with "Post".
{ iLeft in "Post". by iApply "Post". }
wpc_pures.
iCache with "Post".
{ iLeft in "Post". by iApply "Post". }
wpc_pures. rewrite /thread_fill_item. simpl.
wpc_pures.
by iApply "Post".
Qed.
Definition alloc_load : expr :=
let: "ℓ" := ref_NA #4
in !_NA "ℓ".
Lemma wp_load_store SV PV :
{{{ validV SV }}} alloc_load `at` (SV, PV, ∅) {{{ TV', RET ThreadVal (#4) TV'; True }}}.
Proof.
iIntros (Φ) "#val Post".
rewrite /alloc_load.
wp_apply (wp_alloc with "val"). iIntros (ℓ ?) "(_ & _ & _ & pts)".
wp_pures.
wp_pures.
iApply (wp_load with "[$pts $val]").
iNext. iIntros (t msg) "(pts & %look & gt)".
move: look.
rewrite /initial_history.
intros [<- <-]%lookup_singleton_Some.
iApply "Post".
done.
Qed.
Definition incr_both (ℓ1 ℓ2 : loc) : expr :=
#ℓ1 <-_NA #1 ;;
Flush #ℓ1 ;;
Fence ;;
#ℓ2 <-_NA #1.
Definition incr_both_recover (ℓ1 ℓ2 : loc) : expr :=
let: "x" := !_NA #ℓ1 in
let: "y" := !_NA #ℓ2 in
if: "y" ≤ "x"
then #() #()
else #().
Lemma wpc_incr ℓ1 ℓ2 st E1 :
{{{ validV ∅ ∗
ℓ1 ↦h init_hist ∗
ℓ2 ↦h init_hist }}}
ThreadState (incr_both ℓ1 ℓ2) (∅, ∅, ∅) @ st; E1
{{{ v t1 t2 TV, RET ThreadVal v TV;
ℓ1 ↦h {[ t1 := Msg #1 ∅ ∅ ∅; 0 := Msg #0 ∅ ∅ ∅ ]} ∗
ℓ2 ↦h {[ t2 := Msg #1 ∅ ∅ {[ ℓ1 := MaxNat t1 ]}; 0 := Msg #0 ∅ ∅ ∅ ]}
}}}
{{{ True }}}.
Proof.
iIntros (Φ Φc) "(val & ℓ1pts & ℓ2pts) Φpost".
rewrite /incr_both.
iDestruct (mapsto_ne with "ℓ1pts ℓ2pts") as "%ℓne".
wpc_bind (_ <-_NA _)%E.
iApply wpc_atomic_no_mask.
iSplit.
{ crash_case. done. }
iApply (wp_store with "[$ℓ1pts $val]").
iNext. iIntros (t) "(%hlook & %gtT & val & ℓ1pts)".
iSplit.
{ iDestruct "Φpost" as "[Φpost _]". iModIntro. by iApply "Φpost". }
iModIntro.
iCache with "Φpost".
{ iDestruct "Φpost" as "[Φpost _]". by iApply "Φpost". }
simpl.
wpc_pures. rewrite /thread_fill_item. simpl.
wpc_pures.
(* Flush *)
wpc_bind (Flush _)%E.
iApply wpc_atomic_no_mask.
iSplit. { iFromCache. }
iApply (wp_flush with "ℓ1pts").
iNext. iIntros "ℓ1pts".
iSplit.
{ iDestruct "Φpost" as "[Φpost _]". iModIntro. by iApply "Φpost". }
iModIntro.
simpl.
wpc_pures. rewrite /thread_fill_item. simpl.
wpc_pures.
(* Fence *)
wpc_bind Fence.
iApply wpc_atomic_no_mask.
iSplit. { iFromCache. }
iApply (wp_fence with "[//]").
iIntros "!> _".
iSplit.
{ iDestruct "Φpost" as "[Φpost _]". iModIntro. by iApply "Φpost". }
iModIntro.
simpl.
wpc_pures. rewrite /thread_fill_item. simpl.
wpc_pures.
(* Store *)
wpc_bind (_ <-_NA _)%E.
iApply wpc_atomic_no_mask.
iSplit.
{ crash_case. done. }
iApply (wp_store with "[$ℓ2pts $val]").
iNext. iIntros (t') "(%hlook' & %gt & val' & ℓ2pts)".
iSplit.
{ iDestruct "Φpost" as "[Φpost _]". iModIntro. by iApply "Φpost". }
iModIntro.
iApply "Φpost".
rewrite /init_hist. simpl.
iFrame "ℓ1pts".
simpl.
autorewrite with view_simpl in gt.
autorewrite with view_simpl.
rewrite lookup_zero_singleton.
iFrame "ℓ2pts".
Qed.
(* Lemma wpr_incr ℓ1 ℓ2 s E : *)
(* validV ∅ -∗ *)
(* ℓ1 ↦h init_hist -∗ *)
(* ℓ2 ↦h init_hist -∗ *)
(* wpr s E *)
(* (incr_both ℓ1 ℓ2 `at` (∅, ∅, ∅)) *)
(* (incr_both_recover ℓ1 ℓ2 `at` (∅, ∅, ∅)) (λ _, True)%I (λ _, True)%I (λ _ _, True)%I. *)
(* Proof. *)
(* iIntros "#val pts1 pts2". *)
(* iApply (idempotence_wpr with "[pts1 pts2]"). *)
(* - iApply (wpc_incr with "[$val $pts1 $pts2]"). *)
(* iSplit. { iIntros "$". } *)
(* Qed. *)
End simple_increment.