
clasohm@923: (*  Title:      HOL/List.thy
clasohm@923:     ID:         $Id$
clasohm@923:     Author:     Tobias Nipkow
clasohm@923:     Copyright   1994 TU Muenchen
clasohm@923: *)
clasohm@923: 
wenzelm@13114: header {* The datatype of finite lists *}
wenzelm@13122: 
wenzelm@13122: theory List = PreList:
clasohm@923: 
wenzelm@13142: datatype 'a list =
wenzelm@13142:     Nil    ("[]")
wenzelm@13142:   | Cons 'a  "'a list"    (infixr "#" 65)
clasohm@923: 
clasohm@923: consts
wenzelm@13142:   "@"         :: "'a list => 'a list => 'a list"            (infixr 65)
wenzelm@13142:   filter      :: "('a => bool) => 'a list => 'a list"
wenzelm@13142:   concat      :: "'a list list => 'a list"
wenzelm@13142:   foldl       :: "('b => 'a => 'b) => 'b => 'a list => 'b"
wenzelm@13142:   foldr       :: "('a => 'b => 'b) => 'a list => 'b => 'b"
wenzelm@13142:   hd          :: "'a list => 'a"
wenzelm@13142:   tl          :: "'a list => 'a list"
wenzelm@13142:   last        :: "'a list => 'a"
wenzelm@13142:   butlast     :: "'a list => 'a list"
wenzelm@13142:   set         :: "'a list => 'a set"
wenzelm@13142:   list_all    :: "('a => bool) => ('a list => bool)"
wenzelm@13142:   list_all2   :: "('a => 'b => bool) => 'a list => 'b list => bool"
wenzelm@13142:   map         :: "('a=>'b) => ('a list => 'b list)"
wenzelm@13142:   mem         :: "'a => 'a list => bool"                    (infixl 55)
wenzelm@13142:   nth         :: "'a list => nat => 'a"                   (infixl "!" 100)
wenzelm@13142:   list_update :: "'a list => nat => 'a => 'a list"
wenzelm@13142:   take        :: "nat => 'a list => 'a list"
wenzelm@13142:   drop        :: "nat => 'a list => 'a list"
wenzelm@13142:   takeWhile   :: "('a => bool) => 'a list => 'a list"
wenzelm@13142:   dropWhile   :: "('a => bool) => 'a list => 'a list"
wenzelm@13142:   rev         :: "'a list => 'a list"
wenzelm@13142:   zip         :: "'a list => 'b list => ('a * 'b) list"
wenzelm@13142:   upt         :: "nat => nat => nat list"                   ("(1[_../_'(])")
wenzelm@13142:   remdups     :: "'a list => 'a list"
wenzelm@13142:   null        :: "'a list => bool"
wenzelm@13142:   "distinct"  :: "'a list => bool"
wenzelm@13142:   replicate   :: "nat => 'a => 'a list"
clasohm@923: 
nipkow@5077: nonterminals
nipkow@5077:   lupdbinds  lupdbind
nipkow@5077: 
clasohm@923: syntax
wenzelm@13142:   -- {* list Enumeration *}
wenzelm@13142:   "@list"     :: "args => 'a list"                          ("[(_)]")
clasohm@923: 
wenzelm@13142:   -- {* Special syntax for filter *}
wenzelm@13142:   "@filter"   :: "[pttrn, 'a list, bool] => 'a list"        ("(1[_:_./ _])")
clasohm@923: 
wenzelm@13142:   -- {* list update *}
wenzelm@13142:   "_lupdbind"      :: "['a, 'a] => lupdbind"            ("(2_ :=/ _)")
wenzelm@13142:   ""               :: "lupdbind => lupdbinds"           ("_")
wenzelm@13142:   "_lupdbinds"     :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _")
wenzelm@13142:   "_LUpdate"       :: "['a, lupdbinds] => 'a"           ("_/[(_)]" [900,0] 900)
nipkow@5077: 
wenzelm@13142:   upto        :: "nat => nat => nat list"                   ("(1[_../_])")
nipkow@5427: 
clasohm@923: translations
clasohm@923:   "[x, xs]"     == "x#[xs]"
clasohm@923:   "[x]"         == "x#[]"
wenzelm@3842:   "[x:xs . P]"  == "filter (%x. P) xs"
clasohm@923: 
nipkow@5077:   "_LUpdate xs (_lupdbinds b bs)"  == "_LUpdate (_LUpdate xs b) bs"
nipkow@5077:   "xs[i:=x]"                       == "list_update xs i x"
nipkow@5077: 
nipkow@5427:   "[i..j]" == "[i..(Suc j)(]"
nipkow@5427: 
nipkow@5427: 
wenzelm@12114: syntax (xsymbols)
wenzelm@13142:   "@filter"   :: "[pttrn, 'a list, bool] => 'a list"        ("(1[_\<in>_ ./ _])")
paulson@3342: 
paulson@3342: 
wenzelm@13142: text {*
wenzelm@13142:   Function @{text size} is overloaded for all datatypes.  Users may
wenzelm@13142:   refer to the list version as @{text length}. *}
wenzelm@13142: 
wenzelm@13142: syntax length :: "'a list => nat"
wenzelm@13142: translations "length" => "size :: _ list => nat"
wenzelm@13114: 
wenzelm@13142: typed_print_translation {*
wenzelm@13142:   let
wenzelm@13142:     fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
wenzelm@13142:           Syntax.const "length" $ t
wenzelm@13142:       | size_tr' _ _ _ = raise Match;
wenzelm@13142:   in [("size", size_tr')] end
wenzelm@13114: *}
paulson@3437: 
berghofe@5183: primrec
berghofe@1898:   "hd(x#xs) = x"
berghofe@5183: primrec
paulson@8972:   "tl([])   = []"
berghofe@1898:   "tl(x#xs) = xs"
berghofe@5183: primrec
paulson@8972:   "null([])   = True"
paulson@8972:   "null(x#xs) = False"
paulson@8972: primrec
nipkow@3896:   "last(x#xs) = (if xs=[] then x else last xs)"
berghofe@5183: primrec
paulson@8972:   "butlast []    = []"
nipkow@3896:   "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
berghofe@5183: primrec
paulson@8972:   "x mem []     = False"
oheimb@5518:   "x mem (y#ys) = (if y=x then True else x mem ys)"
oheimb@5518: primrec
nipkow@3465:   "set [] = {}"
nipkow@3465:   "set (x#xs) = insert x (set xs)"
berghofe@5183: primrec
wenzelm@13114:   list_all_Nil:  "list_all P [] = True"
wenzelm@13142:   list_all_Cons: "list_all P (x#xs) = (P(x) \<and> list_all P xs)"
oheimb@5518: primrec
paulson@8972:   "map f []     = []"
berghofe@1898:   "map f (x#xs) = f(x)#map f xs"
berghofe@5183: primrec
wenzelm@13114:   append_Nil:  "[]    @ys = ys"
wenzelm@13114:   append_Cons: "(x#xs)@ys = x#(xs@ys)"
berghofe@5183: primrec
paulson@8972:   "rev([])   = []"
berghofe@1898:   "rev(x#xs) = rev(xs) @ [x]"
berghofe@5183: primrec
paulson@8972:   "filter P []     = []"
berghofe@1898:   "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
berghofe@5183: primrec
wenzelm@13114:   foldl_Nil:  "foldl f a [] = a"
wenzelm@13114:   foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
berghofe@5183: primrec
paulson@8972:   "foldr f [] a     = a"
paulson@8000:   "foldr f (x#xs) a = f x (foldr f xs a)"
paulson@8000: primrec
paulson@8972:   "concat([])   = []"
nipkow@2608:   "concat(x#xs) = x @ concat(xs)"
berghofe@5183: primrec
wenzelm@13114:   drop_Nil:  "drop n [] = []"
wenzelm@13142:   drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
wenzelm@13142:     -- {* Warning: simpset does not contain this definition *}
wenzelm@13142:     -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
berghofe@5183: primrec
wenzelm@13114:   take_Nil:  "take n [] = []"
wenzelm@13142:   take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
wenzelm@13142:     -- {* Warning: simpset does not contain this definition *}
wenzelm@13142:     -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
berghofe@5183: primrec
wenzelm@13142:   nth_Cons:  "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
wenzelm@13142:     -- {* Warning: simpset does not contain this definition *}
wenzelm@13142:     -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
wenzelm@13142: primrec
wenzelm@13142:   "[][i:=v] = []"
wenzelm@13142:   "(x#xs)[i:=v] =
wenzelm@13142:     (case i of 0 => v # xs
wenzelm@13142:     | Suc j => x # xs[j:=v])"
berghofe@5183: primrec
paulson@8972:   "takeWhile P []     = []"
nipkow@2608:   "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
berghofe@5183: primrec
paulson@8972:   "dropWhile P []     = []"
nipkow@3584:   "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
berghofe@5183: primrec
oheimb@4132:   "zip xs []     = []"
wenzelm@13142:   zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
wenzelm@13142:     -- {* Warning: simpset does not contain this definition *}
wenzelm@13142:     -- {* but separate theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
nipkow@5427: primrec
wenzelm@13114:   upt_0:   "[i..0(] = []"
wenzelm@13114:   upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
berghofe@5183: primrec
nipkow@12887:   "distinct []     = True"
wenzelm@13142:   "distinct (x#xs) = (x ~: set xs \<and> distinct xs)"
berghofe@5183: primrec
nipkow@4605:   "remdups [] = []"
nipkow@4605:   "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
berghofe@5183: primrec
wenzelm@13114:   replicate_0:   "replicate  0      x = []"
wenzelm@13114:   replicate_Suc: "replicate (Suc n) x = x # replicate n x"
nipkow@8115: defs
wenzelm@13114:  list_all2_def:
wenzelm@13142:  "list_all2 P xs ys == length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)"
nipkow@8115: 
paulson@3196: 
wenzelm@13142: subsection {* Lexicographic orderings on lists *}
nipkow@5281: 
nipkow@5281: consts
wenzelm@13142:   lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
nipkow@5281: primrec
wenzelm@13142:   "lexn r 0 = {}"
wenzelm@13142:   "lexn r (Suc n) =
wenzelm@13142:     (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
wenzelm@13142:       {(xs,ys). length xs = Suc n \<and> length ys = Suc n}"
nipkow@5281: 
nipkow@5281: constdefs
wenzelm@13142:   lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
wenzelm@13142:   "lex r == \<Union>n. lexn r n"
nipkow@5281: 
wenzelm@13142:   lexico :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
wenzelm@13142:   "lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
paulson@9336: 
wenzelm@13142:   sublist :: "'a list => nat set => 'a list"
wenzelm@13142:   "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))"
nipkow@5281: 
wenzelm@13114: 
wenzelm@13142: lemma not_Cons_self [simp]: "xs \<noteq> x # xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
wenzelm@13114: 
wenzelm@13142: lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma length_induct:
wenzelm@13142:     "(!!xs. \<forall>ys. length ys < length xs --> P ys ==> P xs) ==> P xs"
wenzelm@13142:   by (rule measure_induct [of length]) rules
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text lists}: the list-forming operator over sets *}
wenzelm@13114: 
wenzelm@13142: consts lists :: "'a set => 'a list set"
wenzelm@13142: inductive "lists A"
wenzelm@13142:   intros
wenzelm@13142:     Nil [intro!]: "[]: lists A"
wenzelm@13142:     Cons [intro!]: "[| a: A;  l: lists A  |] ==> a#l : lists A"
wenzelm@13114: 
wenzelm@13142: inductive_cases listsE [elim!]: "x#l : lists A"
wenzelm@13114: 
wenzelm@13142: lemma lists_mono: "A \<subseteq> B ==> lists A \<subseteq> lists B"
wenzelm@13142:   by (unfold lists.defs) (blast intro!: lfp_mono)
wenzelm@13114: 
wenzelm@13142: lemma lists_IntI [rule_format]:
wenzelm@13142:     "l: lists A ==> l: lists B --> l: lists (A Int B)"
wenzelm@13142:   apply (erule lists.induct)
wenzelm@13142:   apply blast+
wenzelm@13142:   done
wenzelm@13142: 
wenzelm@13142: lemma lists_Int_eq [simp]: "lists (A \<inter> B) = lists A \<inter> lists B"
wenzelm@13142:   apply (rule mono_Int [THEN equalityI])
wenzelm@13142:   apply (simp add: mono_def lists_mono)
wenzelm@13142:   apply (blast intro!: lists_IntI)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma append_in_lists_conv [iff]:
wenzelm@13142:     "(xs @ ys : lists A) = (xs : lists A \<and> ys : lists A)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13142: 
wenzelm@13142: 
wenzelm@13142: subsection {* @{text length} *}
wenzelm@13114: 
wenzelm@13142: text {*
wenzelm@13142:   Needs to come before @{text "@"} because of theorem @{text
wenzelm@13142:   append_eq_append_conv}.
wenzelm@13142: *}
wenzelm@13114: 
wenzelm@13142: lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma length_map [simp]: "length (map f xs) = length xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma length_rev [simp]: "length (rev xs) = length xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma length_tl [simp]: "length (tl xs) = length xs - 1"
wenzelm@13142:   by (cases xs) auto
wenzelm@13114: 
wenzelm@13142: lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13114: lemma length_Suc_conv:
wenzelm@13142:     "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13142: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text "@"} -- append *}
wenzelm@13114: 
wenzelm@13142: lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma append_Nil2 [simp]: "xs @ [] = xs"
wenzelm@13142:   by (induct xs) auto
nipkow@3507: 
wenzelm@13142: lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma append_eq_append_conv [rule_format, simp]:
wenzelm@13142:  "\<forall>ys. length xs = length ys \<or> length us = length vs
wenzelm@13142:        --> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
wenzelm@13142:   apply (induct_tac xs)
wenzelm@13142:    apply(rule allI)
wenzelm@13142:    apply (case_tac ys)
wenzelm@13142:     apply simp
wenzelm@13142:    apply force
wenzelm@13142:   apply (rule allI)
wenzelm@13142:   apply (case_tac ys)
wenzelm@13142:    apply force
wenzelm@13114:   apply simp
wenzelm@13142:   done
wenzelm@13142: 
wenzelm@13142: lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
wenzelm@13142:   by simp
wenzelm@13142: 
wenzelm@13142: lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
wenzelm@13142:   by simp
wenzelm@13114: 
wenzelm@13142: lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
wenzelm@13142:   by simp
wenzelm@13114: 
wenzelm@13142: lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
wenzelm@13142:   using append_same_eq [of _ _ "[]"] by auto
nipkow@3507: 
wenzelm@13142: lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
wenzelm@13142:   using append_same_eq [of "[]"] by auto
wenzelm@13114: 
wenzelm@13142: lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
wenzelm@13142:   by (simp add: hd_append split: list.split)
wenzelm@13114: 
wenzelm@13142: lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
wenzelm@13142:   by (simp split: list.split)
wenzelm@13114: 
wenzelm@13142: lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
wenzelm@13142:   by (simp add: tl_append split: list.split)
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: text {* Trivial rules for solving @{text "@"}-equations automatically. *}
wenzelm@13114: 
wenzelm@13114: lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
wenzelm@13142:   by simp
wenzelm@13114: 
wenzelm@13142: lemma Cons_eq_appendI:
wenzelm@13142:     "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
wenzelm@13142:   by (drule sym) simp
wenzelm@13114: 
wenzelm@13142: lemma append_eq_appendI:
wenzelm@13142:     "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
wenzelm@13142:   by (drule sym) simp
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: text {*
wenzelm@13142:   Simplification procedure for all list equalities.
wenzelm@13142:   Currently only tries to rearrange @{text "@"} to see if
wenzelm@13142:   - both lists end in a singleton list,
wenzelm@13142:   - or both lists end in the same list.
wenzelm@13142: *}
wenzelm@13142: 
wenzelm@13142: ML_setup {*
nipkow@3507: local
nipkow@3507: 
wenzelm@13122: val append_assoc = thm "append_assoc";
wenzelm@13122: val append_Nil = thm "append_Nil";
wenzelm@13122: val append_Cons = thm "append_Cons";
wenzelm@13122: val append1_eq_conv = thm "append1_eq_conv";
wenzelm@13122: val append_same_eq = thm "append_same_eq";
wenzelm@13122: 
wenzelm@13114: val list_eq_pattern =
wenzelm@13114:   Thm.read_cterm (Theory.sign_of (the_context ())) ("(xs::'a list) = ys",HOLogic.boolT)
wenzelm@13114: 
wenzelm@13114: fun last (cons as Const("List.list.Cons",_) $ _ $ xs) =
wenzelm@13114:       (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
wenzelm@13114:   | last (Const("List.op @",_) $ _ $ ys) = last ys
wenzelm@13114:   | last t = t
wenzelm@13114: 
wenzelm@13114: fun list1 (Const("List.list.Cons",_) $ _ $ Const("List.list.Nil",_)) = true
wenzelm@13114:   | list1 _ = false
wenzelm@13114: 
wenzelm@13114: fun butlast ((cons as Const("List.list.Cons",_) $ x) $ xs) =
wenzelm@13114:       (case xs of Const("List.list.Nil",_) => xs | _ => cons $ butlast xs)
wenzelm@13114:   | butlast ((app as Const("List.op @",_) $ xs) $ ys) = app $ butlast ys
wenzelm@13114:   | butlast xs = Const("List.list.Nil",fastype_of xs)
wenzelm@13114: 
wenzelm@13114: val rearr_tac =
wenzelm@13114:   simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons])
wenzelm@13114: 
wenzelm@13114: fun list_eq sg _ (F as (eq as Const(_,eqT)) $ lhs $ rhs) =
wenzelm@13114:   let
wenzelm@13114:     val lastl = last lhs and lastr = last rhs
wenzelm@13114:     fun rearr conv =
wenzelm@13114:       let val lhs1 = butlast lhs and rhs1 = butlast rhs
wenzelm@13114:           val Type(_,listT::_) = eqT
wenzelm@13114:           val appT = [listT,listT] ---> listT
wenzelm@13114:           val app = Const("List.op @",appT)
wenzelm@13114:           val F2 = eq $ (app$lhs1$lastl) $ (app$rhs1$lastr)
wenzelm@13114:           val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
wenzelm@13114:           val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
wenzelm@13114:             handle ERROR =>
wenzelm@13114:             error("The error(s) above occurred while trying to prove " ^
wenzelm@13114:                   string_of_cterm ct)
wenzelm@13114:       in Some((conv RS (thm RS trans)) RS eq_reflection) end
wenzelm@13114: 
wenzelm@13114:   in if list1 lastl andalso list1 lastr
wenzelm@13114:      then rearr append1_eq_conv
wenzelm@13114:      else
wenzelm@13114:      if lastl aconv lastr
wenzelm@13114:      then rearr append_same_eq
wenzelm@13114:      else None
wenzelm@13114:   end
wenzelm@13114: in
wenzelm@13114: val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq
wenzelm@13114: end;
wenzelm@13114: 
wenzelm@13114: Addsimprocs [list_eq_simproc];
wenzelm@13114: *}
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text map} *}
wenzelm@13114: 
wenzelm@13142: lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
wenzelm@13142:   by (induct xs) simp_all
wenzelm@13114: 
wenzelm@13142: lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
wenzelm@13142:   by (rule ext, induct_tac xs) auto
wenzelm@13114: 
wenzelm@13142: lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma map_compose: "map (f o g) xs = map f (map g xs)"
wenzelm@13142:   by (induct xs) (auto simp add: o_def)
wenzelm@13114: 
wenzelm@13142: lemma rev_map: "rev (map f xs) = map f (rev xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13114: lemma map_cong:
wenzelm@13142:   "xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
wenzelm@13142:   -- {* a congruence rule for @{text map} *}
wenzelm@13142:   by (clarify, induct ys) auto
wenzelm@13114: 
wenzelm@13142: lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
wenzelm@13142:   by (cases xs) auto
wenzelm@13114: 
wenzelm@13142: lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
wenzelm@13142:   by (cases xs) auto
wenzelm@13114: 
wenzelm@13114: lemma map_eq_Cons:
wenzelm@13142:   "(map f xs = y # ys) = (\<exists>x xs'. xs = x # xs' \<and> f x = y \<and> map f xs' = ys)"
wenzelm@13142:   by (cases xs) auto
wenzelm@13114: 
wenzelm@13114: lemma map_injective:
wenzelm@13142:     "!!xs. map f xs = map f ys ==> (\<forall>x y. f x = f y --> x = y) ==> xs = ys"
wenzelm@13142:   by (induct ys) (auto simp add: map_eq_Cons)
wenzelm@13114: 
wenzelm@13114: lemma inj_mapI: "inj f ==> inj (map f)"
wenzelm@13142:   by (rules dest: map_injective injD intro: injI)
wenzelm@13114: 
wenzelm@13114: lemma inj_mapD: "inj (map f) ==> inj f"
wenzelm@13142:   apply (unfold inj_on_def)
wenzelm@13142:   apply clarify
wenzelm@13142:   apply (erule_tac x = "[x]" in ballE)
wenzelm@13142:    apply (erule_tac x = "[y]" in ballE)
wenzelm@13142:     apply simp
wenzelm@13142:    apply blast
wenzelm@13142:   apply blast
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma inj_map: "inj (map f) = inj f"
wenzelm@13142:   by (blast dest: inj_mapD intro: inj_mapI)
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text rev} *}
wenzelm@13114: 
wenzelm@13142: lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
wenzelm@13142:   apply (induct xs)
wenzelm@13142:    apply force
wenzelm@13142:   apply (case_tac ys)
wenzelm@13142:    apply simp
wenzelm@13142:   apply force
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma rev_induct: "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
wenzelm@13142:   apply(subst rev_rev_ident[symmetric])
wenzelm@13142:   apply(rule_tac list = "rev xs" in list.induct, simp_all)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}  -- "compatibility"
wenzelm@13114: 
wenzelm@13142: lemma rev_exhaust: "(xs = [] ==> P) ==>  (!!ys y. xs = ys @ [y] ==> P) ==> P"
wenzelm@13142:   by (induct xs rule: rev_induct) auto
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text set} *}
wenzelm@13114: 
wenzelm@13142: lemma finite_set [iff]: "finite (set xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
wenzelm@13142:   by auto
wenzelm@13114: 
wenzelm@13142: lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma set_rev [simp]: "set (rev xs) = set xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma set_map [simp]: "set (map f xs) = f`(set xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma set_upt [simp]: "set[i..j(] = {k. i \<le> k \<and> k < j}"
wenzelm@13142:   apply (induct j)
wenzelm@13142:    apply simp_all
wenzelm@13142:   apply(erule ssubst)
wenzelm@13142:   apply auto
wenzelm@13142:   apply arith
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
wenzelm@13142:   apply (induct xs)
wenzelm@13142:    apply simp
wenzelm@13142:   apply simp
wenzelm@13142:   apply (rule iffI)
wenzelm@13142:    apply (blast intro: eq_Nil_appendI Cons_eq_appendI)
wenzelm@13142:   apply (erule exE)+
wenzelm@13142:   apply (case_tac ys)
wenzelm@13142:   apply auto
wenzelm@13142:   done
wenzelm@13142: 
wenzelm@13142: lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)"
wenzelm@13142:   -- {* eliminate @{text lists} in favour of @{text set} *}
wenzelm@13142:   by (induct xs) auto
wenzelm@13142: 
wenzelm@13142: lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A"
wenzelm@13142:   by (rule in_lists_conv_set [THEN iffD1])
wenzelm@13142: 
wenzelm@13142: lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A"
wenzelm@13142:   by (rule in_lists_conv_set [THEN iffD2])
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text mem} *}
wenzelm@13114: 
wenzelm@13114: lemma set_mem_eq: "(x mem xs) = (x : set xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text list_all} *}
wenzelm@13114: 
wenzelm@13142: lemma list_all_conv: "list_all P xs = (\<forall>x \<in> set xs. P x)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma list_all_append [simp]:
wenzelm@13142:     "list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text filter} *}
wenzelm@13114: 
wenzelm@13142: lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma length_filter [simp]: "length (filter P xs) \<le> length xs"
wenzelm@13142:   by (induct xs) (auto simp add: le_SucI)
wenzelm@13114: 
wenzelm@13142: lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
wenzelm@13142:   by auto
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text concat} *}
wenzelm@13114: 
wenzelm@13142: lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma concat_eq_Nil_conv [iff]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
wenzelm@13142:   by (induct xss) auto
wenzelm@13114: 
wenzelm@13142: lemma Nil_eq_concat_conv [iff]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
wenzelm@13142:   by (induct xss) auto
wenzelm@13114: 
wenzelm@13142: lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text nth} *}
wenzelm@13114: 
wenzelm@13142: lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
wenzelm@13142:   by auto
wenzelm@13114: 
wenzelm@13142: lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
wenzelm@13142:   by auto
wenzelm@13114: 
wenzelm@13142: declare nth.simps [simp del]
wenzelm@13114: 
wenzelm@13114: lemma nth_append:
wenzelm@13142:     "!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
wenzelm@13142:   apply(induct "xs")
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac n)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)"
wenzelm@13142:   apply(induct xs)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac n)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
wenzelm@13142:   apply (induct_tac xs)
wenzelm@13142:    apply simp
wenzelm@13114:   apply simp
wenzelm@13142:   apply safe
wenzelm@13142:     apply (rule_tac x = 0 in exI)
wenzelm@13142:     apply simp
wenzelm@13142:    apply (rule_tac x = "Suc i" in exI)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac i)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (rename_tac j)
wenzelm@13142:   apply (rule_tac x = j in exI)
wenzelm@13142:   apply simp
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma list_ball_nth: "[| n < length xs; !x : set xs. P x  |] ==> P(xs!n)"
wenzelm@13142:   by (auto simp add: set_conv_nth)
wenzelm@13114: 
wenzelm@13142: lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
wenzelm@13142:   by (auto simp add: set_conv_nth)
wenzelm@13114: 
wenzelm@13114: lemma all_nth_imp_all_set:
wenzelm@13142:     "[| !i < length xs. P(xs!i); x : set xs  |] ==> P x"
wenzelm@13142:   by (auto simp add: set_conv_nth)
wenzelm@13114: 
wenzelm@13114: lemma all_set_conv_all_nth:
wenzelm@13142:     "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
wenzelm@13142:   by (auto simp add: set_conv_nth)
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text list_update} *}
wenzelm@13114: 
wenzelm@13142: lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs"
wenzelm@13142:   by (induct xs) (auto split: nat.split)
wenzelm@13114: 
wenzelm@13114: lemma nth_list_update:
wenzelm@13142:     "!!i j. i < length xs  ==> (xs[i:=x])!j = (if i = j then x else xs!j)"
wenzelm@13142:   by (induct xs) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114: 
wenzelm@13142: lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
wenzelm@13142:   by (simp add: nth_list_update)
wenzelm@13114: 
wenzelm@13142: lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j"
wenzelm@13142:   by (induct xs) (auto simp add: nth_Cons split: nat.split)
wenzelm@13114: 
wenzelm@13142: lemma list_update_overwrite [simp]:
wenzelm@13142:     "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
wenzelm@13142:   by (induct xs) (auto split: nat.split)
wenzelm@13114: 
wenzelm@13114: lemma list_update_same_conv:
wenzelm@13142:     "!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
wenzelm@13142:   by (induct xs) (auto split: nat.split)
wenzelm@13114: 
wenzelm@13114: lemma update_zip:
wenzelm@13142:   "!!i xy xs. length xs = length ys ==>
wenzelm@13114:     (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
wenzelm@13142:   by (induct ys) (auto, case_tac xs, auto split: nat.split)
wenzelm@13114: 
wenzelm@13114: lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"
wenzelm@13142:   by (induct xs) (auto split: nat.split)
wenzelm@13114: 
wenzelm@13114: lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
wenzelm@13142:   by (blast dest!: set_update_subset_insert [THEN subsetD])
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text last} and @{text butlast} *}
wenzelm@13114: 
wenzelm@13142: lemma last_snoc [simp]: "last (xs @ [x]) = x"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
wenzelm@13142:   by (induct xs rule: rev_induct) auto
wenzelm@13114: 
wenzelm@13114: lemma butlast_append:
wenzelm@13142:     "!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma append_butlast_last_id [simp]:
wenzelm@13142:     "xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
wenzelm@13142:   by (induct xs) (auto split: split_if_asm)
wenzelm@13114: 
wenzelm@13114: lemma in_set_butlast_appendI:
wenzelm@13142:     "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
wenzelm@13142:   by (auto dest: in_set_butlastD simp add: butlast_append)
wenzelm@13114: 
wenzelm@13142: 
wenzelm@13142: subsection {* @{text take} and @{text drop} *}
wenzelm@13114: 
wenzelm@13142: lemma take_0 [simp]: "take 0 xs = []"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma drop_0 [simp]: "drop 0 xs = xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
wenzelm@13142:   by simp
wenzelm@13114: 
wenzelm@13142: lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
wenzelm@13142:   by simp
wenzelm@13114: 
wenzelm@13142: declare take_Cons [simp del] and drop_Cons [simp del]
wenzelm@13114: 
wenzelm@13142: lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n"
wenzelm@13142:   by (induct n) (auto, case_tac xs, auto)
wenzelm@13114: 
wenzelm@13142: lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)"
wenzelm@13142:   by (induct n) (auto, case_tac xs, auto)
wenzelm@13114: 
wenzelm@13142: lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs"
wenzelm@13142:   by (induct n) (auto, case_tac xs, auto)
wenzelm@13114: 
wenzelm@13142: lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []"
wenzelm@13142:   by (induct n) (auto, case_tac xs, auto)
wenzelm@13114: 
wenzelm@13142: lemma take_append [simp]:
wenzelm@13142:     "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
wenzelm@13142:   by (induct n) (auto, case_tac xs, auto)
wenzelm@13114: 
wenzelm@13142: lemma drop_append [simp]:
wenzelm@13142:     "!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
wenzelm@13142:   by (induct n) (auto, case_tac xs, auto)
wenzelm@13114: 
wenzelm@13142: lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
wenzelm@13142:   apply (induct m)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac na)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs"
wenzelm@13142:   apply (induct m)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"
wenzelm@13142:   apply (induct m)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs"
wenzelm@13142:   apply (induct n)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"
wenzelm@13142:   apply (induct n)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)"
wenzelm@13142:   apply (induct n)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"
wenzelm@13142:   apply (induct xs)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac i)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"
wenzelm@13142:   apply (induct xs)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac i)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
wenzelm@13142:   apply (induct xs)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac n)
wenzelm@13142:    apply(blast )
wenzelm@13142:   apply (case_tac i)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma nth_drop [simp]:
wenzelm@13142:     "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
wenzelm@13142:   apply (induct n)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply auto
wenzelm@13142:   done
nipkow@3507: 
wenzelm@13114: lemma append_eq_conv_conj:
wenzelm@13142:     "!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
wenzelm@13142:   apply(induct xs)
wenzelm@13142:    apply simp
wenzelm@13142:   apply clarsimp
wenzelm@13142:   apply (case_tac zs)
wenzelm@13142:   apply auto
wenzelm@13142:   done
wenzelm@13142: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text takeWhile} and @{text dropWhile} *}
wenzelm@13114: 
wenzelm@13142: lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma takeWhile_append1 [simp]:
wenzelm@13142:     "[| x:set xs; ~P(x)  |] ==> takeWhile P (xs @ ys) = takeWhile P xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma takeWhile_append2 [simp]:
wenzelm@13142:     "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma dropWhile_append1 [simp]:
wenzelm@13142:     "[| x : set xs; ~P(x)  |] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma dropWhile_append2 [simp]:
wenzelm@13142:     "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
wenzelm@13142:   by (induct xs) (auto split: split_if_asm)
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text zip} *}
wenzelm@13114: 
wenzelm@13142: lemma zip_Nil [simp]: "zip [] ys = []"
wenzelm@13142:   by (induct ys) auto
wenzelm@13114: 
wenzelm@13142: lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
wenzelm@13142:   by simp
wenzelm@13114: 
wenzelm@13142: declare zip_Cons [simp del]
wenzelm@13114: 
wenzelm@13142: lemma length_zip [simp]:
wenzelm@13142:     "!!xs. length (zip xs ys) = min (length xs) (length ys)"
wenzelm@13142:   apply(induct ys)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma zip_append1:
wenzelm@13142:   "!!xs. zip (xs @ ys) zs =
wenzelm@13142:       zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
wenzelm@13142:   apply (induct zs)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply simp_all
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma zip_append2:
wenzelm@13142:   "!!ys. zip xs (ys @ zs) =
wenzelm@13142:       zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
wenzelm@13142:   apply (induct xs)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac ys)
wenzelm@13142:    apply simp_all
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma zip_append [simp]:
wenzelm@13142:  "[| length xs = length us; length ys = length vs |] ==>
wenzelm@13142:     zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
wenzelm@13142:   by (simp add: zip_append1)
wenzelm@13114: 
wenzelm@13114: lemma zip_rev:
wenzelm@13142:     "!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
wenzelm@13142:   apply(induct ys)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply simp_all
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma nth_zip [simp]:
wenzelm@13142:   "!!i xs. [| i < length xs; i < length ys  |] ==> (zip xs ys)!i = (xs!i, ys!i)"
wenzelm@13142:   apply (induct ys)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply (simp_all add: nth.simps split: nat.split)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma set_zip:
wenzelm@13142:     "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
wenzelm@13142:   by (simp add: set_conv_nth cong: rev_conj_cong)
wenzelm@13114: 
wenzelm@13114: lemma zip_update:
wenzelm@13142:     "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
wenzelm@13142:   by (rule sym, simp add: update_zip)
wenzelm@13114: 
wenzelm@13142: lemma zip_replicate [simp]:
wenzelm@13142:     "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
wenzelm@13142:   apply (induct i)
wenzelm@13142:    apply auto
wenzelm@13142:   apply (case_tac j)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: 
wenzelm@13142: subsection {* @{text list_all2} *}
wenzelm@13114: 
wenzelm@13114: lemma list_all2_lengthD: "list_all2 P xs ys ==> length xs = length ys"
wenzelm@13142:   by (simp add: list_all2_def)
wenzelm@13114: 
wenzelm@13142: lemma list_all2_Nil [iff]: "list_all2 P [] ys = (ys = [])"
wenzelm@13142:   by (simp add: list_all2_def)
wenzelm@13114: 
wenzelm@13142: lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])"
wenzelm@13142:   by (simp add: list_all2_def)
wenzelm@13114: 
wenzelm@13142: lemma list_all2_Cons [iff]:
wenzelm@13142:     "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
wenzelm@13142:   by (auto simp add: list_all2_def)
wenzelm@13114: 
wenzelm@13114: lemma list_all2_Cons1:
wenzelm@13142:     "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
wenzelm@13142:   by (cases ys) auto
wenzelm@13114: 
wenzelm@13114: lemma list_all2_Cons2:
wenzelm@13142:     "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
wenzelm@13142:   by (cases xs) auto
wenzelm@13114: 
wenzelm@13142: lemma list_all2_rev [iff]:
wenzelm@13142:     "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
wenzelm@13142:   by (simp add: list_all2_def zip_rev cong: conj_cong)
wenzelm@13114: 
wenzelm@13114: lemma list_all2_append1:
wenzelm@13142:   "list_all2 P (xs @ ys) zs =
wenzelm@13142:     (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
wenzelm@13142:       list_all2 P xs us \<and> list_all2 P ys vs)"
wenzelm@13142:   apply (simp add: list_all2_def zip_append1)
wenzelm@13142:   apply (rule iffI)
wenzelm@13142:    apply (rule_tac x = "take (length xs) zs" in exI)
wenzelm@13142:    apply (rule_tac x = "drop (length xs) zs" in exI)
wenzelm@13142:    apply (force split: nat_diff_split simp add: min_def)
wenzelm@13142:   apply clarify
wenzelm@13142:   apply (simp add: ball_Un)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma list_all2_append2:
wenzelm@13142:   "list_all2 P xs (ys @ zs) =
wenzelm@13142:     (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
wenzelm@13142:       list_all2 P us ys \<and> list_all2 P vs zs)"
wenzelm@13142:   apply (simp add: list_all2_def zip_append2)
wenzelm@13142:   apply (rule iffI)
wenzelm@13142:    apply (rule_tac x = "take (length ys) xs" in exI)
wenzelm@13142:    apply (rule_tac x = "drop (length ys) xs" in exI)
wenzelm@13142:    apply (force split: nat_diff_split simp add: min_def)
wenzelm@13142:   apply clarify
wenzelm@13142:   apply (simp add: ball_Un)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma list_all2_conv_all_nth:
wenzelm@13114:   "list_all2 P xs ys =
wenzelm@13142:     (length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
wenzelm@13142:   by (force simp add: list_all2_def set_zip)
wenzelm@13114: 
wenzelm@13114: lemma list_all2_trans[rule_format]:
wenzelm@13142:   "\<forall>a b c. P1 a b --> P2 b c --> P3 a c ==>
wenzelm@13142:     \<forall>bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs"
wenzelm@13142:   apply(induct_tac as)
wenzelm@13142:    apply simp
wenzelm@13142:   apply(rule allI)
wenzelm@13142:   apply(induct_tac bs)
wenzelm@13142:    apply simp
wenzelm@13142:   apply(rule allI)
wenzelm@13142:   apply(induct_tac cs)
wenzelm@13142:    apply auto
wenzelm@13142:   done
wenzelm@13142: 
wenzelm@13142: 
wenzelm@13142: subsection {* @{text foldl} *}
wenzelm@13142: 
wenzelm@13142: lemma foldl_append [simp]:
wenzelm@13142:   "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
wenzelm@13142:   by (induct xs) auto
wenzelm@13142: 
wenzelm@13142: text {*
wenzelm@13142:   Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
wenzelm@13142:   difficult to use because it requires an additional transitivity step.
wenzelm@13142: *}
wenzelm@13142: 
wenzelm@13142: lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns"
wenzelm@13142:   by (induct ns) auto
wenzelm@13142: 
wenzelm@13142: lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns"
wenzelm@13142:   by (force intro: start_le_sum simp add: in_set_conv_decomp)
wenzelm@13142: 
wenzelm@13142: lemma sum_eq_0_conv [iff]:
wenzelm@13142:     "!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
wenzelm@13142:   by (induct ns) auto
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text upto} *}
wenzelm@13114: 
wenzelm@13142: lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])"
wenzelm@13142:   -- {* Does not terminate! *}
wenzelm@13142:   by (induct j) auto
wenzelm@13142: 
wenzelm@13142: lemma upt_conv_Nil [simp]: "j <= i ==> [i..j(] = []"
wenzelm@13142:   by (subst upt_rec) simp
wenzelm@13114: 
wenzelm@13142: lemma upt_Suc_append: "i <= j ==> [i..(Suc j)(] = [i..j(]@[j]"
wenzelm@13142:   -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
wenzelm@13142:   by simp
wenzelm@13114: 
wenzelm@13142: lemma upt_conv_Cons: "i < j ==> [i..j(] = i # [Suc i..j(]"
wenzelm@13142:   apply(rule trans)
wenzelm@13142:   apply(subst upt_rec)
wenzelm@13142:    prefer 2 apply(rule refl)
wenzelm@13142:   apply simp
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"
wenzelm@13142:   -- {* LOOPS as a simprule, since @{text "j <= j"}. *}
wenzelm@13142:   by (induct k) auto
wenzelm@13114: 
wenzelm@13142: lemma length_upt [simp]: "length [i..j(] = j - i"
wenzelm@13142:   by (induct j) (auto simp add: Suc_diff_le)
wenzelm@13114: 
wenzelm@13142: lemma nth_upt [simp]: "i + k < j ==> [i..j(] ! k = i + k"
wenzelm@13142:   apply (induct j)
wenzelm@13142:   apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]"
wenzelm@13142:   apply (induct m)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (subst upt_rec)
wenzelm@13142:   apply (rule sym)
wenzelm@13142:   apply (subst upt_rec)
wenzelm@13142:   apply (simp del: upt.simps)
wenzelm@13142:   done
nipkow@3507: 
wenzelm@13114: lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]"
wenzelm@13142:   by (induct n) auto
wenzelm@13114: 
wenzelm@13114: lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)"
wenzelm@13142:   apply (induct n m rule: diff_induct)
wenzelm@13142:     prefer 3 apply (subst map_Suc_upt[symmetric])
wenzelm@13142:     apply (auto simp add: less_diff_conv nth_upt)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma nth_take_lemma [rule_format]:
wenzelm@13142:   "ALL xs ys. k <= length xs --> k <= length ys
wenzelm@13142:     --> (ALL i. i < k --> xs!i = ys!i)
wenzelm@13142:     --> take k xs = take k ys"
wenzelm@13142:   apply (induct k)
wenzelm@13142:   apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib)
wenzelm@13142:   apply clarify
wenzelm@13142:   txt {* Both lists must be non-empty *}
wenzelm@13142:   apply (case_tac xs)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac ys)
wenzelm@13142:    apply clarify
wenzelm@13142:    apply (simp (no_asm_use))
wenzelm@13142:   apply clarify
wenzelm@13142:   txt {* prenexing's needed, not miniscoping *}
wenzelm@13142:   apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
wenzelm@13142:   apply blast
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma nth_equalityI:
wenzelm@13114:  "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
wenzelm@13142:   apply (frule nth_take_lemma [OF le_refl eq_imp_le])
wenzelm@13142:   apply (simp_all add: take_all)
wenzelm@13142:   done
wenzelm@13142: 
wenzelm@13142: lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
wenzelm@13142:   -- {* The famous take-lemma. *}
wenzelm@13142:   apply (drule_tac x = "max (length xs) (length ys)" in spec)
wenzelm@13142:   apply (simp add: le_max_iff_disj take_all)
wenzelm@13142:   done
wenzelm@13142: 
wenzelm@13142: 
wenzelm@13142: subsection {* @{text "distinct"} and @{text remdups} *}
wenzelm@13142: 
wenzelm@13142: lemma distinct_append [simp]:
wenzelm@13142:     "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
wenzelm@13142:   by (induct xs) auto
wenzelm@13142: 
wenzelm@13142: lemma set_remdups [simp]: "set (remdups xs) = set xs"
wenzelm@13142:   by (induct xs) (auto simp add: insert_absorb)
wenzelm@13142: 
wenzelm@13142: lemma distinct_remdups [iff]: "distinct (remdups xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13142: 
wenzelm@13142: lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
wenzelm@13142:   by (induct xs) auto
wenzelm@13114: 
wenzelm@13142: text {*
wenzelm@13142:   It is best to avoid this indexed version of distinct, but sometimes
wenzelm@13142:   it is useful. *}
wenzelm@13142: lemma distinct_conv_nth:
wenzelm@13142:     "distinct xs = (\<forall>i j. i < size xs \<and> j < size xs \<and> i \<noteq> j --> xs!i \<noteq> xs!j)"
wenzelm@13142:   apply (induct_tac xs)
wenzelm@13142:    apply simp
wenzelm@13142:   apply simp
wenzelm@13142:   apply (rule iffI)
wenzelm@13142:    apply clarsimp
wenzelm@13142:    apply (case_tac i)
wenzelm@13142:     apply (case_tac j)
wenzelm@13142:      apply simp
wenzelm@13142:     apply (simp add: set_conv_nth)
wenzelm@13142:    apply (case_tac j)
wenzelm@13142:     apply (clarsimp simp add: set_conv_nth)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (rule conjI)
wenzelm@13142:    apply (clarsimp simp add: set_conv_nth)
wenzelm@13142:    apply (erule_tac x = 0 in allE)
wenzelm@13142:    apply (erule_tac x = "Suc i" in allE)
wenzelm@13142:    apply simp
wenzelm@13142:   apply clarsimp
wenzelm@13142:   apply (erule_tac x = "Suc i" in allE)
wenzelm@13142:   apply (erule_tac x = "Suc j" in allE)
wenzelm@13142:   apply simp
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text replicate} *}
wenzelm@13114: 
wenzelm@13142: lemma length_replicate [simp]: "length (replicate n x) = n"
wenzelm@13142:   by (induct n) auto
nipkow@13124: 
wenzelm@13142: lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
wenzelm@13142:   by (induct n) auto
wenzelm@13114: 
wenzelm@13114: lemma replicate_app_Cons_same:
wenzelm@13142:     "(replicate n x) @ (x # xs) = x # replicate n x @ xs"
wenzelm@13142:   by (induct n) auto
wenzelm@13114: 
wenzelm@13142: lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
wenzelm@13142:   apply(induct n)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (simp add: replicate_app_Cons_same)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
wenzelm@13142:   by (induct n) auto
wenzelm@13114: 
wenzelm@13142: lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
wenzelm@13142:   by (induct n) auto
wenzelm@13114: 
wenzelm@13142: lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
wenzelm@13142:   by (induct n) auto
wenzelm@13114: 
wenzelm@13142: lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
wenzelm@13142:   by (atomize (full), induct n) auto
wenzelm@13114: 
wenzelm@13142: lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x"
wenzelm@13142:   apply(induct n)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (simp add: nth_Cons split: nat.split)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
wenzelm@13142:   by (induct n) auto
wenzelm@13114: 
wenzelm@13142: lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
wenzelm@13142:   by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
wenzelm@13114: 
wenzelm@13142: lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
wenzelm@13142:   by auto
wenzelm@13114: 
wenzelm@13142: lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
wenzelm@13142:   by (simp add: set_replicate_conv_if split: split_if_asm)
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* Lexcicographic orderings on lists *}
nipkow@3507: 
wenzelm@13142: lemma wf_lexn: "wf r ==> wf (lexn r n)"
wenzelm@13142:   apply (induct_tac n)
wenzelm@13142:    apply simp
wenzelm@13142:   apply simp
wenzelm@13142:   apply(rule wf_subset)
wenzelm@13142:    prefer 2 apply (rule Int_lower1)
wenzelm@13142:   apply(rule wf_prod_fun_image)
wenzelm@13142:    prefer 2 apply (rule injI)
wenzelm@13142:   apply auto
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma lexn_length:
wenzelm@13142:     "!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
wenzelm@13142:   by (induct n) auto
wenzelm@13114: 
wenzelm@13142: lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
wenzelm@13142:   apply (unfold lex_def)
wenzelm@13142:   apply (rule wf_UN)
wenzelm@13142:   apply (blast intro: wf_lexn)
wenzelm@13142:   apply clarify
wenzelm@13142:   apply (rename_tac m n)
wenzelm@13142:   apply (subgoal_tac "m \<noteq> n")
wenzelm@13142:    prefer 2 apply blast
wenzelm@13142:   apply (blast dest: lexn_length not_sym)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma lexn_conv:
wenzelm@13142:   "lexn r n =
wenzelm@13142:     {(xs,ys). length xs = n \<and> length ys = n \<and>
wenzelm@13142:       (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
wenzelm@13142:   apply (induct_tac n)
wenzelm@13142:    apply simp
wenzelm@13142:    apply blast
wenzelm@13142:   apply (simp add: image_Collect lex_prod_def)
wenzelm@13142:   apply auto
wenzelm@13142:     apply blast
wenzelm@13142:    apply (rename_tac a xys x xs' y ys')
wenzelm@13142:    apply (rule_tac x = "a # xys" in exI)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (case_tac xys)
wenzelm@13142:    apply simp_all
wenzelm@13114:   apply blast
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma lex_conv:
wenzelm@13142:   "lex r =
wenzelm@13142:     {(xs,ys). length xs = length ys \<and>
wenzelm@13142:       (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
wenzelm@13142:   by (force simp add: lex_def lexn_conv)
wenzelm@13114: 
wenzelm@13142: lemma wf_lexico [intro!]: "wf r ==> wf (lexico r)"
wenzelm@13142:   by (unfold lexico_def) blast
wenzelm@13114: 
wenzelm@13114: lemma lexico_conv:
wenzelm@13142:   "lexico r = {(xs,ys). length xs < length ys |
wenzelm@13142:       length xs = length ys \<and> (xs, ys) : lex r}"
wenzelm@13142:   by (simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def)
wenzelm@13114: 
wenzelm@13142: lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
wenzelm@13142:   by (simp add: lex_conv)
wenzelm@13114: 
wenzelm@13142: lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
wenzelm@13142:   by (simp add:lex_conv)
wenzelm@13114: 
wenzelm@13142: lemma Cons_in_lex [iff]:
wenzelm@13142:   "((x # xs, y # ys) : lex r) =
wenzelm@13142:     ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
wenzelm@13142:   apply (simp add: lex_conv)
wenzelm@13142:   apply (rule iffI)
wenzelm@13142:    prefer 2 apply (blast intro: Cons_eq_appendI)
wenzelm@13142:   apply clarify
wenzelm@13142:   apply (case_tac xys)
wenzelm@13142:    apply simp
wenzelm@13142:   apply simp
wenzelm@13142:   apply blast
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: subsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
wenzelm@13114: 
wenzelm@13142: lemma sublist_empty [simp]: "sublist xs {} = []"
wenzelm@13142:   by (auto simp add: sublist_def)
wenzelm@13114: 
wenzelm@13142: lemma sublist_nil [simp]: "sublist [] A = []"
wenzelm@13142:   by (auto simp add: sublist_def)
wenzelm@13114: 
wenzelm@13114: lemma sublist_shift_lemma:
wenzelm@13142:   "map fst [p:zip xs [i..i + length xs(] . snd p : A] =
wenzelm@13142:     map fst [p:zip xs [0..length xs(] . snd p + i : A]"
wenzelm@13142:   by (induct xs rule: rev_induct) (simp_all add: add_commute)
wenzelm@13114: 
wenzelm@13114: lemma sublist_append:
wenzelm@13142:     "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
wenzelm@13142:   apply (unfold sublist_def)
wenzelm@13142:   apply (induct l' rule: rev_induct)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
wenzelm@13142:   apply (simp add: add_commute)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: lemma sublist_Cons:
wenzelm@13142:     "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
wenzelm@13142:   apply (induct l rule: rev_induct)
wenzelm@13142:    apply (simp add: sublist_def)
wenzelm@13142:   apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13142: lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
wenzelm@13142:   by (simp add: sublist_Cons)
wenzelm@13114: 
wenzelm@13142: lemma sublist_upt_eq_take [simp]: "sublist l {..n(} = take n l"
wenzelm@13142:   apply (induct l rule: rev_induct)
wenzelm@13142:    apply simp
wenzelm@13142:   apply (simp split: nat_diff_split add: sublist_append)
wenzelm@13142:   done
wenzelm@13114: 
wenzelm@13114: 
wenzelm@13142: lemma take_Cons':
wenzelm@13142:     "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
wenzelm@13142:   by (cases n) simp_all
wenzelm@13114: 
wenzelm@13142: lemma drop_Cons':
wenzelm@13142:     "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
wenzelm@13142:   by (cases n) simp_all
wenzelm@13114: 
wenzelm@13142: lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
wenzelm@13142:   by (cases n) simp_all
wenzelm@13142: 
wenzelm@13142: lemmas [of "number_of v", standard, simp] =
wenzelm@13142:   take_Cons' drop_Cons' nth_Cons'
nipkow@3507: 
wenzelm@13122: end




