src/HOL/Imperative_HOL/ex/Imperative_Quicksort.thy
author haftmann
Wed Jul 14 16:13:14 2010 +0200 (2010-07-14 ago)
changeset 37826 4c0a5e35931a
parent 37806 a7679be14442
child 37845 b70d7a347964
permissions -rw-r--r--
avoid export_code ... file -
bulwahn@36098
     1
(*  Title:      HOL/Imperative_HOL/ex/Imperative_Quicksort.thy
bulwahn@36098
     2
    Author:     Lukas Bulwahn, TU Muenchen
bulwahn@36098
     3
*)
bulwahn@36098
     4
bulwahn@36098
     5
header {* An imperative implementation of Quicksort on arrays *}
haftmann@30689
     6
haftmann@30689
     7
theory Imperative_Quicksort
haftmann@37771
     8
imports Imperative_HOL Subarray Multiset Efficient_Nat
bulwahn@27656
     9
begin
bulwahn@27656
    10
bulwahn@27656
    11
text {* We prove QuickSort correct in the Relational Calculus. *}
bulwahn@27656
    12
bulwahn@27656
    13
definition swap :: "nat array \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> unit Heap"
bulwahn@27656
    14
where
krauss@37792
    15
  "swap arr i j =
krauss@37792
    16
     do {
haftmann@37798
    17
       x \<leftarrow> Array.nth arr i;
haftmann@37798
    18
       y \<leftarrow> Array.nth arr j;
haftmann@37798
    19
       Array.upd i y arr;
haftmann@37798
    20
       Array.upd j x arr;
bulwahn@27656
    21
       return ()
krauss@37792
    22
     }"
bulwahn@27656
    23
haftmann@37771
    24
lemma crel_swapI [crel_intros]:
haftmann@37802
    25
  assumes "i < Array.length h a" "j < Array.length h a"
haftmann@37806
    26
    "x = Array.get h a ! i" "y = Array.get h a ! j"
haftmann@37798
    27
    "h' = Array.update a j x (Array.update a i y h)"
haftmann@37771
    28
  shows "crel (swap a i j) h h' r"
haftmann@37771
    29
  unfolding swap_def using assms by (auto intro!: crel_intros)
haftmann@37771
    30
bulwahn@27656
    31
lemma swap_permutes:
bulwahn@27656
    32
  assumes "crel (swap a i j) h h' rs"
haftmann@37806
    33
  shows "multiset_of (Array.get h' a) 
haftmann@37806
    34
  = multiset_of (Array.get h a)"
bulwahn@27656
    35
  using assms
haftmann@28145
    36
  unfolding swap_def
haftmann@37771
    37
  by (auto simp add: Array.length_def multiset_of_swap dest: sym [of _ "h'"] elim!: crel_bindE crel_nthE crel_returnE crel_updE)
bulwahn@27656
    38
bulwahn@27656
    39
function part1 :: "nat array \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat Heap"
bulwahn@27656
    40
where
bulwahn@27656
    41
  "part1 a left right p = (
bulwahn@27656
    42
     if (right \<le> left) then return right
krauss@37792
    43
     else do {
haftmann@37798
    44
       v \<leftarrow> Array.nth a left;
bulwahn@27656
    45
       (if (v \<le> p) then (part1 a (left + 1) right p)
krauss@37792
    46
                    else (do { swap a left right;
krauss@37792
    47
  part1 a left (right - 1) p }))
krauss@37792
    48
     })"
bulwahn@27656
    49
by pat_completeness auto
bulwahn@27656
    50
bulwahn@27656
    51
termination
bulwahn@27656
    52
by (relation "measure (\<lambda>(_,l,r,_). r - l )") auto
bulwahn@27656
    53
bulwahn@27656
    54
declare part1.simps[simp del]
bulwahn@27656
    55
bulwahn@27656
    56
lemma part_permutes:
bulwahn@27656
    57
  assumes "crel (part1 a l r p) h h' rs"
haftmann@37806
    58
  shows "multiset_of (Array.get h' a) 
haftmann@37806
    59
  = multiset_of (Array.get h a)"
bulwahn@27656
    60
  using assms
bulwahn@27656
    61
proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
bulwahn@27656
    62
  case (1 a l r p h h' rs)
bulwahn@27656
    63
  thus ?case
haftmann@28145
    64
    unfolding part1.simps [of a l r p]
haftmann@37771
    65
    by (elim crel_bindE crel_ifE crel_returnE crel_nthE) (auto simp add: swap_permutes)
bulwahn@27656
    66
qed
bulwahn@27656
    67
bulwahn@27656
    68
lemma part_returns_index_in_bounds:
bulwahn@27656
    69
  assumes "crel (part1 a l r p) h h' rs"
bulwahn@27656
    70
  assumes "l \<le> r"
bulwahn@27656
    71
  shows "l \<le> rs \<and> rs \<le> r"
bulwahn@27656
    72
using assms
bulwahn@27656
    73
proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
bulwahn@27656
    74
  case (1 a l r p h h' rs)
bulwahn@27656
    75
  note cr = `crel (part1 a l r p) h h' rs`
bulwahn@27656
    76
  show ?case
bulwahn@27656
    77
  proof (cases "r \<le> l")
bulwahn@27656
    78
    case True (* Terminating case *)
bulwahn@27656
    79
    with cr `l \<le> r` show ?thesis
haftmann@28145
    80
      unfolding part1.simps[of a l r p]
haftmann@37771
    81
      by (elim crel_bindE crel_ifE crel_returnE crel_nthE) auto
bulwahn@27656
    82
  next
bulwahn@27656
    83
    case False (* recursive case *)
bulwahn@27656
    84
    note rec_condition = this
haftmann@37806
    85
    let ?v = "Array.get h a ! l"
bulwahn@27656
    86
    show ?thesis
bulwahn@27656
    87
    proof (cases "?v \<le> p")
bulwahn@27656
    88
      case True
bulwahn@27656
    89
      with cr False
bulwahn@27656
    90
      have rec1: "crel (part1 a (l + 1) r p) h h' rs"
haftmann@28145
    91
        unfolding part1.simps[of a l r p]
haftmann@37771
    92
        by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
bulwahn@27656
    93
      from rec_condition have "l + 1 \<le> r" by arith
bulwahn@27656
    94
      from 1(1)[OF rec_condition True rec1 `l + 1 \<le> r`]
bulwahn@27656
    95
      show ?thesis by simp
bulwahn@27656
    96
    next
bulwahn@27656
    97
      case False
bulwahn@27656
    98
      with rec_condition cr
bulwahn@27656
    99
      obtain h1 where swp: "crel (swap a l r) h h1 ()"
bulwahn@27656
   100
        and rec2: "crel (part1 a l (r - 1) p) h1 h' rs"
haftmann@28145
   101
        unfolding part1.simps[of a l r p]
haftmann@37771
   102
        by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
bulwahn@27656
   103
      from rec_condition have "l \<le> r - 1" by arith
bulwahn@27656
   104
      from 1(2) [OF rec_condition False rec2 `l \<le> r - 1`] show ?thesis by fastsimp
bulwahn@27656
   105
    qed
bulwahn@27656
   106
  qed
bulwahn@27656
   107
qed
bulwahn@27656
   108
bulwahn@27656
   109
lemma part_length_remains:
bulwahn@27656
   110
  assumes "crel (part1 a l r p) h h' rs"
haftmann@37802
   111
  shows "Array.length h a = Array.length h' a"
bulwahn@27656
   112
using assms
bulwahn@27656
   113
proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
bulwahn@27656
   114
  case (1 a l r p h h' rs)
bulwahn@27656
   115
  note cr = `crel (part1 a l r p) h h' rs`
bulwahn@27656
   116
  
bulwahn@27656
   117
  show ?case
bulwahn@27656
   118
  proof (cases "r \<le> l")
bulwahn@27656
   119
    case True (* Terminating case *)
bulwahn@27656
   120
    with cr show ?thesis
haftmann@28145
   121
      unfolding part1.simps[of a l r p]
haftmann@37771
   122
      by (elim crel_bindE crel_ifE crel_returnE crel_nthE) auto
bulwahn@27656
   123
  next
bulwahn@27656
   124
    case False (* recursive case *)
bulwahn@27656
   125
    with cr 1 show ?thesis
haftmann@28145
   126
      unfolding part1.simps [of a l r p] swap_def
haftmann@37771
   127
      by (auto elim!: crel_bindE crel_ifE crel_nthE crel_returnE crel_updE) fastsimp
bulwahn@27656
   128
  qed
bulwahn@27656
   129
qed
bulwahn@27656
   130
bulwahn@27656
   131
lemma part_outer_remains:
bulwahn@27656
   132
  assumes "crel (part1 a l r p) h h' rs"
haftmann@37806
   133
  shows "\<forall>i. i < l \<or> r < i \<longrightarrow> Array.get h (a::nat array) ! i = Array.get h' a ! i"
bulwahn@27656
   134
  using assms
bulwahn@27656
   135
proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
bulwahn@27656
   136
  case (1 a l r p h h' rs)
bulwahn@27656
   137
  note cr = `crel (part1 a l r p) h h' rs`
bulwahn@27656
   138
  
bulwahn@27656
   139
  show ?case
bulwahn@27656
   140
  proof (cases "r \<le> l")
bulwahn@27656
   141
    case True (* Terminating case *)
bulwahn@27656
   142
    with cr show ?thesis
haftmann@28145
   143
      unfolding part1.simps[of a l r p]
haftmann@37771
   144
      by (elim crel_bindE crel_ifE crel_returnE crel_nthE) auto
bulwahn@27656
   145
  next
bulwahn@27656
   146
    case False (* recursive case *)
bulwahn@27656
   147
    note rec_condition = this
haftmann@37806
   148
    let ?v = "Array.get h a ! l"
bulwahn@27656
   149
    show ?thesis
bulwahn@27656
   150
    proof (cases "?v \<le> p")
bulwahn@27656
   151
      case True
bulwahn@27656
   152
      with cr False
bulwahn@27656
   153
      have rec1: "crel (part1 a (l + 1) r p) h h' rs"
haftmann@28145
   154
        unfolding part1.simps[of a l r p]
haftmann@37771
   155
        by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
bulwahn@27656
   156
      from 1(1)[OF rec_condition True rec1]
bulwahn@27656
   157
      show ?thesis by fastsimp
bulwahn@27656
   158
    next
bulwahn@27656
   159
      case False
bulwahn@27656
   160
      with rec_condition cr
bulwahn@27656
   161
      obtain h1 where swp: "crel (swap a l r) h h1 ()"
bulwahn@27656
   162
        and rec2: "crel (part1 a l (r - 1) p) h1 h' rs"
haftmann@28145
   163
        unfolding part1.simps[of a l r p]
haftmann@37771
   164
        by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
bulwahn@27656
   165
      from swp rec_condition have
haftmann@37806
   166
        "\<forall>i. i < l \<or> r < i \<longrightarrow> Array.get h a ! i = Array.get h1 a ! i"
wenzelm@32960
   167
        unfolding swap_def
haftmann@37771
   168
        by (elim crel_bindE crel_nthE crel_updE crel_returnE) auto
bulwahn@27656
   169
      with 1(2) [OF rec_condition False rec2] show ?thesis by fastsimp
bulwahn@27656
   170
    qed
bulwahn@27656
   171
  qed
bulwahn@27656
   172
qed
bulwahn@27656
   173
bulwahn@27656
   174
bulwahn@27656
   175
lemma part_partitions:
bulwahn@27656
   176
  assumes "crel (part1 a l r p) h h' rs"
haftmann@37806
   177
  shows "(\<forall>i. l \<le> i \<and> i < rs \<longrightarrow> Array.get h' (a::nat array) ! i \<le> p)
haftmann@37806
   178
  \<and> (\<forall>i. rs < i \<and> i \<le> r \<longrightarrow> Array.get h' a ! i \<ge> p)"
bulwahn@27656
   179
  using assms
bulwahn@27656
   180
proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
bulwahn@27656
   181
  case (1 a l r p h h' rs)
bulwahn@27656
   182
  note cr = `crel (part1 a l r p) h h' rs`
bulwahn@27656
   183
  
bulwahn@27656
   184
  show ?case
bulwahn@27656
   185
  proof (cases "r \<le> l")
bulwahn@27656
   186
    case True (* Terminating case *)
bulwahn@27656
   187
    with cr have "rs = r"
haftmann@28145
   188
      unfolding part1.simps[of a l r p]
haftmann@37771
   189
      by (elim crel_bindE crel_ifE crel_returnE crel_nthE) auto
bulwahn@27656
   190
    with True
bulwahn@27656
   191
    show ?thesis by auto
bulwahn@27656
   192
  next
bulwahn@27656
   193
    case False (* recursive case *)
bulwahn@27656
   194
    note lr = this
haftmann@37806
   195
    let ?v = "Array.get h a ! l"
bulwahn@27656
   196
    show ?thesis
bulwahn@27656
   197
    proof (cases "?v \<le> p")
bulwahn@27656
   198
      case True
bulwahn@27656
   199
      with lr cr
bulwahn@27656
   200
      have rec1: "crel (part1 a (l + 1) r p) h h' rs"
haftmann@28145
   201
        unfolding part1.simps[of a l r p]
haftmann@37771
   202
        by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
haftmann@37806
   203
      from True part_outer_remains[OF rec1] have a_l: "Array.get h' a ! l \<le> p"
wenzelm@32960
   204
        by fastsimp
bulwahn@27656
   205
      have "\<forall>i. (l \<le> i = (l = i \<or> Suc l \<le> i))" by arith
bulwahn@27656
   206
      with 1(1)[OF False True rec1] a_l show ?thesis
wenzelm@32960
   207
        by auto
bulwahn@27656
   208
    next
bulwahn@27656
   209
      case False
bulwahn@27656
   210
      with lr cr
bulwahn@27656
   211
      obtain h1 where swp: "crel (swap a l r) h h1 ()"
bulwahn@27656
   212
        and rec2: "crel (part1 a l (r - 1) p) h1 h' rs"
haftmann@28145
   213
        unfolding part1.simps[of a l r p]
haftmann@37771
   214
        by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
haftmann@37806
   215
      from swp False have "Array.get h1 a ! r \<ge> p"
wenzelm@32960
   216
        unfolding swap_def
haftmann@37771
   217
        by (auto simp add: Array.length_def elim!: crel_bindE crel_nthE crel_updE crel_returnE)
haftmann@37806
   218
      with part_outer_remains [OF rec2] lr have a_r: "Array.get h' a ! r \<ge> p"
wenzelm@32960
   219
        by fastsimp
bulwahn@27656
   220
      have "\<forall>i. (i \<le> r = (i = r \<or> i \<le> r - 1))" by arith
bulwahn@27656
   221
      with 1(2)[OF lr False rec2] a_r show ?thesis
wenzelm@32960
   222
        by auto
bulwahn@27656
   223
    qed
bulwahn@27656
   224
  qed
bulwahn@27656
   225
qed
bulwahn@27656
   226
bulwahn@27656
   227
bulwahn@27656
   228
fun partition :: "nat array \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat Heap"
bulwahn@27656
   229
where
krauss@37792
   230
  "partition a left right = do {
haftmann@37798
   231
     pivot \<leftarrow> Array.nth a right;
bulwahn@27656
   232
     middle \<leftarrow> part1 a left (right - 1) pivot;
haftmann@37798
   233
     v \<leftarrow> Array.nth a middle;
bulwahn@27656
   234
     m \<leftarrow> return (if (v \<le> pivot) then (middle + 1) else middle);
bulwahn@27656
   235
     swap a m right;
bulwahn@27656
   236
     return m
krauss@37792
   237
   }"
bulwahn@27656
   238
bulwahn@27656
   239
declare partition.simps[simp del]
bulwahn@27656
   240
bulwahn@27656
   241
lemma partition_permutes:
bulwahn@27656
   242
  assumes "crel (partition a l r) h h' rs"
haftmann@37806
   243
  shows "multiset_of (Array.get h' a) 
haftmann@37806
   244
  = multiset_of (Array.get h a)"
bulwahn@27656
   245
proof -
bulwahn@27656
   246
    from assms part_permutes swap_permutes show ?thesis
haftmann@28145
   247
      unfolding partition.simps
haftmann@37771
   248
      by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) auto
bulwahn@27656
   249
qed
bulwahn@27656
   250
bulwahn@27656
   251
lemma partition_length_remains:
bulwahn@27656
   252
  assumes "crel (partition a l r) h h' rs"
haftmann@37802
   253
  shows "Array.length h a = Array.length h' a"
bulwahn@27656
   254
proof -
bulwahn@27656
   255
  from assms part_length_remains show ?thesis
haftmann@28145
   256
    unfolding partition.simps swap_def
haftmann@37771
   257
    by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) auto
bulwahn@27656
   258
qed
bulwahn@27656
   259
bulwahn@27656
   260
lemma partition_outer_remains:
bulwahn@27656
   261
  assumes "crel (partition a l r) h h' rs"
bulwahn@27656
   262
  assumes "l < r"
haftmann@37806
   263
  shows "\<forall>i. i < l \<or> r < i \<longrightarrow> Array.get h (a::nat array) ! i = Array.get h' a ! i"
bulwahn@27656
   264
proof -
bulwahn@27656
   265
  from assms part_outer_remains part_returns_index_in_bounds show ?thesis
haftmann@28145
   266
    unfolding partition.simps swap_def
haftmann@37771
   267
    by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) fastsimp
bulwahn@27656
   268
qed
bulwahn@27656
   269
bulwahn@27656
   270
lemma partition_returns_index_in_bounds:
bulwahn@27656
   271
  assumes crel: "crel (partition a l r) h h' rs"
bulwahn@27656
   272
  assumes "l < r"
bulwahn@27656
   273
  shows "l \<le> rs \<and> rs \<le> r"
bulwahn@27656
   274
proof -
bulwahn@27656
   275
  from crel obtain middle h'' p where part: "crel (part1 a l (r - 1) p) h h'' middle"
haftmann@37806
   276
    and rs_equals: "rs = (if Array.get h'' a ! middle \<le> Array.get h a ! r then middle + 1
bulwahn@27656
   277
         else middle)"
haftmann@28145
   278
    unfolding partition.simps
haftmann@37805
   279
    by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) simp 
bulwahn@27656
   280
  from `l < r` have "l \<le> r - 1" by arith
bulwahn@27656
   281
  from part_returns_index_in_bounds[OF part this] rs_equals `l < r` show ?thesis by auto
bulwahn@27656
   282
qed
bulwahn@27656
   283
bulwahn@27656
   284
lemma partition_partitions:
bulwahn@27656
   285
  assumes crel: "crel (partition a l r) h h' rs"
bulwahn@27656
   286
  assumes "l < r"
haftmann@37806
   287
  shows "(\<forall>i. l \<le> i \<and> i < rs \<longrightarrow> Array.get h' (a::nat array) ! i \<le> Array.get h' a ! rs) \<and>
haftmann@37806
   288
  (\<forall>i. rs < i \<and> i \<le> r \<longrightarrow> Array.get h' a ! rs \<le> Array.get h' a ! i)"
bulwahn@27656
   289
proof -
haftmann@37806
   290
  let ?pivot = "Array.get h a ! r" 
bulwahn@27656
   291
  from crel obtain middle h1 where part: "crel (part1 a l (r - 1) ?pivot) h h1 middle"
bulwahn@27656
   292
    and swap: "crel (swap a rs r) h1 h' ()"
haftmann@37806
   293
    and rs_equals: "rs = (if Array.get h1 a ! middle \<le> ?pivot then middle + 1
bulwahn@27656
   294
         else middle)"
haftmann@28145
   295
    unfolding partition.simps
haftmann@37771
   296
    by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) simp
haftmann@37806
   297
  from swap have h'_def: "h' = Array.update a r (Array.get h1 a ! rs)
haftmann@37806
   298
    (Array.update a rs (Array.get h1 a ! r) h1)"
haftmann@28145
   299
    unfolding swap_def
haftmann@37771
   300
    by (elim crel_bindE crel_returnE crel_nthE crel_updE) simp
haftmann@37802
   301
  from swap have in_bounds: "r < Array.length h1 a \<and> rs < Array.length h1 a"
haftmann@28145
   302
    unfolding swap_def
haftmann@37771
   303
    by (elim crel_bindE crel_returnE crel_nthE crel_updE) simp
haftmann@37802
   304
  from swap have swap_length_remains: "Array.length h1 a = Array.length h' a"
haftmann@37771
   305
    unfolding swap_def by (elim crel_bindE crel_returnE crel_nthE crel_updE) auto
haftmann@37771
   306
  from `l < r` have "l \<le> r - 1" by simp
bulwahn@27656
   307
  note middle_in_bounds = part_returns_index_in_bounds[OF part this]
bulwahn@27656
   308
  from part_outer_remains[OF part] `l < r`
haftmann@37806
   309
  have "Array.get h a ! r = Array.get h1 a ! r"
bulwahn@27656
   310
    by fastsimp
bulwahn@27656
   311
  with swap
haftmann@37806
   312
  have right_remains: "Array.get h a ! r = Array.get h' a ! rs"
haftmann@28145
   313
    unfolding swap_def
haftmann@37771
   314
    by (auto simp add: Array.length_def elim!: crel_bindE crel_returnE crel_nthE crel_updE) (cases "r = rs", auto)
bulwahn@27656
   315
  from part_partitions [OF part]
bulwahn@27656
   316
  show ?thesis
haftmann@37806
   317
  proof (cases "Array.get h1 a ! middle \<le> ?pivot")
bulwahn@27656
   318
    case True
bulwahn@27656
   319
    with rs_equals have rs_equals: "rs = middle + 1" by simp
bulwahn@27656
   320
    { 
bulwahn@27656
   321
      fix i
bulwahn@27656
   322
      assume i_is_left: "l \<le> i \<and> i < rs"
bulwahn@27656
   323
      with swap_length_remains in_bounds middle_in_bounds rs_equals `l < r`
haftmann@37802
   324
      have i_props: "i < Array.length h' a" "i \<noteq> r" "i \<noteq> rs" by auto
bulwahn@27656
   325
      from i_is_left rs_equals have "l \<le> i \<and> i < middle \<or> i = middle" by arith
bulwahn@27656
   326
      with part_partitions[OF part] right_remains True
haftmann@37806
   327
      have "Array.get h1 a ! i \<le> Array.get h' a ! rs" by fastsimp
haftmann@37806
   328
      with i_props h'_def in_bounds have "Array.get h' a ! i \<le> Array.get h' a ! rs"
haftmann@37798
   329
        unfolding Array.update_def Array.length_def by simp
bulwahn@27656
   330
    }
bulwahn@27656
   331
    moreover
bulwahn@27656
   332
    {
bulwahn@27656
   333
      fix i
bulwahn@27656
   334
      assume "rs < i \<and> i \<le> r"
bulwahn@27656
   335
bulwahn@27656
   336
      hence "(rs < i \<and> i \<le> r - 1) \<or> (rs < i \<and> i = r)" by arith
haftmann@37806
   337
      hence "Array.get h' a ! rs \<le> Array.get h' a ! i"
bulwahn@27656
   338
      proof
wenzelm@32960
   339
        assume i_is: "rs < i \<and> i \<le> r - 1"
wenzelm@32960
   340
        with swap_length_remains in_bounds middle_in_bounds rs_equals
haftmann@37802
   341
        have i_props: "i < Array.length h' a" "i \<noteq> r" "i \<noteq> rs" by auto
wenzelm@32960
   342
        from part_partitions[OF part] rs_equals right_remains i_is
haftmann@37806
   343
        have "Array.get h' a ! rs \<le> Array.get h1 a ! i"
wenzelm@32960
   344
          by fastsimp
wenzelm@32960
   345
        with i_props h'_def show ?thesis by fastsimp
bulwahn@27656
   346
      next
wenzelm@32960
   347
        assume i_is: "rs < i \<and> i = r"
wenzelm@32960
   348
        with rs_equals have "Suc middle \<noteq> r" by arith
wenzelm@32960
   349
        with middle_in_bounds `l < r` have "Suc middle \<le> r - 1" by arith
wenzelm@32960
   350
        with part_partitions[OF part] right_remains 
haftmann@37806
   351
        have "Array.get h' a ! rs \<le> Array.get h1 a ! (Suc middle)"
wenzelm@32960
   352
          by fastsimp
wenzelm@32960
   353
        with i_is True rs_equals right_remains h'_def
wenzelm@32960
   354
        show ?thesis using in_bounds
haftmann@37798
   355
          unfolding Array.update_def Array.length_def
wenzelm@32960
   356
          by auto
bulwahn@27656
   357
      qed
bulwahn@27656
   358
    }
bulwahn@27656
   359
    ultimately show ?thesis by auto
bulwahn@27656
   360
  next
bulwahn@27656
   361
    case False
bulwahn@27656
   362
    with rs_equals have rs_equals: "middle = rs" by simp
bulwahn@27656
   363
    { 
bulwahn@27656
   364
      fix i
bulwahn@27656
   365
      assume i_is_left: "l \<le> i \<and> i < rs"
bulwahn@27656
   366
      with swap_length_remains in_bounds middle_in_bounds rs_equals
haftmann@37802
   367
      have i_props: "i < Array.length h' a" "i \<noteq> r" "i \<noteq> rs" by auto
bulwahn@27656
   368
      from part_partitions[OF part] rs_equals right_remains i_is_left
haftmann@37806
   369
      have "Array.get h1 a ! i \<le> Array.get h' a ! rs" by fastsimp
haftmann@37806
   370
      with i_props h'_def have "Array.get h' a ! i \<le> Array.get h' a ! rs"
haftmann@37798
   371
        unfolding Array.update_def by simp
bulwahn@27656
   372
    }
bulwahn@27656
   373
    moreover
bulwahn@27656
   374
    {
bulwahn@27656
   375
      fix i
bulwahn@27656
   376
      assume "rs < i \<and> i \<le> r"
bulwahn@27656
   377
      hence "(rs < i \<and> i \<le> r - 1) \<or> i = r" by arith
haftmann@37806
   378
      hence "Array.get h' a ! rs \<le> Array.get h' a ! i"
bulwahn@27656
   379
      proof
wenzelm@32960
   380
        assume i_is: "rs < i \<and> i \<le> r - 1"
wenzelm@32960
   381
        with swap_length_remains in_bounds middle_in_bounds rs_equals
haftmann@37802
   382
        have i_props: "i < Array.length h' a" "i \<noteq> r" "i \<noteq> rs" by auto
wenzelm@32960
   383
        from part_partitions[OF part] rs_equals right_remains i_is
haftmann@37806
   384
        have "Array.get h' a ! rs \<le> Array.get h1 a ! i"
wenzelm@32960
   385
          by fastsimp
wenzelm@32960
   386
        with i_props h'_def show ?thesis by fastsimp
bulwahn@27656
   387
      next
wenzelm@32960
   388
        assume i_is: "i = r"
wenzelm@32960
   389
        from i_is False rs_equals right_remains h'_def
wenzelm@32960
   390
        show ?thesis using in_bounds
haftmann@37798
   391
          unfolding Array.update_def Array.length_def
wenzelm@32960
   392
          by auto
bulwahn@27656
   393
      qed
bulwahn@27656
   394
    }
bulwahn@27656
   395
    ultimately
bulwahn@27656
   396
    show ?thesis by auto
bulwahn@27656
   397
  qed
bulwahn@27656
   398
qed
bulwahn@27656
   399
bulwahn@27656
   400
bulwahn@27656
   401
function quicksort :: "nat array \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> unit Heap"
bulwahn@27656
   402
where
bulwahn@27656
   403
  "quicksort arr left right =
bulwahn@27656
   404
     (if (right > left)  then
krauss@37792
   405
        do {
bulwahn@27656
   406
          pivotNewIndex \<leftarrow> partition arr left right;
bulwahn@27656
   407
          pivotNewIndex \<leftarrow> assert (\<lambda>x. left \<le> x \<and> x \<le> right) pivotNewIndex;
bulwahn@27656
   408
          quicksort arr left (pivotNewIndex - 1);
bulwahn@27656
   409
          quicksort arr (pivotNewIndex + 1) right
krauss@37792
   410
        }
bulwahn@27656
   411
     else return ())"
bulwahn@27656
   412
by pat_completeness auto
bulwahn@27656
   413
bulwahn@27656
   414
(* For termination, we must show that the pivotNewIndex is between left and right *) 
bulwahn@27656
   415
termination
bulwahn@27656
   416
by (relation "measure (\<lambda>(a, l, r). (r - l))") auto
bulwahn@27656
   417
bulwahn@27656
   418
declare quicksort.simps[simp del]
bulwahn@27656
   419
bulwahn@27656
   420
bulwahn@27656
   421
lemma quicksort_permutes:
bulwahn@27656
   422
  assumes "crel (quicksort a l r) h h' rs"
haftmann@37806
   423
  shows "multiset_of (Array.get h' a) 
haftmann@37806
   424
  = multiset_of (Array.get h a)"
bulwahn@27656
   425
  using assms
bulwahn@27656
   426
proof (induct a l r arbitrary: h h' rs rule: quicksort.induct)
bulwahn@27656
   427
  case (1 a l r h h' rs)
bulwahn@27656
   428
  with partition_permutes show ?case
haftmann@28145
   429
    unfolding quicksort.simps [of a l r]
haftmann@37771
   430
    by (elim crel_ifE crel_bindE crel_assertE crel_returnE) auto
bulwahn@27656
   431
qed
bulwahn@27656
   432
bulwahn@27656
   433
lemma length_remains:
bulwahn@27656
   434
  assumes "crel (quicksort a l r) h h' rs"
haftmann@37802
   435
  shows "Array.length h a = Array.length h' a"
bulwahn@27656
   436
using assms
bulwahn@27656
   437
proof (induct a l r arbitrary: h h' rs rule: quicksort.induct)
bulwahn@27656
   438
  case (1 a l r h h' rs)
bulwahn@27656
   439
  with partition_length_remains show ?case
haftmann@28145
   440
    unfolding quicksort.simps [of a l r]
haftmann@37771
   441
    by (elim crel_ifE crel_bindE crel_assertE crel_returnE) auto
bulwahn@27656
   442
qed
bulwahn@27656
   443
bulwahn@27656
   444
lemma quicksort_outer_remains:
bulwahn@27656
   445
  assumes "crel (quicksort a l r) h h' rs"
haftmann@37806
   446
   shows "\<forall>i. i < l \<or> r < i \<longrightarrow> Array.get h (a::nat array) ! i = Array.get h' a ! i"
bulwahn@27656
   447
  using assms
bulwahn@27656
   448
proof (induct a l r arbitrary: h h' rs rule: quicksort.induct)
bulwahn@27656
   449
  case (1 a l r h h' rs)
bulwahn@27656
   450
  note cr = `crel (quicksort a l r) h h' rs`
bulwahn@27656
   451
  thus ?case
bulwahn@27656
   452
  proof (cases "r > l")
bulwahn@27656
   453
    case False
bulwahn@27656
   454
    with cr have "h' = h"
bulwahn@27656
   455
      unfolding quicksort.simps [of a l r]
haftmann@37771
   456
      by (elim crel_ifE crel_returnE) auto
bulwahn@27656
   457
    thus ?thesis by simp
bulwahn@27656
   458
  next
bulwahn@27656
   459
  case True
bulwahn@27656
   460
   { 
bulwahn@27656
   461
      fix h1 h2 p ret1 ret2 i
bulwahn@27656
   462
      assume part: "crel (partition a l r) h h1 p"
bulwahn@27656
   463
      assume qs1: "crel (quicksort a l (p - 1)) h1 h2 ret1"
bulwahn@27656
   464
      assume qs2: "crel (quicksort a (p + 1) r) h2 h' ret2"
bulwahn@27656
   465
      assume pivot: "l \<le> p \<and> p \<le> r"
bulwahn@27656
   466
      assume i_outer: "i < l \<or> r < i"
bulwahn@27656
   467
      from  partition_outer_remains [OF part True] i_outer
haftmann@37806
   468
      have "Array.get h a !i = Array.get h1 a ! i" by fastsimp
bulwahn@27656
   469
      moreover
bulwahn@27656
   470
      with 1(1) [OF True pivot qs1] pivot i_outer
haftmann@37806
   471
      have "Array.get h1 a ! i = Array.get h2 a ! i" by auto
bulwahn@27656
   472
      moreover
bulwahn@27656
   473
      with qs2 1(2) [of p h2 h' ret2] True pivot i_outer
haftmann@37806
   474
      have "Array.get h2 a ! i = Array.get h' a ! i" by auto
haftmann@37806
   475
      ultimately have "Array.get h a ! i= Array.get h' a ! i" by simp
bulwahn@27656
   476
    }
bulwahn@27656
   477
    with cr show ?thesis
haftmann@28145
   478
      unfolding quicksort.simps [of a l r]
haftmann@37771
   479
      by (elim crel_ifE crel_bindE crel_assertE crel_returnE) auto
bulwahn@27656
   480
  qed
bulwahn@27656
   481
qed
bulwahn@27656
   482
bulwahn@27656
   483
lemma quicksort_is_skip:
bulwahn@27656
   484
  assumes "crel (quicksort a l r) h h' rs"
bulwahn@27656
   485
  shows "r \<le> l \<longrightarrow> h = h'"
bulwahn@27656
   486
  using assms
haftmann@28145
   487
  unfolding quicksort.simps [of a l r]
haftmann@37771
   488
  by (elim crel_ifE crel_returnE) auto
bulwahn@27656
   489
 
bulwahn@27656
   490
lemma quicksort_sorts:
bulwahn@27656
   491
  assumes "crel (quicksort a l r) h h' rs"
haftmann@37802
   492
  assumes l_r_length: "l < Array.length h a" "r < Array.length h a" 
bulwahn@27656
   493
  shows "sorted (subarray l (r + 1) a h')"
bulwahn@27656
   494
  using assms
bulwahn@27656
   495
proof (induct a l r arbitrary: h h' rs rule: quicksort.induct)
bulwahn@27656
   496
  case (1 a l r h h' rs)
bulwahn@27656
   497
  note cr = `crel (quicksort a l r) h h' rs`
bulwahn@27656
   498
  thus ?case
bulwahn@27656
   499
  proof (cases "r > l")
bulwahn@27656
   500
    case False
bulwahn@27656
   501
    hence "l \<ge> r + 1 \<or> l = r" by arith 
bulwahn@27656
   502
    with length_remains[OF cr] 1(5) show ?thesis
bulwahn@27656
   503
      by (auto simp add: subarray_Nil subarray_single)
bulwahn@27656
   504
  next
bulwahn@27656
   505
    case True
bulwahn@27656
   506
    { 
bulwahn@27656
   507
      fix h1 h2 p
bulwahn@27656
   508
      assume part: "crel (partition a l r) h h1 p"
bulwahn@27656
   509
      assume qs1: "crel (quicksort a l (p - 1)) h1 h2 ()"
bulwahn@27656
   510
      assume qs2: "crel (quicksort a (p + 1) r) h2 h' ()"
bulwahn@27656
   511
      from partition_returns_index_in_bounds [OF part True]
bulwahn@27656
   512
      have pivot: "l\<le> p \<and> p \<le> r" .
bulwahn@27656
   513
     note length_remains = length_remains[OF qs2] length_remains[OF qs1] partition_length_remains[OF part]
bulwahn@27656
   514
      from quicksort_outer_remains [OF qs2] quicksort_outer_remains [OF qs1] pivot quicksort_is_skip[OF qs1]
haftmann@37806
   515
      have pivot_unchanged: "Array.get h1 a ! p = Array.get h' a ! p" by (cases p, auto)
haftmann@28013
   516
        (*-- First of all, by induction hypothesis both sublists are sorted. *)
bulwahn@27656
   517
      from 1(1)[OF True pivot qs1] length_remains pivot 1(5) 
bulwahn@27656
   518
      have IH1: "sorted (subarray l p a h2)"  by (cases p, auto simp add: subarray_Nil)
bulwahn@27656
   519
      from quicksort_outer_remains [OF qs2] length_remains
bulwahn@27656
   520
      have left_subarray_remains: "subarray l p a h2 = subarray l p a h'"
wenzelm@32960
   521
        by (simp add: subarray_eq_samelength_iff)
bulwahn@27656
   522
      with IH1 have IH1': "sorted (subarray l p a h')" by simp
bulwahn@27656
   523
      from 1(2)[OF True pivot qs2] pivot 1(5) length_remains
bulwahn@27656
   524
      have IH2: "sorted (subarray (p + 1) (r + 1) a h')"
haftmann@28013
   525
        by (cases "Suc p \<le> r", auto simp add: subarray_Nil)
haftmann@28013
   526
           (* -- Secondly, both sublists remain partitioned. *)
bulwahn@27656
   527
      from partition_partitions[OF part True]
haftmann@37806
   528
      have part_conds1: "\<forall>j. j \<in> set (subarray l p a h1) \<longrightarrow> j \<le> Array.get h1 a ! p "
haftmann@37806
   529
        and part_conds2: "\<forall>j. j \<in> set (subarray (p + 1) (r + 1) a h1) \<longrightarrow> Array.get h1 a ! p \<le> j"
haftmann@28013
   530
        by (auto simp add: all_in_set_subarray_conv)
bulwahn@27656
   531
      from quicksort_outer_remains [OF qs1] quicksort_permutes [OF qs1] True
haftmann@37806
   532
        length_remains 1(5) pivot multiset_of_sublist [of l p "Array.get h1 a" "Array.get h2 a"]
bulwahn@27656
   533
      have multiset_partconds1: "multiset_of (subarray l p a h2) = multiset_of (subarray l p a h1)"
haftmann@37719
   534
        unfolding Array.length_def subarray_def by (cases p, auto)
bulwahn@27656
   535
      with left_subarray_remains part_conds1 pivot_unchanged
haftmann@37806
   536
      have part_conds2': "\<forall>j. j \<in> set (subarray l p a h') \<longrightarrow> j \<le> Array.get h' a ! p"
haftmann@28013
   537
        by (simp, subst set_of_multiset_of[symmetric], simp)
haftmann@28013
   538
          (* -- These steps are the analogous for the right sublist \<dots> *)
bulwahn@27656
   539
      from quicksort_outer_remains [OF qs1] length_remains
bulwahn@27656
   540
      have right_subarray_remains: "subarray (p + 1) (r + 1) a h1 = subarray (p + 1) (r + 1) a h2"
wenzelm@32960
   541
        by (auto simp add: subarray_eq_samelength_iff)
bulwahn@27656
   542
      from quicksort_outer_remains [OF qs2] quicksort_permutes [OF qs2] True
haftmann@37806
   543
        length_remains 1(5) pivot multiset_of_sublist [of "p + 1" "r + 1" "Array.get h2 a" "Array.get h' a"]
bulwahn@27656
   544
      have multiset_partconds2: "multiset_of (subarray (p + 1) (r + 1) a h') = multiset_of (subarray (p + 1) (r + 1) a h2)"
haftmann@37719
   545
        unfolding Array.length_def subarray_def by auto
bulwahn@27656
   546
      with right_subarray_remains part_conds2 pivot_unchanged
haftmann@37806
   547
      have part_conds1': "\<forall>j. j \<in> set (subarray (p + 1) (r + 1) a h') \<longrightarrow> Array.get h' a ! p \<le> j"
haftmann@28013
   548
        by (simp, subst set_of_multiset_of[symmetric], simp)
haftmann@28013
   549
          (* -- Thirdly and finally, we show that the array is sorted
haftmann@28013
   550
          following from the facts above. *)
haftmann@37806
   551
      from True pivot 1(5) length_remains have "subarray l (r + 1) a h' = subarray l p a h' @ [Array.get h' a ! p] @ subarray (p + 1) (r + 1) a h'"
wenzelm@32960
   552
        by (simp add: subarray_nth_array_Cons, cases "l < p") (auto simp add: subarray_append subarray_Nil)
bulwahn@27656
   553
      with IH1' IH2 part_conds1' part_conds2' pivot have ?thesis
wenzelm@32960
   554
        unfolding subarray_def
wenzelm@32960
   555
        apply (auto simp add: sorted_append sorted_Cons all_in_set_sublist'_conv)
haftmann@37806
   556
        by (auto simp add: set_sublist' dest: le_trans [of _ "Array.get h' a ! p"])
bulwahn@27656
   557
    }
bulwahn@27656
   558
    with True cr show ?thesis
haftmann@28145
   559
      unfolding quicksort.simps [of a l r]
haftmann@37771
   560
      by (elim crel_ifE crel_returnE crel_bindE crel_assertE) auto
bulwahn@27656
   561
  qed
bulwahn@27656
   562
qed
bulwahn@27656
   563
bulwahn@27656
   564
bulwahn@27656
   565
lemma quicksort_is_sort:
haftmann@37802
   566
  assumes crel: "crel (quicksort a 0 (Array.length h a - 1)) h h' rs"
haftmann@37806
   567
  shows "Array.get h' a = sort (Array.get h a)"
haftmann@37806
   568
proof (cases "Array.get h a = []")
bulwahn@27656
   569
  case True
bulwahn@27656
   570
  with quicksort_is_skip[OF crel] show ?thesis
haftmann@37719
   571
  unfolding Array.length_def by simp
bulwahn@27656
   572
next
bulwahn@27656
   573
  case False
haftmann@37806
   574
  from quicksort_sorts [OF crel] False have "sorted (sublist' 0 (List.length (Array.get h a)) (Array.get h' a))"
haftmann@37719
   575
    unfolding Array.length_def subarray_def by auto
haftmann@37806
   576
  with length_remains[OF crel] have "sorted (Array.get h' a)"
haftmann@37719
   577
    unfolding Array.length_def by simp
bulwahn@27656
   578
  with quicksort_permutes [OF crel] properties_for_sort show ?thesis by fastsimp
bulwahn@27656
   579
qed
bulwahn@27656
   580
bulwahn@27656
   581
subsection {* No Errors in quicksort *}
bulwahn@27656
   582
text {* We have proved that quicksort sorts (if no exceptions occur).
bulwahn@27656
   583
We will now show that exceptions do not occur. *}
bulwahn@27656
   584
haftmann@37758
   585
lemma success_part1I: 
haftmann@37802
   586
  assumes "l < Array.length h a" "r < Array.length h a"
haftmann@37758
   587
  shows "success (part1 a l r p) h"
bulwahn@27656
   588
  using assms
bulwahn@27656
   589
proof (induct a l r p arbitrary: h rule: part1.induct)
bulwahn@27656
   590
  case (1 a l r p)
haftmann@37771
   591
  thus ?case unfolding part1.simps [of a l r]
haftmann@37771
   592
  apply (auto intro!: success_intros del: success_ifI simp add: not_le)
haftmann@37771
   593
  apply (auto intro!: crel_intros crel_swapI)
haftmann@37771
   594
  done
bulwahn@27656
   595
qed
bulwahn@27656
   596
haftmann@37758
   597
lemma success_bindI' [success_intros]: (*FIXME move*)
haftmann@37758
   598
  assumes "success f h"
haftmann@37758
   599
  assumes "\<And>h' r. crel f h h' r \<Longrightarrow> success (g r) h'"
haftmann@37758
   600
  shows "success (f \<guillemotright>= g) h"
haftmann@37771
   601
using assms(1) proof (rule success_crelE)
haftmann@37771
   602
  fix h' r
haftmann@37771
   603
  assume "crel f h h' r"
haftmann@37771
   604
  moreover with assms(2) have "success (g r) h'" .
haftmann@37771
   605
  ultimately show "success (f \<guillemotright>= g) h" by (rule success_bind_crelI)
haftmann@37771
   606
qed
haftmann@37758
   607
haftmann@37758
   608
lemma success_partitionI:
haftmann@37802
   609
  assumes "l < r" "l < Array.length h a" "r < Array.length h a"
haftmann@37758
   610
  shows "success (partition a l r) h"
haftmann@37758
   611
using assms unfolding partition.simps swap_def
haftmann@37771
   612
apply (auto intro!: success_bindI' success_ifI success_returnI success_nthI success_updI success_part1I elim!: crel_bindE crel_updE crel_nthE crel_returnE simp add:)
bulwahn@27656
   613
apply (frule part_length_remains)
bulwahn@27656
   614
apply (frule part_returns_index_in_bounds)
bulwahn@27656
   615
apply auto
bulwahn@27656
   616
apply (frule part_length_remains)
bulwahn@27656
   617
apply (frule part_returns_index_in_bounds)
bulwahn@27656
   618
apply auto
bulwahn@27656
   619
apply (frule part_length_remains)
bulwahn@27656
   620
apply auto
bulwahn@27656
   621
done
bulwahn@27656
   622
haftmann@37758
   623
lemma success_quicksortI:
haftmann@37802
   624
  assumes "l < Array.length h a" "r < Array.length h a"
haftmann@37758
   625
  shows "success (quicksort a l r) h"
bulwahn@27656
   626
using assms
bulwahn@27656
   627
proof (induct a l r arbitrary: h rule: quicksort.induct)
bulwahn@27656
   628
  case (1 a l ri h)
bulwahn@27656
   629
  thus ?case
haftmann@28145
   630
    unfolding quicksort.simps [of a l ri]
haftmann@37758
   631
    apply (auto intro!: success_ifI success_bindI' success_returnI success_nthI success_updI success_assertI success_partitionI)
bulwahn@27656
   632
    apply (frule partition_returns_index_in_bounds)
bulwahn@27656
   633
    apply auto
bulwahn@27656
   634
    apply (frule partition_returns_index_in_bounds)
bulwahn@27656
   635
    apply auto
haftmann@37771
   636
    apply (auto elim!: crel_assertE dest!: partition_length_remains length_remains)
bulwahn@27656
   637
    apply (subgoal_tac "Suc r \<le> ri \<or> r = ri") 
bulwahn@27656
   638
    apply (erule disjE)
bulwahn@27656
   639
    apply auto
haftmann@28145
   640
    unfolding quicksort.simps [of a "Suc ri" ri]
haftmann@37758
   641
    apply (auto intro!: success_ifI success_returnI)
bulwahn@27656
   642
    done
bulwahn@27656
   643
qed
bulwahn@27656
   644
haftmann@27674
   645
haftmann@27674
   646
subsection {* Example *}
haftmann@27674
   647
krauss@37792
   648
definition "qsort a = do {
haftmann@37798
   649
    k \<leftarrow> Array.len a;
haftmann@27674
   650
    quicksort a 0 (k - 1);
haftmann@27674
   651
    return a
krauss@37792
   652
  }"
haftmann@27674
   653
haftmann@35041
   654
code_reserved SML upto
haftmann@35041
   655
haftmann@27674
   656
ML {* @{code qsort} (Array.fromList [42, 2, 3, 5, 0, 1705, 8, 3, 15]) () *}
haftmann@27674
   657
haftmann@37826
   658
export_code qsort checking SML SML_imp OCaml? OCaml_imp? Haskell?
haftmann@27674
   659
bulwahn@27656
   660
end