61784
|
1 |
(* Author: Tobias Nipkow *)
|
|
2 |
|
|
3 |
section \<open>A 1-2 Brother Tree Implementation of Sets\<close>
|
|
4 |
|
|
5 |
theory Brother12_Set
|
|
6 |
imports
|
|
7 |
Cmp
|
|
8 |
Set_by_Ordered
|
|
9 |
begin
|
|
10 |
|
|
11 |
subsection \<open>Data Type and Operations\<close>
|
|
12 |
|
|
13 |
datatype 'a bro =
|
|
14 |
N0 |
|
|
15 |
N1 "'a bro" |
|
|
16 |
N2 "'a bro" 'a "'a bro" |
|
|
17 |
(* auxiliary constructors: *)
|
|
18 |
L2 'a |
|
|
19 |
N3 "'a bro" 'a "'a bro" 'a "'a bro"
|
|
20 |
|
|
21 |
fun inorder :: "'a bro \<Rightarrow> 'a list" where
|
|
22 |
"inorder N0 = []" |
|
|
23 |
"inorder (N1 t) = inorder t" |
|
|
24 |
"inorder (N2 l a r) = inorder l @ a # inorder r" |
|
|
25 |
"inorder (L2 a) = [a]" |
|
|
26 |
"inorder (N3 t1 a1 t2 a2 t3) = inorder t1 @ a1 # inorder t2 @ a2 # inorder t3"
|
|
27 |
|
|
28 |
fun isin :: "'a bro \<Rightarrow> 'a::cmp \<Rightarrow> bool" where
|
|
29 |
"isin N0 x = False" |
|
|
30 |
"isin (N1 t) x = isin t x" |
|
|
31 |
"isin (N2 l a r) x =
|
|
32 |
(case cmp x a of
|
|
33 |
LT \<Rightarrow> isin l x |
|
|
34 |
EQ \<Rightarrow> True |
|
|
35 |
GT \<Rightarrow> isin r x)"
|
|
36 |
|
|
37 |
fun n1 :: "'a bro \<Rightarrow> 'a bro" where
|
|
38 |
"n1 (L2 a) = N2 N0 a N0" |
|
|
39 |
"n1 (N3 t1 a1 t2 a2 t3) = N2 (N2 t1 a1 t2) a2 (N1 t3)" |
|
|
40 |
"n1 t = N1 t"
|
|
41 |
|
|
42 |
hide_const (open) insert
|
|
43 |
|
|
44 |
locale insert
|
|
45 |
begin
|
|
46 |
|
|
47 |
fun n2 :: "'a bro \<Rightarrow> 'a \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
|
|
48 |
"n2 (L2 a1) a2 t = N3 N0 a1 N0 a2 t" |
|
|
49 |
"n2 (N3 t1 a1 t2 a2 t3) a3 (N1 t4) = N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)" |
|
|
50 |
"n2 (N3 t1 a1 t2 a2 t3) a3 t4 = N3 (N2 t1 a1 t2) a2 (N1 t3) a3 t4" |
|
|
51 |
"n2 t1 a1 (L2 a2) = N3 t1 a1 N0 a2 N0" |
|
|
52 |
"n2 (N1 t1) a1 (N3 t2 a2 t3 a3 t4) = N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)" |
|
|
53 |
"n2 t1 a1 (N3 t2 a2 t3 a3 t4) = N3 t1 a1 (N1 t2) a2 (N2 t3 a3 t4)" |
|
|
54 |
"n2 t1 a t2 = N2 t1 a t2"
|
|
55 |
|
|
56 |
fun ins :: "'a::cmp \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
|
61789
|
57 |
"ins x N0 = L2 x" |
|
|
58 |
"ins x (N1 t) = n1 (ins x t)" |
|
|
59 |
"ins x (N2 l a r) =
|
|
60 |
(case cmp x a of
|
|
61 |
LT \<Rightarrow> n2 (ins x l) a r |
|
|
62 |
EQ \<Rightarrow> N2 l a r |
|
|
63 |
GT \<Rightarrow> n2 l a (ins x r))"
|
61784
|
64 |
|
|
65 |
fun tree :: "'a bro \<Rightarrow> 'a bro" where
|
|
66 |
"tree (L2 a) = N2 N0 a N0" |
|
|
67 |
"tree (N3 t1 a1 t2 a2 t3) = N2 (N2 t1 a1 t2) a2 (N1 t3)" |
|
|
68 |
"tree t = t"
|
|
69 |
|
|
70 |
definition insert :: "'a::cmp \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
|
|
71 |
"insert x t = tree(ins x t)"
|
|
72 |
|
|
73 |
end
|
|
74 |
|
|
75 |
locale delete
|
|
76 |
begin
|
|
77 |
|
|
78 |
fun n2 :: "'a bro \<Rightarrow> 'a \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
|
|
79 |
"n2 (N1 t1) a1 (N1 t2) = N1 (N2 t1 a1 t2)" |
|
|
80 |
"n2 (N1 (N1 t1)) a1 (N2 (N1 t2) a2 (N2 t3 a3 t4)) =
|
|
81 |
N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" |
|
|
82 |
"n2 (N1 (N1 t1)) a1 (N2 (N2 t2 a2 t3) a3 (N1 t4)) =
|
|
83 |
N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" |
|
|
84 |
"n2 (N1 (N1 t1)) a1 (N2 (N2 t2 a2 t3) a3 (N2 t4 a4 t5)) =
|
|
85 |
N2 (N2 (N1 t1) a1 (N2 t2 a2 t3)) a3 (N1 (N2 t4 a4 t5))" |
|
|
86 |
"n2 (N2 (N1 t1) a1 (N2 t2 a2 t3)) a3 (N1 (N1 t4)) =
|
|
87 |
N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" |
|
|
88 |
"n2 (N2 (N2 t1 a1 t2) a2 (N1 t3)) a3 (N1 (N1 t4)) =
|
|
89 |
N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" |
|
|
90 |
"n2 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)) a5 (N1 (N1 t5)) =
|
|
91 |
N2 (N1 (N2 t1 a1 t2)) a2 (N2 (N2 t3 a3 t4) a5 (N1 t5))" |
|
|
92 |
"n2 t1 a1 t2 = N2 t1 a1 t2"
|
|
93 |
|
|
94 |
fun del_min :: "'a bro \<Rightarrow> ('a \<times> 'a bro) option" where
|
|
95 |
"del_min N0 = None" |
|
|
96 |
"del_min (N1 t) =
|
|
97 |
(case del_min t of
|
|
98 |
None \<Rightarrow> None |
|
|
99 |
Some (a, t') \<Rightarrow> Some (a, N1 t'))" |
|
|
100 |
"del_min (N2 t1 a t2) =
|
|
101 |
(case del_min t1 of
|
|
102 |
None \<Rightarrow> Some (a, N1 t2) |
|
|
103 |
Some (b, t1') \<Rightarrow> Some (b, n2 t1' a t2))"
|
|
104 |
|
|
105 |
fun del :: "'a::cmp \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
|
|
106 |
"del _ N0 = N0" |
|
|
107 |
"del x (N1 t) = N1 (del x t)" |
|
|
108 |
"del x (N2 l a r) =
|
|
109 |
(case cmp x a of
|
|
110 |
LT \<Rightarrow> n2 (del x l) a r |
|
|
111 |
GT \<Rightarrow> n2 l a (del x r) |
|
|
112 |
EQ \<Rightarrow> (case del_min r of
|
|
113 |
None \<Rightarrow> N1 l |
|
|
114 |
Some (b, r') \<Rightarrow> n2 l b r'))"
|
|
115 |
|
|
116 |
fun tree :: "'a bro \<Rightarrow> 'a bro" where
|
|
117 |
"tree (N1 t) = t" |
|
|
118 |
"tree t = t"
|
|
119 |
|
|
120 |
definition delete :: "'a::cmp \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where
|
|
121 |
"delete a t = tree (del a t)"
|
|
122 |
|
|
123 |
end
|
|
124 |
|
|
125 |
subsection \<open>Invariants\<close>
|
|
126 |
|
|
127 |
fun B :: "nat \<Rightarrow> 'a bro set"
|
|
128 |
and U :: "nat \<Rightarrow> 'a bro set" where
|
|
129 |
"B 0 = {N0}" |
|
|
130 |
"B (Suc h) = { N2 t1 a t2 | t1 a t2.
|
|
131 |
t1 \<in> B h \<union> U h \<and> t2 \<in> B h \<or> t1 \<in> B h \<and> t2 \<in> B h \<union> U h}" |
|
|
132 |
"U 0 = {}" |
|
|
133 |
"U (Suc h) = N1 ` B h"
|
|
134 |
|
|
135 |
abbreviation "T h \<equiv> B h \<union> U h"
|
|
136 |
|
|
137 |
fun Bp :: "nat \<Rightarrow> 'a bro set" where
|
|
138 |
"Bp 0 = B 0 \<union> L2 ` UNIV" |
|
|
139 |
"Bp (Suc 0) = B (Suc 0) \<union> {N3 N0 a N0 b N0|a b. True}" |
|
|
140 |
"Bp (Suc(Suc h)) = B (Suc(Suc h)) \<union>
|
|
141 |
{N3 t1 a t2 b t3 | t1 a t2 b t3. t1 \<in> B (Suc h) \<and> t2 \<in> U (Suc h) \<and> t3 \<in> B (Suc h)}"
|
|
142 |
|
|
143 |
fun Um :: "nat \<Rightarrow> 'a bro set" where
|
|
144 |
"Um 0 = {}" |
|
|
145 |
"Um (Suc h) = N1 ` T h"
|
|
146 |
|
|
147 |
|
|
148 |
subsection "Functional Correctness Proofs"
|
|
149 |
|
|
150 |
subsubsection "Proofs for isin"
|
|
151 |
|
|
152 |
lemma
|
|
153 |
"t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems(inorder t))"
|
|
154 |
by(induction h arbitrary: t) (fastforce simp: elems_simps1 split: if_splits)+
|
|
155 |
|
|
156 |
lemma isin_set: "t \<in> T h \<Longrightarrow>
|
|
157 |
sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems(inorder t))"
|
|
158 |
by(induction h arbitrary: t) (auto simp: elems_simps2 split: if_splits)
|
|
159 |
|
|
160 |
subsubsection "Proofs for insertion"
|
|
161 |
|
|
162 |
lemma inorder_n1: "inorder(n1 t) = inorder t"
|
|
163 |
by(induction t rule: n1.induct) (auto simp: sorted_lems)
|
|
164 |
|
|
165 |
context insert
|
|
166 |
begin
|
|
167 |
|
|
168 |
lemma inorder_n2: "inorder(n2 l a r) = inorder l @ a # inorder r"
|
|
169 |
by(cases "(l,a,r)" rule: n2.cases) (auto simp: sorted_lems)
|
|
170 |
|
|
171 |
lemma inorder_tree: "inorder(tree t) = inorder t"
|
|
172 |
by(cases t) auto
|
|
173 |
|
|
174 |
lemma inorder_ins: "t \<in> T h \<Longrightarrow>
|
|
175 |
sorted(inorder t) \<Longrightarrow> inorder(ins a t) = ins_list a (inorder t)"
|
|
176 |
by(induction h arbitrary: t) (auto simp: ins_list_simps inorder_n1 inorder_n2)
|
|
177 |
|
|
178 |
lemma inorder_insert: "t \<in> T h \<Longrightarrow>
|
|
179 |
sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)"
|
|
180 |
by(simp add: insert_def inorder_ins inorder_tree)
|
|
181 |
|
|
182 |
end
|
|
183 |
|
|
184 |
subsubsection \<open>Proofs for deletion\<close>
|
|
185 |
|
|
186 |
context delete
|
|
187 |
begin
|
|
188 |
|
|
189 |
lemma inorder_tree: "inorder(tree t) = inorder t"
|
|
190 |
by(cases t) auto
|
|
191 |
|
|
192 |
lemma inorder_n2: "inorder(n2 l a r) = inorder l @ a # inorder r"
|
|
193 |
by(induction l a r rule: n2.induct) (auto)
|
|
194 |
|
|
195 |
lemma inorder_del_min:
|
61792
|
196 |
"t \<in> T h \<Longrightarrow> (del_min t = None \<longleftrightarrow> inorder t = []) \<and>
|
61784
|
197 |
(del_min t = Some(a,t') \<longrightarrow> inorder t = a # inorder t')"
|
|
198 |
by(induction h arbitrary: t a t') (auto simp: inorder_n2 split: option.splits)
|
|
199 |
|
|
200 |
lemma inorder_del:
|
61792
|
201 |
"t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(del x t) = del_list x (inorder t)"
|
|
202 |
by(induction h arbitrary: t) (auto simp: del_list_simps inorder_n2
|
|
203 |
inorder_del_min[OF UnI1] inorder_del_min[OF UnI2] split: option.splits)
|
|
204 |
|
|
205 |
lemma inorder_delete:
|
|
206 |
"t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)"
|
|
207 |
by(simp add: delete_def inorder_del inorder_tree)
|
61784
|
208 |
|
|
209 |
end
|
|
210 |
|
|
211 |
|
|
212 |
subsection \<open>Invariant Proofs\<close>
|
|
213 |
|
61789
|
214 |
subsubsection \<open>Proofs for insertion\<close>
|
61784
|
215 |
|
|
216 |
lemma n1_type: "t \<in> Bp h \<Longrightarrow> n1 t \<in> T (Suc h)"
|
|
217 |
by(cases h rule: Bp.cases) auto
|
|
218 |
|
|
219 |
context insert
|
|
220 |
begin
|
|
221 |
|
61809
|
222 |
lemma tree_type: "t \<in> Bp h \<Longrightarrow> tree t \<in> B h \<union> B (Suc h)"
|
61784
|
223 |
by(cases h rule: Bp.cases) auto
|
|
224 |
|
|
225 |
lemma n2_type:
|
|
226 |
"(t1 \<in> Bp h \<and> t2 \<in> T h \<longrightarrow> n2 t1 a t2 \<in> Bp (Suc h)) \<and>
|
|
227 |
(t1 \<in> T h \<and> t2 \<in> Bp h \<longrightarrow> n2 t1 a t2 \<in> Bp (Suc h))"
|
|
228 |
apply(cases h rule: Bp.cases)
|
|
229 |
apply (auto)[2]
|
|
230 |
apply(rule conjI impI | erule conjE exE imageE | simp | erule disjE)+
|
|
231 |
done
|
|
232 |
|
|
233 |
lemma Bp_if_B: "t \<in> B h \<Longrightarrow> t \<in> Bp h"
|
|
234 |
by (cases h rule: Bp.cases) simp_all
|
|
235 |
|
|
236 |
text{* An automatic proof: *}
|
|
237 |
|
|
238 |
lemma
|
|
239 |
"(t \<in> B h \<longrightarrow> ins x t \<in> Bp h) \<and> (t \<in> U h \<longrightarrow> ins x t \<in> T h)"
|
|
240 |
apply(induction h arbitrary: t)
|
|
241 |
apply (simp)
|
|
242 |
apply (fastforce simp: Bp_if_B n2_type dest: n1_type)
|
|
243 |
done
|
|
244 |
|
|
245 |
text{* A detailed proof: *}
|
|
246 |
|
|
247 |
lemma ins_type:
|
|
248 |
shows "t \<in> B h \<Longrightarrow> ins x t \<in> Bp h" and "t \<in> U h \<Longrightarrow> ins x t \<in> T h"
|
|
249 |
proof(induction h arbitrary: t)
|
|
250 |
case 0
|
|
251 |
{ case 1 thus ?case by simp
|
|
252 |
next
|
|
253 |
case 2 thus ?case by simp }
|
|
254 |
next
|
|
255 |
case (Suc h)
|
|
256 |
{ case 1
|
|
257 |
then obtain t1 a t2 where [simp]: "t = N2 t1 a t2" and
|
|
258 |
t1: "t1 \<in> T h" and t2: "t2 \<in> T h" and t12: "t1 \<in> B h \<or> t2 \<in> B h"
|
|
259 |
by auto
|
|
260 |
{ assume "x < a"
|
|
261 |
hence "?case \<longleftrightarrow> n2 (ins x t1) a t2 \<in> Bp (Suc h)" by simp
|
|
262 |
also have "\<dots>"
|
|
263 |
proof cases
|
|
264 |
assume "t1 \<in> B h"
|
|
265 |
with t2 show ?thesis by (simp add: Suc.IH(1) n2_type)
|
|
266 |
next
|
|
267 |
assume "t1 \<notin> B h"
|
|
268 |
hence 1: "t1 \<in> U h" and 2: "t2 \<in> B h" using t1 t12 by auto
|
|
269 |
show ?thesis by (metis Suc.IH(2)[OF 1] Bp_if_B[OF 2] n2_type)
|
|
270 |
qed
|
|
271 |
finally have ?case .
|
|
272 |
}
|
|
273 |
moreover
|
|
274 |
{ assume "a < x"
|
|
275 |
hence "?case \<longleftrightarrow> n2 t1 a (ins x t2) \<in> Bp (Suc h)" by simp
|
|
276 |
also have "\<dots>"
|
|
277 |
proof cases
|
|
278 |
assume "t2 \<in> B h"
|
|
279 |
with t1 show ?thesis by (simp add: Suc.IH(1) n2_type)
|
|
280 |
next
|
|
281 |
assume "t2 \<notin> B h"
|
|
282 |
hence 1: "t1 \<in> B h" and 2: "t2 \<in> U h" using t2 t12 by auto
|
|
283 |
show ?thesis by (metis Bp_if_B[OF 1] Suc.IH(2)[OF 2] n2_type)
|
|
284 |
qed
|
|
285 |
}
|
|
286 |
moreover
|
|
287 |
{ assume "x = a"
|
|
288 |
from 1 have "t \<in> Bp (Suc h)" by(rule Bp_if_B)
|
|
289 |
hence "?case" using `x = a` by simp
|
|
290 |
}
|
|
291 |
ultimately show ?case by auto
|
|
292 |
next
|
|
293 |
case 2 thus ?case using Suc(1) n1_type by fastforce }
|
|
294 |
qed
|
|
295 |
|
|
296 |
lemma insert_type:
|
61809
|
297 |
"t \<in> B h \<Longrightarrow> insert x t \<in> B h \<union> B (Suc h)"
|
|
298 |
unfolding insert_def by (metis ins_type(1) tree_type)
|
61784
|
299 |
|
|
300 |
end
|
|
301 |
|
61789
|
302 |
subsubsection "Proofs for deletion"
|
61784
|
303 |
|
|
304 |
lemma B_simps[simp]:
|
|
305 |
"N1 t \<in> B h = False"
|
|
306 |
"L2 y \<in> B h = False"
|
|
307 |
"(N3 t1 a1 t2 a2 t3) \<in> B h = False"
|
|
308 |
"N0 \<in> B h \<longleftrightarrow> h = 0"
|
|
309 |
by (cases h, auto)+
|
|
310 |
|
|
311 |
context delete
|
|
312 |
begin
|
|
313 |
|
|
314 |
lemma n2_type1:
|
|
315 |
"\<lbrakk>t1 \<in> Um h; t2 \<in> B h\<rbrakk> \<Longrightarrow> n2 t1 a t2 \<in> T (Suc h)"
|
|
316 |
apply(cases h rule: Bp.cases)
|
|
317 |
apply auto[2]
|
|
318 |
apply(erule exE bexE conjE imageE | simp | erule disjE)+
|
|
319 |
done
|
|
320 |
|
|
321 |
lemma n2_type2:
|
|
322 |
"\<lbrakk>t1 \<in> B h ; t2 \<in> Um h \<rbrakk> \<Longrightarrow> n2 t1 a t2 \<in> T (Suc h)"
|
|
323 |
apply(cases h rule: Bp.cases)
|
|
324 |
apply auto[2]
|
|
325 |
apply(erule exE bexE conjE imageE | simp | erule disjE)+
|
|
326 |
done
|
|
327 |
|
|
328 |
lemma n2_type3:
|
|
329 |
"\<lbrakk>t1 \<in> T h ; t2 \<in> T h \<rbrakk> \<Longrightarrow> n2 t1 a t2 \<in> T (Suc h)"
|
|
330 |
apply(cases h rule: Bp.cases)
|
|
331 |
apply auto[2]
|
|
332 |
apply(erule exE bexE conjE imageE | simp | erule disjE)+
|
|
333 |
done
|
|
334 |
|
|
335 |
lemma del_minNoneN0: "\<lbrakk>t \<in> B h; del_min t = None\<rbrakk> \<Longrightarrow> t = N0"
|
|
336 |
by (cases t) (auto split: option.splits)
|
|
337 |
|
|
338 |
lemma del_minNoneN1 : "\<lbrakk>t \<in> U h; del_min t = None\<rbrakk> \<Longrightarrow> t = N1 N0"
|
|
339 |
by (cases h) (auto simp: del_minNoneN0 split: option.splits)
|
|
340 |
|
|
341 |
lemma del_min_type:
|
|
342 |
"t \<in> B h \<Longrightarrow> del_min t = Some (a, t') \<Longrightarrow> t' \<in> T h"
|
|
343 |
"t \<in> U h \<Longrightarrow> del_min t = Some (a, t') \<Longrightarrow> t' \<in> Um h"
|
|
344 |
proof (induction h arbitrary: t a t')
|
|
345 |
case (Suc h)
|
|
346 |
{ case 1
|
|
347 |
then obtain t1 a t2 where [simp]: "t = N2 t1 a t2" and
|
|
348 |
t12: "t1 \<in> T h" "t2 \<in> T h" "t1 \<in> B h \<or> t2 \<in> B h"
|
|
349 |
by auto
|
|
350 |
show ?case
|
|
351 |
proof (cases "del_min t1")
|
|
352 |
case None
|
|
353 |
show ?thesis
|
|
354 |
proof cases
|
|
355 |
assume "t1 \<in> B h"
|
|
356 |
with del_minNoneN0[OF this None] 1 show ?thesis by(auto)
|
|
357 |
next
|
|
358 |
assume "t1 \<notin> B h"
|
|
359 |
thus ?thesis using 1 None by (auto)
|
|
360 |
qed
|
|
361 |
next
|
|
362 |
case [simp]: (Some bt')
|
|
363 |
obtain b t1' where [simp]: "bt' = (b,t1')" by fastforce
|
|
364 |
show ?thesis
|
|
365 |
proof cases
|
|
366 |
assume "t1 \<in> B h"
|
|
367 |
from Suc.IH(1)[OF this] 1 have "t1' \<in> T h" by simp
|
|
368 |
from n2_type3[OF this t12(2)] 1 show ?thesis by auto
|
|
369 |
next
|
|
370 |
assume "t1 \<notin> B h"
|
|
371 |
hence t1: "t1 \<in> U h" and t2: "t2 \<in> B h" using t12 by auto
|
|
372 |
from Suc.IH(2)[OF t1] have "t1' \<in> Um h" by simp
|
|
373 |
from n2_type1[OF this t2] 1 show ?thesis by auto
|
|
374 |
qed
|
|
375 |
qed
|
|
376 |
}
|
|
377 |
{ case 2
|
|
378 |
then obtain t1 where [simp]: "t = N1 t1" and t1: "t1 \<in> B h" by auto
|
|
379 |
show ?case
|
|
380 |
proof (cases "del_min t1")
|
|
381 |
case None
|
|
382 |
with del_minNoneN0[OF t1 None] 2 show ?thesis by(auto)
|
|
383 |
next
|
|
384 |
case [simp]: (Some bt')
|
|
385 |
obtain b t1' where [simp]: "bt' = (b,t1')" by fastforce
|
|
386 |
from Suc.IH(1)[OF t1] have "t1' \<in> T h" by simp
|
|
387 |
thus ?thesis using 2 by auto
|
|
388 |
qed
|
|
389 |
}
|
|
390 |
qed auto
|
|
391 |
|
|
392 |
lemma del_type:
|
|
393 |
"t \<in> B h \<Longrightarrow> del x t \<in> T h"
|
|
394 |
"t \<in> U h \<Longrightarrow> del x t \<in> Um h"
|
|
395 |
proof (induction h arbitrary: x t)
|
|
396 |
case (Suc h)
|
|
397 |
{ case 1
|
|
398 |
then obtain l a r where [simp]: "t = N2 l a r" and
|
|
399 |
lr: "l \<in> T h" "r \<in> T h" "l \<in> B h \<or> r \<in> B h" by auto
|
|
400 |
{ assume "x < a"
|
|
401 |
have ?case
|
|
402 |
proof cases
|
|
403 |
assume "l \<in> B h"
|
|
404 |
from n2_type3[OF Suc.IH(1)[OF this] lr(2)]
|
|
405 |
show ?thesis using `x<a` by(simp)
|
|
406 |
next
|
|
407 |
assume "l \<notin> B h"
|
|
408 |
hence "l \<in> U h" "r \<in> B h" using lr by auto
|
|
409 |
from n2_type1[OF Suc.IH(2)[OF this(1)] this(2)]
|
|
410 |
show ?thesis using `x<a` by(simp)
|
|
411 |
qed
|
|
412 |
} moreover
|
|
413 |
{ assume "x > a"
|
|
414 |
have ?case
|
|
415 |
proof cases
|
|
416 |
assume "r \<in> B h"
|
|
417 |
from n2_type3[OF lr(1) Suc.IH(1)[OF this]]
|
|
418 |
show ?thesis using `x>a` by(simp)
|
|
419 |
next
|
|
420 |
assume "r \<notin> B h"
|
|
421 |
hence "l \<in> B h" "r \<in> U h" using lr by auto
|
|
422 |
from n2_type2[OF this(1) Suc.IH(2)[OF this(2)]]
|
|
423 |
show ?thesis using `x>a` by(simp)
|
|
424 |
qed
|
|
425 |
} moreover
|
|
426 |
{ assume [simp]: "x=a"
|
|
427 |
have ?case
|
|
428 |
proof (cases "del_min r")
|
|
429 |
case None
|
|
430 |
show ?thesis
|
|
431 |
proof cases
|
|
432 |
assume "r \<in> B h"
|
|
433 |
with del_minNoneN0[OF this None] lr show ?thesis by(simp)
|
|
434 |
next
|
|
435 |
assume "r \<notin> B h"
|
|
436 |
hence "r \<in> U h" using lr by auto
|
|
437 |
with del_minNoneN1[OF this None] lr(3) show ?thesis by (simp)
|
|
438 |
qed
|
|
439 |
next
|
|
440 |
case [simp]: (Some br')
|
|
441 |
obtain b r' where [simp]: "br' = (b,r')" by fastforce
|
|
442 |
show ?thesis
|
|
443 |
proof cases
|
|
444 |
assume "r \<in> B h"
|
|
445 |
from del_min_type(1)[OF this] n2_type3[OF lr(1)]
|
|
446 |
show ?thesis by simp
|
|
447 |
next
|
|
448 |
assume "r \<notin> B h"
|
|
449 |
hence "l \<in> B h" and "r \<in> U h" using lr by auto
|
|
450 |
from del_min_type(2)[OF this(2)] n2_type2[OF this(1)]
|
|
451 |
show ?thesis by simp
|
|
452 |
qed
|
|
453 |
qed
|
|
454 |
} ultimately show ?case by auto
|
|
455 |
}
|
|
456 |
{ case 2 with Suc.IH(1) show ?case by auto }
|
|
457 |
qed auto
|
|
458 |
|
61809
|
459 |
lemma tree_type: "t \<in> T (h+1) \<Longrightarrow> tree t : B (h+1) \<union> B h"
|
61784
|
460 |
by(auto)
|
|
461 |
|
61809
|
462 |
lemma delete_type: "t \<in> B h \<Longrightarrow> delete x t \<in> B h \<union> B(h-1)"
|
61784
|
463 |
unfolding delete_def
|
61809
|
464 |
by (cases h) (simp, metis del_type(1) tree_type Suc_eq_plus1 diff_Suc_1)
|
61784
|
465 |
|
|
466 |
end
|
|
467 |
|
61789
|
468 |
|
61784
|
469 |
subsection "Overall correctness"
|
|
470 |
|
|
471 |
interpretation Set_by_Ordered
|
61789
|
472 |
where empty = N0 and isin = isin and insert = insert.insert
|
61809
|
473 |
and delete = delete.delete and inorder = inorder and inv = "\<lambda>t. \<exists>h. t \<in> B h"
|
61784
|
474 |
proof (standard, goal_cases)
|
|
475 |
case 2 thus ?case by(auto intro!: isin_set)
|
|
476 |
next
|
|
477 |
case 3 thus ?case by(auto intro!: insert.inorder_insert)
|
|
478 |
next
|
61792
|
479 |
case 4 thus ?case by(auto intro!: delete.inorder_delete)
|
61784
|
480 |
next
|
|
481 |
case 6 thus ?case using insert.insert_type by blast
|
|
482 |
next
|
|
483 |
case 7 thus ?case using delete.delete_type by blast
|
|
484 |
qed auto
|
|
485 |
|
|
486 |
end
|