src/HOL/Library/Cardinality.thy
author huffman
Sat Jun 02 08:32:42 2012 +0200 (2012-06-02)
changeset 48067 9f458b3efb5c
parent 48063 f02b4302d5dd
parent 48062 9014e78ccde2
child 48070 02d64fd40852
permissions -rw-r--r--
merged
     1 (*  Title:      HOL/Library/Cardinality.thy
     2     Author:     Brian Huffman, Andreas Lochbihler
     3 *)
     4 
     5 header {* Cardinality of types *}
     6 
     7 theory Cardinality
     8 imports "~~/src/HOL/Main"
     9 begin
    10 
    11 subsection {* Preliminary lemmas *}
    12 (* These should be moved elsewhere *)
    13 
    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
    26 
    27 lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
    28   by (simp add: univ card_image inj_on_def Abs_inject)
    29 
    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)
    32 
    33 
    34 subsection {* Cardinalities of types *}
    35 
    36 syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
    37 
    38 translations "CARD('t)" => "CONST card (CONST UNIV \<Colon> 't set)"
    39 
    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 *}
    46 
    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)
    49 
    50 lemma card_UNIV_sum: "CARD('a + 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 then CARD('a) + CARD('b) else 0)"
    51 unfolding UNIV_Plus_UNIV[symmetric]
    52 by(auto simp add: card_eq_0_iff card_Plus simp del: UNIV_Plus_UNIV)
    53 
    54 lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
    55 by(simp add: card_UNIV_sum)
    56 
    57 lemma card_UNIV_option: "CARD('a option) = (if CARD('a) = 0 then 0 else CARD('a) + 1)"
    58 proof -
    59   have "(None :: 'a option) \<notin> range Some" by clarsimp
    60   thus ?thesis
    61     by(simp add: UNIV_option_conv card_eq_0_iff finite_range_Some card_insert_disjoint card_image)
    62 qed
    63 
    64 lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
    65 by(simp add: card_UNIV_option)
    66 
    67 lemma card_UNIV_set: "CARD('a set) = (if CARD('a) = 0 then 0 else 2 ^ CARD('a))"
    68 by(simp add: Pow_UNIV[symmetric] card_eq_0_iff card_Pow del: Pow_UNIV)
    69 
    70 lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
    71 by(simp add: card_UNIV_set)
    72 
    73 lemma card_nat [simp]: "CARD(nat) = 0"
    74   by (simp add: card_eq_0_iff)
    75 
    76 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)"
    77 proof -
    78   {  assume "0 < CARD('a)" and "0 < CARD('b)"
    79     hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
    80       by(simp_all only: card_ge_0_finite)
    81     from finite_distinct_list[OF finb] obtain bs 
    82       where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
    83     from finite_distinct_list[OF fina] obtain as
    84       where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
    85     have cb: "CARD('b) = length bs"
    86       unfolding bs[symmetric] distinct_card[OF distb] ..
    87     have ca: "CARD('a) = length as"
    88       unfolding as[symmetric] distinct_card[OF dista] ..
    89     let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (Enum.n_lists (length as) bs)"
    90     have "UNIV = set ?xs"
    91     proof(rule UNIV_eq_I)
    92       fix f :: "'a \<Rightarrow> 'b"
    93       from as have "f = the \<circ> map_of (zip as (map f as))"
    94         by(auto simp add: map_of_zip_map)
    95       thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
    96     qed
    97     moreover have "distinct ?xs" unfolding distinct_map
    98     proof(intro conjI distinct_n_lists distb inj_onI)
    99       fix xs ys :: "'b list"
   100       assume xs: "xs \<in> set (Enum.n_lists (length as) bs)"
   101         and ys: "ys \<in> set (Enum.n_lists (length as) bs)"
   102         and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
   103       from xs ys have [simp]: "length xs = length as" "length ys = length as"
   104         by(simp_all add: length_n_lists_elem)
   105       have "map_of (zip as xs) = map_of (zip as ys)"
   106       proof
   107         fix x
   108         from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y"
   109           by(simp_all add: map_of_zip_is_Some[symmetric])
   110         with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
   111           by(auto dest: fun_cong[where x=x])
   112       qed
   113       with dista show "xs = ys" by(simp add: map_of_zip_inject)
   114     qed
   115     hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
   116     moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
   117     ultimately have "CARD('a \<Rightarrow> 'b) = CARD('b) ^ CARD('a)" using cb ca by simp }
   118   moreover {
   119     assume cb: "CARD('b) = 1"
   120     then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
   121     have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
   122     proof(rule UNIV_eq_I)
   123       fix x :: "'a \<Rightarrow> 'b"
   124       { fix y
   125         have "x y \<in> UNIV" ..
   126         hence "x y = b" unfolding b by simp }
   127       thus "x \<in> {\<lambda>x. b}" by(auto)
   128     qed
   129     have "CARD('a \<Rightarrow> 'b) = 1" unfolding eq by simp }
   130   ultimately show ?thesis
   131     by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
   132 qed
   133 
   134 lemma card_nibble: "CARD(nibble) = 16"
   135 unfolding UNIV_nibble by simp
   136 
   137 lemma card_UNIV_char: "CARD(char) = 256"
   138 proof -
   139   have "inj (\<lambda>(x, y). Char x y)" by(auto intro: injI)
   140   thus ?thesis unfolding UNIV_char by(simp add: card_image card_nibble)
   141 qed
   142 
   143 lemma card_literal: "CARD(String.literal) = 0"
   144 proof -
   145   have "inj STR" by(auto intro: injI)
   146   thus ?thesis by(simp add: type_definition.univ[OF type_definition_literal] card_image infinite_UNIV_listI)
   147 qed
   148 
   149 subsection {* Classes with at least 1 and 2  *}
   150 
   151 text {* Class finite already captures "at least 1" *}
   152 
   153 lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
   154   unfolding neq0_conv [symmetric] by simp
   155 
   156 lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
   157   by (simp add: less_Suc_eq_le [symmetric])
   158 
   159 text {* Class for cardinality "at least 2" *}
   160 
   161 class card2 = finite + 
   162   assumes two_le_card: "2 \<le> CARD('a)"
   163 
   164 lemma one_less_card: "Suc 0 < CARD('a::card2)"
   165   using two_le_card [where 'a='a] by simp
   166 
   167 lemma one_less_int_card: "1 < int CARD('a::card2)"
   168   using one_less_card [where 'a='a] by simp
   169 
   170 subsection {* A type class for computing the cardinality of types *}
   171 
   172 definition is_list_UNIV :: "'a list \<Rightarrow> bool"
   173 where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"
   174 
   175 lemmas [code_unfold] = is_list_UNIV_def[abs_def]
   176 
   177 lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
   178 by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric] 
   179    dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
   180 
   181 class card_UNIV = 
   182   fixes card_UNIV :: "'a itself \<Rightarrow> nat"
   183   assumes card_UNIV: "card_UNIV x = CARD('a)"
   184 
   185 lemma card_UNIV_code [code_unfold]: "CARD('a :: card_UNIV) = card_UNIV TYPE('a)"
   186 by(simp add: card_UNIV)
   187 
   188 lemma finite_UNIV_conv_card_UNIV [code_unfold]:
   189   "finite (UNIV :: 'a :: card_UNIV set) \<longleftrightarrow> card_UNIV TYPE('a) > 0"
   190 by(simp add: card_UNIV card_gt_0_iff)
   191 
   192 subsection {* Instantiations for @{text "card_UNIV"} *}
   193 
   194 instantiation nat :: card_UNIV begin
   195 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
   196 instance by intro_classes (simp add: card_UNIV_nat_def)
   197 end
   198 
   199 instantiation int :: card_UNIV begin
   200 definition "card_UNIV = (\<lambda>a :: int itself. 0)"
   201 instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int)
   202 end
   203 
   204 instantiation list :: (type) card_UNIV begin
   205 definition "card_UNIV = (\<lambda>a :: 'a list itself. 0)"
   206 instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI)
   207 end
   208 
   209 instantiation unit :: card_UNIV begin
   210 definition "card_UNIV = (\<lambda>a :: unit itself. 1)"
   211 instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit)
   212 end
   213 
   214 instantiation bool :: card_UNIV begin
   215 definition "card_UNIV = (\<lambda>a :: bool itself. 2)"
   216 instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool)
   217 end
   218 
   219 instantiation char :: card_UNIV begin
   220 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
   221 instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char)
   222 end
   223 
   224 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
   225 definition "card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
   226 instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
   227 end
   228 
   229 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
   230 definition "card_UNIV = (\<lambda>a :: ('a + 'b) itself. 
   231   let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   232   in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
   233 instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
   234 end
   235 
   236 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
   237 definition "card_UNIV = (\<lambda>a :: ('a \<Rightarrow> 'b) itself. 
   238   let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   239   in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
   240 instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
   241 end
   242 
   243 instantiation option :: (card_UNIV) card_UNIV begin
   244 definition "card_UNIV = (\<lambda>a :: 'a option itself. 
   245   let c = card_UNIV (TYPE('a)) in if c \<noteq> 0 then Suc c else 0)"
   246 instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
   247 end
   248 
   249 instantiation String.literal :: card_UNIV begin
   250 definition "card_UNIV = (\<lambda>a :: String.literal itself. 0)"
   251 instance by intro_classes (simp add: card_UNIV_literal_def card_literal)
   252 end
   253 
   254 instantiation set :: (card_UNIV) card_UNIV begin
   255 definition "card_UNIV = (\<lambda>a :: 'a set itself.
   256   let c = card_UNIV (TYPE('a)) in if c = 0 then 0 else 2 ^ c)"
   257 instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
   258 end
   259 
   260 
   261 lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]"
   262 by(auto intro: finite_1.exhaust)
   263 
   264 lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]"
   265 by(auto intro: finite_2.exhaust)
   266 
   267 lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^isub>1, finite_3.a\<^isub>2, finite_3.a\<^isub>3]"
   268 by(auto intro: finite_3.exhaust)
   269 
   270 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]"
   271 by(auto intro: finite_4.exhaust)
   272 
   273 lemma UNIV_finite_5:
   274   "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]"
   275 by(auto intro: finite_5.exhaust)
   276 
   277 instantiation Enum.finite_1 :: card_UNIV begin
   278 definition "card_UNIV = (\<lambda>a :: Enum.finite_1 itself. 1)"
   279 instance by intro_classes (simp add: UNIV_finite_1 card_UNIV_finite_1_def)
   280 end
   281 
   282 instantiation Enum.finite_2 :: card_UNIV begin
   283 definition "card_UNIV = (\<lambda>a :: Enum.finite_2 itself. 2)"
   284 instance by intro_classes (simp add: UNIV_finite_2 card_UNIV_finite_2_def)
   285 end
   286 
   287 instantiation Enum.finite_3 :: card_UNIV begin
   288 definition "card_UNIV = (\<lambda>a :: Enum.finite_3 itself. 3)"
   289 instance by intro_classes (simp add: UNIV_finite_3 card_UNIV_finite_3_def)
   290 end
   291 
   292 instantiation Enum.finite_4 :: card_UNIV begin
   293 definition "card_UNIV = (\<lambda>a :: Enum.finite_4 itself. 4)"
   294 instance by intro_classes (simp add: UNIV_finite_4 card_UNIV_finite_4_def)
   295 end
   296 
   297 instantiation Enum.finite_5 :: card_UNIV begin
   298 definition "card_UNIV = (\<lambda>a :: Enum.finite_5 itself. 5)"
   299 instance by intro_classes (simp add: UNIV_finite_5 card_UNIV_finite_5_def)
   300 end
   301 
   302 subsection {* Code setup for sets *}
   303 
   304 lemma card_Compl:
   305   "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
   306 by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
   307 
   308 context fixes xs :: "'a :: card_UNIV list"
   309 begin
   310 
   311 definition card' :: "'a set \<Rightarrow> nat" 
   312 where [simp, code del, code_abbrev]: "card' = card"
   313 
   314 lemma card'_code [code]:
   315   "card' (set xs) = length (remdups xs)"
   316   "card' (List.coset xs) =  card_UNIV TYPE('a) - length (remdups xs)"
   317 by(simp_all add: List.card_set card_Compl card_UNIV)
   318 
   319 lemma card'_UNIV [code_unfold]: "card' (UNIV :: 'a :: card_UNIV set) = card_UNIV TYPE('a)"
   320 by(simp add: card_UNIV)
   321 
   322 definition finite' :: "'a set \<Rightarrow> bool"
   323 where [simp, code del, code_abbrev]: "finite' = finite"
   324 
   325 lemma finite'_code [code]:
   326   "finite' (set xs) \<longleftrightarrow> True"
   327   "finite' (List.coset xs) \<longleftrightarrow> CARD('a) > 0"
   328 by(simp_all add: card_gt_0_iff)
   329 
   330 definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   331 where [simp, code del, code_abbrev]: "subset' = op \<subseteq>"
   332 
   333 lemma subset'_code [code]:
   334   "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
   335   "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
   336   "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
   337 by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card])
   338   (metis finite_compl finite_set rev_finite_subset)
   339 
   340 definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   341 where [simp, code del, code_abbrev]: "eq_set = op ="
   342 
   343 lemma eq_set_code [code]:
   344   fixes ys
   345   defines "rhs \<equiv> 
   346   let n = CARD('a)
   347   in if n = 0 then False else 
   348         let xs' = remdups xs; ys' = remdups ys 
   349         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')"
   350   shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
   351   and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
   352   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)
   353   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)
   354 proof -
   355   show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
   356   proof
   357     assume ?lhs thus ?rhs
   358       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)
   359   next
   360     assume ?rhs
   361     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
   362     ultimately show ?lhs
   363       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)
   364   qed
   365   thus ?thesis2 unfolding eq_set_def by blast
   366   show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
   367 qed
   368 
   369 end
   370 
   371 notepad begin (* test code setup *)
   372 have "List.coset [True] = set [False] \<and> List.coset [] \<subseteq> List.set [True, False] \<and> finite (List.coset [True])"
   373   by eval
   374 end
   375 
   376 hide_const (open) card' finite' subset' eq_set
   377 
   378 end