src/HOL/ex/InSort.thy
author paulson
Fri, 14 Sep 2007 15:27:12 +0200
changeset 24573 5bbdc9b60648
parent 15815 62854cac5410
permissions -rw-r--r--
tidied
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1476
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1376
diff changeset
     1
(*  Title:      HOL/ex/insort.thy
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     2
    ID:         $Id$
1476
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1376
diff changeset
     3
    Author:     Tobias Nipkow
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     4
    Copyright   1994 TU Muenchen
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     5
*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     6
15815
paulson
parents: 15631
diff changeset
     7
header{*Insertion Sort*}
paulson
parents: 15631
diff changeset
     8
paulson
parents: 15631
diff changeset
     9
theory InSort
paulson
parents: 15631
diff changeset
    10
imports Sorting
paulson
parents: 15631
diff changeset
    11
begin
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    12
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    13
consts
15815
paulson
parents: 15631
diff changeset
    14
  ins    :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    15
  insort :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    16
5184
9b8547a9496a Adapted to new datatype package.
berghofe
parents: 1896
diff changeset
    17
primrec
8422
6c6a5410a9bd renamed "f" to "le" and "mset" to "multiset"
paulson
parents: 5184
diff changeset
    18
  "ins le x [] = [x]"
6c6a5410a9bd renamed "f" to "le" and "mset" to "multiset"
paulson
parents: 5184
diff changeset
    19
  "ins le x (y#ys) = (if le x y then (x#y#ys) else y#(ins le x ys))"
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    20
5184
9b8547a9496a Adapted to new datatype package.
berghofe
parents: 1896
diff changeset
    21
primrec
8422
6c6a5410a9bd renamed "f" to "le" and "mset" to "multiset"
paulson
parents: 5184
diff changeset
    22
  "insort le [] = []"
6c6a5410a9bd renamed "f" to "le" and "mset" to "multiset"
paulson
parents: 5184
diff changeset
    23
  "insort le (x#xs) = ins le x (insort le xs)"
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    24
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    25
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    26
lemma multiset_ins[simp]:
15631
cbee04ce413b use Library/Multiset instead of own definition
kleing
parents: 13159
diff changeset
    27
 "\<And>y. multiset_of (ins le x xs) = multiset_of (x#xs)"
cbee04ce413b use Library/Multiset instead of own definition
kleing
parents: 13159
diff changeset
    28
  by (induct xs) (auto simp: union_ac)
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    29
15815
paulson
parents: 15631
diff changeset
    30
theorem insort_permutes[simp]:
15631
cbee04ce413b use Library/Multiset instead of own definition
kleing
parents: 13159
diff changeset
    31
 "\<And>x. multiset_of (insort le xs) = multiset_of xs"
cbee04ce413b use Library/Multiset instead of own definition
kleing
parents: 13159
diff changeset
    32
  by (induct "xs") auto
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    33
15815
paulson
parents: 15631
diff changeset
    34
lemma set_ins [simp]: "set(ins le x xs) = insert x (set xs)"
15631
cbee04ce413b use Library/Multiset instead of own definition
kleing
parents: 13159
diff changeset
    35
  by (simp add: set_count_greater_0) fast
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    36
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    37
lemma sorted_ins[simp]:
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    38
 "\<lbrakk> total le; transf le \<rbrakk> \<Longrightarrow> sorted le (ins le x xs) = sorted le xs";
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    39
apply (induct xs)
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    40
apply simp_all
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    41
apply (unfold Sorting.total_def Sorting.transf_def)
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    42
apply blast
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    43
done
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    44
15815
paulson
parents: 15631
diff changeset
    45
theorem sorted_insort:
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    46
 "[| total(le); transf(le) |] ==>  sorted le (insort le xs)"
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    47
by (induct xs) auto
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    48
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    49
end
1896
df4e40b9ff6d Simplified primrec - definitions.
berghofe
parents: 1476
diff changeset
    50