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