author | nipkow |
Sun, 21 Jan 2018 11:04:18 +0100 | |
changeset 67481 | df252c3d48f2 |
parent 67406 | 23307fd33906 |
child 67613 | ce654b0e6d69 |
permissions | -rw-r--r-- |
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
1 |
(* Author: Tobias Nipkow, Daniel Stüwe *) |
61784 | 2 |
|
62130 | 3 |
section \<open>1-2 Brother Tree Implementation of Sets\<close> |
61784 | 4 |
|
5 |
theory Brother12_Set |
|
6 |
imports |
|
7 |
Cmp |
|
8 |
Set_by_Ordered |
|
66453
cc19f7ca2ed6
session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents:
63411
diff
changeset
|
9 |
"HOL-Number_Theory.Fib" |
61784 | 10 |
begin |
11 |
||
12 |
subsection \<open>Data Type and Operations\<close> |
|
13 |
||
14 |
datatype 'a bro = |
|
15 |
N0 | |
|
16 |
N1 "'a bro" | |
|
17 |
N2 "'a bro" 'a "'a bro" | |
|
18 |
(* auxiliary constructors: *) |
|
19 |
L2 'a | |
|
20 |
N3 "'a bro" 'a "'a bro" 'a "'a bro" |
|
21 |
||
22 |
fun inorder :: "'a bro \<Rightarrow> 'a list" where |
|
23 |
"inorder N0 = []" | |
|
24 |
"inorder (N1 t) = inorder t" | |
|
25 |
"inorder (N2 l a r) = inorder l @ a # inorder r" | |
|
26 |
"inorder (L2 a) = [a]" | |
|
27 |
"inorder (N3 t1 a1 t2 a2 t3) = inorder t1 @ a1 # inorder t2 @ a2 # inorder t3" |
|
28 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
29 |
fun isin :: "'a bro \<Rightarrow> 'a::linorder \<Rightarrow> bool" where |
61784 | 30 |
"isin N0 x = False" | |
31 |
"isin (N1 t) x = isin t x" | |
|
32 |
"isin (N2 l a r) x = |
|
33 |
(case cmp x a of |
|
34 |
LT \<Rightarrow> isin l x | |
|
35 |
EQ \<Rightarrow> True | |
|
36 |
GT \<Rightarrow> isin r x)" |
|
37 |
||
38 |
fun n1 :: "'a bro \<Rightarrow> 'a bro" where |
|
39 |
"n1 (L2 a) = N2 N0 a N0" | |
|
40 |
"n1 (N3 t1 a1 t2 a2 t3) = N2 (N2 t1 a1 t2) a2 (N1 t3)" | |
|
41 |
"n1 t = N1 t" |
|
42 |
||
43 |
hide_const (open) insert |
|
44 |
||
45 |
locale insert |
|
46 |
begin |
|
47 |
||
48 |
fun n2 :: "'a bro \<Rightarrow> 'a \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where |
|
49 |
"n2 (L2 a1) a2 t = N3 N0 a1 N0 a2 t" | |
|
50 |
"n2 (N3 t1 a1 t2 a2 t3) a3 (N1 t4) = N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)" | |
|
51 |
"n2 (N3 t1 a1 t2 a2 t3) a3 t4 = N3 (N2 t1 a1 t2) a2 (N1 t3) a3 t4" | |
|
52 |
"n2 t1 a1 (L2 a2) = N3 t1 a1 N0 a2 N0" | |
|
53 |
"n2 (N1 t1) a1 (N3 t2 a2 t3 a3 t4) = N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)" | |
|
54 |
"n2 t1 a1 (N3 t2 a2 t3 a3 t4) = N3 t1 a1 (N1 t2) a2 (N2 t3 a3 t4)" | |
|
55 |
"n2 t1 a t2 = N2 t1 a t2" |
|
56 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
57 |
fun ins :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where |
61789 | 58 |
"ins x N0 = L2 x" | |
59 |
"ins x (N1 t) = n1 (ins x t)" | |
|
60 |
"ins x (N2 l a r) = |
|
61 |
(case cmp x a of |
|
62 |
LT \<Rightarrow> n2 (ins x l) a r | |
|
63 |
EQ \<Rightarrow> N2 l a r | |
|
64 |
GT \<Rightarrow> n2 l a (ins x r))" |
|
61784 | 65 |
|
66 |
fun tree :: "'a bro \<Rightarrow> 'a bro" where |
|
67 |
"tree (L2 a) = N2 N0 a N0" | |
|
68 |
"tree (N3 t1 a1 t2 a2 t3) = N2 (N2 t1 a1 t2) a2 (N1 t3)" | |
|
69 |
"tree t = t" |
|
70 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
71 |
definition insert :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where |
61784 | 72 |
"insert x t = tree(ins x t)" |
73 |
||
74 |
end |
|
75 |
||
76 |
locale delete |
|
77 |
begin |
|
78 |
||
79 |
fun n2 :: "'a bro \<Rightarrow> 'a \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where |
|
80 |
"n2 (N1 t1) a1 (N1 t2) = N1 (N2 t1 a1 t2)" | |
|
81 |
"n2 (N1 (N1 t1)) a1 (N2 (N1 t2) a2 (N2 t3 a3 t4)) = |
|
82 |
N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" | |
|
83 |
"n2 (N1 (N1 t1)) a1 (N2 (N2 t2 a2 t3) a3 (N1 t4)) = |
|
84 |
N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" | |
|
85 |
"n2 (N1 (N1 t1)) a1 (N2 (N2 t2 a2 t3) a3 (N2 t4 a4 t5)) = |
|
86 |
N2 (N2 (N1 t1) a1 (N2 t2 a2 t3)) a3 (N1 (N2 t4 a4 t5))" | |
|
87 |
"n2 (N2 (N1 t1) a1 (N2 t2 a2 t3)) a3 (N1 (N1 t4)) = |
|
88 |
N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" | |
|
89 |
"n2 (N2 (N2 t1 a1 t2) a2 (N1 t3)) a3 (N1 (N1 t4)) = |
|
90 |
N1 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4))" | |
|
91 |
"n2 (N2 (N2 t1 a1 t2) a2 (N2 t3 a3 t4)) a5 (N1 (N1 t5)) = |
|
92 |
N2 (N1 (N2 t1 a1 t2)) a2 (N2 (N2 t3 a3 t4) a5 (N1 t5))" | |
|
93 |
"n2 t1 a1 t2 = N2 t1 a1 t2" |
|
94 |
||
95 |
fun del_min :: "'a bro \<Rightarrow> ('a \<times> 'a bro) option" where |
|
96 |
"del_min N0 = None" | |
|
97 |
"del_min (N1 t) = |
|
98 |
(case del_min t of |
|
99 |
None \<Rightarrow> None | |
|
100 |
Some (a, t') \<Rightarrow> Some (a, N1 t'))" | |
|
101 |
"del_min (N2 t1 a t2) = |
|
102 |
(case del_min t1 of |
|
103 |
None \<Rightarrow> Some (a, N1 t2) | |
|
104 |
Some (b, t1') \<Rightarrow> Some (b, n2 t1' a t2))" |
|
105 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
106 |
fun del :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where |
61784 | 107 |
"del _ N0 = N0" | |
108 |
"del x (N1 t) = N1 (del x t)" | |
|
109 |
"del x (N2 l a r) = |
|
110 |
(case cmp x a of |
|
111 |
LT \<Rightarrow> n2 (del x l) a r | |
|
112 |
GT \<Rightarrow> n2 l a (del x r) | |
|
113 |
EQ \<Rightarrow> (case del_min r of |
|
114 |
None \<Rightarrow> N1 l | |
|
115 |
Some (b, r') \<Rightarrow> n2 l b r'))" |
|
116 |
||
117 |
fun tree :: "'a bro \<Rightarrow> 'a bro" where |
|
118 |
"tree (N1 t) = t" | |
|
119 |
"tree t = t" |
|
120 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
121 |
definition delete :: "'a::linorder \<Rightarrow> 'a bro \<Rightarrow> 'a bro" where |
61784 | 122 |
"delete a t = tree (del a t)" |
123 |
||
124 |
end |
|
125 |
||
126 |
subsection \<open>Invariants\<close> |
|
127 |
||
128 |
fun B :: "nat \<Rightarrow> 'a bro set" |
|
129 |
and U :: "nat \<Rightarrow> 'a bro set" where |
|
130 |
"B 0 = {N0}" | |
|
131 |
"B (Suc h) = { N2 t1 a t2 | t1 a t2. |
|
132 |
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}" | |
|
133 |
"U 0 = {}" | |
|
134 |
"U (Suc h) = N1 ` B h" |
|
135 |
||
136 |
abbreviation "T h \<equiv> B h \<union> U h" |
|
137 |
||
138 |
fun Bp :: "nat \<Rightarrow> 'a bro set" where |
|
139 |
"Bp 0 = B 0 \<union> L2 ` UNIV" | |
|
140 |
"Bp (Suc 0) = B (Suc 0) \<union> {N3 N0 a N0 b N0|a b. True}" | |
|
141 |
"Bp (Suc(Suc h)) = B (Suc(Suc h)) \<union> |
|
142 |
{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)}" |
|
143 |
||
144 |
fun Um :: "nat \<Rightarrow> 'a bro set" where |
|
145 |
"Um 0 = {}" | |
|
146 |
"Um (Suc h) = N1 ` T h" |
|
147 |
||
148 |
||
149 |
subsection "Functional Correctness Proofs" |
|
150 |
||
151 |
subsubsection "Proofs for isin" |
|
152 |
||
153 |
lemma |
|
154 |
"t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems(inorder t))" |
|
155 |
by(induction h arbitrary: t) (fastforce simp: elems_simps1 split: if_splits)+ |
|
156 |
||
157 |
lemma isin_set: "t \<in> T h \<Longrightarrow> |
|
158 |
sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems(inorder t))" |
|
159 |
by(induction h arbitrary: t) (auto simp: elems_simps2 split: if_splits) |
|
160 |
||
161 |
subsubsection "Proofs for insertion" |
|
162 |
||
163 |
lemma inorder_n1: "inorder(n1 t) = inorder t" |
|
62526 | 164 |
by(cases t rule: n1.cases) (auto simp: sorted_lems) |
61784 | 165 |
|
166 |
context insert |
|
167 |
begin |
|
168 |
||
169 |
lemma inorder_n2: "inorder(n2 l a r) = inorder l @ a # inorder r" |
|
170 |
by(cases "(l,a,r)" rule: n2.cases) (auto simp: sorted_lems) |
|
171 |
||
172 |
lemma inorder_tree: "inorder(tree t) = inorder t" |
|
173 |
by(cases t) auto |
|
174 |
||
175 |
lemma inorder_ins: "t \<in> T h \<Longrightarrow> |
|
176 |
sorted(inorder t) \<Longrightarrow> inorder(ins a t) = ins_list a (inorder t)" |
|
177 |
by(induction h arbitrary: t) (auto simp: ins_list_simps inorder_n1 inorder_n2) |
|
178 |
||
179 |
lemma inorder_insert: "t \<in> T h \<Longrightarrow> |
|
180 |
sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)" |
|
181 |
by(simp add: insert_def inorder_ins inorder_tree) |
|
182 |
||
183 |
end |
|
184 |
||
185 |
subsubsection \<open>Proofs for deletion\<close> |
|
186 |
||
187 |
context delete |
|
188 |
begin |
|
189 |
||
190 |
lemma inorder_tree: "inorder(tree t) = inorder t" |
|
191 |
by(cases t) auto |
|
192 |
||
193 |
lemma inorder_n2: "inorder(n2 l a r) = inorder l @ a # inorder r" |
|
62526 | 194 |
by(cases "(l,a,r)" rule: n2.cases) (auto) |
61784 | 195 |
|
196 |
lemma inorder_del_min: |
|
61792 | 197 |
"t \<in> T h \<Longrightarrow> (del_min t = None \<longleftrightarrow> inorder t = []) \<and> |
61784 | 198 |
(del_min t = Some(a,t') \<longrightarrow> inorder t = a # inorder t')" |
199 |
by(induction h arbitrary: t a t') (auto simp: inorder_n2 split: option.splits) |
|
200 |
||
201 |
lemma inorder_del: |
|
61792 | 202 |
"t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(del x t) = del_list x (inorder t)" |
203 |
by(induction h arbitrary: t) (auto simp: del_list_simps inorder_n2 |
|
204 |
inorder_del_min[OF UnI1] inorder_del_min[OF UnI2] split: option.splits) |
|
205 |
||
206 |
lemma inorder_delete: |
|
207 |
"t \<in> T h \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)" |
|
208 |
by(simp add: delete_def inorder_del inorder_tree) |
|
61784 | 209 |
|
210 |
end |
|
211 |
||
212 |
||
213 |
subsection \<open>Invariant Proofs\<close> |
|
214 |
||
61789 | 215 |
subsubsection \<open>Proofs for insertion\<close> |
61784 | 216 |
|
217 |
lemma n1_type: "t \<in> Bp h \<Longrightarrow> n1 t \<in> T (Suc h)" |
|
218 |
by(cases h rule: Bp.cases) auto |
|
219 |
||
220 |
context insert |
|
221 |
begin |
|
222 |
||
61809 | 223 |
lemma tree_type: "t \<in> Bp h \<Longrightarrow> tree t \<in> B h \<union> B (Suc h)" |
61784 | 224 |
by(cases h rule: Bp.cases) auto |
225 |
||
226 |
lemma n2_type: |
|
227 |
"(t1 \<in> Bp h \<and> t2 \<in> T h \<longrightarrow> n2 t1 a t2 \<in> Bp (Suc h)) \<and> |
|
228 |
(t1 \<in> T h \<and> t2 \<in> Bp h \<longrightarrow> n2 t1 a t2 \<in> Bp (Suc h))" |
|
229 |
apply(cases h rule: Bp.cases) |
|
230 |
apply (auto)[2] |
|
231 |
apply(rule conjI impI | erule conjE exE imageE | simp | erule disjE)+ |
|
232 |
done |
|
233 |
||
234 |
lemma Bp_if_B: "t \<in> B h \<Longrightarrow> t \<in> Bp h" |
|
235 |
by (cases h rule: Bp.cases) simp_all |
|
236 |
||
67406 | 237 |
text\<open>An automatic proof:\<close> |
61784 | 238 |
|
239 |
lemma |
|
240 |
"(t \<in> B h \<longrightarrow> ins x t \<in> Bp h) \<and> (t \<in> U h \<longrightarrow> ins x t \<in> T h)" |
|
241 |
apply(induction h arbitrary: t) |
|
242 |
apply (simp) |
|
243 |
apply (fastforce simp: Bp_if_B n2_type dest: n1_type) |
|
244 |
done |
|
245 |
||
67406 | 246 |
text\<open>A detailed proof:\<close> |
61784 | 247 |
|
248 |
lemma ins_type: |
|
249 |
shows "t \<in> B h \<Longrightarrow> ins x t \<in> Bp h" and "t \<in> U h \<Longrightarrow> ins x t \<in> T h" |
|
250 |
proof(induction h arbitrary: t) |
|
251 |
case 0 |
|
252 |
{ case 1 thus ?case by simp |
|
253 |
next |
|
254 |
case 2 thus ?case by simp } |
|
255 |
next |
|
256 |
case (Suc h) |
|
257 |
{ case 1 |
|
258 |
then obtain t1 a t2 where [simp]: "t = N2 t1 a t2" and |
|
259 |
t1: "t1 \<in> T h" and t2: "t2 \<in> T h" and t12: "t1 \<in> B h \<or> t2 \<in> B h" |
|
260 |
by auto |
|
67040 | 261 |
have ?case if "x < a" |
262 |
proof - |
|
263 |
have "n2 (ins x t1) a t2 \<in> Bp (Suc h)" |
|
61784 | 264 |
proof cases |
265 |
assume "t1 \<in> B h" |
|
266 |
with t2 show ?thesis by (simp add: Suc.IH(1) n2_type) |
|
267 |
next |
|
268 |
assume "t1 \<notin> B h" |
|
269 |
hence 1: "t1 \<in> U h" and 2: "t2 \<in> B h" using t1 t12 by auto |
|
270 |
show ?thesis by (metis Suc.IH(2)[OF 1] Bp_if_B[OF 2] n2_type) |
|
271 |
qed |
|
67406 | 272 |
with \<open>x < a\<close> show ?case by simp |
67040 | 273 |
qed |
61784 | 274 |
moreover |
67040 | 275 |
have ?case if "a < x" |
276 |
proof - |
|
277 |
have "n2 t1 a (ins x t2) \<in> Bp (Suc h)" |
|
61784 | 278 |
proof cases |
279 |
assume "t2 \<in> B h" |
|
280 |
with t1 show ?thesis by (simp add: Suc.IH(1) n2_type) |
|
281 |
next |
|
282 |
assume "t2 \<notin> B h" |
|
283 |
hence 1: "t1 \<in> B h" and 2: "t2 \<in> U h" using t2 t12 by auto |
|
284 |
show ?thesis by (metis Bp_if_B[OF 1] Suc.IH(2)[OF 2] n2_type) |
|
285 |
qed |
|
67406 | 286 |
with \<open>a < x\<close> show ?case by simp |
67040 | 287 |
qed |
288 |
moreover |
|
289 |
have ?case if "x = a" |
|
290 |
proof - |
|
61784 | 291 |
from 1 have "t \<in> Bp (Suc h)" by(rule Bp_if_B) |
67406 | 292 |
thus "?case" using \<open>x = a\<close> by simp |
67040 | 293 |
qed |
61784 | 294 |
ultimately show ?case by auto |
295 |
next |
|
296 |
case 2 thus ?case using Suc(1) n1_type by fastforce } |
|
297 |
qed |
|
298 |
||
299 |
lemma insert_type: |
|
61809 | 300 |
"t \<in> B h \<Longrightarrow> insert x t \<in> B h \<union> B (Suc h)" |
301 |
unfolding insert_def by (metis ins_type(1) tree_type) |
|
61784 | 302 |
|
303 |
end |
|
304 |
||
61789 | 305 |
subsubsection "Proofs for deletion" |
61784 | 306 |
|
307 |
lemma B_simps[simp]: |
|
308 |
"N1 t \<in> B h = False" |
|
309 |
"L2 y \<in> B h = False" |
|
310 |
"(N3 t1 a1 t2 a2 t3) \<in> B h = False" |
|
311 |
"N0 \<in> B h \<longleftrightarrow> h = 0" |
|
312 |
by (cases h, auto)+ |
|
313 |
||
314 |
context delete |
|
315 |
begin |
|
316 |
||
317 |
lemma n2_type1: |
|
318 |
"\<lbrakk>t1 \<in> Um h; t2 \<in> B h\<rbrakk> \<Longrightarrow> n2 t1 a t2 \<in> T (Suc h)" |
|
319 |
apply(cases h rule: Bp.cases) |
|
320 |
apply auto[2] |
|
321 |
apply(erule exE bexE conjE imageE | simp | erule disjE)+ |
|
322 |
done |
|
323 |
||
324 |
lemma n2_type2: |
|
325 |
"\<lbrakk>t1 \<in> B h ; t2 \<in> Um h \<rbrakk> \<Longrightarrow> n2 t1 a t2 \<in> T (Suc h)" |
|
326 |
apply(cases h rule: Bp.cases) |
|
327 |
apply auto[2] |
|
328 |
apply(erule exE bexE conjE imageE | simp | erule disjE)+ |
|
329 |
done |
|
330 |
||
331 |
lemma n2_type3: |
|
332 |
"\<lbrakk>t1 \<in> T h ; t2 \<in> T h \<rbrakk> \<Longrightarrow> n2 t1 a t2 \<in> T (Suc h)" |
|
333 |
apply(cases h rule: Bp.cases) |
|
334 |
apply auto[2] |
|
335 |
apply(erule exE bexE conjE imageE | simp | erule disjE)+ |
|
336 |
done |
|
337 |
||
338 |
lemma del_minNoneN0: "\<lbrakk>t \<in> B h; del_min t = None\<rbrakk> \<Longrightarrow> t = N0" |
|
339 |
by (cases t) (auto split: option.splits) |
|
340 |
||
341 |
lemma del_minNoneN1 : "\<lbrakk>t \<in> U h; del_min t = None\<rbrakk> \<Longrightarrow> t = N1 N0" |
|
342 |
by (cases h) (auto simp: del_minNoneN0 split: option.splits) |
|
343 |
||
344 |
lemma del_min_type: |
|
345 |
"t \<in> B h \<Longrightarrow> del_min t = Some (a, t') \<Longrightarrow> t' \<in> T h" |
|
346 |
"t \<in> U h \<Longrightarrow> del_min t = Some (a, t') \<Longrightarrow> t' \<in> Um h" |
|
347 |
proof (induction h arbitrary: t a t') |
|
348 |
case (Suc h) |
|
349 |
{ case 1 |
|
350 |
then obtain t1 a t2 where [simp]: "t = N2 t1 a t2" and |
|
351 |
t12: "t1 \<in> T h" "t2 \<in> T h" "t1 \<in> B h \<or> t2 \<in> B h" |
|
352 |
by auto |
|
353 |
show ?case |
|
354 |
proof (cases "del_min t1") |
|
355 |
case None |
|
356 |
show ?thesis |
|
357 |
proof cases |
|
358 |
assume "t1 \<in> B h" |
|
359 |
with del_minNoneN0[OF this None] 1 show ?thesis by(auto) |
|
360 |
next |
|
361 |
assume "t1 \<notin> B h" |
|
362 |
thus ?thesis using 1 None by (auto) |
|
363 |
qed |
|
364 |
next |
|
365 |
case [simp]: (Some bt') |
|
366 |
obtain b t1' where [simp]: "bt' = (b,t1')" by fastforce |
|
367 |
show ?thesis |
|
368 |
proof cases |
|
369 |
assume "t1 \<in> B h" |
|
370 |
from Suc.IH(1)[OF this] 1 have "t1' \<in> T h" by simp |
|
371 |
from n2_type3[OF this t12(2)] 1 show ?thesis by auto |
|
372 |
next |
|
373 |
assume "t1 \<notin> B h" |
|
374 |
hence t1: "t1 \<in> U h" and t2: "t2 \<in> B h" using t12 by auto |
|
375 |
from Suc.IH(2)[OF t1] have "t1' \<in> Um h" by simp |
|
376 |
from n2_type1[OF this t2] 1 show ?thesis by auto |
|
377 |
qed |
|
378 |
qed |
|
379 |
} |
|
380 |
{ case 2 |
|
381 |
then obtain t1 where [simp]: "t = N1 t1" and t1: "t1 \<in> B h" by auto |
|
382 |
show ?case |
|
383 |
proof (cases "del_min t1") |
|
384 |
case None |
|
385 |
with del_minNoneN0[OF t1 None] 2 show ?thesis by(auto) |
|
386 |
next |
|
387 |
case [simp]: (Some bt') |
|
388 |
obtain b t1' where [simp]: "bt' = (b,t1')" by fastforce |
|
389 |
from Suc.IH(1)[OF t1] have "t1' \<in> T h" by simp |
|
390 |
thus ?thesis using 2 by auto |
|
391 |
qed |
|
392 |
} |
|
393 |
qed auto |
|
394 |
||
395 |
lemma del_type: |
|
396 |
"t \<in> B h \<Longrightarrow> del x t \<in> T h" |
|
397 |
"t \<in> U h \<Longrightarrow> del x t \<in> Um h" |
|
398 |
proof (induction h arbitrary: x t) |
|
399 |
case (Suc h) |
|
400 |
{ case 1 |
|
401 |
then obtain l a r where [simp]: "t = N2 l a r" and |
|
402 |
lr: "l \<in> T h" "r \<in> T h" "l \<in> B h \<or> r \<in> B h" by auto |
|
67040 | 403 |
have ?case if "x < a" |
404 |
proof cases |
|
405 |
assume "l \<in> B h" |
|
406 |
from n2_type3[OF Suc.IH(1)[OF this] lr(2)] |
|
67406 | 407 |
show ?thesis using \<open>x<a\<close> by(simp) |
67040 | 408 |
next |
409 |
assume "l \<notin> B h" |
|
410 |
hence "l \<in> U h" "r \<in> B h" using lr by auto |
|
411 |
from n2_type1[OF Suc.IH(2)[OF this(1)] this(2)] |
|
67406 | 412 |
show ?thesis using \<open>x<a\<close> by(simp) |
67040 | 413 |
qed |
414 |
moreover |
|
415 |
have ?case if "x > a" |
|
416 |
proof cases |
|
417 |
assume "r \<in> B h" |
|
418 |
from n2_type3[OF lr(1) Suc.IH(1)[OF this]] |
|
67406 | 419 |
show ?thesis using \<open>x>a\<close> by(simp) |
67040 | 420 |
next |
421 |
assume "r \<notin> B h" |
|
422 |
hence "l \<in> B h" "r \<in> U h" using lr by auto |
|
423 |
from n2_type2[OF this(1) Suc.IH(2)[OF this(2)]] |
|
67406 | 424 |
show ?thesis using \<open>x>a\<close> by(simp) |
67040 | 425 |
qed |
426 |
moreover |
|
427 |
have ?case if [simp]: "x=a" |
|
428 |
proof (cases "del_min r") |
|
429 |
case None |
|
430 |
show ?thesis |
|
61784 | 431 |
proof cases |
432 |
assume "r \<in> B h" |
|
67040 | 433 |
with del_minNoneN0[OF this None] lr show ?thesis by(simp) |
61784 | 434 |
next |
435 |
assume "r \<notin> B h" |
|
67040 | 436 |
hence "r \<in> U h" using lr by auto |
437 |
with del_minNoneN1[OF this None] lr(3) show ?thesis by (simp) |
|
61784 | 438 |
qed |
67040 | 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 |
|
61784 | 447 |
next |
67040 | 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 |
|
61784 | 452 |
qed |
67040 | 453 |
qed |
454 |
ultimately show ?case by auto |
|
61784 | 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 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
486 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
487 |
subsection \<open>Height-Size Relation\<close> |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
488 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
489 |
text \<open>By Daniel St\"uwe\<close> |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
490 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
491 |
fun fib_tree :: "nat \<Rightarrow> unit bro" where |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
492 |
"fib_tree 0 = N0" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
493 |
| "fib_tree (Suc 0) = N2 N0 () N0" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
494 |
| "fib_tree (Suc(Suc h)) = N2 (fib_tree (h+1)) () (N1 (fib_tree h))" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
495 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
496 |
fun fib' :: "nat \<Rightarrow> nat" where |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
497 |
"fib' 0 = 0" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
498 |
| "fib' (Suc 0) = 1" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
499 |
| "fib' (Suc(Suc h)) = 1 + fib' (Suc h) + fib' h" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
500 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
501 |
fun size :: "'a bro \<Rightarrow> nat" where |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
502 |
"size N0 = 0" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
503 |
| "size (N1 t) = size t" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
504 |
| "size (N2 t1 _ t2) = 1 + size t1 + size t2" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
505 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
506 |
lemma fib_tree_B: "fib_tree h \<in> B h" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
507 |
by (induction h rule: fib_tree.induct) auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
508 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
509 |
declare [[names_short]] |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
510 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
511 |
lemma size_fib': "size (fib_tree h) = fib' h" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
512 |
by (induction h rule: fib_tree.induct) auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
513 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
514 |
lemma fibfib: "fib' h + 1 = fib (Suc(Suc h))" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
515 |
by (induction h rule: fib_tree.induct) auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
516 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
517 |
lemma B_N2_cases[consumes 1]: |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
518 |
assumes "N2 t1 a t2 \<in> B (Suc n)" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
519 |
obtains |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
520 |
(BB) "t1 \<in> B n" and "t2 \<in> B n" | |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
521 |
(UB) "t1 \<in> U n" and "t2 \<in> B n" | |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
522 |
(BU) "t1 \<in> B n" and "t2 \<in> U n" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
523 |
using assms by auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
524 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
525 |
lemma size_bounded: "t \<in> B h \<Longrightarrow> size t \<ge> size (fib_tree h)" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
526 |
unfolding size_fib' proof (induction h arbitrary: t rule: fib'.induct) |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
527 |
case (3 h t') |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
528 |
note main = 3 |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
529 |
then obtain t1 a t2 where t': "t' = N2 t1 a t2" by auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
530 |
with main have "N2 t1 a t2 \<in> B (Suc (Suc h))" by auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
531 |
thus ?case proof (cases rule: B_N2_cases) |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
532 |
case BB |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
533 |
then obtain x y z where t2: "t2 = N2 x y z \<or> t2 = N2 z y x" "x \<in> B h" by auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
534 |
show ?thesis unfolding t' using main(1)[OF BB(1)] main(2)[OF t2(2)] t2(1) by auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
535 |
next |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
536 |
case UB |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
537 |
then obtain t11 where t1: "t1 = N1 t11" "t11 \<in> B h" by auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
538 |
show ?thesis unfolding t' t1(1) using main(2)[OF t1(2)] main(1)[OF UB(2)] by simp |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
539 |
next |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
540 |
case BU |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
541 |
then obtain t22 where t2: "t2 = N1 t22" "t22 \<in> B h" by auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
542 |
show ?thesis unfolding t' t2(1) using main(2)[OF t2(2)] main(1)[OF BU(1)] by simp |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
543 |
qed |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
544 |
qed auto |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
545 |
|
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
546 |
theorem "t \<in> B h \<Longrightarrow> fib (h + 2) \<le> size t + 1" |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
547 |
using size_bounded |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
548 |
by (simp add: size_fib' fibfib[symmetric] del: fib.simps) |
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
549 |
|
61784 | 550 |
end |