src/HOL/List.thy
author haftmann
Thu Mar 27 19:04:36 2008 +0100 (2008-03-27)
changeset 26442 57fb6a8b099e
parent 26300 03def556e26e
child 26480 544cef16045b
permissions -rw-r--r--
restructuring; explicit case names for rule list_induct2
wenzelm@13462
     1
(*  Title:      HOL/List.thy
wenzelm@13462
     2
    ID:         $Id$
wenzelm@13462
     3
    Author:     Tobias Nipkow
clasohm@923
     4
*)
clasohm@923
     5
wenzelm@13114
     6
header {* The datatype of finite lists *}
wenzelm@13122
     7
nipkow@15131
     8
theory List
haftmann@25591
     9
imports ATP_Linkup
wenzelm@21754
    10
uses "Tools/string_syntax.ML"
nipkow@15131
    11
begin
clasohm@923
    12
wenzelm@13142
    13
datatype 'a list =
wenzelm@13366
    14
    Nil    ("[]")
wenzelm@13366
    15
  | Cons 'a  "'a list"    (infixr "#" 65)
clasohm@923
    16
nipkow@15392
    17
subsection{*Basic list processing functions*}
nipkow@15302
    18
clasohm@923
    19
consts
wenzelm@13366
    20
  filter:: "('a => bool) => 'a list => 'a list"
wenzelm@13366
    21
  concat:: "'a list list => 'a list"
wenzelm@13366
    22
  foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b"
wenzelm@13366
    23
  foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b"
wenzelm@13366
    24
  hd:: "'a list => 'a"
wenzelm@13366
    25
  tl:: "'a list => 'a list"
wenzelm@13366
    26
  last:: "'a list => 'a"
wenzelm@13366
    27
  butlast :: "'a list => 'a list"
wenzelm@13366
    28
  set :: "'a list => 'a set"
wenzelm@13366
    29
  map :: "('a=>'b) => ('a list => 'b list)"
nipkow@23096
    30
  listsum ::  "'a list => 'a::monoid_add"
wenzelm@13366
    31
  nth :: "'a list => nat => 'a"    (infixl "!" 100)
wenzelm@13366
    32
  list_update :: "'a list => nat => 'a => 'a list"
wenzelm@13366
    33
  take:: "nat => 'a list => 'a list"
wenzelm@13366
    34
  drop:: "nat => 'a list => 'a list"
wenzelm@13366
    35
  takeWhile :: "('a => bool) => 'a list => 'a list"
wenzelm@13366
    36
  dropWhile :: "('a => bool) => 'a list => 'a list"
wenzelm@13366
    37
  rev :: "'a list => 'a list"
wenzelm@13366
    38
  zip :: "'a list => 'b list => ('a * 'b) list"
nipkow@15425
    39
  upt :: "nat => nat => nat list" ("(1[_..</_'])")
wenzelm@13366
    40
  remdups :: "'a list => 'a list"
nipkow@15110
    41
  remove1 :: "'a => 'a list => 'a list"
wenzelm@13366
    42
  "distinct":: "'a list => bool"
wenzelm@13366
    43
  replicate :: "nat => 'a => 'a list"
nipkow@19390
    44
  splice :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
nipkow@15302
    45
clasohm@923
    46
nipkow@13146
    47
nonterminals lupdbinds lupdbind
nipkow@5077
    48
clasohm@923
    49
syntax
wenzelm@13366
    50
  -- {* list Enumeration *}
wenzelm@13366
    51
  "@list" :: "args => 'a list"    ("[(_)]")
clasohm@923
    52
wenzelm@13366
    53
  -- {* Special syntax for filter *}
nipkow@23279
    54
  "@filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_<-_./ _])")
clasohm@923
    55
wenzelm@13366
    56
  -- {* list update *}
wenzelm@13366
    57
  "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
wenzelm@13366
    58
  "" :: "lupdbind => lupdbinds"    ("_")
wenzelm@13366
    59
  "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
wenzelm@13366
    60
  "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
nipkow@5077
    61
clasohm@923
    62
translations
wenzelm@13366
    63
  "[x, xs]" == "x#[xs]"
wenzelm@13366
    64
  "[x]" == "x#[]"
nipkow@23279
    65
  "[x<-xs . P]"== "filter (%x. P) xs"
clasohm@923
    66
wenzelm@13366
    67
  "_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs"
wenzelm@13366
    68
  "xs[i:=x]" == "list_update xs i x"
nipkow@5077
    69
nipkow@5427
    70
wenzelm@12114
    71
syntax (xsymbols)
nipkow@23279
    72
  "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
kleing@14565
    73
syntax (HTML output)
nipkow@23279
    74
  "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<leftarrow>_ ./ _])")
paulson@3342
    75
paulson@3342
    76
wenzelm@13142
    77
text {*
wenzelm@14589
    78
  Function @{text size} is overloaded for all datatypes. Users may
wenzelm@13366
    79
  refer to the list version as @{text length}. *}
wenzelm@13142
    80
wenzelm@19363
    81
abbreviation
wenzelm@21404
    82
  length :: "'a list => nat" where
wenzelm@19363
    83
  "length == size"
nipkow@15302
    84
berghofe@5183
    85
primrec
paulson@15307
    86
  "hd(x#xs) = x"
paulson@15307
    87
berghofe@5183
    88
primrec
paulson@15307
    89
  "tl([]) = []"
paulson@15307
    90
  "tl(x#xs) = xs"
paulson@15307
    91
berghofe@5183
    92
primrec
paulson@15307
    93
  "last(x#xs) = (if xs=[] then x else last xs)"
paulson@15307
    94
berghofe@5183
    95
primrec
paulson@15307
    96
  "butlast []= []"
paulson@15307
    97
  "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
paulson@15307
    98
berghofe@5183
    99
primrec
paulson@15307
   100
  "set [] = {}"
paulson@15307
   101
  "set (x#xs) = insert x (set xs)"
paulson@15307
   102
berghofe@5183
   103
primrec
paulson@15307
   104
  "map f [] = []"
paulson@15307
   105
  "map f (x#xs) = f(x)#map f xs"
paulson@15307
   106
wenzelm@25221
   107
primrec
haftmann@25559
   108
  append :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infixr "@" 65)
haftmann@25559
   109
where
haftmann@25559
   110
  append_Nil:"[] @ ys = ys"
haftmann@25559
   111
  | append_Cons: "(x#xs) @ ys = x # xs @ ys"
paulson@15307
   112
berghofe@5183
   113
primrec
paulson@15307
   114
  "rev([]) = []"
paulson@15307
   115
  "rev(x#xs) = rev(xs) @ [x]"
paulson@15307
   116
berghofe@5183
   117
primrec
paulson@15307
   118
  "filter P [] = []"
paulson@15307
   119
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
paulson@15307
   120
berghofe@5183
   121
primrec
paulson@15307
   122
  foldl_Nil:"foldl f a [] = a"
paulson@15307
   123
  foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
paulson@15307
   124
paulson@8000
   125
primrec
paulson@15307
   126
  "foldr f [] a = a"
paulson@15307
   127
  "foldr f (x#xs) a = f x (foldr f xs a)"
paulson@15307
   128
berghofe@5183
   129
primrec
paulson@15307
   130
  "concat([]) = []"
paulson@15307
   131
  "concat(x#xs) = x @ concat(xs)"
paulson@15307
   132
berghofe@5183
   133
primrec
nipkow@23096
   134
"listsum [] = 0"
nipkow@23096
   135
"listsum (x # xs) = x + listsum xs"
nipkow@23096
   136
nipkow@23096
   137
primrec
paulson@15307
   138
  drop_Nil:"drop n [] = []"
paulson@15307
   139
  drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
paulson@15307
   140
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   141
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
paulson@15307
   142
berghofe@5183
   143
primrec
paulson@15307
   144
  take_Nil:"take n [] = []"
paulson@15307
   145
  take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
paulson@15307
   146
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   147
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
paulson@15307
   148
wenzelm@13142
   149
primrec
paulson@15307
   150
  nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
paulson@15307
   151
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   152
       theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
paulson@15307
   153
berghofe@5183
   154
primrec
paulson@15307
   155
  "[][i:=v] = []"
paulson@15307
   156
  "(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])"
paulson@15307
   157
paulson@15307
   158
primrec
paulson@15307
   159
  "takeWhile P [] = []"
paulson@15307
   160
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
paulson@15307
   161
berghofe@5183
   162
primrec
paulson@15307
   163
  "dropWhile P [] = []"
paulson@15307
   164
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
paulson@15307
   165
berghofe@5183
   166
primrec
paulson@15307
   167
  "zip xs [] = []"
paulson@15307
   168
  zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
paulson@15307
   169
  -- {*Warning: simpset does not contain this definition, but separate
paulson@15307
   170
       theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
paulson@15307
   171
nipkow@5427
   172
primrec
nipkow@15425
   173
  upt_0: "[i..<0] = []"
nipkow@15425
   174
  upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])"
paulson@15307
   175
berghofe@5183
   176
primrec
paulson@15307
   177
  "distinct [] = True"
paulson@15307
   178
  "distinct (x#xs) = (x ~: set xs \<and> distinct xs)"
paulson@15307
   179
berghofe@5183
   180
primrec
paulson@15307
   181
  "remdups [] = []"
paulson@15307
   182
  "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
paulson@15307
   183
berghofe@5183
   184
primrec
paulson@15307
   185
  "remove1 x [] = []"
paulson@15307
   186
  "remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)"
paulson@15307
   187
nipkow@15110
   188
primrec
paulson@15307
   189
  replicate_0: "replicate 0 x = []"
paulson@15307
   190
  replicate_Suc: "replicate (Suc n) x = x # replicate n x"
paulson@15307
   191
haftmann@21061
   192
definition
wenzelm@21404
   193
  rotate1 :: "'a list \<Rightarrow> 'a list" where
wenzelm@21404
   194
  "rotate1 xs = (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])"
wenzelm@21404
   195
wenzelm@21404
   196
definition
wenzelm@21404
   197
  rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
wenzelm@21404
   198
  "rotate n = rotate1 ^ n"
wenzelm@21404
   199
wenzelm@21404
   200
definition
wenzelm@21404
   201
  list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool" where
haftmann@25966
   202
  [code func del]: "list_all2 P xs ys =
haftmann@21061
   203
    (length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y))"
wenzelm@21404
   204
wenzelm@21404
   205
definition
wenzelm@21404
   206
  sublist :: "'a list => nat set => 'a list" where
wenzelm@21404
   207
  "sublist xs A = map fst (filter (\<lambda>p. snd p \<in> A) (zip xs [0..<size xs]))"
nipkow@17086
   208
nipkow@17086
   209
primrec
haftmann@21061
   210
  "splice [] ys = ys"
haftmann@21061
   211
  "splice (x#xs) ys = (if ys=[] then x#xs else x # hd ys # splice xs (tl ys))"
haftmann@21061
   212
    -- {*Warning: simpset does not contain the second eqn but a derived one. *}
haftmann@21061
   213
nipkow@24616
   214
text{* The following simple sort functions are intended for proofs,
nipkow@24616
   215
not for efficient implementations. *}
nipkow@24616
   216
wenzelm@25221
   217
context linorder
wenzelm@25221
   218
begin
wenzelm@25221
   219
wenzelm@25221
   220
fun sorted :: "'a list \<Rightarrow> bool" where
nipkow@24697
   221
"sorted [] \<longleftrightarrow> True" |
nipkow@24697
   222
"sorted [x] \<longleftrightarrow> True" |
haftmann@25062
   223
"sorted (x#y#zs) \<longleftrightarrow> x <= y \<and> sorted (y#zs)"
nipkow@24697
   224
haftmann@25559
   225
primrec insort :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
nipkow@24697
   226
"insort x [] = [x]" |
haftmann@25062
   227
"insort x (y#ys) = (if x <= y then (x#y#ys) else y#(insort x ys))"
nipkow@24697
   228
haftmann@25559
   229
primrec sort :: "'a list \<Rightarrow> 'a list" where
nipkow@24697
   230
"sort [] = []" |
nipkow@24697
   231
"sort (x#xs) = insort x (sort xs)"
nipkow@24616
   232
wenzelm@25221
   233
end
wenzelm@25221
   234
nipkow@24616
   235
wenzelm@23388
   236
subsubsection {* List comprehension *}
nipkow@23192
   237
nipkow@24349
   238
text{* Input syntax for Haskell-like list comprehension notation.
nipkow@24349
   239
Typical example: @{text"[(x,y). x \<leftarrow> xs, y \<leftarrow> ys, x \<noteq> y]"},
nipkow@24349
   240
the list of all pairs of distinct elements from @{text xs} and @{text ys}.
nipkow@24349
   241
The syntax is as in Haskell, except that @{text"|"} becomes a dot
nipkow@24349
   242
(like in Isabelle's set comprehension): @{text"[e. x \<leftarrow> xs, \<dots>]"} rather than
nipkow@24349
   243
\verb![e| x <- xs, ...]!.
nipkow@24349
   244
nipkow@24349
   245
The qualifiers after the dot are
nipkow@24349
   246
\begin{description}
nipkow@24349
   247
\item[generators] @{text"p \<leftarrow> xs"},
nipkow@24476
   248
 where @{text p} is a pattern and @{text xs} an expression of list type, or
nipkow@24476
   249
\item[guards] @{text"b"}, where @{text b} is a boolean expression.
nipkow@24476
   250
%\item[local bindings] @ {text"let x = e"}.
nipkow@24349
   251
\end{description}
nipkow@23240
   252
nipkow@24476
   253
Just like in Haskell, list comprehension is just a shorthand. To avoid
nipkow@24476
   254
misunderstandings, the translation into desugared form is not reversed
nipkow@24476
   255
upon output. Note that the translation of @{text"[e. x \<leftarrow> xs]"} is
nipkow@24476
   256
optmized to @{term"map (%x. e) xs"}.
nipkow@23240
   257
nipkow@24349
   258
It is easy to write short list comprehensions which stand for complex
nipkow@24349
   259
expressions. During proofs, they may become unreadable (and
nipkow@24349
   260
mangled). In such cases it can be advisable to introduce separate
nipkow@24349
   261
definitions for the list comprehensions in question.  *}
nipkow@24349
   262
nipkow@23209
   263
(*
nipkow@23240
   264
Proper theorem proving support would be nice. For example, if
nipkow@23192
   265
@{text"set[f x y. x \<leftarrow> xs, y \<leftarrow> ys, P x y]"}
nipkow@23192
   266
produced something like
nipkow@23209
   267
@{term"{z. EX x: set xs. EX y:set ys. P x y \<and> z = f x y}"}.
nipkow@23209
   268
*)
nipkow@23209
   269
nipkow@23240
   270
nonterminals lc_qual lc_quals
nipkow@23192
   271
nipkow@23192
   272
syntax
nipkow@23240
   273
"_listcompr" :: "'a \<Rightarrow> lc_qual \<Rightarrow> lc_quals \<Rightarrow> 'a list"  ("[_ . __")
nipkow@24349
   274
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ <- _")
nipkow@23240
   275
"_lc_test" :: "bool \<Rightarrow> lc_qual" ("_")
nipkow@24476
   276
(*"_lc_let" :: "letbinds => lc_qual"  ("let _")*)
nipkow@23240
   277
"_lc_end" :: "lc_quals" ("]")
nipkow@23240
   278
"_lc_quals" :: "lc_qual \<Rightarrow> lc_quals \<Rightarrow> lc_quals" (", __")
nipkow@24349
   279
"_lc_abs" :: "'a => 'b list => 'b list"
nipkow@23192
   280
nipkow@24476
   281
(* These are easier than ML code but cannot express the optimized
nipkow@24476
   282
   translation of [e. p<-xs]
nipkow@23192
   283
translations
nipkow@24349
   284
"[e. p<-xs]" => "concat(map (_lc_abs p [e]) xs)"
nipkow@23240
   285
"_listcompr e (_lc_gen p xs) (_lc_quals Q Qs)"
nipkow@24349
   286
 => "concat (map (_lc_abs p (_listcompr e Q Qs)) xs)"
nipkow@23240
   287
"[e. P]" => "if P then [e] else []"
nipkow@23240
   288
"_listcompr e (_lc_test P) (_lc_quals Q Qs)"
nipkow@23240
   289
 => "if P then (_listcompr e Q Qs) else []"
nipkow@24349
   290
"_listcompr e (_lc_let b) (_lc_quals Q Qs)"
nipkow@24349
   291
 => "_Let b (_listcompr e Q Qs)"
nipkow@24476
   292
*)
nipkow@23240
   293
nipkow@23279
   294
syntax (xsymbols)
nipkow@24349
   295
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@23279
   296
syntax (HTML output)
nipkow@24349
   297
"_lc_gen" :: "'a \<Rightarrow> 'a list \<Rightarrow> lc_qual" ("_ \<leftarrow> _")
nipkow@24349
   298
nipkow@24349
   299
parse_translation (advanced) {*
nipkow@24349
   300
let
nipkow@24476
   301
  val NilC = Syntax.const @{const_name Nil};
nipkow@24476
   302
  val ConsC = Syntax.const @{const_name Cons};
nipkow@24476
   303
  val mapC = Syntax.const @{const_name map};
nipkow@24476
   304
  val concatC = Syntax.const @{const_name concat};
nipkow@24476
   305
  val IfC = Syntax.const @{const_name If};
nipkow@24476
   306
  fun singl x = ConsC $ x $ NilC;
nipkow@24476
   307
nipkow@24476
   308
   fun pat_tr ctxt p e opti = (* %x. case x of p => e | _ => [] *)
nipkow@24349
   309
    let
nipkow@24476
   310
      val x = Free (Name.variant (add_term_free_names (p$e, [])) "x", dummyT);
nipkow@24476
   311
      val e = if opti then singl e else e;
nipkow@24476
   312
      val case1 = Syntax.const "_case1" $ p $ e;
nipkow@24349
   313
      val case2 = Syntax.const "_case1" $ Syntax.const Term.dummy_patternN
nipkow@24476
   314
                                        $ NilC;
nipkow@24349
   315
      val cs = Syntax.const "_case2" $ case1 $ case2
nipkow@24349
   316
      val ft = DatatypeCase.case_tr false DatatypePackage.datatype_of_constr
nipkow@24349
   317
                 ctxt [x, cs]
nipkow@24349
   318
    in lambda x ft end;
nipkow@24349
   319
nipkow@24476
   320
  fun abs_tr ctxt (p as Free(s,T)) e opti =
nipkow@24349
   321
        let val thy = ProofContext.theory_of ctxt;
nipkow@24349
   322
            val s' = Sign.intern_const thy s
nipkow@24476
   323
        in if Sign.declared_const thy s'
nipkow@24476
   324
           then (pat_tr ctxt p e opti, false)
nipkow@24476
   325
           else (lambda p e, true)
nipkow@24349
   326
        end
nipkow@24476
   327
    | abs_tr ctxt p e opti = (pat_tr ctxt p e opti, false);
nipkow@24476
   328
nipkow@24476
   329
  fun lc_tr ctxt [e, Const("_lc_test",_)$b, qs] =
nipkow@24476
   330
        let val res = case qs of Const("_lc_end",_) => singl e
nipkow@24476
   331
                      | Const("_lc_quals",_)$q$qs => lc_tr ctxt [e,q,qs];
nipkow@24476
   332
        in IfC $ b $ res $ NilC end
nipkow@24476
   333
    | lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_end",_)] =
nipkow@24476
   334
        (case abs_tr ctxt p e true of
nipkow@24476
   335
           (f,true) => mapC $ f $ es
nipkow@24476
   336
         | (f, false) => concatC $ (mapC $ f $ es))
nipkow@24476
   337
    | lc_tr ctxt [e, Const("_lc_gen",_) $ p $ es, Const("_lc_quals",_)$q$qs] =
nipkow@24476
   338
        let val e' = lc_tr ctxt [e,q,qs];
nipkow@24476
   339
        in concatC $ (mapC $ (fst(abs_tr ctxt p e' false)) $ es) end
nipkow@24476
   340
nipkow@24476
   341
in [("_listcompr", lc_tr)] end
nipkow@24349
   342
*}
nipkow@23279
   343
nipkow@23240
   344
(*
nipkow@23240
   345
term "[(x,y,z). b]"
nipkow@24476
   346
term "[(x,y,z). x\<leftarrow>xs]"
nipkow@24476
   347
term "[e x y. x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@24476
   348
term "[(x,y,z). x<a, x>b]"
nipkow@24476
   349
term "[(x,y,z). x\<leftarrow>xs, x>b]"
nipkow@24476
   350
term "[(x,y,z). x<a, x\<leftarrow>xs]"
nipkow@24349
   351
term "[(x,y). Cons True x \<leftarrow> xs]"
nipkow@24349
   352
term "[(x,y,z). Cons x [] \<leftarrow> xs]"
nipkow@23240
   353
term "[(x,y,z). x<a, x>b, x=d]"
nipkow@23240
   354
term "[(x,y,z). x<a, x>b, y\<leftarrow>ys]"
nipkow@23240
   355
term "[(x,y,z). x<a, x\<leftarrow>xs,y>b]"
nipkow@23240
   356
term "[(x,y,z). x<a, x\<leftarrow>xs, y\<leftarrow>ys]"
nipkow@23240
   357
term "[(x,y,z). x\<leftarrow>xs, x>b, y<a]"
nipkow@23240
   358
term "[(x,y,z). x\<leftarrow>xs, x>b, y\<leftarrow>ys]"
nipkow@23240
   359
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,y>x]"
nipkow@23240
   360
term "[(x,y,z). x\<leftarrow>xs, y\<leftarrow>ys,z\<leftarrow>zs]"
nipkow@24349
   361
term "[(x,y). x\<leftarrow>xs, let xx = x+x, y\<leftarrow>ys, y \<noteq> xx]"
nipkow@23192
   362
*)
nipkow@23192
   363
haftmann@21061
   364
subsubsection {* @{const Nil} and @{const Cons} *}
haftmann@21061
   365
haftmann@21061
   366
lemma not_Cons_self [simp]:
haftmann@21061
   367
  "xs \<noteq> x # xs"
nipkow@13145
   368
by (induct xs) auto
wenzelm@13114
   369
wenzelm@13142
   370
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
wenzelm@13114
   371
wenzelm@13142
   372
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
nipkow@13145
   373
by (induct xs) auto
wenzelm@13114
   374
wenzelm@13142
   375
lemma length_induct:
haftmann@21061
   376
  "(\<And>xs. \<forall>ys. length ys < length xs \<longrightarrow> P ys \<Longrightarrow> P xs) \<Longrightarrow> P xs"
nipkow@17589
   377
by (rule measure_induct [of length]) iprover
wenzelm@13114
   378
wenzelm@13114
   379
haftmann@21061
   380
subsubsection {* @{const length} *}
wenzelm@13114
   381
wenzelm@13142
   382
text {*
haftmann@21061
   383
  Needs to come before @{text "@"} because of theorem @{text
haftmann@21061
   384
  append_eq_append_conv}.
wenzelm@13142
   385
*}
wenzelm@13114
   386
wenzelm@13142
   387
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
nipkow@13145
   388
by (induct xs) auto
wenzelm@13114
   389
wenzelm@13142
   390
lemma length_map [simp]: "length (map f xs) = length xs"
nipkow@13145
   391
by (induct xs) auto
wenzelm@13114
   392
wenzelm@13142
   393
lemma length_rev [simp]: "length (rev xs) = length xs"
nipkow@13145
   394
by (induct xs) auto
wenzelm@13114
   395
wenzelm@13142
   396
lemma length_tl [simp]: "length (tl xs) = length xs - 1"
nipkow@13145
   397
by (cases xs) auto
wenzelm@13114
   398
wenzelm@13142
   399
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
nipkow@13145
   400
by (induct xs) auto
wenzelm@13114
   401
wenzelm@13142
   402
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
nipkow@13145
   403
by (induct xs) auto
wenzelm@13114
   404
nipkow@23479
   405
lemma length_pos_if_in_set: "x : set xs \<Longrightarrow> length xs > 0"
nipkow@23479
   406
by auto
nipkow@23479
   407
wenzelm@13114
   408
lemma length_Suc_conv:
nipkow@13145
   409
"(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
nipkow@13145
   410
by (induct xs) auto
wenzelm@13142
   411
nipkow@14025
   412
lemma Suc_length_conv:
nipkow@14025
   413
"(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
paulson@14208
   414
apply (induct xs, simp, simp)
nipkow@14025
   415
apply blast
nipkow@14025
   416
done
nipkow@14025
   417
wenzelm@25221
   418
lemma impossible_Cons: "length xs <= length ys ==> xs = x # ys = False"
wenzelm@25221
   419
  by (induct xs) auto
wenzelm@25221
   420
haftmann@26442
   421
lemma list_induct2 [consumes 1, case_names Nil Cons]:
haftmann@26442
   422
  "length xs = length ys \<Longrightarrow> P [] [] \<Longrightarrow>
haftmann@26442
   423
   (\<And>x xs y ys. length xs = length ys \<Longrightarrow> P xs ys \<Longrightarrow> P (x#xs) (y#ys))
haftmann@26442
   424
   \<Longrightarrow> P xs ys"
haftmann@26442
   425
proof (induct xs arbitrary: ys)
haftmann@26442
   426
  case Nil then show ?case by simp
haftmann@26442
   427
next
haftmann@26442
   428
  case (Cons x xs ys) then show ?case by (cases ys) simp_all
haftmann@26442
   429
qed
haftmann@26442
   430
haftmann@26442
   431
lemma list_induct3 [consumes 2, case_names Nil Cons]:
haftmann@26442
   432
  "length xs = length ys \<Longrightarrow> length ys = length zs \<Longrightarrow> P [] [] [] \<Longrightarrow>
haftmann@26442
   433
   (\<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
   434
   \<Longrightarrow> P xs ys zs"
haftmann@26442
   435
proof (induct xs arbitrary: ys zs)
haftmann@26442
   436
  case Nil then show ?case by simp
haftmann@26442
   437
next
haftmann@26442
   438
  case (Cons x xs ys zs) then show ?case by (cases ys, simp_all)
haftmann@26442
   439
    (cases zs, simp_all)
haftmann@26442
   440
qed
wenzelm@13114
   441
krauss@22493
   442
lemma list_induct2': 
krauss@22493
   443
  "\<lbrakk> P [] [];
krauss@22493
   444
  \<And>x xs. P (x#xs) [];
krauss@22493
   445
  \<And>y ys. P [] (y#ys);
krauss@22493
   446
   \<And>x xs y ys. P xs ys  \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
krauss@22493
   447
 \<Longrightarrow> P xs ys"
krauss@22493
   448
by (induct xs arbitrary: ys) (case_tac x, auto)+
krauss@22493
   449
nipkow@22143
   450
lemma neq_if_length_neq: "length xs \<noteq> length ys \<Longrightarrow> (xs = ys) == False"
nipkow@24349
   451
by (rule Eq_FalseI) auto
wenzelm@24037
   452
wenzelm@24037
   453
simproc_setup list_neq ("(xs::'a list) = ys") = {*
nipkow@22143
   454
(*
nipkow@22143
   455
Reduces xs=ys to False if xs and ys cannot be of the same length.
nipkow@22143
   456
This is the case if the atomic sublists of one are a submultiset
nipkow@22143
   457
of those of the other list and there are fewer Cons's in one than the other.
nipkow@22143
   458
*)
wenzelm@24037
   459
wenzelm@24037
   460
let
nipkow@22143
   461
nipkow@22143
   462
fun len (Const("List.list.Nil",_)) acc = acc
nipkow@22143
   463
  | len (Const("List.list.Cons",_) $ _ $ xs) (ts,n) = len xs (ts,n+1)
haftmann@23029
   464
  | len (Const("List.append",_) $ xs $ ys) acc = len xs (len ys acc)
nipkow@22143
   465
  | len (Const("List.rev",_) $ xs) acc = len xs acc
nipkow@22143
   466
  | len (Const("List.map",_) $ _ $ xs) acc = len xs acc
nipkow@22143
   467
  | len t (ts,n) = (t::ts,n);
nipkow@22143
   468
wenzelm@24037
   469
fun list_neq _ ss ct =
nipkow@22143
   470
  let
wenzelm@24037
   471
    val (Const(_,eqT) $ lhs $ rhs) = Thm.term_of ct;
nipkow@22143
   472
    val (ls,m) = len lhs ([],0) and (rs,n) = len rhs ([],0);
nipkow@22143
   473
    fun prove_neq() =
nipkow@22143
   474
      let
nipkow@22143
   475
        val Type(_,listT::_) = eqT;
haftmann@22994
   476
        val size = HOLogic.size_const listT;
nipkow@22143
   477
        val eq_len = HOLogic.mk_eq (size $ lhs, size $ rhs);
nipkow@22143
   478
        val neq_len = HOLogic.mk_Trueprop (HOLogic.Not $ eq_len);
nipkow@22143
   479
        val thm = Goal.prove (Simplifier.the_context ss) [] [] neq_len
haftmann@22633
   480
          (K (simp_tac (Simplifier.inherit_context ss @{simpset}) 1));
haftmann@22633
   481
      in SOME (thm RS @{thm neq_if_length_neq}) end
nipkow@22143
   482
  in
wenzelm@23214
   483
    if m < n andalso submultiset (op aconv) (ls,rs) orelse
wenzelm@23214
   484
       n < m andalso submultiset (op aconv) (rs,ls)
nipkow@22143
   485
    then prove_neq() else NONE
nipkow@22143
   486
  end;
wenzelm@24037
   487
in list_neq end;
nipkow@22143
   488
*}
nipkow@22143
   489
nipkow@22143
   490
nipkow@15392
   491
subsubsection {* @{text "@"} -- append *}
wenzelm@13114
   492
wenzelm@13142
   493
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
nipkow@13145
   494
by (induct xs) auto
wenzelm@13114
   495
wenzelm@13142
   496
lemma append_Nil2 [simp]: "xs @ [] = xs"
nipkow@13145
   497
by (induct xs) auto
nipkow@3507
   498
nipkow@24449
   499
interpretation semigroup_append: semigroup_add ["op @"]
nipkow@24449
   500
by unfold_locales simp
nipkow@24449
   501
interpretation monoid_append: monoid_add ["[]" "op @"]
nipkow@24449
   502
by unfold_locales (simp+)
nipkow@24449
   503
wenzelm@13142
   504
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
nipkow@13145
   505
by (induct xs) auto
wenzelm@13114
   506
wenzelm@13142
   507
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
nipkow@13145
   508
by (induct xs) auto
wenzelm@13114
   509
wenzelm@13142
   510
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
nipkow@13145
   511
by (induct xs) auto
wenzelm@13114
   512
wenzelm@13142
   513
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
nipkow@13145
   514
by (induct xs) auto
wenzelm@13114
   515
wenzelm@25221
   516
lemma append_eq_append_conv [simp, noatp]:
nipkow@24526
   517
 "length xs = length ys \<or> length us = length vs
berghofe@13883
   518
 ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
nipkow@24526
   519
apply (induct xs arbitrary: ys)
paulson@14208
   520
 apply (case_tac ys, simp, force)
paulson@14208
   521
apply (case_tac ys, force, simp)
nipkow@13145
   522
done
wenzelm@13142
   523
nipkow@24526
   524
lemma append_eq_append_conv2: "(xs @ ys = zs @ ts) =
nipkow@24526
   525
  (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"
nipkow@24526
   526
apply (induct xs arbitrary: ys zs ts)
nipkow@14495
   527
 apply fastsimp
nipkow@14495
   528
apply(case_tac zs)
nipkow@14495
   529
 apply simp
nipkow@14495
   530
apply fastsimp
nipkow@14495
   531
done
nipkow@14495
   532
wenzelm@13142
   533
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
nipkow@13145
   534
by simp
wenzelm@13142
   535
wenzelm@13142
   536
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
nipkow@13145
   537
by simp
wenzelm@13114
   538
wenzelm@13142
   539
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
nipkow@13145
   540
by simp
wenzelm@13114
   541
wenzelm@13142
   542
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
nipkow@13145
   543
using append_same_eq [of _ _ "[]"] by auto
nipkow@3507
   544
wenzelm@13142
   545
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
nipkow@13145
   546
using append_same_eq [of "[]"] by auto
wenzelm@13114
   547
paulson@24286
   548
lemma hd_Cons_tl [simp,noatp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
nipkow@13145
   549
by (induct xs) auto
wenzelm@13114
   550
wenzelm@13142
   551
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
nipkow@13145
   552
by (induct xs) auto
wenzelm@13114
   553
wenzelm@13142
   554
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
nipkow@13145
   555
by (simp add: hd_append split: list.split)
wenzelm@13114
   556
wenzelm@13142
   557
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
nipkow@13145
   558
by (simp split: list.split)
wenzelm@13114
   559
wenzelm@13142
   560
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
nipkow@13145
   561
by (simp add: tl_append split: list.split)
wenzelm@13114
   562
wenzelm@13114
   563
nipkow@14300
   564
lemma Cons_eq_append_conv: "x#xs = ys@zs =
nipkow@14300
   565
 (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"
nipkow@14300
   566
by(cases ys) auto
nipkow@14300
   567
nipkow@15281
   568
lemma append_eq_Cons_conv: "(ys@zs = x#xs) =
nipkow@15281
   569
 (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"
nipkow@15281
   570
by(cases ys) auto
nipkow@15281
   571
nipkow@14300
   572
wenzelm@13142
   573
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114
   574
wenzelm@13114
   575
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
nipkow@13145
   576
by simp
wenzelm@13114
   577
wenzelm@13142
   578
lemma Cons_eq_appendI:
nipkow@13145
   579
"[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
nipkow@13145
   580
by (drule sym) simp
wenzelm@13114
   581
wenzelm@13142
   582
lemma append_eq_appendI:
nipkow@13145
   583
"[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
nipkow@13145
   584
by (drule sym) simp
wenzelm@13114
   585
wenzelm@13114
   586
wenzelm@13142
   587
text {*
nipkow@13145
   588
Simplification procedure for all list equalities.
nipkow@13145
   589
Currently only tries to rearrange @{text "@"} to see if
nipkow@13145
   590
- both lists end in a singleton list,
nipkow@13145
   591
- or both lists end in the same list.
wenzelm@13142
   592
*}
wenzelm@13142
   593
wenzelm@13142
   594
ML_setup {*
nipkow@3507
   595
local
nipkow@3507
   596
wenzelm@13114
   597
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
wenzelm@13462
   598
  (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
haftmann@23029
   599
  | last (Const("List.append",_) $ _ $ ys) = last ys
wenzelm@13462
   600
  | last t = t;
wenzelm@13114
   601
wenzelm@13114
   602
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
wenzelm@13462
   603
  | list1 _ = false;
wenzelm@13114
   604
wenzelm@13114
   605
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
wenzelm@13462
   606
  (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
haftmann@23029
   607
  | butlast ((app as Const("List.append",_) $ xs) $ ys) = app $ butlast ys
wenzelm@13462
   608
  | butlast xs = Const("List.list.Nil",fastype_of xs);
wenzelm@13114
   609
haftmann@22633
   610
val rearr_ss = HOL_basic_ss addsimps [@{thm append_assoc},
haftmann@22633
   611
  @{thm append_Nil}, @{thm append_Cons}];
wenzelm@16973
   612
wenzelm@20044
   613
fun list_eq ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13462
   614
  let
wenzelm@13462
   615
    val lastl = last lhs and lastr = last rhs;
wenzelm@13462
   616
    fun rearr conv =
wenzelm@13462
   617
      let
wenzelm@13462
   618
        val lhs1 = butlast lhs and rhs1 = butlast rhs;
wenzelm@13462
   619
        val Type(_,listT::_) = eqT
wenzelm@13462
   620
        val appT = [listT,listT] ---> listT
haftmann@23029
   621
        val app = Const("List.append",appT)
wenzelm@13462
   622
        val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@13480
   623
        val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
wenzelm@20044
   624
        val thm = Goal.prove (Simplifier.the_context ss) [] [] eq
wenzelm@17877
   625
          (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1));
skalberg@15531
   626
      in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;
wenzelm@13114
   627
wenzelm@13462
   628
  in
haftmann@22633
   629
    if list1 lastl andalso list1 lastr then rearr @{thm append1_eq_conv}
haftmann@22633
   630
    else if lastl aconv lastr then rearr @{thm append_same_eq}
skalberg@15531
   631
    else NONE
wenzelm@13462
   632
  end;
wenzelm@13462
   633
wenzelm@13114
   634
in
wenzelm@13462
   635
wenzelm@13462
   636
val list_eq_simproc =
haftmann@22633
   637
  Simplifier.simproc @{theory} "list_eq" ["(xs::'a list) = ys"] (K list_eq);
wenzelm@13462
   638
wenzelm@13114
   639
end;
wenzelm@13114
   640
wenzelm@13114
   641
Addsimprocs [list_eq_simproc];
wenzelm@13114
   642
*}
wenzelm@13114
   643
wenzelm@13114
   644
nipkow@15392
   645
subsubsection {* @{text map} *}
wenzelm@13114
   646
wenzelm@13142
   647
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
nipkow@13145
   648
by (induct xs) simp_all
wenzelm@13114
   649
wenzelm@13142
   650
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
nipkow@13145
   651
by (rule ext, induct_tac xs) auto
wenzelm@13114
   652
wenzelm@13142
   653
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
nipkow@13145
   654
by (induct xs) auto
wenzelm@13114
   655
wenzelm@13142
   656
lemma map_compose: "map (f o g) xs = map f (map g xs)"
nipkow@13145
   657
by (induct xs) (auto simp add: o_def)
wenzelm@13114
   658
wenzelm@13142
   659
lemma rev_map: "rev (map f xs) = map f (rev xs)"
nipkow@13145
   660
by (induct xs) auto
wenzelm@13114
   661
nipkow@13737
   662
lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
nipkow@13737
   663
by (induct xs) auto
nipkow@13737
   664
krauss@19770
   665
lemma map_cong [fundef_cong, recdef_cong]:
nipkow@13145
   666
"xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
nipkow@13145
   667
-- {* a congruence rule for @{text map} *}
nipkow@13737
   668
by simp
wenzelm@13114
   669
wenzelm@13142
   670
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
nipkow@13145
   671
by (cases xs) auto
wenzelm@13114
   672
wenzelm@13142
   673
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
nipkow@13145
   674
by (cases xs) auto
wenzelm@13114
   675
paulson@18447
   676
lemma map_eq_Cons_conv:
nipkow@14025
   677
 "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
nipkow@13145
   678
by (cases xs) auto
wenzelm@13114
   679
paulson@18447
   680
lemma Cons_eq_map_conv:
nipkow@14025
   681
 "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
nipkow@14025
   682
by (cases ys) auto
nipkow@14025
   683
paulson@18447
   684
lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]
paulson@18447
   685
lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]
paulson@18447
   686
declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!]
paulson@18447
   687
nipkow@14111
   688
lemma ex_map_conv:
nipkow@14111
   689
  "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
paulson@18447
   690
by(induct ys, auto simp add: Cons_eq_map_conv)
nipkow@14111
   691
nipkow@15110
   692
lemma map_eq_imp_length_eq:
nipkow@24526
   693
  "map f xs = map f ys ==> length xs = length ys"
nipkow@24526
   694
apply (induct ys arbitrary: xs)
nipkow@15110
   695
 apply simp
paulson@24632
   696
apply (metis Suc_length_conv length_map)
nipkow@15110
   697
done
nipkow@15110
   698
nipkow@15110
   699
lemma map_inj_on:
nipkow@15110
   700
 "[| map f xs = map f ys; inj_on f (set xs Un set ys) |]
nipkow@15110
   701
  ==> xs = ys"
nipkow@15110
   702
apply(frule map_eq_imp_length_eq)
nipkow@15110
   703
apply(rotate_tac -1)
nipkow@15110
   704
apply(induct rule:list_induct2)
nipkow@15110
   705
 apply simp
nipkow@15110
   706
apply(simp)
nipkow@15110
   707
apply (blast intro:sym)
nipkow@15110
   708
done
nipkow@15110
   709
nipkow@15110
   710
lemma inj_on_map_eq_map:
nipkow@15110
   711
 "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@15110
   712
by(blast dest:map_inj_on)
nipkow@15110
   713
wenzelm@13114
   714
lemma map_injective:
nipkow@24526
   715
 "map f xs = map f ys ==> inj f ==> xs = ys"
nipkow@24526
   716
by (induct ys arbitrary: xs) (auto dest!:injD)
wenzelm@13114
   717
nipkow@14339
   718
lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
nipkow@14339
   719
by(blast dest:map_injective)
nipkow@14339
   720
wenzelm@13114
   721
lemma inj_mapI: "inj f ==> inj (map f)"
nipkow@17589
   722
by (iprover dest: map_injective injD intro: inj_onI)
wenzelm@13114
   723
wenzelm@13114
   724
lemma inj_mapD: "inj (map f) ==> inj f"
paulson@14208
   725
apply (unfold inj_on_def, clarify)
nipkow@13145
   726
apply (erule_tac x = "[x]" in ballE)
paulson@14208
   727
 apply (erule_tac x = "[y]" in ballE, simp, blast)
nipkow@13145
   728
apply blast
nipkow@13145
   729
done
wenzelm@13114
   730
nipkow@14339
   731
lemma inj_map[iff]: "inj (map f) = inj f"
nipkow@13145
   732
by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114
   733
nipkow@15303
   734
lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A"
nipkow@15303
   735
apply(rule inj_onI)
nipkow@15303
   736
apply(erule map_inj_on)
nipkow@15303
   737
apply(blast intro:inj_onI dest:inj_onD)
nipkow@15303
   738
done
nipkow@15303
   739
kleing@14343
   740
lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"
kleing@14343
   741
by (induct xs, auto)
wenzelm@13114
   742
nipkow@14402
   743
lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs"
nipkow@14402
   744
by (induct xs) auto
nipkow@14402
   745
nipkow@15110
   746
lemma map_fst_zip[simp]:
nipkow@15110
   747
  "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs"
nipkow@15110
   748
by (induct rule:list_induct2, simp_all)
nipkow@15110
   749
nipkow@15110
   750
lemma map_snd_zip[simp]:
nipkow@15110
   751
  "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys"
nipkow@15110
   752
by (induct rule:list_induct2, simp_all)
nipkow@15110
   753
nipkow@15110
   754
nipkow@15392
   755
subsubsection {* @{text rev} *}
wenzelm@13114
   756
wenzelm@13142
   757
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
nipkow@13145
   758
by (induct xs) auto
wenzelm@13114
   759
wenzelm@13142
   760
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
nipkow@13145
   761
by (induct xs) auto
wenzelm@13114
   762
kleing@15870
   763
lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"
kleing@15870
   764
by auto
kleing@15870
   765
wenzelm@13142
   766
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
nipkow@13145
   767
by (induct xs) auto
wenzelm@13114
   768
wenzelm@13142
   769
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
nipkow@13145
   770
by (induct xs) auto
wenzelm@13114
   771
kleing@15870
   772
lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"
kleing@15870
   773
by (cases xs) auto
kleing@15870
   774
kleing@15870
   775
lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"
kleing@15870
   776
by (cases xs) auto
kleing@15870
   777
haftmann@21061
   778
lemma rev_is_rev_conv [iff]: "(rev xs = rev ys) = (xs = ys)"
haftmann@21061
   779
apply (induct xs arbitrary: ys, force)
paulson@14208
   780
apply (case_tac ys, simp, force)
nipkow@13145
   781
done
wenzelm@13114
   782
nipkow@15439
   783
lemma inj_on_rev[iff]: "inj_on rev A"
nipkow@15439
   784
by(simp add:inj_on_def)
nipkow@15439
   785
wenzelm@13366
   786
lemma rev_induct [case_names Nil snoc]:
wenzelm@13366
   787
  "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
berghofe@15489
   788
apply(simplesubst rev_rev_ident[symmetric])
nipkow@13145
   789
apply(rule_tac list = "rev xs" in list.induct, simp_all)
nipkow@13145
   790
done
wenzelm@13114
   791
wenzelm@13366
   792
lemma rev_exhaust [case_names Nil snoc]:
wenzelm@13366
   793
  "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
nipkow@13145
   794
by (induct xs rule: rev_induct) auto
wenzelm@13114
   795
wenzelm@13366
   796
lemmas rev_cases = rev_exhaust
wenzelm@13366
   797
nipkow@18423
   798
lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"
nipkow@18423
   799
by(rule rev_cases[of xs]) auto
nipkow@18423
   800
wenzelm@13114
   801
nipkow@15392
   802
subsubsection {* @{text set} *}
wenzelm@13114
   803
wenzelm@13142
   804
lemma finite_set [iff]: "finite (set xs)"
nipkow@13145
   805
by (induct xs) auto
wenzelm@13114
   806
wenzelm@13142
   807
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
nipkow@13145
   808
by (induct xs) auto
wenzelm@13114
   809
nipkow@17830
   810
lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"
nipkow@17830
   811
by(cases xs) auto
oheimb@14099
   812
wenzelm@13142
   813
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
nipkow@13145
   814
by auto
wenzelm@13114
   815
oheimb@14099
   816
lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
oheimb@14099
   817
by auto
oheimb@14099
   818
wenzelm@13142
   819
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
nipkow@13145
   820
by (induct xs) auto
wenzelm@13114
   821
nipkow@15245
   822
lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
nipkow@15245
   823
by(induct xs) auto
nipkow@15245
   824
wenzelm@13142
   825
lemma set_rev [simp]: "set (rev xs) = set xs"
nipkow@13145
   826
by (induct xs) auto
wenzelm@13114
   827
wenzelm@13142
   828
lemma set_map [simp]: "set (map f xs) = f`(set xs)"
nipkow@13145
   829
by (induct xs) auto
wenzelm@13114
   830
wenzelm@13142
   831
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
nipkow@13145
   832
by (induct xs) auto
wenzelm@13114
   833
nipkow@15425
   834
lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}"
paulson@14208
   835
apply (induct j, simp_all)
paulson@14208
   836
apply (erule ssubst, auto)
nipkow@13145
   837
done
wenzelm@13114
   838
wenzelm@13142
   839
wenzelm@25221
   840
lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs"
nipkow@18049
   841
proof (induct xs)
nipkow@26073
   842
  case Nil thus ?case by simp
nipkow@26073
   843
next
nipkow@26073
   844
  case Cons thus ?case by (auto intro: Cons_eq_appendI)
nipkow@26073
   845
qed
nipkow@26073
   846
nipkow@26073
   847
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
nipkow@26073
   848
by (metis Un_upper2 insert_subset set.simps(2) set_append split_list)
nipkow@26073
   849
nipkow@26073
   850
lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys"
nipkow@26073
   851
proof (induct xs)
nipkow@26073
   852
  case Nil thus ?case by simp
nipkow@18049
   853
next
nipkow@18049
   854
  case (Cons a xs)
nipkow@18049
   855
  show ?case
nipkow@18049
   856
  proof cases
wenzelm@25221
   857
    assume "x = a" thus ?case using Cons by fastsimp
nipkow@18049
   858
  next
nipkow@26073
   859
    assume "x \<noteq> a" thus ?case using Cons by(fastsimp intro!: Cons_eq_appendI)
nipkow@26073
   860
  qed
nipkow@26073
   861
qed
nipkow@26073
   862
nipkow@26073
   863
lemma in_set_conv_decomp_first:
nipkow@26073
   864
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"
nipkow@26073
   865
by (metis in_set_conv_decomp split_list_first)
nipkow@26073
   866
nipkow@26073
   867
lemma split_list_last: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs"
nipkow@26073
   868
proof (induct xs rule:rev_induct)
nipkow@26073
   869
  case Nil thus ?case by simp
nipkow@26073
   870
next
nipkow@26073
   871
  case (snoc a xs)
nipkow@26073
   872
  show ?case
nipkow@26073
   873
  proof cases
nipkow@26073
   874
    assume "x = a" thus ?case using snoc by simp (metis ex_in_conv set_empty2)
nipkow@26073
   875
  next
nipkow@26073
   876
    assume "x \<noteq> a" thus ?case using snoc by fastsimp
nipkow@18049
   877
  qed
nipkow@18049
   878
qed
nipkow@18049
   879
nipkow@26073
   880
lemma in_set_conv_decomp_last:
nipkow@26073
   881
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set zs)"
nipkow@26073
   882
by (metis in_set_conv_decomp split_list_last)
nipkow@26073
   883
nipkow@26073
   884
lemma split_list_prop: "\<exists>x \<in> set xs. P x \<Longrightarrow> \<exists>ys x zs. xs = ys @ x # zs & P x"
nipkow@26073
   885
proof (induct xs)
nipkow@26073
   886
  case Nil thus ?case by simp
nipkow@26073
   887
next
nipkow@26073
   888
  case Cons thus ?case
nipkow@26073
   889
    by(simp add:Bex_def)(metis append_Cons append.simps(1))
nipkow@26073
   890
qed
nipkow@26073
   891
nipkow@26073
   892
lemma split_list_propE:
nipkow@26073
   893
assumes "\<exists>x \<in> set xs. P x"
nipkow@26073
   894
obtains ys x zs where "xs = ys @ x # zs" and "P x"
nipkow@26073
   895
by(metis split_list_prop[OF assms])
nipkow@26073
   896
nipkow@26073
   897
nipkow@26073
   898
lemma split_list_first_prop:
nipkow@26073
   899
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
   900
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y)"
nipkow@26073
   901
proof(induct xs)
nipkow@26073
   902
  case Nil thus ?case by simp
nipkow@26073
   903
next
nipkow@26073
   904
  case (Cons x xs)
nipkow@26073
   905
  show ?case
nipkow@26073
   906
  proof cases
nipkow@26073
   907
    assume "P x"
nipkow@26073
   908
    thus ?thesis by simp (metis Un_upper1 contra_subsetD in_set_conv_decomp_first self_append_conv2 set_append)
nipkow@26073
   909
  next
nipkow@26073
   910
    assume "\<not> P x"
nipkow@26073
   911
    hence "\<exists>x\<in>set xs. P x" using Cons(2) by simp
nipkow@26073
   912
    thus ?thesis using `\<not> P x` Cons(1) by (metis append_Cons set_ConsD)
nipkow@26073
   913
  qed
nipkow@26073
   914
qed
nipkow@26073
   915
nipkow@26073
   916
lemma split_list_first_propE:
nipkow@26073
   917
assumes "\<exists>x \<in> set xs. P x"
nipkow@26073
   918
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>y \<in> set ys. \<not> P y"
nipkow@26073
   919
by(metis split_list_first_prop[OF assms])
nipkow@26073
   920
nipkow@26073
   921
lemma split_list_first_prop_iff:
nipkow@26073
   922
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
   923
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>y \<in> set ys. \<not> P y))"
nipkow@26073
   924
by(metis split_list_first_prop[where P=P] in_set_conv_decomp)
nipkow@26073
   925
nipkow@26073
   926
nipkow@26073
   927
lemma split_list_last_prop:
nipkow@26073
   928
  "\<exists>x \<in> set xs. P x \<Longrightarrow>
nipkow@26073
   929
   \<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z)"
nipkow@26073
   930
proof(induct xs rule:rev_induct)
nipkow@26073
   931
  case Nil thus ?case by simp
nipkow@26073
   932
next
nipkow@26073
   933
  case (snoc x xs)
nipkow@26073
   934
  show ?case
nipkow@26073
   935
  proof cases
nipkow@26073
   936
    assume "P x" thus ?thesis by (metis emptyE set_empty)
nipkow@26073
   937
  next
nipkow@26073
   938
    assume "\<not> P x"
nipkow@26073
   939
    hence "\<exists>x\<in>set xs. P x" using snoc(2) by simp
nipkow@26073
   940
    thus ?thesis using `\<not> P x` snoc(1) by fastsimp
nipkow@26073
   941
  qed
nipkow@26073
   942
qed
nipkow@26073
   943
nipkow@26073
   944
lemma split_list_last_propE:
nipkow@26073
   945
assumes "\<exists>x \<in> set xs. P x"
nipkow@26073
   946
obtains ys x zs where "xs = ys @ x # zs" and "P x" and "\<forall>z \<in> set zs. \<not> P z"
nipkow@26073
   947
by(metis split_list_last_prop[OF assms])
nipkow@26073
   948
nipkow@26073
   949
lemma split_list_last_prop_iff:
nipkow@26073
   950
  "(\<exists>x \<in> set xs. P x) \<longleftrightarrow>
nipkow@26073
   951
   (\<exists>ys x zs. xs = ys@x#zs \<and> P x \<and> (\<forall>z \<in> set zs. \<not> P z))"
nipkow@26073
   952
by(metis split_list_last_prop[where P=P] in_set_conv_decomp)
nipkow@26073
   953
nipkow@26073
   954
nipkow@26073
   955
lemma finite_list: "finite A ==> EX xs. set xs = A"
paulson@13508
   956
apply (erule finite_induct, auto)
nipkow@26073
   957
apply (metis set.simps(2))
paulson@13508
   958
done
paulson@13508
   959
kleing@14388
   960
lemma card_length: "card (set xs) \<le> length xs"
kleing@14388
   961
by (induct xs) (auto simp add: card_insert_if)
wenzelm@13114
   962
haftmann@26442
   963
lemma set_minus_filter_out:
haftmann@26442
   964
  "set xs - {y} = set (filter (\<lambda>x. \<not> (x = y)) xs)"
haftmann@26442
   965
  by (induct xs) auto
paulson@15168
   966
nipkow@15392
   967
subsubsection {* @{text filter} *}
wenzelm@13114
   968
wenzelm@13142
   969
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
nipkow@13145
   970
by (induct xs) auto
wenzelm@13114
   971
nipkow@15305
   972
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"
nipkow@15305
   973
by (induct xs) simp_all
nipkow@15305
   974
wenzelm@13142
   975
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
nipkow@13145
   976
by (induct xs) auto
wenzelm@13114
   977
nipkow@16998
   978
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"
nipkow@16998
   979
by (induct xs) (auto simp add: le_SucI)
nipkow@16998
   980
nipkow@18423
   981
lemma sum_length_filter_compl:
nipkow@18423
   982
  "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"
nipkow@18423
   983
by(induct xs) simp_all
nipkow@18423
   984
wenzelm@13142
   985
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
nipkow@13145
   986
by (induct xs) auto
wenzelm@13114
   987
wenzelm@13142
   988
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
nipkow@13145
   989
by (induct xs) auto
wenzelm@13114
   990
nipkow@16998
   991
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
nipkow@24349
   992
by (induct xs) simp_all
nipkow@16998
   993
nipkow@16998
   994
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"
nipkow@16998
   995
apply (induct xs)
nipkow@16998
   996
 apply auto
nipkow@16998
   997
apply(cut_tac P=P and xs=xs in length_filter_le)
nipkow@16998
   998
apply simp
nipkow@16998
   999
done
wenzelm@13114
  1000
nipkow@16965
  1001
lemma filter_map:
nipkow@16965
  1002
  "filter P (map f xs) = map f (filter (P o f) xs)"
nipkow@16965
  1003
by (induct xs) simp_all
nipkow@16965
  1004
nipkow@16965
  1005
lemma length_filter_map[simp]:
nipkow@16965
  1006
  "length (filter P (map f xs)) = length(filter (P o f) xs)"
nipkow@16965
  1007
by (simp add:filter_map)
nipkow@16965
  1008
wenzelm@13142
  1009
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
nipkow@13145
  1010
by auto
wenzelm@13114
  1011
nipkow@15246
  1012
lemma length_filter_less:
nipkow@15246
  1013
  "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"
nipkow@15246
  1014
proof (induct xs)
nipkow@15246
  1015
  case Nil thus ?case by simp
nipkow@15246
  1016
next
nipkow@15246
  1017
  case (Cons x xs) thus ?case
nipkow@15246
  1018
    apply (auto split:split_if_asm)
nipkow@15246
  1019
    using length_filter_le[of P xs] apply arith
nipkow@15246
  1020
  done
nipkow@15246
  1021
qed
wenzelm@13114
  1022
nipkow@15281
  1023
lemma length_filter_conv_card:
nipkow@15281
  1024
 "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
nipkow@15281
  1025
proof (induct xs)
nipkow@15281
  1026
  case Nil thus ?case by simp
nipkow@15281
  1027
next
nipkow@15281
  1028
  case (Cons x xs)
nipkow@15281
  1029
  let ?S = "{i. i < length xs & p(xs!i)}"
nipkow@15281
  1030
  have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)
nipkow@15281
  1031
  show ?case (is "?l = card ?S'")
nipkow@15281
  1032
  proof (cases)
nipkow@15281
  1033
    assume "p x"
nipkow@15281
  1034
    hence eq: "?S' = insert 0 (Suc ` ?S)"
nipkow@25162
  1035
      by(auto simp: image_def split:nat.split dest:gr0_implies_Suc)
nipkow@15281
  1036
    have "length (filter p (x # xs)) = Suc(card ?S)"
wenzelm@23388
  1037
      using Cons `p x` by simp
nipkow@15281
  1038
    also have "\<dots> = Suc(card(Suc ` ?S))" using fin
nipkow@15281
  1039
      by (simp add: card_image inj_Suc)
nipkow@15281
  1040
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1041
      by (simp add:card_insert_if) (simp add:image_def)
nipkow@15281
  1042
    finally show ?thesis .
nipkow@15281
  1043
  next
nipkow@15281
  1044
    assume "\<not> p x"
nipkow@15281
  1045
    hence eq: "?S' = Suc ` ?S"
nipkow@25162
  1046
      by(auto simp add: image_def split:nat.split elim:lessE)
nipkow@15281
  1047
    have "length (filter p (x # xs)) = card ?S"
wenzelm@23388
  1048
      using Cons `\<not> p x` by simp
nipkow@15281
  1049
    also have "\<dots> = card(Suc ` ?S)" using fin
nipkow@15281
  1050
      by (simp add: card_image inj_Suc)
nipkow@15281
  1051
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
  1052
      by (simp add:card_insert_if)
nipkow@15281
  1053
    finally show ?thesis .
nipkow@15281
  1054
  qed
nipkow@15281
  1055
qed
nipkow@15281
  1056
nipkow@17629
  1057
lemma Cons_eq_filterD:
nipkow@17629
  1058
 "x#xs = filter P ys \<Longrightarrow>
nipkow@17629
  1059
  \<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
  1060
  (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")
nipkow@17629
  1061
proof(induct ys)
nipkow@17629
  1062
  case Nil thus ?case by simp
nipkow@17629
  1063
next
nipkow@17629
  1064
  case (Cons y ys)
nipkow@17629
  1065
  show ?case (is "\<exists>x. ?Q x")
nipkow@17629
  1066
  proof cases
nipkow@17629
  1067
    assume Py: "P y"
nipkow@17629
  1068
    show ?thesis
nipkow@17629
  1069
    proof cases
wenzelm@25221
  1070
      assume "x = y"
wenzelm@25221
  1071
      with Py Cons.prems have "?Q []" by simp
wenzelm@25221
  1072
      then show ?thesis ..
nipkow@17629
  1073
    next
wenzelm@25221
  1074
      assume "x \<noteq> y"
wenzelm@25221
  1075
      with Py Cons.prems show ?thesis by simp
nipkow@17629
  1076
    qed
nipkow@17629
  1077
  next
wenzelm@25221
  1078
    assume "\<not> P y"
wenzelm@25221
  1079
    with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastsimp
wenzelm@25221
  1080
    then have "?Q (y#us)" by simp
wenzelm@25221
  1081
    then show ?thesis ..
nipkow@17629
  1082
  qed
nipkow@17629
  1083
qed
nipkow@17629
  1084
nipkow@17629
  1085
lemma filter_eq_ConsD:
nipkow@17629
  1086
 "filter P ys = x#xs \<Longrightarrow>
nipkow@17629
  1087
  \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"
nipkow@17629
  1088
by(rule Cons_eq_filterD) simp
nipkow@17629
  1089
nipkow@17629
  1090
lemma filter_eq_Cons_iff:
nipkow@17629
  1091
 "(filter P ys = x#xs) =
nipkow@17629
  1092
  (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"
nipkow@17629
  1093
by(auto dest:filter_eq_ConsD)
nipkow@17629
  1094
nipkow@17629
  1095
lemma Cons_eq_filter_iff:
nipkow@17629
  1096
 "(x#xs = filter P ys) =
nipkow@17629
  1097
  (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"
nipkow@17629
  1098
by(auto dest:Cons_eq_filterD)
nipkow@17629
  1099
krauss@19770
  1100
lemma filter_cong[fundef_cong, recdef_cong]:
nipkow@17501
  1101
 "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"
nipkow@17501
  1102
apply simp
nipkow@17501
  1103
apply(erule thin_rl)
nipkow@17501
  1104
by (induct ys) simp_all
nipkow@17501
  1105
nipkow@15281
  1106
haftmann@26442
  1107
subsubsection {* List partitioning *}
haftmann@26442
  1108
haftmann@26442
  1109
primrec partition :: "('a \<Rightarrow> bool) \<Rightarrow>'a list \<Rightarrow> 'a list \<times> 'a list" where
haftmann@26442
  1110
  "partition P [] = ([], [])"
haftmann@26442
  1111
  | "partition P (x # xs) = 
haftmann@26442
  1112
      (let (yes, no) = partition P xs
haftmann@26442
  1113
      in if P x then (x # yes, no) else (yes, x # no))"
haftmann@26442
  1114
haftmann@26442
  1115
lemma partition_filter1:
haftmann@26442
  1116
    "fst (partition P xs) = filter P xs"
haftmann@26442
  1117
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1118
haftmann@26442
  1119
lemma partition_filter2:
haftmann@26442
  1120
    "snd (partition P xs) = filter (Not o P) xs"
haftmann@26442
  1121
by (induct xs) (auto simp add: Let_def split_def)
haftmann@26442
  1122
haftmann@26442
  1123
lemma partition_P:
haftmann@26442
  1124
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1125
  shows "(\<forall>p \<in> set yes.  P p) \<and> (\<forall>p  \<in> set no. \<not> P p)"
haftmann@26442
  1126
proof -
haftmann@26442
  1127
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1128
    by simp_all
haftmann@26442
  1129
  then show ?thesis by (simp_all add: partition_filter1 partition_filter2)
haftmann@26442
  1130
qed
haftmann@26442
  1131
haftmann@26442
  1132
lemma partition_set:
haftmann@26442
  1133
  assumes "partition P xs = (yes, no)"
haftmann@26442
  1134
  shows "set yes \<union> set no = set xs"
haftmann@26442
  1135
proof -
haftmann@26442
  1136
  from assms have "yes = fst (partition P xs)" and "no = snd (partition P xs)"
haftmann@26442
  1137
    by simp_all
haftmann@26442
  1138
  then show ?thesis by (auto simp add: partition_filter1 partition_filter2) 
haftmann@26442
  1139
qed
haftmann@26442
  1140
haftmann@26442
  1141
nipkow@15392
  1142
subsubsection {* @{text concat} *}
wenzelm@13114
  1143
wenzelm@13142
  1144
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
nipkow@13145
  1145
by (induct xs) auto
wenzelm@13114
  1146
paulson@18447
  1147
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1148
by (induct xss) auto
wenzelm@13114
  1149
paulson@18447
  1150
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1151
by (induct xss) auto
wenzelm@13114
  1152
nipkow@24308
  1153
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)"
nipkow@13145
  1154
by (induct xs) auto
wenzelm@13114
  1155
nipkow@24476
  1156
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs"
nipkow@24349
  1157
by (induct xs) auto
nipkow@24349
  1158
wenzelm@13142
  1159
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
nipkow@13145
  1160
by (induct xs) auto
wenzelm@13114
  1161
wenzelm@13142
  1162
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
nipkow@13145
  1163
by (induct xs) auto
wenzelm@13114
  1164
wenzelm@13142
  1165
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
nipkow@13145
  1166
by (induct xs) auto
wenzelm@13114
  1167
wenzelm@13114
  1168
nipkow@15392
  1169
subsubsection {* @{text nth} *}
wenzelm@13114
  1170
wenzelm@13142
  1171
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
nipkow@13145
  1172
by auto
wenzelm@13114
  1173
wenzelm@13142
  1174
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
nipkow@13145
  1175
by auto
wenzelm@13114
  1176
wenzelm@13142
  1177
declare nth.simps [simp del]
wenzelm@13114
  1178
wenzelm@13114
  1179
lemma nth_append:
nipkow@24526
  1180
  "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
nipkow@24526
  1181
apply (induct xs arbitrary: n, simp)
paulson@14208
  1182
apply (case_tac n, auto)
nipkow@13145
  1183
done
wenzelm@13114
  1184
nipkow@14402
  1185
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"
wenzelm@25221
  1186
by (induct xs) auto
nipkow@14402
  1187
nipkow@14402
  1188
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"
wenzelm@25221
  1189
by (induct xs) auto
nipkow@14402
  1190
nipkow@24526
  1191
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
nipkow@24526
  1192
apply (induct xs arbitrary: n, simp)
paulson@14208
  1193
apply (case_tac n, auto)
nipkow@13145
  1194
done
wenzelm@13114
  1195
nipkow@18423
  1196
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
nipkow@18423
  1197
by(cases xs) simp_all
nipkow@18423
  1198
nipkow@18049
  1199
nipkow@18049
  1200
lemma list_eq_iff_nth_eq:
nipkow@24526
  1201
 "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"
nipkow@24526
  1202
apply(induct xs arbitrary: ys)
paulson@24632
  1203
 apply force
nipkow@18049
  1204
apply(case_tac ys)
nipkow@18049
  1205
 apply simp
nipkow@18049
  1206
apply(simp add:nth_Cons split:nat.split)apply blast
nipkow@18049
  1207
done
nipkow@18049
  1208
wenzelm@13142
  1209
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
paulson@15251
  1210
apply (induct xs, simp, simp)
nipkow@13145
  1211
apply safe
paulson@24632
  1212
apply (metis nat_case_0 nth.simps zero_less_Suc)
paulson@24632
  1213
apply (metis less_Suc_eq_0_disj nth_Cons_Suc)
paulson@14208
  1214
apply (case_tac i, simp)
paulson@24632
  1215
apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff)
nipkow@13145
  1216
done
wenzelm@13114
  1217
nipkow@17501
  1218
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"
nipkow@17501
  1219
by(auto simp:set_conv_nth)
nipkow@17501
  1220
nipkow@13145
  1221
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
nipkow@13145
  1222
by (auto simp add: set_conv_nth)
wenzelm@13114
  1223
wenzelm@13142
  1224
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
nipkow@13145
  1225
by (auto simp add: set_conv_nth)
wenzelm@13114
  1226
wenzelm@13114
  1227
lemma all_nth_imp_all_set:
nipkow@13145
  1228
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
nipkow@13145
  1229
by (auto simp add: set_conv_nth)
wenzelm@13114
  1230
wenzelm@13114
  1231
lemma all_set_conv_all_nth:
nipkow@13145
  1232
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
nipkow@13145
  1233
by (auto simp add: set_conv_nth)
wenzelm@13114
  1234
kleing@25296
  1235
lemma rev_nth:
kleing@25296
  1236
  "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)"
kleing@25296
  1237
proof (induct xs arbitrary: n)
kleing@25296
  1238
  case Nil thus ?case by simp
kleing@25296
  1239
next
kleing@25296
  1240
  case (Cons x xs)
kleing@25296
  1241
  hence n: "n < Suc (length xs)" by simp
kleing@25296
  1242
  moreover
kleing@25296
  1243
  { assume "n < length xs"
kleing@25296
  1244
    with n obtain n' where "length xs - n = Suc n'"
kleing@25296
  1245
      by (cases "length xs - n", auto)
kleing@25296
  1246
    moreover
kleing@25296
  1247
    then have "length xs - Suc n = n'" by simp
kleing@25296
  1248
    ultimately
kleing@25296
  1249
    have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp
kleing@25296
  1250
  }
kleing@25296
  1251
  ultimately
kleing@25296
  1252
  show ?case by (clarsimp simp add: Cons nth_append)
kleing@25296
  1253
qed
wenzelm@13114
  1254
nipkow@15392
  1255
subsubsection {* @{text list_update} *}
wenzelm@13114
  1256
nipkow@24526
  1257
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs"
nipkow@24526
  1258
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1259
wenzelm@13114
  1260
lemma nth_list_update:
nipkow@24526
  1261
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
nipkow@24526
  1262
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1263
wenzelm@13142
  1264
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
nipkow@13145
  1265
by (simp add: nth_list_update)
wenzelm@13114
  1266
nipkow@24526
  1267
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j"
nipkow@24526
  1268
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1269
nipkow@24526
  1270
lemma list_update_id[simp]: "xs[i := xs!i] = xs"
nipkow@24526
  1271
by (induct xs arbitrary: i) (simp_all split:nat.splits)
nipkow@24526
  1272
nipkow@24526
  1273
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"
nipkow@24526
  1274
apply (induct xs arbitrary: i)
nipkow@17501
  1275
 apply simp
nipkow@17501
  1276
apply (case_tac i)
nipkow@17501
  1277
apply simp_all
nipkow@17501
  1278
done
nipkow@17501
  1279
wenzelm@13114
  1280
lemma list_update_same_conv:
nipkow@24526
  1281
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
nipkow@24526
  1282
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1283
nipkow@14187
  1284
lemma list_update_append1:
nipkow@24526
  1285
 "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
nipkow@24526
  1286
apply (induct xs arbitrary: i, simp)
nipkow@14187
  1287
apply(simp split:nat.split)
nipkow@14187
  1288
done
nipkow@14187
  1289
kleing@15868
  1290
lemma list_update_append:
nipkow@24526
  1291
  "(xs @ ys) [n:= x] = 
kleing@15868
  1292
  (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"
nipkow@24526
  1293
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1294
nipkow@14402
  1295
lemma list_update_length [simp]:
nipkow@14402
  1296
 "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"
nipkow@14402
  1297
by (induct xs, auto)
nipkow@14402
  1298
wenzelm@13114
  1299
lemma update_zip:
nipkow@24526
  1300
  "length xs = length ys ==>
nipkow@24526
  1301
  (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
nipkow@24526
  1302
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split)
nipkow@24526
  1303
nipkow@24526
  1304
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)"
nipkow@24526
  1305
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1306
wenzelm@13114
  1307
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
nipkow@13145
  1308
by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
  1309
nipkow@24526
  1310
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"
nipkow@24526
  1311
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1312
haftmann@24796
  1313
lemma list_update_overwrite:
haftmann@24796
  1314
  "xs [i := x, i := y] = xs [i := y]"
haftmann@24796
  1315
apply (induct xs arbitrary: i)
haftmann@24796
  1316
apply simp
haftmann@24796
  1317
apply (case_tac i)
haftmann@24796
  1318
apply simp_all
haftmann@24796
  1319
done
haftmann@24796
  1320
haftmann@24796
  1321
lemma list_update_swap:
haftmann@24796
  1322
  "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]"
haftmann@24796
  1323
apply (induct xs arbitrary: i i')
haftmann@24796
  1324
apply simp
haftmann@24796
  1325
apply (case_tac i, case_tac i')
haftmann@24796
  1326
apply auto
haftmann@24796
  1327
apply (case_tac i')
haftmann@24796
  1328
apply auto
haftmann@24796
  1329
done
haftmann@24796
  1330
wenzelm@13114
  1331
nipkow@15392
  1332
subsubsection {* @{text last} and @{text butlast} *}
wenzelm@13114
  1333
wenzelm@13142
  1334
lemma last_snoc [simp]: "last (xs @ [x]) = x"
nipkow@13145
  1335
by (induct xs) auto
wenzelm@13114
  1336
wenzelm@13142
  1337
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
nipkow@13145
  1338
by (induct xs) auto
wenzelm@13114
  1339
nipkow@14302
  1340
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
nipkow@14302
  1341
by(simp add:last.simps)
nipkow@14302
  1342
nipkow@14302
  1343
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
nipkow@14302
  1344
by(simp add:last.simps)
nipkow@14302
  1345
nipkow@14302
  1346
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
nipkow@14302
  1347
by (induct xs) (auto)
nipkow@14302
  1348
nipkow@14302
  1349
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
nipkow@14302
  1350
by(simp add:last_append)
nipkow@14302
  1351
nipkow@14302
  1352
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
nipkow@14302
  1353
by(simp add:last_append)
nipkow@14302
  1354
nipkow@17762
  1355
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
nipkow@17762
  1356
by(rule rev_exhaust[of xs]) simp_all
nipkow@17762
  1357
nipkow@17762
  1358
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
nipkow@17762
  1359
by(cases xs) simp_all
nipkow@17762
  1360
nipkow@17765
  1361
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"
nipkow@17765
  1362
by (induct as) auto
nipkow@17762
  1363
wenzelm@13142
  1364
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
nipkow@13145
  1365
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1366
wenzelm@13114
  1367
lemma butlast_append:
nipkow@24526
  1368
  "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
nipkow@24526
  1369
by (induct xs arbitrary: ys) auto
wenzelm@13114
  1370
wenzelm@13142
  1371
lemma append_butlast_last_id [simp]:
nipkow@13145
  1372
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
nipkow@13145
  1373
by (induct xs) auto
wenzelm@13114
  1374
wenzelm@13142
  1375
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
nipkow@13145
  1376
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1377
wenzelm@13114
  1378
lemma in_set_butlast_appendI:
nipkow@13145
  1379
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
nipkow@13145
  1380
by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
  1381
nipkow@24526
  1382
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
nipkow@24526
  1383
apply (induct xs arbitrary: n)
nipkow@17501
  1384
 apply simp
nipkow@17501
  1385
apply (auto split:nat.split)
nipkow@17501
  1386
done
nipkow@17501
  1387
nipkow@17589
  1388
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
nipkow@17589
  1389
by(induct xs)(auto simp:neq_Nil_conv)
nipkow@17589
  1390
haftmann@24796
  1391
nipkow@15392
  1392
subsubsection {* @{text take} and @{text drop} *}
wenzelm@13114
  1393
wenzelm@13142
  1394
lemma take_0 [simp]: "take 0 xs = []"
nipkow@13145
  1395
by (induct xs) auto
wenzelm@13114
  1396
wenzelm@13142
  1397
lemma drop_0 [simp]: "drop 0 xs = xs"
nipkow@13145
  1398
by (induct xs) auto
wenzelm@13114
  1399
wenzelm@13142
  1400
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
nipkow@13145
  1401
by simp
wenzelm@13114
  1402
wenzelm@13142
  1403
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
nipkow@13145
  1404
by simp
wenzelm@13114
  1405
wenzelm@13142
  1406
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
  1407
nipkow@15110
  1408
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"
nipkow@15110
  1409
by(clarsimp simp add:neq_Nil_conv)
nipkow@15110
  1410
nipkow@14187
  1411
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
nipkow@14187
  1412
by(cases xs, simp_all)
nipkow@14187
  1413
nipkow@24526
  1414
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)"
nipkow@24526
  1415
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split)
nipkow@24526
  1416
nipkow@24526
  1417
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y"
nipkow@24526
  1418
apply (induct xs arbitrary: n, simp)
nipkow@14187
  1419
apply(simp add:drop_Cons nth_Cons split:nat.splits)
nipkow@14187
  1420
done
nipkow@14187
  1421
nipkow@13913
  1422
lemma take_Suc_conv_app_nth:
nipkow@24526
  1423
  "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
nipkow@24526
  1424
apply (induct xs arbitrary: i, simp)
paulson@14208
  1425
apply (case_tac i, auto)
nipkow@13913
  1426
done
nipkow@13913
  1427
mehta@14591
  1428
lemma drop_Suc_conv_tl:
nipkow@24526
  1429
  "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
nipkow@24526
  1430
apply (induct xs arbitrary: i, simp)
mehta@14591
  1431
apply (case_tac i, auto)
mehta@14591
  1432
done
mehta@14591
  1433
nipkow@24526
  1434
lemma length_take [simp]: "length (take n xs) = min (length xs) n"
nipkow@24526
  1435
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1436
nipkow@24526
  1437
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"
nipkow@24526
  1438
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1439
nipkow@24526
  1440
lemma take_all [simp]: "length xs <= n ==> take n xs = xs"
nipkow@24526
  1441
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1442
nipkow@24526
  1443
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []"
nipkow@24526
  1444
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1445
wenzelm@13142
  1446
lemma take_append [simp]:
nipkow@24526
  1447
  "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
nipkow@24526
  1448
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1449
wenzelm@13142
  1450
lemma drop_append [simp]:
nipkow@24526
  1451
  "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
nipkow@24526
  1452
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1453
nipkow@24526
  1454
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"
nipkow@24526
  1455
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1456
apply (case_tac xs, auto)
nipkow@15236
  1457
apply (case_tac n, auto)
nipkow@13145
  1458
done
wenzelm@13114
  1459
nipkow@24526
  1460
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"
nipkow@24526
  1461
apply (induct m arbitrary: xs, auto)
paulson@14208
  1462
apply (case_tac xs, auto)
nipkow@13145
  1463
done
wenzelm@13114
  1464
nipkow@24526
  1465
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"
nipkow@24526
  1466
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1467
apply (case_tac xs, auto)
nipkow@13145
  1468
done
wenzelm@13114
  1469
nipkow@24526
  1470
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"
nipkow@24526
  1471
apply(induct xs arbitrary: m n)
nipkow@14802
  1472
 apply simp
nipkow@14802
  1473
apply(simp add: take_Cons drop_Cons split:nat.split)
nipkow@14802
  1474
done
nipkow@14802
  1475
nipkow@24526
  1476
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"
nipkow@24526
  1477
apply (induct n arbitrary: xs, auto)
paulson@14208
  1478
apply (case_tac xs, auto)
nipkow@13145
  1479
done
wenzelm@13114
  1480
nipkow@24526
  1481
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])"
nipkow@24526
  1482
apply(induct xs arbitrary: n)
nipkow@15110
  1483
 apply simp
nipkow@15110
  1484
apply(simp add:take_Cons split:nat.split)
nipkow@15110
  1485
done
nipkow@15110
  1486
nipkow@24526
  1487
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)"
nipkow@24526
  1488
apply(induct xs arbitrary: n)
nipkow@15110
  1489
apply simp
nipkow@15110
  1490
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1491
done
nipkow@15110
  1492
nipkow@24526
  1493
lemma take_map: "take n (map f xs) = map f (take n xs)"
nipkow@24526
  1494
apply (induct n arbitrary: xs, auto)
paulson@14208
  1495
apply (case_tac xs, auto)
nipkow@13145
  1496
done
wenzelm@13114
  1497
nipkow@24526
  1498
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"
nipkow@24526
  1499
apply (induct n arbitrary: xs, auto)
paulson@14208
  1500
apply (case_tac xs, auto)
nipkow@13145
  1501
done
wenzelm@13114
  1502
nipkow@24526
  1503
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@24526
  1504
apply (induct xs arbitrary: i, auto)
paulson@14208
  1505
apply (case_tac i, auto)
nipkow@13145
  1506
done
wenzelm@13114
  1507
nipkow@24526
  1508
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@24526
  1509
apply (induct xs arbitrary: i, auto)
paulson@14208
  1510
apply (case_tac i, auto)
nipkow@13145
  1511
done
wenzelm@13114
  1512
nipkow@24526
  1513
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i"
nipkow@24526
  1514
apply (induct xs arbitrary: i n, auto)
paulson@14208
  1515
apply (case_tac n, blast)
paulson@14208
  1516
apply (case_tac i, auto)
nipkow@13145
  1517
done
wenzelm@13114
  1518
wenzelm@13142
  1519
lemma nth_drop [simp]:
nipkow@24526
  1520
  "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
nipkow@24526
  1521
apply (induct n arbitrary: xs i, auto)
paulson@14208
  1522
apply (case_tac xs, auto)
nipkow@13145
  1523
done
nipkow@3507
  1524
nipkow@18423
  1525
lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n"
nipkow@18423
  1526
by(simp add: hd_conv_nth)
nipkow@18423
  1527
nipkow@24526
  1528
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"
nipkow@24526
  1529
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)
nipkow@24526
  1530
nipkow@24526
  1531
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"
nipkow@24526
  1532
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)
nipkow@14025
  1533
nipkow@14187
  1534
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1535
using set_take_subset by fast
nipkow@14187
  1536
nipkow@14187
  1537
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1538
using set_drop_subset by fast
nipkow@14187
  1539
wenzelm@13114
  1540
lemma append_eq_conv_conj:
nipkow@24526
  1541
  "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
nipkow@24526
  1542
apply (induct xs arbitrary: zs, simp, clarsimp)
paulson@14208
  1543
apply (case_tac zs, auto)
nipkow@13145
  1544
done
wenzelm@13142
  1545
nipkow@24526
  1546
lemma take_add: 
nipkow@24526
  1547
  "i+j \<le> length(xs) \<Longrightarrow> take (i+j) xs = take i xs @ take j (drop i xs)"
nipkow@24526
  1548
apply (induct xs arbitrary: i, auto) 
nipkow@24526
  1549
apply (case_tac i, simp_all)
paulson@14050
  1550
done
paulson@14050
  1551
nipkow@14300
  1552
lemma append_eq_append_conv_if:
nipkow@24526
  1553
 "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
nipkow@14300
  1554
  (if size xs\<^isub>1 \<le> size ys\<^isub>1
nipkow@14300
  1555
   then xs\<^isub>1 = take (size xs\<^isub>1) ys\<^isub>1 \<and> xs\<^isub>2 = drop (size xs\<^isub>1) ys\<^isub>1 @ ys\<^isub>2
nipkow@14300
  1556
   else take (size ys\<^isub>1) xs\<^isub>1 = ys\<^isub>1 \<and> drop (size ys\<^isub>1) xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>2)"
nipkow@24526
  1557
apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1)
nipkow@14300
  1558
 apply simp
nipkow@14300
  1559
apply(case_tac ys\<^isub>1)
nipkow@14300
  1560
apply simp_all
nipkow@14300
  1561
done
nipkow@14300
  1562
nipkow@15110
  1563
lemma take_hd_drop:
nipkow@24526
  1564
  "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (n+1) xs"
nipkow@24526
  1565
apply(induct xs arbitrary: n)
nipkow@15110
  1566
apply simp
nipkow@15110
  1567
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1568
done
nipkow@15110
  1569
nipkow@17501
  1570
lemma id_take_nth_drop:
nipkow@17501
  1571
 "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 
nipkow@17501
  1572
proof -
nipkow@17501
  1573
  assume si: "i < length xs"
nipkow@17501
  1574
  hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
nipkow@17501
  1575
  moreover
nipkow@17501
  1576
  from si have "take (Suc i) xs = take i xs @ [xs!i]"
nipkow@17501
  1577
    apply (rule_tac take_Suc_conv_app_nth) by arith
nipkow@17501
  1578
  ultimately show ?thesis by auto
nipkow@17501
  1579
qed
nipkow@17501
  1580
  
nipkow@17501
  1581
lemma upd_conv_take_nth_drop:
nipkow@17501
  1582
 "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1583
proof -
nipkow@17501
  1584
  assume i: "i < length xs"
nipkow@17501
  1585
  have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
nipkow@17501
  1586
    by(rule arg_cong[OF id_take_nth_drop[OF i]])
nipkow@17501
  1587
  also have "\<dots> = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1588
    using i by (simp add: list_update_append)
nipkow@17501
  1589
  finally show ?thesis .
nipkow@17501
  1590
qed
nipkow@17501
  1591
haftmann@24796
  1592
lemma nth_drop':
haftmann@24796
  1593
  "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs"
haftmann@24796
  1594
apply (induct i arbitrary: xs)
haftmann@24796
  1595
apply (simp add: neq_Nil_conv)
haftmann@24796
  1596
apply (erule exE)+
haftmann@24796
  1597
apply simp
haftmann@24796
  1598
apply (case_tac xs)
haftmann@24796
  1599
apply simp_all
haftmann@24796
  1600
done
haftmann@24796
  1601
wenzelm@13114
  1602
nipkow@15392
  1603
subsubsection {* @{text takeWhile} and @{text dropWhile} *}
wenzelm@13114
  1604
wenzelm@13142
  1605
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
nipkow@13145
  1606
by (induct xs) auto
wenzelm@13114
  1607
wenzelm@13142
  1608
lemma takeWhile_append1 [simp]:
nipkow@13145
  1609
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
nipkow@13145
  1610
by (induct xs) auto
wenzelm@13114
  1611
wenzelm@13142
  1612
lemma takeWhile_append2 [simp]:
nipkow@13145
  1613
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
nipkow@13145
  1614
by (induct xs) auto
wenzelm@13114
  1615
wenzelm@13142
  1616
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
nipkow@13145
  1617
by (induct xs) auto
wenzelm@13114
  1618
wenzelm@13142
  1619
lemma dropWhile_append1 [simp]:
nipkow@13145
  1620
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
nipkow@13145
  1621
by (induct xs) auto
wenzelm@13114
  1622
wenzelm@13142
  1623
lemma dropWhile_append2 [simp]:
nipkow@13145
  1624
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
nipkow@13145
  1625
by (induct xs) auto
wenzelm@13114
  1626
krauss@23971
  1627
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
nipkow@13145
  1628
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1629
nipkow@13913
  1630
lemma takeWhile_eq_all_conv[simp]:
nipkow@13913
  1631
 "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1632
by(induct xs, auto)
nipkow@13913
  1633
nipkow@13913
  1634
lemma dropWhile_eq_Nil_conv[simp]:
nipkow@13913
  1635
 "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1636
by(induct xs, auto)
nipkow@13913
  1637
nipkow@13913
  1638
lemma dropWhile_eq_Cons_conv:
nipkow@13913
  1639
 "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
nipkow@13913
  1640
by(induct xs, auto)
nipkow@13913
  1641
nipkow@17501
  1642
text{* The following two lemmmas could be generalized to an arbitrary
nipkow@17501
  1643
property. *}
nipkow@17501
  1644
nipkow@17501
  1645
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1646
 takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
nipkow@17501
  1647
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
nipkow@17501
  1648
nipkow@17501
  1649
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1650
  dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
nipkow@17501
  1651
apply(induct xs)
nipkow@17501
  1652
 apply simp
nipkow@17501
  1653
apply auto
nipkow@17501
  1654
apply(subst dropWhile_append2)
nipkow@17501
  1655
apply auto
nipkow@17501
  1656
done
nipkow@17501
  1657
nipkow@18423
  1658
lemma takeWhile_not_last:
nipkow@18423
  1659
 "\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
nipkow@18423
  1660
apply(induct xs)
nipkow@18423
  1661
 apply simp
nipkow@18423
  1662
apply(case_tac xs)
nipkow@18423
  1663
apply(auto)
nipkow@18423
  1664
done
nipkow@18423
  1665
krauss@19770
  1666
lemma takeWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1667
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1668
  ==> takeWhile P l = takeWhile Q k"
nipkow@24349
  1669
by (induct k arbitrary: l) (simp_all)
krauss@18336
  1670
krauss@19770
  1671
lemma dropWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1672
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1673
  ==> dropWhile P l = dropWhile Q k"
nipkow@24349
  1674
by (induct k arbitrary: l, simp_all)
krauss@18336
  1675
wenzelm@13114
  1676
nipkow@15392
  1677
subsubsection {* @{text zip} *}
wenzelm@13114
  1678
wenzelm@13142
  1679
lemma zip_Nil [simp]: "zip [] ys = []"
nipkow@13145
  1680
by (induct ys) auto
wenzelm@13114
  1681
wenzelm@13142
  1682
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
nipkow@13145
  1683
by simp
wenzelm@13114
  1684
wenzelm@13142
  1685
declare zip_Cons [simp del]
wenzelm@13114
  1686
nipkow@15281
  1687
lemma zip_Cons1:
nipkow@15281
  1688
 "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"
nipkow@15281
  1689
by(auto split:list.split)
nipkow@15281
  1690
wenzelm@13142
  1691
lemma length_zip [simp]:
krauss@22493
  1692
"length (zip xs ys) = min (length xs) (length ys)"
krauss@22493
  1693
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  1694
wenzelm@13114
  1695
lemma zip_append1:
krauss@22493
  1696
"zip (xs @ ys) zs =
nipkow@13145
  1697
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
krauss@22493
  1698
by (induct xs zs rule:list_induct2') auto
wenzelm@13114
  1699
wenzelm@13114
  1700
lemma zip_append2:
krauss@22493
  1701
"zip xs (ys @ zs) =
nipkow@13145
  1702
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
krauss@22493
  1703
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  1704
wenzelm@13142
  1705
lemma zip_append [simp]:
wenzelm@13142
  1706
 "[| length xs = length us; length ys = length vs |] ==>
nipkow@13145
  1707
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
nipkow@13145
  1708
by (simp add: zip_append1)
wenzelm@13114
  1709
wenzelm@13114
  1710
lemma zip_rev:
nipkow@14247
  1711
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
nipkow@14247
  1712
by (induct rule:list_induct2, simp_all)
wenzelm@13114
  1713
nipkow@23096
  1714
lemma map_zip_map:
nipkow@23096
  1715
 "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)"
nipkow@23096
  1716
apply(induct xs arbitrary:ys) apply simp
nipkow@23096
  1717
apply(case_tac ys)
nipkow@23096
  1718
apply simp_all
nipkow@23096
  1719
done
nipkow@23096
  1720
nipkow@23096
  1721
lemma map_zip_map2:
nipkow@23096
  1722
 "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)"
nipkow@23096
  1723
apply(induct xs arbitrary:ys) apply simp
nipkow@23096
  1724
apply(case_tac ys)
nipkow@23096
  1725
apply simp_all
nipkow@23096
  1726
done
nipkow@23096
  1727
wenzelm@13142
  1728
lemma nth_zip [simp]:
nipkow@24526
  1729
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
nipkow@24526
  1730
apply (induct ys arbitrary: i xs, simp)
nipkow@13145
  1731
apply (case_tac xs)
nipkow@13145
  1732
 apply (simp_all add: nth.simps split: nat.split)
nipkow@13145
  1733
done
wenzelm@13114
  1734
wenzelm@13114
  1735
lemma set_zip:
nipkow@13145
  1736
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
nipkow@13145
  1737
by (simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
  1738
wenzelm@13114
  1739
lemma zip_update:
nipkow@13145
  1740
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
nipkow@13145
  1741
by (rule sym, simp add: update_zip)
wenzelm@13114
  1742
wenzelm@13142
  1743
lemma zip_replicate [simp]:
nipkow@24526
  1744
  "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
nipkow@24526
  1745
apply (induct i arbitrary: j, auto)
paulson@14208
  1746
apply (case_tac j, auto)
nipkow@13145
  1747
done
wenzelm@13114
  1748
nipkow@19487
  1749
lemma take_zip:
nipkow@24526
  1750
  "take n (zip xs ys) = zip (take n xs) (take n ys)"
nipkow@24526
  1751
apply (induct n arbitrary: xs ys)
nipkow@19487
  1752
 apply simp
nipkow@19487
  1753
apply (case_tac xs, simp)
nipkow@19487
  1754
apply (case_tac ys, simp_all)
nipkow@19487
  1755
done
nipkow@19487
  1756
nipkow@19487
  1757
lemma drop_zip:
nipkow@24526
  1758
  "drop n (zip xs ys) = zip (drop n xs) (drop n ys)"
nipkow@24526
  1759
apply (induct n arbitrary: xs ys)
nipkow@19487
  1760
 apply simp
nipkow@19487
  1761
apply (case_tac xs, simp)
nipkow@19487
  1762
apply (case_tac ys, simp_all)
nipkow@19487
  1763
done
nipkow@19487
  1764
krauss@22493
  1765
lemma set_zip_leftD:
krauss@22493
  1766
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"
krauss@22493
  1767
by (induct xs ys rule:list_induct2') auto
krauss@22493
  1768
krauss@22493
  1769
lemma set_zip_rightD:
krauss@22493
  1770
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"
krauss@22493
  1771
by (induct xs ys rule:list_induct2') auto
wenzelm@13142
  1772
nipkow@23983
  1773
lemma in_set_zipE:
nipkow@23983
  1774
  "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23983
  1775
by(blast dest: set_zip_leftD set_zip_rightD)
nipkow@23983
  1776
nipkow@15392
  1777
subsubsection {* @{text list_all2} *}
wenzelm@13114
  1778
kleing@14316
  1779
lemma list_all2_lengthD [intro?]: 
kleing@14316
  1780
  "list_all2 P xs ys ==> length xs = length ys"
nipkow@24349
  1781
by (simp add: list_all2_def)
haftmann@19607
  1782
haftmann@19787
  1783
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"
nipkow@24349
  1784
by (simp add: list_all2_def)
haftmann@19607
  1785
haftmann@19787
  1786
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"
nipkow@24349
  1787
by (simp add: list_all2_def)
haftmann@19607
  1788
haftmann@19607
  1789
lemma list_all2_Cons [iff, code]:
haftmann@19607
  1790
  "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
nipkow@24349
  1791
by (auto simp add: list_all2_def)
wenzelm@13114
  1792
wenzelm@13114
  1793
lemma list_all2_Cons1:
nipkow@13145
  1794
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
nipkow@13145
  1795
by (cases ys) auto
wenzelm@13114
  1796
wenzelm@13114
  1797
lemma list_all2_Cons2:
nipkow@13145
  1798
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
nipkow@13145
  1799
by (cases xs) auto
wenzelm@13114
  1800
wenzelm@13142
  1801
lemma list_all2_rev [iff]:
nipkow@13145
  1802
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
nipkow@13145
  1803
by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
  1804
kleing@13863
  1805
lemma list_all2_rev1:
kleing@13863
  1806
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
kleing@13863
  1807
by (subst list_all2_rev [symmetric]) simp
kleing@13863
  1808
wenzelm@13114
  1809
lemma list_all2_append1:
nipkow@13145
  1810
"list_all2 P (xs @ ys) zs =
nipkow@13145
  1811
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
nipkow@13145
  1812
list_all2 P xs us \<and> list_all2 P ys vs)"
nipkow@13145
  1813
apply (simp add: list_all2_def zip_append1)
nipkow@13145
  1814
apply (rule iffI)
nipkow@13145
  1815
 apply (rule_tac x = "take (length xs) zs" in exI)
nipkow@13145
  1816
 apply (rule_tac x = "drop (length xs) zs" in exI)
paulson@14208
  1817
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  1818
apply (simp add: ball_Un)
nipkow@13145
  1819
done
wenzelm@13114
  1820
wenzelm@13114
  1821
lemma list_all2_append2:
nipkow@13145
  1822
"list_all2 P xs (ys @ zs) =
nipkow@13145
  1823
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
nipkow@13145
  1824
list_all2 P us ys \<and> list_all2 P vs zs)"
nipkow@13145
  1825
apply (simp add: list_all2_def zip_append2)
nipkow@13145
  1826
apply (rule iffI)
nipkow@13145
  1827
 apply (rule_tac x = "take (length ys) xs" in exI)
nipkow@13145
  1828
 apply (rule_tac x = "drop (length ys) xs" in exI)
paulson@14208
  1829
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  1830
apply (simp add: ball_Un)
nipkow@13145
  1831
done
wenzelm@13114
  1832
kleing@13863
  1833
lemma list_all2_append:
nipkow@14247
  1834
  "length xs = length ys \<Longrightarrow>
nipkow@14247
  1835
  list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
nipkow@14247
  1836
by (induct rule:list_induct2, simp_all)
kleing@13863
  1837
kleing@13863
  1838
lemma list_all2_appendI [intro?, trans]:
kleing@13863
  1839
  "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
nipkow@24349
  1840
by (simp add: list_all2_append list_all2_lengthD)
kleing@13863
  1841
wenzelm@13114
  1842
lemma list_all2_conv_all_nth:
nipkow@13145
  1843
"list_all2 P xs ys =
nipkow@13145
  1844
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
nipkow@13145
  1845
by (force simp add: list_all2_def set_zip)
wenzelm@13114
  1846
berghofe@13883
  1847
lemma list_all2_trans:
berghofe@13883
  1848
  assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
berghofe@13883
  1849
  shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
berghofe@13883
  1850
        (is "!!bs cs. PROP ?Q as bs cs")
berghofe@13883
  1851
proof (induct as)
berghofe@13883
  1852
  fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
berghofe@13883
  1853
  show "!!cs. PROP ?Q (x # xs) bs cs"
berghofe@13883
  1854
  proof (induct bs)
berghofe@13883
  1855
    fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
berghofe@13883
  1856
    show "PROP ?Q (x # xs) (y # ys) cs"
berghofe@13883
  1857
      by (induct cs) (auto intro: tr I1 I2)
berghofe@13883
  1858
  qed simp
berghofe@13883
  1859
qed simp
berghofe@13883
  1860
kleing@13863
  1861
lemma list_all2_all_nthI [intro?]:
kleing@13863
  1862
  "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
nipkow@24349
  1863
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1864
paulson@14395
  1865
lemma list_all2I:
paulson@14395
  1866
  "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
nipkow@24349
  1867
by (simp add: list_all2_def)
paulson@14395
  1868
kleing@14328
  1869
lemma list_all2_nthD:
kleing@13863
  1870
  "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  1871
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1872
nipkow@14302
  1873
lemma list_all2_nthD2:
nipkow@14302
  1874
  "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  1875
by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
nipkow@14302
  1876
kleing@13863
  1877
lemma list_all2_map1: 
kleing@13863
  1878
  "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
nipkow@24349
  1879
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1880
kleing@13863
  1881
lemma list_all2_map2: 
kleing@13863
  1882
  "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
nipkow@24349
  1883
by (auto simp add: list_all2_conv_all_nth)
kleing@13863
  1884
kleing@14316
  1885
lemma list_all2_refl [intro?]:
kleing@13863
  1886
  "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
nipkow@24349
  1887
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1888
kleing@13863
  1889
lemma list_all2_update_cong:
kleing@13863
  1890
  "\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
nipkow@24349
  1891
by (simp add: list_all2_conv_all_nth nth_list_update)
kleing@13863
  1892
kleing@13863
  1893
lemma list_all2_update_cong2:
kleing@13863
  1894
  "\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
nipkow@24349
  1895
by (simp add: list_all2_lengthD list_all2_update_cong)
kleing@13863
  1896
nipkow@14302
  1897
lemma list_all2_takeI [simp,intro?]:
nipkow@24526
  1898
  "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
nipkow@24526
  1899
apply (induct xs arbitrary: n ys)
nipkow@24526
  1900
 apply simp
nipkow@24526
  1901
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  1902
apply (case_tac n)
nipkow@24526
  1903
apply auto
nipkow@24526
  1904
done
nipkow@14302
  1905
nipkow@14302
  1906
lemma list_all2_dropI [simp,intro?]:
nipkow@24526
  1907
  "list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"
nipkow@24526
  1908
apply (induct as arbitrary: n bs, simp)
nipkow@24526
  1909
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  1910
apply (case_tac n, simp, simp)
nipkow@24526
  1911
done
kleing@13863
  1912
kleing@14327
  1913
lemma list_all2_mono [intro?]:
nipkow@24526
  1914
  "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys"
nipkow@24526
  1915
apply (induct xs arbitrary: ys, simp)
nipkow@24526
  1916
apply (case_tac ys, auto)
nipkow@24526
  1917
done
kleing@13863
  1918
haftmann@22551
  1919
lemma list_all2_eq:
haftmann@22551
  1920
  "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys"
nipkow@24349
  1921
by (induct xs ys rule: list_induct2') auto
haftmann@22551
  1922
wenzelm@13142
  1923
nipkow@15392
  1924
subsubsection {* @{text foldl} and @{text foldr} *}
wenzelm@13142
  1925
wenzelm@13142
  1926
lemma foldl_append [simp]:
nipkow@24526
  1927
  "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
nipkow@24526
  1928
by (induct xs arbitrary: a) auto
wenzelm@13142
  1929
nipkow@14402
  1930
lemma foldr_append[simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"
nipkow@14402
  1931
by (induct xs) auto
nipkow@14402
  1932
nipkow@23096
  1933
lemma foldr_map: "foldr g (map f xs) a = foldr (g o f) xs a"
nipkow@23096
  1934
by(induct xs) simp_all
nipkow@23096
  1935
nipkow@24449
  1936
text{* For efficient code generation: avoid intermediate list. *}
nipkow@24449
  1937
lemma foldl_map[code unfold]:
nipkow@24449
  1938
  "foldl g a (map f xs) = foldl (%a x. g a (f x)) a xs"
nipkow@23096
  1939
by(induct xs arbitrary:a) simp_all
nipkow@23096
  1940
krauss@19770
  1941
lemma foldl_cong [fundef_cong, recdef_cong]:
krauss@18336
  1942
  "[| a = b; l = k; !!a x. x : set l ==> f a x = g a x |] 
krauss@18336
  1943
  ==> foldl f a l = foldl g b k"
nipkow@24349
  1944
by (induct k arbitrary: a b l) simp_all
krauss@18336
  1945
krauss@19770
  1946
lemma foldr_cong [fundef_cong, recdef_cong]:
krauss@18336
  1947
  "[| a = b; l = k; !!a x. x : set l ==> f x a = g x a |] 
krauss@18336
  1948
  ==> foldr f l a = foldr g k b"
nipkow@24349
  1949
by (induct k arbitrary: a b l) simp_all
krauss@18336
  1950
nipkow@24449
  1951
lemma (in semigroup_add) foldl_assoc:
haftmann@25062
  1952
shows "foldl op+ (x+y) zs = x + (foldl op+ y zs)"
nipkow@24449
  1953
by (induct zs arbitrary: y) (simp_all add:add_assoc)
nipkow@24449
  1954
nipkow@24449
  1955
lemma (in monoid_add) foldl_absorb0:
haftmann@25062
  1956
shows "x + (foldl op+ 0 zs) = foldl op+ x zs"
nipkow@24449
  1957
by (induct zs) (simp_all add:foldl_assoc)
nipkow@24449
  1958
nipkow@24449
  1959
nipkow@23096
  1960
text{* The ``First Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  1961
nipkow@23096
  1962
lemma foldl_foldr1_lemma:
nipkow@23096
  1963
 "foldl op + a xs = a + foldr op + xs (0\<Colon>'a::monoid_add)"
nipkow@23096
  1964
by (induct xs arbitrary: a) (auto simp:add_assoc)
nipkow@23096
  1965
nipkow@23096
  1966
corollary foldl_foldr1:
nipkow@23096
  1967
 "foldl op + 0 xs = foldr op + xs (0\<Colon>'a::monoid_add)"
nipkow@23096
  1968
by (simp add:foldl_foldr1_lemma)
nipkow@23096
  1969
nipkow@23096
  1970
nipkow@23096
  1971
text{* The ``Third Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  1972
nipkow@14402
  1973
lemma foldr_foldl: "foldr f xs a = foldl (%x y. f y x) a (rev xs)"
nipkow@14402
  1974
by (induct xs) auto
nipkow@14402
  1975
nipkow@14402
  1976
lemma foldl_foldr: "foldl f a xs = foldr (%x y. f y x) (rev xs) a"
nipkow@14402
  1977
by (simp add: foldr_foldl [of "%x y. f y x" "rev xs"])
nipkow@14402
  1978
haftmann@25062
  1979
lemma (in ab_semigroup_add) foldr_conv_foldl: "foldr op + xs a = foldl op + a xs"
chaieb@24471
  1980
  by (induct xs, auto simp add: foldl_assoc add_commute)
chaieb@24471
  1981
wenzelm@13142
  1982
text {*
nipkow@13145
  1983
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
nipkow@13145
  1984
difficult to use because it requires an additional transitivity step.
wenzelm@13142
  1985
*}
wenzelm@13142
  1986
nipkow@24526
  1987
lemma start_le_sum: "(m::nat) <= n ==> m <= foldl (op +) n ns"
nipkow@24526
  1988
by (induct ns arbitrary: n) auto
nipkow@24526
  1989
nipkow@24526
  1990
lemma elem_le_sum: "(n::nat) : set ns ==> n <= foldl (op +) 0 ns"
nipkow@13145
  1991
by (force intro: start_le_sum simp add: in_set_conv_decomp)
wenzelm@13142
  1992
wenzelm@13142
  1993
lemma sum_eq_0_conv [iff]:
nipkow@24526
  1994
  "(foldl (op +) (m::nat) ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
nipkow@24526
  1995
by (induct ns arbitrary: m) auto
wenzelm@13114
  1996
chaieb@24471
  1997
lemma foldr_invariant: 
chaieb@24471
  1998
  "\<lbrakk>Q x ; \<forall> x\<in> set xs. P x; \<forall> x y. P x \<and> Q y \<longrightarrow> Q (f x y) \<rbrakk> \<Longrightarrow> Q (foldr f xs x)"
chaieb@24471
  1999
  by (induct xs, simp_all)
chaieb@24471
  2000
chaieb@24471
  2001
lemma foldl_invariant: 
chaieb@24471
  2002
  "\<lbrakk>Q x ; \<forall> x\<in> set xs. P x; \<forall> x y. P x \<and> Q y \<longrightarrow> Q (f y x) \<rbrakk> \<Longrightarrow> Q (foldl f x xs)"
chaieb@24471
  2003
  by (induct xs arbitrary: x, simp_all)
chaieb@24471
  2004
nipkow@24449
  2005
text{* @{const foldl} and @{text concat} *}
nipkow@24449
  2006
nipkow@24449
  2007
lemma concat_conv_foldl: "concat xss = foldl op@ [] xss"
nipkow@24449
  2008
by (induct xss) (simp_all add:monoid_append.foldl_absorb0)
nipkow@24449
  2009
nipkow@24449
  2010
lemma foldl_conv_concat:
nipkow@24449
  2011
  "foldl (op @) xs xxs = xs @ (concat xxs)"
nipkow@24449
  2012
by(simp add:concat_conv_foldl monoid_append.foldl_absorb0)
nipkow@24449
  2013
nipkow@23096
  2014
subsubsection {* List summation: @{const listsum} and @{text"\<Sum>"}*}
nipkow@23096
  2015
haftmann@26442
  2016
lemma listsum_append [simp]: "listsum (xs @ ys) = listsum xs + listsum ys"
nipkow@24449
  2017
by (induct xs) (simp_all add:add_assoc)
nipkow@24449
  2018
haftmann@26442
  2019
lemma listsum_rev [simp]:
haftmann@26442
  2020
  fixes xs :: "'a\<Colon>comm_monoid_add list"
haftmann@26442
  2021
  shows "listsum (rev xs) = listsum xs"
nipkow@24449
  2022
by (induct xs) (simp_all add:add_ac)
nipkow@24449
  2023
haftmann@26442
  2024
lemma listsum_foldr: "listsum xs = foldr (op +) xs 0"
haftmann@26442
  2025
by (induct xs) auto
haftmann@26442
  2026
haftmann@26442
  2027
lemma length_concat: "length (concat xss) = listsum (map length xss)"
haftmann@26442
  2028
by (induct xss) simp_all
nipkow@23096
  2029
nipkow@24449
  2030
text{* For efficient code generation ---
nipkow@24449
  2031
       @{const listsum} is not tail recursive but @{const foldl} is. *}
nipkow@24449
  2032
lemma listsum[code unfold]: "listsum xs = foldl (op +) 0 xs"
nipkow@23096
  2033
by(simp add:listsum_foldr foldl_foldr1)
nipkow@23096
  2034
nipkow@24449
  2035
nipkow@23096
  2036
text{* Some syntactic sugar for summing a function over a list: *}
nipkow@23096
  2037
nipkow@23096
  2038
syntax
nipkow@23096
  2039
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
nipkow@23096
  2040
syntax (xsymbols)
nipkow@23096
  2041
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
nipkow@23096
  2042
syntax (HTML output)
nipkow@23096
  2043
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
nipkow@23096
  2044
nipkow@23096
  2045
translations -- {* Beware of argument permutation! *}
nipkow@23096
  2046
  "SUM x<-xs. b" == "CONST listsum (map (%x. b) xs)"
nipkow@23096
  2047
  "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (map (%x. b) xs)"
nipkow@23096
  2048
haftmann@26442
  2049
lemma listsum_triv: "(\<Sum>x\<leftarrow>xs. r) = of_nat (length xs) * r"
haftmann@26442
  2050
  by (induct xs) (simp_all add: left_distrib)
haftmann@26442
  2051
nipkow@23096
  2052
lemma listsum_0 [simp]: "(\<Sum>x\<leftarrow>xs. 0) = 0"
haftmann@26442
  2053
  by (induct xs) (simp_all add: left_distrib)
nipkow@23096
  2054
nipkow@23096
  2055
text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
nipkow@23096
  2056
lemma uminus_listsum_map:
haftmann@26442
  2057
  fixes f :: "'a \<Rightarrow> 'b\<Colon>ab_group_add"
haftmann@26442
  2058
  shows "- listsum (map f xs) = (listsum (map (uminus o f) xs))"
haftmann@26442
  2059
by (induct xs) simp_all
nipkow@23096
  2060
wenzelm@13114
  2061
nipkow@24645
  2062
subsubsection {* @{text upt} *}
wenzelm@13114
  2063
nipkow@17090
  2064
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"
nipkow@17090
  2065
-- {* simp does not terminate! *}
nipkow@13145
  2066
by (induct j) auto
wenzelm@13142
  2067
nipkow@15425
  2068
lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
nipkow@13145
  2069
by (subst upt_rec) simp
wenzelm@13114
  2070
nipkow@15425
  2071
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)"
nipkow@15281
  2072
by(induct j)simp_all
nipkow@15281
  2073
nipkow@15281
  2074
lemma upt_eq_Cons_conv:
nipkow@24526
  2075
 "([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)"
nipkow@24526
  2076
apply(induct j arbitrary: x xs)
nipkow@15281
  2077
 apply simp
nipkow@15281
  2078
apply(clarsimp simp add: append_eq_Cons_conv)
nipkow@15281
  2079
apply arith
nipkow@15281
  2080
done
nipkow@15281
  2081
nipkow@15425
  2082
lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]"
nipkow@13145
  2083
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
nipkow@13145
  2084
by simp
wenzelm@13114
  2085
nipkow@15425
  2086
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]"
paulson@24632
  2087
by (metis upt_rec)
wenzelm@13114
  2088
nipkow@15425
  2089
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]"
nipkow@13145
  2090
-- {* LOOPS as a simprule, since @{text "j <= j"}. *}
nipkow@13145
  2091
by (induct k) auto
wenzelm@13114
  2092
nipkow@15425
  2093
lemma length_upt [simp]: "length [i..<j] = j - i"
nipkow@13145
  2094
by (induct j) (auto simp add: Suc_diff_le)
wenzelm@13114
  2095
nipkow@15425
  2096
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k"
nipkow@13145
  2097
apply (induct j)
nipkow@13145
  2098
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
nipkow@13145
  2099
done
wenzelm@13114
  2100
nipkow@17906
  2101
nipkow@17906
  2102
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i"
nipkow@17906
  2103
by(simp add:upt_conv_Cons)
nipkow@17906
  2104
nipkow@17906
  2105
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1"
nipkow@17906
  2106
apply(cases j)
nipkow@17906
  2107
 apply simp
nipkow@17906
  2108
by(simp add:upt_Suc_append)
nipkow@17906
  2109
nipkow@24526
  2110
lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]"
nipkow@24526
  2111
apply (induct m arbitrary: i, simp)
nipkow@13145
  2112
apply (subst upt_rec)
nipkow@13145
  2113
apply (rule sym)
nipkow@13145
  2114
apply (subst upt_rec)
nipkow@13145
  2115
apply (simp del: upt.simps)
nipkow@13145
  2116
done
nipkow@3507
  2117
nipkow@17501
  2118
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"
nipkow@17501
  2119
apply(induct j)
nipkow@17501
  2120
apply auto
nipkow@17501
  2121
done
nipkow@17501
  2122
nipkow@24645
  2123
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]"
nipkow@13145
  2124
by (induct n) auto
wenzelm@13114
  2125
nipkow@24526
  2126
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)"
nipkow@24526
  2127
apply (induct n m  arbitrary: i rule: diff_induct)
nipkow@13145
  2128
prefer 3 apply (subst map_Suc_upt[symmetric])
nipkow@13145
  2129
apply (auto simp add: less_diff_conv nth_upt)
nipkow@13145
  2130
done
wenzelm@13114
  2131
berghofe@13883
  2132
lemma nth_take_lemma:
nipkow@24526
  2133
  "k <= length xs ==> k <= length ys ==>
berghofe@13883
  2134
     (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
nipkow@24526
  2135
apply (atomize, induct k arbitrary: xs ys)
paulson@14208
  2136
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
nipkow@13145
  2137
txt {* Both lists must be non-empty *}
paulson@14208
  2138
apply (case_tac xs, simp)
paulson@14208
  2139
apply (case_tac ys, clarify)
nipkow@13145
  2140
 apply (simp (no_asm_use))
nipkow@13145
  2141
apply clarify
nipkow@13145
  2142
txt {* prenexing's needed, not miniscoping *}
nipkow@13145
  2143
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
nipkow@13145
  2144
apply blast
nipkow@13145
  2145
done
wenzelm@13114
  2146
wenzelm@13114
  2147
lemma nth_equalityI:
wenzelm@13114
  2148
 "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
nipkow@13145
  2149
apply (frule nth_take_lemma [OF le_refl eq_imp_le])
nipkow@13145
  2150
apply (simp_all add: take_all)
nipkow@13145
  2151
done
wenzelm@13142
  2152
haftmann@24796
  2153
lemma map_nth:
haftmann@24796
  2154
  "map (\<lambda>i. xs ! i) [0..<length xs] = xs"
haftmann@24796
  2155
  by (rule nth_equalityI, auto)
haftmann@24796
  2156
kleing@13863
  2157
(* needs nth_equalityI *)
kleing@13863
  2158
lemma list_all2_antisym:
kleing@13863
  2159
  "\<lbrakk> (\<And>x y. \<lbrakk>P x y; Q y x\<rbrakk> \<Longrightarrow> x = y); list_all2 P xs ys; list_all2 Q ys xs \<rbrakk> 
kleing@13863
  2160
  \<Longrightarrow> xs = ys"
kleing@13863
  2161
  apply (simp add: list_all2_conv_all_nth) 
paulson@14208
  2162
  apply (rule nth_equalityI, blast, simp)
kleing@13863
  2163
  done
kleing@13863
  2164
wenzelm@13142
  2165
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
nipkow@13145
  2166
-- {* The famous take-lemma. *}
nipkow@13145
  2167
apply (drule_tac x = "max (length xs) (length ys)" in spec)
nipkow@13145
  2168
apply (simp add: le_max_iff_disj take_all)
nipkow@13145
  2169
done
wenzelm@13142
  2170
wenzelm@13142
  2171
nipkow@15302
  2172
lemma take_Cons':
nipkow@15302
  2173
     "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
nipkow@15302
  2174
by (cases n) simp_all
nipkow@15302
  2175
nipkow@15302
  2176
lemma drop_Cons':
nipkow@15302
  2177
     "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
nipkow@15302
  2178
by (cases n) simp_all
nipkow@15302
  2179
nipkow@15302
  2180
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
nipkow@15302
  2181
by (cases n) simp_all
nipkow@15302
  2182
paulson@18622
  2183
lemmas take_Cons_number_of = take_Cons'[of "number_of v",standard]
paulson@18622
  2184
lemmas drop_Cons_number_of = drop_Cons'[of "number_of v",standard]
paulson@18622
  2185
lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v",standard]
paulson@18622
  2186
paulson@18622
  2187
declare take_Cons_number_of [simp] 
paulson@18622
  2188
        drop_Cons_number_of [simp] 
paulson@18622
  2189
        nth_Cons_number_of [simp] 
nipkow@15302
  2190
nipkow@15302
  2191
nipkow@15392
  2192
subsubsection {* @{text "distinct"} and @{text remdups} *}
wenzelm@13142
  2193
wenzelm@13142
  2194
lemma distinct_append [simp]:
nipkow@13145
  2195
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
nipkow@13145
  2196
by (induct xs) auto
wenzelm@13142
  2197
nipkow@15305
  2198
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs"
nipkow@15305
  2199
by(induct xs) auto
nipkow@15305
  2200
wenzelm@13142
  2201
lemma set_remdups [simp]: "set (remdups xs) = set xs"
nipkow@13145
  2202
by (induct xs) (auto simp add: insert_absorb)
wenzelm@13142
  2203
wenzelm@13142
  2204
lemma distinct_remdups [iff]: "distinct (remdups xs)"
nipkow@13145
  2205
by (induct xs) auto
wenzelm@13142
  2206
nipkow@25287
  2207
lemma distinct_remdups_id: "distinct xs ==> remdups xs = xs"
nipkow@25287
  2208
by (induct xs, auto)
nipkow@25287
  2209
nipkow@25287
  2210
lemma remdups_id_iff_distinct[simp]: "(remdups xs = xs) = distinct xs"
nipkow@25287
  2211
by(metis distinct_remdups distinct_remdups_id)
nipkow@25287
  2212
nipkow@24566
  2213
lemma finite_distinct_list: "finite A \<Longrightarrow> EX xs. set xs = A & distinct xs"
paulson@24632
  2214
by (metis distinct_remdups finite_list set_remdups)
nipkow@24566
  2215
paulson@15072
  2216
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])"
nipkow@24349
  2217
by (induct x, auto) 
paulson@15072
  2218
paulson@15072
  2219
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])"
nipkow@24349
  2220
by (induct x, auto)
paulson@15072
  2221
nipkow@15245
  2222
lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs"
nipkow@15245
  2223
by (induct xs) auto
nipkow@15245
  2224
nipkow@15245
  2225
lemma length_remdups_eq[iff]:
nipkow@15245
  2226
  "(length (remdups xs) = length xs) = (remdups xs = xs)"
nipkow@15245
  2227
apply(induct xs)
nipkow@15245
  2228
 apply auto
nipkow@15245
  2229
apply(subgoal_tac "length (remdups xs) <= length xs")
nipkow@15245
  2230
 apply arith
nipkow@15245
  2231
apply(rule length_remdups_leq)
nipkow@15245
  2232
done
nipkow@15245
  2233
nipkow@18490
  2234
nipkow@18490
  2235
lemma distinct_map:
nipkow@18490
  2236
  "distinct(map f xs) = (distinct xs & inj_on f (set xs))"
nipkow@18490
  2237
by (induct xs) auto
nipkow@18490
  2238
nipkow@18490
  2239
wenzelm@13142
  2240
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
nipkow@13145
  2241
by (induct xs) auto
wenzelm@13114
  2242
nipkow@17501
  2243
lemma distinct_upt[simp]: "distinct[i..<j]"
nipkow@17501
  2244
by (induct j) auto
nipkow@17501
  2245
nipkow@24526
  2246
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)"
nipkow@24526
  2247
apply(induct xs arbitrary: i)
nipkow@17501
  2248
 apply simp
nipkow@17501
  2249
apply (case_tac i)
nipkow@17501
  2250
 apply simp_all
nipkow@17501
  2251
apply(blast dest:in_set_takeD)
nipkow@17501
  2252
done
nipkow@17501
  2253
nipkow@24526
  2254
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)"
nipkow@24526
  2255
apply(induct xs arbitrary: i)
nipkow@17501
  2256
 apply simp
nipkow@17501
  2257
apply (case_tac i)
nipkow@17501
  2258
 apply simp_all
nipkow@17501
  2259
done
nipkow@17501
  2260
nipkow@17501
  2261
lemma distinct_list_update:
nipkow@17501
  2262
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
nipkow@17501
  2263
shows "distinct (xs[i:=a])"
nipkow@17501
  2264
proof (cases "i < length xs")
nipkow@17501
  2265
  case True
nipkow@17501
  2266
  with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
nipkow@17501
  2267
    apply (drule_tac id_take_nth_drop) by simp
nipkow@17501
  2268
  with d True show ?thesis
nipkow@17501
  2269
    apply (simp add: upd_conv_take_nth_drop)
nipkow@17501
  2270
    apply (drule subst [OF id_take_nth_drop]) apply assumption
nipkow@17501
  2271
    apply simp apply (cases "a = xs!i") apply simp by blast
nipkow@17501
  2272
next
nipkow@17501
  2273
  case False with d show ?thesis by auto
nipkow@17501
  2274
qed
nipkow@17501
  2275
nipkow@17501
  2276
nipkow@17501
  2277
text {* It is best to avoid this indexed version of distinct, but
nipkow@17501
  2278
sometimes it is useful. *}
nipkow@17501
  2279
wenzelm@13142
  2280
lemma distinct_conv_nth:
nipkow@17501
  2281
"distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)"
paulson@15251
  2282
apply (induct xs, simp, simp)
paulson@14208
  2283
apply (rule iffI, clarsimp)
nipkow@13145
  2284
 apply (case_tac i)
paulson@14208
  2285
apply (case_tac j, simp)
nipkow@13145
  2286
apply (simp add: set_conv_nth)
nipkow@13145
  2287
 apply (case_tac j)
paulson@24648
  2288
apply (clarsimp simp add: set_conv_nth, simp) 
nipkow@13145
  2289
apply (rule conjI)
paulson@24648
  2290
(*TOO SLOW
paulson@24632
  2291
apply (metis Zero_neq_Suc gr0_conv_Suc in_set_conv_nth lessI less_trans_Suc nth_Cons' nth_Cons_Suc)
paulson@24648
  2292
*)
paulson@24648
  2293
 apply (clarsimp simp add: set_conv_nth)
paulson@24648
  2294
 apply (erule_tac x = 0 in allE, simp)
paulson@24648
  2295
 apply (erule_tac x = "Suc i" in allE, simp, clarsimp)
wenzelm@25130
  2296
(*TOO SLOW
paulson@24632
  2297
apply (metis Suc_Suc_eq lessI less_trans_Suc nth_Cons_Suc)
wenzelm@25130
  2298
*)
wenzelm@25130
  2299
apply (erule_tac x = "Suc i" in allE, simp)
wenzelm@25130
  2300
apply (erule_tac x = "Suc j" in allE, simp)
nipkow@13145
  2301
done
wenzelm@13114
  2302
nipkow@18490
  2303
lemma nth_eq_iff_index_eq:
nipkow@18490
  2304
 "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)"
nipkow@18490
  2305
by(auto simp: distinct_conv_nth)
nipkow@18490
  2306
nipkow@15110
  2307
lemma distinct_card: "distinct xs ==> card (set xs) = size xs"
nipkow@24349
  2308
by (induct xs) auto
kleing@14388
  2309
nipkow@15110
  2310
lemma card_distinct: "card (set xs) = size xs ==> distinct xs"
kleing@14388
  2311
proof (induct xs)
kleing@14388
  2312
  case Nil thus ?case by simp
kleing@14388
  2313
next
kleing@14388
  2314
  case (Cons x xs)
kleing@14388
  2315
  show ?case
kleing@14388
  2316
  proof (cases "x \<in> set xs")
kleing@14388
  2317
    case False with Cons show ?thesis by simp
kleing@14388
  2318
  next
kleing@14388
  2319
    case True with Cons.prems
kleing@14388
  2320
    have "card (set xs) = Suc (length xs)" 
kleing@14388
  2321
      by (simp add: card_insert_if split: split_if_asm)
kleing@14388
  2322
    moreover have "card (set xs) \<le> length xs" by (rule card_length)
kleing@14388
  2323
    ultimately have False by simp
kleing@14388
  2324
    thus ?thesis ..
kleing@14388
  2325
  qed
kleing@14388
  2326
qed
kleing@14388
  2327
nipkow@25287
  2328
lemma not_distinct_decomp: "~ distinct ws ==> EX xs ys zs y. ws = xs@[y]@ys@[y]@zs"
nipkow@25287
  2329
apply (induct n == "length ws" arbitrary:ws) apply simp
nipkow@25287
  2330
apply(case_tac ws) apply simp
nipkow@25287
  2331
apply (simp split:split_if_asm)
nipkow@25287
  2332
apply (metis Cons_eq_appendI eq_Nil_appendI split_list)
nipkow@25287
  2333
done
nipkow@18490
  2334
nipkow@18490
  2335
lemma length_remdups_concat:
nipkow@18490
  2336
 "length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)"
nipkow@24308
  2337
by(simp add: set_concat distinct_card[symmetric])
nipkow@17906
  2338
nipkow@17906
  2339
nipkow@15392
  2340
subsubsection {* @{text remove1} *}
nipkow@15110
  2341
nipkow@18049
  2342
lemma remove1_append:
nipkow@18049
  2343
  "remove1 x (xs @ ys) =
nipkow@18049
  2344
  (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)"
nipkow@18049
  2345
by (induct xs) auto
nipkow@18049
  2346
nipkow@23479
  2347
lemma in_set_remove1[simp]:
nipkow@23479
  2348
  "a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)"
nipkow@23479
  2349
apply (induct xs)
nipkow@23479
  2350
apply auto
nipkow@23479
  2351
done
nipkow@23479
  2352
nipkow@15110
  2353
lemma set_remove1_subset: "set(remove1 x xs) <= set xs"
nipkow@15110
  2354
apply(induct xs)
nipkow@15110
  2355
 apply simp
nipkow@15110
  2356
apply simp
nipkow@15110
  2357
apply blast
nipkow@15110
  2358
done
nipkow@15110
  2359
paulson@17724
  2360
lemma set_remove1_eq [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}"
nipkow@15110
  2361
apply(induct xs)
nipkow@15110
  2362
 apply simp
nipkow@15110
  2363
apply simp
nipkow@15110
  2364
apply blast
nipkow@15110
  2365
done
nipkow@15110
  2366
nipkow@23479
  2367
lemma length_remove1:
nipkow@23479
  2368
  "length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)"
nipkow@23479
  2369
apply (induct xs)
nipkow@23479
  2370
 apply (auto dest!:length_pos_if_in_set)
nipkow@23479
  2371
done
nipkow@23479
  2372
nipkow@18049
  2373
lemma remove1_filter_not[simp]:
nipkow@18049
  2374
  "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs"
nipkow@18049
  2375
by(induct xs) auto
nipkow@18049
  2376
nipkow@15110
  2377
lemma notin_set_remove1[simp]: "x ~: set xs ==> x ~: set(remove1 y xs)"
nipkow@15110
  2378
apply(insert set_remove1_subset)
nipkow@15110
  2379
apply fast
nipkow@15110
  2380
done
nipkow@15110
  2381
nipkow@15110
  2382
lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)"
nipkow@15110
  2383
by (induct xs) simp_all
nipkow@15110
  2384
wenzelm@13114
  2385
nipkow@15392
  2386
subsubsection {* @{text replicate} *}
wenzelm@13114
  2387
wenzelm@13142
  2388
lemma length_replicate [simp]: "length (replicate n x) = n"
nipkow@13145
  2389
by (induct n) auto
nipkow@13124
  2390
wenzelm@13142
  2391
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
nipkow@13145
  2392
by (induct n) auto
wenzelm@13114
  2393
wenzelm@13114
  2394
lemma replicate_app_Cons_same:
nipkow@13145
  2395
"(replicate n x) @ (x # xs) = x # replicate n x @ xs"
nipkow@13145
  2396
by (induct n) auto
wenzelm@13114
  2397
wenzelm@13142
  2398
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
paulson@14208
  2399
apply (induct n, simp)
nipkow@13145
  2400
apply (simp add: replicate_app_Cons_same)
nipkow@13145
  2401
done
wenzelm@13114
  2402
wenzelm@13142
  2403
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
nipkow@13145
  2404
by (induct n) auto
wenzelm@13114
  2405
nipkow@16397
  2406
text{* Courtesy of Matthias Daum: *}
nipkow@16397
  2407
lemma append_replicate_commute:
nipkow@16397
  2408
  "replicate n x @ replicate k x = replicate k x @ replicate n x"
nipkow@16397
  2409
apply (simp add: replicate_add [THEN sym])
nipkow@16397
  2410
apply (simp add: add_commute)
nipkow@16397
  2411
done
nipkow@16397
  2412
wenzelm@13142
  2413
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
nipkow@13145
  2414
by (induct n) auto
wenzelm@13114
  2415
wenzelm@13142
  2416
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
nipkow@13145
  2417
by (induct n) auto
wenzelm@13114
  2418
wenzelm@13142
  2419
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
nipkow@13145
  2420
by (atomize (full), induct n) auto
wenzelm@13114
  2421
nipkow@24526
  2422
lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x"
nipkow@24526
  2423
apply (induct n arbitrary: i, simp)
nipkow@13145
  2424
apply (simp add: nth_Cons split: nat.split)
nipkow@13145
  2425
done
wenzelm@13114
  2426
nipkow@16397
  2427
text{* Courtesy of Matthias Daum (2 lemmas): *}
nipkow@16397
  2428
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x"
nipkow@16397
  2429
apply (case_tac "k \<le> i")
nipkow@16397
  2430
 apply  (simp add: min_def)
nipkow@16397
  2431
apply (drule not_leE)
nipkow@16397
  2432
apply (simp add: min_def)
nipkow@16397
  2433
apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x")
nipkow@16397
  2434
 apply  simp
nipkow@16397
  2435
apply (simp add: replicate_add [symmetric])
nipkow@16397
  2436
done
nipkow@16397
  2437
nipkow@24526
  2438
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x"
nipkow@24526
  2439
apply (induct k arbitrary: i)
nipkow@16397
  2440
 apply simp
nipkow@16397
  2441
apply clarsimp
nipkow@16397
  2442
apply (case_tac i)
nipkow@16397
  2443
 apply simp
nipkow@16397
  2444
apply clarsimp
nipkow@16397
  2445
done
nipkow@16397
  2446
nipkow@16397
  2447
wenzelm@13142
  2448
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
nipkow@13145
  2449
by (induct n) auto
wenzelm@13114
  2450
wenzelm@13142
  2451
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
nipkow@13145
  2452
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
wenzelm@13114
  2453
wenzelm@13142
  2454
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
nipkow@13145
  2455
by auto
wenzelm@13114
  2456
wenzelm@13142
  2457
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
nipkow@13145
  2458
by (simp add: set_replicate_conv_if split: split_if_asm)
wenzelm@13114
  2459
haftmann@24796
  2460
lemma replicate_append_same:
haftmann@24796
  2461
  "replicate i x @ [x] = x # replicate i x"
haftmann@24796
  2462
  by (induct i) simp_all
haftmann@24796
  2463
haftmann@24796
  2464
lemma map_replicate_trivial:
haftmann@24796
  2465
  "map (\<lambda>i. x) [0..<i] = replicate i x"
haftmann@24796
  2466
  by (induct i) (simp_all add: replicate_append_same)
haftmann@24796
  2467
wenzelm@13114
  2468
nipkow@15392
  2469
subsubsection{*@{text rotate1} and @{text rotate}*}
nipkow@15302
  2470
nipkow@15302
  2471
lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]"
nipkow@15302
  2472
by(simp add:rotate1_def)
nipkow@15302
  2473
nipkow@15302
  2474
lemma rotate0[simp]: "rotate 0 = id"
nipkow@15302
  2475
by(simp add:rotate_def)
nipkow@15302
  2476
nipkow@15302
  2477
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)"
nipkow@15302
  2478
by(simp add:rotate_def)
nipkow@15302
  2479
nipkow@15302
  2480
lemma rotate_add:
nipkow@15302
  2481
  "rotate (m+n) = rotate m o rotate n"
nipkow@15302
  2482
by(simp add:rotate_def funpow_add)
nipkow@15302
  2483
nipkow@15302
  2484
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs"
nipkow@15302
  2485
by(simp add:rotate_add)
nipkow@15302
  2486
nipkow@18049
  2487
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)"
nipkow@18049
  2488
by(simp add:rotate_def funpow_swap1)
nipkow@18049
  2489
nipkow@15302
  2490
lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs"
nipkow@15302
  2491
by(cases xs) simp_all
nipkow@15302
  2492
nipkow@15302
  2493
lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  2494
apply(induct n)
nipkow@15302
  2495
 apply simp
nipkow@15302
  2496
apply (simp add:rotate_def)
nipkow@13145
  2497
done
wenzelm@13114
  2498
nipkow@15302
  2499
lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]"
nipkow@15302
  2500
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2501
nipkow@15302
  2502
lemma rotate_drop_take:
nipkow@15302
  2503
  "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs"
nipkow@15302
  2504
apply(induct n)
nipkow@15302
  2505
 apply simp
nipkow@15302
  2506
apply(simp add:rotate_def)
nipkow@15302
  2507
apply(cases "xs = []")
nipkow@15302
  2508
 apply (simp)
nipkow@15302
  2509
apply(case_tac "n mod length xs = 0")
nipkow@15302
  2510
 apply(simp add:mod_Suc)
nipkow@15302
  2511
 apply(simp add: rotate1_hd_tl drop_Suc take_Suc)
nipkow@15302
  2512
apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric]
nipkow@15302
  2513
                take_hd_drop linorder_not_le)
nipkow@13145
  2514
done
wenzelm@13114
  2515
nipkow@15302
  2516
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs"
nipkow@15302
  2517
by(simp add:rotate_drop_take)
nipkow@15302
  2518
nipkow@15302
  2519
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  2520
by(simp add:rotate_drop_take)
nipkow@15302
  2521
nipkow@15302
  2522
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs"
nipkow@15302
  2523
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2524
nipkow@24526
  2525
lemma length_rotate[simp]: "length(rotate n xs) = length xs"
nipkow@24526
  2526
by (induct n arbitrary: xs) (simp_all add:rotate_def)
nipkow@15302
  2527
nipkow@15302
  2528
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs"
nipkow@15302
  2529
by(simp add:rotate1_def split:list.split) blast
nipkow@15302
  2530
nipkow@15302
  2531
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs"
nipkow@15302
  2532
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  2533
nipkow@15302
  2534
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)"
nipkow@15302
  2535
by(simp add:rotate_drop_take take_map drop_map)
nipkow@15302
  2536
nipkow@15302
  2537
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs"
nipkow@15302
  2538
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2539
nipkow@15302
  2540
lemma set_rotate[simp]: "set(rotate n xs) = set xs"
nipkow@15302
  2541
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  2542
nipkow@15302
  2543
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])"
nipkow@15302
  2544
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2545
nipkow@15302
  2546
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])"
nipkow@15302
  2547
by (induct n) (simp_all add:rotate_def)
wenzelm@13114
  2548
nipkow@15439
  2549
lemma rotate_rev:
nipkow@15439
  2550
  "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)"
nipkow@15439
  2551
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  2552
apply(cases "length xs = 0")
nipkow@15439
  2553
 apply simp
nipkow@15439
  2554
apply(cases "n mod length xs = 0")
nipkow@15439
  2555
 apply simp
nipkow@15439
  2556
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  2557
done
nipkow@15439
  2558
nipkow@18423
  2559
lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)"
nipkow@18423
  2560
apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth)
nipkow@18423
  2561
apply(subgoal_tac "length xs \<noteq> 0")
nipkow@18423
  2562
 prefer 2 apply simp
nipkow@18423
  2563
using mod_less_divisor[of "length xs" n] by arith
nipkow@18423
  2564
wenzelm@13114
  2565
nipkow@15392
  2566
subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
wenzelm@13114
  2567
wenzelm@13142
  2568
lemma sublist_empty [simp]: "sublist xs {} = []"
nipkow@13145
  2569
by (auto simp add: sublist_def)
wenzelm@13114
  2570
wenzelm@13142
  2571
lemma sublist_nil [simp]: "sublist [] A = []"
nipkow@13145
  2572
by (auto simp add: sublist_def)
wenzelm@13114
  2573
nipkow@15281
  2574
lemma length_sublist:
nipkow@15281
  2575
  "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
nipkow@15281
  2576
by(simp add: sublist_def length_filter_conv_card cong:conj_cong)
nipkow@15281
  2577
nipkow@15281
  2578
lemma sublist_shift_lemma_Suc:
nipkow@24526
  2579
  "map fst (filter (%p. P(Suc(snd p))) (zip xs is)) =
nipkow@24526
  2580
   map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))"
nipkow@24526
  2581
apply(induct xs arbitrary: "is")
nipkow@15281
  2582
 apply simp
nipkow@15281
  2583
apply (case_tac "is")
nipkow@15281
  2584
 apply simp
nipkow@15281
  2585
apply simp
nipkow@15281
  2586
done
nipkow@15281
  2587
wenzelm@13114
  2588
lemma sublist_shift_lemma:
nipkow@23279
  2589
     "map fst [p<-zip xs [i..<i + length xs] . snd p : A] =
nipkow@23279
  2590
      map fst [p<-zip xs [0..<length xs] . snd p + i : A]"
nipkow@13145
  2591
by (induct xs rule: rev_induct) (simp_all add: add_commute)
wenzelm@13114
  2592
wenzelm@13114
  2593
lemma sublist_append:
paulson@15168
  2594
     "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
nipkow@13145
  2595
apply (unfold sublist_def)
paulson@14208
  2596
apply (induct l' rule: rev_induct, simp)
nipkow@13145
  2597
apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
nipkow@13145
  2598
apply (simp add: add_commute)
nipkow@13145
  2599
done
wenzelm@13114
  2600
wenzelm@13114
  2601
lemma sublist_Cons:
nipkow@13145
  2602
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
nipkow@13145
  2603
apply (induct l rule: rev_induct)
nipkow@13145
  2604
 apply (simp add: sublist_def)
nipkow@13145
  2605
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
nipkow@13145
  2606
done
wenzelm@13114
  2607
nipkow@24526
  2608
lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
nipkow@24526
  2609
apply(induct xs arbitrary: I)
nipkow@25162
  2610
apply(auto simp: sublist_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc)
nipkow@15281
  2611
done
nipkow@15281
  2612
nipkow@15281
  2613
lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs"
nipkow@15281
  2614
by(auto simp add:set_sublist)
nipkow@15281
  2615
nipkow@15281
  2616
lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)"
nipkow@15281
  2617
by(auto simp add:set_sublist)
nipkow@15281
  2618
nipkow@15281
  2619
lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs"
nipkow@15281
  2620
by(auto simp add:set_sublist)
nipkow@15281
  2621
wenzelm@13142
  2622
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
nipkow@13145
  2623
by (simp add: sublist_Cons)
wenzelm@13114
  2624
nipkow@15281
  2625
nipkow@24526
  2626
lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)"
nipkow@24526
  2627
apply(induct xs arbitrary: I)
nipkow@15281
  2628
 apply simp
nipkow@15281
  2629
apply(auto simp add:sublist_Cons)
nipkow@15281
  2630
done
nipkow@15281
  2631
nipkow@15281
  2632
nipkow@15045
  2633
lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
paulson@14208
  2634
apply (induct l rule: rev_induct, simp)
nipkow@13145
  2635
apply (simp split: nat_diff_split add: sublist_append)
nipkow@13145
  2636
done
wenzelm@13114
  2637
nipkow@24526
  2638
lemma filter_in_sublist:
nipkow@24526
  2639
 "distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s"
nipkow@24526
  2640
proof (induct xs arbitrary: s)
nipkow@17501
  2641
  case Nil thus ?case by simp
nipkow@17501
  2642
next
nipkow@17501
  2643
  case (Cons a xs)
nipkow@17501
  2644
  moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto
nipkow@17501
  2645
  ultimately show ?case by(simp add: sublist_Cons cong:filter_cong)
nipkow@17501
  2646
qed
nipkow@17501
  2647
wenzelm@13114
  2648
nipkow@19390
  2649
subsubsection {* @{const splice} *}
nipkow@19390
  2650
haftmann@19607
  2651
lemma splice_Nil2 [simp, code]:
nipkow@19390
  2652
 "splice xs [] = xs"
nipkow@19390
  2653
by (cases xs) simp_all
nipkow@19390
  2654
haftmann@19607
  2655
lemma splice_Cons_Cons [simp, code]:
nipkow@19390
  2656
 "splice (x#xs) (y#ys) = x # y # splice xs ys"
nipkow@19390
  2657
by simp
nipkow@19390
  2658
haftmann@19607
  2659
declare splice.simps(2) [simp del, code del]
nipkow@19390
  2660
nipkow@24526
  2661
lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys"
nipkow@24526
  2662
apply(induct xs arbitrary: ys) apply simp
nipkow@22793
  2663
apply(case_tac ys)
nipkow@22793
  2664
 apply auto
nipkow@22793
  2665
done
nipkow@22793
  2666
nipkow@24616
  2667
nipkow@24616
  2668
subsection {*Sorting*}
nipkow@24616
  2669
nipkow@24617
  2670
text{* Currently it is not shown that @{const sort} returns a
nipkow@24617
  2671
permutation of its input because the nicest proof is via multisets,
nipkow@24617
  2672
which are not yet available. Alternatively one could define a function
nipkow@24617
  2673
that counts the number of occurrences of an element in a list and use
nipkow@24617
  2674
that instead of multisets to state the correctness property. *}
nipkow@24617
  2675
nipkow@24616
  2676
context linorder
nipkow@24616
  2677
begin
nipkow@24616
  2678
haftmann@25062
  2679
lemma sorted_Cons: "sorted (x#xs) = (sorted xs & (ALL y:set xs. x <= y))"
nipkow@24616
  2680
apply(induct xs arbitrary: x) apply simp
nipkow@24616
  2681
by simp (blast intro: order_trans)
nipkow@24616
  2682
nipkow@24616
  2683
lemma sorted_append:
haftmann@25062
  2684
  "sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))"
nipkow@24616
  2685
by (induct xs) (auto simp add:sorted_Cons)
nipkow@24616
  2686
nipkow@24616
  2687
lemma set_insort: "set(insort x xs) = insert x (set xs)"
nipkow@24616
  2688
by (induct xs) auto
nipkow@24616
  2689
nipkow@24617
  2690
lemma set_sort[simp]: "set(sort xs) = set xs"
nipkow@24616
  2691
by (induct xs) (simp_all add:set_insort)
nipkow@24616
  2692
nipkow@24616
  2693
lemma distinct_insort: "distinct (insort x xs) = (x \<notin> set xs \<and> distinct xs)"
nipkow@24616
  2694
by(induct xs)(auto simp:set_insort)
nipkow@24616
  2695
nipkow@24617
  2696
lemma distinct_sort[simp]: "distinct (sort xs) = distinct xs"
nipkow@24616
  2697
by(induct xs)(simp_all add:distinct_insort set_sort)
nipkow@24616
  2698
nipkow@24616
  2699
lemma sorted_insort: "sorted (insort x xs) = sorted xs"
nipkow@24616
  2700
apply (induct xs)
nipkow@24650
  2701
 apply(auto simp:sorted_Cons set_insort)
nipkow@24616
  2702
done
nipkow@24616
  2703
nipkow@24616
  2704
theorem sorted_sort[simp]: "sorted (sort xs)"
nipkow@24616
  2705
by (induct xs) (auto simp:sorted_insort)
nipkow@24616
  2706
bulwahn@26143
  2707
lemma insort_is_Cons: "\<forall>x\<in>set xs. a \<le> x \<Longrightarrow> insort a xs = a # xs"
bulwahn@26143
  2708
by (cases xs) auto
bulwahn@26143
  2709
bulwahn@26143
  2710
lemma sorted_remove1: "sorted xs \<Longrightarrow> sorted (remove1 a xs)"
bulwahn@26143
  2711
by (induct xs, auto simp add: sorted_Cons)
bulwahn@26143
  2712
bulwahn@26143
  2713
lemma insort_remove1: "\<lbrakk> a \<in> set xs; sorted xs \<rbrakk> \<Longrightarrow> insort a (remove1 a xs) = xs"
bulwahn@26143
  2714
by (induct xs, auto simp add: sorted_Cons insort_is_Cons)
bulwahn@26143
  2715
bulwahn@26143
  2716
lemma sorted_remdups[simp]:
bulwahn@26143
  2717
  "sorted l \<Longrightarrow> sorted (remdups l)"
bulwahn@26143
  2718
by (induct l) (auto simp: sorted_Cons)
bulwahn@26143
  2719
nipkow@24645
  2720
lemma sorted_distinct_set_unique:
nipkow@24645
  2721
assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys"
nipkow@24645
  2722
shows "xs = ys"
nipkow@24645
  2723
proof -
nipkow@24645
  2724
  from assms have 1: "length xs = length ys" by (metis distinct_card)
nipkow@24645
  2725
  from assms show ?thesis
nipkow@24645
  2726
  proof(induct rule:list_induct2[OF 1])
nipkow@24645
  2727
    case 1 show ?case by simp
nipkow@24645
  2728
  next
nipkow@24645
  2729
    case 2 thus ?case by (simp add:sorted_Cons)
nipkow@24645
  2730
       (metis Diff_insert_absorb antisym insertE insert_iff)
nipkow@24645
  2731
  qed
nipkow@24645
  2732
qed
nipkow@24645
  2733
nipkow@24645
  2734
lemma finite_sorted_distinct_unique:
nipkow@24645
  2735
shows "finite A \<Longrightarrow> EX! xs. set xs = A & sorted xs & distinct xs"
nipkow@24645
  2736
apply(drule finite_distinct_list)
nipkow@24645
  2737
apply clarify
nipkow@24645
  2738
apply(rule_tac a="sort xs" in ex1I)
nipkow@24645
  2739
apply (auto simp: sorted_distinct_set_unique)
nipkow@24645
  2740
done
nipkow@24645
  2741
nipkow@24616
  2742
end
nipkow@24616
  2743
nipkow@25277
  2744
lemma sorted_upt[simp]: "sorted[i..<j]"
nipkow@25277
  2745
by (induct j) (simp_all add:sorted_append)
nipkow@25277
  2746
nipkow@24616
  2747
nipkow@25069
  2748
subsubsection {* @{text sorted_list_of_set} *}
nipkow@25069
  2749
nipkow@25069
  2750
text{* This function maps (finite) linearly ordered sets to sorted
nipkow@25069
  2751
lists. Warning: in most cases it is not a good idea to convert from
nipkow@25069
  2752
sets to lists but one should convert in the other direction (via
nipkow@25069
  2753
@{const set}). *}
nipkow@25069
  2754
nipkow@25069
  2755
nipkow@25069
  2756
context linorder
nipkow@25069
  2757
begin
nipkow@25069
  2758
nipkow@25069
  2759
definition