src/HOL/List.ML
author paulson
Mon Dec 13 10:54:04 1999 +0100 (1999-12-13)
changeset 8064 357652a08ee0
parent 8009 29a7a79ee7f4
child 8115 c802042066e8
permissions -rw-r--r--
expandshort
     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 "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.Cons",_) $ _ $ xs) =
   257       (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
   258   | last (Const("List.op @",_) $ _ $ ys) = last ys
   259   | last t = t;
   260 
   261 fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
   262   | list1 _ = false;
   263 
   264 fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
   265       (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
   266   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
   267   | butlast xs = Const("List.list.Nil",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 (exhaust_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 (exhaust_tac "xs" 1);
   350 by Auto_tac;
   351 qed "Nil_is_map_conv";
   352 AddIffs [Nil_is_map_conv];
   353 
   354 Goal "(map f xs = y#ys) = (? x xs'. xs = x#xs' & f x = y & map f xs' = ys)";
   355 by (exhaust_tac "xs" 1);
   356 by (ALLGOALS Asm_simp_tac);
   357 qed "map_eq_Cons";
   358 
   359 Goal "!xs. map f xs = map f ys --> (!x y. f x = f y --> x=y) --> xs=ys";
   360 by (induct_tac "ys" 1);
   361  by (Asm_simp_tac 1);
   362 by (fast_tac (claset() addss (simpset() addsimps [map_eq_Cons])) 1);
   363 qed_spec_mp "map_injective";
   364 
   365 Goal "inj f ==> inj (map f)";
   366 by (blast_tac (claset() addDs [map_injective,injD] addIs [injI]) 1);
   367 qed "inj_mapI";
   368 
   369 Goalw [inj_on_def] "inj (map f) ==> inj f";
   370 by (Clarify_tac 1);
   371 by (eres_inst_tac [("x","[x]")] ballE 1);
   372  by (eres_inst_tac [("x","[y]")] ballE 1);
   373   by (Asm_full_simp_tac 1);
   374  by (Blast_tac 1);
   375 by (Blast_tac 1);
   376 qed "inj_mapD";
   377 
   378 Goal "inj (map f) = inj f";
   379 by (blast_tac (claset() addDs [inj_mapD] addIs [inj_mapI]) 1);
   380 qed "inj_map";
   381 
   382 (** rev **)
   383 
   384 section "rev";
   385 
   386 Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
   387 by (induct_tac "xs" 1);
   388 by Auto_tac;
   389 qed "rev_append";
   390 Addsimps[rev_append];
   391 
   392 Goal "rev(rev l) = l";
   393 by (induct_tac "l" 1);
   394 by Auto_tac;
   395 qed "rev_rev_ident";
   396 Addsimps[rev_rev_ident];
   397 
   398 Goal "(rev xs = []) = (xs = [])";
   399 by (induct_tac "xs" 1);
   400 by Auto_tac;
   401 qed "rev_is_Nil_conv";
   402 AddIffs [rev_is_Nil_conv];
   403 
   404 Goal "([] = rev xs) = (xs = [])";
   405 by (induct_tac "xs" 1);
   406 by Auto_tac;
   407 qed "Nil_is_rev_conv";
   408 AddIffs [Nil_is_rev_conv];
   409 
   410 Goal "!ys. (rev xs = rev ys) = (xs = ys)";
   411 by (induct_tac "xs" 1);
   412  by (Force_tac 1);
   413 by (rtac allI 1);
   414 by (exhaust_tac "ys" 1);
   415  by (Asm_simp_tac 1);
   416 by (Force_tac 1);
   417 qed_spec_mp "rev_is_rev_conv";
   418 AddIffs [rev_is_rev_conv];
   419 
   420 val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
   421 by (stac (rev_rev_ident RS sym) 1);
   422 by (res_inst_tac [("list", "rev xs")] list.induct 1);
   423 by (ALLGOALS Simp_tac);
   424 by (resolve_tac prems 1);
   425 by (eresolve_tac prems 1);
   426 qed "rev_induct";
   427 
   428 fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
   429 
   430 Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
   431 by (res_inst_tac [("xs","xs")] rev_induct 1);
   432 by Auto_tac;
   433 bind_thm ("rev_exhaust",
   434   impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
   435 
   436 
   437 (** set **)
   438 
   439 section "set";
   440 
   441 Goal "finite (set xs)";
   442 by (induct_tac "xs" 1);
   443 by Auto_tac;
   444 qed "finite_set";
   445 AddIffs [finite_set];
   446 
   447 Goal "set (xs@ys) = (set xs Un set ys)";
   448 by (induct_tac "xs" 1);
   449 by Auto_tac;
   450 qed "set_append";
   451 Addsimps[set_append];
   452 
   453 Goal "set l <= set (x#l)";
   454 by Auto_tac;
   455 qed "set_subset_Cons";
   456 
   457 Goal "(set xs = {}) = (xs = [])";
   458 by (induct_tac "xs" 1);
   459 by Auto_tac;
   460 qed "set_empty";
   461 Addsimps [set_empty];
   462 
   463 Goal "set(rev xs) = set(xs)";
   464 by (induct_tac "xs" 1);
   465 by Auto_tac;
   466 qed "set_rev";
   467 Addsimps [set_rev];
   468 
   469 Goal "set(map f xs) = f``(set xs)";
   470 by (induct_tac "xs" 1);
   471 by Auto_tac;
   472 qed "set_map";
   473 Addsimps [set_map];
   474 
   475 Goal "set(filter P xs) = {x. x : set xs & P x}";
   476 by (induct_tac "xs" 1);
   477 by Auto_tac;
   478 qed "set_filter";
   479 Addsimps [set_filter];
   480 
   481 Goal "set[i..j(] = {k. i <= k & k < j}";
   482 by (induct_tac "j" 1);
   483 by Auto_tac;
   484 by (arith_tac 1);
   485 qed "set_upt";
   486 Addsimps [set_upt];
   487 
   488 Goal "!i < size xs. set(xs[i := x]) <= insert x (set xs)";
   489 by (induct_tac "xs" 1);
   490  by (Simp_tac 1);
   491 by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
   492 by (Blast_tac 1);
   493 qed_spec_mp "set_list_update_subset";
   494 
   495 Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
   496 by (induct_tac "xs" 1);
   497  by (Simp_tac 1);
   498 by (Asm_simp_tac 1);
   499 by (rtac iffI 1);
   500 by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
   501 by (REPEAT(etac exE 1));
   502 by (exhaust_tac "ys" 1);
   503 by Auto_tac;
   504 qed "in_set_conv_decomp";
   505 
   506 
   507 (* eliminate `lists' in favour of `set' *)
   508 
   509 Goal "(xs : lists A) = (!x : set xs. x : A)";
   510 by (induct_tac "xs" 1);
   511 by Auto_tac;
   512 qed "in_lists_conv_set";
   513 
   514 bind_thm("in_listsD",in_lists_conv_set RS iffD1);
   515 AddSDs [in_listsD];
   516 bind_thm("in_listsI",in_lists_conv_set RS iffD2);
   517 AddSIs [in_listsI];
   518 
   519 (** mem **)
   520  
   521 section "mem";
   522 
   523 Goal "(x mem xs) = (x: set xs)";
   524 by (induct_tac "xs" 1);
   525 by Auto_tac;
   526 qed "set_mem_eq";
   527 
   528 
   529 (** list_all **)
   530 
   531 section "list_all";
   532 
   533 Goal "list_all P xs = (!x:set xs. P x)";
   534 by (induct_tac "xs" 1);
   535 by Auto_tac;
   536 qed "list_all_conv";
   537 
   538 Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)";
   539 by (induct_tac "xs" 1);
   540 by Auto_tac;
   541 qed "list_all_append";
   542 Addsimps [list_all_append];
   543 
   544 
   545 (** filter **)
   546 
   547 section "filter";
   548 
   549 Goal "filter P (xs@ys) = filter P xs @ filter P ys";
   550 by (induct_tac "xs" 1);
   551 by Auto_tac;
   552 qed "filter_append";
   553 Addsimps [filter_append];
   554 
   555 Goal "filter (%x. True) xs = xs";
   556 by (induct_tac "xs" 1);
   557 by Auto_tac;
   558 qed "filter_True";
   559 Addsimps [filter_True];
   560 
   561 Goal "filter (%x. False) xs = []";
   562 by (induct_tac "xs" 1);
   563 by Auto_tac;
   564 qed "filter_False";
   565 Addsimps [filter_False];
   566 
   567 Goal "length (filter P xs) <= length xs";
   568 by (induct_tac "xs" 1);
   569 by Auto_tac;
   570 qed "length_filter";
   571 Addsimps[length_filter];
   572 
   573 Goal "set (filter P xs) <= set xs";
   574 by Auto_tac;
   575 qed "filter_is_subset";
   576 Addsimps [filter_is_subset];
   577 
   578 
   579 section "concat";
   580 
   581 Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
   582 by (induct_tac "xs" 1);
   583 by Auto_tac;
   584 qed"concat_append";
   585 Addsimps [concat_append];
   586 
   587 Goal "(concat xss = []) = (!xs:set xss. xs=[])";
   588 by (induct_tac "xss" 1);
   589 by Auto_tac;
   590 qed "concat_eq_Nil_conv";
   591 AddIffs [concat_eq_Nil_conv];
   592 
   593 Goal "([] = concat xss) = (!xs:set xss. xs=[])";
   594 by (induct_tac "xss" 1);
   595 by Auto_tac;
   596 qed "Nil_eq_concat_conv";
   597 AddIffs [Nil_eq_concat_conv];
   598 
   599 Goal  "set(concat xs) = Union(set `` set xs)";
   600 by (induct_tac "xs" 1);
   601 by Auto_tac;
   602 qed"set_concat";
   603 Addsimps [set_concat];
   604 
   605 Goal "map f (concat xs) = concat (map (map f) xs)"; 
   606 by (induct_tac "xs" 1);
   607 by Auto_tac;
   608 qed "map_concat";
   609 
   610 Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
   611 by (induct_tac "xs" 1);
   612 by Auto_tac;
   613 qed"filter_concat"; 
   614 
   615 Goal "rev(concat xs) = concat (map rev (rev xs))";
   616 by (induct_tac "xs" 1);
   617 by Auto_tac;
   618 qed "rev_concat";
   619 
   620 (** nth **)
   621 
   622 section "nth";
   623 
   624 Goal "(x#xs)!0 = x";
   625 by Auto_tac;
   626 qed "nth_Cons_0";
   627 Addsimps [nth_Cons_0];
   628 
   629 Goal "(x#xs)!(Suc n) = xs!n";
   630 by Auto_tac;
   631 qed "nth_Cons_Suc";
   632 Addsimps [nth_Cons_Suc];
   633 
   634 Delsimps (thms "nth.simps");
   635 
   636 Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
   637 by (induct_tac "xs" 1);
   638  by (Asm_simp_tac 1);
   639  by (rtac allI 1);
   640  by (exhaust_tac "n" 1);
   641   by Auto_tac;
   642 qed_spec_mp "nth_append";
   643 
   644 Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
   645 by (induct_tac "xs" 1);
   646 (* case [] *)
   647 by (Asm_full_simp_tac 1);
   648 (* case x#xl *)
   649 by (rtac allI 1);
   650 by (induct_tac "n" 1);
   651 by Auto_tac;
   652 qed_spec_mp "nth_map";
   653 Addsimps [nth_map];
   654 
   655 Goal "!n. n < length xs --> Ball (set xs) P --> P(xs!n)";
   656 by (induct_tac "xs" 1);
   657 (* case [] *)
   658 by (Simp_tac 1);
   659 (* case x#xl *)
   660 by (rtac allI 1);
   661 by (induct_tac "n" 1);
   662 by Auto_tac;
   663 qed_spec_mp "list_ball_nth";
   664 
   665 Goal "!n. n < length xs --> xs!n : set xs";
   666 by (induct_tac "xs" 1);
   667  by (Simp_tac 1);
   668 by (rtac allI 1);
   669 by (induct_tac "n" 1);
   670  by (Asm_full_simp_tac 1);
   671 by (Asm_full_simp_tac 1);
   672 qed_spec_mp "nth_mem";
   673 Addsimps [nth_mem];
   674 
   675 
   676 Goal "(!i. i < length xs --> P(xs!i)) --> (!x : set xs. P x)";
   677 by (induct_tac "xs" 1);
   678  by (Asm_full_simp_tac 1);
   679 by (simp_tac (simpset() addsplits [nat.split] addsimps [nth_Cons]) 1);
   680 by (fast_tac (claset() addss simpset()) 1);
   681 qed_spec_mp "all_nth_imp_all_set";
   682 
   683 Goal "(!x : set xs. P x) = (!i. i<length xs --> P (xs ! i))";
   684 by (rtac iffI 1);
   685  by (Asm_full_simp_tac 1);
   686 by (etac all_nth_imp_all_set 1);
   687 qed_spec_mp "all_set_conv_all_nth";
   688 
   689 
   690 (** list update **)
   691 
   692 section "list update";
   693 
   694 Goal "!i. length(xs[i:=x]) = length xs";
   695 by (induct_tac "xs" 1);
   696 by (Simp_tac 1);
   697 by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
   698 qed_spec_mp "length_list_update";
   699 Addsimps [length_list_update];
   700 
   701 Goal "!i j. i < length xs  --> (xs[i:=x])!j = (if i=j then x else xs!j)";
   702 by (induct_tac "xs" 1);
   703  by (Simp_tac 1);
   704 by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
   705 qed_spec_mp "nth_list_update";
   706 
   707 Goal "!i. i < size xs --> xs[i:=x, i:=y] = xs[i:=y]";
   708 by (induct_tac "xs" 1);
   709  by (Simp_tac 1);
   710 by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
   711 qed_spec_mp "list_update_overwrite";
   712 Addsimps [list_update_overwrite];
   713 
   714 Goal "!i < length xs. (xs[i := x] = xs) = (xs!i = x)";
   715 by (induct_tac "xs" 1);
   716  by (Simp_tac 1);
   717 by (simp_tac (simpset() addsplits [nat.split]) 1);
   718 by (Blast_tac 1);
   719 qed_spec_mp "list_update_same_conv";
   720 
   721 Goal "!i xy xs. length xs = length ys --> \
   722 \     (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])";
   723 by (induct_tac "ys" 1);
   724  by Auto_tac;
   725 by (exhaust_tac "xs" 1);
   726  by (auto_tac (claset(), simpset() addsplits [nat.split]));
   727 qed_spec_mp "update_zip";
   728 
   729 Goal "!i. set(xs[i:=x]) <= insert x (set xs)";
   730 by (induct_tac "xs" 1);
   731  by (asm_full_simp_tac (simpset() addsimps []) 1);
   732 by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
   733 by (Fast_tac  1);
   734 qed_spec_mp "set_update_subset";
   735 
   736 
   737 (** last & butlast **)
   738 
   739 section "last / butlast";
   740 
   741 Goal "last(xs@[x]) = x";
   742 by (induct_tac "xs" 1);
   743 by Auto_tac;
   744 qed "last_snoc";
   745 Addsimps [last_snoc];
   746 
   747 Goal "butlast(xs@[x]) = xs";
   748 by (induct_tac "xs" 1);
   749 by Auto_tac;
   750 qed "butlast_snoc";
   751 Addsimps [butlast_snoc];
   752 
   753 Goal "length(butlast xs) = length xs - 1";
   754 by (res_inst_tac [("xs","xs")] rev_induct 1);
   755 by Auto_tac;
   756 qed "length_butlast";
   757 Addsimps [length_butlast];
   758 
   759 Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
   760 by (induct_tac "xs" 1);
   761 by Auto_tac;
   762 qed_spec_mp "butlast_append";
   763 
   764 Goal "x:set(butlast xs) --> x:set xs";
   765 by (induct_tac "xs" 1);
   766 by Auto_tac;
   767 qed_spec_mp "in_set_butlastD";
   768 
   769 Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))";
   770 by (auto_tac (claset() addDs [in_set_butlastD],
   771 	      simpset() addsimps [butlast_append]));
   772 qed "in_set_butlast_appendI";
   773 
   774 (** take  & drop **)
   775 section "take & drop";
   776 
   777 Goal "take 0 xs = []";
   778 by (induct_tac "xs" 1);
   779 by Auto_tac;
   780 qed "take_0";
   781 
   782 Goal "drop 0 xs = xs";
   783 by (induct_tac "xs" 1);
   784 by Auto_tac;
   785 qed "drop_0";
   786 
   787 Goal "take (Suc n) (x#xs) = x # take n xs";
   788 by (Simp_tac 1);
   789 qed "take_Suc_Cons";
   790 
   791 Goal "drop (Suc n) (x#xs) = drop n xs";
   792 by (Simp_tac 1);
   793 qed "drop_Suc_Cons";
   794 
   795 Delsimps [take_Cons,drop_Cons];
   796 Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
   797 
   798 Goal "!xs. length(take n xs) = min (length xs) n";
   799 by (induct_tac "n" 1);
   800  by Auto_tac;
   801 by (exhaust_tac "xs" 1);
   802  by Auto_tac;
   803 qed_spec_mp "length_take";
   804 Addsimps [length_take];
   805 
   806 Goal "!xs. length(drop n xs) = (length xs - n)";
   807 by (induct_tac "n" 1);
   808  by Auto_tac;
   809 by (exhaust_tac "xs" 1);
   810  by Auto_tac;
   811 qed_spec_mp "length_drop";
   812 Addsimps [length_drop];
   813 
   814 Goal "!xs. length xs <= n --> take n xs = xs";
   815 by (induct_tac "n" 1);
   816  by Auto_tac;
   817 by (exhaust_tac "xs" 1);
   818  by Auto_tac;
   819 qed_spec_mp "take_all";
   820 Addsimps [take_all];
   821 
   822 Goal "!xs. length xs <= n --> drop n xs = []";
   823 by (induct_tac "n" 1);
   824  by Auto_tac;
   825 by (exhaust_tac "xs" 1);
   826  by Auto_tac;
   827 qed_spec_mp "drop_all";
   828 Addsimps [drop_all];
   829 
   830 Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
   831 by (induct_tac "n" 1);
   832  by Auto_tac;
   833 by (exhaust_tac "xs" 1);
   834  by Auto_tac;
   835 qed_spec_mp "take_append";
   836 Addsimps [take_append];
   837 
   838 Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
   839 by (induct_tac "n" 1);
   840  by Auto_tac;
   841 by (exhaust_tac "xs" 1);
   842  by Auto_tac;
   843 qed_spec_mp "drop_append";
   844 Addsimps [drop_append];
   845 
   846 Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
   847 by (induct_tac "m" 1);
   848  by Auto_tac;
   849 by (exhaust_tac "xs" 1);
   850  by Auto_tac;
   851 by (exhaust_tac "na" 1);
   852  by Auto_tac;
   853 qed_spec_mp "take_take";
   854 Addsimps [take_take];
   855 
   856 Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
   857 by (induct_tac "m" 1);
   858  by Auto_tac;
   859 by (exhaust_tac "xs" 1);
   860  by Auto_tac;
   861 qed_spec_mp "drop_drop";
   862 Addsimps [drop_drop];
   863 
   864 Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
   865 by (induct_tac "m" 1);
   866  by Auto_tac;
   867 by (exhaust_tac "xs" 1);
   868  by Auto_tac;
   869 qed_spec_mp "take_drop";
   870 
   871 Goal "!xs. take n xs @ drop n xs = xs";
   872 by (induct_tac "n" 1);
   873  by Auto_tac;
   874 by (exhaust_tac "xs" 1);
   875  by Auto_tac;
   876 qed_spec_mp "append_take_drop_id";
   877 
   878 Goal "!xs. take n (map f xs) = map f (take n xs)"; 
   879 by (induct_tac "n" 1);
   880  by Auto_tac;
   881 by (exhaust_tac "xs" 1);
   882  by Auto_tac;
   883 qed_spec_mp "take_map"; 
   884 
   885 Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
   886 by (induct_tac "n" 1);
   887  by Auto_tac;
   888 by (exhaust_tac "xs" 1);
   889  by Auto_tac;
   890 qed_spec_mp "drop_map";
   891 
   892 Goal "!n i. i < n --> (take n xs)!i = xs!i";
   893 by (induct_tac "xs" 1);
   894  by Auto_tac;
   895 by (exhaust_tac "n" 1);
   896  by (Blast_tac 1);
   897 by (exhaust_tac "i" 1);
   898  by Auto_tac;
   899 qed_spec_mp "nth_take";
   900 Addsimps [nth_take];
   901 
   902 Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
   903 by (induct_tac "n" 1);
   904  by Auto_tac;
   905 by (exhaust_tac "xs" 1);
   906  by Auto_tac;
   907 qed_spec_mp "nth_drop";
   908 Addsimps [nth_drop];
   909 
   910 (** takeWhile & dropWhile **)
   911 
   912 section "takeWhile & dropWhile";
   913 
   914 Goal "takeWhile P xs @ dropWhile P xs = xs";
   915 by (induct_tac "xs" 1);
   916 by Auto_tac;
   917 qed "takeWhile_dropWhile_id";
   918 Addsimps [takeWhile_dropWhile_id];
   919 
   920 Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
   921 by (induct_tac "xs" 1);
   922 by Auto_tac;
   923 bind_thm("takeWhile_append1", conjI RS (result() RS mp));
   924 Addsimps [takeWhile_append1];
   925 
   926 Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
   927 by (induct_tac "xs" 1);
   928 by Auto_tac;
   929 bind_thm("takeWhile_append2", ballI RS (result() RS mp));
   930 Addsimps [takeWhile_append2];
   931 
   932 Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
   933 by (induct_tac "xs" 1);
   934 by Auto_tac;
   935 bind_thm("dropWhile_append1", conjI RS (result() RS mp));
   936 Addsimps [dropWhile_append1];
   937 
   938 Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
   939 by (induct_tac "xs" 1);
   940 by Auto_tac;
   941 bind_thm("dropWhile_append2", ballI RS (result() RS mp));
   942 Addsimps [dropWhile_append2];
   943 
   944 Goal "x:set(takeWhile P xs) --> x:set xs & P x";
   945 by (induct_tac "xs" 1);
   946 by Auto_tac;
   947 qed_spec_mp"set_take_whileD";
   948 
   949 (** zip **)
   950 section "zip";
   951 
   952 Goal "zip [] ys = []";
   953 by (induct_tac "ys" 1);
   954 by Auto_tac;
   955 qed "zip_Nil";
   956 Addsimps [zip_Nil];
   957 
   958 Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys";
   959 by (Simp_tac 1);
   960 qed "zip_Cons_Cons";
   961 Addsimps [zip_Cons_Cons];
   962 
   963 Delsimps(tl (thms"zip.simps"));
   964 
   965 Goal "!xs. length xs = length ys --> length (zip xs ys) = length ys";
   966 by (induct_tac "ys" 1);
   967  by (Simp_tac 1);
   968 by (Clarify_tac 1);
   969 by (exhaust_tac "xs" 1);
   970  by (Auto_tac);
   971 qed_spec_mp "length_zip";
   972 Addsimps [length_zip];
   973 
   974 Goal
   975 "!xs. length xs = length us --> length ys = length vs --> \
   976 \     zip (xs@ys) (us@vs) = zip xs us @ zip ys vs";
   977 by (induct_tac "us" 1);
   978  by (Asm_full_simp_tac 1);
   979 by (Asm_full_simp_tac 1);
   980 by (Clarify_tac 1);
   981 by (exhaust_tac "xs" 1);
   982  by (Auto_tac);
   983 qed_spec_mp "zip_append";
   984 
   985 Goal "!xs. length xs = length ys --> zip (rev xs) (rev ys) = rev (zip xs ys)";
   986 by (induct_tac "ys" 1);
   987  by (Asm_full_simp_tac 1);
   988 by (Asm_full_simp_tac 1);
   989 by (Clarify_tac 1);
   990 by (exhaust_tac "xs" 1);
   991  by (Auto_tac);
   992 by (asm_full_simp_tac (simpset() addsimps [zip_append]) 1);
   993 qed_spec_mp "zip_rev";
   994 
   995 Goal
   996 "!i xs. i < length xs --> i < length ys --> (zip xs ys)!i = (xs!i, ys!i)";
   997 by (induct_tac "ys" 1);
   998  by (Simp_tac 1);
   999 by (Clarify_tac 1);
  1000 by (exhaust_tac "xs" 1);
  1001  by (Auto_tac);
  1002 by (asm_full_simp_tac (simpset() addsimps (thms"nth.simps") addsplits [nat.split]) 1);
  1003 qed_spec_mp "nth_zip";
  1004 Addsimps [nth_zip];
  1005 
  1006 Goal
  1007  "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]";
  1008 by (rtac sym 1);
  1009 by (asm_simp_tac (simpset() addsimps [update_zip]) 1);
  1010 qed_spec_mp "zip_update";
  1011 
  1012 Goal "!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)";
  1013 by (induct_tac "i" 1);
  1014  by (Auto_tac);
  1015 by (exhaust_tac "j" 1);
  1016  by (Auto_tac);
  1017 qed "zip_replicate";
  1018 Addsimps [zip_replicate];
  1019 
  1020 
  1021 (** foldl **)
  1022 section "foldl";
  1023 
  1024 Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
  1025 by (induct_tac "xs" 1);
  1026 by Auto_tac;
  1027 qed_spec_mp "foldl_append";
  1028 Addsimps [foldl_append];
  1029 
  1030 (* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
  1031    because it requires an additional transitivity step
  1032 *)
  1033 Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
  1034 by (induct_tac "ns" 1);
  1035 by Auto_tac;
  1036 qed_spec_mp "start_le_sum";
  1037 
  1038 Goal "n : set ns ==> n <= foldl op+ 0 ns";
  1039 by (force_tac (claset() addIs [start_le_sum],
  1040               simpset() addsimps [in_set_conv_decomp]) 1);
  1041 qed "elem_le_sum";
  1042 
  1043 Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
  1044 by (induct_tac "ns" 1);
  1045 by Auto_tac;
  1046 qed_spec_mp "sum_eq_0_conv";
  1047 AddIffs [sum_eq_0_conv];
  1048 
  1049 (** upto **)
  1050 
  1051 (* Does not terminate! *)
  1052 Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
  1053 by (induct_tac "j" 1);
  1054 by Auto_tac;
  1055 qed "upt_rec";
  1056 
  1057 Goal "j<=i ==> [i..j(] = []";
  1058 by (stac upt_rec 1);
  1059 by (Asm_simp_tac 1);
  1060 qed "upt_conv_Nil";
  1061 Addsimps [upt_conv_Nil];
  1062 
  1063 Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
  1064 by (Asm_simp_tac 1);
  1065 qed "upt_Suc";
  1066 
  1067 Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
  1068 by (rtac trans 1);
  1069 by (stac upt_rec 1);
  1070 by (rtac refl 2);
  1071 by (Asm_simp_tac 1);
  1072 qed "upt_conv_Cons";
  1073 
  1074 Goal "length [i..j(] = j-i";
  1075 by (induct_tac "j" 1);
  1076  by (Simp_tac 1);
  1077 by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
  1078 qed "length_upt";
  1079 Addsimps [length_upt];
  1080 
  1081 Goal "i+k < j --> [i..j(] ! k = i+k";
  1082 by (induct_tac "j" 1);
  1083  by (Simp_tac 1);
  1084 by (asm_simp_tac (simpset() addsimps [nth_append,less_diff_conv]@add_ac) 1);
  1085 by (Clarify_tac 1);
  1086 by (subgoal_tac "n=i+k" 1);
  1087  by (Asm_simp_tac 2);
  1088 by (Asm_simp_tac 1);
  1089 qed_spec_mp "nth_upt";
  1090 Addsimps [nth_upt];
  1091 
  1092 Goal "!i. i+m <= n --> take m [i..n(] = [i..i+m(]";
  1093 by (induct_tac "m" 1);
  1094  by (Simp_tac 1);
  1095 by (Clarify_tac 1);
  1096 by (stac upt_rec 1);
  1097 by (rtac sym 1);
  1098 by (stac upt_rec 1);
  1099 by (asm_simp_tac (simpset() delsimps (thms"upt.simps")) 1);
  1100 qed_spec_mp "take_upt";
  1101 Addsimps [take_upt];
  1102 
  1103 Goal "!m i. i < n-m --> (map f [m..n(]) ! i = f(m+i)";
  1104 by (induct_tac "n" 1);
  1105  by (Simp_tac 1);
  1106 by (Clarify_tac 1);
  1107 by (subgoal_tac "m < Suc n" 1);
  1108  by (arith_tac 2);
  1109 by (stac upt_rec 1);
  1110 by (asm_simp_tac (simpset() delsplits [split_if]) 1);
  1111 by (split_tac [split_if] 1);
  1112 by (rtac conjI 1);
  1113  by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
  1114  by (simp_tac (simpset() addsimps [nth_append] addsplits [nat.split]) 1);
  1115  by (Clarify_tac 1);
  1116  by (rtac conjI 1);
  1117   by (Clarify_tac 1);
  1118   by (subgoal_tac "Suc(m+nat) < n" 1);
  1119    by (arith_tac 2);
  1120   by (Asm_simp_tac 1);
  1121  by (Clarify_tac 1);
  1122  by (subgoal_tac "n = Suc(m+nat)" 1);
  1123   by (arith_tac 2);
  1124  by (Asm_simp_tac 1);
  1125 by (simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
  1126 by (arith_tac 1);
  1127 qed_spec_mp "nth_map_upt";
  1128 
  1129 Goal "ALL xs ys. k <= length xs --> k <= length ys -->  \
  1130 \        (ALL i. i < k --> xs!i = ys!i)  \
  1131 \     --> take k xs = take k ys";
  1132 by (induct_tac "k" 1);
  1133 by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq_0_disj, 
  1134 						all_conj_distrib])));
  1135 by (Clarify_tac 1);
  1136 (*Both lists must be non-empty*)
  1137 by (exhaust_tac "xs" 1);
  1138 by (exhaust_tac "ys" 2);
  1139 by (ALLGOALS Clarify_tac);
  1140 (*prenexing's needed, not miniscoping*)
  1141 by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [sym])  
  1142                                        delsimps (all_simps))));
  1143 by (Blast_tac 1);
  1144 qed_spec_mp "nth_take_lemma";
  1145 
  1146 Goal "[| length xs = length ys;  \
  1147 \        ALL i. i < length xs --> xs!i = ys!i |]  \
  1148 \     ==> xs = ys";
  1149 by (forward_tac [[le_refl, eq_imp_le] MRS nth_take_lemma] 1);
  1150 by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all])));
  1151 qed_spec_mp "nth_equalityI";
  1152 
  1153 (*The famous take-lemma*)
  1154 Goal "(ALL i. take i xs = take i ys) ==> xs = ys";
  1155 by (dres_inst_tac [("x", "max (length xs) (length ys)")] spec 1);
  1156 by (full_simp_tac (simpset() addsimps [le_max_iff_disj, take_all]) 1);
  1157 qed_spec_mp "take_equalityI";
  1158 
  1159 
  1160 (** nodups & remdups **)
  1161 section "nodups & remdups";
  1162 
  1163 Goal "set(remdups xs) = set xs";
  1164 by (induct_tac "xs" 1);
  1165  by (Simp_tac 1);
  1166 by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
  1167 qed "set_remdups";
  1168 Addsimps [set_remdups];
  1169 
  1170 Goal "nodups(remdups xs)";
  1171 by (induct_tac "xs" 1);
  1172 by Auto_tac;
  1173 qed "nodups_remdups";
  1174 
  1175 Goal "nodups xs --> nodups (filter P xs)";
  1176 by (induct_tac "xs" 1);
  1177 by Auto_tac;
  1178 qed_spec_mp "nodups_filter";
  1179 
  1180 (** replicate **)
  1181 section "replicate";
  1182 
  1183 Goal "length(replicate n x) = n";
  1184 by (induct_tac "n" 1);
  1185 by Auto_tac;
  1186 qed "length_replicate";
  1187 Addsimps [length_replicate];
  1188 
  1189 Goal "map f (replicate n x) = replicate n (f x)";
  1190 by (induct_tac "n" 1);
  1191 by Auto_tac;
  1192 qed "map_replicate";
  1193 Addsimps [map_replicate];
  1194 
  1195 Goal "(replicate n x) @ (x#xs) = x # replicate n x @ xs";
  1196 by (induct_tac "n" 1);
  1197 by Auto_tac;
  1198 qed "replicate_app_Cons_same";
  1199 
  1200 Goal "rev(replicate n x) = replicate n x";
  1201 by (induct_tac "n" 1);
  1202  by (Simp_tac 1);
  1203 by (asm_simp_tac (simpset() addsimps [replicate_app_Cons_same]) 1);
  1204 qed "rev_replicate";
  1205 Addsimps [rev_replicate];
  1206 
  1207 Goal "replicate (n+m) x = replicate n x @ replicate m x";
  1208 by (induct_tac "n" 1);
  1209 by Auto_tac;
  1210 qed "replicate_add";
  1211 
  1212 Goal"n ~= 0 --> hd(replicate n x) = x";
  1213 by (induct_tac "n" 1);
  1214 by Auto_tac;
  1215 qed_spec_mp "hd_replicate";
  1216 Addsimps [hd_replicate];
  1217 
  1218 Goal "n ~= 0 --> tl(replicate n x) = replicate (n-1) x";
  1219 by (induct_tac "n" 1);
  1220 by Auto_tac;
  1221 qed_spec_mp "tl_replicate";
  1222 Addsimps [tl_replicate];
  1223 
  1224 Goal "n ~= 0 --> last(replicate n x) = x";
  1225 by (induct_tac "n" 1);
  1226 by Auto_tac;
  1227 qed_spec_mp "last_replicate";
  1228 Addsimps [last_replicate];
  1229 
  1230 Goal "!i. i<n --> (replicate n x)!i = x";
  1231 by (induct_tac "n" 1);
  1232  by (Simp_tac 1);
  1233 by (asm_simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
  1234 qed_spec_mp "nth_replicate";
  1235 Addsimps [nth_replicate];
  1236 
  1237 Goal "set(replicate (Suc n) x) = {x}";
  1238 by (induct_tac "n" 1);
  1239 by Auto_tac;
  1240 val lemma = result();
  1241 
  1242 Goal "n ~= 0 ==> set(replicate n x) = {x}";
  1243 by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
  1244 qed "set_replicate";
  1245 Addsimps [set_replicate];
  1246 
  1247 Goal "set(replicate n x) = (if n=0 then {} else {x})";
  1248 by (Auto_tac);
  1249 qed "set_replicate_conv_if";
  1250 
  1251 Goal "x : set(replicate n y) --> x=y";
  1252 by (asm_simp_tac (simpset() addsimps [set_replicate_conv_if]) 1);
  1253 qed_spec_mp "in_set_replicateD";
  1254 
  1255 
  1256 (*** Lexcicographic orderings on lists ***)
  1257 section"Lexcicographic orderings on lists";
  1258 
  1259 Goal "wf r ==> wf(lexn r n)";
  1260 by (induct_tac "n" 1);
  1261 by (Simp_tac 1);
  1262 by (Simp_tac 1);
  1263 by (rtac wf_subset 1);
  1264 by (rtac Int_lower1 2);
  1265 by (rtac wf_prod_fun_image 1);
  1266 by (rtac injI 2);
  1267 by Auto_tac;
  1268 qed "wf_lexn";
  1269 
  1270 Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
  1271 by (induct_tac "n" 1);
  1272 by Auto_tac;
  1273 qed_spec_mp "lexn_length";
  1274 
  1275 Goalw [lex_def] "wf r ==> wf(lex r)";
  1276 by (rtac wf_UN 1);
  1277 by (blast_tac (claset() addIs [wf_lexn]) 1);
  1278 by (Clarify_tac 1);
  1279 by (rename_tac "m n" 1);
  1280 by (subgoal_tac "m ~= n" 1);
  1281  by (Blast_tac 2);
  1282 by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
  1283 qed "wf_lex";
  1284 AddSIs [wf_lex];
  1285 
  1286 Goal
  1287  "lexn r n = \
  1288 \ {(xs,ys). length xs = n & length ys = n & \
  1289 \           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
  1290 by (induct_tac "n" 1);
  1291  by (Simp_tac 1);
  1292  by (Blast_tac 1);
  1293 by (asm_full_simp_tac (simpset() 
  1294 				addsimps [lex_prod_def]) 1);
  1295 by (auto_tac (claset(), simpset()));
  1296   by (Blast_tac 1);
  1297  by (rename_tac "a xys x xs' y ys'" 1);
  1298  by (res_inst_tac [("x","a#xys")] exI 1);
  1299  by (Simp_tac 1);
  1300 by (exhaust_tac "xys" 1);
  1301  by (ALLGOALS (asm_full_simp_tac (simpset())));
  1302 by (Blast_tac 1);
  1303 qed "lexn_conv";
  1304 
  1305 Goalw [lex_def]
  1306  "lex r = \
  1307 \ {(xs,ys). length xs = length ys & \
  1308 \           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
  1309 by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1);
  1310 qed "lex_conv";
  1311 
  1312 Goalw [lexico_def] "wf r ==> wf(lexico r)";
  1313 by (Blast_tac 1);
  1314 qed "wf_lexico";
  1315 AddSIs [wf_lexico];
  1316 
  1317 Goalw
  1318  [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
  1319 "lexico r = {(xs,ys). length xs < length ys | \
  1320 \                     length xs = length ys & (xs,ys) : lex r}";
  1321 by (Simp_tac 1);
  1322 qed "lexico_conv";
  1323 
  1324 Goal "([],ys) ~: lex r";
  1325 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1326 qed "Nil_notin_lex";
  1327 
  1328 Goal "(xs,[]) ~: lex r";
  1329 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1330 qed "Nil2_notin_lex";
  1331 
  1332 AddIffs [Nil_notin_lex,Nil2_notin_lex];
  1333 
  1334 Goal "((x#xs,y#ys) : lex r) = \
  1335 \     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
  1336 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1337 by (rtac iffI 1);
  1338  by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
  1339 by (REPEAT(eresolve_tac [conjE, exE] 1));
  1340 by (exhaust_tac "xys" 1);
  1341 by (Asm_full_simp_tac 1);
  1342 by (Asm_full_simp_tac 1);
  1343 by (Blast_tac 1);
  1344 qed "Cons_in_lex";
  1345 AddIffs [Cons_in_lex];
  1346 
  1347 
  1348 (*** Versions of some theorems above using binary numerals ***)
  1349 
  1350 AddIffs (map (rename_numerals thy) 
  1351 	  [length_0_conv, zero_length_conv, length_greater_0_conv,
  1352 	   sum_eq_0_conv]);
  1353 
  1354 Goal "take n (x#xs) = (if n = #0 then [] else x # take (n-#1) xs)";
  1355 by (exhaust_tac "n" 1);
  1356 by (ALLGOALS 
  1357     (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
  1358 qed "take_Cons'";
  1359 
  1360 Goal "drop n (x#xs) = (if n = #0 then x#xs else drop (n-#1) xs)";
  1361 by (exhaust_tac "n" 1);
  1362 by (ALLGOALS
  1363     (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
  1364 qed "drop_Cons'";
  1365 
  1366 Goal "(x#xs)!n = (if n = #0 then x else xs!(n-#1))";
  1367 by (exhaust_tac "n" 1);
  1368 by (ALLGOALS
  1369     (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
  1370 qed "nth_Cons'";
  1371 
  1372 Addsimps (map (inst "n" "number_of ?v") [take_Cons', drop_Cons', nth_Cons']);
  1373