src/HOL/List.thy
author nipkow
Fri Dec 14 19:51:20 2012 +0100 (2012-12-14)
changeset 50548 0aec55e63795
parent 50422 ee729dbd1b7f
child 51096 60e4b75fefe1
permissions -rw-r--r--
unified layout of defs
wenzelm@13462
     1
(*  Title:      HOL/List.thy
wenzelm@13462
     2
    Author:     Tobias Nipkow
clasohm@923
     3
*)
clasohm@923
     4
wenzelm@13114
     5
header {* The datatype of finite lists *}
wenzelm@13122
     6
nipkow@15131
     7
theory List
krauss@44013
     8
imports Plain Presburger Code_Numeral Quotient ATP
nipkow@15131
     9
begin
clasohm@923
    10
wenzelm@13142
    11
datatype 'a list =
wenzelm@13366
    12
    Nil    ("[]")
wenzelm@13366
    13
  | Cons 'a  "'a list"    (infixr "#" 65)
clasohm@923
    14
haftmann@34941
    15
syntax
haftmann@34941
    16
  -- {* list Enumeration *}
wenzelm@35115
    17
  "_list" :: "args => 'a list"    ("[(_)]")
haftmann@34941
    18
haftmann@34941
    19
translations
haftmann@34941
    20
  "[x, xs]" == "x#[xs]"
haftmann@34941
    21
  "[x]" == "x#[]"
haftmann@34941
    22
wenzelm@35115
    23
wenzelm@35115
    24
subsection {* Basic list processing functions *}
nipkow@15302
    25
nipkow@50548
    26
primrec hd :: "'a list \<Rightarrow> 'a" where
nipkow@50548
    27
"hd (x # xs) = x"
nipkow@50548
    28
nipkow@50548
    29
primrec tl :: "'a list \<Rightarrow> 'a list" where
nipkow@50548
    30
"tl [] = []" |
nipkow@50548
    31
"tl (x # xs) = xs"
nipkow@50548
    32
nipkow@50548
    33
primrec last :: "'a list \<Rightarrow> 'a" where
nipkow@50548
    34
"last (x # xs) = (if xs = [] then x else last xs)"
nipkow@50548
    35
nipkow@50548
    36
primrec butlast :: "'a list \<Rightarrow> 'a list" where
nipkow@50548
    37
"butlast []= []" |
nipkow@50548
    38
"butlast (x # xs) = (if xs = [] then [] else x # butlast xs)"
nipkow@50548
    39
nipkow@50548
    40
primrec set :: "'a list \<Rightarrow> 'a set" where
nipkow@50548
    41
"set [] = {}" |
nipkow@50548
    42
"set (x # xs) = insert x (set xs)"
nipkow@50548
    43
nipkow@50548
    44
definition coset :: "'a list \<Rightarrow> 'a set" where
nipkow@50548
    45
[simp]: "coset xs = - set xs"
nipkow@50548
    46
nipkow@50548
    47
primrec map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list" where
nipkow@50548
    48
"map f [] = []" |
nipkow@50548
    49
"map f (x # xs) = f x # map f xs"
nipkow@50548
    50
nipkow@50548
    51
primrec append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65) where
nipkow@50548
    52
append_Nil: "[] @ ys = ys" |
nipkow@50548
    53
append_Cons: "(x#xs) @ ys = x # xs @ ys"
nipkow@50548
    54
nipkow@50548
    55
primrec rev :: "'a list \<Rightarrow> 'a list" where
nipkow@50548
    56
"rev [] = []" |
nipkow@50548
    57
"rev (x # xs) = rev xs @ [x]"
nipkow@50548
    58
nipkow@50548
    59
primrec filter:: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@50548
    60
"filter P [] = []" |
nipkow@50548
    61
"filter P (x # xs) = (if P x then x # filter P xs else filter P xs)"
haftmann@34941
    62
haftmann@34941
    63
syntax
haftmann@34941
    64
  -- {* Special syntax for filter *}
wenzelm@35115
    65
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
haftmann@34941
    66
haftmann@34941
    67
translations
haftmann@34941
    68
  "[x<-xs . P]"== "CONST filter (%x. P) xs"
haftmann@34941
    69
haftmann@34941
    70
syntax (xsymbols)
wenzelm@35115
    71
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
haftmann@34941
    72
syntax (HTML output)
wenzelm@35115
    73
  "_filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
haftmann@34941
    74
nipkow@50548
    75
primrec fold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
nipkow@50548
    76
fold_Nil:  "fold f [] = id" |
nipkow@50548
    77
fold_Cons: "fold f (x # xs) = fold f xs \<circ> f x"
nipkow@50548
    78
nipkow@50548
    79
primrec foldr :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b" where
nipkow@50548
    80
foldr_Nil:  "foldr f [] = id" |
nipkow@50548
    81
foldr_Cons: "foldr f (x # xs) = f x \<circ> foldr f xs"
nipkow@50548
    82
nipkow@50548
    83
primrec foldl :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b" where
nipkow@50548
    84
foldl_Nil:  "foldl f a [] = a" |
nipkow@50548
    85
foldl_Cons: "foldl f a (x # xs) = foldl f (f a x) xs"
nipkow@50548
    86
nipkow@50548
    87
primrec concat:: "'a list list \<Rightarrow> 'a list" where
nipkow@50548
    88
"concat [] = []" |
nipkow@50548
    89
"concat (x # xs) = x @ concat xs"
nipkow@50548
    90
nipkow@50548
    91
definition (in monoid_add) listsum :: "'a list \<Rightarrow> 'a" where
nipkow@50548
    92
"listsum xs = foldr plus xs 0"
nipkow@50548
    93
nipkow@50548
    94
primrec drop:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@50548
    95
drop_Nil: "drop n [] = []" |
nipkow@50548
    96
drop_Cons: "drop n (x # xs) = (case n of 0 \<Rightarrow> x # xs | Suc m \<Rightarrow> drop m xs)"
haftmann@34941
    97
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34941
    98
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
haftmann@34941
    99
nipkow@50548
   100
primrec take:: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@50548
   101
take_Nil:"take n [] = []" |
nipkow@50548
   102
take_Cons: "take n (x # xs) = (case n of 0 \<Rightarrow> [] | Suc m \<Rightarrow> x # take m xs)"
haftmann@34941
   103
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34941
   104
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
haftmann@34941
   105
nipkow@50548
   106
primrec nth :: "'a list => nat => 'a" (infixl "!" 100) where
nipkow@50548
   107
nth_Cons: "(x # xs) ! n = (case n of 0 \<Rightarrow> x | Suc k \<Rightarrow> xs ! k)"
haftmann@34941
   108
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34941
   109
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
haftmann@34941
   110
nipkow@50548
   111
primrec list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list" where
nipkow@50548
   112
"list_update [] i v = []" |
nipkow@50548
   113
"list_update (x # xs) i v =
nipkow@50548
   114
  (case i of 0 \<Rightarrow> v # xs | Suc j \<Rightarrow> x # list_update xs j v)"
clasohm@923
   115
wenzelm@41229
   116
nonterminal lupdbinds and lupdbind
nipkow@5077
   117
clasohm@923
   118
syntax
wenzelm@13366
   119
  "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
wenzelm@13366
   120
  "" :: "lupdbind => lupdbinds"    ("_")
wenzelm@13366
   121
  "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
wenzelm@13366
   122
  "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
nipkow@5077
   123
clasohm@923
   124
translations
wenzelm@35115
   125
  "_LUpdate xs (_lupdbinds b bs)" == "_LUpdate (_LUpdate xs b) bs"
haftmann@34941
   126
  "xs[i:=x]" == "CONST list_update xs i x"
haftmann@34941
   127
nipkow@50548
   128
primrec takeWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@50548
   129
"takeWhile P [] = []" |
nipkow@50548
   130
"takeWhile P (x # xs) = (if P x then x # takeWhile P xs else [])"
nipkow@50548
   131
nipkow@50548
   132
primrec dropWhile :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@50548
   133
"dropWhile P [] = []" |
nipkow@50548
   134
"dropWhile P (x # xs) = (if P x then dropWhile P xs else x # xs)"
nipkow@50548
   135
nipkow@50548
   136
primrec zip :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
nipkow@50548
   137
"zip xs [] = []" |
nipkow@50548
   138
zip_Cons: "zip xs (y # ys) =
nipkow@50548
   139
  (case xs of [] => [] | z # zs => (z, y) # zip zs ys)"
haftmann@34941
   140
  -- {*Warning: simpset does not contain this definition, but separate
haftmann@34941
   141
       theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
haftmann@34941
   142
nipkow@50548
   143
primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
nipkow@50548
   144
"product [] _ = []" |
nipkow@50548
   145
"product (x#xs) ys = map (Pair x) ys @ product xs ys"
haftmann@49948
   146
haftmann@49948
   147
hide_const (open) product
haftmann@49948
   148
nipkow@50548
   149
primrec upt :: "nat \<Rightarrow> nat \<Rightarrow> nat list" ("(1[_..</_'])") where
nipkow@50548
   150
upt_0: "[i..<0] = []" |
nipkow@50548
   151
upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])"
nipkow@50548
   152
nipkow@50548
   153
definition insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@50548
   154
"insert x xs = (if x \<in> set xs then xs else x # xs)"
haftmann@34978
   155
wenzelm@36176
   156
hide_const (open) insert
wenzelm@36176
   157
hide_fact (open) insert_def
haftmann@34978
   158
nipkow@47122
   159
primrec find :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a option" where
nipkow@50548
   160
"find _ [] = None" |
nipkow@50548
   161
"find P (x#xs) = (if P x then Some x else find P xs)"
nipkow@47122
   162
nipkow@47122
   163
hide_const (open) find
nipkow@47122
   164
nipkow@50548
   165
primrec remove1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@50548
   166
"remove1 x [] = []" |
nipkow@50548
   167
"remove1 x (y # xs) = (if x = y then xs else y # remove1 x xs)"
nipkow@50548
   168
nipkow@50548
   169
primrec removeAll :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@50548
   170
"removeAll x [] = []" |
nipkow@50548
   171
"removeAll x (y # xs) = (if x = y then removeAll x xs else y # removeAll x xs)"
nipkow@50548
   172
nipkow@50548
   173
primrec distinct :: "'a list \<Rightarrow> bool" where
nipkow@50548
   174
"distinct [] \<longleftrightarrow> True" |
nipkow@50548
   175
"distinct (x # xs) \<longleftrightarrow> x \<notin> set xs \<and> distinct xs"
nipkow@50548
   176
nipkow@50548
   177
primrec remdups :: "'a list \<Rightarrow> 'a list" where
nipkow@50548
   178
"remdups [] = []" |
nipkow@50548
   179
"remdups (x # xs) = (if x \<in> set xs then remdups xs else x # remdups xs)"
nipkow@50548
   180
nipkow@50548
   181
primrec replicate :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list" where
nipkow@50548
   182
replicate_0: "replicate 0 x = []" |
nipkow@50548
   183
replicate_Suc: "replicate (Suc n) x = x # replicate n x"
paulson@3342
   184
wenzelm@13142
   185
text {*
wenzelm@14589
   186
  Function @{text size} is overloaded for all datatypes. Users may
wenzelm@13366
   187
  refer to the list version as @{text length}. *}
wenzelm@13142
   188
nipkow@50548
   189
abbreviation length :: "'a list \<Rightarrow> nat" where
nipkow@50548
   190
"length \<equiv> size"
paulson@15307
   191
blanchet@46440
   192
primrec rotate1 :: "'a list \<Rightarrow> 'a list" where
nipkow@50548
   193
"rotate1 [] = []" |
nipkow@50548
   194
"rotate1 (x # xs) = xs @ [x]"
nipkow@50548
   195
nipkow@50548
   196
definition rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@50548
   197
"rotate n = rotate1 ^^ n"
nipkow@50548
   198
nipkow@50548
   199
definition list_all2 :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> bool" where
nipkow@50548
   200
"list_all2 P xs ys =
nipkow@50548
   201
  (length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))"
nipkow@50548
   202
nipkow@50548
   203
definition sublist :: "'a list => nat set => 'a list" where
nipkow@50548
   204
"sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))"
nipkow@50548
   205
nipkow@50548
   206
primrec sublists :: "'a list \<Rightarrow> 'a list list" where
nipkow@50548
   207
"sublists [] = [[]]" |
nipkow@50548
   208
"sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"
nipkow@50548
   209
nipkow@50548
   210
primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where
nipkow@50548
   211
"n_lists 0 xs = [[]]" |
nipkow@50548
   212
"n_lists (Suc n) xs = concat (map (\<lambda>ys. map (\<lambda>y. y # ys) xs) (n_lists n xs))"
haftmann@49948
   213
haftmann@49948
   214
hide_const (open) n_lists
haftmann@49948
   215
nipkow@40593
   216
fun splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@40593
   217
"splice [] ys = ys" |
nipkow@40593
   218
"splice xs [] = xs" |
nipkow@40593
   219
"splice (x#xs) (y#ys) = x # y # splice xs ys"
haftmann@21061
   220
nipkow@26771
   221
text{*
nipkow@26771
   222
\begin{figure}[htbp]
nipkow@26771
   223
\fbox{
nipkow@26771
   224
\begin{tabular}{l}
wenzelm@27381
   225
@{lemma "[a,b]@[c,d] = [a,b,c,d]" by simp}\\
wenzelm@27381
   226
@{lemma "length [a,b,c] = 3" by simp}\\
wenzelm@27381
   227
@{lemma "set [a,b,c] = {a,b,c}" by simp}\\
wenzelm@27381
   228
@{lemma "map f [a,b,c] = [f a, f b, f c]" by simp}\\
wenzelm@27381
   229
@{lemma "rev [a,b,c] = [c,b,a]" by simp}\\
wenzelm@27381
   230
@{lemma "hd [a,b,c,d] = a" by simp}\\
wenzelm@27381
   231
@{lemma "tl [a,b,c,d] = [b,c,d]" by simp}\\
wenzelm@27381
   232
@{lemma "last [a,b,c,d] = d" by simp}\\
wenzelm@27381
   233
@{lemma "butlast [a,b,c,d] = [a,b,c]" by simp}\\
wenzelm@27381
   234
@{lemma[source] "filter (\<lambda>n::nat. n<2) [0,2,1] = [0,1]" by simp}\\
wenzelm@27381
   235
@{lemma "concat [[a,b],[c,d,e],[],[f]] = [a,b,c,d,e,f]" by simp}\\
haftmann@46133
   236
@{lemma "fold f [a,b,c] x = f c (f b (f a x))" by simp}\\
haftmann@47397
   237
@{lemma "foldr f [a,b,c] x = f a (f b (f c x))" by simp}\\
haftmann@47397
   238
@{lemma "foldl f x [a,b,c] = f (f (f x a) b) c" by simp}\\
wenzelm@27381
   239
@{lemma "zip [a,b,c] [x,y,z] = [(a,x),(b,y),(c,z)]" by simp}\\
wenzelm@27381
   240
@{lemma "zip [a,b] [x,y,z] = [(a,x),(b,y)]" by simp}\\
haftmann@49948
   241
@{lemma "List.product [a,b] [c,d] = [(a, c), (a, d), (b, c), (b, d)]" by simp}\\
wenzelm@27381
   242
@{lemma "splice [a,b,c] [x,y,z] = [a,x,b,y,c,z]" by simp}\\
wenzelm@27381
   243
@{lemma "splice [a,b,c,d] [x,y] = [a,x,b,y,c,d]" by simp}\\
wenzelm@27381
   244
@{lemma "take 2 [a,b,c,d] = [a,b]" by simp}\\
wenzelm@27381
   245
@{lemma "take 6 [a,b,c,d] = [a,b,c,d]" by simp}\\
wenzelm@27381
   246
@{lemma "drop 2 [a,b,c,d] = [c,d]" by simp}\\
wenzelm@27381
   247
@{lemma "drop 6 [a,b,c,d] = []" by simp}\\
wenzelm@27381
   248
@{lemma "takeWhile (%n::nat. n<3) [1,2,3,0] = [1,2]" by simp}\\
wenzelm@27381
   249
@{lemma "dropWhile (%n::nat. n<3) [1,2,3,0] = [3,0]" by simp}\\
wenzelm@27381
   250
@{lemma "distinct [2,0,1::nat]" by simp}\\
wenzelm@27381
   251
@{lemma "remdups [2,0,2,1::nat,2] = [0,1,2]" by simp}\\
haftmann@34978
   252
@{lemma "List.insert 2 [0::nat,1,2] = [0,1,2]" by (simp add: List.insert_def)}\\
haftmann@35295
   253
@{lemma "List.insert 3 [0::nat,1,2] = [3,0,1,2]" by (simp add: List.insert_def)}\\
nipkow@47122
   254
@{lemma "List.find (%i::int. i>0) [0,0] = None" by simp}\\
nipkow@47122
   255
@{lemma "List.find (%i::int. i>0) [0,1,0,2] = Some 1" by simp}\\
wenzelm@27381
   256
@{lemma "remove1 2 [2,0,2,1::nat,2] = [0,2,1,2]" by simp}\\
nipkow@27693
   257
@{lemma "removeAll 2 [2,0,2,1::nat,2] = [0,1]" by simp}\\
wenzelm@27381
   258
@{lemma "nth [a,b,c,d] 2 = c" by simp}\\
wenzelm@27381
   259
@{lemma "[a,b,c,d][2 := x] = [a,b,x,d]" by simp}\\
wenzelm@27381
   260
@{lemma "sublist [a,b,c,d,e] {0,2,3} = [a,c,d]" by (simp add:sublist_def)}\\
haftmann@49948
   261
@{lemma "sublists [a,b] = [[a, b], [a], [b], []]" by simp}\\
haftmann@49948
   262
@{lemma "List.n_lists 2 [a,b,c] = [[a, a], [b, a], [c, a], [a, b], [b, b], [c, b], [a, c], [b, c], [c, c]]" by (simp add: eval_nat_numeral)}\\
blanchet@46440
   263
@{lemma "rotate1 [a,b,c,d] = [b,c,d,a]" by simp}\\
blanchet@46440
   264
@{lemma "rotate 3 [a,b,c,d] = [d,a,b,c]" by (simp add:rotate_def eval_nat_numeral)}\\
nipkow@40077
   265
@{lemma "replicate 4 a = [a,a,a,a]" by (simp add:eval_nat_numeral)}\\
nipkow@40077
   266
@{lemma "[2..<5] = [2,3,4]" by (simp add:eval_nat_numeral)}\\
haftmann@47397
   267
@{lemma "listsum [1,2,3::nat] = 6" by (simp add: listsum_def)}
nipkow@26771
   268
\end{tabular}}
nipkow@26771
   269
\caption{Characteristic examples}
nipkow@26771
   270
\label{fig:Characteristic}
nipkow@26771
   271
\end{figure}
blanchet@29927
   272
Figure~\ref{fig:Characteristic} shows characteristic examples
nipkow@26771
   273
that should give an intuitive understanding of the above functions.
nipkow@26771
   274
*}
nipkow@26771
   275
nipkow@24616
   276
text{* The following simple sort functions are intended for proofs,
nipkow@24616
   277
not for efficient implementations. *}
nipkow@24616
   278
wenzelm@25221
   279
context linorder
wenzelm@25221
   280
begin
wenzelm@25221
   281
haftmann@39915
   282
inductive sorted :: "'a list \<Rightarrow> bool" where
haftmann@39915
   283
  Nil [iff]: "sorted []"
haftmann@39915
   284
| Cons: "\<forall>y\<in>set xs. x \<le> y \<Longrightarrow> sorted xs \<Longrightarrow> sorted (x # xs)"
haftmann@39915
   285
haftmann@39915
   286
lemma sorted_single [iff]:
haftmann@39915
   287
  "sorted [x]"
haftmann@39915
   288
  by (rule sorted.Cons) auto
haftmann@39915
   289
haftmann@39915
   290
lemma sorted_many:
haftmann@39915
   291
  "x \<le> y \<Longrightarrow> sorted (y # zs) \<Longrightarrow> sorted (x # y # zs)"
haftmann@39915
   292
  by (rule sorted.Cons) (cases "y # zs" rule: sorted.cases, auto)
haftmann@39915
   293
haftmann@39915
   294
lemma sorted_many_eq [simp, code]:
haftmann@39915
   295
  "sorted (x # y # zs) \<longleftrightarrow> x \<le> y \<and> sorted (y # zs)"
haftmann@39915
   296
  by (auto intro: sorted_many elim: sorted.cases)
haftmann@39915
   297
haftmann@39915
   298
lemma [code]:
haftmann@39915
   299
  "sorted [] \<longleftrightarrow> True"
haftmann@39915
   300
  "sorted [x] \<longleftrightarrow> True"
haftmann@39915
   301
  by simp_all
nipkow@24697
   302
hoelzl@33639
   303
primrec insort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
nipkow@50548
   304
"insort_key f x [] = [x]" |
nipkow@50548
   305
"insort_key f x (y#ys) =
nipkow@50548
   306
  (if f x \<le> f y then (x#y#ys) else y#(insort_key f x ys))"
hoelzl@33639
   307
haftmann@35195
   308
definition sort_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b list \<Rightarrow> 'b list" where
nipkow@50548
   309
"sort_key f xs = foldr (insort_key f) xs []"
hoelzl@33639
   310
haftmann@40210
   311
definition insort_insert_key :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b \<Rightarrow> 'b list \<Rightarrow> 'b list" where
nipkow@50548
   312
"insort_insert_key f x xs =
nipkow@50548
   313
  (if f x \<in> f ` set xs then xs else insort_key f x xs)"
haftmann@40210
   314
hoelzl@33639
   315
abbreviation "sort \<equiv> sort_key (\<lambda>x. x)"
hoelzl@33639
   316
abbreviation "insort \<equiv> insort_key (\<lambda>x. x)"
haftmann@40210
   317
abbreviation "insort_insert \<equiv> insort_insert_key (\<lambda>x. x)"
haftmann@35608
   318
wenzelm@25221
   319
end
wenzelm@25221
   320
nipkow@24616
   321
wenzelm@23388
   322
subsubsection {* List comprehension *}
nipkow@23192
   323
nipkow@24349
   324
text{* Input syntax for Haskell-like list comprehension notation.
nipkow@24349
   325
Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
nipkow@24349
   326
the list of all pairs of distinct elements from @{text xs} and @{text ys}.
nipkow@24349
   327
The syntax is as in Haskell, except that @{text"|"} becomes a dot
nipkow@24349
   328
(like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
nipkow@24349
   329
\verb![e| x <- xs, ...]!.
nipkow@24349
   330
nipkow@24349
   331
The qualifiers after the dot are
nipkow@24349
   332
\begin{description}
nipkow@24349
   333
\item[generators] @{text"p \<leftarrow> xs"},
nipkow@24476
   334
 where @{text p} is a pattern and @{text xs} an expression of list type, or
nipkow@24476
   335
\item[guards] @{text"b"}, where @{text b} is a boolean expression.
nipkow@24476
   336
%\item[local bindings] @ {text"let x = e"}.
nipkow@24349
   337
\end{description}
nipkow@23240
   338
nipkow@24476
   339
Just like in Haskell, list comprehension is just a shorthand. To avoid
nipkow@24476
   340
misunderstandings, the translation into desugared form is not reversed
nipkow@24476
   341
upon output. Note that the translation of @{text"[e. x \<leftarrow> xs]"} is
nipkow@24476
   342
optmized to @{term"map (%x. e) xs"}.
nipkow@23240
   343
nipkow@24349
   344
It is easy to write short list comprehensions which stand for complex
nipkow@24349
   345
expressions. During proofs, they may become unreadable (and
nipkow@24349
   346
mangled). In such cases it can be advisable to introduce separate
nipkow@24349
   347
definitions for the list comprehensions in question.  *}
nipkow@24349
   348
wenzelm@46138
   349
nonterminal lc_qual and lc_quals
nipkow@23192
   350
nipkow@23192
   351
syntax
wenzelm@46138
   352
  "_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list"  ("[_ . __")
wenzelm@46138
   353
  "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ <- _")
wenzelm@46138
   354
  "_lc_test" :: "bool \<Rightarrow> lc_qual" ("_")
wenzelm@46138
   355
  (*"_lc_let" :: "letbinds => lc_qual"  ("let _")*)
wenzelm@46138
   356
  "_lc_end" :: "lc_quals" ("]")
wenzelm@46138
   357
  "_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals"  (", __")
wenzelm@46138
   358
  "_lc_abs" :: "'a => 'b list => 'b list"
nipkow@23192
   359
nipkow@24476
   360
(* These are easier than ML code but cannot express the optimized
nipkow@24476
   361
   translation of [e. p<-xs]
nipkow@23192
   362
translations
wenzelm@46138
   363
  "[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)"
wenzelm@46138
   364
  "_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)"
wenzelm@46138
   365
   => "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)"
wenzelm@46138
   366
  "[e. P]" => "if P then [e] else []"
wenzelm@46138
   367
  "_listcompr e (_lc_test P) (_lc_quals Q Qs)"
wenzelm@46138
   368
   => "if P then (_listcompr e Q Qs) else []"
wenzelm@46138
   369
  "_listcompr e (_lc_let b) (_lc_quals Q Qs)"
wenzelm@46138
   370
   => "_Let b (_listcompr e Q Qs)"
nipkow@24476
   371
*)
nipkow@23240
   372
nipkow@23279
   373
syntax (xsymbols)
wenzelm@46138
   374
  "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ \<leftarrow> _")
nipkow@23279
   375
syntax (HTML output)
wenzelm@46138
   376
  "_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual"  ("_ \<leftarrow> _")
nipkow@24349
   377
nipkow@24349
   378
parse_translation (advanced) {*
wenzelm@46138
   379
  let
wenzelm@46138
   380
    val NilC = Syntax.const @{const_syntax Nil};
wenzelm@46138
   381
    val ConsC = Syntax.const @{const_syntax Cons};
wenzelm@46138
   382
    val mapC = Syntax.const @{const_syntax map};
wenzelm@46138
   383
    val concatC = Syntax.const @{const_syntax concat};
wenzelm@46138
   384
    val IfC = Syntax.const @{const_syntax If};
wenzelm@46138
   385
wenzelm@46138
   386
    fun single x = ConsC $ x $ NilC;
wenzelm@46138
   387
wenzelm@46138
   388
    fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *)
wenzelm@46138
   389
      let
wenzelm@46138
   390
        (* FIXME proper name context!? *)
wenzelm@46138
   391
        val x =
wenzelm@46138
   392
          Free (singleton (Name.variant_list (fold Term.add_free_names [p, e] [])) "x", dummyT);
wenzelm@46138
   393
        val e = if opti then single e else e;
wenzelm@46138
   394
        val case1 = Syntax.const @{syntax_const "_case1"} $ p $ e;
wenzelm@46138
   395
        val case2 =
wenzelm@46138
   396
          Syntax.const @{syntax_const "_case1"} $
wenzelm@46138
   397
            Syntax.const @{const_syntax dummy_pattern} $ NilC;
wenzelm@46138
   398
        val cs = Syntax.const @{syntax_const "_case2"} $ case1 $ case2;
wenzelm@46138
   399
      in Syntax_Trans.abs_tr [x, Datatype_Case.case_tr false ctxt [x, cs]] end;
wenzelm@46138
   400
wenzelm@46138
   401
    fun abs_tr ctxt p e opti =
wenzelm@46138
   402
      (case Term_Position.strip_positions p of
wenzelm@46138
   403
        Free (s, T) =>
wenzelm@46138
   404
          let
wenzelm@46138
   405
            val thy = Proof_Context.theory_of ctxt;
wenzelm@46138
   406
            val s' = Proof_Context.intern_const ctxt s;
wenzelm@46138
   407
          in
wenzelm@46138
   408
            if Sign.declared_const thy s'
wenzelm@46138
   409
            then (pat_tr ctxt p e opti, false)
wenzelm@46138
   410
            else (Syntax_Trans.abs_tr [p, e], true)
wenzelm@46138
   411
          end
wenzelm@46138
   412
      | _ => (pat_tr ctxt p e opti, false));
wenzelm@46138
   413
wenzelm@46138
   414
    fun lc_tr ctxt [e, Const (@{syntax_const "_lc_test"}, _) $ b, qs] =
wenzelm@46138
   415
          let
wenzelm@46138
   416
            val res =
wenzelm@46138
   417
              (case qs of
wenzelm@46138
   418
                Const (@{syntax_const "_lc_end"}, _) => single e
wenzelm@46138
   419
              | Const (@{syntax_const "_lc_quals"}, _) $ q $ qs => lc_tr ctxt [e, q, qs]);
wenzelm@46138
   420
          in IfC $ b $ res $ NilC end
wenzelm@46138
   421
      | lc_tr ctxt
wenzelm@46138
   422
            [e, Const (@{syntax_const "_lc_gen"}, _) $ p $ es,
wenzelm@46138
   423
              Const(@{syntax_const "_lc_end"}, _)] =
wenzelm@46138
   424
          (case abs_tr ctxt p e true of
wenzelm@46138
   425
            (f, true) => mapC $ f $ es
wenzelm@46138
   426
          | (f, false) => concatC $ (mapC $ f $ es))
wenzelm@46138
   427
      | lc_tr ctxt
wenzelm@46138
   428
            [e, Const (@{syntax_const "_lc_gen"}, _) $ p $ es,
wenzelm@46138
   429
              Const (@{syntax_const "_lc_quals"}, _) $ q $ qs] =
wenzelm@46138
   430
          let val e' = lc_tr ctxt [e, q, qs];
wenzelm@46138
   431
          in concatC $ (mapC $ (fst (abs_tr ctxt p e' false)) $ es) end;
wenzelm@46138
   432
wenzelm@46138
   433
  in [(@{syntax_const "_listcompr"}, lc_tr)] end
nipkow@24349
   434
*}
nipkow@23279
   435
wenzelm@42167
   436
ML {*
wenzelm@42167
   437
  let
wenzelm@42167
   438
    val read = Syntax.read_term @{context};
wenzelm@42167
   439
    fun check s1 s2 = read s1 aconv read s2 orelse error ("Check failed: " ^ quote s1);
wenzelm@42167
   440
  in
wenzelm@42167
   441
    check "[(x,y,z). b]" "if b then [(x, y, z)] else []";
wenzelm@42167
   442
    check "[(x,y,z). x\<leftarrow>xs]" "map (\<lambda>x. (x, y, z)) xs";
wenzelm@42167
   443
    check "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]" "concat (map (\<lambda>x. map (\<lambda>y. e x y) ys) xs)";
wenzelm@42167
   444
    check "[(x,y,z). x<a, x>b]" "if x < a then if b < x then [(x, y, z)] else [] else []";
wenzelm@42167
   445
    check "[(x,y,z). x\<leftarrow>xs, x>b]" "concat (map (\<lambda>x. if b < x then [(x, y, z)] else []) xs)";
wenzelm@42167
   446
    check "[(x,y,z). x<a, x\<leftarrow>xs]" "if x < a then map (\<lambda>x. (x, y, z)) xs else []";
wenzelm@42167
   447
    check "[(x,y). Cons True x \<leftarrow> xs]"
wenzelm@42167
   448
      "concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | True # x \<Rightarrow> [(x, y)] | False # x \<Rightarrow> []) xs)";
wenzelm@42167
   449
    check "[(x,y,z). Cons x [] \<leftarrow> xs]"
wenzelm@42167
   450
      "concat (map (\<lambda>xa. case xa of [] \<Rightarrow> [] | [x] \<Rightarrow> [(x, y, z)] | x # aa # lista \<Rightarrow> []) xs)";
wenzelm@42167
   451
    check "[(x,y,z). x<a, x>b, x=d]"
wenzelm@42167
   452
      "if x < a then if b < x then if x = d then [(x, y, z)] else [] else [] else []";
wenzelm@42167
   453
    check "[(x,y,z). x<a, x>b, y\<leftarrow>ys]"
wenzelm@42167
   454
      "if x < a then if b < x then map (\<lambda>y. (x, y, z)) ys else [] else []";
wenzelm@42167
   455
    check "[(x,y,z). x<a, x\<leftarrow>xs,y>b]"
wenzelm@42167
   456
      "if x < a then concat (map (\<lambda>x. if b < y then [(x, y, z)] else []) xs) else []";
wenzelm@42167
   457
    check "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]"
wenzelm@42167
   458
      "if x < a then concat (map (\<lambda>x. map (\<lambda>y. (x, y, z)) ys) xs) else []";
wenzelm@42167
   459
    check "[(x,y,z). x\<leftarrow>xs, x>b, y<a]"
wenzelm@42167
   460
      "concat (map (\<lambda>x. if b < x then if y < a then [(x, y, z)] else [] else []) xs)";
wenzelm@42167
   461
    check "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]"
wenzelm@42167
   462
      "concat (map (\<lambda>x. if b < x then map (\<lambda>y. (x, y, z)) ys else []) xs)";
wenzelm@42167
   463
    check "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]"
wenzelm@42167
   464
      "concat (map (\<lambda>x. concat (map (\<lambda>y. if x < y then [(x, y, z)] else []) ys)) xs)";
wenzelm@42167
   465
    check "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]"
wenzelm@42167
   466
      "concat (map (\<lambda>x. concat (map (\<lambda>y. map (\<lambda>z. (x, y, z)) zs) ys)) xs)"
wenzelm@42167
   467
  end;
wenzelm@42167
   468
*}
wenzelm@42167
   469
wenzelm@35115
   470
(*
nipkow@24349
   471
term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]"
nipkow@23192
   472
*)
nipkow@23192
   473
wenzelm@42167
   474
wenzelm@50422
   475
ML {*
wenzelm@50422
   476
(* Simproc for rewriting list comprehensions applied to List.set to set
wenzelm@50422
   477
   comprehension. *)
wenzelm@50422
   478
wenzelm@50422
   479
signature LIST_TO_SET_COMPREHENSION =
wenzelm@50422
   480
sig
wenzelm@50422
   481
  val simproc : simpset -> cterm -> thm option
wenzelm@50422
   482
end
wenzelm@50422
   483
wenzelm@50422
   484
structure List_to_Set_Comprehension : LIST_TO_SET_COMPREHENSION =
wenzelm@50422
   485
struct
wenzelm@50422
   486
wenzelm@50422
   487
(* conversion *)
wenzelm@50422
   488
wenzelm@50422
   489
fun all_exists_conv cv ctxt ct =
wenzelm@50422
   490
  (case Thm.term_of ct of
wenzelm@50422
   491
    Const (@{const_name HOL.Ex}, _) $ Abs _ =>
wenzelm@50422
   492
      Conv.arg_conv (Conv.abs_conv (all_exists_conv cv o #2) ctxt) ct
wenzelm@50422
   493
  | _ => cv ctxt ct)
wenzelm@50422
   494
wenzelm@50422
   495
fun all_but_last_exists_conv cv ctxt ct =
wenzelm@50422
   496
  (case Thm.term_of ct of
wenzelm@50422
   497
    Const (@{const_name HOL.Ex}, _) $ Abs (_, _, Const (@{const_name HOL.Ex}, _) $ _) =>
wenzelm@50422
   498
      Conv.arg_conv (Conv.abs_conv (all_but_last_exists_conv cv o #2) ctxt) ct
wenzelm@50422
   499
  | _ => cv ctxt ct)
wenzelm@50422
   500
wenzelm@50422
   501
fun Collect_conv cv ctxt ct =
wenzelm@50422
   502
  (case Thm.term_of ct of
wenzelm@50422
   503
    Const (@{const_name Set.Collect}, _) $ Abs _ => Conv.arg_conv (Conv.abs_conv cv ctxt) ct
wenzelm@50422
   504
  | _ => raise CTERM ("Collect_conv", [ct]))
wenzelm@50422
   505
wenzelm@50422
   506
fun Trueprop_conv cv ct =
wenzelm@50422
   507
  (case Thm.term_of ct of
wenzelm@50422
   508
    Const (@{const_name Trueprop}, _) $ _ => Conv.arg_conv cv ct
wenzelm@50422
   509
  | _ => raise CTERM ("Trueprop_conv", [ct]))
wenzelm@50422
   510
wenzelm@50422
   511
fun eq_conv cv1 cv2 ct =
wenzelm@50422
   512
  (case Thm.term_of ct of
wenzelm@50422
   513
    Const (@{const_name HOL.eq}, _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv1) cv2 ct
wenzelm@50422
   514
  | _ => raise CTERM ("eq_conv", [ct]))
wenzelm@50422
   515
wenzelm@50422
   516
fun conj_conv cv1 cv2 ct =
wenzelm@50422
   517
  (case Thm.term_of ct of
wenzelm@50422
   518
    Const (@{const_name HOL.conj}, _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv1) cv2 ct
wenzelm@50422
   519
  | _ => raise CTERM ("conj_conv", [ct]))
wenzelm@50422
   520
wenzelm@50422
   521
fun rewr_conv' th = Conv.rewr_conv (mk_meta_eq th)
wenzelm@50422
   522
wenzelm@50422
   523
fun conjunct_assoc_conv ct =
wenzelm@50422
   524
  Conv.try_conv
wenzelm@50422
   525
    (rewr_conv' @{thm conj_assoc} then_conv conj_conv Conv.all_conv conjunct_assoc_conv) ct
wenzelm@50422
   526
wenzelm@50422
   527
fun right_hand_set_comprehension_conv conv ctxt =
wenzelm@50422
   528
  Trueprop_conv (eq_conv Conv.all_conv
wenzelm@50422
   529
    (Collect_conv (all_exists_conv conv o #2) ctxt))
wenzelm@50422
   530
wenzelm@50422
   531
wenzelm@50422
   532
(* term abstraction of list comprehension patterns *)
wenzelm@50422
   533
wenzelm@50422
   534
datatype termlets = If | Case of (typ * int)
wenzelm@50422
   535
wenzelm@50422
   536
fun simproc ss redex =
wenzelm@50422
   537
  let
wenzelm@50422
   538
    val ctxt = Simplifier.the_context ss
wenzelm@50422
   539
    val thy = Proof_Context.theory_of ctxt
wenzelm@50422
   540
    val set_Nil_I = @{thm trans} OF [@{thm set.simps(1)}, @{thm empty_def}]
wenzelm@50422
   541
    val set_singleton = @{lemma "set [a] = {x. x = a}" by simp}
wenzelm@50422
   542
    val inst_Collect_mem_eq = @{lemma "set A = {x. x : set A}" by simp}
wenzelm@50422
   543
    val del_refl_eq = @{lemma "(t = t & P) == P" by simp}
wenzelm@50422
   544
    fun mk_set T = Const (@{const_name List.set}, HOLogic.listT T --> HOLogic.mk_setT T)
wenzelm@50422
   545
    fun dest_set (Const (@{const_name List.set}, _) $ xs) = xs
wenzelm@50422
   546
    fun dest_singleton_list (Const (@{const_name List.Cons}, _)
wenzelm@50422
   547
          $ t $ (Const (@{const_name List.Nil}, _))) = t
wenzelm@50422
   548
      | dest_singleton_list t = raise TERM ("dest_singleton_list", [t])
wenzelm@50422
   549
    (* We check that one case returns a singleton list and all other cases
wenzelm@50422
   550
       return [], and return the index of the one singleton list case *)
wenzelm@50422
   551
    fun possible_index_of_singleton_case cases =
wenzelm@50422
   552
      let
wenzelm@50422
   553
        fun check (i, case_t) s =
wenzelm@50422
   554
          (case strip_abs_body case_t of
wenzelm@50422
   555
            (Const (@{const_name List.Nil}, _)) => s
wenzelm@50422
   556
          | _ => (case s of NONE => SOME i | SOME _ => NONE))
wenzelm@50422
   557
      in
wenzelm@50422
   558
        fold_index check cases NONE
wenzelm@50422
   559
      end
wenzelm@50422
   560
    (* returns (case_expr type index chosen_case) option  *)
wenzelm@50422
   561
    fun dest_case case_term =
wenzelm@50422
   562
      let
wenzelm@50422
   563
        val (case_const, args) = strip_comb case_term
wenzelm@50422
   564
      in
wenzelm@50422
   565
        (case try dest_Const case_const of
wenzelm@50422
   566
          SOME (c, T) =>
wenzelm@50422
   567
            (case Datatype.info_of_case thy c of
wenzelm@50422
   568
              SOME _ =>
wenzelm@50422
   569
                (case possible_index_of_singleton_case (fst (split_last args)) of
wenzelm@50422
   570
                  SOME i =>
wenzelm@50422
   571
                    let
wenzelm@50422
   572
                      val (Ts, _) = strip_type T
wenzelm@50422
   573
                      val T' = List.last Ts
wenzelm@50422
   574
                    in SOME (List.last args, T', i, nth args i) end
wenzelm@50422
   575
                | NONE => NONE)
wenzelm@50422
   576
            | NONE => NONE)
wenzelm@50422
   577
        | NONE => NONE)
wenzelm@50422
   578
      end
wenzelm@50422
   579
    (* returns condition continuing term option *)
wenzelm@50422
   580
    fun dest_if (Const (@{const_name If}, _) $ cond $ then_t $ Const (@{const_name Nil}, _)) =
wenzelm@50422
   581
          SOME (cond, then_t)
wenzelm@50422
   582
      | dest_if _ = NONE
wenzelm@50422
   583
    fun tac _ [] = rtac set_singleton 1 ORELSE rtac inst_Collect_mem_eq 1
wenzelm@50422
   584
      | tac ctxt (If :: cont) =
wenzelm@50422
   585
          Splitter.split_tac [@{thm split_if}] 1
wenzelm@50422
   586
          THEN rtac @{thm conjI} 1
wenzelm@50422
   587
          THEN rtac @{thm impI} 1
wenzelm@50422
   588
          THEN Subgoal.FOCUS (fn {prems, context, ...} =>
wenzelm@50422
   589
            CONVERSION (right_hand_set_comprehension_conv (K
wenzelm@50422
   590
              (conj_conv (Conv.rewr_conv (List.last prems RS @{thm Eq_TrueI})) Conv.all_conv
wenzelm@50422
   591
               then_conv
wenzelm@50422
   592
               rewr_conv' @{lemma "(True & P) = P" by simp})) context) 1) ctxt 1
wenzelm@50422
   593
          THEN tac ctxt cont
wenzelm@50422
   594
          THEN rtac @{thm impI} 1
wenzelm@50422
   595
          THEN Subgoal.FOCUS (fn {prems, context, ...} =>
wenzelm@50422
   596
              CONVERSION (right_hand_set_comprehension_conv (K
wenzelm@50422
   597
                (conj_conv (Conv.rewr_conv (List.last prems RS @{thm Eq_FalseI})) Conv.all_conv
wenzelm@50422
   598
                 then_conv rewr_conv' @{lemma "(False & P) = False" by simp})) context) 1) ctxt 1
wenzelm@50422
   599
          THEN rtac set_Nil_I 1
wenzelm@50422
   600
      | tac ctxt (Case (T, i) :: cont) =
wenzelm@50422
   601
          let
wenzelm@50422
   602
            val info = Datatype.the_info thy (fst (dest_Type T))
wenzelm@50422
   603
          in
wenzelm@50422
   604
            (* do case distinction *)
wenzelm@50422
   605
            Splitter.split_tac [#split info] 1
wenzelm@50422
   606
            THEN EVERY (map_index (fn (i', _) =>
wenzelm@50422
   607
              (if i' < length (#case_rewrites info) - 1 then rtac @{thm conjI} 1 else all_tac)
wenzelm@50422
   608
              THEN REPEAT_DETERM (rtac @{thm allI} 1)
wenzelm@50422
   609
              THEN rtac @{thm impI} 1
wenzelm@50422
   610
              THEN (if i' = i then
wenzelm@50422
   611
                (* continue recursively *)
wenzelm@50422
   612
                Subgoal.FOCUS (fn {prems, context, ...} =>
wenzelm@50422
   613
                  CONVERSION (Thm.eta_conversion then_conv right_hand_set_comprehension_conv (K
wenzelm@50422
   614
                      ((conj_conv
wenzelm@50422
   615
                        (eq_conv Conv.all_conv (rewr_conv' (List.last prems)) then_conv
wenzelm@50422
   616
                          (Conv.try_conv (Conv.rewrs_conv (map mk_meta_eq (#inject info)))))
wenzelm@50422
   617
                        Conv.all_conv)
wenzelm@50422
   618
                        then_conv (Conv.try_conv (Conv.rewr_conv del_refl_eq))
wenzelm@50422
   619
                        then_conv conjunct_assoc_conv)) context
wenzelm@50422
   620
                    then_conv (Trueprop_conv (eq_conv Conv.all_conv (Collect_conv (fn (_, ctxt) =>
wenzelm@50422
   621
                      Conv.repeat_conv
wenzelm@50422
   622
                        (all_but_last_exists_conv
wenzelm@50422
   623
                          (K (rewr_conv'
wenzelm@50422
   624
                            @{lemma "(EX x. x = t & P x) = P t" by simp})) ctxt)) context)))) 1) ctxt 1
wenzelm@50422
   625
                THEN tac ctxt cont
wenzelm@50422
   626
              else
wenzelm@50422
   627
                Subgoal.FOCUS (fn {prems, context, ...} =>
wenzelm@50422
   628
                  CONVERSION
wenzelm@50422
   629
                    (right_hand_set_comprehension_conv (K
wenzelm@50422
   630
                      (conj_conv
wenzelm@50422
   631
                        ((eq_conv Conv.all_conv
wenzelm@50422
   632
                          (rewr_conv' (List.last prems))) then_conv
wenzelm@50422
   633
                          (Conv.rewrs_conv (map (fn th => th RS @{thm Eq_FalseI}) (#distinct info))))
wenzelm@50422
   634
                        Conv.all_conv then_conv
wenzelm@50422
   635
                        (rewr_conv' @{lemma "(False & P) = False" by simp}))) context then_conv
wenzelm@50422
   636
                      Trueprop_conv
wenzelm@50422
   637
                        (eq_conv Conv.all_conv
wenzelm@50422
   638
                          (Collect_conv (fn (_, ctxt) =>
wenzelm@50422
   639
                            Conv.repeat_conv
wenzelm@50422
   640
                              (Conv.bottom_conv
wenzelm@50422
   641
                                (K (rewr_conv'
wenzelm@50422
   642
                                  @{lemma "(EX x. P) = P" by simp})) ctxt)) context))) 1) ctxt 1
wenzelm@50422
   643
                THEN rtac set_Nil_I 1)) (#case_rewrites info))
wenzelm@50422
   644
          end
wenzelm@50422
   645
    fun make_inner_eqs bound_vs Tis eqs t =
wenzelm@50422
   646
      (case dest_case t of
wenzelm@50422
   647
        SOME (x, T, i, cont) =>
wenzelm@50422
   648
          let
wenzelm@50422
   649
            val (vs, body) = strip_abs (Pattern.eta_long (map snd bound_vs) cont)
wenzelm@50422
   650
            val x' = incr_boundvars (length vs) x
wenzelm@50422
   651
            val eqs' = map (incr_boundvars (length vs)) eqs
wenzelm@50422
   652
            val (constr_name, _) = nth (the (Datatype.get_constrs thy (fst (dest_Type T)))) i
wenzelm@50422
   653
            val constr_t =
wenzelm@50422
   654
              list_comb
wenzelm@50422
   655
                (Const (constr_name, map snd vs ---> T), map Bound (((length vs) - 1) downto 0))
wenzelm@50422
   656
            val constr_eq = Const (@{const_name HOL.eq}, T --> T --> @{typ bool}) $ constr_t $ x'
wenzelm@50422
   657
          in
wenzelm@50422
   658
            make_inner_eqs (rev vs @ bound_vs) (Case (T, i) :: Tis) (constr_eq :: eqs') body
wenzelm@50422
   659
          end
wenzelm@50422
   660
      | NONE =>
wenzelm@50422
   661
          (case dest_if t of
wenzelm@50422
   662
            SOME (condition, cont) => make_inner_eqs bound_vs (If :: Tis) (condition :: eqs) cont
wenzelm@50422
   663
          | NONE =>
wenzelm@50422
   664
            if eqs = [] then NONE (* no rewriting, nothing to be done *)
wenzelm@50422
   665
            else
wenzelm@50422
   666
              let
wenzelm@50422
   667
                val Type (@{type_name List.list}, [rT]) = fastype_of1 (map snd bound_vs, t)
wenzelm@50422
   668
                val pat_eq =
wenzelm@50422
   669
                  (case try dest_singleton_list t of
wenzelm@50422
   670
                    SOME t' =>
wenzelm@50422
   671
                      Const (@{const_name HOL.eq}, rT --> rT --> @{typ bool}) $
wenzelm@50422
   672
                        Bound (length bound_vs) $ t'
wenzelm@50422
   673
                  | NONE =>
wenzelm@50422
   674
                      Const (@{const_name Set.member}, rT --> HOLogic.mk_setT rT --> @{typ bool}) $
wenzelm@50422
   675
                        Bound (length bound_vs) $ (mk_set rT $ t))
wenzelm@50422
   676
                val reverse_bounds = curry subst_bounds
wenzelm@50422
   677
                  ((map Bound ((length bound_vs - 1) downto 0)) @ [Bound (length bound_vs)])
wenzelm@50422
   678
                val eqs' = map reverse_bounds eqs
wenzelm@50422
   679
                val pat_eq' = reverse_bounds pat_eq
wenzelm@50422
   680
                val inner_t =
wenzelm@50422
   681
                  fold (fn (_, T) => fn t => HOLogic.exists_const T $ absdummy T t)
wenzelm@50422
   682
                    (rev bound_vs) (fold (curry HOLogic.mk_conj) eqs' pat_eq')
wenzelm@50422
   683
                val lhs = term_of redex
wenzelm@50422
   684
                val rhs = HOLogic.mk_Collect ("x", rT, inner_t)
wenzelm@50422
   685
                val rewrite_rule_t = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
wenzelm@50422
   686
              in
wenzelm@50422
   687
                SOME
wenzelm@50422
   688
                  ((Goal.prove ctxt [] [] rewrite_rule_t
wenzelm@50422
   689
                    (fn {context, ...} => tac context (rev Tis))) RS @{thm eq_reflection})
wenzelm@50422
   690
              end))
wenzelm@50422
   691
  in
wenzelm@50422
   692
    make_inner_eqs [] [] [] (dest_set (term_of redex))
wenzelm@50422
   693
  end
wenzelm@50422
   694
wenzelm@50422
   695
end
wenzelm@50422
   696
*}
bulwahn@41463
   697
bulwahn@41463
   698
simproc_setup list_to_set_comprehension ("set xs") = {* K List_to_Set_Comprehension.simproc *}
bulwahn@41463
   699
haftmann@46133
   700
code_datatype set coset
haftmann@46133
   701
haftmann@46133
   702
hide_const (open) coset
wenzelm@35115
   703
haftmann@49948
   704
haftmann@21061
   705
subsubsection {* @{const Nil} and @{const Cons} *}
haftmann@21061
   706
haftmann@21061
   707
lemma not_Cons_self [simp]:
haftmann@21061
   708
  "xs \<noteq> x # xs"
nipkow@13145
   709
by (induct xs) auto
wenzelm@13114
   710
wenzelm@41697
   711
lemma not_Cons_self2 [simp]:
wenzelm@41697
   712
  "x # xs \<noteq> xs"
wenzelm@41697
   713
by (rule not_Cons_self [symmetric])
wenzelm@13114
   714
wenzelm@13142
   715
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
nipkow@13145
   716
by (induct xs) auto
wenzelm@13114
   717
wenzelm@13142
   718
lemma length_induct:
haftmann@21061
   719
  "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs"
nipkow@17589
   720
by (rule measure_induct [of length]) iprover
wenzelm@13114
   721
haftmann@37289
   722
lemma list_nonempty_induct [consumes 1, case_names single cons]:
haftmann@37289
   723
  assumes "xs \<noteq> []"
haftmann@37289
   724
  assumes single: "\<And>x. P [x]"
haftmann@37289
   725
  assumes cons: "\<And>x xs. xs \<noteq> [] \<Longrightarrow> P xs \<Longrightarrow> P (x # xs)"
haftmann@37289
   726
  shows "P xs"
haftmann@37289
   727
using `xs \<noteq> []` proof (induct xs)
haftmann@37289
   728
  case Nil then show ?case by simp
haftmann@37289
   729
next
haftmann@37289
   730
  case (Cons x xs) show ?case proof (cases xs)
haftmann@37289
   731
    case Nil with single show ?thesis by simp
haftmann@37289
   732
  next
haftmann@37289
   733
    case Cons then have "xs \<noteq> []" by simp
haftmann@37289
   734
    moreover with Cons.hyps have "P xs" .
haftmann@37289
   735
    ultimately show ?thesis by (rule cons)
haftmann@37289
   736
  qed
haftmann@37289
   737
qed
haftmann@37289
   738
hoelzl@45714
   739
lemma inj_split_Cons: "inj_on (\<lambda>(xs, n). n#xs) X"
hoelzl@45714
   740
  by (auto intro!: inj_onI)
wenzelm@13114
   741
haftmann@49948
   742
haftmann@21061
   743
subsubsection {* @{const length} *}
wenzelm@13114
   744
wenzelm@13142
   745
text {*
haftmann@21061
   746
  Needs to come before @{text "@"} because of theorem @{text
haftmann@21061
   747
  append_eq_append_conv}.
wenzelm@13142
   748
*}
wenzelm@13114
   749
wenzelm@13142
   750
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
nipkow@13145
   751
by (induct xs) auto
wenzelm@13114
   752
wenzelm@13142
   753
lemma length_map [simp]: "length (map f xs) = length xs"
nipkow@13145
   754
by (induct xs) auto
wenzelm@13114
   755
wenzelm@13142
   756
lemma length_rev [simp]: "length (rev xs) = length xs"
nipkow@13145
   757
by (induct xs) auto
wenzelm@13114
   758
wenzelm@13142
   759
lemma length_tl [simp]: "length (tl xs) = length xs - 1"
nipkow@13145
   760
by (cases xs) auto
wenzelm@13114
   761
wenzelm@13142
   762
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
nipkow@13145
   763
by (induct xs) auto
wenzelm@13114
   764
wenzelm@13142
   765
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
nipkow@13145
   766
by (induct xs) auto
wenzelm@13114
   767
nipkow@23479
   768
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0"
nipkow@23479
   769
by auto
nipkow@23479
   770
wenzelm@13114
   771
lemma length_Suc_conv:
nipkow@13145
   772
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
nipkow@13145
   773
by (induct xs) auto
wenzelm@13142
   774
nipkow@14025
   775
lemma Suc_length_conv:
nipkow@14025
   776
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
paulson@14208
   777
apply (induct xs, simp, simp)
nipkow@14025
   778
apply blast
nipkow@14025
   779
done
nipkow@14025
   780
wenzelm@25221
   781
lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False"
wenzelm@25221
   782
  by (induct xs) auto
wenzelm@25221
   783
haftmann@26442
   784
lemma list_induct2 [consumes 1, case_names Nil Cons]:
haftmann@26442
   785
  "length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow>
haftmann@26442
   786
   (\<And>x xs y ys. length xs = length ys \<Longrightarrow> P xs ys \<Longrightarrow> P (x#xs) (y#ys))
haftmann@26442
   787
   \<Longrightarrow> P xs ys"
haftmann@26442
   788
proof (induct xs arbitrary: ys)
haftmann@26442
   789
  case Nil then show ?case by simp
haftmann@26442
   790
next
haftmann@26442
   791
  case (Cons x xs ys) then show ?case by (cases ys) simp_all
haftmann@26442
   792
qed
haftmann@26442
   793
haftmann@26442
   794
lemma list_induct3 [consumes 2, case_names Nil Cons]:
haftmann@26442
   795
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow>
haftmann@26442
   796
   (\<And>x xs y ys z zs. length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P xs ys zs \<Longrightarrow> P (x#xs) (y#ys) (z#zs))
haftmann@26442
   797
   \<Longrightarrow> P xs ys zs"
haftmann@26442
   798
proof (induct xs arbitrary: ys zs)
haftmann@26442
   799
  case Nil then show ?case by simp
haftmann@26442
   800
next
haftmann@26442
   801
  case (Cons x xs ys zs) then show ?case by (cases ys, simp_all)
haftmann@26442
   802
    (cases zs, simp_all)
haftmann@26442
   803
qed
wenzelm@13114
   804
kaliszyk@36154
   805
lemma list_induct4 [consumes 3, case_names Nil Cons]:
kaliszyk@36154
   806
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow>
kaliszyk@36154
   807
   P [] [] [] [] \<Longrightarrow> (\<And>x xs y ys z zs w ws. length xs = length ys \<Longrightarrow>
kaliszyk@36154
   808
   length ys = length zs \<Longrightarrow> length zs = length ws \<Longrightarrow> P xs ys zs ws \<Longrightarrow>
kaliszyk@36154
   809
   P (x#xs) (y#ys) (z#zs) (w#ws)) \<Longrightarrow> P xs ys zs ws"
kaliszyk@36154
   810
proof (induct xs arbitrary: ys zs ws)
kaliszyk@36154
   811
  case Nil then show ?case by simp
kaliszyk@36154
   812
next
kaliszyk@36154
   813
  case (Cons x xs ys zs ws) then show ?case by ((cases ys, simp_all), (cases zs,simp_all)) (cases ws, simp_all)
kaliszyk@36154
   814
qed
kaliszyk@36154
   815
krauss@22493
   816
lemma list_induct2': 
krauss@22493
   817
  "\<lbrakk> P [] [];
krauss@22493
   818
  \<And>x xs. P (x#xs) [];
krauss@22493
   819
  \<And>y ys. P [] (y#ys);
krauss@22493
   820
   \<And>x xs y ys. P xs ys  \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
krauss@22493
   821
 \<Longrightarrow> P xs ys"
krauss@22493
   822
by (induct xs arbitrary: ys) (case_tac x, auto)+
krauss@22493
   823
nipkow@22143
   824
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False"
nipkow@24349
   825
by (rule Eq_FalseI) auto
wenzelm@24037
   826
wenzelm@24037
   827
simproc_setup list_neq ("(xs::'a list) = ys") = {*
nipkow@22143
   828
(*
nipkow@22143
   829
Reduces xs=ys to False if xs and ys cannot be of the same length.
nipkow@22143
   830
This is the case if the atomic sublists of one are a submultiset
nipkow@22143
   831
of those of the other list and there are fewer Cons's in one than the other.
nipkow@22143
   832
*)
wenzelm@24037
   833
wenzelm@24037
   834
let
nipkow@22143
   835
huffman@29856
   836
fun len (Const(@{const_name Nil},_)) acc = acc
huffman@29856
   837
  | len (Const(@{const_name Cons},_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
huffman@29856
   838
  | len (Const(@{const_name append},_) $ xs $ ys) acc = len xs (len ys acc)
huffman@29856
   839
  | len (Const(@{const_name rev},_) $ xs) acc = len xs acc
huffman@29856
   840
  | len (Const(@{const_name map},_) $ _ $ xs) acc = len xs acc
nipkow@22143
   841
  | len t (ts,n) = (t::ts,n);
nipkow@22143
   842
wenzelm@24037
   843
fun list_neq _ ss ct =
nipkow@22143
   844
  let
wenzelm@24037
   845
    val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct;
nipkow@22143
   846
    val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0);
nipkow@22143
   847
    fun prove_neq() =
nipkow@22143
   848
      let
nipkow@22143
   849
        val Type(_,listT::_) = eqT;
haftmann@22994
   850
        val size = HOLogic.size_const listT;
nipkow@22143
   851
        val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs);
nipkow@22143
   852
        val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len);
nipkow@22143
   853
        val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len
haftmann@22633
   854
          (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));
haftmann@22633
   855
      in SOME (thm RS @{thm neq_if_length_neq}) end
nipkow@22143
   856
  in
wenzelm@23214
   857
    if m < n andalso submultiset (op aconv) (ls,rs) orelse
wenzelm@23214
   858
       n < m andalso submultiset (op aconv) (rs,ls)
nipkow@22143
   859
    then prove_neq() else NONE
nipkow@22143
   860
  end;
wenzelm@24037
   861
in list_neq end;
nipkow@22143
   862
*}
nipkow@22143
   863
nipkow@22143
   864
nipkow@15392
   865
subsubsection {* @{text "@"} -- append *}
wenzelm@13114
   866
wenzelm@13142
   867
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
nipkow@13145
   868
by (induct xs) auto
wenzelm@13114
   869
wenzelm@13142
   870
lemma append_Nil2 [simp]: "xs @ [] = xs"
nipkow@13145
   871
by (induct xs) auto
nipkow@3507
   872
wenzelm@13142
   873
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
nipkow@13145
   874
by (induct xs) auto
wenzelm@13114
   875
wenzelm@13142
   876
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
nipkow@13145
   877
by (induct xs) auto
wenzelm@13114
   878
wenzelm@13142
   879
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
nipkow@13145
   880
by (induct xs) auto
wenzelm@13114
   881
wenzelm@13142
   882
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
nipkow@13145
   883
by (induct xs) auto
wenzelm@13114
   884
blanchet@35828
   885
lemma append_eq_append_conv [simp, no_atp]:
nipkow@24526
   886
 "length xs = length ys \<or> length us = length vs
berghofe@13883
   887
 ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
nipkow@24526
   888
apply (induct xs arbitrary: ys)
paulson@14208
   889
 apply (case_tac ys, simp, force)
paulson@14208
   890
apply (case_tac ys, force, simp)
nipkow@13145
   891
done
wenzelm@13142
   892
nipkow@24526
   893
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) =
nipkow@24526
   894
  (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"
nipkow@24526
   895
apply (induct xs arbitrary: ys zs ts)
nipkow@44890
   896
 apply fastforce
nipkow@14495
   897
apply(case_tac zs)
nipkow@14495
   898
 apply simp
nipkow@44890
   899
apply fastforce
nipkow@14495
   900
done
nipkow@14495
   901
berghofe@34910
   902
lemma same_append_eq [iff, induct_simp]: "(xs @ ys = xs @ zs) = (ys = zs)"
nipkow@13145
   903
by simp
wenzelm@13142
   904
wenzelm@13142
   905
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
nipkow@13145
   906
by simp
wenzelm@13114
   907
berghofe@34910
   908
lemma append_same_eq [iff, induct_simp]: "(ys @ xs = zs @ xs) = (ys = zs)"
nipkow@13145
   909
by simp
wenzelm@13114
   910
wenzelm@13142
   911
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
nipkow@13145
   912
using append_same_eq [of _ _ "[]"] by auto
nipkow@3507
   913
wenzelm@13142
   914
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
nipkow@13145
   915
using append_same_eq [of "[]"] by auto
wenzelm@13114
   916
blanchet@35828
   917
lemma hd_Cons_tl [simp,no_atp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
nipkow@13145
   918
by (induct xs) auto
wenzelm@13114
   919
wenzelm@13142
   920
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
nipkow@13145
   921
by (induct xs) auto
wenzelm@13114
   922
wenzelm@13142
   923
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
nipkow@13145
   924
by (simp add: hd_append split: list.split)
wenzelm@13114
   925
wenzelm@13142
   926
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
nipkow@13145
   927
by (simp split: list.split)
wenzelm@13114
   928
wenzelm@13142
   929
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
nipkow@13145
   930
by (simp add: tl_append split: list.split)
wenzelm@13114
   931
wenzelm@13114
   932
nipkow@14300
   933
lemma Cons_eq_append_conv: "x#xs = ys@zs =
nipkow@14300
   934
 (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"
nipkow@14300
   935
by(cases ys) auto
nipkow@14300
   936
nipkow@15281
   937
lemma append_eq_Cons_conv: "(ys@zs = x#xs) =
nipkow@15281
   938
 (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"
nipkow@15281
   939
by(cases ys) auto
nipkow@15281
   940
nipkow@14300
   941
wenzelm@13142
   942
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114
   943
wenzelm@13114
   944
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
nipkow@13145
   945
by simp
wenzelm@13114
   946
wenzelm@13142
   947
lemma Cons_eq_appendI:
nipkow@13145
   948
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
nipkow@13145
   949
by (drule sym) simp
wenzelm@13114
   950
wenzelm@13142
   951
lemma append_eq_appendI:
nipkow@13145
   952
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
nipkow@13145
   953
by (drule sym) simp
wenzelm@13114
   954
wenzelm@13114
   955
wenzelm@13142
   956
text {*
nipkow@13145
   957
Simplification procedure for all list equalities.
nipkow@13145
   958
Currently only tries to rearrange @{text "@"} to see if
nipkow@13145
   959
- both lists end in a singleton list,
nipkow@13145
   960
- or both lists end in the same list.
wenzelm@13142
   961
*}
wenzelm@13142
   962
wenzelm@43594
   963
simproc_setup list_eq ("(xs::'a list) = ys")  = {*
wenzelm@13462
   964
  let
wenzelm@43594
   965
    fun last (cons as Const (@{const_name Cons}, _) $ _ $ xs) =
wenzelm@43594
   966
          (case xs of Const (@{const_name Nil}, _) => cons | _ => last xs)
wenzelm@43594
   967
      | last (Const(@{const_name append},_) $ _ $ ys) = last ys
wenzelm@43594
   968
      | last t = t;
wenzelm@43594
   969
    
wenzelm@43594
   970
    fun list1 (Const(@{const_name Cons},_) $ _ $ Const(@{const_name Nil},_)) = true
wenzelm@43594
   971
      | list1 _ = false;
wenzelm@43594
   972
    
wenzelm@43594
   973
    fun butlast ((cons as Const(@{const_name Cons},_) $ x) $ xs) =
wenzelm@43594
   974
          (case xs of Const (@{const_name Nil}, _) => xs | _ => cons $ butlast xs)
wenzelm@43594
   975
      | butlast ((app as Const (@{const_name append}, _) $ xs) $ ys) = app $ butlast ys
wenzelm@43594
   976
      | butlast xs = Const(@{const_name Nil}, fastype_of xs);
wenzelm@43594
   977
    
wenzelm@43594
   978
    val rearr_ss =
wenzelm@43594
   979
      HOL_basic_ss addsimps [@{thm append_assoc}, @{thm append_Nil}, @{thm append_Cons}];
wenzelm@43594
   980
    
wenzelm@43594
   981
    fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13462
   982
      let
wenzelm@43594
   983
        val lastl = last lhs and lastr = last rhs;
wenzelm@43594
   984
        fun rearr conv =
wenzelm@43594
   985
          let
wenzelm@43594
   986
            val lhs1 = butlast lhs and rhs1 = butlast rhs;
wenzelm@43594
   987
            val Type(_,listT::_) = eqT
wenzelm@43594
   988
            val appT = [listT,listT] ---> listT
wenzelm@43594
   989
            val app = Const(@{const_name append},appT)
wenzelm@43594
   990
            val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@43594
   991
            val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
wenzelm@43594
   992
            val thm = Goal.prove (Simplifier.the_context ss) [] [] eq
wenzelm@43594
   993
              (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1));
wenzelm@43594
   994
          in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;
wenzelm@43594
   995
      in
wenzelm@43594
   996
        if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
wenzelm@43594
   997
        else if lastl aconv lastr then rearr @{thm append_same_eq}
wenzelm@43594
   998
        else NONE
wenzelm@43594
   999
      end;
wenzelm@43594
  1000
  in fn _ => fn ss => fn ct => list_eq ss (term_of ct) end;
wenzelm@13114
  1001
*}
wenzelm@13114
  1002
wenzelm@13114
  1003
haftmann@49948
  1004
subsubsection {* @{const map} *}
wenzelm@13114
  1005
haftmann@40210
  1006
lemma hd_map:
haftmann@40210
  1007
  "xs \<noteq> [] \<Longrightarrow> hd (map f xs) = f (hd xs)"
haftmann@40210
  1008
  by (cases xs) simp_all
haftmann@40210
  1009
haftmann@40210
  1010
lemma map_tl:
haftmann@40210
  1011
  "map f (tl xs) = tl (map f xs)"
haftmann@40210
  1012
  by (cases xs) simp_all
haftmann@40210
  1013
wenzelm@13142
  1014
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
nipkow@13145
  1015
by (induct xs) simp_all
wenzelm@13114
  1016
wenzelm@13142
  1017
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
nipkow@13145
  1018
by (rule ext, induct_tac xs) auto
wenzelm@13114
  1019
wenzelm@13142
  1020
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
nipkow@13145
  1021
by (induct xs) auto
wenzelm@13114
  1022
hoelzl@33639
  1023
lemma map_map [simp]: "map f (map g xs) = map (f \<circ> g) xs"
hoelzl@33639
  1024
by (induct xs) auto
hoelzl@33639
  1025
nipkow@35208
  1026
lemma map_comp_map[simp]: "((map f) o (map g)) = map(f o g)"
nipkow@35208
  1027
apply(rule ext)
nipkow@35208
  1028
apply(simp)
nipkow@35208
  1029
done
nipkow@35208
  1030
wenzelm@13142
  1031
lemma rev_map: "rev (map f xs) = map f (rev xs)"
nipkow@13145
  1032
by (induct xs) auto
wenzelm@13114
  1033
nipkow@13737
  1034
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
nipkow@13737
  1035
by (induct xs) auto
nipkow@13737
  1036
krauss@44013
  1037
lemma map_cong [fundef_cong]:
haftmann@40122
  1038
  "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys"
haftmann@40122
  1039
  by simp
wenzelm@13114
  1040
wenzelm@13142
  1041
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
nipkow@13145
  1042
by (cases xs) auto
wenzelm@13114
  1043
wenzelm@13142
  1044
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
nipkow@13145
  1045
by (cases xs) auto
wenzelm@13114
  1046
paulson@18447
  1047
lemma map_eq_Cons_conv:
nipkow@14025
  1048
 "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
nipkow@13145
  1049
by (cases xs) auto
wenzelm@13114
  1050
paulson@18447
  1051
lemma Cons_eq_map_conv:
nipkow@14025
  1052
 "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
nipkow@14025
  1053
by (cases ys) auto
nipkow@14025
  1054
paulson@18447
  1055
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]
paulson@18447
  1056
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]
paulson@18447
  1057
declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!]
paulson@18447
  1058
nipkow@14111
  1059
lemma ex_map_conv:
nipkow@14111
  1060
  "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
paulson@18447
  1061
by(induct ys, auto simp add: Cons_eq_map_conv)
nipkow@14111
  1062
nipkow@15110
  1063
lemma map_eq_imp_length_eq:
paulson@35510
  1064
  assumes "map f xs = map g ys"
haftmann@26734
  1065
  shows "length xs = length ys"
haftmann@26734
  1066
using assms proof (induct ys arbitrary: xs)
haftmann@26734
  1067
  case Nil then show ?case by simp
haftmann@26734
  1068
next
haftmann@26734
  1069
  case (Cons y ys) then obtain z zs where xs: "xs = z # zs" by auto
paulson@35510
  1070
  from Cons xs have "map f zs = map g ys" by simp
haftmann@26734
  1071
  moreover with Cons have "length zs = length ys" by blast
haftmann@26734
  1072
  with xs show ?case by simp
haftmann@26734
  1073
qed
haftmann@26734
  1074
  
nipkow@15110
  1075
lemma map_inj_on:
nipkow@15110
  1076
 "[| map f xs = map f ys; inj_on f (set xs Un set ys) |]
nipkow@15110
  1077
  ==> xs = ys"
nipkow@15110
  1078
apply(frule map_eq_imp_length_eq)
nipkow@15110
  1079
apply(rotate_tac -1)
nipkow@15110
  1080
apply(induct rule:list_induct2)
nipkow@15110
  1081
 apply simp
nipkow@15110
  1082
apply(simp)
nipkow@15110
  1083
apply (blast intro:sym)
nipkow@15110
  1084
done
nipkow@15110
  1085
nipkow@15110
  1086
lemma inj_on_map_eq_map:
nipkow@15110
  1087
 "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@15110
  1088
by(blast dest:map_inj_on)
nipkow@15110
  1089
wenzelm@13114
  1090
lemma map_injective:
nipkow@24526
  1091
 "map f xs = map f ys ==> inj f ==> xs = ys"
nipkow@24526
  1092
by (induct ys arbitrary: xs) (auto dest!:injD)
wenzelm@13114
  1093
nipkow@14339
  1094
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@14339
  1095
by(blast dest:map_injective)
nipkow@14339
  1096
wenzelm@13114
  1097
lemma inj_mapI: "inj f ==> inj (map f)"
nipkow@17589
  1098
by (iprover dest: map_injective injD intro: inj_onI)
wenzelm@13114
  1099
wenzelm@13114
  1100
lemma inj_mapD: "inj (map f) ==> inj f"
paulson@14208
  1101
apply (unfold inj_on_def, clarify)
nipkow@13145
  1102
apply (erule_tac x = "[x]" in ballE)
paulson@14208
  1103
 apply (erule_tac x = "[y]" in ballE, simp, blast)
nipkow@13145
  1104
apply blast
nipkow@13145
  1105
done
wenzelm@13114
  1106
nipkow@14339
  1107
lemma inj_map[iff]: "inj (map f) = inj f"
nipkow@13145
  1108
by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114
  1109
nipkow@15303
  1110
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A"
nipkow@15303
  1111
apply(rule inj_onI)
nipkow@15303
  1112
apply(erule map_inj_on)
nipkow@15303
  1113
apply(blast intro:inj_onI dest:inj_onD)
nipkow@15303
  1114
done
nipkow@15303
  1115
kleing@14343
  1116
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"
kleing@14343
  1117
by (induct xs, auto)
wenzelm@13114
  1118
nipkow@14402
  1119
lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs"
nipkow@14402
  1120
by (induct xs) auto
nipkow@14402
  1121
nipkow@15110
  1122
lemma map_fst_zip[simp]:
nipkow@15110
  1123
  "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs"
nipkow@15110
  1124
by (induct rule:list_induct2, simp_all)
nipkow@15110
  1125
nipkow@15110
  1126
lemma map_snd_zip[simp]:
nipkow@15110
  1127
  "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys"
nipkow@15110
  1128
by (induct rule:list_induct2, simp_all)
nipkow@15110
  1129
haftmann@41505
  1130
enriched_type map: map
nipkow@47122
  1131
by (simp_all add: id_def)
nipkow@47122
  1132
haftmann@49948
  1133
declare map.id [simp]
haftmann@49948
  1134
haftmann@49948
  1135
haftmann@49948
  1136
subsubsection {* @{const rev} *}
wenzelm@13114
  1137
wenzelm@13142
  1138
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
nipkow@13145
  1139
by (induct xs) auto
wenzelm@13114
  1140
wenzelm@13142
  1141
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
nipkow@13145
  1142
by (induct xs) auto
wenzelm@13114
  1143
kleing@15870
  1144
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"
kleing@15870
  1145
by auto
kleing@15870
  1146
wenzelm@13142
  1147
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
nipkow@13145
  1148
by (induct xs) auto
wenzelm@13114
  1149
wenzelm@13142
  1150
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
nipkow@13145
  1151
by (induct xs) auto
wenzelm@13114
  1152
kleing@15870
  1153
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"
kleing@15870
  1154
by (cases xs) auto
kleing@15870
  1155
kleing@15870
  1156
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"
kleing@15870
  1157
by (cases xs) auto
kleing@15870
  1158
blanchet@46439
  1159
lemma rev_is_rev_conv [iff, no_atp]: "(rev xs = rev ys) = (xs = ys)"
haftmann@21061
  1160
apply (induct xs arbitrary: ys, force)
paulson@14208
  1161
apply (case_tac ys, simp, force)
nipkow@13145
  1162
done
wenzelm@13114
  1163
nipkow@15439
  1164
lemma inj_on_rev[iff]: "inj_on rev A"
nipkow@15439
  1165
by(simp add:inj_on_def)
nipkow@15439
  1166
wenzelm@13366
  1167
lemma rev_induct [case_names Nil snoc]:
wenzelm@13366
  1168
  "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
berghofe@15489
  1169
apply(simplesubst rev_rev_ident[symmetric])
nipkow@13145
  1170
apply(rule_tac list = "rev xs" in list.induct, simp_all)
nipkow@13145
  1171
done
wenzelm@13114
  1172
wenzelm@13366
  1173
lemma rev_exhaust [case_names Nil snoc]:
wenzelm@13366
  1174
  "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
nipkow@13145
  1175
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1176
wenzelm@13366
  1177
lemmas rev_cases = rev_exhaust
wenzelm@13366
  1178
nipkow@18423
  1179
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"
nipkow@18423
  1180
by(rule rev_cases[of xs]) auto
nipkow@18423
  1181
wenzelm@13114
  1182
haftmann@49948
  1183
subsubsection {* @{const set} *}
wenzelm@13114
  1184
nipkow@46698
  1185
declare set.simps [code_post]  --"pretty output"
nipkow@46698
  1186
wenzelm@13142
  1187
lemma finite_set [iff]: "finite (set xs)"
nipkow@13145
  1188
by (induct xs) auto
wenzelm@13114
  1189
wenzelm@13142
  1190
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
nipkow@13145
  1191
by (induct xs) auto
wenzelm@13114
  1192
nipkow@17830
  1193
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"
nipkow@17830
  1194
by(cases xs) auto
oheimb@14099
  1195
wenzelm@13142
  1196
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
nipkow@13145
  1197
by auto
wenzelm@13114
  1198
oheimb@14099
  1199
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
oheimb@14099
  1200
by auto
oheimb@14099
  1201
wenzelm@13142
  1202
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
nipkow@13145
  1203
by (induct xs) auto
wenzelm@13114
  1204
nipkow@15245
  1205
lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
nipkow@15245
  1206
by(induct xs) auto
nipkow@15245
  1207
wenzelm@13142
  1208
lemma set_rev [simp]: "set (rev xs) = set xs"
nipkow@13145
  1209
by (induct xs) auto
wenzelm@13114
  1210
wenzelm@13142
  1211
lemma set_map [simp]: "set (map f xs) = f`(set xs)"
nipkow@13145
  1212
by (induct xs) auto
wenzelm@13114
  1213
wenzelm@13142
  1214
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
nipkow@13145
  1215
by (induct xs) auto
wenzelm@13114
  1216
nipkow@32417
  1217
lemma set_upt [simp]: "set[i..<j] = {i..<j}"
bulwahn@41463
  1218
by (induct j) auto
wenzelm@13114
  1219
wenzelm@13142
  1220
wenzelm@25221
  1221
lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs"
nipkow@18049
  1222
proof (induct xs)
nipkow@26073
  1223
  case Nil thus ?case by simp
nipkow@26073
  1224
next
nipkow@26073
  1225
  case Cons thus ?case by (auto intro: Cons_eq_appendI)
nipkow@26073
  1226
qed
nipkow@26073
  1227
haftmann@26734
  1228
lemma in_set_conv_decomp: "x \<in> set xs \<longleftrightarrow> (\<exists>ys zs. xs = ys @ x # zs)"
haftmann@26734
  1229
  by (auto elim: split_list)
nipkow@26073
  1230
nipkow@26073
  1231
lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys"
nipkow@26073
  1232
proof (induct xs)
nipkow@26073
  1233
  case Nil thus ?case by simp
nipkow@18049
  1234
next
nipkow@18049
  1235
  case (Cons a xs)
nipkow@18049
  1236
  show ?case
nipkow@18049
  1237
  proof cases
nipkow@44890
  1238
    assume "x = a" thus ?case using Cons by fastforce
nipkow@18049
  1239
  next
nipkow@44890
  1240
    assume "x \<noteq> a" thus ?case using Cons by(fastforce intro!: Cons_eq_appendI)
nipkow@26073
  1241
  qed
nipkow@26073
  1242
qed
nipkow@26073
  1243
nipkow@26073
  1244
lemma in_set_conv_decomp_first:
nipkow@26073
  1245
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"
haftmann@26734
  1246
  by (auto dest!: split_list_first)
nipkow@26073
  1247
haftmann@40122
  1248
lemma split_list_last: "x \<in> set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs"
haftmann@40122
  1249
proof (induct xs rule: rev_induct)
nipkow@26073
  1250
  case Nil thus ?case by simp
nipkow@26073
  1251
next
nipkow@26073
  1252
  case (snoc a xs)
nipkow@26073
  1253
  show ?case
nipkow@26073
  1254
  proof cases
haftmann@40122
  1255
    assume "x = a" thus ?case using snoc by (metis List.set.simps(1) emptyE)
nipkow@26073
  1256
  next
nipkow@44890
  1257
    assume "x \<noteq> a" thus ?case using snoc by fastforce
nipkow@18049
  1258
  qed
nipkow@18049
  1259
qed
nipkow@18049
  1260
nipkow@26073
  1261
lemma in_set_conv_decomp_last:
nipkow@26073
  1262
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)"
haftmann@26734
  1263
  by (auto dest!: split_list_last)
nipkow@26073
  1264
nipkow@26073
  1265
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x"
nipkow@26073
  1266
proof (induct xs)
nipkow@26073
  1267
  case Nil thus ?case by simp
nipkow@26073
  1268
next
nipkow@26073
  1269
  case Cons thus ?case
nipkow@26073
  1270
    by(simp add:Bex_def)(metis append_Cons append.simps(1))
nipkow@26073
  1271
qed
nipkow@26073
  1272
nipkow@26073
  1273
lemma split_list_propE:
haftmann@26734
  1274
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1275
  obtains ys x zs where "xs = ys @ x # zs" and "P x"
haftmann@26734
  1276
using split_list_prop [OF assms] by blast
nipkow@26073
  1277
nipkow@26073
  1278
lemma split_list_first_prop:
nipkow@26073
  1279
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
  1280
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)"
haftmann@26734
  1281
proof (induct xs)
nipkow@26073
  1282
  case Nil thus ?case by simp
nipkow@26073
  1283
next
nipkow@26073
  1284
  case (Cons x xs)
nipkow@26073
  1285
  show ?case
nipkow@26073
  1286
  proof cases
nipkow@26073
  1287
    assume "P x"
haftmann@40122
  1288
    thus ?thesis by simp (metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append)
nipkow@26073
  1289
  next
nipkow@26073
  1290
    assume "\<not> P x"
nipkow@26073
  1291
    hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp
nipkow@26073
  1292
    thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD)
nipkow@26073
  1293
  qed
nipkow@26073
  1294
qed
nipkow@26073
  1295
nipkow@26073
  1296
lemma split_list_first_propE:
haftmann@26734
  1297
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1298
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y"
haftmann@26734
  1299
using split_list_first_prop [OF assms] by blast
nipkow@26073
  1300
nipkow@26073
  1301
lemma split_list_first_prop_iff:
nipkow@26073
  1302
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
  1303
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))"
haftmann@26734
  1304
by (rule, erule split_list_first_prop) auto
nipkow@26073
  1305
nipkow@26073
  1306
lemma split_list_last_prop:
nipkow@26073
  1307
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
  1308
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)"
nipkow@26073
  1309
proof(induct xs rule:rev_induct)
nipkow@26073
  1310
  case Nil thus ?case by simp
nipkow@26073
  1311
next
nipkow@26073
  1312
  case (snoc x xs)
nipkow@26073
  1313
  show ?case
nipkow@26073
  1314
  proof cases
nipkow@26073
  1315
    assume "P x" thus ?thesis by (metis emptyE set_empty)
nipkow@26073
  1316
  next
nipkow@26073
  1317
    assume "\<not> P x"
nipkow@26073
  1318
    hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp
nipkow@44890
  1319
    thus ?thesis using `\<not> P x` snoc(1) by fastforce
nipkow@26073
  1320
  qed
nipkow@26073
  1321
qed
nipkow@26073
  1322
nipkow@26073
  1323
lemma split_list_last_propE:
haftmann@26734
  1324
  assumes "\<exists>x \<in> set xs. P x"
haftmann@26734
  1325
  obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z"
haftmann@26734
  1326
using split_list_last_prop [OF assms] by blast
nipkow@26073
  1327
nipkow@26073
  1328
lemma split_list_last_prop_iff:
nipkow@26073
  1329
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
  1330
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))"
haftmann@26734
  1331
by (metis split_list_last_prop [where P=P] in_set_conv_decomp)
nipkow@26073
  1332
nipkow@26073
  1333
lemma finite_list: "finite A ==> EX xs. set xs = A"
haftmann@26734
  1334
  by (erule finite_induct)
haftmann@26734
  1335
    (auto simp add: set.simps(2) [symmetric] simp del: set.simps(2))
paulson@13508
  1336
kleing@14388
  1337
lemma card_length: "card (set xs) \<le> length xs"
kleing@14388
  1338
by (induct xs) (auto simp add: card_insert_if)
wenzelm@13114
  1339
haftmann@26442
  1340
lemma set_minus_filter_out:
haftmann@26442
  1341
  "set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)"
haftmann@26442
  1342
  by (induct xs) auto
paulson@15168
  1343
wenzelm@35115
  1344
haftmann@49948
  1345
subsubsection {* @{const filter} *}
wenzelm@13114
  1346
wenzelm@13142
  1347
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
nipkow@13145
  1348
by (induct xs) auto
wenzelm@13114
  1349
nipkow@15305
  1350
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"
nipkow@15305
  1351
by (induct xs) simp_all
nipkow@15305
  1352
wenzelm@13142
  1353
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
nipkow@13145
  1354
by (induct xs) auto
wenzelm@13114
  1355
nipkow@16998
  1356
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"
nipkow@16998
  1357
by (induct xs) (auto simp add: le_SucI)
nipkow@16998
  1358
nipkow@18423
  1359
lemma sum_length_filter_compl:
nipkow@18423
  1360
  "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"
nipkow@18423
  1361
by(induct xs) simp_all
nipkow@18423
  1362
wenzelm@13142
  1363
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
nipkow@13145
  1364
by (induct xs) auto
wenzelm@13114
  1365
wenzelm@13142
  1366
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
nipkow@13145
  1367
by (induct xs) auto
wenzelm@13114
  1368
nipkow@16998
  1369
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
nipkow@24349
  1370
by (induct xs) simp_all
nipkow@16998
  1371
nipkow@16998
  1372
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"
nipkow@16998
  1373
apply (induct xs)
nipkow@16998
  1374
 apply auto
nipkow@16998
  1375
apply(cut_tac P=P and xs=xs in length_filter_le)
nipkow@16998
  1376
apply simp
nipkow@16998
  1377
done
wenzelm@13114
  1378
nipkow@16965
  1379
lemma filter_map:
nipkow@16965
  1380
  "filter P (map f xs) = map f (filter (P o f) xs)"
nipkow@16965
  1381
by (induct xs) simp_all
nipkow@16965
  1382
nipkow@16965
  1383
lemma length_filter_map[simp]:
nipkow@16965
  1384
  "length (filter P (map f xs)) = length(filter (P o f) xs)"
nipkow@16965
  1385
by (simp add:filter_map)
nipkow@16965
  1386
wenzelm@13142
  1387
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
nipkow@13145
  1388
by auto
wenzelm@13114
  1389
nipkow@15246
  1390
lemma length_filter_less:
nipkow@15246
  1391
  "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"
nipkow@15246
  1392
proof (induct xs)
nipkow@15246
  1393
  case Nil thus ?case by simp
nipkow@15246
  1394
next
nipkow@15246
  1395
  case (Cons x xs) thus ?case
nipkow@15246
  1396
    apply (auto split:split_if_asm)
nipkow@15246
  1397
    using length_filter_le[of P xs] apply arith
nipkow@15246
  1398
  done
nipkow@15246
  1399
qed
wenzelm@13114
  1400
nipkow@15281
  1401
lemma length_filter_conv_card:
nipkow@15281
  1402
 "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
nipkow@15281
  1403
proof (induct xs)
nipkow@15281
  1404
  case Nil thus ?case by simp
nipkow@15281
  1405
next
nipkow@15281
  1406
  case (Cons x xs)
nipkow@15281
  1407
  let ?S = "{i. i < length xs & p(xs!i)}"
nipkow@15281
  1408
  have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)
nipkow@15281
  1409
  show ?case (is "?l = card ?S'")
nipkow@15281
  1410
  proof (cases)
nipkow@15281
  1411
    assume "p x"
nipkow@15281
  1412
    hence eq: "?S' = insert 0 (Suc ` ?S)"
nipkow@25162
  1413
      by(auto simp: image_def split:nat.split dest:gr0_implies_Suc)
nipkow@15281
  1414
    have "length (filter p (x # xs)) = Suc(card ?S)"
wenzelm@23388
  1415
      using Cons `p x` by simp
nipkow@15281
  1416
    also have "\<dots> = Suc(card(Suc ` ?S))" using fin
huffman@44921
  1417
      by (simp add: card_image)
nipkow@15281
  1418
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1419
      by (simp add:card_insert_if) (simp add:image_def)
nipkow@15281
  1420
    finally show ?thesis .
nipkow@15281
  1421
  next
nipkow@15281
  1422
    assume "\<not> p x"
nipkow@15281
  1423
    hence eq: "?S' = Suc ` ?S"
nipkow@25162
  1424
      by(auto simp add: image_def split:nat.split elim:lessE)
nipkow@15281
  1425
    have "length (filter p (x # xs)) = card ?S"
wenzelm@23388
  1426
      using Cons `\<not> p x` by simp
nipkow@15281
  1427
    also have "\<dots> = card(Suc ` ?S)" using fin
huffman@44921
  1428
      by (simp add: card_image)
nipkow@15281
  1429
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1430
      by (simp add:card_insert_if)
nipkow@15281
  1431
    finally show ?thesis .
nipkow@15281
  1432
  qed
nipkow@15281
  1433
qed
nipkow@15281
  1434
nipkow@17629
  1435
lemma Cons_eq_filterD:
nipkow@17629
  1436
 "x#xs = filter P ys \<Longrightarrow>
nipkow@17629
  1437
  \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"
wenzelm@19585
  1438
  (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")
nipkow@17629
  1439
proof(induct ys)
nipkow@17629
  1440
  case Nil thus ?case by simp
nipkow@17629
  1441
next
nipkow@17629
  1442
  case (Cons y ys)
nipkow@17629
  1443
  show ?case (is "\<exists>x. ?Q x")
nipkow@17629
  1444
  proof cases
nipkow@17629
  1445
    assume Py: "P y"
nipkow@17629
  1446
    show ?thesis
nipkow@17629
  1447
    proof cases
wenzelm@25221
  1448
      assume "x = y"
wenzelm@25221
  1449
      with Py Cons.prems have "?Q []" by simp
wenzelm@25221
  1450
      then show ?thesis ..
nipkow@17629
  1451
    next
wenzelm@25221
  1452
      assume "x \<noteq> y"
wenzelm@25221
  1453
      with Py Cons.prems show ?thesis by simp
nipkow@17629
  1454
    qed
nipkow@17629
  1455
  next
wenzelm@25221
  1456
    assume "\<not> P y"
nipkow@44890
  1457
    with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastforce
wenzelm@25221
  1458
    then have "?Q (y#us)" by simp
wenzelm@25221
  1459
    then show ?thesis ..
nipkow@17629
  1460
  qed
nipkow@17629
  1461
qed
nipkow@17629
  1462
nipkow@17629
  1463
lemma filter_eq_ConsD:
nipkow@17629
  1464
 "filter P ys = x#xs \<Longrightarrow>
nipkow@17629
  1465
  \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"
nipkow@17629
  1466
by(rule Cons_eq_filterD) simp
nipkow@17629
  1467
nipkow@17629
  1468
lemma filter_eq_Cons_iff:
nipkow@17629
  1469
 "(filter P ys = x#xs) =
nipkow@17629
  1470
  (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"
nipkow@17629
  1471
by(auto dest:filter_eq_ConsD)
nipkow@17629
  1472
nipkow@17629
  1473
lemma Cons_eq_filter_iff:
nipkow@17629
  1474
 "(x#xs = filter P ys) =
nipkow@17629
  1475
  (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"
nipkow@17629
  1476
by(auto dest:Cons_eq_filterD)
nipkow@17629
  1477
krauss@44013
  1478
lemma filter_cong[fundef_cong]:
nipkow@17501
  1479
 "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"
nipkow@17501
  1480
apply simp
nipkow@17501
  1481
apply(erule thin_rl)
nipkow@17501
  1482
by (induct ys) simp_all
nipkow@17501
  1483
nipkow@15281
  1484
haftmann@26442
  1485
subsubsection {* List partitioning *}
haftmann@26442
  1486
haftmann@26442
  1487
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
nipkow@50548
  1488
"partition P [] = ([], [])" |
nipkow@50548
  1489
"partition P (x # xs) = 
nipkow@50548
  1490
  (let (yes, no) = partition P xs
nipkow@50548
  1491
   in if P x then (x # yes, no) else (yes, x # no))"
haftmann@26442
  1492
haftmann@26442
  1493
lemma partition_filter1:
haftmann@26442
  1494
    "fst (partition P xs) = filter P xs"
haftmann@26442
  1495
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1496
haftmann@26442
  1497
lemma partition_filter2:
haftmann@26442
  1498
    "snd (partition P xs) = filter (Not o P) xs"
haftmann@26442
  1499
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1500
haftmann@26442
  1501
lemma partition_P:
haftmann@26442
  1502
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1503
  shows "(\<forall>p \<in> set yes.  P p) \<and> (\<forall>p  \<in> set no. \<not> P p)"
haftmann@26442
  1504
proof -
haftmann@26442
  1505
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1506
    by simp_all
haftmann@26442
  1507
  then show ?thesis by (simp_all add: partition_filter1 partition_filter2)
haftmann@26442
  1508
qed
haftmann@26442
  1509
haftmann@26442
  1510
lemma partition_set:
haftmann@26442
  1511
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1512
  shows "set yes \<union> set no = set xs"
haftmann@26442
  1513
proof -
haftmann@26442
  1514
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1515
    by simp_all
haftmann@26442
  1516
  then show ?thesis by (auto simp add: partition_filter1 partition_filter2) 
haftmann@26442
  1517
qed
haftmann@26442
  1518
hoelzl@33639
  1519
lemma partition_filter_conv[simp]:
hoelzl@33639
  1520
  "partition f xs = (filter f xs,filter (Not o f) xs)"
hoelzl@33639
  1521
unfolding partition_filter2[symmetric]
hoelzl@33639
  1522
unfolding partition_filter1[symmetric] by simp
hoelzl@33639
  1523
hoelzl@33639
  1524
declare partition.simps[simp del]
haftmann@26442
  1525
wenzelm@35115
  1526
haftmann@49948
  1527
subsubsection {* @{const concat} *}
wenzelm@13114
  1528
wenzelm@13142
  1529
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
nipkow@13145
  1530
by (induct xs) auto
wenzelm@13114
  1531
paulson@18447
  1532
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1533
by (induct xss) auto
wenzelm@13114
  1534
paulson@18447
  1535
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1536
by (induct xss) auto
wenzelm@13114
  1537
nipkow@24308
  1538
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)"
nipkow@13145
  1539
by (induct xs) auto
wenzelm@13114
  1540
nipkow@24476
  1541
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs"
nipkow@24349
  1542
by (induct xs) auto
nipkow@24349
  1543
wenzelm@13142
  1544
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
nipkow@13145
  1545
by (induct xs) auto
wenzelm@13114
  1546
wenzelm@13142
  1547
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
nipkow@13145
  1548
by (induct xs) auto
wenzelm@13114
  1549
wenzelm@13142
  1550
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
nipkow@13145
  1551
by (induct xs) auto
wenzelm@13114
  1552
bulwahn@40365
  1553
lemma concat_eq_concat_iff: "\<forall>(x, y) \<in> set (zip xs ys). length x = length y ==> length xs = length ys ==> (concat xs = concat ys) = (xs = ys)"
bulwahn@40365
  1554
proof (induct xs arbitrary: ys)
bulwahn@40365
  1555
  case (Cons x xs ys)
bulwahn@40365
  1556
  thus ?case by (cases ys) auto
bulwahn@40365
  1557
qed (auto)
bulwahn@40365
  1558
bulwahn@40365
  1559
lemma concat_injective: "concat xs = concat ys ==> length xs = length ys ==> \<forall>(x, y) \<in> set (zip xs ys). length x = length y ==> xs = ys"
bulwahn@40365
  1560
by (simp add: concat_eq_concat_iff)
bulwahn@40365
  1561
wenzelm@13114
  1562
haftmann@49948
  1563
subsubsection {* @{const nth} *}
wenzelm@13114
  1564
haftmann@29827
  1565
lemma nth_Cons_0 [simp, code]: "(x # xs)!0 = x"
nipkow@13145
  1566
by auto
wenzelm@13114
  1567
haftmann@29827
  1568
lemma nth_Cons_Suc [simp, code]: "(x # xs)!(Suc n) = xs!n"
nipkow@13145
  1569
by auto
wenzelm@13114
  1570
wenzelm@13142
  1571
declare nth.simps [simp del]
wenzelm@13114
  1572
nipkow@41842
  1573
lemma nth_Cons_pos[simp]: "0 < n \<Longrightarrow> (x#xs) ! n = xs ! (n - 1)"
nipkow@41842
  1574
by(auto simp: Nat.gr0_conv_Suc)
nipkow@41842
  1575
wenzelm@13114
  1576
lemma nth_append:
nipkow@24526
  1577
  "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
nipkow@24526
  1578
apply (induct xs arbitrary: n, simp)
paulson@14208
  1579
apply (case_tac n, auto)
nipkow@13145
  1580
done
wenzelm@13114
  1581
nipkow@14402
  1582
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"
wenzelm@25221
  1583
by (induct xs) auto
nipkow@14402
  1584
nipkow@14402
  1585
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"
wenzelm@25221
  1586
by (induct xs) auto
nipkow@14402
  1587
nipkow@24526
  1588
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
nipkow@24526
  1589
apply (induct xs arbitrary: n, simp)
paulson@14208
  1590
apply (case_tac n, auto)
nipkow@13145
  1591
done
wenzelm@13114
  1592
noschinl@45841
  1593
lemma nth_tl:
noschinl@45841
  1594
  assumes "n < length (tl x)" shows "tl x ! n = x ! Suc n"
noschinl@45841
  1595
using assms by (induct x) auto
noschinl@45841
  1596
nipkow@18423
  1597
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
nipkow@18423
  1598
by(cases xs) simp_all
nipkow@18423
  1599
nipkow@18049
  1600
nipkow@18049
  1601
lemma list_eq_iff_nth_eq:
nipkow@24526
  1602
 "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"
nipkow@24526
  1603
apply(induct xs arbitrary: ys)
paulson@24632
  1604
 apply force
nipkow@18049
  1605
apply(case_tac ys)
nipkow@18049
  1606
 apply simp
nipkow@18049
  1607
apply(simp add:nth_Cons split:nat.split)apply blast
nipkow@18049
  1608
done
nipkow@18049
  1609
wenzelm@13142
  1610
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
paulson@15251
  1611
apply (induct xs, simp, simp)
nipkow@13145
  1612
apply safe
paulson@24632
  1613
apply (metis nat_case_0 nth.simps zero_less_Suc)
paulson@24632
  1614
apply (metis less_Suc_eq_0_disj nth_Cons_Suc)
paulson@14208
  1615
apply (case_tac i, simp)
paulson@24632
  1616
apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff)
nipkow@13145
  1617
done
wenzelm@13114
  1618
nipkow@17501
  1619
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"
nipkow@17501
  1620
by(auto simp:set_conv_nth)
nipkow@17501
  1621
nipkow@13145
  1622
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
nipkow@13145
  1623
by (auto simp add: set_conv_nth)
wenzelm@13114
  1624
wenzelm@13142
  1625
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
nipkow@13145
  1626
by (auto simp add: set_conv_nth)
wenzelm@13114
  1627
wenzelm@13114
  1628
lemma all_nth_imp_all_set:
nipkow@13145
  1629
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
nipkow@13145
  1630
by (auto simp add: set_conv_nth)
wenzelm@13114
  1631
wenzelm@13114
  1632
lemma all_set_conv_all_nth:
nipkow@13145
  1633
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
nipkow@13145
  1634
by (auto simp add: set_conv_nth)
wenzelm@13114
  1635
kleing@25296
  1636
lemma rev_nth:
kleing@25296
  1637
  "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)"
kleing@25296
  1638
proof (induct xs arbitrary: n)
kleing@25296
  1639
  case Nil thus ?case by simp
kleing@25296
  1640
next
kleing@25296
  1641
  case (Cons x xs)
kleing@25296
  1642
  hence n: "n < Suc (length xs)" by simp
kleing@25296
  1643
  moreover
kleing@25296
  1644
  { assume "n < length xs"
kleing@25296
  1645
    with n obtain n' where "length xs - n = Suc n'"
kleing@25296
  1646
      by (cases "length xs - n", auto)
kleing@25296
  1647
    moreover
kleing@25296
  1648
    then have "length xs - Suc n = n'" by simp
kleing@25296
  1649
    ultimately
kleing@25296
  1650
    have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp
kleing@25296
  1651
  }
kleing@25296
  1652
  ultimately
kleing@25296
  1653
  show ?case by (clarsimp simp add: Cons nth_append)
kleing@25296
  1654
qed
wenzelm@13114
  1655
nipkow@31159
  1656
lemma Skolem_list_nth:
nipkow@31159
  1657
  "(ALL i<k. EX x. P i x) = (EX xs. size xs = k & (ALL i<k. P i (xs!i)))"
nipkow@31159
  1658
  (is "_ = (EX xs. ?P k xs)")
nipkow@31159
  1659
proof(induct k)
nipkow@31159
  1660
  case 0 show ?case by simp
nipkow@31159
  1661
next
nipkow@31159
  1662
  case (Suc k)
nipkow@31159
  1663
  show ?case (is "?L = ?R" is "_ = (EX xs. ?P' xs)")
nipkow@31159
  1664
  proof
nipkow@31159
  1665
    assume "?R" thus "?L" using Suc by auto
nipkow@31159
  1666
  next
nipkow@31159
  1667
    assume "?L"
nipkow@31159
  1668
    with Suc obtain x xs where "?P k xs & P k x" by (metis less_Suc_eq)
nipkow@31159
  1669
    hence "?P'(xs@[x])" by(simp add:nth_append less_Suc_eq)
nipkow@31159
  1670
    thus "?R" ..
nipkow@31159
  1671
  qed
nipkow@31159
  1672
qed
nipkow@31159
  1673
nipkow@31159
  1674
haftmann@49948
  1675
subsubsection {* @{const list_update} *}
wenzelm@13114
  1676
nipkow@24526
  1677
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs"
nipkow@24526
  1678
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1679
wenzelm@13114
  1680
lemma nth_list_update:
nipkow@24526
  1681
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
nipkow@24526
  1682
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1683
wenzelm@13142
  1684
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
nipkow@13145
  1685
by (simp add: nth_list_update)
wenzelm@13114
  1686
nipkow@24526
  1687
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j"
nipkow@24526
  1688
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1689
nipkow@24526
  1690
lemma list_update_id[simp]: "xs[i := xs!i] = xs"
nipkow@24526
  1691
by (induct xs arbitrary: i) (simp_all split:nat.splits)
nipkow@24526
  1692
nipkow@24526
  1693
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"
nipkow@24526
  1694
apply (induct xs arbitrary: i)
nipkow@17501
  1695
 apply simp
nipkow@17501
  1696
apply (case_tac i)
nipkow@17501
  1697
apply simp_all
nipkow@17501
  1698
done
nipkow@17501
  1699
nipkow@31077
  1700
lemma list_update_nonempty[simp]: "xs[k:=x] = [] \<longleftrightarrow> xs=[]"
nipkow@31077
  1701
by(metis length_0_conv length_list_update)
nipkow@31077
  1702
wenzelm@13114
  1703
lemma list_update_same_conv:
nipkow@24526
  1704
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
nipkow@24526
  1705
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1706
nipkow@14187
  1707
lemma list_update_append1:
nipkow@24526
  1708
 "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
nipkow@24526
  1709
apply (induct xs arbitrary: i, simp)
nipkow@14187
  1710
apply(simp split:nat.split)
nipkow@14187
  1711
done
nipkow@14187
  1712
kleing@15868
  1713
lemma list_update_append:
nipkow@24526
  1714
  "(xs @ ys) [n:= x] = 
kleing@15868
  1715
  (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"
nipkow@24526
  1716
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1717
nipkow@14402
  1718
lemma list_update_length [simp]:
nipkow@14402
  1719
 "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"
nipkow@14402
  1720
by (induct xs, auto)
nipkow@14402
  1721
nipkow@31264
  1722
lemma map_update: "map f (xs[k:= y]) = (map f xs)[k := f y]"
nipkow@31264
  1723
by(induct xs arbitrary: k)(auto split:nat.splits)
nipkow@31264
  1724
nipkow@31264
  1725
lemma rev_update:
nipkow@31264
  1726
  "k < length xs \<Longrightarrow> rev (xs[k:= y]) = (rev xs)[length xs - k - 1 := y]"
nipkow@31264
  1727
by (induct xs arbitrary: k) (auto simp: list_update_append split:nat.splits)
nipkow@31264
  1728
wenzelm@13114
  1729
lemma update_zip:
nipkow@31080
  1730
  "(zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
nipkow@24526
  1731
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split)
nipkow@24526
  1732
nipkow@24526
  1733
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)"
nipkow@24526
  1734
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1735
wenzelm@13114
  1736
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
nipkow@13145
  1737
by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
  1738
nipkow@24526
  1739
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"
nipkow@24526
  1740
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1741
nipkow@31077
  1742
lemma list_update_overwrite[simp]:
haftmann@24796
  1743
  "xs [i := x, i := y] = xs [i := y]"
nipkow@31077
  1744
apply (induct xs arbitrary: i) apply simp
nipkow@31077
  1745
apply (case_tac i, simp_all)
haftmann@24796
  1746
done
haftmann@24796
  1747
haftmann@24796
  1748
lemma list_update_swap:
haftmann@24796
  1749
  "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]"
haftmann@24796
  1750
apply (induct xs arbitrary: i i')
haftmann@24796
  1751
apply simp
haftmann@24796
  1752
apply (case_tac i, case_tac i')
haftmann@24796
  1753
apply auto
haftmann@24796
  1754
apply (case_tac i')
haftmann@24796
  1755
apply auto
haftmann@24796
  1756
done
haftmann@24796
  1757
haftmann@29827
  1758
lemma list_update_code [code]:
haftmann@29827
  1759
  "[][i := y] = []"
haftmann@29827
  1760
  "(x # xs)[0 := y] = y # xs"
haftmann@29827
  1761
  "(x # xs)[Suc i := y] = x # xs[i := y]"
haftmann@29827
  1762
  by simp_all
haftmann@29827
  1763
wenzelm@13114
  1764
haftmann@49948
  1765
subsubsection {* @{const last} and @{const butlast} *}
wenzelm@13114
  1766
wenzelm@13142
  1767
lemma last_snoc [simp]: "last (xs @ [x]) = x"
nipkow@13145
  1768
by (induct xs) auto
wenzelm@13114
  1769
wenzelm@13142
  1770
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
nipkow@13145
  1771
by (induct xs) auto
wenzelm@13114
  1772
nipkow@14302
  1773
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
huffman@44921
  1774
  by simp
nipkow@14302
  1775
nipkow@14302
  1776
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
huffman@44921
  1777
  by simp
nipkow@14302
  1778
nipkow@14302
  1779
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
nipkow@14302
  1780
by (induct xs) (auto)
nipkow@14302
  1781
nipkow@14302
  1782
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
nipkow@14302
  1783
by(simp add:last_append)
nipkow@14302
  1784
nipkow@14302
  1785
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
nipkow@14302
  1786
by(simp add:last_append)
nipkow@14302
  1787
noschinl@45841
  1788
lemma last_tl: "xs = [] \<or> tl xs \<noteq> [] \<Longrightarrow>last (tl xs) = last xs"
noschinl@45841
  1789
by (induct xs) simp_all
noschinl@45841
  1790
noschinl@45841
  1791
lemma butlast_tl: "butlast (tl xs) = tl (butlast xs)"
noschinl@45841
  1792
by (induct xs) simp_all
noschinl@45841
  1793
nipkow@17762
  1794
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
nipkow@17762
  1795
by(rule rev_exhaust[of xs]) simp_all
nipkow@17762
  1796
nipkow@17762
  1797
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
nipkow@17762
  1798
by(cases xs) simp_all
nipkow@17762
  1799
nipkow@17765
  1800
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"
nipkow@17765
  1801
by (induct as) auto
nipkow@17762
  1802
wenzelm@13142
  1803
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
nipkow@13145
  1804
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1805
wenzelm@13114
  1806
lemma butlast_append:
nipkow@24526
  1807
  "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
nipkow@24526
  1808
by (induct xs arbitrary: ys) auto
wenzelm@13114
  1809
wenzelm@13142
  1810
lemma append_butlast_last_id [simp]:
nipkow@13145
  1811
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
nipkow@13145
  1812
by (induct xs) auto
wenzelm@13114
  1813
wenzelm@13142
  1814
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
nipkow@13145
  1815
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1816
wenzelm@13114
  1817
lemma in_set_butlast_appendI:
nipkow@13145
  1818
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
nipkow@13145
  1819
by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
  1820
nipkow@24526
  1821
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
nipkow@24526
  1822
apply (induct xs arbitrary: n)
nipkow@17501
  1823
 apply simp
nipkow@17501
  1824
apply (auto split:nat.split)
nipkow@17501
  1825
done
nipkow@17501
  1826
noschinl@45841
  1827
lemma nth_butlast:
noschinl@45841
  1828
  assumes "n < length (butlast xs)" shows "butlast xs ! n = xs ! n"
noschinl@45841
  1829
proof (cases xs)
noschinl@45841
  1830
  case (Cons y ys)
noschinl@45841
  1831
  moreover from assms have "butlast xs ! n = (butlast xs @ [last xs]) ! n"
noschinl@45841
  1832
    by (simp add: nth_append)
noschinl@45841
  1833
  ultimately show ?thesis using append_butlast_last_id by simp
noschinl@45841
  1834
qed simp
noschinl@45841
  1835
huffman@30128
  1836
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
nipkow@17589
  1837
by(induct xs)(auto simp:neq_Nil_conv)
nipkow@17589
  1838
huffman@30128
  1839
lemma butlast_conv_take: "butlast xs = take (length xs - 1) xs"
huffman@26584
  1840
by (induct xs, simp, case_tac xs, simp_all)
huffman@26584
  1841
nipkow@31077
  1842
lemma last_list_update:
nipkow@31077
  1843
  "xs \<noteq> [] \<Longrightarrow> last(xs[k:=x]) = (if k = size xs - 1 then x else last xs)"
nipkow@31077
  1844
by (auto simp: last_conv_nth)
nipkow@31077
  1845
nipkow@31077
  1846
lemma butlast_list_update:
nipkow@31077
  1847
  "butlast(xs[k:=x]) =
nipkow@31077
  1848
 (if k = size xs - 1 then butlast xs else (butlast xs)[k:=x])"
nipkow@31077
  1849
apply(cases xs rule:rev_cases)
nipkow@31077
  1850
apply simp
nipkow@31077
  1851
apply(simp add:list_update_append split:nat.splits)
nipkow@31077
  1852
done
nipkow@31077
  1853
haftmann@36851
  1854
lemma last_map:
haftmann@36851
  1855
  "xs \<noteq> [] \<Longrightarrow> last (map f xs) = f (last xs)"
haftmann@36851
  1856
  by (cases xs rule: rev_cases) simp_all
haftmann@36851
  1857
haftmann@36851
  1858
lemma map_butlast:
haftmann@36851
  1859
  "map f (butlast xs) = butlast (map f xs)"
haftmann@36851
  1860
  by (induct xs) simp_all
haftmann@36851
  1861
nipkow@40230
  1862
lemma snoc_eq_iff_butlast:
nipkow@40230
  1863
  "xs @ [x] = ys \<longleftrightarrow> (ys \<noteq> [] & butlast ys = xs & last ys = x)"
nipkow@40230
  1864
by (metis append_butlast_last_id append_is_Nil_conv butlast_snoc last_snoc not_Cons_self)
nipkow@40230
  1865
haftmann@24796
  1866
haftmann@49948
  1867
subsubsection {* @{const take} and @{const drop} *}
wenzelm@13114
  1868
wenzelm@13142
  1869
lemma take_0 [simp]: "take 0 xs = []"
nipkow@13145
  1870
by (induct xs) auto
wenzelm@13114
  1871
wenzelm@13142
  1872
lemma drop_0 [simp]: "drop 0 xs = xs"
nipkow@13145
  1873
by (induct xs) auto
wenzelm@13114
  1874
wenzelm@13142
  1875
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
nipkow@13145
  1876
by simp
wenzelm@13114
  1877
wenzelm@13142
  1878
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
nipkow@13145
  1879
by simp
wenzelm@13114
  1880
wenzelm@13142
  1881
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
  1882
huffman@30128
  1883
lemma take_1_Cons [simp]: "take 1 (x # xs) = [x]"
huffman@30128
  1884
  unfolding One_nat_def by simp
huffman@30128
  1885
huffman@30128
  1886
lemma drop_1_Cons [simp]: "drop 1 (x # xs) = xs"
huffman@30128
  1887
  unfolding One_nat_def by simp
huffman@30128
  1888
nipkow@15110
  1889
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"
nipkow@15110
  1890
by(clarsimp simp add:neq_Nil_conv)
nipkow@15110
  1891
nipkow@14187
  1892
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
nipkow@14187
  1893
by(cases xs, simp_all)
nipkow@14187
  1894
huffman@26584
  1895
lemma take_tl: "take n (tl xs) = tl (take (Suc n) xs)"
huffman@26584
  1896
by (induct xs arbitrary: n) simp_all
huffman@26584
  1897
nipkow@24526
  1898
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)"
nipkow@24526
  1899
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split)
nipkow@24526
  1900
huffman@26584
  1901
lemma tl_take: "tl (take n xs) = take (n - 1) (tl xs)"
huffman@26584
  1902
by (cases n, simp, cases xs, auto)
huffman@26584
  1903
huffman@26584
  1904
lemma tl_drop: "tl (drop n xs) = drop n (tl xs)"
huffman@26584
  1905
by (simp only: drop_tl)
huffman@26584
  1906
nipkow@24526
  1907
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y"
nipkow@24526
  1908
apply (induct xs arbitrary: n, simp)
nipkow@14187
  1909
apply(simp add:drop_Cons nth_Cons split:nat.splits)
nipkow@14187
  1910
done
nipkow@14187
  1911
nipkow@13913
  1912
lemma take_Suc_conv_app_nth:
nipkow@24526
  1913
  "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
nipkow@24526
  1914
apply (induct xs arbitrary: i, simp)
paulson@14208
  1915
apply (case_tac i, auto)
nipkow@13913
  1916
done
nipkow@13913
  1917
mehta@14591
  1918
lemma drop_Suc_conv_tl:
nipkow@24526
  1919
  "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
nipkow@24526
  1920
apply (induct xs arbitrary: i, simp)
mehta@14591
  1921
apply (case_tac i, auto)
mehta@14591
  1922
done
mehta@14591
  1923
nipkow@24526
  1924
lemma length_take [simp]: "length (take n xs) = min (length xs) n"
nipkow@24526
  1925
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1926
nipkow@24526
  1927
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"
nipkow@24526
  1928
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1929
nipkow@24526
  1930
lemma take_all [simp]: "length xs <= n ==> take n xs = xs"
nipkow@24526
  1931
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1932
nipkow@24526
  1933
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []"
nipkow@24526
  1934
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1935
wenzelm@13142
  1936
lemma take_append [simp]:
nipkow@24526
  1937
  "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
nipkow@24526
  1938
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1939
wenzelm@13142
  1940
lemma drop_append [simp]:
nipkow@24526
  1941
  "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
nipkow@24526
  1942
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1943
nipkow@24526
  1944
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"
nipkow@24526
  1945
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1946
apply (case_tac xs, auto)
nipkow@15236
  1947
apply (case_tac n, auto)
nipkow@13145
  1948
done
wenzelm@13114
  1949
nipkow@24526
  1950
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"
nipkow@24526
  1951
apply (induct m arbitrary: xs, auto)
paulson@14208
  1952
apply (case_tac xs, auto)
nipkow@13145
  1953
done
wenzelm@13114
  1954
nipkow@24526
  1955
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"
nipkow@24526
  1956
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1957
apply (case_tac xs, auto)
nipkow@13145
  1958
done
wenzelm@13114
  1959
nipkow@24526
  1960
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"
nipkow@24526
  1961
apply(induct xs arbitrary: m n)
nipkow@14802
  1962
 apply simp
nipkow@14802
  1963
apply(simp add: take_Cons drop_Cons split:nat.split)
nipkow@14802
  1964
done
nipkow@14802
  1965
nipkow@24526
  1966
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"
nipkow@24526
  1967
apply (induct n arbitrary: xs, auto)
paulson@14208
  1968
apply (case_tac xs, auto)
nipkow@13145
  1969
done
wenzelm@13114
  1970
nipkow@24526
  1971
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])"
nipkow@24526
  1972
apply(induct xs arbitrary: n)
nipkow@15110
  1973
 apply simp
nipkow@15110
  1974
apply(simp add:take_Cons split:nat.split)
nipkow@15110
  1975
done
nipkow@15110
  1976
nipkow@24526
  1977
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)"
nipkow@24526
  1978
apply(induct xs arbitrary: n)
nipkow@15110
  1979
apply simp
nipkow@15110
  1980
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1981
done
nipkow@15110
  1982
nipkow@24526
  1983
lemma take_map: "take n (map f xs) = map f (take n xs)"
nipkow@24526
  1984
apply (induct n arbitrary: xs, auto)
paulson@14208
  1985
apply (case_tac xs, auto)
nipkow@13145
  1986
done
wenzelm@13114
  1987
nipkow@24526
  1988
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"
nipkow@24526
  1989
apply (induct n arbitrary: xs, auto)
paulson@14208
  1990
apply (case_tac xs, auto)
nipkow@13145
  1991
done
wenzelm@13114
  1992
nipkow@24526
  1993
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@24526
  1994
apply (induct xs arbitrary: i, auto)
paulson@14208
  1995
apply (case_tac i, auto)
nipkow@13145
  1996
done
wenzelm@13114
  1997
nipkow@24526
  1998
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@24526
  1999
apply (induct xs arbitrary: i, auto)
paulson@14208
  2000
apply (case_tac i, auto)
nipkow@13145
  2001
done
wenzelm@13114
  2002
nipkow@24526
  2003
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i"
nipkow@24526
  2004
apply (induct xs arbitrary: i n, auto)
paulson@14208
  2005
apply (case_tac n, blast)
paulson@14208
  2006
apply (case_tac i, auto)
nipkow@13145
  2007
done
wenzelm@13114
  2008
wenzelm@13142
  2009
lemma nth_drop [simp]:
nipkow@24526
  2010
  "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
nipkow@24526
  2011
apply (induct n arbitrary: xs i, auto)
paulson@14208
  2012
apply (case_tac xs, auto)
nipkow@13145
  2013
done
nipkow@3507
  2014
huffman@26584
  2015
lemma butlast_take:
huffman@30128
  2016
  "n <= length xs ==> butlast (take n xs) = take (n - 1) xs"
huffman@26584
  2017
by (simp add: butlast_conv_take min_max.inf_absorb1 min_max.inf_absorb2)
huffman@26584
  2018
huffman@26584
  2019
lemma butlast_drop: "butlast (drop n xs) = drop n (butlast xs)"
huffman@30128
  2020
by (simp add: butlast_conv_take drop_take add_ac)
huffman@26584
  2021
huffman@26584
  2022
lemma take_butlast: "n < length xs ==> take n (butlast xs) = take n xs"
huffman@26584
  2023
by (simp add: butlast_conv_take min_max.inf_absorb1)
huffman@26584
  2024
huffman@26584
  2025
lemma drop_butlast: "drop n (butlast xs) = butlast (drop n xs)"
huffman@30128
  2026
by (simp add: butlast_conv_take drop_take add_ac)
huffman@26584
  2027
bulwahn@46500
  2028
lemma hd_drop_conv_nth: "n < length xs \<Longrightarrow> hd(drop n xs) = xs!n"
nipkow@18423
  2029
by(simp add: hd_conv_nth)
nipkow@18423
  2030
nipkow@35248
  2031
lemma set_take_subset_set_take:
nipkow@35248
  2032
  "m <= n \<Longrightarrow> set(take m xs) <= set(take n xs)"
bulwahn@41463
  2033
apply (induct xs arbitrary: m n)
bulwahn@41463
  2034
apply simp
bulwahn@41463
  2035
apply (case_tac n)
bulwahn@41463
  2036
apply (auto simp: take_Cons)
bulwahn@41463
  2037
done
nipkow@35248
  2038
nipkow@24526
  2039
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"
nipkow@24526
  2040
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)
nipkow@24526
  2041
nipkow@24526
  2042
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"
nipkow@24526
  2043
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)
nipkow@14025
  2044
nipkow@35248
  2045
lemma set_drop_subset_set_drop:
nipkow@35248
  2046
  "m >= n \<Longrightarrow> set(drop m xs) <= set(drop n xs)"
nipkow@35248
  2047
apply(induct xs arbitrary: m n)
nipkow@35248
  2048
apply(auto simp:drop_Cons split:nat.split)
nipkow@35248
  2049
apply (metis set_drop_subset subset_iff)
nipkow@35248
  2050
done
nipkow@35248
  2051
nipkow@14187
  2052
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  2053
using set_take_subset by fast
nipkow@14187
  2054
nipkow@14187
  2055
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  2056
using set_drop_subset by fast
nipkow@14187
  2057
wenzelm@13114
  2058
lemma append_eq_conv_conj:
nipkow@24526
  2059
  "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
nipkow@24526
  2060
apply (induct xs arbitrary: zs, simp, clarsimp)
paulson@14208
  2061
apply (case_tac zs, auto)
nipkow@13145
  2062
done
wenzelm@13142
  2063
nipkow@24526
  2064
lemma take_add: 
noschinl@42713
  2065
  "take (i+j) xs = take i xs @ take j (drop i xs)"
nipkow@24526
  2066
apply (induct xs arbitrary: i, auto) 
nipkow@24526
  2067
apply (case_tac i, simp_all)
paulson@14050
  2068
done
paulson@14050
  2069
nipkow@14300
  2070
lemma append_eq_append_conv_if:
nipkow@24526
  2071
 "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
nipkow@14300
  2072
  (if size xs\<^isub>1 \<le> size ys\<^isub>1
nipkow@14300
  2073
   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
nipkow@14300
  2074
   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)"
nipkow@24526
  2075
apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1)
nipkow@14300
  2076
 apply simp
nipkow@14300
  2077
apply(case_tac ys\<^isub>1)
nipkow@14300
  2078
apply simp_all
nipkow@14300
  2079
done
nipkow@14300
  2080
nipkow@15110
  2081
lemma take_hd_drop:
huffman@30079
  2082
  "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (Suc n) xs"
nipkow@24526
  2083
apply(induct xs arbitrary: n)
nipkow@15110
  2084
apply simp
nipkow@15110
  2085
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  2086
done
nipkow@15110
  2087
nipkow@17501
  2088
lemma id_take_nth_drop:
nipkow@17501
  2089
 "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 
nipkow@17501
  2090
proof -
nipkow@17501
  2091
  assume si: "i < length xs"
nipkow@17501
  2092
  hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
nipkow@17501
  2093
  moreover
nipkow@17501
  2094
  from si have "take (Suc i) xs = take i xs @ [xs!i]"
nipkow@17501
  2095
    apply (rule_tac take_Suc_conv_app_nth) by arith
nipkow@17501
  2096
  ultimately show ?thesis by auto
nipkow@17501
  2097
qed
nipkow@17501
  2098
  
nipkow@17501
  2099
lemma upd_conv_take_nth_drop:
nipkow@17501
  2100
 "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  2101
proof -
nipkow@17501
  2102
  assume i: "i < length xs"
nipkow@17501
  2103
  have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
nipkow@17501
  2104
    by(rule arg_cong[OF id_take_nth_drop[OF i]])
nipkow@17501
  2105
  also have "\<dots> = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  2106
    using i by (simp add: list_update_append)
nipkow@17501
  2107
  finally show ?thesis .
nipkow@17501
  2108
qed
nipkow@17501
  2109
haftmann@24796
  2110
lemma nth_drop':
haftmann@24796
  2111
  "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs"
haftmann@24796
  2112
apply (induct i arbitrary: xs)
haftmann@24796
  2113
apply (simp add: neq_Nil_conv)
haftmann@24796
  2114
apply (erule exE)+
haftmann@24796
  2115
apply simp
haftmann@24796
  2116
apply (case_tac xs)
haftmann@24796
  2117
apply simp_all
haftmann@24796
  2118
done
haftmann@24796
  2119
wenzelm@13114
  2120
haftmann@49948
  2121
subsubsection {* @{const takeWhile} and @{const dropWhile} *}
wenzelm@13114
  2122
hoelzl@33639
  2123
lemma length_takeWhile_le: "length (takeWhile P xs) \<le> length xs"
hoelzl@33639
  2124
  by (induct xs) auto
hoelzl@33639
  2125
wenzelm@13142
  2126
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
nipkow@13145
  2127
by (induct xs) auto
wenzelm@13114
  2128
wenzelm@13142
  2129
lemma takeWhile_append1 [simp]:
nipkow@13145
  2130
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
nipkow@13145
  2131
by (induct xs) auto
wenzelm@13114
  2132
wenzelm@13142
  2133
lemma takeWhile_append2 [simp]:
nipkow@13145
  2134
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
nipkow@13145
  2135
by (induct xs) auto
wenzelm@13114
  2136
wenzelm@13142
  2137
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
nipkow@13145
  2138
by (induct xs) auto
wenzelm@13114
  2139
hoelzl@33639
  2140
lemma takeWhile_nth: "j < length (takeWhile P xs) \<Longrightarrow> takeWhile P xs ! j = xs ! j"
hoelzl@33639
  2141
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto
hoelzl@33639
  2142
hoelzl@33639
  2143
lemma dropWhile_nth: "j < length (dropWhile P xs) \<Longrightarrow> dropWhile P xs ! j = xs ! (j + length (takeWhile P xs))"
hoelzl@33639
  2144
apply (subst (3) takeWhile_dropWhile_id[symmetric]) unfolding nth_append by auto
hoelzl@33639
  2145
hoelzl@33639
  2146
lemma length_dropWhile_le: "length (dropWhile P xs) \<le> length xs"
hoelzl@33639
  2147
by (induct xs) auto
hoelzl@33639
  2148
wenzelm@13142
  2149
lemma dropWhile_append1 [simp]:
nipkow@13145
  2150
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
nipkow@13145
  2151
by (induct xs) auto
wenzelm@13114
  2152
wenzelm@13142
  2153
lemma dropWhile_append2 [simp]:
nipkow@13145
  2154
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
nipkow@13145
  2155
by (induct xs) auto
wenzelm@13114
  2156
noschinl@45841
  2157
lemma dropWhile_append3:
noschinl@45841
  2158
  "\<not> P y \<Longrightarrow>dropWhile P (xs @ y # ys) = dropWhile P xs @ y # ys"
noschinl@45841
  2159
by (induct xs) auto
noschinl@45841
  2160
noschinl@45841
  2161
lemma dropWhile_last:
noschinl@45841
  2162
  "x \<in> set xs \<Longrightarrow> \<not> P x \<Longrightarrow> last (dropWhile P xs) = last xs"
noschinl@45841
  2163
by (auto simp add: dropWhile_append3 in_set_conv_decomp)
noschinl@45841
  2164
noschinl@45841
  2165
lemma set_dropWhileD: "x \<in> set (dropWhile P xs) \<Longrightarrow> x \<in> set xs"
noschinl@45841
  2166
by (induct xs) (auto split: split_if_asm)
noschinl@45841
  2167
krauss@23971
  2168
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
nipkow@13145
  2169
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  2170
nipkow@13913
  2171
lemma takeWhile_eq_all_conv[simp]:
nipkow@13913
  2172
 "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  2173
by(induct xs, auto)
nipkow@13913
  2174
nipkow@13913
  2175
lemma dropWhile_eq_Nil_conv[simp]:
nipkow@13913
  2176
 "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  2177
by(induct xs, auto)
nipkow@13913
  2178
nipkow@13913
  2179
lemma dropWhile_eq_Cons_conv:
nipkow@13913
  2180
 "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
nipkow@13913
  2181
by(induct xs, auto)
nipkow@13913
  2182
nipkow@31077
  2183
lemma distinct_takeWhile[simp]: "distinct xs ==> distinct (takeWhile P xs)"
nipkow@31077
  2184
by (induct xs) (auto dest: set_takeWhileD)
nipkow@31077
  2185
nipkow@31077
  2186
lemma distinct_dropWhile[simp]: "distinct xs ==> distinct (dropWhile P xs)"
nipkow@31077
  2187
by (induct xs) auto
nipkow@31077
  2188
hoelzl@33639
  2189
lemma takeWhile_map: "takeWhile P (map f xs) = map f (takeWhile (P \<circ> f) xs)"
hoelzl@33639
  2190
by (induct xs) auto
hoelzl@33639
  2191
hoelzl@33639
  2192
lemma dropWhile_map: "dropWhile P (map f xs) = map f (dropWhile (P \<circ> f) xs)"
hoelzl@33639
  2193
by (induct xs) auto
hoelzl@33639
  2194
hoelzl@33639
  2195
lemma takeWhile_eq_take: "takeWhile P xs = take (length (takeWhile P xs)) xs"
hoelzl@33639
  2196
by (induct xs) auto
hoelzl@33639
  2197
hoelzl@33639
  2198
lemma dropWhile_eq_drop: "dropWhile P xs = drop (length (takeWhile P xs)) xs"
hoelzl@33639
  2199
by (induct xs) auto
hoelzl@33639
  2200
hoelzl@33639
  2201
lemma hd_dropWhile:
hoelzl@33639
  2202
  "dropWhile P xs \<noteq> [] \<Longrightarrow> \<not> P (hd (dropWhile P xs))"
hoelzl@33639
  2203
using assms by (induct xs) auto
hoelzl@33639
  2204
hoelzl@33639
  2205
lemma takeWhile_eq_filter:
hoelzl@33639
  2206
  assumes "\<And> x. x \<in> set (dropWhile P xs) \<Longrightarrow> \<not> P x"
hoelzl@33639
  2207
  shows "takeWhile P xs = filter P xs"
hoelzl@33639
  2208
proof -
hoelzl@33639
  2209
  have A: "filter P xs = filter P (takeWhile P xs @ dropWhile P xs)"
hoelzl@33639
  2210
    by simp
hoelzl@33639
  2211
  have B: "filter P (dropWhile P xs) = []"
hoelzl@33639
  2212
    unfolding filter_empty_conv using assms by blast
hoelzl@33639
  2213
  have "filter P xs = takeWhile P xs"
hoelzl@33639
  2214
    unfolding A filter_append B
hoelzl@33639
  2215
    by (auto simp add: filter_id_conv dest: set_takeWhileD)
hoelzl@33639
  2216
  thus ?thesis ..
hoelzl@33639
  2217
qed
hoelzl@33639
  2218
hoelzl@33639
  2219
lemma takeWhile_eq_take_P_nth:
hoelzl@33639
  2220
  "\<lbrakk> \<And> i. \<lbrakk> i < n ; i < length xs \<rbrakk> \<Longrightarrow> P (xs ! i) ; n < length xs \<Longrightarrow> \<not> P (xs ! n) \<rbrakk> \<Longrightarrow>
hoelzl@33639
  2221
  takeWhile P xs = take n xs"
hoelzl@33639
  2222
proof (induct xs arbitrary: n)
hoelzl@33639
  2223
  case (Cons x xs)
hoelzl@33639
  2224
  thus ?case
hoelzl@33639
  2225
  proof (cases n)
hoelzl@33639
  2226
    case (Suc n') note this[simp]
hoelzl@33639
  2227
    have "P x" using Cons.prems(1)[of 0] by simp
hoelzl@33639
  2228
    moreover have "takeWhile P xs = take n' xs"
hoelzl@33639
  2229
    proof (rule Cons.hyps)
hoelzl@33639
  2230
      case goal1 thus "P (xs ! i)" using Cons.prems(1)[of "Suc i"] by simp
hoelzl@33639
  2231
    next case goal2 thus ?case using Cons by auto
hoelzl@33639
  2232
    qed
hoelzl@33639
  2233
    ultimately show ?thesis by simp
hoelzl@33639
  2234
   qed simp
hoelzl@33639
  2235
qed simp
hoelzl@33639
  2236
hoelzl@33639
  2237
lemma nth_length_takeWhile:
hoelzl@33639
  2238
  "length (takeWhile P xs) < length xs \<Longrightarrow> \<not> P (xs ! length (takeWhile P xs))"
hoelzl@33639
  2239
by (induct xs) auto
hoelzl@33639
  2240
hoelzl@33639
  2241
lemma length_takeWhile_less_P_nth:
hoelzl@33639
  2242
  assumes all: "\<And> i. i < j \<Longrightarrow> P (xs ! i)" and "j \<le> length xs"
hoelzl@33639
  2243
  shows "j \<le> length (takeWhile P xs)"
hoelzl@33639
  2244
proof (rule classical)
hoelzl@33639
  2245
  assume "\<not> ?thesis"
hoelzl@33639
  2246
  hence "length (takeWhile P xs) < length xs" using assms by simp
hoelzl@33639
  2247
  thus ?thesis using all `\<not> ?thesis` nth_length_takeWhile[of P xs] by auto
hoelzl@33639
  2248
qed
nipkow@31077
  2249
nipkow@17501
  2250
text{* The following two lemmmas could be generalized to an arbitrary
nipkow@17501
  2251
property. *}
nipkow@17501
  2252
nipkow@17501
  2253
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  2254
 takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
nipkow@17501
  2255
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
nipkow@17501
  2256
nipkow@17501
  2257
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  2258
  dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
nipkow@17501
  2259
apply(induct xs)
nipkow@17501
  2260
 apply simp
nipkow@17501
  2261
apply auto
nipkow@17501
  2262
apply(subst dropWhile_append2)
nipkow@17501
  2263
apply auto
nipkow@17501
  2264
done
nipkow@17501
  2265
nipkow@18423
  2266
lemma takeWhile_not_last:
bulwahn@46500
  2267
 "distinct xs \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
nipkow@18423
  2268
apply(induct xs)
nipkow@18423
  2269
 apply simp
nipkow@18423
  2270
apply(case_tac xs)
nipkow@18423
  2271
apply(auto)
nipkow@18423
  2272
done
nipkow@18423
  2273
krauss@44013
  2274
lemma takeWhile_cong [fundef_cong]:
krauss@18336
  2275
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  2276
  ==> takeWhile P l = takeWhile Q k"
nipkow@24349
  2277
by (induct k arbitrary: l) (simp_all)
krauss@18336
  2278
krauss@44013
  2279
lemma dropWhile_cong [fundef_cong]:
krauss@18336
  2280
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  2281
  ==> dropWhile P l = dropWhile Q k"
nipkow@24349
  2282
by (induct k arbitrary: l, simp_all)
krauss@18336
  2283
wenzelm@13114
  2284
haftmann@49948
  2285
subsubsection {* @{const zip} *}
wenzelm@13114
  2286
wenzelm@13142
  2287
lemma zip_Nil [simp]: "zip [] ys = []"
nipkow@13145
  2288
by (induct ys) auto
wenzelm@13114
  2289
wenzelm@13142
  2290
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
nipkow@13145
  2291
by simp
wenzelm@13114
  2292
wenzelm@13142
  2293
declare zip_Cons [simp del]
wenzelm@13114
  2294
haftmann@36198
  2295
lemma [code]:
haftmann@36198
  2296
  "zip [] ys = []"
haftmann@36198
  2297
  "zip xs [] = []"
haftmann@36198
  2298
  "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
haftmann@36198
  2299
  by (fact zip_Nil zip.simps(1) zip_Cons_Cons)+
haftmann@36198
  2300
nipkow@15281
  2301
lemma zip_Cons1:
nipkow@15281
  2302
 "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"
nipkow@15281
  2303
by(auto split:list.split)
nipkow@15281
  2304
wenzelm@13142
  2305
lemma length_zip [simp]:
krauss@22493
  2306
"length (zip xs ys) = min (length xs) (length ys)"
krauss@22493
  2307
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  2308
haftmann@34978
  2309
lemma zip_obtain_same_length:
haftmann@34978
  2310
  assumes "\<And>zs ws n. length zs = length ws \<Longrightarrow> n = min (length xs) (length ys)
haftmann@34978
  2311
    \<Longrightarrow> zs = take n xs \<Longrightarrow> ws = take n ys \<Longrightarrow> P (zip zs ws)"
haftmann@34978
  2312
  shows "P (zip xs ys)"
haftmann@34978
  2313
proof -
haftmann@34978
  2314
  let ?n = "min (length xs) (length ys)"
haftmann@34978
  2315
  have "P (zip (take ?n xs) (take ?n ys))"
haftmann@34978
  2316
    by (rule assms) simp_all
haftmann@34978
  2317
  moreover have "zip xs ys = zip (take ?n xs) (take ?n ys)"
haftmann@34978
  2318
  proof (induct xs arbitrary: ys)
haftmann@34978
  2319
    case Nil then show ?case by simp
haftmann@34978
  2320
  next
haftmann@34978
  2321
    case (Cons x xs) then show ?case by (cases ys) simp_all
haftmann@34978
  2322
  qed
haftmann@34978
  2323
  ultimately show ?thesis by simp
haftmann@34978
  2324
qed
haftmann@34978
  2325
wenzelm@13114
  2326
lemma zip_append1:
krauss@22493
  2327
"zip (xs @ ys) zs =
nipkow@13145
  2328
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
krauss@22493
  2329
by (induct xs zs rule:list_induct2') auto
wenzelm@13114
  2330
wenzelm@13114
  2331
lemma zip_append2:
krauss@22493
  2332
"zip xs (ys @ zs) =
nipkow@13145
  2333
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
krauss@22493
  2334
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  2335
wenzelm@13142
  2336
lemma zip_append [simp]:
bulwahn@46500
  2337
 "[| length xs = length us |] ==>
nipkow@13145
  2338
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
nipkow@13145
  2339
by (simp add: zip_append1)
wenzelm@13114
  2340
wenzelm@13114
  2341
lemma zip_rev:
nipkow@14247
  2342
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
nipkow@14247
  2343
by (induct rule:list_induct2, simp_all)
wenzelm@13114
  2344
hoelzl@33639
  2345
lemma zip_map_map:
hoelzl@33639
  2346
  "zip (map f xs) (map g ys) = map (\<lambda> (x, y). (f x, g y)) (zip xs ys)"
hoelzl@33639
  2347
proof (induct xs arbitrary: ys)
hoelzl@33639
  2348
  case (Cons x xs) note Cons_x_xs = Cons.hyps
hoelzl@33639
  2349
  show ?case
hoelzl@33639
  2350
  proof (cases ys)
hoelzl@33639
  2351
    case (Cons y ys')
hoelzl@33639
  2352
    show ?thesis unfolding Cons using Cons_x_xs by simp
hoelzl@33639
  2353
  qed simp
hoelzl@33639
  2354
qed simp
hoelzl@33639
  2355
hoelzl@33639
  2356
lemma zip_map1:
hoelzl@33639
  2357
  "zip (map f xs) ys = map (\<lambda>(x, y). (f x, y)) (zip xs ys)"
hoelzl@33639
  2358
using zip_map_map[of f xs "\<lambda>x. x" ys] by simp
hoelzl@33639
  2359
hoelzl@33639
  2360
lemma zip_map2:
hoelzl@33639
  2361
  "zip xs (map f ys) = map (\<lambda>(x, y). (x, f y)) (zip xs ys)"
hoelzl@33639
  2362
using zip_map_map[of "\<lambda>x. x" xs f ys] by simp
hoelzl@33639
  2363
nipkow@23096
  2364
lemma map_zip_map:
hoelzl@33639
  2365
  "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)"
hoelzl@33639
  2366
unfolding zip_map1 by auto
nipkow@23096
  2367
nipkow@23096
  2368
lemma map_zip_map2:
hoelzl@33639
  2369
  "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)"
hoelzl@33639
  2370
unfolding zip_map2 by auto
nipkow@23096
  2371
nipkow@31080
  2372
text{* Courtesy of Andreas Lochbihler: *}
nipkow@31080
  2373
lemma zip_same_conv_map: "zip xs xs = map (\<lambda>x. (x, x)) xs"
nipkow@31080
  2374
by(induct xs) auto
nipkow@31080
  2375
wenzelm@13142
  2376
lemma nth_zip [simp]:
nipkow@24526
  2377
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
nipkow@24526
  2378
apply (induct ys arbitrary: i xs, simp)
nipkow@13145
  2379
apply (case_tac xs)
nipkow@13145
  2380
 apply (simp_all add: nth.simps split: nat.split)
nipkow@13145
  2381
done
wenzelm@13114
  2382
wenzelm@13114
  2383
lemma set_zip:
nipkow@13145
  2384
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
nipkow@31080
  2385
by(simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
  2386
hoelzl@33639
  2387
lemma zip_same: "((a,b) \<in> set (zip xs xs)) = (a \<in> set xs \<and> a = b)"
hoelzl@33639
  2388
by(induct xs) auto
hoelzl@33639
  2389
wenzelm@13114
  2390
lemma zip_update:
nipkow@31080
  2391
  "zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
nipkow@31080
  2392
by(rule sym, simp add: update_zip)
wenzelm@13114
  2393
wenzelm@13142
  2394
lemma zip_replicate [simp]:
nipkow@24526
  2395
  "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
nipkow@24526
  2396
apply (induct i arbitrary: j, auto)
paulson@14208
  2397
apply (case_tac j, auto)
nipkow@13145
  2398
done
wenzelm@13114
  2399
nipkow@19487
  2400
lemma take_zip:
nipkow@24526
  2401
  "take n (zip xs ys) = zip (take n xs) (take n ys)"
nipkow@24526
  2402
apply (induct n arbitrary: xs ys)
nipkow@19487
  2403
 apply simp
nipkow@19487
  2404
apply (case_tac xs, simp)
nipkow@19487
  2405
apply (case_tac ys, simp_all)
nipkow@19487
  2406
done
nipkow@19487
  2407
nipkow@19487
  2408
lemma drop_zip:
nipkow@24526
  2409
  "drop n (zip xs ys) = zip (drop n xs) (drop n ys)"
nipkow@24526
  2410
apply (induct n arbitrary: xs ys)
nipkow@19487
  2411
 apply simp
nipkow@19487
  2412
apply (case_tac xs, simp)
nipkow@19487
  2413
apply (case_tac ys, simp_all)
nipkow@19487
  2414
done
nipkow@19487
  2415
hoelzl@33639
  2416
lemma zip_takeWhile_fst: "zip (takeWhile P xs) ys = takeWhile (P \<circ> fst) (zip xs ys)"
hoelzl@33639
  2417
proof (induct xs arbitrary: ys)
hoelzl@33639
  2418
  case (Cons x xs) thus ?case by (cases ys) auto
hoelzl@33639
  2419
qed simp
hoelzl@33639
  2420
hoelzl@33639
  2421
lemma zip_takeWhile_snd: "zip xs (takeWhile P ys) = takeWhile (P \<circ> snd) (zip xs ys)"
hoelzl@33639
  2422
proof (induct xs arbitrary: ys)
hoelzl@33639
  2423
  case (Cons x xs) thus ?case by (cases ys) auto
hoelzl@33639
  2424
qed simp
hoelzl@33639
  2425
krauss@22493
  2426
lemma set_zip_leftD:
krauss@22493
  2427
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"
krauss@22493
  2428
by (induct xs ys rule:list_induct2') auto
krauss@22493
  2429
krauss@22493
  2430
lemma set_zip_rightD:
krauss@22493
  2431
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"
krauss@22493
  2432
by (induct xs ys rule:list_induct2') auto
wenzelm@13142
  2433
nipkow@23983
  2434
lemma in_set_zipE:
nipkow@23983
  2435
  "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23983
  2436
by(blast dest: set_zip_leftD set_zip_rightD)
nipkow@23983
  2437
haftmann@29829
  2438
lemma zip_map_fst_snd:
haftmann@29829
  2439
  "zip (map fst zs) (map snd zs) = zs"
haftmann@29829
  2440
  by (induct zs) simp_all
haftmann@29829
  2441
haftmann@29829
  2442
lemma zip_eq_conv:
haftmann@29829
  2443
  "length xs = length ys \<Longrightarrow> zip xs ys = zs \<longleftrightarrow> map fst zs = xs \<and> map snd zs = ys"
haftmann@29829
  2444
  by (auto simp add: zip_map_fst_snd)
haftmann@29829
  2445
wenzelm@35115
  2446
haftmann@49948
  2447
subsubsection {* @{const list_all2} *}
wenzelm@13114
  2448
kleing@14316
  2449
lemma list_all2_lengthD [intro?]: 
kleing@14316
  2450
  "list_all2 P xs ys ==> length xs = length ys"
nipkow@24349
  2451
by (simp add: list_all2_def)
haftmann@19607
  2452
haftmann@19787
  2453
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"
nipkow@24349
  2454
by (simp add: list_all2_def)
haftmann@19607
  2455
haftmann@19787
  2456
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"
nipkow@24349
  2457
by (simp add: list_all2_def)
haftmann@19607
  2458
haftmann@19607
  2459
lemma list_all2_Cons [iff, code]:
haftmann@19607
  2460
  "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
nipkow@24349
  2461
by (auto simp add: list_all2_def)
wenzelm@13114
  2462
wenzelm@13114
  2463
lemma list_all2_Cons1:
nipkow@13145
  2464
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
nipkow@13145
  2465
by (cases ys) auto
wenzelm@13114
  2466
wenzelm@13114
  2467
lemma list_all2_Cons2:
nipkow@13145
  2468
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
nipkow@13145
  2469
by (cases xs) auto
wenzelm@13114
  2470
huffman@45794
  2471
lemma list_all2_induct
huffman@45794
  2472
  [consumes 1, case_names Nil Cons, induct set: list_all2]:
huffman@45794
  2473
  assumes P: "list_all2 P xs ys"
huffman@45794
  2474
  assumes Nil: "R [] []"
huffman@47640
  2475
  assumes Cons: "\<And>x xs y ys.
huffman@47640
  2476
    \<lbrakk>P x y; list_all2 P xs ys; R xs ys\<rbrakk> \<Longrightarrow> R (x # xs) (y # ys)"
huffman@45794
  2477
  shows "R xs ys"
huffman@45794
  2478
using P
huffman@45794
  2479
by (induct xs arbitrary: ys) (auto simp add: list_all2_Cons1 Nil Cons)
huffman@45794
  2480
wenzelm@13142
  2481
lemma list_all2_rev [iff]:
nipkow@13145
  2482
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
nipkow@13145
  2483
by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
  2484
kleing@13863
  2485
lemma list_all2_rev1:
kleing@13863
  2486
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
kleing@13863
  2487
by (subst list_all2_rev [symmetric]) simp
kleing@13863
  2488
wenzelm@13114
  2489
lemma list_all2_append1:
nipkow@13145
  2490
"list_all2 P (xs @ ys) zs =
nipkow@13145
  2491
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
nipkow@13145
  2492
list_all2 P xs us \<and> list_all2 P ys vs)"
nipkow@13145
  2493
apply (simp add: list_all2_def zip_append1)
nipkow@13145
  2494
apply (rule iffI)
nipkow@13145
  2495
 apply (rule_tac x = "take (length xs) zs" in exI)
nipkow@13145
  2496
 apply (rule_tac x = "drop (length xs) zs" in exI)
paulson@14208
  2497
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  2498
apply (simp add: ball_Un)
nipkow@13145
  2499
done
wenzelm@13114
  2500
wenzelm@13114
  2501
lemma list_all2_append2:
nipkow@13145
  2502
"list_all2 P xs (ys @ zs) =
nipkow@13145
  2503
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
nipkow@13145
  2504
list_all2 P us ys \<and> list_all2 P vs zs)"
nipkow@13145
  2505
apply (simp add: list_all2_def zip_append2)
nipkow@13145
  2506
apply (rule iffI)
nipkow@13145
  2507
 apply (rule_tac x = "take (length ys) xs" in exI)
nipkow@13145
  2508
 apply (rule_tac x = "drop (length ys) xs" in exI)
paulson@14208
  2509
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  2510
apply (simp add: ball_Un)
nipkow@13145
  2511
done
wenzelm@13114
  2512
kleing@13863
  2513
lemma list_all2_append:
nipkow@14247
  2514
  "length xs = length ys \<Longrightarrow>
nipkow@14247
  2515
  list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
nipkow@14247
  2516
by (induct rule:list_induct2, simp_all)
kleing@13863
  2517
kleing@13863
  2518
lemma list_all2_appendI [intro?, trans]:
kleing@13863
  2519
  "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
nipkow@24349
  2520
by (simp add: list_all2_append list_all2_lengthD)
kleing@13863
  2521
wenzelm@13114
  2522
lemma list_all2_conv_all_nth:
nipkow@13145
  2523
"list_all2 P xs ys =
nipkow@13145
  2524
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
nipkow@13145
  2525
by (force simp add: list_all2_def set_zip)
wenzelm@13114
  2526
berghofe@13883
  2527
lemma list_all2_trans:
berghofe@13883
  2528
  assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
berghofe@13883
  2529
  shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
berghofe@13883
  2530
        (is "!!bs cs. PROP ?Q as bs cs")
berghofe@13883
  2531
proof (induct as)
berghofe@13883
  2532
  fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
berghofe@13883
  2533
  show "!!cs. PROP ?Q (x # xs) bs cs"
berghofe@13883
  2534
  proof (induct bs)
berghofe@13883
  2535
    fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
berghofe@13883
  2536
    show "PROP ?Q (x # xs) (y # ys) cs"
berghofe@13883
  2537
      by (induct cs) (auto intro: tr I1 I2)
berghofe@13883
  2538
  qed simp
berghofe@13883
  2539
qed simp
berghofe@13883
  2540
kleing@13863
  2541
lemma list_all2_all_nthI [intro?]:
kleing@13863
  2542
  "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
nipkow@24349
  2543
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2544
paulson@14395
  2545
lemma list_all2I:
paulson@14395
  2546
  "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
nipkow@24349
  2547
by (simp add: list_all2_def)
paulson@14395
  2548
kleing@14328
  2549
lemma list_all2_nthD:
kleing@13863
  2550
  "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  2551
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2552
nipkow@14302
  2553
lemma list_all2_nthD2:
nipkow@14302
  2554
  "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  2555
by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
nipkow@14302
  2556
kleing@13863
  2557
lemma list_all2_map1: 
kleing@13863
  2558
  "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
nipkow@24349
  2559
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2560
kleing@13863
  2561
lemma list_all2_map2: 
kleing@13863
  2562
  "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
nipkow@24349
  2563
by (auto simp add: list_all2_conv_all_nth)
kleing@13863
  2564
kleing@14316
  2565
lemma list_all2_refl [intro?]:
kleing@13863
  2566
  "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
nipkow@24349
  2567
by (simp add: list_all2_conv_all_nth)
kleing@13863
  2568
kleing@13863
  2569
lemma list_all2_update_cong:
bulwahn@46669
  2570
  "\<lbrakk> list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
bulwahn@46669
  2571
by (cases "i < length ys") (auto simp add: list_all2_conv_all_nth nth_list_update)
kleing@13863
  2572
nipkow@14302
  2573
lemma list_all2_takeI [simp,intro?]:
nipkow@24526
  2574
  "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
nipkow@24526
  2575
apply (induct xs arbitrary: n ys)
nipkow@24526
  2576
 apply simp
nipkow@24526
  2577
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  2578
apply (case_tac n)
nipkow@24526
  2579
apply auto
nipkow@24526
  2580
done