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

13 14 15 16
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
17
  ( v, to_val e1 = Some v  |={E}=> Φ v) 
18
  (* step case *)
Ralf Jung's avatar
Ralf Jung committed
19
  (to_val e1 = 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 Φ 
23
       [ list] ef  efs, wp  ef (λ _, True)))%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
24

25 26 27
Local Instance wp_pre_contractive `{irisG Λ Σ} : Contractive wp_pre.
Proof.
  rewrite /wp_pre=> n wp wp' Hwp E e1 Φ.
28
  repeat (f_contractive || f_equiv); apply Hwp.
Robbert Krebbers's avatar
Robbert Krebbers committed
29
Qed.
30

31 32
Definition wp_def `{irisG Λ Σ} :
  coPset  expr Λ  (val Λ  iProp Σ)  iProp Σ := fixpoint wp_pre.
Ralf Jung's avatar
Ralf Jung committed
33 34 35 36
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
37
Arguments wp {_ _ _} _ _%E _.
38
Instance: Params (@wp) 5.
Robbert Krebbers's avatar
Robbert Krebbers committed
39

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

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

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

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

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

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

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

121
Lemma wp_value' E Φ v : Φ v  WP of_val v @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
122
Proof.
123 124
  iIntros "HΦ". rewrite wp_unfold /wp_pre.
  iLeft; iExists v; rewrite to_of_val; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
125
Qed.
126
Lemma wp_value_inv E Φ v : WP of_val v @ E {{ Φ }} ={E}= Φ v.
Ralf Jung's avatar
Ralf Jung committed
127
Proof.
128 129
  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
130
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
131

132
Lemma wp_strong_mono E1 E2 e Φ Ψ :
133
  E1  E2  ( v, Φ v ={E2}= Ψ v)  WP e @ E1 {{ Φ }}  WP e @ E2 {{ Ψ }}.
134
Proof.
135
  iIntros (?) "[HΦ H]". iLöb as "IH" forall (e). rewrite !wp_unfold /wp_pre.
136 137
  iDestruct "H" as "[Hv|[% H]]"; [iLeft|iRight].
  { iDestruct "Hv" as (v) "[% Hv]". iExists v; iSplit; first done.
138
    iApply ("HΦ" with ">[-]"). by iApply (fupd_mask_mono E1 _). }
139
  iSplit; [done|]; iIntros (σ1) "Hσ".
140 141 142 143 144
  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Φ").
145 146
Qed.

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

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

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

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

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

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

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

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

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

(** 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.

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

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

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

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

300
End proofmode_classes.
301

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