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