src/HOL/ex/Quicksort.thy
author bulwahn
Mon, 03 Oct 2011 14:43:13 +0200
changeset 45116 f947eeef6b6f
parent 44604 1ad3159323dc
child 58889 5b7a9633cfa8
permissions -rw-r--r--
adding lemma about rel_pow in Transitive_Closure for executable equation of the (refl) transitive closure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30738
0842e906300c normalized imports
haftmann
parents: 28041
diff changeset
     1
(*  Author:     Tobias Nipkow
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     2
    Copyright   1994 TU Muenchen
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     3
*)
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     4
40349
131cf8790a1c moved theory Quicksort from Library/ to ex/
haftmann
parents: 37075
diff changeset
     5
header {* Quicksort with function package *}
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     6
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     7
theory Quicksort
44604
1ad3159323dc tuned import;
wenzelm
parents: 41413
diff changeset
     8
imports "~~/src/HOL/Library/Multiset"
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     9
begin
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    10
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    11
context linorder
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    12
begin
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    13
28041
f496e9f343b7 quicksort: function -> fun
krauss
parents: 27682
diff changeset
    14
fun quicksort :: "'a list \<Rightarrow> 'a list" where
37075
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    15
  "quicksort []     = []"
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    16
| "quicksort (x#xs) = quicksort [y\<leftarrow>xs. \<not> x\<le>y] @ [x] @ quicksort [y\<leftarrow>xs. x\<le>y]"
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    17
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    18
lemma [code]:
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    19
  "quicksort []     = []"
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    20
  "quicksort (x#xs) = quicksort [y\<leftarrow>xs. y<x] @ [x] @ quicksort [y\<leftarrow>xs. x\<le>y]"
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    21
  by (simp_all add: not_le)
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    22
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    23
lemma quicksort_permutes [simp]:
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    24
  "multiset_of (quicksort xs) = multiset_of xs"
37075
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    25
  by (induct xs rule: quicksort.induct) (simp_all add: ac_simps)
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    26
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    27
lemma set_quicksort [simp]: "set (quicksort xs) = set xs"
37075
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    28
  by (simp add: set_count_greater_0)
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    29
37075
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    30
lemma sorted_quicksort: "sorted (quicksort xs)"
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    31
  by (induct xs rule: quicksort.induct) (auto simp add: sorted_Cons sorted_append not_le less_imp_le)
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    32
40349
131cf8790a1c moved theory Quicksort from Library/ to ex/
haftmann
parents: 37075
diff changeset
    33
theorem sort_quicksort:
37075
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    34
  "sort = quicksort"
a680ce27aa56 modernized sorting algorithms; quicksort implements sort
haftmann
parents: 30738
diff changeset
    35
  by (rule ext, rule properties_for_sort) (fact quicksort_permutes sorted_quicksort)+
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    36
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    37
end
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    38
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    39
end