src/HOL/List.thy
changeset 17090 603f23d71ada
parent 17086 0eb0c9259dd7
child 17501 acbebb72e85a
equal deleted inserted replaced
17089:f708a31fa8bb 17090:603f23d71ada
  1241 
  1241 
  1242 lemma list_all2_lengthD [intro?]: 
  1242 lemma list_all2_lengthD [intro?]: 
  1243   "list_all2 P xs ys ==> length xs = length ys"
  1243   "list_all2 P xs ys ==> length xs = length ys"
  1244 by (simp add: list_all2_def)
  1244 by (simp add: list_all2_def)
  1245 
  1245 
  1246 lemma list_all2_Nil [iff]: "list_all2 P [] ys = (ys = [])"
  1246 lemma list_all2_Nil [iff,code]: "list_all2 P [] ys = (ys = [])"
  1247 by (simp add: list_all2_def)
  1247 by (simp add: list_all2_def)
  1248 
  1248 
  1249 lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])"
  1249 lemma list_all2_Nil2[iff]: "list_all2 P xs [] = (xs = [])"
  1250 by (simp add: list_all2_def)
  1250 by (simp add: list_all2_def)
  1251 
  1251 
  1252 lemma list_all2_Cons [iff]:
  1252 lemma list_all2_Cons [iff,code]:
  1253 "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
  1253 "list_all2 P (x # xs) (y # ys) = (P x y \<and> list_all2 P xs ys)"
  1254 by (auto simp add: list_all2_def)
  1254 by (auto simp add: list_all2_def)
  1255 
  1255 
  1256 lemma list_all2_Cons1:
  1256 lemma list_all2_Cons1:
  1257 "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
  1257 "list_all2 P (x # xs) ys = (\<exists>z zs. ys = z # zs \<and> P x z \<and> list_all2 P xs zs)"
  1411 by (induct ns) auto
  1411 by (induct ns) auto
  1412 
  1412 
  1413 
  1413 
  1414 subsubsection {* @{text upto} *}
  1414 subsubsection {* @{text upto} *}
  1415 
  1415 
  1416 lemma upt_rec: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"
  1416 lemma upt_rec[code]: "[i..<j] = (if i<j then i#[Suc i..<j] else [])"
  1417 -- {* Does not terminate! *}
  1417 -- {* simp does not terminate! *}
  1418 by (induct j) auto
  1418 by (induct j) auto
  1419 
  1419 
  1420 lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
  1420 lemma upt_conv_Nil [simp]: "j <= i ==> [i..<j] = []"
  1421 by (subst upt_rec) simp
  1421 by (subst upt_rec) simp
  1422 
  1422 
  1932 
  1932 
  1933 text{* Only use @{text mem} for generating executable code.  Otherwise
  1933 text{* Only use @{text mem} for generating executable code.  Otherwise
  1934 use @{prop"x : set xs"} instead --- it is much easier to reason about.
  1934 use @{prop"x : set xs"} instead --- it is much easier to reason about.
  1935 The same is true for @{const list_all} and @{const list_ex}: write
  1935 The same is true for @{const list_all} and @{const list_ex}: write
  1936 @{text"\<forall>x\<in>set xs"} and @{text"\<exists>x\<in>set xs"} instead because the HOL
  1936 @{text"\<forall>x\<in>set xs"} and @{text"\<exists>x\<in>set xs"} instead because the HOL
  1937 quantifiers are aleady known to the automatic provers. For the purpose
  1937 quantifiers are aleady known to the automatic provers. In fact, the declarations in the Code subsection make sure that @{text"\<in>"}, @{text"\<forall>x\<in>set xs"}
  1938 of generating executable code use the theorems @{text mem_iff},
  1938 and @{text"\<exists>x\<in>set xs"} are implemented efficiently.
  1939 @{text list_all_iff} and @{text list_ex_iff} to get rid off or
       
  1940 introduce the combinators.
       
  1941 
  1939 
  1942 The functions @{const itrev}, @{const filtermap} and @{const
  1940 The functions @{const itrev}, @{const filtermap} and @{const
  1943 map_filter} are just there to generate efficient code. Do not use them
  1941 map_filter} are just there to generate efficient code. Do not use them
  1944 for modelling and proving. *}
  1942 for modelling and proving. *}
  1945 
  1943 
  1975 
  1973 
  1976 subsubsection {* Code generation *}
  1974 subsubsection {* Code generation *}
  1977 
  1975 
  1978 text{* Defaults for generating efficient code for some standard functions. *}
  1976 text{* Defaults for generating efficient code for some standard functions. *}
  1979 
  1977 
  1980 lemma [code]: "rev xs = itrev xs []"
  1978 lemmas in_set_code[code unfold] = mem_iff[symmetric, THEN eq_reflection]
       
  1979 
       
  1980 lemma rev_code[code unfold]: "rev xs == itrev xs []"
  1981 by simp
  1981 by simp
  1982 
  1982 
  1983 declare upt_rec[code]
  1983 lemma distinct_Cons_mem[code]: "distinct (x#xs) = (~(x mem xs) \<and> distinct xs)"
  1984 
       
  1985 lemma [code]: "distinct (x#xs) = (~(x mem xs) \<and> distinct xs)"
       
  1986 by (simp add:mem_iff)
  1984 by (simp add:mem_iff)
  1987 
  1985 
  1988 lemma [code]:
  1986 lemma remdups_Cons_mem[code]:
  1989  "remdups (x#xs) = (if x mem xs then remdups xs else x # remdups xs)"
  1987  "remdups (x#xs) = (if x mem xs then remdups xs else x # remdups xs)"
  1990 by (simp add:mem_iff)
  1988 by (simp add:mem_iff)
  1991 
  1989 
  1992 lemma [code]:
  1990 lemma list_inter_Cons_mem[code]:  "list_inter (a#as) bs =
  1993  "list_all2 P xs ys =
       
  1994   (length xs = length ys \<and> (list_all (%(x, y). P x y) (zip xs ys)))"
       
  1995 by (simp add:list_all2_def list_all_iff)
       
  1996 
       
  1997 lemma [code]:  "list_inter (a#as) bs =
       
  1998   (if a mem bs then a#(list_inter as bs) else list_inter as bs)"
  1991   (if a mem bs then a#(list_inter as bs) else list_inter as bs)"
  1999 by(simp add:mem_iff)
  1992 by(simp add:mem_iff)
  2000 
  1993 
  2001 (* If you want to replace bounded quantifiers over lists by list_ex/all:
  1994 text{* For implementing bounded quantifiers over lists by
  2002 
  1995 @{const list_ex}/@{const list_all}: *}
  2003 declare list_ex_iff[symmetric, THEN eq_reflection, code unfold]
  1996 
  2004 declare list_all_iff[symmetric, THEN eq_reflection, code unfold]
  1997 lemmas list_bex_code[code unfold] = list_ex_iff[symmetric, THEN eq_reflection]
  2005 *)
  1998 lemmas list_ball_code[code unfold] = list_all_iff[symmetric, THEN eq_reflection]
  2006 
  1999 
  2007 
  2000 
  2008 subsubsection{* Inductive definition for membership *}
  2001 subsubsection{* Inductive definition for membership *}
  2009 
  2002 
  2010 consts ListMem :: "('a \<times> 'a list)set"
  2003 consts ListMem :: "('a \<times> 'a list)set"
  2134 
  2127 
  2135 subsubsection {* Lexicographic Ordering *}
  2128 subsubsection {* Lexicographic Ordering *}
  2136 
  2129 
  2137 text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
  2130 text {* Classical lexicographic ordering on lists, ie. "a" < "ab" < "b".
  2138     This ordering does \emph{not} preserve well-foundedness.
  2131     This ordering does \emph{not} preserve well-foundedness.
  2139      Author: N. Voelker, March 2005 *} 
  2132      Author: N. Voelker, March 2005. *} 
  2140 
  2133 
  2141 constdefs 
  2134 constdefs 
  2142   lexord :: "('a * 'a)set \<Rightarrow> ('a list * 'a list) set" 
  2135   lexord :: "('a * 'a)set \<Rightarrow> ('a list * 'a list) set" 
  2143   "lexord  r == {(x,y). \<exists> a v. y = x @ a # v \<or> 
  2136   "lexord  r == {(x,y). \<exists> a v. y = x @ a # v \<or> 
  2144             (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}"
  2137             (\<exists> u a b v w. (a,b) \<in> r \<and> x = u @ (a # v) \<and> y = u @ (b # w))}"