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