fancy_updates.v 11 KB
Newer Older
1
From iris.base_logic.lib Require Export own.
Ralf Jung's avatar
Ralf Jung committed
2
From stdpp Require Export coPset.
3
From iris.base_logic.lib Require Import wsat.
4
From iris.algebra Require Import gmap.
5
From iris.proofmode Require Import tactics classes.
6
Set Default Proof Using "Type".
7
Export invG.
8 9
Import uPred.

10
Program Definition fupd_def `{invG Σ}
11
    (E1 E2 : coPset) (P : iProp Σ) : iProp Σ :=
12
  (wsat  ownE E1 ==  (wsat  ownE E2  P))%I.
13 14 15
Definition fupd_aux : seal (@fupd_def). by eexists. Qed.
Definition fupd := unseal fupd_aux.
Definition fupd_eq : @fupd = @fupd_def := seal_eq fupd_aux.
16 17
Arguments fupd {_ _} _ _ _%I.
Instance: Params (@fupd) 4.
18 19 20

Notation "|={ E1 , E2 }=> Q" := (fupd E1 E2 Q)
  (at level 99, E1, E2 at level 50, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
21
   format "|={ E1 , E2 }=>  Q") : bi_scope.
22
Notation "P ={ E1 , E2 }=∗ Q" := (P - |={E1,E2}=> Q)%I
23
  (at level 99, E1,E2 at level 50, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
24
   format "P  ={ E1 , E2 }=∗  Q") : bi_scope.
25
Notation "P ={ E1 , E2 }=∗ Q" := (P - |={E1,E2}=> Q)
Robbert Krebbers's avatar
Robbert Krebbers committed
26
  (at level 99, E1, E2 at level 50, Q at level 200, only parsing) : stdpp_scope.
27 28 29

Notation "|={ E }=> Q" := (fupd E E Q)
  (at level 99, E at level 50, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
30
   format "|={ E }=>  Q") : bi_scope.
31
Notation "P ={ E }=∗ Q" := (P - |={E}=> Q)%I
32
  (at level 99, E at level 50, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
33
   format "P  ={ E }=∗  Q") : bi_scope.
34
Notation "P ={ E }=∗ Q" := (P - |={E}=> Q)
Robbert Krebbers's avatar
Robbert Krebbers committed
35
  (at level 99, E at level 50, Q at level 200, only parsing) : stdpp_scope.
36 37

Section fupd.
38
Context `{invG Σ}.
39 40
Implicit Types P Q : iProp Σ.

41
Global Instance fupd_ne E1 E2 : NonExpansive (@fupd Σ _ E1 E2).
42
Proof. rewrite fupd_eq. solve_proper. Qed.
43
Global Instance fupd_proper E1 E2 : Proper (() ==> ()) (@fupd Σ _ E1 E2).
44 45 46 47 48
Proof. apply ne_proper, _. Qed.

Lemma fupd_intro_mask E1 E2 P : E2  E1  P  |={E1,E2}=> |={E2,E1}=> P.
Proof.
  intros (E1''&->&?)%subseteq_disjoint_union_L.
49 50
  rewrite fupd_eq /fupd_def ownE_op //.
  by iIntros "$ ($ & $ & HE) !> !> [$ $] !> !>" .
51 52
Qed.

53
Lemma except_0_fupd E1 E2 P :  (|={E1,E2}=> P) ={E1,E2}= P.
54
Proof. rewrite fupd_eq. iIntros ">H [Hw HE]". iApply "H"; by iFrame. Qed.
55

56
Lemma bupd_fupd E P : (|==> P) ={E}= P.
57
Proof. rewrite fupd_eq /fupd_def. by iIntros ">? [$ $] !> !>". Qed.
58

59
Lemma fupd_mono E1 E2 P Q : (P  Q)  (|={E1,E2}=> P)  |={E1,E2}=> Q.
60 61 62 63 64
Proof.
  rewrite fupd_eq /fupd_def. iIntros (HPQ) "HP HwE".
  rewrite -HPQ. by iApply "HP".
Qed.

65
Lemma fupd_trans E1 E2 E3 P : (|={E1,E2}=> |={E2,E3}=> P)  |={E1,E3}=> P.
66 67
Proof.
  rewrite fupd_eq /fupd_def. iIntros "HP HwE".
68
  iMod ("HP" with "HwE") as ">(Hw & HE & HP)". iApply "HP"; by iFrame.
69 70 71
Qed.

Lemma fupd_mask_frame_r' E1 E2 Ef P :
72
  E1 ## Ef  (|={E1,E2}=> E2 ## Ef  P) ={E1  Ef,E2  Ef}= P.
73 74
Proof.
  intros. rewrite fupd_eq /fupd_def ownE_op //. iIntros "Hvs (Hw & HE1 &HEf)".
75
  iMod ("Hvs" with "[Hw HE1]") as ">($ & HE2 & HP)"; first by iFrame.
76
  iDestruct (ownE_op' with "[HE2 HEf]") as "[? $]"; first by iFrame.
77
  iIntros "!> !>". by iApply "HP".
78 79
Qed.

80
Lemma fupd_frame_r E1 E2 P Q : (|={E1,E2}=> P)  Q ={E1,E2}= P  Q.
81 82
Proof. rewrite fupd_eq /fupd_def. by iIntros "[HwP $]". Qed.

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
Lemma fupd_plain' E1 E2 E2' P Q `{!Plain P} :
  E1  E2 
  (Q ={E1, E2'}= P) - (|={E1, E2}=> Q) ={E1}= (|={E1, E2}=> Q)  P.
Proof.
  iIntros ((E3&->&HE)%subseteq_disjoint_union_L) "HQP HQ".
  rewrite fupd_eq /fupd_def ownE_op //. iIntros "H".
  iMod ("HQ" with "H") as ">(Hws & [HE1 HE3] & HQ)"; iModIntro.
  iAssert ( P)%I as "#HP".
  { by iMod ("HQP" with "HQ [$]") as "(_ & _ & HP)". }
  iMod "HP". iFrame. auto.
Qed.

Lemma later_fupd_plain E P `{!Plain P} : ( |={E}=> P) ={E}=   P.
Proof.
  rewrite fupd_eq /fupd_def. iIntros "HP [Hw HE]".
  iAssert (  P)%I with "[-]" as "#$"; last by iFrame.
  iNext. by iMod ("HP" with "[$]") as "(_ & _ & HP)".
Qed.

102
(** * Derived rules *)
103
Global Instance fupd_mono' E1 E2 : Proper (() ==> ()) (@fupd Σ _ E1 E2).
104 105
Proof. intros P Q; apply fupd_mono. Qed.
Global Instance fupd_flip_mono' E1 E2 :
106
  Proper (flip () ==> flip ()) (@fupd Σ _ E1 E2).
107 108
Proof. intros P Q; apply fupd_mono. Qed.

109
Lemma fupd_intro E P : P ={E}= P.
110
Proof. iIntros "HP". by iApply bupd_fupd. Qed.
111
Lemma fupd_intro_mask' E1 E2 : E2  E1  (|={E1,E2}=> |={E2,E1}=> True)%I.
112
Proof. exact: fupd_intro_mask. Qed.
113
Lemma fupd_except_0 E1 E2 P : (|={E1,E2}=>  P) ={E1,E2}= P.
114
Proof. by rewrite {1}(fupd_intro E2 P) except_0_fupd fupd_trans. Qed.
115

116
Lemma fupd_frame_l E1 E2 P Q : (P  |={E1,E2}=> Q) ={E1,E2}= P  Q.
117
Proof. rewrite !(comm _ P); apply fupd_frame_r. Qed.
118
Lemma fupd_wand_l E1 E2 P Q : (P - Q)  (|={E1,E2}=> P) ={E1,E2}= Q.
119
Proof. by rewrite fupd_frame_l wand_elim_l. Qed.
120
Lemma fupd_wand_r E1 E2 P Q : (|={E1,E2}=> P)  (P - Q) ={E1,E2}= Q.
121 122 123
Proof. by rewrite fupd_frame_r wand_elim_r. Qed.

Lemma fupd_trans_frame E1 E2 E3 P Q :
124
  ((Q ={E2,E3}= True)  |={E1,E2}=> (Q  P)) ={E1,E3}= P.
125 126 127 128 129 130
Proof.
  rewrite fupd_frame_l assoc -(comm _ Q) wand_elim_r.
  by rewrite fupd_frame_r left_id fupd_trans.
Qed.

Lemma fupd_mask_frame_r E1 E2 Ef P :
131
  E1 ## Ef  (|={E1,E2}=> P) ={E1  Ef,E2  Ef}= P.
132 133 134 135
Proof.
  iIntros (?) "H". iApply fupd_mask_frame_r'; auto.
  iApply fupd_wand_r; iFrame "H"; eauto.
Qed.
136
Lemma fupd_mask_mono E1 E2 P : E1  E2  (|={E1}=> P) ={E2}= P.
137 138 139 140
Proof.
  intros (Ef&->&?)%subseteq_disjoint_union_L. by apply fupd_mask_frame_r.
Qed.

141
Lemma fupd_sep E P Q : (|={E}=> P)  (|={E}=> Q) ={E}= P  Q.
142
Proof. by rewrite fupd_frame_r fupd_frame_l fupd_trans. Qed.
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
143 144 145 146 147 148
Lemma fupd_big_sepL {A} E (Φ : nat  A  iProp Σ) (l : list A) :
  ([ list] kx  l, |={E}=> Φ k x) ={E}= [ list] kx  l, Φ k x.
Proof.
  apply (big_opL_forall (λ P Q, P ={E}= Q)); auto using fupd_intro.
  intros P1 P2 HP Q1 Q2 HQ. by rewrite HP HQ -fupd_sep.
Qed.
149
Lemma fupd_big_sepM `{Countable K} {A} E (Φ : K  A  iProp Σ) (m : gmap K A) :
150
  ([ map] kx  m, |={E}=> Φ k x) ={E}= [ map] kx  m, Φ k x.
151
Proof.
152
  apply (big_opM_forall (λ P Q, P ={E}= Q)); auto using fupd_intro.
153 154 155
  intros P1 P2 HP Q1 Q2 HQ. by rewrite HP HQ -fupd_sep.
Qed.
Lemma fupd_big_sepS `{Countable A} E (Φ : A  iProp Σ) X :
156
  ([ set] x  X, |={E}=> Φ x) ={E}= [ set] x  X, Φ x.
157
Proof.
158
  apply (big_opS_forall (λ P Q, P ={E}= Q)); auto using fupd_intro.
159 160
  intros P1 P2 HP Q1 Q2 HQ. by rewrite HP HQ -fupd_sep.
Qed.
161 162 163 164 165 166 167

Lemma fupd_plain E1 E2 P Q `{!Plain P} :
  E1  E2  (Q - P) - (|={E1, E2}=> Q) ={E1}= (|={E1, E2}=> Q)  P.
Proof.
  iIntros (HE) "HQP HQ". iApply (fupd_plain' _ _ E1 with "[HQP] HQ"); first done.
  iIntros "?". iApply fupd_intro. by iApply "HQP".
Qed.
168 169 170 171
End fupd.

(** Proofmode class instances *)
Section proofmode_classes.
172
  Context `{invG Σ}.
173 174 175 176 177 178
  Implicit Types P Q : iProp Σ.

  Global Instance from_pure_fupd E P φ : FromPure P φ  FromPure (|={E}=> P) φ.
  Proof. rewrite /FromPure. intros <-. apply fupd_intro. Qed.

  Global Instance from_assumption_fupd E p P Q :
179
    FromAssumption p P (|==> Q)  FromAssumption p P (|={E}=> Q)%I.
180 181
  Proof. rewrite /FromAssumption=>->. apply bupd_fupd. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
182 183 184
  Global Instance into_wand_fupd E p q R P Q :
    IntoWand false false R P Q 
    IntoWand p q (|={E}=> R) (|={E}=> P) (|={E}=> Q).
185
  Proof.
186
    rewrite /IntoWand /= => HR. rewrite !affinely_persistently_if_elim HR.
Robbert Krebbers's avatar
Robbert Krebbers committed
187
    apply wand_intro_l. by rewrite fupd_sep wand_elim_r.
188
  Qed.
189

Robbert Krebbers's avatar
Robbert Krebbers committed
190 191 192
  Global Instance into_wand_fupd_persistent E1 E2 p q R P Q :
    IntoWand false q R P Q  IntoWand p q (|={E1,E2}=> R) P (|={E1,E2}=> Q).
  Proof.
193
    rewrite /IntoWand /= => HR. rewrite affinely_persistently_if_elim HR.
Robbert Krebbers's avatar
Robbert Krebbers committed
194 195 196 197 198 199 200
    apply wand_intro_l. by rewrite fupd_frame_l wand_elim_r.
  Qed.

  Global Instance into_wand_fupd_args E1 E2 p q R P Q :
    IntoWand p false R P Q  IntoWand' p q R (|={E1,E2}=> P) (|={E1,E2}=> Q).
  Proof.
    rewrite /IntoWand' /IntoWand /= => ->.
201
    apply wand_intro_l. by rewrite affinely_persistently_if_elim fupd_wand_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
202 203 204 205 206
  Qed.

  Global Instance from_sep_fupd E P Q1 Q2 :
    FromSep P Q1 Q2  FromSep (|={E}=> P) (|={E}=> Q1) (|={E}=> Q2).
  Proof. rewrite /FromSep =><-. apply fupd_sep. Qed.
207

Robbert Krebbers's avatar
Robbert Krebbers committed
208
  Global Instance from_or_fupd E1 E2 P Q1 Q2 :
209 210 211
    FromOr P Q1 Q2  FromOr (|={E1,E2}=> P) (|={E1,E2}=> Q1) (|={E1,E2}=> Q2).
  Proof. rewrite /FromOr=><-. apply or_elim; apply fupd_mono; auto with I. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
212
  Global Instance from_exist_fupd {A} E1 E2 P (Φ : A  iProp Σ) :
213 214 215 216 217
    FromExist P Φ  FromExist (|={E1,E2}=> P) (λ a, |={E1,E2}=> Φ a)%I.
  Proof.
    rewrite /FromExist=><-. apply exist_elim=> a. by rewrite -(exist_intro a).
  Qed.

218 219
  Global Instance frame_fupd p E1 E2 R P Q :
    Frame p R P Q  Frame p R (|={E1,E2}=> P) (|={E1,E2}=> Q).
220 221
  Proof. rewrite /Frame=><-. by rewrite fupd_frame_l. Qed.

222 223
  Global Instance is_except_0_fupd E1 E2 P : IsExcept0 (|={E1,E2}=> P).
  Proof. by rewrite /IsExcept0 except_0_fupd. Qed.
224

225 226
  Global Instance from_modal_fupd E P : FromModal (|={E}=> P) P.
  Proof. rewrite /FromModal. apply fupd_intro. Qed.
227

Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
228 229 230
  (* Put a lower priority compared to [elim_modal_fupd_fupd], so that
     it is not taken when the first parameter is not specified (in
     spec. patterns). *)
231
  Global Instance elim_modal_bupd_fupd E1 E2 P Q :
232
    ElimModal (|==> P) P (|={E1,E2}=> Q) (|={E1,E2}=> Q) | 10.
233 234
  Proof.
    by rewrite /ElimModal (bupd_fupd E1) fupd_frame_r wand_elim_r fupd_trans.
235
  Qed.
236
  Global Instance elim_modal_fupd_fupd E1 E2 E3 P Q :
237
    ElimModal (|={E1,E2}=> P) P (|={E1,E3}=> Q) (|={E2,E3}=> Q).
238
  Proof. by rewrite /ElimModal fupd_frame_r wand_elim_r fupd_trans. Qed.
239 240
End proofmode_classes.

241
Hint Extern 2 (coq_tactics.envs_entails _ (|={_}=> _)) => iModIntro.
242 243 244 245 246

(** Fancy updates that take a step. *)

Notation "|={ E1 , E2 }▷=> Q" := (|={E1,E2}=> ( |={E2,E1}=> Q))%I
  (at level 99, E1, E2 at level 50, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
247
   format "|={ E1 , E2 }▷=>  Q") : bi_scope.
248
Notation "P ={ E1 , E2 }▷=∗ Q" := (P - |={ E1 , E2 }=> Q)%I
249
  (at level 99, E1, E2 at level 50, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
250
   format "P  ={ E1 , E2 }▷=∗  Q") : bi_scope.
251 252
Notation "|={ E }▷=> Q" := (|={E,E}=> Q)%I
  (at level 99, E at level 50, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
253
   format "|={ E }▷=>  Q") : bi_scope.
254
Notation "P ={ E }▷=∗ Q" := (P ={E,E}= Q)%I
255
  (at level 99, E at level 50, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
256
   format "P  ={ E }▷=∗  Q") : bi_scope.
257 258 259 260

Section step_fupd.
Context `{invG Σ}.

261 262 263
Lemma step_fupd_wand E1 E2 P Q : (|={E1,E2}=> P) - (P - Q) - |={E1,E2}=> Q.
Proof. iIntros "HP HPQ". by iApply "HPQ". Qed.

264
Lemma step_fupd_mask_frame_r E1 E2 Ef P :
265
  E1 ## Ef  E2 ## Ef  (|={E1,E2}=> P)  |={E1  Ef,E2  Ef}=> P.
266 267 268 269 270 271 272 273 274
Proof.
  iIntros (??) "HP". iApply fupd_mask_frame_r. done. iMod "HP". iModIntro.
  iNext. by iApply fupd_mask_frame_r.
Qed.

Lemma step_fupd_mask_mono E1 E2 F1 F2 P :
  F1  F2  E1  E2  (|={E1,F2}=> P)  |={E2,F1}=> P.
Proof.
  iIntros (??) "HP".
275 276
  iMod (fupd_intro_mask') as "HM1"; first done. iMod "HP".
  iMod (fupd_intro_mask') as "HM2"; first done. iModIntro.
277
  iNext. iMod "HM2". iMod "HP". iMod "HM1". done.
278
Qed.
279 280 281

Lemma step_fupd_intro E1 E2 P : E2  E1   P - |={E1,E2}=> P.
Proof. iIntros (?) "HP". iApply (step_fupd_mask_mono E2 _ _ E2); auto. Qed.
282
End step_fupd.