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