src/HOL/ex/InSort.thy
author nipkow
Fri, 17 May 2002 15:40:59 +0200
changeset 13159 2af7b94892ce
parent 8422 6c6a5410a9bd
child 15631 cbee04ce413b
permissions -rw-r--r--
Turned into Isar theories.
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
Insertion sort
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     7
*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     8
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
     9
theory InSort  =  Sorting:
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    10
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    11
consts
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    12
  ins :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    13
  insort :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    14
5184
9b8547a9496a Adapted to new datatype package.
berghofe
parents: 1896
diff changeset
    15
primrec
8422
6c6a5410a9bd renamed "f" to "le" and "mset" to "multiset"
paulson
parents: 5184
diff changeset
    16
  "ins le x [] = [x]"
6c6a5410a9bd renamed "f" to "le" and "mset" to "multiset"
paulson
parents: 5184
diff changeset
    17
  "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
    18
5184
9b8547a9496a Adapted to new datatype package.
berghofe
parents: 1896
diff changeset
    19
primrec
8422
6c6a5410a9bd renamed "f" to "le" and "mset" to "multiset"
paulson
parents: 5184
diff changeset
    20
  "insort le [] = []"
6c6a5410a9bd renamed "f" to "le" and "mset" to "multiset"
paulson
parents: 5184
diff changeset
    21
  "insort le (x#xs) = ins le x (insort le xs)"
13159
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    22
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    23
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    24
lemma multiset_ins[simp]:
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    25
 "\<And>y. multiset(ins le x xs) y = multiset (x#xs) y"
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    26
by (induct "xs") auto
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    27
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    28
lemma insort_permutes[simp]:
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    29
 "\<And>x. multiset(insort le xs) x = multiset xs x";
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    30
by (induct "xs") auto
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    31
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    32
lemma set_ins[simp]: "set(ins le x xs) = insert x (set xs)"
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    33
by (simp add: set_via_multiset) fast
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    34
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    35
lemma sorted_ins[simp]:
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    36
 "\<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
    37
apply (induct xs)
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    38
apply simp_all
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    39
apply (unfold Sorting.total_def Sorting.transf_def)
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    40
apply blast
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    41
done
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    42
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    43
lemma sorted_insort:
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    44
 "[| total(le); transf(le) |] ==>  sorted le (insort le xs)"
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    45
by (induct xs) auto
2af7b94892ce Turned into Isar theories.
nipkow
parents: 8422
diff changeset
    46
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    47
end
1896
df4e40b9ff6d Simplified primrec - definitions.
berghofe
parents: 1476
diff changeset
    48