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