-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloydHoareWp_Predicative.v
301 lines (291 loc) · 10.2 KB
/
FloydHoareWp_Predicative.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
Require Import Statement FloydHoareWp Predicative.
Lemma pred_if_hoare :
forall (T : Type) (C : @Stmt T) P Q, BlockFree C -> ValidHoareTriple P C Q -> ValidHoareTriple P (Statement.Spec (pred C)) Q.
Proof.
induction C as [ | | | | | | ]; intros P Q BFC; simpl.
{ intros HHtriple; inversion_clear HHtriple; constructor; firstorder. }
{ intros HHtriple; inversion_clear HHtriple; constructor.
intros s HHp.
split.
{ intros s' HHeq; subst s'; auto. }
{ eauto. }
}
{ intros HHtriple; inversion_clear HHtriple; inversion BFC as [BFC1 BFC2]; clear BFC.
apply (IHC1 _ _) in H; auto; apply (IHC2 _ _) in H0; auto; inversion_clear H; inversion_clear H0; constructor.
intros s HHp; split.
{ firstorder. }
{ destruct (H1 _ HHp) as (HH1,(s',HH2)).
destruct (H _ (HH1 _ HH2)) as (HH3,(s'',HH4)).
exists s''; firstorder.
}
}
{ intros HHtriple; inversion_clear HHtriple; inversion BFC as [BFC1 BFC2]; clear BFC.
constructor.
intros s HHp; split.
{ intros s' [HHpred | HHpred].
{ apply (IHC1 _ _) in H0; auto; inversion_clear H0; firstorder. }
{ apply (IHC2 _ _) in H1; auto; inversion_clear H1; firstorder. }
}
{ apply (IHC1 _ _) in H0; auto; inversion_clear H0; apply (IHC2 _ _) in H1; auto; inversion_clear H1.
destruct (H _ HHp) as [HHpt | HHpf].
{ destruct (H2 _ (conj HHpt HHp)) as (_,(s',HHpred)); eauto. }
{ destruct (H0 _ (conj HHpf HHp)) as (_,(s',HHpred)); eauto. }
}
}
{ intros HHtriple; inversion_clear HHtriple; simpl in BFC.
constructor; intros s HHp; split.
{ intros s' HHwhile. apply H3.
{ apply (while_end _ _ _ _ _ HHwhile). }
{ generalize (H1 _ HHp).
pattern s,s'; apply (while_ind _ _ _ _ _ HHwhile).
intros r r'; simpl; intros [(HHwhile1,((sx,(HHpsx,HHwhile21)),HHwhile22)) | (HHp1,HHeq)].
{ pose proof (H2 (V r)) as HHtriple1.
apply (IHC _ _) in HHtriple1; auto.
inversion_clear HHtriple1.
intros; apply HHwhile21; auto.
apply (H4 r); auto.
}
{ subst r'; auto. }
}
}
{ destruct (H _ (H1 _ HHp)).
{ assert (well_founded (fun s s' => R (V s) (V s'))).
{ constructor.
pattern (V a); apply (well_founded_ind H0); eauto; intros.
constructor; intros.
apply (H5 _ H6); auto.
}
{ generalize H4 (H1 _ HHp).
pattern s; apply (well_founded_ind H5).
intros.
pose proof (H2 (V x)).
apply (IHC _ _) in H9; auto.
inversion_clear H9.
destruct (H10 _ (conj H7 (conj H8 (eq_refl _)))).
destruct H11 as (s',H11).
destruct (H9 _ H11).
destruct (H _ H12).
{ destruct (H6 _ H13 H14 H12) as (s'',H15).
exists s''.
apply while_construct; left; split; auto; split; eauto.
intros sx HHpred.
destruct (H9 _ HHpred).
destruct (H _ H16).
{ apply H6; auto. }
{ exists sx; apply while_construct; right; auto. }
}
{ exists s'; apply while_construct; left; split; auto; split.
{ exists s'; split; auto; apply while_construct; right; auto. }
{ intros sx HHpred.
destruct (H9 _ HHpred).
destruct (H _ H15).
{ apply H6; auto. }
{ exists sx; apply while_construct; right; auto. }
}
}
}
}
{ exists s; apply while_construct; right; auto. }
}
}
{ auto. }
{ simpl in BFC; contradiction. }
Qed.
Lemma hoare_if_pred :
forall (S : Type) (C : @Stmt S) P Q, BlockFree C -> ValidHoareTriple P (Statement.Spec (pred C)) Q -> ValidHoareTriple P C Q.
Proof.
induction C as [ | | | | | | ]; intros P Q BFC; simpl.
{ intros HHtriple; inversion_clear HHtriple; constructor; firstorder. }
{ intros HHtriple; inversion_clear HHtriple; constructor.
firstorder.
}
{ intros HHtriple; inversion_clear HHtriple; inversion BFC as [BFC1 BFC2]; clear BFC.
apply (Seq _ (fun s => (forall sx, (pred C2) s sx -> Q sx) /\ (exists sx, (pred C2) s sx))).
{ apply (IHC1 _ _); auto; constructor; firstorder. }
{ apply (IHC2 _ _); auto; constructor; firstorder. }
}
{ intros HHtriple; inversion_clear HHtriple; inversion BFC as [BFC1 BFC2]; clear BFC.
constructor.
{ intros.
destruct (H _ H0) as (_,(s',[(HHp,_) | (HHn,_) ])).
{ left; auto. }
{ right; auto. }
}
{ apply (IHC1 _ _); auto; constructor; intros s (HHp1,HHp2); split; firstorder. }
{ apply (IHC2 _ _); auto; constructor; intros s (HHp1,HHp2); split; firstorder. }
}
{ intros HHtriple; inversion_clear HHtriple; simpl in BFC.
apply (While _ _ _ (fun s => (forall s', while p (pred C) s s' -> Q s') /\ exists s', while p (pred C) s s')
_ _ (fun s s' => p s' /\ pred C s' s /\ (exists r, (while p (pred C)) s' r)) (fun s => s)).
{ intros.
destruct H0 as (_,(s',HHwhile)).
apply while_destruct in HHwhile.
destruct HHwhile as [ (HHp,_) | (HHn,_)]; unfold Decidable.decidable; auto.
}
{ constructor.
intros y [HHp [HHpred HHwhile]].
generalize y HHp HHpred; clear y HHp HHpred.
pattern a; apply (ex_while_ind _ _ _ _ HHwhile).
intros.
destruct H0; try contradiction.
destruct H0.
constructor; intros.
destruct H2.
destruct H3.
apply (H1 y); auto.
}
{ auto. }
{ intros.
apply (IHC _ _); auto.
constructor.
intros.
destruct H0 as (HH1,((HH2,(s',HH3)),HH)); subst v.
apply while_destruct in HH3.
destruct HH3 as [ (HH4,((sx,(HH51,HH52)),HH6)) | (HH4,HH5) ]; try contradiction.
split; intros.
{ split; intros.
{ split; intros.
{ apply HH2.
apply while_construct; left; split; auto.
split.
{ exists s'0; firstorder. }
{ auto. }
}
{ apply (HH6 _ H0). }
}
{ split; auto.
destruct (HH6 _ H0) as (s'',HH).
split; auto.
exists s''; apply while_construct; left; split; auto.
split; eauto.
}
}
{ exists sx; auto. }
}
{ intros.
apply H1.
apply while_construct; right; auto.
}
}
{ auto. }
{ simpl in BFC; contradiction. }
Qed.
Lemma hoare_pred :
forall (T : Type) (C : @Stmt T) P Q, BlockFree C -> ValidHoareTriple P C Q <-> ValidHoareTriple P (Statement.Spec (pred C)) Q.
Proof.
intros T C P Q BFC.
split.
{ apply pred_if_hoare; auto. }
{ apply hoare_if_pred; auto. }
Qed.
Lemma pred_refines_iff_hoare_refines : forall T (Q R : T >> T),
Predicative.refines (Statement.Spec Q) (Statement.Spec R) <-> FloydHoareWp.refines (Statement.Spec Q) (Statement.Spec R).
Proof.
unfold Predicative.refines, FloydHoareWp.refines; intros; split; intros HHrefines.
{ intros K K' HHr.
inversion_clear HHr.
constructor.
split; intros.
{ apply (H _ H0).
apply HHrefines; auto.
apply (H _ H0).
}
{ apply HHrefines.
apply (H _ H0).
}
}
{ intros.
assert (ValidHoareTriple (fun r => r = s) (Statement.Spec R) (fun r' => R s r')).
{ constructor.
intros r HHr.
subst r.
auto.
}
apply HHrefines in H0.
inversion_clear H0.
apply (H1 s (eq_refl s)).
}
Qed.
Theorem hoare_refines_iff_pred_refines_simplified :
forall T (Q R : @Stmt T), BlockFree Q -> BlockFree R -> FloydHoareWp.refines Q R <-> Predicative.refines Q R.
Proof.
intros T Q R BFQ BFR.
unfold FloydHoareWp.refines.
setoid_rewrite (hoare_pred _ _ _ _ BFQ).
setoid_rewrite (hoare_pred _ _ _ _ BFR).
setoid_rewrite <- pred_refines_iff_hoare_refines.
unfold Predicative.refines; simpl.
reflexivity.
Qed.
Theorem pred_refines_if_hoare_refines :
forall T (Q R : @Stmt T), BlockFree Q -> BlockFree R -> FloydHoareWp.refines Q R -> Predicative.refines Q R.
Proof.
intros S Q R BFQ BFR; intros HHrefines.
apply pred_refines_iff_hoare_refines.
unfold FloydHoareWp.refines in *.
intros Pre Post HHprepost.
apply hoare_if_pred in HHprepost; auto.
apply HHrefines in HHprepost.
apply (pred_if_hoare _ Q); auto.
Qed.
Theorem hoare_refines_if_pred_refines :
forall T (Q R : @Stmt T), BlockFree Q -> BlockFree R -> Predicative.refines Q R -> FloydHoareWp.refines Q R.
Proof.
intros S Q R BFQ BFR; intros HHrefines.
unfold FloydHoareWp.refines; intros Pre Post HHprepost.
apply hoare_if_pred; auto.
constructor.
apply pred_if_hoare in HHprepost; auto.
inversion_clear HHprepost.
firstorder.
Qed.
(* The definition of refinement in Hoare Logic is equivalent to the predicative (relational) definition *)
Theorem hoare_refines_iff_pred_refines :
forall T (Q R : @Stmt T), BlockFree Q -> BlockFree R -> FloydHoareWp.refines Q R <-> Predicative.refines Q R.
Proof.
intros T Q R BFQ BFR.
split; intros HHrefines.
{ apply pred_refines_iff_hoare_refines.
unfold FloydHoareWp.refines in *.
intros Pre Post HHprepost.
apply hoare_pred in HHprepost; auto.
apply (hoare_pred _ Q); auto.
}
{ unfold FloydHoareWp.refines; intros Pre Post HHprepost.
apply hoare_pred; auto.
apply hoare_pred in HHprepost; auto.
constructor.
inversion_clear HHprepost.
firstorder.
}
Qed.
(* The definition of refinement with logical constants is equivalent to the definition without logical constants *)
Theorem extended_definition_iff_simple_definition : forall T S1 S2,
BlockFree S1 -> BlockFree S2 ->
(forall (P Q : T -> Prop), P {: S1 :} Q -> P {: S2 :} Q)
<-> (forall P (Q : T -> T -> Prop), (forall r, (fun s => r = s /\ P s) {: S1 :} (Q r)) -> forall r, (fun s => r = s /\ P s) {: S2 :} (Q r)).
Proof.
intros * BFS1 BFS2.
split.
{ intros.
apply H.
apply H0.
}
{ setoid_rewrite (hoare_pred _ _ _ _ BFS1).
setoid_rewrite (hoare_pred _ _ _ _ BFS2).
intros.
pose proof (H P (fun _ s' => Q s')).
simpl in *.
assert (forall r : T, (fun s : T => r = s /\ P s) {: Statement.Spec (Predicative.pred S1) :} (fun s' : T => Q s')).
{ intros.
unshelve eapply (consequence _ _ _ _ _ _ _ _ H0); firstorder.
}
pose proof (H1 H2).
setoid_rewrite hoare_pred; simpl; auto.
constructor; intros.
pose proof (H3 s).
inversion_clear H5.
apply H6.
auto.
}
Qed.