src/HOL/Library/Enum.thy
author haftmann
Mon Jun 08 08:38:50 2009 +0200 (2009-06-08)
changeset 31482 7288382fd549
parent 31464 b2aca38301c4
child 31596 c96d7e5df659
permissions -rw-r--r--
using constant "chars"
haftmann@31482
     1
(* Author: Florian Haftmann, TU Muenchen
haftmann@26348
     2
*)
haftmann@26348
     3
haftmann@26348
     4
header {* Finite types as explicit enumerations *}
haftmann@26348
     5
haftmann@26348
     6
theory Enum
haftmann@30663
     7
imports Map Main
haftmann@26348
     8
begin
haftmann@26348
     9
haftmann@26348
    10
subsection {* Class @{text enum} *}
haftmann@26348
    11
haftmann@29797
    12
class enum =
haftmann@26348
    13
  fixes enum :: "'a list"
haftmann@28562
    14
  assumes UNIV_enum [code]: "UNIV = set enum"
haftmann@26444
    15
    and enum_distinct: "distinct enum"
haftmann@26348
    16
begin
haftmann@26348
    17
haftmann@29797
    18
subclass finite proof
haftmann@29797
    19
qed (simp add: UNIV_enum)
haftmann@26444
    20
haftmann@26444
    21
lemma enum_all: "set enum = UNIV" unfolding UNIV_enum ..
haftmann@26444
    22
haftmann@26348
    23
lemma in_enum [intro]: "x \<in> set enum"
haftmann@26348
    24
  unfolding enum_all by auto
haftmann@26348
    25
haftmann@26348
    26
lemma enum_eq_I:
haftmann@26348
    27
  assumes "\<And>x. x \<in> set xs"
haftmann@26348
    28
  shows "set enum = set xs"
haftmann@26348
    29
proof -
haftmann@26348
    30
  from assms UNIV_eq_I have "UNIV = set xs" by auto
haftmann@26348
    31
  with enum_all show ?thesis by simp
haftmann@26348
    32
qed
haftmann@26348
    33
haftmann@26348
    34
end
haftmann@26348
    35
haftmann@26348
    36
haftmann@26348
    37
subsection {* Equality and order on functions *}
haftmann@26348
    38
haftmann@26513
    39
instantiation "fun" :: (enum, eq) eq
haftmann@26513
    40
begin
haftmann@26348
    41
haftmann@26513
    42
definition
haftmann@26732
    43
  "eq_class.eq f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
haftmann@26513
    44
haftmann@31464
    45
instance proof
haftmann@31464
    46
qed (simp_all add: eq_fun_def enum_all expand_fun_eq)
haftmann@26513
    47
haftmann@26513
    48
end
haftmann@26348
    49
haftmann@28562
    50
lemma order_fun [code]:
haftmann@26348
    51
  fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
haftmann@26968
    52
  shows "f \<le> g \<longleftrightarrow> list_all (\<lambda>x. f x \<le> g x) enum"
haftmann@26968
    53
    and "f < g \<longleftrightarrow> f \<le> g \<and> \<not> list_all (\<lambda>x. f x = g x) enum"
haftmann@28684
    54
  by (simp_all add: list_all_iff enum_all expand_fun_eq le_fun_def order_less_le)
haftmann@26968
    55
haftmann@26968
    56
haftmann@26968
    57
subsection {* Quantifiers *}
haftmann@26968
    58
haftmann@28562
    59
lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> list_all P enum"
haftmann@26968
    60
  by (simp add: list_all_iff enum_all)
haftmann@26968
    61
haftmann@28562
    62
lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> \<not> list_all (Not o P) enum"
haftmann@26968
    63
  by (simp add: list_all_iff enum_all)
haftmann@26348
    64
haftmann@26348
    65
haftmann@26348
    66
subsection {* Default instances *}
haftmann@26348
    67
haftmann@26444
    68
primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where
haftmann@26444
    69
  "n_lists 0 xs = [[]]"
haftmann@26444
    70
  | "n_lists (Suc n) xs = concat (map (\<lambda>ys. map (\<lambda>y. y # ys) xs) (n_lists n xs))"
haftmann@26444
    71
haftmann@26444
    72
lemma n_lists_Nil [simp]: "n_lists n [] = (if n = 0 then [[]] else [])"
haftmann@26444
    73
  by (induct n) simp_all
haftmann@26444
    74
haftmann@26444
    75
lemma length_n_lists: "length (n_lists n xs) = length xs ^ n"
haftmann@26444
    76
  by (induct n) (auto simp add: length_concat map_compose [symmetric] o_def listsum_triv)
haftmann@26444
    77
haftmann@26444
    78
lemma length_n_lists_elem: "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
haftmann@26444
    79
  by (induct n arbitrary: ys) auto
haftmann@26444
    80
haftmann@26444
    81
lemma set_n_lists: "set (n_lists n xs) = {ys. length ys = n \<and> set ys \<subseteq> set xs}"
haftmann@26444
    82
proof (rule set_ext)
haftmann@26444
    83
  fix ys :: "'a list"
haftmann@26444
    84
  show "ys \<in> set (n_lists n xs) \<longleftrightarrow> ys \<in> {ys. length ys = n \<and> set ys \<subseteq> set xs}"
haftmann@26444
    85
  proof -
haftmann@26444
    86
    have "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
haftmann@26444
    87
      by (induct n arbitrary: ys) auto
haftmann@26444
    88
    moreover have "\<And>x. ys \<in> set (n_lists n xs) \<Longrightarrow> x \<in> set ys \<Longrightarrow> x \<in> set xs"
haftmann@26444
    89
      by (induct n arbitrary: ys) auto
haftmann@26444
    90
    moreover have "set ys \<subseteq> set xs \<Longrightarrow> ys \<in> set (n_lists (length ys) xs)"
haftmann@26444
    91
      by (induct ys) auto
haftmann@26444
    92
    ultimately show ?thesis by auto
haftmann@26444
    93
  qed
haftmann@26444
    94
qed
haftmann@26444
    95
haftmann@26444
    96
lemma distinct_n_lists:
haftmann@26444
    97
  assumes "distinct xs"
haftmann@26444
    98
  shows "distinct (n_lists n xs)"
haftmann@26444
    99
proof (rule card_distinct)
haftmann@26444
   100
  from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)
haftmann@26444
   101
  have "card (set (n_lists n xs)) = card (set xs) ^ n"
haftmann@26444
   102
  proof (induct n)
haftmann@26444
   103
    case 0 then show ?case by simp
haftmann@26444
   104
  next
haftmann@26444
   105
    case (Suc n)
haftmann@26444
   106
    moreover have "card (\<Union>ys\<in>set (n_lists n xs). (\<lambda>y. y # ys) ` set xs)
haftmann@26444
   107
      = (\<Sum>ys\<in>set (n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))"
haftmann@26444
   108
      by (rule card_UN_disjoint) auto
haftmann@26444
   109
    moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)"
haftmann@26444
   110
      by (rule card_image) (simp add: inj_on_def)
haftmann@26444
   111
    ultimately show ?case by auto
haftmann@26444
   112
  qed
haftmann@26444
   113
  also have "\<dots> = length xs ^ n" by (simp add: card_length)
haftmann@26444
   114
  finally show "card (set (n_lists n xs)) = length (n_lists n xs)"
haftmann@26444
   115
    by (simp add: length_n_lists)
haftmann@26444
   116
qed
haftmann@26444
   117
haftmann@31193
   118
lemma map_of_zip_map: (*FIXME move to Map.thy*)
haftmann@31193
   119
  "map_of (zip xs (map f xs)) = (\<lambda>x. if x \<in> set xs then Some (f x) else None)"
haftmann@26444
   120
  by (induct xs) (simp_all add: expand_fun_eq)
haftmann@26444
   121
haftmann@26444
   122
lemma map_of_zip_enum_is_Some:
haftmann@26444
   123
  assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   124
  shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
haftmann@26444
   125
proof -
haftmann@26444
   126
  from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
haftmann@26444
   127
    (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
haftmann@26444
   128
    by (auto intro!: map_of_zip_is_Some)
haftmann@26444
   129
  then show ?thesis using enum_all by auto
haftmann@26444
   130
qed
haftmann@26444
   131
haftmann@26444
   132
lemma map_of_zip_enum_inject:
haftmann@26444
   133
  fixes xs ys :: "'b\<Colon>enum list"
haftmann@26444
   134
  assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   135
      "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   136
    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)"
haftmann@26444
   137
  shows "xs = ys"
haftmann@26444
   138
proof -
haftmann@26444
   139
  have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
haftmann@26444
   140
  proof
haftmann@26444
   141
    fix x :: 'a
haftmann@26444
   142
    from length map_of_zip_enum_is_Some obtain y1 y2
haftmann@26444
   143
      where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
haftmann@26444
   144
        and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
haftmann@26444
   145
    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)"
haftmann@26444
   146
      by (auto dest: fun_cong)
haftmann@26444
   147
    ultimately show "map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x = map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x"
haftmann@26444
   148
      by simp
haftmann@26444
   149
  qed
haftmann@26444
   150
  with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
haftmann@26444
   151
qed
haftmann@26444
   152
haftmann@26444
   153
instantiation "fun" :: (enum, enum) enum
haftmann@26444
   154
begin
haftmann@26444
   155
haftmann@26444
   156
definition
haftmann@28562
   157
  [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)"
haftmann@26444
   158
haftmann@26444
   159
instance proof
haftmann@26444
   160
  show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   161
  proof (rule UNIV_eq_I)
haftmann@26444
   162
    fix f :: "'a \<Rightarrow> 'b"
haftmann@26444
   163
    have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
haftmann@26444
   164
      by (auto simp add: map_of_zip_map expand_fun_eq)
haftmann@26444
   165
    then show "f \<in> set enum"
haftmann@26444
   166
      by (auto simp add: enum_fun_def set_n_lists)
haftmann@26444
   167
  qed
haftmann@26444
   168
next
haftmann@26444
   169
  from map_of_zip_enum_inject
haftmann@26444
   170
  show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   171
    by (auto intro!: inj_onI simp add: enum_fun_def
haftmann@26444
   172
      distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
haftmann@26444
   173
qed
haftmann@26444
   174
haftmann@26444
   175
end
haftmann@26444
   176
haftmann@28562
   177
lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, eq} list)
haftmann@28245
   178
  in map (\<lambda>ys. the o map_of (zip enum_a ys)) (n_lists (length enum_a) enum))"
haftmann@28245
   179
  by (simp add: enum_fun_def Let_def)
haftmann@26444
   180
haftmann@26348
   181
instantiation unit :: enum
haftmann@26348
   182
begin
haftmann@26348
   183
haftmann@26348
   184
definition
haftmann@26348
   185
  "enum = [()]"
haftmann@26348
   186
haftmann@31464
   187
instance proof
haftmann@31464
   188
qed (simp_all add: enum_unit_def UNIV_unit)
haftmann@26348
   189
haftmann@26348
   190
end
haftmann@26348
   191
haftmann@26348
   192
instantiation bool :: enum
haftmann@26348
   193
begin
haftmann@26348
   194
haftmann@26348
   195
definition
haftmann@26348
   196
  "enum = [False, True]"
haftmann@26348
   197
haftmann@31464
   198
instance proof
haftmann@31464
   199
qed (simp_all add: enum_bool_def UNIV_bool)
haftmann@26348
   200
haftmann@26348
   201
end
haftmann@26348
   202
haftmann@26348
   203
primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
haftmann@26348
   204
  "product [] _ = []"
haftmann@26348
   205
  | "product (x#xs) ys = map (Pair x) ys @ product xs ys"
haftmann@26348
   206
haftmann@26348
   207
lemma product_list_set:
haftmann@26348
   208
  "set (product xs ys) = set xs \<times> set ys"
haftmann@26348
   209
  by (induct xs) auto
haftmann@26348
   210
haftmann@26444
   211
lemma distinct_product:
haftmann@26444
   212
  assumes "distinct xs" and "distinct ys"
haftmann@26444
   213
  shows "distinct (product xs ys)"
haftmann@26444
   214
  using assms by (induct xs)
haftmann@26444
   215
    (auto intro: inj_onI simp add: product_list_set distinct_map)
haftmann@26444
   216
haftmann@26348
   217
instantiation * :: (enum, enum) enum
haftmann@26348
   218
begin
haftmann@26348
   219
haftmann@26348
   220
definition
haftmann@26348
   221
  "enum = product enum enum"
haftmann@26348
   222
haftmann@26348
   223
instance by default
haftmann@26444
   224
  (simp_all add: enum_prod_def product_list_set distinct_product enum_all enum_distinct)
haftmann@26348
   225
haftmann@26348
   226
end
haftmann@26348
   227
haftmann@26348
   228
instantiation "+" :: (enum, enum) enum
haftmann@26348
   229
begin
haftmann@26348
   230
haftmann@26348
   231
definition
haftmann@26348
   232
  "enum = map Inl enum @ map Inr enum"
haftmann@26348
   233
haftmann@26348
   234
instance by default
haftmann@26444
   235
  (auto simp add: enum_all enum_sum_def, case_tac x, auto intro: inj_onI simp add: distinct_map enum_distinct)
haftmann@26348
   236
haftmann@26348
   237
end
haftmann@26348
   238
haftmann@26348
   239
primrec sublists :: "'a list \<Rightarrow> 'a list list" where
haftmann@26348
   240
  "sublists [] = [[]]"
haftmann@26348
   241
  | "sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"
haftmann@26348
   242
haftmann@26444
   243
lemma length_sublists:
haftmann@26444
   244
  "length (sublists xs) = Suc (Suc (0\<Colon>nat)) ^ length xs"
haftmann@26444
   245
  by (induct xs) (simp_all add: Let_def)
haftmann@26444
   246
haftmann@26348
   247
lemma sublists_powset:
haftmann@26444
   248
  "set ` set (sublists xs) = Pow (set xs)"
haftmann@26348
   249
proof -
haftmann@26348
   250
  have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A"
haftmann@26348
   251
    by (auto simp add: image_def)
haftmann@26444
   252
  have "set (map set (sublists xs)) = Pow (set xs)"
haftmann@26348
   253
    by (induct xs)
haftmann@26444
   254
      (simp_all add: aux Let_def Pow_insert Un_commute)
haftmann@26444
   255
  then show ?thesis by simp
haftmann@26444
   256
qed
haftmann@26444
   257
haftmann@26444
   258
lemma distinct_set_sublists:
haftmann@26444
   259
  assumes "distinct xs"
haftmann@26444
   260
  shows "distinct (map set (sublists xs))"
haftmann@26444
   261
proof (rule card_distinct)
haftmann@26444
   262
  have "finite (set xs)" by rule
haftmann@26444
   263
  then have "card (Pow (set xs)) = Suc (Suc 0) ^ card (set xs)" by (rule card_Pow)
haftmann@26444
   264
  with assms distinct_card [of xs]
haftmann@26444
   265
    have "card (Pow (set xs)) = Suc (Suc 0) ^ length xs" by simp
haftmann@26444
   266
  then show "card (set (map set (sublists xs))) = length (map set (sublists xs))"
haftmann@26444
   267
    by (simp add: sublists_powset length_sublists)
haftmann@26348
   268
qed
haftmann@26348
   269
haftmann@26348
   270
instantiation nibble :: enum
haftmann@26348
   271
begin
haftmann@26348
   272
haftmann@26348
   273
definition
haftmann@26348
   274
  "enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@26348
   275
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
haftmann@26348
   276
haftmann@31464
   277
instance proof
haftmann@31464
   278
qed (simp_all add: enum_nibble_def UNIV_nibble)
haftmann@26348
   279
haftmann@26348
   280
end
haftmann@26348
   281
haftmann@26348
   282
instantiation char :: enum
haftmann@26348
   283
begin
haftmann@26348
   284
haftmann@26348
   285
definition
haftmann@28562
   286
  [code del]: "enum = map (split Char) (product enum enum)"
haftmann@26444
   287
haftmann@31482
   288
lemma enum_chars [code]:
haftmann@31482
   289
  "enum = chars"
haftmann@31482
   290
  unfolding enum_char_def chars_def enum_nibble_def by simp
haftmann@26348
   291
haftmann@31464
   292
instance proof
haftmann@31464
   293
qed (auto intro: char.exhaust injI simp add: enum_char_def product_list_set enum_all full_SetCompr_eq [symmetric]
haftmann@31464
   294
  distinct_map distinct_product enum_distinct)
haftmann@26348
   295
haftmann@26348
   296
end
haftmann@26348
   297
huffman@29024
   298
instantiation option :: (enum) enum
huffman@29024
   299
begin
huffman@29024
   300
huffman@29024
   301
definition
huffman@29024
   302
  "enum = None # map Some enum"
huffman@29024
   303
haftmann@31464
   304
instance proof
haftmann@31464
   305
qed (auto simp add: enum_all enum_option_def, rule option.exhaust, auto intro: simp add: distinct_map enum_distinct)
huffman@29024
   306
huffman@29024
   307
end
huffman@29024
   308
huffman@29024
   309
end