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