src/HOL/Library/Cardinality.thy
 author haftmann Fri Feb 15 08:31:31 2013 +0100 (2013-02-15) changeset 51143 0a2371e7ced3 parent 51139 c8e3cf3520b3 child 51188 9b5bf1a9a710 permissions -rw-r--r--
two target language numeral types: integer and natural, as replacement for code_numeral;
former theory HOL/Library/Code_Numeral_Types replaces HOL/Code_Numeral;
refined stack of theories implementing int and/or nat by target language numerals;
reduced number of target language numeral types to exactly one
1 (*  Title:      HOL/Library/Cardinality.thy
2     Author:     Brian Huffman, Andreas Lochbihler
3 *)
5 header {* Cardinality of types *}
7 theory Cardinality
8 imports Phantom_Type
9 begin
11 subsection {* Preliminary lemmas *}
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 {* Cardinalities of types *}
42 syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
44 translations "CARD('t)" => "CONST card (CONST UNIV \<Colon> 't set)"
46 typed_print_translation (advanced) {*
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 *}
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_insert_disjoint 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 o 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_nibble: "CARD(nibble) = 16"
153 unfolding UNIV_nibble by simp
155 lemma card_UNIV_char: "CARD(char) = 256"
156 proof -
157   have "inj (\<lambda>(x, y). Char x y)" by(auto intro: injI)
158   thus ?thesis unfolding UNIV_char by(simp add: card_image card_nibble)
159 qed
161 lemma card_literal: "CARD(String.literal) = 0"
162 by(simp add: card_eq_0_iff infinite_literal)
164 subsection {* Classes with at least 1 and 2  *}
166 text {* Class finite already captures "at least 1" *}
168 lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
169   unfolding neq0_conv [symmetric] by simp
171 lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
172   by (simp add: less_Suc_eq_le [symmetric])
174 text {* Class for cardinality "at least 2" *}
176 class card2 = finite +
177   assumes two_le_card: "2 \<le> CARD('a)"
179 lemma one_less_card: "Suc 0 < CARD('a::card2)"
180   using two_le_card [where 'a='a] by simp
182 lemma one_less_int_card: "1 < int CARD('a::card2)"
183   using one_less_card [where 'a='a] by simp
186 subsection {* A type class for deciding finiteness of types *}
188 type_synonym 'a finite_UNIV = "('a, bool) phantom"
190 class finite_UNIV =
191   fixes finite_UNIV :: "('a, bool) phantom"
192   assumes finite_UNIV: "finite_UNIV = Phantom('a) (finite (UNIV :: 'a set))"
194 lemma finite_UNIV_code [code_unfold]:
195   "finite (UNIV :: 'a :: finite_UNIV set)
196   \<longleftrightarrow> of_phantom (finite_UNIV :: 'a finite_UNIV)"
197 by(simp add: finite_UNIV)
199 subsection {* A type class for computing the cardinality of types *}
201 definition is_list_UNIV :: "'a list \<Rightarrow> bool"
202 where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"
204 lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
205 by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric]
206    dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
208 type_synonym 'a card_UNIV = "('a, nat) phantom"
210 class card_UNIV = finite_UNIV +
211   fixes card_UNIV :: "'a card_UNIV"
212   assumes card_UNIV: "card_UNIV = Phantom('a) CARD('a)"
214 subsection {* Instantiations for @{text "card_UNIV"} *}
216 instantiation nat :: card_UNIV begin
217 definition "finite_UNIV = Phantom(nat) False"
218 definition "card_UNIV = Phantom(nat) 0"
219 instance by intro_classes (simp_all add: finite_UNIV_nat_def card_UNIV_nat_def)
220 end
222 instantiation int :: card_UNIV begin
223 definition "finite_UNIV = Phantom(int) False"
224 definition "card_UNIV = Phantom(int) 0"
225 instance by intro_classes (simp_all add: card_UNIV_int_def finite_UNIV_int_def infinite_UNIV_int)
226 end
228 instantiation natural :: card_UNIV begin
229 definition "finite_UNIV = Phantom(natural) False"
230 definition "card_UNIV = Phantom(natural) 0"
231 instance proof
232 qed (auto simp add: finite_UNIV_natural_def card_UNIV_natural_def card_eq_0_iff
233   type_definition.univ [OF type_definition_natural] natural_eq_iff
234   dest!: finite_imageD intro: inj_onI)
235 end
237 declare [[show_consts]]
239 instantiation integer :: card_UNIV begin
240 definition "finite_UNIV = Phantom(integer) False"
241 definition "card_UNIV = Phantom(integer) 0"
242 instance proof
243 qed (auto simp add: finite_UNIV_integer_def card_UNIV_integer_def card_eq_0_iff
244   type_definition.univ [OF type_definition_integer] infinite_UNIV_int
245   dest!: finite_imageD intro: inj_onI)
246 end
248 instantiation list :: (type) card_UNIV begin
249 definition "finite_UNIV = Phantom('a list) False"
250 definition "card_UNIV = Phantom('a list) 0"
251 instance by intro_classes (simp_all add: card_UNIV_list_def finite_UNIV_list_def infinite_UNIV_listI)
252 end
254 instantiation unit :: card_UNIV begin
255 definition "finite_UNIV = Phantom(unit) True"
256 definition "card_UNIV = Phantom(unit) 1"
257 instance by intro_classes (simp_all add: card_UNIV_unit_def finite_UNIV_unit_def)
258 end
260 instantiation bool :: card_UNIV begin
261 definition "finite_UNIV = Phantom(bool) True"
262 definition "card_UNIV = Phantom(bool) 2"
263 instance by(intro_classes)(simp_all add: card_UNIV_bool_def finite_UNIV_bool_def)
264 end
266 instantiation nibble :: card_UNIV begin
267 definition "finite_UNIV = Phantom(nibble) True"
268 definition "card_UNIV = Phantom(nibble) 16"
269 instance by(intro_classes)(simp_all add: card_UNIV_nibble_def card_nibble finite_UNIV_nibble_def)
270 end
272 instantiation char :: card_UNIV begin
273 definition "finite_UNIV = Phantom(char) True"
274 definition "card_UNIV = Phantom(char) 256"
275 instance by intro_classes (simp_all add: card_UNIV_char_def card_UNIV_char finite_UNIV_char_def)
276 end
278 instantiation prod :: (finite_UNIV, finite_UNIV) finite_UNIV begin
279 definition "finite_UNIV = Phantom('a \<times> 'b)
280   (of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> of_phantom (finite_UNIV :: 'b finite_UNIV))"
281 instance by intro_classes (simp add: finite_UNIV_prod_def finite_UNIV finite_prod)
282 end
284 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
285 definition "card_UNIV = Phantom('a \<times> 'b)
286   (of_phantom (card_UNIV :: 'a card_UNIV) * of_phantom (card_UNIV :: 'b card_UNIV))"
287 instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
288 end
290 instantiation sum :: (finite_UNIV, finite_UNIV) finite_UNIV begin
291 definition "finite_UNIV = Phantom('a + 'b)
292   (of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> of_phantom (finite_UNIV :: 'b finite_UNIV))"
293 instance
294   by intro_classes (simp add: UNIV_Plus_UNIV[symmetric] finite_UNIV_sum_def finite_UNIV del: UNIV_Plus_UNIV)
295 end
297 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
298 definition "card_UNIV = Phantom('a + 'b)
299   (let ca = of_phantom (card_UNIV :: 'a card_UNIV);
300        cb = of_phantom (card_UNIV :: 'b card_UNIV)
301    in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
302 instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
303 end
305 instantiation "fun" :: (finite_UNIV, card_UNIV) finite_UNIV begin
306 definition "finite_UNIV = Phantom('a \<Rightarrow> 'b)
307   (let cb = of_phantom (card_UNIV :: 'b card_UNIV)
308    in cb = 1 \<or> of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> cb \<noteq> 0)"
309 instance
310   by intro_classes (auto simp add: finite_UNIV_fun_def Let_def card_UNIV finite_UNIV finite_UNIV_fun card_gt_0_iff)
311 end
313 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
314 definition "card_UNIV = Phantom('a \<Rightarrow> 'b)
315   (let ca = of_phantom (card_UNIV :: 'a card_UNIV);
316        cb = of_phantom (card_UNIV :: 'b card_UNIV)
317    in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
318 instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
319 end
321 instantiation option :: (finite_UNIV) finite_UNIV begin
322 definition "finite_UNIV = Phantom('a option) (of_phantom (finite_UNIV :: 'a finite_UNIV))"
323 instance by intro_classes (simp add: finite_UNIV_option_def finite_UNIV)
324 end
326 instantiation option :: (card_UNIV) card_UNIV begin
327 definition "card_UNIV = Phantom('a option)
328   (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c \<noteq> 0 then Suc c else 0)"
329 instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
330 end
332 instantiation String.literal :: card_UNIV begin
333 definition "finite_UNIV = Phantom(String.literal) False"
334 definition "card_UNIV = Phantom(String.literal) 0"
335 instance
336   by intro_classes (simp_all add: card_UNIV_literal_def finite_UNIV_literal_def infinite_literal card_literal)
337 end
339 instantiation set :: (finite_UNIV) finite_UNIV begin
340 definition "finite_UNIV = Phantom('a set) (of_phantom (finite_UNIV :: 'a finite_UNIV))"
341 instance by intro_classes (simp add: finite_UNIV_set_def finite_UNIV Finite_Set.finite_set)
342 end
344 instantiation set :: (card_UNIV) card_UNIV begin
345 definition "card_UNIV = Phantom('a set)
346   (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c = 0 then 0 else 2 ^ c)"
347 instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
348 end
350 lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]"
351 by(auto intro: finite_1.exhaust)
353 lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]"
354 by(auto intro: finite_2.exhaust)
356 lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^isub>1, finite_3.a\<^isub>2, finite_3.a\<^isub>3]"
357 by(auto intro: finite_3.exhaust)
359 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]"
360 by(auto intro: finite_4.exhaust)
362 lemma UNIV_finite_5:
363   "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]"
364 by(auto intro: finite_5.exhaust)
366 instantiation Enum.finite_1 :: card_UNIV begin
367 definition "finite_UNIV = Phantom(Enum.finite_1) True"
368 definition "card_UNIV = Phantom(Enum.finite_1) 1"
369 instance
370   by intro_classes (simp_all add: UNIV_finite_1 card_UNIV_finite_1_def finite_UNIV_finite_1_def)
371 end
373 instantiation Enum.finite_2 :: card_UNIV begin
374 definition "finite_UNIV = Phantom(Enum.finite_2) True"
375 definition "card_UNIV = Phantom(Enum.finite_2) 2"
376 instance
377   by intro_classes (simp_all add: UNIV_finite_2 card_UNIV_finite_2_def finite_UNIV_finite_2_def)
378 end
380 instantiation Enum.finite_3 :: card_UNIV begin
381 definition "finite_UNIV = Phantom(Enum.finite_3) True"
382 definition "card_UNIV = Phantom(Enum.finite_3) 3"
383 instance
384   by intro_classes (simp_all add: UNIV_finite_3 card_UNIV_finite_3_def finite_UNIV_finite_3_def)
385 end
387 instantiation Enum.finite_4 :: card_UNIV begin
388 definition "finite_UNIV = Phantom(Enum.finite_4) True"
389 definition "card_UNIV = Phantom(Enum.finite_4) 4"
390 instance
391   by intro_classes (simp_all add: UNIV_finite_4 card_UNIV_finite_4_def finite_UNIV_finite_4_def)
392 end
394 instantiation Enum.finite_5 :: card_UNIV begin
395 definition "finite_UNIV = Phantom(Enum.finite_5) True"
396 definition "card_UNIV = Phantom(Enum.finite_5) 5"
397 instance
398   by intro_classes (simp_all add: UNIV_finite_5 card_UNIV_finite_5_def finite_UNIV_finite_5_def)
399 end
401 subsection {* Code setup for sets *}
403 text {*
404   Implement @{term "CARD('a)"} via @{term card_UNIV} and provide
405   implementations for @{term "finite"}, @{term "card"}, @{term "op \<subseteq>"},
406   and @{term "op ="}if the calling context already provides @{class finite_UNIV}
407   and @{class card_UNIV} instances. If we implemented the latter
408   always via @{term card_UNIV}, we would require instances of essentially all
409   element types, i.e., a lot of instantiation proofs and -- at run time --
410   possibly slow dictionary constructions.
411 *}
413 definition card_UNIV' :: "'a card_UNIV"
414 where [code del]: "card_UNIV' = Phantom('a) CARD('a)"
416 lemma CARD_code [code_unfold]:
417   "CARD('a) = of_phantom (card_UNIV' :: 'a card_UNIV)"
418 by(simp add: card_UNIV'_def)
420 lemma card_UNIV'_code [code]:
421   "card_UNIV' = card_UNIV"
422 by(simp add: card_UNIV card_UNIV'_def)
424 hide_const (open) card_UNIV'
426 lemma card_Compl:
427   "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
428 by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
430 context fixes xs :: "'a :: finite_UNIV list"
431 begin
433 definition finite' :: "'a set \<Rightarrow> bool"
434 where [simp, code del, code_abbrev]: "finite' = finite"
436 lemma finite'_code [code]:
437   "finite' (set xs) \<longleftrightarrow> True"
438   "finite' (List.coset xs) \<longleftrightarrow> of_phantom (finite_UNIV :: 'a finite_UNIV)"
439 by(simp_all add: card_gt_0_iff finite_UNIV)
441 end
443 context fixes xs :: "'a :: card_UNIV list"
444 begin
446 definition card' :: "'a set \<Rightarrow> nat"
447 where [simp, code del, code_abbrev]: "card' = card"
449 lemma card'_code [code]:
450   "card' (set xs) = length (remdups xs)"
451   "card' (List.coset xs) = of_phantom (card_UNIV :: 'a card_UNIV) - length (remdups xs)"
452 by(simp_all add: List.card_set card_Compl card_UNIV)
455 definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
456 where [simp, code del, code_abbrev]: "subset' = op \<subseteq>"
458 lemma subset'_code [code]:
459   "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
460   "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
461   "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
462 by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card])
463   (metis finite_compl finite_set rev_finite_subset)
465 definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
466 where [simp, code del, code_abbrev]: "eq_set = op ="
468 lemma eq_set_code [code]:
469   fixes ys
470   defines "rhs \<equiv>
471   let n = CARD('a)
472   in if n = 0 then False else
473         let xs' = remdups xs; ys' = remdups ys
474         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')"
475   shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
476   and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
477   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)
478   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)
479 proof -
480   show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
481   proof
482     assume ?lhs thus ?rhs
483       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)
484   next
485     assume ?rhs
486     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
487     ultimately show ?lhs
488       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)
489   qed
490   thus ?thesis2 unfolding eq_set_def by blast
491   show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
492 qed
494 end
496 text {*
497   Provide more informative exceptions than Match for non-rewritten cases.
498   If generated code raises one these exceptions, then a code equation calls
499   the mentioned operator for an element type that is not an instance of
500   @{class card_UNIV} and is therefore not implemented via @{term card_UNIV}.
501   Constrain the element type with sort @{class card_UNIV} to change this.
502 *}
504 definition card_coset_requires_card_UNIV :: "'a list \<Rightarrow> nat"
505 where [code del, simp]: "card_coset_requires_card_UNIV xs = card (List.coset xs)"
507 code_abort card_coset_requires_card_UNIV
509 lemma card_coset_error [code]:
510   "card (List.coset xs) = card_coset_requires_card_UNIV xs"
511 by(simp)
513 definition coset_subseteq_set_requires_card_UNIV :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
514 where [code del, simp]: "coset_subseteq_set_requires_card_UNIV xs ys \<longleftrightarrow> List.coset xs \<subseteq> set ys"
516 code_abort coset_subseteq_set_requires_card_UNIV
518 lemma coset_subseteq_set_code [code]:
519   "List.coset xs \<subseteq> set ys \<longleftrightarrow>
520   (if xs = [] \<and> ys = [] then False else coset_subseteq_set_requires_card_UNIV xs ys)"
521 by simp
523 notepad begin -- "test code setup"
524 have "List.coset [True] = set [False] \<and>
525       List.coset [] \<subseteq> List.set [True, False] \<and>
526       finite (List.coset [True])"
527   by eval
528 end
530 hide_const (open) card' finite' subset' eq_set
532 end