src/HOL/Enum.thy
author bulwahn
Sun Dec 16 18:07:29 2012 +0100 (2012-12-16)
changeset 50567 768a3fbe4149
parent 49972 f11f8905d9fd
child 52435 6646bb548c6b
permissions -rw-r--r--
providing a custom code equation for vimage to overwrite the vimage definition that would be rewritten by set_comprehension_pointfree simproc in the code preprocessor to an non-terminating code equation
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@49949
   120
code_const enum_the (Eval "(fn p => raise Match)")
haftmann@49949
   121
haftmann@49949
   122
haftmann@49949
   123
subsubsection {* Equality and order on functions *}
haftmann@26348
   124
haftmann@38857
   125
instantiation "fun" :: (enum, equal) equal
haftmann@26513
   126
begin
haftmann@26348
   127
haftmann@26513
   128
definition
haftmann@38857
   129
  "HOL.equal f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
haftmann@26513
   130
haftmann@31464
   131
instance proof
haftmann@49950
   132
qed (simp_all add: equal_fun_def fun_eq_iff enum_UNIV)
haftmann@26513
   133
haftmann@26513
   134
end
haftmann@26348
   135
bulwahn@40898
   136
lemma [code]:
bulwahn@41078
   137
  "HOL.equal f g \<longleftrightarrow> enum_all (%x. f x = g x)"
haftmann@49950
   138
  by (auto simp add: equal fun_eq_iff)
bulwahn@40898
   139
haftmann@38857
   140
lemma [code nbe]:
haftmann@38857
   141
  "HOL.equal (f :: _ \<Rightarrow> _) f \<longleftrightarrow> True"
haftmann@38857
   142
  by (fact equal_refl)
haftmann@38857
   143
haftmann@28562
   144
lemma order_fun [code]:
haftmann@26348
   145
  fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
bulwahn@41078
   146
  shows "f \<le> g \<longleftrightarrow> enum_all (\<lambda>x. f x \<le> g x)"
bulwahn@41078
   147
    and "f < g \<longleftrightarrow> f \<le> g \<and> enum_ex (\<lambda>x. f x \<noteq> g x)"
haftmann@49950
   148
  by (simp_all add: fun_eq_iff le_fun_def order_less_le)
haftmann@26968
   149
haftmann@26968
   150
haftmann@49949
   151
subsubsection {* Operations on relations *}
haftmann@49949
   152
haftmann@49949
   153
lemma [code]:
haftmann@49949
   154
  "Id = image (\<lambda>x. (x, x)) (set Enum.enum)"
haftmann@49949
   155
  by (auto intro: imageI in_enum)
haftmann@26968
   156
haftmann@49949
   157
lemma tranclp_unfold [code, no_atp]:
haftmann@49949
   158
  "tranclp r a b \<longleftrightarrow> (a, b) \<in> trancl {(x, y). r x y}"
haftmann@49949
   159
  by (simp add: trancl_def)
haftmann@49949
   160
haftmann@49949
   161
lemma rtranclp_rtrancl_eq [code, no_atp]:
haftmann@49949
   162
  "rtranclp r x y \<longleftrightarrow> (x, y) \<in> rtrancl {(x, y). r x y}"
haftmann@49949
   163
  by (simp add: rtrancl_def)
haftmann@26968
   164
haftmann@49949
   165
lemma max_ext_eq [code]:
haftmann@49949
   166
  "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
   167
  by (auto simp add: max_ext.simps)
haftmann@49949
   168
haftmann@49949
   169
lemma max_extp_eq [code]:
haftmann@49949
   170
  "max_extp r x y \<longleftrightarrow> (x, y) \<in> max_ext {(x, y). r x y}"
haftmann@49949
   171
  by (simp add: max_ext_def)
haftmann@26348
   172
haftmann@49949
   173
lemma mlex_eq [code]:
haftmann@49949
   174
  "f <*mlex*> R = {(x, y). f x < f y \<or> (f x \<le> f y \<and> (x, y) \<in> R)}"
haftmann@49949
   175
  by (auto simp add: mlex_prod_def)
haftmann@49949
   176
haftmann@49949
   177
lemma [code]:
haftmann@49949
   178
  fixes xs :: "('a::finite \<times> 'a) list"
haftmann@49949
   179
  shows "acc (set xs) = bacc (set xs) (card_UNIV TYPE('a))"
haftmann@49949
   180
  by (simp add: card_UNIV_def acc_bacc_eq)
haftmann@49949
   181
haftmann@49949
   182
lemma [code]:
haftmann@49949
   183
  "accp r = (\<lambda>x. x \<in> acc {(x, y). r x y})"
haftmann@49949
   184
  by (simp add: acc_def)
bulwahn@40652
   185
haftmann@26348
   186
haftmann@49949
   187
subsection {* Default instances for @{class enum} *}
haftmann@26348
   188
haftmann@26444
   189
lemma map_of_zip_enum_is_Some:
haftmann@26444
   190
  assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   191
  shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
haftmann@26444
   192
proof -
haftmann@26444
   193
  from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
haftmann@26444
   194
    (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
haftmann@26444
   195
    by (auto intro!: map_of_zip_is_Some)
bulwahn@41078
   196
  then show ?thesis using enum_UNIV by auto
haftmann@26444
   197
qed
haftmann@26444
   198
haftmann@26444
   199
lemma map_of_zip_enum_inject:
haftmann@26444
   200
  fixes xs ys :: "'b\<Colon>enum list"
haftmann@26444
   201
  assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   202
      "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   203
    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
   204
  shows "xs = ys"
haftmann@26444
   205
proof -
haftmann@26444
   206
  have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
haftmann@26444
   207
  proof
haftmann@26444
   208
    fix x :: 'a
haftmann@26444
   209
    from length map_of_zip_enum_is_Some obtain y1 y2
haftmann@26444
   210
      where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
haftmann@26444
   211
        and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
wenzelm@47230
   212
    moreover from map_of
wenzelm@47230
   213
      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
   214
      by (auto dest: fun_cong)
haftmann@26444
   215
    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
   216
      by simp
haftmann@26444
   217
  qed
haftmann@26444
   218
  with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
haftmann@26444
   219
qed
haftmann@26444
   220
haftmann@49950
   221
definition all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   222
where
haftmann@49950
   223
  "all_n_lists P n \<longleftrightarrow> (\<forall>xs \<in> set (List.n_lists n enum). P xs)"
bulwahn@41078
   224
bulwahn@41078
   225
lemma [code]:
haftmann@49950
   226
  "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
   227
  unfolding all_n_lists_def enum_all
haftmann@49950
   228
  by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   229
haftmann@49950
   230
definition ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   231
where
haftmann@49950
   232
  "ex_n_lists P n \<longleftrightarrow> (\<exists>xs \<in> set (List.n_lists n enum). P xs)"
bulwahn@41078
   233
bulwahn@41078
   234
lemma [code]:
haftmann@49950
   235
  "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
   236
  unfolding ex_n_lists_def enum_ex
haftmann@49950
   237
  by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   238
haftmann@26444
   239
instantiation "fun" :: (enum, enum) enum
haftmann@26444
   240
begin
haftmann@26444
   241
haftmann@26444
   242
definition
haftmann@49948
   243
  "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
   244
bulwahn@41078
   245
definition
bulwahn@41078
   246
  "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   247
bulwahn@41078
   248
definition
bulwahn@41078
   249
  "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   250
haftmann@26444
   251
instance proof
haftmann@26444
   252
  show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   253
  proof (rule UNIV_eq_I)
haftmann@26444
   254
    fix f :: "'a \<Rightarrow> 'b"
haftmann@26444
   255
    have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@40683
   256
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
haftmann@26444
   257
    then show "f \<in> set enum"
bulwahn@40683
   258
      by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
haftmann@26444
   259
  qed
haftmann@26444
   260
next
haftmann@26444
   261
  from map_of_zip_enum_inject
haftmann@26444
   262
  show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   263
    by (auto intro!: inj_onI simp add: enum_fun_def
haftmann@49950
   264
      distinct_map distinct_n_lists enum_distinct set_n_lists)
bulwahn@41078
   265
next
bulwahn@41078
   266
  fix P
haftmann@49950
   267
  show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Ball UNIV P"
bulwahn@41078
   268
  proof
bulwahn@41078
   269
    assume "enum_all P"
haftmann@49950
   270
    show "Ball UNIV P"
bulwahn@41078
   271
    proof
bulwahn@41078
   272
      fix f :: "'a \<Rightarrow> 'b"
bulwahn@41078
   273
      have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   274
        by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
bulwahn@41078
   275
      from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
bulwahn@41078
   276
        unfolding enum_all_fun_def all_n_lists_def
bulwahn@41078
   277
        apply (simp add: set_n_lists)
bulwahn@41078
   278
        apply (erule_tac x="map f enum" in allE)
bulwahn@41078
   279
        apply (auto intro!: in_enum)
bulwahn@41078
   280
        done
bulwahn@41078
   281
      from this f show "P f" by auto
bulwahn@41078
   282
    qed
bulwahn@41078
   283
  next
haftmann@49950
   284
    assume "Ball UNIV P"
bulwahn@41078
   285
    from this show "enum_all P"
bulwahn@41078
   286
      unfolding enum_all_fun_def all_n_lists_def by auto
bulwahn@41078
   287
  qed
bulwahn@41078
   288
next
bulwahn@41078
   289
  fix P
haftmann@49950
   290
  show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Bex UNIV P"
bulwahn@41078
   291
  proof
bulwahn@41078
   292
    assume "enum_ex P"
haftmann@49950
   293
    from this show "Bex UNIV P"
bulwahn@41078
   294
      unfolding enum_ex_fun_def ex_n_lists_def by auto
bulwahn@41078
   295
  next
haftmann@49950
   296
    assume "Bex UNIV P"
bulwahn@41078
   297
    from this obtain f where "P f" ..
bulwahn@41078
   298
    have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   299
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum) 
bulwahn@41078
   300
    from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
bulwahn@41078
   301
      by auto
bulwahn@41078
   302
    from  this show "enum_ex P"
bulwahn@41078
   303
      unfolding enum_ex_fun_def ex_n_lists_def
bulwahn@41078
   304
      apply (auto simp add: set_n_lists)
bulwahn@41078
   305
      apply (rule_tac x="map f enum" in exI)
bulwahn@41078
   306
      apply (auto intro!: in_enum)
bulwahn@41078
   307
      done
bulwahn@41078
   308
  qed
haftmann@26444
   309
qed
haftmann@26444
   310
haftmann@26444
   311
end
haftmann@26444
   312
haftmann@38857
   313
lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
haftmann@49948
   314
  in map (\<lambda>ys. the o map_of (zip enum_a ys)) (List.n_lists (length enum_a) enum))"
haftmann@28245
   315
  by (simp add: enum_fun_def Let_def)
haftmann@26444
   316
bulwahn@41078
   317
lemma enum_all_fun_code [code]:
bulwahn@41078
   318
  "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   319
   in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
haftmann@49950
   320
  by (simp only: enum_all_fun_def Let_def)
bulwahn@41078
   321
bulwahn@41078
   322
lemma enum_ex_fun_code [code]:
bulwahn@41078
   323
  "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   324
   in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
haftmann@49950
   325
  by (simp only: enum_ex_fun_def Let_def)
haftmann@45963
   326
haftmann@45963
   327
instantiation set :: (enum) enum
haftmann@45963
   328
begin
haftmann@45963
   329
haftmann@45963
   330
definition
haftmann@45963
   331
  "enum = map set (sublists enum)"
haftmann@45963
   332
haftmann@45963
   333
definition
haftmann@45963
   334
  "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
haftmann@45963
   335
haftmann@45963
   336
definition
haftmann@45963
   337
  "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
haftmann@45963
   338
haftmann@45963
   339
instance proof
haftmann@45963
   340
qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
haftmann@45963
   341
  enum_distinct enum_UNIV)
huffman@29024
   342
huffman@29024
   343
end
huffman@29024
   344
haftmann@49950
   345
instantiation unit :: enum
haftmann@49950
   346
begin
haftmann@49950
   347
haftmann@49950
   348
definition
haftmann@49950
   349
  "enum = [()]"
haftmann@49950
   350
haftmann@49950
   351
definition
haftmann@49950
   352
  "enum_all P = P ()"
haftmann@49950
   353
haftmann@49950
   354
definition
haftmann@49950
   355
  "enum_ex P = P ()"
haftmann@49950
   356
haftmann@49950
   357
instance proof
haftmann@49950
   358
qed (auto simp add: enum_unit_def enum_all_unit_def enum_ex_unit_def)
haftmann@49950
   359
haftmann@49950
   360
end
haftmann@49950
   361
haftmann@49950
   362
instantiation bool :: enum
haftmann@49950
   363
begin
haftmann@49950
   364
haftmann@49950
   365
definition
haftmann@49950
   366
  "enum = [False, True]"
haftmann@49950
   367
haftmann@49950
   368
definition
haftmann@49950
   369
  "enum_all P \<longleftrightarrow> P False \<and> P True"
haftmann@49950
   370
haftmann@49950
   371
definition
haftmann@49950
   372
  "enum_ex P \<longleftrightarrow> P False \<or> P True"
haftmann@49950
   373
haftmann@49950
   374
instance proof
haftmann@49950
   375
qed (simp_all only: enum_bool_def enum_all_bool_def enum_ex_bool_def UNIV_bool, simp_all)
haftmann@49950
   376
haftmann@49950
   377
end
haftmann@49950
   378
haftmann@49950
   379
instantiation prod :: (enum, enum) enum
haftmann@49950
   380
begin
haftmann@49950
   381
haftmann@49950
   382
definition
haftmann@49950
   383
  "enum = List.product enum enum"
haftmann@49950
   384
haftmann@49950
   385
definition
haftmann@49950
   386
  "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
haftmann@49950
   387
haftmann@49950
   388
definition
haftmann@49950
   389
  "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
haftmann@49950
   390
haftmann@49950
   391
 
haftmann@49950
   392
instance by default
haftmann@49950
   393
  (simp_all add: enum_prod_def product_list_set distinct_product
haftmann@49950
   394
    enum_UNIV enum_distinct enum_all_prod_def enum_ex_prod_def)
haftmann@49950
   395
haftmann@49950
   396
end
haftmann@49950
   397
haftmann@49950
   398
instantiation sum :: (enum, enum) enum
haftmann@49950
   399
begin
haftmann@49950
   400
haftmann@49950
   401
definition
haftmann@49950
   402
  "enum = map Inl enum @ map Inr enum"
haftmann@49950
   403
haftmann@49950
   404
definition
haftmann@49950
   405
  "enum_all P \<longleftrightarrow> enum_all (\<lambda>x. P (Inl x)) \<and> enum_all (\<lambda>x. P (Inr x))"
haftmann@49950
   406
haftmann@49950
   407
definition
haftmann@49950
   408
  "enum_ex P \<longleftrightarrow> enum_ex (\<lambda>x. P (Inl x)) \<or> enum_ex (\<lambda>x. P (Inr x))"
haftmann@49950
   409
haftmann@49950
   410
instance proof
haftmann@49950
   411
qed (simp_all only: enum_sum_def enum_all_sum_def enum_ex_sum_def UNIV_sum,
haftmann@49950
   412
  auto simp add: enum_UNIV distinct_map enum_distinct)
haftmann@49950
   413
haftmann@49950
   414
end
haftmann@49950
   415
haftmann@49950
   416
instantiation option :: (enum) enum
haftmann@49950
   417
begin
haftmann@49950
   418
haftmann@49950
   419
definition
haftmann@49950
   420
  "enum = None # map Some enum"
haftmann@49950
   421
haftmann@49950
   422
definition
haftmann@49950
   423
  "enum_all P \<longleftrightarrow> P None \<and> enum_all (\<lambda>x. P (Some x))"
haftmann@49950
   424
haftmann@49950
   425
definition
haftmann@49950
   426
  "enum_ex P \<longleftrightarrow> P None \<or> enum_ex (\<lambda>x. P (Some x))"
haftmann@49950
   427
haftmann@49950
   428
instance proof
haftmann@49950
   429
qed (simp_all only: enum_option_def enum_all_option_def enum_ex_option_def UNIV_option_conv,
haftmann@49950
   430
  auto simp add: distinct_map enum_UNIV enum_distinct)
haftmann@49950
   431
haftmann@49950
   432
end
haftmann@49950
   433
haftmann@45963
   434
bulwahn@40647
   435
subsection {* Small finite types *}
bulwahn@40647
   436
bulwahn@40647
   437
text {* We define small finite types for the use in Quickcheck *}
bulwahn@40647
   438
bulwahn@40647
   439
datatype finite_1 = a\<^isub>1
bulwahn@40647
   440
bulwahn@40900
   441
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   442
haftmann@49950
   443
lemma UNIV_finite_1:
haftmann@49950
   444
  "UNIV = {a\<^isub>1}"
haftmann@49950
   445
  by (auto intro: finite_1.exhaust)
haftmann@49950
   446
bulwahn@40647
   447
instantiation finite_1 :: enum
bulwahn@40647
   448
begin
bulwahn@40647
   449
bulwahn@40647
   450
definition
bulwahn@40647
   451
  "enum = [a\<^isub>1]"
bulwahn@40647
   452
bulwahn@41078
   453
definition
bulwahn@41078
   454
  "enum_all P = P a\<^isub>1"
bulwahn@41078
   455
bulwahn@41078
   456
definition
bulwahn@41078
   457
  "enum_ex P = P a\<^isub>1"
bulwahn@41078
   458
bulwahn@40647
   459
instance proof
haftmann@49950
   460
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
   461
huffman@29024
   462
end
bulwahn@40647
   463
bulwahn@40651
   464
instantiation finite_1 :: linorder
bulwahn@40651
   465
begin
bulwahn@40651
   466
haftmann@49950
   467
definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
haftmann@49950
   468
where
haftmann@49950
   469
  "x < (y :: finite_1) \<longleftrightarrow> False"
haftmann@49950
   470
bulwahn@40651
   471
definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   472
where
haftmann@49950
   473
  "x \<le> (y :: finite_1) \<longleftrightarrow> True"
bulwahn@40651
   474
bulwahn@40651
   475
instance
bulwahn@40651
   476
apply (intro_classes)
bulwahn@40651
   477
apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
bulwahn@40651
   478
apply (metis finite_1.exhaust)
bulwahn@40651
   479
done
bulwahn@40651
   480
bulwahn@40651
   481
end
bulwahn@40651
   482
bulwahn@41085
   483
hide_const (open) a\<^isub>1
bulwahn@40657
   484
bulwahn@40647
   485
datatype finite_2 = a\<^isub>1 | a\<^isub>2
bulwahn@40647
   486
bulwahn@40900
   487
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   488
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   489
haftmann@49950
   490
lemma UNIV_finite_2:
haftmann@49950
   491
  "UNIV = {a\<^isub>1, a\<^isub>2}"
haftmann@49950
   492
  by (auto intro: finite_2.exhaust)
haftmann@49950
   493
bulwahn@40647
   494
instantiation finite_2 :: enum
bulwahn@40647
   495
begin
bulwahn@40647
   496
bulwahn@40647
   497
definition
bulwahn@40647
   498
  "enum = [a\<^isub>1, a\<^isub>2]"
bulwahn@40647
   499
bulwahn@41078
   500
definition
haftmann@49950
   501
  "enum_all P \<longleftrightarrow> P a\<^isub>1 \<and> P a\<^isub>2"
bulwahn@41078
   502
bulwahn@41078
   503
definition
haftmann@49950
   504
  "enum_ex P \<longleftrightarrow> P a\<^isub>1 \<or> P a\<^isub>2"
bulwahn@41078
   505
bulwahn@40647
   506
instance proof
haftmann@49950
   507
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
   508
bulwahn@40647
   509
end
bulwahn@40647
   510
bulwahn@40651
   511
instantiation finite_2 :: linorder
bulwahn@40651
   512
begin
bulwahn@40651
   513
bulwahn@40651
   514
definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   515
where
haftmann@49950
   516
  "x < y \<longleftrightarrow> x = a\<^isub>1 \<and> y = a\<^isub>2"
bulwahn@40651
   517
bulwahn@40651
   518
definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   519
where
haftmann@49950
   520
  "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_2)"
bulwahn@40651
   521
bulwahn@40651
   522
instance
bulwahn@40651
   523
apply (intro_classes)
bulwahn@40651
   524
apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
haftmann@49950
   525
apply (metis finite_2.nchotomy)+
bulwahn@40651
   526
done
bulwahn@40651
   527
bulwahn@40651
   528
end
bulwahn@40651
   529
bulwahn@41085
   530
hide_const (open) a\<^isub>1 a\<^isub>2
bulwahn@40657
   531
bulwahn@40647
   532
datatype finite_3 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3
bulwahn@40647
   533
bulwahn@40900
   534
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   535
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   536
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   537
haftmann@49950
   538
lemma UNIV_finite_3:
haftmann@49950
   539
  "UNIV = {a\<^isub>1, a\<^isub>2, a\<^isub>3}"
haftmann@49950
   540
  by (auto intro: finite_3.exhaust)
haftmann@49950
   541
bulwahn@40647
   542
instantiation finite_3 :: enum
bulwahn@40647
   543
begin
bulwahn@40647
   544
bulwahn@40647
   545
definition
bulwahn@40647
   546
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3]"
bulwahn@40647
   547
bulwahn@41078
   548
definition
haftmann@49950
   549
  "enum_all P \<longleftrightarrow> P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3"
bulwahn@41078
   550
bulwahn@41078
   551
definition
haftmann@49950
   552
  "enum_ex P \<longleftrightarrow> P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3"
bulwahn@41078
   553
bulwahn@40647
   554
instance proof
haftmann@49950
   555
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
   556
bulwahn@40647
   557
end
bulwahn@40647
   558
bulwahn@40651
   559
instantiation finite_3 :: linorder
bulwahn@40651
   560
begin
bulwahn@40651
   561
bulwahn@40651
   562
definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   563
where
haftmann@49950
   564
  "x < y = (case x of a\<^isub>1 \<Rightarrow> y \<noteq> a\<^isub>1 | a\<^isub>2 \<Rightarrow> y = a\<^isub>3 | a\<^isub>3 \<Rightarrow> False)"
bulwahn@40651
   565
bulwahn@40651
   566
definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   567
where
haftmann@49950
   568
  "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_3)"
bulwahn@40651
   569
bulwahn@40651
   570
instance proof (intro_classes)
bulwahn@40651
   571
qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
bulwahn@40651
   572
bulwahn@40651
   573
end
bulwahn@40651
   574
bulwahn@41085
   575
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3
bulwahn@40657
   576
bulwahn@40647
   577
datatype finite_4 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4
bulwahn@40647
   578
bulwahn@40900
   579
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   580
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   581
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   582
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   583
haftmann@49950
   584
lemma UNIV_finite_4:
haftmann@49950
   585
  "UNIV = {a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4}"
haftmann@49950
   586
  by (auto intro: finite_4.exhaust)
haftmann@49950
   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
haftmann@49950
   595
  "enum_all P \<longleftrightarrow> 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
haftmann@49950
   598
  "enum_ex P \<longleftrightarrow> 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
haftmann@49950
   601
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
   602
bulwahn@40647
   603
end
bulwahn@40647
   604
bulwahn@41085
   605
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4
bulwahn@40651
   606
bulwahn@40651
   607
bulwahn@40647
   608
datatype finite_5 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4 | a\<^isub>5
bulwahn@40647
   609
bulwahn@40900
   610
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   611
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   612
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   613
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   614
notation (output) a\<^isub>5  ("a\<^isub>5")
bulwahn@40900
   615
haftmann@49950
   616
lemma UNIV_finite_5:
haftmann@49950
   617
  "UNIV = {a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5}"
haftmann@49950
   618
  by (auto intro: finite_5.exhaust)
haftmann@49950
   619
bulwahn@40647
   620
instantiation finite_5 :: enum
bulwahn@40647
   621
begin
bulwahn@40647
   622
bulwahn@40647
   623
definition
bulwahn@40647
   624
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5]"
bulwahn@40647
   625
bulwahn@41078
   626
definition
haftmann@49950
   627
  "enum_all P \<longleftrightarrow> 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
   628
bulwahn@41078
   629
definition
haftmann@49950
   630
  "enum_ex P \<longleftrightarrow> 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
   631
bulwahn@40647
   632
instance proof
haftmann@49950
   633
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
   634
bulwahn@40647
   635
end
bulwahn@40647
   636
bulwahn@46352
   637
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4 a\<^isub>5
bulwahn@46352
   638
haftmann@49948
   639
bulwahn@46352
   640
subsection {* Closing up *}
bulwahn@40657
   641
bulwahn@41085
   642
hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
haftmann@49948
   643
hide_const (open) enum enum_all enum_ex all_n_lists ex_n_lists ntrancl
bulwahn@40647
   644
bulwahn@40647
   645
end
haftmann@49948
   646