src/HOL/Enum.thy
 author nipkow Thu Jun 12 18:47:16 2014 +0200 (2014-06-12) changeset 57247 8191ccf6a1bd parent 55088 57c82e01022b child 57818 51aa30c9ee4e permissions -rw-r--r--
added [simp]
```     1 (* Author: Florian Haftmann, TU Muenchen *)
```
```     2
```
```     3 header {* Finite types as explicit enumerations *}
```
```     4
```
```     5 theory Enum
```
```     6 imports Map
```
```     7 begin
```
```     8
```
```     9 subsection {* Class @{text enum} *}
```
```    10
```
```    11 class 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 *}
```
```    20 begin
```
```    21
```
```    22 subclass finite proof
```
```    23 qed (simp add: UNIV_enum)
```
```    24
```
```    25 lemma enum_UNIV:
```
```    26   "set enum = UNIV"
```
```    27   by (simp only: UNIV_enum)
```
```    28
```
```    29 lemma in_enum: "x \<in> set enum"
```
```    30   by (simp add: enum_UNIV)
```
```    31
```
```    32 lemma enum_eq_I:
```
```    33   assumes "\<And>x. x \<in> set xs"
```
```    34   shows "set enum = set xs"
```
```    35 proof -
```
```    36   from assms UNIV_eq_I have "UNIV = set xs" by auto
```
```    37   with enum_UNIV show ?thesis by simp
```
```    38 qed
```
```    39
```
```    40 lemma card_UNIV_length_enum:
```
```    41   "card (UNIV :: 'a set) = length enum"
```
```    42   by (simp add: UNIV_enum distinct_card enum_distinct)
```
```    43
```
```    44 lemma enum_all [simp]:
```
```    45   "enum_all = HOL.All"
```
```    46   by (simp add: fun_eq_iff enum_all_UNIV)
```
```    47
```
```    48 lemma enum_ex [simp]:
```
```    49   "enum_ex = HOL.Ex"
```
```    50   by (simp add: fun_eq_iff enum_ex_UNIV)
```
```    51
```
```    52 end
```
```    53
```
```    54
```
```    55 subsection {* Implementations using @{class enum} *}
```
```    56
```
```    57 subsubsection {* Unbounded operations and quantifiers *}
```
```    58
```
```    59 lemma Collect_code [code]:
```
```    60   "Collect P = set (filter P enum)"
```
```    61   by (simp add: enum_UNIV)
```
```    62
```
```    63 lemma vimage_code [code]:
```
```    64   "f -` B = set (filter (%x. f x : B) enum_class.enum)"
```
```    65   unfolding vimage_def Collect_code ..
```
```    66
```
```    67 definition card_UNIV :: "'a itself \<Rightarrow> nat"
```
```    68 where
```
```    69   [code del]: "card_UNIV TYPE('a) = card (UNIV :: 'a set)"
```
```    70
```
```    71 lemma [code]:
```
```    72   "card_UNIV TYPE('a :: enum) = card (set (Enum.enum :: 'a list))"
```
```    73   by (simp only: card_UNIV_def enum_UNIV)
```
```    74
```
```    75 lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> enum_all P"
```
```    76   by simp
```
```    77
```
```    78 lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> enum_ex P"
```
```    79   by simp
```
```    80
```
```    81 lemma exists1_code [code]: "(\<exists>!x. P x) \<longleftrightarrow> list_ex1 P enum"
```
```    82   by (auto simp add: list_ex1_iff enum_UNIV)
```
```    83
```
```    84
```
```    85 subsubsection {* An executable choice operator *}
```
```    86
```
```    87 definition
```
```    88   [code del]: "enum_the = The"
```
```    89
```
```    90 lemma [code]:
```
```    91   "The P = (case filter P enum of [x] => x | _ => enum_the P)"
```
```    92 proof -
```
```    93   {
```
```    94     fix a
```
```    95     assume filter_enum: "filter P enum = [a]"
```
```    96     have "The P = a"
```
```    97     proof (rule the_equality)
```
```    98       fix x
```
```    99       assume "P x"
```
```   100       show "x = a"
```
```   101       proof (rule ccontr)
```
```   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"
```
```   107           and "P a"
```
```   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
```
```   110       qed
```
```   111     next
```
```   112       from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
```
```   113     qed
```
```   114   }
```
```   115   from this show ?thesis
```
```   116     unfolding enum_the_def by (auto split: list.split)
```
```   117 qed
```
```   118
```
```   119 declare [[code abort: enum_the]]
```
```   120
```
```   121 code_printing
```
```   122   constant enum_the \<rightharpoonup> (Eval) "(fn '_ => raise Match)"
```
```   123
```
```   124
```
```   125 subsubsection {* Equality and order on functions *}
```
```   126
```
```   127 instantiation "fun" :: (enum, equal) equal
```
```   128 begin
```
```   129
```
```   130 definition
```
```   131   "HOL.equal f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
```
```   132
```
```   133 instance proof
```
```   134 qed (simp_all add: equal_fun_def fun_eq_iff enum_UNIV)
```
```   135
```
```   136 end
```
```   137
```
```   138 lemma [code]:
```
```   139   "HOL.equal f g \<longleftrightarrow> enum_all (%x. f x = g x)"
```
```   140   by (auto simp add: equal fun_eq_iff)
```
```   141
```
```   142 lemma [code nbe]:
```
```   143   "HOL.equal (f :: _ \<Rightarrow> _) f \<longleftrightarrow> True"
```
```   144   by (fact equal_refl)
```
```   145
```
```   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)
```
```   151
```
```   152
```
```   153 subsubsection {* Operations on relations *}
```
```   154
```
```   155 lemma [code]:
```
```   156   "Id = image (\<lambda>x. (x, x)) (set Enum.enum)"
```
```   157   by (auto intro: imageI in_enum)
```
```   158
```
```   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)
```
```   162
```
```   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)
```
```   166
```
```   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)
```
```   170
```
```   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)
```
```   174
```
```   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)
```
```   178
```
```   179
```
```   180 subsubsection {* Bounded accessible part *}
```
```   181
```
```   182 primrec bacc :: "('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> 'a set"
```
```   183 where
```
```   184   "bacc r 0 = {x. \<forall> y. (y, x) \<notin> r}"
```
```   185 | "bacc r (Suc n) = (bacc r n \<union> {x. \<forall>y. (y, x) \<in> r \<longrightarrow> y \<in> bacc r n})"
```
```   186
```
```   187 lemma bacc_subseteq_acc:
```
```   188   "bacc r n \<subseteq> Wellfounded.acc r"
```
```   189   by (induct n) (auto intro: acc.intros)
```
```   190
```
```   191 lemma bacc_mono:
```
```   192   "n \<le> m \<Longrightarrow> bacc r n \<subseteq> bacc r m"
```
```   193   by (induct rule: dec_induct) auto
```
```   194
```
```   195 lemma bacc_upper_bound:
```
```   196   "bacc (r :: ('a \<times> 'a) set)  (card (UNIV :: 'a::finite set)) = (\<Union>n. bacc r n)"
```
```   197 proof -
```
```   198   have "mono (bacc r)" unfolding mono_def by (simp add: bacc_mono)
```
```   199   moreover have "\<forall>n. bacc r n = bacc r (Suc n) \<longrightarrow> bacc r (Suc n) = bacc r (Suc (Suc n))" by auto
```
```   200   moreover have "finite (range (bacc r))" by auto
```
```   201   ultimately show ?thesis
```
```   202    by (intro finite_mono_strict_prefix_implies_finite_fixpoint)
```
```   203      (auto intro: finite_mono_remains_stable_implies_strict_prefix)
```
```   204 qed
```
```   205
```
```   206 lemma acc_subseteq_bacc:
```
```   207   assumes "finite r"
```
```   208   shows "Wellfounded.acc r \<subseteq> (\<Union>n. bacc r n)"
```
```   209 proof
```
```   210   fix x
```
```   211   assume "x : Wellfounded.acc r"
```
```   212   then have "\<exists> n. x : bacc r n"
```
```   213   proof (induct x arbitrary: rule: acc.induct)
```
```   214     case (accI x)
```
```   215     then have "\<forall>y. \<exists> n. (y, x) \<in> r --> y : bacc r n" by simp
```
```   216     from choice[OF this] obtain n where n: "\<forall>y. (y, x) \<in> r \<longrightarrow> y \<in> bacc r (n y)" ..
```
```   217     obtain n where "\<And>y. (y, x) : r \<Longrightarrow> y : bacc r n"
```
```   218     proof
```
```   219       fix y assume y: "(y, x) : r"
```
```   220       with n have "y : bacc r (n y)" by auto
```
```   221       moreover have "n y <= Max ((%(y, x). n y) ` r)"
```
```   222         using y `finite r` by (auto intro!: Max_ge)
```
```   223       note bacc_mono[OF this, of r]
```
```   224       ultimately show "y : bacc r (Max ((%(y, x). n y) ` r))" by auto
```
```   225     qed
```
```   226     then show ?case
```
```   227       by (auto simp add: Let_def intro!: exI[of _ "Suc n"])
```
```   228   qed
```
```   229   then show "x : (UN n. bacc r n)" by auto
```
```   230 qed
```
```   231
```
```   232 lemma acc_bacc_eq:
```
```   233   fixes A :: "('a :: finite \<times> 'a) set"
```
```   234   assumes "finite A"
```
```   235   shows "Wellfounded.acc A = bacc A (card (UNIV :: 'a set))"
```
```   236   using assms by (metis acc_subseteq_bacc bacc_subseteq_acc bacc_upper_bound order_eq_iff)
```
```   237
```
```   238 lemma [code]:
```
```   239   fixes xs :: "('a::finite \<times> 'a) list"
```
```   240   shows "Wellfounded.acc (set xs) = bacc (set xs) (card_UNIV TYPE('a))"
```
```   241   by (simp add: card_UNIV_def acc_bacc_eq)
```
```   242
```
```   243
```
```   244 subsection {* Default instances for @{class enum} *}
```
```   245
```
```   246 lemma map_of_zip_enum_is_Some:
```
```   247   assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
```
```   248   shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
```
```   249 proof -
```
```   250   from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
```
```   251     (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
```
```   252     by (auto intro!: map_of_zip_is_Some)
```
```   253   then show ?thesis using enum_UNIV by auto
```
```   254 qed
```
```   255
```
```   256 lemma map_of_zip_enum_inject:
```
```   257   fixes xs ys :: "'b\<Colon>enum list"
```
```   258   assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
```
```   259       "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
```
```   260     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)"
```
```   261   shows "xs = ys"
```
```   262 proof -
```
```   263   have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
```
```   264   proof
```
```   265     fix x :: 'a
```
```   266     from length map_of_zip_enum_is_Some obtain y1 y2
```
```   267       where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
```
```   268         and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
```
```   269     moreover from map_of
```
```   270       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)"
```
```   271       by (auto dest: fun_cong)
```
```   272     ultimately show "map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x = map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x"
```
```   273       by simp
```
```   274   qed
```
```   275   with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
```
```   276 qed
```
```   277
```
```   278 definition all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
```
```   279 where
```
```   280   "all_n_lists P n \<longleftrightarrow> (\<forall>xs \<in> set (List.n_lists n enum). P xs)"
```
```   281
```
```   282 lemma [code]:
```
```   283   "all_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_all (%x. all_n_lists (%xs. P (x # xs)) (n - 1)))"
```
```   284   unfolding all_n_lists_def enum_all
```
```   285   by (cases n) (auto simp add: enum_UNIV)
```
```   286
```
```   287 definition ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
```
```   288 where
```
```   289   "ex_n_lists P n \<longleftrightarrow> (\<exists>xs \<in> set (List.n_lists n enum). P xs)"
```
```   290
```
```   291 lemma [code]:
```
```   292   "ex_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_ex (%x. ex_n_lists (%xs. P (x # xs)) (n - 1)))"
```
```   293   unfolding ex_n_lists_def enum_ex
```
```   294   by (cases n) (auto simp add: enum_UNIV)
```
```   295
```
```   296 instantiation "fun" :: (enum, enum) enum
```
```   297 begin
```
```   298
```
```   299 definition
```
```   300   "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)"
```
```   301
```
```   302 definition
```
```   303   "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
```
```   304
```
```   305 definition
```
```   306   "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
```
```   307
```
```   308 instance proof
```
```   309   show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
```
```   310   proof (rule UNIV_eq_I)
```
```   311     fix f :: "'a \<Rightarrow> 'b"
```
```   312     have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
```
```   313       by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
```
```   314     then show "f \<in> set enum"
```
```   315       by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
```
```   316   qed
```
```   317 next
```
```   318   from map_of_zip_enum_inject
```
```   319   show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
```
```   320     by (auto intro!: inj_onI simp add: enum_fun_def
```
```   321       distinct_map distinct_n_lists enum_distinct set_n_lists)
```
```   322 next
```
```   323   fix P
```
```   324   show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Ball UNIV P"
```
```   325   proof
```
```   326     assume "enum_all P"
```
```   327     show "Ball UNIV P"
```
```   328     proof
```
```   329       fix f :: "'a \<Rightarrow> 'b"
```
```   330       have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
```
```   331         by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
```
```   332       from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
```
```   333         unfolding enum_all_fun_def all_n_lists_def
```
```   334         apply (simp add: set_n_lists)
```
```   335         apply (erule_tac x="map f enum" in allE)
```
```   336         apply (auto intro!: in_enum)
```
```   337         done
```
```   338       from this f show "P f" by auto
```
```   339     qed
```
```   340   next
```
```   341     assume "Ball UNIV P"
```
```   342     from this show "enum_all P"
```
```   343       unfolding enum_all_fun_def all_n_lists_def by auto
```
```   344   qed
```
```   345 next
```
```   346   fix P
```
```   347   show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Bex UNIV P"
```
```   348   proof
```
```   349     assume "enum_ex P"
```
```   350     from this show "Bex UNIV P"
```
```   351       unfolding enum_ex_fun_def ex_n_lists_def by auto
```
```   352   next
```
```   353     assume "Bex UNIV P"
```
```   354     from this obtain f where "P f" ..
```
```   355     have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
```
```   356       by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
```
```   357     from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
```
```   358       by auto
```
```   359     from  this show "enum_ex P"
```
```   360       unfolding enum_ex_fun_def ex_n_lists_def
```
```   361       apply (auto simp add: set_n_lists)
```
```   362       apply (rule_tac x="map f enum" in exI)
```
```   363       apply (auto intro!: in_enum)
```
```   364       done
```
```   365   qed
```
```   366 qed
```
```   367
```
```   368 end
```
```   369
```
```   370 lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
```
```   371   in map (\<lambda>ys. the o map_of (zip enum_a ys)) (List.n_lists (length enum_a) enum))"
```
```   372   by (simp add: enum_fun_def Let_def)
```
```   373
```
```   374 lemma enum_all_fun_code [code]:
```
```   375   "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
```
```   376    in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
```
```   377   by (simp only: enum_all_fun_def Let_def)
```
```   378
```
```   379 lemma enum_ex_fun_code [code]:
```
```   380   "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
```
```   381    in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
```
```   382   by (simp only: enum_ex_fun_def Let_def)
```
```   383
```
```   384 instantiation set :: (enum) enum
```
```   385 begin
```
```   386
```
```   387 definition
```
```   388   "enum = map set (sublists enum)"
```
```   389
```
```   390 definition
```
```   391   "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
```
```   392
```
```   393 definition
```
```   394   "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
```
```   395
```
```   396 instance proof
```
```   397 qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
```
```   398   enum_distinct enum_UNIV)
```
```   399
```
```   400 end
```
```   401
```
```   402 instantiation unit :: enum
```
```   403 begin
```
```   404
```
```   405 definition
```
```   406   "enum = [()]"
```
```   407
```
```   408 definition
```
```   409   "enum_all P = P ()"
```
```   410
```
```   411 definition
```
```   412   "enum_ex P = P ()"
```
```   413
```
```   414 instance proof
```
```   415 qed (auto simp add: enum_unit_def enum_all_unit_def enum_ex_unit_def)
```
```   416
```
```   417 end
```
```   418
```
```   419 instantiation bool :: enum
```
```   420 begin
```
```   421
```
```   422 definition
```
```   423   "enum = [False, True]"
```
```   424
```
```   425 definition
```
```   426   "enum_all P \<longleftrightarrow> P False \<and> P True"
```
```   427
```
```   428 definition
```
```   429   "enum_ex P \<longleftrightarrow> P False \<or> P True"
```
```   430
```
```   431 instance proof
```
```   432 qed (simp_all only: enum_bool_def enum_all_bool_def enum_ex_bool_def UNIV_bool, simp_all)
```
```   433
```
```   434 end
```
```   435
```
```   436 instantiation prod :: (enum, enum) enum
```
```   437 begin
```
```   438
```
```   439 definition
```
```   440   "enum = List.product enum enum"
```
```   441
```
```   442 definition
```
```   443   "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
```
```   444
```
```   445 definition
```
```   446   "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
```
```   447
```
```   448
```
```   449 instance by default
```
```   450   (simp_all add: enum_prod_def distinct_product
```
```   451     enum_UNIV enum_distinct enum_all_prod_def enum_ex_prod_def)
```
```   452
```
```   453 end
```
```   454
```
```   455 instantiation sum :: (enum, enum) enum
```
```   456 begin
```
```   457
```
```   458 definition
```
```   459   "enum = map Inl enum @ map Inr enum"
```
```   460
```
```   461 definition
```
```   462   "enum_all P \<longleftrightarrow> enum_all (\<lambda>x. P (Inl x)) \<and> enum_all (\<lambda>x. P (Inr x))"
```
```   463
```
```   464 definition
```
```   465   "enum_ex P \<longleftrightarrow> enum_ex (\<lambda>x. P (Inl x)) \<or> enum_ex (\<lambda>x. P (Inr x))"
```
```   466
```
```   467 instance proof
```
```   468 qed (simp_all only: enum_sum_def enum_all_sum_def enum_ex_sum_def UNIV_sum,
```
```   469   auto simp add: enum_UNIV distinct_map enum_distinct)
```
```   470
```
```   471 end
```
```   472
```
```   473 instantiation option :: (enum) enum
```
```   474 begin
```
```   475
```
```   476 definition
```
```   477   "enum = None # map Some enum"
```
```   478
```
```   479 definition
```
```   480   "enum_all P \<longleftrightarrow> P None \<and> enum_all (\<lambda>x. P (Some x))"
```
```   481
```
```   482 definition
```
```   483   "enum_ex P \<longleftrightarrow> P None \<or> enum_ex (\<lambda>x. P (Some x))"
```
```   484
```
```   485 instance proof
```
```   486 qed (simp_all only: enum_option_def enum_all_option_def enum_ex_option_def UNIV_option_conv,
```
```   487   auto simp add: distinct_map enum_UNIV enum_distinct)
```
```   488
```
```   489 end
```
```   490
```
```   491
```
```   492 subsection {* Small finite types *}
```
```   493
```
```   494 text {* We define small finite types for the use in Quickcheck *}
```
```   495
```
```   496 datatype finite_1 = a\<^sub>1
```
```   497
```
```   498 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   499
```
```   500 lemma UNIV_finite_1:
```
```   501   "UNIV = {a\<^sub>1}"
```
```   502   by (auto intro: finite_1.exhaust)
```
```   503
```
```   504 instantiation finite_1 :: enum
```
```   505 begin
```
```   506
```
```   507 definition
```
```   508   "enum = [a\<^sub>1]"
```
```   509
```
```   510 definition
```
```   511   "enum_all P = P a\<^sub>1"
```
```   512
```
```   513 definition
```
```   514   "enum_ex P = P a\<^sub>1"
```
```   515
```
```   516 instance proof
```
```   517 qed (simp_all only: enum_finite_1_def enum_all_finite_1_def enum_ex_finite_1_def UNIV_finite_1, simp_all)
```
```   518
```
```   519 end
```
```   520
```
```   521 instantiation finite_1 :: linorder
```
```   522 begin
```
```   523
```
```   524 definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
```
```   525 where
```
```   526   "x < (y :: finite_1) \<longleftrightarrow> False"
```
```   527
```
```   528 definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
```
```   529 where
```
```   530   "x \<le> (y :: finite_1) \<longleftrightarrow> True"
```
```   531
```
```   532 instance
```
```   533 apply (intro_classes)
```
```   534 apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
```
```   535 apply (metis finite_1.exhaust)
```
```   536 done
```
```   537
```
```   538 end
```
```   539
```
```   540 hide_const (open) a\<^sub>1
```
```   541
```
```   542 datatype finite_2 = a\<^sub>1 | a\<^sub>2
```
```   543
```
```   544 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   545 notation (output) a\<^sub>2  ("a\<^sub>2")
```
```   546
```
```   547 lemma UNIV_finite_2:
```
```   548   "UNIV = {a\<^sub>1, a\<^sub>2}"
```
```   549   by (auto intro: finite_2.exhaust)
```
```   550
```
```   551 instantiation finite_2 :: enum
```
```   552 begin
```
```   553
```
```   554 definition
```
```   555   "enum = [a\<^sub>1, a\<^sub>2]"
```
```   556
```
```   557 definition
```
```   558   "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2"
```
```   559
```
```   560 definition
```
```   561   "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2"
```
```   562
```
```   563 instance proof
```
```   564 qed (simp_all only: enum_finite_2_def enum_all_finite_2_def enum_ex_finite_2_def UNIV_finite_2, simp_all)
```
```   565
```
```   566 end
```
```   567
```
```   568 instantiation finite_2 :: linorder
```
```   569 begin
```
```   570
```
```   571 definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
```
```   572 where
```
```   573   "x < y \<longleftrightarrow> x = a\<^sub>1 \<and> y = a\<^sub>2"
```
```   574
```
```   575 definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
```
```   576 where
```
```   577   "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_2)"
```
```   578
```
```   579 instance
```
```   580 apply (intro_classes)
```
```   581 apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
```
```   582 apply (metis finite_2.nchotomy)+
```
```   583 done
```
```   584
```
```   585 end
```
```   586
```
```   587 hide_const (open) a\<^sub>1 a\<^sub>2
```
```   588
```
```   589 datatype finite_3 = a\<^sub>1 | a\<^sub>2 | a\<^sub>3
```
```   590
```
```   591 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   592 notation (output) a\<^sub>2  ("a\<^sub>2")
```
```   593 notation (output) a\<^sub>3  ("a\<^sub>3")
```
```   594
```
```   595 lemma UNIV_finite_3:
```
```   596   "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3}"
```
```   597   by (auto intro: finite_3.exhaust)
```
```   598
```
```   599 instantiation finite_3 :: enum
```
```   600 begin
```
```   601
```
```   602 definition
```
```   603   "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3]"
```
```   604
```
```   605 definition
```
```   606   "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3"
```
```   607
```
```   608 definition
```
```   609   "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3"
```
```   610
```
```   611 instance proof
```
```   612 qed (simp_all only: enum_finite_3_def enum_all_finite_3_def enum_ex_finite_3_def UNIV_finite_3, simp_all)
```
```   613
```
```   614 end
```
```   615
```
```   616 instantiation finite_3 :: linorder
```
```   617 begin
```
```   618
```
```   619 definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
```
```   620 where
```
```   621   "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)"
```
```   622
```
```   623 definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
```
```   624 where
```
```   625   "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_3)"
```
```   626
```
```   627 instance proof (intro_classes)
```
```   628 qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
```
```   629
```
```   630 end
```
```   631
```
```   632 hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3
```
```   633
```
```   634 datatype finite_4 = a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4
```
```   635
```
```   636 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   637 notation (output) a\<^sub>2  ("a\<^sub>2")
```
```   638 notation (output) a\<^sub>3  ("a\<^sub>3")
```
```   639 notation (output) a\<^sub>4  ("a\<^sub>4")
```
```   640
```
```   641 lemma UNIV_finite_4:
```
```   642   "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4}"
```
```   643   by (auto intro: finite_4.exhaust)
```
```   644
```
```   645 instantiation finite_4 :: enum
```
```   646 begin
```
```   647
```
```   648 definition
```
```   649   "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4]"
```
```   650
```
```   651 definition
```
```   652   "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3 \<and> P a\<^sub>4"
```
```   653
```
```   654 definition
```
```   655   "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3 \<or> P a\<^sub>4"
```
```   656
```
```   657 instance proof
```
```   658 qed (simp_all only: enum_finite_4_def enum_all_finite_4_def enum_ex_finite_4_def UNIV_finite_4, simp_all)
```
```   659
```
```   660 end
```
```   661
```
```   662 hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4
```
```   663
```
```   664
```
```   665 datatype finite_5 = a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4 | a\<^sub>5
```
```   666
```
```   667 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   668 notation (output) a\<^sub>2  ("a\<^sub>2")
```
```   669 notation (output) a\<^sub>3  ("a\<^sub>3")
```
```   670 notation (output) a\<^sub>4  ("a\<^sub>4")
```
```   671 notation (output) a\<^sub>5  ("a\<^sub>5")
```
```   672
```
```   673 lemma UNIV_finite_5:
```
```   674   "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5}"
```
```   675   by (auto intro: finite_5.exhaust)
```
```   676
```
```   677 instantiation finite_5 :: enum
```
```   678 begin
```
```   679
```
```   680 definition
```
```   681   "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5]"
```
```   682
```
```   683 definition
```
```   684   "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"
```
```   685
```
```   686 definition
```
```   687   "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"
```
```   688
```
```   689 instance proof
```
```   690 qed (simp_all only: enum_finite_5_def enum_all_finite_5_def enum_ex_finite_5_def UNIV_finite_5, simp_all)
```
```   691
```
```   692 end
```
```   693
```
```   694 hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4 a\<^sub>5
```
```   695
```
```   696
```
```   697 subsection {* Closing up *}
```
```   698
```
```   699 hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
```
```   700 hide_const (open) enum enum_all enum_ex all_n_lists ex_n_lists ntrancl
```
```   701
```
```   702 end
```