src/HOL/List.ML
author nipkow
Thu Aug 06 12:46:18 1998 +0200 (1998-08-06)
changeset 5272 95cfd872fe66
parent 5200 a23c23af335f
child 5278 a903b66822e2
permissions -rw-r--r--
New lemmas in List and Lambda in IsaMakefile
     1 (*  Title:      HOL/List
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   1994 TU Muenchen
     5 
     6 List lemmas
     7 *)
     8 
     9 Goal "!x. xs ~= x#xs";
    10 by (induct_tac "xs" 1);
    11 by (Auto_tac);
    12 qed_spec_mp "not_Cons_self";
    13 bind_thm("not_Cons_self2",not_Cons_self RS not_sym);
    14 Addsimps [not_Cons_self,not_Cons_self2];
    15 
    16 Goal "(xs ~= []) = (? y ys. xs = y#ys)";
    17 by (induct_tac "xs" 1);
    18 by (Auto_tac);
    19 qed "neq_Nil_conv";
    20 
    21 (* Induction over the length of a list: *)
    22 val [prem] = Goal
    23   "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)";
    24 by (rtac measure_induct 1 THEN etac prem 1);
    25 qed "length_induct";
    26 
    27 
    28 (** "lists": the list-forming operator over sets **)
    29 
    30 Goalw lists.defs "A<=B ==> lists A <= lists B";
    31 by (rtac lfp_mono 1);
    32 by (REPEAT (ares_tac basic_monos 1));
    33 qed "lists_mono";
    34 
    35 val listsE = lists.mk_cases list.simps  "x#l : lists A";
    36 AddSEs [listsE];
    37 AddSIs lists.intrs;
    38 
    39 Goal "l: lists A ==> l: lists B --> l: lists (A Int B)";
    40 by (etac lists.induct 1);
    41 by (ALLGOALS Blast_tac);
    42 qed_spec_mp "lists_IntI";
    43 
    44 Goal "lists (A Int B) = lists A Int lists B";
    45 by (rtac (mono_Int RS equalityI) 1);
    46 by (simp_tac (simpset() addsimps [mono_def, lists_mono]) 1);
    47 by (blast_tac (claset() addSIs [lists_IntI]) 1);
    48 qed "lists_Int_eq";
    49 Addsimps [lists_Int_eq];
    50 
    51 
    52 (**  Case analysis **)
    53 section "Case analysis";
    54 
    55 val prems = Goal "[| P([]); !!x xs. P(x#xs) |] ==> P(xs)";
    56 by (induct_tac "xs" 1);
    57 by (REPEAT(resolve_tac prems 1));
    58 qed "list_cases";
    59 
    60 Goal "(xs=[] --> P([])) & (!y ys. xs=y#ys --> P(y#ys)) --> P(xs)";
    61 by (induct_tac "xs" 1);
    62 by (Blast_tac 1);
    63 by (Blast_tac 1);
    64 bind_thm("list_eq_cases",
    65   impI RSN (2,allI RSN (2,allI RSN (2,impI RS (conjI RS (result() RS mp))))));
    66 
    67 (** length **)
    68 (* needs to come before "@" because of thm append_eq_append_conv *)
    69 
    70 section "length";
    71 
    72 Goal "length(xs@ys) = length(xs)+length(ys)";
    73 by (induct_tac "xs" 1);
    74 by (Auto_tac);
    75 qed"length_append";
    76 Addsimps [length_append];
    77 
    78 Goal "length (map f xs) = length xs";
    79 by (induct_tac "xs" 1);
    80 by (Auto_tac);
    81 qed "length_map";
    82 Addsimps [length_map];
    83 
    84 Goal "length(rev xs) = length(xs)";
    85 by (induct_tac "xs" 1);
    86 by (Auto_tac);
    87 qed "length_rev";
    88 Addsimps [length_rev];
    89 
    90 Goal "xs ~= [] ==> length(tl xs) = (length xs) - 1";
    91 by (exhaust_tac "xs" 1);
    92 by (Auto_tac);
    93 qed "length_tl";
    94 Addsimps [length_tl];
    95 
    96 Goal "(length xs = 0) = (xs = [])";
    97 by (induct_tac "xs" 1);
    98 by (Auto_tac);
    99 qed "length_0_conv";
   100 AddIffs [length_0_conv];
   101 
   102 Goal "(0 = length xs) = (xs = [])";
   103 by (induct_tac "xs" 1);
   104 by (Auto_tac);
   105 qed "zero_length_conv";
   106 AddIffs [zero_length_conv];
   107 
   108 Goal "(0 < length xs) = (xs ~= [])";
   109 by (induct_tac "xs" 1);
   110 by (Auto_tac);
   111 qed "length_greater_0_conv";
   112 AddIffs [length_greater_0_conv];
   113 
   114 (** @ - append **)
   115 
   116 section "@ - append";
   117 
   118 Goal "(xs@ys)@zs = xs@(ys@zs)";
   119 by (induct_tac "xs" 1);
   120 by (Auto_tac);
   121 qed "append_assoc";
   122 Addsimps [append_assoc];
   123 
   124 Goal "xs @ [] = xs";
   125 by (induct_tac "xs" 1);
   126 by (Auto_tac);
   127 qed "append_Nil2";
   128 Addsimps [append_Nil2];
   129 
   130 Goal "(xs@ys = []) = (xs=[] & ys=[])";
   131 by (induct_tac "xs" 1);
   132 by (Auto_tac);
   133 qed "append_is_Nil_conv";
   134 AddIffs [append_is_Nil_conv];
   135 
   136 Goal "([] = xs@ys) = (xs=[] & ys=[])";
   137 by (induct_tac "xs" 1);
   138 by (Auto_tac);
   139 qed "Nil_is_append_conv";
   140 AddIffs [Nil_is_append_conv];
   141 
   142 Goal "(xs @ ys = xs) = (ys=[])";
   143 by (induct_tac "xs" 1);
   144 by (Auto_tac);
   145 qed "append_self_conv";
   146 
   147 Goal "(xs = xs @ ys) = (ys=[])";
   148 by (induct_tac "xs" 1);
   149 by (Auto_tac);
   150 qed "self_append_conv";
   151 AddIffs [append_self_conv,self_append_conv];
   152 
   153 Goal "!ys. length xs = length ys | length us = length vs \
   154 \              --> (xs@us = ys@vs) = (xs=ys & us=vs)";
   155 by (induct_tac "xs" 1);
   156  by (rtac allI 1);
   157  by (exhaust_tac "ys" 1);
   158   by (Asm_simp_tac 1);
   159  by (fast_tac (claset() addIs [less_add_Suc2] addss simpset()
   160                       addEs [less_not_refl2 RSN (2,rev_notE)]) 1);
   161 by (rtac allI 1);
   162 by (exhaust_tac "ys" 1);
   163  by (fast_tac (claset() addIs [less_add_Suc2] addss simpset()
   164                       addEs [(less_not_refl2 RS not_sym) RSN (2,rev_notE)]) 1);
   165 by (Asm_simp_tac 1);
   166 qed_spec_mp "append_eq_append_conv";
   167 Addsimps [append_eq_append_conv];
   168 
   169 Goal "(xs @ ys = xs @ zs) = (ys=zs)";
   170 by (Simp_tac 1);
   171 qed "same_append_eq";
   172 
   173 Goal "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; 
   174 by (Simp_tac 1);
   175 qed "append1_eq_conv";
   176 
   177 Goal "(ys @ xs = zs @ xs) = (ys=zs)";
   178 by (Simp_tac 1);
   179 qed "append_same_eq";
   180 
   181 AddSIs
   182  [same_append_eq RS iffD2, append1_eq_conv RS iffD2, append_same_eq RS iffD2];
   183 AddSDs
   184  [same_append_eq RS iffD1, append1_eq_conv RS iffD1, append_same_eq RS iffD1];
   185 
   186 Goal "(xs @ ys = ys) = (xs=[])";
   187 by (cut_inst_tac [("zs","[]")] append_same_eq 1);
   188 by (Auto_tac);
   189 qed "append_self_conv2";
   190 
   191 Goal "(ys = xs @ ys) = (xs=[])";
   192 by (simp_tac (simpset() addsimps
   193      [simplify (simpset()) (read_instantiate[("ys","[]")]append_same_eq)]) 1);
   194 by (Blast_tac 1);
   195 qed "self_append_conv2";
   196 AddIffs [append_self_conv2,self_append_conv2];
   197 
   198 Goal "xs ~= [] --> hd xs # tl xs = xs";
   199 by (induct_tac "xs" 1);
   200 by (Auto_tac);
   201 qed_spec_mp "hd_Cons_tl";
   202 Addsimps [hd_Cons_tl];
   203 
   204 Goal "hd(xs@ys) = (if xs=[] then hd ys else hd xs)";
   205 by (induct_tac "xs" 1);
   206 by (Auto_tac);
   207 qed "hd_append";
   208 
   209 Goal "xs ~= [] ==> hd(xs @ ys) = hd xs";
   210 by (asm_simp_tac (simpset() addsimps [hd_append]
   211                            addsplits [list.split]) 1);
   212 qed "hd_append2";
   213 Addsimps [hd_append2];
   214 
   215 Goal "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)";
   216 by (simp_tac (simpset() addsplits [list.split]) 1);
   217 qed "tl_append";
   218 
   219 Goal "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys";
   220 by (asm_simp_tac (simpset() addsimps [tl_append]
   221                            addsplits [list.split]) 1);
   222 qed "tl_append2";
   223 Addsimps [tl_append2];
   224 
   225 (* trivial rules for solving @-equations automatically *)
   226 
   227 Goal "xs = ys ==> xs = [] @ ys";
   228 by(Asm_simp_tac 1);
   229 qed "eq_Nil_appendI";
   230 
   231 Goal "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs";
   232 bd sym 1;
   233 by(Asm_simp_tac 1);
   234 qed "Cons_eq_appendI";
   235 
   236 Goal "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us";
   237 bd sym 1;
   238 by(Asm_simp_tac 1);
   239 qed "append_eq_appendI";
   240 
   241 
   242 (** map **)
   243 
   244 section "map";
   245 
   246 Goal
   247   "(!x. x : set xs --> f x = g x) --> map f xs = map g xs";
   248 by (induct_tac "xs" 1);
   249 by (Auto_tac);
   250 bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
   251 
   252 Goal "map (%x. x) = (%xs. xs)";
   253 by (rtac ext 1);
   254 by (induct_tac "xs" 1);
   255 by (Auto_tac);
   256 qed "map_ident";
   257 Addsimps[map_ident];
   258 
   259 Goal "map f (xs@ys) = map f xs @ map f ys";
   260 by (induct_tac "xs" 1);
   261 by (Auto_tac);
   262 qed "map_append";
   263 Addsimps[map_append];
   264 
   265 Goalw [o_def] "map (f o g) xs = map f (map g xs)";
   266 by (induct_tac "xs" 1);
   267 by (Auto_tac);
   268 qed "map_compose";
   269 Addsimps[map_compose];
   270 
   271 Goal "rev(map f xs) = map f (rev xs)";
   272 by (induct_tac "xs" 1);
   273 by (Auto_tac);
   274 qed "rev_map";
   275 
   276 (* a congruence rule for map: *)
   277 Goal
   278  "(xs=ys) --> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
   279 by (rtac impI 1);
   280 by (hyp_subst_tac 1);
   281 by (induct_tac "ys" 1);
   282 by (Auto_tac);
   283 val lemma = result();
   284 bind_thm("map_cong",impI RSN (2,allI RSN (2,lemma RS mp RS mp)));
   285 
   286 Goal "(map f xs = []) = (xs = [])";
   287 by (induct_tac "xs" 1);
   288 by (Auto_tac);
   289 qed "map_is_Nil_conv";
   290 AddIffs [map_is_Nil_conv];
   291 
   292 Goal "([] = map f xs) = (xs = [])";
   293 by (induct_tac "xs" 1);
   294 by (Auto_tac);
   295 qed "Nil_is_map_conv";
   296 AddIffs [Nil_is_map_conv];
   297 
   298 
   299 (** rev **)
   300 
   301 section "rev";
   302 
   303 Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
   304 by (induct_tac "xs" 1);
   305 by (Auto_tac);
   306 qed "rev_append";
   307 Addsimps[rev_append];
   308 
   309 Goal "rev(rev l) = l";
   310 by (induct_tac "l" 1);
   311 by (Auto_tac);
   312 qed "rev_rev_ident";
   313 Addsimps[rev_rev_ident];
   314 
   315 Goal "(rev xs = []) = (xs = [])";
   316 by (induct_tac "xs" 1);
   317 by (Auto_tac);
   318 qed "rev_is_Nil_conv";
   319 AddIffs [rev_is_Nil_conv];
   320 
   321 Goal "([] = rev xs) = (xs = [])";
   322 by (induct_tac "xs" 1);
   323 by (Auto_tac);
   324 qed "Nil_is_rev_conv";
   325 AddIffs [Nil_is_rev_conv];
   326 
   327 val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
   328 by (stac (rev_rev_ident RS sym) 1);
   329 br(read_instantiate [("P","%xs. ?P(rev xs)")]list.induct)1;
   330 by (ALLGOALS Simp_tac);
   331 by (resolve_tac prems 1);
   332 by (eresolve_tac prems 1);
   333 qed "rev_induct";
   334 
   335 fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
   336 
   337 Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
   338 by (res_inst_tac [("xs","xs")] rev_induct 1);
   339 by (Auto_tac);
   340 bind_thm ("rev_exhaust",
   341   impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
   342 
   343 
   344 (** mem **)
   345 
   346 section "mem";
   347 
   348 Goal "x mem (xs@ys) = (x mem xs | x mem ys)";
   349 by (induct_tac "xs" 1);
   350 by (Auto_tac);
   351 qed "mem_append";
   352 Addsimps[mem_append];
   353 
   354 Goal "x mem [x:xs. P(x)] = (x mem xs & P(x))";
   355 by (induct_tac "xs" 1);
   356 by (Auto_tac);
   357 qed "mem_filter";
   358 Addsimps[mem_filter];
   359 
   360 (** set **)
   361 
   362 section "set";
   363 
   364 Goal "set (xs@ys) = (set xs Un set ys)";
   365 by (induct_tac "xs" 1);
   366 by (Auto_tac);
   367 qed "set_append";
   368 Addsimps[set_append];
   369 
   370 Goal "(x mem xs) = (x: set xs)";
   371 by (induct_tac "xs" 1);
   372 by (Auto_tac);
   373 qed "set_mem_eq";
   374 
   375 Goal "set l <= set (x#l)";
   376 by (Auto_tac);
   377 qed "set_subset_Cons";
   378 
   379 Goal "(set xs = {}) = (xs = [])";
   380 by (induct_tac "xs" 1);
   381 by (Auto_tac);
   382 qed "set_empty";
   383 Addsimps [set_empty];
   384 
   385 Goal "set(rev xs) = set(xs)";
   386 by (induct_tac "xs" 1);
   387 by (Auto_tac);
   388 qed "set_rev";
   389 Addsimps [set_rev];
   390 
   391 Goal "set(map f xs) = f``(set xs)";
   392 by (induct_tac "xs" 1);
   393 by (Auto_tac);
   394 qed "set_map";
   395 Addsimps [set_map];
   396 
   397 Goal "(x : set(filter P xs)) = (x : set xs & P x)";
   398 by (induct_tac "xs" 1);
   399 by (Auto_tac);
   400 qed "in_set_filter";
   401 Addsimps [in_set_filter];
   402 
   403 Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
   404 by(induct_tac "xs" 1);
   405  by(Simp_tac 1);
   406 by(Asm_simp_tac 1);
   407 br iffI 1;
   408 by(blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
   409 by(REPEAT(etac exE 1));
   410 by(exhaust_tac "ys" 1);
   411 by(Auto_tac);
   412 qed "in_set_conv_decomp";
   413 
   414 (* eliminate `lists' in favour of `set' *)
   415 
   416 Goal "(xs : lists A) = (!x : set xs. x : A)";
   417 by(induct_tac "xs" 1);
   418 by(Auto_tac);
   419 qed "in_lists_conv_set";
   420 
   421 bind_thm("in_listsD",in_lists_conv_set RS iffD1);
   422 AddSDs [in_listsD];
   423 bind_thm("in_listsI",in_lists_conv_set RS iffD2);
   424 AddSIs [in_listsI];
   425 
   426 (** list_all **)
   427 
   428 section "list_all";
   429 
   430 Goal "list_all (%x. True) xs = True";
   431 by (induct_tac "xs" 1);
   432 by (Auto_tac);
   433 qed "list_all_True";
   434 Addsimps [list_all_True];
   435 
   436 Goal "list_all p (xs@ys) = (list_all p xs & list_all p ys)";
   437 by (induct_tac "xs" 1);
   438 by (Auto_tac);
   439 qed "list_all_append";
   440 Addsimps [list_all_append];
   441 
   442 Goal "list_all P xs = (!x. x mem xs --> P(x))";
   443 by (induct_tac "xs" 1);
   444 by (Auto_tac);
   445 qed "list_all_mem_conv";
   446 
   447 
   448 (** filter **)
   449 
   450 section "filter";
   451 
   452 Goal "filter P (xs@ys) = filter P xs @ filter P ys";
   453 by (induct_tac "xs" 1);
   454 by (Auto_tac);
   455 qed "filter_append";
   456 Addsimps [filter_append];
   457 
   458 Goal "filter (%x. True) xs = xs";
   459 by (induct_tac "xs" 1);
   460 by (Auto_tac);
   461 qed "filter_True";
   462 Addsimps [filter_True];
   463 
   464 Goal "filter (%x. False) xs = []";
   465 by (induct_tac "xs" 1);
   466 by (Auto_tac);
   467 qed "filter_False";
   468 Addsimps [filter_False];
   469 
   470 Goal "length (filter P xs) <= length xs";
   471 by (induct_tac "xs" 1);
   472 by (Auto_tac);
   473 qed "length_filter";
   474 
   475 
   476 (** concat **)
   477 
   478 section "concat";
   479 
   480 Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
   481 by (induct_tac "xs" 1);
   482 by (Auto_tac);
   483 qed"concat_append";
   484 Addsimps [concat_append];
   485 
   486 Goal "(concat xss = []) = (!xs:set xss. xs=[])";
   487 by (induct_tac "xss" 1);
   488 by (Auto_tac);
   489 qed "concat_eq_Nil_conv";
   490 AddIffs [concat_eq_Nil_conv];
   491 
   492 Goal "([] = concat xss) = (!xs:set xss. xs=[])";
   493 by (induct_tac "xss" 1);
   494 by (Auto_tac);
   495 qed "Nil_eq_concat_conv";
   496 AddIffs [Nil_eq_concat_conv];
   497 
   498 Goal  "set(concat xs) = Union(set `` set xs)";
   499 by (induct_tac "xs" 1);
   500 by (Auto_tac);
   501 qed"set_concat";
   502 Addsimps [set_concat];
   503 
   504 Goal "map f (concat xs) = concat (map (map f) xs)"; 
   505 by (induct_tac "xs" 1);
   506 by (Auto_tac);
   507 qed "map_concat";
   508 
   509 Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
   510 by (induct_tac "xs" 1);
   511 by (Auto_tac);
   512 qed"filter_concat"; 
   513 
   514 Goal "rev(concat xs) = concat (map rev (rev xs))";
   515 by (induct_tac "xs" 1);
   516 by (Auto_tac);
   517 qed "rev_concat";
   518 
   519 (** nth **)
   520 
   521 section "nth";
   522 
   523 Goal
   524   "!xs. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
   525 by (induct_tac "n" 1);
   526  by (Asm_simp_tac 1);
   527  by (rtac allI 1);
   528  by (exhaust_tac "xs" 1);
   529   by (Auto_tac);
   530 qed_spec_mp "nth_append";
   531 
   532 Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
   533 by (induct_tac "xs" 1);
   534 (* case [] *)
   535 by (Asm_full_simp_tac 1);
   536 (* case x#xl *)
   537 by (rtac allI 1);
   538 by (induct_tac "n" 1);
   539 by (Auto_tac);
   540 qed_spec_mp "nth_map";
   541 Addsimps [nth_map];
   542 
   543 Goal "!n. n < length xs --> list_all P xs --> P(xs!n)";
   544 by (induct_tac "xs" 1);
   545 (* case [] *)
   546 by (Simp_tac 1);
   547 (* case x#xl *)
   548 by (rtac allI 1);
   549 by (induct_tac "n" 1);
   550 by (Auto_tac);
   551 qed_spec_mp "list_all_nth";
   552 
   553 Goal "!n. n < length xs --> xs!n mem xs";
   554 by (induct_tac "xs" 1);
   555 (* case [] *)
   556 by (Simp_tac 1);
   557 (* case x#xl *)
   558 by (rtac allI 1);
   559 by (induct_tac "n" 1);
   560 (* case 0 *)
   561 by (Asm_full_simp_tac 1);
   562 (* case Suc x *)
   563 by (Asm_full_simp_tac 1);
   564 qed_spec_mp "nth_mem";
   565 Addsimps [nth_mem];
   566 
   567 (** list update **)
   568 
   569 section "list update";
   570 
   571 Goal "!i. length(xs[i:=x]) = length xs";
   572 by (induct_tac "xs" 1);
   573 by (Simp_tac 1);
   574 by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
   575 qed_spec_mp "length_list_update";
   576 Addsimps [length_list_update];
   577 
   578 
   579 (** last & butlast **)
   580 
   581 Goal "last(xs@[x]) = x";
   582 by (induct_tac "xs" 1);
   583 by (Auto_tac);
   584 qed "last_snoc";
   585 Addsimps [last_snoc];
   586 
   587 Goal "butlast(xs@[x]) = xs";
   588 by (induct_tac "xs" 1);
   589 by (Auto_tac);
   590 qed "butlast_snoc";
   591 Addsimps [butlast_snoc];
   592 
   593 Goal "length(butlast xs) = length xs - 1";
   594 by (res_inst_tac [("xs","xs")] rev_induct 1);
   595 by (Auto_tac);
   596 qed "length_butlast";
   597 Addsimps [length_butlast];
   598 
   599 Goal
   600   "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
   601 by (induct_tac "xs" 1);
   602 by (Auto_tac);
   603 qed_spec_mp "butlast_append";
   604 
   605 Goal "x:set(butlast xs) --> x:set xs";
   606 by (induct_tac "xs" 1);
   607 by (Auto_tac);
   608 qed_spec_mp "in_set_butlastD";
   609 
   610 Goal "x:set(butlast xs) ==> x:set(butlast(xs@ys))";
   611 by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
   612 by (blast_tac (claset() addDs [in_set_butlastD]) 1);
   613 qed "in_set_butlast_appendI1";
   614 
   615 Goal "x:set(butlast ys) ==> x:set(butlast(xs@ys))";
   616 by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
   617 by (Clarify_tac 1);
   618 by (Full_simp_tac 1);
   619 qed "in_set_butlast_appendI2";
   620 
   621 (** take  & drop **)
   622 section "take & drop";
   623 
   624 Goal "take 0 xs = []";
   625 by (induct_tac "xs" 1);
   626 by (Auto_tac);
   627 qed "take_0";
   628 
   629 Goal "drop 0 xs = xs";
   630 by (induct_tac "xs" 1);
   631 by (Auto_tac);
   632 qed "drop_0";
   633 
   634 Goal "take (Suc n) (x#xs) = x # take n xs";
   635 by (Simp_tac 1);
   636 qed "take_Suc_Cons";
   637 
   638 Goal "drop (Suc n) (x#xs) = drop n xs";
   639 by (Simp_tac 1);
   640 qed "drop_Suc_Cons";
   641 
   642 Delsimps [take_Cons,drop_Cons];
   643 Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
   644 
   645 Goal "!xs. length(take n xs) = min (length xs) n";
   646 by (induct_tac "n" 1);
   647  by (Auto_tac);
   648 by (exhaust_tac "xs" 1);
   649  by (Auto_tac);
   650 qed_spec_mp "length_take";
   651 Addsimps [length_take];
   652 
   653 Goal "!xs. length(drop n xs) = (length xs - n)";
   654 by (induct_tac "n" 1);
   655  by (Auto_tac);
   656 by (exhaust_tac "xs" 1);
   657  by (Auto_tac);
   658 qed_spec_mp "length_drop";
   659 Addsimps [length_drop];
   660 
   661 Goal "!xs. length xs <= n --> take n xs = xs";
   662 by (induct_tac "n" 1);
   663  by (Auto_tac);
   664 by (exhaust_tac "xs" 1);
   665  by (Auto_tac);
   666 qed_spec_mp "take_all";
   667 
   668 Goal "!xs. length xs <= n --> drop n xs = []";
   669 by (induct_tac "n" 1);
   670  by (Auto_tac);
   671 by (exhaust_tac "xs" 1);
   672  by (Auto_tac);
   673 qed_spec_mp "drop_all";
   674 
   675 Goal 
   676   "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
   677 by (induct_tac "n" 1);
   678  by (Auto_tac);
   679 by (exhaust_tac "xs" 1);
   680  by (Auto_tac);
   681 qed_spec_mp "take_append";
   682 Addsimps [take_append];
   683 
   684 Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
   685 by (induct_tac "n" 1);
   686  by (Auto_tac);
   687 by (exhaust_tac "xs" 1);
   688  by (Auto_tac);
   689 qed_spec_mp "drop_append";
   690 Addsimps [drop_append];
   691 
   692 Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
   693 by (induct_tac "m" 1);
   694  by (Auto_tac);
   695 by (exhaust_tac "xs" 1);
   696  by (Auto_tac);
   697 by (exhaust_tac "na" 1);
   698  by (Auto_tac);
   699 qed_spec_mp "take_take";
   700 
   701 Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
   702 by (induct_tac "m" 1);
   703  by (Auto_tac);
   704 by (exhaust_tac "xs" 1);
   705  by (Auto_tac);
   706 qed_spec_mp "drop_drop";
   707 
   708 Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
   709 by (induct_tac "m" 1);
   710  by (Auto_tac);
   711 by (exhaust_tac "xs" 1);
   712  by (Auto_tac);
   713 qed_spec_mp "take_drop";
   714 
   715 Goal "!xs. take n (map f xs) = map f (take n xs)"; 
   716 by (induct_tac "n" 1);
   717  by (Auto_tac);
   718 by (exhaust_tac "xs" 1);
   719  by (Auto_tac);
   720 qed_spec_mp "take_map"; 
   721 
   722 Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
   723 by (induct_tac "n" 1);
   724  by (Auto_tac);
   725 by (exhaust_tac "xs" 1);
   726  by (Auto_tac);
   727 qed_spec_mp "drop_map";
   728 
   729 Goal "!n i. i < n --> (take n xs)!i = xs!i";
   730 by (induct_tac "xs" 1);
   731  by (Auto_tac);
   732 by (exhaust_tac "n" 1);
   733  by (Blast_tac 1);
   734 by (exhaust_tac "i" 1);
   735  by (Auto_tac);
   736 qed_spec_mp "nth_take";
   737 Addsimps [nth_take];
   738 
   739 Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
   740 by (induct_tac "n" 1);
   741  by (Auto_tac);
   742 by (exhaust_tac "xs" 1);
   743  by (Auto_tac);
   744 qed_spec_mp "nth_drop";
   745 Addsimps [nth_drop];
   746 
   747 (** takeWhile & dropWhile **)
   748 
   749 section "takeWhile & dropWhile";
   750 
   751 Goal "takeWhile P xs @ dropWhile P xs = xs";
   752 by (induct_tac "xs" 1);
   753 by (Auto_tac);
   754 qed "takeWhile_dropWhile_id";
   755 Addsimps [takeWhile_dropWhile_id];
   756 
   757 Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
   758 by (induct_tac "xs" 1);
   759 by (Auto_tac);
   760 bind_thm("takeWhile_append1", conjI RS (result() RS mp));
   761 Addsimps [takeWhile_append1];
   762 
   763 Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
   764 by (induct_tac "xs" 1);
   765 by (Auto_tac);
   766 bind_thm("takeWhile_append2", ballI RS (result() RS mp));
   767 Addsimps [takeWhile_append2];
   768 
   769 Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
   770 by (induct_tac "xs" 1);
   771 by (Auto_tac);
   772 bind_thm("dropWhile_append1", conjI RS (result() RS mp));
   773 Addsimps [dropWhile_append1];
   774 
   775 Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
   776 by (induct_tac "xs" 1);
   777 by (Auto_tac);
   778 bind_thm("dropWhile_append2", ballI RS (result() RS mp));
   779 Addsimps [dropWhile_append2];
   780 
   781 Goal "x:set(takeWhile P xs) --> x:set xs & P x";
   782 by (induct_tac "xs" 1);
   783 by (Auto_tac);
   784 qed_spec_mp"set_take_whileD";
   785 
   786 qed_goal "zip_Nil_Nil"   thy "zip []     []     = []" (K [Simp_tac 1]);
   787 qed_goal "zip_Cons_Cons" thy "zip (x#xs) (y#ys) = (x,y)#zip xs ys" 
   788 						      (K [Simp_tac 1]);
   789 
   790 
   791 (** foldl **)
   792 section "foldl";
   793 
   794 Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
   795 by(induct_tac "xs" 1);
   796 by(Auto_tac);
   797 qed_spec_mp "foldl_append";
   798 Addsimps [foldl_append];
   799 
   800 (* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
   801    because it requires an additional transitivity step
   802 *)
   803 Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
   804 by(induct_tac "ns" 1);
   805  by(Simp_tac 1);
   806 by(Asm_full_simp_tac 1);
   807 by(blast_tac (claset() addIs [trans_le_add1]) 1);
   808 qed_spec_mp "start_le_sum";
   809 
   810 Goal "n : set ns ==> n <= foldl op+ 0 ns";
   811 by(auto_tac (claset() addIs [start_le_sum],
   812              simpset() addsimps [in_set_conv_decomp]));
   813 qed "elem_le_sum";
   814 
   815 Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
   816 by(induct_tac "ns" 1);
   817 by(Auto_tac);
   818 qed_spec_mp "sum_eq_0_conv";
   819 AddIffs [sum_eq_0_conv];
   820 
   821 
   822 (** nodups & remdups **)
   823 section "nodups & remdups";
   824 
   825 Goal "set(remdups xs) = set xs";
   826 by (induct_tac "xs" 1);
   827  by (Simp_tac 1);
   828 by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
   829 qed "set_remdups";
   830 Addsimps [set_remdups];
   831 
   832 Goal "nodups(remdups xs)";
   833 by (induct_tac "xs" 1);
   834 by (Auto_tac);
   835 qed "nodups_remdups";
   836 
   837 Goal "nodups xs --> nodups (filter P xs)";
   838 by (induct_tac "xs" 1);
   839 by (Auto_tac);
   840 qed_spec_mp "nodups_filter";
   841 
   842 (** replicate **)
   843 section "replicate";
   844 
   845 Goal "set(replicate (Suc n) x) = {x}";
   846 by (induct_tac "n" 1);
   847 by (Auto_tac);
   848 val lemma = result();
   849 
   850 Goal "n ~= 0 ==> set(replicate n x) = {x}";
   851 by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
   852 qed "set_replicate";
   853 Addsimps [set_replicate];
   854 
   855 
   856 (***
   857 Simplification procedure for all list equalities.
   858 Currently only tries to rearranges @ to see if
   859 - both lists end in a singleton list,
   860 - or both lists end in the same list.
   861 ***)
   862 local
   863 
   864 val list_eq_pattern =
   865   read_cterm (sign_of List.thy) ("(xs::'a list) = ys",HOLogic.boolT);
   866 
   867 fun last (cons as Const("List.list.op #",_) $ _ $ xs) =
   868       (case xs of Const("List.list.[]",_) => cons | _ => last xs)
   869   | last (Const("List.op @",_) $ _ $ ys) = last ys
   870   | last t = t;
   871 
   872 fun list1 (Const("List.list.op #",_) $ _ $ Const("List.list.[]",_)) = true
   873   | list1 _ = false;
   874 
   875 fun butlast ((cons as Const("List.list.op #",_) $ x) $ xs) =
   876       (case xs of Const("List.list.[]",_) => xs | _ => cons $ butlast xs)
   877   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
   878   | butlast xs = Const("List.list.[]",fastype_of xs);
   879 
   880 val rearr_tac =
   881   simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons]);
   882 
   883 fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
   884   let
   885     val lastl = last lhs and lastr = last rhs
   886     fun rearr conv =
   887       let val lhs1 = butlast lhs and rhs1 = butlast rhs
   888           val Type(_,listT::_) = eqT
   889           val appT = [listT,listT] ---> listT
   890           val app = Const("List.op @",appT)
   891           val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
   892           val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
   893           val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
   894             handle ERROR =>
   895             error("The error(s) above occurred while trying to prove " ^
   896                   string_of_cterm ct)
   897       in Some((conv RS (thm RS trans)) RS eq_reflection) end
   898 
   899   in if list1 lastl andalso list1 lastr
   900      then rearr append1_eq_conv
   901      else
   902      if lastl aconv lastr
   903      then rearr append_same_eq
   904      else None
   905   end;
   906 in
   907 val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq;
   908 end;
   909 
   910 Addsimprocs [list_eq_simproc];