author | nipkow |
Mon, 01 Sep 2008 19:16:40 +0200 | |
changeset 28068 | f6b2d1995171 |
parent 28064 | d4a6460c53d1 |
child 28072 | a45e8c872dc1 |
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 |
27368 | 9 |
imports Plain Relation_Power Presburger Recdef ATP_Linkup |
21754
6316163ae934
moved char/string syntax to Tools/string_syntax.ML;
wenzelm
parents:
21548
diff
changeset
|
10 |
uses "Tools/string_syntax.ML" |
15131 | 11 |
begin |
923 | 12 |
|
13142 | 13 |
datatype 'a list = |
13366 | 14 |
Nil ("[]") |
15 |
| Cons 'a "'a list" (infixr "#" 65) |
|
923 | 16 |
|
15392 | 17 |
subsection{*Basic list processing functions*} |
15302 | 18 |
|
923 | 19 |
consts |
13366 | 20 |
filter:: "('a => bool) => 'a list => 'a list" |
21 |
concat:: "'a list list => 'a list" |
|
22 |
foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b" |
|
23 |
foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b" |
|
24 |
hd:: "'a list => 'a" |
|
25 |
tl:: "'a list => 'a list" |
|
26 |
last:: "'a list => 'a" |
|
27 |
butlast :: "'a list => 'a list" |
|
28 |
set :: "'a list => 'a set" |
|
29 |
map :: "('a=>'b) => ('a list => 'b list)" |
|
23096 | 30 |
listsum :: "'a list => 'a::monoid_add" |
13366 | 31 |
nth :: "'a list => nat => 'a" (infixl "!" 100) |
32 |
list_update :: "'a list => nat => 'a => 'a list" |
|
33 |
take:: "nat => 'a list => 'a list" |
|
34 |
drop:: "nat => 'a list => 'a list" |
|
35 |
takeWhile :: "('a => bool) => 'a list => 'a list" |
|
36 |
dropWhile :: "('a => bool) => 'a list => 'a list" |
|
37 |
rev :: "'a list => 'a list" |
|
38 |
zip :: "'a list => 'b list => ('a * 'b) list" |
|
15425 | 39 |
upt :: "nat => nat => nat list" ("(1[_..</_'])") |
13366 | 40 |
remdups :: "'a list => 'a list" |
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
41 |
remove1 :: "'a => 'a list => 'a list" |
27693 | 42 |
removeAll :: "'a => 'a list => 'a list" |
13366 | 43 |
"distinct":: "'a list => bool" |
44 |
replicate :: "nat => 'a => 'a list" |
|
19390 | 45 |
splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" |
15302 | 46 |
|
923 | 47 |
|
13146 | 48 |
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
|
49 |
|
923 | 50 |
syntax |
13366 | 51 |
-- {* list Enumeration *} |
52 |
"@list" :: "args => 'a list" ("[(_)]") |
|
923 | 53 |
|
13366 | 54 |
-- {* Special syntax for filter *} |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
55 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list" ("(1[_<-_./ _])") |
923 | 56 |
|
13366 | 57 |
-- {* list update *} |
58 |
"_lupdbind":: "['a, 'a] => lupdbind" ("(2_ :=/ _)") |
|
59 |
"" :: "lupdbind => lupdbinds" ("_") |
|
60 |
"_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _") |
|
61 |
"_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
|
62 |
|
923 | 63 |
translations |
13366 | 64 |
"[x, xs]" == "x#[xs]" |
65 |
"[x]" == "x#[]" |
|
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
66 |
"[x<-xs . P]"== "filter (%x. P) xs" |
923 | 67 |
|
13366 | 68 |
"_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs" |
69 |
"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
|
70 |
|
5427 | 71 |
|
12114
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
wenzelm
parents:
10832
diff
changeset
|
72 |
syntax (xsymbols) |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
73 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])") |
14565 | 74 |
syntax (HTML output) |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
75 |
"@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])") |
3342
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
76 |
|
ec3b55fcb165
New operator "lists" for formalizing sets of lists
paulson
parents:
3320
diff
changeset
|
77 |
|
13142 | 78 |
text {* |
14589 | 79 |
Function @{text size} is overloaded for all datatypes. Users may |
13366 | 80 |
refer to the list version as @{text length}. *} |
13142 | 81 |
|
19363 | 82 |
abbreviation |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
83 |
length :: "'a list => nat" where |
19363 | 84 |
"length == size" |
15302 | 85 |
|
5183 | 86 |
primrec |
15307 | 87 |
"hd(x#xs) = x" |
88 |
||
5183 | 89 |
primrec |
15307 | 90 |
"tl([]) = []" |
91 |
"tl(x#xs) = xs" |
|
92 |
||
5183 | 93 |
primrec |
15307 | 94 |
"last(x#xs) = (if xs=[] then x else last xs)" |
95 |
||
5183 | 96 |
primrec |
15307 | 97 |
"butlast []= []" |
98 |
"butlast(x#xs) = (if xs=[] then [] else x#butlast xs)" |
|
99 |
||
5183 | 100 |
primrec |
15307 | 101 |
"set [] = {}" |
102 |
"set (x#xs) = insert x (set xs)" |
|
103 |
||
5183 | 104 |
primrec |
15307 | 105 |
"map f [] = []" |
106 |
"map f (x#xs) = f(x)#map f xs" |
|
107 |
||
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
108 |
primrec |
25559 | 109 |
append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) |
110 |
where |
|
111 |
append_Nil:"[] @ ys = ys" |
|
112 |
| append_Cons: "(x#xs) @ ys = x # xs @ ys" |
|
15307 | 113 |
|
5183 | 114 |
primrec |
15307 | 115 |
"rev([]) = []" |
116 |
"rev(x#xs) = rev(xs) @ [x]" |
|
117 |
||
5183 | 118 |
primrec |
15307 | 119 |
"filter P [] = []" |
120 |
"filter P (x#xs) = (if P x then x#filter P xs else filter P xs)" |
|
121 |
||
5183 | 122 |
primrec |
15307 | 123 |
foldl_Nil:"foldl f a [] = a" |
124 |
foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs" |
|
125 |
||
8000 | 126 |
primrec |
15307 | 127 |
"foldr f [] a = a" |
128 |
"foldr f (x#xs) a = f x (foldr f xs a)" |
|
129 |
||
5183 | 130 |
primrec |
15307 | 131 |
"concat([]) = []" |
132 |
"concat(x#xs) = x @ concat(xs)" |
|
133 |
||
5183 | 134 |
primrec |
23096 | 135 |
"listsum [] = 0" |
136 |
"listsum (x # xs) = x + listsum xs" |
|
137 |
||
138 |
primrec |
|
15307 | 139 |
drop_Nil:"drop n [] = []" |
140 |
drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)" |
|
141 |
-- {*Warning: simpset does not contain this definition, but separate |
|
142 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
143 |
||
5183 | 144 |
primrec |
15307 | 145 |
take_Nil:"take n [] = []" |
146 |
take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)" |
|
147 |
-- {*Warning: simpset does not contain this definition, but separate |
|
148 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
149 |
||
13142 | 150 |
primrec |
15307 | 151 |
nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)" |
152 |
-- {*Warning: simpset does not contain this definition, but separate |
|
153 |
theorems for @{text "n = 0"} and @{text "n = Suc k"} *} |
|
154 |
||
5183 | 155 |
primrec |
15307 | 156 |
"[][i:=v] = []" |
157 |
"(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])" |
|
158 |
||
159 |
primrec |
|
160 |
"takeWhile P [] = []" |
|
161 |
"takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])" |
|
162 |
||
5183 | 163 |
primrec |
15307 | 164 |
"dropWhile P [] = []" |
165 |
"dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)" |
|
166 |
||
5183 | 167 |
primrec |
15307 | 168 |
"zip xs [] = []" |
169 |
zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)" |
|
170 |
-- {*Warning: simpset does not contain this definition, but separate |
|
171 |
theorems for @{text "xs = []"} and @{text "xs = z # zs"} *} |
|
172 |
||
5427 | 173 |
primrec |
15425 | 174 |
upt_0: "[i..<0] = []" |
175 |
upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])" |
|
15307 | 176 |
|
5183 | 177 |
primrec |
15307 | 178 |
"distinct [] = True" |
179 |
"distinct (x#xs) = (x ~: set xs \<and> distinct xs)" |
|
180 |
||
5183 | 181 |
primrec |
15307 | 182 |
"remdups [] = []" |
183 |
"remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)" |
|
184 |
||
5183 | 185 |
primrec |
15307 | 186 |
"remove1 x [] = []" |
187 |
"remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)" |
|
188 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
189 |
primrec |
27693 | 190 |
"removeAll x [] = []" |
191 |
"removeAll x (y#xs) = (if x=y then removeAll x xs else y # removeAll x xs)" |
|
192 |
||
193 |
primrec |
|
15307 | 194 |
replicate_0: "replicate 0 x = []" |
195 |
replicate_Suc: "replicate (Suc n) x = x # replicate n x" |
|
196 |
||
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
197 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
198 |
rotate1 :: "'a list \<Rightarrow> 'a list" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
199 |
"rotate1 xs = (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])" |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
200 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
201 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
202 |
rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
203 |
"rotate n = rotate1 ^ n" |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
204 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
205 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
206 |
list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where |
25966 | 207 |
[code func del]: "list_all2 P xs ys = |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
208 |
(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
|
209 |
|
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
210 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
211 |
sublist :: "'a list => nat set => 'a list" where |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21211
diff
changeset
|
212 |
"sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))" |
17086 | 213 |
|
214 |
primrec |
|
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
215 |
"splice [] ys = ys" |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
216 |
"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
|
217 |
-- {*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
|
218 |
|
26771 | 219 |
text{* |
220 |
\begin{figure}[htbp] |
|
221 |
\fbox{ |
|
222 |
\begin{tabular}{l} |
|
27381 | 223 |
@{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\ |
224 |
@{lemma "length [a,b,c] = 3" by simp}\\ |
|
225 |
@{lemma "set [a,b,c] = {a,b,c}" by simp}\\ |
|
226 |
@{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\ |
|
227 |
@{lemma "rev [a,b,c] = [c,b,a]" by simp}\\ |
|
228 |
@{lemma "hd [a,b,c,d] = a" by simp}\\ |
|
229 |
@{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\ |
|
230 |
@{lemma "last [a,b,c,d] = d" by simp}\\ |
|
231 |
@{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\ |
|
232 |
@{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\ |
|
233 |
@{lemma "concat [[a,b],[c,d,e],[],[f]] = [a,b,c,d,e,f]" by simp}\\ |
|
234 |
@{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\ |
|
235 |
@{lemma "foldr f [a,b,c] x = f a (f b (f c x))" by simp}\\ |
|
236 |
@{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\ |
|
237 |
@{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\ |
|
238 |
@{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\ |
|
239 |
@{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\ |
|
240 |
@{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\ |
|
241 |
@{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\ |
|
242 |
@{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\ |
|
243 |
@{lemma "drop 6 [a,b,c,d] = []" by simp}\\ |
|
244 |
@{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\ |
|
245 |
@{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\ |
|
246 |
@{lemma "distinct [2,0,1::nat]" by simp}\\ |
|
247 |
@{lemma "remdups [2,0,2,1::nat,2] = [0,1,2]" by simp}\\ |
|
248 |
@{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\ |
|
27693 | 249 |
@{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\ |
27381 | 250 |
@{lemma "nth [a,b,c,d] 2 = c" by simp}\\ |
251 |
@{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\ |
|
252 |
@{lemma "sublist [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:sublist_def)}\\ |
|
253 |
@{lemma "rotate1 [a,b,c,d] = [b,c,d,a]" by (simp add:rotate1_def)}\\ |
|
254 |
@{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate1_def rotate_def nat_number)}\\ |
|
255 |
@{lemma "replicate 4 a = [a,a,a,a]" by (simp add:nat_number)}\\ |
|
256 |
@{lemma "[2..<5] = [2,3,4]" by (simp add:nat_number)}\\ |
|
257 |
@{lemma "listsum [1,2,3::nat] = 6" by simp} |
|
26771 | 258 |
\end{tabular}} |
259 |
\caption{Characteristic examples} |
|
260 |
\label{fig:Characteristic} |
|
261 |
\end{figure} |
|
262 |
Figure~\ref{fig:Characteristic} shows charachteristic examples |
|
263 |
that should give an intuitive understanding of the above functions. |
|
264 |
*} |
|
265 |
||
24616 | 266 |
text{* The following simple sort functions are intended for proofs, |
267 |
not for efficient implementations. *} |
|
268 |
||
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
269 |
context linorder |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
270 |
begin |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
271 |
|
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
272 |
fun sorted :: "'a list \<Rightarrow> bool" where |
24697 | 273 |
"sorted [] \<longleftrightarrow> True" | |
274 |
"sorted [x] \<longleftrightarrow> True" | |
|
25062 | 275 |
"sorted (x#y#zs) \<longleftrightarrow> x <= y \<and> sorted (y#zs)" |
24697 | 276 |
|
25559 | 277 |
primrec insort :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
24697 | 278 |
"insort x [] = [x]" | |
25062 | 279 |
"insort x (y#ys) = (if x <= y then (x#y#ys) else y#(insort x ys))" |
24697 | 280 |
|
25559 | 281 |
primrec sort :: "'a list \<Rightarrow> 'a list" where |
24697 | 282 |
"sort [] = []" | |
283 |
"sort (x#xs) = insort x (sort xs)" |
|
24616 | 284 |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
285 |
end |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
286 |
|
24616 | 287 |
|
23388 | 288 |
subsubsection {* List comprehension *} |
23192 | 289 |
|
24349 | 290 |
text{* Input syntax for Haskell-like list comprehension notation. |
291 |
Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"}, |
|
292 |
the list of all pairs of distinct elements from @{text xs} and @{text ys}. |
|
293 |
The syntax is as in Haskell, except that @{text"|"} becomes a dot |
|
294 |
(like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than |
|
295 |
\verb![e| x <- xs, ...]!. |
|
296 |
||
297 |
The qualifiers after the dot are |
|
298 |
\begin{description} |
|
299 |
\item[generators] @{text"p \<leftarrow> xs"}, |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
300 |
where @{text p} is a pattern and @{text xs} an expression of list type, or |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
301 |
\item[guards] @{text"b"}, where @{text b} is a boolean expression. |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
302 |
%\item[local bindings] @ {text"let x = e"}. |
24349 | 303 |
\end{description} |
23240 | 304 |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
305 |
Just like in Haskell, list comprehension is just a shorthand. To avoid |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
306 |
misunderstandings, the translation into desugared form is not reversed |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
307 |
upon output. Note that the translation of @{text"[e. x \<leftarrow> xs]"} is |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
308 |
optmized to @{term"map (%x. e) xs"}. |
23240 | 309 |
|
24349 | 310 |
It is easy to write short list comprehensions which stand for complex |
311 |
expressions. During proofs, they may become unreadable (and |
|
312 |
mangled). In such cases it can be advisable to introduce separate |
|
313 |
definitions for the list comprehensions in question. *} |
|
314 |
||
23209 | 315 |
(* |
23240 | 316 |
Proper theorem proving support would be nice. For example, if |
23192 | 317 |
@{text"set[f x y. x \<leftarrow> xs, y \<leftarrow> ys, P x y]"} |
318 |
produced something like |
|
23209 | 319 |
@{term"{z. EX x: set xs. EX y:set ys. P x y \<and> z = f x y}"}. |
320 |
*) |
|
321 |
||
23240 | 322 |
nonterminals lc_qual lc_quals |
23192 | 323 |
|
324 |
syntax |
|
23240 | 325 |
"_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list" ("[_ . __") |
24349 | 326 |
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ <- _") |
23240 | 327 |
"_lc_test" :: "bool \<Rightarrow> lc_qual" ("_") |
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
328 |
(*"_lc_let" :: "letbinds => lc_qual" ("let _")*) |
23240 | 329 |
"_lc_end" :: "lc_quals" ("]") |
330 |
"_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __") |
|
24349 | 331 |
"_lc_abs" :: "'a => 'b list => 'b list" |
23192 | 332 |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
333 |
(* These are easier than ML code but cannot express the optimized |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
334 |
translation of [e. p<-xs] |
23192 | 335 |
translations |
24349 | 336 |
"[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)" |
23240 | 337 |
"_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)" |
24349 | 338 |
=> "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)" |
23240 | 339 |
"[e. P]" => "if P then [e] else []" |
340 |
"_listcompr e (_lc_test P) (_lc_quals Q Qs)" |
|
341 |
=> "if P then (_listcompr e Q Qs) else []" |
|
24349 | 342 |
"_listcompr e (_lc_let b) (_lc_quals Q Qs)" |
343 |
=> "_Let b (_listcompr e Q Qs)" |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
344 |
*) |
23240 | 345 |
|
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
346 |
syntax (xsymbols) |
24349 | 347 |
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _") |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
348 |
syntax (HTML output) |
24349 | 349 |
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _") |
350 |
||
351 |
parse_translation (advanced) {* |
|
352 |
let |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
353 |
val NilC = Syntax.const @{const_name Nil}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
354 |
val ConsC = Syntax.const @{const_name Cons}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
355 |
val mapC = Syntax.const @{const_name map}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
356 |
val concatC = Syntax.const @{const_name concat}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
357 |
val IfC = Syntax.const @{const_name If}; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
358 |
fun singl x = ConsC $ x $ NilC; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
359 |
|
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
360 |
fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *) |
24349 | 361 |
let |
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
362 |
val x = Free (Name.variant (add_term_free_names (p$e, [])) "x", dummyT); |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
363 |
val e = if opti then singl e else e; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
364 |
val case1 = Syntax.const "_case1" $ p $ e; |
24349 | 365 |
val case2 = Syntax.const "_case1" $ Syntax.const Term.dummy_patternN |
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
366 |
$ NilC; |
24349 | 367 |
val cs = Syntax.const "_case2" $ case1 $ case2 |
368 |
val ft = DatatypeCase.case_tr false DatatypePackage.datatype_of_constr |
|
369 |
ctxt [x, cs] |
|
370 |
in lambda x ft end; |
|
371 |
||
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
372 |
fun abs_tr ctxt (p as Free(s,T)) e opti = |
24349 | 373 |
let val thy = ProofContext.theory_of ctxt; |
374 |
val s' = Sign.intern_const thy s |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
375 |
in if Sign.declared_const thy s' |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
376 |
then (pat_tr ctxt p e opti, false) |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
377 |
else (lambda p e, true) |
24349 | 378 |
end |
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
379 |
| abs_tr ctxt p e opti = (pat_tr ctxt p e opti, false); |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
380 |
|
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
381 |
fun lc_tr ctxt [e, Const("_lc_test",_)$b, qs] = |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
382 |
let val res = case qs of Const("_lc_end",_) => singl e |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
383 |
| Const("_lc_quals",_)$q$qs => lc_tr ctxt [e,q,qs]; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
384 |
in IfC $ b $ res $ NilC end |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
385 |
| lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_end",_)] = |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
386 |
(case abs_tr ctxt p e true of |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
387 |
(f,true) => mapC $ f $ es |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
388 |
| (f, false) => concatC $ (mapC $ f $ es)) |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
389 |
| lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_quals",_)$q$qs] = |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
390 |
let val e' = lc_tr ctxt [e,q,qs]; |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
391 |
in concatC $ (mapC $ (fst(abs_tr ctxt p e' false)) $ es) end |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
392 |
|
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
393 |
in [("_listcompr", lc_tr)] end |
24349 | 394 |
*} |
23279
e39dd93161d9
tuned list comprehension, changed filter syntax from : to <-
nipkow
parents:
23246
diff
changeset
|
395 |
|
23240 | 396 |
(* |
397 |
term "[(x,y,z). b]" |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
398 |
term "[(x,y,z). x\<leftarrow>xs]" |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
399 |
term "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]" |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
400 |
term "[(x,y,z). x<a, x>b]" |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
401 |
term "[(x,y,z). x\<leftarrow>xs, x>b]" |
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
402 |
term "[(x,y,z). x<a, x\<leftarrow>xs]" |
24349 | 403 |
term "[(x,y). Cons True x \<leftarrow> xs]" |
404 |
term "[(x,y,z). Cons x [] \<leftarrow> xs]" |
|
23240 | 405 |
term "[(x,y,z). x<a, x>b, x=d]" |
406 |
term "[(x,y,z). x<a, x>b, y\<leftarrow>ys]" |
|
407 |
term "[(x,y,z). x<a, x\<leftarrow>xs,y>b]" |
|
408 |
term "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]" |
|
409 |
term "[(x,y,z). x\<leftarrow>xs, x>b, y<a]" |
|
410 |
term "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]" |
|
411 |
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]" |
|
412 |
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]" |
|
24349 | 413 |
term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]" |
23192 | 414 |
*) |
415 |
||
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
416 |
subsubsection {* @{const Nil} and @{const Cons} *} |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
417 |
|
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
418 |
lemma not_Cons_self [simp]: |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
419 |
"xs \<noteq> x # xs" |
13145 | 420 |
by (induct xs) auto |
13114 | 421 |
|
13142 | 422 |
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric] |
13114 | 423 |
|
13142 | 424 |
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)" |
13145 | 425 |
by (induct xs) auto |
13114 | 426 |
|
13142 | 427 |
lemma length_induct: |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
428 |
"(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs" |
17589 | 429 |
by (rule measure_induct [of length]) iprover |
13114 | 430 |
|
431 |
||
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
432 |
subsubsection {* @{const length} *} |
13114 | 433 |
|
13142 | 434 |
text {* |
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
435 |
Needs to come before @{text "@"} because of theorem @{text |
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
436 |
append_eq_append_conv}. |
13142 | 437 |
*} |
13114 | 438 |
|
13142 | 439 |
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys" |
13145 | 440 |
by (induct xs) auto |
13114 | 441 |
|
13142 | 442 |
lemma length_map [simp]: "length (map f xs) = length xs" |
13145 | 443 |
by (induct xs) auto |
13114 | 444 |
|
13142 | 445 |
lemma length_rev [simp]: "length (rev xs) = length xs" |
13145 | 446 |
by (induct xs) auto |
13114 | 447 |
|
13142 | 448 |
lemma length_tl [simp]: "length (tl xs) = length xs - 1" |
13145 | 449 |
by (cases xs) auto |
13114 | 450 |
|
13142 | 451 |
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])" |
13145 | 452 |
by (induct xs) auto |
13114 | 453 |
|
13142 | 454 |
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])" |
13145 | 455 |
by (induct xs) auto |
13114 | 456 |
|
23479 | 457 |
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0" |
458 |
by auto |
|
459 |
||
13114 | 460 |
lemma length_Suc_conv: |
13145 | 461 |
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
462 |
by (induct xs) auto |
|
13142 | 463 |
|
14025 | 464 |
lemma Suc_length_conv: |
465 |
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)" |
|
14208 | 466 |
apply (induct xs, simp, simp) |
14025 | 467 |
apply blast |
468 |
done |
|
469 |
||
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
470 |
lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False" |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
471 |
by (induct xs) auto |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
472 |
|
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
473 |
lemma list_induct2 [consumes 1, case_names Nil Cons]: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
474 |
"length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow> |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
475 |
(\<And>x xs y ys. length xs = length ys \<Longrightarrow> P xs ys \<Longrightarrow> P (x#xs) (y#ys)) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
476 |
\<Longrightarrow> P xs ys" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
477 |
proof (induct xs arbitrary: ys) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
478 |
case Nil then show ?case by simp |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
479 |
next |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
480 |
case (Cons x xs ys) then show ?case by (cases ys) simp_all |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
481 |
qed |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
482 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
483 |
lemma list_induct3 [consumes 2, case_names Nil Cons]: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
484 |
"length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow> |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
485 |
(\<And>x xs y ys z zs. length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P xs ys zs \<Longrightarrow> P (x#xs) (y#ys) (z#zs)) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
486 |
\<Longrightarrow> P xs ys zs" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
487 |
proof (induct xs arbitrary: ys zs) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
488 |
case Nil then show ?case by simp |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
489 |
next |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
490 |
case (Cons x xs ys zs) then show ?case by (cases ys, simp_all) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
491 |
(cases zs, simp_all) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
492 |
qed |
13114 | 493 |
|
22493
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
494 |
lemma list_induct2': |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
495 |
"\<lbrakk> P [] []; |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
496 |
\<And>x xs. P (x#xs) []; |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
497 |
\<And>y ys. P [] (y#ys); |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
498 |
\<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
|
499 |
\<Longrightarrow> P xs ys" |
db930e490fe5
added another rule for simultaneous induction, and lemmas for zip
krauss
parents:
22422
diff
changeset
|
500 |
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
|
501 |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
502 |
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False" |
24349 | 503 |
by (rule Eq_FalseI) auto |
24037 | 504 |
|
505 |
simproc_setup list_neq ("(xs::'a list) = ys") = {* |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
506 |
(* |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
507 |
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
|
508 |
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
|
509 |
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
|
510 |
*) |
24037 | 511 |
|
512 |
let |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
513 |
|
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
514 |
fun len (Const("List.list.Nil",_)) acc = acc |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
515 |
| len (Const("List.list.Cons",_) $ _ $ xs) (ts,n) = len xs (ts,n+1) |
23029 | 516 |
| len (Const("List.append",_) $ xs $ ys) acc = len xs (len ys acc) |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
517 |
| len (Const("List.rev",_) $ xs) acc = len xs acc |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
518 |
| len (Const("List.map",_) $ _ $ xs) acc = len xs acc |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
519 |
| len t (ts,n) = (t::ts,n); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
520 |
|
24037 | 521 |
fun list_neq _ ss ct = |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
522 |
let |
24037 | 523 |
val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct; |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
524 |
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
|
525 |
fun prove_neq() = |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
526 |
let |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
527 |
val Type(_,listT::_) = eqT; |
22994 | 528 |
val size = HOLogic.size_const listT; |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
529 |
val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
530 |
val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len); |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
531 |
val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len |
22633 | 532 |
(K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1)); |
533 |
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
|
534 |
in |
23214 | 535 |
if m < n andalso submultiset (op aconv) (ls,rs) orelse |
536 |
n < m andalso submultiset (op aconv) (rs,ls) |
|
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
537 |
then prove_neq() else NONE |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
538 |
end; |
24037 | 539 |
in list_neq end; |
22143
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
540 |
*} |
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
541 |
|
cf58486ca11b
Added simproc list_neq (prompted by an application)
nipkow
parents:
21911
diff
changeset
|
542 |
|
15392 | 543 |
subsubsection {* @{text "@"} -- append *} |
13114 | 544 |
|
13142 | 545 |
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)" |
13145 | 546 |
by (induct xs) auto |
13114 | 547 |
|
13142 | 548 |
lemma append_Nil2 [simp]: "xs @ [] = xs" |
13145 | 549 |
by (induct xs) auto |
3507 | 550 |
|
24449 | 551 |
interpretation semigroup_append: semigroup_add ["op @"] |
552 |
by unfold_locales simp |
|
553 |
interpretation monoid_append: monoid_add ["[]" "op @"] |
|
554 |
by unfold_locales (simp+) |
|
555 |
||
13142 | 556 |
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])" |
13145 | 557 |
by (induct xs) auto |
13114 | 558 |
|
13142 | 559 |
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])" |
13145 | 560 |
by (induct xs) auto |
13114 | 561 |
|
13142 | 562 |
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])" |
13145 | 563 |
by (induct xs) auto |
13114 | 564 |
|
13142 | 565 |
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])" |
13145 | 566 |
by (induct xs) auto |
13114 | 567 |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
568 |
lemma append_eq_append_conv [simp, noatp]: |
24526 | 569 |
"length xs = length ys \<or> length us = length vs |
13883
0451e0fb3f22
Re-structured some proofs in order to get rid of rule_format attribute.
berghofe
parents:
13863
diff
changeset
|
570 |
==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)" |
24526 | 571 |
apply (induct xs arbitrary: ys) |
14208 | 572 |
apply (case_tac ys, simp, force) |
573 |
apply (case_tac ys, force, simp) |
|
13145 | 574 |
done |
13142 | 575 |
|
24526 | 576 |
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) = |
577 |
(EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)" |
|
578 |
apply (induct xs arbitrary: ys zs ts) |
|
14495 | 579 |
apply fastsimp |
580 |
apply(case_tac zs) |
|
581 |
apply simp |
|
582 |
apply fastsimp |
|
583 |
done |
|
584 |
||
13142 | 585 |
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)" |
13145 | 586 |
by simp |
13142 | 587 |
|
588 |
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)" |
|
13145 | 589 |
by simp |
13114 | 590 |
|
13142 | 591 |
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)" |
13145 | 592 |
by simp |
13114 | 593 |
|
13142 | 594 |
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])" |
13145 | 595 |
using append_same_eq [of _ _ "[]"] by auto |
3507 | 596 |
|
13142 | 597 |
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])" |
13145 | 598 |
using append_same_eq [of "[]"] by auto |
13114 | 599 |
|
24286
7619080e49f0
ATP blacklisting is now in theory data, attribute noatp
paulson
parents:
24219
diff
changeset
|
600 |
lemma hd_Cons_tl [simp,noatp]: "xs \<noteq> [] ==> hd xs # tl xs = xs" |
13145 | 601 |
by (induct xs) auto |
13114 | 602 |
|
13142 | 603 |
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)" |
13145 | 604 |
by (induct xs) auto |
13114 | 605 |
|
13142 | 606 |
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs" |
13145 | 607 |
by (simp add: hd_append split: list.split) |
13114 | 608 |
|
13142 | 609 |
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)" |
13145 | 610 |
by (simp split: list.split) |
13114 | 611 |
|
13142 | 612 |
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys" |
13145 | 613 |
by (simp add: tl_append split: list.split) |
13114 | 614 |
|
615 |
||
14300 | 616 |
lemma Cons_eq_append_conv: "x#xs = ys@zs = |
617 |
(ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))" |
|
618 |
by(cases ys) auto |
|
619 |
||
15281 | 620 |
lemma append_eq_Cons_conv: "(ys@zs = x#xs) = |
621 |
(ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))" |
|
622 |
by(cases ys) auto |
|
623 |
||
14300 | 624 |
|
13142 | 625 |
text {* Trivial rules for solving @{text "@"}-equations automatically. *} |
13114 | 626 |
|
627 |
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys" |
|
13145 | 628 |
by simp |
13114 | 629 |
|
13142 | 630 |
lemma Cons_eq_appendI: |
13145 | 631 |
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs" |
632 |
by (drule sym) simp |
|
13114 | 633 |
|
13142 | 634 |
lemma append_eq_appendI: |
13145 | 635 |
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us" |
636 |
by (drule sym) simp |
|
13114 | 637 |
|
638 |
||
13142 | 639 |
text {* |
13145 | 640 |
Simplification procedure for all list equalities. |
641 |
Currently only tries to rearrange @{text "@"} to see if |
|
642 |
- both lists end in a singleton list, |
|
643 |
- or both lists end in the same list. |
|
13142 | 644 |
*} |
645 |
||
26480 | 646 |
ML {* |
3507 | 647 |
local |
648 |
||
13114 | 649 |
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) = |
13462 | 650 |
(case xs of Const("List.list.Nil",_) => cons | _ => last xs) |
23029 | 651 |
| last (Const("List.append",_) $ _ $ ys) = last ys |
13462 | 652 |
| last t = t; |
13114 | 653 |
|
654 |
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true |
|
13462 | 655 |
| list1 _ = false; |
13114 | 656 |
|
657 |
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) = |
|
13462 | 658 |
(case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs) |
23029 | 659 |
| butlast ((app as Const("List.append",_) $ xs) $ ys) = app $ butlast ys |
13462 | 660 |
| butlast xs = Const("List.list.Nil",fastype_of xs); |
13114 | 661 |
|
22633 | 662 |
val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc}, |
663 |
@{thm append_Nil}, @{thm append_Cons}]; |
|
16973 | 664 |
|
20044
92cc2f4c7335
simprocs: no theory argument -- use simpset context instead;
wenzelm
parents:
19890
diff
changeset
|
665 |
fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) = |
13462 | 666 |
let |
667 |
val lastl = last lhs and lastr = last rhs; |
|
668 |
fun rearr conv = |
|
669 |
let |
|
670 |
val lhs1 = butlast lhs and rhs1 = butlast rhs; |
|
671 |
val Type(_,listT::_) = eqT |
|
672 |
val appT = [listT,listT] ---> listT |
|
23029 | 673 |
val app = Const("List.append",appT) |
13462 | 674 |
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
|
675 |
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
|
676 |
val thm = Goal.prove (Simplifier.the_context ss) [] [] eq |
17877
67d5ab1cb0d8
Simplifier.inherit_context instead of Simplifier.inherit_bounds;
wenzelm
parents:
17830
diff
changeset
|
677 |
(K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1)); |
15531 | 678 |
in SOME ((conv RS (thm RS trans)) RS eq_reflection) end; |
13114 | 679 |
|
13462 | 680 |
in |
22633 | 681 |
if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv} |
682 |
else if lastl aconv lastr then rearr @{thm append_same_eq} |
|
15531 | 683 |
else NONE |
13462 | 684 |
end; |
685 |
||
13114 | 686 |
in |
13462 | 687 |
|
688 |
val list_eq_simproc = |
|
22633 | 689 |
Simplifier.simproc @{theory} "list_eq" ["(xs::'a list) = ys"] (K list_eq); |
13462 | 690 |
|
13114 | 691 |
end; |
692 |
||
693 |
Addsimprocs [list_eq_simproc]; |
|
694 |
*} |
|
695 |
||
696 |
||
15392 | 697 |
subsubsection {* @{text map} *} |
13114 | 698 |
|
13142 | 699 |
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs" |
13145 | 700 |
by (induct xs) simp_all |
13114 | 701 |
|
13142 | 702 |
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)" |
13145 | 703 |
by (rule ext, induct_tac xs) auto |
13114 | 704 |
|
13142 | 705 |
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys" |
13145 | 706 |
by (induct xs) auto |
13114 | 707 |
|
13142 | 708 |
lemma map_compose: "map (f o g) xs = map f (map g xs)" |
13145 | 709 |
by (induct xs) (auto simp add: o_def) |
13114 | 710 |
|
13142 | 711 |
lemma rev_map: "rev (map f xs) = map f (rev xs)" |
13145 | 712 |
by (induct xs) auto |
13114 | 713 |
|
13737 | 714 |
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)" |
715 |
by (induct xs) auto |
|
716 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset
|
717 |
lemma map_cong [fundef_cong, recdef_cong]: |
13145 | 718 |
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys" |
719 |
-- {* a congruence rule for @{text map} *} |
|
13737 | 720 |
by simp |
13114 | 721 |
|
13142 | 722 |
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])" |
13145 | 723 |
by (cases xs) auto |
13114 | 724 |
|
13142 | 725 |
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])" |
13145 | 726 |
by (cases xs) auto |
13114 | 727 |
|
18447 | 728 |
lemma map_eq_Cons_conv: |
14025 | 729 |
"(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)" |
13145 | 730 |
by (cases xs) auto |
13114 | 731 |
|
18447 | 732 |
lemma Cons_eq_map_conv: |
14025 | 733 |
"(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)" |
734 |
by (cases ys) auto |
|
735 |
||
18447 | 736 |
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1] |
737 |
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1] |
|
738 |
declare map_eq_Cons_D [dest!] Cons_eq_map_D [dest!] |
|
739 |
||
14111 | 740 |
lemma ex_map_conv: |
741 |
"(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)" |
|
18447 | 742 |
by(induct ys, auto simp add: Cons_eq_map_conv) |
14111 | 743 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
744 |
lemma map_eq_imp_length_eq: |
26734 | 745 |
assumes "map f xs = map f ys" |
746 |
shows "length xs = length ys" |
|
747 |
using assms proof (induct ys arbitrary: xs) |
|
748 |
case Nil then show ?case by simp |
|
749 |
next |
|
750 |
case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto |
|
751 |
from Cons xs have "map f zs = map f ys" by simp |
|
752 |
moreover with Cons have "length zs = length ys" by blast |
|
753 |
with xs show ?case by simp |
|
754 |
qed |
|
755 |
||
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
756 |
lemma map_inj_on: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
757 |
"[| 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
|
758 |
==> xs = ys" |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
759 |
apply(frule map_eq_imp_length_eq) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
760 |
apply(rotate_tac -1) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
761 |
apply(induct rule:list_induct2) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
762 |
apply simp |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
763 |
apply(simp) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
764 |
apply (blast intro:sym) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
765 |
done |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
766 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
767 |
lemma inj_on_map_eq_map: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
768 |
"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
|
769 |
by(blast dest:map_inj_on) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
770 |
|
13114 | 771 |
lemma map_injective: |
24526 | 772 |
"map f xs = map f ys ==> inj f ==> xs = ys" |
773 |
by (induct ys arbitrary: xs) (auto dest!:injD) |
|
13114 | 774 |
|
14339 | 775 |
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)" |
776 |
by(blast dest:map_injective) |
|
777 |
||
13114 | 778 |
lemma inj_mapI: "inj f ==> inj (map f)" |
17589 | 779 |
by (iprover dest: map_injective injD intro: inj_onI) |
13114 | 780 |
|
781 |
lemma inj_mapD: "inj (map f) ==> inj f" |
|
14208 | 782 |
apply (unfold inj_on_def, clarify) |
13145 | 783 |
apply (erule_tac x = "[x]" in ballE) |
14208 | 784 |
apply (erule_tac x = "[y]" in ballE, simp, blast) |
13145 | 785 |
apply blast |
786 |
done |
|
13114 | 787 |
|
14339 | 788 |
lemma inj_map[iff]: "inj (map f) = inj f" |
13145 | 789 |
by (blast dest: inj_mapD intro: inj_mapI) |
13114 | 790 |
|
15303 | 791 |
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A" |
792 |
apply(rule inj_onI) |
|
793 |
apply(erule map_inj_on) |
|
794 |
apply(blast intro:inj_onI dest:inj_onD) |
|
795 |
done |
|
796 |
||
14343 | 797 |
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs" |
798 |
by (induct xs, auto) |
|
13114 | 799 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
800 |
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
|
801 |
by (induct xs) auto |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
802 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
803 |
lemma map_fst_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
804 |
"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
|
805 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
806 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
807 |
lemma map_snd_zip[simp]: |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
808 |
"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
|
809 |
by (induct rule:list_induct2, simp_all) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
810 |
|
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
811 |
|
15392 | 812 |
subsubsection {* @{text rev} *} |
13114 | 813 |
|
13142 | 814 |
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs" |
13145 | 815 |
by (induct xs) auto |
13114 | 816 |
|
13142 | 817 |
lemma rev_rev_ident [simp]: "rev (rev xs) = xs" |
13145 | 818 |
by (induct xs) auto |
13114 | 819 |
|
15870 | 820 |
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)" |
821 |
by auto |
|
822 |
||
13142 | 823 |
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])" |
13145 | 824 |
by (induct xs) auto |
13114 | 825 |
|
13142 | 826 |
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])" |
13145 | 827 |
by (induct xs) auto |
13114 | 828 |
|
15870 | 829 |
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])" |
830 |
by (cases xs) auto |
|
831 |
||
832 |
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])" |
|
833 |
by (cases xs) auto |
|
834 |
||
21061
580dfc999ef6
added normal post setup; cleaned up "execution" constants
haftmann
parents:
21046
diff
changeset
|
835 |
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
|
836 |
apply (induct xs arbitrary: ys, force) |
14208 | 837 |
apply (case_tac ys, simp, force) |
13145 | 838 |
done |
13114 | 839 |
|
15439 | 840 |
lemma inj_on_rev[iff]: "inj_on rev A" |
841 |
by(simp add:inj_on_def) |
|
842 |
||
13366 | 843 |
lemma rev_induct [case_names Nil snoc]: |
844 |
"[| 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
|
845 |
apply(simplesubst rev_rev_ident[symmetric]) |
13145 | 846 |
apply(rule_tac list = "rev xs" in list.induct, simp_all) |
847 |
done |
|
13114 | 848 |
|
13366 | 849 |
lemma rev_exhaust [case_names Nil snoc]: |
850 |
"(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P" |
|
13145 | 851 |
by (induct xs rule: rev_induct) auto |
13114 | 852 |
|
13366 | 853 |
lemmas rev_cases = rev_exhaust |
854 |
||
18423 | 855 |
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])" |
856 |
by(rule rev_cases[of xs]) auto |
|
857 |
||
13114 | 858 |
|
15392 | 859 |
subsubsection {* @{text set} *} |
13114 | 860 |
|
13142 | 861 |
lemma finite_set [iff]: "finite (set xs)" |
13145 | 862 |
by (induct xs) auto |
13114 | 863 |
|
13142 | 864 |
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)" |
13145 | 865 |
by (induct xs) auto |
13114 | 866 |
|
17830 | 867 |
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs" |
868 |
by(cases xs) auto |
|
14099 | 869 |
|
13142 | 870 |
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)" |
13145 | 871 |
by auto |
13114 | 872 |
|
14099 | 873 |
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" |
874 |
by auto |
|
875 |
||
13142 | 876 |
lemma set_empty [iff]: "(set xs = {}) = (xs = [])" |
13145 | 877 |
by (induct xs) auto |
13114 | 878 |
|
15245 | 879 |
lemma set_empty2[iff]: "({} = set xs) = (xs = [])" |
880 |
by(induct xs) auto |
|
881 |
||
13142 | 882 |
lemma set_rev [simp]: "set (rev xs) = set xs" |
13145 | 883 |
by (induct xs) auto |
13114 | 884 |
|
13142 | 885 |
lemma set_map [simp]: "set (map f xs) = f`(set xs)" |
13145 | 886 |
by (induct xs) auto |
13114 | 887 |
|
13142 | 888 |
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}" |
13145 | 889 |
by (induct xs) auto |
13114 | 890 |
|
15425 | 891 |
lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}" |
14208 | 892 |
apply (induct j, simp_all) |
893 |
apply (erule ssubst, auto) |
|
13145 | 894 |
done |
13114 | 895 |
|
13142 | 896 |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
897 |
lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs" |
18049 | 898 |
proof (induct xs) |
26073 | 899 |
case Nil thus ?case by simp |
900 |
next |
|
901 |
case Cons thus ?case by (auto intro: Cons_eq_appendI) |
|
902 |
qed |
|
903 |
||
26734 | 904 |
lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)" |
905 |
by (auto elim: split_list) |
|
26073 | 906 |
|
907 |
lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys" |
|
908 |
proof (induct xs) |
|
909 |
case Nil thus ?case by simp |
|
18049 | 910 |
next |
911 |
case (Cons a xs) |
|
912 |
show ?case |
|
913 |
proof cases |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
914 |
assume "x = a" thus ?case using Cons by fastsimp |
18049 | 915 |
next |
26073 | 916 |
assume "x \<noteq> a" thus ?case using Cons by(fastsimp intro!: Cons_eq_appendI) |
917 |
qed |
|
918 |
qed |
|
919 |
||
920 |
lemma in_set_conv_decomp_first: |
|
921 |
"(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)" |
|
26734 | 922 |
by (auto dest!: split_list_first) |
26073 | 923 |
|
924 |
lemma split_list_last: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs" |
|
925 |
proof (induct xs rule:rev_induct) |
|
926 |
case Nil thus ?case by simp |
|
927 |
next |
|
928 |
case (snoc a xs) |
|
929 |
show ?case |
|
930 |
proof cases |
|
931 |
assume "x = a" thus ?case using snoc by simp (metis ex_in_conv set_empty2) |
|
932 |
next |
|
933 |
assume "x \<noteq> a" thus ?case using snoc by fastsimp |
|
18049 | 934 |
qed |
935 |
qed |
|
936 |
||
26073 | 937 |
lemma in_set_conv_decomp_last: |
938 |
"(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)" |
|
26734 | 939 |
by (auto dest!: split_list_last) |
26073 | 940 |
|
941 |
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x" |
|
942 |
proof (induct xs) |
|
943 |
case Nil thus ?case by simp |
|
944 |
next |
|
945 |
case Cons thus ?case |
|
946 |
by(simp add:Bex_def)(metis append_Cons append.simps(1)) |
|
947 |
qed |
|
948 |
||
949 |
lemma split_list_propE: |
|
26734 | 950 |
assumes "\<exists>x \<in> set xs. P x" |
951 |
obtains ys x zs where "xs = ys @ x # zs" and "P x" |
|
952 |
using split_list_prop [OF assms] by blast |
|
26073 | 953 |
|
954 |
lemma split_list_first_prop: |
|
955 |
"\<exists>x \<in> set xs. P x \<Longrightarrow> |
|
956 |
\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)" |
|
26734 | 957 |
proof (induct xs) |
26073 | 958 |
case Nil thus ?case by simp |
959 |
next |
|
960 |
case (Cons x xs) |
|
961 |
show ?case |
|
962 |
proof cases |
|
963 |
assume "P x" |
|
26734 | 964 |
thus ?thesis by simp |
965 |
(metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append) |
|
26073 | 966 |
next |
967 |
assume "\<not> P x" |
|
968 |
hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp |
|
969 |
thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD) |
|
970 |
qed |
|
971 |
qed |
|
972 |
||
973 |
lemma split_list_first_propE: |
|
26734 | 974 |
assumes "\<exists>x \<in> set xs. P x" |
975 |
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y" |
|
976 |
using split_list_first_prop [OF assms] by blast |
|
26073 | 977 |
|
978 |
lemma split_list_first_prop_iff: |
|
979 |
"(\<exists>x \<in> set xs. P x) \<longleftrightarrow> |
|
980 |
(\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))" |
|
26734 | 981 |
by (rule, erule split_list_first_prop) auto |
26073 | 982 |
|
983 |
lemma split_list_last_prop: |
|
984 |
"\<exists>x \<in> set xs. P x \<Longrightarrow> |
|
985 |
\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)" |
|
986 |
proof(induct xs rule:rev_induct) |
|
987 |
case Nil thus ?case by simp |
|
988 |
next |
|
989 |
case (snoc x xs) |
|
990 |
show ?case |
|
991 |
proof cases |
|
992 |
assume "P x" thus ?thesis by (metis emptyE set_empty) |
|
993 |
next |
|
994 |
assume "\<not> P x" |
|
995 |
hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp |
|
996 |
thus ?thesis using `\<not> P x` snoc(1) by fastsimp |
|
997 |
qed |
|
998 |
qed |
|
999 |
||
1000 |
lemma split_list_last_propE: |
|
26734 | 1001 |
assumes "\<exists>x \<in> set xs. P x" |
1002 |
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z" |
|
1003 |
using split_list_last_prop [OF assms] by blast |
|
26073 | 1004 |
|
1005 |
lemma split_list_last_prop_iff: |
|
1006 |
"(\<exists>x \<in> set xs. P x) \<longleftrightarrow> |
|
1007 |
(\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))" |
|
26734 | 1008 |
by (metis split_list_last_prop [where P=P] in_set_conv_decomp) |
26073 | 1009 |
|
1010 |
lemma finite_list: "finite A ==> EX xs. set xs = A" |
|
26734 | 1011 |
by (erule finite_induct) |
1012 |
(auto simp add: set.simps(2) [symmetric] simp del: set.simps(2)) |
|
13508 | 1013 |
|
14388 | 1014 |
lemma card_length: "card (set xs) \<le> length xs" |
1015 |
by (induct xs) (auto simp add: card_insert_if) |
|
13114 | 1016 |
|
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1017 |
lemma set_minus_filter_out: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1018 |
"set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1019 |
by (induct xs) auto |
15168 | 1020 |
|
15392 | 1021 |
subsubsection {* @{text filter} *} |
13114 | 1022 |
|
13142 | 1023 |
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys" |
13145 | 1024 |
by (induct xs) auto |
13114 | 1025 |
|
15305 | 1026 |
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)" |
1027 |
by (induct xs) simp_all |
|
1028 |
||
13142 | 1029 |
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs" |
13145 | 1030 |
by (induct xs) auto |
13114 | 1031 |
|
16998 | 1032 |
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs" |
1033 |
by (induct xs) (auto simp add: le_SucI) |
|
1034 |
||
18423 | 1035 |
lemma sum_length_filter_compl: |
1036 |
"length(filter P xs) + length(filter (%x. ~P x) xs) = length xs" |
|
1037 |
by(induct xs) simp_all |
|
1038 |
||
13142 | 1039 |
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs" |
13145 | 1040 |
by (induct xs) auto |
13114 | 1041 |
|
13142 | 1042 |
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []" |
13145 | 1043 |
by (induct xs) auto |
13114 | 1044 |
|
16998 | 1045 |
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" |
24349 | 1046 |
by (induct xs) simp_all |
16998 | 1047 |
|
1048 |
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)" |
|
1049 |
apply (induct xs) |
|
1050 |
apply auto |
|
1051 |
apply(cut_tac P=P and xs=xs in length_filter_le) |
|
1052 |
apply simp |
|
1053 |
done |
|
13114 | 1054 |
|
16965 | 1055 |
lemma filter_map: |
1056 |
"filter P (map f xs) = map f (filter (P o f) xs)" |
|
1057 |
by (induct xs) simp_all |
|
1058 |
||
1059 |
lemma length_filter_map[simp]: |
|
1060 |
"length (filter P (map f xs)) = length(filter (P o f) xs)" |
|
1061 |
by (simp add:filter_map) |
|
1062 |
||
13142 | 1063 |
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs" |
13145 | 1064 |
by auto |
13114 | 1065 |
|
15246 | 1066 |
lemma length_filter_less: |
1067 |
"\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs" |
|
1068 |
proof (induct xs) |
|
1069 |
case Nil thus ?case by simp |
|
1070 |
next |
|
1071 |
case (Cons x xs) thus ?case |
|
1072 |
apply (auto split:split_if_asm) |
|
1073 |
using length_filter_le[of P xs] apply arith |
|
1074 |
done |
|
1075 |
qed |
|
13114 | 1076 |
|
15281 | 1077 |
lemma length_filter_conv_card: |
1078 |
"length(filter p xs) = card{i. i < length xs & p(xs!i)}" |
|
1079 |
proof (induct xs) |
|
1080 |
case Nil thus ?case by simp |
|
1081 |
next |
|
1082 |
case (Cons x xs) |
|
1083 |
let ?S = "{i. i < length xs & p(xs!i)}" |
|
1084 |
have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite) |
|
1085 |
show ?case (is "?l = card ?S'") |
|
1086 |
proof (cases) |
|
1087 |
assume "p x" |
|
1088 |
hence eq: "?S' = insert 0 (Suc ` ?S)" |
|
25162 | 1089 |
by(auto simp: image_def split:nat.split dest:gr0_implies_Suc) |
15281 | 1090 |
have "length (filter p (x # xs)) = Suc(card ?S)" |
23388 | 1091 |
using Cons `p x` by simp |
15281 | 1092 |
also have "\<dots> = Suc(card(Suc ` ?S))" using fin |
1093 |
by (simp add: card_image inj_Suc) |
|
1094 |
also have "\<dots> = card ?S'" using eq fin |
|
1095 |
by (simp add:card_insert_if) (simp add:image_def) |
|
1096 |
finally show ?thesis . |
|
1097 |
next |
|
1098 |
assume "\<not> p x" |
|
1099 |
hence eq: "?S' = Suc ` ?S" |
|
25162 | 1100 |
by(auto simp add: image_def split:nat.split elim:lessE) |
15281 | 1101 |
have "length (filter p (x # xs)) = card ?S" |
23388 | 1102 |
using Cons `\<not> p x` by simp |
15281 | 1103 |
also have "\<dots> = card(Suc ` ?S)" using fin |
1104 |
by (simp add: card_image inj_Suc) |
|
1105 |
also have "\<dots> = card ?S'" using eq fin |
|
1106 |
by (simp add:card_insert_if) |
|
1107 |
finally show ?thesis . |
|
1108 |
qed |
|
1109 |
qed |
|
1110 |
||
17629 | 1111 |
lemma Cons_eq_filterD: |
1112 |
"x#xs = filter P ys \<Longrightarrow> |
|
1113 |
\<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 | 1114 |
(is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs") |
17629 | 1115 |
proof(induct ys) |
1116 |
case Nil thus ?case by simp |
|
1117 |
next |
|
1118 |
case (Cons y ys) |
|
1119 |
show ?case (is "\<exists>x. ?Q x") |
|
1120 |
proof cases |
|
1121 |
assume Py: "P y" |
|
1122 |
show ?thesis |
|
1123 |
proof cases |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1124 |
assume "x = y" |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1125 |
with Py Cons.prems have "?Q []" by simp |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1126 |
then show ?thesis .. |
17629 | 1127 |
next |
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1128 |
assume "x \<noteq> y" |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1129 |
with Py Cons.prems show ?thesis by simp |
17629 | 1130 |
qed |
1131 |
next |
|
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1132 |
assume "\<not> P y" |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1133 |
with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastsimp |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1134 |
then have "?Q (y#us)" by simp |
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1135 |
then show ?thesis .. |
17629 | 1136 |
qed |
1137 |
qed |
|
1138 |
||
1139 |
lemma filter_eq_ConsD: |
|
1140 |
"filter P ys = x#xs \<Longrightarrow> |
|
1141 |
\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs" |
|
1142 |
by(rule Cons_eq_filterD) simp |
|
1143 |
||
1144 |
lemma filter_eq_Cons_iff: |
|
1145 |
"(filter P ys = x#xs) = |
|
1146 |
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" |
|
1147 |
by(auto dest:filter_eq_ConsD) |
|
1148 |
||
1149 |
lemma Cons_eq_filter_iff: |
|
1150 |
"(x#xs = filter P ys) = |
|
1151 |
(\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)" |
|
1152 |
by(auto dest:Cons_eq_filterD) |
|
1153 |
||
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19623
diff
changeset
|
1154 |
lemma filter_cong[fundef_cong, recdef_cong]: |
17501 | 1155 |
"xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys" |
1156 |
apply simp |
|
1157 |
apply(erule thin_rl) |
|
1158 |
by (induct ys) simp_all |
|
1159 |
||
15281 | 1160 |
|
26442
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1161 |
subsubsection {* List partitioning *} |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1162 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1163 |
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1164 |
"partition P [] = ([], [])" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1165 |
| "partition P (x # xs) = |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1166 |
(let (yes, no) = partition P xs |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1167 |
in if P x then (x # yes, no) else (yes, x # no))" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1168 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1169 |
lemma partition_filter1: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1170 |
"fst (partition P xs) = filter P xs" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1171 |
by (induct xs) (auto simp add: Let_def split_def) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1172 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1173 |
lemma partition_filter2: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1174 |
"snd (partition P xs) = filter (Not o P) xs" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1175 |
by (induct xs) (auto simp add: Let_def split_def) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1176 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1177 |
lemma partition_P: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1178 |
assumes "partition P xs = (yes, no)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1179 |
shows "(\<forall>p \<in> set yes. P p) \<and> (\<forall>p \<in> set no. \<not> P p)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1180 |
proof - |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1181 |
from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1182 |
by simp_all |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1183 |
then show ?thesis by (simp_all add: partition_filter1 partition_filter2) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1184 |
qed |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1185 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1186 |
lemma partition_set: |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1187 |
assumes "partition P xs = (yes, no)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1188 |
shows "set yes \<union> set no = set xs" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1189 |
proof - |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1190 |
from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)" |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1191 |
by simp_all |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1192 |
then show ?thesis by (auto simp add: partition_filter1 partition_filter2) |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1193 |
qed |
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1194 |
|
57fb6a8b099e
restructuring; explicit case names for rule list_induct2
haftmann
parents:
26300
diff
changeset
|
1195 |
|
15392 | 1196 |
subsubsection {* @{text concat} *} |
13114 | 1197 |
|
13142 | 1198 |
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys" |
13145 | 1199 |
by (induct xs) auto |
13114 | 1200 |
|
18447 | 1201 |
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 1202 |
by (induct xss) auto |
13114 | 1203 |
|
18447 | 1204 |
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])" |
13145 | 1205 |
by (induct xss) auto |
13114 | 1206 |
|
24308 | 1207 |
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)" |
13145 | 1208 |
by (induct xs) auto |
13114 | 1209 |
|
24476
f7ad9fbbeeaa
turned list comprehension translations into ML to optimize base case
nipkow
parents:
24471
diff
changeset
|
1210 |
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs" |
24349 | 1211 |
by (induct xs) auto |
1212 |
||
13142 | 1213 |
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" |
13145 | 1214 |
by (induct xs) auto |
13114 | 1215 |
|
13142 | 1216 |
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" |
13145 | 1217 |
by (induct xs) auto |
13114 | 1218 |
|
13142 | 1219 |
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))" |
13145 | 1220 |
by (induct xs) auto |
13114 | 1221 |
|
1222 |
||
15392 | 1223 |
subsubsection {* @{text nth} *} |
13114 | 1224 |
|
13142 | 1225 |
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x" |
13145 | 1226 |
by auto |
13114 | 1227 |
|
13142 | 1228 |
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n" |
13145 | 1229 |
by auto |
13114 | 1230 |
|
13142 | 1231 |
declare nth.simps [simp del] |
13114 | 1232 |
|
1233 |
lemma nth_append: |
|
24526 | 1234 |
"(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))" |
1235 |
apply (induct xs arbitrary: n, simp) |
|
14208 | 1236 |
apply (case_tac n, auto) |
13145 | 1237 |
done |
13114 | 1238 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1239 |
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x" |
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1240 |
by (induct xs) auto |
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1241 |
|
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1242 |
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n" |
25221
5ded95dda5df
append/member: more light-weight way to declare authentic syntax;
wenzelm
parents:
25215
diff
changeset
|
1243 |
by (induct xs) auto |
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1244 |
|
24526 | 1245 |
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)" |
1246 |
apply (induct xs arbitrary: n, simp) |
|
14208 | 1247 |
apply (case_tac n, auto) |
13145 | 1248 |
done |
13114 | 1249 |
|
18423 | 1250 |
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0" |
1251 |
by(cases xs) simp_all |
|
1252 |
||
18049 | 1253 |
|
1254 |
lemma list_eq_iff_nth_eq: |
|
24526 | 1255 |
"(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))" |
1256 |
apply(induct xs arbitrary: ys) |
|
24632 | 1257 |
apply force |
18049 | 1258 |
apply(case_tac ys) |
1259 |
apply simp |
|
1260 |
apply(simp add:nth_Cons split:nat.split)apply blast |
|
1261 |
done |
|
1262 |
||
13142 | 1263 |
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}" |
15251 | 1264 |
apply (induct xs, simp, simp) |
13145 | 1265 |
apply safe |
24632 | 1266 |
apply (metis nat_case_0 nth.simps zero_less_Suc) |
1267 |
apply (metis less_Suc_eq_0_disj nth_Cons_Suc) |
|
14208 | 1268 |
apply (case_tac i, simp) |
24632 | 1269 |
apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff) |
13145 | 1270 |
done |
13114 | 1271 |
|
17501 | 1272 |
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)" |
1273 |
by(auto simp:set_conv_nth) |
|
1274 |
||
13145 | 1275 |
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)" |
1276 |
by (auto simp add: set_conv_nth) |
|
13114 | 1277 |
|
13142 | 1278 |
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs" |
13145 | 1279 |
by (auto simp add: set_conv_nth) |
13114 | 1280 |
|
1281 |
lemma all_nth_imp_all_set: |
|
13145 | 1282 |
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x" |
1283 |
by (auto simp add: set_conv_nth) |
|
13114 | 1284 |
|
1285 |
lemma all_set_conv_all_nth: |
|
13145 | 1286 |
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))" |
1287 |
by (auto simp add: set_conv_nth) |
|
13114 | 1288 |
|
25296 | 1289 |
lemma rev_nth: |
1290 |
"n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)" |
|
1291 |
proof (induct xs arbitrary: n) |
|
1292 |
case Nil thus ?case by simp |
|
1293 |
next |
|
1294 |
case (Cons x xs) |
|
1295 |
hence n: "n < Suc (length xs)" by simp |
|
1296 |
moreover |
|
1297 |
{ assume "n < length xs" |
|
1298 |
with n obtain n' where "length xs - n = Suc n'" |
|
1299 |
by (cases "length xs - n", auto) |
|
1300 |
moreover |
|
1301 |
then have "length xs - Suc n = n'" by simp |
|
1302 |
ultimately |
|
1303 |
have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp |
|
1304 |
} |
|
1305 |
ultimately |
|
1306 |
show ?case by (clarsimp simp add: Cons nth_append) |
|
1307 |
qed |
|
13114 | 1308 |
|
15392 | 1309 |
subsubsection {* @{text list_update} *} |
13114 | 1310 |
|
24526 | 1311 |
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs" |
1312 |
by (induct xs arbitrary: i) (auto split: nat.split) |
|
13114 | 1313 |
|
1314 |
lemma nth_list_update: |
|
24526 | 1315 |
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)" |
1316 |
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) |
|
13114 | 1317 |
|
13142 | 1318 |
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x" |
13145 | 1319 |
by (simp add: nth_list_update) |
13114 | 1320 |
|
24526 | 1321 |
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j" |
1322 |
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split) |
|
13114 | 1323 |
|
24526 | 1324 |
lemma list_update_id[simp]: "xs[i := xs!i] = xs" |
1325 |
by (induct xs arbitrary: i) (simp_all split:nat.splits) |
|
1326 |
||
1327 |
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs" |
|
1328 |
apply (induct xs arbitrary: i) |
|
17501 | 1329 |
apply simp |
1330 |
apply (case_tac i) |
|
1331 |
apply simp_all |
|
1332 |
done |
|
1333 |
||
13114 | 1334 |
lemma list_update_same_conv: |
24526 | 1335 |
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)" |
1336 |
by (induct xs arbitrary: i) (auto split: nat.split) |
|
13114 | 1337 |
|
14187 | 1338 |
lemma list_update_append1: |
24526 | 1339 |
"i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys" |
1340 |
apply (induct xs arbitrary: i, simp) |
|
14187 | 1341 |
apply(simp split:nat.split) |
1342 |
done |
|
1343 |
||
15868 | 1344 |
lemma list_update_append: |
24526 | 1345 |
"(xs @ ys) [n:= x] = |
15868 | 1346 |
(if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))" |
24526 | 1347 |
by (induct xs arbitrary: n) (auto split:nat.splits) |
15868 | 1348 |
|
14402
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1349 |
lemma list_update_length [simp]: |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1350 |
"(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
|
1351 |
by (induct xs, auto) |
4201e1916482
moved lemmas from MicroJava/Comp/AuxLemmas.thy to List.thy
nipkow
parents:
14395
diff
changeset
|
1352 |
|
13114 | 1353 |
lemma update_zip: |
24526 | 1354 |
"length xs = length ys ==> |
1355 |
(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])" |
|
1356 |
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split) |
|
1357 |
||
1358 |
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)" |
|
1359 |
by (induct xs arbitrary: i) (auto split: nat.split) |
|
13114 | 1360 |
|
1361 |
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A" |
|
13145 | 1362 |
by (blast dest!: set_update_subset_insert [THEN subsetD]) |
13114 | 1363 |
|
24526 | 1364 |
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])" |
1365 |
by (induct xs arbitrary: n) (auto split:nat.splits) |
|
15868 | 1366 |
|
24796 | 1367 |
lemma list_update_overwrite: |
1368 |
"xs [i := x, i := y] = xs [i := y]" |
|
1369 |
apply (induct xs arbitrary: i) |
|
1370 |
apply simp |
|
1371 |
apply (case_tac i) |
|
1372 |
apply simp_all |
|
1373 |
done |
|
1374 |
||
1375 |
lemma list_update_swap: |
|
1376 |
"i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]" |
|
1377 |
apply (induct xs arbitrary: i i') |
|
1378 |
apply simp |
|
1379 |
apply (case_tac i, case_tac i') |
|
1380 |
apply auto |
|
1381 |
apply (case_tac i') |
|
1382 |
apply auto |
|
1383 |
done |
|
1384 |
||
13114 | 1385 |
|
15392 | 1386 |
subsubsection {* @{text last} and @{text butlast} *} |
13114 | 1387 |
|
13142 | 1388 |
lemma last_snoc [simp]: "last (xs @ [x]) = x" |
13145 | 1389 |
by (induct xs) auto |
13114 | 1390 |
|
13142 | 1391 |
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs" |
13145 | 1392 |
by (induct xs) auto |
13114 | 1393 |
|
14302 | 1394 |
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x" |
1395 |
by(simp add:last.simps) |
|
1396 |
||
1397 |
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs" |
|
1398 |
by(simp add:last.simps) |
|
1399 |
||
1400 |
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)" |
|
1401 |
by (induct xs) (auto) |
|
1402 |
||
1403 |
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs" |
|
1404 |
by(simp add:last_append) |
|
1405 |
||
1406 |
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys" |
|
1407 |
by(simp add:last_append) |
|
1408 |
||
17762 | 1409 |
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs" |
1410 |
by(rule rev_exhaust[of xs]) simp_all |
|
1411 |
||
1412 |
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs" |
|
1413 |
by(cases xs) simp_all |
|
1414 |
||
17765 | 1415 |
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as" |
1416 |
by (induct as) auto |
|
17762 | 1417 |
|
13142 | 1418 |
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1" |
13145 | 1419 |
by (induct xs rule: rev_induct) auto |
13114 | 1420 |
|
1421 |
lemma butlast_append: |
|
24526 | 1422 |
"butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)" |
1423 |
by (induct xs arbitrary: ys) auto |
|
13114 | 1424 |
|
13142 | 1425 |
lemma append_butlast_last_id [simp]: |
13145 | 1426 |
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs" |
1427 |
by (induct xs) auto |
|
13114 | 1428 |
|
13142 | 1429 |
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs" |
13145 | 1430 |
by (induct xs) (auto split: split_if_asm) |
13114 | 1431 |
|
1432 |
lemma in_set_butlast_appendI: |
|
13145 | 1433 |
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))" |
1434 |
by (auto dest: in_set_butlastD simp add: butlast_append) |
|
13114 | 1435 |
|
24526 | 1436 |
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs" |
1437 |
apply (induct xs arbitrary: n) |
|
17501 | 1438 |
apply simp |
1439 |
apply (auto split:nat.split) |
|
1440 |
done |
|
1441 |
||
17589 | 1442 |
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)" |
1443 |
by(induct xs)(auto simp:neq_Nil_conv) |
|
1444 |
||
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1445 |
lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs" |
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1446 |
by (induct xs, simp, case_tac xs, simp_all) |
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1447 |
|
24796 | 1448 |
|
15392 | 1449 |
subsubsection {* @{text take} and @{text drop} *} |
13114 | 1450 |
|
13142 | 1451 |
lemma take_0 [simp]: "take 0 xs = []" |
13145 | 1452 |
by (induct xs) auto |
13114 | 1453 |
|
13142 | 1454 |
lemma drop_0 [simp]: "drop 0 xs = xs" |
13145 | 1455 |
by (induct xs) auto |
13114 | 1456 |
|
13142 | 1457 |
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs" |
13145 | 1458 |
by simp |
13114 | 1459 |
|
13142 | 1460 |
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs" |
13145 | 1461 |
by simp |
13114 | 1462 |
|
13142 | 1463 |
declare take_Cons [simp del] and drop_Cons [simp del] |
13114 | 1464 |
|
15110
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1465 |
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
|
1466 |
by(clarsimp simp add:neq_Nil_conv) |
78b5636eabc7
Added a number of new thms and the new function remove1
nipkow
parents:
15072
diff
changeset
|
1467 |
|
14187 | 1468 |
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)" |
1469 |
by(cases xs, simp_all) |
|
1470 |
||
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1471 |
lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)" |
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1472 |
by (induct xs arbitrary: n) simp_all |
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1473 |
|
24526 | 1474 |
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)" |
1475 |
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split) |
|
1476 |
||
26584
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1477 |
lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)" |
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1478 |
by (cases n, simp, cases xs, auto) |
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1479 |
|
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1480 |
lemma tl_drop: "tl (drop n xs) = drop n (tl xs)" |
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1481 |
by (simp only: drop_tl) |
46f3b89b2445
move lemmas from Word/BinBoolList.thy to List.thy
huffman
parents:
26480
diff
changeset
|
1482 |
|
24526 | 1483 |
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y" |
1484 |
apply (induct xs arbitrary: n, simp) |
|
14187 | 1485 |
apply(simp add:drop_Cons nth_Cons split:nat.splits) |
1486 |
done |
|
1487 |
||
13913 | 1488 |
lemma take_Suc_conv_app_nth: |
24526 | 1489 |
"i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]" |
1490 |
apply (induct xs arbitrary: i, simp) |
|
14208 | 1491 |
apply (case_tac i, auto) |
13913 | 1492 |
done |
1493 |
||
14591 | 1494 |
lemma drop_Suc_conv_tl: |
24526 | 1495 |
"i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs" |
1496 |
apply (induct xs arbitrary: i, simp) |
|
14591 | 1497 |
apply (case_tac i, auto) |
1498 |
done |
|
1499 |
||
24526 | 1500 |
lemma length_take [simp]: "length (take n xs) = min (length xs) n" |
1501 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
1502 |
||
1503 |
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)" |
|
1504 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
1505 |
||
1506 |
lemma take_all [simp]: "length xs <= n ==> take n xs = xs" |
|
1507 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
1508 |
||
1509 |
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []" |
|
1510 |
by (induct n arbitrary: xs) (auto, case_tac xs, auto) |
|
13114 | 1511 |
|
13142 | 1512 |
lemma take_append [simp]: |
24526 | 1513 |
"take n (xs @ ys) = (take n xs @ take (n - length xs) ys)" |