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