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