boxes.v 8.04 KB
Newer Older
1 2
From iris.program_logic Require Export pviewshifts.
From iris.algebra Require Import auth gmap agree upred_big_op.
3
From iris.proofmode Require Import tactics invariants.
4 5 6
Import uPred.

(** The CMRAs we need. *)
7 8
Class boxG Σ :=
  boxG_inG :> inG Σ (prodR
9
    (authR (optionUR (exclR boolC)))
10
    (optionR (agreeR (laterC (iPreProp Σ))))).
11 12

Section box_defs.
13
  Context `{irisG Λ Σ, boxG Σ} (N : namespace).
14

15
  Definition slice_name := gname.
16

17
  Definition box_own_auth (γ : slice_name) (a : auth (option (excl bool)))
18
    := own γ (a, (:option (agree (later (iPreProp Σ))))).
19

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

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

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

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

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

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

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

57
Lemma box_own_auth_agree γ b1 b2 :
58
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)  b1 = b2.
59
Proof.
60
  rewrite /box_own_prop own_valid_2 prod_validI /= and_elim_l.
61
  by iDestruct 1 as % [[[] [=]%leibniz_equiv] ?]%auth_valid_discrete.
62 63
Qed.

64
Lemma box_own_auth_update γ b1 b2 b3 :
65
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)
66
  =r=> box_own_auth γ ( Excl' b3)  box_own_auth γ ( Excl' b3).
67
Proof.
68 69
  rewrite /box_own_prop -!own_op own_valid_l prod_validI; iIntros "[[Hb _] Hγ]".
  iDestruct "Hb" as % [[[] [= ->]%leibniz_equiv] ?]%auth_valid_discrete.
70
  iApply (own_update with "Hγ"); apply prod_update; simpl; last reflexivity.
71
  by apply auth_update_no_frame, option_local_update, exclusive_local_update.
72 73 74 75 76
Qed.

Lemma box_own_agree γ Q1 Q2 :
  (box_own_prop γ Q1  box_own_prop γ Q2)   (Q1  Q2).
Proof.
77
  rewrite /box_own_prop own_valid_2 prod_validI /= and_elim_r.
78
  rewrite option_validI /= agree_validI agree_equivI later_equivI /=.
Robbert Krebbers's avatar
Robbert Krebbers committed
79
  iIntros "#HQ !>". rewrite -{2}(iProp_fold_unfold Q1).
80 81 82 83 84 85 86 87 88 89 90
  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 :
91
   box N f P ={N}=>  γ, f !! γ = None 
92
    slice N γ Q   box N (<[γ:=false]> f) (Q  P).
93
Proof.
94
  iDestruct 1 as (Φ) "[#HeqP Hf]".
95 96
  iVs (own_alloc_strong ( Excl' false   Excl' false,
    Some (to_agree (Next (iProp_unfold Q)))) (dom _ f))
97
    as (γ) "[Hdom Hγ]"; first done.
98 99
  rewrite pair_split. iDestruct "Hγ" as "[[Hγ Hγ'] #HγQ]".
  iDestruct "Hdom" as % ?%not_elem_of_dom.
100
  iVs (inv_alloc N _ (slice_inv γ Q) with "[Hγ]") as "#Hinv".
101
  { iNext. iExists false; eauto. }
102
  iVsIntro; iExists γ; repeat iSplit; auto.
103
  iNext. iExists (<[γ:=Q]> Φ); iSplit.
104 105
  - iNext. iRewrite "HeqP". by rewrite big_sepM_fn_insert'.
  - rewrite (big_sepM_fn_insert (λ _ _ P',  _  _ _ P'  _ _ (_ _ P')))%I //.
106
    iFrame; eauto.
107 108 109 110
Qed.

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

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

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

165
Lemma box_fill_all f P Q : box N f P   P ={N}=> box N (const true <$> f) P.
166
Proof.
167
  iIntros "[H HP]"; iDestruct "H" as (Φ) "[#HeqP Hf]".
168 169 170 171 172
  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".
173
  iAlways; iIntros (γ b' ?) "[(Hγ' & #$ & #$) HΦ]".
Robbert Krebbers's avatar
Robbert Krebbers committed
174
  iInv N as (b) "[>Hγ _]" "Hclose".
175 176
  iVs (box_own_auth_update γ with "[Hγ Hγ']") as "[Hγ $]"; first by iFrame.
  iApply "Hclose". iNext; iExists true. by iFrame.
177 178 179 180
Qed.

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

201
Typeclasses Opaque slice_name slice box.