src/HOL/List.ML
author nipkow
Wed Jun 17 10:49:45 1998 +0200 (1998-06-17)
changeset 5043 3fdc881e8ff9
parent 4935 1694e2daef8f
child 5077 71043526295f
permissions -rw-r--r--
goal -> Goal
     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 (** last & butlast **)
   541 
   542 Goal "last(xs@[x]) = x";
   543 by (induct_tac "xs" 1);
   544 by (ALLGOALS Asm_simp_tac);
   545 qed "last_snoc";
   546 Addsimps [last_snoc];
   547 
   548 Goal "butlast(xs@[x]) = xs";
   549 by (induct_tac "xs" 1);
   550 by (ALLGOALS Asm_simp_tac);
   551 qed "butlast_snoc";
   552 Addsimps [butlast_snoc];
   553 
   554 Goal "length(butlast xs) = length xs - 1";
   555 by (res_inst_tac [("xs","xs")] rev_induct 1);
   556 by (ALLGOALS Asm_simp_tac);
   557 qed "length_butlast";
   558 Addsimps [length_butlast];
   559 
   560 Goal
   561   "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
   562 by (induct_tac "xs" 1);
   563 by (ALLGOALS Asm_simp_tac);
   564 qed_spec_mp "butlast_append";
   565 
   566 Goal "x:set(butlast xs) --> x:set xs";
   567 by (induct_tac "xs" 1);
   568 by (ALLGOALS Asm_simp_tac);
   569 qed_spec_mp "in_set_butlastD";
   570 
   571 Goal "x:set(butlast xs) ==> x:set(butlast(xs@ys))";
   572 by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
   573 by (blast_tac (claset() addDs [in_set_butlastD]) 1);
   574 qed "in_set_butlast_appendI1";
   575 
   576 Goal "x:set(butlast ys) ==> x:set(butlast(xs@ys))";
   577 by (asm_simp_tac (simpset() addsimps [butlast_append]) 1);
   578 by (Clarify_tac 1);
   579 by (Full_simp_tac 1);
   580 qed "in_set_butlast_appendI2";
   581 
   582 (** take  & drop **)
   583 section "take & drop";
   584 
   585 Goal "take 0 xs = []";
   586 by (induct_tac "xs" 1);
   587 by (ALLGOALS Asm_simp_tac);
   588 qed "take_0";
   589 
   590 Goal "drop 0 xs = xs";
   591 by (induct_tac "xs" 1);
   592 by (ALLGOALS Asm_simp_tac);
   593 qed "drop_0";
   594 
   595 Goal "take (Suc n) (x#xs) = x # take n xs";
   596 by (Simp_tac 1);
   597 qed "take_Suc_Cons";
   598 
   599 Goal "drop (Suc n) (x#xs) = drop n xs";
   600 by (Simp_tac 1);
   601 qed "drop_Suc_Cons";
   602 
   603 Delsimps [take_Cons,drop_Cons];
   604 Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
   605 
   606 Goal "!xs. length(take n xs) = min (length xs) n";
   607 by (nat_ind_tac "n" 1);
   608  by (ALLGOALS Asm_simp_tac);
   609 by (rtac allI 1);
   610 by (exhaust_tac "xs" 1);
   611  by (ALLGOALS Asm_simp_tac);
   612 qed_spec_mp "length_take";
   613 Addsimps [length_take];
   614 
   615 Goal "!xs. length(drop n xs) = (length xs - n)";
   616 by (nat_ind_tac "n" 1);
   617  by (ALLGOALS Asm_simp_tac);
   618 by (rtac allI 1);
   619 by (exhaust_tac "xs" 1);
   620  by (ALLGOALS Asm_simp_tac);
   621 qed_spec_mp "length_drop";
   622 Addsimps [length_drop];
   623 
   624 Goal "!xs. length xs <= n --> take n xs = xs";
   625 by (nat_ind_tac "n" 1);
   626  by (ALLGOALS Asm_simp_tac);
   627 by (rtac allI 1);
   628 by (exhaust_tac "xs" 1);
   629  by (ALLGOALS Asm_simp_tac);
   630 qed_spec_mp "take_all";
   631 
   632 Goal "!xs. length xs <= n --> drop n xs = []";
   633 by (nat_ind_tac "n" 1);
   634  by (ALLGOALS Asm_simp_tac);
   635 by (rtac allI 1);
   636 by (exhaust_tac "xs" 1);
   637  by (ALLGOALS Asm_simp_tac);
   638 qed_spec_mp "drop_all";
   639 
   640 Goal 
   641   "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
   642 by (nat_ind_tac "n" 1);
   643  by (ALLGOALS Asm_simp_tac);
   644 by (rtac allI 1);
   645 by (exhaust_tac "xs" 1);
   646  by (ALLGOALS Asm_simp_tac);
   647 qed_spec_mp "take_append";
   648 Addsimps [take_append];
   649 
   650 Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
   651 by (nat_ind_tac "n" 1);
   652  by (ALLGOALS Asm_simp_tac);
   653 by (rtac allI 1);
   654 by (exhaust_tac "xs" 1);
   655  by (ALLGOALS Asm_simp_tac);
   656 qed_spec_mp "drop_append";
   657 Addsimps [drop_append];
   658 
   659 Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
   660 by (nat_ind_tac "m" 1);
   661  by (ALLGOALS Asm_simp_tac);
   662 by (rtac allI 1);
   663 by (exhaust_tac "xs" 1);
   664  by (ALLGOALS Asm_simp_tac);
   665 by (rtac allI 1);
   666 by (exhaust_tac "n" 1);
   667  by (ALLGOALS Asm_simp_tac);
   668 qed_spec_mp "take_take";
   669 
   670 Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
   671 by (nat_ind_tac "m" 1);
   672  by (ALLGOALS Asm_simp_tac);
   673 by (rtac allI 1);
   674 by (exhaust_tac "xs" 1);
   675  by (ALLGOALS Asm_simp_tac);
   676 qed_spec_mp "drop_drop";
   677 
   678 Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
   679 by (nat_ind_tac "m" 1);
   680  by (ALLGOALS Asm_simp_tac);
   681 by (rtac allI 1);
   682 by (exhaust_tac "xs" 1);
   683  by (ALLGOALS Asm_simp_tac);
   684 qed_spec_mp "take_drop";
   685 
   686 Goal "!xs. take n (map f xs) = map f (take n xs)"; 
   687 by (nat_ind_tac "n" 1);
   688 by (ALLGOALS Asm_simp_tac);
   689 by (rtac allI 1);
   690 by (exhaust_tac "xs" 1);
   691 by (ALLGOALS Asm_simp_tac);
   692 qed_spec_mp "take_map"; 
   693 
   694 Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
   695 by (nat_ind_tac "n" 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_map";
   701 
   702 Goal "!n i. i < n --> (take n xs)!i = xs!i";
   703 by (induct_tac "xs" 1);
   704  by (ALLGOALS Asm_simp_tac);
   705 by (Clarify_tac 1);
   706 by (exhaust_tac "n" 1);
   707  by (Blast_tac 1);
   708 by (exhaust_tac "i" 1);
   709 by (ALLGOALS Asm_full_simp_tac);
   710 qed_spec_mp "nth_take";
   711 Addsimps [nth_take];
   712 
   713 Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
   714 by (nat_ind_tac "n" 1);
   715  by (ALLGOALS Asm_simp_tac);
   716 by (rtac allI 1);
   717 by (exhaust_tac "xs" 1);
   718  by (ALLGOALS Asm_simp_tac);
   719 qed_spec_mp "nth_drop";
   720 Addsimps [nth_drop];
   721 
   722 (** takeWhile & dropWhile **)
   723 
   724 section "takeWhile & dropWhile";
   725 
   726 Goal "takeWhile P xs @ dropWhile P xs = xs";
   727 by (induct_tac "xs" 1);
   728 by (ALLGOALS Asm_full_simp_tac);
   729 qed "takeWhile_dropWhile_id";
   730 Addsimps [takeWhile_dropWhile_id];
   731 
   732 Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
   733 by (induct_tac "xs" 1);
   734 by (ALLGOALS Asm_full_simp_tac);
   735 by (Blast_tac 1);
   736 bind_thm("takeWhile_append1", conjI RS (result() RS mp));
   737 Addsimps [takeWhile_append1];
   738 
   739 Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
   740 by (induct_tac "xs" 1);
   741 by (ALLGOALS Asm_full_simp_tac);
   742 bind_thm("takeWhile_append2", ballI RS (result() RS mp));
   743 Addsimps [takeWhile_append2];
   744 
   745 Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
   746 by (induct_tac "xs" 1);
   747 by (ALLGOALS Asm_full_simp_tac);
   748 by (Blast_tac 1);
   749 bind_thm("dropWhile_append1", conjI RS (result() RS mp));
   750 Addsimps [dropWhile_append1];
   751 
   752 Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
   753 by (induct_tac "xs" 1);
   754 by (ALLGOALS Asm_full_simp_tac);
   755 bind_thm("dropWhile_append2", ballI RS (result() RS mp));
   756 Addsimps [dropWhile_append2];
   757 
   758 Goal "x:set(takeWhile P xs) --> x:set xs & P x";
   759 by (induct_tac "xs" 1);
   760 by (ALLGOALS Asm_full_simp_tac);
   761 qed_spec_mp"set_take_whileD";
   762 
   763 qed_goal "zip_Nil_Nil"   thy "zip []     []     = []" (K [Simp_tac 1]);
   764 qed_goal "zip_Cons_Cons" thy "zip (x#xs) (y#ys) = (x,y)#zip xs ys" 
   765 						      (K [Simp_tac 1]);
   766 
   767 (** nodups & remdups **)
   768 section "nodups & remdups";
   769 
   770 Goal "set(remdups xs) = set xs";
   771 by (induct_tac "xs" 1);
   772  by (Simp_tac 1);
   773 by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
   774 qed "set_remdups";
   775 Addsimps [set_remdups];
   776 
   777 Goal "nodups(remdups xs)";
   778 by (induct_tac "xs" 1);
   779 by (ALLGOALS Asm_full_simp_tac);
   780 qed "nodups_remdups";
   781 
   782 Goal "nodups xs --> nodups (filter P xs)";
   783 by (induct_tac "xs" 1);
   784 by (ALLGOALS Asm_full_simp_tac);
   785 qed_spec_mp "nodups_filter";
   786 
   787 (** replicate **)
   788 section "replicate";
   789 
   790 Goal "set(replicate (Suc n) x) = {x}";
   791 by (induct_tac "n" 1);
   792 by (ALLGOALS Asm_full_simp_tac);
   793 val lemma = result();
   794 
   795 Goal "n ~= 0 ==> set(replicate n x) = {x}";
   796 by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
   797 qed "set_replicate";
   798 Addsimps [set_replicate];