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