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