src/HOL/Library/Cardinality.thy
author Andreas Lochbihler
Fri Jun 01 20:40:34 2012 +0200 (2012-06-01)
changeset 48062 9014e78ccde2
parent 48060 1f4d00a7f59f
child 48067 9f458b3efb5c
permissions -rw-r--r--
improved code setup for card, finite, subset
     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_unit [simp]: "CARD(unit) = 1"
    48   unfolding UNIV_unit by simp
    49 
    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)
    52 
    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)
    56 
    57 lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
    58 by(simp add: card_UNIV_sum)
    59 
    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
    66 
    67 lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
    68 by(simp add: card_UNIV_option)
    69 
    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)
    72 
    73 lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
    74 by(simp add: card_UNIV_set)
    75 
    76 lemma card_nat [simp]: "CARD(nat) = 0"
    77   by (simp add: card_eq_0_iff)
    78 
    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
   136 
   137 lemma card_nibble: "CARD(nibble) = 16"
   138 unfolding UNIV_nibble by simp
   139 
   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
   145 
   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
   151 
   152 subsection {* Classes with at least 1 and 2  *}
   153 
   154 text {* Class finite already captures "at least 1" *}
   155 
   156 lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
   157   unfolding neq0_conv [symmetric] by simp
   158 
   159 lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
   160   by (simp add: less_Suc_eq_le [symmetric])
   161 
   162 text {* Class for cardinality "at least 2" *}
   163 
   164 class card2 = finite + 
   165   assumes two_le_card: "2 \<le> CARD('a)"
   166 
   167 lemma one_less_card: "Suc 0 < CARD('a::card2)"
   168   using two_le_card [where 'a='a] by simp
   169 
   170 lemma one_less_int_card: "1 < int CARD('a::card2)"
   171   using one_less_card [where 'a='a] by simp
   172 
   173 subsection {* A type class for computing the cardinality of types *}
   174 
   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)"
   177 
   178 lemmas [code_unfold] = is_list_UNIV_def[abs_def]
   179 
   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)
   183 
   184 class card_UNIV = 
   185   fixes card_UNIV :: "'a itself \<Rightarrow> nat"
   186   assumes card_UNIV: "card_UNIV x = CARD('a)"
   187 
   188 lemma card_UNIV_code [code_unfold]: "CARD('a :: card_UNIV) = card_UNIV TYPE('a)"
   189 by(simp add: card_UNIV)
   190 
   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)
   194 
   195 subsection {* Instantiations for @{text "card_UNIV"} *}
   196 
   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
   201 
   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
   206 
   207 instantiation list :: (type) card_UNIV begin
   208 definition "card_UNIV = (\<lambda>a :: 'a list itself. 0)"
   209 instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI)
   210 end
   211 
   212 instantiation unit :: card_UNIV begin
   213 definition "card_UNIV = (\<lambda>a :: unit itself. 1)"
   214 instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit)
   215 end
   216 
   217 instantiation bool :: card_UNIV begin
   218 definition "card_UNIV = (\<lambda>a :: bool itself. 2)"
   219 instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool)
   220 end
   221 
   222 instantiation char :: card_UNIV begin
   223 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
   224 instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char)
   225 end
   226 
   227 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
   228 definition "card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
   229 instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
   230 end
   231 
   232 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
   233 definition "card_UNIV = (\<lambda>a :: ('a + 'b) itself. 
   234   let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   235   in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
   236 instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
   237 end
   238 
   239 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
   240 definition "card_UNIV = (\<lambda>a :: ('a \<Rightarrow> 'b) itself. 
   241   let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   242   in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
   243 instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
   244 end
   245 
   246 instantiation option :: (card_UNIV) card_UNIV begin
   247 definition "card_UNIV = (\<lambda>a :: 'a option itself. 
   248   let c = card_UNIV (TYPE('a)) in if c \<noteq> 0 then Suc c else 0)"
   249 instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
   250 end
   251 
   252 instantiation String.literal :: card_UNIV begin
   253 definition "card_UNIV = (\<lambda>a :: String.literal itself. 0)"
   254 instance by intro_classes (simp add: card_UNIV_literal_def card_literal)
   255 end
   256 
   257 instantiation set :: (card_UNIV) card_UNIV begin
   258 definition "card_UNIV = (\<lambda>a :: 'a set itself.
   259   let c = card_UNIV (TYPE('a)) in if c = 0 then 0 else 2 ^ c)"
   260 instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
   261 end
   262 
   263 
   264 lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]"
   265 by(auto intro: finite_1.exhaust)
   266 
   267 lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]"
   268 by(auto intro: finite_2.exhaust)
   269 
   270 lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^isub>1, finite_3.a\<^isub>2, finite_3.a\<^isub>3]"
   271 by(auto intro: finite_3.exhaust)
   272 
   273 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]"
   274 by(auto intro: finite_4.exhaust)
   275 
   276 lemma UNIV_finite_5:
   277   "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]"
   278 by(auto intro: finite_5.exhaust)
   279 
   280 instantiation Enum.finite_1 :: card_UNIV begin
   281 definition "card_UNIV = (\<lambda>a :: Enum.finite_1 itself. 1)"
   282 instance by intro_classes (simp add: UNIV_finite_1 card_UNIV_finite_1_def)
   283 end
   284 
   285 instantiation Enum.finite_2 :: card_UNIV begin
   286 definition "card_UNIV = (\<lambda>a :: Enum.finite_2 itself. 2)"
   287 instance by intro_classes (simp add: UNIV_finite_2 card_UNIV_finite_2_def)
   288 end
   289 
   290 instantiation Enum.finite_3 :: card_UNIV begin
   291 definition "card_UNIV = (\<lambda>a :: Enum.finite_3 itself. 3)"
   292 instance by intro_classes (simp add: UNIV_finite_3 card_UNIV_finite_3_def)
   293 end
   294 
   295 instantiation Enum.finite_4 :: card_UNIV begin
   296 definition "card_UNIV = (\<lambda>a :: Enum.finite_4 itself. 4)"
   297 instance by intro_classes (simp add: UNIV_finite_4 card_UNIV_finite_4_def)
   298 end
   299 
   300 instantiation Enum.finite_5 :: card_UNIV begin
   301 definition "card_UNIV = (\<lambda>a :: Enum.finite_5 itself. 5)"
   302 instance by intro_classes (simp add: UNIV_finite_5 card_UNIV_finite_5_def)
   303 end
   304 
   305 subsection {* Code setup for sets *}
   306 
   307 lemma card_Compl:
   308   "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
   309 by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
   310 
   311 context fixes xs :: "'a :: card_UNIV list"
   312 begin
   313 
   314 definition card' :: "'a set \<Rightarrow> nat" 
   315 where [simp, code del, code_abbrev]: "card' = card"
   316 
   317 lemma card'_code [code]:
   318   "card' (set xs) = length (remdups xs)"
   319   "card' (List.coset xs) =  card_UNIV TYPE('a) - length (remdups xs)"
   320 by(simp_all add: List.card_set card_Compl card_UNIV)
   321 
   322 lemma card'_UNIV [code_unfold]: "card' (UNIV :: 'a :: card_UNIV set) = card_UNIV TYPE('a)"
   323 by(simp add: card_UNIV)
   324 
   325 definition finite' :: "'a set \<Rightarrow> bool"
   326 where [simp, code del, code_abbrev]: "finite' = finite"
   327 
   328 lemma finite'_code [code]:
   329   "finite' (set xs) \<longleftrightarrow> True"
   330   "finite' (List.coset xs) \<longleftrightarrow> CARD('a) > 0"
   331 by(simp_all add: card_gt_0_iff)
   332 
   333 definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   334 where [simp, code del, code_abbrev]: "subset' = op \<subseteq>"
   335 
   336 lemma subset'_code [code]:
   337   "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
   338   "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
   339   "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
   340 by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card])
   341   (metis finite_compl finite_set rev_finite_subset)
   342 
   343 definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   344 where [simp, code del, code_abbrev]: "eq_set = op ="
   345 
   346 lemma eq_set_code [code]:
   347   fixes ys
   348   defines "rhs \<equiv> 
   349   let n = CARD('a)
   350   in if n = 0 then False else 
   351         let xs' = remdups xs; ys' = remdups ys 
   352         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')"
   353   shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
   354   and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
   355   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)
   356   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)
   357 proof -
   358   show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
   359   proof
   360     assume ?lhs thus ?rhs
   361       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)
   362   next
   363     assume ?rhs
   364     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
   365     ultimately show ?lhs
   366       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)
   367   qed
   368   thus ?thesis2 unfolding eq_set_def by blast
   369   show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
   370 qed
   371 
   372 end
   373 
   374 notepad begin (* test code setup *)
   375 have "List.coset [True] = set [False] \<and> List.coset [] \<subseteq> List.set [True, False] \<and> finite (List.coset [True])"
   376   by eval
   377 end
   378 
   379 hide_const (open) card' finite' subset' eq_set
   380 
   381 end