-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken2.v
235 lines (202 loc) · 7.09 KB
/
token2.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
From stdpp Require Import coPset.
From iris.algebra Require Import excl functions.
From iris.bi.lib Require Import fractional.
From iris.proofmode Require Import proofmode.
From iris.base_logic.lib Require Export own.
From iris.prelude Require Import options.
From smr Require Import helpers.
Definition token2R : ucmra :=
(positive * positive) -d> optionUR $ exclR $ unitO.
Definition to_token2 (E1 E2 : coPset) : token2R :=
λ k, if bool_decide (k.1 ∈ E1 ∧ k.2 ∈ E2)
then Excl' ()
else None.
Section token2R_lemmas.
Implicit Types (k : positive) (E : coPset).
(* general token2 *)
Lemma to_token2_valid E1 E2 :
✓ to_token2 E1 E2.
Proof.
intros x. unfold to_token2.
by case bool_decide.
Qed.
Lemma to_token2_lookup k1 k2 E1 E2 :
k1 ∈ E1 → k2 ∈ E2 →
to_token2 E1 E2 (k1, k2) = Excl' ().
Proof. intros. unfold to_token2. by rewrite bool_decide_eq_true_2. Qed.
Lemma to_token2_lookup_None k1 k2 E1 E2 :
k1 ∉ E1 ∨ k2 ∉ E2 →
to_token2 E1 E2 (k1, k2) = None.
Proof.
intros. unfold to_token2.
rewrite bool_decide_eq_false_2; auto. set_solver.
Qed.
Local Ltac yes := (rewrite to_token2_lookup; auto).
Local Ltac no := (rewrite to_token2_lookup_None; auto).
Lemma to_token2_union_1 E1A E1B E2 :
E1A ## E1B →
to_token2 E1A E2 ⋅ to_token2 E1B E2
≡ to_token2 (E1A ∪ E1B) E2.
Proof.
intros DISJ (k1, k2). rewrite discrete_fun_lookup_op.
destruct (decide (k1 ∈ E1A)); destruct (decide (k1 ∈ E1B));
destruct (decide (k2 ∈ E2)).
- set_solver.
- set_solver.
- yes. no. yes. set_solver.
- no. no. no.
- no. yes. yes. set_solver.
- no. no. no.
- no. no. no. set_solver.
- no. no. no.
Qed.
Lemma to_token2_union_2 E1 E2A E2B :
E2A ## E2B →
to_token2 E1 E2A ⋅ to_token2 E1 E2B
≡ to_token2 E1 (E2A ∪ E2B).
Proof.
intros DISJ (k1, k2). rewrite discrete_fun_lookup_op.
destruct (decide (k1 ∈ E1));
destruct (decide (k2 ∈ E2A)); destruct (decide (k2 ∈ E2B)).
- set_solver.
- yes. no. yes. set_solver.
- no. yes. yes. set_solver.
- no. no. no. set_solver.
- no. no. no.
- no. no. no.
- no. no. no.
- no. no. no.
Qed.
Lemma to_token2_op E1A E1B E2A E2B :
to_token2 E1A E2A ⋅ to_token2 E1B E2B
= λ x, (to_token2 E1A E2A x) ⋅ (to_token2 E1B E2B x).
Proof. auto. Qed.
Lemma to_token2_insert_1 E1 E2 k :
k ∉ E1 →
to_token2 (E1 ∪ {[k]}) E2
≡ to_token2 E1 E2 ⋅ to_token2 {[k]} E2.
Proof. intros. rewrite to_token2_union_1; set_solver. Qed.
Lemma to_token2_insert_2 E1 E2 k :
k ∉ E2 →
to_token2 E1 (E2 ∪ {[k]})
≡ to_token2 E1 E2 ⋅ to_token2 E1 {[k]}.
Proof. intros. rewrite to_token2_union_2; set_solver. Qed.
End token2R_lemmas.
Class token2G Σ := GhostVars2G {
token2_inG : inG Σ (token2R);
}.
Local Existing Instance token2_inG.
Definition token2Σ : gFunctors := #[GFunctor token2R].
Global Instance subG_token2Σ {Σ} :
subG token2Σ Σ → token2G Σ.
Proof. solve_inG. Qed.
Section def.
Context `{!token2G Σ}.
Definition token2_def (γ : gname) (E1 E2 : coPset) : iProp Σ :=
own γ (to_token2 E1 E2).
Definition token2_aux : seal (@token2_def). Proof. by eexists. Qed.
Definition token2 := token2_aux.(unseal).
Definition token2_unseal : @token2 = @token2_def := token2_aux.(seal_eq).
End def.
Section lemmas.
Context `{!token2G Σ}.
Implicit Types (k : positive) (E : coPset) (q : Qp).
Local Ltac unseal := rewrite ?token2_unseal /token2_def.
Lemma token2_valid_1 γ E1A E1B E2 :
E2 ≠ ∅ →
token2 γ E1A E2 -∗ token2 γ E1B E2
-∗ ⌜E1A ## E1B⌝.
Proof.
unseal. iIntros (HE) "T1 T2".
iCombine "T1 T2" gives %H. iPureIntro.
rewrite to_token2_op in H.
destruct (decide (E1A ∩ E1B = ∅)); first set_solver.
destruct (coPset_choose (E1A ∩ E1B)) as [x Hx]; auto.
destruct (coPset_choose E2) as [y Hy]; auto.
specialize (H (x, y)); simpl in H.
unfold to_token2 in H; simpl in H.
do 2 (rewrite bool_decide_eq_true_2 in H; last set_solver).
done.
Qed.
Lemma token2_valid_2 γ E1 E2A E2B :
E1 ≠ ∅ →
token2 γ E1 E2A -∗ token2 γ E1 E2B
-∗ ⌜E2A ## E2B⌝.
Proof.
unseal. iIntros (HE) "T1 T2".
iCombine "T1 T2" gives %?. iPureIntro.
rewrite to_token2_op in H.
destruct (decide (E2A ∩ E2B = ∅)); first set_solver.
destruct (coPset_choose (E2A ∩ E2B)) as [x Hx]; auto.
destruct (coPset_choose E1) as [y Hy]; auto.
specialize (H (y, x)); simpl in H.
unfold to_token2 in H; simpl in H.
do 2 (rewrite bool_decide_eq_true_2 in H; last set_solver).
done.
Qed.
Lemma token2_union_1 γ E1A E1B E2 :
E1A ## E1B →
token2 γ E1A E2 ∗ token2 γ E1B E2
⊣⊢ token2 γ (E1A ∪ E1B) E2.
Proof. unseal. intros. by rewrite -own_op to_token2_union_1. Qed.
Lemma token2_union_2 γ E1 E2A E2B :
E2A ## E2B →
token2 γ E1 E2A ∗ token2 γ E1 E2B
⊣⊢ token2 γ E1 (E2A ∪ E2B).
Proof. unseal. intros. by rewrite -own_op to_token2_union_2. Qed.
Lemma token2_get_subset_1 γ E1A E1B E2 :
E1B ⊆ E1A →
token2 γ E1A E2 ⊣⊢ token2 γ E1B E2 ∗ token2 γ (E1A ∖ E1B) E2.
Proof.
intro SUBSET. assert (E1A = E1B ∪ E1A ∖ E1B) as EQ.
{ rewrite union_comm_L -union_difference_L'; done. }
rewrite {1}EQ -token2_union_1; [done|].
apply disjoint_difference_r1. done.
Qed.
Lemma token2_get_subset_2 γ E1 E2A E2B :
E2B ⊆ E2A →
token2 γ E1 E2A ⊣⊢ token2 γ E1 E2B ∗ token2 γ E1 (E2A ∖ E2B).
Proof.
intro SUBSET. assert (E2A = E2B ∪ E2A ∖ E2B) as EQ.
{ rewrite union_comm_L -union_difference_L'; done. }
rewrite {1}EQ -token2_union_2; [done|].
apply disjoint_difference_r1. done.
Qed.
Lemma token2_get_empty_1 γ E :
⊢ |==> token2 γ ∅ E.
Proof.
unseal. iMod own_unit as "H". iApply (own_update with "H").
apply discrete_fun_update.
intros []???; rewrite to_token2_lookup_None; set_solver.
Qed.
Lemma token2_get_empty_2 γ E :
⊢ |==> token2 γ E ∅.
Proof.
unseal. iMod own_unit as "H". iApply (own_update with "H").
apply discrete_fun_update.
intros []???; rewrite to_token2_lookup_None; set_solver.
Qed.
Global Instance token2_timeless γ E1 E2 :
Timeless (token2 γ E1 E2).
Proof. unseal. apply _. Qed.
Lemma token2_alloc_strong E1 E2 (P : gname → Prop) :
pred_infinite P →
⊢ |==> ∃ γ, ⌜P γ⌝ ∗ token2 γ E1 E2.
Proof.
unseal. intros. iApply own_alloc_strong; auto.
apply to_token2_valid.
Qed.
Lemma token2_alloc E1 E2 :
⊢ |==> ∃ γ, token2 γ E1 E2.
Proof. unseal. iApply own_alloc. apply to_token2_valid. Qed.
Lemma token2_insert_1 γ E1 E2 k :
k ∉ E1 →
token2 γ (E1 ∪ {[k]}) E2
⊣⊢ token2 γ E1 E2 ∗ token2 γ {[k]} E2.
Proof. unseal. intros. by rewrite -own_op -to_token2_insert_1. Qed.
Lemma token2_insert_2 γ E1 E2 k :
k ∉ E2 →
token2 γ E1 (E2 ∪ {[k]})
⊣⊢ token2 γ E1 E2 ∗ token2 γ E1 {[k]}.
Proof. unseal. intros. by rewrite -own_op -to_token2_insert_2. Qed.
End lemmas.