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