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