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