boxes.v 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
From iris.algebra Require Import upred_big_op.
From iris.program_logic Require Import auth saved_prop.
From iris.proofmode Require Import tactics invariants ghost_ownership.
Import uPred.

(** The CMRAs we need. *)
Class boxG Λ Σ :=
  boxG_inG :> inG Λ Σ (prodR
    (authR (optionUR (exclR boolC)))
    (optionR (agreeR (laterC (iPrePropG Λ Σ))))).

Section box_defs.
  Context `{boxG Λ Σ} (N : namespace).
  Notation iProp := (iPropG Λ Σ).

16
  Definition slice_name := gname.
17

18 19
  Definition box_own_auth (γ : slice_name) (a : auth (option (excl bool)))
    := own γ (a, (:option (agree (later (iPrePropG Λ Σ))))).
20 21

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

24
  Definition slice_inv (γ : slice_name) (P : iProp) : iProp :=
25 26
    ( b, box_own_auth γ ( Excl' b)  box_own_prop γ P  if b then P else True)%I.

27 28
  Definition slice (γ : slice_name) (P : iProp) : iProp :=
    inv N (slice_inv γ P).
29

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

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

43 44 45 46 47
Section box.
Context `{boxG Λ Σ} (N : namespace).
Notation iProp := (iPropG Λ Σ).
Implicit Types P Q : iProp.

48
Global Instance box_own_prop_ne n γ : Proper (dist n ==> dist n) (box_own_prop γ).
49
Proof. solve_proper. Qed.
50
Global Instance box_inv_ne n γ : Proper (dist n ==> dist n) (slice_inv γ).
51
Proof. solve_proper. Qed.
52
Global Instance slice_ne n γ : Proper (dist n ==> dist n) (slice N γ).
53 54 55
Proof. solve_proper. Qed.
Global Instance box_ne n f : Proper (dist n ==> dist n) (box N f).
Proof. solve_proper. Qed.
56
Global Instance slice_persistent γ P : PersistentP (slice N γ P).
57 58
Proof. apply _. Qed.

59
Lemma box_own_auth_agree γ b1 b2 :
60
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)  b1 = b2.
61 62
Proof.
  rewrite /box_own_prop -own_op own_valid prod_validI /= and_elim_l.
63
  by iDestruct 1 as % [[[] [=]%leibniz_equiv] ?]%auth_valid_discrete.
64 65 66
Qed.

Lemma box_own_auth_update E γ b1 b2 b3 :
67 68
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)
  ={E}=> box_own_auth γ ( Excl' b3)  box_own_auth γ ( Excl' b3).
69
Proof.
70 71
  rewrite /box_own_prop -!own_op own_valid_l prod_validI; iIntros "[[Hb _] Hγ]".
  iDestruct "Hb" as % [[[] [= ->]%leibniz_equiv] ?]%auth_valid_discrete.
72
  iApply (@own_update with "Hγ"); apply prod_update; simpl; last reflexivity.
73
  by apply auth_update_no_frame, option_local_update, exclusive_local_update.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
Qed.

Lemma box_own_agree γ Q1 Q2 :
  (box_own_prop γ Q1  box_own_prop γ Q2)   (Q1  Q2).
Proof.
  rewrite /box_own_prop -own_op own_valid prod_validI /= and_elim_r.
  rewrite option_validI /= agree_validI agree_equivI later_equivI /=.
  iIntros "#HQ >". rewrite -{2}(iProp_fold_unfold Q1).
  iRewrite "HQ". by rewrite iProp_fold_unfold.
Qed.

Lemma box_alloc : True  box N  True.
Proof.
  iIntros; iExists (λ _, True)%I; iSplit.
  - iNext. by rewrite big_sepM_empty.
  - by rewrite big_sepM_empty.
Qed.

Lemma box_insert f P Q :
93
   box N f P ={N}=>  γ, f !! γ = None 
94
    slice N γ Q   box N (<[γ:=false]> f) (Q  P).
95
Proof.
96
  iDestruct 1 as (Φ) "[#HeqP Hf]".
97 98
  iPvs (own_alloc_strong ( Excl' false   Excl' false,
    Some (to_agree (Next (iProp_unfold Q)))) _ (dom _ f))
99
    as (γ) "[Hdom Hγ]"; first done.
100 101
  rewrite pair_split. iDestruct "Hγ" as "[[Hγ Hγ'] #HγQ]".
  iDestruct "Hdom" as % ?%not_elem_of_dom.
102
  iPvs (inv_alloc N _ (slice_inv γ Q) with "[Hγ]") as "#Hinv"; first done.
103
  { iNext. iExists false; eauto. }
104 105 106 107
  iPvsIntro; iExists γ; repeat iSplit; auto.
  iNext. iExists (<[γ:=Q]> Φ); iSplit.
  - iNext. iRewrite "HeqP". by rewrite big_sepM_fn_insert'.
  - rewrite (big_sepM_fn_insert (λ _ _ P',  _  _ _ P'  _ _ (_ _ P')))%I //.
108
    iFrame; eauto.
109 110 111 112
Qed.

Lemma box_delete f P Q γ :
  f !! γ = Some false 
113
  slice N γ Q   box N f P ={N}=>  P',
114 115
      (P  (Q  P'))   box N (delete γ f) P'.
Proof.
116
  iIntros (?) "[#Hinv H]"; iDestruct "H" as (Φ) "[#HeqP Hf]".
117
  iExists ([ map] γ'_  delete γ f, Φ γ')%I.
118
  iInv N as (b) "(Hγ & #HγQ &_)"; iPvsIntro; iNext.
119 120
  iDestruct (big_sepM_delete _ f _ false with "Hf")
    as "[[Hγ' #[HγΦ ?]] ?]"; first done.
121
  iDestruct (box_own_agree γ Q (Φ γ) with "[#]") as "HeqQ"; first by eauto.
122
  iDestruct (box_own_auth_agree γ b false with "[#]")
123
    as "%"; subst; first by iFrame.
124
  iSplitL "Hγ"; last iSplit.
125
  - iExists false; eauto.
126
  - iNext. iRewrite "HeqP". iRewrite "HeqQ". by rewrite -big_sepM_delete.
127
  - iExists Φ; eauto.
128 129
Qed.

130 131
Lemma box_fill f γ P Q :
  f !! γ = Some false 
132
  slice N γ Q   Q   box N f P ={N}=>  box N (<[γ:=true]> f) P.
133
Proof.
134 135
  iIntros (?) "(#Hinv & HQ & H)"; iDestruct "H" as (Φ) "[#HeqP Hf]".
  iInv N as (b') "(Hγ & #HγQ & _)"; iTimeless "Hγ".
136
  iDestruct (big_sepM_later _ f with "Hf") as "Hf".
137
  iDestruct (big_sepM_delete _ f _ false with "Hf")
138
    as "[[Hγ' #[HγΦ Hinv']] ?]"; first done; iTimeless "Hγ'".
139
  iPvs (box_own_auth_update _ γ b' false true with "[Hγ Hγ']")
140
    as "[Hγ Hγ']"; first by iFrame.
141 142
  iPvsIntro; iNext; iSplitL "Hγ HQ"; first (iExists true; by iFrame "Hγ HQ").
  iExists Φ; iSplit.
143
  - by rewrite big_sepM_insert_override.
144
  - rewrite -insert_delete big_sepM_insert ?lookup_delete //.
145
    iFrame; eauto.
146 147 148 149
Qed.

Lemma box_empty f P Q γ :
  f !! γ = Some true 
150
  slice N γ Q   box N f P ={N}=>  Q   box N (<[γ:=false]> f) P.
151
Proof.
152 153
  iIntros (?) "[#Hinv H]"; iDestruct "H" as (Φ) "[#HeqP Hf]".
  iInv N as (b) "(Hγ & #HγQ & HQ)"; iTimeless "Hγ".
154
  iDestruct (big_sepM_later _ f with "Hf") as "Hf".
Ralf Jung's avatar
Ralf Jung committed
155
  iDestruct (big_sepM_delete _ f with "Hf")
156 157
    as "[[Hγ' #[HγΦ Hinv']] ?]"; first done; iTimeless "Hγ'".
  iDestruct (box_own_auth_agree γ b true with "[#]")
158
    as "%"; subst; first by iFrame.
159
  iFrame "HQ".
Ralf Jung's avatar
Ralf Jung committed
160
  iPvs (box_own_auth_update _ γ with "[Hγ Hγ']")
161
    as "[Hγ Hγ']"; first by iFrame.
162 163
  iPvsIntro; iNext; iSplitL "Hγ"; first (iExists false; by repeat iSplit).
  iExists Φ; iSplit.
164
  - by rewrite big_sepM_insert_override.
165
  - rewrite -insert_delete big_sepM_insert ?lookup_delete //.
166
    iFrame; eauto.
167 168
Qed.

169
Lemma box_fill_all f P Q : box N f P   P ={N}=> box N (const true <$> f) P.
170
Proof.
171
  iIntros "[H HP]"; iDestruct "H" as (Φ) "[#HeqP Hf]".
172 173 174 175 176
  iExists Φ; iSplitR; first by rewrite big_sepM_fmap.
  rewrite eq_iff later_iff big_sepM_later; iDestruct ("HeqP" with "HP") as "HP".
  iCombine "Hf" "HP" as "Hf".
  rewrite big_sepM_fmap; iApply (pvs_big_sepM _ _ f).
  iApply (big_sepM_impl _ _ f); iFrame "Hf".
177 178
  iAlways; iIntros (γ b' ?) "[(Hγ' & #$ & #$) HΦ]".
  iInv N as (b) "[Hγ _]"; iTimeless "Hγ".
Ralf Jung's avatar
Ralf Jung committed
179
  iPvs (box_own_auth_update _ γ with "[Hγ Hγ']")
180 181
    as "[Hγ $]"; first by iFrame.
  iPvsIntro; iNext; iExists true. by iFrame.
182 183 184 185
Qed.

Lemma box_empty_all f P Q :
  map_Forall (λ _, (true =)) f 
186
  box N f P ={N}=>  P  box N (const false <$> f) P.
187
Proof.
188
  iDestruct 1 as (Φ) "[#HeqP Hf]".
189
  iAssert ([ map] γ↦b  f,  Φ γ  box_own_auth γ ( Excl' false) 
190
    box_own_prop γ (Φ γ)  inv N (slice_inv γ (Φ γ)))%I with "|==>[Hf]" as "[HΦ ?]".
191
  { iApply (pvs_big_sepM _ _ f); iApply (big_sepM_impl _ _ f); iFrame "Hf".
192
    iAlways; iIntros (γ b ?) "(Hγ' & #$ & #$)".
193
    assert (true = b) as <- by eauto.
194
    iInv N as (b) "(Hγ & _ & HΦ)"; iTimeless "Hγ".
195
    iDestruct (box_own_auth_agree γ b true with "[#]")
196
      as "%"; subst; first by iFrame.
197
    iPvs (box_own_auth_update _ γ true true false with "[Hγ Hγ']")
198 199
      as "[Hγ $]"; first by iFrame.
    iPvsIntro; iNext. iFrame "HΦ". iExists false. iFrame; eauto. }
200 201 202 203 204
  iPvsIntro; iSplitL "HΦ".
  - rewrite eq_iff later_iff big_sepM_later. by iApply "HeqP".
  - iExists Φ; iSplit; by rewrite big_sepM_fmap.
Qed.
End box.
205

206
Typeclasses Opaque slice_name slice box.