src/HOL/Enum.thy
author haftmann
Thu Oct 13 23:27:46 2011 +0200 (2011-10-13)
changeset 45140 339a8b3c4791
parent 45119 055c6ff9c5c3
child 45141 b2eb87bd541b
permissions -rw-r--r--
bouned transitive closure
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
haftmann@26444
   155
    moreover from map_of 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
   156
      by (auto dest: fun_cong)
haftmann@26444
   157
    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
   158
      by simp
haftmann@26444
   159
  qed
haftmann@26444
   160
  with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
haftmann@26444
   161
qed
haftmann@26444
   162
bulwahn@41078
   163
definition
bulwahn@41078
   164
  all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   165
where
bulwahn@41078
   166
  "all_n_lists P n = (\<forall>xs \<in> set (n_lists n enum). P xs)"
bulwahn@41078
   167
bulwahn@41078
   168
lemma [code]:
bulwahn@41078
   169
  "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
   170
unfolding all_n_lists_def enum_all
bulwahn@41078
   171
by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   172
bulwahn@41078
   173
definition
bulwahn@41078
   174
  ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   175
where
bulwahn@41078
   176
  "ex_n_lists P n = (\<exists>xs \<in> set (n_lists n enum). P xs)"
bulwahn@41078
   177
bulwahn@41078
   178
lemma [code]:
bulwahn@41078
   179
  "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
   180
unfolding ex_n_lists_def enum_ex
bulwahn@41078
   181
by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   182
bulwahn@41078
   183
haftmann@26444
   184
instantiation "fun" :: (enum, enum) enum
haftmann@26444
   185
begin
haftmann@26444
   186
haftmann@26444
   187
definition
haftmann@37765
   188
  "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
   189
bulwahn@41078
   190
definition
bulwahn@41078
   191
  "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   192
bulwahn@41078
   193
definition
bulwahn@41078
   194
  "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   195
bulwahn@41078
   196
haftmann@26444
   197
instance proof
haftmann@26444
   198
  show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   199
  proof (rule UNIV_eq_I)
haftmann@26444
   200
    fix f :: "'a \<Rightarrow> 'b"
haftmann@26444
   201
    have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@40683
   202
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
haftmann@26444
   203
    then show "f \<in> set enum"
bulwahn@40683
   204
      by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
haftmann@26444
   205
  qed
haftmann@26444
   206
next
haftmann@26444
   207
  from map_of_zip_enum_inject
haftmann@26444
   208
  show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
haftmann@26444
   209
    by (auto intro!: inj_onI simp add: enum_fun_def
haftmann@26444
   210
      distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
bulwahn@41078
   211
next
bulwahn@41078
   212
  fix P
bulwahn@41078
   213
  show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   214
  proof
bulwahn@41078
   215
    assume "enum_all P"
bulwahn@41078
   216
    show "\<forall>x. P x"
bulwahn@41078
   217
    proof
bulwahn@41078
   218
      fix f :: "'a \<Rightarrow> 'b"
bulwahn@41078
   219
      have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   220
        by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
bulwahn@41078
   221
      from `enum_all P` have "P (the \<circ> map_of (zip enum (map f enum)))"
bulwahn@41078
   222
        unfolding enum_all_fun_def all_n_lists_def
bulwahn@41078
   223
        apply (simp add: set_n_lists)
bulwahn@41078
   224
        apply (erule_tac x="map f enum" in allE)
bulwahn@41078
   225
        apply (auto intro!: in_enum)
bulwahn@41078
   226
        done
bulwahn@41078
   227
      from this f show "P f" by auto
bulwahn@41078
   228
    qed
bulwahn@41078
   229
  next
bulwahn@41078
   230
    assume "\<forall>x. P x"
bulwahn@41078
   231
    from this show "enum_all P"
bulwahn@41078
   232
      unfolding enum_all_fun_def all_n_lists_def by auto
bulwahn@41078
   233
  qed
bulwahn@41078
   234
next
bulwahn@41078
   235
  fix P
bulwahn@41078
   236
  show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   237
  proof
bulwahn@41078
   238
    assume "enum_ex P"
bulwahn@41078
   239
    from this show "\<exists>x. P x"
bulwahn@41078
   240
      unfolding enum_ex_fun_def ex_n_lists_def by auto
bulwahn@41078
   241
  next
bulwahn@41078
   242
    assume "\<exists>x. P x"
bulwahn@41078
   243
    from this obtain f where "P f" ..
bulwahn@41078
   244
    have f: "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
bulwahn@41078
   245
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum) 
bulwahn@41078
   246
    from `P f` this have "P (the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum)))"
bulwahn@41078
   247
      by auto
bulwahn@41078
   248
    from  this show "enum_ex P"
bulwahn@41078
   249
      unfolding enum_ex_fun_def ex_n_lists_def
bulwahn@41078
   250
      apply (auto simp add: set_n_lists)
bulwahn@41078
   251
      apply (rule_tac x="map f enum" in exI)
bulwahn@41078
   252
      apply (auto intro!: in_enum)
bulwahn@41078
   253
      done
bulwahn@41078
   254
  qed
haftmann@26444
   255
qed
haftmann@26444
   256
haftmann@26444
   257
end
haftmann@26444
   258
haftmann@38857
   259
lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, equal} list)
haftmann@28245
   260
  in map (\<lambda>ys. the o map_of (zip enum_a ys)) (n_lists (length enum_a) enum))"
haftmann@28245
   261
  by (simp add: enum_fun_def Let_def)
haftmann@26444
   262
bulwahn@41078
   263
lemma enum_all_fun_code [code]:
bulwahn@41078
   264
  "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   265
   in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
bulwahn@41078
   266
  by (simp add: enum_all_fun_def Let_def)
bulwahn@41078
   267
bulwahn@41078
   268
lemma enum_ex_fun_code [code]:
bulwahn@41078
   269
  "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   270
   in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
bulwahn@41078
   271
  by (simp add: enum_ex_fun_def Let_def)
bulwahn@41078
   272
haftmann@26348
   273
instantiation unit :: enum
haftmann@26348
   274
begin
haftmann@26348
   275
haftmann@26348
   276
definition
haftmann@26348
   277
  "enum = [()]"
haftmann@26348
   278
bulwahn@41078
   279
definition
bulwahn@41078
   280
  "enum_all P = P ()"
bulwahn@41078
   281
bulwahn@41078
   282
definition
bulwahn@41078
   283
  "enum_ex P = P ()"
bulwahn@41078
   284
haftmann@31464
   285
instance proof
bulwahn@41078
   286
qed (auto simp add: enum_unit_def UNIV_unit enum_all_unit_def enum_ex_unit_def intro: unit.exhaust)
haftmann@26348
   287
haftmann@26348
   288
end
haftmann@26348
   289
haftmann@26348
   290
instantiation bool :: enum
haftmann@26348
   291
begin
haftmann@26348
   292
haftmann@26348
   293
definition
haftmann@26348
   294
  "enum = [False, True]"
haftmann@26348
   295
bulwahn@41078
   296
definition
bulwahn@41078
   297
  "enum_all P = (P False \<and> P True)"
bulwahn@41078
   298
bulwahn@41078
   299
definition
bulwahn@41078
   300
  "enum_ex P = (P False \<or> P True)"
bulwahn@41078
   301
haftmann@31464
   302
instance proof
bulwahn@41078
   303
  fix P
bulwahn@41078
   304
  show "enum_all (P :: bool \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   305
    unfolding enum_all_bool_def by (auto, case_tac x) auto
bulwahn@41078
   306
next
bulwahn@41078
   307
  fix P
bulwahn@41078
   308
  show "enum_ex (P :: bool \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   309
    unfolding enum_ex_bool_def by (auto, case_tac x) auto
bulwahn@41078
   310
qed (auto simp add: enum_bool_def UNIV_bool)
haftmann@26348
   311
haftmann@26348
   312
end
haftmann@26348
   313
haftmann@26348
   314
primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
haftmann@26348
   315
  "product [] _ = []"
haftmann@26348
   316
  | "product (x#xs) ys = map (Pair x) ys @ product xs ys"
haftmann@26348
   317
haftmann@26348
   318
lemma product_list_set:
haftmann@26348
   319
  "set (product xs ys) = set xs \<times> set ys"
haftmann@26348
   320
  by (induct xs) auto
haftmann@26348
   321
haftmann@26444
   322
lemma distinct_product:
haftmann@26444
   323
  assumes "distinct xs" and "distinct ys"
haftmann@26444
   324
  shows "distinct (product xs ys)"
haftmann@26444
   325
  using assms by (induct xs)
haftmann@26444
   326
    (auto intro: inj_onI simp add: product_list_set distinct_map)
haftmann@26444
   327
haftmann@37678
   328
instantiation prod :: (enum, enum) enum
haftmann@26348
   329
begin
haftmann@26348
   330
haftmann@26348
   331
definition
haftmann@26348
   332
  "enum = product enum enum"
haftmann@26348
   333
bulwahn@41078
   334
definition
bulwahn@41078
   335
  "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
bulwahn@41078
   336
bulwahn@41078
   337
definition
bulwahn@41078
   338
  "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
bulwahn@41078
   339
bulwahn@41078
   340
 
haftmann@26348
   341
instance by default
bulwahn@41078
   342
  (simp_all add: enum_prod_def product_list_set distinct_product
bulwahn@41078
   343
    enum_UNIV enum_distinct enum_all_prod_def enum_all enum_ex_prod_def enum_ex)
haftmann@26348
   344
haftmann@26348
   345
end
haftmann@26348
   346
haftmann@37678
   347
instantiation sum :: (enum, enum) enum
haftmann@26348
   348
begin
haftmann@26348
   349
haftmann@26348
   350
definition
haftmann@26348
   351
  "enum = map Inl enum @ map Inr enum"
haftmann@26348
   352
bulwahn@41078
   353
definition
bulwahn@41078
   354
  "enum_all P = (enum_all (%x. P (Inl x)) \<and> enum_all (%x. P (Inr x)))"
bulwahn@41078
   355
bulwahn@41078
   356
definition
bulwahn@41078
   357
  "enum_ex P = (enum_ex (%x. P (Inl x)) \<or> enum_ex (%x. P (Inr x)))"
bulwahn@41078
   358
bulwahn@41078
   359
instance proof
bulwahn@41078
   360
  fix P
bulwahn@41078
   361
  show "enum_all (P :: ('a + 'b) \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   362
    unfolding enum_all_sum_def enum_all
bulwahn@41078
   363
    by (auto, case_tac x) auto
bulwahn@41078
   364
next
bulwahn@41078
   365
  fix P
bulwahn@41078
   366
  show "enum_ex (P :: ('a + 'b) \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   367
    unfolding enum_ex_sum_def enum_ex
bulwahn@41078
   368
    by (auto, case_tac x) auto
bulwahn@41078
   369
qed (auto simp add: enum_UNIV enum_sum_def, case_tac x, auto intro: inj_onI simp add: distinct_map enum_distinct)
haftmann@26348
   370
haftmann@26348
   371
end
haftmann@26348
   372
haftmann@26348
   373
primrec sublists :: "'a list \<Rightarrow> 'a list list" where
haftmann@26348
   374
  "sublists [] = [[]]"
haftmann@26348
   375
  | "sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"
haftmann@26348
   376
haftmann@26444
   377
lemma length_sublists:
haftmann@26444
   378
  "length (sublists xs) = Suc (Suc (0\<Colon>nat)) ^ length xs"
haftmann@26444
   379
  by (induct xs) (simp_all add: Let_def)
haftmann@26444
   380
haftmann@26348
   381
lemma sublists_powset:
haftmann@26444
   382
  "set ` set (sublists xs) = Pow (set xs)"
haftmann@26348
   383
proof -
haftmann@26348
   384
  have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A"
haftmann@26348
   385
    by (auto simp add: image_def)
haftmann@26444
   386
  have "set (map set (sublists xs)) = Pow (set xs)"
haftmann@26348
   387
    by (induct xs)
hoelzl@33639
   388
      (simp_all add: aux Let_def Pow_insert Un_commute comp_def del: map_map)
haftmann@26444
   389
  then show ?thesis by simp
haftmann@26444
   390
qed
haftmann@26444
   391
haftmann@26444
   392
lemma distinct_set_sublists:
haftmann@26444
   393
  assumes "distinct xs"
haftmann@26444
   394
  shows "distinct (map set (sublists xs))"
haftmann@26444
   395
proof (rule card_distinct)
haftmann@26444
   396
  have "finite (set xs)" by rule
haftmann@26444
   397
  then have "card (Pow (set xs)) = Suc (Suc 0) ^ card (set xs)" by (rule card_Pow)
haftmann@26444
   398
  with assms distinct_card [of xs]
haftmann@26444
   399
    have "card (Pow (set xs)) = Suc (Suc 0) ^ length xs" by simp
haftmann@26444
   400
  then show "card (set (map set (sublists xs))) = length (map set (sublists xs))"
haftmann@26444
   401
    by (simp add: sublists_powset length_sublists)
haftmann@26348
   402
qed
haftmann@26348
   403
haftmann@26348
   404
instantiation nibble :: enum
haftmann@26348
   405
begin
haftmann@26348
   406
haftmann@26348
   407
definition
haftmann@26348
   408
  "enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@26348
   409
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
haftmann@26348
   410
bulwahn@41078
   411
definition
bulwahn@41078
   412
  "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
   413
     \<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
   414
bulwahn@41078
   415
definition
bulwahn@41078
   416
  "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
   417
     \<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
   418
haftmann@31464
   419
instance proof
bulwahn@41078
   420
  fix P
bulwahn@41078
   421
  show "enum_all (P :: nibble \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   422
    unfolding enum_all_nibble_def
bulwahn@41078
   423
    by (auto, case_tac x) auto
bulwahn@41078
   424
next
bulwahn@41078
   425
  fix P
bulwahn@41078
   426
  show "enum_ex (P :: nibble \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   427
    unfolding enum_ex_nibble_def
bulwahn@41078
   428
    by (auto, case_tac x) auto
haftmann@31464
   429
qed (simp_all add: enum_nibble_def UNIV_nibble)
haftmann@26348
   430
haftmann@26348
   431
end
haftmann@26348
   432
haftmann@26348
   433
instantiation char :: enum
haftmann@26348
   434
begin
haftmann@26348
   435
haftmann@26348
   436
definition
haftmann@37765
   437
  "enum = map (split Char) (product enum enum)"
haftmann@26444
   438
haftmann@31482
   439
lemma enum_chars [code]:
haftmann@31482
   440
  "enum = chars"
haftmann@31482
   441
  unfolding enum_char_def chars_def enum_nibble_def by simp
haftmann@26348
   442
bulwahn@41078
   443
definition
bulwahn@41078
   444
  "enum_all P = list_all P chars"
bulwahn@41078
   445
bulwahn@41078
   446
definition
bulwahn@41078
   447
  "enum_ex P = list_ex P chars"
bulwahn@41078
   448
bulwahn@41078
   449
lemma set_enum_char: "set (enum :: char list) = UNIV"
bulwahn@41078
   450
    by (auto intro: char.exhaust simp add: enum_char_def product_list_set enum_UNIV full_SetCompr_eq [symmetric])
bulwahn@41078
   451
haftmann@31464
   452
instance proof
bulwahn@41078
   453
  fix P
bulwahn@41078
   454
  show "enum_all (P :: char \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   455
    unfolding enum_all_char_def enum_chars[symmetric]
bulwahn@41078
   456
    by (auto simp add: list_all_iff set_enum_char)
bulwahn@41078
   457
next
bulwahn@41078
   458
  fix P
bulwahn@41078
   459
  show "enum_ex (P :: char \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   460
    unfolding enum_ex_char_def enum_chars[symmetric]
bulwahn@41078
   461
    by (auto simp add: list_ex_iff set_enum_char)
bulwahn@41078
   462
next
bulwahn@41078
   463
  show "distinct (enum :: char list)"
bulwahn@41078
   464
    by (auto intro: inj_onI simp add: enum_char_def product_list_set distinct_map distinct_product enum_distinct)
bulwahn@41078
   465
qed (auto simp add: set_enum_char)
haftmann@26348
   466
haftmann@26348
   467
end
haftmann@26348
   468
huffman@29024
   469
instantiation option :: (enum) enum
huffman@29024
   470
begin
huffman@29024
   471
huffman@29024
   472
definition
huffman@29024
   473
  "enum = None # map Some enum"
huffman@29024
   474
bulwahn@41078
   475
definition
bulwahn@41078
   476
  "enum_all P = (P None \<and> enum_all (%x. P (Some x)))"
bulwahn@41078
   477
bulwahn@41078
   478
definition
bulwahn@41078
   479
  "enum_ex P = (P None \<or> enum_ex (%x. P (Some x)))"
bulwahn@41078
   480
haftmann@31464
   481
instance proof
bulwahn@41078
   482
  fix P
bulwahn@41078
   483
  show "enum_all (P :: 'a option \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   484
    unfolding enum_all_option_def enum_all
bulwahn@41078
   485
    by (auto, case_tac x) auto
bulwahn@41078
   486
next
bulwahn@41078
   487
  fix P
bulwahn@41078
   488
  show "enum_ex (P :: 'a option \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   489
    unfolding enum_ex_option_def enum_ex
bulwahn@41078
   490
    by (auto, case_tac x) auto
bulwahn@41078
   491
qed (auto simp add: enum_UNIV enum_option_def, rule option.exhaust, auto intro: simp add: distinct_map enum_distinct)
huffman@29024
   492
huffman@29024
   493
end
huffman@29024
   494
bulwahn@40647
   495
subsection {* Small finite types *}
bulwahn@40647
   496
bulwahn@40647
   497
text {* We define small finite types for the use in Quickcheck *}
bulwahn@40647
   498
bulwahn@40647
   499
datatype finite_1 = a\<^isub>1
bulwahn@40647
   500
bulwahn@40900
   501
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   502
bulwahn@40647
   503
instantiation finite_1 :: enum
bulwahn@40647
   504
begin
bulwahn@40647
   505
bulwahn@40647
   506
definition
bulwahn@40647
   507
  "enum = [a\<^isub>1]"
bulwahn@40647
   508
bulwahn@41078
   509
definition
bulwahn@41078
   510
  "enum_all P = P a\<^isub>1"
bulwahn@41078
   511
bulwahn@41078
   512
definition
bulwahn@41078
   513
  "enum_ex P = P a\<^isub>1"
bulwahn@41078
   514
bulwahn@40647
   515
instance proof
bulwahn@41078
   516
  fix P
bulwahn@41078
   517
  show "enum_all (P :: finite_1 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   518
    unfolding enum_all_finite_1_def
bulwahn@41078
   519
    by (auto, case_tac x) auto
bulwahn@41078
   520
next
bulwahn@41078
   521
  fix P
bulwahn@41078
   522
  show "enum_ex (P :: finite_1 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   523
    unfolding enum_ex_finite_1_def
bulwahn@41078
   524
    by (auto, case_tac x) auto
bulwahn@40647
   525
qed (auto simp add: enum_finite_1_def intro: finite_1.exhaust)
bulwahn@40647
   526
huffman@29024
   527
end
bulwahn@40647
   528
bulwahn@40651
   529
instantiation finite_1 :: linorder
bulwahn@40651
   530
begin
bulwahn@40651
   531
bulwahn@40651
   532
definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   533
where
bulwahn@40651
   534
  "less_eq_finite_1 x y = True"
bulwahn@40651
   535
bulwahn@40651
   536
definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   537
where
bulwahn@40651
   538
  "less_finite_1 x y = False"
bulwahn@40651
   539
bulwahn@40651
   540
instance
bulwahn@40651
   541
apply (intro_classes)
bulwahn@40651
   542
apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
bulwahn@40651
   543
apply (metis finite_1.exhaust)
bulwahn@40651
   544
done
bulwahn@40651
   545
bulwahn@40651
   546
end
bulwahn@40651
   547
bulwahn@41085
   548
hide_const (open) a\<^isub>1
bulwahn@40657
   549
bulwahn@40647
   550
datatype finite_2 = a\<^isub>1 | a\<^isub>2
bulwahn@40647
   551
bulwahn@40900
   552
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   553
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   554
bulwahn@40647
   555
instantiation finite_2 :: enum
bulwahn@40647
   556
begin
bulwahn@40647
   557
bulwahn@40647
   558
definition
bulwahn@40647
   559
  "enum = [a\<^isub>1, a\<^isub>2]"
bulwahn@40647
   560
bulwahn@41078
   561
definition
bulwahn@41078
   562
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2)"
bulwahn@41078
   563
bulwahn@41078
   564
definition
bulwahn@41078
   565
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2)"
bulwahn@41078
   566
bulwahn@40647
   567
instance proof
bulwahn@41078
   568
  fix P
bulwahn@41078
   569
  show "enum_all (P :: finite_2 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   570
    unfolding enum_all_finite_2_def
bulwahn@41078
   571
    by (auto, case_tac x) auto
bulwahn@41078
   572
next
bulwahn@41078
   573
  fix P
bulwahn@41078
   574
  show "enum_ex (P :: finite_2 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   575
    unfolding enum_ex_finite_2_def
bulwahn@41078
   576
    by (auto, case_tac x) auto
bulwahn@40647
   577
qed (auto simp add: enum_finite_2_def intro: finite_2.exhaust)
bulwahn@40647
   578
bulwahn@40647
   579
end
bulwahn@40647
   580
bulwahn@40651
   581
instantiation finite_2 :: linorder
bulwahn@40651
   582
begin
bulwahn@40651
   583
bulwahn@40651
   584
definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   585
where
bulwahn@40651
   586
  "less_finite_2 x y = ((x = a\<^isub>1) & (y = a\<^isub>2))"
bulwahn@40651
   587
bulwahn@40651
   588
definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   589
where
bulwahn@40651
   590
  "less_eq_finite_2 x y = ((x = y) \<or> (x < y))"
bulwahn@40651
   591
bulwahn@40651
   592
bulwahn@40651
   593
instance
bulwahn@40651
   594
apply (intro_classes)
bulwahn@40651
   595
apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
bulwahn@40651
   596
apply (metis finite_2.distinct finite_2.nchotomy)+
bulwahn@40651
   597
done
bulwahn@40651
   598
bulwahn@40651
   599
end
bulwahn@40651
   600
bulwahn@41085
   601
hide_const (open) a\<^isub>1 a\<^isub>2
bulwahn@40657
   602
bulwahn@40651
   603
bulwahn@40647
   604
datatype finite_3 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3
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
bulwahn@40647
   610
instantiation finite_3 :: enum
bulwahn@40647
   611
begin
bulwahn@40647
   612
bulwahn@40647
   613
definition
bulwahn@40647
   614
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3]"
bulwahn@40647
   615
bulwahn@41078
   616
definition
bulwahn@41078
   617
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3)"
bulwahn@41078
   618
bulwahn@41078
   619
definition
bulwahn@41078
   620
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3)"
bulwahn@41078
   621
bulwahn@40647
   622
instance proof
bulwahn@41078
   623
  fix P
bulwahn@41078
   624
  show "enum_all (P :: finite_3 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   625
    unfolding enum_all_finite_3_def
bulwahn@41078
   626
    by (auto, case_tac x) auto
bulwahn@41078
   627
next
bulwahn@41078
   628
  fix P
bulwahn@41078
   629
  show "enum_ex (P :: finite_3 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   630
    unfolding enum_ex_finite_3_def
bulwahn@41078
   631
    by (auto, case_tac x) auto
bulwahn@40647
   632
qed (auto simp add: enum_finite_3_def intro: finite_3.exhaust)
bulwahn@40647
   633
bulwahn@40647
   634
end
bulwahn@40647
   635
bulwahn@40651
   636
instantiation finite_3 :: linorder
bulwahn@40651
   637
begin
bulwahn@40651
   638
bulwahn@40651
   639
definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   640
where
bulwahn@40651
   641
  "less_finite_3 x y = (case x of a\<^isub>1 => (y \<noteq> a\<^isub>1)
bulwahn@40651
   642
     | a\<^isub>2 => (y = a\<^isub>3)| a\<^isub>3 => False)"
bulwahn@40651
   643
bulwahn@40651
   644
definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   645
where
bulwahn@40651
   646
  "less_eq_finite_3 x y = ((x = y) \<or> (x < y))"
bulwahn@40651
   647
bulwahn@40651
   648
bulwahn@40651
   649
instance proof (intro_classes)
bulwahn@40651
   650
qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
bulwahn@40651
   651
bulwahn@40651
   652
end
bulwahn@40651
   653
bulwahn@41085
   654
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3
bulwahn@40657
   655
bulwahn@40651
   656
bulwahn@40647
   657
datatype finite_4 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4
bulwahn@40647
   658
bulwahn@40900
   659
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   660
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   661
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   662
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   663
bulwahn@40647
   664
instantiation finite_4 :: enum
bulwahn@40647
   665
begin
bulwahn@40647
   666
bulwahn@40647
   667
definition
bulwahn@40647
   668
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4]"
bulwahn@40647
   669
bulwahn@41078
   670
definition
bulwahn@41078
   671
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4)"
bulwahn@41078
   672
bulwahn@41078
   673
definition
bulwahn@41078
   674
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4)"
bulwahn@41078
   675
bulwahn@40647
   676
instance proof
bulwahn@41078
   677
  fix P
bulwahn@41078
   678
  show "enum_all (P :: finite_4 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   679
    unfolding enum_all_finite_4_def
bulwahn@41078
   680
    by (auto, case_tac x) auto
bulwahn@41078
   681
next
bulwahn@41078
   682
  fix P
bulwahn@41078
   683
  show "enum_ex (P :: finite_4 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   684
    unfolding enum_ex_finite_4_def
bulwahn@41078
   685
    by (auto, case_tac x) auto
bulwahn@40647
   686
qed (auto simp add: enum_finite_4_def intro: finite_4.exhaust)
bulwahn@40647
   687
bulwahn@40647
   688
end
bulwahn@40647
   689
bulwahn@41085
   690
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4
bulwahn@40651
   691
bulwahn@40651
   692
bulwahn@40647
   693
datatype finite_5 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4 | a\<^isub>5
bulwahn@40647
   694
bulwahn@40900
   695
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   696
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   697
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   698
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   699
notation (output) a\<^isub>5  ("a\<^isub>5")
bulwahn@40900
   700
bulwahn@40647
   701
instantiation finite_5 :: enum
bulwahn@40647
   702
begin
bulwahn@40647
   703
bulwahn@40647
   704
definition
bulwahn@40647
   705
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5]"
bulwahn@40647
   706
bulwahn@41078
   707
definition
bulwahn@41078
   708
  "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
   709
bulwahn@41078
   710
definition
bulwahn@41078
   711
  "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
   712
bulwahn@40647
   713
instance proof
bulwahn@41078
   714
  fix P
bulwahn@41078
   715
  show "enum_all (P :: finite_5 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   716
    unfolding enum_all_finite_5_def
bulwahn@41078
   717
    by (auto, case_tac x) auto
bulwahn@41078
   718
next
bulwahn@41078
   719
  fix P
bulwahn@41078
   720
  show "enum_ex (P :: finite_5 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   721
    unfolding enum_ex_finite_5_def
bulwahn@41078
   722
    by (auto, case_tac x) auto
bulwahn@40647
   723
qed (auto simp add: enum_finite_5_def intro: finite_5.exhaust)
bulwahn@40647
   724
bulwahn@40647
   725
end
bulwahn@40647
   726
bulwahn@41115
   727
subsection {* An executable THE operator on finite types *}
bulwahn@41115
   728
bulwahn@41115
   729
definition
bulwahn@41115
   730
  [code del]: "enum_the P = The P"
bulwahn@41115
   731
bulwahn@41115
   732
lemma [code]:
bulwahn@41115
   733
  "The P = (case filter P enum of [x] => x | _ => enum_the P)"
bulwahn@41115
   734
proof -
bulwahn@41115
   735
  {
bulwahn@41115
   736
    fix a
bulwahn@41115
   737
    assume filter_enum: "filter P enum = [a]"
bulwahn@41115
   738
    have "The P = a"
bulwahn@41115
   739
    proof (rule the_equality)
bulwahn@41115
   740
      fix x
bulwahn@41115
   741
      assume "P x"
bulwahn@41115
   742
      show "x = a"
bulwahn@41115
   743
      proof (rule ccontr)
bulwahn@41115
   744
        assume "x \<noteq> a"
bulwahn@41115
   745
        from filter_enum obtain us vs
bulwahn@41115
   746
          where enum_eq: "enum = us @ [a] @ vs"
bulwahn@41115
   747
          and "\<forall> x \<in> set us. \<not> P x"
bulwahn@41115
   748
          and "\<forall> x \<in> set vs. \<not> P x"
bulwahn@41115
   749
          and "P a"
bulwahn@41115
   750
          by (auto simp add: filter_eq_Cons_iff) (simp only: filter_empty_conv[symmetric])
bulwahn@41115
   751
        with `P x` in_enum[of x, unfolded enum_eq] `x \<noteq> a` show "False" by auto
bulwahn@41115
   752
      qed
bulwahn@41115
   753
    next
bulwahn@41115
   754
      from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
bulwahn@41115
   755
    qed
bulwahn@41115
   756
  }
bulwahn@41115
   757
  from this show ?thesis
bulwahn@41115
   758
    unfolding enum_the_def by (auto split: list.split)
bulwahn@41115
   759
qed
bulwahn@41115
   760
haftmann@45140
   761
bulwahn@45117
   762
subsection {* An executable card operator on finite types *}
bulwahn@45117
   763
bulwahn@45117
   764
lemma [code]:
haftmann@45140
   765
  "card R = length (filter R enum)"
haftmann@45140
   766
  by (simp add: distinct_length_filter [OF enum_distinct] enum_UNIV Collect_def)
bulwahn@45117
   767
bulwahn@45117
   768
bulwahn@45117
   769
subsection {* Closing up *}
bulwahn@45117
   770
bulwahn@41115
   771
code_abort enum_the
bulwahn@41115
   772
bulwahn@41085
   773
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4 a\<^isub>5
bulwahn@40657
   774
bulwahn@40657
   775
bulwahn@41085
   776
hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
bulwahn@45117
   777
hide_const (open) enum enum_all enum_ex n_lists all_n_lists ex_n_lists product ntrancl
bulwahn@40647
   778
bulwahn@40647
   779
end