src/HOL/List.ML
author nipkow
Wed Jun 24 13:59:45 1998 +0200 (1998-06-24)
changeset 5077 71043526295f
parent 5043 3fdc881e8ff9
child 5122 229190f9f303
permissions -rw-r--r--
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
list position. May also be iterated as in xs[i:=a,j:=b,...].
     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 (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 "(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
    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 lists.defs "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 "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 "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 "[| 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 "(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 "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 "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 "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 "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 "(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 "(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 "(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 "(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 "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 "(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 "([] = 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 "(xs @ ys = xs) = (ys=[])";
   145 by (induct_tac "xs" 1);
   146 by (ALLGOALS Asm_simp_tac);
   147 qed "append_self_conv";
   148 
   149 Goal "(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 "!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 "(xs @ ys = xs @ zs) = (ys=zs)";
   173 by (Simp_tac 1);
   174 qed "same_append_eq";
   175 
   176 Goal "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; 
   177 by (Simp_tac 1);
   178 qed "append1_eq_conv";
   179 
   180 Goal "(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 "(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 "(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 "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 "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 "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 "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 "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 (** map **)
   230 
   231 section "map";
   232 
   233 Goal
   234   "(!x. x : set xs --> f x = g x) --> map f xs = map g xs";
   235 by (induct_tac "xs" 1);
   236 by (ALLGOALS Asm_simp_tac);
   237 bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
   238 
   239 Goal "map (%x. x) = (%xs. xs)";
   240 by (rtac ext 1);
   241 by (induct_tac "xs" 1);
   242 by (ALLGOALS Asm_simp_tac);
   243 qed "map_ident";
   244 Addsimps[map_ident];
   245 
   246 Goal "map f (xs@ys) = map f xs @ map f ys";
   247 by (induct_tac "xs" 1);
   248 by (ALLGOALS Asm_simp_tac);
   249 qed "map_append";
   250 Addsimps[map_append];
   251 
   252 Goalw [o_def] "map (f o g) xs = map f (map g xs)";
   253 by (induct_tac "xs" 1);
   254 by (ALLGOALS Asm_simp_tac);
   255 qed "map_compose";
   256 Addsimps[map_compose];
   257 
   258 Goal "rev(map f xs) = map f (rev xs)";
   259 by (induct_tac "xs" 1);
   260 by (ALLGOALS Asm_simp_tac);
   261 qed "rev_map";
   262 
   263 (* a congruence rule for map: *)
   264 Goal
   265  "(xs=ys) --> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
   266 by (rtac impI 1);
   267 by (hyp_subst_tac 1);
   268 by (induct_tac "ys" 1);
   269 by (ALLGOALS Asm_simp_tac);
   270 val lemma = result();
   271 bind_thm("map_cong",impI RSN (2,allI RSN (2,lemma RS mp RS mp)));
   272 
   273 Goal "(map f xs = []) = (xs = [])";
   274 by (induct_tac "xs" 1);
   275 by (ALLGOALS Asm_simp_tac);
   276 qed "map_is_Nil_conv";
   277 AddIffs [map_is_Nil_conv];
   278 
   279 Goal "([] = map f xs) = (xs = [])";
   280 by (induct_tac "xs" 1);
   281 by (ALLGOALS Asm_simp_tac);
   282 qed "Nil_is_map_conv";
   283 AddIffs [Nil_is_map_conv];
   284 
   285 
   286 (** rev **)
   287 
   288 section "rev";
   289 
   290 Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
   291 by (induct_tac "xs" 1);
   292 by (ALLGOALS Asm_simp_tac);
   293 qed "rev_append";
   294 Addsimps[rev_append];
   295 
   296 Goal "rev(rev l) = l";
   297 by (induct_tac "l" 1);
   298 by (ALLGOALS Asm_simp_tac);
   299 qed "rev_rev_ident";
   300 Addsimps[rev_rev_ident];
   301 
   302 Goal "(rev xs = []) = (xs = [])";
   303 by (induct_tac "xs" 1);
   304 by (ALLGOALS Asm_simp_tac);
   305 qed "rev_is_Nil_conv";
   306 AddIffs [rev_is_Nil_conv];
   307 
   308 Goal "([] = rev xs) = (xs = [])";
   309 by (induct_tac "xs" 1);
   310 by (ALLGOALS Asm_simp_tac);
   311 qed "Nil_is_rev_conv";
   312 AddIffs [Nil_is_rev_conv];
   313 
   314 val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
   315 by(stac (rev_rev_ident RS sym) 1);
   316 br(read_instantiate [("P","%xs. ?P(rev xs)")]list.induct)1;
   317 by(ALLGOALS Simp_tac);
   318 brs prems 1;
   319 bes prems 1;
   320 qed "rev_induct";
   321 
   322 Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
   323 by(res_inst_tac [("xs","xs")] rev_induct 1);
   324 by(ALLGOALS Asm_simp_tac);
   325 bind_thm ("rev_exhaust",
   326   impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
   327 
   328 
   329 (** mem **)
   330 
   331 section "mem";
   332 
   333 Goal "x mem (xs@ys) = (x mem xs | x mem ys)";
   334 by (induct_tac "xs" 1);
   335 by (ALLGOALS Asm_simp_tac);
   336 qed "mem_append";
   337 Addsimps[mem_append];
   338 
   339 Goal "x mem [x:xs. P(x)] = (x mem xs & P(x))";
   340 by (induct_tac "xs" 1);
   341 by (ALLGOALS Asm_simp_tac);
   342 qed "mem_filter";
   343 Addsimps[mem_filter];
   344 
   345 (** set **)
   346 
   347 section "set";
   348 
   349 Goal "set (xs@ys) = (set xs Un set ys)";
   350 by (induct_tac "xs" 1);
   351 by (ALLGOALS Asm_simp_tac);
   352 qed "set_append";
   353 Addsimps[set_append];
   354 
   355 Goal "(x mem xs) = (x: set xs)";
   356 by (induct_tac "xs" 1);
   357 by (ALLGOALS Asm_simp_tac);
   358 by (Blast_tac 1);
   359 qed "set_mem_eq";
   360 
   361 Goal "set l <= set (x#l)";
   362 by (Simp_tac 1);
   363 by (Blast_tac 1);
   364 qed "set_subset_Cons";
   365 
   366 Goal "(set xs = {}) = (xs = [])";
   367 by (induct_tac "xs" 1);
   368 by (ALLGOALS Asm_simp_tac);
   369 qed "set_empty";
   370 Addsimps [set_empty];
   371 
   372 Goal "set(rev xs) = set(xs)";
   373 by (induct_tac "xs" 1);
   374 by (ALLGOALS Asm_simp_tac);
   375 qed "set_rev";
   376 Addsimps [set_rev];
   377 
   378 Goal "set(map f xs) = f``(set xs)";
   379 by (induct_tac "xs" 1);
   380 by (ALLGOALS Asm_simp_tac);
   381 qed "set_map";
   382 Addsimps [set_map];
   383 
   384 Goal "set(map f xs) = f``(set xs)";
   385 by (induct_tac "xs" 1);
   386 by (ALLGOALS Asm_simp_tac);
   387 qed "set_map";
   388 Addsimps [set_map];
   389 
   390 Goal "(x : set(filter P xs)) = (x : set xs & P x)";
   391 by (induct_tac "xs" 1);
   392 by (ALLGOALS Asm_simp_tac);
   393 by(Blast_tac 1);
   394 qed "in_set_filter";
   395 Addsimps [in_set_filter];
   396 
   397 
   398 (** list_all **)
   399 
   400 section "list_all";
   401 
   402 Goal "list_all (%x. True) xs = True";
   403 by (induct_tac "xs" 1);
   404 by (ALLGOALS Asm_simp_tac);
   405 qed "list_all_True";
   406 Addsimps [list_all_True];
   407 
   408 Goal "list_all p (xs@ys) = (list_all p xs & list_all p ys)";
   409 by (induct_tac "xs" 1);
   410 by (ALLGOALS Asm_simp_tac);
   411 qed "list_all_append";
   412 Addsimps [list_all_append];
   413 
   414 Goal "list_all P xs = (!x. x mem xs --> P(x))";
   415 by (induct_tac "xs" 1);
   416 by (ALLGOALS Asm_simp_tac);
   417 by (Blast_tac 1);
   418 qed "list_all_mem_conv";
   419 
   420 
   421 (** filter **)
   422 
   423 section "filter";
   424 
   425 Goal "filter P (xs@ys) = filter P xs @ filter P ys";
   426 by (induct_tac "xs" 1);
   427 by (ALLGOALS Asm_simp_tac);
   428 qed "filter_append";
   429 Addsimps [filter_append];
   430 
   431 Goal "filter (%x. True) xs = xs";
   432 by (induct_tac "xs" 1);
   433 by (ALLGOALS Asm_simp_tac);
   434 qed "filter_True";
   435 Addsimps [filter_True];
   436 
   437 Goal "filter (%x. False) xs = []";
   438 by (induct_tac "xs" 1);
   439 by (ALLGOALS Asm_simp_tac);
   440 qed "filter_False";
   441 Addsimps [filter_False];
   442 
   443 Goal "length (filter P xs) <= length xs";
   444 by (induct_tac "xs" 1);
   445 by (ALLGOALS Asm_simp_tac);
   446 qed "length_filter";
   447 
   448 
   449 (** concat **)
   450 
   451 section "concat";
   452 
   453 Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
   454 by (induct_tac "xs" 1);
   455 by (ALLGOALS Asm_simp_tac);
   456 qed"concat_append";
   457 Addsimps [concat_append];
   458 
   459 Goal "(concat xss = []) = (!xs:set xss. xs=[])";
   460 by (induct_tac "xss" 1);
   461 by (ALLGOALS Asm_simp_tac);
   462 qed "concat_eq_Nil_conv";
   463 AddIffs [concat_eq_Nil_conv];
   464 
   465 Goal "([] = concat xss) = (!xs:set xss. xs=[])";
   466 by (induct_tac "xss" 1);
   467 by (ALLGOALS Asm_simp_tac);
   468 qed "Nil_eq_concat_conv";
   469 AddIffs [Nil_eq_concat_conv];
   470 
   471 Goal  "set(concat xs) = Union(set `` set xs)";
   472 by (induct_tac "xs" 1);
   473 by (ALLGOALS Asm_simp_tac);
   474 qed"set_concat";
   475 Addsimps [set_concat];
   476 
   477 Goal "map f (concat xs) = concat (map (map f) xs)"; 
   478 by (induct_tac "xs" 1);
   479 by (ALLGOALS Asm_simp_tac);
   480 qed "map_concat";
   481 
   482 Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
   483 by (induct_tac "xs" 1);
   484 by (ALLGOALS Asm_simp_tac);
   485 qed"filter_concat"; 
   486 
   487 Goal "rev(concat xs) = concat (map rev (rev xs))";
   488 by (induct_tac "xs" 1);
   489 by (ALLGOALS Asm_simp_tac);
   490 qed "rev_concat";
   491 
   492 (** nth **)
   493 
   494 section "nth";
   495 
   496 Goal
   497   "!xs. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
   498 by (nat_ind_tac "n" 1);
   499  by (Asm_simp_tac 1);
   500  by (rtac allI 1);
   501  by (exhaust_tac "xs" 1);
   502   by (ALLGOALS Asm_simp_tac);
   503 qed_spec_mp "nth_append";
   504 
   505 Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
   506 by (induct_tac "xs" 1);
   507 (* case [] *)
   508 by (Asm_full_simp_tac 1);
   509 (* case x#xl *)
   510 by (rtac allI 1);
   511 by (nat_ind_tac "n" 1);
   512 by (ALLGOALS Asm_full_simp_tac);
   513 qed_spec_mp "nth_map";
   514 Addsimps [nth_map];
   515 
   516 Goal "!n. n < length xs --> list_all P xs --> P(xs!n)";
   517 by (induct_tac "xs" 1);
   518 (* case [] *)
   519 by (Simp_tac 1);
   520 (* case x#xl *)
   521 by (rtac allI 1);
   522 by (nat_ind_tac "n" 1);
   523 by (ALLGOALS Asm_full_simp_tac);
   524 qed_spec_mp "list_all_nth";
   525 
   526 Goal "!n. n < length xs --> xs!n mem xs";
   527 by (induct_tac "xs" 1);
   528 (* case [] *)
   529 by (Simp_tac 1);
   530 (* case x#xl *)
   531 by (rtac allI 1);
   532 by (nat_ind_tac "n" 1);
   533 (* case 0 *)
   534 by (Asm_full_simp_tac 1);
   535 (* case Suc x *)
   536 by (Asm_full_simp_tac 1);
   537 qed_spec_mp "nth_mem";
   538 Addsimps [nth_mem];
   539 
   540 (** list update **)
   541 
   542 section "list update";
   543 
   544 Goal "!i. length(xs[i:=x]) = length xs";
   545 by (induct_tac "xs" 1);
   546 by (Simp_tac 1);
   547 by (asm_full_simp_tac (simpset() addsplits [split_nat_case]) 1);
   548 qed_spec_mp "length_list_update";
   549 Addsimps [length_list_update];
   550 
   551 
   552 (** last & butlast **)
   553 
   554 Goal "last(xs@[x]) = x";
   555 by (induct_tac "xs" 1);
   556 by (ALLGOALS Asm_simp_tac);
   557 qed "last_snoc";
   558 Addsimps [last_snoc];
   559 
   560 Goal "butlast(xs@[x]) = xs";
   561 by (induct_tac "xs" 1);
   562 by (ALLGOALS Asm_simp_tac);
   563 qed "butlast_snoc";
   564 Addsimps [butlast_snoc];
   565 
   566 Goal "length(butlast xs) = length xs - 1";
   567 by (res_inst_tac [("xs","xs")] rev_induct 1);
   568 by (ALLGOALS Asm_simp_tac);
   569 qed "length_butlast";
   570 Addsimps [length_butlast];
   571 
   572 Goal
   573   "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
   574 by (induct_tac "xs" 1);
   575 by (ALLGOALS Asm_simp_tac);
   576 qed_spec_mp "butlast_append";
   577 
   578 Goal "x:set(butlast xs) --> x:set xs";
   579 by (induct_tac "xs" 1);
   580 by (ALLGOALS Asm_simp_tac);
   581 qed_spec_mp "in_set_butlastD";
   582 
   583 Goal "x:set(butlast xs) ==> x:set(butlast(xs@ys))";
   584 by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
   585 by (blast_tac (claset() addDs [in_set_butlastD]) 1);
   586 qed "in_set_butlast_appendI1";
   587 
   588 Goal "x:set(butlast ys) ==> x:set(butlast(xs@ys))";
   589 by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
   590 by (Clarify_tac 1);
   591 by (Full_simp_tac 1);
   592 qed "in_set_butlast_appendI2";
   593 
   594 (** take  & drop **)
   595 section "take & drop";
   596 
   597 Goal "take 0 xs = []";
   598 by (induct_tac "xs" 1);
   599 by (ALLGOALS Asm_simp_tac);
   600 qed "take_0";
   601 
   602 Goal "drop 0 xs = xs";
   603 by (induct_tac "xs" 1);
   604 by (ALLGOALS Asm_simp_tac);
   605 qed "drop_0";
   606 
   607 Goal "take (Suc n) (x#xs) = x # take n xs";
   608 by (Simp_tac 1);
   609 qed "take_Suc_Cons";
   610 
   611 Goal "drop (Suc n) (x#xs) = drop n xs";
   612 by (Simp_tac 1);
   613 qed "drop_Suc_Cons";
   614 
   615 Delsimps [take_Cons,drop_Cons];
   616 Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
   617 
   618 Goal "!xs. length(take n xs) = min (length xs) n";
   619 by (nat_ind_tac "n" 1);
   620  by (ALLGOALS Asm_simp_tac);
   621 by (rtac allI 1);
   622 by (exhaust_tac "xs" 1);
   623  by (ALLGOALS Asm_simp_tac);
   624 qed_spec_mp "length_take";
   625 Addsimps [length_take];
   626 
   627 Goal "!xs. length(drop n xs) = (length xs - n)";
   628 by (nat_ind_tac "n" 1);
   629  by (ALLGOALS Asm_simp_tac);
   630 by (rtac allI 1);
   631 by (exhaust_tac "xs" 1);
   632  by (ALLGOALS Asm_simp_tac);
   633 qed_spec_mp "length_drop";
   634 Addsimps [length_drop];
   635 
   636 Goal "!xs. length xs <= n --> take n xs = xs";
   637 by (nat_ind_tac "n" 1);
   638  by (ALLGOALS Asm_simp_tac);
   639 by (rtac allI 1);
   640 by (exhaust_tac "xs" 1);
   641  by (ALLGOALS Asm_simp_tac);
   642 qed_spec_mp "take_all";
   643 
   644 Goal "!xs. length xs <= n --> drop n xs = []";
   645 by (nat_ind_tac "n" 1);
   646  by (ALLGOALS Asm_simp_tac);
   647 by (rtac allI 1);
   648 by (exhaust_tac "xs" 1);
   649  by (ALLGOALS Asm_simp_tac);
   650 qed_spec_mp "drop_all";
   651 
   652 Goal 
   653   "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
   654 by (nat_ind_tac "n" 1);
   655  by (ALLGOALS Asm_simp_tac);
   656 by (rtac allI 1);
   657 by (exhaust_tac "xs" 1);
   658  by (ALLGOALS Asm_simp_tac);
   659 qed_spec_mp "take_append";
   660 Addsimps [take_append];
   661 
   662 Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
   663 by (nat_ind_tac "n" 1);
   664  by (ALLGOALS Asm_simp_tac);
   665 by (rtac allI 1);
   666 by (exhaust_tac "xs" 1);
   667  by (ALLGOALS Asm_simp_tac);
   668 qed_spec_mp "drop_append";
   669 Addsimps [drop_append];
   670 
   671 Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
   672 by (nat_ind_tac "m" 1);
   673  by (ALLGOALS Asm_simp_tac);
   674 by (rtac allI 1);
   675 by (exhaust_tac "xs" 1);
   676  by (ALLGOALS Asm_simp_tac);
   677 by (rtac allI 1);
   678 by (exhaust_tac "n" 1);
   679  by (ALLGOALS Asm_simp_tac);
   680 qed_spec_mp "take_take";
   681 
   682 Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
   683 by (nat_ind_tac "m" 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 "drop_drop";
   689 
   690 Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
   691 by (nat_ind_tac "m" 1);
   692  by (ALLGOALS Asm_simp_tac);
   693 by (rtac allI 1);
   694 by (exhaust_tac "xs" 1);
   695  by (ALLGOALS Asm_simp_tac);
   696 qed_spec_mp "take_drop";
   697 
   698 Goal "!xs. take n (map f xs) = map f (take n xs)"; 
   699 by (nat_ind_tac "n" 1);
   700 by (ALLGOALS Asm_simp_tac);
   701 by (rtac allI 1);
   702 by (exhaust_tac "xs" 1);
   703 by (ALLGOALS Asm_simp_tac);
   704 qed_spec_mp "take_map"; 
   705 
   706 Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
   707 by (nat_ind_tac "n" 1);
   708 by (ALLGOALS Asm_simp_tac);
   709 by (rtac allI 1);
   710 by (exhaust_tac "xs" 1);
   711 by (ALLGOALS Asm_simp_tac);
   712 qed_spec_mp "drop_map";
   713 
   714 Goal "!n i. i < n --> (take n xs)!i = xs!i";
   715 by (induct_tac "xs" 1);
   716  by (ALLGOALS Asm_simp_tac);
   717 by (Clarify_tac 1);
   718 by (exhaust_tac "n" 1);
   719  by (Blast_tac 1);
   720 by (exhaust_tac "i" 1);
   721 by (ALLGOALS Asm_full_simp_tac);
   722 qed_spec_mp "nth_take";
   723 Addsimps [nth_take];
   724 
   725 Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
   726 by (nat_ind_tac "n" 1);
   727  by (ALLGOALS Asm_simp_tac);
   728 by (rtac allI 1);
   729 by (exhaust_tac "xs" 1);
   730  by (ALLGOALS Asm_simp_tac);
   731 qed_spec_mp "nth_drop";
   732 Addsimps [nth_drop];
   733 
   734 (** takeWhile & dropWhile **)
   735 
   736 section "takeWhile & dropWhile";
   737 
   738 Goal "takeWhile P xs @ dropWhile P xs = xs";
   739 by (induct_tac "xs" 1);
   740 by (ALLGOALS Asm_full_simp_tac);
   741 qed "takeWhile_dropWhile_id";
   742 Addsimps [takeWhile_dropWhile_id];
   743 
   744 Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
   745 by (induct_tac "xs" 1);
   746 by (ALLGOALS Asm_full_simp_tac);
   747 by (Blast_tac 1);
   748 bind_thm("takeWhile_append1", conjI RS (result() RS mp));
   749 Addsimps [takeWhile_append1];
   750 
   751 Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
   752 by (induct_tac "xs" 1);
   753 by (ALLGOALS Asm_full_simp_tac);
   754 bind_thm("takeWhile_append2", ballI RS (result() RS mp));
   755 Addsimps [takeWhile_append2];
   756 
   757 Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
   758 by (induct_tac "xs" 1);
   759 by (ALLGOALS Asm_full_simp_tac);
   760 by (Blast_tac 1);
   761 bind_thm("dropWhile_append1", conjI RS (result() RS mp));
   762 Addsimps [dropWhile_append1];
   763 
   764 Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
   765 by (induct_tac "xs" 1);
   766 by (ALLGOALS Asm_full_simp_tac);
   767 bind_thm("dropWhile_append2", ballI RS (result() RS mp));
   768 Addsimps [dropWhile_append2];
   769 
   770 Goal "x:set(takeWhile P xs) --> x:set xs & P x";
   771 by (induct_tac "xs" 1);
   772 by (ALLGOALS Asm_full_simp_tac);
   773 qed_spec_mp"set_take_whileD";
   774 
   775 qed_goal "zip_Nil_Nil"   thy "zip []     []     = []" (K [Simp_tac 1]);
   776 qed_goal "zip_Cons_Cons" thy "zip (x#xs) (y#ys) = (x,y)#zip xs ys" 
   777 						      (K [Simp_tac 1]);
   778 
   779 (** nodups & remdups **)
   780 section "nodups & remdups";
   781 
   782 Goal "set(remdups xs) = set xs";
   783 by (induct_tac "xs" 1);
   784  by (Simp_tac 1);
   785 by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
   786 qed "set_remdups";
   787 Addsimps [set_remdups];
   788 
   789 Goal "nodups(remdups xs)";
   790 by (induct_tac "xs" 1);
   791 by (ALLGOALS Asm_full_simp_tac);
   792 qed "nodups_remdups";
   793 
   794 Goal "nodups xs --> nodups (filter P xs)";
   795 by (induct_tac "xs" 1);
   796 by (ALLGOALS Asm_full_simp_tac);
   797 qed_spec_mp "nodups_filter";
   798 
   799 (** replicate **)
   800 section "replicate";
   801 
   802 Goal "set(replicate (Suc n) x) = {x}";
   803 by (induct_tac "n" 1);
   804 by (ALLGOALS Asm_full_simp_tac);
   805 val lemma = result();
   806 
   807 Goal "n ~= 0 ==> set(replicate n x) = {x}";
   808 by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
   809 qed "set_replicate";
   810 Addsimps [set_replicate];