src/HOL/List.thy
author paulson
Thu Feb 19 10:40:28 2004 +0100 (2004-02-19)
changeset 14395 cc96cc06abf9
parent 14388 04f04408b99b
child 14402 4201e1916482
permissions -rw-r--r--
new theorem
     1 (*  Title:      HOL/List.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 *)
     6 
     7 header {* The datatype of finite lists *}
     8 
     9 theory List = PreList:
    10 
    11 datatype 'a list =
    12     Nil    ("[]")
    13   | Cons 'a  "'a list"    (infixr "#" 65)
    14 
    15 consts
    16   "@" :: "'a list => 'a list => 'a list"    (infixr 65)
    17   filter:: "('a => bool) => 'a list => 'a list"
    18   concat:: "'a list list => 'a list"
    19   foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b"
    20   foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b"
    21   fold_rel :: "('a * 'c * 'a) set => ('a * 'c list * 'a) set"
    22   hd:: "'a list => 'a"
    23   tl:: "'a list => 'a list"
    24   last:: "'a list => 'a"
    25   butlast :: "'a list => 'a list"
    26   set :: "'a list => 'a set"
    27   o2l :: "'a option => 'a list"
    28   list_all:: "('a => bool) => ('a list => bool)"
    29   list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool"
    30   map :: "('a=>'b) => ('a list => 'b list)"
    31   mem :: "'a => 'a list => bool"    (infixl 55)
    32   nth :: "'a list => nat => 'a"    (infixl "!" 100)
    33   list_update :: "'a list => nat => 'a => 'a list"
    34   take:: "nat => 'a list => 'a list"
    35   drop:: "nat => 'a list => 'a list"
    36   takeWhile :: "('a => bool) => 'a list => 'a list"
    37   dropWhile :: "('a => bool) => 'a list => 'a list"
    38   rev :: "'a list => 'a list"
    39   zip :: "'a list => 'b list => ('a * 'b) list"
    40   upt :: "nat => nat => nat list" ("(1[_../_'(])")
    41   remdups :: "'a list => 'a list"
    42   null:: "'a list => bool"
    43   "distinct":: "'a list => bool"
    44   replicate :: "nat => 'a => 'a list"
    45   postfix :: "'a list => 'a list => bool"
    46 
    47 syntax (xsymbols)
    48   postfix :: "'a list => 'a list => bool"             ("(_/ \<sqsupseteq> _)" [51, 51] 50)
    49 
    50 nonterminals lupdbinds lupdbind
    51 
    52 syntax
    53   -- {* list Enumeration *}
    54   "@list" :: "args => 'a list"    ("[(_)]")
    55 
    56   -- {* Special syntax for filter *}
    57   "@filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_:_./ _])")
    58 
    59   -- {* list update *}
    60   "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
    61   "" :: "lupdbind => lupdbinds"    ("_")
    62   "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
    63   "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
    64 
    65   upto:: "nat => nat => nat list"    ("(1[_../_])")
    66 
    67 translations
    68   "[x, xs]" == "x#[xs]"
    69   "[x]" == "x#[]"
    70   "[x:xs . P]"== "filter (%x. P) xs"
    71 
    72   "_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs"
    73   "xs[i:=x]" == "list_update xs i x"
    74 
    75   "[i..j]" == "[i..(Suc j)(]"
    76 
    77 
    78 syntax (xsymbols)
    79   "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])")
    80 
    81 
    82 text {*
    83   Function @{text size} is overloaded for all datatypes.Users may
    84   refer to the list version as @{text length}. *}
    85 
    86 syntax length :: "'a list => nat"
    87 translations "length" => "size :: _ list => nat"
    88 
    89 typed_print_translation {*
    90   let
    91     fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
    92           Syntax.const "length" $ t
    93       | size_tr' _ _ _ = raise Match;
    94   in [("size", size_tr')] end
    95 *}
    96 
    97 primrec
    98 "hd(x#xs) = x"
    99 primrec
   100 "tl([]) = []"
   101 "tl(x#xs) = xs"
   102 primrec
   103 "null([]) = True"
   104 "null(x#xs) = False"
   105 primrec
   106 "last(x#xs) = (if xs=[] then x else last xs)"
   107 primrec
   108 "butlast []= []"
   109 "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
   110 primrec
   111 "x mem [] = False"
   112 "x mem (y#ys) = (if y=x then True else x mem ys)"
   113 primrec
   114 "set [] = {}"
   115 "set (x#xs) = insert x (set xs)"
   116 primrec
   117  "o2l  None    = []"
   118  "o2l (Some x) = [x]"
   119 primrec
   120 list_all_Nil:"list_all P [] = True"
   121 list_all_Cons: "list_all P (x#xs) = (P(x) \<and> list_all P xs)"
   122 primrec
   123 "map f [] = []"
   124 "map f (x#xs) = f(x)#map f xs"
   125 primrec
   126 append_Nil:"[]@ys = ys"
   127 append_Cons: "(x#xs)@ys = x#(xs@ys)"
   128 primrec
   129 "rev([]) = []"
   130 "rev(x#xs) = rev(xs) @ [x]"
   131 primrec
   132 "filter P [] = []"
   133 "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
   134 primrec
   135 foldl_Nil:"foldl f a [] = a"
   136 foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
   137 primrec
   138 "foldr f [] a = a"
   139 "foldr f (x#xs) a = f x (foldr f xs a)"
   140 primrec
   141 "concat([]) = []"
   142 "concat(x#xs) = x @ concat(xs)"
   143 primrec
   144 drop_Nil:"drop n [] = []"
   145 drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
   146 -- {* Warning: simpset does not contain this definition *}
   147 -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
   148 primrec
   149 take_Nil:"take n [] = []"
   150 take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
   151 -- {* Warning: simpset does not contain this definition *}
   152 -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
   153 primrec
   154 nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
   155 -- {* Warning: simpset does not contain this definition *}
   156 -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
   157 primrec
   158 "[][i:=v] = []"
   159 "(x#xs)[i:=v] =
   160 (case i of 0 => v # xs
   161 | Suc j => x # xs[j:=v])"
   162 primrec
   163 "takeWhile P [] = []"
   164 "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
   165 primrec
   166 "dropWhile P [] = []"
   167 "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
   168 primrec
   169 "zip xs [] = []"
   170 zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
   171 -- {* Warning: simpset does not contain this definition *}
   172 -- {* but separate theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
   173 primrec
   174 upt_0: "[i..0(] = []"
   175 upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
   176 primrec
   177 "distinct [] = True"
   178 "distinct (x#xs) = (x ~: set xs \<and> distinct xs)"
   179 primrec
   180 "remdups [] = []"
   181 "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
   182 primrec
   183 replicate_0: "replicate 0 x = []"
   184 replicate_Suc: "replicate (Suc n) x = x # replicate n x"
   185 defs
   186  postfix_def: "postfix xs ys == \<exists>zs. xs = zs @ ys"
   187 defs
   188  list_all2_def:
   189  "list_all2 P xs ys == length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)"
   190 
   191 
   192 subsection {* Lexicographic orderings on lists *}
   193 
   194 consts
   195 lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
   196 primrec
   197 "lexn r 0 = {}"
   198 "lexn r (Suc n) =
   199 (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
   200 {(xs,ys). length xs = Suc n \<and> length ys = Suc n}"
   201 
   202 constdefs
   203 lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
   204 "lex r == \<Union>n. lexn r n"
   205 
   206 lexico :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
   207 "lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
   208 
   209 sublist :: "'a list => nat set => 'a list"
   210 "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))"
   211 
   212 
   213 lemma not_Cons_self [simp]: "xs \<noteq> x # xs"
   214 by (induct xs) auto
   215 
   216 lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
   217 
   218 lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
   219 by (induct xs) auto
   220 
   221 lemma length_induct:
   222 "(!!xs. \<forall>ys. length ys < length xs --> P ys ==> P xs) ==> P xs"
   223 by (rule measure_induct [of length]) rules
   224 
   225 
   226 subsection {* @{text lists}: the list-forming operator over sets *}
   227 
   228 consts lists :: "'a set => 'a list set"
   229 inductive "lists A"
   230 intros
   231 Nil [intro!]: "[]: lists A"
   232 Cons [intro!]: "[| a: A;l: lists A|] ==> a#l : lists A"
   233 
   234 inductive_cases listsE [elim!]: "x#l : lists A"
   235 
   236 lemma lists_mono [mono]: "A \<subseteq> B ==> lists A \<subseteq> lists B"
   237 by (unfold lists.defs) (blast intro!: lfp_mono)
   238 
   239 lemma lists_IntI:
   240   assumes l: "l: lists A" shows "l: lists B ==> l: lists (A Int B)" using l
   241   by induct blast+
   242 
   243 lemma lists_Int_eq [simp]: "lists (A \<inter> B) = lists A \<inter> lists B"
   244 apply (rule mono_Int [THEN equalityI])
   245 apply (simp add: mono_def lists_mono)
   246 apply (blast intro!: lists_IntI)
   247 done
   248 
   249 lemma append_in_lists_conv [iff]:
   250 "(xs @ ys : lists A) = (xs : lists A \<and> ys : lists A)"
   251 by (induct xs) auto
   252 
   253 
   254 subsection {* @{text length} *}
   255 
   256 text {*
   257 Needs to come before @{text "@"} because of theorem @{text
   258 append_eq_append_conv}.
   259 *}
   260 
   261 lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
   262 by (induct xs) auto
   263 
   264 lemma length_map [simp]: "length (map f xs) = length xs"
   265 by (induct xs) auto
   266 
   267 lemma length_rev [simp]: "length (rev xs) = length xs"
   268 by (induct xs) auto
   269 
   270 lemma length_tl [simp]: "length (tl xs) = length xs - 1"
   271 by (cases xs) auto
   272 
   273 lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
   274 by (induct xs) auto
   275 
   276 lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
   277 by (induct xs) auto
   278 
   279 lemma length_Suc_conv:
   280 "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
   281 by (induct xs) auto
   282 
   283 lemma Suc_length_conv:
   284 "(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
   285 apply (induct xs, simp, simp)
   286 apply blast
   287 done
   288 
   289 lemma impossible_Cons [rule_format]: 
   290   "length xs <= length ys --> xs = x # ys = False"
   291 apply (induct xs, auto)
   292 done
   293 
   294 lemma list_induct2[consumes 1]: "\<And>ys.
   295  \<lbrakk> length xs = length ys;
   296    P [] [];
   297    \<And>x xs y ys. \<lbrakk> length xs = length ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
   298  \<Longrightarrow> P xs ys"
   299 apply(induct xs)
   300  apply simp
   301 apply(case_tac ys)
   302  apply simp
   303 apply(simp)
   304 done
   305 
   306 subsection {* @{text "@"} -- append *}
   307 
   308 lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
   309 by (induct xs) auto
   310 
   311 lemma append_Nil2 [simp]: "xs @ [] = xs"
   312 by (induct xs) auto
   313 
   314 lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
   315 by (induct xs) auto
   316 
   317 lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
   318 by (induct xs) auto
   319 
   320 lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
   321 by (induct xs) auto
   322 
   323 lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
   324 by (induct xs) auto
   325 
   326 lemma append_eq_append_conv [simp]:
   327  "!!ys. length xs = length ys \<or> length us = length vs
   328  ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
   329 apply (induct xs)
   330  apply (case_tac ys, simp, force)
   331 apply (case_tac ys, force, simp)
   332 done
   333 
   334 lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
   335 by simp
   336 
   337 lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
   338 by simp
   339 
   340 lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
   341 by simp
   342 
   343 lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
   344 using append_same_eq [of _ _ "[]"] by auto
   345 
   346 lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
   347 using append_same_eq [of "[]"] by auto
   348 
   349 lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
   350 by (induct xs) auto
   351 
   352 lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
   353 by (induct xs) auto
   354 
   355 lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
   356 by (simp add: hd_append split: list.split)
   357 
   358 lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
   359 by (simp split: list.split)
   360 
   361 lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
   362 by (simp add: tl_append split: list.split)
   363 
   364 
   365 lemma Cons_eq_append_conv: "x#xs = ys@zs =
   366  (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"
   367 by(cases ys) auto
   368 
   369 
   370 text {* Trivial rules for solving @{text "@"}-equations automatically. *}
   371 
   372 lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
   373 by simp
   374 
   375 lemma Cons_eq_appendI:
   376 "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
   377 by (drule sym) simp
   378 
   379 lemma append_eq_appendI:
   380 "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
   381 by (drule sym) simp
   382 
   383 
   384 text {*
   385 Simplification procedure for all list equalities.
   386 Currently only tries to rearrange @{text "@"} to see if
   387 - both lists end in a singleton list,
   388 - or both lists end in the same list.
   389 *}
   390 
   391 ML_setup {*
   392 local
   393 
   394 val append_assoc = thm "append_assoc";
   395 val append_Nil = thm "append_Nil";
   396 val append_Cons = thm "append_Cons";
   397 val append1_eq_conv = thm "append1_eq_conv";
   398 val append_same_eq = thm "append_same_eq";
   399 
   400 fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
   401   (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
   402   | last (Const("List.op @",_) $ _ $ ys) = last ys
   403   | last t = t;
   404 
   405 fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
   406   | list1 _ = false;
   407 
   408 fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
   409   (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
   410   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
   411   | butlast xs = Const("List.list.Nil",fastype_of xs);
   412 
   413 val rearr_tac =
   414   simp_tac (HOL_basic_ss addsimps [append_assoc, append_Nil, append_Cons]);
   415 
   416 fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
   417   let
   418     val lastl = last lhs and lastr = last rhs;
   419     fun rearr conv =
   420       let
   421         val lhs1 = butlast lhs and rhs1 = butlast rhs;
   422         val Type(_,listT::_) = eqT
   423         val appT = [listT,listT] ---> listT
   424         val app = Const("List.op @",appT)
   425         val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
   426         val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
   427         val thm = Tactic.prove sg [] [] eq (K (rearr_tac 1));
   428       in Some ((conv RS (thm RS trans)) RS eq_reflection) end;
   429 
   430   in
   431     if list1 lastl andalso list1 lastr then rearr append1_eq_conv
   432     else if lastl aconv lastr then rearr append_same_eq
   433     else None
   434   end;
   435 
   436 in
   437 
   438 val list_eq_simproc =
   439   Simplifier.simproc (Theory.sign_of (the_context ())) "list_eq" ["(xs::'a list) = ys"] list_eq;
   440 
   441 end;
   442 
   443 Addsimprocs [list_eq_simproc];
   444 *}
   445 
   446 
   447 subsection {* @{text map} *}
   448 
   449 lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
   450 by (induct xs) simp_all
   451 
   452 lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
   453 by (rule ext, induct_tac xs) auto
   454 
   455 lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
   456 by (induct xs) auto
   457 
   458 lemma map_compose: "map (f o g) xs = map f (map g xs)"
   459 by (induct xs) (auto simp add: o_def)
   460 
   461 lemma rev_map: "rev (map f xs) = map f (rev xs)"
   462 by (induct xs) auto
   463 
   464 lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
   465 by (induct xs) auto
   466 
   467 lemma map_cong [recdef_cong]:
   468 "xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
   469 -- {* a congruence rule for @{text map} *}
   470 by simp
   471 
   472 lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
   473 by (cases xs) auto
   474 
   475 lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
   476 by (cases xs) auto
   477 
   478 lemma map_eq_Cons_conv[iff]:
   479  "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
   480 by (cases xs) auto
   481 
   482 lemma Cons_eq_map_conv[iff]:
   483  "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
   484 by (cases ys) auto
   485 
   486 lemma ex_map_conv:
   487   "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
   488 by(induct ys, auto)
   489 
   490 lemma map_injective:
   491  "!!xs. map f xs = map f ys ==> inj f ==> xs = ys"
   492 by (induct ys) (auto dest!:injD)
   493 
   494 lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
   495 by(blast dest:map_injective)
   496 
   497 lemma inj_mapI: "inj f ==> inj (map f)"
   498 by (rules dest: map_injective injD intro: inj_onI)
   499 
   500 lemma inj_mapD: "inj (map f) ==> inj f"
   501 apply (unfold inj_on_def, clarify)
   502 apply (erule_tac x = "[x]" in ballE)
   503  apply (erule_tac x = "[y]" in ballE, simp, blast)
   504 apply blast
   505 done
   506 
   507 lemma inj_map[iff]: "inj (map f) = inj f"
   508 by (blast dest: inj_mapD intro: inj_mapI)
   509 
   510 lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"
   511 by (induct xs, auto)
   512 
   513 subsection {* @{text rev} *}
   514 
   515 lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
   516 by (induct xs) auto
   517 
   518 lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
   519 by (induct xs) auto
   520 
   521 lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
   522 by (induct xs) auto
   523 
   524 lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
   525 by (induct xs) auto
   526 
   527 lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
   528 apply (induct xs, force)
   529 apply (case_tac ys, simp, force)
   530 done
   531 
   532 lemma rev_induct [case_names Nil snoc]:
   533   "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
   534 apply(subst rev_rev_ident[symmetric])
   535 apply(rule_tac list = "rev xs" in list.induct, simp_all)
   536 done
   537 
   538 ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility"
   539 
   540 lemma rev_exhaust [case_names Nil snoc]:
   541   "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
   542 by (induct xs rule: rev_induct) auto
   543 
   544 lemmas rev_cases = rev_exhaust
   545 
   546 
   547 subsection {* @{text set} *}
   548 
   549 lemma finite_set [iff]: "finite (set xs)"
   550 by (induct xs) auto
   551 
   552 lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
   553 by (induct xs) auto
   554 
   555 lemma hd_in_set: "l = x#xs \<Longrightarrow> x\<in>set l"
   556 by (case_tac l, auto)
   557 
   558 lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
   559 by auto
   560 
   561 lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
   562 by auto
   563 
   564 lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
   565 by (induct xs) auto
   566 
   567 lemma set_rev [simp]: "set (rev xs) = set xs"
   568 by (induct xs) auto
   569 
   570 lemma set_map [simp]: "set (map f xs) = f`(set xs)"
   571 by (induct xs) auto
   572 
   573 lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
   574 by (induct xs) auto
   575 
   576 lemma set_upt [simp]: "set[i..j(] = {k. i \<le> k \<and> k < j}"
   577 apply (induct j, simp_all)
   578 apply (erule ssubst, auto)
   579 done
   580 
   581 lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
   582 apply (induct xs, simp, simp)
   583 apply (rule iffI)
   584  apply (blast intro: eq_Nil_appendI Cons_eq_appendI)
   585 apply (erule exE)+
   586 apply (case_tac ys, auto)
   587 done
   588 
   589 lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)"
   590 -- {* eliminate @{text lists} in favour of @{text set} *}
   591 by (induct xs) auto
   592 
   593 lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A"
   594 by (rule in_lists_conv_set [THEN iffD1])
   595 
   596 lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A"
   597 by (rule in_lists_conv_set [THEN iffD2])
   598 
   599 lemma finite_list: "finite A ==> EX l. set l = A"
   600 apply (erule finite_induct, auto)
   601 apply (rule_tac x="x#l" in exI, auto)
   602 done
   603 
   604 lemma card_length: "card (set xs) \<le> length xs"
   605 by (induct xs) (auto simp add: card_insert_if)
   606 
   607 subsection {* @{text mem} *}
   608 
   609 lemma set_mem_eq: "(x mem xs) = (x : set xs)"
   610 by (induct xs) auto
   611 
   612 
   613 subsection {* @{text list_all} *}
   614 
   615 lemma list_all_conv: "list_all P xs = (\<forall>x \<in> set xs. P x)"
   616 by (induct xs) auto
   617 
   618 lemma list_all_append [simp]:
   619 "list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)"
   620 by (induct xs) auto
   621 
   622 
   623 subsection {* @{text filter} *}
   624 
   625 lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
   626 by (induct xs) auto
   627 
   628 lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
   629 by (induct xs) auto
   630 
   631 lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
   632 by (induct xs) auto
   633 
   634 lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
   635 by (induct xs) auto
   636 
   637 lemma length_filter [simp]: "length (filter P xs) \<le> length xs"
   638 by (induct xs) (auto simp add: le_SucI)
   639 
   640 lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
   641 by auto
   642 
   643 
   644 subsection {* @{text concat} *}
   645 
   646 lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
   647 by (induct xs) auto
   648 
   649 lemma concat_eq_Nil_conv [iff]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
   650 by (induct xss) auto
   651 
   652 lemma Nil_eq_concat_conv [iff]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
   653 by (induct xss) auto
   654 
   655 lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)"
   656 by (induct xs) auto
   657 
   658 lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
   659 by (induct xs) auto
   660 
   661 lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
   662 by (induct xs) auto
   663 
   664 lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
   665 by (induct xs) auto
   666 
   667 
   668 subsection {* @{text nth} *}
   669 
   670 lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
   671 by auto
   672 
   673 lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
   674 by auto
   675 
   676 declare nth.simps [simp del]
   677 
   678 lemma nth_append:
   679 "!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
   680 apply (induct "xs", simp)
   681 apply (case_tac n, auto)
   682 done
   683 
   684 lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)"
   685 apply (induct xs, simp)
   686 apply (case_tac n, auto)
   687 done
   688 
   689 lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
   690 apply (induct_tac xs, simp, simp)
   691 apply safe
   692 apply (rule_tac x = 0 in exI, simp)
   693  apply (rule_tac x = "Suc i" in exI, simp)
   694 apply (case_tac i, simp)
   695 apply (rename_tac j)
   696 apply (rule_tac x = j in exI, simp)
   697 done
   698 
   699 lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
   700 by (auto simp add: set_conv_nth)
   701 
   702 lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
   703 by (auto simp add: set_conv_nth)
   704 
   705 lemma all_nth_imp_all_set:
   706 "[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
   707 by (auto simp add: set_conv_nth)
   708 
   709 lemma all_set_conv_all_nth:
   710 "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
   711 by (auto simp add: set_conv_nth)
   712 
   713 
   714 subsection {* @{text list_update} *}
   715 
   716 lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs"
   717 by (induct xs) (auto split: nat.split)
   718 
   719 lemma nth_list_update:
   720 "!!i j. i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
   721 by (induct xs) (auto simp add: nth_Cons split: nat.split)
   722 
   723 lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
   724 by (simp add: nth_list_update)
   725 
   726 lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j"
   727 by (induct xs) (auto simp add: nth_Cons split: nat.split)
   728 
   729 lemma list_update_overwrite [simp]:
   730 "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
   731 by (induct xs) (auto split: nat.split)
   732 
   733 lemma list_update_id[simp]: "!!i. i < length xs \<Longrightarrow> xs[i := xs!i] = xs"
   734 apply (induct xs, simp)
   735 apply(simp split:nat.splits)
   736 done
   737 
   738 lemma list_update_same_conv:
   739 "!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
   740 by (induct xs) (auto split: nat.split)
   741 
   742 lemma list_update_append1:
   743  "!!i. i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
   744 apply (induct xs, simp)
   745 apply(simp split:nat.split)
   746 done
   747 
   748 lemma update_zip:
   749 "!!i xy xs. length xs = length ys ==>
   750 (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
   751 by (induct ys) (auto, case_tac xs, auto split: nat.split)
   752 
   753 lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"
   754 by (induct xs) (auto split: nat.split)
   755 
   756 lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
   757 by (blast dest!: set_update_subset_insert [THEN subsetD])
   758 
   759 
   760 subsection {* @{text last} and @{text butlast} *}
   761 
   762 lemma last_snoc [simp]: "last (xs @ [x]) = x"
   763 by (induct xs) auto
   764 
   765 lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
   766 by (induct xs) auto
   767 
   768 lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
   769 by(simp add:last.simps)
   770 
   771 lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
   772 by(simp add:last.simps)
   773 
   774 lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
   775 by (induct xs) (auto)
   776 
   777 lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
   778 by(simp add:last_append)
   779 
   780 lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
   781 by(simp add:last_append)
   782 
   783 
   784 
   785 lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
   786 by (induct xs rule: rev_induct) auto
   787 
   788 lemma butlast_append:
   789 "!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
   790 by (induct xs) auto
   791 
   792 lemma append_butlast_last_id [simp]:
   793 "xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
   794 by (induct xs) auto
   795 
   796 lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
   797 by (induct xs) (auto split: split_if_asm)
   798 
   799 lemma in_set_butlast_appendI:
   800 "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
   801 by (auto dest: in_set_butlastD simp add: butlast_append)
   802 
   803 
   804 subsection {* @{text take} and @{text drop} *}
   805 
   806 lemma take_0 [simp]: "take 0 xs = []"
   807 by (induct xs) auto
   808 
   809 lemma drop_0 [simp]: "drop 0 xs = xs"
   810 by (induct xs) auto
   811 
   812 lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
   813 by simp
   814 
   815 lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
   816 by simp
   817 
   818 declare take_Cons [simp del] and drop_Cons [simp del]
   819 
   820 lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
   821 by(cases xs, simp_all)
   822 
   823 lemma drop_tl: "!!n. drop n (tl xs) = tl(drop n xs)"
   824 by(induct xs, simp_all add:drop_Cons drop_Suc split:nat.split)
   825 
   826 lemma nth_via_drop: "!!n. drop n xs = y#ys \<Longrightarrow> xs!n = y"
   827 apply (induct xs, simp)
   828 apply(simp add:drop_Cons nth_Cons split:nat.splits)
   829 done
   830 
   831 lemma take_Suc_conv_app_nth:
   832  "!!i. i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
   833 apply (induct xs, simp)
   834 apply (case_tac i, auto)
   835 done
   836 
   837 lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n"
   838 by (induct n) (auto, case_tac xs, auto)
   839 
   840 lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)"
   841 by (induct n) (auto, case_tac xs, auto)
   842 
   843 lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs"
   844 by (induct n) (auto, case_tac xs, auto)
   845 
   846 lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []"
   847 by (induct n) (auto, case_tac xs, auto)
   848 
   849 lemma take_append [simp]:
   850 "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
   851 by (induct n) (auto, case_tac xs, auto)
   852 
   853 lemma drop_append [simp]:
   854 "!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
   855 by (induct n) (auto, case_tac xs, auto)
   856 
   857 lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
   858 apply (induct m, auto)
   859 apply (case_tac xs, auto)
   860 apply (case_tac na, auto)
   861 done
   862 
   863 lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs"
   864 apply (induct m, auto)
   865 apply (case_tac xs, auto)
   866 done
   867 
   868 lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"
   869 apply (induct m, auto)
   870 apply (case_tac xs, auto)
   871 done
   872 
   873 lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs"
   874 apply (induct n, auto)
   875 apply (case_tac xs, auto)
   876 done
   877 
   878 lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"
   879 apply (induct n, auto)
   880 apply (case_tac xs, auto)
   881 done
   882 
   883 lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)"
   884 apply (induct n, auto)
   885 apply (case_tac xs, auto)
   886 done
   887 
   888 lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"
   889 apply (induct xs, auto)
   890 apply (case_tac i, auto)
   891 done
   892 
   893 lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"
   894 apply (induct xs, auto)
   895 apply (case_tac i, auto)
   896 done
   897 
   898 lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
   899 apply (induct xs, auto)
   900 apply (case_tac n, blast)
   901 apply (case_tac i, auto)
   902 done
   903 
   904 lemma nth_drop [simp]:
   905 "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
   906 apply (induct n, auto)
   907 apply (case_tac xs, auto)
   908 done
   909 
   910 lemma set_take_subset: "\<And>n. set(take n xs) \<subseteq> set xs"
   911 by(induct xs)(auto simp:take_Cons split:nat.split)
   912 
   913 lemma set_drop_subset: "\<And>n. set(drop n xs) \<subseteq> set xs"
   914 by(induct xs)(auto simp:drop_Cons split:nat.split)
   915 
   916 lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
   917 using set_take_subset by fast
   918 
   919 lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
   920 using set_drop_subset by fast
   921 
   922 lemma append_eq_conv_conj:
   923 "!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
   924 apply (induct xs, simp, clarsimp)
   925 apply (case_tac zs, auto)
   926 done
   927 
   928 lemma take_add [rule_format]: 
   929     "\<forall>i. i+j \<le> length(xs) --> take (i+j) xs = take i xs @ take j (drop i xs)"
   930 apply (induct xs, auto) 
   931 apply (case_tac i, simp_all) 
   932 done
   933 
   934 lemma append_eq_append_conv_if:
   935  "!! ys\<^isub>1. (xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
   936   (if size xs\<^isub>1 \<le> size ys\<^isub>1
   937    then xs\<^isub>1 = take (size xs\<^isub>1) ys\<^isub>1 \<and> xs\<^isub>2 = drop (size xs\<^isub>1) ys\<^isub>1 @ ys\<^isub>2
   938    else take (size ys\<^isub>1) xs\<^isub>1 = ys\<^isub>1 \<and> drop (size ys\<^isub>1) xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>2)"
   939 apply(induct xs\<^isub>1)
   940  apply simp
   941 apply(case_tac ys\<^isub>1)
   942 apply simp_all
   943 done
   944 
   945 
   946 subsection {* @{text takeWhile} and @{text dropWhile} *}
   947 
   948 lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
   949 by (induct xs) auto
   950 
   951 lemma takeWhile_append1 [simp]:
   952 "[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
   953 by (induct xs) auto
   954 
   955 lemma takeWhile_append2 [simp]:
   956 "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
   957 by (induct xs) auto
   958 
   959 lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
   960 by (induct xs) auto
   961 
   962 lemma dropWhile_append1 [simp]:
   963 "[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
   964 by (induct xs) auto
   965 
   966 lemma dropWhile_append2 [simp]:
   967 "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
   968 by (induct xs) auto
   969 
   970 lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
   971 by (induct xs) (auto split: split_if_asm)
   972 
   973 lemma takeWhile_eq_all_conv[simp]:
   974  "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
   975 by(induct xs, auto)
   976 
   977 lemma dropWhile_eq_Nil_conv[simp]:
   978  "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
   979 by(induct xs, auto)
   980 
   981 lemma dropWhile_eq_Cons_conv:
   982  "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
   983 by(induct xs, auto)
   984 
   985 
   986 subsection {* @{text zip} *}
   987 
   988 lemma zip_Nil [simp]: "zip [] ys = []"
   989 by (induct ys) auto
   990 
   991 lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
   992 by simp
   993 
   994 declare zip_Cons [simp del]
   995 
   996 lemma length_zip [simp]:
   997 "!!xs. length (zip xs ys) = min (length xs) (length ys)"
   998 apply (induct ys, simp)
   999 apply (case_tac xs, auto)
  1000 done
  1001 
  1002 lemma zip_append1:
  1003 "!!xs. zip (xs @ ys) zs =
  1004 zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
  1005 apply (induct zs, simp)
  1006 apply (case_tac xs, simp_all)
  1007 done
  1008 
  1009 lemma zip_append2:
  1010 "!!ys. zip xs (ys @ zs) =
  1011 zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
  1012 apply (induct xs, simp)
  1013 apply (case_tac ys, simp_all)
  1014 done
  1015 
  1016 lemma zip_append [simp]:
  1017  "[| length xs = length us; length ys = length vs |] ==>
  1018 zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
  1019 by (simp add: zip_append1)
  1020 
  1021 lemma zip_rev:
  1022 "length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
  1023 by (induct rule:list_induct2, simp_all)
  1024 
  1025 lemma nth_zip [simp]:
  1026 "!!i xs. [| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
  1027 apply (induct ys, simp)
  1028 apply (case_tac xs)
  1029  apply (simp_all add: nth.simps split: nat.split)
  1030 done
  1031 
  1032 lemma set_zip:
  1033 "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
  1034 by (simp add: set_conv_nth cong: rev_conj_cong)
  1035 
  1036 lemma zip_update:
  1037 "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
  1038 by (rule sym, simp add: update_zip)
  1039 
  1040 lemma zip_replicate [simp]:
  1041 "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
  1042 apply (induct i, auto)
  1043 apply (case_tac j, auto)
  1044 done
  1045 
  1046 
  1047 subsection {* @{text list_all2} *}
  1048 
  1049 lemma list_all2_lengthD [intro?]: 
  1050   "list_all2 P xs ys ==> length xs = length ys"
  1051 by (simp add: list_all2_def)
  1052 
  1053 lemma list_all2_Nil [iff]: "list_all2 P [] ys = (ys = [])"
  1054 by (simp add: list_all2_def)
  1055 
  1056 lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])"
  1057 by (simp add: list_all2_def)
  1058 
  1059 lemma list_all2_Cons [iff]:
  1060 "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
  1061 by (auto simp add: list_all2_def)
  1062 
  1063 lemma list_all2_Cons1:
  1064 "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
  1065 by (cases ys) auto
  1066 
  1067 lemma list_all2_Cons2:
  1068 "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
  1069 by (cases xs) auto
  1070 
  1071 lemma list_all2_rev [iff]:
  1072 "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
  1073 by (simp add: list_all2_def zip_rev cong: conj_cong)
  1074 
  1075 lemma list_all2_rev1:
  1076 "list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
  1077 by (subst list_all2_rev [symmetric]) simp
  1078 
  1079 lemma list_all2_append1:
  1080 "list_all2 P (xs @ ys) zs =
  1081 (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
  1082 list_all2 P xs us \<and> list_all2 P ys vs)"
  1083 apply (simp add: list_all2_def zip_append1)
  1084 apply (rule iffI)
  1085  apply (rule_tac x = "take (length xs) zs" in exI)
  1086  apply (rule_tac x = "drop (length xs) zs" in exI)
  1087  apply (force split: nat_diff_split simp add: min_def, clarify)
  1088 apply (simp add: ball_Un)
  1089 done
  1090 
  1091 lemma list_all2_append2:
  1092 "list_all2 P xs (ys @ zs) =
  1093 (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
  1094 list_all2 P us ys \<and> list_all2 P vs zs)"
  1095 apply (simp add: list_all2_def zip_append2)
  1096 apply (rule iffI)
  1097  apply (rule_tac x = "take (length ys) xs" in exI)
  1098  apply (rule_tac x = "drop (length ys) xs" in exI)
  1099  apply (force split: nat_diff_split simp add: min_def, clarify)
  1100 apply (simp add: ball_Un)
  1101 done
  1102 
  1103 lemma list_all2_append:
  1104   "length xs = length ys \<Longrightarrow>
  1105   list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
  1106 by (induct rule:list_induct2, simp_all)
  1107 
  1108 lemma list_all2_appendI [intro?, trans]:
  1109   "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
  1110   by (simp add: list_all2_append list_all2_lengthD)
  1111 
  1112 lemma list_all2_conv_all_nth:
  1113 "list_all2 P xs ys =
  1114 (length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
  1115 by (force simp add: list_all2_def set_zip)
  1116 
  1117 lemma list_all2_trans:
  1118   assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
  1119   shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
  1120         (is "!!bs cs. PROP ?Q as bs cs")
  1121 proof (induct as)
  1122   fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
  1123   show "!!cs. PROP ?Q (x # xs) bs cs"
  1124   proof (induct bs)
  1125     fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
  1126     show "PROP ?Q (x # xs) (y # ys) cs"
  1127       by (induct cs) (auto intro: tr I1 I2)
  1128   qed simp
  1129 qed simp
  1130 
  1131 lemma list_all2_all_nthI [intro?]:
  1132   "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
  1133   by (simp add: list_all2_conv_all_nth)
  1134 
  1135 lemma list_all2I:
  1136   "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
  1137   by (simp add: list_all2_def)
  1138 
  1139 lemma list_all2_nthD:
  1140   "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
  1141   by (simp add: list_all2_conv_all_nth)
  1142 
  1143 lemma list_all2_nthD2:
  1144   "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
  1145   by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
  1146 
  1147 lemma list_all2_map1: 
  1148   "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
  1149   by (simp add: list_all2_conv_all_nth)
  1150 
  1151 lemma list_all2_map2: 
  1152   "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
  1153   by (auto simp add: list_all2_conv_all_nth)
  1154 
  1155 lemma list_all2_refl [intro?]:
  1156   "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
  1157   by (simp add: list_all2_conv_all_nth)
  1158 
  1159 lemma list_all2_update_cong:
  1160   "\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
  1161   by (simp add: list_all2_conv_all_nth nth_list_update)
  1162 
  1163 lemma list_all2_update_cong2:
  1164   "\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
  1165   by (simp add: list_all2_lengthD list_all2_update_cong)
  1166 
  1167 lemma list_all2_takeI [simp,intro?]:
  1168   "\<And>n ys. list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
  1169   apply (induct xs)
  1170    apply simp
  1171   apply (clarsimp simp add: list_all2_Cons1)
  1172   apply (case_tac n)
  1173   apply auto
  1174   done
  1175 
  1176 lemma list_all2_dropI [simp,intro?]:
  1177   "\<And>n bs. list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"
  1178   apply (induct as, simp)
  1179   apply (clarsimp simp add: list_all2_Cons1)
  1180   apply (case_tac n, simp, simp)
  1181   done
  1182 
  1183 lemma list_all2_mono [intro?]:
  1184   "\<And>y. list_all2 P x y \<Longrightarrow> (\<And>x y. P x y \<Longrightarrow> Q x y) \<Longrightarrow> list_all2 Q x y"
  1185   apply (induct x, simp)
  1186   apply (case_tac y, auto)
  1187   done
  1188 
  1189 
  1190 subsection {* @{text foldl} *}
  1191 
  1192 lemma foldl_append [simp]:
  1193 "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
  1194 by (induct xs) auto
  1195 
  1196 text {*
  1197 Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
  1198 difficult to use because it requires an additional transitivity step.
  1199 *}
  1200 
  1201 lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns"
  1202 by (induct ns) auto
  1203 
  1204 lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns"
  1205 by (force intro: start_le_sum simp add: in_set_conv_decomp)
  1206 
  1207 lemma sum_eq_0_conv [iff]:
  1208 "!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
  1209 by (induct ns) auto
  1210 
  1211 
  1212 subsection {* folding a relation over a list *}
  1213 
  1214 (*"fold_rel R cs \<equiv> foldl (%r c. r O {(x,y). (c,x,y):R}) Id cs"*)
  1215 inductive "fold_rel R" intros
  1216   Nil:  "(a, [],a) : fold_rel R"
  1217   Cons: "[|(a,x,b) : R; (b,xs,c) : fold_rel R|] ==> (a,x#xs,c) : fold_rel R"
  1218 inductive_cases fold_rel_elim_case [elim!]:
  1219    "(a, [] , b) : fold_rel R"
  1220    "(a, x#xs, b) : fold_rel R"
  1221 
  1222 lemma fold_rel_Nil [intro!]: "a = b ==> (a, [], b) : fold_rel R" 
  1223 by (simp add: fold_rel.Nil)
  1224 declare fold_rel.Cons [intro!]
  1225 
  1226 
  1227 subsection {* @{text upto} *}
  1228 
  1229 lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])"
  1230 -- {* Does not terminate! *}
  1231 by (induct j) auto
  1232 
  1233 lemma upt_conv_Nil [simp]: "j <= i ==> [i..j(] = []"
  1234 by (subst upt_rec) simp
  1235 
  1236 lemma upt_Suc_append: "i <= j ==> [i..(Suc j)(] = [i..j(]@[j]"
  1237 -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
  1238 by simp
  1239 
  1240 lemma upt_conv_Cons: "i < j ==> [i..j(] = i # [Suc i..j(]"
  1241 apply(rule trans)
  1242 apply(subst upt_rec)
  1243  prefer 2 apply (rule refl, simp)
  1244 done
  1245 
  1246 lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"
  1247 -- {* LOOPS as a simprule, since @{text "j <= j"}. *}
  1248 by (induct k) auto
  1249 
  1250 lemma length_upt [simp]: "length [i..j(] = j - i"
  1251 by (induct j) (auto simp add: Suc_diff_le)
  1252 
  1253 lemma nth_upt [simp]: "i + k < j ==> [i..j(] ! k = i + k"
  1254 apply (induct j)
  1255 apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
  1256 done
  1257 
  1258 lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]"
  1259 apply (induct m, simp)
  1260 apply (subst upt_rec)
  1261 apply (rule sym)
  1262 apply (subst upt_rec)
  1263 apply (simp del: upt.simps)
  1264 done
  1265 
  1266 lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]"
  1267 by (induct n) auto
  1268 
  1269 lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)"
  1270 apply (induct n m rule: diff_induct)
  1271 prefer 3 apply (subst map_Suc_upt[symmetric])
  1272 apply (auto simp add: less_diff_conv nth_upt)
  1273 done
  1274 
  1275 lemma nth_take_lemma:
  1276   "!!xs ys. k <= length xs ==> k <= length ys ==>
  1277      (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
  1278 apply (atomize, induct k)
  1279 apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
  1280 txt {* Both lists must be non-empty *}
  1281 apply (case_tac xs, simp)
  1282 apply (case_tac ys, clarify)
  1283  apply (simp (no_asm_use))
  1284 apply clarify
  1285 txt {* prenexing's needed, not miniscoping *}
  1286 apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
  1287 apply blast
  1288 done
  1289 
  1290 lemma nth_equalityI:
  1291  "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
  1292 apply (frule nth_take_lemma [OF le_refl eq_imp_le])
  1293 apply (simp_all add: take_all)
  1294 done
  1295 
  1296 (* needs nth_equalityI *)
  1297 lemma list_all2_antisym:
  1298   "\<lbrakk> (\<And>x y. \<lbrakk>P x y; Q y x\<rbrakk> \<Longrightarrow> x = y); list_all2 P xs ys; list_all2 Q ys xs \<rbrakk> 
  1299   \<Longrightarrow> xs = ys"
  1300   apply (simp add: list_all2_conv_all_nth) 
  1301   apply (rule nth_equalityI, blast, simp)
  1302   done
  1303 
  1304 lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
  1305 -- {* The famous take-lemma. *}
  1306 apply (drule_tac x = "max (length xs) (length ys)" in spec)
  1307 apply (simp add: le_max_iff_disj take_all)
  1308 done
  1309 
  1310 
  1311 subsection {* @{text "distinct"} and @{text remdups} *}
  1312 
  1313 lemma distinct_append [simp]:
  1314 "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
  1315 by (induct xs) auto
  1316 
  1317 lemma set_remdups [simp]: "set (remdups xs) = set xs"
  1318 by (induct xs) (auto simp add: insert_absorb)
  1319 
  1320 lemma distinct_remdups [iff]: "distinct (remdups xs)"
  1321 by (induct xs) auto
  1322 
  1323 lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
  1324 by (induct xs) auto
  1325 
  1326 text {*
  1327 It is best to avoid this indexed version of distinct, but sometimes
  1328 it is useful. *}
  1329 lemma distinct_conv_nth:
  1330 "distinct xs = (\<forall>i j. i < size xs \<and> j < size xs \<and> i \<noteq> j --> xs!i \<noteq> xs!j)"
  1331 apply (induct_tac xs, simp, simp)
  1332 apply (rule iffI, clarsimp)
  1333  apply (case_tac i)
  1334 apply (case_tac j, simp)
  1335 apply (simp add: set_conv_nth)
  1336  apply (case_tac j)
  1337 apply (clarsimp simp add: set_conv_nth, simp)
  1338 apply (rule conjI)
  1339  apply (clarsimp simp add: set_conv_nth)
  1340  apply (erule_tac x = 0 in allE)
  1341  apply (erule_tac x = "Suc i" in allE, simp, clarsimp)
  1342 apply (erule_tac x = "Suc i" in allE)
  1343 apply (erule_tac x = "Suc j" in allE, simp)
  1344 done
  1345 
  1346 lemma distinct_card: "distinct xs \<Longrightarrow> card (set xs) = size xs"
  1347   by (induct xs) auto
  1348 
  1349 lemma card_distinct: "card (set xs) = size xs \<Longrightarrow> distinct xs"
  1350 proof (induct xs)
  1351   case Nil thus ?case by simp
  1352 next
  1353   case (Cons x xs)
  1354   show ?case
  1355   proof (cases "x \<in> set xs")
  1356     case False with Cons show ?thesis by simp
  1357   next
  1358     case True with Cons.prems
  1359     have "card (set xs) = Suc (length xs)" 
  1360       by (simp add: card_insert_if split: split_if_asm)
  1361     moreover have "card (set xs) \<le> length xs" by (rule card_length)
  1362     ultimately have False by simp
  1363     thus ?thesis ..
  1364   qed
  1365 qed
  1366 
  1367 
  1368 subsection {* @{text replicate} *}
  1369 
  1370 lemma length_replicate [simp]: "length (replicate n x) = n"
  1371 by (induct n) auto
  1372 
  1373 lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
  1374 by (induct n) auto
  1375 
  1376 lemma replicate_app_Cons_same:
  1377 "(replicate n x) @ (x # xs) = x # replicate n x @ xs"
  1378 by (induct n) auto
  1379 
  1380 lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
  1381 apply (induct n, simp)
  1382 apply (simp add: replicate_app_Cons_same)
  1383 done
  1384 
  1385 lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
  1386 by (induct n) auto
  1387 
  1388 lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
  1389 by (induct n) auto
  1390 
  1391 lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
  1392 by (induct n) auto
  1393 
  1394 lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
  1395 by (atomize (full), induct n) auto
  1396 
  1397 lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x"
  1398 apply (induct n, simp)
  1399 apply (simp add: nth_Cons split: nat.split)
  1400 done
  1401 
  1402 lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
  1403 by (induct n) auto
  1404 
  1405 lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
  1406 by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
  1407 
  1408 lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
  1409 by auto
  1410 
  1411 lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
  1412 by (simp add: set_replicate_conv_if split: split_if_asm)
  1413 
  1414 
  1415 subsection {* @{text postfix} *}
  1416 
  1417 lemma postfix_refl [simp, intro!]: "xs \<sqsupseteq> xs" by (auto simp add: postfix_def)
  1418 lemma postfix_trans: "\<lbrakk>xs \<sqsupseteq> ys; ys \<sqsupseteq> zs\<rbrakk> \<Longrightarrow> xs \<sqsupseteq> zs" 
  1419          by (auto simp add: postfix_def)
  1420 lemma postfix_antisym: "\<lbrakk>xs \<sqsupseteq> ys; ys \<sqsupseteq> xs\<rbrakk> \<Longrightarrow> xs = ys" 
  1421          by (auto simp add: postfix_def)
  1422 
  1423 lemma postfix_emptyI [simp, intro!]: "xs \<sqsupseteq> []" by (auto simp add: postfix_def)
  1424 lemma postfix_emptyD [dest!]: "[] \<sqsupseteq> xs \<Longrightarrow> xs = []"by(auto simp add:postfix_def)
  1425 lemma postfix_ConsI: "xs \<sqsupseteq> ys \<Longrightarrow> x#xs \<sqsupseteq> ys" by (auto simp add: postfix_def)
  1426 lemma postfix_ConsD: "xs \<sqsupseteq> y#ys \<Longrightarrow> xs \<sqsupseteq> ys" by (auto simp add: postfix_def)
  1427 lemma postfix_appendI: "xs \<sqsupseteq> ys \<Longrightarrow> zs@xs \<sqsupseteq> ys" by (auto simp add: postfix_def)
  1428 lemma postfix_appendD: "xs \<sqsupseteq> zs@ys \<Longrightarrow> xs \<sqsupseteq> ys" by (auto simp add: postfix_def)
  1429 
  1430 lemma postfix_is_subset_lemma: "xs = zs @ ys \<Longrightarrow> set ys \<subseteq> set xs"
  1431 by (induct zs, auto)
  1432 lemma postfix_is_subset: "xs \<sqsupseteq> ys \<Longrightarrow> set ys \<subseteq> set xs"
  1433 by (unfold postfix_def, erule exE, erule postfix_is_subset_lemma)
  1434 
  1435 lemma postfix_ConsD2_lemma [rule_format]: "x#xs = zs @ y#ys \<longrightarrow> xs \<sqsupseteq> ys"
  1436 by (induct zs, auto intro!: postfix_appendI postfix_ConsI)
  1437 lemma postfix_ConsD2: "x#xs \<sqsupseteq> y#ys \<Longrightarrow> xs \<sqsupseteq> ys"
  1438 by (auto simp add: postfix_def dest!: postfix_ConsD2_lemma)
  1439 
  1440 subsection {* Lexicographic orderings on lists *}
  1441 
  1442 lemma wf_lexn: "wf r ==> wf (lexn r n)"
  1443 apply (induct_tac n, simp, simp)
  1444 apply(rule wf_subset)
  1445  prefer 2 apply (rule Int_lower1)
  1446 apply(rule wf_prod_fun_image)
  1447  prefer 2 apply (rule inj_onI, auto)
  1448 done
  1449 
  1450 lemma lexn_length:
  1451 "!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
  1452 by (induct n) auto
  1453 
  1454 lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
  1455 apply (unfold lex_def)
  1456 apply (rule wf_UN)
  1457 apply (blast intro: wf_lexn, clarify)
  1458 apply (rename_tac m n)
  1459 apply (subgoal_tac "m \<noteq> n")
  1460  prefer 2 apply blast
  1461 apply (blast dest: lexn_length not_sym)
  1462 done
  1463 
  1464 lemma lexn_conv:
  1465 "lexn r n =
  1466 {(xs,ys). length xs = n \<and> length ys = n \<and>
  1467 (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
  1468 apply (induct_tac n, simp, blast)
  1469 apply (simp add: image_Collect lex_prod_def, safe, blast)
  1470  apply (rule_tac x = "ab # xys" in exI, simp)
  1471 apply (case_tac xys, simp_all, blast)
  1472 done
  1473 
  1474 lemma lex_conv:
  1475 "lex r =
  1476 {(xs,ys). length xs = length ys \<and>
  1477 (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
  1478 by (force simp add: lex_def lexn_conv)
  1479 
  1480 lemma wf_lexico [intro!]: "wf r ==> wf (lexico r)"
  1481 by (unfold lexico_def) blast
  1482 
  1483 lemma lexico_conv:
  1484 "lexico r = {(xs,ys). length xs < length ys |
  1485 length xs = length ys \<and> (xs, ys) : lex r}"
  1486 by (simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def)
  1487 
  1488 lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
  1489 by (simp add: lex_conv)
  1490 
  1491 lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
  1492 by (simp add:lex_conv)
  1493 
  1494 lemma Cons_in_lex [iff]:
  1495 "((x # xs, y # ys) : lex r) =
  1496 ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
  1497 apply (simp add: lex_conv)
  1498 apply (rule iffI)
  1499  prefer 2 apply (blast intro: Cons_eq_appendI, clarify)
  1500 apply (case_tac xys, simp, simp)
  1501 apply blast
  1502 done
  1503 
  1504 
  1505 subsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
  1506 
  1507 lemma sublist_empty [simp]: "sublist xs {} = []"
  1508 by (auto simp add: sublist_def)
  1509 
  1510 lemma sublist_nil [simp]: "sublist [] A = []"
  1511 by (auto simp add: sublist_def)
  1512 
  1513 lemma sublist_shift_lemma:
  1514 "map fst [p:zip xs [i..i + length xs(] . snd p : A] =
  1515 map fst [p:zip xs [0..length xs(] . snd p + i : A]"
  1516 by (induct xs rule: rev_induct) (simp_all add: add_commute)
  1517 
  1518 lemma sublist_append:
  1519 "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
  1520 apply (unfold sublist_def)
  1521 apply (induct l' rule: rev_induct, simp)
  1522 apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
  1523 apply (simp add: add_commute)
  1524 done
  1525 
  1526 lemma sublist_Cons:
  1527 "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
  1528 apply (induct l rule: rev_induct)
  1529  apply (simp add: sublist_def)
  1530 apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
  1531 done
  1532 
  1533 lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
  1534 by (simp add: sublist_Cons)
  1535 
  1536 lemma sublist_upt_eq_take [simp]: "sublist l {..n(} = take n l"
  1537 apply (induct l rule: rev_induct, simp)
  1538 apply (simp split: nat_diff_split add: sublist_append)
  1539 done
  1540 
  1541 
  1542 lemma take_Cons':
  1543 "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
  1544 by (cases n) simp_all
  1545 
  1546 lemma drop_Cons':
  1547 "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
  1548 by (cases n) simp_all
  1549 
  1550 lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
  1551 by (cases n) simp_all
  1552 
  1553 lemmas [simp] = take_Cons'[of "number_of v",standard]
  1554                 drop_Cons'[of "number_of v",standard]
  1555                 nth_Cons'[of _ _ "number_of v",standard]
  1556 
  1557 
  1558 lemma distinct_card: "distinct xs \<Longrightarrow> card (set xs) = size xs"
  1559   by (induct xs) auto
  1560 
  1561 lemma card_length: "card (set xs) \<le> length xs"
  1562   by (induct xs) (auto simp add: card_insert_if)
  1563 
  1564 lemma "card (set xs) = size xs \<Longrightarrow> distinct xs"
  1565 proof (induct xs)
  1566   case Nil thus ?case by simp
  1567 next
  1568   case (Cons x xs)
  1569   show ?case
  1570   proof (cases "x \<in> set xs")
  1571     case False with Cons show ?thesis by simp
  1572   next
  1573     case True with Cons.prems
  1574     have "card (set xs) = Suc (length xs)" 
  1575       by (simp add: card_insert_if split: split_if_asm)
  1576     moreover have "card (set xs) \<le> length xs" by (rule card_length)
  1577     ultimately have False by simp
  1578     thus ?thesis ..
  1579   qed
  1580 qed
  1581 
  1582 subsection {* Characters and strings *}
  1583 
  1584 datatype nibble =
  1585     Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7
  1586   | Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF
  1587 
  1588 datatype char = Char nibble nibble
  1589   -- "Note: canonical order of character encoding coincides with standard term ordering"
  1590 
  1591 types string = "char list"
  1592 
  1593 syntax
  1594   "_Char" :: "xstr => char"    ("CHR _")
  1595   "_String" :: "xstr => string"    ("_")
  1596 
  1597 parse_ast_translation {*
  1598   let
  1599     val constants = Syntax.Appl o map Syntax.Constant;
  1600 
  1601     fun mk_nib n = "Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10));
  1602     fun mk_char c =
  1603       if Symbol.is_ascii c andalso Symbol.is_printable c then
  1604         constants ["Char", mk_nib (ord c div 16), mk_nib (ord c mod 16)]
  1605       else error ("Printable ASCII character expected: " ^ quote c);
  1606 
  1607     fun mk_string [] = Syntax.Constant "Nil"
  1608       | mk_string (c :: cs) = Syntax.Appl [Syntax.Constant "Cons", mk_char c, mk_string cs];
  1609 
  1610     fun char_ast_tr [Syntax.Variable xstr] =
  1611         (case Syntax.explode_xstr xstr of
  1612           [c] => mk_char c
  1613         | _ => error ("Single character expected: " ^ xstr))
  1614       | char_ast_tr asts = raise AST ("char_ast_tr", asts);
  1615 
  1616     fun string_ast_tr [Syntax.Variable xstr] =
  1617         (case Syntax.explode_xstr xstr of
  1618           [] => constants [Syntax.constrainC, "Nil", "string"]
  1619         | cs => mk_string cs)
  1620       | string_ast_tr asts = raise AST ("string_tr", asts);
  1621   in [("_Char", char_ast_tr), ("_String", string_ast_tr)] end;
  1622 *}
  1623 
  1624 print_ast_translation {*
  1625   let
  1626     fun dest_nib (Syntax.Constant c) =
  1627         (case explode c of
  1628           ["N", "i", "b", "b", "l", "e", h] =>
  1629             if "0" <= h andalso h <= "9" then ord h - ord "0"
  1630             else if "A" <= h andalso h <= "F" then ord h - ord "A" + 10
  1631             else raise Match
  1632         | _ => raise Match)
  1633       | dest_nib _ = raise Match;
  1634 
  1635     fun dest_chr c1 c2 =
  1636       let val c = chr (dest_nib c1 * 16 + dest_nib c2)
  1637       in if Symbol.is_printable c then c else raise Match end;
  1638 
  1639     fun dest_char (Syntax.Appl [Syntax.Constant "Char", c1, c2]) = dest_chr c1 c2
  1640       | dest_char _ = raise Match;
  1641 
  1642     fun xstr cs = Syntax.Appl [Syntax.Constant "_xstr", Syntax.Variable (Syntax.implode_xstr cs)];
  1643 
  1644     fun char_ast_tr' [c1, c2] = Syntax.Appl [Syntax.Constant "_Char", xstr [dest_chr c1 c2]]
  1645       | char_ast_tr' _ = raise Match;
  1646 
  1647     fun list_ast_tr' [args] = Syntax.Appl [Syntax.Constant "_String",
  1648             xstr (map dest_char (Syntax.unfold_ast "_args" args))]
  1649       | list_ast_tr' ts = raise Match;
  1650   in [("Char", char_ast_tr'), ("@list", list_ast_tr')] end;
  1651 *}
  1652 
  1653 end