boxes.v 12 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

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

17
  Definition slice_name := gname.
18

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

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

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

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

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

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

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

47
Global Instance box_own_prop_ne n γ : Proper (dist n ==> dist n) (box_own_prop γ).
48
Proof. solve_proper. Qed.
49 50 51
Global Instance box_own_prop_contractive γ : Contractive (box_own_prop γ).
Proof. solve_contractive. Qed.

52
Global Instance box_inv_ne n γ : Proper (dist n ==> dist n) (slice_inv γ).
53
Proof. solve_proper. Qed.
54

55
Global Instance slice_ne n γ : Proper (dist n ==> dist n) (slice N γ).
56
Proof. solve_proper. Qed.
57 58 59
Global Instance slice_contractive γ : Contractive (slice N γ).
Proof. solve_contractive. Qed.

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

63 64 65 66 67
Global Instance box_contractive f : Contractive (box N f).
Proof. solve_contractive. Qed.
Global Instance box_ne n f : Proper (dist n ==> dist n) (box N f).
Proof. apply (contractive_ne _). Qed.

68
Lemma box_own_auth_agree γ b1 b2 :
Ralf Jung's avatar
Ralf Jung committed
69
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)  b1 = b2.
70
Proof.
71
  rewrite /box_own_prop -own_op own_valid prod_validI /= and_elim_l.
72
  by iDestruct 1 as % [[[] [=]%leibniz_equiv] ?]%auth_valid_discrete.
73 74
Qed.

75
Lemma box_own_auth_update γ b1 b2 b3 :
76 77
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)
  == box_own_auth γ ( Excl' b3)  box_own_auth γ ( Excl' b3).
78
Proof.
79 80
  rewrite /box_own_auth -!own_op. apply own_update, prod_update; last done.
  by apply auth_update, option_local_update, exclusive_local_update.
81 82 83
Qed.

Lemma box_own_agree γ Q1 Q2 :
84
  box_own_prop γ Q1  box_own_prop γ Q2   (Q1  Q2).
85
Proof.
86
  rewrite /box_own_prop -own_op own_valid prod_validI /= and_elim_r.
87
  rewrite option_validI /= agree_validI agree_equivI later_equivI /=.
88
  iIntros "#HQ". iNext. rewrite -{2}(iProp_fold_unfold Q1).
89 90 91
  iRewrite "HQ". by rewrite iProp_fold_unfold.
Qed.

92
Lemma box_alloc : box N  True%I.
93 94 95 96 97 98
Proof.
  iIntros; iExists (λ _, True)%I; iSplit.
  - iNext. by rewrite big_sepM_empty.
  - by rewrite big_sepM_empty.
Qed.

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

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

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

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

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

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

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

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

235
Lemma slice_split E q f P Q1 Q2 γ b :
236
  N  E  f !! γ = Some b 
237
  slice N γ (Q1  Q2) - ?q box N f P ={E}=  γ1 γ2,
238
    delete γ f !! γ1 = None  delete γ f !! γ2 = None  ⌜γ1  γ2 
239
    slice N γ1 Q1  slice N γ2 Q2  ?q box N (<[γ2 := b]>(<[γ1 := b]>(delete γ f))) P.
240 241
Proof.
  iIntros (??) "#Hslice Hbox". destruct b.
Robbert Krebbers's avatar
Robbert Krebbers committed
242
  - iMod (slice_delete_full with "Hslice Hbox") as (P') "([HQ1 HQ2] & Heq & Hbox)"; try done.
243 244
    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.
245 246 247
    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). }
248
    iNext. eapply internal_eq_rewrite_contractive; [by apply _| |by eauto].
Robbert Krebbers's avatar
Robbert Krebbers committed
249 250
    iNext. iRewrite "Heq". iPureIntro. by rewrite assoc (comm _ Q2).
  - iMod (slice_delete_empty with "Hslice Hbox") as (P') "[Heq Hbox]"; try done.
251 252
    iMod (slice_insert_empty with "Hbox") as (γ1) "(% & #Hslice1 & Hbox)".
    iMod (slice_insert_empty with "Hbox") as (γ2) "(% & #Hslice2 & Hbox)".
253 254 255
    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). }
256
    iNext. eapply internal_eq_rewrite_contractive; [by apply _| |by eauto].
Robbert Krebbers's avatar
Robbert Krebbers committed
257
    iNext. iRewrite "Heq". iPureIntro. by rewrite assoc (comm _ Q2).
258 259
Qed.

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

285
Typeclasses Opaque slice box.