boxes.v 13.2 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
Definition boxΣ : gFunctors := #[ GFunctor (authR (optionUR (exclR boolC)) *
                                            optionRF (agreeRF ( )) ) ].

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

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

23
  Definition slice_name := gname.
24

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Robbert Krebbers's avatar
Robbert Krebbers committed
220
Lemma box_empty E f P :
221
  N  E 
222
  map_Forall (λ _, (true =)) f 
223
  box N f P ={E}=  P  box N (const false <$> f) P.
224
Proof.
225
  iDestruct 1 as (Φ) "[#HeqP Hf]".
226 227 228 229
  iAssert (([ map] γ↦b  f,  Φ γ) 
    [ map] γ↦b  f, box_own_auth γ ( Excl' false)   box_own_prop γ (Φ γ) 
      inv N (slice_inv γ (Φ γ)))%I with "[> Hf]" as "[HΦ ?]".
  { rewrite -big_sepM_sepM -fupd_big_sepM. iApply (@big_sepM_impl with "[$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.