author | paulson |
Thu, 05 Aug 2004 10:50:58 +0200 | |
changeset 15113 | fafcd72b9d4b |
parent 15110 | 78b5636eabc7 |
child 15131 | c69542757a4d |
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 |
|
8 |
theory List = PreList: |
|
923 | 9 |
|
13142 | 10 |
datatype 'a list = |
13366 | 11 |
Nil ("[]") |
12 |
| Cons 'a "'a list" (infixr "#" 65) |
|
923 | 13 |
|
14 |
consts |
|
13366 | 15 |
"@" :: "'a list => 'a list => 'a list" (infixr 65) |
16 |
filter:: "('a => bool) => 'a list => 'a list" |
|
17 |
concat:: "'a list list => 'a list" |
|
18 |
foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b" |
|
19 |
foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b" |
|
20 |
hd:: "'a list => 'a" |
|
21 |
tl:: "'a list => 'a list" |
|
22 |
last:: "'a list => 'a" |
|
23 |
butlast :: "'a list => 'a list" |
|
24 |
set :: "'a list => 'a set" |
|
25 |
list_all:: "('a => bool) => ('a list => bool)" |
|
26 |
list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" |
|
27 |
map :: "('a=>'b) => ('a list => 'b list)" |
|
28 |
mem :: "'a => 'a list => bool" (infixl 55) |
|
29 |
nth :: "'a list => nat => 'a" (infixl "!" 100) |
|
30 |
list_update :: "'a list => nat => 'a => 'a list" |
|
31 |
take:: "nat => 'a list => 'a list" |
|
32 |
drop:: "nat => 'a list => 'a list" |
|
33 |
takeWhile :: "('a => bool) => 'a list => 'a list" |
|
34 |
dropWhile :: "('a => bool) => 'a list => 'a list" |
|
35 |
rev :: "'a list => 'a list" |
|
36 |
zip :: "'a list => 'b list => ('a * 'b) list" |
|
37 |
upt :: "nat => nat => nat list" ("(1[_../_'(])") |
|
38 |
remdups :: "'a list => 'a list" |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
39 |
remove1 :: "'a => 'a list => 'a list" |
13366 | 40 |
null:: "'a list => bool" |
41 |
"distinct":: "'a list => bool" |
|
42 |
replicate :: "nat => 'a => 'a list" |
|
923 | 43 |
|
13146 | 44 |
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
|
45 |
|
923 | 46 |
syntax |
13366 | 47 |
-- {* list Enumeration *} |
48 |
"@list" :: "args => 'a list" ("[(_)]") |
|
923 | 49 |
|
13366 | 50 |
-- {* Special syntax for filter *} |
51 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list" ("(1[_:_./ _])") |
|
923 | 52 |
|
13366 | 53 |
-- {* list update *} |
54 |
"_lupdbind":: "['a, 'a] => lupdbind" ("(2_ :=/ _)") |
|
55 |
"" :: "lupdbind => lupdbinds" ("_") |
|
56 |
"_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _") |
|
57 |
"_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
|
58 |
|
13366 | 59 |
upto:: "nat => nat => nat list" ("(1[_../_])") |
5427 | 60 |
|
923 | 61 |
translations |
13366 | 62 |
"[x, xs]" == "x#[xs]" |
63 |
"[x]" == "x#[]" |
|
64 |
"[x:xs . P]"== "filter (%x. P) xs" |
|
923 | 65 |
|
13366 | 66 |
"_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs" |
67 |
"xs[i:=x]" == "list_update xs i x" |
|
5077
71043526295f
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
nipkow
parents:
4643
diff
changeset
|
68 |
|
13366 | 69 |
"[i..j]" == "[i..(Suc j)(]" |
5427 | 70 |
|
71 |
||
12114
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
wenzelm
parents:
10832
diff
changeset
|
72 |
syntax (xsymbols) |
13366 | 73 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") |
14565 | 74 |
syntax (HTML output) |
75 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])") |
|
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
76 |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
77 |
|
13142 | 78 |
text {* |
14589 | 79 |
Function @{text size} is overloaded for all datatypes. Users may |
13366 | 80 |
refer to the list version as @{text length}. *} |
13142 | 81 |
|
82 |
syntax length :: "'a list => nat" |
|
83 |
translations "length" => "size :: _ list => nat" |
|
13114 | 84 |
|
13142 | 85 |
typed_print_translation {* |
13366 | 86 |
let |
87 |
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] = |
|
88 |
Syntax.const "length" $ t |
|
89 |
| size_tr' _ _ _ = raise Match; |
|
90 |
in [("size", size_tr')] end |
|
13114 | 91 |
*} |
3437
bea2faf1641d
Replacing the primrec definition of "length" by a translation to the built-in
paulson
parents:
3401
diff
changeset
|
92 |
|
5183 | 93 |
primrec |
13145 | 94 |
"hd(x#xs) = x" |
5183 | 95 |
primrec |
13145 | 96 |
"tl([]) = []" |
97 |
"tl(x#xs) = xs" |
|
5183 | 98 |
primrec |
13145 | 99 |
"null([]) = True" |
100 |
"null(x#xs) = False" |
|
8972 | 101 |
primrec |
13145 | 102 |
"last(x#xs) = (if xs=[] then x else last xs)" |
5183 | 103 |
primrec |
13145 | 104 |
"butlast []= []" |
105 |
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" |
|
5183 | 106 |
primrec |
13145 | 107 |
"x mem [] = False" |
108 |
"x mem (y#ys) = (if y=x then True else x mem ys)" |
|
5518 | 109 |
primrec |
13145 | 110 |
"set [] = {}" |
111 |
"set (x#xs) = insert x (set xs)" |
|
5183 | 112 |
primrec |
13145 | 113 |
list_all_Nil:"list_all P [] = True" |
114 |
list_all_Cons: "list_all P (x#xs) = (P(x) \<and> list_all P xs)" |
|
5518 | 115 |
primrec |
13145 | 116 |
"map f [] = []" |
117 |
"map f (x#xs) = f(x)#map f xs" |
|
5183 | 118 |
primrec |
13145 | 119 |
append_Nil:"[]@ys = ys" |
120 |
append_Cons: "(x#xs)@ys = x#(xs@ys)" |
|
5183 | 121 |
primrec |
13145 | 122 |
"rev([]) = []" |
123 |
"rev(x#xs) = rev(xs) @ [x]" |
|
5183 | 124 |
primrec |
13145 | 125 |
"filter P [] = []" |
126 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
5183 | 127 |
primrec |
13145 | 128 |
foldl_Nil:"foldl f a [] = a" |
129 |
foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" |
|
5183 | 130 |
primrec |
13145 | 131 |
"foldr f [] a = a" |
132 |
"foldr f (x#xs) a = f x (foldr f xs a)" |
|
8000 | 133 |
primrec |
13145 | 134 |
"concat([]) = []" |
135 |
"concat(x#xs) = x @ concat(xs)" |
|
5183 | 136 |
primrec |
13145 | 137 |
drop_Nil:"drop n [] = []" |
138 |
drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" |
|
139 |
-- {* Warning: simpset does not contain this definition *} |
|
140 |
-- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
5183 | 141 |
primrec |
13145 | 142 |
take_Nil:"take n [] = []" |
143 |
take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
|
144 |
-- {* Warning: simpset does not contain this definition *} |
|
145 |
-- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
5183 | 146 |
primrec |
13145 | 147 |
nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)" |
148 |
-- {* Warning: simpset does not contain this definition *} |
|
149 |
-- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
13142 | 150 |
primrec |
13145 | 151 |
"[][i:=v] = []" |
152 |
"(x#xs)[i:=v] = |
|
153 |
(case i of 0 => v # xs |
|
154 |
| Suc j => x # xs[j:=v])" |
|
5183 | 155 |
primrec |
13145 | 156 |
"takeWhile P [] = []" |
157 |
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" |
|
5183 | 158 |
primrec |
13145 | 159 |
"dropWhile P [] = []" |
160 |
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" |
|
5183 | 161 |
primrec |
13145 | 162 |
"zip xs [] = []" |
163 |
zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)" |
|
164 |
-- {* Warning: simpset does not contain this definition *} |
|
165 |
-- {* but separate theorems for @{text "xs = []"} and @{text "xs = z # zs"} *} |
|
5427 | 166 |
primrec |
13145 | 167 |
upt_0: "[i..0(] = []" |
168 |
upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])" |
|
5183 | 169 |
primrec |
13145 | 170 |
"distinct [] = True" |
171 |
"distinct (x#xs) = (x ~: set xs \<and> distinct xs)" |
|
5183 | 172 |
primrec |
13145 | 173 |
"remdups [] = []" |
174 |
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" |
|
5183 | 175 |
primrec |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
176 |
"remove1 x [] = []" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
177 |
"remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
178 |
primrec |
13147 | 179 |
replicate_0: "replicate 0 x = []" |
13145 | 180 |
replicate_Suc: "replicate (Suc n) x = x # replicate n x" |
8115 | 181 |
defs |
13114 | 182 |
list_all2_def: |
13142 | 183 |
"list_all2 P xs ys == length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)" |
8115 | 184 |
|
3196 | 185 |
|
13142 | 186 |
subsection {* Lexicographic orderings on lists *} |
5281 | 187 |
|
188 |
consts |
|
13145 | 189 |
lexn :: "('a * 'a)set => nat => ('a list * 'a list)set" |
5281 | 190 |
primrec |
13145 | 191 |
"lexn r 0 = {}" |
192 |
"lexn r (Suc n) = |
|
193 |
(prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int |
|
194 |
{(xs,ys). length xs = Suc n \<and> length ys = Suc n}" |
|
5281 | 195 |
|
196 |
constdefs |
|
13145 | 197 |
lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" |
198 |
"lex r == \<Union>n. lexn r n" |
|
5281 | 199 |
|
13145 | 200 |
lexico :: "('a \<times> 'a) set => ('a list \<times> 'a list) set" |
201 |
"lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))" |
|
9336 | 202 |
|
13145 | 203 |
sublist :: "'a list => nat set => 'a list" |
204 |
"sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))" |
|
5281 | 205 |
|
13114 | 206 |
|
13142 | 207 |
lemma not_Cons_self [simp]: "xs \<noteq> x # xs" |
13145 | 208 |
by (induct xs) auto |
13114 | 209 |
|
13142 | 210 |
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric] |
13114 | 211 |
|
13142 | 212 |
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" |
13145 | 213 |
by (induct xs) auto |
13114 | 214 |
|
13142 | 215 |
lemma length_induct: |
13145 | 216 |
"(!!xs. \<forall>ys. length ys < length xs --> P ys ==> P xs) ==> P xs" |
217 |
by (rule measure_induct [of length]) rules |
|
13114 | 218 |
|
219 |
||
13142 | 220 |
subsection {* @{text lists}: the list-forming operator over sets *} |
13114 | 221 |
|
13142 | 222 |
consts lists :: "'a set => 'a list set" |
223 |
inductive "lists A" |
|
13145 | 224 |
intros |
225 |
Nil [intro!]: "[]: lists A" |
|
226 |
Cons [intro!]: "[| a: A;l: lists A|] ==> a#l : lists A" |
|
13114 | 227 |
|
13142 | 228 |
inductive_cases listsE [elim!]: "x#l : lists A" |
13114 | 229 |
|
13366 | 230 |
lemma lists_mono [mono]: "A \<subseteq> B ==> lists A \<subseteq> lists B" |
13145 | 231 |
by (unfold lists.defs) (blast intro!: lfp_mono) |
13114 | 232 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
233 |
lemma lists_IntI: |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
234 |
assumes l: "l: lists A" shows "l: lists B ==> l: lists (A Int B)" using l |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
235 |
by induct blast+ |
13142 | 236 |
|
237 |
lemma lists_Int_eq [simp]: "lists (A \<inter> B) = lists A \<inter> lists B" |
|
15113 | 238 |
proof (rule mono_Int [THEN equalityI]) |
239 |
show "mono lists" by (simp add: mono_def lists_mono) |
|
240 |
show "lists A \<inter> lists B \<subseteq> lists (A \<inter> B)" by (blast intro: lists_IntI) |
|
241 |
qed |
|
13114 | 242 |
|
13142 | 243 |
lemma append_in_lists_conv [iff]: |
15113 | 244 |
"(xs @ ys : lists A) = (xs : lists A \<and> ys : lists A)" |
13145 | 245 |
by (induct xs) auto |
13142 | 246 |
|
247 |
||
248 |
subsection {* @{text length} *} |
|
13114 | 249 |
|
13142 | 250 |
text {* |
13145 | 251 |
Needs to come before @{text "@"} because of theorem @{text |
252 |
append_eq_append_conv}. |
|
13142 | 253 |
*} |
13114 | 254 |
|
13142 | 255 |
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" |
13145 | 256 |
by (induct xs) auto |
13114 | 257 |
|
13142 | 258 |
lemma length_map [simp]: "length (map f xs) = length xs" |
13145 | 259 |
by (induct xs) auto |
13114 | 260 |
|
13142 | 261 |
lemma length_rev [simp]: "length (rev xs) = length xs" |
13145 | 262 |
by (induct xs) auto |
13114 | 263 |
|
13142 | 264 |
lemma length_tl [simp]: "length (tl xs) = length xs - 1" |
13145 | 265 |
by (cases xs) auto |
13114 | 266 |
|
13142 | 267 |
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" |
13145 | 268 |
by (induct xs) auto |
13114 | 269 |
|
13142 | 270 |
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" |
13145 | 271 |
by (induct xs) auto |
13114 | 272 |
|
273 |
lemma length_Suc_conv: |
|
13145 | 274 |
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
275 |
by (induct xs) auto |
|
13142 | 276 |
|
14025 | 277 |
lemma Suc_length_conv: |
278 |
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
|
14208 | 279 |
apply (induct xs, simp, simp) |
14025 | 280 |
apply blast |
281 |
done |
|
282 |
||
14099 | 283 |
lemma impossible_Cons [rule_format]: |
284 |
"length xs <= length ys --> xs = x # ys = False" |
|
14208 | 285 |
apply (induct xs, auto) |
14099 | 286 |
done |
287 |
||
14247 | 288 |
lemma list_induct2[consumes 1]: "\<And>ys. |
289 |
\<lbrakk> length xs = length ys; |
|
290 |
P [] []; |
|
291 |
\<And>x xs y ys. \<lbrakk> length xs = length ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk> |
|
292 |
\<Longrightarrow> P xs ys" |
|
293 |
apply(induct xs) |
|
294 |
apply simp |
|
295 |
apply(case_tac ys) |
|
296 |
apply simp |
|
297 |
apply(simp) |
|
298 |
done |
|
13114 | 299 |
|
13142 | 300 |
subsection {* @{text "@"} -- append *} |
13114 | 301 |
|
13142 | 302 |
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" |
13145 | 303 |
by (induct xs) auto |
13114 | 304 |
|
13142 | 305 |
lemma append_Nil2 [simp]: "xs @ [] = xs" |
13145 | 306 |
by (induct xs) auto |
3507 | 307 |
|
13142 | 308 |
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" |
13145 | 309 |
by (induct xs) auto |
13114 | 310 |
|
13142 | 311 |
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" |
13145 | 312 |
by (induct xs) auto |
13114 | 313 |
|
13142 | 314 |
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" |
13145 | 315 |
by (induct xs) auto |
13114 | 316 |
|
13142 | 317 |
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" |
13145 | 318 |
by (induct xs) auto |
13114 | 319 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
320 |
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
|
321 |
"!!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
|
322 |
==> (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
|
323 |
apply (induct xs) |
14208 | 324 |
apply (case_tac ys, simp, force) |
325 |
apply (case_tac ys, force, simp) |
|
13145 | 326 |
done |
13142 | 327 |
|
14495 | 328 |
lemma append_eq_append_conv2: "!!ys zs ts. |
329 |
(xs @ ys = zs @ ts) = |
|
330 |
(EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)" |
|
331 |
apply (induct xs) |
|
332 |
apply fastsimp |
|
333 |
apply(case_tac zs) |
|
334 |
apply simp |
|
335 |
apply fastsimp |
|
336 |
done |
|
337 |
||
13142 | 338 |
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)" |
13145 | 339 |
by simp |
13142 | 340 |
|
341 |
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" |
|
13145 | 342 |
by simp |
13114 | 343 |
|
13142 | 344 |
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)" |
13145 | 345 |
by simp |
13114 | 346 |
|
13142 | 347 |
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" |
13145 | 348 |
using append_same_eq [of _ _ "[]"] by auto |
3507 | 349 |
|
13142 | 350 |
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" |
13145 | 351 |
using append_same_eq [of "[]"] by auto |
13114 | 352 |
|
13142 | 353 |
lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs" |
13145 | 354 |
by (induct xs) auto |
13114 | 355 |
|
13142 | 356 |
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" |
13145 | 357 |
by (induct xs) auto |
13114 | 358 |
|
13142 | 359 |
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" |
13145 | 360 |
by (simp add: hd_append split: list.split) |
13114 | 361 |
|
13142 | 362 |
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)" |
13145 | 363 |
by (simp split: list.split) |
13114 | 364 |
|
13142 | 365 |
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" |
13145 | 366 |
by (simp add: tl_append split: list.split) |
13114 | 367 |
|
368 |
||
14300 | 369 |
lemma Cons_eq_append_conv: "x#xs = ys@zs = |
370 |
(ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))" |
|
371 |
by(cases ys) auto |
|
372 |
||
373 |
||
13142 | 374 |
text {* Trivial rules for solving @{text "@"}-equations automatically. *} |
13114 | 375 |
|
376 |
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" |
|
13145 | 377 |
by simp |
13114 | 378 |
|
13142 | 379 |
lemma Cons_eq_appendI: |
13145 | 380 |
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs" |
381 |
by (drule sym) simp |
|
13114 | 382 |
|
13142 | 383 |
lemma append_eq_appendI: |
13145 | 384 |
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us" |
385 |
by (drule sym) simp |
|
13114 | 386 |
|
387 |
||
13142 | 388 |
text {* |
13145 | 389 |
Simplification procedure for all list equalities. |
390 |
Currently only tries to rearrange @{text "@"} to see if |
|
391 |
- both lists end in a singleton list, |
|
392 |
- or both lists end in the same list. |
|
13142 | 393 |
*} |
394 |
||
395 |
ML_setup {* |
|
3507 | 396 |
local |
397 |
||
13122 | 398 |
val append_assoc = thm "append_assoc"; |
399 |
val append_Nil = thm "append_Nil"; |
|
400 |
val append_Cons = thm "append_Cons"; |
|
401 |
val append1_eq_conv = thm "append1_eq_conv"; |
|
402 |
val append_same_eq = thm "append_same_eq"; |
|
403 |
||
13114 | 404 |
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) = |
13462 | 405 |
(case xs of Const("List.list.Nil",_) => cons | _ => last xs) |
406 |
| last (Const("List.op @",_) $ _ $ ys) = last ys |
|
407 |
| last t = t; |
|
13114 | 408 |
|
409 |
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true |
|
13462 | 410 |
| list1 _ = false; |
13114 | 411 |
|
412 |
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) = |
|
13462 | 413 |
(case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs) |
414 |
| butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys |
|
415 |
| butlast xs = Const("List.list.Nil",fastype_of xs); |
|
13114 | 416 |
|
417 |
val rearr_tac = |
|
13462 | 418 |
simp_tac (HOL_basic_ss addsimps [append_assoc, append_Nil, append_Cons]); |
13114 | 419 |
|
420 |
fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) = |
|
13462 | 421 |
let |
422 |
val lastl = last lhs and lastr = last rhs; |
|
423 |
fun rearr conv = |
|
424 |
let |
|
425 |
val lhs1 = butlast lhs and rhs1 = butlast rhs; |
|
426 |
val Type(_,listT::_) = eqT |
|
427 |
val appT = [listT,listT] ---> listT |
|
428 |
val app = Const("List.op @",appT) |
|
429 |
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
|
430 |
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2)); |
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset
|
431 |
val thm = Tactic.prove sg [] [] eq (K (rearr_tac 1)); |
13462 | 432 |
in Some ((conv RS (thm RS trans)) RS eq_reflection) end; |
13114 | 433 |
|
13462 | 434 |
in |
435 |
if list1 lastl andalso list1 lastr then rearr append1_eq_conv |
|
436 |
else if lastl aconv lastr then rearr append_same_eq |
|
437 |
else None |
|
438 |
end; |
|
439 |
||
13114 | 440 |
in |
13462 | 441 |
|
442 |
val list_eq_simproc = |
|
443 |
Simplifier.simproc (Theory.sign_of (the_context ())) "list_eq" ["(xs::'a list) = ys"] list_eq; |
|
444 |
||
13114 | 445 |
end; |
446 |
||
447 |
Addsimprocs [list_eq_simproc]; |
|
448 |
*} |
|
449 |
||
450 |
||
13142 | 451 |
subsection {* @{text map} *} |
13114 | 452 |
|
13142 | 453 |
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs" |
13145 | 454 |
by (induct xs) simp_all |
13114 | 455 |
|
13142 | 456 |
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" |
13145 | 457 |
by (rule ext, induct_tac xs) auto |
13114 | 458 |
|
13142 | 459 |
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" |
13145 | 460 |
by (induct xs) auto |
13114 | 461 |
|
13142 | 462 |
lemma map_compose: "map (f o g) xs = map f (map g xs)" |
13145 | 463 |
by (induct xs) (auto simp add: o_def) |
13114 | 464 |
|
13142 | 465 |
lemma rev_map: "rev (map f xs) = map f (rev xs)" |
13145 | 466 |
by (induct xs) auto |
13114 | 467 |
|
13737 | 468 |
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)" |
469 |
by (induct xs) auto |
|
470 |
||
13366 | 471 |
lemma map_cong [recdef_cong]: |
13145 | 472 |
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys" |
473 |
-- {* a congruence rule for @{text map} *} |
|
13737 | 474 |
by simp |
13114 | 475 |
|
13142 | 476 |
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" |
13145 | 477 |
by (cases xs) auto |
13114 | 478 |
|
13142 | 479 |
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" |
13145 | 480 |
by (cases xs) auto |
13114 | 481 |
|
14025 | 482 |
lemma map_eq_Cons_conv[iff]: |
483 |
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" |
|
13145 | 484 |
by (cases xs) auto |
13114 | 485 |
|
14025 | 486 |
lemma Cons_eq_map_conv[iff]: |
487 |
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" |
|
488 |
by (cases ys) auto |
|
489 |
||
14111 | 490 |
lemma ex_map_conv: |
491 |
"(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)" |
|
492 |
by(induct ys, auto) |
|
493 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
494 |
lemma map_eq_imp_length_eq: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
495 |
"!!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
|
496 |
apply (induct ys) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
497 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
498 |
apply(simp (no_asm_use)) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
499 |
apply clarify |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
500 |
apply(simp (no_asm_use)) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
501 |
apply fast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
502 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
503 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
504 |
lemma map_inj_on: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
505 |
"[| 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
|
506 |
==> xs = ys" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
507 |
apply(frule map_eq_imp_length_eq) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
508 |
apply(rotate_tac -1) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
509 |
apply(induct rule:list_induct2) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
510 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
511 |
apply(simp) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
512 |
apply (blast intro:sym) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
513 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
514 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
515 |
lemma inj_on_map_eq_map: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
516 |
"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
|
517 |
by(blast dest:map_inj_on) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
518 |
|
13114 | 519 |
lemma map_injective: |
14338 | 520 |
"!!xs. map f xs = map f ys ==> inj f ==> xs = ys" |
521 |
by (induct ys) (auto dest!:injD) |
|
13114 | 522 |
|
14339 | 523 |
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" |
524 |
by(blast dest:map_injective) |
|
525 |
||
13114 | 526 |
lemma inj_mapI: "inj f ==> inj (map f)" |
13585 | 527 |
by (rules dest: map_injective injD intro: inj_onI) |
13114 | 528 |
|
529 |
lemma inj_mapD: "inj (map f) ==> inj f" |
|
14208 | 530 |
apply (unfold inj_on_def, clarify) |
13145 | 531 |
apply (erule_tac x = "[x]" in ballE) |
14208 | 532 |
apply (erule_tac x = "[y]" in ballE, simp, blast) |
13145 | 533 |
apply blast |
534 |
done |
|
13114 | 535 |
|
14339 | 536 |
lemma inj_map[iff]: "inj (map f) = inj f" |
13145 | 537 |
by (blast dest: inj_mapD intro: inj_mapI) |
13114 | 538 |
|
14343 | 539 |
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" |
540 |
by (induct xs, auto) |
|
13114 | 541 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
542 |
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
|
543 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
544 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
545 |
lemma map_fst_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
546 |
"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
|
547 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
548 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
549 |
lemma map_snd_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
550 |
"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
|
551 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
552 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
553 |
|
13142 | 554 |
subsection {* @{text rev} *} |
13114 | 555 |
|
13142 | 556 |
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" |
13145 | 557 |
by (induct xs) auto |
13114 | 558 |
|
13142 | 559 |
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" |
13145 | 560 |
by (induct xs) auto |
13114 | 561 |
|
13142 | 562 |
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" |
13145 | 563 |
by (induct xs) auto |
13114 | 564 |
|
13142 | 565 |
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" |
13145 | 566 |
by (induct xs) auto |
13114 | 567 |
|
13142 | 568 |
lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)" |
14208 | 569 |
apply (induct xs, force) |
570 |
apply (case_tac ys, simp, force) |
|
13145 | 571 |
done |
13114 | 572 |
|
13366 | 573 |
lemma rev_induct [case_names Nil snoc]: |
574 |
"[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs" |
|
13145 | 575 |
apply(subst rev_rev_ident[symmetric]) |
576 |
apply(rule_tac list = "rev xs" in list.induct, simp_all) |
|
577 |
done |
|
13114 | 578 |
|
13145 | 579 |
ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility" |
13114 | 580 |
|
13366 | 581 |
lemma rev_exhaust [case_names Nil snoc]: |
582 |
"(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" |
|
13145 | 583 |
by (induct xs rule: rev_induct) auto |
13114 | 584 |
|
13366 | 585 |
lemmas rev_cases = rev_exhaust |
586 |
||
13114 | 587 |
|
13142 | 588 |
subsection {* @{text set} *} |
13114 | 589 |
|
13142 | 590 |
lemma finite_set [iff]: "finite (set xs)" |
13145 | 591 |
by (induct xs) auto |
13114 | 592 |
|
13142 | 593 |
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" |
13145 | 594 |
by (induct xs) auto |
13114 | 595 |
|
14099 | 596 |
lemma hd_in_set: "l = x#xs \<Longrightarrow> x\<in>set l" |
14208 | 597 |
by (case_tac l, auto) |
14099 | 598 |
|
13142 | 599 |
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" |
13145 | 600 |
by auto |
13114 | 601 |
|
14099 | 602 |
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" |
603 |
by auto |
|
604 |
||
13142 | 605 |
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" |
13145 | 606 |
by (induct xs) auto |
13114 | 607 |
|
13142 | 608 |
lemma set_rev [simp]: "set (rev xs) = set xs" |
13145 | 609 |
by (induct xs) auto |
13114 | 610 |
|
13142 | 611 |
lemma set_map [simp]: "set (map f xs) = f`(set xs)" |
13145 | 612 |
by (induct xs) auto |
13114 | 613 |
|
13142 | 614 |
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}" |
13145 | 615 |
by (induct xs) auto |
13114 | 616 |
|
13142 | 617 |
lemma set_upt [simp]: "set[i..j(] = {k. i \<le> k \<and> k < j}" |
14208 | 618 |
apply (induct j, simp_all) |
619 |
apply (erule ssubst, auto) |
|
13145 | 620 |
done |
13114 | 621 |
|
13142 | 622 |
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)" |
15113 | 623 |
proof (induct xs) |
624 |
case Nil show ?case by simp |
|
625 |
case (Cons a xs) |
|
626 |
show ?case |
|
627 |
proof |
|
628 |
assume "x \<in> set (a # xs)" |
|
629 |
with prems show "\<exists>ys zs. a # xs = ys @ x # zs" |
|
630 |
by (simp, blast intro: Cons_eq_appendI) |
|
631 |
next |
|
632 |
assume "\<exists>ys zs. a # xs = ys @ x # zs" |
|
633 |
then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast |
|
634 |
show "x \<in> set (a # xs)" |
|
635 |
by (cases ys, auto simp add: eq) |
|
636 |
qed |
|
637 |
qed |
|
13142 | 638 |
|
639 |
lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)" |
|
13145 | 640 |
-- {* eliminate @{text lists} in favour of @{text set} *} |
641 |
by (induct xs) auto |
|
13142 | 642 |
|
643 |
lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A" |
|
13145 | 644 |
by (rule in_lists_conv_set [THEN iffD1]) |
13142 | 645 |
|
646 |
lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A" |
|
13145 | 647 |
by (rule in_lists_conv_set [THEN iffD2]) |
13114 | 648 |
|
13508 | 649 |
lemma finite_list: "finite A ==> EX l. set l = A" |
650 |
apply (erule finite_induct, auto) |
|
651 |
apply (rule_tac x="x#l" in exI, auto) |
|
652 |
done |
|
653 |
||
14388 | 654 |
lemma card_length: "card (set xs) \<le> length xs" |
655 |
by (induct xs) (auto simp add: card_insert_if) |
|
13114 | 656 |
|
13142 | 657 |
subsection {* @{text mem} *} |
13114 | 658 |
|
659 |
lemma set_mem_eq: "(x mem xs) = (x : set xs)" |
|
13145 | 660 |
by (induct xs) auto |
13114 | 661 |
|
662 |
||
13142 | 663 |
subsection {* @{text list_all} *} |
13114 | 664 |
|
13142 | 665 |
lemma list_all_conv: "list_all P xs = (\<forall>x \<in> set xs. P x)" |
13145 | 666 |
by (induct xs) auto |
13114 | 667 |
|
13142 | 668 |
lemma list_all_append [simp]: |
13145 | 669 |
"list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)" |
670 |
by (induct xs) auto |
|
13114 | 671 |
|
672 |
||
13142 | 673 |
subsection {* @{text filter} *} |
13114 | 674 |
|
13142 | 675 |
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" |
13145 | 676 |
by (induct xs) auto |
13114 | 677 |
|
13142 | 678 |
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" |
13145 | 679 |
by (induct xs) auto |
13114 | 680 |
|
13142 | 681 |
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" |
13145 | 682 |
by (induct xs) auto |
13114 | 683 |
|
13142 | 684 |
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" |
13145 | 685 |
by (induct xs) auto |
13114 | 686 |
|
13142 | 687 |
lemma length_filter [simp]: "length (filter P xs) \<le> length xs" |
13145 | 688 |
by (induct xs) (auto simp add: le_SucI) |
13114 | 689 |
|
13142 | 690 |
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" |
13145 | 691 |
by auto |
13114 | 692 |
|
693 |
||
13142 | 694 |
subsection {* @{text concat} *} |
13114 | 695 |
|
13142 | 696 |
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" |
13145 | 697 |
by (induct xs) auto |
13114 | 698 |
|
13142 | 699 |
lemma concat_eq_Nil_conv [iff]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 700 |
by (induct xss) auto |
13114 | 701 |
|
13142 | 702 |
lemma Nil_eq_concat_conv [iff]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 703 |
by (induct xss) auto |
13114 | 704 |
|
13142 | 705 |
lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)" |
13145 | 706 |
by (induct xs) auto |
13114 | 707 |
|
13142 | 708 |
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" |
13145 | 709 |
by (induct xs) auto |
13114 | 710 |
|
13142 | 711 |
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" |
13145 | 712 |
by (induct xs) auto |
13114 | 713 |
|
13142 | 714 |
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" |
13145 | 715 |
by (induct xs) auto |
13114 | 716 |
|
717 |
||
13142 | 718 |
subsection {* @{text nth} *} |
13114 | 719 |
|
13142 | 720 |
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x" |
13145 | 721 |
by auto |
13114 | 722 |
|
13142 | 723 |
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n" |
13145 | 724 |
by auto |
13114 | 725 |
|
13142 | 726 |
declare nth.simps [simp del] |
13114 | 727 |
|
728 |
lemma nth_append: |
|
13145 | 729 |
"!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" |
14208 | 730 |
apply (induct "xs", simp) |
731 |
apply (case_tac n, auto) |
|
13145 | 732 |
done |
13114 | 733 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
734 |
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
|
735 |
by (induct "xs") auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
736 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
737 |
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
|
738 |
by (induct "xs") auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
739 |
|
13142 | 740 |
lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)" |
14208 | 741 |
apply (induct xs, simp) |
742 |
apply (case_tac n, auto) |
|
13145 | 743 |
done |
13114 | 744 |
|
13142 | 745 |
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}" |
14208 | 746 |
apply (induct_tac xs, simp, simp) |
13145 | 747 |
apply safe |
14208 | 748 |
apply (rule_tac x = 0 in exI, simp) |
749 |
apply (rule_tac x = "Suc i" in exI, simp) |
|
750 |
apply (case_tac i, simp) |
|
13145 | 751 |
apply (rename_tac j) |
14208 | 752 |
apply (rule_tac x = j in exI, simp) |
13145 | 753 |
done |
13114 | 754 |
|
13145 | 755 |
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)" |
756 |
by (auto simp add: set_conv_nth) |
|
13114 | 757 |
|
13142 | 758 |
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs" |
13145 | 759 |
by (auto simp add: set_conv_nth) |
13114 | 760 |
|
761 |
lemma all_nth_imp_all_set: |
|
13145 | 762 |
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x" |
763 |
by (auto simp add: set_conv_nth) |
|
13114 | 764 |
|
765 |
lemma all_set_conv_all_nth: |
|
13145 | 766 |
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))" |
767 |
by (auto simp add: set_conv_nth) |
|
13114 | 768 |
|
769 |
||
13142 | 770 |
subsection {* @{text list_update} *} |
13114 | 771 |
|
13142 | 772 |
lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs" |
13145 | 773 |
by (induct xs) (auto split: nat.split) |
13114 | 774 |
|
775 |
lemma nth_list_update: |
|
13145 | 776 |
"!!i j. i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" |
777 |
by (induct xs) (auto simp add: nth_Cons split: nat.split) |
|
13114 | 778 |
|
13142 | 779 |
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" |
13145 | 780 |
by (simp add: nth_list_update) |
13114 | 781 |
|
13142 | 782 |
lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j" |
13145 | 783 |
by (induct xs) (auto simp add: nth_Cons split: nat.split) |
13114 | 784 |
|
13142 | 785 |
lemma list_update_overwrite [simp]: |
13145 | 786 |
"!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]" |
787 |
by (induct xs) (auto split: nat.split) |
|
13114 | 788 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
789 |
lemma list_update_id[simp]: "!!i. i < length xs ==> xs[i := xs!i] = xs" |
14208 | 790 |
apply (induct xs, simp) |
14187 | 791 |
apply(simp split:nat.splits) |
792 |
done |
|
793 |
||
13114 | 794 |
lemma list_update_same_conv: |
13145 | 795 |
"!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" |
796 |
by (induct xs) (auto split: nat.split) |
|
13114 | 797 |
|
14187 | 798 |
lemma list_update_append1: |
799 |
"!!i. i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" |
|
14208 | 800 |
apply (induct xs, simp) |
14187 | 801 |
apply(simp split:nat.split) |
802 |
done |
|
803 |
||
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
804 |
lemma list_update_length [simp]: |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
805 |
"(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
|
806 |
by (induct xs, auto) |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
807 |
|
13114 | 808 |
lemma update_zip: |
13145 | 809 |
"!!i xy xs. length xs = length ys ==> |
810 |
(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" |
|
811 |
by (induct ys) (auto, case_tac xs, auto split: nat.split) |
|
13114 | 812 |
|
813 |
lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)" |
|
13145 | 814 |
by (induct xs) (auto split: nat.split) |
13114 | 815 |
|
816 |
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A" |
|
13145 | 817 |
by (blast dest!: set_update_subset_insert [THEN subsetD]) |
13114 | 818 |
|
819 |
||
13142 | 820 |
subsection {* @{text last} and @{text butlast} *} |
13114 | 821 |
|
13142 | 822 |
lemma last_snoc [simp]: "last (xs @ [x]) = x" |
13145 | 823 |
by (induct xs) auto |
13114 | 824 |
|
13142 | 825 |
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" |
13145 | 826 |
by (induct xs) auto |
13114 | 827 |
|
14302 | 828 |
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" |
829 |
by(simp add:last.simps) |
|
830 |
||
831 |
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" |
|
832 |
by(simp add:last.simps) |
|
833 |
||
834 |
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" |
|
835 |
by (induct xs) (auto) |
|
836 |
||
837 |
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" |
|
838 |
by(simp add:last_append) |
|
839 |
||
840 |
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" |
|
841 |
by(simp add:last_append) |
|
842 |
||
843 |
||
13142 | 844 |
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" |
13145 | 845 |
by (induct xs rule: rev_induct) auto |
13114 | 846 |
|
847 |
lemma butlast_append: |
|
13145 | 848 |
"!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" |
849 |
by (induct xs) auto |
|
13114 | 850 |
|
13142 | 851 |
lemma append_butlast_last_id [simp]: |
13145 | 852 |
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs" |
853 |
by (induct xs) auto |
|
13114 | 854 |
|
13142 | 855 |
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" |
13145 | 856 |
by (induct xs) (auto split: split_if_asm) |
13114 | 857 |
|
858 |
lemma in_set_butlast_appendI: |
|
13145 | 859 |
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))" |
860 |
by (auto dest: in_set_butlastD simp add: butlast_append) |
|
13114 | 861 |
|
13142 | 862 |
|
863 |
subsection {* @{text take} and @{text drop} *} |
|
13114 | 864 |
|
13142 | 865 |
lemma take_0 [simp]: "take 0 xs = []" |
13145 | 866 |
by (induct xs) auto |
13114 | 867 |
|
13142 | 868 |
lemma drop_0 [simp]: "drop 0 xs = xs" |
13145 | 869 |
by (induct xs) auto |
13114 | 870 |
|
13142 | 871 |
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" |
13145 | 872 |
by simp |
13114 | 873 |
|
13142 | 874 |
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" |
13145 | 875 |
by simp |
13114 | 876 |
|
13142 | 877 |
declare take_Cons [simp del] and drop_Cons [simp del] |
13114 | 878 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
879 |
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
|
880 |
by(clarsimp simp add:neq_Nil_conv) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
881 |
|
14187 | 882 |
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" |
883 |
by(cases xs, simp_all) |
|
884 |
||
885 |
lemma drop_tl: "!!n. drop n (tl xs) = tl(drop n xs)" |
|
886 |
by(induct xs, simp_all add:drop_Cons drop_Suc split:nat.split) |
|
887 |
||
888 |
lemma nth_via_drop: "!!n. drop n xs = y#ys \<Longrightarrow> xs!n = y" |
|
14208 | 889 |
apply (induct xs, simp) |
14187 | 890 |
apply(simp add:drop_Cons nth_Cons split:nat.splits) |
891 |
done |
|
892 |
||
13913 | 893 |
lemma take_Suc_conv_app_nth: |
894 |
"!!i. i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" |
|
14208 | 895 |
apply (induct xs, simp) |
896 |
apply (case_tac i, auto) |
|
13913 | 897 |
done |
898 |
||
14591 | 899 |
lemma drop_Suc_conv_tl: |
900 |
"!!i. i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" |
|
901 |
apply (induct xs, simp) |
|
902 |
apply (case_tac i, auto) |
|
903 |
done |
|
904 |
||
13142 | 905 |
lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n" |
13145 | 906 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 907 |
|
13142 | 908 |
lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)" |
13145 | 909 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 910 |
|
13142 | 911 |
lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs" |
13145 | 912 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 913 |
|
13142 | 914 |
lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []" |
13145 | 915 |
by (induct n) (auto, case_tac xs, auto) |
13114 | 916 |
|
13142 | 917 |
lemma take_append [simp]: |
13145 | 918 |
"!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" |
919 |
by (induct n) (auto, case_tac xs, auto) |
|
13114 | 920 |
|
13142 | 921 |
lemma drop_append [simp]: |
13145 | 922 |
"!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys" |
923 |
by (induct n) (auto, case_tac xs, auto) |
|
13114 | 924 |
|
13142 | 925 |
lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs" |
14208 | 926 |
apply (induct m, auto) |
927 |
apply (case_tac xs, auto) |
|
928 |
apply (case_tac na, auto) |
|
13145 | 929 |
done |
13114 | 930 |
|
13142 | 931 |
lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs" |
14208 | 932 |
apply (induct m, auto) |
933 |
apply (case_tac xs, auto) |
|
13145 | 934 |
done |
13114 | 935 |
|
936 |
lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)" |
|
14208 | 937 |
apply (induct m, auto) |
938 |
apply (case_tac xs, auto) |
|
13145 | 939 |
done |
13114 | 940 |
|
14802 | 941 |
lemma drop_take: "!!m n. drop n (take m xs) = take (m-n) (drop n xs)" |
942 |
apply(induct xs) |
|
943 |
apply simp |
|
944 |
apply(simp add: take_Cons drop_Cons split:nat.split) |
|
945 |
done |
|
946 |
||
13142 | 947 |
lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs" |
14208 | 948 |
apply (induct n, auto) |
949 |
apply (case_tac xs, auto) |
|
13145 | 950 |
done |
13114 | 951 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
952 |
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
|
953 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
954 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
955 |
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
|
956 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
957 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
958 |
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
|
959 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
960 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
961 |
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
|
962 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
963 |
|
13114 | 964 |
lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)" |
14208 | 965 |
apply (induct n, auto) |
966 |
apply (case_tac xs, auto) |
|
13145 | 967 |
done |
13114 | 968 |
|
13142 | 969 |
lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)" |
14208 | 970 |
apply (induct n, auto) |
971 |
apply (case_tac xs, auto) |
|
13145 | 972 |
done |
13114 | 973 |
|
974 |
lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)" |
|
14208 | 975 |
apply (induct xs, auto) |
976 |
apply (case_tac i, auto) |
|
13145 | 977 |
done |
13114 | 978 |
|
979 |
lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)" |
|
14208 | 980 |
apply (induct xs, auto) |
981 |
apply (case_tac i, auto) |
|
13145 | 982 |
done |
13114 | 983 |
|
13142 | 984 |
lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i" |
14208 | 985 |
apply (induct xs, auto) |
986 |
apply (case_tac n, blast) |
|
987 |
apply (case_tac i, auto) |
|
13145 | 988 |
done |
13114 | 989 |
|
13142 | 990 |
lemma nth_drop [simp]: |
13145 | 991 |
"!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)" |
14208 | 992 |
apply (induct n, auto) |
993 |
apply (case_tac xs, auto) |
|
13145 | 994 |
done |
3507 | 995 |
|
14025 | 996 |
lemma set_take_subset: "\<And>n. set(take n xs) \<subseteq> set xs" |
997 |
by(induct xs)(auto simp:take_Cons split:nat.split) |
|
998 |
||
999 |
lemma set_drop_subset: "\<And>n. set(drop n xs) \<subseteq> set xs" |
|
1000 |
by(induct xs)(auto simp:drop_Cons split:nat.split) |
|
1001 |
||
14187 | 1002 |
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs" |
1003 |
using set_take_subset by fast |
|
1004 |
||
1005 |
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs" |
|
1006 |
using set_drop_subset by fast |
|
1007 |
||
13114 | 1008 |
lemma append_eq_conv_conj: |
13145 | 1009 |
"!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)" |
14208 | 1010 |
apply (induct xs, simp, clarsimp) |
1011 |
apply (case_tac zs, auto) |
|
13145 | 1012 |
done |
13142 | 1013 |
|
14050 | 1014 |
lemma take_add [rule_format]: |
1015 |
"\<forall>i. i+j \<le> length(xs) --> take (i+j) xs = take i xs @ take j (drop i xs)" |
|
1016 |
apply (induct xs, auto) |
|
1017 |
apply (case_tac i, simp_all) |
|
1018 |
done |
|
1019 |
||
14300 | 1020 |
lemma append_eq_append_conv_if: |
1021 |
"!! ys\<^isub>1. (xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) = |
|
1022 |
(if size xs\<^isub>1 \<le> size ys\<^isub>1 |
|
1023 |
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 |
|
1024 |
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)" |
|
1025 |
apply(induct xs\<^isub>1) |
|
1026 |
apply simp |
|
1027 |
apply(case_tac ys\<^isub>1) |
|
1028 |
apply simp_all |
|
1029 |
done |
|
1030 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1031 |
lemma take_hd_drop: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1032 |
"!!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
|
1033 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1034 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1035 |
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
|
1036 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1037 |
|
13114 | 1038 |
|
13142 | 1039 |
subsection {* @{text takeWhile} and @{text dropWhile} *} |
13114 | 1040 |
|
13142 | 1041 |
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs" |
13145 | 1042 |
by (induct xs) auto |
13114 | 1043 |
|
13142 | 1044 |
lemma takeWhile_append1 [simp]: |
13145 | 1045 |
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs" |
1046 |
by (induct xs) auto |
|
13114 | 1047 |
|
13142 | 1048 |
lemma takeWhile_append2 [simp]: |
13145 | 1049 |
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys" |
1050 |
by (induct xs) auto |
|
13114 | 1051 |
|
13142 | 1052 |
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs" |
13145 | 1053 |
by (induct xs) auto |
13114 | 1054 |
|
13142 | 1055 |
lemma dropWhile_append1 [simp]: |
13145 | 1056 |
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys" |
1057 |
by (induct xs) auto |
|
13114 | 1058 |
|
13142 | 1059 |
lemma dropWhile_append2 [simp]: |
13145 | 1060 |
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys" |
1061 |
by (induct xs) auto |
|
13114 | 1062 |
|
13142 | 1063 |
lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x" |
13145 | 1064 |
by (induct xs) (auto split: split_if_asm) |
13114 | 1065 |
|
13913 | 1066 |
lemma takeWhile_eq_all_conv[simp]: |
1067 |
"(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)" |
|
1068 |
by(induct xs, auto) |
|
1069 |
||
1070 |
lemma dropWhile_eq_Nil_conv[simp]: |
|
1071 |
"(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)" |
|
1072 |
by(induct xs, auto) |
|
1073 |
||
1074 |
lemma dropWhile_eq_Cons_conv: |
|
1075 |
"(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)" |
|
1076 |
by(induct xs, auto) |
|
1077 |
||
13114 | 1078 |
|
13142 | 1079 |
subsection {* @{text zip} *} |
13114 | 1080 |
|
13142 | 1081 |
lemma zip_Nil [simp]: "zip [] ys = []" |
13145 | 1082 |
by (induct ys) auto |
13114 | 1083 |
|
13142 | 1084 |
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys" |
13145 | 1085 |
by simp |
13114 | 1086 |
|
13142 | 1087 |
declare zip_Cons [simp del] |
13114 | 1088 |
|
13142 | 1089 |
lemma length_zip [simp]: |
13145 | 1090 |
"!!xs. length (zip xs ys) = min (length xs) (length ys)" |
14208 | 1091 |
apply (induct ys, simp) |
1092 |
apply (case_tac xs, auto) |
|
13145 | 1093 |
done |
13114 | 1094 |
|
1095 |
lemma zip_append1: |
|
13145 | 1096 |
"!!xs. zip (xs @ ys) zs = |
1097 |
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)" |
|
14208 | 1098 |
apply (induct zs, simp) |
1099 |
apply (case_tac xs, simp_all) |
|
13145 | 1100 |
done |
13114 | 1101 |
|
1102 |
lemma zip_append2: |
|
13145 | 1103 |
"!!ys. zip xs (ys @ zs) = |
1104 |
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs" |
|
14208 | 1105 |
apply (induct xs, simp) |
1106 |
apply (case_tac ys, simp_all) |
|
13145 | 1107 |
done |
13114 | 1108 |
|
13142 | 1109 |
lemma zip_append [simp]: |
1110 |
"[| length xs = length us; length ys = length vs |] ==> |
|
13145 | 1111 |
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs" |
1112 |
by (simp add: zip_append1) |
|
13114 | 1113 |
|
1114 |
lemma zip_rev: |
|
14247 | 1115 |
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)" |
1116 |
by (induct rule:list_induct2, simp_all) |
|
13114 | 1117 |
|
13142 | 1118 |
lemma nth_zip [simp]: |
13145 | 1119 |
"!!i xs. [| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)" |
14208 | 1120 |
apply (induct ys, simp) |
13145 | 1121 |
apply (case_tac xs) |
1122 |
apply (simp_all add: nth.simps split: nat.split) |
|
1123 |
done |
|
13114 | 1124 |
|
1125 |
lemma set_zip: |
|
13145 | 1126 |
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}" |
1127 |
by (simp add: set_conv_nth cong: rev_conj_cong) |
|
13114 | 1128 |
|
1129 |
lemma zip_update: |
|
13145 | 1130 |
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]" |
1131 |
by (rule sym, simp add: update_zip) |
|
13114 | 1132 |
|
13142 | 1133 |
lemma zip_replicate [simp]: |
13145 | 1134 |
"!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)" |
14208 | 1135 |
apply (induct i, auto) |
1136 |
apply (case_tac j, auto) |
|
13145 | 1137 |
done |
13114 | 1138 |
|
13142 | 1139 |
|
1140 |
subsection {* @{text list_all2} *} |
|
13114 | 1141 |
|
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1142 |
lemma list_all2_lengthD [intro?]: |
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1143 |
"list_all2 P xs ys ==> length xs = length ys" |
13145 | 1144 |
by (simp add: list_all2_def) |
13114 | 1145 |
|
13142 | 1146 |
lemma list_all2_Nil [iff]: "list_all2 P [] ys = (ys = [])" |
13145 | 1147 |
by (simp add: list_all2_def) |
13114 | 1148 |
|
13142 | 1149 |
lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])" |
13145 | 1150 |
by (simp add: list_all2_def) |
13114 | 1151 |
|
13142 | 1152 |
lemma list_all2_Cons [iff]: |
13145 | 1153 |
"list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)" |
1154 |
by (auto simp add: list_all2_def) |
|
13114 | 1155 |
|
1156 |
lemma list_all2_Cons1: |
|
13145 | 1157 |
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)" |
1158 |
by (cases ys) auto |
|
13114 | 1159 |
|
1160 |
lemma list_all2_Cons2: |
|
13145 | 1161 |
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)" |
1162 |
by (cases xs) auto |
|
13114 | 1163 |
|
13142 | 1164 |
lemma list_all2_rev [iff]: |
13145 | 1165 |
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys" |
1166 |
by (simp add: list_all2_def zip_rev cong: conj_cong) |
|
13114 | 1167 |
|
13863 | 1168 |
lemma list_all2_rev1: |
1169 |
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)" |
|
1170 |
by (subst list_all2_rev [symmetric]) simp |
|
1171 |
||
13114 | 1172 |
lemma list_all2_append1: |
13145 | 1173 |
"list_all2 P (xs @ ys) zs = |
1174 |
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and> |
|
1175 |
list_all2 P xs us \<and> list_all2 P ys vs)" |
|
1176 |
apply (simp add: list_all2_def zip_append1) |
|
1177 |
apply (rule iffI) |
|
1178 |
apply (rule_tac x = "take (length xs) zs" in exI) |
|
1179 |
apply (rule_tac x = "drop (length xs) zs" in exI) |
|
14208 | 1180 |
apply (force split: nat_diff_split simp add: min_def, clarify) |
13145 | 1181 |
apply (simp add: ball_Un) |
1182 |
done |
|
13114 | 1183 |
|
1184 |
lemma list_all2_append2: |
|
13145 | 1185 |
"list_all2 P xs (ys @ zs) = |
1186 |
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and> |
|
1187 |
list_all2 P us ys \<and> list_all2 P vs zs)" |
|
1188 |
apply (simp add: list_all2_def zip_append2) |
|
1189 |
apply (rule iffI) |
|
1190 |
apply (rule_tac x = "take (length ys) xs" in exI) |
|
1191 |
apply (rule_tac x = "drop (length ys) xs" in exI) |
|
14208 | 1192 |
apply (force split: nat_diff_split simp add: min_def, clarify) |
13145 | 1193 |
apply (simp add: ball_Un) |
1194 |
done |
|
13114 | 1195 |
|
13863 | 1196 |
lemma list_all2_append: |
14247 | 1197 |
"length xs = length ys \<Longrightarrow> |
1198 |
list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)" |
|
1199 |
by (induct rule:list_induct2, simp_all) |
|
13863 | 1200 |
|
1201 |
lemma list_all2_appendI [intro?, trans]: |
|
1202 |
"\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)" |
|
1203 |
by (simp add: list_all2_append list_all2_lengthD) |
|
1204 |
||
13114 | 1205 |
lemma list_all2_conv_all_nth: |
13145 | 1206 |
"list_all2 P xs ys = |
1207 |
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))" |
|
1208 |
by (force simp add: list_all2_def set_zip) |
|
13114 | 1209 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1210 |
lemma list_all2_trans: |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1211 |
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
|
1212 |
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
|
1213 |
(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
|
1214 |
proof (induct as) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1215 |
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
|
1216 |
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
|
1217 |
proof (induct bs) |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1218 |
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
|
1219 |
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
|
1220 |
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
|
1221 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1222 |
qed simp |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1223 |
|
13863 | 1224 |
lemma list_all2_all_nthI [intro?]: |
1225 |
"length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b" |
|
1226 |
by (simp add: list_all2_conv_all_nth) |
|
1227 |
||
14395 | 1228 |
lemma list_all2I: |
1229 |
"\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b" |
|
1230 |
by (simp add: list_all2_def) |
|
1231 |
||
14328 | 1232 |
lemma list_all2_nthD: |
13863 | 1233 |
"\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
1234 |
by (simp add: list_all2_conv_all_nth) |
|
1235 |
||
14302 | 1236 |
lemma list_all2_nthD2: |
1237 |
"\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)" |
|
1238 |
by (frule list_all2_lengthD) (auto intro: list_all2_nthD) |
|
1239 |
||
13863 | 1240 |
lemma list_all2_map1: |
1241 |
"list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs" |
|
1242 |
by (simp add: list_all2_conv_all_nth) |
|
1243 |
||
1244 |
lemma list_all2_map2: |
|
1245 |
"list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs" |
|
1246 |
by (auto simp add: list_all2_conv_all_nth) |
|
1247 |
||
14316
91b897b9a2dc
added some [intro?] and [trans] for list_all2 lemmas
kleing
parents:
14302
diff
changeset
|
1248 |
lemma list_all2_refl [intro?]: |
13863 | 1249 |
"(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs" |
1250 |
by (simp add: list_all2_conv_all_nth) |
|
1251 |
||
1252 |
lemma list_all2_update_cong: |
|
1253 |
"\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" |
|
1254 |
by (simp add: list_all2_conv_all_nth nth_list_update) |
|
1255 |
||
1256 |
lemma list_all2_update_cong2: |
|
1257 |
"\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])" |
|
1258 |
by (simp add: list_all2_lengthD list_all2_update_cong) |
|
1259 |
||
14302 | 1260 |
lemma list_all2_takeI [simp,intro?]: |
1261 |
"\<And>n ys. list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)" |
|
1262 |
apply (induct xs) |
|
1263 |
apply simp |
|
1264 |
apply (clarsimp simp add: list_all2_Cons1) |
|
1265 |
apply (case_tac n) |
|
1266 |
apply auto |
|
1267 |
done |
|
1268 |
||
1269 |
lemma list_all2_dropI [simp,intro?]: |
|
13863 | 1270 |
"\<And>n bs. list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)" |
14208 | 1271 |
apply (induct as, simp) |
13863 | 1272 |
apply (clarsimp simp add: list_all2_Cons1) |
14208 | 1273 |
apply (case_tac n, simp, simp) |
13863 | 1274 |
done |
1275 |
||
14327 | 1276 |
lemma list_all2_mono [intro?]: |
13863 | 1277 |
"\<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 | 1278 |
apply (induct x, simp) |
1279 |
apply (case_tac y, auto) |
|
13863 | 1280 |
done |
1281 |
||
13142 | 1282 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1283 |
subsection {* @{text foldl} and @{text foldr} *} |
13142 | 1284 |
|
1285 |
lemma foldl_append [simp]: |
|
13145 | 1286 |
"!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys" |
1287 |
by (induct xs) auto |
|
13142 | 1288 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1289 |
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
|
1290 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1291 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1292 |
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
|
1293 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1294 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1295 |
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
|
1296 |
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
|
1297 |
|
13142 | 1298 |
text {* |
13145 | 1299 |
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more |
1300 |
difficult to use because it requires an additional transitivity step. |
|
13142 | 1301 |
*} |
1302 |
||
1303 |
lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns" |
|
13145 | 1304 |
by (induct ns) auto |
13142 | 1305 |
|
1306 |
lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns" |
|
13145 | 1307 |
by (force intro: start_le_sum simp add: in_set_conv_decomp) |
13142 | 1308 |
|
1309 |
lemma sum_eq_0_conv [iff]: |
|
13145 | 1310 |
"!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))" |
1311 |
by (induct ns) auto |
|
13114 | 1312 |
|
1313 |
||
13142 | 1314 |
subsection {* @{text upto} *} |
13114 | 1315 |
|
13142 | 1316 |
lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])" |
13145 | 1317 |
-- {* Does not terminate! *} |
1318 |
by (induct j) auto |
|
13142 | 1319 |
|
1320 |
lemma upt_conv_Nil [simp]: "j <= i ==> [i..j(] = []" |
|
13145 | 1321 |
by (subst upt_rec) simp |
13114 | 1322 |
|
13142 | 1323 |
lemma upt_Suc_append: "i <= j ==> [i..(Suc j)(] = [i..j(]@[j]" |
13145 | 1324 |
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *} |
1325 |
by simp |
|
13114 | 1326 |
|
13142 | 1327 |
lemma upt_conv_Cons: "i < j ==> [i..j(] = i # [Suc i..j(]" |
13145 | 1328 |
apply(rule trans) |
1329 |
apply(subst upt_rec) |
|
14208 | 1330 |
prefer 2 apply (rule refl, simp) |
13145 | 1331 |
done |
13114 | 1332 |
|
13142 | 1333 |
lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]" |
13145 | 1334 |
-- {* LOOPS as a simprule, since @{text "j <= j"}. *} |
1335 |
by (induct k) auto |
|
13114 | 1336 |
|
13142 | 1337 |
lemma length_upt [simp]: "length [i..j(] = j - i" |
13145 | 1338 |
by (induct j) (auto simp add: Suc_diff_le) |
13114 | 1339 |
|
13142 | 1340 |
lemma nth_upt [simp]: "i + k < j ==> [i..j(] ! k = i + k" |
13145 | 1341 |
apply (induct j) |
1342 |
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split) |
|
1343 |
done |
|
13114 | 1344 |
|
13142 | 1345 |
lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]" |
14208 | 1346 |
apply (induct m, simp) |
13145 | 1347 |
apply (subst upt_rec) |
1348 |
apply (rule sym) |
|
1349 |
apply (subst upt_rec) |
|
1350 |
apply (simp del: upt.simps) |
|
1351 |
done |
|
3507 | 1352 |
|
13114 | 1353 |
lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]" |
13145 | 1354 |
by (induct n) auto |
13114 | 1355 |
|
1356 |
lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)" |
|
13145 | 1357 |
apply (induct n m rule: diff_induct) |
1358 |
prefer 3 apply (subst map_Suc_upt[symmetric]) |
|
1359 |
apply (auto simp add: less_diff_conv nth_upt) |
|
1360 |
done |
|
13114 | 1361 |
|
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1362 |
lemma nth_take_lemma: |
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
1363 |
"!!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
|
1364 |
(!!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
|
1365 |
apply (atomize, induct k) |
14208 | 1366 |
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify) |
13145 | 1367 |
txt {* Both lists must be non-empty *} |
14208 | 1368 |
apply (case_tac xs, simp) |
1369 |
apply (case_tac ys, clarify) |
|
13145 | 1370 |
apply (simp (no_asm_use)) |
1371 |
apply clarify |
|
1372 |
txt {* prenexing's needed, not miniscoping *} |
|
1373 |
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps) |
|
1374 |
apply blast |
|
1375 |
done |
|
13114 | 1376 |
|
1377 |
lemma nth_equalityI: |
|
1378 |
"[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys" |
|
13145 | 1379 |
apply (frule nth_take_lemma [OF le_refl eq_imp_le]) |
1380 |
apply (simp_all add: take_all) |
|
1381 |
done |
|
13142 | 1382 |
|
13863 | 1383 |
(* needs nth_equalityI *) |
1384 |
lemma list_all2_antisym: |
|
1385 |
"\<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> |
|
1386 |
\<Longrightarrow> xs = ys" |
|
1387 |
apply (simp add: list_all2_conv_all_nth) |
|
14208 | 1388 |
apply (rule nth_equalityI, blast, simp) |
13863 | 1389 |
done |
1390 |
||
13142 | 1391 |
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys" |
13145 | 1392 |
-- {* The famous take-lemma. *} |
1393 |
apply (drule_tac x = "max (length xs) (length ys)" in spec) |
|
1394 |
apply (simp add: le_max_iff_disj take_all) |
|
1395 |
done |
|
13142 | 1396 |
|
1397 |
||
1398 |
subsection {* @{text "distinct"} and @{text remdups} *} |
|
1399 |
||
1400 |
lemma distinct_append [simp]: |
|
13145 | 1401 |
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})" |
1402 |
by (induct xs) auto |
|
13142 | 1403 |
|
1404 |
lemma set_remdups [simp]: "set (remdups xs) = set xs" |
|
13145 | 1405 |
by (induct xs) (auto simp add: insert_absorb) |
13142 | 1406 |
|
1407 |
lemma distinct_remdups [iff]: "distinct (remdups xs)" |
|
13145 | 1408 |
by (induct xs) auto |
13142 | 1409 |
|
15072 | 1410 |
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])" |
1411 |
by (induct_tac x, auto) |
|
1412 |
||
1413 |
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])" |
|
1414 |
by (induct_tac x, auto) |
|
1415 |
||
13142 | 1416 |
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)" |
13145 | 1417 |
by (induct xs) auto |
13114 | 1418 |
|
13142 | 1419 |
text {* |
13145 | 1420 |
It is best to avoid this indexed version of distinct, but sometimes |
1421 |
it is useful. *} |
|
13142 | 1422 |
lemma distinct_conv_nth: |
13145 | 1423 |
"distinct xs = (\<forall>i j. i < size xs \<and> j < size xs \<and> i \<noteq> j --> xs!i \<noteq> xs!j)" |
14208 | 1424 |
apply (induct_tac xs, simp, simp) |
1425 |
apply (rule iffI, clarsimp) |
|
13145 | 1426 |
apply (case_tac i) |
14208 | 1427 |
apply (case_tac j, simp) |
13145 | 1428 |
apply (simp add: set_conv_nth) |
1429 |
apply (case_tac j) |
|
14208 | 1430 |
apply (clarsimp simp add: set_conv_nth, simp) |
13145 | 1431 |
apply (rule conjI) |
1432 |
apply (clarsimp simp add: set_conv_nth) |
|
1433 |
apply (erule_tac x = 0 in allE) |
|
14208 | 1434 |
apply (erule_tac x = "Suc i" in allE, simp, clarsimp) |
13145 | 1435 |
apply (erule_tac x = "Suc i" in allE) |
14208 | 1436 |
apply (erule_tac x = "Suc j" in allE, simp) |
13145 | 1437 |
done |
13114 | 1438 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1439 |
lemma distinct_card: "distinct xs ==> card (set xs) = size xs" |
14388 | 1440 |
by (induct xs) auto |
1441 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1442 |
lemma card_distinct: "card (set xs) = size xs ==> distinct xs" |
14388 | 1443 |
proof (induct xs) |
1444 |
case Nil thus ?case by simp |
|
1445 |
next |
|
1446 |
case (Cons x xs) |
|
1447 |
show ?case |
|
1448 |
proof (cases "x \<in> set xs") |
|
1449 |
case False with Cons show ?thesis by simp |
|
1450 |
next |
|
1451 |
case True with Cons.prems |
|
1452 |
have "card (set xs) = Suc (length xs)" |
|
1453 |
by (simp add: card_insert_if split: split_if_asm) |
|
1454 |
moreover have "card (set xs) \<le> length xs" by (rule card_length) |
|
1455 |
ultimately have False by simp |
|
1456 |
thus ?thesis .. |
|
1457 |
qed |
|
1458 |
qed |
|
1459 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1460 |
lemma inj_on_setI: "distinct(map f xs) ==> inj_on f (set xs)" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1461 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1462 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1463 |
apply fastsimp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1464 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1465 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1466 |
lemma inj_on_set_conv: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1467 |
"distinct xs \<Longrightarrow> inj_on f (set xs) = distinct(map f xs)" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1468 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1469 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1470 |
apply fastsimp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1471 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1472 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1473 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1474 |
subsection {* @{text remove1} *} |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1475 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1476 |
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
|
1477 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1478 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1479 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1480 |
apply blast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1481 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1482 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1483 |
lemma [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1484 |
apply(induct xs) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1485 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1486 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1487 |
apply blast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1488 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1489 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1490 |
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
|
1491 |
apply(insert set_remove1_subset) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1492 |
apply fast |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1493 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1494 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1495 |
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
|
1496 |
by (induct xs) simp_all |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1497 |
|
13114 | 1498 |
|
13142 | 1499 |
subsection {* @{text replicate} *} |
13114 | 1500 |
|
13142 | 1501 |
lemma length_replicate [simp]: "length (replicate n x) = n" |
13145 | 1502 |
by (induct n) auto |
13124 | 1503 |
|
13142 | 1504 |
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)" |
13145 | 1505 |
by (induct n) auto |
13114 | 1506 |
|
1507 |
lemma replicate_app_Cons_same: |
|
13145 | 1508 |
"(replicate n x) @ (x # xs) = x # replicate n x @ xs" |
1509 |
by (induct n) auto |
|
13114 | 1510 |
|
13142 | 1511 |
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x" |
14208 | 1512 |
apply (induct n, simp) |
13145 | 1513 |
apply (simp add: replicate_app_Cons_same) |
1514 |
done |
|
13114 | 1515 |
|
13142 | 1516 |
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x" |
13145 | 1517 |
by (induct n) auto |
13114 | 1518 |
|
13142 | 1519 |
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x" |
13145 | 1520 |
by (induct n) auto |
13114 | 1521 |
|
13142 | 1522 |
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x" |
13145 | 1523 |
by (induct n) auto |
13114 | 1524 |
|
13142 | 1525 |
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x" |
13145 | 1526 |
by (atomize (full), induct n) auto |
13114 | 1527 |
|
13142 | 1528 |
lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x" |
14208 | 1529 |
apply (induct n, simp) |
13145 | 1530 |
apply (simp add: nth_Cons split: nat.split) |
1531 |
done |
|
13114 | 1532 |
|
13142 | 1533 |
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}" |
13145 | 1534 |
by (induct n) auto |
13114 | 1535 |
|
13142 | 1536 |
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}" |
13145 | 1537 |
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc) |
13114 | 1538 |
|
13142 | 1539 |
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})" |
13145 | 1540 |
by auto |
13114 | 1541 |
|
13142 | 1542 |
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y" |
13145 | 1543 |
by (simp add: set_replicate_conv_if split: split_if_asm) |
13114 | 1544 |
|
1545 |
||
14099 | 1546 |
subsection {* Lexicographic orderings on lists *} |
3507 | 1547 |
|
13142 | 1548 |
lemma wf_lexn: "wf r ==> wf (lexn r n)" |
14208 | 1549 |
apply (induct_tac n, simp, simp) |
13145 | 1550 |
apply(rule wf_subset) |
1551 |
prefer 2 apply (rule Int_lower1) |
|
1552 |
apply(rule wf_prod_fun_image) |
|
14208 | 1553 |
prefer 2 apply (rule inj_onI, auto) |
13145 | 1554 |
done |
13114 | 1555 |
|
1556 |
lemma lexn_length: |
|
13145 | 1557 |
"!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n" |
1558 |
by (induct n) auto |
|
13114 | 1559 |
|
13142 | 1560 |
lemma wf_lex [intro!]: "wf r ==> wf (lex r)" |
13145 | 1561 |
apply (unfold lex_def) |
1562 |
apply (rule wf_UN) |
|
14208 | 1563 |
apply (blast intro: wf_lexn, clarify) |
13145 | 1564 |
apply (rename_tac m n) |
1565 |
apply (subgoal_tac "m \<noteq> n") |
|
1566 |
prefer 2 apply blast |
|
1567 |
apply (blast dest: lexn_length not_sym) |
|
1568 |
done |
|
13114 | 1569 |
|
1570 |
lemma lexn_conv: |
|
13145 | 1571 |
"lexn r n = |
1572 |
{(xs,ys). length xs = n \<and> length ys = n \<and> |
|
1573 |
(\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}" |
|
14208 | 1574 |
apply (induct_tac n, simp, blast) |
1575 |
apply (simp add: image_Collect lex_prod_def, safe, blast) |
|
1576 |
apply (rule_tac x = "ab # xys" in exI, simp) |
|
1577 |
apply (case_tac xys, simp_all, blast) |
|
13145 | 1578 |
done |
13114 | 1579 |
|
1580 |
lemma lex_conv: |
|
13145 | 1581 |
"lex r = |
1582 |
{(xs,ys). length xs = length ys \<and> |
|
1583 |
(\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}" |
|
1584 |
by (force simp add: lex_def lexn_conv) |
|
13114 | 1585 |
|
13142 | 1586 |
lemma wf_lexico [intro!]: "wf r ==> wf (lexico r)" |
13145 | 1587 |
by (unfold lexico_def) blast |
13114 | 1588 |
|
1589 |
lemma lexico_conv: |
|
13145 | 1590 |
"lexico r = {(xs,ys). length xs < length ys | |
1591 |
length xs = length ys \<and> (xs, ys) : lex r}" |
|
1592 |
by (simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def) |
|
13114 | 1593 |
|
13142 | 1594 |
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r" |
13145 | 1595 |
by (simp add: lex_conv) |
13114 | 1596 |
|
13142 | 1597 |
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r" |
13145 | 1598 |
by (simp add:lex_conv) |
13114 | 1599 |
|
13142 | 1600 |
lemma Cons_in_lex [iff]: |
13145 | 1601 |
"((x # xs, y # ys) : lex r) = |
1602 |
((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)" |
|
1603 |
apply (simp add: lex_conv) |
|
1604 |
apply (rule iffI) |
|
14208 | 1605 |
prefer 2 apply (blast intro: Cons_eq_appendI, clarify) |
1606 |
apply (case_tac xys, simp, simp) |
|
13145 | 1607 |
apply blast |
1608 |
done |
|
13114 | 1609 |
|
1610 |
||
13142 | 1611 |
subsection {* @{text sublist} --- a generalization of @{text nth} to sets *} |
13114 | 1612 |
|
13142 | 1613 |
lemma sublist_empty [simp]: "sublist xs {} = []" |
13145 | 1614 |
by (auto simp add: sublist_def) |
13114 | 1615 |
|
13142 | 1616 |
lemma sublist_nil [simp]: "sublist [] A = []" |
13145 | 1617 |
by (auto simp add: sublist_def) |
13114 | 1618 |
|
1619 |
lemma sublist_shift_lemma: |
|
13145 | 1620 |
"map fst [p:zip xs [i..i + length xs(] . snd p : A] = |
1621 |
map fst [p:zip xs [0..length xs(] . snd p + i : A]" |
|
1622 |
by (induct xs rule: rev_induct) (simp_all add: add_commute) |
|
13114 | 1623 |
|
1624 |
lemma sublist_append: |
|
13145 | 1625 |
"sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}" |
1626 |
apply (unfold sublist_def) |
|
14208 | 1627 |
apply (induct l' rule: rev_induct, simp) |
13145 | 1628 |
apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma) |
1629 |
apply (simp add: add_commute) |
|
1630 |
done |
|
13114 | 1631 |
|
1632 |
lemma sublist_Cons: |
|
13145 | 1633 |
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}" |
1634 |
apply (induct l rule: rev_induct) |
|
1635 |
apply (simp add: sublist_def) |
|
1636 |
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append) |
|
1637 |
done |
|
13114 | 1638 |
|
13142 | 1639 |
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])" |
13145 | 1640 |
by (simp add: sublist_Cons) |
13114 | 1641 |
|
15045 | 1642 |
lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l" |
14208 | 1643 |
apply (induct l rule: rev_induct, simp) |
13145 | 1644 |
apply (simp split: nat_diff_split add: sublist_append) |
1645 |
done |
|
13114 | 1646 |
|
1647 |
||
13142 | 1648 |
lemma take_Cons': |
13145 | 1649 |
"take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)" |
1650 |
by (cases n) simp_all |
|
13114 | 1651 |
|
13142 | 1652 |
lemma drop_Cons': |
13145 | 1653 |
"drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)" |
1654 |
by (cases n) simp_all |
|
13114 | 1655 |
|
13142 | 1656 |
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))" |
13145 | 1657 |
by (cases n) simp_all |
13142 | 1658 |
|
13145 | 1659 |
lemmas [simp] = take_Cons'[of "number_of v",standard] |
1660 |
drop_Cons'[of "number_of v",standard] |
|
1661 |
nth_Cons'[of _ _ "number_of v",standard] |
|
3507 | 1662 |
|
13462 | 1663 |
|
14388 | 1664 |
lemma "card (set xs) = size xs \<Longrightarrow> distinct xs" |
1665 |
proof (induct xs) |
|
1666 |
case Nil thus ?case by simp |
|
1667 |
next |
|
1668 |
case (Cons x xs) |
|
1669 |
show ?case |
|
1670 |
proof (cases "x \<in> set xs") |
|
1671 |
case False with Cons show ?thesis by simp |
|
1672 |
next |
|
1673 |
case True with Cons.prems |
|
1674 |
have "card (set xs) = Suc (length xs)" |
|
1675 |
by (simp add: card_insert_if split: split_if_asm) |
|
1676 |
moreover have "card (set xs) \<le> length xs" by (rule card_length) |
|
1677 |
ultimately have False by simp |
|
1678 |
thus ?thesis .. |
|
1679 |
qed |
|
1680 |
qed |
|
1681 |
||
13366 | 1682 |
subsection {* Characters and strings *} |
1683 |
||
1684 |
datatype nibble = |
|
1685 |
Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7 |
|
1686 |
| Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF |
|
1687 |
||
1688 |
datatype char = Char nibble nibble |
|
1689 |
-- "Note: canonical order of character encoding coincides with standard term ordering" |
|
1690 |
||
1691 |
types string = "char list" |
|
1692 |
||
1693 |
syntax |
|
1694 |
"_Char" :: "xstr => char" ("CHR _") |
|
1695 |
"_String" :: "xstr => string" ("_") |
|
1696 |
||
1697 |
parse_ast_translation {* |
|
1698 |
let |
|
1699 |
val constants = Syntax.Appl o map Syntax.Constant; |
|
1700 |
||
1701 |
fun mk_nib n = "Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10)); |
|
1702 |
fun mk_char c = |
|
1703 |
if Symbol.is_ascii c andalso Symbol.is_printable c then |
|
1704 |
constants ["Char", mk_nib (ord c div 16), mk_nib (ord c mod 16)] |
|
1705 |
else error ("Printable ASCII character expected: " ^ quote c); |
|
1706 |
||
1707 |
fun mk_string [] = Syntax.Constant "Nil" |
|
1708 |
| mk_string (c :: cs) = Syntax.Appl [Syntax.Constant "Cons", mk_char c, mk_string cs]; |
|
1709 |
||
1710 |
fun char_ast_tr [Syntax.Variable xstr] = |
|
1711 |
(case Syntax.explode_xstr xstr of |
|
1712 |
[c] => mk_char c |
|
1713 |
| _ => error ("Single character expected: " ^ xstr)) |
|
1714 |
| char_ast_tr asts = raise AST ("char_ast_tr", asts); |
|
1715 |
||
1716 |
fun string_ast_tr [Syntax.Variable xstr] = |
|
1717 |
(case Syntax.explode_xstr xstr of |
|
1718 |
[] => constants [Syntax.constrainC, "Nil", "string"] |
|
1719 |
| cs => mk_string cs) |
|
1720 |
| string_ast_tr asts = raise AST ("string_tr", asts); |
|
1721 |
in [("_Char", char_ast_tr), ("_String", string_ast_tr)] end; |
|
1722 |
*} |
|
1723 |
||
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1724 |
ML {* |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1725 |
fun int_of_nibble h = |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1726 |
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
|
1727 |
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
|
1728 |
else raise Match; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1729 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1730 |
fun nibble_of_int i = |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1731 |
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
|
1732 |
*} |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1733 |
|
13366 | 1734 |
print_ast_translation {* |
1735 |
let |
|
1736 |
fun dest_nib (Syntax.Constant c) = |
|
1737 |
(case explode c of |
|
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1738 |
["N", "i", "b", "b", "l", "e", h] => int_of_nibble h |
13366 | 1739 |
| _ => raise Match) |
1740 |
| dest_nib _ = raise Match; |
|
1741 |
||
1742 |
fun dest_chr c1 c2 = |
|
1743 |
let val c = chr (dest_nib c1 * 16 + dest_nib c2) |
|
1744 |
in if Symbol.is_printable c then c else raise Match end; |
|
1745 |
||
1746 |
fun dest_char (Syntax.Appl [Syntax.Constant "Char", c1, c2]) = dest_chr c1 c2 |
|
1747 |
| dest_char _ = raise Match; |
|
1748 |
||
1749 |
fun xstr cs = Syntax.Appl [Syntax.Constant "_xstr", Syntax.Variable (Syntax.implode_xstr cs)]; |
|
1750 |
||
1751 |
fun char_ast_tr' [c1, c2] = Syntax.Appl [Syntax.Constant "_Char", xstr [dest_chr c1 c2]] |
|
1752 |
| char_ast_tr' _ = raise Match; |
|
1753 |
||
1754 |
fun list_ast_tr' [args] = Syntax.Appl [Syntax.Constant "_String", |
|
1755 |
xstr (map dest_char (Syntax.unfold_ast "_args" args))] |
|
1756 |
| list_ast_tr' ts = raise Match; |
|
1757 |
in [("Char", char_ast_tr'), ("@list", list_ast_tr')] end; |
|
1758 |
*} |
|
1759 |
||
15064
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1760 |
subsection {* Code generator setup *} |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1761 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1762 |
ML {* |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1763 |
local |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1764 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1765 |
fun list_codegen thy gr dep b t = |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1766 |
let val (gr', ps) = foldl_map (Codegen.invoke_codegen thy dep false) |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1767 |
(gr, HOLogic.dest_list t) |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1768 |
in Some (gr', Pretty.list "[" "]" ps) end handle TERM _ => None; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1769 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1770 |
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
|
1771 |
| dest_nibble _ = raise Match; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1772 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1773 |
fun char_codegen thy gr dep b (Const ("List.char.Char", _) $ c1 $ c2) = |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1774 |
(let val c = chr (dest_nibble c1 * 16 + dest_nibble c2) |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1775 |
in if Symbol.is_printable c then Some (gr, Pretty.quote (Pretty.str c)) |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1776 |
else None |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1777 |
end handle LIST _ => None | Match => None) |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1778 |
| char_codegen thy gr dep b _ = None; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1779 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1780 |
in |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1781 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1782 |
val list_codegen_setup = |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1783 |
[Codegen.add_codegen "list_codegen" list_codegen, |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1784 |
Codegen.add_codegen "char_codegen" char_codegen]; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1785 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1786 |
end; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1787 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1788 |
val term_of_list = HOLogic.mk_list; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1789 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1790 |
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
|
1791 |
[(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
|
1792 |
and gen_list aG i = gen_list' aG i i; |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1793 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1794 |
val nibbleT = Type ("List.nibble", []); |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1795 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1796 |
fun term_of_char c = |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1797 |
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
|
1798 |
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
|
1799 |
Const ("List.nibble.Nibble" ^ nibble_of_int (ord c mod 16), nibbleT); |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1800 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1801 |
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
|
1802 |
*} |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1803 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1804 |
types_code |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1805 |
"list" ("_ list") |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1806 |
"char" ("string") |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1807 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1808 |
consts_code "Cons" ("(_ ::/ _)") |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1809 |
|
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1810 |
setup list_codegen_setup |
4f3102b50197
- Moved code generator setup for lists from Main.thy to List.thy
berghofe
parents:
15045
diff
changeset
|
1811 |
|
13122 | 1812 |
end |