src/ZF/List.thy
author wenzelm
Tue Aug 27 11:03:05 2002 +0200 (2002-08-27)
changeset 13524 604d0f3622d6
parent 13509 6f168374652a
child 13611 2edf034c902a
permissions -rw-r--r--
*** empty log message ***
     1 (*  Title:      ZF/List
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1994  University of Cambridge
     5 
     6 *)
     7 
     8 header{*Lists in Zermelo-Fraenkel Set Theory*}
     9 
    10 theory List = Datatype + ArithSimp:
    11 
    12 consts
    13   list       :: "i=>i"
    14 
    15 datatype
    16   "list(A)" = Nil | Cons ("a:A", "l: list(A)")
    17 
    18 
    19 syntax
    20  "[]"        :: i                                       ("[]")
    21  "@List"     :: "is => i"                                 ("[(_)]")
    22 
    23 translations
    24   "[x, xs]"     == "Cons(x, [xs])"
    25   "[x]"         == "Cons(x, [])"
    26   "[]"          == "Nil"
    27 
    28 
    29 consts
    30   length :: "i=>i"
    31   hd     :: "i=>i"
    32   tl     :: "i=>i"
    33 
    34 primrec
    35   "length([]) = 0"
    36   "length(Cons(a,l)) = succ(length(l))"
    37 
    38 primrec
    39   "hd([]) = 0"
    40   "hd(Cons(a,l)) = a"
    41 
    42 primrec
    43   "tl([]) = []"
    44   "tl(Cons(a,l)) = l"
    45 
    46 
    47 consts
    48   map         :: "[i=>i, i] => i"
    49   set_of_list :: "i=>i"
    50   app         :: "[i,i]=>i"                        (infixr "@" 60)
    51 
    52 (*map is a binding operator -- it applies to meta-level functions, not
    53 object-level functions.  This simplifies the final form of term_rec_conv,
    54 although complicating its derivation.*)
    55 primrec
    56   "map(f,[]) = []"
    57   "map(f,Cons(a,l)) = Cons(f(a), map(f,l))"
    58 
    59 primrec
    60   "set_of_list([]) = 0"
    61   "set_of_list(Cons(a,l)) = cons(a, set_of_list(l))"
    62 
    63 primrec
    64   app_Nil:  "[] @ ys = ys"
    65   app_Cons: "(Cons(a,l)) @ ys = Cons(a, l @ ys)"
    66 
    67 
    68 consts
    69   rev :: "i=>i"
    70   flat       :: "i=>i"
    71   list_add   :: "i=>i"
    72 
    73 primrec
    74   "rev([]) = []"
    75   "rev(Cons(a,l)) = rev(l) @ [a]"
    76 
    77 primrec
    78   "flat([]) = []"
    79   "flat(Cons(l,ls)) = l @ flat(ls)"
    80 
    81 primrec
    82   "list_add([]) = 0"
    83   "list_add(Cons(a,l)) = a #+ list_add(l)"
    84 
    85 consts
    86   drop       :: "[i,i]=>i"
    87 
    88 primrec
    89   drop_0:    "drop(0,l) = l"
    90   drop_succ: "drop(succ(i), l) = tl (drop(i,l))"
    91 
    92 
    93 (*** Thanks to Sidi Ehmety for the following ***)
    94 
    95 constdefs
    96 (* Function `take' returns the first n elements of a list *)
    97   take     :: "[i,i]=>i"
    98   "take(n, as) == list_rec(lam n:nat. [],
    99 		%a l r. lam n:nat. nat_case([], %m. Cons(a, r`m), n), as)`n"
   100 
   101   nth :: "[i, i]=>i"
   102   --{*returns the (n+1)th element of a list, or 0 if the
   103    list is too short.*}
   104   "nth(n, as) == list_rec(lam n:nat. 0,
   105  		          %a l r. lam n:nat. nat_case(a, %m. r`m, n), as) ` n"
   106 
   107   list_update :: "[i, i, i]=>i"
   108   "list_update(xs, i, v) == list_rec(lam n:nat. Nil,
   109       %u us vs. lam n:nat. nat_case(Cons(v, us), %m. Cons(u, vs`m), n), xs)`i"
   110 
   111 consts
   112   filter :: "[i=>o, i] => i"
   113   upt :: "[i, i] =>i"
   114 
   115 primrec
   116   "filter(P, Nil) = Nil"
   117   "filter(P, Cons(x, xs)) =
   118      (if P(x) then Cons(x, filter(P, xs)) else filter(P, xs))"
   119 
   120 primrec
   121   "upt(i, 0) = Nil"
   122   "upt(i, succ(j)) = (if i le j then upt(i, j)@[j] else Nil)"
   123 
   124 constdefs
   125   min :: "[i,i] =>i"
   126     "min(x, y) == (if x le y then x else y)"
   127 
   128   max :: "[i, i] =>i"
   129     "max(x, y) == (if x le y then y else x)"
   130 
   131 (*** Aspects of the datatype definition ***)
   132 
   133 declare list.intros [simp,TC]
   134 
   135 (*An elimination rule, for type-checking*)
   136 inductive_cases ConsE: "Cons(a,l) : list(A)"
   137 
   138 lemma Cons_type_iff [simp]: "Cons(a,l) \<in> list(A) <-> a \<in> A & l \<in> list(A)"
   139 by (blast elim: ConsE) 
   140 
   141 (*Proving freeness results*)
   142 lemma Cons_iff: "Cons(a,l)=Cons(a',l') <-> a=a' & l=l'"
   143 by auto
   144 
   145 lemma Nil_Cons_iff: "~ Nil=Cons(a,l)"
   146 by auto
   147 
   148 lemma list_unfold: "list(A) = {0} + (A * list(A))"
   149 by (blast intro!: list.intros [unfolded list.con_defs]
   150           elim: list.cases [unfolded list.con_defs])
   151 
   152 
   153 (**  Lemmas to justify using "list" in other recursive type definitions **)
   154 
   155 lemma list_mono: "A<=B ==> list(A) <= list(B)"
   156 apply (unfold list.defs )
   157 apply (rule lfp_mono)
   158 apply (simp_all add: list.bnd_mono)
   159 apply (assumption | rule univ_mono basic_monos)+
   160 done
   161 
   162 (*There is a similar proof by list induction.*)
   163 lemma list_univ: "list(univ(A)) <= univ(A)"
   164 apply (unfold list.defs list.con_defs)
   165 apply (rule lfp_lowerbound)
   166 apply (rule_tac [2] A_subset_univ [THEN univ_mono])
   167 apply (blast intro!: zero_in_univ Inl_in_univ Inr_in_univ Pair_in_univ)
   168 done
   169 
   170 (*These two theorems justify datatypes involving list(nat), list(A), ...*)
   171 lemmas list_subset_univ = subset_trans [OF list_mono list_univ]
   172 
   173 lemma list_into_univ: "[| l: list(A);  A <= univ(B) |] ==> l: univ(B)"
   174 by (blast intro: list_subset_univ [THEN subsetD])
   175 
   176 lemma list_case_type:
   177     "[| l: list(A);
   178         c: C(Nil);
   179         !!x y. [| x: A;  y: list(A) |] ==> h(x,y): C(Cons(x,y))
   180      |] ==> list_case(c,h,l) : C(l)"
   181 by (erule list.induct, auto)
   182 
   183 lemma list_0_triv: "list(0) = {Nil}"
   184 apply (rule equalityI, auto) 
   185 apply (induct_tac x, auto) 
   186 done
   187 
   188 
   189 (*** List functions ***)
   190 
   191 lemma tl_type: "l: list(A) ==> tl(l) : list(A)"
   192 apply (induct_tac "l")
   193 apply (simp_all (no_asm_simp) add: list.intros)
   194 done
   195 
   196 (** drop **)
   197 
   198 lemma drop_Nil [simp]: "i:nat ==> drop(i, Nil) = Nil"
   199 apply (induct_tac "i")
   200 apply (simp_all (no_asm_simp))
   201 done
   202 
   203 lemma drop_succ_Cons [simp]: "i:nat ==> drop(succ(i), Cons(a,l)) = drop(i,l)"
   204 apply (rule sym)
   205 apply (induct_tac "i")
   206 apply (simp (no_asm))
   207 apply (simp (no_asm_simp))
   208 done
   209 
   210 lemma drop_type [simp,TC]: "[| i:nat; l: list(A) |] ==> drop(i,l) : list(A)"
   211 apply (induct_tac "i")
   212 apply (simp_all (no_asm_simp) add: tl_type)
   213 done
   214 
   215 declare drop_succ [simp del]
   216 
   217 
   218 (** Type checking -- proved by induction, as usual **)
   219 
   220 lemma list_rec_type [TC]:
   221     "[| l: list(A);
   222         c: C(Nil);
   223         !!x y r. [| x:A;  y: list(A);  r: C(y) |] ==> h(x,y,r): C(Cons(x,y))
   224      |] ==> list_rec(c,h,l) : C(l)"
   225 by (induct_tac "l", auto)
   226 
   227 (** map **)
   228 
   229 lemma map_type [TC]:
   230     "[| l: list(A);  !!x. x: A ==> h(x): B |] ==> map(h,l) : list(B)"
   231 apply (simp add: map_list_def)
   232 apply (typecheck add: list.intros list_rec_type, blast)
   233 done
   234 
   235 lemma map_type2 [TC]: "l: list(A) ==> map(h,l) : list({h(u). u:A})"
   236 apply (erule map_type)
   237 apply (erule RepFunI)
   238 done
   239 
   240 (** length **)
   241 
   242 lemma length_type [TC]: "l: list(A) ==> length(l) : nat"
   243 by (simp add: length_list_def)
   244 
   245 lemma lt_length_in_nat:
   246    "[|x < length(xs); xs \<in> list(A)|] ==> x \<in> nat"
   247 by (frule lt_nat_in_nat, typecheck) 
   248 
   249 (** app **)
   250 
   251 lemma app_type [TC]: "[| xs: list(A);  ys: list(A) |] ==> xs@ys : list(A)"
   252 by (simp add: app_list_def)
   253 
   254 (** rev **)
   255 
   256 lemma rev_type [TC]: "xs: list(A) ==> rev(xs) : list(A)"
   257 by (simp add: rev_list_def)
   258 
   259 
   260 (** flat **)
   261 
   262 lemma flat_type [TC]: "ls: list(list(A)) ==> flat(ls) : list(A)"
   263 by (simp add: flat_list_def)
   264 
   265 
   266 (** set_of_list **)
   267 
   268 lemma set_of_list_type [TC]: "l: list(A) ==> set_of_list(l) : Pow(A)"
   269 apply (unfold set_of_list_list_def)
   270 apply (erule list_rec_type, auto)
   271 done
   272 
   273 lemma set_of_list_append:
   274      "xs: list(A) ==> set_of_list (xs@ys) = set_of_list(xs) Un set_of_list(ys)"
   275 apply (erule list.induct)
   276 apply (simp_all (no_asm_simp) add: Un_cons)
   277 done
   278 
   279 
   280 (** list_add **)
   281 
   282 lemma list_add_type [TC]: "xs: list(nat) ==> list_add(xs) : nat"
   283 by (simp add: list_add_list_def)
   284 
   285 
   286 (*** theorems about map ***)
   287 
   288 lemma map_ident [simp]: "l: list(A) ==> map(%u. u, l) = l"
   289 apply (induct_tac "l")
   290 apply (simp_all (no_asm_simp))
   291 done
   292 
   293 lemma map_compose: "l: list(A) ==> map(h, map(j,l)) = map(%u. h(j(u)), l)"
   294 apply (induct_tac "l")
   295 apply (simp_all (no_asm_simp))
   296 done
   297 
   298 lemma map_app_distrib: "xs: list(A) ==> map(h, xs@ys) = map(h,xs) @ map(h,ys)"
   299 apply (induct_tac "xs")
   300 apply (simp_all (no_asm_simp))
   301 done
   302 
   303 lemma map_flat: "ls: list(list(A)) ==> map(h, flat(ls)) = flat(map(map(h),ls))"
   304 apply (induct_tac "ls")
   305 apply (simp_all (no_asm_simp) add: map_app_distrib)
   306 done
   307 
   308 lemma list_rec_map:
   309      "l: list(A) ==>
   310       list_rec(c, d, map(h,l)) =
   311       list_rec(c, %x xs r. d(h(x), map(h,xs), r), l)"
   312 apply (induct_tac "l")
   313 apply (simp_all (no_asm_simp))
   314 done
   315 
   316 (** theorems about list(Collect(A,P)) -- used in Induct/Term.thy **)
   317 
   318 (* c : list(Collect(B,P)) ==> c : list(B) *)
   319 lemmas list_CollectD = Collect_subset [THEN list_mono, THEN subsetD, standard]
   320 
   321 lemma map_list_Collect: "l: list({x:A. h(x)=j(x)}) ==> map(h,l) = map(j,l)"
   322 apply (induct_tac "l")
   323 apply (simp_all (no_asm_simp))
   324 done
   325 
   326 (*** theorems about length ***)
   327 
   328 lemma length_map [simp]: "xs: list(A) ==> length(map(h,xs)) = length(xs)"
   329 by (induct_tac "xs", simp_all)
   330 
   331 lemma length_app [simp]:
   332      "[| xs: list(A); ys: list(A) |]
   333       ==> length(xs@ys) = length(xs) #+ length(ys)"
   334 by (induct_tac "xs", simp_all)
   335 
   336 lemma length_rev [simp]: "xs: list(A) ==> length(rev(xs)) = length(xs)"
   337 apply (induct_tac "xs")
   338 apply (simp_all (no_asm_simp) add: length_app)
   339 done
   340 
   341 lemma length_flat:
   342      "ls: list(list(A)) ==> length(flat(ls)) = list_add(map(length,ls))"
   343 apply (induct_tac "ls")
   344 apply (simp_all (no_asm_simp) add: length_app)
   345 done
   346 
   347 (** Length and drop **)
   348 
   349 (*Lemma for the inductive step of drop_length*)
   350 lemma drop_length_Cons [rule_format]:
   351      "xs: list(A) ==>
   352            \<forall>x.  EX z zs. drop(length(xs), Cons(x,xs)) = Cons(z,zs)"
   353 by (erule list.induct, simp_all)
   354 
   355 lemma drop_length [rule_format]:
   356      "l: list(A) ==> \<forall>i \<in> length(l). (EX z zs. drop(i,l) = Cons(z,zs))"
   357 apply (erule list.induct, simp_all)
   358 apply safe
   359 apply (erule drop_length_Cons)
   360 apply (rule natE)
   361 apply (erule Ord_trans [OF asm_rl length_type Ord_nat], assumption, simp_all)
   362 apply (blast intro: succ_in_naturalD length_type)
   363 done
   364 
   365 
   366 (*** theorems about app ***)
   367 
   368 lemma app_right_Nil [simp]: "xs: list(A) ==> xs@Nil=xs"
   369 by (erule list.induct, simp_all)
   370 
   371 lemma app_assoc: "xs: list(A) ==> (xs@ys)@zs = xs@(ys@zs)"
   372 by (induct_tac "xs", simp_all)
   373 
   374 lemma flat_app_distrib: "ls: list(list(A)) ==> flat(ls@ms) = flat(ls)@flat(ms)"
   375 apply (induct_tac "ls")
   376 apply (simp_all (no_asm_simp) add: app_assoc)
   377 done
   378 
   379 (*** theorems about rev ***)
   380 
   381 lemma rev_map_distrib: "l: list(A) ==> rev(map(h,l)) = map(h,rev(l))"
   382 apply (induct_tac "l")
   383 apply (simp_all (no_asm_simp) add: map_app_distrib)
   384 done
   385 
   386 (*Simplifier needs the premises as assumptions because rewriting will not
   387   instantiate the variable ?A in the rules' typing conditions; note that
   388   rev_type does not instantiate ?A.  Only the premises do.
   389 *)
   390 lemma rev_app_distrib:
   391      "[| xs: list(A);  ys: list(A) |] ==> rev(xs@ys) = rev(ys)@rev(xs)"
   392 apply (erule list.induct)
   393 apply (simp_all add: app_assoc)
   394 done
   395 
   396 lemma rev_rev_ident [simp]: "l: list(A) ==> rev(rev(l))=l"
   397 apply (induct_tac "l")
   398 apply (simp_all (no_asm_simp) add: rev_app_distrib)
   399 done
   400 
   401 lemma rev_flat: "ls: list(list(A)) ==> rev(flat(ls)) = flat(map(rev,rev(ls)))"
   402 apply (induct_tac "ls")
   403 apply (simp_all add: map_app_distrib flat_app_distrib rev_app_distrib)
   404 done
   405 
   406 
   407 (*** theorems about list_add ***)
   408 
   409 lemma list_add_app:
   410      "[| xs: list(nat);  ys: list(nat) |]
   411       ==> list_add(xs@ys) = list_add(ys) #+ list_add(xs)"
   412 apply (induct_tac "xs", simp_all)
   413 done
   414 
   415 lemma list_add_rev: "l: list(nat) ==> list_add(rev(l)) = list_add(l)"
   416 apply (induct_tac "l")
   417 apply (simp_all (no_asm_simp) add: list_add_app)
   418 done
   419 
   420 lemma list_add_flat:
   421      "ls: list(list(nat)) ==> list_add(flat(ls)) = list_add(map(list_add,ls))"
   422 apply (induct_tac "ls")
   423 apply (simp_all (no_asm_simp) add: list_add_app)
   424 done
   425 
   426 (** New induction rules **)
   427 
   428 lemma list_append_induct [case_names Nil snoc, consumes 1]:
   429     "[| l: list(A);
   430         P(Nil);
   431         !!x y. [| x: A;  y: list(A);  P(y) |] ==> P(y @ [x])
   432      |] ==> P(l)"
   433 apply (subgoal_tac "P(rev(rev(l)))", simp)
   434 apply (erule rev_type [THEN list.induct], simp_all)
   435 done
   436 
   437 lemma list_complete_induct_lemma [rule_format]:
   438  assumes ih: 
   439     "\<And>l. [| l \<in> list(A); 
   440              \<forall>l' \<in> list(A). length(l') < length(l) --> P(l')|] 
   441           ==> P(l)"
   442   shows "n \<in> nat ==> \<forall>l \<in> list(A). length(l) < n --> P(l)"
   443 apply (induct_tac n, simp)
   444 apply (blast intro: ih elim!: leE) 
   445 done
   446 
   447 theorem list_complete_induct:
   448       "[| l \<in> list(A); 
   449           \<And>l. [| l \<in> list(A); 
   450                   \<forall>l' \<in> list(A). length(l') < length(l) --> P(l')|] 
   451                ==> P(l)
   452        |] ==> P(l)"
   453 apply (rule list_complete_induct_lemma [of A]) 
   454    prefer 4 apply (rule le_refl, simp) 
   455   apply blast 
   456  apply simp 
   457 apply assumption
   458 done
   459 
   460 
   461 (*** Thanks to Sidi Ehmety for these results about min, take, etc. ***)
   462 
   463 (** min FIXME: replace by Int! **)
   464 (* Min theorems are also true for i, j ordinals *)
   465 lemma min_sym: "[| i:nat; j:nat |] ==> min(i,j)=min(j,i)"
   466 apply (unfold min_def)
   467 apply (auto dest!: not_lt_imp_le dest: lt_not_sym intro: le_anti_sym)
   468 done
   469 
   470 lemma min_type [simp,TC]: "[| i:nat; j:nat |] ==> min(i,j):nat"
   471 by (unfold min_def, auto)
   472 
   473 lemma min_0 [simp]: "i:nat ==> min(0,i) = 0"
   474 apply (unfold min_def)
   475 apply (auto dest: not_lt_imp_le)
   476 done
   477 
   478 lemma min_02 [simp]: "i:nat ==> min(i, 0) = 0"
   479 apply (unfold min_def)
   480 apply (auto dest: not_lt_imp_le)
   481 done
   482 
   483 lemma lt_min_iff: "[| i:nat; j:nat; k:nat |] ==> i<min(j,k) <-> i<j & i<k"
   484 apply (unfold min_def)
   485 apply (auto dest!: not_lt_imp_le intro: lt_trans2 lt_trans)
   486 done
   487 
   488 lemma min_succ_succ [simp]:
   489      "[| i:nat; j:nat |] ==>  min(succ(i), succ(j))= succ(min(i, j))"
   490 apply (unfold min_def, auto)
   491 done
   492 
   493 (*** more theorems about lists ***)
   494 
   495 (** filter **)
   496 
   497 lemma filter_append [simp]:
   498      "xs:list(A) ==> filter(P, xs@ys) = filter(P, xs) @ filter(P, ys)"
   499 by (induct_tac "xs", auto)
   500 
   501 lemma filter_type [simp,TC]: "xs:list(A) ==> filter(P, xs):list(A)"
   502 by (induct_tac "xs", auto)
   503 
   504 lemma length_filter: "xs:list(A) ==> length(filter(P, xs)) le length(xs)"
   505 apply (induct_tac "xs", auto)
   506 apply (rule_tac j = "length (l) " in le_trans)
   507 apply (auto simp add: le_iff)
   508 done
   509 
   510 lemma filter_is_subset: "xs:list(A) ==> set_of_list(filter(P,xs)) <= set_of_list(xs)"
   511 by (induct_tac "xs", auto)
   512 
   513 lemma filter_False [simp]: "xs:list(A) ==> filter(%p. False, xs) = Nil"
   514 by (induct_tac "xs", auto)
   515 
   516 lemma filter_True [simp]: "xs:list(A) ==> filter(%p. True, xs) = xs"
   517 by (induct_tac "xs", auto)
   518 
   519 (** length **)
   520 
   521 lemma length_is_0_iff [simp]: "xs:list(A) ==> length(xs)=0 <-> xs=Nil"
   522 by (erule list.induct, auto)
   523 
   524 lemma length_is_0_iff2 [simp]: "xs:list(A) ==> 0 = length(xs) <-> xs=Nil"
   525 by (erule list.induct, auto)
   526 
   527 lemma length_tl [simp]: "xs:list(A) ==> length(tl(xs)) = length(xs) #- 1"
   528 by (erule list.induct, auto)
   529 
   530 lemma length_greater_0_iff: "xs:list(A) ==> 0<length(xs) <-> xs ~= Nil"
   531 by (erule list.induct, auto)
   532 
   533 lemma length_succ_iff: "xs:list(A) ==> length(xs)=succ(n) <-> (EX y ys. xs=Cons(y, ys) & length(ys)=n)"
   534 by (erule list.induct, auto)
   535 
   536 (** more theorems about append **)
   537 
   538 lemma append_is_Nil_iff [simp]:
   539      "xs:list(A) ==> (xs@ys = Nil) <-> (xs=Nil & ys = Nil)"
   540 by (erule list.induct, auto)
   541 
   542 lemma append_is_Nil_iff2 [simp]:
   543      "xs:list(A) ==> (Nil = xs@ys) <-> (xs=Nil & ys = Nil)"
   544 by (erule list.induct, auto)
   545 
   546 lemma append_left_is_self_iff [simp]:
   547      "xs:list(A) ==> (xs@ys = xs) <-> (ys = Nil)"
   548 by (erule list.induct, auto)
   549 
   550 lemma append_left_is_self_iff2 [simp]:
   551      "xs:list(A) ==> (xs = xs@ys) <-> (ys = Nil)"
   552 by (erule list.induct, auto)
   553 
   554 (*TOO SLOW as a default simprule!*)
   555 lemma append_left_is_Nil_iff [rule_format]:
   556      "[| xs:list(A); ys:list(A); zs:list(A) |] ==>
   557    length(ys)=length(zs) --> (xs@ys=zs <-> (xs=Nil & ys=zs))"
   558 apply (erule list.induct)
   559 apply (auto simp add: length_app)
   560 done
   561 
   562 (*TOO SLOW as a default simprule!*)
   563 lemma append_left_is_Nil_iff2 [rule_format]:
   564      "[| xs:list(A); ys:list(A); zs:list(A) |] ==>
   565    length(ys)=length(zs) --> (zs=ys@xs <-> (xs=Nil & ys=zs))"
   566 apply (erule list.induct)
   567 apply (auto simp add: length_app)
   568 done
   569 
   570 lemma append_eq_append_iff [rule_format,simp]:
   571      "xs:list(A) ==> \<forall>ys \<in> list(A).
   572       length(xs)=length(ys) --> (xs@us = ys@vs) <-> (xs=ys & us=vs)"
   573 apply (erule list.induct)
   574 apply (simp (no_asm_simp))
   575 apply clarify
   576 apply (erule_tac a = ys in list.cases, auto)
   577 done
   578 
   579 lemma append_eq_append [rule_format]:
   580   "xs:list(A) ==>
   581    \<forall>ys \<in> list(A). \<forall>us \<in> list(A). \<forall>vs \<in> list(A).
   582    length(us) = length(vs) --> (xs@us = ys@vs) --> (xs=ys & us=vs)"
   583 apply (induct_tac "xs")
   584 apply (force simp add: length_app, clarify)
   585 apply (erule_tac a = ys in list.cases, simp)
   586 apply (subgoal_tac "Cons (a, l) @ us =vs")
   587  apply (drule rev_iffD1 [OF _ append_left_is_Nil_iff], simp_all, blast)
   588 done
   589 
   590 lemma append_eq_append_iff2 [simp]:
   591  "[| xs:list(A); ys:list(A); us:list(A); vs:list(A); length(us)=length(vs) |]
   592   ==>  xs@us = ys@vs <-> (xs=ys & us=vs)"
   593 apply (rule iffI)
   594 apply (rule append_eq_append, auto)
   595 done
   596 
   597 lemma append_self_iff [simp]:
   598      "[| xs:list(A); ys:list(A); zs:list(A) |] ==> xs@ys=xs@zs <-> ys=zs"
   599 by simp
   600 
   601 lemma append_self_iff2 [simp]:
   602      "[| xs:list(A); ys:list(A); zs:list(A) |] ==> ys@xs=zs@xs <-> ys=zs"
   603 by simp
   604 
   605 (* Can also be proved from append_eq_append_iff2,
   606 but the proof requires two more hypotheses: x:A and y:A *)
   607 lemma append1_eq_iff [rule_format,simp]:
   608      "xs:list(A) ==> \<forall>ys \<in> list(A). xs@[x] = ys@[y] <-> (xs = ys & x=y)"
   609 apply (erule list.induct)  
   610  apply clarify 
   611  apply (erule list.cases)
   612  apply simp_all
   613 txt{*Inductive step*}  
   614 apply clarify 
   615 apply (erule_tac a=ys in list.cases, simp_all)  
   616 done
   617 
   618 
   619 lemma append_right_is_self_iff [simp]:
   620      "[| xs:list(A); ys:list(A) |] ==> (xs@ys = ys) <-> (xs=Nil)"
   621 by (simp (no_asm_simp) add: append_left_is_Nil_iff)
   622 
   623 lemma append_right_is_self_iff2 [simp]:
   624      "[| xs:list(A); ys:list(A) |] ==> (ys = xs@ys) <-> (xs=Nil)"
   625 apply (rule iffI)
   626 apply (drule sym, auto) 
   627 done
   628 
   629 lemma hd_append [rule_format,simp]:
   630      "xs:list(A) ==> xs ~= Nil --> hd(xs @ ys) = hd(xs)"
   631 by (induct_tac "xs", auto)
   632 
   633 lemma tl_append [rule_format,simp]:
   634      "xs:list(A) ==> xs~=Nil --> tl(xs @ ys) = tl(xs)@ys"
   635 by (induct_tac "xs", auto)
   636 
   637 (** rev **)
   638 lemma rev_is_Nil_iff [simp]: "xs:list(A) ==> (rev(xs) = Nil <-> xs = Nil)"
   639 by (erule list.induct, auto)
   640 
   641 lemma Nil_is_rev_iff [simp]: "xs:list(A) ==> (Nil = rev(xs) <-> xs = Nil)"
   642 by (erule list.induct, auto)
   643 
   644 lemma rev_is_rev_iff [rule_format,simp]:
   645      "xs:list(A) ==> \<forall>ys \<in> list(A). rev(xs)=rev(ys) <-> xs=ys"
   646 apply (erule list.induct, force, clarify)
   647 apply (erule_tac a = ys in list.cases, auto)
   648 done
   649 
   650 lemma rev_list_elim [rule_format]:
   651      "xs:list(A) ==>
   652       (xs=Nil --> P) --> (\<forall>ys \<in> list(A). \<forall>y \<in> A. xs =ys@[y] -->P)-->P"
   653 by (erule list_append_induct, auto)
   654 
   655 
   656 (** more theorems about drop **)
   657 
   658 lemma length_drop [rule_format,simp]:
   659      "n:nat ==> \<forall>xs \<in> list(A). length(drop(n, xs)) = length(xs) #- n"
   660 apply (erule nat_induct)
   661 apply (auto elim: list.cases)
   662 done
   663 
   664 lemma drop_all [rule_format,simp]:
   665      "n:nat ==> \<forall>xs \<in> list(A). length(xs) le n --> drop(n, xs)=Nil"
   666 apply (erule nat_induct)
   667 apply (auto elim: list.cases)
   668 done
   669 
   670 lemma drop_append [rule_format]:
   671      "n:nat ==> 
   672       \<forall>xs \<in> list(A). drop(n, xs@ys) = drop(n,xs) @ drop(n #- length(xs), ys)"
   673 apply (induct_tac "n")
   674 apply (auto elim: list.cases)
   675 done
   676 
   677 lemma drop_drop:
   678     "m:nat ==> \<forall>xs \<in> list(A). \<forall>n \<in> nat. drop(n, drop(m, xs))=drop(n #+ m, xs)"
   679 apply (induct_tac "m")
   680 apply (auto elim: list.cases)
   681 done
   682 
   683 (** take **)
   684 
   685 lemma take_0 [simp]: "xs:list(A) ==> take(0, xs) =  Nil"
   686 apply (unfold take_def)
   687 apply (erule list.induct, auto)
   688 done
   689 
   690 lemma take_succ_Cons [simp]:
   691     "n:nat ==> take(succ(n), Cons(a, xs)) = Cons(a, take(n, xs))"
   692 by (simp add: take_def)
   693 
   694 (* Needed for proving take_all *)
   695 lemma take_Nil [simp]: "n:nat ==> take(n, Nil) = Nil"
   696 by (unfold take_def, auto)
   697 
   698 lemma take_all [rule_format,simp]:
   699      "n:nat ==> \<forall>xs \<in> list(A). length(xs) le n  --> take(n, xs) = xs"
   700 apply (erule nat_induct)
   701 apply (auto elim: list.cases) 
   702 done
   703 
   704 lemma take_type [rule_format,simp,TC]:
   705      "xs:list(A) ==> \<forall>n \<in> nat. take(n, xs):list(A)"
   706 apply (erule list.induct, simp, clarify) 
   707 apply (erule natE, auto)
   708 done
   709 
   710 lemma take_append [rule_format,simp]:
   711  "xs:list(A) ==>
   712   \<forall>ys \<in> list(A). \<forall>n \<in> nat. take(n, xs @ ys) =
   713                             take(n, xs) @ take(n #- length(xs), ys)"
   714 apply (erule list.induct, simp, clarify) 
   715 apply (erule natE, auto)
   716 done
   717 
   718 lemma take_take [rule_format]:
   719    "m : nat ==>
   720     \<forall>xs \<in> list(A). \<forall>n \<in> nat. take(n, take(m,xs))= take(min(n, m), xs)"
   721 apply (induct_tac "m", auto)
   722 apply (erule_tac a = xs in list.cases)
   723 apply (auto simp add: take_Nil)
   724 apply (rotate_tac 1)
   725 apply (erule natE)
   726 apply (auto intro: take_0 take_type)
   727 done
   728 
   729 (** nth **)
   730 
   731 lemma nth_0 [simp]: "nth(0, Cons(a, l)) = a"
   732 by (simp add: nth_def) 
   733 
   734 lemma nth_Cons [simp]: "n:nat ==> nth(succ(n), Cons(a,l)) = nth(n,l)"
   735 by (simp add: nth_def) 
   736 
   737 lemma nth_empty [simp]: "nth(n, Nil) = 0"
   738 by (simp add: nth_def) 
   739 
   740 lemma nth_type [rule_format,simp,TC]:
   741      "xs:list(A) ==> \<forall>n \<in> nat. n < length(xs) --> nth(n,xs) : A"
   742 apply (erule list.induct, simp, clarify) 
   743 apply (erule natE, auto)
   744 done
   745 
   746 lemma nth_eq_0 [rule_format]:
   747      "xs:list(A) ==> \<forall>n \<in> nat. length(xs) le n --> nth(n,xs) = 0"
   748 apply (erule list.induct, simp, clarify) 
   749 apply (erule natE, auto)
   750 done
   751 
   752 lemma nth_append [rule_format]:
   753   "xs:list(A) ==> 
   754    \<forall>n \<in> nat. nth(n, xs @ ys) = (if n < length(xs) then nth(n,xs)
   755                                 else nth(n #- length(xs), ys))"
   756 apply (induct_tac "xs", simp, clarify) 
   757 apply (erule natE, auto)
   758 done
   759 
   760 lemma set_of_list_conv_nth:
   761     "xs:list(A)
   762      ==> set_of_list(xs) = {x:A. EX i:nat. i<length(xs) & x = nth(i,xs)}"
   763 apply (induct_tac "xs", simp_all)
   764 apply (rule equalityI, auto)
   765 apply (rule_tac x = 0 in bexI, auto)
   766 apply (erule natE, auto)
   767 done
   768 
   769 (* Other theorems about lists *)
   770 
   771 lemma nth_take_lemma [rule_format]:
   772  "k:nat ==>
   773   \<forall>xs \<in> list(A). (\<forall>ys \<in> list(A). k le length(xs) --> k le length(ys) -->
   774       (\<forall>i \<in> nat. i<k --> nth(i,xs) = nth(i,ys))--> take(k,xs) = take(k,ys))"
   775 apply (induct_tac "k")
   776 apply (simp_all (no_asm_simp) add: lt_succ_eq_0_disj all_conj_distrib)
   777 apply clarify
   778 (*Both lists are non-empty*)
   779 apply (erule_tac a=xs in list.cases, simp) 
   780 apply (erule_tac a=ys in list.cases, clarify) 
   781 apply (simp (no_asm_use) )
   782 apply clarify
   783 apply (simp (no_asm_simp))
   784 apply (rule conjI, force)
   785 apply (rename_tac y ys z zs) 
   786 apply (drule_tac x = zs and x1 = ys in bspec [THEN bspec], auto)   
   787 done
   788 
   789 lemma nth_equalityI [rule_format]:
   790      "[| xs:list(A); ys:list(A); length(xs) = length(ys);
   791          \<forall>i \<in> nat. i < length(xs) --> nth(i,xs) = nth(i,ys) |]
   792       ==> xs = ys"
   793 apply (subgoal_tac "length (xs) le length (ys) ")
   794 apply (cut_tac k="length(xs)" and xs=xs and ys=ys in nth_take_lemma) 
   795 apply (simp_all add: take_all)
   796 done
   797 
   798 (*The famous take-lemma*)
   799 
   800 lemma take_equalityI [rule_format]:
   801     "[| xs:list(A); ys:list(A); (\<forall>i \<in> nat. take(i, xs) = take(i,ys)) |] 
   802      ==> xs = ys"
   803 apply (case_tac "length (xs) le length (ys) ")
   804 apply (drule_tac x = "length (ys) " in bspec)
   805 apply (drule_tac [3] not_lt_imp_le)
   806 apply (subgoal_tac [5] "length (ys) le length (xs) ")
   807 apply (rule_tac [6] j = "succ (length (ys))" in le_trans)
   808 apply (rule_tac [6] leI)
   809 apply (drule_tac [5] x = "length (xs) " in bspec)
   810 apply (simp_all add: take_all)
   811 done
   812 
   813 lemma nth_drop [rule_format]:
   814   "n:nat ==> \<forall>i \<in> nat. \<forall>xs \<in> list(A). nth(i, drop(n, xs)) = nth(n #+ i, xs)"
   815 apply (induct_tac "n", simp_all, clarify)
   816 apply (erule list.cases, auto)  
   817 done
   818 
   819 subsection{*The function zip*}
   820 
   821 text{*Crafty definition to eliminate a type argument*}
   822 
   823 consts
   824   zip_aux        :: "[i,i]=>i"
   825 
   826 primrec (*explicit lambda is required because both arguments of "un" vary*)
   827   "zip_aux(B,[]) =
   828      (\<lambda>ys \<in> list(B). list_case([], %y l. [], ys))"
   829 
   830   "zip_aux(B,Cons(x,l)) =
   831      (\<lambda>ys \<in> list(B).
   832         list_case(Nil, %y zs. Cons(<x,y>, zip_aux(B,l)`zs), ys))"
   833 
   834 constdefs
   835   zip :: "[i, i]=>i"
   836    "zip(xs, ys) == zip_aux(set_of_list(ys),xs)`ys"
   837 
   838 
   839 (* zip equations *)
   840 
   841 lemma list_on_set_of_list: "xs \<in> list(A) ==> xs \<in> list(set_of_list(xs))"
   842 apply (induct_tac xs, simp_all) 
   843 apply (blast intro: list_mono [THEN subsetD]) 
   844 done
   845 
   846 lemma zip_Nil [simp]: "ys:list(A) ==> zip(Nil, ys)=Nil"
   847 apply (simp add: zip_def list_on_set_of_list [of _ A])
   848 apply (erule list.cases, simp_all) 
   849 done
   850 
   851 lemma zip_Nil2 [simp]: "xs:list(A) ==> zip(xs, Nil)=Nil"
   852 apply (simp add: zip_def list_on_set_of_list [of _ A])
   853 apply (erule list.cases, simp_all) 
   854 done
   855 
   856 lemma zip_aux_unique [rule_format]:
   857      "[|B<=C;  xs \<in> list(A)|] 
   858       ==> \<forall>ys \<in> list(B). zip_aux(C,xs) ` ys = zip_aux(B,xs) ` ys"
   859 apply (induct_tac xs) 
   860  apply simp_all 
   861  apply (blast intro: list_mono [THEN subsetD], clarify) 
   862 apply (erule_tac a=ys in list.cases, auto) 
   863 apply (blast intro: list_mono [THEN subsetD]) 
   864 done
   865 
   866 lemma zip_Cons_Cons [simp]:
   867      "[| xs:list(A); ys:list(B); x:A; y:B |] ==>
   868       zip(Cons(x,xs), Cons(y, ys)) = Cons(<x,y>, zip(xs, ys))"
   869 apply (simp add: zip_def, auto) 
   870 apply (rule zip_aux_unique, auto) 
   871 apply (simp add: list_on_set_of_list [of _ B])
   872 apply (blast intro: list_on_set_of_list list_mono [THEN subsetD]) 
   873 done
   874 
   875 lemma zip_type [rule_format,simp,TC]:
   876      "xs:list(A) ==> \<forall>ys \<in> list(B). zip(xs, ys):list(A*B)"
   877 apply (induct_tac "xs")
   878 apply (simp (no_asm))
   879 apply clarify
   880 apply (erule_tac a = ys in list.cases, auto)
   881 done
   882 
   883 (* zip length *)
   884 lemma length_zip [rule_format,simp]:
   885      "xs:list(A) ==> \<forall>ys \<in> list(B). length(zip(xs,ys)) =
   886                                      min(length(xs), length(ys))"
   887 apply (unfold min_def)
   888 apply (induct_tac "xs", simp_all, clarify) 
   889 apply (erule_tac a = ys in list.cases, auto)
   890 done
   891 
   892 lemma zip_append1 [rule_format]:
   893  "[| ys:list(A); zs:list(B) |] ==>
   894   \<forall>xs \<in> list(A). zip(xs @ ys, zs) = 
   895                  zip(xs, take(length(xs), zs)) @ zip(ys, drop(length(xs),zs))"
   896 apply (induct_tac "zs", force, clarify) 
   897 apply (erule_tac a = xs in list.cases, simp_all) 
   898 done
   899 
   900 lemma zip_append2 [rule_format]:
   901  "[| xs:list(A); zs:list(B) |] ==> \<forall>ys \<in> list(B). zip(xs, ys@zs) =
   902        zip(take(length(ys), xs), ys) @ zip(drop(length(ys), xs), zs)"
   903 apply (induct_tac "xs", force, clarify) 
   904 apply (erule_tac a = ys in list.cases, auto)
   905 done
   906 
   907 lemma zip_append [simp]:
   908  "[| length(xs) = length(us); length(ys) = length(vs);
   909      xs:list(A); us:list(B); ys:list(A); vs:list(B) |] 
   910   ==> zip(xs@ys,us@vs) = zip(xs, us) @ zip(ys, vs)"
   911 by (simp (no_asm_simp) add: zip_append1 drop_append diff_self_eq_0)
   912 
   913 
   914 lemma zip_rev [rule_format,simp]:
   915  "ys:list(B) ==> \<forall>xs \<in> list(A).
   916     length(xs) = length(ys) --> zip(rev(xs), rev(ys)) = rev(zip(xs, ys))"
   917 apply (induct_tac "ys", force, clarify) 
   918 apply (erule_tac a = xs in list.cases)
   919 apply (auto simp add: length_rev)
   920 done
   921 
   922 lemma nth_zip [rule_format,simp]:
   923    "ys:list(B) ==> \<forall>i \<in> nat. \<forall>xs \<in> list(A).
   924                     i < length(xs) --> i < length(ys) -->
   925                     nth(i,zip(xs, ys)) = <nth(i,xs),nth(i, ys)>"
   926 apply (induct_tac "ys", force, clarify) 
   927 apply (erule_tac a = xs in list.cases, simp) 
   928 apply (auto elim: natE)
   929 done
   930 
   931 lemma set_of_list_zip [rule_format]:
   932      "[| xs:list(A); ys:list(B); i:nat |]
   933       ==> set_of_list(zip(xs, ys)) =
   934           {<x, y>:A*B. EX i:nat. i < min(length(xs), length(ys))
   935           & x = nth(i, xs) & y = nth(i, ys)}"
   936 by (force intro!: Collect_cong simp add: lt_min_iff set_of_list_conv_nth)
   937 
   938 (** list_update **)
   939 
   940 lemma list_update_Nil [simp]: "i:nat ==>list_update(Nil, i, v) = Nil"
   941 by (unfold list_update_def, auto)
   942 
   943 lemma list_update_Cons_0 [simp]: "list_update(Cons(x, xs), 0, v)= Cons(v, xs)"
   944 by (unfold list_update_def, auto)
   945 
   946 lemma list_update_Cons_succ [simp]:
   947   "n:nat ==>
   948     list_update(Cons(x, xs), succ(n), v)= Cons(x, list_update(xs, n, v))"
   949 apply (unfold list_update_def, auto)
   950 done
   951 
   952 lemma list_update_type [rule_format,simp,TC]:
   953      "[| xs:list(A); v:A |] ==> \<forall>n \<in> nat. list_update(xs, n, v):list(A)"
   954 apply (induct_tac "xs")
   955 apply (simp (no_asm))
   956 apply clarify
   957 apply (erule natE, auto)
   958 done
   959 
   960 lemma length_list_update [rule_format,simp]:
   961      "xs:list(A) ==> \<forall>i \<in> nat. length(list_update(xs, i, v))=length(xs)"
   962 apply (induct_tac "xs")
   963 apply (simp (no_asm))
   964 apply clarify
   965 apply (erule natE, auto)
   966 done
   967 
   968 lemma nth_list_update [rule_format]:
   969      "[| xs:list(A) |] ==> \<forall>i \<in> nat. \<forall>j \<in> nat. i < length(xs)  -->
   970          nth(j, list_update(xs, i, x)) = (if i=j then x else nth(j, xs))"
   971 apply (induct_tac "xs")
   972  apply simp_all
   973 apply clarify
   974 apply (rename_tac i j) 
   975 apply (erule_tac n=i in natE) 
   976 apply (erule_tac [2] n=j in natE)
   977 apply (erule_tac n=j in natE, simp_all, force) 
   978 done
   979 
   980 lemma nth_list_update_eq [simp]:
   981      "[| i < length(xs); xs:list(A) |] ==> nth(i, list_update(xs, i,x)) = x"
   982 by (simp (no_asm_simp) add: lt_length_in_nat nth_list_update)
   983 
   984 
   985 lemma nth_list_update_neq [rule_format,simp]:
   986   "xs:list(A) ==> 
   987      \<forall>i \<in> nat. \<forall>j \<in> nat. i ~= j --> nth(j, list_update(xs,i,x)) = nth(j,xs)"
   988 apply (induct_tac "xs")
   989  apply (simp (no_asm))
   990 apply clarify
   991 apply (erule natE)
   992 apply (erule_tac [2] natE, simp_all) 
   993 apply (erule natE, simp_all)
   994 done
   995 
   996 lemma list_update_overwrite [rule_format,simp]:
   997      "xs:list(A) ==> \<forall>i \<in> nat. i < length(xs)
   998    --> list_update(list_update(xs, i, x), i, y) = list_update(xs, i,y)"
   999 apply (induct_tac "xs")
  1000  apply (simp (no_asm))
  1001 apply clarify
  1002 apply (erule natE, auto)
  1003 done
  1004 
  1005 lemma list_update_same_conv [rule_format]:
  1006      "xs:list(A) ==> 
  1007       \<forall>i \<in> nat. i < length(xs) --> 
  1008                  (list_update(xs, i, x) = xs) <-> (nth(i, xs) = x)"
  1009 apply (induct_tac "xs")
  1010  apply (simp (no_asm))
  1011 apply clarify
  1012 apply (erule natE, auto)
  1013 done
  1014 
  1015 lemma update_zip [rule_format]:
  1016      "ys:list(B) ==> 
  1017       \<forall>i \<in> nat. \<forall>xy \<in> A*B. \<forall>xs \<in> list(A).
  1018         length(xs) = length(ys) -->
  1019         list_update(zip(xs, ys), i, xy) = zip(list_update(xs, i, fst(xy)),
  1020                                               list_update(ys, i, snd(xy)))"
  1021 apply (induct_tac "ys")
  1022  apply auto
  1023 apply (erule_tac a = xs in list.cases)
  1024 apply (auto elim: natE)
  1025 done
  1026 
  1027 lemma set_update_subset_cons [rule_format]:
  1028   "xs:list(A) ==> 
  1029    \<forall>i \<in> nat. set_of_list(list_update(xs, i, x)) <= cons(x, set_of_list(xs))"
  1030 apply (induct_tac "xs")
  1031  apply simp
  1032 apply (rule ballI)
  1033 apply (erule natE, simp_all, auto)
  1034 done
  1035 
  1036 lemma set_of_list_update_subsetI:
  1037      "[| set_of_list(xs) <= A; xs:list(A); x:A; i:nat|]
  1038    ==> set_of_list(list_update(xs, i,x)) <= A"
  1039 apply (rule subset_trans)
  1040 apply (rule set_update_subset_cons, auto)
  1041 done
  1042 
  1043 (** upt **)
  1044 
  1045 lemma upt_rec:
  1046      "j:nat ==> upt(i,j) = (if i<j then Cons(i, upt(succ(i), j)) else Nil)"
  1047 apply (induct_tac "j", auto)
  1048 apply (drule not_lt_imp_le)
  1049 apply (auto simp: lt_Ord intro: le_anti_sym)
  1050 done
  1051 
  1052 lemma upt_conv_Nil [simp]: "[| j le i; j:nat |] ==> upt(i,j) = Nil"
  1053 apply (subst upt_rec, auto)
  1054 apply (auto simp add: le_iff)
  1055 apply (drule lt_asym [THEN notE], auto)
  1056 done
  1057 
  1058 (*Only needed if upt_Suc is deleted from the simpset*)
  1059 lemma upt_succ_append:
  1060      "[| i le j; j:nat |] ==> upt(i,succ(j)) = upt(i, j)@[j]"
  1061 by simp
  1062 
  1063 lemma upt_conv_Cons:
  1064      "[| i<j; j:nat |]  ==> upt(i,j) = Cons(i,upt(succ(i),j))"
  1065 apply (rule trans)
  1066 apply (rule upt_rec, auto)
  1067 done
  1068 
  1069 lemma upt_type [simp,TC]: "j:nat ==> upt(i,j):list(nat)"
  1070 by (induct_tac "j", auto)
  1071 
  1072 (*LOOPS as a simprule, since j<=j*)
  1073 lemma upt_add_eq_append:
  1074      "[| i le j; j:nat; k:nat |] ==> upt(i, j #+k) = upt(i,j)@upt(j,j#+k)"
  1075 apply (induct_tac "k")
  1076 apply (auto simp add: app_assoc app_type)
  1077 apply (rule_tac j = j in le_trans, auto)
  1078 done
  1079 
  1080 lemma length_upt [simp]: "[| i:nat; j:nat |] ==>length(upt(i,j)) = j #- i"
  1081 apply (induct_tac "j")
  1082 apply (rule_tac [2] sym)
  1083 apply (auto dest!: not_lt_imp_le simp add: length_app diff_succ diff_is_0_iff)
  1084 done
  1085 
  1086 lemma nth_upt [rule_format,simp]:
  1087      "[| i:nat; j:nat; k:nat |] ==> i #+ k < j --> nth(k, upt(i,j)) = i #+ k"
  1088 apply (induct_tac "j", simp)
  1089 apply (simp add: nth_append le_iff split add: nat_diff_split)
  1090 apply (auto dest!: not_lt_imp_le 
  1091             simp add: nth_append diff_self_eq_0 less_diff_conv add_commute)
  1092 apply (drule_tac j = x in lt_trans2, auto)
  1093 done
  1094 
  1095 lemma take_upt [rule_format,simp]:
  1096      "[| m:nat; n:nat |] ==>
  1097          \<forall>i \<in> nat. i #+ m le n --> take(m, upt(i,n)) = upt(i,i#+m)"
  1098 apply (induct_tac "m")
  1099 apply (simp (no_asm_simp) add: take_0)
  1100 apply clarify
  1101 apply (subst upt_rec, simp) 
  1102 apply (rule sym)
  1103 apply (subst upt_rec, simp) 
  1104 apply (simp_all del: upt.simps)
  1105 apply (rule_tac j = "succ (i #+ x) " in lt_trans2)
  1106 apply auto
  1107 done
  1108 
  1109 lemma map_succ_upt:
  1110      "[| m:nat; n:nat |] ==> map(succ, upt(m,n))= upt(succ(m), succ(n))"
  1111 apply (induct_tac "n")
  1112 apply (auto simp add: map_app_distrib)
  1113 done
  1114 
  1115 lemma nth_map [rule_format,simp]:
  1116      "xs:list(A) ==>
  1117       \<forall>n \<in> nat. n < length(xs) --> nth(n, map(f, xs)) = f(nth(n, xs))"
  1118 apply (induct_tac "xs", simp)
  1119 apply (rule ballI)
  1120 apply (induct_tac "n", auto)
  1121 done
  1122 
  1123 lemma nth_map_upt [rule_format]:
  1124      "[| m:nat; n:nat |] ==>
  1125       \<forall>i \<in> nat. i < n #- m --> nth(i, map(f, upt(m,n))) = f(m #+ i)"
  1126 apply (rule_tac n = m and m = n in diff_induct, typecheck, simp) 
  1127 apply simp 
  1128 apply (subst map_succ_upt [symmetric], simp_all, clarify) 
  1129 apply (subgoal_tac "i < length (upt (0, x))")
  1130  prefer 2 
  1131  apply (simp add: less_diff_conv) 
  1132  apply (rule_tac j = "succ (i #+ y) " in lt_trans2)
  1133   apply simp 
  1134  apply simp 
  1135 apply (subgoal_tac "i < length (upt (y, x))")
  1136  apply (simp_all add: add_commute less_diff_conv)
  1137 done
  1138 
  1139 (** sublist (a generalization of nth to sets) **)
  1140 
  1141 constdefs
  1142   sublist :: "[i, i] => i"
  1143     "sublist(xs, A)==
  1144      map(fst, (filter(%p. snd(p): A, zip(xs, upt(0,length(xs))))))"
  1145 
  1146 lemma sublist_0 [simp]: "xs:list(A) ==>sublist(xs, 0) =Nil"
  1147 by (unfold sublist_def, auto)
  1148 
  1149 lemma sublist_Nil [simp]: "sublist(Nil, A) = Nil"
  1150 by (unfold sublist_def, auto)
  1151 
  1152 lemma sublist_shift_lemma:
  1153  "[| xs:list(B); i:nat |] ==>
  1154   map(fst, filter(%p. snd(p):A, zip(xs, upt(i,i #+ length(xs))))) =
  1155   map(fst, filter(%p. snd(p):nat & snd(p) #+ i:A, zip(xs,upt(0,length(xs)))))"
  1156 apply (erule list_append_induct)
  1157 apply (simp (no_asm_simp))
  1158 apply (auto simp add: add_commute length_app filter_append map_app_distrib)
  1159 done
  1160 
  1161 lemma sublist_type [simp,TC]:
  1162      "xs:list(B) ==> sublist(xs, A):list(B)"
  1163 apply (unfold sublist_def)
  1164 apply (induct_tac "xs")
  1165 apply (auto simp add: filter_append map_app_distrib)
  1166 done
  1167 
  1168 lemma upt_add_eq_append2:
  1169      "[| i:nat; j:nat |] ==> upt(0, i #+ j) = upt(0, i) @ upt(i, i #+ j)"
  1170 by (simp add: upt_add_eq_append [of 0] nat_0_le)
  1171 
  1172 lemma sublist_append:
  1173  "[| xs:list(B); ys:list(B)  |] ==>
  1174   sublist(xs@ys, A) = sublist(xs, A) @ sublist(ys, {j:nat. j #+ length(xs): A})"
  1175 apply (unfold sublist_def)
  1176 apply (erule_tac l = ys in list_append_induct, simp)
  1177 apply (simp (no_asm_simp) add: upt_add_eq_append2 app_assoc [symmetric])
  1178 apply (auto simp add: sublist_shift_lemma length_type map_app_distrib app_assoc)
  1179 apply (simp_all add: add_commute)
  1180 done
  1181 
  1182 
  1183 lemma sublist_Cons:
  1184      "[| xs:list(B); x:B |] ==>
  1185       sublist(Cons(x, xs), A) = 
  1186       (if 0:A then [x] else []) @ sublist(xs, {j:nat. succ(j) : A})"
  1187 apply (erule_tac l = xs in list_append_induct)
  1188 apply (simp (no_asm_simp) add: sublist_def)  
  1189 apply (simp del: app_Cons add: app_Cons [symmetric] sublist_append, simp) 
  1190 done
  1191 
  1192 lemma sublist_singleton [simp]:
  1193      "sublist([x], A) = (if 0 : A then [x] else [])"
  1194 by (simp (no_asm) add: sublist_Cons)
  1195 
  1196 
  1197 lemma sublist_upt_eq_take [simp]:
  1198     "[| xs:list(A); n:nat |] ==> sublist(xs, less_than(n)) = take(n,xs)"
  1199 apply (unfold less_than_def)
  1200 apply (erule_tac l = xs in list_append_induct, simp) 
  1201 apply (simp split add: nat_diff_split add: sublist_append, auto)
  1202 apply (subgoal_tac "n #- length (y) = 0")
  1203 apply (simp (no_asm_simp))
  1204 apply (auto dest!: not_lt_imp_le simp add: diff_is_0_iff)
  1205 done
  1206 
  1207 text{*Repetition of a List Element*}
  1208 
  1209 consts   repeat :: "[i,i]=>i"
  1210 primrec
  1211   "repeat(a,0) = []"
  1212 
  1213   "repeat(a,succ(n)) = Cons(a,repeat(a,n))"
  1214 
  1215 lemma length_repeat: "n \<in> nat ==> length(repeat(a,n)) = n"
  1216 by (induct_tac n, auto)
  1217 
  1218 lemma repeat_succ_app: "n \<in> nat ==> repeat(a,succ(n)) = repeat(a,n) @ [a]"
  1219 apply (induct_tac n)
  1220 apply (simp_all del: app_Cons add: app_Cons [symmetric])
  1221 done
  1222 
  1223 lemma repeat_type [TC]: "[|a \<in> A; n \<in> nat|] ==> repeat(a,n) \<in> list(A)"
  1224 by (induct_tac n, auto)
  1225 
  1226 
  1227 ML
  1228 {*
  1229 val ConsE = thm "ConsE";
  1230 val Cons_iff = thm "Cons_iff";
  1231 val Nil_Cons_iff = thm "Nil_Cons_iff";
  1232 val list_unfold = thm "list_unfold";
  1233 val list_mono = thm "list_mono";
  1234 val list_univ = thm "list_univ";
  1235 val list_subset_univ = thm "list_subset_univ";
  1236 val list_into_univ = thm "list_into_univ";
  1237 val list_case_type = thm "list_case_type";
  1238 val tl_type = thm "tl_type";
  1239 val drop_Nil = thm "drop_Nil";
  1240 val drop_succ_Cons = thm "drop_succ_Cons";
  1241 val drop_type = thm "drop_type";
  1242 val list_rec_type = thm "list_rec_type";
  1243 val map_type = thm "map_type";
  1244 val map_type2 = thm "map_type2";
  1245 val length_type = thm "length_type";
  1246 val lt_length_in_nat = thm "lt_length_in_nat";
  1247 val app_type = thm "app_type";
  1248 val rev_type = thm "rev_type";
  1249 val flat_type = thm "flat_type";
  1250 val set_of_list_type = thm "set_of_list_type";
  1251 val set_of_list_append = thm "set_of_list_append";
  1252 val list_add_type = thm "list_add_type";
  1253 val map_ident = thm "map_ident";
  1254 val map_compose = thm "map_compose";
  1255 val map_app_distrib = thm "map_app_distrib";
  1256 val map_flat = thm "map_flat";
  1257 val list_rec_map = thm "list_rec_map";
  1258 val list_CollectD = thm "list_CollectD";
  1259 val map_list_Collect = thm "map_list_Collect";
  1260 val length_map = thm "length_map";
  1261 val length_app = thm "length_app";
  1262 val length_rev = thm "length_rev";
  1263 val length_flat = thm "length_flat";
  1264 val drop_length_Cons = thm "drop_length_Cons";
  1265 val drop_length = thm "drop_length";
  1266 val app_right_Nil = thm "app_right_Nil";
  1267 val app_assoc = thm "app_assoc";
  1268 val flat_app_distrib = thm "flat_app_distrib";
  1269 val rev_map_distrib = thm "rev_map_distrib";
  1270 val rev_app_distrib = thm "rev_app_distrib";
  1271 val rev_rev_ident = thm "rev_rev_ident";
  1272 val rev_flat = thm "rev_flat";
  1273 val list_add_app = thm "list_add_app";
  1274 val list_add_rev = thm "list_add_rev";
  1275 val list_add_flat = thm "list_add_flat";
  1276 val list_append_induct = thm "list_append_induct";
  1277 val min_sym = thm "min_sym";
  1278 val min_type = thm "min_type";
  1279 val min_0 = thm "min_0";
  1280 val min_02 = thm "min_02";
  1281 val lt_min_iff = thm "lt_min_iff";
  1282 val min_succ_succ = thm "min_succ_succ";
  1283 val filter_append = thm "filter_append";
  1284 val filter_type = thm "filter_type";
  1285 val length_filter = thm "length_filter";
  1286 val filter_is_subset = thm "filter_is_subset";
  1287 val filter_False = thm "filter_False";
  1288 val filter_True = thm "filter_True";
  1289 val length_is_0_iff = thm "length_is_0_iff";
  1290 val length_is_0_iff2 = thm "length_is_0_iff2";
  1291 val length_tl = thm "length_tl";
  1292 val length_greater_0_iff = thm "length_greater_0_iff";
  1293 val length_succ_iff = thm "length_succ_iff";
  1294 val append_is_Nil_iff = thm "append_is_Nil_iff";
  1295 val append_is_Nil_iff2 = thm "append_is_Nil_iff2";
  1296 val append_left_is_self_iff = thm "append_left_is_self_iff";
  1297 val append_left_is_self_iff2 = thm "append_left_is_self_iff2";
  1298 val append_left_is_Nil_iff = thm "append_left_is_Nil_iff";
  1299 val append_left_is_Nil_iff2 = thm "append_left_is_Nil_iff2";
  1300 val append_eq_append_iff = thm "append_eq_append_iff";
  1301 val append_eq_append = thm "append_eq_append";
  1302 val append_eq_append_iff2 = thm "append_eq_append_iff2";
  1303 val append_self_iff = thm "append_self_iff";
  1304 val append_self_iff2 = thm "append_self_iff2";
  1305 val append1_eq_iff = thm "append1_eq_iff";
  1306 val append_right_is_self_iff = thm "append_right_is_self_iff";
  1307 val append_right_is_self_iff2 = thm "append_right_is_self_iff2";
  1308 val hd_append = thm "hd_append";
  1309 val tl_append = thm "tl_append";
  1310 val rev_is_Nil_iff = thm "rev_is_Nil_iff";
  1311 val Nil_is_rev_iff = thm "Nil_is_rev_iff";
  1312 val rev_is_rev_iff = thm "rev_is_rev_iff";
  1313 val rev_list_elim = thm "rev_list_elim";
  1314 val length_drop = thm "length_drop";
  1315 val drop_all = thm "drop_all";
  1316 val drop_append = thm "drop_append";
  1317 val drop_drop = thm "drop_drop";
  1318 val take_0 = thm "take_0";
  1319 val take_succ_Cons = thm "take_succ_Cons";
  1320 val take_Nil = thm "take_Nil";
  1321 val take_all = thm "take_all";
  1322 val take_type = thm "take_type";
  1323 val take_append = thm "take_append";
  1324 val take_take = thm "take_take";
  1325 val nth_0 = thm "nth_0";
  1326 val nth_Cons = thm "nth_Cons";
  1327 val nth_type = thm "nth_type";
  1328 val nth_append = thm "nth_append";
  1329 val set_of_list_conv_nth = thm "set_of_list_conv_nth";
  1330 val nth_take_lemma = thm "nth_take_lemma";
  1331 val nth_equalityI = thm "nth_equalityI";
  1332 val take_equalityI = thm "take_equalityI";
  1333 val nth_drop = thm "nth_drop";
  1334 val list_on_set_of_list = thm "list_on_set_of_list";
  1335 val zip_Nil = thm "zip_Nil";
  1336 val zip_Nil2 = thm "zip_Nil2";
  1337 val zip_Cons_Cons = thm "zip_Cons_Cons";
  1338 val zip_type = thm "zip_type";
  1339 val length_zip = thm "length_zip";
  1340 val zip_append1 = thm "zip_append1";
  1341 val zip_append2 = thm "zip_append2";
  1342 val zip_append = thm "zip_append";
  1343 val zip_rev = thm "zip_rev";
  1344 val nth_zip = thm "nth_zip";
  1345 val set_of_list_zip = thm "set_of_list_zip";
  1346 val list_update_Nil = thm "list_update_Nil";
  1347 val list_update_Cons_0 = thm "list_update_Cons_0";
  1348 val list_update_Cons_succ = thm "list_update_Cons_succ";
  1349 val list_update_type = thm "list_update_type";
  1350 val length_list_update = thm "length_list_update";
  1351 val nth_list_update = thm "nth_list_update";
  1352 val nth_list_update_eq = thm "nth_list_update_eq";
  1353 val nth_list_update_neq = thm "nth_list_update_neq";
  1354 val list_update_overwrite = thm "list_update_overwrite";
  1355 val list_update_same_conv = thm "list_update_same_conv";
  1356 val update_zip = thm "update_zip";
  1357 val set_update_subset_cons = thm "set_update_subset_cons";
  1358 val set_of_list_update_subsetI = thm "set_of_list_update_subsetI";
  1359 val upt_rec = thm "upt_rec";
  1360 val upt_conv_Nil = thm "upt_conv_Nil";
  1361 val upt_succ_append = thm "upt_succ_append";
  1362 val upt_conv_Cons = thm "upt_conv_Cons";
  1363 val upt_type = thm "upt_type";
  1364 val upt_add_eq_append = thm "upt_add_eq_append";
  1365 val length_upt = thm "length_upt";
  1366 val nth_upt = thm "nth_upt";
  1367 val take_upt = thm "take_upt";
  1368 val map_succ_upt = thm "map_succ_upt";
  1369 val nth_map = thm "nth_map";
  1370 val nth_map_upt = thm "nth_map_upt";
  1371 val sublist_0 = thm "sublist_0";
  1372 val sublist_Nil = thm "sublist_Nil";
  1373 val sublist_shift_lemma = thm "sublist_shift_lemma";
  1374 val sublist_type = thm "sublist_type";
  1375 val upt_add_eq_append2 = thm "upt_add_eq_append2";
  1376 val sublist_append = thm "sublist_append";
  1377 val sublist_Cons = thm "sublist_Cons";
  1378 val sublist_singleton = thm "sublist_singleton";
  1379 val sublist_upt_eq_take = thm "sublist_upt_eq_take";
  1380 
  1381 structure list =
  1382 struct
  1383 val induct = thm "list.induct"
  1384 val elim   = thm "list.cases"
  1385 val intrs  = thms "list.intros"
  1386 end;  
  1387 *}
  1388 
  1389 end