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

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

26 27 28
Local Instance wp_pre_contractive `{irisG Λ Σ} : Contractive wp_pre.
Proof.
  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 Λ Σ} :
  coPset  expr Λ  (val Λ  iProp Σ)  iProp Σ := fixpoint wp_pre.
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

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

Janno's avatar
Janno committed
41
Notation "'WP' e @ E {{ Φ } }" := (wp E e%E Φ)
42
  (at level 20, e, Φ at level 200,
43
   format "'[' 'WP'  e  '/' @  E  {{  Φ  } } ']'") : uPred_scope.
Janno's avatar
Janno committed
44
Notation "'WP' e {{ Φ } }" := (wp  e%E Φ)
45
  (at level 20, e, Φ at level 200,
46
   format "'[' 'WP'  e  '/' {{  Φ  } } ']'") : uPred_scope.
47

Janno's avatar
Janno committed
48
Notation "'WP' e @ E {{ v , Q } }" := (wp E e%E (λ v, Q))
49
  (at level 20, e, Q at level 200,
50
   format "'[' 'WP'  e  '/' @  E  {{  v ,  Q  } } ']'") : uPred_scope.
Janno's avatar
Janno committed
51
Notation "'WP' e {{ v , Q } }" := (wp  e%E (λ v, Q))
52
  (at level 20, e, Q at level 200,
53
   format "'[' 'WP'  e  '/' {{  v ,  Q  } } ']'") : uPred_scope.
54

Ralf Jung's avatar
Ralf Jung committed
55
(* Texan triples *)
56
Notation "'{{{' P } } } e @ E {{{ x .. y , 'RET' pat ; Q } } }" :=
Ralf Jung's avatar
Ralf Jung committed
57
  (  Φ,
58
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
59
    (at level 20, x closed binder, y closed binder,
60
     format "{{{  P  } } }  e  @  E  {{{  x .. y ,  RET  pat ;  Q } } }") : uPred_scope.
61 62 63 64 65
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.
66
Notation "'{{{' P } } } e @ E {{{ 'RET' pat ; Q } } }" :=
67
  (  Φ, P -  (Q - Φ pat%V) - WP e @ E {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
68
    (at level 20,
69
     format "{{{  P  } } }  e  @  E  {{{  RET  pat ;  Q } } }") : uPred_scope.
70 71 72 73
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
74

75
Notation "'{{{' P } } } e @ E {{{ x .. y , 'RET' pat ; Q } } }" :=
76
  ( Φ : _  uPred _,
77
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
78
    (at level 20, x closed binder, y closed binder,
79
     format "{{{  P  } } }  e  @  E  {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
80 81 82 83 84
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.
85
Notation "'{{{' P } } } e @ E {{{ 'RET' pat ; Q } } }" :=
86
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
87
    (at level 20,
88
     format "{{{  P  } } }  e  @  E  {{{  RET  pat ;  Q } } }") : C_scope.
89 90 91 92
Notation "'{{{' P } } } e {{{ 'RET' pat ; Q } } }" :=
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e {{ Φ }})
    (at level 20,
     format "{{{  P  } } }  e  {{{  RET  pat ;  Q } } }") : C_scope.
93

Robbert Krebbers's avatar
Robbert Krebbers committed
94
Section wp.
95 96 97
Context `{irisG Λ Σ}.
Implicit Types P : iProp Σ.
Implicit Types Φ : val Λ  iProp Σ.
98 99
Implicit Types v : val Λ.
Implicit Types e : expr Λ.
Robbert Krebbers's avatar
Robbert Krebbers committed
100

101
(* Weakest pre *)
102 103 104
Lemma wp_unfold E e Φ : WP e @ E {{ Φ }}  wp_pre wp E e Φ.
Proof. rewrite wp_eq. apply (fixpoint_unfold wp_pre). Qed.

105
Global Instance wp_ne E e n :
106
  Proper (pointwise_relation _ (dist n) ==> dist n) (@wp Λ Σ _ E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
107
Proof.
108
  revert e. induction (lt_wf n) as [n _ IH]=> e Φ Ψ HΦ.
109 110 111 112
  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
113
  do 17 (f_contractive || f_equiv). apply IH; first lia.
114
  intros v. eapply dist_le; eauto with omega.
Robbert Krebbers's avatar
Robbert Krebbers committed
115 116
Qed.
Global Instance wp_proper E e :
117
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ _ E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
118
Proof.
119
  by intros Φ Φ' ?; apply equiv_dist=>n; apply wp_ne=>v; apply equiv_dist.
Robbert Krebbers's avatar
Robbert Krebbers committed
120
Qed.
121

122
Lemma wp_value' E Φ v : Φ v  WP of_val v @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
123
Proof. iIntros "HΦ". rewrite wp_unfold /wp_pre to_of_val. auto. Qed.
124
Lemma wp_value_inv E Φ v : WP of_val v @ E {{ Φ }} ={E}= Φ v.
Robbert Krebbers's avatar
Robbert Krebbers committed
125
Proof. by rewrite wp_unfold /wp_pre to_of_val. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
126

127
Lemma wp_strong_mono E1 E2 e Φ Ψ :
128
  E1  E2  ( v, Φ v ={E2}= Ψ v)  WP e @ E1 {{ Φ }}  WP e @ E2 {{ Ψ }}.
129
Proof.
130
  iIntros (?) "[HΦ H]". iLöb as "IH" forall (e). rewrite !wp_unfold /wp_pre.
Robbert Krebbers's avatar
Robbert Krebbers committed
131
  destruct (to_val e) as [v|] eqn:?.
132
  { iApply ("HΦ" with "[> -]"). by iApply (fupd_mask_mono E1 _). }
Robbert Krebbers's avatar
Robbert Krebbers committed
133
  iIntros (σ1) "Hσ". iMod (fupd_intro_mask' E2 E1) as "Hclose"; first done.
134
  iMod ("H" with "[$]") as "[$ H]".
135
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
136
  iMod ("H" with "[//]") as "($ & H & $)"; auto.
137
  iMod "Hclose" as "_". by iApply ("IH" with "HΦ").
138 139
Qed.

140
Lemma fupd_wp E e Φ : (|={E}=> WP e @ E {{ Φ }})  WP e @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
141
Proof.
142
  rewrite wp_unfold /wp_pre. iIntros "H". destruct (to_val e) as [v|] eqn:?.
Robbert Krebbers's avatar
Robbert Krebbers committed
143 144
  { by iMod "H". }
  iIntros (σ1) "Hσ1". iMod "H". by iApply "H".
145
Qed.
146
Lemma wp_fupd E e Φ : WP e @ E {{ v, |={E}=> Φ v }}  WP e @ E {{ Φ }}.
147
Proof. iIntros "H". iApply (wp_strong_mono E); try iFrame; auto. Qed.
148

149
Lemma wp_atomic E1 E2 e Φ :
150
  atomic e 
151
  (|={E1,E2}=> WP e @ E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ E1 {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
152
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
153 154 155 156
  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]".
157
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
158
  iMod ("H" with "[//]") as "(Hphy & H & $)".
Robbert Krebbers's avatar
Robbert Krebbers committed
159
  rewrite !wp_unfold /wp_pre. destruct (to_val e2) as [v2|] eqn:He2.
160 161
  - iDestruct "H" as ">> $". eauto with iFrame.
  - iMod ("H" with "[$]") as "[H _]".
Robbert Krebbers's avatar
Robbert Krebbers committed
162
    iDestruct "H" as %(? & ? & ? & ?). by edestruct (Hatomic _ _ _ _ Hstep).
Robbert Krebbers's avatar
Robbert Krebbers committed
163
Qed.
164

165
Lemma wp_step_fupd E1 E2 e P Φ :
166
  to_val e = None  E2  E1 
167
  (|={E1,E2}=> P) - WP e @ E2 {{ v, P ={E1}= Φ v }} - WP e @ E1 {{ Φ }}.
168
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
169
  rewrite !wp_unfold /wp_pre. iIntros (-> ?) "HR H".
170
  iIntros (σ1) "Hσ". iMod "HR". iMod ("H" with "[$]") as "[$ H]".
171
  iModIntro; iNext; iIntros (e2 σ2 efs Hstep).
172
  iMod ("H" $! e2 σ2 efs with "[% //]") as "($ & H & $)".
173 174
  iMod "HR". iModIntro. iApply (wp_strong_mono E2); first done.
  iSplitR "H"; last iExact "H". iIntros (v) "H". by iApply "H".
Robbert Krebbers's avatar
Robbert Krebbers committed
175
Qed.
176

177
Lemma wp_bind K `{!LanguageCtx Λ K} E e Φ :
178
  WP e @ E {{ v, WP K (of_val v) @ E {{ Φ }} }}  WP K e @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
179
Proof.
180
  iIntros "H". iLöb as "IH" forall (E e Φ). rewrite wp_unfold /wp_pre.
Robbert Krebbers's avatar
Robbert Krebbers committed
181 182 183
  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 //.
184
  iIntros (σ1) "Hσ". iMod ("H" with "[$]") as "[% H]". iModIntro; iSplit.
185
  { iPureIntro. unfold reducible in *. naive_solver eauto using fill_step. }
186 187
  iNext; iIntros (e2 σ2 efs Hstep).
  destruct (fill_step_inv e σ1 e2 σ2 efs) as (e2'&->&?); auto.
188
  iMod ("H" $! e2' σ2 efs with "[//]") as "($ & H & $)".
189
  by iApply "IH".
Robbert Krebbers's avatar
Robbert Krebbers committed
190 191
Qed.

192 193 194 195 196 197 198 199 200 201 202 203 204 205
Lemma wp_bind_inv K `{!LanguageCtx Λ K} E e Φ :
  WP K e @ E {{ Φ }}  WP e @ E {{ v, WP K (of_val v) @ E {{ Φ }} }}.
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.
  { eauto using reducible_fill. }
  iNext; iIntros (e2 σ2 efs Hstep).
  iMod ("H" $! (K e2) σ2 efs with "[]") as "($ & H & $)"; eauto using fill_step.
  by iApply "IH".
Qed.

206
(** * Derived rules *)
207
Lemma wp_mono E e Φ Ψ : ( v, Φ v  Ψ v)  WP e @ E {{ Φ }}  WP e @ E {{ Ψ }}.
208 209
Proof.
  iIntros (HΦ) "H"; iApply (wp_strong_mono E E); auto.
210
  iIntros "{$H}" (v) "?". by iApply HΦ.
211 212 213
Qed.
Lemma wp_mask_mono E1 E2 e Φ : E1  E2  WP e @ E1 {{ Φ }}  WP e @ E2 {{ Φ }}.
Proof. iIntros (?) "H"; iApply (wp_strong_mono E1 E2); auto. iFrame; eauto. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
214
Global Instance wp_mono' E e :
215
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ _ E e).
216
Proof. by intros Φ Φ' ?; apply wp_mono. Qed.
217

218
Lemma wp_value E Φ e v `{!IntoVal e v} : Φ v  WP e @ E {{ Φ }}.
219
Proof. intros; rewrite -(of_to_val e v) //; by apply wp_value'. Qed.
220 221
Lemma wp_value_fupd' E Φ v : (|={E}=> Φ v)  WP of_val v @ E {{ Φ }}.
Proof. intros. by rewrite -wp_fupd -wp_value'. Qed.
222
Lemma wp_value_fupd E Φ e v `{!IntoVal e v} : (|={E}=> Φ v)  WP e @ E {{ Φ }}.
223
Proof. intros. rewrite -wp_fupd -wp_value //. Qed.
224

225
Lemma wp_frame_l E e Φ R : R  WP e @ E {{ Φ }}  WP e @ E {{ v, R  Φ v }}.
226
Proof. iIntros "[??]". iApply (wp_strong_mono E E _ Φ); try iFrame; eauto. Qed.
227
Lemma wp_frame_r E e Φ R : WP e @ E {{ Φ }}  R  WP e @ E {{ v, Φ v  R }}.
228 229
Proof. iIntros "[??]". iApply (wp_strong_mono E E _ Φ); try iFrame; eauto. Qed.

230 231 232 233
Lemma wp_frame_step_l E1 E2 e Φ R :
  to_val e = None  E2  E1 
  (|={E1,E2}=> R)  WP e @ E2 {{ Φ }}  WP e @ E1 {{ v, R  Φ v }}.
Proof.
234
  iIntros (??) "[Hu Hwp]". iApply (wp_step_fupd with "Hu"); try done.
235 236
  iApply (wp_mono with "Hwp"). by iIntros (?) "$$".
Qed.
237 238
Lemma wp_frame_step_r E1 E2 e Φ R :
  to_val e = None  E2  E1 
239
  WP e @ E2 {{ Φ }}  (|={E1,E2}=> R)  WP e @ E1 {{ v, Φ v  R }}.
Ralf Jung's avatar
Ralf Jung committed
240
Proof.
241
  rewrite [(WP _ @ _ {{ _ }}  _)%I]comm; setoid_rewrite (comm _ _ R).
242
  apply wp_frame_step_l.
Ralf Jung's avatar
Ralf Jung committed
243 244
Qed.
Lemma wp_frame_step_l' E e Φ R :
245
  to_val e = None   R  WP e @ E {{ Φ }}  WP e @ E {{ v, R  Φ v }}.
246 247
Proof. iIntros (?) "[??]". iApply (wp_frame_step_l E E); try iFrame; eauto. Qed.
Lemma wp_frame_step_r' E e Φ R :
248
  to_val e = None  WP e @ E {{ Φ }}   R  WP e @ E {{ v, Φ v  R }}.
249 250
Proof. iIntros (?) "[??]". iApply (wp_frame_step_r E E); try iFrame; eauto. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
251
Lemma wp_wand E e Φ Ψ :
252
  WP e @ E {{ Φ }} - ( v, Φ v - Ψ v) - WP e @ E {{ Ψ }}.
253
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
254
  iIntros "Hwp H". iApply (wp_strong_mono E); auto.
255
  iIntros "{$Hwp}" (?) "?". by iApply "H".
256
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
257 258 259
Lemma wp_wand_l E e Φ Ψ :
  ( v, Φ v - Ψ v)  WP e @ E {{ Φ }}  WP e @ E {{ Ψ }}.
Proof. iIntros "[H Hwp]". iApply (wp_wand with "Hwp H"). Qed.
260
Lemma wp_wand_r E e Φ Ψ :
261
  WP e @ E {{ Φ }}  ( v, Φ v - Ψ v)  WP e @ E {{ Ψ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
262
Proof. iIntros "[Hwp H]". iApply (wp_wand with "Hwp H"). Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
263
End wp.
264 265 266 267 268 269 270

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

271 272
  Global Instance frame_wp p E e R Φ Ψ :
    ( v, Frame p R (Φ v) (Ψ v))  Frame p R (WP e @ E {{ Φ }}) (WP e @ E {{ Ψ }}).
273 274
  Proof. rewrite /Frame=> HR. rewrite wp_frame_l. apply wp_mono, HR. Qed.

275 276
  Global Instance is_except_0_wp E e Φ : IsExcept0 (WP e @ E {{ Φ }}).
  Proof. by rewrite /IsExcept0 -{2}fupd_wp -except_0_fupd -fupd_intro. Qed.
277

278 279 280
  Global Instance elim_modal_bupd_wp E e P Φ :
    ElimModal (|==> P) P (WP e @ E {{ Φ }}) (WP e @ E {{ Φ }}).
  Proof. by rewrite /ElimModal (bupd_fupd E) fupd_frame_r wand_elim_r fupd_wp. Qed.
281

282 283 284
  Global Instance elim_modal_fupd_wp E e P Φ :
    ElimModal (|={E}=> P) P (WP e @ E {{ Φ }}) (WP e @ E {{ Φ }}).
  Proof. by rewrite /ElimModal fupd_frame_r wand_elim_r fupd_wp. Qed.
285

286 287
  (* lower precedence, if possible, it should always pick elim_upd_fupd_wp *)
  Global Instance elim_modal_fupd_wp_atomic E1 E2 e P Φ :
288
    atomic e 
289
    ElimModal (|={E1,E2}=> P) P
290
            (WP e @ E1 {{ Φ }}) (WP e @ E2 {{ v, |={E2,E1}=> Φ v }})%I | 100.
291
  Proof. intros. by rewrite /ElimModal fupd_frame_r wand_elim_r wp_atomic. Qed.
292
End proofmode_classes.
293

294
Hint Extern 0 (atomic _) => assumption : typeclass_instances.