src/HOL/Library/Cardinality.thy
 author huffman Wed Aug 10 18:02:16 2011 -0700 (2011-08-10) changeset 44142 8e27e0177518 parent 42247 12fe41a92cd5 child 47108 2a1953f0d20d permissions -rw-r--r--
avoid warnings about duplicate rules
1 (*  Title:      HOL/Library/Cardinality.thy
2     Author:     Brian Huffman
3 *)
5 header {* Cardinality of types *}
7 theory Cardinality
8 imports 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::finite) * CARD('b::finite)"
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 numeral_2_eq_2)
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 end