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