1 (* Title: HOL/Imperative_HOL/ex/Imperative_Quicksort.thy
2 Author: Lukas Bulwahn, TU Muenchen
5 header {* An imperative implementation of Quicksort on arrays *}
7 theory Imperative_Quicksort
8 imports Imperative_HOL Subarray Multiset Efficient_Nat
11 text {* We prove QuickSort correct in the Relational Calculus. *}
13 definition swap :: "nat array \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> unit Heap"
17 x \<leftarrow> Array.nth arr i;
18 y \<leftarrow> Array.nth arr j;
24 lemma crel_swapI [crel_intros]:
25 assumes "i < Array.length h a" "j < Array.length h a"
26 "x = Array.get h a ! i" "y = Array.get h a ! j"
27 "h' = Array.update a j x (Array.update a i y h)"
28 shows "crel (swap a i j) h h' r"
29 unfolding swap_def using assms by (auto intro!: crel_intros)
32 assumes "crel (swap a i j) h h' rs"
33 shows "multiset_of (Array.get h' a)
34 = multiset_of (Array.get h a)"
37 by (auto simp add: Array.length_def multiset_of_swap dest: sym [of _ "h'"] elim!: crel_bindE crel_nthE crel_returnE crel_updE)
39 function part1 :: "nat array \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat Heap"
41 "part1 a left right p = (
42 if (right \<le> left) then return right
44 v \<leftarrow> Array.nth a left;
45 (if (v \<le> p) then (part1 a (left + 1) right p)
46 else (do { swap a left right;
47 part1 a left (right - 1) p }))
49 by pat_completeness auto
52 by (relation "measure (\<lambda>(_,l,r,_). r - l )") auto
54 declare part1.simps[simp del]
57 assumes "crel (part1 a l r p) h h' rs"
58 shows "multiset_of (Array.get h' a)
59 = multiset_of (Array.get h a)"
61 proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
62 case (1 a l r p h h' rs)
64 unfolding part1.simps [of a l r p]
65 by (elim crel_bindE crel_ifE crel_returnE crel_nthE) (auto simp add: swap_permutes)
68 lemma part_returns_index_in_bounds:
69 assumes "crel (part1 a l r p) h h' rs"
71 shows "l \<le> rs \<and> rs \<le> r"
73 proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
74 case (1 a l r p h h' rs)
75 note cr = `crel (part1 a l r p) h h' rs`
77 proof (cases "r \<le> l")
78 case True (* Terminating case *)
79 with cr `l \<le> r` show ?thesis
80 unfolding part1.simps[of a l r p]
81 by (elim crel_bindE crel_ifE crel_returnE crel_nthE) auto
83 case False (* recursive case *)
84 note rec_condition = this
85 let ?v = "Array.get h a ! l"
87 proof (cases "?v \<le> p")
90 have rec1: "crel (part1 a (l + 1) r p) h h' rs"
91 unfolding part1.simps[of a l r p]
92 by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
93 from rec_condition have "l + 1 \<le> r" by arith
94 from 1(1)[OF rec_condition True rec1 `l + 1 \<le> r`]
99 obtain h1 where swp: "crel (swap a l r) h h1 ()"
100 and rec2: "crel (part1 a l (r - 1) p) h1 h' rs"
101 unfolding part1.simps[of a l r p]
102 by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
103 from rec_condition have "l \<le> r - 1" by arith
104 from 1(2) [OF rec_condition False rec2 `l \<le> r - 1`] show ?thesis by fastsimp
109 lemma part_length_remains:
110 assumes "crel (part1 a l r p) h h' rs"
111 shows "Array.length h a = Array.length h' a"
113 proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
114 case (1 a l r p h h' rs)
115 note cr = `crel (part1 a l r p) h h' rs`
118 proof (cases "r \<le> l")
119 case True (* Terminating case *)
121 unfolding part1.simps[of a l r p]
122 by (elim crel_bindE crel_ifE crel_returnE crel_nthE) auto
124 case False (* recursive case *)
125 with cr 1 show ?thesis
126 unfolding part1.simps [of a l r p] swap_def
127 by (auto elim!: crel_bindE crel_ifE crel_nthE crel_returnE crel_updE) fastsimp
131 lemma part_outer_remains:
132 assumes "crel (part1 a l r p) h h' rs"
133 shows "\<forall>i. i < l \<or> r < i \<longrightarrow> Array.get h (a::nat array) ! i = Array.get h' a ! i"
135 proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
136 case (1 a l r p h h' rs)
137 note cr = `crel (part1 a l r p) h h' rs`
140 proof (cases "r \<le> l")
141 case True (* Terminating case *)
143 unfolding part1.simps[of a l r p]
144 by (elim crel_bindE crel_ifE crel_returnE crel_nthE) auto
146 case False (* recursive case *)
147 note rec_condition = this
148 let ?v = "Array.get h a ! l"
150 proof (cases "?v \<le> p")
153 have rec1: "crel (part1 a (l + 1) r p) h h' rs"
154 unfolding part1.simps[of a l r p]
155 by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
156 from 1(1)[OF rec_condition True rec1]
157 show ?thesis by fastsimp
160 with rec_condition cr
161 obtain h1 where swp: "crel (swap a l r) h h1 ()"
162 and rec2: "crel (part1 a l (r - 1) p) h1 h' rs"
163 unfolding part1.simps[of a l r p]
164 by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
165 from swp rec_condition have
166 "\<forall>i. i < l \<or> r < i \<longrightarrow> Array.get h a ! i = Array.get h1 a ! i"
168 by (elim crel_bindE crel_nthE crel_updE crel_returnE) auto
169 with 1(2) [OF rec_condition False rec2] show ?thesis by fastsimp
175 lemma part_partitions:
176 assumes "crel (part1 a l r p) h h' rs"
177 shows "(\<forall>i. l \<le> i \<and> i < rs \<longrightarrow> Array.get h' (a::nat array) ! i \<le> p)
178 \<and> (\<forall>i. rs < i \<and> i \<le> r \<longrightarrow> Array.get h' a ! i \<ge> p)"
180 proof (induct a l r p arbitrary: h h' rs rule:part1.induct)
181 case (1 a l r p h h' rs)
182 note cr = `crel (part1 a l r p) h h' rs`
185 proof (cases "r \<le> l")
186 case True (* Terminating case *)
187 with cr have "rs = r"
188 unfolding part1.simps[of a l r p]
189 by (elim crel_bindE crel_ifE crel_returnE crel_nthE) auto
193 case False (* recursive case *)
195 let ?v = "Array.get h a ! l"
197 proof (cases "?v \<le> p")
200 have rec1: "crel (part1 a (l + 1) r p) h h' rs"
201 unfolding part1.simps[of a l r p]
202 by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
203 from True part_outer_remains[OF rec1] have a_l: "Array.get h' a ! l \<le> p"
205 have "\<forall>i. (l \<le> i = (l = i \<or> Suc l \<le> i))" by arith
206 with 1(1)[OF False True rec1] a_l show ?thesis
211 obtain h1 where swp: "crel (swap a l r) h h1 ()"
212 and rec2: "crel (part1 a l (r - 1) p) h1 h' rs"
213 unfolding part1.simps[of a l r p]
214 by (elim crel_bindE crel_nthE crel_ifE crel_returnE) auto
215 from swp False have "Array.get h1 a ! r \<ge> p"
217 by (auto simp add: Array.length_def elim!: crel_bindE crel_nthE crel_updE crel_returnE)
218 with part_outer_remains [OF rec2] lr have a_r: "Array.get h' a ! r \<ge> p"
220 have "\<forall>i. (i \<le> r = (i = r \<or> i \<le> r - 1))" by arith
221 with 1(2)[OF lr False rec2] a_r show ?thesis
228 fun partition :: "nat array \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat Heap"
230 "partition a left right = do {
231 pivot \<leftarrow> Array.nth a right;
232 middle \<leftarrow> part1 a left (right - 1) pivot;
233 v \<leftarrow> Array.nth a middle;
234 m \<leftarrow> return (if (v \<le> pivot) then (middle + 1) else middle);
239 declare partition.simps[simp del]
241 lemma partition_permutes:
242 assumes "crel (partition a l r) h h' rs"
243 shows "multiset_of (Array.get h' a)
244 = multiset_of (Array.get h a)"
246 from assms part_permutes swap_permutes show ?thesis
247 unfolding partition.simps
248 by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) auto
251 lemma partition_length_remains:
252 assumes "crel (partition a l r) h h' rs"
253 shows "Array.length h a = Array.length h' a"
255 from assms part_length_remains show ?thesis
256 unfolding partition.simps swap_def
257 by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) auto
260 lemma partition_outer_remains:
261 assumes "crel (partition a l r) h h' rs"
263 shows "\<forall>i. i < l \<or> r < i \<longrightarrow> Array.get h (a::nat array) ! i = Array.get h' a ! i"
265 from assms part_outer_remains part_returns_index_in_bounds show ?thesis
266 unfolding partition.simps swap_def
267 by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) fastsimp
270 lemma partition_returns_index_in_bounds:
271 assumes crel: "crel (partition a l r) h h' rs"
273 shows "l \<le> rs \<and> rs \<le> r"
275 from crel obtain middle h'' p where part: "crel (part1 a l (r - 1) p) h h'' middle"
276 and rs_equals: "rs = (if Array.get h'' a ! middle \<le> Array.get h a ! r then middle + 1
278 unfolding partition.simps
279 by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) simp
280 from `l < r` have "l \<le> r - 1" by arith
281 from part_returns_index_in_bounds[OF part this] rs_equals `l < r` show ?thesis by auto
284 lemma partition_partitions:
285 assumes crel: "crel (partition a l r) h h' rs"
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>
288 (\<forall>i. rs < i \<and> i \<le> r \<longrightarrow> Array.get h' a ! rs \<le> Array.get h' a ! i)"
290 let ?pivot = "Array.get h a ! r"
291 from crel obtain middle h1 where part: "crel (part1 a l (r - 1) ?pivot) h h1 middle"
292 and swap: "crel (swap a rs r) h1 h' ()"
293 and rs_equals: "rs = (if Array.get h1 a ! middle \<le> ?pivot then middle + 1
295 unfolding partition.simps
296 by (elim crel_bindE crel_returnE crel_nthE crel_ifE crel_updE) simp
297 from swap have h'_def: "h' = Array.update a r (Array.get h1 a ! rs)
298 (Array.update a rs (Array.get h1 a ! r) h1)"
300 by (elim crel_bindE crel_returnE crel_nthE crel_updE) simp
301 from swap have in_bounds: "r < Array.length h1 a \<and> rs < Array.length h1 a"
303 by (elim crel_bindE crel_returnE crel_nthE crel_updE) simp
304 from swap have swap_length_remains: "Array.length h1 a = Array.length h' a"
305 unfolding swap_def by (elim crel_bindE crel_returnE crel_nthE crel_updE) auto
306 from `l < r` have "l \<le> r - 1" by simp
307 note middle_in_bounds = part_returns_index_in_bounds[OF part this]
308 from part_outer_remains[OF part] `l < r`
309 have "Array.get h a ! r = Array.get h1 a ! r"
312 have right_remains: "Array.get h a ! r = Array.get h' a ! rs"
314 by (auto simp add: Array.length_def elim!: crel_bindE crel_returnE crel_nthE crel_updE) (cases "r = rs", auto)
315 from part_partitions [OF part]
317 proof (cases "Array.get h1 a ! middle \<le> ?pivot")
319 with rs_equals have rs_equals: "rs = middle + 1" by simp
322 assume i_is_left: "l \<le> i \<and> i < rs"
323 with swap_length_remains in_bounds middle_in_bounds rs_equals `l < r`
324 have i_props: "i < Array.length h' a" "i \<noteq> r" "i \<noteq> rs" by auto
325 from i_is_left rs_equals have "l \<le> i \<and> i < middle \<or> i = middle" by arith
326 with part_partitions[OF part] right_remains True
327 have "Array.get h1 a ! i \<le> Array.get h' a ! rs" by fastsimp
328 with i_props h'_def in_bounds have "Array.get h' a ! i \<le> Array.get h' a ! rs"
329 unfolding Array.update_def Array.length_def by simp
334 assume "rs < i \<and> i \<le> r"
336 hence "(rs < i \<and> i \<le> r - 1) \<or> (rs < i \<and> i = r)" by arith
337 hence "Array.get h' a ! rs \<le> Array.get h' a ! i"
339 assume i_is: "rs < i \<and> i \<le> r - 1"
340 with swap_length_remains in_bounds middle_in_bounds rs_equals
341 have i_props: "i < Array.length h' a" "i \<noteq> r" "i \<noteq> rs" by auto
342 from part_partitions[OF part] rs_equals right_remains i_is
343 have "Array.get h' a ! rs \<le> Array.get h1 a ! i"
345 with i_props h'_def show ?thesis by fastsimp
347 assume i_is: "rs < i \<and> i = r"
348 with rs_equals have "Suc middle \<noteq> r" by arith
349 with middle_in_bounds `l < r` have "Suc middle \<le> r - 1" by arith
350 with part_partitions[OF part] right_remains
351 have "Array.get h' a ! rs \<le> Array.get h1 a ! (Suc middle)"
353 with i_is True rs_equals right_remains h'_def
354 show ?thesis using in_bounds
355 unfolding Array.update_def Array.length_def
359 ultimately show ?thesis by auto
362 with rs_equals have rs_equals: "middle = rs" by simp
365 assume i_is_left: "l \<le> i \<and> i < rs"
366 with swap_length_remains in_bounds middle_in_bounds rs_equals
367 have i_props: "i < Array.length h' a" "i \<noteq> r" "i \<noteq> rs" by auto
368 from part_partitions[OF part] rs_equals right_remains i_is_left
369 have "Array.get h1 a ! i \<le> Array.get h' a ! rs" by fastsimp
370 with i_props h'_def have "Array.get h' a ! i \<le> Array.get h' a ! rs"
371 unfolding Array.update_def by simp
376 assume "rs < i \<and> i \<le> r"
377 hence "(rs < i \<and> i \<le> r - 1) \<or> i = r" by arith
378 hence "Array.get h' a ! rs \<le> Array.get h' a ! i"
380 assume i_is: "rs < i \<and> i \<le> r - 1"
381 with swap_length_remains in_bounds middle_in_bounds rs_equals
382 have i_props: "i < Array.length h' a" "i \<noteq> r" "i \<noteq> rs" by auto
383 from part_partitions[OF part] rs_equals right_remains i_is
384 have "Array.get h' a ! rs \<le> Array.get h1 a ! i"
386 with i_props h'_def show ?thesis by fastsimp
389 from i_is False rs_equals right_remains h'_def
390 show ?thesis using in_bounds
391 unfolding Array.update_def Array.length_def
401 function quicksort :: "nat array \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> unit Heap"
403 "quicksort arr left right =
404 (if (right > left) then
406 pivotNewIndex \<leftarrow> partition arr left right;
407 pivotNewIndex \<leftarrow> assert (\<lambda>x. left \<le> x \<and> x \<le> right) pivotNewIndex;
408 quicksort arr left (pivotNewIndex - 1);
409 quicksort arr (pivotNewIndex + 1) right
412 by pat_completeness auto
414 (* For termination, we must show that the pivotNewIndex is between left and right *)
416 by (relation "measure (\<lambda>(a, l, r). (r - l))") auto
418 declare quicksort.simps[simp del]
421 lemma quicksort_permutes:
422 assumes "crel (quicksort a l r) h h' rs"
423 shows "multiset_of (Array.get h' a)
424 = multiset_of (Array.get h a)"
426 proof (induct a l r arbitrary: h h' rs rule: quicksort.induct)
427 case (1 a l r h h' rs)
428 with partition_permutes show ?case
429 unfolding quicksort.simps [of a l r]
430 by (elim crel_ifE crel_bindE crel_assertE crel_returnE) auto
433 lemma length_remains:
434 assumes "crel (quicksort a l r) h h' rs"
435 shows "Array.length h a = Array.length h' a"
437 proof (induct a l r arbitrary: h h' rs rule: quicksort.induct)
438 case (1 a l r h h' rs)
439 with partition_length_remains show ?case
440 unfolding quicksort.simps [of a l r]
441 by (elim crel_ifE crel_bindE crel_assertE crel_returnE) auto
444 lemma quicksort_outer_remains:
445 assumes "crel (quicksort a l r) h h' rs"
446 shows "\<forall>i. i < l \<or> r < i \<longrightarrow> Array.get h (a::nat array) ! i = Array.get h' a ! i"
448 proof (induct a l r arbitrary: h h' rs rule: quicksort.induct)
449 case (1 a l r h h' rs)
450 note cr = `crel (quicksort a l r) h h' rs`
452 proof (cases "r > l")
454 with cr have "h' = h"
455 unfolding quicksort.simps [of a l r]
456 by (elim crel_ifE crel_returnE) auto
461 fix h1 h2 p ret1 ret2 i
462 assume part: "crel (partition a l r) h h1 p"
463 assume qs1: "crel (quicksort a l (p - 1)) h1 h2 ret1"
464 assume qs2: "crel (quicksort a (p + 1) r) h2 h' ret2"
465 assume pivot: "l \<le> p \<and> p \<le> r"
466 assume i_outer: "i < l \<or> r < i"
467 from partition_outer_remains [OF part True] i_outer
468 have "Array.get h a !i = Array.get h1 a ! i" by fastsimp
470 with 1(1) [OF True pivot qs1] pivot i_outer
471 have "Array.get h1 a ! i = Array.get h2 a ! i" by auto
473 with qs2 1(2) [of p h2 h' ret2] True pivot i_outer
474 have "Array.get h2 a ! i = Array.get h' a ! i" by auto
475 ultimately have "Array.get h a ! i= Array.get h' a ! i" by simp
478 unfolding quicksort.simps [of a l r]
479 by (elim crel_ifE crel_bindE crel_assertE crel_returnE) auto
483 lemma quicksort_is_skip:
484 assumes "crel (quicksort a l r) h h' rs"
485 shows "r \<le> l \<longrightarrow> h = h'"
487 unfolding quicksort.simps [of a l r]
488 by (elim crel_ifE crel_returnE) auto
490 lemma quicksort_sorts:
491 assumes "crel (quicksort a l r) h h' rs"
492 assumes l_r_length: "l < Array.length h a" "r < Array.length h a"
493 shows "sorted (subarray l (r + 1) a h')"
495 proof (induct a l r arbitrary: h h' rs rule: quicksort.induct)
496 case (1 a l r h h' rs)
497 note cr = `crel (quicksort a l r) h h' rs`
499 proof (cases "r > l")
501 hence "l \<ge> r + 1 \<or> l = r" by arith
502 with length_remains[OF cr] 1(5) show ?thesis
503 by (auto simp add: subarray_Nil subarray_single)
508 assume part: "crel (partition a l r) h h1 p"
509 assume qs1: "crel (quicksort a l (p - 1)) h1 h2 ()"
510 assume qs2: "crel (quicksort a (p + 1) r) h2 h' ()"
511 from partition_returns_index_in_bounds [OF part True]
512 have pivot: "l\<le> p \<and> p \<le> r" .
513 note length_remains = length_remains[OF qs2] length_remains[OF qs1] partition_length_remains[OF part]
514 from quicksort_outer_remains [OF qs2] quicksort_outer_remains [OF qs1] pivot quicksort_is_skip[OF qs1]
515 have pivot_unchanged: "Array.get h1 a ! p = Array.get h' a ! p" by (cases p, auto)
516 (*-- First of all, by induction hypothesis both sublists are sorted. *)
517 from 1(1)[OF True pivot qs1] length_remains pivot 1(5)
518 have IH1: "sorted (subarray l p a h2)" by (cases p, auto simp add: subarray_Nil)
519 from quicksort_outer_remains [OF qs2] length_remains
520 have left_subarray_remains: "subarray l p a h2 = subarray l p a h'"
521 by (simp add: subarray_eq_samelength_iff)
522 with IH1 have IH1': "sorted (subarray l p a h')" by simp
523 from 1(2)[OF True pivot qs2] pivot 1(5) length_remains
524 have IH2: "sorted (subarray (p + 1) (r + 1) a h')"
525 by (cases "Suc p \<le> r", auto simp add: subarray_Nil)
526 (* -- Secondly, both sublists remain partitioned. *)
527 from partition_partitions[OF part True]
528 have part_conds1: "\<forall>j. j \<in> set (subarray l p a h1) \<longrightarrow> j \<le> Array.get h1 a ! p "
529 and part_conds2: "\<forall>j. j \<in> set (subarray (p + 1) (r + 1) a h1) \<longrightarrow> Array.get h1 a ! p \<le> j"
530 by (auto simp add: all_in_set_subarray_conv)
531 from quicksort_outer_remains [OF qs1] quicksort_permutes [OF qs1] True
532 length_remains 1(5) pivot multiset_of_sublist [of l p "Array.get h1 a" "Array.get h2 a"]
533 have multiset_partconds1: "multiset_of (subarray l p a h2) = multiset_of (subarray l p a h1)"
534 unfolding Array.length_def subarray_def by (cases p, auto)
535 with left_subarray_remains part_conds1 pivot_unchanged
536 have part_conds2': "\<forall>j. j \<in> set (subarray l p a h') \<longrightarrow> j \<le> Array.get h' a ! p"
537 by (simp, subst set_of_multiset_of[symmetric], simp)
538 (* -- These steps are the analogous for the right sublist \<dots> *)
539 from quicksort_outer_remains [OF qs1] length_remains
540 have right_subarray_remains: "subarray (p + 1) (r + 1) a h1 = subarray (p + 1) (r + 1) a h2"
541 by (auto simp add: subarray_eq_samelength_iff)
542 from quicksort_outer_remains [OF qs2] quicksort_permutes [OF qs2] True
543 length_remains 1(5) pivot multiset_of_sublist [of "p + 1" "r + 1" "Array.get h2 a" "Array.get h' a"]
544 have multiset_partconds2: "multiset_of (subarray (p + 1) (r + 1) a h') = multiset_of (subarray (p + 1) (r + 1) a h2)"
545 unfolding Array.length_def subarray_def by auto
546 with right_subarray_remains part_conds2 pivot_unchanged
547 have part_conds1': "\<forall>j. j \<in> set (subarray (p + 1) (r + 1) a h') \<longrightarrow> Array.get h' a ! p \<le> j"
548 by (simp, subst set_of_multiset_of[symmetric], simp)
549 (* -- Thirdly and finally, we show that the array is sorted
550 following from the facts above. *)
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'"
552 by (simp add: subarray_nth_array_Cons, cases "l < p") (auto simp add: subarray_append subarray_Nil)
553 with IH1' IH2 part_conds1' part_conds2' pivot have ?thesis
554 unfolding subarray_def
555 apply (auto simp add: sorted_append sorted_Cons all_in_set_sublist'_conv)
556 by (auto simp add: set_sublist' dest: le_trans [of _ "Array.get h' a ! p"])
558 with True cr show ?thesis
559 unfolding quicksort.simps [of a l r]
560 by (elim crel_ifE crel_returnE crel_bindE crel_assertE) auto
565 lemma quicksort_is_sort:
566 assumes crel: "crel (quicksort a 0 (Array.length h a - 1)) h h' rs"
567 shows "Array.get h' a = sort (Array.get h a)"
568 proof (cases "Array.get h a = []")
570 with quicksort_is_skip[OF crel] show ?thesis
571 unfolding Array.length_def by simp
574 from quicksort_sorts [OF crel] False have "sorted (sublist' 0 (List.length (Array.get h a)) (Array.get h' a))"
575 unfolding Array.length_def subarray_def by auto
576 with length_remains[OF crel] have "sorted (Array.get h' a)"
577 unfolding Array.length_def by simp
578 with quicksort_permutes [OF crel] properties_for_sort show ?thesis by fastsimp
581 subsection {* No Errors in quicksort *}
582 text {* We have proved that quicksort sorts (if no exceptions occur).
583 We will now show that exceptions do not occur. *}
585 lemma success_part1I:
586 assumes "l < Array.length h a" "r < Array.length h a"
587 shows "success (part1 a l r p) h"
589 proof (induct a l r p arbitrary: h rule: part1.induct)
591 thus ?case unfolding part1.simps [of a l r]
592 apply (auto intro!: success_intros del: success_ifI simp add: not_le)
593 apply (auto intro!: crel_intros crel_swapI)
597 lemma success_bindI' [success_intros]: (*FIXME move*)
598 assumes "success f h"
599 assumes "\<And>h' r. crel f h h' r \<Longrightarrow> success (g r) h'"
600 shows "success (f \<guillemotright>= g) h"
601 using assms(1) proof (rule success_crelE)
603 assume "crel f h h' r"
604 moreover with assms(2) have "success (g r) h'" .
605 ultimately show "success (f \<guillemotright>= g) h" by (rule success_bind_crelI)
608 lemma success_partitionI:
609 assumes "l < r" "l < Array.length h a" "r < Array.length h a"
610 shows "success (partition a l r) h"
611 using assms unfolding partition.simps swap_def
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:)
613 apply (frule part_length_remains)
614 apply (frule part_returns_index_in_bounds)
616 apply (frule part_length_remains)
617 apply (frule part_returns_index_in_bounds)
619 apply (frule part_length_remains)
623 lemma success_quicksortI:
624 assumes "l < Array.length h a" "r < Array.length h a"
625 shows "success (quicksort a l r) h"
627 proof (induct a l r arbitrary: h rule: quicksort.induct)
630 unfolding quicksort.simps [of a l ri]
631 apply (auto intro!: success_ifI success_bindI' success_returnI success_nthI success_updI success_assertI success_partitionI)
632 apply (frule partition_returns_index_in_bounds)
634 apply (frule partition_returns_index_in_bounds)
636 apply (auto elim!: crel_assertE dest!: partition_length_remains length_remains)
637 apply (subgoal_tac "Suc r \<le> ri \<or> r = ri")
640 unfolding quicksort.simps [of a "Suc ri" ri]
641 apply (auto intro!: success_ifI success_returnI)
646 subsection {* Example *}
648 definition "qsort a = do {
649 k \<leftarrow> Array.len a;
650 quicksort a 0 (k - 1);
654 code_reserved SML upto
656 ML {* @{code qsort} (Array.fromList [42, 2, 3, 5, 0, 1705, 8, 3, 15]) () *}
658 export_code qsort checking SML SML_imp OCaml? OCaml_imp? Haskell? Scala? Scala_imp?