boxes.v 11.8 KB
Newer Older
1
From iris.base_logic.lib Require Export invariants.
2
3
From iris.algebra Require Import auth gmap agree.
From iris.base_logic Require Import big_op.
4
From iris.proofmode Require Import tactics.
5
6
7
Import uPred.

(** The CMRAs we need. *)
8
9
Class boxG Σ :=
  boxG_inG :> inG Σ (prodR
10
    (authR (optionUR (exclR boolC)))
11
    (optionR (agreeR (laterC (iPreProp Σ))))).
12
13

Section box_defs.
14
  Context `{invG Σ, boxG Σ} (N : namespace).
15

16
  Definition slice_name := gname.
17

Robbert Krebbers's avatar
Robbert Krebbers committed
18
19
  Definition box_own_auth (γ : slice_name) (a : auth (option (excl bool))) : iProp Σ :=
    own γ (a, (:option (agree (later (iPreProp Σ))))).
20

21
  Definition box_own_prop (γ : slice_name) (P : iProp Σ) : iProp Σ :=
22
    own γ (:auth (option (excl bool)), Some (to_agree (Next (iProp_unfold P)))).
23

24
  Definition slice_inv (γ : slice_name) (P : iProp Σ) : iProp Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
25
    ( b, box_own_auth γ ( Excl' b)  if b then P else True)%I.
26

27
  Definition slice (γ : slice_name) (P : iProp Σ) : iProp Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
28
    (box_own_prop γ P  inv N (slice_inv γ P))%I.
29

30
31
  Definition box (f : gmap slice_name bool) (P : iProp Σ) : iProp Σ :=
    ( Φ : slice_name  iProp Σ,
32
33
       (P  [ map] γ  b  f, Φ γ) 
      [ map] γ  b  f, box_own_auth γ ( Excl' b)  box_own_prop γ (Φ γ) 
34
                         inv N (slice_inv γ (Φ γ)))%I.
35
36
End box_defs.

37
38
39
40
Instance: Params (@box_own_prop) 3.
Instance: Params (@slice_inv) 3.
Instance: Params (@slice) 5.
Instance: Params (@box) 5.
41

42
Section box.
43
Context `{invG Σ, boxG Σ} (N : namespace).
44
Implicit Types P Q : iProp Σ.
45

46
Global Instance box_own_prop_ne n γ : Proper (dist n ==> dist n) (box_own_prop γ).
47
Proof. solve_proper. Qed.
48
Global Instance box_inv_ne n γ : Proper (dist n ==> dist n) (slice_inv γ).
49
Proof. solve_proper. Qed.
50
Global Instance slice_ne n γ : Proper (dist n ==> dist n) (slice N γ).
51
Proof. solve_proper. Qed.
52
53
54
55
56
57
Global Instance box_contractive f : Contractive (box N f).
Proof.
  intros n P1 P2 HP1P2. apply exist_ne. intros Φ. f_equiv; last done.
  apply contractive. intros n' ?. by rewrite HP1P2.
Qed.

58
Global Instance slice_persistent γ P : PersistentP (slice N γ P).
59
60
Proof. apply _. Qed.

61
Lemma box_own_auth_agree γ b1 b2 :
Ralf Jung's avatar
Ralf Jung committed
62
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)  b1 = b2.
63
Proof.
64
  rewrite /box_own_prop -own_op own_valid prod_validI /= and_elim_l.
65
  by iDestruct 1 as % [[[] [=]%leibniz_equiv] ?]%auth_valid_discrete.
66
67
Qed.

68
Lemma box_own_auth_update γ b1 b2 b3 :
69
70
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)
  == box_own_auth γ ( Excl' b3)  box_own_auth γ ( Excl' b3).
71
Proof.
72
73
  rewrite /box_own_auth -!own_op. apply own_update, prod_update; last done.
  by apply auth_update, option_local_update, exclusive_local_update.
74
75
76
Qed.

Lemma box_own_agree γ Q1 Q2 :
77
  box_own_prop γ Q1  box_own_prop γ Q2   (Q1  Q2).
78
Proof.
79
  rewrite /box_own_prop -own_op own_valid prod_validI /= and_elim_r.
80
  rewrite option_validI /= agree_validI agree_equivI later_equivI /=.
81
  iIntros "#HQ". iNext. rewrite -{2}(iProp_fold_unfold Q1).
82
83
84
  iRewrite "HQ". by rewrite iProp_fold_unfold.
Qed.

85
Lemma box_alloc : box N  True%I.
86
87
88
89
90
91
Proof.
  iIntros; iExists (λ _, True)%I; iSplit.
  - iNext. by rewrite big_sepM_empty.
  - by rewrite big_sepM_empty.
Qed.

92
93
94
Lemma slice_insert_empty E q f Q P :
  ?q box N f P ={E}=  γ, f !! γ = None 
    slice N γ Q  ?q box N (<[γ:=false]> f) (Q  P).
95
Proof.
96
  iDestruct 1 as (Φ) "[#HeqP Hf]".
97
  iMod (own_alloc_strong ( Excl' false   Excl' false,
98
    Some (to_agree (Next (iProp_unfold Q)))) (dom _ f))
99
    as (γ) "[Hdom Hγ]"; first done.
100
101
  rewrite pair_split. iDestruct "Hγ" as "[[Hγ Hγ'] #HγQ]".
  iDestruct "Hdom" as % ?%not_elem_of_dom.
102
  iMod (inv_alloc N _ (slice_inv γ Q) with "[Hγ]") as "#Hinv".
103
  { iNext. iExists false; eauto. }
104
  iModIntro; iExists γ; repeat iSplit; auto.
105
  iNext. iExists (<[γ:=Q]> Φ); iSplit.
106
  - iNext. iRewrite "HeqP". by rewrite big_sepM_fn_insert'.
107
  - rewrite (big_sepM_fn_insert (λ _ _ P',  _  _ _ P'  _ _ (_ _ P')))%I //.
108
    iFrame; eauto.
109
110
Qed.

111
Lemma slice_delete_empty E q f P Q γ :
112
  N  E 
113
  f !! γ = Some false 
114
115
  slice N γ Q - ?q box N f P ={E}=  P',
    ?q  (P  (Q  P'))  ?q box N (delete γ f) P'.
116
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
117
  iIntros (??) "[#HγQ Hinv] H". iDestruct "H" as (Φ) "[#HeqP Hf]".
118
  iExists ([ map] γ'_  delete γ f, Φ γ')%I.
119
  iInv N as (b) "[>Hγ _]" "Hclose".
120
  iDestruct (big_sepM_delete _ f _ false with "Hf")
121
    as "[[>Hγ' #[HγΦ ?]] ?]"; first done.
122
  iDestruct (box_own_auth_agree γ b false with "[-]") as %->; first by iFrame.
123
124
125
126
  iMod ("Hclose" with "[Hγ]"); first iExists false; eauto.
  iModIntro. iNext. iSplit.
  - iDestruct (box_own_agree γ Q (Φ γ) with "[#]") as "HeqQ"; first by eauto.
    iNext. iRewrite "HeqP". iRewrite "HeqQ". by rewrite -big_sepM_delete.
127
  - iExists Φ; eauto.
128
129
Qed.

130
Lemma slice_fill E q f γ P Q :
131
  N  E 
132
  f !! γ = Some false 
133
  slice N γ Q -  Q - ?q box N f P ={E}= ?q box N (<[γ:=true]> f) P.
134
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
135
136
  iIntros (??) "#[HγQ Hinv] HQ H"; iDestruct "H" as (Φ) "[#HeqP Hf]".
  iInv N as (b') "[>Hγ _]" "Hclose".
137
  iDestruct (big_sepM_delete _ f _ false with "Hf")
Robbert Krebbers's avatar
Robbert Krebbers committed
138
    as "[[>Hγ' #[HγΦ Hinv']] ?]"; first done.
139
  iMod (box_own_auth_update γ b' false true with "[$Hγ $Hγ']") as "[Hγ Hγ']".
140
141
  iMod ("Hclose" with "[Hγ HQ]"); first (iNext; iExists true; by iFrame).
  iModIntro; iNext; iExists Φ; iSplit.
142
  - by rewrite big_sepM_insert_override.
143
  - rewrite -insert_delete big_sepM_insert ?lookup_delete //.
144
    iFrame; eauto.
145
146
Qed.

147
Lemma slice_empty E q f P Q γ :
148
  N  E 
149
  f !! γ = Some true 
150
  slice N γ Q - ?q box N f P ={E}=  Q  ?q box N (<[γ:=false]> f) P.
151
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
152
153
  iIntros (??) "#[HγQ Hinv] H"; iDestruct "H" as (Φ) "[#HeqP Hf]".
  iInv N as (b) "[>Hγ HQ]" "Hclose".
Ralf Jung's avatar
Ralf Jung committed
154
  iDestruct (big_sepM_delete _ f with "Hf")
Robbert Krebbers's avatar
Robbert Krebbers committed
155
    as "[[>Hγ' #[HγΦ Hinv']] ?]"; first done.
156
  iDestruct (box_own_auth_agree γ b true with "[-]") as %->; first by iFrame.
157
  iFrame "HQ".
158
  iMod (box_own_auth_update γ with "[$Hγ $Hγ']") as "[Hγ Hγ']".
159
160
  iMod ("Hclose" with "[Hγ]"); first (iNext; iExists false; by repeat iSplit).
  iModIntro; iNext; iExists Φ; iSplit.
161
  - by rewrite big_sepM_insert_override.
162
  - rewrite -insert_delete big_sepM_insert ?lookup_delete //.
163
    iFrame; eauto.
164
165
Qed.

166
Lemma slice_insert_full E q f P Q :
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
167
  N  E 
168
169
   Q - ?q box N f P ={E}=  γ, f !! γ = None 
    slice N γ Q  ?q box N (<[γ:=true]> f) (Q  P).
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
170
Proof.
171
  iIntros (?) "HQ Hbox".
Robbert Krebbers's avatar
Robbert Krebbers committed
172
173
  iMod (slice_insert_empty with "Hbox") as (γ) "(% & #Hslice & Hbox)".
  iExists γ. iFrame "%#". iMod (slice_fill with "Hslice HQ Hbox"); first done.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
174
175
176
  by apply lookup_insert. by rewrite insert_insert.
Qed.

177
Lemma slice_delete_full E q f P Q γ :
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
178
179
  N  E 
  f !! γ = Some true 
180
181
  slice N γ Q - ?q box N f P ={E}=
   P',  Q  ?q  (P  (Q  P'))  ?q box N (delete γ f) P'.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
182
Proof.
183
  iIntros (??) "#Hslice Hbox".
Robbert Krebbers's avatar
Robbert Krebbers committed
184
185
186
  iMod (slice_empty with "Hslice Hbox") as "[$ Hbox]"; try done.
  iMod (slice_delete_empty with "Hslice Hbox") as (P') "[Heq Hbox]"; first done.
  { by apply lookup_insert. }
187
  iExists P'. iFrame. rewrite -insert_delete delete_insert ?lookup_delete //.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
188
189
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
190
Lemma box_fill E f P :
191
  N  E 
192
  box N f P -  P ={E}= box N (const true <$> f) P.
193
Proof.
194
  iIntros (?) "H HP"; iDestruct "H" as (Φ) "[#HeqP Hf]".
195
  iExists Φ; iSplitR; first by rewrite big_sepM_fmap.
196
197
  rewrite internal_eq_iff later_iff big_sepM_later.
  iDestruct ("HeqP" with "HP") as "HP".
198
  iCombine "Hf" "HP" as "Hf".
199
  rewrite big_sepM_fmap; iApply (fupd_big_sepM _ _ f).
200
  iApply (big_sepM_impl _ _ f); iFrame "Hf".
201
  iAlways; iIntros (γ b' ?) "[(Hγ' & #$ & #$) HΦ]".
Robbert Krebbers's avatar
Robbert Krebbers committed
202
  iInv N as (b) "[>Hγ _]" "Hclose".
203
  iMod (box_own_auth_update γ with "[Hγ Hγ']") as "[Hγ $]"; first by iFrame.
204
  iApply "Hclose". iNext; iExists true. by iFrame.
205
206
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
207
Lemma box_empty E f P :
208
  N  E 
209
  map_Forall (λ _, (true =)) f 
210
  box N f P ={E}=  P  box N (const false <$> f) P.
211
Proof.
212
  iDestruct 1 as (Φ) "[#HeqP Hf]".
213
214
  iAssert ([ map] γ↦b  f,  Φ γ  box_own_auth γ ( Excl' false) 
    box_own_prop γ (Φ γ)  inv N (slice_inv γ (Φ γ)))%I with ">[Hf]" as "[HΦ ?]".
215
  { iApply (fupd_big_sepM _ _ f); iApply (big_sepM_impl _ _ f); iFrame "Hf".
Robbert Krebbers's avatar
Robbert Krebbers committed
216
    iAlways; iIntros (γ b ?) "(Hγ' & #HγΦ & #Hinv)".
217
    assert (true = b) as <- by eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
218
    iInv N as (b) "[>Hγ HΦ]" "Hclose".
219
    iDestruct (box_own_auth_agree γ b true with "[-]") as %->; first by iFrame.
220
    iMod (box_own_auth_update γ true true false with "[$Hγ $Hγ']") as "[Hγ $]".
221
    iMod ("Hclose" with "[Hγ]"); first (iNext; iExists false; iFrame; eauto).
Robbert Krebbers's avatar
Robbert Krebbers committed
222
    iFrame "HγΦ Hinv". by iApply "HΦ". }
223
  iModIntro; iSplitL "HΦ".
224
  - rewrite internal_eq_iff later_iff big_sepM_later. by iApply "HeqP".
225
226
  - iExists Φ; iSplit; by rewrite big_sepM_fmap.
Qed.
227

228
Lemma slice_split E q f P Q1 Q2 γ b :
229
  N  E  f !! γ = Some b 
230
  slice N γ (Q1  Q2) - ?q box N f P ={E}=  γ1 γ2,
231
    delete γ f !! γ1 = None  delete γ f !! γ2 = None  ⌜γ1  γ2 
232
    slice N γ1 Q1  slice N γ2 Q2  ?q box N (<[γ2 := b]>(<[γ1 := b]>(delete γ f))) P.
233
234
Proof.
  iIntros (??) "#Hslice Hbox". destruct b.
Robbert Krebbers's avatar
Robbert Krebbers committed
235
  - iMod (slice_delete_full with "Hslice Hbox") as (P') "([HQ1 HQ2] & Heq & Hbox)"; try done.
236
237
    iMod (slice_insert_full with "HQ1 Hbox") as (γ1) "(% & #Hslice1 & Hbox)"; first done.
    iMod (slice_insert_full with "HQ2 Hbox") as (γ2) "(% & #Hslice2 & Hbox)"; first done.
238
239
240
    iExists γ1, γ2. iFrame "%#". iModIntro. iSplit; last iSplit; try iPureIntro.
    { by eapply lookup_insert_None. }
    { by apply (lookup_insert_None (delete γ f) γ1 γ2 true). }
241
    iNext. eapply internal_eq_rewrite_contractive; [by apply _| |by eauto].
Robbert Krebbers's avatar
Robbert Krebbers committed
242
243
    iNext. iRewrite "Heq". iPureIntro. by rewrite assoc (comm _ Q2).
  - iMod (slice_delete_empty with "Hslice Hbox") as (P') "[Heq Hbox]"; try done.
244
245
    iMod (slice_insert_empty with "Hbox") as (γ1) "(% & #Hslice1 & Hbox)".
    iMod (slice_insert_empty with "Hbox") as (γ2) "(% & #Hslice2 & Hbox)".
246
247
248
    iExists γ1, γ2. iFrame "%#". iModIntro. iSplit; last iSplit; try iPureIntro.
    { by eapply lookup_insert_None. }
    { by apply (lookup_insert_None (delete γ f) γ1 γ2 false). }
249
    iNext. eapply internal_eq_rewrite_contractive; [by apply _| |by eauto].
Robbert Krebbers's avatar
Robbert Krebbers committed
250
    iNext. iRewrite "Heq". iPureIntro. by rewrite assoc (comm _ Q2).
251
252
Qed.

253
Lemma slice_combine E q f P Q1 Q2 γ1 γ2 b :
254
  N  E  γ1  γ2  f !! γ1 = Some b  f !! γ2 = Some b 
255
  slice N γ1 Q1 - slice N γ2 Q2 - ?q box N f P ={E}=  γ,
256
    delete γ2 (delete γ1 f) !! γ = None  slice N γ (Q1  Q2) 
257
    ?q box N (<[γ := b]>(delete γ2 (delete γ1 f))) P.
258
259
Proof.
  iIntros (????) "#Hslice1 #Hslice2 Hbox". destruct b.
Robbert Krebbers's avatar
Robbert Krebbers committed
260
261
262
  - iMod (slice_delete_full with "Hslice1 Hbox") as (P1) "(HQ1 & Heq1 & Hbox)"; try done.
    iMod (slice_delete_full with "Hslice2 Hbox") as (P2) "(HQ2 & Heq2 & Hbox)"; first done.
    { by simplify_map_eq. }
263
    iMod (slice_insert_full _ _ _ _ (Q1  Q2)%I with "[$HQ1 $HQ2] Hbox")
Robbert Krebbers's avatar
Robbert Krebbers committed
264
      as (γ) "(% & #Hslice & Hbox)"; first done.
265
266
267
    iExists γ. iFrame "%#". iModIntro. iNext.
    eapply internal_eq_rewrite_contractive; [by apply _| |by eauto].
    iNext. iRewrite "Heq1". iRewrite "Heq2". by rewrite assoc.
Robbert Krebbers's avatar
Robbert Krebbers committed
268
269
270
  - iMod (slice_delete_empty with "Hslice1 Hbox") as (P1) "(Heq1 & Hbox)"; try done.
    iMod (slice_delete_empty with "Hslice2 Hbox") as (P2) "(Heq2 & Hbox)"; first done.
    { by simplify_map_eq. }
271
    iMod (slice_insert_empty with "Hbox") as (γ) "(% & #Hslice & Hbox)".
272
273
274
275
    iExists γ. iFrame "%#". iModIntro. iNext.
    eapply internal_eq_rewrite_contractive; [by apply _| |by eauto].
    iNext. iRewrite "Heq1". iRewrite "Heq2". by rewrite assoc.
Qed.
276
End box.
277

278
Typeclasses Opaque slice box.