src/HOL/ex/MergeSort.thy
author kleing
Sat Mar 26 00:01:56 2005 +0100 (2005-03-26)
changeset 15631 cbee04ce413b
parent 13201 3cc108872aca
child 15732 faa48c5b1402
permissions -rw-r--r--
use Library/Multiset instead of own definition
nipkow@13201
     1
(*  Title:      HOL/ex/Merge.thy
nipkow@13201
     2
    ID:         $Id$
nipkow@13201
     3
    Author:     Tobias Nipkow
nipkow@13201
     4
    Copyright   2002 TU Muenchen
nipkow@13201
     5
nipkow@13201
     6
Merge sort
nipkow@13201
     7
*)
nipkow@13201
     8
nipkow@13201
     9
theory MergeSort = Sorting:
nipkow@13201
    10
nipkow@13201
    11
consts merge :: "('a::linorder)list * 'a list \<Rightarrow> 'a list"
nipkow@13201
    12
nipkow@13201
    13
recdef merge "measure(%(xs,ys). size xs + size ys)"
nipkow@13201
    14
"merge(x#xs,y#ys) =
nipkow@13201
    15
 (if x <= y then x # merge(xs,y#ys) else y # merge(x#xs,ys))"
nipkow@13201
    16
"merge(xs,[]) = xs"
nipkow@13201
    17
"merge([],ys) = ys"
nipkow@13201
    18
kleing@15631
    19
lemma [simp]: "multiset_of (merge(xs,ys)) = multiset_of xs + multiset_of ys"
nipkow@13201
    20
apply(induct xs ys rule: merge.induct)
kleing@15631
    21
apply (auto simp: union_ac)
nipkow@13201
    22
done
nipkow@13201
    23
nipkow@13201
    24
lemma [simp]: "set(merge(xs,ys)) = set xs \<union> set ys"
nipkow@13201
    25
apply(induct xs ys rule: merge.induct)
nipkow@13201
    26
apply auto
nipkow@13201
    27
done
nipkow@13201
    28
nipkow@13201
    29
lemma [simp]:
nipkow@13201
    30
 "sorted (op <=) (merge(xs,ys)) = (sorted (op <=) xs & sorted (op <=) ys)"
nipkow@13201
    31
apply(induct xs ys rule: merge.induct)
nipkow@13201
    32
apply(simp_all add:ball_Un linorder_not_le order_less_le)
nipkow@13201
    33
apply(blast intro: order_trans)
nipkow@13201
    34
done
nipkow@13201
    35
nipkow@13201
    36
consts msort :: "('a::linorder) list \<Rightarrow> 'a list"
nipkow@13201
    37
recdef msort "measure size"
nipkow@13201
    38
"msort [] = []"
nipkow@13201
    39
"msort [x] = [x]"
nipkow@13201
    40
"msort xs = merge(msort(take (size xs div 2) xs),
nipkow@13201
    41
                  msort(drop (size xs div 2) xs))"
nipkow@13201
    42
nipkow@13201
    43
lemma "sorted op <= (msort xs)"
nipkow@13201
    44
by (induct xs rule: msort.induct) simp_all
nipkow@13201
    45
kleing@15631
    46
lemma "multiset_of (msort xs) = multiset_of xs"
nipkow@13201
    47
apply (induct xs rule: msort.induct)
nipkow@13201
    48
  apply simp
nipkow@13201
    49
 apply simp
kleing@15631
    50
apply simp
kleing@15631
    51
apply (subst union_commute)
kleing@15631
    52
apply (simp del:multiset_of_append add:multiset_of_append[symmetric] union_assoc)
kleing@15631
    53
apply (simp add: union_ac)
nipkow@13201
    54
done
nipkow@13201
    55
nipkow@13201
    56
end