src/HOL/Library/Cardinality.thy
author Andreas Lochbihler
Thu Jun 28 09:16:00 2012 +0200 (2012-06-28)
changeset 48164 e97369f20c30
parent 48070 02d64fd40852
child 48165 d07a0b9601aa
permissions -rw-r--r--
change card_UNIV from itself to phantom type to avoid unnecessary closures in generated code
     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 Phantom_Type
     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 [code del]: "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"
   174 
   175 lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
   176 by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric] 
   177    dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
   178 
   179 type_synonym 'a card_UNIV = "('a, nat) phantom"
   180 
   181 class card_UNIV = 
   182   fixes card_UNIV :: "'a card_UNIV"
   183   assumes card_UNIV: "card_UNIV = Phantom('a) CARD('a)"
   184 
   185 lemma card_UNIV_code [code_unfold]: 
   186   "CARD('a :: card_UNIV) = of_phantom (card_UNIV :: 'a card_UNIV)"
   187 by(simp add: card_UNIV)
   188 
   189 lemma is_list_UNIV_code [code]:
   190   "is_list_UNIV (xs :: 'a list) = 
   191   (let c = CARD('a :: card_UNIV) in if c = 0 then False else size (remdups xs) = c)"
   192 by(rule is_list_UNIV_def)
   193 
   194 lemma finite_UNIV_conv_card_UNIV [code_unfold]:
   195   "finite (UNIV :: 'a :: card_UNIV set) \<longleftrightarrow> 
   196   of_phantom (card_UNIV :: 'a card_UNIV) > 0"
   197 by(simp add: card_UNIV card_gt_0_iff)
   198 
   199 subsection {* Instantiations for @{text "card_UNIV"} *}
   200 
   201 instantiation nat :: card_UNIV begin
   202 definition "card_UNIV = Phantom(nat) 0"
   203 instance by intro_classes (simp add: card_UNIV_nat_def)
   204 end
   205 
   206 instantiation int :: card_UNIV begin
   207 definition "card_UNIV = Phantom(int) 0"
   208 instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int)
   209 end
   210 
   211 instantiation list :: (type) card_UNIV begin
   212 definition "card_UNIV = Phantom('a list) 0"
   213 instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI)
   214 end
   215 
   216 instantiation unit :: card_UNIV begin
   217 definition "card_UNIV = Phantom(unit) 1"
   218 instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit)
   219 end
   220 
   221 instantiation bool :: card_UNIV begin
   222 definition "card_UNIV = Phantom(bool) 2"
   223 instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool)
   224 end
   225 
   226 instantiation char :: card_UNIV begin
   227 definition "card_UNIV = Phantom(char) 256"
   228 instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char)
   229 end
   230 
   231 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
   232 definition "card_UNIV = 
   233   Phantom('a \<times> 'b) (of_phantom (card_UNIV :: 'a card_UNIV) * of_phantom (card_UNIV :: 'b card_UNIV))"
   234 instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
   235 end
   236 
   237 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
   238 definition "card_UNIV = Phantom('a + 'b)
   239   (let ca = of_phantom (card_UNIV :: 'a card_UNIV); 
   240        cb = of_phantom (card_UNIV :: 'b card_UNIV)
   241    in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
   242 instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
   243 end
   244 
   245 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
   246 definition "card_UNIV = Phantom('a \<Rightarrow> 'b)
   247   (let ca = of_phantom (card_UNIV :: 'a card_UNIV);
   248        cb = of_phantom (card_UNIV :: 'b card_UNIV)
   249    in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
   250 instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
   251 end
   252 
   253 instantiation option :: (card_UNIV) card_UNIV begin
   254 definition "card_UNIV = Phantom('a option)
   255   (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c \<noteq> 0 then Suc c else 0)"
   256 instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
   257 end
   258 
   259 instantiation String.literal :: card_UNIV begin
   260 definition "card_UNIV = Phantom(String.literal) 0"
   261 instance by intro_classes (simp add: card_UNIV_literal_def card_literal)
   262 end
   263 
   264 instantiation set :: (card_UNIV) card_UNIV begin
   265 definition "card_UNIV = Phantom('a set)
   266   (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c = 0 then 0 else 2 ^ c)"
   267 instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
   268 end
   269 
   270 
   271 lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]"
   272 by(auto intro: finite_1.exhaust)
   273 
   274 lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]"
   275 by(auto intro: finite_2.exhaust)
   276 
   277 lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^isub>1, finite_3.a\<^isub>2, finite_3.a\<^isub>3]"
   278 by(auto intro: finite_3.exhaust)
   279 
   280 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]"
   281 by(auto intro: finite_4.exhaust)
   282 
   283 lemma UNIV_finite_5:
   284   "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]"
   285 by(auto intro: finite_5.exhaust)
   286 
   287 instantiation Enum.finite_1 :: card_UNIV begin
   288 definition "card_UNIV = Phantom(Enum.finite_1) 1"
   289 instance by intro_classes (simp add: UNIV_finite_1 card_UNIV_finite_1_def)
   290 end
   291 
   292 instantiation Enum.finite_2 :: card_UNIV begin
   293 definition "card_UNIV = Phantom(Enum.finite_2) 2"
   294 instance by intro_classes (simp add: UNIV_finite_2 card_UNIV_finite_2_def)
   295 end
   296 
   297 instantiation Enum.finite_3 :: card_UNIV begin
   298 definition "card_UNIV = Phantom(Enum.finite_3) 3"
   299 instance by intro_classes (simp add: UNIV_finite_3 card_UNIV_finite_3_def)
   300 end
   301 
   302 instantiation Enum.finite_4 :: card_UNIV begin
   303 definition "card_UNIV = Phantom(Enum.finite_4) 4"
   304 instance by intro_classes (simp add: UNIV_finite_4 card_UNIV_finite_4_def)
   305 end
   306 
   307 instantiation Enum.finite_5 :: card_UNIV begin
   308 definition "card_UNIV = Phantom(Enum.finite_5) 5"
   309 instance by intro_classes (simp add: UNIV_finite_5 card_UNIV_finite_5_def)
   310 end
   311 
   312 subsection {* Code setup for sets *}
   313 
   314 lemma card_Compl:
   315   "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
   316 by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
   317 
   318 context fixes xs :: "'a :: card_UNIV list"
   319 begin
   320 
   321 definition card' :: "'a set \<Rightarrow> nat" 
   322 where [simp, code del, code_abbrev]: "card' = card"
   323 
   324 lemma card'_code [code]:
   325   "card' (set xs) = length (remdups xs)"
   326   "card' (List.coset xs) = of_phantom (card_UNIV :: 'a card_UNIV) - length (remdups xs)"
   327 by(simp_all add: List.card_set card_Compl card_UNIV)
   328 
   329 lemma card'_UNIV [code_unfold]: 
   330   "card' (UNIV :: 'a :: card_UNIV set) = of_phantom (card_UNIV :: 'a card_UNIV)"
   331 by(simp add: card_UNIV)
   332 
   333 definition finite' :: "'a set \<Rightarrow> bool"
   334 where [simp, code del, code_abbrev]: "finite' = finite"
   335 
   336 lemma finite'_code [code]:
   337   "finite' (set xs) \<longleftrightarrow> True"
   338   "finite' (List.coset xs) \<longleftrightarrow> CARD('a) > 0"
   339 by(simp_all add: card_gt_0_iff)
   340 
   341 definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   342 where [simp, code del, code_abbrev]: "subset' = op \<subseteq>"
   343 
   344 lemma subset'_code [code]:
   345   "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
   346   "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
   347   "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
   348 by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card])
   349   (metis finite_compl finite_set rev_finite_subset)
   350 
   351 definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
   352 where [simp, code del, code_abbrev]: "eq_set = op ="
   353 
   354 lemma eq_set_code [code]:
   355   fixes ys
   356   defines "rhs \<equiv> 
   357   let n = CARD('a)
   358   in if n = 0 then False else 
   359         let xs' = remdups xs; ys' = remdups ys 
   360         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')"
   361   shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
   362   and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
   363   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)
   364   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)
   365 proof -
   366   show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
   367   proof
   368     assume ?lhs thus ?rhs
   369       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)
   370   next
   371     assume ?rhs
   372     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
   373     ultimately show ?lhs
   374       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)
   375   qed
   376   thus ?thesis2 unfolding eq_set_def by blast
   377   show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
   378 qed
   379 
   380 end
   381 
   382 notepad begin (* test code setup *)
   383 have "List.coset [True] = set [False] \<and> List.coset [] \<subseteq> List.set [True, False] \<and> finite (List.coset [True])"
   384   by eval
   385 end
   386 
   387 hide_const (open) card' finite' subset' eq_set
   388 
   389 end