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