migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
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, no_atp]:
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, no_atp]:
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 "acc (set xs) = bacc (set xs) (card_UNIV TYPE('a))"
182 by (simp add: card_UNIV_def acc_bacc_eq)
185 "accp r = (\<lambda>x. x \<in> acc {(x, y). r x y})"
186 by (simp add: acc_def)
189 subsection {* Default instances for @{class enum} *}
191 lemma map_of_zip_enum_is_Some:
192 assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
193 shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
195 from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
196 (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
197 by (auto intro!: map_of_zip_is_Some)
198 then show ?thesis using enum_UNIV by auto
201 lemma map_of_zip_enum_inject:
202 fixes xs ys :: "'b\<Colon>enum list"
203 assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
204 "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
205 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)"
208 have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
211 from length map_of_zip_enum_is_Some obtain y1 y2
212 where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
213 and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
215 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)"
216 by (auto dest: fun_cong)
217 ultimately show "map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x = map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x"
220 with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
223 definition all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
225 "all_n_lists P n \<longleftrightarrow> (\<forall>xs \<in> set (List.n_lists n enum). P xs)"
228 "all_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_all (%x. all_n_lists (%xs. P (x # xs)) (n - 1)))"
229 unfolding all_n_lists_def enum_all
230 by (cases n) (auto simp add: enum_UNIV)
232 definition ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
234 "ex_n_lists P n \<longleftrightarrow> (\<exists>xs \<in> set (List.n_lists n enum). P xs)"
237 "ex_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_ex (%x. ex_n_lists (%xs. P (x # xs)) (n - 1)))"
238 unfolding ex_n_lists_def enum_ex
239 by (cases n) (auto simp add: enum_UNIV)
241 instantiation "fun" :: (enum, enum) enum
245 "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)"
248 "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
251 "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
254 show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
255 proof (rule UNIV_eq_I)
256 fix f :: "'a \<Rightarrow> 'b"
257 have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
258 by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
259 then show "f \<in> set enum"
260 by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
263 from map_of_zip_enum_inject
264 show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
265 by (auto intro!: inj_onI simp add: enum_fun_def
266 distinct_map distinct_n_lists enum_distinct set_n_lists)
269 show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Ball UNIV P"
274 fix f :: "'a \<Rightarrow> 'b"
275 have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
276 by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
277 from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
278 unfolding enum_all_fun_def all_n_lists_def
279 apply (simp add: set_n_lists)
280 apply (erule_tac x="map f enum" in allE)
281 apply (auto intro!: in_enum)
283 from this f show "P f" by auto
287 from this show "enum_all P"
288 unfolding enum_all_fun_def all_n_lists_def by auto
292 show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Bex UNIV P"
295 from this show "Bex UNIV P"
296 unfolding enum_ex_fun_def ex_n_lists_def by auto
299 from this obtain f where "P f" ..
300 have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
301 by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
302 from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
304 from this show "enum_ex P"
305 unfolding enum_ex_fun_def ex_n_lists_def
306 apply (auto simp add: set_n_lists)
307 apply (rule_tac x="map f enum" in exI)
308 apply (auto intro!: in_enum)
315 lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
316 in map (\<lambda>ys. the o map_of (zip enum_a ys)) (List.n_lists (length enum_a) enum))"
317 by (simp add: enum_fun_def Let_def)
319 lemma enum_all_fun_code [code]:
320 "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
321 in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
322 by (simp only: enum_all_fun_def Let_def)
324 lemma enum_ex_fun_code [code]:
325 "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
326 in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
327 by (simp only: enum_ex_fun_def Let_def)
329 instantiation set :: (enum) enum
333 "enum = map set (sublists enum)"
336 "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
339 "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
342 qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
343 enum_distinct enum_UNIV)
347 instantiation unit :: enum
360 qed (auto simp add: enum_unit_def enum_all_unit_def enum_ex_unit_def)
364 instantiation bool :: enum
368 "enum = [False, True]"
371 "enum_all P \<longleftrightarrow> P False \<and> P True"
374 "enum_ex P \<longleftrightarrow> P False \<or> P True"
377 qed (simp_all only: enum_bool_def enum_all_bool_def enum_ex_bool_def UNIV_bool, simp_all)
381 instantiation prod :: (enum, enum) enum
385 "enum = List.product enum enum"
388 "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
391 "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
395 (simp_all add: enum_prod_def product_list_set distinct_product
396 enum_UNIV enum_distinct enum_all_prod_def enum_ex_prod_def)
400 instantiation sum :: (enum, enum) enum
404 "enum = map Inl enum @ map Inr enum"
407 "enum_all P \<longleftrightarrow> enum_all (\<lambda>x. P (Inl x)) \<and> enum_all (\<lambda>x. P (Inr x))"
410 "enum_ex P \<longleftrightarrow> enum_ex (\<lambda>x. P (Inl x)) \<or> enum_ex (\<lambda>x. P (Inr x))"
413 qed (simp_all only: enum_sum_def enum_all_sum_def enum_ex_sum_def UNIV_sum,
414 auto simp add: enum_UNIV distinct_map enum_distinct)
418 instantiation option :: (enum) enum
422 "enum = None # map Some enum"
425 "enum_all P \<longleftrightarrow> P None \<and> enum_all (\<lambda>x. P (Some x))"
428 "enum_ex P \<longleftrightarrow> P None \<or> enum_ex (\<lambda>x. P (Some x))"
431 qed (simp_all only: enum_option_def enum_all_option_def enum_ex_option_def UNIV_option_conv,
432 auto simp add: distinct_map enum_UNIV enum_distinct)
437 subsection {* Small finite types *}
439 text {* We define small finite types for the use in Quickcheck *}
441 datatype finite_1 = a\<^isub>1
443 notation (output) a\<^isub>1 ("a\<^isub>1")
446 "UNIV = {a\<^isub>1}"
447 by (auto intro: finite_1.exhaust)
449 instantiation finite_1 :: enum
453 "enum = [a\<^isub>1]"
456 "enum_all P = P a\<^isub>1"
459 "enum_ex P = P a\<^isub>1"
462 qed (simp_all only: enum_finite_1_def enum_all_finite_1_def enum_ex_finite_1_def UNIV_finite_1, simp_all)
466 instantiation finite_1 :: linorder
469 definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
471 "x < (y :: finite_1) \<longleftrightarrow> False"
473 definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
475 "x \<le> (y :: finite_1) \<longleftrightarrow> True"
478 apply (intro_classes)
479 apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
480 apply (metis finite_1.exhaust)
485 hide_const (open) a\<^isub>1
487 datatype finite_2 = a\<^isub>1 | a\<^isub>2
489 notation (output) a\<^isub>1 ("a\<^isub>1")
490 notation (output) a\<^isub>2 ("a\<^isub>2")
493 "UNIV = {a\<^isub>1, a\<^isub>2}"
494 by (auto intro: finite_2.exhaust)
496 instantiation finite_2 :: enum
500 "enum = [a\<^isub>1, a\<^isub>2]"
503 "enum_all P \<longleftrightarrow> P a\<^isub>1 \<and> P a\<^isub>2"
506 "enum_ex P \<longleftrightarrow> P a\<^isub>1 \<or> P a\<^isub>2"
509 qed (simp_all only: enum_finite_2_def enum_all_finite_2_def enum_ex_finite_2_def UNIV_finite_2, simp_all)
513 instantiation finite_2 :: linorder
516 definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
518 "x < y \<longleftrightarrow> x = a\<^isub>1 \<and> y = a\<^isub>2"
520 definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
522 "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_2)"
525 apply (intro_classes)
526 apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
527 apply (metis finite_2.nchotomy)+
532 hide_const (open) a\<^isub>1 a\<^isub>2
534 datatype finite_3 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3
536 notation (output) a\<^isub>1 ("a\<^isub>1")
537 notation (output) a\<^isub>2 ("a\<^isub>2")
538 notation (output) a\<^isub>3 ("a\<^isub>3")
541 "UNIV = {a\<^isub>1, a\<^isub>2, a\<^isub>3}"
542 by (auto intro: finite_3.exhaust)
544 instantiation finite_3 :: enum
548 "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3]"
551 "enum_all P \<longleftrightarrow> P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3"
554 "enum_ex P \<longleftrightarrow> P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3"
557 qed (simp_all only: enum_finite_3_def enum_all_finite_3_def enum_ex_finite_3_def UNIV_finite_3, simp_all)
561 instantiation finite_3 :: linorder
564 definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
566 "x < y = (case x of a\<^isub>1 \<Rightarrow> y \<noteq> a\<^isub>1 | a\<^isub>2 \<Rightarrow> y = a\<^isub>3 | a\<^isub>3 \<Rightarrow> False)"
568 definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
570 "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_3)"
572 instance proof (intro_classes)
573 qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
577 hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3
579 datatype finite_4 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4
581 notation (output) a\<^isub>1 ("a\<^isub>1")
582 notation (output) a\<^isub>2 ("a\<^isub>2")
583 notation (output) a\<^isub>3 ("a\<^isub>3")
584 notation (output) a\<^isub>4 ("a\<^isub>4")
587 "UNIV = {a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4}"
588 by (auto intro: finite_4.exhaust)
590 instantiation finite_4 :: enum
594 "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4]"
597 "enum_all P \<longleftrightarrow> P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4"
600 "enum_ex P \<longleftrightarrow> P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4"
603 qed (simp_all only: enum_finite_4_def enum_all_finite_4_def enum_ex_finite_4_def UNIV_finite_4, simp_all)
607 hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4
610 datatype finite_5 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4 | a\<^isub>5
612 notation (output) a\<^isub>1 ("a\<^isub>1")
613 notation (output) a\<^isub>2 ("a\<^isub>2")
614 notation (output) a\<^isub>3 ("a\<^isub>3")
615 notation (output) a\<^isub>4 ("a\<^isub>4")
616 notation (output) a\<^isub>5 ("a\<^isub>5")
619 "UNIV = {a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5}"
620 by (auto intro: finite_5.exhaust)
622 instantiation finite_5 :: enum
626 "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5]"
629 "enum_all P \<longleftrightarrow> P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4 \<and> P a\<^isub>5"
632 "enum_ex P \<longleftrightarrow> P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4 \<or> P a\<^isub>5"
635 qed (simp_all only: enum_finite_5_def enum_all_finite_5_def enum_ex_finite_5_def UNIV_finite_5, simp_all)
639 hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4 a\<^isub>5
642 subsection {* Closing up *}
644 hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
645 hide_const (open) enum enum_all enum_ex all_n_lists ex_n_lists ntrancl