weakestpre.v 18.7 KB
Newer Older
1 2
From iris.base_logic.lib Require Export fancy_updates.
From iris.program_logic Require Export language.
3
From iris.base_logic Require Import big_op.
4
From iris.proofmode Require Import tactics classes.
5
Set Default Proof Using "Type".
6 7
Import uPred.

8
Class irisG' (Λstate : Type) (Σ : gFunctors) := IrisG {
9
  iris_invG :> invG Σ;
10
  state_interp : Λstate  iProp Σ;
11 12 13
}.
Notation irisG Λ Σ := (irisG' (state Λ) Σ).

14
Definition wp_pre `{irisG Λ Σ} (s : stuckness)
15 16
    (wp : coPset -c> expr Λ -c> (val Λ -c> iProp Σ) -c> iProp Σ) :
    coPset -c> expr Λ -c> (val Λ -c> iProp Σ) -c> iProp Σ := λ E e1 Φ,
Robbert Krebbers's avatar
Robbert Krebbers committed
17 18 19
  match to_val e1 with
  | Some v => |={E}=> Φ v
  | None =>  σ1,
20
     state_interp σ1 ={E,}= if s is not_stuck then reducible e1 σ1 else True 
Ralf Jung's avatar
Ralf Jung committed
21
       e2 σ2 efs, prim_step e1 σ1 e2 σ2 efs ={,E}=
22 23
       state_interp σ2  wp E e2 Φ 
       [ list] ef  efs, wp  ef (λ _, True)
Robbert Krebbers's avatar
Robbert Krebbers committed
24
  end%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
25

26
Local Instance wp_pre_contractive `{irisG Λ Σ} s : Contractive (wp_pre s).
27
Proof.
28
  rewrite /wp_pre=> n wp wp' Hwp E e1 Φ.
29
  repeat (f_contractive || f_equiv); apply Hwp.
Robbert Krebbers's avatar
Robbert Krebbers committed
30
Qed.
31

32 33
Definition wp_def `{irisG Λ Σ} s :
  coPset  expr Λ  (val Λ  iProp Σ)  iProp Σ := fixpoint (wp_pre s).
34 35 36
Definition wp_aux : seal (@wp_def). by eexists. Qed.
Definition wp := unseal wp_aux.
Definition wp_eq : @wp = @wp_def := seal_eq wp_aux.
Ralf Jung's avatar
Ralf Jung committed
37

38 39
Arguments wp {_ _ _} _ _ _%E _.
Instance: Params (@wp) 6.
Robbert Krebbers's avatar
Robbert Krebbers committed
40

41
Notation "'WP' e @ s ; E {{ Φ } }" := (wp s E e%E Φ)
42
  (at level 20, e, Φ at level 200,
43 44
   format "'[' 'WP'  e  '/' @  s ;  E  {{  Φ  } } ']'") : uPred_scope.
Notation "'WP' e @ E {{ Φ } }" := (wp not_stuck E e%E Φ)
45
  (at level 20, e, Φ at level 200,
46
   format "'[' 'WP'  e  '/' @  E  {{  Φ  } } ']'") : uPred_scope.
47
Notation "'WP' e @ E ? {{ Φ } }" := (wp maybe_stuck E e%E Φ)
48 49
  (at level 20, e, Φ at level 200,
   format "'[' 'WP'  e  '/' @  E  ? {{  Φ  } } ']'") : uPred_scope.
50
Notation "'WP' e {{ Φ } }" := (wp not_stuck  e%E Φ)
51
  (at level 20, e, Φ at level 200,
52
   format "'[' 'WP'  e  '/' {{  Φ  } } ']'") : uPred_scope.
53
Notation "'WP' e ? {{ Φ } }" := (wp maybe_stuck  e%E Φ)
54 55
  (at level 20, e, Φ at level 200,
   format "'[' 'WP'  e  '/' ? {{  Φ  } } ']'") : uPred_scope.
56

57
Notation "'WP' e @ s ; E {{ v , Q } }" := (wp s E e%E (λ v, Q))
58
  (at level 20, e, Q at level 200,
59 60
   format "'[' 'WP'  e  '/' @  s ;  E  {{  v ,  Q  } } ']'") : uPred_scope.
Notation "'WP' e @ E {{ v , Q } }" := (wp not_stuck E e%E (λ v, Q))
61
  (at level 20, e, Q at level 200,
62
   format "'[' 'WP'  e  '/' @  E  {{  v ,  Q  } } ']'") : uPred_scope.
63
Notation "'WP' e @ E ? {{ v , Q } }" := (wp maybe_stuck E e%E (λ v, Q))
64 65
  (at level 20, e, Q at level 200,
   format "'[' 'WP'  e  '/' @  E  ? {{  v ,  Q  } } ']'") : uPred_scope.
66
Notation "'WP' e {{ v , Q } }" := (wp not_stuck  e%E (λ v, Q))
67
  (at level 20, e, Q at level 200,
68
   format "'[' 'WP'  e  '/' {{  v ,  Q  } } ']'") : uPred_scope.
69
Notation "'WP' e ? {{ v , Q } }" := (wp maybe_stuck  e%E (λ v, Q))
70 71
  (at level 20, e, Q at level 200,
   format "'[' 'WP'  e  '/' ? {{  v ,  Q  } } ']'") : uPred_scope.
72

Ralf Jung's avatar
Ralf Jung committed
73
(* Texan triples *)
74
Notation "'{{{' P } } } e @ s ; E {{{ x .. y , 'RET' pat ; Q } } }" :=
75
  (  Φ,
76
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ s; E {{ Φ }})%I
77
    (at level 20, x closed binder, y closed binder,
78
     format "{{{  P  } } }  e  @  s ;  E  {{{  x .. y ,  RET  pat ;  Q } } }") : uPred_scope.
79
Notation "'{{{' P } } } e @ E {{{ x .. y , 'RET' pat ; Q } } }" :=
Ralf Jung's avatar
Ralf Jung committed
80
  (  Φ,
81
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
82
    (at level 20, x closed binder, y closed binder,
83
     format "{{{  P  } } }  e  @  E  {{{  x .. y ,  RET  pat ;  Q } } }") : uPred_scope.
84 85 86 87 88
Notation "'{{{' P } } } e @ E ? {{{ x .. y , 'RET' pat ; Q } } }" :=
  (  Φ,
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E ?{{ Φ }})%I
    (at level 20, x closed binder, y closed binder,
     format "{{{  P  } } }  e  @  E  ? {{{  x .. y ,  RET  pat ;  Q } } }") : uPred_scope.
89 90 91 92 93
Notation "'{{{' P } } } e {{{ x .. y , 'RET' pat ; Q } } }" :=
  (  Φ,
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e {{ Φ }})%I
    (at level 20, x closed binder, y closed binder,
     format "{{{  P  } } }  e  {{{  x .. y ,   RET  pat ;  Q } } }") : uPred_scope.
94 95 96 97 98
Notation "'{{{' P } } } e ? {{{ x .. y , 'RET' pat ; Q } } }" :=
  (  Φ,
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e ?{{ Φ }})%I
    (at level 20, x closed binder, y closed binder,
     format "{{{  P  } } }  e  ? {{{  x .. y ,   RET  pat ;  Q } } }") : uPred_scope.
99 100
Notation "'{{{' P } } } e @ s ; E {{{ 'RET' pat ; Q } } }" :=
  (  Φ, P -  (Q - Φ pat%V) - WP e @ s; E {{ Φ }})%I
101
    (at level 20,
102
     format "{{{  P  } } }  e  @  s ;  E  {{{  RET  pat ;  Q } } }") : uPred_scope.
103
Notation "'{{{' P } } } e @ E {{{ 'RET' pat ; Q } } }" :=
104
  (  Φ, P -  (Q - Φ pat%V) - WP e @ E {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
105
    (at level 20,
106
     format "{{{  P  } } }  e  @  E  {{{  RET  pat ;  Q } } }") : uPred_scope.
107 108 109 110
Notation "'{{{' P } } } e @ E ? {{{ 'RET' pat ; Q } } }" :=
  (  Φ, P -  (Q - Φ pat%V) - WP e @ E ?{{ Φ }})%I
    (at level 20,
     format "{{{  P  } } }  e  @  E  ? {{{  RET  pat ;  Q } } }") : uPred_scope.
111 112 113 114
Notation "'{{{' P } } } e {{{ 'RET' pat ; Q } } }" :=
  (  Φ, P -  (Q - Φ pat%V) - WP e {{ Φ }})%I
    (at level 20,
     format "{{{  P  } } }  e  {{{  RET  pat ;  Q } } }") : uPred_scope.
115 116 117 118
Notation "'{{{' P } } } e ? {{{ 'RET' pat ; Q } } }" :=
  (  Φ, P -  (Q - Φ pat%V) - WP e ?{{ Φ }})%I
    (at level 20,
     format "{{{  P  } } }  e  ? {{{  RET  pat ;  Q } } }") : uPred_scope.
Ralf Jung's avatar
Ralf Jung committed
119

120
Notation "'{{{' P } } } e @ s ; E {{{ x .. y , 'RET' pat ; Q } } }" :=
121
  ( Φ : _  uPred _,
122
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ s; E {{ Φ }})
123
    (at level 20, x closed binder, y closed binder,
124
     format "{{{  P  } } }  e  @  s ;  E  {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
125
Notation "'{{{' P } } } e @ E {{{ x .. y , 'RET' pat ; Q } } }" :=
126
  ( Φ : _  uPred _,
127
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
128
    (at level 20, x closed binder, y closed binder,
129
     format "{{{  P  } } }  e  @  E  {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
130 131 132 133 134
Notation "'{{{' P } } } e @ E ? {{{ x .. y , 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _,
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E ?{{ Φ }})
    (at level 20, x closed binder, y closed binder,
     format "{{{  P  } } }  e  @  E  ? {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
135 136 137 138 139
Notation "'{{{' P } } } e {{{ x .. y , 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _,
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e {{ Φ }})
    (at level 20, x closed binder, y closed binder,
     format "{{{  P  } } }  e  {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
140 141 142 143 144
Notation "'{{{' P } } } e ? {{{ x .. y , 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _,
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e ?{{ Φ }})
    (at level 20, x closed binder, y closed binder,
     format "{{{  P  } } }  e  ? {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
145 146
Notation "'{{{' P } } } e @ s ; E {{{ 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e @ s; E {{ Φ }})
147
    (at level 20,
148
     format "{{{  P  } } }  e  @  s ;  E  {{{  RET  pat ;  Q } } }") : C_scope.
149
Notation "'{{{' P } } } e @ E {{{ 'RET' pat ; Q } } }" :=
150
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
151
    (at level 20,
152
     format "{{{  P  } } }  e  @  E  {{{  RET  pat ;  Q } } }") : C_scope.
153 154 155 156
Notation "'{{{' P } } } e @ E ? {{{ 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e @ E ?{{ Φ }})
    (at level 20,
     format "{{{  P  } } }  e  @  E  ? {{{  RET  pat ;  Q } } }") : C_scope.
157 158 159 160
Notation "'{{{' P } } } e {{{ 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e {{ Φ }})
    (at level 20,
     format "{{{  P  } } }  e  {{{  RET  pat ;  Q } } }") : C_scope.
161 162 163 164
Notation "'{{{' P } } } e ? {{{ 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e ?{{ Φ }})
    (at level 20,
     format "{{{  P  } } }  e  ? {{{  RET  pat ;  Q } } }") : C_scope.
165

Robbert Krebbers's avatar
Robbert Krebbers committed
166
Section wp.
167
Context `{irisG Λ Σ}.
168
Implicit Types s : stuckness.
169 170
Implicit Types P : iProp Σ.
Implicit Types Φ : val Λ  iProp Σ.
171 172
Implicit Types v : val Λ.
Implicit Types e : expr Λ.
Robbert Krebbers's avatar
Robbert Krebbers committed
173

174
(* Weakest pre *)
175 176
Lemma wp_unfold s E e Φ : WP e @ s; E {{ Φ }}  wp_pre s (wp s) E e Φ.
Proof. rewrite wp_eq. apply (fixpoint_unfold (wp_pre s)). Qed.
177

178 179
Global Instance wp_ne s E e n :
  Proper (pointwise_relation _ (dist n) ==> dist n) (@wp Λ Σ _ s E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
180
Proof.
181
  revert e. induction (lt_wf n) as [n _ IH]=> e Φ Ψ HΦ.
182 183 184 185
  rewrite !wp_unfold /wp_pre.
  (* FIXME: figure out a way to properly automate this proof *)
  (* FIXME: reflexivity, as being called many times by f_equiv and f_contractive
  is very slow here *)
Robbert Krebbers's avatar
Robbert Krebbers committed
186
  do 17 (f_contractive || f_equiv). apply IH; first lia.
187
  intros v. eapply dist_le; eauto with omega.
Robbert Krebbers's avatar
Robbert Krebbers committed
188
Qed.
189 190
Global Instance wp_proper s E e :
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ _ s E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
191
Proof.
192
  by intros Φ Φ' ?; apply equiv_dist=>n; apply wp_ne=>v; apply equiv_dist.
Robbert Krebbers's avatar
Robbert Krebbers committed
193
Qed.
194

195
Lemma wp_value' s E Φ v : Φ v  WP of_val v @ s; E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
196
Proof. iIntros "HΦ". rewrite wp_unfold /wp_pre to_of_val. auto. Qed.
197
Lemma wp_value_inv s E Φ v : WP of_val v @ s; E {{ Φ }} ={E}= Φ v.
Robbert Krebbers's avatar
Robbert Krebbers committed
198
Proof. by rewrite wp_unfold /wp_pre to_of_val. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
199

200 201
Lemma wp_strong_mono s E1 E2 e Φ Ψ :
  E1  E2  ( v, Φ v ={E2}= Ψ v)  WP e @ s; E1 {{ Φ }}  WP e @ s; E2 {{ Ψ }}.
202
Proof.
203
  iIntros (?) "[HΦ H]". iLöb as "IH" forall (e). rewrite !wp_unfold /wp_pre.
Robbert Krebbers's avatar
Robbert Krebbers committed
204
  destruct (to_val e) as [v|] eqn:?.
205
  { iApply ("HΦ" with "[> -]"). by iApply (fupd_mask_mono E1 _). }
Robbert Krebbers's avatar
Robbert Krebbers committed
206
  iIntros (σ1) "Hσ". iMod (fupd_intro_mask' E2 E1) as "Hclose"; first done.
207
  iMod ("H" with "[$]") as "[$ H]".
208
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
209
  iMod ("H" with "[//]") as "($ & H & $)"; auto.
210
  iMod "Hclose" as "_". by iApply ("IH" with "HΦ").
211 212
Qed.

213 214
Lemma wp_forget_not_stuck s E e Φ :
  WP e @ s; E {{ Φ }}  WP e @ E ?{{ Φ }}.
215 216 217 218 219 220 221 222 223 224 225 226
Proof.
  iIntros "H". iLöb as "IH" forall (E e Φ). rewrite !wp_unfold /wp_pre.
  destruct (to_val e) as [v|]; first iExact "H".
  iIntros (σ1) "Hσ". iMod ("H" with "Hσ") as "[#Hred H]". iModIntro.
  iSplit; first done. iNext. iIntros (e2 σ2 efs) "#Hstep".
  iMod ("H" with "Hstep") as "($ & He2 & Hefs)". iModIntro.
  iSplitL "He2"; first by iApply ("IH" with "He2"). iClear "Hred Hstep".
  induction efs as [|ef efs IH]; first by iApply big_sepL_nil.
  rewrite !big_sepL_cons. iDestruct "Hefs" as "(Hef & Hefs)".
  iSplitL "Hef". by iApply ("IH" with "Hef"). exact: IH. 
Qed.

227
Lemma fupd_wp s E e Φ : (|={E}=> WP e @ s; E {{ Φ }})  WP e @ s; E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
228
Proof.
229
  rewrite wp_unfold /wp_pre. iIntros "H". destruct (to_val e) as [v|] eqn:?.
Robbert Krebbers's avatar
Robbert Krebbers committed
230 231
  { by iMod "H". }
  iIntros (σ1) "Hσ1". iMod "H". by iApply "H".
232
Qed.
233 234
Lemma wp_fupd s E e Φ : WP e @ s; E {{ v, |={E}=> Φ v }}  WP e @ s; E {{ Φ }}.
Proof. iIntros "H". iApply (wp_strong_mono s E); try iFrame; auto. Qed.
235

236 237
Lemma wp_strong_atomic s E1 E2 e Φ :
  Atomic maybe_stuck e 
238
  (|={E1,E2}=> WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ s; E1 {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
239
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
240
  iIntros (Hatomic) "H". rewrite !wp_unfold /wp_pre.
241
  destruct (to_val e) as [v|].
Robbert Krebbers's avatar
Robbert Krebbers committed
242 243
  { by iDestruct "H" as ">>> $". }
  iIntros (σ1) "Hσ". iMod "H". iMod ("H" $! σ1 with "Hσ") as "[$ H]".
244
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  destruct (Hatomic _ _ _ _ Hstep) as [v <-%of_to_val].
  iMod ("H" with "[#]") as "($ & H & $)"; first done.
  iMod (wp_value_inv with "H") as ">H". by iApply wp_value'.
Qed.

Lemma wp_weak_atomic E1 E2 e Φ :
  Atomic not_stuck e 
  (|={E1,E2}=> WP e @ E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ E1 {{ Φ }}.
Proof.
  iIntros (Hatomic) "H". rewrite !wp_unfold /wp_pre.
  destruct (to_val e) as [v|] eqn:He.
  { by iDestruct "H" as ">>> $". }
  iIntros (σ1) "Hσ". iMod "H". iMod ("H" $! σ1 with "Hσ") as "[$ H]".
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
  iMod ("H" with "[//]") as "(Hphy & H & $)".
  rewrite !wp_unfold /wp_pre. destruct (to_val e2) as [v2|] eqn:He2.
  - iDestruct "H" as ">> $". by iFrame.
  - iMod ("H" with "[$]") as "[H _]".
    iDestruct "H" as %(? & ? & ? & ?). by edestruct (Hatomic _ _ _ _ Hstep).
Robbert Krebbers's avatar
Robbert Krebbers committed
264
Qed.
265

266
Lemma wp_step_fupd s E1 E2 e P Φ :
267
  to_val e = None  E2  E1 
268
  (|={E1,E2}=> P) - WP e @ s; E2 {{ v, P ={E1}= Φ v }} - WP e @ s; E1 {{ Φ }}.
269
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
270
  rewrite !wp_unfold /wp_pre. iIntros (-> ?) "HR H".
271
  iIntros (σ1) "Hσ". iMod "HR". iMod ("H" with "[$]") as "[$ H]".
272
  iModIntro; iNext; iIntros (e2 σ2 efs Hstep).
273
  iMod ("H" $! e2 σ2 efs with "[% //]") as "($ & H & $)".
274
  iMod "HR". iModIntro. iApply (wp_strong_mono s E2); first done.
275
  iSplitR "H"; last iExact "H". iIntros (v) "H". by iApply "H".
Robbert Krebbers's avatar
Robbert Krebbers committed
276
Qed.
277

278 279
Lemma wp_bind K `{!LanguageCtx Λ K} s E e Φ :
  WP e @ s; E {{ v, WP K (of_val v) @ s; E {{ Φ }} }}  WP K e @ s; E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
280
Proof.
281
  iIntros "H". iLöb as "IH" forall (E e Φ). rewrite wp_unfold /wp_pre.
Robbert Krebbers's avatar
Robbert Krebbers committed
282 283 284
  destruct (to_val e) as [v|] eqn:He.
  { apply of_to_val in He as <-. by iApply fupd_wp. }
  rewrite wp_unfold /wp_pre fill_not_val //.
285
  iIntros (σ1) "Hσ". iMod ("H" with "[$]") as "[% H]". iModIntro; iSplit.
286
  { iPureIntro. destruct s; last done.
287
    unfold reducible in *. naive_solver eauto using fill_step. }
288 289
  iNext; iIntros (e2 σ2 efs Hstep).
  destruct (fill_step_inv e σ1 e2 σ2 efs) as (e2'&->&?); auto.
290
  iMod ("H" $! e2' σ2 efs with "[//]") as "($ & H & $)".
291
  by iApply "IH".
Robbert Krebbers's avatar
Robbert Krebbers committed
292 293
Qed.

294 295
Lemma wp_bind_inv K `{!LanguageCtx Λ K} s E e Φ :
  WP K e @ s; E {{ Φ }}  WP e @ s; E {{ v, WP K (of_val v) @ s; E {{ Φ }} }}.
296 297 298 299 300 301
Proof.
  iIntros "H". iLöb as "IH" forall (E e Φ). rewrite !wp_unfold /wp_pre.
  destruct (to_val e) as [v|] eqn:He.
  { apply of_to_val in He as <-. by rewrite !wp_unfold /wp_pre. }
  rewrite fill_not_val //.
  iIntros (σ1) "Hσ". iMod ("H" with "[$]") as "[% H]". iModIntro; iSplit.
302
  { destruct s; eauto using reducible_fill. }
303 304 305 306 307
  iNext; iIntros (e2 σ2 efs Hstep).
  iMod ("H" $! (K e2) σ2 efs with "[]") as "($ & H & $)"; eauto using fill_step.
  by iApply "IH".
Qed.

308
(** * Derived rules *)
309
Lemma wp_mono s E e Φ Ψ : ( v, Φ v  Ψ v)  WP e @ s; E {{ Φ }}  WP e @ s; E {{ Ψ }}.
310
Proof.
311
  iIntros (HΦ) "H"; iApply (wp_strong_mono s E E); auto.
312
  iIntros "{$H}" (v) "?". by iApply HΦ.
313
Qed.
David Swasey's avatar
David Swasey committed
314 315 316
Lemma wp_stuckness_mono s1 s2 E e Φ :
  (s1  s2)%stuckness  WP e @ s1; E {{ Φ }}  WP e @ s2; E {{ Φ }}.
Proof. case: s1; case: s2 => // _. exact: wp_forget_not_stuck. Qed.
317 318 319 320
Lemma wp_mask_mono s E1 E2 e Φ : E1  E2  WP e @ s; E1 {{ Φ }}  WP e @ s; E2 {{ Φ }}.
Proof. iIntros (?) "H"; iApply (wp_strong_mono s E1 E2); auto. iFrame; eauto. Qed.
Global Instance wp_mono' s E e :
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ _ s E e).
321
Proof. by intros Φ Φ' ?; apply wp_mono. Qed.
322

323
Lemma wp_value s E Φ e v `{!IntoVal e v} : Φ v  WP e @ s; E {{ Φ }}.
324
Proof. intros; rewrite -(of_to_val e v) //; by apply wp_value'. Qed.
325
Lemma wp_value_fupd' s E Φ v : (|={E}=> Φ v)  WP of_val v @ s; E {{ Φ }}.
326
Proof. intros. by rewrite -wp_fupd -wp_value'. Qed.
327 328
Lemma wp_value_fupd s E Φ e v `{!IntoVal e v} :
  (|={E}=> Φ v)  WP e @ s; E {{ Φ }}.
329
Proof. intros. rewrite -wp_fupd -wp_value //. Qed.
330

331
Lemma wp_atomic s E1 E2 e Φ `{!Atomic s e} :
332
  (|={E1,E2}=> WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ s; E1 {{ Φ }}.
333
Proof. destruct s. exact: wp_weak_atomic. exact: wp_strong_atomic. Qed.
334

335 336 337 338
Lemma wp_frame_l s E e Φ R : R  WP e @ s; E {{ Φ }}  WP e @ s; E {{ v, R  Φ v }}.
Proof. iIntros "[??]". iApply (wp_strong_mono s E E _ Φ); try iFrame; eauto. Qed.
Lemma wp_frame_r s E e Φ R : WP e @ s; E {{ Φ }}  R  WP e @ s; E {{ v, Φ v  R }}.
Proof. iIntros "[??]". iApply (wp_strong_mono s E E _ Φ); try iFrame; eauto. Qed.
339

340
Lemma wp_frame_step_l s E1 E2 e Φ R :
341
  to_val e = None  E2  E1 
342
  (|={E1,E2}=> R)  WP e @ s; E2 {{ Φ }}  WP e @ s; E1 {{ v, R  Φ v }}.
343
Proof.
344
  iIntros (??) "[Hu Hwp]". iApply (wp_step_fupd with "Hu"); try done.
345 346
  iApply (wp_mono with "Hwp"). by iIntros (?) "$$".
Qed.
347
Lemma wp_frame_step_r s E1 E2 e Φ R :
348
  to_val e = None  E2  E1 
349
  WP e @ s; E2 {{ Φ }}  (|={E1,E2}=> R)  WP e @ s; E1 {{ v, Φ v  R }}.
Ralf Jung's avatar
Ralf Jung committed
350
Proof.
351
  rewrite [(WP _ @ _; _ {{ _ }}  _)%I]comm; setoid_rewrite (comm _ _ R).
352
  apply wp_frame_step_l.
Ralf Jung's avatar
Ralf Jung committed
353
Qed.
354 355 356 357 358 359 360 361 362
Lemma wp_frame_step_l' s E e Φ R :
  to_val e = None   R  WP e @ s; E {{ Φ }}  WP e @ s; E {{ v, R  Φ v }}.
Proof. iIntros (?) "[??]". iApply (wp_frame_step_l s E E); try iFrame; eauto. Qed.
Lemma wp_frame_step_r' s E e Φ R :
  to_val e = None  WP e @ s; E {{ Φ }}   R  WP e @ s; E {{ v, Φ v  R }}.
Proof. iIntros (?) "[??]". iApply (wp_frame_step_r s E E); try iFrame; eauto. Qed.

Lemma wp_wand s E e Φ Ψ :
  WP e @ s; E {{ Φ }} - ( v, Φ v - Ψ v) - WP e @ s; E {{ Ψ }}.
363
Proof.
364
  iIntros "Hwp H". iApply (wp_strong_mono s E); auto.
365
  iIntros "{$Hwp}" (?) "?". by iApply "H".
366
Qed.
367 368
Lemma wp_wand_l s E e Φ Ψ :
  ( v, Φ v - Ψ v)  WP e @ s; E {{ Φ }}  WP e @ s; E {{ Ψ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
369
Proof. iIntros "[H Hwp]". iApply (wp_wand with "Hwp H"). Qed.
370 371
Lemma wp_wand_r s E e Φ Ψ :
  WP e @ s; E {{ Φ }}  ( v, Φ v - Ψ v)  WP e @ s; E {{ Ψ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
372
Proof. iIntros "[Hwp H]". iApply (wp_wand with "Hwp H"). Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
373
End wp.
374 375 376 377 378 379 380

(** Proofmode class instances *)
Section proofmode_classes.
  Context `{irisG Λ Σ}.
  Implicit Types P Q : iProp Σ.
  Implicit Types Φ : val Λ  iProp Σ.

381 382
  Global Instance frame_wp p s E e R Φ Ψ :
    ( v, Frame p R (Φ v) (Ψ v))  Frame p R (WP e @ s; E {{ Φ }}) (WP e @ s; E {{ Ψ }}).
383 384
  Proof. rewrite /Frame=> HR. rewrite wp_frame_l. apply wp_mono, HR. Qed.

385
  Global Instance is_except_0_wp s E e Φ : IsExcept0 (WP e @ s; E {{ Φ }}).
386
  Proof. by rewrite /IsExcept0 -{2}fupd_wp -except_0_fupd -fupd_intro. Qed.
387

388 389
  Global Instance elim_modal_bupd_wp s E e P Φ :
    ElimModal (|==> P) P (WP e @ s; E {{ Φ }}) (WP e @ s; E {{ Φ }}).
390
  Proof. by rewrite /ElimModal (bupd_fupd E) fupd_frame_r wand_elim_r fupd_wp. Qed.
391

392 393
  Global Instance elim_modal_fupd_wp s E e P Φ :
    ElimModal (|={E}=> P) P (WP e @ s; E {{ Φ }}) (WP e @ s; E {{ Φ }}).
394
  Proof. by rewrite /ElimModal fupd_frame_r wand_elim_r fupd_wp. Qed.
395

396
  (* lower precedence, if possible, it should persistently pick elim_upd_fupd_wp *)
397 398
  Global Instance elim_modal_fupd_wp_atomic s E1 E2 e P Φ :
    Atomic s e 
399
    ElimModal (|={E1,E2}=> P) P
400
            (WP e @ s; E1 {{ Φ }}) (WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})%I | 100.
401
  Proof. intros. by rewrite /ElimModal fupd_frame_r wand_elim_r wp_atomic. Qed.
402
End proofmode_classes.