boxes.v 8.19 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
20
21
  Definition box_own_auth (γ : slice_name)
    (a : auth (option (excl bool))) : iProp := own γ (a, ).

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

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
49
(* FIXME: solve_proper picks the eq ==> instance for Next. *)
Global Instance box_own_prop_ne n γ : Proper (dist n ==> dist n) (box_own_prop γ).
50
Proof. solve_proper. Qed.
51
Global Instance box_inv_ne n γ : Proper (dist n ==> dist n) (slice_inv γ).
52
Proof. solve_proper. Qed.
53
Global Instance slice_ne n γ : Proper (dist n ==> dist n) (slice N γ).
54
55
56
Proof. solve_proper. Qed.
Global Instance box_ne n f : Proper (dist n ==> dist n) (box N f).
Proof. solve_proper. Qed.
57
Global Instance slice_persistent γ P : PersistentP (slice N γ P).
58
59
60
Proof. apply _. Qed.

(* This should go automatic *)
61
62
63
64
65
Instance box_own_auth_timeless γ
  (a : auth (option (excl bool))) : TimelessP (box_own_auth γ a).
Proof. apply own_timeless, pair_timeless; apply _. Qed.

Lemma box_own_auth_agree γ b1 b2 :
66
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)  b1 = b2.
67
68
69
70
71
72
73
Proof.
  rewrite /box_own_prop -own_op own_valid prod_validI /= and_elim_l.
  iIntros "Hb".
  by iDestruct "Hb" as % [[[] [=]%leibniz_equiv] ?]%auth_valid_discrete.
Qed.

Lemma box_own_auth_update E γ b1 b2 b3 :
74
75
  box_own_auth γ ( Excl' b1)  box_own_auth γ ( Excl' b2)
  ={E}=> box_own_auth γ ( Excl' b3)  box_own_auth γ ( Excl' b3).
76
77
78
Proof.
  rewrite /box_own_prop -!own_op.
  apply own_update, prod_update; simpl; last reflexivity.
79
  apply (auth_local_update (λ _, Excl' b3)). apply _. done.
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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 :
99
   box N f P ={N}=>  γ, f !! γ = None 
100
    slice N γ Q   box N (<[γ:=false]> f) (Q  P).
101
102
103
104
105
106
107
Proof.
  iIntros "H"; iDestruct "H" as {Φ} "[#HeqP Hf]".
  iPvs (own_alloc_strong ( Excl' false   Excl' false,
    Some (to_agree (Next (iProp_unfold Q)))) _ (dom _ f))
    as {γ} "[Hdom Hγ]"; first done.
  rewrite pair_split. iDestruct "Hγ" as "[[Hγ Hγ'] #HγQ]".
  iDestruct "Hdom" as % ?%not_elem_of_dom.
108
  iPvs (inv_alloc N _ (slice_inv γ Q) with "[Hγ]") as "#Hinv"; first done.
109
  { iNext. iExists false; eauto. }
110
111
112
113
  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 //.
114
    iFrame; eauto.
115
116
117
118
Qed.

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

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

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

175
Lemma box_fill_all f P Q : box N f P   P ={N}=> box N (const true <$> f) P.
176
177
178
179
180
181
182
183
184
Proof.
  iIntros "[H HP]"; iDestruct "H" as {Φ} "[#HeqP Hf]".
  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".
  iAlways; iIntros {γ b' ?} "[(Hγ' & #$ & #$) HΦ]".
  iInv N as {b} "[Hγ _]"; iTimeless "Hγ".
Ralf Jung's avatar
Ralf Jung committed
185
  iPvs (box_own_auth_update _ γ with "[Hγ Hγ']")
186
187
    as "[Hγ $]"; first by iFrame.
  iPvsIntro; iNext; iExists true. by iFrame.
188
189
190
191
Qed.

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

212
Typeclasses Opaque slice_name slice box.