src/HOL/Library/Cardinality.thy
 author haftmann Fri Mar 22 19:18:08 2019 +0000 (3 months ago) changeset 69946 494934c30f38 parent 69663 41ff40bf1530 permissions -rw-r--r--
improved code equations taken over from AFP
```     1 (*  Title:      HOL/Library/Cardinality.thy
```
```     2     Author:     Brian Huffman, Andreas Lochbihler
```
```     3 *)
```
```     4
```
```     5 section \<open>Cardinality of types\<close>
```
```     6
```
```     7 theory Cardinality
```
```     8 imports Phantom_Type
```
```     9 begin
```
```    10
```
```    11 subsection \<open>Preliminary lemmas\<close>
```
```    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 \<open>Cardinalities of types\<close>
```
```    32
```
```    33 syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
```
```    34
```
```    35 translations "CARD('t)" => "CONST card (CONST UNIV :: 't set)"
```
```    36
```
```    37 print_translation \<open>
```
```    38   let
```
```    39     fun card_univ_tr' ctxt [Const (\<^const_syntax>\<open>UNIV\<close>, Type (_, [T]))] =
```
```    40       Syntax.const \<^syntax_const>\<open>_type_card\<close> \$ Syntax_Phases.term_of_typ ctxt T
```
```    41   in [(\<^const_syntax>\<open>card\<close>, card_univ_tr')] end
```
```    42 \<close>
```
```    43
```
```    44 lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a) * CARD('b)"
```
```    45   unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
```
```    46
```
```    47 lemma card_UNIV_sum: "CARD('a + 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 then CARD('a) + CARD('b) else 0)"
```
```    48 unfolding UNIV_Plus_UNIV[symmetric]
```
```    49 by(auto simp add: card_eq_0_iff card_Plus simp del: UNIV_Plus_UNIV)
```
```    50
```
```    51 lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
```
```    52 by(simp add: card_UNIV_sum)
```
```    53
```
```    54 lemma card_UNIV_option: "CARD('a option) = (if CARD('a) = 0 then 0 else CARD('a) + 1)"
```
```    55 proof -
```
```    56   have "(None :: 'a option) \<notin> range Some" by clarsimp
```
```    57   thus ?thesis
```
```    58     by (simp add: UNIV_option_conv card_eq_0_iff finite_range_Some card_image)
```
```    59 qed
```
```    60
```
```    61 lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
```
```    62 by(simp add: card_UNIV_option)
```
```    63
```
```    64 lemma card_UNIV_set: "CARD('a set) = (if CARD('a) = 0 then 0 else 2 ^ CARD('a))"
```
```    65 by(simp add: card_eq_0_iff card_Pow flip: Pow_UNIV)
```
```    66
```
```    67 lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
```
```    68 by(simp add: card_UNIV_set)
```
```    69
```
```    70 lemma card_nat [simp]: "CARD(nat) = 0"
```
```    71   by (simp add: card_eq_0_iff)
```
```    72
```
```    73 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)"
```
```    74 proof -
```
```    75   {  assume "0 < CARD('a)" and "0 < CARD('b)"
```
```    76     hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
```
```    77       by(simp_all only: card_ge_0_finite)
```
```    78     from finite_distinct_list[OF finb] obtain bs
```
```    79       where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
```
```    80     from finite_distinct_list[OF fina] obtain as
```
```    81       where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
```
```    82     have cb: "CARD('b) = length bs"
```
```    83       unfolding bs[symmetric] distinct_card[OF distb] ..
```
```    84     have ca: "CARD('a) = length as"
```
```    85       unfolding as[symmetric] distinct_card[OF dista] ..
```
```    86     let ?xs = "map (\<lambda>ys. the \<circ> map_of (zip as ys)) (List.n_lists (length as) bs)"
```
```    87     have "UNIV = set ?xs"
```
```    88     proof(rule UNIV_eq_I)
```
```    89       fix f :: "'a \<Rightarrow> 'b"
```
```    90       from as have "f = the \<circ> map_of (zip as (map f as))"
```
```    91         by(auto simp add: map_of_zip_map)
```
```    92       thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
```
```    93     qed
```
```    94     moreover have "distinct ?xs" unfolding distinct_map
```
```    95     proof(intro conjI distinct_n_lists distb inj_onI)
```
```    96       fix xs ys :: "'b list"
```
```    97       assume xs: "xs \<in> set (List.n_lists (length as) bs)"
```
```    98         and ys: "ys \<in> set (List.n_lists (length as) bs)"
```
```    99         and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
```
```   100       from xs ys have [simp]: "length xs = length as" "length ys = length as"
```
```   101         by(simp_all add: length_n_lists_elem)
```
```   102       have "map_of (zip as xs) = map_of (zip as ys)"
```
```   103       proof
```
```   104         fix x
```
```   105         from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y"
```
```   106           by(simp_all add: map_of_zip_is_Some[symmetric])
```
```   107         with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
```
```   108           by(auto dest: fun_cong[where x=x])
```
```   109       qed
```
```   110       with dista show "xs = ys" by(simp add: map_of_zip_inject)
```
```   111     qed
```
```   112     hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
```
```   113     moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
```
```   114     ultimately have "CARD('a \<Rightarrow> 'b) = CARD('b) ^ CARD('a)" using cb ca by simp }
```
```   115   moreover {
```
```   116     assume cb: "CARD('b) = 1"
```
```   117     then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
```
```   118     have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
```
```   119     proof(rule UNIV_eq_I)
```
```   120       fix x :: "'a \<Rightarrow> 'b"
```
```   121       { fix y
```
```   122         have "x y \<in> UNIV" ..
```
```   123         hence "x y = b" unfolding b by simp }
```
```   124       thus "x \<in> {\<lambda>x. b}" by(auto)
```
```   125     qed
```
```   126     have "CARD('a \<Rightarrow> 'b) = 1" unfolding eq by simp }
```
```   127   ultimately show ?thesis
```
```   128     by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
```
```   129 qed
```
```   130
```
```   131 corollary finite_UNIV_fun:
```
```   132   "finite (UNIV :: ('a \<Rightarrow> 'b) set) \<longleftrightarrow>
```
```   133    finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set) \<or> CARD('b) = 1"
```
```   134   (is "?lhs \<longleftrightarrow> ?rhs")
```
```   135 proof -
```
```   136   have "?lhs \<longleftrightarrow> CARD('a \<Rightarrow> 'b) > 0" by(simp add: card_gt_0_iff)
```
```   137   also have "\<dots> \<longleftrightarrow> CARD('a) > 0 \<and> CARD('b) > 0 \<or> CARD('b) = 1"
```
```   138     by(simp add: card_fun)
```
```   139   also have "\<dots> = ?rhs" by(simp add: card_gt_0_iff)
```
```   140   finally show ?thesis .
```
```   141 qed
```
```   142
```
```   143 lemma card_literal: "CARD(String.literal) = 0"
```
```   144 by(simp add: card_eq_0_iff infinite_literal)
```
```   145
```
```   146 subsection \<open>Classes with at least 1 and 2\<close>
```
```   147
```
```   148 text \<open>Class finite already captures "at least 1"\<close>
```
```   149
```
```   150 lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
```
```   151   unfolding neq0_conv [symmetric] by simp
```
```   152
```
```   153 lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
```
```   154   by (simp add: less_Suc_eq_le [symmetric])
```
```   155
```
```   156
```
```   157 class CARD_1 =
```
```   158   assumes CARD_1: "CARD ('a) = 1"
```
```   159 begin
```
```   160
```
```   161 subclass finite
```
```   162 proof
```
```   163   from CARD_1 show "finite (UNIV :: 'a set)"
```
```   164     by (auto intro!: card_ge_0_finite)
```
```   165 qed
```
```   166
```
```   167 end
```
```   168
```
```   169 text \<open>Class for cardinality "at least 2"\<close>
```
```   170
```
```   171 class card2 = finite +
```
```   172   assumes two_le_card: "2 \<le> CARD('a)"
```
```   173
```
```   174 lemma one_less_card: "Suc 0 < CARD('a::card2)"
```
```   175   using two_le_card [where 'a='a] by simp
```
```   176
```
```   177 lemma one_less_int_card: "1 < int CARD('a::card2)"
```
```   178   using one_less_card [where 'a='a] by simp
```
```   179
```
```   180
```
```   181 subsection \<open>A type class for deciding finiteness of types\<close>
```
```   182
```
```   183 type_synonym 'a finite_UNIV = "('a, bool) phantom"
```
```   184
```
```   185 class finite_UNIV =
```
```   186   fixes finite_UNIV :: "('a, bool) phantom"
```
```   187   assumes finite_UNIV: "finite_UNIV = Phantom('a) (finite (UNIV :: 'a set))"
```
```   188
```
```   189 lemma finite_UNIV_code [code_unfold]:
```
```   190   "finite (UNIV :: 'a :: finite_UNIV set)
```
```   191   \<longleftrightarrow> of_phantom (finite_UNIV :: 'a finite_UNIV)"
```
```   192 by(simp add: finite_UNIV)
```
```   193
```
```   194 subsection \<open>A type class for computing the cardinality of types\<close>
```
```   195
```
```   196 definition is_list_UNIV :: "'a list \<Rightarrow> bool"
```
```   197 where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"
```
```   198
```
```   199 lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
```
```   200 by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric]
```
```   201    dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
```
```   202
```
```   203 type_synonym 'a card_UNIV = "('a, nat) phantom"
```
```   204
```
```   205 class card_UNIV = finite_UNIV +
```
```   206   fixes card_UNIV :: "'a card_UNIV"
```
```   207   assumes card_UNIV: "card_UNIV = Phantom('a) CARD('a)"
```
```   208
```
```   209 subsection \<open>Instantiations for \<open>card_UNIV\<close>\<close>
```
```   210
```
```   211 instantiation nat :: card_UNIV begin
```
```   212 definition "finite_UNIV = Phantom(nat) False"
```
```   213 definition "card_UNIV = Phantom(nat) 0"
```
```   214 instance by intro_classes (simp_all add: finite_UNIV_nat_def card_UNIV_nat_def)
```
```   215 end
```
```   216
```
```   217 instantiation int :: card_UNIV begin
```
```   218 definition "finite_UNIV = Phantom(int) False"
```
```   219 definition "card_UNIV = Phantom(int) 0"
```
```   220 instance by intro_classes (simp_all add: card_UNIV_int_def finite_UNIV_int_def infinite_UNIV_int)
```
```   221 end
```
```   222
```
```   223 instantiation natural :: card_UNIV begin
```
```   224 definition "finite_UNIV = Phantom(natural) False"
```
```   225 definition "card_UNIV = Phantom(natural) 0"
```
```   226 instance
```
```   227   by standard
```
```   228     (auto simp add: finite_UNIV_natural_def card_UNIV_natural_def card_eq_0_iff
```
```   229       type_definition.univ [OF type_definition_natural] natural_eq_iff
```
```   230       dest!: finite_imageD intro: inj_onI)
```
```   231 end
```
```   232
```
```   233 instantiation integer :: card_UNIV begin
```
```   234 definition "finite_UNIV = Phantom(integer) False"
```
```   235 definition "card_UNIV = Phantom(integer) 0"
```
```   236 instance
```
```   237   by standard
```
```   238     (auto simp add: finite_UNIV_integer_def card_UNIV_integer_def card_eq_0_iff
```
```   239       type_definition.univ [OF type_definition_integer] infinite_UNIV_int
```
```   240       dest!: finite_imageD intro: inj_onI)
```
```   241 end
```
```   242
```
```   243 instantiation list :: (type) card_UNIV begin
```
```   244 definition "finite_UNIV = Phantom('a list) False"
```
```   245 definition "card_UNIV = Phantom('a list) 0"
```
```   246 instance by intro_classes (simp_all add: card_UNIV_list_def finite_UNIV_list_def infinite_UNIV_listI)
```
```   247 end
```
```   248
```
```   249 instantiation unit :: card_UNIV begin
```
```   250 definition "finite_UNIV = Phantom(unit) True"
```
```   251 definition "card_UNIV = Phantom(unit) 1"
```
```   252 instance by intro_classes (simp_all add: card_UNIV_unit_def finite_UNIV_unit_def)
```
```   253 end
```
```   254
```
```   255 instantiation bool :: card_UNIV begin
```
```   256 definition "finite_UNIV = Phantom(bool) True"
```
```   257 definition "card_UNIV = Phantom(bool) 2"
```
```   258 instance by(intro_classes)(simp_all add: card_UNIV_bool_def finite_UNIV_bool_def)
```
```   259 end
```
```   260
```
```   261 instantiation char :: card_UNIV begin
```
```   262 definition "finite_UNIV = Phantom(char) True"
```
```   263 definition "card_UNIV = Phantom(char) 256"
```
```   264 instance by intro_classes (simp_all add: card_UNIV_char_def card_UNIV_char finite_UNIV_char_def)
```
```   265 end
```
```   266
```
```   267 instantiation prod :: (finite_UNIV, finite_UNIV) finite_UNIV begin
```
```   268 definition "finite_UNIV = Phantom('a \<times> 'b)
```
```   269   (of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> of_phantom (finite_UNIV :: 'b finite_UNIV))"
```
```   270 instance by intro_classes (simp add: finite_UNIV_prod_def finite_UNIV finite_prod)
```
```   271 end
```
```   272
```
```   273 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
```
```   274 definition "card_UNIV = Phantom('a \<times> 'b)
```
```   275   (of_phantom (card_UNIV :: 'a card_UNIV) * of_phantom (card_UNIV :: 'b card_UNIV))"
```
```   276 instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
```
```   277 end
```
```   278
```
```   279 instantiation sum :: (finite_UNIV, finite_UNIV) finite_UNIV begin
```
```   280 definition "finite_UNIV = Phantom('a + 'b)
```
```   281   (of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> of_phantom (finite_UNIV :: 'b finite_UNIV))"
```
```   282 instance
```
```   283   by intro_classes (simp add: finite_UNIV_sum_def finite_UNIV)
```
```   284 end
```
```   285
```
```   286 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
```
```   287 definition "card_UNIV = Phantom('a + 'b)
```
```   288   (let ca = of_phantom (card_UNIV :: 'a card_UNIV);
```
```   289        cb = of_phantom (card_UNIV :: 'b card_UNIV)
```
```   290    in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
```
```   291 instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
```
```   292 end
```
```   293
```
```   294 instantiation "fun" :: (finite_UNIV, card_UNIV) finite_UNIV begin
```
```   295 definition "finite_UNIV = Phantom('a \<Rightarrow> 'b)
```
```   296   (let cb = of_phantom (card_UNIV :: 'b card_UNIV)
```
```   297    in cb = 1 \<or> of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> cb \<noteq> 0)"
```
```   298 instance
```
```   299   by intro_classes (auto simp add: finite_UNIV_fun_def Let_def card_UNIV finite_UNIV finite_UNIV_fun card_gt_0_iff)
```
```   300 end
```
```   301
```
```   302 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
```
```   303 definition "card_UNIV = Phantom('a \<Rightarrow> 'b)
```
```   304   (let ca = of_phantom (card_UNIV :: 'a card_UNIV);
```
```   305        cb = of_phantom (card_UNIV :: 'b card_UNIV)
```
```   306    in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
```
```   307 instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
```
```   308 end
```
```   309
```
```   310 instantiation option :: (finite_UNIV) finite_UNIV begin
```
```   311 definition "finite_UNIV = Phantom('a option) (of_phantom (finite_UNIV :: 'a finite_UNIV))"
```
```   312 instance by intro_classes (simp add: finite_UNIV_option_def finite_UNIV)
```
```   313 end
```
```   314
```
```   315 instantiation option :: (card_UNIV) card_UNIV begin
```
```   316 definition "card_UNIV = Phantom('a option)
```
```   317   (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c \<noteq> 0 then Suc c else 0)"
```
```   318 instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
```
```   319 end
```
```   320
```
```   321 instantiation String.literal :: card_UNIV begin
```
```   322 definition "finite_UNIV = Phantom(String.literal) False"
```
```   323 definition "card_UNIV = Phantom(String.literal) 0"
```
```   324 instance
```
```   325   by intro_classes (simp_all add: card_UNIV_literal_def finite_UNIV_literal_def infinite_literal card_literal)
```
```   326 end
```
```   327
```
```   328 instantiation set :: (finite_UNIV) finite_UNIV begin
```
```   329 definition "finite_UNIV = Phantom('a set) (of_phantom (finite_UNIV :: 'a finite_UNIV))"
```
```   330 instance by intro_classes (simp add: finite_UNIV_set_def finite_UNIV Finite_Set.finite_set)
```
```   331 end
```
```   332
```
```   333 instantiation set :: (card_UNIV) card_UNIV begin
```
```   334 definition "card_UNIV = Phantom('a set)
```
```   335   (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c = 0 then 0 else 2 ^ c)"
```
```   336 instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
```
```   337 end
```
```   338
```
```   339 lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^sub>1]"
```
```   340 by(auto intro: finite_1.exhaust)
```
```   341
```
```   342 lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^sub>1, finite_2.a\<^sub>2]"
```
```   343 by(auto intro: finite_2.exhaust)
```
```   344
```
```   345 lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^sub>1, finite_3.a\<^sub>2, finite_3.a\<^sub>3]"
```
```   346 by(auto intro: finite_3.exhaust)
```
```   347
```
```   348 lemma UNIV_finite_4: "UNIV = set [finite_4.a\<^sub>1, finite_4.a\<^sub>2, finite_4.a\<^sub>3, finite_4.a\<^sub>4]"
```
```   349 by(auto intro: finite_4.exhaust)
```
```   350
```
```   351 lemma UNIV_finite_5:
```
```   352   "UNIV = set [finite_5.a\<^sub>1, finite_5.a\<^sub>2, finite_5.a\<^sub>3, finite_5.a\<^sub>4, finite_5.a\<^sub>5]"
```
```   353 by(auto intro: finite_5.exhaust)
```
```   354
```
```   355 instantiation Enum.finite_1 :: card_UNIV begin
```
```   356 definition "finite_UNIV = Phantom(Enum.finite_1) True"
```
```   357 definition "card_UNIV = Phantom(Enum.finite_1) 1"
```
```   358 instance
```
```   359   by intro_classes (simp_all add: UNIV_finite_1 card_UNIV_finite_1_def finite_UNIV_finite_1_def)
```
```   360 end
```
```   361
```
```   362 instantiation Enum.finite_2 :: card_UNIV begin
```
```   363 definition "finite_UNIV = Phantom(Enum.finite_2) True"
```
```   364 definition "card_UNIV = Phantom(Enum.finite_2) 2"
```
```   365 instance
```
```   366   by intro_classes (simp_all add: UNIV_finite_2 card_UNIV_finite_2_def finite_UNIV_finite_2_def)
```
```   367 end
```
```   368
```
```   369 instantiation Enum.finite_3 :: card_UNIV begin
```
```   370 definition "finite_UNIV = Phantom(Enum.finite_3) True"
```
```   371 definition "card_UNIV = Phantom(Enum.finite_3) 3"
```
```   372 instance
```
```   373   by intro_classes (simp_all add: UNIV_finite_3 card_UNIV_finite_3_def finite_UNIV_finite_3_def)
```
```   374 end
```
```   375
```
```   376 instantiation Enum.finite_4 :: card_UNIV begin
```
```   377 definition "finite_UNIV = Phantom(Enum.finite_4) True"
```
```   378 definition "card_UNIV = Phantom(Enum.finite_4) 4"
```
```   379 instance
```
```   380   by intro_classes (simp_all add: UNIV_finite_4 card_UNIV_finite_4_def finite_UNIV_finite_4_def)
```
```   381 end
```
```   382
```
```   383 instantiation Enum.finite_5 :: card_UNIV begin
```
```   384 definition "finite_UNIV = Phantom(Enum.finite_5) True"
```
```   385 definition "card_UNIV = Phantom(Enum.finite_5) 5"
```
```   386 instance
```
```   387   by intro_classes (simp_all add: UNIV_finite_5 card_UNIV_finite_5_def finite_UNIV_finite_5_def)
```
```   388 end
```
```   389
```
```   390 subsection \<open>Code setup for sets\<close>
```
```   391
```
```   392 text \<open>
```
```   393   Implement \<^term>\<open>CARD('a)\<close> via \<^term>\<open>card_UNIV\<close> and provide
```
```   394   implementations for \<^term>\<open>finite\<close>, \<^term>\<open>card\<close>, \<^term>\<open>(\<subseteq>)\<close>,
```
```   395   and \<^term>\<open>(=)\<close>if the calling context already provides \<^class>\<open>finite_UNIV\<close>
```
```   396   and \<^class>\<open>card_UNIV\<close> instances. If we implemented the latter
```
```   397   always via \<^term>\<open>card_UNIV\<close>, we would require instances of essentially all
```
```   398   element types, i.e., a lot of instantiation proofs and -- at run time --
```
```   399   possibly slow dictionary constructions.
```
```   400 \<close>
```
```   401
```
```   402 context
```
```   403 begin
```
```   404
```
```   405 qualified definition card_UNIV' :: "'a card_UNIV"
```
```   406 where [code del]: "card_UNIV' = Phantom('a) CARD('a)"
```
```   407
```
```   408 lemma CARD_code [code_unfold]:
```
```   409   "CARD('a) = of_phantom (card_UNIV' :: 'a card_UNIV)"
```
```   410 by(simp add: card_UNIV'_def)
```
```   411
```
```   412 lemma card_UNIV'_code [code]:
```
```   413   "card_UNIV' = card_UNIV"
```
```   414 by(simp add: card_UNIV card_UNIV'_def)
```
```   415
```
```   416 end
```
```   417
```
```   418 lemma card_Compl:
```
```   419   "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
```
```   420 by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
```
```   421
```
```   422 context fixes xs :: "'a :: finite_UNIV list"
```
```   423 begin
```
```   424
```
```   425 qualified definition finite' :: "'a set \<Rightarrow> bool"
```
```   426 where [simp, code del, code_abbrev]: "finite' = finite"
```
```   427
```
```   428 lemma finite'_code [code]:
```
```   429   "finite' (set xs) \<longleftrightarrow> True"
```
```   430   "finite' (List.coset xs) \<longleftrightarrow> of_phantom (finite_UNIV :: 'a finite_UNIV)"
```
```   431 by(simp_all add: card_gt_0_iff finite_UNIV)
```
```   432
```
```   433 end
```
```   434
```
```   435 context fixes xs :: "'a :: card_UNIV list"
```
```   436 begin
```
```   437
```
```   438 qualified definition card' :: "'a set \<Rightarrow> nat"
```
```   439 where [simp, code del, code_abbrev]: "card' = card"
```
```   440
```
```   441 lemma card'_code [code]:
```
```   442   "card' (set xs) = length (remdups xs)"
```
```   443   "card' (List.coset xs) = of_phantom (card_UNIV :: 'a card_UNIV) - length (remdups xs)"
```
```   444 by(simp_all add: List.card_set card_Compl card_UNIV)
```
```   445
```
```   446
```
```   447 qualified definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
```
```   448 where [simp, code del, code_abbrev]: "subset' = (\<subseteq>)"
```
```   449
```
```   450 lemma subset'_code [code]:
```
```   451   "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
```
```   452   "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
```
```   453   "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
```
```   454 by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card])
```
```   455   (metis finite_compl finite_set rev_finite_subset)
```
```   456
```
```   457 qualified definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
```
```   458 where [simp, code del, code_abbrev]: "eq_set = (=)"
```
```   459
```
```   460 lemma eq_set_code [code]:
```
```   461   fixes ys
```
```   462   defines "rhs \<equiv>
```
```   463   let n = CARD('a)
```
```   464   in if n = 0 then False else
```
```   465         let xs' = remdups xs; ys' = remdups ys
```
```   466         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')"
```
```   467   shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs"
```
```   468   and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs"
```
```   469   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)"
```
```   470   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)"
```
```   471 proof goal_cases
```
```   472   {
```
```   473     case 1
```
```   474     show ?case (is "?lhs \<longleftrightarrow> ?rhs")
```
```   475     proof
```
```   476       show ?rhs if ?lhs
```
```   477         using that
```
```   478         by (auto simp add: rhs_def Let_def List.card_set[symmetric]
```
```   479           card_Un_Int[where A="set xs" and B="- set xs"] card_UNIV
```
```   480           Compl_partition card_gt_0_iff dest: sym)(metis finite_compl finite_set)
```
```   481       show ?lhs if ?rhs
```
```   482       proof -
```
```   483         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
```
```   484         with that show ?thesis
```
```   485           by (auto simp add: rhs_def Let_def List.card_set[symmetric]
```
```   486             card_UNIV card_gt_0_iff card_Un_Int[where A="set xs" and B="set ys"]
```
```   487             dest: card_eq_UNIV_imp_eq_UNIV split: if_split_asm)
```
```   488       qed
```
```   489     qed
```
```   490   }
```
```   491   moreover
```
```   492   case 2
```
```   493   ultimately show ?case unfolding eq_set_def by blast
```
```   494 next
```
```   495   case 3
```
```   496   show ?case unfolding eq_set_def List.coset_def by blast
```
```   497 next
```
```   498   case 4
```
```   499   show ?case unfolding eq_set_def List.coset_def by blast
```
```   500 qed
```
```   501
```
```   502 end
```
```   503
```
```   504 text \<open>
```
```   505   Provide more informative exceptions than Match for non-rewritten cases.
```
```   506   If generated code raises one these exceptions, then a code equation calls
```
```   507   the mentioned operator for an element type that is not an instance of
```
```   508   \<^class>\<open>card_UNIV\<close> and is therefore not implemented via \<^term>\<open>card_UNIV\<close>.
```
```   509   Constrain the element type with sort \<^class>\<open>card_UNIV\<close> to change this.
```
```   510 \<close>
```
```   511
```
```   512 lemma card_coset_error [code]:
```
```   513   "card (List.coset xs) =
```
```   514    Code.abort (STR ''card (List.coset _) requires type class instance card_UNIV'')
```
```   515      (\<lambda>_. card (List.coset xs))"
```
```   516 by(simp)
```
```   517
```
```   518 lemma coset_subseteq_set_code [code]:
```
```   519   "List.coset xs \<subseteq> set ys \<longleftrightarrow>
```
```   520   (if xs = [] \<and> ys = [] then False
```
```   521    else Code.abort
```
```   522      (STR ''subset_eq (List.coset _) (List.set _) requires type class instance card_UNIV'')
```
```   523      (\<lambda>_. List.coset xs \<subseteq> set ys))"
```
```   524 by simp
```
```   525
```
```   526 notepad begin \<comment> \<open>test code setup\<close>
```
```   527 have "List.coset [True] = set [False] \<and>
```
```   528       List.coset [] \<subseteq> List.set [True, False] \<and>
```
```   529       finite (List.coset [True])"
```
```   530   by eval
```
```   531 end
```
```   532
```
```   533 end
```