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