src/HOL/Library/Cardinality.thy
 author wenzelm Wed Apr 06 12:58:13 2011 +0200 (2011-04-06) changeset 42245 29e3967550d5 parent 37653 847e95ca9b0a child 42247 12fe41a92cd5 permissions -rw-r--r--
moved unparse material to syntax_phases.ML;
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 {*
38 let
39   fun card_univ_tr' show_sorts _ [Const (@{const_syntax UNIV}, Type(_, [T, _]))] =
40     Syntax.const @{syntax_const "_type_card"} \$ Syntax_Phases.term_of_typ show_sorts T;
41 in [(@{const_syntax card}, card_univ_tr')]
42 end
43 *}
45 lemma card_unit [simp]: "CARD(unit) = 1"
46   unfolding UNIV_unit by simp
48 lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a::finite) * CARD('b::finite)"
49   unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
51 lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
52   unfolding UNIV_Plus_UNIV [symmetric] by (simp only: finite card_Plus)
54 lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
55   unfolding UNIV_option_conv
56   apply (subgoal_tac "(None::'a option) \<notin> range Some")
57   apply (simp add: card_image)
58   apply fast
59   done
61 lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
62   unfolding Pow_UNIV [symmetric]
63   by (simp only: card_Pow finite numeral_2_eq_2)
65 lemma card_nat [simp]: "CARD(nat) = 0"
66   by (simp add: infinite_UNIV_nat card_eq_0_iff)
69 subsection {* Classes with at least 1 and 2  *}
71 text {* Class finite already captures "at least 1" *}
73 lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
74   unfolding neq0_conv [symmetric] by simp
76 lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
77   by (simp add: less_Suc_eq_le [symmetric])
79 text {* Class for cardinality "at least 2" *}
81 class card2 = finite +
82   assumes two_le_card: "2 \<le> CARD('a)"
84 lemma one_less_card: "Suc 0 < CARD('a::card2)"
85   using two_le_card [where 'a='a] by simp
87 lemma one_less_int_card: "1 < int CARD('a::card2)"
88   using one_less_card [where 'a='a] by simp
90 end