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

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

Ralf Jung's avatar
Ralf Jung committed
13
Inductive stuckness := NotStuck | MaybeStuck.
Ralf Jung's avatar
Ralf Jung committed
14

15
Definition stuckness_leb (s1 s2 : stuckness) : bool :=
Ralf Jung's avatar
Ralf Jung committed
16
  match s1, s2 with
Ralf Jung's avatar
Ralf Jung committed
17
  | MaybeStuck, NotStuck => false
Ralf Jung's avatar
Ralf Jung committed
18 19
  | _, _ => true
  end.
20 21 22
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
23

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

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

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

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

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

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

70 71
Notation "'WP' e @ s ; E {{ v , Q } }" := (wp s E e%E (λ v, Q))
  (at level 20, e, Q at level 200,
72
   format "'[' 'WP'  e  '/' @  s ;  E  {{  v ,  Q  } } ']'") : bi_scope.
Ralf Jung's avatar
Ralf Jung committed
73
Notation "'WP' e @ E {{ v , Q } }" := (wp NotStuck E e%E (λ v, Q))
74
  (at level 20, e, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
75
   format "'[' 'WP'  e  '/' @  E  {{  v ,  Q  } } ']'") : bi_scope.
Ralf Jung's avatar
Ralf Jung committed
76
Notation "'WP' e @ E ? {{ v , Q } }" := (wp MaybeStuck E e%E (λ v, Q))
77
  (at level 20, e, Q at level 200,
78
   format "'[' 'WP'  e  '/' @  E  ? {{  v ,  Q  } } ']'") : bi_scope.
Ralf Jung's avatar
Ralf Jung committed
79
Notation "'WP' e {{ v , Q } }" := (wp NotStuck  e%E (λ v, Q))
80
  (at level 20, e, Q at level 200,
Robbert Krebbers's avatar
Robbert Krebbers committed
81
   format "'[' 'WP'  e  '/' {{  v ,  Q  } } ']'") : bi_scope.
Ralf Jung's avatar
Ralf Jung committed
82
Notation "'WP' e ? {{ v , Q } }" := (wp MaybeStuck  e%E (λ v, Q))
83
  (at level 20, e, Q at level 200,
84
   format "'[' 'WP'  e  '/' ? {{  v ,  Q  } } ']'") : bi_scope.
85

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

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

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

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

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

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

213 214
Lemma wp_strong_mono s1 s2 E1 E2 e Φ Ψ :
  s1  s2  E1  E2 
215
  WP e @ s1; E1 {{ Φ }} - ( v, Φ v ={E2}= Ψ v) - WP e @ s2; E2 {{ Ψ }}.
216
Proof.
217
  iIntros (? HE) "H HΦ". iLöb as "IH" forall (e E1 E2 HE Φ Ψ).
218
  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 223 224 225
  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".
226
  - iApply ("IH" with "[//] H HΦ").
227
  - iApply (big_sepL_impl with "[$Hefs]"); iIntros "!#" (k ef _) "H".
228
    by iApply ("IH" with "[] H").
229 230 231
Qed.

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

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

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

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

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

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

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

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

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

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

373
  Global Instance frame_wp p s E e R Φ Ψ :
374 375 376
    ( v, Frame p R (Φ v) (Ψ v)) 
    KnownFrame p R (WP e @ s; E {{ Φ }}) (WP e @ s; E {{ Ψ }}).
  Proof. rewrite /KnownFrame /Frame=> HR. rewrite wp_frame_l. apply wp_mono, HR. Qed.
377

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

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

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

389
  Global Instance elim_modal_fupd_wp_atomic s E1 E2 e P Φ :
Ralf Jung's avatar
Ralf Jung committed
390
    Atomic (stuckness_to_atomicity s) e 
391
    ElimModal True (|={E1,E2}=> P) P
392
            (WP e @ s; E1 {{ Φ }}) (WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})%I.
393
  Proof. intros. by rewrite /ElimModal fupd_frame_r wand_elim_r wp_atomic. Qed.
394 395 396 397

  Global Instance add_modal_fupd_wp s E e P Φ :
    AddModal (|={E}=> P) P (WP e @ s; E {{ Φ }}).
  Proof. by rewrite /AddModal fupd_frame_r wand_elim_r fupd_wp. Qed.
398
End proofmode_classes.