src/HOL/Enum.thy
author haftmann
Sat Oct 20 09:12:16 2012 +0200 (2012-10-20)
changeset 49948 744934b818c7
parent 48123 104e5fccea12
child 49949 be3dd2e602e8
permissions -rw-r--r--
moved quite generic material from theory Enum to more appropriate places
     1 (* Author: Florian Haftmann, TU Muenchen *)
     2 
     3 header {* Finite types as explicit enumerations *}
     4 
     5 theory Enum
     6 imports Map String
     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 : "enum_all P = (\<forall> x. P x)"
    18   assumes enum_ex  : "enum_ex P = (\<exists> x. P x)" 
    19 begin
    20 
    21 subclass finite proof
    22 qed (simp add: UNIV_enum)
    23 
    24 lemma enum_UNIV: "set enum = UNIV" unfolding UNIV_enum ..
    25 
    26 lemma in_enum: "x \<in> set enum"
    27   unfolding enum_UNIV by auto
    28 
    29 lemma enum_eq_I:
    30   assumes "\<And>x. x \<in> set xs"
    31   shows "set enum = set xs"
    32 proof -
    33   from assms UNIV_eq_I have "UNIV = set xs" by auto
    34   with enum_UNIV show ?thesis by simp
    35 qed
    36 
    37 end
    38 
    39 
    40 subsection {* Equality and order on functions *}
    41 
    42 instantiation "fun" :: (enum, equal) equal
    43 begin
    44 
    45 definition
    46   "HOL.equal f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
    47 
    48 instance proof
    49 qed (simp_all add: equal_fun_def enum_UNIV fun_eq_iff)
    50 
    51 end
    52 
    53 lemma [code]:
    54   "HOL.equal f g \<longleftrightarrow> enum_all (%x. f x = g x)"
    55 by (auto simp add: equal enum_all fun_eq_iff)
    56 
    57 lemma [code nbe]:
    58   "HOL.equal (f :: _ \<Rightarrow> _) f \<longleftrightarrow> True"
    59   by (fact equal_refl)
    60 
    61 lemma order_fun [code]:
    62   fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
    63   shows "f \<le> g \<longleftrightarrow> enum_all (\<lambda>x. f x \<le> g x)"
    64     and "f < g \<longleftrightarrow> f \<le> g \<and> enum_ex (\<lambda>x. f x \<noteq> g x)"
    65   by (simp_all add: enum_all enum_ex fun_eq_iff le_fun_def order_less_le)
    66 
    67 
    68 subsection {* Quantifiers *}
    69 
    70 lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> enum_all P"
    71   by (simp add: enum_all)
    72 
    73 lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> enum_ex P"
    74   by (simp add: enum_ex)
    75 
    76 lemma exists1_code[code]: "(\<exists>!x. P x) \<longleftrightarrow> list_ex1 P enum"
    77   by (auto simp add: enum_UNIV list_ex1_iff)
    78 
    79 
    80 subsection {* Default instances *}
    81 
    82 lemma map_of_zip_enum_is_Some:
    83   assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
    84   shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
    85 proof -
    86   from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
    87     (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
    88     by (auto intro!: map_of_zip_is_Some)
    89   then show ?thesis using enum_UNIV by auto
    90 qed
    91 
    92 lemma map_of_zip_enum_inject:
    93   fixes xs ys :: "'b\<Colon>enum list"
    94   assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
    95       "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
    96     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)"
    97   shows "xs = ys"
    98 proof -
    99   have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
   100   proof
   101     fix x :: 'a
   102     from length map_of_zip_enum_is_Some obtain y1 y2
   103       where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
   104         and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
   105     moreover from map_of
   106       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)"
   107       by (auto dest: fun_cong)
   108     ultimately show "map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x = map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x"
   109       by simp
   110   qed
   111   with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
   112 qed
   113 
   114 definition
   115   all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
   116 where
   117   "all_n_lists P n = (\<forall>xs \<in> set (List.n_lists n enum). P xs)"
   118 
   119 lemma [code]:
   120   "all_n_lists P n = (if n = 0 then P [] else enum_all (%x. all_n_lists (%xs. P (x # xs)) (n - 1)))"
   121 unfolding all_n_lists_def enum_all
   122 by (cases n) (auto simp add: enum_UNIV)
   123 
   124 definition
   125   ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
   126 where
   127   "ex_n_lists P n = (\<exists>xs \<in> set (List.n_lists n enum). P xs)"
   128 
   129 lemma [code]:
   130   "ex_n_lists P n = (if n = 0 then P [] else enum_ex (%x. ex_n_lists (%xs. P (x # xs)) (n - 1)))"
   131 unfolding ex_n_lists_def enum_ex
   132 by (cases n) (auto simp add: enum_UNIV)
   133 
   134 
   135 instantiation "fun" :: (enum, enum) enum
   136 begin
   137 
   138 definition
   139   "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)"
   140 
   141 definition
   142   "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
   143 
   144 definition
   145   "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
   146 
   147 
   148 instance proof
   149   show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
   150   proof (rule UNIV_eq_I)
   151     fix f :: "'a \<Rightarrow> 'b"
   152     have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
   153       by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
   154     then show "f \<in> set enum"
   155       by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
   156   qed
   157 next
   158   from map_of_zip_enum_inject
   159   show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
   160     by (auto intro!: inj_onI simp add: enum_fun_def
   161       distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
   162 next
   163   fix P
   164   show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
   165   proof
   166     assume "enum_all P"
   167     show "\<forall>x. P x"
   168     proof
   169       fix f :: "'a \<Rightarrow> 'b"
   170       have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
   171         by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
   172       from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
   173         unfolding enum_all_fun_def all_n_lists_def
   174         apply (simp add: set_n_lists)
   175         apply (erule_tac x="map f enum" in allE)
   176         apply (auto intro!: in_enum)
   177         done
   178       from this f show "P f" by auto
   179     qed
   180   next
   181     assume "\<forall>x. P x"
   182     from this show "enum_all P"
   183       unfolding enum_all_fun_def all_n_lists_def by auto
   184   qed
   185 next
   186   fix P
   187   show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
   188   proof
   189     assume "enum_ex P"
   190     from this show "\<exists>x. P x"
   191       unfolding enum_ex_fun_def ex_n_lists_def by auto
   192   next
   193     assume "\<exists>x. P x"
   194     from this obtain f where "P f" ..
   195     have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
   196       by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum) 
   197     from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
   198       by auto
   199     from  this show "enum_ex P"
   200       unfolding enum_ex_fun_def ex_n_lists_def
   201       apply (auto simp add: set_n_lists)
   202       apply (rule_tac x="map f enum" in exI)
   203       apply (auto intro!: in_enum)
   204       done
   205   qed
   206 qed
   207 
   208 end
   209 
   210 lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
   211   in map (\<lambda>ys. the o map_of (zip enum_a ys)) (List.n_lists (length enum_a) enum))"
   212   by (simp add: enum_fun_def Let_def)
   213 
   214 lemma enum_all_fun_code [code]:
   215   "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
   216    in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
   217   by (simp add: enum_all_fun_def Let_def)
   218 
   219 lemma enum_ex_fun_code [code]:
   220   "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
   221    in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
   222   by (simp add: enum_ex_fun_def Let_def)
   223 
   224 instantiation unit :: enum
   225 begin
   226 
   227 definition
   228   "enum = [()]"
   229 
   230 definition
   231   "enum_all P = P ()"
   232 
   233 definition
   234   "enum_ex P = P ()"
   235 
   236 instance proof
   237 qed (auto simp add: enum_unit_def UNIV_unit enum_all_unit_def enum_ex_unit_def intro: unit.exhaust)
   238 
   239 end
   240 
   241 instantiation bool :: enum
   242 begin
   243 
   244 definition
   245   "enum = [False, True]"
   246 
   247 definition
   248   "enum_all P = (P False \<and> P True)"
   249 
   250 definition
   251   "enum_ex P = (P False \<or> P True)"
   252 
   253 instance proof
   254   fix P
   255   show "enum_all (P :: bool \<Rightarrow> bool) = (\<forall>x. P x)"
   256     unfolding enum_all_bool_def by (auto, case_tac x) auto
   257 next
   258   fix P
   259   show "enum_ex (P :: bool \<Rightarrow> bool) = (\<exists>x. P x)"
   260     unfolding enum_ex_bool_def by (auto, case_tac x) auto
   261 qed (auto simp add: enum_bool_def UNIV_bool)
   262 
   263 end
   264 
   265 instantiation prod :: (enum, enum) enum
   266 begin
   267 
   268 definition
   269   "enum = List.product enum enum"
   270 
   271 definition
   272   "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
   273 
   274 definition
   275   "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
   276 
   277  
   278 instance by default
   279   (simp_all add: enum_prod_def product_list_set distinct_product
   280     enum_UNIV enum_distinct enum_all_prod_def enum_all enum_ex_prod_def enum_ex)
   281 
   282 end
   283 
   284 instantiation sum :: (enum, enum) enum
   285 begin
   286 
   287 definition
   288   "enum = map Inl enum @ map Inr enum"
   289 
   290 definition
   291   "enum_all P = (enum_all (%x. P (Inl x)) \<and> enum_all (%x. P (Inr x)))"
   292 
   293 definition
   294   "enum_ex P = (enum_ex (%x. P (Inl x)) \<or> enum_ex (%x. P (Inr x)))"
   295 
   296 instance proof
   297   fix P
   298   show "enum_all (P :: ('a + 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
   299     unfolding enum_all_sum_def enum_all
   300     by (auto, case_tac x) auto
   301 next
   302   fix P
   303   show "enum_ex (P :: ('a + 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
   304     unfolding enum_ex_sum_def enum_ex
   305     by (auto, case_tac x) auto
   306 qed (auto simp add: enum_UNIV enum_sum_def, case_tac x, auto intro: inj_onI simp add: distinct_map enum_distinct)
   307 
   308 end
   309 
   310 instantiation nibble :: enum
   311 begin
   312 
   313 definition
   314   "enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
   315     Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
   316 
   317 definition
   318   "enum_all P = (P Nibble0 \<and> P Nibble1 \<and> P Nibble2 \<and> P Nibble3 \<and> P Nibble4 \<and> P Nibble5 \<and> P Nibble6 \<and> P Nibble7
   319      \<and> P Nibble8 \<and> P Nibble9 \<and> P NibbleA \<and> P NibbleB \<and> P NibbleC \<and> P NibbleD \<and> P NibbleE \<and> P NibbleF)"
   320 
   321 definition
   322   "enum_ex P = (P Nibble0 \<or> P Nibble1 \<or> P Nibble2 \<or> P Nibble3 \<or> P Nibble4 \<or> P Nibble5 \<or> P Nibble6 \<or> P Nibble7
   323      \<or> P Nibble8 \<or> P Nibble9 \<or> P NibbleA \<or> P NibbleB \<or> P NibbleC \<or> P NibbleD \<or> P NibbleE \<or> P NibbleF)"
   324 
   325 instance proof
   326   fix P
   327   show "enum_all (P :: nibble \<Rightarrow> bool) = (\<forall>x. P x)"
   328     unfolding enum_all_nibble_def
   329     by (auto, case_tac x) auto
   330 next
   331   fix P
   332   show "enum_ex (P :: nibble \<Rightarrow> bool) = (\<exists>x. P x)"
   333     unfolding enum_ex_nibble_def
   334     by (auto, case_tac x) auto
   335 qed (simp_all add: enum_nibble_def UNIV_nibble)
   336 
   337 end
   338 
   339 instantiation char :: enum
   340 begin
   341 
   342 definition
   343   "enum = map (split Char) (List.product enum enum)"
   344 
   345 lemma enum_chars [code]:
   346   "enum = chars"
   347   unfolding enum_char_def chars_def enum_nibble_def by simp
   348 
   349 definition
   350   "enum_all P = list_all P chars"
   351 
   352 definition
   353   "enum_ex P = list_ex P chars"
   354 
   355 lemma set_enum_char: "set (enum :: char list) = UNIV"
   356     by (auto intro: char.exhaust simp add: enum_char_def product_list_set enum_UNIV full_SetCompr_eq [symmetric])
   357 
   358 instance proof
   359   fix P
   360   show "enum_all (P :: char \<Rightarrow> bool) = (\<forall>x. P x)"
   361     unfolding enum_all_char_def enum_chars[symmetric]
   362     by (auto simp add: list_all_iff set_enum_char)
   363 next
   364   fix P
   365   show "enum_ex (P :: char \<Rightarrow> bool) = (\<exists>x. P x)"
   366     unfolding enum_ex_char_def enum_chars[symmetric]
   367     by (auto simp add: list_ex_iff set_enum_char)
   368 next
   369   show "distinct (enum :: char list)"
   370     by (auto intro: inj_onI simp add: enum_char_def product_list_set distinct_map distinct_product enum_distinct)
   371 qed (auto simp add: set_enum_char)
   372 
   373 end
   374 
   375 instantiation option :: (enum) enum
   376 begin
   377 
   378 definition
   379   "enum = None # map Some enum"
   380 
   381 definition
   382   "enum_all P = (P None \<and> enum_all (%x. P (Some x)))"
   383 
   384 definition
   385   "enum_ex P = (P None \<or> enum_ex (%x. P (Some x)))"
   386 
   387 instance proof
   388   fix P
   389   show "enum_all (P :: 'a option \<Rightarrow> bool) = (\<forall>x. P x)"
   390     unfolding enum_all_option_def enum_all
   391     by (auto, case_tac x) auto
   392 next
   393   fix P
   394   show "enum_ex (P :: 'a option \<Rightarrow> bool) = (\<exists>x. P x)"
   395     unfolding enum_ex_option_def enum_ex
   396     by (auto, case_tac x) auto
   397 qed (auto simp add: enum_UNIV enum_option_def, rule option.exhaust, auto intro: simp add: distinct_map enum_distinct)
   398 end
   399 
   400 instantiation set :: (enum) enum
   401 begin
   402 
   403 definition
   404   "enum = map set (sublists enum)"
   405 
   406 definition
   407   "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
   408 
   409 definition
   410   "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
   411 
   412 instance proof
   413 qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
   414   enum_distinct enum_UNIV)
   415 
   416 end
   417 
   418 
   419 subsection {* Small finite types *}
   420 
   421 text {* We define small finite types for the use in Quickcheck *}
   422 
   423 datatype finite_1 = a\<^isub>1
   424 
   425 notation (output) a\<^isub>1  ("a\<^isub>1")
   426 
   427 instantiation finite_1 :: enum
   428 begin
   429 
   430 definition
   431   "enum = [a\<^isub>1]"
   432 
   433 definition
   434   "enum_all P = P a\<^isub>1"
   435 
   436 definition
   437   "enum_ex P = P a\<^isub>1"
   438 
   439 instance proof
   440   fix P
   441   show "enum_all (P :: finite_1 \<Rightarrow> bool) = (\<forall>x. P x)"
   442     unfolding enum_all_finite_1_def
   443     by (auto, case_tac x) auto
   444 next
   445   fix P
   446   show "enum_ex (P :: finite_1 \<Rightarrow> bool) = (\<exists>x. P x)"
   447     unfolding enum_ex_finite_1_def
   448     by (auto, case_tac x) auto
   449 qed (auto simp add: enum_finite_1_def intro: finite_1.exhaust)
   450 
   451 end
   452 
   453 instantiation finite_1 :: linorder
   454 begin
   455 
   456 definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
   457 where
   458   "less_eq_finite_1 x y = True"
   459 
   460 definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
   461 where
   462   "less_finite_1 x y = False"
   463 
   464 instance
   465 apply (intro_classes)
   466 apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
   467 apply (metis finite_1.exhaust)
   468 done
   469 
   470 end
   471 
   472 hide_const (open) a\<^isub>1
   473 
   474 datatype finite_2 = a\<^isub>1 | a\<^isub>2
   475 
   476 notation (output) a\<^isub>1  ("a\<^isub>1")
   477 notation (output) a\<^isub>2  ("a\<^isub>2")
   478 
   479 instantiation finite_2 :: enum
   480 begin
   481 
   482 definition
   483   "enum = [a\<^isub>1, a\<^isub>2]"
   484 
   485 definition
   486   "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2)"
   487 
   488 definition
   489   "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2)"
   490 
   491 instance proof
   492   fix P
   493   show "enum_all (P :: finite_2 \<Rightarrow> bool) = (\<forall>x. P x)"
   494     unfolding enum_all_finite_2_def
   495     by (auto, case_tac x) auto
   496 next
   497   fix P
   498   show "enum_ex (P :: finite_2 \<Rightarrow> bool) = (\<exists>x. P x)"
   499     unfolding enum_ex_finite_2_def
   500     by (auto, case_tac x) auto
   501 qed (auto simp add: enum_finite_2_def intro: finite_2.exhaust)
   502 
   503 end
   504 
   505 instantiation finite_2 :: linorder
   506 begin
   507 
   508 definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
   509 where
   510   "less_finite_2 x y = ((x = a\<^isub>1) & (y = a\<^isub>2))"
   511 
   512 definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
   513 where
   514   "less_eq_finite_2 x y = ((x = y) \<or> (x < y))"
   515 
   516 
   517 instance
   518 apply (intro_classes)
   519 apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
   520 apply (metis finite_2.distinct finite_2.nchotomy)+
   521 done
   522 
   523 end
   524 
   525 hide_const (open) a\<^isub>1 a\<^isub>2
   526 
   527 
   528 datatype finite_3 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3
   529 
   530 notation (output) a\<^isub>1  ("a\<^isub>1")
   531 notation (output) a\<^isub>2  ("a\<^isub>2")
   532 notation (output) a\<^isub>3  ("a\<^isub>3")
   533 
   534 instantiation finite_3 :: enum
   535 begin
   536 
   537 definition
   538   "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3]"
   539 
   540 definition
   541   "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3)"
   542 
   543 definition
   544   "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3)"
   545 
   546 instance proof
   547   fix P
   548   show "enum_all (P :: finite_3 \<Rightarrow> bool) = (\<forall>x. P x)"
   549     unfolding enum_all_finite_3_def
   550     by (auto, case_tac x) auto
   551 next
   552   fix P
   553   show "enum_ex (P :: finite_3 \<Rightarrow> bool) = (\<exists>x. P x)"
   554     unfolding enum_ex_finite_3_def
   555     by (auto, case_tac x) auto
   556 qed (auto simp add: enum_finite_3_def intro: finite_3.exhaust)
   557 
   558 end
   559 
   560 instantiation finite_3 :: linorder
   561 begin
   562 
   563 definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
   564 where
   565   "less_finite_3 x y = (case x of a\<^isub>1 => (y \<noteq> a\<^isub>1)
   566      | a\<^isub>2 => (y = a\<^isub>3)| a\<^isub>3 => False)"
   567 
   568 definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
   569 where
   570   "less_eq_finite_3 x y = ((x = y) \<or> (x < y))"
   571 
   572 
   573 instance proof (intro_classes)
   574 qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
   575 
   576 end
   577 
   578 hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3
   579 
   580 
   581 datatype finite_4 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4
   582 
   583 notation (output) a\<^isub>1  ("a\<^isub>1")
   584 notation (output) a\<^isub>2  ("a\<^isub>2")
   585 notation (output) a\<^isub>3  ("a\<^isub>3")
   586 notation (output) a\<^isub>4  ("a\<^isub>4")
   587 
   588 instantiation finite_4 :: enum
   589 begin
   590 
   591 definition
   592   "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4]"
   593 
   594 definition
   595   "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4)"
   596 
   597 definition
   598   "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4)"
   599 
   600 instance proof
   601   fix P
   602   show "enum_all (P :: finite_4 \<Rightarrow> bool) = (\<forall>x. P x)"
   603     unfolding enum_all_finite_4_def
   604     by (auto, case_tac x) auto
   605 next
   606   fix P
   607   show "enum_ex (P :: finite_4 \<Rightarrow> bool) = (\<exists>x. P x)"
   608     unfolding enum_ex_finite_4_def
   609     by (auto, case_tac x) auto
   610 qed (auto simp add: enum_finite_4_def intro: finite_4.exhaust)
   611 
   612 end
   613 
   614 hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4
   615 
   616 
   617 datatype finite_5 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4 | a\<^isub>5
   618 
   619 notation (output) a\<^isub>1  ("a\<^isub>1")
   620 notation (output) a\<^isub>2  ("a\<^isub>2")
   621 notation (output) a\<^isub>3  ("a\<^isub>3")
   622 notation (output) a\<^isub>4  ("a\<^isub>4")
   623 notation (output) a\<^isub>5  ("a\<^isub>5")
   624 
   625 instantiation finite_5 :: enum
   626 begin
   627 
   628 definition
   629   "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5]"
   630 
   631 definition
   632   "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4 \<and> P a\<^isub>5)"
   633 
   634 definition
   635   "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4 \<or> P a\<^isub>5)"
   636 
   637 instance proof
   638   fix P
   639   show "enum_all (P :: finite_5 \<Rightarrow> bool) = (\<forall>x. P x)"
   640     unfolding enum_all_finite_5_def
   641     by (auto, case_tac x) auto
   642 next
   643   fix P
   644   show "enum_ex (P :: finite_5 \<Rightarrow> bool) = (\<exists>x. P x)"
   645     unfolding enum_ex_finite_5_def
   646     by (auto, case_tac x) auto
   647 qed (auto simp add: enum_finite_5_def intro: finite_5.exhaust)
   648 
   649 end
   650 
   651 hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4 a\<^isub>5
   652 
   653 
   654 subsection {* An executable THE operator on finite types *}
   655 
   656 definition
   657   [code del]: "enum_the = The"
   658 
   659 lemma [code]:
   660   "The P = (case filter P enum of [x] => x | _ => enum_the P)"
   661 proof -
   662   {
   663     fix a
   664     assume filter_enum: "filter P enum = [a]"
   665     have "The P = a"
   666     proof (rule the_equality)
   667       fix x
   668       assume "P x"
   669       show "x = a"
   670       proof (rule ccontr)
   671         assume "x \<noteq> a"
   672         from filter_enum obtain us vs
   673           where enum_eq: "enum = us @ [a] @ vs"
   674           and "\<forall> x \<in> set us. \<not> P x"
   675           and "\<forall> x \<in> set vs. \<not> P x"
   676           and "P a"
   677           by (auto simp add: filter_eq_Cons_iff) (simp only: filter_empty_conv[symmetric])
   678         with `P x` in_enum[of x, unfolded enum_eq] `x \<noteq> a` show "False" by auto
   679       qed
   680     next
   681       from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
   682     qed
   683   }
   684   from this show ?thesis
   685     unfolding enum_the_def by (auto split: list.split)
   686 qed
   687 
   688 code_abort enum_the
   689 code_const enum_the (Eval "(fn p => raise Match)")
   690 
   691 
   692 subsection {* Further operations on finite types *}
   693 
   694 lemma Collect_code [code]:
   695   "Collect P = set (filter P enum)"
   696 by (auto simp add: enum_UNIV)
   697 
   698 lemma [code]:
   699   "Id = image (%x. (x, x)) (set Enum.enum)"
   700 by (auto intro: imageI in_enum)
   701 
   702 lemma tranclp_unfold [code, no_atp]:
   703   "tranclp r a b \<equiv> (a, b) \<in> trancl {(x, y). r x y}"
   704 by (simp add: trancl_def)
   705 
   706 lemma rtranclp_rtrancl_eq [code, no_atp]:
   707   "rtranclp r x y = ((x, y) : rtrancl {(x, y). r x y})"
   708 unfolding rtrancl_def by auto
   709 
   710 lemma max_ext_eq [code]:
   711   "max_ext R = {(X, Y). finite X & finite Y & Y ~={} & (ALL x. x : X --> (EX xa : Y. (x, xa) : R))}"
   712 by (auto simp add: max_ext.simps)
   713 
   714 lemma max_extp_eq[code]:
   715   "max_extp r x y = ((x, y) : max_ext {(x, y). r x y})"
   716 unfolding max_ext_def by auto
   717 
   718 lemma mlex_eq[code]:
   719   "f <*mlex*> R = {(x, y). f x < f y \<or> (f x <= f y \<and> (x, y) : R)}"
   720 unfolding mlex_prod_def by auto
   721 
   722 subsection {* Executable accessible part *}
   723 
   724 definition 
   725   [code del]: "card_UNIV = card UNIV"
   726 
   727 lemma [code]:
   728   "card_UNIV TYPE('a :: enum) = card (set (Enum.enum :: 'a list))"
   729   unfolding card_UNIV_def enum_UNIV ..
   730 
   731 lemma [code]:
   732   fixes xs :: "('a::finite \<times> 'a) list"
   733   shows "acc (set xs) = bacc (set xs) (card_UNIV TYPE('a))"
   734   by (simp add: card_UNIV_def acc_bacc_eq)
   735 
   736 lemma [code_unfold]: "accp r = (\<lambda>x. x \<in> acc {(x, y). r x y})"
   737   unfolding acc_def by simp
   738 
   739 subsection {* Closing up *}
   740 
   741 hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
   742 hide_const (open) enum enum_all enum_ex all_n_lists ex_n_lists ntrancl
   743 
   744 end
   745