src/HOL/List.thy
author kleing
Mon Nov 05 22:49:28 2007 +0100 (2007-11-05)
changeset 25296 c187b7422156
parent 25287 094dab519ff5
child 25502 9200b36280c0
permissions -rw-r--r--
rev_nth
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@22844
     9
imports PreList
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
setup {* snd o Sign.declare_const [] (*authentic syntax*)
wenzelm@25221
   108
  ("append", @{typ "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"}, InfixrName ("@", 65)) *}
wenzelm@25221
   109
primrec
wenzelm@25221
   110
  append_Nil:"[]@ys = ys"
wenzelm@25221
   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
wenzelm@21404
   202
  "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
wenzelm@25221
   225
fun 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
wenzelm@25221
   229
fun 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
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
paulson@15113
   831
proof (induct xs)
paulson@15113
   832
  case Nil show ?case by simp
wenzelm@25221
   833
next
paulson@15113
   834
  case (Cons a xs)
paulson@15113
   835
  show ?case
paulson@15113
   836
  proof 
paulson@15113
   837
    assume "x \<in> set (a # xs)"
wenzelm@25221
   838
    with Cons show "\<exists>ys zs. a # xs = ys @ x # zs"
wenzelm@25221
   839
      by (auto intro: Cons_eq_appendI)
paulson@15113
   840
  next
paulson@15113
   841
    assume "\<exists>ys zs. a # xs = ys @ x # zs"
paulson@15113
   842
    then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast
paulson@15113
   843
    show "x \<in> set (a # xs)" 
wenzelm@25221
   844
      by (cases ys) (auto simp add: eq)
paulson@15113
   845
  qed
paulson@15113
   846
qed
wenzelm@13142
   847
wenzelm@25221
   848
lemma split_list: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs"
wenzelm@25221
   849
  by (rule in_set_conv_decomp [THEN iffD1])
wenzelm@25221
   850
nipkow@18049
   851
lemma in_set_conv_decomp_first:
wenzelm@25221
   852
  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"
nipkow@18049
   853
proof (induct xs)
nipkow@18049
   854
  case Nil show ?case by simp
nipkow@18049
   855
next
nipkow@18049
   856
  case (Cons a xs)
nipkow@18049
   857
  show ?case
nipkow@18049
   858
  proof cases
wenzelm@25221
   859
    assume "x = a" thus ?case using Cons by fastsimp
nipkow@18049
   860
  next
nipkow@18049
   861
    assume "x \<noteq> a"
nipkow@18049
   862
    show ?case
nipkow@18049
   863
    proof
nipkow@18049
   864
      assume "x \<in> set (a # xs)"
wenzelm@25221
   865
      with Cons and `x \<noteq> a` show "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys"
wenzelm@25221
   866
	by (fastsimp intro!: Cons_eq_appendI)
nipkow@18049
   867
    next
nipkow@18049
   868
      assume "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys"
nipkow@18049
   869
      then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast
wenzelm@25221
   870
      show "x \<in> set (a # xs)" by (cases ys) (auto simp add: eq)
nipkow@18049
   871
    qed
nipkow@18049
   872
  qed
nipkow@18049
   873
qed
nipkow@18049
   874
wenzelm@25221
   875
lemma split_list_first: "x : set xs \<Longrightarrow> \<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys"
wenzelm@25221
   876
  by (rule in_set_conv_decomp_first [THEN iffD1])
nipkow@18049
   877
nipkow@18049
   878
paulson@13508
   879
lemma finite_list: "finite A ==> EX l. set l = A"
paulson@13508
   880
apply (erule finite_induct, auto)
paulson@13508
   881
apply (rule_tac x="x#l" in exI, auto)
paulson@13508
   882
done
paulson@13508
   883
kleing@14388
   884
lemma card_length: "card (set xs) \<le> length xs"
kleing@14388
   885
by (induct xs) (auto simp add: card_insert_if)
wenzelm@13114
   886
paulson@15168
   887
nipkow@15392
   888
subsubsection {* @{text filter} *}
wenzelm@13114
   889
wenzelm@13142
   890
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
nipkow@13145
   891
by (induct xs) auto
wenzelm@13114
   892
nipkow@15305
   893
lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"
nipkow@15305
   894
by (induct xs) simp_all
nipkow@15305
   895
wenzelm@13142
   896
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
nipkow@13145
   897
by (induct xs) auto
wenzelm@13114
   898
nipkow@16998
   899
lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"
nipkow@16998
   900
by (induct xs) (auto simp add: le_SucI)
nipkow@16998
   901
nipkow@18423
   902
lemma sum_length_filter_compl:
nipkow@18423
   903
  "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"
nipkow@18423
   904
by(induct xs) simp_all
nipkow@18423
   905
wenzelm@13142
   906
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
nipkow@13145
   907
by (induct xs) auto
wenzelm@13114
   908
wenzelm@13142
   909
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
nipkow@13145
   910
by (induct xs) auto
wenzelm@13114
   911
nipkow@16998
   912
lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
nipkow@24349
   913
by (induct xs) simp_all
nipkow@16998
   914
nipkow@16998
   915
lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"
nipkow@16998
   916
apply (induct xs)
nipkow@16998
   917
 apply auto
nipkow@16998
   918
apply(cut_tac P=P and xs=xs in length_filter_le)
nipkow@16998
   919
apply simp
nipkow@16998
   920
done
wenzelm@13114
   921
nipkow@16965
   922
lemma filter_map:
nipkow@16965
   923
  "filter P (map f xs) = map f (filter (P o f) xs)"
nipkow@16965
   924
by (induct xs) simp_all
nipkow@16965
   925
nipkow@16965
   926
lemma length_filter_map[simp]:
nipkow@16965
   927
  "length (filter P (map f xs)) = length(filter (P o f) xs)"
nipkow@16965
   928
by (simp add:filter_map)
nipkow@16965
   929
wenzelm@13142
   930
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
nipkow@13145
   931
by auto
wenzelm@13114
   932
nipkow@15246
   933
lemma length_filter_less:
nipkow@15246
   934
  "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"
nipkow@15246
   935
proof (induct xs)
nipkow@15246
   936
  case Nil thus ?case by simp
nipkow@15246
   937
next
nipkow@15246
   938
  case (Cons x xs) thus ?case
nipkow@15246
   939
    apply (auto split:split_if_asm)
nipkow@15246
   940
    using length_filter_le[of P xs] apply arith
nipkow@15246
   941
  done
nipkow@15246
   942
qed
wenzelm@13114
   943
nipkow@15281
   944
lemma length_filter_conv_card:
nipkow@15281
   945
 "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
nipkow@15281
   946
proof (induct xs)
nipkow@15281
   947
  case Nil thus ?case by simp
nipkow@15281
   948
next
nipkow@15281
   949
  case (Cons x xs)
nipkow@15281
   950
  let ?S = "{i. i < length xs & p(xs!i)}"
nipkow@15281
   951
  have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)
nipkow@15281
   952
  show ?case (is "?l = card ?S'")
nipkow@15281
   953
  proof (cases)
nipkow@15281
   954
    assume "p x"
nipkow@15281
   955
    hence eq: "?S' = insert 0 (Suc ` ?S)"
nipkow@25162
   956
      by(auto simp: image_def split:nat.split dest:gr0_implies_Suc)
nipkow@15281
   957
    have "length (filter p (x # xs)) = Suc(card ?S)"
wenzelm@23388
   958
      using Cons `p x` by simp
nipkow@15281
   959
    also have "\<dots> = Suc(card(Suc ` ?S))" using fin
nipkow@15281
   960
      by (simp add: card_image inj_Suc)
nipkow@15281
   961
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
   962
      by (simp add:card_insert_if) (simp add:image_def)
nipkow@15281
   963
    finally show ?thesis .
nipkow@15281
   964
  next
nipkow@15281
   965
    assume "\<not> p x"
nipkow@15281
   966
    hence eq: "?S' = Suc ` ?S"
nipkow@25162
   967
      by(auto simp add: image_def split:nat.split elim:lessE)
nipkow@15281
   968
    have "length (filter p (x # xs)) = card ?S"
wenzelm@23388
   969
      using Cons `\<not> p x` by simp
nipkow@15281
   970
    also have "\<dots> = card(Suc ` ?S)" using fin
nipkow@15281
   971
      by (simp add: card_image inj_Suc)
nipkow@15281
   972
    also have "\<dots> = card ?S'" using eq fin
nipkow@15281
   973
      by (simp add:card_insert_if)
nipkow@15281
   974
    finally show ?thesis .
nipkow@15281
   975
  qed
nipkow@15281
   976
qed
nipkow@15281
   977
nipkow@17629
   978
lemma Cons_eq_filterD:
nipkow@17629
   979
 "x#xs = filter P ys \<Longrightarrow>
nipkow@17629
   980
  \<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
   981
  (is "_ \<Longrightarrow> \<exists>us vs. ?P ys us vs")
nipkow@17629
   982
proof(induct ys)
nipkow@17629
   983
  case Nil thus ?case by simp
nipkow@17629
   984
next
nipkow@17629
   985
  case (Cons y ys)
nipkow@17629
   986
  show ?case (is "\<exists>x. ?Q x")
nipkow@17629
   987
  proof cases
nipkow@17629
   988
    assume Py: "P y"
nipkow@17629
   989
    show ?thesis
nipkow@17629
   990
    proof cases
wenzelm@25221
   991
      assume "x = y"
wenzelm@25221
   992
      with Py Cons.prems have "?Q []" by simp
wenzelm@25221
   993
      then show ?thesis ..
nipkow@17629
   994
    next
wenzelm@25221
   995
      assume "x \<noteq> y"
wenzelm@25221
   996
      with Py Cons.prems show ?thesis by simp
nipkow@17629
   997
    qed
nipkow@17629
   998
  next
wenzelm@25221
   999
    assume "\<not> P y"
wenzelm@25221
  1000
    with Cons obtain us vs where "?P (y#ys) (y#us) vs" by fastsimp
wenzelm@25221
  1001
    then have "?Q (y#us)" by simp
wenzelm@25221
  1002
    then show ?thesis ..
nipkow@17629
  1003
  qed
nipkow@17629
  1004
qed
nipkow@17629
  1005
nipkow@17629
  1006
lemma filter_eq_ConsD:
nipkow@17629
  1007
 "filter P ys = x#xs \<Longrightarrow>
nipkow@17629
  1008
  \<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
  1009
by(rule Cons_eq_filterD) simp
nipkow@17629
  1010
nipkow@17629
  1011
lemma filter_eq_Cons_iff:
nipkow@17629
  1012
 "(filter P ys = x#xs) =
nipkow@17629
  1013
  (\<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
  1014
by(auto dest:filter_eq_ConsD)
nipkow@17629
  1015
nipkow@17629
  1016
lemma Cons_eq_filter_iff:
nipkow@17629
  1017
 "(x#xs = filter P ys) =
nipkow@17629
  1018
  (\<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
  1019
by(auto dest:Cons_eq_filterD)
nipkow@17629
  1020
krauss@19770
  1021
lemma filter_cong[fundef_cong, recdef_cong]:
nipkow@17501
  1022
 "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"
nipkow@17501
  1023
apply simp
nipkow@17501
  1024
apply(erule thin_rl)
nipkow@17501
  1025
by (induct ys) simp_all
nipkow@17501
  1026
nipkow@15281
  1027
nipkow@15392
  1028
subsubsection {* @{text concat} *}
wenzelm@13114
  1029
wenzelm@13142
  1030
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
nipkow@13145
  1031
by (induct xs) auto
wenzelm@13114
  1032
paulson@18447
  1033
lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1034
by (induct xss) auto
wenzelm@13114
  1035
paulson@18447
  1036
lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
nipkow@13145
  1037
by (induct xss) auto
wenzelm@13114
  1038
nipkow@24308
  1039
lemma set_concat [simp]: "set (concat xs) = (UN x:set xs. set x)"
nipkow@13145
  1040
by (induct xs) auto
wenzelm@13114
  1041
nipkow@24476
  1042
lemma concat_map_singleton[simp]: "concat(map (%x. [f x]) xs) = map f xs"
nipkow@24349
  1043
by (induct xs) auto
nipkow@24349
  1044
wenzelm@13142
  1045
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
nipkow@13145
  1046
by (induct xs) auto
wenzelm@13114
  1047
wenzelm@13142
  1048
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
nipkow@13145
  1049
by (induct xs) auto
wenzelm@13114
  1050
wenzelm@13142
  1051
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
nipkow@13145
  1052
by (induct xs) auto
wenzelm@13114
  1053
wenzelm@13114
  1054
nipkow@15392
  1055
subsubsection {* @{text nth} *}
wenzelm@13114
  1056
wenzelm@13142
  1057
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
nipkow@13145
  1058
by auto
wenzelm@13114
  1059
wenzelm@13142
  1060
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
nipkow@13145
  1061
by auto
wenzelm@13114
  1062
wenzelm@13142
  1063
declare nth.simps [simp del]
wenzelm@13114
  1064
wenzelm@13114
  1065
lemma nth_append:
nipkow@24526
  1066
  "(xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
nipkow@24526
  1067
apply (induct xs arbitrary: n, simp)
paulson@14208
  1068
apply (case_tac n, auto)
nipkow@13145
  1069
done
wenzelm@13114
  1070
nipkow@14402
  1071
lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"
wenzelm@25221
  1072
by (induct xs) auto
nipkow@14402
  1073
nipkow@14402
  1074
lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"
wenzelm@25221
  1075
by (induct xs) auto
nipkow@14402
  1076
nipkow@24526
  1077
lemma nth_map [simp]: "n < length xs ==> (map f xs)!n = f(xs!n)"
nipkow@24526
  1078
apply (induct xs arbitrary: n, simp)
paulson@14208
  1079
apply (case_tac n, auto)
nipkow@13145
  1080
done
wenzelm@13114
  1081
nipkow@18423
  1082
lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
nipkow@18423
  1083
by(cases xs) simp_all
nipkow@18423
  1084
nipkow@18049
  1085
nipkow@18049
  1086
lemma list_eq_iff_nth_eq:
nipkow@24526
  1087
 "(xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"
nipkow@24526
  1088
apply(induct xs arbitrary: ys)
paulson@24632
  1089
 apply force
nipkow@18049
  1090
apply(case_tac ys)
nipkow@18049
  1091
 apply simp
nipkow@18049
  1092
apply(simp add:nth_Cons split:nat.split)apply blast
nipkow@18049
  1093
done
nipkow@18049
  1094
wenzelm@13142
  1095
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
paulson@15251
  1096
apply (induct xs, simp, simp)
nipkow@13145
  1097
apply safe
paulson@24632
  1098
apply (metis nat_case_0 nth.simps zero_less_Suc)
paulson@24632
  1099
apply (metis less_Suc_eq_0_disj nth_Cons_Suc)
paulson@14208
  1100
apply (case_tac i, simp)
paulson@24632
  1101
apply (metis diff_Suc_Suc nat_case_Suc nth.simps zero_less_diff)
nipkow@13145
  1102
done
wenzelm@13114
  1103
nipkow@17501
  1104
lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"
nipkow@17501
  1105
by(auto simp:set_conv_nth)
nipkow@17501
  1106
nipkow@13145
  1107
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
nipkow@13145
  1108
by (auto simp add: set_conv_nth)
wenzelm@13114
  1109
wenzelm@13142
  1110
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
nipkow@13145
  1111
by (auto simp add: set_conv_nth)
wenzelm@13114
  1112
wenzelm@13114
  1113
lemma all_nth_imp_all_set:
nipkow@13145
  1114
"[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
nipkow@13145
  1115
by (auto simp add: set_conv_nth)
wenzelm@13114
  1116
wenzelm@13114
  1117
lemma all_set_conv_all_nth:
nipkow@13145
  1118
"(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
nipkow@13145
  1119
by (auto simp add: set_conv_nth)
wenzelm@13114
  1120
kleing@25296
  1121
lemma rev_nth:
kleing@25296
  1122
  "n < size xs \<Longrightarrow> rev xs ! n = xs ! (length xs - Suc n)"
kleing@25296
  1123
proof (induct xs arbitrary: n)
kleing@25296
  1124
  case Nil thus ?case by simp
kleing@25296
  1125
next
kleing@25296
  1126
  case (Cons x xs)
kleing@25296
  1127
  hence n: "n < Suc (length xs)" by simp
kleing@25296
  1128
  moreover
kleing@25296
  1129
  { assume "n < length xs"
kleing@25296
  1130
    with n obtain n' where "length xs - n = Suc n'"
kleing@25296
  1131
      by (cases "length xs - n", auto)
kleing@25296
  1132
    moreover
kleing@25296
  1133
    then have "length xs - Suc n = n'" by simp
kleing@25296
  1134
    ultimately
kleing@25296
  1135
    have "xs ! (length xs - Suc n) = (x # xs) ! (length xs - n)" by simp
kleing@25296
  1136
  }
kleing@25296
  1137
  ultimately
kleing@25296
  1138
  show ?case by (clarsimp simp add: Cons nth_append)
kleing@25296
  1139
qed
wenzelm@13114
  1140
nipkow@15392
  1141
subsubsection {* @{text list_update} *}
wenzelm@13114
  1142
nipkow@24526
  1143
lemma length_list_update [simp]: "length(xs[i:=x]) = length xs"
nipkow@24526
  1144
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1145
wenzelm@13114
  1146
lemma nth_list_update:
nipkow@24526
  1147
"i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
nipkow@24526
  1148
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1149
wenzelm@13142
  1150
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
nipkow@13145
  1151
by (simp add: nth_list_update)
wenzelm@13114
  1152
nipkow@24526
  1153
lemma nth_list_update_neq [simp]: "i \<noteq> j ==> xs[i:=x]!j = xs!j"
nipkow@24526
  1154
by (induct xs arbitrary: i j) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
  1155
wenzelm@13142
  1156
lemma list_update_overwrite [simp]:
nipkow@24526
  1157
"i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
nipkow@24526
  1158
by (induct xs arbitrary: i) (auto split: nat.split)
nipkow@24526
  1159
nipkow@24526
  1160
lemma list_update_id[simp]: "xs[i := xs!i] = xs"
nipkow@24526
  1161
by (induct xs arbitrary: i) (simp_all split:nat.splits)
nipkow@24526
  1162
nipkow@24526
  1163
lemma list_update_beyond[simp]: "length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"
nipkow@24526
  1164
apply (induct xs arbitrary: i)
nipkow@17501
  1165
 apply simp
nipkow@17501
  1166
apply (case_tac i)
nipkow@17501
  1167
apply simp_all
nipkow@17501
  1168
done
nipkow@17501
  1169
wenzelm@13114
  1170
lemma list_update_same_conv:
nipkow@24526
  1171
"i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
nipkow@24526
  1172
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1173
nipkow@14187
  1174
lemma list_update_append1:
nipkow@24526
  1175
 "i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
nipkow@24526
  1176
apply (induct xs arbitrary: i, simp)
nipkow@14187
  1177
apply(simp split:nat.split)
nipkow@14187
  1178
done
nipkow@14187
  1179
kleing@15868
  1180
lemma list_update_append:
nipkow@24526
  1181
  "(xs @ ys) [n:= x] = 
kleing@15868
  1182
  (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"
nipkow@24526
  1183
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1184
nipkow@14402
  1185
lemma list_update_length [simp]:
nipkow@14402
  1186
 "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"
nipkow@14402
  1187
by (induct xs, auto)
nipkow@14402
  1188
wenzelm@13114
  1189
lemma update_zip:
nipkow@24526
  1190
  "length xs = length ys ==>
nipkow@24526
  1191
  (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
nipkow@24526
  1192
by (induct ys arbitrary: i xy xs) (auto, case_tac xs, auto split: nat.split)
nipkow@24526
  1193
nipkow@24526
  1194
lemma set_update_subset_insert: "set(xs[i:=x]) <= insert x (set xs)"
nipkow@24526
  1195
by (induct xs arbitrary: i) (auto split: nat.split)
wenzelm@13114
  1196
wenzelm@13114
  1197
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
nipkow@13145
  1198
by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
  1199
nipkow@24526
  1200
lemma set_update_memI: "n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"
nipkow@24526
  1201
by (induct xs arbitrary: n) (auto split:nat.splits)
kleing@15868
  1202
haftmann@24796
  1203
lemma list_update_overwrite:
haftmann@24796
  1204
  "xs [i := x, i := y] = xs [i := y]"
haftmann@24796
  1205
apply (induct xs arbitrary: i)
haftmann@24796
  1206
apply simp
haftmann@24796
  1207
apply (case_tac i)
haftmann@24796
  1208
apply simp_all
haftmann@24796
  1209
done
haftmann@24796
  1210
haftmann@24796
  1211
lemma list_update_swap:
haftmann@24796
  1212
  "i \<noteq> i' \<Longrightarrow> xs [i := x, i' := x'] = xs [i' := x', i := x]"
haftmann@24796
  1213
apply (induct xs arbitrary: i i')
haftmann@24796
  1214
apply simp
haftmann@24796
  1215
apply (case_tac i, case_tac i')
haftmann@24796
  1216
apply auto
haftmann@24796
  1217
apply (case_tac i')
haftmann@24796
  1218
apply auto
haftmann@24796
  1219
done
haftmann@24796
  1220
wenzelm@13114
  1221
nipkow@15392
  1222
subsubsection {* @{text last} and @{text butlast} *}
wenzelm@13114
  1223
wenzelm@13142
  1224
lemma last_snoc [simp]: "last (xs @ [x]) = x"
nipkow@13145
  1225
by (induct xs) auto
wenzelm@13114
  1226
wenzelm@13142
  1227
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
nipkow@13145
  1228
by (induct xs) auto
wenzelm@13114
  1229
nipkow@14302
  1230
lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
nipkow@14302
  1231
by(simp add:last.simps)
nipkow@14302
  1232
nipkow@14302
  1233
lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
nipkow@14302
  1234
by(simp add:last.simps)
nipkow@14302
  1235
nipkow@14302
  1236
lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
nipkow@14302
  1237
by (induct xs) (auto)
nipkow@14302
  1238
nipkow@14302
  1239
lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
nipkow@14302
  1240
by(simp add:last_append)
nipkow@14302
  1241
nipkow@14302
  1242
lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
nipkow@14302
  1243
by(simp add:last_append)
nipkow@14302
  1244
nipkow@17762
  1245
lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
nipkow@17762
  1246
by(rule rev_exhaust[of xs]) simp_all
nipkow@17762
  1247
nipkow@17762
  1248
lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
nipkow@17762
  1249
by(cases xs) simp_all
nipkow@17762
  1250
nipkow@17765
  1251
lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"
nipkow@17765
  1252
by (induct as) auto
nipkow@17762
  1253
wenzelm@13142
  1254
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
nipkow@13145
  1255
by (induct xs rule: rev_induct) auto
wenzelm@13114
  1256
wenzelm@13114
  1257
lemma butlast_append:
nipkow@24526
  1258
  "butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
nipkow@24526
  1259
by (induct xs arbitrary: ys) auto
wenzelm@13114
  1260
wenzelm@13142
  1261
lemma append_butlast_last_id [simp]:
nipkow@13145
  1262
"xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
nipkow@13145
  1263
by (induct xs) auto
wenzelm@13114
  1264
wenzelm@13142
  1265
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
nipkow@13145
  1266
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1267
wenzelm@13114
  1268
lemma in_set_butlast_appendI:
nipkow@13145
  1269
"x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
nipkow@13145
  1270
by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
  1271
nipkow@24526
  1272
lemma last_drop[simp]: "n < length xs \<Longrightarrow> last (drop n xs) = last xs"
nipkow@24526
  1273
apply (induct xs arbitrary: n)
nipkow@17501
  1274
 apply simp
nipkow@17501
  1275
apply (auto split:nat.split)
nipkow@17501
  1276
done
nipkow@17501
  1277
nipkow@17589
  1278
lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
nipkow@17589
  1279
by(induct xs)(auto simp:neq_Nil_conv)
nipkow@17589
  1280
haftmann@24796
  1281
nipkow@15392
  1282
subsubsection {* @{text take} and @{text drop} *}
wenzelm@13114
  1283
wenzelm@13142
  1284
lemma take_0 [simp]: "take 0 xs = []"
nipkow@13145
  1285
by (induct xs) auto
wenzelm@13114
  1286
wenzelm@13142
  1287
lemma drop_0 [simp]: "drop 0 xs = xs"
nipkow@13145
  1288
by (induct xs) auto
wenzelm@13114
  1289
wenzelm@13142
  1290
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
nipkow@13145
  1291
by simp
wenzelm@13114
  1292
wenzelm@13142
  1293
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
nipkow@13145
  1294
by simp
wenzelm@13114
  1295
wenzelm@13142
  1296
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
  1297
nipkow@15110
  1298
lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"
nipkow@15110
  1299
by(clarsimp simp add:neq_Nil_conv)
nipkow@15110
  1300
nipkow@14187
  1301
lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
nipkow@14187
  1302
by(cases xs, simp_all)
nipkow@14187
  1303
nipkow@24526
  1304
lemma drop_tl: "drop n (tl xs) = tl(drop n xs)"
nipkow@24526
  1305
by(induct xs arbitrary: n, simp_all add:drop_Cons drop_Suc split:nat.split)
nipkow@24526
  1306
nipkow@24526
  1307
lemma nth_via_drop: "drop n xs = y#ys \<Longrightarrow> xs!n = y"
nipkow@24526
  1308
apply (induct xs arbitrary: n, simp)
nipkow@14187
  1309
apply(simp add:drop_Cons nth_Cons split:nat.splits)
nipkow@14187
  1310
done
nipkow@14187
  1311
nipkow@13913
  1312
lemma take_Suc_conv_app_nth:
nipkow@24526
  1313
  "i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
nipkow@24526
  1314
apply (induct xs arbitrary: i, simp)
paulson@14208
  1315
apply (case_tac i, auto)
nipkow@13913
  1316
done
nipkow@13913
  1317
mehta@14591
  1318
lemma drop_Suc_conv_tl:
nipkow@24526
  1319
  "i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
nipkow@24526
  1320
apply (induct xs arbitrary: i, simp)
mehta@14591
  1321
apply (case_tac i, auto)
mehta@14591
  1322
done
mehta@14591
  1323
nipkow@24526
  1324
lemma length_take [simp]: "length (take n xs) = min (length xs) n"
nipkow@24526
  1325
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1326
nipkow@24526
  1327
lemma length_drop [simp]: "length (drop n xs) = (length xs - n)"
nipkow@24526
  1328
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1329
nipkow@24526
  1330
lemma take_all [simp]: "length xs <= n ==> take n xs = xs"
nipkow@24526
  1331
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1332
nipkow@24526
  1333
lemma drop_all [simp]: "length xs <= n ==> drop n xs = []"
nipkow@24526
  1334
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1335
wenzelm@13142
  1336
lemma take_append [simp]:
nipkow@24526
  1337
  "take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
nipkow@24526
  1338
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
wenzelm@13114
  1339
wenzelm@13142
  1340
lemma drop_append [simp]:
nipkow@24526
  1341
  "drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
nipkow@24526
  1342
by (induct n arbitrary: xs) (auto, case_tac xs, auto)
nipkow@24526
  1343
nipkow@24526
  1344
lemma take_take [simp]: "take n (take m xs) = take (min n m) xs"
nipkow@24526
  1345
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1346
apply (case_tac xs, auto)
nipkow@15236
  1347
apply (case_tac n, auto)
nipkow@13145
  1348
done
wenzelm@13114
  1349
nipkow@24526
  1350
lemma drop_drop [simp]: "drop n (drop m xs) = drop (n + m) xs"
nipkow@24526
  1351
apply (induct m arbitrary: xs, auto)
paulson@14208
  1352
apply (case_tac xs, auto)
nipkow@13145
  1353
done
wenzelm@13114
  1354
nipkow@24526
  1355
lemma take_drop: "take n (drop m xs) = drop m (take (n + m) xs)"
nipkow@24526
  1356
apply (induct m arbitrary: xs n, auto)
paulson@14208
  1357
apply (case_tac xs, auto)
nipkow@13145
  1358
done
wenzelm@13114
  1359
nipkow@24526
  1360
lemma drop_take: "drop n (take m xs) = take (m-n) (drop n xs)"
nipkow@24526
  1361
apply(induct xs arbitrary: m n)
nipkow@14802
  1362
 apply simp
nipkow@14802
  1363
apply(simp add: take_Cons drop_Cons split:nat.split)
nipkow@14802
  1364
done
nipkow@14802
  1365
nipkow@24526
  1366
lemma append_take_drop_id [simp]: "take n xs @ drop n xs = xs"
nipkow@24526
  1367
apply (induct n arbitrary: xs, auto)
paulson@14208
  1368
apply (case_tac xs, auto)
nipkow@13145
  1369
done
wenzelm@13114
  1370
nipkow@24526
  1371
lemma take_eq_Nil[simp]: "(take n xs = []) = (n = 0 \<or> xs = [])"
nipkow@24526
  1372
apply(induct xs arbitrary: n)
nipkow@15110
  1373
 apply simp
nipkow@15110
  1374
apply(simp add:take_Cons split:nat.split)
nipkow@15110
  1375
done
nipkow@15110
  1376
nipkow@24526
  1377
lemma drop_eq_Nil[simp]: "(drop n xs = []) = (length xs <= n)"
nipkow@24526
  1378
apply(induct xs arbitrary: n)
nipkow@15110
  1379
apply simp
nipkow@15110
  1380
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1381
done
nipkow@15110
  1382
nipkow@24526
  1383
lemma take_map: "take n (map f xs) = map f (take n xs)"
nipkow@24526
  1384
apply (induct n arbitrary: xs, auto)
paulson@14208
  1385
apply (case_tac xs, auto)
nipkow@13145
  1386
done
wenzelm@13114
  1387
nipkow@24526
  1388
lemma drop_map: "drop n (map f xs) = map f (drop n xs)"
nipkow@24526
  1389
apply (induct n arbitrary: xs, auto)
paulson@14208
  1390
apply (case_tac xs, auto)
nipkow@13145
  1391
done
wenzelm@13114
  1392
nipkow@24526
  1393
lemma rev_take: "rev (take i xs) = drop (length xs - i) (rev xs)"
nipkow@24526
  1394
apply (induct xs arbitrary: i, auto)
paulson@14208
  1395
apply (case_tac i, auto)
nipkow@13145
  1396
done
wenzelm@13114
  1397
nipkow@24526
  1398
lemma rev_drop: "rev (drop i xs) = take (length xs - i) (rev xs)"
nipkow@24526
  1399
apply (induct xs arbitrary: i, auto)
paulson@14208
  1400
apply (case_tac i, auto)
nipkow@13145
  1401
done
wenzelm@13114
  1402
nipkow@24526
  1403
lemma nth_take [simp]: "i < n ==> (take n xs)!i = xs!i"
nipkow@24526
  1404
apply (induct xs arbitrary: i n, auto)
paulson@14208
  1405
apply (case_tac n, blast)
paulson@14208
  1406
apply (case_tac i, auto)
nipkow@13145
  1407
done
wenzelm@13114
  1408
wenzelm@13142
  1409
lemma nth_drop [simp]:
nipkow@24526
  1410
  "n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
nipkow@24526
  1411
apply (induct n arbitrary: xs i, auto)
paulson@14208
  1412
apply (case_tac xs, auto)
nipkow@13145
  1413
done
nipkow@3507
  1414
nipkow@18423
  1415
lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n"
nipkow@18423
  1416
by(simp add: hd_conv_nth)
nipkow@18423
  1417
nipkow@24526
  1418
lemma set_take_subset: "set(take n xs) \<subseteq> set xs"
nipkow@24526
  1419
by(induct xs arbitrary: n)(auto simp:take_Cons split:nat.split)
nipkow@24526
  1420
nipkow@24526
  1421
lemma set_drop_subset: "set(drop n xs) \<subseteq> set xs"
nipkow@24526
  1422
by(induct xs arbitrary: n)(auto simp:drop_Cons split:nat.split)
nipkow@14025
  1423
nipkow@14187
  1424
lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1425
using set_take_subset by fast
nipkow@14187
  1426
nipkow@14187
  1427
lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
nipkow@14187
  1428
using set_drop_subset by fast
nipkow@14187
  1429
wenzelm@13114
  1430
lemma append_eq_conv_conj:
nipkow@24526
  1431
  "(xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
nipkow@24526
  1432
apply (induct xs arbitrary: zs, simp, clarsimp)
paulson@14208
  1433
apply (case_tac zs, auto)
nipkow@13145
  1434
done
wenzelm@13142
  1435
nipkow@24526
  1436
lemma take_add: 
nipkow@24526
  1437
  "i+j \<le> length(xs) \<Longrightarrow> take (i+j) xs = take i xs @ take j (drop i xs)"
nipkow@24526
  1438
apply (induct xs arbitrary: i, auto) 
nipkow@24526
  1439
apply (case_tac i, simp_all)
paulson@14050
  1440
done
paulson@14050
  1441
nipkow@14300
  1442
lemma append_eq_append_conv_if:
nipkow@24526
  1443
 "(xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
nipkow@14300
  1444
  (if size xs\<^isub>1 \<le> size ys\<^isub>1
nipkow@14300
  1445
   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
  1446
   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
  1447
apply(induct xs\<^isub>1 arbitrary: ys\<^isub>1)
nipkow@14300
  1448
 apply simp
nipkow@14300
  1449
apply(case_tac ys\<^isub>1)
nipkow@14300
  1450
apply simp_all
nipkow@14300
  1451
done
nipkow@14300
  1452
nipkow@15110
  1453
lemma take_hd_drop:
nipkow@24526
  1454
  "n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (n+1) xs"
nipkow@24526
  1455
apply(induct xs arbitrary: n)
nipkow@15110
  1456
apply simp
nipkow@15110
  1457
apply(simp add:drop_Cons split:nat.split)
nipkow@15110
  1458
done
nipkow@15110
  1459
nipkow@17501
  1460
lemma id_take_nth_drop:
nipkow@17501
  1461
 "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 
nipkow@17501
  1462
proof -
nipkow@17501
  1463
  assume si: "i < length xs"
nipkow@17501
  1464
  hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
nipkow@17501
  1465
  moreover
nipkow@17501
  1466
  from si have "take (Suc i) xs = take i xs @ [xs!i]"
nipkow@17501
  1467
    apply (rule_tac take_Suc_conv_app_nth) by arith
nipkow@17501
  1468
  ultimately show ?thesis by auto
nipkow@17501
  1469
qed
nipkow@17501
  1470
  
nipkow@17501
  1471
lemma upd_conv_take_nth_drop:
nipkow@17501
  1472
 "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1473
proof -
nipkow@17501
  1474
  assume i: "i < length xs"
nipkow@17501
  1475
  have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
nipkow@17501
  1476
    by(rule arg_cong[OF id_take_nth_drop[OF i]])
nipkow@17501
  1477
  also have "\<dots> = take i xs @ a # drop (Suc i) xs"
nipkow@17501
  1478
    using i by (simp add: list_update_append)
nipkow@17501
  1479
  finally show ?thesis .
nipkow@17501
  1480
qed
nipkow@17501
  1481
haftmann@24796
  1482
lemma nth_drop':
haftmann@24796
  1483
  "i < length xs \<Longrightarrow> xs ! i # drop (Suc i) xs = drop i xs"
haftmann@24796
  1484
apply (induct i arbitrary: xs)
haftmann@24796
  1485
apply (simp add: neq_Nil_conv)
haftmann@24796
  1486
apply (erule exE)+
haftmann@24796
  1487
apply simp
haftmann@24796
  1488
apply (case_tac xs)
haftmann@24796
  1489
apply simp_all
haftmann@24796
  1490
done
haftmann@24796
  1491
wenzelm@13114
  1492
nipkow@15392
  1493
subsubsection {* @{text takeWhile} and @{text dropWhile} *}
wenzelm@13114
  1494
wenzelm@13142
  1495
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
nipkow@13145
  1496
by (induct xs) auto
wenzelm@13114
  1497
wenzelm@13142
  1498
lemma takeWhile_append1 [simp]:
nipkow@13145
  1499
"[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
nipkow@13145
  1500
by (induct xs) auto
wenzelm@13114
  1501
wenzelm@13142
  1502
lemma takeWhile_append2 [simp]:
nipkow@13145
  1503
"(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
nipkow@13145
  1504
by (induct xs) auto
wenzelm@13114
  1505
wenzelm@13142
  1506
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
nipkow@13145
  1507
by (induct xs) auto
wenzelm@13114
  1508
wenzelm@13142
  1509
lemma dropWhile_append1 [simp]:
nipkow@13145
  1510
"[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
nipkow@13145
  1511
by (induct xs) auto
wenzelm@13114
  1512
wenzelm@13142
  1513
lemma dropWhile_append2 [simp]:
nipkow@13145
  1514
"(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
nipkow@13145
  1515
by (induct xs) auto
wenzelm@13114
  1516
krauss@23971
  1517
lemma set_takeWhileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
nipkow@13145
  1518
by (induct xs) (auto split: split_if_asm)
wenzelm@13114
  1519
nipkow@13913
  1520
lemma takeWhile_eq_all_conv[simp]:
nipkow@13913
  1521
 "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1522
by(induct xs, auto)
nipkow@13913
  1523
nipkow@13913
  1524
lemma dropWhile_eq_Nil_conv[simp]:
nipkow@13913
  1525
 "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
nipkow@13913
  1526
by(induct xs, auto)
nipkow@13913
  1527
nipkow@13913
  1528
lemma dropWhile_eq_Cons_conv:
nipkow@13913
  1529
 "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
nipkow@13913
  1530
by(induct xs, auto)
nipkow@13913
  1531
nipkow@17501
  1532
text{* The following two lemmmas could be generalized to an arbitrary
nipkow@17501
  1533
property. *}
nipkow@17501
  1534
nipkow@17501
  1535
lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1536
 takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
nipkow@17501
  1537
by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
nipkow@17501
  1538
nipkow@17501
  1539
lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
nipkow@17501
  1540
  dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
nipkow@17501
  1541
apply(induct xs)
nipkow@17501
  1542
 apply simp
nipkow@17501
  1543
apply auto
nipkow@17501
  1544
apply(subst dropWhile_append2)
nipkow@17501
  1545
apply auto
nipkow@17501
  1546
done
nipkow@17501
  1547
nipkow@18423
  1548
lemma takeWhile_not_last:
nipkow@18423
  1549
 "\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
nipkow@18423
  1550
apply(induct xs)
nipkow@18423
  1551
 apply simp
nipkow@18423
  1552
apply(case_tac xs)
nipkow@18423
  1553
apply(auto)
nipkow@18423
  1554
done
nipkow@18423
  1555
krauss@19770
  1556
lemma takeWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1557
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1558
  ==> takeWhile P l = takeWhile Q k"
nipkow@24349
  1559
by (induct k arbitrary: l) (simp_all)
krauss@18336
  1560
krauss@19770
  1561
lemma dropWhile_cong [fundef_cong, recdef_cong]:
krauss@18336
  1562
  "[| l = k; !!x. x : set l ==> P x = Q x |] 
krauss@18336
  1563
  ==> dropWhile P l = dropWhile Q k"
nipkow@24349
  1564
by (induct k arbitrary: l, simp_all)
krauss@18336
  1565
wenzelm@13114
  1566
nipkow@15392
  1567
subsubsection {* @{text zip} *}
wenzelm@13114
  1568
wenzelm@13142
  1569
lemma zip_Nil [simp]: "zip [] ys = []"
nipkow@13145
  1570
by (induct ys) auto
wenzelm@13114
  1571
wenzelm@13142
  1572
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
nipkow@13145
  1573
by simp
wenzelm@13114
  1574
wenzelm@13142
  1575
declare zip_Cons [simp del]
wenzelm@13114
  1576
nipkow@15281
  1577
lemma zip_Cons1:
nipkow@15281
  1578
 "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"
nipkow@15281
  1579
by(auto split:list.split)
nipkow@15281
  1580
wenzelm@13142
  1581
lemma length_zip [simp]:
krauss@22493
  1582
"length (zip xs ys) = min (length xs) (length ys)"
krauss@22493
  1583
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  1584
wenzelm@13114
  1585
lemma zip_append1:
krauss@22493
  1586
"zip (xs @ ys) zs =
nipkow@13145
  1587
zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
krauss@22493
  1588
by (induct xs zs rule:list_induct2') auto
wenzelm@13114
  1589
wenzelm@13114
  1590
lemma zip_append2:
krauss@22493
  1591
"zip xs (ys @ zs) =
nipkow@13145
  1592
zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
krauss@22493
  1593
by (induct xs ys rule:list_induct2') auto
wenzelm@13114
  1594
wenzelm@13142
  1595
lemma zip_append [simp]:
wenzelm@13142
  1596
 "[| length xs = length us; length ys = length vs |] ==>
nipkow@13145
  1597
zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
nipkow@13145
  1598
by (simp add: zip_append1)
wenzelm@13114
  1599
wenzelm@13114
  1600
lemma zip_rev:
nipkow@14247
  1601
"length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
nipkow@14247
  1602
by (induct rule:list_induct2, simp_all)
wenzelm@13114
  1603
nipkow@23096
  1604
lemma map_zip_map:
nipkow@23096
  1605
 "map f (zip (map g xs) ys) = map (%(x,y). f(g x, y)) (zip xs ys)"
nipkow@23096
  1606
apply(induct xs arbitrary:ys) apply simp
nipkow@23096
  1607
apply(case_tac ys)
nipkow@23096
  1608
apply simp_all
nipkow@23096
  1609
done
nipkow@23096
  1610
nipkow@23096
  1611
lemma map_zip_map2:
nipkow@23096
  1612
 "map f (zip xs (map g ys)) = map (%(x,y). f(x, g y)) (zip xs ys)"
nipkow@23096
  1613
apply(induct xs arbitrary:ys) apply simp
nipkow@23096
  1614
apply(case_tac ys)
nipkow@23096
  1615
apply simp_all
nipkow@23096
  1616
done
nipkow@23096
  1617
wenzelm@13142
  1618
lemma nth_zip [simp]:
nipkow@24526
  1619
"[| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
nipkow@24526
  1620
apply (induct ys arbitrary: i xs, simp)
nipkow@13145
  1621
apply (case_tac xs)
nipkow@13145
  1622
 apply (simp_all add: nth.simps split: nat.split)
nipkow@13145
  1623
done
wenzelm@13114
  1624
wenzelm@13114
  1625
lemma set_zip:
nipkow@13145
  1626
"set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
nipkow@13145
  1627
by (simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
  1628
wenzelm@13114
  1629
lemma zip_update:
nipkow@13145
  1630
"length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
nipkow@13145
  1631
by (rule sym, simp add: update_zip)
wenzelm@13114
  1632
wenzelm@13142
  1633
lemma zip_replicate [simp]:
nipkow@24526
  1634
  "zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
nipkow@24526
  1635
apply (induct i arbitrary: j, auto)
paulson@14208
  1636
apply (case_tac j, auto)
nipkow@13145
  1637
done
wenzelm@13114
  1638
nipkow@19487
  1639
lemma take_zip:
nipkow@24526
  1640
  "take n (zip xs ys) = zip (take n xs) (take n ys)"
nipkow@24526
  1641
apply (induct n arbitrary: xs ys)
nipkow@19487
  1642
 apply simp
nipkow@19487
  1643
apply (case_tac xs, simp)
nipkow@19487
  1644
apply (case_tac ys, simp_all)
nipkow@19487
  1645
done
nipkow@19487
  1646
nipkow@19487
  1647
lemma drop_zip:
nipkow@24526
  1648
  "drop n (zip xs ys) = zip (drop n xs) (drop n ys)"
nipkow@24526
  1649
apply (induct n arbitrary: xs ys)
nipkow@19487
  1650
 apply simp
nipkow@19487
  1651
apply (case_tac xs, simp)
nipkow@19487
  1652
apply (case_tac ys, simp_all)
nipkow@19487
  1653
done
nipkow@19487
  1654
krauss@22493
  1655
lemma set_zip_leftD:
krauss@22493
  1656
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> x \<in> set xs"
krauss@22493
  1657
by (induct xs ys rule:list_induct2') auto
krauss@22493
  1658
krauss@22493
  1659
lemma set_zip_rightD:
krauss@22493
  1660
  "(x,y)\<in> set (zip xs ys) \<Longrightarrow> y \<in> set ys"
krauss@22493
  1661
by (induct xs ys rule:list_induct2') auto
wenzelm@13142
  1662
nipkow@23983
  1663
lemma in_set_zipE:
nipkow@23983
  1664
  "(x,y) : set(zip xs ys) \<Longrightarrow> (\<lbrakk> x : set xs; y : set ys \<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
nipkow@23983
  1665
by(blast dest: set_zip_leftD set_zip_rightD)
nipkow@23983
  1666
nipkow@15392
  1667
subsubsection {* @{text list_all2} *}
wenzelm@13114
  1668
kleing@14316
  1669
lemma list_all2_lengthD [intro?]: 
kleing@14316
  1670
  "list_all2 P xs ys ==> length xs = length ys"
nipkow@24349
  1671
by (simp add: list_all2_def)
haftmann@19607
  1672
haftmann@19787
  1673
lemma list_all2_Nil [iff, code]: "list_all2 P [] ys = (ys = [])"
nipkow@24349
  1674
by (simp add: list_all2_def)
haftmann@19607
  1675
haftmann@19787
  1676
lemma list_all2_Nil2 [iff, code]: "list_all2 P xs [] = (xs = [])"
nipkow@24349
  1677
by (simp add: list_all2_def)
haftmann@19607
  1678
haftmann@19607
  1679
lemma list_all2_Cons [iff, code]:
haftmann@19607
  1680
  "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
nipkow@24349
  1681
by (auto simp add: list_all2_def)
wenzelm@13114
  1682
wenzelm@13114
  1683
lemma list_all2_Cons1:
nipkow@13145
  1684
"list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
nipkow@13145
  1685
by (cases ys) auto
wenzelm@13114
  1686
wenzelm@13114
  1687
lemma list_all2_Cons2:
nipkow@13145
  1688
"list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
nipkow@13145
  1689
by (cases xs) auto
wenzelm@13114
  1690
wenzelm@13142
  1691
lemma list_all2_rev [iff]:
nipkow@13145
  1692
"list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
nipkow@13145
  1693
by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
  1694
kleing@13863
  1695
lemma list_all2_rev1:
kleing@13863
  1696
"list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
kleing@13863
  1697
by (subst list_all2_rev [symmetric]) simp
kleing@13863
  1698
wenzelm@13114
  1699
lemma list_all2_append1:
nipkow@13145
  1700
"list_all2 P (xs @ ys) zs =
nipkow@13145
  1701
(EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
nipkow@13145
  1702
list_all2 P xs us \<and> list_all2 P ys vs)"
nipkow@13145
  1703
apply (simp add: list_all2_def zip_append1)
nipkow@13145
  1704
apply (rule iffI)
nipkow@13145
  1705
 apply (rule_tac x = "take (length xs) zs" in exI)
nipkow@13145
  1706
 apply (rule_tac x = "drop (length xs) zs" in exI)
paulson@14208
  1707
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  1708
apply (simp add: ball_Un)
nipkow@13145
  1709
done
wenzelm@13114
  1710
wenzelm@13114
  1711
lemma list_all2_append2:
nipkow@13145
  1712
"list_all2 P xs (ys @ zs) =
nipkow@13145
  1713
(EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
nipkow@13145
  1714
list_all2 P us ys \<and> list_all2 P vs zs)"
nipkow@13145
  1715
apply (simp add: list_all2_def zip_append2)
nipkow@13145
  1716
apply (rule iffI)
nipkow@13145
  1717
 apply (rule_tac x = "take (length ys) xs" in exI)
nipkow@13145
  1718
 apply (rule_tac x = "drop (length ys) xs" in exI)
paulson@14208
  1719
 apply (force split: nat_diff_split simp add: min_def, clarify)
nipkow@13145
  1720
apply (simp add: ball_Un)
nipkow@13145
  1721
done
wenzelm@13114
  1722
kleing@13863
  1723
lemma list_all2_append:
nipkow@14247
  1724
  "length xs = length ys \<Longrightarrow>
nipkow@14247
  1725
  list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
nipkow@14247
  1726
by (induct rule:list_induct2, simp_all)
kleing@13863
  1727
kleing@13863
  1728
lemma list_all2_appendI [intro?, trans]:
kleing@13863
  1729
  "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
nipkow@24349
  1730
by (simp add: list_all2_append list_all2_lengthD)
kleing@13863
  1731
wenzelm@13114
  1732
lemma list_all2_conv_all_nth:
nipkow@13145
  1733
"list_all2 P xs ys =
nipkow@13145
  1734
(length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
nipkow@13145
  1735
by (force simp add: list_all2_def set_zip)
wenzelm@13114
  1736
berghofe@13883
  1737
lemma list_all2_trans:
berghofe@13883
  1738
  assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
berghofe@13883
  1739
  shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
berghofe@13883
  1740
        (is "!!bs cs. PROP ?Q as bs cs")
berghofe@13883
  1741
proof (induct as)
berghofe@13883
  1742
  fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
berghofe@13883
  1743
  show "!!cs. PROP ?Q (x # xs) bs cs"
berghofe@13883
  1744
  proof (induct bs)
berghofe@13883
  1745
    fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
berghofe@13883
  1746
    show "PROP ?Q (x # xs) (y # ys) cs"
berghofe@13883
  1747
      by (induct cs) (auto intro: tr I1 I2)
berghofe@13883
  1748
  qed simp
berghofe@13883
  1749
qed simp
berghofe@13883
  1750
kleing@13863
  1751
lemma list_all2_all_nthI [intro?]:
kleing@13863
  1752
  "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
nipkow@24349
  1753
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1754
paulson@14395
  1755
lemma list_all2I:
paulson@14395
  1756
  "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
nipkow@24349
  1757
by (simp add: list_all2_def)
paulson@14395
  1758
kleing@14328
  1759
lemma list_all2_nthD:
kleing@13863
  1760
  "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  1761
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1762
nipkow@14302
  1763
lemma list_all2_nthD2:
nipkow@14302
  1764
  "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
nipkow@24349
  1765
by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
nipkow@14302
  1766
kleing@13863
  1767
lemma list_all2_map1: 
kleing@13863
  1768
  "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
nipkow@24349
  1769
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1770
kleing@13863
  1771
lemma list_all2_map2: 
kleing@13863
  1772
  "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
nipkow@24349
  1773
by (auto simp add: list_all2_conv_all_nth)
kleing@13863
  1774
kleing@14316
  1775
lemma list_all2_refl [intro?]:
kleing@13863
  1776
  "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
nipkow@24349
  1777
by (simp add: list_all2_conv_all_nth)
kleing@13863
  1778
kleing@13863
  1779
lemma list_all2_update_cong:
kleing@13863
  1780
  "\<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
  1781
by (simp add: list_all2_conv_all_nth nth_list_update)
kleing@13863
  1782
kleing@13863
  1783
lemma list_all2_update_cong2:
kleing@13863
  1784
  "\<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
  1785
by (simp add: list_all2_lengthD list_all2_update_cong)
kleing@13863
  1786
nipkow@14302
  1787
lemma list_all2_takeI [simp,intro?]:
nipkow@24526
  1788
  "list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
nipkow@24526
  1789
apply (induct xs arbitrary: n ys)
nipkow@24526
  1790
 apply simp
nipkow@24526
  1791
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  1792
apply (case_tac n)
nipkow@24526
  1793
apply auto
nipkow@24526
  1794
done
nipkow@14302
  1795
nipkow@14302
  1796
lemma list_all2_dropI [simp,intro?]:
nipkow@24526
  1797
  "list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"
nipkow@24526
  1798
apply (induct as arbitrary: n bs, simp)
nipkow@24526
  1799
apply (clarsimp simp add: list_all2_Cons1)
nipkow@24526
  1800
apply (case_tac n, simp, simp)
nipkow@24526
  1801
done
kleing@13863
  1802
kleing@14327
  1803
lemma list_all2_mono [intro?]:
nipkow@24526
  1804
  "list_all2 P xs ys \<Longrightarrow> (\<And>xs ys. P xs ys \<Longrightarrow> Q xs ys) \<Longrightarrow> list_all2 Q xs ys"
nipkow@24526
  1805
apply (induct xs arbitrary: ys, simp)
nipkow@24526
  1806
apply (case_tac ys, auto)
nipkow@24526
  1807
done
kleing@13863
  1808
haftmann@22551
  1809
lemma list_all2_eq:
haftmann@22551
  1810
  "xs = ys \<longleftrightarrow> list_all2 (op =) xs ys"
nipkow@24349
  1811
by (induct xs ys rule: list_induct2') auto
haftmann@22551
  1812
wenzelm@13142
  1813
nipkow@15392
  1814
subsubsection {* @{text foldl} and @{text foldr} *}
wenzelm@13142
  1815
wenzelm@13142
  1816
lemma foldl_append [simp]:
nipkow@24526
  1817
  "foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
nipkow@24526
  1818
by (induct xs arbitrary: a) auto
wenzelm@13142
  1819
nipkow@14402
  1820
lemma foldr_append[simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"
nipkow@14402
  1821
by (induct xs) auto
nipkow@14402
  1822
nipkow@23096
  1823
lemma foldr_map: "foldr g (map f xs) a = foldr (g o f) xs a"
nipkow@23096
  1824
by(induct xs) simp_all
nipkow@23096
  1825
nipkow@24449
  1826
text{* For efficient code generation: avoid intermediate list. *}
nipkow@24449
  1827
lemma foldl_map[code unfold]:
nipkow@24449
  1828
  "foldl g a (map f xs) = foldl (%a x. g a (f x)) a xs"
nipkow@23096
  1829
by(induct xs arbitrary:a) simp_all
nipkow@23096
  1830
krauss@19770
  1831
lemma foldl_cong [fundef_cong, recdef_cong]:
krauss@18336
  1832
  "[| a = b; l = k; !!a x. x : set l ==> f a x = g a x |] 
krauss@18336
  1833
  ==> foldl f a l = foldl g b k"
nipkow@24349
  1834
by (induct k arbitrary: a b l) simp_all
krauss@18336
  1835
krauss@19770
  1836
lemma foldr_cong [fundef_cong, recdef_cong]:
krauss@18336
  1837
  "[| a = b; l = k; !!a x. x : set l ==> f x a = g x a |] 
krauss@18336
  1838
  ==> foldr f l a = foldr g k b"
nipkow@24349
  1839
by (induct k arbitrary: a b l) simp_all
krauss@18336
  1840
nipkow@24449
  1841
lemma (in semigroup_add) foldl_assoc:
haftmann@25062
  1842
shows "foldl op+ (x+y) zs = x + (foldl op+ y zs)"
nipkow@24449
  1843
by (induct zs arbitrary: y) (simp_all add:add_assoc)
nipkow@24449
  1844
nipkow@24449
  1845
lemma (in monoid_add) foldl_absorb0:
haftmann@25062
  1846
shows "x + (foldl op+ 0 zs) = foldl op+ x zs"
nipkow@24449
  1847
by (induct zs) (simp_all add:foldl_assoc)
nipkow@24449
  1848
nipkow@24449
  1849
nipkow@23096
  1850
text{* The ``First Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  1851
nipkow@23096
  1852
lemma foldl_foldr1_lemma:
nipkow@23096
  1853
 "foldl op + a xs = a + foldr op + xs (0\<Colon>'a::monoid_add)"
nipkow@23096
  1854
by (induct xs arbitrary: a) (auto simp:add_assoc)
nipkow@23096
  1855
nipkow@23096
  1856
corollary foldl_foldr1:
nipkow@23096
  1857
 "foldl op + 0 xs = foldr op + xs (0\<Colon>'a::monoid_add)"
nipkow@23096
  1858
by (simp add:foldl_foldr1_lemma)
nipkow@23096
  1859
nipkow@23096
  1860
nipkow@23096
  1861
text{* The ``Third Duality Theorem'' in Bird \& Wadler: *}
nipkow@23096
  1862
nipkow@14402
  1863
lemma foldr_foldl: "foldr f xs a = foldl (%x y. f y x) a (rev xs)"
nipkow@14402
  1864
by (induct xs) auto
nipkow@14402
  1865
nipkow@14402
  1866
lemma foldl_foldr: "foldl f a xs = foldr (%x y. f y x) (rev xs) a"
nipkow@14402
  1867
by (simp add: foldr_foldl [of "%x y. f y x" "rev xs"])
nipkow@14402
  1868
haftmann@25062
  1869
lemma (in ab_semigroup_add) foldr_conv_foldl: "foldr op + xs a = foldl op + a xs"
chaieb@24471
  1870
  by (induct xs, auto simp add: foldl_assoc add_commute)
chaieb@24471
  1871
wenzelm@13142
  1872
text {*
nipkow@13145
  1873
Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
nipkow@13145
  1874
difficult to use because it requires an additional transitivity step.
wenzelm@13142
  1875
*}
wenzelm@13142
  1876
nipkow@24526
  1877
lemma start_le_sum: "(m::nat) <= n ==> m <= foldl (op +) n ns"
nipkow@24526
  1878
by (induct ns arbitrary: n) auto
nipkow@24526
  1879
nipkow@24526
  1880
lemma elem_le_sum: "(n::nat) : set ns ==> n <= foldl (op +) 0 ns"
nipkow@13145
  1881
by (force intro: start_le_sum simp add: in_set_conv_decomp)
wenzelm@13142
  1882
wenzelm@13142
  1883
lemma sum_eq_0_conv [iff]:
nipkow@24526
  1884
  "(foldl (op +) (m::nat) ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
nipkow@24526
  1885
by (induct ns arbitrary: m) auto
wenzelm@13114
  1886
chaieb@24471
  1887
lemma foldr_invariant: 
chaieb@24471
  1888
  "\<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
  1889
  by (induct xs, simp_all)
chaieb@24471
  1890
chaieb@24471
  1891
lemma foldl_invariant: 
chaieb@24471
  1892
  "\<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
  1893
  by (induct xs arbitrary: x, simp_all)
chaieb@24471
  1894
nipkow@24449
  1895
text{* @{const foldl} and @{text concat} *}
nipkow@24449
  1896
nipkow@24449
  1897
lemma concat_conv_foldl: "concat xss = foldl op@ [] xss"
nipkow@24449
  1898
by (induct xss) (simp_all add:monoid_append.foldl_absorb0)
nipkow@24449
  1899
nipkow@24449
  1900
lemma foldl_conv_concat:
nipkow@24449
  1901
  "foldl (op @) xs xxs = xs @ (concat xxs)"
nipkow@24449
  1902
by(simp add:concat_conv_foldl monoid_append.foldl_absorb0)
nipkow@24449
  1903
nipkow@23096
  1904
subsubsection {* List summation: @{const listsum} and @{text"\<Sum>"}*}
nipkow@23096
  1905
nipkow@24449
  1906
lemma listsum_append[simp]: "listsum (xs @ ys) = listsum xs + listsum ys"
nipkow@24449
  1907
by (induct xs) (simp_all add:add_assoc)
nipkow@24449
  1908
nipkow@24449
  1909
lemma listsum_rev[simp]:
nipkow@24449
  1910
fixes xs :: "'a::comm_monoid_add list"
nipkow@24449
  1911
shows "listsum (rev xs) = listsum xs"
nipkow@24449
  1912
by (induct xs) (simp_all add:add_ac)
nipkow@24449
  1913
nipkow@23096
  1914
lemma listsum_foldr:
nipkow@23096
  1915
 "listsum xs = foldr (op +) xs 0"
nipkow@23096
  1916
by(induct xs) auto
nipkow@23096
  1917
nipkow@24449
  1918
text{* For efficient code generation ---
nipkow@24449
  1919
       @{const listsum} is not tail recursive but @{const foldl} is. *}
nipkow@24449
  1920
lemma listsum[code unfold]: "listsum xs = foldl (op +) 0 xs"
nipkow@23096
  1921
by(simp add:listsum_foldr foldl_foldr1)
nipkow@23096
  1922
nipkow@24449
  1923
nipkow@23096
  1924
text{* Some syntactic sugar for summing a function over a list: *}
nipkow@23096
  1925
nipkow@23096
  1926
syntax
nipkow@23096
  1927
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3SUM _<-_. _)" [0, 51, 10] 10)
nipkow@23096
  1928
syntax (xsymbols)
nipkow@23096
  1929
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
nipkow@23096
  1930
syntax (HTML output)
nipkow@23096
  1931
  "_listsum" :: "pttrn => 'a list => 'b => 'b"    ("(3\<Sum>_\<leftarrow>_. _)" [0, 51, 10] 10)
nipkow@23096
  1932
nipkow@23096
  1933
translations -- {* Beware of argument permutation! *}
nipkow@23096
  1934
  "SUM x<-xs. b" == "CONST listsum (map (%x. b) xs)"
nipkow@23096
  1935
  "\<Sum>x\<leftarrow>xs. b" == "CONST listsum (map (%x. b) xs)"
nipkow@23096
  1936
nipkow@23096
  1937
lemma listsum_0 [simp]: "(\<Sum>x\<leftarrow>xs. 0) = 0"
nipkow@23096
  1938
by (induct xs) simp_all
nipkow@23096
  1939
nipkow@23096
  1940
text{* For non-Abelian groups @{text xs} needs to be reversed on one side: *}
nipkow@23096
  1941
lemma uminus_listsum_map:
nipkow@23096
  1942
 "- listsum (map f xs) = (listsum (map (uminus o f) xs) :: 'a::ab_group_add)"
nipkow@23096
  1943
by(induct xs) simp_all
nipkow@23096
  1944
wenzelm@13114
  1945
nipkow@24645
  1946
subsubsection {* @{text upt} *}
wenzelm@13114
  1947
nipkow@17090
  1948
lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"
nipkow@17090
  1949
-- {* simp does not terminate! *}
nipkow@13145
  1950
by (induct j) auto
wenzelm@13142
  1951
nipkow@15425
  1952
lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
nipkow@13145
  1953
by (subst upt_rec) simp
wenzelm@13114
  1954
nipkow@15425
  1955
lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)"
nipkow@15281
  1956
by(induct j)simp_all
nipkow@15281
  1957
nipkow@15281
  1958
lemma upt_eq_Cons_conv:
nipkow@24526
  1959
 "([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)"
nipkow@24526
  1960
apply(induct j arbitrary: x xs)
nipkow@15281
  1961
 apply simp
nipkow@15281
  1962
apply(clarsimp simp add: append_eq_Cons_conv)
nipkow@15281
  1963
apply arith
nipkow@15281
  1964
done
nipkow@15281
  1965
nipkow@15425
  1966
lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]"
nipkow@13145
  1967
-- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
nipkow@13145
  1968
by simp
wenzelm@13114
  1969
nipkow@15425
  1970
lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]"
paulson@24632
  1971
by (metis upt_rec)
wenzelm@13114
  1972
nipkow@15425
  1973
lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]"
nipkow@13145
  1974
-- {* LOOPS as a simprule, since @{text "j <= j"}. *}
nipkow@13145
  1975
by (induct k) auto
wenzelm@13114
  1976
nipkow@15425
  1977
lemma length_upt [simp]: "length [i..<j] = j - i"
nipkow@13145
  1978
by (induct j) (auto simp add: Suc_diff_le)
wenzelm@13114
  1979
nipkow@15425
  1980
lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k"
nipkow@13145
  1981
apply (induct j)
nipkow@13145
  1982
apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
nipkow@13145
  1983
done
wenzelm@13114
  1984
nipkow@17906
  1985
nipkow@17906
  1986
lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i"
nipkow@17906
  1987
by(simp add:upt_conv_Cons)
nipkow@17906
  1988
nipkow@17906
  1989
lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1"
nipkow@17906
  1990
apply(cases j)
nipkow@17906
  1991
 apply simp
nipkow@17906
  1992
by(simp add:upt_Suc_append)
nipkow@17906
  1993
nipkow@24526
  1994
lemma take_upt [simp]: "i+m <= n ==> take m [i..<n] = [i..<i+m]"
nipkow@24526
  1995
apply (induct m arbitrary: i, simp)
nipkow@13145
  1996
apply (subst upt_rec)
nipkow@13145
  1997
apply (rule sym)
nipkow@13145
  1998
apply (subst upt_rec)
nipkow@13145
  1999
apply (simp del: upt.simps)
nipkow@13145
  2000
done
nipkow@3507
  2001
nipkow@17501
  2002
lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"
nipkow@17501
  2003
apply(induct j)
nipkow@17501
  2004
apply auto
nipkow@17501
  2005
done
nipkow@17501
  2006
nipkow@24645
  2007
lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..<Suc n]"
nipkow@13145
  2008
by (induct n) auto
wenzelm@13114
  2009
nipkow@24526
  2010
lemma nth_map_upt: "i < n-m ==> (map f [m..<n]) ! i = f(m+i)"
nipkow@24526
  2011
apply (induct n m  arbitrary: i rule: diff_induct)
nipkow@13145
  2012
prefer 3 apply (subst map_Suc_upt[symmetric])
nipkow@13145
  2013
apply (auto simp add: less_diff_conv nth_upt)
nipkow@13145
  2014
done
wenzelm@13114
  2015
berghofe@13883
  2016
lemma nth_take_lemma:
nipkow@24526
  2017
  "k <= length xs ==> k <= length ys ==>
berghofe@13883
  2018
     (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
nipkow@24526
  2019
apply (atomize, induct k arbitrary: xs ys)
paulson@14208
  2020
apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
nipkow@13145
  2021
txt {* Both lists must be non-empty *}
paulson@14208
  2022
apply (case_tac xs, simp)
paulson@14208
  2023
apply (case_tac ys, clarify)
nipkow@13145
  2024
 apply (simp (no_asm_use))
nipkow@13145
  2025
apply clarify
nipkow@13145
  2026
txt {* prenexing's needed, not miniscoping *}
nipkow@13145
  2027
apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
nipkow@13145
  2028
apply blast
nipkow@13145
  2029
done
wenzelm@13114
  2030
wenzelm@13114
  2031
lemma nth_equalityI:
wenzelm@13114
  2032
 "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
nipkow@13145
  2033
apply (frule nth_take_lemma [OF le_refl eq_imp_le])
nipkow@13145
  2034
apply (simp_all add: take_all)
nipkow@13145
  2035
done
wenzelm@13142
  2036
haftmann@24796
  2037
lemma map_nth:
haftmann@24796
  2038
  "map (\<lambda>i. xs ! i) [0..<length xs] = xs"
haftmann@24796
  2039
  by (rule nth_equalityI, auto)
haftmann@24796
  2040
kleing@13863
  2041
(* needs nth_equalityI *)
kleing@13863
  2042
lemma list_all2_antisym:
kleing@13863
  2043
  "\<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
  2044
  \<Longrightarrow> xs = ys"
kleing@13863
  2045
  apply (simp add: list_all2_conv_all_nth) 
paulson@14208
  2046
  apply (rule nth_equalityI, blast, simp)
kleing@13863
  2047
  done
kleing@13863
  2048
wenzelm@13142
  2049
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
nipkow@13145
  2050
-- {* The famous take-lemma. *}
nipkow@13145
  2051
apply (drule_tac x = "max (length xs) (length ys)" in spec)
nipkow@13145
  2052
apply (simp add: le_max_iff_disj take_all)
nipkow@13145
  2053
done
wenzelm@13142
  2054
wenzelm@13142
  2055
nipkow@15302
  2056
lemma take_Cons':
nipkow@15302
  2057
     "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
nipkow@15302
  2058
by (cases n) simp_all
nipkow@15302
  2059
nipkow@15302
  2060
lemma drop_Cons':
nipkow@15302
  2061
     "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
nipkow@15302
  2062
by (cases n) simp_all
nipkow@15302
  2063
nipkow@15302
  2064
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
nipkow@15302
  2065
by (cases n) simp_all
nipkow@15302
  2066
paulson@18622
  2067
lemmas take_Cons_number_of = take_Cons'[of "number_of v",standard]
paulson@18622
  2068
lemmas drop_Cons_number_of = drop_Cons'[of "number_of v",standard]
paulson@18622
  2069
lemmas nth_Cons_number_of = nth_Cons'[of _ _ "number_of v",standard]
paulson@18622
  2070
paulson@18622
  2071
declare take_Cons_number_of [simp] 
paulson@18622
  2072
        drop_Cons_number_of [simp] 
paulson@18622
  2073
        nth_Cons_number_of [simp] 
nipkow@15302
  2074
nipkow@15302
  2075
nipkow@15392
  2076
subsubsection {* @{text "distinct"} and @{text remdups} *}
wenzelm@13142
  2077
wenzelm@13142
  2078
lemma distinct_append [simp]:
nipkow@13145
  2079
"distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
nipkow@13145
  2080
by (induct xs) auto
wenzelm@13142
  2081
nipkow@15305
  2082
lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs"
nipkow@15305
  2083
by(induct xs) auto
nipkow@15305
  2084
wenzelm@13142
  2085
lemma set_remdups [simp]: "set (remdups xs) = set xs"
nipkow@13145
  2086
by (induct xs) (auto simp add: insert_absorb)
wenzelm@13142
  2087
wenzelm@13142
  2088
lemma distinct_remdups [iff]: "distinct (remdups xs)"
nipkow@13145
  2089
by (induct xs) auto
wenzelm@13142
  2090
nipkow@25287
  2091
lemma distinct_remdups_id: "distinct xs ==> remdups xs = xs"
nipkow@25287
  2092
by (induct xs, auto)
nipkow@25287
  2093
nipkow@25287
  2094
lemma remdups_id_iff_distinct[simp]: "(remdups xs = xs) = distinct xs"
nipkow@25287
  2095
by(metis distinct_remdups distinct_remdups_id)
nipkow@25287
  2096
nipkow@24566
  2097
lemma finite_distinct_list: "finite A \<Longrightarrow> EX xs. set xs = A & distinct xs"
paulson@24632
  2098
by (metis distinct_remdups finite_list set_remdups)
nipkow@24566
  2099
paulson@15072
  2100
lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])"
nipkow@24349
  2101
by (induct x, auto) 
paulson@15072
  2102
paulson@15072
  2103
lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])"
nipkow@24349
  2104
by (induct x, auto)
paulson@15072
  2105
nipkow@15245
  2106
lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs"
nipkow@15245
  2107
by (induct xs) auto
nipkow@15245
  2108
nipkow@15245
  2109
lemma length_remdups_eq[iff]:
nipkow@15245
  2110
  "(length (remdups xs) = length xs) = (remdups xs = xs)"
nipkow@15245
  2111
apply(induct xs)
nipkow@15245
  2112
 apply auto
nipkow@15245
  2113
apply(subgoal_tac "length (remdups xs) <= length xs")
nipkow@15245
  2114
 apply arith
nipkow@15245
  2115
apply(rule length_remdups_leq)
nipkow@15245
  2116
done
nipkow@15245
  2117
nipkow@18490
  2118
nipkow@18490
  2119
lemma distinct_map:
nipkow@18490
  2120
  "distinct(map f xs) = (distinct xs & inj_on f (set xs))"
nipkow@18490
  2121
by (induct xs) auto
nipkow@18490
  2122
nipkow@18490
  2123
wenzelm@13142
  2124
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
nipkow@13145
  2125
by (induct xs) auto
wenzelm@13114
  2126
nipkow@17501
  2127
lemma distinct_upt[simp]: "distinct[i..<j]"
nipkow@17501
  2128
by (induct j) auto
nipkow@17501
  2129
nipkow@24526
  2130
lemma distinct_take[simp]: "distinct xs \<Longrightarrow> distinct (take i xs)"
nipkow@24526
  2131
apply(induct xs arbitrary: i)
nipkow@17501
  2132
 apply simp
nipkow@17501
  2133
apply (case_tac i)
nipkow@17501
  2134
 apply simp_all
nipkow@17501
  2135
apply(blast dest:in_set_takeD)
nipkow@17501
  2136
done
nipkow@17501
  2137
nipkow@24526
  2138
lemma distinct_drop[simp]: "distinct xs \<Longrightarrow> distinct (drop i xs)"
nipkow@24526
  2139
apply(induct xs arbitrary: i)
nipkow@17501
  2140
 apply simp
nipkow@17501
  2141
apply (case_tac i)
nipkow@17501
  2142
 apply simp_all
nipkow@17501
  2143
done
nipkow@17501
  2144
nipkow@17501
  2145
lemma distinct_list_update:
nipkow@17501
  2146
assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
nipkow@17501
  2147
shows "distinct (xs[i:=a])"
nipkow@17501
  2148
proof (cases "i < length xs")
nipkow@17501
  2149
  case True
nipkow@17501
  2150
  with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
nipkow@17501
  2151
    apply (drule_tac id_take_nth_drop) by simp
nipkow@17501
  2152
  with d True show ?thesis
nipkow@17501
  2153
    apply (simp add: upd_conv_take_nth_drop)
nipkow@17501
  2154
    apply (drule subst [OF id_take_nth_drop]) apply assumption
nipkow@17501
  2155
    apply simp apply (cases "a = xs!i") apply simp by blast
nipkow@17501
  2156
next
nipkow@17501
  2157
  case False with d show ?thesis by auto
nipkow@17501
  2158
qed
nipkow@17501
  2159
nipkow@17501
  2160
nipkow@17501
  2161
text {* It is best to avoid this indexed version of distinct, but
nipkow@17501
  2162
sometimes it is useful. *}
nipkow@17501
  2163
wenzelm@13142
  2164
lemma distinct_conv_nth:
nipkow@17501
  2165
"distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)"
paulson@15251
  2166
apply (induct xs, simp, simp)
paulson@14208
  2167
apply (rule iffI, clarsimp)
nipkow@13145
  2168
 apply (case_tac i)
paulson@14208
  2169
apply (case_tac j, simp)
nipkow@13145
  2170
apply (simp add: set_conv_nth)
nipkow@13145
  2171
 apply (case_tac j)
paulson@24648
  2172
apply (clarsimp simp add: set_conv_nth, simp) 
nipkow@13145
  2173
apply (rule conjI)
paulson@24648
  2174
(*TOO SLOW
paulson@24632
  2175
apply (metis Zero_neq_Suc gr0_conv_Suc in_set_conv_nth lessI less_trans_Suc nth_Cons' nth_Cons_Suc)
paulson@24648
  2176
*)
paulson@24648
  2177
 apply (clarsimp simp add: set_conv_nth)
paulson@24648
  2178
 apply (erule_tac x = 0 in allE, simp)
paulson@24648
  2179
 apply (erule_tac x = "Suc i" in allE, simp, clarsimp)
wenzelm@25130
  2180
(*TOO SLOW
paulson@24632
  2181
apply (metis Suc_Suc_eq lessI less_trans_Suc nth_Cons_Suc)
wenzelm@25130
  2182
*)
wenzelm@25130
  2183
apply (erule_tac x = "Suc i" in allE, simp)
wenzelm@25130
  2184
apply (erule_tac x = "Suc j" in allE, simp)
nipkow@13145
  2185
done
wenzelm@13114
  2186
nipkow@18490
  2187
lemma nth_eq_iff_index_eq:
nipkow@18490
  2188
 "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)"
nipkow@18490
  2189
by(auto simp: distinct_conv_nth)
nipkow@18490
  2190
nipkow@15110
  2191
lemma distinct_card: "distinct xs ==> card (set xs) = size xs"
nipkow@24349
  2192
by (induct xs) auto
kleing@14388
  2193
nipkow@15110
  2194
lemma card_distinct: "card (set xs) = size xs ==> distinct xs"
kleing@14388
  2195
proof (induct xs)
kleing@14388
  2196
  case Nil thus ?case by simp
kleing@14388
  2197
next
kleing@14388
  2198
  case (Cons x xs)
kleing@14388
  2199
  show ?case
kleing@14388
  2200
  proof (cases "x \<in> set xs")
kleing@14388
  2201
    case False with Cons show ?thesis by simp
kleing@14388
  2202
  next
kleing@14388
  2203
    case True with Cons.prems
kleing@14388
  2204
    have "card (set xs) = Suc (length xs)" 
kleing@14388
  2205
      by (simp add: card_insert_if split: split_if_asm)
kleing@14388
  2206
    moreover have "card (set xs) \<le> length xs" by (rule card_length)
kleing@14388
  2207
    ultimately have False by simp
kleing@14388
  2208
    thus ?thesis ..
kleing@14388
  2209
  qed
kleing@14388
  2210
qed
kleing@14388
  2211
nipkow@25287
  2212
lemma not_distinct_decomp: "~ distinct ws ==> EX xs ys zs y. ws = xs@[y]@ys@[y]@zs"
nipkow@25287
  2213
apply (induct n == "length ws" arbitrary:ws) apply simp
nipkow@25287
  2214
apply(case_tac ws) apply simp
nipkow@25287
  2215
apply (simp split:split_if_asm)
nipkow@25287
  2216
apply (metis Cons_eq_appendI eq_Nil_appendI split_list)
nipkow@25287
  2217
done
nipkow@18490
  2218
nipkow@18490
  2219
lemma length_remdups_concat:
nipkow@18490
  2220
 "length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)"
nipkow@24308
  2221
by(simp add: set_concat distinct_card[symmetric])
nipkow@17906
  2222
nipkow@17906
  2223
nipkow@15392
  2224
subsubsection {* @{text remove1} *}
nipkow@15110
  2225
nipkow@18049
  2226
lemma remove1_append:
nipkow@18049
  2227
  "remove1 x (xs @ ys) =
nipkow@18049
  2228
  (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)"
nipkow@18049
  2229
by (induct xs) auto
nipkow@18049
  2230
nipkow@23479
  2231
lemma in_set_remove1[simp]:
nipkow@23479
  2232
  "a \<noteq> b \<Longrightarrow> a : set(remove1 b xs) = (a : set xs)"
nipkow@23479
  2233
apply (induct xs)
nipkow@23479
  2234
apply auto
nipkow@23479
  2235
done
nipkow@23479
  2236
nipkow@15110
  2237
lemma set_remove1_subset: "set(remove1 x xs) <= set xs"
nipkow@15110
  2238
apply(induct xs)
nipkow@15110
  2239
 apply simp
nipkow@15110
  2240
apply simp
nipkow@15110
  2241
apply blast
nipkow@15110
  2242
done
nipkow@15110
  2243
paulson@17724
  2244
lemma set_remove1_eq [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}"
nipkow@15110
  2245
apply(induct xs)
nipkow@15110
  2246
 apply simp
nipkow@15110
  2247
apply simp
nipkow@15110
  2248
apply blast
nipkow@15110
  2249
done
nipkow@15110
  2250
nipkow@23479
  2251
lemma length_remove1:
nipkow@23479
  2252
  "length(remove1 x xs) = (if x : set xs then length xs - 1 else length xs)"
nipkow@23479
  2253
apply (induct xs)
nipkow@23479
  2254
 apply (auto dest!:length_pos_if_in_set)
nipkow@23479
  2255
done
nipkow@23479
  2256
nipkow@18049
  2257
lemma remove1_filter_not[simp]:
nipkow@18049
  2258
  "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs"
nipkow@18049
  2259
by(induct xs) auto
nipkow@18049
  2260
nipkow@15110
  2261
lemma notin_set_remove1[simp]: "x ~: set xs ==> x ~: set(remove1 y xs)"
nipkow@15110
  2262
apply(insert set_remove1_subset)
nipkow@15110
  2263
apply fast
nipkow@15110
  2264
done
nipkow@15110
  2265
nipkow@15110
  2266
lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)"
nipkow@15110
  2267
by (induct xs) simp_all
nipkow@15110
  2268
wenzelm@13114
  2269
nipkow@15392
  2270
subsubsection {* @{text replicate} *}
wenzelm@13114
  2271
wenzelm@13142
  2272
lemma length_replicate [simp]: "length (replicate n x) = n"
nipkow@13145
  2273
by (induct n) auto
nipkow@13124
  2274
wenzelm@13142
  2275
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
nipkow@13145
  2276
by (induct n) auto
wenzelm@13114
  2277
wenzelm@13114
  2278
lemma replicate_app_Cons_same:
nipkow@13145
  2279
"(replicate n x) @ (x # xs) = x # replicate n x @ xs"
nipkow@13145
  2280
by (induct n) auto
wenzelm@13114
  2281
wenzelm@13142
  2282
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
paulson@14208
  2283
apply (induct n, simp)
nipkow@13145
  2284
apply (simp add: replicate_app_Cons_same)
nipkow@13145
  2285
done
wenzelm@13114
  2286
wenzelm@13142
  2287
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
nipkow@13145
  2288
by (induct n) auto
wenzelm@13114
  2289
nipkow@16397
  2290
text{* Courtesy of Matthias Daum: *}
nipkow@16397
  2291
lemma append_replicate_commute:
nipkow@16397
  2292
  "replicate n x @ replicate k x = replicate k x @ replicate n x"
nipkow@16397
  2293
apply (simp add: replicate_add [THEN sym])
nipkow@16397
  2294
apply (simp add: add_commute)
nipkow@16397
  2295
done
nipkow@16397
  2296
wenzelm@13142
  2297
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
nipkow@13145
  2298
by (induct n) auto
wenzelm@13114
  2299
wenzelm@13142
  2300
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
nipkow@13145
  2301
by (induct n) auto
wenzelm@13114
  2302
wenzelm@13142
  2303
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
nipkow@13145
  2304
by (atomize (full), induct n) auto
wenzelm@13114
  2305
nipkow@24526
  2306
lemma nth_replicate[simp]: "i < n ==> (replicate n x)!i = x"
nipkow@24526
  2307
apply (induct n arbitrary: i, simp)
nipkow@13145
  2308
apply (simp add: nth_Cons split: nat.split)
nipkow@13145
  2309
done
wenzelm@13114
  2310
nipkow@16397
  2311
text{* Courtesy of Matthias Daum (2 lemmas): *}
nipkow@16397
  2312
lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x"
nipkow@16397
  2313
apply (case_tac "k \<le> i")
nipkow@16397
  2314
 apply  (simp add: min_def)
nipkow@16397
  2315
apply (drule not_leE)
nipkow@16397
  2316
apply (simp add: min_def)
nipkow@16397
  2317
apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x")
nipkow@16397
  2318
 apply  simp
nipkow@16397
  2319
apply (simp add: replicate_add [symmetric])
nipkow@16397
  2320
done
nipkow@16397
  2321
nipkow@24526
  2322
lemma drop_replicate[simp]: "drop i (replicate k x) = replicate (k-i) x"
nipkow@24526
  2323
apply (induct k arbitrary: i)
nipkow@16397
  2324
 apply simp
nipkow@16397
  2325
apply clarsimp
nipkow@16397
  2326
apply (case_tac i)
nipkow@16397
  2327
 apply simp
nipkow@16397
  2328
apply clarsimp
nipkow@16397
  2329
done
nipkow@16397
  2330
nipkow@16397
  2331
wenzelm@13142
  2332
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
nipkow@13145
  2333
by (induct n) auto
wenzelm@13114
  2334
wenzelm@13142
  2335
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
nipkow@13145
  2336
by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
wenzelm@13114
  2337
wenzelm@13142
  2338
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
nipkow@13145
  2339
by auto
wenzelm@13114
  2340
wenzelm@13142
  2341
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
nipkow@13145
  2342
by (simp add: set_replicate_conv_if split: split_if_asm)
wenzelm@13114
  2343
haftmann@24796
  2344
lemma replicate_append_same:
haftmann@24796
  2345
  "replicate i x @ [x] = x # replicate i x"
haftmann@24796
  2346
  by (induct i) simp_all
haftmann@24796
  2347
haftmann@24796
  2348
lemma map_replicate_trivial:
haftmann@24796
  2349
  "map (\<lambda>i. x) [0..<i] = replicate i x"
haftmann@24796
  2350
  by (induct i) (simp_all add: replicate_append_same)
haftmann@24796
  2351
wenzelm@13114
  2352
nipkow@15392
  2353
subsubsection{*@{text rotate1} and @{text rotate}*}
nipkow@15302
  2354
nipkow@15302
  2355
lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]"
nipkow@15302
  2356
by(simp add:rotate1_def)
nipkow@15302
  2357
nipkow@15302
  2358
lemma rotate0[simp]: "rotate 0 = id"
nipkow@15302
  2359
by(simp add:rotate_def)
nipkow@15302
  2360
nipkow@15302
  2361
lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)"
nipkow@15302
  2362
by(simp add:rotate_def)
nipkow@15302
  2363
nipkow@15302
  2364
lemma rotate_add:
nipkow@15302
  2365
  "rotate (m+n) = rotate m o rotate n"
nipkow@15302
  2366
by(simp add:rotate_def funpow_add)
nipkow@15302
  2367
nipkow@15302
  2368
lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs"
nipkow@15302
  2369
by(simp add:rotate_add)
nipkow@15302
  2370
nipkow@18049
  2371
lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)"
nipkow@18049
  2372
by(simp add:rotate_def funpow_swap1)
nipkow@18049
  2373
nipkow@15302
  2374
lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs"
nipkow@15302
  2375
by(cases xs) simp_all
nipkow@15302
  2376
nipkow@15302
  2377
lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  2378
apply(induct n)
nipkow@15302
  2379
 apply simp
nipkow@15302
  2380
apply (simp add:rotate_def)
nipkow@13145
  2381
done
wenzelm@13114
  2382
nipkow@15302
  2383
lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]"
nipkow@15302
  2384
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2385
nipkow@15302
  2386
lemma rotate_drop_take:
nipkow@15302
  2387
  "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs"
nipkow@15302
  2388
apply(induct n)
nipkow@15302
  2389
 apply simp
nipkow@15302
  2390
apply(simp add:rotate_def)
nipkow@15302
  2391
apply(cases "xs = []")
nipkow@15302
  2392
 apply (simp)
nipkow@15302
  2393
apply(case_tac "n mod length xs = 0")
nipkow@15302
  2394
 apply(simp add:mod_Suc)
nipkow@15302
  2395
 apply(simp add: rotate1_hd_tl drop_Suc take_Suc)
nipkow@15302
  2396
apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric]
nipkow@15302
  2397
                take_hd_drop linorder_not_le)
nipkow@13145
  2398
done
wenzelm@13114
  2399
nipkow@15302
  2400
lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs"
nipkow@15302
  2401
by(simp add:rotate_drop_take)
nipkow@15302
  2402
nipkow@15302
  2403
lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs"
nipkow@15302
  2404
by(simp add:rotate_drop_take)
nipkow@15302
  2405
nipkow@15302
  2406
lemma length_rotate1[simp]: "length(rotate1 xs) = length xs"
nipkow@15302
  2407
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2408
nipkow@24526
  2409
lemma length_rotate[simp]: "length(rotate n xs) = length xs"
nipkow@24526
  2410
by (induct n arbitrary: xs) (simp_all add:rotate_def)
nipkow@15302
  2411
nipkow@15302
  2412
lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs"
nipkow@15302
  2413
by(simp add:rotate1_def split:list.split) blast
nipkow@15302
  2414
nipkow@15302
  2415
lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs"
nipkow@15302
  2416
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  2417
nipkow@15302
  2418
lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)"
nipkow@15302
  2419
by(simp add:rotate_drop_take take_map drop_map)
nipkow@15302
  2420
nipkow@15302
  2421
lemma set_rotate1[simp]: "set(rotate1 xs) = set xs"
nipkow@15302
  2422
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2423
nipkow@15302
  2424
lemma set_rotate[simp]: "set(rotate n xs) = set xs"
nipkow@15302
  2425
by (induct n) (simp_all add:rotate_def)
nipkow@15302
  2426
nipkow@15302
  2427
lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])"
nipkow@15302
  2428
by(simp add:rotate1_def split:list.split)
nipkow@15302
  2429
nipkow@15302
  2430
lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])"
nipkow@15302
  2431
by (induct n) (simp_all add:rotate_def)
wenzelm@13114
  2432
nipkow@15439
  2433
lemma rotate_rev:
nipkow@15439
  2434
  "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)"
nipkow@15439
  2435
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  2436
apply(cases "length xs = 0")
nipkow@15439
  2437
 apply simp
nipkow@15439
  2438
apply(cases "n mod length xs = 0")
nipkow@15439
  2439
 apply simp
nipkow@15439
  2440
apply(simp add:rotate_drop_take rev_drop rev_take)
nipkow@15439
  2441
done
nipkow@15439
  2442
nipkow@18423
  2443
lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)"
nipkow@18423
  2444
apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth)
nipkow@18423
  2445
apply(subgoal_tac "length xs \<noteq> 0")
nipkow@18423
  2446
 prefer 2 apply simp
nipkow@18423
  2447
using mod_less_divisor[of "length xs" n] by arith
nipkow@18423
  2448
wenzelm@13114
  2449
nipkow@15392
  2450
subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
wenzelm@13114
  2451
wenzelm@13142
  2452
lemma sublist_empty [simp]: "sublist xs {} = []"
nipkow@13145
  2453
by (auto simp add: sublist_def)
wenzelm@13114
  2454
wenzelm@13142
  2455
lemma sublist_nil [simp]: "sublist [] A = []"
nipkow@13145
  2456
by (auto simp add: sublist_def)
wenzelm@13114
  2457
nipkow@15281
  2458
lemma length_sublist:
nipkow@15281
  2459
  "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
nipkow@15281
  2460
by(simp add: sublist_def length_filter_conv_card cong:conj_cong)
nipkow@15281
  2461
nipkow@15281
  2462
lemma sublist_shift_lemma_Suc:
nipkow@24526
  2463
  "map fst (filter (%p. P(Suc(snd p))) (zip xs is)) =
nipkow@24526
  2464
   map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))"
nipkow@24526
  2465
apply(induct xs arbitrary: "is")
nipkow@15281
  2466
 apply simp
nipkow@15281
  2467
apply (case_tac "is")
nipkow@15281
  2468
 apply simp
nipkow@15281
  2469
apply simp
nipkow@15281
  2470
done
nipkow@15281
  2471
wenzelm@13114
  2472
lemma sublist_shift_lemma:
nipkow@23279
  2473
     "map fst [p<-zip xs [i..<i + length xs] . snd p : A] =
nipkow@23279
  2474
      map fst [p<-zip xs [0..<length xs] . snd p + i : A]"
nipkow@13145
  2475
by (induct xs rule: rev_induct) (simp_all add: add_commute)
wenzelm@13114
  2476
wenzelm@13114
  2477
lemma sublist_append:
paulson@15168
  2478
     "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
nipkow@13145
  2479
apply (unfold sublist_def)
paulson@14208
  2480
apply (induct l' rule: rev_induct, simp)
nipkow@13145
  2481
apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
nipkow@13145
  2482
apply (simp add: add_commute)
nipkow@13145
  2483
done
wenzelm@13114
  2484
wenzelm@13114
  2485
lemma sublist_Cons:
nipkow@13145
  2486
"sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
nipkow@13145
  2487
apply (induct l rule: rev_induct)
nipkow@13145
  2488
 apply (simp add: sublist_def)
nipkow@13145
  2489
apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
nipkow@13145
  2490
done
wenzelm@13114
  2491
nipkow@24526
  2492
lemma set_sublist: "set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
nipkow@24526
  2493
apply(induct xs arbitrary: I)
nipkow@25162
  2494
apply(auto simp: sublist_Cons nth_Cons split:nat.split dest!: gr0_implies_Suc)
nipkow@15281
  2495
done
nipkow@15281
  2496
nipkow@15281
  2497
lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs"
nipkow@15281
  2498
by(auto simp add:set_sublist)
nipkow@15281
  2499
nipkow@15281
  2500
lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)"
nipkow@15281
  2501
by(auto simp add:set_sublist)
nipkow@15281
  2502
nipkow@15281
  2503
lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs"
nipkow@15281
  2504
by(auto simp add:set_sublist)
nipkow@15281
  2505
wenzelm@13142
  2506
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
nipkow@13145
  2507
by (simp add: sublist_Cons)
wenzelm@13114
  2508
nipkow@15281
  2509
nipkow@24526
  2510
lemma distinct_sublistI[simp]: "distinct xs \<Longrightarrow> distinct(sublist xs I)"
nipkow@24526
  2511
apply(induct xs arbitrary: I)
nipkow@15281
  2512
 apply simp
nipkow@15281
  2513
apply(auto simp add:sublist_Cons)
nipkow@15281
  2514
done
nipkow@15281
  2515
nipkow@15281
  2516
nipkow@15045
  2517
lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
paulson@14208
  2518
apply (induct l rule: rev_induct, simp)
nipkow@13145
  2519
apply (simp split: nat_diff_split add: sublist_append)
nipkow@13145
  2520
done
wenzelm@13114
  2521
nipkow@24526
  2522
lemma filter_in_sublist:
nipkow@24526
  2523
 "distinct xs \<Longrightarrow> filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s"
nipkow@24526
  2524
proof (induct xs arbitrary: s)
nipkow@17501
  2525
  case Nil thus ?case by simp
nipkow@17501
  2526
next
nipkow@17501
  2527
  case (Cons a xs)
nipkow@17501
  2528
  moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto
nipkow@17501
  2529
  ultimately show ?case by(simp add: sublist_Cons cong:filter_cong)
nipkow@17501
  2530
qed
nipkow@17501
  2531
wenzelm@13114
  2532
nipkow@19390
  2533
subsubsection {* @{const splice} *}
nipkow@19390
  2534
haftmann@19607
  2535
lemma splice_Nil2 [simp, code]:
nipkow@19390
  2536
 "splice xs [] = xs"
nipkow@19390
  2537
by (cases xs) simp_all
nipkow@19390
  2538
haftmann@19607
  2539
lemma splice_Cons_Cons [simp, code]:
nipkow@19390
  2540
 "splice (x#xs) (y#ys) = x # y # splice xs ys"
nipkow@19390
  2541
by simp
nipkow@19390
  2542
haftmann@19607
  2543
declare splice.simps(2) [simp del, code del]
nipkow@19390
  2544
nipkow@24526
  2545
lemma length_splice[simp]: "length(splice xs ys) = length xs + length ys"
nipkow@24526
  2546
apply(induct xs arbitrary: ys) apply simp
nipkow@22793
  2547
apply(case_tac ys)
nipkow@22793
  2548
 apply auto
nipkow@22793
  2549
done
nipkow@22793
  2550
nipkow@24616
  2551
nipkow@24616
  2552
subsection {*Sorting*}
nipkow@24616
  2553
nipkow@24617
  2554
text{* Currently it is not shown that @{const sort} returns a
nipkow@24617
  2555
permutation of its input because the nicest proof is via multisets,
nipkow@24617
  2556
which are not yet available. Alternatively one could define a function
nipkow@24617
  2557
that counts the number of occurrences of an element in a list and use
nipkow@24617
  2558
that instead of multisets to state the correctness property. *}
nipkow@24617
  2559
nipkow@24616
  2560
context linorder
nipkow@24616
  2561
begin
nipkow@24616
  2562
haftmann@25062
  2563
lemma sorted_Cons: "sorted (x#xs) = (sorted xs & (ALL y:set xs. x <= y))"
nipkow@24616
  2564
apply(induct xs arbitrary: x) apply simp
nipkow@24616
  2565
by simp (blast intro: order_trans)
nipkow@24616
  2566
nipkow@24616
  2567
lemma sorted_append:
haftmann@25062
  2568
  "sorted (xs@ys) = (sorted xs & sorted ys & (\<forall>x \<in> set xs. \<forall>y \<in> set ys. x\<le>y))"
nipkow@24616
  2569
by (induct xs) (auto simp add:sorted_Cons)
nipkow@24616
  2570
nipkow@24616
  2571
lemma set_insort: "set(insort x xs) = insert x (set xs)"
nipkow@24616
  2572
by (induct xs) auto
nipkow@24616
  2573
nipkow@24617
  2574
lemma set_sort[simp]: "set(sort xs) = set xs"
nipkow@24616
  2575
by (induct xs) (simp_all add:set_insort)
nipkow@24616
  2576
nipkow@24616
  2577
lemma distinct_insort: "distinct (insort x xs) = (x \<notin> set xs \<and> distinct xs)"
nipkow@24616
  2578
by(induct xs)(auto simp:set_insort)
nipkow@24616
  2579
nipkow@24617
  2580
lemma distinct_sort[simp]: "distinct (sort xs) = distinct xs"
nipkow@24616
  2581
by(induct xs)(simp_all add:distinct_insort set_sort)
nipkow@24616
  2582
nipkow@24616
  2583
lemma sorted_insort: "sorted (insort x xs) = sorted xs"
nipkow@24616
  2584
apply (induct xs)
nipkow@24650
  2585
 apply(auto simp:sorted_Cons set_insort)
nipkow@24616
  2586
done
nipkow@24616
  2587
nipkow@24616
  2588
theorem sorted_sort[simp]: "sorted (sort xs)"
nipkow@24616
  2589
by (induct xs) (auto simp:sorted_insort)
nipkow@24616
  2590
nipkow@24645
  2591
lemma sorted_distinct_set_unique:
nipkow@24645
  2592
assumes "sorted xs" "distinct xs" "sorted ys" "distinct ys" "set xs = set ys"
nipkow@24645
  2593
shows "xs = ys"
nipkow@24645
  2594
proof -
nipkow@24645
  2595
  from assms have 1: "length xs = length ys" by (metis distinct_card)
nipkow@24645
  2596
  from assms show ?thesis
nipkow@24645
  2597
  proof(induct rule:list_induct2[OF 1])
nipkow@24645
  2598
    case 1 show ?case by simp
nipkow@24645
  2599
  next
nipkow@24645
  2600
    case 2 thus ?case by (simp add:sorted_Cons)
nipkow@24645
  2601
       (metis Diff_insert_absorb antisym insertE insert_iff)
nipkow@24645
  2602
  qed
nipkow@24645
  2603
qed
nipkow@24645
  2604
nipkow@24645
  2605
lemma finite_sorted_distinct_unique:
nipkow@24645
  2606
shows "finite A \<Longrightarrow> EX! xs. set xs = A & sorted xs & distinct xs"
nipkow@24645
  2607
apply(drule finite_distinct_list)
nipkow@24645
  2608
apply clarify
nipkow@24645
  2609
apply(rule_tac a="sort xs" in ex1I)
nipkow@24645
  2610
apply (auto simp: sorted_distinct_set_unique)
nipkow@24645
  2611
done
nipkow@24645
  2612
nipkow@24616
  2613
end
nipkow@24616
  2614
nipkow@25277
  2615
lemma sorted_upt[simp]: "sorted[i..<j]"
nipkow@25277
  2616
by (induct j) (simp_all add:sorted_append)
nipkow@25277
  2617
nipkow@24616
  2618
nipkow@25069
  2619
subsubsection {* @{text sorted_list_of_set} *}
nipkow@25069
  2620
nipkow@25069
  2621
text{* This function maps (finite) linearly ordered sets to sorted
nipkow@25069
  2622
lists. Warning: in most cases it is not a good idea to convert from
nipkow@25069
  2623
sets to lists but one should convert in the other direction (via
nipkow@25069
  2624
@{const set}). *}
nipkow@25069
  2625
nipkow@25069
  2626
nipkow@25069
  2627
context linorder
nipkow@25069
  2628
begin
nipkow@25069
  2629
nipkow@25069
  2630
definition
nipkow@25069
  2631
 sorted_list_of_set :: "'a set \<Rightarrow> 'a list" where
nipkow@25069
  2632
"sorted_list_of_set A == THE xs. set xs = A & sorted xs & distinct xs"
nipkow@25069
  2633
nipkow@25069
  2634
lemma sorted_list_of_set[simp]: "finite A \<Longrightarrow>
nipkow@25069
  2635
  set(sorted_list_of_set A) = A &
nipkow@25069
  2636
  sorted(sorted_list_of_set A) & distinct(sorted_list_of_set A)"
nipkow@25069
  2637
apply(simp add:sorted_list_of_set_def)
nipkow@25069
  2638
apply(rule the1I2)
nipkow@25069
  2639
 apply(simp_all add: finite_sorted_distinct_unique)
nipkow@25069
  2640
done
nipkow@25069
  2641
nipkow@25069
  2642
lemma sorted_list_of_empty[simp]: "sorted_list_of_set {} = []"
nipkow@25069
  2643
unfolding sorted_list_of_set_def
nipkow@25069
  2644
apply(subst the_equality[of _ "[]"])
nipkow@25069
  2645
apply simp_all
nipkow@25069
  2646
done
nipkow@25069
  2647
nipkow@25069
  2648
end
nipkow@25069
  2649
nipkow@25069
  2650
nipkow@24645
  2651
subsubsection {* @{text upto}: the generic interval-list *}
nipkow@24645
  2652
nipkow@24697
  2653
class finite_intvl_succ = linorder +
nipkow@24697
  2654
fixes successor :: "'a \<Rightarrow> 'a"
nipkow@25069
  2655
assumes finite_intvl: "finite{a..b}"
haftmann@25062
  2656
and successor_incr: "a < successor a"
haftmann@25062
  2657
and ord_discrete: "\<not>(\<exists>x. a < x & x < successor a)"
nipkow@24697
  2658
nipkow@24697
  2659
context finite_intvl_succ
nipkow@24697
  2660
begin
nipkow@24697
  2661
nipkow@24697
  2662
definition
haftmann@25062
  2663
 upto :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list" ("(1[_../_])") where
nipkow@25069
  2664
"upto i j == sorted_list_of_set {i..j}"
nipkow@25069
  2665
nipkow@25069
  2666
lemma upto[simp]: "set[a..b] = {a..b} & sorted[a..b] & distinct[a..b]"
nipkow@25069
  2667
by(simp add:upto_def finite_intvl)
nipkow@24697
  2668
haftmann@25062
  2669
lemma insert_intvl: "i \<le> j \<Longrightarrow> insert i {successor i..j} = {i..j}"
nipkow@24697
  2670
apply(insert successor_incr[of i])
nipkow@24697
  2671
apply(auto simp: atLeastAtMost_def atLeast_def atMost_def)
nipkow@24697
  2672
apply (metis ord_discrete less_le not_le)
nipkow@24645
  2673
done
nipkow@24645
  2674
nipkow@25069
  2675
lemma sorted_list_of_set_rec: "i \<le> j \<Longrightarrow>
nipkow@25069
  2676
  sorted_list_of_set {i..j} = i # sorted_list_of_set {successor i..j}"
nipkow@25069
  2677
apply(simp add:sorted_list_of_set_def upto_def)
nipkow@25069
  2678
apply (rule the1_equality[OF finite_sorted_distinct_unique])
nipkow@25069
  2679
 apply (simp add:finite_intvl)
nipkow@25069
  2680
apply(rule the1I2[OF finite_sorted_distinct_unique])
nipkow@25069
  2681
 apply (simp add:finite_intvl)
nipkow@25069
  2682
apply (simp add: sorted_Cons insert_intvl Ball_def)
nipkow@25069
  2683
apply (metis successor_incr leD less_imp_le order_trans)
nipkow@25069
  2684
done
nipkow@25069
  2685
haftmann@25062
  2686
lemma upto_rec[code]: "[i..j] = (if i \<le> j then i # [successor i..j] else [])"
nipkow@25069
  2687
by(simp add: upto_def sorted_list_of_set_rec)
nipkow@24697
  2688
nipkow@24697
  2689
end
nipkow@24697
  2690
nipkow@24697
  2691
text{* The integers are an instance of the above class: *}
nipkow@24697
  2692
nipkow@24697
  2693
instance int:: finite_intvl_succ
nipkow@24697
  2694
  successor_int_def: "successor == (%i. i+1)"
haftmann@25082
  2695
by intro_classes (simp_all add: successor_int_def)
nipkow@24645
  2696
nipkow@24697
  2697
text{* Now @{term"[i..j::int]"} is defined for integers. *}
nipkow@24697
  2698
nipkow@24698
  2699
hide (open) const successor
nipkow@24698
  2700
nipkow@24645
  2701
nipkow@15392
  2702
subsubsection {* @{text lists}: the list-forming operator over sets *}
nipkow@15302
  2703
berghofe@23740
  2704
inductive_set
berghofe@22262
  2705
  lists :: "'a set => 'a list set"
berghofe@23740
  2706
  for A :: "'a set"
berghofe@23740
  2707
where
berghofe@23740
  2708
    Nil [intro!]: "[]: lists A"
paulson@24286
  2709
  | Cons [intro!,noatp]: "[| a: A;l: lists A|] ==> a#l : lists A"
paulson@24286
  2710
paulson@24286
  2711
inductive_cases listsE [elim!,noatp]: "x#l : lists A"
paulson@24286
  2712
inductive_cases listspE [elim!,noatp]: "listsp A (x # l)"
berghofe@23740
  2713
berghofe@23740
  2714
lemma listsp_mono [mono]: "A \<le> B ==> listsp A \<le> listsp B"
nipkow@24349
  2715
by (clarify, erule listsp.induct, blast+)
berghofe@22262
  2716
berghofe@23740
  2717
lemmas lists_mono = listsp_mono [to_set]
berghofe@22262
  2718
haftmann@22422
  2719
lemma listsp_infI:
haftmann@22422
  2720
  assumes l: "listsp A l" shows "listsp B l ==> listsp (inf A B) l" using l
nipkow@24349
  2721
by induct blast+
nipkow@15302
  2722
haftmann@22422
  2723
lemmas lists_IntI = listsp_infI [to_set]
haftmann@22422
  2724
haftmann@22422
  2725
lemma listsp_inf_eq [simp]: "listsp (inf A B) = inf (listsp A) (listsp B)"
haftmann@22422
  2726
proof (rule mono_inf [where f=listsp, THEN order_antisym])
berghofe@22262
  2727
  show "mono listsp" by (simp add: mono_def listsp_mono)
haftmann@22422
  2728
  show "inf (listsp A) (listsp B) \<le> listsp (inf A B)" by (blast intro: listsp_infI)
kleing@14388
  2729
qed
kleing@14388
  2730
haftmann@22422
  2731
lemmas listsp_conj_eq [simp] = listsp_inf_eq [simplified inf_fun_eq inf_bool_eq]
haftmann@22422
  2732
haftmann@22422
  2733
lemmas lists_Int_eq [simp] = listsp_inf_eq [to_set]
berghofe@22262
  2734
berghofe@22262
  2735
lemma append_in_listsp_conv [iff]:
berghofe@22262
  2736
     "(listsp A (xs @ ys)) = (listsp A xs \<and> listsp A ys)"
nipkow@15302
  2737
by (induct xs) auto
nipkow@15302
  2738
berghofe@22262
  2739
lemmas append_in_lists_conv [iff] = append_in_listsp_conv [to_set]
berghofe@22262
  2740
berghofe@22262
  2741
lemma in_listsp_conv_set: "(listsp A xs) = (\<forall>x \<in> set xs. A x)"
berghofe@22262
  2742
-- {* eliminate @{text listsp} in favour of @{text set} *}
nipkow@15302
  2743
by (induct xs) auto
nipkow@15302
  2744
berghofe@22262
  2745
lemmas in_lists_conv_set = in_listsp_conv_set [to_set]
berghofe@22262
  2746
paulson@24286
  2747
lemma in_listspD [dest!,noatp]: "listsp A xs ==> \<forall>x\<in>set xs. A x"
berghofe@22262
  2748
by (rule in_listsp_conv_set [THEN iffD1])
berghofe@22262
  2749
paulson@24286
  2750
lemmas in_listsD [dest!,noatp] = in_listspD [to_set]
paulson@24286
  2751
paulson@24286
  2752
lemma in_listspI [intro!,noatp]: "\<forall>x\<in>set xs. A x ==> listsp A xs"
berghofe@22262
  2753
by (rule in_listsp_conv_set [THEN iffD2])
berghofe@22262
  2754
paulson@24286
  2755
lemmas in_listsI [intro!,noatp] = in_listspI [to_set]
nipkow@15302
  2756
nipkow@15302
  2757
lemma lists_UNIV [simp]: "lists UNIV = UNIV"
nipkow@15302
  2758
by auto
nipkow@15302
  2759