1476
|
1 |
(* Title: HOL/ex/insort.thy
|
969
|
2 |
ID: $Id$
|
1476
|
3 |
Author: Tobias Nipkow
|
969
|
4 |
Copyright 1994 TU Muenchen
|
|
5 |
|
|
6 |
Insertion sort
|
|
7 |
*)
|
|
8 |
|
13159
|
9 |
theory InSort = Sorting:
|
969
|
10 |
|
|
11 |
consts
|
13159
|
12 |
ins :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
13 |
insort :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
969
|
14 |
|
5184
|
15 |
primrec
|
8422
|
16 |
"ins le x [] = [x]"
|
|
17 |
"ins le x (y#ys) = (if le x y then (x#y#ys) else y#(ins le x ys))"
|
13159
|
18 |
|
5184
|
19 |
primrec
|
8422
|
20 |
"insort le [] = []"
|
|
21 |
"insort le (x#xs) = ins le x (insort le xs)"
|
13159
|
22 |
|
|
23 |
|
|
24 |
lemma multiset_ins[simp]:
|
|
25 |
"\<And>y. multiset(ins le x xs) y = multiset (x#xs) y"
|
|
26 |
by (induct "xs") auto
|
|
27 |
|
|
28 |
lemma insort_permutes[simp]:
|
|
29 |
"\<And>x. multiset(insort le xs) x = multiset xs x";
|
|
30 |
by (induct "xs") auto
|
|
31 |
|
|
32 |
lemma set_ins[simp]: "set(ins le x xs) = insert x (set xs)"
|
|
33 |
by (simp add: set_via_multiset) fast
|
|
34 |
|
|
35 |
lemma sorted_ins[simp]:
|
|
36 |
"\<lbrakk> total le; transf le \<rbrakk> \<Longrightarrow> sorted le (ins le x xs) = sorted le xs";
|
|
37 |
apply (induct xs)
|
|
38 |
apply simp_all
|
|
39 |
apply (unfold Sorting.total_def Sorting.transf_def)
|
|
40 |
apply blast
|
|
41 |
done
|
|
42 |
|
|
43 |
lemma sorted_insort:
|
|
44 |
"[| total(le); transf(le) |] ==> sorted le (insort le xs)"
|
|
45 |
by (induct xs) auto
|
|
46 |
|
969
|
47 |
end
|
1896
|
48 |
|