author | nipkow |
Sun, 18 Oct 2015 17:25:13 +0200 | |
changeset 61469 | cd82b1023932 |
child 61513 | c0126c001b3d |
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 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
3 |
section \<open>2-3 Tree Implementation of Maps\<close> |
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_Map |
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_Set |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
8 |
Map_by_Ordered |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
9 |
begin |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
10 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
11 |
fun lookup :: "('a::linorder * 'b) tree23 \<Rightarrow> 'a \<Rightarrow> 'b option" where |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
12 |
"lookup Leaf x = None" | |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
13 |
"lookup (Node2 l (a,b) r) x = |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
14 |
(if x < a then lookup l x else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
15 |
if a < x then lookup r x else Some b)" | |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
16 |
"lookup (Node3 l (a1,b1) m (a2,b2) r) x = |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
17 |
(if x < a1 then lookup l x else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
18 |
if x = a1 then Some b1 else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
19 |
if x < a2 then lookup m x else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
20 |
if x = a2 then Some b2 |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
21 |
else lookup r x)" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
22 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
23 |
fun upd :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>i" where |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
24 |
"upd a b Leaf = Up\<^sub>i Leaf (a,b) Leaf" | |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
25 |
"upd a b (Node2 l xy r) = |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
26 |
(if a < fst xy then |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
27 |
(case upd a b l of |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
28 |
T\<^sub>i l' => T\<^sub>i (Node2 l' xy r) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
29 |
| Up\<^sub>i l1 q l2 => T\<^sub>i (Node3 l1 q l2 xy r)) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
30 |
else if a = fst xy then T\<^sub>i (Node2 l (a,b) r) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
31 |
else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
32 |
(case upd a b r of |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
33 |
T\<^sub>i r' => T\<^sub>i (Node2 l xy r') |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
34 |
| Up\<^sub>i r1 q r2 => T\<^sub>i (Node3 l xy r1 q r2)))" | |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
35 |
"upd a b (Node3 l xy1 m xy2 r) = |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
36 |
(if a < fst xy1 then |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
37 |
(case upd a b l of |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
38 |
T\<^sub>i l' => T\<^sub>i (Node3 l' xy1 m xy2 r) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
39 |
| Up\<^sub>i l1 q l2 => Up\<^sub>i (Node2 l1 q l2) xy1 (Node2 m xy2 r)) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
40 |
else if a = fst xy1 then T\<^sub>i (Node3 l (a,b) m xy2 r) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
41 |
else if a < fst xy2 then |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
42 |
(case upd a b m of |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
43 |
T\<^sub>i m' => T\<^sub>i (Node3 l xy1 m' xy2 r) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
44 |
| Up\<^sub>i m1 q m2 => Up\<^sub>i (Node2 l xy1 m1) q (Node2 m2 xy2 r)) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
45 |
else if a = fst xy2 then T\<^sub>i (Node3 l xy1 m (a,b) r) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
46 |
else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
47 |
(case upd a b r of |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
48 |
T\<^sub>i r' => T\<^sub>i (Node3 l xy1 m xy2 r') |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
49 |
| Up\<^sub>i r1 q r2 => Up\<^sub>i (Node2 l xy1 m) xy2 (Node2 r1 q r2)))" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
50 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
51 |
definition update :: "'a::linorder \<Rightarrow> 'b \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
52 |
"update a b t = tree\<^sub>i(upd a b t)" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
53 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
54 |
fun del :: "'a::linorder \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) up\<^sub>d" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
55 |
where |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
56 |
"del k Leaf = T\<^sub>d Leaf" | |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
57 |
"del k (Node2 Leaf p Leaf) = (if k=fst p then Up\<^sub>d Leaf else T\<^sub>d(Node2 Leaf p Leaf))" | |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
58 |
"del k (Node3 Leaf p Leaf q Leaf) = T\<^sub>d(if k=fst p then Node2 Leaf q Leaf |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
59 |
else if k=fst q then Node2 Leaf p Leaf else Node3 Leaf p Leaf q Leaf)" | |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
60 |
"del k (Node2 l a r) = (if k<fst a then node21 (del k l) a r else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
61 |
if k > fst a then node22 l a (del k r) else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
62 |
let (a',t) = del_min r in node22 l a' t)" | |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
63 |
"del k (Node3 l a m b r) = (if k<fst a then node31 (del k l) a m b r else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
64 |
if k = fst a then let (a',m') = del_min m in node32 l a' m' b r else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
65 |
if k < fst b then node32 l a (del k m) b r else |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
66 |
if k = fst b then let (b',r') = del_min r in 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
|
67 |
else node33 l a m b (del k r))" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
68 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
69 |
definition delete :: "'a::linorder \<Rightarrow> ('a*'b) tree23 \<Rightarrow> ('a*'b) tree23" where |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
70 |
"delete k t = tree\<^sub>d(del k t)" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
71 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
72 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
73 |
subsection "Proofs for Lookup" |
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 |
lemma lookup: "sorted1(inorder t) \<Longrightarrow> lookup t x = map_of (inorder t) x" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
76 |
by (induction t) (auto simp: map_of_simps split: option.split) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
77 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
78 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
79 |
subsection "Proofs for Update" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
80 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
81 |
text {* Balanced trees *} |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
82 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
83 |
text{* First a standard proof that @{const upd} preserves @{const bal}. *} |
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 |
lemma bal_upd: "bal t \<Longrightarrow> bal (tree\<^sub>i(upd a b t)) \<and> height(upd a b t) = height t" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
86 |
by (induct t) (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
|
87 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
91 |
lemma full\<^sub>i_ins: "full n t \<Longrightarrow> full\<^sub>i n (upd a b t)" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
92 |
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
|
93 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
94 |
text {* The @{const update} operation preserves balance. *} |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
95 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
96 |
lemma bal_update: "bal t \<Longrightarrow> bal (update a b t)" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
97 |
unfolding bal_iff_full update_def |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
98 |
apply (erule exE) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
99 |
apply (drule full\<^sub>i_ins [of _ _ a b]) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
100 |
apply (cases "upd a b t") |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
101 |
apply (auto intro: full.intros) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
102 |
done |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
103 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
104 |
text {* Functional correctness of @{const "update"}. *} |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
105 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
106 |
lemma inorder_upd: |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
107 |
"sorted1(inorder t) \<Longrightarrow> inorder(tree\<^sub>i(upd a b t)) = upd_list a b (inorder t)" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
108 |
by(induction t) (auto simp: upd_list_simps split: up\<^sub>i.splits) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
109 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
110 |
lemma inorder_update: |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
111 |
"sorted1(inorder t) \<Longrightarrow> inorder(update a b t) = upd_list a b (inorder t)" |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
112 |
by(simp add: update_def inorder_upd) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
113 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
114 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
115 |
subsection "Proofs for Deletion" |
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 |
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
|
118 |
by(induction x t rule: del.induct) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
119 |
(auto simp add: heights max_def height_del_min split: prod.split) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
120 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
121 |
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
|
122 |
by(induction x t rule: del.induct) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
123 |
(auto simp: bals bal_del_min height_del height_del_min split: prod.split) |
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 |
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
|
126 |
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
|
127 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
128 |
lemma inorder_del: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow> |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
129 |
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
|
130 |
by(induction t rule: del.induct) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
131 |
(auto simp: del_list_simps inorder_nodes del_minD split: prod.splits) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
132 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
133 |
lemma inorder_delete: "\<lbrakk> bal t ; sorted1(inorder t) \<rbrakk> \<Longrightarrow> |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
134 |
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
|
135 |
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
|
136 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
137 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
138 |
subsection \<open>Overall Correctness\<close> |
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 |
interpretation T23_Map: Map_by_Ordered |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
141 |
where empty = Leaf and lookup = lookup and update = update and delete = delete |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
142 |
and inorder = inorder and wf = bal |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
143 |
proof (standard, goal_cases) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
144 |
case 2 thus ?case by(simp add: lookup) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
145 |
next |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
146 |
case 3 thus ?case by(simp add: inorder_update) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
147 |
next |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
148 |
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
|
149 |
next |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
150 |
case 6 thus ?case by(simp add: bal_update) |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
151 |
next |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
152 |
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
|
153 |
qed simp+ |
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
154 |
|
cd82b1023932
added 2-3 trees (simpler and more complete than the version in ex/Tree23)
nipkow
parents:
diff
changeset
|
155 |
end |