weakestpre.v 18.3 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 := NotStuck | MaybeStuck.
Ralf Jung's avatar
Ralf Jung committed
15

16
Definition stuckness_leb (s1 s2 : stuckness) : bool :=
Ralf Jung's avatar
Ralf Jung committed
17
  match s1, s2 with
Ralf Jung's avatar
Ralf Jung committed
18
  | MaybeStuck, NotStuck => false
Ralf Jung's avatar
Ralf Jung committed
19 20
  | _, _ => true
  end.
21 22 23
Instance stuckness_le : SqSubsetEq stuckness := stuckness_leb.
Instance stuckness_le_po : PreOrder stuckness_le.
Proof. split; by repeat intros []. Qed.
Ralf Jung's avatar
Ralf Jung committed
24

Ralf Jung's avatar
Ralf Jung committed
25
Definition stuckness_to_atomicity (s : stuckness) : atomicity :=
Ralf Jung's avatar
Ralf Jung committed
26
  if s is MaybeStuck then StronglyAtomic else WeaklyAtomic.
Ralf Jung's avatar
Ralf Jung committed
27

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

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

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

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

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

71 72 73
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.
Ralf Jung's avatar
Ralf Jung committed
74
Notation "'WP' e @ E {{ v , Q } }" := (wp NotStuck E e%E (λ v, Q))
75
  (at level 20, e, Q at level 200,
76
   format "'[' 'WP'  e  '/' @  E  {{  v ,  Q  } } ']'") : uPred_scope.
Ralf Jung's avatar
Ralf Jung committed
77
Notation "'WP' e @ E ? {{ v , Q } }" := (wp MaybeStuck E e%E (λ v, Q))
78 79
  (at level 20, e, Q at level 200,
   format "'[' 'WP'  e  '/' @  E  ? {{  v ,  Q  } } ']'") : uPred_scope.
Ralf Jung's avatar
Ralf Jung committed
80
Notation "'WP' e {{ v , Q } }" := (wp NotStuck  e%E (λ v, Q))
81
  (at level 20, e, Q at level 200,
82
   format "'[' 'WP'  e  '/' {{  v ,  Q  } } ']'") : uPred_scope.
Ralf Jung's avatar
Ralf Jung committed
83
Notation "'WP' e ? {{ v , Q } }" := (wp MaybeStuck  e%E (λ v, Q))
84 85
  (at level 20, e, Q at level 200,
   format "'[' 'WP'  e  '/' ? {{  v ,  Q  } } ']'") : uPred_scope.
86

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

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

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

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

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

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

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

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

Ralf Jung's avatar
Ralf Jung committed
241
Lemma wp_atomic s E1 E2 e Φ `{!Atomic (stuckness_to_atomicity s) e} :
242 243
  (|={E1,E2}=> WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ s; E1 {{ Φ }}.
Proof.
David Swasey's avatar
David Swasey committed
244
  iIntros "H". rewrite !wp_unfold /wp_pre.
245 246 247
  destruct (to_val e) as [v|] eqn:He.
  { by iDestruct "H" as ">>> $". }
  iIntros (σ1) "Hσ". iMod "H". iMod ("H" $! σ1 with "Hσ") as "[$ H]".
248 249 250
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
  iMod ("H" with "[//]") as "(Hphy & H & $)". destruct s.
  - rewrite !wp_unfold /wp_pre. destruct (to_val e2) as [v2|] eqn:He2.
David Swasey's avatar
David Swasey committed
251 252
    + iDestruct "H" as ">> $". by iFrame.
    + iMod ("H" with "[$]") as "[H _]". iDestruct "H" as %(? & ? & ? & ?).
Ralf Jung's avatar
Ralf Jung committed
253 254
      by edestruct (atomic _ _ _ _ Hstep).
  - destruct (atomic _ _ _ _ Hstep) as [v <-%of_to_val].
255
    iMod (wp_value_inv with "H") as ">H". iFrame "Hphy". by iApply wp_value'.
Robbert Krebbers's avatar
Robbert Krebbers committed
256
Qed.
257

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

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

286
Lemma wp_bind_inv K `{!LanguageCtx K} s E e Φ :
287
  WP K e @ s; E {{ Φ }}  WP e @ s; E {{ v, WP K (of_val v) @ s; E {{ Φ }} }}.
288 289 290 291 292 293
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.
294
  { destruct s; eauto using reducible_fill. }
295 296 297 298 299
  iNext; iIntros (e2 σ2 efs Hstep).
  iMod ("H" $! (K e2) σ2 efs with "[]") as "($ & H & $)"; eauto using fill_step.
  by iApply "IH".
Qed.

300
(** * Derived rules *)
301
Lemma wp_mono s E e Φ Ψ : ( v, Φ v  Ψ v)  WP e @ s; E {{ Φ }}  WP e @ s; E {{ Ψ }}.
302
Proof.
303
  iIntros (HΦ) "H"; iApply (wp_strong_mono s s E E); auto.
304
  iIntros "{$H}" (v) "?". by iApply HΦ.
305
Qed.
David Swasey's avatar
David Swasey committed
306
Lemma wp_stuck_mono s1 s2 E e Φ :
307
  s1  s2  WP e @ s1; E {{ Φ }}  WP e @ s2; E {{ Φ }}.
308 309 310 311
Proof. iIntros (?) "H". iApply (wp_strong_mono s1 s2); auto with iFrame. Qed.
Lemma wp_stuck_weaken s E e Φ :
  WP e @ s; E {{ Φ }}  WP e @ E ?{{ Φ }}.
Proof. apply wp_stuck_mono. by destruct s. Qed.
312
Lemma wp_mask_mono s E1 E2 e Φ : E1  E2  WP e @ s; E1 {{ Φ }}  WP e @ s; E2 {{ Φ }}.
313
Proof. iIntros (?) "H"; iApply (wp_strong_mono s s E1 E2); auto. iFrame; eauto. Qed.
314 315
Global Instance wp_mono' s E e :
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ _ s E e).
316
Proof. by intros Φ Φ' ?; apply wp_mono. Qed.
317

318
Lemma wp_value s E Φ e v `{!IntoVal e v} : Φ v  WP e @ s; E {{ Φ }}.
319
Proof. intros; rewrite -(of_to_val e v) //; by apply wp_value'. Qed.
320
Lemma wp_value_fupd' s E Φ v : (|={E}=> Φ v)  WP of_val v @ s; E {{ Φ }}.
321
Proof. intros. by rewrite -wp_fupd -wp_value'. Qed.
322 323
Lemma wp_value_fupd s E Φ e v `{!IntoVal e v} :
  (|={E}=> Φ v)  WP e @ s; E {{ Φ }}.
324
Proof. intros. rewrite -wp_fupd -wp_value //. Qed.
325

326
Lemma wp_frame_l s E e Φ R : R  WP e @ s; E {{ Φ }}  WP e @ s; E {{ v, R  Φ v }}.
327
Proof. iIntros "[??]". iApply (wp_strong_mono s s E E _ Φ); try iFrame; eauto. Qed.
328
Lemma wp_frame_r s E e Φ R : WP e @ s; E {{ Φ }}  R  WP e @ s; E {{ v, Φ v  R }}.
329
Proof. iIntros "[??]". iApply (wp_strong_mono s s E E _ Φ); try iFrame; eauto. Qed.
330 331

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

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

372 373
  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 {{ Ψ }}).
374 375
  Proof. rewrite /Frame=> HR. rewrite wp_frame_l. apply wp_mono, HR. Qed.

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

379 380
  Global Instance elim_modal_bupd_wp s E e P Φ :
    ElimModal (|==> P) P (WP e @ s; E {{ Φ }}) (WP e @ s; E {{ Φ }}).
381
  Proof. by rewrite /ElimModal (bupd_fupd E) fupd_frame_r wand_elim_r fupd_wp. Qed.
382

383 384
  Global Instance elim_modal_fupd_wp s E e P Φ :
    ElimModal (|={E}=> P) P (WP e @ s; E {{ Φ }}) (WP e @ s; E {{ Φ }}).
385
  Proof. by rewrite /ElimModal fupd_frame_r wand_elim_r fupd_wp. Qed.
386

387
  (* lower precedence, if possible, it should persistently pick elim_upd_fupd_wp *)
388
  Global Instance elim_modal_fupd_wp_atomic s E1 E2 e P Φ :
Ralf Jung's avatar
Ralf Jung committed
389
    Atomic (stuckness_to_atomicity s) e 
390
    ElimModal (|={E1,E2}=> P) P
391
            (WP e @ s; E1 {{ Φ }}) (WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})%I | 100.
392
  Proof. intros. by rewrite /ElimModal fupd_frame_r wand_elim_r wp_atomic. Qed.
393
End proofmode_classes.