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

8
Class irisG' (Λstate Λobservation : Type) (Σ : gFunctors) := IrisG {
9
  iris_invG :> invG Σ;
10 11
  state_interp : Λstate  list Λobservation  nat  iProp Σ;
  fork_post : iProp Σ;
12
}.
13
Notation irisG Λ Σ := (irisG' (state Λ) (observation Λ) Σ).
14
Global Opaque iris_invG.
15

16
Definition wp_pre `{irisG Λ Σ} (s : stuckness)
17
    (wp : coPset -c> expr Λ -c> (val Λ -c> iProp Σ) -c> iProp Σ) :
Robbert Krebbers's avatar
Robbert Krebbers committed
18 19 20
    coPset -c> expr Λ -c> (val Λ -c> iProp Σ) -c> iProp Σ := λ E e1 Φ,
  match to_val e1 with
  | Some v => |={E}=> Φ v
21 22 23 24 25 26 27
  | None =>  σ1 κ κs n,
     state_interp σ1 (κ ++ κs) n ={E,}=
       if s is NotStuck then reducible e1 σ1 else True 
        e2 σ2 efs, prim_step e1 σ1 κ e2 σ2 efs ={,,E}=
         state_interp σ2 κs (length efs + n) 
         wp E e2 Φ 
         [ list] i  ef  efs, wp  ef (λ _, fork_post)
Robbert Krebbers's avatar
Robbert Krebbers committed
28
  end%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
29

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

36
Definition wp_def `{irisG Λ Σ} (s : stuckness) :
37
  coPset  expr Λ  (val Λ  iProp Σ)  iProp Σ := fixpoint (wp_pre s).
38 39 40
Definition wp_aux `{irisG Λ Σ} : seal (@wp_def Λ Σ _). by eexists. Qed.
Instance wp' `{irisG Λ Σ} : Wp Λ (iProp Σ) stuckness := wp_aux.(unseal).
Definition wp_eq `{irisG Λ Σ} : wp = @wp_def Λ Σ _ := wp_aux.(seal_eq).
41

Robbert Krebbers's avatar
Robbert Krebbers committed
42
Section wp.
43
Context `{irisG Λ Σ}.
44
Implicit Types s : stuckness.
45 46
Implicit Types P : iProp Σ.
Implicit Types Φ : val Λ  iProp Σ.
47 48
Implicit Types v : val Λ.
Implicit Types e : expr Λ.
Robbert Krebbers's avatar
Robbert Krebbers committed
49

50
(* Weakest pre *)
51 52
Lemma wp_unfold s E e Φ :
  WP e @ s; E {{ Φ }}  wp_pre s (wp (PROP:=iProp Σ)  s) E e Φ.
53
Proof. rewrite wp_eq. apply (fixpoint_unfold (wp_pre s)). Qed.
54

55
Global Instance wp_ne s E e n :
56
  Proper (pointwise_relation _ (dist n) ==> dist n) (wp (PROP:=iProp Σ) s E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
57
Proof.
58
  revert e. induction (lt_wf n) as [n _ IH]=> e Φ Ψ HΦ.
59 60 61 62
  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 *)
63
  do 24 (f_contractive || f_equiv). apply IH; first lia.
Ralf Jung's avatar
Ralf Jung committed
64
  intros v. eapply dist_le; eauto with lia.
Robbert Krebbers's avatar
Robbert Krebbers committed
65
Qed.
66
Global Instance wp_proper s E e :
67
  Proper (pointwise_relation _ () ==> ()) (wp (PROP:=iProp Σ) s E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
68
Proof.
69
  by intros Φ Φ' ?; apply equiv_dist=>n; apply wp_ne=>v; apply equiv_dist.
Robbert Krebbers's avatar
Robbert Krebbers committed
70
Qed.
71

72
Lemma wp_value' s E Φ v : Φ v  WP of_val v @ s; E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
73
Proof. iIntros "HΦ". rewrite wp_unfold /wp_pre to_of_val. auto. Qed.
74
Lemma wp_value_inv' s E Φ v : WP of_val v @ s; E {{ Φ }} ={E}= Φ v.
Robbert Krebbers's avatar
Robbert Krebbers committed
75
Proof. by rewrite wp_unfold /wp_pre to_of_val. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
76

77 78
Lemma wp_strong_mono s1 s2 E1 E2 e Φ Ψ :
  s1  s2  E1  E2 
79
  WP e @ s1; E1 {{ Φ }} - ( v, Φ v ={E2}= Ψ v) - WP e @ s2; E2 {{ Ψ }}.
80
Proof.
81
  iIntros (? HE) "H HΦ". iLöb as "IH" forall (e E1 E2 HE Φ Ψ).
82
  rewrite !wp_unfold /wp_pre.
Robbert Krebbers's avatar
Robbert Krebbers committed
83
  destruct (to_val e) as [v|] eqn:?.
84
  { iApply ("HΦ" with "[> -]"). by iApply (fupd_mask_mono E1 _). }
85
  iIntros (σ1 κ κs n) "Hσ". iMod (fupd_intro_mask' E2 E1) as "Hclose"; first done.
86
  iMod ("H" with "[$]") as "[% H]".
87
  iModIntro. iSplit; [by destruct s1, s2|]. iIntros (e2 σ2 efs Hstep).
88 89 90
  iMod ("H" with "[//]") as "H". iIntros "!> !>".
  iMod "H" as "(Hσ & H & Hefs)".
  iMod "Hclose" as "_". iModIntro. iFrame "Hσ". iSplitR "Hefs".
91
  - iApply ("IH" with "[//] H HΦ").
92 93
  - iApply (big_sepL_impl with "Hefs"); iIntros "!#" (k ef _).
    iIntros "H". iApply ("IH" with "[] H"); auto.
94 95 96
Qed.

Lemma fupd_wp s E e Φ : (|={E}=> WP e @ s; E {{ Φ }})  WP e @ s; E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
97
Proof.
98
  rewrite wp_unfold /wp_pre. iIntros "H". destruct (to_val e) as [v|] eqn:?.
Robbert Krebbers's avatar
Robbert Krebbers committed
99
  { by iMod "H". }
100
  iIntros (σ1 κ κs n) "Hσ1". iMod "H". by iApply "H".
101
Qed.
102
Lemma wp_fupd s E e Φ : WP e @ s; E {{ v, |={E}=> Φ v }}  WP e @ s; E {{ Φ }}.
103
Proof. iIntros "H". iApply (wp_strong_mono s s E with "H"); auto. Qed.
104

Ralf Jung's avatar
Ralf Jung committed
105
Lemma wp_atomic s E1 E2 e Φ `{!Atomic (stuckness_to_atomicity s) e} :
106
  (|={E1,E2}=> WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ s; E1 {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
107
Proof.
David Swasey's avatar
David Swasey committed
108
  iIntros "H". rewrite !wp_unfold /wp_pre.
Robbert Krebbers's avatar
Robbert Krebbers committed
109 110
  destruct (to_val e) as [v|] eqn:He.
  { by iDestruct "H" as ">>> $". }
111
  iIntros (σ1 κ κs n) "Hσ". iMod "H". iMod ("H" $! σ1 with "Hσ") as "[$ H]".
112
  iModIntro. iIntros (e2 σ2 efs Hstep).
113 114
  iMod ("H" with "[//]") as "H". iIntros "!>!>".
  iMod "H" as "(Hσ & H & Hefs)". destruct s.
115
  - rewrite !wp_unfold /wp_pre. destruct (to_val e2) as [v2|] eqn:He2.
David Swasey's avatar
David Swasey committed
116
    + iDestruct "H" as ">> $". by iFrame.
117
    + iMod ("H" $! _ [] with "[$]") as "[H _]". iDestruct "H" as %(? & ? & ? & ? & ?).
118 119
      by edestruct (atomic _ _ _ _ _ Hstep).
  - destruct (atomic _ _ _ _ _ Hstep) as [v <-%of_to_val].
120 121
    iMod (wp_value_inv' with "H") as ">H".
    iModIntro. iFrame "Hσ Hefs". by iApply wp_value'.
Robbert Krebbers's avatar
Robbert Krebbers committed
122
Qed.
123

124
Lemma wp_step_fupd s E1 E2 e P Φ :
125
  to_val e = None  E2  E1 
126
  (|={E1,E2}=> P) - WP e @ s; E2 {{ v, P ={E1}= Φ v }} - WP e @ s; E1 {{ Φ }}.
127
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
128
  rewrite !wp_unfold /wp_pre. iIntros (-> ?) "HR H".
129
  iIntros (σ1 κ κs n) "Hσ". iMod "HR". iMod ("H" with "[$]") as "[$ H]".
130
  iIntros "!>" (e2 σ2 efs Hstep). iMod ("H" $! e2 σ2 efs with "[% //]") as "H".
131 132 133
  iIntros "!>!>". iMod "H" as "(Hσ & H & Hefs)".
  iMod "HR". iModIntro. iFrame "Hσ Hefs".
  iApply (wp_strong_mono s s E2 with "H"); [done..|].
134
  iIntros (v) "H". by iApply "H".
Robbert Krebbers's avatar
Robbert Krebbers committed
135
Qed.
136

137
Lemma wp_bind K `{!LanguageCtx K} s E e Φ :
138
  WP e @ s; E {{ v, WP K (of_val v) @ s; E {{ Φ }} }}  WP K e @ s; E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
139
Proof.
140
  iIntros "H". iLöb as "IH" forall (E e Φ). rewrite wp_unfold /wp_pre.
Robbert Krebbers's avatar
Robbert Krebbers committed
141 142 143
  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 //.
144
  iIntros (σ1 κ κs n) "Hσ". iMod ("H" with "[$]") as "[% H]". iModIntro; iSplit.
145 146
  { iPureIntro. destruct s; last done.
    unfold reducible in *. naive_solver eauto using fill_step. }
147
  iIntros (e2 σ2 efs Hstep).
148
  destruct (fill_step_inv e σ1 κ e2 σ2 efs) as (e2'&->&?); auto.
149
  iMod ("H" $! e2' σ2 efs with "[//]") as "H". iIntros "!>!>".
150 151
  iMod "H" as "(Hσ & H & Hefs)".
  iModIntro. iFrame "Hσ Hefs". by iApply "IH".
Robbert Krebbers's avatar
Robbert Krebbers committed
152 153
Qed.

154
Lemma wp_bind_inv K `{!LanguageCtx K} s E e Φ :
155
  WP K e @ s; E {{ Φ }}  WP e @ s; E {{ v, WP K (of_val v) @ s; E {{ Φ }} }}.
156 157 158 159 160
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 //.
161
  iIntros (σ1 κ κs n) "Hσ". iMod ("H" with "[$]") as "[% H]". iModIntro; iSplit.
162
  { destruct s; eauto using reducible_fill. }
163 164
  iIntros (e2 σ2 efs Hstep).
  iMod ("H" $! (K e2) σ2 efs with "[]") as "H"; [by eauto using fill_step|].
165 166
  iIntros "!>!>". iMod "H" as "(Hσ & H & Hefs)".
  iModIntro. iFrame "Hσ Hefs". by iApply "IH".
167 168
Qed.

169
(** * Derived rules *)
170
Lemma wp_mono s E e Φ Ψ : ( v, Φ v  Ψ v)  WP e @ s; E {{ Φ }}  WP e @ s; E {{ Ψ }}.
171
Proof.
172 173
  iIntros (HΦ) "H"; iApply (wp_strong_mono with "H"); auto.
  iIntros (v) "?". by iApply HΦ.
174
Qed.
David Swasey's avatar
David Swasey committed
175
Lemma wp_stuck_mono s1 s2 E e Φ :
176
  s1  s2  WP e @ s1; E {{ Φ }}  WP e @ s2; E {{ Φ }}.
177
Proof. iIntros (?) "H". iApply (wp_strong_mono with "H"); auto. Qed.
178 179 180
Lemma wp_stuck_weaken s E e Φ :
  WP e @ s; E {{ Φ }}  WP e @ E ?{{ Φ }}.
Proof. apply wp_stuck_mono. by destruct s. Qed.
181
Lemma wp_mask_mono s E1 E2 e Φ : E1  E2  WP e @ s; E1 {{ Φ }}  WP e @ s; E2 {{ Φ }}.
182
Proof. iIntros (?) "H"; iApply (wp_strong_mono with "H"); auto. Qed.
183
Global Instance wp_mono' s E e :
184
  Proper (pointwise_relation _ () ==> ()) (wp (PROP:=iProp Σ) s E e).
185
Proof. by intros Φ Φ' ?; apply wp_mono. Qed.
186

187 188
Lemma wp_value s E Φ e v : IntoVal e v  Φ v  WP e @ s; E {{ Φ }}.
Proof. intros <-. by apply wp_value'. Qed.
189
Lemma wp_value_fupd' s E Φ v : (|={E}=> Φ v)  WP of_val v @ s; E {{ Φ }}.
190
Proof. intros. by rewrite -wp_fupd -wp_value'. Qed.
191 192
Lemma wp_value_fupd s E Φ e v `{!IntoVal e v} :
  (|={E}=> Φ v)  WP e @ s; E {{ Φ }}.
193
Proof. intros. rewrite -wp_fupd -wp_value //. Qed.
194 195
Lemma wp_value_inv s E Φ e v : IntoVal e v  WP e @ s; E {{ Φ }} ={E}= Φ v.
Proof. intros <-. by apply wp_value_inv'. Qed.
196

197
Lemma wp_frame_l s E e Φ R : R  WP e @ s; E {{ Φ }}  WP e @ s; E {{ v, R  Φ v }}.
198
Proof. iIntros "[? H]". iApply (wp_strong_mono with "H"); auto with iFrame. Qed.
199
Lemma wp_frame_r s E e Φ R : WP e @ s; E {{ Φ }}  R  WP e @ s; E {{ v, Φ v  R }}.
200
Proof. iIntros "[H ?]". iApply (wp_strong_mono with "H"); auto with iFrame. Qed.
201

202
Lemma wp_frame_step_l s E1 E2 e Φ R :
203
  to_val e = None  E2  E1 
204
  (|={E1,E2}=> R)  WP e @ s; E2 {{ Φ }}  WP e @ s; E1 {{ v, R  Φ v }}.
205
Proof.
206
  iIntros (??) "[Hu Hwp]". iApply (wp_step_fupd with "Hu"); try done.
207 208
  iApply (wp_mono with "Hwp"). by iIntros (?) "$$".
Qed.
209
Lemma wp_frame_step_r s E1 E2 e Φ R :
210
  to_val e = None  E2  E1 
211
  WP e @ s; E2 {{ Φ }}  (|={E1,E2}=> R)  WP e @ s; E1 {{ v, Φ v  R }}.
Ralf Jung's avatar
Ralf Jung committed
212
Proof.
213
  rewrite [(WP _ @ _; _ {{ _ }}  _)%I]comm; setoid_rewrite (comm _ _ R).
214
  apply wp_frame_step_l.
Ralf Jung's avatar
Ralf Jung committed
215
Qed.
216 217 218 219 220 221 222 223 224
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 {{ Ψ }}.
225
Proof.
226 227
  iIntros "Hwp H". iApply (wp_strong_mono with "Hwp"); auto.
  iIntros (?) "?". by iApply "H".
228
Qed.
229 230
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
231
Proof. iIntros "[H Hwp]". iApply (wp_wand with "Hwp H"). Qed.
232 233
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
234
Proof. iIntros "[Hwp H]". iApply (wp_wand with "Hwp H"). Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
235
End wp.
236 237 238 239 240 241 242

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

243
  Global Instance frame_wp p s E e R Φ Ψ :
244
    ( v, Frame p R (Φ v) (Ψ v)) 
245 246
    Frame p R (WP e @ s; E {{ Φ }}) (WP e @ s; E {{ Ψ }}).
  Proof. rewrite /Frame=> HR. rewrite wp_frame_l. apply wp_mono, HR. Qed.
247

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

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  Global Instance elim_modal_bupd_wp p s E e P Φ :
    ElimModal True p false (|==> P) P (WP e @ s; E {{ Φ }}) (WP e @ s; E {{ Φ }}).
  Proof.
    by rewrite /ElimModal intuitionistically_if_elim
      (bupd_fupd E) fupd_frame_r wand_elim_r fupd_wp.
  Qed.

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

  Global Instance elim_modal_fupd_wp_atomic p s E1 E2 e P Φ :
Ralf Jung's avatar
Ralf Jung committed
266
    Atomic (stuckness_to_atomicity s) e 
267
    ElimModal True p false (|={E1,E2}=> P) P
268
            (WP e @ s; E1 {{ Φ }}) (WP e @ s; E2 {{ v, |={E2,E1}=> Φ v }})%I.
269 270 271 272
  Proof.
    intros. by rewrite /ElimModal intuitionistically_if_elim
      fupd_frame_r wand_elim_r wp_atomic.
  Qed.
273 274 275 276

  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.
Ralf Jung's avatar
Ralf Jung committed
277

Ralf Jung's avatar
Ralf Jung committed
278
  Global Instance elim_acc_wp {X} E1 E2 α β γ e s Φ :
Ralf Jung's avatar
Ralf Jung committed
279
    Atomic (stuckness_to_atomicity s) e 
280 281
    ElimAcc (X:=X) (fupd E1 E2) (fupd E2 E1)
            α β γ (WP e @ s; E1 {{ Φ }})
282
            (λ x, WP e @ s; E2 {{ v, |={E2}=> β x  (γ x -? Φ v) }})%I.
Ralf Jung's avatar
Ralf Jung committed
283
  Proof.
284
    intros ?. rewrite /ElimAcc.
285 286
    iIntros "Hinner >Hacc". iDestruct "Hacc" as (x) "[Hα Hclose]".
    iApply (wp_wand with "[Hinner Hα]"); first by iApply "Hinner".
287
    iIntros (v) ">[Hβ HΦ]". iApply "HΦ". by iApply "Hclose".
Ralf Jung's avatar
Ralf Jung committed
288 289
  Qed.

Ralf Jung's avatar
Ralf Jung committed
290
  Global Instance elim_acc_wp_nonatomic {X} E α β γ e s Φ :
291 292
    ElimAcc (X:=X) (fupd E E) (fupd E E)
            α β γ (WP e @ s; E {{ Φ }})
293
            (λ x, WP e @ s; E {{ v, |={E}=> β x  (γ x -? Φ v) }})%I.
Ralf Jung's avatar
Ralf Jung committed
294
  Proof.
295
    rewrite /ElimAcc.
296 297 298
    iIntros "Hinner >Hacc". iDestruct "Hacc" as (x) "[Hα Hclose]".
    iApply wp_fupd.
    iApply (wp_wand with "[Hinner Hα]"); first by iApply "Hinner".
299
    iIntros (v) ">[Hβ HΦ]". iApply "HΦ". by iApply "Hclose".
Ralf Jung's avatar
Ralf Jung committed
300
  Qed.
301
End proofmode_classes.