src/HOL/List.ML
author wenzelm
Sun Oct 14 22:08:29 2001 +0200 (2001-10-14)
changeset 11770 b6bb7a853dd2
parent 11701 3d51fbf81c17
child 11868 56db9f3a6b3e
permissions -rw-r--r--
moved rulify to ObjectLogic;
     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 bind_thm ("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 Goal "(xs@ys : lists A) = (xs : lists A & ys : lists A)";
    52 by(induct_tac "xs" 1);
    53 by(Auto_tac);
    54 qed "append_in_lists_conv";
    55 AddIffs [append_in_lists_conv];
    56 
    57 (** length **)
    58 (* needs to come before "@" because of thm append_eq_append_conv *)
    59 
    60 section "length";
    61 
    62 Goal "length(xs@ys) = length(xs)+length(ys)";
    63 by (induct_tac "xs" 1);
    64 by Auto_tac;
    65 qed"length_append";
    66 Addsimps [length_append];
    67 
    68 Goal "length (map f xs) = length xs";
    69 by (induct_tac "xs" 1);
    70 by Auto_tac;
    71 qed "length_map";
    72 Addsimps [length_map];
    73 
    74 Goal "length(rev xs) = length(xs)";
    75 by (induct_tac "xs" 1);
    76 by Auto_tac;
    77 qed "length_rev";
    78 Addsimps [length_rev];
    79 
    80 Goal "length(tl xs) = (length xs) - 1";
    81 by (case_tac "xs" 1);
    82 by Auto_tac;
    83 qed "length_tl";
    84 Addsimps [length_tl];
    85 
    86 Goal "(length xs = 0) = (xs = [])";
    87 by (induct_tac "xs" 1);
    88 by Auto_tac;
    89 qed "length_0_conv";
    90 AddIffs [length_0_conv];
    91 
    92 Goal "(0 < length xs) = (xs ~= [])";
    93 by (induct_tac "xs" 1);
    94 by Auto_tac;
    95 qed "length_greater_0_conv";
    96 AddIffs [length_greater_0_conv];
    97 
    98 Goal "(length xs = Suc n) = (? y ys. xs = y#ys & length ys = n)";
    99 by (induct_tac "xs" 1);
   100 by Auto_tac;
   101 qed "length_Suc_conv";
   102 
   103 (** @ - append **)
   104 
   105 section "@ - append";
   106 
   107 Goal "(xs@ys)@zs = xs@(ys@zs)";
   108 by (induct_tac "xs" 1);
   109 by Auto_tac;
   110 qed "append_assoc";
   111 Addsimps [append_assoc];
   112 
   113 Goal "xs @ [] = xs";
   114 by (induct_tac "xs" 1);
   115 by Auto_tac;
   116 qed "append_Nil2";
   117 Addsimps [append_Nil2];
   118 
   119 Goal "(xs@ys = []) = (xs=[] & ys=[])";
   120 by (induct_tac "xs" 1);
   121 by Auto_tac;
   122 qed "append_is_Nil_conv";
   123 AddIffs [append_is_Nil_conv];
   124 
   125 Goal "([] = xs@ys) = (xs=[] & ys=[])";
   126 by (induct_tac "xs" 1);
   127 by Auto_tac;
   128 qed "Nil_is_append_conv";
   129 AddIffs [Nil_is_append_conv];
   130 
   131 Goal "(xs @ ys = xs) = (ys=[])";
   132 by (induct_tac "xs" 1);
   133 by Auto_tac;
   134 qed "append_self_conv";
   135 
   136 Goal "(xs = xs @ ys) = (ys=[])";
   137 by (induct_tac "xs" 1);
   138 by Auto_tac;
   139 qed "self_append_conv";
   140 AddIffs [append_self_conv,self_append_conv];
   141 
   142 Goal "!ys. length xs = length ys | length us = length vs \
   143 \              --> (xs@us = ys@vs) = (xs=ys & us=vs)";
   144 by (induct_tac "xs" 1);
   145  by (rtac allI 1);
   146  by (case_tac "ys" 1);
   147   by (Asm_simp_tac 1);
   148  by (Force_tac 1);
   149 by (rtac allI 1);
   150 by (case_tac "ys" 1);
   151 by (Force_tac 1);
   152 by (Asm_simp_tac 1);
   153 qed_spec_mp "append_eq_append_conv";
   154 Addsimps [append_eq_append_conv];
   155 
   156 Goal "(xs @ ys = xs @ zs) = (ys=zs)";
   157 by (Simp_tac 1);
   158 qed "same_append_eq";
   159 
   160 Goal "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"; 
   161 by (Simp_tac 1);
   162 qed "append1_eq_conv";
   163 
   164 Goal "(ys @ xs = zs @ xs) = (ys=zs)";
   165 by (Simp_tac 1);
   166 qed "append_same_eq";
   167 
   168 AddIffs [same_append_eq, append1_eq_conv, append_same_eq];
   169 
   170 Goal "(xs @ ys = ys) = (xs=[])";
   171 by (cut_inst_tac [("zs","[]")] append_same_eq 1);
   172 by Auto_tac;
   173 qed "append_self_conv2";
   174 
   175 Goal "(ys = xs @ ys) = (xs=[])";
   176 by (simp_tac (simpset() addsimps
   177      [simplify (simpset()) (read_instantiate[("ys","[]")]append_same_eq)]) 1);
   178 by (Blast_tac 1);
   179 qed "self_append_conv2";
   180 AddIffs [append_self_conv2,self_append_conv2];
   181 
   182 Goal "xs ~= [] --> hd xs # tl xs = xs";
   183 by (induct_tac "xs" 1);
   184 by Auto_tac;
   185 qed_spec_mp "hd_Cons_tl";
   186 Addsimps [hd_Cons_tl];
   187 
   188 Goal "hd(xs@ys) = (if xs=[] then hd ys else hd xs)";
   189 by (induct_tac "xs" 1);
   190 by Auto_tac;
   191 qed "hd_append";
   192 
   193 Goal "xs ~= [] ==> hd(xs @ ys) = hd xs";
   194 by (asm_simp_tac (simpset() addsimps [hd_append]
   195                            addsplits [list.split]) 1);
   196 qed "hd_append2";
   197 Addsimps [hd_append2];
   198 
   199 Goal "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)";
   200 by (simp_tac (simpset() addsplits [list.split]) 1);
   201 qed "tl_append";
   202 
   203 Goal "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys";
   204 by (asm_simp_tac (simpset() addsimps [tl_append]
   205                            addsplits [list.split]) 1);
   206 qed "tl_append2";
   207 Addsimps [tl_append2];
   208 
   209 (* trivial rules for solving @-equations automatically *)
   210 
   211 Goal "xs = ys ==> xs = [] @ ys";
   212 by (Asm_simp_tac 1);
   213 qed "eq_Nil_appendI";
   214 
   215 Goal "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs";
   216 by (dtac sym 1);
   217 by (Asm_simp_tac 1);
   218 qed "Cons_eq_appendI";
   219 
   220 Goal "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us";
   221 by (dtac sym 1);
   222 by (Asm_simp_tac 1);
   223 qed "append_eq_appendI";
   224 
   225 
   226 (***
   227 Simplification procedure for all list equalities.
   228 Currently only tries to rearranges @ to see if
   229 - both lists end in a singleton list,
   230 - or both lists end in the same list.
   231 ***)
   232 local
   233 
   234 val list_eq_pattern =
   235   Thm.read_cterm (Theory.sign_of (the_context ())) ("(xs::'a list) = ys",HOLogic.boolT);
   236 
   237 fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
   238       (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
   239   | last (Const("List.op @",_) $ _ $ ys) = last ys
   240   | last t = t;
   241 
   242 fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
   243   | list1 _ = false;
   244 
   245 fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
   246       (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
   247   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
   248   | butlast xs = Const("List.list.Nil",fastype_of xs);
   249 
   250 val rearr_tac =
   251   simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons]);
   252 
   253 fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
   254   let
   255     val lastl = last lhs and lastr = last rhs
   256     fun rearr conv =
   257       let val lhs1 = butlast lhs and rhs1 = butlast rhs
   258           val Type(_,listT::_) = eqT
   259           val appT = [listT,listT] ---> listT
   260           val app = Const("List.op @",appT)
   261           val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
   262           val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
   263           val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
   264             handle ERROR =>
   265             error("The error(s) above occurred while trying to prove " ^
   266                   string_of_cterm ct)
   267       in Some((conv RS (thm RS trans)) RS eq_reflection) end
   268 
   269   in if list1 lastl andalso list1 lastr
   270      then rearr append1_eq_conv
   271      else
   272      if lastl aconv lastr
   273      then rearr append_same_eq
   274      else None
   275   end;
   276 in
   277 val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq;
   278 end;
   279 
   280 Addsimprocs [list_eq_simproc];
   281 
   282 
   283 (** map **)
   284 
   285 section "map";
   286 
   287 Goal "(!x. x : set xs --> f x = g x) --> map f xs = map g xs";
   288 by (induct_tac "xs" 1);
   289 by Auto_tac;
   290 bind_thm("map_ext", impI RS (allI RS (result() RS mp)));
   291 
   292 Goal "map (%x. x) = (%xs. xs)";
   293 by (rtac ext 1);
   294 by (induct_tac "xs" 1);
   295 by Auto_tac;
   296 qed "map_ident";
   297 Addsimps[map_ident];
   298 
   299 Goal "map f (xs@ys) = map f xs @ map f ys";
   300 by (induct_tac "xs" 1);
   301 by Auto_tac;
   302 qed "map_append";
   303 Addsimps[map_append];
   304 
   305 Goalw [o_def] "map (f o g) xs = map f (map g xs)";
   306 by (induct_tac "xs" 1);
   307 by Auto_tac;
   308 qed "map_compose";
   309 (*Addsimps[map_compose];*)
   310 
   311 Goal "rev(map f xs) = map f (rev xs)";
   312 by (induct_tac "xs" 1);
   313 by Auto_tac;
   314 qed "rev_map";
   315 
   316 (* a congruence rule for map: *)
   317 Goal "xs=ys ==> (!x. x : set ys --> f x = g x) --> map f xs = map g ys";
   318 by (hyp_subst_tac 1);
   319 by (induct_tac "ys" 1);
   320 by Auto_tac;
   321 bind_thm("map_cong", impI RSN (2,allI RSN (2, result() RS mp)));
   322 
   323 Goal "(map f xs = []) = (xs = [])";
   324 by (case_tac "xs" 1);
   325 by Auto_tac;
   326 qed "map_is_Nil_conv";
   327 AddIffs [map_is_Nil_conv];
   328 
   329 Goal "([] = map f xs) = (xs = [])";
   330 by (case_tac "xs" 1);
   331 by Auto_tac;
   332 qed "Nil_is_map_conv";
   333 AddIffs [Nil_is_map_conv];
   334 
   335 Goal "(map f xs = y#ys) = (? x xs'. xs = x#xs' & f x = y & map f xs' = ys)";
   336 by (case_tac "xs" 1);
   337 by (ALLGOALS Asm_simp_tac);
   338 qed "map_eq_Cons";
   339 
   340 Goal "!xs. map f xs = map f ys --> (!x y. f x = f y --> x=y) --> xs=ys";
   341 by (induct_tac "ys" 1);
   342  by (Asm_simp_tac 1);
   343 by (fast_tac (claset() addss (simpset() addsimps [map_eq_Cons])) 1);
   344 qed_spec_mp "map_injective";
   345 
   346 Goal "inj f ==> inj (map f)";
   347 by (blast_tac (claset() addDs [map_injective,injD] addIs [injI]) 1);
   348 qed "inj_mapI";
   349 
   350 Goalw [inj_on_def] "inj (map f) ==> inj f";
   351 by (Clarify_tac 1);
   352 by (eres_inst_tac [("x","[x]")] ballE 1);
   353  by (eres_inst_tac [("x","[y]")] ballE 1);
   354   by (Asm_full_simp_tac 1);
   355  by (Blast_tac 1);
   356 by (Blast_tac 1);
   357 qed "inj_mapD";
   358 
   359 Goal "inj (map f) = inj f";
   360 by (blast_tac (claset() addDs [inj_mapD] addIs [inj_mapI]) 1);
   361 qed "inj_map";
   362 
   363 (** rev **)
   364 
   365 section "rev";
   366 
   367 Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
   368 by (induct_tac "xs" 1);
   369 by Auto_tac;
   370 qed "rev_append";
   371 Addsimps[rev_append];
   372 
   373 Goal "rev(rev l) = l";
   374 by (induct_tac "l" 1);
   375 by Auto_tac;
   376 qed "rev_rev_ident";
   377 Addsimps[rev_rev_ident];
   378 
   379 Goal "(rev xs = []) = (xs = [])";
   380 by (induct_tac "xs" 1);
   381 by Auto_tac;
   382 qed "rev_is_Nil_conv";
   383 AddIffs [rev_is_Nil_conv];
   384 
   385 Goal "([] = rev xs) = (xs = [])";
   386 by (induct_tac "xs" 1);
   387 by Auto_tac;
   388 qed "Nil_is_rev_conv";
   389 AddIffs [Nil_is_rev_conv];
   390 
   391 Goal "!ys. (rev xs = rev ys) = (xs = ys)";
   392 by (induct_tac "xs" 1);
   393  by (Force_tac 1);
   394 by (rtac allI 1);
   395 by (case_tac "ys" 1);
   396  by (Asm_simp_tac 1);
   397 by (Force_tac 1);
   398 qed_spec_mp "rev_is_rev_conv";
   399 AddIffs [rev_is_rev_conv];
   400 
   401 val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
   402 by (stac (rev_rev_ident RS sym) 1);
   403 by (res_inst_tac [("list", "rev xs")] list.induct 1);
   404 by (ALLGOALS Simp_tac);
   405 by (resolve_tac prems 1);
   406 by (eresolve_tac prems 1);
   407 qed "rev_induct";
   408 
   409 val rev_induct_tac = induct_thm_tac rev_induct;
   410 
   411 Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
   412 by (rev_induct_tac "xs" 1);
   413 by Auto_tac;
   414 qed "rev_exhaust_aux";
   415 
   416 bind_thm ("rev_exhaust", ObjectLogic.rulify rev_exhaust_aux);
   417 
   418 
   419 (** set **)
   420 
   421 section "set";
   422 
   423 Goal "finite (set xs)";
   424 by (induct_tac "xs" 1);
   425 by Auto_tac;
   426 qed "finite_set";
   427 AddIffs [finite_set];
   428 
   429 Goal "set (xs@ys) = (set xs Un set ys)";
   430 by (induct_tac "xs" 1);
   431 by Auto_tac;
   432 qed "set_append";
   433 Addsimps[set_append];
   434 
   435 Goal "set l <= set (x#l)";
   436 by Auto_tac;
   437 qed "set_subset_Cons";
   438 
   439 Goal "(set xs = {}) = (xs = [])";
   440 by (induct_tac "xs" 1);
   441 by Auto_tac;
   442 qed "set_empty";
   443 Addsimps [set_empty];
   444 
   445 Goal "set(rev xs) = set(xs)";
   446 by (induct_tac "xs" 1);
   447 by Auto_tac;
   448 qed "set_rev";
   449 Addsimps [set_rev];
   450 
   451 Goal "set(map f xs) = f`(set xs)";
   452 by (induct_tac "xs" 1);
   453 by Auto_tac;
   454 qed "set_map";
   455 Addsimps [set_map];
   456 
   457 Goal "set(filter P xs) = {x. x : set xs & P x}";
   458 by (induct_tac "xs" 1);
   459 by Auto_tac;
   460 qed "set_filter";
   461 Addsimps [set_filter];
   462 
   463 Goal "set[i..j(] = {k. i <= k & k < j}";
   464 by (induct_tac "j" 1);
   465 by (ALLGOALS Asm_simp_tac);
   466 by (etac ssubst 1);
   467 by Auto_tac;
   468 by (arith_tac 1);
   469 qed "set_upt";
   470 Addsimps [set_upt];
   471 
   472 Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
   473 by (induct_tac "xs" 1);
   474  by (Simp_tac 1);
   475 by (Asm_simp_tac 1);
   476 by (rtac iffI 1);
   477 by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
   478 by (REPEAT(etac exE 1));
   479 by (case_tac "ys" 1);
   480 by Auto_tac;
   481 qed "in_set_conv_decomp";
   482 
   483 
   484 (* eliminate `lists' in favour of `set' *)
   485 
   486 Goal "(xs : lists A) = (!x : set xs. x : A)";
   487 by (induct_tac "xs" 1);
   488 by Auto_tac;
   489 qed "in_lists_conv_set";
   490 
   491 bind_thm("in_listsD",in_lists_conv_set RS iffD1);
   492 AddSDs [in_listsD];
   493 bind_thm("in_listsI",in_lists_conv_set RS iffD2);
   494 AddSIs [in_listsI];
   495 
   496 (** mem **)
   497  
   498 section "mem";
   499 
   500 Goal "(x mem xs) = (x: set xs)";
   501 by (induct_tac "xs" 1);
   502 by Auto_tac;
   503 qed "set_mem_eq";
   504 
   505 
   506 (** list_all **)
   507 
   508 section "list_all";
   509 
   510 Goal "list_all P xs = (!x:set xs. P x)";
   511 by (induct_tac "xs" 1);
   512 by Auto_tac;
   513 qed "list_all_conv";
   514 
   515 Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)";
   516 by (induct_tac "xs" 1);
   517 by Auto_tac;
   518 qed "list_all_append";
   519 Addsimps [list_all_append];
   520 
   521 
   522 (** filter **)
   523 
   524 section "filter";
   525 
   526 Goal "filter P (xs@ys) = filter P xs @ filter P ys";
   527 by (induct_tac "xs" 1);
   528 by Auto_tac;
   529 qed "filter_append";
   530 Addsimps [filter_append];
   531 
   532 Goal "filter (%x. True) xs = xs";
   533 by (induct_tac "xs" 1);
   534 by Auto_tac;
   535 qed "filter_True";
   536 Addsimps [filter_True];
   537 
   538 Goal "filter (%x. False) xs = []";
   539 by (induct_tac "xs" 1);
   540 by Auto_tac;
   541 qed "filter_False";
   542 Addsimps [filter_False];
   543 
   544 Goal "length (filter P xs) <= length xs";
   545 by (induct_tac "xs" 1);
   546 by Auto_tac;
   547 by (asm_simp_tac (simpset() addsimps [le_SucI]) 1);
   548 qed "length_filter";
   549 Addsimps[length_filter];
   550 
   551 Goal "set (filter P xs) <= set xs";
   552 by Auto_tac;
   553 qed "filter_is_subset";
   554 Addsimps [filter_is_subset];
   555 
   556 
   557 section "concat";
   558 
   559 Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
   560 by (induct_tac "xs" 1);
   561 by Auto_tac;
   562 qed"concat_append";
   563 Addsimps [concat_append];
   564 
   565 Goal "(concat xss = []) = (!xs:set xss. xs=[])";
   566 by (induct_tac "xss" 1);
   567 by Auto_tac;
   568 qed "concat_eq_Nil_conv";
   569 AddIffs [concat_eq_Nil_conv];
   570 
   571 Goal "([] = concat xss) = (!xs:set xss. xs=[])";
   572 by (induct_tac "xss" 1);
   573 by Auto_tac;
   574 qed "Nil_eq_concat_conv";
   575 AddIffs [Nil_eq_concat_conv];
   576 
   577 Goal  "set(concat xs) = Union(set ` set xs)";
   578 by (induct_tac "xs" 1);
   579 by Auto_tac;
   580 qed"set_concat";
   581 Addsimps [set_concat];
   582 
   583 Goal "map f (concat xs) = concat (map (map f) xs)"; 
   584 by (induct_tac "xs" 1);
   585 by Auto_tac;
   586 qed "map_concat";
   587 
   588 Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
   589 by (induct_tac "xs" 1);
   590 by Auto_tac;
   591 qed"filter_concat"; 
   592 
   593 Goal "rev(concat xs) = concat (map rev (rev xs))";
   594 by (induct_tac "xs" 1);
   595 by Auto_tac;
   596 qed "rev_concat";
   597 
   598 (** nth **)
   599 
   600 section "nth";
   601 
   602 Goal "(x#xs)!0 = x";
   603 by Auto_tac;
   604 qed "nth_Cons_0";
   605 Addsimps [nth_Cons_0];
   606 
   607 Goal "(x#xs)!(Suc n) = xs!n";
   608 by Auto_tac;
   609 qed "nth_Cons_Suc";
   610 Addsimps [nth_Cons_Suc];
   611 
   612 Delsimps (thms "nth.simps");
   613 
   614 Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
   615 by (induct_tac "xs" 1);
   616  by (Asm_simp_tac 1);
   617  by (rtac allI 1);
   618  by (case_tac "n" 1);
   619   by Auto_tac;
   620 qed_spec_mp "nth_append";
   621 
   622 Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
   623 by (induct_tac "xs" 1);
   624  by (Asm_full_simp_tac 1);
   625 by (rtac allI 1);
   626 by (induct_tac "n" 1);
   627 by Auto_tac;
   628 qed_spec_mp "nth_map";
   629 Addsimps [nth_map];
   630 
   631 Goal "set xs = {xs!i |i. i < length xs}";
   632 by (induct_tac "xs" 1);
   633  by (Simp_tac 1);
   634 by (Asm_simp_tac 1);
   635 by Safe_tac;
   636   by (res_inst_tac [("x","0")] exI 1);
   637   by (Simp_tac 1);
   638  by (res_inst_tac [("x","Suc i")] exI 1);
   639  by (Asm_simp_tac 1);
   640 by (case_tac "i" 1);
   641  by (Asm_full_simp_tac 1);
   642 by (rename_tac "j" 1);
   643  by (res_inst_tac [("x","j")] exI 1);
   644 by (Asm_simp_tac 1);
   645 qed "set_conv_nth";
   646 
   647 Goal "n < length xs ==> Ball (set xs) P --> P(xs!n)";
   648 by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
   649 by (Blast_tac 1);
   650 qed_spec_mp "list_ball_nth";
   651 
   652 Goal "n < length xs ==> xs!n : set xs";
   653 by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
   654 by (Blast_tac 1);
   655 qed_spec_mp "nth_mem";
   656 Addsimps [nth_mem];
   657 
   658 Goal "(!i. i < length xs --> P(xs!i)) --> (!x : set xs. P x)";
   659 by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
   660 by (Blast_tac 1);
   661 qed_spec_mp "all_nth_imp_all_set";
   662 
   663 Goal "(!x : set xs. P x) = (!i. i<length xs --> P (xs ! i))";
   664 by (simp_tac (simpset() addsimps [set_conv_nth]) 1);
   665 by (Blast_tac 1);
   666 qed_spec_mp "all_set_conv_all_nth";
   667 
   668 
   669 (** list update **)
   670 
   671 section "list update";
   672 
   673 Goal "!i. length(xs[i:=x]) = length xs";
   674 by (induct_tac "xs" 1);
   675 by (Simp_tac 1);
   676 by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
   677 qed_spec_mp "length_list_update";
   678 Addsimps [length_list_update];
   679 
   680 Goal "!i j. i < length xs  --> (xs[i:=x])!j = (if i=j then x else xs!j)";
   681 by (induct_tac "xs" 1);
   682  by (Simp_tac 1);
   683 by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
   684 qed_spec_mp "nth_list_update";
   685 
   686 Goal "i < length xs  ==> (xs[i:=x])!i = x";
   687 by (asm_simp_tac (simpset() addsimps [nth_list_update]) 1);
   688 qed "nth_list_update_eq";
   689 Addsimps [nth_list_update_eq];
   690 
   691 Goal "!i j. i ~= j --> xs[i:=x]!j = xs!j";
   692 by (induct_tac "xs" 1);
   693  by (Simp_tac 1);
   694 by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
   695 qed_spec_mp "nth_list_update_neq";
   696 Addsimps [nth_list_update_neq];
   697 
   698 Goal "!i. i < size xs --> xs[i:=x, i:=y] = xs[i:=y]";
   699 by (induct_tac "xs" 1);
   700  by (Simp_tac 1);
   701 by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
   702 qed_spec_mp "list_update_overwrite";
   703 Addsimps [list_update_overwrite];
   704 
   705 Goal "!i < length xs. (xs[i := x] = xs) = (xs!i = x)";
   706 by (induct_tac "xs" 1);
   707  by (Simp_tac 1);
   708 by (simp_tac (simpset() addsplits [nat.split]) 1);
   709 by (Blast_tac 1);
   710 qed_spec_mp "list_update_same_conv";
   711 
   712 Goal "!i xy xs. length xs = length ys --> \
   713 \     (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])";
   714 by (induct_tac "ys" 1);
   715  by Auto_tac;
   716 by (case_tac "xs" 1);
   717  by (auto_tac (claset(), simpset() addsplits [nat.split]));
   718 qed_spec_mp "update_zip";
   719 
   720 Goal "!i. set(xs[i:=x]) <= insert x (set xs)";
   721 by (induct_tac "xs" 1);
   722  by (asm_full_simp_tac (simpset() addsimps []) 1);
   723 by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
   724 by (Fast_tac  1);
   725 qed_spec_mp "set_update_subset_insert";
   726 
   727 Goal "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A";
   728 by(fast_tac (claset() addSDs [set_update_subset_insert RS subsetD]) 1);
   729 qed "set_update_subsetI";
   730 
   731 (** last & butlast **)
   732 
   733 section "last / butlast";
   734 
   735 Goal "last(xs@[x]) = x";
   736 by (induct_tac "xs" 1);
   737 by Auto_tac;
   738 qed "last_snoc";
   739 Addsimps [last_snoc];
   740 
   741 Goal "butlast(xs@[x]) = xs";
   742 by (induct_tac "xs" 1);
   743 by Auto_tac;
   744 qed "butlast_snoc";
   745 Addsimps [butlast_snoc];
   746 
   747 Goal "length(butlast xs) = length xs - 1";
   748 by (rev_induct_tac "xs" 1);
   749 by Auto_tac;
   750 qed "length_butlast";
   751 Addsimps [length_butlast];
   752 
   753 Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
   754 by (induct_tac "xs" 1);
   755 by Auto_tac;
   756 qed_spec_mp "butlast_append";
   757 
   758 Goal "xs ~= [] --> butlast xs @ [last xs] = xs";
   759 by (induct_tac "xs" 1);
   760 by (ALLGOALS Asm_simp_tac);
   761 qed_spec_mp "append_butlast_last_id";
   762 Addsimps [append_butlast_last_id];
   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 (case_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 (case_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 (case_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 (case_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 (case_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 (case_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 (case_tac "xs" 1);
   850  by Auto_tac;
   851 by (case_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 (case_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 (case_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 (case_tac "xs" 1);
   875  by Auto_tac;
   876 qed_spec_mp "append_take_drop_id";
   877 Addsimps [append_take_drop_id];
   878 
   879 Goal "!xs. take n (map f xs) = map f (take n xs)"; 
   880 by (induct_tac "n" 1);
   881  by Auto_tac;
   882 by (case_tac "xs" 1);
   883  by Auto_tac;
   884 qed_spec_mp "take_map"; 
   885 
   886 Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
   887 by (induct_tac "n" 1);
   888  by Auto_tac;
   889 by (case_tac "xs" 1);
   890  by Auto_tac;
   891 qed_spec_mp "drop_map";
   892 
   893 Goal "!n i. i < n --> (take n xs)!i = xs!i";
   894 by (induct_tac "xs" 1);
   895  by Auto_tac;
   896 by (case_tac "n" 1);
   897  by (Blast_tac 1);
   898 by (case_tac "i" 1);
   899  by Auto_tac;
   900 qed_spec_mp "nth_take";
   901 Addsimps [nth_take];
   902 
   903 Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
   904 by (induct_tac "n" 1);
   905  by Auto_tac;
   906 by (case_tac "xs" 1);
   907  by Auto_tac;
   908 qed_spec_mp "nth_drop";
   909 Addsimps [nth_drop];
   910 
   911 
   912 Goal
   913  "!zs. (xs@ys = zs) = (xs = take (length xs) zs & ys = drop (length xs) zs)";
   914 by (induct_tac "xs" 1);
   915  by (Simp_tac 1);
   916 by (Asm_full_simp_tac 1);
   917 by (Clarify_tac 1);
   918 by (case_tac "zs" 1);
   919 by (Auto_tac);
   920 qed_spec_mp "append_eq_conv_conj";
   921 
   922 (** takeWhile & dropWhile **)
   923 
   924 section "takeWhile & dropWhile";
   925 
   926 Goal "takeWhile P xs @ dropWhile P xs = xs";
   927 by (induct_tac "xs" 1);
   928 by Auto_tac;
   929 qed "takeWhile_dropWhile_id";
   930 Addsimps [takeWhile_dropWhile_id];
   931 
   932 Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
   933 by (induct_tac "xs" 1);
   934 by Auto_tac;
   935 bind_thm("takeWhile_append1", conjI RS (result() RS mp));
   936 Addsimps [takeWhile_append1];
   937 
   938 Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
   939 by (induct_tac "xs" 1);
   940 by Auto_tac;
   941 bind_thm("takeWhile_append2", ballI RS (result() RS mp));
   942 Addsimps [takeWhile_append2];
   943 
   944 Goal "~P(x) ==> takeWhile P (xs @ (x#l)) = takeWhile P xs";
   945 by (induct_tac "xs" 1);
   946 by Auto_tac;
   947 qed "takeWhile_tail";
   948 
   949 Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
   950 by (induct_tac "xs" 1);
   951 by Auto_tac;
   952 bind_thm("dropWhile_append1", conjI RS (result() RS mp));
   953 Addsimps [dropWhile_append1];
   954 
   955 Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
   956 by (induct_tac "xs" 1);
   957 by Auto_tac;
   958 bind_thm("dropWhile_append2", ballI RS (result() RS mp));
   959 Addsimps [dropWhile_append2];
   960 
   961 Goal "x:set(takeWhile P xs) --> x:set xs & P x";
   962 by (induct_tac "xs" 1);
   963 by Auto_tac;
   964 qed_spec_mp"set_take_whileD";
   965 
   966 (** zip **)
   967 section "zip";
   968 
   969 Goal "zip [] ys = []";
   970 by (induct_tac "ys" 1);
   971 by Auto_tac;
   972 qed "zip_Nil";
   973 Addsimps [zip_Nil];
   974 
   975 Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys";
   976 by (Simp_tac 1);
   977 qed "zip_Cons_Cons";
   978 Addsimps [zip_Cons_Cons];
   979 
   980 Delsimps(tl (thms"zip.simps"));
   981 
   982 Goal "!xs. length (zip xs ys) = min (length xs) (length ys)";
   983 by (induct_tac "ys" 1);
   984  by (Simp_tac 1);
   985 by (Clarify_tac 1);
   986 by (case_tac "xs" 1);
   987  by (Auto_tac);
   988 qed_spec_mp "length_zip";
   989 Addsimps [length_zip];
   990 
   991 Goal
   992  "!xs. zip (xs@ys) zs = \
   993 \      zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)";
   994 by (induct_tac "zs" 1);
   995  by (Simp_tac 1);
   996 by (Clarify_tac 1);
   997 by (case_tac "xs" 1);
   998  by (Asm_simp_tac 1);
   999 by (Asm_simp_tac 1);
  1000 qed_spec_mp "zip_append1";
  1001 
  1002 Goal
  1003  "!ys. zip xs (ys@zs) = \
  1004 \      zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs";
  1005 by (induct_tac "xs" 1);
  1006  by (Simp_tac 1);
  1007 by (Clarify_tac 1);
  1008 by (case_tac "ys" 1);
  1009  by (Asm_simp_tac 1);
  1010 by (Asm_simp_tac 1);
  1011 qed_spec_mp "zip_append2";
  1012 
  1013 Goal
  1014  "[| length xs = length us; length ys = length vs |] ==> \
  1015 \ zip (xs@ys) (us@vs) = zip xs us @ zip ys vs";
  1016 by (asm_simp_tac (simpset() addsimps [zip_append1]) 1);
  1017 qed_spec_mp "zip_append";
  1018 Addsimps [zip_append];
  1019 
  1020 Goal "!xs. length xs = length ys --> zip (rev xs) (rev ys) = rev (zip xs ys)";
  1021 by (induct_tac "ys" 1);
  1022  by (Asm_full_simp_tac 1);
  1023 by (Asm_full_simp_tac 1);
  1024 by (Clarify_tac 1);
  1025 by (case_tac "xs" 1);
  1026  by (Auto_tac);
  1027 qed_spec_mp "zip_rev";
  1028 
  1029 
  1030 Goal
  1031 "!i xs. i < length xs --> i < length ys --> (zip xs ys)!i = (xs!i, ys!i)";
  1032 by (induct_tac "ys" 1);
  1033  by (Simp_tac 1);
  1034 by (Clarify_tac 1);
  1035 by (case_tac "xs" 1);
  1036  by (Auto_tac);
  1037 by (asm_full_simp_tac (simpset() addsimps (thms"nth.simps") addsplits [nat.split]) 1);
  1038 qed_spec_mp "nth_zip";
  1039 Addsimps [nth_zip];
  1040 
  1041 Goal "set(zip xs ys) = {(xs!i,ys!i) |i. i < min (length xs) (length ys)}";
  1042 by (simp_tac (simpset() addsimps [set_conv_nth]addcongs [rev_conj_cong]) 1);
  1043 qed_spec_mp "set_zip";
  1044 
  1045 Goal
  1046  "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]";
  1047 by (rtac sym 1);
  1048 by (asm_simp_tac (simpset() addsimps [update_zip]) 1);
  1049 qed_spec_mp "zip_update";
  1050 
  1051 Goal "!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)";
  1052 by (induct_tac "i" 1);
  1053  by (Auto_tac);
  1054 by (case_tac "j" 1);
  1055  by (Auto_tac);
  1056 qed "zip_replicate";
  1057 Addsimps [zip_replicate];
  1058 
  1059 (** list_all2 **)
  1060 section "list_all2";
  1061 
  1062 Goalw [list_all2_def] "list_all2 P xs ys ==> length xs = length ys";
  1063 by (Asm_simp_tac 1);
  1064 qed "list_all2_lengthD";
  1065 
  1066 Goalw [list_all2_def] "list_all2 P [] ys = (ys=[])";
  1067 by (Simp_tac 1);
  1068 qed "list_all2_Nil";
  1069 AddIffs [list_all2_Nil];
  1070 
  1071 Goalw [list_all2_def] "list_all2 P xs [] = (xs=[])";
  1072 by (Simp_tac 1);
  1073 qed "list_all2_Nil2";
  1074 AddIffs [list_all2_Nil2];
  1075 
  1076 Goalw [list_all2_def]
  1077  "list_all2 P (x#xs) (y#ys) = (P x y & list_all2 P xs ys)";
  1078 by (Auto_tac);
  1079 qed "list_all2_Cons";
  1080 AddIffs[list_all2_Cons];
  1081 
  1082 Goalw [list_all2_def]
  1083  "list_all2 P (x#xs) ys = (? z zs. ys = z#zs & P x z & list_all2 P xs zs)";
  1084 by (case_tac "ys" 1);
  1085 by (Auto_tac);
  1086 qed "list_all2_Cons1";
  1087 
  1088 Goalw [list_all2_def]
  1089  "list_all2 P xs (y#ys) = (? z zs. xs = z#zs & P z y & list_all2 P zs ys)";
  1090 by (case_tac "xs" 1);
  1091 by (Auto_tac);
  1092 qed "list_all2_Cons2";
  1093 
  1094 Goalw [list_all2_def]
  1095  "list_all2 P (xs@ys) zs = \
  1096 \ (EX us vs. zs = us@vs & length us = length xs & length vs = length ys & \
  1097 \            list_all2 P xs us & list_all2 P ys vs)";
  1098 by (simp_tac (simpset() addsimps [zip_append1]) 1);
  1099 by (rtac iffI 1);
  1100  by (res_inst_tac [("x","take (length xs) zs")] exI 1);
  1101  by (res_inst_tac [("x","drop (length xs) zs")] exI 1);
  1102  by (force_tac (claset(),
  1103 		simpset() addsplits [nat_diff_split] addsimps [min_def]) 1);
  1104 by (Clarify_tac 1);
  1105 by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1);
  1106 qed "list_all2_append1";
  1107 
  1108 Goalw [list_all2_def]
  1109  "list_all2 P xs (ys@zs) = \
  1110 \ (EX us vs. xs = us@vs & length us = length ys & length vs = length zs & \
  1111 \            list_all2 P us ys & list_all2 P vs zs)";
  1112 by (simp_tac (simpset() addsimps [zip_append2]) 1);
  1113 by (rtac iffI 1);
  1114  by (res_inst_tac [("x","take (length ys) xs")] exI 1);
  1115  by (res_inst_tac [("x","drop (length ys) xs")] exI 1);
  1116  by (force_tac (claset(),
  1117 		simpset() addsplits [nat_diff_split] addsimps [min_def]) 1);
  1118 by (Clarify_tac 1);
  1119 by (asm_full_simp_tac (simpset() addsimps [ball_Un]) 1);
  1120 qed "list_all2_append2";
  1121 
  1122 Goalw [list_all2_def]
  1123   "list_all2 P xs ys = \
  1124 \  (length xs = length ys & (!i<length xs. P (xs!i) (ys!i)))";
  1125 by (force_tac (claset(), simpset() addsimps [set_zip]) 1);
  1126 qed "list_all2_conv_all_nth";
  1127 
  1128 Goal "ALL a b c. P1 a b --> P2 b c --> P3 a c ==> \
  1129 \ ALL bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs";
  1130 by (induct_tac "as" 1);
  1131 by  (Simp_tac 1);
  1132 by (rtac allI 1);
  1133 by (induct_tac "bs" 1);
  1134 by  (Simp_tac 1);
  1135 by (rtac allI 1);
  1136 by (induct_tac "cs" 1);
  1137 by Auto_tac;
  1138 qed_spec_mp "list_all2_trans";
  1139 
  1140 
  1141 section "foldl";
  1142 
  1143 Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
  1144 by (induct_tac "xs" 1);
  1145 by Auto_tac;
  1146 qed_spec_mp "foldl_append";
  1147 Addsimps [foldl_append];
  1148 
  1149 (* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
  1150    because it requires an additional transitivity step
  1151 *)
  1152 Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
  1153 by (induct_tac "ns" 1);
  1154 by Auto_tac;
  1155 qed_spec_mp "start_le_sum";
  1156 
  1157 Goal "!!n::nat. n : set ns ==> n <= foldl op+ 0 ns";
  1158 by (force_tac (claset() addIs [start_le_sum],
  1159               simpset() addsimps [in_set_conv_decomp]) 1);
  1160 qed "elem_le_sum";
  1161 
  1162 Goal "!m::nat. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
  1163 by (induct_tac "ns" 1);
  1164 by Auto_tac;
  1165 qed_spec_mp "sum_eq_0_conv";
  1166 AddIffs [sum_eq_0_conv];
  1167 
  1168 (** upto **)
  1169 
  1170 (* Does not terminate! *)
  1171 Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
  1172 by (induct_tac "j" 1);
  1173 by Auto_tac;
  1174 qed "upt_rec";
  1175 
  1176 Goal "j<=i ==> [i..j(] = []";
  1177 by (stac upt_rec 1);
  1178 by (Asm_simp_tac 1);
  1179 qed "upt_conv_Nil";
  1180 Addsimps [upt_conv_Nil];
  1181 
  1182 (*Only needed if upt_Suc is deleted from the simpset*)
  1183 Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
  1184 by (Asm_simp_tac 1);
  1185 qed "upt_Suc_append";
  1186 
  1187 Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
  1188 by (rtac trans 1);
  1189 by (stac upt_rec 1);
  1190 by (rtac refl 2);
  1191 by (Asm_simp_tac 1);
  1192 qed "upt_conv_Cons";
  1193 
  1194 (*LOOPS as a simprule, since j<=j*)
  1195 Goal "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]";
  1196 by (induct_tac "k" 1);
  1197 by Auto_tac;
  1198 qed "upt_add_eq_append";
  1199 
  1200 Goal "length [i..j(] = j-i";
  1201 by (induct_tac "j" 1);
  1202  by (Simp_tac 1);
  1203 by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
  1204 qed "length_upt";
  1205 Addsimps [length_upt];
  1206 
  1207 Goal "i+k < j --> [i..j(] ! k = i+k";
  1208 by (induct_tac "j" 1);
  1209  by (asm_simp_tac (simpset() addsimps [less_Suc_eq, nth_append] 
  1210                              addsplits [nat_diff_split]) 2);
  1211 by (Simp_tac 1);
  1212 qed_spec_mp "nth_upt";
  1213 Addsimps [nth_upt];
  1214 
  1215 Goal "!i. i+m <= n --> take m [i..n(] = [i..i+m(]";
  1216 by (induct_tac "m" 1);
  1217  by (Simp_tac 1);
  1218 by (Clarify_tac 1);
  1219 by (stac upt_rec 1);
  1220 by (rtac sym 1);
  1221 by (stac upt_rec 1);
  1222 by (asm_simp_tac (simpset() delsimps (thms"upt.simps")) 1);
  1223 qed_spec_mp "take_upt";
  1224 Addsimps [take_upt];
  1225 
  1226 Goal "map Suc [m..n(] = [Suc m..n]";
  1227 by (induct_tac "n" 1);
  1228 by Auto_tac;
  1229 qed "map_Suc_upt";
  1230 
  1231 Goal "ALL i. i < n-m --> (map f [m..n(]) ! i = f(m+i)";
  1232 by (induct_thm_tac diff_induct "n m" 1);
  1233 by (stac (map_Suc_upt RS sym) 3);
  1234 by (auto_tac (claset(), simpset() addsimps [less_diff_conv, nth_upt]));
  1235 qed_spec_mp "nth_map_upt";
  1236 
  1237 Goal "ALL xs ys. k <= length xs --> k <= length ys -->  \
  1238 \        (ALL i. i < k --> xs!i = ys!i)  \
  1239 \     --> take k xs = take k ys";
  1240 by (induct_tac "k" 1);
  1241 by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq_0_disj, 
  1242 						all_conj_distrib])));
  1243 by (Clarify_tac 1);
  1244 (*Both lists must be non-empty*)
  1245 by (case_tac "xs" 1);
  1246 by (case_tac "ys" 2);
  1247 by (ALLGOALS Clarify_tac);
  1248 (*prenexing's needed, not miniscoping*)
  1249 by (ALLGOALS (full_simp_tac (simpset() addsimps (all_simps RL [sym])  
  1250                                        delsimps (all_simps))));
  1251 by (Blast_tac 1);
  1252 qed_spec_mp "nth_take_lemma";
  1253 
  1254 Goal "[| length xs = length ys;  \
  1255 \        ALL i. i < length xs --> xs!i = ys!i |]  \
  1256 \     ==> xs = ys";
  1257 by (forward_tac [[le_refl, eq_imp_le] MRS nth_take_lemma] 1);
  1258 by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [take_all])));
  1259 qed_spec_mp "nth_equalityI";
  1260 
  1261 (*The famous take-lemma*)
  1262 Goal "(ALL i. take i xs = take i ys) ==> xs = ys";
  1263 by (dres_inst_tac [("x", "max (length xs) (length ys)")] spec 1);
  1264 by (full_simp_tac (simpset() addsimps [le_max_iff_disj, take_all]) 1);
  1265 qed_spec_mp "take_equalityI";
  1266 
  1267 
  1268 (** nodups & remdups **)
  1269 section "nodups & remdups";
  1270 
  1271 Goal "set(remdups xs) = set xs";
  1272 by (induct_tac "xs" 1);
  1273  by (Simp_tac 1);
  1274 by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
  1275 qed "set_remdups";
  1276 Addsimps [set_remdups];
  1277 
  1278 Goal "nodups(remdups xs)";
  1279 by (induct_tac "xs" 1);
  1280 by Auto_tac;
  1281 qed "nodups_remdups";
  1282 
  1283 Goal "nodups xs --> nodups (filter P xs)";
  1284 by (induct_tac "xs" 1);
  1285 by Auto_tac;
  1286 qed_spec_mp "nodups_filter";
  1287 
  1288 (** replicate **)
  1289 section "replicate";
  1290 
  1291 Goal "length(replicate n x) = n";
  1292 by (induct_tac "n" 1);
  1293 by Auto_tac;
  1294 qed "length_replicate";
  1295 Addsimps [length_replicate];
  1296 
  1297 Goal "map f (replicate n x) = replicate n (f x)";
  1298 by (induct_tac "n" 1);
  1299 by Auto_tac;
  1300 qed "map_replicate";
  1301 Addsimps [map_replicate];
  1302 
  1303 Goal "(replicate n x) @ (x#xs) = x # replicate n x @ xs";
  1304 by (induct_tac "n" 1);
  1305 by Auto_tac;
  1306 qed "replicate_app_Cons_same";
  1307 
  1308 Goal "rev(replicate n x) = replicate n x";
  1309 by (induct_tac "n" 1);
  1310  by (Simp_tac 1);
  1311 by (asm_simp_tac (simpset() addsimps [replicate_app_Cons_same]) 1);
  1312 qed "rev_replicate";
  1313 Addsimps [rev_replicate];
  1314 
  1315 Goal "replicate (n+m) x = replicate n x @ replicate m x";
  1316 by (induct_tac "n" 1);
  1317 by Auto_tac;
  1318 qed "replicate_add";
  1319 
  1320 Goal"n ~= 0 --> hd(replicate n x) = x";
  1321 by (induct_tac "n" 1);
  1322 by Auto_tac;
  1323 qed_spec_mp "hd_replicate";
  1324 Addsimps [hd_replicate];
  1325 
  1326 Goal "n ~= 0 --> tl(replicate n x) = replicate (n - 1) x";
  1327 by (induct_tac "n" 1);
  1328 by Auto_tac;
  1329 qed_spec_mp "tl_replicate";
  1330 Addsimps [tl_replicate];
  1331 
  1332 Goal "n ~= 0 --> last(replicate n x) = x";
  1333 by (induct_tac "n" 1);
  1334 by Auto_tac;
  1335 qed_spec_mp "last_replicate";
  1336 Addsimps [last_replicate];
  1337 
  1338 Goal "!i. i<n --> (replicate n x)!i = x";
  1339 by (induct_tac "n" 1);
  1340  by (Simp_tac 1);
  1341 by (asm_simp_tac (simpset() addsimps [nth_Cons] addsplits [nat.split]) 1);
  1342 qed_spec_mp "nth_replicate";
  1343 Addsimps [nth_replicate];
  1344 
  1345 Goal "set(replicate (Suc n) x) = {x}";
  1346 by (induct_tac "n" 1);
  1347 by Auto_tac;
  1348 val lemma = result();
  1349 
  1350 Goal "n ~= 0 ==> set(replicate n x) = {x}";
  1351 by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
  1352 qed "set_replicate";
  1353 Addsimps [set_replicate];
  1354 
  1355 Goal "set(replicate n x) = (if n=0 then {} else {x})";
  1356 by (Auto_tac);
  1357 qed "set_replicate_conv_if";
  1358 
  1359 Goal "x : set(replicate n y) --> x=y";
  1360 by (asm_simp_tac (simpset() addsimps [set_replicate_conv_if]) 1);
  1361 qed_spec_mp "in_set_replicateD";
  1362 
  1363 
  1364 (*** Lexcicographic orderings on lists ***)
  1365 section"Lexcicographic orderings on lists";
  1366 
  1367 Goal "wf r ==> wf(lexn r n)";
  1368 by (induct_tac "n" 1);
  1369 by (Simp_tac 1);
  1370 by (Simp_tac 1);
  1371 by (rtac wf_subset 1);
  1372 by (rtac Int_lower1 2);
  1373 by (rtac wf_prod_fun_image 1);
  1374 by (rtac injI 2);
  1375 by Auto_tac;
  1376 qed "wf_lexn";
  1377 
  1378 Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
  1379 by (induct_tac "n" 1);
  1380 by Auto_tac;
  1381 qed_spec_mp "lexn_length";
  1382 
  1383 Goalw [lex_def] "wf r ==> wf(lex r)";
  1384 by (rtac wf_UN 1);
  1385 by (blast_tac (claset() addIs [wf_lexn]) 1);
  1386 by (Clarify_tac 1);
  1387 by (rename_tac "m n" 1);
  1388 by (subgoal_tac "m ~= n" 1);
  1389  by (Blast_tac 2);
  1390 by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
  1391 qed "wf_lex";
  1392 AddSIs [wf_lex];
  1393 
  1394 
  1395 Goal
  1396  "lexn r n = \
  1397 \ {(xs,ys). length xs = n & length ys = n & \
  1398 \           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
  1399 by (induct_tac "n" 1);
  1400  by (Simp_tac 1);
  1401  by (Blast_tac 1);
  1402 by (asm_full_simp_tac (simpset() addsimps [image_Collect, lex_prod_def]) 1);
  1403 by Auto_tac;
  1404   by (Blast_tac 1);
  1405  by (rename_tac "a xys x xs' y ys'" 1);
  1406  by (res_inst_tac [("x","a#xys")] exI 1);
  1407  by (Simp_tac 1);
  1408 by (case_tac "xys" 1);
  1409  by (ALLGOALS Asm_full_simp_tac);
  1410 by (Blast_tac 1);
  1411 qed "lexn_conv";
  1412 
  1413 Goalw [lex_def]
  1414  "lex r = \
  1415 \ {(xs,ys). length xs = length ys & \
  1416 \           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
  1417 by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1);
  1418 qed "lex_conv";
  1419 
  1420 Goalw [lexico_def] "wf r ==> wf(lexico r)";
  1421 by (Blast_tac 1);
  1422 qed "wf_lexico";
  1423 AddSIs [wf_lexico];
  1424 
  1425 Goalw [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
  1426 "lexico r = {(xs,ys). length xs < length ys | \
  1427 \                     length xs = length ys & (xs,ys) : lex r}";
  1428 by (Simp_tac 1);
  1429 qed "lexico_conv";
  1430 
  1431 Goal "([],ys) ~: lex r";
  1432 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1433 qed "Nil_notin_lex";
  1434 
  1435 Goal "(xs,[]) ~: lex r";
  1436 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1437 qed "Nil2_notin_lex";
  1438 
  1439 AddIffs [Nil_notin_lex,Nil2_notin_lex];
  1440 
  1441 Goal "((x#xs,y#ys) : lex r) = \
  1442 \     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
  1443 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1444 by (rtac iffI 1);
  1445  by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
  1446 by (Clarify_tac 1);
  1447 by (case_tac "xys" 1);
  1448 by (Asm_full_simp_tac 1);
  1449 by (Asm_full_simp_tac 1);
  1450 by (Blast_tac 1);
  1451 qed "Cons_in_lex";
  1452 AddIffs [Cons_in_lex];
  1453 
  1454 
  1455 (*** sublist (a generalization of nth to sets) ***)
  1456 
  1457 Goalw [sublist_def] "sublist l {} = []";
  1458 by Auto_tac;
  1459 qed "sublist_empty";
  1460 
  1461 Goalw [sublist_def] "sublist [] A = []";
  1462 by Auto_tac;
  1463 qed "sublist_nil";
  1464 
  1465 Goal "map fst [p:zip xs [i..i + length xs(] . snd p : A] =     \
  1466 \     map fst [p:zip xs [0..length xs(] . snd p + i : A]";
  1467 by (rev_induct_tac "xs" 1);
  1468  by (asm_simp_tac (simpset() addsimps [add_commute]) 2);
  1469 by (Simp_tac 1);
  1470 qed "sublist_shift_lemma";
  1471 
  1472 Goalw [sublist_def]
  1473      "sublist (l@l') A = sublist l A @ sublist l' {j. j + length l : A}";
  1474 by (rev_induct_tac "l'" 1);
  1475 by (Simp_tac 1);
  1476 by (asm_simp_tac (simpset() addsimps [inst "i" "0" upt_add_eq_append, 
  1477 	                              zip_append, sublist_shift_lemma]) 1);
  1478 by (asm_simp_tac (simpset() addsimps [add_commute]) 1);
  1479 qed "sublist_append";
  1480 
  1481 Addsimps [sublist_empty, sublist_nil];
  1482 
  1483 Goal "sublist (x#l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}";
  1484 by (rev_induct_tac "l" 1);
  1485  by (asm_simp_tac (simpset() delsimps [append_Cons]
  1486 	 		     addsimps [append_Cons RS sym, sublist_append]) 2);
  1487 by (simp_tac (simpset() addsimps [sublist_def]) 1);
  1488 qed "sublist_Cons";
  1489 
  1490 Goal "sublist [x] A = (if 0 : A then [x] else [])";
  1491 by (simp_tac (simpset() addsimps [sublist_Cons]) 1);
  1492 qed "sublist_singleton";
  1493 Addsimps [sublist_singleton];
  1494 
  1495 Goal "sublist l {..n(} = take n l";
  1496 by (rev_induct_tac "l" 1);
  1497  by (asm_simp_tac (simpset() addsplits [nat_diff_split]
  1498                              addsimps [sublist_append]) 2);
  1499 by (Simp_tac 1);
  1500 qed "sublist_upt_eq_take";
  1501 Addsimps [sublist_upt_eq_take];
  1502 
  1503 
  1504 (*** Versions of some theorems above using binary numerals ***)
  1505 
  1506 AddIffs (map rename_numerals
  1507 	  [length_0_conv, length_greater_0_conv, sum_eq_0_conv]);
  1508 
  1509 Goal "take n (x#xs) = (if n = Numeral0 then [] else x # take (n - Numeral1) xs)";
  1510 by (case_tac "n" 1);
  1511 by (ALLGOALS 
  1512     (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
  1513 qed "take_Cons'";
  1514 
  1515 Goal "drop n (x#xs) = (if n = Numeral0 then x#xs else drop (n - Numeral1) xs)";
  1516 by (case_tac "n" 1);
  1517 by (ALLGOALS
  1518     (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
  1519 qed "drop_Cons'";
  1520 
  1521 Goal "(x#xs)!n = (if n = Numeral0 then x else xs!(n - Numeral1))";
  1522 by (case_tac "n" 1);
  1523 by (ALLGOALS
  1524     (asm_simp_tac (simpset() addsimps [numeral_0_eq_0, numeral_1_eq_1])));
  1525 qed "nth_Cons'";
  1526 
  1527 Addsimps (map (inst "n" "number_of ?v") [take_Cons', drop_Cons', nth_Cons']);
  1528