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