boxes.v 13.2 KB
Newer Older
1
From iris.base_logic.lib Require Export invariants.
2
From iris.algebra Require Import excl 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 ( )) ) ].

Robbert Krebbers's avatar
Robbert Krebbers committed
16
Instance subG_boxΣ Σ : 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_2.
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.
103
  iIntros. iExists (λ _, True)%I. iSplit; by auto.
104 105
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]".
Dan Frumin's avatar
Dan Frumin committed
111
  iMod (own_alloc_cofinite ( Excl' false   Excl' false,
112
    Some (to_agree (Next (iProp_unfold Q)))) (dom _ f))
113
    as (γ) "[Hdom Hγ]"; first by (split; [apply auth_valid_discrete_2|]).
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 121
  - iNext. iRewrite "HeqP". by rewrite big_opM_fn_insert'.
  - rewrite (big_opM_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
  slice N γ Q - ?q box N f P ={E}=  P',
129
    ?q ( (P  (Q  P'))  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.
Ralf Jung's avatar
Ralf Jung committed
133
  iInv N as (b) "[>Hγ _]".
134
  iDestruct (big_opM_delete _ f _ false with "Hf")
135
    as "[[>Hγ' #[HγΦ ?]] ?]"; first done.
136
  iDestruct (box_own_auth_agree γ b false with "[-]") as %->; first by iFrame.
Ralf Jung's avatar
Ralf Jung committed
137
  iModIntro. iSplitL "Hγ"; first iExists false; eauto.
138 139
  iModIntro. iNext. iSplit.
  - iDestruct (box_own_agree γ Q (Φ γ) with "[#]") as "HeqQ"; first by eauto.
140
    iNext. iRewrite "HeqP". iRewrite "HeqQ". by rewrite -big_opM_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
  iIntros (??) "#[HγQ Hinv] HQ H"; iDestruct "H" as (Φ) "[#HeqP Hf]".
Ralf Jung's avatar
Ralf Jung committed
150
  iInv N as (b') "[>Hγ _]".
151
  iDestruct (big_opM_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γ']".
Ralf Jung's avatar
Ralf Jung committed
154
  iModIntro. iSplitL "Hγ HQ"; first (iNext; iExists true; by iFrame).
155
  iModIntro; iNext; iExists Φ; iSplit.
156 157
  - by rewrite big_opM_insert_override.
  - rewrite -insert_delete big_opM_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
  iIntros (??) "#[HγQ Hinv] H"; iDestruct "H" as (Φ) "[#HeqP Hf]".
Ralf Jung's avatar
Ralf Jung committed
167
  iInv N as (b) "[>Hγ HQ]".
168
  iDestruct (big_opM_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γ']".
Ralf Jung's avatar
Ralf Jung committed
173
  iModIntro. iSplitL "Hγ"; first (iNext; iExists false; by repeat iSplit).
174
  iModIntro; iNext; iExists Φ; iSplit.
175 176
  - by rewrite big_opM_insert_override.
  - rewrite -insert_delete big_opM_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".
186
  iMod (slice_insert_empty with "Hbox") as (γ ?) "[#Hslice Hbox]".
Robbert Krebbers's avatar
Robbert Krebbers committed
187
  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_opM_fmap.
210
  iEval (rewrite internal_eq_iff later_iff big_sepM_later) in "HeqP".
211
  iDestruct ("HeqP" with "HP") as "HP".
212
  iCombine "Hf" "HP" as "Hf".
213
  rewrite -big_sepM_sep big_opM_fmap; iApply (fupd_big_sepM _ _ f).
Robbert Krebbers's avatar
Robbert Krebbers committed
214
  iApply (@big_sepM_impl with "Hf").
215
  iIntros "!#" (γ b' ?) "[(Hγ' & #$ & #$) HΦ]".
Ralf Jung's avatar
Ralf Jung committed
216
  iInv N as (b) "[>Hγ _]".
217
  iMod (box_own_auth_update γ with "[Hγ Hγ']") as "[Hγ $]"; first by iFrame.
Ralf Jung's avatar
Ralf Jung committed
218
  iModIntro. iSplitL; last done. iNext; iExists true. 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 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Φ ?]".
230
  { rewrite -big_sepM_sep -fupd_big_sepM. iApply (@big_sepM_impl with "[$Hf]").
231
    iIntros "!#" (γ b ?) "(Hγ' & #HγΦ & #Hinv)".
232
    assert (true = b) as <- by eauto.
Ralf Jung's avatar
Ralf Jung committed
233
    iInv N as (b) "[>Hγ HΦ]".
234
    iDestruct (box_own_auth_agree γ b true with "[-]") as %->; first by iFrame.
235
    iMod (box_own_auth_update γ true true false with "[$Hγ $Hγ']") as "[Hγ $]".
Ralf Jung's avatar
Ralf Jung committed
236
    iModIntro. iSplitL "Hγ"; first (iNext; iExists false; iFrame; eauto).
Robbert Krebbers's avatar
Robbert Krebbers committed
237
    iFrame "HγΦ Hinv". by iApply "HΦ". }
238
  iModIntro; iSplitL "HΦ".
Robbert Krebbers's avatar
Robbert Krebbers committed
239
  - rewrite internal_eq_iff later_iff big_sepM_later. by iApply "HeqP".
240
  - iExists Φ; iSplit; by rewrite big_opM_fmap.
241
Qed.
242

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

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

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

311
Typeclasses Opaque slice box.