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

David Swasey's avatar
David Swasey committed
227
Lemma wp_stuck_weaken s E e Φ :
228
229
230
231
232
233
234
235
236
237
  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)".
Jacques-Henri Jourdan's avatar
Jacques-Henri Jourdan committed
238
  iSplitL "Hef". by iApply ("IH" with "Hef"). exact: IH.
239
240
241
Qed.

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

Ralf Jung's avatar
Ralf Jung committed
250
Lemma wp_atomic s E1 E2 e Φ `{!Atomic (stuckness_to_atomicity s) e} :
251
252
  (|={E1,E2}=> WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ s; E1 {{ Φ }}.
Proof.
David Swasey's avatar
David Swasey committed
253
  iIntros "H". rewrite !wp_unfold /wp_pre.
254
255
256
  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
257
258
259
260
261
  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
262
263
      by edestruct (atomic _ _ _ _ Hstep).
  - destruct (atomic _ _ _ _ Hstep) as [v <-%of_to_val].
David Swasey's avatar
David Swasey committed
264
265
    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
266
Qed.
267

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

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

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

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

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

333
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.

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

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

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

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

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

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

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