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