| author | nipkow |
| Thu, 05 Nov 2015 18:38:08 +0100 | |
| changeset 61588 | 1d2907d0ed73 |
| parent 61581 | 00d9682e8dd7 |
| child 61640 | 44c9198f210c |
| permissions | -rw-r--r-- |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
1 |
(* Author: Tobias Nipkow *) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
2 |
|
| 61513 | 3 |
section \<open>A 2-3 Tree Implementation of Sets\<close> |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
4 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
5 |
theory Tree23_Set |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
6 |
imports |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
7 |
Tree23 |
| 61581 | 8 |
Cmp |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
9 |
Set_by_Ordered |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
10 |
begin |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
11 |
|
| 61581 | 12 |
fun isin :: "'a::cmp tree23 \<Rightarrow> 'a \<Rightarrow> bool" where |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
13 |
"isin Leaf x = False" | |
| 61581 | 14 |
"isin (Node2 l a r) x = |
15 |
(case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x)" | |
|
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
16 |
"isin (Node3 l a m b r) x = |
| 61581 | 17 |
(case cmp x a of LT \<Rightarrow> isin l x | EQ \<Rightarrow> True | GT \<Rightarrow> (case cmp x b of |
18 |
LT \<Rightarrow> isin m x | EQ \<Rightarrow> True | GT \<Rightarrow> isin r x))" |
|
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
19 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
20 |
datatype 'a up\<^sub>i = T\<^sub>i "'a tree23" | Up\<^sub>i "'a tree23" 'a "'a tree23" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
21 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
22 |
fun tree\<^sub>i :: "'a up\<^sub>i \<Rightarrow> 'a tree23" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
23 |
"tree\<^sub>i (T\<^sub>i t) = t" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
24 |
"tree\<^sub>i (Up\<^sub>i l p r) = Node2 l p r" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
25 |
|
| 61581 | 26 |
fun ins :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>i" where |
| 61534 | 27 |
"ins x Leaf = Up\<^sub>i Leaf x Leaf" | |
28 |
"ins x (Node2 l a r) = |
|
| 61581 | 29 |
(case cmp x a of |
30 |
LT \<Rightarrow> (case ins x l of |
|
31 |
T\<^sub>i l' => T\<^sub>i (Node2 l' a r) |
|
32 |
| Up\<^sub>i l1 b l2 => T\<^sub>i (Node3 l1 b l2 a r)) | |
|
33 |
EQ \<Rightarrow> T\<^sub>i (Node2 l x r) | |
|
34 |
GT \<Rightarrow> (case ins x r of |
|
35 |
T\<^sub>i r' => T\<^sub>i (Node2 l a r') |
|
36 |
| Up\<^sub>i r1 b r2 => T\<^sub>i (Node3 l a r1 b r2)))" | |
|
| 61534 | 37 |
"ins x (Node3 l a m b r) = |
| 61581 | 38 |
(case cmp x a of |
39 |
LT \<Rightarrow> (case ins x l of |
|
40 |
T\<^sub>i l' => T\<^sub>i (Node3 l' a m b r) |
|
41 |
| Up\<^sub>i l1 c l2 => Up\<^sub>i (Node2 l1 c l2) a (Node2 m b r)) | |
|
42 |
EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) | |
|
43 |
GT \<Rightarrow> (case cmp x b of |
|
44 |
GT \<Rightarrow> (case ins x r of |
|
45 |
T\<^sub>i r' => T\<^sub>i (Node3 l a m b r') |
|
46 |
| Up\<^sub>i r1 c r2 => Up\<^sub>i (Node2 l a m) b (Node2 r1 c r2)) | |
|
47 |
EQ \<Rightarrow> T\<^sub>i (Node3 l a m b r) | |
|
48 |
LT \<Rightarrow> (case ins x m of |
|
49 |
T\<^sub>i m' => T\<^sub>i (Node3 l a m' b r) |
|
50 |
| Up\<^sub>i m1 c m2 => Up\<^sub>i (Node2 l a m1) c (Node2 m2 b r))))" |
|
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
51 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
52 |
hide_const insert |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
53 |
|
| 61581 | 54 |
definition insert :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where |
| 61534 | 55 |
"insert x t = tree\<^sub>i(ins x t)" |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
56 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
57 |
datatype 'a up\<^sub>d = T\<^sub>d "'a tree23" | Up\<^sub>d "'a tree23" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
58 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
59 |
fun tree\<^sub>d :: "'a up\<^sub>d \<Rightarrow> 'a tree23" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
60 |
"tree\<^sub>d (T\<^sub>d x) = x" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
61 |
"tree\<^sub>d (Up\<^sub>d x) = x" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
62 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
63 |
(* Variation: return None to signal no-change *) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
64 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
65 |
fun node21 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
66 |
"node21 (T\<^sub>d t1) a t2 = T\<^sub>d(Node2 t1 a t2)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
67 |
"node21 (Up\<^sub>d t1) a (Node2 t2 b t3) = Up\<^sub>d(Node3 t1 a t2 b t3)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
68 |
"node21 (Up\<^sub>d t1) a (Node3 t2 b t3 c t4) = T\<^sub>d(Node2 (Node2 t1 a t2) b (Node2 t3 c t4))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
69 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
70 |
fun node22 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
71 |
"node22 t1 a (T\<^sub>d t2) = T\<^sub>d(Node2 t1 a t2)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
72 |
"node22 (Node2 t1 b t2) a (Up\<^sub>d t3) = Up\<^sub>d(Node3 t1 b t2 a t3)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
73 |
"node22 (Node3 t1 b t2 c t3) a (Up\<^sub>d t4) = T\<^sub>d(Node2 (Node2 t1 b t2) c (Node2 t3 a t4))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
74 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
75 |
fun node31 :: "'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
76 |
"node31 (T\<^sub>d t1) a t2 b t3 = T\<^sub>d(Node3 t1 a t2 b t3)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
77 |
"node31 (Up\<^sub>d t1) a (Node2 t2 b t3) c t4 = T\<^sub>d(Node2 (Node3 t1 a t2 b t3) c t4)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
78 |
"node31 (Up\<^sub>d t1) a (Node3 t2 b t3 c t4) d t5 = T\<^sub>d(Node3 (Node2 t1 a t2) b (Node2 t3 c t4) d t5)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
79 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
80 |
fun node32 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
81 |
"node32 t1 a (T\<^sub>d t2) b t3 = T\<^sub>d(Node3 t1 a t2 b t3)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
82 |
"node32 t1 a (Up\<^sub>d t2) b (Node2 t3 c t4) = T\<^sub>d(Node2 t1 a (Node3 t2 b t3 c t4))" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
83 |
"node32 t1 a (Up\<^sub>d t2) b (Node3 t3 c t4 d t5) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
84 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
85 |
fun node33 :: "'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a tree23 \<Rightarrow> 'a \<Rightarrow> 'a up\<^sub>d \<Rightarrow> 'a up\<^sub>d" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
86 |
"node33 l a m b (T\<^sub>d r) = T\<^sub>d(Node3 l a m b r)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
87 |
"node33 t1 a (Node2 t2 b t3) c (Up\<^sub>d t4) = T\<^sub>d(Node2 t1 a (Node3 t2 b t3 c t4))" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
88 |
"node33 t1 a (Node3 t2 b t3 c t4) d (Up\<^sub>d t5) = T\<^sub>d(Node3 t1 a (Node2 t2 b t3) c (Node2 t4 d t5))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
89 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
90 |
fun del_min :: "'a tree23 \<Rightarrow> 'a * 'a up\<^sub>d" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
91 |
"del_min (Node2 Leaf a Leaf) = (a, Up\<^sub>d Leaf)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
92 |
"del_min (Node3 Leaf a Leaf b Leaf) = (a, T\<^sub>d(Node2 Leaf b Leaf))" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
93 |
"del_min (Node2 l a r) = (let (x,l') = del_min l in (x, node21 l' a r))" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
94 |
"del_min (Node3 l a m b r) = (let (x,l') = del_min l in (x, node31 l' a m b r))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
95 |
|
| 61581 | 96 |
fun del :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a up\<^sub>d" |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
97 |
where |
| 61534 | 98 |
"del x Leaf = T\<^sub>d Leaf" | |
99 |
"del x (Node2 Leaf a Leaf) = (if x = a then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf a Leaf))" | |
|
100 |
"del x (Node3 Leaf a Leaf b Leaf) = T\<^sub>d(if x = a then Node2 Leaf b Leaf |
|
101 |
else if x = b then Node2 Leaf a Leaf else Node3 Leaf a Leaf b Leaf)" | |
|
| 61581 | 102 |
"del x (Node2 l a r) = (case cmp x a of |
103 |
LT \<Rightarrow> node21 (del x l) a r | |
|
104 |
GT \<Rightarrow> node22 l a (del x r) | |
|
105 |
EQ \<Rightarrow> let (a',t) = del_min r in node22 l a' t)" | |
|
106 |
"del x (Node3 l a m b r) = (case cmp x a of |
|
107 |
LT \<Rightarrow> node31 (del x l) a m b r | |
|
108 |
EQ \<Rightarrow> let (a',m') = del_min m in node32 l a' m' b r | |
|
109 |
GT \<Rightarrow> (case cmp x b of |
|
110 |
LT \<Rightarrow> node32 l a (del x m) b r | |
|
111 |
EQ \<Rightarrow> let (b',r') = del_min r in node33 l a m b' r' | |
|
112 |
GT \<Rightarrow> node33 l a m b (del x r)))" |
|
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
113 |
|
| 61581 | 114 |
definition delete :: "'a::cmp \<Rightarrow> 'a tree23 \<Rightarrow> 'a tree23" where |
| 61534 | 115 |
"delete x t = tree\<^sub>d(del x t)" |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
116 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
117 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
118 |
subsection "Functional Correctness" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
119 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
120 |
subsubsection "Proofs for isin" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
121 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
122 |
lemma "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))" |
| 61581 | 123 |
by (induction t) (auto simp: elems_simps1 ball_Un) |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
124 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
125 |
lemma isin_set: "sorted(inorder t) \<Longrightarrow> isin t x = (x \<in> elems (inorder t))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
126 |
by (induction t) (auto simp: elems_simps2) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
127 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
128 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
129 |
subsubsection "Proofs for insert" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
130 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
131 |
lemma inorder_ins: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
132 |
"sorted(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(ins x t)) = ins_list x (inorder t)" |
| 61581 | 133 |
by(induction t) (auto simp: ins_list_simps split: up\<^sub>i.splits) |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
134 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
135 |
lemma inorder_insert: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
136 |
"sorted(inorder t) \<Longrightarrow> inorder(insert a t) = ins_list a (inorder t)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
137 |
by(simp add: insert_def inorder_ins) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
138 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
139 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
140 |
subsubsection "Proofs for delete" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
141 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
142 |
lemma inorder_node21: "height r > 0 \<Longrightarrow> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
143 |
inorder (tree\<^sub>d (node21 l' a r)) = inorder (tree\<^sub>d l') @ a # inorder r" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
144 |
by(induct l' a r rule: node21.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
145 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
146 |
lemma inorder_node22: "height l > 0 \<Longrightarrow> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
147 |
inorder (tree\<^sub>d (node22 l a r')) = inorder l @ a # inorder (tree\<^sub>d r')" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
148 |
by(induct l a r' rule: node22.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
149 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
150 |
lemma inorder_node31: "height m > 0 \<Longrightarrow> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
151 |
inorder (tree\<^sub>d (node31 l' a m b r)) = inorder (tree\<^sub>d l') @ a # inorder m @ b # inorder r" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
152 |
by(induct l' a m b r rule: node31.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
153 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
154 |
lemma inorder_node32: "height r > 0 \<Longrightarrow> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
155 |
inorder (tree\<^sub>d (node32 l a m' b r)) = inorder l @ a # inorder (tree\<^sub>d m') @ b # inorder r" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
156 |
by(induct l a m' b r rule: node32.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
157 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
158 |
lemma inorder_node33: "height m > 0 \<Longrightarrow> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
159 |
inorder (tree\<^sub>d (node33 l a m b r')) = inorder l @ a # inorder m @ b # inorder (tree\<^sub>d r')" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
160 |
by(induct l a m b r' rule: node33.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
161 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
162 |
lemmas inorder_nodes = inorder_node21 inorder_node22 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
163 |
inorder_node31 inorder_node32 inorder_node33 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
164 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
165 |
lemma del_minD: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
166 |
"del_min t = (x,t') \<Longrightarrow> bal t \<Longrightarrow> height t > 0 \<Longrightarrow> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
167 |
x # inorder(tree\<^sub>d t') = inorder t" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
168 |
by(induction t arbitrary: t' rule: del_min.induct) |
| 61513 | 169 |
(auto simp: inorder_nodes split: prod.splits) |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
170 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
171 |
lemma inorder_del: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
172 |
inorder(tree\<^sub>d (del x t)) = del_list x (inorder t)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
173 |
by(induction t rule: del.induct) |
| 61513 | 174 |
(auto simp: del_list_simps inorder_nodes del_minD split: prod.splits) |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
175 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
176 |
lemma inorder_delete: "\<lbrakk> bal t ; sorted(inorder t) \<rbrakk> \<Longrightarrow> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
177 |
inorder(delete x t) = del_list x (inorder t)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
178 |
by(simp add: delete_def inorder_del) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
179 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
180 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
181 |
subsection \<open>Balancedness\<close> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
182 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
183 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
184 |
subsubsection "Proofs for insert" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
185 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
186 |
text{* First a standard proof that @{const ins} preserves @{const bal}. *}
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
187 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
188 |
instantiation up\<^sub>i :: (type)height |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
189 |
begin |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
190 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
191 |
fun height_up\<^sub>i :: "'a up\<^sub>i \<Rightarrow> nat" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
192 |
"height (T\<^sub>i t) = height t" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
193 |
"height (Up\<^sub>i l a r) = height l" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
194 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
195 |
instance .. |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
196 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
197 |
end |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
198 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
199 |
lemma bal_ins: "bal t \<Longrightarrow> bal (tree\<^sub>i(ins a t)) \<and> height(ins a t) = height t" |
| 61581 | 200 |
by (induct t) (auto split: up\<^sub>i.split) (* 15 secs in 2015 *) |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
201 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
202 |
text{* Now an alternative proof (by Brian Huffman) that runs faster because
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
203 |
two properties (balance and height) are combined in one predicate. *} |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
204 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
205 |
inductive full :: "nat \<Rightarrow> 'a tree23 \<Rightarrow> bool" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
206 |
"full 0 Leaf" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
207 |
"\<lbrakk>full n l; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node2 l p r)" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
208 |
"\<lbrakk>full n l; full n m; full n r\<rbrakk> \<Longrightarrow> full (Suc n) (Node3 l p m q r)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
209 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
210 |
inductive_cases full_elims: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
211 |
"full n Leaf" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
212 |
"full n (Node2 l p r)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
213 |
"full n (Node3 l p m q r)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
214 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
215 |
inductive_cases full_0_elim: "full 0 t" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
216 |
inductive_cases full_Suc_elim: "full (Suc n) t" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
217 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
218 |
lemma full_0_iff [simp]: "full 0 t \<longleftrightarrow> t = Leaf" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
219 |
by (auto elim: full_0_elim intro: full.intros) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
220 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
221 |
lemma full_Leaf_iff [simp]: "full n Leaf \<longleftrightarrow> n = 0" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
222 |
by (auto elim: full_elims intro: full.intros) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
223 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
224 |
lemma full_Suc_Node2_iff [simp]: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
225 |
"full (Suc n) (Node2 l p r) \<longleftrightarrow> full n l \<and> full n r" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
226 |
by (auto elim: full_elims intro: full.intros) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
227 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
228 |
lemma full_Suc_Node3_iff [simp]: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
229 |
"full (Suc n) (Node3 l p m q r) \<longleftrightarrow> full n l \<and> full n m \<and> full n r" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
230 |
by (auto elim: full_elims intro: full.intros) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
231 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
232 |
lemma full_imp_height: "full n t \<Longrightarrow> height t = n" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
233 |
by (induct set: full, simp_all) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
234 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
235 |
lemma full_imp_bal: "full n t \<Longrightarrow> bal t" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
236 |
by (induct set: full, auto dest: full_imp_height) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
237 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
238 |
lemma bal_imp_full: "bal t \<Longrightarrow> full (height t) t" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
239 |
by (induct t, simp_all) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
240 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
241 |
lemma bal_iff_full: "bal t \<longleftrightarrow> (\<exists>n. full n t)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
242 |
by (auto elim!: bal_imp_full full_imp_bal) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
243 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
244 |
text {* The @{const "insert"} function either preserves the height of the
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
245 |
tree, or increases it by one. The constructor returned by the @{term
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
246 |
"insert"} function determines which: A return value of the form @{term
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
247 |
"T\<^sub>i t"} indicates that the height will be the same. A value of the |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
248 |
form @{term "Up\<^sub>i l p r"} indicates an increase in height. *}
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
249 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
250 |
fun full\<^sub>i :: "nat \<Rightarrow> 'a up\<^sub>i \<Rightarrow> bool" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
251 |
"full\<^sub>i n (T\<^sub>i t) \<longleftrightarrow> full n t" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
252 |
"full\<^sub>i n (Up\<^sub>i l p r) \<longleftrightarrow> full n l \<and> full n r" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
253 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
254 |
lemma full\<^sub>i_ins: "full n t \<Longrightarrow> full\<^sub>i n (ins a t)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
255 |
by (induct rule: full.induct) (auto split: up\<^sub>i.split) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
256 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
257 |
text {* The @{const insert} operation preserves balance. *}
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
258 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
259 |
lemma bal_insert: "bal t \<Longrightarrow> bal (insert a t)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
260 |
unfolding bal_iff_full insert_def |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
261 |
apply (erule exE) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
262 |
apply (drule full\<^sub>i_ins [of _ _ a]) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
263 |
apply (cases "ins a t") |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
264 |
apply (auto intro: full.intros) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
265 |
done |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
266 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
267 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
268 |
subsection "Proofs for delete" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
269 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
270 |
instantiation up\<^sub>d :: (type)height |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
271 |
begin |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
272 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
273 |
fun height_up\<^sub>d :: "'a up\<^sub>d \<Rightarrow> nat" where |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
274 |
"height (T\<^sub>d t) = height t" | |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
275 |
"height (Up\<^sub>d t) = height t + 1" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
276 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
277 |
instance .. |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
278 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
279 |
end |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
280 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
281 |
lemma bal_tree\<^sub>d_node21: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
282 |
"\<lbrakk>bal r; bal (tree\<^sub>d l'); height r = height l' \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d (node21 l' a r))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
283 |
by(induct l' a r rule: node21.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
284 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
285 |
lemma bal_tree\<^sub>d_node22: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
286 |
"\<lbrakk>bal(tree\<^sub>d r'); bal l; height r' = height l \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d (node22 l a r'))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
287 |
by(induct l a r' rule: node22.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
288 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
289 |
lemma bal_tree\<^sub>d_node31: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
290 |
"\<lbrakk> bal (tree\<^sub>d l'); bal m; bal r; height l' = height r; height m = height r \<rbrakk> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
291 |
\<Longrightarrow> bal (tree\<^sub>d (node31 l' a m b r))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
292 |
by(induct l' a m b r rule: node31.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
293 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
294 |
lemma bal_tree\<^sub>d_node32: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
295 |
"\<lbrakk> bal l; bal (tree\<^sub>d m'); bal r; height l = height r; height m' = height r \<rbrakk> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
296 |
\<Longrightarrow> bal (tree\<^sub>d (node32 l a m' b r))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
297 |
by(induct l a m' b r rule: node32.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
298 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
299 |
lemma bal_tree\<^sub>d_node33: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
300 |
"\<lbrakk> bal l; bal m; bal(tree\<^sub>d r'); height l = height r'; height m = height r' \<rbrakk> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
301 |
\<Longrightarrow> bal (tree\<^sub>d (node33 l a m b r'))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
302 |
by(induct l a m b r' rule: node33.induct) auto |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
303 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
304 |
lemmas bals = bal_tree\<^sub>d_node21 bal_tree\<^sub>d_node22 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
305 |
bal_tree\<^sub>d_node31 bal_tree\<^sub>d_node32 bal_tree\<^sub>d_node33 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
306 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
307 |
lemma height'_node21: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
308 |
"height r > 0 \<Longrightarrow> height(node21 l' a r) = max (height l') (height r) + 1" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
309 |
by(induct l' a r rule: node21.induct)(simp_all) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
310 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
311 |
lemma height'_node22: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
312 |
"height l > 0 \<Longrightarrow> height(node22 l a r') = max (height l) (height r') + 1" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
313 |
by(induct l a r' rule: node22.induct)(simp_all) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
314 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
315 |
lemma height'_node31: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
316 |
"height m > 0 \<Longrightarrow> height(node31 l a m b r) = |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
317 |
max (height l) (max (height m) (height r)) + 1" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
318 |
by(induct l a m b r rule: node31.induct)(simp_all add: max_def) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
319 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
320 |
lemma height'_node32: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
321 |
"height r > 0 \<Longrightarrow> height(node32 l a m b r) = |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
322 |
max (height l) (max (height m) (height r)) + 1" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
323 |
by(induct l a m b r rule: node32.induct)(simp_all add: max_def) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
324 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
325 |
lemma height'_node33: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
326 |
"height m > 0 \<Longrightarrow> height(node33 l a m b r) = |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
327 |
max (height l) (max (height m) (height r)) + 1" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
328 |
by(induct l a m b r rule: node33.induct)(simp_all add: max_def) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
329 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
330 |
lemmas heights = height'_node21 height'_node22 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
331 |
height'_node31 height'_node32 height'_node33 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
332 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
333 |
lemma height_del_min: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
334 |
"del_min t = (x, t') \<Longrightarrow> height t > 0 \<Longrightarrow> bal t \<Longrightarrow> height t' = height t" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
335 |
by(induct t arbitrary: x t' rule: del_min.induct) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
336 |
(auto simp: heights split: prod.splits) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
337 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
338 |
lemma height_del: "bal t \<Longrightarrow> height(del x t) = height t" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
339 |
by(induction x t rule: del.induct) |
| 61513 | 340 |
(auto simp: heights max_def height_del_min split: prod.splits) |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
341 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
342 |
lemma bal_del_min: |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
343 |
"\<lbrakk> del_min t = (x, t'); bal t; height t > 0 \<rbrakk> \<Longrightarrow> bal (tree\<^sub>d t')" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
344 |
by(induct t arbitrary: x t' rule: del_min.induct) |
| 61513 | 345 |
(auto simp: heights height_del_min bals split: prod.splits) |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
346 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
347 |
lemma bal_tree\<^sub>d_del: "bal t \<Longrightarrow> bal(tree\<^sub>d(del x t))" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
348 |
by(induction x t rule: del.induct) |
| 61513 | 349 |
(auto simp: bals bal_del_min height_del height_del_min split: prod.splits) |
350 |
||
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
351 |
corollary bal_delete: "bal t \<Longrightarrow> bal(delete x t)" |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
352 |
by(simp add: delete_def bal_tree\<^sub>d_del) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
353 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
354 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
355 |
subsection \<open>Overall Correctness\<close> |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
356 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
357 |
interpretation Set_by_Ordered |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
358 |
where empty = Leaf and isin = isin and insert = insert and delete = delete |
| 61588 | 359 |
and inorder = inorder and inv = bal |
|
61469
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
360 |
proof (standard, goal_cases) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
361 |
case 2 thus ?case by(simp add: isin_set) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
362 |
next |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
363 |
case 3 thus ?case by(simp add: inorder_insert) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
364 |
next |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
365 |
case 4 thus ?case by(simp add: inorder_delete) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
366 |
next |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
367 |
case 6 thus ?case by(simp add: bal_insert) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
368 |
next |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
369 |
case 7 thus ?case by(simp add: bal_delete) |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
370 |
qed simp+ |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
371 |
|
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
372 |
end |