src/HOL/List.thy
changeset 13142 1ebd8ed5a1a0
parent 13124 6e1decd8a7a9
child 13145 59bc43b51aa2
     1.1 --- a/src/HOL/List.thy	Mon May 13 10:40:59 2002 +0200
     1.2 +++ b/src/HOL/List.thy	Mon May 13 11:05:27 2002 +0200
     1.3 @@ -8,54 +8,56 @@
     1.4  
     1.5  theory List = PreList:
     1.6  
     1.7 -datatype 'a list = Nil ("[]") | Cons 'a "'a list" (infixr "#" 65)
     1.8 +datatype 'a list =
     1.9 +    Nil    ("[]")
    1.10 +  | Cons 'a  "'a list"    (infixr "#" 65)
    1.11  
    1.12  consts
    1.13 -  "@"         :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"            (infixr 65)
    1.14 -  filter      :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
    1.15 -  concat      :: "'a list list \<Rightarrow> 'a list"
    1.16 -  foldl       :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b"
    1.17 -  foldr       :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b"
    1.18 -  hd          :: "'a list \<Rightarrow> 'a"
    1.19 -  tl          :: "'a list \<Rightarrow> 'a list"
    1.20 -  last        :: "'a list \<Rightarrow> 'a"
    1.21 -  butlast     :: "'a list \<Rightarrow> 'a list"
    1.22 -  set         :: "'a list \<Rightarrow> 'a set"
    1.23 -  list_all    :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<Rightarrow> bool)"
    1.24 -  list_all2   :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> bool"
    1.25 -  map         :: "('a\<Rightarrow>'b) \<Rightarrow> ('a list \<Rightarrow> 'b list)"
    1.26 -  mem         :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"                    (infixl 55)
    1.27 -  nth         :: "'a list \<Rightarrow> nat \<Rightarrow> 'a"			  (infixl "!" 100)
    1.28 -  list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list"
    1.29 -  take        :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
    1.30 -  drop        :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
    1.31 -  takeWhile   :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
    1.32 -  dropWhile   :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
    1.33 -  rev         :: "'a list \<Rightarrow> 'a list"
    1.34 -  zip	      :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a * 'b) list"
    1.35 -  upt         :: "nat \<Rightarrow> nat \<Rightarrow> nat list"                   ("(1[_../_'(])")
    1.36 -  remdups     :: "'a list \<Rightarrow> 'a list"
    1.37 -  null        :: "'a list \<Rightarrow> bool"
    1.38 -  "distinct"  :: "'a list \<Rightarrow> bool"
    1.39 -  replicate   :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list"
    1.40 +  "@"         :: "'a list => 'a list => 'a list"            (infixr 65)
    1.41 +  filter      :: "('a => bool) => 'a list => 'a list"
    1.42 +  concat      :: "'a list list => 'a list"
    1.43 +  foldl       :: "('b => 'a => 'b) => 'b => 'a list => 'b"
    1.44 +  foldr       :: "('a => 'b => 'b) => 'a list => 'b => 'b"
    1.45 +  hd          :: "'a list => 'a"
    1.46 +  tl          :: "'a list => 'a list"
    1.47 +  last        :: "'a list => 'a"
    1.48 +  butlast     :: "'a list => 'a list"
    1.49 +  set         :: "'a list => 'a set"
    1.50 +  list_all    :: "('a => bool) => ('a list => bool)"
    1.51 +  list_all2   :: "('a => 'b => bool) => 'a list => 'b list => bool"
    1.52 +  map         :: "('a=>'b) => ('a list => 'b list)"
    1.53 +  mem         :: "'a => 'a list => bool"                    (infixl 55)
    1.54 +  nth         :: "'a list => nat => 'a"                   (infixl "!" 100)
    1.55 +  list_update :: "'a list => nat => 'a => 'a list"
    1.56 +  take        :: "nat => 'a list => 'a list"
    1.57 +  drop        :: "nat => 'a list => 'a list"
    1.58 +  takeWhile   :: "('a => bool) => 'a list => 'a list"
    1.59 +  dropWhile   :: "('a => bool) => 'a list => 'a list"
    1.60 +  rev         :: "'a list => 'a list"
    1.61 +  zip         :: "'a list => 'b list => ('a * 'b) list"
    1.62 +  upt         :: "nat => nat => nat list"                   ("(1[_../_'(])")
    1.63 +  remdups     :: "'a list => 'a list"
    1.64 +  null        :: "'a list => bool"
    1.65 +  "distinct"  :: "'a list => bool"
    1.66 +  replicate   :: "nat => 'a => 'a list"
    1.67  
    1.68  nonterminals
    1.69    lupdbinds  lupdbind
    1.70  
    1.71  syntax
    1.72 -  (* list Enumeration *)
    1.73 -  "@list"     :: "args \<Rightarrow> 'a list"                          ("[(_)]")
    1.74 +  -- {* list Enumeration *}
    1.75 +  "@list"     :: "args => 'a list"                          ("[(_)]")
    1.76  
    1.77 -  (* Special syntax for filter *)
    1.78 -  "@filter"   :: "[pttrn, 'a list, bool] \<Rightarrow> 'a list"        ("(1[_:_./ _])")
    1.79 +  -- {* Special syntax for filter *}
    1.80 +  "@filter"   :: "[pttrn, 'a list, bool] => 'a list"        ("(1[_:_./ _])")
    1.81  
    1.82 -  (* list update *)
    1.83 -  "_lupdbind"      :: "['a, 'a] \<Rightarrow> lupdbind"            ("(2_ :=/ _)")
    1.84 -  ""               :: "lupdbind \<Rightarrow> lupdbinds"           ("_")
    1.85 -  "_lupdbinds"     :: "[lupdbind, lupdbinds] \<Rightarrow> lupdbinds" ("_,/ _")
    1.86 -  "_LUpdate"       :: "['a, lupdbinds] \<Rightarrow> 'a"           ("_/[(_)]" [900,0] 900)
    1.87 +  -- {* list update *}
    1.88 +  "_lupdbind"      :: "['a, 'a] => lupdbind"            ("(2_ :=/ _)")
    1.89 +  ""               :: "lupdbind => lupdbinds"           ("_")
    1.90 +  "_lupdbinds"     :: "[lupdbind, lupdbinds] => lupdbinds" ("_,/ _")
    1.91 +  "_LUpdate"       :: "['a, lupdbinds] => 'a"           ("_/[(_)]" [900,0] 900)
    1.92  
    1.93 -  upto        :: "nat \<Rightarrow> nat \<Rightarrow> nat list"                   ("(1[_../_])")
    1.94 +  upto        :: "nat => nat => nat list"                   ("(1[_../_])")
    1.95  
    1.96  translations
    1.97    "[x, xs]"     == "x#[xs]"
    1.98 @@ -69,31 +71,22 @@
    1.99  
   1.100  
   1.101  syntax (xsymbols)
   1.102 -  "@filter"   :: "[pttrn, 'a list, bool] \<Rightarrow> 'a list"        ("(1[_\<in>_ ./ _])")
   1.103 -
   1.104 -
   1.105 -consts
   1.106 -  lists        :: "'a set \<Rightarrow> 'a list set"
   1.107 -
   1.108 -inductive "lists A"
   1.109 -intros
   1.110 -Nil:  "[]: lists A"
   1.111 -Cons: "\<lbrakk> a: A;  l: lists A \<rbrakk> \<Longrightarrow> a#l : lists A"
   1.112 +  "@filter"   :: "[pttrn, 'a list, bool] => 'a list"        ("(1[_\<in>_ ./ _])")
   1.113  
   1.114  
   1.115 -(*Function "size" is overloaded for all datatypes.  Users may refer to the
   1.116 -  list version as "length".*)
   1.117 -syntax   length :: "'a list \<Rightarrow> nat"
   1.118 -translations  "length"  =>  "size:: _ list \<Rightarrow> nat"
   1.119 +text {*
   1.120 +  Function @{text size} is overloaded for all datatypes.  Users may
   1.121 +  refer to the list version as @{text length}. *}
   1.122 +
   1.123 +syntax length :: "'a list => nat"
   1.124 +translations "length" => "size :: _ list => nat"
   1.125  
   1.126 -(* translating size::list -> length *)
   1.127 -typed_print_translation
   1.128 -{*
   1.129 -let
   1.130 -fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
   1.131 -      Syntax.const "length" $ t
   1.132 -  | size_tr' _ _ _ = raise Match;
   1.133 -in [("size", size_tr')] end
   1.134 +typed_print_translation {*
   1.135 +  let
   1.136 +    fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
   1.137 +          Syntax.const "length" $ t
   1.138 +      | size_tr' _ _ _ = raise Match;
   1.139 +  in [("size", size_tr')] end
   1.140  *}
   1.141  
   1.142  primrec
   1.143 @@ -117,7 +110,7 @@
   1.144    "set (x#xs) = insert x (set xs)"
   1.145  primrec
   1.146    list_all_Nil:  "list_all P [] = True"
   1.147 -  list_all_Cons: "list_all P (x#xs) = (P(x) & list_all P xs)"
   1.148 +  list_all_Cons: "list_all P (x#xs) = (P(x) \<and> list_all P xs)"
   1.149  primrec
   1.150    "map f []     = []"
   1.151    "map f (x#xs) = f(x)#map f xs"
   1.152 @@ -141,22 +134,23 @@
   1.153    "concat(x#xs) = x @ concat(xs)"
   1.154  primrec
   1.155    drop_Nil:  "drop n [] = []"
   1.156 -  drop_Cons: "drop n (x#xs) = (case n of 0 \<Rightarrow> x#xs | Suc(m) \<Rightarrow> drop m xs)"
   1.157 -  (* Warning: simpset does not contain this definition but separate theorems 
   1.158 -     for n=0 / n=Suc k*)
   1.159 +  drop_Cons: "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
   1.160 +    -- {* Warning: simpset does not contain this definition *}
   1.161 +    -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
   1.162  primrec
   1.163    take_Nil:  "take n [] = []"
   1.164 -  take_Cons: "take n (x#xs) = (case n of 0 \<Rightarrow> [] | Suc(m) \<Rightarrow> x # take m xs)"
   1.165 -  (* Warning: simpset does not contain this definition but separate theorems 
   1.166 -     for n=0 / n=Suc k*)
   1.167 -primrec 
   1.168 -  nth_Cons:  "(x#xs)!n = (case n of 0 \<Rightarrow> x | (Suc k) \<Rightarrow> xs!k)"
   1.169 -  (* Warning: simpset does not contain this definition but separate theorems 
   1.170 -     for n=0 / n=Suc k*)
   1.171 +  take_Cons: "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
   1.172 +    -- {* Warning: simpset does not contain this definition *}
   1.173 +    -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
   1.174  primrec
   1.175 - "    [][i:=v] = []"
   1.176 - "(x#xs)[i:=v] = (case i of 0     \<Rightarrow> v # xs 
   1.177 -			  | Suc j \<Rightarrow> x # xs[j:=v])"
   1.178 +  nth_Cons:  "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
   1.179 +    -- {* Warning: simpset does not contain this definition *}
   1.180 +    -- {* but separate theorems for @{text "n = 0"} and @{text "n = Suc k"} *}
   1.181 +primrec
   1.182 +  "[][i:=v] = []"
   1.183 +  "(x#xs)[i:=v] =
   1.184 +    (case i of 0 => v # xs
   1.185 +    | Suc j => x # xs[j:=v])"
   1.186  primrec
   1.187    "takeWhile P []     = []"
   1.188    "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
   1.189 @@ -165,16 +159,15 @@
   1.190    "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
   1.191  primrec
   1.192    "zip xs []     = []"
   1.193 -zip_Cons:
   1.194 -  "zip xs (y#ys) = (case xs of [] \<Rightarrow> [] | z#zs \<Rightarrow> (z,y)#zip zs ys)"
   1.195 -  (* Warning: simpset does not contain this definition but separate theorems 
   1.196 -     for xs=[] / xs=z#zs *)
   1.197 +  zip_Cons: "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
   1.198 +    -- {* Warning: simpset does not contain this definition *}
   1.199 +    -- {* but separate theorems for @{text "xs = []"} and @{text "xs = z # zs"} *}
   1.200  primrec
   1.201    upt_0:   "[i..0(] = []"
   1.202    upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
   1.203  primrec
   1.204    "distinct []     = True"
   1.205 -  "distinct (x#xs) = (x ~: set xs & distinct xs)"
   1.206 +  "distinct (x#xs) = (x ~: set xs \<and> distinct xs)"
   1.207  primrec
   1.208    "remdups [] = []"
   1.209    "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
   1.210 @@ -183,180 +176,190 @@
   1.211    replicate_Suc: "replicate (Suc n) x = x # replicate n x"
   1.212  defs
   1.213   list_all2_def:
   1.214 - "list_all2 P xs ys == length xs = length ys & (!(x,y):set(zip xs ys). P x y)"
   1.215 + "list_all2 P xs ys == length xs = length ys \<and> (\<forall>(x, y) \<in> set (zip xs ys). P x y)"
   1.216  
   1.217  
   1.218 -(** Lexicographic orderings on lists **)
   1.219 +subsection {* Lexicographic orderings on lists *}
   1.220  
   1.221  consts
   1.222 - lexn :: "('a * 'a)set \<Rightarrow> nat \<Rightarrow> ('a list * 'a list)set"
   1.223 +  lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
   1.224  primrec
   1.225 -"lexn r 0       = {}"
   1.226 -"lexn r (Suc n) = (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
   1.227 -                  {(xs,ys). length xs = Suc n & length ys = Suc n}"
   1.228 +  "lexn r 0 = {}"
   1.229 +  "lexn r (Suc n) =
   1.230 +    (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
   1.231 +      {(xs,ys). length xs = Suc n \<and> length ys = Suc n}"
   1.232  
   1.233  constdefs
   1.234 -  lex :: "('a * 'a)set \<Rightarrow> ('a list * 'a list)set"
   1.235 -    "lex r == UN n. lexn r n"
   1.236 +  lex :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
   1.237 +  "lex r == \<Union>n. lexn r n"
   1.238  
   1.239 -  lexico :: "('a * 'a)set \<Rightarrow> ('a list * 'a list)set"
   1.240 -    "lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
   1.241 +  lexico :: "('a \<times> 'a) set => ('a list \<times> 'a list) set"
   1.242 +  "lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
   1.243  
   1.244 -  sublist :: "['a list, nat set] \<Rightarrow> 'a list"
   1.245 -    "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))"
   1.246 +  sublist :: "'a list => nat set => 'a list"
   1.247 +  "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))"
   1.248  
   1.249  
   1.250 -lemma not_Cons_self[simp]: "\<And>x. xs ~= x#xs"
   1.251 -by(induct_tac "xs", auto)
   1.252 +lemma not_Cons_self [simp]: "xs \<noteq> x # xs"
   1.253 +  by (induct xs) auto
   1.254  
   1.255 -lemmas not_Cons_self2[simp] = not_Cons_self[THEN not_sym]
   1.256 +lemmas not_Cons_self2 [simp] = not_Cons_self [symmetric]
   1.257  
   1.258 -lemma neq_Nil_conv: "(xs ~= []) = (? y ys. xs = y#ys)"
   1.259 -by(induct_tac "xs", auto)
   1.260 +lemma neq_Nil_conv: "(xs \<noteq> []) = (\<exists>y ys. xs = y # ys)"
   1.261 +  by (induct xs) auto
   1.262  
   1.263 -(* Induction over the length of a list: *)
   1.264 -(* "(!!xs. (!ys. length ys < length xs --> P ys) ==> P xs) ==> P(xs)" *)
   1.265 -lemmas length_induct = measure_induct[of length]
   1.266 +lemma length_induct:
   1.267 +    "(!!xs. \<forall>ys. length ys < length xs --> P ys ==> P xs) ==> P xs"
   1.268 +  by (rule measure_induct [of length]) rules
   1.269  
   1.270  
   1.271 -(** "lists": the list-forming operator over sets **)
   1.272 +subsection {* @{text lists}: the list-forming operator over sets *}
   1.273  
   1.274 -lemma lists_mono: "A<=B ==> lists A <= lists B"
   1.275 -apply(unfold lists.defs)
   1.276 -apply(blast intro!:lfp_mono)
   1.277 -done
   1.278 +consts lists :: "'a set => 'a list set"
   1.279 +inductive "lists A"
   1.280 +  intros
   1.281 +    Nil [intro!]: "[]: lists A"
   1.282 +    Cons [intro!]: "[| a: A;  l: lists A  |] ==> a#l : lists A"
   1.283  
   1.284 -inductive_cases listsE[elim!]: "x#l : lists A"
   1.285 -declare lists.intros[intro!]
   1.286 +inductive_cases listsE [elim!]: "x#l : lists A"
   1.287  
   1.288 -lemma lists_IntI[rule_format]:
   1.289 - "l: lists A ==> l: lists B --> l: lists (A Int B)"
   1.290 -apply(erule lists.induct)
   1.291 -apply blast+
   1.292 -done
   1.293 +lemma lists_mono: "A \<subseteq> B ==> lists A \<subseteq> lists B"
   1.294 +  by (unfold lists.defs) (blast intro!: lfp_mono)
   1.295  
   1.296 -lemma lists_Int_eq[simp]: "lists (A Int B) = lists A Int lists B"
   1.297 -apply(rule mono_Int[THEN equalityI])
   1.298 -apply(simp add:mono_def lists_mono)
   1.299 -apply(blast intro!: lists_IntI)
   1.300 -done
   1.301 +lemma lists_IntI [rule_format]:
   1.302 +    "l: lists A ==> l: lists B --> l: lists (A Int B)"
   1.303 +  apply (erule lists.induct)
   1.304 +  apply blast+
   1.305 +  done
   1.306 +
   1.307 +lemma lists_Int_eq [simp]: "lists (A \<inter> B) = lists A \<inter> lists B"
   1.308 +  apply (rule mono_Int [THEN equalityI])
   1.309 +  apply (simp add: mono_def lists_mono)
   1.310 +  apply (blast intro!: lists_IntI)
   1.311 +  done
   1.312  
   1.313 -lemma append_in_lists_conv[iff]:
   1.314 - "(xs@ys : lists A) = (xs : lists A & ys : lists A)"
   1.315 -by(induct_tac "xs", auto)
   1.316 +lemma append_in_lists_conv [iff]:
   1.317 +    "(xs @ ys : lists A) = (xs : lists A \<and> ys : lists A)"
   1.318 +  by (induct xs) auto
   1.319 +
   1.320 +
   1.321 +subsection {* @{text length} *}
   1.322  
   1.323 -(** length **)
   1.324 -(* needs to come before "@" because of thm append_eq_append_conv *)
   1.325 +text {*
   1.326 +  Needs to come before @{text "@"} because of theorem @{text
   1.327 +  append_eq_append_conv}.
   1.328 +*}
   1.329  
   1.330 -section "length"
   1.331 -
   1.332 -lemma length_append[simp]: "length(xs@ys) = length(xs)+length(ys)"
   1.333 -by(induct_tac "xs", auto)
   1.334 +lemma length_append [simp]: "length (xs @ ys) = length xs + length ys"
   1.335 +  by (induct xs) auto
   1.336  
   1.337 -lemma length_map[simp]: "length (map f xs) = length xs"
   1.338 -by(induct_tac "xs", auto)
   1.339 +lemma length_map [simp]: "length (map f xs) = length xs"
   1.340 +  by (induct xs) auto
   1.341  
   1.342 -lemma length_rev[simp]: "length(rev xs) = length(xs)"
   1.343 -by(induct_tac "xs", auto)
   1.344 +lemma length_rev [simp]: "length (rev xs) = length xs"
   1.345 +  by (induct xs) auto
   1.346  
   1.347 -lemma length_tl[simp]: "length(tl xs) = (length xs) - 1"
   1.348 -by(case_tac "xs", auto)
   1.349 +lemma length_tl [simp]: "length (tl xs) = length xs - 1"
   1.350 +  by (cases xs) auto
   1.351  
   1.352 -lemma length_0_conv[iff]: "(length xs = 0) = (xs = [])"
   1.353 -by(induct_tac "xs", auto)
   1.354 +lemma length_0_conv [iff]: "(length xs = 0) = (xs = [])"
   1.355 +  by (induct xs) auto
   1.356  
   1.357 -lemma length_greater_0_conv[iff]: "(0 < length xs) = (xs ~= [])"
   1.358 -by(induct_tac xs, auto)
   1.359 +lemma length_greater_0_conv [iff]: "(0 < length xs) = (xs \<noteq> [])"
   1.360 +  by (induct xs) auto
   1.361  
   1.362  lemma length_Suc_conv:
   1.363 - "(length xs = Suc n) = (? y ys. xs = y#ys & length ys = n)"
   1.364 -by(induct_tac "xs", auto)
   1.365 +    "(length xs = Suc n) = (\<exists>y ys. xs = y # ys \<and> length ys = n)"
   1.366 +  by (induct xs) auto
   1.367 +
   1.368  
   1.369 -(** @ - append **)
   1.370 +subsection {* @{text "@"} -- append *}
   1.371  
   1.372 -section "@ - append"
   1.373 +lemma append_assoc [simp]: "(xs @ ys) @ zs = xs @ (ys @ zs)"
   1.374 +  by (induct xs) auto
   1.375  
   1.376 -lemma append_assoc[simp]: "(xs@ys)@zs = xs@(ys@zs)"
   1.377 -by(induct_tac "xs", auto)
   1.378 +lemma append_Nil2 [simp]: "xs @ [] = xs"
   1.379 +  by (induct xs) auto
   1.380  
   1.381 -lemma append_Nil2[simp]: "xs @ [] = xs"
   1.382 -by(induct_tac "xs", auto)
   1.383 +lemma append_is_Nil_conv [iff]: "(xs @ ys = []) = (xs = [] \<and> ys = [])"
   1.384 +  by (induct xs) auto
   1.385  
   1.386 -lemma append_is_Nil_conv[iff]: "(xs@ys = []) = (xs=[] & ys=[])"
   1.387 -by(induct_tac "xs", auto)
   1.388 +lemma Nil_is_append_conv [iff]: "([] = xs @ ys) = (xs = [] \<and> ys = [])"
   1.389 +  by (induct xs) auto
   1.390  
   1.391 -lemma Nil_is_append_conv[iff]: "([] = xs@ys) = (xs=[] & ys=[])"
   1.392 -by(induct_tac "xs", auto)
   1.393 +lemma append_self_conv [iff]: "(xs @ ys = xs) = (ys = [])"
   1.394 +  by (induct xs) auto
   1.395  
   1.396 -lemma append_self_conv[iff]: "(xs @ ys = xs) = (ys=[])"
   1.397 -by(induct_tac "xs", auto)
   1.398 +lemma self_append_conv [iff]: "(xs = xs @ ys) = (ys = [])"
   1.399 +  by (induct xs) auto
   1.400  
   1.401 -lemma self_append_conv[iff]: "(xs = xs @ ys) = (ys=[])"
   1.402 -by(induct_tac "xs", auto)
   1.403 -
   1.404 -lemma append_eq_append_conv[rule_format,simp]:
   1.405 - "!ys. length xs = length ys | length us = length vs
   1.406 -       --> (xs@us = ys@vs) = (xs=ys & us=vs)"
   1.407 -apply(induct_tac "xs")
   1.408 - apply(rule allI)
   1.409 - apply(case_tac "ys")
   1.410 +lemma append_eq_append_conv [rule_format, simp]:
   1.411 + "\<forall>ys. length xs = length ys \<or> length us = length vs
   1.412 +       --> (xs@us = ys@vs) = (xs=ys \<and> us=vs)"
   1.413 +  apply (induct_tac xs)
   1.414 +   apply(rule allI)
   1.415 +   apply (case_tac ys)
   1.416 +    apply simp
   1.417 +   apply force
   1.418 +  apply (rule allI)
   1.419 +  apply (case_tac ys)
   1.420 +   apply force
   1.421    apply simp
   1.422 - apply force
   1.423 -apply(rule allI)
   1.424 -apply(case_tac "ys")
   1.425 - apply force
   1.426 -apply simp
   1.427 -done
   1.428 +  done
   1.429 +
   1.430 +lemma same_append_eq [iff]: "(xs @ ys = xs @ zs) = (ys = zs)"
   1.431 +  by simp
   1.432 +
   1.433 +lemma append1_eq_conv [iff]: "(xs @ [x] = ys @ [y]) = (xs = ys \<and> x = y)"
   1.434 +  by simp
   1.435  
   1.436 -lemma same_append_eq[iff]: "(xs @ ys = xs @ zs) = (ys=zs)"
   1.437 -by simp
   1.438 -
   1.439 -lemma append1_eq_conv[iff]: "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)" 
   1.440 -by simp
   1.441 +lemma append_same_eq [iff]: "(ys @ xs = zs @ xs) = (ys = zs)"
   1.442 +  by simp
   1.443  
   1.444 -lemma append_same_eq[iff]: "(ys @ xs = zs @ xs) = (ys=zs)"
   1.445 -by simp
   1.446 +lemma append_self_conv2 [iff]: "(xs @ ys = ys) = (xs = [])"
   1.447 +  using append_same_eq [of _ _ "[]"] by auto
   1.448  
   1.449 -lemma append_self_conv2[iff]: "(xs @ ys = ys) = (xs=[])"
   1.450 -by(insert append_same_eq[of _ _ "[]"], auto)
   1.451 +lemma self_append_conv2 [iff]: "(ys = xs @ ys) = (xs = [])"
   1.452 +  using append_same_eq [of "[]"] by auto
   1.453  
   1.454 -lemma self_append_conv2[iff]: "(ys = xs @ ys) = (xs=[])"
   1.455 -by(auto simp add: append_same_eq[of "[]", simplified])
   1.456 +lemma hd_Cons_tl [simp]: "xs \<noteq> [] ==> hd xs # tl xs = xs"
   1.457 +  by (induct xs) auto
   1.458  
   1.459 -lemma hd_Cons_tl[rule_format,simp]: "xs ~= [] --> hd xs # tl xs = xs"
   1.460 -by(induct_tac "xs", auto)
   1.461 +lemma hd_append: "hd (xs @ ys) = (if xs = [] then hd ys else hd xs)"
   1.462 +  by (induct xs) auto
   1.463  
   1.464 -lemma hd_append: "hd(xs@ys) = (if xs=[] then hd ys else hd xs)"
   1.465 -by(induct_tac "xs", auto)
   1.466 +lemma hd_append2 [simp]: "xs \<noteq> [] ==> hd (xs @ ys) = hd xs"
   1.467 +  by (simp add: hd_append split: list.split)
   1.468  
   1.469 -lemma hd_append2[simp]: "xs ~= [] ==> hd(xs @ ys) = hd xs"
   1.470 -by(simp add: hd_append split: list.split)
   1.471 +lemma tl_append: "tl (xs @ ys) = (case xs of [] => tl ys | z#zs => zs @ ys)"
   1.472 +  by (simp split: list.split)
   1.473  
   1.474 -lemma tl_append: "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)"
   1.475 -by(simp split: list.split)
   1.476 +lemma tl_append2 [simp]: "xs \<noteq> [] ==> tl (xs @ ys) = tl xs @ ys"
   1.477 +  by (simp add: tl_append split: list.split)
   1.478  
   1.479 -lemma tl_append2[simp]: "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys"
   1.480 -by(simp add: tl_append split: list.split)
   1.481  
   1.482 -(* trivial rules for solving @-equations automatically *)
   1.483 +text {* Trivial rules for solving @{text "@"}-equations automatically. *}
   1.484  
   1.485  lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
   1.486 -by simp
   1.487 +  by simp
   1.488  
   1.489 -lemma Cons_eq_appendI: "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs"
   1.490 -by(drule sym, simp)
   1.491 +lemma Cons_eq_appendI:
   1.492 +    "[| x # xs1 = ys; xs = xs1 @ zs |] ==> x # xs = ys @ zs"
   1.493 +  by (drule sym) simp
   1.494  
   1.495 -lemma append_eq_appendI: "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us"
   1.496 -by(drule sym, simp)
   1.497 +lemma append_eq_appendI:
   1.498 +    "[| xs @ xs1 = zs; ys = xs1 @ us |] ==> xs @ ys = zs @ us"
   1.499 +  by (drule sym) simp
   1.500  
   1.501  
   1.502 -(***
   1.503 -Simplification procedure for all list equalities.
   1.504 -Currently only tries to rearrange @ to see if
   1.505 -- both lists end in a singleton list,
   1.506 -- or both lists end in the same list.
   1.507 -***)
   1.508 -ML_setup{*
   1.509 +text {*
   1.510 +  Simplification procedure for all list equalities.
   1.511 +  Currently only tries to rearrange @{text "@"} to see if
   1.512 +  - both lists end in a singleton list,
   1.513 +  - or both lists end in the same list.
   1.514 +*}
   1.515 +
   1.516 +ML_setup {*
   1.517  local
   1.518  
   1.519  val append_assoc = thm "append_assoc";
   1.520 @@ -415,967 +418,947 @@
   1.521  *}
   1.522  
   1.523  
   1.524 -(** map **)
   1.525 +subsection {* @{text map} *}
   1.526  
   1.527 -section "map"
   1.528 +lemma map_ext: "(!!x. x : set xs --> f x = g x) ==> map f xs = map g xs"
   1.529 +  by (induct xs) simp_all
   1.530  
   1.531 -lemma map_ext: "(\<And>x. x : set xs \<longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g xs"
   1.532 -by (induct xs, simp_all)
   1.533 +lemma map_ident [simp]: "map (\<lambda>x. x) = (\<lambda>xs. xs)"
   1.534 +  by (rule ext, induct_tac xs) auto
   1.535  
   1.536 -lemma map_ident[simp]: "map (%x. x) = (%xs. xs)"
   1.537 -by(rule ext, induct_tac "xs", auto)
   1.538 +lemma map_append [simp]: "map f (xs @ ys) = map f xs @ map f ys"
   1.539 +  by (induct xs) auto
   1.540  
   1.541 -lemma map_append[simp]: "map f (xs@ys) = map f xs @ map f ys"
   1.542 -by(induct_tac "xs", auto)
   1.543 +lemma map_compose: "map (f o g) xs = map f (map g xs)"
   1.544 +  by (induct xs) (auto simp add: o_def)
   1.545  
   1.546 -lemma map_compose(*[simp]*): "map (f o g) xs = map f (map g xs)"
   1.547 -by(unfold o_def, induct_tac "xs", auto)
   1.548 +lemma rev_map: "rev (map f xs) = map f (rev xs)"
   1.549 +  by (induct xs) auto
   1.550  
   1.551 -lemma rev_map: "rev(map f xs) = map f (rev xs)"
   1.552 -by(induct_tac xs, auto)
   1.553 -
   1.554 -(* a congruence rule for map: *)
   1.555  lemma map_cong:
   1.556 - "xs=ys ==> (!!x. x : set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys"
   1.557 -by (clarify, induct ys, auto)
   1.558 +  "xs = ys ==> (!!x. x : set ys ==> f x = g x) ==> map f xs = map g ys"
   1.559 +  -- {* a congruence rule for @{text map} *}
   1.560 +  by (clarify, induct ys) auto
   1.561  
   1.562 -lemma map_is_Nil_conv[iff]: "(map f xs = []) = (xs = [])"
   1.563 -by(case_tac xs, auto)
   1.564 +lemma map_is_Nil_conv [iff]: "(map f xs = []) = (xs = [])"
   1.565 +  by (cases xs) auto
   1.566  
   1.567 -lemma Nil_is_map_conv[iff]: "([] = map f xs) = (xs = [])"
   1.568 -by(case_tac xs, auto)
   1.569 +lemma Nil_is_map_conv [iff]: "([] = map f xs) = (xs = [])"
   1.570 +  by (cases xs) auto
   1.571  
   1.572  lemma map_eq_Cons:
   1.573 - "(map f xs = y#ys) = (? x xs'. xs = x#xs' & f x = y & map f xs' = ys)"
   1.574 -by(case_tac xs, auto)
   1.575 +  "(map f xs = y # ys) = (\<exists>x xs'. xs = x # xs' \<and> f x = y \<and> map f xs' = ys)"
   1.576 +  by (cases xs) auto
   1.577  
   1.578  lemma map_injective:
   1.579 - "\<And>xs. map f xs = map f ys \<Longrightarrow> (!x y. f x = f y --> x=y) \<Longrightarrow> xs=ys"
   1.580 -by(induct "ys", simp, fastsimp simp add:map_eq_Cons)
   1.581 +    "!!xs. map f xs = map f ys ==> (\<forall>x y. f x = f y --> x = y) ==> xs = ys"
   1.582 +  by (induct ys) (auto simp add: map_eq_Cons)
   1.583  
   1.584  lemma inj_mapI: "inj f ==> inj (map f)"
   1.585 -by(blast dest:map_injective injD intro:injI)
   1.586 +  by (rules dest: map_injective injD intro: injI)
   1.587  
   1.588  lemma inj_mapD: "inj (map f) ==> inj f"
   1.589 -apply(unfold inj_on_def)
   1.590 -apply clarify
   1.591 -apply(erule_tac x = "[x]" in ballE)
   1.592 - apply(erule_tac x = "[y]" in ballE)
   1.593 -  apply simp
   1.594 - apply blast
   1.595 -apply blast
   1.596 -done
   1.597 +  apply (unfold inj_on_def)
   1.598 +  apply clarify
   1.599 +  apply (erule_tac x = "[x]" in ballE)
   1.600 +   apply (erule_tac x = "[y]" in ballE)
   1.601 +    apply simp
   1.602 +   apply blast
   1.603 +  apply blast
   1.604 +  done
   1.605  
   1.606  lemma inj_map: "inj (map f) = inj f"
   1.607 -by(blast dest:inj_mapD intro:inj_mapI)
   1.608 +  by (blast dest: inj_mapD intro: inj_mapI)
   1.609  
   1.610 -(** rev **)
   1.611  
   1.612 -section "rev"
   1.613 +subsection {* @{text rev} *}
   1.614  
   1.615 -lemma rev_append[simp]: "rev(xs@ys) = rev(ys) @ rev(xs)"
   1.616 -by(induct_tac xs, auto)
   1.617 +lemma rev_append [simp]: "rev (xs @ ys) = rev ys @ rev xs"
   1.618 +  by (induct xs) auto
   1.619  
   1.620 -lemma rev_rev_ident[simp]: "rev(rev xs) = xs"
   1.621 -by(induct_tac xs, auto)
   1.622 +lemma rev_rev_ident [simp]: "rev (rev xs) = xs"
   1.623 +  by (induct xs) auto
   1.624  
   1.625 -lemma rev_is_Nil_conv[iff]: "(rev xs = []) = (xs = [])"
   1.626 -by(induct_tac xs, auto)
   1.627 +lemma rev_is_Nil_conv [iff]: "(rev xs = []) = (xs = [])"
   1.628 +  by (induct xs) auto
   1.629  
   1.630 -lemma Nil_is_rev_conv[iff]: "([] = rev xs) = (xs = [])"
   1.631 -by(induct_tac xs, auto)
   1.632 +lemma Nil_is_rev_conv [iff]: "([] = rev xs) = (xs = [])"
   1.633 +  by (induct xs) auto
   1.634  
   1.635 -lemma rev_is_rev_conv[iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
   1.636 -apply(induct "xs" )
   1.637 - apply force
   1.638 -apply(case_tac ys)
   1.639 - apply simp
   1.640 -apply force
   1.641 -done
   1.642 +lemma rev_is_rev_conv [iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
   1.643 +  apply (induct xs)
   1.644 +   apply force
   1.645 +  apply (case_tac ys)
   1.646 +   apply simp
   1.647 +  apply force
   1.648 +  done
   1.649  
   1.650 -lemma rev_induct: "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs"
   1.651 -apply(subst rev_rev_ident[symmetric])
   1.652 -apply(rule_tac list = "rev xs" in list.induct, simp_all)
   1.653 -done
   1.654 +lemma rev_induct: "[| P []; !!x xs. P xs ==> P (xs @ [x]) |] ==> P xs"
   1.655 +  apply(subst rev_rev_ident[symmetric])
   1.656 +  apply(rule_tac list = "rev xs" in list.induct, simp_all)
   1.657 +  done
   1.658  
   1.659 -(* Instead of (rev_induct_tac xs) use (induct_tac xs rule: rev_induct) *)
   1.660 +ML {* val rev_induct_tac = induct_thm_tac (thm "rev_induct") *}  -- "compatibility"
   1.661  
   1.662 -lemma rev_exhaust: "(xs = [] \<Longrightarrow> P) \<Longrightarrow>  (!!ys y. xs = ys@[y] \<Longrightarrow> P) \<Longrightarrow> P"
   1.663 -by(induct xs rule: rev_induct, auto)
   1.664 +lemma rev_exhaust: "(xs = [] ==> P) ==>  (!!ys y. xs = ys @ [y] ==> P) ==> P"
   1.665 +  by (induct xs rule: rev_induct) auto
   1.666  
   1.667  
   1.668 -(** set **)
   1.669 +subsection {* @{text set} *}
   1.670  
   1.671 -section "set"
   1.672 +lemma finite_set [iff]: "finite (set xs)"
   1.673 +  by (induct xs) auto
   1.674  
   1.675 -lemma finite_set[iff]: "finite (set xs)"
   1.676 -by(induct_tac xs, auto)
   1.677 +lemma set_append [simp]: "set (xs @ ys) = (set xs \<union> set ys)"
   1.678 +  by (induct xs) auto
   1.679  
   1.680 -lemma set_append[simp]: "set (xs@ys) = (set xs Un set ys)"
   1.681 -by(induct_tac xs, auto)
   1.682 +lemma set_subset_Cons: "set xs \<subseteq> set (x # xs)"
   1.683 +  by auto
   1.684  
   1.685 -lemma set_subset_Cons: "set xs \<subseteq> set (x#xs)"
   1.686 -by auto
   1.687 +lemma set_empty [iff]: "(set xs = {}) = (xs = [])"
   1.688 +  by (induct xs) auto
   1.689  
   1.690 -lemma set_empty[iff]: "(set xs = {}) = (xs = [])"
   1.691 -by(induct_tac xs, auto)
   1.692 +lemma set_rev [simp]: "set (rev xs) = set xs"
   1.693 +  by (induct xs) auto
   1.694  
   1.695 -lemma set_rev[simp]: "set(rev xs) = set(xs)"
   1.696 -by(induct_tac xs, auto)
   1.697 +lemma set_map [simp]: "set (map f xs) = f`(set xs)"
   1.698 +  by (induct xs) auto
   1.699  
   1.700 -lemma set_map[simp]: "set(map f xs) = f`(set xs)"
   1.701 -by(induct_tac xs, auto)
   1.702 +lemma set_filter [simp]: "set (filter P xs) = {x. x : set xs \<and> P x}"
   1.703 +  by (induct xs) auto
   1.704  
   1.705 -lemma set_filter[simp]: "set(filter P xs) = {x. x : set xs & P x}"
   1.706 -by(induct_tac xs, auto)
   1.707 -
   1.708 -lemma set_upt[simp]: "set[i..j(] = {k. i <= k & k < j}"
   1.709 -apply(induct_tac j)
   1.710 - apply simp_all
   1.711 -apply(erule ssubst)
   1.712 -apply auto
   1.713 -apply arith
   1.714 -done
   1.715 +lemma set_upt [simp]: "set[i..j(] = {k. i \<le> k \<and> k < j}"
   1.716 +  apply (induct j)
   1.717 +   apply simp_all
   1.718 +  apply(erule ssubst)
   1.719 +  apply auto
   1.720 +  apply arith
   1.721 +  done
   1.722  
   1.723 -lemma in_set_conv_decomp: "(x : set xs) = (? ys zs. xs = ys@x#zs)"
   1.724 -apply(induct_tac "xs")
   1.725 - apply simp
   1.726 -apply simp
   1.727 -apply(rule iffI)
   1.728 - apply(blast intro: eq_Nil_appendI Cons_eq_appendI)
   1.729 -apply(erule exE)+
   1.730 -apply(case_tac "ys")
   1.731 -apply auto
   1.732 -done
   1.733 +lemma in_set_conv_decomp: "(x : set xs) = (\<exists>ys zs. xs = ys @ x # zs)"
   1.734 +  apply (induct xs)
   1.735 +   apply simp
   1.736 +  apply simp
   1.737 +  apply (rule iffI)
   1.738 +   apply (blast intro: eq_Nil_appendI Cons_eq_appendI)
   1.739 +  apply (erule exE)+
   1.740 +  apply (case_tac ys)
   1.741 +  apply auto
   1.742 +  done
   1.743 +
   1.744 +lemma in_lists_conv_set: "(xs : lists A) = (\<forall>x \<in> set xs. x : A)"
   1.745 +  -- {* eliminate @{text lists} in favour of @{text set} *}
   1.746 +  by (induct xs) auto
   1.747 +
   1.748 +lemma in_listsD [dest!]: "xs \<in> lists A ==> \<forall>x\<in>set xs. x \<in> A"
   1.749 +  by (rule in_lists_conv_set [THEN iffD1])
   1.750 +
   1.751 +lemma in_listsI [intro!]: "\<forall>x\<in>set xs. x \<in> A ==> xs \<in> lists A"
   1.752 +  by (rule in_lists_conv_set [THEN iffD2])
   1.753  
   1.754  
   1.755 -(* eliminate `lists' in favour of `set' *)
   1.756 -
   1.757 -lemma in_lists_conv_set: "(xs : lists A) = (!x : set xs. x : A)"
   1.758 -by(induct_tac xs, auto)
   1.759 -
   1.760 -lemmas in_listsD[dest!] = in_lists_conv_set[THEN iffD1]
   1.761 -lemmas in_listsI[intro!] = in_lists_conv_set[THEN iffD2]
   1.762 -
   1.763 -
   1.764 -(** mem **)
   1.765 - 
   1.766 -section "mem"
   1.767 +subsection {* @{text mem} *}
   1.768  
   1.769  lemma set_mem_eq: "(x mem xs) = (x : set xs)"
   1.770 -by(induct_tac xs, auto)
   1.771 +  by (induct xs) auto
   1.772  
   1.773  
   1.774 -(** list_all **)
   1.775 -
   1.776 -section "list_all"
   1.777 +subsection {* @{text list_all} *}
   1.778  
   1.779 -lemma list_all_conv: "list_all P xs = (!x:set xs. P x)"
   1.780 -by(induct_tac xs, auto)
   1.781 +lemma list_all_conv: "list_all P xs = (\<forall>x \<in> set xs. P x)"
   1.782 +  by (induct xs) auto
   1.783  
   1.784 -lemma list_all_append[simp]:
   1.785 - "list_all P (xs@ys) = (list_all P xs & list_all P ys)"
   1.786 -by(induct_tac xs, auto)
   1.787 +lemma list_all_append [simp]:
   1.788 +    "list_all P (xs @ ys) = (list_all P xs \<and> list_all P ys)"
   1.789 +  by (induct xs) auto
   1.790  
   1.791  
   1.792 -(** filter **)
   1.793 -
   1.794 -section "filter"
   1.795 +subsection {* @{text filter} *}
   1.796  
   1.797 -lemma filter_append[simp]: "filter P (xs@ys) = filter P xs @ filter P ys"
   1.798 -by(induct_tac xs, auto)
   1.799 +lemma filter_append [simp]: "filter P (xs @ ys) = filter P xs @ filter P ys"
   1.800 +  by (induct xs) auto
   1.801  
   1.802 -lemma filter_filter[simp]: "filter P (filter Q xs) = filter (%x. Q x & P x) xs"
   1.803 -by(induct_tac xs, auto)
   1.804 +lemma filter_filter [simp]: "filter P (filter Q xs) = filter (\<lambda>x. Q x \<and> P x) xs"
   1.805 +  by (induct xs) auto
   1.806  
   1.807 -lemma filter_True[simp]: "!x : set xs. P x \<Longrightarrow> filter P xs = xs"
   1.808 -by(induct xs, auto)
   1.809 +lemma filter_True [simp]: "\<forall>x \<in> set xs. P x ==> filter P xs = xs"
   1.810 +  by (induct xs) auto
   1.811  
   1.812 -lemma filter_False[simp]: "!x : set xs. ~P x \<Longrightarrow> filter P xs = []"
   1.813 -by(induct xs, auto)
   1.814 +lemma filter_False [simp]: "\<forall>x \<in> set xs. \<not> P x ==> filter P xs = []"
   1.815 +  by (induct xs) auto
   1.816  
   1.817 -lemma length_filter[simp]: "length (filter P xs) <= length xs"
   1.818 -by(induct xs, auto simp add: le_SucI)
   1.819 +lemma length_filter [simp]: "length (filter P xs) \<le> length xs"
   1.820 +  by (induct xs) (auto simp add: le_SucI)
   1.821  
   1.822 -lemma filter_is_subset[simp]: "set (filter P xs) <= set xs"
   1.823 -by auto
   1.824 +lemma filter_is_subset [simp]: "set (filter P xs) \<le> set xs"
   1.825 +  by auto
   1.826  
   1.827  
   1.828 -section "concat"
   1.829 +subsection {* @{text concat} *}
   1.830  
   1.831 -lemma concat_append[simp]: "concat(xs@ys) = concat(xs)@concat(ys)"
   1.832 -by(induct xs, auto)
   1.833 +lemma concat_append [simp]: "concat (xs @ ys) = concat xs @ concat ys"
   1.834 +  by (induct xs) auto
   1.835  
   1.836 -lemma concat_eq_Nil_conv[iff]: "(concat xss = []) = (!xs:set xss. xs=[])"
   1.837 -by(induct xss, auto)
   1.838 +lemma concat_eq_Nil_conv [iff]: "(concat xss = []) = (\<forall>xs \<in> set xss. xs = [])"
   1.839 +  by (induct xss) auto
   1.840  
   1.841 -lemma Nil_eq_concat_conv[iff]: "([] = concat xss) = (!xs:set xss. xs=[])"
   1.842 -by(induct xss, auto)
   1.843 +lemma Nil_eq_concat_conv [iff]: "([] = concat xss) = (\<forall>xs \<in> set xss. xs = [])"
   1.844 +  by (induct xss) auto
   1.845  
   1.846 -lemma set_concat[simp]: "set(concat xs) = Union(set ` set xs)"
   1.847 -by(induct xs, auto)
   1.848 +lemma set_concat [simp]: "set (concat xs) = \<Union>(set ` set xs)"
   1.849 +  by (induct xs) auto
   1.850  
   1.851 -lemma map_concat: "map f (concat xs) = concat (map (map f) xs)" 
   1.852 -by(induct xs, auto)
   1.853 +lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
   1.854 +  by (induct xs) auto
   1.855  
   1.856 -lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)" 
   1.857 -by(induct xs, auto)
   1.858 +lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
   1.859 +  by (induct xs) auto
   1.860  
   1.861 -lemma rev_concat: "rev(concat xs) = concat (map rev (rev xs))"
   1.862 -by(induct xs, auto)
   1.863 +lemma rev_concat: "rev (concat xs) = concat (map rev (rev xs))"
   1.864 +  by (induct xs) auto
   1.865  
   1.866 -(** nth **)
   1.867  
   1.868 -section "nth"
   1.869 +subsection {* @{text nth} *}
   1.870  
   1.871 -lemma nth_Cons_0[simp]: "(x#xs)!0 = x"
   1.872 -by auto
   1.873 +lemma nth_Cons_0 [simp]: "(x # xs)!0 = x"
   1.874 +  by auto
   1.875  
   1.876 -lemma nth_Cons_Suc[simp]: "(x#xs)!(Suc n) = xs!n"
   1.877 -by auto
   1.878 +lemma nth_Cons_Suc [simp]: "(x # xs)!(Suc n) = xs!n"
   1.879 +  by auto
   1.880  
   1.881 -declare nth.simps[simp del]
   1.882 +declare nth.simps [simp del]
   1.883  
   1.884  lemma nth_append:
   1.885 - "!!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
   1.886 -apply(induct "xs")
   1.887 - apply simp
   1.888 -apply(case_tac "n" )
   1.889 - apply auto
   1.890 -done
   1.891 +    "!!n. (xs @ ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
   1.892 +  apply(induct "xs")
   1.893 +   apply simp
   1.894 +  apply (case_tac n)
   1.895 +   apply auto
   1.896 +  done
   1.897  
   1.898 -lemma nth_map[simp]: "!!n. n < length xs \<Longrightarrow> (map f xs)!n = f(xs!n)"
   1.899 -apply(induct "xs" )
   1.900 - apply simp
   1.901 -apply(case_tac "n")
   1.902 - apply auto
   1.903 -done
   1.904 +lemma nth_map [simp]: "!!n. n < length xs ==> (map f xs)!n = f(xs!n)"
   1.905 +  apply(induct xs)
   1.906 +   apply simp
   1.907 +  apply (case_tac n)
   1.908 +   apply auto
   1.909 +  done
   1.910  
   1.911 -lemma set_conv_nth: "set xs = {xs!i |i. i < length xs}"
   1.912 -apply(induct_tac "xs")
   1.913 - apply simp
   1.914 -apply simp
   1.915 -apply safe
   1.916 -  apply(rule_tac x = 0 in exI)
   1.917 +lemma set_conv_nth: "set xs = {xs!i | i. i < length xs}"
   1.918 +  apply (induct_tac xs)
   1.919 +   apply simp
   1.920    apply simp
   1.921 - apply(rule_tac x = "Suc i" in exI)
   1.922 - apply simp
   1.923 -apply(case_tac "i")
   1.924 - apply simp
   1.925 -apply(rename_tac "j")
   1.926 -apply(rule_tac x = "j" in exI)
   1.927 -apply simp
   1.928 -done
   1.929 +  apply safe
   1.930 +    apply (rule_tac x = 0 in exI)
   1.931 +    apply simp
   1.932 +   apply (rule_tac x = "Suc i" in exI)
   1.933 +   apply simp
   1.934 +  apply (case_tac i)
   1.935 +   apply simp
   1.936 +  apply (rename_tac j)
   1.937 +  apply (rule_tac x = j in exI)
   1.938 +  apply simp
   1.939 +  done
   1.940  
   1.941 -lemma list_ball_nth: "\<lbrakk> n < length xs; !x : set xs. P x \<rbrakk> \<Longrightarrow> P(xs!n)"
   1.942 -by(simp add:set_conv_nth, blast)
   1.943 +lemma list_ball_nth: "[| n < length xs; !x : set xs. P x  |] ==> P(xs!n)"
   1.944 +  by (auto simp add: set_conv_nth)
   1.945  
   1.946 -lemma nth_mem[simp]: "n < length xs ==> xs!n : set xs"
   1.947 -by(simp add:set_conv_nth, blast)
   1.948 +lemma nth_mem [simp]: "n < length xs ==> xs!n : set xs"
   1.949 +  by (auto simp add: set_conv_nth)
   1.950  
   1.951  lemma all_nth_imp_all_set:
   1.952 - "\<lbrakk> !i < length xs. P(xs!i); x : set xs \<rbrakk> \<Longrightarrow> P x"
   1.953 -by(simp add:set_conv_nth, blast)
   1.954 +    "[| !i < length xs. P(xs!i); x : set xs  |] ==> P x"
   1.955 +  by (auto simp add: set_conv_nth)
   1.956  
   1.957  lemma all_set_conv_all_nth:
   1.958 - "(!x : set xs. P x) = (!i. i<length xs --> P (xs ! i))"
   1.959 -by(simp add:set_conv_nth, blast)
   1.960 +    "(\<forall>x \<in> set xs. P x) = (\<forall>i. i < length xs --> P (xs ! i))"
   1.961 +  by (auto simp add: set_conv_nth)
   1.962  
   1.963  
   1.964 -(** list update **)
   1.965 +subsection {* @{text list_update} *}
   1.966  
   1.967 -section "list update"
   1.968 -
   1.969 -lemma length_list_update[simp]: "!!i. length(xs[i:=x]) = length xs"
   1.970 -by(induct xs, simp, simp split:nat.split)
   1.971 +lemma length_list_update [simp]: "!!i. length(xs[i:=x]) = length xs"
   1.972 +  by (induct xs) (auto split: nat.split)
   1.973  
   1.974  lemma nth_list_update:
   1.975 - "!!i j. i < length xs  \<Longrightarrow> (xs[i:=x])!j = (if i=j then x else xs!j)"
   1.976 -by(induct xs, simp, auto simp add:nth_Cons split:nat.split)
   1.977 +    "!!i j. i < length xs  ==> (xs[i:=x])!j = (if i = j then x else xs!j)"
   1.978 +  by (induct xs) (auto simp add: nth_Cons split: nat.split)
   1.979  
   1.980 -lemma nth_list_update_eq[simp]: "i < length xs  ==> (xs[i:=x])!i = x"
   1.981 -by(simp add:nth_list_update)
   1.982 +lemma nth_list_update_eq [simp]: "i < length xs ==> (xs[i:=x])!i = x"
   1.983 +  by (simp add: nth_list_update)
   1.984  
   1.985 -lemma nth_list_update_neq[simp]: "!!i j. i ~= j \<Longrightarrow> xs[i:=x]!j = xs!j"
   1.986 -by(induct xs, simp, auto simp add:nth_Cons split:nat.split)
   1.987 +lemma nth_list_update_neq [simp]: "!!i j. i \<noteq> j ==> xs[i:=x]!j = xs!j"
   1.988 +  by (induct xs) (auto simp add: nth_Cons split: nat.split)
   1.989  
   1.990 -lemma list_update_overwrite[simp]:
   1.991 - "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
   1.992 -by(induct xs, simp, simp split:nat.split)
   1.993 +lemma list_update_overwrite [simp]:
   1.994 +    "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
   1.995 +  by (induct xs) (auto split: nat.split)
   1.996  
   1.997  lemma list_update_same_conv:
   1.998 - "!!i. i < length xs \<Longrightarrow> (xs[i := x] = xs) = (xs!i = x)"
   1.999 -by(induct xs, simp, simp split:nat.split, blast)
  1.1000 +    "!!i. i < length xs ==> (xs[i := x] = xs) = (xs!i = x)"
  1.1001 +  by (induct xs) (auto split: nat.split)
  1.1002  
  1.1003  lemma update_zip:
  1.1004 -"!!i xy xs. length xs = length ys \<Longrightarrow>
  1.1005 +  "!!i xy xs. length xs = length ys ==>
  1.1006      (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
  1.1007 -by(induct ys, auto, case_tac xs, auto split:nat.split)
  1.1008 +  by (induct ys) (auto, case_tac xs, auto split: nat.split)
  1.1009  
  1.1010  lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"
  1.1011 -by(induct xs, simp, simp split:nat.split, fast)
  1.1012 +  by (induct xs) (auto split: nat.split)
  1.1013  
  1.1014  lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
  1.1015 -by(fast dest!:set_update_subset_insert[THEN subsetD])
  1.1016 +  by (blast dest!: set_update_subset_insert [THEN subsetD])
  1.1017  
  1.1018  
  1.1019 -(** last & butlast **)
  1.1020 +subsection {* @{text last} and @{text butlast} *}
  1.1021  
  1.1022 -section "last / butlast"
  1.1023 +lemma last_snoc [simp]: "last (xs @ [x]) = x"
  1.1024 +  by (induct xs) auto
  1.1025  
  1.1026 -lemma last_snoc[simp]: "last(xs@[x]) = x"
  1.1027 -by(induct xs, auto)
  1.1028 +lemma butlast_snoc [simp]: "butlast (xs @ [x]) = xs"
  1.1029 +  by (induct xs) auto
  1.1030  
  1.1031 -lemma butlast_snoc[simp]:"butlast(xs@[x]) = xs"
  1.1032 -by(induct xs, auto)
  1.1033 -
  1.1034 -lemma length_butlast[simp]: "length(butlast xs) = length xs - 1"
  1.1035 -by(induct xs rule:rev_induct, auto)
  1.1036 +lemma length_butlast [simp]: "length (butlast xs) = length xs - 1"
  1.1037 +  by (induct xs rule: rev_induct) auto
  1.1038  
  1.1039  lemma butlast_append:
  1.1040 - "!!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)"
  1.1041 -by(induct xs, auto)
  1.1042 +    "!!ys. butlast (xs @ ys) = (if ys = [] then butlast xs else xs @ butlast ys)"
  1.1043 +  by (induct xs) auto
  1.1044  
  1.1045 -lemma append_butlast_last_id[simp]:
  1.1046 - "xs ~= [] --> butlast xs @ [last xs] = xs"
  1.1047 -by(induct xs, auto)
  1.1048 +lemma append_butlast_last_id [simp]:
  1.1049 +    "xs \<noteq> [] ==> butlast xs @ [last xs] = xs"
  1.1050 +  by (induct xs) auto
  1.1051  
  1.1052 -lemma in_set_butlastD: "x:set(butlast xs) ==> x:set xs"
  1.1053 -by(induct xs, auto split:split_if_asm)
  1.1054 +lemma in_set_butlastD: "x : set (butlast xs) ==> x : set xs"
  1.1055 +  by (induct xs) (auto split: split_if_asm)
  1.1056  
  1.1057  lemma in_set_butlast_appendI:
  1.1058 - "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))"
  1.1059 -by(auto dest:in_set_butlastD simp add:butlast_append)
  1.1060 +    "x : set (butlast xs) | x : set (butlast ys) ==> x : set (butlast (xs @ ys))"
  1.1061 +  by (auto dest: in_set_butlastD simp add: butlast_append)
  1.1062  
  1.1063 -(** take  & drop **)
  1.1064 -section "take & drop"
  1.1065 +
  1.1066 +subsection {* @{text take} and @{text drop} *}
  1.1067  
  1.1068 -lemma take_0[simp]: "take 0 xs = []"
  1.1069 -by(induct xs, auto)
  1.1070 +lemma take_0 [simp]: "take 0 xs = []"
  1.1071 +  by (induct xs) auto
  1.1072  
  1.1073 -lemma drop_0[simp]: "drop 0 xs = xs"
  1.1074 -by(induct xs, auto)
  1.1075 +lemma drop_0 [simp]: "drop 0 xs = xs"
  1.1076 +  by (induct xs) auto
  1.1077  
  1.1078 -lemma take_Suc_Cons[simp]: "take (Suc n) (x#xs) = x # take n xs"
  1.1079 -by simp
  1.1080 +lemma take_Suc_Cons [simp]: "take (Suc n) (x # xs) = x # take n xs"
  1.1081 +  by simp
  1.1082  
  1.1083 -lemma drop_Suc_Cons[simp]: "drop (Suc n) (x#xs) = drop n xs"
  1.1084 -by simp
  1.1085 +lemma drop_Suc_Cons [simp]: "drop (Suc n) (x # xs) = drop n xs"
  1.1086 +  by simp
  1.1087  
  1.1088 -declare take_Cons[simp del] drop_Cons[simp del]
  1.1089 +declare take_Cons [simp del] and drop_Cons [simp del]
  1.1090  
  1.1091 -lemma length_take[simp]: "!!xs. length(take n xs) = min (length xs) n"
  1.1092 -by(induct n, auto, case_tac xs, auto)
  1.1093 +lemma length_take [simp]: "!!xs. length (take n xs) = min (length xs) n"
  1.1094 +  by (induct n) (auto, case_tac xs, auto)
  1.1095  
  1.1096 -lemma length_drop[simp]: "!!xs. length(drop n xs) = (length xs - n)"
  1.1097 -by(induct n, auto, case_tac xs, auto)
  1.1098 +lemma length_drop [simp]: "!!xs. length (drop n xs) = (length xs - n)"
  1.1099 +  by (induct n) (auto, case_tac xs, auto)
  1.1100  
  1.1101 -lemma take_all[simp]: "!!xs. length xs <= n ==> take n xs = xs"
  1.1102 -by(induct n, auto, case_tac xs, auto)
  1.1103 +lemma take_all [simp]: "!!xs. length xs <= n ==> take n xs = xs"
  1.1104 +  by (induct n) (auto, case_tac xs, auto)
  1.1105  
  1.1106 -lemma drop_all[simp]: "!!xs. length xs <= n ==> drop n xs = []"
  1.1107 -by(induct n, auto, case_tac xs, auto)
  1.1108 +lemma drop_all [simp]: "!!xs. length xs <= n ==> drop n xs = []"
  1.1109 +  by (induct n) (auto, case_tac xs, auto)
  1.1110  
  1.1111 -lemma take_append[simp]:
  1.1112 - "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
  1.1113 -by(induct n, auto, case_tac xs, auto)
  1.1114 +lemma take_append [simp]:
  1.1115 +    "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
  1.1116 +  by (induct n) (auto, case_tac xs, auto)
  1.1117  
  1.1118 -lemma drop_append[simp]:
  1.1119 - "!!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys" 
  1.1120 -by(induct n, auto, case_tac xs, auto)
  1.1121 +lemma drop_append [simp]:
  1.1122 +    "!!xs. drop n (xs @ ys) = drop n xs @ drop (n - length xs) ys"
  1.1123 +  by (induct n) (auto, case_tac xs, auto)
  1.1124  
  1.1125 -lemma take_take[simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
  1.1126 -apply(induct m)
  1.1127 - apply auto
  1.1128 -apply(case_tac xs)
  1.1129 - apply auto
  1.1130 -apply(case_tac na)
  1.1131 - apply auto
  1.1132 -done
  1.1133 +lemma take_take [simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
  1.1134 +  apply (induct m)
  1.1135 +   apply auto
  1.1136 +  apply (case_tac xs)
  1.1137 +   apply auto
  1.1138 +  apply (case_tac na)
  1.1139 +   apply auto
  1.1140 +  done
  1.1141  
  1.1142 -lemma drop_drop[simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs" 
  1.1143 -apply(induct m)
  1.1144 - apply auto
  1.1145 -apply(case_tac xs)
  1.1146 - apply auto
  1.1147 -done
  1.1148 +lemma drop_drop [simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs"
  1.1149 +  apply (induct m)
  1.1150 +   apply auto
  1.1151 +  apply (case_tac xs)
  1.1152 +   apply auto
  1.1153 +  done
  1.1154  
  1.1155  lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"
  1.1156 -apply(induct m)
  1.1157 - apply auto
  1.1158 -apply(case_tac xs)
  1.1159 - apply auto
  1.1160 -done
  1.1161 +  apply (induct m)
  1.1162 +   apply auto
  1.1163 +  apply (case_tac xs)
  1.1164 +   apply auto
  1.1165 +  done
  1.1166  
  1.1167 -lemma append_take_drop_id[simp]: "!!xs. take n xs @ drop n xs = xs"
  1.1168 -apply(induct n)
  1.1169 - apply auto
  1.1170 -apply(case_tac xs)
  1.1171 - apply auto
  1.1172 -done
  1.1173 +lemma append_take_drop_id [simp]: "!!xs. take n xs @ drop n xs = xs"
  1.1174 +  apply (induct n)
  1.1175 +   apply auto
  1.1176 +  apply (case_tac xs)
  1.1177 +   apply auto
  1.1178 +  done
  1.1179  
  1.1180  lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"
  1.1181 -apply(induct n)
  1.1182 - apply auto
  1.1183 -apply(case_tac xs)
  1.1184 - apply auto
  1.1185 -done
  1.1186 +  apply (induct n)
  1.1187 +   apply auto
  1.1188 +  apply (case_tac xs)
  1.1189 +   apply auto
  1.1190 +  done
  1.1191  
  1.1192 -lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)" 
  1.1193 -apply(induct n)
  1.1194 - apply auto
  1.1195 -apply(case_tac xs)
  1.1196 - apply auto
  1.1197 -done
  1.1198 +lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)"
  1.1199 +  apply (induct n)
  1.1200 +   apply auto
  1.1201 +  apply (case_tac xs)
  1.1202 +   apply auto
  1.1203 +  done
  1.1204  
  1.1205  lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"
  1.1206 -apply(induct xs)
  1.1207 - apply auto
  1.1208 -apply(case_tac i)
  1.1209 - apply auto
  1.1210 -done
  1.1211 +  apply (induct xs)
  1.1212 +   apply auto
  1.1213 +  apply (case_tac i)
  1.1214 +   apply auto
  1.1215 +  done
  1.1216  
  1.1217  lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"
  1.1218 -apply(induct xs)
  1.1219 - apply auto
  1.1220 -apply(case_tac i)
  1.1221 - apply auto
  1.1222 -done
  1.1223 +  apply (induct xs)
  1.1224 +   apply auto
  1.1225 +  apply (case_tac i)
  1.1226 +   apply auto
  1.1227 +  done
  1.1228  
  1.1229 -lemma nth_take[simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
  1.1230 -apply(induct xs)
  1.1231 - apply auto
  1.1232 -apply(case_tac n)
  1.1233 - apply(blast )
  1.1234 -apply(case_tac i)
  1.1235 - apply auto
  1.1236 -done
  1.1237 +lemma nth_take [simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
  1.1238 +  apply (induct xs)
  1.1239 +   apply auto
  1.1240 +  apply (case_tac n)
  1.1241 +   apply(blast )
  1.1242 +  apply (case_tac i)
  1.1243 +   apply auto
  1.1244 +  done
  1.1245  
  1.1246 -lemma nth_drop[simp]: "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n+i)"
  1.1247 -apply(induct n)
  1.1248 - apply auto
  1.1249 -apply(case_tac xs)
  1.1250 - apply auto
  1.1251 -done
  1.1252 +lemma nth_drop [simp]:
  1.1253 +    "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n + i)"
  1.1254 +  apply (induct n)
  1.1255 +   apply auto
  1.1256 +  apply (case_tac xs)
  1.1257 +   apply auto
  1.1258 +  done
  1.1259  
  1.1260  lemma append_eq_conv_conj:
  1.1261 - "!!zs. (xs@ys = zs) = (xs = take (length xs) zs & ys = drop (length xs) zs)"
  1.1262 -apply(induct xs)
  1.1263 - apply simp
  1.1264 -apply clarsimp
  1.1265 -apply(case_tac zs)
  1.1266 -apply auto
  1.1267 -done
  1.1268 +    "!!zs. (xs @ ys = zs) = (xs = take (length xs) zs \<and> ys = drop (length xs) zs)"
  1.1269 +  apply(induct xs)
  1.1270 +   apply simp
  1.1271 +  apply clarsimp
  1.1272 +  apply (case_tac zs)
  1.1273 +  apply auto
  1.1274 +  done
  1.1275 +
  1.1276  
  1.1277 -(** takeWhile & dropWhile **)
  1.1278 +subsection {* @{text takeWhile} and @{text dropWhile} *}
  1.1279  
  1.1280 -section "takeWhile & dropWhile"
  1.1281 +lemma takeWhile_dropWhile_id [simp]: "takeWhile P xs @ dropWhile P xs = xs"
  1.1282 +  by (induct xs) auto
  1.1283  
  1.1284 -lemma takeWhile_dropWhile_id[simp]: "takeWhile P xs @ dropWhile P xs = xs"
  1.1285 -by(induct xs, auto)
  1.1286 +lemma takeWhile_append1 [simp]:
  1.1287 +    "[| x:set xs; ~P(x)  |] ==> takeWhile P (xs @ ys) = takeWhile P xs"
  1.1288 +  by (induct xs) auto
  1.1289  
  1.1290 -lemma  takeWhile_append1[simp]:
  1.1291 - "\<lbrakk> x:set xs; ~P(x) \<rbrakk> \<Longrightarrow> takeWhile P (xs @ ys) = takeWhile P xs"
  1.1292 -by(induct xs, auto)
  1.1293 +lemma takeWhile_append2 [simp]:
  1.1294 +    "(!!x. x : set xs ==> P x) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
  1.1295 +  by (induct xs) auto
  1.1296  
  1.1297 -lemma takeWhile_append2[simp]:
  1.1298 - "(!!x. x : set xs \<Longrightarrow> P(x)) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
  1.1299 -by(induct xs, auto)
  1.1300 -
  1.1301 -lemma takeWhile_tail: "~P(x) ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
  1.1302 -by(induct xs, auto)
  1.1303 +lemma takeWhile_tail: "\<not> P x ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
  1.1304 +  by (induct xs) auto
  1.1305  
  1.1306 -lemma dropWhile_append1[simp]:
  1.1307 - "\<lbrakk> x : set xs; ~P(x) \<rbrakk> \<Longrightarrow> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
  1.1308 -by(induct xs, auto)
  1.1309 +lemma dropWhile_append1 [simp]:
  1.1310 +    "[| x : set xs; ~P(x)  |] ==> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
  1.1311 +  by (induct xs) auto
  1.1312  
  1.1313 -lemma dropWhile_append2[simp]:
  1.1314 - "(!!x. x:set xs \<Longrightarrow> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
  1.1315 -by(induct xs, auto)
  1.1316 +lemma dropWhile_append2 [simp]:
  1.1317 +    "(!!x. x:set xs ==> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
  1.1318 +  by (induct xs) auto
  1.1319  
  1.1320 -lemma set_take_whileD: "x:set(takeWhile P xs) ==> x:set xs & P x"
  1.1321 -by(induct xs, auto split:split_if_asm)
  1.1322 +lemma set_take_whileD: "x : set (takeWhile P xs) ==> x : set xs \<and> P x"
  1.1323 +  by (induct xs) (auto split: split_if_asm)
  1.1324  
  1.1325  
  1.1326 -(** zip **)
  1.1327 -section "zip"
  1.1328 +subsection {* @{text zip} *}
  1.1329  
  1.1330 -lemma zip_Nil[simp]: "zip [] ys = []"
  1.1331 -by(induct ys, auto)
  1.1332 +lemma zip_Nil [simp]: "zip [] ys = []"
  1.1333 +  by (induct ys) auto
  1.1334  
  1.1335 -lemma zip_Cons_Cons[simp]: "zip (x#xs) (y#ys) = (x,y)#zip xs ys"
  1.1336 -by simp
  1.1337 +lemma zip_Cons_Cons [simp]: "zip (x # xs) (y # ys) = (x, y) # zip xs ys"
  1.1338 +  by simp
  1.1339  
  1.1340 -declare zip_Cons[simp del]
  1.1341 +declare zip_Cons [simp del]
  1.1342  
  1.1343 -lemma length_zip[simp]:
  1.1344 - "!!xs. length (zip xs ys) = min (length xs) (length ys)"
  1.1345 -apply(induct ys)
  1.1346 - apply simp
  1.1347 -apply(case_tac xs)
  1.1348 - apply auto
  1.1349 -done
  1.1350 +lemma length_zip [simp]:
  1.1351 +    "!!xs. length (zip xs ys) = min (length xs) (length ys)"
  1.1352 +  apply(induct ys)
  1.1353 +   apply simp
  1.1354 +  apply (case_tac xs)
  1.1355 +   apply auto
  1.1356 +  done
  1.1357  
  1.1358  lemma zip_append1:
  1.1359 - "!!xs. zip (xs@ys) zs =
  1.1360 -        zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
  1.1361 -apply(induct zs)
  1.1362 - apply simp
  1.1363 -apply(case_tac xs)
  1.1364 - apply simp_all
  1.1365 -done
  1.1366 +  "!!xs. zip (xs @ ys) zs =
  1.1367 +      zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
  1.1368 +  apply (induct zs)
  1.1369 +   apply simp
  1.1370 +  apply (case_tac xs)
  1.1371 +   apply simp_all
  1.1372 +  done
  1.1373  
  1.1374  lemma zip_append2:
  1.1375 - "!!ys. zip xs (ys@zs) =
  1.1376 -        zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
  1.1377 -apply(induct xs)
  1.1378 - apply simp
  1.1379 -apply(case_tac ys)
  1.1380 - apply simp_all
  1.1381 -done
  1.1382 +  "!!ys. zip xs (ys @ zs) =
  1.1383 +      zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
  1.1384 +  apply (induct xs)
  1.1385 +   apply simp
  1.1386 +  apply (case_tac ys)
  1.1387 +   apply simp_all
  1.1388 +  done
  1.1389  
  1.1390 -lemma zip_append[simp]:
  1.1391 - "[| length xs = length us; length ys = length vs |] ==> \
  1.1392 -\ zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
  1.1393 -by(simp add: zip_append1)
  1.1394 +lemma zip_append [simp]:
  1.1395 + "[| length xs = length us; length ys = length vs |] ==>
  1.1396 +    zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
  1.1397 +  by (simp add: zip_append1)
  1.1398  
  1.1399  lemma zip_rev:
  1.1400 - "!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
  1.1401 -apply(induct ys)
  1.1402 - apply simp
  1.1403 -apply(case_tac xs)
  1.1404 - apply simp_all
  1.1405 -done
  1.1406 +    "!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
  1.1407 +  apply(induct ys)
  1.1408 +   apply simp
  1.1409 +  apply (case_tac xs)
  1.1410 +   apply simp_all
  1.1411 +  done
  1.1412  
  1.1413 -lemma nth_zip[simp]:
  1.1414 -"!!i xs. \<lbrakk> i < length xs; i < length ys \<rbrakk> \<Longrightarrow> (zip xs ys)!i = (xs!i, ys!i)"
  1.1415 -apply(induct ys)
  1.1416 - apply simp
  1.1417 -apply(case_tac xs)
  1.1418 - apply (simp_all add: nth.simps split:nat.split)
  1.1419 -done
  1.1420 +lemma nth_zip [simp]:
  1.1421 +  "!!i xs. [| i < length xs; i < length ys  |] ==> (zip xs ys)!i = (xs!i, ys!i)"
  1.1422 +  apply (induct ys)
  1.1423 +   apply simp
  1.1424 +  apply (case_tac xs)
  1.1425 +   apply (simp_all add: nth.simps split: nat.split)
  1.1426 +  done
  1.1427  
  1.1428  lemma set_zip:
  1.1429 - "set(zip xs ys) = {(xs!i,ys!i) |i. i < min (length xs) (length ys)}"
  1.1430 -by(simp add: set_conv_nth cong: rev_conj_cong)
  1.1431 +    "set (zip xs ys) = {(xs!i, ys!i) | i. i < min (length xs) (length ys)}"
  1.1432 +  by (simp add: set_conv_nth cong: rev_conj_cong)
  1.1433  
  1.1434  lemma zip_update:
  1.1435 - "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
  1.1436 -by(rule sym, simp add: update_zip)
  1.1437 +    "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
  1.1438 +  by (rule sym, simp add: update_zip)
  1.1439  
  1.1440 -lemma zip_replicate[simp]:
  1.1441 - "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
  1.1442 -apply(induct i)
  1.1443 - apply auto
  1.1444 -apply(case_tac j)
  1.1445 - apply auto
  1.1446 -done
  1.1447 +lemma zip_replicate [simp]:
  1.1448 +    "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
  1.1449 +  apply (induct i)
  1.1450 +   apply auto
  1.1451 +  apply (case_tac j)
  1.1452 +   apply auto
  1.1453 +  done
  1.1454  
  1.1455 -(** list_all2 **)
  1.1456 -section "list_all2"
  1.1457 +
  1.1458 +subsection {* @{text list_all2} *}
  1.1459  
  1.1460  lemma list_all2_lengthD: "list_all2 P xs ys ==> length xs = length ys"
  1.1461 -by(simp add:list_all2_def)
  1.1462 +  by (simp add: list_all2_def)
  1.1463  
  1.1464 -lemma list_all2_Nil[iff]: "list_all2 P [] ys = (ys=[])"
  1.1465 -by(simp add:list_all2_def)
  1.1466 +lemma list_all2_Nil [iff]: "list_all2 P [] ys = (ys = [])"
  1.1467 +  by (simp add: list_all2_def)
  1.1468  
  1.1469 -lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs=[])"
  1.1470 -by(simp add:list_all2_def)
  1.1471 +lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])"
  1.1472 +  by (simp add: list_all2_def)
  1.1473  
  1.1474 -lemma list_all2_Cons[iff]:
  1.1475 - "list_all2 P (x#xs) (y#ys) = (P x y & list_all2 P xs ys)"
  1.1476 -by(auto simp add:list_all2_def)
  1.1477 +lemma list_all2_Cons [iff]:
  1.1478 +    "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
  1.1479 +  by (auto simp add: list_all2_def)
  1.1480  
  1.1481  lemma list_all2_Cons1:
  1.1482 - "list_all2 P (x#xs) ys = (? z zs. ys = z#zs & P x z & list_all2 P xs zs)"
  1.1483 -by(case_tac ys, auto)
  1.1484 +    "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
  1.1485 +  by (cases ys) auto
  1.1486  
  1.1487  lemma list_all2_Cons2:
  1.1488 - "list_all2 P xs (y#ys) = (? z zs. xs = z#zs & P z y & list_all2 P zs ys)"
  1.1489 -by(case_tac xs, auto)
  1.1490 +    "list_all2 P xs (y # ys) = (\<exists>z zs. xs = z # zs \<and> P z y \<and> list_all2 P zs ys)"
  1.1491 +  by (cases xs) auto
  1.1492  
  1.1493 -lemma list_all2_rev[iff]:
  1.1494 - "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
  1.1495 -by(simp add:list_all2_def zip_rev cong:conj_cong)
  1.1496 +lemma list_all2_rev [iff]:
  1.1497 +    "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
  1.1498 +  by (simp add: list_all2_def zip_rev cong: conj_cong)
  1.1499  
  1.1500  lemma list_all2_append1:
  1.1501 - "list_all2 P (xs@ys) zs =
  1.1502 -  (EX us vs. zs = us@vs & length us = length xs & length vs = length ys &
  1.1503 -             list_all2 P xs us & list_all2 P ys vs)"
  1.1504 -apply(simp add:list_all2_def zip_append1)
  1.1505 -apply(rule iffI)
  1.1506 - apply(rule_tac x = "take (length xs) zs" in exI)
  1.1507 - apply(rule_tac x = "drop (length xs) zs" in exI)
  1.1508 - apply(force split: nat_diff_split simp add:min_def)
  1.1509 -apply clarify
  1.1510 -apply(simp add: ball_Un)
  1.1511 -done
  1.1512 +  "list_all2 P (xs @ ys) zs =
  1.1513 +    (EX us vs. zs = us @ vs \<and> length us = length xs \<and> length vs = length ys \<and>
  1.1514 +      list_all2 P xs us \<and> list_all2 P ys vs)"
  1.1515 +  apply (simp add: list_all2_def zip_append1)
  1.1516 +  apply (rule iffI)
  1.1517 +   apply (rule_tac x = "take (length xs) zs" in exI)
  1.1518 +   apply (rule_tac x = "drop (length xs) zs" in exI)
  1.1519 +   apply (force split: nat_diff_split simp add: min_def)
  1.1520 +  apply clarify
  1.1521 +  apply (simp add: ball_Un)
  1.1522 +  done
  1.1523  
  1.1524  lemma list_all2_append2:
  1.1525 - "list_all2 P xs (ys@zs) =
  1.1526 -  (EX us vs. xs = us@vs & length us = length ys & length vs = length zs &
  1.1527 -             list_all2 P us ys & list_all2 P vs zs)"
  1.1528 -apply(simp add:list_all2_def zip_append2)
  1.1529 -apply(rule iffI)
  1.1530 - apply(rule_tac x = "take (length ys) xs" in exI)
  1.1531 - apply(rule_tac x = "drop (length ys) xs" in exI)
  1.1532 - apply(force split: nat_diff_split simp add:min_def)
  1.1533 -apply clarify
  1.1534 -apply(simp add: ball_Un)
  1.1535 -done
  1.1536 +  "list_all2 P xs (ys @ zs) =
  1.1537 +    (EX us vs. xs = us @ vs \<and> length us = length ys \<and> length vs = length zs \<and>
  1.1538 +      list_all2 P us ys \<and> list_all2 P vs zs)"
  1.1539 +  apply (simp add: list_all2_def zip_append2)
  1.1540 +  apply (rule iffI)
  1.1541 +   apply (rule_tac x = "take (length ys) xs" in exI)
  1.1542 +   apply (rule_tac x = "drop (length ys) xs" in exI)
  1.1543 +   apply (force split: nat_diff_split simp add: min_def)
  1.1544 +  apply clarify
  1.1545 +  apply (simp add: ball_Un)
  1.1546 +  done
  1.1547  
  1.1548  lemma list_all2_conv_all_nth:
  1.1549    "list_all2 P xs ys =
  1.1550 -   (length xs = length ys & (!i<length xs. P (xs!i) (ys!i)))"
  1.1551 -by(force simp add:list_all2_def set_zip)
  1.1552 +    (length xs = length ys \<and> (\<forall>i < length xs. P (xs!i) (ys!i)))"
  1.1553 +  by (force simp add: list_all2_def set_zip)
  1.1554  
  1.1555  lemma list_all2_trans[rule_format]:
  1.1556 - "ALL a b c. P1 a b --> P2 b c --> P3 a c ==>
  1.1557 -  ALL bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs"
  1.1558 -apply(induct_tac as)
  1.1559 - apply simp
  1.1560 -apply(rule allI)
  1.1561 -apply(induct_tac bs)
  1.1562 - apply simp
  1.1563 -apply(rule allI)
  1.1564 -apply(induct_tac cs)
  1.1565 - apply auto
  1.1566 -done
  1.1567 +  "\<forall>a b c. P1 a b --> P2 b c --> P3 a c ==>
  1.1568 +    \<forall>bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs"
  1.1569 +  apply(induct_tac as)
  1.1570 +   apply simp
  1.1571 +  apply(rule allI)
  1.1572 +  apply(induct_tac bs)
  1.1573 +   apply simp
  1.1574 +  apply(rule allI)
  1.1575 +  apply(induct_tac cs)
  1.1576 +   apply auto
  1.1577 +  done
  1.1578 +
  1.1579 +
  1.1580 +subsection {* @{text foldl} *}
  1.1581 +
  1.1582 +lemma foldl_append [simp]:
  1.1583 +  "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
  1.1584 +  by (induct xs) auto
  1.1585 +
  1.1586 +text {*
  1.1587 +  Note: @{text "n \<le> foldl (op +) n ns"} looks simpler, but is more
  1.1588 +  difficult to use because it requires an additional transitivity step.
  1.1589 +*}
  1.1590 +
  1.1591 +lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl (op +) n ns"
  1.1592 +  by (induct ns) auto
  1.1593 +
  1.1594 +lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl (op +) 0 ns"
  1.1595 +  by (force intro: start_le_sum simp add: in_set_conv_decomp)
  1.1596 +
  1.1597 +lemma sum_eq_0_conv [iff]:
  1.1598 +    "!!m::nat. (foldl (op +) m ns = 0) = (m = 0 \<and> (\<forall>n \<in> set ns. n = 0))"
  1.1599 +  by (induct ns) auto
  1.1600  
  1.1601  
  1.1602 -section "foldl"
  1.1603 -
  1.1604 -lemma foldl_append[simp]:
  1.1605 - "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
  1.1606 -by(induct xs, auto)
  1.1607 +subsection {* @{text upto} *}
  1.1608  
  1.1609 -(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
  1.1610 -   because it requires an additional transitivity step
  1.1611 -*)
  1.1612 -lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl op+ n ns"
  1.1613 -by(induct ns, auto)
  1.1614 +lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])"
  1.1615 +  -- {* Does not terminate! *}
  1.1616 +  by (induct j) auto
  1.1617 +
  1.1618 +lemma upt_conv_Nil [simp]: "j <= i ==> [i..j(] = []"
  1.1619 +  by (subst upt_rec) simp
  1.1620  
  1.1621 -lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl op+ 0 ns"
  1.1622 -by(force intro: start_le_sum simp add:in_set_conv_decomp)
  1.1623 -
  1.1624 -lemma sum_eq_0_conv[iff]:
  1.1625 - "!!m::nat. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))"
  1.1626 -by(induct ns, auto)
  1.1627 +lemma upt_Suc_append: "i <= j ==> [i..(Suc j)(] = [i..j(]@[j]"
  1.1628 +  -- {* Only needed if @{text upt_Suc} is deleted from the simpset. *}
  1.1629 +  by simp
  1.1630  
  1.1631 -(** upto **)
  1.1632 -
  1.1633 -(* Does not terminate! *)
  1.1634 -lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])"
  1.1635 -by(induct j, auto)
  1.1636 -
  1.1637 -lemma upt_conv_Nil[simp]: "j<=i ==> [i..j(] = []"
  1.1638 -by(subst upt_rec, simp)
  1.1639 +lemma upt_conv_Cons: "i < j ==> [i..j(] = i # [Suc i..j(]"
  1.1640 +  apply(rule trans)
  1.1641 +  apply(subst upt_rec)
  1.1642 +   prefer 2 apply(rule refl)
  1.1643 +  apply simp
  1.1644 +  done
  1.1645  
  1.1646 -(*Only needed if upt_Suc is deleted from the simpset*)
  1.1647 -lemma upt_Suc_append: "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]"
  1.1648 -by simp
  1.1649 +lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"
  1.1650 +  -- {* LOOPS as a simprule, since @{text "j <= j"}. *}
  1.1651 +  by (induct k) auto
  1.1652  
  1.1653 -lemma upt_conv_Cons: "i<j ==> [i..j(] = i#[Suc i..j(]"
  1.1654 -apply(rule trans)
  1.1655 -apply(subst upt_rec)
  1.1656 - prefer 2 apply(rule refl)
  1.1657 -apply simp
  1.1658 -done
  1.1659 -
  1.1660 -(*LOOPS as a simprule, since j<=j*)
  1.1661 -lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"
  1.1662 -by(induct_tac "k", auto)
  1.1663 +lemma length_upt [simp]: "length [i..j(] = j - i"
  1.1664 +  by (induct j) (auto simp add: Suc_diff_le)
  1.1665  
  1.1666 -lemma length_upt[simp]: "length [i..j(] = j-i"
  1.1667 -by(induct_tac j, simp, simp add: Suc_diff_le)
  1.1668 -
  1.1669 -lemma nth_upt[simp]: "i+k < j ==> [i..j(] ! k = i+k"
  1.1670 -apply(induct j)
  1.1671 -apply(auto simp add: less_Suc_eq nth_append split:nat_diff_split)
  1.1672 -done
  1.1673 +lemma nth_upt [simp]: "i + k < j ==> [i..j(] ! k = i + k"
  1.1674 +  apply (induct j)
  1.1675 +  apply (auto simp add: less_Suc_eq nth_append split: nat_diff_split)
  1.1676 +  done
  1.1677  
  1.1678 -lemma take_upt[simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]"
  1.1679 -apply(induct m)
  1.1680 - apply simp
  1.1681 -apply(subst upt_rec)
  1.1682 -apply(rule sym)
  1.1683 -apply(subst upt_rec)
  1.1684 -apply(simp del: upt.simps)
  1.1685 -done
  1.1686 +lemma take_upt [simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]"
  1.1687 +  apply (induct m)
  1.1688 +   apply simp
  1.1689 +  apply (subst upt_rec)
  1.1690 +  apply (rule sym)
  1.1691 +  apply (subst upt_rec)
  1.1692 +  apply (simp del: upt.simps)
  1.1693 +  done
  1.1694  
  1.1695  lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]"
  1.1696 -by(induct n, auto)
  1.1697 +  by (induct n) auto
  1.1698  
  1.1699  lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)"
  1.1700 -thm diff_induct
  1.1701 -apply(induct n m rule: diff_induct)
  1.1702 -prefer 3 apply(subst map_Suc_upt[symmetric])
  1.1703 -apply(auto simp add: less_diff_conv nth_upt)
  1.1704 -done
  1.1705 +  apply (induct n m rule: diff_induct)
  1.1706 +    prefer 3 apply (subst map_Suc_upt[symmetric])
  1.1707 +    apply (auto simp add: less_diff_conv nth_upt)
  1.1708 +  done
  1.1709  
  1.1710 -lemma nth_take_lemma[rule_format]:
  1.1711 - "ALL xs ys. k <= length xs --> k <= length ys
  1.1712 -             --> (ALL i. i < k --> xs!i = ys!i)
  1.1713 -             --> take k xs = take k ys"
  1.1714 -apply(induct_tac k)
  1.1715 -apply(simp_all add: less_Suc_eq_0_disj all_conj_distrib)
  1.1716 -apply clarify
  1.1717 -(*Both lists must be non-empty*)
  1.1718 -apply(case_tac xs)
  1.1719 - apply simp
  1.1720 -apply(case_tac ys)
  1.1721 - apply clarify
  1.1722 - apply(simp (no_asm_use))
  1.1723 -apply clarify
  1.1724 -(*prenexing's needed, not miniscoping*)
  1.1725 -apply(simp (no_asm_use) add: all_simps[symmetric] del: all_simps)
  1.1726 -apply blast
  1.1727 -(*prenexing's needed, not miniscoping*)
  1.1728 -done
  1.1729 +lemma nth_take_lemma [rule_format]:
  1.1730 +  "ALL xs ys. k <= length xs --> k <= length ys
  1.1731 +    --> (ALL i. i < k --> xs!i = ys!i)
  1.1732 +    --> take k xs = take k ys"
  1.1733 +  apply (induct k)
  1.1734 +  apply (simp_all add: less_Suc_eq_0_disj all_conj_distrib)
  1.1735 +  apply clarify
  1.1736 +  txt {* Both lists must be non-empty *}
  1.1737 +  apply (case_tac xs)
  1.1738 +   apply simp
  1.1739 +  apply (case_tac ys)
  1.1740 +   apply clarify
  1.1741 +   apply (simp (no_asm_use))
  1.1742 +  apply clarify
  1.1743 +  txt {* prenexing's needed, not miniscoping *}
  1.1744 +  apply (simp (no_asm_use) add: all_simps [symmetric] del: all_simps)
  1.1745 +  apply blast
  1.1746 +  done
  1.1747  
  1.1748  lemma nth_equalityI:
  1.1749   "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
  1.1750 -apply(frule nth_take_lemma[OF le_refl eq_imp_le])
  1.1751 -apply(simp_all add: take_all)
  1.1752 -done
  1.1753 +  apply (frule nth_take_lemma [OF le_refl eq_imp_le])
  1.1754 +  apply (simp_all add: take_all)
  1.1755 +  done
  1.1756 +
  1.1757 +lemma take_equalityI: "(\<forall>i. take i xs = take i ys) ==> xs = ys"
  1.1758 +  -- {* The famous take-lemma. *}
  1.1759 +  apply (drule_tac x = "max (length xs) (length ys)" in spec)
  1.1760 +  apply (simp add: le_max_iff_disj take_all)
  1.1761 +  done
  1.1762 +
  1.1763 +
  1.1764 +subsection {* @{text "distinct"} and @{text remdups} *}
  1.1765 +
  1.1766 +lemma distinct_append [simp]:
  1.1767 +    "distinct (xs @ ys) = (distinct xs \<and> distinct ys \<and> set xs \<inter> set ys = {})"
  1.1768 +  by (induct xs) auto
  1.1769 +
  1.1770 +lemma set_remdups [simp]: "set (remdups xs) = set xs"
  1.1771 +  by (induct xs) (auto simp add: insert_absorb)
  1.1772 +
  1.1773 +lemma distinct_remdups [iff]: "distinct (remdups xs)"
  1.1774 +  by (induct xs) auto
  1.1775 +
  1.1776 +lemma distinct_filter [simp]: "distinct xs ==> distinct (filter P xs)"
  1.1777 +  by (induct xs) auto
  1.1778  
  1.1779 -(*The famous take-lemma*)
  1.1780 -lemma take_equalityI: "(ALL i. take i xs = take i ys) ==> xs = ys"
  1.1781 -apply(drule_tac x = "max (length xs) (length ys)" in spec)
  1.1782 -apply(simp add: le_max_iff_disj take_all)
  1.1783 -done
  1.1784 +text {*
  1.1785 +  It is best to avoid this indexed version of distinct, but sometimes
  1.1786 +  it is useful. *}
  1.1787 +lemma distinct_conv_nth:
  1.1788 +    "distinct xs = (\<forall>i j. i < size xs \<and> j < size xs \<and> i \<noteq> j --> xs!i \<noteq> xs!j)"
  1.1789 +  apply (induct_tac xs)
  1.1790 +   apply simp
  1.1791 +  apply simp
  1.1792 +  apply (rule iffI)
  1.1793 +   apply clarsimp
  1.1794 +   apply (case_tac i)
  1.1795 +    apply (case_tac j)
  1.1796 +     apply simp
  1.1797 +    apply (simp add: set_conv_nth)
  1.1798 +   apply (case_tac j)
  1.1799 +    apply (clarsimp simp add: set_conv_nth)
  1.1800 +   apply simp
  1.1801 +  apply (rule conjI)
  1.1802 +   apply (clarsimp simp add: set_conv_nth)
  1.1803 +   apply (erule_tac x = 0 in allE)
  1.1804 +   apply (erule_tac x = "Suc i" in allE)
  1.1805 +   apply simp
  1.1806 +  apply clarsimp
  1.1807 +  apply (erule_tac x = "Suc i" in allE)
  1.1808 +  apply (erule_tac x = "Suc j" in allE)
  1.1809 +  apply simp
  1.1810 +  done
  1.1811  
  1.1812  
  1.1813 -(** distinct & remdups **)
  1.1814 -section "distinct & remdups"
  1.1815 -
  1.1816 -lemma distinct_append[simp]:
  1.1817 - "distinct(xs@ys) = (distinct xs & distinct ys & set xs Int set ys = {})"
  1.1818 -by(induct xs, auto)
  1.1819 -
  1.1820 -lemma set_remdups[simp]: "set(remdups xs) = set xs"
  1.1821 -by(induct xs, simp, simp add:insert_absorb)
  1.1822 -
  1.1823 -lemma distinct_remdups[iff]: "distinct(remdups xs)"
  1.1824 -by(induct xs, auto)
  1.1825 -
  1.1826 -lemma distinct_filter[simp]: "distinct xs ==> distinct (filter P xs)"
  1.1827 -by(induct xs, auto)
  1.1828 +subsection {* @{text replicate} *}
  1.1829  
  1.1830 -(* It is best to avoid this indexed version of distinct,
  1.1831 -   but sometimes it is useful *)
  1.1832 -lemma distinct_conv_nth:
  1.1833 - "distinct xs = (\<forall>i j. i < size xs \<and> j < size xs \<and> i \<noteq> j \<longrightarrow> xs!i \<noteq> xs!j)"
  1.1834 -apply(induct_tac xs)
  1.1835 - apply simp
  1.1836 -apply simp
  1.1837 -apply(rule iffI)
  1.1838 - apply(clarsimp)
  1.1839 - apply(case_tac i)
  1.1840 -  apply(case_tac j)
  1.1841 -   apply simp
  1.1842 -  apply(simp add:set_conv_nth)
  1.1843 - apply(case_tac j)
  1.1844 -  apply(clarsimp simp add:set_conv_nth)
  1.1845 - apply simp
  1.1846 -apply(rule conjI)
  1.1847 - apply(clarsimp simp add:set_conv_nth)
  1.1848 - apply(erule_tac x = 0 in allE)
  1.1849 - apply(erule_tac x = "Suc i" in allE)
  1.1850 - apply simp
  1.1851 -apply clarsimp
  1.1852 -apply(erule_tac x = "Suc i" in allE)
  1.1853 -apply(erule_tac x = "Suc j" in allE)
  1.1854 -apply simp
  1.1855 -done
  1.1856 +lemma length_replicate [simp]: "length (replicate n x) = n"
  1.1857 +  by (induct n) auto
  1.1858  
  1.1859 -(** replicate **)
  1.1860 -section "replicate"
  1.1861 -
  1.1862 -lemma length_replicate[simp]: "length(replicate n x) = n"
  1.1863 -by(induct n, auto)
  1.1864 -
  1.1865 -lemma map_replicate[simp]: "map f (replicate n x) = replicate n (f x)"
  1.1866 -by(induct n, auto)
  1.1867 +lemma map_replicate [simp]: "map f (replicate n x) = replicate n (f x)"
  1.1868 +  by (induct n) auto
  1.1869  
  1.1870  lemma replicate_app_Cons_same:
  1.1871 - "(replicate n x) @ (x#xs) = x # replicate n x @ xs"
  1.1872 -by(induct n, auto)
  1.1873 +    "(replicate n x) @ (x # xs) = x # replicate n x @ xs"
  1.1874 +  by (induct n) auto
  1.1875  
  1.1876 -lemma rev_replicate[simp]: "rev(replicate n x) = replicate n x"
  1.1877 -apply(induct n)
  1.1878 - apply simp
  1.1879 -apply(simp add: replicate_app_Cons_same)
  1.1880 -done
  1.1881 +lemma rev_replicate [simp]: "rev (replicate n x) = replicate n x"
  1.1882 +  apply(induct n)
  1.1883 +   apply simp
  1.1884 +  apply (simp add: replicate_app_Cons_same)
  1.1885 +  done
  1.1886  
  1.1887 -lemma replicate_add: "replicate (n+m) x = replicate n x @ replicate m x"
  1.1888 -by(induct n, auto)
  1.1889 +lemma replicate_add: "replicate (n + m) x = replicate n x @ replicate m x"
  1.1890 +  by (induct n) auto
  1.1891  
  1.1892 -lemma hd_replicate[simp]: "n ~= 0 ==> hd(replicate n x) = x"
  1.1893 -by(induct n, auto)
  1.1894 +lemma hd_replicate [simp]: "n \<noteq> 0 ==> hd (replicate n x) = x"
  1.1895 +  by (induct n) auto
  1.1896  
  1.1897 -lemma tl_replicate[simp]: "n ~= 0 ==> tl(replicate n x) = replicate (n - 1) x"
  1.1898 -by(induct n, auto)
  1.1899 +lemma tl_replicate [simp]: "n \<noteq> 0 ==> tl (replicate n x) = replicate (n - 1) x"
  1.1900 +  by (induct n) auto
  1.1901  
  1.1902 -lemma last_replicate[rule_format,simp]:
  1.1903 - "n ~= 0 --> last(replicate n x) = x"
  1.1904 -by(induct_tac n, auto)
  1.1905 +lemma last_replicate [simp]: "n \<noteq> 0 ==> last (replicate n x) = x"
  1.1906 +  by (atomize (full), induct n) auto
  1.1907  
  1.1908 -lemma nth_replicate[simp]: "!!i. i<n ==> (replicate n x)!i = x"
  1.1909 -apply(induct n)
  1.1910 - apply simp
  1.1911 -apply(simp add: nth_Cons split:nat.split)
  1.1912 -done
  1.1913 +lemma nth_replicate[simp]: "!!i. i < n ==> (replicate n x)!i = x"
  1.1914 +  apply(induct n)
  1.1915 +   apply simp
  1.1916 +  apply (simp add: nth_Cons split: nat.split)
  1.1917 +  done
  1.1918  
  1.1919 -lemma set_replicate_Suc: "set(replicate (Suc n) x) = {x}"
  1.1920 -by(induct n, auto)
  1.1921 +lemma set_replicate_Suc: "set (replicate (Suc n) x) = {x}"
  1.1922 +  by (induct n) auto
  1.1923  
  1.1924 -lemma set_replicate[simp]: "n ~= 0 ==> set(replicate n x) = {x}"
  1.1925 -by(fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
  1.1926 +lemma set_replicate [simp]: "n \<noteq> 0 ==> set (replicate n x) = {x}"
  1.1927 +  by (fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
  1.1928  
  1.1929 -lemma set_replicate_conv_if: "set(replicate n x) = (if n=0 then {} else {x})"
  1.1930 -by auto
  1.1931 +lemma set_replicate_conv_if: "set (replicate n x) = (if n = 0 then {} else {x})"
  1.1932 +  by auto
  1.1933  
  1.1934 -lemma in_set_replicateD: "x : set(replicate n y) ==> x=y"
  1.1935 -by(simp add: set_replicate_conv_if split:split_if_asm)
  1.1936 +lemma in_set_replicateD: "x : set (replicate n y) ==> x = y"
  1.1937 +  by (simp add: set_replicate_conv_if split: split_if_asm)
  1.1938  
  1.1939  
  1.1940 -(*** Lexcicographic orderings on lists ***)
  1.1941 -section"Lexcicographic orderings on lists"
  1.1942 +subsection {* Lexcicographic orderings on lists *}
  1.1943  
  1.1944 -lemma wf_lexn: "wf r ==> wf(lexn r n)"
  1.1945 -apply(induct_tac n)
  1.1946 - apply simp
  1.1947 -apply simp
  1.1948 -apply(rule wf_subset)
  1.1949 - prefer 2 apply(rule Int_lower1)
  1.1950 -apply(rule wf_prod_fun_image)
  1.1951 - prefer 2 apply(rule injI)
  1.1952 -apply auto
  1.1953 -done
  1.1954 +lemma wf_lexn: "wf r ==> wf (lexn r n)"
  1.1955 +  apply (induct_tac n)
  1.1956 +   apply simp
  1.1957 +  apply simp
  1.1958 +  apply(rule wf_subset)
  1.1959 +   prefer 2 apply (rule Int_lower1)
  1.1960 +  apply(rule wf_prod_fun_image)
  1.1961 +   prefer 2 apply (rule injI)
  1.1962 +  apply auto
  1.1963 +  done
  1.1964  
  1.1965  lemma lexn_length:
  1.1966 - "!!xs ys. (xs,ys) : lexn r n ==> length xs = n & length ys = n"
  1.1967 -by(induct n, auto)
  1.1968 +    "!!xs ys. (xs, ys) : lexn r n ==> length xs = n \<and> length ys = n"
  1.1969 +  by (induct n) auto
  1.1970  
  1.1971 -lemma wf_lex[intro!]: "wf r ==> wf(lex r)"
  1.1972 -apply(unfold lex_def)
  1.1973 -apply(rule wf_UN)
  1.1974 -apply(blast intro: wf_lexn)
  1.1975 -apply clarify
  1.1976 -apply(rename_tac m n)
  1.1977 -apply(subgoal_tac "m ~= n")
  1.1978 - prefer 2 apply blast
  1.1979 -apply(blast dest: lexn_length not_sym)
  1.1980 -done
  1.1981 -
  1.1982 +lemma wf_lex [intro!]: "wf r ==> wf (lex r)"
  1.1983 +  apply (unfold lex_def)
  1.1984 +  apply (rule wf_UN)
  1.1985 +  apply (blast intro: wf_lexn)
  1.1986 +  apply clarify
  1.1987 +  apply (rename_tac m n)
  1.1988 +  apply (subgoal_tac "m \<noteq> n")
  1.1989 +   prefer 2 apply blast
  1.1990 +  apply (blast dest: lexn_length not_sym)
  1.1991 +  done
  1.1992  
  1.1993  lemma lexn_conv:
  1.1994 - "lexn r n =
  1.1995 -  {(xs,ys). length xs = n & length ys = n &
  1.1996 -            (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"
  1.1997 -apply(induct_tac n)
  1.1998 - apply simp
  1.1999 - apply blast
  1.2000 -apply(simp add: image_Collect lex_prod_def)
  1.2001 -apply auto
  1.2002 +  "lexn r n =
  1.2003 +    {(xs,ys). length xs = n \<and> length ys = n \<and>
  1.2004 +      (\<exists>xys x y xs' ys'. xs= xys @ x#xs' \<and> ys= xys @ y # ys' \<and> (x, y):r)}"
  1.2005 +  apply (induct_tac n)
  1.2006 +   apply simp
  1.2007 +   apply blast
  1.2008 +  apply (simp add: image_Collect lex_prod_def)
  1.2009 +  apply auto
  1.2010 +    apply blast
  1.2011 +   apply (rename_tac a xys x xs' y ys')
  1.2012 +   apply (rule_tac x = "a # xys" in exI)
  1.2013 +   apply simp
  1.2014 +  apply (case_tac xys)
  1.2015 +   apply simp_all
  1.2016    apply blast
  1.2017 - apply(rename_tac a xys x xs' y ys')
  1.2018 - apply(rule_tac x = "a#xys" in exI)
  1.2019 - apply simp
  1.2020 -apply(case_tac xys)
  1.2021 - apply simp_all
  1.2022 -apply blast
  1.2023 -done
  1.2024 +  done
  1.2025  
  1.2026  lemma lex_conv:
  1.2027 - "lex r =
  1.2028 -  {(xs,ys). length xs = length ys &
  1.2029 -            (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"
  1.2030 -by(force simp add: lex_def lexn_conv)
  1.2031 +  "lex r =
  1.2032 +    {(xs,ys). length xs = length ys \<and>
  1.2033 +      (\<exists>xys x y xs' ys'. xs = xys @ x # xs' \<and> ys = xys @ y # ys' \<and> (x, y):r)}"
  1.2034 +  by (force simp add: lex_def lexn_conv)
  1.2035  
  1.2036 -lemma wf_lexico[intro!]: "wf r ==> wf(lexico r)"
  1.2037 -by(unfold lexico_def, blast)
  1.2038 +lemma wf_lexico [intro!]: "wf r ==> wf (lexico r)"
  1.2039 +  by (unfold lexico_def) blast
  1.2040  
  1.2041  lemma lexico_conv:
  1.2042 -"lexico r = {(xs,ys). length xs < length ys |
  1.2043 -                      length xs = length ys & (xs,ys) : lex r}"
  1.2044 -by(simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def)
  1.2045 +  "lexico r = {(xs,ys). length xs < length ys |
  1.2046 +      length xs = length ys \<and> (xs, ys) : lex r}"
  1.2047 +  by (simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def)
  1.2048  
  1.2049 -lemma Nil_notin_lex[iff]: "([],ys) ~: lex r"
  1.2050 -by(simp add:lex_conv)
  1.2051 +lemma Nil_notin_lex [iff]: "([], ys) \<notin> lex r"
  1.2052 +  by (simp add: lex_conv)
  1.2053  
  1.2054 -lemma Nil2_notin_lex[iff]: "(xs,[]) ~: lex r"
  1.2055 -by(simp add:lex_conv)
  1.2056 +lemma Nil2_notin_lex [iff]: "(xs, []) \<notin> lex r"
  1.2057 +  by (simp add:lex_conv)
  1.2058  
  1.2059 -lemma Cons_in_lex[iff]:
  1.2060 - "((x#xs,y#ys) : lex r) =
  1.2061 -  ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)"
  1.2062 -apply(simp add:lex_conv)
  1.2063 -apply(rule iffI)
  1.2064 - prefer 2 apply(blast intro: Cons_eq_appendI)
  1.2065 -apply clarify
  1.2066 -apply(case_tac xys)
  1.2067 - apply simp
  1.2068 -apply simp
  1.2069 -apply blast
  1.2070 -done
  1.2071 +lemma Cons_in_lex [iff]:
  1.2072 +  "((x # xs, y # ys) : lex r) =
  1.2073 +    ((x, y) : r \<and> length xs = length ys | x = y \<and> (xs, ys) : lex r)"
  1.2074 +  apply (simp add: lex_conv)
  1.2075 +  apply (rule iffI)
  1.2076 +   prefer 2 apply (blast intro: Cons_eq_appendI)
  1.2077 +  apply clarify
  1.2078 +  apply (case_tac xys)
  1.2079 +   apply simp
  1.2080 +  apply simp
  1.2081 +  apply blast
  1.2082 +  done
  1.2083  
  1.2084  
  1.2085 -(*** sublist (a generalization of nth to sets) ***)
  1.2086 +subsection {* @{text sublist} --- a generalization of @{text nth} to sets *}
  1.2087  
  1.2088 -lemma sublist_empty[simp]: "sublist xs {} = []"
  1.2089 -by(auto simp add:sublist_def)
  1.2090 +lemma sublist_empty [simp]: "sublist xs {} = []"
  1.2091 +  by (auto simp add: sublist_def)
  1.2092  
  1.2093 -lemma sublist_nil[simp]: "sublist [] A = []"
  1.2094 -by(auto simp add:sublist_def)
  1.2095 +lemma sublist_nil [simp]: "sublist [] A = []"
  1.2096 +  by (auto simp add: sublist_def)
  1.2097  
  1.2098  lemma sublist_shift_lemma:
  1.2099 - "map fst [p:zip xs [i..i + length xs(] . snd p : A] =
  1.2100 -  map fst [p:zip xs [0..length xs(] . snd p + i : A]"
  1.2101 -apply(induct_tac xs rule: rev_induct)
  1.2102 - apply simp
  1.2103 -apply(simp add:add_commute)
  1.2104 -done
  1.2105 +  "map fst [p:zip xs [i..i + length xs(] . snd p : A] =
  1.2106 +    map fst [p:zip xs [0..length xs(] . snd p + i : A]"
  1.2107 +  by (induct xs rule: rev_induct) (simp_all add: add_commute)
  1.2108  
  1.2109  lemma sublist_append:
  1.2110 - "sublist (l@l') A = sublist l A @ sublist l' {j. j + length l : A}"
  1.2111 -apply(unfold sublist_def)
  1.2112 -apply(induct_tac l' rule: rev_induct)
  1.2113 - apply simp
  1.2114 -apply(simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
  1.2115 -apply(simp add:add_commute)
  1.2116 -done
  1.2117 +    "sublist (l @ l') A = sublist l A @ sublist l' {j. j + length l : A}"
  1.2118 +  apply (unfold sublist_def)
  1.2119 +  apply (induct l' rule: rev_induct)
  1.2120 +   apply simp
  1.2121 +  apply (simp add: upt_add_eq_append[of 0] zip_append sublist_shift_lemma)
  1.2122 +  apply (simp add: add_commute)
  1.2123 +  done
  1.2124  
  1.2125  lemma sublist_Cons:
  1.2126 - "sublist (x#l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
  1.2127 -apply(induct_tac l rule: rev_induct)
  1.2128 - apply(simp add:sublist_def)
  1.2129 -apply(simp del: append_Cons add: append_Cons[symmetric] sublist_append)
  1.2130 -done
  1.2131 +    "sublist (x # l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
  1.2132 +  apply (induct l rule: rev_induct)
  1.2133 +   apply (simp add: sublist_def)
  1.2134 +  apply (simp del: append_Cons add: append_Cons[symmetric] sublist_append)
  1.2135 +  done
  1.2136  
  1.2137 -lemma sublist_singleton[simp]: "sublist [x] A = (if 0 : A then [x] else [])"
  1.2138 -by(simp add:sublist_Cons)
  1.2139 +lemma sublist_singleton [simp]: "sublist [x] A = (if 0 : A then [x] else [])"
  1.2140 +  by (simp add: sublist_Cons)
  1.2141  
  1.2142 -lemma sublist_upt_eq_take[simp]: "sublist l {..n(} = take n l"
  1.2143 -apply(induct_tac l rule: rev_induct)
  1.2144 - apply simp
  1.2145 -apply(simp split:nat_diff_split add:sublist_append)
  1.2146 -done
  1.2147 +lemma sublist_upt_eq_take [simp]: "sublist l {..n(} = take n l"
  1.2148 +  apply (induct l rule: rev_induct)
  1.2149 +   apply simp
  1.2150 +  apply (simp split: nat_diff_split add: sublist_append)
  1.2151 +  done
  1.2152  
  1.2153  
  1.2154 -lemma take_Cons': "take n (x#xs) = (if n=0 then [] else x # take (n - 1) xs)"
  1.2155 -by(case_tac n, simp_all)
  1.2156 -
  1.2157 -lemma drop_Cons': "drop n (x#xs) = (if n=0 then x#xs else drop (n - 1) xs)"
  1.2158 -by(case_tac n, simp_all)
  1.2159 +lemma take_Cons':
  1.2160 +    "take n (x # xs) = (if n = 0 then [] else x # take (n - 1) xs)"
  1.2161 +  by (cases n) simp_all
  1.2162  
  1.2163 -lemma nth_Cons': "(x#xs)!n = (if n=0 then x else xs!(n - 1))"
  1.2164 -by(case_tac n, simp_all)
  1.2165 +lemma drop_Cons':
  1.2166 +    "drop n (x # xs) = (if n = 0 then x # xs else drop (n - 1) xs)"
  1.2167 +  by (cases n) simp_all
  1.2168  
  1.2169 -lemmas [simp] = take_Cons'[of "number_of v",standard]
  1.2170 -                drop_Cons'[of "number_of v",standard]
  1.2171 -                nth_Cons'[of "number_of v",standard]
  1.2172 +lemma nth_Cons': "(x # xs)!n = (if n = 0 then x else xs!(n - 1))"
  1.2173 +  by (cases n) simp_all
  1.2174 +
  1.2175 +lemmas [of "number_of v", standard, simp] =
  1.2176 +  take_Cons' drop_Cons' nth_Cons'
  1.2177  
  1.2178  end