src/HOL/List.ML
author pusch
Thu Apr 01 18:42:48 1999 +0200 (1999-04-01)
changeset 6408 5b443d6331ed
parent 6394 3d9fd50fcc43
child 6433 228237ec56e5
permissions -rw-r--r--
new definition for nth.
added warnings, if primrec definition is deleted from simpset.
     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 "xs ~= [] ==> 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.op #",_) $ _ $ xs) =
   257       (case xs of Const("List.list.[]",_) => cons | _ => last xs)
   258   | last (Const("List.op @",_) $ _ $ ys) = last ys
   259   | last t = t;
   260 
   261 fun list1 (Const("List.list.op #",_) $ _ $ Const("List.list.[]",_)) = true
   262   | list1 _ = false;
   263 
   264 fun butlast ((cons as Const("List.list.op #",_) $ x) $ xs) =
   265       (case xs of Const("List.list.[]",_) => xs | _ => cons $ butlast xs)
   266   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
   267   | butlast xs = Const("List.list.[]",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 (rtac impI 1);
   338 by (hyp_subst_tac 1);
   339 by (induct_tac "ys" 1);
   340 by Auto_tac;
   341 val lemma = result();
   342 bind_thm("map_cong",impI RSN (2,allI RSN (2,lemma RS mp RS mp)));
   343 
   344 Goal "(map f xs = []) = (xs = [])";
   345 by (induct_tac "xs" 1);
   346 by Auto_tac;
   347 qed "map_is_Nil_conv";
   348 AddIffs [map_is_Nil_conv];
   349 
   350 Goal "([] = map f xs) = (xs = [])";
   351 by (induct_tac "xs" 1);
   352 by Auto_tac;
   353 qed "Nil_is_map_conv";
   354 AddIffs [Nil_is_map_conv];
   355 
   356 
   357 (** rev **)
   358 
   359 section "rev";
   360 
   361 Goal "rev(xs@ys) = rev(ys) @ rev(xs)";
   362 by (induct_tac "xs" 1);
   363 by Auto_tac;
   364 qed "rev_append";
   365 Addsimps[rev_append];
   366 
   367 Goal "rev(rev l) = l";
   368 by (induct_tac "l" 1);
   369 by Auto_tac;
   370 qed "rev_rev_ident";
   371 Addsimps[rev_rev_ident];
   372 
   373 Goal "(rev xs = []) = (xs = [])";
   374 by (induct_tac "xs" 1);
   375 by Auto_tac;
   376 qed "rev_is_Nil_conv";
   377 AddIffs [rev_is_Nil_conv];
   378 
   379 Goal "([] = rev xs) = (xs = [])";
   380 by (induct_tac "xs" 1);
   381 by Auto_tac;
   382 qed "Nil_is_rev_conv";
   383 AddIffs [Nil_is_rev_conv];
   384 
   385 val prems = Goal "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs";
   386 by (stac (rev_rev_ident RS sym) 1);
   387 by (res_inst_tac [("list", "rev xs")] list.induct 1);
   388 by (ALLGOALS Simp_tac);
   389 by (resolve_tac prems 1);
   390 by (eresolve_tac prems 1);
   391 qed "rev_induct";
   392 
   393 fun rev_induct_tac xs = res_inst_tac [("xs",xs)] rev_induct;
   394 
   395 Goal  "(xs = [] --> P) -->  (!ys y. xs = ys@[y] --> P) --> P";
   396 by (res_inst_tac [("xs","xs")] rev_induct 1);
   397 by Auto_tac;
   398 bind_thm ("rev_exhaust",
   399   impI RSN (2,allI RSN (2,allI RSN (2,impI RS (result() RS mp RS mp)))));
   400 
   401 
   402 (** set **)
   403 
   404 section "set";
   405 
   406 qed_goal "finite_set" thy "finite (set xs)" 
   407 	(K [induct_tac "xs" 1, Auto_tac]);
   408 Addsimps[finite_set];
   409 AddSIs[finite_set];
   410 
   411 Goal "set (xs@ys) = (set xs Un set ys)";
   412 by (induct_tac "xs" 1);
   413 by Auto_tac;
   414 qed "set_append";
   415 Addsimps[set_append];
   416 
   417 Goal "set l <= set (x#l)";
   418 by Auto_tac;
   419 qed "set_subset_Cons";
   420 
   421 Goal "(set xs = {}) = (xs = [])";
   422 by (induct_tac "xs" 1);
   423 by Auto_tac;
   424 qed "set_empty";
   425 Addsimps [set_empty];
   426 
   427 Goal "set(rev xs) = set(xs)";
   428 by (induct_tac "xs" 1);
   429 by Auto_tac;
   430 qed "set_rev";
   431 Addsimps [set_rev];
   432 
   433 Goal "set(map f xs) = f``(set xs)";
   434 by (induct_tac "xs" 1);
   435 by Auto_tac;
   436 qed "set_map";
   437 Addsimps [set_map];
   438 
   439 Goal "(x : set (filter P xs)) = (x : set xs & P x)";
   440 by (induct_tac "xs" 1);
   441 by Auto_tac;
   442 qed "in_set_filter";
   443 Addsimps [in_set_filter];
   444 
   445 Goal "(x : set xs) = (? ys zs. xs = ys@x#zs)";
   446 by (induct_tac "xs" 1);
   447  by (Simp_tac 1);
   448 by (Asm_simp_tac 1);
   449 by (rtac iffI 1);
   450 by (blast_tac (claset() addIs [eq_Nil_appendI,Cons_eq_appendI]) 1);
   451 by (REPEAT(etac exE 1));
   452 by (exhaust_tac "ys" 1);
   453 by Auto_tac;
   454 qed "in_set_conv_decomp";
   455 
   456 (* eliminate `lists' in favour of `set' *)
   457 
   458 Goal "(xs : lists A) = (!x : set xs. x : A)";
   459 by (induct_tac "xs" 1);
   460 by Auto_tac;
   461 qed "in_lists_conv_set";
   462 
   463 bind_thm("in_listsD",in_lists_conv_set RS iffD1);
   464 AddSDs [in_listsD];
   465 bind_thm("in_listsI",in_lists_conv_set RS iffD2);
   466 AddSIs [in_listsI];
   467 
   468 (** mem **)
   469  
   470 section "mem";
   471 
   472 Goal "(x mem xs) = (x: set xs)";
   473 by (induct_tac "xs" 1);
   474 by Auto_tac;
   475 qed "set_mem_eq";
   476 
   477 
   478 (** list_all **)
   479 
   480 section "list_all";
   481 
   482 Goal "list_all P xs = (!x:set xs. P x)";
   483 by (induct_tac "xs" 1);
   484 by Auto_tac;
   485 qed "list_all_conv";
   486 
   487 Goal "list_all P (xs@ys) = (list_all P xs & list_all P ys)";
   488 by (induct_tac "xs" 1);
   489 by Auto_tac;
   490 qed "list_all_append";
   491 Addsimps [list_all_append];
   492 
   493 
   494 (** filter **)
   495 
   496 section "filter";
   497 
   498 Goal "filter P (xs@ys) = filter P xs @ filter P ys";
   499 by (induct_tac "xs" 1);
   500 by Auto_tac;
   501 qed "filter_append";
   502 Addsimps [filter_append];
   503 
   504 Goal "filter (%x. True) xs = xs";
   505 by (induct_tac "xs" 1);
   506 by Auto_tac;
   507 qed "filter_True";
   508 Addsimps [filter_True];
   509 
   510 Goal "filter (%x. False) xs = []";
   511 by (induct_tac "xs" 1);
   512 by Auto_tac;
   513 qed "filter_False";
   514 Addsimps [filter_False];
   515 
   516 Goal "length (filter P xs) <= length xs";
   517 by (induct_tac "xs" 1);
   518 by Auto_tac;
   519 qed "length_filter";
   520 Addsimps[length_filter];
   521 
   522 Goal "set (filter P xs) <= set xs";
   523 by Auto_tac;
   524 qed "filter_is_subset";
   525 Addsimps [filter_is_subset];
   526 
   527 
   528 section "concat";
   529 
   530 Goal  "concat(xs@ys) = concat(xs)@concat(ys)";
   531 by (induct_tac "xs" 1);
   532 by Auto_tac;
   533 qed"concat_append";
   534 Addsimps [concat_append];
   535 
   536 Goal "(concat xss = []) = (!xs:set xss. xs=[])";
   537 by (induct_tac "xss" 1);
   538 by Auto_tac;
   539 qed "concat_eq_Nil_conv";
   540 AddIffs [concat_eq_Nil_conv];
   541 
   542 Goal "([] = concat xss) = (!xs:set xss. xs=[])";
   543 by (induct_tac "xss" 1);
   544 by Auto_tac;
   545 qed "Nil_eq_concat_conv";
   546 AddIffs [Nil_eq_concat_conv];
   547 
   548 Goal  "set(concat xs) = Union(set `` set xs)";
   549 by (induct_tac "xs" 1);
   550 by Auto_tac;
   551 qed"set_concat";
   552 Addsimps [set_concat];
   553 
   554 Goal "map f (concat xs) = concat (map (map f) xs)"; 
   555 by (induct_tac "xs" 1);
   556 by Auto_tac;
   557 qed "map_concat";
   558 
   559 Goal "filter p (concat xs) = concat (map (filter p) xs)"; 
   560 by (induct_tac "xs" 1);
   561 by Auto_tac;
   562 qed"filter_concat"; 
   563 
   564 Goal "rev(concat xs) = concat (map rev (rev xs))";
   565 by (induct_tac "xs" 1);
   566 by Auto_tac;
   567 qed "rev_concat";
   568 
   569 (** nth **)
   570 
   571 section "nth";
   572 
   573 Goal "(x#xs)!0 = x";
   574 by Auto_tac;
   575 qed "nth_Cons_0";
   576 Addsimps [nth_Cons_0];
   577 
   578 Goal "(x#xs)!(Suc n) = xs!n";
   579 by Auto_tac;
   580 qed "nth_Cons_Suc";
   581 Addsimps [nth_Cons_Suc];
   582 
   583 Delsimps (thms "nth.simps");
   584 
   585 Goal "!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))";
   586 by (induct_tac "xs" 1);
   587  by (Asm_simp_tac 1);
   588  by (rtac allI 1);
   589  by (exhaust_tac "n" 1);
   590   by Auto_tac;
   591 qed_spec_mp "nth_append";
   592 
   593 Goal "!n. n < length xs --> (map f xs)!n = f(xs!n)";
   594 by (induct_tac "xs" 1);
   595 (* case [] *)
   596 by (Asm_full_simp_tac 1);
   597 (* case x#xl *)
   598 by (rtac allI 1);
   599 by (induct_tac "n" 1);
   600 by Auto_tac;
   601 qed_spec_mp "nth_map";
   602 Addsimps [nth_map];
   603 
   604 Goal "!n. n < length xs --> Ball (set xs) P --> P(xs!n)";
   605 by (induct_tac "xs" 1);
   606 (* case [] *)
   607 by (Simp_tac 1);
   608 (* case x#xl *)
   609 by (rtac allI 1);
   610 by (induct_tac "n" 1);
   611 by Auto_tac;
   612 qed_spec_mp "list_ball_nth";
   613 
   614 Goal "!n. n < length xs --> xs!n : set xs";
   615 by (induct_tac "xs" 1);
   616 (* case [] *)
   617 by (Simp_tac 1);
   618 (* case x#xl *)
   619 by (rtac allI 1);
   620 by (induct_tac "n" 1);
   621 (* case 0 *)
   622 by (Asm_full_simp_tac 1);
   623 (* case Suc x *)
   624 by (Asm_full_simp_tac 1);
   625 qed_spec_mp "nth_mem";
   626 Addsimps [nth_mem];
   627 
   628 
   629 (** list update **)
   630 
   631 section "list update";
   632 
   633 Goal "!i. length(xs[i:=x]) = length xs";
   634 by (induct_tac "xs" 1);
   635 by (Simp_tac 1);
   636 by (asm_full_simp_tac (simpset() addsplits [nat.split]) 1);
   637 qed_spec_mp "length_list_update";
   638 Addsimps [length_list_update];
   639 
   640 Goal "!i j. i < length xs  --> (xs[i:=x])!j = (if i=j then x else xs!j)";
   641 by (induct_tac "xs" 1);
   642  by (Simp_tac 1);
   643 by (auto_tac (claset(), simpset() addsimps [nth_Cons] addsplits [nat.split]));
   644 qed_spec_mp "nth_list_update";
   645 
   646 
   647 (** last & butlast **)
   648 
   649 section "last / butlast";
   650 
   651 Goal "last(xs@[x]) = x";
   652 by (induct_tac "xs" 1);
   653 by Auto_tac;
   654 qed "last_snoc";
   655 Addsimps [last_snoc];
   656 
   657 Goal "butlast(xs@[x]) = xs";
   658 by (induct_tac "xs" 1);
   659 by Auto_tac;
   660 qed "butlast_snoc";
   661 Addsimps [butlast_snoc];
   662 
   663 Goal "length(butlast xs) = length xs - 1";
   664 by (res_inst_tac [("xs","xs")] rev_induct 1);
   665 by Auto_tac;
   666 qed "length_butlast";
   667 Addsimps [length_butlast];
   668 
   669 Goal "!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)";
   670 by (induct_tac "xs" 1);
   671 by Auto_tac;
   672 qed_spec_mp "butlast_append";
   673 
   674 Goal "x:set(butlast xs) --> x:set xs";
   675 by (induct_tac "xs" 1);
   676 by Auto_tac;
   677 qed_spec_mp "in_set_butlastD";
   678 
   679 Goal "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))";
   680 by (auto_tac (claset() addDs [in_set_butlastD],
   681 	      simpset() addsimps [butlast_append]));
   682 qed "in_set_butlast_appendI";
   683 
   684 (** take  & drop **)
   685 section "take & drop";
   686 
   687 Goal "take 0 xs = []";
   688 by (induct_tac "xs" 1);
   689 by Auto_tac;
   690 qed "take_0";
   691 
   692 Goal "drop 0 xs = xs";
   693 by (induct_tac "xs" 1);
   694 by Auto_tac;
   695 qed "drop_0";
   696 
   697 Goal "take (Suc n) (x#xs) = x # take n xs";
   698 by (Simp_tac 1);
   699 qed "take_Suc_Cons";
   700 
   701 Goal "drop (Suc n) (x#xs) = drop n xs";
   702 by (Simp_tac 1);
   703 qed "drop_Suc_Cons";
   704 
   705 Delsimps [take_Cons,drop_Cons];
   706 Addsimps [take_0,take_Suc_Cons,drop_0,drop_Suc_Cons];
   707 
   708 Goal "!xs. length(take n xs) = min (length xs) n";
   709 by (induct_tac "n" 1);
   710  by Auto_tac;
   711 by (exhaust_tac "xs" 1);
   712  by Auto_tac;
   713 qed_spec_mp "length_take";
   714 Addsimps [length_take];
   715 
   716 Goal "!xs. length(drop n xs) = (length xs - n)";
   717 by (induct_tac "n" 1);
   718  by Auto_tac;
   719 by (exhaust_tac "xs" 1);
   720  by Auto_tac;
   721 qed_spec_mp "length_drop";
   722 Addsimps [length_drop];
   723 
   724 Goal "!xs. length xs <= n --> take n xs = xs";
   725 by (induct_tac "n" 1);
   726  by Auto_tac;
   727 by (exhaust_tac "xs" 1);
   728  by Auto_tac;
   729 qed_spec_mp "take_all";
   730 
   731 Goal "!xs. length xs <= n --> drop n xs = []";
   732 by (induct_tac "n" 1);
   733  by Auto_tac;
   734 by (exhaust_tac "xs" 1);
   735  by Auto_tac;
   736 qed_spec_mp "drop_all";
   737 
   738 Goal "!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)";
   739 by (induct_tac "n" 1);
   740  by Auto_tac;
   741 by (exhaust_tac "xs" 1);
   742  by Auto_tac;
   743 qed_spec_mp "take_append";
   744 Addsimps [take_append];
   745 
   746 Goal "!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"; 
   747 by (induct_tac "n" 1);
   748  by Auto_tac;
   749 by (exhaust_tac "xs" 1);
   750  by Auto_tac;
   751 qed_spec_mp "drop_append";
   752 Addsimps [drop_append];
   753 
   754 Goal "!xs n. take n (take m xs) = take (min n m) xs"; 
   755 by (induct_tac "m" 1);
   756  by Auto_tac;
   757 by (exhaust_tac "xs" 1);
   758  by Auto_tac;
   759 by (exhaust_tac "na" 1);
   760  by Auto_tac;
   761 qed_spec_mp "take_take";
   762 
   763 Goal "!xs. drop n (drop m xs) = drop (n + m) xs"; 
   764 by (induct_tac "m" 1);
   765  by Auto_tac;
   766 by (exhaust_tac "xs" 1);
   767  by Auto_tac;
   768 qed_spec_mp "drop_drop";
   769 
   770 Goal "!xs n. take n (drop m xs) = drop m (take (n + m) xs)"; 
   771 by (induct_tac "m" 1);
   772  by Auto_tac;
   773 by (exhaust_tac "xs" 1);
   774  by Auto_tac;
   775 qed_spec_mp "take_drop";
   776 
   777 Goal "!xs. take n (map f xs) = map f (take n xs)"; 
   778 by (induct_tac "n" 1);
   779  by Auto_tac;
   780 by (exhaust_tac "xs" 1);
   781  by Auto_tac;
   782 qed_spec_mp "take_map"; 
   783 
   784 Goal "!xs. drop n (map f xs) = map f (drop n xs)"; 
   785 by (induct_tac "n" 1);
   786  by Auto_tac;
   787 by (exhaust_tac "xs" 1);
   788  by Auto_tac;
   789 qed_spec_mp "drop_map";
   790 
   791 Goal "!n i. i < n --> (take n xs)!i = xs!i";
   792 by (induct_tac "xs" 1);
   793  by Auto_tac;
   794 by (exhaust_tac "n" 1);
   795  by (Blast_tac 1);
   796 by (exhaust_tac "i" 1);
   797  by Auto_tac;
   798 qed_spec_mp "nth_take";
   799 Addsimps [nth_take];
   800 
   801 Goal  "!xs i. n + i <= length xs --> (drop n xs)!i = xs!(n+i)";
   802 by (induct_tac "n" 1);
   803  by Auto_tac;
   804 by (exhaust_tac "xs" 1);
   805  by Auto_tac;
   806 qed_spec_mp "nth_drop";
   807 Addsimps [nth_drop];
   808 
   809 (** takeWhile & dropWhile **)
   810 
   811 section "takeWhile & dropWhile";
   812 
   813 Goal "takeWhile P xs @ dropWhile P xs = xs";
   814 by (induct_tac "xs" 1);
   815 by Auto_tac;
   816 qed "takeWhile_dropWhile_id";
   817 Addsimps [takeWhile_dropWhile_id];
   818 
   819 Goal  "x:set xs & ~P(x) --> takeWhile P (xs @ ys) = takeWhile P xs";
   820 by (induct_tac "xs" 1);
   821 by Auto_tac;
   822 bind_thm("takeWhile_append1", conjI RS (result() RS mp));
   823 Addsimps [takeWhile_append1];
   824 
   825 Goal "(!x:set xs. P(x)) --> takeWhile P (xs @ ys) = xs @ takeWhile P ys";
   826 by (induct_tac "xs" 1);
   827 by Auto_tac;
   828 bind_thm("takeWhile_append2", ballI RS (result() RS mp));
   829 Addsimps [takeWhile_append2];
   830 
   831 Goal "x:set xs & ~P(x) --> dropWhile P (xs @ ys) = (dropWhile P xs)@ys";
   832 by (induct_tac "xs" 1);
   833 by Auto_tac;
   834 bind_thm("dropWhile_append1", conjI RS (result() RS mp));
   835 Addsimps [dropWhile_append1];
   836 
   837 Goal "(!x:set xs. P(x)) --> dropWhile P (xs @ ys) = dropWhile P ys";
   838 by (induct_tac "xs" 1);
   839 by Auto_tac;
   840 bind_thm("dropWhile_append2", ballI RS (result() RS mp));
   841 Addsimps [dropWhile_append2];
   842 
   843 Goal "x:set(takeWhile P xs) --> x:set xs & P x";
   844 by (induct_tac "xs" 1);
   845 by Auto_tac;
   846 qed_spec_mp"set_take_whileD";
   847 
   848 (** zip **)
   849 section "zip";
   850 
   851 Goal "zip [] ys = []";
   852 by(induct_tac "ys" 1);
   853 by Auto_tac;
   854 qed "zip_Nil";
   855 Addsimps [zip_Nil];
   856 
   857 Goal "zip (x#xs) (y#ys) = (x,y)#zip xs ys";
   858 by(Simp_tac 1);
   859 qed "zip_Cons_Cons";
   860 Addsimps [zip_Cons_Cons];
   861 
   862 Delsimps(tl (thms"zip.simps"));
   863 
   864 
   865 (** foldl **)
   866 section "foldl";
   867 
   868 Goal "!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys";
   869 by (induct_tac "xs" 1);
   870 by Auto_tac;
   871 qed_spec_mp "foldl_append";
   872 Addsimps [foldl_append];
   873 
   874 (* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
   875    because it requires an additional transitivity step
   876 *)
   877 Goal "!n::nat. m <= n --> m <= foldl op+ n ns";
   878 by (induct_tac "ns" 1);
   879 by Auto_tac;
   880 qed_spec_mp "start_le_sum";
   881 
   882 Goal "n : set ns ==> n <= foldl op+ 0 ns";
   883 by (force_tac (claset() addIs [start_le_sum],
   884               simpset() addsimps [in_set_conv_decomp]) 1);
   885 qed "elem_le_sum";
   886 
   887 Goal "!m. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))";
   888 by (induct_tac "ns" 1);
   889 by Auto_tac;
   890 qed_spec_mp "sum_eq_0_conv";
   891 AddIffs [sum_eq_0_conv];
   892 
   893 (** upto **)
   894 
   895 (* Does not terminate! *)
   896 Goal "[i..j(] = (if i<j then i#[Suc i..j(] else [])";
   897 by (induct_tac "j" 1);
   898 by Auto_tac;
   899 qed "upt_rec";
   900 
   901 Goal "j<=i ==> [i..j(] = []";
   902 by (stac upt_rec 1);
   903 by (Asm_simp_tac 1);
   904 qed "upt_conv_Nil";
   905 Addsimps [upt_conv_Nil];
   906 
   907 Goal "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]";
   908 by (Asm_simp_tac 1);
   909 qed "upt_Suc";
   910 
   911 Goal "i<j ==> [i..j(] = i#[Suc i..j(]";
   912 by (rtac trans 1);
   913 by (stac upt_rec 1);
   914 by (rtac refl 2);
   915 by (Asm_simp_tac 1);
   916 qed "upt_conv_Cons";
   917 
   918 Goal "length [i..j(] = j-i";
   919 by (induct_tac "j" 1);
   920  by (Simp_tac 1);
   921 by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
   922 qed "length_upt";
   923 Addsimps [length_upt];
   924 
   925 Goal "i+k < j --> [i..j(] ! k = i+k";
   926 by (induct_tac "j" 1);
   927  by (Simp_tac 1);
   928 by (asm_simp_tac (simpset() addsimps [nth_append,less_diff_conv]@add_ac) 1);
   929 by (Clarify_tac 1);
   930 by (subgoal_tac "n=i+k" 1);
   931  by (Asm_simp_tac 2);
   932 by (Asm_simp_tac 1);
   933 qed_spec_mp "nth_upt";
   934 Addsimps [nth_upt];
   935 
   936 
   937 (** nodups & remdups **)
   938 section "nodups & remdups";
   939 
   940 Goal "set(remdups xs) = set xs";
   941 by (induct_tac "xs" 1);
   942  by (Simp_tac 1);
   943 by (asm_full_simp_tac (simpset() addsimps [insert_absorb]) 1);
   944 qed "set_remdups";
   945 Addsimps [set_remdups];
   946 
   947 Goal "nodups(remdups xs)";
   948 by (induct_tac "xs" 1);
   949 by Auto_tac;
   950 qed "nodups_remdups";
   951 
   952 Goal "nodups xs --> nodups (filter P xs)";
   953 by (induct_tac "xs" 1);
   954 by Auto_tac;
   955 qed_spec_mp "nodups_filter";
   956 
   957 (** replicate **)
   958 section "replicate";
   959 
   960 Goal "set(replicate (Suc n) x) = {x}";
   961 by (induct_tac "n" 1);
   962 by Auto_tac;
   963 val lemma = result();
   964 
   965 Goal "n ~= 0 ==> set(replicate n x) = {x}";
   966 by (fast_tac (claset() addSDs [not0_implies_Suc] addSIs [lemma]) 1);
   967 qed "set_replicate";
   968 Addsimps [set_replicate];
   969 
   970 
   971 (*** Lexcicographic orderings on lists ***)
   972 section"Lexcicographic orderings on lists";
   973 
   974 Goal "wf r ==> wf(lexn r n)";
   975 by (induct_tac "n" 1);
   976 by (Simp_tac 1);
   977 by (Simp_tac 1);
   978 by (rtac wf_subset 1);
   979 by (rtac Int_lower1 2);
   980 by (rtac wf_prod_fun_image 1);
   981 by (rtac injI 2);
   982 by (Auto_tac);
   983 qed "wf_lexn";
   984 
   985 Goal "!xs ys. (xs,ys) : lexn r n --> length xs = n & length ys = n";
   986 by (induct_tac "n" 1);
   987 by (Auto_tac);
   988 qed_spec_mp "lexn_length";
   989 
   990 Goalw [lex_def] "wf r ==> wf(lex r)";
   991 by (rtac wf_UN 1);
   992 by (blast_tac (claset() addIs [wf_lexn]) 1);
   993 by (Clarify_tac 1);
   994 by (rename_tac "m n" 1);
   995 by (subgoal_tac "m ~= n" 1);
   996  by (Blast_tac 2);
   997 by (blast_tac (claset() addDs [lexn_length,not_sym]) 1);
   998 qed "wf_lex";
   999 AddSIs [wf_lex];
  1000 
  1001 Goal
  1002  "lexn r n = \
  1003 \ {(xs,ys). length xs = n & length ys = n & \
  1004 \           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
  1005 by (induct_tac "n" 1);
  1006  by (Simp_tac 1);
  1007  by (Blast_tac 1);
  1008 by (asm_full_simp_tac (simpset() 
  1009 				addsimps [lex_prod_def]) 1);
  1010 by (auto_tac (claset(), simpset()));
  1011   by (Blast_tac 1);
  1012  by (rename_tac "a xys x xs' y ys'" 1);
  1013  by (res_inst_tac [("x","a#xys")] exI 1);
  1014  by (Simp_tac 1);
  1015 by (exhaust_tac "xys" 1);
  1016  by (ALLGOALS (asm_full_simp_tac (simpset())));
  1017 by (Blast_tac 1);
  1018 qed "lexn_conv";
  1019 
  1020 Goalw [lex_def]
  1021  "lex r = \
  1022 \ {(xs,ys). length xs = length ys & \
  1023 \           (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}";
  1024 by (force_tac (claset(), simpset() addsimps [lexn_conv]) 1);
  1025 qed "lex_conv";
  1026 
  1027 Goalw [lexico_def] "wf r ==> wf(lexico r)";
  1028 by (Blast_tac 1);
  1029 qed "wf_lexico";
  1030 AddSIs [wf_lexico];
  1031 
  1032 Goalw
  1033  [lexico_def,diag_def,lex_prod_def,measure_def,inv_image_def]
  1034 "lexico r = {(xs,ys). length xs < length ys | \
  1035 \                     length xs = length ys & (xs,ys) : lex r}";
  1036 by (Simp_tac 1);
  1037 qed "lexico_conv";
  1038 
  1039 Goal "([],ys) ~: lex r";
  1040 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1041 qed "Nil_notin_lex";
  1042 
  1043 Goal "(xs,[]) ~: lex r";
  1044 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1045 qed "Nil2_notin_lex";
  1046 
  1047 AddIffs [Nil_notin_lex,Nil2_notin_lex];
  1048 
  1049 Goal "((x#xs,y#ys) : lex r) = \
  1050 \     ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)";
  1051 by (simp_tac (simpset() addsimps [lex_conv]) 1);
  1052 by (rtac iffI 1);
  1053  by (blast_tac (claset() addIs [Cons_eq_appendI]) 2);
  1054 by (REPEAT(eresolve_tac [conjE, exE] 1));
  1055 by (exhaust_tac "xys" 1);
  1056 by (Asm_full_simp_tac 1);
  1057 by (Asm_full_simp_tac 1);
  1058 by (Blast_tac 1);
  1059 qed "Cons_in_lex";
  1060 AddIffs [Cons_in_lex];