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
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
Proof.
100 101
  iIntros; iExists (λ _, True)%I; iSplit; last done.
  iNext. by rewrite big_opM_empty.
102 103
Qed.

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

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

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

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

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

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

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

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

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
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.

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

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

309
Typeclasses Opaque slice box.