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