src/HOL/Library/Enum.thy
changeset 28562 4e74209f113e
parent 28245 9767dd8e1e54
child 28684 48faac324061
equal deleted inserted replaced
28561:056255ade52a 28562:4e74209f113e
    11 
    11 
    12 subsection {* Class @{text enum} *}
    12 subsection {* Class @{text enum} *}
    13 
    13 
    14 class enum = itself +
    14 class enum = itself +
    15   fixes enum :: "'a list"
    15   fixes enum :: "'a list"
    16   assumes UNIV_enum [code func]: "UNIV = set enum"
    16   assumes UNIV_enum [code]: "UNIV = set enum"
    17     and enum_distinct: "distinct enum"
    17     and enum_distinct: "distinct enum"
    18 begin
    18 begin
    19 
    19 
    20 lemma finite_enum: "finite (UNIV \<Colon> 'a set)"
    20 lemma finite_enum: "finite (UNIV \<Colon> 'a set)"
    21   unfolding UNIV_enum ..
    21   unfolding UNIV_enum ..
    47 instance by default
    47 instance by default
    48   (simp_all add: eq_fun_def enum_all expand_fun_eq)
    48   (simp_all add: eq_fun_def enum_all expand_fun_eq)
    49 
    49 
    50 end
    50 end
    51 
    51 
    52 lemma order_fun [code func]:
    52 lemma order_fun [code]:
    53   fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
    53   fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
    54   shows "f \<le> g \<longleftrightarrow> list_all (\<lambda>x. f x \<le> g x) enum"
    54   shows "f \<le> g \<longleftrightarrow> list_all (\<lambda>x. f x \<le> g x) enum"
    55     and "f < g \<longleftrightarrow> f \<le> g \<and> \<not> list_all (\<lambda>x. f x = g x) enum"
    55     and "f < g \<longleftrightarrow> f \<le> g \<and> \<not> list_all (\<lambda>x. f x = g x) enum"
    56   by (simp_all add: list_all_iff enum_all expand_fun_eq le_fun_def less_fun_def order_less_le)
    56   by (simp_all add: list_all_iff enum_all expand_fun_eq le_fun_def less_fun_def order_less_le)
    57 
    57 
    58 
    58 
    59 subsection {* Quantifiers *}
    59 subsection {* Quantifiers *}
    60 
    60 
    61 lemma all_code [code func]: "(\<forall>x. P x) \<longleftrightarrow> list_all P enum"
    61 lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> list_all P enum"
    62   by (simp add: list_all_iff enum_all)
    62   by (simp add: list_all_iff enum_all)
    63 
    63 
    64 lemma exists_code [code func]: "(\<exists>x. P x) \<longleftrightarrow> \<not> list_all (Not o P) enum"
    64 lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> \<not> list_all (Not o P) enum"
    65   by (simp add: list_all_iff enum_all)
    65   by (simp add: list_all_iff enum_all)
    66 
    66 
    67 
    67 
    68 subsection {* Default instances *}
    68 subsection {* Default instances *}
    69 
    69 
   155 
   155 
   156 instantiation "fun" :: (enum, enum) enum
   156 instantiation "fun" :: (enum, enum) enum
   157 begin
   157 begin
   158 
   158 
   159 definition
   159 definition
   160   [code func del]: "enum = map (\<lambda>ys. the o map_of (zip (enum\<Colon>'a list) ys)) (n_lists (length (enum\<Colon>'a\<Colon>enum list)) enum)"
   160   [code del]: "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 
   161 
   162 instance proof
   162 instance proof
   163   show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
   163   show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
   164   proof (rule UNIV_eq_I)
   164   proof (rule UNIV_eq_I)
   165     fix f :: "'a \<Rightarrow> 'b"
   165     fix f :: "'a \<Rightarrow> 'b"
   175       distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
   175       distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
   176 qed
   176 qed
   177 
   177 
   178 end
   178 end
   179 
   179 
   180 lemma enum_fun_code [code func]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, eq} list)
   180 lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, eq} list)
   181   in map (\<lambda>ys. the o map_of (zip enum_a ys)) (n_lists (length enum_a) enum))"
   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)
   182   by (simp add: enum_fun_def Let_def)
   183 
   183 
   184 instantiation unit :: enum
   184 instantiation unit :: enum
   185 begin
   185 begin
   284 
   284 
   285 instantiation char :: enum
   285 instantiation char :: enum
   286 begin
   286 begin
   287 
   287 
   288 definition
   288 definition
   289   [code func del]: "enum = map (split Char) (product enum enum)"
   289   [code del]: "enum = map (split Char) (product enum enum)"
   290 
   290 
   291 lemma enum_char [code func]:
   291 lemma enum_char [code]:
   292   "enum = [Char Nibble0 Nibble0, Char Nibble0 Nibble1, Char Nibble0 Nibble2,
   292   "enum = [Char Nibble0 Nibble0, Char Nibble0 Nibble1, Char Nibble0 Nibble2,
   293   Char Nibble0 Nibble3, Char Nibble0 Nibble4, Char Nibble0 Nibble5,
   293   Char Nibble0 Nibble3, Char Nibble0 Nibble4, Char Nibble0 Nibble5,
   294   Char Nibble0 Nibble6, Char Nibble0 Nibble7, Char Nibble0 Nibble8,
   294   Char Nibble0 Nibble6, Char Nibble0 Nibble7, Char Nibble0 Nibble8,
   295   Char Nibble0 Nibble9, Char Nibble0 NibbleA, Char Nibble0 NibbleB,
   295   Char Nibble0 Nibble9, Char Nibble0 NibbleA, Char Nibble0 NibbleB,
   296   Char Nibble0 NibbleC, Char Nibble0 NibbleD, Char Nibble0 NibbleE,
   296   Char Nibble0 NibbleC, Char Nibble0 NibbleD, Char Nibble0 NibbleE,