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