src/HOL/List.thy
author nipkow
Thu Dec 22 13:00:53 2005 +0100 (2005-12-22)
changeset 18490 434e34392c40
parent 18451 5ff0244e25e8
child 18622 4524643feecc
permissions -rw-r--r--
new lemmas
     1 (*  Title:      HOL/List.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4 *)
     5 
     6 header {* The datatype of finite lists *}
     7 
     8 theory List
     9 imports PreList
    10 begin
    11 
    12 datatype 'a list =
    13     Nil    ("[]")
    14   | Cons 'a  "'a list"    (infixr "#" 65)
    15 
    16 subsection{*Basic list processing functions*}
    17 
    18 consts
    19   "@" :: "'a list => 'a list => 'a list"    (infixr 65)
    20   filter:: "('a => bool) => 'a list => 'a list"
    21   concat:: "'a list list => 'a list"
    22   foldl :: "('b => 'a => 'b) => 'b => 'a list => 'b"
    23   foldr :: "('a => 'b => 'b) => 'a list => 'b => 'b"
    24   hd:: "'a list => 'a"
    25   tl:: "'a list => 'a list"
    26   last:: "'a list => 'a"
    27   butlast :: "'a list => 'a list"
    28   set :: "'a list => 'a set"
    29   list_all2 :: "('a => 'b => bool) => 'a list => 'b list => bool"
    30   map :: "('a=>'b) => ('a list => 'b list)"
    31   nth :: "'a list => nat => 'a"    (infixl "!" 100)
    32   list_update :: "'a list => nat => 'a => 'a list"
    33   take:: "nat => 'a list => 'a list"
    34   drop:: "nat => 'a list => 'a list"
    35   takeWhile :: "('a => bool) => 'a list => 'a list"
    36   dropWhile :: "('a => bool) => 'a list => 'a list"
    37   rev :: "'a list => 'a list"
    38   zip :: "'a list => 'b list => ('a * 'b) list"
    39   upt :: "nat => nat => nat list" ("(1[_..</_'])")
    40   remdups :: "'a list => 'a list"
    41   remove1 :: "'a => 'a list => 'a list"
    42   null:: "'a list => bool"
    43   "distinct":: "'a list => bool"
    44   replicate :: "nat => 'a => 'a list"
    45   rotate1 :: "'a list \<Rightarrow> 'a list"
    46   rotate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
    47   sublist :: "'a list => nat set => 'a list"
    48 (* For efficiency *)
    49   mem :: "'a => 'a list => bool"    (infixl 55)
    50   list_inter :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
    51   list_ex :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> bool"
    52   list_all:: "('a => bool) => ('a list => bool)"
    53   itrev :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
    54   filtermap :: "('a \<Rightarrow> 'b option) \<Rightarrow> 'a list \<Rightarrow> 'b list"
    55   map_filter :: "('a => 'b) => ('a => bool) => 'a list => 'b list"
    56 
    57 
    58 nonterminals lupdbinds lupdbind
    59 
    60 syntax
    61   -- {* list Enumeration *}
    62   "@list" :: "args => 'a list"    ("[(_)]")
    63 
    64   -- {* Special syntax for filter *}
    65   "@filter" :: "[pttrn, 'a list, bool] => 'a list"    ("(1[_:_./ _])")
    66 
    67   -- {* list update *}
    68   "_lupdbind":: "['a, 'a] => lupdbind"    ("(2_ :=/ _)")
    69   "" :: "lupdbind => lupdbinds"    ("_")
    70   "_lupdbinds" :: "[lupdbind, lupdbinds] => lupdbinds"    ("_,/ _")
    71   "_LUpdate" :: "['a, lupdbinds] => 'a"    ("_/[(_)]" [900,0] 900)
    72 
    73   upto:: "nat => nat => nat list"    ("(1[_../_])")
    74 
    75 translations
    76   "[x, xs]" == "x#[xs]"
    77   "[x]" == "x#[]"
    78   "[x:xs . P]"== "filter (%x. P) xs"
    79 
    80   "_LUpdate xs (_lupdbinds b bs)"== "_LUpdate (_LUpdate xs b) bs"
    81   "xs[i:=x]" == "list_update xs i x"
    82 
    83   "[i..j]" == "[i..<(Suc j)]"
    84 
    85 
    86 syntax (xsymbols)
    87   "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])")
    88 syntax (HTML output)
    89   "@filter" :: "[pttrn, 'a list, bool] => 'a list"("(1[_\<in>_ ./ _])")
    90 
    91 
    92 text {*
    93   Function @{text size} is overloaded for all datatypes. Users may
    94   refer to the list version as @{text length}. *}
    95 
    96 syntax length :: "'a list => nat"
    97 translations "length" => "size :: _ list => nat"
    98 
    99 typed_print_translation {*
   100   let
   101     fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
   102           Syntax.const "length" $ t
   103       | size_tr' _ _ _ = raise Match;
   104   in [("size", size_tr')] end
   105 *}
   106 
   107 
   108 primrec
   109   "hd(x#xs) = x"
   110 
   111 primrec
   112   "tl([]) = []"
   113   "tl(x#xs) = xs"
   114 
   115 primrec
   116   "null([]) = True"
   117   "null(x#xs) = False"
   118 
   119 primrec
   120   "last(x#xs) = (if xs=[] then x else last xs)"
   121 
   122 primrec
   123   "butlast []= []"
   124   "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
   125 
   126 primrec
   127   "set [] = {}"
   128   "set (x#xs) = insert x (set xs)"
   129 
   130 primrec
   131   "map f [] = []"
   132   "map f (x#xs) = f(x)#map f xs"
   133 
   134 primrec
   135   append_Nil:"[]@ys = ys"
   136   append_Cons: "(x#xs)@ys = x#(xs@ys)"
   137 
   138 primrec
   139   "rev([]) = []"
   140   "rev(x#xs) = rev(xs) @ [x]"
   141 
   142 primrec
   143   "filter P [] = []"
   144   "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
   145 
   146 primrec
   147   foldl_Nil:"foldl f a [] = a"
   148   foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
   149 
   150 primrec
   151   "foldr f [] a = a"
   152   "foldr f (x#xs) a = f x (foldr f xs a)"
   153 
   154 primrec
   155   "concat([]) = []"
   156   "concat(x#xs) = x @ concat(xs)"
   157 
   158 primrec
   159   drop_Nil:"drop n [] = []"
   160   drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
   161   -- {*Warning: simpset does not contain this definition, but separate
   162        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
   163 
   164 primrec
   165   take_Nil:"take n [] = []"
   166   take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
   167   -- {*Warning: simpset does not contain this definition, but separate
   168        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
   169 
   170 primrec
   171   nth_Cons:"(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
   172   -- {*Warning: simpset does not contain this definition, but separate
   173        theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
   174 
   175 primrec
   176   "[][i:=v] = []"
   177   "(x#xs)[i:=v] = (case i of 0 => v # xs | Suc j => x # xs[j:=v])"
   178 
   179 primrec
   180   "takeWhile P [] = []"
   181   "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
   182 
   183 primrec
   184   "dropWhile P [] = []"
   185   "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
   186 
   187 primrec
   188   "zip xs [] = []"
   189   zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
   190   -- {*Warning: simpset does not contain this definition, but separate
   191        theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
   192 
   193 primrec
   194   upt_0: "[i..<0] = []"
   195   upt_Suc: "[i..<(Suc j)] = (if i <= j then [i..<j] @ [j] else [])"
   196 
   197 primrec
   198   "distinct [] = True"
   199   "distinct (x#xs) = (x ~: set xs \<and> distinct xs)"
   200 
   201 primrec
   202   "remdups [] = []"
   203   "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
   204 
   205 primrec
   206   "remove1 x [] = []"
   207   "remove1 x (y#xs) = (if x=y then xs else y # remove1 x xs)"
   208 
   209 primrec
   210   replicate_0: "replicate 0 x = []"
   211   replicate_Suc: "replicate (Suc n) x = x # replicate n x"
   212 
   213 defs
   214 rotate1_def: "rotate1 xs == (case xs of [] \<Rightarrow> [] | x#xs \<Rightarrow> xs @ [x])"
   215 rotate_def:  "rotate n == rotate1 ^ n"
   216 
   217 list_all2_def:
   218  "list_all2 P xs ys ==
   219   length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)"
   220 
   221 sublist_def:
   222  "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..<size xs]))"
   223 
   224 primrec
   225   "x mem [] = False"
   226   "x mem (y#ys) = (if y=x then True else x mem ys)"
   227 
   228 primrec
   229  "list_inter [] bs = []"
   230  "list_inter (a#as) bs =
   231   (if a \<in> set bs then a#(list_inter as bs) else list_inter as bs)"
   232 
   233 primrec
   234   "list_all P [] = True"
   235   "list_all P (x#xs) = (P(x) \<and> list_all P xs)"
   236 
   237 primrec
   238 "list_ex P [] = False"
   239 "list_ex P (x#xs) = (P x \<or> list_ex P xs)"
   240 
   241 primrec
   242  "filtermap f [] = []"
   243  "filtermap f (x#xs) =
   244     (case f x of None \<Rightarrow> filtermap f xs
   245      | Some y \<Rightarrow> y # (filtermap f xs))"
   246 
   247 primrec
   248   "map_filter f P [] = []"
   249   "map_filter f P (x#xs) = (if P x then f x # map_filter f P xs else 
   250                map_filter f P xs)"
   251 
   252 primrec
   253 "itrev [] ys = ys"
   254 "itrev (x#xs) ys = itrev xs (x#ys)"
   255 
   256 
   257 lemma not_Cons_self [simp]: "xs \<noteq> x # xs"
   258 by (induct xs) auto
   259 
   260 lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
   261 
   262 lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
   263 by (induct xs) auto
   264 
   265 lemma length_induct:
   266 "(!!xs. \<forall>ys. length ys < length xs --> P ys ==> P xs) ==> P xs"
   267 by (rule measure_induct [of length]) iprover
   268 
   269 
   270 subsubsection {* @{text length} *}
   271 
   272 text {*
   273 Needs to come before @{text "@"} because of theorem @{text
   274 append_eq_append_conv}.
   275 *}
   276 
   277 lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
   278 by (induct xs) auto
   279 
   280 lemma length_map [simp]: "length (map f xs) = length xs"
   281 by (induct xs) auto
   282 
   283 lemma length_rev [simp]: "length (rev xs) = length xs"
   284 by (induct xs) auto
   285 
   286 lemma length_tl [simp]: "length (tl xs) = length xs - 1"
   287 by (cases xs) auto
   288 
   289 lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
   290 by (induct xs) auto
   291 
   292 lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
   293 by (induct xs) auto
   294 
   295 lemma length_Suc_conv:
   296 "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
   297 by (induct xs) auto
   298 
   299 lemma Suc_length_conv:
   300 "(Suc n = length xs) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
   301 apply (induct xs, simp, simp)
   302 apply blast
   303 done
   304 
   305 lemma impossible_Cons [rule_format]: 
   306   "length xs <= length ys --> xs = x # ys = False"
   307 apply (induct xs, auto)
   308 done
   309 
   310 lemma list_induct2[consumes 1]: "\<And>ys.
   311  \<lbrakk> length xs = length ys;
   312    P [] [];
   313    \<And>x xs y ys. \<lbrakk> length xs = length ys; P xs ys \<rbrakk> \<Longrightarrow> P (x#xs) (y#ys) \<rbrakk>
   314  \<Longrightarrow> P xs ys"
   315 apply(induct xs)
   316  apply simp
   317 apply(case_tac ys)
   318  apply simp
   319 apply(simp)
   320 done
   321 
   322 subsubsection {* @{text "@"} -- append *}
   323 
   324 lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
   325 by (induct xs) auto
   326 
   327 lemma append_Nil2 [simp]: "xs @ [] = xs"
   328 by (induct xs) auto
   329 
   330 lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
   331 by (induct xs) auto
   332 
   333 lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
   334 by (induct xs) auto
   335 
   336 lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
   337 by (induct xs) auto
   338 
   339 lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
   340 by (induct xs) auto
   341 
   342 lemma append_eq_append_conv [simp]:
   343  "!!ys. length xs = length ys \<or> length us = length vs
   344  ==> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
   345 apply (induct xs)
   346  apply (case_tac ys, simp, force)
   347 apply (case_tac ys, force, simp)
   348 done
   349 
   350 lemma append_eq_append_conv2: "!!ys zs ts.
   351  (xs @ ys = zs @ ts) =
   352  (EX us. xs = zs @ us & us @ ys = ts | xs @ us = zs & ys = us@ ts)"
   353 apply (induct xs)
   354  apply fastsimp
   355 apply(case_tac zs)
   356  apply simp
   357 apply fastsimp
   358 done
   359 
   360 lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
   361 by simp
   362 
   363 lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
   364 by simp
   365 
   366 lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
   367 by simp
   368 
   369 lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
   370 using append_same_eq [of _ _ "[]"] by auto
   371 
   372 lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
   373 using append_same_eq [of "[]"] by auto
   374 
   375 lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
   376 by (induct xs) auto
   377 
   378 lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
   379 by (induct xs) auto
   380 
   381 lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
   382 by (simp add: hd_append split: list.split)
   383 
   384 lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
   385 by (simp split: list.split)
   386 
   387 lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
   388 by (simp add: tl_append split: list.split)
   389 
   390 
   391 lemma Cons_eq_append_conv: "x#xs = ys@zs =
   392  (ys = [] & x#xs = zs | (EX ys'. x#ys' = ys & xs = ys'@zs))"
   393 by(cases ys) auto
   394 
   395 lemma append_eq_Cons_conv: "(ys@zs = x#xs) =
   396  (ys = [] & zs = x#xs | (EX ys'. ys = x#ys' & ys'@zs = xs))"
   397 by(cases ys) auto
   398 
   399 
   400 text {* Trivial rules for solving @{text "@"}-equations automatically. *}
   401 
   402 lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
   403 by simp
   404 
   405 lemma Cons_eq_appendI:
   406 "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
   407 by (drule sym) simp
   408 
   409 lemma append_eq_appendI:
   410 "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
   411 by (drule sym) simp
   412 
   413 
   414 text {*
   415 Simplification procedure for all list equalities.
   416 Currently only tries to rearrange @{text "@"} to see if
   417 - both lists end in a singleton list,
   418 - or both lists end in the same list.
   419 *}
   420 
   421 ML_setup {*
   422 local
   423 
   424 val append_assoc = thm "append_assoc";
   425 val append_Nil = thm "append_Nil";
   426 val append_Cons = thm "append_Cons";
   427 val append1_eq_conv = thm "append1_eq_conv";
   428 val append_same_eq = thm "append_same_eq";
   429 
   430 fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
   431   (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
   432   | last (Const("List.op @",_) $ _ $ ys) = last ys
   433   | last t = t;
   434 
   435 fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
   436   | list1 _ = false;
   437 
   438 fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
   439   (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
   440   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
   441   | butlast xs = Const("List.list.Nil",fastype_of xs);
   442 
   443 val rearr_ss = HOL_basic_ss addsimps [append_assoc, append_Nil, append_Cons];
   444 
   445 fun list_eq sg ss (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
   446   let
   447     val lastl = last lhs and lastr = last rhs;
   448     fun rearr conv =
   449       let
   450         val lhs1 = butlast lhs and rhs1 = butlast rhs;
   451         val Type(_,listT::_) = eqT
   452         val appT = [listT,listT] ---> listT
   453         val app = Const("List.op @",appT)
   454         val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
   455         val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (F,F2));
   456         val thm = Goal.prove sg [] [] eq
   457           (K (simp_tac (Simplifier.inherit_context ss rearr_ss) 1));
   458       in SOME ((conv RS (thm RS trans)) RS eq_reflection) end;
   459 
   460   in
   461     if list1 lastl andalso list1 lastr then rearr append1_eq_conv
   462     else if lastl aconv lastr then rearr append_same_eq
   463     else NONE
   464   end;
   465 
   466 in
   467 
   468 val list_eq_simproc =
   469   Simplifier.simproc (Theory.sign_of (the_context ())) "list_eq" ["(xs::'a list) = ys"] list_eq;
   470 
   471 end;
   472 
   473 Addsimprocs [list_eq_simproc];
   474 *}
   475 
   476 
   477 subsubsection {* @{text map} *}
   478 
   479 lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
   480 by (induct xs) simp_all
   481 
   482 lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
   483 by (rule ext, induct_tac xs) auto
   484 
   485 lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
   486 by (induct xs) auto
   487 
   488 lemma map_compose: "map (f o g) xs = map f (map g xs)"
   489 by (induct xs) (auto simp add: o_def)
   490 
   491 lemma rev_map: "rev (map f xs) = map f (rev xs)"
   492 by (induct xs) auto
   493 
   494 lemma map_eq_conv[simp]: "(map f xs = map g xs) = (!x : set xs. f x = g x)"
   495 by (induct xs) auto
   496 
   497 lemma map_cong [recdef_cong]:
   498 "xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
   499 -- {* a congruence rule for @{text map} *}
   500 by simp
   501 
   502 lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
   503 by (cases xs) auto
   504 
   505 lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
   506 by (cases xs) auto
   507 
   508 lemma map_eq_Cons_conv:
   509  "(map f xs = y#ys) = (\<exists>z zs. xs = z#zs \<and> f z = y \<and> map f zs = ys)"
   510 by (cases xs) auto
   511 
   512 lemma Cons_eq_map_conv:
   513  "(x#xs = map f ys) = (\<exists>z zs. ys = z#zs \<and> x = f z \<and> xs = map f zs)"
   514 by (cases ys) auto
   515 
   516 lemmas map_eq_Cons_D = map_eq_Cons_conv [THEN iffD1]
   517 lemmas Cons_eq_map_D = Cons_eq_map_conv [THEN iffD1]
   518 declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!]
   519 
   520 lemma ex_map_conv:
   521   "(EX xs. ys = map f xs) = (ALL y : set ys. EX x. y = f x)"
   522 by(induct ys, auto simp add: Cons_eq_map_conv)
   523 
   524 lemma map_eq_imp_length_eq:
   525   "!!xs. map f xs = map f ys ==> length xs = length ys"
   526 apply (induct ys)
   527  apply simp
   528 apply(simp (no_asm_use))
   529 apply clarify
   530 apply(simp (no_asm_use))
   531 apply fast
   532 done
   533 
   534 lemma map_inj_on:
   535  "[| map f xs = map f ys; inj_on f (set xs Un set ys) |]
   536   ==> xs = ys"
   537 apply(frule map_eq_imp_length_eq)
   538 apply(rotate_tac -1)
   539 apply(induct rule:list_induct2)
   540  apply simp
   541 apply(simp)
   542 apply (blast intro:sym)
   543 done
   544 
   545 lemma inj_on_map_eq_map:
   546  "inj_on f (set xs Un set ys) \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
   547 by(blast dest:map_inj_on)
   548 
   549 lemma map_injective:
   550  "!!xs. map f xs = map f ys ==> inj f ==> xs = ys"
   551 by (induct ys) (auto dest!:injD)
   552 
   553 lemma inj_map_eq_map[simp]: "inj f \<Longrightarrow> (map f xs = map f ys) = (xs = ys)"
   554 by(blast dest:map_injective)
   555 
   556 lemma inj_mapI: "inj f ==> inj (map f)"
   557 by (iprover dest: map_injective injD intro: inj_onI)
   558 
   559 lemma inj_mapD: "inj (map f) ==> inj f"
   560 apply (unfold inj_on_def, clarify)
   561 apply (erule_tac x = "[x]" in ballE)
   562  apply (erule_tac x = "[y]" in ballE, simp, blast)
   563 apply blast
   564 done
   565 
   566 lemma inj_map[iff]: "inj (map f) = inj f"
   567 by (blast dest: inj_mapD intro: inj_mapI)
   568 
   569 lemma inj_on_mapI: "inj_on f (\<Union>(set ` A)) \<Longrightarrow> inj_on (map f) A"
   570 apply(rule inj_onI)
   571 apply(erule map_inj_on)
   572 apply(blast intro:inj_onI dest:inj_onD)
   573 done
   574 
   575 lemma map_idI: "(\<And>x. x \<in> set xs \<Longrightarrow> f x = x) \<Longrightarrow> map f xs = xs"
   576 by (induct xs, auto)
   577 
   578 lemma map_fun_upd [simp]: "y \<notin> set xs \<Longrightarrow> map (f(y:=v)) xs = map f xs"
   579 by (induct xs) auto
   580 
   581 lemma map_fst_zip[simp]:
   582   "length xs = length ys \<Longrightarrow> map fst (zip xs ys) = xs"
   583 by (induct rule:list_induct2, simp_all)
   584 
   585 lemma map_snd_zip[simp]:
   586   "length xs = length ys \<Longrightarrow> map snd (zip xs ys) = ys"
   587 by (induct rule:list_induct2, simp_all)
   588 
   589 
   590 subsubsection {* @{text rev} *}
   591 
   592 lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
   593 by (induct xs) auto
   594 
   595 lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
   596 by (induct xs) auto
   597 
   598 lemma rev_swap: "(rev xs = ys) = (xs = rev ys)"
   599 by auto
   600 
   601 lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
   602 by (induct xs) auto
   603 
   604 lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
   605 by (induct xs) auto
   606 
   607 lemma rev_singleton_conv [simp]: "(rev xs = [x]) = (xs = [x])"
   608 by (cases xs) auto
   609 
   610 lemma singleton_rev_conv [simp]: "([x] = rev xs) = (xs = [x])"
   611 by (cases xs) auto
   612 
   613 lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
   614 apply (induct xs, force)
   615 apply (case_tac ys, simp, force)
   616 done
   617 
   618 lemma inj_on_rev[iff]: "inj_on rev A"
   619 by(simp add:inj_on_def)
   620 
   621 lemma rev_induct [case_names Nil snoc]:
   622   "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
   623 apply(simplesubst rev_rev_ident[symmetric])
   624 apply(rule_tac list = "rev xs" in list.induct, simp_all)
   625 done
   626 
   627 ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}-- "compatibility"
   628 
   629 lemma rev_exhaust [case_names Nil snoc]:
   630   "(xs = [] ==> P) ==>(!!ys y. xs = ys @ [y] ==> P) ==> P"
   631 by (induct xs rule: rev_induct) auto
   632 
   633 lemmas rev_cases = rev_exhaust
   634 
   635 lemma rev_eq_Cons_iff[iff]: "(rev xs = y#ys) = (xs = rev ys @ [y])"
   636 by(rule rev_cases[of xs]) auto
   637 
   638 
   639 subsubsection {* @{text set} *}
   640 
   641 lemma finite_set [iff]: "finite (set xs)"
   642 by (induct xs) auto
   643 
   644 lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
   645 by (induct xs) auto
   646 
   647 lemma hd_in_set[simp]: "xs \<noteq> [] \<Longrightarrow> hd xs : set xs"
   648 by(cases xs) auto
   649 
   650 lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
   651 by auto
   652 
   653 lemma set_ConsD: "y \<in> set (x # xs) \<Longrightarrow> y=x \<or> y \<in> set xs" 
   654 by auto
   655 
   656 lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
   657 by (induct xs) auto
   658 
   659 lemma set_empty2[iff]: "({} = set xs) = (xs = [])"
   660 by(induct xs) auto
   661 
   662 lemma set_rev [simp]: "set (rev xs) = set xs"
   663 by (induct xs) auto
   664 
   665 lemma set_map [simp]: "set (map f xs) = f`(set xs)"
   666 by (induct xs) auto
   667 
   668 lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
   669 by (induct xs) auto
   670 
   671 lemma set_upt [simp]: "set[i..<j] = {k. i \<le> k \<and> k < j}"
   672 apply (induct j, simp_all)
   673 apply (erule ssubst, auto)
   674 done
   675 
   676 lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
   677 proof (induct xs)
   678   case Nil show ?case by simp
   679   case (Cons a xs)
   680   show ?case
   681   proof 
   682     assume "x \<in> set (a # xs)"
   683     with prems show "\<exists>ys zs. a # xs = ys @ x # zs"
   684       by (simp, blast intro: Cons_eq_appendI)
   685   next
   686     assume "\<exists>ys zs. a # xs = ys @ x # zs"
   687     then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast
   688     show "x \<in> set (a # xs)" 
   689       by (cases ys, auto simp add: eq)
   690   qed
   691 qed
   692 
   693 lemma in_set_conv_decomp_first:
   694  "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs \<and> x \<notin> set ys)"
   695 proof (induct xs)
   696   case Nil show ?case by simp
   697 next
   698   case (Cons a xs)
   699   show ?case
   700   proof cases
   701     assume "x = a" thus ?case using Cons by force
   702   next
   703     assume "x \<noteq> a"
   704     show ?case
   705     proof
   706       assume "x \<in> set (a # xs)"
   707       from prems show "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys"
   708 	by(fastsimp intro!: Cons_eq_appendI)
   709     next
   710       assume "\<exists>ys zs. a # xs = ys @ x # zs \<and> x \<notin> set ys"
   711       then obtain ys zs where eq: "a # xs = ys @ x # zs" by blast
   712       show "x \<in> set (a # xs)" by (cases ys, auto simp add: eq)
   713     qed
   714   qed
   715 qed
   716 
   717 lemmas split_list       = in_set_conv_decomp[THEN iffD1, standard]
   718 lemmas split_list_first = in_set_conv_decomp_first[THEN iffD1, standard]
   719 
   720 
   721 lemma finite_list: "finite A ==> EX l. set l = A"
   722 apply (erule finite_induct, auto)
   723 apply (rule_tac x="x#l" in exI, auto)
   724 done
   725 
   726 lemma card_length: "card (set xs) \<le> length xs"
   727 by (induct xs) (auto simp add: card_insert_if)
   728 
   729 
   730 subsubsection {* @{text filter} *}
   731 
   732 lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
   733 by (induct xs) auto
   734 
   735 lemma rev_filter: "rev (filter P xs) = filter P (rev xs)"
   736 by (induct xs) simp_all
   737 
   738 lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
   739 by (induct xs) auto
   740 
   741 lemma length_filter_le [simp]: "length (filter P xs) \<le> length xs"
   742 by (induct xs) (auto simp add: le_SucI)
   743 
   744 lemma sum_length_filter_compl:
   745   "length(filter P xs) + length(filter (%x. ~P x) xs) = length xs"
   746 by(induct xs) simp_all
   747 
   748 lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
   749 by (induct xs) auto
   750 
   751 lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
   752 by (induct xs) auto
   753 
   754 lemma filter_empty_conv: "(filter P xs = []) = (\<forall>x\<in>set xs. \<not> P x)" 
   755   by (induct xs) simp_all
   756 
   757 lemma filter_id_conv: "(filter P xs = xs) = (\<forall>x\<in>set xs. P x)"
   758 apply (induct xs)
   759  apply auto
   760 apply(cut_tac P=P and xs=xs in length_filter_le)
   761 apply simp
   762 done
   763 
   764 lemma filter_map:
   765   "filter P (map f xs) = map f (filter (P o f) xs)"
   766 by (induct xs) simp_all
   767 
   768 lemma length_filter_map[simp]:
   769   "length (filter P (map f xs)) = length(filter (P o f) xs)"
   770 by (simp add:filter_map)
   771 
   772 lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
   773 by auto
   774 
   775 lemma length_filter_less:
   776   "\<lbrakk> x : set xs; ~ P x \<rbrakk> \<Longrightarrow> length(filter P xs) < length xs"
   777 proof (induct xs)
   778   case Nil thus ?case by simp
   779 next
   780   case (Cons x xs) thus ?case
   781     apply (auto split:split_if_asm)
   782     using length_filter_le[of P xs] apply arith
   783   done
   784 qed
   785 
   786 lemma length_filter_conv_card:
   787  "length(filter p xs) = card{i. i < length xs & p(xs!i)}"
   788 proof (induct xs)
   789   case Nil thus ?case by simp
   790 next
   791   case (Cons x xs)
   792   let ?S = "{i. i < length xs & p(xs!i)}"
   793   have fin: "finite ?S" by(fast intro: bounded_nat_set_is_finite)
   794   show ?case (is "?l = card ?S'")
   795   proof (cases)
   796     assume "p x"
   797     hence eq: "?S' = insert 0 (Suc ` ?S)"
   798       by(auto simp add: nth_Cons image_def split:nat.split elim:lessE)
   799     have "length (filter p (x # xs)) = Suc(card ?S)"
   800       using Cons by simp
   801     also have "\<dots> = Suc(card(Suc ` ?S))" using fin
   802       by (simp add: card_image inj_Suc)
   803     also have "\<dots> = card ?S'" using eq fin
   804       by (simp add:card_insert_if) (simp add:image_def)
   805     finally show ?thesis .
   806   next
   807     assume "\<not> p x"
   808     hence eq: "?S' = Suc ` ?S"
   809       by(auto simp add: nth_Cons image_def split:nat.split elim:lessE)
   810     have "length (filter p (x # xs)) = card ?S"
   811       using Cons by simp
   812     also have "\<dots> = card(Suc ` ?S)" using fin
   813       by (simp add: card_image inj_Suc)
   814     also have "\<dots> = card ?S'" using eq fin
   815       by (simp add:card_insert_if)
   816     finally show ?thesis .
   817   qed
   818 qed
   819 
   820 lemma Cons_eq_filterD:
   821  "x#xs = filter P ys \<Longrightarrow>
   822   \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"
   823   (concl is "\<exists>us vs. ?P ys us vs")
   824 proof(induct ys)
   825   case Nil thus ?case by simp
   826 next
   827   case (Cons y ys)
   828   show ?case (is "\<exists>x. ?Q x")
   829   proof cases
   830     assume Py: "P y"
   831     show ?thesis
   832     proof cases
   833       assume xy: "x = y"
   834       show ?thesis
   835       proof from Py xy Cons(2) show "?Q []" by simp qed
   836     next
   837       assume "x \<noteq> y" with Py Cons(2) show ?thesis by simp
   838     qed
   839   next
   840     assume Py: "\<not> P y"
   841     with Cons obtain us vs where 1 : "?P (y#ys) (y#us) vs" by fastsimp
   842     show ?thesis (is "? us. ?Q us")
   843     proof show "?Q (y#us)" using 1 by simp qed
   844   qed
   845 qed
   846 
   847 lemma filter_eq_ConsD:
   848  "filter P ys = x#xs \<Longrightarrow>
   849   \<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs"
   850 by(rule Cons_eq_filterD) simp
   851 
   852 lemma filter_eq_Cons_iff:
   853  "(filter P ys = x#xs) =
   854   (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"
   855 by(auto dest:filter_eq_ConsD)
   856 
   857 lemma Cons_eq_filter_iff:
   858  "(x#xs = filter P ys) =
   859   (\<exists>us vs. ys = us @ x # vs \<and> (\<forall>u\<in>set us. \<not> P u) \<and> P x \<and> xs = filter P vs)"
   860 by(auto dest:Cons_eq_filterD)
   861 
   862 lemma filter_cong[recdef_cong]:
   863  "xs = ys \<Longrightarrow> (\<And>x. x \<in> set ys \<Longrightarrow> P x = Q x) \<Longrightarrow> filter P xs = filter Q ys"
   864 apply simp
   865 apply(erule thin_rl)
   866 by (induct ys) simp_all
   867 
   868 
   869 subsubsection {* @{text concat} *}
   870 
   871 lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
   872 by (induct xs) auto
   873 
   874 lemma concat_eq_Nil_conv [simp]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
   875 by (induct xss) auto
   876 
   877 lemma Nil_eq_concat_conv [simp]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
   878 by (induct xss) auto
   879 
   880 lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)"
   881 by (induct xs) auto
   882 
   883 lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
   884 by (induct xs) auto
   885 
   886 lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
   887 by (induct xs) auto
   888 
   889 lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
   890 by (induct xs) auto
   891 
   892 
   893 subsubsection {* @{text nth} *}
   894 
   895 lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
   896 by auto
   897 
   898 lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
   899 by auto
   900 
   901 declare nth.simps [simp del]
   902 
   903 lemma nth_append:
   904 "!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
   905 apply (induct "xs", simp)
   906 apply (case_tac n, auto)
   907 done
   908 
   909 lemma nth_append_length [simp]: "(xs @ x # ys) ! length xs = x"
   910 by (induct "xs") auto
   911 
   912 lemma nth_append_length_plus[simp]: "(xs @ ys) ! (length xs + n) = ys ! n"
   913 by (induct "xs") auto
   914 
   915 lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)"
   916 apply (induct xs, simp)
   917 apply (case_tac n, auto)
   918 done
   919 
   920 lemma hd_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd xs = xs!0"
   921 by(cases xs) simp_all
   922 
   923 
   924 lemma list_eq_iff_nth_eq:
   925  "!!ys. (xs = ys) = (length xs = length ys \<and> (ALL i<length xs. xs!i = ys!i))"
   926 apply(induct xs)
   927  apply simp apply blast
   928 apply(case_tac ys)
   929  apply simp
   930 apply(simp add:nth_Cons split:nat.split)apply blast
   931 done
   932 
   933 lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
   934 apply (induct xs, simp, simp)
   935 apply safe
   936 apply (rule_tac x = 0 in exI, simp)
   937  apply (rule_tac x = "Suc i" in exI, simp)
   938 apply (case_tac i, simp)
   939 apply (rename_tac j)
   940 apply (rule_tac x = j in exI, simp)
   941 done
   942 
   943 lemma in_set_conv_nth: "(x \<in> set xs) = (\<exists>i < length xs. xs!i = x)"
   944 by(auto simp:set_conv_nth)
   945 
   946 lemma list_ball_nth: "[| n < length xs; !x : set xs. P x|] ==> P(xs!n)"
   947 by (auto simp add: set_conv_nth)
   948 
   949 lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
   950 by (auto simp add: set_conv_nth)
   951 
   952 lemma all_nth_imp_all_set:
   953 "[| !i < length xs. P(xs!i); x : set xs|] ==> P x"
   954 by (auto simp add: set_conv_nth)
   955 
   956 lemma all_set_conv_all_nth:
   957 "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
   958 by (auto simp add: set_conv_nth)
   959 
   960 
   961 subsubsection {* @{text list_update} *}
   962 
   963 lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs"
   964 by (induct xs) (auto split: nat.split)
   965 
   966 lemma nth_list_update:
   967 "!!i j. i < length xs==> (xs[i:=x])!j = (if i = j then x else xs!j)"
   968 by (induct xs) (auto simp add: nth_Cons split: nat.split)
   969 
   970 lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
   971 by (simp add: nth_list_update)
   972 
   973 lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j"
   974 by (induct xs) (auto simp add: nth_Cons split: nat.split)
   975 
   976 lemma list_update_overwrite [simp]:
   977 "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
   978 by (induct xs) (auto split: nat.split)
   979 
   980 lemma list_update_id[simp]: "!!i. i < length xs ==> xs[i := xs!i] = xs"
   981 apply (induct xs, simp)
   982 apply(simp split:nat.splits)
   983 done
   984 
   985 lemma list_update_beyond[simp]: "\<And>i. length xs \<le> i \<Longrightarrow> xs[i:=x] = xs"
   986 apply (induct xs)
   987  apply simp
   988 apply (case_tac i)
   989 apply simp_all
   990 done
   991 
   992 lemma list_update_same_conv:
   993 "!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
   994 by (induct xs) (auto split: nat.split)
   995 
   996 lemma list_update_append1:
   997  "!!i. i < size xs \<Longrightarrow> (xs @ ys)[i:=x] = xs[i:=x] @ ys"
   998 apply (induct xs, simp)
   999 apply(simp split:nat.split)
  1000 done
  1001 
  1002 lemma list_update_append:
  1003   "!!n. (xs @ ys) [n:= x] = 
  1004   (if n < length xs then xs[n:= x] @ ys else xs @ (ys [n-length xs:= x]))"
  1005 by (induct xs) (auto split:nat.splits)
  1006 
  1007 lemma list_update_length [simp]:
  1008  "(xs @ x # ys)[length xs := y] = (xs @ y # ys)"
  1009 by (induct xs, auto)
  1010 
  1011 lemma update_zip:
  1012 "!!i xy xs. length xs = length ys ==>
  1013 (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
  1014 by (induct ys) (auto, case_tac xs, auto split: nat.split)
  1015 
  1016 lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"
  1017 by (induct xs) (auto split: nat.split)
  1018 
  1019 lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
  1020 by (blast dest!: set_update_subset_insert [THEN subsetD])
  1021 
  1022 lemma set_update_memI: "!!n. n < length xs \<Longrightarrow> x \<in> set (xs[n := x])"
  1023 by (induct xs) (auto split:nat.splits)
  1024 
  1025 
  1026 subsubsection {* @{text last} and @{text butlast} *}
  1027 
  1028 lemma last_snoc [simp]: "last (xs @ [x]) = x"
  1029 by (induct xs) auto
  1030 
  1031 lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
  1032 by (induct xs) auto
  1033 
  1034 lemma last_ConsL: "xs = [] \<Longrightarrow> last(x#xs) = x"
  1035 by(simp add:last.simps)
  1036 
  1037 lemma last_ConsR: "xs \<noteq> [] \<Longrightarrow> last(x#xs) = last xs"
  1038 by(simp add:last.simps)
  1039 
  1040 lemma last_append: "last(xs @ ys) = (if ys = [] then last xs else last ys)"
  1041 by (induct xs) (auto)
  1042 
  1043 lemma last_appendL[simp]: "ys = [] \<Longrightarrow> last(xs @ ys) = last xs"
  1044 by(simp add:last_append)
  1045 
  1046 lemma last_appendR[simp]: "ys \<noteq> [] \<Longrightarrow> last(xs @ ys) = last ys"
  1047 by(simp add:last_append)
  1048 
  1049 lemma hd_rev: "xs \<noteq> [] \<Longrightarrow> hd(rev xs) = last xs"
  1050 by(rule rev_exhaust[of xs]) simp_all
  1051 
  1052 lemma last_rev: "xs \<noteq> [] \<Longrightarrow> last(rev xs) = hd xs"
  1053 by(cases xs) simp_all
  1054 
  1055 lemma last_in_set[simp]: "as \<noteq> [] \<Longrightarrow> last as \<in> set as"
  1056 by (induct as) auto
  1057 
  1058 lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
  1059 by (induct xs rule: rev_induct) auto
  1060 
  1061 lemma butlast_append:
  1062 "!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
  1063 by (induct xs) auto
  1064 
  1065 lemma append_butlast_last_id [simp]:
  1066 "xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
  1067 by (induct xs) auto
  1068 
  1069 lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
  1070 by (induct xs) (auto split: split_if_asm)
  1071 
  1072 lemma in_set_butlast_appendI:
  1073 "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
  1074 by (auto dest: in_set_butlastD simp add: butlast_append)
  1075 
  1076 lemma last_drop[simp]: "!!n. n < length xs \<Longrightarrow> last (drop n xs) = last xs"
  1077 apply (induct xs)
  1078  apply simp
  1079 apply (auto split:nat.split)
  1080 done
  1081 
  1082 lemma last_conv_nth: "xs\<noteq>[] \<Longrightarrow> last xs = xs!(length xs - 1)"
  1083 by(induct xs)(auto simp:neq_Nil_conv)
  1084 
  1085 
  1086 subsubsection {* @{text take} and @{text drop} *}
  1087 
  1088 lemma take_0 [simp]: "take 0 xs = []"
  1089 by (induct xs) auto
  1090 
  1091 lemma drop_0 [simp]: "drop 0 xs = xs"
  1092 by (induct xs) auto
  1093 
  1094 lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
  1095 by simp
  1096 
  1097 lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
  1098 by simp
  1099 
  1100 declare take_Cons [simp del] and drop_Cons [simp del]
  1101 
  1102 lemma take_Suc: "xs ~= [] ==> take (Suc n) xs = hd xs # take n (tl xs)"
  1103 by(clarsimp simp add:neq_Nil_conv)
  1104 
  1105 lemma drop_Suc: "drop (Suc n) xs = drop n (tl xs)"
  1106 by(cases xs, simp_all)
  1107 
  1108 lemma drop_tl: "!!n. drop n (tl xs) = tl(drop n xs)"
  1109 by(induct xs, simp_all add:drop_Cons drop_Suc split:nat.split)
  1110 
  1111 lemma nth_via_drop: "!!n. drop n xs = y#ys \<Longrightarrow> xs!n = y"
  1112 apply (induct xs, simp)
  1113 apply(simp add:drop_Cons nth_Cons split:nat.splits)
  1114 done
  1115 
  1116 lemma take_Suc_conv_app_nth:
  1117  "!!i. i < length xs \<Longrightarrow> take (Suc i) xs = take i xs @ [xs!i]"
  1118 apply (induct xs, simp)
  1119 apply (case_tac i, auto)
  1120 done
  1121 
  1122 lemma drop_Suc_conv_tl:
  1123   "!!i. i < length xs \<Longrightarrow> (xs!i) # (drop (Suc i) xs) = drop i xs"
  1124 apply (induct xs, simp)
  1125 apply (case_tac i, auto)
  1126 done
  1127 
  1128 lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n"
  1129 by (induct n) (auto, case_tac xs, auto)
  1130 
  1131 lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)"
  1132 by (induct n) (auto, case_tac xs, auto)
  1133 
  1134 lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs"
  1135 by (induct n) (auto, case_tac xs, auto)
  1136 
  1137 lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []"
  1138 by (induct n) (auto, case_tac xs, auto)
  1139 
  1140 lemma take_append [simp]:
  1141 "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
  1142 by (induct n) (auto, case_tac xs, auto)
  1143 
  1144 lemma drop_append [simp]:
  1145 "!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
  1146 by (induct n) (auto, case_tac xs, auto)
  1147 
  1148 lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
  1149 apply (induct m, auto)
  1150 apply (case_tac xs, auto)
  1151 apply (case_tac n, auto)
  1152 done
  1153 
  1154 lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs"
  1155 apply (induct m, auto)
  1156 apply (case_tac xs, auto)
  1157 done
  1158 
  1159 lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"
  1160 apply (induct m, auto)
  1161 apply (case_tac xs, auto)
  1162 done
  1163 
  1164 lemma drop_take: "!!m n. drop n (take m xs) = take (m-n) (drop n xs)"
  1165 apply(induct xs)
  1166  apply simp
  1167 apply(simp add: take_Cons drop_Cons split:nat.split)
  1168 done
  1169 
  1170 lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs"
  1171 apply (induct n, auto)
  1172 apply (case_tac xs, auto)
  1173 done
  1174 
  1175 lemma take_eq_Nil[simp]: "!!n. (take n xs = []) = (n = 0 \<or> xs = [])"
  1176 apply(induct xs)
  1177  apply simp
  1178 apply(simp add:take_Cons split:nat.split)
  1179 done
  1180 
  1181 lemma drop_eq_Nil[simp]: "!!n. (drop n xs = []) = (length xs <= n)"
  1182 apply(induct xs)
  1183 apply simp
  1184 apply(simp add:drop_Cons split:nat.split)
  1185 done
  1186 
  1187 lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"
  1188 apply (induct n, auto)
  1189 apply (case_tac xs, auto)
  1190 done
  1191 
  1192 lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)"
  1193 apply (induct n, auto)
  1194 apply (case_tac xs, auto)
  1195 done
  1196 
  1197 lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"
  1198 apply (induct xs, auto)
  1199 apply (case_tac i, auto)
  1200 done
  1201 
  1202 lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"
  1203 apply (induct xs, auto)
  1204 apply (case_tac i, auto)
  1205 done
  1206 
  1207 lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
  1208 apply (induct xs, auto)
  1209 apply (case_tac n, blast)
  1210 apply (case_tac i, auto)
  1211 done
  1212 
  1213 lemma nth_drop [simp]:
  1214 "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
  1215 apply (induct n, auto)
  1216 apply (case_tac xs, auto)
  1217 done
  1218 
  1219 lemma hd_drop_conv_nth: "\<lbrakk> xs \<noteq> []; n < length xs \<rbrakk> \<Longrightarrow> hd(drop n xs) = xs!n"
  1220 by(simp add: hd_conv_nth)
  1221 
  1222 lemma set_take_subset: "\<And>n. set(take n xs) \<subseteq> set xs"
  1223 by(induct xs)(auto simp:take_Cons split:nat.split)
  1224 
  1225 lemma set_drop_subset: "\<And>n. set(drop n xs) \<subseteq> set xs"
  1226 by(induct xs)(auto simp:drop_Cons split:nat.split)
  1227 
  1228 lemma in_set_takeD: "x : set(take n xs) \<Longrightarrow> x : set xs"
  1229 using set_take_subset by fast
  1230 
  1231 lemma in_set_dropD: "x : set(drop n xs) \<Longrightarrow> x : set xs"
  1232 using set_drop_subset by fast
  1233 
  1234 lemma append_eq_conv_conj:
  1235 "!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
  1236 apply (induct xs, simp, clarsimp)
  1237 apply (case_tac zs, auto)
  1238 done
  1239 
  1240 lemma take_add [rule_format]: 
  1241     "\<forall>i. i+j \<le> length(xs) --> take (i+j) xs = take i xs @ take j (drop i xs)"
  1242 apply (induct xs, auto) 
  1243 apply (case_tac i, simp_all) 
  1244 done
  1245 
  1246 lemma append_eq_append_conv_if:
  1247  "!! ys\<^isub>1. (xs\<^isub>1 @ xs\<^isub>2 = ys\<^isub>1 @ ys\<^isub>2) =
  1248   (if size xs\<^isub>1 \<le> size ys\<^isub>1
  1249    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
  1250    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)"
  1251 apply(induct xs\<^isub>1)
  1252  apply simp
  1253 apply(case_tac ys\<^isub>1)
  1254 apply simp_all
  1255 done
  1256 
  1257 lemma take_hd_drop:
  1258   "!!n. n < length xs \<Longrightarrow> take n xs @ [hd (drop n xs)] = take (n+1) xs"
  1259 apply(induct xs)
  1260 apply simp
  1261 apply(simp add:drop_Cons split:nat.split)
  1262 done
  1263 
  1264 lemma id_take_nth_drop:
  1265  "i < length xs \<Longrightarrow> xs = take i xs @ xs!i # drop (Suc i) xs" 
  1266 proof -
  1267   assume si: "i < length xs"
  1268   hence "xs = take (Suc i) xs @ drop (Suc i) xs" by auto
  1269   moreover
  1270   from si have "take (Suc i) xs = take i xs @ [xs!i]"
  1271     apply (rule_tac take_Suc_conv_app_nth) by arith
  1272   ultimately show ?thesis by auto
  1273 qed
  1274   
  1275 lemma upd_conv_take_nth_drop:
  1276  "i < length xs \<Longrightarrow> xs[i:=a] = take i xs @ a # drop (Suc i) xs"
  1277 proof -
  1278   assume i: "i < length xs"
  1279   have "xs[i:=a] = (take i xs @ xs!i # drop (Suc i) xs)[i:=a]"
  1280     by(rule arg_cong[OF id_take_nth_drop[OF i]])
  1281   also have "\<dots> = take i xs @ a # drop (Suc i) xs"
  1282     using i by (simp add: list_update_append)
  1283   finally show ?thesis .
  1284 qed
  1285 
  1286 
  1287 subsubsection {* @{text takeWhile} and @{text dropWhile} *}
  1288 
  1289 lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
  1290 by (induct xs) auto
  1291 
  1292 lemma takeWhile_append1 [simp]:
  1293 "[| x:set xs; ~P(x)|] ==> takeWhile P (xs @ ys) = takeWhile P xs"
  1294 by (induct xs) auto
  1295 
  1296 lemma takeWhile_append2 [simp]:
  1297 "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
  1298 by (induct xs) auto
  1299 
  1300 lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
  1301 by (induct xs) auto
  1302 
  1303 lemma dropWhile_append1 [simp]:
  1304 "[| x : set xs; ~P(x)|] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
  1305 by (induct xs) auto
  1306 
  1307 lemma dropWhile_append2 [simp]:
  1308 "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
  1309 by (induct xs) auto
  1310 
  1311 lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
  1312 by (induct xs) (auto split: split_if_asm)
  1313 
  1314 lemma takeWhile_eq_all_conv[simp]:
  1315  "(takeWhile P xs = xs) = (\<forall>x \<in> set xs. P x)"
  1316 by(induct xs, auto)
  1317 
  1318 lemma dropWhile_eq_Nil_conv[simp]:
  1319  "(dropWhile P xs = []) = (\<forall>x \<in> set xs. P x)"
  1320 by(induct xs, auto)
  1321 
  1322 lemma dropWhile_eq_Cons_conv:
  1323  "(dropWhile P xs = y#ys) = (xs = takeWhile P xs @ y # ys & \<not> P y)"
  1324 by(induct xs, auto)
  1325 
  1326 text{* The following two lemmmas could be generalized to an arbitrary
  1327 property. *}
  1328 
  1329 lemma takeWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
  1330  takeWhile (\<lambda>y. y \<noteq> x) (rev xs) = rev (tl (dropWhile (\<lambda>y. y \<noteq> x) xs))"
  1331 by(induct xs) (auto simp: takeWhile_tail[where l="[]"])
  1332 
  1333 lemma dropWhile_neq_rev: "\<lbrakk>distinct xs; x \<in> set xs\<rbrakk> \<Longrightarrow>
  1334   dropWhile (\<lambda>y. y \<noteq> x) (rev xs) = x # rev (takeWhile (\<lambda>y. y \<noteq> x) xs)"
  1335 apply(induct xs)
  1336  apply simp
  1337 apply auto
  1338 apply(subst dropWhile_append2)
  1339 apply auto
  1340 done
  1341 
  1342 lemma takeWhile_not_last:
  1343  "\<lbrakk> xs \<noteq> []; distinct xs\<rbrakk> \<Longrightarrow> takeWhile (\<lambda>y. y \<noteq> last xs) xs = butlast xs"
  1344 apply(induct xs)
  1345  apply simp
  1346 apply(case_tac xs)
  1347 apply(auto)
  1348 done
  1349 
  1350 lemma takeWhile_cong [recdef_cong]:
  1351   "[| l = k; !!x. x : set l ==> P x = Q x |] 
  1352   ==> takeWhile P l = takeWhile Q k"
  1353   by (induct k fixing: l, simp_all)
  1354 
  1355 lemma dropWhile_cong [recdef_cong]:
  1356   "[| l = k; !!x. x : set l ==> P x = Q x |] 
  1357   ==> dropWhile P l = dropWhile Q k"
  1358   by (induct k fixing: l, simp_all)
  1359 
  1360 
  1361 subsubsection {* @{text zip} *}
  1362 
  1363 lemma zip_Nil [simp]: "zip [] ys = []"
  1364 by (induct ys) auto
  1365 
  1366 lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
  1367 by simp
  1368 
  1369 declare zip_Cons [simp del]
  1370 
  1371 lemma zip_Cons1:
  1372  "zip (x#xs) ys = (case ys of [] \<Rightarrow> [] | y#ys \<Rightarrow> (x,y)#zip xs ys)"
  1373 by(auto split:list.split)
  1374 
  1375 lemma length_zip [simp]:
  1376 "!!xs. length (zip xs ys) = min (length xs) (length ys)"
  1377 apply (induct ys, simp)
  1378 apply (case_tac xs, auto)
  1379 done
  1380 
  1381 lemma zip_append1:
  1382 "!!xs. zip (xs @ ys) zs =
  1383 zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
  1384 apply (induct zs, simp)
  1385 apply (case_tac xs, simp_all)
  1386 done
  1387 
  1388 lemma zip_append2:
  1389 "!!ys. zip xs (ys @ zs) =
  1390 zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
  1391 apply (induct xs, simp)
  1392 apply (case_tac ys, simp_all)
  1393 done
  1394 
  1395 lemma zip_append [simp]:
  1396  "[| length xs = length us; length ys = length vs |] ==>
  1397 zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
  1398 by (simp add: zip_append1)
  1399 
  1400 lemma zip_rev:
  1401 "length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
  1402 by (induct rule:list_induct2, simp_all)
  1403 
  1404 lemma nth_zip [simp]:
  1405 "!!i xs. [| i < length xs; i < length ys|] ==> (zip xs ys)!i = (xs!i, ys!i)"
  1406 apply (induct ys, simp)
  1407 apply (case_tac xs)
  1408  apply (simp_all add: nth.simps split: nat.split)
  1409 done
  1410 
  1411 lemma set_zip:
  1412 "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
  1413 by (simp add: set_conv_nth cong: rev_conj_cong)
  1414 
  1415 lemma zip_update:
  1416 "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
  1417 by (rule sym, simp add: update_zip)
  1418 
  1419 lemma zip_replicate [simp]:
  1420 "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
  1421 apply (induct i, auto)
  1422 apply (case_tac j, auto)
  1423 done
  1424 
  1425 
  1426 subsubsection {* @{text list_all2} *}
  1427 
  1428 lemma list_all2_lengthD [intro?]: 
  1429   "list_all2 P xs ys ==> length xs = length ys"
  1430 by (simp add: list_all2_def)
  1431 
  1432 lemma list_all2_Nil [iff,code]: "list_all2 P [] ys = (ys = [])"
  1433 by (simp add: list_all2_def)
  1434 
  1435 lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])"
  1436 by (simp add: list_all2_def)
  1437 
  1438 lemma list_all2_Cons [iff,code]:
  1439 "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
  1440 by (auto simp add: list_all2_def)
  1441 
  1442 lemma list_all2_Cons1:
  1443 "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
  1444 by (cases ys) auto
  1445 
  1446 lemma list_all2_Cons2:
  1447 "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
  1448 by (cases xs) auto
  1449 
  1450 lemma list_all2_rev [iff]:
  1451 "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
  1452 by (simp add: list_all2_def zip_rev cong: conj_cong)
  1453 
  1454 lemma list_all2_rev1:
  1455 "list_all2 P (rev xs) ys = list_all2 P xs (rev ys)"
  1456 by (subst list_all2_rev [symmetric]) simp
  1457 
  1458 lemma list_all2_append1:
  1459 "list_all2 P (xs @ ys) zs =
  1460 (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
  1461 list_all2 P xs us \<and> list_all2 P ys vs)"
  1462 apply (simp add: list_all2_def zip_append1)
  1463 apply (rule iffI)
  1464  apply (rule_tac x = "take (length xs) zs" in exI)
  1465  apply (rule_tac x = "drop (length xs) zs" in exI)
  1466  apply (force split: nat_diff_split simp add: min_def, clarify)
  1467 apply (simp add: ball_Un)
  1468 done
  1469 
  1470 lemma list_all2_append2:
  1471 "list_all2 P xs (ys @ zs) =
  1472 (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
  1473 list_all2 P us ys \<and> list_all2 P vs zs)"
  1474 apply (simp add: list_all2_def zip_append2)
  1475 apply (rule iffI)
  1476  apply (rule_tac x = "take (length ys) xs" in exI)
  1477  apply (rule_tac x = "drop (length ys) xs" in exI)
  1478  apply (force split: nat_diff_split simp add: min_def, clarify)
  1479 apply (simp add: ball_Un)
  1480 done
  1481 
  1482 lemma list_all2_append:
  1483   "length xs = length ys \<Longrightarrow>
  1484   list_all2 P (xs@us) (ys@vs) = (list_all2 P xs ys \<and> list_all2 P us vs)"
  1485 by (induct rule:list_induct2, simp_all)
  1486 
  1487 lemma list_all2_appendI [intro?, trans]:
  1488   "\<lbrakk> list_all2 P a b; list_all2 P c d \<rbrakk> \<Longrightarrow> list_all2 P (a@c) (b@d)"
  1489   by (simp add: list_all2_append list_all2_lengthD)
  1490 
  1491 lemma list_all2_conv_all_nth:
  1492 "list_all2 P xs ys =
  1493 (length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
  1494 by (force simp add: list_all2_def set_zip)
  1495 
  1496 lemma list_all2_trans:
  1497   assumes tr: "!!a b c. P1 a b ==> P2 b c ==> P3 a c"
  1498   shows "!!bs cs. list_all2 P1 as bs ==> list_all2 P2 bs cs ==> list_all2 P3 as cs"
  1499         (is "!!bs cs. PROP ?Q as bs cs")
  1500 proof (induct as)
  1501   fix x xs bs assume I1: "!!bs cs. PROP ?Q xs bs cs"
  1502   show "!!cs. PROP ?Q (x # xs) bs cs"
  1503   proof (induct bs)
  1504     fix y ys cs assume I2: "!!cs. PROP ?Q (x # xs) ys cs"
  1505     show "PROP ?Q (x # xs) (y # ys) cs"
  1506       by (induct cs) (auto intro: tr I1 I2)
  1507   qed simp
  1508 qed simp
  1509 
  1510 lemma list_all2_all_nthI [intro?]:
  1511   "length a = length b \<Longrightarrow> (\<And>n. n < length a \<Longrightarrow> P (a!n) (b!n)) \<Longrightarrow> list_all2 P a b"
  1512   by (simp add: list_all2_conv_all_nth)
  1513 
  1514 lemma list_all2I:
  1515   "\<forall>x \<in> set (zip a b). split P x \<Longrightarrow> length a = length b \<Longrightarrow> list_all2 P a b"
  1516   by (simp add: list_all2_def)
  1517 
  1518 lemma list_all2_nthD:
  1519   "\<lbrakk> list_all2 P xs ys; p < size xs \<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
  1520   by (simp add: list_all2_conv_all_nth)
  1521 
  1522 lemma list_all2_nthD2:
  1523   "\<lbrakk>list_all2 P xs ys; p < size ys\<rbrakk> \<Longrightarrow> P (xs!p) (ys!p)"
  1524   by (frule list_all2_lengthD) (auto intro: list_all2_nthD)
  1525 
  1526 lemma list_all2_map1: 
  1527   "list_all2 P (map f as) bs = list_all2 (\<lambda>x y. P (f x) y) as bs"
  1528   by (simp add: list_all2_conv_all_nth)
  1529 
  1530 lemma list_all2_map2: 
  1531   "list_all2 P as (map f bs) = list_all2 (\<lambda>x y. P x (f y)) as bs"
  1532   by (auto simp add: list_all2_conv_all_nth)
  1533 
  1534 lemma list_all2_refl [intro?]:
  1535   "(\<And>x. P x x) \<Longrightarrow> list_all2 P xs xs"
  1536   by (simp add: list_all2_conv_all_nth)
  1537 
  1538 lemma list_all2_update_cong:
  1539   "\<lbrakk> i<size xs; list_all2 P xs ys; P x y \<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
  1540   by (simp add: list_all2_conv_all_nth nth_list_update)
  1541 
  1542 lemma list_all2_update_cong2:
  1543   "\<lbrakk>list_all2 P xs ys; P x y; i < length ys\<rbrakk> \<Longrightarrow> list_all2 P (xs[i:=x]) (ys[i:=y])"
  1544   by (simp add: list_all2_lengthD list_all2_update_cong)
  1545 
  1546 lemma list_all2_takeI [simp,intro?]:
  1547   "\<And>n ys. list_all2 P xs ys \<Longrightarrow> list_all2 P (take n xs) (take n ys)"
  1548   apply (induct xs)
  1549    apply simp
  1550   apply (clarsimp simp add: list_all2_Cons1)
  1551   apply (case_tac n)
  1552   apply auto
  1553   done
  1554 
  1555 lemma list_all2_dropI [simp,intro?]:
  1556   "\<And>n bs. list_all2 P as bs \<Longrightarrow> list_all2 P (drop n as) (drop n bs)"
  1557   apply (induct as, simp)
  1558   apply (clarsimp simp add: list_all2_Cons1)
  1559   apply (case_tac n, simp, simp)
  1560   done
  1561 
  1562 lemma list_all2_mono [intro?]:
  1563   "\<And>y. list_all2 P x y \<Longrightarrow> (\<And>x y. P x y \<Longrightarrow> Q x y) \<Longrightarrow> list_all2 Q x y"
  1564   apply (induct x, simp)
  1565   apply (case_tac y, auto)
  1566   done
  1567 
  1568 
  1569 subsubsection {* @{text foldl} and @{text foldr} *}
  1570 
  1571 lemma foldl_append [simp]:
  1572 "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
  1573 by (induct xs) auto
  1574 
  1575 lemma foldr_append[simp]: "foldr f (xs @ ys) a = foldr f xs (foldr f ys a)"
  1576 by (induct xs) auto
  1577 
  1578 lemma foldl_cong [recdef_cong]:
  1579   "[| a = b; l = k; !!a x. x : set l ==> f a x = g a x |] 
  1580   ==> foldl f a l = foldl g b k"
  1581   by (induct k fixing: a b l, simp_all)
  1582 
  1583 lemma foldr_cong [recdef_cong]:
  1584   "[| a = b; l = k; !!a x. x : set l ==> f x a = g x a |] 
  1585   ==> foldr f l a = foldr g k b"
  1586   by (induct k fixing: a b l, simp_all)
  1587 
  1588 lemma foldr_foldl: "foldr f xs a = foldl (%x y. f y x) a (rev xs)"
  1589 by (induct xs) auto
  1590 
  1591 lemma foldl_foldr: "foldl f a xs = foldr (%x y. f y x) (rev xs) a"
  1592 by (simp add: foldr_foldl [of "%x y. f y x" "rev xs"])
  1593 
  1594 text {*
  1595 Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
  1596 difficult to use because it requires an additional transitivity step.
  1597 *}
  1598 
  1599 lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns"
  1600 by (induct ns) auto
  1601 
  1602 lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns"
  1603 by (force intro: start_le_sum simp add: in_set_conv_decomp)
  1604 
  1605 lemma sum_eq_0_conv [iff]:
  1606 "!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
  1607 by (induct ns) auto
  1608 
  1609 
  1610 subsubsection {* @{text upto} *}
  1611 
  1612 lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"
  1613 -- {* simp does not terminate! *}
  1614 by (induct j) auto
  1615 
  1616 lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
  1617 by (subst upt_rec) simp
  1618 
  1619 lemma upt_eq_Nil_conv[simp]: "([i..<j] = []) = (j = 0 \<or> j <= i)"
  1620 by(induct j)simp_all
  1621 
  1622 lemma upt_eq_Cons_conv:
  1623  "!!x xs. ([i..<j] = x#xs) = (i < j & i = x & [i+1..<j] = xs)"
  1624 apply(induct j)
  1625  apply simp
  1626 apply(clarsimp simp add: append_eq_Cons_conv)
  1627 apply arith
  1628 done
  1629 
  1630 lemma upt_Suc_append: "i <= j ==> [i..<(Suc j)] = [i..<j]@[j]"
  1631 -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
  1632 by simp
  1633 
  1634 lemma upt_conv_Cons: "i < j ==> [i..<j] = i # [Suc i..<j]"
  1635 apply(rule trans)
  1636 apply(subst upt_rec)
  1637  prefer 2 apply (rule refl, simp)
  1638 done
  1639 
  1640 lemma upt_add_eq_append: "i<=j ==> [i..<j+k] = [i..<j]@[j..<j+k]"
  1641 -- {* LOOPS as a simprule, since @{text "j <= j"}. *}
  1642 by (induct k) auto
  1643 
  1644 lemma length_upt [simp]: "length [i..<j] = j - i"
  1645 by (induct j) (auto simp add: Suc_diff_le)
  1646 
  1647 lemma nth_upt [simp]: "i + k < j ==> [i..<j] ! k = i + k"
  1648 apply (induct j)
  1649 apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
  1650 done
  1651 
  1652 
  1653 lemma hd_upt[simp]: "i < j \<Longrightarrow> hd[i..<j] = i"
  1654 by(simp add:upt_conv_Cons)
  1655 
  1656 lemma last_upt[simp]: "i < j \<Longrightarrow> last[i..<j] = j - 1"
  1657 apply(cases j)
  1658  apply simp
  1659 by(simp add:upt_Suc_append)
  1660 
  1661 lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..<n] = [i..<i+m]"
  1662 apply (induct m, simp)
  1663 apply (subst upt_rec)
  1664 apply (rule sym)
  1665 apply (subst upt_rec)
  1666 apply (simp del: upt.simps)
  1667 done
  1668 
  1669 lemma drop_upt[simp]: "drop m [i..<j] = [i+m..<j]"
  1670 apply(induct j)
  1671 apply auto
  1672 apply arith
  1673 done
  1674 
  1675 lemma map_Suc_upt: "map Suc [m..<n] = [Suc m..n]"
  1676 by (induct n) auto
  1677 
  1678 lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..<n]) ! i = f(m+i)"
  1679 apply (induct n m rule: diff_induct)
  1680 prefer 3 apply (subst map_Suc_upt[symmetric])
  1681 apply (auto simp add: less_diff_conv nth_upt)
  1682 done
  1683 
  1684 lemma nth_take_lemma:
  1685   "!!xs ys. k <= length xs ==> k <= length ys ==>
  1686      (!!i. i < k --> xs!i = ys!i) ==> take k xs = take k ys"
  1687 apply (atomize, induct k)
  1688 apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib, clarify)
  1689 txt {* Both lists must be non-empty *}
  1690 apply (case_tac xs, simp)
  1691 apply (case_tac ys, clarify)
  1692  apply (simp (no_asm_use))
  1693 apply clarify
  1694 txt {* prenexing's needed, not miniscoping *}
  1695 apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
  1696 apply blast
  1697 done
  1698 
  1699 lemma nth_equalityI:
  1700  "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
  1701 apply (frule nth_take_lemma [OF le_refl eq_imp_le])
  1702 apply (simp_all add: take_all)
  1703 done
  1704 
  1705 (* needs nth_equalityI *)
  1706 lemma list_all2_antisym:
  1707   "\<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> 
  1708   \<Longrightarrow> xs = ys"
  1709   apply (simp add: list_all2_conv_all_nth) 
  1710   apply (rule nth_equalityI, blast, simp)
  1711   done
  1712 
  1713 lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
  1714 -- {* The famous take-lemma. *}
  1715 apply (drule_tac x = "max (length xs) (length ys)" in spec)
  1716 apply (simp add: le_max_iff_disj take_all)
  1717 done
  1718 
  1719 
  1720 lemma take_Cons':
  1721      "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
  1722 by (cases n) simp_all
  1723 
  1724 lemma drop_Cons':
  1725      "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
  1726 by (cases n) simp_all
  1727 
  1728 lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
  1729 by (cases n) simp_all
  1730 
  1731 lemmas [simp] = take_Cons'[of "number_of v",standard]
  1732                 drop_Cons'[of "number_of v",standard]
  1733                 nth_Cons'[of _ _ "number_of v",standard]
  1734 
  1735 
  1736 subsubsection {* @{text "distinct"} and @{text remdups} *}
  1737 
  1738 lemma distinct_append [simp]:
  1739 "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
  1740 by (induct xs) auto
  1741 
  1742 lemma distinct_rev[simp]: "distinct(rev xs) = distinct xs"
  1743 by(induct xs) auto
  1744 
  1745 lemma set_remdups [simp]: "set (remdups xs) = set xs"
  1746 by (induct xs) (auto simp add: insert_absorb)
  1747 
  1748 lemma distinct_remdups [iff]: "distinct (remdups xs)"
  1749 by (induct xs) auto
  1750 
  1751 lemma remdups_eq_nil_iff [simp]: "(remdups x = []) = (x = [])"
  1752   by (induct x, auto) 
  1753 
  1754 lemma remdups_eq_nil_right_iff [simp]: "([] = remdups x) = (x = [])"
  1755   by (induct x, auto)
  1756 
  1757 lemma length_remdups_leq[iff]: "length(remdups xs) <= length xs"
  1758 by (induct xs) auto
  1759 
  1760 lemma length_remdups_eq[iff]:
  1761   "(length (remdups xs) = length xs) = (remdups xs = xs)"
  1762 apply(induct xs)
  1763  apply auto
  1764 apply(subgoal_tac "length (remdups xs) <= length xs")
  1765  apply arith
  1766 apply(rule length_remdups_leq)
  1767 done
  1768 
  1769 
  1770 lemma distinct_map:
  1771   "distinct(map f xs) = (distinct xs & inj_on f (set xs))"
  1772 by (induct xs) auto
  1773 
  1774 
  1775 lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
  1776 by (induct xs) auto
  1777 
  1778 lemma distinct_upt[simp]: "distinct[i..<j]"
  1779 by (induct j) auto
  1780 
  1781 lemma distinct_take[simp]: "\<And>i. distinct xs \<Longrightarrow> distinct (take i xs)"
  1782 apply(induct xs)
  1783  apply simp
  1784 apply (case_tac i)
  1785  apply simp_all
  1786 apply(blast dest:in_set_takeD)
  1787 done
  1788 
  1789 lemma distinct_drop[simp]: "\<And>i. distinct xs \<Longrightarrow> distinct (drop i xs)"
  1790 apply(induct xs)
  1791  apply simp
  1792 apply (case_tac i)
  1793  apply simp_all
  1794 done
  1795 
  1796 lemma distinct_list_update:
  1797 assumes d: "distinct xs" and a: "a \<notin> set xs - {xs!i}"
  1798 shows "distinct (xs[i:=a])"
  1799 proof (cases "i < length xs")
  1800   case True
  1801   with a have "a \<notin> set (take i xs @ xs ! i # drop (Suc i) xs) - {xs!i}"
  1802     apply (drule_tac id_take_nth_drop) by simp
  1803   with d True show ?thesis
  1804     apply (simp add: upd_conv_take_nth_drop)
  1805     apply (drule subst [OF id_take_nth_drop]) apply assumption
  1806     apply simp apply (cases "a = xs!i") apply simp by blast
  1807 next
  1808   case False with d show ?thesis by auto
  1809 qed
  1810 
  1811 
  1812 text {* It is best to avoid this indexed version of distinct, but
  1813 sometimes it is useful. *}
  1814 
  1815 lemma distinct_conv_nth:
  1816 "distinct xs = (\<forall>i < size xs. \<forall>j < size xs. i \<noteq> j --> xs!i \<noteq> xs!j)"
  1817 apply (induct xs, simp, simp)
  1818 apply (rule iffI, clarsimp)
  1819  apply (case_tac i)
  1820 apply (case_tac j, simp)
  1821 apply (simp add: set_conv_nth)
  1822  apply (case_tac j)
  1823 apply (clarsimp simp add: set_conv_nth, simp)
  1824 apply (rule conjI)
  1825  apply (clarsimp simp add: set_conv_nth)
  1826  apply (erule_tac x = 0 in allE, simp)
  1827  apply (erule_tac x = "Suc i" in allE, simp, clarsimp)
  1828 apply (erule_tac x = "Suc i" in allE, simp)
  1829 apply (erule_tac x = "Suc j" in allE, simp)
  1830 done
  1831 
  1832 lemma nth_eq_iff_index_eq:
  1833  "\<lbrakk> distinct xs; i < length xs; j < length xs \<rbrakk> \<Longrightarrow> (xs!i = xs!j) = (i = j)"
  1834 by(auto simp: distinct_conv_nth)
  1835 
  1836 lemma distinct_card: "distinct xs ==> card (set xs) = size xs"
  1837   by (induct xs) auto
  1838 
  1839 lemma card_distinct: "card (set xs) = size xs ==> distinct xs"
  1840 proof (induct xs)
  1841   case Nil thus ?case by simp
  1842 next
  1843   case (Cons x xs)
  1844   show ?case
  1845   proof (cases "x \<in> set xs")
  1846     case False with Cons show ?thesis by simp
  1847   next
  1848     case True with Cons.prems
  1849     have "card (set xs) = Suc (length xs)" 
  1850       by (simp add: card_insert_if split: split_if_asm)
  1851     moreover have "card (set xs) \<le> length xs" by (rule card_length)
  1852     ultimately have False by simp
  1853     thus ?thesis ..
  1854   qed
  1855 qed
  1856 
  1857 
  1858 lemma length_remdups_concat:
  1859  "length(remdups(concat xss)) = card(\<Union>xs \<in> set xss. set xs)"
  1860 by(simp add: distinct_card[symmetric])
  1861 
  1862 
  1863 subsubsection {* @{text remove1} *}
  1864 
  1865 lemma remove1_append:
  1866   "remove1 x (xs @ ys) =
  1867   (if x \<in> set xs then remove1 x xs @ ys else xs @ remove1 x ys)"
  1868 by (induct xs) auto
  1869 
  1870 lemma set_remove1_subset: "set(remove1 x xs) <= set xs"
  1871 apply(induct xs)
  1872  apply simp
  1873 apply simp
  1874 apply blast
  1875 done
  1876 
  1877 lemma set_remove1_eq [simp]: "distinct xs ==> set(remove1 x xs) = set xs - {x}"
  1878 apply(induct xs)
  1879  apply simp
  1880 apply simp
  1881 apply blast
  1882 done
  1883 
  1884 lemma remove1_filter_not[simp]:
  1885   "\<not> P x \<Longrightarrow> remove1 x (filter P xs) = filter P xs"
  1886 by(induct xs) auto
  1887 
  1888 lemma notin_set_remove1[simp]: "x ~: set xs ==> x ~: set(remove1 y xs)"
  1889 apply(insert set_remove1_subset)
  1890 apply fast
  1891 done
  1892 
  1893 lemma distinct_remove1[simp]: "distinct xs ==> distinct(remove1 x xs)"
  1894 by (induct xs) simp_all
  1895 
  1896 
  1897 subsubsection {* @{text replicate} *}
  1898 
  1899 lemma length_replicate [simp]: "length (replicate n x) = n"
  1900 by (induct n) auto
  1901 
  1902 lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
  1903 by (induct n) auto
  1904 
  1905 lemma replicate_app_Cons_same:
  1906 "(replicate n x) @ (x # xs) = x # replicate n x @ xs"
  1907 by (induct n) auto
  1908 
  1909 lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
  1910 apply (induct n, simp)
  1911 apply (simp add: replicate_app_Cons_same)
  1912 done
  1913 
  1914 lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
  1915 by (induct n) auto
  1916 
  1917 text{* Courtesy of Matthias Daum: *}
  1918 lemma append_replicate_commute:
  1919   "replicate n x @ replicate k x = replicate k x @ replicate n x"
  1920 apply (simp add: replicate_add [THEN sym])
  1921 apply (simp add: add_commute)
  1922 done
  1923 
  1924 lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
  1925 by (induct n) auto
  1926 
  1927 lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
  1928 by (induct n) auto
  1929 
  1930 lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
  1931 by (atomize (full), induct n) auto
  1932 
  1933 lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x"
  1934 apply (induct n, simp)
  1935 apply (simp add: nth_Cons split: nat.split)
  1936 done
  1937 
  1938 text{* Courtesy of Matthias Daum (2 lemmas): *}
  1939 lemma take_replicate[simp]: "take i (replicate k x) = replicate (min i k) x"
  1940 apply (case_tac "k \<le> i")
  1941  apply  (simp add: min_def)
  1942 apply (drule not_leE)
  1943 apply (simp add: min_def)
  1944 apply (subgoal_tac "replicate k x = replicate i x @ replicate (k - i) x")
  1945  apply  simp
  1946 apply (simp add: replicate_add [symmetric])
  1947 done
  1948 
  1949 lemma drop_replicate[simp]: "!!i. drop i (replicate k x) = replicate (k-i) x"
  1950 apply (induct k)
  1951  apply simp
  1952 apply clarsimp
  1953 apply (case_tac i)
  1954  apply simp
  1955 apply clarsimp
  1956 done
  1957 
  1958 
  1959 lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
  1960 by (induct n) auto
  1961 
  1962 lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
  1963 by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
  1964 
  1965 lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
  1966 by auto
  1967 
  1968 lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
  1969 by (simp add: set_replicate_conv_if split: split_if_asm)
  1970 
  1971 
  1972 subsubsection{*@{text rotate1} and @{text rotate}*}
  1973 
  1974 lemma rotate_simps[simp]: "rotate1 [] = [] \<and> rotate1 (x#xs) = xs @ [x]"
  1975 by(simp add:rotate1_def)
  1976 
  1977 lemma rotate0[simp]: "rotate 0 = id"
  1978 by(simp add:rotate_def)
  1979 
  1980 lemma rotate_Suc[simp]: "rotate (Suc n) xs = rotate1(rotate n xs)"
  1981 by(simp add:rotate_def)
  1982 
  1983 lemma rotate_add:
  1984   "rotate (m+n) = rotate m o rotate n"
  1985 by(simp add:rotate_def funpow_add)
  1986 
  1987 lemma rotate_rotate: "rotate m (rotate n xs) = rotate (m+n) xs"
  1988 by(simp add:rotate_add)
  1989 
  1990 lemma rotate1_rotate_swap: "rotate1 (rotate n xs) = rotate n (rotate1 xs)"
  1991 by(simp add:rotate_def funpow_swap1)
  1992 
  1993 lemma rotate1_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate1 xs = xs"
  1994 by(cases xs) simp_all
  1995 
  1996 lemma rotate_length01[simp]: "length xs <= 1 \<Longrightarrow> rotate n xs = xs"
  1997 apply(induct n)
  1998  apply simp
  1999 apply (simp add:rotate_def)
  2000 done
  2001 
  2002 lemma rotate1_hd_tl: "xs \<noteq> [] \<Longrightarrow> rotate1 xs = tl xs @ [hd xs]"
  2003 by(simp add:rotate1_def split:list.split)
  2004 
  2005 lemma rotate_drop_take:
  2006   "rotate n xs = drop (n mod length xs) xs @ take (n mod length xs) xs"
  2007 apply(induct n)
  2008  apply simp
  2009 apply(simp add:rotate_def)
  2010 apply(cases "xs = []")
  2011  apply (simp)
  2012 apply(case_tac "n mod length xs = 0")
  2013  apply(simp add:mod_Suc)
  2014  apply(simp add: rotate1_hd_tl drop_Suc take_Suc)
  2015 apply(simp add:mod_Suc rotate1_hd_tl drop_Suc[symmetric] drop_tl[symmetric]
  2016                 take_hd_drop linorder_not_le)
  2017 done
  2018 
  2019 lemma rotate_conv_mod: "rotate n xs = rotate (n mod length xs) xs"
  2020 by(simp add:rotate_drop_take)
  2021 
  2022 lemma rotate_id[simp]: "n mod length xs = 0 \<Longrightarrow> rotate n xs = xs"
  2023 by(simp add:rotate_drop_take)
  2024 
  2025 lemma length_rotate1[simp]: "length(rotate1 xs) = length xs"
  2026 by(simp add:rotate1_def split:list.split)
  2027 
  2028 lemma length_rotate[simp]: "!!xs. length(rotate n xs) = length xs"
  2029 by (induct n) (simp_all add:rotate_def)
  2030 
  2031 lemma distinct1_rotate[simp]: "distinct(rotate1 xs) = distinct xs"
  2032 by(simp add:rotate1_def split:list.split) blast
  2033 
  2034 lemma distinct_rotate[simp]: "distinct(rotate n xs) = distinct xs"
  2035 by (induct n) (simp_all add:rotate_def)
  2036 
  2037 lemma rotate_map: "rotate n (map f xs) = map f (rotate n xs)"
  2038 by(simp add:rotate_drop_take take_map drop_map)
  2039 
  2040 lemma set_rotate1[simp]: "set(rotate1 xs) = set xs"
  2041 by(simp add:rotate1_def split:list.split)
  2042 
  2043 lemma set_rotate[simp]: "set(rotate n xs) = set xs"
  2044 by (induct n) (simp_all add:rotate_def)
  2045 
  2046 lemma rotate1_is_Nil_conv[simp]: "(rotate1 xs = []) = (xs = [])"
  2047 by(simp add:rotate1_def split:list.split)
  2048 
  2049 lemma rotate_is_Nil_conv[simp]: "(rotate n xs = []) = (xs = [])"
  2050 by (induct n) (simp_all add:rotate_def)
  2051 
  2052 lemma rotate_rev:
  2053   "rotate n (rev xs) = rev(rotate (length xs - (n mod length xs)) xs)"
  2054 apply(simp add:rotate_drop_take rev_drop rev_take)
  2055 apply(cases "length xs = 0")
  2056  apply simp
  2057 apply(cases "n mod length xs = 0")
  2058  apply simp
  2059 apply(simp add:rotate_drop_take rev_drop rev_take)
  2060 done
  2061 
  2062 lemma hd_rotate_conv_nth: "xs \<noteq> [] \<Longrightarrow> hd(rotate n xs) = xs!(n mod length xs)"
  2063 apply(simp add:rotate_drop_take hd_append hd_drop_conv_nth hd_conv_nth)
  2064 apply(subgoal_tac "length xs \<noteq> 0")
  2065  prefer 2 apply simp
  2066 using mod_less_divisor[of "length xs" n] by arith
  2067 
  2068 
  2069 subsubsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
  2070 
  2071 lemma sublist_empty [simp]: "sublist xs {} = []"
  2072 by (auto simp add: sublist_def)
  2073 
  2074 lemma sublist_nil [simp]: "sublist [] A = []"
  2075 by (auto simp add: sublist_def)
  2076 
  2077 lemma length_sublist:
  2078   "length(sublist xs I) = card{i. i < length xs \<and> i : I}"
  2079 by(simp add: sublist_def length_filter_conv_card cong:conj_cong)
  2080 
  2081 lemma sublist_shift_lemma_Suc:
  2082   "!!is. map fst (filter (%p. P(Suc(snd p))) (zip xs is)) =
  2083          map fst (filter (%p. P(snd p)) (zip xs (map Suc is)))"
  2084 apply(induct xs)
  2085  apply simp
  2086 apply (case_tac "is")
  2087  apply simp
  2088 apply simp
  2089 done
  2090 
  2091 lemma sublist_shift_lemma:
  2092      "map fst [p:zip xs [i..<i + length xs] . snd p : A] =
  2093       map fst [p:zip xs [0..<length xs] . snd p + i : A]"
  2094 by (induct xs rule: rev_induct) (simp_all add: add_commute)
  2095 
  2096 lemma sublist_append:
  2097      "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
  2098 apply (unfold sublist_def)
  2099 apply (induct l' rule: rev_induct, simp)
  2100 apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
  2101 apply (simp add: add_commute)
  2102 done
  2103 
  2104 lemma sublist_Cons:
  2105 "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
  2106 apply (induct l rule: rev_induct)
  2107  apply (simp add: sublist_def)
  2108 apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
  2109 done
  2110 
  2111 lemma set_sublist: "!!I. set(sublist xs I) = {xs!i|i. i<size xs \<and> i \<in> I}"
  2112 apply(induct xs)
  2113  apply simp
  2114 apply(auto simp add:sublist_Cons nth_Cons split:nat.split elim: lessE)
  2115  apply(erule lessE)
  2116   apply auto
  2117 apply(erule lessE)
  2118 apply auto
  2119 done
  2120 
  2121 lemma set_sublist_subset: "set(sublist xs I) \<subseteq> set xs"
  2122 by(auto simp add:set_sublist)
  2123 
  2124 lemma notin_set_sublistI[simp]: "x \<notin> set xs \<Longrightarrow> x \<notin> set(sublist xs I)"
  2125 by(auto simp add:set_sublist)
  2126 
  2127 lemma in_set_sublistD: "x \<in> set(sublist xs I) \<Longrightarrow> x \<in> set xs"
  2128 by(auto simp add:set_sublist)
  2129 
  2130 lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
  2131 by (simp add: sublist_Cons)
  2132 
  2133 
  2134 lemma distinct_sublistI[simp]: "!!I. distinct xs \<Longrightarrow> distinct(sublist xs I)"
  2135 apply(induct xs)
  2136  apply simp
  2137 apply(auto simp add:sublist_Cons)
  2138 done
  2139 
  2140 
  2141 lemma sublist_upt_eq_take [simp]: "sublist l {..<n} = take n l"
  2142 apply (induct l rule: rev_induct, simp)
  2143 apply (simp split: nat_diff_split add: sublist_append)
  2144 done
  2145 
  2146 lemma filter_in_sublist: "\<And>s. distinct xs \<Longrightarrow>
  2147   filter (%x. x \<in> set(sublist xs s)) xs = sublist xs s"
  2148 proof (induct xs)
  2149   case Nil thus ?case by simp
  2150 next
  2151   case (Cons a xs)
  2152   moreover hence "!x. x: set xs \<longrightarrow> x \<noteq> a" by auto
  2153   ultimately show ?case by(simp add: sublist_Cons cong:filter_cong)
  2154 qed
  2155 
  2156 
  2157 subsubsection{*Sets of Lists*}
  2158 
  2159 subsubsection {* @{text lists}: the list-forming operator over sets *}
  2160 
  2161 consts lists :: "'a set => 'a list set"
  2162 inductive "lists A"
  2163  intros
  2164   Nil [intro!]: "[]: lists A"
  2165   Cons [intro!]: "[| a: A;l: lists A|] ==> a#l : lists A"
  2166 
  2167 inductive_cases listsE [elim!]: "x#l : lists A"
  2168 
  2169 lemma lists_mono [mono]: "A \<subseteq> B ==> lists A \<subseteq> lists B"
  2170 by (unfold lists.defs) (blast intro!: lfp_mono)
  2171 
  2172 lemma lists_IntI:
  2173   assumes l: "l: lists A" shows "l: lists B ==> l: lists (A Int B)" using l
  2174   by induct blast+
  2175 
  2176 lemma lists_Int_eq [simp]: "lists (A \<inter> B) = lists A \<inter> lists B"
  2177 proof (rule mono_Int [THEN equalityI])
  2178   show "mono lists" by (simp add: mono_def lists_mono)
  2179   show "lists A \<inter> lists B \<subseteq> lists (A \<inter> B)" by (blast intro: lists_IntI)
  2180 qed
  2181 
  2182 lemma append_in_lists_conv [iff]:
  2183      "(xs @ ys : lists A) = (xs : lists A \<and> ys : lists A)"
  2184 by (induct xs) auto
  2185 
  2186 lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)"
  2187 -- {* eliminate @{text lists} in favour of @{text set} *}
  2188 by (induct xs) auto
  2189 
  2190 lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A"
  2191 by (rule in_lists_conv_set [THEN iffD1])
  2192 
  2193 lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A"
  2194 by (rule in_lists_conv_set [THEN iffD2])
  2195 
  2196 lemma lists_UNIV [simp]: "lists UNIV = UNIV"
  2197 by auto
  2198 
  2199 subsubsection {* For efficiency *}
  2200 
  2201 text{* Only use @{text mem} for generating executable code.  Otherwise
  2202 use @{prop"x : set xs"} instead --- it is much easier to reason about.
  2203 The same is true for @{const list_all} and @{const list_ex}: write
  2204 @{text"\<forall>x\<in>set xs"} and @{text"\<exists>x\<in>set xs"} instead because the HOL
  2205 quantifiers are aleady known to the automatic provers. In fact, the declarations in the Code subsection make sure that @{text"\<in>"}, @{text"\<forall>x\<in>set xs"}
  2206 and @{text"\<exists>x\<in>set xs"} are implemented efficiently.
  2207 
  2208 The functions @{const itrev}, @{const filtermap} and @{const
  2209 map_filter} are just there to generate efficient code. Do not use them
  2210 for modelling and proving. *}
  2211 
  2212 lemma mem_iff: "(x mem xs) = (x : set xs)"
  2213 by (induct xs) auto
  2214 
  2215 lemma list_inter_conv: "set(list_inter xs ys) = set xs \<inter> set ys"
  2216 by (induct xs) auto
  2217 
  2218 lemma list_all_iff: "list_all P xs = (\<forall>x \<in> set xs. P x)"
  2219 by (induct xs) auto
  2220 
  2221 lemma list_all_append [simp]:
  2222 "list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)"
  2223 by (induct xs) auto
  2224 
  2225 lemma list_all_rev [simp]: "list_all P (rev xs) = list_all P xs"
  2226 by (simp add: list_all_iff)
  2227 
  2228 lemma list_ex_iff: "list_ex P xs = (\<exists>x \<in> set xs. P x)"
  2229 by (induct xs) simp_all
  2230 
  2231 lemma itrev[simp]: "ALL ys. itrev xs ys = rev xs @ ys"
  2232 by (induct xs) simp_all
  2233 
  2234 lemma filtermap_conv:
  2235      "filtermap f xs = map (%x. the(f x)) (filter (%x. f x \<noteq> None) xs)"
  2236   by (induct xs) (simp_all split: option.split) 
  2237 
  2238 lemma map_filter_conv[simp]: "map_filter f P xs = map f (filter P xs)"
  2239 by (induct xs) auto
  2240 
  2241 
  2242 subsubsection {* Code generation *}
  2243 
  2244 text{* Defaults for generating efficient code for some standard functions. *}
  2245 
  2246 lemmas in_set_code[code unfold] = mem_iff[symmetric, THEN eq_reflection]
  2247 
  2248 lemma rev_code[code unfold]: "rev xs == itrev xs []"
  2249 by simp
  2250 
  2251 lemma distinct_Cons_mem[code]: "distinct (x#xs) = (~(x mem xs) \<and> distinct xs)"
  2252 by (simp add:mem_iff)
  2253 
  2254 lemma remdups_Cons_mem[code]:
  2255  "remdups (x#xs) = (if x mem xs then remdups xs else x # remdups xs)"
  2256 by (simp add:mem_iff)
  2257 
  2258 lemma list_inter_Cons_mem[code]:  "list_inter (a#as) bs =
  2259   (if a mem bs then a#(list_inter as bs) else list_inter as bs)"
  2260 by(simp add:mem_iff)
  2261 
  2262 text{* For implementing bounded quantifiers over lists by
  2263 @{const list_ex}/@{const list_all}: *}
  2264 
  2265 lemmas list_bex_code[code unfold] = list_ex_iff[symmetric, THEN eq_reflection]
  2266 lemmas list_ball_code[code unfold] = list_all_iff[symmetric, THEN eq_reflection]
  2267 
  2268 
  2269 subsubsection{* Inductive definition for membership *}
  2270 
  2271 consts ListMem :: "('a \<times> 'a list)set"
  2272 inductive ListMem
  2273 intros
  2274  elem:  "(x,x#xs) \<in> ListMem"
  2275  insert:  "(x,xs) \<in> ListMem \<Longrightarrow> (x,y#xs) \<in> ListMem"
  2276 
  2277 lemma ListMem_iff: "((x,xs) \<in> ListMem) = (x \<in> set xs)"
  2278 apply (rule iffI)
  2279  apply (induct set: ListMem)
  2280   apply auto
  2281 apply (induct xs)
  2282  apply (auto intro: ListMem.intros)
  2283 done
  2284 
  2285 
  2286 
  2287 subsubsection{*Lists as Cartesian products*}
  2288 
  2289 text{*@{text"set_Cons A Xs"}: the set of lists with head drawn from
  2290 @{term A} and tail drawn from @{term Xs}.*}
  2291 
  2292 constdefs
  2293   set_Cons :: "'a set \<Rightarrow> 'a list set \<Rightarrow> 'a list set"
  2294   "set_Cons A XS == {z. \<exists>x xs. z = x#xs & x \<in> A & xs \<in> XS}"
  2295 
  2296 lemma set_Cons_sing_Nil [simp]: "set_Cons A {[]} = (%x. [x])`A"
  2297 by (auto simp add: set_Cons_def)
  2298 
  2299 text{*Yields the set of lists, all of the same length as the argument and
  2300 with elements drawn from the corresponding element of the argument.*}
  2301 
  2302 consts  listset :: "'a set list \<Rightarrow> 'a list set"
  2303 primrec
  2304    "listset []    = {[]}"
  2305    "listset(A#As) = set_Cons A (listset As)"
  2306 
  2307 
  2308 subsection{*Relations on Lists*}
  2309 
  2310 subsubsection {* Length Lexicographic Ordering *}
  2311 
  2312 text{*These orderings preserve well-foundedness: shorter lists 
  2313   precede longer lists. These ordering are not used in dictionaries.*}
  2314 
  2315 consts lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
  2316         --{*The lexicographic ordering for lists of the specified length*}
  2317 primrec
  2318   "lexn r 0 = {}"
  2319   "lexn r (Suc n) =
  2320     (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
  2321     {(xs,ys). length xs = Suc n \<and> length ys = Suc n}"
  2322 
  2323 constdefs
  2324   lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
  2325     "lex r == \<Union>n. lexn r n"
  2326         --{*Holds only between lists of the same length*}
  2327 
  2328   lenlex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
  2329     "lenlex r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
  2330         --{*Compares lists by their length and then lexicographically*}
  2331 
  2332 
  2333 lemma wf_lexn: "wf r ==> wf (lexn r n)"
  2334 apply (induct n, simp, simp)
  2335 apply(rule wf_subset)
  2336  prefer 2 apply (rule Int_lower1)
  2337 apply(rule wf_prod_fun_image)
  2338  prefer 2 apply (rule inj_onI, auto)
  2339 done
  2340 
  2341 lemma lexn_length:
  2342      "!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
  2343 by (induct n) auto
  2344 
  2345 lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
  2346 apply (unfold lex_def)
  2347 apply (rule wf_UN)
  2348 apply (blast intro: wf_lexn, clarify)
  2349 apply (rename_tac m n)
  2350 apply (subgoal_tac "m \<noteq> n")
  2351  prefer 2 apply blast
  2352 apply (blast dest: lexn_length not_sym)
  2353 done
  2354 
  2355 lemma lexn_conv:
  2356   "lexn r n =
  2357     {(xs,ys). length xs = n \<and> length ys = n \<and>
  2358     (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
  2359 apply (induct n, simp)
  2360 apply (simp add: image_Collect lex_prod_def, safe, blast)
  2361  apply (rule_tac x = "ab # xys" in exI, simp)
  2362 apply (case_tac xys, simp_all, blast)
  2363 done
  2364 
  2365 lemma lex_conv:
  2366   "lex r =
  2367     {(xs,ys). length xs = length ys \<and>
  2368     (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
  2369 by (force simp add: lex_def lexn_conv)
  2370 
  2371 lemma wf_lenlex [intro!]: "wf r ==> wf (lenlex r)"
  2372 by (unfold lenlex_def) blast
  2373 
  2374 lemma lenlex_conv:
  2375     "lenlex r = {(xs,ys). length xs < length ys |
  2376                  length xs = length ys \<and> (xs, ys) : lex r}"
  2377 by (simp add: lenlex_def diag_def lex_prod_def measure_def inv_image_def)
  2378 
  2379 lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
  2380 by (simp add: lex_conv)
  2381 
  2382 lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
  2383 by (simp add:lex_conv)
  2384 
  2385 lemma Cons_in_lex [simp]:
  2386     "((x # xs, y # ys) : lex r) =
  2387       ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
  2388 apply (simp add: lex_conv)
  2389 apply (rule iffI)
  2390  prefer 2 apply (blast intro: Cons_eq_appendI, clarify)
  2391 apply (case_tac xys, simp, simp)
  2392 apply blast
  2393 done
  2394 
  2395 
  2396 subsubsection {* Lexicographic Ordering *}
  2397 
  2398 text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
  2399     This ordering does \emph{not} preserve well-foundedness.
  2400      Author: N. Voelker, March 2005. *} 
  2401 
  2402 constdefs 
  2403   lexord :: "('a * 'a)set \<Rightarrow> ('a list * 'a list) set" 
  2404   "lexord  r == {(x,y). \<exists> a v. y = x @ a # v \<or> 
  2405             (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}"
  2406 
  2407 lemma lexord_Nil_left[simp]:  "([],y) \<in> lexord r = (\<exists> a x. y = a # x)"
  2408   by (unfold lexord_def, induct_tac y, auto) 
  2409 
  2410 lemma lexord_Nil_right[simp]: "(x,[]) \<notin> lexord r"
  2411   by (unfold lexord_def, induct_tac x, auto)
  2412 
  2413 lemma lexord_cons_cons[simp]:
  2414      "((a # x, b # y) \<in> lexord r) = ((a,b)\<in> r | (a = b & (x,y)\<in> lexord r))"
  2415   apply (unfold lexord_def, safe, simp_all)
  2416   apply (case_tac u, simp, simp)
  2417   apply (case_tac u, simp, clarsimp, blast, blast, clarsimp)
  2418   apply (erule_tac x="b # u" in allE)
  2419   by force
  2420 
  2421 lemmas lexord_simps = lexord_Nil_left lexord_Nil_right lexord_cons_cons
  2422 
  2423 lemma lexord_append_rightI: "\<exists> b z. y = b # z \<Longrightarrow> (x, x @ y) \<in> lexord r"
  2424   by (induct_tac x, auto)  
  2425 
  2426 lemma lexord_append_left_rightI:
  2427      "(a,b) \<in> r \<Longrightarrow> (u @ a # x, u @ b # y) \<in> lexord r"
  2428   by (induct_tac u, auto)
  2429 
  2430 lemma lexord_append_leftI: " (u,v) \<in> lexord r \<Longrightarrow> (x @ u, x @ v) \<in> lexord r"
  2431   by (induct x, auto)
  2432 
  2433 lemma lexord_append_leftD:
  2434      "\<lbrakk> (x @ u, x @ v) \<in> lexord r; (! a. (a,a) \<notin> r) \<rbrakk> \<Longrightarrow> (u,v) \<in> lexord r"
  2435   by (erule rev_mp, induct_tac x, auto)
  2436 
  2437 lemma lexord_take_index_conv: 
  2438    "((x,y) : lexord r) = 
  2439     ((length x < length y \<and> take (length x) y = x) \<or> 
  2440      (\<exists>i. i < min(length x)(length y) & take i x = take i y & (x!i,y!i) \<in> r))"
  2441   apply (unfold lexord_def Let_def, clarsimp) 
  2442   apply (rule_tac f = "(% a b. a \<or> b)" in arg_cong2)
  2443   apply auto 
  2444   apply (rule_tac x="hd (drop (length x) y)" in exI)
  2445   apply (rule_tac x="tl (drop (length x) y)" in exI)
  2446   apply (erule subst, simp add: min_def) 
  2447   apply (rule_tac x ="length u" in exI, simp) 
  2448   apply (rule_tac x ="take i x" in exI) 
  2449   apply (rule_tac x ="x ! i" in exI) 
  2450   apply (rule_tac x ="y ! i" in exI, safe) 
  2451   apply (rule_tac x="drop (Suc i) x" in exI)
  2452   apply (drule sym, simp add: drop_Suc_conv_tl) 
  2453   apply (rule_tac x="drop (Suc i) y" in exI)
  2454   by (simp add: drop_Suc_conv_tl) 
  2455 
  2456 -- {* lexord is extension of partial ordering List.lex *} 
  2457 lemma lexord_lex: " (x,y) \<in> lex r = ((x,y) \<in> lexord r \<and> length x = length y)"
  2458   apply (rule_tac x = y in spec) 
  2459   apply (induct_tac x, clarsimp) 
  2460   by (clarify, case_tac x, simp, force)
  2461 
  2462 lemma lexord_irreflexive: "(! x. (x,x) \<notin> r) \<Longrightarrow> (y,y) \<notin> lexord r"
  2463   by (induct y, auto)
  2464 
  2465 lemma lexord_trans: 
  2466     "\<lbrakk> (x, y) \<in> lexord r; (y, z) \<in> lexord r; trans r \<rbrakk> \<Longrightarrow> (x, z) \<in> lexord r"
  2467    apply (erule rev_mp)+
  2468    apply (rule_tac x = x in spec) 
  2469   apply (rule_tac x = z in spec) 
  2470   apply ( induct_tac y, simp, clarify)
  2471   apply (case_tac xa, erule ssubst) 
  2472   apply (erule allE, erule allE) -- {* avoid simp recursion *} 
  2473   apply (case_tac x, simp, simp) 
  2474   apply (case_tac x, erule allE, erule allE, simp) 
  2475   apply (erule_tac x = listb in allE) 
  2476   apply (erule_tac x = lista in allE, simp)
  2477   apply (unfold trans_def)
  2478   by blast
  2479 
  2480 lemma lexord_transI:  "trans r \<Longrightarrow> trans (lexord r)"
  2481   by (rule transI, drule lexord_trans, blast) 
  2482 
  2483 lemma lexord_linear: "(! a b. (a,b)\<in> r | a = b | (b,a) \<in> r) \<Longrightarrow> (x,y) : lexord r | x = y | (y,x) : lexord r"
  2484   apply (rule_tac x = y in spec) 
  2485   apply (induct_tac x, rule allI) 
  2486   apply (case_tac x, simp, simp) 
  2487   apply (rule allI, case_tac x, simp, simp) 
  2488   by blast
  2489 
  2490 
  2491 subsubsection{*Lifting a Relation on List Elements to the Lists*}
  2492 
  2493 consts  listrel :: "('a * 'a)set => ('a list * 'a list)set"
  2494 
  2495 inductive "listrel(r)"
  2496  intros
  2497    Nil:  "([],[]) \<in> listrel r"
  2498    Cons: "[| (x,y) \<in> r; (xs,ys) \<in> listrel r |] ==> (x#xs, y#ys) \<in> listrel r"
  2499 
  2500 inductive_cases listrel_Nil1 [elim!]: "([],xs) \<in> listrel r"
  2501 inductive_cases listrel_Nil2 [elim!]: "(xs,[]) \<in> listrel r"
  2502 inductive_cases listrel_Cons1 [elim!]: "(y#ys,xs) \<in> listrel r"
  2503 inductive_cases listrel_Cons2 [elim!]: "(xs,y#ys) \<in> listrel r"
  2504 
  2505 
  2506 lemma listrel_mono: "r \<subseteq> s \<Longrightarrow> listrel r \<subseteq> listrel s"
  2507 apply clarify  
  2508 apply (erule listrel.induct)
  2509 apply (blast intro: listrel.intros)+
  2510 done
  2511 
  2512 lemma listrel_subset: "r \<subseteq> A \<times> A \<Longrightarrow> listrel r \<subseteq> lists A \<times> lists A"
  2513 apply clarify 
  2514 apply (erule listrel.induct, auto) 
  2515 done
  2516 
  2517 lemma listrel_refl: "refl A r \<Longrightarrow> refl (lists A) (listrel r)" 
  2518 apply (simp add: refl_def listrel_subset Ball_def)
  2519 apply (rule allI) 
  2520 apply (induct_tac x) 
  2521 apply (auto intro: listrel.intros)
  2522 done
  2523 
  2524 lemma listrel_sym: "sym r \<Longrightarrow> sym (listrel r)" 
  2525 apply (auto simp add: sym_def)
  2526 apply (erule listrel.induct) 
  2527 apply (blast intro: listrel.intros)+
  2528 done
  2529 
  2530 lemma listrel_trans: "trans r \<Longrightarrow> trans (listrel r)" 
  2531 apply (simp add: trans_def)
  2532 apply (intro allI) 
  2533 apply (rule impI) 
  2534 apply (erule listrel.induct) 
  2535 apply (blast intro: listrel.intros)+
  2536 done
  2537 
  2538 theorem equiv_listrel: "equiv A r \<Longrightarrow> equiv (lists A) (listrel r)"
  2539 by (simp add: equiv_def listrel_refl listrel_sym listrel_trans) 
  2540 
  2541 lemma listrel_Nil [simp]: "listrel r `` {[]} = {[]}"
  2542 by (blast intro: listrel.intros)
  2543 
  2544 lemma listrel_Cons:
  2545      "listrel r `` {x#xs} = set_Cons (r``{x}) (listrel r `` {xs})";
  2546 by (auto simp add: set_Cons_def intro: listrel.intros) 
  2547 
  2548 
  2549 subsection{*Miscellany*}
  2550 
  2551 subsubsection {* Characters and strings *}
  2552 
  2553 datatype nibble =
  2554     Nibble0 | Nibble1 | Nibble2 | Nibble3 | Nibble4 | Nibble5 | Nibble6 | Nibble7
  2555   | Nibble8 | Nibble9 | NibbleA | NibbleB | NibbleC | NibbleD | NibbleE | NibbleF
  2556 
  2557 datatype char = Char nibble nibble
  2558   -- "Note: canonical order of character encoding coincides with standard term ordering"
  2559 
  2560 types string = "char list"
  2561 
  2562 syntax
  2563   "_Char" :: "xstr => char"    ("CHR _")
  2564   "_String" :: "xstr => string"    ("_")
  2565 
  2566 parse_ast_translation {*
  2567   let
  2568     val constants = Syntax.Appl o map Syntax.Constant;
  2569 
  2570     fun mk_nib n = "Nibble" ^ chr (n + (if n <= 9 then ord "0" else ord "A" - 10));
  2571     fun mk_char c =
  2572       if Symbol.is_ascii c andalso Symbol.is_printable c then
  2573         constants ["Char", mk_nib (ord c div 16), mk_nib (ord c mod 16)]
  2574       else error ("Printable ASCII character expected: " ^ quote c);
  2575 
  2576     fun mk_string [] = Syntax.Constant "Nil"
  2577       | mk_string (c :: cs) = Syntax.Appl [Syntax.Constant "Cons", mk_char c, mk_string cs];
  2578 
  2579     fun char_ast_tr [Syntax.Variable xstr] =
  2580         (case Syntax.explode_xstr xstr of
  2581           [c] => mk_char c
  2582         | _ => error ("Single character expected: " ^ xstr))
  2583       | char_ast_tr asts = raise AST ("char_ast_tr", asts);
  2584 
  2585     fun string_ast_tr [Syntax.Variable xstr] =
  2586         (case Syntax.explode_xstr xstr of
  2587           [] => constants [Syntax.constrainC, "Nil", "string"]
  2588         | cs => mk_string cs)
  2589       | string_ast_tr asts = raise AST ("string_tr", asts);
  2590   in [("_Char", char_ast_tr), ("_String", string_ast_tr)] end;
  2591 *}
  2592 
  2593 ML {*
  2594 fun int_of_nibble h =
  2595   if "0" <= h andalso h <= "9" then ord h - ord "0"
  2596   else if "A" <= h andalso h <= "F" then ord h - ord "A" + 10
  2597   else raise Match;
  2598 
  2599 fun nibble_of_int i =
  2600   if i <= 9 then chr (ord "0" + i) else chr (ord "A" + i - 10);
  2601 *}
  2602 
  2603 print_ast_translation {*
  2604   let
  2605     fun dest_nib (Syntax.Constant c) =
  2606         (case explode c of
  2607           ["N", "i", "b", "b", "l", "e", h] => int_of_nibble h
  2608         | _ => raise Match)
  2609       | dest_nib _ = raise Match;
  2610 
  2611     fun dest_chr c1 c2 =
  2612       let val c = chr (dest_nib c1 * 16 + dest_nib c2)
  2613       in if Symbol.is_printable c then c else raise Match end;
  2614 
  2615     fun dest_char (Syntax.Appl [Syntax.Constant "Char", c1, c2]) = dest_chr c1 c2
  2616       | dest_char _ = raise Match;
  2617 
  2618     fun xstr cs = Syntax.Appl [Syntax.Constant "_xstr", Syntax.Variable (Syntax.implode_xstr cs)];
  2619 
  2620     fun char_ast_tr' [c1, c2] = Syntax.Appl [Syntax.Constant "_Char", xstr [dest_chr c1 c2]]
  2621       | char_ast_tr' _ = raise Match;
  2622 
  2623     fun list_ast_tr' [args] = Syntax.Appl [Syntax.Constant "_String",
  2624             xstr (map dest_char (Syntax.unfold_ast "_args" args))]
  2625       | list_ast_tr' ts = raise Match;
  2626   in [("Char", char_ast_tr'), ("@list", list_ast_tr')] end;
  2627 *}
  2628 
  2629 subsubsection {* Code generator setup *}
  2630 
  2631 ML {*
  2632 local
  2633 
  2634 fun list_codegen thy defs gr dep thyname b t =
  2635   let val (gr', ps) = foldl_map (Codegen.invoke_codegen thy defs dep thyname false)
  2636     (gr, HOLogic.dest_list t)
  2637   in SOME (gr', Pretty.list "[" "]" ps) end handle TERM _ => NONE;
  2638 
  2639 fun dest_nibble (Const (s, _)) = int_of_nibble (unprefix "List.nibble.Nibble" s)
  2640   | dest_nibble _ = raise Match;
  2641 
  2642 fun char_codegen thy defs gr dep thyname b (Const ("List.char.Char", _) $ c1 $ c2) =
  2643     (let val c = chr (dest_nibble c1 * 16 + dest_nibble c2)
  2644      in if Symbol.is_printable c then SOME (gr, Pretty.quote (Pretty.str c))
  2645        else NONE
  2646      end handle Fail _ => NONE | Match => NONE)
  2647   | char_codegen thy defs gr dep thyname b _ = NONE;
  2648 
  2649 in
  2650 
  2651 val list_codegen_setup =
  2652   [Codegen.add_codegen "list_codegen" list_codegen,
  2653    Codegen.add_codegen "char_codegen" char_codegen];
  2654 
  2655 end;
  2656 *}
  2657 
  2658 types_code
  2659   "list" ("_ list")
  2660 attach (term_of) {*
  2661 val term_of_list = HOLogic.mk_list;
  2662 *}
  2663 attach (test) {*
  2664 fun gen_list' aG i j = frequency
  2665   [(i, fn () => aG j :: gen_list' aG (i-1) j), (1, fn () => [])] ()
  2666 and gen_list aG i = gen_list' aG i i;
  2667 *}
  2668   "char" ("string")
  2669 attach (term_of) {*
  2670 val nibbleT = Type ("List.nibble", []);
  2671 
  2672 fun term_of_char c =
  2673   Const ("List.char.Char", nibbleT --> nibbleT --> Type ("List.char", [])) $
  2674     Const ("List.nibble.Nibble" ^ nibble_of_int (ord c div 16), nibbleT) $
  2675     Const ("List.nibble.Nibble" ^ nibble_of_int (ord c mod 16), nibbleT);
  2676 *}
  2677 attach (test) {*
  2678 fun gen_char i = chr (random_range (ord "a") (Int.min (ord "a" + i, ord "z")));
  2679 *}
  2680 
  2681 consts_code "Cons" ("(_ ::/ _)")
  2682 
  2683 setup list_codegen_setup
  2684 
  2685 setup "[CodegenPackage.rename_inconsistent]"
  2686 
  2687 end