1 (* Author: Florian Haftmann, TU Muenchen *)
3 header {* Finite types as explicit enumerations *}
9 subsection {* Class @{text enum} *}
12 fixes enum :: "'a list"
13 fixes enum_all :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
14 fixes enum_ex :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
15 assumes UNIV_enum: "UNIV = set enum"
16 and enum_distinct: "distinct enum"
17 assumes enum_all_UNIV: "enum_all P \<longleftrightarrow> Ball UNIV P"
18 assumes enum_ex_UNIV: "enum_ex P \<longleftrightarrow> Bex UNIV P"
19 -- {* tailored towards simple instantiation *}
23 qed (simp add: UNIV_enum)
27 by (simp only: UNIV_enum)
29 lemma in_enum: "x \<in> set enum"
30 by (simp add: enum_UNIV)
33 assumes "\<And>x. x \<in> set xs"
34 shows "set enum = set xs"
36 from assms UNIV_eq_I have "UNIV = set xs" by auto
37 with enum_UNIV show ?thesis by simp
40 lemma card_UNIV_length_enum:
41 "card (UNIV :: 'a set) = length enum"
42 by (simp add: UNIV_enum distinct_card enum_distinct)
44 lemma enum_all [simp]:
46 by (simp add: fun_eq_iff enum_all_UNIV)
50 by (simp add: fun_eq_iff enum_ex_UNIV)
55 subsection {* Implementations using @{class enum} *}
57 subsubsection {* Unbounded operations and quantifiers *}
59 lemma Collect_code [code]:
60 "Collect P = set (filter P enum)"
61 by (simp add: enum_UNIV)
63 lemma vimage_code [code]:
64 "f -` B = set (filter (%x. f x : B) enum_class.enum)"
65 unfolding vimage_def Collect_code ..
67 definition card_UNIV :: "'a itself \<Rightarrow> nat"
69 [code del]: "card_UNIV TYPE('a) = card (UNIV :: 'a set)"
72 "card_UNIV TYPE('a :: enum) = card (set (Enum.enum :: 'a list))"
73 by (simp only: card_UNIV_def enum_UNIV)
75 lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> enum_all P"
78 lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> enum_ex P"
81 lemma exists1_code [code]: "(\<exists>!x. P x) \<longleftrightarrow> list_ex1 P enum"
82 by (auto simp add: list_ex1_iff enum_UNIV)
85 subsubsection {* An executable choice operator *}
88 [code del]: "enum_the = The"
91 "The P = (case filter P enum of [x] => x | _ => enum_the P)"
95 assume filter_enum: "filter P enum = [a]"
97 proof (rule the_equality)
102 assume "x \<noteq> a"
103 from filter_enum obtain us vs
104 where enum_eq: "enum = us @ [a] @ vs"
105 and "\<forall> x \<in> set us. \<not> P x"
106 and "\<forall> x \<in> set vs. \<not> P x"
108 by (auto simp add: filter_eq_Cons_iff) (simp only: filter_empty_conv[symmetric])
109 with `P x` in_enum[of x, unfolded enum_eq] `x \<noteq> a` show "False" by auto
112 from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
115 from this show ?thesis
116 unfolding enum_the_def by (auto split: list.split)
122 constant enum_the \<rightharpoonup> (Eval) "(fn '_ => raise Match)"
125 subsubsection {* Equality and order on functions *}
127 instantiation "fun" :: (enum, equal) equal
131 "HOL.equal f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
134 qed (simp_all add: equal_fun_def fun_eq_iff enum_UNIV)
139 "HOL.equal f g \<longleftrightarrow> enum_all (%x. f x = g x)"
140 by (auto simp add: equal fun_eq_iff)
143 "HOL.equal (f :: _ \<Rightarrow> _) f \<longleftrightarrow> True"
146 lemma order_fun [code]:
147 fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
148 shows "f \<le> g \<longleftrightarrow> enum_all (\<lambda>x. f x \<le> g x)"
149 and "f < g \<longleftrightarrow> f \<le> g \<and> enum_ex (\<lambda>x. f x \<noteq> g x)"
150 by (simp_all add: fun_eq_iff le_fun_def order_less_le)
153 subsubsection {* Operations on relations *}
156 "Id = image (\<lambda>x. (x, x)) (set Enum.enum)"
157 by (auto intro: imageI in_enum)
159 lemma tranclp_unfold [code]:
160 "tranclp r a b \<longleftrightarrow> (a, b) \<in> trancl {(x, y). r x y}"
161 by (simp add: trancl_def)
163 lemma rtranclp_rtrancl_eq [code]:
164 "rtranclp r x y \<longleftrightarrow> (x, y) \<in> rtrancl {(x, y). r x y}"
165 by (simp add: rtrancl_def)
167 lemma max_ext_eq [code]:
168 "max_ext R = {(X, Y). finite X \<and> finite Y \<and> Y \<noteq> {} \<and> (\<forall>x. x \<in> X \<longrightarrow> (\<exists>xa \<in> Y. (x, xa) \<in> R))}"
169 by (auto simp add: max_ext.simps)
171 lemma max_extp_eq [code]:
172 "max_extp r x y \<longleftrightarrow> (x, y) \<in> max_ext {(x, y). r x y}"
173 by (simp add: max_ext_def)
175 lemma mlex_eq [code]:
176 "f <*mlex*> R = {(x, y). f x < f y \<or> (f x \<le> f y \<and> (x, y) \<in> R)}"
177 by (auto simp add: mlex_prod_def)
180 fixes xs :: "('a::finite \<times> 'a) list"
181 shows "Wellfounded.acc (set xs) = bacc (set xs) (card_UNIV TYPE('a))"
182 by (simp add: card_UNIV_def acc_bacc_eq)
185 subsection {* Default instances for @{class enum} *}
187 lemma map_of_zip_enum_is_Some:
188 assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
189 shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
191 from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
192 (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
193 by (auto intro!: map_of_zip_is_Some)
194 then show ?thesis using enum_UNIV by auto
197 lemma map_of_zip_enum_inject:
198 fixes xs ys :: "'b\<Colon>enum list"
199 assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
200 "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
201 and map_of: "the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys)"
204 have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
207 from length map_of_zip_enum_is_Some obtain y1 y2
208 where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
209 and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
211 have "the (map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x) = the (map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x)"
212 by (auto dest: fun_cong)
213 ultimately show "map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x = map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x"
216 with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
219 definition all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
221 "all_n_lists P n \<longleftrightarrow> (\<forall>xs \<in> set (List.n_lists n enum). P xs)"
224 "all_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_all (%x. all_n_lists (%xs. P (x # xs)) (n - 1)))"
225 unfolding all_n_lists_def enum_all
226 by (cases n) (auto simp add: enum_UNIV)
228 definition ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
230 "ex_n_lists P n \<longleftrightarrow> (\<exists>xs \<in> set (List.n_lists n enum). P xs)"
233 "ex_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_ex (%x. ex_n_lists (%xs. P (x # xs)) (n - 1)))"
234 unfolding ex_n_lists_def enum_ex
235 by (cases n) (auto simp add: enum_UNIV)
237 instantiation "fun" :: (enum, enum) enum
241 "enum = map (\<lambda>ys. the o map_of (zip (enum\<Colon>'a list) ys)) (List.n_lists (length (enum\<Colon>'a\<Colon>enum list)) enum)"
244 "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
247 "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
250 show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
251 proof (rule UNIV_eq_I)
252 fix f :: "'a \<Rightarrow> 'b"
253 have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
254 by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
255 then show "f \<in> set enum"
256 by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
259 from map_of_zip_enum_inject
260 show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
261 by (auto intro!: inj_onI simp add: enum_fun_def
262 distinct_map distinct_n_lists enum_distinct set_n_lists)
265 show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Ball UNIV P"
270 fix f :: "'a \<Rightarrow> 'b"
271 have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
272 by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
273 from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
274 unfolding enum_all_fun_def all_n_lists_def
275 apply (simp add: set_n_lists)
276 apply (erule_tac x="map f enum" in allE)
277 apply (auto intro!: in_enum)
279 from this f show "P f" by auto
283 from this show "enum_all P"
284 unfolding enum_all_fun_def all_n_lists_def by auto
288 show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Bex UNIV P"
291 from this show "Bex UNIV P"
292 unfolding enum_ex_fun_def ex_n_lists_def by auto
295 from this obtain f where "P f" ..
296 have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
297 by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
298 from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
300 from this show "enum_ex P"
301 unfolding enum_ex_fun_def ex_n_lists_def
302 apply (auto simp add: set_n_lists)
303 apply (rule_tac x="map f enum" in exI)
304 apply (auto intro!: in_enum)
311 lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
312 in map (\<lambda>ys. the o map_of (zip enum_a ys)) (List.n_lists (length enum_a) enum))"
313 by (simp add: enum_fun_def Let_def)
315 lemma enum_all_fun_code [code]:
316 "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
317 in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
318 by (simp only: enum_all_fun_def Let_def)
320 lemma enum_ex_fun_code [code]:
321 "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
322 in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
323 by (simp only: enum_ex_fun_def Let_def)
325 instantiation set :: (enum) enum
329 "enum = map set (sublists enum)"
332 "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
335 "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
338 qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
339 enum_distinct enum_UNIV)
343 instantiation unit :: enum
356 qed (auto simp add: enum_unit_def enum_all_unit_def enum_ex_unit_def)
360 instantiation bool :: enum
364 "enum = [False, True]"
367 "enum_all P \<longleftrightarrow> P False \<and> P True"
370 "enum_ex P \<longleftrightarrow> P False \<or> P True"
373 qed (simp_all only: enum_bool_def enum_all_bool_def enum_ex_bool_def UNIV_bool, simp_all)
377 instantiation prod :: (enum, enum) enum
381 "enum = List.product enum enum"
384 "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
387 "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
391 (simp_all add: enum_prod_def product_list_set distinct_product
392 enum_UNIV enum_distinct enum_all_prod_def enum_ex_prod_def)
396 instantiation sum :: (enum, enum) enum
400 "enum = map Inl enum @ map Inr enum"
403 "enum_all P \<longleftrightarrow> enum_all (\<lambda>x. P (Inl x)) \<and> enum_all (\<lambda>x. P (Inr x))"
406 "enum_ex P \<longleftrightarrow> enum_ex (\<lambda>x. P (Inl x)) \<or> enum_ex (\<lambda>x. P (Inr x))"
409 qed (simp_all only: enum_sum_def enum_all_sum_def enum_ex_sum_def UNIV_sum,
410 auto simp add: enum_UNIV distinct_map enum_distinct)
414 instantiation option :: (enum) enum
418 "enum = None # map Some enum"
421 "enum_all P \<longleftrightarrow> P None \<and> enum_all (\<lambda>x. P (Some x))"
424 "enum_ex P \<longleftrightarrow> P None \<or> enum_ex (\<lambda>x. P (Some x))"
427 qed (simp_all only: enum_option_def enum_all_option_def enum_ex_option_def UNIV_option_conv,
428 auto simp add: distinct_map enum_UNIV enum_distinct)
433 subsection {* Small finite types *}
435 text {* We define small finite types for the use in Quickcheck *}
437 datatype finite_1 = a\<^sub>1
439 notation (output) a\<^sub>1 ("a\<^sub>1")
443 by (auto intro: finite_1.exhaust)
445 instantiation finite_1 :: enum
452 "enum_all P = P a\<^sub>1"
455 "enum_ex P = P a\<^sub>1"
458 qed (simp_all only: enum_finite_1_def enum_all_finite_1_def enum_ex_finite_1_def UNIV_finite_1, simp_all)
462 instantiation finite_1 :: linorder
465 definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
467 "x < (y :: finite_1) \<longleftrightarrow> False"
469 definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
471 "x \<le> (y :: finite_1) \<longleftrightarrow> True"
474 apply (intro_classes)
475 apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
476 apply (metis finite_1.exhaust)
481 hide_const (open) a\<^sub>1
483 datatype finite_2 = a\<^sub>1 | a\<^sub>2
485 notation (output) a\<^sub>1 ("a\<^sub>1")
486 notation (output) a\<^sub>2 ("a\<^sub>2")
489 "UNIV = {a\<^sub>1, a\<^sub>2}"
490 by (auto intro: finite_2.exhaust)
492 instantiation finite_2 :: enum
496 "enum = [a\<^sub>1, a\<^sub>2]"
499 "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2"
502 "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2"
505 qed (simp_all only: enum_finite_2_def enum_all_finite_2_def enum_ex_finite_2_def UNIV_finite_2, simp_all)
509 instantiation finite_2 :: linorder
512 definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
514 "x < y \<longleftrightarrow> x = a\<^sub>1 \<and> y = a\<^sub>2"
516 definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
518 "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_2)"
521 apply (intro_classes)
522 apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
523 apply (metis finite_2.nchotomy)+
528 hide_const (open) a\<^sub>1 a\<^sub>2
530 datatype finite_3 = a\<^sub>1 | a\<^sub>2 | a\<^sub>3
532 notation (output) a\<^sub>1 ("a\<^sub>1")
533 notation (output) a\<^sub>2 ("a\<^sub>2")
534 notation (output) a\<^sub>3 ("a\<^sub>3")
537 "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3}"
538 by (auto intro: finite_3.exhaust)
540 instantiation finite_3 :: enum
544 "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3]"
547 "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3"
550 "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3"
553 qed (simp_all only: enum_finite_3_def enum_all_finite_3_def enum_ex_finite_3_def UNIV_finite_3, simp_all)
557 instantiation finite_3 :: linorder
560 definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
562 "x < y = (case x of a\<^sub>1 \<Rightarrow> y \<noteq> a\<^sub>1 | a\<^sub>2 \<Rightarrow> y = a\<^sub>3 | a\<^sub>3 \<Rightarrow> False)"
564 definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
566 "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_3)"
568 instance proof (intro_classes)
569 qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
573 hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3
575 datatype finite_4 = a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4
577 notation (output) a\<^sub>1 ("a\<^sub>1")
578 notation (output) a\<^sub>2 ("a\<^sub>2")
579 notation (output) a\<^sub>3 ("a\<^sub>3")
580 notation (output) a\<^sub>4 ("a\<^sub>4")
583 "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4}"
584 by (auto intro: finite_4.exhaust)
586 instantiation finite_4 :: enum
590 "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4]"
593 "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3 \<and> P a\<^sub>4"
596 "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3 \<or> P a\<^sub>4"
599 qed (simp_all only: enum_finite_4_def enum_all_finite_4_def enum_ex_finite_4_def UNIV_finite_4, simp_all)
603 hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4
606 datatype finite_5 = a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4 | a\<^sub>5
608 notation (output) a\<^sub>1 ("a\<^sub>1")
609 notation (output) a\<^sub>2 ("a\<^sub>2")
610 notation (output) a\<^sub>3 ("a\<^sub>3")
611 notation (output) a\<^sub>4 ("a\<^sub>4")
612 notation (output) a\<^sub>5 ("a\<^sub>5")
615 "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5}"
616 by (auto intro: finite_5.exhaust)
618 instantiation finite_5 :: enum
622 "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5]"
625 "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3 \<and> P a\<^sub>4 \<and> P a\<^sub>5"
628 "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3 \<or> P a\<^sub>4 \<or> P a\<^sub>5"
631 qed (simp_all only: enum_finite_5_def enum_all_finite_5_def enum_ex_finite_5_def UNIV_finite_5, simp_all)
635 hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4 a\<^sub>5
638 subsection {* Closing up *}
640 hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
641 hide_const (open) enum enum_all enum_ex all_n_lists ex_n_lists ntrancl