forked from B-Rich/vfa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchTree.v
283 lines (259 loc) · 7.72 KB
/
SearchTree.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
(* Binary search trees, with specification and proof of correctness.
Andrew W. Appel, 2013.
*)
(* IMPORTS *)
Require Import ZArith Permutation Omega List Classical_sets.
Require Import FunctionalExtensionality.
Axiom prop_ext: ClassicalFacts.prop_extensionality.
Implicit Arguments prop_ext.
Open Scope Z.
(* A FEW LEMMAS ABOUT SET UNION, ET CETERA *)
Arguments In {U} A x.
Arguments Union {U} B C x.
Arguments Singleton {U} x x0.
Arguments Empty_set {U} x.
Lemma In_Singleton_iff:
forall U (x y: U), In (Singleton x) y <-> x=y.
Proof.
intros. split; intros.
inversion H. reflexivity.
subst. constructor.
Qed.
Lemma In_Union_iff:
forall U (B C: Ensemble U) x,
In (Union B C) x <-> (In B x \/ In C x).
Proof.
intros. split.
intros. inversion H.
left. apply H0.
right. apply H0.
intros. inversion H.
apply Union_introl. apply H0.
apply Union_intror. apply H0.
Qed.
Lemma Union_Empty_set_l: forall U (B: Ensemble U),
Union Empty_set B = B.
Proof.
intros. extensionality x. apply prop_ext.
split; intros.
inversion H; subst. apply Empty_set_ind. apply H0. apply H0.
apply Union_intror. apply H.
Qed.
Lemma Union_sym:
forall {U} (B C: Ensemble U), Union B C = Union C B.
Proof.
intros. extensionality x. apply prop_ext.
split; intros; inversion H; subst.
- apply Union_intror. apply H0.
- apply Union_introl. apply H0.
- apply Union_intror. apply H0.
- apply Union_introl. apply H0.
Qed.
Lemma Union_assoc:
forall {U} (A B C: Ensemble U),
Union A (Union B C) = Union (Union A B) C.
Proof.
intros. extensionality x. apply prop_ext.
split; intros; inversion H; subst.
- apply Union_introl. apply Union_introl. apply H0.
- inversion H0; subst.
apply Union_introl. apply Union_intror. apply H1.
apply Union_intror. apply H1.
- inversion H0; subst.
apply Union_introl. apply H1.
apply Union_intror. apply Union_introl. apply H1.
- apply Union_intror. apply Union_intror. apply H0.
Qed.
Lemma Union_refl: forall {U} (A: Ensemble U), Union A A = A.
Proof.
intros. extensionality x. apply prop_ext.
split; intros.
destruct H; apply H.
constructor. apply H.
Qed.
(* PROGRAM FOR BINARY SEARCH TREES *)
Inductive tree : Type :=
| E : tree
| T: tree -> Z -> tree -> tree.
Fixpoint member (x: Z) (t : tree) : bool :=
match t with
| E => false
| T tl k tr => if Z.ltb x k then member x tl
else if Z.ltb k x then member x tr
else true
end.
Fixpoint insert (x: Z) (s: tree) :=
match s with
| E => T E x E
| T a y b => if Z.ltb x y then T (insert x a) y b
else if Z.ltb y x then T a y (insert x b)
else T a x b
end.
(* SPECIFICATIONS AND PROOF *)
Fixpoint allb (R : Z -> Z -> bool) (x: Z) (s: tree) : bool :=
match s with
| E => true
| T l n r => andb (R x n) (andb (allb R x l) (allb R x r))
end.
Inductive SearchTree: tree -> Prop :=
| st_empty : SearchTree E
| st_node l n r : SearchTree l -> allb Z.gtb n l = true
-> SearchTree r -> allb Z.ltb n r = true
-> SearchTree (T l n r).
Inductive Contents: Ensemble Z -> tree -> Prop :=
| Ct_E: Contents Empty_set E
| Ct_U lset l n rset r: Contents lset l
-> Contents rset r
-> Contents (Union (Singleton n) (Union lset rset)) (T l n r).
Ltac inv H := inversion H; subst; clear H.
Lemma allb_split : forall R n l1 z l2,
allb R n (T l1 z l2) = true
-> R n z = true /\ allb R n l1 = true /\ allb R n l2 = true.
Proof.
intros.
apply Bool.andb_true_iff in H. inversion H.
apply Bool.andb_true_iff in H1. inversion H1.
intuition.
Qed.
Lemma allb_ltb_spec : forall R cts l n k,
Contents cts l -> In cts k -> allb R n l = true -> R n k = true.
Proof.
intros.
generalize dependent cts.
induction l; intros; inv H. inv H0.
apply allb_split in H1.
inversion H1. inversion H2.
inv H0; inv H6.
- assumption.
- apply IHl1 with (cts := lset); try assumption.
- apply IHl2 with (cts := rset); try assumption.
Qed.
Lemma ltb_antisym : forall k n, (k <? n) = false -> (n <? k) = false -> n = k.
Proof.
intros.
apply Z.ltb_ge in H.
apply Z.ltb_ge in H0.
omega.
Qed.
Theorem member_spec:
forall k cts t,
SearchTree t ->
Contents cts t ->
(In cts k <-> member k t = true).
Proof.
split; intros.
- induction H0; inv H1; simpl; inv H; inv H0.
+ rewrite Z.ltb_irrefl; reflexivity.
+ destruct (k <? n) eqn:Heqe.
apply IHContents1; assumption.
pose (allb_ltb_spec Z.gtb lset l n k H0_ H H5).
rewrite Z.gtb_ltb in e.
rewrite Heqe in e. inversion e.
+ destruct (k <? n) eqn:Heqe.
pose (allb_ltb_spec Z.ltb rset r n k H0_0 H H7).
apply Z.ltb_lt in Heqe.
apply Z.ltb_lt in e. omega.
destruct (n <? k) eqn:Heqe2.
apply IHContents2; assumption.
reflexivity.
- induction H0; inv H1; simpl; inv H.
apply In_Union_iff.
destruct (k <? n) eqn:Heqe.
right. apply In_Union_iff. left.
apply IHContents1; assumption.
destruct (n <? k) eqn:Heqe2.
right. apply In_Union_iff. right.
apply IHContents2; assumption.
left. apply In_Singleton_iff.
apply ltb_antisym; assumption.
Qed.
Theorem insert_contents:
forall k t cts,
SearchTree t ->
Contents cts t ->
Contents (Union (Singleton k) cts) (insert k t).
Proof.
intros.
induction H0. simpl.
replace (@Empty_set Z) with (@Union Z Empty_set Empty_set).
repeat constructor.
apply Union_refl.
simpl.
inv H.
destruct (k <? n) eqn:Heqe.
replace (Union (Singleton k) (Union (Singleton n) (Union lset rset)))
with (Union (Singleton n) (Union (Union (Singleton k) lset) rset)).
constructor.
apply IHContents1; assumption.
assumption.
rewrite Union_sym.
repeat rewrite <- Union_assoc. f_equal.
rewrite (Union_sym rset).
repeat rewrite <- Union_assoc. f_equal.
rewrite Union_sym. reflexivity.
destruct (n <? k) eqn:Heqe2.
replace (Union (Singleton k) (Union (Singleton n) (Union lset rset)))
with (Union (Singleton n) (Union lset (Union (Singleton k) rset))).
constructor. assumption.
apply IHContents2; assumption.
repeat rewrite <- Union_assoc.
rewrite Union_sym.
repeat rewrite <- Union_assoc.
rewrite Union_sym.
repeat rewrite <- Union_assoc. f_equal.
rewrite Union_sym.
repeat rewrite <- Union_assoc. reflexivity.
pose (ltb_antisym _ _ Heqe Heqe2). rewrite e.
replace (Union (Singleton k) (Union (Singleton k) (Union lset rset)))
with (Union (Singleton k) (Union lset rset)).
constructor; assumption.
repeat rewrite Union_assoc.
rewrite Union_refl. reflexivity.
Qed.
Lemma allb_impl : forall R z t k,
allb R z t = true -> R z k = true -> allb R z (insert k t) = true.
Proof.
intros.
induction t; simpl.
apply Bool.andb_true_iff.
split. assumption.
reflexivity.
inv H.
apply Bool.andb_true_iff in H2.
inversion H2.
apply Bool.andb_true_iff in H1.
inversion H1.
destruct (k <? z0) eqn:Heqe; simpl.
f_equal. f_equal.
specialize (IHt1 H3).
rewrite IHt1. auto.
destruct (z0 <? k) eqn:Heqe2; simpl.
f_equal. f_equal.
specialize (IHt2 H4).
rewrite IHt2. auto.
pose (ltb_antisym _ _ Heqe Heqe2). rewrite e.
reflexivity.
Qed.
Theorem insert_searchtree:
forall k t,
SearchTree t -> SearchTree (insert k t).
Proof.
intros.
induction t; simpl.
constructor; auto.
inv H.
destruct (k <? z) eqn:Heqe.
constructor; auto.
apply allb_impl with (R := Z.gtb).
assumption.
rewrite Z.gtb_ltb.
assumption.
destruct (z <? k) eqn:Heqe2.
constructor; auto.
apply allb_impl with (R := Z.ltb).
assumption. assumption.
constructor; auto;
pose (ltb_antisym _ _ Heqe Heqe2);
rewrite <- e;
assumption.
Qed.