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