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