src/HOL/List.thy
 changeset 13114 f2b00262bdfc parent 12887 d25b43743e10 child 13122 c63612ffb186
```     1.1 --- a/src/HOL/List.thy	Tue May 07 19:54:04 2002 +0200
1.2 +++ b/src/HOL/List.thy	Tue May 07 19:54:29 2002 +0200
1.3 @@ -2,56 +2,59 @@
1.4      ID:         \$Id\$
1.5      Author:     Tobias Nipkow
1.7 -
1.8 -The datatype of finite lists.
1.9  *)
1.10
1.11 -List = PreList +
1.12 +header {* The datatype of finite lists *}
1.13 +theory List1 = PreList:
1.14
1.15 -datatype 'a list = Nil ("[]") | Cons 'a ('a list) (infixr "#" 65)
1.16 +datatype 'a list = Nil ("[]") | Cons 'a "'a list" (infixr "#" 65)
1.17
1.18  consts
1.19 -  "@"         :: ['a list, 'a list] => 'a list            (infixr 65)
1.20 -  filter      :: ['a => bool, 'a list] => 'a list
1.21 -  concat      :: 'a list list => 'a list
1.22 -  foldl       :: [['b,'a] => 'b, 'b, 'a list] => 'b
1.23 -  foldr       :: [['a,'b] => 'b, 'a list, 'b] => 'b
1.24 -  hd, last    :: 'a list => 'a
1.25 -  set         :: 'a list => 'a set
1.26 -  list_all    :: ('a => bool) => ('a list => bool)
1.27 -  list_all2   :: ('a => 'b => bool) => 'a list => 'b list => bool
1.28 -  map         :: ('a=>'b) => ('a list => 'b list)
1.29 -  mem         :: ['a, 'a list] => bool                    (infixl 55)
1.30 -  nth         :: ['a list, nat] => 'a			  (infixl "!" 100)
1.31 -  list_update :: 'a list => nat => 'a => 'a list
1.32 -  take, drop  :: [nat, 'a list] => 'a list
1.33 -  takeWhile,
1.34 -  dropWhile   :: ('a => bool) => 'a list => 'a list
1.35 -  tl, butlast :: 'a list => 'a list
1.36 -  rev         :: 'a list => 'a list
1.37 -  zip	      :: "'a list => 'b list => ('a * 'b) list"
1.38 -  upt         :: nat => nat => nat list                   ("(1[_../_'(])")
1.39 -  remdups     :: "'a list => 'a list"
1.40 -  null, "distinct" :: "'a list => bool"
1.41 -  replicate   :: nat => 'a => 'a list
1.42 +  "@"         :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"            (infixr 65)
1.43 +  filter      :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
1.44 +  concat      :: "'a list list \<Rightarrow> 'a list"
1.45 +  foldl       :: "('b \<Rightarrow> 'a \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a list \<Rightarrow> 'b"
1.46 +  foldr       :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b"
1.47 +  hd          :: "'a list \<Rightarrow> 'a"
1.48 +  tl          :: "'a list \<Rightarrow> 'a list"
1.49 +  last        :: "'a list \<Rightarrow> 'a"
1.50 +  butlast     :: "'a list \<Rightarrow> 'a list"
1.51 +  set         :: "'a list \<Rightarrow> 'a set"
1.52 +  list_all    :: "('a \<Rightarrow> bool) \<Rightarrow> ('a list \<Rightarrow> bool)"
1.53 +  list_all2   :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'b list \<Rightarrow> bool"
1.54 +  map         :: "('a\<Rightarrow>'b) \<Rightarrow> ('a list \<Rightarrow> 'b list)"
1.55 +  mem         :: "'a \<Rightarrow> 'a list \<Rightarrow> bool"                    (infixl 55)
1.56 +  nth         :: "'a list \<Rightarrow> nat \<Rightarrow> 'a"			  (infixl "!" 100)
1.57 +  list_update :: "'a list \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a list"
1.58 +  take        :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
1.59 +  drop        :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list"
1.60 +  takeWhile   :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
1.61 +  dropWhile   :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list"
1.62 +  rev         :: "'a list \<Rightarrow> 'a list"
1.63 +  zip	      :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a * 'b) list"
1.64 +  upt         :: "nat \<Rightarrow> nat \<Rightarrow> nat list"                   ("(1[_../_'(])")
1.65 +  remdups     :: "'a list \<Rightarrow> 'a list"
1.66 +  null        :: "'a list \<Rightarrow> bool"
1.67 +  "distinct"  :: "'a list \<Rightarrow> bool"
1.68 +  replicate   :: "nat \<Rightarrow> 'a \<Rightarrow> 'a list"
1.69
1.70  nonterminals
1.71    lupdbinds  lupdbind
1.72
1.73  syntax
1.74    (* list Enumeration *)
1.75 -  "@list"     :: args => 'a list                          ("[(_)]")
1.76 +  "@list"     :: "args \<Rightarrow> 'a list"                          ("[(_)]")
1.77
1.78    (* Special syntax for filter *)
1.79 -  "@filter"   :: [pttrn, 'a list, bool] => 'a list        ("(1[_:_./ _])")
1.80 +  "@filter"   :: "[pttrn, 'a list, bool] \<Rightarrow> 'a list"        ("(1[_:_./ _])")
1.81
1.82    (* list update *)
1.83 -  "_lupdbind"      :: ['a, 'a] => lupdbind            ("(2_ :=/ _)")
1.84 -  ""               :: lupdbind => lupdbinds           ("_")
1.85 -  "_lupdbinds"     :: [lupdbind, lupdbinds] => lupdbinds ("_,/ _")
1.86 -  "_LUpdate"       :: ['a, lupdbinds] => 'a           ("_/[(_)]" [900,0] 900)
1.87 +  "_lupdbind"      :: "['a, 'a] \<Rightarrow> lupdbind"            ("(2_ :=/ _)")
1.88 +  ""               :: "lupdbind \<Rightarrow> lupdbinds"           ("_")
1.89 +  "_lupdbinds"     :: "[lupdbind, lupdbinds] \<Rightarrow> lupdbinds" ("_,/ _")
1.90 +  "_LUpdate"       :: "['a, lupdbinds] \<Rightarrow> 'a"           ("_/[(_)]" [900,0] 900)
1.91
1.92 -  upto        :: nat => nat => nat list                   ("(1[_../_])")
1.93 +  upto        :: "nat \<Rightarrow> nat \<Rightarrow> nat list"                   ("(1[_../_])")
1.94
1.95  translations
1.96    "[x, xs]"     == "x#[xs]"
1.97 @@ -65,22 +68,32 @@
1.98
1.99
1.100  syntax (xsymbols)
1.101 -  "@filter"   :: [pttrn, 'a list, bool] => 'a list        ("(1[_\\<in>_ ./ _])")
1.102 +  "@filter"   :: "[pttrn, 'a list, bool] \<Rightarrow> 'a list"        ("(1[_\<in>_ ./ _])")
1.103
1.104
1.105  consts
1.106 -  lists        :: 'a set => 'a list set
1.107 +  lists        :: "'a set \<Rightarrow> 'a list set"
1.108
1.109 -  inductive "lists A"
1.110 -  intrs
1.111 -    Nil  "[]: lists A"
1.112 -    Cons "[| a: A;  l: lists A |] ==> a#l : lists A"
1.113 +inductive "lists A"
1.114 +intros
1.115 +Nil:  "[]: lists A"
1.116 +Cons: "\<lbrakk> a: A;  l: lists A \<rbrakk> \<Longrightarrow> a#l : lists A"
1.117
1.118
1.119  (*Function "size" is overloaded for all datatypes.  Users may refer to the
1.120    list version as "length".*)
1.121 -syntax   length :: 'a list => nat
1.122 -translations  "length"  =>  "size:: _ list => nat"
1.123 +syntax   length :: "'a list \<Rightarrow> nat"
1.124 +translations  "length"  =>  "size:: _ list \<Rightarrow> 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 +*}
1.135
1.136  primrec
1.137    "hd(x#xs) = x"
1.138 @@ -102,14 +115,14 @@
1.139    "set [] = {}"
1.140    "set (x#xs) = insert x (set xs)"
1.141  primrec
1.142 -  list_all_Nil  "list_all P [] = True"
1.143 -  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
1.144 +  list_all_Nil:  "list_all P [] = True"
1.145 +  list_all_Cons: "list_all P (x#xs) = (P(x) & list_all P xs)"
1.146  primrec
1.147    "map f []     = []"
1.148    "map f (x#xs) = f(x)#map f xs"
1.149  primrec
1.150 -  append_Nil  "[]    @ys = ys"
1.151 -  append_Cons "(x#xs)@ys = x#(xs@ys)"
1.152 +  append_Nil:  "[]    @ys = ys"
1.153 +  append_Cons: "(x#xs)@ys = x#(xs@ys)"
1.154  primrec
1.155    "rev([])   = []"
1.156    "rev(x#xs) = rev(xs) @ [x]"
1.157 @@ -117,8 +130,8 @@
1.158    "filter P []     = []"
1.159    "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
1.160  primrec
1.161 -  foldl_Nil  "foldl f a [] = a"
1.162 -  foldl_Cons "foldl f a (x#xs) = foldl f (f a x) xs"
1.163 +  foldl_Nil:  "foldl f a [] = a"
1.164 +  foldl_Cons: "foldl f a (x#xs) = foldl f (f a x) xs"
1.165  primrec
1.166    "foldr f [] a     = a"
1.167    "foldr f (x#xs) a = f x (foldr f xs a)"
1.168 @@ -126,23 +139,23 @@
1.169    "concat([])   = []"
1.170    "concat(x#xs) = x @ concat(xs)"
1.171  primrec
1.172 -  drop_Nil  "drop n [] = []"
1.173 -  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
1.174 +  drop_Nil:  "drop n [] = []"
1.175 +  drop_Cons: "drop n (x#xs) = (case n of 0 \<Rightarrow> x#xs | Suc(m) \<Rightarrow> drop m xs)"
1.176    (* Warning: simpset does not contain this definition but separate theorems
1.177       for n=0 / n=Suc k*)
1.178  primrec
1.179 -  take_Nil  "take n [] = []"
1.180 -  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
1.181 +  take_Nil:  "take n [] = []"
1.182 +  take_Cons: "take n (x#xs) = (case n of 0 \<Rightarrow> [] | Suc(m) \<Rightarrow> x # take m xs)"
1.183    (* Warning: simpset does not contain this definition but separate theorems
1.184       for n=0 / n=Suc k*)
1.185  primrec
1.186 -  nth_Cons  "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
1.187 +  nth_Cons:  "(x#xs)!n = (case n of 0 \<Rightarrow> x | (Suc k) \<Rightarrow> xs!k)"
1.188    (* Warning: simpset does not contain this definition but separate theorems
1.189       for n=0 / n=Suc k*)
1.190  primrec
1.191   "    [][i:=v] = []"
1.192 - "(x#xs)[i:=v] = (case i of 0     => v # xs
1.193 -			  | Suc j => x # xs[j:=v])"
1.194 + "(x#xs)[i:=v] = (case i of 0     \<Rightarrow> v # xs
1.195 +			  | Suc j \<Rightarrow> x # xs[j:=v])"
1.196  primrec
1.197    "takeWhile P []     = []"
1.198    "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
1.199 @@ -151,12 +164,13 @@
1.200    "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
1.201  primrec
1.202    "zip xs []     = []"
1.203 -  "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
1.204 +zip_Cons:
1.205 +  "zip xs (y#ys) = (case xs of [] \<Rightarrow> [] | z#zs \<Rightarrow> (z,y)#zip zs ys)"
1.206    (* Warning: simpset does not contain this definition but separate theorems
1.207       for xs=[] / xs=z#zs *)
1.208  primrec
1.209 -  upt_0   "[i..0(] = []"
1.210 -  upt_Suc "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
1.211 +  upt_0:   "[i..0(] = []"
1.212 +  upt_Suc: "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
1.213  primrec
1.214    "distinct []     = True"
1.215    "distinct (x#xs) = (x ~: set xs & distinct xs)"
1.216 @@ -164,46 +178,1170 @@
1.217    "remdups [] = []"
1.218    "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
1.219  primrec
1.220 -  replicate_0   "replicate  0      x = []"
1.221 -  replicate_Suc "replicate (Suc n) x = x # replicate n x"
1.222 +  replicate_0:   "replicate  0      x = []"
1.223 +  replicate_Suc: "replicate (Suc n) x = x # replicate n x"
1.224  defs
1.225 - list_all2_def
1.226 + list_all2_def:
1.227   "list_all2 P xs ys == length xs = length ys & (!(x,y):set(zip xs ys). P x y)"
1.228
1.229
1.230  (** Lexicographic orderings on lists **)
1.231
1.232  consts
1.233 - lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
1.234 + lexn :: "('a * 'a)set \<Rightarrow> nat \<Rightarrow> ('a list * 'a list)set"
1.235  primrec
1.236  "lexn r 0       = {}"
1.237  "lexn r (Suc n) = (prod_fun (%(x,xs). x#xs) (%(x,xs). x#xs) ` (r <*lex*> lexn r n)) Int
1.238                    {(xs,ys). length xs = Suc n & length ys = Suc n}"
1.239
1.240  constdefs
1.241 -  lex :: "('a * 'a)set => ('a list * 'a list)set"
1.242 +  lex :: "('a * 'a)set \<Rightarrow> ('a list * 'a list)set"
1.243      "lex r == UN n. lexn r n"
1.244
1.245 -  lexico :: "('a * 'a)set => ('a list * 'a list)set"
1.246 +  lexico :: "('a * 'a)set \<Rightarrow> ('a list * 'a list)set"
1.247      "lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
1.248
1.249 -  sublist :: "['a list, nat set] => 'a list"
1.250 +  sublist :: "['a list, nat set] \<Rightarrow> 'a list"
1.251      "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))"
1.252
1.253 -end
1.254 +
1.255 +lemma not_Cons_self[simp]: "\<And>x. xs ~= x#xs"
1.256 +by(induct_tac "xs", auto)
1.257 +
1.258 +lemmas not_Cons_self2[simp] = not_Cons_self[THEN not_sym]
1.259 +
1.260 +lemma neq_Nil_conv: "(xs ~= []) = (? y ys. xs = y#ys)";
1.261 +by(induct_tac "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 +
1.267 +
1.268 +(** "lists": the list-forming operator over sets **)
1.269 +
1.270 +lemma lists_mono: "A<=B ==> lists A <= lists B"
1.271 +apply(unfold lists.defs)
1.272 +apply(blast intro!:lfp_mono)
1.273 +done
1.274 +
1.275 +inductive_cases listsE[elim!]: "x#l : lists A"
1.276 +declare lists.intros[intro!]
1.277 +
1.278 +lemma lists_IntI[rule_format]:
1.279 + "l: lists A ==> l: lists B --> l: lists (A Int B)";
1.280 +apply(erule lists.induct)
1.281 +apply blast+
1.282 +done
1.283 +
1.284 +lemma lists_Int_eq[simp]: "lists (A Int B) = lists A Int lists B"
1.285 +apply(rule mono_Int[THEN equalityI])
1.287 +apply(blast intro!: lists_IntI)
1.288 +done
1.289 +
1.290 +lemma append_in_lists_conv[iff]:
1.291 + "(xs@ys : lists A) = (xs : lists A & ys : lists A)"
1.292 +by(induct_tac "xs", auto)
1.293 +
1.294 +(** length **)
1.295 +(* needs to come before "@" because of thm append_eq_append_conv *)
1.296 +
1.297 +section "length"
1.298 +
1.299 +lemma length_append[simp]: "length(xs@ys) = length(xs)+length(ys)"
1.300 +by(induct_tac "xs", auto)
1.301 +
1.302 +lemma length_map[simp]: "length (map f xs) = length xs"
1.303 +by(induct_tac "xs", auto)
1.304 +
1.305 +lemma length_rev[simp]: "length(rev xs) = length(xs)"
1.306 +by(induct_tac "xs", auto)
1.307 +
1.308 +lemma length_tl[simp]: "length(tl xs) = (length xs) - 1"
1.309 +by(case_tac "xs", auto)
1.310 +
1.311 +lemma length_0_conv[iff]: "(length xs = 0) = (xs = [])"
1.312 +by(induct_tac "xs", auto)
1.313 +
1.314 +lemma length_greater_0_conv[iff]: "(0 < length xs) = (xs ~= [])"
1.315 +by(induct_tac xs, auto)
1.316 +
1.317 +lemma length_Suc_conv:
1.318 + "(length xs = Suc n) = (? y ys. xs = y#ys & length ys = n)"
1.319 +by(induct_tac "xs", auto)
1.320 +
1.321 +(** @ - append **)
1.322 +
1.323 +section "@ - append"
1.324 +
1.325 +lemma append_assoc[simp]: "(xs@ys)@zs = xs@(ys@zs)"
1.326 +by(induct_tac "xs", auto)
1.327
1.328 -ML
1.329 +lemma append_Nil2[simp]: "xs @ [] = xs"
1.330 +by(induct_tac "xs", auto)
1.331 +
1.332 +lemma append_is_Nil_conv[iff]: "(xs@ys = []) = (xs=[] & ys=[])"
1.333 +by(induct_tac "xs", auto)
1.334 +
1.335 +lemma Nil_is_append_conv[iff]: "([] = xs@ys) = (xs=[] & ys=[])"
1.336 +by(induct_tac "xs", auto)
1.337 +
1.338 +lemma append_self_conv[iff]: "(xs @ ys = xs) = (ys=[])"
1.339 +by(induct_tac "xs", auto)
1.340 +
1.341 +lemma self_append_conv[iff]: "(xs = xs @ ys) = (ys=[])"
1.342 +by(induct_tac "xs", auto)
1.343 +
1.344 +lemma append_eq_append_conv[rule_format,simp]:
1.345 + "!ys. length xs = length ys | length us = length vs
1.346 +       --> (xs@us = ys@vs) = (xs=ys & us=vs)"
1.347 +apply(induct_tac "xs")
1.348 + apply(rule allI)
1.349 + apply(case_tac "ys")
1.350 +  apply simp
1.351 + apply force
1.352 +apply(rule allI)
1.353 +apply(case_tac "ys")
1.354 + apply force
1.355 +apply simp
1.356 +done
1.357 +
1.358 +lemma same_append_eq[iff]: "(xs @ ys = xs @ zs) = (ys=zs)"
1.359 +by simp
1.360 +
1.361 +lemma append1_eq_conv[iff]: "(xs @ [x] = ys @ [y]) = (xs = ys & x = y)"
1.362 +by simp
1.363 +
1.364 +lemma append_same_eq[iff]: "(ys @ xs = zs @ xs) = (ys=zs)"
1.365 +by simp
1.366
1.367 +lemma append_self_conv2[iff]: "(xs @ ys = ys) = (xs=[])"
1.368 +by(insert append_same_eq[of _ _ "[]"], auto)
1.369 +
1.370 +lemma self_append_conv2[iff]: "(ys = xs @ ys) = (xs=[])"
1.371 +by(auto simp add: append_same_eq[of "[]", simplified])
1.372 +
1.373 +lemma hd_Cons_tl[rule_format,simp]: "xs ~= [] --> hd xs # tl xs = xs"
1.374 +by(induct_tac "xs", auto)
1.375 +
1.376 +lemma hd_append: "hd(xs@ys) = (if xs=[] then hd ys else hd xs)"
1.377 +by(induct_tac "xs", auto)
1.378 +
1.379 +lemma hd_append2[simp]: "xs ~= [] ==> hd(xs @ ys) = hd xs"
1.380 +by(simp add: hd_append split: list.split)
1.381 +
1.382 +lemma tl_append: "tl(xs@ys) = (case xs of [] => tl(ys) | z#zs => zs@ys)"
1.383 +by(simp split: list.split)
1.384 +
1.385 +lemma tl_append2[simp]: "xs ~= [] ==> tl(xs @ ys) = (tl xs) @ ys"
1.386 +by(simp add: tl_append split: list.split)
1.387 +
1.388 +(* trivial rules for solving @-equations automatically *)
1.389 +
1.390 +lemma eq_Nil_appendI: "xs = ys ==> xs = [] @ ys"
1.391 +by simp
1.392 +
1.393 +lemma Cons_eq_appendI: "[| x#xs1 = ys; xs = xs1 @ zs |] ==> x#xs = ys@zs"
1.394 +by(drule sym, simp)
1.395 +
1.396 +lemma append_eq_appendI: "[| xs@xs1 = zs; ys = xs1 @ us |] ==> xs@ys = zs@us"
1.397 +by(drule sym, simp)
1.398 +
1.399 +
1.400 +(***
1.401 +Simplification procedure for all list equalities.
1.402 +Currently only tries to rearrange @ to see if
1.403 +- both lists end in a singleton list,
1.404 +- or both lists end in the same list.
1.405 +***)
1.406 +ML_setup{*
1.407  local
1.408
1.409 -(* translating size::list -> length *)
1.410 +val list_eq_pattern =
1.411 +  Thm.read_cterm (Theory.sign_of (the_context ())) ("(xs::'a list) = ys",HOLogic.boolT)
1.412 +
1.413 +fun last (cons as Const("List.list.Cons",_) \$ _ \$ xs) =
1.414 +      (case xs of Const("List.list.Nil",_) => cons | _ => last xs)
1.415 +  | last (Const("List.op @",_) \$ _ \$ ys) = last ys
1.416 +  | last t = t
1.417 +
1.418 +fun list1 (Const("List.list.Cons",_) \$ _ \$ Const("List.list.Nil",_)) = true
1.419 +  | list1 _ = false
1.420 +
1.421 +fun butlast ((cons as Const("List.list.Cons",_) \$ x) \$ xs) =
1.422 +      (case xs of Const("List.list.Nil",_) => xs | _ => cons \$ butlast xs)
1.423 +  | butlast ((app as Const("List.op @",_) \$ xs) \$ ys) = app \$ butlast ys
1.424 +  | butlast xs = Const("List.list.Nil",fastype_of xs)
1.425 +
1.426 +val rearr_tac =
1.427 +  simp_tac (HOL_basic_ss addsimps [append_assoc,append_Nil,append_Cons])
1.428 +
1.429 +fun list_eq sg _ (F as (eq as Const(_,eqT)) \$ lhs \$ rhs) =
1.430 +  let
1.431 +    val lastl = last lhs and lastr = last rhs
1.432 +    fun rearr conv =
1.433 +      let val lhs1 = butlast lhs and rhs1 = butlast rhs
1.434 +          val Type(_,listT::_) = eqT
1.435 +          val appT = [listT,listT] ---> listT
1.436 +          val app = Const("List.op @",appT)
1.437 +          val F2 = eq \$ (app\$lhs1\$lastl) \$ (app\$rhs1\$lastr)
1.438 +          val ct = cterm_of sg (HOLogic.mk_Trueprop(HOLogic.mk_eq(F,F2)))
1.439 +          val thm = prove_goalw_cterm [] ct (K [rearr_tac 1])
1.440 +            handle ERROR =>
1.441 +            error("The error(s) above occurred while trying to prove " ^
1.442 +                  string_of_cterm ct)
1.443 +      in Some((conv RS (thm RS trans)) RS eq_reflection) end
1.444 +
1.445 +  in if list1 lastl andalso list1 lastr
1.446 +     then rearr append1_eq_conv
1.447 +     else
1.448 +     if lastl aconv lastr
1.449 +     then rearr append_same_eq
1.450 +     else None
1.451 +  end
1.452 +in
1.453 +val list_eq_simproc = mk_simproc "list_eq" [list_eq_pattern] list_eq
1.454 +end;
1.455 +
1.457 +*}
1.458 +
1.459 +
1.460 +(** map **)
1.461 +
1.462 +section "map"
1.463 +
1.464 +lemma map_ext: "(\<And>x. x : set xs \<longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g xs"
1.465 +by (induct xs, simp_all)
1.466 +
1.467 +lemma map_ident[simp]: "map (%x. x) = (%xs. xs)"
1.468 +by(rule ext, induct_tac "xs", auto)
1.469 +
1.470 +lemma map_append[simp]: "map f (xs@ys) = map f xs @ map f ys"
1.471 +by(induct_tac "xs", auto)
1.472 +
1.473 +lemma map_compose(*[simp]*): "map (f o g) xs = map f (map g xs)"
1.474 +by(unfold o_def, induct_tac "xs", auto)
1.475 +
1.476 +lemma rev_map: "rev(map f xs) = map f (rev xs)"
1.477 +by(induct_tac xs, auto)
1.478 +
1.479 +(* a congruence rule for map: *)
1.480 +lemma map_cong:
1.481 + "xs=ys ==> (!!x. x : set ys \<Longrightarrow> f x = g x) \<Longrightarrow> map f xs = map g ys"
1.482 +by (clarify, induct ys, auto)
1.483 +
1.484 +lemma map_is_Nil_conv[iff]: "(map f xs = []) = (xs = [])"
1.485 +by(case_tac xs, auto)
1.486 +
1.487 +lemma Nil_is_map_conv[iff]: "([] = map f xs) = (xs = [])"
1.488 +by(case_tac xs, auto)
1.489 +
1.490 +lemma map_eq_Cons:
1.491 + "(map f xs = y#ys) = (? x xs'. xs = x#xs' & f x = y & map f xs' = ys)"
1.492 +by(case_tac xs, auto)
1.493 +
1.494 +lemma map_injective:
1.495 + "\<And>xs. map f xs = map f ys \<Longrightarrow> (!x y. f x = f y --> x=y) \<Longrightarrow> xs=ys"
1.496 +by(induct "ys", simp, fastsimp simp add:map_eq_Cons)
1.497 +
1.498 +lemma inj_mapI: "inj f ==> inj (map f)"
1.499 +by(blast dest:map_injective injD intro:injI)
1.500 +
1.501 +lemma inj_mapD: "inj (map f) ==> inj f"
1.502 +apply(unfold inj_on_def)
1.503 +apply clarify
1.504 +apply(erule_tac x = "[x]" in ballE)
1.505 + apply(erule_tac x = "[y]" in ballE)
1.506 +  apply simp
1.507 + apply blast
1.508 +apply blast
1.509 +done
1.510 +
1.511 +lemma inj_map: "inj (map f) = inj f"
1.512 +by(blast dest:inj_mapD intro:inj_mapI)
1.513 +
1.514 +(** rev **)
1.515 +
1.516 +section "rev"
1.517 +
1.518 +lemma rev_append[simp]: "rev(xs@ys) = rev(ys) @ rev(xs)"
1.519 +by(induct_tac xs, auto)
1.520 +
1.521 +lemma rev_rev_ident[simp]: "rev(rev xs) = xs"
1.522 +by(induct_tac xs, auto)
1.523 +
1.524 +lemma rev_is_Nil_conv[iff]: "(rev xs = []) = (xs = [])"
1.525 +by(induct_tac xs, auto)
1.526 +
1.527 +lemma Nil_is_rev_conv[iff]: "([] = rev xs) = (xs = [])"
1.528 +by(induct_tac xs, auto)
1.529 +
1.530 +lemma rev_is_rev_conv[iff]: "!!ys. (rev xs = rev ys) = (xs = ys)"
1.531 +apply(induct "xs" )
1.532 + apply force
1.533 +apply(case_tac ys)
1.534 + apply simp
1.535 +apply force
1.536 +done
1.537 +
1.538 +lemma rev_induct: "[| P []; !!x xs. P xs ==> P(xs@[x]) |] ==> P xs"
1.539 +apply(subst rev_rev_ident[symmetric])
1.540 +apply(rule_tac list = "rev xs" in list.induct, simp_all)
1.541 +done
1.542 +
1.543 +(* Instead of (rev_induct_tac xs) use (induct_tac xs rule: rev_induct) *)
1.544 +
1.545 +lemma rev_exhaust: "(xs = [] \<Longrightarrow> P) \<Longrightarrow>  (!!ys y. xs = ys@[y] \<Longrightarrow> P) \<Longrightarrow> P"
1.546 +by(induct xs rule: rev_induct, auto)
1.547 +
1.548 +
1.549 +(** set **)
1.550 +
1.551 +section "set"
1.552 +
1.553 +lemma finite_set[iff]: "finite (set xs)"
1.554 +by(induct_tac xs, auto)
1.555 +
1.556 +lemma set_append[simp]: "set (xs@ys) = (set xs Un set ys)"
1.557 +by(induct_tac xs, auto)
1.558 +
1.559 +lemma set_subset_Cons: "set xs \<subseteq> set (x#xs)"
1.560 +by auto
1.561 +
1.562 +lemma set_empty[iff]: "(set xs = {}) = (xs = [])"
1.563 +by(induct_tac xs, auto)
1.564 +
1.565 +lemma set_rev[simp]: "set(rev xs) = set(xs)"
1.566 +by(induct_tac xs, auto)
1.567 +
1.568 +lemma set_map[simp]: "set(map f xs) = f`(set xs)"
1.569 +by(induct_tac xs, auto)
1.570 +
1.571 +lemma set_filter[simp]: "set(filter P xs) = {x. x : set xs & P x}"
1.572 +by(induct_tac xs, auto)
1.573 +
1.574 +lemma set_upt[simp]: "set[i..j(] = {k. i <= k & k < j}"
1.575 +apply(induct_tac j)
1.576 + apply simp_all
1.577 +apply(erule ssubst)
1.578 +apply auto
1.579 +apply arith
1.580 +done
1.581 +
1.582 +lemma in_set_conv_decomp: "(x : set xs) = (? ys zs. xs = ys@x#zs)"
1.583 +apply(induct_tac "xs")
1.584 + apply simp
1.585 +apply simp
1.586 +apply(rule iffI)
1.587 + apply(blast intro: eq_Nil_appendI Cons_eq_appendI)
1.588 +apply(erule exE)+
1.589 +apply(case_tac "ys")
1.590 +apply auto
1.591 +done
1.592 +
1.593 +
1.594 +(* eliminate `lists' in favour of `set' *)
1.595 +
1.596 +lemma in_lists_conv_set: "(xs : lists A) = (!x : set xs. x : A)"
1.597 +by(induct_tac xs, auto)
1.598 +
1.599 +lemmas in_listsD[dest!] = in_lists_conv_set[THEN iffD1]
1.600 +lemmas in_listsI[intro!] = in_lists_conv_set[THEN iffD2]
1.601 +
1.602 +
1.603 +(** mem **)
1.604 +
1.605 +section "mem"
1.606 +
1.607 +lemma set_mem_eq: "(x mem xs) = (x : set xs)"
1.608 +by(induct_tac xs, auto)
1.609 +
1.610 +
1.611 +(** list_all **)
1.612 +
1.613 +section "list_all"
1.614 +
1.615 +lemma list_all_conv: "list_all P xs = (!x:set xs. P x)"
1.616 +by(induct_tac xs, auto)
1.617 +
1.618 +lemma list_all_append[simp]:
1.619 + "list_all P (xs@ys) = (list_all P xs & list_all P ys)"
1.620 +by(induct_tac xs, auto)
1.621 +
1.622 +
1.623 +(** filter **)
1.624 +
1.625 +section "filter"
1.626 +
1.627 +lemma filter_append[simp]: "filter P (xs@ys) = filter P xs @ filter P ys"
1.628 +by(induct_tac xs, auto)
1.629 +
1.630 +lemma filter_filter[simp]: "filter P (filter Q xs) = filter (%x. Q x & P x) xs"
1.631 +by(induct_tac xs, auto)
1.632 +
1.633 +lemma filter_True[simp]: "!x : set xs. P x \<Longrightarrow> filter P xs = xs"
1.634 +by(induct xs, auto)
1.635 +
1.636 +lemma filter_False[simp]: "!x : set xs. ~P x \<Longrightarrow> filter P xs = []"
1.637 +by(induct xs, auto)
1.638 +
1.639 +lemma length_filter[simp]: "length (filter P xs) <= length xs"
1.640 +by(induct xs, auto simp add: le_SucI)
1.641 +
1.642 +lemma filter_is_subset[simp]: "set (filter P xs) <= set xs"
1.643 +by auto
1.644 +
1.645 +
1.646 +section "concat"
1.647 +
1.648 +lemma concat_append[simp]: "concat(xs@ys) = concat(xs)@concat(ys)"
1.649 +by(induct xs, auto)
1.650 +
1.651 +lemma concat_eq_Nil_conv[iff]: "(concat xss = []) = (!xs:set xss. xs=[])"
1.652 +by(induct xss, auto)
1.653 +
1.654 +lemma Nil_eq_concat_conv[iff]: "([] = concat xss) = (!xs:set xss. xs=[])"
1.655 +by(induct xss, auto)
1.656 +
1.657 +lemma set_concat[simp]: "set(concat xs) = Union(set ` set xs)"
1.658 +by(induct xs, auto)
1.659 +
1.660 +lemma map_concat: "map f (concat xs) = concat (map (map f) xs)"
1.661 +by(induct xs, auto)
1.662 +
1.663 +lemma filter_concat: "filter p (concat xs) = concat (map (filter p) xs)"
1.664 +by(induct xs, auto)
1.665 +
1.666 +lemma rev_concat: "rev(concat xs) = concat (map rev (rev xs))"
1.667 +by(induct xs, auto)
1.668 +
1.669 +(** nth **)
1.670 +
1.671 +section "nth"
1.672 +
1.673 +lemma nth_Cons_0[simp]: "(x#xs)!0 = x"
1.674 +by auto
1.675 +
1.676 +lemma nth_Cons_Suc[simp]: "(x#xs)!(Suc n) = xs!n"
1.677 +by auto
1.678 +
1.679 +declare nth.simps[simp del]
1.680 +
1.681 +lemma nth_append:
1.682 + "!!n. (xs@ys)!n = (if n < length xs then xs!n else ys!(n - length xs))"
1.683 +apply(induct "xs")
1.684 + apply simp
1.685 +apply(case_tac "n" )
1.686 + apply auto
1.687 +done
1.688 +
1.689 +lemma nth_map[simp]: "!!n. n < length xs \<Longrightarrow> (map f xs)!n = f(xs!n)"
1.690 +apply(induct "xs" )
1.691 + apply simp
1.692 +apply(case_tac "n")
1.693 + apply auto
1.694 +done
1.695 +
1.696 +lemma set_conv_nth: "set xs = {xs!i |i. i < length xs}"
1.697 +apply(induct_tac "xs")
1.698 + apply simp
1.699 +apply simp
1.700 +apply safe
1.701 +  apply(rule_tac x = 0 in exI)
1.702 +  apply simp
1.703 + apply(rule_tac x = "Suc i" in exI)
1.704 + apply simp
1.705 +apply(case_tac "i")
1.706 + apply simp
1.707 +apply(rename_tac "j")
1.708 +apply(rule_tac x = "j" in exI)
1.709 +apply simp
1.710 +done
1.711 +
1.712 +lemma list_ball_nth: "\<lbrakk> n < length xs; !x : set xs. P x \<rbrakk> \<Longrightarrow> P(xs!n)"
1.714 +
1.715 +lemma nth_mem[simp]: "n < length xs ==> xs!n : set xs"
1.717 +
1.718 +lemma all_nth_imp_all_set:
1.719 + "\<lbrakk> !i < length xs. P(xs!i); x : set xs \<rbrakk> \<Longrightarrow> P x"
1.721 +
1.722 +lemma all_set_conv_all_nth:
1.723 + "(!x : set xs. P x) = (!i. i<length xs --> P (xs ! i))"
1.725 +
1.726 +
1.727 +(** list update **)
1.728 +
1.729 +section "list update"
1.730 +
1.731 +lemma length_list_update[simp]: "!!i. length(xs[i:=x]) = length xs"
1.732 +by(induct xs, simp, simp split:nat.split)
1.733 +
1.734 +lemma nth_list_update:
1.735 + "!!i j. i < length xs  \<Longrightarrow> (xs[i:=x])!j = (if i=j then x else xs!j)"
1.736 +by(induct xs, simp, auto simp add:nth_Cons split:nat.split)
1.737 +
1.738 +lemma nth_list_update_eq[simp]: "i < length xs  ==> (xs[i:=x])!i = x"
1.740 +
1.741 +lemma nth_list_update_neq[simp]: "!!i j. i ~= j \<Longrightarrow> xs[i:=x]!j = xs!j"
1.742 +by(induct xs, simp, auto simp add:nth_Cons split:nat.split)
1.743 +
1.744 +lemma list_update_overwrite[simp]:
1.745 + "!!i. i < size xs ==> xs[i:=x, i:=y] = xs[i:=y]"
1.746 +by(induct xs, simp, simp split:nat.split)
1.747 +
1.748 +lemma list_update_same_conv:
1.749 + "!!i. i < length xs \<Longrightarrow> (xs[i := x] = xs) = (xs!i = x)"
1.750 +by(induct xs, simp, simp split:nat.split, blast)
1.751 +
1.752 +lemma update_zip:
1.753 +"!!i xy xs. length xs = length ys \<Longrightarrow>
1.754 +    (zip xs ys)[i:=xy] = zip (xs[i:=fst xy]) (ys[i:=snd xy])"
1.755 +by(induct ys, auto, case_tac xs, auto split:nat.split)
1.756 +
1.757 +lemma set_update_subset_insert: "!!i. set(xs[i:=x]) <= insert x (set xs)"
1.758 +by(induct xs, simp, simp split:nat.split, fast)
1.759 +
1.760 +lemma set_update_subsetI: "[| set xs <= A; x:A |] ==> set(xs[i := x]) <= A"
1.761 +by(fast dest!:set_update_subset_insert[THEN subsetD])
1.762 +
1.763 +
1.764 +(** last & butlast **)
1.765 +
1.766 +section "last / butlast"
1.767 +
1.768 +lemma last_snoc[simp]: "last(xs@[x]) = x"
1.769 +by(induct xs, auto)
1.770 +
1.771 +lemma butlast_snoc[simp]:"butlast(xs@[x]) = xs"
1.772 +by(induct xs, auto)
1.773 +
1.774 +lemma length_butlast[simp]: "length(butlast xs) = length xs - 1"
1.775 +by(induct xs rule:rev_induct, auto)
1.776 +
1.777 +lemma butlast_append:
1.778 + "!!ys. butlast (xs@ys) = (if ys=[] then butlast xs else xs@butlast ys)"
1.779 +by(induct xs, auto)
1.780 +
1.781 +lemma append_butlast_last_id[simp]:
1.782 + "xs ~= [] --> butlast xs @ [last xs] = xs"
1.783 +by(induct xs, auto)
1.784 +
1.785 +lemma in_set_butlastD: "x:set(butlast xs) ==> x:set xs"
1.786 +by(induct xs, auto split:split_if_asm)
1.787 +
1.788 +lemma in_set_butlast_appendI:
1.789 + "x:set(butlast xs) | x:set(butlast ys) ==> x:set(butlast(xs@ys))"
1.791 +
1.792 +(** take  & drop **)
1.793 +section "take & drop"
1.794 +
1.795 +lemma take_0[simp]: "take 0 xs = []"
1.796 +by(induct xs, auto)
1.797 +
1.798 +lemma drop_0[simp]: "drop 0 xs = xs"
1.799 +by(induct xs, auto)
1.800 +
1.801 +lemma take_Suc_Cons[simp]: "take (Suc n) (x#xs) = x # take n xs"
1.802 +by simp
1.803 +
1.804 +lemma drop_Suc_Cons[simp]: "drop (Suc n) (x#xs) = drop n xs"
1.805 +by simp
1.806 +
1.807 +declare take_Cons[simp del] drop_Cons[simp del]
1.808 +
1.809 +lemma length_take[simp]: "!!xs. length(take n xs) = min (length xs) n"
1.810 +by(induct n, auto, case_tac xs, auto)
1.811 +
1.812 +lemma length_drop[simp]: "!!xs. length(drop n xs) = (length xs - n)"
1.813 +by(induct n, auto, case_tac xs, auto)
1.814 +
1.815 +lemma take_all[simp]: "!!xs. length xs <= n ==> take n xs = xs"
1.816 +by(induct n, auto, case_tac xs, auto)
1.817 +
1.818 +lemma drop_all[simp]: "!!xs. length xs <= n ==> drop n xs = []"
1.819 +by(induct n, auto, case_tac xs, auto)
1.820 +
1.821 +lemma take_append[simp]:
1.822 + "!!xs. take n (xs @ ys) = (take n xs @ take (n - length xs) ys)"
1.823 +by(induct n, auto, case_tac xs, auto)
1.824 +
1.825 +lemma drop_append[simp]:
1.826 + "!!xs. drop n (xs@ys) = drop n xs @ drop (n - length xs) ys"
1.827 +by(induct n, auto, case_tac xs, auto)
1.828 +
1.829 +lemma take_take[simp]: "!!xs n. take n (take m xs) = take (min n m) xs"
1.830 +apply(induct m)
1.831 + apply auto
1.832 +apply(case_tac xs)
1.833 + apply auto
1.834 +apply(case_tac na)
1.835 + apply auto
1.836 +done
1.837 +
1.838 +lemma drop_drop[simp]: "!!xs. drop n (drop m xs) = drop (n + m) xs"
1.839 +apply(induct m)
1.840 + apply auto
1.841 +apply(case_tac xs)
1.842 + apply auto
1.843 +done
1.844 +
1.845 +lemma take_drop: "!!xs n. take n (drop m xs) = drop m (take (n + m) xs)"
1.846 +apply(induct m)
1.847 + apply auto
1.848 +apply(case_tac xs)
1.849 + apply auto
1.850 +done
1.851 +
1.852 +lemma append_take_drop_id[simp]: "!!xs. take n xs @ drop n xs = xs"
1.853 +apply(induct n)
1.854 + apply auto
1.855 +apply(case_tac xs)
1.856 + apply auto
1.857 +done
1.858 +
1.859 +lemma take_map: "!!xs. take n (map f xs) = map f (take n xs)"
1.860 +apply(induct n)
1.861 + apply auto
1.862 +apply(case_tac xs)
1.863 + apply auto
1.864 +done
1.865 +
1.866 +lemma drop_map: "!!xs. drop n (map f xs) = map f (drop n xs)"
1.867 +apply(induct n)
1.868 + apply auto
1.869 +apply(case_tac xs)
1.870 + apply auto
1.871 +done
1.872 +
1.873 +lemma rev_take: "!!i. rev (take i xs) = drop (length xs - i) (rev xs)"
1.874 +apply(induct xs)
1.875 + apply auto
1.876 +apply(case_tac i)
1.877 + apply auto
1.878 +done
1.879 +
1.880 +lemma rev_drop: "!!i. rev (drop i xs) = take (length xs - i) (rev xs)"
1.881 +apply(induct xs)
1.882 + apply auto
1.883 +apply(case_tac i)
1.884 + apply auto
1.885 +done
1.886 +
1.887 +lemma nth_take[simp]: "!!n i. i < n ==> (take n xs)!i = xs!i"
1.888 +apply(induct xs)
1.889 + apply auto
1.890 +apply(case_tac n)
1.891 + apply(blast )
1.892 +apply(case_tac i)
1.893 + apply auto
1.894 +done
1.895 +
1.896 +lemma nth_drop[simp]: "!!xs i. n + i <= length xs ==> (drop n xs)!i = xs!(n+i)"
1.897 +apply(induct n)
1.898 + apply auto
1.899 +apply(case_tac xs)
1.900 + apply auto
1.901 +done
1.902
1.903 -fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
1.904 -      Syntax.const "length" \$ t
1.905 -  | size_tr' _ _ _ = raise Match;
1.906 +lemma append_eq_conv_conj:
1.907 + "!!zs. (xs@ys = zs) = (xs = take (length xs) zs & ys = drop (length xs) zs)"
1.908 +apply(induct xs)
1.909 + apply simp
1.910 +apply clarsimp
1.911 +apply(case_tac zs)
1.912 +apply auto
1.913 +done
1.914 +
1.915 +(** takeWhile & dropWhile **)
1.916 +
1.917 +section "takeWhile & dropWhile"
1.918 +
1.919 +lemma takeWhile_dropWhile_id[simp]: "takeWhile P xs @ dropWhile P xs = xs"
1.920 +by(induct xs, auto)
1.921 +
1.922 +lemma  takeWhile_append1[simp]:
1.923 + "\<lbrakk> x:set xs; ~P(x) \<rbrakk> \<Longrightarrow> takeWhile P (xs @ ys) = takeWhile P xs"
1.924 +by(induct xs, auto)
1.925 +
1.926 +lemma takeWhile_append2[simp]:
1.927 + "(!!x. x : set xs \<Longrightarrow> P(x)) ==> takeWhile P (xs @ ys) = xs @ takeWhile P ys"
1.928 +by(induct xs, auto)
1.929 +
1.930 +lemma takeWhile_tail: "~P(x) ==> takeWhile P (xs @ (x#l)) = takeWhile P xs"
1.931 +by(induct xs, auto)
1.932 +
1.933 +lemma dropWhile_append1[simp]:
1.934 + "\<lbrakk> x : set xs; ~P(x) \<rbrakk> \<Longrightarrow> dropWhile P (xs @ ys) = (dropWhile P xs)@ys"
1.935 +by(induct xs, auto)
1.936 +
1.937 +lemma dropWhile_append2[simp]:
1.938 + "(!!x. x:set xs \<Longrightarrow> P(x)) ==> dropWhile P (xs @ ys) = dropWhile P ys"
1.939 +by(induct xs, auto)
1.940 +
1.941 +lemma set_take_whileD: "x:set(takeWhile P xs) ==> x:set xs & P x"
1.942 +by(induct xs, auto split:split_if_asm)
1.943 +
1.944 +
1.945 +(** zip **)
1.946 +section "zip"
1.947 +
1.948 +lemma zip_Nil[simp]: "zip [] ys = []"
1.949 +by(induct ys, auto)
1.950 +
1.951 +lemma zip_Cons_Cons[simp]: "zip (x#xs) (y#ys) = (x,y)#zip xs ys"
1.952 +by simp
1.953 +
1.954 +declare zip_Cons[simp del]
1.955 +
1.956 +lemma length_zip[simp]:
1.957 + "!!xs. length (zip xs ys) = min (length xs) (length ys)"
1.958 +apply(induct ys)
1.959 + apply simp
1.960 +apply(case_tac xs)
1.961 + apply auto
1.962 +done
1.963 +
1.964 +lemma zip_append1:
1.965 + "!!xs. zip (xs@ys) zs =
1.966 +        zip xs (take (length xs) zs) @ zip ys (drop (length xs) zs)"
1.967 +apply(induct zs)
1.968 + apply simp
1.969 +apply(case_tac xs)
1.970 + apply simp_all
1.971 +done
1.972 +
1.973 +lemma zip_append2:
1.974 + "!!ys. zip xs (ys@zs) =
1.975 +        zip (take (length ys) xs) ys @ zip (drop (length ys) xs) zs"
1.976 +apply(induct xs)
1.977 + apply simp
1.978 +apply(case_tac ys)
1.979 + apply simp_all
1.980 +done
1.981 +
1.982 +lemma zip_append[simp]:
1.983 + "[| length xs = length us; length ys = length vs |] ==> \
1.984 +\ zip (xs@ys) (us@vs) = zip xs us @ zip ys vs"
1.986 +
1.987 +lemma zip_rev:
1.988 + "!!xs. length xs = length ys ==> zip (rev xs) (rev ys) = rev (zip xs ys)"
1.989 +apply(induct ys)
1.990 + apply simp
1.991 +apply(case_tac xs)
1.992 + apply simp_all
1.993 +done
1.994 +
1.995 +lemma nth_zip[simp]:
1.996 +"!!i xs. \<lbrakk> i < length xs; i < length ys \<rbrakk> \<Longrightarrow> (zip xs ys)!i = (xs!i, ys!i)"
1.997 +apply(induct ys)
1.998 + apply simp
1.999 +apply(case_tac xs)
1.1000 + apply (simp_all add: nth.simps split:nat.split)
1.1001 +done
1.1002 +
1.1003 +lemma set_zip:
1.1004 + "set(zip xs ys) = {(xs!i,ys!i) |i. i < min (length xs) (length ys)}"
1.1005 +by(simp add: set_conv_nth cong: rev_conj_cong)
1.1006 +
1.1007 +lemma zip_update:
1.1008 + "length xs = length ys ==> zip (xs[i:=x]) (ys[i:=y]) = (zip xs ys)[i:=(x,y)]"
1.1009 +by(rule sym, simp add: update_zip)
1.1010 +
1.1011 +lemma zip_replicate[simp]:
1.1012 + "!!j. zip (replicate i x) (replicate j y) = replicate (min i j) (x,y)"
1.1013 +apply(induct i)
1.1014 + apply auto
1.1015 +apply(case_tac j)
1.1016 + apply auto
1.1017 +done
1.1018 +
1.1019 +(** list_all2 **)
1.1020 +section "list_all2"
1.1021 +
1.1022 +lemma list_all2_lengthD: "list_all2 P xs ys ==> length xs = length ys"
1.1024 +
1.1025 +lemma list_all2_Nil[iff]: "list_all2 P [] ys = (ys=[])"
1.1027 +
1.1028 +lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs=[])"
1.1030 +
1.1031 +lemma list_all2_Cons[iff]:
1.1032 + "list_all2 P (x#xs) (y#ys) = (P x y & list_all2 P xs ys)"
1.1034 +
1.1035 +lemma list_all2_Cons1:
1.1036 + "list_all2 P (x#xs) ys = (? z zs. ys = z#zs & P x z & list_all2 P xs zs)"
1.1037 +by(case_tac ys, auto)
1.1038 +
1.1039 +lemma list_all2_Cons2:
1.1040 + "list_all2 P xs (y#ys) = (? z zs. xs = z#zs & P z y & list_all2 P zs ys)"
1.1041 +by(case_tac xs, auto)
1.1042 +
1.1043 +lemma list_all2_rev[iff]:
1.1044 + "list_all2 P (rev xs) (rev ys) = list_all2 P xs ys"
1.1046 +
1.1047 +lemma list_all2_append1:
1.1048 + "list_all2 P (xs@ys) zs =
1.1049 +  (EX us vs. zs = us@vs & length us = length xs & length vs = length ys &
1.1050 +             list_all2 P xs us & list_all2 P ys vs)"
1.1052 +apply(rule iffI)
1.1053 + apply(rule_tac x = "take (length xs) zs" in exI)
1.1054 + apply(rule_tac x = "drop (length xs) zs" in exI)
1.1055 + apply(force split: nat_diff_split simp add:min_def)
1.1056 +apply clarify
1.1058 +done
1.1059 +
1.1060 +lemma list_all2_append2:
1.1061 + "list_all2 P xs (ys@zs) =
1.1062 +  (EX us vs. xs = us@vs & length us = length ys & length vs = length zs &
1.1063 +             list_all2 P us ys & list_all2 P vs zs)"
1.1065 +apply(rule iffI)
1.1066 + apply(rule_tac x = "take (length ys) xs" in exI)
1.1067 + apply(rule_tac x = "drop (length ys) xs" in exI)
1.1068 + apply(force split: nat_diff_split simp add:min_def)
1.1069 +apply clarify
1.1071 +done
1.1072 +
1.1073 +lemma list_all2_conv_all_nth:
1.1074 +  "list_all2 P xs ys =
1.1075 +   (length xs = length ys & (!i<length xs. P (xs!i) (ys!i)))"
1.1077 +
1.1078 +lemma list_all2_trans[rule_format]:
1.1079 + "ALL a b c. P1 a b --> P2 b c --> P3 a c ==>
1.1080 +  ALL bs cs. list_all2 P1 as bs --> list_all2 P2 bs cs --> list_all2 P3 as cs"
1.1081 +apply(induct_tac as)
1.1082 + apply simp
1.1083 +apply(rule allI)
1.1084 +apply(induct_tac bs)
1.1085 + apply simp
1.1086 +apply(rule allI)
1.1087 +apply(induct_tac cs)
1.1088 + apply auto
1.1089 +done
1.1090 +
1.1091 +
1.1092 +section "foldl"
1.1093 +
1.1094 +lemma foldl_append[simp]:
1.1095 + "!!a. foldl f a (xs @ ys) = foldl f (foldl f a xs) ys"
1.1096 +by(induct xs, auto)
1.1097 +
1.1098 +(* Note: `n <= foldl op+ n ns' looks simpler, but is more difficult to use
1.1099 +   because it requires an additional transitivity step
1.1100 +*)
1.1101 +lemma start_le_sum: "!!n::nat. m <= n ==> m <= foldl op+ n ns"
1.1102 +by(induct ns, auto)
1.1103 +
1.1104 +lemma elem_le_sum: "!!n::nat. n : set ns ==> n <= foldl op+ 0 ns"
1.1105 +by(force intro: start_le_sum simp add:in_set_conv_decomp)
1.1106 +
1.1107 +lemma sum_eq_0_conv[iff]:
1.1108 + "!!m::nat. (foldl op+ m ns = 0) = (m=0 & (!n : set ns. n=0))"
1.1109 +by(induct ns, auto)
1.1110 +
1.1111 +(** upto **)
1.1112 +
1.1113 +(* Does not terminate! *)
1.1114 +lemma upt_rec: "[i..j(] = (if i<j then i#[Suc i..j(] else [])"
1.1115 +by(induct j, auto)
1.1116 +
1.1117 +lemma upt_conv_Nil[simp]: "j<=i ==> [i..j(] = []"
1.1118 +by(subst upt_rec, simp)
1.1119 +
1.1120 +(*Only needed if upt_Suc is deleted from the simpset*)
1.1121 +lemma upt_Suc_append: "i<=j ==> [i..(Suc j)(] = [i..j(]@[j]"
1.1122 +by simp
1.1123 +
1.1124 +lemma upt_conv_Cons: "i<j ==> [i..j(] = i#[Suc i..j(]"
1.1125 +apply(rule trans)
1.1126 +apply(subst upt_rec)
1.1127 + prefer 2 apply(rule refl)
1.1128 +apply simp
1.1129 +done
1.1130 +
1.1131 +(*LOOPS as a simprule, since j<=j*)
1.1132 +lemma upt_add_eq_append: "i<=j ==> [i..j+k(] = [i..j(]@[j..j+k(]"
1.1133 +by(induct_tac "k", auto)
1.1134 +
1.1135 +lemma length_upt[simp]: "length [i..j(] = j-i"
1.1136 +by(induct_tac j, simp, simp add: Suc_diff_le)
1.1137 +
1.1138 +lemma nth_upt[simp]: "i+k < j ==> [i..j(] ! k = i+k"
1.1139 +apply(induct j)
1.1140 +apply(auto simp add: less_Suc_eq nth_append split:nat_diff_split)
1.1141 +done
1.1142 +
1.1143 +lemma take_upt[simp]: "!!i. i+m <= n ==> take m [i..n(] = [i..i+m(]"
1.1144 +apply(induct m)
1.1145 + apply simp
1.1146 +apply(subst upt_rec)
1.1147 +apply(rule sym)
1.1148 +apply(subst upt_rec)
1.1149 +apply(simp del: upt.simps)
1.1150 +done
1.1151
1.1152 -in
1.1153 +lemma map_Suc_upt: "map Suc [m..n(] = [Suc m..n]"
1.1154 +by(induct n, auto)
1.1155 +
1.1156 +lemma nth_map_upt: "!!i. i < n-m ==> (map f [m..n(]) ! i = f(m+i)"
1.1157 +thm diff_induct
1.1158 +apply(induct n m rule: diff_induct)
1.1159 +prefer 3 apply(subst map_Suc_upt[symmetric])
1.1160 +apply(auto simp add: less_diff_conv nth_upt)
1.1161 +done
1.1162 +
1.1163 +lemma nth_take_lemma[rule_format]:
1.1164 + "ALL xs ys. k <= length xs --> k <= length ys
1.1165 +             --> (ALL i. i < k --> xs!i = ys!i)
1.1166 +             --> take k xs = take k ys"
1.1167 +apply(induct_tac k)
1.1169 +apply clarify
1.1170 +(*Both lists must be non-empty*)
1.1171 +apply(case_tac xs)
1.1172 + apply simp
1.1173 +apply(case_tac ys)
1.1174 + apply clarify
1.1175 + apply(simp (no_asm_use))
1.1176 +apply clarify
1.1177 +(*prenexing's needed, not miniscoping*)
1.1178 +apply(simp (no_asm_use) add: all_simps[symmetric] del: all_simps)
1.1179 +apply blast
1.1180 +(*prenexing's needed, not miniscoping*)
1.1181 +done
1.1182 +
1.1183 +lemma nth_equalityI:
1.1184 + "[| length xs = length ys; ALL i < length xs. xs!i = ys!i |] ==> xs = ys"
1.1185 +apply(frule nth_take_lemma[OF le_refl eq_imp_le])
1.1187 +done
1.1188 +
1.1189 +(*The famous take-lemma*)
1.1190 +lemma take_equalityI: "(ALL i. take i xs = take i ys) ==> xs = ys"
1.1191 +apply(drule_tac x = "max (length xs) (length ys)" in spec)
1.1193 +done
1.1194 +
1.1195 +
1.1196 +(** distinct & remdups **)
1.1197 +section "distinct & remdups"
1.1198 +
1.1199 +lemma distinct_append[simp]:
1.1200 + "distinct(xs@ys) = (distinct xs & distinct ys & set xs Int set ys = {})"
1.1201 +by(induct xs, auto)
1.1202 +
1.1203 +lemma set_remdups[simp]: "set(remdups xs) = set xs"
1.1204 +by(induct xs, simp, simp add:insert_absorb)
1.1205 +
1.1206 +lemma distinct_remdups[iff]: "distinct(remdups xs)"
1.1207 +by(induct xs, auto)
1.1208 +
1.1209 +lemma distinct_filter[simp]: "distinct xs ==> distinct (filter P xs)"
1.1210 +by(induct xs, auto)
1.1211 +
1.1212 +(** replicate **)
1.1213 +section "replicate"
1.1214 +
1.1215 +lemma length_replicate[simp]: "length(replicate n x) = n"
1.1216 +by(induct n, auto)
1.1217 +
1.1218 +lemma map_replicate[simp]: "map f (replicate n x) = replicate n (f x)"
1.1219 +by(induct n, auto)
1.1220 +
1.1221 +lemma replicate_app_Cons_same:
1.1222 + "(replicate n x) @ (x#xs) = x # replicate n x @ xs"
1.1223 +by(induct n, auto)
1.1224 +
1.1225 +lemma rev_replicate[simp]: "rev(replicate n x) = replicate n x"
1.1226 +apply(induct n)
1.1227 + apply simp
1.1229 +done
1.1230 +
1.1231 +lemma replicate_add: "replicate (n+m) x = replicate n x @ replicate m x"
1.1232 +by(induct n, auto)
1.1233 +
1.1234 +lemma hd_replicate[simp]: "n ~= 0 ==> hd(replicate n x) = x"
1.1235 +by(induct n, auto)
1.1236 +
1.1237 +lemma tl_replicate[simp]: "n ~= 0 ==> tl(replicate n x) = replicate (n - 1) x"
1.1238 +by(induct n, auto)
1.1239 +
1.1240 +lemma last_replicate[rule_format,simp]:
1.1241 + "n ~= 0 --> last(replicate n x) = x"
1.1242 +by(induct_tac n, auto)
1.1243 +
1.1244 +lemma nth_replicate[simp]: "!!i. i<n ==> (replicate n x)!i = x"
1.1245 +apply(induct n)
1.1246 + apply simp
1.1248 +done
1.1249 +
1.1250 +lemma set_replicate_Suc: "set(replicate (Suc n) x) = {x}"
1.1251 +by(induct n, auto)
1.1252 +
1.1253 +lemma set_replicate[simp]: "n ~= 0 ==> set(replicate n x) = {x}"
1.1254 +by(fast dest!: not0_implies_Suc intro!: set_replicate_Suc)
1.1255 +
1.1256 +lemma set_replicate_conv_if: "set(replicate n x) = (if n=0 then {} else {x})"
1.1257 +by auto
1.1258 +
1.1259 +lemma in_set_replicateD: "x : set(replicate n y) ==> x=y"
1.1261 +
1.1262 +
1.1263 +(*** Lexcicographic orderings on lists ***)
1.1264 +section"Lexcicographic orderings on lists"
1.1265
1.1266 -val typed_print_translation = [("size", size_tr')];
1.1267 +lemma wf_lexn: "wf r ==> wf(lexn r n)"
1.1268 +apply(induct_tac n)
1.1269 + apply simp
1.1270 +apply simp
1.1271 +apply(rule wf_subset)
1.1272 + prefer 2 apply(rule Int_lower1)
1.1273 +apply(rule wf_prod_fun_image)
1.1274 + prefer 2 apply(rule injI)
1.1275 +apply auto
1.1276 +done
1.1277 +
1.1278 +lemma lexn_length:
1.1279 + "!!xs ys. (xs,ys) : lexn r n ==> length xs = n & length ys = n"
1.1280 +by(induct n, auto)
1.1281 +
1.1282 +lemma wf_lex[intro!]: "wf r ==> wf(lex r)"
1.1283 +apply(unfold lex_def)
1.1284 +apply(rule wf_UN)
1.1285 +apply(blast intro: wf_lexn)
1.1286 +apply clarify
1.1287 +apply(rename_tac m n)
1.1288 +apply(subgoal_tac "m ~= n")
1.1289 + prefer 2 apply blast
1.1290 +apply(blast dest: lexn_length not_sym)
1.1291 +done
1.1292 +
1.1293 +
1.1294 +lemma lexn_conv:
1.1295 + "lexn r n =
1.1296 +  {(xs,ys). length xs = n & length ys = n &
1.1297 +            (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"
1.1298 +apply(induct_tac n)
1.1299 + apply simp
1.1300 + apply blast
1.1302 +apply auto
1.1303 +  apply blast
1.1304 + apply(rename_tac a xys x xs' y ys')
1.1305 + apply(rule_tac x = "a#xys" in exI)
1.1306 + apply simp
1.1307 +apply(case_tac xys)
1.1308 + apply simp_all
1.1309 +apply blast
1.1310 +done
1.1311 +
1.1312 +lemma lex_conv:
1.1313 + "lex r =
1.1314 +  {(xs,ys). length xs = length ys &
1.1315 +            (? xys x y xs' ys'. xs= xys @ x#xs' & ys= xys @ y#ys' & (x,y):r)}"
1.1316 +by(force simp add: lex_def lexn_conv)
1.1317 +
1.1318 +lemma wf_lexico[intro!]: "wf r ==> wf(lexico r)"
1.1319 +by(unfold lexico_def, blast)
1.1320 +
1.1321 +lemma lexico_conv:
1.1322 +"lexico r = {(xs,ys). length xs < length ys |
1.1323 +                      length xs = length ys & (xs,ys) : lex r}"
1.1324 +by(simp add: lexico_def diag_def lex_prod_def measure_def inv_image_def)
1.1325 +
1.1326 +lemma Nil_notin_lex[iff]: "([],ys) ~: lex r"
1.1328 +
1.1329 +lemma Nil2_notin_lex[iff]: "(xs,[]) ~: lex r"
1.1331 +
1.1332 +lemma Cons_in_lex[iff]:
1.1333 + "((x#xs,y#ys) : lex r) =
1.1334 +  ((x,y) : r & length xs = length ys | x=y & (xs,ys) : lex r)"
1.1336 +apply(rule iffI)
1.1337 + prefer 2 apply(blast intro: Cons_eq_appendI)
1.1338 +apply clarify
1.1339 +apply(case_tac xys)
1.1340 + apply simp
1.1341 +apply simp
1.1342 +apply blast
1.1343 +done
1.1344 +
1.1345 +
1.1346 +(*** sublist (a generalization of nth to sets) ***)
1.1347 +
1.1348 +lemma sublist_empty[simp]: "sublist xs {} = []"
1.1350 +
1.1351 +lemma sublist_nil[simp]: "sublist [] A = []"
1.1353 +
1.1354 +lemma sublist_shift_lemma:
1.1355 + "map fst [p:zip xs [i..i + length xs(] . snd p : A] =
1.1356 +  map fst [p:zip xs [0..length xs(] . snd p + i : A]"
1.1357 +apply(induct_tac xs rule: rev_induct)
1.1358 + apply simp
1.1360 +done
1.1361 +
1.1362 +lemma sublist_append:
1.1363 + "sublist (l@l') A = sublist l A @ sublist l' {j. j + length l : A}"
1.1364 +apply(unfold sublist_def)
1.1365 +apply(induct_tac l' rule: rev_induct)
1.1366 + apply simp
1.1369 +done
1.1370 +
1.1371 +lemma sublist_Cons:
1.1372 + "sublist (x#l) A = (if 0:A then [x] else []) @ sublist l {j. Suc j : A}"
1.1373 +apply(induct_tac l rule: rev_induct)
1.1375 +apply(simp del: append_Cons add: append_Cons[symmetric] sublist_append)
1.1376 +done
1.1377 +
1.1378 +lemma sublist_singleton[simp]: "sublist [x] A = (if 0 : A then [x] else [])"
1.1380 +
1.1381 +lemma sublist_upt_eq_take[simp]: "sublist l {..n(} = take n l"
1.1382 +apply(induct_tac l rule: rev_induct)
1.1383 + apply simp
1.1385 +done
1.1386 +
1.1387 +
1.1388 +lemma take_Cons': "take n (x#xs) = (if n=0 then [] else x # take (n - 1) xs)"
1.1389 +by(case_tac n, simp_all)
1.1390 +
1.1391 +lemma drop_Cons': "drop n (x#xs) = (if n=0 then x#xs else drop (n - 1) xs)"
1.1392 +by(case_tac n, simp_all)
1.1393 +
1.1394 +lemma nth_Cons': "(x#xs)!n = (if n=0 then x else xs!(n - 1))"
1.1395 +by(case_tac n, simp_all)
1.1396 +
1.1397 +lemmas [simp] = take_Cons'[of "number_of v",standard]
1.1398 +                drop_Cons'[of "number_of v",standard]
1.1399 +                nth_Cons'[of "number_of v",standard]
1.1400
1.1401  end;
```