src/HOL/Library/Cardinality.thy
author Andreas Lochbihler
Thu May 31 16:58:38 2012 +0200 (2012-05-31)
changeset 48051 53a0df441e20
parent 47221 7205eb4a0a05
child 48052 b74766e4c11e
permissions -rw-r--r--
unify Card_Univ and Cardinality
     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 
    31 subsection {* Cardinalities of types *}
    32 
    33 syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
    34 
    35 translations "CARD('t)" => "CONST card (CONST UNIV \<Colon> 't set)"
    36 
    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 *}
    43 
    44 lemma card_unit [simp]: "CARD(unit) = 1"
    45   unfolding UNIV_unit by simp
    46 
    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)
    49 
    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)
    52 
    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
    59 
    60 lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
    61   unfolding Pow_UNIV [symmetric]
    62   by (simp only: card_Pow finite)
    63 
    64 lemma card_nat [simp]: "CARD(nat) = 0"
    65   by (simp add: card_eq_0_iff)
    66 
    67 
    68 subsection {* Classes with at least 1 and 2  *}
    69 
    70 text {* Class finite already captures "at least 1" *}
    71 
    72 lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
    73   unfolding neq0_conv [symmetric] by simp
    74 
    75 lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
    76   by (simp add: less_Suc_eq_le [symmetric])
    77 
    78 text {* Class for cardinality "at least 2" *}
    79 
    80 class card2 = finite + 
    81   assumes two_le_card: "2 \<le> CARD('a)"
    82 
    83 lemma one_less_card: "Suc 0 < CARD('a::card2)"
    84   using two_le_card [where 'a='a] by simp
    85 
    86 lemma one_less_int_card: "1 < int CARD('a::card2)"
    87   using one_less_card [where 'a='a] by simp
    88 
    89 subsection {* A type class for computing the cardinality of types *}
    90 
    91 class card_UNIV = 
    92   fixes card_UNIV :: "'a itself \<Rightarrow> nat"
    93   assumes card_UNIV: "card_UNIV x = card (UNIV :: 'a set)"
    94 begin
    95 
    96 lemma card_UNIV_neq_0_finite_UNIV:
    97   "card_UNIV x \<noteq> 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
    98 by(simp add: card_UNIV card_eq_0_iff)
    99 
   100 lemma card_UNIV_ge_0_finite_UNIV:
   101   "card_UNIV x > 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
   102 by(auto simp add: card_UNIV intro: card_ge_0_finite finite_UNIV_card_ge_0)
   103 
   104 lemma card_UNIV_eq_0_infinite_UNIV:
   105   "card_UNIV x = 0 \<longleftrightarrow> \<not> finite (UNIV :: 'a set)"
   106 by(simp add: card_UNIV card_eq_0_iff)
   107 
   108 definition is_list_UNIV :: "'a list \<Rightarrow> bool"
   109 where "is_list_UNIV xs = (let c = card_UNIV (TYPE('a)) in if c = 0 then False else size (remdups xs) = c)"
   110 
   111 lemma is_list_UNIV_iff: fixes xs :: "'a list"
   112   shows "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
   113 proof
   114   assume "is_list_UNIV xs"
   115   hence c: "card_UNIV (TYPE('a)) > 0" and xs: "size (remdups xs) = card_UNIV (TYPE('a))"
   116     unfolding is_list_UNIV_def by(simp_all add: Let_def split: split_if_asm)
   117   from c have fin: "finite (UNIV :: 'a set)" by(auto simp add: card_UNIV_ge_0_finite_UNIV)
   118   have "card (set (remdups xs)) = size (remdups xs)" by(subst distinct_card) auto
   119   also note set_remdups
   120   finally show "set xs = UNIV" using fin unfolding xs card_UNIV by-(rule card_eq_UNIV_imp_eq_UNIV)
   121 next
   122   assume xs: "set xs = UNIV"
   123   from finite_set[of xs] have fin: "finite (UNIV :: 'a set)" unfolding xs .
   124   hence "card_UNIV (TYPE ('a)) \<noteq> 0" unfolding card_UNIV_neq_0_finite_UNIV .
   125   moreover have "size (remdups xs) = card (set (remdups xs))"
   126     by(subst distinct_card) auto
   127   ultimately show "is_list_UNIV xs" using xs by(simp add: is_list_UNIV_def Let_def card_UNIV)
   128 qed
   129 
   130 lemma card_UNIV_eq_0_is_list_UNIV_False:
   131   assumes cU0: "card_UNIV x = 0"
   132   shows "is_list_UNIV = (\<lambda>xs. False)"
   133 proof(rule ext)
   134   fix xs :: "'a list"
   135   from cU0 have "\<not> finite (UNIV :: 'a set)"
   136     by(auto simp only: card_UNIV_eq_0_infinite_UNIV)
   137   moreover have "finite (set xs)" by(rule finite_set)
   138   ultimately have "(UNIV :: 'a set) \<noteq> set xs" by(auto simp del: finite_set)
   139   thus "is_list_UNIV xs = False" unfolding is_list_UNIV_iff by simp
   140 qed
   141 
   142 end
   143 
   144 subsection {* Instantiations for @{text "card_UNIV"} *}
   145 
   146 subsubsection {* @{typ "nat"} *}
   147 
   148 instantiation nat :: card_UNIV begin
   149 
   150 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
   151 
   152 instance proof
   153   fix x :: "nat itself"
   154   show "card_UNIV x = card (UNIV :: nat set)"
   155     unfolding card_UNIV_nat_def by simp
   156 qed
   157 
   158 end
   159 
   160 subsubsection {* @{typ "int"} *}
   161 
   162 instantiation int :: card_UNIV begin
   163 
   164 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: int itself. 0)"
   165 
   166 instance proof
   167   fix x :: "int itself"
   168   show "card_UNIV x = card (UNIV :: int set)"
   169     unfolding card_UNIV_int_def by(simp add: infinite_UNIV_int)
   170 qed
   171 
   172 end
   173 
   174 subsubsection {* @{typ "'a list"} *}
   175 
   176 instantiation list :: (type) card_UNIV begin
   177 
   178 definition "card_UNIV_class.card_UNIV = (\<lambda>a :: 'a list itself. 0)"
   179 
   180 instance proof
   181   fix x :: "'a list itself"
   182   show "card_UNIV x = card (UNIV :: 'a list set)"
   183     unfolding card_UNIV_list_def by(simp add: infinite_UNIV_listI)
   184 qed
   185 
   186 end
   187 
   188 subsubsection {* @{typ "unit"} *}
   189 
   190 lemma card_UNIV_unit: "card (UNIV :: unit set) = 1"
   191   unfolding UNIV_unit by simp
   192 
   193 instantiation unit :: card_UNIV begin
   194 
   195 definition card_UNIV_unit_def: 
   196   "card_UNIV_class.card_UNIV = (\<lambda>a :: unit itself. 1)"
   197 
   198 instance proof
   199   fix x :: "unit itself"
   200   show "card_UNIV x = card (UNIV :: unit set)"
   201     by(simp add: card_UNIV_unit_def card_UNIV_unit)
   202 qed
   203 
   204 end
   205 
   206 subsubsection {* @{typ "bool"} *}
   207 
   208 lemma card_UNIV_bool: "card (UNIV :: bool set) = 2"
   209   unfolding UNIV_bool by simp
   210 
   211 instantiation bool :: card_UNIV begin
   212 
   213 definition card_UNIV_bool_def: 
   214   "card_UNIV_class.card_UNIV = (\<lambda>a :: bool itself. 2)"
   215 
   216 instance proof
   217   fix x :: "bool itself"
   218   show "card_UNIV x = card (UNIV :: bool set)"
   219     by(simp add: card_UNIV_bool_def card_UNIV_bool)
   220 qed
   221 
   222 end
   223 
   224 subsubsection {* @{typ "char"} *}
   225 
   226 lemma card_UNIV_char: "card (UNIV :: char set) = 256"
   227 proof -
   228   from enum_distinct
   229   have "card (set (Enum.enum :: char list)) = length (Enum.enum :: char list)"
   230     by (rule distinct_card)
   231   also have "set Enum.enum = (UNIV :: char set)" by (auto intro: in_enum)
   232   also note enum_chars
   233   finally show ?thesis by (simp add: chars_def)
   234 qed
   235 
   236 instantiation char :: card_UNIV begin
   237 
   238 definition card_UNIV_char_def: 
   239   "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
   240 
   241 instance proof
   242   fix x :: "char itself"
   243   show "card_UNIV x = card (UNIV :: char set)"
   244     by(simp add: card_UNIV_char_def card_UNIV_char)
   245 qed
   246 
   247 end
   248 
   249 subsubsection {* @{typ "'a \<times> 'b"} *}
   250 
   251 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
   252 
   253 definition card_UNIV_product_def: 
   254   "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
   255 
   256 instance proof
   257   fix x :: "('a \<times> 'b) itself"
   258   show "card_UNIV x = card (UNIV :: ('a \<times> 'b) set)"
   259     by(simp add: card_UNIV_product_def card_UNIV UNIV_Times_UNIV[symmetric] card_cartesian_product del: UNIV_Times_UNIV)
   260 qed
   261 
   262 end
   263 
   264 subsubsection {* @{typ "'a + 'b"} *}
   265 
   266 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
   267 
   268 definition card_UNIV_sum_def: 
   269   "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a + 'b) itself. let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   270                            in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
   271 
   272 instance proof
   273   fix x :: "('a + 'b) itself"
   274   show "card_UNIV x = card (UNIV :: ('a + 'b) set)"
   275     by (auto simp add: card_UNIV_sum_def card_UNIV card_eq_0_iff UNIV_Plus_UNIV[symmetric] finite_Plus_iff Let_def card_Plus simp del: UNIV_Plus_UNIV dest!: card_ge_0_finite)
   276 qed
   277 
   278 end
   279 
   280 subsubsection {* @{typ "'a \<Rightarrow> 'b"} *}
   281 
   282 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
   283 
   284 definition card_UNIV_fun_def: 
   285   "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a \<Rightarrow> 'b) itself. let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
   286                            in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
   287 
   288 instance proof
   289   fix x :: "('a \<Rightarrow> 'b) itself"
   290 
   291   { assume "0 < card (UNIV :: 'a set)"
   292     and "0 < card (UNIV :: 'b set)"
   293     hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
   294       by(simp_all only: card_ge_0_finite)
   295     from finite_distinct_list[OF finb] obtain bs 
   296       where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
   297     from finite_distinct_list[OF fina] obtain as
   298       where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
   299     have cb: "card (UNIV :: 'b set) = length bs"
   300       unfolding bs[symmetric] distinct_card[OF distb] ..
   301     have ca: "card (UNIV :: 'a set) = length as"
   302       unfolding as[symmetric] distinct_card[OF dista] ..
   303     let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (Enum.n_lists (length as) bs)"
   304     have "UNIV = set ?xs"
   305     proof(rule UNIV_eq_I)
   306       fix f :: "'a \<Rightarrow> 'b"
   307       from as have "f = the \<circ> map_of (zip as (map f as))"
   308         by(auto simp add: map_of_zip_map intro: ext)
   309       thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
   310     qed
   311     moreover have "distinct ?xs" unfolding distinct_map
   312     proof(intro conjI distinct_n_lists distb inj_onI)
   313       fix xs ys :: "'b list"
   314       assume xs: "xs \<in> set (Enum.n_lists (length as) bs)"
   315         and ys: "ys \<in> set (Enum.n_lists (length as) bs)"
   316         and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
   317       from xs ys have [simp]: "length xs = length as" "length ys = length as"
   318         by(simp_all add: length_n_lists_elem)
   319       have "map_of (zip as xs) = map_of (zip as ys)"
   320       proof
   321         fix x
   322         from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y"
   323           by(simp_all add: map_of_zip_is_Some[symmetric])
   324         with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
   325           by(auto dest: fun_cong[where x=x])
   326       qed
   327       with dista show "xs = ys" by(simp add: map_of_zip_inject)
   328     qed
   329     hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
   330     moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
   331     ultimately have "card (UNIV :: ('a \<Rightarrow> 'b) set) = card (UNIV :: 'b set) ^ card (UNIV :: 'a set)"
   332       using cb ca by simp }
   333   moreover {
   334     assume cb: "card (UNIV :: 'b set) = Suc 0"
   335     then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
   336     have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
   337     proof(rule UNIV_eq_I)
   338       fix x :: "'a \<Rightarrow> 'b"
   339       { fix y
   340         have "x y \<in> UNIV" ..
   341         hence "x y = b" unfolding b by simp }
   342       thus "x \<in> {\<lambda>x. b}" by(auto intro: ext)
   343     qed
   344     have "card (UNIV :: ('a \<Rightarrow> 'b) set) = Suc 0" unfolding eq by simp }
   345   ultimately show "card_UNIV x = card (UNIV :: ('a \<Rightarrow> 'b) set)"
   346     unfolding card_UNIV_fun_def card_UNIV Let_def
   347     by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
   348 qed
   349 
   350 end
   351 
   352 subsubsection {* @{typ "'a option"} *}
   353 
   354 instantiation option :: (card_UNIV) card_UNIV
   355 begin
   356 
   357 definition card_UNIV_option_def: 
   358   "card_UNIV_class.card_UNIV = (\<lambda>a :: 'a option itself. let c = card_UNIV (TYPE('a))
   359                            in if c \<noteq> 0 then Suc c else 0)"
   360 
   361 instance proof
   362   fix x :: "'a option itself"
   363   show "card_UNIV x = card (UNIV :: 'a option set)"
   364     unfolding UNIV_option_conv
   365     by(auto simp add: card_UNIV_option_def card_UNIV card_eq_0_iff Let_def intro: inj_Some dest: finite_imageD)
   366       (subst card_insert_disjoint, auto simp add: card_eq_0_iff card_image inj_Some intro: finite_imageI card_ge_0_finite)
   367 qed
   368 
   369 end
   370 
   371 subsection {* Code setup for equality on sets *}
   372 
   373 definition eq_set :: "'a :: card_UNIV set \<Rightarrow> 'a :: card_UNIV set \<Rightarrow> bool"
   374 where [simp, code del]: "eq_set = op ="
   375 
   376 lemmas [code_unfold] = eq_set_def[symmetric]
   377 
   378 lemma card_Compl:
   379   "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
   380 by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
   381 
   382 lemma eq_set_code [code]:
   383   fixes xs ys :: "'a :: card_UNIV list"
   384   defines "rhs \<equiv> 
   385   let n = card_UNIV TYPE('a)
   386   in if n = 0 then False else 
   387         let xs' = remdups xs; ys' = remdups ys 
   388         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')"
   389   shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
   390   and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
   391   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)
   392   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)
   393 proof -
   394   show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
   395   proof
   396     assume ?lhs thus ?rhs
   397       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)
   398   next
   399     assume ?rhs
   400     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
   401     ultimately show ?lhs
   402       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)
   403   qed
   404   thus ?thesis2 unfolding eq_set_def by blast
   405   show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
   406 qed
   407 
   408 (* test code setup *)
   409 value [code] "List.coset [True] = set [False] \<and> set [] = List.coset [True, False]"
   410 
   411 end