src/HOL/List.thy
author wenzelm
Mon May 13 11:05:27 2002 +0200 (2002-05-13)
changeset 13142 1ebd8ed5a1a0
parent 13124 6e1decd8a7a9
child 13145 59bc43b51aa2
permissions -rw-r--r--
tuned document;
clasohm@923
     1
(*  Title:      HOL/List.thy
clasohm@923
     2
    ID:         $Id$
clasohm@923
     3
    Author:     Tobias Nipkow
clasohm@923
     4
    Copyright   1994 TU Muenchen
clasohm@923
     5
*)
clasohm@923
     6
wenzelm@13114
     7
header {* The datatype of finite lists *}
wenzelm@13122
     8
wenzelm@13122
     9
theory List = PreList:
clasohm@923
    10
wenzelm@13142
    11
datatype 'a list =
wenzelm@13142
    12
    Nil    ("[]")
wenzelm@13142
    13
  | Cons 'a  "'a list"    (infixr "#" 65)
clasohm@923
    14
clasohm@923
    15
consts
wenzelm@13142
    16
  "@"         :: "'a list => 'a list => 'a list"            (infixr 65)
wenzelm@13142
    17
  filter      :: "('a => bool) => 'a list => 'a list"
wenzelm@13142
    18
  concat      :: "'a list list => 'a list"
wenzelm@13142
    19
  foldl       :: "('b => 'a => 'b) => 'b => 'a list => 'b"
wenzelm@13142
    20
  foldr       :: "('a => 'b => 'b) => 'a list => 'b => 'b"
wenzelm@13142
    21
  hd          :: "'a list => 'a"
wenzelm@13142
    22
  tl          :: "'a list => 'a list"
wenzelm@13142
    23
  last        :: "'a list => 'a"
wenzelm@13142
    24
  butlast     :: "'a list => 'a list"
wenzelm@13142
    25
  set         :: "'a list => 'a set"
wenzelm@13142
    26
  list_all    :: "('a => bool) => ('a list => bool)"
wenzelm@13142
    27
  list_all2   :: "('a => 'b => bool) => 'a list => 'b list => bool"
wenzelm@13142
    28
  map         :: "('a=>'b) => ('a list => 'b list)"
wenzelm@13142
    29
  mem         :: "'a => 'a list => bool"                    (infixl 55)
wenzelm@13142
    30
  nth         :: "'a list => nat => 'a"                   (infixl "!" 100)
wenzelm@13142
    31
  list_update :: "'a list => nat => 'a => 'a list"
wenzelm@13142
    32
  take        :: "nat => 'a list => 'a list"
wenzelm@13142
    33
  drop        :: "nat => 'a list => 'a list"
wenzelm@13142
    34
  takeWhile   :: "('a => bool) => 'a list => 'a list"
wenzelm@13142
    35
  dropWhile   :: "('a => bool) => 'a list => 'a list"
wenzelm@13142
    36
  rev         :: "'a list => 'a list"
wenzelm@13142
    37
  zip         :: "'a list => 'b list => ('a * 'b) list"
wenzelm@13142
    38
  upt         :: "nat => nat => nat list"                   ("(1[_../_'(])")
wenzelm@13142
    39
  remdups     :: "'a list => 'a list"
wenzelm@13142
    40
  null        :: "'a list => bool"
wenzelm@13142
    41
  "distinct"  :: "'a list => bool"
wenzelm@13142
    42
  replicate   :: "nat => 'a => 'a list"
clasohm@923
    43
nipkow@5077
    44
nonterminals
nipkow@5077
    45
  lupdbinds  lupdbind
nipkow@5077
    46
clasohm@923
    47
syntax
wenzelm@13142
    48
  -- {* list Enumeration *}
wenzelm@13142
    49
  "@list"     :: "args => 'a list"                          ("[(_)]")
clasohm@923
    50
wenzelm@13142
    51
  -- {* Special syntax for filter *}
wenzelm@13142
    52
  "@filter"   :: "[pttrn, 'a list, bool] => 'a list"        ("(1[_:_./ _])")
clasohm@923
    53
wenzelm@13142
    54
  -- {* list update *}
wenzelm@13142
    55
  "_lupdbind"      :: "['a, 'a] => lupdbind"            ("(2_ :=/ _)")
wenzelm@13142
    56
  ""               :: "lupdbind => lupdbinds"           ("_")
wenzelm@13142
    57
  "_lupdbinds"     :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _")
wenzelm@13142
    58
  "_LUpdate"       :: "['a, lupdbinds] => 'a"           ("_/[(_)]" [900,0] 900)
nipkow@5077
    59
wenzelm@13142
    60
  upto        :: "nat => nat => nat list"                   ("(1[_../_])")
nipkow@5427
    61
clasohm@923
    62
translations
clasohm@923
    63
  "[x, xs]"     == "x#[xs]"
clasohm@923
    64
  "[x]"         == "x#[]"
wenzelm@3842
    65
  "[x:xs . P]"  == "filter (%x. P) xs"
clasohm@923
    66
nipkow@5077
    67
  "_LUpdate xs (_lupdbinds b bs)"  == "_LUpdate (_LUpdate xs b) bs"
nipkow@5077
    68
  "xs[i:=x]"                       == "list_update xs i x"
nipkow@5077
    69
nipkow@5427
    70
  "[i..j]" == "[i..(Suc j)(]"
nipkow@5427
    71
nipkow@5427
    72
wenzelm@12114
    73
syntax (xsymbols)
wenzelm@13142
    74
  "@filter"   :: "[pttrn, 'a list, bool] => 'a list"        ("(1[_\<in>_ ./ _])")
paulson@3342
    75
paulson@3342
    76
wenzelm@13142
    77
text {*
wenzelm@13142
    78
  Function @{text size} is overloaded for all datatypes.  Users may
wenzelm@13142
    79
  refer to the list version as @{text length}. *}
wenzelm@13142
    80
wenzelm@13142
    81
syntax length :: "'a list => nat"
wenzelm@13142
    82
translations "length" => "size :: _ list => nat"
wenzelm@13114
    83
wenzelm@13142
    84
typed_print_translation {*
wenzelm@13142
    85
  let
wenzelm@13142
    86
    fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
wenzelm@13142
    87
          Syntax.const "length" $ t
wenzelm@13142
    88
      | size_tr' _ _ _ = raise Match;
wenzelm@13142
    89
  in [("size", size_tr')] end
wenzelm@13114
    90
*}
paulson@3437
    91
berghofe@5183
    92
primrec
berghofe@1898
    93
  "hd(x#xs) = x"
berghofe@5183
    94
primrec
paulson@8972
    95
  "tl([])   = []"
berghofe@1898
    96
  "tl(x#xs) = xs"
berghofe@5183
    97
primrec
paulson@8972
    98
  "null([])   = True"
paulson@8972
    99
  "null(x#xs) = False"
paulson@8972
   100
primrec
nipkow@3896
   101
  "last(x#xs) = (if xs=[] then x else last xs)"
berghofe@5183
   102
primrec
paulson@8972
   103
  "butlast []    = []"
nipkow@3896
   104
  "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
berghofe@5183
   105
primrec
paulson@8972
   106
  "x mem []     = False"
oheimb@5518
   107
  "x mem (y#ys) = (if y=x then True else x mem ys)"
oheimb@5518
   108
primrec
nipkow@3465
   109
  "set [] = {}"
nipkow@3465
   110
  "set (x#xs) = insert x (set xs)"
berghofe@5183
   111
primrec
wenzelm@13114
   112
  list_all_Nil:  "list_all P [] = True"
wenzelm@13142
   113
  list_all_Cons: "list_all P (x#xs) = (P(x) \<and> list_all P xs)"
oheimb@5518
   114
primrec
paulson@8972
   115
  "map f []     = []"
berghofe@1898
   116
  "map f (x#xs) = f(x)#map f xs"
berghofe@5183
   117
primrec
wenzelm@13114
   118
  append_Nil:  "[]    @ys = ys"
wenzelm@13114
   119
  append_Cons: "(x#xs)@ys = x#(xs@ys)"
berghofe@5183
   120
primrec
paulson@8972
   121
  "rev([])   = []"
berghofe@1898
   122
  "rev(x#xs) = rev(xs) @ [x]"
berghofe@5183
   123
primrec
paulson@8972
   124
  "filter P []     = []"
berghofe@1898
   125
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
berghofe@5183
   126
primrec
wenzelm@13114
   127
  foldl_Nil:  "foldl f a [] = a"
wenzelm@13114
   128
  foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
berghofe@5183
   129
primrec
paulson@8972
   130
  "foldr f [] a     = a"
paulson@8000
   131
  "foldr f (x#xs) a = f x (foldr f xs a)"
paulson@8000
   132
primrec
paulson@8972
   133
  "concat([])   = []"
nipkow@2608
   134
  "concat(x#xs) = x @ concat(xs)"
berghofe@5183
   135
primrec
wenzelm@13114
   136
  drop_Nil:  "drop n [] = []"
wenzelm@13142
   137
  drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
wenzelm@13142
   138
    -- {* Warning: simpset does not contain this definition *}
wenzelm@13142
   139
    -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
berghofe@5183
   140
primrec
wenzelm@13114
   141
  take_Nil:  "take n [] = []"
wenzelm@13142
   142
  take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
wenzelm@13142
   143
    -- {* Warning: simpset does not contain this definition *}
wenzelm@13142
   144
    -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
berghofe@5183
   145
primrec
wenzelm@13142
   146
  nth_Cons:  "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
wenzelm@13142
   147
    -- {* Warning: simpset does not contain this definition *}
wenzelm@13142
   148
    -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
wenzelm@13142
   149
primrec
wenzelm@13142
   150
  "[][i:=v] = []"
wenzelm@13142
   151
  "(x#xs)[i:=v] =
wenzelm@13142
   152
    (case i of 0 => v # xs
wenzelm@13142
   153
    | Suc j => x # xs[j:=v])"
berghofe@5183
   154
primrec
paulson@8972
   155
  "takeWhile P []     = []"
nipkow@2608
   156
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
berghofe@5183
   157
primrec
paulson@8972
   158
  "dropWhile P []     = []"
nipkow@3584
   159
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
berghofe@5183
   160
primrec
oheimb@4132
   161
  "zip xs []     = []"
wenzelm@13142
   162
  zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
wenzelm@13142
   163
    -- {* Warning: simpset does not contain this definition *}
wenzelm@13142
   164
    -- {* but separate theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
nipkow@5427
   165
primrec
wenzelm@13114
   166
  upt_0:   "[i..0(] = []"
wenzelm@13114
   167
  upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
berghofe@5183
   168
primrec
nipkow@12887
   169
  "distinct []     = True"
wenzelm@13142
   170
  "distinct (x#xs) = (x ~: set xs \<and> distinct xs)"
berghofe@5183
   171
primrec
nipkow@4605
   172
  "remdups [] = []"
nipkow@4605
   173
  "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
berghofe@5183
   174
primrec
wenzelm@13114
   175
  replicate_0:   "replicate  0      x = []"
wenzelm@13114
   176
  replicate_Suc: "replicate (Suc n) x = x # replicate n x"
nipkow@8115
   177
defs
wenzelm@13114
   178
 list_all2_def:
wenzelm@13142
   179
 "list_all2 P xs ys == length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)"
nipkow@8115
   180
paulson@3196
   181
wenzelm@13142
   182
subsection {* Lexicographic orderings on lists *}
nipkow@5281
   183
nipkow@5281
   184
consts
wenzelm@13142
   185
  lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
nipkow@5281
   186
primrec
wenzelm@13142
   187
  "lexn r 0 = {}"
wenzelm@13142
   188
  "lexn r (Suc n) =
wenzelm@13142
   189
    (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
wenzelm@13142
   190
      {(xs,ys). length xs = Suc n \<and> length ys = Suc n}"
nipkow@5281
   191
nipkow@5281
   192
constdefs
wenzelm@13142
   193
  lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
wenzelm@13142
   194
  "lex r == \<Union>n. lexn r n"
nipkow@5281
   195
wenzelm@13142
   196
  lexico :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
wenzelm@13142
   197
  "lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
paulson@9336
   198
wenzelm@13142
   199
  sublist :: "'a list => nat set => 'a list"
wenzelm@13142
   200
  "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))"
nipkow@5281
   201
wenzelm@13114
   202
wenzelm@13142
   203
lemma not_Cons_self [simp]: "xs \<noteq> x # xs"
wenzelm@13142
   204
  by (induct xs) auto
wenzelm@13114
   205
wenzelm@13142
   206
lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
wenzelm@13114
   207
wenzelm@13142
   208
lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
wenzelm@13142
   209
  by (induct xs) auto
wenzelm@13114
   210
wenzelm@13142
   211
lemma length_induct:
wenzelm@13142
   212
    "(!!xs. \<forall>ys. length ys < length xs --> P ys ==> P xs) ==> P xs"
wenzelm@13142
   213
  by (rule measure_induct [of length]) rules
wenzelm@13114
   214
wenzelm@13114
   215
wenzelm@13142
   216
subsection {* @{text lists}: the list-forming operator over sets *}
wenzelm@13114
   217
wenzelm@13142
   218
consts lists :: "'a set => 'a list set"
wenzelm@13142
   219
inductive "lists A"
wenzelm@13142
   220
  intros
wenzelm@13142
   221
    Nil [intro!]: "[]: lists A"
wenzelm@13142
   222
    Cons [intro!]: "[| a: A;  l: lists A  |] ==> a#l : lists A"
wenzelm@13114
   223
wenzelm@13142
   224
inductive_cases listsE [elim!]: "x#l : lists A"
wenzelm@13114
   225
wenzelm@13142
   226
lemma lists_mono: "A \<subseteq> B ==> lists A \<subseteq> lists B"
wenzelm@13142
   227
  by (unfold lists.defs) (blast intro!: lfp_mono)
wenzelm@13114
   228
wenzelm@13142
   229
lemma lists_IntI [rule_format]:
wenzelm@13142
   230
    "l: lists A ==> l: lists B --> l: lists (A Int B)"
wenzelm@13142
   231
  apply (erule lists.induct)
wenzelm@13142
   232
  apply blast+
wenzelm@13142
   233
  done
wenzelm@13142
   234
wenzelm@13142
   235
lemma lists_Int_eq [simp]: "lists (A \<inter> B) = lists A \<inter> lists B"
wenzelm@13142
   236
  apply (rule mono_Int [THEN equalityI])
wenzelm@13142
   237
  apply (simp add: mono_def lists_mono)
wenzelm@13142
   238
  apply (blast intro!: lists_IntI)
wenzelm@13142
   239
  done
wenzelm@13114
   240
wenzelm@13142
   241
lemma append_in_lists_conv [iff]:
wenzelm@13142
   242
    "(xs @ ys : lists A) = (xs : lists A \<and> ys : lists A)"
wenzelm@13142
   243
  by (induct xs) auto
wenzelm@13142
   244
wenzelm@13142
   245
wenzelm@13142
   246
subsection {* @{text length} *}
wenzelm@13114
   247
wenzelm@13142
   248
text {*
wenzelm@13142
   249
  Needs to come before @{text "@"} because of theorem @{text
wenzelm@13142
   250
  append_eq_append_conv}.
wenzelm@13142
   251
*}
wenzelm@13114
   252
wenzelm@13142
   253
lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
wenzelm@13142
   254
  by (induct xs) auto
wenzelm@13114
   255
wenzelm@13142
   256
lemma length_map [simp]: "length (map f xs) = length xs"
wenzelm@13142
   257
  by (induct xs) auto
wenzelm@13114
   258
wenzelm@13142
   259
lemma length_rev [simp]: "length (rev xs) = length xs"
wenzelm@13142
   260
  by (induct xs) auto
wenzelm@13114
   261
wenzelm@13142
   262
lemma length_tl [simp]: "length (tl xs) = length xs - 1"
wenzelm@13142
   263
  by (cases xs) auto
wenzelm@13114
   264
wenzelm@13142
   265
lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
wenzelm@13142
   266
  by (induct xs) auto
wenzelm@13114
   267
wenzelm@13142
   268
lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
wenzelm@13142
   269
  by (induct xs) auto
wenzelm@13114
   270
wenzelm@13114
   271
lemma length_Suc_conv:
wenzelm@13142
   272
    "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
wenzelm@13142
   273
  by (induct xs) auto
wenzelm@13142
   274
wenzelm@13114
   275
wenzelm@13142
   276
subsection {* @{text "@"} -- append *}
wenzelm@13114
   277
wenzelm@13142
   278
lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
wenzelm@13142
   279
  by (induct xs) auto
wenzelm@13114
   280
wenzelm@13142
   281
lemma append_Nil2 [simp]: "xs @ [] = xs"
wenzelm@13142
   282
  by (induct xs) auto
nipkow@3507
   283
wenzelm@13142
   284
lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
wenzelm@13142
   285
  by (induct xs) auto
wenzelm@13114
   286
wenzelm@13142
   287
lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
wenzelm@13142
   288
  by (induct xs) auto
wenzelm@13114
   289
wenzelm@13142
   290
lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
wenzelm@13142
   291
  by (induct xs) auto
wenzelm@13114
   292
wenzelm@13142
   293
lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
wenzelm@13142
   294
  by (induct xs) auto
wenzelm@13114
   295
wenzelm@13142
   296
lemma append_eq_append_conv [rule_format, simp]:
wenzelm@13142
   297
 "\<forall>ys. length xs = length ys \<or> length us = length vs
wenzelm@13142
   298
       --> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
wenzelm@13142
   299
  apply (induct_tac xs)
wenzelm@13142
   300
   apply(rule allI)
wenzelm@13142
   301
   apply (case_tac ys)
wenzelm@13142
   302
    apply simp
wenzelm@13142
   303
   apply force
wenzelm@13142
   304
  apply (rule allI)
wenzelm@13142
   305
  apply (case_tac ys)
wenzelm@13142
   306
   apply force
wenzelm@13114
   307
  apply simp
wenzelm@13142
   308
  done
wenzelm@13142
   309
wenzelm@13142
   310
lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
wenzelm@13142
   311
  by simp
wenzelm@13142
   312
wenzelm@13142
   313
lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
wenzelm@13142
   314
  by simp
wenzelm@13114
   315
wenzelm@13142
   316
lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
wenzelm@13142
   317
  by simp
wenzelm@13114
   318
wenzelm@13142
   319
lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
wenzelm@13142
   320
  using append_same_eq [of _ _ "[]"] by auto
nipkow@3507
   321
wenzelm@13142
   322
lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
wenzelm@13142
   323
  using append_same_eq [of "[]"] by auto
wenzelm@13114
   324
wenzelm@13142
   325
lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
wenzelm@13142
   326
  by (induct xs) auto
wenzelm@13114
   327
wenzelm@13142
   328
lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
wenzelm@13142
   329
  by (induct xs) auto
wenzelm@13114
   330
wenzelm@13142
   331
lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
wenzelm@13142
   332
  by (simp add: hd_append split: list.split)
wenzelm@13114
   333
wenzelm@13142
   334
lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
wenzelm@13142
   335
  by (simp split: list.split)
wenzelm@13114
   336
wenzelm@13142
   337
lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
wenzelm@13142
   338
  by (simp add: tl_append split: list.split)
wenzelm@13114
   339
wenzelm@13114
   340
wenzelm@13142
   341
text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114
   342
wenzelm@13114
   343
lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
wenzelm@13142
   344
  by simp
wenzelm@13114
   345
wenzelm@13142
   346
lemma Cons_eq_appendI:
wenzelm@13142
   347
    "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
wenzelm@13142
   348
  by (drule sym) simp
wenzelm@13114
   349
wenzelm@13142
   350
lemma append_eq_appendI:
wenzelm@13142
   351
    "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
wenzelm@13142
   352
  by (drule sym) simp
wenzelm@13114
   353
wenzelm@13114
   354
wenzelm@13142
   355
text {*
wenzelm@13142
   356
  Simplification procedure for all list equalities.
wenzelm@13142
   357
  Currently only tries to rearrange @{text "@"} to see if
wenzelm@13142
   358
  - both lists end in a singleton list,
wenzelm@13142
   359
  - or both lists end in the same list.
wenzelm@13142
   360
*}
wenzelm@13142
   361
wenzelm@13142
   362
ML_setup {*
nipkow@3507
   363
local
nipkow@3507
   364
wenzelm@13122
   365
val append_assoc = thm "append_assoc";
wenzelm@13122
   366
val append_Nil = thm "append_Nil";
wenzelm@13122
   367
val append_Cons = thm "append_Cons";
wenzelm@13122
   368
val append1_eq_conv = thm "append1_eq_conv";
wenzelm@13122
   369
val append_same_eq = thm "append_same_eq";
wenzelm@13122
   370
wenzelm@13114
   371
val list_eq_pattern =
wenzelm@13114
   372
  Thm.read_cterm (Theory.sign_of (the_context ())) ("(xs::'a list) = ys",HOLogic.boolT)
wenzelm@13114
   373
wenzelm@13114
   374
fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
wenzelm@13114
   375
      (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
wenzelm@13114
   376
  | last (Const("List.op @",_) $ _ $ ys) = last ys
wenzelm@13114
   377
  | last t = t
wenzelm@13114
   378
wenzelm@13114
   379
fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
wenzelm@13114
   380
  | list1 _ = false
wenzelm@13114
   381
wenzelm@13114
   382
fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
wenzelm@13114
   383
      (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
wenzelm@13114
   384
  | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
wenzelm@13114
   385
  | butlast xs = Const("List.list.Nil",fastype_of xs)
wenzelm@13114
   386
wenzelm@13114
   387
val rearr_tac =
wenzelm@13114
   388
  simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons])
wenzelm@13114
   389
wenzelm@13114
   390
fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13114
   391
  let
wenzelm@13114
   392
    val lastl = last lhs and lastr = last rhs
wenzelm@13114
   393
    fun rearr conv =
wenzelm@13114
   394
      let val lhs1 = butlast lhs and rhs1 = butlast rhs
wenzelm@13114
   395
          val Type(_,listT::_) = eqT
wenzelm@13114
   396
          val appT = [listT,listT] ---> listT
wenzelm@13114
   397
          val app = Const("List.op @",appT)
wenzelm@13114
   398
          val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@13114
   399
          val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
wenzelm@13114
   400
          val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
wenzelm@13114
   401
            handle ERROR =>
wenzelm@13114
   402
            error("The error(s) above occurred while trying to prove " ^
wenzelm@13114
   403
                  string_of_cterm ct)
wenzelm@13114
   404
      in Some((conv RS (thm RS trans)) RS eq_reflection) end
wenzelm@13114
   405
wenzelm@13114
   406
  in if list1 lastl andalso list1 lastr
wenzelm@13114
   407
     then rearr append1_eq_conv
wenzelm@13114
   408
     else
wenzelm@13114
   409
     if lastl aconv lastr
wenzelm@13114
   410
     then rearr append_same_eq
wenzelm@13114
   411
     else None
wenzelm@13114
   412
  end
wenzelm@13114
   413
in
wenzelm@13114
   414
val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq
wenzelm@13114
   415
end;
wenzelm@13114
   416
wenzelm@13114
   417
Addsimprocs [list_eq_simproc];
wenzelm@13114
   418
*}
wenzelm@13114
   419
wenzelm@13114
   420
wenzelm@13142
   421
subsection {* @{text map} *}
wenzelm@13114
   422
wenzelm@13142
   423
lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
wenzelm@13142
   424
  by (induct xs) simp_all
wenzelm@13114
   425
wenzelm@13142
   426
lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
wenzelm@13142
   427
  by (rule ext, induct_tac xs) auto
wenzelm@13114
   428
wenzelm@13142
   429
lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
wenzelm@13142
   430
  by (induct xs) auto
wenzelm@13114
   431
wenzelm@13142
   432
lemma map_compose: "map (f o g) xs = map f (map g xs)"
wenzelm@13142
   433
  by (induct xs) (auto simp add: o_def)
wenzelm@13114
   434
wenzelm@13142
   435
lemma rev_map: "rev (map f xs) = map f (rev xs)"
wenzelm@13142
   436
  by (induct xs) auto
wenzelm@13114
   437
wenzelm@13114
   438
lemma map_cong:
wenzelm@13142
   439
  "xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
wenzelm@13142
   440
  -- {* a congruence rule for @{text map} *}
wenzelm@13142
   441
  by (clarify, induct ys) auto
wenzelm@13114
   442
wenzelm@13142
   443
lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
wenzelm@13142
   444
  by (cases xs) auto
wenzelm@13114
   445
wenzelm@13142
   446
lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
wenzelm@13142
   447
  by (cases xs) auto
wenzelm@13114
   448
wenzelm@13114
   449
lemma map_eq_Cons:
wenzelm@13142
   450
  "(map f xs = y # ys) = (\<exists>x xs'. xs = x # xs' \<and> f x = y \<and> map f xs' = ys)"
wenzelm@13142
   451
  by (cases xs) auto
wenzelm@13114
   452
wenzelm@13114
   453
lemma map_injective:
wenzelm@13142
   454
    "!!xs. map f xs = map f ys ==> (\<forall>x y. f x = f y --> x = y) ==> xs = ys"
wenzelm@13142
   455
  by (induct ys) (auto simp add: map_eq_Cons)
wenzelm@13114
   456
wenzelm@13114
   457
lemma inj_mapI: "inj f ==> inj (map f)"
wenzelm@13142
   458
  by (rules dest: map_injective injD intro: injI)
wenzelm@13114
   459
wenzelm@13114
   460
lemma inj_mapD: "inj (map f) ==> inj f"
wenzelm@13142
   461
  apply (unfold inj_on_def)
wenzelm@13142
   462
  apply clarify
wenzelm@13142
   463
  apply (erule_tac x = "[x]" in ballE)
wenzelm@13142
   464
   apply (erule_tac x = "[y]" in ballE)
wenzelm@13142
   465
    apply simp
wenzelm@13142
   466
   apply blast
wenzelm@13142
   467
  apply blast
wenzelm@13142
   468
  done
wenzelm@13114
   469
wenzelm@13114
   470
lemma inj_map: "inj (map f) = inj f"
wenzelm@13142
   471
  by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114
   472
wenzelm@13114
   473
wenzelm@13142
   474
subsection {* @{text rev} *}
wenzelm@13114
   475
wenzelm@13142
   476
lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
wenzelm@13142
   477
  by (induct xs) auto
wenzelm@13114
   478
wenzelm@13142
   479
lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
wenzelm@13142
   480
  by (induct xs) auto
wenzelm@13114
   481
wenzelm@13142
   482
lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
wenzelm@13142
   483
  by (induct xs) auto
wenzelm@13114
   484
wenzelm@13142
   485
lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
wenzelm@13142
   486
  by (induct xs) auto
wenzelm@13114
   487
wenzelm@13142
   488
lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
wenzelm@13142
   489
  apply (induct xs)
wenzelm@13142
   490
   apply force
wenzelm@13142
   491
  apply (case_tac ys)
wenzelm@13142
   492
   apply simp
wenzelm@13142
   493
  apply force
wenzelm@13142
   494
  done
wenzelm@13114
   495
wenzelm@13142
   496
lemma rev_induct: "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
wenzelm@13142
   497
  apply(subst rev_rev_ident[symmetric])
wenzelm@13142
   498
  apply(rule_tac list = "rev xs" in list.induct, simp_all)
wenzelm@13142
   499
  done
wenzelm@13114
   500
wenzelm@13142
   501
ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}  -- "compatibility"
wenzelm@13114
   502
wenzelm@13142
   503
lemma rev_exhaust: "(xs = [] ==> P) ==>  (!!ys y. xs = ys @ [y] ==> P) ==> P"
wenzelm@13142
   504
  by (induct xs rule: rev_induct) auto
wenzelm@13114
   505
wenzelm@13114
   506
wenzelm@13142
   507
subsection {* @{text set} *}
wenzelm@13114
   508
wenzelm@13142
   509
lemma finite_set [iff]: "finite (set xs)"
wenzelm@13142
   510
  by (induct xs) auto
wenzelm@13114
   511
wenzelm@13142
   512
lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
wenzelm@13142
   513
  by (induct xs) auto
wenzelm@13114
   514
wenzelm@13142
   515
lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
wenzelm@13142
   516
  by auto
wenzelm@13114
   517
wenzelm@13142
   518
lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
wenzelm@13142
   519
  by (induct xs) auto
wenzelm@13114
   520
wenzelm@13142
   521
lemma set_rev [simp]: "set (rev xs) = set xs"
wenzelm@13142
   522
  by (induct xs) auto
wenzelm@13114
   523
wenzelm@13142
   524
lemma set_map [simp]: "set (map f xs) = f`(set xs)"
wenzelm@13142
   525
  by (induct xs) auto
wenzelm@13114
   526
wenzelm@13142
   527
lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
wenzelm@13142
   528
  by (induct xs) auto
wenzelm@13114
   529
wenzelm@13142
   530
lemma set_upt [simp]: "set[i..j(] = {k. i \<le> k \<and> k < j}"
wenzelm@13142
   531
  apply (induct j)
wenzelm@13142
   532
   apply simp_all
wenzelm@13142
   533
  apply(erule ssubst)
wenzelm@13142
   534
  apply auto
wenzelm@13142
   535
  apply arith
wenzelm@13142
   536
  done
wenzelm@13114
   537
wenzelm@13142
   538
lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
wenzelm@13142
   539
  apply (induct xs)
wenzelm@13142
   540
   apply simp
wenzelm@13142
   541
  apply simp
wenzelm@13142
   542
  apply (rule iffI)
wenzelm@13142
   543
   apply (blast intro: eq_Nil_appendI Cons_eq_appendI)
wenzelm@13142
   544
  apply (erule exE)+
wenzelm@13142
   545
  apply (case_tac ys)
wenzelm@13142
   546
  apply auto
wenzelm@13142
   547
  done
wenzelm@13142
   548
wenzelm@13142
   549
lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)"
wenzelm@13142
   550
  -- {* eliminate @{text lists} in favour of @{text set} *}
wenzelm@13142
   551
  by (induct xs) auto
wenzelm@13142
   552
wenzelm@13142
   553
lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A"
wenzelm@13142
   554
  by (rule in_lists_conv_set [THEN iffD1])
wenzelm@13142
   555
wenzelm@13142
   556
lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A"
wenzelm@13142
   557
  by (rule in_lists_conv_set [THEN iffD2])
wenzelm@13114
   558
wenzelm@13114
   559
wenzelm@13142
   560
subsection {* @{text mem} *}
wenzelm@13114
   561
wenzelm@13114
   562
lemma set_mem_eq: "(x mem xs) = (x : set xs)"
wenzelm@13142
   563
  by (induct xs) auto
wenzelm@13114
   564
wenzelm@13114
   565
wenzelm@13142
   566
subsection {* @{text list_all} *}
wenzelm@13114
   567
wenzelm@13142
   568
lemma list_all_conv: "list_all P xs = (\<forall>x \<in> set xs. P x)"
wenzelm@13142
   569
  by (induct xs) auto
wenzelm@13114
   570
wenzelm@13142
   571
lemma list_all_append [simp]:
wenzelm@13142
   572
    "list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)"
wenzelm@13142
   573
  by (induct xs) auto
wenzelm@13114
   574
wenzelm@13114
   575
wenzelm@13142
   576
subsection {* @{text filter} *}
wenzelm@13114
   577
wenzelm@13142
   578
lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
wenzelm@13142
   579
  by (induct xs) auto
wenzelm@13114
   580
wenzelm@13142
   581
lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
wenzelm@13142
   582
  by (induct xs) auto
wenzelm@13114
   583
wenzelm@13142
   584
lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
wenzelm@13142
   585
  by (induct xs) auto
wenzelm@13114
   586
wenzelm@13142
   587
lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
wenzelm@13142
   588
  by (induct xs) auto
wenzelm@13114
   589
wenzelm@13142
   590
lemma length_filter [simp]: "length (filter P xs) \<le> length xs"
wenzelm@13142
   591
  by (induct xs) (auto simp add: le_SucI)
wenzelm@13114
   592
wenzelm@13142
   593
lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
wenzelm@13142
   594
  by auto
wenzelm@13114
   595
wenzelm@13114
   596
wenzelm@13142
   597
subsection {* @{text concat} *}
wenzelm@13114
   598
wenzelm@13142
   599
lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
wenzelm@13142
   600
  by (induct xs) auto
wenzelm@13114
   601
wenzelm@13142
   602
lemma concat_eq_Nil_conv [iff]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
wenzelm@13142
   603
  by (induct xss) auto
wenzelm@13114
   604
wenzelm@13142
   605
lemma Nil_eq_concat_conv [iff]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
wenzelm@13142
   606
  by (induct xss) auto
wenzelm@13114
   607
wenzelm@13142
   608
lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)"
wenzelm@13142
   609
  by (induct xs) auto
wenzelm@13114
   610
wenzelm@13142
   611
lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
wenzelm@13142
   612
  by (induct xs) auto
wenzelm@13114
   613
wenzelm@13142
   614
lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
wenzelm@13142
   615
  by (induct xs) auto
wenzelm@13114
   616
wenzelm@13142
   617
lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
wenzelm@13142
   618
  by (induct xs) auto
wenzelm@13114
   619
wenzelm@13114
   620
wenzelm@13142
   621
subsection {* @{text nth} *}
wenzelm@13114
   622
wenzelm@13142
   623
lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
wenzelm@13142
   624
  by auto
wenzelm@13114
   625
wenzelm@13142
   626
lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
wenzelm@13142
   627
  by auto
wenzelm@13114
   628
wenzelm@13142
   629
declare nth.simps [simp del]
wenzelm@13114
   630
wenzelm@13114
   631
lemma nth_append:
wenzelm@13142
   632
    "!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
wenzelm@13142
   633
  apply(induct "xs")
wenzelm@13142
   634
   apply simp
wenzelm@13142
   635
  apply (case_tac n)
wenzelm@13142
   636
   apply auto
wenzelm@13142
   637
  done
wenzelm@13114
   638
wenzelm@13142
   639
lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)"
wenzelm@13142
   640
  apply(induct xs)
wenzelm@13142
   641
   apply simp
wenzelm@13142
   642
  apply (case_tac n)
wenzelm@13142
   643
   apply auto
wenzelm@13142
   644
  done
wenzelm@13114
   645
wenzelm@13142
   646
lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
wenzelm@13142
   647
  apply (induct_tac xs)
wenzelm@13142
   648
   apply simp
wenzelm@13114
   649
  apply simp
wenzelm@13142
   650
  apply safe
wenzelm@13142
   651
    apply (rule_tac x = 0 in exI)
wenzelm@13142
   652
    apply simp
wenzelm@13142
   653
   apply (rule_tac x = "Suc i" in exI)
wenzelm@13142
   654
   apply simp
wenzelm@13142
   655
  apply (case_tac i)
wenzelm@13142
   656
   apply simp
wenzelm@13142
   657
  apply (rename_tac j)
wenzelm@13142
   658
  apply (rule_tac x = j in exI)
wenzelm@13142
   659
  apply simp
wenzelm@13142
   660
  done
wenzelm@13114
   661
wenzelm@13142
   662
lemma list_ball_nth: "[| n < length xs; !x : set xs. P x  |] ==> P(xs!n)"
wenzelm@13142
   663
  by (auto simp add: set_conv_nth)
wenzelm@13114
   664
wenzelm@13142
   665
lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
wenzelm@13142
   666
  by (auto simp add: set_conv_nth)
wenzelm@13114
   667
wenzelm@13114
   668
lemma all_nth_imp_all_set:
wenzelm@13142
   669
    "[| !i < length xs. P(xs!i); x : set xs  |] ==> P x"
wenzelm@13142
   670
  by (auto simp add: set_conv_nth)
wenzelm@13114
   671
wenzelm@13114
   672
lemma all_set_conv_all_nth:
wenzelm@13142
   673
    "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
wenzelm@13142
   674
  by (auto simp add: set_conv_nth)
wenzelm@13114
   675
wenzelm@13114
   676
wenzelm@13142
   677
subsection {* @{text list_update} *}
wenzelm@13114
   678
wenzelm@13142
   679
lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs"
wenzelm@13142
   680
  by (induct xs) (auto split: nat.split)
wenzelm@13114
   681
wenzelm@13114
   682
lemma nth_list_update:
wenzelm@13142
   683
    "!!i j. i < length xs  ==> (xs[i:=x])!j = (if i = j then x else xs!j)"
wenzelm@13142
   684
  by (induct xs) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
   685
wenzelm@13142
   686
lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
wenzelm@13142
   687
  by (simp add: nth_list_update)
wenzelm@13114
   688
wenzelm@13142
   689
lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j"
wenzelm@13142
   690
  by (induct xs) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114
   691
wenzelm@13142
   692
lemma list_update_overwrite [simp]:
wenzelm@13142
   693
    "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
wenzelm@13142
   694
  by (induct xs) (auto split: nat.split)
wenzelm@13114
   695
wenzelm@13114
   696
lemma list_update_same_conv:
wenzelm@13142
   697
    "!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
wenzelm@13142
   698
  by (induct xs) (auto split: nat.split)
wenzelm@13114
   699
wenzelm@13114
   700
lemma update_zip:
wenzelm@13142
   701
  "!!i xy xs. length xs = length ys ==>
wenzelm@13114
   702
    (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
wenzelm@13142
   703
  by (induct ys) (auto, case_tac xs, auto split: nat.split)
wenzelm@13114
   704
wenzelm@13114
   705
lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"
wenzelm@13142
   706
  by (induct xs) (auto split: nat.split)
wenzelm@13114
   707
wenzelm@13114
   708
lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
wenzelm@13142
   709
  by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114
   710
wenzelm@13114
   711
wenzelm@13142
   712
subsection {* @{text last} and @{text butlast} *}
wenzelm@13114
   713
wenzelm@13142
   714
lemma last_snoc [simp]: "last (xs @ [x]) = x"
wenzelm@13142
   715
  by (induct xs) auto
wenzelm@13114
   716
wenzelm@13142
   717
lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
wenzelm@13142
   718
  by (induct xs) auto
wenzelm@13114
   719
wenzelm@13142
   720
lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
wenzelm@13142
   721
  by (induct xs rule: rev_induct) auto
wenzelm@13114
   722
wenzelm@13114
   723
lemma butlast_append:
wenzelm@13142
   724
    "!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
wenzelm@13142
   725
  by (induct xs) auto
wenzelm@13114
   726
wenzelm@13142
   727
lemma append_butlast_last_id [simp]:
wenzelm@13142
   728
    "xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
wenzelm@13142
   729
  by (induct xs) auto
wenzelm@13114
   730
wenzelm@13142
   731
lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
wenzelm@13142
   732
  by (induct xs) (auto split: split_if_asm)
wenzelm@13114
   733
wenzelm@13114
   734
lemma in_set_butlast_appendI:
wenzelm@13142
   735
    "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
wenzelm@13142
   736
  by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114
   737
wenzelm@13142
   738
wenzelm@13142
   739
subsection {* @{text take} and @{text drop} *}
wenzelm@13114
   740
wenzelm@13142
   741
lemma take_0 [simp]: "take 0 xs = []"
wenzelm@13142
   742
  by (induct xs) auto
wenzelm@13114
   743
wenzelm@13142
   744
lemma drop_0 [simp]: "drop 0 xs = xs"
wenzelm@13142
   745
  by (induct xs) auto
wenzelm@13114
   746
wenzelm@13142
   747
lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
wenzelm@13142
   748
  by simp
wenzelm@13114
   749
wenzelm@13142
   750
lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
wenzelm@13142
   751
  by simp
wenzelm@13114
   752
wenzelm@13142
   753
declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114
   754
wenzelm@13142
   755
lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n"
wenzelm@13142
   756
  by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   757
wenzelm@13142
   758
lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)"
wenzelm@13142
   759
  by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   760
wenzelm@13142
   761
lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs"
wenzelm@13142
   762
  by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   763
wenzelm@13142
   764
lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []"
wenzelm@13142
   765
  by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   766
wenzelm@13142
   767
lemma take_append [simp]:
wenzelm@13142
   768
    "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
wenzelm@13142
   769
  by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   770
wenzelm@13142
   771
lemma drop_append [simp]:
wenzelm@13142
   772
    "!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
wenzelm@13142
   773
  by (induct n) (auto, case_tac xs, auto)
wenzelm@13114
   774
wenzelm@13142
   775
lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
wenzelm@13142
   776
  apply (induct m)
wenzelm@13142
   777
   apply auto
wenzelm@13142
   778
  apply (case_tac xs)
wenzelm@13142
   779
   apply auto
wenzelm@13142
   780
  apply (case_tac na)
wenzelm@13142
   781
   apply auto
wenzelm@13142
   782
  done
wenzelm@13114
   783
wenzelm@13142
   784
lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs"
wenzelm@13142
   785
  apply (induct m)
wenzelm@13142
   786
   apply auto
wenzelm@13142
   787
  apply (case_tac xs)
wenzelm@13142
   788
   apply auto
wenzelm@13142
   789
  done
wenzelm@13114
   790
wenzelm@13114
   791
lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"
wenzelm@13142
   792
  apply (induct m)
wenzelm@13142
   793
   apply auto
wenzelm@13142
   794
  apply (case_tac xs)
wenzelm@13142
   795
   apply auto
wenzelm@13142
   796
  done
wenzelm@13114
   797
wenzelm@13142
   798
lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs"
wenzelm@13142
   799
  apply (induct n)
wenzelm@13142
   800
   apply auto
wenzelm@13142
   801
  apply (case_tac xs)
wenzelm@13142
   802
   apply auto
wenzelm@13142
   803
  done
wenzelm@13114
   804
wenzelm@13114
   805
lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"
wenzelm@13142
   806
  apply (induct n)
wenzelm@13142
   807
   apply auto
wenzelm@13142
   808
  apply (case_tac xs)
wenzelm@13142
   809
   apply auto
wenzelm@13142
   810
  done
wenzelm@13114
   811
wenzelm@13142
   812
lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)"
wenzelm@13142
   813
  apply (induct n)
wenzelm@13142
   814
   apply auto
wenzelm@13142
   815
  apply (case_tac xs)
wenzelm@13142
   816
   apply auto
wenzelm@13142
   817
  done
wenzelm@13114
   818
wenzelm@13114
   819
lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"
wenzelm@13142
   820
  apply (induct xs)
wenzelm@13142
   821
   apply auto
wenzelm@13142
   822
  apply (case_tac i)
wenzelm@13142
   823
   apply auto
wenzelm@13142
   824
  done
wenzelm@13114
   825
wenzelm@13114
   826
lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"
wenzelm@13142
   827
  apply (induct xs)
wenzelm@13142
   828
   apply auto
wenzelm@13142
   829
  apply (case_tac i)
wenzelm@13142
   830
   apply auto
wenzelm@13142
   831
  done
wenzelm@13114
   832
wenzelm@13142
   833
lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
wenzelm@13142
   834
  apply (induct xs)
wenzelm@13142
   835
   apply auto
wenzelm@13142
   836
  apply (case_tac n)
wenzelm@13142
   837
   apply(blast )
wenzelm@13142
   838
  apply (case_tac i)
wenzelm@13142
   839
   apply auto
wenzelm@13142
   840
  done
wenzelm@13114
   841
wenzelm@13142
   842
lemma nth_drop [simp]:
wenzelm@13142
   843
    "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
wenzelm@13142
   844
  apply (induct n)
wenzelm@13142
   845
   apply auto
wenzelm@13142
   846
  apply (case_tac xs)
wenzelm@13142
   847
   apply auto
wenzelm@13142
   848
  done
nipkow@3507
   849
wenzelm@13114
   850
lemma append_eq_conv_conj:
wenzelm@13142
   851
    "!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
wenzelm@13142
   852
  apply(induct xs)
wenzelm@13142
   853
   apply simp
wenzelm@13142
   854
  apply clarsimp
wenzelm@13142
   855
  apply (case_tac zs)
wenzelm@13142
   856
  apply auto
wenzelm@13142
   857
  done
wenzelm@13142
   858
wenzelm@13114
   859
wenzelm@13142
   860
subsection {* @{text takeWhile} and @{text dropWhile} *}
wenzelm@13114
   861
wenzelm@13142
   862
lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
wenzelm@13142
   863
  by (induct xs) auto
wenzelm@13114
   864
wenzelm@13142
   865
lemma takeWhile_append1 [simp]:
wenzelm@13142
   866
    "[| x:set xs; ~P(x)  |] ==> takeWhile P (xs @ ys) = takeWhile P xs"
wenzelm@13142
   867
  by (induct xs) auto
wenzelm@13114
   868
wenzelm@13142
   869
lemma takeWhile_append2 [simp]:
wenzelm@13142
   870
    "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
wenzelm@13142
   871
  by (induct xs) auto
wenzelm@13114
   872
wenzelm@13142
   873
lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
wenzelm@13142
   874
  by (induct xs) auto
wenzelm@13114
   875
wenzelm@13142
   876
lemma dropWhile_append1 [simp]:
wenzelm@13142
   877
    "[| x : set xs; ~P(x)  |] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
wenzelm@13142
   878
  by (induct xs) auto
wenzelm@13114
   879
wenzelm@13142
   880
lemma dropWhile_append2 [simp]:
wenzelm@13142
   881
    "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
wenzelm@13142
   882
  by (induct xs) auto
wenzelm@13114
   883
wenzelm@13142
   884
lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
wenzelm@13142
   885
  by (induct xs) (auto split: split_if_asm)
wenzelm@13114
   886
wenzelm@13114
   887
wenzelm@13142
   888
subsection {* @{text zip} *}
wenzelm@13114
   889
wenzelm@13142
   890
lemma zip_Nil [simp]: "zip [] ys = []"
wenzelm@13142
   891
  by (induct ys) auto
wenzelm@13114
   892
wenzelm@13142
   893
lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
wenzelm@13142
   894
  by simp
wenzelm@13114
   895
wenzelm@13142
   896
declare zip_Cons [simp del]
wenzelm@13114
   897
wenzelm@13142
   898
lemma length_zip [simp]:
wenzelm@13142
   899
    "!!xs. length (zip xs ys) = min (length xs) (length ys)"
wenzelm@13142
   900
  apply(induct ys)
wenzelm@13142
   901
   apply simp
wenzelm@13142
   902
  apply (case_tac xs)
wenzelm@13142
   903
   apply auto
wenzelm@13142
   904
  done
wenzelm@13114
   905
wenzelm@13114
   906
lemma zip_append1:
wenzelm@13142
   907
  "!!xs. zip (xs @ ys) zs =
wenzelm@13142
   908
      zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
wenzelm@13142
   909
  apply (induct zs)
wenzelm@13142
   910
   apply simp
wenzelm@13142
   911
  apply (case_tac xs)
wenzelm@13142
   912
   apply simp_all
wenzelm@13142
   913
  done
wenzelm@13114
   914
wenzelm@13114
   915
lemma zip_append2:
wenzelm@13142
   916
  "!!ys. zip xs (ys @ zs) =
wenzelm@13142
   917
      zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
wenzelm@13142
   918
  apply (induct xs)
wenzelm@13142
   919
   apply simp
wenzelm@13142
   920
  apply (case_tac ys)
wenzelm@13142
   921
   apply simp_all
wenzelm@13142
   922
  done
wenzelm@13114
   923
wenzelm@13142
   924
lemma zip_append [simp]:
wenzelm@13142
   925
 "[| length xs = length us; length ys = length vs |] ==>
wenzelm@13142
   926
    zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
wenzelm@13142
   927
  by (simp add: zip_append1)
wenzelm@13114
   928
wenzelm@13114
   929
lemma zip_rev:
wenzelm@13142
   930
    "!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
wenzelm@13142
   931
  apply(induct ys)
wenzelm@13142
   932
   apply simp
wenzelm@13142
   933
  apply (case_tac xs)
wenzelm@13142
   934
   apply simp_all
wenzelm@13142
   935
  done
wenzelm@13114
   936
wenzelm@13142
   937
lemma nth_zip [simp]:
wenzelm@13142
   938
  "!!i xs. [| i < length xs; i < length ys  |] ==> (zip xs ys)!i = (xs!i, ys!i)"
wenzelm@13142
   939
  apply (induct ys)
wenzelm@13142
   940
   apply simp
wenzelm@13142
   941
  apply (case_tac xs)
wenzelm@13142
   942
   apply (simp_all add: nth.simps split: nat.split)
wenzelm@13142
   943
  done
wenzelm@13114
   944
wenzelm@13114
   945
lemma set_zip:
wenzelm@13142
   946
    "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
wenzelm@13142
   947
  by (simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114
   948
wenzelm@13114
   949
lemma zip_update:
wenzelm@13142
   950
    "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
wenzelm@13142
   951
  by (rule sym, simp add: update_zip)
wenzelm@13114
   952
wenzelm@13142
   953
lemma zip_replicate [simp]:
wenzelm@13142
   954
    "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
wenzelm@13142
   955
  apply (induct i)
wenzelm@13142
   956
   apply auto
wenzelm@13142
   957
  apply (case_tac j)
wenzelm@13142
   958
   apply auto
wenzelm@13142
   959
  done
wenzelm@13114
   960
wenzelm@13142
   961
wenzelm@13142
   962
subsection {* @{text list_all2} *}
wenzelm@13114
   963
wenzelm@13114
   964
lemma list_all2_lengthD: "list_all2 P xs ys ==> length xs = length ys"
wenzelm@13142
   965
  by (simp add: list_all2_def)
wenzelm@13114
   966
wenzelm@13142
   967
lemma list_all2_Nil [iff]: "list_all2 P [] ys = (ys = [])"
wenzelm@13142
   968
  by (simp add: list_all2_def)
wenzelm@13114
   969
wenzelm@13142
   970
lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])"
wenzelm@13142
   971
  by (simp add: list_all2_def)
wenzelm@13114
   972
wenzelm@13142
   973
lemma list_all2_Cons [iff]:
wenzelm@13142
   974
    "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
wenzelm@13142
   975
  by (auto simp add: list_all2_def)
wenzelm@13114
   976
wenzelm@13114
   977
lemma list_all2_Cons1:
wenzelm@13142
   978
    "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
wenzelm@13142
   979
  by (cases ys) auto
wenzelm@13114
   980
wenzelm@13114
   981
lemma list_all2_Cons2:
wenzelm@13142
   982
    "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
wenzelm@13142
   983
  by (cases xs) auto
wenzelm@13114
   984
wenzelm@13142
   985
lemma list_all2_rev [iff]:
wenzelm@13142
   986
    "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
wenzelm@13142
   987
  by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114
   988
wenzelm@13114
   989
lemma list_all2_append1:
wenzelm@13142
   990
  "list_all2 P (xs @ ys) zs =
wenzelm@13142
   991
    (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
wenzelm@13142
   992
      list_all2 P xs us \<and> list_all2 P ys vs)"
wenzelm@13142
   993
  apply (simp add: list_all2_def zip_append1)
wenzelm@13142
   994
  apply (rule iffI)
wenzelm@13142
   995
   apply (rule_tac x = "take (length xs) zs" in exI)
wenzelm@13142
   996
   apply (rule_tac x = "drop (length xs) zs" in exI)
wenzelm@13142
   997
   apply (force split: nat_diff_split simp add: min_def)
wenzelm@13142
   998
  apply clarify
wenzelm@13142
   999
  apply (simp add: ball_Un)
wenzelm@13142
  1000
  done
wenzelm@13114
  1001
wenzelm@13114
  1002
lemma list_all2_append2:
wenzelm@13142
  1003
  "list_all2 P xs (ys @ zs) =
wenzelm@13142
  1004
    (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
wenzelm@13142
  1005
      list_all2 P us ys \<and> list_all2 P vs zs)"
wenzelm@13142
  1006
  apply (simp add: list_all2_def zip_append2)
wenzelm@13142
  1007
  apply (rule iffI)
wenzelm@13142
  1008
   apply (rule_tac x = "take (length ys) xs" in exI)
wenzelm@13142
  1009
   apply (rule_tac x = "drop (length ys) xs" in exI)
wenzelm@13142
  1010
   apply (force split: nat_diff_split simp add: min_def)
wenzelm@13142
  1011
  apply clarify
wenzelm@13142
  1012
  apply (simp add: ball_Un)
wenzelm@13142
  1013
  done
wenzelm@13114
  1014
wenzelm@13114
  1015
lemma list_all2_conv_all_nth:
wenzelm@13114
  1016
  "list_all2 P xs ys =
wenzelm@13142
  1017
    (length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
wenzelm@13142
  1018
  by (force simp add: list_all2_def set_zip)
wenzelm@13114
  1019
wenzelm@13114
  1020
lemma list_all2_trans[rule_format]:
wenzelm@13142
  1021
  "\<forall>a b c. P1 a b --> P2 b c --> P3 a c ==>
wenzelm@13142
  1022
    \<forall>bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs"
wenzelm@13142
  1023
  apply(induct_tac as)
wenzelm@13142
  1024
   apply simp
wenzelm@13142
  1025
  apply(rule allI)
wenzelm@13142
  1026
  apply(induct_tac bs)
wenzelm@13142
  1027
   apply simp
wenzelm@13142
  1028
  apply(rule allI)
wenzelm@13142
  1029
  apply(induct_tac cs)
wenzelm@13142
  1030
   apply auto
wenzelm@13142
  1031
  done
wenzelm@13142
  1032
wenzelm@13142
  1033
wenzelm@13142
  1034
subsection {* @{text foldl} *}
wenzelm@13142
  1035
wenzelm@13142
  1036
lemma foldl_append [simp]:
wenzelm@13142
  1037
  "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
wenzelm@13142
  1038
  by (induct xs) auto
wenzelm@13142
  1039
wenzelm@13142
  1040
text {*
wenzelm@13142
  1041
  Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
wenzelm@13142
  1042
  difficult to use because it requires an additional transitivity step.
wenzelm@13142
  1043
*}
wenzelm@13142
  1044
wenzelm@13142
  1045
lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns"
wenzelm@13142
  1046
  by (induct ns) auto
wenzelm@13142
  1047
wenzelm@13142
  1048
lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns"
wenzelm@13142
  1049
  by (force intro: start_le_sum simp add: in_set_conv_decomp)
wenzelm@13142
  1050
wenzelm@13142
  1051
lemma sum_eq_0_conv [iff]:
wenzelm@13142
  1052
    "!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
wenzelm@13142
  1053
  by (induct ns) auto
wenzelm@13114
  1054
wenzelm@13114
  1055
wenzelm@13142
  1056
subsection {* @{text upto} *}
wenzelm@13114
  1057
wenzelm@13142
  1058
lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])"
wenzelm@13142
  1059
  -- {* Does not terminate! *}
wenzelm@13142
  1060
  by (induct j) auto
wenzelm@13142
  1061
wenzelm@13142
  1062
lemma upt_conv_Nil [simp]: "j <= i ==> [i..j(] = []"
wenzelm@13142
  1063
  by (subst upt_rec) simp
wenzelm@13114
  1064
wenzelm@13142
  1065
lemma upt_Suc_append: "i <= j ==> [i..(Suc j)(] = [i..j(]@[j]"
wenzelm@13142
  1066
  -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
wenzelm@13142
  1067
  by simp
wenzelm@13114
  1068
wenzelm@13142
  1069
lemma upt_conv_Cons: "i < j ==> [i..j(] = i # [Suc i..j(]"
wenzelm@13142
  1070
  apply(rule trans)
wenzelm@13142
  1071
  apply(subst upt_rec)
wenzelm@13142
  1072
   prefer 2 apply(rule refl)
wenzelm@13142
  1073
  apply simp
wenzelm@13142
  1074
  done
wenzelm@13114
  1075
wenzelm@13142
  1076
lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"
wenzelm@13142
  1077
  -- {* LOOPS as a simprule, since @{text "j <= j"}. *}
wenzelm@13142
  1078
  by (induct k) auto
wenzelm@13114
  1079
wenzelm@13142
  1080
lemma length_upt [simp]: "length [i..j(] = j - i"
wenzelm@13142
  1081
  by (induct j) (auto simp add: Suc_diff_le)
wenzelm@13114
  1082
wenzelm@13142
  1083
lemma nth_upt [simp]: "i + k < j ==> [i..j(] ! k = i + k"
wenzelm@13142
  1084
  apply (induct j)
wenzelm@13142
  1085
  apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
wenzelm@13142
  1086
  done
wenzelm@13114
  1087
wenzelm@13142
  1088
lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]"
wenzelm@13142
  1089
  apply (induct m)
wenzelm@13142
  1090
   apply simp
wenzelm@13142
  1091
  apply (subst upt_rec)
wenzelm@13142
  1092
  apply (rule sym)
wenzelm@13142
  1093
  apply (subst upt_rec)
wenzelm@13142
  1094
  apply (simp del: upt.simps)
wenzelm@13142
  1095
  done
nipkow@3507
  1096
wenzelm@13114
  1097
lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]"
wenzelm@13142
  1098
  by (induct n) auto
wenzelm@13114
  1099
wenzelm@13114
  1100
lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)"
wenzelm@13142
  1101
  apply (induct n m rule: diff_induct)
wenzelm@13142
  1102
    prefer 3 apply (subst map_Suc_upt[symmetric])
wenzelm@13142
  1103
    apply (auto simp add: less_diff_conv nth_upt)
wenzelm@13142
  1104
  done
wenzelm@13114
  1105
wenzelm@13142
  1106
lemma nth_take_lemma [rule_format]:
wenzelm@13142
  1107
  "ALL xs ys. k <= length xs --> k <= length ys
wenzelm@13142
  1108
    --> (ALL i. i < k --> xs!i = ys!i)
wenzelm@13142
  1109
    --> take k xs = take k ys"
wenzelm@13142
  1110
  apply (induct k)
wenzelm@13142
  1111
  apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib)
wenzelm@13142
  1112
  apply clarify
wenzelm@13142
  1113
  txt {* Both lists must be non-empty *}
wenzelm@13142
  1114
  apply (case_tac xs)
wenzelm@13142
  1115
   apply simp
wenzelm@13142
  1116
  apply (case_tac ys)
wenzelm@13142
  1117
   apply clarify
wenzelm@13142
  1118
   apply (simp (no_asm_use))
wenzelm@13142
  1119
  apply clarify
wenzelm@13142
  1120
  txt {* prenexing's needed, not miniscoping *}
wenzelm@13142
  1121
  apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
wenzelm@13142
  1122
  apply blast
wenzelm@13142
  1123
  done
wenzelm@13114
  1124
wenzelm@13114
  1125
lemma nth_equalityI:
wenzelm@13114
  1126
 "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
wenzelm@13142
  1127
  apply (frule nth_take_lemma [OF le_refl eq_imp_le])
wenzelm@13142
  1128
  apply (simp_all add: take_all)
wenzelm@13142
  1129
  done
wenzelm@13142
  1130
wenzelm@13142
  1131
lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
wenzelm@13142
  1132
  -- {* The famous take-lemma. *}
wenzelm@13142
  1133
  apply (drule_tac x = "max (length xs) (length ys)" in spec)
wenzelm@13142
  1134
  apply (simp add: le_max_iff_disj take_all)
wenzelm@13142
  1135
  done
wenzelm@13142
  1136
wenzelm@13142
  1137
wenzelm@13142
  1138
subsection {* @{text "distinct"} and @{text remdups} *}
wenzelm@13142
  1139
wenzelm@13142
  1140
lemma distinct_append [simp]:
wenzelm@13142
  1141
    "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
wenzelm@13142
  1142
  by (induct xs) auto
wenzelm@13142
  1143
wenzelm@13142
  1144
lemma set_remdups [simp]: "set (remdups xs) = set xs"
wenzelm@13142
  1145
  by (induct xs) (auto simp add: insert_absorb)
wenzelm@13142
  1146
wenzelm@13142
  1147
lemma distinct_remdups [iff]: "distinct (remdups xs)"
wenzelm@13142
  1148
  by (induct xs) auto
wenzelm@13142
  1149
wenzelm@13142
  1150
lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
wenzelm@13142
  1151
  by (induct xs) auto
wenzelm@13114
  1152
wenzelm@13142
  1153
text {*
wenzelm@13142
  1154
  It is best to avoid this indexed version of distinct, but sometimes
wenzelm@13142
  1155
  it is useful. *}
wenzelm@13142
  1156
lemma distinct_conv_nth:
wenzelm@13142
  1157
    "distinct xs = (\<forall>i j. i < size xs \<and> j < size xs \<and> i \<noteq> j --> xs!i \<noteq> xs!j)"
wenzelm@13142
  1158
  apply (induct_tac xs)
wenzelm@13142
  1159
   apply simp
wenzelm@13142
  1160
  apply simp
wenzelm@13142
  1161
  apply (rule iffI)
wenzelm@13142
  1162
   apply clarsimp
wenzelm@13142
  1163
   apply (case_tac i)
wenzelm@13142
  1164
    apply (case_tac j)
wenzelm@13142
  1165
     apply simp
wenzelm@13142
  1166
    apply (simp add: set_conv_nth)
wenzelm@13142
  1167
   apply (case_tac j)
wenzelm@13142
  1168
    apply (clarsimp simp add: set_conv_nth)
wenzelm@13142
  1169
   apply simp
wenzelm@13142
  1170
  apply (rule conjI)
wenzelm@13142
  1171
   apply (clarsimp simp add: set_conv_nth)
wenzelm@13142
  1172
   apply (erule_tac x = 0 in allE)
wenzelm@13142
  1173
   apply (erule_tac x = "Suc i" in allE)
wenzelm@13142
  1174
   apply simp
wenzelm@13142
  1175
  apply clarsimp
wenzelm@13142
  1176
  apply (erule_tac x = "Suc i" in allE)
wenzelm@13142
  1177
  apply (erule_tac x = "Suc j" in allE)
wenzelm@13142
  1178
  apply simp
wenzelm@13142
  1179
  done
wenzelm@13114
  1180
wenzelm@13114
  1181
wenzelm@13142
  1182
subsection {* @{text replicate} *}
wenzelm@13114
  1183
wenzelm@13142
  1184
lemma length_replicate [simp]: "length (replicate n x) = n"
wenzelm@13142
  1185
  by (induct n) auto
nipkow@13124
  1186
wenzelm@13142
  1187
lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
wenzelm@13142
  1188
  by (induct n) auto
wenzelm@13114
  1189
wenzelm@13114
  1190
lemma replicate_app_Cons_same:
wenzelm@13142
  1191
    "(replicate n x) @ (x # xs) = x # replicate n x @ xs"
wenzelm@13142
  1192
  by (induct n) auto
wenzelm@13114
  1193
wenzelm@13142
  1194
lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
wenzelm@13142
  1195
  apply(induct n)
wenzelm@13142
  1196
   apply simp
wenzelm@13142
  1197
  apply (simp add: replicate_app_Cons_same)
wenzelm@13142
  1198
  done
wenzelm@13114
  1199
wenzelm@13142
  1200
lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
wenzelm@13142
  1201
  by (induct n) auto
wenzelm@13114
  1202
wenzelm@13142
  1203
lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
wenzelm@13142
  1204
  by (induct n) auto
wenzelm@13114
  1205
wenzelm@13142
  1206
lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
wenzelm@13142
  1207
  by (induct n) auto
wenzelm@13114
  1208
wenzelm@13142
  1209
lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
wenzelm@13142
  1210
  by (atomize (full), induct n) auto
wenzelm@13114
  1211
wenzelm@13142
  1212
lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x"
wenzelm@13142
  1213
  apply(induct n)
wenzelm@13142
  1214
   apply simp
wenzelm@13142
  1215
  apply (simp add: nth_Cons split: nat.split)
wenzelm@13142
  1216
  done
wenzelm@13114
  1217
wenzelm@13142
  1218
lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
wenzelm@13142
  1219
  by (induct n) auto
wenzelm@13114
  1220
wenzelm@13142
  1221
lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
wenzelm@13142
  1222
  by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
wenzelm@13114
  1223
wenzelm@13142
  1224
lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
wenzelm@13142
  1225
  by auto
wenzelm@13114
  1226
wenzelm@13142
  1227
lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
wenzelm@13142
  1228
  by (simp add: set_replicate_conv_if split: split_if_asm)
wenzelm@13114
  1229
wenzelm@13114
  1230
wenzelm@13142
  1231
subsection {* Lexcicographic orderings on lists *}
nipkow@3507
  1232
wenzelm@13142
  1233
lemma wf_lexn: "wf r ==> wf (lexn r n)"
wenzelm@13142
  1234
  apply (induct_tac n)
wenzelm@13142
  1235
   apply simp
wenzelm@13142
  1236
  apply simp
wenzelm@13142
  1237
  apply(rule wf_subset)
wenzelm@13142
  1238
   prefer 2 apply (rule Int_lower1)
wenzelm@13142
  1239
  apply(rule wf_prod_fun_image)
wenzelm@13142
  1240
   prefer 2 apply (rule injI)
wenzelm@13142
  1241
  apply auto
wenzelm@13142
  1242
  done
wenzelm@13114
  1243
wenzelm@13114
  1244
lemma lexn_length:
wenzelm@13142
  1245
    "!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
wenzelm@13142
  1246
  by (induct n) auto
wenzelm@13114
  1247
wenzelm@13142
  1248
lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
wenzelm@13142
  1249
  apply (unfold lex_def)
wenzelm@13142
  1250
  apply (rule wf_UN)
wenzelm@13142
  1251
  apply (blast intro: wf_lexn)
wenzelm@13142
  1252
  apply clarify
wenzelm@13142
  1253
  apply (rename_tac m n)
wenzelm@13142
  1254
  apply (subgoal_tac "m \<noteq> n")
wenzelm@13142
  1255
   prefer 2 apply blast
wenzelm@13142
  1256
  apply (blast dest: lexn_length not_sym)
wenzelm@13142
  1257
  done
wenzelm@13114
  1258
wenzelm@13114
  1259
lemma lexn_conv:
wenzelm@13142
  1260
  "lexn r n =
wenzelm@13142
  1261
    {(xs,ys). length xs = n \<and> length ys = n \<and>
wenzelm@13142
  1262
      (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
wenzelm@13142
  1263
  apply (induct_tac n)
wenzelm@13142
  1264
   apply simp
wenzelm@13142
  1265
   apply blast
wenzelm@13142
  1266
  apply (simp add: image_Collect lex_prod_def)
wenzelm@13142
  1267
  apply auto
wenzelm@13142
  1268
    apply blast
wenzelm@13142
  1269
   apply (rename_tac a xys x xs' y ys')
wenzelm@13142
  1270
   apply (rule_tac x = "a # xys" in exI)
wenzelm@13142
  1271
   apply simp
wenzelm@13142
  1272
  apply (case_tac xys)
wenzelm@13142
  1273
   apply simp_all
wenzelm@13114
  1274
  apply blast
wenzelm@13142
  1275
  done
wenzelm@13114
  1276
wenzelm@13114
  1277
lemma lex_conv:
wenzelm@13142
  1278
  "lex r =
wenzelm@13142
  1279
    {(xs,ys). length xs = length ys \<and>
wenzelm@13142
  1280
      (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
wenzelm@13142
  1281
  by (force simp add: lex_def lexn_conv)
wenzelm@13114
  1282
wenzelm@13142
  1283
lemma wf_lexico [intro!]: "wf r ==> wf (lexico r)"
wenzelm@13142
  1284
  by (unfold lexico_def) blast
wenzelm@13114
  1285
wenzelm@13114
  1286
lemma lexico_conv:
wenzelm@13142
  1287
  "lexico r = {(xs,ys). length xs < length ys |
wenzelm@13142
  1288
      length xs = length ys \<and> (xs, ys) : lex r}"
wenzelm@13142
  1289
  by (simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def)
wenzelm@13114
  1290
wenzelm@13142
  1291
lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
wenzelm@13142
  1292
  by (simp add: lex_conv)
wenzelm@13114
  1293
wenzelm@13142
  1294
lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
wenzelm@13142
  1295
  by (simp add:lex_conv)
wenzelm@13114
  1296
wenzelm@13142
  1297
lemma Cons_in_lex [iff]:
wenzelm@13142
  1298
  "((x # xs, y # ys) : lex r) =
wenzelm@13142
  1299
    ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
wenzelm@13142
  1300
  apply (simp add: lex_conv)
wenzelm@13142
  1301
  apply (rule iffI)
wenzelm@13142
  1302
   prefer 2 apply (blast intro: Cons_eq_appendI)
wenzelm@13142
  1303
  apply clarify
wenzelm@13142
  1304
  apply (case_tac xys)
wenzelm@13142
  1305
   apply simp
wenzelm@13142
  1306
  apply simp
wenzelm@13142
  1307
  apply blast
wenzelm@13142
  1308
  done
wenzelm@13114
  1309
wenzelm@13114
  1310
wenzelm@13142
  1311
subsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
wenzelm@13114
  1312
wenzelm@13142
  1313
lemma sublist_empty [simp]: "sublist xs {} = []"
wenzelm@13142
  1314
  by (auto simp add: sublist_def)
wenzelm@13114
  1315
wenzelm@13142
  1316
lemma sublist_nil [simp]: "sublist [] A = []"
wenzelm@13142
  1317
  by (auto simp add: sublist_def)
wenzelm@13114
  1318
wenzelm@13114
  1319
lemma sublist_shift_lemma:
wenzelm@13142
  1320
  "map fst [p:zip xs [i..i + length xs(] . snd p : A] =
wenzelm@13142
  1321
    map fst [p:zip xs [0..length xs(] . snd p + i : A]"
wenzelm@13142
  1322
  by (induct xs rule: rev_induct) (simp_all add: add_commute)
wenzelm@13114
  1323
wenzelm@13114
  1324
lemma sublist_append:
wenzelm@13142
  1325
    "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
wenzelm@13142
  1326
  apply (unfold sublist_def)
wenzelm@13142
  1327
  apply (induct l' rule: rev_induct)
wenzelm@13142
  1328
   apply simp
wenzelm@13142
  1329
  apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
wenzelm@13142
  1330
  apply (simp add: add_commute)
wenzelm@13142
  1331
  done
wenzelm@13114
  1332
wenzelm@13114
  1333
lemma sublist_Cons:
wenzelm@13142
  1334
    "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
wenzelm@13142
  1335
  apply (induct l rule: rev_induct)
wenzelm@13142
  1336
   apply (simp add: sublist_def)
wenzelm@13142
  1337
  apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
wenzelm@13142
  1338
  done
wenzelm@13114
  1339
wenzelm@13142
  1340
lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
wenzelm@13142
  1341
  by (simp add: sublist_Cons)
wenzelm@13114
  1342
wenzelm@13142
  1343
lemma sublist_upt_eq_take [simp]: "sublist l {..n(} = take n l"
wenzelm@13142
  1344
  apply (induct l rule: rev_induct)
wenzelm@13142
  1345
   apply simp
wenzelm@13142
  1346
  apply (simp split: nat_diff_split add: sublist_append)
wenzelm@13142
  1347
  done
wenzelm@13114
  1348
wenzelm@13114
  1349
wenzelm@13142
  1350
lemma take_Cons':
wenzelm@13142
  1351
    "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
wenzelm@13142
  1352
  by (cases n) simp_all
wenzelm@13114
  1353
wenzelm@13142
  1354
lemma drop_Cons':
wenzelm@13142
  1355
    "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
wenzelm@13142
  1356
  by (cases n) simp_all
wenzelm@13114
  1357
wenzelm@13142
  1358
lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
wenzelm@13142
  1359
  by (cases n) simp_all
wenzelm@13142
  1360
wenzelm@13142
  1361
lemmas [of "number_of v", standard, simp] =
wenzelm@13142
  1362
  take_Cons' drop_Cons' nth_Cons'
nipkow@3507
  1363
wenzelm@13122
  1364
end