author | wenzelm |
Fri, 07 Aug 2020 22:28:04 +0200 | |
changeset 72118 | 84f716e72fa3 |
parent 71918 | 4e0a58818edc |
child 72501 | 70b420065a07 |
permissions | -rw-r--r-- |
66543 | 1 |
(* Author: Tobias Nipkow *) |
2 |
||
70250 | 3 |
section "Sorting" |
4 |
||
66543 | 5 |
theory Sorting |
6 |
imports |
|
7 |
Complex_Main |
|
8 |
"HOL-Library.Multiset" |
|
9 |
begin |
|
10 |
||
68160 | 11 |
hide_const List.insort |
66543 | 12 |
|
13 |
declare Let_def [simp] |
|
14 |
||
15 |
||
16 |
subsection "Insertion Sort" |
|
17 |
||
18 |
fun insort :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
19 |
"insort x [] = [x]" | |
|
20 |
"insort x (y#ys) = |
|
21 |
(if x \<le> y then x#y#ys else y#(insort x ys))" |
|
22 |
||
23 |
fun isort :: "'a::linorder list \<Rightarrow> 'a list" where |
|
24 |
"isort [] = []" | |
|
25 |
"isort (x#xs) = insort x (isort xs)" |
|
26 |
||
68078 | 27 |
|
66543 | 28 |
subsubsection "Functional Correctness" |
29 |
||
30 |
lemma mset_insort: "mset (insort x xs) = add_mset x (mset xs)" |
|
31 |
apply(induction xs) |
|
32 |
apply auto |
|
33 |
done |
|
34 |
||
35 |
lemma mset_isort: "mset (isort xs) = mset xs" |
|
36 |
apply(induction xs) |
|
37 |
apply simp |
|
38 |
apply (simp add: mset_insort) |
|
39 |
done |
|
40 |
||
41 |
lemma set_insort: "set (insort x xs) = insert x (set xs)" |
|
42 |
by (metis mset_insort set_mset_add_mset_insert set_mset_mset) |
|
43 |
||
44 |
lemma sorted_insort: "sorted (insort a xs) = sorted xs" |
|
45 |
apply(induction xs) |
|
46 |
apply(auto simp add: set_insort) |
|
47 |
done |
|
48 |
||
68934 | 49 |
lemma sorted_isort: "sorted (isort xs)" |
66543 | 50 |
apply(induction xs) |
51 |
apply(auto simp: sorted_insort) |
|
52 |
done |
|
53 |
||
68078 | 54 |
|
55 |
subsubsection "Time Complexity" |
|
66543 | 56 |
|
57 |
text \<open>We count the number of function calls.\<close> |
|
58 |
||
59 |
text\<open> |
|
60 |
\<open>insort x [] = [x]\<close> |
|
61 |
\<open>insort x (y#ys) = |
|
62 |
(if x \<le> y then x#y#ys else y#(insort x ys))\<close> |
|
63 |
\<close> |
|
64 |
fun t_insort :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> nat" where |
|
65 |
"t_insort x [] = 1" | |
|
66 |
"t_insort x (y#ys) = |
|
67 |
(if x \<le> y then 0 else t_insort x ys) + 1" |
|
68 |
||
69 |
text\<open> |
|
70 |
\<open>isort [] = []\<close> |
|
71 |
\<open>isort (x#xs) = insort x (isort xs)\<close> |
|
72 |
\<close> |
|
73 |
fun t_isort :: "'a::linorder list \<Rightarrow> nat" where |
|
74 |
"t_isort [] = 1" | |
|
75 |
"t_isort (x#xs) = t_isort xs + t_insort x (isort xs) + 1" |
|
76 |
||
77 |
||
78 |
lemma t_insort_length: "t_insort x xs \<le> length xs + 1" |
|
79 |
apply(induction xs) |
|
80 |
apply auto |
|
81 |
done |
|
82 |
||
83 |
lemma length_insort: "length (insort x xs) = length xs + 1" |
|
84 |
apply(induction xs) |
|
85 |
apply auto |
|
86 |
done |
|
87 |
||
88 |
lemma length_isort: "length (isort xs) = length xs" |
|
89 |
apply(induction xs) |
|
90 |
apply (auto simp: length_insort) |
|
91 |
done |
|
92 |
||
93 |
lemma t_isort_length: "t_isort xs \<le> (length xs + 1) ^ 2" |
|
94 |
proof(induction xs) |
|
95 |
case Nil show ?case by simp |
|
96 |
next |
|
97 |
case (Cons x xs) |
|
98 |
have "t_isort (x#xs) = t_isort xs + t_insort x (isort xs) + 1" by simp |
|
99 |
also have "\<dots> \<le> (length xs + 1) ^ 2 + t_insort x (isort xs) + 1" |
|
100 |
using Cons.IH by simp |
|
101 |
also have "\<dots> \<le> (length xs + 1) ^ 2 + length xs + 1 + 1" |
|
102 |
using t_insort_length[of x "isort xs"] by (simp add: length_isort) |
|
103 |
also have "\<dots> \<le> (length(x#xs) + 1) ^ 2" |
|
104 |
by (simp add: power2_eq_square) |
|
105 |
finally show ?case . |
|
106 |
qed |
|
107 |
||
108 |
||
109 |
subsection "Merge Sort" |
|
110 |
||
111 |
fun merge :: "'a::linorder list \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
112 |
"merge [] ys = ys" | |
|
113 |
"merge xs [] = xs" | |
|
114 |
"merge (x#xs) (y#ys) = (if x \<le> y then x # merge xs (y#ys) else y # merge (x#xs) ys)" |
|
115 |
||
116 |
fun msort :: "'a::linorder list \<Rightarrow> 'a list" where |
|
117 |
"msort xs = (let n = length xs in |
|
118 |
if n \<le> 1 then xs |
|
119 |
else merge (msort (take (n div 2) xs)) (msort (drop (n div 2) xs)))" |
|
120 |
||
121 |
declare msort.simps [simp del] |
|
122 |
||
68078 | 123 |
|
67983 | 124 |
subsubsection "Functional Correctness" |
125 |
||
126 |
lemma mset_merge: "mset(merge xs ys) = mset xs + mset ys" |
|
127 |
by(induction xs ys rule: merge.induct) auto |
|
128 |
||
68963 | 129 |
lemma mset_msort: "mset (msort xs) = mset xs" |
67983 | 130 |
proof(induction xs rule: msort.induct) |
131 |
case (1 xs) |
|
132 |
let ?n = "length xs" |
|
68966 | 133 |
let ?ys = "take (?n div 2) xs" |
134 |
let ?zs = "drop (?n div 2) xs" |
|
67983 | 135 |
show ?case |
136 |
proof cases |
|
137 |
assume "?n \<le> 1" |
|
138 |
thus ?thesis by(simp add: msort.simps[of xs]) |
|
139 |
next |
|
140 |
assume "\<not> ?n \<le> 1" |
|
68966 | 141 |
hence "mset (msort xs) = mset (msort ?ys) + mset (msort ?zs)" |
67983 | 142 |
by(simp add: msort.simps[of xs] mset_merge) |
68966 | 143 |
also have "\<dots> = mset ?ys + mset ?zs" |
67983 | 144 |
using \<open>\<not> ?n \<le> 1\<close> by(simp add: "1.IH") |
68966 | 145 |
also have "\<dots> = mset (?ys @ ?zs)" by (simp del: append_take_drop_id) |
67983 | 146 |
also have "\<dots> = mset xs" by simp |
147 |
finally show ?thesis . |
|
148 |
qed |
|
149 |
qed |
|
150 |
||
68966 | 151 |
text \<open>Via the previous lemma or directly:\<close> |
68963 | 152 |
|
67983 | 153 |
lemma set_merge: "set(merge xs ys) = set xs \<union> set ys" |
68963 | 154 |
by (metis mset_merge set_mset_mset set_mset_union) |
155 |
||
156 |
lemma "set(merge xs ys) = set xs \<union> set ys" |
|
67983 | 157 |
by(induction xs ys rule: merge.induct) (auto) |
158 |
||
159 |
lemma sorted_merge: "sorted (merge xs ys) \<longleftrightarrow> (sorted xs \<and> sorted ys)" |
|
160 |
by(induction xs ys rule: merge.induct) (auto simp: set_merge) |
|
161 |
||
68966 | 162 |
lemma sorted_msort: "sorted (msort xs)" |
67983 | 163 |
proof(induction xs rule: msort.induct) |
164 |
case (1 xs) |
|
165 |
let ?n = "length xs" |
|
166 |
show ?case |
|
167 |
proof cases |
|
168 |
assume "?n \<le> 1" |
|
169 |
thus ?thesis by(simp add: msort.simps[of xs] sorted01) |
|
170 |
next |
|
171 |
assume "\<not> ?n \<le> 1" |
|
172 |
thus ?thesis using "1.IH" |
|
68966 | 173 |
by(simp add: sorted_merge msort.simps[of xs]) |
67983 | 174 |
qed |
175 |
qed |
|
176 |
||
68078 | 177 |
|
178 |
subsubsection "Time Complexity" |
|
67983 | 179 |
|
180 |
text \<open>We only count the number of comparisons between list elements.\<close> |
|
66543 | 181 |
|
182 |
fun c_merge :: "'a::linorder list \<Rightarrow> 'a list \<Rightarrow> nat" where |
|
183 |
"c_merge [] ys = 0" | |
|
184 |
"c_merge xs [] = 0" | |
|
185 |
"c_merge (x#xs) (y#ys) = 1 + (if x \<le> y then c_merge xs (y#ys) else c_merge (x#xs) ys)" |
|
186 |
||
187 |
lemma c_merge_ub: "c_merge xs ys \<le> length xs + length ys" |
|
188 |
by (induction xs ys rule: c_merge.induct) auto |
|
189 |
||
190 |
fun c_msort :: "'a::linorder list \<Rightarrow> nat" where |
|
191 |
"c_msort xs = |
|
192 |
(let n = length xs; |
|
193 |
ys = take (n div 2) xs; |
|
194 |
zs = drop (n div 2) xs |
|
195 |
in if n \<le> 1 then 0 |
|
196 |
else c_msort ys + c_msort zs + c_merge (msort ys) (msort zs))" |
|
197 |
||
198 |
declare c_msort.simps [simp del] |
|
199 |
||
200 |
lemma length_merge: "length(merge xs ys) = length xs + length ys" |
|
201 |
apply (induction xs ys rule: merge.induct) |
|
202 |
apply auto |
|
203 |
done |
|
204 |
||
205 |
lemma length_msort: "length(msort xs) = length xs" |
|
206 |
proof (induction xs rule: msort.induct) |
|
207 |
case (1 xs) |
|
71918 | 208 |
show ?case |
209 |
by (auto simp: msort.simps [of xs] 1 length_merge) |
|
66543 | 210 |
qed |
211 |
text \<open>Why structured proof? |
|
212 |
To have the name "xs" to specialize msort.simps with xs |
|
213 |
to ensure that msort.simps cannot be used recursively. |
|
214 |
Also works without this precaution, but that is just luck.\<close> |
|
215 |
||
216 |
lemma c_msort_le: "length xs = 2^k \<Longrightarrow> c_msort xs \<le> k * 2^k" |
|
217 |
proof(induction k arbitrary: xs) |
|
218 |
case 0 thus ?case by (simp add: c_msort.simps) |
|
219 |
next |
|
220 |
case (Suc k) |
|
221 |
let ?n = "length xs" |
|
222 |
let ?ys = "take (?n div 2) xs" |
|
223 |
let ?zs = "drop (?n div 2) xs" |
|
224 |
show ?case |
|
225 |
proof (cases "?n \<le> 1") |
|
226 |
case True |
|
227 |
thus ?thesis by(simp add: c_msort.simps) |
|
228 |
next |
|
229 |
case False |
|
230 |
have "c_msort(xs) = |
|
231 |
c_msort ?ys + c_msort ?zs + c_merge (msort ?ys) (msort ?zs)" |
|
232 |
by (simp add: c_msort.simps msort.simps) |
|
233 |
also have "\<dots> \<le> c_msort ?ys + c_msort ?zs + length ?ys + length ?zs" |
|
234 |
using c_merge_ub[of "msort ?ys" "msort ?zs"] length_msort[of ?ys] length_msort[of ?zs] |
|
235 |
by arith |
|
236 |
also have "\<dots> \<le> k * 2^k + c_msort ?zs + length ?ys + length ?zs" |
|
237 |
using Suc.IH[of ?ys] Suc.prems by simp |
|
238 |
also have "\<dots> \<le> k * 2^k + k * 2^k + length ?ys + length ?zs" |
|
239 |
using Suc.IH[of ?zs] Suc.prems by simp |
|
240 |
also have "\<dots> = 2 * k * 2^k + 2 * 2 ^ k" |
|
241 |
using Suc.prems by simp |
|
242 |
finally show ?thesis by simp |
|
243 |
qed |
|
244 |
qed |
|
245 |
||
70295 | 246 |
(* Beware of implicit conversions: *) |
68963 | 247 |
lemma c_msort_log: "length xs = 2^k \<Longrightarrow> c_msort xs \<le> length xs * log 2 (length xs)" |
66543 | 248 |
using c_msort_le[of xs k] apply (simp add: log_nat_power algebra_simps) |
66912
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents:
66543
diff
changeset
|
249 |
by (metis (mono_tags) numeral_power_eq_of_nat_cancel_iff of_nat_le_iff of_nat_mult) |
66543 | 250 |
|
67983 | 251 |
|
252 |
subsection "Bottom-Up Merge Sort" |
|
253 |
||
254 |
fun merge_adj :: "('a::linorder) list list \<Rightarrow> 'a list list" where |
|
255 |
"merge_adj [] = []" | |
|
256 |
"merge_adj [xs] = [xs]" | |
|
257 |
"merge_adj (xs # ys # zss) = merge xs ys # merge_adj zss" |
|
258 |
||
259 |
text \<open>For the termination proof of \<open>merge_all\<close> below.\<close> |
|
260 |
lemma length_merge_adjacent[simp]: "length (merge_adj xs) = (length xs + 1) div 2" |
|
261 |
by (induction xs rule: merge_adj.induct) auto |
|
262 |
||
263 |
fun merge_all :: "('a::linorder) list list \<Rightarrow> 'a list" where |
|
68970 | 264 |
"merge_all [] = []" | |
67983 | 265 |
"merge_all [xs] = xs" | |
266 |
"merge_all xss = merge_all (merge_adj xss)" |
|
267 |
||
268 |
definition msort_bu :: "('a::linorder) list \<Rightarrow> 'a list" where |
|
68971 | 269 |
"msort_bu xs = merge_all (map (\<lambda>x. [x]) xs)" |
67983 | 270 |
|
68078 | 271 |
|
67983 | 272 |
subsubsection "Functional Correctness" |
273 |
||
274 |
lemma mset_merge_adj: |
|
69036 | 275 |
"\<Union># (image_mset mset (mset (merge_adj xss))) = \<Union># (image_mset mset (mset xss))" |
67983 | 276 |
by(induction xss rule: merge_adj.induct) (auto simp: mset_merge) |
277 |
||
68967 | 278 |
lemma mset_merge_all: |
68971 | 279 |
"mset (merge_all xss) = (\<Union># (mset (map mset xss)))" |
67983 | 280 |
by(induction xss rule: merge_all.induct) (auto simp: mset_merge mset_merge_adj) |
281 |
||
68968 | 282 |
lemma mset_msort_bu: "mset (msort_bu xs) = mset xs" |
283 |
by(simp add: msort_bu_def mset_merge_all comp_def) |
|
284 |
||
67983 | 285 |
lemma sorted_merge_adj: |
286 |
"\<forall>xs \<in> set xss. sorted xs \<Longrightarrow> \<forall>xs \<in> set (merge_adj xss). sorted xs" |
|
287 |
by(induction xss rule: merge_adj.induct) (auto simp: sorted_merge) |
|
288 |
||
289 |
lemma sorted_merge_all: |
|
68971 | 290 |
"\<forall>xs \<in> set xss. sorted xs \<Longrightarrow> sorted (merge_all xss)" |
67983 | 291 |
apply(induction xss rule: merge_all.induct) |
292 |
using [[simp_depth_limit=3]] by (auto simp add: sorted_merge_adj) |
|
293 |
||
294 |
lemma sorted_msort_bu: "sorted (msort_bu xs)" |
|
295 |
by(simp add: msort_bu_def sorted_merge_all) |
|
296 |
||
68078 | 297 |
|
298 |
subsubsection "Time Complexity" |
|
67983 | 299 |
|
68139 | 300 |
fun c_merge_adj :: "('a::linorder) list list \<Rightarrow> nat" where |
67983 | 301 |
"c_merge_adj [] = 0" | |
68161 | 302 |
"c_merge_adj [xs] = 0" | |
303 |
"c_merge_adj (xs # ys # zss) = c_merge xs ys + c_merge_adj zss" |
|
67983 | 304 |
|
68139 | 305 |
fun c_merge_all :: "('a::linorder) list list \<Rightarrow> nat" where |
68971 | 306 |
"c_merge_all [] = 0" | |
68161 | 307 |
"c_merge_all [xs] = 0" | |
308 |
"c_merge_all xss = c_merge_adj xss + c_merge_all (merge_adj xss)" |
|
67983 | 309 |
|
68139 | 310 |
definition c_msort_bu :: "('a::linorder) list \<Rightarrow> nat" where |
68971 | 311 |
"c_msort_bu xs = c_merge_all (map (\<lambda>x. [x]) xs)" |
67983 | 312 |
|
313 |
lemma length_merge_adj: |
|
68974 | 314 |
"\<lbrakk> even(length xss); \<forall>xs \<in> set xss. length xs = m \<rbrakk> |
315 |
\<Longrightarrow> \<forall>xs \<in> set (merge_adj xss). length xs = 2*m" |
|
68161 | 316 |
by(induction xss rule: merge_adj.induct) (auto simp: length_merge) |
67983 | 317 |
|
68161 | 318 |
lemma c_merge_adj: "\<forall>xs \<in> set xss. length xs = m \<Longrightarrow> c_merge_adj xss \<le> m * length xss" |
319 |
proof(induction xss rule: c_merge_adj.induct) |
|
67983 | 320 |
case 1 thus ?case by simp |
321 |
next |
|
322 |
case 2 thus ?case by simp |
|
323 |
next |
|
324 |
case (3 x y) thus ?case using c_merge_ub[of x y] by (simp add: algebra_simps) |
|
325 |
qed |
|
326 |
||
68161 | 327 |
lemma c_merge_all: "\<lbrakk> \<forall>xs \<in> set xss. length xs = m; length xss = 2^k \<rbrakk> |
328 |
\<Longrightarrow> c_merge_all xss \<le> m * k * 2^k" |
|
329 |
proof (induction xss arbitrary: k m rule: c_merge_all.induct) |
|
67983 | 330 |
case 1 thus ?case by simp |
331 |
next |
|
68158 | 332 |
case 2 thus ?case by simp |
67983 | 333 |
next |
68162 | 334 |
case (3 xs ys xss) |
335 |
let ?xss = "xs # ys # xss" |
|
336 |
let ?xss2 = "merge_adj ?xss" |
|
67983 | 337 |
obtain k' where k': "k = Suc k'" using "3.prems"(2) |
338 |
by (metis length_Cons nat.inject nat_power_eq_Suc_0_iff nat.exhaust) |
|
68972 | 339 |
have "even (length ?xss)" using "3.prems"(2) k' by auto |
340 |
from length_merge_adj[OF this "3.prems"(1)] |
|
341 |
have *: "\<forall>x \<in> set(merge_adj ?xss). length x = 2*m" . |
|
68162 | 342 |
have **: "length ?xss2 = 2 ^ k'" using "3.prems"(2) k' by auto |
343 |
have "c_merge_all ?xss = c_merge_adj ?xss + c_merge_all ?xss2" by simp |
|
344 |
also have "\<dots> \<le> m * 2^k + c_merge_all ?xss2" |
|
67983 | 345 |
using "3.prems"(2) c_merge_adj[OF "3.prems"(1)] by (auto simp: algebra_simps) |
346 |
also have "\<dots> \<le> m * 2^k + (2*m) * k' * 2^k'" |
|
68079 | 347 |
using "3.IH"[OF * **] by simp |
67983 | 348 |
also have "\<dots> = m * k * 2^k" |
349 |
using k' by (simp add: algebra_simps) |
|
350 |
finally show ?case . |
|
351 |
qed |
|
352 |
||
353 |
corollary c_msort_bu: "length xs = 2 ^ k \<Longrightarrow> c_msort_bu xs \<le> k * 2 ^ k" |
|
354 |
using c_merge_all[of "map (\<lambda>x. [x]) xs" 1] by (simp add: c_msort_bu_def) |
|
355 |
||
68993 | 356 |
|
357 |
subsection "Quicksort" |
|
358 |
||
359 |
fun quicksort :: "('a::linorder) list \<Rightarrow> 'a list" where |
|
360 |
"quicksort [] = []" | |
|
361 |
"quicksort (x#xs) = quicksort (filter (\<lambda>y. y < x) xs) @ [x] @ quicksort (filter (\<lambda>y. x \<le> y) xs)" |
|
362 |
||
363 |
lemma mset_quicksort: "mset (quicksort xs) = mset xs" |
|
364 |
apply (induction xs rule: quicksort.induct) |
|
365 |
apply (auto simp: not_le) |
|
366 |
done |
|
367 |
||
368 |
lemma set_quicksort: "set (quicksort xs) = set xs" |
|
369 |
by(rule mset_eq_setD[OF mset_quicksort]) |
|
370 |
||
371 |
lemma sorted_quicksort: "sorted (quicksort xs)" |
|
372 |
apply (induction xs rule: quicksort.induct) |
|
373 |
apply (auto simp add: sorted_append set_quicksort) |
|
374 |
done |
|
375 |
||
69005 | 376 |
|
377 |
subsection "Insertion Sort w.r.t. Keys and Stability" |
|
378 |
||
69597 | 379 |
text \<open>Note that \<^const>\<open>insort_key\<close> is already defined in theory \<^theory>\<open>HOL.List\<close>. |
69005 | 380 |
Thus some of the lemmas are already present as well.\<close> |
381 |
||
382 |
fun isort_key :: "('a \<Rightarrow> 'k::linorder) \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
383 |
"isort_key f [] = []" | |
|
384 |
"isort_key f (x # xs) = insort_key f x (isort_key f xs)" |
|
385 |
||
386 |
||
387 |
subsubsection "Standard functional correctness" |
|
388 |
||
389 |
lemma mset_insort_key: "mset (insort_key f x xs) = add_mset x (mset xs)" |
|
390 |
by(induction xs) simp_all |
|
391 |
||
392 |
lemma mset_isort_key: "mset (isort_key f xs) = mset xs" |
|
393 |
by(induction xs) (simp_all add: mset_insort_key) |
|
394 |
||
395 |
lemma set_isort_key: "set (isort_key f xs) = set xs" |
|
396 |
by (rule mset_eq_setD[OF mset_isort_key]) |
|
397 |
||
398 |
lemma sorted_insort_key: "sorted (map f (insort_key f a xs)) = sorted (map f xs)" |
|
399 |
by(induction xs)(auto simp: set_insort_key) |
|
400 |
||
401 |
lemma sorted_isort_key: "sorted (map f (isort_key f xs))" |
|
402 |
by(induction xs)(simp_all add: sorted_insort_key) |
|
403 |
||
404 |
||
405 |
subsubsection "Stability" |
|
406 |
||
407 |
lemma insort_is_Cons: "\<forall>x\<in>set xs. f a \<le> f x \<Longrightarrow> insort_key f a xs = a # xs" |
|
408 |
by (cases xs) auto |
|
409 |
||
410 |
lemma filter_insort_key_neg: |
|
411 |
"\<not> P x \<Longrightarrow> filter P (insort_key f x xs) = filter P xs" |
|
412 |
by (induction xs) simp_all |
|
413 |
||
414 |
lemma filter_insort_key_pos: |
|
415 |
"sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort_key f x xs) = insort_key f x (filter P xs)" |
|
416 |
by (induction xs) (auto, subst insort_is_Cons, auto) |
|
417 |
||
418 |
lemma sort_key_stable: "filter (\<lambda>y. f y = k) (isort_key f xs) = filter (\<lambda>y. f y = k) xs" |
|
419 |
proof (induction xs) |
|
420 |
case Nil thus ?case by simp |
|
421 |
next |
|
422 |
case (Cons a xs) |
|
423 |
thus ?case |
|
424 |
proof (cases "f a = k") |
|
425 |
case False thus ?thesis by (simp add: Cons.IH filter_insort_key_neg) |
|
426 |
next |
|
427 |
case True |
|
428 |
have "filter (\<lambda>y. f y = k) (isort_key f (a # xs)) |
|
429 |
= filter (\<lambda>y. f y = k) (insort_key f a (isort_key f xs))" by simp |
|
430 |
also have "\<dots> = insort_key f a (filter (\<lambda>y. f y = k) (isort_key f xs))" |
|
431 |
by (simp add: True filter_insort_key_pos sorted_isort_key) |
|
432 |
also have "\<dots> = insort_key f a (filter (\<lambda>y. f y = k) xs)" by (simp add: Cons.IH) |
|
433 |
also have "\<dots> = a # (filter (\<lambda>y. f y = k) xs)" by(simp add: True insort_is_Cons) |
|
434 |
also have "\<dots> = filter (\<lambda>y. f y = k) (a # xs)" by (simp add: True) |
|
435 |
finally show ?thesis . |
|
436 |
qed |
|
437 |
qed |
|
438 |
||
66543 | 439 |
end |