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