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