author | immler |
Sun, 27 Oct 2019 21:51:14 -0400 | |
changeset 71035 | 6fe5a0e1fa8e |
parent 70755 | 3fb16bed5d6c |
permissions | -rw-r--r-- |
61793 | 1 |
(* |
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
2 |
Author: Tobias Nipkow, Daniel Stüwe |
61793 | 3 |
*) |
4 |
||
62130 | 5 |
section \<open>AA Tree Implementation of Sets\<close> |
61793 | 6 |
|
7 |
theory AA_Set |
|
8 |
imports |
|
9 |
Isin2 |
|
10 |
Cmp |
|
11 |
begin |
|
12 |
||
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
13 |
type_synonym 'a aa_tree = "('a*nat) tree" |
61793 | 14 |
|
68431 | 15 |
definition empty :: "'a aa_tree" where |
16 |
"empty = Leaf" |
|
17 |
||
61793 | 18 |
fun lvl :: "'a aa_tree \<Rightarrow> nat" where |
19 |
"lvl Leaf = 0" | |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
20 |
"lvl (Node _ (_, lv) _) = lv" |
62496 | 21 |
|
61793 | 22 |
fun invar :: "'a aa_tree \<Rightarrow> bool" where |
23 |
"invar Leaf = True" | |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
24 |
"invar (Node l (a, h) r) = |
61793 | 25 |
(invar l \<and> invar r \<and> |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
26 |
h = lvl l + 1 \<and> (h = lvl r + 1 \<or> (\<exists>lr b rr. r = Node lr (b,h) rr \<and> h = lvl rr + 1)))" |
62496 | 27 |
|
61793 | 28 |
fun skew :: "'a aa_tree \<Rightarrow> 'a aa_tree" where |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
29 |
"skew (Node (Node t1 (b, lvb) t2) (a, lva) t3) = |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
30 |
(if lva = lvb then Node t1 (b, lvb) (Node t2 (a, lva) t3) else Node (Node t1 (b, lvb) t2) (a, lva) t3)" | |
61793 | 31 |
"skew t = t" |
32 |
||
33 |
fun split :: "'a aa_tree \<Rightarrow> 'a aa_tree" where |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
34 |
"split (Node t1 (a, lva) (Node t2 (b, lvb) (Node t3 (c, lvc) t4))) = |
67369 | 35 |
(if lva = lvb \<and> lvb = lvc \<comment> \<open>\<open>lva = lvc\<close> suffices\<close> |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
36 |
then Node (Node t1 (a,lva) t2) (b,lva+1) (Node t3 (c, lva) t4) |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
37 |
else Node t1 (a,lva) (Node t2 (b,lvb) (Node t3 (c,lvc) t4)))" | |
61793 | 38 |
"split t = t" |
39 |
||
40 |
hide_const (open) insert |
|
41 |
||
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
42 |
fun insert :: "'a::linorder \<Rightarrow> 'a aa_tree \<Rightarrow> 'a aa_tree" where |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
43 |
"insert x Leaf = Node Leaf (x, 1) Leaf" | |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
44 |
"insert x (Node t1 (a,lv) t2) = |
61793 | 45 |
(case cmp x a of |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
46 |
LT \<Rightarrow> split (skew (Node (insert x t1) (a,lv) t2)) | |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
47 |
GT \<Rightarrow> split (skew (Node t1 (a,lv) (insert x t2))) | |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
48 |
EQ \<Rightarrow> Node t1 (x, lv) t2)" |
61793 | 49 |
|
50 |
fun sngl :: "'a aa_tree \<Rightarrow> bool" where |
|
51 |
"sngl Leaf = False" | |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
52 |
"sngl (Node _ _ Leaf) = True" | |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
53 |
"sngl (Node _ (_, lva) (Node _ (_, lvb) _)) = (lva > lvb)" |
61793 | 54 |
|
55 |
definition adjust :: "'a aa_tree \<Rightarrow> 'a aa_tree" where |
|
56 |
"adjust t = |
|
57 |
(case t of |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
58 |
Node l (x,lv) r \<Rightarrow> |
61793 | 59 |
(if lvl l >= lv-1 \<and> lvl r >= lv-1 then t else |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
60 |
if lvl r < lv-1 \<and> sngl l then skew (Node l (x,lv-1) r) else |
61793 | 61 |
if lvl r < lv-1 |
62 |
then case l of |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
63 |
Node t1 (a,lva) (Node t2 (b,lvb) t3) |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
64 |
\<Rightarrow> Node (Node t1 (a,lva) t2) (b,lvb+1) (Node t3 (x,lv-1) r) |
61793 | 65 |
else |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
66 |
if lvl r < lv then split (Node l (x,lv-1) r) |
61793 | 67 |
else |
68 |
case r of |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
69 |
Node t1 (b,lvb) t4 \<Rightarrow> |
61793 | 70 |
(case t1 of |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
71 |
Node t2 (a,lva) t3 |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
72 |
\<Rightarrow> Node (Node l (x,lv-1) t2) (a,lva+1) |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
73 |
(split (Node t3 (b, if sngl t1 then lva else lva+1) t4)))))" |
62496 | 74 |
|
69597 | 75 |
text\<open>In the paper, the last case of \<^const>\<open>adjust\<close> is expressed with the help of an |
62496 | 76 |
incorrect auxiliary function \texttt{nlvl}. |
77 |
||
69505 | 78 |
Function \<open>split_max\<close> below is called \texttt{dellrg} in the paper. |
62496 | 79 |
The latter is incorrect for two reasons: \texttt{dellrg} is meant to delete the largest |
80 |
element but recurses on the left instead of the right subtree; the invariant |
|
67406 | 81 |
is not restored.\<close> |
62496 | 82 |
|
68023 | 83 |
fun split_max :: "'a aa_tree \<Rightarrow> 'a aa_tree * 'a" where |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
84 |
"split_max (Node l (a,lv) Leaf) = (l,a)" | |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
85 |
"split_max (Node l (a,lv) r) = (let (r',b) = split_max r in (adjust(Node l (a,lv) r'), b))" |
61793 | 86 |
|
63411
e051eea34990
got rid of class cmp; added height-size proofs by Daniel Stuewe
nipkow
parents:
62526
diff
changeset
|
87 |
fun delete :: "'a::linorder \<Rightarrow> 'a aa_tree \<Rightarrow> 'a aa_tree" where |
61793 | 88 |
"delete _ Leaf = Leaf" | |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
89 |
"delete x (Node l (a,lv) r) = |
61793 | 90 |
(case cmp x a of |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
91 |
LT \<Rightarrow> adjust (Node (delete x l) (a,lv) r) | |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
92 |
GT \<Rightarrow> adjust (Node l (a,lv) (delete x r)) | |
61793 | 93 |
EQ \<Rightarrow> (if l = Leaf then r |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
94 |
else let (l',b) = split_max l in adjust (Node l' (b,lv) r)))" |
61793 | 95 |
|
62496 | 96 |
fun pre_adjust where |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
97 |
"pre_adjust (Node l (a,lv) r) = (invar l \<and> invar r \<and> |
62496 | 98 |
((lv = lvl l + 1 \<and> (lv = lvl r + 1 \<or> lv = lvl r + 2 \<or> lv = lvl r \<and> sngl r)) \<or> |
99 |
(lv = lvl l + 2 \<and> (lv = lvl r + 1 \<or> lv = lvl r \<and> sngl r))))" |
|
100 |
||
101 |
declare pre_adjust.simps [simp del] |
|
102 |
||
103 |
subsection "Auxiliary Proofs" |
|
104 |
||
105 |
lemma split_case: "split t = (case t of |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
106 |
Node t1 (x,lvx) (Node t2 (y,lvy) (Node t3 (z,lvz) t4)) \<Rightarrow> |
62496 | 107 |
(if lvx = lvy \<and> lvy = lvz |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
108 |
then Node (Node t1 (x,lvx) t2) (y,lvx+1) (Node t3 (z,lvx) t4) |
62496 | 109 |
else t) |
110 |
| t \<Rightarrow> t)" |
|
111 |
by(auto split: tree.split) |
|
112 |
||
113 |
lemma skew_case: "skew t = (case t of |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
114 |
Node (Node t1 (y,lvy) t2) (x,lvx) t3 \<Rightarrow> |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
115 |
(if lvx = lvy then Node t1 (y, lvx) (Node t2 (x,lvx) t3) else t) |
62496 | 116 |
| t \<Rightarrow> t)" |
117 |
by(auto split: tree.split) |
|
118 |
||
119 |
lemma lvl_0_iff: "invar t \<Longrightarrow> lvl t = 0 \<longleftrightarrow> t = Leaf" |
|
120 |
by(cases t) auto |
|
121 |
||
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
122 |
lemma lvl_Suc_iff: "lvl t = Suc n \<longleftrightarrow> (\<exists> l a r. t = Node l (a,Suc n) r)" |
62496 | 123 |
by(cases t) auto |
124 |
||
125 |
lemma lvl_skew: "lvl (skew t) = lvl t" |
|
62526 | 126 |
by(cases t rule: skew.cases) auto |
62496 | 127 |
|
128 |
lemma lvl_split: "lvl (split t) = lvl t \<or> lvl (split t) = lvl t + 1 \<and> sngl (split t)" |
|
62526 | 129 |
by(cases t rule: split.cases) auto |
62496 | 130 |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
131 |
lemma invar_2Nodes:"invar (Node l (x,lv) (Node rl (rx, rlv) rr)) = |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
132 |
(invar l \<and> invar \<langle>rl, (rx, rlv), rr\<rangle> \<and> lv = Suc (lvl l) \<and> |
62496 | 133 |
(lv = Suc rlv \<or> rlv = lv \<and> lv = Suc (lvl rr)))" |
134 |
by simp |
|
135 |
||
136 |
lemma invar_NodeLeaf[simp]: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
137 |
"invar (Node l (x,lv) Leaf) = (invar l \<and> lv = Suc (lvl l) \<and> lv = Suc 0)" |
62496 | 138 |
by simp |
139 |
||
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
140 |
lemma sngl_if_invar: "invar (Node l (a, n) r) \<Longrightarrow> n = lvl r \<Longrightarrow> sngl r" |
62496 | 141 |
by(cases r rule: sngl.cases) clarsimp+ |
142 |
||
143 |
||
144 |
subsection "Invariance" |
|
145 |
||
146 |
subsubsection "Proofs for insert" |
|
147 |
||
148 |
lemma lvl_insert_aux: |
|
149 |
"lvl (insert x t) = lvl t \<or> lvl (insert x t) = lvl t + 1 \<and> sngl (insert x t)" |
|
150 |
apply(induction t) |
|
151 |
apply (auto simp: lvl_skew) |
|
152 |
apply (metis Suc_eq_plus1 lvl.simps(2) lvl_split lvl_skew)+ |
|
153 |
done |
|
154 |
||
155 |
lemma lvl_insert: obtains |
|
156 |
(Same) "lvl (insert x t) = lvl t" | |
|
157 |
(Incr) "lvl (insert x t) = lvl t + 1" "sngl (insert x t)" |
|
158 |
using lvl_insert_aux by blast |
|
159 |
||
160 |
lemma lvl_insert_sngl: "invar t \<Longrightarrow> sngl t \<Longrightarrow> lvl(insert x t) = lvl t" |
|
62526 | 161 |
proof (induction t rule: insert.induct) |
68413 | 162 |
case (2 x t1 a lv t2) |
62496 | 163 |
consider (LT) "x < a" | (GT) "x > a" | (EQ) "x = a" |
164 |
using less_linear by blast |
|
165 |
thus ?case proof cases |
|
166 |
case LT |
|
167 |
thus ?thesis using 2 by (auto simp add: skew_case split_case split: tree.splits) |
|
168 |
next |
|
169 |
case GT |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
170 |
thus ?thesis using 2 |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
171 |
proof (cases t1 rule: tree2_cases) |
62496 | 172 |
case Node |
173 |
thus ?thesis using 2 GT |
|
174 |
apply (auto simp add: skew_case split_case split: tree.splits) |
|
175 |
by (metis less_not_refl2 lvl.simps(2) lvl_insert_aux n_not_Suc_n sngl.simps(3))+ |
|
176 |
qed (auto simp add: lvl_0_iff) |
|
177 |
qed simp |
|
178 |
qed simp |
|
179 |
||
180 |
lemma skew_invar: "invar t \<Longrightarrow> skew t = t" |
|
62526 | 181 |
by(cases t rule: skew.cases) auto |
62496 | 182 |
|
183 |
lemma split_invar: "invar t \<Longrightarrow> split t = t" |
|
62526 | 184 |
by(cases t rule: split.cases) clarsimp+ |
62496 | 185 |
|
186 |
lemma invar_NodeL: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
187 |
"\<lbrakk> invar(Node l (x, n) r); invar l'; lvl l' = lvl l \<rbrakk> \<Longrightarrow> invar(Node l' (x, n) r)" |
62496 | 188 |
by(auto) |
189 |
||
190 |
lemma invar_NodeR: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
191 |
"\<lbrakk> invar(Node l (x, n) r); n = lvl r + 1; invar r'; lvl r' = lvl r \<rbrakk> \<Longrightarrow> invar(Node l (x, n) r')" |
62496 | 192 |
by(auto) |
193 |
||
194 |
lemma invar_NodeR2: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
195 |
"\<lbrakk> invar(Node l (x, n) r); sngl r'; n = lvl r + 1; invar r'; lvl r' = n \<rbrakk> \<Longrightarrow> invar(Node l (x, n) r')" |
62496 | 196 |
by(cases r' rule: sngl.cases) clarsimp+ |
197 |
||
198 |
||
199 |
lemma lvl_insert_incr_iff: "(lvl(insert a t) = lvl t + 1) \<longleftrightarrow> |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
200 |
(\<exists>l x r. insert a t = Node l (x, lvl t + 1) r \<and> lvl l = lvl r)" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
201 |
apply(cases t rule: tree2_cases) |
62496 | 202 |
apply(auto simp add: skew_case split_case split: if_splits) |
203 |
apply(auto split: tree.splits if_splits) |
|
204 |
done |
|
205 |
||
206 |
lemma invar_insert: "invar t \<Longrightarrow> invar(insert a t)" |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
207 |
proof(induction t rule: tree2_induct) |
68413 | 208 |
case N: (Node l x n r) |
62496 | 209 |
hence il: "invar l" and ir: "invar r" by auto |
67040 | 210 |
note iil = N.IH(1)[OF il] |
211 |
note iir = N.IH(2)[OF ir] |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
212 |
let ?t = "Node l (x, n) r" |
62496 | 213 |
have "a < x \<or> a = x \<or> x < a" by auto |
214 |
moreover |
|
67040 | 215 |
have ?case if "a < x" |
216 |
proof (cases rule: lvl_insert[of a l]) |
|
217 |
case (Same) thus ?thesis |
|
218 |
using \<open>a<x\<close> invar_NodeL[OF N.prems iil Same] |
|
219 |
by (simp add: skew_invar split_invar del: invar.simps) |
|
220 |
next |
|
221 |
case (Incr) |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
222 |
then obtain t1 w t2 where ial[simp]: "insert a l = Node t1 (w, n) t2" |
67040 | 223 |
using N.prems by (auto simp: lvl_Suc_iff) |
224 |
have l12: "lvl t1 = lvl t2" |
|
225 |
by (metis Incr(1) ial lvl_insert_incr_iff tree.inject) |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
226 |
have "insert a ?t = split(skew(Node (insert a l) (x,n) r))" |
67040 | 227 |
by(simp add: \<open>a<x\<close>) |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
228 |
also have "skew(Node (insert a l) (x,n) r) = Node t1 (w,n) (Node t2 (x,n) r)" |
67040 | 229 |
by(simp) |
230 |
also have "invar(split \<dots>)" |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
231 |
proof (cases r rule: tree2_cases) |
67040 | 232 |
case Leaf |
233 |
hence "l = Leaf" using N.prems by(auto simp: lvl_0_iff) |
|
234 |
thus ?thesis using Leaf ial by simp |
|
62496 | 235 |
next |
68413 | 236 |
case [simp]: (Node t3 y m t4) |
67040 | 237 |
show ?thesis (*using N(3) iil l12 by(auto)*) |
238 |
proof cases |
|
239 |
assume "m = n" thus ?thesis using N(3) iil by(auto) |
|
62496 | 240 |
next |
67040 | 241 |
assume "m \<noteq> n" thus ?thesis using N(3) iil l12 by(auto) |
62496 | 242 |
qed |
243 |
qed |
|
67040 | 244 |
finally show ?thesis . |
245 |
qed |
|
62496 | 246 |
moreover |
67040 | 247 |
have ?case if "x < a" |
248 |
proof - |
|
62496 | 249 |
from \<open>invar ?t\<close> have "n = lvl r \<or> n = lvl r + 1" by auto |
67040 | 250 |
thus ?case |
62496 | 251 |
proof |
252 |
assume 0: "n = lvl r" |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
253 |
have "insert a ?t = split(skew(Node l (x, n) (insert a r)))" |
62496 | 254 |
using \<open>a>x\<close> by(auto) |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
255 |
also have "skew(Node l (x,n) (insert a r)) = Node l (x,n) (insert a r)" |
67040 | 256 |
using N.prems by(simp add: skew_case split: tree.split) |
62496 | 257 |
also have "invar(split \<dots>)" |
258 |
proof - |
|
259 |
from lvl_insert_sngl[OF ir sngl_if_invar[OF \<open>invar ?t\<close> 0], of a] |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
260 |
obtain t1 y t2 where iar: "insert a r = Node t1 (y,n) t2" |
67040 | 261 |
using N.prems 0 by (auto simp: lvl_Suc_iff) |
262 |
from N.prems iar 0 iir |
|
62496 | 263 |
show ?thesis by (auto simp: split_case split: tree.splits) |
264 |
qed |
|
265 |
finally show ?thesis . |
|
266 |
next |
|
267 |
assume 1: "n = lvl r + 1" |
|
268 |
hence "sngl ?t" by(cases r) auto |
|
269 |
show ?thesis |
|
270 |
proof (cases rule: lvl_insert[of a r]) |
|
271 |
case (Same) |
|
67040 | 272 |
show ?thesis using \<open>x<a\<close> il ir invar_NodeR[OF N.prems 1 iir Same] |
62496 | 273 |
by (auto simp add: skew_invar split_invar) |
274 |
next |
|
275 |
case (Incr) |
|
67406 | 276 |
thus ?thesis using invar_NodeR2[OF \<open>invar ?t\<close> Incr(2) 1 iir] 1 \<open>x < a\<close> |
62496 | 277 |
by (auto simp add: skew_invar split_invar split: if_splits) |
278 |
qed |
|
279 |
qed |
|
67040 | 280 |
qed |
281 |
moreover |
|
282 |
have "a = x \<Longrightarrow> ?case" using N.prems by auto |
|
62496 | 283 |
ultimately show ?case by blast |
284 |
qed simp |
|
285 |
||
286 |
||
287 |
subsubsection "Proofs for delete" |
|
288 |
||
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
289 |
lemma invarL: "ASSUMPTION(invar \<langle>l, (a, lv), r\<rangle>) \<Longrightarrow> invar l" |
62496 | 290 |
by(simp add: ASSUMPTION_def) |
291 |
||
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
292 |
lemma invarR: "ASSUMPTION(invar \<langle>l, (a,lv), r\<rangle>) \<Longrightarrow> invar r" |
62496 | 293 |
by(simp add: ASSUMPTION_def) |
294 |
||
295 |
lemma sngl_NodeI: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
296 |
"sngl (Node l (a,lv) r) \<Longrightarrow> sngl (Node l' (a', lv) r)" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
297 |
by(cases r rule: tree2_cases) (simp_all) |
62496 | 298 |
|
299 |
||
300 |
declare invarL[simp] invarR[simp] |
|
301 |
||
302 |
lemma pre_cases: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
303 |
assumes "pre_adjust (Node l (x,lv) r)" |
62496 | 304 |
obtains |
305 |
(tSngl) "invar l \<and> invar r \<and> |
|
306 |
lv = Suc (lvl r) \<and> lvl l = lvl r" | |
|
307 |
(tDouble) "invar l \<and> invar r \<and> |
|
308 |
lv = lvl r \<and> Suc (lvl l) = lvl r \<and> sngl r " | |
|
309 |
(rDown) "invar l \<and> invar r \<and> |
|
310 |
lv = Suc (Suc (lvl r)) \<and> lv = Suc (lvl l)" | |
|
311 |
(lDown_tSngl) "invar l \<and> invar r \<and> |
|
312 |
lv = Suc (lvl r) \<and> lv = Suc (Suc (lvl l))" | |
|
313 |
(lDown_tDouble) "invar l \<and> invar r \<and> |
|
314 |
lv = lvl r \<and> lv = Suc (Suc (lvl l)) \<and> sngl r" |
|
315 |
using assms unfolding pre_adjust.simps |
|
316 |
by auto |
|
317 |
||
318 |
declare invar.simps(2)[simp del] invar_2Nodes[simp add] |
|
319 |
||
320 |
lemma invar_adjust: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
321 |
assumes pre: "pre_adjust (Node l (a,lv) r)" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
322 |
shows "invar(adjust (Node l (a,lv) r))" |
62496 | 323 |
using pre proof (cases rule: pre_cases) |
324 |
case (tDouble) thus ?thesis unfolding adjust_def by (cases r) (auto simp: invar.simps(2)) |
|
325 |
next |
|
326 |
case (rDown) |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
327 |
from rDown obtain llv ll la lr where l: "l = Node ll (la, llv) lr" by (cases l) auto |
62496 | 328 |
from rDown show ?thesis unfolding adjust_def by (auto simp: l invar.simps(2) split: tree.splits) |
329 |
next |
|
330 |
case (lDown_tDouble) |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
331 |
from lDown_tDouble obtain rlv rr ra rl where r: "r = Node rl (ra, rlv) rr" by (cases r) auto |
62496 | 332 |
from lDown_tDouble and r obtain rrlv rrr rra rrl where |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
333 |
rr :"rr = Node rrr (rra, rrlv) rrl" by (cases rr) auto |
62496 | 334 |
from lDown_tDouble show ?thesis unfolding adjust_def r rr |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
335 |
apply (cases rl rule: tree2_cases) apply (auto simp add: invar.simps(2) split!: if_split) |
62496 | 336 |
using lDown_tDouble by (auto simp: split_case lvl_0_iff elim:lvl.elims split: tree.split) |
337 |
qed (auto simp: split_case invar.simps(2) adjust_def split: tree.splits) |
|
338 |
||
339 |
lemma lvl_adjust: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
340 |
assumes "pre_adjust (Node l (a,lv) r)" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
341 |
shows "lv = lvl (adjust(Node l (a,lv) r)) \<or> lv = lvl (adjust(Node l (a,lv) r)) + 1" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
342 |
using assms(1) |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
343 |
proof(cases rule: pre_cases) |
62496 | 344 |
case lDown_tSngl thus ?thesis |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
345 |
using lvl_split[of "\<langle>l, (a, lvl r), r\<rangle>"] by (auto simp: adjust_def) |
62496 | 346 |
next |
347 |
case lDown_tDouble thus ?thesis |
|
348 |
by (auto simp: adjust_def invar.simps(2) split: tree.split) |
|
349 |
qed (auto simp: adjust_def split: tree.splits) |
|
350 |
||
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
351 |
lemma sngl_adjust: assumes "pre_adjust (Node l (a,lv) r)" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
352 |
"sngl \<langle>l, (a, lv), r\<rangle>" "lv = lvl (adjust \<langle>l, (a, lv), r\<rangle>)" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
353 |
shows "sngl (adjust \<langle>l, (a, lv), r\<rangle>)" |
62496 | 354 |
using assms proof (cases rule: pre_cases) |
355 |
case rDown |
|
356 |
thus ?thesis using assms(2,3) unfolding adjust_def |
|
357 |
by (auto simp add: skew_case) (auto split: tree.split) |
|
358 |
qed (auto simp: adjust_def skew_case split_case split: tree.split) |
|
359 |
||
360 |
definition "post_del t t' == |
|
361 |
invar t' \<and> |
|
362 |
(lvl t' = lvl t \<or> lvl t' + 1 = lvl t) \<and> |
|
363 |
(lvl t' = lvl t \<and> sngl t \<longrightarrow> sngl t')" |
|
364 |
||
365 |
lemma pre_adj_if_postR: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
366 |
"invar\<langle>lv, (l, a), r\<rangle> \<Longrightarrow> post_del r r' \<Longrightarrow> pre_adjust \<langle>lv, (l, a), r'\<rangle>" |
62496 | 367 |
by(cases "sngl r") |
368 |
(auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims) |
|
369 |
||
370 |
lemma pre_adj_if_postL: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
371 |
"invar\<langle>l, (a, lv), r\<rangle> \<Longrightarrow> post_del l l' \<Longrightarrow> pre_adjust \<langle>l', (b, lv), r\<rangle>" |
62496 | 372 |
by(cases "sngl r") |
373 |
(auto simp: pre_adjust.simps post_del_def invar.simps(2) elim: sngl.elims) |
|
374 |
||
375 |
lemma post_del_adjL: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
376 |
"\<lbrakk> invar\<langle>l, (a, lv), r\<rangle>; pre_adjust \<langle>l', (b, lv), r\<rangle> \<rbrakk> |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
377 |
\<Longrightarrow> post_del \<langle>l, (a, lv), r\<rangle> (adjust \<langle>l', (b, lv), r\<rangle>)" |
62496 | 378 |
unfolding post_del_def |
379 |
by (metis invar_adjust lvl_adjust sngl_NodeI sngl_adjust lvl.simps(2)) |
|
380 |
||
381 |
lemma post_del_adjR: |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
382 |
assumes "invar\<langle>l, (a,lv), r\<rangle>" "pre_adjust \<langle>l, (a,lv), r'\<rangle>" "post_del r r'" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
383 |
shows "post_del \<langle>l, (a,lv), r\<rangle> (adjust \<langle>l, (a,lv), r'\<rangle>)" |
62496 | 384 |
proof(unfold post_del_def, safe del: disjCI) |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
385 |
let ?t = "\<langle>l, (a,lv), r\<rangle>" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
386 |
let ?t' = "adjust \<langle>l, (a,lv), r'\<rangle>" |
62496 | 387 |
show "invar ?t'" by(rule invar_adjust[OF assms(2)]) |
388 |
show "lvl ?t' = lvl ?t \<or> lvl ?t' + 1 = lvl ?t" |
|
389 |
using lvl_adjust[OF assms(2)] by auto |
|
390 |
show "sngl ?t'" if as: "lvl ?t' = lvl ?t" "sngl ?t" |
|
391 |
proof - |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
392 |
have s: "sngl \<langle>l, (a,lv), r'\<rangle>" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
393 |
proof(cases r' rule: tree2_cases) |
62496 | 394 |
case Leaf thus ?thesis by simp |
395 |
next |
|
396 |
case Node thus ?thesis using as(2) assms(1,3) |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
397 |
by (cases r rule: tree2_cases) (auto simp: post_del_def) |
62496 | 398 |
qed |
399 |
show ?thesis using as(1) sngl_adjust[OF assms(2) s] by simp |
|
400 |
qed |
|
401 |
qed |
|
402 |
||
403 |
declare prod.splits[split] |
|
404 |
||
68023 | 405 |
theorem post_split_max: |
406 |
"\<lbrakk> invar t; (t', x) = split_max t; t \<noteq> Leaf \<rbrakk> \<Longrightarrow> post_del t t'" |
|
407 |
proof (induction t arbitrary: t' rule: split_max.induct) |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
408 |
case (2 l a lv rl bl rr) |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
409 |
let ?r = "\<langle>rl, bl, rr\<rangle>" |
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
410 |
let ?t = "\<langle>l, (a, lv), ?r\<rangle>" |
68023 | 411 |
from "2.prems"(2) obtain r' where r': "(r', x) = split_max ?r" |
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
412 |
and [simp]: "t' = adjust \<langle>l, (a, lv), r'\<rangle>" by auto |
62496 | 413 |
from "2.IH"[OF _ r'] \<open>invar ?t\<close> have post: "post_del ?r r'" by simp |
414 |
note preR = pre_adj_if_postR[OF \<open>invar ?t\<close> post] |
|
415 |
show ?case by (simp add: post_del_adjR[OF "2.prems"(1) preR post]) |
|
416 |
qed (auto simp: post_del_def) |
|
417 |
||
418 |
theorem post_delete: "invar t \<Longrightarrow> post_del t (delete x t)" |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
419 |
proof (induction t rule: tree2_induct) |
68413 | 420 |
case (Node l a lv r) |
62496 | 421 |
|
422 |
let ?l' = "delete x l" and ?r' = "delete x r" |
|
70755
3fb16bed5d6c
replaced new type ('a,'b) tree by old type ('a*'b) tree.
nipkow
parents:
69597
diff
changeset
|
423 |
let ?t = "Node l (a,lv) r" let ?t' = "delete x ?t" |
62496 | 424 |
|
425 |
from Node.prems have inv_l: "invar l" and inv_r: "invar r" by (auto) |
|
426 |
||
427 |
note post_l' = Node.IH(1)[OF inv_l] |
|
428 |
note preL = pre_adj_if_postL[OF Node.prems post_l'] |
|
429 |
||
430 |
note post_r' = Node.IH(2)[OF inv_r] |
|
431 |
note preR = pre_adj_if_postR[OF Node.prems post_r'] |
|
432 |
||
433 |
show ?case |
|
434 |
proof (cases rule: linorder_cases[of x a]) |
|
435 |
case less |
|
436 |
thus ?thesis using Node.prems by (simp add: post_del_adjL preL) |
|
437 |
next |
|
438 |
case greater |
|
439 |
thus ?thesis using Node.prems by (simp add: post_del_adjR preR post_r') |
|
440 |
next |
|
441 |
case equal |
|
442 |
show ?thesis |
|
443 |
proof cases |
|
444 |
assume "l = Leaf" thus ?thesis using equal Node.prems |
|
445 |
by(auto simp: post_del_def invar.simps(2)) |
|
446 |
next |
|
447 |
assume "l \<noteq> Leaf" thus ?thesis using equal |
|
68023 | 448 |
by simp (metis Node.prems inv_l post_del_adjL post_split_max pre_adj_if_postL) |
62496 | 449 |
qed |
450 |
qed |
|
451 |
qed (simp add: post_del_def) |
|
452 |
||
453 |
declare invar_2Nodes[simp del] |
|
454 |
||
61793 | 455 |
|
456 |
subsection "Functional Correctness" |
|
457 |
||
62496 | 458 |
|
61793 | 459 |
subsubsection "Proofs for insert" |
460 |
||
461 |
lemma inorder_split: "inorder(split t) = inorder t" |
|
462 |
by(cases t rule: split.cases) (auto) |
|
463 |
||
464 |
lemma inorder_skew: "inorder(skew t) = inorder t" |
|
465 |
by(cases t rule: skew.cases) (auto) |
|
466 |
||
467 |
lemma inorder_insert: |
|
468 |
"sorted(inorder t) \<Longrightarrow> inorder(insert x t) = ins_list x (inorder t)" |
|
469 |
by(induction t) (auto simp: ins_list_simps inorder_split inorder_skew) |
|
470 |
||
62496 | 471 |
|
61793 | 472 |
subsubsection "Proofs for delete" |
473 |
||
62496 | 474 |
lemma inorder_adjust: "t \<noteq> Leaf \<Longrightarrow> pre_adjust t \<Longrightarrow> inorder(adjust t) = inorder t" |
62526 | 475 |
by(cases t) |
62496 | 476 |
(auto simp: adjust_def inorder_skew inorder_split invar.simps(2) pre_adjust.simps |
477 |
split: tree.splits) |
|
478 |
||
68023 | 479 |
lemma split_maxD: |
480 |
"\<lbrakk> split_max t = (t',x); t \<noteq> Leaf; invar t \<rbrakk> \<Longrightarrow> inorder t' @ [x] = inorder t" |
|
481 |
by(induction t arbitrary: t' rule: split_max.induct) |
|
482 |
(auto simp: sorted_lems inorder_adjust pre_adj_if_postR post_split_max split: prod.splits) |
|
61793 | 483 |
|
484 |
lemma inorder_delete: |
|
62496 | 485 |
"invar t \<Longrightarrow> sorted(inorder t) \<Longrightarrow> inorder(delete x t) = del_list x (inorder t)" |
61793 | 486 |
by(induction t) |
62496 | 487 |
(auto simp: del_list_simps inorder_adjust pre_adj_if_postL pre_adj_if_postR |
68023 | 488 |
post_split_max post_delete split_maxD split: prod.splits) |
61793 | 489 |
|
68440 | 490 |
interpretation S: Set_by_Ordered |
68431 | 491 |
where empty = empty and isin = isin and insert = insert and delete = delete |
62496 | 492 |
and inorder = inorder and inv = invar |
61793 | 493 |
proof (standard, goal_cases) |
68431 | 494 |
case 1 show ?case by (simp add: empty_def) |
61793 | 495 |
next |
67967 | 496 |
case 2 thus ?case by(simp add: isin_set_inorder) |
61793 | 497 |
next |
498 |
case 3 thus ?case by(simp add: inorder_insert) |
|
499 |
next |
|
500 |
case 4 thus ?case by(simp add: inorder_delete) |
|
62496 | 501 |
next |
68431 | 502 |
case 5 thus ?case by(simp add: empty_def) |
62496 | 503 |
next |
504 |
case 6 thus ?case by(simp add: invar_insert) |
|
505 |
next |
|
506 |
case 7 thus ?case using post_delete by(auto simp: post_del_def) |
|
507 |
qed |
|
61793 | 508 |
|
62390 | 509 |
end |