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

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

Section wp.
59
60
61
62
63
Context {Λ : language} {Σ : iFunctor}.
Implicit Types P : iProp Λ Σ.
Implicit Types Q : val Λ  iProp Λ Σ.
Implicit Types v : val Λ.
Implicit Types e : expr Λ.
64
Transparent uPred_holds.
Robbert Krebbers's avatar
Robbert Krebbers committed
65

66
67
Global Instance wp_ne E e n :
  Proper (pointwise_relation _ (dist n) ==> dist n) (@wp Λ Σ E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
68
Proof.
69
  cut ( Q1 Q2, ( v, Q1 v {n} Q2 v) 
70
71
72
73
74
75
76
77
     r n', n'  n  {n'} r  wp E e Q1 n' r  wp E e Q2 n' r).
  { by intros help Q Q' HQ; split; apply help. }
  intros Q1 Q2 HQ r n'; revert e r.
  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
78
79
  split; [done|intros e2 σ2 ef ?].
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&?&?); auto.
80
  exists r2, r2'; split_ands; [|eapply IH|]; eauto.
Robbert Krebbers's avatar
Robbert Krebbers committed
81
82
Qed.
Global Instance wp_proper E e :
83
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
84
85
86
Proof.
  by intros Q Q' ?; apply equiv_dist=>n; apply wp_ne=>v; apply equiv_dist.
Qed.
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Lemma wp_mask_frame_mono E1 E2 e Q1 Q2 :
  E1  E2  ( v, Q1 v  Q2 v)  wp E1 e Q1  wp E2 e Q2.
Proof.
  intros HE HQ r n; revert e r; induction n as [n IH] using lt_wf_ind=> e r.
  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'.
  { by rewrite associative_L -union_difference_L. }
  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.
  exists r2, r2'; split_ands; [rewrite HE'|eapply IH|]; eauto.
Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
101

102
Lemma wp_value_inv E Q v n r : wp E (of_val v) Q n r  pvs E E (Q v) n r.
Robbert Krebbers's avatar
Robbert Krebbers committed
103
Proof.
104
  by inversion 1 as [|??? He]; [|rewrite ?to_of_val in He]; simplify_equality.
Robbert Krebbers's avatar
Robbert Krebbers committed
105
106
107
108
109
Qed.
Lemma wp_step_inv E Ef Q e k n σ r rf :
  to_val e = None  1 < k < n  E  Ef =  
  wp E e Q n r  wsat (S k) (E  Ef) σ (r  rf) 
  wp_go (E  Ef) (λ e, wp E e Q) (λ e, wp coPset_all e (λ _, True%I)) k rf e σ.
110
Proof. intros He; destruct 3; [by rewrite ?to_of_val in He|eauto]. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
111

Robbert Krebbers's avatar
Robbert Krebbers committed
112
Lemma wp_value E Q v : Q v  wp E (of_val v) Q.
113
114
Proof. by constructor; apply pvs_intro. Qed.
Lemma pvs_wp E e Q : pvs E E (wp E e Q)  wp E e Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
115
116
117
Proof.
  intros r [|n] ?; [done|]; intros Hvs.
  destruct (to_val e) as [v|] eqn:He; [apply of_to_val in He; subst|].
118
119
120
  { constructor; eapply pvs_trans', pvs_mono, Hvs; eauto.
    intros ???; apply wp_value_inv. }
  constructor; [done|]=> rf k Ef σ1 ???.
Robbert Krebbers's avatar
Robbert Krebbers committed
121
  destruct (Hvs rf (S k) Ef σ1) as (r'&Hwp&?); auto.
122
123
124
125
126
127
128
129
130
  eapply wp_step_inv with (S k) r'; eauto.
Qed.
Lemma wp_pvs E e Q : wp E e (λ v, pvs E E (Q v))  wp E e Q.
Proof.
  intros r n; revert e r; induction n as [n IH] using lt_wf_ind=> e r Hr HQ.
  destruct (to_val e) as [v|] eqn:He; [apply of_to_val in He; subst|].
  { constructor; apply pvs_trans', (wp_value_inv _ (pvs E E  Q)); auto. }
  constructor; [done|]=> rf k Ef σ1 ???.
  destruct (wp_step_inv E Ef (pvs E E  Q) e k n σ1 r rf) as [? Hstep]; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
131
132
  split; [done|intros e2 σ2 ef ?].
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&Hwp'&?); auto.
133
  exists r2, r2'; split_ands; [|apply (IH k)|]; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
134
135
136
137
Qed.
Lemma wp_atomic E1 E2 e Q :
  E2  E1  atomic e  pvs E1 E2 (wp E2 e (λ v, pvs E2 E1 (Q v)))  wp E1 e Q.
Proof.
138
  intros ? He r n ? Hvs; constructor; eauto using atomic_not_val.
Robbert Krebbers's avatar
Robbert Krebbers committed
139
140
  intros rf k Ef σ1 ???.
  destruct (Hvs rf (S k) Ef σ1) as (r'&Hwp&?); auto.
141
142
143
  destruct (wp_step_inv E2 Ef (pvs E2 E1  Q) e k (S k) σ1 r' rf)
    as [Hsafe Hstep]; auto using atomic_not_val.
  split; [done|]=> e2 σ2 ef ?.
Robbert Krebbers's avatar
Robbert Krebbers committed
144
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&Hwp'&?); clear Hsafe Hstep; auto.
145
146
  destruct Hwp' as [k r2 v Hvs'|k r2 e2 Hgo];
    [|destruct (atomic_step e σ1 e2 σ2 ef); naive_solver].
147
  apply pvs_trans in Hvs'; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
148
  destruct (Hvs' (r2'  rf) k Ef σ2) as (r3&[]); rewrite ?(associative _); auto.
149
150
151
  exists r3, r2'; split_ands; last done.
  * by rewrite -(associative _).
  * constructor; apply pvs_intro; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
152
153
154
155
156
157
Qed.
Lemma wp_frame_r E e Q R : (wp E e Q  R)  wp E e (λ v, Q v  R).
Proof.
  intros r' n Hvalid (r&rR&Hr&Hwp&?); revert Hvalid.
  rewrite Hr; clear Hr; revert e r Hwp.
  induction n as [n IH] using lt_wf_ind; intros e r1.
158
159
160
161
  destruct 1 as [|n r e ? Hgo]=>?.
  { constructor; apply pvs_frame_r; auto. exists r, rR; eauto. }
  constructor; [done|]=> rf k Ef σ1 ???.
  destruct (Hgo (rRrf) k Ef σ1) as [Hsafe Hstep]; auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
162
163
164
165
166
  { by rewrite (associative _). }
  split; [done|intros e2 σ2 ef ?].
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&?&?); auto.
  exists (r2  rR), r2'; split_ands; auto.
  * by rewrite -(associative _ r2)
167
      (commutative _ rR) !associative -(associative _ _ rR).
Robbert Krebbers's avatar
Robbert Krebbers committed
168
169
170
171
172
173
  * apply IH; eauto using uPred_weaken.
Qed.
Lemma wp_frame_later_r E e Q R :
  to_val e = None  (wp E e Q   R)  wp E e (λ v, Q v  R).
Proof.
  intros He r' n Hvalid (r&rR&Hr&Hwp&?); revert Hvalid; rewrite Hr; clear Hr.
174
  destruct Hwp as [|[|n] r e ? Hgo]; [by rewrite to_of_val in He|done|].
Robbert Krebbers's avatar
Robbert Krebbers committed
175
176
177
178
179
180
  constructor; [done|intros rf k Ef σ1 ???].
  destruct (Hgo (rRrf) k Ef σ1) as [Hsafe Hstep];rewrite ?(associative _);auto.
  split; [done|intros e2 σ2 ef ?].
  destruct (Hstep e2 σ2 ef) as (r2&r2'&?&?&?); auto.
  exists (r2  rR), r2'; split_ands; auto.
  * by rewrite -(associative _ r2)
181
      (commutative _ rR) !associative -(associative _ _ rR).
Robbert Krebbers's avatar
Robbert Krebbers committed
182
183
184
  * apply wp_frame_r; [auto|exists r2, rR; split_ands; auto].
    eapply uPred_weaken with rR n; eauto.
Qed.
185
186
Lemma wp_bind `{LanguageCtx Λ K} E e Q :
  wp E e (λ v, wp E (K (of_val v)) Q)  wp E (K e) Q.
Robbert Krebbers's avatar
Robbert Krebbers committed
187
Proof.
188
189
190
191
  intros r n; revert e r; induction n as [n IH] using lt_wf_ind=> e r ?.
  destruct 1 as [|n r e ? Hgo]; [by apply pvs_wp|].
  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
192
193
  split.
  { destruct Hsafe as (e2&σ2&ef&?).
194
    by exists (K e2), σ2, ef; apply fill_step. }
Robbert Krebbers's avatar
Robbert Krebbers committed
195
  intros e2 σ2 ef ?.
196
  destruct (fill_step_inv e σ1 e2 σ2 ef) as (e2'&->&?); auto.
Robbert Krebbers's avatar
Robbert Krebbers committed
197
198
199
200
201
  destruct (Hstep e2' σ2 ef) as (r2&r2'&?&?&?); auto.
  exists r2, r2'; split_ands; try eapply IH; eauto.
Qed.

(* Derived rules *)
202
Opaque uPred_holds.
Robbert Krebbers's avatar
Robbert Krebbers committed
203
Import uPred.
204
205
Lemma wp_mono E e Q1 Q2 : ( v, Q1 v  Q2 v)  wp E e Q1  wp E e Q2.
Proof. by apply wp_mask_frame_mono. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
206
Global Instance wp_mono' E e :
207
  Proper (pointwise_relation _ () ==> ()) (@wp Λ Σ E e).
Robbert Krebbers's avatar
Robbert Krebbers committed
208
Proof. by intros Q Q' ?; apply wp_mono. Qed.
209
210
Lemma wp_value' E Q e v : to_val e = Some v  Q v  wp E e Q.
Proof. intros; rewrite -(of_to_val e v) //; by apply wp_value. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
211
212
213
214
215
216
217
218
Lemma wp_frame_l E e Q R : (R  wp E e Q)  wp E e (λ v, R  Q v).
Proof. setoid_rewrite (commutative _ R); apply wp_frame_r. Qed.
Lemma wp_frame_later_l E e Q R :
  to_val e = None  ( R  wp E e Q)  wp E e (λ v, R  Q v).
Proof.
  rewrite (commutative _ ( R)%I); setoid_rewrite (commutative _ R).
  apply wp_frame_later_r.
Qed.
219
220
221
222
223
224
Lemma wp_always_l E e Q R `{!AlwaysStable R} :
  (R  wp E e Q)  wp E e (λ v, R  Q v).
Proof. by setoid_rewrite (always_and_sep_l' _ _); rewrite wp_frame_l. Qed.
Lemma wp_always_r E e Q R `{!AlwaysStable R} :
  (wp E e Q  R)  wp E e (λ v, Q v  R).
Proof. by setoid_rewrite (always_and_sep_r' _ _); rewrite wp_frame_r. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
225
226
Lemma wp_impl_l E e Q1 Q2 : ((  v, Q1 v  Q2 v)  wp E e Q1)  wp E e Q2.
Proof.
227
  rewrite wp_always_l; apply wp_mono=> // v.
228
  by rewrite always_elim (forall_elim v) impl_elim_l.
Robbert Krebbers's avatar
Robbert Krebbers committed
229
230
Qed.
Lemma wp_impl_r E e Q1 Q2 : (wp E e Q1    v, Q1 v  Q2 v)  wp E e Q2.
231
Proof. by rewrite commutative wp_impl_l. Qed.
Robbert Krebbers's avatar
Robbert Krebbers committed
232
End wp.