src/HOL/Library/Cardinality.thy
 author Andreas Lochbihler Fri Jun 01 15:33:31 2012 +0200 (2012-06-01) changeset 48060 1f4d00a7f59f parent 48059 f6ce99d3719b child 48062 9014e78ccde2 permissions -rw-r--r--
more instantiations for card_UNIV,
more lemmas for CARD
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)
30 lemma finite_range_Some: "finite (range (Some :: 'a \<Rightarrow> 'a option)) = finite (UNIV :: 'a set)"
31 by(auto dest: finite_imageD intro: inj_Some)
34 subsection {* Cardinalities of types *}
36 syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
38 translations "CARD('t)" => "CONST card (CONST UNIV \<Colon> 't set)"
40 typed_print_translation (advanced) {*
41   let
42     fun card_univ_tr' ctxt _ [Const (@{const_syntax UNIV}, Type (_, [T, _]))] =
43       Syntax.const @{syntax_const "_type_card"} \$ Syntax_Phases.term_of_typ ctxt T;
44   in [(@{const_syntax card}, card_univ_tr')] end
45 *}
47 lemma card_unit [simp]: "CARD(unit) = 1"
48   unfolding UNIV_unit by simp
50 lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a) * CARD('b)"
51   unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
53 lemma card_UNIV_sum: "CARD('a + 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 then CARD('a) + CARD('b) else 0)"
54 unfolding UNIV_Plus_UNIV[symmetric]
55 by(auto simp add: card_eq_0_iff card_Plus simp del: UNIV_Plus_UNIV)
57 lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
58 by(simp add: card_UNIV_sum)
60 lemma card_UNIV_option: "CARD('a option) = (if CARD('a) = 0 then 0 else CARD('a) + 1)"
61 proof -
62   have "(None :: 'a option) \<notin> range Some" by clarsimp
63   thus ?thesis
64     by(simp add: UNIV_option_conv card_eq_0_iff finite_range_Some card_insert_disjoint card_image)
65 qed
67 lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
68 by(simp add: card_UNIV_option)
70 lemma card_UNIV_set: "CARD('a set) = (if CARD('a) = 0 then 0 else 2 ^ CARD('a))"
71 by(simp add: Pow_UNIV[symmetric] card_eq_0_iff card_Pow del: Pow_UNIV)
73 lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
74 by(simp add: card_UNIV_set)
76 lemma card_nat [simp]: "CARD(nat) = 0"
77   by (simp add: card_eq_0_iff)
79 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)"
80 proof -
81   {  assume "0 < CARD('a)" and "0 < CARD('b)"
82     hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
83       by(simp_all only: card_ge_0_finite)
84     from finite_distinct_list[OF finb] obtain bs
85       where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
86     from finite_distinct_list[OF fina] obtain as
87       where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
88     have cb: "CARD('b) = length bs"
89       unfolding bs[symmetric] distinct_card[OF distb] ..
90     have ca: "CARD('a) = length as"
91       unfolding as[symmetric] distinct_card[OF dista] ..
92     let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (Enum.n_lists (length as) bs)"
93     have "UNIV = set ?xs"
94     proof(rule UNIV_eq_I)
95       fix f :: "'a \<Rightarrow> 'b"
96       from as have "f = the \<circ> map_of (zip as (map f as))"
97         by(auto simp add: map_of_zip_map)
98       thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
99     qed
100     moreover have "distinct ?xs" unfolding distinct_map
101     proof(intro conjI distinct_n_lists distb inj_onI)
102       fix xs ys :: "'b list"
103       assume xs: "xs \<in> set (Enum.n_lists (length as) bs)"
104         and ys: "ys \<in> set (Enum.n_lists (length as) bs)"
105         and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
106       from xs ys have [simp]: "length xs = length as" "length ys = length as"
107         by(simp_all add: length_n_lists_elem)
108       have "map_of (zip as xs) = map_of (zip as ys)"
109       proof
110         fix x
111         from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y"
112           by(simp_all add: map_of_zip_is_Some[symmetric])
113         with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
114           by(auto dest: fun_cong[where x=x])
115       qed
116       with dista show "xs = ys" by(simp add: map_of_zip_inject)
117     qed
118     hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
119     moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
120     ultimately have "CARD('a \<Rightarrow> 'b) = CARD('b) ^ CARD('a)" using cb ca by simp }
121   moreover {
122     assume cb: "CARD('b) = 1"
123     then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
124     have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
125     proof(rule UNIV_eq_I)
126       fix x :: "'a \<Rightarrow> 'b"
127       { fix y
128         have "x y \<in> UNIV" ..
129         hence "x y = b" unfolding b by simp }
130       thus "x \<in> {\<lambda>x. b}" by(auto)
131     qed
132     have "CARD('a \<Rightarrow> 'b) = 1" unfolding eq by simp }
133   ultimately show ?thesis
134     by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
135 qed
137 lemma card_nibble: "CARD(nibble) = 16"
138 unfolding UNIV_nibble by simp
140 lemma card_UNIV_char: "CARD(char) = 256"
141 proof -
142   have "inj (\<lambda>(x, y). Char x y)" by(auto intro: injI)
143   thus ?thesis unfolding UNIV_char by(simp add: card_image card_nibble)
144 qed
146 lemma card_literal: "CARD(String.literal) = 0"
147 proof -
148   have "inj STR" by(auto intro: injI)
149   thus ?thesis by(simp add: type_definition.univ[OF type_definition_literal] card_image infinite_UNIV_listI)
150 qed
152 subsection {* Classes with at least 1 and 2  *}
154 text {* Class finite already captures "at least 1" *}
156 lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
157   unfolding neq0_conv [symmetric] by simp
159 lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
160   by (simp add: less_Suc_eq_le [symmetric])
162 text {* Class for cardinality "at least 2" *}
164 class card2 = finite +
165   assumes two_le_card: "2 \<le> CARD('a)"
167 lemma one_less_card: "Suc 0 < CARD('a::card2)"
168   using two_le_card [where 'a='a] by simp
170 lemma one_less_int_card: "1 < int CARD('a::card2)"
171   using one_less_card [where 'a='a] by simp
173 subsection {* A type class for computing the cardinality of types *}
175 definition is_list_UNIV :: "'a list \<Rightarrow> bool"
176 where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"
178 lemmas [code_unfold] = is_list_UNIV_def[abs_def]
180 lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
181 by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric]
182    dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
184 class card_UNIV =
185   fixes card_UNIV :: "'a itself \<Rightarrow> nat"
186   assumes card_UNIV: "card_UNIV x = CARD('a)"
188 lemma card_UNIV_code [code_unfold]: "CARD('a :: card_UNIV) = card_UNIV TYPE('a)"
189 by(simp add: card_UNIV)
191 lemma finite_UNIV_conv_card_UNIV [code_unfold]:
192   "finite (UNIV :: 'a :: card_UNIV set) \<longleftrightarrow> card_UNIV TYPE('a) > 0"
193 by(simp add: card_UNIV card_gt_0_iff)
195 subsection {* Instantiations for @{text "card_UNIV"} *}
197 instantiation nat :: card_UNIV begin
198 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
199 instance by intro_classes (simp add: card_UNIV_nat_def)
200 end
202 instantiation int :: card_UNIV begin
203 definition "card_UNIV = (\<lambda>a :: int itself. 0)"
204 instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int)
205 end
207 print_classes
208 instantiation list :: (type) card_UNIV begin
209 definition "card_UNIV = (\<lambda>a :: 'a list itself. 0)"
210 instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI)
211 end
213 instantiation unit :: card_UNIV begin
214 definition "card_UNIV = (\<lambda>a :: unit itself. 1)"
215 instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit)
216 end
218 instantiation bool :: card_UNIV begin
219 definition "card_UNIV = (\<lambda>a :: bool itself. 2)"
220 instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool)
221 end
223 instantiation char :: card_UNIV begin
224 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
225 instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char)
226 end
228 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
229 definition "card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
230 instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
231 end
233 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
234 definition "card_UNIV = (\<lambda>a :: ('a + 'b) itself.
235   let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
236   in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
237 instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
238 end
240 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
241 definition "card_UNIV = (\<lambda>a :: ('a \<Rightarrow> 'b) itself.
242   let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
243   in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
244 instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
245 end
247 instantiation option :: (card_UNIV) card_UNIV begin
248 definition "card_UNIV = (\<lambda>a :: 'a option itself.
249   let c = card_UNIV (TYPE('a)) in if c \<noteq> 0 then Suc c else 0)"
250 instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
251 end
253 instantiation String.literal :: card_UNIV begin
254 definition "card_UNIV = (\<lambda>a :: String.literal itself. 0)"
255 instance by intro_classes (simp add: card_UNIV_literal_def card_literal)
256 end
258 instantiation set :: (card_UNIV) card_UNIV begin
259 definition "card_UNIV = (\<lambda>a :: 'a set itself.
260   let c = card_UNIV (TYPE('a)) in if c = 0 then 0 else 2 ^ c)"
261 instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
262 end
265 lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]"
266 by(auto intro: finite_1.exhaust)
268 lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]"
269 by(auto intro: finite_2.exhaust)
271 lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^isub>1, finite_3.a\<^isub>2, finite_3.a\<^isub>3]"
272 by(auto intro: finite_3.exhaust)
274 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]"
275 by(auto intro: finite_4.exhaust)
277 lemma UNIV_finite_5:
278   "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]"
279 by(auto intro: finite_5.exhaust)
281 instantiation Enum.finite_1 :: card_UNIV begin
282 definition "card_UNIV = (\<lambda>a :: Enum.finite_1 itself. 1)"
283 instance by intro_classes (simp add: UNIV_finite_1 card_UNIV_finite_1_def)
284 end
286 instantiation Enum.finite_2 :: card_UNIV begin
287 definition "card_UNIV = (\<lambda>a :: Enum.finite_2 itself. 2)"
288 instance by intro_classes (simp add: UNIV_finite_2 card_UNIV_finite_2_def)
289 end
291 instantiation Enum.finite_3 :: card_UNIV begin
292 definition "card_UNIV = (\<lambda>a :: Enum.finite_3 itself. 3)"
293 instance by intro_classes (simp add: UNIV_finite_3 card_UNIV_finite_3_def)
294 end
296 instantiation Enum.finite_4 :: card_UNIV begin
297 definition "card_UNIV = (\<lambda>a :: Enum.finite_4 itself. 4)"
298 instance by intro_classes (simp add: UNIV_finite_4 card_UNIV_finite_4_def)
299 end
301 instantiation Enum.finite_5 :: card_UNIV begin
302 definition "card_UNIV = (\<lambda>a :: Enum.finite_5 itself. 5)"
303 instance by intro_classes (simp add: UNIV_finite_5 card_UNIV_finite_5_def)
304 end
306 subsection {* Code setup for equality on sets *}
308 definition eq_set :: "'a :: card_UNIV set \<Rightarrow> 'a :: card_UNIV set \<Rightarrow> bool"
309 where [simp, code del]: "eq_set = op ="
311 lemmas [code_unfold] = eq_set_def[symmetric]
313 lemma card_Compl:
314   "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
315 by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
317 lemma eq_set_code [code]:
318   fixes xs ys :: "'a :: card_UNIV list"
319   defines "rhs \<equiv>
320   let n = CARD('a)
321   in if n = 0 then False else
322         let xs' = remdups xs; ys' = remdups ys
323         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')"
324   shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
325   and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
326   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)
327   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)
328 proof -
329   show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
330   proof
331     assume ?lhs thus ?rhs
332       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)
333   next
334     assume ?rhs
335     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
336     ultimately show ?lhs
337       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)
338   qed
339   thus ?thesis2 unfolding eq_set_def by blast
340   show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
341 qed
343 (* test code setup *)
344 value [code] "List.coset [True] = set [False] \<and> set [] = List.coset [True, False]"
346 end