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