author | schirmer |
Fri, 17 Mar 2006 17:38:38 +0100 | |
changeset 19284 | 4c86109423d5 |
parent 19138 | 42ff710d432f |
child 19302 | e1bda4fc1d1d |
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 |
15140 | 9 |
imports PreList |
15131 | 10 |
begin |
923 | 11 |
|
13142 | 12 |
datatype 'a list = |
13366 | 13 |
Nil ("[]") |
14 |
| Cons 'a "'a list" (infixr "#" 65) |
|
923 | 15 |
|
15392 | 16 |
subsection{*Basic list processing functions*} |
15302 | 17 |
|
923 | 18 |
consts |
13366 | 19 |
"@" :: "'a list => 'a list => 'a list" (infixr 65) |
20 |
filter:: "('a => bool) => 'a list => 'a list" |
|
21 |
concat:: "'a list list => 'a list" |
|
22 |
foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b" |
|
23 |
foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b" |
|
24 |
hd:: "'a list => 'a" |
|
25 |
tl:: "'a list => 'a list" |
|
26 |
last:: "'a list => 'a" |
|
27 |
butlast :: "'a list => 'a list" |
|
28 |
set :: "'a list => 'a set" |
|
29 |
list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" |
|
30 |
map :: "('a=>'b) => ('a list => 'b list)" |
|
31 |
nth :: "'a list => nat => 'a" (infixl "!" 100) |
|
32 |
list_update :: "'a list => nat => 'a => 'a list" |
|
33 |
take:: "nat => 'a list => 'a list" |
|
34 |
drop:: "nat => 'a list => 'a list" |
|
35 |
takeWhile :: "('a => bool) => 'a list => 'a list" |
|
36 |
dropWhile :: "('a => bool) => 'a list => 'a list" |
|
37 |
rev :: "'a list => 'a list" |
|
38 |
zip :: "'a list => 'b list => ('a * 'b) list" |
|
15425 | 39 |
upt :: "nat => nat => nat list" ("(1[_..</_'])") |
13366 | 40 |
remdups :: "'a list => 'a list" |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
41 |
remove1 :: "'a => 'a list => 'a list" |
13366 | 42 |
null:: "'a list => bool" |
43 |
"distinct":: "'a list => bool" |
|
44 |
replicate :: "nat => 'a => 'a list" |
|
15302 | 45 |
rotate1 :: "'a list \<Rightarrow> 'a list" |
46 |
rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
47 |
sublist :: "'a list => nat set => 'a list" |
|
17086 | 48 |
(* For efficiency *) |
49 |
mem :: "'a => 'a list => bool" (infixl 55) |
|
50 |
list_inter :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
51 |
list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool" |
|
52 |
list_all:: "('a => bool) => ('a list => bool)" |
|
53 |
itrev :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
|
54 |
filtermap :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list" |
|
55 |
map_filter :: "('a => 'b) => ('a => bool) => 'a list => 'b list" |
|
15302 | 56 |
|
923 | 57 |
|
13146 | 58 |
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
|
59 |
|
923 | 60 |
syntax |
13366 | 61 |
-- {* list Enumeration *} |
62 |
"@list" :: "args => 'a list" ("[(_)]") |
|
923 | 63 |
|
13366 | 64 |
-- {* Special syntax for filter *} |
65 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list" ("(1[_:_./ _])") |
|
923 | 66 |
|
13366 | 67 |
-- {* list update *} |
68 |
"_lupdbind":: "['a, 'a] => lupdbind" ("(2_ :=/ _)") |
|
69 |
"" :: "lupdbind => lupdbinds" ("_") |
|
70 |
"_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _") |
|
71 |
"_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
|
72 |
|
13366 | 73 |
upto:: "nat => nat => nat list" ("(1[_../_])") |
5427 | 74 |
|
923 | 75 |
translations |
13366 | 76 |
"[x, xs]" == "x#[xs]" |
77 |
"[x]" == "x#[]" |
|
78 |
"[x:xs . P]"== "filter (%x. P) xs" |
|
923 | 79 |
|
13366 | 80 |
"_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs" |
81 |
"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
|
82 |
|
15425 | 83 |
"[i..j]" == "[i..<(Suc j)]" |
5427 | 84 |
|
85 |
||
12114
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
wenzelm
parents:
10832
diff
changeset
|
86 |
syntax (xsymbols) |
13366 | 87 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") |
14565 | 88 |
syntax (HTML output) |
89 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") |
|
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
90 |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
91 |
|
13142 | 92 |
text {* |
14589 | 93 |
Function @{text size} is overloaded for all datatypes. Users may |
13366 | 94 |
refer to the list version as @{text length}. *} |
13142 | 95 |
|
96 |
syntax length :: "'a list => nat" |
|
97 |
translations "length" => "size :: _ list => nat" |
|
13114 | 98 |
|
13142 | 99 |
typed_print_translation {* |
13366 | 100 |
let |
101 |
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] = |
|
102 |
Syntax.const "length" $ t |
|
103 |
| size_tr' _ _ _ = raise Match; |
|
104 |
in [("size", size_tr')] end |
|
13114 | 105 |
*} |
3437
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
106 |
|
15302 | 107 |
|
5183 | 108 |
primrec |
15307 | 109 |
"hd(x#xs) = x" |
110 |
||
5183 | 111 |
primrec |
15307 | 112 |
"tl([]) = []" |
113 |
"tl(x#xs) = xs" |
|
114 |
||
5183 | 115 |
primrec |
15307 | 116 |
"null([]) = True" |
117 |
"null(x#xs) = False" |
|
118 |
||
8972 | 119 |
primrec |
15307 | 120 |
"last(x#xs) = (if xs=[] then x else last xs)" |
121 |
||
5183 | 122 |
primrec |
15307 | 123 |
"butlast []= []" |
124 |
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" |
|
125 |
||
5183 | 126 |
primrec |
15307 | 127 |
"set [] = {}" |
128 |
"set (x#xs) = insert x (set xs)" |
|
129 |
||
5183 | 130 |
primrec |
15307 | 131 |
"map f [] = []" |
132 |
"map f (x#xs) = f(x)#map f xs" |
|
133 |
||
5183 | 134 |
primrec |
15307 | 135 |
append_Nil:"[]@ys = ys" |
136 |
append_Cons: "(x#xs)@ys = x#(xs@ys)" |
|
137 |
||
5183 | 138 |
primrec |
15307 | 139 |
"rev([]) = []" |
140 |
"rev(x#xs) = rev(xs) @ [x]" |
|
141 |
||
5183 | 142 |
primrec |
15307 | 143 |
"filter P [] = []" |
144 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
145 |
||
5183 | 146 |
primrec |
15307 | 147 |
foldl_Nil:"foldl f a [] = a" |
148 |
foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" |
|
149 |
||
8000 | 150 |
primrec |
15307 | 151 |
"foldr f [] a = a" |
152 |
"foldr f (x#xs) a = f x (foldr f xs a)" |
|
153 |
||
5183 | 154 |
primrec |
15307 | 155 |
"concat([]) = []" |
156 |
"concat(x#xs) = x @ concat(xs)" |
|
157 |
||
5183 | 158 |
primrec |
15307 | 159 |
drop_Nil:"drop n [] = []" |
160 |
drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" |
|
161 |
-- {*Warning: simpset does not contain this definition, but separate |
|
162 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
163 |
||
5183 | 164 |
primrec |
15307 | 165 |
take_Nil:"take n [] = []" |
166 |
take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
|
167 |
-- {*Warning: simpset does not contain this definition, but separate |
|
168 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
169 |
||
13142 | 170 |
primrec |
15307 | 171 |
nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)" |
172 |
-- {*Warning: simpset does not contain this definition, but separate |
|
173 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
174 |
||
5183 | 175 |
primrec |
15307 | 176 |
"[][i:=v] = []" |
177 |
"(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])" |
|
178 |
||
179 |
primrec |
|
180 |
"takeWhile P [] = []" |
|
181 |
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" |
|
182 |
||
5183 | 183 |
primrec |
15307 | 184 |
"dropWhile P [] = []" |
185 |
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" |
|
186 |
||
5183 | 187 |
primrec |
15307 | 188 |
"zip xs [] = []" |
189 |
zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)" |
|
190 |
-- {*Warning: simpset does not contain this definition, but separate |
|
191 |
theorems for @{text "xs = []"} and @{text "xs = z # zs"} *} |
|
192 |
||
5427 | 193 |
primrec |
15425 | 194 |
upt_0: "[i..<0] = []" |
195 |
upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])" |
|
15307 | 196 |
|
5183 | 197 |
primrec |
15307 | 198 |
"distinct [] = True" |
199 |
"distinct (x#xs) = (x ~: set xs \<and> distinct xs)" |
|
200 |
||
5183 | 201 |
primrec |
15307 | 202 |
"remdups [] = []" |
203 |
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" |
|
204 |
||
5183 | 205 |
primrec |
15307 | 206 |
"remove1 x [] = []" |
207 |
"remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)" |
|
208 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
209 |
primrec |
15307 | 210 |
replicate_0: "replicate 0 x = []" |
211 |
replicate_Suc: "replicate (Suc n) x = x # replicate n x" |
|
212 |
||
8115 | 213 |
defs |
15302 | 214 |
rotate1_def: "rotate1 xs == (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])" |
215 |
rotate_def: "rotate n == rotate1 ^ n" |
|
216 |
||
217 |
list_all2_def: |
|
218 |
"list_all2 P xs ys == |
|
219 |
length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)" |
|
220 |
||
221 |
sublist_def: |
|
15425 | 222 |
"sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..<size xs]))" |
5281 | 223 |
|
17086 | 224 |
primrec |
225 |
"x mem [] = False" |
|
226 |
"x mem (y#ys) = (if y=x then True else x mem ys)" |
|
227 |
||
228 |
primrec |
|
229 |
"list_inter [] bs = []" |
|
230 |
"list_inter (a#as) bs = |
|
231 |
(if a \<in> set bs then a#(list_inter as bs) else list_inter as bs)" |
|
232 |
||
233 |
primrec |
|
234 |
"list_all P [] = True" |
|
235 |
"list_all P (x#xs) = (P(x) \<and> list_all P xs)" |
|
236 |
||
237 |
primrec |
|
238 |
"list_ex P [] = False" |
|
239 |
"list_ex P (x#xs) = (P x \<or> list_ex P xs)" |
|
240 |
||
241 |
primrec |
|
242 |
"filtermap f [] = []" |
|
243 |
"filtermap f (x#xs) = |
|
244 |
(case f x of None \<Rightarrow> filtermap f xs |
|
245 |
| Some y \<Rightarrow> y # (filtermap f xs))" |
|
246 |
||
247 |
primrec |
|
248 |
"map_filter f P [] = []" |
|
249 |
"map_filter f P (x#xs) = (if P x then f x # map_filter f P xs else |
|
250 |
map_filter f P xs)" |
|
251 |
||
252 |
primrec |
|
253 |
"itrev [] ys = ys" |
|
254 |
"itrev (x#xs) ys = itrev xs (x#ys)" |
|
255 |
||
13114 | 256 |
|
13142 | 257 |
lemma not_Cons_self [simp]: "xs \<noteq> x # xs" |
13145 | 258 |
by (induct xs) auto |
13114 | 259 |
|
13142 | 260 |
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric] |
13114 | 261 |
|
13142 | 262 |
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" |
13145 | 263 |
by (induct xs) auto |
13114 | 264 |
|
13142 | 265 |
lemma length_induct: |
13145 | 266 |
"(!!xs. \<forall>ys. length ys < length xs --> P ys ==> P xs) ==> P xs" |
17589 | 267 |
by (rule measure_induct [of length]) iprover |
13114 | 268 |
|
269 |
||
15392 | 270 |
subsubsection {* @{text length} *} |
13114 | 271 |
|
13142 | 272 |
text {* |
13145 | 273 |
Needs to come before @{text "@"} because of theorem @{text |
274 |
append_eq_append_conv}. |
|
13142 | 275 |
*} |
13114 | 276 |
|
13142 | 277 |
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" |
13145 | 278 |
by (induct xs) auto |
13114 | 279 |
|
13142 | 280 |
lemma length_map [simp]: "length (map f xs) = length xs" |
13145 | 281 |
by (induct xs) auto |
13114 | 282 |
|
13142 | 283 |
lemma length_rev [simp]: "length (rev xs) = length xs" |
13145 | 284 |
by (induct xs) auto |
13114 | 285 |
|
13142 | 286 |
lemma length_tl [simp]: "length (tl xs) = length xs - 1" |
13145 | 287 |
by (cases xs) auto |
13114 | 288 |
|
13142 | 289 |
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" |
13145 | 290 |
by (induct xs) auto |
13114 | 291 |
|
13142 | 292 |
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" |
13145 | 293 |
by (induct xs) auto |
13114 | 294 |
|
295 |
lemma length_Suc_conv: |
|
13145 | 296 |
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
297 |
by (induct xs) auto |
|
13142 | 298 |
|
14025 | 299 |
lemma Suc_length_conv: |
300 |
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
|
14208 | 301 |
apply (induct xs, simp, simp) |
14025 | 302 |
apply blast |
303 |
done |
|
304 |
||
14099 | 305 |
lemma impossible_Cons [rule_format]: |
306 |
"length xs <= length ys --> xs = x # ys = False" |
|
14208 | 307 |
apply (induct xs, auto) |
14099 | 308 |
done |
309 |
||
14247 | 310 |
lemma list_induct2[consumes 1]: "\<And>ys. |
311 |
\<lbrakk> length xs = length ys; |
|
312 |
P [] []; |
|
313 |
\<And>x xs y ys. \<lbrakk> length xs = length ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk> |
|
314 |
\<Longrightarrow> P xs ys" |
|
315 |
apply(induct xs) |
|
316 |
apply simp |
|
317 |
apply(case_tac ys) |
|
318 |
apply simp |
|
319 |
apply(simp) |
|
320 |
done |
|
13114 | 321 |
|
15392 | 322 |
subsubsection {* @{text "@"} -- append *} |
13114 | 323 |
|
13142 | 324 |
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" |
13145 | 325 |
by (induct xs) auto |
13114 | 326 |
|
13142 | 327 |
lemma append_Nil2 [simp]: "xs @ [] = xs" |
13145 | 328 |
by (induct xs) auto |
3507 | 329 |
|
13142 | 330 |
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" |
13145 | 331 |
by (induct xs) auto |
13114 | 332 |
|
13142 | 333 |
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" |
13145 | 334 |
by (induct xs) auto |
13114 | 335 |
|
13142 | 336 |
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" |
13145 | 337 |
by (induct xs) auto |
13114 | 338 |
|
13142 | 339 |
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" |
13145 | 340 |
by (induct xs) auto |
13114 | 341 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
342 |
lemma append_eq_append_conv [simp]: |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
343 |
"!!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
|
344 |
==> (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
|
345 |
apply (induct xs) |
14208 | 346 |
apply (case_tac ys, simp, force) |
347 |
apply (case_tac ys, force, simp) |
|
13145 | 348 |
done |
13142 | 349 |
|
14495 | 350 |
lemma append_eq_append_conv2: "!!ys zs ts. |
351 |
(xs @ ys = zs @ ts) = |
|
352 |
(EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)" |
|
353 |
apply (induct xs) |
|
354 |
apply fastsimp |
|
355 |
apply(case_tac zs) |
|
356 |
apply simp |
|
357 |
apply fastsimp |
|
358 |
done |
|
359 |
||
13142 | 360 |
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)" |
13145 | 361 |
by simp |
13142 | 362 |
|
363 |
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" |
|
13145 | 364 |
by simp |
13114 | 365 |
|
13142 | 366 |
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)" |
13145 | 367 |
by simp |
13114 | 368 |
|
13142 | 369 |
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" |
13145 | 370 |
using append_same_eq [of _ _ "[]"] by auto |
3507 | 371 |
|
13142 | 372 |
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" |
13145 | 373 |
using append_same_eq [of "[]"] by auto |
13114 | 374 |
|
13142 | 375 |
lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs" |
13145 | 376 |
by (induct xs) auto |
13114 | 377 |
|
13142 | 378 |
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" |
13145 | 379 |
by (induct xs) auto |
13114 | 380 |
|
13142 | 381 |
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" |
13145 | 382 |
by (simp add: hd_append split: list.split) |
13114 | 383 |
|
13142 | 384 |
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)" |
13145 | 385 |
by (simp split: list.split) |
13114 | 386 |
|
13142 | 387 |
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" |
13145 | 388 |
by (simp add: tl_append split: list.split) |
13114 | 389 |
|
390 |
||
14300 | 391 |
lemma Cons_eq_append_conv: "x#xs = ys@zs = |
392 |
(ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))" |
|
393 |
by(cases ys) auto |
|
394 |
||
15281 | 395 |
lemma append_eq_Cons_conv: "(ys@zs = x#xs) = |
396 |
(ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))" |
|
397 |
by(cases ys) auto |
|
398 |
||
14300 | 399 |
|
13142 | 400 |
text {* Trivial rules for solving @{text "@"}-equations automatically. *} |
13114 | 401 |
|
402 |
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" |
|
13145 | 403 |
by simp |
13114 | 404 |
|
13142 | 405 |
lemma Cons_eq_appendI: |
13145 | 406 |
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs" |
407 |
by (drule sym) simp |
|
13114 | 408 |
|
13142 | 409 |
lemma append_eq_appendI: |
13145 | 410 |
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us" |
411 |
by (drule sym) simp |
|
13114 | 412 |
|
413 |
||
13142 | 414 |
text {* |
13145 | 415 |
Simplification procedure for all list equalities. |
416 |
Currently only tries to rearrange @{text "@"} to see if |
|
417 |
- both lists end in a singleton list, |
|
418 |
- or both lists end in the same list. |
|
13142 | 419 |
*} |
420 |
||
421 |
ML_setup {* |
|
3507 | 422 |
local |
423 |
||
13122 | 424 |
val append_assoc = thm "append_assoc"; |
425 |
val append_Nil = thm "append_Nil"; |
|
426 |
val append_Cons = thm "append_Cons"; |
|
427 |
val append1_eq_conv = thm "append1_eq_conv"; |
|
428 |
val append_same_eq = thm "append_same_eq"; |
|
429 |
||
13114 | 430 |
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) = |
13462 | 431 |
(case xs of Const("List.list.Nil",_) => cons | _ => last xs) |
432 |
| last (Const("List.op @",_) $ _ $ ys) = last ys |
|
433 |
| last t = t; |
|
13114 | 434 |
|
435 |
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true |
|
13462 | 436 |
| list1 _ = false; |
13114 | 437 |
|
438 |
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) = |
|
13462 | 439 |
(case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs) |
440 |
| butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys |
|
441 |
| butlast xs = Const("List.list.Nil",fastype_of xs); |
|
13114 | 442 |
|
16973 | 443 |
val rearr_ss = HOL_basic_ss addsimps [append_assoc, append_Nil, append_Cons]; |
444 |
||
445 |
fun list_eq sg ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) = |
|
13462 | 446 |
let |
447 |
val lastl = last lhs and lastr = last rhs; |
|
448 |
fun rearr conv = |
|
449 |
let |
|
450 |
val lhs1 = butlast lhs and rhs1 = butlast rhs; |
|
451 |
val Type(_,listT::_) = eqT |
|
452 |
val appT = [listT,listT] ---> listT |
|
453 |
val app = Const("List.op @",appT) |
|
454 |
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
|
455 |
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); |
17956 | 456 |
val thm = Goal.prove sg [] [] eq |
17877
67d5ab1cb0d8
Simplifier.inherit_context instead of Simplifier.inherit_bounds;
wenzelm
parents:
17830
diff
changeset
|
457 |
(K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1)); |
15531 | 458 |
in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; |
13114 | 459 |
|
13462 | 460 |
in |
461 |
if list1 lastl andalso list1 lastr then rearr append1_eq_conv |
|
462 |
else if lastl aconv lastr then rearr append_same_eq |
|
15531 | 463 |
else NONE |
13462 | 464 |
end; |
465 |
||
13114 | 466 |
in |
13462 | 467 |
|
468 |
val list_eq_simproc = |
|
469 |
Simplifier.simproc (Theory.sign_of (the_context ())) "list_eq" ["(xs::'a list) = ys"] list_eq; |
|
470 |
||
13114 | 471 |
end; |
472 |
||
473 |
Addsimprocs [list_eq_simproc]; |
|
474 |
*} |
|
475 |
||
476 |
||
15392 | 477 |
subsubsection {* @{text map} *} |
13114 | 478 |
|
13142 | 479 |
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs" |
13145 | 480 |
by (induct xs) simp_all |
13114 | 481 |
|
13142 | 482 |
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" |
13145 | 483 |
by (rule ext, induct_tac xs) auto |
13114 | 484 |
|
13142 | 485 |
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" |
13145 | 486 |
by (induct xs) auto |
13114 | 487 |
|
13142 | 488 |
lemma map_compose: "map (f o g) xs = map f (map g xs)" |
13145 | 489 |
by (induct xs) (auto simp add: o_def) |
13114 | 490 |
|
13142 | 491 |
lemma rev_map: "rev (map f xs) = map f (rev xs)" |
13145 | 492 |
by (induct xs) auto |
13114 | 493 |
|
13737 | 494 |
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)" |
495 |
by (induct xs) auto |
|
496 |
||
13366 | 497 |
lemma map_cong [recdef_cong]: |
13145 | 498 |
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys" |
499 |
-- {* a congruence rule for @{text map} *} |
|
13737 | 500 |
by simp |
13114 | 501 |
|
13142 | 502 |
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" |
13145 | 503 |
by (cases xs) auto |
13114 | 504 |
|
13142 | 505 |
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" |
13145 | 506 |
by (cases xs) auto |
13114 | 507 |
|
18447 | 508 |
lemma map_eq_Cons_conv: |
14025 | 509 |
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" |
13145 | 510 |
by (cases xs) auto |
13114 | 511 |
|
18447 | 512 |
lemma Cons_eq_map_conv: |
14025 | 513 |
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" |
514 |
by (cases ys) auto |
|
515 |
||
18447 | 516 |
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] |
517 |
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] |
|
518 |
declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] |
|
519 |
||
14111 | 520 |
lemma ex_map_conv: |
521 |
"(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)" |
|
18447 | 522 |
by(induct ys, auto simp add: Cons_eq_map_conv) |
14111 | 523 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
524 |
lemma map_eq_imp_length_eq: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
525 |
"!!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
|
526 |
apply (induct ys) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
527 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
528 |
apply(simp (no_asm_use)) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
529 |
apply clarify |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
530 |
apply(simp (no_asm_use)) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
531 |
apply fast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
532 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
533 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
534 |
lemma map_inj_on: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
535 |
"[| 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
|
536 |
==> xs = ys" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
537 |
apply(frule map_eq_imp_length_eq) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
538 |
apply(rotate_tac -1) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
539 |
apply(induct rule:list_induct2) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
540 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
541 |
apply(simp) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
542 |
apply (blast intro:sym) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
543 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
544 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
545 |
lemma inj_on_map_eq_map: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
546 |
"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
|
547 |
by(blast dest:map_inj_on) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
548 |
|
13114 | 549 |
lemma map_injective: |
14338 | 550 |
"!!xs. map f xs = map f ys ==> inj f ==> xs = ys" |
551 |
by (induct ys) (auto dest!:injD) |
|
13114 | 552 |
|
14339 | 553 |
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" |
554 |
by(blast dest:map_injective) |
|
555 |
||
13114 | 556 |
lemma inj_mapI: "inj f ==> inj (map f)" |
17589 | 557 |
by (iprover dest: map_injective injD intro: inj_onI) |
13114 | 558 |
|
559 |
lemma inj_mapD: "inj (map f) ==> inj f" |
|
14208 | 560 |
apply (unfold inj_on_def, clarify) |
13145 | 561 |
apply (erule_tac x = "[x]" in ballE) |
14208 | 562 |
apply (erule_tac x = "[y]" in ballE, simp, blast) |
13145 | 563 |
apply blast |
564 |
done |
|
13114 | 565 |
|
14339 | 566 |
lemma inj_map[iff]: "inj (map f) = inj f" |
13145 | 567 |
by (blast dest: inj_mapD intro: inj_mapI) |
13114 | 568 |
|
15303 | 569 |
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" |
570 |
apply(rule inj_onI) |
|
571 |
apply(erule map_inj_on) |
|
572 |
apply(blast intro:inj_onI dest:inj_onD) |
|
573 |
done |
|
574 |
||
14343 | 575 |
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" |
576 |
by (induct xs, auto) |
|
13114 | 577 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
578 |
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
|
579 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
580 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
581 |
lemma map_fst_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
582 |
"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
|
583 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
584 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
585 |
lemma map_snd_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
586 |
"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
|
587 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
588 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
589 |
|
15392 | 590 |
subsubsection {* @{text rev} *} |
13114 | 591 |
|
13142 | 592 |
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" |
13145 | 593 |
by (induct xs) auto |
13114 | 594 |
|
13142 | 595 |
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" |
13145 | 596 |
by (induct xs) auto |
13114 | 597 |
|
15870 | 598 |
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" |
599 |
by auto |
|
600 |
||
13142 | 601 |
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" |
13145 | 602 |
by (induct xs) auto |
13114 | 603 |
|
13142 | 604 |
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" |
13145 | 605 |
by (induct xs) auto |
13114 | 606 |
|
15870 | 607 |
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" |
608 |
by (cases xs) auto |
|
609 |
||
610 |
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])" |
|
611 |
by (cases xs) auto |
|
612 |
||
13142 | 613 |
lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)" |
14208 | 614 |
apply (induct xs, force) |
615 |
apply (case_tac ys, simp, force) |
|
13145 | 616 |
done |
13114 | 617 |
|
15439 | 618 |
lemma inj_on_rev[iff]: "inj_on rev A" |
619 |
by(simp add:inj_on_def) |
|
620 |
||
13366 | 621 |
lemma rev_induct [case_names Nil snoc]: |
622 |
"[| 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
|
623 |
apply(simplesubst rev_rev_ident[symmetric]) |
13145 | 624 |
apply(rule_tac list = "rev xs" in list.induct, simp_all) |
625 |
done |
|
13114 | 626 |
|
13145 | 627 |
ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility" |
13114 | 628 |
|
13366 | 629 |
lemma rev_exhaust [case_names Nil snoc]: |
630 |
"(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" |
|
13145 | 631 |
by (induct xs rule: rev_induct) auto |
13114 | 632 |
|
13366 | 633 |
lemmas rev_cases = rev_exhaust |
634 |
||
18423 | 635 |
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])" |
636 |
by(rule rev_cases[of xs]) auto |
|
637 |
||
13114 | 638 |
|
15392 | 639 |
subsubsection {* @{text set} *} |
13114 | 640 |
|
13142 | 641 |
lemma finite_set [iff]: "finite (set xs)" |
13145 | 642 |
by (induct xs) auto |
13114 | 643 |
|
13142 | 644 |
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" |
13145 | 645 |
by (induct xs) auto |
13114 | 646 |
|
17830 | 647 |
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs" |
648 |
by(cases xs) auto |
|
14099 | 649 |
|
13142 | 650 |
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" |
13145 | 651 |
by auto |
13114 | 652 |
|
14099 | 653 |
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" |
654 |
by auto |
|
655 |
||
13142 | 656 |
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" |
13145 | 657 |
by (induct xs) auto |
13114 | 658 |
|
15245 | 659 |
lemma set_empty2[iff]: "({} = set xs) = (xs = [])" |
660 |
by(induct xs) auto |
|
661 |
||
13142 | 662 |
lemma set_rev [simp]: "set (rev xs) = set xs" |
13145 | 663 |
by (induct xs) auto |
13114 | 664 |
|
13142 | 665 |
lemma set_map [simp]: "set (map f xs) = f`(set xs)" |
13145 | 666 |
by (induct xs) auto |
13114 | 667 |
|
13142 | 668 |
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}" |
13145 | 669 |
by (induct xs) auto |
13114 | 670 |
|
15425 | 671 |
lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}" |
14208 | 672 |
apply (induct j, simp_all) |
673 |
apply (erule ssubst, auto) |
|
13145 | 674 |
done |
13114 | 675 |
|
13142 | 676 |
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)" |
15113 | 677 |
proof (induct xs) |
678 |
case Nil show ?case by simp |
|
679 |
case (Cons a xs) |
|
680 |
show ?case |
|
681 |
proof |
|
682 |
assume "x \<in> set (a # xs)" |
|
683 |
with prems show "\<exists>ys zs. a # xs = ys @ x # zs" |
|
684 |
by (simp, blast intro: Cons_eq_appendI) |
|
685 |
next |
|
686 |
assume "\<exists>ys zs. a # xs = ys @ x # zs" |
|
687 |
then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast |
|
688 |
show "x \<in> set (a # xs)" |
|
689 |
by (cases ys, auto simp add: eq) |
|
690 |
qed |
|
691 |
qed |
|
13142 | 692 |
|
18049 | 693 |
lemma in_set_conv_decomp_first: |
694 |
"(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)" |
|
695 |
proof (induct xs) |
|
696 |
case Nil show ?case by simp |
|
697 |
next |
|
698 |
case (Cons a xs) |
|
699 |
show ?case |
|
700 |
proof cases |
|
701 |
assume "x = a" thus ?case using Cons by force |
|
702 |
next |
|
703 |
assume "x \<noteq> a" |
|
704 |
show ?case |
|
705 |
proof |
|
706 |
assume "x \<in> set (a # xs)" |
|
707 |
from prems show "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys" |
|
708 |
by(fastsimp intro!: Cons_eq_appendI) |
|
709 |
next |
|
710 |
assume "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys" |
|
711 |
then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast |
|
712 |
show "x \<in> set (a # xs)" by (cases ys, auto simp add: eq) |
|
713 |
qed |
|
714 |
qed |
|
715 |
qed |
|
716 |
||
717 |
lemmas split_list = in_set_conv_decomp[THEN iffD1, standard] |
|
718 |
lemmas split_list_first = in_set_conv_decomp_first[THEN iffD1, standard] |
|
719 |
||
720 |
||
13508 | 721 |
lemma finite_list: "finite A ==> EX l. set l = A" |
722 |
apply (erule finite_induct, auto) |
|
723 |
apply (rule_tac x="x#l" in exI, auto) |
|
724 |
done |
|
725 |
||
14388 | 726 |
lemma card_length: "card (set xs) \<le> length xs" |
727 |
by (induct xs) (auto simp add: card_insert_if) |
|
13114 | 728 |
|
15168 | 729 |
|
15392 | 730 |
subsubsection {* @{text filter} *} |
13114 | 731 |
|
13142 | 732 |
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" |
13145 | 733 |
by (induct xs) auto |
13114 | 734 |
|
15305 | 735 |
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" |
736 |
by (induct xs) simp_all |
|
737 |
||
13142 | 738 |
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" |
13145 | 739 |
by (induct xs) auto |
13114 | 740 |
|
16998 | 741 |
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" |
742 |
by (induct xs) (auto simp add: le_SucI) |
|
743 |
||
18423 | 744 |
lemma sum_length_filter_compl: |
745 |
"length(filter P xs) + length(filter (%x. ~P x) xs) = length xs" |
|
746 |
by(induct xs) simp_all |
|
747 |
||
13142 | 748 |
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" |
13145 | 749 |
by (induct xs) auto |
13114 | 750 |
|
13142 | 751 |
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" |
13145 | 752 |
by (induct xs) auto |
13114 | 753 |
|
16998 | 754 |
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" |
755 |
by (induct xs) simp_all |
|
756 |
||
757 |
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" |
|
758 |
apply (induct xs) |
|
759 |
apply auto |
|
760 |
apply(cut_tac P=P and xs=xs in length_filter_le) |
|
761 |
apply simp |
|
762 |
done |
|
13114 | 763 |
|
16965 | 764 |
lemma filter_map: |
765 |
"filter P (map f xs) = map f (filter (P o f) xs)" |
|
766 |
by (induct xs) simp_all |
|
767 |
||
768 |
lemma length_filter_map[simp]: |
|
769 |
"length (filter P (map f xs)) = length(filter (P o f) xs)" |
|
770 |
by (simp add:filter_map) |
|
771 |
||
13142 | 772 |
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" |
13145 | 773 |
by auto |
13114 | 774 |
|
15246 | 775 |
lemma length_filter_less: |
776 |
"\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" |
|
777 |
proof (induct xs) |
|
778 |
case Nil thus ?case by simp |
|
779 |
next |
|
780 |
case (Cons x xs) thus ?case |
|
781 |
apply (auto split:split_if_asm) |
|
782 |
using length_filter_le[of P xs] apply arith |
|
783 |
done |
|
784 |
qed |
|
13114 | 785 |
|
15281 | 786 |
lemma length_filter_conv_card: |
787 |
"length(filter p xs) = card{i. i < length xs & p(xs!i)}" |
|
788 |
proof (induct xs) |
|
789 |
case Nil thus ?case by simp |
|
790 |
next |
|
791 |
case (Cons x xs) |
|
792 |
let ?S = "{i. i < length xs & p(xs!i)}" |
|
793 |
have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) |
|
794 |
show ?case (is "?l = card ?S'") |
|
795 |
proof (cases) |
|
796 |
assume "p x" |
|
797 |
hence eq: "?S' = insert 0 (Suc ` ?S)" |
|
798 |
by(auto simp add: nth_Cons image_def split:nat.split elim:lessE) |
|
799 |
have "length (filter p (x # xs)) = Suc(card ?S)" |
|
800 |
using Cons by simp |
|
801 |
also have "\<dots> = Suc(card(Suc ` ?S))" using fin |
|
802 |
by (simp add: card_image inj_Suc) |
|
803 |
also have "\<dots> = card ?S'" using eq fin |
|
804 |
by (simp add:card_insert_if) (simp add:image_def) |
|
805 |
finally show ?thesis . |
|
806 |
next |
|
807 |
assume "\<not> p x" |
|
808 |
hence eq: "?S' = Suc ` ?S" |
|
809 |
by(auto simp add: nth_Cons image_def split:nat.split elim:lessE) |
|
810 |
have "length (filter p (x # xs)) = card ?S" |
|
811 |
using Cons by simp |
|
812 |
also have "\<dots> = card(Suc ` ?S)" using fin |
|
813 |
by (simp add: card_image inj_Suc) |
|
814 |
also have "\<dots> = card ?S'" using eq fin |
|
815 |
by (simp add:card_insert_if) |
|
816 |
finally show ?thesis . |
|
817 |
qed |
|
818 |
qed |
|
819 |
||
17629 | 820 |
lemma Cons_eq_filterD: |
821 |
"x#xs = filter P ys \<Longrightarrow> |
|
822 |
\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" |
|
823 |
(concl is "\<exists>us vs. ?P ys us vs") |
|
824 |
proof(induct ys) |
|
825 |
case Nil thus ?case by simp |
|
826 |
next |
|
827 |
case (Cons y ys) |
|
828 |
show ?case (is "\<exists>x. ?Q x") |
|
829 |
proof cases |
|
830 |
assume Py: "P y" |
|
831 |
show ?thesis |
|
832 |
proof cases |
|
833 |
assume xy: "x = y" |
|
834 |
show ?thesis |
|
835 |
proof from Py xy Cons(2) show "?Q []" by simp qed |
|
836 |
next |
|
837 |
assume "x \<noteq> y" with Py Cons(2) show ?thesis by simp |
|
838 |
qed |
|
839 |
next |
|
840 |
assume Py: "\<not> P y" |
|
841 |
with Cons obtain us vs where 1 : "?P (y#ys) (y#us) vs" by fastsimp |
|
842 |
show ?thesis (is "? us. ?Q us") |
|
843 |
proof show "?Q (y#us)" using 1 by simp qed |
|
844 |
qed |
|
845 |
qed |
|
846 |
||
847 |
lemma filter_eq_ConsD: |
|
848 |
"filter P ys = x#xs \<Longrightarrow> |
|
849 |
\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" |
|
850 |
by(rule Cons_eq_filterD) simp |
|
851 |
||
852 |
lemma filter_eq_Cons_iff: |
|
853 |
"(filter P ys = x#xs) = |
|
854 |
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" |
|
855 |
by(auto dest:filter_eq_ConsD) |
|
856 |
||
857 |
lemma Cons_eq_filter_iff: |
|
858 |
"(x#xs = filter P ys) = |
|
859 |
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" |
|
860 |
by(auto dest:Cons_eq_filterD) |
|
861 |
||
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
862 |
lemma filter_cong[recdef_cong]: |
17501 | 863 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" |
864 |
apply simp |
|
865 |
apply(erule thin_rl) |
|
866 |
by (induct ys) simp_all |
|
867 |
||
15281 | 868 |
|
15392 | 869 |
subsubsection {* @{text concat} *} |
13114 | 870 |
|
13142 | 871 |
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" |
13145 | 872 |
by (induct xs) auto |
13114 | 873 |
|
18447 | 874 |
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 875 |
by (induct xss) auto |
13114 | 876 |
|
18447 | 877 |
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 878 |
by (induct xss) auto |
13114 | 879 |
|
13142 | 880 |
lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)" |
13145 | 881 |
by (induct xs) auto |
13114 | 882 |
|
13142 | 883 |
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" |
13145 | 884 |
by (induct xs) auto |
13114 | 885 |
|
13142 | 886 |
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" |
13145 | 887 |
by (induct xs) auto |
13114 | 888 |
|
13142 | 889 |
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" |
13145 | 890 |
by (induct xs) auto |
13114 | 891 |
|
892 |
||
15392 | 893 |
subsubsection {* @{text nth} *} |
13114 | 894 |
|
13142 | 895 |
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x" |
13145 | 896 |
by auto |
13114 | 897 |
|
13142 | 898 |
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n" |
13145 | 899 |
by auto |
13114 | 900 |
|
13142 | 901 |
declare nth.simps [simp del] |
13114 | 902 |
|
903 |
lemma nth_append: |
|
13145 | 904 |
"!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" |
14208 | 905 |
apply (induct "xs", simp) |
906 |
apply (case_tac n, auto) |
|
13145 | 907 |
done |
13114 | 908 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
909 |
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
|
910 |
by (induct "xs") auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
911 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
912 |
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
|
913 |
by (induct "xs") auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
914 |
|
13142 | 915 |
lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)" |
14208 | 916 |
apply (induct xs, simp) |
917 |
apply (case_tac n, auto) |
|
13145 | 918 |
done |
13114 | 919 |
|
18423 | 920 |
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0" |
921 |
by(cases xs) simp_all |
|
922 |
||
18049 | 923 |
|
924 |
lemma list_eq_iff_nth_eq: |
|
925 |
"!!ys. (xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))" |
|
926 |
apply(induct xs) |
|
927 |
apply simp apply blast |
|
928 |
apply(case_tac ys) |
|
929 |
apply simp |
|
930 |
apply(simp add:nth_Cons split:nat.split)apply blast |
|
931 |
done |
|
932 |
||
13142 | 933 |
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}" |
15251 | 934 |
apply (induct xs, simp, simp) |
13145 | 935 |
apply safe |
14208 | 936 |
apply (rule_tac x = 0 in exI, simp) |
937 |
apply (rule_tac x = "Suc i" in exI, simp) |
|
938 |
apply (case_tac i, simp) |
|
13145 | 939 |
apply (rename_tac j) |
14208 | 940 |
apply (rule_tac x = j in exI, simp) |
13145 | 941 |
done |
13114 | 942 |
|
17501 | 943 |
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" |
944 |
by(auto simp:set_conv_nth) |
|
945 |
||
13145 | 946 |
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)" |
947 |
by (auto simp add: set_conv_nth) |
|
13114 | 948 |
|
13142 | 949 |
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs" |
13145 | 950 |
by (auto simp add: set_conv_nth) |
13114 | 951 |
|
952 |
lemma all_nth_imp_all_set: |
|
13145 | 953 |
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x" |
954 |
by (auto simp add: set_conv_nth) |
|
13114 | 955 |
|
956 |
lemma all_set_conv_all_nth: |
|
13145 | 957 |
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))" |
958 |
by (auto simp add: set_conv_nth) |
|
13114 | 959 |
|
960 |
||
15392 | 961 |
subsubsection {* @{text list_update} *} |
13114 | 962 |
|
13142 | 963 |
lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs" |
13145 | 964 |
by (induct xs) (auto split: nat.split) |
13114 | 965 |
|
966 |
lemma nth_list_update: |
|
13145 | 967 |
"!!i j. i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" |
968 |
by (induct xs) (auto simp add: nth_Cons split: nat.split) |
|
13114 | 969 |
|
13142 | 970 |
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" |
13145 | 971 |
by (simp add: nth_list_update) |
13114 | 972 |
|
13142 | 973 |
lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j" |
13145 | 974 |
by (induct xs) (auto simp add: nth_Cons split: nat.split) |
13114 | 975 |
|
13142 | 976 |
lemma list_update_overwrite [simp]: |
13145 | 977 |
"!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]" |
978 |
by (induct xs) (auto split: nat.split) |
|
13114 | 979 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
980 |
lemma list_update_id[simp]: "!!i. i < length xs ==> xs[i := xs!i] = xs" |
14208 | 981 |
apply (induct xs, simp) |
14187 | 982 |
apply(simp split:nat.splits) |
983 |
done |
|
984 |
||
17501 | 985 |
lemma list_update_beyond[simp]: "\<And>i. length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" |
986 |
apply (induct xs) |
|
987 |
apply simp |
|
988 |
apply (case_tac i) |
|
989 |
apply simp_all |
|
990 |
done |
|
991 |
||
13114 | 992 |
lemma list_update_same_conv: |
13145 | 993 |
"!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" |
994 |
by (induct xs) (auto split: nat.split) |
|
13114 | 995 |
|
14187 | 996 |
lemma list_update_append1: |
997 |
"!!i. i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" |
|
14208 | 998 |
apply (induct xs, simp) |
14187 | 999 |
apply(simp split:nat.split) |
1000 |
done |
|
1001 |
||
15868 | 1002 |
lemma list_update_append: |
1003 |
"!!n. (xs @ ys) [n:= x] = |
|
1004 |
(if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))" |
|
1005 |
by (induct xs) (auto split:nat.splits) |
|
1006 |
||
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1007 |
lemma list_update_length [simp]: |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1008 |
"(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
|
1009 |
by (induct xs, auto) |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1010 |
|
13114 | 1011 |
lemma update_zip: |
13145 | 1012 |
"!!i xy xs. length xs = length ys ==> |
1013 |
(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" |
|
1014 |
by (induct ys) (auto, case_tac xs, auto split: nat.split) |
|
13114 | 1015 |
|
1016 |
lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)" |
|
13145 | 1017 |
by (induct xs) (auto split: nat.split) |
13114 | 1018 |
|
1019 |
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A" |
|
13145 | 1020 |
by (blast dest!: set_update_subset_insert [THEN subsetD]) |
13114 | 1021 |
|
15868 | 1022 |
lemma set_update_memI: "!!n. n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" |
1023 |
by (induct xs) (auto split:nat.splits) |
|
1024 |
||
13114 | 1025 |
|
15392 | 1026 |
subsubsection {* @{text last} and @{text butlast} *} |
13114 | 1027 |
|
13142 | 1028 |
lemma last_snoc [simp]: "last (xs @ [x]) = x" |
13145 | 1029 |
by (induct xs) auto |
13114 | 1030 |
|
13142 | 1031 |
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" |
13145 | 1032 |
by (induct xs) auto |
13114 | 1033 |
|
14302 | 1034 |
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" |
1035 |
by(simp add:last.simps) |
|
1036 |
||
1037 |
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" |
|
1038 |
by(simp add:last.simps) |
|
1039 |
||
1040 |
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" |
|
1041 |
by (induct xs) (auto) |
|
1042 |
||
1043 |
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" |
|
1044 |
by(simp add:last_append) |
|
1045 |
||
1046 |
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" |
|
1047 |
by(simp add:last_append) |
|
1048 |
||
17762 | 1049 |
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs" |
1050 |
by(rule rev_exhaust[of xs]) simp_all |
|
1051 |
||
1052 |
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs" |
|
1053 |
by(cases xs) simp_all |
|
1054 |
||
17765 | 1055 |
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as" |
1056 |
by (induct as) auto |
|
17762 | 1057 |
|
13142 | 1058 |
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" |
13145 | 1059 |
by (induct xs rule: rev_induct) auto |
13114 | 1060 |
|
1061 |
lemma butlast_append: |
|
13145 | 1062 |
"!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" |
1063 |
by (induct xs) auto |
|
13114 | 1064 |
|
13142 | 1065 |
lemma append_butlast_last_id [simp]: |
13145 | 1066 |
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs" |
1067 |
by (induct xs) auto |
|
13114 | 1068 |
|
13142 | 1069 |
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" |
13145 | 1070 |
by (induct xs) (auto split: split_if_asm) |
13114 | 1071 |
|
1072 |
lemma in_set_butlast_appendI: |
|
13145 | 1073 |
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))" |
1074 |
by (auto dest: in_set_butlastD simp add: butlast_append) |
|
13114 | 1075 |
|
17501 | 1076 |
lemma last_drop[simp]: "!!n. n < length xs \<Longrightarrow> last (drop n xs) = last xs" |
1077 |
apply (induct xs) |
|
1078 |
apply simp |
|
1079 |
apply (auto split:nat.split) |
|
1080 |
done |
|
1081 |
||
17589 | 1082 |
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)" |
1083 |
by(induct xs)(auto simp:neq_Nil_conv) |
|
1084 |
||
13142 | 1085 |
|
15392 | 1086 |
subsubsection {* @{text take} and @{text drop} *} |
13114 | 1087 |
|
13142 | 1088 |
lemma take_0 [simp]: "take 0 xs = []" |
13145 | 1089 |
by (induct xs) auto |
13114 | 1090 |
|
13142 | 1091 |
lemma drop_0 [simp]: "drop 0 xs = xs" |
13145 | 1092 |
by (induct xs) auto |
13114 | 1093 |
|
13142 | 1094 |
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" |
13145 | 1095 |
by simp |
13114 | 1096 |
|
13142 | 1097 |
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" |
13145 | 1098 |
by simp |
13114 | 1099 |
|
13142 | 1100 |
declare take_Cons [simp del] and drop_Cons [simp del] |
13114 | 1101 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1102 |
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
|
1103 |
by(clarsimp simp add:neq_Nil_conv) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1104 |
|
14187 | 1105 |
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" |
1106 |
by(cases xs, simp_all) |
|
1107 |
||
1108 |
lemma drop_tl: "!!n. drop n (tl xs) = tl(drop n xs)" |
|
1109 |
by(induct xs, simp_all add:drop_Cons drop_Suc split:nat.split) |
|
1110 |
||
1111 |
lemma nth_via_drop: "!!n. drop n xs = y#ys \<Longrightarrow> xs!n = y" |
|
14208 | 1112 |
apply (induct xs, simp) |
14187 | 1113 |
apply(simp add:drop_Cons nth_Cons split:nat.splits) |
1114 |
done |
|
1115 |
||
13913 | 1116 |
lemma take_Suc_conv_app_nth: |
1117 |
"!!i. i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" |
|
14208 | 1118 |
apply (induct xs, simp) |
1119 |
apply (case_tac i, auto) |
|
13913 | 1120 |
done |
1121 |
||
14591 | 1122 |
lemma drop_Suc_conv_tl: |
1123 |
"!!i. i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" |
|
1124 |
apply (induct xs, simp) |
|
1125 |
apply (case_tac i, auto) |
|
1126 |
done |
|
1127 |
||
13142 | 1128 |
lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n" |
13145 | 1129 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 1130 |
|
13142 | 1131 |
lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)" |
13145 | 1132 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 1133 |
|
13142 | 1134 |
lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs" |
13145 | 1135 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 1136 |
|
13142 | 1137 |
lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []" |
13145 | 1138 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 1139 |
|
13142 | 1140 |
lemma take_append [simp]: |
13145 | 1141 |
"!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" |
1142 |
by (induct n) (auto, case_tac xs, auto) |
|
13114 | 1143 |
|
13142 | 1144 |
lemma drop_append [simp]: |
13145 | 1145 |
"!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys" |
1146 |
by (induct n) (auto, case_tac xs, auto) |
|
13114 | 1147 |
|
13142 | 1148 |
lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs" |
14208 | 1149 |
apply (induct m, auto) |
1150 |
apply (case_tac xs, auto) |
|
15236
f289e8ba2bb3
Proofs needed to be updated because induction now preserves name of
nipkow
parents:
15176
diff
changeset
|
1151 |
apply (case_tac n, auto) |
13145 | 1152 |
done |
13114 | 1153 |
|
13142 | 1154 |
lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs" |
14208 | 1155 |
apply (induct m, auto) |
1156 |
apply (case_tac xs, auto) |
|
13145 | 1157 |
done |
13114 | 1158 |
|
1159 |
lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)" |
|
14208 | 1160 |
apply (induct m, auto) |
1161 |
apply (case_tac xs, auto) |
|
13145 | 1162 |
done |
13114 | 1163 |
|
14802 | 1164 |
lemma drop_take: "!!m n. drop n (take m xs) = take (m-n) (drop n xs)" |
1165 |
apply(induct xs) |
|
1166 |
apply simp |
|
1167 |
apply(simp add: take_Cons drop_Cons split:nat.split) |
|
1168 |
done |
|
1169 |
||
13142 | 1170 |
lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs" |
14208 | 1171 |
apply (induct n, auto) |
1172 |
apply (case_tac xs, auto) |
|
13145 | 1173 |
done |
13114 | 1174 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1175 |
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
|
1176 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1177 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1178 |
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
|
1179 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1180 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1181 |
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
|
1182 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1183 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1184 |
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
|
1185 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1186 |
|
13114 | 1187 |
lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)" |
14208 | 1188 |
apply (induct n, auto) |
1189 |
apply (case_tac xs, auto) |
|
13145 | 1190 |
done |
13114 | 1191 |
|
13142 | 1192 |
lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)" |
14208 | 1193 |
apply (induct n, auto) |
1194 |
apply (case_tac xs, auto) |
|
13145 | 1195 |
done |
13114 | 1196 |
|
1197 |
lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)" |
|
14208 | 1198 |
apply (induct xs, auto) |
1199 |
apply (case_tac i, auto) |
|
13145 | 1200 |
done |
13114 | 1201 |
|
1202 |
lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)" |
|
14208 | 1203 |
apply (induct xs, auto) |
1204 |
apply (case_tac i, auto) |
|
13145 | 1205 |
done |
13114 | 1206 |
|
13142 | 1207 |
lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i" |
14208 | 1208 |
apply (induct xs, auto) |
1209 |
apply (case_tac n, blast) |
|
1210 |
apply (case_tac i, auto) |
|
13145 | 1211 |
done |
13114 | 1212 |
|
13142 | 1213 |
lemma nth_drop [simp]: |
13145 | 1214 |
"!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" |
14208 | 1215 |
apply (induct n, auto) |
1216 |
apply (case_tac xs, auto) |
|
13145 | 1217 |
done |
3507 | 1218 |
|
18423 | 1219 |
lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n" |
1220 |
by(simp add: hd_conv_nth) |
|
1221 |
||
14025 | 1222 |
lemma set_take_subset: "\<And>n. set(take n xs) \<subseteq> set xs" |
1223 |
by(induct xs)(auto simp:take_Cons split:nat.split) |
|
1224 |
||
1225 |
lemma set_drop_subset: "\<And>n. set(drop n xs) \<subseteq> set xs" |
|
1226 |
by(induct xs)(auto simp:drop_Cons split:nat.split) |
|
1227 |
||
14187 | 1228 |
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs" |
1229 |
using set_take_subset by fast |
|
1230 |
||
1231 |
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs" |
|
1232 |
using set_drop_subset by fast |
|
1233 |
||
13114 | 1234 |
lemma append_eq_conv_conj: |
13145 | 1235 |
"!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" |
14208 | 1236 |
apply (induct xs, simp, clarsimp) |
1237 |
apply (case_tac zs, auto) |
|
13145 | 1238 |
done |
13142 | 1239 |
|
14050 | 1240 |
lemma take_add [rule_format]: |
1241 |
"\<forall>i. i+j \<le> length(xs) --> take (i+j) xs = take i xs @ take j (drop i xs)" |
|
1242 |
apply (induct xs, auto) |
|
1243 |
apply (case_tac i, simp_all) |
|
1244 |
done |
|
1245 |
||
14300 | 1246 |
lemma append_eq_append_conv_if: |
1247 |
"!! ys\<^isub>1. (xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) = |
|
1248 |
(if size xs\<^isub>1 \<le> size ys\<^isub>1 |
|
1249 |
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 |
|
1250 |
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)" |
|
1251 |
apply(induct xs\<^isub>1) |
|
1252 |
apply simp |
|
1253 |
apply(case_tac ys\<^isub>1) |
|
1254 |
apply simp_all |
|
1255 |
done |
|
1256 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1257 |
lemma take_hd_drop: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1258 |
"!!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
|
1259 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1260 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1261 |
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
|
1262 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1263 |
|
17501 | 1264 |
lemma id_take_nth_drop: |
1265 |
"i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" |
|
1266 |
proof - |
|
1267 |
assume si: "i < length xs" |
|
1268 |
hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto |
|
1269 |
moreover |
|
1270 |
from si have "take (Suc i) xs = take i xs @ [xs!i]" |
|
1271 |
apply (rule_tac take_Suc_conv_app_nth) by arith |
|
1272 |
ultimately show ?thesis by auto |
|
1273 |
qed |
|
1274 |
||
1275 |
lemma upd_conv_take_nth_drop: |
|
1276 |
"i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs" |
|
1277 |
proof - |
|
1278 |
assume i: "i < length xs" |
|
1279 |
have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]" |
|
1280 |
by(rule arg_cong[OF id_take_nth_drop[OF i]]) |
|
1281 |
also have "\<dots> = take i xs @ a # drop (Suc i) xs" |
|
1282 |
using i by (simp add: list_update_append) |
|
1283 |
finally show ?thesis . |
|
1284 |
qed |
|
1285 |
||
13114 | 1286 |
|
15392 | 1287 |
subsubsection {* @{text takeWhile} and @{text dropWhile} *} |
13114 | 1288 |
|
13142 | 1289 |
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" |
13145 | 1290 |
by (induct xs) auto |
13114 | 1291 |
|
13142 | 1292 |
lemma takeWhile_append1 [simp]: |
13145 | 1293 |
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs" |
1294 |
by (induct xs) auto |
|
13114 | 1295 |
|
13142 | 1296 |
lemma takeWhile_append2 [simp]: |
13145 | 1297 |
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys" |
1298 |
by (induct xs) auto |
|
13114 | 1299 |
|
13142 | 1300 |
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs" |
13145 | 1301 |
by (induct xs) auto |
13114 | 1302 |
|
13142 | 1303 |
lemma dropWhile_append1 [simp]: |
13145 | 1304 |
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" |
1305 |
by (induct xs) auto |
|
13114 | 1306 |
|
13142 | 1307 |
lemma dropWhile_append2 [simp]: |
13145 | 1308 |
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" |
1309 |
by (induct xs) auto |
|
13114 | 1310 |
|
13142 | 1311 |
lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x" |
13145 | 1312 |
by (induct xs) (auto split: split_if_asm) |
13114 | 1313 |
|
13913 | 1314 |
lemma takeWhile_eq_all_conv[simp]: |
1315 |
"(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" |
|
1316 |
by(induct xs, auto) |
|
1317 |
||
1318 |
lemma dropWhile_eq_Nil_conv[simp]: |
|
1319 |
"(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" |
|
1320 |
by(induct xs, auto) |
|
1321 |
||
1322 |
lemma dropWhile_eq_Cons_conv: |
|
1323 |
"(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)" |
|
1324 |
by(induct xs, auto) |
|
1325 |
||
17501 | 1326 |
text{* The following two lemmmas could be generalized to an arbitrary |
1327 |
property. *} |
|
1328 |
||
1329 |
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
|
1330 |
takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))" |
|
1331 |
by(induct xs) (auto simp: takeWhile_tail[where l="[]"]) |
|
1332 |
||
1333 |
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow> |
|
1334 |
dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)" |
|
1335 |
apply(induct xs) |
|
1336 |
apply simp |
|
1337 |
apply auto |
|
1338 |
apply(subst dropWhile_append2) |
|
1339 |
apply auto |
|
1340 |
done |
|
1341 |
||
18423 | 1342 |
lemma takeWhile_not_last: |
1343 |
"\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs" |
|
1344 |
apply(induct xs) |
|
1345 |
apply simp |
|
1346 |
apply(case_tac xs) |
|
1347 |
apply(auto) |
|
1348 |
done |
|
1349 |
||
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1350 |
lemma takeWhile_cong [recdef_cong]: |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1351 |
"[| 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
|
1352 |
==> takeWhile P l = takeWhile Q k" |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1353 |
by (induct k fixing: l, simp_all) |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1354 |
|
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1355 |
lemma dropWhile_cong [recdef_cong]: |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1356 |
"[| 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
|
1357 |
==> dropWhile P l = dropWhile Q k" |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1358 |
by (induct k fixing: l, simp_all) |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1359 |
|
13114 | 1360 |
|
15392 | 1361 |
subsubsection {* @{text zip} *} |
13114 | 1362 |
|
13142 | 1363 |
lemma zip_Nil [simp]: "zip [] ys = []" |
13145 | 1364 |
by (induct ys) auto |
13114 | 1365 |
|
13142 | 1366 |
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" |
13145 | 1367 |
by simp |
13114 | 1368 |
|
13142 | 1369 |
declare zip_Cons [simp del] |
13114 | 1370 |
|
15281 | 1371 |
lemma zip_Cons1: |
1372 |
"zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)" |
|
1373 |
by(auto split:list.split) |
|
1374 |
||
13142 | 1375 |
lemma length_zip [simp]: |
13145 | 1376 |
"!!xs. length (zip xs ys) = min (length xs) (length ys)" |
14208 | 1377 |
apply (induct ys, simp) |
1378 |
apply (case_tac xs, auto) |
|
13145 | 1379 |
done |
13114 | 1380 |
|
1381 |
lemma zip_append1: |
|
13145 | 1382 |
"!!xs. zip (xs @ ys) zs = |
1383 |
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)" |
|
14208 | 1384 |
apply (induct zs, simp) |
1385 |
apply (case_tac xs, simp_all) |
|
13145 | 1386 |
done |
13114 | 1387 |
|
1388 |
lemma zip_append2: |
|
13145 | 1389 |
"!!ys. zip xs (ys @ zs) = |
1390 |
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs" |
|
14208 | 1391 |
apply (induct xs, simp) |
1392 |
apply (case_tac ys, simp_all) |
|
13145 | 1393 |
done |
13114 | 1394 |
|
13142 | 1395 |
lemma zip_append [simp]: |
1396 |
"[| length xs = length us; length ys = length vs |] ==> |
|
13145 | 1397 |
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" |
1398 |
by (simp add: zip_append1) |
|
13114 | 1399 |
|
1400 |
lemma zip_rev: |
|
14247 | 1401 |
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" |
1402 |
by (induct rule:list_induct2, simp_all) |
|
13114 | 1403 |
|
13142 | 1404 |
lemma nth_zip [simp]: |
13145 | 1405 |
"!!i xs. [| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)" |
14208 | 1406 |
apply (induct ys, simp) |
13145 | 1407 |
apply (case_tac xs) |
1408 |
apply (simp_all add: nth.simps split: nat.split) |
|
1409 |
done |
|
13114 | 1410 |
|
1411 |
lemma set_zip: |
|
13145 | 1412 |
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}" |
1413 |
by (simp add: set_conv_nth cong: rev_conj_cong) |
|
13114 | 1414 |
|
1415 |
lemma zip_update: |
|
13145 | 1416 |
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" |
1417 |
by (rule sym, simp add: update_zip) |
|
13114 | 1418 |
|
13142 | 1419 |
lemma zip_replicate [simp]: |
13145 | 1420 |
"!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" |
14208 | 1421 |
apply (induct i, auto) |
1422 |
apply (case_tac j, auto) |
|
13145 | 1423 |
done |
13114 | 1424 |
|
13142 | 1425 |
|
15392 | 1426 |
subsubsection {* @{text list_all2} *} |
13114 | 1427 |
|
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1428 |
lemma list_all2_lengthD [intro?]: |
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1429 |
"list_all2 P xs ys ==> length xs = length ys" |
13145 | 1430 |
by (simp add: list_all2_def) |
13114 | 1431 |
|
17090 | 1432 |
lemma list_all2_Nil [iff,code]: "list_all2 P [] ys = (ys = [])" |
13145 | 1433 |
by (simp add: list_all2_def) |
13114 | 1434 |
|
13142 | 1435 |
lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])" |
13145 | 1436 |
by (simp add: list_all2_def) |
13114 | 1437 |
|
17090 | 1438 |
lemma list_all2_Cons [iff,code]: |
13145 | 1439 |
"list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)" |
1440 |
by (auto simp add: list_all2_def) |
|
13114 | 1441 |
|
1442 |
lemma list_all2_Cons1: |
|
13145 | 1443 |
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" |
1444 |
by (cases ys) auto |
|
13114 | 1445 |
|
1446 |
lemma list_all2_Cons2: |
|
13145 | 1447 |
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" |
1448 |
by (cases xs) auto |
|
13114 | 1449 |
|
13142 | 1450 |
lemma list_all2_rev [iff]: |
13145 | 1451 |
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" |
1452 |
by (simp add: list_all2_def zip_rev cong: conj_cong) |
|
13114 | 1453 |
|
13863 | 1454 |
lemma list_all2_rev1: |
1455 |
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" |
|
1456 |
by (subst list_all2_rev [symmetric]) simp |
|
1457 |
||
13114 | 1458 |
lemma list_all2_append1: |
13145 | 1459 |
"list_all2 P (xs @ ys) zs = |
1460 |
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> |
|
1461 |
list_all2 P xs us \<and> list_all2 P ys vs)" |
|
1462 |
apply (simp add: list_all2_def zip_append1) |
|
1463 |
apply (rule iffI) |
|
1464 |
apply (rule_tac x = "take (length xs) zs" in exI) |
|
1465 |
apply (rule_tac x = "drop (length xs) zs" in exI) |
|
14208 | 1466 |
apply (force split: nat_diff_split simp add: min_def, clarify) |
13145 | 1467 |
apply (simp add: ball_Un) |
1468 |
done |
|
13114 | 1469 |
|
1470 |
lemma list_all2_append2: |
|
13145 | 1471 |
"list_all2 P xs (ys @ zs) = |
1472 |
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> |
|
1473 |
list_all2 P us ys \<and> list_all2 P vs zs)" |
|
1474 |
apply (simp add: list_all2_def zip_append2) |
|
1475 |
apply (rule iffI) |
|
1476 |
apply (rule_tac x = "take (length ys) xs" in exI) |
|
1477 |
apply (rule_tac x = "drop (length ys) xs" in exI) |
|
14208 | 1478 |
apply (force split: nat_diff_split simp add: min_def, clarify) |
13145 | 1479 |
apply (simp add: ball_Un) |
1480 |
done |
|
13114 | 1481 |
|
13863 | 1482 |
lemma list_all2_append: |
14247 | 1483 |
"length xs = length ys \<Longrightarrow> |
1484 |
list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)" |
|
1485 |
by (induct rule:list_induct2, simp_all) |
|
13863 | 1486 |
|
1487 |
lemma list_all2_appendI [intro?, trans]: |
|
1488 |
"\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)" |
|
1489 |
by (simp add: list_all2_append list_all2_lengthD) |
|
1490 |
||
13114 | 1491 |
lemma list_all2_conv_all_nth: |
13145 | 1492 |
"list_all2 P xs ys = |
1493 |
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))" |
|
1494 |
by (force simp add: list_all2_def set_zip) |
|
13114 | 1495 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1496 |
lemma list_all2_trans: |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1497 |
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
|
1498 |
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
|
1499 |
(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
|
1500 |
proof (induct as) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1501 |
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
|
1502 |
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
|
1503 |
proof (induct bs) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1504 |
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
|
1505 |
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
|
1506 |
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
|
1507 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1508 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1509 |
|
13863 | 1510 |
lemma list_all2_all_nthI [intro?]: |
1511 |
"length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b" |
|
1512 |
by (simp add: list_all2_conv_all_nth) |
|
1513 |
||
14395 | 1514 |
lemma list_all2I: |
1515 |
"\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b" |
|
1516 |
by (simp add: list_all2_def) |
|
1517 |
||
14328 | 1518 |
lemma list_all2_nthD: |
13863 | 1519 |
"\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
1520 |
by (simp add: list_all2_conv_all_nth) |
|
1521 |
||
14302 | 1522 |
lemma list_all2_nthD2: |
1523 |
"\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
|
1524 |
by (frule list_all2_lengthD) (auto intro: list_all2_nthD) |
|
1525 |
||
13863 | 1526 |
lemma list_all2_map1: |
1527 |
"list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs" |
|
1528 |
by (simp add: list_all2_conv_all_nth) |
|
1529 |
||
1530 |
lemma list_all2_map2: |
|
1531 |
"list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs" |
|
1532 |
by (auto simp add: list_all2_conv_all_nth) |
|
1533 |
||
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1534 |
lemma list_all2_refl [intro?]: |
13863 | 1535 |
"(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs" |
1536 |
by (simp add: list_all2_conv_all_nth) |
|
1537 |
||
1538 |
lemma list_all2_update_cong: |
|
1539 |
"\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" |
|
1540 |
by (simp add: list_all2_conv_all_nth nth_list_update) |
|
1541 |
||
1542 |
lemma list_all2_update_cong2: |
|
1543 |
"\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" |
|
1544 |
by (simp add: list_all2_lengthD list_all2_update_cong) |
|
1545 |
||
14302 | 1546 |
lemma list_all2_takeI [simp,intro?]: |
1547 |
"\<And>n ys. list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)" |
|
1548 |
apply (induct xs) |
|
1549 |
apply simp |
|
1550 |
apply (clarsimp simp add: list_all2_Cons1) |
|
1551 |
apply (case_tac n) |
|
1552 |
apply auto |
|
1553 |
done |
|
1554 |
||
1555 |
lemma list_all2_dropI [simp,intro?]: |
|
13863 | 1556 |
"\<And>n bs. list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)" |
14208 | 1557 |
apply (induct as, simp) |
13863 | 1558 |
apply (clarsimp simp add: list_all2_Cons1) |
14208 | 1559 |
apply (case_tac n, simp, simp) |
13863 | 1560 |
done |
1561 |
||
14327 | 1562 |
lemma list_all2_mono [intro?]: |
13863 | 1563 |
"\<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 | 1564 |
apply (induct x, simp) |
1565 |
apply (case_tac y, auto) |
|
13863 | 1566 |
done |
1567 |
||
13142 | 1568 |
|
15392 | 1569 |
subsubsection {* @{text foldl} and @{text foldr} *} |
13142 | 1570 |
|
1571 |
lemma foldl_append [simp]: |
|
13145 | 1572 |
"!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys" |
1573 |
by (induct xs) auto |
|
13142 | 1574 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1575 |
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
|
1576 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1577 |
|
18336
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1578 |
lemma foldl_cong [recdef_cong]: |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1579 |
"[| 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
|
1580 |
==> foldl f a l = foldl g b k" |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1581 |
by (induct k fixing: a b l, simp_all) |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1582 |
|
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1583 |
lemma foldr_cong [recdef_cong]: |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1584 |
"[| 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
|
1585 |
==> foldr f l a = foldr g k b" |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1586 |
by (induct k fixing: a b l, simp_all) |
1a2e30b37ed3
Added recdef congruence rules for bounded quantifiers and commonly used
krauss
parents:
18049
diff
changeset
|
1587 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1588 |
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
|
1589 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1590 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1591 |
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
|
1592 |
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
|
1593 |
|
13142 | 1594 |
text {* |
13145 | 1595 |
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more |
1596 |
difficult to use because it requires an additional transitivity step. |
|
13142 | 1597 |
*} |
1598 |
||
1599 |
lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns" |
|
13145 | 1600 |
by (induct ns) auto |
13142 | 1601 |
|
1602 |
lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns" |
|
13145 | 1603 |
by (force intro: start_le_sum simp add: in_set_conv_decomp) |
13142 | 1604 |
|
1605 |
lemma sum_eq_0_conv [iff]: |
|
13145 | 1606 |
"!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))" |
1607 |
by (induct ns) auto |
|
13114 | 1608 |
|
1609 |
||
15392 | 1610 |
subsubsection {* @{text upto} *} |
13114 | 1611 |
|
17090 | 1612 |
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])" |
1613 |
-- {* simp does not terminate! *} |
|
13145 | 1614 |
by (induct j) auto |
13142 | 1615 |
|
15425 | 1616 |
lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []" |
13145 | 1617 |
by (subst upt_rec) simp |
13114 | 1618 |
|
15425 | 1619 |
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)" |
15281 | 1620 |
by(induct j)simp_all |
1621 |
||
1622 |
lemma upt_eq_Cons_conv: |
|
15425 | 1623 |
"!!x xs. ([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)" |
15281 | 1624 |
apply(induct j) |
1625 |
apply simp |
|
1626 |
apply(clarsimp simp add: append_eq_Cons_conv) |
|
1627 |
apply arith |
|
1628 |
done |
|
1629 |
||
15425 | 1630 |
lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]" |
13145 | 1631 |
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *} |
1632 |
by simp |
|
13114 | 1633 |
|
15425 | 1634 |
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]" |
13145 | 1635 |
apply(rule trans) |
1636 |
apply(subst upt_rec) |
|
14208 | 1637 |
prefer 2 apply (rule refl, simp) |
13145 | 1638 |
done |
13114 | 1639 |
|
15425 | 1640 |
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]" |
13145 | 1641 |
-- {* LOOPS as a simprule, since @{text "j <= j"}. *} |
1642 |
by (induct k) auto |
|
13114 | 1643 |
|
15425 | 1644 |
lemma length_upt [simp]: "length [i..<j] = j - i" |
13145 | 1645 |
by (induct j) (auto simp add: Suc_diff_le) |
13114 | 1646 |
|
15425 | 1647 |
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k" |
13145 | 1648 |
apply (induct j) |
1649 |
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split) |
|
1650 |
done |
|
13114 | 1651 |
|
17906 | 1652 |
|
1653 |
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i" |
|
1654 |
by(simp add:upt_conv_Cons) |
|
1655 |
||
1656 |
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1" |
|
1657 |
apply(cases j) |
|
1658 |
apply simp |
|
1659 |
by(simp add:upt_Suc_append) |
|
1660 |
||
15425 | 1661 |
lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..<n] = [i..<i+m]" |
14208 | 1662 |
apply (induct m, simp) |
13145 | 1663 |
apply (subst upt_rec) |
1664 |
apply (rule sym) |
|
1665 |
apply (subst upt_rec) |
|
1666 |
apply (simp del: upt.simps) |
|
1667 |
done |
|
3507 | 1668 |
|
17501 | 1669 |
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]" |
1670 |
apply(induct j) |
|
1671 |
apply auto |
|
1672 |
apply arith |
|
1673 |
done |
|
1674 |
||
15425 | 1675 |
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..n]" |
13145 | 1676 |
by (induct n) auto |
13114 | 1677 |
|
15425 | 1678 |
lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..<n]) ! i = f(m+i)" |
13145 | 1679 |
apply (induct n m rule: diff_induct) |
1680 |
prefer 3 apply (subst map_Suc_upt[symmetric]) |
|
1681 |
apply (auto simp add: less_diff_conv nth_upt) |
|
1682 |
done |
|
13114 | 1683 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1684 |
lemma nth_take_lemma: |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1685 |
"!!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
|
1686 |
(!!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
|
1687 |
apply (atomize, induct k) |
14208 | 1688 |
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify) |
13145 | 1689 |
txt {* Both lists must be non-empty *} |
14208 | 1690 |
apply (case_tac xs, simp) |
1691 |
apply (case_tac ys, clarify) |
|
13145 | 1692 |
apply (simp (no_asm_use)) |
1693 |
apply clarify |
|
1694 |
txt {* prenexing's needed, not miniscoping *} |
|
1695 |
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps) |
|
1696 |
apply blast |
|
1697 |
done |
|
13114 | 1698 |
|
1699 |
lemma nth_equalityI: |
|
1700 |
"[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys" |
|
13145 | 1701 |
apply (frule nth_take_lemma [OF le_refl eq_imp_le]) |
1702 |
apply (simp_all add: take_all) |
|
1703 |
done |
|
13142 | 1704 |
|
13863 | 1705 |
(* needs nth_equalityI *) |
1706 |
lemma list_all2_antisym: |
|
1707 |
"\<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> |
|
1708 |
\<Longrightarrow> xs = ys" |
|
1709 |
apply (simp add: list_all2_conv_all_nth) |
|
14208 | 1710 |
apply (rule nth_equalityI, blast, simp) |
13863 | 1711 |
done |
1712 |
||
13142 | 1713 |
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys" |
13145 | 1714 |
-- {* The famous take-lemma. *} |
1715 |
apply (drule_tac x = "max (length xs) (length ys)" in spec) |
|
1716 |
apply (simp add: le_max_iff_disj take_all) |
|
1717 |
done |
|
13142 | 1718 |
|
1719 |
||
15302 | 1720 |
lemma take_Cons': |
1721 |
"take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)" |
|
1722 |
by (cases n) simp_all |
|
1723 |
||
1724 |
lemma drop_Cons': |
|
1725 |
"drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)" |
|
1726 |
by (cases n) simp_all |
|
1727 |
||
1728 |
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))" |
|
1729 |
by (cases n) simp_all |
|
1730 |
||
18622 | 1731 |
lemmas take_Cons_number_of = take_Cons'[of "number_of v",standard] |
1732 |
lemmas drop_Cons_number_of = drop_Cons'[of "number_of v",standard] |
|
1733 |
lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v",standard] |
|
1734 |
||
1735 |
declare take_Cons_number_of [simp] |
|
1736 |
drop_Cons_number_of [simp] |
|
1737 |
nth_Cons_number_of [simp] |
|
15302 | 1738 |
|
1739 |
||
15392 | 1740 |
subsubsection {* @{text "distinct"} and @{text remdups} *} |
13142 | 1741 |
|
1742 |
lemma distinct_append [simp]: |
|
13145 | 1743 |
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})" |
1744 |
by (induct xs) auto |
|
13142 | 1745 |
|
15305 | 1746 |
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs" |
1747 |
by(induct xs) auto |
|
1748 |
||
13142 | 1749 |
lemma set_remdups [simp]: "set (remdups xs) = set xs" |
13145 | 1750 |
by (induct xs) (auto simp add: insert_absorb) |
13142 | 1751 |
|
1752 |
lemma distinct_remdups [iff]: "distinct (remdups xs)" |
|
13145 | 1753 |
by (induct xs) auto |
13142 | 1754 |
|
15072 | 1755 |
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])" |
15251 | 1756 |
by (induct x, auto) |
15072 | 1757 |
|
1758 |
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])" |
|
15251 | 1759 |
by (induct x, auto) |
15072 | 1760 |
|
15245 | 1761 |
lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs" |
1762 |
by (induct xs) auto |
|
1763 |
||
1764 |
lemma length_remdups_eq[iff]: |
|
1765 |
"(length (remdups xs) = length xs) = (remdups xs = xs)" |
|
1766 |
apply(induct xs) |
|
1767 |
apply auto |
|
1768 |
apply(subgoal_tac "length (remdups xs) <= length xs") |
|
1769 |
apply arith |
|
1770 |
apply(rule length_remdups_leq) |
|
1771 |
done |
|
1772 |
||
18490 | 1773 |
|
1774 |
lemma distinct_map: |
|
1775 |
"distinct(map f xs) = (distinct xs & inj_on f (set xs))" |
|
1776 |
by (induct xs) auto |
|
1777 |
||
1778 |
||
13142 | 1779 |
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)" |
13145 | 1780 |
by (induct xs) auto |
13114 | 1781 |
|
17501 | 1782 |
lemma distinct_upt[simp]: "distinct[i..<j]" |
1783 |
by (induct j) auto |
|
1784 |
||
1785 |
lemma distinct_take[simp]: "\<And>i. distinct xs \<Longrightarrow> distinct (take i xs)" |
|
1786 |
apply(induct xs) |
|
1787 |
apply simp |
|
1788 |
apply (case_tac i) |
|
1789 |
apply simp_all |
|
1790 |
apply(blast dest:in_set_takeD) |
|
1791 |
done |
|
1792 |
||
1793 |
lemma distinct_drop[simp]: "\<And>i. distinct xs \<Longrightarrow> distinct (drop i xs)" |
|
1794 |
apply(induct xs) |
|
1795 |
apply simp |
|
1796 |
apply (case_tac i) |
|
1797 |
apply simp_all |
|
1798 |
done |
|
1799 |
||
1800 |
lemma distinct_list_update: |
|
1801 |
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}" |
|
1802 |
shows "distinct (xs[i:=a])" |
|
1803 |
proof (cases "i < length xs") |
|
1804 |
case True |
|
1805 |
with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}" |
|
1806 |
apply (drule_tac id_take_nth_drop) by simp |
|
1807 |
with d True show ?thesis |
|
1808 |
apply (simp add: upd_conv_take_nth_drop) |
|
1809 |
apply (drule subst [OF id_take_nth_drop]) apply assumption |
|
1810 |
apply simp apply (cases "a = xs!i") apply simp by blast |
|
1811 |
next |
|
1812 |
case False with d show ?thesis by auto |
|
1813 |
qed |
|
1814 |
||
1815 |
||
1816 |
text {* It is best to avoid this indexed version of distinct, but |
|
1817 |
sometimes it is useful. *} |
|
1818 |
||
13142 | 1819 |
lemma distinct_conv_nth: |
17501 | 1820 |
"distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)" |
15251 | 1821 |
apply (induct xs, simp, simp) |
14208 | 1822 |
apply (rule iffI, clarsimp) |
13145 | 1823 |
apply (case_tac i) |
14208 | 1824 |
apply (case_tac j, simp) |
13145 | 1825 |
apply (simp add: set_conv_nth) |
1826 |
apply (case_tac j) |
|
14208 | 1827 |
apply (clarsimp simp add: set_conv_nth, simp) |
13145 | 1828 |
apply (rule conjI) |
1829 |
apply (clarsimp simp add: set_conv_nth) |
|
17501 | 1830 |
apply (erule_tac x = 0 in allE, simp) |
14208 | 1831 |
apply (erule_tac x = "Suc i" in allE, simp, clarsimp) |
17501 | 1832 |
apply (erule_tac x = "Suc i" in allE, simp) |
14208 | 1833 |
apply (erule_tac x = "Suc j" in allE, simp) |
13145 | 1834 |
done |
13114 | 1835 |
|
18490 | 1836 |
lemma nth_eq_iff_index_eq: |
1837 |
"\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)" |
|
1838 |
by(auto simp: distinct_conv_nth) |
|
1839 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1840 |
lemma distinct_card: "distinct xs ==> card (set xs) = size xs" |
14388 | 1841 |
by (induct xs) auto |
1842 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1843 |
lemma card_distinct: "card (set xs) = size xs ==> distinct xs" |
14388 | 1844 |
proof (induct xs) |
1845 |
case Nil thus ?case by simp |
|
1846 |
next |
|
1847 |
case (Cons x xs) |
|
1848 |
show ?case |
|
1849 |
proof (cases "x \<in> set xs") |
|
1850 |
case False with Cons show ?thesis by simp |
|
1851 |
next |
|
1852 |
case True with Cons.prems |
|
1853 |
have "card (set xs) = Suc (length xs)" |
|
1854 |
by (simp add: card_insert_if split: split_if_asm) |
|
1855 |
moreover have "card (set xs) \<le> length xs" by (rule card_length) |
|
1856 |
ultimately have False by simp |
|
1857 |
thus ?thesis .. |
|
1858 |
qed |
|
1859 |
qed |
|
1860 |
||
18490 | 1861 |
|
1862 |
lemma length_remdups_concat: |
|
1863 |
"length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)" |
|
1864 |
by(simp add: distinct_card[symmetric]) |
|
17906 | 1865 |
|
1866 |
||
15392 | 1867 |
subsubsection {* @{text remove1} *} |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1868 |
|
18049 | 1869 |
lemma remove1_append: |
1870 |
"remove1 x (xs @ ys) = |
|
1871 |
(if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)" |
|
1872 |
by (induct xs) auto |
|
1873 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1874 |
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
|
1875 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1876 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1877 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1878 |
apply blast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1879 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1880 |
|
17724 | 1881 |
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
|
1882 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1883 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1884 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1885 |
apply blast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1886 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1887 |
|
18049 | 1888 |
lemma remove1_filter_not[simp]: |
1889 |
"\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs" |
|
1890 |
by(induct xs) auto |
|
1891 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1892 |
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
|
1893 |
apply(insert set_remove1_subset) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1894 |
apply fast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1895 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1896 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1897 |
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
|
1898 |
by (induct xs) simp_all |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1899 |
|
13114 | 1900 |
|
15392 | 1901 |
subsubsection {* @{text replicate} *} |
13114 | 1902 |
|
13142 | 1903 |
lemma length_replicate [simp]: "length (replicate n x) = n" |
13145 | 1904 |
by (induct n) auto |
13124 | 1905 |
|
13142 | 1906 |
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)" |
13145 | 1907 |
by (induct n) auto |
13114 | 1908 |
|
1909 |
lemma replicate_app_Cons_same: |
|
13145 | 1910 |
"(replicate n x) @ (x # xs) = x # replicate n x @ xs" |
1911 |
by (induct n) auto |
|
13114 | 1912 |
|
13142 | 1913 |
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x" |
14208 | 1914 |
apply (induct n, simp) |
13145 | 1915 |
apply (simp add: replicate_app_Cons_same) |
1916 |
done |
|
13114 | 1917 |
|
13142 | 1918 |
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x" |
13145 | 1919 |
by (induct n) auto |
13114 | 1920 |
|
16397 | 1921 |
text{* Courtesy of Matthias Daum: *} |
1922 |
lemma append_replicate_commute: |
|
1923 |
"replicate n x @ replicate k x = replicate k x @ replicate n x" |
|
1924 |
apply (simp add: replicate_add [THEN sym]) |
|
1925 |
apply (simp add: add_commute) |
|
1926 |
done |
|
1927 |
||
13142 | 1928 |
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x" |
13145 | 1929 |
by (induct n) auto |
13114 | 1930 |
|
13142 | 1931 |
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x" |
13145 | 1932 |
by (induct n) auto |
13114 | 1933 |
|
13142 | 1934 |
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x" |
13145 | 1935 |
by (atomize (full), induct n) auto |
13114 | 1936 |
|
13142 | 1937 |
lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x" |
14208 | 1938 |
apply (induct n, simp) |
13145 | 1939 |
apply (simp add: nth_Cons split: nat.split) |
1940 |
done |
|
13114 | 1941 |
|
16397 | 1942 |
text{* Courtesy of Matthias Daum (2 lemmas): *} |
1943 |
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x" |
|
1944 |
apply (case_tac "k \<le> i") |
|
1945 |
apply (simp add: min_def) |
|
1946 |
apply (drule not_leE) |
|
1947 |
apply (simp add: min_def) |
|
1948 |
apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x") |
|
1949 |
apply simp |
|
1950 |
apply (simp add: replicate_add [symmetric]) |
|
1951 |
done |
|
1952 |
||
1953 |
lemma drop_replicate[simp]: "!!i. drop i (replicate k x) = replicate (k-i) x" |
|
1954 |
apply (induct k) |
|
1955 |
apply simp |
|
1956 |
apply clarsimp |
|
1957 |
apply (case_tac i) |
|
1958 |
apply simp |
|
1959 |
apply clarsimp |
|
1960 |
done |
|
1961 |
||
1962 |
||
13142 | 1963 |
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}" |
13145 | 1964 |
by (induct n) auto |
13114 | 1965 |
|
13142 | 1966 |
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}" |
13145 | 1967 |
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc) |
13114 | 1968 |
|
13142 | 1969 |
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})" |
13145 | 1970 |
by auto |
13114 | 1971 |
|
13142 | 1972 |
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y" |
13145 | 1973 |
by (simp add: set_replicate_conv_if split: split_if_asm) |
13114 | 1974 |
|
1975 |
||
15392 | 1976 |
subsubsection{*@{text rotate1} and @{text rotate}*} |
15302 | 1977 |
|
1978 |
lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]" |
|
1979 |
by(simp add:rotate1_def) |
|
1980 |
||
1981 |
lemma rotate0[simp]: "rotate 0 = id" |
|
1982 |
by(simp add:rotate_def) |
|
1983 |
||
1984 |
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)" |
|
1985 |
by(simp add:rotate_def) |
|
1986 |
||
1987 |
lemma rotate_add: |
|
1988 |
"rotate (m+n) = rotate m o rotate n" |
|
1989 |
by(simp add:rotate_def funpow_add) |
|
1990 |
||
1991 |
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs" |
|
1992 |
by(simp add:rotate_add) |
|
1993 |
||
18049 | 1994 |
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)" |
1995 |
by(simp add:rotate_def funpow_swap1) |
|
1996 |
||
15302 | 1997 |
lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs" |
1998 |
by(cases xs) simp_all |
|
1999 |
||
2000 |
lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs" |
|
2001 |
apply(induct n) |
|
2002 |
apply simp |
|
2003 |
apply (simp add:rotate_def) |
|
13145 | 2004 |
done |
13114 | 2005 |
|
15302 | 2006 |
lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]" |
2007 |
by(simp add:rotate1_def split:list.split) |
|
2008 |
||
2009 |
lemma rotate_drop_take: |
|
2010 |
"rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs" |
|
2011 |
apply(induct n) |
|
2012 |
apply simp |
|
2013 |
apply(simp add:rotate_def) |
|
2014 |
apply(cases "xs = []") |
|
2015 |
apply (simp) |
|
2016 |
apply(case_tac "n mod length xs = 0") |
|
2017 |
apply(simp add:mod_Suc) |
|
2018 |
apply(simp add: rotate1_hd_tl drop_Suc take_Suc) |
|
2019 |
apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric] |
|
2020 |
take_hd_drop linorder_not_le) |
|
13145 | 2021 |
done |
13114 | 2022 |
|
15302 | 2023 |
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs" |
2024 |
by(simp add:rotate_drop_take) |
|
2025 |
||
2026 |
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs" |
|
2027 |
by(simp add:rotate_drop_take) |
|
2028 |
||
2029 |
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs" |
|
2030 |
by(simp add:rotate1_def split:list.split) |
|
2031 |
||
2032 |
lemma length_rotate[simp]: "!!xs. length(rotate n xs) = length xs" |
|
2033 |
by (induct n) (simp_all add:rotate_def) |
|
2034 |
||
2035 |
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs" |
|
2036 |
by(simp add:rotate1_def split:list.split) blast |
|
2037 |
||
2038 |
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs" |
|
2039 |
by (induct n) (simp_all add:rotate_def) |
|
2040 |
||
2041 |
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)" |
|
2042 |
by(simp add:rotate_drop_take take_map drop_map) |
|
2043 |
||
2044 |
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs" |
|
2045 |
by(simp add:rotate1_def split:list.split) |
|
2046 |
||
2047 |
lemma set_rotate[simp]: "set(rotate n xs) = set xs" |
|
2048 |
by (induct n) (simp_all add:rotate_def) |
|
2049 |
||
2050 |
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])" |
|
2051 |
by(simp add:rotate1_def split:list.split) |
|
2052 |
||
2053 |
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])" |
|
2054 |
by (induct n) (simp_all add:rotate_def) |
|
13114 | 2055 |
|
15439 | 2056 |
lemma rotate_rev: |
2057 |
"rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)" |
|
2058 |
apply(simp add:rotate_drop_take rev_drop rev_take) |
|
2059 |
apply(cases "length xs = 0") |
|
2060 |
apply simp |
|
2061 |
apply(cases "n mod length xs = 0") |
|
2062 |
apply simp |
|
2063 |
apply(simp add:rotate_drop_take rev_drop rev_take) |
|
2064 |
done |
|
2065 |
||
18423 | 2066 |
lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)" |
2067 |
apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth) |
|
2068 |
apply(subgoal_tac "length xs \<noteq> 0") |
|
2069 |
prefer 2 apply simp |
|
2070 |
using mod_less_divisor[of "length xs" n] by arith |
|
2071 |
||
13114 | 2072 |
|
15392 | 2073 |
subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *} |
13114 | 2074 |
|
13142 | 2075 |
lemma sublist_empty [simp]: "sublist xs {} = []" |
13145 | 2076 |
by (auto simp add: sublist_def) |
13114 | 2077 |
|
13142 | 2078 |
lemma sublist_nil [simp]: "sublist [] A = []" |
13145 | 2079 |
by (auto simp add: sublist_def) |
13114 | 2080 |
|
15281 | 2081 |
lemma length_sublist: |
2082 |
"length(sublist xs I) = card{i. i < length xs \<and> i : I}" |
|
2083 |
by(simp add: sublist_def length_filter_conv_card cong:conj_cong) |
|
2084 |
||
2085 |
lemma sublist_shift_lemma_Suc: |
|
2086 |
"!!is. map fst (filter (%p. P(Suc(snd p))) (zip xs is)) = |
|
2087 |
map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))" |
|
2088 |
apply(induct xs) |
|
2089 |
apply simp |
|
2090 |
apply (case_tac "is") |
|
2091 |
apply simp |
|
2092 |
apply simp |
|
2093 |
done |
|
2094 |
||
13114 | 2095 |
lemma sublist_shift_lemma: |
15425 | 2096 |
"map fst [p:zip xs [i..<i + length xs] . snd p : A] = |
2097 |
map fst [p:zip xs [0..<length xs] . snd p + i : A]" |
|
13145 | 2098 |
by (induct xs rule: rev_induct) (simp_all add: add_commute) |
13114 | 2099 |
|
2100 |
lemma sublist_append: |
|
15168 | 2101 |
"sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}" |
13145 | 2102 |
apply (unfold sublist_def) |
14208 | 2103 |
apply (induct l' rule: rev_induct, simp) |
13145 | 2104 |
apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma) |
2105 |
apply (simp add: add_commute) |
|
2106 |
done |
|
13114 | 2107 |
|
2108 |
lemma sublist_Cons: |
|
13145 | 2109 |
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}" |
2110 |
apply (induct l rule: rev_induct) |
|
2111 |
apply (simp add: sublist_def) |
|
2112 |
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append) |
|
2113 |
done |
|
13114 | 2114 |
|
15281 | 2115 |
lemma set_sublist: "!!I. set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}" |
2116 |
apply(induct xs) |
|
2117 |
apply simp |
|
2118 |
apply(auto simp add:sublist_Cons nth_Cons split:nat.split elim: lessE) |
|
2119 |
apply(erule lessE) |
|
2120 |
apply auto |
|
2121 |
apply(erule lessE) |
|
2122 |
apply auto |
|
2123 |
done |
|
2124 |
||
2125 |
lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs" |
|
2126 |
by(auto simp add:set_sublist) |
|
2127 |
||
2128 |
lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)" |
|
2129 |
by(auto simp add:set_sublist) |
|
2130 |
||
2131 |
lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs" |
|
2132 |
by(auto simp add:set_sublist) |
|
2133 |
||
13142 | 2134 |
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])" |
13145 | 2135 |
by (simp add: sublist_Cons) |
13114 | 2136 |
|
15281 | 2137 |
|
2138 |
lemma distinct_sublistI[simp]: "!!I. distinct xs \<Longrightarrow> distinct(sublist xs I)" |
|
2139 |
apply(induct xs) |
|
2140 |
apply simp |
|
2141 |
apply(auto simp add:sublist_Cons) |
|
2142 |
done |
|
2143 |
||
2144 |
||
15045 | 2145 |
lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l" |
14208 | 2146 |
apply (induct l rule: rev_induct, simp) |
13145 | 2147 |
apply (simp split: nat_diff_split add: sublist_append) |
2148 |
done |
|
13114 | 2149 |
|
17501 | 2150 |
lemma filter_in_sublist: "\<And>s. distinct xs \<Longrightarrow> |
2151 |
filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s" |
|
2152 |
proof (induct xs) |
|
2153 |
case Nil thus ?case by simp |
|
2154 |
next |
|
2155 |
case (Cons a xs) |
|
2156 |
moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto |
|
2157 |
ultimately show ?case by(simp add: sublist_Cons cong:filter_cong) |
|
2158 |
qed |
|
2159 |
||
13114 | 2160 |
|
15392 | 2161 |
subsubsection{*Sets of Lists*} |
2162 |
||
2163 |
subsubsection {* @{text lists}: the list-forming operator over sets *} |
|
15302 | 2164 |
|
2165 |
consts lists :: "'a set => 'a list set" |
|
2166 |
inductive "lists A" |
|
2167 |
intros |
|
2168 |
Nil [intro!]: "[]: lists A" |
|
2169 |
Cons [intro!]: "[| a: A;l: lists A|] ==> a#l : lists A" |
|
2170 |
||
2171 |
inductive_cases listsE [elim!]: "x#l : lists A" |
|
2172 |
||
2173 |
lemma lists_mono [mono]: "A \<subseteq> B ==> lists A \<subseteq> lists B" |
|
2174 |
by (unfold lists.defs) (blast intro!: lfp_mono) |
|
2175 |
||
2176 |
lemma lists_IntI: |
|
2177 |
assumes l: "l: lists A" shows "l: lists B ==> l: lists (A Int B)" using l |
|
2178 |
by induct blast+ |
|
2179 |
||
2180 |
lemma lists_Int_eq [simp]: "lists (A \<inter> B) = lists A \<inter> lists B" |
|
2181 |
proof (rule mono_Int [THEN equalityI]) |
|
2182 |
show "mono lists" by (simp add: mono_def lists_mono) |
|
2183 |
show "lists A \<inter> lists B \<subseteq> lists (A \<inter> B)" by (blast intro: lists_IntI) |
|
14388 | 2184 |
qed |
2185 |
||
15302 | 2186 |
lemma append_in_lists_conv [iff]: |
2187 |
"(xs @ ys : lists A) = (xs : lists A \<and> ys : lists A)" |
|
2188 |
by (induct xs) auto |
|
2189 |
||
2190 |
lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)" |
|
2191 |
-- {* eliminate @{text lists} in favour of @{text set} *} |
|
2192 |
by (induct xs) auto |
|
2193 |
||
2194 |
lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A" |
|
2195 |
by (rule in_lists_conv_set [THEN iffD1]) |
|
2196 |
||
2197 |
lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A" |
|
2198 |
by (rule in_lists_conv_set [THEN iffD2]) |
|
2199 |
||
2200 |
lemma lists_UNIV [simp]: "lists UNIV = UNIV" |
|
2201 |
by auto |
|
2202 |
||
17086 | 2203 |
subsubsection {* For efficiency *} |
2204 |
||
2205 |
text{* Only use @{text mem} for generating executable code. Otherwise |
|
2206 |
use @{prop"x : set xs"} instead --- it is much easier to reason about. |
|
2207 |
The same is true for @{const list_all} and @{const list_ex}: write |
|
2208 |
@{text"\<forall>x\<in>set xs"} and @{text"\<exists>x\<in>set xs"} instead because the HOL |
|
17090 | 2209 |
quantifiers are aleady known to the automatic provers. In fact, the declarations in the Code subsection make sure that @{text"\<in>"}, @{text"\<forall>x\<in>set xs"} |
2210 |
and @{text"\<exists>x\<in>set xs"} are implemented efficiently. |
|
17086 | 2211 |
|
2212 |
The functions @{const itrev}, @{const filtermap} and @{const |
|
2213 |
map_filter} are just there to generate efficient code. Do not use them |
|
2214 |
for modelling and proving. *} |
|
2215 |
||
2216 |
lemma mem_iff: "(x mem xs) = (x : set xs)" |
|
2217 |
by (induct xs) auto |
|
2218 |
||
2219 |
lemma list_inter_conv: "set(list_inter xs ys) = set xs \<inter> set ys" |
|
2220 |
by (induct xs) auto |
|
2221 |
||
2222 |
lemma list_all_iff: "list_all P xs = (\<forall>x \<in> set xs. P x)" |
|
2223 |
by (induct xs) auto |
|
2224 |
||
2225 |
lemma list_all_append [simp]: |
|
2226 |
"list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)" |
|
2227 |
by (induct xs) auto |
|
2228 |
||
2229 |
lemma list_all_rev [simp]: "list_all P (rev xs) = list_all P xs" |
|
2230 |
by (simp add: list_all_iff) |
|
2231 |
||
2232 |
lemma list_ex_iff: "list_ex P xs = (\<exists>x \<in> set xs. P x)" |
|
2233 |
by (induct xs) simp_all |
|
2234 |
||
2235 |
lemma itrev[simp]: "ALL ys. itrev xs ys = rev xs @ ys" |
|
2236 |
by (induct xs) simp_all |
|
2237 |
||
2238 |
lemma filtermap_conv: |
|
18447 | 2239 |
"filtermap f xs = map (%x. the(f x)) (filter (%x. f x \<noteq> None) xs)" |
2240 |
by (induct xs) (simp_all split: option.split) |
|
17086 | 2241 |
|
2242 |
lemma map_filter_conv[simp]: "map_filter f P xs = map f (filter P xs)" |
|
2243 |
by (induct xs) auto |
|
2244 |
||
2245 |
||
2246 |
subsubsection {* Code generation *} |
|
2247 |
||
2248 |
text{* Defaults for generating efficient code for some standard functions. *} |
|
2249 |
||
17090 | 2250 |
lemmas in_set_code[code unfold] = mem_iff[symmetric, THEN eq_reflection] |
2251 |
||
2252 |
lemma rev_code[code unfold]: "rev xs == itrev xs []" |
|
17086 | 2253 |
by simp |
2254 |
||
17090 | 2255 |
lemma distinct_Cons_mem[code]: "distinct (x#xs) = (~(x mem xs) \<and> distinct xs)" |
17086 | 2256 |
by (simp add:mem_iff) |
2257 |
||
17090 | 2258 |
lemma remdups_Cons_mem[code]: |
17086 | 2259 |
"remdups (x#xs) = (if x mem xs then remdups xs else x # remdups xs)" |
2260 |
by (simp add:mem_iff) |
|
2261 |
||
17090 | 2262 |
lemma list_inter_Cons_mem[code]: "list_inter (a#as) bs = |
17086 | 2263 |
(if a mem bs then a#(list_inter as bs) else list_inter as bs)" |
2264 |
by(simp add:mem_iff) |
|
2265 |
||
17090 | 2266 |
text{* For implementing bounded quantifiers over lists by |
2267 |
@{const list_ex}/@{const list_all}: *} |
|
2268 |
||
2269 |
lemmas list_bex_code[code unfold] = list_ex_iff[symmetric, THEN eq_reflection] |
|
2270 |
lemmas list_ball_code[code unfold] = list_all_iff[symmetric, THEN eq_reflection] |
|
17086 | 2271 |
|
2272 |
||
2273 |
subsubsection{* Inductive definition for membership *} |
|
2274 |
||
2275 |
consts ListMem :: "('a \<times> 'a list)set" |
|
2276 |
inductive ListMem |
|
2277 |
intros |
|
2278 |
elem: "(x,x#xs) \<in> ListMem" |
|
2279 |
insert: "(x,xs) \<in> ListMem \<Longrightarrow> (x,y#xs) \<in> ListMem" |
|
2280 |
||
2281 |
lemma ListMem_iff: "((x,xs) \<in> ListMem) = (x \<in> set xs)" |
|
2282 |
apply (rule iffI) |
|
2283 |
apply (induct set: ListMem) |
|
2284 |
apply auto |
|
2285 |
apply (induct xs) |
|
2286 |
apply (auto intro: ListMem.intros) |
|
2287 |
done |
|
2288 |
||
2289 |
||
2290 |
||
15392 | 2291 |
subsubsection{*Lists as Cartesian products*} |
15302 | 2292 |
|
2293 |
text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from |
|
2294 |
@{term A} and tail drawn from @{term Xs}.*} |
|
2295 |
||
2296 |
constdefs |
|
2297 |
set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set" |
|
2298 |
"set_Cons A XS == {z. \<exists>x xs. z = x#xs & x \<in> A & xs \<in> XS}" |
|
2299 |
||
17724 | 2300 |
lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A" |
15302 | 2301 |
by (auto simp add: set_Cons_def) |
2302 |
||
2303 |
text{*Yields the set of lists, all of the same length as the argument and |
|
2304 |
with elements drawn from the corresponding element of the argument.*} |
|
2305 |
||
2306 |
consts listset :: "'a set list \<Rightarrow> 'a list set" |
|
2307 |
primrec |
|
2308 |
"listset [] = {[]}" |
|
2309 |
"listset(A#As) = set_Cons A (listset As)" |
|
2310 |
||
2311 |
||
15656 | 2312 |
subsection{*Relations on Lists*} |
2313 |
||
2314 |
subsubsection {* Length Lexicographic Ordering *} |
|
2315 |
||
2316 |
text{*These orderings preserve well-foundedness: shorter lists |
|
2317 |
precede longer lists. These ordering are not used in dictionaries.*} |
|
2318 |
||
2319 |
consts lexn :: "('a * 'a)set => nat => ('a list * 'a list)set" |
|
2320 |
--{*The lexicographic ordering for lists of the specified length*} |
|
15302 | 2321 |
primrec |
15656 | 2322 |
"lexn r 0 = {}" |
2323 |
"lexn r (Suc n) = |
|
2324 |
(prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int |
|
2325 |
{(xs,ys). length xs = Suc n \<and> length ys = Suc n}" |
|
15302 | 2326 |
|
2327 |
constdefs |
|
15656 | 2328 |
lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" |
2329 |
"lex r == \<Union>n. lexn r n" |
|
2330 |
--{*Holds only between lists of the same length*} |
|
2331 |
||
15693 | 2332 |
lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" |
2333 |
"lenlex r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))" |
|
15656 | 2334 |
--{*Compares lists by their length and then lexicographically*} |
15302 | 2335 |
|
2336 |
||
2337 |
lemma wf_lexn: "wf r ==> wf (lexn r n)" |
|
2338 |
apply (induct n, simp, simp) |
|
2339 |
apply(rule wf_subset) |
|
2340 |
prefer 2 apply (rule Int_lower1) |
|
2341 |
apply(rule wf_prod_fun_image) |
|
2342 |
prefer 2 apply (rule inj_onI, auto) |
|
2343 |
done |
|
2344 |
||
2345 |
lemma lexn_length: |
|
2346 |
"!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n" |
|
2347 |
by (induct n) auto |
|
2348 |
||
2349 |
lemma wf_lex [intro!]: "wf r ==> wf (lex r)" |
|
2350 |
apply (unfold lex_def) |
|
2351 |
apply (rule wf_UN) |
|
2352 |
apply (blast intro: wf_lexn, clarify) |
|
2353 |
apply (rename_tac m n) |
|
2354 |
apply (subgoal_tac "m \<noteq> n") |
|
2355 |
prefer 2 apply blast |
|
2356 |
apply (blast dest: lexn_length not_sym) |
|
2357 |
done |
|
2358 |
||
2359 |
lemma lexn_conv: |
|
15656 | 2360 |
"lexn r n = |
2361 |
{(xs,ys). length xs = n \<and> length ys = n \<and> |
|
2362 |
(\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}" |
|
18423 | 2363 |
apply (induct n, simp) |
15302 | 2364 |
apply (simp add: image_Collect lex_prod_def, safe, blast) |
2365 |
apply (rule_tac x = "ab # xys" in exI, simp) |
|
2366 |
apply (case_tac xys, simp_all, blast) |
|
2367 |
done |
|
2368 |
||
2369 |
lemma lex_conv: |
|
15656 | 2370 |
"lex r = |
2371 |
{(xs,ys). length xs = length ys \<and> |
|
2372 |
(\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}" |
|
15302 | 2373 |
by (force simp add: lex_def lexn_conv) |
2374 |
||
15693 | 2375 |
lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)" |
2376 |
by (unfold lenlex_def) blast |
|
2377 |
||
2378 |
lemma lenlex_conv: |
|
2379 |
"lenlex r = {(xs,ys). length xs < length ys | |
|
15656 | 2380 |
length xs = length ys \<and> (xs, ys) : lex r}" |
15693 | 2381 |
by (simp add: lenlex_def diag_def lex_prod_def measure_def inv_image_def) |
15302 | 2382 |
|
2383 |
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r" |
|
2384 |
by (simp add: lex_conv) |
|
2385 |
||
2386 |
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r" |
|
2387 |
by (simp add:lex_conv) |
|
2388 |
||
18447 | 2389 |
lemma Cons_in_lex [simp]: |
15656 | 2390 |
"((x # xs, y # ys) : lex r) = |
2391 |
((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)" |
|
15302 | 2392 |
apply (simp add: lex_conv) |
2393 |
apply (rule iffI) |
|
2394 |
prefer 2 apply (blast intro: Cons_eq_appendI, clarify) |
|
2395 |
apply (case_tac xys, simp, simp) |
|
2396 |
apply blast |
|
2397 |
done |
|
2398 |
||
2399 |
||
15656 | 2400 |
subsubsection {* Lexicographic Ordering *} |
2401 |
||
2402 |
text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b". |
|
2403 |
This ordering does \emph{not} preserve well-foundedness. |
|
17090 | 2404 |
Author: N. Voelker, March 2005. *} |
15656 | 2405 |
|
2406 |
constdefs |
|
2407 |
lexord :: "('a * 'a)set \<Rightarrow> ('a list * 'a list) set" |
|
2408 |
"lexord r == {(x,y). \<exists> a v. y = x @ a # v \<or> |
|
2409 |
(\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}" |
|
2410 |
||
2411 |
lemma lexord_Nil_left[simp]: "([],y) \<in> lexord r = (\<exists> a x. y = a # x)" |
|
2412 |
by (unfold lexord_def, induct_tac y, auto) |
|
2413 |
||
2414 |
lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r" |
|
2415 |
by (unfold lexord_def, induct_tac x, auto) |
|
2416 |
||
2417 |
lemma lexord_cons_cons[simp]: |
|
2418 |
"((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))" |
|
2419 |
apply (unfold lexord_def, safe, simp_all) |
|
2420 |
apply (case_tac u, simp, simp) |
|
2421 |
apply (case_tac u, simp, clarsimp, blast, blast, clarsimp) |
|
2422 |
apply (erule_tac x="b # u" in allE) |
|
2423 |
by force |
|
2424 |
||
2425 |
lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons |
|
2426 |
||
2427 |
lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r" |
|
2428 |
by (induct_tac x, auto) |
|
2429 |
||
2430 |
lemma lexord_append_left_rightI: |
|
2431 |
"(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r" |
|
2432 |
by (induct_tac u, auto) |
|
2433 |
||
2434 |
lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r" |
|
2435 |
by (induct x, auto) |
|
2436 |
||
2437 |
lemma lexord_append_leftD: |
|
2438 |
"\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r" |
|
2439 |
by (erule rev_mp, induct_tac x, auto) |
|
2440 |
||
2441 |
lemma lexord_take_index_conv: |
|
2442 |
"((x,y) : lexord r) = |
|
2443 |
((length x < length y \<and> take (length x) y = x) \<or> |
|
2444 |
(\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))" |
|
2445 |
apply (unfold lexord_def Let_def, clarsimp) |
|
2446 |
apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2) |
|
2447 |
apply auto |
|
2448 |
apply (rule_tac x="hd (drop (length x) y)" in exI) |
|
2449 |
apply (rule_tac x="tl (drop (length x) y)" in exI) |
|
2450 |
apply (erule subst, simp add: min_def) |
|
2451 |
apply (rule_tac x ="length u" in exI, simp) |
|
2452 |
apply (rule_tac x ="take i x" in exI) |
|
2453 |
apply (rule_tac x ="x ! i" in exI) |
|
2454 |
apply (rule_tac x ="y ! i" in exI, safe) |
|
2455 |
apply (rule_tac x="drop (Suc i) x" in exI) |
|
2456 |
apply (drule sym, simp add: drop_Suc_conv_tl) |
|
2457 |
apply (rule_tac x="drop (Suc i) y" in exI) |
|
2458 |
by (simp add: drop_Suc_conv_tl) |
|
2459 |
||
2460 |
-- {* lexord is extension of partial ordering List.lex *} |
|
2461 |
lemma lexord_lex: " (x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)" |
|
2462 |
apply (rule_tac x = y in spec) |
|
2463 |
apply (induct_tac x, clarsimp) |
|
2464 |
by (clarify, case_tac x, simp, force) |
|
2465 |
||
2466 |
lemma lexord_irreflexive: "(! x. (x,x) \<notin> r) \<Longrightarrow> (y,y) \<notin> lexord r" |
|
2467 |
by (induct y, auto) |
|
2468 |
||
2469 |
lemma lexord_trans: |
|
2470 |
"\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r" |
|
2471 |
apply (erule rev_mp)+ |
|
2472 |
apply (rule_tac x = x in spec) |
|
2473 |
apply (rule_tac x = z in spec) |
|
2474 |
apply ( induct_tac y, simp, clarify) |
|
2475 |
apply (case_tac xa, erule ssubst) |
|
2476 |
apply (erule allE, erule allE) -- {* avoid simp recursion *} |
|
2477 |
apply (case_tac x, simp, simp) |
|
2478 |
apply (case_tac x, erule allE, erule allE, simp) |
|
2479 |
apply (erule_tac x = listb in allE) |
|
2480 |
apply (erule_tac x = lista in allE, simp) |
|
2481 |
apply (unfold trans_def) |
|
2482 |
by blast |
|
2483 |
||
2484 |
lemma lexord_transI: "trans r \<Longrightarrow> trans (lexord r)" |
|
2485 |
by (rule transI, drule lexord_trans, blast) |
|
2486 |
||
2487 |
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" |
|
2488 |
apply (rule_tac x = y in spec) |
|
2489 |
apply (induct_tac x, rule allI) |
|
2490 |
apply (case_tac x, simp, simp) |
|
2491 |
apply (rule allI, case_tac x, simp, simp) |
|
2492 |
by blast |
|
2493 |
||
2494 |
||
15392 | 2495 |
subsubsection{*Lifting a Relation on List Elements to the Lists*} |
15302 | 2496 |
|
2497 |
consts listrel :: "('a * 'a)set => ('a list * 'a list)set" |
|
2498 |
||
2499 |
inductive "listrel(r)" |
|
2500 |
intros |
|
2501 |
Nil: "([],[]) \<in> listrel r" |
|
2502 |
Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r" |
|
2503 |
||
2504 |
inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r" |
|
2505 |
inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r" |
|
2506 |
inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r" |
|
2507 |
inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r" |
|
2508 |
||
2509 |
||
2510 |
lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s" |
|
2511 |
apply clarify |
|
2512 |
apply (erule listrel.induct) |
|
2513 |
apply (blast intro: listrel.intros)+ |
|
2514 |
done |
|
2515 |
||
2516 |
lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A" |
|
2517 |
apply clarify |
|
2518 |
apply (erule listrel.induct, auto) |
|
2519 |
done |
|
2520 |
||
2521 |
lemma listrel_refl: "refl A r \<Longrightarrow> refl (lists A) (listrel r)" |
|
2522 |
apply (simp add: refl_def listrel_subset Ball_def) |
|
2523 |
apply (rule allI) |
|
2524 |
apply (induct_tac x) |
|
2525 |
apply (auto intro: listrel.intros) |
|
2526 |
done |
|
2527 |
||
2528 |
lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" |
|
2529 |
apply (auto simp add: sym_def) |
|
2530 |
apply (erule listrel.induct) |
|
2531 |
apply (blast intro: listrel.intros)+ |
|
2532 |
done |
|
2533 |
||
2534 |
lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)" |
|
2535 |
apply (simp add: trans_def) |
|
2536 |
apply (intro allI) |
|
2537 |
apply (rule impI) |
|
2538 |
apply (erule listrel.induct) |
|
2539 |
apply (blast intro: listrel.intros)+ |
|
2540 |
done |
|
2541 |
||
2542 |
theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)" |
|
2543 |
by (simp add: equiv_def listrel_refl listrel_sym listrel_trans) |
|
2544 |
||
2545 |
lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}" |
|
2546 |
by (blast intro: listrel.intros) |
|
2547 |
||
2548 |
lemma listrel_Cons: |
|
2549 |
"listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})"; |
|
2550 |
by (auto simp add: set_Cons_def intro: listrel.intros) |
|
2551 |
||
2552 |
||
15392 | 2553 |
subsection{*Miscellany*} |
2554 |
||
2555 |
subsubsection {* Characters and strings *} |
|
13366 | 2556 |
|
2557 |
datatype nibble = |
|
2558 |
Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7 |
|
2559 |
| Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF |
|
2560 |
||
2561 |
datatype char = Char nibble nibble |
|
2562 |
-- "Note: canonical order of character encoding coincides with standard term ordering" |
|
2563 |
||
2564 |
types string = "char list" |
|
2565 |
||
2566 |
syntax |
|
2567 |
"_Char" :: "xstr => char" ("CHR _") |
|
2568 |
"_String" :: "xstr => string" ("_") |
|
2569 |
||
2570 |
parse_ast_translation {* |
|
2571 |
let |
|
2572 |
val constants = Syntax.Appl o map Syntax.Constant; |
|
2573 |
||
2574 |
fun mk_nib n = "Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10)); |
|
2575 |
fun mk_char c = |
|
2576 |
if Symbol.is_ascii c andalso Symbol.is_printable c then |
|
2577 |
constants ["Char", mk_nib (ord c div 16), mk_nib (ord c mod 16)] |
|
2578 |
else error ("Printable ASCII character expected: " ^ quote c); |
|
2579 |
||
2580 |
fun mk_string [] = Syntax.Constant "Nil" |
|
2581 |
| mk_string (c :: cs) = Syntax.Appl [Syntax.Constant "Cons", mk_char c, mk_string cs]; |
|
2582 |
||
2583 |
fun char_ast_tr [Syntax.Variable xstr] = |
|
2584 |
(case Syntax.explode_xstr xstr of |
|
2585 |
[c] => mk_char c |
|
2586 |
| _ => error ("Single character expected: " ^ xstr)) |
|
2587 |
| char_ast_tr asts = raise AST ("char_ast_tr", asts); |
|
2588 |
||
2589 |
fun string_ast_tr [Syntax.Variable xstr] = |
|
2590 |
(case Syntax.explode_xstr xstr of |
|
2591 |
[] => constants [Syntax.constrainC, "Nil", "string"] |
|
2592 |
| cs => mk_string cs) |
|
2593 |
| string_ast_tr asts = raise AST ("string_tr", asts); |
|
2594 |
in [("_Char", char_ast_tr), ("_String", string_ast_tr)] end; |
|
2595 |
*} |
|
2596 |
||
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2597 |
ML {* |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2598 |
fun int_of_nibble h = |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2599 |
if "0" <= h andalso h <= "9" then ord h - ord "0" |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2600 |
else if "A" <= h andalso h <= "F" then ord h - ord "A" + 10 |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2601 |
else raise Match; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2602 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2603 |
fun nibble_of_int i = |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2604 |
if i <= 9 then chr (ord "0" + i) else chr (ord "A" + i - 10); |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2605 |
*} |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2606 |
|
13366 | 2607 |
print_ast_translation {* |
2608 |
let |
|
2609 |
fun dest_nib (Syntax.Constant c) = |
|
2610 |
(case explode c of |
|
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2611 |
["N", "i", "b", "b", "l", "e", h] => int_of_nibble h |
13366 | 2612 |
| _ => raise Match) |
2613 |
| dest_nib _ = raise Match; |
|
2614 |
||
2615 |
fun dest_chr c1 c2 = |
|
2616 |
let val c = chr (dest_nib c1 * 16 + dest_nib c2) |
|
2617 |
in if Symbol.is_printable c then c else raise Match end; |
|
2618 |
||
2619 |
fun dest_char (Syntax.Appl [Syntax.Constant "Char", c1, c2]) = dest_chr c1 c2 |
|
2620 |
| dest_char _ = raise Match; |
|
2621 |
||
2622 |
fun xstr cs = Syntax.Appl [Syntax.Constant "_xstr", Syntax.Variable (Syntax.implode_xstr cs)]; |
|
2623 |
||
2624 |
fun char_ast_tr' [c1, c2] = Syntax.Appl [Syntax.Constant "_Char", xstr [dest_chr c1 c2]] |
|
2625 |
| char_ast_tr' _ = raise Match; |
|
2626 |
||
2627 |
fun list_ast_tr' [args] = Syntax.Appl [Syntax.Constant "_String", |
|
2628 |
xstr (map dest_char (Syntax.unfold_ast "_args" args))] |
|
2629 |
| list_ast_tr' ts = raise Match; |
|
2630 |
in [("Char", char_ast_tr'), ("@list", list_ast_tr')] end; |
|
2631 |
*} |
|
2632 |
||
15392 | 2633 |
subsubsection {* Code generator setup *} |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2634 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2635 |
ML {* |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2636 |
local |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2637 |
|
16634 | 2638 |
fun list_codegen thy defs gr dep thyname b t = |
2639 |
let val (gr', ps) = foldl_map (Codegen.invoke_codegen thy defs dep thyname false) |
|
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2640 |
(gr, HOLogic.dest_list t) |
15531 | 2641 |
in SOME (gr', Pretty.list "[" "]" ps) end handle TERM _ => NONE; |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2642 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2643 |
fun dest_nibble (Const (s, _)) = int_of_nibble (unprefix "List.nibble.Nibble" s) |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2644 |
| dest_nibble _ = raise Match; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2645 |
|
16634 | 2646 |
fun char_codegen thy defs gr dep thyname b (Const ("List.char.Char", _) $ c1 $ c2) = |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2647 |
(let val c = chr (dest_nibble c1 * 16 + dest_nibble c2) |
15531 | 2648 |
in if Symbol.is_printable c then SOME (gr, Pretty.quote (Pretty.str c)) |
2649 |
else NONE |
|
15570 | 2650 |
end handle Fail _ => NONE | Match => NONE) |
16634 | 2651 |
| char_codegen thy defs gr dep thyname b _ = NONE; |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2652 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2653 |
in |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2654 |
|
18708 | 2655 |
val list_codegen_setup = |
2656 |
Codegen.add_codegen "list_codegen" list_codegen #> |
|
2657 |
Codegen.add_codegen "char_codegen" char_codegen #> |
|
18704
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
2658 |
fold (CodegenPackage.add_pretty_list "Nil" "Cons") [ |
2c86ced392a8
substantial improvement in serialization handling
haftmann
parents:
18702
diff
changeset
|
2659 |
("ml", (7, "::")), |
18708 | 2660 |
("haskell", (5, ":"))]; |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2661 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2662 |
end; |
16770
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2663 |
*} |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2664 |
|
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2665 |
types_code |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2666 |
"list" ("_ list") |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2667 |
attach (term_of) {* |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2668 |
val term_of_list = HOLogic.mk_list; |
16770
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2669 |
*} |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2670 |
attach (test) {* |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2671 |
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
|
2672 |
[(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
|
2673 |
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
|
2674 |
*} |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2675 |
"char" ("string") |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2676 |
attach (term_of) {* |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2677 |
val nibbleT = Type ("List.nibble", []); |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2678 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2679 |
fun term_of_char c = |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2680 |
Const ("List.char.Char", nibbleT --> nibbleT --> Type ("List.char", [])) $ |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2681 |
Const ("List.nibble.Nibble" ^ nibble_of_int (ord c div 16), nibbleT) $ |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2682 |
Const ("List.nibble.Nibble" ^ nibble_of_int (ord c mod 16), nibbleT); |
16770
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2683 |
*} |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset
|
2684 |
attach (test) {* |
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2685 |
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
|
2686 |
*} |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2687 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2688 |
consts_code "Cons" ("(_ ::/ _)") |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2689 |
|
18702 | 2690 |
code_alias |
2691 |
"List.op @" "List.append" |
|
2692 |
"List.op mem" "List.member" |
|
2693 |
||
19138 | 2694 |
code_generate Nil Cons |
2695 |
||
18702 | 2696 |
code_syntax_tyco |
2697 |
list |
|
2698 |
ml ("_ list") |
|
18757 | 2699 |
haskell (target_atom "[_]") |
18702 | 2700 |
|
2701 |
code_syntax_const |
|
2702 |
Nil |
|
18757 | 2703 |
ml (target_atom "[]") |
2704 |
haskell (target_atom "[]") |
|
18702 | 2705 |
|
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2706 |
setup list_codegen_setup |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
2707 |
|
18708 | 2708 |
setup CodegenPackage.rename_inconsistent |
18451 | 2709 |
|
13122 | 2710 |
end |