author | nipkow |
Wed, 19 Sep 2007 13:14:00 +0200 | |
changeset 24645 | 1af302128da2 |
parent 24640 | 85a6c200ecd3 |
child 24648 | 1e8053a6d725 |
permissions | -rw-r--r-- |
13462 | 1 |
(* Title: HOL/List.thy |
2 |
ID: $Id$ |
|
3 |
Author: Tobias Nipkow |
|
923 | 4 |
*) |
5 |
||
13114 | 6 |
header {* The datatype of finite lists *} |
13122 | 7 |
|
15131 | 8 |
theory List |
22844 | 9 |
imports PreList |
21754
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset
|
10 |
uses "Tools/string_syntax.ML" |
15131 | 11 |
begin |
923 | 12 |
|
13142 | 13 |
datatype 'a list = |
13366 | 14 |
Nil ("[]") |
15 |
| Cons 'a "'a list" (infixr "#" 65) |
|
923 | 16 |
|
15392 | 17 |
subsection{*Basic list processing functions*} |
15302 | 18 |
|
923 | 19 |
consts |
13366 | 20 |
filter:: "('a => bool) => 'a list => 'a list" |
21 |
concat:: "'a list list => 'a list" |
|
22 |
foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b" |
|
23 |
foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b" |
|
24 |
hd:: "'a list => 'a" |
|
25 |
tl:: "'a list => 'a list" |
|
26 |
last:: "'a list => 'a" |
|
27 |
butlast :: "'a list => 'a list" |
|
28 |
set :: "'a list => 'a set" |
|
29 |
map :: "('a=>'b) => ('a list => 'b list)" |
|
23096 | 30 |
listsum :: "'a list => 'a::monoid_add" |
13366 | 31 |
nth :: "'a list => nat => 'a" (infixl "!" 100) |
32 |
list_update :: "'a list => nat => 'a => 'a list" |
|
33 |
take:: "nat => 'a list => 'a list" |
|
34 |
drop:: "nat => 'a list => 'a list" |
|
35 |
takeWhile :: "('a => bool) => 'a list => 'a list" |
|
36 |
dropWhile :: "('a => bool) => 'a list => 'a list" |
|
37 |
rev :: "'a list => 'a list" |
|
38 |
zip :: "'a list => 'b list => ('a * 'b) list" |
|
15425 | 39 |
upt :: "nat => nat => nat list" ("(1[_..</_'])") |
13366 | 40 |
remdups :: "'a list => 'a list" |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
41 |
remove1 :: "'a => 'a list => 'a list" |
13366 | 42 |
"distinct":: "'a list => bool" |
43 |
replicate :: "nat => 'a => 'a list" |
|
19390 | 44 |
splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
15302 | 45 |
|
24645 | 46 |
(* has been generalized |
19363 | 47 |
abbreviation |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
48 |
upto:: "nat => nat => nat list" ("(1[_../_])") where |
19363 | 49 |
"[i..j] == [i..<(Suc j)]" |
24645 | 50 |
*) |
923 | 51 |
|
13146 | 52 |
nonterminals lupdbinds lupdbind |
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
53 |
|
923 | 54 |
syntax |
13366 | 55 |
-- {* list Enumeration *} |
56 |
"@list" :: "args => 'a list" ("[(_)]") |
|
923 | 57 |
|
13366 | 58 |
-- {* Special syntax for filter *} |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
59 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list" ("(1[_<-_./ _])") |
923 | 60 |
|
13366 | 61 |
-- {* list update *} |
62 |
"_lupdbind":: "['a, 'a] => lupdbind" ("(2_ :=/ _)") |
|
63 |
"" :: "lupdbind => lupdbinds" ("_") |
|
64 |
"_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _") |
|
65 |
"_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:
4643
diff
changeset
|
66 |
|
923 | 67 |
translations |
13366 | 68 |
"[x, xs]" == "x#[xs]" |
69 |
"[x]" == "x#[]" |
|
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
70 |
"[x<-xs . P]"== "filter (%x. P) xs" |
923 | 71 |
|
13366 | 72 |
"_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs" |
73 |
"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:
4643
diff
changeset
|
74 |
|
5427 | 75 |
|
12114
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
wenzelm
parents:
10832
diff
changeset
|
76 |
syntax (xsymbols) |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
77 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])") |
14565 | 78 |
syntax (HTML output) |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
79 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])") |
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
80 |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
81 |
|
13142 | 82 |
text {* |
14589 | 83 |
Function @{text size} is overloaded for all datatypes. Users may |
13366 | 84 |
refer to the list version as @{text length}. *} |
13142 | 85 |
|
19363 | 86 |
abbreviation |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
87 |
length :: "'a list => nat" where |
19363 | 88 |
"length == size" |
15302 | 89 |
|
5183 | 90 |
primrec |
15307 | 91 |
"hd(x#xs) = x" |
92 |
||
5183 | 93 |
primrec |
15307 | 94 |
"tl([]) = []" |
95 |
"tl(x#xs) = xs" |
|
96 |
||
5183 | 97 |
primrec |
15307 | 98 |
"last(x#xs) = (if xs=[] then x else last xs)" |
99 |
||
5183 | 100 |
primrec |
15307 | 101 |
"butlast []= []" |
102 |
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" |
|
103 |
||
5183 | 104 |
primrec |
15307 | 105 |
"set [] = {}" |
106 |
"set (x#xs) = insert x (set xs)" |
|
107 |
||
5183 | 108 |
primrec |
15307 | 109 |
"map f [] = []" |
110 |
"map f (x#xs) = f(x)#map f xs" |
|
111 |
||
24645 | 112 |
fun (*authentic syntax for append -- revert to primrec |
23235 | 113 |
as soon as "authentic" primrec is available*) |
114 |
append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) |
|
115 |
where |
|
116 |
append_Nil: "[] @ ys = ys" |
|
117 |
| append_Cons: "(x # xs) @ ys = x # (xs @ ys)" |
|
15307 | 118 |
|
5183 | 119 |
primrec |
15307 | 120 |
"rev([]) = []" |
121 |
"rev(x#xs) = rev(xs) @ [x]" |
|
122 |
||
5183 | 123 |
primrec |
15307 | 124 |
"filter P [] = []" |
125 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
126 |
||
5183 | 127 |
primrec |
15307 | 128 |
foldl_Nil:"foldl f a [] = a" |
129 |
foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" |
|
130 |
||
8000 | 131 |
primrec |
15307 | 132 |
"foldr f [] a = a" |
133 |
"foldr f (x#xs) a = f x (foldr f xs a)" |
|
134 |
||
5183 | 135 |
primrec |
15307 | 136 |
"concat([]) = []" |
137 |
"concat(x#xs) = x @ concat(xs)" |
|
138 |
||
5183 | 139 |
primrec |
23096 | 140 |
"listsum [] = 0" |
141 |
"listsum (x # xs) = x + listsum xs" |
|
142 |
||
143 |
primrec |
|
15307 | 144 |
drop_Nil:"drop n [] = []" |
145 |
drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" |
|
146 |
-- {*Warning: simpset does not contain this definition, but separate |
|
147 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
148 |
||
5183 | 149 |
primrec |
15307 | 150 |
take_Nil:"take n [] = []" |
151 |
take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
|
152 |
-- {*Warning: simpset does not contain this definition, but separate |
|
153 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
154 |
||
13142 | 155 |
primrec |
15307 | 156 |
nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)" |
157 |
-- {*Warning: simpset does not contain this definition, but separate |
|
158 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
159 |
||
5183 | 160 |
primrec |
15307 | 161 |
"[][i:=v] = []" |
162 |
"(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])" |
|
163 |
||
164 |
primrec |
|
165 |
"takeWhile P [] = []" |
|
166 |
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" |
|
167 |
||
5183 | 168 |
primrec |
15307 | 169 |
"dropWhile P [] = []" |
170 |
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" |
|
171 |
||
5183 | 172 |
primrec |
15307 | 173 |
"zip xs [] = []" |
174 |
zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)" |
|
175 |
-- {*Warning: simpset does not contain this definition, but separate |
|
176 |
theorems for @{text "xs = []"} and @{text "xs = z # zs"} *} |
|
177 |
||
5427 | 178 |
primrec |
15425 | 179 |
upt_0: "[i..<0] = []" |
180 |
upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])" |
|
15307 | 181 |
|
5183 | 182 |
primrec |
15307 | 183 |
"distinct [] = True" |
184 |
"distinct (x#xs) = (x ~: set xs \<and> distinct xs)" |
|
185 |
||
5183 | 186 |
primrec |
15307 | 187 |
"remdups [] = []" |
188 |
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" |
|
189 |
||
5183 | 190 |
primrec |
15307 | 191 |
"remove1 x [] = []" |
192 |
"remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)" |
|
193 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
194 |
primrec |
15307 | 195 |
replicate_0: "replicate 0 x = []" |
196 |
replicate_Suc: "replicate (Suc n) x = x # replicate n x" |
|
197 |
||
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
198 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
199 |
rotate1 :: "'a list \<Rightarrow> 'a list" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
200 |
"rotate1 xs = (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])" |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
201 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
202 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
203 |
rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
204 |
"rotate n = rotate1 ^ n" |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
205 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
206 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
207 |
list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
208 |
"list_all2 P xs ys = |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
209 |
(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:
21211
diff
changeset
|
210 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
211 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
212 |
sublist :: "'a list => nat set => 'a list" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
213 |
"sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))" |
17086 | 214 |
|
215 |
primrec |
|
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
216 |
"splice [] ys = ys" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
217 |
"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:
21046
diff
changeset
|
218 |
-- {*Warning: simpset does not contain the second eqn but a derived one. *} |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
219 |
|
24616 | 220 |
text{* The following simple sort functions are intended for proofs, |
221 |
not for efficient implementations. *} |
|
222 |
||
223 |
fun (in linorder) sorted :: "'a list \<Rightarrow> bool" where |
|
224 |
"sorted [] \<longleftrightarrow> True" | |
|
225 |
"sorted [x] \<longleftrightarrow> True" | |
|
226 |
"sorted (x#y#zs) \<longleftrightarrow> x \<^loc><= y \<and> sorted (y#zs)" |
|
227 |
||
228 |
fun (in linorder) insort :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
229 |
"insort x [] = [x]" | |
|
230 |
"insort x (y#ys) = (if x \<^loc><= y then (x#y#ys) else y#(insort x ys))" |
|
231 |
||
232 |
fun (in linorder) sort :: "'a list \<Rightarrow> 'a list" where |
|
233 |
"sort [] = []" | |
|
234 |
"sort (x#xs) = insort x (sort xs)" |
|
235 |
||
236 |
||
23388 | 237 |
subsubsection {* List comprehension *} |
23192 | 238 |
|
24349 | 239 |
text{* Input syntax for Haskell-like list comprehension notation. |
240 |
Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"}, |
|
241 |
the list of all pairs of distinct elements from @{text xs} and @{text ys}. |
|
242 |
The syntax is as in Haskell, except that @{text"|"} becomes a dot |
|
243 |
(like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than |
|
244 |
\verb![e| x <- xs, ...]!. |
|
245 |
||
246 |
The qualifiers after the dot are |
|
247 |
\begin{description} |
|
248 |
\item[generators] @{text"p \<leftarrow> xs"}, |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
249 |
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:
24471
diff
changeset
|
250 |
\item[guards] @{text"b"}, where @{text b} is a boolean expression. |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
251 |
%\item[local bindings] @ {text"let x = e"}. |
24349 | 252 |
\end{description} |
23240 | 253 |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
254 |
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:
24471
diff
changeset
|
255 |
misunderstandings, the translation into desugared form is not reversed |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
256 |
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:
24471
diff
changeset
|
257 |
optmized to @{term"map (%x. e) xs"}. |
23240 | 258 |
|
24349 | 259 |
It is easy to write short list comprehensions which stand for complex |
260 |
expressions. During proofs, they may become unreadable (and |
|
261 |
mangled). In such cases it can be advisable to introduce separate |
|
262 |
definitions for the list comprehensions in question. *} |
|
263 |
||
23209 | 264 |
(* |
23240 | 265 |
Proper theorem proving support would be nice. For example, if |
23192 | 266 |
@{text"set[f x y. x \<leftarrow> xs, y \<leftarrow> ys, P x y]"} |
267 |
produced something like |
|
23209 | 268 |
@{term"{z. EX x: set xs. EX y:set ys. P x y \<and> z = f x y}"}. |
269 |
*) |
|
270 |
||
23240 | 271 |
nonterminals lc_qual lc_quals |
23192 | 272 |
|
273 |
syntax |
|
23240 | 274 |
"_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list" ("[_ . __") |
24349 | 275 |
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ <- _") |
23240 | 276 |
"_lc_test" :: "bool \<Rightarrow> lc_qual" ("_") |
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
277 |
(*"_lc_let" :: "letbinds => lc_qual" ("let _")*) |
23240 | 278 |
"_lc_end" :: "lc_quals" ("]") |
279 |
"_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __") |
|
24349 | 280 |
"_lc_abs" :: "'a => 'b list => 'b list" |
23192 | 281 |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
282 |
(* These are easier than ML code but cannot express the optimized |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
283 |
translation of [e. p<-xs] |
23192 | 284 |
translations |
24349 | 285 |
"[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)" |
23240 | 286 |
"_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)" |
24349 | 287 |
=> "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)" |
23240 | 288 |
"[e. P]" => "if P then [e] else []" |
289 |
"_listcompr e (_lc_test P) (_lc_quals Q Qs)" |
|
290 |
=> "if P then (_listcompr e Q Qs) else []" |
|
24349 | 291 |
"_listcompr e (_lc_let b) (_lc_quals Q Qs)" |
292 |
=> "_Let b (_listcompr e Q Qs)" |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
293 |
*) |
23240 | 294 |
|
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
295 |
syntax (xsymbols) |
24349 | 296 |
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _") |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
297 |
syntax (HTML output) |
24349 | 298 |
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _") |
299 |
||
300 |
parse_translation (advanced) {* |
|
301 |
let |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
302 |
val NilC = Syntax.const @{const_name Nil}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
303 |
val ConsC = Syntax.const @{const_name Cons}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
304 |
val mapC = Syntax.const @{const_name map}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
305 |
val concatC = Syntax.const @{const_name concat}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
306 |
val IfC = Syntax.const @{const_name If}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
307 |
fun singl x = ConsC $ x $ NilC; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
308 |
|
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
309 |
fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *) |
24349 | 310 |
let |
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
311 |
val x = Free (Name.variant (add_term_free_names (p$e, [])) "x", dummyT); |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
312 |
val e = if opti then singl e else e; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
313 |
val case1 = Syntax.const "_case1" $ p $ e; |
24349 | 314 |
val case2 = Syntax.const "_case1" $ Syntax.const Term.dummy_patternN |
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
315 |
$ NilC; |
24349 | 316 |
val cs = Syntax.const "_case2" $ case1 $ case2 |
317 |
val ft = DatatypeCase.case_tr false DatatypePackage.datatype_of_constr |
|
318 |
ctxt [x, cs] |
|
319 |
in lambda x ft end; |
|
320 |
||
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
321 |
fun abs_tr ctxt (p as Free(s,T)) e opti = |
24349 | 322 |
let val thy = ProofContext.theory_of ctxt; |
323 |
val s' = Sign.intern_const thy s |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
324 |
in if Sign.declared_const thy s' |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
325 |
then (pat_tr ctxt p e opti, false) |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
326 |
else (lambda p e, true) |
24349 | 327 |
end |
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
328 |
| 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:
24471
diff
changeset
|
329 |
|
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
330 |
fun lc_tr ctxt [e, Const("_lc_test",_)$b, qs] = |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
331 |
let val res = case qs of Const("_lc_end",_) => singl e |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
332 |
| Const("_lc_quals",_)$q$qs => lc_tr ctxt [e,q,qs]; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
333 |
in IfC $ b $ res $ NilC end |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
334 |
| 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:
24471
diff
changeset
|
335 |
(case abs_tr ctxt p e true of |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
336 |
(f,true) => mapC $ f $ es |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
337 |
| (f, false) => concatC $ (mapC $ f $ es)) |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
338 |
| 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:
24471
diff
changeset
|
339 |
let val e' = lc_tr ctxt [e,q,qs]; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
340 |
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:
24471
diff
changeset
|
341 |
|
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
342 |
in [("_listcompr", lc_tr)] end |
24349 | 343 |
*} |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
344 |
|
23240 | 345 |
(* |
346 |
term "[(x,y,z). b]" |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
347 |
term "[(x,y,z). x\<leftarrow>xs]" |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
348 |
term "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]" |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
349 |
term "[(x,y,z). x<a, x>b]" |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
350 |
term "[(x,y,z). x\<leftarrow>xs, x>b]" |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
351 |
term "[(x,y,z). x<a, x\<leftarrow>xs]" |
24349 | 352 |
term "[(x,y). Cons True x \<leftarrow> xs]" |
353 |
term "[(x,y,z). Cons x [] \<leftarrow> xs]" |
|
23240 | 354 |
term "[(x,y,z). x<a, x>b, x=d]" |
355 |
term "[(x,y,z). x<a, x>b, y\<leftarrow>ys]" |
|
356 |
term "[(x,y,z). x<a, x\<leftarrow>xs,y>b]" |
|
357 |
term "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]" |
|
358 |
term "[(x,y,z). x\<leftarrow>xs, x>b, y<a]" |
|
359 |
term "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]" |
|
360 |
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]" |
|
361 |
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]" |
|
24349 | 362 |
term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]" |
23192 | 363 |
*) |
364 |
||
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
365 |
subsubsection {* @{const Nil} and @{const Cons} *} |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
366 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
367 |
lemma not_Cons_self [simp]: |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
368 |
"xs \<noteq> x # xs" |
13145 | 369 |
by (induct xs) auto |
13114 | 370 |
|
13142 | 371 |
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric] |
13114 | 372 |
|
13142 | 373 |
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" |
13145 | 374 |
by (induct xs) auto |
13114 | 375 |
|
13142 | 376 |
lemma length_induct: |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
377 |
"(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs" |
17589 | 378 |
by (rule measure_induct [of length]) iprover |
13114 | 379 |
|
380 |
||
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
381 |
subsubsection {* @{const length} *} |
13114 | 382 |
|
13142 | 383 |
text {* |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
384 |
Needs to come before @{text "@"} because of theorem @{text |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
385 |
append_eq_append_conv}. |
13142 | 386 |
*} |
13114 | 387 |
|
13142 | 388 |
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" |
13145 | 389 |
by (induct xs) auto |
13114 | 390 |
|
13142 | 391 |
lemma length_map [simp]: "length (map f xs) = length xs" |
13145 | 392 |
by (induct xs) auto |
13114 | 393 |
|
13142 | 394 |
lemma length_rev [simp]: "length (rev xs) = length xs" |
13145 | 395 |
by (induct xs) auto |
13114 | 396 |
|
13142 | 397 |
lemma length_tl [simp]: "length (tl xs) = length xs - 1" |
13145 | 398 |
by (cases xs) auto |
13114 | 399 |
|
13142 | 400 |
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" |
13145 | 401 |
by (induct xs) auto |
13114 | 402 |
|
13142 | 403 |
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" |
13145 | 404 |
by (induct xs) auto |
13114 | 405 |
|
23479 | 406 |
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0" |
407 |
by auto |
|
408 |
||
13114 | 409 |
lemma length_Suc_conv: |
13145 | 410 |
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
411 |
by (induct xs) auto |
|
13142 | 412 |
|
14025 | 413 |
lemma Suc_length_conv: |
414 |
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
|
14208 | 415 |
apply (induct xs, simp, simp) |
14025 | 416 |
apply blast |
417 |
done |
|
418 |
||
14099 | 419 |
lemma impossible_Cons [rule_format]: |
420 |
"length xs <= length ys --> xs = x # ys = False" |
|
20503 | 421 |
apply (induct xs) |
422 |
apply auto |
|
14099 | 423 |
done |
424 |
||
24526 | 425 |
lemma list_induct2[consumes 1]: |
426 |
"\<lbrakk> length xs = length ys; |
|
14247 | 427 |
P [] []; |
428 |
\<And>x xs y ys. \<lbrakk> length xs = length ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk> |
|
429 |
\<Longrightarrow> P xs ys" |
|
24526 | 430 |
apply(induct xs arbitrary: ys) |
14247 | 431 |
apply simp |
432 |
apply(case_tac ys) |
|
433 |
apply simp |
|
434 |
apply(simp) |
|
435 |
done |
|
13114 | 436 |
|
22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
437 |
lemma list_induct2': |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
438 |
"\<lbrakk> P [] []; |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
439 |
\<And>x xs. P (x#xs) []; |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
440 |
\<And>y ys. P [] (y#ys); |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
441 |
\<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:
22422
diff
changeset
|
442 |
\<Longrightarrow> P xs ys" |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
443 |
by (induct xs arbitrary: ys) (case_tac x, auto)+ |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
444 |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
445 |
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False" |
24349 | 446 |
by (rule Eq_FalseI) auto |
24037 | 447 |
|
448 |
simproc_setup list_neq ("(xs::'a list) = ys") = {* |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
449 |
(* |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
450 |
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:
21911
diff
changeset
|
451 |
This is the case if the atomic sublists of one are a submultiset |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
452 |
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:
21911
diff
changeset
|
453 |
*) |
24037 | 454 |
|
455 |
let |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
456 |
|
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
457 |
fun len (Const("List.list.Nil",_)) acc = acc |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
458 |
| len (Const("List.list.Cons",_) $ _ $ xs) (ts,n) = len xs (ts,n+1) |
23029 | 459 |
| len (Const("List.append",_) $ xs $ ys) acc = len xs (len ys acc) |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
460 |
| len (Const("List.rev",_) $ xs) acc = len xs acc |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
461 |
| len (Const("List.map",_) $ _ $ xs) acc = len xs acc |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
462 |
| len t (ts,n) = (t::ts,n); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
463 |
|
24037 | 464 |
fun list_neq _ ss ct = |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
465 |
let |
24037 | 466 |
val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct; |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
467 |
val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
468 |
fun prove_neq() = |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
469 |
let |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
470 |
val Type(_,listT::_) = eqT; |
22994 | 471 |
val size = HOLogic.size_const listT; |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
472 |
val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
473 |
val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
474 |
val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len |
22633 | 475 |
(K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1)); |
476 |
in SOME (thm RS @{thm neq_if_length_neq}) end |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
477 |
in |
23214 | 478 |
if m < n andalso submultiset (op aconv) (ls,rs) orelse |
479 |
n < m andalso submultiset (op aconv) (rs,ls) |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
480 |
then prove_neq() else NONE |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
481 |
end; |
24037 | 482 |
in list_neq end; |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
483 |
*} |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
484 |
|
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
485 |
|
15392 | 486 |
subsubsection {* @{text "@"} -- append *} |
13114 | 487 |
|
13142 | 488 |
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" |
13145 | 489 |
by (induct xs) auto |
13114 | 490 |
|
13142 | 491 |
lemma append_Nil2 [simp]: "xs @ [] = xs" |
13145 | 492 |
by (induct xs) auto |
3507 | 493 |
|
24449 | 494 |
interpretation semigroup_append: semigroup_add ["op @"] |
495 |
by unfold_locales simp |
|
496 |
interpretation monoid_append: monoid_add ["[]" "op @"] |
|
497 |
by unfold_locales (simp+) |
|
498 |
||
13142 | 499 |
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" |
13145 | 500 |
by (induct xs) auto |
13114 | 501 |
|
13142 | 502 |
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" |
13145 | 503 |
by (induct xs) auto |
13114 | 504 |
|
13142 | 505 |
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" |
13145 | 506 |
by (induct xs) auto |
13114 | 507 |
|
13142 | 508 |
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" |
13145 | 509 |
by (induct xs) auto |
13114 | 510 |
|
24286
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
511 |
lemma append_eq_append_conv [simp,noatp]: |
24526 | 512 |
"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:
13863
diff
changeset
|
513 |
==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)" |
24526 | 514 |
apply (induct xs arbitrary: ys) |
14208 | 515 |
apply (case_tac ys, simp, force) |
516 |
apply (case_tac ys, force, simp) |
|
13145 | 517 |
done |
13142 | 518 |
|
24526 | 519 |
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) = |
520 |
(EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)" |
|
521 |
apply (induct xs arbitrary: ys zs ts) |
|
14495 | 522 |
apply fastsimp |
523 |
apply(case_tac zs) |
|
524 |
apply simp |
|
525 |
apply fastsimp |
|
526 |
done |
|
527 |
||
13142 | 528 |
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)" |
13145 | 529 |
by simp |
13142 | 530 |
|
531 |
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" |
|
13145 | 532 |
by simp |
13114 | 533 |
|
13142 | 534 |
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)" |
13145 | 535 |
by simp |
13114 | 536 |
|
13142 | 537 |
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" |
13145 | 538 |
using append_same_eq [of _ _ "[]"] by auto |
3507 | 539 |
|
13142 | 540 |
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" |
13145 | 541 |
using append_same_eq [of "[]"] by auto |
13114 | 542 |
|
24286
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
543 |
lemma hd_Cons_tl [simp,noatp]: "xs \<noteq> [] ==> hd xs # tl xs = xs" |
13145 | 544 |
by (induct xs) auto |
13114 | 545 |
|
13142 | 546 |
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" |
13145 | 547 |
by (induct xs) auto |
13114 | 548 |
|
13142 | 549 |
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" |
13145 | 550 |
by (simp add: hd_append split: list.split) |
13114 | 551 |
|
13142 | 552 |
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)" |
13145 | 553 |
by (simp split: list.split) |
13114 | 554 |
|
13142 | 555 |
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" |
13145 | 556 |
by (simp add: tl_append split: list.split) |
13114 | 557 |
|
558 |
||
14300 | 559 |
lemma Cons_eq_append_conv: "x#xs = ys@zs = |
560 |
(ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))" |
|
561 |
by(cases ys) auto |
|
562 |
||
15281 | 563 |
lemma append_eq_Cons_conv: "(ys@zs = x#xs) = |
564 |
(ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))" |
|
565 |
by(cases ys) auto |
|
566 |
||
14300 | 567 |
|
13142 | 568 |
text {* Trivial rules for solving @{text "@"}-equations automatically. *} |
13114 | 569 |
|
570 |
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" |
|
13145 | 571 |
by simp |
13114 | 572 |
|
13142 | 573 |
lemma Cons_eq_appendI: |
13145 | 574 |
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs" |
575 |
by (drule sym) simp |
|
13114 | 576 |
|
13142 | 577 |
lemma append_eq_appendI: |
13145 | 578 |
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us" |
579 |
by (drule sym) simp |
|
13114 | 580 |
|
581 |
||
13142 | 582 |
text {* |
13145 | 583 |
Simplification procedure for all list equalities. |
584 |
Currently only tries to rearrange @{text "@"} to see if |
|
585 |
- both lists end in a singleton list, |
|
586 |
- or both lists end in the same list. |
|
13142 | 587 |
*} |
588 |
||
589 |
ML_setup {* |
|
3507 | 590 |
local |
591 |
||
13114 | 592 |
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) = |
13462 | 593 |
(case xs of Const("List.list.Nil",_) => cons | _ => last xs) |
23029 | 594 |
| last (Const("List.append",_) $ _ $ ys) = last ys |
13462 | 595 |
| last t = t; |
13114 | 596 |
|
597 |
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true |
|
13462 | 598 |
| list1 _ = false; |
13114 | 599 |
|
600 |
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) = |
|
13462 | 601 |
(case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs) |
23029 | 602 |
| butlast ((app as Const("List.append",_) $ xs) $ ys) = app $ butlast ys |
13462 | 603 |
| butlast xs = Const("List.list.Nil",fastype_of xs); |
13114 | 604 |
|
22633 | 605 |
val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc}, |
606 |
@{thm append_Nil}, @{thm append_Cons}]; |
|
16973 | 607 |
|
20044
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
wenzelm
parents:
19890
diff
changeset
|
608 |
fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) = |
13462 | 609 |
let |
610 |
val lastl = last lhs and lastr = last rhs; |
|
611 |
fun rearr conv = |
|
612 |
let |
|
613 |
val lhs1 = butlast lhs and rhs1 = butlast rhs; |
|
614 |
val Type(_,listT::_) = eqT |
|
615 |
val appT = [listT,listT] ---> listT |
|
23029 | 616 |
val app = Const("List.append",appT) |
13462 | 617 |
val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr) |
13480
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
618 |
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); |
20044
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
wenzelm
parents:
19890
diff
changeset
|
619 |
val thm = Goal.prove (Simplifier.the_context ss) [] [] eq |
17877
67d5ab1cb0d8
Simplifier.inherit_context instead of Simplifier.inherit_bounds;
wenzelm
parents:
17830
diff
changeset
|
620 |
(K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1)); |
15531 | 621 |
in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; |
13114 | 622 |
|
13462 | 623 |
in |
22633 | 624 |
if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv} |
625 |
else if lastl aconv lastr then rearr @{thm append_same_eq} |
|
15531 | 626 |
else NONE |
13462 | 627 |
end; |
628 |
||
13114 | 629 |
in |
13462 | 630 |
|
631 |
val list_eq_simproc = |
|
22633 | 632 |
Simplifier.simproc @{theory} "list_eq" ["(xs::'a list) = ys"] (K list_eq); |
13462 | 633 |
|
13114 | 634 |
end; |
635 |
||
636 |
Addsimprocs [list_eq_simproc]; |
|
637 |
*} |
|
638 |
||
639 |
||
15392 | 640 |
subsubsection {* @{text map} *} |
13114 | 641 |
|
13142 | 642 |
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs" |
13145 | 643 |
by (induct xs) simp_all |
13114 | 644 |
|
13142 | 645 |
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" |
13145 | 646 |
by (rule ext, induct_tac xs) auto |
13114 | 647 |
|
13142 | 648 |
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" |
13145 | 649 |
by (induct xs) auto |
13114 | 650 |
|
13142 | 651 |
lemma map_compose: "map (f o g) xs = map f (map g xs)" |
13145 | 652 |
by (induct xs) (auto simp add: o_def) |
13114 | 653 |
|
13142 | 654 |
lemma rev_map: "rev (map f xs) = map f (rev xs)" |
13145 | 655 |
by (induct xs) auto |
13114 | 656 |
|
13737 | 657 |
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)" |
658 |
by (induct xs) auto |
|
659 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset
|
660 |
lemma map_cong [fundef_cong, recdef_cong]: |
13145 | 661 |
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys" |
662 |
-- {* a congruence rule for @{text map} *} |
|
13737 | 663 |
by simp |
13114 | 664 |
|
13142 | 665 |
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" |
13145 | 666 |
by (cases xs) auto |
13114 | 667 |
|
13142 | 668 |
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" |
13145 | 669 |
by (cases xs) auto |
13114 | 670 |
|
18447 | 671 |
lemma map_eq_Cons_conv: |
14025 | 672 |
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" |
13145 | 673 |
by (cases xs) auto |
13114 | 674 |
|
18447 | 675 |
lemma Cons_eq_map_conv: |
14025 | 676 |
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" |
677 |
by (cases ys) auto |
|
678 |
||
18447 | 679 |
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] |
680 |
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] |
|
681 |
declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] |
|
682 |
||
14111 | 683 |
lemma ex_map_conv: |
684 |
"(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)" |
|
18447 | 685 |
by(induct ys, auto simp add: Cons_eq_map_conv) |
14111 | 686 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
687 |
lemma map_eq_imp_length_eq: |
24526 | 688 |
"map f xs = map f ys ==> length xs = length ys" |
689 |
apply (induct ys arbitrary: xs) |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
690 |
apply simp |
24632 | 691 |
apply (metis Suc_length_conv length_map) |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
692 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
693 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
694 |
lemma map_inj_on: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
695 |
"[| 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:
15072
diff
changeset
|
696 |
==> xs = ys" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
697 |
apply(frule map_eq_imp_length_eq) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
698 |
apply(rotate_tac -1) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
699 |
apply(induct rule:list_induct2) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
700 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
701 |
apply(simp) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
702 |
apply (blast intro:sym) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
703 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
704 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
705 |
lemma inj_on_map_eq_map: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
706 |
"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:
15072
diff
changeset
|
707 |
by(blast dest:map_inj_on) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
708 |
|
13114 | 709 |
lemma map_injective: |
24526 | 710 |
"map f xs = map f ys ==> inj f ==> xs = ys" |
711 |
by (induct ys arbitrary: xs) (auto dest!:injD) |
|
13114 | 712 |
|
14339 | 713 |
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" |
714 |
by(blast dest:map_injective) |
|
715 |
||
13114 | 716 |
lemma inj_mapI: "inj f ==> inj (map f)" |
17589 | 717 |
by (iprover dest: map_injective injD intro: inj_onI) |
13114 | 718 |
|
719 |
lemma inj_mapD: "inj (map f) ==> inj f" |
|
14208 | 720 |
apply (unfold inj_on_def, clarify) |
13145 | 721 |
apply (erule_tac x = "[x]" in ballE) |
14208 | 722 |
apply (erule_tac x = "[y]" in ballE, simp, blast) |
13145 | 723 |
apply blast |
724 |
done |
|
13114 | 725 |
|
14339 | 726 |
lemma inj_map[iff]: "inj (map f) = inj f" |
13145 | 727 |
by (blast dest: inj_mapD intro: inj_mapI) |
13114 | 728 |
|
15303 | 729 |
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" |
730 |
apply(rule inj_onI) |
|
731 |
apply(erule map_inj_on) |
|
732 |
apply(blast intro:inj_onI dest:inj_onD) |
|
733 |
done |
|
734 |
||
14343 | 735 |
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" |
736 |
by (induct xs, auto) |
|
13114 | 737 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
738 |
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:
14395
diff
changeset
|
739 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
740 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
741 |
lemma map_fst_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
742 |
"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:
15072
diff
changeset
|
743 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
744 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
745 |
lemma map_snd_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
746 |
"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:
15072
diff
changeset
|
747 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
748 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
749 |
|
15392 | 750 |
subsubsection {* @{text rev} *} |
13114 | 751 |
|
13142 | 752 |
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" |
13145 | 753 |
by (induct xs) auto |
13114 | 754 |
|
13142 | 755 |
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" |
13145 | 756 |
by (induct xs) auto |
13114 | 757 |
|
15870 | 758 |
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" |
759 |
by auto |
|
760 |
||
13142 | 761 |
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" |
13145 | 762 |
by (induct xs) auto |
13114 | 763 |
|
13142 | 764 |
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" |
13145 | 765 |
by (induct xs) auto |
13114 | 766 |
|
15870 | 767 |
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" |
768 |
by (cases xs) auto |
|
769 |
||
770 |
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])" |
|
771 |
by (cases xs) auto |
|
772 |
||
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
773 |
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
774 |
apply (induct xs arbitrary: ys, force) |
14208 | 775 |
apply (case_tac ys, simp, force) |
13145 | 776 |
done |
13114 | 777 |
|
15439 | 778 |
lemma inj_on_rev[iff]: "inj_on rev A" |
779 |
by(simp add:inj_on_def) |
|
780 |
||
13366 | 781 |
lemma rev_induct [case_names Nil snoc]: |
782 |
"[| 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:
15439
diff
changeset
|
783 |
apply(simplesubst rev_rev_ident[symmetric]) |
13145 | 784 |
apply(rule_tac list = "rev xs" in list.induct, simp_all) |
785 |
done |
|
13114 | 786 |
|
13145 | 787 |
ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility" |
13114 | 788 |
|
13366 | 789 |
lemma rev_exhaust [case_names Nil snoc]: |
790 |
"(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" |
|
13145 | 791 |
by (induct xs rule: rev_induct) auto |
13114 | 792 |
|
13366 | 793 |
lemmas rev_cases = rev_exhaust |
794 |
||
18423 | 795 |
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])" |
796 |
by(rule rev_cases[of xs]) auto |
|
797 |
||
13114 | 798 |
|
15392 | 799 |
subsubsection {* @{text set} *} |
13114 | 800 |
|
13142 | 801 |
lemma finite_set [iff]: "finite (set xs)" |
13145 | 802 |
by (induct xs) auto |
13114 | 803 |
|
13142 | 804 |
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" |
13145 | 805 |
by (induct xs) auto |
13114 | 806 |
|
17830 | 807 |
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs" |
808 |
by(cases xs) auto |
|
14099 | 809 |
|
13142 | 810 |
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" |
13145 | 811 |
by auto |
13114 | 812 |
|
14099 | 813 |
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" |
814 |
by auto |
|
815 |
||
13142 | 816 |
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" |
13145 | 817 |
by (induct xs) auto |
13114 | 818 |
|
15245 | 819 |
lemma set_empty2[iff]: "({} = set xs) = (xs = [])" |
820 |
by(induct xs) auto |
|
821 |
||
13142 | 822 |
lemma set_rev [simp]: "set (rev xs) = set xs" |
13145 | 823 |
by (induct xs) auto |
13114 | 824 |
|
13142 | 825 |
lemma set_map [simp]: "set (map f xs) = f`(set xs)" |
13145 | 826 |
by (induct xs) auto |
13114 | 827 |
|
13142 | 828 |
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}" |
13145 | 829 |
by (induct xs) auto |
13114 | 830 |
|
15425 | 831 |
lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}" |
14208 | 832 |
apply (induct j, simp_all) |
833 |
apply (erule ssubst, auto) |
|
13145 | 834 |
done |
13114 | 835 |
|
13142 | 836 |
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)" |
15113 | 837 |
proof (induct xs) |
838 |
case Nil show ?case by simp |
|
839 |
case (Cons a xs) |
|
840 |
show ?case |
|
841 |
proof |
|
842 |
assume "x \<in> set (a # xs)" |
|
843 |
with prems show "\<exists>ys zs. a # xs = ys @ x # zs" |
|
844 |
by (simp, blast intro: Cons_eq_appendI) |
|
845 |
next |
|
846 |
assume "\<exists>ys zs. a # xs = ys @ x # zs" |
|
847 |
then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast |
|
848 |
show "x \<in> set (a # xs)" |
|
849 |
by (cases ys, auto simp add: eq) |
|
850 |
qed |
|
851 |
qed |
|
13142 | 852 |
|
18049 | 853 |
lemma in_set_conv_decomp_first: |
854 |
"(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)" |
|
855 |
proof (induct xs) |
|
856 |
case Nil show ?case by simp |
|
857 |
next |
|
858 |
case (Cons a xs) |
|
859 |
show ?case |
|
860 |
proof cases |
|
861 |
assume "x = a" thus ?case using Cons by force |
|
862 |
next |
|
863 |
assume "x \<noteq> a" |
|
864 |
show ?case |
|
865 |
proof |
|
866 |
assume "x \<in> set (a # xs)" |
|
867 |
from prems show "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys" |
|
868 |
by(fastsimp intro!: Cons_eq_appendI) |
|
869 |
next |
|
870 |
assume "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys" |
|
871 |
then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast |
|
872 |
show "x \<in> set (a # xs)" by (cases ys, auto simp add: eq) |
|
873 |
qed |
|
874 |
qed |
|
875 |
qed |
|
876 |
||
877 |
lemmas split_list = in_set_conv_decomp[THEN iffD1, standard] |
|
878 |
lemmas split_list_first = in_set_conv_decomp_first[THEN iffD1, standard] |
|
879 |
||
880 |
||
13508 | 881 |
lemma finite_list: "finite A ==> EX l. set l = A" |
882 |
apply (erule finite_induct, auto) |
|
883 |
apply (rule_tac x="x#l" in exI, auto) |
|
884 |
done |
|
885 |
||
14388 | 886 |
lemma card_length: "card (set xs) \<le> length xs" |
887 |
by (induct xs) (auto simp add: card_insert_if) |
|
13114 | 888 |
|
15168 | 889 |
|
15392 | 890 |
subsubsection {* @{text filter} *} |
13114 | 891 |
|
13142 | 892 |
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" |
13145 | 893 |
by (induct xs) auto |
13114 | 894 |
|
15305 | 895 |
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" |
896 |
by (induct xs) simp_all |
|
897 |
||
13142 | 898 |
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" |
13145 | 899 |
by (induct xs) auto |
13114 | 900 |
|
16998 | 901 |
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" |
902 |
by (induct xs) (auto simp add: le_SucI) |
|
903 |
||
18423 | 904 |
lemma sum_length_filter_compl: |
905 |
"length(filter P xs) + length(filter (%x. ~P x) xs) = length xs" |
|
906 |
by(induct xs) simp_all |
|
907 |
||
13142 | 908 |
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" |
13145 | 909 |
by (induct xs) auto |
13114 | 910 |
|
13142 | 911 |
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" |
13145 | 912 |
by (induct xs) auto |
13114 | 913 |
|
16998 | 914 |
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" |
24349 | 915 |
by (induct xs) simp_all |
16998 | 916 |
|
917 |
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" |
|
918 |
apply (induct xs) |
|
919 |
apply auto |
|
920 |
apply(cut_tac P=P and xs=xs in length_filter_le) |
|
921 |
apply simp |
|
922 |
done |
|
13114 | 923 |
|
16965 | 924 |
lemma filter_map: |
925 |
"filter P (map f xs) = map f (filter (P o f) xs)" |
|
926 |
by (induct xs) simp_all |
|
927 |
||
928 |
lemma length_filter_map[simp]: |
|
929 |
"length (filter P (map f xs)) = length(filter (P o f) xs)" |
|
930 |
by (simp add:filter_map) |
|
931 |
||
13142 | 932 |
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" |
13145 | 933 |
by auto |
13114 | 934 |
|
15246 | 935 |
lemma length_filter_less: |
936 |
"\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" |
|
937 |
proof (induct xs) |
|
938 |
case Nil thus ?case by simp |
|
939 |
next |
|
940 |
case (Cons x xs) thus ?case |
|
941 |
apply (auto split:split_if_asm) |
|
942 |
using length_filter_le[of P xs] apply arith |
|
943 |
done |
|
944 |
qed |
|
13114 | 945 |
|
15281 | 946 |
lemma length_filter_conv_card: |
947 |
"length(filter p xs) = card{i. i < length xs & p(xs!i)}" |
|
948 |
proof (induct xs) |
|
949 |
case Nil thus ?case by simp |
|
950 |
next |
|
951 |
case (Cons x xs) |
|
952 |
let ?S = "{i. i < length xs & p(xs!i)}" |
|
953 |
have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) |
|
954 |
show ?case (is "?l = card ?S'") |
|
955 |
proof (cases) |
|
956 |
assume "p x" |
|
957 |
hence eq: "?S' = insert 0 (Suc ` ?S)" |
|
958 |
by(auto simp add: nth_Cons image_def split:nat.split elim:lessE) |
|
959 |
have "length (filter p (x # xs)) = Suc(card ?S)" |
|
23388 | 960 |
using Cons `p x` by simp |
15281 | 961 |
also have "\<dots> = Suc(card(Suc ` ?S))" using fin |
962 |
by (simp add: card_image inj_Suc) |
|
963 |
also have "\<dots> = card ?S'" using eq fin |
|
964 |
by (simp add:card_insert_if) (simp add:image_def) |
|
965 |
finally show ?thesis . |
|
966 |
next |
|
967 |
assume "\<not> p x" |
|
968 |
hence eq: "?S' = Suc ` ?S" |
|
969 |
by(auto simp add: nth_Cons image_def split:nat.split elim:lessE) |
|
970 |
have "length (filter p (x # xs)) = card ?S" |
|
23388 | 971 |
using Cons `\<not> p x` by simp |
15281 | 972 |
also have "\<dots> = card(Suc ` ?S)" using fin |
973 |
by (simp add: card_image inj_Suc) |
|
974 |
also have "\<dots> = card ?S'" using eq fin |
|
975 |
by (simp add:card_insert_if) |
|
976 |
finally show ?thesis . |
|
977 |
qed |
|
978 |
qed |
|
979 |
||
17629 | 980 |
lemma Cons_eq_filterD: |
981 |
"x#xs = filter P ys \<Longrightarrow> |
|
982 |
\<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 | 983 |
(is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs") |
17629 | 984 |
proof(induct ys) |
985 |
case Nil thus ?case by simp |
|
986 |
next |
|
987 |
case (Cons y ys) |
|
988 |
show ?case (is "\<exists>x. ?Q x") |
|
989 |
proof cases |
|
990 |
assume Py: "P y" |
|
991 |
show ?thesis |
|
992 |
proof cases |
|
993 |
assume xy: "x = y" |
|
994 |
show ?thesis |
|
995 |
proof from Py xy Cons(2) show "?Q []" by simp qed |
|
996 |
next |
|
997 |
assume "x \<noteq> y" with Py Cons(2) show ?thesis by simp |
|
998 |
qed |
|
999 |
next |
|
1000 |
assume Py: "\<not> P y" |
|
1001 |
with Cons obtain us vs where 1 : "?P (y#ys) (y#us) vs" by fastsimp |
|
1002 |
show ?thesis (is "? us. ?Q us") |
|
1003 |
proof show "?Q (y#us)" using 1 by simp qed |
|
1004 |
qed |
|
1005 |
qed |
|
1006 |
||
1007 |
lemma filter_eq_ConsD: |
|
1008 |
"filter P ys = x#xs \<Longrightarrow> |
|
1009 |
\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" |
|
1010 |
by(rule Cons_eq_filterD) simp |
|
1011 |
||
1012 |
lemma filter_eq_Cons_iff: |
|
1013 |
"(filter P ys = x#xs) = |
|
1014 |
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" |
|
1015 |
by(auto dest:filter_eq_ConsD) |
|
1016 |
||
1017 |
lemma Cons_eq_filter_iff: |
|
1018 |
"(x#xs = filter P ys) = |
|
1019 |
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" |
|
1020 |
by(auto dest:Cons_eq_filterD) |
|
1021 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset
|
1022 |
lemma filter_cong[fundef_cong, recdef_cong]: |
17501 | 1023 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" |
1024 |
apply simp |
|
1025 |
apply(erule thin_rl) |
|
1026 |
by (induct ys) simp_all |
|
1027 |
||
15281 | 1028 |
|
15392 | 1029 |
subsubsection {* @{text concat} *} |
13114 | 1030 |
|
13142 | 1031 |
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" |
13145 | 1032 |
by (induct xs) auto |
13114 | 1033 |
|
18447 | 1034 |
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 1035 |
by (induct xss) auto |
13114 | 1036 |
|
18447 | 1037 |
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 1038 |
by (induct xss) auto |
13114 | 1039 |
|
24308 | 1040 |
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)" |
13145 | 1041 |
by (induct xs) auto |
13114 | 1042 |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
1043 |
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs" |
24349 | 1044 |
by (induct xs) auto |
1045 |
||
13142 | 1046 |
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" |
13145 | 1047 |
by (induct xs) auto |
13114 | 1048 |
|
13142 | 1049 |
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" |
13145 | 1050 |
by (induct xs) auto |
13114 | 1051 |
|
13142 | 1052 |
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" |
13145 | 1053 |
by (induct xs) auto |
13114 | 1054 |
|
1055 |
||
15392 | 1056 |
subsubsection {* @{text nth} *} |
13114 | 1057 |
|
13142 | 1058 |
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x" |
13145 | 1059 |
by auto |
13114 | 1060 |
|
13142 | 1061 |
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n" |
13145 | 1062 |
by auto |
13114 | 1063 |
|
13142 | 1064 |
declare nth.simps [simp del] |
13114 | 1065 |
|
1066 |
lemma nth_append: |
|
24526 | 1067 |
"(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" |
1068 |
apply (induct xs arbitrary: n, simp) |
|
14208 | 1069 |
apply (case_tac n, auto) |
13145 | 1070 |
done |
13114 | 1071 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1072 |
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x" |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1073 |
by (induct "xs") auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1074 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1075 |
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n" |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1076 |
by (induct "xs") auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1077 |
|
24526 | 1078 |
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)" |
1079 |
apply (induct xs arbitrary: n, simp) |
|
14208 | 1080 |
apply (case_tac n, auto) |
13145 | 1081 |
done |
13114 | 1082 |
|
18423 | 1083 |
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0" |
1084 |
by(cases xs) simp_all |
|
1085 |
||
18049 | 1086 |
|
1087 |
lemma list_eq_iff_nth_eq: |
|
24526 | 1088 |
"(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))" |
1089 |
apply(induct xs arbitrary: ys) |
|
24632 | 1090 |
apply force |
18049 | 1091 |
apply(case_tac ys) |
1092 |
apply simp |
|
1093 |
apply(simp add:nth_Cons split:nat.split)apply blast |
|
1094 |
done |
|
1095 |
||
13142 | 1096 |
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}" |
15251 | 1097 |
apply (induct xs, simp, simp) |
13145 | 1098 |
apply safe |
24632 | 1099 |
apply (metis nat_case_0 nth.simps zero_less_Suc) |
1100 |
apply (metis less_Suc_eq_0_disj nth_Cons_Suc) |
|
14208 | 1101 |
apply (case_tac i, simp) |
24632 | 1102 |
apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff) |
13145 | 1103 |
done |
13114 | 1104 |
|
17501 | 1105 |
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" |
1106 |
by(auto simp:set_conv_nth) |
|
1107 |
||
13145 | 1108 |
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)" |
1109 |
by (auto simp add: set_conv_nth) |
|
13114 | 1110 |
|
13142 | 1111 |
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs" |
13145 | 1112 |
by (auto simp add: set_conv_nth) |
13114 | 1113 |
|
1114 |
lemma all_nth_imp_all_set: |
|
13145 | 1115 |
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x" |
1116 |
by (auto simp add: set_conv_nth) |
|
13114 | 1117 |
|
1118 |
lemma all_set_conv_all_nth: |
|
13145 | 1119 |
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))" |
1120 |
by (auto simp add: set_conv_nth) |
|
13114 | 1121 |
|
1122 |
||
15392 | 1123 |
subsubsection {* @{text list_update} *} |
13114 | 1124 |
|
24526 | 1125 |
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs" |
1126 |
by (induct xs arbitrary: i) (auto split: nat.split) |
|
13114 | 1127 |
|
1128 |
lemma nth_list_update: |
|
24526 | 1129 |
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" |
1130 |
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) |
|
13114 | 1131 |
|
13142 | 1132 |
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" |
13145 | 1133 |
by (simp add: nth_list_update) |
13114 | 1134 |
|
24526 | 1135 |
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j" |
1136 |
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) |
|
13114 | 1137 |
|
13142 | 1138 |
lemma list_update_overwrite [simp]: |
24526 | 1139 |
"i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]" |
1140 |
by (induct xs arbitrary: i) (auto split: nat.split) |
|
1141 |
||
1142 |
lemma list_update_id[simp]: "xs[i := xs!i] = xs" |
|
1143 |
by (induct xs arbitrary: i) (simp_all split:nat.splits) |
|
1144 |
||
1145 |
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" |
|
1146 |
apply (induct xs arbitrary: i) |
|
17501 | 1147 |
apply simp |
1148 |
apply (case_tac i) |
|
1149 |
apply simp_all |
|
1150 |
done |
|
1151 |
||
13114 | 1152 |
lemma list_update_same_conv: |
24526 | 1153 |
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" |
1154 |
by (induct xs arbitrary: i) (auto split: nat.split) |
|
13114 | 1155 |
|
14187 | 1156 |
lemma list_update_append1: |
24526 | 1157 |
"i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" |
1158 |
apply (induct xs arbitrary: i, simp) |
|
14187 | 1159 |
apply(simp split:nat.split) |
1160 |
done |
|
1161 |
||
15868 | 1162 |
lemma list_update_append: |
24526 | 1163 |
"(xs @ ys) [n:= x] = |
15868 | 1164 |
(if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))" |
24526 | 1165 |
by (induct xs arbitrary: n) (auto split:nat.splits) |
15868 | 1166 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1167 |
lemma list_update_length [simp]: |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1168 |
"(xs @ x # ys)[length xs := y] = (xs @ y # ys)" |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1169 |
by (induct xs, auto) |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1170 |
|
13114 | 1171 |
lemma update_zip: |
24526 | 1172 |
"length xs = length ys ==> |
1173 |
(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" |
|
1174 |
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split) |
|
1175 |
||
1176 |
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)" |
|
1177 |
by (induct xs arbitrary: i) (auto split: nat.split) |
|
13114 | 1178 |
|
1179 |
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A" |
|
13145 | 1180 |
by (blast dest!: set_update_subset_insert [THEN subsetD]) |
13114 | 1181 |
|
24526 | 1182 |
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" |
1183 |
by (induct xs arbitrary: n) (auto split:nat.splits) |
|
15868 | 1184 |
|
13114 | 1185 |
|
15392 | 1186 |
subsubsection {* @{text last} and @{text butlast} *} |
13114 | 1187 |
|
13142 | 1188 |
lemma last_snoc [simp]: "last (xs @ [x]) = x" |
13145 | 1189 |
by (induct xs) auto |
13114 | 1190 |
|
13142 | 1191 |
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" |
13145 | 1192 |
by (induct xs) auto |
13114 | 1193 |
|
14302 | 1194 |
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" |
1195 |
by(simp add:last.simps) |
|
1196 |
||
1197 |
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" |
|
1198 |
by(simp add:last.simps) |
|
1199 |
||
1200 |
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" |
|
1201 |
by (induct xs) (auto) |
|
1202 |
||
1203 |
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" |
|
1204 |
by(simp add:last_append) |
|
1205 |
||
1206 |
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" |
|
1207 |
by(simp add:last_append) |
|
1208 |
||
17762 | 1209 |
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs" |
1210 |
by(rule rev_exhaust[of xs]) simp_all |
|
1211 |
||
1212 |
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs" |
|
1213 |
by(cases xs) simp_all |
|
1214 |
||
17765 | 1215 |
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as" |
1216 |
by (induct as) auto |
|
17762 | 1217 |
|
13142 | 1218 |
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" |
13145 | 1219 |
by (induct xs rule: rev_induct) auto |
13114 | 1220 |
|
1221 |
lemma butlast_append: |
|
24526 | 1222 |
"butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" |
1223 |
by (induct xs arbitrary: ys) auto |
|
13114 | 1224 |
|
13142 | 1225 |
lemma append_butlast_last_id [simp]: |
13145 | 1226 |
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs" |
1227 |
by (induct xs) auto |
|
13114 | 1228 |
|
13142 | 1229 |
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" |
13145 | 1230 |
by (induct xs) (auto split: split_if_asm) |
13114 | 1231 |
|
1232 |
lemma in_set_butlast_appendI: |
|
13145 | 1233 |
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))" |
1234 |
by (auto dest: in_set_butlastD simp add: butlast_append) |
|
13114 | 1235 |
|
24526 | 1236 |
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs" |
1237 |
apply (induct xs arbitrary: n) |
|
17501 | 1238 |
apply simp |
1239 |
apply (auto split:nat.split) |
|
1240 |
done |
|
1241 |
||
17589 | 1242 |
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)" |
1243 |
by(induct xs)(auto simp:neq_Nil_conv) |
|
1244 |
||
15392 | 1245 |
subsubsection {* @{text take} and @{text drop} *} |
13114 | 1246 |
|
13142 | 1247 |
lemma take_0 [simp]: "take 0 xs = []" |
13145 | 1248 |
by (induct xs) auto |
13114 | 1249 |
|
13142 | 1250 |
lemma drop_0 [simp]: "drop 0 xs = xs" |
13145 | 1251 |
by (induct xs) auto |
13114 | 1252 |
|
13142 | 1253 |
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" |
13145 | 1254 |
by simp |
13114 | 1255 |
|
13142 | 1256 |
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" |
13145 | 1257 |
by simp |
13114 | 1258 |
|
13142 | 1259 |
declare take_Cons [simp del] and drop_Cons [simp del] |
13114 | 1260 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1261 |
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:
15072
diff
changeset
|
1262 |
by(clarsimp simp add:neq_Nil_conv) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1263 |
|
14187 | 1264 |
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" |
1265 |
by(cases xs, simp_all) |
|
1266 |
||
24526 | 1267 |
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)" |
1268 |
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split) |
|
1269 |
||
1270 |
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y" |
|
1271 |
apply (induct xs arbitrary: n, simp) |
|
14187 | 1272 |
apply(simp add:drop_Cons nth_Cons split:nat.splits) |
1273 |
done |
|
1274 |
||
13913 | 1275 |
lemma take_Suc_conv_app_nth: |
24526 | 1276 |
"i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" |
1277 |
apply (induct xs arbitrary: i, simp) |
|
14208 | 1278 |
apply (case_tac i, auto) |
13913 | 1279 |
done |
1280 |
||
14591 | 1281 |
lemma drop_Suc_conv_tl: |
24526 | 1282 |
"i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" |
1283 |
apply (induct xs arbitrary: i, simp) |
|
14591 | 1284 |
apply (case_tac i, auto) |
1285 |
done |
|
1286 |
||
24526 | 1287 |
lemma length_take [simp]: "length (take n xs) = min (length xs) n" |
1288 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
1289 |
||
1290 |
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)" |
|
1291 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
1292 |
||
1293 |
lemma take_all [simp]: "length xs <= n ==> take n xs = xs" |
|
1294 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
1295 |
||
1296 |
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []" |
|
1297 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
13114 | 1298 |
|
13142 | 1299 |
lemma take_append [simp]: |
24526 | 1300 |
"take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" |
1301 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
13114 | 1302 |
|
13142 | 1303 |
lemma drop_append [simp]: |
24526 | 1304 |
"drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys" |
1305 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
1306 |
||
1307 |
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs" |
|
1308 |
apply (induct m arbitrary: xs n, auto) |
|
14208 | 1309 |
apply (case_tac xs, auto) |
15236
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
nipkow
parents:
15176
diff
changeset
|
1310 |
apply (case_tac n, auto) |
13145 | 1311 |
done |
13114 | 1312 |
|
24526 | 1313 |
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs" |
1314 |
apply (induct m arbitrary: xs, auto) |
|
14208 | 1315 |
apply (case_tac xs, auto) |
13145 | 1316 |
done |
13114 | 1317 |
|
24526 | 1318 |
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)" |
1319 |
apply (induct m arbitrary: xs n, auto) |
|
14208 | 1320 |
apply (case_tac xs, auto) |
13145 | 1321 |
done |
13114 | 1322 |
|
24526 | 1323 |
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)" |
1324 |
apply(induct xs arbitrary: m n) |
|
14802 | 1325 |
apply simp |
1326 |
apply(simp add: take_Cons drop_Cons split:nat.split) |
|
1327 |
done |
|
1328 |
||
24526 | 1329 |
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs" |
1330 |
apply (induct n arbitrary: xs, auto) |
|
14208 | 1331 |
apply (case_tac xs, auto) |
13145 | 1332 |
done |
13114 | 1333 |
|
24526 | 1334 |
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])" |
1335 |
apply(induct xs arbitrary: n) |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1336 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1337 |
apply(simp add:take_Cons split:nat.split) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1338 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1339 |
|
24526 | 1340 |
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)" |
1341 |
apply(induct xs arbitrary: n) |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1342 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1343 |
apply(simp add:drop_Cons split:nat.split) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1344 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1345 |
|
24526 | 1346 |
lemma take_map: "take n (map f xs) = map f (take n xs)" |
1347 |
apply (induct n arbitrary: xs, auto) |
|
14208 | 1348 |
apply (case_tac xs, auto) |
13145 | 1349 |
done |
13114 | 1350 |
|
24526 | 1351 |
lemma drop_map: "drop n (map f xs) = map f (drop n xs)" |
1352 |
apply (induct n arbitrary: xs, auto) |
|
14208 | 1353 |
apply (case_tac xs, auto) |
13145 | 1354 |
done |
13114 | 1355 |
|
24526 | 1356 |
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)" |
1357 |
apply (induct xs arbitrary: i, auto) |
|
14208 | 1358 |
apply (case_tac i, auto) |
13145 | 1359 |
done |
13114 | 1360 |
|
24526 | 1361 |
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)" |
1362 |
apply (induct xs arbitrary: i, auto) |
|
14208 | 1363 |
apply (case_tac i, auto) |
13145 | 1364 |
done |
13114 | 1365 |
|
24526 | 1366 |
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i" |
1367 |
apply (induct xs arbitrary: i n, auto) |
|
14208 | 1368 |
apply (case_tac n, blast) |
1369 |
apply (case_tac i, auto) |
|
13145 | 1370 |
done |
13114 | 1371 |
|
13142 | 1372 |
lemma nth_drop [simp]: |
24526 | 1373 |
"n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" |
1374 |
apply (induct n arbitrary: xs i, auto) |
|
14208 | 1375 |
apply (case_tac xs, auto) |
13145 | 1376 |
done |
3507 | 1377 |
|
18423 | 1378 |
lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n" |
1379 |
by(simp add: hd_conv_nth) |
|
1380 |
||
24526 | 1381 |
lemma set_take_subset: "set(take n xs) \<subseteq> set xs" |
1382 |
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split) |
|
1383 |
||
1384 |
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs" |
|
1385 |
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split) |
|
14025 | 1386 |
|
14187 | 1387 |
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs" |
1388 |
using set_take_subset by fast |
|
1389 |
||
1390 |
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs" |
|
1391 |
using set_drop_subset by fast |
|
1392 |
||
13114 | 1393 |
lemma append_eq_conv_conj: |
24526 | 1394 |
"(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" |
1395 |
apply (induct xs arbitrary: zs, simp, clarsimp) |
|
14208 | 1396 |
apply (case_tac zs, auto) |
13145 | 1397 |
done |
13142 | 1398 |
|
24526 | 1399 |
lemma take_add: |
1400 |
"i+j \<le> length(xs) \<Longrightarrow> take (i+j) xs = take i xs @ take j (drop i xs)" |
|
1401 |
apply (induct xs arbitrary: i, auto) |
|
1402 |
apply (case_tac i, simp_all) |
|
14050 | 1403 |
done |
1404 |
||
14300 | 1405 |
lemma append_eq_append_conv_if: |
24526 | 1406 |
"(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) = |
14300 | 1407 |
(if size xs\<^isub>1 \<le> size ys\<^isub>1 |
1408 |
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 |
|
1409 |
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 | 1410 |
apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1) |
14300 | 1411 |
apply simp |
1412 |
apply(case_tac ys\<^isub>1) |
|
1413 |
apply simp_all |
|
1414 |
done |
|
1415 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1416 |
lemma take_hd_drop: |
24526 | 1417 |
"n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (n+1) xs" |
1418 |
apply(induct xs arbitrary: n) |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1419 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1420 |
apply(simp add:drop_Cons split:nat.split) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1421 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1422 |
|
17501 | 1423 |
lemma id_take_nth_drop: |
1424 |
"i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" |
|
1425 |
proof - |
|
1426 |
assume si: "i < length xs" |
|
1427 |
hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto |
|
1428 |
moreover |
|
1429 |
from si have "take (Suc i) xs = take i xs @ [xs!i]" |
|
1430 |
apply (rule_tac take_Suc_conv_app_nth) by arith |
|
1431 |
ultimately show ?thesis by auto |
|
1432 |
qed |
|
1433 |
||
1434 |
lemma upd_conv_take_nth_drop: |
|
1435 |
"i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs" |
|
1436 |
proof - |
|
1437 |
assume i: "i < length xs" |
|
1438 |
have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]" |
|
1439 |
by(rule arg_cong[OF id_take_nth_drop[OF i]]) |
|
1440 |
also have "\<dots> = take i xs @ a # drop (Suc i) xs" |
|
1441 |
using i by (simp add: list_update_append) |
|
1442 |
finally show ?thesis . |
|
1443 |
qed |
|
1444 |
||
13114 | 1445 |
|
15392 | 1446 |
subsubsection {* @{text takeWhile} and @{text dropWhile} *} |
13114 | 1447 |
|
13142 | 1448 |
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" |
13145 | 1449 |
by (induct xs) auto |
13114 | 1450 |
|
13142 | 1451 |
lemma takeWhile_append1 [simp]: |
13145 | 1452 |
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs" |
1453 |
by (induct xs) auto |
|
13114 | 1454 |
|
13142 | 1455 |
lemma takeWhile_append2 [simp]: |
13145 | 1456 |
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys" |
1457 |
by (induct xs) auto |
|
13114 | 1458 |
|
13142 | 1459 |
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs" |
13145 | 1460 |
by (induct xs) auto |
13114 | 1461 |
|
13142 | 1462 |
lemma dropWhile_append1 [simp]: |
13145 | 1463 |
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" |
1464 |
by (induct xs) auto |
|
13114 | 1465 |
|
13142 | 1466 |
lemma dropWhile_append2 [simp]: |
13145 | 1467 |
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" |
1468 |
by (induct xs) auto |
|
13114 | 1469 |
|
23971
e6d505d5b03d
renamed lemma "set_take_whileD" to "set_takeWhileD"
krauss
parents:
23740
diff
changeset
|
1470 |
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x" |
13145 | 1471 |
by (induct xs) (auto split: split_if_asm) |
13114 | 1472 |
|
13913 | 1473 |
lemma takeWhile_eq_all_conv[simp]: |
1474 |
"(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" |
|
1475 |
by(induct xs, auto) |
|
1476 |
||
1477 |
lemma dropWhile_eq_Nil_conv[simp]: |
|
1478 |
"(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" |
|
1479 |
by(induct xs, auto) |
|
1480 |
||
1481 |
lemma dropWhile_eq_Cons_conv: |
|
1482 |
"(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)" |
|
1483 |
by(induct xs, auto) |
|
1484 |
||
17501 | 1485 |
text{* The following two lemmmas could be generalized to an arbitrary |
1486 |
property. *} |
|
1487 |
||
1488 |
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
|
1489 |
takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))" |
|
1490 |
by(induct xs) (auto simp: takeWhile_tail[where l="[]"]) |
|
1491 |
||
1492 |
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
|
1493 |
dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)" |
|
1494 |
apply(induct xs) |
|
1495 |
apply simp |
|
1496 |
apply auto |
|
1497 |
apply(subst dropWhile_append2) |
|
1498 |
apply auto |
|
1499 |
done |
|
1500 |
||
18423 | 1501 |
lemma takeWhile_not_last: |
1502 |
"\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs" |
|
1503 |
apply(induct xs) |
|
1504 |
apply simp |
|
1505 |
apply(case_tac xs) |
|
1506 |
apply(auto) |
|
1507 |
done |
|
1508 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset
|
1509 |
lemma takeWhile_cong [fundef_cong, recdef_cong]: |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1510 |
"[| l = k; !!x. x : set l ==> P x = Q x |] |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1511 |
==> takeWhile P l = takeWhile Q k" |
24349 | 1512 |
by (induct k arbitrary: l) (simp_all) |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1513 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset
|
1514 |
lemma dropWhile_cong [fundef_cong, recdef_cong]: |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1515 |
"[| l = k; !!x. x : set l ==> P x = Q x |] |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1516 |
==> dropWhile P l = dropWhile Q k" |
24349 | 1517 |
by (induct k arbitrary: l, simp_all) |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1518 |
|
13114 | 1519 |
|
15392 | 1520 |
subsubsection {* @{text zip} *} |
13114 | 1521 |
|
13142 | 1522 |
lemma zip_Nil [simp]: "zip [] ys = []" |
13145 | 1523 |
by (induct ys) auto |
13114 | 1524 |
|
13142 | 1525 |
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" |
13145 | 1526 |
by simp |
13114 | 1527 |
|
13142 | 1528 |
declare zip_Cons [simp del] |
13114 | 1529 |
|
15281 | 1530 |
lemma zip_Cons1: |
1531 |
"zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)" |
|
1532 |
by(auto split:list.split) |
|
1533 |
||
13142 | 1534 |
lemma length_zip [simp]: |
22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
1535 |
"length (zip xs ys) = min (length xs) (length ys)" |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
1536 |
by (induct xs ys rule:list_induct2') auto |
13114 | 1537 |
|
1538 |
lemma zip_append1: |
|
22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
1539 |
"zip (xs @ ys) zs = |
13145 | 1540 |
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:
22422
diff
changeset
|
1541 |
by (induct xs zs rule:list_induct2') auto |
13114 | 1542 |
|
1543 |
lemma zip_append2: |
|
22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
1544 |
"zip xs (ys @ zs) = |
13145 | 1545 |
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:
22422
diff
changeset
|
1546 |
by (induct xs ys rule:list_induct2') auto |
13114 | 1547 |
|
13142 | 1548 |
lemma zip_append [simp]: |
1549 |
"[| length xs = length us; length ys = length vs |] ==> |
|
13145 | 1550 |
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" |
1551 |
by (simp add: zip_append1) |
|
13114 | 1552 |
|
1553 |
lemma zip_rev: |
|
14247 | 1554 |
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" |
1555 |
by (induct rule:list_induct2, simp_all) |
|
13114 | 1556 |
|
23096 | 1557 |
lemma map_zip_map: |
1558 |
"map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)" |
|
1559 |
apply(induct xs arbitrary:ys) apply simp |
|
1560 |
apply(case_tac ys) |
|
1561 |
apply simp_all |
|
1562 |
done |
|
1563 |
||
1564 |
lemma map_zip_map2: |
|
1565 |
"map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)" |
|
1566 |
apply(induct xs arbitrary:ys) apply simp |
|
1567 |
apply(case_tac ys) |
|
1568 |
apply simp_all |
|
1569 |
done |
|
1570 |
||
13142 | 1571 |
lemma nth_zip [simp]: |
24526 | 1572 |
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)" |
1573 |
apply (induct ys arbitrary: i xs, simp) |
|
13145 | 1574 |
apply (case_tac xs) |
1575 |
apply (simp_all add: nth.simps split: nat.split) |
|
1576 |
done |
|
13114 | 1577 |
|
1578 |
lemma set_zip: |
|
13145 | 1579 |
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}" |
1580 |
by (simp add: set_conv_nth cong: rev_conj_cong) |
|
13114 | 1581 |
|
1582 |
lemma zip_update: |
|
13145 | 1583 |
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" |
1584 |
by (rule sym, simp add: update_zip) |
|
13114 | 1585 |
|
13142 | 1586 |
lemma zip_replicate [simp]: |
24526 | 1587 |
"zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" |
1588 |
apply (induct i arbitrary: j, auto) |
|
14208 | 1589 |
apply (case_tac j, auto) |
13145 | 1590 |
done |
13114 | 1591 |
|
19487 | 1592 |
lemma take_zip: |
24526 | 1593 |
"take n (zip xs ys) = zip (take n xs) (take n ys)" |
1594 |
apply (induct n arbitrary: xs ys) |
|
19487 | 1595 |
apply simp |
1596 |
apply (case_tac xs, simp) |
|
1597 |
apply (case_tac ys, simp_all) |
|
1598 |
done |
|
1599 |
||
1600 |
lemma drop_zip: |
|
24526 | 1601 |
"drop n (zip xs ys) = zip (drop n xs) (drop n ys)" |
1602 |
apply (induct n arbitrary: xs ys) |
|
19487 | 1603 |
apply simp |
1604 |
apply (case_tac xs, simp) |
|
1605 |
apply (case_tac ys, simp_all) |
|
1606 |
done |
|
1607 |
||
22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
1608 |
lemma set_zip_leftD: |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
1609 |
"(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:
22422
diff
changeset
|
1610 |
by (induct xs ys rule:list_induct2') auto |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
1611 |
|
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
1612 |
lemma set_zip_rightD: |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
1613 |
"(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:
22422
diff
changeset
|
1614 |
by (induct xs ys rule:list_induct2') auto |
13142 | 1615 |
|
23983 | 1616 |
lemma in_set_zipE: |
1617 |
"(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R" |
|
1618 |
by(blast dest: set_zip_leftD set_zip_rightD) |
|
1619 |
||
15392 | 1620 |
subsubsection {* @{text list_all2} *} |
13114 | 1621 |
|
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1622 |
lemma list_all2_lengthD [intro?]: |
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1623 |
"list_all2 P xs ys ==> length xs = length ys" |
24349 | 1624 |
by (simp add: list_all2_def) |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
1625 |
|
19787 | 1626 |
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])" |
24349 | 1627 |
by (simp add: list_all2_def) |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
1628 |
|
19787 | 1629 |
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])" |
24349 | 1630 |
by (simp add: list_all2_def) |
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
1631 |
|
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
1632 |
lemma list_all2_Cons [iff, code]: |
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
1633 |
"list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)" |
24349 | 1634 |
by (auto simp add: list_all2_def) |
13114 | 1635 |
|
1636 |
lemma list_all2_Cons1: |
|
13145 | 1637 |
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" |
1638 |
by (cases ys) auto |
|
13114 | 1639 |
|
1640 |
lemma list_all2_Cons2: |
|
13145 | 1641 |
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" |
1642 |
by (cases xs) auto |
|
13114 | 1643 |
|
13142 | 1644 |
lemma list_all2_rev [iff]: |
13145 | 1645 |
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" |
1646 |
by (simp add: list_all2_def zip_rev cong: conj_cong) |
|
13114 | 1647 |
|
13863 | 1648 |
lemma list_all2_rev1: |
1649 |
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" |
|
1650 |
by (subst list_all2_rev [symmetric]) simp |
|
1651 |
||
13114 | 1652 |
lemma list_all2_append1: |
13145 | 1653 |
"list_all2 P (xs @ ys) zs = |
1654 |
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> |
|
1655 |
list_all2 P xs us \<and> list_all2 P ys vs)" |
|
1656 |
apply (simp add: list_all2_def zip_append1) |
|
1657 |
apply (rule iffI) |
|
1658 |
apply (rule_tac x = "take (length xs) zs" in exI) |
|
1659 |
apply (rule_tac x = "drop (length xs) zs" in exI) |
|
14208 | 1660 |
apply (force split: nat_diff_split simp add: min_def, clarify) |
13145 | 1661 |
apply (simp add: ball_Un) |
1662 |
done |
|
13114 | 1663 |
|
1664 |
lemma list_all2_append2: |
|
13145 | 1665 |
"list_all2 P xs (ys @ zs) = |
1666 |
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> |
|
1667 |
list_all2 P us ys \<and> list_all2 P vs zs)" |
|
1668 |
apply (simp add: list_all2_def zip_append2) |
|
1669 |
apply (rule iffI) |
|
1670 |
apply (rule_tac x = "take (length ys) xs" in exI) |
|
1671 |
apply (rule_tac x = "drop (length ys) xs" in exI) |
|
14208 | 1672 |
apply (force split: nat_diff_split simp add: min_def, clarify) |
13145 | 1673 |
apply (simp add: ball_Un) |
1674 |
done |
|
13114 | 1675 |
|
13863 | 1676 |
lemma list_all2_append: |
14247 | 1677 |
"length xs = length ys \<Longrightarrow> |
1678 |
list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)" |
|
1679 |
by (induct rule:list_induct2, simp_all) |
|
13863 | 1680 |
|
1681 |
lemma list_all2_appendI [intro?, trans]: |
|
1682 |
"\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)" |
|
24349 | 1683 |
by (simp add: list_all2_append list_all2_lengthD) |
13863 | 1684 |
|
13114 | 1685 |
lemma list_all2_conv_all_nth: |
13145 | 1686 |
"list_all2 P xs ys = |
1687 |
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))" |
|
1688 |
by (force simp add: list_all2_def set_zip) |
|
13114 | 1689 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1690 |
lemma list_all2_trans: |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1691 |
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:
13863
diff
changeset
|
1692 |
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:
13863
diff
changeset
|
1693 |
(is "!!bs cs. PROP ?Q as bs cs") |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1694 |
proof (induct as) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1695 |
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:
13863
diff
changeset
|
1696 |
show "!!cs. PROP ?Q (x # xs) bs cs" |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1697 |
proof (induct bs) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1698 |
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:
13863
diff
changeset
|
1699 |
show "PROP ?Q (x # xs) (y # ys) cs" |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1700 |
by (induct cs) (auto intro: tr I1 I2) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1701 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1702 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1703 |
|
13863 | 1704 |
lemma list_all2_all_nthI [intro?]: |
1705 |
"length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b" |
|
24349 | 1706 |
by (simp add: list_all2_conv_all_nth) |
13863 | 1707 |
|
14395 | 1708 |
lemma list_all2I: |
1709 |
"\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b" |
|
24349 | 1710 |
by (simp add: list_all2_def) |
14395 | 1711 |
|
14328 | 1712 |
lemma list_all2_nthD: |
13863 | 1713 |
"\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
24349 | 1714 |
by (simp add: list_all2_conv_all_nth) |
13863 | 1715 |
|
14302 | 1716 |
lemma list_all2_nthD2: |
1717 |
"\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
|
24349 | 1718 |
by (frule list_all2_lengthD) (auto intro: list_all2_nthD) |
14302 | 1719 |
|
13863 | 1720 |
lemma list_all2_map1: |
1721 |
"list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs" |
|
24349 | 1722 |
by (simp add: list_all2_conv_all_nth) |
13863 | 1723 |
|
1724 |
lemma list_all2_map2: |
|
1725 |
"list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs" |
|
24349 | 1726 |
by (auto simp add: list_all2_conv_all_nth) |
13863 | 1727 |
|
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1728 |
lemma list_all2_refl [intro?]: |
13863 | 1729 |
"(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs" |
24349 | 1730 |
by (simp add: list_all2_conv_all_nth) |
13863 | 1731 |
|
1732 |
lemma list_all2_update_cong: |
|
1733 |
"\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" |
|
24349 | 1734 |
by (simp add: list_all2_conv_all_nth nth_list_update) |
13863 | 1735 |
|
1736 |
lemma list_all2_update_cong2: |
|
1737 |
"\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" |
|
24349 | 1738 |
by (simp add: list_all2_lengthD list_all2_update_cong) |
13863 | 1739 |
|
14302 | 1740 |
lemma list_all2_takeI [simp,intro?]: |
24526 | 1741 |
"list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)" |
1742 |
apply (induct xs arbitrary: n ys) |
|
1743 |
apply simp |
|
1744 |
apply (clarsimp simp add: list_all2_Cons1) |
|
1745 |
apply (case_tac n) |
|
1746 |
apply auto |
|
1747 |
done |
|
14302 | 1748 |
|
1749 |
lemma list_all2_dropI [simp,intro?]: |
|
24526 | 1750 |
"list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)" |
1751 |
apply (induct as arbitrary: n bs, simp) |
|
1752 |
apply (clarsimp simp add: list_all2_Cons1) |
|
1753 |
apply (case_tac n, simp, simp) |
|
1754 |
done |
|
13863 | 1755 |
|
14327 | 1756 |
lemma list_all2_mono [intro?]: |
24526 | 1757 |
"list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys" |
1758 |
apply (induct xs arbitrary: ys, simp) |
|
1759 |
apply (case_tac ys, auto) |
|
1760 |
done |
|
13863 | 1761 |
|
22551 | 1762 |
lemma list_all2_eq: |
1763 |
"xs = ys \<longleftrightarrow> list_all2 (op =) xs ys" |
|
24349 | 1764 |
by (induct xs ys rule: list_induct2') auto |
22551 | 1765 |
|
13142 | 1766 |
|
15392 | 1767 |
subsubsection {* @{text foldl} and @{text foldr} *} |
13142 | 1768 |
|
1769 |
lemma foldl_append [simp]: |
|
24526 | 1770 |
"foldl f a (xs @ ys) = foldl f (foldl f a xs) ys" |
1771 |
by (induct xs arbitrary: a) auto |
|
13142 | 1772 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1773 |
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:
14395
diff
changeset
|
1774 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1775 |
|
23096 | 1776 |
lemma foldr_map: "foldr g (map f xs) a = foldr (g o f) xs a" |
1777 |
by(induct xs) simp_all |
|
1778 |
||
24449 | 1779 |
text{* For efficient code generation: avoid intermediate list. *} |
1780 |
lemma foldl_map[code unfold]: |
|
1781 |
"foldl g a (map f xs) = foldl (%a x. g a (f x)) a xs" |
|
23096 | 1782 |
by(induct xs arbitrary:a) simp_all |
1783 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset
|
1784 |
lemma foldl_cong [fundef_cong, recdef_cong]: |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1785 |
"[| 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:
18049
diff
changeset
|
1786 |
==> foldl f a l = foldl g b k" |
24349 | 1787 |
by (induct k arbitrary: a b l) simp_all |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1788 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset
|
1789 |
lemma foldr_cong [fundef_cong, recdef_cong]: |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1790 |
"[| 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:
18049
diff
changeset
|
1791 |
==> foldr f l a = foldr g k b" |
24349 | 1792 |
by (induct k arbitrary: a b l) simp_all |
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1793 |
|
24449 | 1794 |
lemma (in semigroup_add) foldl_assoc: |
1795 |
shows "foldl op\<^loc>+ (x\<^loc>+y) zs = x \<^loc>+ (foldl op\<^loc>+ y zs)" |
|
1796 |
by (induct zs arbitrary: y) (simp_all add:add_assoc) |
|
1797 |
||
1798 |
lemma (in monoid_add) foldl_absorb0: |
|
1799 |
shows "x \<^loc>+ (foldl op\<^loc>+ \<^loc>0 zs) = foldl op\<^loc>+ x zs" |
|
1800 |
by (induct zs) (simp_all add:foldl_assoc) |
|
1801 |
||
1802 |
||
23096 | 1803 |
text{* The ``First Duality Theorem'' in Bird \& Wadler: *} |
1804 |
||
1805 |
lemma foldl_foldr1_lemma: |
|
1806 |
"foldl op + a xs = a + foldr op + xs (0\<Colon>'a::monoid_add)" |
|
1807 |
by (induct xs arbitrary: a) (auto simp:add_assoc) |
|
1808 |
||
1809 |
corollary foldl_foldr1: |
|
1810 |
"foldl op + 0 xs = foldr op + xs (0\<Colon>'a::monoid_add)" |
|
1811 |
by (simp add:foldl_foldr1_lemma) |
|
1812 |
||
1813 |
||
1814 |
text{* The ``Third Duality Theorem'' in Bird \& Wadler: *} |
|
1815 |
||
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1816 |
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:
14395
diff
changeset
|
1817 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1818 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1819 |
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:
14395
diff
changeset
|
1820 |
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:
14395
diff
changeset
|
1821 |
|
24471
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
chaieb
parents:
24461
diff
changeset
|
1822 |
lemma (in ab_semigroup_add) foldr_conv_foldl: "foldr op \<^loc>+ xs a = foldl op \<^loc>+ a xs" |
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
chaieb
parents:
24461
diff
changeset
|
1823 |
by (induct xs, auto simp add: foldl_assoc add_commute) |
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
chaieb
parents:
24461
diff
changeset
|
1824 |
|
13142 | 1825 |
text {* |
13145 | 1826 |
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more |
1827 |
difficult to use because it requires an additional transitivity step. |
|
13142 | 1828 |
*} |
1829 |
||
24526 | 1830 |
lemma start_le_sum: "(m::nat) <= n ==> m <= foldl (op +) n ns" |
1831 |
by (induct ns arbitrary: n) auto |
|
1832 |
||
1833 |
lemma elem_le_sum: "(n::nat) : set ns ==> n <= foldl (op +) 0 ns" |
|
13145 | 1834 |
by (force intro: start_le_sum simp add: in_set_conv_decomp) |
13142 | 1835 |
|
1836 |
lemma sum_eq_0_conv [iff]: |
|
24526 | 1837 |
"(foldl (op +) (m::nat) ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))" |
1838 |
by (induct ns arbitrary: m) auto |
|
13114 | 1839 |
|
24471
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
chaieb
parents:
24461
diff
changeset
|
1840 |
lemma foldr_invariant: |
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
chaieb
parents:
24461
diff
changeset
|
1841 |
"\<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:
24461
diff
changeset
|
1842 |
by (induct xs, simp_all) |
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
chaieb
parents:
24461
diff
changeset
|
1843 |
|
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
chaieb
parents:
24461
diff
changeset
|
1844 |
lemma foldl_invariant: |
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
chaieb
parents:
24461
diff
changeset
|
1845 |
"\<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:
24461
diff
changeset
|
1846 |
by (induct xs arbitrary: x, simp_all) |
d7cf53c1085f
removed unused theorems ; added lifting properties for foldr and foldl
chaieb
parents:
24461
diff
changeset
|
1847 |
|
24449 | 1848 |
text{* @{const foldl} and @{text concat} *} |
1849 |
||
1850 |
lemma concat_conv_foldl: "concat xss = foldl op@ [] xss" |
|
1851 |
by (induct xss) (simp_all add:monoid_append.foldl_absorb0) |
|
1852 |
||
1853 |
lemma foldl_conv_concat: |
|
1854 |
"foldl (op @) xs xxs = xs @ (concat xxs)" |
|
1855 |
by(simp add:concat_conv_foldl monoid_append.foldl_absorb0) |
|
1856 |
||
23096 | 1857 |
subsubsection {* List summation: @{const listsum} and @{text"\<Sum>"}*} |
1858 |
||
24449 | 1859 |
lemma listsum_append[simp]: "listsum (xs @ ys) = listsum xs + listsum ys" |
1860 |
by (induct xs) (simp_all add:add_assoc) |
|
1861 |
||
1862 |
lemma listsum_rev[simp]: |
|
1863 |
fixes xs :: "'a::comm_monoid_add list" |
|
1864 |
shows "listsum (rev xs) = listsum xs" |
|
1865 |
by (induct xs) (simp_all add:add_ac) |
|
1866 |
||
23096 | 1867 |
lemma listsum_foldr: |
1868 |
"listsum xs = foldr (op +) xs 0" |
|
1869 |
by(induct xs) auto |
|
1870 |
||
24449 | 1871 |
text{* For efficient code generation --- |
1872 |
@{const listsum} is not tail recursive but @{const foldl} is. *} |
|
1873 |
lemma listsum[code unfold]: "listsum xs = foldl (op +) 0 xs" |
|
23096 | 1874 |
by(simp add:listsum_foldr foldl_foldr1) |
1875 |
||
24449 | 1876 |
|
23096 | 1877 |
text{* Some syntactic sugar for summing a function over a list: *} |
1878 |
||
1879 |
syntax |
|
1880 |
"_listsum" :: "pttrn => 'a list => 'b => 'b" ("(3SUM _<-_. _)" [0, 51, 10] 10) |
|
1881 |
syntax (xsymbols) |
|
1882 |
"_listsum" :: "pttrn => 'a list => 'b => 'b" ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10) |
|
1883 |
syntax (HTML output) |
|
1884 |
"_listsum" :: "pttrn => 'a list => 'b => 'b" ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10) |
|
1885 |
||
1886 |
translations -- {* Beware of argument permutation! *} |
|
1887 |
"SUM x<-xs. b" == "CONST listsum (map (%x. b) xs)" |
|
1888 |
"\<Sum>x\<leftarrow>xs. b" == "CONST listsum (map (%x. b) xs)" |
|
1889 |
||
1890 |
lemma listsum_0 [simp]: "(\<Sum>x\<leftarrow>xs. 0) = 0" |
|
1891 |
by (induct xs) simp_all |
|
1892 |
||
1893 |
text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *} |
|
1894 |
lemma uminus_listsum_map: |
|
1895 |
"- listsum (map f xs) = (listsum (map (uminus o f) xs) :: 'a::ab_group_add)" |
|
1896 |
by(induct xs) simp_all |
|
1897 |
||
13114 | 1898 |
|
24645 | 1899 |
subsubsection {* @{text upt} *} |
13114 | 1900 |
|
17090 | 1901 |
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])" |
1902 |
-- {* simp does not terminate! *} |
|
13145 | 1903 |
by (induct j) auto |
13142 | 1904 |
|
15425 | 1905 |
lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []" |
13145 | 1906 |
by (subst upt_rec) simp |
13114 | 1907 |
|
15425 | 1908 |
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)" |
15281 | 1909 |
by(induct j)simp_all |
1910 |
||
1911 |
lemma upt_eq_Cons_conv: |
|
24526 | 1912 |
"([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)" |
1913 |
apply(induct j arbitrary: x xs) |
|
15281 | 1914 |
apply simp |
1915 |
apply(clarsimp simp add: append_eq_Cons_conv) |
|
1916 |
apply arith |
|
1917 |
done |
|
1918 |
||
15425 | 1919 |
lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]" |
13145 | 1920 |
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *} |
1921 |
by simp |
|
13114 | 1922 |
|
15425 | 1923 |
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]" |
24632 | 1924 |
by (metis upt_rec) |
13114 | 1925 |
|
15425 | 1926 |
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]" |
13145 | 1927 |
-- {* LOOPS as a simprule, since @{text "j <= j"}. *} |
1928 |
by (induct k) auto |
|
13114 | 1929 |
|
15425 | 1930 |
lemma length_upt [simp]: "length [i..<j] = j - i" |
13145 | 1931 |
by (induct j) (auto simp add: Suc_diff_le) |
13114 | 1932 |
|
15425 | 1933 |
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k" |
13145 | 1934 |
apply (induct j) |
1935 |
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split) |
|
1936 |
done |
|
13114 | 1937 |
|
17906 | 1938 |
|
1939 |
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i" |
|
1940 |
by(simp add:upt_conv_Cons) |
|
1941 |
||
1942 |
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1" |
|
1943 |
apply(cases j) |
|
1944 |
apply simp |
|
1945 |
by(simp add:upt_Suc_append) |
|
1946 |
||
24526 | 1947 |
lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]" |
1948 |
apply (induct m arbitrary: i, simp) |
|
13145 | 1949 |
apply (subst upt_rec) |
1950 |
apply (rule sym) |
|
1951 |
apply (subst upt_rec) |
|
1952 |
apply (simp del: upt.simps) |
|
1953 |
done |
|
3507 | 1954 |
|
17501 | 1955 |
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]" |
1956 |
apply(induct j) |
|
1957 |
apply auto |
|
1958 |
done |
|
1959 |
||
24645 | 1960 |
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]" |
13145 | 1961 |
by (induct n) auto |
13114 | 1962 |
|
24526 | 1963 |
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)" |
1964 |
apply (induct n m arbitrary: i rule: diff_induct) |
|
13145 | 1965 |
prefer 3 apply (subst map_Suc_upt[symmetric]) |
1966 |
apply (auto simp add: less_diff_conv nth_upt) |
|
1967 |
done |
|
13114 | 1968 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1969 |
lemma nth_take_lemma: |
24526 | 1970 |
"k <= length xs ==> k <= length ys ==> |
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1971 |
(!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys" |
24526 | 1972 |
apply (atomize, induct k arbitrary: xs ys) |
14208 | 1973 |
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify) |
13145 | 1974 |
txt {* Both lists must be non-empty *} |
14208 | 1975 |
apply (case_tac xs, simp) |
1976 |
apply (case_tac ys, clarify) |
|
13145 | 1977 |
apply (simp (no_asm_use)) |
1978 |
apply clarify |
|
1979 |
txt {* prenexing's needed, not miniscoping *} |
|
1980 |
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps) |
|
1981 |
apply blast |
|
1982 |
done |
|
13114 | 1983 |
|
1984 |
lemma nth_equalityI: |
|
1985 |
"[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys" |
|
13145 | 1986 |
apply (frule nth_take_lemma [OF le_refl eq_imp_le]) |
1987 |
apply (simp_all add: take_all) |
|
1988 |
done |
|
13142 | 1989 |
|
13863 | 1990 |
(* needs nth_equalityI *) |
1991 |
lemma list_all2_antisym: |
|
1992 |
"\<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> |
|
1993 |
\<Longrightarrow> xs = ys" |
|
1994 |
apply (simp add: list_all2_conv_all_nth) |
|
14208 | 1995 |
apply (rule nth_equalityI, blast, simp) |
13863 | 1996 |
done |
1997 |
||
13142 | 1998 |
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys" |
13145 | 1999 |
-- {* The famous take-lemma. *} |
2000 |
apply (drule_tac x = "max (length xs) (length ys)" in spec) |
|
2001 |
apply (simp add: le_max_iff_disj take_all) |
|
2002 |
done |
|
13142 | 2003 |
|
2004 |
||
15302 | 2005 |
lemma take_Cons': |
2006 |
"take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)" |
|
2007 |
by (cases n) simp_all |
|
2008 |
||
2009 |
lemma drop_Cons': |
|
2010 |
"drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)" |
|
2011 |
by (cases n) simp_all |
|
2012 |
||
2013 |
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))" |
|
2014 |
by (cases n) simp_all |
|
2015 |
||
18622 | 2016 |
lemmas take_Cons_number_of = take_Cons'[of "number_of v",standard] |
2017 |
lemmas drop_Cons_number_of = drop_Cons'[of "number_of v",standard] |
|
2018 |
lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v",standard] |
|
2019 |
||
2020 |
declare take_Cons_number_of [simp] |
|
2021 |
drop_Cons_number_of [simp] |
|
2022 |
nth_Cons_number_of [simp] |
|
15302 | 2023 |
|
2024 |
||
15392 | 2025 |
subsubsection {* @{text "distinct"} and @{text remdups} *} |
13142 | 2026 |
|
2027 |
lemma distinct_append [simp]: |
|
13145 | 2028 |
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})" |
2029 |
by (induct xs) auto |
|
13142 | 2030 |
|
15305 | 2031 |
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs" |
2032 |
by(induct xs) auto |
|
2033 |
||
13142 | 2034 |
lemma set_remdups [simp]: "set (remdups xs) = set xs" |
13145 | 2035 |
by (induct xs) (auto simp add: insert_absorb) |
13142 | 2036 |
|
2037 |
lemma distinct_remdups [iff]: "distinct (remdups xs)" |
|
13145 | 2038 |
by (induct xs) auto |
13142 | 2039 |
|
24566 | 2040 |
lemma finite_distinct_list: "finite A \<Longrightarrow> EX xs. set xs = A & distinct xs" |
24632 | 2041 |
by (metis distinct_remdups finite_list set_remdups) |
24566 | 2042 |
|
15072 | 2043 |
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])" |
24349 | 2044 |
by (induct x, auto) |
15072 | 2045 |
|
2046 |
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])" |
|
24349 | 2047 |
by (induct x, auto) |
15072 | 2048 |
|
15245 | 2049 |
lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs" |
2050 |
by (induct xs) auto |
|
2051 |
||
2052 |
lemma length_remdups_eq[iff]: |
|
2053 |
"(length (remdups xs) = length xs) = (remdups xs = xs)" |
|
2054 |
apply(induct xs) |
|
2055 |
apply auto |
|
2056 |
apply(subgoal_tac "length (remdups xs) <= length xs") |
|
2057 |
apply arith |
|
2058 |
apply(rule length_remdups_leq) |
|
2059 |
done |
|
2060 |
||
18490 | 2061 |
|
2062 |
lemma distinct_map: |
|
2063 |
"distinct(map f xs) = (distinct xs & inj_on f (set xs))" |
|
2064 |
by (induct xs) auto |
|
2065 |
||
2066 |
||
13142 | 2067 |
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)" |
13145 | 2068 |
by (induct xs) auto |
13114 | 2069 |
|
17501 | 2070 |
lemma distinct_upt[simp]: "distinct[i..<j]" |
2071 |
by (induct j) auto |
|
2072 |
||
24526 | 2073 |
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)" |
2074 |
apply(induct xs arbitrary: i) |
|
17501 | 2075 |
apply simp |
2076 |
apply (case_tac i) |
|
2077 |
apply simp_all |
|
2078 |
apply(blast dest:in_set_takeD) |
|
2079 |
done |
|
2080 |
||
24526 | 2081 |
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)" |
2082 |
apply(induct xs arbitrary: i) |
|
17501 | 2083 |
apply simp |
2084 |
apply (case_tac i) |
|
2085 |
apply simp_all |
|
2086 |
done |
|
2087 |
||
2088 |
lemma distinct_list_update: |
|
2089 |
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}" |
|
2090 |
shows "distinct (xs[i:=a])" |
|
2091 |
proof (cases "i < length xs") |
|
2092 |
case True |
|
2093 |
with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}" |
|
2094 |
apply (drule_tac id_take_nth_drop) by simp |
|
2095 |
with d True show ?thesis |
|
2096 |
apply (simp add: upd_conv_take_nth_drop) |
|
2097 |
apply (drule subst [OF id_take_nth_drop]) apply assumption |
|
2098 |
apply simp apply (cases "a = xs!i") apply simp by blast |
|
2099 |
next |
|
2100 |
case False with d show ?thesis by auto |
|
2101 |
qed |
|
2102 |
||
2103 |
||
2104 |
text {* It is best to avoid this indexed version of distinct, but |
|
2105 |
sometimes it is useful. *} |
|
2106 |
||
13142 | 2107 |
lemma distinct_conv_nth: |
17501 | 2108 |
"distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)" |
15251 | 2109 |
apply (induct xs, simp, simp) |
14208 | 2110 |
apply (rule iffI, clarsimp) |
13145 | 2111 |
apply (case_tac i) |
14208 | 2112 |
apply (case_tac j, simp) |
13145 | 2113 |
apply (simp add: set_conv_nth) |
2114 |
apply (case_tac j) |
|
14208 | 2115 |
apply (clarsimp simp add: set_conv_nth, simp) |
13145 | 2116 |
apply (rule conjI) |
24632 | 2117 |
apply (metis Zero_neq_Suc gr0_conv_Suc in_set_conv_nth lessI less_trans_Suc nth_Cons' nth_Cons_Suc) |
2118 |
apply (metis Suc_Suc_eq lessI less_trans_Suc nth_Cons_Suc) |
|
13145 | 2119 |
done |
13114 | 2120 |
|
18490 | 2121 |
lemma nth_eq_iff_index_eq: |
2122 |
"\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)" |
|
2123 |
by(auto simp: distinct_conv_nth) |
|
2124 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2125 |
lemma distinct_card: "distinct xs ==> card (set xs) = size xs" |
24349 | 2126 |
by (induct xs) auto |
14388 | 2127 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2128 |
lemma card_distinct: "card (set xs) = size xs ==> distinct xs" |
14388 | 2129 |
proof (induct xs) |
2130 |
case Nil thus ?case by simp |
|
2131 |
next |
|
2132 |
case (Cons x xs) |
|
2133 |
show ?case |
|
2134 |
proof (cases "x \<in> set xs") |
|
2135 |
case False with Cons show ?thesis by simp |
|
2136 |
next |
|
2137 |
case True with Cons.prems |
|
2138 |
have "card (set xs) = Suc (length xs)" |
|
2139 |
by (simp add: card_insert_if split: split_if_asm) |
|
2140 |
moreover have "card (set xs) \<le> length xs" by (rule card_length) |
|
2141 |
ultimately have False by simp |
|
2142 |
thus ?thesis .. |
|
2143 |
qed |
|
2144 |
qed |
|
2145 |
||
18490 | 2146 |
|
2147 |
lemma length_remdups_concat: |
|
2148 |
"length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)" |
|
24308 | 2149 |
by(simp add: set_concat distinct_card[symmetric]) |
17906 | 2150 |
|
2151 |
||
15392 | 2152 |
subsubsection {* @{text remove1} *} |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2153 |
|
18049 | 2154 |
lemma remove1_append: |
2155 |
"remove1 x (xs @ ys) = |
|
2156 |
(if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)" |
|
2157 |
by (induct xs) auto |
|
2158 |
||
23479 | 2159 |
lemma in_set_remove1[simp]: |
2160 |
"a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)" |
|
2161 |
apply (induct xs) |
|
2162 |
apply auto |
|
2163 |
done |
|
2164 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2165 |
lemma set_remove1_subset: "set(remove1 x xs) <= set xs" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2166 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2167 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2168 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2169 |
apply blast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2170 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2171 |
|
17724 | 2172 |
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:
15072
diff
changeset
|
2173 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2174 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2175 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2176 |
apply blast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2177 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2178 |
|
23479 | 2179 |
lemma length_remove1: |
2180 |
"length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)" |
|
2181 |
apply (induct xs) |
|
2182 |
apply (auto dest!:length_pos_if_in_set) |
|
2183 |
done |
|
2184 |
||
18049 | 2185 |
lemma remove1_filter_not[simp]: |
2186 |
"\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs" |
|
2187 |
by(induct xs) auto |
|
2188 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2189 |
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:
15072
diff
changeset
|
2190 |
apply(insert set_remove1_subset) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2191 |
apply fast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2192 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2193 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2194 |
lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2195 |
by (induct xs) simp_all |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
2196 |
|
13114 | 2197 |
|
15392 | 2198 |
subsubsection {* @{text replicate} *} |
13114 | 2199 |
|
13142 | 2200 |
lemma length_replicate [simp]: "length (replicate n x) = n" |
13145 | 2201 |
by (induct n) auto |
13124 | 2202 |
|
13142 | 2203 |
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)" |
13145 | 2204 |
by (induct n) auto |
13114 | 2205 |
|
2206 |
lemma replicate_app_Cons_same: |
|
13145 | 2207 |
"(replicate n x) @ (x # xs) = x # replicate n x @ xs" |
2208 |
by (induct n) auto |
|
13114 | 2209 |
|
13142 | 2210 |
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x" |
14208 | 2211 |
apply (induct n, simp) |
13145 | 2212 |
apply (simp add: replicate_app_Cons_same) |
2213 |
done |
|
13114 | 2214 |
|
13142 | 2215 |
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x" |
13145 | 2216 |
by (induct n) auto |
13114 | 2217 |
|
16397 | 2218 |
text{* Courtesy of Matthias Daum: *} |
2219 |
lemma append_replicate_commute: |
|
2220 |
"replicate n x @ replicate k x = replicate k x @ replicate n x" |
|
2221 |
apply (simp add: replicate_add [THEN sym]) |
|
2222 |
apply (simp add: add_commute) |
|
2223 |
done |
|
2224 |
||
13142 | 2225 |
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x" |
13145 | 2226 |
by (induct n) auto |
13114 | 2227 |
|
13142 | 2228 |
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x" |
13145 | 2229 |
by (induct n) auto |
13114 | 2230 |
|
13142 | 2231 |
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x" |
13145 | 2232 |
by (atomize (full), induct n) auto |
13114 | 2233 |
|
24526 | 2234 |
lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x" |
2235 |
apply (induct n arbitrary: i, simp) |
|
13145 | 2236 |
apply (simp add: nth_Cons split: nat.split) |
2237 |
done |
|
13114 | 2238 |
|
16397 | 2239 |
text{* Courtesy of Matthias Daum (2 lemmas): *} |
2240 |
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x" |
|
2241 |
apply (case_tac "k \<le> i") |
|
2242 |
apply (simp add: min_def) |
|
2243 |
apply (drule not_leE) |
|
2244 |
apply (simp add: min_def) |
|
2245 |
apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x") |
|
2246 |
apply simp |
|
2247 |
apply (simp add: replicate_add [symmetric]) |
|
2248 |
done |
|
2249 |
||
24526 | 2250 |
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x" |
2251 |
apply (induct k arbitrary: i) |
|
16397 | 2252 |
apply simp |
2253 |
apply clarsimp |
|
2254 |
apply (case_tac i) |
|
2255 |
apply simp |
|
2256 |
apply clarsimp |
|
2257 |
done |
|
2258 |
||
2259 |
||
13142 | 2260 |
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}" |
13145 | 2261 |
by (induct n) auto |
13114 | 2262 |
|
13142 | 2263 |
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}" |
13145 | 2264 |
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc) |
13114 | 2265 |
|
13142 | 2266 |
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})" |
13145 | 2267 |
by auto |
13114 | 2268 |
|
13142 | 2269 |
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y" |
13145 | 2270 |
by (simp add: set_replicate_conv_if split: split_if_asm) |
13114 | 2271 |
|
2272 |
||
15392 | 2273 |
subsubsection{*@{text rotate1} and @{text rotate}*} |
15302 | 2274 |
|
2275 |
lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]" |
|
2276 |
by(simp add:rotate1_def) |
|
2277 |
||
2278 |
lemma rotate0[simp]: "rotate 0 = id" |
|
2279 |
by(simp add:rotate_def) |
|
2280 |
||
2281 |
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)" |
|
2282 |
by(simp add:rotate_def) |
|
2283 |
||
2284 |
lemma rotate_add: |
|
2285 |
"rotate (m+n) = rotate m o rotate n" |
|
2286 |
by(simp add:rotate_def funpow_add) |
|
2287 |
||
2288 |
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs" |
|
2289 |
by(simp add:rotate_add) |
|
2290 |
||
18049 | 2291 |
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)" |
2292 |
by(simp add:rotate_def funpow_swap1) |
|
2293 |
||
15302 | 2294 |
lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs" |
2295 |
by(cases xs) simp_all |
|
2296 |
||
2297 |
lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs" |
|
2298 |
apply(induct n) |
|
2299 |
apply simp |
|
2300 |
apply (simp add:rotate_def) |
|
13145 | 2301 |
done |
13114 | 2302 |
|
15302 | 2303 |
lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]" |
2304 |
by(simp add:rotate1_def split:list.split) |
|
2305 |
||
2306 |
lemma rotate_drop_take: |
|
2307 |
"rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs" |
|
2308 |
apply(induct n) |
|
2309 |
apply simp |
|
2310 |
apply(simp add:rotate_def) |
|
2311 |
apply(cases "xs = []") |
|
2312 |
apply (simp) |
|
2313 |
apply(case_tac "n mod length xs = 0") |
|
2314 |
apply(simp add:mod_Suc) |
|
2315 |
apply(simp add: rotate1_hd_tl drop_Suc take_Suc) |
|
2316 |
apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric] |
|
2317 |
take_hd_drop linorder_not_le) |
|
13145 | 2318 |
done |
13114 | 2319 |
|
15302 | 2320 |
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs" |
2321 |
by(simp add:rotate_drop_take) |
|
2322 |
||
2323 |
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs" |
|
2324 |
by(simp add:rotate_drop_take) |
|
2325 |
||
2326 |
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs" |
|
2327 |
by(simp add:rotate1_def split:list.split) |
|
2328 |
||
24526 | 2329 |
lemma length_rotate[simp]: "length(rotate n xs) = length xs" |
2330 |
by (induct n arbitrary: xs) (simp_all add:rotate_def) |
|
15302 | 2331 |
|
2332 |
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs" |
|
2333 |
by(simp add:rotate1_def split:list.split) blast |
|
2334 |
||
2335 |
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs" |
|
2336 |
by (induct n) (simp_all add:rotate_def) |
|
2337 |
||
2338 |
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)" |
|
2339 |
by(simp add:rotate_drop_take take_map drop_map) |
|
2340 |
||
2341 |
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs" |
|
2342 |
by(simp add:rotate1_def split:list.split) |
|
2343 |
||
2344 |
lemma set_rotate[simp]: "set(rotate n xs) = set xs" |
|
2345 |
by (induct n) (simp_all add:rotate_def) |
|
2346 |
||
2347 |
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])" |
|
2348 |
by(simp add:rotate1_def split:list.split) |
|
2349 |
||
2350 |
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])" |
|
2351 |
by (induct n) (simp_all add:rotate_def) |
|
13114 | 2352 |
|
15439 | 2353 |
lemma rotate_rev: |
2354 |
"rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)" |
|
2355 |
apply(simp add:rotate_drop_take rev_drop rev_take) |
|
2356 |
apply(cases "length xs = 0") |
|
2357 |
apply simp |
|
2358 |
apply(cases "n mod length xs = 0") |
|
2359 |
apply simp |
|
2360 |
apply(simp add:rotate_drop_take rev_drop rev_take) |
|
2361 |
done |
|
2362 |
||
18423 | 2363 |
lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)" |
2364 |
apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth) |
|
2365 |
apply(subgoal_tac "length xs \<noteq> 0") |
|
2366 |
prefer 2 apply simp |
|
2367 |
using mod_less_divisor[of "length xs" n] by arith |
|
2368 |
||
13114 | 2369 |
|
15392 | 2370 |
subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *} |
13114 | 2371 |
|
13142 | 2372 |
lemma sublist_empty [simp]: "sublist xs {} = []" |
13145 | 2373 |
by (auto simp add: sublist_def) |
13114 | 2374 |
|
13142 | 2375 |
lemma sublist_nil [simp]: "sublist [] A = []" |
13145 | 2376 |
by (auto simp add: sublist_def) |
13114 | 2377 |
|
15281 | 2378 |
lemma length_sublist: |
2379 |
"length(sublist xs I) = card{i. i < length xs \<and> i : I}" |
|
2380 |
by(simp add: sublist_def length_filter_conv_card cong:conj_cong) |
|
2381 |
||
2382 |
lemma sublist_shift_lemma_Suc: |
|
24526 | 2383 |
"map fst (filter (%p. P(Suc(snd p))) (zip xs is)) = |
2384 |
map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))" |
|
2385 |
apply(induct xs arbitrary: "is") |
|
15281 | 2386 |
apply simp |
2387 |
apply (case_tac "is") |
|
2388 |
apply simp |
|
2389 |
apply simp |
|
2390 |
done |
|
2391 |
||
13114 | 2392 |
lemma sublist_shift_lemma: |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
2393 |
"map fst [p<-zip xs [i..<i + length xs] . snd p : A] = |
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
2394 |
map fst [p<-zip xs [0..<length xs] . snd p + i : A]" |
13145 | 2395 |
by (induct xs rule: rev_induct) (simp_all add: add_commute) |
13114 | 2396 |
|
2397 |
lemma sublist_append: |
|
15168 | 2398 |
"sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}" |
13145 | 2399 |
apply (unfold sublist_def) |
14208 | 2400 |
apply (induct l' rule: rev_induct, simp) |
13145 | 2401 |
apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma) |
2402 |
apply (simp add: add_commute) |
|
2403 |
done |
|
13114 | 2404 |
|
2405 |
lemma sublist_Cons: |
|
13145 | 2406 |
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}" |
2407 |
apply (induct l rule: rev_induct) |
|
2408 |
apply (simp add: sublist_def) |
|
2409 |
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append) |
|
2410 |
done |
|
13114 | 2411 |
|
24526 | 2412 |
lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}" |
2413 |
apply(induct xs arbitrary: I) |
|
15281 | 2414 |
apply simp |
2415 |
apply(auto simp add:sublist_Cons nth_Cons split:nat.split elim: lessE) |
|
2416 |
apply(erule lessE) |
|
2417 |
apply auto |
|
2418 |
apply(erule lessE) |
|
2419 |
apply auto |
|
2420 |
done |
|
2421 |
||
2422 |
lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs" |
|
2423 |
by(auto simp add:set_sublist) |
|
2424 |
||
2425 |
lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)" |
|
2426 |
by(auto simp add:set_sublist) |
|
2427 |
||
2428 |
lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs" |
|
2429 |
by(auto simp add:set_sublist) |
|
2430 |
||
13142 | 2431 |
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])" |
13145 | 2432 |
by (simp add: sublist_Cons) |
13114 | 2433 |
|
15281 | 2434 |
|
24526 | 2435 |
lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)" |
2436 |
apply(induct xs arbitrary: I) |
|
15281 | 2437 |
apply simp |
2438 |
apply(auto simp add:sublist_Cons) |
|
2439 |
done |
|
2440 |
||
2441 |
||
15045 | 2442 |
lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l" |
14208 | 2443 |
apply (induct l rule: rev_induct, simp) |
13145 | 2444 |
apply (simp split: nat_diff_split add: sublist_append) |
2445 |
done |
|
13114 | 2446 |
|
24526 | 2447 |
lemma filter_in_sublist: |
2448 |
"distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s" |
|
2449 |
proof (induct xs arbitrary: s) |
|
17501 | 2450 |
case Nil thus ?case by simp |
2451 |
next |
|
2452 |
case (Cons a xs) |
|
2453 |
moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto |
|
2454 |
ultimately show ?case by(simp add: sublist_Cons cong:filter_cong) |
|
2455 |
qed |
|
2456 |
||
13114 | 2457 |
|
19390 | 2458 |
subsubsection {* @{const splice} *} |
2459 |
||
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
2460 |
lemma splice_Nil2 [simp, code]: |
19390 | 2461 |
"splice xs [] = xs" |
2462 |
by (cases xs) simp_all |
|
2463 |
||
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
2464 |
lemma splice_Cons_Cons [simp, code]: |
19390 | 2465 |
"splice (x#xs) (y#ys) = x # y # splice xs ys" |
2466 |
by simp |
|
2467 |
||
19607
07eeb832f28d
introduced characters for code generator; some improved code lemmas for some list functions
haftmann
parents:
19585
diff
changeset
|
2468 |
declare splice.simps(2) [simp del, code del] |
19390 | 2469 |
|
24526 | 2470 |
lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys" |
2471 |
apply(induct xs arbitrary: ys) apply simp |
|
22793 | 2472 |
apply(case_tac ys) |
2473 |
apply auto |
|
2474 |
done |
|
2475 |
||
24616 | 2476 |
|
2477 |
subsection {*Sorting*} |
|
2478 |
||
24617 | 2479 |
text{* Currently it is not shown that @{const sort} returns a |
2480 |
permutation of its input because the nicest proof is via multisets, |
|
2481 |
which are not yet available. Alternatively one could define a function |
|
2482 |
that counts the number of occurrences of an element in a list and use |
|
2483 |
that instead of multisets to state the correctness property. *} |
|
2484 |
||
24616 | 2485 |
context linorder |
2486 |
begin |
|
2487 |
||
2488 |
lemma sorted_Cons: "sorted (x#xs) = (sorted xs & (ALL y:set xs. x \<^loc><= y))" |
|
2489 |
apply(induct xs arbitrary: x) apply simp |
|
2490 |
by simp (blast intro: order_trans) |
|
2491 |
||
2492 |
lemma sorted_append: |
|
2493 |
"sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<^loc>\<le>y))" |
|
2494 |
by (induct xs) (auto simp add:sorted_Cons) |
|
2495 |
||
2496 |
lemma set_insort: "set(insort x xs) = insert x (set xs)" |
|
2497 |
by (induct xs) auto |
|
2498 |
||
24617 | 2499 |
lemma set_sort[simp]: "set(sort xs) = set xs" |
24616 | 2500 |
by (induct xs) (simp_all add:set_insort) |
2501 |
||
2502 |
lemma distinct_insort: "distinct (insort x xs) = (x \<notin> set xs \<and> distinct xs)" |
|
2503 |
by(induct xs)(auto simp:set_insort) |
|
2504 |
||
24617 | 2505 |
lemma distinct_sort[simp]: "distinct (sort xs) = distinct xs" |
24616 | 2506 |
by(induct xs)(simp_all add:distinct_insort set_sort) |
2507 |
||
2508 |
lemma sorted_insort: "sorted (insort x xs) = sorted xs" |
|
2509 |
apply (induct xs) |
|
2510 |
apply(auto simp:sorted_Cons set_insort not_le less_imp_le) |
|
2511 |
done |
|
2512 |
||
2513 |
theorem sorted_sort[simp]: "sorted (sort xs)" |
|
2514 |
by (induct xs) (auto simp:sorted_insort) |
|
2515 |
||
24645 | 2516 |
lemma sorted_distinct_set_unique: |
2517 |
assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys" |
|
2518 |
shows "xs = ys" |
|
2519 |
proof - |
|
2520 |
from assms have 1: "length xs = length ys" by (metis distinct_card) |
|
2521 |
from assms show ?thesis |
|
2522 |
proof(induct rule:list_induct2[OF 1]) |
|
2523 |
case 1 show ?case by simp |
|
2524 |
next |
|
2525 |
case 2 thus ?case by (simp add:sorted_Cons) |
|
2526 |
(metis Diff_insert_absorb antisym insertE insert_iff) |
|
2527 |
qed |
|
2528 |
qed |
|
2529 |
||
2530 |
lemma finite_sorted_distinct_unique: |
|
2531 |
shows "finite A \<Longrightarrow> EX! xs. set xs = A & sorted xs & distinct xs" |
|
2532 |
apply(drule finite_distinct_list) |
|
2533 |
apply clarify |
|
2534 |
apply(rule_tac a="sort xs" in ex1I) |
|
2535 |
apply (auto simp: sorted_distinct_set_unique) |
|
2536 |
done |
|
2537 |
||
24616 | 2538 |
end |
2539 |
||
2540 |
||
24645 | 2541 |
subsubsection {* @{text upto}: the generic interval-list *} |
2542 |
||
2543 |
(* FIXME why does (in linorder) fail? *) |
|
2544 |
definition upto :: "'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a list" ("(1[_../_])") where |
|
2545 |
"upto i j == THE is. set is = {i..j} & sorted is & distinct is" |
|
2546 |
||
2547 |
(*The following lemmas could/should be generalized to discrete linear orders*) |
|
2548 |
||
2549 |
lemma set_upto[simp]: "set[a::int..b] = {a..b}" |
|
2550 |
apply(simp add:upto_def) |
|
2551 |
apply(rule the1I2) |
|
2552 |
apply(simp_all add: finite_sorted_distinct_unique) |
|
2553 |
done |
|
2554 |
||
2555 |
lemma upto_rec: "[i::int..j] = (if i <= j then i # [i+1..j] else [])" |
|
2556 |
apply(simp add:upto_def cong:conj_cong) |
|
2557 |
apply clarify |
|
2558 |
apply (rule the1_equality[OF finite_sorted_distinct_unique]) |
|
2559 |
apply simp |
|
2560 |
apply(rule the1I2[OF finite_sorted_distinct_unique]) |
|
2561 |
apply simp |
|
2562 |
apply (fastsimp simp: sorted_Cons) |
|
2563 |
done |
|
2564 |
||
2565 |
||
15392 | 2566 |
subsubsection {* @{text lists}: the list-forming operator over sets *} |
15302 | 2567 |
|
23740 | 2568 |
inductive_set |
22262 | 2569 |
lists :: "'a set => 'a list set" |
23740 | 2570 |
for A :: "'a set" |
2571 |
where |
|
2572 |
Nil [intro!]: "[]: lists A" |
|
24286
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
2573 |
| Cons [intro!,noatp]: "[| a: A;l: lists A|] ==> a#l : lists A" |
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
2574 |
|
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
2575 |
inductive_cases listsE [elim!,noatp]: "x#l : lists A" |
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
2576 |
inductive_cases listspE [elim!,noatp]: "listsp A (x # l)" |
23740 | 2577 |
|
2578 |
lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B" |
|
24349 | 2579 |
by (clarify, erule listsp.induct, blast+) |
22262 | 2580 |
|
23740 | 2581 |
lemmas lists_mono = listsp_mono [to_set] |
22262 | 2582 |
|
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
2583 |
lemma listsp_infI: |
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
2584 |
assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l |
24349 | 2585 |
by induct blast+ |
15302 | 2586 |
|
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
2587 |
lemmas lists_IntI = listsp_infI [to_set] |
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
2588 |
|
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
2589 |
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:
22262
diff
changeset
|
2590 |
proof (rule mono_inf [where f=listsp, THEN order_antisym]) |
22262 | 2591 |
show "mono listsp" by (simp add: mono_def listsp_mono) |
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
2592 |
show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro: listsp_infI) |
14388 | 2593 |
qed |
2594 |
||
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
2595 |
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:
22262
diff
changeset
|
2596 |
|
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
2597 |
lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set] |
22262 | 2598 |
|
2599 |
lemma append_in_listsp_conv [iff]: |
|
2600 |
"(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)" |
|
15302 | 2601 |
by (induct xs) auto |
2602 |
||
22262 | 2603 |
lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set] |
2604 |
||
2605 |
lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)" |
|
2606 |
-- {* eliminate @{text listsp} in favour of @{text set} *} |
|
15302 | 2607 |
by (induct xs) auto |
2608 |
||
22262 | 2609 |
lemmas in_lists_conv_set = in_listsp_conv_set [to_set] |
2610 |
||
24286
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
2611 |
lemma in_listspD [dest!,noatp]: "listsp A xs ==> \<forall>x\<in>set xs. A x" |
22262 | 2612 |
by (rule in_listsp_conv_set [THEN iffD1]) |
2613 |
||
24286
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
2614 |
lemmas in_listsD [dest!,noatp] = in_listspD [to_set] |
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
2615 |
|
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
2616 |
lemma in_listspI [intro!,noatp]: "\<forall>x\<in>set xs. A x ==> listsp A xs" |
22262 | 2617 |
by (rule in_listsp_conv_set [THEN iffD2]) |
2618 |
||
24286
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
2619 |
lemmas in_listsI [intro!,noatp] = in_listspI [to_set] |
15302 | 2620 |
|
2621 |
lemma lists_UNIV [simp]: "lists UNIV = UNIV" |
|
2622 |
by auto |
|
2623 |
||
17086 | 2624 |
|
2625 |
||
2626 |
subsubsection{* Inductive definition for membership *} |
|
2627 |
||
23740 | 2628 |
inductive ListMem :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" |
22262 | 2629 |
where |
2630 |
elem: "ListMem x (x # xs)" |
|
2631 |
| insert: "ListMem x xs \<Longrightarrow> ListMem x (y # xs)" |
|
2632 |
||
2633 |
lemma ListMem_iff: "(ListMem x xs) = (x \<in> set xs)" |
|
17086 | 2634 |
apply (rule iffI) |
2635 |
apply (induct set: ListMem) |
|
2636 |
apply auto |
|
2637 |
apply (induct xs) |
|
2638 |
apply (auto intro: ListMem.intros) |
|
2639 |
done |
|
2640 |
||
2641 |
||
2642 |
||
15392 | 2643 |
subsubsection{*Lists as Cartesian products*} |
15302 | 2644 |
|
2645 |
text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from |
|
2646 |
@{term A} and tail drawn from @{term Xs}.*} |
|
2647 |
||
2648 |
constdefs |
|
2649 |
set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" |
|
2650 |
"set_Cons A XS == {z. \<exists>x xs. z = x#xs & x \<in> A & xs \<in> XS}" |
|
2651 |
||
17724 | 2652 |
lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A" |
15302 | 2653 |
by (auto simp add: set_Cons_def) |
2654 |
||
2655 |
text{*Yields the set of lists, all of the same length as the argument and |
|
2656 |
with elements drawn from the corresponding element of the argument.*} |
|
2657 |
||
2658 |
consts listset :: "'a set list \<Rightarrow> 'a list set" |
|
2659 |
primrec |
|
2660 |
"listset [] = {[]}" |
|
2661 |
"listset(A#As) = set_Cons A (listset As)" |
|
2662 |
||
2663 |
||
15656 | 2664 |
subsection{*Relations on Lists*} |
2665 |
||
2666 |
subsubsection {* Length Lexicographic Ordering *} |
|
2667 |
||
2668 |
text{*These orderings preserve well-foundedness: shorter lists |
|
2669 |
precede longer lists. These ordering are not used in dictionaries.*} |
|
2670 |
||
2671 |
consts lexn :: "('a * 'a)set => nat => ('a list * 'a list)set" |
|
2672 |
--{*The lexicographic ordering for lists of the specified length*} |
|
15302 | 2673 |
primrec |
15656 | 2674 |
"lexn r 0 = {}" |
2675 |
"lexn r (Suc n) = |
|
2676 |
(prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int |
|
2677 |
{(xs,ys). length xs = Suc n \<and> length ys = Suc n}" |
|
15302 | 2678 |
|
2679 |
constdefs |
|
15656 | 2680 |
lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" |
2681 |
"lex r == \<Union>n. lexn r n" |
|
2682 |
--{*Holds only between lists of the same length*} |
|
2683 |
||
15693 | 2684 |
lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" |
2685 |
"lenlex r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))" |
|
15656 | 2686 |
--{*Compares lists by their length and then lexicographically*} |
15302 | 2687 |
|
2688 |
||
2689 |
lemma wf_lexn: "wf r ==> wf (lexn r n)" |
|
2690 |
apply (induct n, simp, simp) |
|
2691 |
apply(rule wf_subset) |
|
2692 |
prefer 2 apply (rule Int_lower1) |
|
2693 |
apply(rule wf_prod_fun_image) |
|
2694 |
prefer 2 apply (rule inj_onI, auto) |
|
2695 |
done |
|
2696 |
||
2697 |
lemma lexn_length: |
|
24526 | 2698 |
"(xs, ys) : lexn r n ==> length xs = n \<and> length ys = n" |
2699 |
by (induct n arbitrary: xs ys) auto |
|
15302 | 2700 |
|
2701 |
lemma wf_lex [intro!]: "wf r ==> wf (lex r)" |
|
2702 |
apply (unfold lex_def) |
|
2703 |
apply (rule wf_UN) |
|
2704 |
apply (blast intro: wf_lexn, clarify) |
|
2705 |
apply (rename_tac m n) |
|
2706 |
apply (subgoal_tac "m \<noteq> n") |
|
2707 |
prefer 2 apply blast |
|
2708 |
apply (blast dest: lexn_length not_sym) |
|
2709 |
done |
|
2710 |
||
2711 |
lemma lexn_conv: |
|
15656 | 2712 |
"lexn r n = |
2713 |
{(xs,ys). length xs = n \<and> length ys = n \<and> |
|
2714 |
(\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}" |
|
18423 | 2715 |
apply (induct n, simp) |
15302 | 2716 |
apply (simp add: image_Collect lex_prod_def, safe, blast) |
2717 |
apply (rule_tac x = "ab # xys" in exI, simp) |
|
2718 |
apply (case_tac xys, simp_all, blast) |
|
2719 |
done |
|
2720 |
||
2721 |
lemma lex_conv: |
|
15656 | 2722 |
"lex r = |
2723 |
{(xs,ys). length xs = length ys \<and> |
|
2724 |
(\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}" |
|
15302 | 2725 |
by (force simp add: lex_def lexn_conv) |
2726 |
||
15693 | 2727 |
lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)" |
2728 |
by (unfold lenlex_def) blast |
|
2729 |
||
2730 |
lemma lenlex_conv: |
|
2731 |
"lenlex r = {(xs,ys). length xs < length ys | |
|
15656 | 2732 |
length xs = length ys \<and> (xs, ys) : lex r}" |
19623 | 2733 |
by (simp add: lenlex_def diag_def lex_prod_def inv_image_def) |
15302 | 2734 |
|
2735 |
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r" |
|
2736 |
by (simp add: lex_conv) |
|
2737 |
||
2738 |
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r" |
|
2739 |
by (simp add:lex_conv) |
|
2740 |
||
18447 | 2741 |
lemma Cons_in_lex [simp]: |
15656 | 2742 |
"((x # xs, y # ys) : lex r) = |
2743 |
((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)" |
|
15302 | 2744 |
apply (simp add: lex_conv) |
2745 |
apply (rule iffI) |
|
2746 |
prefer 2 apply (blast intro: Cons_eq_appendI, clarify) |
|
2747 |
apply (case_tac xys, simp, simp) |
|
2748 |
apply blast |
|
2749 |
done |
|
2750 |
||
2751 |
||
15656 | 2752 |
subsubsection {* Lexicographic Ordering *} |
2753 |
||
2754 |
text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b". |
|
2755 |
This ordering does \emph{not} preserve well-foundedness. |
|
17090 | 2756 |
Author: N. Voelker, March 2005. *} |
15656 | 2757 |
|
2758 |
constdefs |
|
2759 |
lexord :: "('a * 'a)set \<Rightarrow> ('a list * 'a list) set" |
|
2760 |
"lexord r == {(x,y). \<exists> a v. y = x @ a # v \<or> |
|
2761 |
(\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}" |
|
2762 |
||
2763 |
lemma lexord_Nil_left[simp]: "([],y) \<in> lexord r = (\<exists> a x. y = a # x)" |
|
24349 | 2764 |
by (unfold lexord_def, induct_tac y, auto) |
15656 | 2765 |
|
2766 |
lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r" |
|
24349 | 2767 |
by (unfold lexord_def, induct_tac x, auto) |
15656 | 2768 |
|
2769 |
lemma lexord_cons_cons[simp]: |
|
2770 |
"((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))" |
|
2771 |
apply (unfold lexord_def, safe, simp_all) |
|
2772 |
apply (case_tac u, simp, simp) |
|
2773 |
apply (case_tac u, simp, clarsimp, blast, blast, clarsimp) |
|
2774 |
apply (erule_tac x="b # u" in allE) |
|
2775 |
by force |
|
2776 |
||
2777 |
lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons |
|
2778 |
||
2779 |
lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r" |
|
24349 | 2780 |
by (induct_tac x, auto) |
15656 | 2781 |
|
2782 |
lemma lexord_append_left_rightI: |
|
2783 |
"(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r" |
|
24349 | 2784 |
by (induct_tac u, auto) |
15656 | 2785 |
|
2786 |
lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r" |
|
24349 | 2787 |
by (induct x, auto) |
15656 | 2788 |
|
2789 |
lemma lexord_append_leftD: |
|
2790 |
"\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r" |
|
24349 | 2791 |
by (erule rev_mp, induct_tac x, auto) |
15656 | 2792 |
|
2793 |
lemma lexord_take_index_conv: |
|
2794 |
"((x,y) : lexord r) = |
|
2795 |
((length x < length y \<and> take (length x) y = x) \<or> |
|
2796 |
(\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))" |
|
2797 |
apply (unfold lexord_def Let_def, clarsimp) |
|
2798 |
apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2) |
|
2799 |
apply auto |
|
2800 |
apply (rule_tac x="hd (drop (length x) y)" in exI) |
|
2801 |
apply (rule_tac x="tl (drop (length x) y)" in exI) |
|
2802 |
apply (erule subst, simp add: min_def) |
|
2803 |
apply (rule_tac x ="length u" in exI, simp) |
|
2804 |
apply (rule_tac x ="take i x" in exI) |
|
2805 |
apply (rule_tac x ="x ! i" in exI) |
|
2806 |
apply (rule_tac x ="y ! i" in exI, safe) |
|
2807 |
apply (rule_tac x="drop (Suc i) x" in exI) |
|
2808 |
apply (drule sym, simp add: drop_Suc_conv_tl) |
|
2809 |
apply (rule_tac x="drop (Suc i) y" in exI) |
|
2810 |
by (simp add: drop_Suc_conv_tl) |
|
2811 |
||
2812 |
-- {* lexord is extension of partial ordering List.lex *} |
|
2813 |
lemma lexord_lex: " (x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)" |
|
2814 |
apply (rule_tac x = y in spec) |
|
2815 |
apply (induct_tac x, clarsimp) |
|
2816 |
by (clarify, case_tac x, simp, force) |
|
2817 |
||
2818 |
lemma lexord_irreflexive: "(! x. (x,x) \<notin> r) \<Longrightarrow> (y,y) \<notin> lexord r" |
|
2819 |
by (induct y, auto) |
|
2820 |
||
2821 |
lemma lexord_trans: |
|
2822 |
"\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r" |
|
2823 |
apply (erule rev_mp)+ |
|
2824 |
apply (rule_tac x = x in spec) |
|
2825 |
apply (rule_tac x = z in spec) |
|
2826 |
apply ( induct_tac y, simp, clarify) |
|
2827 |
apply (case_tac xa, erule ssubst) |
|
2828 |
apply (erule allE, erule allE) -- {* avoid simp recursion *} |
|
2829 |
apply (case_tac x, simp, simp) |
|
24632 | 2830 |
apply (case_tac x, erule allE, erule allE, simp) |
15656 | 2831 |
apply (erule_tac x = listb in allE) |
2832 |
apply (erule_tac x = lista in allE, simp) |
|
2833 |
apply (unfold trans_def) |
|
2834 |
by blast |
|
2835 |
||
2836 |
lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)" |
|
24349 | 2837 |
by (rule transI, drule lexord_trans, blast) |
15656 | 2838 |
|
2839 |
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" |
|
2840 |
apply (rule_tac x = y in spec) |
|
2841 |
apply (induct_tac x, rule allI) |
|
2842 |
apply (case_tac x, simp, simp) |
|
2843 |
apply (rule allI, case_tac x, simp, simp) |
|
2844 |
by blast |
|
2845 |
||
2846 |
||
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2847 |
subsection {* Lexicographic combination of measure functions *} |
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2848 |
|
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2849 |
text {* These are useful for termination proofs *} |
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2850 |
|
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2851 |
definition |
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2852 |
"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:
21079
diff
changeset
|
2853 |
|
21106
51599a81b308
Added "recdef_wf" and "simp" attribute to "wf_measures"
krauss
parents:
21103
diff
changeset
|
2854 |
lemma wf_measures[recdef_wf, simp]: "wf (measures fs)" |
24349 | 2855 |
unfolding measures_def |
2856 |
by blast |
|
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2857 |
|
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2858 |
lemma in_measures[simp]: |
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2859 |
"(x, y) \<in> measures [] = False" |
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2860 |
"(x, y) \<in> measures (f # fs) |
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2861 |
= (f x < f y \<or> (f x = f y \<and> (x, y) \<in> measures fs))" |
24349 | 2862 |
unfolding measures_def |
2863 |
by auto |
|
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2864 |
|
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2865 |
lemma measures_less: "f x < f y ==> (x, y) \<in> measures (f#fs)" |
24349 | 2866 |
by simp |
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2867 |
|
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2868 |
lemma measures_lesseq: "f x <= f y ==> (x, y) \<in> measures fs ==> (x, y) \<in> measures (f#fs)" |
24349 | 2869 |
by auto |
21103
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2870 |
|
367b4ad7c7cc
Added "measures" combinator for lexicographic combinations of multiple measures.
krauss
parents:
21079
diff
changeset
|
2871 |
|
15392 | 2872 |
subsubsection{*Lifting a Relation on List Elements to the Lists*} |
15302 | 2873 |
|
23740 | 2874 |
inductive_set |
2875 |
listrel :: "('a * 'a)set => ('a list * 'a list)set" |
|
2876 |
for r :: "('a * 'a)set" |
|
22262 | 2877 |
where |
23740 | 2878 |
Nil: "([],[]) \<in> listrel r" |
2879 |
| Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r" |
|
2880 |
||
2881 |
inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r" |
|
2882 |
inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r" |
|
2883 |
inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r" |
|
2884 |
inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r" |
|
15302 | 2885 |
|
2886 |
||
2887 |
lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s" |
|
2888 |
apply clarify |
|
23740 | 2889 |
apply (erule listrel.induct) |
2890 |
apply (blast intro: listrel.intros)+ |
|
15302 | 2891 |
done |
2892 |
||
2893 |
lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A" |
|
2894 |
apply clarify |
|
23740 | 2895 |
apply (erule listrel.induct, auto) |
15302 | 2896 |
done |
2897 |
||
2898 |
lemma listrel_refl: "refl A r \<Longrightarrow> refl (lists A) (listrel r)" |
|
2899 |
apply (simp add: refl_def listrel_subset Ball_def) |
|
2900 |
apply (rule allI) |
|
2901 |
apply (induct_tac x) |
|
23740 | 2902 |
apply (auto intro: listrel.intros) |
15302 | 2903 |
done |
2904 |
||
2905 |
lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" |
|
2906 |
apply (auto simp add: sym_def) |
|
23740 | 2907 |
apply (erule listrel.induct) |
2908 |
apply (blast intro: listrel.intros)+ |
|
15302 | 2909 |
done |
2910 |
||
2911 |
lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)" |
|
2912 |
apply (simp add: trans_def) |
|
2913 |
apply (intro allI) |
|
2914 |
apply (rule impI) |
|
23740 | 2915 |
apply (erule listrel.induct) |
2916 |
apply (blast intro: listrel.intros)+ |
|
15302 | 2917 |
done |
2918 |
||
2919 |
theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)" |
|
2920 |
by (simp add: equiv_def listrel_refl listrel_sym listrel_trans) |
|
2921 |
||
2922 |
lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}" |
|
23740 | 2923 |
by (blast intro: listrel.intros) |
15302 | 2924 |
|
2925 |
lemma listrel_Cons: |
|
2926 |
"listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})"; |
|
23740 | 2927 |
by (auto simp add: set_Cons_def intro: listrel.intros) |
15302 | 2928 |
|
2929 |
||
15392 | 2930 |
subsection{*Miscellany*} |
2931 |
||
2932 |
subsubsection {* Characters and strings *} |
|
13366 | 2933 |
|
2934 |
datatype nibble = |
|
2935 |
Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7 |
|
2936 |
| Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF |
|
2937 |
||
2938 |
datatype char = Char nibble nibble |
|
2939 |
-- "Note: canonical order of character encoding coincides with standard term ordering" |
|
2940 |
||
2941 |
types string = "char list" |
|
2942 |
||
2943 |
syntax |
|
2944 |
"_Char" :: "xstr => char" ("CHR _") |
|
2945 |
"_String" :: "xstr => string" ("_") |
|
2946 |
||
21754
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset
|
2947 |
setup StringSyntax.setup |
13366 | 2948 |
|
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
2949 |
|
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
2950 |
subsection {* Code generator *} |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
2951 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
2952 |
subsubsection {* Setup *} |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2953 |
|
16770
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2954 |
types_code |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2955 |
"list" ("_ list") |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2956 |
attach (term_of) {* |
21760 | 2957 |
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:
16634
diff
changeset
|
2958 |
*} |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2959 |
attach (test) {* |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2960 |
fun gen_list' aG i j = frequency |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2961 |
[(i, fn () => aG j :: gen_list' aG (i-1) j), (1, fn () => [])] () |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2962 |
and gen_list aG i = gen_list' aG i i; |
16770
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2963 |
*} |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2964 |
"char" ("string") |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2965 |
attach (term_of) {* |
24130 | 2966 |
val term_of_char = HOLogic.mk_char o ord; |
16770
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2967 |
*} |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2968 |
attach (test) {* |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2969 |
fun gen_char i = chr (random_range (ord "a") (Int.min (ord "a" + i, ord "z"))); |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2970 |
*} |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2971 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2972 |
consts_code "Cons" ("(_ ::/ _)") |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2973 |
|
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
2974 |
code_type list |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
2975 |
(SML "_ list") |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21891
diff
changeset
|
2976 |
(OCaml "_ list") |
21113 | 2977 |
(Haskell "![_]") |
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
2978 |
|
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2979 |
code_reserved SML |
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2980 |
list |
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2981 |
|
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2982 |
code_reserved OCaml |
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2983 |
list |
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2984 |
|
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
2985 |
code_const Nil |
21113 | 2986 |
(SML "[]") |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21891
diff
changeset
|
2987 |
(OCaml "[]") |
21113 | 2988 |
(Haskell "[]") |
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
2989 |
|
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21891
diff
changeset
|
2990 |
setup {* |
24219 | 2991 |
fold (fn target => CodeTarget.add_pretty_list target |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2992 |
@{const_name Nil} @{const_name Cons} |
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2993 |
) ["SML", "OCaml", "Haskell"] |
21911
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21891
diff
changeset
|
2994 |
*} |
e29bcab0c81c
added OCaml code generation (without dictionaries)
haftmann
parents:
21891
diff
changeset
|
2995 |
|
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2996 |
code_instance list :: eq |
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
2997 |
(Haskell -) |
20588 | 2998 |
|
21455 | 2999 |
code_const "op = \<Colon> 'a\<Colon>eq list \<Rightarrow> 'a list \<Rightarrow> bool" |
20588 | 3000 |
(Haskell infixl 4 "==") |
3001 |
||
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3002 |
setup {* |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3003 |
let |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3004 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3005 |
fun list_codegen thy defs gr dep thyname b t = |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3006 |
let val (gr', ps) = foldl_map (Codegen.invoke_codegen thy defs dep thyname false) |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3007 |
(gr, HOLogic.dest_list t) |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3008 |
in SOME (gr', Pretty.list "[" "]" ps) end handle TERM _ => NONE; |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3009 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3010 |
fun char_codegen thy defs gr dep thyname b t = |
22539
ad3bd3d6ba8a
Improved code generator for characters: now handles
berghofe
parents:
22506
diff
changeset
|
3011 |
case Option.map chr (try HOLogic.dest_char t) of |
ad3bd3d6ba8a
Improved code generator for characters: now handles
berghofe
parents:
22506
diff
changeset
|
3012 |
SOME c => SOME (gr, Pretty.quote (Pretty.str (ML_Syntax.print_char c))) |
20453
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3013 |
| NONE => NONE; |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3014 |
|
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3015 |
in |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3016 |
Codegen.add_codegen "list_codegen" list_codegen |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3017 |
#> Codegen.add_codegen "char_codegen" char_codegen |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3018 |
end; |
855f07fabd76
final syntax for some Isar code generator keywords
haftmann
parents:
20439
diff
changeset
|
3019 |
*} |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
3020 |
|
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3021 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3022 |
subsubsection {* Generation of efficient code *} |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3023 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3024 |
consts |
23740 | 3025 |
member :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" (infixl "mem" 55) |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3026 |
null:: "'a list \<Rightarrow> bool" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3027 |
list_inter :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3028 |
list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3029 |
list_all :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<Rightarrow> bool)" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3030 |
filtermap :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3031 |
map_filter :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3032 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3033 |
primrec |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3034 |
"x mem [] = False" |
21079 | 3035 |
"x mem (y#ys) = (x = y \<or> x mem ys)" |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3036 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3037 |
primrec |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3038 |
"null [] = True" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3039 |
"null (x#xs) = False" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3040 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3041 |
primrec |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3042 |
"list_inter [] bs = []" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3043 |
"list_inter (a#as) bs = |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3044 |
(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:
21046
diff
changeset
|
3045 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3046 |
primrec |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3047 |
"list_all P [] = True" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3048 |
"list_all P (x#xs) = (P x \<and> list_all P xs)" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3049 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3050 |
primrec |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3051 |
"list_ex P [] = False" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3052 |
"list_ex P (x#xs) = (P x \<or> list_ex P xs)" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3053 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3054 |
primrec |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3055 |
"filtermap f [] = []" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3056 |
"filtermap f (x#xs) = |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3057 |
(case f x of None \<Rightarrow> filtermap f xs |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3058 |
| Some y \<Rightarrow> y # filtermap f xs)" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3059 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3060 |
primrec |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3061 |
"map_filter f P [] = []" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3062 |
"map_filter f P (x#xs) = |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3063 |
(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:
21046
diff
changeset
|
3064 |
|
23096 | 3065 |
|
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3066 |
text {* |
21754
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset
|
3067 |
Only use @{text mem} for generating executable code. Otherwise use |
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset
|
3068 |
@{prop "x : set xs"} instead --- it is much easier to reason about. |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3069 |
The same is true for @{const list_all} and @{const list_ex}: write |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3070 |
@{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:
21548
diff
changeset
|
3071 |
quantifiers are aleady known to the automatic provers. In fact, the |
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset
|
3072 |
declarations in the code subsection make sure that @{text "\<in>"}, |
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset
|
3073 |
@{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:
21548
diff
changeset
|
3074 |
efficiently. |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3075 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3076 |
Efficient emptyness check is implemented by @{const null}. |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3077 |
|
23060 | 3078 |
The functions @{const filtermap} and @{const map_filter} are just |
3079 |
there to generate efficient code. Do not use |
|
21754
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset
|
3080 |
them for modelling and proving. |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3081 |
*} |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3082 |
|
23060 | 3083 |
lemma rev_foldl_cons [code]: |
3084 |
"rev xs = foldl (\<lambda>xs x. x # xs) [] xs" |
|
3085 |
proof (induct xs) |
|
3086 |
case Nil then show ?case by simp |
|
3087 |
next |
|
3088 |
case Cons |
|
3089 |
{ |
|
3090 |
fix x xs ys |
|
3091 |
have "foldl (\<lambda>xs x. x # xs) ys xs @ [x] |
|
3092 |
= foldl (\<lambda>xs x. x # xs) (ys @ [x]) xs" |
|
3093 |
by (induct xs arbitrary: ys) auto |
|
3094 |
} |
|
3095 |
note aux = this |
|
3096 |
show ?case by (induct xs) (auto simp add: Cons aux) |
|
3097 |
qed |
|
3098 |
||
24166 | 3099 |
lemma mem_iff [code post]: |
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
3100 |
"x mem xs \<longleftrightarrow> x \<in> set xs" |
24349 | 3101 |
by (induct xs) auto |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3102 |
|
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3103 |
lemmas in_set_code [code unfold] = mem_iff [symmetric] |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3104 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3105 |
lemma empty_null [code inline]: |
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
3106 |
"xs = [] \<longleftrightarrow> null xs" |
24349 | 3107 |
by (cases xs) simp_all |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3108 |
|
24166 | 3109 |
lemmas null_empty [code post] = |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3110 |
empty_null [symmetric] |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3111 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3112 |
lemma list_inter_conv: |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3113 |
"set (list_inter xs ys) = set xs \<inter> set ys" |
24349 | 3114 |
by (induct xs) auto |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3115 |
|
24166 | 3116 |
lemma list_all_iff [code post]: |
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
3117 |
"list_all P xs \<longleftrightarrow> (\<forall>x \<in> set xs. P x)" |
24349 | 3118 |
by (induct xs) auto |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3119 |
|
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3120 |
lemmas list_ball_code [code unfold] = list_all_iff [symmetric] |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3121 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3122 |
lemma list_all_append [simp]: |
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
3123 |
"list_all P (xs @ ys) \<longleftrightarrow> (list_all P xs \<and> list_all P ys)" |
24349 | 3124 |
by (induct xs) auto |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3125 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3126 |
lemma list_all_rev [simp]: |
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
3127 |
"list_all P (rev xs) \<longleftrightarrow> list_all P xs" |
24349 | 3128 |
by (simp add: list_all_iff) |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3129 |
|
22506 | 3130 |
lemma list_all_length: |
3131 |
"list_all P xs \<longleftrightarrow> (\<forall>n < length xs. P (xs ! n))" |
|
3132 |
unfolding list_all_iff by (auto intro: all_nth_imp_all_set) |
|
3133 |
||
24166 | 3134 |
lemma list_ex_iff [code post]: |
22422
ee19cdb07528
stepping towards uniform lattice theory development in HOL
haftmann
parents:
22262
diff
changeset
|
3135 |
"list_ex P xs \<longleftrightarrow> (\<exists>x \<in> set xs. P x)" |
24349 | 3136 |
by (induct xs) simp_all |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3137 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3138 |
lemmas list_bex_code [code unfold] = |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3139 |
list_ex_iff [symmetric] |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3140 |
|
22506 | 3141 |
lemma list_ex_length: |
3142 |
"list_ex P xs \<longleftrightarrow> (\<exists>n < length xs. P (xs ! n))" |
|
3143 |
unfolding list_ex_iff set_conv_nth by auto |
|
3144 |
||
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3145 |
lemma filtermap_conv: |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3146 |
"filtermap f xs = map (\<lambda>x. the (f x)) (filter (\<lambda>x. f x \<noteq> None) xs)" |
24349 | 3147 |
by (induct xs) (simp_all split: option.split) |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3148 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3149 |
lemma map_filter_conv [simp]: |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3150 |
"map_filter f P xs = map f (filter P xs)" |
24349 | 3151 |
by (induct xs) auto |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
3152 |
|
24449 | 3153 |
|
3154 |
text {* Code for bounded quantification and summation over nats. *} |
|
21891
b4e4ea3db161
added code lemmas for quantification over bounded nats
haftmann
parents:
21871
diff
changeset
|
3155 |
|
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3156 |
lemma atMost_upto [code unfold]: |
24645 | 3157 |
"{..n} = set [0..<Suc n]" |
24349 | 3158 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3159 |
|
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3160 |
lemma atLeast_upt [code unfold]: |
21891
b4e4ea3db161
added code lemmas for quantification over bounded nats
haftmann
parents:
21871
diff
changeset
|
3161 |
"{..<n} = set [0..<n]" |
24349 | 3162 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3163 |
|
24449 | 3164 |
lemma greaterThanLessThan_upt [code unfold]: |
21891
b4e4ea3db161
added code lemmas for quantification over bounded nats
haftmann
parents:
21871
diff
changeset
|
3165 |
"{n<..<m} = set [Suc n..<m]" |
24349 | 3166 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3167 |
|
24449 | 3168 |
lemma atLeastLessThan_upt [code unfold]: |
21891
b4e4ea3db161
added code lemmas for quantification over bounded nats
haftmann
parents:
21871
diff
changeset
|
3169 |
"{n..<m} = set [n..<m]" |
24349 | 3170 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3171 |
|
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3172 |
lemma greaterThanAtMost_upto [code unfold]: |
24645 | 3173 |
"{n<..m} = set [Suc n..<Suc m]" |
24349 | 3174 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3175 |
|
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3176 |
lemma atLeastAtMost_upto [code unfold]: |
24645 | 3177 |
"{n..m} = set [n..<Suc m]" |
24349 | 3178 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3179 |
|
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3180 |
lemma all_nat_less_eq [code unfold]: |
21891
b4e4ea3db161
added code lemmas for quantification over bounded nats
haftmann
parents:
21871
diff
changeset
|
3181 |
"(\<forall>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..<n}. P m)" |
24349 | 3182 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3183 |
|
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3184 |
lemma ex_nat_less_eq [code unfold]: |
21891
b4e4ea3db161
added code lemmas for quantification over bounded nats
haftmann
parents:
21871
diff
changeset
|
3185 |
"(\<exists>m<n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..<n}. P m)" |
24349 | 3186 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3187 |
|
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3188 |
lemma all_nat_less [code unfold]: |
21891
b4e4ea3db161
added code lemmas for quantification over bounded nats
haftmann
parents:
21871
diff
changeset
|
3189 |
"(\<forall>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<forall>m \<in> {0..n}. P m)" |
24349 | 3190 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3191 |
|
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3192 |
lemma ex_nat_less [code unfold]: |
21891
b4e4ea3db161
added code lemmas for quantification over bounded nats
haftmann
parents:
21871
diff
changeset
|
3193 |
"(\<exists>m\<le>n\<Colon>nat. P m) \<longleftrightarrow> (\<exists>m \<in> {0..n}. P m)" |
24349 | 3194 |
by auto |
22799
ed7d53db2170
moved code generation pretty integers and characters to separate theories
haftmann
parents:
22793
diff
changeset
|
3195 |
|
24449 | 3196 |
lemma setsum_set_upt_conv_listsum[code unfold]: |
3197 |
"setsum f (set[k..<n]) = listsum (map f [k..<n])" |
|
3198 |
apply(subst atLeastLessThan_upt[symmetric]) |
|
3199 |
by (induct n) simp_all |
|
3200 |
||
23388 | 3201 |
subsubsection {* List partitioning *} |
3202 |
||
3203 |
consts |
|
3204 |
partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" |
|
23246 | 3205 |
primrec |
23388 | 3206 |
"partition P [] = ([], [])" |
3207 |
"partition P (x # xs) = |
|
3208 |
(let (yes, no) = partition P xs |
|
3209 |
in if P x then (x # yes, no) else (yes, x # no))" |
|
23246 | 3210 |
|
3211 |
lemma partition_P: |
|
23388 | 3212 |
"partition P xs = (yes, no) \<Longrightarrow> (\<forall>p\<in> set yes. P p) \<and> (\<forall>p\<in> set no. \<not> P p)" |
3213 |
proof (induct xs arbitrary: yes no rule: partition.induct) |
|
3214 |
case Nil then show ?case by simp |
|
3215 |
next |
|
3216 |
case (Cons a as) |
|
3217 |
let ?p = "partition P as" |
|
3218 |
let ?p' = "partition P (a # as)" |
|
3219 |
note prem = `?p' = (yes, no)` |
|
3220 |
show ?case |
|
3221 |
proof (cases "P a") |
|
3222 |
case True |
|
3223 |
with prem have yes: "yes = a # fst ?p" and no: "no = snd ?p" |
|
3224 |
by (simp_all add: Let_def split_def) |
|
3225 |
have "(\<forall>p\<in> set (fst ?p). P p) \<and> (\<forall>p\<in> set no. \<not> P p)" |
|
3226 |
by (rule Cons.hyps) (simp add: yes no) |
|
3227 |
with True yes show ?thesis by simp |
|
3228 |
next |
|
3229 |
case False |
|
3230 |
with prem have yes: "yes = fst ?p" and no: "no = a # snd ?p" |
|
3231 |
by (simp_all add: Let_def split_def) |
|
3232 |
have "(\<forall>p\<in> set yes. P p) \<and> (\<forall>p\<in> set (snd ?p). \<not> P p)" |
|
3233 |
by (rule Cons.hyps) (simp add: yes no) |
|
3234 |
with False no show ?thesis by simp |
|
3235 |
qed |
|
3236 |
qed |
|
23246 | 3237 |
|
3238 |
lemma partition_filter1: |
|
23388 | 3239 |
"fst (partition P xs) = filter P xs" |
24349 | 3240 |
by (induct xs rule: partition.induct) (auto simp add: Let_def split_def) |
23246 | 3241 |
|
3242 |
lemma partition_filter2: |
|
23388 | 3243 |
"snd (partition P xs) = filter (Not o P) xs" |
24349 | 3244 |
by (induct xs rule: partition.induct) (auto simp add: Let_def split_def) |
23388 | 3245 |
|
3246 |
lemma partition_set: |
|
3247 |
assumes "partition P xs = (yes, no)" |
|
3248 |
shows "set yes \<union> set no = set xs" |
|
3249 |
proof - |
|
3250 |
have "set xs = {x. x \<in> set xs \<and> P x} \<union> {x. x \<in> set xs \<and> \<not> P x}" by blast |
|
3251 |
also have "\<dots> = set (List.filter P xs) Un (set (List.filter (Not o P) xs))" by simp |
|
3252 |
also have "\<dots> = set (fst (partition P xs)) \<union> set (snd (partition P xs))" |
|
3253 |
using partition_filter1 [of P xs] partition_filter2 [of P xs] by simp |
|
3254 |
finally show "set yes Un set no = set xs" using assms by simp |
|
23246 | 3255 |
qed |
3256 |
||
23388 | 3257 |
end |