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