weakestpre.v 13.7 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
From iris.algebra Require Import auth.
6
7
Import uPred.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Class irisG' (Λstate : Type) (Σ : gFunctors) : Set := IrisG {
  iris_invG :> invG Σ;
  state_inG :> inG Σ (authR (optionUR (exclR (leibnizC Λstate))));
  state_name : gname;
}.
Notation irisG Λ Σ := (irisG' (state Λ) Σ).

Definition ownP `{irisG' Λstate Σ} (σ : Λstate) : iProp Σ :=
  own state_name ( (Excl' σ)).
Typeclasses Opaque ownP.
Instance: Params (@ownP) 3.

Definition ownP_auth `{irisG' Λstate Σ} (σ : Λstate) : iProp Σ :=
  own state_name ( (Excl' (σ:leibnizC Λstate))).
Typeclasses Opaque ownP_auth.
Instance: Params (@ownP_auth) 4.

25
26
27
28
29
30
31
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 *)
  ( v, to_val e1 = Some v  |={E}=> Φ v) 
  (* step case *)
  (to_val e1 = None   σ1,
32
33
34
35
     ownP_auth σ1 ={E,}=  reducible e1 σ1 
       e2 σ2 efs,  prim_step e1 σ1 e2 σ2 efs ={,E}=
       ownP_auth σ2  wp E e2 Φ 
       [ list] ef  efs, wp  ef (λ _, True)))%I.
Robbert Krebbers's avatar
Robbert Krebbers committed
36

37
38
39
40
Local Instance wp_pre_contractive `{irisG Λ Σ} : Contractive wp_pre.
Proof.
  rewrite /wp_pre=> n wp wp' Hwp E e1 Φ.
  apply or_ne, and_ne, forall_ne; auto=> σ1; apply wand_ne; auto.
41
  apply fupd_ne, sep_ne, later_contractive; auto=> i ?.
42
  apply forall_ne=> e2; apply forall_ne=> σ2; apply forall_ne=> efs.
43
  apply wand_ne, fupd_ne, sep_ne, sep_ne; auto; first by apply Hwp.
44
  apply big_opL_ne=> ? ef. by apply Hwp.
Robbert Krebbers's avatar
Robbert Krebbers committed
45
Qed.
46

47
48
Definition wp_def `{irisG Λ Σ} :
  coPset  expr Λ  (val Λ  iProp Σ)  iProp Σ := fixpoint wp_pre.
Ralf Jung's avatar
Ralf Jung committed
49
50
51
52
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
53
Arguments wp {_ _ _} _ _%E _.
54
Instance: Params (@wp) 5.
Robbert Krebbers's avatar
Robbert Krebbers committed
55

Janno's avatar
Janno committed
56
Notation "'WP' e @ E {{ Φ } }" := (wp E e%E Φ)
57
  (at level 20, e, Φ at level 200,
58
   format "'WP'  e  @  E  {{  Φ  } }") : uPred_scope.
Janno's avatar
Janno committed
59
Notation "'WP' e {{ Φ } }" := (wp  e%E Φ)
60
  (at level 20, e, Φ at level 200,
61
   format "'WP'  e  {{  Φ  } }") : uPred_scope.
62

Janno's avatar
Janno committed
63
Notation "'WP' e @ E {{ v , Q } }" := (wp E e%E (λ v, Q))
64
65
  (at level 20, e, Q at level 200,
   format "'WP'  e  @  E  {{  v ,  Q  } }") : uPred_scope.
Janno's avatar
Janno committed
66
Notation "'WP' e {{ v , Q } }" := (wp  e%E (λ v, Q))
67
68
69
  (at level 20, e, Q at level 200,
   format "'WP'  e  {{  v ,  Q  } }") : uPred_scope.

Ralf Jung's avatar
Ralf Jung committed
70
(* Texan triples *)
71
Notation "'{{{' P } } } e {{{ x .. y , 'RET' pat ; Q } } }" :=
Ralf Jung's avatar
Ralf Jung committed
72
  (  Φ,
73
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
74
    (at level 20, x closed binder, y closed binder,
75
76
     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
77
  (  Φ,
78
      P -  ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
79
    (at level 20, x closed binder, y closed binder,
80
81
     format "{{{  P  } } }  e  @  E  {{{  x .. y ,  RET  pat ;  Q } } }") : uPred_scope.
Notation "'{{{' P } } } e {{{ 'RET' pat ; Q } } }" :=
82
  (  Φ, P -  (Q - Φ pat%V) - WP e {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
83
    (at level 20,
84
85
     format "{{{  P  } } }  e  {{{  RET  pat ;  Q } } }") : uPred_scope.
Notation "'{{{' P } } } e @ E {{{ 'RET' pat ; Q } } }" :=
86
  (  Φ, P -  (Q - Φ pat%V) - WP e @ E {{ Φ }})%I
Ralf Jung's avatar
Ralf Jung committed
87
    (at level 20,
88
     format "{{{  P  } } }  e  @  E  {{{  RET  pat ;  Q } } }") : uPred_scope.
Ralf Jung's avatar
Ralf Jung committed
89

90
Notation "'{{{' P } } } e {{{ x .. y , 'RET' pat ; Q } } }" :=
91
  ( Φ : _  uPred _,
92
      P   ( x, .. ( y, Q - Φ pat%V) .. ) - WP e {{ Φ }})
93
    (at level 20, x closed binder, y closed binder,
94
95
     format "{{{  P  } } }  e  {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
Notation "'{{{' P } } } e @ E {{{ x .. y , 'RET' pat ; Q } } }" :=
96
  ( Φ : _  uPred _,
97
      P   ( x, .. ( y, Q - Φ pat%V) .. ) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
98
    (at level 20, x closed binder, y closed binder,
99
100
     format "{{{  P  } } }  e  @  E  {{{  x .. y ,  RET  pat ;  Q } } }") : C_scope.
Notation "'{{{' P } } } e {{{ 'RET' pat ; Q } } }" :=
101
  ( Φ : _  uPred _, P   (Q - Φ pat%V) - WP e {{ Φ }})
102
    (at level 20,
103
104
     format "{{{  P  } } }  e  {{{  RET  pat ;  Q } } }") : C_scope.
Notation "'{{{' P } } } e @ E {{{ 'RET' pat ; Q } } }" :=
105
  ( Φ : _  uPred _, P   (Q - Φ pat%V) - WP e @ E {{ Φ }})
Ralf Jung's avatar
Ralf Jung committed
106
    (at level 20,
107
     format "{{{  P  } } }  e  @  E  {{{  RET  pat ;  Q } } }") : C_scope.
108

Robbert Krebbers's avatar
Robbert Krebbers committed
109
Section wp.
110
111
112
Context `{irisG Λ Σ}.
Implicit Types P : iProp Σ.
Implicit Types Φ : val Λ  iProp Σ.
113
114
Implicit Types v : val Λ.
Implicit Types e : expr Λ.
Robbert Krebbers's avatar
Robbert Krebbers committed
115

116
(* Physical state *)
117
Lemma ownP_twice σ1 σ2 : ownP σ1  ownP σ2  False.
118
119
120
121
Proof. rewrite /ownP own_valid_2. by iIntros (?). Qed.
Global Instance ownP_timeless σ : TimelessP (@ownP (state Λ) Σ _ σ).
Proof. rewrite /ownP; apply _. Qed.

122
Lemma ownP_agree σ1 σ2 : ownP_auth σ1  ownP σ2  σ1 = σ2.
123
124
125
126
Proof.
  rewrite /ownP /ownP_auth own_valid_2 -auth_both_op.
  by iIntros ([[[] [=]%leibniz_equiv] _]%auth_valid_discrete).
Qed.
127
Lemma ownP_update σ1 σ2 : ownP_auth σ1  ownP σ1 == ownP_auth σ2  ownP σ2.
128
129
130
131
132
133
Proof.
  rewrite /ownP -!own_op.
  by apply own_update, auth_update, option_local_update, exclusive_local_update.
Qed.

(* Weakest pre *)
134
135
136
Lemma wp_unfold E e Φ : WP e @ E {{ Φ }}  wp_pre wp E e Φ.
Proof. rewrite wp_eq. apply (fixpoint_unfold wp_pre). Qed.

137
Global Instance wp_ne E e n :
138
  Proper (pointwise_relation _ (dist n) ==> dist n) (@wp Λ Σ _ E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
139
Proof.
140
141
142
  revert e. induction (lt_wf n) as [n _ IH]=> e Φ Ψ HΦ.
  rewrite !wp_unfold /wp_pre. apply or_ne, and_ne; auto; first solve_proper.
  apply forall_ne=> σ1.
143
  apply wand_ne, fupd_ne, sep_ne, later_contractive; auto=> i ?.
144
  apply forall_ne=> e2; apply forall_ne=> σ2; apply forall_ne=> ef.
145
  apply wand_ne, fupd_ne, sep_ne, sep_ne; auto.
146
  apply IH; [done|]=> v. eapply dist_le; eauto with omega.
Robbert Krebbers's avatar
Robbert Krebbers committed
147
148
Qed.
Global Instance wp_proper E e :
149
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ _ E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
150
Proof.
151
  by intros Φ Φ' ?; apply equiv_dist=>n; apply wp_ne=>v; apply equiv_dist.
Robbert Krebbers's avatar
Robbert Krebbers committed
152
Qed.
153

154
Lemma wp_value' E Φ v : Φ v  WP of_val v @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
155
Proof.
156
157
  iIntros "HΦ". rewrite wp_unfold /wp_pre.
  iLeft; iExists v; rewrite to_of_val; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
158
Qed.
159
Lemma wp_value_inv E Φ v : WP of_val v @ E {{ Φ }} ={E}= Φ v.
Ralf Jung's avatar
Ralf Jung committed
160
Proof.
161
162
  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
163
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
164

165
Lemma wp_strong_mono E1 E2 e Φ Ψ :
166
  E1  E2  ( v, Φ v ={E2}= Ψ v)  WP e @ E1 {{ Φ }}  WP e @ E2 {{ Ψ }}.
167
Proof.
168
  iIntros (?) "[HΦ H]". iLöb as "IH" forall (e). rewrite !wp_unfold /wp_pre.
169
170
  iDestruct "H" as "[Hv|[% H]]"; [iLeft|iRight].
  { iDestruct "Hv" as (v) "[% Hv]". iExists v; iSplit; first done.
171
    iApply ("HΦ" with ">[-]"). by iApply (fupd_mask_mono E1 _). }
172
  iSplit; [done|]; iIntros (σ1) "Hσ".
173
174
175
176
177
  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Φ").
178
179
Qed.

180
Lemma fupd_wp E e Φ : (|={E}=> WP e @ E {{ Φ }})  WP e @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
181
Proof.
182
183
  rewrite wp_unfold /wp_pre. iIntros "H". destruct (to_val e) as [v|] eqn:?.
  { iLeft. iExists v; iSplit; first done.
184
    by iMod "H" as "[H|[% ?]]"; [iDestruct "H" as (v') "[% ?]"|]; simplify_eq. }
185
  iRight; iSplit; [done|]; iIntros (σ1) "Hσ1".
186
  iMod "H" as "[H|[% H]]"; last by iApply "H".
187
  iDestruct "H" as (v') "[% ?]"; simplify_eq.
188
Qed.
189
Lemma wp_fupd E e Φ : WP e @ E {{ v, |={E}=> Φ v }}  WP e @ E {{ Φ }}.
190
Proof. iIntros "H". iApply (wp_strong_mono E); try iFrame; auto. Qed.
191

192
Lemma wp_atomic E1 E2 e Φ :
193
  atomic e 
194
  (|={E1,E2}=> WP e @ E2 {{ v, |={E2,E1}=> Φ v }})  WP e @ E1 {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
195
Proof.
196
  iIntros (Hatomic) "H". destruct (to_val e) as [v|] eqn:He.
197
  { apply of_to_val in He as <-. iApply wp_fupd. iApply wp_value'.
198
    iMod "H". by iMod (wp_value_inv with "H"). }
199
  setoid_rewrite wp_unfold; rewrite /wp_pre. iRight; iSplit; auto.
200
  iIntros (σ1) "Hσ". iMod "H" as "[H|[_ H]]".
201
  { iDestruct "H" as (v') "[% ?]"; simplify_eq. }
202
203
  iMod ("H" $! σ1 with "Hσ") as "[$ H]".
  iModIntro. iNext. iIntros (e2 σ2 efs Hstep).
204
  destruct (Hatomic _ _ _ _ Hstep) as [v <-%of_to_val].
205
206
  iMod ("H" $! _ σ2 efs with "[#]") as "($ & H & $)"; auto.
  iMod (wp_value_inv with "H") as ">H". by iApply wp_value'.
Robbert Krebbers's avatar
Robbert Krebbers committed
207
Qed.
208
209
210

Lemma wp_frame_step_l E1 E2 e Φ R :
  to_val e = None  E2  E1 
211
  (|={E1,E2}=> R)  WP e @ E2 {{ Φ }}  WP e @ E1 {{ v, R  Φ v }}.
212
213
214
215
Proof.
  rewrite !wp_unfold /wp_pre. iIntros (??) "[HR [Hv|[_ H]]]".
  { iDestruct "Hv" as (v) "[% Hv]"; simplify_eq. }
  iRight; iSplit; [done|]. iIntros (σ1) "Hσ".
216
217
218
219
  iMod "HR". iMod ("H" $! _ with "Hσ") as "[$ H]".
  iModIntro; iNext; iIntros (e2 σ2 efs Hstep).
  iMod ("H" $! e2 σ2 efs with "[%]") as "($ & H & $)"; auto.
  iMod "HR". iModIntro. iApply (wp_strong_mono E2 _ _ Φ); try iFrame; eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
220
Qed.
221

222
Lemma wp_bind K `{!LanguageCtx Λ K} E e Φ :
223
  WP e @ E {{ v, WP K (of_val v) @ E {{ Φ }} }}  WP K e @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
224
Proof.
225
  iIntros "H". iLöb as "IH" forall (E e Φ). rewrite wp_unfold /wp_pre.
226
227
  iDestruct "H" as "[Hv|[% H]]".
  { iDestruct "Hv" as (v) "[Hev Hv]"; iDestruct "Hev" as % <-%of_to_val.
228
    by iApply fupd_wp. }
229
  rewrite wp_unfold /wp_pre. iRight; iSplit; eauto using fill_not_val.
230
231
  iIntros (σ1) "Hσ". iMod ("H" $! _ with "Hσ") as "[% H]".
  iModIntro; iSplit.
232
  { iPureIntro. unfold reducible in *. naive_solver eauto using fill_step. }
233
234
  iNext; iIntros (e2 σ2 efs Hstep).
  destruct (fill_step_inv e σ1 e2 σ2 efs) as (e2'&->&?); auto.
235
  iMod ("H" $! e2' σ2 efs with "[%]") as "($ & H & $)"; auto.
236
  by iApply "IH".
Robbert Krebbers's avatar
Robbert Krebbers committed
237
238
Qed.

239
(** * Derived rules *)
240
Lemma wp_mono E e Φ Ψ : ( v, Φ v  Ψ v)  WP e @ E {{ Φ }}  WP e @ E {{ Ψ }}.
241
242
Proof.
  iIntros (HΦ) "H"; iApply (wp_strong_mono E E); auto.
243
  iFrame. iIntros (v) "?". by iApply HΦ.
244
245
246
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
247
Global Instance wp_mono' E e :
248
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ _ E e).
249
Proof. by intros Φ Φ' ?; apply wp_mono. Qed.
250

251
Lemma wp_value E Φ e v : to_val e = Some v  Φ v  WP e @ E {{ Φ }}.
252
Proof. intros; rewrite -(of_to_val e v) //; by apply wp_value'. Qed.
253
254
255
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 :
256
  to_val e = Some v  (|={E}=> Φ v)  WP e @ E {{ Φ }}.
257
Proof. intros. rewrite -wp_fupd -wp_value //. Qed.
258

259
Lemma wp_frame_l E e Φ R : R  WP e @ E {{ Φ }}  WP e @ E {{ v, R  Φ v }}.
260
Proof. iIntros "[??]". iApply (wp_strong_mono E E _ Φ); try iFrame; eauto. Qed.
261
Lemma wp_frame_r E e Φ R : WP e @ E {{ Φ }}  R  WP e @ E {{ v, Φ v  R }}.
262
263
264
265
Proof. iIntros "[??]". iApply (wp_strong_mono E E _ Φ); try iFrame; eauto. Qed.

Lemma wp_frame_step_r E1 E2 e Φ R :
  to_val e = None  E2  E1 
266
  WP e @ E2 {{ Φ }}  (|={E1,E2}=> R)  WP e @ E1 {{ v, Φ v  R }}.
Ralf Jung's avatar
Ralf Jung committed
267
Proof.
268
  rewrite [(WP _ @ _ {{ _ }}  _)%I]comm; setoid_rewrite (comm _ _ R).
269
  apply wp_frame_step_l.
Ralf Jung's avatar
Ralf Jung committed
270
271
Qed.
Lemma wp_frame_step_l' E e Φ R :
272
  to_val e = None   R  WP e @ E {{ Φ }}  WP e @ E {{ v, R  Φ v }}.
273
274
Proof. iIntros (?) "[??]". iApply (wp_frame_step_l E E); try iFrame; eauto. Qed.
Lemma wp_frame_step_r' E e Φ R :
275
  to_val e = None  WP e @ E {{ Φ }}   R  WP e @ E {{ v, Φ v  R }}.
276
277
Proof. iIntros (?) "[??]". iApply (wp_frame_step_r E E); try iFrame; eauto. Qed.

Robbert Krebbers's avatar
Robbert Krebbers committed
278
279
Lemma wp_wand E e Φ Ψ :
  WP e @ E {{ Φ }}  ( v, Φ v - Ψ v) - WP e @ E {{ Ψ }}.
280
Proof.
Robbert Krebbers's avatar
Robbert Krebbers committed
281
  iIntros "Hwp H". iApply (wp_strong_mono E); auto.
282
283
  iFrame "Hwp". iIntros (?) "?". by iApply "H".
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
284
285
286
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.
287
Lemma wp_wand_r E e Φ Ψ :
288
  WP e @ E {{ Φ }}  ( v, Φ v - Ψ v)  WP e @ E {{ Ψ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
289
Proof. iIntros "[Hwp H]". iApply (wp_wand with "Hwp H"). Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
290
End wp.
291
292
293
294
295
296
297
298
299
300
301

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

302
303
  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.
304

305
306
307
  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.
308

309
310
311
  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.
312

313
314
  (* lower precedence, if possible, it should always pick elim_upd_fupd_wp *)
  Global Instance elim_modal_fupd_wp_atomic E1 E2 e P Φ :
315
    atomic e 
316
    ElimModal (|={E1,E2}=> P) P
317
            (WP e @ E1 {{ Φ }}) (WP e @ E2 {{ v, |={E2,E1}=> Φ v }})%I | 100.
318
  Proof. intros. by rewrite /ElimModal fupd_frame_r wand_elim_r wp_atomic. Qed.
319
End proofmode_classes.
320

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