src/HOL/Enum.thy
author paulson <lp15@cam.ac.uk>
Tue Apr 25 16:39:54 2017 +0100 (2017-04-25)
changeset 65578 e4997c181cce
parent 64592 7759f1766189
child 65956 639eb3617a86
permissions -rw-r--r--
New material from PNT proof, as well as more default [simp] declarations. Also removed duplicate theorems about geometric series
haftmann@31596
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@26348
     2
wenzelm@60758
     3
section \<open>Finite types as explicit enumerations\<close>
haftmann@26348
     4
haftmann@26348
     5
theory Enum
haftmann@58101
     6
imports Map Groups_List
haftmann@26348
     7
begin
haftmann@26348
     8
wenzelm@61799
     9
subsection \<open>Class \<open>enum\<close>\<close>
haftmann@26348
    10
haftmann@29797
    11
class enum =
haftmann@26348
    12
  fixes enum :: "'a list"
bulwahn@41078
    13
  fixes enum_all :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
haftmann@49950
    14
  fixes enum_ex :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
haftmann@33635
    15
  assumes UNIV_enum: "UNIV = set enum"
haftmann@26444
    16
    and enum_distinct: "distinct enum"
haftmann@49950
    17
  assumes enum_all_UNIV: "enum_all P \<longleftrightarrow> Ball UNIV P"
haftmann@49950
    18
  assumes enum_ex_UNIV: "enum_ex P \<longleftrightarrow> Bex UNIV P" 
wenzelm@61799
    19
   \<comment> \<open>tailored towards simple instantiation\<close>
haftmann@26348
    20
begin
haftmann@26348
    21
haftmann@29797
    22
subclass finite proof
haftmann@29797
    23
qed (simp add: UNIV_enum)
haftmann@26444
    24
haftmann@49950
    25
lemma enum_UNIV:
haftmann@49950
    26
  "set enum = UNIV"
haftmann@49950
    27
  by (simp only: UNIV_enum)
haftmann@26444
    28
bulwahn@40683
    29
lemma in_enum: "x \<in> set enum"
haftmann@49950
    30
  by (simp add: enum_UNIV)
haftmann@26348
    31
haftmann@26348
    32
lemma enum_eq_I:
haftmann@26348
    33
  assumes "\<And>x. x \<in> set xs"
haftmann@26348
    34
  shows "set enum = set xs"
haftmann@26348
    35
proof -
haftmann@26348
    36
  from assms UNIV_eq_I have "UNIV = set xs" by auto
bulwahn@41078
    37
  with enum_UNIV show ?thesis by simp
haftmann@26348
    38
qed
haftmann@26348
    39
haftmann@49972
    40
lemma card_UNIV_length_enum:
haftmann@49972
    41
  "card (UNIV :: 'a set) = length enum"
haftmann@49972
    42
  by (simp add: UNIV_enum distinct_card enum_distinct)
haftmann@49972
    43
haftmann@49950
    44
lemma enum_all [simp]:
haftmann@49950
    45
  "enum_all = HOL.All"
haftmann@49950
    46
  by (simp add: fun_eq_iff enum_all_UNIV)
haftmann@49950
    47
haftmann@49950
    48
lemma enum_ex [simp]:
haftmann@49950
    49
  "enum_ex = HOL.Ex" 
haftmann@49950
    50
  by (simp add: fun_eq_iff enum_ex_UNIV)
haftmann@49950
    51
haftmann@26348
    52
end
haftmann@26348
    53
haftmann@26348
    54
wenzelm@60758
    55
subsection \<open>Implementations using @{class enum}\<close>
haftmann@49949
    56
wenzelm@60758
    57
subsubsection \<open>Unbounded operations and quantifiers\<close>
haftmann@49949
    58
haftmann@49949
    59
lemma Collect_code [code]:
haftmann@49949
    60
  "Collect P = set (filter P enum)"
haftmann@49950
    61
  by (simp add: enum_UNIV)
haftmann@49949
    62
bulwahn@50567
    63
lemma vimage_code [code]:
bulwahn@50567
    64
  "f -` B = set (filter (%x. f x : B) enum_class.enum)"
bulwahn@50567
    65
  unfolding vimage_def Collect_code ..
bulwahn@50567
    66
haftmann@49949
    67
definition card_UNIV :: "'a itself \<Rightarrow> nat"
haftmann@49949
    68
where
haftmann@49949
    69
  [code del]: "card_UNIV TYPE('a) = card (UNIV :: 'a set)"
haftmann@49949
    70
haftmann@49949
    71
lemma [code]:
haftmann@49949
    72
  "card_UNIV TYPE('a :: enum) = card (set (Enum.enum :: 'a list))"
haftmann@49949
    73
  by (simp only: card_UNIV_def enum_UNIV)
haftmann@49949
    74
haftmann@49949
    75
lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> enum_all P"
haftmann@49950
    76
  by simp
haftmann@49949
    77
haftmann@49949
    78
lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> enum_ex P"
haftmann@49950
    79
  by simp
haftmann@49949
    80
haftmann@49949
    81
lemma exists1_code [code]: "(\<exists>!x. P x) \<longleftrightarrow> list_ex1 P enum"
haftmann@49950
    82
  by (auto simp add: list_ex1_iff enum_UNIV)
haftmann@49949
    83
haftmann@49949
    84
wenzelm@60758
    85
subsubsection \<open>An executable choice operator\<close>
haftmann@49949
    86
haftmann@49949
    87
definition
haftmann@49949
    88
  [code del]: "enum_the = The"
haftmann@49949
    89
haftmann@49949
    90
lemma [code]:
haftmann@49949
    91
  "The P = (case filter P enum of [x] => x | _ => enum_the P)"
haftmann@49949
    92
proof -
haftmann@49949
    93
  {
haftmann@49949
    94
    fix a
haftmann@49949
    95
    assume filter_enum: "filter P enum = [a]"
haftmann@49949
    96
    have "The P = a"
haftmann@49949
    97
    proof (rule the_equality)
haftmann@49949
    98
      fix x
haftmann@49949
    99
      assume "P x"
haftmann@49949
   100
      show "x = a"
haftmann@49949
   101
      proof (rule ccontr)
haftmann@49949
   102
        assume "x \<noteq> a"
haftmann@49949
   103
        from filter_enum obtain us vs
haftmann@49949
   104
          where enum_eq: "enum = us @ [a] @ vs"
haftmann@49949
   105
          and "\<forall> x \<in> set us. \<not> P x"
haftmann@49949
   106
          and "\<forall> x \<in> set vs. \<not> P x"
haftmann@49949
   107
          and "P a"
haftmann@49949
   108
          by (auto simp add: filter_eq_Cons_iff) (simp only: filter_empty_conv[symmetric])
wenzelm@60758
   109
        with \<open>P x\<close> in_enum[of x, unfolded enum_eq] \<open>x \<noteq> a\<close> show "False" by auto
haftmann@49949
   110
      qed
haftmann@49949
   111
    next
haftmann@49949
   112
      from filter_enum show "P a" by (auto simp add: filter_eq_Cons_iff)
haftmann@49949
   113
    qed
haftmann@49949
   114
  }
haftmann@49949
   115
  from this show ?thesis
haftmann@49949
   116
    unfolding enum_the_def by (auto split: list.split)
haftmann@49949
   117
qed
haftmann@49949
   118
haftmann@54890
   119
declare [[code abort: enum_the]]
haftmann@52435
   120
haftmann@52435
   121
code_printing
haftmann@52435
   122
  constant enum_the \<rightharpoonup> (Eval) "(fn '_ => raise Match)"
haftmann@49949
   123
haftmann@49949
   124
wenzelm@60758
   125
subsubsection \<open>Equality and order on functions\<close>
haftmann@26348
   126
haftmann@38857
   127
instantiation "fun" :: (enum, equal) equal
haftmann@26513
   128
begin
haftmann@26348
   129
haftmann@26513
   130
definition
haftmann@38857
   131
  "HOL.equal f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
haftmann@26513
   132
haftmann@31464
   133
instance proof
haftmann@49950
   134
qed (simp_all add: equal_fun_def fun_eq_iff enum_UNIV)
haftmann@26513
   135
haftmann@26513
   136
end
haftmann@26348
   137
bulwahn@40898
   138
lemma [code]:
bulwahn@41078
   139
  "HOL.equal f g \<longleftrightarrow> enum_all (%x. f x = g x)"
haftmann@49950
   140
  by (auto simp add: equal fun_eq_iff)
bulwahn@40898
   141
haftmann@38857
   142
lemma [code nbe]:
haftmann@38857
   143
  "HOL.equal (f :: _ \<Rightarrow> _) f \<longleftrightarrow> True"
haftmann@38857
   144
  by (fact equal_refl)
haftmann@38857
   145
haftmann@28562
   146
lemma order_fun [code]:
wenzelm@61076
   147
  fixes f g :: "'a::enum \<Rightarrow> 'b::order"
bulwahn@41078
   148
  shows "f \<le> g \<longleftrightarrow> enum_all (\<lambda>x. f x \<le> g x)"
bulwahn@41078
   149
    and "f < g \<longleftrightarrow> f \<le> g \<and> enum_ex (\<lambda>x. f x \<noteq> g x)"
haftmann@49950
   150
  by (simp_all add: fun_eq_iff le_fun_def order_less_le)
haftmann@26968
   151
haftmann@26968
   152
wenzelm@60758
   153
subsubsection \<open>Operations on relations\<close>
haftmann@49949
   154
haftmann@49949
   155
lemma [code]:
haftmann@49949
   156
  "Id = image (\<lambda>x. (x, x)) (set Enum.enum)"
haftmann@49949
   157
  by (auto intro: imageI in_enum)
haftmann@26968
   158
blanchet@54148
   159
lemma tranclp_unfold [code]:
haftmann@49949
   160
  "tranclp r a b \<longleftrightarrow> (a, b) \<in> trancl {(x, y). r x y}"
haftmann@49949
   161
  by (simp add: trancl_def)
haftmann@49949
   162
blanchet@54148
   163
lemma rtranclp_rtrancl_eq [code]:
haftmann@49949
   164
  "rtranclp r x y \<longleftrightarrow> (x, y) \<in> rtrancl {(x, y). r x y}"
haftmann@49949
   165
  by (simp add: rtrancl_def)
haftmann@26968
   166
haftmann@49949
   167
lemma max_ext_eq [code]:
haftmann@49949
   168
  "max_ext R = {(X, Y). finite X \<and> finite Y \<and> Y \<noteq> {} \<and> (\<forall>x. x \<in> X \<longrightarrow> (\<exists>xa \<in> Y. (x, xa) \<in> R))}"
haftmann@49949
   169
  by (auto simp add: max_ext.simps)
haftmann@49949
   170
haftmann@49949
   171
lemma max_extp_eq [code]:
haftmann@49949
   172
  "max_extp r x y \<longleftrightarrow> (x, y) \<in> max_ext {(x, y). r x y}"
haftmann@49949
   173
  by (simp add: max_ext_def)
haftmann@26348
   174
haftmann@49949
   175
lemma mlex_eq [code]:
haftmann@49949
   176
  "f <*mlex*> R = {(x, y). f x < f y \<or> (f x \<le> f y \<and> (x, y) \<in> R)}"
haftmann@49949
   177
  by (auto simp add: mlex_prod_def)
haftmann@49949
   178
blanchet@55088
   179
wenzelm@60758
   180
subsubsection \<open>Bounded accessible part\<close>
blanchet@55088
   181
blanchet@55088
   182
primrec bacc :: "('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> 'a set" 
blanchet@55088
   183
where
blanchet@55088
   184
  "bacc r 0 = {x. \<forall> y. (y, x) \<notin> r}"
blanchet@55088
   185
| "bacc r (Suc n) = (bacc r n \<union> {x. \<forall>y. (y, x) \<in> r \<longrightarrow> y \<in> bacc r n})"
blanchet@55088
   186
blanchet@55088
   187
lemma bacc_subseteq_acc:
blanchet@55088
   188
  "bacc r n \<subseteq> Wellfounded.acc r"
blanchet@55088
   189
  by (induct n) (auto intro: acc.intros)
blanchet@55088
   190
blanchet@55088
   191
lemma bacc_mono:
blanchet@55088
   192
  "n \<le> m \<Longrightarrow> bacc r n \<subseteq> bacc r m"
blanchet@55088
   193
  by (induct rule: dec_induct) auto
blanchet@55088
   194
  
blanchet@55088
   195
lemma bacc_upper_bound:
blanchet@55088
   196
  "bacc (r :: ('a \<times> 'a) set)  (card (UNIV :: 'a::finite set)) = (\<Union>n. bacc r n)"
blanchet@55088
   197
proof -
blanchet@55088
   198
  have "mono (bacc r)" unfolding mono_def by (simp add: bacc_mono)
blanchet@55088
   199
  moreover have "\<forall>n. bacc r n = bacc r (Suc n) \<longrightarrow> bacc r (Suc n) = bacc r (Suc (Suc n))" by auto
blanchet@55088
   200
  moreover have "finite (range (bacc r))" by auto
blanchet@55088
   201
  ultimately show ?thesis
blanchet@55088
   202
   by (intro finite_mono_strict_prefix_implies_finite_fixpoint)
blanchet@55088
   203
     (auto intro: finite_mono_remains_stable_implies_strict_prefix)
blanchet@55088
   204
qed
blanchet@55088
   205
blanchet@55088
   206
lemma acc_subseteq_bacc:
blanchet@55088
   207
  assumes "finite r"
blanchet@55088
   208
  shows "Wellfounded.acc r \<subseteq> (\<Union>n. bacc r n)"
blanchet@55088
   209
proof
blanchet@55088
   210
  fix x
blanchet@55088
   211
  assume "x : Wellfounded.acc r"
blanchet@55088
   212
  then have "\<exists> n. x : bacc r n"
blanchet@55088
   213
  proof (induct x arbitrary: rule: acc.induct)
blanchet@55088
   214
    case (accI x)
blanchet@55088
   215
    then have "\<forall>y. \<exists> n. (y, x) \<in> r --> y : bacc r n" by simp
blanchet@55088
   216
    from choice[OF this] obtain n where n: "\<forall>y. (y, x) \<in> r \<longrightarrow> y \<in> bacc r (n y)" ..
blanchet@55088
   217
    obtain n where "\<And>y. (y, x) : r \<Longrightarrow> y : bacc r n"
blanchet@55088
   218
    proof
blanchet@55088
   219
      fix y assume y: "(y, x) : r"
blanchet@55088
   220
      with n have "y : bacc r (n y)" by auto
blanchet@55088
   221
      moreover have "n y <= Max ((%(y, x). n y) ` r)"
wenzelm@60758
   222
        using y \<open>finite r\<close> by (auto intro!: Max_ge)
blanchet@55088
   223
      note bacc_mono[OF this, of r]
blanchet@55088
   224
      ultimately show "y : bacc r (Max ((%(y, x). n y) ` r))" by auto
blanchet@55088
   225
    qed
blanchet@55088
   226
    then show ?case
blanchet@55088
   227
      by (auto simp add: Let_def intro!: exI[of _ "Suc n"])
blanchet@55088
   228
  qed
blanchet@55088
   229
  then show "x : (UN n. bacc r n)" by auto
blanchet@55088
   230
qed
blanchet@55088
   231
blanchet@55088
   232
lemma acc_bacc_eq:
blanchet@55088
   233
  fixes A :: "('a :: finite \<times> 'a) set"
blanchet@55088
   234
  assumes "finite A"
blanchet@55088
   235
  shows "Wellfounded.acc A = bacc A (card (UNIV :: 'a set))"
blanchet@55088
   236
  using assms by (metis acc_subseteq_bacc bacc_subseteq_acc bacc_upper_bound order_eq_iff)
blanchet@55088
   237
haftmann@49949
   238
lemma [code]:
haftmann@49949
   239
  fixes xs :: "('a::finite \<times> 'a) list"
haftmann@54295
   240
  shows "Wellfounded.acc (set xs) = bacc (set xs) (card_UNIV TYPE('a))"
haftmann@49949
   241
  by (simp add: card_UNIV_def acc_bacc_eq)
haftmann@49949
   242
haftmann@26348
   243
wenzelm@60758
   244
subsection \<open>Default instances for @{class enum}\<close>
haftmann@26348
   245
haftmann@26444
   246
lemma map_of_zip_enum_is_Some:
wenzelm@61076
   247
  assumes "length ys = length (enum :: 'a::enum list)"
wenzelm@61076
   248
  shows "\<exists>y. map_of (zip (enum :: 'a::enum list) ys) x = Some y"
haftmann@26444
   249
proof -
wenzelm@61076
   250
  from assms have "x \<in> set (enum :: 'a::enum list) \<longleftrightarrow>
wenzelm@61076
   251
    (\<exists>y. map_of (zip (enum :: 'a::enum list) ys) x = Some y)"
haftmann@26444
   252
    by (auto intro!: map_of_zip_is_Some)
bulwahn@41078
   253
  then show ?thesis using enum_UNIV by auto
haftmann@26444
   254
qed
haftmann@26444
   255
haftmann@26444
   256
lemma map_of_zip_enum_inject:
wenzelm@61076
   257
  fixes xs ys :: "'b::enum list"
wenzelm@61076
   258
  assumes length: "length xs = length (enum :: 'a::enum list)"
wenzelm@61076
   259
      "length ys = length (enum :: 'a::enum list)"
wenzelm@61076
   260
    and map_of: "the \<circ> map_of (zip (enum :: 'a::enum list) xs) = the \<circ> map_of (zip (enum :: 'a::enum list) ys)"
haftmann@26444
   261
  shows "xs = ys"
haftmann@26444
   262
proof -
wenzelm@61076
   263
  have "map_of (zip (enum :: 'a list) xs) = map_of (zip (enum :: 'a list) ys)"
haftmann@26444
   264
  proof
haftmann@26444
   265
    fix x :: 'a
haftmann@26444
   266
    from length map_of_zip_enum_is_Some obtain y1 y2
wenzelm@61076
   267
      where "map_of (zip (enum :: 'a list) xs) x = Some y1"
wenzelm@61076
   268
        and "map_of (zip (enum :: 'a list) ys) x = Some y2" by blast
wenzelm@47230
   269
    moreover from map_of
wenzelm@61076
   270
      have "the (map_of (zip (enum :: 'a::enum list) xs) x) = the (map_of (zip (enum :: 'a::enum list) ys) x)"
haftmann@26444
   271
      by (auto dest: fun_cong)
wenzelm@61076
   272
    ultimately show "map_of (zip (enum :: 'a::enum list) xs) x = map_of (zip (enum :: 'a::enum list) ys) x"
haftmann@26444
   273
      by simp
haftmann@26444
   274
  qed
haftmann@26444
   275
  with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
haftmann@26444
   276
qed
haftmann@26444
   277
haftmann@49950
   278
definition all_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   279
where
haftmann@49950
   280
  "all_n_lists P n \<longleftrightarrow> (\<forall>xs \<in> set (List.n_lists n enum). P xs)"
bulwahn@41078
   281
bulwahn@41078
   282
lemma [code]:
haftmann@49950
   283
  "all_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_all (%x. all_n_lists (%xs. P (x # xs)) (n - 1)))"
haftmann@49950
   284
  unfolding all_n_lists_def enum_all
haftmann@49950
   285
  by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   286
haftmann@49950
   287
definition ex_n_lists :: "(('a :: enum) list \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> bool"
bulwahn@41078
   288
where
haftmann@49950
   289
  "ex_n_lists P n \<longleftrightarrow> (\<exists>xs \<in> set (List.n_lists n enum). P xs)"
bulwahn@41078
   290
bulwahn@41078
   291
lemma [code]:
haftmann@49950
   292
  "ex_n_lists P n \<longleftrightarrow> (if n = 0 then P [] else enum_ex (%x. ex_n_lists (%xs. P (x # xs)) (n - 1)))"
haftmann@49950
   293
  unfolding ex_n_lists_def enum_ex
haftmann@49950
   294
  by (cases n) (auto simp add: enum_UNIV)
bulwahn@41078
   295
haftmann@26444
   296
instantiation "fun" :: (enum, enum) enum
haftmann@26444
   297
begin
haftmann@26444
   298
haftmann@26444
   299
definition
wenzelm@61076
   300
  "enum = map (\<lambda>ys. the o map_of (zip (enum::'a list) ys)) (List.n_lists (length (enum::'a::enum list)) enum)"
haftmann@26444
   301
bulwahn@41078
   302
definition
bulwahn@41078
   303
  "enum_all P = all_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   304
bulwahn@41078
   305
definition
bulwahn@41078
   306
  "enum_ex P = ex_n_lists (\<lambda>bs. P (the o map_of (zip enum bs))) (length (enum :: 'a list))"
bulwahn@41078
   307
haftmann@26444
   308
instance proof
wenzelm@61076
   309
  show "UNIV = set (enum :: ('a \<Rightarrow> 'b) list)"
haftmann@26444
   310
  proof (rule UNIV_eq_I)
haftmann@26444
   311
    fix f :: "'a \<Rightarrow> 'b"
wenzelm@61076
   312
    have "f = the \<circ> map_of (zip (enum :: 'a::enum list) (map f enum))"
bulwahn@40683
   313
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
haftmann@26444
   314
    then show "f \<in> set enum"
bulwahn@40683
   315
      by (auto simp add: enum_fun_def set_n_lists intro: in_enum)
haftmann@26444
   316
  qed
haftmann@26444
   317
next
haftmann@26444
   318
  from map_of_zip_enum_inject
wenzelm@61076
   319
  show "distinct (enum :: ('a \<Rightarrow> 'b) list)"
haftmann@26444
   320
    by (auto intro!: inj_onI simp add: enum_fun_def
haftmann@49950
   321
      distinct_map distinct_n_lists enum_distinct set_n_lists)
bulwahn@41078
   322
next
bulwahn@41078
   323
  fix P
haftmann@49950
   324
  show "enum_all (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Ball UNIV P"
bulwahn@41078
   325
  proof
bulwahn@41078
   326
    assume "enum_all P"
haftmann@49950
   327
    show "Ball UNIV P"
bulwahn@41078
   328
    proof
bulwahn@41078
   329
      fix f :: "'a \<Rightarrow> 'b"
wenzelm@61076
   330
      have f: "f = the \<circ> map_of (zip (enum :: 'a::enum list) (map f enum))"
bulwahn@41078
   331
        by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum)
wenzelm@60758
   332
      from \<open>enum_all P\<close> have "P (the \<circ> map_of (zip enum (map f enum)))"
bulwahn@41078
   333
        unfolding enum_all_fun_def all_n_lists_def
bulwahn@41078
   334
        apply (simp add: set_n_lists)
bulwahn@41078
   335
        apply (erule_tac x="map f enum" in allE)
bulwahn@41078
   336
        apply (auto intro!: in_enum)
bulwahn@41078
   337
        done
bulwahn@41078
   338
      from this f show "P f" by auto
bulwahn@41078
   339
    qed
bulwahn@41078
   340
  next
haftmann@49950
   341
    assume "Ball UNIV P"
bulwahn@41078
   342
    from this show "enum_all P"
bulwahn@41078
   343
      unfolding enum_all_fun_def all_n_lists_def by auto
bulwahn@41078
   344
  qed
bulwahn@41078
   345
next
bulwahn@41078
   346
  fix P
haftmann@49950
   347
  show "enum_ex (P :: ('a \<Rightarrow> 'b) \<Rightarrow> bool) = Bex UNIV P"
bulwahn@41078
   348
  proof
bulwahn@41078
   349
    assume "enum_ex P"
haftmann@49950
   350
    from this show "Bex UNIV P"
bulwahn@41078
   351
      unfolding enum_ex_fun_def ex_n_lists_def by auto
bulwahn@41078
   352
  next
haftmann@49950
   353
    assume "Bex UNIV P"
bulwahn@41078
   354
    from this obtain f where "P f" ..
wenzelm@61076
   355
    have f: "f = the \<circ> map_of (zip (enum :: 'a::enum list) (map f enum))"
bulwahn@41078
   356
      by (auto simp add: map_of_zip_map fun_eq_iff intro: in_enum) 
wenzelm@61076
   357
    from \<open>P f\<close> this have "P (the \<circ> map_of (zip (enum :: 'a::enum list) (map f enum)))"
bulwahn@41078
   358
      by auto
bulwahn@41078
   359
    from  this show "enum_ex P"
bulwahn@41078
   360
      unfolding enum_ex_fun_def ex_n_lists_def
bulwahn@41078
   361
      apply (auto simp add: set_n_lists)
bulwahn@41078
   362
      apply (rule_tac x="map f enum" in exI)
bulwahn@41078
   363
      apply (auto intro!: in_enum)
bulwahn@41078
   364
      done
bulwahn@41078
   365
  qed
haftmann@26444
   366
qed
haftmann@26444
   367
haftmann@26444
   368
end
haftmann@26444
   369
wenzelm@61076
   370
lemma enum_fun_code [code]: "enum = (let enum_a = (enum :: 'a::{enum, equal} list)
haftmann@49948
   371
  in map (\<lambda>ys. the o map_of (zip enum_a ys)) (List.n_lists (length enum_a) enum))"
haftmann@28245
   372
  by (simp add: enum_fun_def Let_def)
haftmann@26444
   373
bulwahn@41078
   374
lemma enum_all_fun_code [code]:
bulwahn@41078
   375
  "enum_all P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   376
   in all_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
haftmann@49950
   377
  by (simp only: enum_all_fun_def Let_def)
bulwahn@41078
   378
bulwahn@41078
   379
lemma enum_ex_fun_code [code]:
bulwahn@41078
   380
  "enum_ex P = (let enum_a = (enum :: 'a::{enum, equal} list)
bulwahn@41078
   381
   in ex_n_lists (\<lambda>bs. P (the o map_of (zip enum_a bs))) (length enum_a))"
haftmann@49950
   382
  by (simp only: enum_ex_fun_def Let_def)
haftmann@45963
   383
haftmann@45963
   384
instantiation set :: (enum) enum
haftmann@45963
   385
begin
haftmann@45963
   386
haftmann@45963
   387
definition
haftmann@45963
   388
  "enum = map set (sublists enum)"
haftmann@45963
   389
haftmann@45963
   390
definition
haftmann@45963
   391
  "enum_all P \<longleftrightarrow> (\<forall>A\<in>set enum. P (A::'a set))"
haftmann@45963
   392
haftmann@45963
   393
definition
haftmann@45963
   394
  "enum_ex P \<longleftrightarrow> (\<exists>A\<in>set enum. P (A::'a set))"
haftmann@45963
   395
haftmann@45963
   396
instance proof
haftmann@45963
   397
qed (simp_all add: enum_set_def enum_all_set_def enum_ex_set_def sublists_powset distinct_set_sublists
haftmann@45963
   398
  enum_distinct enum_UNIV)
huffman@29024
   399
huffman@29024
   400
end
huffman@29024
   401
haftmann@49950
   402
instantiation unit :: enum
haftmann@49950
   403
begin
haftmann@49950
   404
haftmann@49950
   405
definition
haftmann@49950
   406
  "enum = [()]"
haftmann@49950
   407
haftmann@49950
   408
definition
haftmann@49950
   409
  "enum_all P = P ()"
haftmann@49950
   410
haftmann@49950
   411
definition
haftmann@49950
   412
  "enum_ex P = P ()"
haftmann@49950
   413
haftmann@49950
   414
instance proof
haftmann@49950
   415
qed (auto simp add: enum_unit_def enum_all_unit_def enum_ex_unit_def)
haftmann@49950
   416
haftmann@49950
   417
end
haftmann@49950
   418
haftmann@49950
   419
instantiation bool :: enum
haftmann@49950
   420
begin
haftmann@49950
   421
haftmann@49950
   422
definition
haftmann@49950
   423
  "enum = [False, True]"
haftmann@49950
   424
haftmann@49950
   425
definition
haftmann@49950
   426
  "enum_all P \<longleftrightarrow> P False \<and> P True"
haftmann@49950
   427
haftmann@49950
   428
definition
haftmann@49950
   429
  "enum_ex P \<longleftrightarrow> P False \<or> P True"
haftmann@49950
   430
haftmann@49950
   431
instance proof
haftmann@49950
   432
qed (simp_all only: enum_bool_def enum_all_bool_def enum_ex_bool_def UNIV_bool, simp_all)
haftmann@49950
   433
haftmann@49950
   434
end
haftmann@49950
   435
haftmann@49950
   436
instantiation prod :: (enum, enum) enum
haftmann@49950
   437
begin
haftmann@49950
   438
haftmann@49950
   439
definition
haftmann@49950
   440
  "enum = List.product enum enum"
haftmann@49950
   441
haftmann@49950
   442
definition
haftmann@49950
   443
  "enum_all P = enum_all (%x. enum_all (%y. P (x, y)))"
haftmann@49950
   444
haftmann@49950
   445
definition
haftmann@49950
   446
  "enum_ex P = enum_ex (%x. enum_ex (%y. P (x, y)))"
haftmann@49950
   447
haftmann@49950
   448
 
wenzelm@61169
   449
instance
wenzelm@61169
   450
  by standard
wenzelm@61169
   451
    (simp_all add: enum_prod_def distinct_product
wenzelm@61169
   452
      enum_UNIV enum_distinct enum_all_prod_def enum_ex_prod_def)
haftmann@49950
   453
haftmann@49950
   454
end
haftmann@49950
   455
haftmann@49950
   456
instantiation sum :: (enum, enum) enum
haftmann@49950
   457
begin
haftmann@49950
   458
haftmann@49950
   459
definition
haftmann@49950
   460
  "enum = map Inl enum @ map Inr enum"
haftmann@49950
   461
haftmann@49950
   462
definition
haftmann@49950
   463
  "enum_all P \<longleftrightarrow> enum_all (\<lambda>x. P (Inl x)) \<and> enum_all (\<lambda>x. P (Inr x))"
haftmann@49950
   464
haftmann@49950
   465
definition
haftmann@49950
   466
  "enum_ex P \<longleftrightarrow> enum_ex (\<lambda>x. P (Inl x)) \<or> enum_ex (\<lambda>x. P (Inr x))"
haftmann@49950
   467
haftmann@49950
   468
instance proof
haftmann@49950
   469
qed (simp_all only: enum_sum_def enum_all_sum_def enum_ex_sum_def UNIV_sum,
haftmann@49950
   470
  auto simp add: enum_UNIV distinct_map enum_distinct)
haftmann@49950
   471
haftmann@49950
   472
end
haftmann@49950
   473
haftmann@49950
   474
instantiation option :: (enum) enum
haftmann@49950
   475
begin
haftmann@49950
   476
haftmann@49950
   477
definition
haftmann@49950
   478
  "enum = None # map Some enum"
haftmann@49950
   479
haftmann@49950
   480
definition
haftmann@49950
   481
  "enum_all P \<longleftrightarrow> P None \<and> enum_all (\<lambda>x. P (Some x))"
haftmann@49950
   482
haftmann@49950
   483
definition
haftmann@49950
   484
  "enum_ex P \<longleftrightarrow> P None \<or> enum_ex (\<lambda>x. P (Some x))"
haftmann@49950
   485
haftmann@49950
   486
instance proof
haftmann@49950
   487
qed (simp_all only: enum_option_def enum_all_option_def enum_ex_option_def UNIV_option_conv,
haftmann@49950
   488
  auto simp add: distinct_map enum_UNIV enum_distinct)
haftmann@49950
   489
haftmann@49950
   490
end
haftmann@49950
   491
haftmann@45963
   492
wenzelm@60758
   493
subsection \<open>Small finite types\<close>
bulwahn@40647
   494
wenzelm@60758
   495
text \<open>We define small finite types for use in Quickcheck\<close>
bulwahn@40647
   496
wenzelm@58659
   497
datatype (plugins only: code "quickcheck" extraction) finite_1 =
blanchet@58350
   498
  a\<^sub>1
bulwahn@40647
   499
wenzelm@53015
   500
notation (output) a\<^sub>1  ("a\<^sub>1")
bulwahn@40900
   501
haftmann@49950
   502
lemma UNIV_finite_1:
wenzelm@53015
   503
  "UNIV = {a\<^sub>1}"
haftmann@49950
   504
  by (auto intro: finite_1.exhaust)
haftmann@49950
   505
bulwahn@40647
   506
instantiation finite_1 :: enum
bulwahn@40647
   507
begin
bulwahn@40647
   508
bulwahn@40647
   509
definition
wenzelm@53015
   510
  "enum = [a\<^sub>1]"
bulwahn@40647
   511
bulwahn@41078
   512
definition
wenzelm@53015
   513
  "enum_all P = P a\<^sub>1"
bulwahn@41078
   514
bulwahn@41078
   515
definition
wenzelm@53015
   516
  "enum_ex P = P a\<^sub>1"
bulwahn@41078
   517
bulwahn@40647
   518
instance proof
haftmann@49950
   519
qed (simp_all only: enum_finite_1_def enum_all_finite_1_def enum_ex_finite_1_def UNIV_finite_1, simp_all)
bulwahn@40647
   520
huffman@29024
   521
end
bulwahn@40647
   522
bulwahn@40651
   523
instantiation finite_1 :: linorder
bulwahn@40651
   524
begin
bulwahn@40651
   525
haftmann@49950
   526
definition less_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
haftmann@49950
   527
where
haftmann@49950
   528
  "x < (y :: finite_1) \<longleftrightarrow> False"
haftmann@49950
   529
bulwahn@40651
   530
definition less_eq_finite_1 :: "finite_1 \<Rightarrow> finite_1 \<Rightarrow> bool"
bulwahn@40651
   531
where
haftmann@49950
   532
  "x \<le> (y :: finite_1) \<longleftrightarrow> True"
bulwahn@40651
   533
bulwahn@40651
   534
instance
bulwahn@40651
   535
apply (intro_classes)
bulwahn@40651
   536
apply (auto simp add: less_finite_1_def less_eq_finite_1_def)
bulwahn@40651
   537
apply (metis finite_1.exhaust)
bulwahn@40651
   538
done
bulwahn@40651
   539
bulwahn@40651
   540
end
bulwahn@40651
   541
Andreas@57922
   542
instance finite_1 :: "{dense_linorder, wellorder}"
Andreas@57922
   543
by intro_classes (simp_all add: less_finite_1_def)
Andreas@57922
   544
Andreas@57818
   545
instantiation finite_1 :: complete_lattice
Andreas@57818
   546
begin
Andreas@57818
   547
Andreas@57818
   548
definition [simp]: "Inf = (\<lambda>_. a\<^sub>1)"
Andreas@57818
   549
definition [simp]: "Sup = (\<lambda>_. a\<^sub>1)"
Andreas@57818
   550
definition [simp]: "bot = a\<^sub>1"
Andreas@57818
   551
definition [simp]: "top = a\<^sub>1"
Andreas@57818
   552
definition [simp]: "inf = (\<lambda>_ _. a\<^sub>1)"
Andreas@57818
   553
definition [simp]: "sup = (\<lambda>_ _. a\<^sub>1)"
Andreas@57818
   554
Andreas@57818
   555
instance by intro_classes(simp_all add: less_eq_finite_1_def)
Andreas@57818
   556
end
Andreas@57818
   557
Andreas@57818
   558
instance finite_1 :: complete_distrib_lattice
haftmann@62343
   559
  by standard simp_all
Andreas@57818
   560
Andreas@57818
   561
instance finite_1 :: complete_linorder ..
Andreas@57818
   562
Andreas@57922
   563
lemma finite_1_eq: "x = a\<^sub>1"
Andreas@57922
   564
by(cases x) simp
Andreas@57922
   565
wenzelm@60758
   566
simproc_setup finite_1_eq ("x::finite_1") = \<open>
wenzelm@59582
   567
  fn _ => fn _ => fn ct =>
wenzelm@59582
   568
    (case Thm.term_of ct of
wenzelm@59582
   569
      Const (@{const_name a\<^sub>1}, _) => NONE
wenzelm@59582
   570
    | _ => SOME (mk_meta_eq @{thm finite_1_eq}))
wenzelm@60758
   571
\<close>
Andreas@57922
   572
wenzelm@59582
   573
instantiation finite_1 :: complete_boolean_algebra
wenzelm@59582
   574
begin
Andreas@57922
   575
definition [simp]: "op - = (\<lambda>_ _. a\<^sub>1)"
Andreas@57922
   576
definition [simp]: "uminus = (\<lambda>_. a\<^sub>1)"
Andreas@57922
   577
instance by intro_classes simp_all
Andreas@57922
   578
end
Andreas@57922
   579
Andreas@57922
   580
instantiation finite_1 :: 
Andreas@57922
   581
  "{linordered_ring_strict, linordered_comm_semiring_strict, ordered_comm_ring,
Andreas@57922
   582
    ordered_cancel_comm_monoid_diff, comm_monoid_mult, ordered_ring_abs,
haftmann@64290
   583
    one, modulo, sgn, inverse}"
Andreas@57922
   584
begin
Andreas@57922
   585
definition [simp]: "Groups.zero = a\<^sub>1"
Andreas@57922
   586
definition [simp]: "Groups.one = a\<^sub>1"
Andreas@57922
   587
definition [simp]: "op + = (\<lambda>_ _. a\<^sub>1)"
Andreas@57922
   588
definition [simp]: "op * = (\<lambda>_ _. a\<^sub>1)"
Andreas@57922
   589
definition [simp]: "op mod = (\<lambda>_ _. a\<^sub>1)" 
Andreas@57922
   590
definition [simp]: "abs = (\<lambda>_. a\<^sub>1)"
Andreas@57922
   591
definition [simp]: "sgn = (\<lambda>_. a\<^sub>1)"
Andreas@57922
   592
definition [simp]: "inverse = (\<lambda>_. a\<^sub>1)"
haftmann@60352
   593
definition [simp]: "divide = (\<lambda>_ _. a\<^sub>1)"
Andreas@57922
   594
Andreas@57922
   595
instance by intro_classes(simp_all add: less_finite_1_def)
Andreas@57922
   596
end
Andreas@57922
   597
Andreas@57922
   598
declare [[simproc del: finite_1_eq]]
wenzelm@53015
   599
hide_const (open) a\<^sub>1
bulwahn@40657
   600
wenzelm@58659
   601
datatype (plugins only: code "quickcheck" extraction) finite_2 =
blanchet@58350
   602
  a\<^sub>1 | a\<^sub>2
bulwahn@40647
   603
wenzelm@53015
   604
notation (output) a\<^sub>1  ("a\<^sub>1")
wenzelm@53015
   605
notation (output) a\<^sub>2  ("a\<^sub>2")
bulwahn@40900
   606
haftmann@49950
   607
lemma UNIV_finite_2:
wenzelm@53015
   608
  "UNIV = {a\<^sub>1, a\<^sub>2}"
haftmann@49950
   609
  by (auto intro: finite_2.exhaust)
haftmann@49950
   610
bulwahn@40647
   611
instantiation finite_2 :: enum
bulwahn@40647
   612
begin
bulwahn@40647
   613
bulwahn@40647
   614
definition
wenzelm@53015
   615
  "enum = [a\<^sub>1, a\<^sub>2]"
bulwahn@40647
   616
bulwahn@41078
   617
definition
wenzelm@53015
   618
  "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2"
bulwahn@41078
   619
bulwahn@41078
   620
definition
wenzelm@53015
   621
  "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2"
bulwahn@41078
   622
bulwahn@40647
   623
instance proof
haftmann@49950
   624
qed (simp_all only: enum_finite_2_def enum_all_finite_2_def enum_ex_finite_2_def UNIV_finite_2, simp_all)
bulwahn@40647
   625
bulwahn@40647
   626
end
bulwahn@40647
   627
bulwahn@40651
   628
instantiation finite_2 :: linorder
bulwahn@40651
   629
begin
bulwahn@40651
   630
bulwahn@40651
   631
definition less_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   632
where
wenzelm@53015
   633
  "x < y \<longleftrightarrow> x = a\<^sub>1 \<and> y = a\<^sub>2"
bulwahn@40651
   634
bulwahn@40651
   635
definition less_eq_finite_2 :: "finite_2 \<Rightarrow> finite_2 \<Rightarrow> bool"
bulwahn@40651
   636
where
haftmann@49950
   637
  "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_2)"
bulwahn@40651
   638
bulwahn@40651
   639
instance
bulwahn@40651
   640
apply (intro_classes)
bulwahn@40651
   641
apply (auto simp add: less_finite_2_def less_eq_finite_2_def)
haftmann@49950
   642
apply (metis finite_2.nchotomy)+
bulwahn@40651
   643
done
bulwahn@40651
   644
bulwahn@40651
   645
end
bulwahn@40651
   646
Andreas@57922
   647
instance finite_2 :: wellorder
Andreas@57922
   648
by(rule wf_wellorderI)(simp add: less_finite_2_def, intro_classes)
Andreas@57922
   649
Andreas@57818
   650
instantiation finite_2 :: complete_lattice
Andreas@57818
   651
begin
Andreas@57818
   652
Andreas@57818
   653
definition "\<Sqinter>A = (if a\<^sub>1 \<in> A then a\<^sub>1 else a\<^sub>2)"
Andreas@57818
   654
definition "\<Squnion>A = (if a\<^sub>2 \<in> A then a\<^sub>2 else a\<^sub>1)"
Andreas@57818
   655
definition [simp]: "bot = a\<^sub>1"
Andreas@57818
   656
definition [simp]: "top = a\<^sub>2"
Andreas@57818
   657
definition "x \<sqinter> y = (if x = a\<^sub>1 \<or> y = a\<^sub>1 then a\<^sub>1 else a\<^sub>2)"
Andreas@57818
   658
definition "x \<squnion> y = (if x = a\<^sub>2 \<or> y = a\<^sub>2 then a\<^sub>2 else a\<^sub>1)"
Andreas@57818
   659
Andreas@57818
   660
lemma neq_finite_2_a\<^sub>1_iff [simp]: "x \<noteq> a\<^sub>1 \<longleftrightarrow> x = a\<^sub>2"
Andreas@57818
   661
by(cases x) simp_all
Andreas@57818
   662
Andreas@57818
   663
lemma neq_finite_2_a\<^sub>1_iff' [simp]: "a\<^sub>1 \<noteq> x \<longleftrightarrow> x = a\<^sub>2"
Andreas@57818
   664
by(cases x) simp_all
Andreas@57818
   665
Andreas@57818
   666
lemma neq_finite_2_a\<^sub>2_iff [simp]: "x \<noteq> a\<^sub>2 \<longleftrightarrow> x = a\<^sub>1"
Andreas@57818
   667
by(cases x) simp_all
Andreas@57818
   668
Andreas@57818
   669
lemma neq_finite_2_a\<^sub>2_iff' [simp]: "a\<^sub>2 \<noteq> x \<longleftrightarrow> x = a\<^sub>1"
Andreas@57818
   670
by(cases x) simp_all
Andreas@57818
   671
Andreas@57818
   672
instance
Andreas@57818
   673
proof
Andreas@57818
   674
  fix x :: finite_2 and A
Andreas@57818
   675
  assume "x \<in> A"
Andreas@57818
   676
  then show "\<Sqinter>A \<le> x" "x \<le> \<Squnion>A"
Andreas@57818
   677
    by(case_tac [!] x)(auto simp add: less_eq_finite_2_def less_finite_2_def Inf_finite_2_def Sup_finite_2_def)
Andreas@57818
   678
qed(auto simp add: less_eq_finite_2_def less_finite_2_def inf_finite_2_def sup_finite_2_def Inf_finite_2_def Sup_finite_2_def)
Andreas@57818
   679
end
Andreas@57818
   680
Andreas@57818
   681
instance finite_2 :: complete_distrib_lattice
haftmann@62343
   682
  by standard (auto simp add: sup_finite_2_def inf_finite_2_def Inf_finite_2_def Sup_finite_2_def)
Andreas@57818
   683
Andreas@57818
   684
instance finite_2 :: complete_linorder ..
Andreas@57818
   685
haftmann@64592
   686
instantiation finite_2 :: "{field, idom_abs_sgn}" begin
Andreas@57922
   687
definition [simp]: "0 = a\<^sub>1"
Andreas@57922
   688
definition [simp]: "1 = a\<^sub>2"
Andreas@57922
   689
definition "x + y = (case (x, y) of (a\<^sub>1, a\<^sub>1) \<Rightarrow> a\<^sub>1 | (a\<^sub>2, a\<^sub>2) \<Rightarrow> a\<^sub>1 | _ \<Rightarrow> a\<^sub>2)"
Andreas@57922
   690
definition "uminus = (\<lambda>x :: finite_2. x)"
Andreas@57922
   691
definition "op - = (op + :: finite_2 \<Rightarrow> _)"
Andreas@57922
   692
definition "x * y = (case (x, y) of (a\<^sub>2, a\<^sub>2) \<Rightarrow> a\<^sub>2 | _ \<Rightarrow> a\<^sub>1)"
Andreas@57922
   693
definition "inverse = (\<lambda>x :: finite_2. x)"
haftmann@60352
   694
definition "divide = (op * :: finite_2 \<Rightarrow> _)"
Andreas@57922
   695
definition "abs = (\<lambda>x :: finite_2. x)"
Andreas@57922
   696
definition "sgn = (\<lambda>x :: finite_2. x)"
Andreas@57922
   697
instance
haftmann@64592
   698
  by standard
haftmann@64592
   699
    (simp_all add: plus_finite_2_def uminus_finite_2_def minus_finite_2_def times_finite_2_def
haftmann@64592
   700
      inverse_finite_2_def divide_finite_2_def abs_finite_2_def sgn_finite_2_def
haftmann@64592
   701
      split: finite_2.splits)
Andreas@57922
   702
end
Andreas@57922
   703
haftmann@58646
   704
lemma two_finite_2 [simp]:
haftmann@58646
   705
  "2 = a\<^sub>1"
haftmann@58646
   706
  by (simp add: numeral.simps plus_finite_2_def)
haftmann@64592
   707
haftmann@64592
   708
lemma dvd_finite_2_unfold:
haftmann@64592
   709
  "x dvd y \<longleftrightarrow> x = a\<^sub>2 \<or> y = a\<^sub>1"
haftmann@64592
   710
  by (auto simp add: dvd_def times_finite_2_def split: finite_2.splits)
haftmann@64592
   711
haftmann@64592
   712
instantiation finite_2 :: "{ring_div, normalization_semidom}" begin
haftmann@64592
   713
definition [simp]: "normalize = (id :: finite_2 \<Rightarrow> _)"
haftmann@64592
   714
definition [simp]: "unit_factor = (id :: finite_2 \<Rightarrow> _)"
haftmann@64592
   715
definition "x mod y = (case (x, y) of (a\<^sub>2, a\<^sub>1) \<Rightarrow> a\<^sub>2 | _ \<Rightarrow> a\<^sub>1)"
haftmann@64592
   716
instance
haftmann@64592
   717
  by standard
haftmann@64592
   718
    (simp_all add: dvd_finite_2_unfold times_finite_2_def
haftmann@64592
   719
      divide_finite_2_def modulo_finite_2_def split: finite_2.splits)
haftmann@64592
   720
end
haftmann@64592
   721
haftmann@64592
   722
 
wenzelm@53015
   723
hide_const (open) a\<^sub>1 a\<^sub>2
bulwahn@40657
   724
wenzelm@58659
   725
datatype (plugins only: code "quickcheck" extraction) finite_3 =
blanchet@58350
   726
  a\<^sub>1 | a\<^sub>2 | a\<^sub>3
bulwahn@40647
   727
wenzelm@53015
   728
notation (output) a\<^sub>1  ("a\<^sub>1")
wenzelm@53015
   729
notation (output) a\<^sub>2  ("a\<^sub>2")
wenzelm@53015
   730
notation (output) a\<^sub>3  ("a\<^sub>3")
bulwahn@40900
   731
haftmann@49950
   732
lemma UNIV_finite_3:
wenzelm@53015
   733
  "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3}"
haftmann@49950
   734
  by (auto intro: finite_3.exhaust)
haftmann@49950
   735
bulwahn@40647
   736
instantiation finite_3 :: enum
bulwahn@40647
   737
begin
bulwahn@40647
   738
bulwahn@40647
   739
definition
wenzelm@53015
   740
  "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3]"
bulwahn@40647
   741
bulwahn@41078
   742
definition
wenzelm@53015
   743
  "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3"
bulwahn@41078
   744
bulwahn@41078
   745
definition
wenzelm@53015
   746
  "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3"
bulwahn@41078
   747
bulwahn@40647
   748
instance proof
haftmann@49950
   749
qed (simp_all only: enum_finite_3_def enum_all_finite_3_def enum_ex_finite_3_def UNIV_finite_3, simp_all)
bulwahn@40647
   750
bulwahn@40647
   751
end
bulwahn@40647
   752
haftmann@64592
   753
lemma finite_3_not_eq_unfold:
haftmann@64592
   754
  "x \<noteq> a\<^sub>1 \<longleftrightarrow> x \<in> {a\<^sub>2, a\<^sub>3}"
haftmann@64592
   755
  "x \<noteq> a\<^sub>2 \<longleftrightarrow> x \<in> {a\<^sub>1, a\<^sub>3}"
haftmann@64592
   756
  "x \<noteq> a\<^sub>3 \<longleftrightarrow> x \<in> {a\<^sub>1, a\<^sub>2}"
haftmann@64592
   757
  by (cases x; simp)+
haftmann@64592
   758
bulwahn@40651
   759
instantiation finite_3 :: linorder
bulwahn@40651
   760
begin
bulwahn@40651
   761
bulwahn@40651
   762
definition less_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   763
where
wenzelm@53015
   764
  "x < y = (case x of a\<^sub>1 \<Rightarrow> y \<noteq> a\<^sub>1 | a\<^sub>2 \<Rightarrow> y = a\<^sub>3 | a\<^sub>3 \<Rightarrow> False)"
bulwahn@40651
   765
bulwahn@40651
   766
definition less_eq_finite_3 :: "finite_3 \<Rightarrow> finite_3 \<Rightarrow> bool"
bulwahn@40651
   767
where
haftmann@49950
   768
  "x \<le> y \<longleftrightarrow> x = y \<or> x < (y :: finite_3)"
bulwahn@40651
   769
bulwahn@40651
   770
instance proof (intro_classes)
bulwahn@40651
   771
qed (auto simp add: less_finite_3_def less_eq_finite_3_def split: finite_3.split_asm)
bulwahn@40651
   772
bulwahn@40651
   773
end
bulwahn@40651
   774
Andreas@57922
   775
instance finite_3 :: wellorder
Andreas@57922
   776
proof(rule wf_wellorderI)
Andreas@57922
   777
  have "inv_image less_than (case_finite_3 0 1 2) = {(x, y). x < y}"
Andreas@57922
   778
    by(auto simp add: less_finite_3_def split: finite_3.splits)
Andreas@57922
   779
  from this[symmetric] show "wf \<dots>" by simp
Andreas@57922
   780
qed intro_classes
Andreas@57922
   781
Andreas@57818
   782
instantiation finite_3 :: complete_lattice
Andreas@57818
   783
begin
Andreas@57818
   784
Andreas@57818
   785
definition "\<Sqinter>A = (if a\<^sub>1 \<in> A then a\<^sub>1 else if a\<^sub>2 \<in> A then a\<^sub>2 else a\<^sub>3)"
Andreas@57818
   786
definition "\<Squnion>A = (if a\<^sub>3 \<in> A then a\<^sub>3 else if a\<^sub>2 \<in> A then a\<^sub>2 else a\<^sub>1)"
Andreas@57818
   787
definition [simp]: "bot = a\<^sub>1"
Andreas@57818
   788
definition [simp]: "top = a\<^sub>3"
Andreas@57818
   789
definition [simp]: "inf = (min :: finite_3 \<Rightarrow> _)"
Andreas@57818
   790
definition [simp]: "sup = (max :: finite_3 \<Rightarrow> _)"
Andreas@57818
   791
Andreas@57818
   792
instance
Andreas@57818
   793
proof
Andreas@57818
   794
  fix x :: finite_3 and A
Andreas@57818
   795
  assume "x \<in> A"
Andreas@57818
   796
  then show "\<Sqinter>A \<le> x" "x \<le> \<Squnion>A"
Andreas@57818
   797
    by(case_tac [!] x)(auto simp add: Inf_finite_3_def Sup_finite_3_def less_eq_finite_3_def less_finite_3_def)
Andreas@57818
   798
next
Andreas@57818
   799
  fix A and z :: finite_3
Andreas@57818
   800
  assume "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
Andreas@57818
   801
  then show "z \<le> \<Sqinter>A"
Andreas@57818
   802
    by(cases z)(auto simp add: Inf_finite_3_def less_eq_finite_3_def less_finite_3_def)
Andreas@57818
   803
next
Andreas@57818
   804
  fix A and z :: finite_3
Andreas@57818
   805
  assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
Andreas@57818
   806
  show "\<Squnion>A \<le> z"
Andreas@57818
   807
    by(auto simp add: Sup_finite_3_def less_eq_finite_3_def less_finite_3_def dest: *)
Andreas@57818
   808
qed(auto simp add: Inf_finite_3_def Sup_finite_3_def)
Andreas@57818
   809
end
Andreas@57818
   810
Andreas@57818
   811
instance finite_3 :: complete_distrib_lattice
Andreas@57818
   812
proof
Andreas@57818
   813
  fix a :: finite_3 and B
Andreas@57818
   814
  show "a \<squnion> \<Sqinter>B = (\<Sqinter>b\<in>B. a \<squnion> b)"
Andreas@57818
   815
  proof(cases a "\<Sqinter>B" rule: finite_3.exhaust[case_product finite_3.exhaust])
Andreas@57818
   816
    case a\<^sub>2_a\<^sub>3
Andreas@57818
   817
    then have "\<And>x. x \<in> B \<Longrightarrow> x = a\<^sub>3"
nipkow@62390
   818
      by(case_tac x)(auto simp add: Inf_finite_3_def split: if_split_asm)
Andreas@57818
   819
    then show ?thesis using a\<^sub>2_a\<^sub>3
nipkow@62390
   820
      by(auto simp add: Inf_finite_3_def max_def less_eq_finite_3_def less_finite_3_def split: if_split_asm)
nipkow@62390
   821
  qed (auto simp add: Inf_finite_3_def max_def less_finite_3_def less_eq_finite_3_def split: if_split_asm)
Andreas@57818
   822
  show "a \<sqinter> \<Squnion>B = (\<Squnion>b\<in>B. a \<sqinter> b)"
haftmann@62343
   823
    by (cases a "\<Squnion>B" rule: finite_3.exhaust[case_product finite_3.exhaust])
nipkow@62390
   824
      (auto simp add: Sup_finite_3_def min_def less_finite_3_def less_eq_finite_3_def split: if_split_asm)
Andreas@57818
   825
qed
Andreas@57818
   826
Andreas@57818
   827
instance finite_3 :: complete_linorder ..
Andreas@57818
   828
haftmann@64592
   829
instantiation finite_3 :: "{field, idom_abs_sgn}" begin
Andreas@57922
   830
definition [simp]: "0 = a\<^sub>1"
Andreas@57922
   831
definition [simp]: "1 = a\<^sub>2"
Andreas@57922
   832
definition
Andreas@57922
   833
  "x + y = (case (x, y) of
Andreas@57922
   834
     (a\<^sub>1, a\<^sub>1) \<Rightarrow> a\<^sub>1 | (a\<^sub>2, a\<^sub>3) \<Rightarrow> a\<^sub>1 | (a\<^sub>3, a\<^sub>2) \<Rightarrow> a\<^sub>1
Andreas@57922
   835
   | (a\<^sub>1, a\<^sub>2) \<Rightarrow> a\<^sub>2 | (a\<^sub>2, a\<^sub>1) \<Rightarrow> a\<^sub>2 | (a\<^sub>3, a\<^sub>3) \<Rightarrow> a\<^sub>2
Andreas@57922
   836
   | _ \<Rightarrow> a\<^sub>3)"
Andreas@57922
   837
definition "- x = (case x of a\<^sub>1 \<Rightarrow> a\<^sub>1 | a\<^sub>2 \<Rightarrow> a\<^sub>3 | a\<^sub>3 \<Rightarrow> a\<^sub>2)"
Andreas@57922
   838
definition "x - y = x + (- y :: finite_3)"
Andreas@57922
   839
definition "x * y = (case (x, y) of (a\<^sub>2, a\<^sub>2) \<Rightarrow> a\<^sub>2 | (a\<^sub>3, a\<^sub>3) \<Rightarrow> a\<^sub>2 | (a\<^sub>2, a\<^sub>3) \<Rightarrow> a\<^sub>3 | (a\<^sub>3, a\<^sub>2) \<Rightarrow> a\<^sub>3 | _ \<Rightarrow> a\<^sub>1)"
Andreas@57922
   840
definition "inverse = (\<lambda>x :: finite_3. x)" 
haftmann@60429
   841
definition "x div y = x * inverse (y :: finite_3)"
haftmann@64290
   842
definition "abs = (\<lambda>x. case x of a\<^sub>3 \<Rightarrow> a\<^sub>2 | _ \<Rightarrow> x)"
haftmann@64290
   843
definition "sgn = (\<lambda>x :: finite_3. x)"
Andreas@57922
   844
instance
haftmann@64592
   845
  by standard
haftmann@64592
   846
    (simp_all add: plus_finite_3_def uminus_finite_3_def minus_finite_3_def times_finite_3_def
haftmann@64592
   847
      inverse_finite_3_def divide_finite_3_def abs_finite_3_def sgn_finite_3_def
haftmann@64592
   848
      less_finite_3_def
haftmann@64592
   849
      split: finite_3.splits)
haftmann@64592
   850
end
haftmann@64592
   851
haftmann@64592
   852
lemma two_finite_3 [simp]:
haftmann@64592
   853
  "2 = a\<^sub>3"
haftmann@64592
   854
  by (simp add: numeral.simps plus_finite_3_def)
haftmann@64592
   855
haftmann@64592
   856
lemma dvd_finite_3_unfold:
haftmann@64592
   857
  "x dvd y \<longleftrightarrow> x = a\<^sub>2 \<or> x = a\<^sub>3 \<or> y = a\<^sub>1"
haftmann@64592
   858
  by (cases x) (auto simp add: dvd_def times_finite_3_def split: finite_3.splits)
haftmann@64592
   859
haftmann@64592
   860
instantiation finite_3 :: "{ring_div, normalization_semidom}" begin
haftmann@64592
   861
definition "normalize x = (case x of a\<^sub>3 \<Rightarrow> a\<^sub>2 | _ \<Rightarrow> x)"
haftmann@64592
   862
definition [simp]: "unit_factor = (id :: finite_3 \<Rightarrow> _)"
haftmann@64592
   863
definition "x mod y = (case (x, y) of (a\<^sub>2, a\<^sub>1) \<Rightarrow> a\<^sub>2 | (a\<^sub>3, a\<^sub>1) \<Rightarrow> a\<^sub>3 | _ \<Rightarrow> a\<^sub>1)"
haftmann@64592
   864
instance
haftmann@64592
   865
  by standard
haftmann@64592
   866
    (auto simp add: finite_3_not_eq_unfold plus_finite_3_def
haftmann@64592
   867
      dvd_finite_3_unfold times_finite_3_def inverse_finite_3_def
haftmann@64592
   868
      normalize_finite_3_def divide_finite_3_def modulo_finite_3_def
haftmann@64592
   869
      split: finite_3.splits)
Andreas@57922
   870
end
Andreas@57922
   871
haftmann@58646
   872
haftmann@58646
   873
wenzelm@53015
   874
hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3
bulwahn@40657
   875
wenzelm@58659
   876
datatype (plugins only: code "quickcheck" extraction) finite_4 =
blanchet@58350
   877
  a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4
bulwahn@40647
   878
wenzelm@53015
   879
notation (output) a\<^sub>1  ("a\<^sub>1")
wenzelm@53015
   880
notation (output) a\<^sub>2  ("a\<^sub>2")
wenzelm@53015
   881
notation (output) a\<^sub>3  ("a\<^sub>3")
wenzelm@53015
   882
notation (output) a\<^sub>4  ("a\<^sub>4")
bulwahn@40900
   883
haftmann@49950
   884
lemma UNIV_finite_4:
wenzelm@53015
   885
  "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4}"
haftmann@49950
   886
  by (auto intro: finite_4.exhaust)
haftmann@49950
   887
bulwahn@40647
   888
instantiation finite_4 :: enum
bulwahn@40647
   889
begin
bulwahn@40647
   890
bulwahn@40647
   891
definition
wenzelm@53015
   892
  "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4]"
bulwahn@40647
   893
bulwahn@41078
   894
definition
wenzelm@53015
   895
  "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3 \<and> P a\<^sub>4"
bulwahn@41078
   896
bulwahn@41078
   897
definition
wenzelm@53015
   898
  "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3 \<or> P a\<^sub>4"
bulwahn@41078
   899
bulwahn@40647
   900
instance proof
haftmann@49950
   901
qed (simp_all only: enum_finite_4_def enum_all_finite_4_def enum_ex_finite_4_def UNIV_finite_4, simp_all)
bulwahn@40647
   902
bulwahn@40647
   903
end
bulwahn@40647
   904
Andreas@57818
   905
instantiation finite_4 :: complete_lattice begin
Andreas@57818
   906
wenzelm@60758
   907
text \<open>@{term a\<^sub>1} $<$ @{term a\<^sub>2},@{term a\<^sub>3} $<$ @{term a\<^sub>4},
wenzelm@60758
   908
  but @{term a\<^sub>2} and @{term a\<^sub>3} are incomparable.\<close>
Andreas@57818
   909
Andreas@57818
   910
definition
Andreas@57818
   911
  "x < y \<longleftrightarrow> (case (x, y) of
Andreas@57818
   912
     (a\<^sub>1, a\<^sub>1) \<Rightarrow> False | (a\<^sub>1, _) \<Rightarrow> True
Andreas@57818
   913
   |  (a\<^sub>2, a\<^sub>4) \<Rightarrow> True
Andreas@57818
   914
   |  (a\<^sub>3, a\<^sub>4) \<Rightarrow> True  | _ \<Rightarrow> False)"
Andreas@57818
   915
Andreas@57818
   916
definition 
Andreas@57818
   917
  "x \<le> y \<longleftrightarrow> (case (x, y) of
Andreas@57818
   918
     (a\<^sub>1, _) \<Rightarrow> True
Andreas@57818
   919
   | (a\<^sub>2, a\<^sub>2) \<Rightarrow> True | (a\<^sub>2, a\<^sub>4) \<Rightarrow> True
Andreas@57818
   920
   | (a\<^sub>3, a\<^sub>3) \<Rightarrow> True | (a\<^sub>3, a\<^sub>4) \<Rightarrow> True
Andreas@57818
   921
   | (a\<^sub>4, a\<^sub>4) \<Rightarrow> True | _ \<Rightarrow> False)"
Andreas@57818
   922
Andreas@57818
   923
definition
Andreas@57818
   924
  "\<Sqinter>A = (if a\<^sub>1 \<in> A \<or> a\<^sub>2 \<in> A \<and> a\<^sub>3 \<in> A then a\<^sub>1 else if a\<^sub>2 \<in> A then a\<^sub>2 else if a\<^sub>3 \<in> A then a\<^sub>3 else a\<^sub>4)"
Andreas@57818
   925
definition
Andreas@57818
   926
  "\<Squnion>A = (if a\<^sub>4 \<in> A \<or> a\<^sub>2 \<in> A \<and> a\<^sub>3 \<in> A then a\<^sub>4 else if a\<^sub>2 \<in> A then a\<^sub>2 else if a\<^sub>3 \<in> A then a\<^sub>3 else a\<^sub>1)"
Andreas@57818
   927
definition [simp]: "bot = a\<^sub>1"
Andreas@57818
   928
definition [simp]: "top = a\<^sub>4"
Andreas@57818
   929
definition
Andreas@57818
   930
  "x \<sqinter> y = (case (x, y) of
Andreas@57818
   931
     (a\<^sub>1, _) \<Rightarrow> a\<^sub>1 | (_, a\<^sub>1) \<Rightarrow> a\<^sub>1 | (a\<^sub>2, a\<^sub>3) \<Rightarrow> a\<^sub>1 | (a\<^sub>3, a\<^sub>2) \<Rightarrow> a\<^sub>1
Andreas@57818
   932
   | (a\<^sub>2, _) \<Rightarrow> a\<^sub>2 | (_, a\<^sub>2) \<Rightarrow> a\<^sub>2
Andreas@57818
   933
   | (a\<^sub>3, _) \<Rightarrow> a\<^sub>3 | (_, a\<^sub>3) \<Rightarrow> a\<^sub>3
Andreas@57818
   934
   | _ \<Rightarrow> a\<^sub>4)"
Andreas@57818
   935
definition
Andreas@57818
   936
  "x \<squnion> y = (case (x, y) of
Andreas@57818
   937
     (a\<^sub>4, _) \<Rightarrow> a\<^sub>4 | (_, a\<^sub>4) \<Rightarrow> a\<^sub>4 | (a\<^sub>2, a\<^sub>3) \<Rightarrow> a\<^sub>4 | (a\<^sub>3, a\<^sub>2) \<Rightarrow> a\<^sub>4
Andreas@57818
   938
  | (a\<^sub>2, _) \<Rightarrow> a\<^sub>2 | (_, a\<^sub>2) \<Rightarrow> a\<^sub>2
Andreas@57818
   939
  | (a\<^sub>3, _) \<Rightarrow> a\<^sub>3 | (_, a\<^sub>3) \<Rightarrow> a\<^sub>3
Andreas@57818
   940
  | _ \<Rightarrow> a\<^sub>1)"
Andreas@57818
   941
Andreas@57818
   942
instance
Andreas@57818
   943
proof
Andreas@57818
   944
  fix A and z :: finite_4
Andreas@57818
   945
  assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
Andreas@57818
   946
  show "\<Squnion>A \<le> z"
Andreas@57818
   947
    by(auto simp add: Sup_finite_4_def less_eq_finite_4_def dest!: * split: finite_4.splits)
Andreas@57818
   948
next
Andreas@57818
   949
  fix A and z :: finite_4
Andreas@57818
   950
  assume *: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
Andreas@57818
   951
  show "z \<le> \<Sqinter>A"
Andreas@57818
   952
    by(auto simp add: Inf_finite_4_def less_eq_finite_4_def dest!: * split: finite_4.splits)
Andreas@57818
   953
qed(auto simp add: less_finite_4_def less_eq_finite_4_def Inf_finite_4_def Sup_finite_4_def inf_finite_4_def sup_finite_4_def split: finite_4.splits)
Andreas@57818
   954
Andreas@57818
   955
end
Andreas@57818
   956
Andreas@57818
   957
instance finite_4 :: complete_distrib_lattice
Andreas@57818
   958
proof
Andreas@57818
   959
  fix a :: finite_4 and B
Andreas@57818
   960
  show "a \<squnion> \<Sqinter>B = (\<Sqinter>b\<in>B. a \<squnion> b)"
Andreas@57818
   961
    by(cases a "\<Sqinter>B" rule: finite_4.exhaust[case_product finite_4.exhaust])
nipkow@62390
   962
      (auto simp add: sup_finite_4_def Inf_finite_4_def split: finite_4.splits if_split_asm)
Andreas@57818
   963
  show "a \<sqinter> \<Squnion>B = (\<Squnion>b\<in>B. a \<sqinter> b)"
Andreas@57818
   964
    by(cases a "\<Squnion>B" rule: finite_4.exhaust[case_product finite_4.exhaust])
nipkow@62390
   965
      (auto simp add: inf_finite_4_def Sup_finite_4_def split: finite_4.splits if_split_asm)
Andreas@57818
   966
qed
Andreas@57818
   967
Andreas@57922
   968
instantiation finite_4 :: complete_boolean_algebra begin
Andreas@57922
   969
definition "- x = (case x of a\<^sub>1 \<Rightarrow> a\<^sub>4 | a\<^sub>2 \<Rightarrow> a\<^sub>3 | a\<^sub>3 \<Rightarrow> a\<^sub>2 | a\<^sub>4 \<Rightarrow> a\<^sub>1)"
Andreas@57922
   970
definition "x - y = x \<sqinter> - (y :: finite_4)"
Andreas@57922
   971
instance
Andreas@57922
   972
by intro_classes
Andreas@57922
   973
  (simp_all add: inf_finite_4_def sup_finite_4_def uminus_finite_4_def minus_finite_4_def split: finite_4.splits)
Andreas@57922
   974
end
Andreas@57922
   975
wenzelm@53015
   976
hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4
bulwahn@40651
   977
wenzelm@58659
   978
datatype (plugins only: code "quickcheck" extraction) finite_5 =
blanchet@58350
   979
  a\<^sub>1 | a\<^sub>2 | a\<^sub>3 | a\<^sub>4 | a\<^sub>5
bulwahn@40647
   980
wenzelm@53015
   981
notation (output) a\<^sub>1  ("a\<^sub>1")
wenzelm@53015
   982
notation (output) a\<^sub>2  ("a\<^sub>2")
wenzelm@53015
   983
notation (output) a\<^sub>3  ("a\<^sub>3")
wenzelm@53015
   984
notation (output) a\<^sub>4  ("a\<^sub>4")
wenzelm@53015
   985
notation (output) a\<^sub>5  ("a\<^sub>5")
bulwahn@40900
   986
haftmann@49950
   987
lemma UNIV_finite_5:
wenzelm@53015
   988
  "UNIV = {a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5}"
haftmann@49950
   989
  by (auto intro: finite_5.exhaust)
haftmann@49950
   990
bulwahn@40647
   991
instantiation finite_5 :: enum
bulwahn@40647
   992
begin
bulwahn@40647
   993
bulwahn@40647
   994
definition
wenzelm@53015
   995
  "enum = [a\<^sub>1, a\<^sub>2, a\<^sub>3, a\<^sub>4, a\<^sub>5]"
bulwahn@40647
   996
bulwahn@41078
   997
definition
wenzelm@53015
   998
  "enum_all P \<longleftrightarrow> P a\<^sub>1 \<and> P a\<^sub>2 \<and> P a\<^sub>3 \<and> P a\<^sub>4 \<and> P a\<^sub>5"
bulwahn@41078
   999
bulwahn@41078
  1000
definition
wenzelm@53015
  1001
  "enum_ex P \<longleftrightarrow> P a\<^sub>1 \<or> P a\<^sub>2 \<or> P a\<^sub>3 \<or> P a\<^sub>4 \<or> P a\<^sub>5"
bulwahn@41078
  1002
bulwahn@40647
  1003
instance proof
haftmann@49950
  1004
qed (simp_all only: enum_finite_5_def enum_all_finite_5_def enum_ex_finite_5_def UNIV_finite_5, simp_all)
bulwahn@40647
  1005
bulwahn@40647
  1006
end
bulwahn@40647
  1007
Andreas@57818
  1008
instantiation finite_5 :: complete_lattice
Andreas@57818
  1009
begin
Andreas@57818
  1010
wenzelm@60758
  1011
text \<open>The non-distributive pentagon lattice $N_5$\<close>
Andreas@57818
  1012
Andreas@57818
  1013
definition
Andreas@57818
  1014
  "x < y \<longleftrightarrow> (case (x, y) of
Andreas@57818
  1015
     (a\<^sub>1, a\<^sub>1) \<Rightarrow> False | (a\<^sub>1, _) \<Rightarrow> True
Andreas@57818
  1016
   | (a\<^sub>2, a\<^sub>3) \<Rightarrow> True  | (a\<^sub>2, a\<^sub>5) \<Rightarrow> True
Andreas@57818
  1017
   | (a\<^sub>3, a\<^sub>5) \<Rightarrow> True
Andreas@57818
  1018
   | (a\<^sub>4, a\<^sub>5) \<Rightarrow> True  | _ \<Rightarrow> False)"
Andreas@57818
  1019
Andreas@57818
  1020
definition
Andreas@57818
  1021
  "x \<le> y \<longleftrightarrow> (case (x, y) of
Andreas@57818
  1022
     (a\<^sub>1, _) \<Rightarrow> True
Andreas@57818
  1023
   | (a\<^sub>2, a\<^sub>2) \<Rightarrow> True | (a\<^sub>2, a\<^sub>3) \<Rightarrow> True | (a\<^sub>2, a\<^sub>5) \<Rightarrow> True
Andreas@57818
  1024
   | (a\<^sub>3, a\<^sub>3) \<Rightarrow> True | (a\<^sub>3, a\<^sub>5) \<Rightarrow> True
Andreas@57818
  1025
   | (a\<^sub>4, a\<^sub>4) \<Rightarrow> True | (a\<^sub>4, a\<^sub>5) \<Rightarrow> True
Andreas@57818
  1026
   | (a\<^sub>5, a\<^sub>5) \<Rightarrow> True | _ \<Rightarrow> False)"
Andreas@57818
  1027
Andreas@57818
  1028
definition
Andreas@57818
  1029
  "\<Sqinter>A = 
Andreas@57818
  1030
  (if a\<^sub>1 \<in> A \<or> a\<^sub>4 \<in> A \<and> (a\<^sub>2 \<in> A \<or> a\<^sub>3 \<in> A) then a\<^sub>1
Andreas@57818
  1031
   else if a\<^sub>2 \<in> A then a\<^sub>2
Andreas@57818
  1032
   else if a\<^sub>3 \<in> A then a\<^sub>3
Andreas@57818
  1033
   else if a\<^sub>4 \<in> A then a\<^sub>4
Andreas@57818
  1034
   else a\<^sub>5)"
Andreas@57818
  1035
definition
Andreas@57818
  1036
  "\<Squnion>A = 
Andreas@57818
  1037
  (if a\<^sub>5 \<in> A \<or> a\<^sub>4 \<in> A \<and> (a\<^sub>2 \<in> A \<or> a\<^sub>3 \<in> A) then a\<^sub>5
Andreas@57818
  1038
   else if a\<^sub>3 \<in> A then a\<^sub>3
Andreas@57818
  1039
   else if a\<^sub>2 \<in> A then a\<^sub>2
Andreas@57818
  1040
   else if a\<^sub>4 \<in> A then a\<^sub>4
Andreas@57818
  1041
   else a\<^sub>1)"
Andreas@57818
  1042
definition [simp]: "bot = a\<^sub>1"
Andreas@57818
  1043
definition [simp]: "top = a\<^sub>5"
Andreas@57818
  1044
definition
Andreas@57818
  1045
  "x \<sqinter> y = (case (x, y) of
Andreas@57818
  1046
     (a\<^sub>1, _) \<Rightarrow> a\<^sub>1 | (_, a\<^sub>1) \<Rightarrow> a\<^sub>1 | (a\<^sub>2, a\<^sub>4) \<Rightarrow> a\<^sub>1 | (a\<^sub>4, a\<^sub>2) \<Rightarrow> a\<^sub>1 | (a\<^sub>3, a\<^sub>4) \<Rightarrow> a\<^sub>1 | (a\<^sub>4, a\<^sub>3) \<Rightarrow> a\<^sub>1
Andreas@57818
  1047
   | (a\<^sub>2, _) \<Rightarrow> a\<^sub>2 | (_, a\<^sub>2) \<Rightarrow> a\<^sub>2
Andreas@57818
  1048
   | (a\<^sub>3, _) \<Rightarrow> a\<^sub>3 | (_, a\<^sub>3) \<Rightarrow> a\<^sub>3
Andreas@57818
  1049
   | (a\<^sub>4, _) \<Rightarrow> a\<^sub>4 | (_, a\<^sub>4) \<Rightarrow> a\<^sub>4
Andreas@57818
  1050
   | _ \<Rightarrow> a\<^sub>5)"
Andreas@57818
  1051
definition
Andreas@57818
  1052
  "x \<squnion> y = (case (x, y) of
Andreas@57818
  1053
     (a\<^sub>5, _) \<Rightarrow> a\<^sub>5 | (_, a\<^sub>5) \<Rightarrow> a\<^sub>5 | (a\<^sub>2, a\<^sub>4) \<Rightarrow> a\<^sub>5 | (a\<^sub>4, a\<^sub>2) \<Rightarrow> a\<^sub>5 | (a\<^sub>3, a\<^sub>4) \<Rightarrow> a\<^sub>5 | (a\<^sub>4, a\<^sub>3) \<Rightarrow> a\<^sub>5
Andreas@57818
  1054
   | (a\<^sub>3, _) \<Rightarrow> a\<^sub>3 | (_, a\<^sub>3) \<Rightarrow> a\<^sub>3
Andreas@57818
  1055
   | (a\<^sub>2, _) \<Rightarrow> a\<^sub>2 | (_, a\<^sub>2) \<Rightarrow> a\<^sub>2
Andreas@57818
  1056
   | (a\<^sub>4, _) \<Rightarrow> a\<^sub>4 | (_, a\<^sub>4) \<Rightarrow> a\<^sub>4
Andreas@57818
  1057
   | _ \<Rightarrow> a\<^sub>1)"
Andreas@57818
  1058
Andreas@57818
  1059
instance 
Andreas@57818
  1060
proof intro_classes
Andreas@57818
  1061
  fix A and z :: finite_5
Andreas@57818
  1062
  assume *: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
Andreas@57818
  1063
  show "z \<le> \<Sqinter>A"
nipkow@62390
  1064
    by(auto simp add: less_eq_finite_5_def Inf_finite_5_def split: finite_5.splits if_split_asm dest!: *)
Andreas@57818
  1065
next
Andreas@57818
  1066
  fix A and z :: finite_5
Andreas@57818
  1067
  assume *: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
Andreas@57818
  1068
  show "\<Squnion>A \<le> z"
nipkow@62390
  1069
    by(auto simp add: less_eq_finite_5_def Sup_finite_5_def split: finite_5.splits if_split_asm dest!: *)
nipkow@62390
  1070
qed(auto simp add: less_eq_finite_5_def less_finite_5_def inf_finite_5_def sup_finite_5_def Inf_finite_5_def Sup_finite_5_def split: finite_5.splits if_split_asm)
Andreas@57818
  1071
Andreas@57818
  1072
end
Andreas@57818
  1073
wenzelm@53015
  1074
hide_const (open) a\<^sub>1 a\<^sub>2 a\<^sub>3 a\<^sub>4 a\<^sub>5
bulwahn@46352
  1075
haftmann@49948
  1076
wenzelm@60758
  1077
subsection \<open>Closing up\<close>
bulwahn@40657
  1078
bulwahn@41085
  1079
hide_type (open) finite_1 finite_2 finite_3 finite_4 finite_5
haftmann@49948
  1080
hide_const (open) enum enum_all enum_ex all_n_lists ex_n_lists ntrancl
bulwahn@40647
  1081
bulwahn@40647
  1082
end