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