src/HOL/Data_Structures/Sorting.thy
author wenzelm
Thu, 08 Dec 2022 11:24:43 +0100
changeset 76598 9f97eda3fcf1
parent 75501 426afab39a55
child 77922 d28dcd57d2f3
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
70250
20d819b0a29d New version of tries
nipkow
parents: 69597
diff changeset
     3
section "Sorting"
20d819b0a29d New version of tries
nipkow
parents: 69597
diff changeset
     4
66543
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
68160
efce008331f6 mv lemma
nipkow
parents: 68159
diff changeset
    11
hide_const List.insort
66543
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
a90dbf19f573 new file
nipkow
parents:
diff changeset
    16
subsection "Insertion Sort"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    17
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    18
fun insort1 :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> 'a list" where
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    19
"insort1 x [] = [x]" |
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    20
"insort1 x (y#ys) =
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    21
  (if x \<le> y then x#y#ys else y#(insort1 x ys))"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    22
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    23
fun insort :: "'a::linorder list \<Rightarrow> 'a list" where
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    24
"insort [] = []" |
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    25
"insort (x#xs) = insort1 x (insort xs)"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    26
68078
nipkow
parents: 67983
diff changeset
    27
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    28
subsubsection "Functional Correctness"
a90dbf19f573 new file
nipkow
parents:
diff changeset
    29
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    30
lemma mset_insort1: "mset (insort1 x xs) = {#x#} + mset xs"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    31
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    32
apply auto
a90dbf19f573 new file
nipkow
parents:
diff changeset
    33
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    34
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    35
lemma mset_insort: "mset (insort xs) = mset xs"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    36
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    37
apply simp
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    38
apply (simp add: mset_insort1)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    39
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    40
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    41
lemma set_insort1: "set (insort1 x xs) = {x} \<union> set xs"
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    42
by(simp add: mset_insort1 flip: set_mset_mset)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    43
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    44
lemma sorted_insort1: "sorted (insort1 a xs) = sorted xs"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    45
apply(induction xs)
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    46
apply(auto simp add: set_insort1)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    47
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    48
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    49
lemma sorted_insort: "sorted (insort xs)"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    50
apply(induction xs)
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    51
apply(auto simp: sorted_insort1)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    52
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    53
68078
nipkow
parents: 67983
diff changeset
    54
nipkow
parents: 67983
diff changeset
    55
subsubsection "Time Complexity"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    56
a90dbf19f573 new file
nipkow
parents:
diff changeset
    57
text \<open>We count the number of function calls.\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
    58
a90dbf19f573 new file
nipkow
parents:
diff changeset
    59
text\<open>
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    60
\<open>insort1 x [] = [x]\<close>
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    61
\<open>insort1 x (y#ys) =
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    62
  (if x \<le> y then x#y#ys else y#(insort1 x ys))\<close>
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    63
\<close>
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    64
fun T_insort1 :: "'a::linorder \<Rightarrow> 'a list \<Rightarrow> nat" where
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    65
"T_insort1 x [] = 1" |
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    66
"T_insort1 x (y#ys) =
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    67
  (if x \<le> y then 0 else T_insort1 x ys) + 1"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    68
a90dbf19f573 new file
nipkow
parents:
diff changeset
    69
text\<open>
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    70
\<open>insort [] = []\<close>
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    71
\<open>insort (x#xs) = insort1 x (insort xs)\<close>
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    72
\<close>
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    73
fun T_insort :: "'a::linorder list \<Rightarrow> nat" where
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    74
"T_insort [] = 1" |
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    75
"T_insort (x#xs) = T_insort xs + T_insort1 x (insort xs) + 1"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    76
a90dbf19f573 new file
nipkow
parents:
diff changeset
    77
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    78
lemma T_insort1_length: "T_insort1 x xs \<le> length xs + 1"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    79
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    80
apply auto
a90dbf19f573 new file
nipkow
parents:
diff changeset
    81
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    82
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    83
lemma length_insort1: "length (insort1 x xs) = length xs + 1"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    84
apply(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    85
apply auto
a90dbf19f573 new file
nipkow
parents:
diff changeset
    86
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    87
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    88
lemma length_insort: "length (insort xs) = length xs"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    89
apply(induction xs)
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    90
apply (auto simp: length_insort1)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    91
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
    92
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    93
lemma T_insort_length: "T_insort xs \<le> (length xs + 1) ^ 2"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
    94
proof(induction xs)
a90dbf19f573 new file
nipkow
parents:
diff changeset
    95
  case Nil show ?case by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
    96
next
a90dbf19f573 new file
nipkow
parents:
diff changeset
    97
  case (Cons x xs)
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    98
  have "T_insort (x#xs) = T_insort xs + T_insort1 x (insort xs) + 1" by simp
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
    99
  also have "\<dots> \<le> (length xs + 1) ^ 2 + T_insort1 x (insort xs) + 1"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   100
    using Cons.IH by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   101
  also have "\<dots> \<le> (length xs + 1) ^ 2 + length xs + 1 + 1"
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   102
    using T_insort1_length[of x "insort xs"] by (simp add: length_insort)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   103
  also have "\<dots> \<le> (length(x#xs) + 1) ^ 2"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   104
    by (simp add: power2_eq_square)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   105
  finally show ?case .
a90dbf19f573 new file
nipkow
parents:
diff changeset
   106
qed
a90dbf19f573 new file
nipkow
parents:
diff changeset
   107
a90dbf19f573 new file
nipkow
parents:
diff changeset
   108
a90dbf19f573 new file
nipkow
parents:
diff changeset
   109
subsection "Merge Sort"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   110
a90dbf19f573 new file
nipkow
parents:
diff changeset
   111
fun merge :: "'a::linorder list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
   112
"merge [] ys = ys" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
   113
"merge xs [] = xs" |
a90dbf19f573 new file
nipkow
parents:
diff changeset
   114
"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
   115
a90dbf19f573 new file
nipkow
parents:
diff changeset
   116
fun msort :: "'a::linorder list \<Rightarrow> 'a list" where
a90dbf19f573 new file
nipkow
parents:
diff changeset
   117
"msort xs = (let n = length xs in
a90dbf19f573 new file
nipkow
parents:
diff changeset
   118
  if n \<le> 1 then xs
a90dbf19f573 new file
nipkow
parents:
diff changeset
   119
  else merge (msort (take (n div 2) xs)) (msort (drop (n div 2) xs)))"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   120
a90dbf19f573 new file
nipkow
parents:
diff changeset
   121
declare msort.simps [simp del]
a90dbf19f573 new file
nipkow
parents:
diff changeset
   122
68078
nipkow
parents: 67983
diff changeset
   123
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   124
subsubsection "Functional Correctness"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   125
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   126
lemma mset_merge: "mset(merge xs ys) = mset xs + mset ys"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   127
by(induction xs ys rule: merge.induct) auto
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   128
68963
nipkow
parents: 68934
diff changeset
   129
lemma mset_msort: "mset (msort xs) = mset xs"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   130
proof(induction xs rule: msort.induct)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   131
  case (1 xs)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   132
  let ?n = "length xs"
68966
nipkow
parents: 68963
diff changeset
   133
  let ?ys = "take (?n div 2) xs"
nipkow
parents: 68963
diff changeset
   134
  let ?zs = "drop (?n div 2) xs"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   135
  show ?case
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   136
  proof cases
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   137
    assume "?n \<le> 1"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   138
    thus ?thesis by(simp add: msort.simps[of xs])
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   139
  next
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   140
    assume "\<not> ?n \<le> 1"
68966
nipkow
parents: 68963
diff changeset
   141
    hence "mset (msort xs) = mset (msort ?ys) + mset (msort ?zs)"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   142
      by(simp add: msort.simps[of xs] mset_merge)
68966
nipkow
parents: 68963
diff changeset
   143
    also have "\<dots> = mset ?ys + mset ?zs"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   144
      using \<open>\<not> ?n \<le> 1\<close> by(simp add: "1.IH")
68966
nipkow
parents: 68963
diff changeset
   145
    also have "\<dots> = mset (?ys @ ?zs)" by (simp del: append_take_drop_id)
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   146
    also have "\<dots> = mset xs" by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   147
    finally show ?thesis .
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   148
  qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   149
qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   150
68966
nipkow
parents: 68963
diff changeset
   151
text \<open>Via the previous lemma or directly:\<close>
68963
nipkow
parents: 68934
diff changeset
   152
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   153
lemma set_merge: "set(merge xs ys) = set xs \<union> set ys"
68963
nipkow
parents: 68934
diff changeset
   154
by (metis mset_merge set_mset_mset set_mset_union)
nipkow
parents: 68934
diff changeset
   155
nipkow
parents: 68934
diff changeset
   156
lemma "set(merge xs ys) = set xs \<union> set ys"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   157
by(induction xs ys rule: merge.induct) (auto)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   158
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   159
lemma sorted_merge: "sorted (merge xs ys) \<longleftrightarrow> (sorted xs \<and> sorted ys)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   160
by(induction xs ys rule: merge.induct) (auto simp: set_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   161
68966
nipkow
parents: 68963
diff changeset
   162
lemma sorted_msort: "sorted (msort xs)"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   163
proof(induction xs rule: msort.induct)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   164
  case (1 xs)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   165
  let ?n = "length xs"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   166
  show ?case
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   167
  proof cases
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   168
    assume "?n \<le> 1"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   169
    thus ?thesis by(simp add: msort.simps[of xs] sorted01)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   170
  next
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   171
    assume "\<not> ?n \<le> 1"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   172
    thus ?thesis using "1.IH"
68966
nipkow
parents: 68963
diff changeset
   173
      by(simp add: sorted_merge msort.simps[of xs])
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   174
  qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   175
qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   176
68078
nipkow
parents: 67983
diff changeset
   177
nipkow
parents: 67983
diff changeset
   178
subsubsection "Time Complexity"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   179
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   180
text \<open>We only count the number of comparisons between list elements.\<close>
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   181
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   182
fun C_merge :: "'a::linorder list \<Rightarrow> 'a list \<Rightarrow> nat" where
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   183
"C_merge [] ys = 0" |
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   184
"C_merge xs [] = 0" |
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   185
"C_merge (x#xs) (y#ys) = 1 + (if x \<le> y then C_merge xs (y#ys) else C_merge (x#xs) ys)"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   186
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   187
lemma C_merge_ub: "C_merge xs ys \<le> length xs + length ys"
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   188
by (induction xs ys rule: C_merge.induct) auto
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   189
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   190
fun C_msort :: "'a::linorder list \<Rightarrow> nat" where
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   191
"C_msort xs =
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   192
  (let n = length xs;
a90dbf19f573 new file
nipkow
parents:
diff changeset
   193
       ys = take (n div 2) xs;
a90dbf19f573 new file
nipkow
parents:
diff changeset
   194
       zs = drop (n div 2) xs
a90dbf19f573 new file
nipkow
parents:
diff changeset
   195
   in if n \<le> 1 then 0
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   196
      else C_msort ys + C_msort zs + C_merge (msort ys) (msort zs))"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   197
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   198
declare C_msort.simps [simp del]
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   199
a90dbf19f573 new file
nipkow
parents:
diff changeset
   200
lemma length_merge: "length(merge xs ys) = length xs + length ys"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   201
apply (induction xs ys rule: merge.induct)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   202
apply auto
a90dbf19f573 new file
nipkow
parents:
diff changeset
   203
done
a90dbf19f573 new file
nipkow
parents:
diff changeset
   204
a90dbf19f573 new file
nipkow
parents:
diff changeset
   205
lemma length_msort: "length(msort xs) = length xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   206
proof (induction xs rule: msort.induct)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   207
  case (1 xs)
71918
4e0a58818edc more simp rules
haftmann
parents: 70295
diff changeset
   208
  show ?case
4e0a58818edc more simp rules
haftmann
parents: 70295
diff changeset
   209
    by (auto simp: msort.simps [of xs] 1 length_merge)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   210
qed
a90dbf19f573 new file
nipkow
parents:
diff changeset
   211
text \<open>Why structured proof?
a90dbf19f573 new file
nipkow
parents:
diff changeset
   212
   To have the name "xs" to specialize msort.simps with xs
a90dbf19f573 new file
nipkow
parents:
diff changeset
   213
   to ensure that msort.simps cannot be used recursively.
a90dbf19f573 new file
nipkow
parents:
diff changeset
   214
Also works without this precaution, but that is just luck.\<close>
a90dbf19f573 new file
nipkow
parents:
diff changeset
   215
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   216
lemma C_msort_le: "length xs = 2^k \<Longrightarrow> C_msort xs \<le> k * 2^k"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   217
proof(induction k arbitrary: xs)
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   218
  case 0 thus ?case by (simp add: C_msort.simps)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   219
next
a90dbf19f573 new file
nipkow
parents:
diff changeset
   220
  case (Suc k)
a90dbf19f573 new file
nipkow
parents:
diff changeset
   221
  let ?n = "length xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   222
  let ?ys = "take (?n div 2) xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   223
  let ?zs = "drop (?n div 2) xs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   224
  show ?case
a90dbf19f573 new file
nipkow
parents:
diff changeset
   225
  proof (cases "?n \<le> 1")
a90dbf19f573 new file
nipkow
parents:
diff changeset
   226
    case True
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   227
    thus ?thesis by(simp add: C_msort.simps)
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   228
  next
a90dbf19f573 new file
nipkow
parents:
diff changeset
   229
    case False
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   230
    have "C_msort(xs) =
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   231
      C_msort ?ys + C_msort ?zs + C_merge (msort ?ys) (msort ?zs)"
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   232
      by (simp add: C_msort.simps msort.simps)
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   233
    also have "\<dots> \<le> C_msort ?ys + C_msort ?zs + length ?ys + length ?zs"
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   234
      using C_merge_ub[of "msort ?ys" "msort ?zs"] length_msort[of ?ys] length_msort[of ?zs]
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   235
      by arith
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   236
    also have "\<dots> \<le> k * 2^k + C_msort ?zs + length ?ys + length ?zs"
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   237
      using Suc.IH[of ?ys] Suc.prems by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   238
    also have "\<dots> \<le> k * 2^k + k * 2^k + length ?ys + length ?zs"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   239
      using Suc.IH[of ?zs] Suc.prems by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   240
    also have "\<dots> = 2 * k * 2^k + 2 * 2 ^ k"
a90dbf19f573 new file
nipkow
parents:
diff changeset
   241
      using Suc.prems by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   242
    finally show ?thesis by simp
a90dbf19f573 new file
nipkow
parents:
diff changeset
   243
  qed
a90dbf19f573 new file
nipkow
parents:
diff changeset
   244
qed
a90dbf19f573 new file
nipkow
parents:
diff changeset
   245
70295
nipkow
parents: 70250
diff changeset
   246
(* Beware of implicit conversions: *)
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   247
lemma C_msort_log: "length xs = 2^k \<Longrightarrow> C_msort xs \<le> length xs * log 2 (length xs)"
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   248
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
   249
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
   250
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   251
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   252
subsection "Bottom-Up Merge Sort"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   253
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   254
fun merge_adj :: "('a::linorder) list list \<Rightarrow> 'a list list" where
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   255
"merge_adj [] = []" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   256
"merge_adj [xs] = [xs]" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   257
"merge_adj (xs # ys # zss) = merge xs ys # merge_adj zss"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   258
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   259
text \<open>For the termination proof of \<open>merge_all\<close> below.\<close>
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   260
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
   261
by (induction xs rule: merge_adj.induct) auto
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   262
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   263
fun merge_all :: "('a::linorder) list list \<Rightarrow> 'a list" where
68970
b0dfe57dfa09 "undefined" not needed, [] is perfectly natural
nipkow
parents: 68968
diff changeset
   264
"merge_all [] = []" |
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   265
"merge_all [xs] = xs" |
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   266
"merge_all xss = merge_all (merge_adj xss)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   267
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   268
definition msort_bu :: "('a::linorder) list \<Rightarrow> 'a list" where
68971
938f4058c07c simplified defns
nipkow
parents: 68970
diff changeset
   269
"msort_bu xs = merge_all (map (\<lambda>x. [x]) xs)"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   270
68078
nipkow
parents: 67983
diff changeset
   271
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   272
subsubsection "Functional Correctness"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   273
72802
9bd2ed5e83f3 added abbrev
nipkow
parents: 72562
diff changeset
   274
abbreviation mset_mset :: "'a list list \<Rightarrow> 'a multiset" where
73047
ab9e27da0e85 HOL-Library: Changed notation for sum_mset
Manuel Eberl <eberlm@in.tum.de>
parents: 72802
diff changeset
   275
"mset_mset xss \<equiv> \<Sum>\<^sub># (image_mset mset (mset xss))"
72802
9bd2ed5e83f3 added abbrev
nipkow
parents: 72562
diff changeset
   276
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   277
lemma mset_merge_adj:
72802
9bd2ed5e83f3 added abbrev
nipkow
parents: 72562
diff changeset
   278
  "mset_mset (merge_adj xss) = mset_mset xss"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   279
by(induction xss rule: merge_adj.induct) (auto simp: mset_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   280
68967
nipkow
parents: 68966
diff changeset
   281
lemma mset_merge_all:
72802
9bd2ed5e83f3 added abbrev
nipkow
parents: 72562
diff changeset
   282
  "mset (merge_all xss) = mset_mset xss"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   283
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
   284
68968
nipkow
parents: 68967
diff changeset
   285
lemma mset_msort_bu: "mset (msort_bu xs) = mset xs"
72802
9bd2ed5e83f3 added abbrev
nipkow
parents: 72562
diff changeset
   286
by(simp add: msort_bu_def mset_merge_all multiset.map_comp comp_def)
68968
nipkow
parents: 68967
diff changeset
   287
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   288
lemma sorted_merge_adj:
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   289
  "\<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
   290
by(induction xss rule: merge_adj.induct) (auto simp: sorted_merge)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   291
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   292
lemma sorted_merge_all:
68971
938f4058c07c simplified defns
nipkow
parents: 68970
diff changeset
   293
  "\<forall>xs \<in> set xss. sorted xs \<Longrightarrow> sorted (merge_all xss)"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   294
apply(induction xss rule: merge_all.induct)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   295
using [[simp_depth_limit=3]] by (auto simp add: sorted_merge_adj)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   296
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   297
lemma sorted_msort_bu: "sorted (msort_bu xs)"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   298
by(simp add: msort_bu_def sorted_merge_all)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   299
68078
nipkow
parents: 67983
diff changeset
   300
nipkow
parents: 67983
diff changeset
   301
subsubsection "Time Complexity"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   302
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   303
fun C_merge_adj :: "('a::linorder) list list \<Rightarrow> nat" where
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   304
"C_merge_adj [] = 0" |
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   305
"C_merge_adj [xs] = 0" |
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   306
"C_merge_adj (xs # ys # zss) = C_merge xs ys + C_merge_adj zss"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   307
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   308
fun C_merge_all :: "('a::linorder) list list \<Rightarrow> nat" where
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   309
"C_merge_all [] = 0" |
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   310
"C_merge_all [xs] = 0" |
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   311
"C_merge_all xss = C_merge_adj xss + C_merge_all (merge_adj xss)"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   312
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   313
definition C_msort_bu :: "('a::linorder) list \<Rightarrow> nat" where
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   314
"C_msort_bu xs = C_merge_all (map (\<lambda>x. [x]) xs)"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   315
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   316
lemma length_merge_adj:
68974
nipkow
parents: 68972
diff changeset
   317
  "\<lbrakk> even(length xss); \<forall>xs \<in> set xss. length xs = m \<rbrakk>
nipkow
parents: 68972
diff changeset
   318
  \<Longrightarrow> \<forall>xs \<in> set (merge_adj xss). length xs = 2*m"
68161
nipkow
parents: 68160
diff changeset
   319
by(induction xss rule: merge_adj.induct) (auto simp: length_merge)
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   320
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   321
lemma C_merge_adj: "\<forall>xs \<in> set xss. length xs = m \<Longrightarrow> C_merge_adj xss \<le> m * length xss"
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   322
proof(induction xss rule: C_merge_adj.induct)
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   323
  case 1 thus ?case by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   324
next
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   325
  case 2 thus ?case by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   326
next
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   327
  case (3 x y) thus ?case using C_merge_ub[of x y] by (simp add: algebra_simps)
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   328
qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   329
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   330
lemma C_merge_all: "\<lbrakk> \<forall>xs \<in> set xss. length xs = m; length xss = 2^k \<rbrakk>
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   331
  \<Longrightarrow> C_merge_all xss \<le> m * k * 2^k"
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   332
proof (induction xss arbitrary: k m rule: C_merge_all.induct)
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   333
  case 1 thus ?case by simp
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   334
next
68158
nipkow
parents: 68139
diff changeset
   335
  case 2 thus ?case by simp
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   336
next
68162
nipkow
parents: 68161
diff changeset
   337
  case (3 xs ys xss)
nipkow
parents: 68161
diff changeset
   338
  let ?xss = "xs # ys # xss"
nipkow
parents: 68161
diff changeset
   339
  let ?xss2 = "merge_adj ?xss"
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   340
  obtain k' where k': "k = Suc k'" using "3.prems"(2)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   341
    by (metis length_Cons nat.inject nat_power_eq_Suc_0_iff nat.exhaust)
68972
96b15934a17a tuned proof
nipkow
parents: 68971
diff changeset
   342
  have "even (length ?xss)" using "3.prems"(2) k' by auto
96b15934a17a tuned proof
nipkow
parents: 68971
diff changeset
   343
  from length_merge_adj[OF this "3.prems"(1)]
96b15934a17a tuned proof
nipkow
parents: 68971
diff changeset
   344
  have *: "\<forall>x \<in> set(merge_adj ?xss). length x = 2*m" .
68162
nipkow
parents: 68161
diff changeset
   345
  have **: "length ?xss2 = 2 ^ k'" using "3.prems"(2) k' by auto
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   346
  have "C_merge_all ?xss = C_merge_adj ?xss + C_merge_all ?xss2" by simp
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   347
  also have "\<dots> \<le> m * 2^k + C_merge_all ?xss2"
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   348
    using "3.prems"(2) C_merge_adj[OF "3.prems"(1)] by (auto simp: algebra_simps)
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   349
  also have "\<dots> \<le> m * 2^k + (2*m) * k' * 2^k'"
68079
nipkow
parents: 68078
diff changeset
   350
    using "3.IH"[OF * **] by simp
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   351
  also have "\<dots> = m * k * 2^k"
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   352
    using k' by (simp add: algebra_simps)
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   353
  finally show ?case .
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   354
qed
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   355
72501
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   356
corollary C_msort_bu: "length xs = 2 ^ k \<Longrightarrow> C_msort_bu xs \<le> k * 2 ^ k"
70b420065a07 tuned names: t_ -> T_
nipkow
parents: 71918
diff changeset
   357
using C_merge_all[of "map (\<lambda>x. [x]) xs" 1] by (simp add: C_msort_bu_def)
67983
487685540a51 added bottom-up merge sort
nipkow
parents: 66912
diff changeset
   358
68993
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   359
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   360
subsection "Quicksort"
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   361
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   362
fun quicksort :: "('a::linorder) list \<Rightarrow> 'a list" where
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   363
"quicksort []     = []" |
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   364
"quicksort (x#xs) = quicksort (filter (\<lambda>y. y < x) xs) @ [x] @ quicksort (filter (\<lambda>y. x \<le> y) xs)"
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   365
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   366
lemma mset_quicksort: "mset (quicksort xs) = mset xs"
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   367
apply (induction xs rule: quicksort.induct)
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   368
apply (auto simp: not_le)
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   369
done
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   370
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   371
lemma set_quicksort: "set (quicksort xs) = set xs"
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   372
by(rule mset_eq_setD[OF mset_quicksort])
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   373
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   374
lemma sorted_quicksort: "sorted (quicksort xs)"
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   375
apply (induction xs rule: quicksort.induct)
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   376
apply (auto simp add: sorted_append set_quicksort)
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   377
done
e66783811518 added quicksort
nipkow
parents: 68974
diff changeset
   378
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   379
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   380
subsection "Insertion Sort w.r.t. Keys and Stability"
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   381
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   382
hide_const List.insort_key
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   383
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   384
fun insort1_key :: "('a \<Rightarrow> 'k::linorder) \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   385
"insort1_key f x [] = [x]" |
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   386
"insort1_key f x (y # ys) = (if f x \<le> f y then x # y # ys else y # insort1_key f x ys)"
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   387
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   388
fun insort_key :: "('a \<Rightarrow> 'k::linorder) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   389
"insort_key f [] = []" |
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   390
"insort_key f (x # xs) = insort1_key f x (insort_key f xs)"
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   391
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   392
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   393
subsubsection "Standard functional correctness"
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   394
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   395
lemma mset_insort1_key: "mset (insort1_key f x xs) = {#x#} + mset xs"
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   396
by(induction xs) simp_all
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   397
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   398
lemma mset_insort_key: "mset (insort_key f xs) = mset xs"
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   399
by(induction xs) (simp_all add: mset_insort1_key)
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   400
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   401
(* Inductive proof simpler than derivation from mset lemma: *)
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   402
lemma set_insort1_key: "set (insort1_key f x xs) = {x} \<union> set xs"
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   403
by (induction xs) auto
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   404
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   405
lemma sorted_insort1_key: "sorted (map f (insort1_key f a xs)) = sorted (map f xs)"
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   406
by(induction xs)(auto simp: set_insort1_key)
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   407
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   408
lemma sorted_insort_key: "sorted (map f (insort_key f xs))"
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   409
by(induction xs)(simp_all add: sorted_insort1_key)
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   410
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   411
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   412
subsubsection "Stability"
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   413
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   414
lemma insort1_is_Cons: "\<forall>x\<in>set xs. f a \<le> f x \<Longrightarrow> insort1_key f a xs = a # xs"
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   415
by (cases xs) auto
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   416
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   417
lemma filter_insort1_key_neg:
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   418
  "\<not> P x \<Longrightarrow> filter P (insort1_key f x xs) = filter P xs"
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   419
by (induction xs) simp_all
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   420
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   421
lemma filter_insort1_key_pos:
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   422
  "sorted (map f xs) \<Longrightarrow> P x \<Longrightarrow> filter P (insort1_key f x xs) = insort1_key f x (filter P xs)"
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   423
by (induction xs) (auto, subst insort1_is_Cons, auto)
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   424
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   425
lemma sort_key_stable: "filter (\<lambda>y. f y = k) (insort_key f xs) = filter (\<lambda>y. f y = k) xs"
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   426
proof (induction xs)
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   427
  case Nil thus ?case by simp
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   428
next
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   429
  case (Cons a xs)
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   430
  thus ?case
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   431
  proof (cases "f a = k")
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   432
    case False thus ?thesis  by (simp add: Cons.IH filter_insort1_key_neg)
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   433
  next
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   434
    case True
75501
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   435
    have "filter (\<lambda>y. f y = k) (insort_key f (a # xs))
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   436
      = filter (\<lambda>y. f y = k) (insort1_key f a (insort_key f xs))"  by simp
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   437
    also have "\<dots> = insort1_key f a (filter (\<lambda>y. f y = k) (insort_key f xs))"
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   438
      by (simp add: True filter_insort1_key_pos sorted_insort_key)
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   439
    also have "\<dots> = insort1_key f a (filter (\<lambda>y. f y = k) xs)"  by (simp add: Cons.IH)
426afab39a55 insort renamings
nipkow
parents: 73047
diff changeset
   440
    also have "\<dots> = a # (filter (\<lambda>y. f y = k) xs)"  by(simp add: True insort1_is_Cons)
69005
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   441
    also have "\<dots> = filter (\<lambda>y. f y = k) (a # xs)" by (simp add: True)
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   442
    finally show ?thesis .
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   443
  qed
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   444
qed
778434adc352 added insertion sort with keys
nipkow
parents: 68993
diff changeset
   445
66543
a90dbf19f573 new file
nipkow
parents:
diff changeset
   446
end