src/HOL/Library/Cardinality.thy
 author Andreas Lochbihler Fri Jun 01 14:34:46 2012 +0200 (2012-06-01) changeset 48059 f6ce99d3719b parent 48058 11a732f7d79f child 48060 1f4d00a7f59f permissions -rw-r--r--
simplify card_UNIV type class,
tuned proofs
1 (*  Title:      HOL/Library/Cardinality.thy
2     Author:     Brian Huffman, Andreas Lochbihler
3 *)
5 header {* Cardinality of types *}
7 theory Cardinality
8 imports "~~/src/HOL/Main"
9 begin
11 subsection {* Preliminary lemmas *}
12 (* These should be moved elsewhere *)
14 lemma (in type_definition) univ:
15   "UNIV = Abs ` A"
16 proof
17   show "Abs ` A \<subseteq> UNIV" by (rule subset_UNIV)
18   show "UNIV \<subseteq> Abs ` A"
19   proof
20     fix x :: 'b
21     have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
22     moreover have "Rep x \<in> A" by (rule Rep)
23     ultimately show "x \<in> Abs ` A" by (rule image_eqI)
24   qed
25 qed
27 lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
28   by (simp add: univ card_image inj_on_def Abs_inject)
31 subsection {* Cardinalities of types *}
33 syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
35 translations "CARD('t)" => "CONST card (CONST UNIV \<Colon> 't set)"
37 typed_print_translation (advanced) {*
38   let
39     fun card_univ_tr' ctxt _ [Const (@{const_syntax UNIV}, Type (_, [T, _]))] =
40       Syntax.const @{syntax_const "_type_card"} \$ Syntax_Phases.term_of_typ ctxt T;
41   in [(@{const_syntax card}, card_univ_tr')] end
42 *}
44 lemma card_unit [simp]: "CARD(unit) = 1"
45   unfolding UNIV_unit by simp
47 lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a) * CARD('b)"
48   unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
50 lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
51   unfolding UNIV_Plus_UNIV [symmetric] by (simp only: finite card_Plus)
53 lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
54   unfolding UNIV_option_conv
55   apply (subgoal_tac "(None::'a option) \<notin> range Some")
56   apply (simp add: card_image)
57   apply fast
58   done
60 lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
61   unfolding Pow_UNIV [symmetric]
62   by (simp only: card_Pow finite)
64 lemma card_nat [simp]: "CARD(nat) = 0"
65   by (simp add: card_eq_0_iff)
68 subsection {* Classes with at least 1 and 2  *}
70 text {* Class finite already captures "at least 1" *}
72 lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
73   unfolding neq0_conv [symmetric] by simp
75 lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
76   by (simp add: less_Suc_eq_le [symmetric])
78 text {* Class for cardinality "at least 2" *}
80 class card2 = finite +
81   assumes two_le_card: "2 \<le> CARD('a)"
83 lemma one_less_card: "Suc 0 < CARD('a::card2)"
84   using two_le_card [where 'a='a] by simp
86 lemma one_less_int_card: "1 < int CARD('a::card2)"
87   using one_less_card [where 'a='a] by simp
89 subsection {* A type class for computing the cardinality of types *}
91 definition is_list_UNIV :: "'a list \<Rightarrow> bool"
92 where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"
94 lemmas [code_unfold] = is_list_UNIV_def[abs_def]
96 lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
97 by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric]
98    dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
100 lemma card_UNIV_eq_0_is_list_UNIV_False:
101   "CARD('a) = 0 \<Longrightarrow> is_list_UNIV = (\<lambda>xs :: 'a list. False)"
102 by(simp add: is_list_UNIV_def[abs_def])
104 class card_UNIV =
105   fixes card_UNIV :: "'a itself \<Rightarrow> nat"
106   assumes card_UNIV: "card_UNIV x = CARD('a)"
108 lemma card_UNIV_code [code_unfold]: "CARD('a :: card_UNIV) = card_UNIV TYPE('a)"
109 by(simp add: card_UNIV)
111 subsection {* Instantiations for @{text "card_UNIV"} *}
113 subsubsection {* @{typ "nat"} *}
115 instantiation nat :: card_UNIV begin
116 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
117 instance by intro_classes (simp add: card_UNIV_nat_def)
118 end
120 subsubsection {* @{typ "int"} *}
122 instantiation int :: card_UNIV begin
123 definition "card_UNIV = (\<lambda>a :: int itself. 0)"
124 instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int)
125 end
127 subsubsection {* @{typ "'a list"} *}
129 instantiation list :: (type) card_UNIV begin
130 definition "card_UNIV = (\<lambda>a :: 'a list itself. 0)"
131 instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI)
132 end
134 subsubsection {* @{typ "unit"} *}
136 instantiation unit :: card_UNIV begin
137 definition "card_UNIV = (\<lambda>a :: unit itself. 1)"
138 instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit)
139 end
141 subsubsection {* @{typ "bool"} *}
143 instantiation bool :: card_UNIV begin
144 definition "card_UNIV = (\<lambda>a :: bool itself. 2)"
145 instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool)
146 end
148 subsubsection {* @{typ "char"} *}
150 lemma card_UNIV_char: "card (UNIV :: char set) = 256"
151 proof -
152   from enum_distinct
153   have "card (set (Enum.enum :: char list)) = length (Enum.enum :: char list)"
154     by (rule distinct_card)
155   also have "set Enum.enum = (UNIV :: char set)" by (auto intro: in_enum)
156   also note enum_chars
157   finally show ?thesis by (simp add: chars_def)
158 qed
160 instantiation char :: card_UNIV begin
161 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
162 instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char)
163 end
165 subsubsection {* @{typ "'a \<times> 'b"} *}
167 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
168 definition "card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
169 instance
170   by intro_classes (simp add: card_UNIV_prod_def card_UNIV UNIV_Times_UNIV[symmetric] card_cartesian_product del: UNIV_Times_UNIV)
171 end
173 subsubsection {* @{typ "'a + 'b"} *}
175 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
176 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a + 'b) itself.
177   let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
178   in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
179 instance
180   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)
181 end
183 subsubsection {* @{typ "'a \<Rightarrow> 'b"} *}
185 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
187 definition "card_UNIV =
188   (\<lambda>a :: ('a \<Rightarrow> 'b) itself. let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
189                             in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
191 instance proof
192   fix x :: "('a \<Rightarrow> 'b) itself"
194   { assume "0 < card (UNIV :: 'a set)"
195     and "0 < card (UNIV :: 'b set)"
196     hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
197       by(simp_all only: card_ge_0_finite)
198     from finite_distinct_list[OF finb] obtain bs
199       where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
200     from finite_distinct_list[OF fina] obtain as
201       where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
202     have cb: "card (UNIV :: 'b set) = length bs"
203       unfolding bs[symmetric] distinct_card[OF distb] ..
204     have ca: "card (UNIV :: 'a set) = length as"
205       unfolding as[symmetric] distinct_card[OF dista] ..
206     let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (Enum.n_lists (length as) bs)"
207     have "UNIV = set ?xs"
208     proof(rule UNIV_eq_I)
209       fix f :: "'a \<Rightarrow> 'b"
210       from as have "f = the \<circ> map_of (zip as (map f as))"
211         by(auto simp add: map_of_zip_map)
212       thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
213     qed
214     moreover have "distinct ?xs" unfolding distinct_map
215     proof(intro conjI distinct_n_lists distb inj_onI)
216       fix xs ys :: "'b list"
217       assume xs: "xs \<in> set (Enum.n_lists (length as) bs)"
218         and ys: "ys \<in> set (Enum.n_lists (length as) bs)"
219         and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
220       from xs ys have [simp]: "length xs = length as" "length ys = length as"
221         by(simp_all add: length_n_lists_elem)
222       have "map_of (zip as xs) = map_of (zip as ys)"
223       proof
224         fix x
225         from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y"
226           by(simp_all add: map_of_zip_is_Some[symmetric])
227         with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
228           by(auto dest: fun_cong[where x=x])
229       qed
230       with dista show "xs = ys" by(simp add: map_of_zip_inject)
231     qed
232     hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
233     moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
234     ultimately have "card (UNIV :: ('a \<Rightarrow> 'b) set) = card (UNIV :: 'b set) ^ card (UNIV :: 'a set)"
235       using cb ca by simp }
236   moreover {
237     assume cb: "card (UNIV :: 'b set) = Suc 0"
238     then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
239     have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
240     proof(rule UNIV_eq_I)
241       fix x :: "'a \<Rightarrow> 'b"
242       { fix y
243         have "x y \<in> UNIV" ..
244         hence "x y = b" unfolding b by simp }
245       thus "x \<in> {\<lambda>x. b}" by(auto)
246     qed
247     have "card (UNIV :: ('a \<Rightarrow> 'b) set) = Suc 0" unfolding eq by simp }
248   ultimately show "card_UNIV x = card (UNIV :: ('a \<Rightarrow> 'b) set)"
249     unfolding card_UNIV_fun_def card_UNIV Let_def
250     by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
251 qed
253 end
255 subsubsection {* @{typ "'a option"} *}
257 instantiation option :: (card_UNIV) card_UNIV
258 begin
260 definition "card_UNIV = (\<lambda>a :: 'a option itself. let c = card_UNIV (TYPE('a)) in if c \<noteq> 0 then Suc c else 0)"
262 instance proof
263   fix x :: "'a option itself"
264   show "card_UNIV x = card (UNIV :: 'a option set)"
265     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)
266       (subst card_insert_disjoint, auto simp add: card_eq_0_iff card_image inj_Some intro: finite_imageI card_ge_0_finite)
267 qed
269 end
271 subsection {* Code setup for equality on sets *}
273 definition eq_set :: "'a :: card_UNIV set \<Rightarrow> 'a :: card_UNIV set \<Rightarrow> bool"
274 where [simp, code del]: "eq_set = op ="
276 lemmas [code_unfold] = eq_set_def[symmetric]
278 lemma card_Compl:
279   "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
280 by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
282 lemma eq_set_code [code]:
283   fixes xs ys :: "'a :: card_UNIV list"
284   defines "rhs \<equiv>
285   let n = CARD('a)
286   in if n = 0 then False else
287         let xs' = remdups xs; ys' = remdups ys
288         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')"
289   shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
290   and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
291   and "eq_set (set xs) (set ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)" (is ?thesis3)
292   and "eq_set (List.coset xs) (List.coset ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)" (is ?thesis4)
293 proof -
294   show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
295   proof
296     assume ?lhs thus ?rhs
297       by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_Un_Int[where A="set xs" and B="- set xs"] card_UNIV Compl_partition card_gt_0_iff dest: sym)(metis finite_compl finite_set)
298   next
299     assume ?rhs
300     moreover have "\<lbrakk> \<forall>y\<in>set xs. y \<notin> set ys; \<forall>x\<in>set ys. x \<notin> set xs \<rbrakk> \<Longrightarrow> set xs \<inter> set ys = {}" by blast
301     ultimately show ?lhs
302       by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_UNIV card_gt_0_iff card_Un_Int[where A="set xs" and B="set ys"] dest: card_eq_UNIV_imp_eq_UNIV split: split_if_asm)
303   qed
304   thus ?thesis2 unfolding eq_set_def by blast
305   show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
306 qed
308 (* test code setup *)
309 value [code] "List.coset [True] = set [False] \<and> set [] = List.coset [True, False]"
311 end