summary |
shortlog |
changelog |
graph |
tags |
branches |
files |
changeset |
file |
revisions |
annotate |
diff |
raw

src/HOL/ex/MergeSort.thy

author | wenzelm |

Mon Aug 31 21:28:08 2015 +0200 (2015-08-31) | |

changeset 61070 | b72a990adfe2 |

parent 60515 | 484559628038 |

child 61343 | 5b5656a63bd6 |

permissions | -rw-r--r-- |

prefer symbols;

1 (* Title: HOL/ex/MergeSort.thy

2 Author: Tobias Nipkow

3 Copyright 2002 TU Muenchen

4 *)

6 section{*Merge Sort*}

8 theory MergeSort

9 imports "~~/src/HOL/Library/Multiset"

10 begin

12 context linorder

13 begin

15 fun merge :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"

16 where

17 "merge (x#xs) (y#ys) =

18 (if x \<le> y then x # merge xs (y#ys) else y # merge (x#xs) ys)"

19 | "merge xs [] = xs"

20 | "merge [] ys = ys"

22 lemma mset_merge [simp]:

23 "mset (merge xs ys) = mset xs + mset ys"

24 by (induct xs ys rule: merge.induct) (simp_all add: ac_simps)

26 lemma set_merge [simp]:

27 "set (merge xs ys) = set xs \<union> set ys"

28 by (induct xs ys rule: merge.induct) auto

30 lemma sorted_merge [simp]:

31 "sorted (merge xs ys) \<longleftrightarrow> sorted xs \<and> sorted ys"

32 by (induct xs ys rule: merge.induct) (auto simp add: ball_Un not_le less_le sorted_Cons)

34 fun msort :: "'a list \<Rightarrow> 'a list"

35 where

36 "msort [] = []"

37 | "msort [x] = [x]"

38 | "msort xs = merge (msort (take (size xs div 2) xs))

39 (msort (drop (size xs div 2) xs))"

41 lemma sorted_msort:

42 "sorted (msort xs)"

43 by (induct xs rule: msort.induct) simp_all

45 lemma mset_msort:

46 "mset (msort xs) = mset xs"

47 by (induct xs rule: msort.induct)

48 (simp_all, metis append_take_drop_id drop_Suc_Cons mset.simps(2) mset_append take_Suc_Cons)

50 theorem msort_sort:

51 "sort = msort"

52 by (rule ext, rule properties_for_sort) (fact mset_msort sorted_msort)+

54 end

56 end