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