boxes.v 13.1 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
Set Default Proof Using "Type".
6 7 8
Import uPred.

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

14 15 16 17 18
Definition boxΣ : gFunctors := #[ GFunctor (authR (optionUR (exclR boolC)) *
                                            optionRF (agreeRF ( )) ) ].

Instance subG_stsΣ Σ :
  subG boxΣ Σ  boxG Σ.
19
Proof. solve_inG. Qed.
20

21
Section box_defs.
22
  Context `{invG Σ, boxG Σ} (N : namespace).
23

24
  Definition slice_name := gname.
25

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

29
  Definition box_own_prop (γ : slice_name) (P : iProp Σ) : iProp Σ :=
30
    own γ (:auth (option (excl bool)), Some (to_agree (Next (iProp_unfold P)))).
31

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

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

38 39
  Definition box (f : gmap slice_name bool) (P : iProp Σ) : iProp Σ :=
    ( Φ : slice_name  iProp Σ,
Robbert Krebbers's avatar
Robbert Krebbers committed
40
       (P  [ map] γ  _  f, Φ γ) 
41
      [ map] γ  b  f, box_own_auth γ ( Excl' b)  box_own_prop γ (Φ γ) 
42
                         inv N (slice_inv γ (Φ γ)))%I.
43 44
End box_defs.

45 46 47 48
Instance: Params (@box_own_prop) 3.
Instance: Params (@slice_inv) 3.
Instance: Params (@slice) 5.
Instance: Params (@box) 5.
49

50
Section box.
51
Context `{invG Σ, boxG Σ} (N : namespace).
52
Implicit Types P Q : iProp Σ.
53

54
Global Instance box_own_prop_ne γ : NonExpansive (box_own_prop γ).
55
Proof. solve_proper. Qed.
56 57 58
Global Instance box_own_prop_contractive γ : Contractive (box_own_prop γ).
Proof. solve_contractive. Qed.

59
Global Instance box_inv_ne γ : NonExpansive (slice_inv γ).
60
Proof. solve_proper. Qed.
61

62
Global Instance slice_ne γ : NonExpansive (slice N γ).
63
Proof. solve_proper. Qed.
64 65 66
Global Instance slice_contractive γ : Contractive (slice N γ).
Proof. solve_contractive. Qed.

67
Global Instance slice_persistent γ P : PersistentP (slice N γ P).
68 69
Proof. apply _. Qed.

70 71
Global Instance box_contractive f : Contractive (box N f).
Proof. solve_contractive. Qed.
72
Global Instance box_ne f : NonExpansive (box N f).
73 74
Proof. apply (contractive_ne _). Qed.

75
Lemma box_own_auth_agree γ b1 b2 :
Ralf Jung's avatar
Ralf Jung committed
76
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)  b1 = b2.
77
Proof.
78
  rewrite /box_own_prop -own_op own_valid prod_validI /= and_elim_l.
79
  by iDestruct 1 as % [[[] [=]%leibniz_equiv] ?]%auth_valid_discrete.
80 81
Qed.

82
Lemma box_own_auth_update γ b1 b2 b3 :
83 84
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)
  == box_own_auth γ ( Excl' b3)  box_own_auth γ ( Excl' b3).
85
Proof.
86 87
  rewrite /box_own_auth -!own_op. apply own_update, prod_update; last done.
  by apply auth_update, option_local_update, exclusive_local_update.
88 89 90
Qed.

Lemma box_own_agree γ Q1 Q2 :
91
  box_own_prop γ Q1  box_own_prop γ Q2   (Q1  Q2).
92
Proof.
93
  rewrite /box_own_prop -own_op own_valid prod_validI /= and_elim_r.
94
  rewrite option_validI /= agree_validI agree_equivI later_equivI /=.
95
  iIntros "#HQ". iNext. rewrite -{2}(iProp_fold_unfold Q1).
96 97 98
  iRewrite "HQ". by rewrite iProp_fold_unfold.
Qed.

99
Lemma box_alloc : box N  True%I.
100 101 102 103 104 105
Proof.
  iIntros; iExists (λ _, True)%I; iSplit.
  - iNext. by rewrite big_sepM_empty.
  - by rewrite big_sepM_empty.
Qed.

106 107 108
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).
109
Proof.
110
  iDestruct 1 as (Φ) "[#HeqP Hf]".
111
  iMod (own_alloc_strong ( Excl' false   Excl' false,
112
    Some (to_agree (Next (iProp_unfold Q)))) (dom _ f))
113
    as (γ) "[Hdom Hγ]"; first done.
114 115
  rewrite pair_split. iDestruct "Hγ" as "[[Hγ Hγ'] #HγQ]".
  iDestruct "Hdom" as % ?%not_elem_of_dom.
116
  iMod (inv_alloc N _ (slice_inv γ Q) with "[Hγ]") as "#Hinv".
117
  { iNext. iExists false; eauto. }
118
  iModIntro; iExists γ; repeat iSplit; auto.
119
  iNext. iExists (<[γ:=Q]> Φ); iSplit.
120
  - iNext. iRewrite "HeqP". by rewrite big_sepM_fn_insert'.
121
  - rewrite (big_sepM_fn_insert (λ _ _ P',  _  _ _ P'  _ _ (_ _ P')))%I //.
122
    iFrame; eauto.
123 124
Qed.

125
Lemma slice_delete_empty E q f P Q γ :
126
  N  E 
127
  f !! γ = Some false 
128 129
  slice N γ Q - ?q box N f P ={E}=  P',
    ?q  (P  (Q  P'))  ?q box N (delete γ f) P'.
130
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
131
  iIntros (??) "[#HγQ Hinv] H". iDestruct "H" as (Φ) "[#HeqP Hf]".
132
  iExists ([ map] γ'_  delete γ f, Φ γ')%I.
133
  iInv N as (b) "[>Hγ _]" "Hclose".
134
  iDestruct (big_sepM_delete _ f _ false with "Hf")
135
    as "[[>Hγ' #[HγΦ ?]] ?]"; first done.
136
  iDestruct (box_own_auth_agree γ b false with "[-]") as %->; first by iFrame.
137 138 139 140
  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.
141
  - iExists Φ; eauto.
142 143
Qed.

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

161
Lemma slice_empty E q f P Q γ :
162
  N  E 
163
  f !! γ = Some true 
164
  slice N γ Q - ?q box N f P ={E}=  Q  ?q box N (<[γ:=false]> f) P.
165
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
166 167
  iIntros (??) "#[HγQ Hinv] H"; iDestruct "H" as (Φ) "[#HeqP Hf]".
  iInv N as (b) "[>Hγ HQ]" "Hclose".
Ralf Jung's avatar
Ralf Jung committed
168
  iDestruct (big_sepM_delete _ f with "Hf")
Robbert Krebbers's avatar
Robbert Krebbers committed
169
    as "[[>Hγ' #[HγΦ Hinv']] ?]"; first done.
170
  iDestruct (box_own_auth_agree γ b true with "[-]") as %->; first by iFrame.
171
  iFrame "HQ".
172
  iMod (box_own_auth_update γ with "[$Hγ $Hγ']") as "[Hγ Hγ']".
173 174
  iMod ("Hclose" with "[Hγ]"); first (iNext; iExists false; by repeat iSplit).
  iModIntro; iNext; iExists Φ; iSplit.
175
  - by rewrite big_sepM_insert_override.
176
  - rewrite -insert_delete big_sepM_insert ?lookup_delete //.
177
    iFrame; eauto.
178 179
Qed.

180
Lemma slice_insert_full E q f P Q :
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
181
  N  E 
182 183
   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
184
Proof.
185
  iIntros (?) "HQ Hbox".
Robbert Krebbers's avatar
Robbert Krebbers committed
186 187
  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
188 189 190
  by apply lookup_insert. by rewrite insert_insert.
Qed.

191
Lemma slice_delete_full E q f P Q γ :
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
192 193
  N  E 
  f !! γ = Some true 
194 195
  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
196
Proof.
197
  iIntros (??) "#Hslice Hbox".
Robbert Krebbers's avatar
Robbert Krebbers committed
198 199 200
  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. }
201
  iExists P'. iFrame. rewrite -insert_delete delete_insert ?lookup_delete //.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
202 203
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
204
Lemma box_fill E f P :
205
  N  E 
206
  box N f P -  P ={E}= box N (const true <$> f) P.
207
Proof.
208
  iIntros (?) "H HP"; iDestruct "H" as (Φ) "[#HeqP Hf]".
209
  iExists Φ; iSplitR; first by rewrite big_sepM_fmap.
210 211
  rewrite internal_eq_iff later_iff big_sepM_later.
  iDestruct ("HeqP" with "HP") as "HP".
212
  iCombine "Hf" "HP" as "Hf".
213
  rewrite big_sepM_fmap; iApply (fupd_big_sepM _ _ f).
214
  iApply (big_sepM_impl _ _ f); iFrame "Hf".
215
  iAlways; iIntros (γ b' ?) "[(Hγ' & #$ & #$) HΦ]".
Robbert Krebbers's avatar
Robbert Krebbers committed
216
  iInv N as (b) "[>Hγ _]" "Hclose".
217
  iMod (box_own_auth_update γ with "[Hγ Hγ']") as "[Hγ $]"; first by iFrame.
218
  iApply "Hclose". iNext; iExists true. by iFrame.
219 220
Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
221
Lemma box_empty E f P :
222
  N  E 
223
  map_Forall (λ _, (true =)) f 
224
  box N f P ={E}=  P  box N (const false <$> f) P.
225
Proof.
226
  iDestruct 1 as (Φ) "[#HeqP Hf]".
227 228
  iAssert ([ map] γ↦b  f,  Φ γ  box_own_auth γ ( Excl' false) 
    box_own_prop γ (Φ γ)  inv N (slice_inv γ (Φ γ)))%I with ">[Hf]" as "[HΦ ?]".
229
  { iApply (fupd_big_sepM _ _ f); iApply (big_sepM_impl _ _ f); iFrame "Hf".
Robbert Krebbers's avatar
Robbert Krebbers committed
230
    iAlways; iIntros (γ b ?) "(Hγ' & #HγΦ & #Hinv)".
231
    assert (true = b) as <- by eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
232
    iInv N as (b) "[>Hγ HΦ]" "Hclose".
233
    iDestruct (box_own_auth_agree γ b true with "[-]") as %->; first by iFrame.
234
    iMod (box_own_auth_update γ true true false with "[$Hγ $Hγ']") as "[Hγ $]".
235
    iMod ("Hclose" with "[Hγ]"); first (iNext; iExists false; iFrame; eauto).
Robbert Krebbers's avatar
Robbert Krebbers committed
236
    iFrame "HγΦ Hinv". by iApply "HΦ". }
237
  iModIntro; iSplitL "HΦ".
238
  - rewrite internal_eq_iff later_iff big_sepM_later. by iApply "HeqP".
239 240
  - iExists Φ; iSplit; by rewrite big_sepM_fmap.
Qed.
241

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
Lemma slice_iff E q f P Q Q' γ b :
  N  E  f !! γ = Some b 
    (Q  Q') - slice N γ Q - ?q box N f P ={E}=  γ' P',
    delete γ f !! γ' = None  ?q   (P  P') 
    slice N γ' Q'  ?q box N (<[γ' := b]>(delete γ f)) P'.
Proof.
  iIntros (??) "#HQQ' #Hs Hb". destruct b.
  - iMod (slice_delete_full with "Hs Hb") as (P') "(HQ & Heq & Hb)"; try done.
    iDestruct ("HQQ'" with "HQ") as "HQ'".
    iMod (slice_insert_full with "HQ' Hb") as (γ') "(% & #Hs' & Hb)"; try done.
    iExists γ', _. iFrame "∗#%". iIntros "!>". do 2 iNext. iRewrite "Heq".
    iAlways. by iSplit; iIntros "[? $]"; iApply "HQQ'".
  - iMod (slice_delete_empty with "Hs Hb") as (P') "(Heq & Hb)"; try done.
    iMod (slice_insert_empty with "Hb") as (γ') "(% & #Hs' & Hb)"; try done.
    iExists γ', _. iFrame "∗#%". iIntros "!>". do 2 iNext. iRewrite "Heq".
    iAlways. by iSplit; iIntros "[? $]"; iApply "HQQ'".
Qed.

260
Lemma slice_split E q f P Q1 Q2 γ b :
261
  N  E  f !! γ = Some b 
262
  slice N γ (Q1  Q2) - ?q box N f P ={E}=  γ1 γ2,
263
    delete γ f !! γ1 = None  delete γ f !! γ2 = None  ⌜γ1  γ2 
264
    slice N γ1 Q1  slice N γ2 Q2  ?q box N (<[γ2 := b]>(<[γ1 := b]>(delete γ f))) P.
265 266
Proof.
  iIntros (??) "#Hslice Hbox". destruct b.
Robbert Krebbers's avatar
Robbert Krebbers committed
267
  - iMod (slice_delete_full with "Hslice Hbox") as (P') "([HQ1 HQ2] & Heq & Hbox)"; try done.
268 269
    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.
270 271 272
    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). }
273
    iNext. eapply internal_eq_rewrite_contractive; [by apply _| |by eauto].
Robbert Krebbers's avatar
Robbert Krebbers committed
274 275
    iNext. iRewrite "Heq". iPureIntro. by rewrite assoc (comm _ Q2).
  - iMod (slice_delete_empty with "Hslice Hbox") as (P') "[Heq Hbox]"; try done.
276 277
    iMod (slice_insert_empty with "Hbox") as (γ1) "(% & #Hslice1 & Hbox)".
    iMod (slice_insert_empty with "Hbox") as (γ2) "(% & #Hslice2 & Hbox)".
278 279 280
    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). }
281
    iNext. eapply internal_eq_rewrite_contractive; [by apply _| |by eauto].
Robbert Krebbers's avatar
Robbert Krebbers committed
282
    iNext. iRewrite "Heq". iPureIntro. by rewrite assoc (comm _ Q2).
283 284
Qed.

285
Lemma slice_combine E q f P Q1 Q2 γ1 γ2 b :
286
  N  E  γ1  γ2  f !! γ1 = Some b  f !! γ2 = Some b 
287
  slice N γ1 Q1 - slice N γ2 Q2 - ?q box N f P ={E}=  γ,
288
    delete γ2 (delete γ1 f) !! γ = None  slice N γ (Q1  Q2) 
289
    ?q box N (<[γ := b]>(delete γ2 (delete γ1 f))) P.
290 291
Proof.
  iIntros (????) "#Hslice1 #Hslice2 Hbox". destruct b.
Robbert Krebbers's avatar
Robbert Krebbers committed
292 293 294
  - 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. }
295
    iMod (slice_insert_full _ _ _ _ (Q1  Q2)%I with "[$HQ1 $HQ2] Hbox")
Robbert Krebbers's avatar
Robbert Krebbers committed
296
      as (γ) "(% & #Hslice & Hbox)"; first done.
297 298 299
    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
300 301 302
  - 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. }
303
    iMod (slice_insert_empty with "Hbox") as (γ) "(% & #Hslice & Hbox)".
304 305 306 307
    iExists γ. iFrame "%#". iModIntro. iNext.
    eapply internal_eq_rewrite_contractive; [by apply _| |by eauto].
    iNext. iRewrite "Heq1". iRewrite "Heq2". by rewrite assoc.
Qed.
308
End box.
309

310
Typeclasses Opaque slice box.