weakestpre.v 11.9 KB
Newer Older
1
2
From iris.program_logic Require Export pviewshifts.
From iris.program_logic Require Import wsat.
Robbert Krebbers's avatar
Robbert Krebbers committed
3
Local Hint Extern 10 (_  _) => omega.
4
5
6
Local Hint Extern 100 (@eq coPset _ _) => eassumption || set_solver.
Local Hint Extern 100 (_  _) => set_solver.
Local Hint Extern 100 (@subseteq coPset _ _ _) => set_solver.
Robbert Krebbers's avatar
Robbert Krebbers committed
7
Local Hint Extern 10 ({_} _) =>
8
9
10
  repeat match goal with
  | H : wsat _ _ _ _ |- _ => apply wsat_valid in H; last omega
  end; solve_validN.
Robbert Krebbers's avatar
Robbert Krebbers committed
11

12
Record wp_go {Λ Σ} (E : coPset) (Φ Φfork : expr Λ  nat  iRes Λ Σ  Prop)
13
    (k : nat) (rf : iRes Λ Σ) (e1 : expr Λ) (σ1 : state Λ) := {
14
  wf_safe : reducible e1 σ1;
Robbert Krebbers's avatar
Robbert Krebbers committed
15
16
17
18
  wp_step e2 σ2 ef :
    prim_step e1 σ1 e2 σ2 ef 
     r2 r2',
      wsat k E σ2 (r2  r2'  rf) 
19
20
      Φ e2 k r2 
       e', ef = Some e'  Φfork e' k r2'
Robbert Krebbers's avatar
Robbert Krebbers committed
21
}.
22
CoInductive wp_pre {Λ Σ} (E : coPset)
23
     (Φ : val Λ  iProp Λ Σ) : expr Λ  nat  iRes Λ Σ  Prop :=
24
  | wp_pre_value n r v : (|={E}=> Φ v)%I n r  wp_pre E Φ (of_val v) n r
Robbert Krebbers's avatar
Robbert Krebbers committed
25
26
27
  | wp_pre_step n r1 e1 :
     to_val e1 = None 
     ( rf k Ef σ1,
28
       0 < k < n  E  Ef =  
Robbert Krebbers's avatar
Robbert Krebbers committed
29
       wsat (S k) (E  Ef) σ1 (r1  rf) 
30
       wp_go (E  Ef) (wp_pre E Φ)
31
                      (wp_pre  (λ _, True%I)) k rf e1 σ1) 
32
     wp_pre E Φ e1 n r1.
Ralf Jung's avatar
Ralf Jung committed
33
Program Definition wp_def {Λ Σ} (E : coPset) (e : expr Λ)
34
  (Φ : val Λ  iProp Λ Σ) : iProp Λ Σ := {| uPred_holds := wp_pre E Φ e |}.
Robbert Krebbers's avatar
Robbert Krebbers committed
35
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
36
  intros Λ Σ E e Φ n r1 r2 Hwp Hr.
37
  destruct Hwp as [|n r1 e2 ? Hgo]; constructor; rewrite -?Hr; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
38
39
40
  intros rf k Ef σ1 ?; rewrite -(dist_le _ _ _ _ Hr); naive_solver.
Qed.
Next Obligation.
Robbert Krebbers's avatar
Robbert Krebbers committed
41
42
  intros Λ Σ E e Φ n1 n2 r1 r2; revert Φ E e n2 r1 r2.
  induction n1 as [n1 IH] using lt_wf_ind; intros Φ E e n2 r1 r1'.
43
  destruct 1 as [|n1 r1 e1 ? Hgo].
44
45
  - constructor; eauto using uPred_weaken.
  - intros [rf' Hr] ??; constructor; [done|intros rf k Ef σ1 ???].
Robbert Krebbers's avatar
Robbert Krebbers committed
46
    destruct (Hgo (rf'  rf) k Ef σ1) as [Hsafe Hstep];
47
      rewrite ?assoc -?Hr; auto; constructor; [done|].
Robbert Krebbers's avatar
Robbert Krebbers committed
48
    intros e2 σ2 ef ?; destruct (Hstep e2 σ2 ef) as (r2&r2'&?&?&?); auto.
49
    exists r2, (r2'  rf'); split_and?; eauto 10 using (IH k), cmra_included_l.
50
    by rewrite -!assoc (assoc _ r2).
Robbert Krebbers's avatar
Robbert Krebbers committed
51
Qed.
Ralf Jung's avatar
Ralf Jung committed
52
53
54
55
56
57
(* Perform sealing. *)
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.

Arguments wp {_ _} _ _ _.
58
Instance: Params (@wp) 4.
Robbert Krebbers's avatar
Robbert Krebbers committed
59

60
Notation "'WP' e @ E {{ Φ } }" := (wp E e Φ)
61
  (at level 20, e, Φ at level 200,
62
63
   format "'WP'  e  @  E  {{  Φ  } }") : uPred_scope.
Notation "'WP' e {{ Φ } }" := (wp  e Φ)
64
  (at level 20, e, Φ at level 200,
65
   format "'WP'  e  {{  Φ  } }") : uPred_scope.
66

Robbert Krebbers's avatar
Robbert Krebbers committed
67
Section wp.
68
Context {Λ : language} {Σ : iFunctor}.
69
Implicit Types P : iProp Λ Σ.
70
Implicit Types Φ : val Λ  iProp Λ Σ.
71
72
Implicit Types v : val Λ.
Implicit Types e : expr Λ.
Robbert Krebbers's avatar
Robbert Krebbers committed
73

74
75
Global Instance wp_ne E e n :
  Proper (pointwise_relation _ (dist n) ==> dist n) (@wp Λ Σ E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
76
Proof.
77
  cut ( Φ Ψ, ( v, Φ v {n} Ψ v) 
Robbert Krebbers's avatar
Robbert Krebbers committed
78
     n' r, n'  n  {n'} r  wp E e Φ n' r  wp E e Ψ n' r).
Ralf Jung's avatar
Ralf Jung committed
79
80
  { rewrite wp_eq. intros help Φ Ψ HΦΨ. by do 2 split; apply help. }
  rewrite wp_eq. intros Φ Ψ HΦΨ n' r; revert e r.
81
82
83
84
85
  induction n' as [n' IH] using lt_wf_ind=> e r.
  destruct 3 as [n' r v HpvsQ|n' r e1 ? Hgo].
  { constructor. by eapply pvs_ne, HpvsQ; eauto. }
  constructor; [done|]=> rf k Ef σ1 ???.
  destruct (Hgo rf k Ef σ1) as [Hsafe Hstep]; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
86
87
  split; [done|intros e2 σ2 ef ?].
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&?&?); auto.
88
  exists r2, r2'; split_and?; [|eapply IH|]; eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
89
90
Qed.
Global Instance wp_proper E e :
91
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
92
Proof.
93
  by intros Φ Φ' ?; apply equiv_dist=>n; apply wp_ne=>v; apply equiv_dist.
Robbert Krebbers's avatar
Robbert Krebbers committed
94
Qed.
95

96
Lemma wp_mask_frame_mono E1 E2 e Φ Ψ :
97
  E1  E2  ( v, Φ v  Ψ v)  WP e @ E1 {{ Φ }}  WP e @ E2 {{ Ψ }}.
98
Proof.
Ralf Jung's avatar
Ralf Jung committed
99
  rewrite wp_eq. intros HE HΦ; split=> n r.
100
  revert e r; induction n as [n IH] using lt_wf_ind=> e r.
101
102
103
104
  destruct 2 as [n' r v HpvsQ|n' r e1 ? Hgo].
  { constructor; eapply pvs_mask_frame_mono, HpvsQ; eauto. }
  constructor; [done|]=> rf k Ef σ1 ???.
  assert (E2  Ef = E1  (E2  E1  Ef)) as HE'.
105
  { by rewrite assoc_L -union_difference_L. }
106
107
108
  destruct (Hgo rf k ((E2  E1)  Ef) σ1) as [Hsafe Hstep]; rewrite -?HE'; auto.
  split; [done|intros e2 σ2 ef ?].
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&?&?); auto.
109
  exists r2, r2'; split_and?; [rewrite HE'|eapply IH|]; eauto.
110
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
111

112
113
114
115
116
117
118
Lemma wp_zero E e Φ r : wp_def E e Φ 0 r.
Proof.
  case EQ: (to_val e).
  - rewrite -(of_to_val _ _ EQ). constructor. rewrite pvs_eq.
    exact: pvs_zero.
  - constructor; first done. intros ?????. exfalso. omega.
Qed.
119
Lemma wp_value_inv E Φ v n r :
Ralf Jung's avatar
Ralf Jung committed
120
  wp_def E (of_val v) Φ n r  pvs E E (Φ v) n r.
Robbert Krebbers's avatar
Robbert Krebbers committed
121
Proof.
122
  by inversion 1 as [|??? He]; [|rewrite ?to_of_val in He]; simplify_eq.
Robbert Krebbers's avatar
Robbert Krebbers committed
123
Qed.
124
Lemma wp_step_inv E Ef Φ e k n σ r rf :
125
  to_val e = None  0 < k < n  E  Ef =  
Ralf Jung's avatar
Ralf Jung committed
126
127
128
129
130
  wp_def E e Φ n r  wsat (S k) (E  Ef) σ (r  rf) 
  wp_go (E  Ef) (λ e, wp_def E e Φ) (λ e, wp_def  e (λ _, True%I)) k rf e σ.
Proof.
  intros He; destruct 3; [by rewrite ?to_of_val in He|eauto].
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
131

132
Lemma wp_value' E Φ v : Φ v  WP of_val v @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
133
Proof. rewrite wp_eq. split=> n r; constructor; by apply pvs_intro. Qed.
134
Lemma pvs_wp E e Φ : (|={E}=> WP e @ E {{ Φ }})  WP e @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
135
Proof.
Ralf Jung's avatar
Ralf Jung committed
136
  rewrite wp_eq. split=> n r ? Hvs.
Robbert Krebbers's avatar
Robbert Krebbers committed
137
  destruct (to_val e) as [v|] eqn:He; [apply of_to_val in He; subst|].
138
  { constructor; eapply pvs_trans', pvs_mono, Hvs; eauto.
139
    split=> ???; apply wp_value_inv. }
140
  constructor; [done|]=> rf k Ef σ1 ???.
Ralf Jung's avatar
Ralf Jung committed
141
  rewrite pvs_eq in Hvs. destruct (Hvs rf (S k) Ef σ1) as (r'&Hwp&?); auto.
142
143
  eapply wp_step_inv with (S k) r'; eauto.
Qed.
144
Lemma wp_pvs E e Φ : WP e @  E {{ λ v, |={E}=> Φ v }}  WP e @ E {{ Φ }}.
145
Proof.
Ralf Jung's avatar
Ralf Jung committed
146
147
  rewrite wp_eq. split=> n r; revert e r;
    induction n as [n IH] using lt_wf_ind=> e r Hr HΦ.
148
  destruct (to_val e) as [v|] eqn:He; [apply of_to_val in He; subst|].
149
  { constructor; apply pvs_trans', (wp_value_inv _ (pvs E E  Φ)); auto. }
150
  constructor; [done|]=> rf k Ef σ1 ???.
151
  destruct (wp_step_inv E Ef (pvs E E  Φ) e k n σ1 r rf) as [? Hstep]; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
152
153
  split; [done|intros e2 σ2 ef ?].
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&Hwp'&?); auto.
154
  exists r2, r2'; split_and?; [|apply (IH k)|]; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
155
Qed.
156
Lemma wp_atomic E1 E2 e Φ :
157
  E2  E1  atomic e 
158
  (|={E1,E2}=> WP e @ E2 {{ λ v, |={E2,E1}=> Φ v }})  WP e @ E1 {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
159
Proof.
Ralf Jung's avatar
Ralf Jung committed
160
161
  rewrite wp_eq pvs_eq. intros ? He; split=> n r ? Hvs; constructor.
  eauto using atomic_not_val. intros rf k Ef σ1 ???.
Robbert Krebbers's avatar
Robbert Krebbers committed
162
  destruct (Hvs rf (S k) Ef σ1) as (r'&Hwp&?); auto.
Ralf Jung's avatar
Ralf Jung committed
163
164
  destruct (wp_step_inv E2 Ef (pvs_def E2 E1  Φ) e k (S k) σ1 r' rf)
    as [Hsafe Hstep]; auto using atomic_not_val; [].
165
  split; [done|]=> e2 σ2 ef ?.
Robbert Krebbers's avatar
Robbert Krebbers committed
166
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&Hwp'&?); clear Hsafe Hstep; auto.
167
168
  destruct Hwp' as [k r2 v Hvs'|k r2 e2 Hgo];
    [|destruct (atomic_step e σ1 e2 σ2 ef); naive_solver].
Ralf Jung's avatar
Ralf Jung committed
169
  rewrite -pvs_eq in Hvs'. apply pvs_trans in Hvs';auto. rewrite pvs_eq in Hvs'.
170
  destruct (Hvs' (r2'  rf) k Ef σ2) as (r3&[]); rewrite ?assoc; auto.
171
  exists r3, r2'; split_and?; last done.
172
173
  - by rewrite -assoc.
  - constructor; apply pvs_intro; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
174
Qed.
175
Lemma wp_frame_r E e Φ R : (WP e @ E {{ Φ }}  R)  WP e @ E {{ λ v, Φ v  R }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
176
Proof.
Ralf Jung's avatar
Ralf Jung committed
177
178
  rewrite wp_eq. uPred.unseal; split; intros n r' Hvalid (r&rR&Hr&Hwp&?).
  revert Hvalid. rewrite Hr; clear Hr; revert e r Hwp.
Robbert Krebbers's avatar
Robbert Krebbers committed
179
  induction n as [n IH] using lt_wf_ind; intros e r1.
180
  destruct 1 as [|n r e ? Hgo]=>?.
181
182
  { constructor. rewrite -uPred_sep_eq; apply pvs_frame_r; auto.
    uPred.unseal; exists r, rR; eauto. }
183
184
  constructor; [done|]=> rf k Ef σ1 ???.
  destruct (Hgo (rRrf) k Ef σ1) as [Hsafe Hstep]; auto.
185
  { by rewrite assoc. }
Robbert Krebbers's avatar
Robbert Krebbers committed
186
187
  split; [done|intros e2 σ2 ef ?].
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&?&?); auto.
188
  exists (r2  rR), r2'; split_and?; auto.
189
  - by rewrite -(assoc _ r2)
190
      (comm _ rR) !assoc -(assoc _ _ rR).
191
  - apply IH; eauto using uPred_weaken.
Robbert Krebbers's avatar
Robbert Krebbers committed
192
Qed.
Ralf Jung's avatar
Ralf Jung committed
193
Lemma wp_frame_step_r E e Φ R :
194
  to_val e = None  (WP e @ E {{ Φ }}   R)  WP e @ E {{ λ v, Φ v  R }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
195
Proof.
Ralf Jung's avatar
Ralf Jung committed
196
  rewrite wp_eq. intros He; uPred.unseal; split; intros n r' Hvalid (r&rR&Hr&Hwp&?).
197
  revert Hvalid; rewrite Hr; clear Hr.
198
199
  destruct Hwp as [|n r e ? Hgo]; [by rewrite to_of_val in He|].
  constructor; [done|]=>rf k Ef σ1 ???; destruct n as [|n]; first omega.
200
  destruct (Hgo (rRrf) k Ef σ1) as [Hsafe Hstep];rewrite ?assoc;auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
201
202
  split; [done|intros e2 σ2 ef ?].
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&?&?); auto.
203
  exists (r2  rR), r2'; split_and?; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
204
  - by rewrite -(assoc _ r2) (comm _ rR) !assoc -(assoc _ _ rR).
Ralf Jung's avatar
Ralf Jung committed
205
206
  - rewrite -uPred_sep_eq. move:(wp_frame_r). rewrite wp_eq=>Hframe.
    apply Hframe; [auto|uPred.unseal; exists r2, rR; split_and?; auto].
Robbert Krebbers's avatar
Robbert Krebbers committed
207
    eapply uPred_weaken with n rR; eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
208
Qed.
209
Lemma wp_bind `{LanguageCtx Λ K} E e Φ :
210
  WP e @ E {{ λ v, WP K (of_val v) @ E {{ Φ }} }}  WP K e @ E {{ Φ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
211
Proof.
Ralf Jung's avatar
Ralf Jung committed
212
213
214
215
  rewrite wp_eq. split=> n r; revert e r;
    induction n as [n IH] using lt_wf_ind=> e r ?.
  destruct 1 as [|n r e ? Hgo].
  { rewrite -wp_eq. apply pvs_wp; rewrite ?wp_eq; done. }
216
217
  constructor; auto using fill_not_val=> rf k Ef σ1 ???.
  destruct (Hgo rf k Ef σ1) as [Hsafe Hstep]; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
218
219
  split.
  { destruct Hsafe as (e2&σ2&ef&?).
220
    by exists (K e2), σ2, ef; apply fill_step. }
Robbert Krebbers's avatar
Robbert Krebbers committed
221
  intros e2 σ2 ef ?.
222
  destruct (fill_step_inv e σ1 e2 σ2 ef) as (e2'&->&?); auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
223
  destruct (Hstep e2' σ2 ef) as (r2&r2'&?&?&?); auto.
224
  exists r2, r2'; split_and?; try eapply IH; eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
225
226
Qed.

227
(** * Derived rules *)
Robbert Krebbers's avatar
Robbert Krebbers committed
228
Import uPred.
229
Lemma wp_mono E e Φ Ψ : ( v, Φ v  Ψ v)  WP e @ E {{ Φ }}  WP e @ E {{ Ψ }}.
230
Proof. by apply wp_mask_frame_mono. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
231
Global Instance wp_mono' E e :
232
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ E e).
233
Proof. by intros Φ Φ' ?; apply wp_mono. Qed.
234
Lemma wp_strip_pvs E e P Φ :
235
  P  WP e @ E {{ Φ }}  (|={E}=> P)  WP e @ E {{ Φ }}.
236
Proof. move=>->. by rewrite pvs_wp. Qed.
237
Lemma wp_value E Φ e v : to_val e = Some v  Φ v  WP e @ E {{ Φ }}.
238
Proof. intros; rewrite -(of_to_val e v) //; by apply wp_value'. Qed.
239
Lemma wp_value_pvs E Φ e v :
240
  to_val e = Some v  (|={E}=> Φ v)  WP e @ E {{ Φ }}.
Ralf Jung's avatar
Ralf Jung committed
241
Proof. intros. rewrite -wp_pvs. rewrite -wp_value //. Qed.
242
Lemma wp_frame_l E e Φ R : (R  WP e @ E {{ Φ }})  WP e @ E {{ λ v, R  Φ v }}.
243
Proof. setoid_rewrite (comm _ R); apply wp_frame_r. Qed.
Ralf Jung's avatar
Ralf Jung committed
244
Lemma wp_frame_step_l E e Φ R :
245
  to_val e = None  ( R  WP e @ E {{ Φ }})  WP e @ E {{ λ v, R  Φ v }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
246
Proof.
247
  rewrite (comm _ ( R)%I); setoid_rewrite (comm _ R).
Ralf Jung's avatar
Ralf Jung committed
248
  apply wp_frame_step_r.
Robbert Krebbers's avatar
Robbert Krebbers committed
249
Qed.
250
Lemma wp_always_l E e Φ R `{!PersistentP R} :
251
  (R  WP e @ E {{ Φ }})  WP e @ E {{ λ v, R  Φ v }}.
252
Proof. by setoid_rewrite (always_and_sep_l _ _); rewrite wp_frame_l. Qed.
253
Lemma wp_always_r E e Φ R `{!PersistentP R} :
254
  (WP e @ E {{ Φ }}  R)  WP e @ E {{ λ v, Φ v  R }}.
255
Proof. by setoid_rewrite (always_and_sep_r _ _); rewrite wp_frame_r. Qed.
256
Lemma wp_impl_l E e Φ Ψ :
257
  ((  v, Φ v  Ψ v)  WP e @ E {{ Φ }})  WP e @ E {{ Ψ }}.
Robbert Krebbers's avatar
Robbert Krebbers committed
258
Proof.
Ralf Jung's avatar
Ralf Jung committed
259
  rewrite wp_always_l. apply wp_mono=> // v.
260
  by rewrite always_elim (forall_elim v) impl_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
261
Qed.
262
Lemma wp_impl_r E e Φ Ψ :
263
  (WP e @ E {{ Φ }}   ( v, Φ v  Ψ v))  WP e @ E {{ Ψ }}.
264
Proof. by rewrite comm wp_impl_l. Qed.
265
Lemma wp_mask_weaken E1 E2 e Φ :
266
  E1  E2  WP e @ E1 {{ Φ }}  WP e @ E2 {{ Φ }}.
267
268
269
Proof. auto using wp_mask_frame_mono. Qed.

(** * Weakest-pre is a FSA. *)
270
Definition wp_fsa (e : expr Λ) : FSA Λ Σ (val Λ) := λ E, wp E e.
271
Global Arguments wp_fsa _ _ / _.
272
Global Instance wp_fsa_prf : FrameShiftAssertion (atomic e) (wp_fsa e).
273
Proof.
274
  rewrite /wp_fsa; split; auto using wp_mask_frame_mono, wp_atomic, wp_frame_r.
275
  intros E Φ. by rewrite -(pvs_wp E e Φ) -(wp_pvs E e Φ).
276
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
277
End wp.