src/HOL/Enum.thy
author bulwahn
Mon Jan 30 13:55:20 2012 +0100 (2012-01-30)
changeset 46357 2795607a1f40
parent 46352 73b03235799a
child 46358 b2a936486685
permissions -rw-r--r--
adding code equation for tranclp
     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 unfolding list_ex1_iff enum_UNIV by auto
    78 
    79 
    80 subsection {* Default instances *}
    81 
    82 primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where
    83   "n_lists 0 xs = [[]]"
    84   | "n_lists (Suc n) xs = concat (map (\<lambda>ys. map (\<lambda>y. y # ys) xs) (n_lists n xs))"
    85 
    86 lemma n_lists_Nil [simp]: "n_lists n [] = (if n = 0 then [[]] else [])"
    87   by (induct n) simp_all
    88 
    89 lemma length_n_lists: "length (n_lists n xs) = length xs ^ n"
    90   by (induct n) (auto simp add: length_concat o_def listsum_triv)
    91 
    92 lemma length_n_lists_elem: "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
    93   by (induct n arbitrary: ys) auto
    94 
    95 lemma set_n_lists: "set (n_lists n xs) = {ys. length ys = n \<and> set ys \<subseteq> set xs}"
    96 proof (rule set_eqI)
    97   fix ys :: "'a list"
    98   show "ys \<in> set (n_lists n xs) \<longleftrightarrow> ys \<in> {ys. length ys = n \<and> set ys \<subseteq> set xs}"
    99   proof -
   100     have "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
   101       by (induct n arbitrary: ys) auto
   102     moreover have "\<And>x. ys \<in> set (n_lists n xs) \<Longrightarrow> x \<in> set ys \<Longrightarrow> x \<in> set xs"
   103       by (induct n arbitrary: ys) auto
   104     moreover have "set ys \<subseteq> set xs \<Longrightarrow> ys \<in> set (n_lists (length ys) xs)"
   105       by (induct ys) auto
   106     ultimately show ?thesis by auto
   107   qed
   108 qed
   109 
   110 lemma distinct_n_lists:
   111   assumes "distinct xs"
   112   shows "distinct (n_lists n xs)"
   113 proof (rule card_distinct)
   114   from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)
   115   have "card (set (n_lists n xs)) = card (set xs) ^ n"
   116   proof (induct n)
   117     case 0 then show ?case by simp
   118   next
   119     case (Suc n)
   120     moreover have "card (\<Union>ys\<in>set (n_lists n xs). (\<lambda>y. y # ys) ` set xs)
   121       = (\<Sum>ys\<in>set (n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))"
   122       by (rule card_UN_disjoint) auto
   123     moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)"
   124       by (rule card_image) (simp add: inj_on_def)
   125     ultimately show ?case by auto
   126   qed
   127   also have "\<dots> = length xs ^ n" by (simp add: card_length)
   128   finally show "card (set (n_lists n xs)) = length (n_lists n xs)"
   129     by (simp add: length_n_lists)
   130 qed
   131 
   132 lemma map_of_zip_enum_is_Some:
   133   assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
   134   shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
   135 proof -
   136   from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
   137     (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
   138     by (auto intro!: map_of_zip_is_Some)
   139   then show ?thesis using enum_UNIV by auto
   140 qed
   141 
   142 lemma map_of_zip_enum_inject:
   143   fixes xs ys :: "'b\<Colon>enum list"
   144   assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
   145       "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
   146     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)"
   147   shows "xs = ys"
   148 proof -
   149   have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
   150   proof
   151     fix x :: 'a
   152     from length map_of_zip_enum_is_Some obtain y1 y2
   153       where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
   154         and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
   155     moreover from map_of 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)"
   156       by (auto dest: fun_cong)
   157     ultimately show "map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x = map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x"
   158       by simp
   159   qed
   160   with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
   161 qed
   162 
   163 definition
   164   all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
   165 where
   166   "all_n_lists P n = (\<forall>xs \<in> set (n_lists n enum). P xs)"
   167 
   168 lemma [code]:
   169   "all_n_lists P n = (if n = 0 then P [] else enum_all (%x. all_n_lists (%xs. P (x # xs)) (n - 1)))"
   170 unfolding all_n_lists_def enum_all
   171 by (cases n) (auto simp add: enum_UNIV)
   172 
   173 definition
   174   ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
   175 where
   176   "ex_n_lists P n = (\<exists>xs \<in> set (n_lists n enum). P xs)"
   177 
   178 lemma [code]:
   179   "ex_n_lists P n = (if n = 0 then P [] else enum_ex (%x. ex_n_lists (%xs. P (x # xs)) (n - 1)))"
   180 unfolding ex_n_lists_def enum_ex
   181 by (cases n) (auto simp add: enum_UNIV)
   182 
   183 
   184 instantiation "fun" :: (enum, enum) enum
   185 begin
   186 
   187 definition
   188   "enum = map (\<lambda>ys. the o map_of (zip (enum\<Colon>'a list) ys)) (n_lists (length (enum\<Colon>'a\<Colon>enum list)) enum)"
   189 
   190 definition
   191   "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
   192 
   193 definition
   194   "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
   195 
   196 
   197 instance proof
   198   show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
   199   proof (rule UNIV_eq_I)
   200     fix f :: "'a \<Rightarrow> 'b"
   201     have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
   202       by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
   203     then show "f \<in> set enum"
   204       by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
   205   qed
   206 next
   207   from map_of_zip_enum_inject
   208   show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
   209     by (auto intro!: inj_onI simp add: enum_fun_def
   210       distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
   211 next
   212   fix P
   213   show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
   214   proof
   215     assume "enum_all P"
   216     show "\<forall>x. P x"
   217     proof
   218       fix f :: "'a \<Rightarrow> 'b"
   219       have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
   220         by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
   221       from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
   222         unfolding enum_all_fun_def all_n_lists_def
   223         apply (simp add: set_n_lists)
   224         apply (erule_tac x="map f enum" in allE)
   225         apply (auto intro!: in_enum)
   226         done
   227       from this f show "P f" by auto
   228     qed
   229   next
   230     assume "\<forall>x. P x"
   231     from this show "enum_all P"
   232       unfolding enum_all_fun_def all_n_lists_def by auto
   233   qed
   234 next
   235   fix P
   236   show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
   237   proof
   238     assume "enum_ex P"
   239     from this show "\<exists>x. P x"
   240       unfolding enum_ex_fun_def ex_n_lists_def by auto
   241   next
   242     assume "\<exists>x. P x"
   243     from this obtain f where "P f" ..
   244     have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
   245       by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum) 
   246     from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
   247       by auto
   248     from  this show "enum_ex P"
   249       unfolding enum_ex_fun_def ex_n_lists_def
   250       apply (auto simp add: set_n_lists)
   251       apply (rule_tac x="map f enum" in exI)
   252       apply (auto intro!: in_enum)
   253       done
   254   qed
   255 qed
   256 
   257 end
   258 
   259 lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
   260   in map (\<lambda>ys. the o map_of (zip enum_a ys)) (n_lists (length enum_a) enum))"
   261   by (simp add: enum_fun_def Let_def)
   262 
   263 lemma enum_all_fun_code [code]:
   264   "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
   265    in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
   266   by (simp add: enum_all_fun_def Let_def)
   267 
   268 lemma enum_ex_fun_code [code]:
   269   "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
   270    in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
   271   by (simp add: enum_ex_fun_def Let_def)
   272 
   273 instantiation unit :: enum
   274 begin
   275 
   276 definition
   277   "enum = [()]"
   278 
   279 definition
   280   "enum_all P = P ()"
   281 
   282 definition
   283   "enum_ex P = P ()"
   284 
   285 instance proof
   286 qed (auto simp add: enum_unit_def UNIV_unit enum_all_unit_def enum_ex_unit_def intro: unit.exhaust)
   287 
   288 end
   289 
   290 instantiation bool :: enum
   291 begin
   292 
   293 definition
   294   "enum = [False, True]"
   295 
   296 definition
   297   "enum_all P = (P False \<and> P True)"
   298 
   299 definition
   300   "enum_ex P = (P False \<or> P True)"
   301 
   302 instance proof
   303   fix P
   304   show "enum_all (P :: bool \<Rightarrow> bool) = (\<forall>x. P x)"
   305     unfolding enum_all_bool_def by (auto, case_tac x) auto
   306 next
   307   fix P
   308   show "enum_ex (P :: bool \<Rightarrow> bool) = (\<exists>x. P x)"
   309     unfolding enum_ex_bool_def by (auto, case_tac x) auto
   310 qed (auto simp add: enum_bool_def UNIV_bool)
   311 
   312 end
   313 
   314 primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
   315   "product [] _ = []"
   316   | "product (x#xs) ys = map (Pair x) ys @ product xs ys"
   317 
   318 lemma product_list_set:
   319   "set (product xs ys) = set xs \<times> set ys"
   320   by (induct xs) auto
   321 
   322 lemma distinct_product:
   323   assumes "distinct xs" and "distinct ys"
   324   shows "distinct (product xs ys)"
   325   using assms by (induct xs)
   326     (auto intro: inj_onI simp add: product_list_set distinct_map)
   327 
   328 instantiation prod :: (enum, enum) enum
   329 begin
   330 
   331 definition
   332   "enum = product enum enum"
   333 
   334 definition
   335   "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
   336 
   337 definition
   338   "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
   339 
   340  
   341 instance by default
   342   (simp_all add: enum_prod_def product_list_set distinct_product
   343     enum_UNIV enum_distinct enum_all_prod_def enum_all enum_ex_prod_def enum_ex)
   344 
   345 end
   346 
   347 instantiation sum :: (enum, enum) enum
   348 begin
   349 
   350 definition
   351   "enum = map Inl enum @ map Inr enum"
   352 
   353 definition
   354   "enum_all P = (enum_all (%x. P (Inl x)) \<and> enum_all (%x. P (Inr x)))"
   355 
   356 definition
   357   "enum_ex P = (enum_ex (%x. P (Inl x)) \<or> enum_ex (%x. P (Inr x)))"
   358 
   359 instance proof
   360   fix P
   361   show "enum_all (P :: ('a + 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
   362     unfolding enum_all_sum_def enum_all
   363     by (auto, case_tac x) auto
   364 next
   365   fix P
   366   show "enum_ex (P :: ('a + 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
   367     unfolding enum_ex_sum_def enum_ex
   368     by (auto, case_tac x) auto
   369 qed (auto simp add: enum_UNIV enum_sum_def, case_tac x, auto intro: inj_onI simp add: distinct_map enum_distinct)
   370 
   371 end
   372 
   373 instantiation nibble :: enum
   374 begin
   375 
   376 definition
   377   "enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
   378     Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
   379 
   380 definition
   381   "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
   382      \<and> P Nibble8 \<and> P Nibble9 \<and> P NibbleA \<and> P NibbleB \<and> P NibbleC \<and> P NibbleD \<and> P NibbleE \<and> P NibbleF)"
   383 
   384 definition
   385   "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
   386      \<or> P Nibble8 \<or> P Nibble9 \<or> P NibbleA \<or> P NibbleB \<or> P NibbleC \<or> P NibbleD \<or> P NibbleE \<or> P NibbleF)"
   387 
   388 instance proof
   389   fix P
   390   show "enum_all (P :: nibble \<Rightarrow> bool) = (\<forall>x. P x)"
   391     unfolding enum_all_nibble_def
   392     by (auto, case_tac x) auto
   393 next
   394   fix P
   395   show "enum_ex (P :: nibble \<Rightarrow> bool) = (\<exists>x. P x)"
   396     unfolding enum_ex_nibble_def
   397     by (auto, case_tac x) auto
   398 qed (simp_all add: enum_nibble_def UNIV_nibble)
   399 
   400 end
   401 
   402 instantiation char :: enum
   403 begin
   404 
   405 definition
   406   "enum = map (split Char) (product enum enum)"
   407 
   408 lemma enum_chars [code]:
   409   "enum = chars"
   410   unfolding enum_char_def chars_def enum_nibble_def by simp
   411 
   412 definition
   413   "enum_all P = list_all P chars"
   414 
   415 definition
   416   "enum_ex P = list_ex P chars"
   417 
   418 lemma set_enum_char: "set (enum :: char list) = UNIV"
   419     by (auto intro: char.exhaust simp add: enum_char_def product_list_set enum_UNIV full_SetCompr_eq [symmetric])
   420 
   421 instance proof
   422   fix P
   423   show "enum_all (P :: char \<Rightarrow> bool) = (\<forall>x. P x)"
   424     unfolding enum_all_char_def enum_chars[symmetric]
   425     by (auto simp add: list_all_iff set_enum_char)
   426 next
   427   fix P
   428   show "enum_ex (P :: char \<Rightarrow> bool) = (\<exists>x. P x)"
   429     unfolding enum_ex_char_def enum_chars[symmetric]
   430     by (auto simp add: list_ex_iff set_enum_char)
   431 next
   432   show "distinct (enum :: char list)"
   433     by (auto intro: inj_onI simp add: enum_char_def product_list_set distinct_map distinct_product enum_distinct)
   434 qed (auto simp add: set_enum_char)
   435 
   436 end
   437 
   438 instantiation option :: (enum) enum
   439 begin
   440 
   441 definition
   442   "enum = None # map Some enum"
   443 
   444 definition
   445   "enum_all P = (P None \<and> enum_all (%x. P (Some x)))"
   446 
   447 definition
   448   "enum_ex P = (P None \<or> enum_ex (%x. P (Some x)))"
   449 
   450 instance proof
   451   fix P
   452   show "enum_all (P :: 'a option \<Rightarrow> bool) = (\<forall>x. P x)"
   453     unfolding enum_all_option_def enum_all
   454     by (auto, case_tac x) auto
   455 next
   456   fix P
   457   show "enum_ex (P :: 'a option \<Rightarrow> bool) = (\<exists>x. P x)"
   458     unfolding enum_ex_option_def enum_ex
   459     by (auto, case_tac x) auto
   460 qed (auto simp add: enum_UNIV enum_option_def, rule option.exhaust, auto intro: simp add: distinct_map enum_distinct)
   461 end
   462 
   463 primrec sublists :: "'a list \<Rightarrow> 'a list list" where
   464   "sublists [] = [[]]"
   465   | "sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"
   466 
   467 lemma length_sublists:
   468   "length (sublists xs) = Suc (Suc (0\<Colon>nat)) ^ length xs"
   469   by (induct xs) (simp_all add: Let_def)
   470 
   471 lemma sublists_powset:
   472   "set ` set (sublists xs) = Pow (set xs)"
   473 proof -
   474   have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A"
   475     by (auto simp add: image_def)
   476   have "set (map set (sublists xs)) = Pow (set xs)"
   477     by (induct xs)
   478       (simp_all add: aux Let_def Pow_insert Un_commute comp_def del: map_map)
   479   then show ?thesis by simp
   480 qed
   481 
   482 lemma distinct_set_sublists:
   483   assumes "distinct xs"
   484   shows "distinct (map set (sublists xs))"
   485 proof (rule card_distinct)
   486   have "finite (set xs)" by rule
   487   then have "card (Pow (set xs)) = Suc (Suc 0) ^ card (set xs)" by (rule card_Pow)
   488   with assms distinct_card [of xs]
   489     have "card (Pow (set xs)) = Suc (Suc 0) ^ length xs" by simp
   490   then show "card (set (map set (sublists xs))) = length (map set (sublists xs))"
   491     by (simp add: sublists_powset length_sublists)
   492 qed
   493 
   494 instantiation set :: (enum) enum
   495 begin
   496 
   497 definition
   498   "enum = map set (sublists enum)"
   499 
   500 definition
   501   "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
   502 
   503 definition
   504   "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
   505 
   506 instance proof
   507 qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
   508   enum_distinct enum_UNIV)
   509 
   510 end
   511 
   512 
   513 subsection {* Small finite types *}
   514 
   515 text {* We define small finite types for the use in Quickcheck *}
   516 
   517 datatype finite_1 = a\<^isub>1
   518 
   519 notation (output) a\<^isub>1  ("a\<^isub>1")
   520 
   521 instantiation finite_1 :: enum
   522 begin
   523 
   524 definition
   525   "enum = [a\<^isub>1]"
   526 
   527 definition
   528   "enum_all P = P a\<^isub>1"
   529 
   530 definition
   531   "enum_ex P = P a\<^isub>1"
   532 
   533 instance proof
   534   fix P
   535   show "enum_all (P :: finite_1 \<Rightarrow> bool) = (\<forall>x. P x)"
   536     unfolding enum_all_finite_1_def
   537     by (auto, case_tac x) auto
   538 next
   539   fix P
   540   show "enum_ex (P :: finite_1 \<Rightarrow> bool) = (\<exists>x. P x)"
   541     unfolding enum_ex_finite_1_def
   542     by (auto, case_tac x) auto
   543 qed (auto simp add: enum_finite_1_def intro: finite_1.exhaust)
   544 
   545 end
   546 
   547 instantiation finite_1 :: linorder
   548 begin
   549 
   550 definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
   551 where
   552   "less_eq_finite_1 x y = True"
   553 
   554 definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
   555 where
   556   "less_finite_1 x y = False"
   557 
   558 instance
   559 apply (intro_classes)
   560 apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
   561 apply (metis finite_1.exhaust)
   562 done
   563 
   564 end
   565 
   566 hide_const (open) a\<^isub>1
   567 
   568 datatype finite_2 = a\<^isub>1 | a\<^isub>2
   569 
   570 notation (output) a\<^isub>1  ("a\<^isub>1")
   571 notation (output) a\<^isub>2  ("a\<^isub>2")
   572 
   573 instantiation finite_2 :: enum
   574 begin
   575 
   576 definition
   577   "enum = [a\<^isub>1, a\<^isub>2]"
   578 
   579 definition
   580   "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2)"
   581 
   582 definition
   583   "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2)"
   584 
   585 instance proof
   586   fix P
   587   show "enum_all (P :: finite_2 \<Rightarrow> bool) = (\<forall>x. P x)"
   588     unfolding enum_all_finite_2_def
   589     by (auto, case_tac x) auto
   590 next
   591   fix P
   592   show "enum_ex (P :: finite_2 \<Rightarrow> bool) = (\<exists>x. P x)"
   593     unfolding enum_ex_finite_2_def
   594     by (auto, case_tac x) auto
   595 qed (auto simp add: enum_finite_2_def intro: finite_2.exhaust)
   596 
   597 end
   598 
   599 instantiation finite_2 :: linorder
   600 begin
   601 
   602 definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
   603 where
   604   "less_finite_2 x y = ((x = a\<^isub>1) & (y = a\<^isub>2))"
   605 
   606 definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
   607 where
   608   "less_eq_finite_2 x y = ((x = y) \<or> (x < y))"
   609 
   610 
   611 instance
   612 apply (intro_classes)
   613 apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
   614 apply (metis finite_2.distinct finite_2.nchotomy)+
   615 done
   616 
   617 end
   618 
   619 hide_const (open) a\<^isub>1 a\<^isub>2
   620 
   621 
   622 datatype finite_3 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3
   623 
   624 notation (output) a\<^isub>1  ("a\<^isub>1")
   625 notation (output) a\<^isub>2  ("a\<^isub>2")
   626 notation (output) a\<^isub>3  ("a\<^isub>3")
   627 
   628 instantiation finite_3 :: enum
   629 begin
   630 
   631 definition
   632   "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3]"
   633 
   634 definition
   635   "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3)"
   636 
   637 definition
   638   "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3)"
   639 
   640 instance proof
   641   fix P
   642   show "enum_all (P :: finite_3 \<Rightarrow> bool) = (\<forall>x. P x)"
   643     unfolding enum_all_finite_3_def
   644     by (auto, case_tac x) auto
   645 next
   646   fix P
   647   show "enum_ex (P :: finite_3 \<Rightarrow> bool) = (\<exists>x. P x)"
   648     unfolding enum_ex_finite_3_def
   649     by (auto, case_tac x) auto
   650 qed (auto simp add: enum_finite_3_def intro: finite_3.exhaust)
   651 
   652 end
   653 
   654 instantiation finite_3 :: linorder
   655 begin
   656 
   657 definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
   658 where
   659   "less_finite_3 x y = (case x of a\<^isub>1 => (y \<noteq> a\<^isub>1)
   660      | a\<^isub>2 => (y = a\<^isub>3)| a\<^isub>3 => False)"
   661 
   662 definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
   663 where
   664   "less_eq_finite_3 x y = ((x = y) \<or> (x < y))"
   665 
   666 
   667 instance proof (intro_classes)
   668 qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
   669 
   670 end
   671 
   672 hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3
   673 
   674 
   675 datatype finite_4 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4
   676 
   677 notation (output) a\<^isub>1  ("a\<^isub>1")
   678 notation (output) a\<^isub>2  ("a\<^isub>2")
   679 notation (output) a\<^isub>3  ("a\<^isub>3")
   680 notation (output) a\<^isub>4  ("a\<^isub>4")
   681 
   682 instantiation finite_4 :: enum
   683 begin
   684 
   685 definition
   686   "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4]"
   687 
   688 definition
   689   "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4)"
   690 
   691 definition
   692   "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4)"
   693 
   694 instance proof
   695   fix P
   696   show "enum_all (P :: finite_4 \<Rightarrow> bool) = (\<forall>x. P x)"
   697     unfolding enum_all_finite_4_def
   698     by (auto, case_tac x) auto
   699 next
   700   fix P
   701   show "enum_ex (P :: finite_4 \<Rightarrow> bool) = (\<exists>x. P x)"
   702     unfolding enum_ex_finite_4_def
   703     by (auto, case_tac x) auto
   704 qed (auto simp add: enum_finite_4_def intro: finite_4.exhaust)
   705 
   706 end
   707 
   708 hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4
   709 
   710 
   711 datatype finite_5 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4 | a\<^isub>5
   712 
   713 notation (output) a\<^isub>1  ("a\<^isub>1")
   714 notation (output) a\<^isub>2  ("a\<^isub>2")
   715 notation (output) a\<^isub>3  ("a\<^isub>3")
   716 notation (output) a\<^isub>4  ("a\<^isub>4")
   717 notation (output) a\<^isub>5  ("a\<^isub>5")
   718 
   719 instantiation finite_5 :: enum
   720 begin
   721 
   722 definition
   723   "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5]"
   724 
   725 definition
   726   "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)"
   727 
   728 definition
   729   "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)"
   730 
   731 instance proof
   732   fix P
   733   show "enum_all (P :: finite_5 \<Rightarrow> bool) = (\<forall>x. P x)"
   734     unfolding enum_all_finite_5_def
   735     by (auto, case_tac x) auto
   736 next
   737   fix P
   738   show "enum_ex (P :: finite_5 \<Rightarrow> bool) = (\<exists>x. P x)"
   739     unfolding enum_ex_finite_5_def
   740     by (auto, case_tac x) auto
   741 qed (auto simp add: enum_finite_5_def intro: finite_5.exhaust)
   742 
   743 end
   744 
   745 hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4 a\<^isub>5
   746 
   747 subsection {* An executable THE operator on finite types *}
   748 
   749 definition
   750   [code del]: "enum_the P = The P"
   751 
   752 lemma [code]:
   753   "The P = (case filter P enum of [x] => x | _ => enum_the P)"
   754 proof -
   755   {
   756     fix a
   757     assume filter_enum: "filter P enum = [a]"
   758     have "The P = a"
   759     proof (rule the_equality)
   760       fix x
   761       assume "P x"
   762       show "x = a"
   763       proof (rule ccontr)
   764         assume "x \<noteq> a"
   765         from filter_enum obtain us vs
   766           where enum_eq: "enum = us @ [a] @ vs"
   767           and "\<forall> x \<in> set us. \<not> P x"
   768           and "\<forall> x \<in> set vs. \<not> P x"
   769           and "P a"
   770           by (auto simp add: filter_eq_Cons_iff) (simp only: filter_empty_conv[symmetric])
   771         with `P x` in_enum[of x, unfolded enum_eq] `x \<noteq> a` show "False" by auto
   772       qed
   773     next
   774       from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
   775     qed
   776   }
   777   from this show ?thesis
   778     unfolding enum_the_def by (auto split: list.split)
   779 qed
   780 
   781 code_abort enum_the
   782 code_const enum_the (Eval "(fn p => raise Match)")
   783 
   784 subsection {* Further operations on finite types *}
   785 
   786 lemma [code]:
   787   "Collect P = set (filter P enum)"
   788 by (auto simp add: enum_UNIV)
   789 
   790 lemma tranclp_unfold [code, no_atp]:
   791   "tranclp r a b \<equiv> (a, b) \<in> trancl {(x, y). r x y}"
   792 by (simp add: trancl_def)
   793 
   794 subsection {* Executable accessible part *}
   795 (* FIXME: should be moved somewhere else !? *)
   796 
   797 subsubsection {* Finite monotone eventually stable sequences *}
   798 
   799 lemma finite_mono_remains_stable_implies_strict_prefix:
   800   fixes f :: "nat \<Rightarrow> 'a::order"
   801   assumes S: "finite (range f)" "mono f" and eq: "\<forall>n. f n = f (Suc n) \<longrightarrow> f (Suc n) = f (Suc (Suc n))"
   802   shows "\<exists>N. (\<forall>n\<le>N. \<forall>m\<le>N. m < n \<longrightarrow> f m < f n) \<and> (\<forall>n\<ge>N. f N = f n)"
   803   using assms
   804 proof -
   805   have "\<exists>n. f n = f (Suc n)"
   806   proof (rule ccontr)
   807     assume "\<not> ?thesis"
   808     then have "\<And>n. f n \<noteq> f (Suc n)" by auto
   809     then have "\<And>n. f n < f (Suc n)"
   810       using  `mono f` by (auto simp: le_less mono_iff_le_Suc)
   811     with lift_Suc_mono_less_iff[of f]
   812     have "\<And>n m. n < m \<Longrightarrow> f n < f m" by auto
   813     then have "inj f"
   814       by (auto simp: inj_on_def) (metis linorder_less_linear order_less_imp_not_eq)
   815     with `finite (range f)` have "finite (UNIV::nat set)"
   816       by (rule finite_imageD)
   817     then show False by simp
   818   qed
   819   then guess n .. note n = this
   820   def N \<equiv> "LEAST n. f n = f (Suc n)"
   821   have N: "f N = f (Suc N)"
   822     unfolding N_def using n by (rule LeastI)
   823   show ?thesis
   824   proof (intro exI[of _ N] conjI allI impI)
   825     fix n assume "N \<le> n"
   826     then have "\<And>m. N \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> f m = f N"
   827     proof (induct rule: dec_induct)
   828       case (step n) then show ?case
   829         using eq[rule_format, of "n - 1"] N
   830         by (cases n) (auto simp add: le_Suc_eq)
   831     qed simp
   832     from this[of n] `N \<le> n` show "f N = f n" by auto
   833   next
   834     fix n m :: nat assume "m < n" "n \<le> N"
   835     then show "f m < f n"
   836     proof (induct rule: less_Suc_induct[consumes 1])
   837       case (1 i)
   838       then have "i < N" by simp
   839       then have "f i \<noteq> f (Suc i)"
   840         unfolding N_def by (rule not_less_Least)
   841       with `mono f` show ?case by (simp add: mono_iff_le_Suc less_le)
   842     qed auto
   843   qed
   844 qed
   845 
   846 lemma finite_mono_strict_prefix_implies_finite_fixpoint:
   847   fixes f :: "nat \<Rightarrow> 'a set"
   848   assumes S: "\<And>i. f i \<subseteq> S" "finite S"
   849     and inj: "\<exists>N. (\<forall>n\<le>N. \<forall>m\<le>N. m < n \<longrightarrow> f m \<subset> f n) \<and> (\<forall>n\<ge>N. f N = f n)"
   850   shows "f (card S) = (\<Union>n. f n)"
   851 proof -
   852   from inj obtain N where inj: "(\<forall>n\<le>N. \<forall>m\<le>N. m < n \<longrightarrow> f m \<subset> f n)" and eq: "(\<forall>n\<ge>N. f N = f n)" by auto
   853 
   854   { fix i have "i \<le> N \<Longrightarrow> i \<le> card (f i)"
   855     proof (induct i)
   856       case 0 then show ?case by simp
   857     next
   858       case (Suc i)
   859       with inj[rule_format, of "Suc i" i]
   860       have "(f i) \<subset> (f (Suc i))" by auto
   861       moreover have "finite (f (Suc i))" using S by (rule finite_subset)
   862       ultimately have "card (f i) < card (f (Suc i))" by (intro psubset_card_mono)
   863       with Suc show ?case using inj by auto
   864     qed
   865   }
   866   then have "N \<le> card (f N)" by simp
   867   also have "\<dots> \<le> card S" using S by (intro card_mono)
   868   finally have "f (card S) = f N" using eq by auto
   869   then show ?thesis using eq inj[rule_format, of N]
   870     apply auto
   871     apply (case_tac "n < N")
   872     apply (auto simp: not_less)
   873     done
   874 qed
   875 
   876 subsubsection {* Bounded accessible part *}
   877 
   878 fun bacc :: "('a * 'a) set => nat => 'a set" 
   879 where
   880   "bacc r 0 = {x. \<forall> y. (y, x) \<notin> r}"
   881 | "bacc r (Suc n) = (bacc r n Un {x. \<forall> y. (y, x) : r --> y : bacc r n})"
   882 
   883 lemma bacc_subseteq_acc:
   884   "bacc r n \<subseteq> acc r"
   885 by (induct n) (auto intro: acc.intros)
   886 
   887 lemma bacc_mono:
   888   "n <= m ==> bacc r n \<subseteq> bacc r m"
   889 by (induct rule: dec_induct) auto
   890   
   891 lemma bacc_upper_bound:
   892   "bacc (r :: ('a * 'a) set)  (card (UNIV :: ('a :: enum) set)) = (UN n. bacc r n)"
   893 proof -
   894   have "mono (bacc r)" unfolding mono_def by (simp add: bacc_mono)
   895   moreover have "\<forall>n. bacc r n = bacc r (Suc n) \<longrightarrow> bacc r (Suc n) = bacc r (Suc (Suc n))" by auto
   896   moreover have "finite (range (bacc r))" by auto
   897   ultimately show ?thesis
   898    by (intro finite_mono_strict_prefix_implies_finite_fixpoint)
   899      (auto intro: finite_mono_remains_stable_implies_strict_prefix  simp add: enum_UNIV)
   900 qed
   901 
   902 lemma acc_subseteq_bacc:
   903   assumes "finite r"
   904   shows "acc r \<subseteq> (UN n. bacc r n)"
   905 proof
   906   fix x
   907   assume "x : acc r"
   908   from this have "\<exists> n. x : bacc r n"
   909   proof (induct x arbitrary: n rule: acc.induct)
   910     case (accI x)
   911     from accI have "\<forall> y. \<exists> n. (y, x) \<in> r --> y : bacc r n" by simp
   912     from choice[OF this] guess n .. note n = this
   913     have "\<exists> n. \<forall>y. (y, x) : r --> y : bacc r n"
   914     proof (safe intro!: exI[of _ "Max ((%(y,x). n y)`r)"])
   915       fix y assume y: "(y, x) : r"
   916       with n have "y : bacc r (n y)" by auto
   917       moreover have "n y <= Max ((%(y, x). n y) ` r)"
   918         using y `finite r` by (auto intro!: Max_ge)
   919       note bacc_mono[OF this, of r]
   920       ultimately show "y : bacc r (Max ((%(y, x). n y) ` r))" by auto
   921     qed
   922     from this guess n ..
   923     from this show ?case
   924       by (auto simp add: Let_def intro!: exI[of _ "Suc n"])
   925   qed
   926   thus "x : (UN n. bacc r n)" by auto
   927 qed
   928 
   929 lemma acc_bacc_eq: "acc ((set xs) :: (('a :: enum) * 'a) set) = bacc (set xs) (card (UNIV :: 'a set))"
   930 by (metis acc_subseteq_bacc bacc_subseteq_acc bacc_upper_bound finite_set order_eq_iff)
   931 
   932 definition 
   933   [code del]: "card_UNIV = card UNIV"
   934 
   935 lemma [code]:
   936   "card_UNIV TYPE('a :: enum) = card (set (Enum.enum :: 'a list))"
   937 unfolding card_UNIV_def enum_UNIV ..
   938 
   939 declare acc_bacc_eq[folded card_UNIV_def, code]
   940 
   941 lemma [code_unfold]: "accp r = (%x. x : acc {(x, y). r x y})"
   942 unfolding acc_def by simp
   943 
   944 subsection {* Closing up *}
   945 
   946 hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
   947 hide_const (open) enum enum_all enum_ex n_lists all_n_lists ex_n_lists product ntrancl
   948 
   949 end