src/HOL/Enum.thy
author wenzelm
Fri Mar 30 17:25:34 2012 +0200 (2012-03-30)
changeset 47231 3ff8c79a9e2f
parent 47221 7205eb4a0a05
parent 47230 6584098d5378
child 48123 104e5fccea12
permissions -rw-r--r--
merged
haftmann@31596
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@26348
     2
haftmann@26348
     3
header {* Finite types as explicit enumerations *}
haftmann@26348
     4
haftmann@26348
     5
theory Enum
bulwahn@40650
     6
imports Map String
haftmann@26348
     7
begin
haftmann@26348
     8
haftmann@26348
     9
subsection {* Class @{text enum} *}
haftmann@26348
    10
haftmann@29797
    11
class enum =
haftmann@26348
    12
  fixes enum :: "'a list"
bulwahn@41078
    13
  fixes enum_all :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
bulwahn@41078
    14
  fixes enum_ex  :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
haftmann@33635
    15
  assumes UNIV_enum: "UNIV = set enum"
haftmann@26444
    16
    and enum_distinct: "distinct enum"
bulwahn@41078
    17
  assumes enum_all : "enum_all P = (\<forall> x. P x)"
bulwahn@41078
    18
  assumes enum_ex  : "enum_ex P = (\<exists> x. P x)" 
haftmann@26348
    19
begin
haftmann@26348
    20
haftmann@29797
    21
subclass finite proof
haftmann@29797
    22
qed (simp add: UNIV_enum)
haftmann@26444
    23
bulwahn@41078
    24
lemma enum_UNIV: "set enum = UNIV" unfolding UNIV_enum ..
haftmann@26444
    25
bulwahn@40683
    26
lemma in_enum: "x \<in> set enum"
bulwahn@41078
    27
  unfolding enum_UNIV by auto
haftmann@26348
    28
haftmann@26348
    29
lemma enum_eq_I:
haftmann@26348
    30
  assumes "\<And>x. x \<in> set xs"
haftmann@26348
    31
  shows "set enum = set xs"
haftmann@26348
    32
proof -
haftmann@26348
    33
  from assms UNIV_eq_I have "UNIV = set xs" by auto
bulwahn@41078
    34
  with enum_UNIV show ?thesis by simp
haftmann@26348
    35
qed
haftmann@26348
    36
haftmann@26348
    37
end
haftmann@26348
    38
haftmann@26348
    39
haftmann@26348
    40
subsection {* Equality and order on functions *}
haftmann@26348
    41
haftmann@38857
    42
instantiation "fun" :: (enum, equal) equal
haftmann@26513
    43
begin
haftmann@26348
    44
haftmann@26513
    45
definition
haftmann@38857
    46
  "HOL.equal f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
haftmann@26513
    47
haftmann@31464
    48
instance proof
bulwahn@41078
    49
qed (simp_all add: equal_fun_def enum_UNIV fun_eq_iff)
haftmann@26513
    50
haftmann@26513
    51
end
haftmann@26348
    52
bulwahn@40898
    53
lemma [code]:
bulwahn@41078
    54
  "HOL.equal f g \<longleftrightarrow> enum_all (%x. f x = g x)"
bulwahn@41078
    55
by (auto simp add: equal enum_all fun_eq_iff)
bulwahn@40898
    56
haftmann@38857
    57
lemma [code nbe]:
haftmann@38857
    58
  "HOL.equal (f :: _ \<Rightarrow> _) f \<longleftrightarrow> True"
haftmann@38857
    59
  by (fact equal_refl)
haftmann@38857
    60
haftmann@28562
    61
lemma order_fun [code]:
haftmann@26348
    62
  fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
bulwahn@41078
    63
  shows "f \<le> g \<longleftrightarrow> enum_all (\<lambda>x. f x \<le> g x)"
bulwahn@41078
    64
    and "f < g \<longleftrightarrow> f \<le> g \<and> enum_ex (\<lambda>x. f x \<noteq> g x)"
bulwahn@41078
    65
  by (simp_all add: enum_all enum_ex fun_eq_iff le_fun_def order_less_le)
haftmann@26968
    66
haftmann@26968
    67
haftmann@26968
    68
subsection {* Quantifiers *}
haftmann@26968
    69
bulwahn@41078
    70
lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> enum_all P"
bulwahn@41078
    71
  by (simp add: enum_all)
haftmann@26968
    72
bulwahn@41078
    73
lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> enum_ex P"
bulwahn@41078
    74
  by (simp add: enum_ex)
haftmann@26348
    75
bulwahn@40652
    76
lemma exists1_code[code]: "(\<exists>!x. P x) \<longleftrightarrow> list_ex1 P enum"
bulwahn@41078
    77
unfolding list_ex1_iff enum_UNIV by auto
bulwahn@40652
    78
haftmann@26348
    79
haftmann@26348
    80
subsection {* Default instances *}
haftmann@26348
    81
haftmann@26444
    82
primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where
haftmann@26444
    83
  "n_lists 0 xs = [[]]"
haftmann@26444
    84
  | "n_lists (Suc n) xs = concat (map (\<lambda>ys. map (\<lambda>y. y # ys) xs) (n_lists n xs))"
haftmann@26444
    85
haftmann@26444
    86
lemma n_lists_Nil [simp]: "n_lists n [] = (if n = 0 then [[]] else [])"
haftmann@26444
    87
  by (induct n) simp_all
haftmann@26444
    88
haftmann@26444
    89
lemma length_n_lists: "length (n_lists n xs) = length xs ^ n"
hoelzl@33639
    90
  by (induct n) (auto simp add: length_concat o_def listsum_triv)
haftmann@26444
    91
haftmann@26444
    92
lemma length_n_lists_elem: "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
haftmann@26444
    93
  by (induct n arbitrary: ys) auto
haftmann@26444
    94
haftmann@26444
    95
lemma set_n_lists: "set (n_lists n xs) = {ys. length ys = n \<and> set ys \<subseteq> set xs}"
nipkow@39302
    96
proof (rule set_eqI)
haftmann@26444
    97
  fix ys :: "'a list"
haftmann@26444
    98
  show "ys \<in> set (n_lists n xs) \<longleftrightarrow> ys \<in> {ys. length ys = n \<and> set ys \<subseteq> set xs}"
haftmann@26444
    99
  proof -
haftmann@26444
   100
    have "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
haftmann@26444
   101
      by (induct n arbitrary: ys) auto
haftmann@26444
   102
    moreover have "\<And>x. ys \<in> set (n_lists n xs) \<Longrightarrow> x \<in> set ys \<Longrightarrow> x \<in> set xs"
haftmann@26444
   103
      by (induct n arbitrary: ys) auto
haftmann@26444
   104
    moreover have "set ys \<subseteq> set xs \<Longrightarrow> ys \<in> set (n_lists (length ys) xs)"
haftmann@26444
   105
      by (induct ys) auto
haftmann@26444
   106
    ultimately show ?thesis by auto
haftmann@26444
   107
  qed
haftmann@26444
   108
qed
haftmann@26444
   109
haftmann@26444
   110
lemma distinct_n_lists:
haftmann@26444
   111
  assumes "distinct xs"
haftmann@26444
   112
  shows "distinct (n_lists n xs)"
haftmann@26444
   113
proof (rule card_distinct)
haftmann@26444
   114
  from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)
haftmann@26444
   115
  have "card (set (n_lists n xs)) = card (set xs) ^ n"
haftmann@26444
   116
  proof (induct n)
haftmann@26444
   117
    case 0 then show ?case by simp
haftmann@26444
   118
  next
haftmann@26444
   119
    case (Suc n)
haftmann@26444
   120
    moreover have "card (\<Union>ys\<in>set (n_lists n xs). (\<lambda>y. y # ys) ` set xs)
haftmann@26444
   121
      = (\<Sum>ys\<in>set (n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))"
haftmann@26444
   122
      by (rule card_UN_disjoint) auto
haftmann@26444
   123
    moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)"
haftmann@26444
   124
      by (rule card_image) (simp add: inj_on_def)
haftmann@26444
   125
    ultimately show ?case by auto
haftmann@26444
   126
  qed
haftmann@26444
   127
  also have "\<dots> = length xs ^ n" by (simp add: card_length)
haftmann@26444
   128
  finally show "card (set (n_lists n xs)) = length (n_lists n xs)"
haftmann@26444
   129
    by (simp add: length_n_lists)
haftmann@26444
   130
qed
haftmann@26444
   131
haftmann@26444
   132
lemma map_of_zip_enum_is_Some:
haftmann@26444
   133
  assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   134
  shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
haftmann@26444
   135
proof -
haftmann@26444
   136
  from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
haftmann@26444
   137
    (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
haftmann@26444
   138
    by (auto intro!: map_of_zip_is_Some)
bulwahn@41078
   139
  then show ?thesis using enum_UNIV by auto
haftmann@26444
   140
qed
haftmann@26444
   141
haftmann@26444
   142
lemma map_of_zip_enum_inject:
haftmann@26444
   143
  fixes xs ys :: "'b\<Colon>enum list"
haftmann@26444
   144
  assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   145
      "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
haftmann@26444
   146
    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
   147
  shows "xs = ys"
haftmann@26444
   148
proof -
haftmann@26444
   149
  have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
haftmann@26444
   150
  proof
haftmann@26444
   151
    fix x :: 'a
haftmann@26444
   152
    from length map_of_zip_enum_is_Some obtain y1 y2
haftmann@26444
   153
      where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
haftmann@26444
   154
        and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
wenzelm@47230
   155
    moreover from map_of
wenzelm@47230
   156
      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
   157
      by (auto dest: fun_cong)
haftmann@26444
   158
    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
   159
      by simp
haftmann@26444
   160
  qed
haftmann@26444
   161
  with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
haftmann@26444
   162
qed
haftmann@26444
   163
bulwahn@41078
   164
definition
bulwahn@41078
   165
  all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   166
where
bulwahn@41078
   167
  "all_n_lists P n = (\<forall>xs \<in> set (n_lists n enum). P xs)"
bulwahn@41078
   168
bulwahn@41078
   169
lemma [code]:
bulwahn@41078
   170
  "all_n_lists P n = (if n = 0 then P [] else enum_all (%x. all_n_lists (%xs. P (x # xs)) (n - 1)))"
bulwahn@41078
   171
unfolding all_n_lists_def enum_all
bulwahn@41078
   172
by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   173
bulwahn@41078
   174
definition
bulwahn@41078
   175
  ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   176
where
bulwahn@41078
   177
  "ex_n_lists P n = (\<exists>xs \<in> set (n_lists n enum). P xs)"
bulwahn@41078
   178
bulwahn@41078
   179
lemma [code]:
bulwahn@41078
   180
  "ex_n_lists P n = (if n = 0 then P [] else enum_ex (%x. ex_n_lists (%xs. P (x # xs)) (n - 1)))"
bulwahn@41078
   181
unfolding ex_n_lists_def enum_ex
bulwahn@41078
   182
by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   183
bulwahn@41078
   184
haftmann@26444
   185
instantiation "fun" :: (enum, enum) enum
haftmann@26444
   186
begin
haftmann@26444
   187
haftmann@26444
   188
definition
haftmann@37765
   189
  "enum = map (\<lambda>ys. the o map_of (zip (enum\<Colon>'a list) ys)) (n_lists (length (enum\<Colon>'a\<Colon>enum list)) enum)"
haftmann@26444
   190
bulwahn@41078
   191
definition
bulwahn@41078
   192
  "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   193
bulwahn@41078
   194
definition
bulwahn@41078
   195
  "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   196
bulwahn@41078
   197
haftmann@26444
   198
instance proof
haftmann@26444
   199
  show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   200
  proof (rule UNIV_eq_I)
haftmann@26444
   201
    fix f :: "'a \<Rightarrow> 'b"
haftmann@26444
   202
    have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@40683
   203
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
haftmann@26444
   204
    then show "f \<in> set enum"
bulwahn@40683
   205
      by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
haftmann@26444
   206
  qed
haftmann@26444
   207
next
haftmann@26444
   208
  from map_of_zip_enum_inject
haftmann@26444
   209
  show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   210
    by (auto intro!: inj_onI simp add: enum_fun_def
haftmann@26444
   211
      distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
bulwahn@41078
   212
next
bulwahn@41078
   213
  fix P
bulwahn@41078
   214
  show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   215
  proof
bulwahn@41078
   216
    assume "enum_all P"
bulwahn@41078
   217
    show "\<forall>x. P x"
bulwahn@41078
   218
    proof
bulwahn@41078
   219
      fix f :: "'a \<Rightarrow> 'b"
bulwahn@41078
   220
      have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   221
        by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
bulwahn@41078
   222
      from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
bulwahn@41078
   223
        unfolding enum_all_fun_def all_n_lists_def
bulwahn@41078
   224
        apply (simp add: set_n_lists)
bulwahn@41078
   225
        apply (erule_tac x="map f enum" in allE)
bulwahn@41078
   226
        apply (auto intro!: in_enum)
bulwahn@41078
   227
        done
bulwahn@41078
   228
      from this f show "P f" by auto
bulwahn@41078
   229
    qed
bulwahn@41078
   230
  next
bulwahn@41078
   231
    assume "\<forall>x. P x"
bulwahn@41078
   232
    from this show "enum_all P"
bulwahn@41078
   233
      unfolding enum_all_fun_def all_n_lists_def by auto
bulwahn@41078
   234
  qed
bulwahn@41078
   235
next
bulwahn@41078
   236
  fix P
bulwahn@41078
   237
  show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   238
  proof
bulwahn@41078
   239
    assume "enum_ex P"
bulwahn@41078
   240
    from this show "\<exists>x. P x"
bulwahn@41078
   241
      unfolding enum_ex_fun_def ex_n_lists_def by auto
bulwahn@41078
   242
  next
bulwahn@41078
   243
    assume "\<exists>x. P x"
bulwahn@41078
   244
    from this obtain f where "P f" ..
bulwahn@41078
   245
    have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   246
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum) 
bulwahn@41078
   247
    from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
bulwahn@41078
   248
      by auto
bulwahn@41078
   249
    from  this show "enum_ex P"
bulwahn@41078
   250
      unfolding enum_ex_fun_def ex_n_lists_def
bulwahn@41078
   251
      apply (auto simp add: set_n_lists)
bulwahn@41078
   252
      apply (rule_tac x="map f enum" in exI)
bulwahn@41078
   253
      apply (auto intro!: in_enum)
bulwahn@41078
   254
      done
bulwahn@41078
   255
  qed
haftmann@26444
   256
qed
haftmann@26444
   257
haftmann@26444
   258
end
haftmann@26444
   259
haftmann@38857
   260
lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
haftmann@28245
   261
  in map (\<lambda>ys. the o map_of (zip enum_a ys)) (n_lists (length enum_a) enum))"
haftmann@28245
   262
  by (simp add: enum_fun_def Let_def)
haftmann@26444
   263
bulwahn@41078
   264
lemma enum_all_fun_code [code]:
bulwahn@41078
   265
  "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   266
   in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
bulwahn@41078
   267
  by (simp add: enum_all_fun_def Let_def)
bulwahn@41078
   268
bulwahn@41078
   269
lemma enum_ex_fun_code [code]:
bulwahn@41078
   270
  "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   271
   in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
bulwahn@41078
   272
  by (simp add: enum_ex_fun_def Let_def)
bulwahn@41078
   273
haftmann@26348
   274
instantiation unit :: enum
haftmann@26348
   275
begin
haftmann@26348
   276
haftmann@26348
   277
definition
haftmann@26348
   278
  "enum = [()]"
haftmann@26348
   279
bulwahn@41078
   280
definition
bulwahn@41078
   281
  "enum_all P = P ()"
bulwahn@41078
   282
bulwahn@41078
   283
definition
bulwahn@41078
   284
  "enum_ex P = P ()"
bulwahn@41078
   285
haftmann@31464
   286
instance proof
bulwahn@41078
   287
qed (auto simp add: enum_unit_def UNIV_unit enum_all_unit_def enum_ex_unit_def intro: unit.exhaust)
haftmann@26348
   288
haftmann@26348
   289
end
haftmann@26348
   290
haftmann@26348
   291
instantiation bool :: enum
haftmann@26348
   292
begin
haftmann@26348
   293
haftmann@26348
   294
definition
haftmann@26348
   295
  "enum = [False, True]"
haftmann@26348
   296
bulwahn@41078
   297
definition
bulwahn@41078
   298
  "enum_all P = (P False \<and> P True)"
bulwahn@41078
   299
bulwahn@41078
   300
definition
bulwahn@41078
   301
  "enum_ex P = (P False \<or> P True)"
bulwahn@41078
   302
haftmann@31464
   303
instance proof
bulwahn@41078
   304
  fix P
bulwahn@41078
   305
  show "enum_all (P :: bool \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   306
    unfolding enum_all_bool_def by (auto, case_tac x) auto
bulwahn@41078
   307
next
bulwahn@41078
   308
  fix P
bulwahn@41078
   309
  show "enum_ex (P :: bool \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   310
    unfolding enum_ex_bool_def by (auto, case_tac x) auto
bulwahn@41078
   311
qed (auto simp add: enum_bool_def UNIV_bool)
haftmann@26348
   312
haftmann@26348
   313
end
haftmann@26348
   314
haftmann@26348
   315
primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
haftmann@26348
   316
  "product [] _ = []"
haftmann@26348
   317
  | "product (x#xs) ys = map (Pair x) ys @ product xs ys"
haftmann@26348
   318
haftmann@26348
   319
lemma product_list_set:
haftmann@26348
   320
  "set (product xs ys) = set xs \<times> set ys"
haftmann@26348
   321
  by (induct xs) auto
haftmann@26348
   322
haftmann@26444
   323
lemma distinct_product:
haftmann@26444
   324
  assumes "distinct xs" and "distinct ys"
haftmann@26444
   325
  shows "distinct (product xs ys)"
haftmann@26444
   326
  using assms by (induct xs)
haftmann@26444
   327
    (auto intro: inj_onI simp add: product_list_set distinct_map)
haftmann@26444
   328
haftmann@37678
   329
instantiation prod :: (enum, enum) enum
haftmann@26348
   330
begin
haftmann@26348
   331
haftmann@26348
   332
definition
haftmann@26348
   333
  "enum = product enum enum"
haftmann@26348
   334
bulwahn@41078
   335
definition
bulwahn@41078
   336
  "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
bulwahn@41078
   337
bulwahn@41078
   338
definition
bulwahn@41078
   339
  "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
bulwahn@41078
   340
bulwahn@41078
   341
 
haftmann@26348
   342
instance by default
bulwahn@41078
   343
  (simp_all add: enum_prod_def product_list_set distinct_product
bulwahn@41078
   344
    enum_UNIV enum_distinct enum_all_prod_def enum_all enum_ex_prod_def enum_ex)
haftmann@26348
   345
haftmann@26348
   346
end
haftmann@26348
   347
haftmann@37678
   348
instantiation sum :: (enum, enum) enum
haftmann@26348
   349
begin
haftmann@26348
   350
haftmann@26348
   351
definition
haftmann@26348
   352
  "enum = map Inl enum @ map Inr enum"
haftmann@26348
   353
bulwahn@41078
   354
definition
bulwahn@41078
   355
  "enum_all P = (enum_all (%x. P (Inl x)) \<and> enum_all (%x. P (Inr x)))"
bulwahn@41078
   356
bulwahn@41078
   357
definition
bulwahn@41078
   358
  "enum_ex P = (enum_ex (%x. P (Inl x)) \<or> enum_ex (%x. P (Inr x)))"
bulwahn@41078
   359
bulwahn@41078
   360
instance proof
bulwahn@41078
   361
  fix P
bulwahn@41078
   362
  show "enum_all (P :: ('a + 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   363
    unfolding enum_all_sum_def enum_all
bulwahn@41078
   364
    by (auto, case_tac x) auto
bulwahn@41078
   365
next
bulwahn@41078
   366
  fix P
bulwahn@41078
   367
  show "enum_ex (P :: ('a + 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   368
    unfolding enum_ex_sum_def enum_ex
bulwahn@41078
   369
    by (auto, case_tac x) auto
bulwahn@41078
   370
qed (auto simp add: enum_UNIV enum_sum_def, case_tac x, auto intro: inj_onI simp add: distinct_map enum_distinct)
haftmann@26348
   371
haftmann@26348
   372
end
haftmann@26348
   373
haftmann@26348
   374
instantiation nibble :: enum
haftmann@26348
   375
begin
haftmann@26348
   376
haftmann@26348
   377
definition
haftmann@26348
   378
  "enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@26348
   379
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
haftmann@26348
   380
bulwahn@41078
   381
definition
bulwahn@41078
   382
  "enum_all P = (P Nibble0 \<and> P Nibble1 \<and> P Nibble2 \<and> P Nibble3 \<and> P Nibble4 \<and> P Nibble5 \<and> P Nibble6 \<and> P Nibble7
bulwahn@41078
   383
     \<and> P Nibble8 \<and> P Nibble9 \<and> P NibbleA \<and> P NibbleB \<and> P NibbleC \<and> P NibbleD \<and> P NibbleE \<and> P NibbleF)"
bulwahn@41078
   384
bulwahn@41078
   385
definition
bulwahn@41078
   386
  "enum_ex P = (P Nibble0 \<or> P Nibble1 \<or> P Nibble2 \<or> P Nibble3 \<or> P Nibble4 \<or> P Nibble5 \<or> P Nibble6 \<or> P Nibble7
bulwahn@41078
   387
     \<or> P Nibble8 \<or> P Nibble9 \<or> P NibbleA \<or> P NibbleB \<or> P NibbleC \<or> P NibbleD \<or> P NibbleE \<or> P NibbleF)"
bulwahn@41078
   388
haftmann@31464
   389
instance proof
bulwahn@41078
   390
  fix P
bulwahn@41078
   391
  show "enum_all (P :: nibble \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   392
    unfolding enum_all_nibble_def
bulwahn@41078
   393
    by (auto, case_tac x) auto
bulwahn@41078
   394
next
bulwahn@41078
   395
  fix P
bulwahn@41078
   396
  show "enum_ex (P :: nibble \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   397
    unfolding enum_ex_nibble_def
bulwahn@41078
   398
    by (auto, case_tac x) auto
haftmann@31464
   399
qed (simp_all add: enum_nibble_def UNIV_nibble)
haftmann@26348
   400
haftmann@26348
   401
end
haftmann@26348
   402
haftmann@26348
   403
instantiation char :: enum
haftmann@26348
   404
begin
haftmann@26348
   405
haftmann@26348
   406
definition
haftmann@37765
   407
  "enum = map (split Char) (product enum enum)"
haftmann@26444
   408
haftmann@31482
   409
lemma enum_chars [code]:
haftmann@31482
   410
  "enum = chars"
haftmann@31482
   411
  unfolding enum_char_def chars_def enum_nibble_def by simp
haftmann@26348
   412
bulwahn@41078
   413
definition
bulwahn@41078
   414
  "enum_all P = list_all P chars"
bulwahn@41078
   415
bulwahn@41078
   416
definition
bulwahn@41078
   417
  "enum_ex P = list_ex P chars"
bulwahn@41078
   418
bulwahn@41078
   419
lemma set_enum_char: "set (enum :: char list) = UNIV"
bulwahn@41078
   420
    by (auto intro: char.exhaust simp add: enum_char_def product_list_set enum_UNIV full_SetCompr_eq [symmetric])
bulwahn@41078
   421
haftmann@31464
   422
instance proof
bulwahn@41078
   423
  fix P
bulwahn@41078
   424
  show "enum_all (P :: char \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   425
    unfolding enum_all_char_def enum_chars[symmetric]
bulwahn@41078
   426
    by (auto simp add: list_all_iff set_enum_char)
bulwahn@41078
   427
next
bulwahn@41078
   428
  fix P
bulwahn@41078
   429
  show "enum_ex (P :: char \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   430
    unfolding enum_ex_char_def enum_chars[symmetric]
bulwahn@41078
   431
    by (auto simp add: list_ex_iff set_enum_char)
bulwahn@41078
   432
next
bulwahn@41078
   433
  show "distinct (enum :: char list)"
bulwahn@41078
   434
    by (auto intro: inj_onI simp add: enum_char_def product_list_set distinct_map distinct_product enum_distinct)
bulwahn@41078
   435
qed (auto simp add: set_enum_char)
haftmann@26348
   436
haftmann@26348
   437
end
haftmann@26348
   438
huffman@29024
   439
instantiation option :: (enum) enum
huffman@29024
   440
begin
huffman@29024
   441
huffman@29024
   442
definition
huffman@29024
   443
  "enum = None # map Some enum"
huffman@29024
   444
bulwahn@41078
   445
definition
bulwahn@41078
   446
  "enum_all P = (P None \<and> enum_all (%x. P (Some x)))"
bulwahn@41078
   447
bulwahn@41078
   448
definition
bulwahn@41078
   449
  "enum_ex P = (P None \<or> enum_ex (%x. P (Some x)))"
bulwahn@41078
   450
haftmann@31464
   451
instance proof
bulwahn@41078
   452
  fix P
bulwahn@41078
   453
  show "enum_all (P :: 'a option \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   454
    unfolding enum_all_option_def enum_all
bulwahn@41078
   455
    by (auto, case_tac x) auto
bulwahn@41078
   456
next
bulwahn@41078
   457
  fix P
bulwahn@41078
   458
  show "enum_ex (P :: 'a option \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   459
    unfolding enum_ex_option_def enum_ex
bulwahn@41078
   460
    by (auto, case_tac x) auto
bulwahn@41078
   461
qed (auto simp add: enum_UNIV enum_option_def, rule option.exhaust, auto intro: simp add: distinct_map enum_distinct)
haftmann@45963
   462
end
haftmann@45963
   463
haftmann@45963
   464
primrec sublists :: "'a list \<Rightarrow> 'a list list" where
haftmann@45963
   465
  "sublists [] = [[]]"
haftmann@45963
   466
  | "sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"
haftmann@45963
   467
haftmann@45963
   468
lemma length_sublists:
huffman@47221
   469
  "length (sublists xs) = 2 ^ length xs"
haftmann@45963
   470
  by (induct xs) (simp_all add: Let_def)
haftmann@45963
   471
haftmann@45963
   472
lemma sublists_powset:
haftmann@45963
   473
  "set ` set (sublists xs) = Pow (set xs)"
haftmann@45963
   474
proof -
haftmann@45963
   475
  have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A"
haftmann@45963
   476
    by (auto simp add: image_def)
haftmann@45963
   477
  have "set (map set (sublists xs)) = Pow (set xs)"
haftmann@45963
   478
    by (induct xs)
haftmann@45963
   479
      (simp_all add: aux Let_def Pow_insert Un_commute comp_def del: map_map)
haftmann@45963
   480
  then show ?thesis by simp
haftmann@45963
   481
qed
haftmann@45963
   482
haftmann@45963
   483
lemma distinct_set_sublists:
haftmann@45963
   484
  assumes "distinct xs"
haftmann@45963
   485
  shows "distinct (map set (sublists xs))"
haftmann@45963
   486
proof (rule card_distinct)
haftmann@45963
   487
  have "finite (set xs)" by rule
huffman@47221
   488
  then have "card (Pow (set xs)) = 2 ^ card (set xs)" by (rule card_Pow)
haftmann@45963
   489
  with assms distinct_card [of xs]
huffman@47221
   490
    have "card (Pow (set xs)) = 2 ^ length xs" by simp
haftmann@45963
   491
  then show "card (set (map set (sublists xs))) = length (map set (sublists xs))"
haftmann@45963
   492
    by (simp add: sublists_powset length_sublists)
haftmann@45963
   493
qed
haftmann@45963
   494
haftmann@45963
   495
instantiation set :: (enum) enum
haftmann@45963
   496
begin
haftmann@45963
   497
haftmann@45963
   498
definition
haftmann@45963
   499
  "enum = map set (sublists enum)"
haftmann@45963
   500
haftmann@45963
   501
definition
haftmann@45963
   502
  "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
haftmann@45963
   503
haftmann@45963
   504
definition
haftmann@45963
   505
  "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
haftmann@45963
   506
haftmann@45963
   507
instance proof
haftmann@45963
   508
qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
haftmann@45963
   509
  enum_distinct enum_UNIV)
huffman@29024
   510
huffman@29024
   511
end
huffman@29024
   512
haftmann@45963
   513
bulwahn@40647
   514
subsection {* Small finite types *}
bulwahn@40647
   515
bulwahn@40647
   516
text {* We define small finite types for the use in Quickcheck *}
bulwahn@40647
   517
bulwahn@40647
   518
datatype finite_1 = a\<^isub>1
bulwahn@40647
   519
bulwahn@40900
   520
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   521
bulwahn@40647
   522
instantiation finite_1 :: enum
bulwahn@40647
   523
begin
bulwahn@40647
   524
bulwahn@40647
   525
definition
bulwahn@40647
   526
  "enum = [a\<^isub>1]"
bulwahn@40647
   527
bulwahn@41078
   528
definition
bulwahn@41078
   529
  "enum_all P = P a\<^isub>1"
bulwahn@41078
   530
bulwahn@41078
   531
definition
bulwahn@41078
   532
  "enum_ex P = P a\<^isub>1"
bulwahn@41078
   533
bulwahn@40647
   534
instance proof
bulwahn@41078
   535
  fix P
bulwahn@41078
   536
  show "enum_all (P :: finite_1 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   537
    unfolding enum_all_finite_1_def
bulwahn@41078
   538
    by (auto, case_tac x) auto
bulwahn@41078
   539
next
bulwahn@41078
   540
  fix P
bulwahn@41078
   541
  show "enum_ex (P :: finite_1 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   542
    unfolding enum_ex_finite_1_def
bulwahn@41078
   543
    by (auto, case_tac x) auto
bulwahn@40647
   544
qed (auto simp add: enum_finite_1_def intro: finite_1.exhaust)
bulwahn@40647
   545
huffman@29024
   546
end
bulwahn@40647
   547
bulwahn@40651
   548
instantiation finite_1 :: linorder
bulwahn@40651
   549
begin
bulwahn@40651
   550
bulwahn@40651
   551
definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   552
where
bulwahn@40651
   553
  "less_eq_finite_1 x y = True"
bulwahn@40651
   554
bulwahn@40651
   555
definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   556
where
bulwahn@40651
   557
  "less_finite_1 x y = False"
bulwahn@40651
   558
bulwahn@40651
   559
instance
bulwahn@40651
   560
apply (intro_classes)
bulwahn@40651
   561
apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
bulwahn@40651
   562
apply (metis finite_1.exhaust)
bulwahn@40651
   563
done
bulwahn@40651
   564
bulwahn@40651
   565
end
bulwahn@40651
   566
bulwahn@41085
   567
hide_const (open) a\<^isub>1
bulwahn@40657
   568
bulwahn@40647
   569
datatype finite_2 = a\<^isub>1 | a\<^isub>2
bulwahn@40647
   570
bulwahn@40900
   571
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   572
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   573
bulwahn@40647
   574
instantiation finite_2 :: enum
bulwahn@40647
   575
begin
bulwahn@40647
   576
bulwahn@40647
   577
definition
bulwahn@40647
   578
  "enum = [a\<^isub>1, a\<^isub>2]"
bulwahn@40647
   579
bulwahn@41078
   580
definition
bulwahn@41078
   581
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2)"
bulwahn@41078
   582
bulwahn@41078
   583
definition
bulwahn@41078
   584
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2)"
bulwahn@41078
   585
bulwahn@40647
   586
instance proof
bulwahn@41078
   587
  fix P
bulwahn@41078
   588
  show "enum_all (P :: finite_2 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   589
    unfolding enum_all_finite_2_def
bulwahn@41078
   590
    by (auto, case_tac x) auto
bulwahn@41078
   591
next
bulwahn@41078
   592
  fix P
bulwahn@41078
   593
  show "enum_ex (P :: finite_2 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   594
    unfolding enum_ex_finite_2_def
bulwahn@41078
   595
    by (auto, case_tac x) auto
bulwahn@40647
   596
qed (auto simp add: enum_finite_2_def intro: finite_2.exhaust)
bulwahn@40647
   597
bulwahn@40647
   598
end
bulwahn@40647
   599
bulwahn@40651
   600
instantiation finite_2 :: linorder
bulwahn@40651
   601
begin
bulwahn@40651
   602
bulwahn@40651
   603
definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   604
where
bulwahn@40651
   605
  "less_finite_2 x y = ((x = a\<^isub>1) & (y = a\<^isub>2))"
bulwahn@40651
   606
bulwahn@40651
   607
definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   608
where
bulwahn@40651
   609
  "less_eq_finite_2 x y = ((x = y) \<or> (x < y))"
bulwahn@40651
   610
bulwahn@40651
   611
bulwahn@40651
   612
instance
bulwahn@40651
   613
apply (intro_classes)
bulwahn@40651
   614
apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
bulwahn@40651
   615
apply (metis finite_2.distinct finite_2.nchotomy)+
bulwahn@40651
   616
done
bulwahn@40651
   617
bulwahn@40651
   618
end
bulwahn@40651
   619
bulwahn@41085
   620
hide_const (open) a\<^isub>1 a\<^isub>2
bulwahn@40657
   621
bulwahn@40651
   622
bulwahn@40647
   623
datatype finite_3 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3
bulwahn@40647
   624
bulwahn@40900
   625
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   626
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   627
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   628
bulwahn@40647
   629
instantiation finite_3 :: enum
bulwahn@40647
   630
begin
bulwahn@40647
   631
bulwahn@40647
   632
definition
bulwahn@40647
   633
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3]"
bulwahn@40647
   634
bulwahn@41078
   635
definition
bulwahn@41078
   636
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3)"
bulwahn@41078
   637
bulwahn@41078
   638
definition
bulwahn@41078
   639
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3)"
bulwahn@41078
   640
bulwahn@40647
   641
instance proof
bulwahn@41078
   642
  fix P
bulwahn@41078
   643
  show "enum_all (P :: finite_3 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   644
    unfolding enum_all_finite_3_def
bulwahn@41078
   645
    by (auto, case_tac x) auto
bulwahn@41078
   646
next
bulwahn@41078
   647
  fix P
bulwahn@41078
   648
  show "enum_ex (P :: finite_3 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   649
    unfolding enum_ex_finite_3_def
bulwahn@41078
   650
    by (auto, case_tac x) auto
bulwahn@40647
   651
qed (auto simp add: enum_finite_3_def intro: finite_3.exhaust)
bulwahn@40647
   652
bulwahn@40647
   653
end
bulwahn@40647
   654
bulwahn@40651
   655
instantiation finite_3 :: linorder
bulwahn@40651
   656
begin
bulwahn@40651
   657
bulwahn@40651
   658
definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   659
where
bulwahn@40651
   660
  "less_finite_3 x y = (case x of a\<^isub>1 => (y \<noteq> a\<^isub>1)
bulwahn@40651
   661
     | a\<^isub>2 => (y = a\<^isub>3)| a\<^isub>3 => False)"
bulwahn@40651
   662
bulwahn@40651
   663
definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   664
where
bulwahn@40651
   665
  "less_eq_finite_3 x y = ((x = y) \<or> (x < y))"
bulwahn@40651
   666
bulwahn@40651
   667
bulwahn@40651
   668
instance proof (intro_classes)
bulwahn@40651
   669
qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
bulwahn@40651
   670
bulwahn@40651
   671
end
bulwahn@40651
   672
bulwahn@41085
   673
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3
bulwahn@40657
   674
bulwahn@40651
   675
bulwahn@40647
   676
datatype finite_4 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4
bulwahn@40647
   677
bulwahn@40900
   678
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   679
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   680
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   681
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   682
bulwahn@40647
   683
instantiation finite_4 :: enum
bulwahn@40647
   684
begin
bulwahn@40647
   685
bulwahn@40647
   686
definition
bulwahn@40647
   687
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4]"
bulwahn@40647
   688
bulwahn@41078
   689
definition
bulwahn@41078
   690
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4)"
bulwahn@41078
   691
bulwahn@41078
   692
definition
bulwahn@41078
   693
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4)"
bulwahn@41078
   694
bulwahn@40647
   695
instance proof
bulwahn@41078
   696
  fix P
bulwahn@41078
   697
  show "enum_all (P :: finite_4 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   698
    unfolding enum_all_finite_4_def
bulwahn@41078
   699
    by (auto, case_tac x) auto
bulwahn@41078
   700
next
bulwahn@41078
   701
  fix P
bulwahn@41078
   702
  show "enum_ex (P :: finite_4 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   703
    unfolding enum_ex_finite_4_def
bulwahn@41078
   704
    by (auto, case_tac x) auto
bulwahn@40647
   705
qed (auto simp add: enum_finite_4_def intro: finite_4.exhaust)
bulwahn@40647
   706
bulwahn@40647
   707
end
bulwahn@40647
   708
bulwahn@41085
   709
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4
bulwahn@40651
   710
bulwahn@40651
   711
bulwahn@40647
   712
datatype finite_5 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4 | a\<^isub>5
bulwahn@40647
   713
bulwahn@40900
   714
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   715
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   716
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   717
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   718
notation (output) a\<^isub>5  ("a\<^isub>5")
bulwahn@40900
   719
bulwahn@40647
   720
instantiation finite_5 :: enum
bulwahn@40647
   721
begin
bulwahn@40647
   722
bulwahn@40647
   723
definition
bulwahn@40647
   724
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5]"
bulwahn@40647
   725
bulwahn@41078
   726
definition
bulwahn@41078
   727
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4 \<and> P a\<^isub>5)"
bulwahn@41078
   728
bulwahn@41078
   729
definition
bulwahn@41078
   730
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4 \<or> P a\<^isub>5)"
bulwahn@41078
   731
bulwahn@40647
   732
instance proof
bulwahn@41078
   733
  fix P
bulwahn@41078
   734
  show "enum_all (P :: finite_5 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   735
    unfolding enum_all_finite_5_def
bulwahn@41078
   736
    by (auto, case_tac x) auto
bulwahn@41078
   737
next
bulwahn@41078
   738
  fix P
bulwahn@41078
   739
  show "enum_ex (P :: finite_5 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   740
    unfolding enum_ex_finite_5_def
bulwahn@41078
   741
    by (auto, case_tac x) auto
bulwahn@40647
   742
qed (auto simp add: enum_finite_5_def intro: finite_5.exhaust)
bulwahn@40647
   743
bulwahn@40647
   744
end
bulwahn@40647
   745
bulwahn@46352
   746
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4 a\<^isub>5
bulwahn@46352
   747
bulwahn@41115
   748
subsection {* An executable THE operator on finite types *}
bulwahn@41115
   749
bulwahn@41115
   750
definition
bulwahn@41115
   751
  [code del]: "enum_the P = The P"
bulwahn@41115
   752
bulwahn@41115
   753
lemma [code]:
bulwahn@41115
   754
  "The P = (case filter P enum of [x] => x | _ => enum_the P)"
bulwahn@41115
   755
proof -
bulwahn@41115
   756
  {
bulwahn@41115
   757
    fix a
bulwahn@41115
   758
    assume filter_enum: "filter P enum = [a]"
bulwahn@41115
   759
    have "The P = a"
bulwahn@41115
   760
    proof (rule the_equality)
bulwahn@41115
   761
      fix x
bulwahn@41115
   762
      assume "P x"
bulwahn@41115
   763
      show "x = a"
bulwahn@41115
   764
      proof (rule ccontr)
bulwahn@41115
   765
        assume "x \<noteq> a"
bulwahn@41115
   766
        from filter_enum obtain us vs
bulwahn@41115
   767
          where enum_eq: "enum = us @ [a] @ vs"
bulwahn@41115
   768
          and "\<forall> x \<in> set us. \<not> P x"
bulwahn@41115
   769
          and "\<forall> x \<in> set vs. \<not> P x"
bulwahn@41115
   770
          and "P a"
bulwahn@41115
   771
          by (auto simp add: filter_eq_Cons_iff) (simp only: filter_empty_conv[symmetric])
bulwahn@41115
   772
        with `P x` in_enum[of x, unfolded enum_eq] `x \<noteq> a` show "False" by auto
bulwahn@41115
   773
      qed
bulwahn@41115
   774
    next
bulwahn@41115
   775
      from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
bulwahn@41115
   776
    qed
bulwahn@41115
   777
  }
bulwahn@41115
   778
  from this show ?thesis
bulwahn@41115
   779
    unfolding enum_the_def by (auto split: list.split)
bulwahn@41115
   780
qed
bulwahn@41115
   781
bulwahn@46329
   782
code_abort enum_the
bulwahn@46336
   783
code_const enum_the (Eval "(fn p => raise Match)")
bulwahn@46329
   784
bulwahn@46329
   785
subsection {* Further operations on finite types *}
bulwahn@46329
   786
bulwahn@46329
   787
lemma [code]:
bulwahn@46329
   788
  "Collect P = set (filter P enum)"
bulwahn@46329
   789
by (auto simp add: enum_UNIV)
haftmann@45140
   790
bulwahn@46357
   791
lemma tranclp_unfold [code, no_atp]:
bulwahn@46357
   792
  "tranclp r a b \<equiv> (a, b) \<in> trancl {(x, y). r x y}"
bulwahn@46357
   793
by (simp add: trancl_def)
bulwahn@46352
   794
bulwahn@46359
   795
lemma rtranclp_rtrancl_eq[code, no_atp]:
bulwahn@46359
   796
  "rtranclp r x y = ((x, y) : rtrancl {(x, y). r x y})"
bulwahn@46359
   797
unfolding rtrancl_def by auto
bulwahn@46359
   798
bulwahn@46358
   799
lemma max_ext_eq[code]:
bulwahn@46358
   800
  "max_ext R = {(X, Y). finite X & finite Y & Y ~={} & (ALL x. x : X --> (EX xa : Y. (x, xa) : R))}"
bulwahn@46358
   801
by (auto simp add: max_ext.simps)
bulwahn@46358
   802
bulwahn@46361
   803
lemma max_extp_eq[code]:
bulwahn@46361
   804
  "max_extp r x y = ((x, y) : max_ext {(x, y). r x y})"
bulwahn@46361
   805
unfolding max_ext_def by auto
bulwahn@46361
   806
bulwahn@46361
   807
lemma mlex_eq[code]:
bulwahn@46361
   808
  "f <*mlex*> R = {(x, y). f x < f y \<or> (f x <= f y \<and> (x, y) : R)}"
bulwahn@46361
   809
unfolding mlex_prod_def by auto
bulwahn@46361
   810
bulwahn@46352
   811
subsection {* Executable accessible part *}
bulwahn@46352
   812
(* FIXME: should be moved somewhere else !? *)
bulwahn@46352
   813
bulwahn@46352
   814
subsubsection {* Finite monotone eventually stable sequences *}
bulwahn@46352
   815
bulwahn@46352
   816
lemma finite_mono_remains_stable_implies_strict_prefix:
bulwahn@46352
   817
  fixes f :: "nat \<Rightarrow> 'a::order"
bulwahn@46352
   818
  assumes S: "finite (range f)" "mono f" and eq: "\<forall>n. f n = f (Suc n) \<longrightarrow> f (Suc n) = f (Suc (Suc n))"
bulwahn@46352
   819
  shows "\<exists>N. (\<forall>n\<le>N. \<forall>m\<le>N. m < n \<longrightarrow> f m < f n) \<and> (\<forall>n\<ge>N. f N = f n)"
bulwahn@46352
   820
  using assms
bulwahn@46352
   821
proof -
bulwahn@46352
   822
  have "\<exists>n. f n = f (Suc n)"
bulwahn@46352
   823
  proof (rule ccontr)
bulwahn@46352
   824
    assume "\<not> ?thesis"
bulwahn@46352
   825
    then have "\<And>n. f n \<noteq> f (Suc n)" by auto
bulwahn@46352
   826
    then have "\<And>n. f n < f (Suc n)"
bulwahn@46352
   827
      using  `mono f` by (auto simp: le_less mono_iff_le_Suc)
bulwahn@46352
   828
    with lift_Suc_mono_less_iff[of f]
bulwahn@46352
   829
    have "\<And>n m. n < m \<Longrightarrow> f n < f m" by auto
bulwahn@46352
   830
    then have "inj f"
bulwahn@46352
   831
      by (auto simp: inj_on_def) (metis linorder_less_linear order_less_imp_not_eq)
bulwahn@46352
   832
    with `finite (range f)` have "finite (UNIV::nat set)"
bulwahn@46352
   833
      by (rule finite_imageD)
bulwahn@46352
   834
    then show False by simp
bulwahn@46352
   835
  qed
wenzelm@47230
   836
  then obtain n where n: "f n = f (Suc n)" ..
bulwahn@46352
   837
  def N \<equiv> "LEAST n. f n = f (Suc n)"
bulwahn@46352
   838
  have N: "f N = f (Suc N)"
bulwahn@46352
   839
    unfolding N_def using n by (rule LeastI)
bulwahn@46352
   840
  show ?thesis
bulwahn@46352
   841
  proof (intro exI[of _ N] conjI allI impI)
bulwahn@46352
   842
    fix n assume "N \<le> n"
bulwahn@46352
   843
    then have "\<And>m. N \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> f m = f N"
bulwahn@46352
   844
    proof (induct rule: dec_induct)
bulwahn@46352
   845
      case (step n) then show ?case
bulwahn@46352
   846
        using eq[rule_format, of "n - 1"] N
bulwahn@46352
   847
        by (cases n) (auto simp add: le_Suc_eq)
bulwahn@46352
   848
    qed simp
bulwahn@46352
   849
    from this[of n] `N \<le> n` show "f N = f n" by auto
bulwahn@46352
   850
  next
bulwahn@46352
   851
    fix n m :: nat assume "m < n" "n \<le> N"
bulwahn@46352
   852
    then show "f m < f n"
bulwahn@46352
   853
    proof (induct rule: less_Suc_induct[consumes 1])
bulwahn@46352
   854
      case (1 i)
bulwahn@46352
   855
      then have "i < N" by simp
bulwahn@46352
   856
      then have "f i \<noteq> f (Suc i)"
bulwahn@46352
   857
        unfolding N_def by (rule not_less_Least)
bulwahn@46352
   858
      with `mono f` show ?case by (simp add: mono_iff_le_Suc less_le)
bulwahn@46352
   859
    qed auto
bulwahn@46352
   860
  qed
bulwahn@46352
   861
qed
bulwahn@46352
   862
bulwahn@46352
   863
lemma finite_mono_strict_prefix_implies_finite_fixpoint:
bulwahn@46352
   864
  fixes f :: "nat \<Rightarrow> 'a set"
bulwahn@46352
   865
  assumes S: "\<And>i. f i \<subseteq> S" "finite S"
bulwahn@46352
   866
    and inj: "\<exists>N. (\<forall>n\<le>N. \<forall>m\<le>N. m < n \<longrightarrow> f m \<subset> f n) \<and> (\<forall>n\<ge>N. f N = f n)"
bulwahn@46352
   867
  shows "f (card S) = (\<Union>n. f n)"
bulwahn@46352
   868
proof -
bulwahn@46352
   869
  from inj obtain N where inj: "(\<forall>n\<le>N. \<forall>m\<le>N. m < n \<longrightarrow> f m \<subset> f n)" and eq: "(\<forall>n\<ge>N. f N = f n)" by auto
bulwahn@45117
   870
bulwahn@46352
   871
  { fix i have "i \<le> N \<Longrightarrow> i \<le> card (f i)"
bulwahn@46352
   872
    proof (induct i)
bulwahn@46352
   873
      case 0 then show ?case by simp
bulwahn@46352
   874
    next
bulwahn@46352
   875
      case (Suc i)
bulwahn@46352
   876
      with inj[rule_format, of "Suc i" i]
bulwahn@46352
   877
      have "(f i) \<subset> (f (Suc i))" by auto
bulwahn@46352
   878
      moreover have "finite (f (Suc i))" using S by (rule finite_subset)
bulwahn@46352
   879
      ultimately have "card (f i) < card (f (Suc i))" by (intro psubset_card_mono)
bulwahn@46352
   880
      with Suc show ?case using inj by auto
bulwahn@46352
   881
    qed
bulwahn@46352
   882
  }
bulwahn@46352
   883
  then have "N \<le> card (f N)" by simp
bulwahn@46352
   884
  also have "\<dots> \<le> card S" using S by (intro card_mono)
bulwahn@46352
   885
  finally have "f (card S) = f N" using eq by auto
bulwahn@46352
   886
  then show ?thesis using eq inj[rule_format, of N]
bulwahn@46352
   887
    apply auto
bulwahn@46352
   888
    apply (case_tac "n < N")
bulwahn@46352
   889
    apply (auto simp: not_less)
bulwahn@46352
   890
    done
bulwahn@46352
   891
qed
bulwahn@46352
   892
bulwahn@46352
   893
subsubsection {* Bounded accessible part *}
bulwahn@46352
   894
bulwahn@46352
   895
fun bacc :: "('a * 'a) set => nat => 'a set" 
bulwahn@46352
   896
where
bulwahn@46352
   897
  "bacc r 0 = {x. \<forall> y. (y, x) \<notin> r}"
bulwahn@46352
   898
| "bacc r (Suc n) = (bacc r n Un {x. \<forall> y. (y, x) : r --> y : bacc r n})"
bulwahn@46352
   899
bulwahn@46352
   900
lemma bacc_subseteq_acc:
bulwahn@46352
   901
  "bacc r n \<subseteq> acc r"
bulwahn@46352
   902
by (induct n) (auto intro: acc.intros)
bulwahn@40657
   903
bulwahn@46352
   904
lemma bacc_mono:
bulwahn@46352
   905
  "n <= m ==> bacc r n \<subseteq> bacc r m"
bulwahn@46352
   906
by (induct rule: dec_induct) auto
bulwahn@46352
   907
  
bulwahn@46352
   908
lemma bacc_upper_bound:
bulwahn@46352
   909
  "bacc (r :: ('a * 'a) set)  (card (UNIV :: ('a :: enum) set)) = (UN n. bacc r n)"
bulwahn@46352
   910
proof -
bulwahn@46352
   911
  have "mono (bacc r)" unfolding mono_def by (simp add: bacc_mono)
bulwahn@46352
   912
  moreover have "\<forall>n. bacc r n = bacc r (Suc n) \<longrightarrow> bacc r (Suc n) = bacc r (Suc (Suc n))" by auto
bulwahn@46352
   913
  moreover have "finite (range (bacc r))" by auto
bulwahn@46352
   914
  ultimately show ?thesis
bulwahn@46352
   915
   by (intro finite_mono_strict_prefix_implies_finite_fixpoint)
bulwahn@46352
   916
     (auto intro: finite_mono_remains_stable_implies_strict_prefix  simp add: enum_UNIV)
bulwahn@46352
   917
qed
bulwahn@46352
   918
bulwahn@46352
   919
lemma acc_subseteq_bacc:
bulwahn@46352
   920
  assumes "finite r"
bulwahn@46352
   921
  shows "acc r \<subseteq> (UN n. bacc r n)"
bulwahn@46352
   922
proof
bulwahn@46352
   923
  fix x
bulwahn@46352
   924
  assume "x : acc r"
wenzelm@47230
   925
  then have "\<exists> n. x : bacc r n"
wenzelm@47230
   926
  proof (induct x arbitrary: rule: acc.induct)
bulwahn@46352
   927
    case (accI x)
wenzelm@47230
   928
    then have "\<forall>y. \<exists> n. (y, x) \<in> r --> y : bacc r n" by simp
wenzelm@47230
   929
    from choice[OF this] obtain n where n: "\<forall>y. (y, x) \<in> r \<longrightarrow> y \<in> bacc r (n y)" ..
wenzelm@47230
   930
    obtain n where "\<And>y. (y, x) : r \<Longrightarrow> y : bacc r n"
wenzelm@47230
   931
    proof
bulwahn@46352
   932
      fix y assume y: "(y, x) : r"
bulwahn@46352
   933
      with n have "y : bacc r (n y)" by auto
bulwahn@46352
   934
      moreover have "n y <= Max ((%(y, x). n y) ` r)"
bulwahn@46352
   935
        using y `finite r` by (auto intro!: Max_ge)
bulwahn@46352
   936
      note bacc_mono[OF this, of r]
bulwahn@46352
   937
      ultimately show "y : bacc r (Max ((%(y, x). n y) ` r))" by auto
bulwahn@46352
   938
    qed
wenzelm@47230
   939
    then show ?case
bulwahn@46352
   940
      by (auto simp add: Let_def intro!: exI[of _ "Suc n"])
bulwahn@46352
   941
  qed
wenzelm@47230
   942
  then show "x : (UN n. bacc r n)" by auto
bulwahn@46352
   943
qed
bulwahn@46352
   944
bulwahn@46352
   945
lemma acc_bacc_eq: "acc ((set xs) :: (('a :: enum) * 'a) set) = bacc (set xs) (card (UNIV :: 'a set))"
bulwahn@46352
   946
by (metis acc_subseteq_bacc bacc_subseteq_acc bacc_upper_bound finite_set order_eq_iff)
bulwahn@46352
   947
bulwahn@46352
   948
definition 
bulwahn@46352
   949
  [code del]: "card_UNIV = card UNIV"
bulwahn@46352
   950
bulwahn@46352
   951
lemma [code]:
bulwahn@46352
   952
  "card_UNIV TYPE('a :: enum) = card (set (Enum.enum :: 'a list))"
bulwahn@46352
   953
unfolding card_UNIV_def enum_UNIV ..
bulwahn@46352
   954
bulwahn@46352
   955
declare acc_bacc_eq[folded card_UNIV_def, code]
bulwahn@46352
   956
bulwahn@46352
   957
lemma [code_unfold]: "accp r = (%x. x : acc {(x, y). r x y})"
bulwahn@46352
   958
unfolding acc_def by simp
bulwahn@46352
   959
bulwahn@46352
   960
subsection {* Closing up *}
bulwahn@40657
   961
bulwahn@41085
   962
hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
bulwahn@45117
   963
hide_const (open) enum enum_all enum_ex n_lists all_n_lists ex_n_lists product ntrancl
bulwahn@40647
   964
bulwahn@40647
   965
end