src/HOL/Enum.thy
 author paulson Mon Feb 22 14:37:56 2016 +0000 (2016-02-22) changeset 62379 340738057c8c parent 62343 24106dc44def child 62390 842917225d56 permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
```     1 (* Author: Florian Haftmann, TU Muenchen *)
```
```     2
```
```     3 section \<open>Finite types as explicit enumerations\<close>
```
```     4
```
```     5 theory Enum
```
```     6 imports Map Groups_List
```
```     7 begin
```
```     8
```
```     9 subsection \<open>Class \<open>enum\<close>\<close>
```
```    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    \<comment> \<open>tailored towards simple instantiation\<close>
```
```    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 \<open>Implementations using @{class enum}\<close>
```
```    56
```
```    57 subsubsection \<open>Unbounded operations and quantifiers\<close>
```
```    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 \<open>An executable choice operator\<close>
```
```    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 \<open>P x\<close> in_enum[of x, unfolded enum_eq] \<open>x \<noteq> a\<close> 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 \<open>Equality and order on functions\<close>
```
```   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::enum \<Rightarrow> 'b::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 \<open>Operations on relations\<close>
```
```   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 \<open>Bounded accessible part\<close>
```
```   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 \<open>finite r\<close> 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 \<open>Default instances for @{class enum}\<close>
```
```   245
```
```   246 lemma map_of_zip_enum_is_Some:
```
```   247   assumes "length ys = length (enum :: 'a::enum list)"
```
```   248   shows "\<exists>y. map_of (zip (enum :: 'a::enum list) ys) x = Some y"
```
```   249 proof -
```
```   250   from assms have "x \<in> set (enum :: 'a::enum list) \<longleftrightarrow>
```
```   251     (\<exists>y. map_of (zip (enum :: 'a::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::enum list"
```
```   258   assumes length: "length xs = length (enum :: 'a::enum list)"
```
```   259       "length ys = length (enum :: 'a::enum list)"
```
```   260     and map_of: "the \<circ> map_of (zip (enum :: 'a::enum list) xs) = the \<circ> map_of (zip (enum :: 'a::enum list) ys)"
```
```   261   shows "xs = ys"
```
```   262 proof -
```
```   263   have "map_of (zip (enum :: 'a list) xs) = map_of (zip (enum :: '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 :: 'a list) xs) x = Some y1"
```
```   268         and "map_of (zip (enum :: 'a list) ys) x = Some y2" by blast
```
```   269     moreover from map_of
```
```   270       have "the (map_of (zip (enum :: 'a::enum list) xs) x) = the (map_of (zip (enum :: 'a::enum list) ys) x)"
```
```   271       by (auto dest: fun_cong)
```
```   272     ultimately show "map_of (zip (enum :: 'a::enum list) xs) x = map_of (zip (enum :: 'a::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::'a list) ys)) (List.n_lists (length (enum::'a::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 :: ('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 :: 'a::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 :: ('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 :: 'a::enum list) (map f enum))"
```
```   331         by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
```
```   332       from \<open>enum_all P\<close> 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 :: 'a::enum list) (map f enum))"
```
```   356       by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
```
```   357     from \<open>P f\<close> this have "P (the \<circ> map_of (zip (enum :: 'a::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 :: 'a::{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
```
```   450   by standard
```
```   451     (simp_all add: enum_prod_def distinct_product
```
```   452       enum_UNIV enum_distinct enum_all_prod_def enum_ex_prod_def)
```
```   453
```
```   454 end
```
```   455
```
```   456 instantiation sum :: (enum, enum) enum
```
```   457 begin
```
```   458
```
```   459 definition
```
```   460   "enum = map Inl enum @ map Inr enum"
```
```   461
```
```   462 definition
```
```   463   "enum_all P \<longleftrightarrow> enum_all (\<lambda>x. P (Inl x)) \<and> enum_all (\<lambda>x. P (Inr x))"
```
```   464
```
```   465 definition
```
```   466   "enum_ex P \<longleftrightarrow> enum_ex (\<lambda>x. P (Inl x)) \<or> enum_ex (\<lambda>x. P (Inr x))"
```
```   467
```
```   468 instance proof
```
```   469 qed (simp_all only: enum_sum_def enum_all_sum_def enum_ex_sum_def UNIV_sum,
```
```   470   auto simp add: enum_UNIV distinct_map enum_distinct)
```
```   471
```
```   472 end
```
```   473
```
```   474 instantiation option :: (enum) enum
```
```   475 begin
```
```   476
```
```   477 definition
```
```   478   "enum = None # map Some enum"
```
```   479
```
```   480 definition
```
```   481   "enum_all P \<longleftrightarrow> P None \<and> enum_all (\<lambda>x. P (Some x))"
```
```   482
```
```   483 definition
```
```   484   "enum_ex P \<longleftrightarrow> P None \<or> enum_ex (\<lambda>x. P (Some x))"
```
```   485
```
```   486 instance proof
```
```   487 qed (simp_all only: enum_option_def enum_all_option_def enum_ex_option_def UNIV_option_conv,
```
```   488   auto simp add: distinct_map enum_UNIV enum_distinct)
```
```   489
```
```   490 end
```
```   491
```
```   492
```
```   493 subsection \<open>Small finite types\<close>
```
```   494
```
```   495 text \<open>We define small finite types for use in Quickcheck\<close>
```
```   496
```
```   497 datatype (plugins only: code "quickcheck" extraction) finite_1 =
```
```   498   a\<^sub>1
```
```   499
```
```   500 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   501
```
```   502 lemma UNIV_finite_1:
```
```   503   "UNIV = {a\<^sub>1}"
```
```   504   by (auto intro: finite_1.exhaust)
```
```   505
```
```   506 instantiation finite_1 :: enum
```
```   507 begin
```
```   508
```
```   509 definition
```
```   510   "enum = [a\<^sub>1]"
```
```   511
```
```   512 definition
```
```   513   "enum_all P = P a\<^sub>1"
```
```   514
```
```   515 definition
```
```   516   "enum_ex P = P a\<^sub>1"
```
```   517
```
```   518 instance proof
```
```   519 qed (simp_all only: enum_finite_1_def enum_all_finite_1_def enum_ex_finite_1_def UNIV_finite_1, simp_all)
```
```   520
```
```   521 end
```
```   522
```
```   523 instantiation finite_1 :: linorder
```
```   524 begin
```
```   525
```
```   526 definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
```
```   527 where
```
```   528   "x < (y :: finite_1) \<longleftrightarrow> False"
```
```   529
```
```   530 definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
```
```   531 where
```
```   532   "x \<le> (y :: finite_1) \<longleftrightarrow> True"
```
```   533
```
```   534 instance
```
```   535 apply (intro_classes)
```
```   536 apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
```
```   537 apply (metis finite_1.exhaust)
```
```   538 done
```
```   539
```
```   540 end
```
```   541
```
```   542 instance finite_1 :: "{dense_linorder, wellorder}"
```
```   543 by intro_classes (simp_all add: less_finite_1_def)
```
```   544
```
```   545 instantiation finite_1 :: complete_lattice
```
```   546 begin
```
```   547
```
```   548 definition [simp]: "Inf = (\<lambda>_. a\<^sub>1)"
```
```   549 definition [simp]: "Sup = (\<lambda>_. a\<^sub>1)"
```
```   550 definition [simp]: "bot = a\<^sub>1"
```
```   551 definition [simp]: "top = a\<^sub>1"
```
```   552 definition [simp]: "inf = (\<lambda>_ _. a\<^sub>1)"
```
```   553 definition [simp]: "sup = (\<lambda>_ _. a\<^sub>1)"
```
```   554
```
```   555 instance by intro_classes(simp_all add: less_eq_finite_1_def)
```
```   556 end
```
```   557
```
```   558 instance finite_1 :: complete_distrib_lattice
```
```   559   by standard simp_all
```
```   560
```
```   561 instance finite_1 :: complete_linorder ..
```
```   562
```
```   563 lemma finite_1_eq: "x = a\<^sub>1"
```
```   564 by(cases x) simp
```
```   565
```
```   566 simproc_setup finite_1_eq ("x::finite_1") = \<open>
```
```   567   fn _ => fn _ => fn ct =>
```
```   568     (case Thm.term_of ct of
```
```   569       Const (@{const_name a\<^sub>1}, _) => NONE
```
```   570     | _ => SOME (mk_meta_eq @{thm finite_1_eq}))
```
```   571 \<close>
```
```   572
```
```   573 instantiation finite_1 :: complete_boolean_algebra
```
```   574 begin
```
```   575 definition [simp]: "op - = (\<lambda>_ _. a\<^sub>1)"
```
```   576 definition [simp]: "uminus = (\<lambda>_. a\<^sub>1)"
```
```   577 instance by intro_classes simp_all
```
```   578 end
```
```   579
```
```   580 instantiation finite_1 ::
```
```   581   "{linordered_ring_strict, linordered_comm_semiring_strict, ordered_comm_ring,
```
```   582     ordered_cancel_comm_monoid_diff, comm_monoid_mult, ordered_ring_abs,
```
```   583     one, Divides.div, sgn_if, inverse}"
```
```   584 begin
```
```   585 definition [simp]: "Groups.zero = a\<^sub>1"
```
```   586 definition [simp]: "Groups.one = a\<^sub>1"
```
```   587 definition [simp]: "op + = (\<lambda>_ _. a\<^sub>1)"
```
```   588 definition [simp]: "op * = (\<lambda>_ _. a\<^sub>1)"
```
```   589 definition [simp]: "op mod = (\<lambda>_ _. a\<^sub>1)"
```
```   590 definition [simp]: "abs = (\<lambda>_. a\<^sub>1)"
```
```   591 definition [simp]: "sgn = (\<lambda>_. a\<^sub>1)"
```
```   592 definition [simp]: "inverse = (\<lambda>_. a\<^sub>1)"
```
```   593 definition [simp]: "divide = (\<lambda>_ _. a\<^sub>1)"
```
```   594
```
```   595 instance by intro_classes(simp_all add: less_finite_1_def)
```
```   596 end
```
```   597
```
```   598 declare [[simproc del: finite_1_eq]]
```
```   599 hide_const (open) a\<^sub>1
```
```   600
```
```   601 datatype (plugins only: code "quickcheck" extraction) finite_2 =
```
```   602   a\<^sub>1 | a\<^sub>2
```
```   603
```
```   604 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   605 notation (output) a\<^sub>2  ("a\<^sub>2")
```
```   606
```
```   607 lemma UNIV_finite_2:
```
```   608   "UNIV = {a\<^sub>1, a\<^sub>2}"
```
```   609   by (auto intro: finite_2.exhaust)
```
```   610
```
```   611 instantiation finite_2 :: enum
```
```   612 begin
```
```   613
```
```   614 definition
```
```   615   "enum = [a\<^sub>1, a\<^sub>2]"
```
```   616
```
```   617 definition
```
```   618   "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2"
```
```   619
```
```   620 definition
```
```   621   "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2"
```
```   622
```
```   623 instance proof
```
```   624 qed (simp_all only: enum_finite_2_def enum_all_finite_2_def enum_ex_finite_2_def UNIV_finite_2, simp_all)
```
```   625
```
```   626 end
```
```   627
```
```   628 instantiation finite_2 :: linorder
```
```   629 begin
```
```   630
```
```   631 definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
```
```   632 where
```
```   633   "x < y \<longleftrightarrow> x = a\<^sub>1 \<and> y = a\<^sub>2"
```
```   634
```
```   635 definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
```
```   636 where
```
```   637   "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_2)"
```
```   638
```
```   639 instance
```
```   640 apply (intro_classes)
```
```   641 apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
```
```   642 apply (metis finite_2.nchotomy)+
```
```   643 done
```
```   644
```
```   645 end
```
```   646
```
```   647 instance finite_2 :: wellorder
```
```   648 by(rule wf_wellorderI)(simp add: less_finite_2_def, intro_classes)
```
```   649
```
```   650 instantiation finite_2 :: complete_lattice
```
```   651 begin
```
```   652
```
```   653 definition "\<Sqinter>A = (if a\<^sub>1 \<in> A then a\<^sub>1 else a\<^sub>2)"
```
```   654 definition "\<Squnion>A = (if a\<^sub>2 \<in> A then a\<^sub>2 else a\<^sub>1)"
```
```   655 definition [simp]: "bot = a\<^sub>1"
```
```   656 definition [simp]: "top = a\<^sub>2"
```
```   657 definition "x \<sqinter> y = (if x = a\<^sub>1 \<or> y = a\<^sub>1 then a\<^sub>1 else a\<^sub>2)"
```
```   658 definition "x \<squnion> y = (if x = a\<^sub>2 \<or> y = a\<^sub>2 then a\<^sub>2 else a\<^sub>1)"
```
```   659
```
```   660 lemma neq_finite_2_a\<^sub>1_iff [simp]: "x \<noteq> a\<^sub>1 \<longleftrightarrow> x = a\<^sub>2"
```
```   661 by(cases x) simp_all
```
```   662
```
```   663 lemma neq_finite_2_a\<^sub>1_iff' [simp]: "a\<^sub>1 \<noteq> x \<longleftrightarrow> x = a\<^sub>2"
```
```   664 by(cases x) simp_all
```
```   665
```
```   666 lemma neq_finite_2_a\<^sub>2_iff [simp]: "x \<noteq> a\<^sub>2 \<longleftrightarrow> x = a\<^sub>1"
```
```   667 by(cases x) simp_all
```
```   668
```
```   669 lemma neq_finite_2_a\<^sub>2_iff' [simp]: "a\<^sub>2 \<noteq> x \<longleftrightarrow> x = a\<^sub>1"
```
```   670 by(cases x) simp_all
```
```   671
```
```   672 instance
```
```   673 proof
```
```   674   fix x :: finite_2 and A
```
```   675   assume "x \<in> A"
```
```   676   then show "\<Sqinter>A \<le> x" "x \<le> \<Squnion>A"
```
```   677     by(case_tac [!] x)(auto simp add: less_eq_finite_2_def less_finite_2_def Inf_finite_2_def Sup_finite_2_def)
```
```   678 qed(auto simp add: less_eq_finite_2_def less_finite_2_def inf_finite_2_def sup_finite_2_def Inf_finite_2_def Sup_finite_2_def)
```
```   679 end
```
```   680
```
```   681 instance finite_2 :: complete_distrib_lattice
```
```   682   by standard (auto simp add: sup_finite_2_def inf_finite_2_def Inf_finite_2_def Sup_finite_2_def)
```
```   683
```
```   684 instance finite_2 :: complete_linorder ..
```
```   685
```
```   686 instantiation finite_2 :: "{field, abs_if, ring_div, sgn_if, semiring_div}" begin
```
```   687 definition [simp]: "0 = a\<^sub>1"
```
```   688 definition [simp]: "1 = a\<^sub>2"
```
```   689 definition "x + y = (case (x, y) of (a\<^sub>1, a\<^sub>1) \<Rightarrow> a\<^sub>1 | (a\<^sub>2, a\<^sub>2) \<Rightarrow> a\<^sub>1 | _ \<Rightarrow> a\<^sub>2)"
```
```   690 definition "uminus = (\<lambda>x :: finite_2. x)"
```
```   691 definition "op - = (op + :: finite_2 \<Rightarrow> _)"
```
```   692 definition "x * y = (case (x, y) of (a\<^sub>2, a\<^sub>2) \<Rightarrow> a\<^sub>2 | _ \<Rightarrow> a\<^sub>1)"
```
```   693 definition "inverse = (\<lambda>x :: finite_2. x)"
```
```   694 definition "divide = (op * :: finite_2 \<Rightarrow> _)"
```
```   695 definition "abs = (\<lambda>x :: finite_2. x)"
```
```   696 definition "x mod y = (case (x, y) of (a\<^sub>2, a\<^sub>1) \<Rightarrow> a\<^sub>2 | _ \<Rightarrow> a\<^sub>1)"
```
```   697 definition "sgn = (\<lambda>x :: finite_2. x)"
```
```   698 instance
```
```   699 by intro_classes
```
```   700   (simp_all add: plus_finite_2_def uminus_finite_2_def minus_finite_2_def times_finite_2_def
```
```   701        inverse_finite_2_def divide_finite_2_def abs_finite_2_def mod_finite_2_def sgn_finite_2_def
```
```   702      split: finite_2.splits)
```
```   703 end
```
```   704
```
```   705 lemma two_finite_2 [simp]:
```
```   706   "2 = a\<^sub>1"
```
```   707   by (simp add: numeral.simps plus_finite_2_def)
```
```   708
```
```   709 hide_const (open) a\<^sub>1 a\<^sub>2
```
```   710
```
```   711 datatype (plugins only: code "quickcheck" extraction) finite_3 =
```
```   712   a\<^sub>1 | a\<^sub>2 | a\<^sub>3
```
```   713
```
```   714 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   715 notation (output) a\<^sub>2  ("a\<^sub>2")
```
```   716 notation (output) a\<^sub>3  ("a\<^sub>3")
```
```   717
```
```   718 lemma UNIV_finite_3:
```
```   719   "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3}"
```
```   720   by (auto intro: finite_3.exhaust)
```
```   721
```
```   722 instantiation finite_3 :: enum
```
```   723 begin
```
```   724
```
```   725 definition
```
```   726   "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3]"
```
```   727
```
```   728 definition
```
```   729   "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3"
```
```   730
```
```   731 definition
```
```   732   "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3"
```
```   733
```
```   734 instance proof
```
```   735 qed (simp_all only: enum_finite_3_def enum_all_finite_3_def enum_ex_finite_3_def UNIV_finite_3, simp_all)
```
```   736
```
```   737 end
```
```   738
```
```   739 instantiation finite_3 :: linorder
```
```   740 begin
```
```   741
```
```   742 definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
```
```   743 where
```
```   744   "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)"
```
```   745
```
```   746 definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
```
```   747 where
```
```   748   "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_3)"
```
```   749
```
```   750 instance proof (intro_classes)
```
```   751 qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
```
```   752
```
```   753 end
```
```   754
```
```   755 instance finite_3 :: wellorder
```
```   756 proof(rule wf_wellorderI)
```
```   757   have "inv_image less_than (case_finite_3 0 1 2) = {(x, y). x < y}"
```
```   758     by(auto simp add: less_finite_3_def split: finite_3.splits)
```
```   759   from this[symmetric] show "wf \<dots>" by simp
```
```   760 qed intro_classes
```
```   761
```
```   762 instantiation finite_3 :: complete_lattice
```
```   763 begin
```
```   764
```
```   765 definition "\<Sqinter>A = (if a\<^sub>1 \<in> A then a\<^sub>1 else if a\<^sub>2 \<in> A then a\<^sub>2 else a\<^sub>3)"
```
```   766 definition "\<Squnion>A = (if a\<^sub>3 \<in> A then a\<^sub>3 else if a\<^sub>2 \<in> A then a\<^sub>2 else a\<^sub>1)"
```
```   767 definition [simp]: "bot = a\<^sub>1"
```
```   768 definition [simp]: "top = a\<^sub>3"
```
```   769 definition [simp]: "inf = (min :: finite_3 \<Rightarrow> _)"
```
```   770 definition [simp]: "sup = (max :: finite_3 \<Rightarrow> _)"
```
```   771
```
```   772 instance
```
```   773 proof
```
```   774   fix x :: finite_3 and A
```
```   775   assume "x \<in> A"
```
```   776   then show "\<Sqinter>A \<le> x" "x \<le> \<Squnion>A"
```
```   777     by(case_tac [!] x)(auto simp add: Inf_finite_3_def Sup_finite_3_def less_eq_finite_3_def less_finite_3_def)
```
```   778 next
```
```   779   fix A and z :: finite_3
```
```   780   assume "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
```
```   781   then show "z \<le> \<Sqinter>A"
```
```   782     by(cases z)(auto simp add: Inf_finite_3_def less_eq_finite_3_def less_finite_3_def)
```
```   783 next
```
```   784   fix A and z :: finite_3
```
```   785   assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
```
```   786   show "\<Squnion>A \<le> z"
```
```   787     by(auto simp add: Sup_finite_3_def less_eq_finite_3_def less_finite_3_def dest: *)
```
```   788 qed(auto simp add: Inf_finite_3_def Sup_finite_3_def)
```
```   789 end
```
```   790
```
```   791 instance finite_3 :: complete_distrib_lattice
```
```   792 proof
```
```   793   fix a :: finite_3 and B
```
```   794   show "a \<squnion> \<Sqinter>B = (\<Sqinter>b\<in>B. a \<squnion> b)"
```
```   795   proof(cases a "\<Sqinter>B" rule: finite_3.exhaust[case_product finite_3.exhaust])
```
```   796     case a\<^sub>2_a\<^sub>3
```
```   797     then have "\<And>x. x \<in> B \<Longrightarrow> x = a\<^sub>3"
```
```   798       by(case_tac x)(auto simp add: Inf_finite_3_def split: split_if_asm)
```
```   799     then show ?thesis using a\<^sub>2_a\<^sub>3
```
```   800       by(auto simp add: Inf_finite_3_def max_def less_eq_finite_3_def less_finite_3_def split: split_if_asm)
```
```   801   qed (auto simp add: Inf_finite_3_def max_def less_finite_3_def less_eq_finite_3_def split: split_if_asm)
```
```   802   show "a \<sqinter> \<Squnion>B = (\<Squnion>b\<in>B. a \<sqinter> b)"
```
```   803     by (cases a "\<Squnion>B" rule: finite_3.exhaust[case_product finite_3.exhaust])
```
```   804       (auto simp add: Sup_finite_3_def min_def less_finite_3_def less_eq_finite_3_def split: split_if_asm)
```
```   805 qed
```
```   806
```
```   807 instance finite_3 :: complete_linorder ..
```
```   808
```
```   809 instantiation finite_3 :: "{field, abs_if, ring_div, semiring_div, sgn_if}" begin
```
```   810 definition [simp]: "0 = a\<^sub>1"
```
```   811 definition [simp]: "1 = a\<^sub>2"
```
```   812 definition
```
```   813   "x + y = (case (x, y) of
```
```   814      (a\<^sub>1, a\<^sub>1) \<Rightarrow> a\<^sub>1 | (a\<^sub>2, a\<^sub>3) \<Rightarrow> a\<^sub>1 | (a\<^sub>3, a\<^sub>2) \<Rightarrow> a\<^sub>1
```
```   815    | (a\<^sub>1, a\<^sub>2) \<Rightarrow> a\<^sub>2 | (a\<^sub>2, a\<^sub>1) \<Rightarrow> a\<^sub>2 | (a\<^sub>3, a\<^sub>3) \<Rightarrow> a\<^sub>2
```
```   816    | _ \<Rightarrow> a\<^sub>3)"
```
```   817 definition "- x = (case x of a\<^sub>1 \<Rightarrow> a\<^sub>1 | a\<^sub>2 \<Rightarrow> a\<^sub>3 | a\<^sub>3 \<Rightarrow> a\<^sub>2)"
```
```   818 definition "x - y = x + (- y :: finite_3)"
```
```   819 definition "x * y = (case (x, y) of (a\<^sub>2, a\<^sub>2) \<Rightarrow> a\<^sub>2 | (a\<^sub>3, a\<^sub>3) \<Rightarrow> a\<^sub>2 | (a\<^sub>2, a\<^sub>3) \<Rightarrow> a\<^sub>3 | (a\<^sub>3, a\<^sub>2) \<Rightarrow> a\<^sub>3 | _ \<Rightarrow> a\<^sub>1)"
```
```   820 definition "inverse = (\<lambda>x :: finite_3. x)"
```
```   821 definition "x div y = x * inverse (y :: finite_3)"
```
```   822 definition "abs = (\<lambda>x :: finite_3. x)"
```
```   823 definition "x mod y = (case (x, y) of (a\<^sub>2, a\<^sub>1) \<Rightarrow> a\<^sub>2 | (a\<^sub>3, a\<^sub>1) \<Rightarrow> a\<^sub>3 | _ \<Rightarrow> a\<^sub>1)"
```
```   824 definition "sgn = (\<lambda>x. case x of a\<^sub>1 \<Rightarrow> a\<^sub>1 | _ \<Rightarrow> a\<^sub>2)"
```
```   825 instance
```
```   826 by intro_classes
```
```   827   (simp_all add: plus_finite_3_def uminus_finite_3_def minus_finite_3_def times_finite_3_def
```
```   828        inverse_finite_3_def divide_finite_3_def abs_finite_3_def mod_finite_3_def sgn_finite_3_def
```
```   829        less_finite_3_def
```
```   830      split: finite_3.splits)
```
```   831 end
```
```   832
```
```   833
```
```   834
```
```   835 hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3
```
```   836
```
```   837 datatype (plugins only: code "quickcheck" extraction) finite_4 =
```
```   838   a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4
```
```   839
```
```   840 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   841 notation (output) a\<^sub>2  ("a\<^sub>2")
```
```   842 notation (output) a\<^sub>3  ("a\<^sub>3")
```
```   843 notation (output) a\<^sub>4  ("a\<^sub>4")
```
```   844
```
```   845 lemma UNIV_finite_4:
```
```   846   "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4}"
```
```   847   by (auto intro: finite_4.exhaust)
```
```   848
```
```   849 instantiation finite_4 :: enum
```
```   850 begin
```
```   851
```
```   852 definition
```
```   853   "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4]"
```
```   854
```
```   855 definition
```
```   856   "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3 \<and> P a\<^sub>4"
```
```   857
```
```   858 definition
```
```   859   "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3 \<or> P a\<^sub>4"
```
```   860
```
```   861 instance proof
```
```   862 qed (simp_all only: enum_finite_4_def enum_all_finite_4_def enum_ex_finite_4_def UNIV_finite_4, simp_all)
```
```   863
```
```   864 end
```
```   865
```
```   866 instantiation finite_4 :: complete_lattice begin
```
```   867
```
```   868 text \<open>@{term a\<^sub>1} \$<\$ @{term a\<^sub>2},@{term a\<^sub>3} \$<\$ @{term a\<^sub>4},
```
```   869   but @{term a\<^sub>2} and @{term a\<^sub>3} are incomparable.\<close>
```
```   870
```
```   871 definition
```
```   872   "x < y \<longleftrightarrow> (case (x, y) of
```
```   873      (a\<^sub>1, a\<^sub>1) \<Rightarrow> False | (a\<^sub>1, _) \<Rightarrow> True
```
```   874    |  (a\<^sub>2, a\<^sub>4) \<Rightarrow> True
```
```   875    |  (a\<^sub>3, a\<^sub>4) \<Rightarrow> True  | _ \<Rightarrow> False)"
```
```   876
```
```   877 definition
```
```   878   "x \<le> y \<longleftrightarrow> (case (x, y) of
```
```   879      (a\<^sub>1, _) \<Rightarrow> True
```
```   880    | (a\<^sub>2, a\<^sub>2) \<Rightarrow> True | (a\<^sub>2, a\<^sub>4) \<Rightarrow> True
```
```   881    | (a\<^sub>3, a\<^sub>3) \<Rightarrow> True | (a\<^sub>3, a\<^sub>4) \<Rightarrow> True
```
```   882    | (a\<^sub>4, a\<^sub>4) \<Rightarrow> True | _ \<Rightarrow> False)"
```
```   883
```
```   884 definition
```
```   885   "\<Sqinter>A = (if a\<^sub>1 \<in> A \<or> a\<^sub>2 \<in> A \<and> a\<^sub>3 \<in> A then a\<^sub>1 else if a\<^sub>2 \<in> A then a\<^sub>2 else if a\<^sub>3 \<in> A then a\<^sub>3 else a\<^sub>4)"
```
```   886 definition
```
```   887   "\<Squnion>A = (if a\<^sub>4 \<in> A \<or> a\<^sub>2 \<in> A \<and> a\<^sub>3 \<in> A then a\<^sub>4 else if a\<^sub>2 \<in> A then a\<^sub>2 else if a\<^sub>3 \<in> A then a\<^sub>3 else a\<^sub>1)"
```
```   888 definition [simp]: "bot = a\<^sub>1"
```
```   889 definition [simp]: "top = a\<^sub>4"
```
```   890 definition
```
```   891   "x \<sqinter> y = (case (x, y) of
```
```   892      (a\<^sub>1, _) \<Rightarrow> a\<^sub>1 | (_, a\<^sub>1) \<Rightarrow> a\<^sub>1 | (a\<^sub>2, a\<^sub>3) \<Rightarrow> a\<^sub>1 | (a\<^sub>3, a\<^sub>2) \<Rightarrow> a\<^sub>1
```
```   893    | (a\<^sub>2, _) \<Rightarrow> a\<^sub>2 | (_, a\<^sub>2) \<Rightarrow> a\<^sub>2
```
```   894    | (a\<^sub>3, _) \<Rightarrow> a\<^sub>3 | (_, a\<^sub>3) \<Rightarrow> a\<^sub>3
```
```   895    | _ \<Rightarrow> a\<^sub>4)"
```
```   896 definition
```
```   897   "x \<squnion> y = (case (x, y) of
```
```   898      (a\<^sub>4, _) \<Rightarrow> a\<^sub>4 | (_, a\<^sub>4) \<Rightarrow> a\<^sub>4 | (a\<^sub>2, a\<^sub>3) \<Rightarrow> a\<^sub>4 | (a\<^sub>3, a\<^sub>2) \<Rightarrow> a\<^sub>4
```
```   899   | (a\<^sub>2, _) \<Rightarrow> a\<^sub>2 | (_, a\<^sub>2) \<Rightarrow> a\<^sub>2
```
```   900   | (a\<^sub>3, _) \<Rightarrow> a\<^sub>3 | (_, a\<^sub>3) \<Rightarrow> a\<^sub>3
```
```   901   | _ \<Rightarrow> a\<^sub>1)"
```
```   902
```
```   903 instance
```
```   904 proof
```
```   905   fix A and z :: finite_4
```
```   906   assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
```
```   907   show "\<Squnion>A \<le> z"
```
```   908     by(auto simp add: Sup_finite_4_def less_eq_finite_4_def dest!: * split: finite_4.splits)
```
```   909 next
```
```   910   fix A and z :: finite_4
```
```   911   assume *: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
```
```   912   show "z \<le> \<Sqinter>A"
```
```   913     by(auto simp add: Inf_finite_4_def less_eq_finite_4_def dest!: * split: finite_4.splits)
```
```   914 qed(auto simp add: less_finite_4_def less_eq_finite_4_def Inf_finite_4_def Sup_finite_4_def inf_finite_4_def sup_finite_4_def split: finite_4.splits)
```
```   915
```
```   916 end
```
```   917
```
```   918 instance finite_4 :: complete_distrib_lattice
```
```   919 proof
```
```   920   fix a :: finite_4 and B
```
```   921   show "a \<squnion> \<Sqinter>B = (\<Sqinter>b\<in>B. a \<squnion> b)"
```
```   922     by(cases a "\<Sqinter>B" rule: finite_4.exhaust[case_product finite_4.exhaust])
```
```   923       (auto simp add: sup_finite_4_def Inf_finite_4_def split: finite_4.splits split_if_asm)
```
```   924   show "a \<sqinter> \<Squnion>B = (\<Squnion>b\<in>B. a \<sqinter> b)"
```
```   925     by(cases a "\<Squnion>B" rule: finite_4.exhaust[case_product finite_4.exhaust])
```
```   926       (auto simp add: inf_finite_4_def Sup_finite_4_def split: finite_4.splits split_if_asm)
```
```   927 qed
```
```   928
```
```   929 instantiation finite_4 :: complete_boolean_algebra begin
```
```   930 definition "- x = (case x of a\<^sub>1 \<Rightarrow> a\<^sub>4 | a\<^sub>2 \<Rightarrow> a\<^sub>3 | a\<^sub>3 \<Rightarrow> a\<^sub>2 | a\<^sub>4 \<Rightarrow> a\<^sub>1)"
```
```   931 definition "x - y = x \<sqinter> - (y :: finite_4)"
```
```   932 instance
```
```   933 by intro_classes
```
```   934   (simp_all add: inf_finite_4_def sup_finite_4_def uminus_finite_4_def minus_finite_4_def split: finite_4.splits)
```
```   935 end
```
```   936
```
```   937 hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4
```
```   938
```
```   939 datatype (plugins only: code "quickcheck" extraction) finite_5 =
```
```   940   a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4 | a\<^sub>5
```
```   941
```
```   942 notation (output) a\<^sub>1  ("a\<^sub>1")
```
```   943 notation (output) a\<^sub>2  ("a\<^sub>2")
```
```   944 notation (output) a\<^sub>3  ("a\<^sub>3")
```
```   945 notation (output) a\<^sub>4  ("a\<^sub>4")
```
```   946 notation (output) a\<^sub>5  ("a\<^sub>5")
```
```   947
```
```   948 lemma UNIV_finite_5:
```
```   949   "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5}"
```
```   950   by (auto intro: finite_5.exhaust)
```
```   951
```
```   952 instantiation finite_5 :: enum
```
```   953 begin
```
```   954
```
```   955 definition
```
```   956   "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5]"
```
```   957
```
```   958 definition
```
```   959   "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"
```
```   960
```
```   961 definition
```
```   962   "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"
```
```   963
```
```   964 instance proof
```
```   965 qed (simp_all only: enum_finite_5_def enum_all_finite_5_def enum_ex_finite_5_def UNIV_finite_5, simp_all)
```
```   966
```
```   967 end
```
```   968
```
```   969 instantiation finite_5 :: complete_lattice
```
```   970 begin
```
```   971
```
```   972 text \<open>The non-distributive pentagon lattice \$N_5\$\<close>
```
```   973
```
```   974 definition
```
```   975   "x < y \<longleftrightarrow> (case (x, y) of
```
```   976      (a\<^sub>1, a\<^sub>1) \<Rightarrow> False | (a\<^sub>1, _) \<Rightarrow> True
```
```   977    | (a\<^sub>2, a\<^sub>3) \<Rightarrow> True  | (a\<^sub>2, a\<^sub>5) \<Rightarrow> True
```
```   978    | (a\<^sub>3, a\<^sub>5) \<Rightarrow> True
```
```   979    | (a\<^sub>4, a\<^sub>5) \<Rightarrow> True  | _ \<Rightarrow> False)"
```
```   980
```
```   981 definition
```
```   982   "x \<le> y \<longleftrightarrow> (case (x, y) of
```
```   983      (a\<^sub>1, _) \<Rightarrow> True
```
```   984    | (a\<^sub>2, a\<^sub>2) \<Rightarrow> True | (a\<^sub>2, a\<^sub>3) \<Rightarrow> True | (a\<^sub>2, a\<^sub>5) \<Rightarrow> True
```
```   985    | (a\<^sub>3, a\<^sub>3) \<Rightarrow> True | (a\<^sub>3, a\<^sub>5) \<Rightarrow> True
```
```   986    | (a\<^sub>4, a\<^sub>4) \<Rightarrow> True | (a\<^sub>4, a\<^sub>5) \<Rightarrow> True
```
```   987    | (a\<^sub>5, a\<^sub>5) \<Rightarrow> True | _ \<Rightarrow> False)"
```
```   988
```
```   989 definition
```
```   990   "\<Sqinter>A =
```
```   991   (if a\<^sub>1 \<in> A \<or> a\<^sub>4 \<in> A \<and> (a\<^sub>2 \<in> A \<or> a\<^sub>3 \<in> A) then a\<^sub>1
```
```   992    else if a\<^sub>2 \<in> A then a\<^sub>2
```
```   993    else if a\<^sub>3 \<in> A then a\<^sub>3
```
```   994    else if a\<^sub>4 \<in> A then a\<^sub>4
```
```   995    else a\<^sub>5)"
```
```   996 definition
```
```   997   "\<Squnion>A =
```
```   998   (if a\<^sub>5 \<in> A \<or> a\<^sub>4 \<in> A \<and> (a\<^sub>2 \<in> A \<or> a\<^sub>3 \<in> A) then a\<^sub>5
```
```   999    else if a\<^sub>3 \<in> A then a\<^sub>3
```
```  1000    else if a\<^sub>2 \<in> A then a\<^sub>2
```
```  1001    else if a\<^sub>4 \<in> A then a\<^sub>4
```
```  1002    else a\<^sub>1)"
```
```  1003 definition [simp]: "bot = a\<^sub>1"
```
```  1004 definition [simp]: "top = a\<^sub>5"
```
```  1005 definition
```
```  1006   "x \<sqinter> y = (case (x, y) of
```
```  1007      (a\<^sub>1, _) \<Rightarrow> a\<^sub>1 | (_, a\<^sub>1) \<Rightarrow> a\<^sub>1 | (a\<^sub>2, a\<^sub>4) \<Rightarrow> a\<^sub>1 | (a\<^sub>4, a\<^sub>2) \<Rightarrow> a\<^sub>1 | (a\<^sub>3, a\<^sub>4) \<Rightarrow> a\<^sub>1 | (a\<^sub>4, a\<^sub>3) \<Rightarrow> a\<^sub>1
```
```  1008    | (a\<^sub>2, _) \<Rightarrow> a\<^sub>2 | (_, a\<^sub>2) \<Rightarrow> a\<^sub>2
```
```  1009    | (a\<^sub>3, _) \<Rightarrow> a\<^sub>3 | (_, a\<^sub>3) \<Rightarrow> a\<^sub>3
```
```  1010    | (a\<^sub>4, _) \<Rightarrow> a\<^sub>4 | (_, a\<^sub>4) \<Rightarrow> a\<^sub>4
```
```  1011    | _ \<Rightarrow> a\<^sub>5)"
```
```  1012 definition
```
```  1013   "x \<squnion> y = (case (x, y) of
```
```  1014      (a\<^sub>5, _) \<Rightarrow> a\<^sub>5 | (_, a\<^sub>5) \<Rightarrow> a\<^sub>5 | (a\<^sub>2, a\<^sub>4) \<Rightarrow> a\<^sub>5 | (a\<^sub>4, a\<^sub>2) \<Rightarrow> a\<^sub>5 | (a\<^sub>3, a\<^sub>4) \<Rightarrow> a\<^sub>5 | (a\<^sub>4, a\<^sub>3) \<Rightarrow> a\<^sub>5
```
```  1015    | (a\<^sub>3, _) \<Rightarrow> a\<^sub>3 | (_, a\<^sub>3) \<Rightarrow> a\<^sub>3
```
```  1016    | (a\<^sub>2, _) \<Rightarrow> a\<^sub>2 | (_, a\<^sub>2) \<Rightarrow> a\<^sub>2
```
```  1017    | (a\<^sub>4, _) \<Rightarrow> a\<^sub>4 | (_, a\<^sub>4) \<Rightarrow> a\<^sub>4
```
```  1018    | _ \<Rightarrow> a\<^sub>1)"
```
```  1019
```
```  1020 instance
```
```  1021 proof intro_classes
```
```  1022   fix A and z :: finite_5
```
```  1023   assume *: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
```
```  1024   show "z \<le> \<Sqinter>A"
```
```  1025     by(auto simp add: less_eq_finite_5_def Inf_finite_5_def split: finite_5.splits split_if_asm dest!: *)
```
```  1026 next
```
```  1027   fix A and z :: finite_5
```
```  1028   assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
```
```  1029   show "\<Squnion>A \<le> z"
```
```  1030     by(auto simp add: less_eq_finite_5_def Sup_finite_5_def split: finite_5.splits split_if_asm dest!: *)
```
```  1031 qed(auto simp add: less_eq_finite_5_def less_finite_5_def inf_finite_5_def sup_finite_5_def Inf_finite_5_def Sup_finite_5_def split: finite_5.splits split_if_asm)
```
```  1032
```
```  1033 end
```
```  1034
```
```  1035 hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4 a\<^sub>5
```
```  1036
```
```  1037
```
```  1038 subsection \<open>Closing up\<close>
```
```  1039
```
```  1040 hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
```
```  1041 hide_const (open) enum enum_all enum_ex all_n_lists ex_n_lists ntrancl
```
```  1042
```
```  1043 end
```