| author | wenzelm | 
| Sun, 15 Nov 2009 00:23:26 +0100 | |
| changeset 33689 | d0a9ce721e0c | 
| parent 33640 | 0d82107dc07a | 
| child 33945 | 8493ed132fed | 
| child 33968 | f94fb13ecbb3 | 
| permissions | -rw-r--r-- | 
| 13462 | 1 | (* Title: HOL/List.thy | 
| 2 | Author: Tobias Nipkow | |
| 923 | 3 | *) | 
| 4 | ||
| 13114 | 5 | header {* The datatype of finite lists *}
 | 
| 13122 | 6 | |
| 15131 | 7 | theory List | 
| 33593 | 8 | imports Plain Presburger ATP_Linkup Recdef | 
| 31055 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 9 | uses ("Tools/list_code.ML")
 | 
| 15131 | 10 | begin | 
| 923 | 11 | |
| 13142 | 12 | datatype 'a list = | 
| 13366 | 13 |     Nil    ("[]")
 | 
| 14 | | Cons 'a "'a list" (infixr "#" 65) | |
| 923 | 15 | |
| 15392 | 16 | subsection{*Basic list processing functions*}
 | 
| 15302 | 17 | |
| 923 | 18 | consts | 
| 13366 | 19 |   filter:: "('a => bool) => 'a list => 'a list"
 | 
| 20 | concat:: "'a list list => 'a list" | |
| 21 |   foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b"
 | |
| 22 |   foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b"
 | |
| 23 | hd:: "'a list => 'a" | |
| 24 | tl:: "'a list => 'a list" | |
| 25 | last:: "'a list => 'a" | |
| 26 | butlast :: "'a list => 'a list" | |
| 27 | set :: "'a list => 'a set" | |
| 28 |   map :: "('a=>'b) => ('a list => 'b list)"
 | |
| 23096 | 29 | listsum :: "'a list => 'a::monoid_add" | 
| 13366 | 30 | list_update :: "'a list => nat => 'a => 'a list" | 
| 31 | take:: "nat => 'a list => 'a list" | |
| 32 | drop:: "nat => 'a list => 'a list" | |
| 33 |   takeWhile :: "('a => bool) => 'a list => 'a list"
 | |
| 34 |   dropWhile :: "('a => bool) => 'a list => 'a list"
 | |
| 35 | rev :: "'a list => 'a list" | |
| 36 |   zip :: "'a list => 'b list => ('a * 'b) list"
 | |
| 15425 | 37 |   upt :: "nat => nat => nat list" ("(1[_..</_'])")
 | 
| 13366 | 38 | remdups :: "'a list => 'a list" | 
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 39 | remove1 :: "'a => 'a list => 'a list" | 
| 27693 | 40 | removeAll :: "'a => 'a list => 'a list" | 
| 13366 | 41 | "distinct":: "'a list => bool" | 
| 42 | replicate :: "nat => 'a => 'a list" | |
| 19390 | 43 | splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" | 
| 15302 | 44 | |
| 923 | 45 | |
| 13146 | 46 | nonterminals lupdbinds lupdbind | 
| 5077 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
4643diff
changeset | 47 | |
| 923 | 48 | syntax | 
| 13366 | 49 |   -- {* list Enumeration *}
 | 
| 50 |   "@list" :: "args => 'a list"    ("[(_)]")
 | |
| 923 | 51 | |
| 13366 | 52 |   -- {* Special syntax for filter *}
 | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 53 |   "@filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
 | 
| 923 | 54 | |
| 13366 | 55 |   -- {* list update *}
 | 
| 56 |   "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
 | |
| 57 |   "" :: "lupdbind => lupdbinds"    ("_")
 | |
| 58 |   "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
 | |
| 59 |   "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
 | |
| 5077 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
4643diff
changeset | 60 | |
| 923 | 61 | translations | 
| 13366 | 62 | "[x, xs]" == "x#[xs]" | 
| 63 | "[x]" == "x#[]" | |
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 64 | "[x<-xs . P]"== "filter (%x. P) xs" | 
| 923 | 65 | |
| 13366 | 66 | "_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs" | 
| 67 | "xs[i:=x]" == "list_update xs i x" | |
| 5077 
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
 nipkow parents: 
4643diff
changeset | 68 | |
| 5427 | 69 | |
| 12114 
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
 wenzelm parents: 
10832diff
changeset | 70 | syntax (xsymbols) | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 71 |   "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
 | 
| 14565 | 72 | syntax (HTML output) | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 73 |   "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
 | 
| 3342 
ec3b55fcb165
New operator "lists" for formalizing sets of lists
 paulson parents: 
3320diff
changeset | 74 | |
| 
ec3b55fcb165
New operator "lists" for formalizing sets of lists
 paulson parents: 
3320diff
changeset | 75 | |
| 13142 | 76 | text {*
 | 
| 14589 | 77 |   Function @{text size} is overloaded for all datatypes. Users may
 | 
| 13366 | 78 |   refer to the list version as @{text length}. *}
 | 
| 13142 | 79 | |
| 19363 | 80 | abbreviation | 
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 81 | length :: "'a list => nat" where | 
| 19363 | 82 | "length == size" | 
| 15302 | 83 | |
| 5183 | 84 | primrec | 
| 15307 | 85 | "hd(x#xs) = x" | 
| 86 | ||
| 5183 | 87 | primrec | 
| 15307 | 88 | "tl([]) = []" | 
| 89 | "tl(x#xs) = xs" | |
| 90 | ||
| 5183 | 91 | primrec | 
| 15307 | 92 | "last(x#xs) = (if xs=[] then x else last xs)" | 
| 93 | ||
| 5183 | 94 | primrec | 
| 15307 | 95 | "butlast []= []" | 
| 96 | "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" | |
| 97 | ||
| 5183 | 98 | primrec | 
| 15307 | 99 |   "set [] = {}"
 | 
| 100 | "set (x#xs) = insert x (set xs)" | |
| 101 | ||
| 5183 | 102 | primrec | 
| 15307 | 103 | "map f [] = []" | 
| 104 | "map f (x#xs) = f(x)#map f xs" | |
| 105 | ||
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 106 | primrec | 
| 25559 | 107 | append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) | 
| 108 | where | |
| 109 | append_Nil:"[] @ ys = ys" | |
| 110 | | append_Cons: "(x#xs) @ ys = x # xs @ ys" | |
| 15307 | 111 | |
| 5183 | 112 | primrec | 
| 15307 | 113 | "rev([]) = []" | 
| 114 | "rev(x#xs) = rev(xs) @ [x]" | |
| 115 | ||
| 5183 | 116 | primrec | 
| 15307 | 117 | "filter P [] = []" | 
| 118 | "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" | |
| 119 | ||
| 5183 | 120 | primrec | 
| 15307 | 121 | foldl_Nil:"foldl f a [] = a" | 
| 122 | foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" | |
| 123 | ||
| 8000 | 124 | primrec | 
| 15307 | 125 | "foldr f [] a = a" | 
| 126 | "foldr f (x#xs) a = f x (foldr f xs a)" | |
| 127 | ||
| 5183 | 128 | primrec | 
| 15307 | 129 | "concat([]) = []" | 
| 130 | "concat(x#xs) = x @ concat(xs)" | |
| 131 | ||
| 5183 | 132 | primrec | 
| 23096 | 133 | "listsum [] = 0" | 
| 134 | "listsum (x # xs) = x + listsum xs" | |
| 135 | ||
| 136 | primrec | |
| 15307 | 137 | drop_Nil:"drop n [] = []" | 
| 138 | drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" | |
| 139 |   -- {*Warning: simpset does not contain this definition, but separate
 | |
| 140 |        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | |
| 141 | ||
| 5183 | 142 | primrec | 
| 15307 | 143 | take_Nil:"take n [] = []" | 
| 144 | take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" | |
| 145 |   -- {*Warning: simpset does not contain this definition, but separate
 | |
| 146 |        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | |
| 147 | ||
| 29822 | 148 | primrec nth :: "'a list => nat => 'a" (infixl "!" 100) where | 
| 149 | nth_Cons: "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)" | |
| 15307 | 150 |   -- {*Warning: simpset does not contain this definition, but separate
 | 
| 151 |        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
 | |
| 152 | ||
| 5183 | 153 | primrec | 
| 15307 | 154 | "[][i:=v] = []" | 
| 155 | "(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])" | |
| 156 | ||
| 157 | primrec | |
| 158 | "takeWhile P [] = []" | |
| 159 | "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" | |
| 160 | ||
| 5183 | 161 | primrec | 
| 15307 | 162 | "dropWhile P [] = []" | 
| 163 | "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" | |
| 164 | ||
| 5183 | 165 | primrec | 
| 15307 | 166 | "zip xs [] = []" | 
| 167 | zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)" | |
| 168 |   -- {*Warning: simpset does not contain this definition, but separate
 | |
| 169 |        theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
 | |
| 170 | ||
| 5427 | 171 | primrec | 
| 15425 | 172 | upt_0: "[i..<0] = []" | 
| 173 | upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])" | |
| 15307 | 174 | |
| 5183 | 175 | primrec | 
| 15307 | 176 | "distinct [] = True" | 
| 177 | "distinct (x#xs) = (x ~: set xs \<and> distinct xs)" | |
| 178 | ||
| 5183 | 179 | primrec | 
| 15307 | 180 | "remdups [] = []" | 
| 181 | "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" | |
| 182 | ||
| 5183 | 183 | primrec | 
| 15307 | 184 | "remove1 x [] = []" | 
| 185 | "remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)" | |
| 186 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 187 | primrec | 
| 27693 | 188 | "removeAll x [] = []" | 
| 189 | "removeAll x (y#xs) = (if x=y then removeAll x xs else y # removeAll x xs)" | |
| 190 | ||
| 191 | primrec | |
| 15307 | 192 | replicate_0: "replicate 0 x = []" | 
| 193 | replicate_Suc: "replicate (Suc n) x = x # replicate n x" | |
| 194 | ||
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 195 | definition | 
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 196 | rotate1 :: "'a list \<Rightarrow> 'a list" where | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 197 | "rotate1 xs = (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])" | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 198 | |
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 199 | definition | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 200 | rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where | 
| 30971 | 201 | "rotate n = rotate1 ^^ n" | 
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 202 | |
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 203 | definition | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 204 |   list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where
 | 
| 28562 | 205 | [code del]: "list_all2 P xs ys = | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 206 | (length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))" | 
| 21404 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 207 | |
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 208 | definition | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 209 | sublist :: "'a list => nat set => 'a list" where | 
| 
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
 wenzelm parents: 
21211diff
changeset | 210 | "sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))" | 
| 17086 | 211 | |
| 212 | primrec | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 213 | "splice [] ys = ys" | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 214 | "splice (x#xs) ys = (if ys=[] then x#xs else x # hd ys # splice xs (tl ys))" | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 215 |     -- {*Warning: simpset does not contain the second eqn but a derived one. *}
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 216 | |
| 26771 | 217 | text{*
 | 
| 218 | \begin{figure}[htbp]
 | |
| 219 | \fbox{
 | |
| 220 | \begin{tabular}{l}
 | |
| 27381 | 221 | @{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\
 | 
| 222 | @{lemma "length [a,b,c] = 3" by simp}\\
 | |
| 223 | @{lemma "set [a,b,c] = {a,b,c}" by simp}\\
 | |
| 224 | @{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\
 | |
| 225 | @{lemma "rev [a,b,c] = [c,b,a]" by simp}\\
 | |
| 226 | @{lemma "hd [a,b,c,d] = a" by simp}\\
 | |
| 227 | @{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\
 | |
| 228 | @{lemma "last [a,b,c,d] = d" by simp}\\
 | |
| 229 | @{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\
 | |
| 230 | @{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\
 | |
| 231 | @{lemma "concat [[a,b],[c,d,e],[],[f]] = [a,b,c,d,e,f]" by simp}\\
 | |
| 232 | @{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\
 | |
| 233 | @{lemma "foldr f [a,b,c] x = f a (f b (f c x))" by simp}\\
 | |
| 234 | @{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\
 | |
| 235 | @{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\
 | |
| 236 | @{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\
 | |
| 237 | @{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\
 | |
| 238 | @{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\
 | |
| 239 | @{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\
 | |
| 240 | @{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\
 | |
| 241 | @{lemma "drop 6 [a,b,c,d] = []" by simp}\\
 | |
| 242 | @{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\
 | |
| 243 | @{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\
 | |
| 244 | @{lemma "distinct [2,0,1::nat]" by simp}\\
 | |
| 245 | @{lemma "remdups [2,0,2,1::nat,2] = [0,1,2]" by simp}\\
 | |
| 246 | @{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\
 | |
| 27693 | 247 | @{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\
 | 
| 27381 | 248 | @{lemma "nth [a,b,c,d] 2 = c" by simp}\\
 | 
| 249 | @{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\
 | |
| 250 | @{lemma "sublist [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:sublist_def)}\\
 | |
| 251 | @{lemma "rotate1 [a,b,c,d] = [b,c,d,a]" by (simp add:rotate1_def)}\\
 | |
| 252 | @{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate1_def rotate_def nat_number)}\\
 | |
| 253 | @{lemma "replicate 4 a = [a,a,a,a]" by (simp add:nat_number)}\\
 | |
| 254 | @{lemma "[2..<5] = [2,3,4]" by (simp add:nat_number)}\\
 | |
| 255 | @{lemma "listsum [1,2,3::nat] = 6" by simp}
 | |
| 26771 | 256 | \end{tabular}}
 | 
| 257 | \caption{Characteristic examples}
 | |
| 258 | \label{fig:Characteristic}
 | |
| 259 | \end{figure}
 | |
| 29927 | 260 | Figure~\ref{fig:Characteristic} shows characteristic examples
 | 
| 26771 | 261 | that should give an intuitive understanding of the above functions. | 
| 262 | *} | |
| 263 | ||
| 24616 | 264 | text{* The following simple sort functions are intended for proofs,
 | 
| 265 | not for efficient implementations. *} | |
| 266 | ||
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 267 | context linorder | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 268 | begin | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 269 | |
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 270 | fun sorted :: "'a list \<Rightarrow> bool" where | 
| 24697 | 271 | "sorted [] \<longleftrightarrow> True" | | 
| 272 | "sorted [x] \<longleftrightarrow> True" | | |
| 25062 | 273 | "sorted (x#y#zs) \<longleftrightarrow> x <= y \<and> sorted (y#zs)" | 
| 24697 | 274 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 275 | primrec insort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 276 | "insort_key f x [] = [x]" | | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 277 | "insort_key f x (y#ys) = (if f x \<le> f y then (x#y#ys) else y#(insort_key f x ys))" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 278 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 279 | primrec sort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list" where
 | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 280 | "sort_key f [] = []" | | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 281 | "sort_key f (x#xs) = insort_key f x (sort_key f xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 282 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 283 | abbreviation "sort \<equiv> sort_key (\<lambda>x. x)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 284 | abbreviation "insort \<equiv> insort_key (\<lambda>x. x)" | 
| 24616 | 285 | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 286 | end | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 287 | |
| 24616 | 288 | |
| 23388 | 289 | subsubsection {* List comprehension *}
 | 
| 23192 | 290 | |
| 24349 | 291 | text{* Input syntax for Haskell-like list comprehension notation.
 | 
| 292 | Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
 | |
| 293 | the list of all pairs of distinct elements from @{text xs} and @{text ys}.
 | |
| 294 | The syntax is as in Haskell, except that @{text"|"} becomes a dot
 | |
| 295 | (like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
 | |
| 296 | \verb![e| x <- xs, ...]!. | |
| 297 | ||
| 298 | The qualifiers after the dot are | |
| 299 | \begin{description}
 | |
| 300 | \item[generators] @{text"p \<leftarrow> xs"},
 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 301 |  where @{text p} is a pattern and @{text xs} an expression of list type, or
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 302 | \item[guards] @{text"b"}, where @{text b} is a boolean expression.
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 303 | %\item[local bindings] @ {text"let x = e"}.
 | 
| 24349 | 304 | \end{description}
 | 
| 23240 | 305 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 306 | Just like in Haskell, list comprehension is just a shorthand. To avoid | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 307 | misunderstandings, the translation into desugared form is not reversed | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 308 | upon output. Note that the translation of @{text"[e. x \<leftarrow> xs]"} is
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 309 | optmized to @{term"map (%x. e) xs"}.
 | 
| 23240 | 310 | |
| 24349 | 311 | It is easy to write short list comprehensions which stand for complex | 
| 312 | expressions. During proofs, they may become unreadable (and | |
| 313 | mangled). In such cases it can be advisable to introduce separate | |
| 314 | definitions for the list comprehensions in question. *} | |
| 315 | ||
| 23209 | 316 | (* | 
| 23240 | 317 | Proper theorem proving support would be nice. For example, if | 
| 23192 | 318 | @{text"set[f x y. x \<leftarrow> xs, y \<leftarrow> ys, P x y]"}
 | 
| 319 | produced something like | |
| 23209 | 320 | @{term"{z. EX x: set xs. EX y:set ys. P x y \<and> z = f x y}"}.
 | 
| 321 | *) | |
| 322 | ||
| 23240 | 323 | nonterminals lc_qual lc_quals | 
| 23192 | 324 | |
| 325 | syntax | |
| 23240 | 326 | "_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list"  ("[_ . __")
 | 
| 24349 | 327 | "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ <- _")
 | 
| 23240 | 328 | "_lc_test" :: "bool \<Rightarrow> lc_qual" ("_")
 | 
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 329 | (*"_lc_let" :: "letbinds => lc_qual"  ("let _")*)
 | 
| 23240 | 330 | "_lc_end" :: "lc_quals" ("]")
 | 
| 331 | "_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __")
 | |
| 24349 | 332 | "_lc_abs" :: "'a => 'b list => 'b list" | 
| 23192 | 333 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 334 | (* These are easier than ML code but cannot express the optimized | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 335 | translation of [e. p<-xs] | 
| 23192 | 336 | translations | 
| 24349 | 337 | "[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)" | 
| 23240 | 338 | "_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)" | 
| 24349 | 339 | => "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)" | 
| 23240 | 340 | "[e. P]" => "if P then [e] else []" | 
| 341 | "_listcompr e (_lc_test P) (_lc_quals Q Qs)" | |
| 342 | => "if P then (_listcompr e Q Qs) else []" | |
| 24349 | 343 | "_listcompr e (_lc_let b) (_lc_quals Q Qs)" | 
| 344 | => "_Let b (_listcompr e Q Qs)" | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 345 | *) | 
| 23240 | 346 | |
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 347 | syntax (xsymbols) | 
| 24349 | 348 | "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
 | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 349 | syntax (HTML output) | 
| 24349 | 350 | "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
 | 
| 351 | ||
| 352 | parse_translation (advanced) {*
 | |
| 353 | let | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 354 |   val NilC = Syntax.const @{const_name Nil};
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 355 |   val ConsC = Syntax.const @{const_name Cons};
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 356 |   val mapC = Syntax.const @{const_name map};
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 357 |   val concatC = Syntax.const @{const_name concat};
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 358 |   val IfC = Syntax.const @{const_name If};
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 359 | fun singl x = ConsC $ x $ NilC; | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 360 | |
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 361 | fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *) | 
| 24349 | 362 | let | 
| 29281 | 363 | val x = Free (Name.variant (fold Term.add_free_names [p, e] []) "x", dummyT); | 
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 364 | val e = if opti then singl e else e; | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 365 | val case1 = Syntax.const "_case1" $ p $ e; | 
| 24349 | 366 | val case2 = Syntax.const "_case1" $ Syntax.const Term.dummy_patternN | 
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 367 | $ NilC; | 
| 24349 | 368 | val cs = Syntax.const "_case2" $ case1 $ case2 | 
| 31784 | 369 | val ft = DatatypeCase.case_tr false Datatype.info_of_constr | 
| 24349 | 370 | ctxt [x, cs] | 
| 371 | in lambda x ft end; | |
| 372 | ||
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 373 | fun abs_tr ctxt (p as Free(s,T)) e opti = | 
| 24349 | 374 | let val thy = ProofContext.theory_of ctxt; | 
| 375 | val s' = Sign.intern_const thy s | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 376 | in if Sign.declared_const thy s' | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 377 | then (pat_tr ctxt p e opti, false) | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 378 | else (lambda p e, true) | 
| 24349 | 379 | end | 
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 380 | | abs_tr ctxt p e opti = (pat_tr ctxt p e opti, false); | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 381 | |
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 382 |   fun lc_tr ctxt [e, Const("_lc_test",_)$b, qs] =
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 383 |         let val res = case qs of Const("_lc_end",_) => singl e
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 384 |                       | Const("_lc_quals",_)$q$qs => lc_tr ctxt [e,q,qs];
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 385 | in IfC $ b $ res $ NilC end | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 386 |     | lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_end",_)] =
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 387 | (case abs_tr ctxt p e true of | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 388 | (f,true) => mapC $ f $ es | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 389 | | (f, false) => concatC $ (mapC $ f $ es)) | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 390 |     | lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_quals",_)$q$qs] =
 | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 391 | let val e' = lc_tr ctxt [e,q,qs]; | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 392 | in concatC $ (mapC $ (fst(abs_tr ctxt p e' false)) $ es) end | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 393 | |
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 394 | in [("_listcompr", lc_tr)] end
 | 
| 24349 | 395 | *} | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 396 | |
| 23240 | 397 | (* | 
| 398 | term "[(x,y,z). b]" | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 399 | term "[(x,y,z). x\<leftarrow>xs]" | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 400 | term "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]" | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 401 | term "[(x,y,z). x<a, x>b]" | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 402 | term "[(x,y,z). x\<leftarrow>xs, x>b]" | 
| 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 403 | term "[(x,y,z). x<a, x\<leftarrow>xs]" | 
| 24349 | 404 | term "[(x,y). Cons True x \<leftarrow> xs]" | 
| 405 | term "[(x,y,z). Cons x [] \<leftarrow> xs]" | |
| 23240 | 406 | term "[(x,y,z). x<a, x>b, x=d]" | 
| 407 | term "[(x,y,z). x<a, x>b, y\<leftarrow>ys]" | |
| 408 | term "[(x,y,z). x<a, x\<leftarrow>xs,y>b]" | |
| 409 | term "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]" | |
| 410 | term "[(x,y,z). x\<leftarrow>xs, x>b, y<a]" | |
| 411 | term "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]" | |
| 412 | term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]" | |
| 413 | term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]" | |
| 24349 | 414 | term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]" | 
| 23192 | 415 | *) | 
| 416 | ||
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 417 | subsubsection {* @{const Nil} and @{const Cons} *}
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 418 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 419 | lemma not_Cons_self [simp]: | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 420 | "xs \<noteq> x # xs" | 
| 13145 | 421 | by (induct xs) auto | 
| 13114 | 422 | |
| 13142 | 423 | lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric] | 
| 13114 | 424 | |
| 13142 | 425 | lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" | 
| 13145 | 426 | by (induct xs) auto | 
| 13114 | 427 | |
| 13142 | 428 | lemma length_induct: | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 429 | "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs" | 
| 17589 | 430 | by (rule measure_induct [of length]) iprover | 
| 13114 | 431 | |
| 432 | ||
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 433 | subsubsection {* @{const length} *}
 | 
| 13114 | 434 | |
| 13142 | 435 | text {*
 | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 436 |   Needs to come before @{text "@"} because of theorem @{text
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 437 | append_eq_append_conv}. | 
| 13142 | 438 | *} | 
| 13114 | 439 | |
| 13142 | 440 | lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" | 
| 13145 | 441 | by (induct xs) auto | 
| 13114 | 442 | |
| 13142 | 443 | lemma length_map [simp]: "length (map f xs) = length xs" | 
| 13145 | 444 | by (induct xs) auto | 
| 13114 | 445 | |
| 13142 | 446 | lemma length_rev [simp]: "length (rev xs) = length xs" | 
| 13145 | 447 | by (induct xs) auto | 
| 13114 | 448 | |
| 13142 | 449 | lemma length_tl [simp]: "length (tl xs) = length xs - 1" | 
| 13145 | 450 | by (cases xs) auto | 
| 13114 | 451 | |
| 13142 | 452 | lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" | 
| 13145 | 453 | by (induct xs) auto | 
| 13114 | 454 | |
| 13142 | 455 | lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" | 
| 13145 | 456 | by (induct xs) auto | 
| 13114 | 457 | |
| 23479 | 458 | lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0" | 
| 459 | by auto | |
| 460 | ||
| 13114 | 461 | lemma length_Suc_conv: | 
| 13145 | 462 | "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" | 
| 463 | by (induct xs) auto | |
| 13142 | 464 | |
| 14025 | 465 | lemma Suc_length_conv: | 
| 466 | "(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" | |
| 14208 | 467 | apply (induct xs, simp, simp) | 
| 14025 | 468 | apply blast | 
| 469 | done | |
| 470 | ||
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 471 | lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False" | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 472 | by (induct xs) auto | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 473 | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 474 | lemma list_induct2 [consumes 1, case_names Nil Cons]: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 475 | "length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow> | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 476 | (\<And>x xs y ys. length xs = length ys \<Longrightarrow> P xs ys \<Longrightarrow> P (x#xs) (y#ys)) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 477 | \<Longrightarrow> P xs ys" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 478 | proof (induct xs arbitrary: ys) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 479 | case Nil then show ?case by simp | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 480 | next | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 481 | case (Cons x xs ys) then show ?case by (cases ys) simp_all | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 482 | qed | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 483 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 484 | lemma list_induct3 [consumes 2, case_names Nil Cons]: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 485 | "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow> | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 486 | (\<And>x xs y ys z zs. length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P xs ys zs \<Longrightarrow> P (x#xs) (y#ys) (z#zs)) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 487 | \<Longrightarrow> P xs ys zs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 488 | proof (induct xs arbitrary: ys zs) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 489 | case Nil then show ?case by simp | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 490 | next | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 491 | case (Cons x xs ys zs) then show ?case by (cases ys, simp_all) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 492 | (cases zs, simp_all) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 493 | qed | 
| 13114 | 494 | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 495 | lemma list_induct2': | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 496 | "\<lbrakk> P [] []; | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 497 | \<And>x xs. P (x#xs) []; | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 498 | \<And>y ys. P [] (y#ys); | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 499 | \<And>x xs y ys. P xs ys \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk> | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 500 | \<Longrightarrow> P xs ys" | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 501 | by (induct xs arbitrary: ys) (case_tac x, auto)+ | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 502 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 503 | lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False" | 
| 24349 | 504 | by (rule Eq_FalseI) auto | 
| 24037 | 505 | |
| 506 | simproc_setup list_neq ("(xs::'a list) = ys") = {*
 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 507 | (* | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 508 | Reduces xs=ys to False if xs and ys cannot be of the same length. | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 509 | This is the case if the atomic sublists of one are a submultiset | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 510 | of those of the other list and there are fewer Cons's in one than the other. | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 511 | *) | 
| 24037 | 512 | |
| 513 | let | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 514 | |
| 29856 | 515 | fun len (Const(@{const_name Nil},_)) acc = acc
 | 
| 516 |   | len (Const(@{const_name Cons},_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
 | |
| 517 |   | len (Const(@{const_name append},_) $ xs $ ys) acc = len xs (len ys acc)
 | |
| 518 |   | len (Const(@{const_name rev},_) $ xs) acc = len xs acc
 | |
| 519 |   | len (Const(@{const_name map},_) $ _ $ xs) acc = len xs acc
 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 520 | | len t (ts,n) = (t::ts,n); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 521 | |
| 24037 | 522 | fun list_neq _ ss ct = | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 523 | let | 
| 24037 | 524 | val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct; | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 525 | val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 526 | fun prove_neq() = | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 527 | let | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 528 | val Type(_,listT::_) = eqT; | 
| 22994 | 529 | val size = HOLogic.size_const listT; | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 530 | val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 531 | val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len); | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 532 | val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len | 
| 22633 | 533 |           (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));
 | 
| 534 |       in SOME (thm RS @{thm neq_if_length_neq}) end
 | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 535 | in | 
| 23214 | 536 | if m < n andalso submultiset (op aconv) (ls,rs) orelse | 
| 537 | n < m andalso submultiset (op aconv) (rs,ls) | |
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 538 | then prove_neq() else NONE | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 539 | end; | 
| 24037 | 540 | in list_neq end; | 
| 22143 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 541 | *} | 
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 542 | |
| 
cf58486ca11b
Added simproc list_neq (prompted by an application)
 nipkow parents: 
21911diff
changeset | 543 | |
| 15392 | 544 | subsubsection {* @{text "@"} -- append *}
 | 
| 13114 | 545 | |
| 13142 | 546 | lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" | 
| 13145 | 547 | by (induct xs) auto | 
| 13114 | 548 | |
| 13142 | 549 | lemma append_Nil2 [simp]: "xs @ [] = xs" | 
| 13145 | 550 | by (induct xs) auto | 
| 3507 | 551 | |
| 13142 | 552 | lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" | 
| 13145 | 553 | by (induct xs) auto | 
| 13114 | 554 | |
| 13142 | 555 | lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" | 
| 13145 | 556 | by (induct xs) auto | 
| 13114 | 557 | |
| 13142 | 558 | lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" | 
| 13145 | 559 | by (induct xs) auto | 
| 13114 | 560 | |
| 13142 | 561 | lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" | 
| 13145 | 562 | by (induct xs) auto | 
| 13114 | 563 | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 564 | lemma append_eq_append_conv [simp, noatp]: | 
| 24526 | 565 | "length xs = length ys \<or> length us = length vs | 
| 13883 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 566 | ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)" | 
| 24526 | 567 | apply (induct xs arbitrary: ys) | 
| 14208 | 568 | apply (case_tac ys, simp, force) | 
| 569 | apply (case_tac ys, force, simp) | |
| 13145 | 570 | done | 
| 13142 | 571 | |
| 24526 | 572 | lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) = | 
| 573 | (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)" | |
| 574 | apply (induct xs arbitrary: ys zs ts) | |
| 14495 | 575 | apply fastsimp | 
| 576 | apply(case_tac zs) | |
| 577 | apply simp | |
| 578 | apply fastsimp | |
| 579 | done | |
| 580 | ||
| 13142 | 581 | lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)" | 
| 13145 | 582 | by simp | 
| 13142 | 583 | |
| 584 | lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" | |
| 13145 | 585 | by simp | 
| 13114 | 586 | |
| 13142 | 587 | lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)" | 
| 13145 | 588 | by simp | 
| 13114 | 589 | |
| 13142 | 590 | lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" | 
| 13145 | 591 | using append_same_eq [of _ _ "[]"] by auto | 
| 3507 | 592 | |
| 13142 | 593 | lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" | 
| 13145 | 594 | using append_same_eq [of "[]"] by auto | 
| 13114 | 595 | |
| 24286 
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
 paulson parents: 
24219diff
changeset | 596 | lemma hd_Cons_tl [simp,noatp]: "xs \<noteq> [] ==> hd xs # tl xs = xs" | 
| 13145 | 597 | by (induct xs) auto | 
| 13114 | 598 | |
| 13142 | 599 | lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" | 
| 13145 | 600 | by (induct xs) auto | 
| 13114 | 601 | |
| 13142 | 602 | lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" | 
| 13145 | 603 | by (simp add: hd_append split: list.split) | 
| 13114 | 604 | |
| 13142 | 605 | lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)" | 
| 13145 | 606 | by (simp split: list.split) | 
| 13114 | 607 | |
| 13142 | 608 | lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" | 
| 13145 | 609 | by (simp add: tl_append split: list.split) | 
| 13114 | 610 | |
| 611 | ||
| 14300 | 612 | lemma Cons_eq_append_conv: "x#xs = ys@zs = | 
| 613 | (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))" | |
| 614 | by(cases ys) auto | |
| 615 | ||
| 15281 | 616 | lemma append_eq_Cons_conv: "(ys@zs = x#xs) = | 
| 617 | (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))" | |
| 618 | by(cases ys) auto | |
| 619 | ||
| 14300 | 620 | |
| 13142 | 621 | text {* Trivial rules for solving @{text "@"}-equations automatically. *}
 | 
| 13114 | 622 | |
| 623 | lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" | |
| 13145 | 624 | by simp | 
| 13114 | 625 | |
| 13142 | 626 | lemma Cons_eq_appendI: | 
| 13145 | 627 | "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs" | 
| 628 | by (drule sym) simp | |
| 13114 | 629 | |
| 13142 | 630 | lemma append_eq_appendI: | 
| 13145 | 631 | "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us" | 
| 632 | by (drule sym) simp | |
| 13114 | 633 | |
| 634 | ||
| 13142 | 635 | text {*
 | 
| 13145 | 636 | Simplification procedure for all list equalities. | 
| 637 | Currently only tries to rearrange @{text "@"} to see if
 | |
| 638 | - both lists end in a singleton list, | |
| 639 | - or both lists end in the same list. | |
| 13142 | 640 | *} | 
| 641 | ||
| 26480 | 642 | ML {*
 | 
| 3507 | 643 | local | 
| 644 | ||
| 29856 | 645 | fun last (cons as Const(@{const_name Cons},_) $ _ $ xs) =
 | 
| 646 |   (case xs of Const(@{const_name Nil},_) => cons | _ => last xs)
 | |
| 647 |   | last (Const(@{const_name append},_) $ _ $ ys) = last ys
 | |
| 13462 | 648 | | last t = t; | 
| 13114 | 649 | |
| 29856 | 650 | fun list1 (Const(@{const_name Cons},_) $ _ $ Const(@{const_name Nil},_)) = true
 | 
| 13462 | 651 | | list1 _ = false; | 
| 13114 | 652 | |
| 29856 | 653 | fun butlast ((cons as Const(@{const_name Cons},_) $ x) $ xs) =
 | 
| 654 |   (case xs of Const(@{const_name Nil},_) => xs | _ => cons $ butlast xs)
 | |
| 655 |   | butlast ((app as Const(@{const_name append},_) $ xs) $ ys) = app $ butlast ys
 | |
| 656 |   | butlast xs = Const(@{const_name Nil},fastype_of xs);
 | |
| 13114 | 657 | |
| 22633 | 658 | val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc},
 | 
| 659 |   @{thm append_Nil}, @{thm append_Cons}];
 | |
| 16973 | 660 | |
| 20044 
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
 wenzelm parents: 
19890diff
changeset | 661 | fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) = | 
| 13462 | 662 | let | 
| 663 | val lastl = last lhs and lastr = last rhs; | |
| 664 | fun rearr conv = | |
| 665 | let | |
| 666 | val lhs1 = butlast lhs and rhs1 = butlast rhs; | |
| 667 | val Type(_,listT::_) = eqT | |
| 668 | val appT = [listT,listT] ---> listT | |
| 29856 | 669 |         val app = Const(@{const_name append},appT)
 | 
| 13462 | 670 | val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) | 
| 13480 
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
 wenzelm parents: 
13462diff
changeset | 671 | val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); | 
| 20044 
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
 wenzelm parents: 
19890diff
changeset | 672 | val thm = Goal.prove (Simplifier.the_context ss) [] [] eq | 
| 17877 
67d5ab1cb0d8
Simplifier.inherit_context instead of Simplifier.inherit_bounds;
 wenzelm parents: 
17830diff
changeset | 673 | (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1)); | 
| 15531 | 674 | in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; | 
| 13114 | 675 | |
| 13462 | 676 | in | 
| 22633 | 677 |     if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
 | 
| 678 |     else if lastl aconv lastr then rearr @{thm append_same_eq}
 | |
| 15531 | 679 | else NONE | 
| 13462 | 680 | end; | 
| 681 | ||
| 13114 | 682 | in | 
| 13462 | 683 | |
| 684 | val list_eq_simproc = | |
| 32010 | 685 |   Simplifier.simproc @{theory} "list_eq" ["(xs::'a list) = ys"] (K list_eq);
 | 
| 13462 | 686 | |
| 13114 | 687 | end; | 
| 688 | ||
| 689 | Addsimprocs [list_eq_simproc]; | |
| 690 | *} | |
| 691 | ||
| 692 | ||
| 15392 | 693 | subsubsection {* @{text map} *}
 | 
| 13114 | 694 | |
| 13142 | 695 | lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs" | 
| 13145 | 696 | by (induct xs) simp_all | 
| 13114 | 697 | |
| 13142 | 698 | lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" | 
| 13145 | 699 | by (rule ext, induct_tac xs) auto | 
| 13114 | 700 | |
| 13142 | 701 | lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" | 
| 13145 | 702 | by (induct xs) auto | 
| 13114 | 703 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 704 | lemma map_map [simp]: "map f (map g xs) = map (f \<circ> g) xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 705 | by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 706 | |
| 13142 | 707 | lemma rev_map: "rev (map f xs) = map f (rev xs)" | 
| 13145 | 708 | by (induct xs) auto | 
| 13114 | 709 | |
| 13737 | 710 | lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)" | 
| 711 | by (induct xs) auto | |
| 712 | ||
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19623diff
changeset | 713 | lemma map_cong [fundef_cong, recdef_cong]: | 
| 13145 | 714 | "xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys" | 
| 715 | -- {* a congruence rule for @{text map} *}
 | |
| 13737 | 716 | by simp | 
| 13114 | 717 | |
| 13142 | 718 | lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" | 
| 13145 | 719 | by (cases xs) auto | 
| 13114 | 720 | |
| 13142 | 721 | lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" | 
| 13145 | 722 | by (cases xs) auto | 
| 13114 | 723 | |
| 18447 | 724 | lemma map_eq_Cons_conv: | 
| 14025 | 725 | "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" | 
| 13145 | 726 | by (cases xs) auto | 
| 13114 | 727 | |
| 18447 | 728 | lemma Cons_eq_map_conv: | 
| 14025 | 729 | "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" | 
| 730 | by (cases ys) auto | |
| 731 | ||
| 18447 | 732 | lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] | 
| 733 | lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] | |
| 734 | declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] | |
| 735 | ||
| 14111 | 736 | lemma ex_map_conv: | 
| 737 | "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)" | |
| 18447 | 738 | by(induct ys, auto simp add: Cons_eq_map_conv) | 
| 14111 | 739 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 740 | lemma map_eq_imp_length_eq: | 
| 26734 | 741 | assumes "map f xs = map f ys" | 
| 742 | shows "length xs = length ys" | |
| 743 | using assms proof (induct ys arbitrary: xs) | |
| 744 | case Nil then show ?case by simp | |
| 745 | next | |
| 746 | case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto | |
| 747 | from Cons xs have "map f zs = map f ys" by simp | |
| 748 | moreover with Cons have "length zs = length ys" by blast | |
| 749 | with xs show ?case by simp | |
| 750 | qed | |
| 751 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 752 | lemma map_inj_on: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 753 | "[| map f xs = map f ys; inj_on f (set xs Un set ys) |] | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 754 | ==> xs = ys" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 755 | apply(frule map_eq_imp_length_eq) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 756 | apply(rotate_tac -1) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 757 | apply(induct rule:list_induct2) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 758 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 759 | apply(simp) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 760 | apply (blast intro:sym) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 761 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 762 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 763 | lemma inj_on_map_eq_map: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 764 | "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 765 | by(blast dest:map_inj_on) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 766 | |
| 13114 | 767 | lemma map_injective: | 
| 24526 | 768 | "map f xs = map f ys ==> inj f ==> xs = ys" | 
| 769 | by (induct ys arbitrary: xs) (auto dest!:injD) | |
| 13114 | 770 | |
| 14339 | 771 | lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" | 
| 772 | by(blast dest:map_injective) | |
| 773 | ||
| 13114 | 774 | lemma inj_mapI: "inj f ==> inj (map f)" | 
| 17589 | 775 | by (iprover dest: map_injective injD intro: inj_onI) | 
| 13114 | 776 | |
| 777 | lemma inj_mapD: "inj (map f) ==> inj f" | |
| 14208 | 778 | apply (unfold inj_on_def, clarify) | 
| 13145 | 779 | apply (erule_tac x = "[x]" in ballE) | 
| 14208 | 780 | apply (erule_tac x = "[y]" in ballE, simp, blast) | 
| 13145 | 781 | apply blast | 
| 782 | done | |
| 13114 | 783 | |
| 14339 | 784 | lemma inj_map[iff]: "inj (map f) = inj f" | 
| 13145 | 785 | by (blast dest: inj_mapD intro: inj_mapI) | 
| 13114 | 786 | |
| 15303 | 787 | lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" | 
| 788 | apply(rule inj_onI) | |
| 789 | apply(erule map_inj_on) | |
| 790 | apply(blast intro:inj_onI dest:inj_onD) | |
| 791 | done | |
| 792 | ||
| 14343 | 793 | lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" | 
| 794 | by (induct xs, auto) | |
| 13114 | 795 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 796 | lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs" | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 797 | by (induct xs) auto | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 798 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 799 | lemma map_fst_zip[simp]: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 800 | "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 801 | by (induct rule:list_induct2, simp_all) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 802 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 803 | lemma map_snd_zip[simp]: | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 804 | "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 805 | by (induct rule:list_induct2, simp_all) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 806 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 807 | |
| 15392 | 808 | subsubsection {* @{text rev} *}
 | 
| 13114 | 809 | |
| 13142 | 810 | lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" | 
| 13145 | 811 | by (induct xs) auto | 
| 13114 | 812 | |
| 13142 | 813 | lemma rev_rev_ident [simp]: "rev (rev xs) = xs" | 
| 13145 | 814 | by (induct xs) auto | 
| 13114 | 815 | |
| 15870 | 816 | lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" | 
| 817 | by auto | |
| 818 | ||
| 13142 | 819 | lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" | 
| 13145 | 820 | by (induct xs) auto | 
| 13114 | 821 | |
| 13142 | 822 | lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" | 
| 13145 | 823 | by (induct xs) auto | 
| 13114 | 824 | |
| 15870 | 825 | lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" | 
| 826 | by (cases xs) auto | |
| 827 | ||
| 828 | lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])" | |
| 829 | by (cases xs) auto | |
| 830 | ||
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 831 | lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)" | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 832 | apply (induct xs arbitrary: ys, force) | 
| 14208 | 833 | apply (case_tac ys, simp, force) | 
| 13145 | 834 | done | 
| 13114 | 835 | |
| 15439 | 836 | lemma inj_on_rev[iff]: "inj_on rev A" | 
| 837 | by(simp add:inj_on_def) | |
| 838 | ||
| 13366 | 839 | lemma rev_induct [case_names Nil snoc]: | 
| 840 | "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs" | |
| 15489 
d136af442665
Replaced application of subst by simplesubst in proof of rev_induct
 berghofe parents: 
15439diff
changeset | 841 | apply(simplesubst rev_rev_ident[symmetric]) | 
| 13145 | 842 | apply(rule_tac list = "rev xs" in list.induct, simp_all) | 
| 843 | done | |
| 13114 | 844 | |
| 13366 | 845 | lemma rev_exhaust [case_names Nil snoc]: | 
| 846 | "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" | |
| 13145 | 847 | by (induct xs rule: rev_induct) auto | 
| 13114 | 848 | |
| 13366 | 849 | lemmas rev_cases = rev_exhaust | 
| 850 | ||
| 18423 | 851 | lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])" | 
| 852 | by(rule rev_cases[of xs]) auto | |
| 853 | ||
| 13114 | 854 | |
| 15392 | 855 | subsubsection {* @{text set} *}
 | 
| 13114 | 856 | |
| 13142 | 857 | lemma finite_set [iff]: "finite (set xs)" | 
| 13145 | 858 | by (induct xs) auto | 
| 13114 | 859 | |
| 13142 | 860 | lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" | 
| 13145 | 861 | by (induct xs) auto | 
| 13114 | 862 | |
| 17830 | 863 | lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs" | 
| 864 | by(cases xs) auto | |
| 14099 | 865 | |
| 13142 | 866 | lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" | 
| 13145 | 867 | by auto | 
| 13114 | 868 | |
| 14099 | 869 | lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" | 
| 870 | by auto | |
| 871 | ||
| 13142 | 872 | lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
 | 
| 13145 | 873 | by (induct xs) auto | 
| 13114 | 874 | |
| 15245 | 875 | lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
 | 
| 876 | by(induct xs) auto | |
| 877 | ||
| 13142 | 878 | lemma set_rev [simp]: "set (rev xs) = set xs" | 
| 13145 | 879 | by (induct xs) auto | 
| 13114 | 880 | |
| 13142 | 881 | lemma set_map [simp]: "set (map f xs) = f`(set xs)" | 
| 13145 | 882 | by (induct xs) auto | 
| 13114 | 883 | |
| 13142 | 884 | lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
 | 
| 13145 | 885 | by (induct xs) auto | 
| 13114 | 886 | |
| 32417 | 887 | lemma set_upt [simp]: "set[i..<j] = {i..<j}"
 | 
| 888 | by (induct j) (simp_all add: atLeastLessThanSuc) | |
| 13114 | 889 | |
| 13142 | 890 | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 891 | lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs" | 
| 18049 | 892 | proof (induct xs) | 
| 26073 | 893 | case Nil thus ?case by simp | 
| 894 | next | |
| 895 | case Cons thus ?case by (auto intro: Cons_eq_appendI) | |
| 896 | qed | |
| 897 | ||
| 26734 | 898 | lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)" | 
| 899 | by (auto elim: split_list) | |
| 26073 | 900 | |
| 901 | lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys" | |
| 902 | proof (induct xs) | |
| 903 | case Nil thus ?case by simp | |
| 18049 | 904 | next | 
| 905 | case (Cons a xs) | |
| 906 | show ?case | |
| 907 | proof cases | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 908 | assume "x = a" thus ?case using Cons by fastsimp | 
| 18049 | 909 | next | 
| 26073 | 910 | assume "x \<noteq> a" thus ?case using Cons by(fastsimp intro!: Cons_eq_appendI) | 
| 911 | qed | |
| 912 | qed | |
| 913 | ||
| 914 | lemma in_set_conv_decomp_first: | |
| 915 | "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)" | |
| 26734 | 916 | by (auto dest!: split_list_first) | 
| 26073 | 917 | |
| 918 | lemma split_list_last: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs" | |
| 919 | proof (induct xs rule:rev_induct) | |
| 920 | case Nil thus ?case by simp | |
| 921 | next | |
| 922 | case (snoc a xs) | |
| 923 | show ?case | |
| 924 | proof cases | |
| 925 | assume "x = a" thus ?case using snoc by simp (metis ex_in_conv set_empty2) | |
| 926 | next | |
| 927 | assume "x \<noteq> a" thus ?case using snoc by fastsimp | |
| 18049 | 928 | qed | 
| 929 | qed | |
| 930 | ||
| 26073 | 931 | lemma in_set_conv_decomp_last: | 
| 932 | "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)" | |
| 26734 | 933 | by (auto dest!: split_list_last) | 
| 26073 | 934 | |
| 935 | lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x" | |
| 936 | proof (induct xs) | |
| 937 | case Nil thus ?case by simp | |
| 938 | next | |
| 939 | case Cons thus ?case | |
| 940 | by(simp add:Bex_def)(metis append_Cons append.simps(1)) | |
| 941 | qed | |
| 942 | ||
| 943 | lemma split_list_propE: | |
| 26734 | 944 | assumes "\<exists>x \<in> set xs. P x" | 
| 945 | obtains ys x zs where "xs = ys @ x # zs" and "P x" | |
| 946 | using split_list_prop [OF assms] by blast | |
| 26073 | 947 | |
| 948 | lemma split_list_first_prop: | |
| 949 | "\<exists>x \<in> set xs. P x \<Longrightarrow> | |
| 950 | \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)" | |
| 26734 | 951 | proof (induct xs) | 
| 26073 | 952 | case Nil thus ?case by simp | 
| 953 | next | |
| 954 | case (Cons x xs) | |
| 955 | show ?case | |
| 956 | proof cases | |
| 957 | assume "P x" | |
| 26734 | 958 | thus ?thesis by simp | 
| 959 | (metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append) | |
| 26073 | 960 | next | 
| 961 | assume "\<not> P x" | |
| 962 | hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp | |
| 963 | thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD) | |
| 964 | qed | |
| 965 | qed | |
| 966 | ||
| 967 | lemma split_list_first_propE: | |
| 26734 | 968 | assumes "\<exists>x \<in> set xs. P x" | 
| 969 | obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y" | |
| 970 | using split_list_first_prop [OF assms] by blast | |
| 26073 | 971 | |
| 972 | lemma split_list_first_prop_iff: | |
| 973 | "(\<exists>x \<in> set xs. P x) \<longleftrightarrow> | |
| 974 | (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))" | |
| 26734 | 975 | by (rule, erule split_list_first_prop) auto | 
| 26073 | 976 | |
| 977 | lemma split_list_last_prop: | |
| 978 | "\<exists>x \<in> set xs. P x \<Longrightarrow> | |
| 979 | \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)" | |
| 980 | proof(induct xs rule:rev_induct) | |
| 981 | case Nil thus ?case by simp | |
| 982 | next | |
| 983 | case (snoc x xs) | |
| 984 | show ?case | |
| 985 | proof cases | |
| 986 | assume "P x" thus ?thesis by (metis emptyE set_empty) | |
| 987 | next | |
| 988 | assume "\<not> P x" | |
| 989 | hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp | |
| 990 | thus ?thesis using `\<not> P x` snoc(1) by fastsimp | |
| 991 | qed | |
| 992 | qed | |
| 993 | ||
| 994 | lemma split_list_last_propE: | |
| 26734 | 995 | assumes "\<exists>x \<in> set xs. P x" | 
| 996 | obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z" | |
| 997 | using split_list_last_prop [OF assms] by blast | |
| 26073 | 998 | |
| 999 | lemma split_list_last_prop_iff: | |
| 1000 | "(\<exists>x \<in> set xs. P x) \<longleftrightarrow> | |
| 1001 | (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))" | |
| 26734 | 1002 | by (metis split_list_last_prop [where P=P] in_set_conv_decomp) | 
| 26073 | 1003 | |
| 1004 | lemma finite_list: "finite A ==> EX xs. set xs = A" | |
| 26734 | 1005 | by (erule finite_induct) | 
| 1006 | (auto simp add: set.simps(2) [symmetric] simp del: set.simps(2)) | |
| 13508 | 1007 | |
| 14388 | 1008 | lemma card_length: "card (set xs) \<le> length xs" | 
| 1009 | by (induct xs) (auto simp add: card_insert_if) | |
| 13114 | 1010 | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1011 | lemma set_minus_filter_out: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1012 |   "set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)"
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1013 | by (induct xs) auto | 
| 15168 | 1014 | |
| 15392 | 1015 | subsubsection {* @{text filter} *}
 | 
| 13114 | 1016 | |
| 13142 | 1017 | lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" | 
| 13145 | 1018 | by (induct xs) auto | 
| 13114 | 1019 | |
| 15305 | 1020 | lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" | 
| 1021 | by (induct xs) simp_all | |
| 1022 | ||
| 13142 | 1023 | lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" | 
| 13145 | 1024 | by (induct xs) auto | 
| 13114 | 1025 | |
| 16998 | 1026 | lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" | 
| 1027 | by (induct xs) (auto simp add: le_SucI) | |
| 1028 | ||
| 18423 | 1029 | lemma sum_length_filter_compl: | 
| 1030 | "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs" | |
| 1031 | by(induct xs) simp_all | |
| 1032 | ||
| 13142 | 1033 | lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" | 
| 13145 | 1034 | by (induct xs) auto | 
| 13114 | 1035 | |
| 13142 | 1036 | lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" | 
| 13145 | 1037 | by (induct xs) auto | 
| 13114 | 1038 | |
| 16998 | 1039 | lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" | 
| 24349 | 1040 | by (induct xs) simp_all | 
| 16998 | 1041 | |
| 1042 | lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" | |
| 1043 | apply (induct xs) | |
| 1044 | apply auto | |
| 1045 | apply(cut_tac P=P and xs=xs in length_filter_le) | |
| 1046 | apply simp | |
| 1047 | done | |
| 13114 | 1048 | |
| 16965 | 1049 | lemma filter_map: | 
| 1050 | "filter P (map f xs) = map f (filter (P o f) xs)" | |
| 1051 | by (induct xs) simp_all | |
| 1052 | ||
| 1053 | lemma length_filter_map[simp]: | |
| 1054 | "length (filter P (map f xs)) = length(filter (P o f) xs)" | |
| 1055 | by (simp add:filter_map) | |
| 1056 | ||
| 13142 | 1057 | lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" | 
| 13145 | 1058 | by auto | 
| 13114 | 1059 | |
| 15246 | 1060 | lemma length_filter_less: | 
| 1061 | "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" | |
| 1062 | proof (induct xs) | |
| 1063 | case Nil thus ?case by simp | |
| 1064 | next | |
| 1065 | case (Cons x xs) thus ?case | |
| 1066 | apply (auto split:split_if_asm) | |
| 1067 | using length_filter_le[of P xs] apply arith | |
| 1068 | done | |
| 1069 | qed | |
| 13114 | 1070 | |
| 15281 | 1071 | lemma length_filter_conv_card: | 
| 1072 |  "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
 | |
| 1073 | proof (induct xs) | |
| 1074 | case Nil thus ?case by simp | |
| 1075 | next | |
| 1076 | case (Cons x xs) | |
| 1077 |   let ?S = "{i. i < length xs & p(xs!i)}"
 | |
| 1078 | have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) | |
| 1079 | show ?case (is "?l = card ?S'") | |
| 1080 | proof (cases) | |
| 1081 | assume "p x" | |
| 1082 | hence eq: "?S' = insert 0 (Suc ` ?S)" | |
| 25162 | 1083 | by(auto simp: image_def split:nat.split dest:gr0_implies_Suc) | 
| 15281 | 1084 | have "length (filter p (x # xs)) = Suc(card ?S)" | 
| 23388 | 1085 | using Cons `p x` by simp | 
| 15281 | 1086 | also have "\<dots> = Suc(card(Suc ` ?S))" using fin | 
| 1087 | by (simp add: card_image inj_Suc) | |
| 1088 | also have "\<dots> = card ?S'" using eq fin | |
| 1089 | by (simp add:card_insert_if) (simp add:image_def) | |
| 1090 | finally show ?thesis . | |
| 1091 | next | |
| 1092 | assume "\<not> p x" | |
| 1093 | hence eq: "?S' = Suc ` ?S" | |
| 25162 | 1094 | by(auto simp add: image_def split:nat.split elim:lessE) | 
| 15281 | 1095 | have "length (filter p (x # xs)) = card ?S" | 
| 23388 | 1096 | using Cons `\<not> p x` by simp | 
| 15281 | 1097 | also have "\<dots> = card(Suc ` ?S)" using fin | 
| 1098 | by (simp add: card_image inj_Suc) | |
| 1099 | also have "\<dots> = card ?S'" using eq fin | |
| 1100 | by (simp add:card_insert_if) | |
| 1101 | finally show ?thesis . | |
| 1102 | qed | |
| 1103 | qed | |
| 1104 | ||
| 17629 | 1105 | lemma Cons_eq_filterD: | 
| 1106 | "x#xs = filter P ys \<Longrightarrow> | |
| 1107 | \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" | |
| 19585 | 1108 | (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs") | 
| 17629 | 1109 | proof(induct ys) | 
| 1110 | case Nil thus ?case by simp | |
| 1111 | next | |
| 1112 | case (Cons y ys) | |
| 1113 | show ?case (is "\<exists>x. ?Q x") | |
| 1114 | proof cases | |
| 1115 | assume Py: "P y" | |
| 1116 | show ?thesis | |
| 1117 | proof cases | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1118 | assume "x = y" | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1119 | with Py Cons.prems have "?Q []" by simp | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1120 | then show ?thesis .. | 
| 17629 | 1121 | next | 
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1122 | assume "x \<noteq> y" | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1123 | with Py Cons.prems show ?thesis by simp | 
| 17629 | 1124 | qed | 
| 1125 | next | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1126 | assume "\<not> P y" | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1127 | with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastsimp | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1128 | then have "?Q (y#us)" by simp | 
| 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1129 | then show ?thesis .. | 
| 17629 | 1130 | qed | 
| 1131 | qed | |
| 1132 | ||
| 1133 | lemma filter_eq_ConsD: | |
| 1134 | "filter P ys = x#xs \<Longrightarrow> | |
| 1135 | \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" | |
| 1136 | by(rule Cons_eq_filterD) simp | |
| 1137 | ||
| 1138 | lemma filter_eq_Cons_iff: | |
| 1139 | "(filter P ys = x#xs) = | |
| 1140 | (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" | |
| 1141 | by(auto dest:filter_eq_ConsD) | |
| 1142 | ||
| 1143 | lemma Cons_eq_filter_iff: | |
| 1144 | "(x#xs = filter P ys) = | |
| 1145 | (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" | |
| 1146 | by(auto dest:Cons_eq_filterD) | |
| 1147 | ||
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19623diff
changeset | 1148 | lemma filter_cong[fundef_cong, recdef_cong]: | 
| 17501 | 1149 | "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" | 
| 1150 | apply simp | |
| 1151 | apply(erule thin_rl) | |
| 1152 | by (induct ys) simp_all | |
| 1153 | ||
| 15281 | 1154 | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1155 | subsubsection {* List partitioning *}
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1156 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1157 | primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1158 | "partition P [] = ([], [])" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1159 | | "partition P (x # xs) = | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1160 | (let (yes, no) = partition P xs | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1161 | in if P x then (x # yes, no) else (yes, x # no))" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1162 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1163 | lemma partition_filter1: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1164 | "fst (partition P xs) = filter P xs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1165 | by (induct xs) (auto simp add: Let_def split_def) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1166 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1167 | lemma partition_filter2: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1168 | "snd (partition P xs) = filter (Not o P) xs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1169 | by (induct xs) (auto simp add: Let_def split_def) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1170 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1171 | lemma partition_P: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1172 | assumes "partition P xs = (yes, no)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1173 | shows "(\<forall>p \<in> set yes. P p) \<and> (\<forall>p \<in> set no. \<not> P p)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1174 | proof - | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1175 | from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1176 | by simp_all | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1177 | then show ?thesis by (simp_all add: partition_filter1 partition_filter2) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1178 | qed | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1179 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1180 | lemma partition_set: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1181 | assumes "partition P xs = (yes, no)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1182 | shows "set yes \<union> set no = set xs" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1183 | proof - | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1184 | from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1185 | by simp_all | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1186 | then show ?thesis by (auto simp add: partition_filter1 partition_filter2) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1187 | qed | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1188 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1189 | lemma partition_filter_conv[simp]: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1190 | "partition f xs = (filter f xs,filter (Not o f) xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1191 | unfolding partition_filter2[symmetric] | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1192 | unfolding partition_filter1[symmetric] by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1193 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1194 | declare partition.simps[simp del] | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 1195 | |
| 15392 | 1196 | subsubsection {* @{text concat} *}
 | 
| 13114 | 1197 | |
| 13142 | 1198 | lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" | 
| 13145 | 1199 | by (induct xs) auto | 
| 13114 | 1200 | |
| 18447 | 1201 | lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" | 
| 13145 | 1202 | by (induct xss) auto | 
| 13114 | 1203 | |
| 18447 | 1204 | lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" | 
| 13145 | 1205 | by (induct xss) auto | 
| 13114 | 1206 | |
| 24308 | 1207 | lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)" | 
| 13145 | 1208 | by (induct xs) auto | 
| 13114 | 1209 | |
| 24476 
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
 nipkow parents: 
24471diff
changeset | 1210 | lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs" | 
| 24349 | 1211 | by (induct xs) auto | 
| 1212 | ||
| 13142 | 1213 | lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" | 
| 13145 | 1214 | by (induct xs) auto | 
| 13114 | 1215 | |
| 13142 | 1216 | lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" | 
| 13145 | 1217 | by (induct xs) auto | 
| 13114 | 1218 | |
| 13142 | 1219 | lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" | 
| 13145 | 1220 | by (induct xs) auto | 
| 13114 | 1221 | |
| 1222 | ||
| 15392 | 1223 | subsubsection {* @{text nth} *}
 | 
| 13114 | 1224 | |
| 29827 | 1225 | lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x" | 
| 13145 | 1226 | by auto | 
| 13114 | 1227 | |
| 29827 | 1228 | lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n" | 
| 13145 | 1229 | by auto | 
| 13114 | 1230 | |
| 13142 | 1231 | declare nth.simps [simp del] | 
| 13114 | 1232 | |
| 1233 | lemma nth_append: | |
| 24526 | 1234 | "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" | 
| 1235 | apply (induct xs arbitrary: n, simp) | |
| 14208 | 1236 | apply (case_tac n, auto) | 
| 13145 | 1237 | done | 
| 13114 | 1238 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1239 | lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x" | 
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1240 | by (induct xs) auto | 
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1241 | |
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1242 | lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n" | 
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 1243 | by (induct xs) auto | 
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1244 | |
| 24526 | 1245 | lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)" | 
| 1246 | apply (induct xs arbitrary: n, simp) | |
| 14208 | 1247 | apply (case_tac n, auto) | 
| 13145 | 1248 | done | 
| 13114 | 1249 | |
| 18423 | 1250 | lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0" | 
| 1251 | by(cases xs) simp_all | |
| 1252 | ||
| 18049 | 1253 | |
| 1254 | lemma list_eq_iff_nth_eq: | |
| 24526 | 1255 | "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))" | 
| 1256 | apply(induct xs arbitrary: ys) | |
| 24632 | 1257 | apply force | 
| 18049 | 1258 | apply(case_tac ys) | 
| 1259 | apply simp | |
| 1260 | apply(simp add:nth_Cons split:nat.split)apply blast | |
| 1261 | done | |
| 1262 | ||
| 13142 | 1263 | lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
 | 
| 15251 | 1264 | apply (induct xs, simp, simp) | 
| 13145 | 1265 | apply safe | 
| 24632 | 1266 | apply (metis nat_case_0 nth.simps zero_less_Suc) | 
| 1267 | apply (metis less_Suc_eq_0_disj nth_Cons_Suc) | |
| 14208 | 1268 | apply (case_tac i, simp) | 
| 24632 | 1269 | apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff) | 
| 13145 | 1270 | done | 
| 13114 | 1271 | |
| 17501 | 1272 | lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" | 
| 1273 | by(auto simp:set_conv_nth) | |
| 1274 | ||
| 13145 | 1275 | lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)" | 
| 1276 | by (auto simp add: set_conv_nth) | |
| 13114 | 1277 | |
| 13142 | 1278 | lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs" | 
| 13145 | 1279 | by (auto simp add: set_conv_nth) | 
| 13114 | 1280 | |
| 1281 | lemma all_nth_imp_all_set: | |
| 13145 | 1282 | "[| !i < length xs. P(xs!i); x : set xs|] ==> P x" | 
| 1283 | by (auto simp add: set_conv_nth) | |
| 13114 | 1284 | |
| 1285 | lemma all_set_conv_all_nth: | |
| 13145 | 1286 | "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))" | 
| 1287 | by (auto simp add: set_conv_nth) | |
| 13114 | 1288 | |
| 25296 | 1289 | lemma rev_nth: | 
| 1290 | "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)" | |
| 1291 | proof (induct xs arbitrary: n) | |
| 1292 | case Nil thus ?case by simp | |
| 1293 | next | |
| 1294 | case (Cons x xs) | |
| 1295 | hence n: "n < Suc (length xs)" by simp | |
| 1296 | moreover | |
| 1297 |   { assume "n < length xs"
 | |
| 1298 | with n obtain n' where "length xs - n = Suc n'" | |
| 1299 | by (cases "length xs - n", auto) | |
| 1300 | moreover | |
| 1301 | then have "length xs - Suc n = n'" by simp | |
| 1302 | ultimately | |
| 1303 | have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp | |
| 1304 | } | |
| 1305 | ultimately | |
| 1306 | show ?case by (clarsimp simp add: Cons nth_append) | |
| 1307 | qed | |
| 13114 | 1308 | |
| 31159 | 1309 | lemma Skolem_list_nth: | 
| 1310 | "(ALL i<k. EX x. P i x) = (EX xs. size xs = k & (ALL i<k. P i (xs!i)))" | |
| 1311 | (is "_ = (EX xs. ?P k xs)") | |
| 1312 | proof(induct k) | |
| 1313 | case 0 show ?case by simp | |
| 1314 | next | |
| 1315 | case (Suc k) | |
| 1316 | show ?case (is "?L = ?R" is "_ = (EX xs. ?P' xs)") | |
| 1317 | proof | |
| 1318 | assume "?R" thus "?L" using Suc by auto | |
| 1319 | next | |
| 1320 | assume "?L" | |
| 1321 | with Suc obtain x xs where "?P k xs & P k x" by (metis less_Suc_eq) | |
| 1322 | hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq) | |
| 1323 | thus "?R" .. | |
| 1324 | qed | |
| 1325 | qed | |
| 1326 | ||
| 1327 | ||
| 15392 | 1328 | subsubsection {* @{text list_update} *}
 | 
| 13114 | 1329 | |
| 24526 | 1330 | lemma length_list_update [simp]: "length(xs[i:=x]) = length xs" | 
| 1331 | by (induct xs arbitrary: i) (auto split: nat.split) | |
| 13114 | 1332 | |
| 1333 | lemma nth_list_update: | |
| 24526 | 1334 | "i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" | 
| 1335 | by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) | |
| 13114 | 1336 | |
| 13142 | 1337 | lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" | 
| 13145 | 1338 | by (simp add: nth_list_update) | 
| 13114 | 1339 | |
| 24526 | 1340 | lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j" | 
| 1341 | by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) | |
| 13114 | 1342 | |
| 24526 | 1343 | lemma list_update_id[simp]: "xs[i := xs!i] = xs" | 
| 1344 | by (induct xs arbitrary: i) (simp_all split:nat.splits) | |
| 1345 | ||
| 1346 | lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" | |
| 1347 | apply (induct xs arbitrary: i) | |
| 17501 | 1348 | apply simp | 
| 1349 | apply (case_tac i) | |
| 1350 | apply simp_all | |
| 1351 | done | |
| 1352 | ||
| 31077 | 1353 | lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]" | 
| 1354 | by(metis length_0_conv length_list_update) | |
| 1355 | ||
| 13114 | 1356 | lemma list_update_same_conv: | 
| 24526 | 1357 | "i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" | 
| 1358 | by (induct xs arbitrary: i) (auto split: nat.split) | |
| 13114 | 1359 | |
| 14187 | 1360 | lemma list_update_append1: | 
| 24526 | 1361 | "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" | 
| 1362 | apply (induct xs arbitrary: i, simp) | |
| 14187 | 1363 | apply(simp split:nat.split) | 
| 1364 | done | |
| 1365 | ||
| 15868 | 1366 | lemma list_update_append: | 
| 24526 | 1367 | "(xs @ ys) [n:= x] = | 
| 15868 | 1368 | (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))" | 
| 24526 | 1369 | by (induct xs arbitrary: n) (auto split:nat.splits) | 
| 15868 | 1370 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1371 | lemma list_update_length [simp]: | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1372 | "(xs @ x # ys)[length xs := y] = (xs @ y # ys)" | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1373 | by (induct xs, auto) | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 1374 | |
| 31264 | 1375 | lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]" | 
| 1376 | by(induct xs arbitrary: k)(auto split:nat.splits) | |
| 1377 | ||
| 1378 | lemma rev_update: | |
| 1379 | "k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]" | |
| 1380 | by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits) | |
| 1381 | ||
| 13114 | 1382 | lemma update_zip: | 
| 31080 | 1383 | "(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" | 
| 24526 | 1384 | by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split) | 
| 1385 | ||
| 1386 | lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)" | |
| 1387 | by (induct xs arbitrary: i) (auto split: nat.split) | |
| 13114 | 1388 | |
| 1389 | lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A" | |
| 13145 | 1390 | by (blast dest!: set_update_subset_insert [THEN subsetD]) | 
| 13114 | 1391 | |
| 24526 | 1392 | lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" | 
| 1393 | by (induct xs arbitrary: n) (auto split:nat.splits) | |
| 15868 | 1394 | |
| 31077 | 1395 | lemma list_update_overwrite[simp]: | 
| 24796 | 1396 | "xs [i := x, i := y] = xs [i := y]" | 
| 31077 | 1397 | apply (induct xs arbitrary: i) apply simp | 
| 1398 | apply (case_tac i, simp_all) | |
| 24796 | 1399 | done | 
| 1400 | ||
| 1401 | lemma list_update_swap: | |
| 1402 | "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]" | |
| 1403 | apply (induct xs arbitrary: i i') | |
| 1404 | apply simp | |
| 1405 | apply (case_tac i, case_tac i') | |
| 1406 | apply auto | |
| 1407 | apply (case_tac i') | |
| 1408 | apply auto | |
| 1409 | done | |
| 1410 | ||
| 29827 | 1411 | lemma list_update_code [code]: | 
| 1412 | "[][i := y] = []" | |
| 1413 | "(x # xs)[0 := y] = y # xs" | |
| 1414 | "(x # xs)[Suc i := y] = x # xs[i := y]" | |
| 1415 | by simp_all | |
| 1416 | ||
| 13114 | 1417 | |
| 15392 | 1418 | subsubsection {* @{text last} and @{text butlast} *}
 | 
| 13114 | 1419 | |
| 13142 | 1420 | lemma last_snoc [simp]: "last (xs @ [x]) = x" | 
| 13145 | 1421 | by (induct xs) auto | 
| 13114 | 1422 | |
| 13142 | 1423 | lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" | 
| 13145 | 1424 | by (induct xs) auto | 
| 13114 | 1425 | |
| 14302 | 1426 | lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" | 
| 1427 | by(simp add:last.simps) | |
| 1428 | ||
| 1429 | lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" | |
| 1430 | by(simp add:last.simps) | |
| 1431 | ||
| 1432 | lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" | |
| 1433 | by (induct xs) (auto) | |
| 1434 | ||
| 1435 | lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" | |
| 1436 | by(simp add:last_append) | |
| 1437 | ||
| 1438 | lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" | |
| 1439 | by(simp add:last_append) | |
| 1440 | ||
| 17762 | 1441 | lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs" | 
| 1442 | by(rule rev_exhaust[of xs]) simp_all | |
| 1443 | ||
| 1444 | lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs" | |
| 1445 | by(cases xs) simp_all | |
| 1446 | ||
| 17765 | 1447 | lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as" | 
| 1448 | by (induct as) auto | |
| 17762 | 1449 | |
| 13142 | 1450 | lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" | 
| 13145 | 1451 | by (induct xs rule: rev_induct) auto | 
| 13114 | 1452 | |
| 1453 | lemma butlast_append: | |
| 24526 | 1454 | "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" | 
| 1455 | by (induct xs arbitrary: ys) auto | |
| 13114 | 1456 | |
| 13142 | 1457 | lemma append_butlast_last_id [simp]: | 
| 13145 | 1458 | "xs \<noteq> [] ==> butlast xs @ [last xs] = xs" | 
| 1459 | by (induct xs) auto | |
| 13114 | 1460 | |
| 13142 | 1461 | lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" | 
| 13145 | 1462 | by (induct xs) (auto split: split_if_asm) | 
| 13114 | 1463 | |
| 1464 | lemma in_set_butlast_appendI: | |
| 13145 | 1465 | "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))" | 
| 1466 | by (auto dest: in_set_butlastD simp add: butlast_append) | |
| 13114 | 1467 | |
| 24526 | 1468 | lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs" | 
| 1469 | apply (induct xs arbitrary: n) | |
| 17501 | 1470 | apply simp | 
| 1471 | apply (auto split:nat.split) | |
| 1472 | done | |
| 1473 | ||
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1474 | lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)" | 
| 17589 | 1475 | by(induct xs)(auto simp:neq_Nil_conv) | 
| 1476 | ||
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1477 | lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs" | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1478 | by (induct xs, simp, case_tac xs, simp_all) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1479 | |
| 31077 | 1480 | lemma last_list_update: | 
| 1481 | "xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)" | |
| 1482 | by (auto simp: last_conv_nth) | |
| 1483 | ||
| 1484 | lemma butlast_list_update: | |
| 1485 | "butlast(xs[k:=x]) = | |
| 1486 | (if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])" | |
| 1487 | apply(cases xs rule:rev_cases) | |
| 1488 | apply simp | |
| 1489 | apply(simp add:list_update_append split:nat.splits) | |
| 1490 | done | |
| 1491 | ||
| 24796 | 1492 | |
| 15392 | 1493 | subsubsection {* @{text take} and @{text drop} *}
 | 
| 13114 | 1494 | |
| 13142 | 1495 | lemma take_0 [simp]: "take 0 xs = []" | 
| 13145 | 1496 | by (induct xs) auto | 
| 13114 | 1497 | |
| 13142 | 1498 | lemma drop_0 [simp]: "drop 0 xs = xs" | 
| 13145 | 1499 | by (induct xs) auto | 
| 13114 | 1500 | |
| 13142 | 1501 | lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" | 
| 13145 | 1502 | by simp | 
| 13114 | 1503 | |
| 13142 | 1504 | lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" | 
| 13145 | 1505 | by simp | 
| 13114 | 1506 | |
| 13142 | 1507 | declare take_Cons [simp del] and drop_Cons [simp del] | 
| 13114 | 1508 | |
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1509 | lemma take_1_Cons [simp]: "take 1 (x # xs) = [x]" | 
| 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1510 | unfolding One_nat_def by simp | 
| 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1511 | |
| 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1512 | lemma drop_1_Cons [simp]: "drop 1 (x # xs) = xs" | 
| 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1513 | unfolding One_nat_def by simp | 
| 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1514 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1515 | lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1516 | by(clarsimp simp add:neq_Nil_conv) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1517 | |
| 14187 | 1518 | lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" | 
| 1519 | by(cases xs, simp_all) | |
| 1520 | ||
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1521 | lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)" | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1522 | by (induct xs arbitrary: n) simp_all | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1523 | |
| 24526 | 1524 | lemma drop_tl: "drop n (tl xs) = tl(drop n xs)" | 
| 1525 | by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split) | |
| 1526 | ||
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1527 | lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)" | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1528 | by (cases n, simp, cases xs, auto) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1529 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1530 | lemma tl_drop: "tl (drop n xs) = drop n (tl xs)" | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1531 | by (simp only: drop_tl) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1532 | |
| 24526 | 1533 | lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y" | 
| 1534 | apply (induct xs arbitrary: n, simp) | |
| 14187 | 1535 | apply(simp add:drop_Cons nth_Cons split:nat.splits) | 
| 1536 | done | |
| 1537 | ||
| 13913 | 1538 | lemma take_Suc_conv_app_nth: | 
| 24526 | 1539 | "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" | 
| 1540 | apply (induct xs arbitrary: i, simp) | |
| 14208 | 1541 | apply (case_tac i, auto) | 
| 13913 | 1542 | done | 
| 1543 | ||
| 14591 | 1544 | lemma drop_Suc_conv_tl: | 
| 24526 | 1545 | "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" | 
| 1546 | apply (induct xs arbitrary: i, simp) | |
| 14591 | 1547 | apply (case_tac i, auto) | 
| 1548 | done | |
| 1549 | ||
| 24526 | 1550 | lemma length_take [simp]: "length (take n xs) = min (length xs) n" | 
| 1551 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1552 | ||
| 1553 | lemma length_drop [simp]: "length (drop n xs) = (length xs - n)" | |
| 1554 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1555 | ||
| 1556 | lemma take_all [simp]: "length xs <= n ==> take n xs = xs" | |
| 1557 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1558 | ||
| 1559 | lemma drop_all [simp]: "length xs <= n ==> drop n xs = []" | |
| 1560 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 13114 | 1561 | |
| 13142 | 1562 | lemma take_append [simp]: | 
| 24526 | 1563 | "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" | 
| 1564 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 13114 | 1565 | |
| 13142 | 1566 | lemma drop_append [simp]: | 
| 24526 | 1567 | "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys" | 
| 1568 | by (induct n arbitrary: xs) (auto, case_tac xs, auto) | |
| 1569 | ||
| 1570 | lemma take_take [simp]: "take n (take m xs) = take (min n m) xs" | |
| 1571 | apply (induct m arbitrary: xs n, auto) | |
| 14208 | 1572 | apply (case_tac xs, auto) | 
| 15236 
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
 nipkow parents: 
15176diff
changeset | 1573 | apply (case_tac n, auto) | 
| 13145 | 1574 | done | 
| 13114 | 1575 | |
| 24526 | 1576 | lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs" | 
| 1577 | apply (induct m arbitrary: xs, auto) | |
| 14208 | 1578 | apply (case_tac xs, auto) | 
| 13145 | 1579 | done | 
| 13114 | 1580 | |
| 24526 | 1581 | lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)" | 
| 1582 | apply (induct m arbitrary: xs n, auto) | |
| 14208 | 1583 | apply (case_tac xs, auto) | 
| 13145 | 1584 | done | 
| 13114 | 1585 | |
| 24526 | 1586 | lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)" | 
| 1587 | apply(induct xs arbitrary: m n) | |
| 14802 | 1588 | apply simp | 
| 1589 | apply(simp add: take_Cons drop_Cons split:nat.split) | |
| 1590 | done | |
| 1591 | ||
| 24526 | 1592 | lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs" | 
| 1593 | apply (induct n arbitrary: xs, auto) | |
| 14208 | 1594 | apply (case_tac xs, auto) | 
| 13145 | 1595 | done | 
| 13114 | 1596 | |
| 24526 | 1597 | lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])" | 
| 1598 | apply(induct xs arbitrary: n) | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1599 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1600 | apply(simp add:take_Cons split:nat.split) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1601 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1602 | |
| 24526 | 1603 | lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)" | 
| 1604 | apply(induct xs arbitrary: n) | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1605 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1606 | apply(simp add:drop_Cons split:nat.split) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1607 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1608 | |
| 24526 | 1609 | lemma take_map: "take n (map f xs) = map f (take n xs)" | 
| 1610 | apply (induct n arbitrary: xs, auto) | |
| 14208 | 1611 | apply (case_tac xs, auto) | 
| 13145 | 1612 | done | 
| 13114 | 1613 | |
| 24526 | 1614 | lemma drop_map: "drop n (map f xs) = map f (drop n xs)" | 
| 1615 | apply (induct n arbitrary: xs, auto) | |
| 14208 | 1616 | apply (case_tac xs, auto) | 
| 13145 | 1617 | done | 
| 13114 | 1618 | |
| 24526 | 1619 | lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)" | 
| 1620 | apply (induct xs arbitrary: i, auto) | |
| 14208 | 1621 | apply (case_tac i, auto) | 
| 13145 | 1622 | done | 
| 13114 | 1623 | |
| 24526 | 1624 | lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)" | 
| 1625 | apply (induct xs arbitrary: i, auto) | |
| 14208 | 1626 | apply (case_tac i, auto) | 
| 13145 | 1627 | done | 
| 13114 | 1628 | |
| 24526 | 1629 | lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i" | 
| 1630 | apply (induct xs arbitrary: i n, auto) | |
| 14208 | 1631 | apply (case_tac n, blast) | 
| 1632 | apply (case_tac i, auto) | |
| 13145 | 1633 | done | 
| 13114 | 1634 | |
| 13142 | 1635 | lemma nth_drop [simp]: | 
| 24526 | 1636 | "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" | 
| 1637 | apply (induct n arbitrary: xs i, auto) | |
| 14208 | 1638 | apply (case_tac xs, auto) | 
| 13145 | 1639 | done | 
| 3507 | 1640 | |
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1641 | lemma butlast_take: | 
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1642 | "n <= length xs ==> butlast (take n xs) = take (n - 1) xs" | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1643 | by (simp add: butlast_conv_take min_max.inf_absorb1 min_max.inf_absorb2) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1644 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1645 | lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)" | 
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1646 | by (simp add: butlast_conv_take drop_take add_ac) | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1647 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1648 | lemma take_butlast: "n < length xs ==> take n (butlast xs) = take n xs" | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1649 | by (simp add: butlast_conv_take min_max.inf_absorb1) | 
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1650 | |
| 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1651 | lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)" | 
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 1652 | by (simp add: butlast_conv_take drop_take add_ac) | 
| 26584 
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
 huffman parents: 
26480diff
changeset | 1653 | |
| 18423 | 1654 | lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n" | 
| 1655 | by(simp add: hd_conv_nth) | |
| 1656 | ||
| 24526 | 1657 | lemma set_take_subset: "set(take n xs) \<subseteq> set xs" | 
| 1658 | by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split) | |
| 1659 | ||
| 1660 | lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs" | |
| 1661 | by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split) | |
| 14025 | 1662 | |
| 14187 | 1663 | lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs" | 
| 1664 | using set_take_subset by fast | |
| 1665 | ||
| 1666 | lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs" | |
| 1667 | using set_drop_subset by fast | |
| 1668 | ||
| 13114 | 1669 | lemma append_eq_conv_conj: | 
| 24526 | 1670 | "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" | 
| 1671 | apply (induct xs arbitrary: zs, simp, clarsimp) | |
| 14208 | 1672 | apply (case_tac zs, auto) | 
| 13145 | 1673 | done | 
| 13142 | 1674 | |
| 24526 | 1675 | lemma take_add: | 
| 1676 | "i+j \<le> length(xs) \<Longrightarrow> take (i+j) xs = take i xs @ take j (drop i xs)" | |
| 1677 | apply (induct xs arbitrary: i, auto) | |
| 1678 | apply (case_tac i, simp_all) | |
| 14050 | 1679 | done | 
| 1680 | ||
| 14300 | 1681 | lemma append_eq_append_conv_if: | 
| 24526 | 1682 | "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) = | 
| 14300 | 1683 | (if size xs\<^isub>1 \<le> size ys\<^isub>1 | 
| 1684 | then xs\<^isub>1 = take (size xs\<^isub>1) ys\<^isub>1 \<and> xs\<^isub>2 = drop (size xs\<^isub>1) ys\<^isub>1 @ ys\<^isub>2 | |
| 1685 | else take (size ys\<^isub>1) xs\<^isub>1 = ys\<^isub>1 \<and> drop (size ys\<^isub>1) xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>2)" | |
| 24526 | 1686 | apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1) | 
| 14300 | 1687 | apply simp | 
| 1688 | apply(case_tac ys\<^isub>1) | |
| 1689 | apply simp_all | |
| 1690 | done | |
| 1691 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1692 | lemma take_hd_drop: | 
| 30079 
293b896b9c25
make proofs work whether or not One_nat_def is a simp rule; replace 1 with Suc 0 in the rhs of some simp rules
 huffman parents: 
30008diff
changeset | 1693 | "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs" | 
| 24526 | 1694 | apply(induct xs arbitrary: n) | 
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1695 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1696 | apply(simp add:drop_Cons split:nat.split) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1697 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 1698 | |
| 17501 | 1699 | lemma id_take_nth_drop: | 
| 1700 | "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" | |
| 1701 | proof - | |
| 1702 | assume si: "i < length xs" | |
| 1703 | hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto | |
| 1704 | moreover | |
| 1705 | from si have "take (Suc i) xs = take i xs @ [xs!i]" | |
| 1706 | apply (rule_tac take_Suc_conv_app_nth) by arith | |
| 1707 | ultimately show ?thesis by auto | |
| 1708 | qed | |
| 1709 | ||
| 1710 | lemma upd_conv_take_nth_drop: | |
| 1711 | "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs" | |
| 1712 | proof - | |
| 1713 | assume i: "i < length xs" | |
| 1714 | have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]" | |
| 1715 | by(rule arg_cong[OF id_take_nth_drop[OF i]]) | |
| 1716 | also have "\<dots> = take i xs @ a # drop (Suc i) xs" | |
| 1717 | using i by (simp add: list_update_append) | |
| 1718 | finally show ?thesis . | |
| 1719 | qed | |
| 1720 | ||
| 24796 | 1721 | lemma nth_drop': | 
| 1722 | "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs" | |
| 1723 | apply (induct i arbitrary: xs) | |
| 1724 | apply (simp add: neq_Nil_conv) | |
| 1725 | apply (erule exE)+ | |
| 1726 | apply simp | |
| 1727 | apply (case_tac xs) | |
| 1728 | apply simp_all | |
| 1729 | done | |
| 1730 | ||
| 13114 | 1731 | |
| 15392 | 1732 | subsubsection {* @{text takeWhile} and @{text dropWhile} *}
 | 
| 13114 | 1733 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1734 | lemma length_takeWhile_le: "length (takeWhile P xs) \<le> length xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1735 | by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1736 | |
| 13142 | 1737 | lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" | 
| 13145 | 1738 | by (induct xs) auto | 
| 13114 | 1739 | |
| 13142 | 1740 | lemma takeWhile_append1 [simp]: | 
| 13145 | 1741 | "[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs" | 
| 1742 | by (induct xs) auto | |
| 13114 | 1743 | |
| 13142 | 1744 | lemma takeWhile_append2 [simp]: | 
| 13145 | 1745 | "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys" | 
| 1746 | by (induct xs) auto | |
| 13114 | 1747 | |
| 13142 | 1748 | lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs" | 
| 13145 | 1749 | by (induct xs) auto | 
| 13114 | 1750 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1751 | lemma takeWhile_nth: "j < length (takeWhile P xs) \<Longrightarrow> takeWhile P xs ! j = xs ! j" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1752 | apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1753 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1754 | lemma dropWhile_nth: "j < length (dropWhile P xs) \<Longrightarrow> dropWhile P xs ! j = xs ! (j + length (takeWhile P xs))" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1755 | apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1756 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1757 | lemma length_dropWhile_le: "length (dropWhile P xs) \<le> length xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1758 | by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1759 | |
| 13142 | 1760 | lemma dropWhile_append1 [simp]: | 
| 13145 | 1761 | "[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" | 
| 1762 | by (induct xs) auto | |
| 13114 | 1763 | |
| 13142 | 1764 | lemma dropWhile_append2 [simp]: | 
| 13145 | 1765 | "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" | 
| 1766 | by (induct xs) auto | |
| 13114 | 1767 | |
| 23971 
e6d505d5b03d
renamed lemma "set_take_whileD" to "set_takeWhileD"
 krauss parents: 
23740diff
changeset | 1768 | lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x" | 
| 13145 | 1769 | by (induct xs) (auto split: split_if_asm) | 
| 13114 | 1770 | |
| 13913 | 1771 | lemma takeWhile_eq_all_conv[simp]: | 
| 1772 | "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" | |
| 1773 | by(induct xs, auto) | |
| 1774 | ||
| 1775 | lemma dropWhile_eq_Nil_conv[simp]: | |
| 1776 | "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" | |
| 1777 | by(induct xs, auto) | |
| 1778 | ||
| 1779 | lemma dropWhile_eq_Cons_conv: | |
| 1780 | "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)" | |
| 1781 | by(induct xs, auto) | |
| 1782 | ||
| 31077 | 1783 | lemma distinct_takeWhile[simp]: "distinct xs ==> distinct (takeWhile P xs)" | 
| 1784 | by (induct xs) (auto dest: set_takeWhileD) | |
| 1785 | ||
| 1786 | lemma distinct_dropWhile[simp]: "distinct xs ==> distinct (dropWhile P xs)" | |
| 1787 | by (induct xs) auto | |
| 1788 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1789 | lemma takeWhile_map: "takeWhile P (map f xs) = map f (takeWhile (P \<circ> f) xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1790 | by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1791 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1792 | lemma dropWhile_map: "dropWhile P (map f xs) = map f (dropWhile (P \<circ> f) xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1793 | by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1794 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1795 | lemma takeWhile_eq_take: "takeWhile P xs = take (length (takeWhile P xs)) xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1796 | by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1797 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1798 | lemma dropWhile_eq_drop: "dropWhile P xs = drop (length (takeWhile P xs)) xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1799 | by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1800 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1801 | lemma hd_dropWhile: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1802 | "dropWhile P xs \<noteq> [] \<Longrightarrow> \<not> P (hd (dropWhile P xs))" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1803 | using assms by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1804 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1805 | lemma takeWhile_eq_filter: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1806 | assumes "\<And> x. x \<in> set (dropWhile P xs) \<Longrightarrow> \<not> P x" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1807 | shows "takeWhile P xs = filter P xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1808 | proof - | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1809 | have A: "filter P xs = filter P (takeWhile P xs @ dropWhile P xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1810 | by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1811 | have B: "filter P (dropWhile P xs) = []" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1812 | unfolding filter_empty_conv using assms by blast | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1813 | have "filter P xs = takeWhile P xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1814 | unfolding A filter_append B | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1815 | by (auto simp add: filter_id_conv dest: set_takeWhileD) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1816 | thus ?thesis .. | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1817 | qed | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1818 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1819 | lemma takeWhile_eq_take_P_nth: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1820 | "\<lbrakk> \<And> i. \<lbrakk> i < n ; i < length xs \<rbrakk> \<Longrightarrow> P (xs ! i) ; n < length xs \<Longrightarrow> \<not> P (xs ! n) \<rbrakk> \<Longrightarrow> | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1821 | takeWhile P xs = take n xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1822 | proof (induct xs arbitrary: n) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1823 | case (Cons x xs) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1824 | thus ?case | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1825 | proof (cases n) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1826 | case (Suc n') note this[simp] | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1827 | have "P x" using Cons.prems(1)[of 0] by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1828 | moreover have "takeWhile P xs = take n' xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1829 | proof (rule Cons.hyps) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1830 | case goal1 thus "P (xs ! i)" using Cons.prems(1)[of "Suc i"] by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1831 | next case goal2 thus ?case using Cons by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1832 | qed | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1833 | ultimately show ?thesis by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1834 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1835 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1836 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1837 | lemma nth_length_takeWhile: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1838 | "length (takeWhile P xs) < length xs \<Longrightarrow> \<not> P (xs ! length (takeWhile P xs))" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1839 | by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1840 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1841 | lemma length_takeWhile_less_P_nth: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1842 | assumes all: "\<And> i. i < j \<Longrightarrow> P (xs ! i)" and "j \<le> length xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1843 | shows "j \<le> length (takeWhile P xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1844 | proof (rule classical) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1845 | assume "\<not> ?thesis" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1846 | hence "length (takeWhile P xs) < length xs" using assms by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1847 | thus ?thesis using all `\<not> ?thesis` nth_length_takeWhile[of P xs] by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1848 | qed | 
| 31077 | 1849 | |
| 17501 | 1850 | text{* The following two lemmmas could be generalized to an arbitrary
 | 
| 1851 | property. *} | |
| 1852 | ||
| 1853 | lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> | |
| 1854 | takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))" | |
| 1855 | by(induct xs) (auto simp: takeWhile_tail[where l="[]"]) | |
| 1856 | ||
| 1857 | lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> | |
| 1858 | dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)" | |
| 1859 | apply(induct xs) | |
| 1860 | apply simp | |
| 1861 | apply auto | |
| 1862 | apply(subst dropWhile_append2) | |
| 1863 | apply auto | |
| 1864 | done | |
| 1865 | ||
| 18423 | 1866 | lemma takeWhile_not_last: | 
| 1867 | "\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs" | |
| 1868 | apply(induct xs) | |
| 1869 | apply simp | |
| 1870 | apply(case_tac xs) | |
| 1871 | apply(auto) | |
| 1872 | done | |
| 1873 | ||
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19623diff
changeset | 1874 | lemma takeWhile_cong [fundef_cong, recdef_cong]: | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 1875 | "[| l = k; !!x. x : set l ==> P x = Q x |] | 
| 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 1876 | ==> takeWhile P l = takeWhile Q k" | 
| 24349 | 1877 | by (induct k arbitrary: l) (simp_all) | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 1878 | |
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19623diff
changeset | 1879 | lemma dropWhile_cong [fundef_cong, recdef_cong]: | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 1880 | "[| l = k; !!x. x : set l ==> P x = Q x |] | 
| 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 1881 | ==> dropWhile P l = dropWhile Q k" | 
| 24349 | 1882 | by (induct k arbitrary: l, simp_all) | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 1883 | |
| 13114 | 1884 | |
| 15392 | 1885 | subsubsection {* @{text zip} *}
 | 
| 13114 | 1886 | |
| 13142 | 1887 | lemma zip_Nil [simp]: "zip [] ys = []" | 
| 13145 | 1888 | by (induct ys) auto | 
| 13114 | 1889 | |
| 13142 | 1890 | lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" | 
| 13145 | 1891 | by simp | 
| 13114 | 1892 | |
| 13142 | 1893 | declare zip_Cons [simp del] | 
| 13114 | 1894 | |
| 15281 | 1895 | lemma zip_Cons1: | 
| 1896 | "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)" | |
| 1897 | by(auto split:list.split) | |
| 1898 | ||
| 13142 | 1899 | lemma length_zip [simp]: | 
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 1900 | "length (zip xs ys) = min (length xs) (length ys)" | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 1901 | by (induct xs ys rule:list_induct2') auto | 
| 13114 | 1902 | |
| 1903 | lemma zip_append1: | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 1904 | "zip (xs @ ys) zs = | 
| 13145 | 1905 | zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)" | 
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 1906 | by (induct xs zs rule:list_induct2') auto | 
| 13114 | 1907 | |
| 1908 | lemma zip_append2: | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 1909 | "zip xs (ys @ zs) = | 
| 13145 | 1910 | zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs" | 
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 1911 | by (induct xs ys rule:list_induct2') auto | 
| 13114 | 1912 | |
| 13142 | 1913 | lemma zip_append [simp]: | 
| 1914 | "[| length xs = length us; length ys = length vs |] ==> | |
| 13145 | 1915 | zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" | 
| 1916 | by (simp add: zip_append1) | |
| 13114 | 1917 | |
| 1918 | lemma zip_rev: | |
| 14247 | 1919 | "length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" | 
| 1920 | by (induct rule:list_induct2, simp_all) | |
| 13114 | 1921 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1922 | lemma zip_map_map: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1923 | "zip (map f xs) (map g ys) = map (\<lambda> (x, y). (f x, g y)) (zip xs ys)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1924 | proof (induct xs arbitrary: ys) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1925 | case (Cons x xs) note Cons_x_xs = Cons.hyps | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1926 | show ?case | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1927 | proof (cases ys) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1928 | case (Cons y ys') | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1929 | show ?thesis unfolding Cons using Cons_x_xs by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1930 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1931 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1932 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1933 | lemma zip_map1: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1934 | "zip (map f xs) ys = map (\<lambda>(x, y). (f x, y)) (zip xs ys)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1935 | using zip_map_map[of f xs "\<lambda>x. x" ys] by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1936 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1937 | lemma zip_map2: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1938 | "zip xs (map f ys) = map (\<lambda>(x, y). (x, f y)) (zip xs ys)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1939 | using zip_map_map[of "\<lambda>x. x" xs f ys] by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1940 | |
| 23096 | 1941 | lemma map_zip_map: | 
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1942 | "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1943 | unfolding zip_map1 by auto | 
| 23096 | 1944 | |
| 1945 | lemma map_zip_map2: | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1946 | "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1947 | unfolding zip_map2 by auto | 
| 23096 | 1948 | |
| 31080 | 1949 | text{* Courtesy of Andreas Lochbihler: *}
 | 
| 1950 | lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs" | |
| 1951 | by(induct xs) auto | |
| 1952 | ||
| 13142 | 1953 | lemma nth_zip [simp]: | 
| 24526 | 1954 | "[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)" | 
| 1955 | apply (induct ys arbitrary: i xs, simp) | |
| 13145 | 1956 | apply (case_tac xs) | 
| 1957 | apply (simp_all add: nth.simps split: nat.split) | |
| 1958 | done | |
| 13114 | 1959 | |
| 1960 | lemma set_zip: | |
| 13145 | 1961 | "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
 | 
| 31080 | 1962 | by(simp add: set_conv_nth cong: rev_conj_cong) | 
| 13114 | 1963 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1964 | lemma zip_same: "((a,b) \<in> set (zip xs xs)) = (a \<in> set xs \<and> a = b)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1965 | by(induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1966 | |
| 13114 | 1967 | lemma zip_update: | 
| 31080 | 1968 | "zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" | 
| 1969 | by(rule sym, simp add: update_zip) | |
| 13114 | 1970 | |
| 13142 | 1971 | lemma zip_replicate [simp]: | 
| 24526 | 1972 | "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" | 
| 1973 | apply (induct i arbitrary: j, auto) | |
| 14208 | 1974 | apply (case_tac j, auto) | 
| 13145 | 1975 | done | 
| 13114 | 1976 | |
| 19487 | 1977 | lemma take_zip: | 
| 24526 | 1978 | "take n (zip xs ys) = zip (take n xs) (take n ys)" | 
| 1979 | apply (induct n arbitrary: xs ys) | |
| 19487 | 1980 | apply simp | 
| 1981 | apply (case_tac xs, simp) | |
| 1982 | apply (case_tac ys, simp_all) | |
| 1983 | done | |
| 1984 | ||
| 1985 | lemma drop_zip: | |
| 24526 | 1986 | "drop n (zip xs ys) = zip (drop n xs) (drop n ys)" | 
| 1987 | apply (induct n arbitrary: xs ys) | |
| 19487 | 1988 | apply simp | 
| 1989 | apply (case_tac xs, simp) | |
| 1990 | apply (case_tac ys, simp_all) | |
| 1991 | done | |
| 1992 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1993 | lemma zip_takeWhile_fst: "zip (takeWhile P xs) ys = takeWhile (P \<circ> fst) (zip xs ys)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1994 | proof (induct xs arbitrary: ys) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1995 | case (Cons x xs) thus ?case by (cases ys) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1996 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1997 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1998 | lemma zip_takeWhile_snd: "zip xs (takeWhile P ys) = takeWhile (P \<circ> snd) (zip xs ys)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 1999 | proof (induct xs arbitrary: ys) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2000 | case (Cons x xs) thus ?case by (cases ys) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2001 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2002 | |
| 22493 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2003 | lemma set_zip_leftD: | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2004 | "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs" | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2005 | by (induct xs ys rule:list_induct2') auto | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2006 | |
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2007 | lemma set_zip_rightD: | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2008 | "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys" | 
| 
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
 krauss parents: 
22422diff
changeset | 2009 | by (induct xs ys rule:list_induct2') auto | 
| 13142 | 2010 | |
| 23983 | 2011 | lemma in_set_zipE: | 
| 2012 | "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R" | |
| 2013 | by(blast dest: set_zip_leftD set_zip_rightD) | |
| 2014 | ||
| 29829 | 2015 | lemma zip_map_fst_snd: | 
| 2016 | "zip (map fst zs) (map snd zs) = zs" | |
| 2017 | by (induct zs) simp_all | |
| 2018 | ||
| 2019 | lemma zip_eq_conv: | |
| 2020 | "length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys" | |
| 2021 | by (auto simp add: zip_map_fst_snd) | |
| 2022 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2023 | lemma distinct_zipI1: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2024 | "distinct xs \<Longrightarrow> distinct (zip xs ys)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2025 | proof (induct xs arbitrary: ys) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2026 | case (Cons x xs) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2027 | show ?case | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2028 | proof (cases ys) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2029 | case (Cons y ys') | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2030 | have "(x, y) \<notin> set (zip xs ys')" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2031 | using Cons.prems by (auto simp: set_zip) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2032 | thus ?thesis | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2033 | unfolding Cons zip_Cons_Cons distinct.simps | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2034 | using Cons.hyps Cons.prems by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2035 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2036 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2037 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2038 | lemma distinct_zipI2: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2039 | "distinct xs \<Longrightarrow> distinct (zip xs ys)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2040 | proof (induct xs arbitrary: ys) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2041 | case (Cons x xs) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2042 | show ?case | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2043 | proof (cases ys) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2044 | case (Cons y ys') | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2045 | have "(x, y) \<notin> set (zip xs ys')" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2046 | using Cons.prems by (auto simp: set_zip) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2047 | thus ?thesis | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2048 | unfolding Cons zip_Cons_Cons distinct.simps | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2049 | using Cons.hyps Cons.prems by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2050 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2051 | qed simp | 
| 29829 | 2052 | |
| 15392 | 2053 | subsubsection {* @{text list_all2} *}
 | 
| 13114 | 2054 | |
| 14316 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 kleing parents: 
14302diff
changeset | 2055 | lemma list_all2_lengthD [intro?]: | 
| 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 kleing parents: 
14302diff
changeset | 2056 | "list_all2 P xs ys ==> length xs = length ys" | 
| 24349 | 2057 | by (simp add: list_all2_def) | 
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2058 | |
| 19787 | 2059 | lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])" | 
| 24349 | 2060 | by (simp add: list_all2_def) | 
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2061 | |
| 19787 | 2062 | lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])" | 
| 24349 | 2063 | by (simp add: list_all2_def) | 
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2064 | |
| 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2065 | lemma list_all2_Cons [iff, code]: | 
| 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 2066 | "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)" | 
| 24349 | 2067 | by (auto simp add: list_all2_def) | 
| 13114 | 2068 | |
| 2069 | lemma list_all2_Cons1: | |
| 13145 | 2070 | "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" | 
| 2071 | by (cases ys) auto | |
| 13114 | 2072 | |
| 2073 | lemma list_all2_Cons2: | |
| 13145 | 2074 | "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" | 
| 2075 | by (cases xs) auto | |
| 13114 | 2076 | |
| 13142 | 2077 | lemma list_all2_rev [iff]: | 
| 13145 | 2078 | "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" | 
| 2079 | by (simp add: list_all2_def zip_rev cong: conj_cong) | |
| 13114 | 2080 | |
| 13863 | 2081 | lemma list_all2_rev1: | 
| 2082 | "list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" | |
| 2083 | by (subst list_all2_rev [symmetric]) simp | |
| 2084 | ||
| 13114 | 2085 | lemma list_all2_append1: | 
| 13145 | 2086 | "list_all2 P (xs @ ys) zs = | 
| 2087 | (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> | |
| 2088 | list_all2 P xs us \<and> list_all2 P ys vs)" | |
| 2089 | apply (simp add: list_all2_def zip_append1) | |
| 2090 | apply (rule iffI) | |
| 2091 | apply (rule_tac x = "take (length xs) zs" in exI) | |
| 2092 | apply (rule_tac x = "drop (length xs) zs" in exI) | |
| 14208 | 2093 | apply (force split: nat_diff_split simp add: min_def, clarify) | 
| 13145 | 2094 | apply (simp add: ball_Un) | 
| 2095 | done | |
| 13114 | 2096 | |
| 2097 | lemma list_all2_append2: | |
| 13145 | 2098 | "list_all2 P xs (ys @ zs) = | 
| 2099 | (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> | |
| 2100 | list_all2 P us ys \<and> list_all2 P vs zs)" | |
| 2101 | apply (simp add: list_all2_def zip_append2) | |
| 2102 | apply (rule iffI) | |
| 2103 | apply (rule_tac x = "take (length ys) xs" in exI) | |
| 2104 | apply (rule_tac x = "drop (length ys) xs" in exI) | |
| 14208 | 2105 | apply (force split: nat_diff_split simp add: min_def, clarify) | 
| 13145 | 2106 | apply (simp add: ball_Un) | 
| 2107 | done | |
| 13114 | 2108 | |
| 13863 | 2109 | lemma list_all2_append: | 
| 14247 | 2110 | "length xs = length ys \<Longrightarrow> | 
| 2111 | list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)" | |
| 2112 | by (induct rule:list_induct2, simp_all) | |
| 13863 | 2113 | |
| 2114 | lemma list_all2_appendI [intro?, trans]: | |
| 2115 | "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)" | |
| 24349 | 2116 | by (simp add: list_all2_append list_all2_lengthD) | 
| 13863 | 2117 | |
| 13114 | 2118 | lemma list_all2_conv_all_nth: | 
| 13145 | 2119 | "list_all2 P xs ys = | 
| 2120 | (length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))" | |
| 2121 | by (force simp add: list_all2_def set_zip) | |
| 13114 | 2122 | |
| 13883 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2123 | lemma list_all2_trans: | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2124 | assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c" | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2125 | shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs" | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2126 | (is "!!bs cs. PROP ?Q as bs cs") | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2127 | proof (induct as) | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2128 | fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs" | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2129 | show "!!cs. PROP ?Q (x # xs) bs cs" | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2130 | proof (induct bs) | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2131 | fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs" | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2132 | show "PROP ?Q (x # xs) (y # ys) cs" | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2133 | by (induct cs) (auto intro: tr I1 I2) | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2134 | qed simp | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2135 | qed simp | 
| 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2136 | |
| 13863 | 2137 | lemma list_all2_all_nthI [intro?]: | 
| 2138 | "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b" | |
| 24349 | 2139 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2140 | |
| 14395 | 2141 | lemma list_all2I: | 
| 2142 | "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b" | |
| 24349 | 2143 | by (simp add: list_all2_def) | 
| 14395 | 2144 | |
| 14328 | 2145 | lemma list_all2_nthD: | 
| 13863 | 2146 | "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" | 
| 24349 | 2147 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2148 | |
| 14302 | 2149 | lemma list_all2_nthD2: | 
| 2150 | "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" | |
| 24349 | 2151 | by (frule list_all2_lengthD) (auto intro: list_all2_nthD) | 
| 14302 | 2152 | |
| 13863 | 2153 | lemma list_all2_map1: | 
| 2154 | "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs" | |
| 24349 | 2155 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2156 | |
| 2157 | lemma list_all2_map2: | |
| 2158 | "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs" | |
| 24349 | 2159 | by (auto simp add: list_all2_conv_all_nth) | 
| 13863 | 2160 | |
| 14316 
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
 kleing parents: 
14302diff
changeset | 2161 | lemma list_all2_refl [intro?]: | 
| 13863 | 2162 | "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs" | 
| 24349 | 2163 | by (simp add: list_all2_conv_all_nth) | 
| 13863 | 2164 | |
| 2165 | lemma list_all2_update_cong: | |
| 2166 | "\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" | |
| 24349 | 2167 | by (simp add: list_all2_conv_all_nth nth_list_update) | 
| 13863 | 2168 | |
| 2169 | lemma list_all2_update_cong2: | |
| 2170 | "\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" | |
| 24349 | 2171 | by (simp add: list_all2_lengthD list_all2_update_cong) | 
| 13863 | 2172 | |
| 14302 | 2173 | lemma list_all2_takeI [simp,intro?]: | 
| 24526 | 2174 | "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)" | 
| 2175 | apply (induct xs arbitrary: n ys) | |
| 2176 | apply simp | |
| 2177 | apply (clarsimp simp add: list_all2_Cons1) | |
| 2178 | apply (case_tac n) | |
| 2179 | apply auto | |
| 2180 | done | |
| 14302 | 2181 | |
| 2182 | lemma list_all2_dropI [simp,intro?]: | |
| 24526 | 2183 | "list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)" | 
| 2184 | apply (induct as arbitrary: n bs, simp) | |
| 2185 | apply (clarsimp simp add: list_all2_Cons1) | |
| 2186 | apply (case_tac n, simp, simp) | |
| 2187 | done | |
| 13863 | 2188 | |
| 14327 | 2189 | lemma list_all2_mono [intro?]: | 
| 24526 | 2190 | "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys" | 
| 2191 | apply (induct xs arbitrary: ys, simp) | |
| 2192 | apply (case_tac ys, auto) | |
| 2193 | done | |
| 13863 | 2194 | |
| 22551 | 2195 | lemma list_all2_eq: | 
| 2196 | "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys" | |
| 24349 | 2197 | by (induct xs ys rule: list_induct2') auto | 
| 22551 | 2198 | |
| 13142 | 2199 | |
| 15392 | 2200 | subsubsection {* @{text foldl} and @{text foldr} *}
 | 
| 13142 | 2201 | |
| 2202 | lemma foldl_append [simp]: | |
| 24526 | 2203 | "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys" | 
| 2204 | by (induct xs arbitrary: a) auto | |
| 13142 | 2205 | |
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 2206 | lemma foldr_append[simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)" | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 2207 | by (induct xs) auto | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 2208 | |
| 23096 | 2209 | lemma foldr_map: "foldr g (map f xs) a = foldr (g o f) xs a" | 
| 2210 | by(induct xs) simp_all | |
| 2211 | ||
| 24449 | 2212 | text{* For efficient code generation: avoid intermediate list. *}
 | 
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 2213 | lemma foldl_map[code_unfold]: | 
| 24449 | 2214 | "foldl g a (map f xs) = foldl (%a x. g a (f x)) a xs" | 
| 23096 | 2215 | by(induct xs arbitrary:a) simp_all | 
| 2216 | ||
| 31930 | 2217 | lemma foldl_apply_inv: | 
| 2218 | assumes "\<And>x. g (h x) = x" | |
| 2219 | shows "foldl f (g s) xs = g (foldl (\<lambda>s x. h (f (g s) x)) s xs)" | |
| 2220 | by (rule sym, induct xs arbitrary: s) (simp_all add: assms) | |
| 2221 | ||
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19623diff
changeset | 2222 | lemma foldl_cong [fundef_cong, recdef_cong]: | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2223 | "[| a = b; l = k; !!a x. x : set l ==> f a x = g a x |] | 
| 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2224 | ==> foldl f a l = foldl g b k" | 
| 24349 | 2225 | by (induct k arbitrary: a b l) simp_all | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2226 | |
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19623diff
changeset | 2227 | lemma foldr_cong [fundef_cong, recdef_cong]: | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2228 | "[| a = b; l = k; !!a x. x : set l ==> f x a = g x a |] | 
| 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2229 | ==> foldr f l a = foldr g k b" | 
| 24349 | 2230 | by (induct k arbitrary: a b l) simp_all | 
| 18336 
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
 krauss parents: 
18049diff
changeset | 2231 | |
| 24449 | 2232 | lemma (in semigroup_add) foldl_assoc: | 
| 25062 | 2233 | shows "foldl op+ (x+y) zs = x + (foldl op+ y zs)" | 
| 24449 | 2234 | by (induct zs arbitrary: y) (simp_all add:add_assoc) | 
| 2235 | ||
| 2236 | lemma (in monoid_add) foldl_absorb0: | |
| 25062 | 2237 | shows "x + (foldl op+ 0 zs) = foldl op+ x zs" | 
| 24449 | 2238 | by (induct zs) (simp_all add:foldl_assoc) | 
| 2239 | ||
| 2240 | ||
| 23096 | 2241 | text{* The ``First Duality Theorem'' in Bird \& Wadler: *}
 | 
| 2242 | ||
| 2243 | lemma foldl_foldr1_lemma: | |
| 2244 | "foldl op + a xs = a + foldr op + xs (0\<Colon>'a::monoid_add)" | |
| 2245 | by (induct xs arbitrary: a) (auto simp:add_assoc) | |
| 2246 | ||
| 2247 | corollary foldl_foldr1: | |
| 2248 | "foldl op + 0 xs = foldr op + xs (0\<Colon>'a::monoid_add)" | |
| 2249 | by (simp add:foldl_foldr1_lemma) | |
| 2250 | ||
| 2251 | ||
| 2252 | text{* The ``Third Duality Theorem'' in Bird \& Wadler: *}
 | |
| 2253 | ||
| 14402 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 2254 | lemma foldr_foldl: "foldr f xs a = foldl (%x y. f y x) a (rev xs)" | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 2255 | by (induct xs) auto | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 2256 | |
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 2257 | lemma foldl_foldr: "foldl f a xs = foldr (%x y. f y x) (rev xs) a" | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 2258 | by (simp add: foldr_foldl [of "%x y. f y x" "rev xs"]) | 
| 
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
 nipkow parents: 
14395diff
changeset | 2259 | |
| 25062 | 2260 | lemma (in ab_semigroup_add) foldr_conv_foldl: "foldr op + xs a = foldl op + a xs" | 
| 24471 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2261 | by (induct xs, auto simp add: foldl_assoc add_commute) | 
| 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2262 | |
| 13142 | 2263 | text {*
 | 
| 13145 | 2264 | Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
 | 
| 2265 | difficult to use because it requires an additional transitivity step. | |
| 13142 | 2266 | *} | 
| 2267 | ||
| 24526 | 2268 | lemma start_le_sum: "(m::nat) <= n ==> m <= foldl (op +) n ns" | 
| 2269 | by (induct ns arbitrary: n) auto | |
| 2270 | ||
| 2271 | lemma elem_le_sum: "(n::nat) : set ns ==> n <= foldl (op +) 0 ns" | |
| 13145 | 2272 | by (force intro: start_le_sum simp add: in_set_conv_decomp) | 
| 13142 | 2273 | |
| 2274 | lemma sum_eq_0_conv [iff]: | |
| 24526 | 2275 | "(foldl (op +) (m::nat) ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))" | 
| 2276 | by (induct ns arbitrary: m) auto | |
| 13114 | 2277 | |
| 24471 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2278 | lemma foldr_invariant: | 
| 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2279 | "\<lbrakk>Q x ; \<forall> x\<in> set xs. P x; \<forall> x y. P x \<and> Q y \<longrightarrow> Q (f x y) \<rbrakk> \<Longrightarrow> Q (foldr f xs x)" | 
| 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2280 | by (induct xs, simp_all) | 
| 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2281 | |
| 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2282 | lemma foldl_invariant: | 
| 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2283 | "\<lbrakk>Q x ; \<forall> x\<in> set xs. P x; \<forall> x y. P x \<and> Q y \<longrightarrow> Q (f y x) \<rbrakk> \<Longrightarrow> Q (foldl f x xs)" | 
| 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2284 | by (induct xs arbitrary: x, simp_all) | 
| 
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
 chaieb parents: 
24461diff
changeset | 2285 | |
| 31455 | 2286 | text {* @{const foldl} and @{const concat} *}
 | 
| 24449 | 2287 | |
| 2288 | lemma foldl_conv_concat: | |
| 29782 | 2289 | "foldl (op @) xs xss = xs @ concat xss" | 
| 2290 | proof (induct xss arbitrary: xs) | |
| 2291 | case Nil show ?case by simp | |
| 2292 | next | |
| 2293 | interpret monoid_add "[]" "op @" proof qed simp_all | |
| 2294 | case Cons then show ?case by (simp add: foldl_absorb0) | |
| 2295 | qed | |
| 2296 | ||
| 2297 | lemma concat_conv_foldl: "concat xss = foldl (op @) [] xss" | |
| 2298 | by (simp add: foldl_conv_concat) | |
| 2299 | ||
| 31455 | 2300 | text {* @{const Finite_Set.fold} and @{const foldl} *}
 | 
| 2301 | ||
| 2302 | lemma (in fun_left_comm_idem) fold_set: | |
| 2303 | "fold f y (set xs) = foldl (\<lambda>y x. f x y) y xs" | |
| 2304 | by (rule sym, induct xs arbitrary: y) (simp_all add: fold_fun_comm) | |
| 2305 | ||
| 32681 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2306 | lemma (in ab_semigroup_idem_mult) fold1_set: | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2307 | assumes "xs \<noteq> []" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2308 | shows "fold1 times (set xs) = foldl times (hd xs) (tl xs)" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2309 | proof - | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2310 | interpret fun_left_comm_idem times by (fact fun_left_comm_idem) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2311 | from assms obtain y ys where xs: "xs = y # ys" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2312 | by (cases xs) auto | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2313 | show ?thesis | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2314 |   proof (cases "set ys = {}")
 | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2315 | case True with xs show ?thesis by simp | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2316 | next | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2317 | case False | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2318 | then have "fold1 times (insert y (set ys)) = fold times y (set ys)" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2319 | by (simp only: finite_set fold1_eq_fold_idem) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2320 | with xs show ?thesis by (simp add: fold_set mult_commute) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2321 | qed | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2322 | qed | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2323 | |
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2324 | lemma (in lattice) Inf_fin_set_fold [code_unfold]: | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2325 | "Inf_fin (set (x # xs)) = foldl inf x xs" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2326 | proof - | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2327 | interpret ab_semigroup_idem_mult "inf :: 'a \<Rightarrow> 'a \<Rightarrow> 'a" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2328 | by (fact ab_semigroup_idem_mult_inf) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2329 | show ?thesis | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2330 | by (simp add: Inf_fin_def fold1_set del: set.simps) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2331 | qed | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2332 | |
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2333 | lemma (in lattice) Sup_fin_set_fold [code_unfold]: | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2334 | "Sup_fin (set (x # xs)) = foldl sup x xs" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2335 | proof - | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2336 | interpret ab_semigroup_idem_mult "sup :: 'a \<Rightarrow> 'a \<Rightarrow> 'a" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2337 | by (fact ab_semigroup_idem_mult_sup) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2338 | show ?thesis | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2339 | by (simp add: Sup_fin_def fold1_set del: set.simps) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2340 | qed | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2341 | |
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2342 | lemma (in linorder) Min_fin_set_fold [code_unfold]: | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2343 | "Min (set (x # xs)) = foldl min x xs" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2344 | proof - | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2345 | interpret ab_semigroup_idem_mult "min :: 'a \<Rightarrow> 'a \<Rightarrow> 'a" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2346 | by (fact ab_semigroup_idem_mult_min) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2347 | show ?thesis | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2348 | by (simp add: Min_def fold1_set del: set.simps) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2349 | qed | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2350 | |
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2351 | lemma (in linorder) Max_fin_set_fold [code_unfold]: | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2352 | "Max (set (x # xs)) = foldl max x xs" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2353 | proof - | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2354 | interpret ab_semigroup_idem_mult "max :: 'a \<Rightarrow> 'a \<Rightarrow> 'a" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2355 | by (fact ab_semigroup_idem_mult_max) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2356 | show ?thesis | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2357 | by (simp add: Max_def fold1_set del: set.simps) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2358 | qed | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2359 | |
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2360 | lemma (in complete_lattice) Inf_set_fold [code_unfold]: | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2361 | "Inf (set xs) = foldl inf top xs" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2362 | by (cases xs) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2363 | (simp_all add: Inf_fin_Inf [symmetric] Inf_fin_set_fold | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2364 | inf_commute del: set.simps, simp add: top_def) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2365 | |
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2366 | lemma (in complete_lattice) Sup_set_fold [code_unfold]: | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2367 | "Sup (set xs) = foldl sup bot xs" | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2368 | by (cases xs) | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2369 | (simp_all add: Sup_fin_Sup [symmetric] Sup_fin_set_fold | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 2370 | sup_commute del: set.simps, simp add: bot_def) | 
| 31455 | 2371 | |
| 24449 | 2372 | |
| 23096 | 2373 | subsubsection {* List summation: @{const listsum} and @{text"\<Sum>"}*}
 | 
| 2374 | ||
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2375 | lemma listsum_append [simp]: "listsum (xs @ ys) = listsum xs + listsum ys" | 
| 24449 | 2376 | by (induct xs) (simp_all add:add_assoc) | 
| 2377 | ||
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2378 | lemma listsum_rev [simp]: | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2379 | fixes xs :: "'a\<Colon>comm_monoid_add list" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2380 | shows "listsum (rev xs) = listsum xs" | 
| 24449 | 2381 | by (induct xs) (simp_all add:add_ac) | 
| 2382 | ||
| 31022 | 2383 | lemma listsum_map_remove1: | 
| 2384 | fixes f :: "'a \<Rightarrow> ('b::comm_monoid_add)"
 | |
| 2385 | shows "x : set xs \<Longrightarrow> listsum(map f xs) = f x + listsum(map f (remove1 x xs))" | |
| 2386 | by (induct xs)(auto simp add:add_ac) | |
| 2387 | ||
| 2388 | lemma list_size_conv_listsum: | |
| 2389 | "list_size f xs = listsum (map f xs) + size xs" | |
| 2390 | by(induct xs) auto | |
| 2391 | ||
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2392 | lemma listsum_foldr: "listsum xs = foldr (op +) xs 0" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2393 | by (induct xs) auto | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2394 | |
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2395 | lemma length_concat: "length (concat xss) = listsum (map length xss)" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2396 | by (induct xss) simp_all | 
| 23096 | 2397 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2398 | lemma listsum_map_filter: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2399 | fixes f :: "'a \<Rightarrow> 'b \<Colon> comm_monoid_add" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2400 | assumes "\<And> x. \<lbrakk> x \<in> set xs ; \<not> P x \<rbrakk> \<Longrightarrow> f x = 0" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2401 | shows "listsum (map f (filter P xs)) = listsum (map f xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2402 | using assms by (induct xs) auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2403 | |
| 24449 | 2404 | text{* For efficient code generation ---
 | 
| 2405 |        @{const listsum} is not tail recursive but @{const foldl} is. *}
 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 2406 | lemma listsum[code_unfold]: "listsum xs = foldl (op +) 0 xs" | 
| 23096 | 2407 | by(simp add:listsum_foldr foldl_foldr1) | 
| 2408 | ||
| 31077 | 2409 | lemma distinct_listsum_conv_Setsum: | 
| 2410 | "distinct xs \<Longrightarrow> listsum xs = Setsum(set xs)" | |
| 2411 | by (induct xs) simp_all | |
| 2412 | ||
| 24449 | 2413 | |
| 23096 | 2414 | text{* Some syntactic sugar for summing a function over a list: *}
 | 
| 2415 | ||
| 2416 | syntax | |
| 2417 |   "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
 | |
| 2418 | syntax (xsymbols) | |
| 2419 |   "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
 | |
| 2420 | syntax (HTML output) | |
| 2421 |   "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
 | |
| 2422 | ||
| 2423 | translations -- {* Beware of argument permutation! *}
 | |
| 2424 | "SUM x<-xs. b" == "CONST listsum (map (%x. b) xs)" | |
| 2425 | "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (map (%x. b) xs)" | |
| 2426 | ||
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2427 | lemma listsum_triv: "(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2428 | by (induct xs) (simp_all add: left_distrib) | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2429 | |
| 23096 | 2430 | lemma listsum_0 [simp]: "(\<Sum>x\<leftarrow>xs. 0) = 0" | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2431 | by (induct xs) (simp_all add: left_distrib) | 
| 23096 | 2432 | |
| 2433 | text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
 | |
| 2434 | lemma uminus_listsum_map: | |
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2435 | fixes f :: "'a \<Rightarrow> 'b\<Colon>ab_group_add" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2436 | shows "- listsum (map f xs) = (listsum (map (uminus o f) xs))" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 2437 | by (induct xs) simp_all | 
| 23096 | 2438 | |
| 31258 | 2439 | lemma listsum_addf: | 
| 2440 | fixes f g :: "'a \<Rightarrow> 'b::comm_monoid_add" | |
| 2441 | shows "(\<Sum>x\<leftarrow>xs. f x + g x) = listsum (map f xs) + listsum (map g xs)" | |
| 2442 | by (induct xs) (simp_all add: algebra_simps) | |
| 2443 | ||
| 2444 | lemma listsum_subtractf: | |
| 2445 | fixes f g :: "'a \<Rightarrow> 'b::ab_group_add" | |
| 2446 | shows "(\<Sum>x\<leftarrow>xs. f x - g x) = listsum (map f xs) - listsum (map g xs)" | |
| 2447 | by (induct xs) simp_all | |
| 2448 | ||
| 2449 | lemma listsum_const_mult: | |
| 2450 | fixes f :: "'a \<Rightarrow> 'b::semiring_0" | |
| 2451 | shows "(\<Sum>x\<leftarrow>xs. c * f x) = c * (\<Sum>x\<leftarrow>xs. f x)" | |
| 2452 | by (induct xs, simp_all add: algebra_simps) | |
| 2453 | ||
| 2454 | lemma listsum_mult_const: | |
| 2455 | fixes f :: "'a \<Rightarrow> 'b::semiring_0" | |
| 2456 | shows "(\<Sum>x\<leftarrow>xs. f x * c) = (\<Sum>x\<leftarrow>xs. f x) * c" | |
| 2457 | by (induct xs, simp_all add: algebra_simps) | |
| 2458 | ||
| 2459 | lemma listsum_abs: | |
| 2460 | fixes xs :: "'a::pordered_ab_group_add_abs list" | |
| 2461 | shows "\<bar>listsum xs\<bar> \<le> listsum (map abs xs)" | |
| 2462 | by (induct xs, simp, simp add: order_trans [OF abs_triangle_ineq]) | |
| 2463 | ||
| 2464 | lemma listsum_mono: | |
| 2465 |   fixes f g :: "'a \<Rightarrow> 'b::{comm_monoid_add, pordered_ab_semigroup_add}"
 | |
| 2466 | shows "(\<And>x. x \<in> set xs \<Longrightarrow> f x \<le> g x) \<Longrightarrow> (\<Sum>x\<leftarrow>xs. f x) \<le> (\<Sum>x\<leftarrow>xs. g x)" | |
| 2467 | by (induct xs, simp, simp add: add_mono) | |
| 2468 | ||
| 13114 | 2469 | |
| 24645 | 2470 | subsubsection {* @{text upt} *}
 | 
| 13114 | 2471 | |
| 17090 | 2472 | lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])" | 
| 2473 | -- {* simp does not terminate! *}
 | |
| 13145 | 2474 | by (induct j) auto | 
| 13142 | 2475 | |
| 32005 | 2476 | lemmas upt_rec_number_of[simp] = upt_rec[of "number_of m" "number_of n", standard] | 
| 2477 | ||
| 15425 | 2478 | lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []" | 
| 13145 | 2479 | by (subst upt_rec) simp | 
| 13114 | 2480 | |
| 15425 | 2481 | lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)" | 
| 15281 | 2482 | by(induct j)simp_all | 
| 2483 | ||
| 2484 | lemma upt_eq_Cons_conv: | |
| 24526 | 2485 | "([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)" | 
| 2486 | apply(induct j arbitrary: x xs) | |
| 15281 | 2487 | apply simp | 
| 2488 | apply(clarsimp simp add: append_eq_Cons_conv) | |
| 2489 | apply arith | |
| 2490 | done | |
| 2491 | ||
| 15425 | 2492 | lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]" | 
| 13145 | 2493 | -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
 | 
| 2494 | by simp | |
| 13114 | 2495 | |
| 15425 | 2496 | lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]" | 
| 26734 | 2497 | by (simp add: upt_rec) | 
| 13114 | 2498 | |
| 15425 | 2499 | lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]" | 
| 13145 | 2500 | -- {* LOOPS as a simprule, since @{text "j <= j"}. *}
 | 
| 2501 | by (induct k) auto | |
| 13114 | 2502 | |
| 15425 | 2503 | lemma length_upt [simp]: "length [i..<j] = j - i" | 
| 13145 | 2504 | by (induct j) (auto simp add: Suc_diff_le) | 
| 13114 | 2505 | |
| 15425 | 2506 | lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k" | 
| 13145 | 2507 | apply (induct j) | 
| 2508 | apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split) | |
| 2509 | done | |
| 13114 | 2510 | |
| 17906 | 2511 | |
| 2512 | lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i" | |
| 2513 | by(simp add:upt_conv_Cons) | |
| 2514 | ||
| 2515 | lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1" | |
| 2516 | apply(cases j) | |
| 2517 | apply simp | |
| 2518 | by(simp add:upt_Suc_append) | |
| 2519 | ||
| 24526 | 2520 | lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]" | 
| 2521 | apply (induct m arbitrary: i, simp) | |
| 13145 | 2522 | apply (subst upt_rec) | 
| 2523 | apply (rule sym) | |
| 2524 | apply (subst upt_rec) | |
| 2525 | apply (simp del: upt.simps) | |
| 2526 | done | |
| 3507 | 2527 | |
| 17501 | 2528 | lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]" | 
| 2529 | apply(induct j) | |
| 2530 | apply auto | |
| 2531 | done | |
| 2532 | ||
| 24645 | 2533 | lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]" | 
| 13145 | 2534 | by (induct n) auto | 
| 13114 | 2535 | |
| 24526 | 2536 | lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)" | 
| 2537 | apply (induct n m arbitrary: i rule: diff_induct) | |
| 13145 | 2538 | prefer 3 apply (subst map_Suc_upt[symmetric]) | 
| 2539 | apply (auto simp add: less_diff_conv nth_upt) | |
| 2540 | done | |
| 13114 | 2541 | |
| 13883 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2542 | lemma nth_take_lemma: | 
| 24526 | 2543 | "k <= length xs ==> k <= length ys ==> | 
| 13883 
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
 berghofe parents: 
13863diff
changeset | 2544 | (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys" | 
| 24526 | 2545 | apply (atomize, induct k arbitrary: xs ys) | 
| 14208 | 2546 | apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify) | 
| 13145 | 2547 | txt {* Both lists must be non-empty *}
 | 
| 14208 | 2548 | apply (case_tac xs, simp) | 
| 2549 | apply (case_tac ys, clarify) | |
| 13145 | 2550 | apply (simp (no_asm_use)) | 
| 2551 | apply clarify | |
| 2552 | txt {* prenexing's needed, not miniscoping *}
 | |
| 2553 | apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps) | |
| 2554 | apply blast | |
| 2555 | done | |
| 13114 | 2556 | |
| 2557 | lemma nth_equalityI: | |
| 2558 | "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys" | |
| 13145 | 2559 | apply (frule nth_take_lemma [OF le_refl eq_imp_le]) | 
| 2560 | apply (simp_all add: take_all) | |
| 2561 | done | |
| 13142 | 2562 | |
| 24796 | 2563 | lemma map_nth: | 
| 2564 | "map (\<lambda>i. xs ! i) [0..<length xs] = xs" | |
| 2565 | by (rule nth_equalityI, auto) | |
| 2566 | ||
| 13863 | 2567 | (* needs nth_equalityI *) | 
| 2568 | lemma list_all2_antisym: | |
| 2569 | "\<lbrakk> (\<And>x y. \<lbrakk>P x y; Q y x\<rbrakk> \<Longrightarrow> x = y); list_all2 P xs ys; list_all2 Q ys xs \<rbrakk> | |
| 2570 | \<Longrightarrow> xs = ys" | |
| 2571 | apply (simp add: list_all2_conv_all_nth) | |
| 14208 | 2572 | apply (rule nth_equalityI, blast, simp) | 
| 13863 | 2573 | done | 
| 2574 | ||
| 13142 | 2575 | lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys" | 
| 13145 | 2576 | -- {* The famous take-lemma. *}
 | 
| 2577 | apply (drule_tac x = "max (length xs) (length ys)" in spec) | |
| 2578 | apply (simp add: le_max_iff_disj take_all) | |
| 2579 | done | |
| 13142 | 2580 | |
| 2581 | ||
| 15302 | 2582 | lemma take_Cons': | 
| 2583 | "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)" | |
| 2584 | by (cases n) simp_all | |
| 2585 | ||
| 2586 | lemma drop_Cons': | |
| 2587 | "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)" | |
| 2588 | by (cases n) simp_all | |
| 2589 | ||
| 2590 | lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))" | |
| 2591 | by (cases n) simp_all | |
| 2592 | ||
| 18622 | 2593 | lemmas take_Cons_number_of = take_Cons'[of "number_of v",standard] | 
| 2594 | lemmas drop_Cons_number_of = drop_Cons'[of "number_of v",standard] | |
| 2595 | lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v",standard] | |
| 2596 | ||
| 2597 | declare take_Cons_number_of [simp] | |
| 2598 | drop_Cons_number_of [simp] | |
| 2599 | nth_Cons_number_of [simp] | |
| 15302 | 2600 | |
| 2601 | ||
| 32415 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2602 | subsubsection {* @{text upto}: interval-list on @{typ int} *}
 | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2603 | |
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2604 | (* FIXME make upto tail recursive? *) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2605 | |
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2606 | function upto :: "int \<Rightarrow> int \<Rightarrow> int list" ("(1[_../_])") where
 | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2607 | "upto i j = (if i \<le> j then i # [i+1..j] else [])" | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2608 | by auto | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2609 | termination | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2610 | by(relation "measure(%(i::int,j). nat(j - i + 1))") auto | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2611 | |
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2612 | declare upto.simps[code, simp del] | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2613 | |
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2614 | lemmas upto_rec_number_of[simp] = | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2615 | upto.simps[of "number_of m" "number_of n", standard] | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2616 | |
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2617 | lemma upto_empty[simp]: "j < i \<Longrightarrow> [i..j] = []" | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2618 | by(simp add: upto.simps) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2619 | |
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2620 | lemma set_upto[simp]: "set[i..j] = {i..j}"
 | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2621 | apply(induct i j rule:upto.induct) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2622 | apply(simp add: upto.simps simp_from_to) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2623 | done | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2624 | |
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2625 | |
| 15392 | 2626 | subsubsection {* @{text "distinct"} and @{text remdups} *}
 | 
| 13142 | 2627 | |
| 2628 | lemma distinct_append [simp]: | |
| 13145 | 2629 | "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
 | 
| 2630 | by (induct xs) auto | |
| 13142 | 2631 | |
| 15305 | 2632 | lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs" | 
| 2633 | by(induct xs) auto | |
| 2634 | ||
| 13142 | 2635 | lemma set_remdups [simp]: "set (remdups xs) = set xs" | 
| 13145 | 2636 | by (induct xs) (auto simp add: insert_absorb) | 
| 13142 | 2637 | |
| 2638 | lemma distinct_remdups [iff]: "distinct (remdups xs)" | |
| 13145 | 2639 | by (induct xs) auto | 
| 13142 | 2640 | |
| 25287 | 2641 | lemma distinct_remdups_id: "distinct xs ==> remdups xs = xs" | 
| 2642 | by (induct xs, auto) | |
| 2643 | ||
| 26734 | 2644 | lemma remdups_id_iff_distinct [simp]: "remdups xs = xs \<longleftrightarrow> distinct xs" | 
| 2645 | by (metis distinct_remdups distinct_remdups_id) | |
| 25287 | 2646 | |
| 24566 | 2647 | lemma finite_distinct_list: "finite A \<Longrightarrow> EX xs. set xs = A & distinct xs" | 
| 24632 | 2648 | by (metis distinct_remdups finite_list set_remdups) | 
| 24566 | 2649 | |
| 15072 | 2650 | lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])" | 
| 24349 | 2651 | by (induct x, auto) | 
| 15072 | 2652 | |
| 2653 | lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])" | |
| 24349 | 2654 | by (induct x, auto) | 
| 15072 | 2655 | |
| 15245 | 2656 | lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs" | 
| 2657 | by (induct xs) auto | |
| 2658 | ||
| 2659 | lemma length_remdups_eq[iff]: | |
| 2660 | "(length (remdups xs) = length xs) = (remdups xs = xs)" | |
| 2661 | apply(induct xs) | |
| 2662 | apply auto | |
| 2663 | apply(subgoal_tac "length (remdups xs) <= length xs") | |
| 2664 | apply arith | |
| 2665 | apply(rule length_remdups_leq) | |
| 2666 | done | |
| 2667 | ||
| 18490 | 2668 | |
| 2669 | lemma distinct_map: | |
| 2670 | "distinct(map f xs) = (distinct xs & inj_on f (set xs))" | |
| 2671 | by (induct xs) auto | |
| 2672 | ||
| 2673 | ||
| 13142 | 2674 | lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)" | 
| 13145 | 2675 | by (induct xs) auto | 
| 13114 | 2676 | |
| 17501 | 2677 | lemma distinct_upt[simp]: "distinct[i..<j]" | 
| 2678 | by (induct j) auto | |
| 2679 | ||
| 32415 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2680 | lemma distinct_upto[simp]: "distinct[i..j]" | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2681 | apply(induct i j rule:upto.induct) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2682 | apply(subst upto.simps) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2683 | apply(simp) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2684 | done | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 2685 | |
| 24526 | 2686 | lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)" | 
| 2687 | apply(induct xs arbitrary: i) | |
| 17501 | 2688 | apply simp | 
| 2689 | apply (case_tac i) | |
| 2690 | apply simp_all | |
| 2691 | apply(blast dest:in_set_takeD) | |
| 2692 | done | |
| 2693 | ||
| 24526 | 2694 | lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)" | 
| 2695 | apply(induct xs arbitrary: i) | |
| 17501 | 2696 | apply simp | 
| 2697 | apply (case_tac i) | |
| 2698 | apply simp_all | |
| 2699 | done | |
| 2700 | ||
| 2701 | lemma distinct_list_update: | |
| 2702 | assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
 | |
| 2703 | shows "distinct (xs[i:=a])" | |
| 2704 | proof (cases "i < length xs") | |
| 2705 | case True | |
| 2706 |   with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
 | |
| 2707 | apply (drule_tac id_take_nth_drop) by simp | |
| 2708 | with d True show ?thesis | |
| 2709 | apply (simp add: upd_conv_take_nth_drop) | |
| 2710 | apply (drule subst [OF id_take_nth_drop]) apply assumption | |
| 2711 | apply simp apply (cases "a = xs!i") apply simp by blast | |
| 2712 | next | |
| 2713 | case False with d show ?thesis by auto | |
| 2714 | qed | |
| 2715 | ||
| 31363 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2716 | lemma distinct_concat: | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2717 | assumes "distinct xs" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2718 | and "\<And> ys. ys \<in> set xs \<Longrightarrow> distinct ys" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2719 |   and "\<And> ys zs. \<lbrakk> ys \<in> set xs ; zs \<in> set xs ; ys \<noteq> zs \<rbrakk> \<Longrightarrow> set ys \<inter> set zs = {}"
 | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2720 | shows "distinct (concat xs)" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2721 | using assms by (induct xs) auto | 
| 17501 | 2722 | |
| 2723 | text {* It is best to avoid this indexed version of distinct, but
 | |
| 2724 | sometimes it is useful. *} | |
| 2725 | ||
| 13142 | 2726 | lemma distinct_conv_nth: | 
| 17501 | 2727 | "distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)" | 
| 15251 | 2728 | apply (induct xs, simp, simp) | 
| 14208 | 2729 | apply (rule iffI, clarsimp) | 
| 13145 | 2730 | apply (case_tac i) | 
| 14208 | 2731 | apply (case_tac j, simp) | 
| 13145 | 2732 | apply (simp add: set_conv_nth) | 
| 2733 | apply (case_tac j) | |
| 24648 | 2734 | apply (clarsimp simp add: set_conv_nth, simp) | 
| 13145 | 2735 | apply (rule conjI) | 
| 24648 | 2736 | (*TOO SLOW | 
| 24632 | 2737 | apply (metis Zero_neq_Suc gr0_conv_Suc in_set_conv_nth lessI less_trans_Suc nth_Cons' nth_Cons_Suc) | 
| 24648 | 2738 | *) | 
| 2739 | apply (clarsimp simp add: set_conv_nth) | |
| 2740 | apply (erule_tac x = 0 in allE, simp) | |
| 2741 | apply (erule_tac x = "Suc i" in allE, simp, clarsimp) | |
| 25130 | 2742 | (*TOO SLOW | 
| 24632 | 2743 | apply (metis Suc_Suc_eq lessI less_trans_Suc nth_Cons_Suc) | 
| 25130 | 2744 | *) | 
| 2745 | apply (erule_tac x = "Suc i" in allE, simp) | |
| 2746 | apply (erule_tac x = "Suc j" in allE, simp) | |
| 13145 | 2747 | done | 
| 13114 | 2748 | |
| 18490 | 2749 | lemma nth_eq_iff_index_eq: | 
| 2750 | "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)" | |
| 2751 | by(auto simp: distinct_conv_nth) | |
| 2752 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2753 | lemma distinct_card: "distinct xs ==> card (set xs) = size xs" | 
| 24349 | 2754 | by (induct xs) auto | 
| 14388 | 2755 | |
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2756 | lemma card_distinct: "card (set xs) = size xs ==> distinct xs" | 
| 14388 | 2757 | proof (induct xs) | 
| 2758 | case Nil thus ?case by simp | |
| 2759 | next | |
| 2760 | case (Cons x xs) | |
| 2761 | show ?case | |
| 2762 | proof (cases "x \<in> set xs") | |
| 2763 | case False with Cons show ?thesis by simp | |
| 2764 | next | |
| 2765 | case True with Cons.prems | |
| 2766 | have "card (set xs) = Suc (length xs)" | |
| 2767 | by (simp add: card_insert_if split: split_if_asm) | |
| 2768 | moreover have "card (set xs) \<le> length xs" by (rule card_length) | |
| 2769 | ultimately have False by simp | |
| 2770 | thus ?thesis .. | |
| 2771 | qed | |
| 2772 | qed | |
| 2773 | ||
| 25287 | 2774 | lemma not_distinct_decomp: "~ distinct ws ==> EX xs ys zs y. ws = xs@[y]@ys@[y]@zs" | 
| 2775 | apply (induct n == "length ws" arbitrary:ws) apply simp | |
| 2776 | apply(case_tac ws) apply simp | |
| 2777 | apply (simp split:split_if_asm) | |
| 2778 | apply (metis Cons_eq_appendI eq_Nil_appendI split_list) | |
| 2779 | done | |
| 18490 | 2780 | |
| 2781 | lemma length_remdups_concat: | |
| 2782 | "length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)" | |
| 24308 | 2783 | by(simp add: set_concat distinct_card[symmetric]) | 
| 17906 | 2784 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2785 | lemma length_remdups_card_conv: "length(remdups xs) = card(set xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2786 | proof - | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2787 | have xs: "concat[xs] = xs" by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2788 | from length_remdups_concat[of "[xs]"] show ?thesis unfolding xs by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 2789 | qed | 
| 17906 | 2790 | |
| 15392 | 2791 | subsubsection {* @{text remove1} *}
 | 
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2792 | |
| 18049 | 2793 | lemma remove1_append: | 
| 2794 | "remove1 x (xs @ ys) = | |
| 2795 | (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)" | |
| 2796 | by (induct xs) auto | |
| 2797 | ||
| 23479 | 2798 | lemma in_set_remove1[simp]: | 
| 2799 | "a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)" | |
| 2800 | apply (induct xs) | |
| 2801 | apply auto | |
| 2802 | done | |
| 2803 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2804 | lemma set_remove1_subset: "set(remove1 x xs) <= set xs" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2805 | apply(induct xs) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2806 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2807 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2808 | apply blast | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2809 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2810 | |
| 17724 | 2811 | lemma set_remove1_eq [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}"
 | 
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2812 | apply(induct xs) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2813 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2814 | apply simp | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2815 | apply blast | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2816 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2817 | |
| 23479 | 2818 | lemma length_remove1: | 
| 30128 
365ee7319b86
revert some Suc 0 lemmas back to their original forms; added some simp rules for (1::nat)
 huffman parents: 
30079diff
changeset | 2819 | "length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)" | 
| 23479 | 2820 | apply (induct xs) | 
| 2821 | apply (auto dest!:length_pos_if_in_set) | |
| 2822 | done | |
| 2823 | ||
| 18049 | 2824 | lemma remove1_filter_not[simp]: | 
| 2825 | "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs" | |
| 2826 | by(induct xs) auto | |
| 2827 | ||
| 15110 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2828 | lemma notin_set_remove1[simp]: "x ~: set xs ==> x ~: set(remove1 y xs)" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2829 | apply(insert set_remove1_subset) | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2830 | apply fast | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2831 | done | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2832 | |
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2833 | lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)" | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2834 | by (induct xs) simp_all | 
| 
78b5636eabc7
Added a number of new thms and the new function remove1
 nipkow parents: 
15072diff
changeset | 2835 | |
| 13114 | 2836 | |
| 27693 | 2837 | subsubsection {* @{text removeAll} *}
 | 
| 2838 | ||
| 2839 | lemma removeAll_append[simp]: | |
| 2840 | "removeAll x (xs @ ys) = removeAll x xs @ removeAll x ys" | |
| 2841 | by (induct xs) auto | |
| 2842 | ||
| 2843 | lemma set_removeAll[simp]: "set(removeAll x xs) = set xs - {x}"
 | |
| 2844 | by (induct xs) auto | |
| 2845 | ||
| 2846 | lemma removeAll_id[simp]: "x \<notin> set xs \<Longrightarrow> removeAll x xs = xs" | |
| 2847 | by (induct xs) auto | |
| 2848 | ||
| 2849 | (* Needs count:: 'a \<Rightarrow> a' list \<Rightarrow> nat | |
| 2850 | lemma length_removeAll: | |
| 2851 | "length(removeAll x xs) = length xs - count x xs" | |
| 2852 | *) | |
| 2853 | ||
| 2854 | lemma removeAll_filter_not[simp]: | |
| 2855 | "\<not> P x \<Longrightarrow> removeAll x (filter P xs) = filter P xs" | |
| 2856 | by(induct xs) auto | |
| 2857 | ||
| 2858 | ||
| 2859 | lemma distinct_remove1_removeAll: | |
| 2860 | "distinct xs ==> remove1 x xs = removeAll x xs" | |
| 2861 | by (induct xs) simp_all | |
| 2862 | ||
| 2863 | lemma map_removeAll_inj_on: "inj_on f (insert x (set xs)) \<Longrightarrow> | |
| 2864 | map f (removeAll x xs) = removeAll (f x) (map f xs)" | |
| 2865 | by (induct xs) (simp_all add:inj_on_def) | |
| 2866 | ||
| 2867 | lemma map_removeAll_inj: "inj f \<Longrightarrow> | |
| 2868 | map f (removeAll x xs) = removeAll (f x) (map f xs)" | |
| 2869 | by(metis map_removeAll_inj_on subset_inj_on subset_UNIV) | |
| 2870 | ||
| 2871 | ||
| 15392 | 2872 | subsubsection {* @{text replicate} *}
 | 
| 13114 | 2873 | |
| 13142 | 2874 | lemma length_replicate [simp]: "length (replicate n x) = n" | 
| 13145 | 2875 | by (induct n) auto | 
| 13124 | 2876 | |
| 13142 | 2877 | lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)" | 
| 13145 | 2878 | by (induct n) auto | 
| 13114 | 2879 | |
| 31363 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2880 | lemma map_replicate_const: | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2881 | "map (\<lambda> x. k) lst = replicate (length lst) k" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2882 | by (induct lst) auto | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2883 | |
| 13114 | 2884 | lemma replicate_app_Cons_same: | 
| 13145 | 2885 | "(replicate n x) @ (x # xs) = x # replicate n x @ xs" | 
| 2886 | by (induct n) auto | |
| 13114 | 2887 | |
| 13142 | 2888 | lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x" | 
| 14208 | 2889 | apply (induct n, simp) | 
| 13145 | 2890 | apply (simp add: replicate_app_Cons_same) | 
| 2891 | done | |
| 13114 | 2892 | |
| 13142 | 2893 | lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x" | 
| 13145 | 2894 | by (induct n) auto | 
| 13114 | 2895 | |
| 16397 | 2896 | text{* Courtesy of Matthias Daum: *}
 | 
| 2897 | lemma append_replicate_commute: | |
| 2898 | "replicate n x @ replicate k x = replicate k x @ replicate n x" | |
| 2899 | apply (simp add: replicate_add [THEN sym]) | |
| 2900 | apply (simp add: add_commute) | |
| 2901 | done | |
| 2902 | ||
| 31080 | 2903 | text{* Courtesy of Andreas Lochbihler: *}
 | 
| 2904 | lemma filter_replicate: | |
| 2905 | "filter P (replicate n x) = (if P x then replicate n x else [])" | |
| 2906 | by(induct n) auto | |
| 2907 | ||
| 13142 | 2908 | lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x" | 
| 13145 | 2909 | by (induct n) auto | 
| 13114 | 2910 | |
| 13142 | 2911 | lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x" | 
| 13145 | 2912 | by (induct n) auto | 
| 13114 | 2913 | |
| 13142 | 2914 | lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x" | 
| 13145 | 2915 | by (atomize (full), induct n) auto | 
| 13114 | 2916 | |
| 24526 | 2917 | lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x" | 
| 2918 | apply (induct n arbitrary: i, simp) | |
| 13145 | 2919 | apply (simp add: nth_Cons split: nat.split) | 
| 2920 | done | |
| 13114 | 2921 | |
| 16397 | 2922 | text{* Courtesy of Matthias Daum (2 lemmas): *}
 | 
| 2923 | lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x" | |
| 2924 | apply (case_tac "k \<le> i") | |
| 2925 | apply (simp add: min_def) | |
| 2926 | apply (drule not_leE) | |
| 2927 | apply (simp add: min_def) | |
| 2928 | apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x") | |
| 2929 | apply simp | |
| 2930 | apply (simp add: replicate_add [symmetric]) | |
| 2931 | done | |
| 2932 | ||
| 24526 | 2933 | lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x" | 
| 2934 | apply (induct k arbitrary: i) | |
| 16397 | 2935 | apply simp | 
| 2936 | apply clarsimp | |
| 2937 | apply (case_tac i) | |
| 2938 | apply simp | |
| 2939 | apply clarsimp | |
| 2940 | done | |
| 2941 | ||
| 2942 | ||
| 13142 | 2943 | lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
 | 
| 13145 | 2944 | by (induct n) auto | 
| 13114 | 2945 | |
| 13142 | 2946 | lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
 | 
| 13145 | 2947 | by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc) | 
| 13114 | 2948 | |
| 13142 | 2949 | lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
 | 
| 13145 | 2950 | by auto | 
| 13114 | 2951 | |
| 13142 | 2952 | lemma in_set_replicateD: "x : set (replicate n y) ==> x = y" | 
| 13145 | 2953 | by (simp add: set_replicate_conv_if split: split_if_asm) | 
| 13114 | 2954 | |
| 24796 | 2955 | lemma replicate_append_same: | 
| 2956 | "replicate i x @ [x] = x # replicate i x" | |
| 2957 | by (induct i) simp_all | |
| 2958 | ||
| 2959 | lemma map_replicate_trivial: | |
| 2960 | "map (\<lambda>i. x) [0..<i] = replicate i x" | |
| 2961 | by (induct i) (simp_all add: replicate_append_same) | |
| 2962 | ||
| 31363 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2963 | lemma concat_replicate_trivial[simp]: | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2964 | "concat (replicate i []) = []" | 
| 
7493b571b37d
Added theorems about distinct & concat, map & replicate and concat & replicate
 hoelzl parents: 
31264diff
changeset | 2965 | by (induct i) (auto simp add: map_replicate_const) | 
| 13114 | 2966 | |
| 28642 | 2967 | lemma replicate_empty[simp]: "(replicate n x = []) \<longleftrightarrow> n=0" | 
| 2968 | by (induct n) auto | |
| 2969 | ||
| 2970 | lemma empty_replicate[simp]: "([] = replicate n x) \<longleftrightarrow> n=0" | |
| 2971 | by (induct n) auto | |
| 2972 | ||
| 2973 | lemma replicate_eq_replicate[simp]: | |
| 2974 | "(replicate m x = replicate n y) \<longleftrightarrow> (m=n & (m\<noteq>0 \<longrightarrow> x=y))" | |
| 2975 | apply(induct m arbitrary: n) | |
| 2976 | apply simp | |
| 2977 | apply(induct_tac n) | |
| 2978 | apply auto | |
| 2979 | done | |
| 2980 | ||
| 2981 | ||
| 15392 | 2982 | subsubsection{*@{text rotate1} and @{text rotate}*}
 | 
| 15302 | 2983 | |
| 2984 | lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]" | |
| 2985 | by(simp add:rotate1_def) | |
| 2986 | ||
| 2987 | lemma rotate0[simp]: "rotate 0 = id" | |
| 2988 | by(simp add:rotate_def) | |
| 2989 | ||
| 2990 | lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)" | |
| 2991 | by(simp add:rotate_def) | |
| 2992 | ||
| 2993 | lemma rotate_add: | |
| 2994 | "rotate (m+n) = rotate m o rotate n" | |
| 2995 | by(simp add:rotate_def funpow_add) | |
| 2996 | ||
| 2997 | lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs" | |
| 2998 | by(simp add:rotate_add) | |
| 2999 | ||
| 18049 | 3000 | lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)" | 
| 3001 | by(simp add:rotate_def funpow_swap1) | |
| 3002 | ||
| 15302 | 3003 | lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs" | 
| 3004 | by(cases xs) simp_all | |
| 3005 | ||
| 3006 | lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs" | |
| 3007 | apply(induct n) | |
| 3008 | apply simp | |
| 3009 | apply (simp add:rotate_def) | |
| 13145 | 3010 | done | 
| 13114 | 3011 | |
| 15302 | 3012 | lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]" | 
| 3013 | by(simp add:rotate1_def split:list.split) | |
| 3014 | ||
| 3015 | lemma rotate_drop_take: | |
| 3016 | "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs" | |
| 3017 | apply(induct n) | |
| 3018 | apply simp | |
| 3019 | apply(simp add:rotate_def) | |
| 3020 | apply(cases "xs = []") | |
| 3021 | apply (simp) | |
| 3022 | apply(case_tac "n mod length xs = 0") | |
| 3023 | apply(simp add:mod_Suc) | |
| 3024 | apply(simp add: rotate1_hd_tl drop_Suc take_Suc) | |
| 3025 | apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric] | |
| 3026 | take_hd_drop linorder_not_le) | |
| 13145 | 3027 | done | 
| 13114 | 3028 | |
| 15302 | 3029 | lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs" | 
| 3030 | by(simp add:rotate_drop_take) | |
| 3031 | ||
| 3032 | lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs" | |
| 3033 | by(simp add:rotate_drop_take) | |
| 3034 | ||
| 3035 | lemma length_rotate1[simp]: "length(rotate1 xs) = length xs" | |
| 3036 | by(simp add:rotate1_def split:list.split) | |
| 3037 | ||
| 24526 | 3038 | lemma length_rotate[simp]: "length(rotate n xs) = length xs" | 
| 3039 | by (induct n arbitrary: xs) (simp_all add:rotate_def) | |
| 15302 | 3040 | |
| 3041 | lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs" | |
| 3042 | by(simp add:rotate1_def split:list.split) blast | |
| 3043 | ||
| 3044 | lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs" | |
| 3045 | by (induct n) (simp_all add:rotate_def) | |
| 3046 | ||
| 3047 | lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)" | |
| 3048 | by(simp add:rotate_drop_take take_map drop_map) | |
| 3049 | ||
| 3050 | lemma set_rotate1[simp]: "set(rotate1 xs) = set xs" | |
| 3051 | by(simp add:rotate1_def split:list.split) | |
| 3052 | ||
| 3053 | lemma set_rotate[simp]: "set(rotate n xs) = set xs" | |
| 3054 | by (induct n) (simp_all add:rotate_def) | |
| 3055 | ||
| 3056 | lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])" | |
| 3057 | by(simp add:rotate1_def split:list.split) | |
| 3058 | ||
| 3059 | lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])" | |
| 3060 | by (induct n) (simp_all add:rotate_def) | |
| 13114 | 3061 | |
| 15439 | 3062 | lemma rotate_rev: | 
| 3063 | "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)" | |
| 3064 | apply(simp add:rotate_drop_take rev_drop rev_take) | |
| 3065 | apply(cases "length xs = 0") | |
| 3066 | apply simp | |
| 3067 | apply(cases "n mod length xs = 0") | |
| 3068 | apply simp | |
| 3069 | apply(simp add:rotate_drop_take rev_drop rev_take) | |
| 3070 | done | |
| 3071 | ||
| 18423 | 3072 | lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)" | 
| 3073 | apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth) | |
| 3074 | apply(subgoal_tac "length xs \<noteq> 0") | |
| 3075 | prefer 2 apply simp | |
| 3076 | using mod_less_divisor[of "length xs" n] by arith | |
| 3077 | ||
| 13114 | 3078 | |
| 15392 | 3079 | subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
 | 
| 13114 | 3080 | |
| 13142 | 3081 | lemma sublist_empty [simp]: "sublist xs {} = []"
 | 
| 13145 | 3082 | by (auto simp add: sublist_def) | 
| 13114 | 3083 | |
| 13142 | 3084 | lemma sublist_nil [simp]: "sublist [] A = []" | 
| 13145 | 3085 | by (auto simp add: sublist_def) | 
| 13114 | 3086 | |
| 15281 | 3087 | lemma length_sublist: | 
| 3088 |   "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
 | |
| 3089 | by(simp add: sublist_def length_filter_conv_card cong:conj_cong) | |
| 3090 | ||
| 3091 | lemma sublist_shift_lemma_Suc: | |
| 24526 | 3092 | "map fst (filter (%p. P(Suc(snd p))) (zip xs is)) = | 
| 3093 | map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))" | |
| 3094 | apply(induct xs arbitrary: "is") | |
| 15281 | 3095 | apply simp | 
| 3096 | apply (case_tac "is") | |
| 3097 | apply simp | |
| 3098 | apply simp | |
| 3099 | done | |
| 3100 | ||
| 13114 | 3101 | lemma sublist_shift_lemma: | 
| 23279 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 3102 | "map fst [p<-zip xs [i..<i + length xs] . snd p : A] = | 
| 
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
 nipkow parents: 
23246diff
changeset | 3103 | map fst [p<-zip xs [0..<length xs] . snd p + i : A]" | 
| 13145 | 3104 | by (induct xs rule: rev_induct) (simp_all add: add_commute) | 
| 13114 | 3105 | |
| 3106 | lemma sublist_append: | |
| 15168 | 3107 |      "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
 | 
| 13145 | 3108 | apply (unfold sublist_def) | 
| 14208 | 3109 | apply (induct l' rule: rev_induct, simp) | 
| 13145 | 3110 | apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma) | 
| 3111 | apply (simp add: add_commute) | |
| 3112 | done | |
| 13114 | 3113 | |
| 3114 | lemma sublist_Cons: | |
| 13145 | 3115 | "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
 | 
| 3116 | apply (induct l rule: rev_induct) | |
| 3117 | apply (simp add: sublist_def) | |
| 3118 | apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append) | |
| 3119 | done | |
| 13114 | 3120 | |
| 24526 | 3121 | lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
 | 
| 3122 | apply(induct xs arbitrary: I) | |
| 25162 | 3123 | apply(auto simp: sublist_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc) | 
| 15281 | 3124 | done | 
| 3125 | ||
| 3126 | lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs" | |
| 3127 | by(auto simp add:set_sublist) | |
| 3128 | ||
| 3129 | lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)" | |
| 3130 | by(auto simp add:set_sublist) | |
| 3131 | ||
| 3132 | lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs" | |
| 3133 | by(auto simp add:set_sublist) | |
| 3134 | ||
| 13142 | 3135 | lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])" | 
| 13145 | 3136 | by (simp add: sublist_Cons) | 
| 13114 | 3137 | |
| 15281 | 3138 | |
| 24526 | 3139 | lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)" | 
| 3140 | apply(induct xs arbitrary: I) | |
| 15281 | 3141 | apply simp | 
| 3142 | apply(auto simp add:sublist_Cons) | |
| 3143 | done | |
| 3144 | ||
| 3145 | ||
| 15045 | 3146 | lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
 | 
| 14208 | 3147 | apply (induct l rule: rev_induct, simp) | 
| 13145 | 3148 | apply (simp split: nat_diff_split add: sublist_append) | 
| 3149 | done | |
| 13114 | 3150 | |
| 24526 | 3151 | lemma filter_in_sublist: | 
| 3152 | "distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s" | |
| 3153 | proof (induct xs arbitrary: s) | |
| 17501 | 3154 | case Nil thus ?case by simp | 
| 3155 | next | |
| 3156 | case (Cons a xs) | |
| 3157 | moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto | |
| 3158 | ultimately show ?case by(simp add: sublist_Cons cong:filter_cong) | |
| 3159 | qed | |
| 3160 | ||
| 13114 | 3161 | |
| 19390 | 3162 | subsubsection {* @{const splice} *}
 | 
| 3163 | ||
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 3164 | lemma splice_Nil2 [simp, code]: | 
| 19390 | 3165 | "splice xs [] = xs" | 
| 3166 | by (cases xs) simp_all | |
| 3167 | ||
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 3168 | lemma splice_Cons_Cons [simp, code]: | 
| 19390 | 3169 | "splice (x#xs) (y#ys) = x # y # splice xs ys" | 
| 3170 | by simp | |
| 3171 | ||
| 19607 
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
 haftmann parents: 
19585diff
changeset | 3172 | declare splice.simps(2) [simp del, code del] | 
| 19390 | 3173 | |
| 24526 | 3174 | lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys" | 
| 3175 | apply(induct xs arbitrary: ys) apply simp | |
| 22793 | 3176 | apply(case_tac ys) | 
| 3177 | apply auto | |
| 3178 | done | |
| 3179 | ||
| 24616 | 3180 | |
| 31557 | 3181 | subsubsection {* (In)finiteness *}
 | 
| 28642 | 3182 | |
| 3183 | lemma finite_maxlen: | |
| 3184 | "finite (M::'a list set) ==> EX n. ALL s:M. size s < n" | |
| 3185 | proof (induct rule: finite.induct) | |
| 3186 | case emptyI show ?case by simp | |
| 3187 | next | |
| 3188 | case (insertI M xs) | |
| 3189 | then obtain n where "\<forall>s\<in>M. length s < n" by blast | |
| 3190 | hence "ALL s:insert xs M. size s < max n (size xs) + 1" by auto | |
| 3191 | thus ?case .. | |
| 3192 | qed | |
| 3193 | ||
| 31557 | 3194 | lemma finite_lists_length_eq: | 
| 3195 | assumes "finite A" | |
| 3196 | shows "finite {xs. set xs \<subseteq> A \<and> length xs = n}" (is "finite (?S n)")
 | |
| 3197 | proof(induct n) | |
| 3198 | case 0 show ?case by simp | |
| 3199 | next | |
| 3200 | case (Suc n) | |
| 3201 | have "?S (Suc n) = (\<Union>x\<in>A. (\<lambda>xs. x#xs) ` ?S n)" | |
| 3202 | by (auto simp:length_Suc_conv) | |
| 3203 | then show ?case using `finite A` | |
| 3204 | by (auto intro: finite_imageI Suc) (* FIXME metis? *) | |
| 3205 | qed | |
| 3206 | ||
| 3207 | lemma finite_lists_length_le: | |
| 3208 |   assumes "finite A" shows "finite {xs. set xs \<subseteq> A \<and> length xs \<le> n}"
 | |
| 3209 | (is "finite ?S") | |
| 3210 | proof- | |
| 3211 |   have "?S = (\<Union>n\<in>{0..n}. {xs. set xs \<subseteq> A \<and> length xs = n})" by auto
 | |
| 3212 | thus ?thesis by (auto intro: finite_lists_length_eq[OF `finite A`]) | |
| 3213 | qed | |
| 3214 | ||
| 28642 | 3215 | lemma infinite_UNIV_listI: "~ finite(UNIV::'a list set)" | 
| 3216 | apply(rule notI) | |
| 3217 | apply(drule finite_maxlen) | |
| 3218 | apply (metis UNIV_I length_replicate less_not_refl) | |
| 3219 | done | |
| 3220 | ||
| 3221 | ||
| 24616 | 3222 | subsection {*Sorting*}
 | 
| 3223 | ||
| 24617 | 3224 | text{* Currently it is not shown that @{const sort} returns a
 | 
| 3225 | permutation of its input because the nicest proof is via multisets, | |
| 3226 | which are not yet available. Alternatively one could define a function | |
| 3227 | that counts the number of occurrences of an element in a list and use | |
| 3228 | that instead of multisets to state the correctness property. *} | |
| 3229 | ||
| 24616 | 3230 | context linorder | 
| 3231 | begin | |
| 3232 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3233 | lemma length_insert[simp] : "length (insort_key f x xs) = Suc (length xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3234 | by (induct xs, auto) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3235 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3236 | lemma length_sort[simp]: "length (sort_key f xs) = length xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3237 | by (induct xs, auto) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3238 | |
| 25062 | 3239 | lemma sorted_Cons: "sorted (x#xs) = (sorted xs & (ALL y:set xs. x <= y))" | 
| 24616 | 3240 | apply(induct xs arbitrary: x) apply simp | 
| 3241 | by simp (blast intro: order_trans) | |
| 3242 | ||
| 3243 | lemma sorted_append: | |
| 25062 | 3244 | "sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))" | 
| 24616 | 3245 | by (induct xs) (auto simp add:sorted_Cons) | 
| 3246 | ||
| 31201 | 3247 | lemma sorted_nth_mono: | 
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3248 | "sorted xs \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!i \<le> xs!j" | 
| 31201 | 3249 | by (induct xs arbitrary: i j) (auto simp:nth_Cons' sorted_Cons) | 
| 3250 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3251 | lemma sorted_rev_nth_mono: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3252 | "sorted (rev xs) \<Longrightarrow> i \<le> j \<Longrightarrow> j < length xs \<Longrightarrow> xs!j \<le> xs!i" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3253 | using sorted_nth_mono[ of "rev xs" "length xs - j - 1" "length xs - i - 1"] | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3254 | rev_nth[of "length xs - i - 1" "xs"] rev_nth[of "length xs - j - 1" "xs"] | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3255 | by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3256 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3257 | lemma sorted_nth_monoI: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3258 | "(\<And> i j. \<lbrakk> i \<le> j ; j < length xs \<rbrakk> \<Longrightarrow> xs ! i \<le> xs ! j) \<Longrightarrow> sorted xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3259 | proof (induct xs) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3260 | case (Cons x xs) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3261 | have "sorted xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3262 | proof (rule Cons.hyps) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3263 | fix i j assume "i \<le> j" and "j < length xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3264 | with Cons.prems[of "Suc i" "Suc j"] | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3265 | show "xs ! i \<le> xs ! j" by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3266 | qed | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3267 | moreover | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3268 |   {
 | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3269 | fix y assume "y \<in> set xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3270 | then obtain j where "j < length xs" and "xs ! j = y" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3271 | unfolding in_set_conv_nth by blast | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3272 | with Cons.prems[of 0 "Suc j"] | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3273 | have "x \<le> y" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3274 | by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3275 | } | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3276 | ultimately | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3277 | show ?case | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3278 | unfolding sorted_Cons by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3279 | qed simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3280 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3281 | lemma sorted_equals_nth_mono: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3282 | "sorted xs = (\<forall>j < length xs. \<forall>i \<le> j. xs ! i \<le> xs ! j)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3283 | by (auto intro: sorted_nth_monoI sorted_nth_mono) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3284 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3285 | lemma set_insort: "set(insort_key f x xs) = insert x (set xs)" | 
| 24616 | 3286 | by (induct xs) auto | 
| 3287 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3288 | lemma set_sort[simp]: "set(sort_key f xs) = set xs" | 
| 24616 | 3289 | by (induct xs) (simp_all add:set_insort) | 
| 3290 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3291 | lemma distinct_insort: "distinct (insort_key f x xs) = (x \<notin> set xs \<and> distinct xs)" | 
| 24616 | 3292 | by(induct xs)(auto simp:set_insort) | 
| 3293 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3294 | lemma distinct_sort[simp]: "distinct (sort_key f xs) = distinct xs" | 
| 24616 | 3295 | by(induct xs)(simp_all add:distinct_insort set_sort) | 
| 3296 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3297 | lemma sorted_insort_key: "sorted (map f (insort_key f x xs)) = sorted (map f xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3298 | by(induct xs)(auto simp:sorted_Cons set_insort) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3299 | |
| 24616 | 3300 | lemma sorted_insort: "sorted (insort x xs) = sorted xs" | 
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3301 | using sorted_insort_key[where f="\<lambda>x. x"] by simp | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3302 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3303 | theorem sorted_sort_key[simp]: "sorted (map f (sort_key f xs))" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3304 | by(induct xs)(auto simp:sorted_insort_key) | 
| 24616 | 3305 | |
| 3306 | theorem sorted_sort[simp]: "sorted (sort xs)" | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3307 | by(induct xs)(auto simp:sorted_insort) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3308 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3309 | lemma insort_is_Cons: "\<forall>x\<in>set xs. f a \<le> f x \<Longrightarrow> insort_key f a xs = a # xs" | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3310 | by (cases xs) auto | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3311 | |
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3312 | lemma sorted_remove1: "sorted xs \<Longrightarrow> sorted (remove1 a xs)" | 
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3313 | by(induct xs)(auto simp add: sorted_Cons) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3314 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3315 | lemma insort_key_remove1: "\<lbrakk> a \<in> set xs; sorted (map f xs) ; inj_on f (set xs) \<rbrakk> | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3316 | \<Longrightarrow> insort_key f a (remove1 a xs) = xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3317 | proof (induct xs) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3318 | case (Cons x xs) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3319 | thus ?case | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3320 | proof (cases "x = a") | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3321 | case False | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3322 | hence "f x \<noteq> f a" using Cons.prems by auto | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3323 | hence "f x < f a" using Cons.prems by (auto simp: sorted_Cons) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3324 | thus ?thesis using Cons by (auto simp: sorted_Cons insort_is_Cons) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3325 | qed (auto simp: sorted_Cons insort_is_Cons) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3326 | qed simp | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3327 | |
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3328 | lemma insort_remove1: "\<lbrakk> a \<in> set xs; sorted xs \<rbrakk> \<Longrightarrow> insort a (remove1 a xs) = xs" | 
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3329 | using insort_key_remove1[where f="\<lambda>x. x"] by simp | 
| 26143 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3330 | |
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3331 | lemma sorted_remdups[simp]: | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3332 | "sorted l \<Longrightarrow> sorted (remdups l)" | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3333 | by (induct l) (auto simp: sorted_Cons) | 
| 
314c0bcb7df7
Added useful general lemmas from the work with the HeapMonad
 bulwahn parents: 
26073diff
changeset | 3334 | |
| 24645 | 3335 | lemma sorted_distinct_set_unique: | 
| 3336 | assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys" | |
| 3337 | shows "xs = ys" | |
| 3338 | proof - | |
| 26734 | 3339 | from assms have 1: "length xs = length ys" by (auto dest!: distinct_card) | 
| 24645 | 3340 | from assms show ?thesis | 
| 3341 | proof(induct rule:list_induct2[OF 1]) | |
| 3342 | case 1 show ?case by simp | |
| 3343 | next | |
| 3344 | case 2 thus ?case by (simp add:sorted_Cons) | |
| 3345 | (metis Diff_insert_absorb antisym insertE insert_iff) | |
| 3346 | qed | |
| 3347 | qed | |
| 3348 | ||
| 3349 | lemma finite_sorted_distinct_unique: | |
| 3350 | shows "finite A \<Longrightarrow> EX! xs. set xs = A & sorted xs & distinct xs" | |
| 3351 | apply(drule finite_distinct_list) | |
| 3352 | apply clarify | |
| 3353 | apply(rule_tac a="sort xs" in ex1I) | |
| 3354 | apply (auto simp: sorted_distinct_set_unique) | |
| 3355 | done | |
| 3356 | ||
| 29626 | 3357 | lemma sorted_take: | 
| 3358 | "sorted xs \<Longrightarrow> sorted (take n xs)" | |
| 3359 | proof (induct xs arbitrary: n rule: sorted.induct) | |
| 3360 | case 1 show ?case by simp | |
| 3361 | next | |
| 3362 | case 2 show ?case by (cases n) simp_all | |
| 3363 | next | |
| 3364 | case (3 x y xs) | |
| 3365 | then have "x \<le> y" by simp | |
| 3366 | show ?case proof (cases n) | |
| 3367 | case 0 then show ?thesis by simp | |
| 3368 | next | |
| 3369 | case (Suc m) | |
| 3370 | with 3 have "sorted (take m (y # xs))" by simp | |
| 3371 | with Suc `x \<le> y` show ?thesis by (cases m) simp_all | |
| 3372 | qed | |
| 3373 | qed | |
| 3374 | ||
| 3375 | lemma sorted_drop: | |
| 3376 | "sorted xs \<Longrightarrow> sorted (drop n xs)" | |
| 3377 | proof (induct xs arbitrary: n rule: sorted.induct) | |
| 3378 | case 1 show ?case by simp | |
| 3379 | next | |
| 3380 | case 2 show ?case by (cases n) simp_all | |
| 3381 | next | |
| 3382 | case 3 then show ?case by (cases n) simp_all | |
| 3383 | qed | |
| 3384 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3385 | lemma sorted_dropWhile: "sorted xs \<Longrightarrow> sorted (dropWhile P xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3386 | unfolding dropWhile_eq_drop by (rule sorted_drop) | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3387 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3388 | lemma sorted_takeWhile: "sorted xs \<Longrightarrow> sorted (takeWhile P xs)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3389 | apply (subst takeWhile_eq_take) by (rule sorted_take) | 
| 29626 | 3390 | |
| 24616 | 3391 | end | 
| 3392 | ||
| 25277 | 3393 | lemma sorted_upt[simp]: "sorted[i..<j]" | 
| 3394 | by (induct j) (simp_all add:sorted_append) | |
| 3395 | ||
| 32415 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3396 | lemma sorted_upto[simp]: "sorted[i..j]" | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3397 | apply(induct i j rule:upto.induct) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3398 | apply(subst upto.simps) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3399 | apply(simp add:sorted_Cons) | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3400 | done | 
| 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 3401 | |
| 24616 | 3402 | |
| 25069 | 3403 | subsubsection {* @{text sorted_list_of_set} *}
 | 
| 3404 | ||
| 3405 | text{* This function maps (finite) linearly ordered sets to sorted
 | |
| 3406 | lists. Warning: in most cases it is not a good idea to convert from | |
| 3407 | sets to lists but one should convert in the other direction (via | |
| 3408 | @{const set}). *}
 | |
| 3409 | ||
| 3410 | ||
| 3411 | context linorder | |
| 3412 | begin | |
| 3413 | ||
| 3414 | definition | |
| 3415 | sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where | |
| 28562 | 3416 | [code del]: "sorted_list_of_set A == THE xs. set xs = A & sorted xs & distinct xs" | 
| 25069 | 3417 | |
| 3418 | lemma sorted_list_of_set[simp]: "finite A \<Longrightarrow> | |
| 3419 | set(sorted_list_of_set A) = A & | |
| 3420 | sorted(sorted_list_of_set A) & distinct(sorted_list_of_set A)" | |
| 3421 | apply(simp add:sorted_list_of_set_def) | |
| 3422 | apply(rule the1I2) | |
| 3423 | apply(simp_all add: finite_sorted_distinct_unique) | |
| 3424 | done | |
| 3425 | ||
| 3426 | lemma sorted_list_of_empty[simp]: "sorted_list_of_set {} = []"
 | |
| 3427 | unfolding sorted_list_of_set_def | |
| 3428 | apply(subst the_equality[of _ "[]"]) | |
| 3429 | apply simp_all | |
| 3430 | done | |
| 3431 | ||
| 3432 | end | |
| 3433 | ||
| 3434 | ||
| 15392 | 3435 | subsubsection {* @{text lists}: the list-forming operator over sets *}
 | 
| 15302 | 3436 | |
| 23740 | 3437 | inductive_set | 
| 22262 | 3438 | lists :: "'a set => 'a list set" | 
| 23740 | 3439 | for A :: "'a set" | 
| 3440 | where | |
| 3441 | Nil [intro!]: "[]: lists A" | |
| 27715 | 3442 | | Cons [intro!,noatp]: "[| a: A; l: lists A|] ==> a#l : lists A" | 
| 24286 
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
 paulson parents: 
24219diff
changeset | 3443 | |
| 
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
 paulson parents: 
24219diff
changeset | 3444 | inductive_cases listsE [elim!,noatp]: "x#l : lists A" | 
| 
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
 paulson parents: 
24219diff
changeset | 3445 | inductive_cases listspE [elim!,noatp]: "listsp A (x # l)" | 
| 23740 | 3446 | |
| 3447 | lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B" | |
| 26795 
a27607030a1c
- Explicitely applied predicate1I in a few proofs, because it is no longer
 berghofe parents: 
26771diff
changeset | 3448 | by (rule predicate1I, erule listsp.induct, blast+) | 
| 
a27607030a1c
- Explicitely applied predicate1I in a few proofs, because it is no longer
 berghofe parents: 
26771diff
changeset | 3449 | |
| 
a27607030a1c
- Explicitely applied predicate1I in a few proofs, because it is no longer
 berghofe parents: 
26771diff
changeset | 3450 | lemmas lists_mono = listsp_mono [to_set pred_subset_eq] | 
| 22262 | 3451 | |
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 3452 | lemma listsp_infI: | 
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 3453 | assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l | 
| 24349 | 3454 | by induct blast+ | 
| 15302 | 3455 | |
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 3456 | lemmas lists_IntI = listsp_infI [to_set] | 
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 3457 | |
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 3458 | lemma listsp_inf_eq [simp]: "listsp (inf A B) = inf (listsp A) (listsp B)" | 
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 3459 | proof (rule mono_inf [where f=listsp, THEN order_antisym]) | 
| 22262 | 3460 | show "mono listsp" by (simp add: mono_def listsp_mono) | 
| 26795 
a27607030a1c
- Explicitely applied predicate1I in a few proofs, because it is no longer
 berghofe parents: 
26771diff
changeset | 3461 | show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro!: listsp_infI predicate1I) | 
| 14388 | 3462 | qed | 
| 3463 | ||
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 3464 | lemmas listsp_conj_eq [simp] = listsp_inf_eq [simplified inf_fun_eq inf_bool_eq] | 
| 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 3465 | |
| 26795 
a27607030a1c
- Explicitely applied predicate1I in a few proofs, because it is no longer
 berghofe parents: 
26771diff
changeset | 3466 | lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set pred_equals_eq] | 
| 22262 | 3467 | |
| 3468 | lemma append_in_listsp_conv [iff]: | |
| 3469 | "(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)" | |
| 15302 | 3470 | by (induct xs) auto | 
| 3471 | ||
| 22262 | 3472 | lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set] | 
| 3473 | ||
| 3474 | lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)" | |
| 3475 | -- {* eliminate @{text listsp} in favour of @{text set} *}
 | |
| 15302 | 3476 | by (induct xs) auto | 
| 3477 | ||
| 22262 | 3478 | lemmas in_lists_conv_set = in_listsp_conv_set [to_set] | 
| 3479 | ||
| 24286 
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
 paulson parents: 
24219diff
changeset | 3480 | lemma in_listspD [dest!,noatp]: "listsp A xs ==> \<forall>x\<in>set xs. A x" | 
| 22262 | 3481 | by (rule in_listsp_conv_set [THEN iffD1]) | 
| 3482 | ||
| 24286 
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
 paulson parents: 
24219diff
changeset | 3483 | lemmas in_listsD [dest!,noatp] = in_listspD [to_set] | 
| 
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
 paulson parents: 
24219diff
changeset | 3484 | |
| 
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
 paulson parents: 
24219diff
changeset | 3485 | lemma in_listspI [intro!,noatp]: "\<forall>x\<in>set xs. A x ==> listsp A xs" | 
| 22262 | 3486 | by (rule in_listsp_conv_set [THEN iffD2]) | 
| 3487 | ||
| 24286 
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
 paulson parents: 
24219diff
changeset | 3488 | lemmas in_listsI [intro!,noatp] = in_listspI [to_set] | 
| 15302 | 3489 | |
| 3490 | lemma lists_UNIV [simp]: "lists UNIV = UNIV" | |
| 3491 | by auto | |
| 3492 | ||
| 17086 | 3493 | |
| 3494 | ||
| 3495 | subsubsection{* Inductive definition for membership *}
 | |
| 3496 | ||
| 23740 | 3497 | inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 22262 | 3498 | where | 
| 3499 | elem: "ListMem x (x # xs)" | |
| 3500 | | insert: "ListMem x xs \<Longrightarrow> ListMem x (y # xs)" | |
| 3501 | ||
| 3502 | lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)" | |
| 17086 | 3503 | apply (rule iffI) | 
| 3504 | apply (induct set: ListMem) | |
| 3505 | apply auto | |
| 3506 | apply (induct xs) | |
| 3507 | apply (auto intro: ListMem.intros) | |
| 3508 | done | |
| 3509 | ||
| 3510 | ||
| 3511 | ||
| 15392 | 3512 | subsubsection{*Lists as Cartesian products*}
 | 
| 15302 | 3513 | |
| 3514 | text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from
 | |
| 3515 | @{term A} and tail drawn from @{term Xs}.*}
 | |
| 3516 | ||
| 3517 | constdefs | |
| 3518 | set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" | |
| 3519 |   "set_Cons A XS == {z. \<exists>x xs. z = x#xs & x \<in> A & xs \<in> XS}"
 | |
| 28562 | 3520 | declare set_Cons_def [code del] | 
| 15302 | 3521 | |
| 17724 | 3522 | lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A"
 | 
| 15302 | 3523 | by (auto simp add: set_Cons_def) | 
| 3524 | ||
| 3525 | text{*Yields the set of lists, all of the same length as the argument and
 | |
| 3526 | with elements drawn from the corresponding element of the argument.*} | |
| 3527 | ||
| 3528 | consts listset :: "'a set list \<Rightarrow> 'a list set" | |
| 3529 | primrec | |
| 3530 |    "listset []    = {[]}"
 | |
| 3531 | "listset(A#As) = set_Cons A (listset As)" | |
| 3532 | ||
| 3533 | ||
| 15656 | 3534 | subsection{*Relations on Lists*}
 | 
| 3535 | ||
| 3536 | subsubsection {* Length Lexicographic Ordering *}
 | |
| 3537 | ||
| 3538 | text{*These orderings preserve well-foundedness: shorter lists 
 | |
| 3539 | precede longer lists. These ordering are not used in dictionaries.*} | |
| 3540 | ||
| 3541 | consts lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
 | |
| 3542 |         --{*The lexicographic ordering for lists of the specified length*}
 | |
| 15302 | 3543 | primrec | 
| 15656 | 3544 |   "lexn r 0 = {}"
 | 
| 3545 | "lexn r (Suc n) = | |
| 3546 | (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int | |
| 3547 |     {(xs,ys). length xs = Suc n \<and> length ys = Suc n}"
 | |
| 15302 | 3548 | |
| 3549 | constdefs | |
| 15656 | 3550 |   lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
 | 
| 3551 | "lex r == \<Union>n. lexn r n" | |
| 3552 |         --{*Holds only between lists of the same length*}
 | |
| 3553 | ||
| 15693 | 3554 |   lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
 | 
| 3555 | "lenlex r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))" | |
| 15656 | 3556 |         --{*Compares lists by their length and then lexicographically*}
 | 
| 15302 | 3557 | |
| 28562 | 3558 | declare lex_def [code del] | 
| 27106 | 3559 | |
| 15302 | 3560 | |
| 3561 | lemma wf_lexn: "wf r ==> wf (lexn r n)" | |
| 3562 | apply (induct n, simp, simp) | |
| 3563 | apply(rule wf_subset) | |
| 3564 | prefer 2 apply (rule Int_lower1) | |
| 3565 | apply(rule wf_prod_fun_image) | |
| 3566 | prefer 2 apply (rule inj_onI, auto) | |
| 3567 | done | |
| 3568 | ||
| 3569 | lemma lexn_length: | |
| 24526 | 3570 | "(xs, ys) : lexn r n ==> length xs = n \<and> length ys = n" | 
| 3571 | by (induct n arbitrary: xs ys) auto | |
| 15302 | 3572 | |
| 3573 | lemma wf_lex [intro!]: "wf r ==> wf (lex r)" | |
| 3574 | apply (unfold lex_def) | |
| 3575 | apply (rule wf_UN) | |
| 3576 | apply (blast intro: wf_lexn, clarify) | |
| 3577 | apply (rename_tac m n) | |
| 3578 | apply (subgoal_tac "m \<noteq> n") | |
| 3579 | prefer 2 apply blast | |
| 3580 | apply (blast dest: lexn_length not_sym) | |
| 3581 | done | |
| 3582 | ||
| 3583 | lemma lexn_conv: | |
| 15656 | 3584 | "lexn r n = | 
| 3585 |     {(xs,ys). length xs = n \<and> length ys = n \<and>
 | |
| 3586 | (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}" | |
| 18423 | 3587 | apply (induct n, simp) | 
| 15302 | 3588 | apply (simp add: image_Collect lex_prod_def, safe, blast) | 
| 3589 | apply (rule_tac x = "ab # xys" in exI, simp) | |
| 3590 | apply (case_tac xys, simp_all, blast) | |
| 3591 | done | |
| 3592 | ||
| 3593 | lemma lex_conv: | |
| 15656 | 3594 | "lex r = | 
| 3595 |     {(xs,ys). length xs = length ys \<and>
 | |
| 3596 | (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}" | |
| 15302 | 3597 | by (force simp add: lex_def lexn_conv) | 
| 3598 | ||
| 15693 | 3599 | lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)" | 
| 3600 | by (unfold lenlex_def) blast | |
| 3601 | ||
| 3602 | lemma lenlex_conv: | |
| 3603 |     "lenlex r = {(xs,ys). length xs < length ys |
 | |
| 15656 | 3604 | length xs = length ys \<and> (xs, ys) : lex r}" | 
| 30198 | 3605 | by (simp add: lenlex_def Id_on_def lex_prod_def inv_image_def) | 
| 15302 | 3606 | |
| 3607 | lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r" | |
| 3608 | by (simp add: lex_conv) | |
| 3609 | ||
| 3610 | lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r" | |
| 3611 | by (simp add:lex_conv) | |
| 3612 | ||
| 18447 | 3613 | lemma Cons_in_lex [simp]: | 
| 15656 | 3614 | "((x # xs, y # ys) : lex r) = | 
| 3615 | ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)" | |
| 15302 | 3616 | apply (simp add: lex_conv) | 
| 3617 | apply (rule iffI) | |
| 3618 | prefer 2 apply (blast intro: Cons_eq_appendI, clarify) | |
| 3619 | apply (case_tac xys, simp, simp) | |
| 3620 | apply blast | |
| 3621 | done | |
| 3622 | ||
| 3623 | ||
| 15656 | 3624 | subsubsection {* Lexicographic Ordering *}
 | 
| 3625 | ||
| 3626 | text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
 | |
| 3627 |     This ordering does \emph{not} preserve well-foundedness.
 | |
| 17090 | 3628 | Author: N. Voelker, March 2005. *} | 
| 15656 | 3629 | |
| 3630 | constdefs | |
| 3631 |   lexord :: "('a * 'a)set \<Rightarrow> ('a list * 'a list) set" 
 | |
| 3632 |   "lexord  r == {(x,y). \<exists> a v. y = x @ a # v \<or> 
 | |
| 3633 | (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}" | |
| 28562 | 3634 | declare lexord_def [code del] | 
| 15656 | 3635 | |
| 3636 | lemma lexord_Nil_left[simp]: "([],y) \<in> lexord r = (\<exists> a x. y = a # x)" | |
| 24349 | 3637 | by (unfold lexord_def, induct_tac y, auto) | 
| 15656 | 3638 | |
| 3639 | lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r" | |
| 24349 | 3640 | by (unfold lexord_def, induct_tac x, auto) | 
| 15656 | 3641 | |
| 3642 | lemma lexord_cons_cons[simp]: | |
| 3643 | "((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))" | |
| 3644 | apply (unfold lexord_def, safe, simp_all) | |
| 3645 | apply (case_tac u, simp, simp) | |
| 3646 | apply (case_tac u, simp, clarsimp, blast, blast, clarsimp) | |
| 3647 | apply (erule_tac x="b # u" in allE) | |
| 3648 | by force | |
| 3649 | ||
| 3650 | lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons | |
| 3651 | ||
| 3652 | lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r" | |
| 24349 | 3653 | by (induct_tac x, auto) | 
| 15656 | 3654 | |
| 3655 | lemma lexord_append_left_rightI: | |
| 3656 | "(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r" | |
| 24349 | 3657 | by (induct_tac u, auto) | 
| 15656 | 3658 | |
| 3659 | lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r" | |
| 24349 | 3660 | by (induct x, auto) | 
| 15656 | 3661 | |
| 3662 | lemma lexord_append_leftD: | |
| 3663 | "\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r" | |
| 24349 | 3664 | by (erule rev_mp, induct_tac x, auto) | 
| 15656 | 3665 | |
| 3666 | lemma lexord_take_index_conv: | |
| 3667 | "((x,y) : lexord r) = | |
| 3668 | ((length x < length y \<and> take (length x) y = x) \<or> | |
| 3669 | (\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))" | |
| 3670 | apply (unfold lexord_def Let_def, clarsimp) | |
| 3671 | apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2) | |
| 3672 | apply auto | |
| 3673 | apply (rule_tac x="hd (drop (length x) y)" in exI) | |
| 3674 | apply (rule_tac x="tl (drop (length x) y)" in exI) | |
| 3675 | apply (erule subst, simp add: min_def) | |
| 3676 | apply (rule_tac x ="length u" in exI, simp) | |
| 3677 | apply (rule_tac x ="take i x" in exI) | |
| 3678 | apply (rule_tac x ="x ! i" in exI) | |
| 3679 | apply (rule_tac x ="y ! i" in exI, safe) | |
| 3680 | apply (rule_tac x="drop (Suc i) x" in exI) | |
| 3681 | apply (drule sym, simp add: drop_Suc_conv_tl) | |
| 3682 | apply (rule_tac x="drop (Suc i) y" in exI) | |
| 3683 | by (simp add: drop_Suc_conv_tl) | |
| 3684 | ||
| 3685 | -- {* lexord is extension of partial ordering List.lex *} 
 | |
| 3686 | lemma lexord_lex: " (x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)" | |
| 3687 | apply (rule_tac x = y in spec) | |
| 3688 | apply (induct_tac x, clarsimp) | |
| 3689 | by (clarify, case_tac x, simp, force) | |
| 3690 | ||
| 3691 | lemma lexord_irreflexive: "(! x. (x,x) \<notin> r) \<Longrightarrow> (y,y) \<notin> lexord r" | |
| 3692 | by (induct y, auto) | |
| 3693 | ||
| 3694 | lemma lexord_trans: | |
| 3695 | "\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r" | |
| 3696 | apply (erule rev_mp)+ | |
| 3697 | apply (rule_tac x = x in spec) | |
| 3698 | apply (rule_tac x = z in spec) | |
| 3699 | apply ( induct_tac y, simp, clarify) | |
| 3700 | apply (case_tac xa, erule ssubst) | |
| 3701 |   apply (erule allE, erule allE) -- {* avoid simp recursion *} 
 | |
| 3702 | apply (case_tac x, simp, simp) | |
| 24632 | 3703 | apply (case_tac x, erule allE, erule allE, simp) | 
| 15656 | 3704 | apply (erule_tac x = listb in allE) | 
| 3705 | apply (erule_tac x = lista in allE, simp) | |
| 3706 | apply (unfold trans_def) | |
| 3707 | by blast | |
| 3708 | ||
| 3709 | lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)" | |
| 24349 | 3710 | by (rule transI, drule lexord_trans, blast) | 
| 15656 | 3711 | |
| 3712 | lemma lexord_linear: "(! a b. (a,b)\<in> r | a = b | (b,a) \<in> r) \<Longrightarrow> (x,y) : lexord r | x = y | (y,x) : lexord r" | |
| 3713 | apply (rule_tac x = y in spec) | |
| 3714 | apply (induct_tac x, rule allI) | |
| 3715 | apply (case_tac x, simp, simp) | |
| 3716 | apply (rule allI, case_tac x, simp, simp) | |
| 3717 | by blast | |
| 3718 | ||
| 3719 | ||
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3720 | subsection {* Lexicographic combination of measure functions *}
 | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3721 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3722 | text {* These are useful for termination proofs *}
 | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3723 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3724 | definition | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3725 | "measures fs = inv_image (lex less_than) (%a. map (%f. f a) fs)" | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3726 | |
| 21106 
51599a81b308
Added "recdef_wf" and "simp" attribute to "wf_measures"
 krauss parents: 
21103diff
changeset | 3727 | lemma wf_measures[recdef_wf, simp]: "wf (measures fs)" | 
| 24349 | 3728 | unfolding measures_def | 
| 3729 | by blast | |
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3730 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3731 | lemma in_measures[simp]: | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3732 | "(x, y) \<in> measures [] = False" | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3733 | "(x, y) \<in> measures (f # fs) | 
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3734 | = (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))" | 
| 24349 | 3735 | unfolding measures_def | 
| 3736 | by auto | |
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3737 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3738 | lemma measures_less: "f x < f y ==> (x, y) \<in> measures (f#fs)" | 
| 24349 | 3739 | by simp | 
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3740 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3741 | lemma measures_lesseq: "f x <= f y ==> (x, y) \<in> measures fs ==> (x, y) \<in> measures (f#fs)" | 
| 24349 | 3742 | by auto | 
| 21103 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3743 | |
| 
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
 krauss parents: 
21079diff
changeset | 3744 | |
| 15392 | 3745 | subsubsection{*Lifting a Relation on List Elements to the Lists*}
 | 
| 15302 | 3746 | |
| 23740 | 3747 | inductive_set | 
| 3748 |   listrel :: "('a * 'a)set => ('a list * 'a list)set"
 | |
| 3749 |   for r :: "('a * 'a)set"
 | |
| 22262 | 3750 | where | 
| 23740 | 3751 | Nil: "([],[]) \<in> listrel r" | 
| 3752 | | Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r" | |
| 3753 | ||
| 3754 | inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r" | |
| 3755 | inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r" | |
| 3756 | inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r" | |
| 3757 | inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r" | |
| 15302 | 3758 | |
| 3759 | ||
| 3760 | lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s" | |
| 3761 | apply clarify | |
| 23740 | 3762 | apply (erule listrel.induct) | 
| 3763 | apply (blast intro: listrel.intros)+ | |
| 15302 | 3764 | done | 
| 3765 | ||
| 3766 | lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A" | |
| 3767 | apply clarify | |
| 23740 | 3768 | apply (erule listrel.induct, auto) | 
| 15302 | 3769 | done | 
| 3770 | ||
| 30198 | 3771 | lemma listrel_refl_on: "refl_on A r \<Longrightarrow> refl_on (lists A) (listrel r)" | 
| 3772 | apply (simp add: refl_on_def listrel_subset Ball_def) | |
| 15302 | 3773 | apply (rule allI) | 
| 3774 | apply (induct_tac x) | |
| 23740 | 3775 | apply (auto intro: listrel.intros) | 
| 15302 | 3776 | done | 
| 3777 | ||
| 3778 | lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" | |
| 3779 | apply (auto simp add: sym_def) | |
| 23740 | 3780 | apply (erule listrel.induct) | 
| 3781 | apply (blast intro: listrel.intros)+ | |
| 15302 | 3782 | done | 
| 3783 | ||
| 3784 | lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)" | |
| 3785 | apply (simp add: trans_def) | |
| 3786 | apply (intro allI) | |
| 3787 | apply (rule impI) | |
| 23740 | 3788 | apply (erule listrel.induct) | 
| 3789 | apply (blast intro: listrel.intros)+ | |
| 15302 | 3790 | done | 
| 3791 | ||
| 3792 | theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)" | |
| 30198 | 3793 | by (simp add: equiv_def listrel_refl_on listrel_sym listrel_trans) | 
| 15302 | 3794 | |
| 3795 | lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}"
 | |
| 23740 | 3796 | by (blast intro: listrel.intros) | 
| 15302 | 3797 | |
| 3798 | lemma listrel_Cons: | |
| 33318 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3799 |      "listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})"
 | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3800 | by (auto simp add: set_Cons_def intro: listrel.intros) | 
| 15302 | 3801 | |
| 3802 | ||
| 26749 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 3803 | subsection {* Size function *}
 | 
| 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 3804 | |
| 26875 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3805 | lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (list_size f)" | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3806 | by (rule is_measure_trivial) | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3807 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3808 | lemma [measure_function]: "is_measure f \<Longrightarrow> is_measure (option_size f)" | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3809 | by (rule is_measure_trivial) | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3810 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3811 | lemma list_size_estimation[termination_simp]: | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3812 | "x \<in> set xs \<Longrightarrow> y < f x \<Longrightarrow> y < list_size f xs" | 
| 26749 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 3813 | by (induct xs) auto | 
| 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 3814 | |
| 26875 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3815 | lemma list_size_estimation'[termination_simp]: | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3816 | "x \<in> set xs \<Longrightarrow> y \<le> f x \<Longrightarrow> y \<le> list_size f xs" | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3817 | by (induct xs) auto | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3818 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3819 | lemma list_size_map[simp]: "list_size f (map g xs) = list_size (f o g) xs" | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3820 | by (induct xs) auto | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3821 | |
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3822 | lemma list_size_pointwise[termination_simp]: | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3823 | "(\<And>x. x \<in> set xs \<Longrightarrow> f x < g x) \<Longrightarrow> list_size f xs \<le> list_size g xs" | 
| 
e18574413bc4
Measure functions can now be declared via special rules, allowing for a
 krauss parents: 
26795diff
changeset | 3824 | by (induct xs) force+ | 
| 26749 
397a1aeede7d
* New attribute "termination_simp": Simp rules for termination proofs
 krauss parents: 
26734diff
changeset | 3825 | |
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3826 | |
| 33318 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3827 | subsection {* Transfer *}
 | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3828 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3829 | definition | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3830 | embed_list :: "nat list \<Rightarrow> int list" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3831 | where | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3832 | "embed_list l = map int l" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3833 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3834 | definition | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3835 | nat_list :: "int list \<Rightarrow> bool" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3836 | where | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3837 | "nat_list l = nat_set (set l)" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3838 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3839 | definition | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3840 | return_list :: "int list \<Rightarrow> nat list" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3841 | where | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3842 | "return_list l = map nat l" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3843 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3844 | lemma transfer_nat_int_list_return_embed: "nat_list l \<longrightarrow> | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3845 | embed_list (return_list l) = l" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3846 | unfolding embed_list_def return_list_def nat_list_def nat_set_def | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3847 | apply (induct l) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3848 | apply auto | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3849 | done | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3850 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3851 | lemma transfer_nat_int_list_functions: | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3852 | "l @ m = return_list (embed_list l @ embed_list m)" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3853 | "[] = return_list []" | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3854 | unfolding return_list_def embed_list_def | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3855 | apply auto | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3856 | apply (induct l, auto) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3857 | apply (induct m, auto) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3858 | done | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3859 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3860 | (* | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3861 | lemma transfer_nat_int_fold1: "fold f l x = | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3862 | fold (%x. f (nat x)) (embed_list l) x"; | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3863 | *) | 
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3864 | |
| 
ddd97d9dfbfb
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
 haftmann parents: 
32960diff
changeset | 3865 | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3866 | subsection {* Code generator *}
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3867 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3868 | subsubsection {* Setup *}
 | 
| 15064 
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
 berghofe parents: 
15045diff
changeset | 3869 | |
| 31055 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3870 | use "Tools/list_code.ML" | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3871 | |
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3872 | code_type list | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3873 | (SML "_ list") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3874 | (OCaml "_ list") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3875 | (Haskell "![_]") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3876 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3877 | code_const Nil | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3878 | (SML "[]") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3879 | (OCaml "[]") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3880 | (Haskell "[]") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3881 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3882 | code_instance list :: eq | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3883 | (Haskell -) | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3884 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3885 | code_const "eq_class.eq \<Colon> 'a\<Colon>eq list \<Rightarrow> 'a list \<Rightarrow> bool" | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3886 | (Haskell infixl 4 "==") | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3887 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3888 | code_reserved SML | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3889 | list | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3890 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3891 | code_reserved OCaml | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3892 | list | 
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3893 | |
| 16770 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
 berghofe parents: 
16634diff
changeset | 3894 | types_code | 
| 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
 berghofe parents: 
16634diff
changeset | 3895 |   "list" ("_ list")
 | 
| 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
 berghofe parents: 
16634diff
changeset | 3896 | attach (term_of) {*
 | 
| 21760 | 3897 | fun term_of_list f T = HOLogic.mk_list T o map f; | 
| 16770 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
 berghofe parents: 
16634diff
changeset | 3898 | *} | 
| 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
 berghofe parents: 
16634diff
changeset | 3899 | attach (test) {*
 | 
| 25885 | 3900 | fun gen_list' aG aT i j = frequency | 
| 3901 | [(i, fn () => | |
| 3902 | let | |
| 3903 | val (x, t) = aG j; | |
| 3904 | val (xs, ts) = gen_list' aG aT (i-1) j | |
| 3905 | in (x :: xs, fn () => HOLogic.cons_const aT $ t () $ ts ()) end), | |
| 3906 | (1, fn () => ([], fn () => HOLogic.nil_const aT))] () | |
| 3907 | and gen_list aG aT i = gen_list' aG aT i i; | |
| 16770 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
 berghofe parents: 
16634diff
changeset | 3908 | *} | 
| 31048 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3909 | |
| 
ac146fc38b51
refined HOL string theories and corresponding ML fragments
 haftmann parents: 
31022diff
changeset | 3910 | consts_code Cons ("(_ ::/ _)")
 | 
| 20588 | 3911 | |
| 20453 
855f07fabd76
final syntax for some Isar code generator keywords
 haftmann parents: 
20439diff
changeset | 3912 | setup {*
 | 
| 
855f07fabd76
final syntax for some Isar code generator keywords
 haftmann parents: 
20439diff
changeset | 3913 | let | 
| 31055 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3914 | fun list_codegen thy defs dep thyname b t gr = | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3915 | let | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3916 | val ts = HOLogic.dest_list t; | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3917 | val (_, gr') = Codegen.invoke_tycodegen thy defs dep thyname false | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3918 | (fastype_of t) gr; | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3919 | val (ps, gr'') = fold_map | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3920 | (Codegen.invoke_codegen thy defs dep thyname false) ts gr' | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3921 | in SOME (Pretty.list "[" "]" ps, gr'') end handle TERM _ => NONE; | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3922 | in | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3923 | fold (List_Code.add_literal_list) ["SML", "OCaml", "Haskell"] | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3924 | #> Codegen.add_codegen "list_codegen" list_codegen | 
| 
2cf6efca6c71
proper structures for list and string code generation stuff
 haftmann parents: 
31048diff
changeset | 3925 | end | 
| 20453 
855f07fabd76
final syntax for some Isar code generator keywords
 haftmann parents: 
20439diff
changeset | 3926 | *} | 
| 15064 
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
 berghofe parents: 
15045diff
changeset | 3927 | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3928 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3929 | subsubsection {* Generation of efficient code *}
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3930 | |
| 25221 
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
 wenzelm parents: 
25215diff
changeset | 3931 | primrec | 
| 25559 | 3932 | member :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" (infixl "mem" 55) | 
| 3933 | where | |
| 3934 | "x mem [] \<longleftrightarrow> False" | |
| 28515 | 3935 | | "x mem (y#ys) \<longleftrightarrow> x = y \<or> x mem ys" | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3936 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3937 | primrec | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3938 | null:: "'a list \<Rightarrow> bool" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3939 | where | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3940 | "null [] = True" | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3941 | | "null (x#xs) = False" | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3942 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3943 | primrec | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3944 | list_inter :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3945 | where | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3946 | "list_inter [] bs = []" | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3947 | | "list_inter (a#as) bs = | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3948 | (if a \<in> set bs then a # list_inter as bs else list_inter as bs)" | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3949 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3950 | primrec | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3951 |   list_all :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<Rightarrow> bool)"
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3952 | where | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3953 | "list_all P [] = True" | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3954 | | "list_all P (x#xs) = (P x \<and> list_all P xs)" | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3955 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3956 | primrec | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3957 |   list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3958 | where | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3959 | "list_ex P [] = False" | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3960 | | "list_ex P (x#xs) = (P x \<or> list_ex P xs)" | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3961 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3962 | primrec | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3963 |   filtermap :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list"
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3964 | where | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3965 | "filtermap f [] = []" | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3966 | | "filtermap f (x#xs) = | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3967 | (case f x of None \<Rightarrow> filtermap f xs | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3968 | | Some y \<Rightarrow> y # filtermap f xs)" | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3969 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3970 | primrec | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3971 |   map_filter :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list"
 | 
| 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3972 | where | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3973 | "map_filter f P [] = []" | 
| 26442 
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
 haftmann parents: 
26300diff
changeset | 3974 | | "map_filter f P (x#xs) = | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3975 | (if P x then f x # map_filter f P xs else map_filter f P xs)" | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3976 | |
| 28789 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 3977 | primrec | 
| 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 3978 | length_unique :: "'a list \<Rightarrow> nat" | 
| 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 3979 | where | 
| 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 3980 | "length_unique [] = 0" | 
| 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 3981 | | "length_unique (x#xs) = | 
| 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 3982 | (if x \<in> set xs then length_unique xs else Suc (length_unique xs))" | 
| 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 3983 | |
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3984 | primrec | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3985 |   concat_map :: "('a => 'b list) => 'a list => 'b list"
 | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3986 | where | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3987 | "concat_map f [] = []" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3988 | | "concat_map f (x#xs) = f x @ concat_map f xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 3989 | |
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3990 | text {*
 | 
| 21754 
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
 wenzelm parents: 
21548diff
changeset | 3991 |   Only use @{text mem} for generating executable code.  Otherwise use
 | 
| 
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
 wenzelm parents: 
21548diff
changeset | 3992 |   @{prop "x : set xs"} instead --- it is much easier to reason about.
 | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3993 |   The same is true for @{const list_all} and @{const list_ex}: write
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3994 |   @{text "\<forall>x\<in>set xs"} and @{text "\<exists>x\<in>set xs"} instead because the HOL
 | 
| 21754 
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
 wenzelm parents: 
21548diff
changeset | 3995 | quantifiers are aleady known to the automatic provers. In fact, the | 
| 
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
 wenzelm parents: 
21548diff
changeset | 3996 |   declarations in the code subsection make sure that @{text "\<in>"},
 | 
| 
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
 wenzelm parents: 
21548diff
changeset | 3997 |   @{text "\<forall>x\<in>set xs"} and @{text "\<exists>x\<in>set xs"} are implemented
 | 
| 
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
 wenzelm parents: 
21548diff
changeset | 3998 | efficiently. | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 3999 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4000 |   Efficient emptyness check is implemented by @{const null}.
 | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4001 | |
| 23060 | 4002 |   The functions @{const filtermap} and @{const map_filter} are just
 | 
| 4003 | there to generate efficient code. Do not use | |
| 21754 
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
 wenzelm parents: 
21548diff
changeset | 4004 | them for modelling and proving. | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4005 | *} | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4006 | |
| 23060 | 4007 | lemma rev_foldl_cons [code]: | 
| 4008 | "rev xs = foldl (\<lambda>xs x. x # xs) [] xs" | |
| 4009 | proof (induct xs) | |
| 4010 | case Nil then show ?case by simp | |
| 4011 | next | |
| 4012 | case Cons | |
| 4013 |   {
 | |
| 4014 | fix x xs ys | |
| 4015 | have "foldl (\<lambda>xs x. x # xs) ys xs @ [x] | |
| 4016 | = foldl (\<lambda>xs x. x # xs) (ys @ [x]) xs" | |
| 4017 | by (induct xs arbitrary: ys) auto | |
| 4018 | } | |
| 4019 | note aux = this | |
| 4020 | show ?case by (induct xs) (auto simp add: Cons aux) | |
| 4021 | qed | |
| 4022 | ||
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4023 | lemma mem_iff [code_post]: | 
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4024 | "x mem xs \<longleftrightarrow> x \<in> set xs" | 
| 24349 | 4025 | by (induct xs) auto | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4026 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4027 | lemmas in_set_code [code_unfold] = mem_iff [symmetric] | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4028 | |
| 31154 | 4029 | lemma empty_null: | 
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4030 | "xs = [] \<longleftrightarrow> null xs" | 
| 24349 | 4031 | by (cases xs) simp_all | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4032 | |
| 32069 
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
 haftmann parents: 
31998diff
changeset | 4033 | lemma [code_unfold]: | 
| 31154 | 4034 | "eq_class.eq xs [] \<longleftrightarrow> null xs" | 
| 4035 | by (simp add: eq empty_null) | |
| 4036 | ||
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4037 | lemmas null_empty [code_post] = | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4038 | empty_null [symmetric] | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4039 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4040 | lemma list_inter_conv: | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4041 | "set (list_inter xs ys) = set xs \<inter> set ys" | 
| 24349 | 4042 | by (induct xs) auto | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4043 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4044 | lemma list_all_iff [code_post]: | 
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4045 | "list_all P xs \<longleftrightarrow> (\<forall>x \<in> set xs. P x)" | 
| 24349 | 4046 | by (induct xs) auto | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4047 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4048 | lemmas list_ball_code [code_unfold] = list_all_iff [symmetric] | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4049 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4050 | lemma list_all_append [simp]: | 
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4051 | "list_all P (xs @ ys) \<longleftrightarrow> (list_all P xs \<and> list_all P ys)" | 
| 24349 | 4052 | by (induct xs) auto | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4053 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4054 | lemma list_all_rev [simp]: | 
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4055 | "list_all P (rev xs) \<longleftrightarrow> list_all P xs" | 
| 24349 | 4056 | by (simp add: list_all_iff) | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4057 | |
| 22506 | 4058 | lemma list_all_length: | 
| 4059 | "list_all P xs \<longleftrightarrow> (\<forall>n < length xs. P (xs ! n))" | |
| 4060 | unfolding list_all_iff by (auto intro: all_nth_imp_all_set) | |
| 4061 | ||
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4062 | lemma list_ex_iff [code_post]: | 
| 22422 
ee19cdb07528
stepping towards uniform lattice theory development in HOL
 haftmann parents: 
22262diff
changeset | 4063 | "list_ex P xs \<longleftrightarrow> (\<exists>x \<in> set xs. P x)" | 
| 24349 | 4064 | by (induct xs) simp_all | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4065 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4066 | lemmas list_bex_code [code_unfold] = | 
| 22799 
ed7d53db2170
moved code generation pretty integers and characters to separate theories
 haftmann parents: 
22793diff
changeset | 4067 | list_ex_iff [symmetric] | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4068 | |
| 22506 | 4069 | lemma list_ex_length: | 
| 4070 | "list_ex P xs \<longleftrightarrow> (\<exists>n < length xs. P (xs ! n))" | |
| 4071 | unfolding list_ex_iff set_conv_nth by auto | |
| 4072 | ||
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4073 | lemma filtermap_conv: | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4074 | "filtermap f xs = map (\<lambda>x. the (f x)) (filter (\<lambda>x. f x \<noteq> None) xs)" | 
| 24349 | 4075 | by (induct xs) (simp_all split: option.split) | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4076 | |
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4077 | lemma map_filter_conv [simp]: | 
| 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4078 | "map_filter f P xs = map f (filter P xs)" | 
| 24349 | 4079 | by (induct xs) auto | 
| 21061 
580dfc999ef6
added normal post setup; cleaned up "execution" constants
 haftmann parents: 
21046diff
changeset | 4080 | |
| 32069 
6d28bbd33e2c
prefer code_inline over code_unfold; use code_unfold_post where appropriate
 haftmann parents: 
31998diff
changeset | 4081 | lemma length_remdups_length_unique [code_unfold]: | 
| 28789 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 4082 | "length (remdups xs) = length_unique xs" | 
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4083 | by (induct xs) simp_all | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4084 | |
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4085 | lemma concat_map_code[code_unfold]: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4086 | "concat(map f xs) = concat_map f xs" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4087 | by (induct xs) simp_all | 
| 28789 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 4088 | |
| 32681 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 4089 | declare INFI_def [code_unfold] | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 4090 | declare SUPR_def [code_unfold] | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 4091 | |
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 4092 | declare set_map [symmetric, code_unfold] | 
| 
adeac3cbb659
lemma relating fold1 and foldl; code_unfold rules for Inf_fin, Sup_fin, Min, Max, Inf, Sup
 haftmann parents: 
32422diff
changeset | 4093 | |
| 28789 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 4094 | hide (open) const length_unique | 
| 
5a404273ea8f
added length_unique operation for code generation
 haftmann parents: 
28708diff
changeset | 4095 | |
| 24449 | 4096 | |
| 4097 | text {* Code for bounded quantification and summation over nats. *}
 | |
| 21891 
b4e4ea3db161
added code lemmas for quantification over bounded nats
 haftmann parents: 
21871diff
changeset | 4098 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4099 | lemma atMost_upto [code_unfold]: | 
| 28072 
a45e8c872dc1
It appears that the code generator (Stefan's) needs some laws that appear superfluous: {..n} = set ...
 nipkow parents: 
28068diff
changeset | 4100 |   "{..n} = set [0..<Suc n]"
 | 
| 
a45e8c872dc1
It appears that the code generator (Stefan's) needs some laws that appear superfluous: {..n} = set ...
 nipkow parents: 
28068diff
changeset | 4101 | by auto | 
| 
a45e8c872dc1
It appears that the code generator (Stefan's) needs some laws that appear superfluous: {..n} = set ...
 nipkow parents: 
28068diff
changeset | 4102 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4103 | lemma atLeast_upt [code_unfold]: | 
| 28072 
a45e8c872dc1
It appears that the code generator (Stefan's) needs some laws that appear superfluous: {..n} = set ...
 nipkow parents: 
28068diff
changeset | 4104 |   "{..<n} = set [0..<n]"
 | 
| 
a45e8c872dc1
It appears that the code generator (Stefan's) needs some laws that appear superfluous: {..n} = set ...
 nipkow parents: 
28068diff
changeset | 4105 | by auto | 
| 
a45e8c872dc1
It appears that the code generator (Stefan's) needs some laws that appear superfluous: {..n} = set ...
 nipkow parents: 
28068diff
changeset | 4106 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4107 | lemma greaterThanLessThan_upt [code_unfold]: | 
| 21891 
b4e4ea3db161
added code lemmas for quantification over bounded nats
 haftmann parents: 
21871diff
changeset | 4108 |   "{n<..<m} = set [Suc n..<m]"
 | 
| 24349 | 4109 | by auto | 
| 22799 
ed7d53db2170
moved code generation pretty integers and characters to separate theories
 haftmann parents: 
22793diff
changeset | 4110 | |
| 32417 | 4111 | lemmas atLeastLessThan_upt [code_unfold] = set_upt [symmetric] | 
| 22799 
ed7d53db2170
moved code generation pretty integers and characters to separate theories
 haftmann parents: 
22793diff
changeset | 4112 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4113 | lemma greaterThanAtMost_upt [code_unfold]: | 
| 24645 | 4114 |   "{n<..m} = set [Suc n..<Suc m]"
 | 
| 24349 | 4115 | by auto | 
| 22799 
ed7d53db2170
moved code generation pretty integers and characters to separate theories
 haftmann parents: 
22793diff
changeset | 4116 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4117 | lemma atLeastAtMost_upt [code_unfold]: | 
| 24645 | 4118 |   "{n..m} = set [n..<Suc m]"
 | 
| 24349 | 4119 | by auto | 
| 22799 
ed7d53db2170
moved code generation pretty integers and characters to separate theories
 haftmann parents: 
22793diff
changeset | 4120 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4121 | lemma all_nat_less_eq [code_unfold]: | 
| 21891 
b4e4ea3db161
added code lemmas for quantification over bounded nats
 haftmann parents: 
21871diff
changeset | 4122 |   "(\<forall>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..<n}. P m)"
 | 
| 24349 | 4123 | by auto | 
| 22799 
ed7d53db2170
moved code generation pretty integers and characters to separate theories
 haftmann parents: 
22793diff
changeset | 4124 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4125 | lemma ex_nat_less_eq [code_unfold]: | 
| 21891 
b4e4ea3db161
added code lemmas for quantification over bounded nats
 haftmann parents: 
21871diff
changeset | 4126 |   "(\<exists>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..<n}. P m)"
 | 
| 24349 | 4127 | by auto | 
| 22799 
ed7d53db2170
moved code generation pretty integers and characters to separate theories
 haftmann parents: 
22793diff
changeset | 4128 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4129 | lemma all_nat_less [code_unfold]: | 
| 21891 
b4e4ea3db161
added code lemmas for quantification over bounded nats
 haftmann parents: 
21871diff
changeset | 4130 |   "(\<forall>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..n}. P m)"
 | 
| 24349 | 4131 | by auto | 
| 22799 
ed7d53db2170
moved code generation pretty integers and characters to separate theories
 haftmann parents: 
22793diff
changeset | 4132 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4133 | lemma ex_nat_less [code_unfold]: | 
| 21891 
b4e4ea3db161
added code lemmas for quantification over bounded nats
 haftmann parents: 
21871diff
changeset | 4134 |   "(\<exists>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..n}. P m)"
 | 
| 24349 | 4135 | by auto | 
| 22799 
ed7d53db2170
moved code generation pretty integers and characters to separate theories
 haftmann parents: 
22793diff
changeset | 4136 | |
| 27715 | 4137 | lemma setsum_set_distinct_conv_listsum: | 
| 4138 | "distinct xs \<Longrightarrow> setsum f (set xs) = listsum (map f xs)" | |
| 4139 | by (induct xs) simp_all | |
| 4140 | ||
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4141 | lemma setsum_set_upt_conv_listsum [code_unfold]: | 
| 27715 | 4142 | "setsum f (set [m..<n]) = listsum (map f [m..<n])" | 
| 4143 | by (rule setsum_set_distinct_conv_listsum) simp | |
| 4144 | ||
| 33639 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4145 | text {* General equivalence between @{const listsum} and @{const setsum} *}
 | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4146 | lemma listsum_setsum_nth: | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4147 | "listsum xs = (\<Sum> i = 0 ..< length xs. xs ! i)" | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4148 | using setsum_set_upt_conv_listsum[of "op ! xs" 0 "length xs"] | 
| 
603320b93668
New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
 hoelzl parents: 
33593diff
changeset | 4149 | by (simp add: map_nth) | 
| 27715 | 4150 | |
| 4151 | text {* Code for summation over ints. *}
 | |
| 4152 | ||
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4153 | lemma greaterThanLessThan_upto [code_unfold]: | 
| 27715 | 4154 |   "{i<..<j::int} = set [i+1..j - 1]"
 | 
| 4155 | by auto | |
| 4156 | ||
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4157 | lemma atLeastLessThan_upto [code_unfold]: | 
| 27715 | 4158 |   "{i..<j::int} = set [i..j - 1]"
 | 
| 4159 | by auto | |
| 4160 | ||
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4161 | lemma greaterThanAtMost_upto [code_unfold]: | 
| 27715 | 4162 |   "{i<..j::int} = set [i+1..j]"
 | 
| 4163 | by auto | |
| 4164 | ||
| 32415 
1dddf2f64266
got rid of complicated class finite_intvl_succ and defined "upto" directly on int, the only instance of the class.
 nipkow parents: 
32078diff
changeset | 4165 | lemmas atLeastAtMost_upto [code_unfold] = set_upto[symmetric] | 
| 27715 | 4166 | |
| 31998 
2c7a24f74db9
code attributes use common underscore convention
 haftmann parents: 
31930diff
changeset | 4167 | lemma setsum_set_upto_conv_listsum [code_unfold]: | 
| 27715 | 4168 | "setsum f (set [i..j::int]) = listsum (map f [i..j])" | 
| 4169 | by (rule setsum_set_distinct_conv_listsum) simp | |
| 24449 | 4170 | |
| 32422 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4171 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4172 | text {* Optimized code for @{text"\<forall>i\<in>{a..b::int}"} and @{text"\<forall>n:{a..<b::nat}"}
 | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4173 | and similiarly for @{text"\<exists>"}. *}
 | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4174 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4175 | function all_from_to_nat :: "(nat \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4176 | "all_from_to_nat P i j = | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4177 | (if i < j then if P i then all_from_to_nat P (i+1) j else False | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4178 | else True)" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4179 | by auto | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4180 | termination | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4181 | by (relation "measure(%(P,i,j). j - i)") auto | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4182 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4183 | declare all_from_to_nat.simps[simp del] | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4184 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4185 | lemma all_from_to_nat_iff_ball: | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4186 |   "all_from_to_nat P i j = (ALL n : {i ..< j}. P n)"
 | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4187 | proof(induct P i j rule:all_from_to_nat.induct) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4188 | case (1 P i j) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4189 | let ?yes = "i < j & P i" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4190 | show ?case | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4191 | proof (cases) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4192 | assume ?yes | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4193 | hence "all_from_to_nat P i j = (P i & all_from_to_nat P (i+1) j)" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4194 | by(simp add: all_from_to_nat.simps) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4195 |     also have "... = (P i & (ALL n : {i+1 ..< j}. P n))" using `?yes` 1 by simp
 | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4196 |     also have "... = (ALL n : {i ..< j}. P n)" (is "?L = ?R")
 | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4197 | proof | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4198 | assume L: ?L | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4199 | show ?R | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4200 | proof clarify | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4201 |         fix n assume n: "n : {i..<j}"
 | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4202 | show "P n" | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4203 | proof cases | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4204 | assume "n = i" thus "P n" using L by simp | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4205 | next | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4206 | assume "n ~= i" | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4207 | hence "i+1 <= n" using n by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4208 | thus "P n" using L n by simp | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4209 | qed | 
| 32422 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4210 | qed | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4211 | next | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4212 | assume R: ?R thus ?L using `?yes` 1 by auto | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4213 | qed | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4214 | finally show ?thesis . | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4215 | next | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4216 | assume "~?yes" thus ?thesis by(auto simp add: all_from_to_nat.simps) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4217 | qed | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4218 | qed | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4219 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4220 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4221 | lemma list_all_iff_all_from_to_nat[code_unfold]: | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4222 | "list_all P [i..<j] = all_from_to_nat P i j" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4223 | by(simp add: all_from_to_nat_iff_ball list_all_iff) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4224 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4225 | lemma list_ex_iff_not_all_from_to_not_nat[code_unfold]: | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4226 | "list_ex P [i..<j] = (~all_from_to_nat (%x. ~P x) i j)" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4227 | by(simp add: all_from_to_nat_iff_ball list_ex_iff) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4228 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4229 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4230 | function all_from_to_int :: "(int \<Rightarrow> bool) \<Rightarrow> int \<Rightarrow> int \<Rightarrow> bool" where | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4231 | "all_from_to_int P i j = | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4232 | (if i <= j then if P i then all_from_to_int P (i+1) j else False | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4233 | else True)" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4234 | by auto | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4235 | termination | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4236 | by (relation "measure(%(P,i,j). nat(j - i + 1))") auto | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4237 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4238 | declare all_from_to_int.simps[simp del] | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4239 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4240 | lemma all_from_to_int_iff_ball: | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4241 |   "all_from_to_int P i j = (ALL n : {i .. j}. P n)"
 | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4242 | proof(induct P i j rule:all_from_to_int.induct) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4243 | case (1 P i j) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4244 | let ?yes = "i <= j & P i" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4245 | show ?case | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4246 | proof (cases) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4247 | assume ?yes | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4248 | hence "all_from_to_int P i j = (P i & all_from_to_int P (i+1) j)" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4249 | by(simp add: all_from_to_int.simps) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4250 |     also have "... = (P i & (ALL n : {i+1 .. j}. P n))" using `?yes` 1 by simp
 | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4251 |     also have "... = (ALL n : {i .. j}. P n)" (is "?L = ?R")
 | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4252 | proof | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4253 | assume L: ?L | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4254 | show ?R | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4255 | proof clarify | 
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4256 |         fix n assume n: "n : {i..j}"
 | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4257 | show "P n" | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4258 | proof cases | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4259 | assume "n = i" thus "P n" using L by simp | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4260 | next | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4261 | assume "n ~= i" | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4262 | hence "i+1 <= n" using n by auto | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4263 | thus "P n" using L n by simp | 
| 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32681diff
changeset | 4264 | qed | 
| 32422 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4265 | qed | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4266 | next | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4267 | assume R: ?R thus ?L using `?yes` 1 by auto | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4268 | qed | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4269 | finally show ?thesis . | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4270 | next | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4271 | assume "~?yes" thus ?thesis by(auto simp add: all_from_to_int.simps) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4272 | qed | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4273 | qed | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4274 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4275 | lemma list_all_iff_all_from_to_int[code_unfold]: | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4276 | "list_all P [i..j] = all_from_to_int P i j" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4277 | by(simp add: all_from_to_int_iff_ball list_all_iff) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4278 | |
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4279 | lemma list_ex_iff_not_all_from_to_not_int[code_unfold]: | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4280 | "list_ex P [i..j] = (~ all_from_to_int (%x. ~P x) i j)" | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4281 | by(simp add: all_from_to_int_iff_ball list_ex_iff) | 
| 
46fc4d4ff4c0
code generator: quantifiers over {_.._::int} and {_..<_::nat}
 nipkow parents: 
32417diff
changeset | 4282 | |
| 23388 | 4283 | end |