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