weakestpre.v 12.9 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 16 17
Definition wp_pre `{irisG Λ Σ}
    (wp : coPset -c> expr Λ -c> (val Λ -c> iProp Σ) -c> iProp Σ) :
    coPset -c> expr Λ -c> (val Λ -c> iProp Σ) -c> iProp Σ := λ E e1 Φ, (
  (* value case *)
Ralf Jung's avatar
Ralf Jung committed
18
  ( v, to_val e1 = Some v  |={E}=> Φ v) 
19
  (* step case *)
Ralf Jung's avatar
Ralf Jung committed
20
  (to_val e1 = None   σ1,
21
     state_interp σ1 ={E,}= reducible e1 σ1 
Ralf Jung's avatar
Ralf Jung committed
22
       e2 σ2 efs, prim_step e1 σ1 e2 σ2 efs ={,E}=
23
       state_interp σ2  wp E e2 Φ 
24
       [ list] ef  efs, wp  ef (λ _, True)))%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.
Ralf Jung's avatar
Ralf Jung committed
34 35 36 37
Definition wp_aux : { x | x = @wp_def }. by eexists. Qed.
Definition wp := proj1_sig wp_aux.
Definition wp_eq : @wp = @wp_def := proj2_sig wp_aux.

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 50
  (at level 20, e, Q at level 200,
   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 53 54
  (at level 20, e, Q at level 200,
   format "'WP'  e  {{  v ,  Q  } }") : uPred_scope.

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

75
Notation "'{{{' P } } } e {{{ x .. y , 'RET' pat ; Q } } }" :=
76
  ( Φ : _  uPred _,
77
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e {{ Φ }})
78
    (at level 20, x closed binder, y closed binder,
79 80
     format "{{{  P  } } }  e  {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
Notation "'{{{' P } } } e @ E {{{ x .. y , 'RET' pat ; Q } } }" :=
81
  ( Φ : _  uPred _,
82
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
83
    (at level 20, x closed binder, y closed binder,
84 85
     format "{{{  P  } } }  e  @  E  {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
Notation "'{{{' P } } } e {{{ 'RET' pat ; Q } } }" :=
86
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e {{ Φ }})
87
    (at level 20,
88 89
     format "{{{  P  } } }  e  {{{  RET  pat ;  Q } } }") : C_scope.
Notation "'{{{' P } } } e @ E {{{ 'RET' pat ; Q } } }" :=
90
  ( Φ : _  uPred _, P -  (Q - Φ pat%V) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
91
    (at level 20,
92
     format "{{{  P  } } }  e  @  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 113 114
  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 *)
  do 18 (f_contractive || f_equiv). apply IH; first lia.
  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.
124 125
  iIntros "HΦ". rewrite wp_unfold /wp_pre.
  iLeft; iExists v; rewrite to_of_val; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
126
Qed.
127
Lemma wp_value_inv E Φ v : WP of_val v @ E {{ Φ }} ={E}= Φ v.
Ralf Jung's avatar
Ralf Jung committed
128
Proof.
129 130
  rewrite wp_unfold /wp_pre to_of_val. iIntros "[H|[% _]]"; [|done].
  by iDestruct "H" as (v') "[% ?]"; simplify_eq.
Ralf Jung's avatar
Ralf Jung committed
131
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
132

133
Lemma wp_strong_mono E1 E2 e Φ Ψ :
134
  E1  E2  ( v, Φ v ={E2}= Ψ v)  WP e @ E1 {{ Φ }}  WP e @ E2 {{ Ψ }}.
135
Proof.
136
  iIntros (?) "[HΦ H]". iLöb as "IH" forall (e). rewrite !wp_unfold /wp_pre.
137 138
  iDestruct "H" as "[Hv|[% H]]"; [iLeft|iRight].
  { iDestruct "Hv" as (v) "[% Hv]". iExists v; iSplit; first done.
139
    iApply ("HΦ" with ">[-]"). by iApply (fupd_mask_mono E1 _). }
140
  iSplit; [done|]; iIntros (σ1) "Hσ".
141 142 143 144 145
  iMod (fupd_intro_mask' E2 E1) as "Hclose"; first done.
  iMod ("H" $! σ1 with "Hσ") as "[$ H]".
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
  iMod ("H" $! _ σ2 efs with "[#]") as "($ & H & $)"; auto.
  iMod "Hclose" as "_". by iApply ("IH" with "HΦ").
146 147
Qed.

148
Lemma fupd_wp E e Φ : (|={E}=> WP e @ E {{ Φ }})  WP e @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
149
Proof.
150 151
  rewrite wp_unfold /wp_pre. iIntros "H". destruct (to_val e) as [v|] eqn:?.
  { iLeft. iExists v; iSplit; first done.
152
    by iMod "H" as "[H|[% ?]]"; [iDestruct "H" as (v') "[% ?]"|]; simplify_eq. }
153
  iRight; iSplit; [done|]; iIntros (σ1) "Hσ1".
154
  iMod "H" as "[H|[% H]]"; last by iApply "H".
155
  iDestruct "H" as (v') "[% ?]"; simplify_eq.
156
Qed.
157
Lemma wp_fupd E e Φ : WP e @ E {{ v, |={E}=> Φ v }}  WP e @ E {{ Φ }}.
158
Proof. iIntros "H". iApply (wp_strong_mono E); try iFrame; auto. Qed.
159

160
Lemma wp_atomic E1 E2 e Φ :
161
  atomic e 
162
  (|={E1,E2}=> WP e @ E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ E1 {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
163
Proof.
164
  iIntros (Hatomic) "H". destruct (to_val e) as [v|] eqn:He.
165
  { apply of_to_val in He as <-. iApply wp_fupd. iApply wp_value'.
166
    iMod "H". by iMod (wp_value_inv with "H"). }
167
  setoid_rewrite wp_unfold; rewrite /wp_pre. iRight; iSplit; auto.
168
  iIntros (σ1) "Hσ". iMod "H" as "[H|[_ H]]".
169
  { iDestruct "H" as (v') "[% ?]"; simplify_eq. }
170 171
  iMod ("H" $! σ1 with "Hσ") as "[$ H]".
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
172 173 174 175 176 177 178 179
  iMod ("H" with "* []") as "(Hphy & H & $)"; first done.
  rewrite wp_unfold /wp_pre. iDestruct "H" as "[H|H]".
  - iDestruct "H" as (v) "[% >>?]". iModIntro. iFrame.
    rewrite -(of_to_val e2 v) //. by iApply wp_value'.
  - iDestruct "H" as "[_ H]".
    iMod ("H" with "* Hphy") as "[H _]".
    iDestruct "H" as %(? & ? & ? & ?). exfalso.
    by eapply (Hatomic _ _ _ _ Hstep).
Robbert Krebbers's avatar
Robbert Krebbers committed
180
Qed.
181

182
Lemma wp_fupd_step E1 E2 e P Φ :
183
  to_val e = None  E2  E1 
184
  (|={E1,E2}=> P) - WP e @ E2 {{ v, P ={E1}= Φ v }} - WP e @ E1 {{ Φ }}.
185
Proof.
186
  rewrite !wp_unfold /wp_pre. iIntros (??) "HR [Hv|[_ H]]".
187 188
  { iDestruct "Hv" as (v) "[% Hv]"; simplify_eq. }
  iRight; iSplit; [done|]. iIntros (σ1) "Hσ".
189 190 191
  iMod "HR". iMod ("H" $! _ with "Hσ") as "[$ H]".
  iModIntro; iNext; iIntros (e2 σ2 efs Hstep).
  iMod ("H" $! e2 σ2 efs with "[%]") as "($ & H & $)"; auto.
192 193
  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
194
Qed.
195

196
Lemma wp_bind K `{!LanguageCtx Λ K} E e Φ :
197
  WP e @ E {{ v, WP K (of_val v) @ E {{ Φ }} }}  WP K e @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
198
Proof.
199
  iIntros "H". iLöb as "IH" forall (E e Φ). rewrite wp_unfold /wp_pre.
200 201
  iDestruct "H" as "[Hv|[% H]]".
  { iDestruct "Hv" as (v) "[Hev Hv]"; iDestruct "Hev" as % <-%of_to_val.
202
    by iApply fupd_wp. }
203
  rewrite wp_unfold /wp_pre. iRight; iSplit; eauto using fill_not_val.
204 205
  iIntros (σ1) "Hσ". iMod ("H" $! _ with "Hσ") as "[% H]".
  iModIntro; iSplit.
206
  { iPureIntro. unfold reducible in *. naive_solver eauto using fill_step. }
207 208
  iNext; iIntros (e2 σ2 efs Hstep).
  destruct (fill_step_inv e σ1 e2 σ2 efs) as (e2'&->&?); auto.
209
  iMod ("H" $! e2' σ2 efs with "[%]") as "($ & H & $)"; auto.
210
  by iApply "IH".
Robbert Krebbers's avatar
Robbert Krebbers committed
211 212
Qed.

213
(** * Derived rules *)
214
Lemma wp_mono E e Φ Ψ : ( v, Φ v  Ψ v)  WP e @ E {{ Φ }}  WP e @ E {{ Ψ }}.
215 216
Proof.
  iIntros (HΦ) "H"; iApply (wp_strong_mono E E); auto.
217
  iFrame. iIntros (v) "?". by iApply HΦ.
218 219 220
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
221
Global Instance wp_mono' E e :
222
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ _ E e).
223
Proof. by intros Φ Φ' ?; apply wp_mono. Qed.
224

225
Lemma wp_value E Φ e v : to_val e = Some v  Φ v  WP e @ E {{ Φ }}.
226
Proof. intros; rewrite -(of_to_val e v) //; by apply wp_value'. Qed.
227 228 229
Lemma wp_value_fupd' E Φ v : (|={E}=> Φ v)  WP of_val v @ E {{ Φ }}.
Proof. intros. by rewrite -wp_fupd -wp_value'. Qed.
Lemma wp_value_fupd E Φ e v :
230
  to_val e = Some v  (|={E}=> Φ v)  WP e @ E {{ Φ }}.
231
Proof. intros. rewrite -wp_fupd -wp_value //. Qed.
232

233
Lemma wp_frame_l E e Φ R : R  WP e @ E {{ Φ }}  WP e @ E {{ v, R  Φ v }}.
234
Proof. iIntros "[??]". iApply (wp_strong_mono E E _ Φ); try iFrame; eauto. Qed.
235
Lemma wp_frame_r E e Φ R : WP e @ E {{ Φ }}  R  WP e @ E {{ v, Φ v  R }}.
236 237
Proof. iIntros "[??]". iApply (wp_strong_mono E E _ Φ); try iFrame; eauto. Qed.

238 239 240 241 242 243 244
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.
  iIntros (??) "[Hu Hwp]". iApply (wp_fupd_step with "Hu"); try done.
  iApply (wp_mono with "Hwp"). by iIntros (?) "$$".
Qed.
245 246
Lemma wp_frame_step_r E1 E2 e Φ R :
  to_val e = None  E2  E1 
247
  WP e @ E2 {{ Φ }}  (|={E1,E2}=> R)  WP e @ E1 {{ v, Φ v  R }}.
Ralf Jung's avatar
Ralf Jung committed
248
Proof.
249
  rewrite [(WP _ @ _ {{ _ }}  _)%I]comm; setoid_rewrite (comm _ _ R).
250
  apply wp_frame_step_l.
Ralf Jung's avatar
Ralf Jung committed
251 252
Qed.
Lemma wp_frame_step_l' E e Φ R :
253
  to_val e = None   R  WP e @ E {{ Φ }}  WP e @ E {{ v, R  Φ v }}.
254 255
Proof. iIntros (?) "[??]". iApply (wp_frame_step_l E E); try iFrame; eauto. Qed.
Lemma wp_frame_step_r' E e Φ R :
256
  to_val e = None  WP e @ E {{ Φ }}   R  WP e @ E {{ v, Φ v  R }}.
257 258
Proof. iIntros (?) "[??]". iApply (wp_frame_step_r E E); try iFrame; eauto. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
259
Lemma wp_wand E e Φ Ψ :
260
  WP e @ E {{ Φ }} - ( v, Φ v - Ψ v) - WP e @ E {{ Ψ }}.
261
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
262
  iIntros "Hwp H". iApply (wp_strong_mono E); auto.
263 264
  iFrame "Hwp". iIntros (?) "?". by iApply "H".
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
265 266 267
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.
268
Lemma wp_wand_r E e Φ Ψ :
269
  WP e @ E {{ Φ }}  ( v, Φ v - Ψ v)  WP e @ E {{ Ψ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
270
Proof. iIntros "[Hwp H]". iApply (wp_wand with "Hwp H"). Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
271
End wp.
272 273 274 275 276 277 278 279 280 281 282

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

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

283 284
  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.
285

286 287 288
  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.
289

290 291 292
  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.
293

294 295
  (* lower precedence, if possible, it should always pick elim_upd_fupd_wp *)
  Global Instance elim_modal_fupd_wp_atomic E1 E2 e P Φ :
296
    atomic e 
297
    ElimModal (|={E1,E2}=> P) P
298
            (WP e @ E1 {{ Φ }}) (WP e @ E2 {{ v, |={E2,E1}=> Φ v }})%I | 100.
299
  Proof. intros. by rewrite /ElimModal fupd_frame_r wand_elim_r wp_atomic. Qed.
300

301
End proofmode_classes.
302

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