author | nipkow |
Mon, 23 Sep 2019 17:15:29 +0200 | |
changeset 70747 | 548420d389ea |
parent 66516 | 97c2d3846e10 |
child 70751 | fd9614c98dd6 |
permissions | -rw-r--r-- |
63829 | 1 |
(* Author: Tobias Nipkow *) |
63643 | 2 |
|
63829 | 3 |
section \<open>Creating Balanced Trees\<close> |
63643 | 4 |
|
63829 | 5 |
theory Balance |
63643 | 6 |
imports |
66510 | 7 |
"HOL-Library.Tree_Real" |
63643 | 8 |
begin |
9 |
||
64444 | 10 |
fun bal :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a tree * 'a list" where |
11 |
"bal n xs = (if n=0 then (Leaf,xs) else |
|
63643 | 12 |
(let m = n div 2; |
64444 | 13 |
(l, ys) = bal m xs; |
14 |
(r, zs) = bal (n-1-m) (tl ys) |
|
63643 | 15 |
in (Node l (hd ys) r, zs)))" |
16 |
||
17 |
declare bal.simps[simp del] |
|
18 |
||
64444 | 19 |
definition bal_list :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a tree" where |
20 |
"bal_list n xs = fst (bal n xs)" |
|
21 |
||
63829 | 22 |
definition balance_list :: "'a list \<Rightarrow> 'a tree" where |
64444 | 23 |
"balance_list xs = bal_list (length xs) xs" |
24 |
||
25 |
definition bal_tree :: "nat \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where |
|
26 |
"bal_tree n t = bal_list n (inorder t)" |
|
63829 | 27 |
|
28 |
definition balance_tree :: "'a tree \<Rightarrow> 'a tree" where |
|
64444 | 29 |
"balance_tree t = bal_tree (size t) t" |
63829 | 30 |
|
63843 | 31 |
lemma bal_simps: |
64444 | 32 |
"bal 0 xs = (Leaf, xs)" |
63843 | 33 |
"n > 0 \<Longrightarrow> |
64444 | 34 |
bal n xs = |
63843 | 35 |
(let m = n div 2; |
64444 | 36 |
(l, ys) = bal m xs; |
37 |
(r, zs) = bal (n-1-m) (tl ys) |
|
63843 | 38 |
in (Node l (hd ys) r, zs))" |
39 |
by(simp_all add: bal.simps) |
|
63643 | 40 |
|
41 |
lemma bal_inorder: |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
42 |
"\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk> |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
43 |
\<Longrightarrow> inorder t = take n xs \<and> zs = drop n xs" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
44 |
proof(induction n xs arbitrary: t zs rule: bal.induct) |
64444 | 45 |
case (1 n xs) show ?case |
63643 | 46 |
proof cases |
63843 | 47 |
assume "n = 0" thus ?thesis using 1 by (simp add: bal_simps) |
63643 | 48 |
next |
49 |
assume [arith]: "n \<noteq> 0" |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
50 |
let ?m = "n div 2" let ?m' = "n - 1 - ?m" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
51 |
from "1.prems"(2) obtain l r ys where |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
52 |
b1: "bal ?m xs = (l,ys)" and |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
53 |
b2: "bal ?m' (tl ys) = (r,zs)" and |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
54 |
t: "t = \<langle>l, hd ys, r\<rangle>" |
63843 | 55 |
by(auto simp: Let_def bal_simps split: prod.splits) |
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
56 |
have IH1: "inorder l = take ?m xs \<and> ys = drop ?m xs" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
57 |
using b1 "1.prems"(1) by(intro "1.IH"(1)) auto |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
58 |
have IH2: "inorder r = take ?m' (tl ys) \<and> zs = drop ?m' (tl ys)" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
59 |
using b1 b2 IH1 "1.prems"(1) by(intro "1.IH"(2)) auto |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
60 |
have "drop (n div 2) xs \<noteq> []" using "1.prems"(1) by simp |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
61 |
hence "hd (drop ?m xs) # take ?m' (tl (drop ?m xs)) = take (?m' + 1) (drop ?m xs)" |
63643 | 62 |
by (metis Suc_eq_plus1 take_Suc) |
63 |
hence *: "inorder t = take n xs" using t IH1 IH2 |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
64 |
using take_add[of ?m "?m'+1" xs] by(simp) |
63643 | 65 |
have "n - n div 2 + n div 2 = n" by simp |
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
66 |
hence "zs = drop n xs" using IH1 IH2 by (simp add: drop_Suc[symmetric]) |
63643 | 67 |
thus ?thesis using * by blast |
68 |
qed |
|
69 |
qed |
|
70 |
||
64444 | 71 |
corollary inorder_bal_list[simp]: |
72 |
"n \<le> length xs \<Longrightarrow> inorder(bal_list n xs) = take n xs" |
|
73 |
unfolding bal_list_def by (metis bal_inorder eq_fst_iff) |
|
74 |
||
75 |
corollary inorder_balance_list[simp]: "inorder(balance_list xs) = xs" |
|
76 |
by(simp add: balance_list_def) |
|
77 |
||
78 |
corollary inorder_bal_tree: |
|
79 |
"n \<le> size t \<Longrightarrow> inorder(bal_tree n t) = take n (inorder t)" |
|
80 |
by(simp add: bal_tree_def) |
|
63643 | 81 |
|
64018 | 82 |
corollary inorder_balance_tree[simp]: "inorder(balance_tree t) = inorder t" |
64444 | 83 |
by(simp add: balance_tree_def inorder_bal_tree) |
84 |
||
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
85 |
|
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
86 |
text\<open>The size lemmas below do not require the precondition @{prop"n \<le> length xs"} |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
87 |
(or @{prop"n \<le> size t"}) that they come with. They could take advantage of the fact |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
88 |
that @{term "bal xs n"} yields a result even if @{prop "n > length xs"}. |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
89 |
In that case the result will contain one or more occurrences of @{term "hd []"}. |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
90 |
However, this is counter-intuitive and does not reflect the execution |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
91 |
in an eager functional language.\<close> |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
92 |
|
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
93 |
lemma size_bal: "\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk> \<Longrightarrow> size t = n \<and> length zs = length xs - n" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
94 |
by (metis bal_inorder length_drop length_inorder length_take min.absorb2) |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
95 |
|
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
96 |
corollary size_bal_list[simp]: "n \<le> length xs \<Longrightarrow> size(bal_list n xs) = n" |
64444 | 97 |
unfolding bal_list_def by (metis prod.collapse size_bal) |
64018 | 98 |
|
99 |
corollary size_balance_list[simp]: "size(balance_list xs) = length xs" |
|
64444 | 100 |
by (simp add: balance_list_def) |
101 |
||
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
102 |
corollary size_bal_tree[simp]: "n \<le> size t \<Longrightarrow> size(bal_tree n t) = n" |
64444 | 103 |
by(simp add: bal_tree_def) |
64018 | 104 |
|
105 |
corollary size_balance_tree[simp]: "size(balance_tree t) = size t" |
|
64444 | 106 |
by(simp add: balance_tree_def) |
64018 | 107 |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
108 |
lemma pre_rec2: "\<lbrakk> n \<le> length xs; bal (n div 2) xs = (l, ys) \<rbrakk> |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
109 |
\<Longrightarrow> (n - 1 - n div 2) \<le> length(tl ys)" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
110 |
using size_bal[of "n div 2" xs l ys] by simp |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
111 |
|
64018 | 112 |
lemma min_height_bal: |
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
113 |
"\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk> \<Longrightarrow> min_height t = nat(\<lfloor>log 2 (n + 1)\<rfloor>)" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
114 |
proof(induction n xs arbitrary: t zs rule: bal.induct) |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
115 |
case (1 n xs) |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
116 |
show ?case |
63643 | 117 |
proof cases |
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
118 |
assume "n = 0" thus ?thesis using "1.prems"(2) by (simp add: bal_simps) |
63643 | 119 |
next |
120 |
assume [arith]: "n \<noteq> 0" |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
121 |
from "1.prems" obtain l r ys where |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
122 |
b1: "bal (n div 2) xs = (l,ys)" and |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
123 |
b2: "bal (n - 1 - n div 2) (tl ys) = (r,zs)" and |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
124 |
t: "t = \<langle>l, hd ys, r\<rangle>" |
63843 | 125 |
by(auto simp: bal_simps Let_def split: prod.splits) |
64018 | 126 |
let ?log1 = "nat (floor(log 2 (n div 2 + 1)))" |
127 |
let ?log2 = "nat (floor(log 2 (n - 1 - n div 2 + 1)))" |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
128 |
have IH1: "min_height l = ?log1" using "1.IH"(1) b1 "1.prems"(1) by simp |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
129 |
have IH2: "min_height r = ?log2" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
130 |
using "1.prems"(1) size_bal[OF _ b1] size_bal[OF _ b2] b1 b2 by(intro "1.IH"(2)) auto |
64018 | 131 |
have "(n+1) div 2 \<ge> 1" by arith |
132 |
hence 0: "log 2 ((n+1) div 2) \<ge> 0" by simp |
|
133 |
have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
134 |
hence le: "?log2 \<le> ?log1" by(simp add: nat_mono floor_mono) |
64018 | 135 |
have "min_height t = min ?log1 ?log2 + 1" by (simp add: t IH1 IH2) |
136 |
also have "\<dots> = ?log2 + 1" using le by (simp add: min_absorb2) |
|
137 |
also have "n - 1 - n div 2 + 1 = (n+1) div 2" by linarith |
|
138 |
also have "nat (floor(log 2 ((n+1) div 2))) + 1 |
|
139 |
= nat (floor(log 2 ((n+1) div 2) + 1))" |
|
140 |
using 0 by linarith |
|
141 |
also have "\<dots> = nat (floor(log 2 (n + 1)))" |
|
142 |
using floor_log2_div2[of "n+1"] by (simp add: log_mult) |
|
143 |
finally show ?thesis . |
|
144 |
qed |
|
145 |
qed |
|
146 |
||
147 |
lemma height_bal: |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
148 |
"\<lbrakk> n \<le> length xs; bal n xs = (t,zs) \<rbrakk> \<Longrightarrow> height t = nat \<lceil>log 2 (n + 1)\<rceil>" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
149 |
proof(induction n xs arbitrary: t zs rule: bal.induct) |
64444 | 150 |
case (1 n xs) show ?case |
64018 | 151 |
proof cases |
152 |
assume "n = 0" thus ?thesis |
|
153 |
using "1.prems" by (simp add: bal_simps) |
|
154 |
next |
|
155 |
assume [arith]: "n \<noteq> 0" |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
156 |
from "1.prems" obtain l r ys where |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
157 |
b1: "bal (n div 2) xs = (l,ys)" and |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
158 |
b2: "bal (n - 1 - n div 2) (tl ys) = (r,zs)" and |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
159 |
t: "t = \<langle>l, hd ys, r\<rangle>" |
64018 | 160 |
by(auto simp: bal_simps Let_def split: prod.splits) |
161 |
let ?log1 = "nat \<lceil>log 2 (n div 2 + 1)\<rceil>" |
|
162 |
let ?log2 = "nat \<lceil>log 2 (n - 1 - n div 2 + 1)\<rceil>" |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
163 |
have 1: "n div 2 \<le> length xs" using "1.prems"(1) by linarith |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
164 |
have 2: "n - 1 - n div 2 \<le> length (tl ys)" using "1.prems"(1) size_bal[OF 1 b1] by simp |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
165 |
have IH1: "height l = ?log1" using "1.IH"(1) b1 "1.prems"(1) by simp |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
166 |
have IH2: "height r = ?log2" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
167 |
using b1 b2 size_bal[OF _ b1] size_bal[OF _ b2] "1.prems"(1) by(intro "1.IH"(2)) auto |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
168 |
have 0: "log 2 (n div 2 + 1) \<ge> 0" by simp |
64018 | 169 |
have "n - 1 - n div 2 + 1 \<le> n div 2 + 1" by arith |
170 |
hence le: "?log2 \<le> ?log1" |
|
171 |
by(simp add: nat_mono ceiling_mono del: nat_ceiling_le_eq) |
|
172 |
have "height t = max ?log1 ?log2 + 1" by (simp add: t IH1 IH2) |
|
173 |
also have "\<dots> = ?log1 + 1" using le by (simp add: max_absorb1) |
|
174 |
also have "\<dots> = nat \<lceil>log 2 (n div 2 + 1) + 1\<rceil>" using 0 by linarith |
|
175 |
also have "\<dots> = nat \<lceil>log 2 (n + 1)\<rceil>" |
|
66515 | 176 |
using ceiling_log2_div2[of "n+1"] by (simp) |
63643 | 177 |
finally show ?thesis . |
178 |
qed |
|
179 |
qed |
|
180 |
||
181 |
lemma balanced_bal: |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
182 |
assumes "n \<le> length xs" "bal n xs = (t,ys)" shows "balanced t" |
64018 | 183 |
unfolding balanced_def |
184 |
using height_bal[OF assms] min_height_bal[OF assms] |
|
185 |
by linarith |
|
63643 | 186 |
|
64444 | 187 |
lemma height_bal_list: |
188 |
"n \<le> length xs \<Longrightarrow> height (bal_list n xs) = nat \<lceil>log 2 (n + 1)\<rceil>" |
|
189 |
unfolding bal_list_def by (metis height_bal prod.collapse) |
|
190 |
||
64018 | 191 |
lemma height_balance_list: |
192 |
"height (balance_list xs) = nat \<lceil>log 2 (length xs + 1)\<rceil>" |
|
64444 | 193 |
by (simp add: balance_list_def height_bal_list) |
194 |
||
195 |
corollary height_bal_tree: |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
196 |
"n \<le> size t \<Longrightarrow> height (bal_tree n t) = nat\<lceil>log 2 (n + 1)\<rceil>" |
64444 | 197 |
unfolding bal_list_def bal_tree_def |
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
198 |
by (metis bal_list_def height_bal_list length_inorder) |
64018 | 199 |
|
200 |
corollary height_balance_tree: |
|
66515 | 201 |
"height (balance_tree t) = nat\<lceil>log 2 (size t + 1)\<rceil>" |
64444 | 202 |
by (simp add: bal_tree_def balance_tree_def height_bal_list) |
203 |
||
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
204 |
corollary balanced_bal_list[simp]: "n \<le> length xs \<Longrightarrow> balanced (bal_list n xs)" |
64444 | 205 |
unfolding bal_list_def by (metis balanced_bal prod.collapse) |
63829 | 206 |
|
207 |
corollary balanced_balance_list[simp]: "balanced (balance_list xs)" |
|
64444 | 208 |
by (simp add: balance_list_def) |
209 |
||
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
210 |
corollary balanced_bal_tree[simp]: "n \<le> size t \<Longrightarrow> balanced (bal_tree n t)" |
64444 | 211 |
by (simp add: bal_tree_def) |
63829 | 212 |
|
213 |
corollary balanced_balance_tree[simp]: "balanced (balance_tree t)" |
|
214 |
by (simp add: balance_tree_def) |
|
215 |
||
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
216 |
lemma wbalanced_bal: "\<lbrakk> n \<le> length xs; bal n xs = (t,ys) \<rbrakk> \<Longrightarrow> wbalanced t" |
64444 | 217 |
proof(induction n xs arbitrary: t ys rule: bal.induct) |
218 |
case (1 n xs) |
|
63861 | 219 |
show ?case |
220 |
proof cases |
|
221 |
assume "n = 0" |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
222 |
thus ?thesis using "1.prems"(2) by(simp add: bal_simps) |
63861 | 223 |
next |
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
224 |
assume [arith]: "n \<noteq> 0" |
63861 | 225 |
with "1.prems" obtain l ys r zs where |
64444 | 226 |
rec1: "bal (n div 2) xs = (l, ys)" and |
227 |
rec2: "bal (n - 1 - n div 2) (tl ys) = (r, zs)" and |
|
63861 | 228 |
t: "t = \<langle>l, hd ys, r\<rangle>" |
229 |
by(auto simp add: bal_simps Let_def split: prod.splits) |
|
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
230 |
have l: "wbalanced l" using "1.IH"(1)[OF \<open>n\<noteq>0\<close> refl _ rec1] "1.prems"(1) by linarith |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
231 |
have "wbalanced r" |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
232 |
using rec1 rec2 pre_rec2[OF "1.prems"(1) rec1] by(intro "1.IH"(2)) auto |
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
233 |
with l t size_bal[OF _ rec1] size_bal[OF _ rec2] "1.prems"(1) |
63861 | 234 |
show ?thesis by auto |
235 |
qed |
|
236 |
qed |
|
237 |
||
64541 | 238 |
text\<open>An alternative proof via @{thm balanced_if_wbalanced}:\<close> |
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
239 |
lemma "\<lbrakk> n \<le> length xs; bal n xs = (t,ys) \<rbrakk> \<Longrightarrow> balanced t" |
64541 | 240 |
by(rule balanced_if_wbalanced[OF wbalanced_bal]) |
241 |
||
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
242 |
lemma wbalanced_bal_list[simp]: "n \<le> length xs \<Longrightarrow> wbalanced (bal_list n xs)" |
64444 | 243 |
by(simp add: bal_list_def) (metis prod.collapse wbalanced_bal) |
244 |
||
245 |
lemma wbalanced_balance_list[simp]: "wbalanced (balance_list xs)" |
|
246 |
by(simp add: balance_list_def) |
|
247 |
||
70747
548420d389ea
Enforced precodition "n <= length xs" to avoid relying on "hd []".
nipkow
parents:
66516
diff
changeset
|
248 |
lemma wbalanced_bal_tree[simp]: "n \<le> size t \<Longrightarrow> wbalanced (bal_tree n t)" |
64444 | 249 |
by(simp add: bal_tree_def) |
250 |
||
63861 | 251 |
lemma wbalanced_balance_tree: "wbalanced (balance_tree t)" |
64444 | 252 |
by (simp add: balance_tree_def) |
63861 | 253 |
|
63829 | 254 |
hide_const (open) bal |
63643 | 255 |
|
256 |
end |