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