src/HOL/Data_Structures/Sorting.thy
author nipkow
Fri, 04 May 2018 15:59:21 +0200
changeset 68078 48e188cb1591
parent 67983 487685540a51
child 68079 9c2088adff8b
permissions -rw-r--r--
tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
     1
(* Author: Tobias Nipkow *)
a90dbf19f573 new file
nipkow
parents:
diff changeset
     2
a90dbf19f573 new file
nipkow
parents:
diff changeset
     3
(* FIXME adjust List.sorted to work like below; [code] is different! *)
a90dbf19f573 new file
nipkow
parents:
diff changeset
     4
a90dbf19f573 new file
nipkow
parents:
diff changeset
     5
theory Sorting
a90dbf19f573 new file
nipkow
parents:
diff changeset
     6
imports
a90dbf19f573 new file
nipkow
parents:
diff changeset
     7
  Complex_Main
a90dbf19f573 new file
nipkow
parents:
diff changeset
     8
  "HOL-Library.Multiset"
a90dbf19f573 new file
nipkow
parents:
diff changeset
     9
begin
a90dbf19f573 new file
nipkow
parents:
diff changeset
    10
a90dbf19f573 new file
nipkow
parents:
diff changeset
    11
hide_const List.sorted List.insort
a90dbf19f573 new file
nipkow
parents:
diff changeset
    12
a90dbf19f573 new file
nipkow
parents:
diff changeset
    13
declare Let_def [simp]
a90dbf19f573 new file
nipkow
parents:
diff changeset
    14
a90dbf19f573 new file
nipkow
parents:
diff changeset
    15
fun sorted :: "'a::linorder list \<Rightarrow> bool" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
    16
"sorted [] = True" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
    17
"sorted (x # xs) = ((\<forall>y\<in>set xs. x \<le> y) & sorted xs)"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    18
a90dbf19f573 new file
nipkow
parents:
diff changeset
    19
lemma sorted_append:
a90dbf19f573 new file
nipkow
parents:
diff changeset
    20
  "sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    21
by (induct xs) (auto)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    22
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
    23
lemma sorted01: "length xs \<le> 1 \<Longrightarrow> sorted xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
    24
by(auto simp: le_Suc_eq length_Suc_conv)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
    25
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    26
a90dbf19f573 new file
nipkow
parents:
diff changeset
    27
subsection "Insertion Sort"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    28
a90dbf19f573 new file
nipkow
parents:
diff changeset
    29
fun insort :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> 'a list" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
    30
"insort x [] = [x]" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
    31
"insort x (y#ys) =
a90dbf19f573 new file
nipkow
parents:
diff changeset
    32
  (if x \<le> y then x#y#ys else y#(insort x ys))"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    33
a90dbf19f573 new file
nipkow
parents:
diff changeset
    34
fun isort :: "'a::linorder list \<Rightarrow> 'a list" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
    35
"isort [] = []" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
    36
"isort (x#xs) = insort x (isort xs)"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    37
68078
nipkow
parents: 67983
diff changeset
    38
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    39
subsubsection "Functional Correctness"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    40
a90dbf19f573 new file
nipkow
parents:
diff changeset
    41
lemma mset_insort: "mset (insort x xs) = add_mset x (mset xs)"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    42
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    43
apply auto
a90dbf19f573 new file
nipkow
parents:
diff changeset
    44
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    45
a90dbf19f573 new file
nipkow
parents:
diff changeset
    46
lemma mset_isort: "mset (isort xs) = mset xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    47
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    48
apply simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
    49
apply (simp add: mset_insort)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    50
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    51
a90dbf19f573 new file
nipkow
parents:
diff changeset
    52
lemma set_insort: "set (insort x xs) = insert x (set xs)"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    53
by (metis mset_insort set_mset_add_mset_insert set_mset_mset)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    54
a90dbf19f573 new file
nipkow
parents:
diff changeset
    55
lemma set_isort: "set (isort xs) = set xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    56
by (metis mset_isort set_mset_mset)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    57
a90dbf19f573 new file
nipkow
parents:
diff changeset
    58
lemma sorted_insort: "sorted (insort a xs) = sorted xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    59
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    60
apply(auto simp add: set_insort)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    61
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    62
a90dbf19f573 new file
nipkow
parents:
diff changeset
    63
lemma "sorted (isort xs)"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    64
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    65
apply(auto simp: sorted_insort)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    66
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    67
68078
nipkow
parents: 67983
diff changeset
    68
nipkow
parents: 67983
diff changeset
    69
subsubsection "Time Complexity"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    70
a90dbf19f573 new file
nipkow
parents:
diff changeset
    71
text \<open>We count the number of function calls.\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    72
a90dbf19f573 new file
nipkow
parents:
diff changeset
    73
text\<open>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    74
\<open>insort x [] = [x]\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    75
\<open>insort x (y#ys) =
a90dbf19f573 new file
nipkow
parents:
diff changeset
    76
  (if x \<le> y then x#y#ys else y#(insort x ys))\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    77
\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    78
fun t_insort :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> nat" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
    79
"t_insort x [] = 1" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
    80
"t_insort x (y#ys) =
a90dbf19f573 new file
nipkow
parents:
diff changeset
    81
  (if x \<le> y then 0 else t_insort x ys) + 1"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    82
a90dbf19f573 new file
nipkow
parents:
diff changeset
    83
text\<open>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    84
\<open>isort [] = []\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    85
\<open>isort (x#xs) = insort x (isort xs)\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    86
\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    87
fun t_isort :: "'a::linorder list \<Rightarrow> nat" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
    88
"t_isort [] = 1" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
    89
"t_isort (x#xs) = t_isort xs + t_insort x (isort xs) + 1"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    90
a90dbf19f573 new file
nipkow
parents:
diff changeset
    91
a90dbf19f573 new file
nipkow
parents:
diff changeset
    92
lemma t_insort_length: "t_insort x xs \<le> length xs + 1"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    93
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    94
apply auto
a90dbf19f573 new file
nipkow
parents:
diff changeset
    95
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    96
a90dbf19f573 new file
nipkow
parents:
diff changeset
    97
lemma length_insort: "length (insort x xs) = length xs + 1"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    98
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    99
apply auto
a90dbf19f573 new file
nipkow
parents:
diff changeset
   100
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
   101
a90dbf19f573 new file
nipkow
parents:
diff changeset
   102
lemma length_isort: "length (isort xs) = length xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   103
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   104
apply (auto simp: length_insort)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   105
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
   106
a90dbf19f573 new file
nipkow
parents:
diff changeset
   107
lemma t_isort_length: "t_isort xs \<le> (length xs + 1) ^ 2"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   108
proof(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   109
  case Nil show ?case by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   110
next
a90dbf19f573 new file
nipkow
parents:
diff changeset
   111
  case (Cons x xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   112
  have "t_isort (x#xs) = t_isort xs + t_insort x (isort xs) + 1" by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   113
  also have "\<dots> \<le> (length xs + 1) ^ 2 + t_insort x (isort xs) + 1"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   114
    using Cons.IH by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   115
  also have "\<dots> \<le> (length xs + 1) ^ 2 + length xs + 1 + 1"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   116
    using t_insort_length[of x "isort xs"] by (simp add: length_isort)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   117
  also have "\<dots> \<le> (length(x#xs) + 1) ^ 2"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   118
    by (simp add: power2_eq_square)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   119
  finally show ?case .
a90dbf19f573 new file
nipkow
parents:
diff changeset
   120
qed
a90dbf19f573 new file
nipkow
parents:
diff changeset
   121
a90dbf19f573 new file
nipkow
parents:
diff changeset
   122
a90dbf19f573 new file
nipkow
parents:
diff changeset
   123
subsection "Merge Sort"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   124
a90dbf19f573 new file
nipkow
parents:
diff changeset
   125
fun merge :: "'a::linorder list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
   126
"merge [] ys = ys" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
   127
"merge xs [] = xs" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
   128
"merge (x#xs) (y#ys) = (if x \<le> y then x # merge xs (y#ys) else y # merge (x#xs) ys)"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   129
a90dbf19f573 new file
nipkow
parents:
diff changeset
   130
fun msort :: "'a::linorder list \<Rightarrow> 'a list" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
   131
"msort xs = (let n = length xs in
a90dbf19f573 new file
nipkow
parents:
diff changeset
   132
  if n \<le> 1 then xs
a90dbf19f573 new file
nipkow
parents:
diff changeset
   133
  else merge (msort (take (n div 2) xs)) (msort (drop (n div 2) xs)))"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   134
a90dbf19f573 new file
nipkow
parents:
diff changeset
   135
declare msort.simps [simp del]
a90dbf19f573 new file
nipkow
parents:
diff changeset
   136
68078
nipkow
parents: 67983
diff changeset
   137
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   138
subsubsection "Functional Correctness"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   139
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   140
lemma mset_merge: "mset(merge xs ys) = mset xs + mset ys"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   141
by(induction xs ys rule: merge.induct) auto
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   142
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   143
lemma "mset (msort xs) = mset xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   144
proof(induction xs rule: msort.induct)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   145
  case (1 xs)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   146
  let ?n = "length xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   147
  let ?xs1 = "take (?n div 2) xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   148
  let ?xs2 = "drop (?n div 2) xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   149
  show ?case
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   150
  proof cases
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   151
    assume "?n \<le> 1"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   152
    thus ?thesis by(simp add: msort.simps[of xs])
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   153
  next
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   154
    assume "\<not> ?n \<le> 1"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   155
    hence "mset (msort xs) = mset (msort ?xs1) + mset (msort ?xs2)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   156
      by(simp add: msort.simps[of xs] mset_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   157
    also have "\<dots> = mset ?xs1 + mset ?xs2"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   158
      using \<open>\<not> ?n \<le> 1\<close> by(simp add: "1.IH")
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   159
    also have "\<dots> = mset (?xs1 @ ?xs2)" by (simp del: append_take_drop_id)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   160
    also have "\<dots> = mset xs" by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   161
    finally show ?thesis .
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   162
  qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   163
qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   164
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   165
lemma set_merge: "set(merge xs ys) = set xs \<union> set ys"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   166
by(induction xs ys rule: merge.induct) (auto)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   167
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   168
lemma sorted_merge: "sorted (merge xs ys) \<longleftrightarrow> (sorted xs \<and> sorted ys)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   169
by(induction xs ys rule: merge.induct) (auto simp: set_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   170
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   171
lemma "sorted (msort xs)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   172
proof(induction xs rule: msort.induct)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   173
  case (1 xs)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   174
  let ?n = "length xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   175
  show ?case
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   176
  proof cases
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   177
    assume "?n \<le> 1"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   178
    thus ?thesis by(simp add: msort.simps[of xs] sorted01)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   179
  next
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   180
    assume "\<not> ?n \<le> 1"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   181
    thus ?thesis using "1.IH"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   182
      by(simp add: sorted_merge  msort.simps[of xs] mset_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   183
  qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   184
qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   185
68078
nipkow
parents: 67983
diff changeset
   186
nipkow
parents: 67983
diff changeset
   187
subsubsection "Time Complexity"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   188
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   189
text \<open>We only count the number of comparisons between list elements.\<close>
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   190
a90dbf19f573 new file
nipkow
parents:
diff changeset
   191
fun c_merge :: "'a::linorder list \<Rightarrow> 'a list \<Rightarrow> nat" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
   192
"c_merge [] ys = 0" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
   193
"c_merge xs [] = 0" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
   194
"c_merge (x#xs) (y#ys) = 1 + (if x \<le> y then c_merge xs (y#ys) else c_merge (x#xs) ys)"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   195
a90dbf19f573 new file
nipkow
parents:
diff changeset
   196
lemma c_merge_ub: "c_merge xs ys \<le> length xs + length ys"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   197
by (induction xs ys rule: c_merge.induct) auto
a90dbf19f573 new file
nipkow
parents:
diff changeset
   198
a90dbf19f573 new file
nipkow
parents:
diff changeset
   199
fun c_msort :: "'a::linorder list \<Rightarrow> nat" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
   200
"c_msort xs =
a90dbf19f573 new file
nipkow
parents:
diff changeset
   201
  (let n = length xs;
a90dbf19f573 new file
nipkow
parents:
diff changeset
   202
       ys = take (n div 2) xs;
a90dbf19f573 new file
nipkow
parents:
diff changeset
   203
       zs = drop (n div 2) xs
a90dbf19f573 new file
nipkow
parents:
diff changeset
   204
   in if n \<le> 1 then 0
a90dbf19f573 new file
nipkow
parents:
diff changeset
   205
      else c_msort ys + c_msort zs + c_merge (msort ys) (msort zs))"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   206
a90dbf19f573 new file
nipkow
parents:
diff changeset
   207
declare c_msort.simps [simp del]
a90dbf19f573 new file
nipkow
parents:
diff changeset
   208
a90dbf19f573 new file
nipkow
parents:
diff changeset
   209
lemma length_merge: "length(merge xs ys) = length xs + length ys"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   210
apply (induction xs ys rule: merge.induct)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   211
apply auto
a90dbf19f573 new file
nipkow
parents:
diff changeset
   212
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
   213
a90dbf19f573 new file
nipkow
parents:
diff changeset
   214
lemma length_msort: "length(msort xs) = length xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   215
proof (induction xs rule: msort.induct)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   216
  case (1 xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   217
  thus ?case by (auto simp: msort.simps[of xs] length_merge)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   218
qed
a90dbf19f573 new file
nipkow
parents:
diff changeset
   219
text \<open>Why structured proof?
a90dbf19f573 new file
nipkow
parents:
diff changeset
   220
   To have the name "xs" to specialize msort.simps with xs
a90dbf19f573 new file
nipkow
parents:
diff changeset
   221
   to ensure that msort.simps cannot be used recursively.
a90dbf19f573 new file
nipkow
parents:
diff changeset
   222
Also works without this precaution, but that is just luck.\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
   223
a90dbf19f573 new file
nipkow
parents:
diff changeset
   224
lemma c_msort_le: "length xs = 2^k \<Longrightarrow> c_msort xs \<le> k * 2^k"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   225
proof(induction k arbitrary: xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   226
  case 0 thus ?case by (simp add: c_msort.simps)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   227
next
a90dbf19f573 new file
nipkow
parents:
diff changeset
   228
  case (Suc k)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   229
  let ?n = "length xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   230
  let ?ys = "take (?n div 2) xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   231
  let ?zs = "drop (?n div 2) xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   232
  show ?case
a90dbf19f573 new file
nipkow
parents:
diff changeset
   233
  proof (cases "?n \<le> 1")
a90dbf19f573 new file
nipkow
parents:
diff changeset
   234
    case True
a90dbf19f573 new file
nipkow
parents:
diff changeset
   235
    thus ?thesis by(simp add: c_msort.simps)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   236
  next
a90dbf19f573 new file
nipkow
parents:
diff changeset
   237
    case False
a90dbf19f573 new file
nipkow
parents:
diff changeset
   238
    have "c_msort(xs) =
a90dbf19f573 new file
nipkow
parents:
diff changeset
   239
      c_msort ?ys + c_msort ?zs + c_merge (msort ?ys) (msort ?zs)"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   240
      by (simp add: c_msort.simps msort.simps)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   241
    also have "\<dots> \<le> c_msort ?ys + c_msort ?zs + length ?ys + length ?zs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   242
      using c_merge_ub[of "msort ?ys" "msort ?zs"] length_msort[of ?ys] length_msort[of ?zs]
a90dbf19f573 new file
nipkow
parents:
diff changeset
   243
      by arith
a90dbf19f573 new file
nipkow
parents:
diff changeset
   244
    also have "\<dots> \<le> k * 2^k + c_msort ?zs + length ?ys + length ?zs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   245
      using Suc.IH[of ?ys] Suc.prems by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   246
    also have "\<dots> \<le> k * 2^k + k * 2^k + length ?ys + length ?zs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   247
      using Suc.IH[of ?zs] Suc.prems by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   248
    also have "\<dots> = 2 * k * 2^k + 2 * 2 ^ k"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   249
      using Suc.prems by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   250
    finally show ?thesis by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   251
  qed
a90dbf19f573 new file
nipkow
parents:
diff changeset
   252
qed
a90dbf19f573 new file
nipkow
parents:
diff changeset
   253
a90dbf19f573 new file
nipkow
parents:
diff changeset
   254
(* Beware of conversions: *)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   255
lemma "length xs = 2^k \<Longrightarrow> c_msort xs \<le> length xs * log 2 (length xs)"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   256
using c_msort_le[of xs k] apply (simp add: log_nat_power algebra_simps)
66912
a99a7cbf0fb5 generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
immler
parents: 66543
diff changeset
   257
by (metis (mono_tags) numeral_power_eq_of_nat_cancel_iff of_nat_le_iff of_nat_mult)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   258
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   259
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   260
subsection "Bottom-Up Merge Sort"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   261
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   262
(* Exercise: make tail recursive *)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   263
fun merge_adj :: "('a::linorder) list list \<Rightarrow> 'a list list" where
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   264
"merge_adj [] = []" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   265
"merge_adj [xs] = [xs]" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   266
"merge_adj (xs # ys # zss) = merge xs ys # merge_adj zss"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   267
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   268
text \<open>For the termination proof of \<open>merge_all\<close> below.\<close>
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   269
lemma length_merge_adjacent[simp]: "length (merge_adj xs) = (length xs + 1) div 2"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   270
by (induction xs rule: merge_adj.induct) auto
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   271
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   272
fun merge_all :: "('a::linorder) list list \<Rightarrow> 'a list" where
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   273
"merge_all [] = undefined" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   274
"merge_all [xs] = xs" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   275
"merge_all xss = merge_all (merge_adj xss)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   276
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   277
definition msort_bu :: "('a::linorder) list \<Rightarrow> 'a list" where
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   278
"msort_bu xs = (if xs = [] then [] else merge_all (map (\<lambda>x. [x]) xs))"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   279
68078
nipkow
parents: 67983
diff changeset
   280
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   281
subsubsection "Functional Correctness"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   282
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   283
lemma mset_merge_adj:
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   284
  "\<Union># image_mset mset (mset (merge_adj xss)) = \<Union># image_mset mset (mset xss)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   285
by(induction xss rule: merge_adj.induct) (auto simp: mset_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   286
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   287
lemma msec_merge_all:
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   288
  "xss \<noteq> [] \<Longrightarrow> mset (merge_all xss) = (\<Union># (mset (map mset xss)))"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   289
by(induction xss rule: merge_all.induct) (auto simp: mset_merge mset_merge_adj)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   290
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   291
lemma sorted_merge_adj:
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   292
  "\<forall>xs \<in> set xss. sorted xs \<Longrightarrow> \<forall>xs \<in> set (merge_adj xss). sorted xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   293
by(induction xss rule: merge_adj.induct) (auto simp: sorted_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   294
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   295
lemma sorted_merge_all:
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   296
  "\<forall>xs \<in> set xss. sorted xs \<Longrightarrow> xss \<noteq> [] \<Longrightarrow> sorted (merge_all xss)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   297
apply(induction xss rule: merge_all.induct)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   298
using [[simp_depth_limit=3]] by (auto simp add: sorted_merge_adj)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   299
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   300
lemma sorted_msort_bu: "sorted (msort_bu xs)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   301
by(simp add: msort_bu_def sorted_merge_all)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   302
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   303
lemma mset_msort: "mset (msort_bu xs) = mset xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   304
by(simp add: msort_bu_def msec_merge_all comp_def)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   305
68078
nipkow
parents: 67983
diff changeset
   306
nipkow
parents: 67983
diff changeset
   307
subsubsection "Time Complexity"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   308
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   309
fun c_merge_adj :: "('a::linorder) list list \<Rightarrow> real" where
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   310
"c_merge_adj [] = 0" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   311
"c_merge_adj [x] = 0" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   312
"c_merge_adj (x # y # zs) = c_merge x y + c_merge_adj zs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   313
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   314
fun c_merge_all :: "('a::linorder) list list \<Rightarrow> real" where
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   315
"c_merge_all [] = 0" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   316
"c_merge_all [x] = 0" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   317
"c_merge_all xs = c_merge_adj xs + c_merge_all (merge_adj xs)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   318
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   319
definition c_msort_bu :: "('a::linorder) list \<Rightarrow> real" where
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   320
"c_msort_bu xs = (if xs = [] then 0 else c_merge_all (map (\<lambda>x. [x]) xs))"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   321
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   322
lemma length_merge_adj:
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   323
  "\<lbrakk> even(length xs); \<forall>x \<in> set xs. length x = m \<rbrakk> \<Longrightarrow> \<forall>x \<in> set (merge_adj xs). length x = 2*m"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   324
by(induction xs rule: merge_adj.induct) (auto simp: length_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   325
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   326
lemma c_merge_adj: "\<forall>x \<in> set xs. length x = m \<Longrightarrow> c_merge_adj xs \<le> m * length xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   327
proof(induction xs rule: c_merge_adj.induct)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   328
  case 1 thus ?case by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   329
next
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   330
  case 2 thus ?case by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   331
next
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   332
  case (3 x y) thus ?case using c_merge_ub[of x y] by (simp add: algebra_simps)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   333
qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   334
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   335
lemma c_merge_all: "\<lbrakk> \<forall>x \<in> set xs. length x = m; length xs = 2^k \<rbrakk>
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   336
  \<Longrightarrow> c_merge_all xs \<le> m * k * 2^k"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   337
proof (induction xs arbitrary: k m rule: c_merge_all.induct)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   338
  case 1 thus ?case by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   339
next
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   340
  case (2 x)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   341
  then show ?case by (simp)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   342
next
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   343
  case (3 x y xs)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   344
  let ?xs = "x # y # xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   345
  let ?xs2 = "merge_adj ?xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   346
  obtain k' where k': "k = Suc k'" using "3.prems"(2)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   347
    by (metis length_Cons nat.inject nat_power_eq_Suc_0_iff nat.exhaust)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   348
  have "even (length xs)" using "3.prems"(2) even_Suc_Suc_iff by fastforce
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   349
  from "3.prems"(1) length_merge_adj[OF this]
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   350
  have 2: "\<forall>x \<in> set(merge_adj ?xs). length x = 2*m" by(auto simp: length_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   351
  have 3: "length ?xs2 = 2 ^ k'" using "3.prems"(2) k' by auto
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   352
  have 4: "length ?xs div 2 = 2 ^ k'"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   353
    using "3.prems"(2) k' by(simp add: power_eq_if[of 2 k] split: if_splits)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   354
  have "c_merge_all ?xs = c_merge_adj ?xs + c_merge_all ?xs2" by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   355
  also have "\<dots> \<le> m * 2^k + c_merge_all ?xs2"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   356
    using "3.prems"(2) c_merge_adj[OF "3.prems"(1)] by (auto simp: algebra_simps)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   357
  also have "\<dots> \<le> m * 2^k + (2*m) * k' * 2^k'"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   358
    using "3.IH"[OF 2 3] by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   359
  also have "\<dots> = m * k * 2^k"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   360
    using k' by (simp add: algebra_simps)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   361
  finally show ?case .
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   362
qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   363
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   364
corollary c_msort_bu: "length xs = 2 ^ k \<Longrightarrow> c_msort_bu xs \<le> k * 2 ^ k"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   365
using c_merge_all[of "map (\<lambda>x. [x]) xs" 1] by (simp add: c_msort_bu_def)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   366
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   367
end