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