src/HOL/Enum.thy
author bulwahn
Mon Jan 30 13:55:24 2012 +0100 (2012-01-30)
changeset 46361 87d5d36a9005
parent 46359 9bc43dc49d0a
child 47221 7205eb4a0a05
child 47230 6584098d5378
permissions -rw-r--r--
adding code equations for max_extp and mlex
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
instantiation nibble :: enum
haftmann@26348
   374
begin
haftmann@26348
   375
haftmann@26348
   376
definition
haftmann@26348
   377
  "enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
haftmann@26348
   378
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
haftmann@26348
   379
bulwahn@41078
   380
definition
bulwahn@41078
   381
  "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
   382
     \<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
   383
bulwahn@41078
   384
definition
bulwahn@41078
   385
  "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
   386
     \<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
   387
haftmann@31464
   388
instance proof
bulwahn@41078
   389
  fix P
bulwahn@41078
   390
  show "enum_all (P :: nibble \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   391
    unfolding enum_all_nibble_def
bulwahn@41078
   392
    by (auto, case_tac x) auto
bulwahn@41078
   393
next
bulwahn@41078
   394
  fix P
bulwahn@41078
   395
  show "enum_ex (P :: nibble \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   396
    unfolding enum_ex_nibble_def
bulwahn@41078
   397
    by (auto, case_tac x) auto
haftmann@31464
   398
qed (simp_all add: enum_nibble_def UNIV_nibble)
haftmann@26348
   399
haftmann@26348
   400
end
haftmann@26348
   401
haftmann@26348
   402
instantiation char :: enum
haftmann@26348
   403
begin
haftmann@26348
   404
haftmann@26348
   405
definition
haftmann@37765
   406
  "enum = map (split Char) (product enum enum)"
haftmann@26444
   407
haftmann@31482
   408
lemma enum_chars [code]:
haftmann@31482
   409
  "enum = chars"
haftmann@31482
   410
  unfolding enum_char_def chars_def enum_nibble_def by simp
haftmann@26348
   411
bulwahn@41078
   412
definition
bulwahn@41078
   413
  "enum_all P = list_all P chars"
bulwahn@41078
   414
bulwahn@41078
   415
definition
bulwahn@41078
   416
  "enum_ex P = list_ex P chars"
bulwahn@41078
   417
bulwahn@41078
   418
lemma set_enum_char: "set (enum :: char list) = UNIV"
bulwahn@41078
   419
    by (auto intro: char.exhaust simp add: enum_char_def product_list_set enum_UNIV full_SetCompr_eq [symmetric])
bulwahn@41078
   420
haftmann@31464
   421
instance proof
bulwahn@41078
   422
  fix P
bulwahn@41078
   423
  show "enum_all (P :: char \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   424
    unfolding enum_all_char_def enum_chars[symmetric]
bulwahn@41078
   425
    by (auto simp add: list_all_iff set_enum_char)
bulwahn@41078
   426
next
bulwahn@41078
   427
  fix P
bulwahn@41078
   428
  show "enum_ex (P :: char \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   429
    unfolding enum_ex_char_def enum_chars[symmetric]
bulwahn@41078
   430
    by (auto simp add: list_ex_iff set_enum_char)
bulwahn@41078
   431
next
bulwahn@41078
   432
  show "distinct (enum :: char list)"
bulwahn@41078
   433
    by (auto intro: inj_onI simp add: enum_char_def product_list_set distinct_map distinct_product enum_distinct)
bulwahn@41078
   434
qed (auto simp add: set_enum_char)
haftmann@26348
   435
haftmann@26348
   436
end
haftmann@26348
   437
huffman@29024
   438
instantiation option :: (enum) enum
huffman@29024
   439
begin
huffman@29024
   440
huffman@29024
   441
definition
huffman@29024
   442
  "enum = None # map Some enum"
huffman@29024
   443
bulwahn@41078
   444
definition
bulwahn@41078
   445
  "enum_all P = (P None \<and> enum_all (%x. P (Some x)))"
bulwahn@41078
   446
bulwahn@41078
   447
definition
bulwahn@41078
   448
  "enum_ex P = (P None \<or> enum_ex (%x. P (Some x)))"
bulwahn@41078
   449
haftmann@31464
   450
instance proof
bulwahn@41078
   451
  fix P
bulwahn@41078
   452
  show "enum_all (P :: 'a option \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   453
    unfolding enum_all_option_def enum_all
bulwahn@41078
   454
    by (auto, case_tac x) auto
bulwahn@41078
   455
next
bulwahn@41078
   456
  fix P
bulwahn@41078
   457
  show "enum_ex (P :: 'a option \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   458
    unfolding enum_ex_option_def enum_ex
bulwahn@41078
   459
    by (auto, case_tac x) auto
bulwahn@41078
   460
qed (auto simp add: enum_UNIV enum_option_def, rule option.exhaust, auto intro: simp add: distinct_map enum_distinct)
haftmann@45963
   461
end
haftmann@45963
   462
haftmann@45963
   463
primrec sublists :: "'a list \<Rightarrow> 'a list list" where
haftmann@45963
   464
  "sublists [] = [[]]"
haftmann@45963
   465
  | "sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"
haftmann@45963
   466
haftmann@45963
   467
lemma length_sublists:
haftmann@45963
   468
  "length (sublists xs) = Suc (Suc (0\<Colon>nat)) ^ length xs"
haftmann@45963
   469
  by (induct xs) (simp_all add: Let_def)
haftmann@45963
   470
haftmann@45963
   471
lemma sublists_powset:
haftmann@45963
   472
  "set ` set (sublists xs) = Pow (set xs)"
haftmann@45963
   473
proof -
haftmann@45963
   474
  have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A"
haftmann@45963
   475
    by (auto simp add: image_def)
haftmann@45963
   476
  have "set (map set (sublists xs)) = Pow (set xs)"
haftmann@45963
   477
    by (induct xs)
haftmann@45963
   478
      (simp_all add: aux Let_def Pow_insert Un_commute comp_def del: map_map)
haftmann@45963
   479
  then show ?thesis by simp
haftmann@45963
   480
qed
haftmann@45963
   481
haftmann@45963
   482
lemma distinct_set_sublists:
haftmann@45963
   483
  assumes "distinct xs"
haftmann@45963
   484
  shows "distinct (map set (sublists xs))"
haftmann@45963
   485
proof (rule card_distinct)
haftmann@45963
   486
  have "finite (set xs)" by rule
haftmann@45963
   487
  then have "card (Pow (set xs)) = Suc (Suc 0) ^ card (set xs)" by (rule card_Pow)
haftmann@45963
   488
  with assms distinct_card [of xs]
haftmann@45963
   489
    have "card (Pow (set xs)) = Suc (Suc 0) ^ length xs" by simp
haftmann@45963
   490
  then show "card (set (map set (sublists xs))) = length (map set (sublists xs))"
haftmann@45963
   491
    by (simp add: sublists_powset length_sublists)
haftmann@45963
   492
qed
haftmann@45963
   493
haftmann@45963
   494
instantiation set :: (enum) enum
haftmann@45963
   495
begin
haftmann@45963
   496
haftmann@45963
   497
definition
haftmann@45963
   498
  "enum = map set (sublists enum)"
haftmann@45963
   499
haftmann@45963
   500
definition
haftmann@45963
   501
  "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
haftmann@45963
   502
haftmann@45963
   503
definition
haftmann@45963
   504
  "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
haftmann@45963
   505
haftmann@45963
   506
instance proof
haftmann@45963
   507
qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
haftmann@45963
   508
  enum_distinct enum_UNIV)
huffman@29024
   509
huffman@29024
   510
end
huffman@29024
   511
haftmann@45963
   512
bulwahn@40647
   513
subsection {* Small finite types *}
bulwahn@40647
   514
bulwahn@40647
   515
text {* We define small finite types for the use in Quickcheck *}
bulwahn@40647
   516
bulwahn@40647
   517
datatype finite_1 = a\<^isub>1
bulwahn@40647
   518
bulwahn@40900
   519
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   520
bulwahn@40647
   521
instantiation finite_1 :: enum
bulwahn@40647
   522
begin
bulwahn@40647
   523
bulwahn@40647
   524
definition
bulwahn@40647
   525
  "enum = [a\<^isub>1]"
bulwahn@40647
   526
bulwahn@41078
   527
definition
bulwahn@41078
   528
  "enum_all P = P a\<^isub>1"
bulwahn@41078
   529
bulwahn@41078
   530
definition
bulwahn@41078
   531
  "enum_ex P = P a\<^isub>1"
bulwahn@41078
   532
bulwahn@40647
   533
instance proof
bulwahn@41078
   534
  fix P
bulwahn@41078
   535
  show "enum_all (P :: finite_1 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   536
    unfolding enum_all_finite_1_def
bulwahn@41078
   537
    by (auto, case_tac x) auto
bulwahn@41078
   538
next
bulwahn@41078
   539
  fix P
bulwahn@41078
   540
  show "enum_ex (P :: finite_1 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   541
    unfolding enum_ex_finite_1_def
bulwahn@41078
   542
    by (auto, case_tac x) auto
bulwahn@40647
   543
qed (auto simp add: enum_finite_1_def intro: finite_1.exhaust)
bulwahn@40647
   544
huffman@29024
   545
end
bulwahn@40647
   546
bulwahn@40651
   547
instantiation finite_1 :: linorder
bulwahn@40651
   548
begin
bulwahn@40651
   549
bulwahn@40651
   550
definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   551
where
bulwahn@40651
   552
  "less_eq_finite_1 x y = True"
bulwahn@40651
   553
bulwahn@40651
   554
definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   555
where
bulwahn@40651
   556
  "less_finite_1 x y = False"
bulwahn@40651
   557
bulwahn@40651
   558
instance
bulwahn@40651
   559
apply (intro_classes)
bulwahn@40651
   560
apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
bulwahn@40651
   561
apply (metis finite_1.exhaust)
bulwahn@40651
   562
done
bulwahn@40651
   563
bulwahn@40651
   564
end
bulwahn@40651
   565
bulwahn@41085
   566
hide_const (open) a\<^isub>1
bulwahn@40657
   567
bulwahn@40647
   568
datatype finite_2 = a\<^isub>1 | a\<^isub>2
bulwahn@40647
   569
bulwahn@40900
   570
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   571
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   572
bulwahn@40647
   573
instantiation finite_2 :: enum
bulwahn@40647
   574
begin
bulwahn@40647
   575
bulwahn@40647
   576
definition
bulwahn@40647
   577
  "enum = [a\<^isub>1, a\<^isub>2]"
bulwahn@40647
   578
bulwahn@41078
   579
definition
bulwahn@41078
   580
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2)"
bulwahn@41078
   581
bulwahn@41078
   582
definition
bulwahn@41078
   583
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2)"
bulwahn@41078
   584
bulwahn@40647
   585
instance proof
bulwahn@41078
   586
  fix P
bulwahn@41078
   587
  show "enum_all (P :: finite_2 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   588
    unfolding enum_all_finite_2_def
bulwahn@41078
   589
    by (auto, case_tac x) auto
bulwahn@41078
   590
next
bulwahn@41078
   591
  fix P
bulwahn@41078
   592
  show "enum_ex (P :: finite_2 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   593
    unfolding enum_ex_finite_2_def
bulwahn@41078
   594
    by (auto, case_tac x) auto
bulwahn@40647
   595
qed (auto simp add: enum_finite_2_def intro: finite_2.exhaust)
bulwahn@40647
   596
bulwahn@40647
   597
end
bulwahn@40647
   598
bulwahn@40651
   599
instantiation finite_2 :: linorder
bulwahn@40651
   600
begin
bulwahn@40651
   601
bulwahn@40651
   602
definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   603
where
bulwahn@40651
   604
  "less_finite_2 x y = ((x = a\<^isub>1) & (y = a\<^isub>2))"
bulwahn@40651
   605
bulwahn@40651
   606
definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   607
where
bulwahn@40651
   608
  "less_eq_finite_2 x y = ((x = y) \<or> (x < y))"
bulwahn@40651
   609
bulwahn@40651
   610
bulwahn@40651
   611
instance
bulwahn@40651
   612
apply (intro_classes)
bulwahn@40651
   613
apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
bulwahn@40651
   614
apply (metis finite_2.distinct finite_2.nchotomy)+
bulwahn@40651
   615
done
bulwahn@40651
   616
bulwahn@40651
   617
end
bulwahn@40651
   618
bulwahn@41085
   619
hide_const (open) a\<^isub>1 a\<^isub>2
bulwahn@40657
   620
bulwahn@40651
   621
bulwahn@40647
   622
datatype finite_3 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3
bulwahn@40647
   623
bulwahn@40900
   624
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   625
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   626
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   627
bulwahn@40647
   628
instantiation finite_3 :: enum
bulwahn@40647
   629
begin
bulwahn@40647
   630
bulwahn@40647
   631
definition
bulwahn@40647
   632
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3]"
bulwahn@40647
   633
bulwahn@41078
   634
definition
bulwahn@41078
   635
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3)"
bulwahn@41078
   636
bulwahn@41078
   637
definition
bulwahn@41078
   638
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3)"
bulwahn@41078
   639
bulwahn@40647
   640
instance proof
bulwahn@41078
   641
  fix P
bulwahn@41078
   642
  show "enum_all (P :: finite_3 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   643
    unfolding enum_all_finite_3_def
bulwahn@41078
   644
    by (auto, case_tac x) auto
bulwahn@41078
   645
next
bulwahn@41078
   646
  fix P
bulwahn@41078
   647
  show "enum_ex (P :: finite_3 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   648
    unfolding enum_ex_finite_3_def
bulwahn@41078
   649
    by (auto, case_tac x) auto
bulwahn@40647
   650
qed (auto simp add: enum_finite_3_def intro: finite_3.exhaust)
bulwahn@40647
   651
bulwahn@40647
   652
end
bulwahn@40647
   653
bulwahn@40651
   654
instantiation finite_3 :: linorder
bulwahn@40651
   655
begin
bulwahn@40651
   656
bulwahn@40651
   657
definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   658
where
bulwahn@40651
   659
  "less_finite_3 x y = (case x of a\<^isub>1 => (y \<noteq> a\<^isub>1)
bulwahn@40651
   660
     | a\<^isub>2 => (y = a\<^isub>3)| a\<^isub>3 => False)"
bulwahn@40651
   661
bulwahn@40651
   662
definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   663
where
bulwahn@40651
   664
  "less_eq_finite_3 x y = ((x = y) \<or> (x < y))"
bulwahn@40651
   665
bulwahn@40651
   666
bulwahn@40651
   667
instance proof (intro_classes)
bulwahn@40651
   668
qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
bulwahn@40651
   669
bulwahn@40651
   670
end
bulwahn@40651
   671
bulwahn@41085
   672
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3
bulwahn@40657
   673
bulwahn@40651
   674
bulwahn@40647
   675
datatype finite_4 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4
bulwahn@40647
   676
bulwahn@40900
   677
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   678
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   679
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   680
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   681
bulwahn@40647
   682
instantiation finite_4 :: enum
bulwahn@40647
   683
begin
bulwahn@40647
   684
bulwahn@40647
   685
definition
bulwahn@40647
   686
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4]"
bulwahn@40647
   687
bulwahn@41078
   688
definition
bulwahn@41078
   689
  "enum_all P = (P a\<^isub>1 \<and> P a\<^isub>2 \<and> P a\<^isub>3 \<and> P a\<^isub>4)"
bulwahn@41078
   690
bulwahn@41078
   691
definition
bulwahn@41078
   692
  "enum_ex P = (P a\<^isub>1 \<or> P a\<^isub>2 \<or> P a\<^isub>3 \<or> P a\<^isub>4)"
bulwahn@41078
   693
bulwahn@40647
   694
instance proof
bulwahn@41078
   695
  fix P
bulwahn@41078
   696
  show "enum_all (P :: finite_4 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   697
    unfolding enum_all_finite_4_def
bulwahn@41078
   698
    by (auto, case_tac x) auto
bulwahn@41078
   699
next
bulwahn@41078
   700
  fix P
bulwahn@41078
   701
  show "enum_ex (P :: finite_4 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   702
    unfolding enum_ex_finite_4_def
bulwahn@41078
   703
    by (auto, case_tac x) auto
bulwahn@40647
   704
qed (auto simp add: enum_finite_4_def intro: finite_4.exhaust)
bulwahn@40647
   705
bulwahn@40647
   706
end
bulwahn@40647
   707
bulwahn@41085
   708
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4
bulwahn@40651
   709
bulwahn@40651
   710
bulwahn@40647
   711
datatype finite_5 = a\<^isub>1 | a\<^isub>2 | a\<^isub>3 | a\<^isub>4 | a\<^isub>5
bulwahn@40647
   712
bulwahn@40900
   713
notation (output) a\<^isub>1  ("a\<^isub>1")
bulwahn@40900
   714
notation (output) a\<^isub>2  ("a\<^isub>2")
bulwahn@40900
   715
notation (output) a\<^isub>3  ("a\<^isub>3")
bulwahn@40900
   716
notation (output) a\<^isub>4  ("a\<^isub>4")
bulwahn@40900
   717
notation (output) a\<^isub>5  ("a\<^isub>5")
bulwahn@40900
   718
bulwahn@40647
   719
instantiation finite_5 :: enum
bulwahn@40647
   720
begin
bulwahn@40647
   721
bulwahn@40647
   722
definition
bulwahn@40647
   723
  "enum = [a\<^isub>1, a\<^isub>2, a\<^isub>3, a\<^isub>4, a\<^isub>5]"
bulwahn@40647
   724
bulwahn@41078
   725
definition
bulwahn@41078
   726
  "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
   727
bulwahn@41078
   728
definition
bulwahn@41078
   729
  "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
   730
bulwahn@40647
   731
instance proof
bulwahn@41078
   732
  fix P
bulwahn@41078
   733
  show "enum_all (P :: finite_5 \<Rightarrow> bool) = (\<forall>x. P x)"
bulwahn@41078
   734
    unfolding enum_all_finite_5_def
bulwahn@41078
   735
    by (auto, case_tac x) auto
bulwahn@41078
   736
next
bulwahn@41078
   737
  fix P
bulwahn@41078
   738
  show "enum_ex (P :: finite_5 \<Rightarrow> bool) = (\<exists>x. P x)"
bulwahn@41078
   739
    unfolding enum_ex_finite_5_def
bulwahn@41078
   740
    by (auto, case_tac x) auto
bulwahn@40647
   741
qed (auto simp add: enum_finite_5_def intro: finite_5.exhaust)
bulwahn@40647
   742
bulwahn@40647
   743
end
bulwahn@40647
   744
bulwahn@46352
   745
hide_const (open) a\<^isub>1 a\<^isub>2 a\<^isub>3 a\<^isub>4 a\<^isub>5
bulwahn@46352
   746
bulwahn@41115
   747
subsection {* An executable THE operator on finite types *}
bulwahn@41115
   748
bulwahn@41115
   749
definition
bulwahn@41115
   750
  [code del]: "enum_the P = The P"
bulwahn@41115
   751
bulwahn@41115
   752
lemma [code]:
bulwahn@41115
   753
  "The P = (case filter P enum of [x] => x | _ => enum_the P)"
bulwahn@41115
   754
proof -
bulwahn@41115
   755
  {
bulwahn@41115
   756
    fix a
bulwahn@41115
   757
    assume filter_enum: "filter P enum = [a]"
bulwahn@41115
   758
    have "The P = a"
bulwahn@41115
   759
    proof (rule the_equality)
bulwahn@41115
   760
      fix x
bulwahn@41115
   761
      assume "P x"
bulwahn@41115
   762
      show "x = a"
bulwahn@41115
   763
      proof (rule ccontr)
bulwahn@41115
   764
        assume "x \<noteq> a"
bulwahn@41115
   765
        from filter_enum obtain us vs
bulwahn@41115
   766
          where enum_eq: "enum = us @ [a] @ vs"
bulwahn@41115
   767
          and "\<forall> x \<in> set us. \<not> P x"
bulwahn@41115
   768
          and "\<forall> x \<in> set vs. \<not> P x"
bulwahn@41115
   769
          and "P a"
bulwahn@41115
   770
          by (auto simp add: filter_eq_Cons_iff) (simp only: filter_empty_conv[symmetric])
bulwahn@41115
   771
        with `P x` in_enum[of x, unfolded enum_eq] `x \<noteq> a` show "False" by auto
bulwahn@41115
   772
      qed
bulwahn@41115
   773
    next
bulwahn@41115
   774
      from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
bulwahn@41115
   775
    qed
bulwahn@41115
   776
  }
bulwahn@41115
   777
  from this show ?thesis
bulwahn@41115
   778
    unfolding enum_the_def by (auto split: list.split)
bulwahn@41115
   779
qed
bulwahn@41115
   780
bulwahn@46329
   781
code_abort enum_the
bulwahn@46336
   782
code_const enum_the (Eval "(fn p => raise Match)")
bulwahn@46329
   783
bulwahn@46329
   784
subsection {* Further operations on finite types *}
bulwahn@46329
   785
bulwahn@46329
   786
lemma [code]:
bulwahn@46329
   787
  "Collect P = set (filter P enum)"
bulwahn@46329
   788
by (auto simp add: enum_UNIV)
haftmann@45140
   789
bulwahn@46357
   790
lemma tranclp_unfold [code, no_atp]:
bulwahn@46357
   791
  "tranclp r a b \<equiv> (a, b) \<in> trancl {(x, y). r x y}"
bulwahn@46357
   792
by (simp add: trancl_def)
bulwahn@46352
   793
bulwahn@46359
   794
lemma rtranclp_rtrancl_eq[code, no_atp]:
bulwahn@46359
   795
  "rtranclp r x y = ((x, y) : rtrancl {(x, y). r x y})"
bulwahn@46359
   796
unfolding rtrancl_def by auto
bulwahn@46359
   797
bulwahn@46358
   798
lemma max_ext_eq[code]:
bulwahn@46358
   799
  "max_ext R = {(X, Y). finite X & finite Y & Y ~={} & (ALL x. x : X --> (EX xa : Y. (x, xa) : R))}"
bulwahn@46358
   800
by (auto simp add: max_ext.simps)
bulwahn@46358
   801
bulwahn@46361
   802
lemma max_extp_eq[code]:
bulwahn@46361
   803
  "max_extp r x y = ((x, y) : max_ext {(x, y). r x y})"
bulwahn@46361
   804
unfolding max_ext_def by auto
bulwahn@46361
   805
bulwahn@46361
   806
lemma mlex_eq[code]:
bulwahn@46361
   807
  "f <*mlex*> R = {(x, y). f x < f y \<or> (f x <= f y \<and> (x, y) : R)}"
bulwahn@46361
   808
unfolding mlex_prod_def by auto
bulwahn@46361
   809
bulwahn@46352
   810
subsection {* Executable accessible part *}
bulwahn@46352
   811
(* FIXME: should be moved somewhere else !? *)
bulwahn@46352
   812
bulwahn@46352
   813
subsubsection {* Finite monotone eventually stable sequences *}
bulwahn@46352
   814
bulwahn@46352
   815
lemma finite_mono_remains_stable_implies_strict_prefix:
bulwahn@46352
   816
  fixes f :: "nat \<Rightarrow> 'a::order"
bulwahn@46352
   817
  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
   818
  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
   819
  using assms
bulwahn@46352
   820
proof -
bulwahn@46352
   821
  have "\<exists>n. f n = f (Suc n)"
bulwahn@46352
   822
  proof (rule ccontr)
bulwahn@46352
   823
    assume "\<not> ?thesis"
bulwahn@46352
   824
    then have "\<And>n. f n \<noteq> f (Suc n)" by auto
bulwahn@46352
   825
    then have "\<And>n. f n < f (Suc n)"
bulwahn@46352
   826
      using  `mono f` by (auto simp: le_less mono_iff_le_Suc)
bulwahn@46352
   827
    with lift_Suc_mono_less_iff[of f]
bulwahn@46352
   828
    have "\<And>n m. n < m \<Longrightarrow> f n < f m" by auto
bulwahn@46352
   829
    then have "inj f"
bulwahn@46352
   830
      by (auto simp: inj_on_def) (metis linorder_less_linear order_less_imp_not_eq)
bulwahn@46352
   831
    with `finite (range f)` have "finite (UNIV::nat set)"
bulwahn@46352
   832
      by (rule finite_imageD)
bulwahn@46352
   833
    then show False by simp
bulwahn@46352
   834
  qed
bulwahn@46352
   835
  then guess n .. note n = this
bulwahn@46352
   836
  def N \<equiv> "LEAST n. f n = f (Suc n)"
bulwahn@46352
   837
  have N: "f N = f (Suc N)"
bulwahn@46352
   838
    unfolding N_def using n by (rule LeastI)
bulwahn@46352
   839
  show ?thesis
bulwahn@46352
   840
  proof (intro exI[of _ N] conjI allI impI)
bulwahn@46352
   841
    fix n assume "N \<le> n"
bulwahn@46352
   842
    then have "\<And>m. N \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> f m = f N"
bulwahn@46352
   843
    proof (induct rule: dec_induct)
bulwahn@46352
   844
      case (step n) then show ?case
bulwahn@46352
   845
        using eq[rule_format, of "n - 1"] N
bulwahn@46352
   846
        by (cases n) (auto simp add: le_Suc_eq)
bulwahn@46352
   847
    qed simp
bulwahn@46352
   848
    from this[of n] `N \<le> n` show "f N = f n" by auto
bulwahn@46352
   849
  next
bulwahn@46352
   850
    fix n m :: nat assume "m < n" "n \<le> N"
bulwahn@46352
   851
    then show "f m < f n"
bulwahn@46352
   852
    proof (induct rule: less_Suc_induct[consumes 1])
bulwahn@46352
   853
      case (1 i)
bulwahn@46352
   854
      then have "i < N" by simp
bulwahn@46352
   855
      then have "f i \<noteq> f (Suc i)"
bulwahn@46352
   856
        unfolding N_def by (rule not_less_Least)
bulwahn@46352
   857
      with `mono f` show ?case by (simp add: mono_iff_le_Suc less_le)
bulwahn@46352
   858
    qed auto
bulwahn@46352
   859
  qed
bulwahn@46352
   860
qed
bulwahn@46352
   861
bulwahn@46352
   862
lemma finite_mono_strict_prefix_implies_finite_fixpoint:
bulwahn@46352
   863
  fixes f :: "nat \<Rightarrow> 'a set"
bulwahn@46352
   864
  assumes S: "\<And>i. f i \<subseteq> S" "finite S"
bulwahn@46352
   865
    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
   866
  shows "f (card S) = (\<Union>n. f n)"
bulwahn@46352
   867
proof -
bulwahn@46352
   868
  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
   869
bulwahn@46352
   870
  { fix i have "i \<le> N \<Longrightarrow> i \<le> card (f i)"
bulwahn@46352
   871
    proof (induct i)
bulwahn@46352
   872
      case 0 then show ?case by simp
bulwahn@46352
   873
    next
bulwahn@46352
   874
      case (Suc i)
bulwahn@46352
   875
      with inj[rule_format, of "Suc i" i]
bulwahn@46352
   876
      have "(f i) \<subset> (f (Suc i))" by auto
bulwahn@46352
   877
      moreover have "finite (f (Suc i))" using S by (rule finite_subset)
bulwahn@46352
   878
      ultimately have "card (f i) < card (f (Suc i))" by (intro psubset_card_mono)
bulwahn@46352
   879
      with Suc show ?case using inj by auto
bulwahn@46352
   880
    qed
bulwahn@46352
   881
  }
bulwahn@46352
   882
  then have "N \<le> card (f N)" by simp
bulwahn@46352
   883
  also have "\<dots> \<le> card S" using S by (intro card_mono)
bulwahn@46352
   884
  finally have "f (card S) = f N" using eq by auto
bulwahn@46352
   885
  then show ?thesis using eq inj[rule_format, of N]
bulwahn@46352
   886
    apply auto
bulwahn@46352
   887
    apply (case_tac "n < N")
bulwahn@46352
   888
    apply (auto simp: not_less)
bulwahn@46352
   889
    done
bulwahn@46352
   890
qed
bulwahn@46352
   891
bulwahn@46352
   892
subsubsection {* Bounded accessible part *}
bulwahn@46352
   893
bulwahn@46352
   894
fun bacc :: "('a * 'a) set => nat => 'a set" 
bulwahn@46352
   895
where
bulwahn@46352
   896
  "bacc r 0 = {x. \<forall> y. (y, x) \<notin> r}"
bulwahn@46352
   897
| "bacc r (Suc n) = (bacc r n Un {x. \<forall> y. (y, x) : r --> y : bacc r n})"
bulwahn@46352
   898
bulwahn@46352
   899
lemma bacc_subseteq_acc:
bulwahn@46352
   900
  "bacc r n \<subseteq> acc r"
bulwahn@46352
   901
by (induct n) (auto intro: acc.intros)
bulwahn@40657
   902
bulwahn@46352
   903
lemma bacc_mono:
bulwahn@46352
   904
  "n <= m ==> bacc r n \<subseteq> bacc r m"
bulwahn@46352
   905
by (induct rule: dec_induct) auto
bulwahn@46352
   906
  
bulwahn@46352
   907
lemma bacc_upper_bound:
bulwahn@46352
   908
  "bacc (r :: ('a * 'a) set)  (card (UNIV :: ('a :: enum) set)) = (UN n. bacc r n)"
bulwahn@46352
   909
proof -
bulwahn@46352
   910
  have "mono (bacc r)" unfolding mono_def by (simp add: bacc_mono)
bulwahn@46352
   911
  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
   912
  moreover have "finite (range (bacc r))" by auto
bulwahn@46352
   913
  ultimately show ?thesis
bulwahn@46352
   914
   by (intro finite_mono_strict_prefix_implies_finite_fixpoint)
bulwahn@46352
   915
     (auto intro: finite_mono_remains_stable_implies_strict_prefix  simp add: enum_UNIV)
bulwahn@46352
   916
qed
bulwahn@46352
   917
bulwahn@46352
   918
lemma acc_subseteq_bacc:
bulwahn@46352
   919
  assumes "finite r"
bulwahn@46352
   920
  shows "acc r \<subseteq> (UN n. bacc r n)"
bulwahn@46352
   921
proof
bulwahn@46352
   922
  fix x
bulwahn@46352
   923
  assume "x : acc r"
bulwahn@46352
   924
  from this have "\<exists> n. x : bacc r n"
bulwahn@46352
   925
  proof (induct x arbitrary: n rule: acc.induct)
bulwahn@46352
   926
    case (accI x)
bulwahn@46352
   927
    from accI have "\<forall> y. \<exists> n. (y, x) \<in> r --> y : bacc r n" by simp
bulwahn@46352
   928
    from choice[OF this] guess n .. note n = this
bulwahn@46352
   929
    have "\<exists> n. \<forall>y. (y, x) : r --> y : bacc r n"
bulwahn@46352
   930
    proof (safe intro!: exI[of _ "Max ((%(y,x). n y)`r)"])
bulwahn@46352
   931
      fix y assume y: "(y, x) : r"
bulwahn@46352
   932
      with n have "y : bacc r (n y)" by auto
bulwahn@46352
   933
      moreover have "n y <= Max ((%(y, x). n y) ` r)"
bulwahn@46352
   934
        using y `finite r` by (auto intro!: Max_ge)
bulwahn@46352
   935
      note bacc_mono[OF this, of r]
bulwahn@46352
   936
      ultimately show "y : bacc r (Max ((%(y, x). n y) ` r))" by auto
bulwahn@46352
   937
    qed
bulwahn@46352
   938
    from this guess n ..
bulwahn@46352
   939
    from this show ?case
bulwahn@46352
   940
      by (auto simp add: Let_def intro!: exI[of _ "Suc n"])
bulwahn@46352
   941
  qed
bulwahn@46352
   942
  thus "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