merged
authorhuffman
Sat Jun 02 08:32:42 2012 +0200 (2012-06-02)
changeset 480679f458b3efb5c
parent 48066 c6783c9b87bf
parent 48062 9014e78ccde2
child 48068 04aeda922be2
merged
src/HOL/Library/Cardinality.thy
     1.1 --- a/doc-src/IsarRef/isar-ref.tex	Sat Jun 02 08:27:29 2012 +0200
     1.2 +++ b/doc-src/IsarRef/isar-ref.tex	Sat Jun 02 08:32:42 2012 +0200
     1.3 @@ -81,10 +81,12 @@
     1.4  \input{Thy/document/ML_Tactic.tex}
     1.5  
     1.6  \begingroup
     1.7 +  \tocentry{\bibname}
     1.8    \bibliographystyle{abbrv} \small\raggedright\frenchspacing
     1.9    \bibliography{../manual}
    1.10  \endgroup
    1.11  
    1.12 +\tocentry{\indexname}
    1.13  \printindex
    1.14  
    1.15  \end{document}
     2.1 --- a/doc-src/System/system.tex	Sat Jun 02 08:27:29 2012 +0200
     2.2 +++ b/doc-src/System/system.tex	Sat Jun 02 08:32:42 2012 +0200
     2.3 @@ -35,10 +35,12 @@
     2.4  \input{Thy/document/Misc.tex}
     2.5  
     2.6  \begingroup
     2.7 +  \tocentry{\bibname}
     2.8    \bibliographystyle{abbrv} \small\raggedright\frenchspacing
     2.9    \bibliography{../manual}
    2.10  \endgroup
    2.11  
    2.12 +\tocentry{\indexname}
    2.13  \printindex
    2.14  
    2.15  \end{document}
     3.1 --- a/src/HOL/Library/Cardinality.thy	Sat Jun 02 08:27:29 2012 +0200
     3.2 +++ b/src/HOL/Library/Cardinality.thy	Sat Jun 02 08:32:42 2012 +0200
     3.3 @@ -27,6 +27,9 @@
     3.4  lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
     3.5    by (simp add: univ card_image inj_on_def Abs_inject)
     3.6  
     3.7 +lemma finite_range_Some: "finite (range (Some :: 'a \<Rightarrow> 'a option)) = finite (UNIV :: 'a set)"
     3.8 +by(auto dest: finite_imageD intro: inj_Some)
     3.9 +
    3.10  
    3.11  subsection {* Cardinalities of types *}
    3.12  
    3.13 @@ -41,197 +44,47 @@
    3.14    in [(@{const_syntax card}, card_univ_tr')] end
    3.15  *}
    3.16  
    3.17 -lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a::finite) * CARD('b::finite)"
    3.18 +lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a) * CARD('b)"
    3.19    unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
    3.20  
    3.21 +lemma card_UNIV_sum: "CARD('a + 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 then CARD('a) + CARD('b) else 0)"
    3.22 +unfolding UNIV_Plus_UNIV[symmetric]
    3.23 +by(auto simp add: card_eq_0_iff card_Plus simp del: UNIV_Plus_UNIV)
    3.24 +
    3.25  lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
    3.26 -  unfolding UNIV_Plus_UNIV [symmetric] by (simp only: finite card_Plus)
    3.27 +by(simp add: card_UNIV_sum)
    3.28 +
    3.29 +lemma card_UNIV_option: "CARD('a option) = (if CARD('a) = 0 then 0 else CARD('a) + 1)"
    3.30 +proof -
    3.31 +  have "(None :: 'a option) \<notin> range Some" by clarsimp
    3.32 +  thus ?thesis
    3.33 +    by(simp add: UNIV_option_conv card_eq_0_iff finite_range_Some card_insert_disjoint card_image)
    3.34 +qed
    3.35  
    3.36  lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
    3.37 -  unfolding UNIV_option_conv
    3.38 -  apply (subgoal_tac "(None::'a option) \<notin> range Some")
    3.39 -  apply (simp add: card_image)
    3.40 -  apply fast
    3.41 -  done
    3.42 +by(simp add: card_UNIV_option)
    3.43 +
    3.44 +lemma card_UNIV_set: "CARD('a set) = (if CARD('a) = 0 then 0 else 2 ^ CARD('a))"
    3.45 +by(simp add: Pow_UNIV[symmetric] card_eq_0_iff card_Pow del: Pow_UNIV)
    3.46  
    3.47  lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
    3.48 -  unfolding Pow_UNIV [symmetric]
    3.49 -  by (simp only: card_Pow finite)
    3.50 +by(simp add: card_UNIV_set)
    3.51  
    3.52  lemma card_nat [simp]: "CARD(nat) = 0"
    3.53    by (simp add: card_eq_0_iff)
    3.54  
    3.55 -
    3.56 -subsection {* Classes with at least 1 and 2  *}
    3.57 -
    3.58 -text {* Class finite already captures "at least 1" *}
    3.59 -
    3.60 -lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
    3.61 -  unfolding neq0_conv [symmetric] by simp
    3.62 -
    3.63 -lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
    3.64 -  by (simp add: less_Suc_eq_le [symmetric])
    3.65 -
    3.66 -text {* Class for cardinality "at least 2" *}
    3.67 -
    3.68 -class card2 = finite + 
    3.69 -  assumes two_le_card: "2 \<le> CARD('a)"
    3.70 -
    3.71 -lemma one_less_card: "Suc 0 < CARD('a::card2)"
    3.72 -  using two_le_card [where 'a='a] by simp
    3.73 -
    3.74 -lemma one_less_int_card: "1 < int CARD('a::card2)"
    3.75 -  using one_less_card [where 'a='a] by simp
    3.76 -
    3.77 -subsection {* A type class for computing the cardinality of types *}
    3.78 -
    3.79 -class card_UNIV = 
    3.80 -  fixes card_UNIV :: "'a itself \<Rightarrow> nat"
    3.81 -  assumes card_UNIV: "card_UNIV x = card (UNIV :: 'a set)"
    3.82 -begin
    3.83 -
    3.84 -lemma card_UNIV_neq_0_finite_UNIV:
    3.85 -  "card_UNIV x \<noteq> 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
    3.86 -by(simp add: card_UNIV card_eq_0_iff)
    3.87 -
    3.88 -lemma card_UNIV_ge_0_finite_UNIV:
    3.89 -  "card_UNIV x > 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
    3.90 -by(auto simp add: card_UNIV intro: card_ge_0_finite finite_UNIV_card_ge_0)
    3.91 -
    3.92 -lemma card_UNIV_eq_0_infinite_UNIV:
    3.93 -  "card_UNIV x = 0 \<longleftrightarrow> \<not> finite (UNIV :: 'a set)"
    3.94 -by(simp add: card_UNIV card_eq_0_iff)
    3.95 -
    3.96 -definition is_list_UNIV :: "'a list \<Rightarrow> bool"
    3.97 -where "is_list_UNIV xs = (let c = card_UNIV (TYPE('a)) in if c = 0 then False else size (remdups xs) = c)"
    3.98 -
    3.99 -lemma is_list_UNIV_iff: fixes xs :: "'a list"
   3.100 -  shows "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
   3.101 -proof
   3.102 -  assume "is_list_UNIV xs"
   3.103 -  hence c: "card_UNIV (TYPE('a)) > 0" and xs: "size (remdups xs) = card_UNIV (TYPE('a))"
   3.104 -    unfolding is_list_UNIV_def by(simp_all add: Let_def split: split_if_asm)
   3.105 -  from c have fin: "finite (UNIV :: 'a set)" by(auto simp add: card_UNIV_ge_0_finite_UNIV)
   3.106 -  have "card (set (remdups xs)) = size (remdups xs)" by(subst distinct_card) auto
   3.107 -  also note set_remdups
   3.108 -  finally show "set xs = UNIV" using fin unfolding xs card_UNIV by-(rule card_eq_UNIV_imp_eq_UNIV)
   3.109 -next
   3.110 -  assume xs: "set xs = UNIV"
   3.111 -  from finite_set[of xs] have fin: "finite (UNIV :: 'a set)" unfolding xs .
   3.112 -  hence "card_UNIV (TYPE ('a)) \<noteq> 0" unfolding card_UNIV_neq_0_finite_UNIV .
   3.113 -  moreover have "size (remdups xs) = card (set (remdups xs))"
   3.114 -    by(subst distinct_card) auto
   3.115 -  ultimately show "is_list_UNIV xs" using xs by(simp add: is_list_UNIV_def Let_def card_UNIV)
   3.116 -qed
   3.117 -
   3.118 -lemma card_UNIV_eq_0_is_list_UNIV_False:
   3.119 -  assumes cU0: "card_UNIV x = 0"
   3.120 -  shows "is_list_UNIV = (\<lambda>xs. False)"
   3.121 -proof(rule ext)
   3.122 -  fix xs :: "'a list"
   3.123 -  from cU0 have "\<not> finite (UNIV :: 'a set)"
   3.124 -    by(auto simp only: card_UNIV_eq_0_infinite_UNIV)
   3.125 -  moreover have "finite (set xs)" by(rule finite_set)
   3.126 -  ultimately have "(UNIV :: 'a set) \<noteq> set xs" by(auto simp del: finite_set)
   3.127 -  thus "is_list_UNIV xs = False" unfolding is_list_UNIV_iff by simp
   3.128 -qed
   3.129 -
   3.130 -end
   3.131 -
   3.132 -subsection {* Instantiations for @{text "card_UNIV"} *}
   3.133 -
   3.134 -subsubsection {* @{typ "nat"} *}
   3.135 -
   3.136 -instantiation nat :: card_UNIV begin
   3.137 -definition "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
   3.138 -instance by intro_classes (simp add: card_UNIV_nat_def)
   3.139 -end
   3.140 -
   3.141 -subsubsection {* @{typ "int"} *}
   3.142 -
   3.143 -instantiation int :: card_UNIV begin
   3.144 -definition "card_UNIV = (\<lambda>a :: int itself. 0)"
   3.145 -instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int)
   3.146 -end
   3.147 -
   3.148 -subsubsection {* @{typ "'a list"} *}
   3.149 -
   3.150 -instantiation list :: (type) card_UNIV begin
   3.151 -definition "card_UNIV = (\<lambda>a :: 'a list itself. 0)"
   3.152 -instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI)
   3.153 -end
   3.154 -
   3.155 -subsubsection {* @{typ "unit"} *}
   3.156 -
   3.157 -instantiation unit :: card_UNIV begin
   3.158 -definition "card_UNIV = (\<lambda>a :: unit itself. 1)"
   3.159 -instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit)
   3.160 -end
   3.161 -
   3.162 -subsubsection {* @{typ "bool"} *}
   3.163 -
   3.164 -instantiation bool :: card_UNIV begin
   3.165 -definition "card_UNIV = (\<lambda>a :: bool itself. 2)"
   3.166 -instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool)
   3.167 -end
   3.168 -
   3.169 -subsubsection {* @{typ "char"} *}
   3.170 -
   3.171 -lemma card_UNIV_char: "card (UNIV :: char set) = 256"
   3.172 +lemma card_fun: "CARD('a \<Rightarrow> 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 \<or> CARD('b) = 1 then CARD('b) ^ CARD('a) else 0)"
   3.173  proof -
   3.174 -  from enum_distinct
   3.175 -  have "card (set (Enum.enum :: char list)) = length (Enum.enum :: char list)"
   3.176 -    by (rule distinct_card)
   3.177 -  also have "set Enum.enum = (UNIV :: char set)" by (auto intro: in_enum)
   3.178 -  also note enum_chars
   3.179 -  finally show ?thesis by (simp add: chars_def)
   3.180 -qed
   3.181 -
   3.182 -instantiation char :: card_UNIV begin
   3.183 -definition "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
   3.184 -instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char)
   3.185 -end
   3.186 -
   3.187 -subsubsection {* @{typ "'a \<times> 'b"} *}
   3.188 -
   3.189 -instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
   3.190 -definition "card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
   3.191 -instance 
   3.192 -  by intro_classes (simp add: card_UNIV_prod_def card_UNIV UNIV_Times_UNIV[symmetric] card_cartesian_product del: UNIV_Times_UNIV)
   3.193 -end
   3.194 -
   3.195 -subsubsection {* @{typ "'a + 'b"} *}
   3.196 -
   3.197 -instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
   3.198 -definition "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a + 'b) itself. 
   3.199 -  let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   3.200 -  in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
   3.201 -instance
   3.202 -  by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_eq_0_iff UNIV_Plus_UNIV[symmetric] finite_Plus_iff Let_def card_Plus simp del: UNIV_Plus_UNIV dest!: card_ge_0_finite)
   3.203 -end
   3.204 -
   3.205 -subsubsection {* @{typ "'a \<Rightarrow> 'b"} *}
   3.206 -
   3.207 -instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
   3.208 -
   3.209 -definition "card_UNIV = 
   3.210 -  (\<lambda>a :: ('a \<Rightarrow> 'b) itself. let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   3.211 -                            in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
   3.212 -
   3.213 -instance proof
   3.214 -  fix x :: "('a \<Rightarrow> 'b) itself"
   3.215 -
   3.216 -  { assume "0 < card (UNIV :: 'a set)"
   3.217 -    and "0 < card (UNIV :: 'b set)"
   3.218 +  {  assume "0 < CARD('a)" and "0 < CARD('b)"
   3.219      hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
   3.220        by(simp_all only: card_ge_0_finite)
   3.221      from finite_distinct_list[OF finb] obtain bs 
   3.222        where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
   3.223      from finite_distinct_list[OF fina] obtain as
   3.224        where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
   3.225 -    have cb: "card (UNIV :: 'b set) = length bs"
   3.226 +    have cb: "CARD('b) = length bs"
   3.227        unfolding bs[symmetric] distinct_card[OF distb] ..
   3.228 -    have ca: "card (UNIV :: 'a set) = length as"
   3.229 +    have ca: "CARD('a) = length as"
   3.230        unfolding as[symmetric] distinct_card[OF dista] ..
   3.231      let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (Enum.n_lists (length as) bs)"
   3.232      have "UNIV = set ?xs"
   3.233 @@ -261,10 +114,9 @@
   3.234      qed
   3.235      hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
   3.236      moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
   3.237 -    ultimately have "card (UNIV :: ('a \<Rightarrow> 'b) set) = card (UNIV :: 'b set) ^ card (UNIV :: 'a set)"
   3.238 -      using cb ca by simp }
   3.239 +    ultimately have "CARD('a \<Rightarrow> 'b) = CARD('b) ^ CARD('a)" using cb ca by simp }
   3.240    moreover {
   3.241 -    assume cb: "card (UNIV :: 'b set) = Suc 0"
   3.242 +    assume cb: "CARD('b) = 1"
   3.243      then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
   3.244      have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
   3.245      proof(rule UNIV_eq_I)
   3.246 @@ -274,45 +126,224 @@
   3.247          hence "x y = b" unfolding b by simp }
   3.248        thus "x \<in> {\<lambda>x. b}" by(auto)
   3.249      qed
   3.250 -    have "card (UNIV :: ('a \<Rightarrow> 'b) set) = Suc 0" unfolding eq by simp }
   3.251 -  ultimately show "card_UNIV x = card (UNIV :: ('a \<Rightarrow> 'b) set)"
   3.252 -    unfolding card_UNIV_fun_def card_UNIV Let_def
   3.253 +    have "CARD('a \<Rightarrow> 'b) = 1" unfolding eq by simp }
   3.254 +  ultimately show ?thesis
   3.255      by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
   3.256  qed
   3.257  
   3.258 +lemma card_nibble: "CARD(nibble) = 16"
   3.259 +unfolding UNIV_nibble by simp
   3.260 +
   3.261 +lemma card_UNIV_char: "CARD(char) = 256"
   3.262 +proof -
   3.263 +  have "inj (\<lambda>(x, y). Char x y)" by(auto intro: injI)
   3.264 +  thus ?thesis unfolding UNIV_char by(simp add: card_image card_nibble)
   3.265 +qed
   3.266 +
   3.267 +lemma card_literal: "CARD(String.literal) = 0"
   3.268 +proof -
   3.269 +  have "inj STR" by(auto intro: injI)
   3.270 +  thus ?thesis by(simp add: type_definition.univ[OF type_definition_literal] card_image infinite_UNIV_listI)
   3.271 +qed
   3.272 +
   3.273 +subsection {* Classes with at least 1 and 2  *}
   3.274 +
   3.275 +text {* Class finite already captures "at least 1" *}
   3.276 +
   3.277 +lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
   3.278 +  unfolding neq0_conv [symmetric] by simp
   3.279 +
   3.280 +lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
   3.281 +  by (simp add: less_Suc_eq_le [symmetric])
   3.282 +
   3.283 +text {* Class for cardinality "at least 2" *}
   3.284 +
   3.285 +class card2 = finite + 
   3.286 +  assumes two_le_card: "2 \<le> CARD('a)"
   3.287 +
   3.288 +lemma one_less_card: "Suc 0 < CARD('a::card2)"
   3.289 +  using two_le_card [where 'a='a] by simp
   3.290 +
   3.291 +lemma one_less_int_card: "1 < int CARD('a::card2)"
   3.292 +  using one_less_card [where 'a='a] by simp
   3.293 +
   3.294 +subsection {* A type class for computing the cardinality of types *}
   3.295 +
   3.296 +definition is_list_UNIV :: "'a list \<Rightarrow> bool"
   3.297 +where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"
   3.298 +
   3.299 +lemmas [code_unfold] = is_list_UNIV_def[abs_def]
   3.300 +
   3.301 +lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
   3.302 +by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric] 
   3.303 +   dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
   3.304 +
   3.305 +class card_UNIV = 
   3.306 +  fixes card_UNIV :: "'a itself \<Rightarrow> nat"
   3.307 +  assumes card_UNIV: "card_UNIV x = CARD('a)"
   3.308 +
   3.309 +lemma card_UNIV_code [code_unfold]: "CARD('a :: card_UNIV) = card_UNIV TYPE('a)"
   3.310 +by(simp add: card_UNIV)
   3.311 +
   3.312 +lemma finite_UNIV_conv_card_UNIV [code_unfold]:
   3.313 +  "finite (UNIV :: 'a :: card_UNIV set) \<longleftrightarrow> card_UNIV TYPE('a) > 0"
   3.314 +by(simp add: card_UNIV card_gt_0_iff)
   3.315 +
   3.316 +subsection {* Instantiations for @{text "card_UNIV"} *}
   3.317 +
   3.318 +instantiation nat :: card_UNIV begin
   3.319 +definition "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
   3.320 +instance by intro_classes (simp add: card_UNIV_nat_def)
   3.321 +end
   3.322 +
   3.323 +instantiation int :: card_UNIV begin
   3.324 +definition "card_UNIV = (\<lambda>a :: int itself. 0)"
   3.325 +instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int)
   3.326 +end
   3.327 +
   3.328 +instantiation list :: (type) card_UNIV begin
   3.329 +definition "card_UNIV = (\<lambda>a :: 'a list itself. 0)"
   3.330 +instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI)
   3.331 +end
   3.332 +
   3.333 +instantiation unit :: card_UNIV begin
   3.334 +definition "card_UNIV = (\<lambda>a :: unit itself. 1)"
   3.335 +instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit)
   3.336 +end
   3.337 +
   3.338 +instantiation bool :: card_UNIV begin
   3.339 +definition "card_UNIV = (\<lambda>a :: bool itself. 2)"
   3.340 +instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool)
   3.341  end
   3.342  
   3.343 -subsubsection {* @{typ "'a option"} *}
   3.344 +instantiation char :: card_UNIV begin
   3.345 +definition "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
   3.346 +instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char)
   3.347 +end
   3.348  
   3.349 -instantiation option :: (card_UNIV) card_UNIV
   3.350 -begin
   3.351 +instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
   3.352 +definition "card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
   3.353 +instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
   3.354 +end
   3.355  
   3.356 -definition "card_UNIV = (\<lambda>a :: 'a option itself. let c = card_UNIV (TYPE('a)) in if c \<noteq> 0 then Suc c else 0)"
   3.357 +instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
   3.358 +definition "card_UNIV = (\<lambda>a :: ('a + 'b) itself. 
   3.359 +  let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   3.360 +  in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
   3.361 +instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
   3.362 +end
   3.363  
   3.364 -instance proof
   3.365 -  fix x :: "'a option itself"
   3.366 -  show "card_UNIV x = card (UNIV :: 'a option set)"
   3.367 -    by(auto simp add: UNIV_option_conv card_UNIV_option_def card_UNIV card_eq_0_iff Let_def intro: inj_Some dest: finite_imageD)
   3.368 -      (subst card_insert_disjoint, auto simp add: card_eq_0_iff card_image inj_Some intro: finite_imageI card_ge_0_finite)
   3.369 -qed
   3.370 +instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
   3.371 +definition "card_UNIV = (\<lambda>a :: ('a \<Rightarrow> 'b) itself. 
   3.372 +  let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   3.373 +  in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
   3.374 +instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
   3.375 +end
   3.376  
   3.377 +instantiation option :: (card_UNIV) card_UNIV begin
   3.378 +definition "card_UNIV = (\<lambda>a :: 'a option itself. 
   3.379 +  let c = card_UNIV (TYPE('a)) in if c \<noteq> 0 then Suc c else 0)"
   3.380 +instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
   3.381 +end
   3.382 +
   3.383 +instantiation String.literal :: card_UNIV begin
   3.384 +definition "card_UNIV = (\<lambda>a :: String.literal itself. 0)"
   3.385 +instance by intro_classes (simp add: card_UNIV_literal_def card_literal)
   3.386 +end
   3.387 +
   3.388 +instantiation set :: (card_UNIV) card_UNIV begin
   3.389 +definition "card_UNIV = (\<lambda>a :: 'a set itself.
   3.390 +  let c = card_UNIV (TYPE('a)) in if c = 0 then 0 else 2 ^ c)"
   3.391 +instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
   3.392  end
   3.393  
   3.394 -subsection {* Code setup for equality on sets *}
   3.395 +
   3.396 +lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]"
   3.397 +by(auto intro: finite_1.exhaust)
   3.398 +
   3.399 +lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]"
   3.400 +by(auto intro: finite_2.exhaust)
   3.401 +
   3.402 +lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^isub>1, finite_3.a\<^isub>2, finite_3.a\<^isub>3]"
   3.403 +by(auto intro: finite_3.exhaust)
   3.404 +
   3.405 +lemma UNIV_finite_4: "UNIV = set [finite_4.a\<^isub>1, finite_4.a\<^isub>2, finite_4.a\<^isub>3, finite_4.a\<^isub>4]"
   3.406 +by(auto intro: finite_4.exhaust)
   3.407 +
   3.408 +lemma UNIV_finite_5:
   3.409 +  "UNIV = set [finite_5.a\<^isub>1, finite_5.a\<^isub>2, finite_5.a\<^isub>3, finite_5.a\<^isub>4, finite_5.a\<^isub>5]"
   3.410 +by(auto intro: finite_5.exhaust)
   3.411 +
   3.412 +instantiation Enum.finite_1 :: card_UNIV begin
   3.413 +definition "card_UNIV = (\<lambda>a :: Enum.finite_1 itself. 1)"
   3.414 +instance by intro_classes (simp add: UNIV_finite_1 card_UNIV_finite_1_def)
   3.415 +end
   3.416  
   3.417 -definition eq_set :: "'a :: card_UNIV set \<Rightarrow> 'a :: card_UNIV set \<Rightarrow> bool"
   3.418 -where [simp, code del]: "eq_set = op ="
   3.419 +instantiation Enum.finite_2 :: card_UNIV begin
   3.420 +definition "card_UNIV = (\<lambda>a :: Enum.finite_2 itself. 2)"
   3.421 +instance by intro_classes (simp add: UNIV_finite_2 card_UNIV_finite_2_def)
   3.422 +end
   3.423 +
   3.424 +instantiation Enum.finite_3 :: card_UNIV begin
   3.425 +definition "card_UNIV = (\<lambda>a :: Enum.finite_3 itself. 3)"
   3.426 +instance by intro_classes (simp add: UNIV_finite_3 card_UNIV_finite_3_def)
   3.427 +end
   3.428  
   3.429 -lemmas [code_unfold] = eq_set_def[symmetric]
   3.430 +instantiation Enum.finite_4 :: card_UNIV begin
   3.431 +definition "card_UNIV = (\<lambda>a :: Enum.finite_4 itself. 4)"
   3.432 +instance by intro_classes (simp add: UNIV_finite_4 card_UNIV_finite_4_def)
   3.433 +end
   3.434 +
   3.435 +instantiation Enum.finite_5 :: card_UNIV begin
   3.436 +definition "card_UNIV = (\<lambda>a :: Enum.finite_5 itself. 5)"
   3.437 +instance by intro_classes (simp add: UNIV_finite_5 card_UNIV_finite_5_def)
   3.438 +end
   3.439 +
   3.440 +subsection {* Code setup for sets *}
   3.441  
   3.442  lemma card_Compl:
   3.443    "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
   3.444  by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
   3.445  
   3.446 +context fixes xs :: "'a :: card_UNIV list"
   3.447 +begin
   3.448 +
   3.449 +definition card' :: "'a set \<Rightarrow> nat" 
   3.450 +where [simp, code del, code_abbrev]: "card' = card"
   3.451 +
   3.452 +lemma card'_code [code]:
   3.453 +  "card' (set xs) = length (remdups xs)"
   3.454 +  "card' (List.coset xs) =  card_UNIV TYPE('a) - length (remdups xs)"
   3.455 +by(simp_all add: List.card_set card_Compl card_UNIV)
   3.456 +
   3.457 +lemma card'_UNIV [code_unfold]: "card' (UNIV :: 'a :: card_UNIV set) = card_UNIV TYPE('a)"
   3.458 +by(simp add: card_UNIV)
   3.459 +
   3.460 +definition finite' :: "'a set \<Rightarrow> bool"
   3.461 +where [simp, code del, code_abbrev]: "finite' = finite"
   3.462 +
   3.463 +lemma finite'_code [code]:
   3.464 +  "finite' (set xs) \<longleftrightarrow> True"
   3.465 +  "finite' (List.coset xs) \<longleftrightarrow> CARD('a) > 0"
   3.466 +by(simp_all add: card_gt_0_iff)
   3.467 +
   3.468 +definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   3.469 +where [simp, code del, code_abbrev]: "subset' = op \<subseteq>"
   3.470 +
   3.471 +lemma subset'_code [code]:
   3.472 +  "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
   3.473 +  "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
   3.474 +  "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
   3.475 +by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card])
   3.476 +  (metis finite_compl finite_set rev_finite_subset)
   3.477 +
   3.478 +definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   3.479 +where [simp, code del, code_abbrev]: "eq_set = op ="
   3.480 +
   3.481  lemma eq_set_code [code]:
   3.482 -  fixes xs ys :: "'a :: card_UNIV list"
   3.483 +  fixes ys
   3.484    defines "rhs \<equiv> 
   3.485 -  let n = card_UNIV TYPE('a)
   3.486 +  let n = CARD('a)
   3.487    in if n = 0 then False else 
   3.488          let xs' = remdups xs; ys' = remdups ys 
   3.489          in length xs' + length ys' = n \<and> (\<forall>x \<in> set xs'. x \<notin> set ys') \<and> (\<forall>y \<in> set ys'. y \<notin> set xs')"
   3.490 @@ -335,7 +366,13 @@
   3.491    show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
   3.492  qed
   3.493  
   3.494 -(* test code setup *)
   3.495 -value [code] "List.coset [True] = set [False] \<and> set [] = List.coset [True, False]"
   3.496 +end
   3.497 +
   3.498 +notepad begin (* test code setup *)
   3.499 +have "List.coset [True] = set [False] \<and> List.coset [] \<subseteq> List.set [True, False] \<and> finite (List.coset [True])"
   3.500 +  by eval
   3.501 +end
   3.502 +
   3.503 +hide_const (open) card' finite' subset' eq_set
   3.504  
   3.505  end
     4.1 --- a/src/HOL/Library/FinFun.thy	Sat Jun 02 08:27:29 2012 +0200
     4.2 +++ b/src/HOL/Library/FinFun.thy	Sat Jun 02 08:32:42 2012 +0200
     4.3 @@ -435,8 +435,8 @@
     4.4  by transfer (simp add: finfun_default_aux_update_const)
     4.5  
     4.6  lemma finfun_default_const_code [code]:
     4.7 -  "finfun_default ((K$ c) :: ('a :: card_UNIV) \<Rightarrow>f 'b) = (if card_UNIV (TYPE('a)) = 0 then c else undefined)"
     4.8 -by(simp add: finfun_default_const card_UNIV_eq_0_infinite_UNIV)
     4.9 +  "finfun_default ((K$ c) :: 'a \<Rightarrow>f 'b) = (if CARD('a) = 0 then c else undefined)"
    4.10 +by(simp add: finfun_default_const)
    4.11  
    4.12  lemma finfun_default_update_code [code]:
    4.13    "finfun_default (finfun_update_code f a b) = finfun_default f"
    4.14 @@ -1285,9 +1285,8 @@
    4.15  
    4.16  lemma finfun_dom_const_code [code]:
    4.17    "finfun_dom ((K$ c) :: ('a :: card_UNIV) \<Rightarrow>f 'b) = 
    4.18 -   (if card_UNIV (TYPE('a)) = 0 then (K$ False) else FinFun.code_abort (\<lambda>_. finfun_dom (K$ c)))"
    4.19 -unfolding card_UNIV_eq_0_infinite_UNIV
    4.20 -by(simp add: finfun_dom_const)
    4.21 +   (if CARD('a) = 0 then (K$ False) else FinFun.code_abort (\<lambda>_. finfun_dom (K$ c)))"
    4.22 +by(simp add: finfun_dom_const card_UNIV card_eq_0_iff)
    4.23  
    4.24  lemma finfun_dom_finfunI: "(\<lambda>a. f $ a \<noteq> finfun_default f) \<in> finfun"
    4.25  using finite_finfun_default[of f]
    4.26 @@ -1349,9 +1348,8 @@
    4.27  
    4.28  lemma finfun_to_list_const_code [code]:
    4.29    "finfun_to_list ((K$ c) :: ('a :: {linorder, card_UNIV} \<Rightarrow>f 'b)) =
    4.30 -   (if card_UNIV (TYPE('a)) = 0 then [] else FinFun.code_abort (\<lambda>_. finfun_to_list ((K$ c) :: ('a \<Rightarrow>f 'b))))"
    4.31 -unfolding card_UNIV_eq_0_infinite_UNIV
    4.32 -by(auto simp add: finfun_to_list_const)
    4.33 +   (if CARD('a) = 0 then [] else FinFun.code_abort (\<lambda>_. finfun_to_list ((K$ c) :: ('a \<Rightarrow>f 'b))))"
    4.34 +by(auto simp add: finfun_to_list_const card_UNIV card_eq_0_iff)
    4.35  
    4.36  lemma remove1_insort_insert_same:
    4.37    "x \<notin> set xs \<Longrightarrow> remove1 x (insort_insert x xs) = xs"
     5.1 --- a/src/HOL/ex/FinFunPred.thy	Sat Jun 02 08:27:29 2012 +0200
     5.2 +++ b/src/HOL/ex/FinFunPred.thy	Sat Jun 02 08:32:42 2012 +0200
     5.3 @@ -258,4 +258,11 @@
     5.4    by eval
     5.5  end
     5.6  
     5.7 +declare iso_finfun_Ball_Ball[code_unfold]
     5.8 +notepad begin
     5.9 +have "(\<forall>x. ((\<lambda>_ :: nat. False)(1 := True, 2 := True)) x \<longrightarrow> x < 3)"
    5.10 +  by eval
    5.11 +end
    5.12 +declare iso_finfun_Ball_Ball[code_unfold del]
    5.13 +
    5.14  end
    5.15 \ No newline at end of file
     6.1 --- a/src/HOL/ex/Set_Comprehension_Pointfree_Tests.thy	Sat Jun 02 08:27:29 2012 +0200
     6.2 +++ b/src/HOL/ex/Set_Comprehension_Pointfree_Tests.thy	Sat Jun 02 08:32:42 2012 +0200
     6.3 @@ -7,7 +7,7 @@
     6.4  
     6.5  theory Set_Comprehension_Pointfree_Tests
     6.6  imports Main
     6.7 -uses "~~/src/HOL/ex/set_comprehension_pointfree.ML"
     6.8 +uses "set_comprehension_pointfree.ML"
     6.9  begin
    6.10  
    6.11  simproc_setup finite_Collect ("finite (Collect P)") = {* Set_Comprehension_Pointfree.simproc *}
     7.1 --- a/src/HOL/ex/set_comprehension_pointfree.ML	Sat Jun 02 08:27:29 2012 +0200
     7.2 +++ b/src/HOL/ex/set_comprehension_pointfree.ML	Sat Jun 02 08:32:42 2012 +0200
     7.3 @@ -1,4 +1,4 @@
     7.4 -(*  Title:      HOL/Tools/set_comprehension_pointfree.ML
     7.5 +(*  Title:      HOL/ex/set_comprehension_pointfree.ML
     7.6      Author:     Felix Kuperjans, Lukas Bulwahn, TU Muenchen
     7.7  
     7.8  Simproc for rewriting set comprehensions to pointfree expressions.
     8.1 --- a/src/Pure/System/isabelle_process.ML	Sat Jun 02 08:27:29 2012 +0200
     8.2 +++ b/src/Pure/System/isabelle_process.ML	Sat Jun 02 08:32:42 2012 +0200
     8.3 @@ -57,7 +57,8 @@
     8.4      NONE => error ("Undefined Isabelle process command " ^ quote name)
     8.5    | SOME cmd =>
     8.6        (Runtime.debugging cmd args handle exn =>
     8.7 -        error ("Isabelle process protocol failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn)));
     8.8 +        error ("Isabelle process protocol failure: " ^ quote name ^ "\n" ^
     8.9 +          ML_Compiler.exn_message exn)));
    8.10  
    8.11  end;
    8.12