src/HOL/Library/Quicksort.thy
author bulwahn
Sat, 19 Jul 2008 19:27:13 +0200
changeset 27656 d4f6e64ee7cc
parent 27580 e16f4baa3db6
child 27682 25aceefd4786
permissions -rw-r--r--
added verification framework for the HeapMonad and quicksort as example for this framework
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     1
(*  ID:         $Id$
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     2
    Author:     Tobias Nipkow
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     3
    Copyright   1994 TU Muenchen
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     4
*)
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     5
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     6
header{*Quicksort*}
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     7
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
     8
theory Quicksort
27368
9f90ac19e32b established Plain theory and image
haftmann
parents: 25062
diff changeset
     9
imports Plain Multiset
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    10
begin
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    11
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    12
context linorder
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    13
begin
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    14
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    15
function quicksort :: "'a list \<Rightarrow> 'a list" where
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    16
"quicksort []     = []" |
25062
af5ef0d4d655 global class syntax
haftmann
parents: 24615
diff changeset
    17
"quicksort (x#xs) = quicksort([y\<leftarrow>xs. ~ x\<le>y]) @ [x] @ quicksort([y\<leftarrow>xs. x\<le>y])"
24615
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    18
by pat_completeness auto
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    19
27580
haftmann
parents: 27368
diff changeset
    20
termination by (relation "measure size")
haftmann
parents: 27368
diff changeset
    21
  (auto simp: length_filter_le [THEN order_class.le_less_trans])
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"
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    25
by (induct xs rule: quicksort.induct) (auto simp: union_ac)
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"
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    28
by(simp add: set_count_greater_0)
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    29
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    30
lemma sorted_quicksort: "sorted(quicksort xs)"
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    31
apply (induct xs rule: quicksort.induct)
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    32
 apply simp
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    33
apply (simp add:sorted_Cons sorted_append not_le less_imp_le)
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    34
apply (metis leD le_cases le_less_trans)
17dbd993293d Added function package to PreList
nipkow
parents:
diff changeset
    35
done
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