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