src/HOL/Enum.thy
author bulwahn
Mon Nov 22 11:34:57 2010 +0100 (2010-11-22)
changeset 40651 9752ba7348b5
parent 40650 d40b347d5b0b
child 40652 7bdfc1d6b143
permissions -rw-r--r--
adding code equation for function equality; adding some instantiations for the finite types
     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   assumes UNIV_enum: "UNIV = set enum"
    14     and enum_distinct: "distinct enum"
    15 begin
    16 
    17 subclass finite proof
    18 qed (simp add: UNIV_enum)
    19 
    20 lemma enum_all: "set enum = UNIV" unfolding UNIV_enum ..
    21 
    22 lemma in_enum [intro]: "x \<in> set enum"
    23   unfolding enum_all by auto
    24 
    25 lemma enum_eq_I:
    26   assumes "\<And>x. x \<in> set xs"
    27   shows "set enum = set xs"
    28 proof -
    29   from assms UNIV_eq_I have "UNIV = set xs" by auto
    30   with enum_all show ?thesis by simp
    31 qed
    32 
    33 end
    34 
    35 
    36 subsection {* Equality and order on functions *}
    37 
    38 instantiation "fun" :: (enum, equal) equal
    39 begin
    40 
    41 definition
    42   "HOL.equal f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
    43 
    44 instance proof
    45 qed (simp_all add: equal_fun_def enum_all fun_eq_iff)
    46 
    47 end
    48 
    49 lemma [code nbe]:
    50   "HOL.equal (f :: _ \<Rightarrow> _) f \<longleftrightarrow> True"
    51   by (fact equal_refl)
    52 
    53 lemma [code]:
    54   "HOL.equal f g \<longleftrightarrow>  list_all (%x. f x = g x) enum"
    55 by (auto simp add: list_all_iff enum_all equal fun_eq_iff)
    56 
    57 lemma order_fun [code]:
    58   fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
    59   shows "f \<le> g \<longleftrightarrow> list_all (\<lambda>x. f x \<le> g x) enum"
    60     and "f < g \<longleftrightarrow> f \<le> g \<and> list_ex (\<lambda>x. f x \<noteq> g x) enum"
    61   by (simp_all add: list_all_iff list_ex_iff enum_all fun_eq_iff le_fun_def order_less_le)
    62 
    63 
    64 subsection {* Quantifiers *}
    65 
    66 lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> list_all P enum"
    67   by (simp add: list_all_iff enum_all)
    68 
    69 lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> list_ex P enum"
    70   by (simp add: list_ex_iff enum_all)
    71 
    72 
    73 subsection {* Default instances *}
    74 
    75 primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where
    76   "n_lists 0 xs = [[]]"
    77   | "n_lists (Suc n) xs = concat (map (\<lambda>ys. map (\<lambda>y. y # ys) xs) (n_lists n xs))"
    78 
    79 lemma n_lists_Nil [simp]: "n_lists n [] = (if n = 0 then [[]] else [])"
    80   by (induct n) simp_all
    81 
    82 lemma length_n_lists: "length (n_lists n xs) = length xs ^ n"
    83   by (induct n) (auto simp add: length_concat o_def listsum_triv)
    84 
    85 lemma length_n_lists_elem: "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
    86   by (induct n arbitrary: ys) auto
    87 
    88 lemma set_n_lists: "set (n_lists n xs) = {ys. length ys = n \<and> set ys \<subseteq> set xs}"
    89 proof (rule set_eqI)
    90   fix ys :: "'a list"
    91   show "ys \<in> set (n_lists n xs) \<longleftrightarrow> ys \<in> {ys. length ys = n \<and> set ys \<subseteq> set xs}"
    92   proof -
    93     have "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
    94       by (induct n arbitrary: ys) auto
    95     moreover have "\<And>x. ys \<in> set (n_lists n xs) \<Longrightarrow> x \<in> set ys \<Longrightarrow> x \<in> set xs"
    96       by (induct n arbitrary: ys) auto
    97     moreover have "set ys \<subseteq> set xs \<Longrightarrow> ys \<in> set (n_lists (length ys) xs)"
    98       by (induct ys) auto
    99     ultimately show ?thesis by auto
   100   qed
   101 qed
   102 
   103 lemma distinct_n_lists:
   104   assumes "distinct xs"
   105   shows "distinct (n_lists n xs)"
   106 proof (rule card_distinct)
   107   from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)
   108   have "card (set (n_lists n xs)) = card (set xs) ^ n"
   109   proof (induct n)
   110     case 0 then show ?case by simp
   111   next
   112     case (Suc n)
   113     moreover have "card (\<Union>ys\<in>set (n_lists n xs). (\<lambda>y. y # ys) ` set xs)
   114       = (\<Sum>ys\<in>set (n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))"
   115       by (rule card_UN_disjoint) auto
   116     moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)"
   117       by (rule card_image) (simp add: inj_on_def)
   118     ultimately show ?case by auto
   119   qed
   120   also have "\<dots> = length xs ^ n" by (simp add: card_length)
   121   finally show "card (set (n_lists n xs)) = length (n_lists n xs)"
   122     by (simp add: length_n_lists)
   123 qed
   124 
   125 lemma map_of_zip_enum_is_Some:
   126   assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
   127   shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
   128 proof -
   129   from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
   130     (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
   131     by (auto intro!: map_of_zip_is_Some)
   132   then show ?thesis using enum_all by auto
   133 qed
   134 
   135 lemma map_of_zip_enum_inject:
   136   fixes xs ys :: "'b\<Colon>enum list"
   137   assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
   138       "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
   139     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)"
   140   shows "xs = ys"
   141 proof -
   142   have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
   143   proof
   144     fix x :: 'a
   145     from length map_of_zip_enum_is_Some obtain y1 y2
   146       where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
   147         and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
   148     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)"
   149       by (auto dest: fun_cong)
   150     ultimately show "map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x = map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x"
   151       by simp
   152   qed
   153   with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
   154 qed
   155 
   156 instantiation "fun" :: (enum, enum) enum
   157 begin
   158 
   159 definition
   160   "enum = map (\<lambda>ys. the o map_of (zip (enum\<Colon>'a list) ys)) (n_lists (length (enum\<Colon>'a\<Colon>enum list)) enum)"
   161 
   162 instance proof
   163   show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
   164   proof (rule UNIV_eq_I)
   165     fix f :: "'a \<Rightarrow> 'b"
   166     have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
   167       by (auto simp add: map_of_zip_map fun_eq_iff)
   168     then show "f \<in> set enum"
   169       by (auto simp add: enum_fun_def set_n_lists)
   170   qed
   171 next
   172   from map_of_zip_enum_inject
   173   show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
   174     by (auto intro!: inj_onI simp add: enum_fun_def
   175       distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
   176 qed
   177 
   178 end
   179 
   180 lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
   181   in map (\<lambda>ys. the o map_of (zip enum_a ys)) (n_lists (length enum_a) enum))"
   182   by (simp add: enum_fun_def Let_def)
   183 
   184 instantiation unit :: enum
   185 begin
   186 
   187 definition
   188   "enum = [()]"
   189 
   190 instance proof
   191 qed (simp_all add: enum_unit_def UNIV_unit)
   192 
   193 end
   194 
   195 instantiation bool :: enum
   196 begin
   197 
   198 definition
   199   "enum = [False, True]"
   200 
   201 instance proof
   202 qed (simp_all add: enum_bool_def UNIV_bool)
   203 
   204 end
   205 
   206 primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
   207   "product [] _ = []"
   208   | "product (x#xs) ys = map (Pair x) ys @ product xs ys"
   209 
   210 lemma product_list_set:
   211   "set (product xs ys) = set xs \<times> set ys"
   212   by (induct xs) auto
   213 
   214 lemma distinct_product:
   215   assumes "distinct xs" and "distinct ys"
   216   shows "distinct (product xs ys)"
   217   using assms by (induct xs)
   218     (auto intro: inj_onI simp add: product_list_set distinct_map)
   219 
   220 instantiation prod :: (enum, enum) enum
   221 begin
   222 
   223 definition
   224   "enum = product enum enum"
   225 
   226 instance by default
   227   (simp_all add: enum_prod_def product_list_set distinct_product enum_all enum_distinct)
   228 
   229 end
   230 
   231 instantiation sum :: (enum, enum) enum
   232 begin
   233 
   234 definition
   235   "enum = map Inl enum @ map Inr enum"
   236 
   237 instance by default
   238   (auto simp add: enum_all enum_sum_def, case_tac x, auto intro: inj_onI simp add: distinct_map enum_distinct)
   239 
   240 end
   241 
   242 primrec sublists :: "'a list \<Rightarrow> 'a list list" where
   243   "sublists [] = [[]]"
   244   | "sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"
   245 
   246 lemma length_sublists:
   247   "length (sublists xs) = Suc (Suc (0\<Colon>nat)) ^ length xs"
   248   by (induct xs) (simp_all add: Let_def)
   249 
   250 lemma sublists_powset:
   251   "set ` set (sublists xs) = Pow (set xs)"
   252 proof -
   253   have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A"
   254     by (auto simp add: image_def)
   255   have "set (map set (sublists xs)) = Pow (set xs)"
   256     by (induct xs)
   257       (simp_all add: aux Let_def Pow_insert Un_commute comp_def del: map_map)
   258   then show ?thesis by simp
   259 qed
   260 
   261 lemma distinct_set_sublists:
   262   assumes "distinct xs"
   263   shows "distinct (map set (sublists xs))"
   264 proof (rule card_distinct)
   265   have "finite (set xs)" by rule
   266   then have "card (Pow (set xs)) = Suc (Suc 0) ^ card (set xs)" by (rule card_Pow)
   267   with assms distinct_card [of xs]
   268     have "card (Pow (set xs)) = Suc (Suc 0) ^ length xs" by simp
   269   then show "card (set (map set (sublists xs))) = length (map set (sublists xs))"
   270     by (simp add: sublists_powset length_sublists)
   271 qed
   272 
   273 instantiation nibble :: enum
   274 begin
   275 
   276 definition
   277   "enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
   278     Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
   279 
   280 instance proof
   281 qed (simp_all add: enum_nibble_def UNIV_nibble)
   282 
   283 end
   284 
   285 instantiation char :: enum
   286 begin
   287 
   288 definition
   289   "enum = map (split Char) (product enum enum)"
   290 
   291 lemma enum_chars [code]:
   292   "enum = chars"
   293   unfolding enum_char_def chars_def enum_nibble_def by simp
   294 
   295 instance proof
   296 qed (auto intro: char.exhaust injI simp add: enum_char_def product_list_set enum_all full_SetCompr_eq [symmetric]
   297   distinct_map distinct_product enum_distinct)
   298 
   299 end
   300 
   301 instantiation option :: (enum) enum
   302 begin
   303 
   304 definition
   305   "enum = None # map Some enum"
   306 
   307 instance proof
   308 qed (auto simp add: enum_all enum_option_def, rule option.exhaust, auto intro: simp add: distinct_map enum_distinct)
   309 
   310 end
   311 
   312 subsection {* Small finite types *}
   313 
   314 text {* We define small finite types for the use in Quickcheck *}
   315 
   316 datatype finite_1 = a\<^isub>1
   317 
   318 instantiation finite_1 :: enum
   319 begin
   320 
   321 definition
   322   "enum = [a\<^isub>1]"
   323 
   324 instance proof
   325 qed (auto simp add: enum_finite_1_def intro: finite_1.exhaust)
   326 
   327 end
   328 
   329 instantiation finite_1 :: linorder
   330 begin
   331 
   332 definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
   333 where
   334   "less_eq_finite_1 x y = True"
   335 
   336 definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
   337 where
   338   "less_finite_1 x y = False"
   339 
   340 instance
   341 apply (intro_classes)
   342 apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
   343 apply (metis finite_1.exhaust)
   344 done
   345 
   346 end
   347 
   348 datatype finite_2 = a\<^isub>1 | a\<^isub>2
   349 
   350 instantiation finite_2 :: enum
   351 begin
   352 
   353 definition
   354   "enum = [a\<^isub>1, a\<^isub>2]"
   355 
   356 instance proof
   357 qed (auto simp add: enum_finite_2_def intro: finite_2.exhaust)
   358 
   359 end
   360 
   361 instantiation finite_2 :: linorder
   362 begin
   363 
   364 definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
   365 where
   366   "less_finite_2 x y = ((x = a\<^isub>1) & (y = a\<^isub>2))"
   367 
   368 definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
   369 where
   370   "less_eq_finite_2 x y = ((x = y) \<or> (x < y))"
   371 
   372 
   373 instance
   374 apply (intro_classes)
   375 apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
   376 apply (metis finite_2.distinct finite_2.nchotomy)+
   377 done
   378 
   379 end
   380 
   381 
   382 datatype finite_3 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3
   383 
   384 instantiation finite_3 :: enum
   385 begin
   386 
   387 definition
   388   "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3]"
   389 
   390 instance proof
   391 qed (auto simp add: enum_finite_3_def intro: finite_3.exhaust)
   392 
   393 end
   394 
   395 instantiation finite_3 :: linorder
   396 begin
   397 
   398 definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
   399 where
   400   "less_finite_3 x y = (case x of a\<^isub>1 => (y \<noteq> a\<^isub>1)
   401      | a\<^isub>2 => (y = a\<^isub>3)| a\<^isub>3 => False)"
   402 
   403 definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
   404 where
   405   "less_eq_finite_3 x y = ((x = y) \<or> (x < y))"
   406 
   407 
   408 instance proof (intro_classes)
   409 qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
   410 
   411 end
   412 
   413 
   414 datatype finite_4 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4
   415 
   416 instantiation finite_4 :: enum
   417 begin
   418 
   419 definition
   420   "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4]"
   421 
   422 instance proof
   423 qed (auto simp add: enum_finite_4_def intro: finite_4.exhaust)
   424 
   425 end
   426 
   427 
   428 
   429 datatype finite_5 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4 | a\<^isub>5
   430 
   431 instantiation finite_5 :: enum
   432 begin
   433 
   434 definition
   435   "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5]"
   436 
   437 instance proof
   438 qed (auto simp add: enum_finite_5_def intro: finite_5.exhaust)
   439 
   440 end
   441 
   442 hide_type finite_1 finite_2 finite_3 finite_4 finite_5
   443 hide_const (open) n_lists product
   444 
   445 end