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