129 have "CARD('a \<Rightarrow> 'b) = 1" unfolding eq by simp } |
135 have "CARD('a \<Rightarrow> 'b) = 1" unfolding eq by simp } |
130 ultimately show ?thesis |
136 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) |
137 by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1) |
132 qed |
138 qed |
133 |
139 |
|
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 |
|
151 |
134 lemma card_nibble: "CARD(nibble) = 16" |
152 lemma card_nibble: "CARD(nibble) = 16" |
135 unfolding UNIV_nibble by simp |
153 unfolding UNIV_nibble by simp |
136 |
154 |
137 lemma card_UNIV_char: "CARD(char) = 256" |
155 lemma card_UNIV_char: "CARD(char) = 256" |
138 proof - |
156 proof - |
139 have "inj (\<lambda>(x, y). Char x y)" by(auto intro: injI) |
157 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) |
158 thus ?thesis unfolding UNIV_char by(simp add: card_image card_nibble) |
141 qed |
159 qed |
142 |
160 |
143 lemma card_literal: "CARD(String.literal) = 0" |
161 lemma card_literal: "CARD(String.literal) = 0" |
144 proof - |
162 by(simp add: card_eq_0_iff infinite_literal) |
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 |
163 |
149 subsection {* Classes with at least 1 and 2 *} |
164 subsection {* Classes with at least 1 and 2 *} |
150 |
165 |
151 text {* Class finite already captures "at least 1" *} |
166 text {* Class finite already captures "at least 1" *} |
152 |
167 |
189 lemma is_list_UNIV_code [code]: |
218 lemma is_list_UNIV_code [code]: |
190 "is_list_UNIV (xs :: 'a list) = |
219 "is_list_UNIV (xs :: 'a list) = |
191 (let c = CARD('a :: card_UNIV) in if c = 0 then False else size (remdups xs) = c)" |
220 (let c = CARD('a :: card_UNIV) in if c = 0 then False else size (remdups xs) = c)" |
192 by(rule is_list_UNIV_def) |
221 by(rule is_list_UNIV_def) |
193 |
222 |
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"} *} |
223 subsection {* Instantiations for @{text "card_UNIV"} *} |
200 |
224 |
201 instantiation nat :: card_UNIV begin |
225 instantiation nat :: card_UNIV begin |
|
226 definition "finite_UNIV = Phantom(nat) False" |
202 definition "card_UNIV = Phantom(nat) 0" |
227 definition "card_UNIV = Phantom(nat) 0" |
203 instance by intro_classes (simp add: card_UNIV_nat_def) |
228 instance by intro_classes (simp_all add: finite_UNIV_nat_def card_UNIV_nat_def) |
204 end |
229 end |
205 |
230 |
206 instantiation int :: card_UNIV begin |
231 instantiation int :: card_UNIV begin |
|
232 definition "finite_UNIV = Phantom(int) False" |
207 definition "card_UNIV = Phantom(int) 0" |
233 definition "card_UNIV = Phantom(int) 0" |
208 instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int) |
234 instance by intro_classes (simp_all add: card_UNIV_int_def finite_UNIV_int_def infinite_UNIV_int) |
209 end |
235 end |
210 |
236 |
211 instantiation code_numeral :: card_UNIV begin |
237 instantiation code_numeral :: card_UNIV begin |
|
238 definition "finite_UNIV = Phantom(code_numeral) False" |
212 definition "card_UNIV = Phantom(code_numeral) 0" |
239 definition "card_UNIV = Phantom(code_numeral) 0" |
213 instance |
240 instance |
214 by(intro_classes)(auto simp add: card_UNIV_code_numeral_def type_definition.univ[OF type_definition_code_numeral] card_eq_0_iff dest!: finite_imageD intro: inj_onI) |
241 by(intro_classes)(auto simp add: card_UNIV_code_numeral_def finite_UNIV_code_numeral_def type_definition.univ[OF type_definition_code_numeral] card_eq_0_iff dest!: finite_imageD intro: inj_onI) |
215 end |
242 end |
216 |
243 |
217 instantiation list :: (type) card_UNIV begin |
244 instantiation list :: (type) card_UNIV begin |
|
245 definition "finite_UNIV = Phantom('a list) False" |
218 definition "card_UNIV = Phantom('a list) 0" |
246 definition "card_UNIV = Phantom('a list) 0" |
219 instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI) |
247 instance by intro_classes (simp_all add: card_UNIV_list_def finite_UNIV_list_def infinite_UNIV_listI) |
220 end |
248 end |
221 |
249 |
222 instantiation unit :: card_UNIV begin |
250 instantiation unit :: card_UNIV begin |
|
251 definition "finite_UNIV = Phantom(unit) True" |
223 definition "card_UNIV = Phantom(unit) 1" |
252 definition "card_UNIV = Phantom(unit) 1" |
224 instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit) |
253 instance by intro_classes (simp_all add: card_UNIV_unit_def finite_UNIV_unit_def) |
225 end |
254 end |
226 |
255 |
227 instantiation bool :: card_UNIV begin |
256 instantiation bool :: card_UNIV begin |
|
257 definition "finite_UNIV = Phantom(bool) True" |
228 definition "card_UNIV = Phantom(bool) 2" |
258 definition "card_UNIV = Phantom(bool) 2" |
229 instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool) |
259 instance by(intro_classes)(simp_all add: card_UNIV_bool_def finite_UNIV_bool_def) |
230 end |
260 end |
231 |
261 |
232 instantiation nibble :: card_UNIV begin |
262 instantiation nibble :: card_UNIV begin |
|
263 definition "finite_UNIV = Phantom(nibble) True" |
233 definition "card_UNIV = Phantom(nibble) 16" |
264 definition "card_UNIV = Phantom(nibble) 16" |
234 instance by(intro_classes)(simp add: card_UNIV_nibble_def card_nibble) |
265 instance by(intro_classes)(simp_all add: card_UNIV_nibble_def card_nibble finite_UNIV_nibble_def) |
235 end |
266 end |
236 |
267 |
237 instantiation char :: card_UNIV begin |
268 instantiation char :: card_UNIV begin |
|
269 definition "finite_UNIV = Phantom(char) True" |
238 definition "card_UNIV = Phantom(char) 256" |
270 definition "card_UNIV = Phantom(char) 256" |
239 instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char) |
271 instance by intro_classes (simp_all add: card_UNIV_char_def card_UNIV_char finite_UNIV_char_def) |
|
272 end |
|
273 |
|
274 instantiation prod :: (finite_UNIV, finite_UNIV) finite_UNIV begin |
|
275 definition "finite_UNIV = Phantom('a \<times> 'b) |
|
276 (of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> of_phantom (finite_UNIV :: 'b finite_UNIV))" |
|
277 instance by intro_classes (simp add: finite_UNIV_prod_def finite_UNIV finite_prod) |
240 end |
278 end |
241 |
279 |
242 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin |
280 instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin |
243 definition "card_UNIV = |
281 definition "card_UNIV = Phantom('a \<times> 'b) |
244 Phantom('a \<times> 'b) (of_phantom (card_UNIV :: 'a card_UNIV) * of_phantom (card_UNIV :: 'b card_UNIV))" |
282 (of_phantom (card_UNIV :: 'a card_UNIV) * of_phantom (card_UNIV :: 'b card_UNIV))" |
245 instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV) |
283 instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV) |
|
284 end |
|
285 |
|
286 instantiation sum :: (finite_UNIV, finite_UNIV) finite_UNIV begin |
|
287 definition "finite_UNIV = Phantom('a + 'b) |
|
288 (of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> of_phantom (finite_UNIV :: 'b finite_UNIV))" |
|
289 instance |
|
290 by intro_classes (simp add: UNIV_Plus_UNIV[symmetric] finite_UNIV_sum_def finite_UNIV del: UNIV_Plus_UNIV) |
246 end |
291 end |
247 |
292 |
248 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin |
293 instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin |
249 definition "card_UNIV = Phantom('a + 'b) |
294 definition "card_UNIV = Phantom('a + 'b) |
250 (let ca = of_phantom (card_UNIV :: 'a card_UNIV); |
295 (let ca = of_phantom (card_UNIV :: 'a card_UNIV); |
251 cb = of_phantom (card_UNIV :: 'b card_UNIV) |
296 cb = of_phantom (card_UNIV :: 'b card_UNIV) |
252 in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)" |
297 in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)" |
253 instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum) |
298 instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum) |
254 end |
299 end |
255 |
300 |
|
301 instantiation "fun" :: (finite_UNIV, card_UNIV) finite_UNIV begin |
|
302 definition "finite_UNIV = Phantom('a \<Rightarrow> 'b) |
|
303 (let cb = of_phantom (card_UNIV :: 'b card_UNIV) |
|
304 in cb = 1 \<or> of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> cb \<noteq> 0)" |
|
305 instance |
|
306 by intro_classes (auto simp add: finite_UNIV_fun_def Let_def card_UNIV finite_UNIV finite_UNIV_fun card_gt_0_iff) |
|
307 end |
|
308 |
256 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin |
309 instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin |
257 definition "card_UNIV = Phantom('a \<Rightarrow> 'b) |
310 definition "card_UNIV = Phantom('a \<Rightarrow> 'b) |
258 (let ca = of_phantom (card_UNIV :: 'a card_UNIV); |
311 (let ca = of_phantom (card_UNIV :: 'a card_UNIV); |
259 cb = of_phantom (card_UNIV :: 'b card_UNIV) |
312 cb = of_phantom (card_UNIV :: 'b card_UNIV) |
260 in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)" |
313 in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)" |
261 instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun) |
314 instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun) |
262 end |
315 end |
263 |
316 |
|
317 instantiation option :: (finite_UNIV) finite_UNIV begin |
|
318 definition "finite_UNIV = Phantom('a option) (of_phantom (finite_UNIV :: 'a finite_UNIV))" |
|
319 instance by intro_classes (simp add: finite_UNIV_option_def finite_UNIV) |
|
320 end |
|
321 |
264 instantiation option :: (card_UNIV) card_UNIV begin |
322 instantiation option :: (card_UNIV) card_UNIV begin |
265 definition "card_UNIV = Phantom('a option) |
323 definition "card_UNIV = Phantom('a option) |
266 (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c \<noteq> 0 then Suc c else 0)" |
324 (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c \<noteq> 0 then Suc c else 0)" |
267 instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option) |
325 instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option) |
268 end |
326 end |
269 |
327 |
270 instantiation String.literal :: card_UNIV begin |
328 instantiation String.literal :: card_UNIV begin |
|
329 definition "finite_UNIV = Phantom(String.literal) False" |
271 definition "card_UNIV = Phantom(String.literal) 0" |
330 definition "card_UNIV = Phantom(String.literal) 0" |
272 instance by intro_classes (simp add: card_UNIV_literal_def card_literal) |
331 instance |
|
332 by intro_classes (simp_all add: card_UNIV_literal_def finite_UNIV_literal_def infinite_literal card_literal) |
|
333 end |
|
334 |
|
335 instantiation set :: (finite_UNIV) finite_UNIV begin |
|
336 definition "finite_UNIV = Phantom('a set) (of_phantom (finite_UNIV :: 'a finite_UNIV))" |
|
337 instance by intro_classes (simp add: finite_UNIV_set_def finite_UNIV Finite_Set.finite_set) |
273 end |
338 end |
274 |
339 |
275 instantiation set :: (card_UNIV) card_UNIV begin |
340 instantiation set :: (card_UNIV) card_UNIV begin |
276 definition "card_UNIV = Phantom('a set) |
341 definition "card_UNIV = Phantom('a set) |
277 (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c = 0 then 0 else 2 ^ c)" |
342 (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c = 0 then 0 else 2 ^ c)" |
278 instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV) |
343 instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV) |
279 end |
344 end |
280 |
345 |
281 |
|
282 lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]" |
346 lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]" |
283 by(auto intro: finite_1.exhaust) |
347 by(auto intro: finite_1.exhaust) |
284 |
348 |
285 lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]" |
349 lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]" |
286 by(auto intro: finite_2.exhaust) |
350 by(auto intro: finite_2.exhaust) |
294 lemma UNIV_finite_5: |
358 lemma UNIV_finite_5: |
295 "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]" |
359 "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]" |
296 by(auto intro: finite_5.exhaust) |
360 by(auto intro: finite_5.exhaust) |
297 |
361 |
298 instantiation Enum.finite_1 :: card_UNIV begin |
362 instantiation Enum.finite_1 :: card_UNIV begin |
|
363 definition "finite_UNIV = Phantom(Enum.finite_1) True" |
299 definition "card_UNIV = Phantom(Enum.finite_1) 1" |
364 definition "card_UNIV = Phantom(Enum.finite_1) 1" |
300 instance by intro_classes (simp add: UNIV_finite_1 card_UNIV_finite_1_def) |
365 instance |
|
366 by intro_classes (simp_all add: UNIV_finite_1 card_UNIV_finite_1_def finite_UNIV_finite_1_def) |
301 end |
367 end |
302 |
368 |
303 instantiation Enum.finite_2 :: card_UNIV begin |
369 instantiation Enum.finite_2 :: card_UNIV begin |
|
370 definition "finite_UNIV = Phantom(Enum.finite_2) True" |
304 definition "card_UNIV = Phantom(Enum.finite_2) 2" |
371 definition "card_UNIV = Phantom(Enum.finite_2) 2" |
305 instance by intro_classes (simp add: UNIV_finite_2 card_UNIV_finite_2_def) |
372 instance |
|
373 by intro_classes (simp_all add: UNIV_finite_2 card_UNIV_finite_2_def finite_UNIV_finite_2_def) |
306 end |
374 end |
307 |
375 |
308 instantiation Enum.finite_3 :: card_UNIV begin |
376 instantiation Enum.finite_3 :: card_UNIV begin |
|
377 definition "finite_UNIV = Phantom(Enum.finite_3) True" |
309 definition "card_UNIV = Phantom(Enum.finite_3) 3" |
378 definition "card_UNIV = Phantom(Enum.finite_3) 3" |
310 instance by intro_classes (simp add: UNIV_finite_3 card_UNIV_finite_3_def) |
379 instance |
|
380 by intro_classes (simp_all add: UNIV_finite_3 card_UNIV_finite_3_def finite_UNIV_finite_3_def) |
311 end |
381 end |
312 |
382 |
313 instantiation Enum.finite_4 :: card_UNIV begin |
383 instantiation Enum.finite_4 :: card_UNIV begin |
|
384 definition "finite_UNIV = Phantom(Enum.finite_4) True" |
314 definition "card_UNIV = Phantom(Enum.finite_4) 4" |
385 definition "card_UNIV = Phantom(Enum.finite_4) 4" |
315 instance by intro_classes (simp add: UNIV_finite_4 card_UNIV_finite_4_def) |
386 instance |
|
387 by intro_classes (simp_all add: UNIV_finite_4 card_UNIV_finite_4_def finite_UNIV_finite_4_def) |
316 end |
388 end |
317 |
389 |
318 instantiation Enum.finite_5 :: card_UNIV begin |
390 instantiation Enum.finite_5 :: card_UNIV begin |
|
391 definition "finite_UNIV = Phantom(Enum.finite_5) True" |
319 definition "card_UNIV = Phantom(Enum.finite_5) 5" |
392 definition "card_UNIV = Phantom(Enum.finite_5) 5" |
320 instance by intro_classes (simp add: UNIV_finite_5 card_UNIV_finite_5_def) |
393 instance |
|
394 by intro_classes (simp_all add: UNIV_finite_5 card_UNIV_finite_5_def finite_UNIV_finite_5_def) |
321 end |
395 end |
322 |
396 |
323 subsection {* Code setup for sets *} |
397 subsection {* Code setup for sets *} |
324 |
398 |
325 lemma card_Compl: |
399 lemma card_Compl: |