weakestpre.v 18.6 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 Λ) Σ).

Ralf Jung's avatar
Ralf Jung committed
14
Inductive stuckness := not_stuck | maybe_stuck.
Ralf Jung's avatar
Ralf Jung committed
15

Ralf Jung's avatar
Ralf Jung committed
16 17 18 19 20 21 22 23 24 25
Definition stuckness_le (s1 s2 : stuckness) : bool :=
  match s1, s2 with
  | maybe_stuck, not_stuck => false
  | _, _ => true
  end.
Instance: PreOrder stuckness_le.
Proof.
  split; first by case. move=>s1 s2 s3. by case: s1; case: s2; case: s3.
Qed.

Ralf Jung's avatar
Ralf Jung committed
26 27 28
Definition stuckness_to_atomicity (s : stuckness) : atomicity :=
  if s is maybe_stuck then strongly_atomic else weakly_atomic.

29
Definition wp_pre `{irisG Λ Σ} (s : stuckness)
30 31
    (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
32 33 34
  match to_val e1 with
  | Some v => |={E}=> Φ v
  | None =>  σ1,
35
     state_interp σ1 ={E,}= if s is not_stuck then reducible e1 σ1 else True 
Ralf Jung's avatar
Ralf Jung committed
36
       e2 σ2 efs, prim_step e1 σ1 e2 σ2 efs ={,E}=
37 38
       state_interp σ2  wp E e2 Φ 
       [ list] ef  efs, wp  ef (λ _, True)
Robbert Krebbers's avatar
Robbert Krebbers committed
39
  end%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
40

41
Local Instance wp_pre_contractive `{irisG Λ Σ} s : Contractive (wp_pre s).
42
Proof.
43
  rewrite /wp_pre=> n wp wp' Hwp E e1 Φ.
44
  repeat (f_contractive || f_equiv); apply Hwp.
Robbert Krebbers's avatar
Robbert Krebbers committed
45
Qed.
46

47 48
Definition wp_def `{irisG Λ Σ} s :
  coPset  expr Λ  (val Λ  iProp Σ)  iProp Σ := fixpoint (wp_pre s).
49 50 51
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
52

53 54
Arguments wp {_ _ _} _ _ _%E _.
Instance: Params (@wp) 6.
Robbert Krebbers's avatar
Robbert Krebbers committed
55

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

72 73 74 75
Notation "'WP' e @ s ; E {{ v , Q } }" := (wp s E e%E (λ v, Q))
  (at level 20, e, Q at level 200,
   format "'[' 'WP'  e  '/' @  s ;  E  {{  v ,  Q  } } ']'") : uPred_scope.
Notation "'WP' e @ E {{ v , Q } }" := (wp not_stuck E e%E (λ v, Q))
76
  (at level 20, e, Q at level 200,
77
   format "'[' 'WP'  e  '/' @  E  {{  v ,  Q  } } ']'") : uPred_scope.
78 79 80 81
Notation "'WP' e @ E ? {{ v , Q } }" := (wp maybe_stuck E e%E (λ v, Q))
  (at level 20, e, Q at level 200,
   format "'[' 'WP'  e  '/' @  E  ? {{  v ,  Q  } } ']'") : uPred_scope.
Notation "'WP' e {{ v , Q } }" := (wp not_stuck  e%E (λ v, Q))
82
  (at level 20, e, Q at level 200,
83
   format "'[' 'WP'  e  '/' {{  v ,  Q  } } ']'") : uPred_scope.
84 85 86
Notation "'WP' e ? {{ v , Q } }" := (wp maybe_stuck  e%E (λ v, Q))
  (at level 20, e, Q at level 200,
   format "'[' 'WP'  e  '/' ? {{  v ,  Q  } } ']'") : uPred_scope.
87

Ralf Jung's avatar
Ralf Jung committed
88
(* Texan triples *)
89 90 91 92 93
Notation "'{{{' P } } } e @ s ; E {{{ x .. y , 'RET' pat ; Q } } }" :=
  (  Φ,
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ s; E {{ Φ }})%I
    (at level 20, x closed binder, y closed binder,
     format "{{{  P  } } }  e  @  s ;  E  {{{  x .. y ,  RET  pat ;  Q } } }") : uPred_scope.
94
Notation "'{{{' P } } } e @ E {{{ x .. y , 'RET' pat ; Q } } }" :=
Ralf Jung's avatar
Ralf Jung committed
95
  (  Φ,
96
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
97
    (at level 20, x closed binder, y closed binder,
98
     format "{{{  P  } } }  e  @  E  {{{  x .. y ,  RET  pat ;  Q } } }") : uPred_scope.
99 100 101 102 103
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.
104 105 106 107 108
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.
109 110 111 112 113 114 115 116 117
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.
Notation "'{{{' P } } } e @ s ; E {{{ 'RET' pat ; Q } } }" :=
  (  Φ, P -  (Q - Φ pat%V) - WP e @ s; E {{ Φ }})%I
    (at level 20,
     format "{{{  P  } } }  e  @  s ;  E  {{{  RET  pat ;  Q } } }") : uPred_scope.
118
Notation "'{{{' P } } } e @ E {{{ 'RET' pat ; Q } } }" :=
119
  (  Φ, P -  (Q - Φ pat%V) - WP e @ E {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
120
    (at level 20,
121
     format "{{{  P  } } }  e  @  E  {{{  RET  pat ;  Q } } }") : uPred_scope.
122 123 124 125
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.
126 127 128 129
Notation "'{{{' P } } } e {{{ 'RET' pat ; Q } } }" :=
  (  Φ, P -  (Q - Φ pat%V) - WP e {{ Φ }})%I
    (at level 20,
     format "{{{  P  } } }  e  {{{  RET  pat ;  Q } } }") : uPred_scope.
130 131 132 133
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
134

135 136 137 138
Notation "'{{{' P } } } e @ s ; E {{{ x .. y , 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _,
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ s; E {{ Φ }})
    (at level 20, x closed binder, y closed binder,
139
     format "{{{  P  } } }  e  @  s ;  E  {{{  x .. y ,  RET  pat ;  Q } } }") : stdpp_scope.
140
Notation "'{{{' P } } } e @ E {{{ x .. y , 'RET' pat ; Q } } }" :=
141
  ( Φ : _  uPred _,
142
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
143
    (at level 20, x closed binder, y closed binder,
Robbert Krebbers's avatar
Robbert Krebbers committed
144
     format "{{{  P  } } }  e  @  E  {{{  x .. y ,  RET  pat ;  Q } } }") : stdpp_scope.
145 146 147 148
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,
149
     format "{{{  P  } } }  e  @  E  ? {{{  x .. y ,  RET  pat ;  Q } } }") : stdpp_scope.
150 151 152 153
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,
Robbert Krebbers's avatar
Robbert Krebbers committed
154
     format "{{{  P  } } }  e  {{{  x .. y ,  RET  pat ;  Q } } }") : stdpp_scope.
155 156 157 158
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,
159
     format "{{{  P  } } }  e  ? {{{  x .. y ,  RET  pat ;  Q } } }") : stdpp_scope.
160 161 162
Notation "'{{{' P } } } e @ s ; E {{{ 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e @ s; E {{ Φ }})
    (at level 20,
163
     format "{{{  P  } } }  e  @  s ;  E  {{{  RET  pat ;  Q } } }") : stdpp_scope.
164
Notation "'{{{' P } } } e @ E {{{ 'RET' pat ; Q } } }" :=
165
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
166
    (at level 20,
Robbert Krebbers's avatar
Robbert Krebbers committed
167
     format "{{{  P  } } }  e  @  E  {{{  RET  pat ;  Q } } }") : stdpp_scope.
168 169 170
Notation "'{{{' P } } } e @ E ? {{{ 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e @ E ?{{ Φ }})
    (at level 20,
171
     format "{{{  P  } } }  e  @  E  ? {{{  RET  pat ;  Q } } }") : stdpp_scope.
172 173 174
Notation "'{{{' P } } } e {{{ 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e {{ Φ }})
    (at level 20,
Robbert Krebbers's avatar
Robbert Krebbers committed
175
     format "{{{  P  } } }  e  {{{  RET  pat ;  Q } } }") : stdpp_scope.
176 177 178
Notation "'{{{' P } } } e ? {{{ 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e ?{{ Φ }})
    (at level 20,
179
     format "{{{  P  } } }  e  ? {{{  RET  pat ;  Q } } }") : stdpp_scope.
180

Robbert Krebbers's avatar
Robbert Krebbers committed
181
Section wp.
182
Context `{irisG Λ Σ}.
183
Implicit Types s : stuckness.
184 185
Implicit Types P : iProp Σ.
Implicit Types Φ : val Λ  iProp Σ.
186 187
Implicit Types v : val Λ.
Implicit Types e : expr Λ.
Robbert Krebbers's avatar
Robbert Krebbers committed
188

189
(* Weakest pre *)
190 191
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.
192

193 194
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
195
Proof.
196
  revert e. induction (lt_wf n) as [n _ IH]=> e Φ Ψ HΦ.
197 198 199 200
  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
201
  do 17 (f_contractive || f_equiv). apply IH; first lia.
202
  intros v. eapply dist_le; eauto with omega.
Robbert Krebbers's avatar
Robbert Krebbers committed
203
Qed.
204 205
Global Instance wp_proper s E e :
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ _ s E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
206
Proof.
207
  by intros Φ Φ' ?; apply equiv_dist=>n; apply wp_ne=>v; apply equiv_dist.
Robbert Krebbers's avatar
Robbert Krebbers committed
208
Qed.
209

210
Lemma wp_value' s E Φ v : Φ v  WP of_val v @ s; E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
211
Proof. iIntros "HΦ". rewrite wp_unfold /wp_pre to_of_val. auto. Qed.
212
Lemma wp_value_inv s E Φ v : WP of_val v @ s; E {{ Φ }} ={E}= Φ v.
Robbert Krebbers's avatar
Robbert Krebbers committed
213
Proof. by rewrite wp_unfold /wp_pre to_of_val. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
214

215 216
Lemma wp_strong_mono s E1 E2 e Φ Ψ :
  E1  E2  ( v, Φ v ={E2}= Ψ v)  WP e @ s; E1 {{ Φ }}  WP e @ s; E2 {{ Ψ }}.
217
Proof.
218
  iIntros (?) "[HΦ H]". iLöb as "IH" forall (e). rewrite !wp_unfold /wp_pre.
Robbert Krebbers's avatar
Robbert Krebbers committed
219
  destruct (to_val e) as [v|] eqn:?.
220
  { iApply ("HΦ" with "[> -]"). by iApply (fupd_mask_mono E1 _). }
Robbert Krebbers's avatar
Robbert Krebbers committed
221
  iIntros (σ1) "Hσ". iMod (fupd_intro_mask' E2 E1) as "Hclose"; first done.
222
  iMod ("H" with "[$]") as "[$ H]".
223
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
224
  iMod ("H" with "[//]") as "($ & H & $)"; auto.
225
  iMod "Hclose" as "_". by iApply ("IH" with "HΦ").
226 227
Qed.

David Swasey's avatar
David Swasey committed
228
Lemma wp_stuck_weaken s E e Φ :
229 230 231 232 233 234 235 236 237 238 239 240 241 242
  WP e @ s; E {{ Φ }}  WP e @ E ?{{ Φ }}.
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.

Lemma fupd_wp s E e Φ : (|={E}=> WP e @ s; E {{ Φ }})  WP e @ s; E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
243
Proof.
244
  rewrite wp_unfold /wp_pre. iIntros "H". destruct (to_val e) as [v|] eqn:?.
Robbert Krebbers's avatar
Robbert Krebbers committed
245 246
  { by iMod "H". }
  iIntros (σ1) "Hσ1". iMod "H". by iApply "H".
247
Qed.
248 249
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.
250

Ralf Jung's avatar
Ralf Jung committed
251
Lemma wp_atomic s E1 E2 e Φ `{!Atomic (stuckness_to_atomicity s) e} :
252 253
  (|={E1,E2}=> WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ s; E1 {{ Φ }}.
Proof.
David Swasey's avatar
David Swasey committed
254
  iIntros "H". rewrite !wp_unfold /wp_pre.
255 256 257
  destruct (to_val e) as [v|] eqn:He.
  { by iDestruct "H" as ">>> $". }
  iIntros (σ1) "Hσ". iMod "H". iMod ("H" $! σ1 with "Hσ") as "[$ H]".
David Swasey's avatar
David Swasey committed
258 259 260 261 262
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep). destruct s.
  - 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 %(? & ? & ? & ?).
Ralf Jung's avatar
Ralf Jung committed
263 264
      by edestruct (atomic _ _ _ _ Hstep).
  - destruct (atomic _ _ _ _ Hstep) as [v <-%of_to_val].
David Swasey's avatar
David Swasey committed
265 266
    iMod ("H" with "[#]") as "($ & H & $)"; first done.
    iMod (wp_value_inv with "H") as ">H". by iApply wp_value'.
Robbert Krebbers's avatar
Robbert Krebbers committed
267
Qed.
268

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

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

297
Lemma wp_bind_inv K `{!LanguageCtx K} s E e Φ :
298
  WP K e @ s; E {{ Φ }}  WP e @ s; E {{ v, WP K (of_val v) @ s; E {{ Φ }} }}.
299 300 301 302 303 304
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.
305
  { destruct s; eauto using reducible_fill. }
306 307 308 309 310
  iNext; iIntros (e2 σ2 efs Hstep).
  iMod ("H" $! (K e2) σ2 efs with "[]") as "($ & H & $)"; eauto using fill_step.
  by iApply "IH".
Qed.

311
(** * Derived rules *)
312
Lemma wp_mono s E e Φ Ψ : ( v, Φ v  Ψ v)  WP e @ s; E {{ Φ }}  WP e @ s; E {{ Ψ }}.
313
Proof.
314
  iIntros (HΦ) "H"; iApply (wp_strong_mono s E E); auto.
315
  iIntros "{$H}" (v) "?". by iApply HΦ.
316
Qed.
David Swasey's avatar
David Swasey committed
317
Lemma wp_stuck_mono s1 s2 E e Φ :
Ralf Jung's avatar
Ralf Jung committed
318
  stuckness_le s1 s2  WP e @ s1; E {{ Φ }}  WP e @ s2; E {{ Φ }}.
David Swasey's avatar
David Swasey committed
319
Proof. case: s1; case: s2 => // _. exact: wp_stuck_weaken. Qed.
320 321 322 323
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).
324
Proof. by intros Φ Φ' ?; apply wp_mono. Qed.
325

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

334 335 336 337 338 339
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.

Lemma wp_frame_step_l s E1 E2 e Φ R :
340
  to_val e = None  E2  E1 
341
  (|={E1,E2}=> R)  WP e @ s; E2 {{ Φ }}  WP e @ s; E1 {{ v, R  Φ v }}.
342
Proof.
343
  iIntros (??) "[Hu Hwp]". iApply (wp_step_fupd with "Hu"); try done.
344 345
  iApply (wp_mono with "Hwp"). by iIntros (?) "$$".
Qed.
346
Lemma wp_frame_step_r s E1 E2 e Φ R :
347
  to_val e = None  E2  E1 
348
  WP e @ s; E2 {{ Φ }}  (|={E1,E2}=> R)  WP e @ s; E1 {{ v, Φ v  R }}.
Ralf Jung's avatar
Ralf Jung committed
349
Proof.
350
  rewrite [(WP _ @ _; _ {{ _ }}  _)%I]comm; setoid_rewrite (comm _ _ R).
351
  apply wp_frame_step_l.
Ralf Jung's avatar
Ralf Jung committed
352
Qed.
353 354 355 356 357 358 359 360 361
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 {{ Ψ }}.
362
Proof.
363
  iIntros "Hwp H". iApply (wp_strong_mono s E); auto.
364
  iIntros "{$Hwp}" (?) "?". by iApply "H".
365
Qed.
366 367
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
368
Proof. iIntros "[H Hwp]". iApply (wp_wand with "Hwp H"). Qed.
369 370
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
371
Proof. iIntros "[Hwp H]". iApply (wp_wand with "Hwp H"). Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
372
End wp.
373 374 375 376 377 378 379

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

380 381
  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 {{ Ψ }}).
382 383
  Proof. rewrite /Frame=> HR. rewrite wp_frame_l. apply wp_mono, HR. Qed.

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

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

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

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