src/HOL/Enum.thy
author haftmann
Sat Oct 20 09:12:16 2012 +0200 (2012-10-20)
changeset 49948 744934b818c7
parent 48123 104e5fccea12
child 49949 be3dd2e602e8
permissions -rw-r--r--
moved quite generic material from theory Enum to more appropriate places
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
bulwahn@40650
     6
imports Map String
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"
bulwahn@41078
    13
  fixes enum_all :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
bulwahn@41078
    14
  fixes enum_ex  :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
haftmann@33635
    15
  assumes UNIV_enum: "UNIV = set enum"
haftmann@26444
    16
    and enum_distinct: "distinct enum"
bulwahn@41078
    17
  assumes enum_all : "enum_all P = (\<forall> x. P x)"
bulwahn@41078
    18
  assumes enum_ex  : "enum_ex P = (\<exists> x. P x)" 
haftmann@26348
    19
begin
haftmann@26348
    20
haftmann@29797
    21
subclass finite proof
haftmann@29797
    22
qed (simp add: UNIV_enum)
haftmann@26444
    23
bulwahn@41078
    24
lemma enum_UNIV: "set enum = UNIV" unfolding UNIV_enum ..
haftmann@26444
    25
bulwahn@40683
    26
lemma in_enum: "x \<in> set enum"
bulwahn@41078
    27
  unfolding enum_UNIV by auto
haftmann@26348
    28
haftmann@26348
    29
lemma enum_eq_I:
haftmann@26348
    30
  assumes "\<And>x. x \<in> set xs"
haftmann@26348
    31
  shows "set enum = set xs"
haftmann@26348
    32
proof -
haftmann@26348
    33
  from assms UNIV_eq_I have "UNIV = set xs" by auto
bulwahn@41078
    34
  with enum_UNIV show ?thesis by simp
haftmann@26348
    35
qed
haftmann@26348
    36
haftmann@26348
    37
end
haftmann@26348
    38
haftmann@26348
    39
haftmann@26348
    40
subsection {* Equality and order on functions *}
haftmann@26348
    41
haftmann@38857
    42
instantiation "fun" :: (enum, equal) equal
haftmann@26513
    43
begin
haftmann@26348
    44
haftmann@26513
    45
definition
haftmann@38857
    46
  "HOL.equal f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
haftmann@26513
    47
haftmann@31464
    48
instance proof
bulwahn@41078
    49
qed (simp_all add: equal_fun_def enum_UNIV fun_eq_iff)
haftmann@26513
    50
haftmann@26513
    51
end
haftmann@26348
    52
bulwahn@40898
    53
lemma [code]:
bulwahn@41078
    54
  "HOL.equal f g \<longleftrightarrow> enum_all (%x. f x = g x)"
bulwahn@41078
    55
by (auto simp add: equal enum_all fun_eq_iff)
bulwahn@40898
    56
haftmann@38857
    57
lemma [code nbe]:
haftmann@38857
    58
  "HOL.equal (f :: _ \<Rightarrow> _) f \<longleftrightarrow> True"
haftmann@38857
    59
  by (fact equal_refl)
haftmann@38857
    60
haftmann@28562
    61
lemma order_fun [code]:
haftmann@26348
    62
  fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
bulwahn@41078
    63
  shows "f \<le> g \<longleftrightarrow> enum_all (\<lambda>x. f x \<le> g x)"
bulwahn@41078
    64
    and "f < g \<longleftrightarrow> f \<le> g \<and> enum_ex (\<lambda>x. f x \<noteq> g x)"
bulwahn@41078
    65
  by (simp_all add: enum_all enum_ex fun_eq_iff le_fun_def order_less_le)
haftmann@26968
    66
haftmann@26968
    67
haftmann@26968
    68
subsection {* Quantifiers *}
haftmann@26968
    69
bulwahn@41078
    70
lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> enum_all P"
bulwahn@41078
    71
  by (simp add: enum_all)
haftmann@26968
    72
bulwahn@41078
    73
lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> enum_ex P"
bulwahn@41078
    74
  by (simp add: enum_ex)
haftmann@26348
    75
bulwahn@40652
    76
lemma exists1_code[code]: "(\<exists>!x. P x) \<longleftrightarrow> list_ex1 P enum"
haftmann@49948
    77
  by (auto simp add: enum_UNIV list_ex1_iff)
bulwahn@40652
    78
haftmann@26348
    79
haftmann@26348
    80
subsection {* Default instances *}
haftmann@26348
    81
haftmann@26444
    82
lemma map_of_zip_enum_is_Some:
haftmann@26444
    83
  assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
    84
  shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
haftmann@26444
    85
proof -
haftmann@26444
    86
  from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
haftmann@26444
    87
    (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
haftmann@26444
    88
    by (auto intro!: map_of_zip_is_Some)
bulwahn@41078
    89
  then show ?thesis using enum_UNIV by auto
haftmann@26444
    90
qed
haftmann@26444
    91
haftmann@26444
    92
lemma map_of_zip_enum_inject:
haftmann@26444
    93
  fixes xs ys :: "'b\<Colon>enum list"
haftmann@26444
    94
  assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
    95
      "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
    96
    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
    97
  shows "xs = ys"
haftmann@26444
    98
proof -
haftmann@26444
    99
  have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
haftmann@26444
   100
  proof
haftmann@26444
   101
    fix x :: 'a
haftmann@26444
   102
    from length map_of_zip_enum_is_Some obtain y1 y2
haftmann@26444
   103
      where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
haftmann@26444
   104
        and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
wenzelm@47230
   105
    moreover from map_of
wenzelm@47230
   106
      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
   107
      by (auto dest: fun_cong)
haftmann@26444
   108
    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
   109
      by simp
haftmann@26444
   110
  qed
haftmann@26444
   111
  with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
haftmann@26444
   112
qed
haftmann@26444
   113
bulwahn@41078
   114
definition
bulwahn@41078
   115
  all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   116
where
haftmann@49948
   117
  "all_n_lists P n = (\<forall>xs \<in> set (List.n_lists n enum). P xs)"
bulwahn@41078
   118
bulwahn@41078
   119
lemma [code]:
bulwahn@41078
   120
  "all_n_lists P n = (if n = 0 then P [] else enum_all (%x. all_n_lists (%xs. P (x # xs)) (n - 1)))"
bulwahn@41078
   121
unfolding all_n_lists_def enum_all
bulwahn@41078
   122
by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   123
bulwahn@41078
   124
definition
bulwahn@41078
   125
  ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   126
where
haftmann@49948
   127
  "ex_n_lists P n = (\<exists>xs \<in> set (List.n_lists n enum). P xs)"
bulwahn@41078
   128
bulwahn@41078
   129
lemma [code]:
bulwahn@41078
   130
  "ex_n_lists P n = (if n = 0 then P [] else enum_ex (%x. ex_n_lists (%xs. P (x # xs)) (n - 1)))"
bulwahn@41078
   131
unfolding ex_n_lists_def enum_ex
bulwahn@41078
   132
by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   133
bulwahn@41078
   134
haftmann@26444
   135
instantiation "fun" :: (enum, enum) enum
haftmann@26444
   136
begin
haftmann@26444
   137
haftmann@26444
   138
definition
haftmann@49948
   139
  "enum = map (\<lambda>ys. the o map_of (zip (enum\<Colon>'a list) ys)) (List.n_lists (length (enum\<Colon>'a\<Colon>enum list)) enum)"
haftmann@26444
   140
bulwahn@41078
   141
definition
bulwahn@41078
   142
  "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   143
bulwahn@41078
   144
definition
bulwahn@41078
   145
  "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   146
bulwahn@41078
   147
haftmann@26444
   148
instance proof
haftmann@26444
   149
  show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   150
  proof (rule UNIV_eq_I)
haftmann@26444
   151
    fix f :: "'a \<Rightarrow> 'b"
haftmann@26444
   152
    have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@40683
   153
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
haftmann@26444
   154
    then show "f \<in> set enum"
bulwahn@40683
   155
      by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
haftmann@26444
   156
  qed
haftmann@26444
   157
next
haftmann@26444
   158
  from map_of_zip_enum_inject
haftmann@26444
   159
  show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   160
    by (auto intro!: inj_onI simp add: enum_fun_def
haftmann@26444
   161
      distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
bulwahn@41078
   162
next
bulwahn@41078
   163
  fix P
bulwahn@41078
   164
  show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   165
  proof
bulwahn@41078
   166
    assume "enum_all P"
bulwahn@41078
   167
    show "\<forall>x. P x"
bulwahn@41078
   168
    proof
bulwahn@41078
   169
      fix f :: "'a \<Rightarrow> 'b"
bulwahn@41078
   170
      have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   171
        by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
bulwahn@41078
   172
      from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
bulwahn@41078
   173
        unfolding enum_all_fun_def all_n_lists_def
bulwahn@41078
   174
        apply (simp add: set_n_lists)
bulwahn@41078
   175
        apply (erule_tac x="map f enum" in allE)
bulwahn@41078
   176
        apply (auto intro!: in_enum)
bulwahn@41078
   177
        done
bulwahn@41078
   178
      from this f show "P f" by auto
bulwahn@41078
   179
    qed
bulwahn@41078
   180
  next
bulwahn@41078
   181
    assume "\<forall>x. P x"
bulwahn@41078
   182
    from this show "enum_all P"
bulwahn@41078
   183
      unfolding enum_all_fun_def all_n_lists_def by auto
bulwahn@41078
   184
  qed
bulwahn@41078
   185
next
bulwahn@41078
   186
  fix P
bulwahn@41078
   187
  show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   188
  proof
bulwahn@41078
   189
    assume "enum_ex P"
bulwahn@41078
   190
    from this show "\<exists>x. P x"
bulwahn@41078
   191
      unfolding enum_ex_fun_def ex_n_lists_def by auto
bulwahn@41078
   192
  next
bulwahn@41078
   193
    assume "\<exists>x. P x"
bulwahn@41078
   194
    from this obtain f where "P f" ..
bulwahn@41078
   195
    have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   196
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum) 
bulwahn@41078
   197
    from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
bulwahn@41078
   198
      by auto
bulwahn@41078
   199
    from  this show "enum_ex P"
bulwahn@41078
   200
      unfolding enum_ex_fun_def ex_n_lists_def
bulwahn@41078
   201
      apply (auto simp add: set_n_lists)
bulwahn@41078
   202
      apply (rule_tac x="map f enum" in exI)
bulwahn@41078
   203
      apply (auto intro!: in_enum)
bulwahn@41078
   204
      done
bulwahn@41078
   205
  qed
haftmann@26444
   206
qed
haftmann@26444
   207
haftmann@26444
   208
end
haftmann@26444
   209
haftmann@38857
   210
lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
haftmann@49948
   211
  in map (\<lambda>ys. the o map_of (zip enum_a ys)) (List.n_lists (length enum_a) enum))"
haftmann@28245
   212
  by (simp add: enum_fun_def Let_def)
haftmann@26444
   213
bulwahn@41078
   214
lemma enum_all_fun_code [code]:
bulwahn@41078
   215
  "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   216
   in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
bulwahn@41078
   217
  by (simp add: enum_all_fun_def Let_def)
bulwahn@41078
   218
bulwahn@41078
   219
lemma enum_ex_fun_code [code]:
bulwahn@41078
   220
  "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   221
   in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
bulwahn@41078
   222
  by (simp add: enum_ex_fun_def Let_def)
bulwahn@41078
   223
haftmann@26348
   224
instantiation unit :: enum
haftmann@26348
   225
begin
haftmann@26348
   226
haftmann@26348
   227
definition
haftmann@26348
   228
  "enum = [()]"
haftmann@26348
   229
bulwahn@41078
   230
definition
bulwahn@41078
   231
  "enum_all P = P ()"
bulwahn@41078
   232
bulwahn@41078
   233
definition
bulwahn@41078
   234
  "enum_ex P = P ()"
bulwahn@41078
   235
haftmann@31464
   236
instance proof
bulwahn@41078
   237
qed (auto simp add: enum_unit_def UNIV_unit enum_all_unit_def enum_ex_unit_def intro: unit.exhaust)
haftmann@26348
   238
haftmann@26348
   239
end
haftmann@26348
   240
haftmann@26348
   241
instantiation bool :: enum
haftmann@26348
   242
begin
haftmann@26348
   243
haftmann@26348
   244
definition
haftmann@26348
   245
  "enum = [False, True]"
haftmann@26348
   246
bulwahn@41078
   247
definition
bulwahn@41078
   248
  "enum_all P = (P False \<and> P True)"
bulwahn@41078
   249
bulwahn@41078
   250
definition
bulwahn@41078
   251
  "enum_ex P = (P False \<or> P True)"
bulwahn@41078
   252
haftmann@31464
   253
instance proof
bulwahn@41078
   254
  fix P
bulwahn@41078
   255
  show "enum_all (P :: bool \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   256
    unfolding enum_all_bool_def by (auto, case_tac x) auto
bulwahn@41078
   257
next
bulwahn@41078
   258
  fix P
bulwahn@41078
   259
  show "enum_ex (P :: bool \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   260
    unfolding enum_ex_bool_def by (auto, case_tac x) auto
bulwahn@41078
   261
qed (auto simp add: enum_bool_def UNIV_bool)
haftmann@26348
   262
haftmann@26348
   263
end
haftmann@26348
   264
haftmann@37678
   265
instantiation prod :: (enum, enum) enum
haftmann@26348
   266
begin
haftmann@26348
   267
haftmann@26348
   268
definition
haftmann@49948
   269
  "enum = List.product enum enum"
haftmann@26348
   270
bulwahn@41078
   271
definition
bulwahn@41078
   272
  "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
bulwahn@41078
   273
bulwahn@41078
   274
definition
bulwahn@41078
   275
  "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
bulwahn@41078
   276
bulwahn@41078
   277
 
haftmann@26348
   278
instance by default
bulwahn@41078
   279
  (simp_all add: enum_prod_def product_list_set distinct_product
bulwahn@41078
   280
    enum_UNIV enum_distinct enum_all_prod_def enum_all enum_ex_prod_def enum_ex)
haftmann@26348
   281
haftmann@26348
   282
end
haftmann@26348
   283
haftmann@37678
   284
instantiation sum :: (enum, enum) enum
haftmann@26348
   285
begin
haftmann@26348
   286
haftmann@26348
   287
definition
haftmann@26348
   288
  "enum = map Inl enum @ map Inr enum"
haftmann@26348
   289
bulwahn@41078
   290
definition
bulwahn@41078
   291
  "enum_all P = (enum_all (%x. P (Inl x)) \<and> enum_all (%x. P (Inr x)))"
bulwahn@41078
   292
bulwahn@41078
   293
definition
bulwahn@41078
   294
  "enum_ex P = (enum_ex (%x. P (Inl x)) \<or> enum_ex (%x. P (Inr x)))"
bulwahn@41078
   295
bulwahn@41078
   296
instance proof
bulwahn@41078
   297
  fix P
bulwahn@41078
   298
  show "enum_all (P :: ('a + 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   299
    unfolding enum_all_sum_def enum_all
bulwahn@41078
   300
    by (auto, case_tac x) auto
bulwahn@41078
   301
next
bulwahn@41078
   302
  fix P
bulwahn@41078
   303
  show "enum_ex (P :: ('a + 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   304
    unfolding enum_ex_sum_def enum_ex
bulwahn@41078
   305
    by (auto, case_tac x) auto
bulwahn@41078
   306
qed (auto simp add: enum_UNIV enum_sum_def, case_tac x, auto intro: inj_onI simp add: distinct_map enum_distinct)
haftmann@26348
   307
haftmann@26348
   308
end
haftmann@26348
   309
haftmann@26348
   310
instantiation nibble :: enum
haftmann@26348
   311
begin
haftmann@26348
   312
haftmann@26348
   313
definition
haftmann@26348
   314
  "enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@26348
   315
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
haftmann@26348
   316
bulwahn@41078
   317
definition
bulwahn@41078
   318
  "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
bulwahn@41078
   319
     \<and> P Nibble8 \<and> P Nibble9 \<and> P NibbleA \<and> P NibbleB \<and> P NibbleC \<and> P NibbleD \<and> P NibbleE \<and> P NibbleF)"
bulwahn@41078
   320
bulwahn@41078
   321
definition
bulwahn@41078
   322
  "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
bulwahn@41078
   323
     \<or> P Nibble8 \<or> P Nibble9 \<or> P NibbleA \<or> P NibbleB \<or> P NibbleC \<or> P NibbleD \<or> P NibbleE \<or> P NibbleF)"
bulwahn@41078
   324
haftmann@31464
   325
instance proof
bulwahn@41078
   326
  fix P
bulwahn@41078
   327
  show "enum_all (P :: nibble \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   328
    unfolding enum_all_nibble_def
bulwahn@41078
   329
    by (auto, case_tac x) auto
bulwahn@41078
   330
next
bulwahn@41078
   331
  fix P
bulwahn@41078
   332
  show "enum_ex (P :: nibble \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   333
    unfolding enum_ex_nibble_def
bulwahn@41078
   334
    by (auto, case_tac x) auto
haftmann@31464
   335
qed (simp_all add: enum_nibble_def UNIV_nibble)
haftmann@26348
   336
haftmann@26348
   337
end
haftmann@26348
   338
haftmann@26348
   339
instantiation char :: enum
haftmann@26348
   340
begin
haftmann@26348
   341
haftmann@26348
   342
definition
haftmann@49948
   343
  "enum = map (split Char) (List.product enum enum)"
haftmann@26444
   344
haftmann@31482
   345
lemma enum_chars [code]:
haftmann@31482
   346
  "enum = chars"
haftmann@31482
   347
  unfolding enum_char_def chars_def enum_nibble_def by simp
haftmann@26348
   348
bulwahn@41078
   349
definition
bulwahn@41078
   350
  "enum_all P = list_all P chars"
bulwahn@41078
   351
bulwahn@41078
   352
definition
bulwahn@41078
   353
  "enum_ex P = list_ex P chars"
bulwahn@41078
   354
bulwahn@41078
   355
lemma set_enum_char: "set (enum :: char list) = UNIV"
bulwahn@41078
   356
    by (auto intro: char.exhaust simp add: enum_char_def product_list_set enum_UNIV full_SetCompr_eq [symmetric])
bulwahn@41078
   357
haftmann@31464
   358
instance proof
bulwahn@41078
   359
  fix P
bulwahn@41078
   360
  show "enum_all (P :: char \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   361
    unfolding enum_all_char_def enum_chars[symmetric]
bulwahn@41078
   362
    by (auto simp add: list_all_iff set_enum_char)
bulwahn@41078
   363
next
bulwahn@41078
   364
  fix P
bulwahn@41078
   365
  show "enum_ex (P :: char \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   366
    unfolding enum_ex_char_def enum_chars[symmetric]
bulwahn@41078
   367
    by (auto simp add: list_ex_iff set_enum_char)
bulwahn@41078
   368
next
bulwahn@41078
   369
  show "distinct (enum :: char list)"
bulwahn@41078
   370
    by (auto intro: inj_onI simp add: enum_char_def product_list_set distinct_map distinct_product enum_distinct)
bulwahn@41078
   371
qed (auto simp add: set_enum_char)
haftmann@26348
   372
haftmann@26348
   373
end
haftmann@26348
   374
huffman@29024
   375
instantiation option :: (enum) enum
huffman@29024
   376
begin
huffman@29024
   377
huffman@29024
   378
definition
huffman@29024
   379
  "enum = None # map Some enum"
huffman@29024
   380
bulwahn@41078
   381
definition
bulwahn@41078
   382
  "enum_all P = (P None \<and> enum_all (%x. P (Some x)))"
bulwahn@41078
   383
bulwahn@41078
   384
definition
bulwahn@41078
   385
  "enum_ex P = (P None \<or> enum_ex (%x. P (Some x)))"
bulwahn@41078
   386
haftmann@31464
   387
instance proof
bulwahn@41078
   388
  fix P
bulwahn@41078
   389
  show "enum_all (P :: 'a option \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   390
    unfolding enum_all_option_def enum_all
bulwahn@41078
   391
    by (auto, case_tac x) auto
bulwahn@41078
   392
next
bulwahn@41078
   393
  fix P
bulwahn@41078
   394
  show "enum_ex (P :: 'a option \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   395
    unfolding enum_ex_option_def enum_ex
bulwahn@41078
   396
    by (auto, case_tac x) auto
bulwahn@41078
   397
qed (auto simp add: enum_UNIV enum_option_def, rule option.exhaust, auto intro: simp add: distinct_map enum_distinct)
haftmann@45963
   398
end
haftmann@45963
   399
haftmann@45963
   400
instantiation set :: (enum) enum
haftmann@45963
   401
begin
haftmann@45963
   402
haftmann@45963
   403
definition
haftmann@45963
   404
  "enum = map set (sublists enum)"
haftmann@45963
   405
haftmann@45963
   406
definition
haftmann@45963
   407
  "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
haftmann@45963
   408
haftmann@45963
   409
definition
haftmann@45963
   410
  "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
haftmann@45963
   411
haftmann@45963
   412
instance proof
haftmann@45963
   413
qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
haftmann@45963
   414
  enum_distinct enum_UNIV)
huffman@29024
   415
huffman@29024
   416
end
huffman@29024
   417
haftmann@45963
   418
bulwahn@40647
   419
subsection {* Small finite types *}
bulwahn@40647
   420
bulwahn@40647
   421
text {* We define small finite types for the use in Quickcheck *}
bulwahn@40647
   422
bulwahn@40647
   423
datatype finite_1 = a\<^isub>1
bulwahn@40647
   424
bulwahn@40900
   425
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   426
bulwahn@40647
   427
instantiation finite_1 :: enum
bulwahn@40647
   428
begin
bulwahn@40647
   429
bulwahn@40647
   430
definition
bulwahn@40647
   431
  "enum = [a\<^isub>1]"
bulwahn@40647
   432
bulwahn@41078
   433
definition
bulwahn@41078
   434
  "enum_all P = P a\<^isub>1"
bulwahn@41078
   435
bulwahn@41078
   436
definition
bulwahn@41078
   437
  "enum_ex P = P a\<^isub>1"
bulwahn@41078
   438
bulwahn@40647
   439
instance proof
bulwahn@41078
   440
  fix P
bulwahn@41078
   441
  show "enum_all (P :: finite_1 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   442
    unfolding enum_all_finite_1_def
bulwahn@41078
   443
    by (auto, case_tac x) auto
bulwahn@41078
   444
next
bulwahn@41078
   445
  fix P
bulwahn@41078
   446
  show "enum_ex (P :: finite_1 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   447
    unfolding enum_ex_finite_1_def
bulwahn@41078
   448
    by (auto, case_tac x) auto
bulwahn@40647
   449
qed (auto simp add: enum_finite_1_def intro: finite_1.exhaust)
bulwahn@40647
   450
huffman@29024
   451
end
bulwahn@40647
   452
bulwahn@40651
   453
instantiation finite_1 :: linorder
bulwahn@40651
   454
begin
bulwahn@40651
   455
bulwahn@40651
   456
definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   457
where
bulwahn@40651
   458
  "less_eq_finite_1 x y = True"
bulwahn@40651
   459
bulwahn@40651
   460
definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   461
where
bulwahn@40651
   462
  "less_finite_1 x y = False"
bulwahn@40651
   463
bulwahn@40651
   464
instance
bulwahn@40651
   465
apply (intro_classes)
bulwahn@40651
   466
apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
bulwahn@40651
   467
apply (metis finite_1.exhaust)
bulwahn@40651
   468
done
bulwahn@40651
   469
bulwahn@40651
   470
end
bulwahn@40651
   471
bulwahn@41085
   472
hide_const (open) a\<^isub>1
bulwahn@40657
   473
bulwahn@40647
   474
datatype finite_2 = a\<^isub>1 | a\<^isub>2
bulwahn@40647
   475
bulwahn@40900
   476
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   477
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   478
bulwahn@40647
   479
instantiation finite_2 :: enum
bulwahn@40647
   480
begin
bulwahn@40647
   481
bulwahn@40647
   482
definition
bulwahn@40647
   483
  "enum = [a\<^isub>1, a\<^isub>2]"
bulwahn@40647
   484
bulwahn@41078
   485
definition
bulwahn@41078
   486
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2)"
bulwahn@41078
   487
bulwahn@41078
   488
definition
bulwahn@41078
   489
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2)"
bulwahn@41078
   490
bulwahn@40647
   491
instance proof
bulwahn@41078
   492
  fix P
bulwahn@41078
   493
  show "enum_all (P :: finite_2 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   494
    unfolding enum_all_finite_2_def
bulwahn@41078
   495
    by (auto, case_tac x) auto
bulwahn@41078
   496
next
bulwahn@41078
   497
  fix P
bulwahn@41078
   498
  show "enum_ex (P :: finite_2 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   499
    unfolding enum_ex_finite_2_def
bulwahn@41078
   500
    by (auto, case_tac x) auto
bulwahn@40647
   501
qed (auto simp add: enum_finite_2_def intro: finite_2.exhaust)
bulwahn@40647
   502
bulwahn@40647
   503
end
bulwahn@40647
   504
bulwahn@40651
   505
instantiation finite_2 :: linorder
bulwahn@40651
   506
begin
bulwahn@40651
   507
bulwahn@40651
   508
definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   509
where
bulwahn@40651
   510
  "less_finite_2 x y = ((x = a\<^isub>1) & (y = a\<^isub>2))"
bulwahn@40651
   511
bulwahn@40651
   512
definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   513
where
bulwahn@40651
   514
  "less_eq_finite_2 x y = ((x = y) \<or> (x < y))"
bulwahn@40651
   515
bulwahn@40651
   516
bulwahn@40651
   517
instance
bulwahn@40651
   518
apply (intro_classes)
bulwahn@40651
   519
apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
bulwahn@40651
   520
apply (metis finite_2.distinct finite_2.nchotomy)+
bulwahn@40651
   521
done
bulwahn@40651
   522
bulwahn@40651
   523
end
bulwahn@40651
   524
bulwahn@41085
   525
hide_const (open) a\<^isub>1 a\<^isub>2
bulwahn@40657
   526
bulwahn@40651
   527
bulwahn@40647
   528
datatype finite_3 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3
bulwahn@40647
   529
bulwahn@40900
   530
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   531
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   532
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   533
bulwahn@40647
   534
instantiation finite_3 :: enum
bulwahn@40647
   535
begin
bulwahn@40647
   536
bulwahn@40647
   537
definition
bulwahn@40647
   538
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3]"
bulwahn@40647
   539
bulwahn@41078
   540
definition
bulwahn@41078
   541
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3)"
bulwahn@41078
   542
bulwahn@41078
   543
definition
bulwahn@41078
   544
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3)"
bulwahn@41078
   545
bulwahn@40647
   546
instance proof
bulwahn@41078
   547
  fix P
bulwahn@41078
   548
  show "enum_all (P :: finite_3 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   549
    unfolding enum_all_finite_3_def
bulwahn@41078
   550
    by (auto, case_tac x) auto
bulwahn@41078
   551
next
bulwahn@41078
   552
  fix P
bulwahn@41078
   553
  show "enum_ex (P :: finite_3 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   554
    unfolding enum_ex_finite_3_def
bulwahn@41078
   555
    by (auto, case_tac x) auto
bulwahn@40647
   556
qed (auto simp add: enum_finite_3_def intro: finite_3.exhaust)
bulwahn@40647
   557
bulwahn@40647
   558
end
bulwahn@40647
   559
bulwahn@40651
   560
instantiation finite_3 :: linorder
bulwahn@40651
   561
begin
bulwahn@40651
   562
bulwahn@40651
   563
definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   564
where
bulwahn@40651
   565
  "less_finite_3 x y = (case x of a\<^isub>1 => (y \<noteq> a\<^isub>1)
bulwahn@40651
   566
     | a\<^isub>2 => (y = a\<^isub>3)| a\<^isub>3 => False)"
bulwahn@40651
   567
bulwahn@40651
   568
definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   569
where
bulwahn@40651
   570
  "less_eq_finite_3 x y = ((x = y) \<or> (x < y))"
bulwahn@40651
   571
bulwahn@40651
   572
bulwahn@40651
   573
instance proof (intro_classes)
bulwahn@40651
   574
qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
bulwahn@40651
   575
bulwahn@40651
   576
end
bulwahn@40651
   577
bulwahn@41085
   578
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3
bulwahn@40657
   579
bulwahn@40651
   580
bulwahn@40647
   581
datatype finite_4 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4
bulwahn@40647
   582
bulwahn@40900
   583
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   584
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   585
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   586
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   587
bulwahn@40647
   588
instantiation finite_4 :: enum
bulwahn@40647
   589
begin
bulwahn@40647
   590
bulwahn@40647
   591
definition
bulwahn@40647
   592
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4]"
bulwahn@40647
   593
bulwahn@41078
   594
definition
bulwahn@41078
   595
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4)"
bulwahn@41078
   596
bulwahn@41078
   597
definition
bulwahn@41078
   598
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4)"
bulwahn@41078
   599
bulwahn@40647
   600
instance proof
bulwahn@41078
   601
  fix P
bulwahn@41078
   602
  show "enum_all (P :: finite_4 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   603
    unfolding enum_all_finite_4_def
bulwahn@41078
   604
    by (auto, case_tac x) auto
bulwahn@41078
   605
next
bulwahn@41078
   606
  fix P
bulwahn@41078
   607
  show "enum_ex (P :: finite_4 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   608
    unfolding enum_ex_finite_4_def
bulwahn@41078
   609
    by (auto, case_tac x) auto
bulwahn@40647
   610
qed (auto simp add: enum_finite_4_def intro: finite_4.exhaust)
bulwahn@40647
   611
bulwahn@40647
   612
end
bulwahn@40647
   613
bulwahn@41085
   614
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4
bulwahn@40651
   615
bulwahn@40651
   616
bulwahn@40647
   617
datatype finite_5 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4 | a\<^isub>5
bulwahn@40647
   618
bulwahn@40900
   619
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   620
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   621
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   622
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   623
notation (output) a\<^isub>5  ("a\<^isub>5")
bulwahn@40900
   624
bulwahn@40647
   625
instantiation finite_5 :: enum
bulwahn@40647
   626
begin
bulwahn@40647
   627
bulwahn@40647
   628
definition
bulwahn@40647
   629
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5]"
bulwahn@40647
   630
bulwahn@41078
   631
definition
bulwahn@41078
   632
  "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)"
bulwahn@41078
   633
bulwahn@41078
   634
definition
bulwahn@41078
   635
  "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)"
bulwahn@41078
   636
bulwahn@40647
   637
instance proof
bulwahn@41078
   638
  fix P
bulwahn@41078
   639
  show "enum_all (P :: finite_5 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   640
    unfolding enum_all_finite_5_def
bulwahn@41078
   641
    by (auto, case_tac x) auto
bulwahn@41078
   642
next
bulwahn@41078
   643
  fix P
bulwahn@41078
   644
  show "enum_ex (P :: finite_5 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   645
    unfolding enum_ex_finite_5_def
bulwahn@41078
   646
    by (auto, case_tac x) auto
bulwahn@40647
   647
qed (auto simp add: enum_finite_5_def intro: finite_5.exhaust)
bulwahn@40647
   648
bulwahn@40647
   649
end
bulwahn@40647
   650
bulwahn@46352
   651
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4 a\<^isub>5
bulwahn@46352
   652
haftmann@49948
   653
bulwahn@41115
   654
subsection {* An executable THE operator on finite types *}
bulwahn@41115
   655
bulwahn@41115
   656
definition
haftmann@49948
   657
  [code del]: "enum_the = The"
bulwahn@41115
   658
bulwahn@41115
   659
lemma [code]:
bulwahn@41115
   660
  "The P = (case filter P enum of [x] => x | _ => enum_the P)"
bulwahn@41115
   661
proof -
bulwahn@41115
   662
  {
bulwahn@41115
   663
    fix a
bulwahn@41115
   664
    assume filter_enum: "filter P enum = [a]"
bulwahn@41115
   665
    have "The P = a"
bulwahn@41115
   666
    proof (rule the_equality)
bulwahn@41115
   667
      fix x
bulwahn@41115
   668
      assume "P x"
bulwahn@41115
   669
      show "x = a"
bulwahn@41115
   670
      proof (rule ccontr)
bulwahn@41115
   671
        assume "x \<noteq> a"
bulwahn@41115
   672
        from filter_enum obtain us vs
bulwahn@41115
   673
          where enum_eq: "enum = us @ [a] @ vs"
bulwahn@41115
   674
          and "\<forall> x \<in> set us. \<not> P x"
bulwahn@41115
   675
          and "\<forall> x \<in> set vs. \<not> P x"
bulwahn@41115
   676
          and "P a"
bulwahn@41115
   677
          by (auto simp add: filter_eq_Cons_iff) (simp only: filter_empty_conv[symmetric])
bulwahn@41115
   678
        with `P x` in_enum[of x, unfolded enum_eq] `x \<noteq> a` show "False" by auto
bulwahn@41115
   679
      qed
bulwahn@41115
   680
    next
bulwahn@41115
   681
      from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
bulwahn@41115
   682
    qed
bulwahn@41115
   683
  }
bulwahn@41115
   684
  from this show ?thesis
bulwahn@41115
   685
    unfolding enum_the_def by (auto split: list.split)
bulwahn@41115
   686
qed
bulwahn@41115
   687
bulwahn@46329
   688
code_abort enum_the
bulwahn@46336
   689
code_const enum_the (Eval "(fn p => raise Match)")
bulwahn@46329
   690
haftmann@49948
   691
bulwahn@46329
   692
subsection {* Further operations on finite types *}
bulwahn@46329
   693
haftmann@49948
   694
lemma Collect_code [code]:
bulwahn@46329
   695
  "Collect P = set (filter P enum)"
bulwahn@46329
   696
by (auto simp add: enum_UNIV)
haftmann@45140
   697
bulwahn@48123
   698
lemma [code]:
bulwahn@48123
   699
  "Id = image (%x. (x, x)) (set Enum.enum)"
bulwahn@48123
   700
by (auto intro: imageI in_enum)
bulwahn@48123
   701
bulwahn@46357
   702
lemma tranclp_unfold [code, no_atp]:
bulwahn@46357
   703
  "tranclp r a b \<equiv> (a, b) \<in> trancl {(x, y). r x y}"
bulwahn@46357
   704
by (simp add: trancl_def)
bulwahn@46352
   705
haftmann@49948
   706
lemma rtranclp_rtrancl_eq [code, no_atp]:
bulwahn@46359
   707
  "rtranclp r x y = ((x, y) : rtrancl {(x, y). r x y})"
bulwahn@46359
   708
unfolding rtrancl_def by auto
bulwahn@46359
   709
haftmann@49948
   710
lemma max_ext_eq [code]:
bulwahn@46358
   711
  "max_ext R = {(X, Y). finite X & finite Y & Y ~={} & (ALL x. x : X --> (EX xa : Y. (x, xa) : R))}"
bulwahn@46358
   712
by (auto simp add: max_ext.simps)
bulwahn@46358
   713
bulwahn@46361
   714
lemma max_extp_eq[code]:
bulwahn@46361
   715
  "max_extp r x y = ((x, y) : max_ext {(x, y). r x y})"
bulwahn@46361
   716
unfolding max_ext_def by auto
bulwahn@46361
   717
bulwahn@46361
   718
lemma mlex_eq[code]:
bulwahn@46361
   719
  "f <*mlex*> R = {(x, y). f x < f y \<or> (f x <= f y \<and> (x, y) : R)}"
bulwahn@46361
   720
unfolding mlex_prod_def by auto
bulwahn@46361
   721
bulwahn@46352
   722
subsection {* Executable accessible part *}
bulwahn@46352
   723
bulwahn@46352
   724
definition 
bulwahn@46352
   725
  [code del]: "card_UNIV = card UNIV"
bulwahn@46352
   726
bulwahn@46352
   727
lemma [code]:
bulwahn@46352
   728
  "card_UNIV TYPE('a :: enum) = card (set (Enum.enum :: 'a list))"
haftmann@49948
   729
  unfolding card_UNIV_def enum_UNIV ..
bulwahn@46352
   730
haftmann@49948
   731
lemma [code]:
haftmann@49948
   732
  fixes xs :: "('a::finite \<times> 'a) list"
haftmann@49948
   733
  shows "acc (set xs) = bacc (set xs) (card_UNIV TYPE('a))"
haftmann@49948
   734
  by (simp add: card_UNIV_def acc_bacc_eq)
bulwahn@46352
   735
haftmann@49948
   736
lemma [code_unfold]: "accp r = (\<lambda>x. x \<in> acc {(x, y). r x y})"
haftmann@49948
   737
  unfolding acc_def by simp
bulwahn@46352
   738
bulwahn@46352
   739
subsection {* Closing up *}
bulwahn@40657
   740
bulwahn@41085
   741
hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
haftmann@49948
   742
hide_const (open) enum enum_all enum_ex all_n_lists ex_n_lists ntrancl
bulwahn@40647
   743
bulwahn@40647
   744
end
haftmann@49948
   745