src/HOL/Enum.thy
author haftmann
Sun Nov 10 15:05:06 2013 +0100 (2013-11-10)
changeset 54295 45a5523d4a63
parent 54148 c8cc5ab4a863
child 54890 cb892d835803
permissions -rw-r--r--
qualifed popular user space names
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@49972
     6
imports Map
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"
haftmann@49950
    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"
haftmann@49950
    17
  assumes enum_all_UNIV: "enum_all P \<longleftrightarrow> Ball UNIV P"
haftmann@49950
    18
  assumes enum_ex_UNIV: "enum_ex P \<longleftrightarrow> Bex UNIV P" 
haftmann@49950
    19
   -- {* tailored towards simple instantiation *}
haftmann@26348
    20
begin
haftmann@26348
    21
haftmann@29797
    22
subclass finite proof
haftmann@29797
    23
qed (simp add: UNIV_enum)
haftmann@26444
    24
haftmann@49950
    25
lemma enum_UNIV:
haftmann@49950
    26
  "set enum = UNIV"
haftmann@49950
    27
  by (simp only: UNIV_enum)
haftmann@26444
    28
bulwahn@40683
    29
lemma in_enum: "x \<in> set enum"
haftmann@49950
    30
  by (simp add: enum_UNIV)
haftmann@26348
    31
haftmann@26348
    32
lemma enum_eq_I:
haftmann@26348
    33
  assumes "\<And>x. x \<in> set xs"
haftmann@26348
    34
  shows "set enum = set xs"
haftmann@26348
    35
proof -
haftmann@26348
    36
  from assms UNIV_eq_I have "UNIV = set xs" by auto
bulwahn@41078
    37
  with enum_UNIV show ?thesis by simp
haftmann@26348
    38
qed
haftmann@26348
    39
haftmann@49972
    40
lemma card_UNIV_length_enum:
haftmann@49972
    41
  "card (UNIV :: 'a set) = length enum"
haftmann@49972
    42
  by (simp add: UNIV_enum distinct_card enum_distinct)
haftmann@49972
    43
haftmann@49950
    44
lemma enum_all [simp]:
haftmann@49950
    45
  "enum_all = HOL.All"
haftmann@49950
    46
  by (simp add: fun_eq_iff enum_all_UNIV)
haftmann@49950
    47
haftmann@49950
    48
lemma enum_ex [simp]:
haftmann@49950
    49
  "enum_ex = HOL.Ex" 
haftmann@49950
    50
  by (simp add: fun_eq_iff enum_ex_UNIV)
haftmann@49950
    51
haftmann@26348
    52
end
haftmann@26348
    53
haftmann@26348
    54
haftmann@49949
    55
subsection {* Implementations using @{class enum} *}
haftmann@49949
    56
haftmann@49949
    57
subsubsection {* Unbounded operations and quantifiers *}
haftmann@49949
    58
haftmann@49949
    59
lemma Collect_code [code]:
haftmann@49949
    60
  "Collect P = set (filter P enum)"
haftmann@49950
    61
  by (simp add: enum_UNIV)
haftmann@49949
    62
bulwahn@50567
    63
lemma vimage_code [code]:
bulwahn@50567
    64
  "f -` B = set (filter (%x. f x : B) enum_class.enum)"
bulwahn@50567
    65
  unfolding vimage_def Collect_code ..
bulwahn@50567
    66
haftmann@49949
    67
definition card_UNIV :: "'a itself \<Rightarrow> nat"
haftmann@49949
    68
where
haftmann@49949
    69
  [code del]: "card_UNIV TYPE('a) = card (UNIV :: 'a set)"
haftmann@49949
    70
haftmann@49949
    71
lemma [code]:
haftmann@49949
    72
  "card_UNIV TYPE('a :: enum) = card (set (Enum.enum :: 'a list))"
haftmann@49949
    73
  by (simp only: card_UNIV_def enum_UNIV)
haftmann@49949
    74
haftmann@49949
    75
lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> enum_all P"
haftmann@49950
    76
  by simp
haftmann@49949
    77
haftmann@49949
    78
lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> enum_ex P"
haftmann@49950
    79
  by simp
haftmann@49949
    80
haftmann@49949
    81
lemma exists1_code [code]: "(\<exists>!x. P x) \<longleftrightarrow> list_ex1 P enum"
haftmann@49950
    82
  by (auto simp add: list_ex1_iff enum_UNIV)
haftmann@49949
    83
haftmann@49949
    84
haftmann@49949
    85
subsubsection {* An executable choice operator *}
haftmann@49949
    86
haftmann@49949
    87
definition
haftmann@49949
    88
  [code del]: "enum_the = The"
haftmann@49949
    89
haftmann@49949
    90
lemma [code]:
haftmann@49949
    91
  "The P = (case filter P enum of [x] => x | _ => enum_the P)"
haftmann@49949
    92
proof -
haftmann@49949
    93
  {
haftmann@49949
    94
    fix a
haftmann@49949
    95
    assume filter_enum: "filter P enum = [a]"
haftmann@49949
    96
    have "The P = a"
haftmann@49949
    97
    proof (rule the_equality)
haftmann@49949
    98
      fix x
haftmann@49949
    99
      assume "P x"
haftmann@49949
   100
      show "x = a"
haftmann@49949
   101
      proof (rule ccontr)
haftmann@49949
   102
        assume "x \<noteq> a"
haftmann@49949
   103
        from filter_enum obtain us vs
haftmann@49949
   104
          where enum_eq: "enum = us @ [a] @ vs"
haftmann@49949
   105
          and "\<forall> x \<in> set us. \<not> P x"
haftmann@49949
   106
          and "\<forall> x \<in> set vs. \<not> P x"
haftmann@49949
   107
          and "P a"
haftmann@49949
   108
          by (auto simp add: filter_eq_Cons_iff) (simp only: filter_empty_conv[symmetric])
haftmann@49949
   109
        with `P x` in_enum[of x, unfolded enum_eq] `x \<noteq> a` show "False" by auto
haftmann@49949
   110
      qed
haftmann@49949
   111
    next
haftmann@49949
   112
      from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
haftmann@49949
   113
    qed
haftmann@49949
   114
  }
haftmann@49949
   115
  from this show ?thesis
haftmann@49949
   116
    unfolding enum_the_def by (auto split: list.split)
haftmann@49949
   117
qed
haftmann@49949
   118
haftmann@49949
   119
code_abort enum_the
haftmann@52435
   120
haftmann@52435
   121
code_printing
haftmann@52435
   122
  constant enum_the \<rightharpoonup> (Eval) "(fn '_ => raise Match)"
haftmann@49949
   123
haftmann@49949
   124
haftmann@49949
   125
subsubsection {* Equality and order on functions *}
haftmann@26348
   126
haftmann@38857
   127
instantiation "fun" :: (enum, equal) equal
haftmann@26513
   128
begin
haftmann@26348
   129
haftmann@26513
   130
definition
haftmann@38857
   131
  "HOL.equal f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
haftmann@26513
   132
haftmann@31464
   133
instance proof
haftmann@49950
   134
qed (simp_all add: equal_fun_def fun_eq_iff enum_UNIV)
haftmann@26513
   135
haftmann@26513
   136
end
haftmann@26348
   137
bulwahn@40898
   138
lemma [code]:
bulwahn@41078
   139
  "HOL.equal f g \<longleftrightarrow> enum_all (%x. f x = g x)"
haftmann@49950
   140
  by (auto simp add: equal fun_eq_iff)
bulwahn@40898
   141
haftmann@38857
   142
lemma [code nbe]:
haftmann@38857
   143
  "HOL.equal (f :: _ \<Rightarrow> _) f \<longleftrightarrow> True"
haftmann@38857
   144
  by (fact equal_refl)
haftmann@38857
   145
haftmann@28562
   146
lemma order_fun [code]:
haftmann@26348
   147
  fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
bulwahn@41078
   148
  shows "f \<le> g \<longleftrightarrow> enum_all (\<lambda>x. f x \<le> g x)"
bulwahn@41078
   149
    and "f < g \<longleftrightarrow> f \<le> g \<and> enum_ex (\<lambda>x. f x \<noteq> g x)"
haftmann@49950
   150
  by (simp_all add: fun_eq_iff le_fun_def order_less_le)
haftmann@26968
   151
haftmann@26968
   152
haftmann@49949
   153
subsubsection {* Operations on relations *}
haftmann@49949
   154
haftmann@49949
   155
lemma [code]:
haftmann@49949
   156
  "Id = image (\<lambda>x. (x, x)) (set Enum.enum)"
haftmann@49949
   157
  by (auto intro: imageI in_enum)
haftmann@26968
   158
blanchet@54148
   159
lemma tranclp_unfold [code]:
haftmann@49949
   160
  "tranclp r a b \<longleftrightarrow> (a, b) \<in> trancl {(x, y). r x y}"
haftmann@49949
   161
  by (simp add: trancl_def)
haftmann@49949
   162
blanchet@54148
   163
lemma rtranclp_rtrancl_eq [code]:
haftmann@49949
   164
  "rtranclp r x y \<longleftrightarrow> (x, y) \<in> rtrancl {(x, y). r x y}"
haftmann@49949
   165
  by (simp add: rtrancl_def)
haftmann@26968
   166
haftmann@49949
   167
lemma max_ext_eq [code]:
haftmann@49949
   168
  "max_ext R = {(X, Y). finite X \<and> finite Y \<and> Y \<noteq> {} \<and> (\<forall>x. x \<in> X \<longrightarrow> (\<exists>xa \<in> Y. (x, xa) \<in> R))}"
haftmann@49949
   169
  by (auto simp add: max_ext.simps)
haftmann@49949
   170
haftmann@49949
   171
lemma max_extp_eq [code]:
haftmann@49949
   172
  "max_extp r x y \<longleftrightarrow> (x, y) \<in> max_ext {(x, y). r x y}"
haftmann@49949
   173
  by (simp add: max_ext_def)
haftmann@26348
   174
haftmann@49949
   175
lemma mlex_eq [code]:
haftmann@49949
   176
  "f <*mlex*> R = {(x, y). f x < f y \<or> (f x \<le> f y \<and> (x, y) \<in> R)}"
haftmann@49949
   177
  by (auto simp add: mlex_prod_def)
haftmann@49949
   178
haftmann@49949
   179
lemma [code]:
haftmann@49949
   180
  fixes xs :: "('a::finite \<times> 'a) list"
haftmann@54295
   181
  shows "Wellfounded.acc (set xs) = bacc (set xs) (card_UNIV TYPE('a))"
haftmann@49949
   182
  by (simp add: card_UNIV_def acc_bacc_eq)
haftmann@49949
   183
haftmann@26348
   184
haftmann@49949
   185
subsection {* Default instances for @{class enum} *}
haftmann@26348
   186
haftmann@26444
   187
lemma map_of_zip_enum_is_Some:
haftmann@26444
   188
  assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   189
  shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
haftmann@26444
   190
proof -
haftmann@26444
   191
  from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
haftmann@26444
   192
    (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
haftmann@26444
   193
    by (auto intro!: map_of_zip_is_Some)
bulwahn@41078
   194
  then show ?thesis using enum_UNIV by auto
haftmann@26444
   195
qed
haftmann@26444
   196
haftmann@26444
   197
lemma map_of_zip_enum_inject:
haftmann@26444
   198
  fixes xs ys :: "'b\<Colon>enum list"
haftmann@26444
   199
  assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   200
      "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   201
    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
   202
  shows "xs = ys"
haftmann@26444
   203
proof -
haftmann@26444
   204
  have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
haftmann@26444
   205
  proof
haftmann@26444
   206
    fix x :: 'a
haftmann@26444
   207
    from length map_of_zip_enum_is_Some obtain y1 y2
haftmann@26444
   208
      where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
haftmann@26444
   209
        and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
wenzelm@47230
   210
    moreover from map_of
wenzelm@47230
   211
      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
   212
      by (auto dest: fun_cong)
haftmann@26444
   213
    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
   214
      by simp
haftmann@26444
   215
  qed
haftmann@26444
   216
  with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
haftmann@26444
   217
qed
haftmann@26444
   218
haftmann@49950
   219
definition all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   220
where
haftmann@49950
   221
  "all_n_lists P n \<longleftrightarrow> (\<forall>xs \<in> set (List.n_lists n enum). P xs)"
bulwahn@41078
   222
bulwahn@41078
   223
lemma [code]:
haftmann@49950
   224
  "all_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_all (%x. all_n_lists (%xs. P (x # xs)) (n - 1)))"
haftmann@49950
   225
  unfolding all_n_lists_def enum_all
haftmann@49950
   226
  by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   227
haftmann@49950
   228
definition ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   229
where
haftmann@49950
   230
  "ex_n_lists P n \<longleftrightarrow> (\<exists>xs \<in> set (List.n_lists n enum). P xs)"
bulwahn@41078
   231
bulwahn@41078
   232
lemma [code]:
haftmann@49950
   233
  "ex_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_ex (%x. ex_n_lists (%xs. P (x # xs)) (n - 1)))"
haftmann@49950
   234
  unfolding ex_n_lists_def enum_ex
haftmann@49950
   235
  by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   236
haftmann@26444
   237
instantiation "fun" :: (enum, enum) enum
haftmann@26444
   238
begin
haftmann@26444
   239
haftmann@26444
   240
definition
haftmann@49948
   241
  "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
   242
bulwahn@41078
   243
definition
bulwahn@41078
   244
  "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   245
bulwahn@41078
   246
definition
bulwahn@41078
   247
  "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   248
haftmann@26444
   249
instance proof
haftmann@26444
   250
  show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   251
  proof (rule UNIV_eq_I)
haftmann@26444
   252
    fix f :: "'a \<Rightarrow> 'b"
haftmann@26444
   253
    have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@40683
   254
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
haftmann@26444
   255
    then show "f \<in> set enum"
bulwahn@40683
   256
      by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
haftmann@26444
   257
  qed
haftmann@26444
   258
next
haftmann@26444
   259
  from map_of_zip_enum_inject
haftmann@26444
   260
  show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   261
    by (auto intro!: inj_onI simp add: enum_fun_def
haftmann@49950
   262
      distinct_map distinct_n_lists enum_distinct set_n_lists)
bulwahn@41078
   263
next
bulwahn@41078
   264
  fix P
haftmann@49950
   265
  show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Ball UNIV P"
bulwahn@41078
   266
  proof
bulwahn@41078
   267
    assume "enum_all P"
haftmann@49950
   268
    show "Ball UNIV P"
bulwahn@41078
   269
    proof
bulwahn@41078
   270
      fix f :: "'a \<Rightarrow> 'b"
bulwahn@41078
   271
      have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   272
        by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
bulwahn@41078
   273
      from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
bulwahn@41078
   274
        unfolding enum_all_fun_def all_n_lists_def
bulwahn@41078
   275
        apply (simp add: set_n_lists)
bulwahn@41078
   276
        apply (erule_tac x="map f enum" in allE)
bulwahn@41078
   277
        apply (auto intro!: in_enum)
bulwahn@41078
   278
        done
bulwahn@41078
   279
      from this f show "P f" by auto
bulwahn@41078
   280
    qed
bulwahn@41078
   281
  next
haftmann@49950
   282
    assume "Ball UNIV P"
bulwahn@41078
   283
    from this show "enum_all P"
bulwahn@41078
   284
      unfolding enum_all_fun_def all_n_lists_def by auto
bulwahn@41078
   285
  qed
bulwahn@41078
   286
next
bulwahn@41078
   287
  fix P
haftmann@49950
   288
  show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Bex UNIV P"
bulwahn@41078
   289
  proof
bulwahn@41078
   290
    assume "enum_ex P"
haftmann@49950
   291
    from this show "Bex UNIV P"
bulwahn@41078
   292
      unfolding enum_ex_fun_def ex_n_lists_def by auto
bulwahn@41078
   293
  next
haftmann@49950
   294
    assume "Bex UNIV P"
bulwahn@41078
   295
    from this obtain f where "P f" ..
bulwahn@41078
   296
    have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   297
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum) 
bulwahn@41078
   298
    from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
bulwahn@41078
   299
      by auto
bulwahn@41078
   300
    from  this show "enum_ex P"
bulwahn@41078
   301
      unfolding enum_ex_fun_def ex_n_lists_def
bulwahn@41078
   302
      apply (auto simp add: set_n_lists)
bulwahn@41078
   303
      apply (rule_tac x="map f enum" in exI)
bulwahn@41078
   304
      apply (auto intro!: in_enum)
bulwahn@41078
   305
      done
bulwahn@41078
   306
  qed
haftmann@26444
   307
qed
haftmann@26444
   308
haftmann@26444
   309
end
haftmann@26444
   310
haftmann@38857
   311
lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
haftmann@49948
   312
  in map (\<lambda>ys. the o map_of (zip enum_a ys)) (List.n_lists (length enum_a) enum))"
haftmann@28245
   313
  by (simp add: enum_fun_def Let_def)
haftmann@26444
   314
bulwahn@41078
   315
lemma enum_all_fun_code [code]:
bulwahn@41078
   316
  "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   317
   in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
haftmann@49950
   318
  by (simp only: enum_all_fun_def Let_def)
bulwahn@41078
   319
bulwahn@41078
   320
lemma enum_ex_fun_code [code]:
bulwahn@41078
   321
  "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   322
   in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
haftmann@49950
   323
  by (simp only: enum_ex_fun_def Let_def)
haftmann@45963
   324
haftmann@45963
   325
instantiation set :: (enum) enum
haftmann@45963
   326
begin
haftmann@45963
   327
haftmann@45963
   328
definition
haftmann@45963
   329
  "enum = map set (sublists enum)"
haftmann@45963
   330
haftmann@45963
   331
definition
haftmann@45963
   332
  "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
haftmann@45963
   333
haftmann@45963
   334
definition
haftmann@45963
   335
  "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
haftmann@45963
   336
haftmann@45963
   337
instance proof
haftmann@45963
   338
qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
haftmann@45963
   339
  enum_distinct enum_UNIV)
huffman@29024
   340
huffman@29024
   341
end
huffman@29024
   342
haftmann@49950
   343
instantiation unit :: enum
haftmann@49950
   344
begin
haftmann@49950
   345
haftmann@49950
   346
definition
haftmann@49950
   347
  "enum = [()]"
haftmann@49950
   348
haftmann@49950
   349
definition
haftmann@49950
   350
  "enum_all P = P ()"
haftmann@49950
   351
haftmann@49950
   352
definition
haftmann@49950
   353
  "enum_ex P = P ()"
haftmann@49950
   354
haftmann@49950
   355
instance proof
haftmann@49950
   356
qed (auto simp add: enum_unit_def enum_all_unit_def enum_ex_unit_def)
haftmann@49950
   357
haftmann@49950
   358
end
haftmann@49950
   359
haftmann@49950
   360
instantiation bool :: enum
haftmann@49950
   361
begin
haftmann@49950
   362
haftmann@49950
   363
definition
haftmann@49950
   364
  "enum = [False, True]"
haftmann@49950
   365
haftmann@49950
   366
definition
haftmann@49950
   367
  "enum_all P \<longleftrightarrow> P False \<and> P True"
haftmann@49950
   368
haftmann@49950
   369
definition
haftmann@49950
   370
  "enum_ex P \<longleftrightarrow> P False \<or> P True"
haftmann@49950
   371
haftmann@49950
   372
instance proof
haftmann@49950
   373
qed (simp_all only: enum_bool_def enum_all_bool_def enum_ex_bool_def UNIV_bool, simp_all)
haftmann@49950
   374
haftmann@49950
   375
end
haftmann@49950
   376
haftmann@49950
   377
instantiation prod :: (enum, enum) enum
haftmann@49950
   378
begin
haftmann@49950
   379
haftmann@49950
   380
definition
haftmann@49950
   381
  "enum = List.product enum enum"
haftmann@49950
   382
haftmann@49950
   383
definition
haftmann@49950
   384
  "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
haftmann@49950
   385
haftmann@49950
   386
definition
haftmann@49950
   387
  "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
haftmann@49950
   388
haftmann@49950
   389
 
haftmann@49950
   390
instance by default
haftmann@49950
   391
  (simp_all add: enum_prod_def product_list_set distinct_product
haftmann@49950
   392
    enum_UNIV enum_distinct enum_all_prod_def enum_ex_prod_def)
haftmann@49950
   393
haftmann@49950
   394
end
haftmann@49950
   395
haftmann@49950
   396
instantiation sum :: (enum, enum) enum
haftmann@49950
   397
begin
haftmann@49950
   398
haftmann@49950
   399
definition
haftmann@49950
   400
  "enum = map Inl enum @ map Inr enum"
haftmann@49950
   401
haftmann@49950
   402
definition
haftmann@49950
   403
  "enum_all P \<longleftrightarrow> enum_all (\<lambda>x. P (Inl x)) \<and> enum_all (\<lambda>x. P (Inr x))"
haftmann@49950
   404
haftmann@49950
   405
definition
haftmann@49950
   406
  "enum_ex P \<longleftrightarrow> enum_ex (\<lambda>x. P (Inl x)) \<or> enum_ex (\<lambda>x. P (Inr x))"
haftmann@49950
   407
haftmann@49950
   408
instance proof
haftmann@49950
   409
qed (simp_all only: enum_sum_def enum_all_sum_def enum_ex_sum_def UNIV_sum,
haftmann@49950
   410
  auto simp add: enum_UNIV distinct_map enum_distinct)
haftmann@49950
   411
haftmann@49950
   412
end
haftmann@49950
   413
haftmann@49950
   414
instantiation option :: (enum) enum
haftmann@49950
   415
begin
haftmann@49950
   416
haftmann@49950
   417
definition
haftmann@49950
   418
  "enum = None # map Some enum"
haftmann@49950
   419
haftmann@49950
   420
definition
haftmann@49950
   421
  "enum_all P \<longleftrightarrow> P None \<and> enum_all (\<lambda>x. P (Some x))"
haftmann@49950
   422
haftmann@49950
   423
definition
haftmann@49950
   424
  "enum_ex P \<longleftrightarrow> P None \<or> enum_ex (\<lambda>x. P (Some x))"
haftmann@49950
   425
haftmann@49950
   426
instance proof
haftmann@49950
   427
qed (simp_all only: enum_option_def enum_all_option_def enum_ex_option_def UNIV_option_conv,
haftmann@49950
   428
  auto simp add: distinct_map enum_UNIV enum_distinct)
haftmann@49950
   429
haftmann@49950
   430
end
haftmann@49950
   431
haftmann@45963
   432
bulwahn@40647
   433
subsection {* Small finite types *}
bulwahn@40647
   434
bulwahn@40647
   435
text {* We define small finite types for the use in Quickcheck *}
bulwahn@40647
   436
wenzelm@53015
   437
datatype finite_1 = a\<^sub>1
bulwahn@40647
   438
wenzelm@53015
   439
notation (output) a\<^sub>1  ("a\<^sub>1")
bulwahn@40900
   440
haftmann@49950
   441
lemma UNIV_finite_1:
wenzelm@53015
   442
  "UNIV = {a\<^sub>1}"
haftmann@49950
   443
  by (auto intro: finite_1.exhaust)
haftmann@49950
   444
bulwahn@40647
   445
instantiation finite_1 :: enum
bulwahn@40647
   446
begin
bulwahn@40647
   447
bulwahn@40647
   448
definition
wenzelm@53015
   449
  "enum = [a\<^sub>1]"
bulwahn@40647
   450
bulwahn@41078
   451
definition
wenzelm@53015
   452
  "enum_all P = P a\<^sub>1"
bulwahn@41078
   453
bulwahn@41078
   454
definition
wenzelm@53015
   455
  "enum_ex P = P a\<^sub>1"
bulwahn@41078
   456
bulwahn@40647
   457
instance proof
haftmann@49950
   458
qed (simp_all only: enum_finite_1_def enum_all_finite_1_def enum_ex_finite_1_def UNIV_finite_1, simp_all)
bulwahn@40647
   459
huffman@29024
   460
end
bulwahn@40647
   461
bulwahn@40651
   462
instantiation finite_1 :: linorder
bulwahn@40651
   463
begin
bulwahn@40651
   464
haftmann@49950
   465
definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
haftmann@49950
   466
where
haftmann@49950
   467
  "x < (y :: finite_1) \<longleftrightarrow> False"
haftmann@49950
   468
bulwahn@40651
   469
definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   470
where
haftmann@49950
   471
  "x \<le> (y :: finite_1) \<longleftrightarrow> True"
bulwahn@40651
   472
bulwahn@40651
   473
instance
bulwahn@40651
   474
apply (intro_classes)
bulwahn@40651
   475
apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
bulwahn@40651
   476
apply (metis finite_1.exhaust)
bulwahn@40651
   477
done
bulwahn@40651
   478
bulwahn@40651
   479
end
bulwahn@40651
   480
wenzelm@53015
   481
hide_const (open) a\<^sub>1
bulwahn@40657
   482
wenzelm@53015
   483
datatype finite_2 = a\<^sub>1 | a\<^sub>2
bulwahn@40647
   484
wenzelm@53015
   485
notation (output) a\<^sub>1  ("a\<^sub>1")
wenzelm@53015
   486
notation (output) a\<^sub>2  ("a\<^sub>2")
bulwahn@40900
   487
haftmann@49950
   488
lemma UNIV_finite_2:
wenzelm@53015
   489
  "UNIV = {a\<^sub>1, a\<^sub>2}"
haftmann@49950
   490
  by (auto intro: finite_2.exhaust)
haftmann@49950
   491
bulwahn@40647
   492
instantiation finite_2 :: enum
bulwahn@40647
   493
begin
bulwahn@40647
   494
bulwahn@40647
   495
definition
wenzelm@53015
   496
  "enum = [a\<^sub>1, a\<^sub>2]"
bulwahn@40647
   497
bulwahn@41078
   498
definition
wenzelm@53015
   499
  "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2"
bulwahn@41078
   500
bulwahn@41078
   501
definition
wenzelm@53015
   502
  "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2"
bulwahn@41078
   503
bulwahn@40647
   504
instance proof
haftmann@49950
   505
qed (simp_all only: enum_finite_2_def enum_all_finite_2_def enum_ex_finite_2_def UNIV_finite_2, simp_all)
bulwahn@40647
   506
bulwahn@40647
   507
end
bulwahn@40647
   508
bulwahn@40651
   509
instantiation finite_2 :: linorder
bulwahn@40651
   510
begin
bulwahn@40651
   511
bulwahn@40651
   512
definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   513
where
wenzelm@53015
   514
  "x < y \<longleftrightarrow> x = a\<^sub>1 \<and> y = a\<^sub>2"
bulwahn@40651
   515
bulwahn@40651
   516
definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   517
where
haftmann@49950
   518
  "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_2)"
bulwahn@40651
   519
bulwahn@40651
   520
instance
bulwahn@40651
   521
apply (intro_classes)
bulwahn@40651
   522
apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
haftmann@49950
   523
apply (metis finite_2.nchotomy)+
bulwahn@40651
   524
done
bulwahn@40651
   525
bulwahn@40651
   526
end
bulwahn@40651
   527
wenzelm@53015
   528
hide_const (open) a\<^sub>1 a\<^sub>2
bulwahn@40657
   529
wenzelm@53015
   530
datatype finite_3 = a\<^sub>1 | a\<^sub>2 | a\<^sub>3
bulwahn@40647
   531
wenzelm@53015
   532
notation (output) a\<^sub>1  ("a\<^sub>1")
wenzelm@53015
   533
notation (output) a\<^sub>2  ("a\<^sub>2")
wenzelm@53015
   534
notation (output) a\<^sub>3  ("a\<^sub>3")
bulwahn@40900
   535
haftmann@49950
   536
lemma UNIV_finite_3:
wenzelm@53015
   537
  "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3}"
haftmann@49950
   538
  by (auto intro: finite_3.exhaust)
haftmann@49950
   539
bulwahn@40647
   540
instantiation finite_3 :: enum
bulwahn@40647
   541
begin
bulwahn@40647
   542
bulwahn@40647
   543
definition
wenzelm@53015
   544
  "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3]"
bulwahn@40647
   545
bulwahn@41078
   546
definition
wenzelm@53015
   547
  "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3"
bulwahn@41078
   548
bulwahn@41078
   549
definition
wenzelm@53015
   550
  "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3"
bulwahn@41078
   551
bulwahn@40647
   552
instance proof
haftmann@49950
   553
qed (simp_all only: enum_finite_3_def enum_all_finite_3_def enum_ex_finite_3_def UNIV_finite_3, simp_all)
bulwahn@40647
   554
bulwahn@40647
   555
end
bulwahn@40647
   556
bulwahn@40651
   557
instantiation finite_3 :: linorder
bulwahn@40651
   558
begin
bulwahn@40651
   559
bulwahn@40651
   560
definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   561
where
wenzelm@53015
   562
  "x < y = (case x of a\<^sub>1 \<Rightarrow> y \<noteq> a\<^sub>1 | a\<^sub>2 \<Rightarrow> y = a\<^sub>3 | a\<^sub>3 \<Rightarrow> False)"
bulwahn@40651
   563
bulwahn@40651
   564
definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   565
where
haftmann@49950
   566
  "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_3)"
bulwahn@40651
   567
bulwahn@40651
   568
instance proof (intro_classes)
bulwahn@40651
   569
qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
bulwahn@40651
   570
bulwahn@40651
   571
end
bulwahn@40651
   572
wenzelm@53015
   573
hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3
bulwahn@40657
   574
wenzelm@53015
   575
datatype finite_4 = a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4
bulwahn@40647
   576
wenzelm@53015
   577
notation (output) a\<^sub>1  ("a\<^sub>1")
wenzelm@53015
   578
notation (output) a\<^sub>2  ("a\<^sub>2")
wenzelm@53015
   579
notation (output) a\<^sub>3  ("a\<^sub>3")
wenzelm@53015
   580
notation (output) a\<^sub>4  ("a\<^sub>4")
bulwahn@40900
   581
haftmann@49950
   582
lemma UNIV_finite_4:
wenzelm@53015
   583
  "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4}"
haftmann@49950
   584
  by (auto intro: finite_4.exhaust)
haftmann@49950
   585
bulwahn@40647
   586
instantiation finite_4 :: enum
bulwahn@40647
   587
begin
bulwahn@40647
   588
bulwahn@40647
   589
definition
wenzelm@53015
   590
  "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4]"
bulwahn@40647
   591
bulwahn@41078
   592
definition
wenzelm@53015
   593
  "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3 \<and> P a\<^sub>4"
bulwahn@41078
   594
bulwahn@41078
   595
definition
wenzelm@53015
   596
  "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3 \<or> P a\<^sub>4"
bulwahn@41078
   597
bulwahn@40647
   598
instance proof
haftmann@49950
   599
qed (simp_all only: enum_finite_4_def enum_all_finite_4_def enum_ex_finite_4_def UNIV_finite_4, simp_all)
bulwahn@40647
   600
bulwahn@40647
   601
end
bulwahn@40647
   602
wenzelm@53015
   603
hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4
bulwahn@40651
   604
bulwahn@40651
   605
wenzelm@53015
   606
datatype finite_5 = a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4 | a\<^sub>5
bulwahn@40647
   607
wenzelm@53015
   608
notation (output) a\<^sub>1  ("a\<^sub>1")
wenzelm@53015
   609
notation (output) a\<^sub>2  ("a\<^sub>2")
wenzelm@53015
   610
notation (output) a\<^sub>3  ("a\<^sub>3")
wenzelm@53015
   611
notation (output) a\<^sub>4  ("a\<^sub>4")
wenzelm@53015
   612
notation (output) a\<^sub>5  ("a\<^sub>5")
bulwahn@40900
   613
haftmann@49950
   614
lemma UNIV_finite_5:
wenzelm@53015
   615
  "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5}"
haftmann@49950
   616
  by (auto intro: finite_5.exhaust)
haftmann@49950
   617
bulwahn@40647
   618
instantiation finite_5 :: enum
bulwahn@40647
   619
begin
bulwahn@40647
   620
bulwahn@40647
   621
definition
wenzelm@53015
   622
  "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5]"
bulwahn@40647
   623
bulwahn@41078
   624
definition
wenzelm@53015
   625
  "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3 \<and> P a\<^sub>4 \<and> P a\<^sub>5"
bulwahn@41078
   626
bulwahn@41078
   627
definition
wenzelm@53015
   628
  "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3 \<or> P a\<^sub>4 \<or> P a\<^sub>5"
bulwahn@41078
   629
bulwahn@40647
   630
instance proof
haftmann@49950
   631
qed (simp_all only: enum_finite_5_def enum_all_finite_5_def enum_ex_finite_5_def UNIV_finite_5, simp_all)
bulwahn@40647
   632
bulwahn@40647
   633
end
bulwahn@40647
   634
wenzelm@53015
   635
hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4 a\<^sub>5
bulwahn@46352
   636
haftmann@49948
   637
bulwahn@46352
   638
subsection {* Closing up *}
bulwahn@40657
   639
bulwahn@41085
   640
hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
haftmann@49948
   641
hide_const (open) enum enum_all enum_ex all_n_lists ex_n_lists ntrancl
bulwahn@40647
   642
bulwahn@40647
   643
end
haftmann@49948
   644