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