boxes.v 13.2 KB
Newer Older
1
From iris.base_logic.lib Require Export invariants.
2
From iris.algebra Require Import auth gmap agree.
3
From iris.proofmode Require Import tactics.
4
Set Default Proof Using "Type".
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 14 15
Definition boxΣ : gFunctors := #[ GFunctor (authR (optionUR (exclR boolC)) *
                                            optionRF (agreeRF ( )) ) ].

16
Instance subG_stsΣ Σ : subG boxΣ Σ  boxG Σ.
17
Proof. solve_inG. Qed.
18

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

22
  Definition slice_name := gname.
23

Robbert Krebbers's avatar
Robbert Krebbers committed
24
  Definition box_own_auth (γ : slice_name) (a : auth (option (excl bool))) : iProp Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
25
    own γ (a, None).
26

27
  Definition box_own_prop (γ : slice_name) (P : iProp Σ) : iProp Σ :=
Robbert Krebbers's avatar
Robbert Krebbers committed
28
    own γ (ε, Some (to_agree (Next (iProp_unfold P)))).
29

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

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

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

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

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

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

57
Global Instance box_inv_ne γ : NonExpansive (slice_inv γ).
58
Proof. solve_proper. Qed.
59

60
Global Instance slice_ne γ : NonExpansive (slice N γ).
61
Proof. solve_proper. Qed.
62 63
Global Instance slice_contractive γ : Contractive (slice N γ).
Proof. solve_contractive. Qed.
64 65
Global Instance slice_proper γ : Proper (() ==> ()) (slice N γ).
Proof. apply ne_proper, _. Qed.
66

67
Global Instance slice_persistent γ P : Persistent (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
Proof. apply (contractive_ne _). Qed.
74 75
Global Instance box_proper f : Proper (() ==> ()) (box N f).
Proof. apply ne_proper, _. Qed.
76

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

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

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

101
Lemma box_alloc : box N  True%I.
102
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
103
  iIntros; iExists (λ _, True)%I; iSplit; last by auto.
104
  iNext. by rewrite big_opM_empty.
105 106
Qed.

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

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

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

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

181
Lemma slice_insert_full E q f P Q :
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
182
  N  E 
183 184
   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
185
Proof.
186
  iIntros (?) "HQ Hbox".
187
  iMod (slice_insert_empty with "Hbox") as (γ ?) "[#Hslice Hbox]".
Robbert Krebbers's avatar
Robbert Krebbers committed
188
  iExists γ. iFrame "%#". iMod (slice_fill with "Hslice HQ Hbox"); first done.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
189 190 191
  by apply lookup_insert. by rewrite insert_insert.
Qed.

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

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

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

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
244 245 246 247 248 249 250 251 252
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'".
253
    iMod (slice_insert_full with "HQ' Hb") as (γ' ?) "[#Hs' Hb]"; try done.
254
    iExists γ', _. iIntros "{$∗ $# $%} !>". do 2 iNext. iRewrite "Heq".
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
255 256
    iAlways. by iSplit; iIntros "[? $]"; iApply "HQQ'".
  - iMod (slice_delete_empty with "Hs Hb") as (P') "(Heq & Hb)"; try done.
257
    iMod (slice_insert_empty with "Hb") as (γ' ?) "[#Hs' Hb]"; try done.
258
    iExists γ', (Q'  P')%I. iIntros "{$∗ $# $%} !>".  do 2 iNext. iRewrite "Heq".
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
259 260 261
    iAlways. by iSplit; iIntros "[? $]"; iApply "HQQ'".
Qed.

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

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

312
Typeclasses Opaque slice box.