src/HOL/List.thy
author paulson
Fri Sep 26 10:34:57 2003 +0200 (2003-09-26)
changeset 14208 144f45277d5a
parent 14187 26dfcd0ac436
child 14247 cb32eb89bddd
permissions -rw-r--r--
misc tidying
     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 
   295 subsection {* @{text "@"} -- append *}
   296 
   297 lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
   298 by (induct xs) auto
   299 
   300 lemma append_Nil2 [simp]: "xs @ [] = xs"
   301 by (induct xs) auto
   302 
   303 lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
   304 by (induct xs) auto
   305 
   306 lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
   307 by (induct xs) auto
   308 
   309 lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
   310 by (induct xs) auto
   311 
   312 lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
   313 by (induct xs) auto
   314 
   315 lemma append_eq_append_conv [simp]:
   316  "!!ys. length xs = length ys \<or> length us = length vs
   317  ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
   318 apply (induct xs)
   319  apply (case_tac ys, simp, force)
   320 apply (case_tac ys, force, simp)
   321 done
   322 
   323 lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
   324 by simp
   325 
   326 lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
   327 by simp
   328 
   329 lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
   330 by simp
   331 
   332 lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
   333 using append_same_eq [of _ _ "[]"] by auto
   334 
   335 lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
   336 using append_same_eq [of "[]"] by auto
   337 
   338 lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
   339 by (induct xs) auto
   340 
   341 lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
   342 by (induct xs) auto
   343 
   344 lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
   345 by (simp add: hd_append split: list.split)
   346 
   347 lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
   348 by (simp split: list.split)
   349 
   350 lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
   351 by (simp add: tl_append split: list.split)
   352 
   353 
   354 text {* Trivial rules for solving @{text "@"}-equations automatically. *}
   355 
   356 lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
   357 by simp
   358 
   359 lemma Cons_eq_appendI:
   360 "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
   361 by (drule sym) simp
   362 
   363 lemma append_eq_appendI:
   364 "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
   365 by (drule sym) simp
   366 
   367 
   368 text {*
   369 Simplification procedure for all list equalities.
   370 Currently only tries to rearrange @{text "@"} to see if
   371 - both lists end in a singleton list,
   372 - or both lists end in the same list.
   373 *}
   374 
   375 ML_setup {*
   376 local
   377 
   378 val append_assoc = thm "append_assoc";
   379 val append_Nil = thm "append_Nil";
   380 val append_Cons = thm "append_Cons";
   381 val append1_eq_conv = thm "append1_eq_conv";
   382 val append_same_eq = thm "append_same_eq";
   383 
   384 fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
   385   (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
   386   | last (Const("List.op @",_) $ _ $ ys) = last ys
   387   | last t = t;
   388 
   389 fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
   390   | list1 _ = false;
   391 
   392 fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
   393   (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
   394   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
   395   | butlast xs = Const("List.list.Nil",fastype_of xs);
   396 
   397 val rearr_tac =
   398   simp_tac (HOL_basic_ss addsimps [append_assoc, append_Nil, append_Cons]);
   399 
   400 fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
   401   let
   402     val lastl = last lhs and lastr = last rhs;
   403     fun rearr conv =
   404       let
   405         val lhs1 = butlast lhs and rhs1 = butlast rhs;
   406         val Type(_,listT::_) = eqT
   407         val appT = [listT,listT] ---> listT
   408         val app = Const("List.op @",appT)
   409         val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
   410         val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
   411         val thm = Tactic.prove sg [] [] eq (K (rearr_tac 1));
   412       in Some ((conv RS (thm RS trans)) RS eq_reflection) end;
   413 
   414   in
   415     if list1 lastl andalso list1 lastr then rearr append1_eq_conv
   416     else if lastl aconv lastr then rearr append_same_eq
   417     else None
   418   end;
   419 
   420 in
   421 
   422 val list_eq_simproc =
   423   Simplifier.simproc (Theory.sign_of (the_context ())) "list_eq" ["(xs::'a list) = ys"] list_eq;
   424 
   425 end;
   426 
   427 Addsimprocs [list_eq_simproc];
   428 *}
   429 
   430 
   431 subsection {* @{text map} *}
   432 
   433 lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
   434 by (induct xs) simp_all
   435 
   436 lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
   437 by (rule ext, induct_tac xs) auto
   438 
   439 lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
   440 by (induct xs) auto
   441 
   442 lemma map_compose: "map (f o g) xs = map f (map g xs)"
   443 by (induct xs) (auto simp add: o_def)
   444 
   445 lemma rev_map: "rev (map f xs) = map f (rev xs)"
   446 by (induct xs) auto
   447 
   448 lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
   449 by (induct xs) auto
   450 
   451 lemma map_cong [recdef_cong]:
   452 "xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
   453 -- {* a congruence rule for @{text map} *}
   454 by simp
   455 
   456 lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
   457 by (cases xs) auto
   458 
   459 lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
   460 by (cases xs) auto
   461 
   462 lemma map_eq_Cons_conv[iff]:
   463  "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
   464 by (cases xs) auto
   465 
   466 lemma Cons_eq_map_conv[iff]:
   467  "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
   468 by (cases ys) auto
   469 
   470 lemma ex_map_conv:
   471   "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
   472 by(induct ys, auto)
   473 
   474 lemma map_injective:
   475  "!!xs. map f xs = map f ys ==> (\<forall>x y. f x = f y --> x = y) ==> xs = ys"
   476 by (induct ys) auto
   477 
   478 lemma inj_mapI: "inj f ==> inj (map f)"
   479 by (rules dest: map_injective injD intro: inj_onI)
   480 
   481 lemma inj_mapD: "inj (map f) ==> inj f"
   482 apply (unfold inj_on_def, clarify)
   483 apply (erule_tac x = "[x]" in ballE)
   484  apply (erule_tac x = "[y]" in ballE, simp, blast)
   485 apply blast
   486 done
   487 
   488 lemma inj_map: "inj (map f) = inj f"
   489 by (blast dest: inj_mapD intro: inj_mapI)
   490 
   491 
   492 subsection {* @{text rev} *}
   493 
   494 lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
   495 by (induct xs) auto
   496 
   497 lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
   498 by (induct xs) auto
   499 
   500 lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
   501 by (induct xs) auto
   502 
   503 lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
   504 by (induct xs) auto
   505 
   506 lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
   507 apply (induct xs, force)
   508 apply (case_tac ys, simp, force)
   509 done
   510 
   511 lemma rev_induct [case_names Nil snoc]:
   512   "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
   513 apply(subst rev_rev_ident[symmetric])
   514 apply(rule_tac list = "rev xs" in list.induct, simp_all)
   515 done
   516 
   517 ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility"
   518 
   519 lemma rev_exhaust [case_names Nil snoc]:
   520   "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
   521 by (induct xs rule: rev_induct) auto
   522 
   523 lemmas rev_cases = rev_exhaust
   524 
   525 
   526 subsection {* @{text set} *}
   527 
   528 lemma finite_set [iff]: "finite (set xs)"
   529 by (induct xs) auto
   530 
   531 lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
   532 by (induct xs) auto
   533 
   534 lemma hd_in_set: "l = x#xs \<Longrightarrow> x\<in>set l"
   535 by (case_tac l, auto)
   536 
   537 lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
   538 by auto
   539 
   540 lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
   541 by auto
   542 
   543 lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
   544 by (induct xs) auto
   545 
   546 lemma set_rev [simp]: "set (rev xs) = set xs"
   547 by (induct xs) auto
   548 
   549 lemma set_map [simp]: "set (map f xs) = f`(set xs)"
   550 by (induct xs) auto
   551 
   552 lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
   553 by (induct xs) auto
   554 
   555 lemma set_upt [simp]: "set[i..j(] = {k. i \<le> k \<and> k < j}"
   556 apply (induct j, simp_all)
   557 apply (erule ssubst, auto)
   558 done
   559 
   560 lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
   561 apply (induct xs, simp, simp)
   562 apply (rule iffI)
   563  apply (blast intro: eq_Nil_appendI Cons_eq_appendI)
   564 apply (erule exE)+
   565 apply (case_tac ys, auto)
   566 done
   567 
   568 lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)"
   569 -- {* eliminate @{text lists} in favour of @{text set} *}
   570 by (induct xs) auto
   571 
   572 lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A"
   573 by (rule in_lists_conv_set [THEN iffD1])
   574 
   575 lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A"
   576 by (rule in_lists_conv_set [THEN iffD2])
   577 
   578 lemma finite_list: "finite A ==> EX l. set l = A"
   579 apply (erule finite_induct, auto)
   580 apply (rule_tac x="x#l" in exI, auto)
   581 done
   582 
   583 
   584 subsection {* @{text mem} *}
   585 
   586 lemma set_mem_eq: "(x mem xs) = (x : set xs)"
   587 by (induct xs) auto
   588 
   589 
   590 subsection {* @{text list_all} *}
   591 
   592 lemma list_all_conv: "list_all P xs = (\<forall>x \<in> set xs. P x)"
   593 by (induct xs) auto
   594 
   595 lemma list_all_append [simp]:
   596 "list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)"
   597 by (induct xs) auto
   598 
   599 
   600 subsection {* @{text filter} *}
   601 
   602 lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
   603 by (induct xs) auto
   604 
   605 lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
   606 by (induct xs) auto
   607 
   608 lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
   609 by (induct xs) auto
   610 
   611 lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
   612 by (induct xs) auto
   613 
   614 lemma length_filter [simp]: "length (filter P xs) \<le> length xs"
   615 by (induct xs) (auto simp add: le_SucI)
   616 
   617 lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
   618 by auto
   619 
   620 
   621 subsection {* @{text concat} *}
   622 
   623 lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
   624 by (induct xs) auto
   625 
   626 lemma concat_eq_Nil_conv [iff]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
   627 by (induct xss) auto
   628 
   629 lemma Nil_eq_concat_conv [iff]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
   630 by (induct xss) auto
   631 
   632 lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)"
   633 by (induct xs) auto
   634 
   635 lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
   636 by (induct xs) auto
   637 
   638 lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
   639 by (induct xs) auto
   640 
   641 lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
   642 by (induct xs) auto
   643 
   644 
   645 subsection {* @{text nth} *}
   646 
   647 lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
   648 by auto
   649 
   650 lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
   651 by auto
   652 
   653 declare nth.simps [simp del]
   654 
   655 lemma nth_append:
   656 "!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
   657 apply (induct "xs", simp)
   658 apply (case_tac n, auto)
   659 done
   660 
   661 lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)"
   662 apply (induct xs, simp)
   663 apply (case_tac n, auto)
   664 done
   665 
   666 lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
   667 apply (induct_tac xs, simp, simp)
   668 apply safe
   669 apply (rule_tac x = 0 in exI, simp)
   670  apply (rule_tac x = "Suc i" in exI, simp)
   671 apply (case_tac i, simp)
   672 apply (rename_tac j)
   673 apply (rule_tac x = j in exI, simp)
   674 done
   675 
   676 lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
   677 by (auto simp add: set_conv_nth)
   678 
   679 lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
   680 by (auto simp add: set_conv_nth)
   681 
   682 lemma all_nth_imp_all_set:
   683 "[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
   684 by (auto simp add: set_conv_nth)
   685 
   686 lemma all_set_conv_all_nth:
   687 "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
   688 by (auto simp add: set_conv_nth)
   689 
   690 
   691 subsection {* @{text list_update} *}
   692 
   693 lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs"
   694 by (induct xs) (auto split: nat.split)
   695 
   696 lemma nth_list_update:
   697 "!!i j. i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
   698 by (induct xs) (auto simp add: nth_Cons split: nat.split)
   699 
   700 lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
   701 by (simp add: nth_list_update)
   702 
   703 lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j"
   704 by (induct xs) (auto simp add: nth_Cons split: nat.split)
   705 
   706 lemma list_update_overwrite [simp]:
   707 "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
   708 by (induct xs) (auto split: nat.split)
   709 
   710 lemma list_update_id[simp]: "!!i. i < length xs \<Longrightarrow> xs[i := xs!i] = xs"
   711 apply (induct xs, simp)
   712 apply(simp split:nat.splits)
   713 done
   714 
   715 lemma list_update_same_conv:
   716 "!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
   717 by (induct xs) (auto split: nat.split)
   718 
   719 lemma list_update_append1:
   720  "!!i. i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
   721 apply (induct xs, simp)
   722 apply(simp split:nat.split)
   723 done
   724 
   725 lemma update_zip:
   726 "!!i xy xs. length xs = length ys ==>
   727 (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
   728 by (induct ys) (auto, case_tac xs, auto split: nat.split)
   729 
   730 lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"
   731 by (induct xs) (auto split: nat.split)
   732 
   733 lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
   734 by (blast dest!: set_update_subset_insert [THEN subsetD])
   735 
   736 
   737 subsection {* @{text last} and @{text butlast} *}
   738 
   739 lemma last_snoc [simp]: "last (xs @ [x]) = x"
   740 by (induct xs) auto
   741 
   742 lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
   743 by (induct xs) auto
   744 
   745 lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
   746 by (induct xs rule: rev_induct) auto
   747 
   748 lemma butlast_append:
   749 "!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
   750 by (induct xs) auto
   751 
   752 lemma append_butlast_last_id [simp]:
   753 "xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
   754 by (induct xs) auto
   755 
   756 lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
   757 by (induct xs) (auto split: split_if_asm)
   758 
   759 lemma in_set_butlast_appendI:
   760 "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
   761 by (auto dest: in_set_butlastD simp add: butlast_append)
   762 
   763 
   764 subsection {* @{text take} and @{text drop} *}
   765 
   766 lemma take_0 [simp]: "take 0 xs = []"
   767 by (induct xs) auto
   768 
   769 lemma drop_0 [simp]: "drop 0 xs = xs"
   770 by (induct xs) auto
   771 
   772 lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
   773 by simp
   774 
   775 lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
   776 by simp
   777 
   778 declare take_Cons [simp del] and drop_Cons [simp del]
   779 
   780 lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
   781 by(cases xs, simp_all)
   782 
   783 lemma drop_tl: "!!n. drop n (tl xs) = tl(drop n xs)"
   784 by(induct xs, simp_all add:drop_Cons drop_Suc split:nat.split)
   785 
   786 lemma nth_via_drop: "!!n. drop n xs = y#ys \<Longrightarrow> xs!n = y"
   787 apply (induct xs, simp)
   788 apply(simp add:drop_Cons nth_Cons split:nat.splits)
   789 done
   790 
   791 lemma take_Suc_conv_app_nth:
   792  "!!i. i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
   793 apply (induct xs, simp)
   794 apply (case_tac i, auto)
   795 done
   796 
   797 lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n"
   798 by (induct n) (auto, case_tac xs, auto)
   799 
   800 lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)"
   801 by (induct n) (auto, case_tac xs, auto)
   802 
   803 lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs"
   804 by (induct n) (auto, case_tac xs, auto)
   805 
   806 lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []"
   807 by (induct n) (auto, case_tac xs, auto)
   808 
   809 lemma take_append [simp]:
   810 "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
   811 by (induct n) (auto, case_tac xs, auto)
   812 
   813 lemma drop_append [simp]:
   814 "!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
   815 by (induct n) (auto, case_tac xs, auto)
   816 
   817 lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
   818 apply (induct m, auto)
   819 apply (case_tac xs, auto)
   820 apply (case_tac na, auto)
   821 done
   822 
   823 lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs"
   824 apply (induct m, auto)
   825 apply (case_tac xs, auto)
   826 done
   827 
   828 lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"
   829 apply (induct m, auto)
   830 apply (case_tac xs, auto)
   831 done
   832 
   833 lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs"
   834 apply (induct n, auto)
   835 apply (case_tac xs, auto)
   836 done
   837 
   838 lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"
   839 apply (induct n, auto)
   840 apply (case_tac xs, auto)
   841 done
   842 
   843 lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)"
   844 apply (induct n, auto)
   845 apply (case_tac xs, auto)
   846 done
   847 
   848 lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"
   849 apply (induct xs, auto)
   850 apply (case_tac i, auto)
   851 done
   852 
   853 lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"
   854 apply (induct xs, auto)
   855 apply (case_tac i, auto)
   856 done
   857 
   858 lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
   859 apply (induct xs, auto)
   860 apply (case_tac n, blast)
   861 apply (case_tac i, auto)
   862 done
   863 
   864 lemma nth_drop [simp]:
   865 "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
   866 apply (induct n, auto)
   867 apply (case_tac xs, auto)
   868 done
   869 
   870 lemma set_take_subset: "\<And>n. set(take n xs) \<subseteq> set xs"
   871 by(induct xs)(auto simp:take_Cons split:nat.split)
   872 
   873 lemma set_drop_subset: "\<And>n. set(drop n xs) \<subseteq> set xs"
   874 by(induct xs)(auto simp:drop_Cons split:nat.split)
   875 
   876 lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
   877 using set_take_subset by fast
   878 
   879 lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
   880 using set_drop_subset by fast
   881 
   882 lemma append_eq_conv_conj:
   883 "!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
   884 apply (induct xs, simp, clarsimp)
   885 apply (case_tac zs, auto)
   886 done
   887 
   888 lemma take_add [rule_format]: 
   889     "\<forall>i. i+j \<le> length(xs) --> take (i+j) xs = take i xs @ take j (drop i xs)"
   890 apply (induct xs, auto) 
   891 apply (case_tac i, simp_all) 
   892 done
   893 
   894 
   895 subsection {* @{text takeWhile} and @{text dropWhile} *}
   896 
   897 lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
   898 by (induct xs) auto
   899 
   900 lemma takeWhile_append1 [simp]:
   901 "[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
   902 by (induct xs) auto
   903 
   904 lemma takeWhile_append2 [simp]:
   905 "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
   906 by (induct xs) auto
   907 
   908 lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
   909 by (induct xs) auto
   910 
   911 lemma dropWhile_append1 [simp]:
   912 "[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
   913 by (induct xs) auto
   914 
   915 lemma dropWhile_append2 [simp]:
   916 "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
   917 by (induct xs) auto
   918 
   919 lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
   920 by (induct xs) (auto split: split_if_asm)
   921 
   922 lemma takeWhile_eq_all_conv[simp]:
   923  "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
   924 by(induct xs, auto)
   925 
   926 lemma dropWhile_eq_Nil_conv[simp]:
   927  "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
   928 by(induct xs, auto)
   929 
   930 lemma dropWhile_eq_Cons_conv:
   931  "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
   932 by(induct xs, auto)
   933 
   934 
   935 subsection {* @{text zip} *}
   936 
   937 lemma zip_Nil [simp]: "zip [] ys = []"
   938 by (induct ys) auto
   939 
   940 lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
   941 by simp
   942 
   943 declare zip_Cons [simp del]
   944 
   945 lemma length_zip [simp]:
   946 "!!xs. length (zip xs ys) = min (length xs) (length ys)"
   947 apply (induct ys, simp)
   948 apply (case_tac xs, auto)
   949 done
   950 
   951 lemma zip_append1:
   952 "!!xs. zip (xs @ ys) zs =
   953 zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
   954 apply (induct zs, simp)
   955 apply (case_tac xs, simp_all)
   956 done
   957 
   958 lemma zip_append2:
   959 "!!ys. zip xs (ys @ zs) =
   960 zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
   961 apply (induct xs, simp)
   962 apply (case_tac ys, simp_all)
   963 done
   964 
   965 lemma zip_append [simp]:
   966  "[| length xs = length us; length ys = length vs |] ==>
   967 zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
   968 by (simp add: zip_append1)
   969 
   970 lemma zip_rev:
   971 "!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
   972 apply (induct ys, simp)
   973 apply (case_tac xs, simp_all)
   974 done
   975 
   976 lemma nth_zip [simp]:
   977 "!!i xs. [| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
   978 apply (induct ys, simp)
   979 apply (case_tac xs)
   980  apply (simp_all add: nth.simps split: nat.split)
   981 done
   982 
   983 lemma set_zip:
   984 "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
   985 by (simp add: set_conv_nth cong: rev_conj_cong)
   986 
   987 lemma zip_update:
   988 "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
   989 by (rule sym, simp add: update_zip)
   990 
   991 lemma zip_replicate [simp]:
   992 "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
   993 apply (induct i, auto)
   994 apply (case_tac j, auto)
   995 done
   996 
   997 
   998 subsection {* @{text list_all2} *}
   999 
  1000 lemma list_all2_lengthD: "list_all2 P xs ys ==> length xs = length ys"
  1001 by (simp add: list_all2_def)
  1002 
  1003 lemma list_all2_Nil [iff]: "list_all2 P [] ys = (ys = [])"
  1004 by (simp add: list_all2_def)
  1005 
  1006 lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])"
  1007 by (simp add: list_all2_def)
  1008 
  1009 lemma list_all2_Cons [iff]:
  1010 "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
  1011 by (auto simp add: list_all2_def)
  1012 
  1013 lemma list_all2_Cons1:
  1014 "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
  1015 by (cases ys) auto
  1016 
  1017 lemma list_all2_Cons2:
  1018 "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
  1019 by (cases xs) auto
  1020 
  1021 lemma list_all2_rev [iff]:
  1022 "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
  1023 by (simp add: list_all2_def zip_rev cong: conj_cong)
  1024 
  1025 lemma list_all2_rev1:
  1026 "list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
  1027 by (subst list_all2_rev [symmetric]) simp
  1028 
  1029 lemma list_all2_append1:
  1030 "list_all2 P (xs @ ys) zs =
  1031 (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
  1032 list_all2 P xs us \<and> list_all2 P ys vs)"
  1033 apply (simp add: list_all2_def zip_append1)
  1034 apply (rule iffI)
  1035  apply (rule_tac x = "take (length xs) zs" in exI)
  1036  apply (rule_tac x = "drop (length xs) zs" in exI)
  1037  apply (force split: nat_diff_split simp add: min_def, clarify)
  1038 apply (simp add: ball_Un)
  1039 done
  1040 
  1041 lemma list_all2_append2:
  1042 "list_all2 P xs (ys @ zs) =
  1043 (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
  1044 list_all2 P us ys \<and> list_all2 P vs zs)"
  1045 apply (simp add: list_all2_def zip_append2)
  1046 apply (rule iffI)
  1047  apply (rule_tac x = "take (length ys) xs" in exI)
  1048  apply (rule_tac x = "drop (length ys) xs" in exI)
  1049  apply (force split: nat_diff_split simp add: min_def, clarify)
  1050 apply (simp add: ball_Un)
  1051 done
  1052 
  1053 lemma list_all2_append:
  1054   "\<And>b. length a = length b \<Longrightarrow>
  1055   list_all2 P (a@c) (b@d) = (list_all2 P a b \<and> list_all2 P c d)"
  1056   apply (induct a, simp)
  1057   apply (case_tac b, auto)
  1058   done
  1059 
  1060 lemma list_all2_appendI [intro?, trans]:
  1061   "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
  1062   by (simp add: list_all2_append list_all2_lengthD)
  1063 
  1064 lemma list_all2_conv_all_nth:
  1065 "list_all2 P xs ys =
  1066 (length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
  1067 by (force simp add: list_all2_def set_zip)
  1068 
  1069 lemma list_all2_trans:
  1070   assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
  1071   shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
  1072         (is "!!bs cs. PROP ?Q as bs cs")
  1073 proof (induct as)
  1074   fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
  1075   show "!!cs. PROP ?Q (x # xs) bs cs"
  1076   proof (induct bs)
  1077     fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
  1078     show "PROP ?Q (x # xs) (y # ys) cs"
  1079       by (induct cs) (auto intro: tr I1 I2)
  1080   qed simp
  1081 qed simp
  1082 
  1083 lemma list_all2_all_nthI [intro?]:
  1084   "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
  1085   by (simp add: list_all2_conv_all_nth)
  1086 
  1087 lemma list_all2_nthD [dest?]:
  1088   "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
  1089   by (simp add: list_all2_conv_all_nth)
  1090 
  1091 lemma list_all2_map1: 
  1092   "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
  1093   by (simp add: list_all2_conv_all_nth)
  1094 
  1095 lemma list_all2_map2: 
  1096   "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
  1097   by (auto simp add: list_all2_conv_all_nth)
  1098 
  1099 lemma list_all2_refl:
  1100   "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
  1101   by (simp add: list_all2_conv_all_nth)
  1102 
  1103 lemma list_all2_update_cong:
  1104   "\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
  1105   by (simp add: list_all2_conv_all_nth nth_list_update)
  1106 
  1107 lemma list_all2_update_cong2:
  1108   "\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
  1109   by (simp add: list_all2_lengthD list_all2_update_cong)
  1110 
  1111 lemma list_all2_dropI [intro?]:
  1112   "\<And>n bs. list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"
  1113   apply (induct as, simp)
  1114   apply (clarsimp simp add: list_all2_Cons1)
  1115   apply (case_tac n, simp, simp)
  1116   done
  1117 
  1118 lemma list_all2_mono [intro?]:
  1119   "\<And>y. list_all2 P x y \<Longrightarrow> (\<And>x y. P x y \<Longrightarrow> Q x y) \<Longrightarrow> list_all2 Q x y"
  1120   apply (induct x, simp)
  1121   apply (case_tac y, auto)
  1122   done
  1123 
  1124 
  1125 subsection {* @{text foldl} *}
  1126 
  1127 lemma foldl_append [simp]:
  1128 "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
  1129 by (induct xs) auto
  1130 
  1131 text {*
  1132 Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
  1133 difficult to use because it requires an additional transitivity step.
  1134 *}
  1135 
  1136 lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns"
  1137 by (induct ns) auto
  1138 
  1139 lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns"
  1140 by (force intro: start_le_sum simp add: in_set_conv_decomp)
  1141 
  1142 lemma sum_eq_0_conv [iff]:
  1143 "!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
  1144 by (induct ns) auto
  1145 
  1146 
  1147 subsection {* folding a relation over a list *}
  1148 
  1149 (*"fold_rel R cs \<equiv> foldl (%r c. r O {(x,y). (c,x,y):R}) Id cs"*)
  1150 inductive "fold_rel R" intros
  1151   Nil:  "(a, [],a) : fold_rel R"
  1152   Cons: "[|(a,x,b) : R; (b,xs,c) : fold_rel R|] ==> (a,x#xs,c) : fold_rel R"
  1153 inductive_cases fold_rel_elim_case [elim!]:
  1154    "(a, [] , b) : fold_rel R"
  1155    "(a, x#xs, b) : fold_rel R"
  1156 
  1157 lemma fold_rel_Nil [intro!]: "a = b ==> (a, [], b) : fold_rel R" 
  1158 by (simp add: fold_rel.Nil)
  1159 declare fold_rel.Cons [intro!]
  1160 
  1161 
  1162 subsection {* @{text upto} *}
  1163 
  1164 lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])"
  1165 -- {* Does not terminate! *}
  1166 by (induct j) auto
  1167 
  1168 lemma upt_conv_Nil [simp]: "j <= i ==> [i..j(] = []"
  1169 by (subst upt_rec) simp
  1170 
  1171 lemma upt_Suc_append: "i <= j ==> [i..(Suc j)(] = [i..j(]@[j]"
  1172 -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
  1173 by simp
  1174 
  1175 lemma upt_conv_Cons: "i < j ==> [i..j(] = i # [Suc i..j(]"
  1176 apply(rule trans)
  1177 apply(subst upt_rec)
  1178  prefer 2 apply (rule refl, simp)
  1179 done
  1180 
  1181 lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"
  1182 -- {* LOOPS as a simprule, since @{text "j <= j"}. *}
  1183 by (induct k) auto
  1184 
  1185 lemma length_upt [simp]: "length [i..j(] = j - i"
  1186 by (induct j) (auto simp add: Suc_diff_le)
  1187 
  1188 lemma nth_upt [simp]: "i + k < j ==> [i..j(] ! k = i + k"
  1189 apply (induct j)
  1190 apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
  1191 done
  1192 
  1193 lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]"
  1194 apply (induct m, simp)
  1195 apply (subst upt_rec)
  1196 apply (rule sym)
  1197 apply (subst upt_rec)
  1198 apply (simp del: upt.simps)
  1199 done
  1200 
  1201 lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]"
  1202 by (induct n) auto
  1203 
  1204 lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)"
  1205 apply (induct n m rule: diff_induct)
  1206 prefer 3 apply (subst map_Suc_upt[symmetric])
  1207 apply (auto simp add: less_diff_conv nth_upt)
  1208 done
  1209 
  1210 lemma nth_take_lemma:
  1211   "!!xs ys. k <= length xs ==> k <= length ys ==>
  1212      (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
  1213 apply (atomize, induct k)
  1214 apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
  1215 txt {* Both lists must be non-empty *}
  1216 apply (case_tac xs, simp)
  1217 apply (case_tac ys, clarify)
  1218  apply (simp (no_asm_use))
  1219 apply clarify
  1220 txt {* prenexing's needed, not miniscoping *}
  1221 apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
  1222 apply blast
  1223 done
  1224 
  1225 lemma nth_equalityI:
  1226  "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
  1227 apply (frule nth_take_lemma [OF le_refl eq_imp_le])
  1228 apply (simp_all add: take_all)
  1229 done
  1230 
  1231 (* needs nth_equalityI *)
  1232 lemma list_all2_antisym:
  1233   "\<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> 
  1234   \<Longrightarrow> xs = ys"
  1235   apply (simp add: list_all2_conv_all_nth) 
  1236   apply (rule nth_equalityI, blast, simp)
  1237   done
  1238 
  1239 lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
  1240 -- {* The famous take-lemma. *}
  1241 apply (drule_tac x = "max (length xs) (length ys)" in spec)
  1242 apply (simp add: le_max_iff_disj take_all)
  1243 done
  1244 
  1245 
  1246 subsection {* @{text "distinct"} and @{text remdups} *}
  1247 
  1248 lemma distinct_append [simp]:
  1249 "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
  1250 by (induct xs) auto
  1251 
  1252 lemma set_remdups [simp]: "set (remdups xs) = set xs"
  1253 by (induct xs) (auto simp add: insert_absorb)
  1254 
  1255 lemma distinct_remdups [iff]: "distinct (remdups xs)"
  1256 by (induct xs) auto
  1257 
  1258 lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
  1259 by (induct xs) auto
  1260 
  1261 text {*
  1262 It is best to avoid this indexed version of distinct, but sometimes
  1263 it is useful. *}
  1264 lemma distinct_conv_nth:
  1265 "distinct xs = (\<forall>i j. i < size xs \<and> j < size xs \<and> i \<noteq> j --> xs!i \<noteq> xs!j)"
  1266 apply (induct_tac xs, simp, simp)
  1267 apply (rule iffI, clarsimp)
  1268  apply (case_tac i)
  1269 apply (case_tac j, simp)
  1270 apply (simp add: set_conv_nth)
  1271  apply (case_tac j)
  1272 apply (clarsimp simp add: set_conv_nth, simp)
  1273 apply (rule conjI)
  1274  apply (clarsimp simp add: set_conv_nth)
  1275  apply (erule_tac x = 0 in allE)
  1276  apply (erule_tac x = "Suc i" in allE, simp, clarsimp)
  1277 apply (erule_tac x = "Suc i" in allE)
  1278 apply (erule_tac x = "Suc j" in allE, simp)
  1279 done
  1280 
  1281 
  1282 subsection {* @{text replicate} *}
  1283 
  1284 lemma length_replicate [simp]: "length (replicate n x) = n"
  1285 by (induct n) auto
  1286 
  1287 lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
  1288 by (induct n) auto
  1289 
  1290 lemma replicate_app_Cons_same:
  1291 "(replicate n x) @ (x # xs) = x # replicate n x @ xs"
  1292 by (induct n) auto
  1293 
  1294 lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
  1295 apply (induct n, simp)
  1296 apply (simp add: replicate_app_Cons_same)
  1297 done
  1298 
  1299 lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
  1300 by (induct n) auto
  1301 
  1302 lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
  1303 by (induct n) auto
  1304 
  1305 lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
  1306 by (induct n) auto
  1307 
  1308 lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
  1309 by (atomize (full), induct n) auto
  1310 
  1311 lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x"
  1312 apply (induct n, simp)
  1313 apply (simp add: nth_Cons split: nat.split)
  1314 done
  1315 
  1316 lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
  1317 by (induct n) auto
  1318 
  1319 lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
  1320 by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
  1321 
  1322 lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
  1323 by auto
  1324 
  1325 lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
  1326 by (simp add: set_replicate_conv_if split: split_if_asm)
  1327 
  1328 
  1329 subsection {* @{text postfix} *}
  1330 
  1331 lemma postfix_refl [simp, intro!]: "xs \<sqsupseteq> xs" by (auto simp add: postfix_def)
  1332 lemma postfix_trans: "\<lbrakk>xs \<sqsupseteq> ys; ys \<sqsupseteq> zs\<rbrakk> \<Longrightarrow> xs \<sqsupseteq> zs" 
  1333          by (auto simp add: postfix_def)
  1334 lemma postfix_antisym: "\<lbrakk>xs \<sqsupseteq> ys; ys \<sqsupseteq> xs\<rbrakk> \<Longrightarrow> xs = ys" 
  1335          by (auto simp add: postfix_def)
  1336 
  1337 lemma postfix_emptyI [simp, intro!]: "xs \<sqsupseteq> []" by (auto simp add: postfix_def)
  1338 lemma postfix_emptyD [dest!]: "[] \<sqsupseteq> xs \<Longrightarrow> xs = []"by(auto simp add:postfix_def)
  1339 lemma postfix_ConsI: "xs \<sqsupseteq> ys \<Longrightarrow> x#xs \<sqsupseteq> ys" by (auto simp add: postfix_def)
  1340 lemma postfix_ConsD: "xs \<sqsupseteq> y#ys \<Longrightarrow> xs \<sqsupseteq> ys" by (auto simp add: postfix_def)
  1341 lemma postfix_appendI: "xs \<sqsupseteq> ys \<Longrightarrow> zs@xs \<sqsupseteq> ys" by (auto simp add: postfix_def)
  1342 lemma postfix_appendD: "xs \<sqsupseteq> zs@ys \<Longrightarrow> xs \<sqsupseteq> ys" by (auto simp add: postfix_def)
  1343 
  1344 lemma postfix_is_subset_lemma: "xs = zs @ ys \<Longrightarrow> set ys \<subseteq> set xs"
  1345 by (induct zs, auto)
  1346 lemma postfix_is_subset: "xs \<sqsupseteq> ys \<Longrightarrow> set ys \<subseteq> set xs"
  1347 by (unfold postfix_def, erule exE, erule postfix_is_subset_lemma)
  1348 
  1349 lemma postfix_ConsD2_lemma [rule_format]: "x#xs = zs @ y#ys \<longrightarrow> xs \<sqsupseteq> ys"
  1350 by (induct zs, auto intro!: postfix_appendI postfix_ConsI)
  1351 lemma postfix_ConsD2: "x#xs \<sqsupseteq> y#ys \<Longrightarrow> xs \<sqsupseteq> ys"
  1352 by (auto simp add: postfix_def dest!: postfix_ConsD2_lemma)
  1353 
  1354 subsection {* Lexicographic orderings on lists *}
  1355 
  1356 lemma wf_lexn: "wf r ==> wf (lexn r n)"
  1357 apply (induct_tac n, simp, simp)
  1358 apply(rule wf_subset)
  1359  prefer 2 apply (rule Int_lower1)
  1360 apply(rule wf_prod_fun_image)
  1361  prefer 2 apply (rule inj_onI, auto)
  1362 done
  1363 
  1364 lemma lexn_length:
  1365 "!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
  1366 by (induct n) auto
  1367 
  1368 lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
  1369 apply (unfold lex_def)
  1370 apply (rule wf_UN)
  1371 apply (blast intro: wf_lexn, clarify)
  1372 apply (rename_tac m n)
  1373 apply (subgoal_tac "m \<noteq> n")
  1374  prefer 2 apply blast
  1375 apply (blast dest: lexn_length not_sym)
  1376 done
  1377 
  1378 lemma lexn_conv:
  1379 "lexn r n =
  1380 {(xs,ys). length xs = n \<and> length ys = n \<and>
  1381 (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
  1382 apply (induct_tac n, simp, blast)
  1383 apply (simp add: image_Collect lex_prod_def, safe, blast)
  1384  apply (rule_tac x = "ab # xys" in exI, simp)
  1385 apply (case_tac xys, simp_all, blast)
  1386 done
  1387 
  1388 lemma lex_conv:
  1389 "lex r =
  1390 {(xs,ys). length xs = length ys \<and>
  1391 (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
  1392 by (force simp add: lex_def lexn_conv)
  1393 
  1394 lemma wf_lexico [intro!]: "wf r ==> wf (lexico r)"
  1395 by (unfold lexico_def) blast
  1396 
  1397 lemma lexico_conv:
  1398 "lexico r = {(xs,ys). length xs < length ys |
  1399 length xs = length ys \<and> (xs, ys) : lex r}"
  1400 by (simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def)
  1401 
  1402 lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
  1403 by (simp add: lex_conv)
  1404 
  1405 lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
  1406 by (simp add:lex_conv)
  1407 
  1408 lemma Cons_in_lex [iff]:
  1409 "((x # xs, y # ys) : lex r) =
  1410 ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
  1411 apply (simp add: lex_conv)
  1412 apply (rule iffI)
  1413  prefer 2 apply (blast intro: Cons_eq_appendI, clarify)
  1414 apply (case_tac xys, simp, simp)
  1415 apply blast
  1416 done
  1417 
  1418 
  1419 subsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
  1420 
  1421 lemma sublist_empty [simp]: "sublist xs {} = []"
  1422 by (auto simp add: sublist_def)
  1423 
  1424 lemma sublist_nil [simp]: "sublist [] A = []"
  1425 by (auto simp add: sublist_def)
  1426 
  1427 lemma sublist_shift_lemma:
  1428 "map fst [p:zip xs [i..i + length xs(] . snd p : A] =
  1429 map fst [p:zip xs [0..length xs(] . snd p + i : A]"
  1430 by (induct xs rule: rev_induct) (simp_all add: add_commute)
  1431 
  1432 lemma sublist_append:
  1433 "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
  1434 apply (unfold sublist_def)
  1435 apply (induct l' rule: rev_induct, simp)
  1436 apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
  1437 apply (simp add: add_commute)
  1438 done
  1439 
  1440 lemma sublist_Cons:
  1441 "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
  1442 apply (induct l rule: rev_induct)
  1443  apply (simp add: sublist_def)
  1444 apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
  1445 done
  1446 
  1447 lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
  1448 by (simp add: sublist_Cons)
  1449 
  1450 lemma sublist_upt_eq_take [simp]: "sublist l {..n(} = take n l"
  1451 apply (induct l rule: rev_induct, simp)
  1452 apply (simp split: nat_diff_split add: sublist_append)
  1453 done
  1454 
  1455 
  1456 lemma take_Cons':
  1457 "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
  1458 by (cases n) simp_all
  1459 
  1460 lemma drop_Cons':
  1461 "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
  1462 by (cases n) simp_all
  1463 
  1464 lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
  1465 by (cases n) simp_all
  1466 
  1467 lemmas [simp] = take_Cons'[of "number_of v",standard]
  1468                 drop_Cons'[of "number_of v",standard]
  1469                 nth_Cons'[of _ _ "number_of v",standard]
  1470 
  1471 
  1472 subsection {* Characters and strings *}
  1473 
  1474 datatype nibble =
  1475     Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7
  1476   | Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF
  1477 
  1478 datatype char = Char nibble nibble
  1479   -- "Note: canonical order of character encoding coincides with standard term ordering"
  1480 
  1481 types string = "char list"
  1482 
  1483 syntax
  1484   "_Char" :: "xstr => char"    ("CHR _")
  1485   "_String" :: "xstr => string"    ("_")
  1486 
  1487 parse_ast_translation {*
  1488   let
  1489     val constants = Syntax.Appl o map Syntax.Constant;
  1490 
  1491     fun mk_nib n = "Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10));
  1492     fun mk_char c =
  1493       if Symbol.is_ascii c andalso Symbol.is_printable c then
  1494         constants ["Char", mk_nib (ord c div 16), mk_nib (ord c mod 16)]
  1495       else error ("Printable ASCII character expected: " ^ quote c);
  1496 
  1497     fun mk_string [] = Syntax.Constant "Nil"
  1498       | mk_string (c :: cs) = Syntax.Appl [Syntax.Constant "Cons", mk_char c, mk_string cs];
  1499 
  1500     fun char_ast_tr [Syntax.Variable xstr] =
  1501         (case Syntax.explode_xstr xstr of
  1502           [c] => mk_char c
  1503         | _ => error ("Single character expected: " ^ xstr))
  1504       | char_ast_tr asts = raise AST ("char_ast_tr", asts);
  1505 
  1506     fun string_ast_tr [Syntax.Variable xstr] =
  1507         (case Syntax.explode_xstr xstr of
  1508           [] => constants [Syntax.constrainC, "Nil", "string"]
  1509         | cs => mk_string cs)
  1510       | string_ast_tr asts = raise AST ("string_tr", asts);
  1511   in [("_Char", char_ast_tr), ("_String", string_ast_tr)] end;
  1512 *}
  1513 
  1514 print_ast_translation {*
  1515   let
  1516     fun dest_nib (Syntax.Constant c) =
  1517         (case explode c of
  1518           ["N", "i", "b", "b", "l", "e", h] =>
  1519             if "0" <= h andalso h <= "9" then ord h - ord "0"
  1520             else if "A" <= h andalso h <= "F" then ord h - ord "A" + 10
  1521             else raise Match
  1522         | _ => raise Match)
  1523       | dest_nib _ = raise Match;
  1524 
  1525     fun dest_chr c1 c2 =
  1526       let val c = chr (dest_nib c1 * 16 + dest_nib c2)
  1527       in if Symbol.is_printable c then c else raise Match end;
  1528 
  1529     fun dest_char (Syntax.Appl [Syntax.Constant "Char", c1, c2]) = dest_chr c1 c2
  1530       | dest_char _ = raise Match;
  1531 
  1532     fun xstr cs = Syntax.Appl [Syntax.Constant "_xstr", Syntax.Variable (Syntax.implode_xstr cs)];
  1533 
  1534     fun char_ast_tr' [c1, c2] = Syntax.Appl [Syntax.Constant "_Char", xstr [dest_chr c1 c2]]
  1535       | char_ast_tr' _ = raise Match;
  1536 
  1537     fun list_ast_tr' [args] = Syntax.Appl [Syntax.Constant "_String",
  1538             xstr (map dest_char (Syntax.unfold_ast "_args" args))]
  1539       | list_ast_tr' ts = raise Match;
  1540   in [("Char", char_ast_tr'), ("@list", list_ast_tr')] end;
  1541 *}
  1542 
  1543 end