src/HOL/Library/Cardinality.thy
author haftmann
Fri Mar 22 19:18:08 2019 +0000 (4 months ago)
changeset 69946 494934c30f38
parent 69663 41ff40bf1530
permissions -rw-r--r--
improved code equations taken over from AFP
haftmann@37653
     1
(*  Title:      HOL/Library/Cardinality.thy
Andreas@48051
     2
    Author:     Brian Huffman, Andreas Lochbihler
kleing@24332
     3
*)
kleing@24332
     4
wenzelm@60500
     5
section \<open>Cardinality of types\<close>
kleing@24332
     6
haftmann@37653
     7
theory Cardinality
Andreas@48164
     8
imports Phantom_Type
kleing@24332
     9
begin
kleing@24332
    10
wenzelm@60500
    11
subsection \<open>Preliminary lemmas\<close>
kleing@24332
    12
(* These should be moved elsewhere *)
kleing@24332
    13
kleing@24332
    14
lemma (in type_definition) univ:
kleing@24332
    15
  "UNIV = Abs ` A"
kleing@24332
    16
proof
kleing@24332
    17
  show "Abs ` A \<subseteq> UNIV" by (rule subset_UNIV)
kleing@24332
    18
  show "UNIV \<subseteq> Abs ` A"
kleing@24332
    19
  proof
kleing@24332
    20
    fix x :: 'b
kleing@24332
    21
    have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
kleing@24332
    22
    moreover have "Rep x \<in> A" by (rule Rep)
kleing@24332
    23
    ultimately show "x \<in> Abs ` A" by (rule image_eqI)
kleing@24332
    24
  qed
kleing@24332
    25
qed
kleing@24332
    26
kleing@24332
    27
lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
kleing@24332
    28
  by (simp add: univ card_image inj_on_def Abs_inject)
kleing@24332
    29
kleing@24332
    30
wenzelm@60500
    31
subsection \<open>Cardinalities of types\<close>
kleing@24332
    32
kleing@24332
    33
syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
kleing@24332
    34
wenzelm@61076
    35
translations "CARD('t)" => "CONST card (CONST UNIV :: 't set)"
kleing@24332
    36
wenzelm@60500
    37
print_translation \<open>
wenzelm@42247
    38
  let
wenzelm@69593
    39
    fun card_univ_tr' ctxt [Const (\<^const_syntax>\<open>UNIV\<close>, Type (_, [T]))] =
wenzelm@69593
    40
      Syntax.const \<^syntax_const>\<open>_type_card\<close> $ Syntax_Phases.term_of_typ ctxt T
wenzelm@69593
    41
  in [(\<^const_syntax>\<open>card\<close>, card_univ_tr')] end
wenzelm@60500
    42
\<close>
huffman@24407
    43
Andreas@48058
    44
lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a) * CARD('b)"
haftmann@26153
    45
  unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
kleing@24332
    46
Andreas@48060
    47
lemma card_UNIV_sum: "CARD('a + 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 then CARD('a) + CARD('b) else 0)"
Andreas@48060
    48
unfolding UNIV_Plus_UNIV[symmetric]
Andreas@48060
    49
by(auto simp add: card_eq_0_iff card_Plus simp del: UNIV_Plus_UNIV)
Andreas@48060
    50
huffman@30001
    51
lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
Andreas@48060
    52
by(simp add: card_UNIV_sum)
Andreas@48060
    53
Andreas@48060
    54
lemma card_UNIV_option: "CARD('a option) = (if CARD('a) = 0 then 0 else CARD('a) + 1)"
Andreas@48060
    55
proof -
Andreas@48060
    56
  have "(None :: 'a option) \<notin> range Some" by clarsimp
Andreas@48060
    57
  thus ?thesis
wenzelm@53191
    58
    by (simp add: UNIV_option_conv card_eq_0_iff finite_range_Some card_image)
Andreas@48060
    59
qed
kleing@24332
    60
huffman@30001
    61
lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
Andreas@48060
    62
by(simp add: card_UNIV_option)
Andreas@48060
    63
Andreas@48060
    64
lemma card_UNIV_set: "CARD('a set) = (if CARD('a) = 0 then 0 else 2 ^ CARD('a))"
nipkow@68406
    65
by(simp add: card_eq_0_iff card_Pow flip: Pow_UNIV)
kleing@24332
    66
huffman@30001
    67
lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
Andreas@48060
    68
by(simp add: card_UNIV_set)
kleing@24332
    69
huffman@30001
    70
lemma card_nat [simp]: "CARD(nat) = 0"
huffman@44142
    71
  by (simp add: card_eq_0_iff)
huffman@30001
    72
Andreas@48060
    73
lemma card_fun: "CARD('a \<Rightarrow> 'b) = (if CARD('a) \<noteq> 0 \<and> CARD('b) \<noteq> 0 \<or> CARD('b) = 1 then CARD('b) ^ CARD('a) else 0)"
Andreas@48060
    74
proof -
Andreas@48060
    75
  {  assume "0 < CARD('a)" and "0 < CARD('b)"
Andreas@48060
    76
    hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
Andreas@48060
    77
      by(simp_all only: card_ge_0_finite)
Andreas@48060
    78
    from finite_distinct_list[OF finb] obtain bs 
Andreas@48060
    79
      where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
Andreas@48060
    80
    from finite_distinct_list[OF fina] obtain as
Andreas@48060
    81
      where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
Andreas@48060
    82
    have cb: "CARD('b) = length bs"
Andreas@48060
    83
      unfolding bs[symmetric] distinct_card[OF distb] ..
Andreas@48060
    84
    have ca: "CARD('a) = length as"
Andreas@48060
    85
      unfolding as[symmetric] distinct_card[OF dista] ..
wenzelm@67091
    86
    let ?xs = "map (\<lambda>ys. the \<circ> map_of (zip as ys)) (List.n_lists (length as) bs)"
Andreas@48060
    87
    have "UNIV = set ?xs"
Andreas@48060
    88
    proof(rule UNIV_eq_I)
Andreas@48060
    89
      fix f :: "'a \<Rightarrow> 'b"
Andreas@48060
    90
      from as have "f = the \<circ> map_of (zip as (map f as))"
Andreas@48060
    91
        by(auto simp add: map_of_zip_map)
Andreas@48060
    92
      thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
Andreas@48060
    93
    qed
Andreas@48060
    94
    moreover have "distinct ?xs" unfolding distinct_map
Andreas@48060
    95
    proof(intro conjI distinct_n_lists distb inj_onI)
Andreas@48060
    96
      fix xs ys :: "'b list"
haftmann@49948
    97
      assume xs: "xs \<in> set (List.n_lists (length as) bs)"
haftmann@49948
    98
        and ys: "ys \<in> set (List.n_lists (length as) bs)"
Andreas@48060
    99
        and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
Andreas@48060
   100
      from xs ys have [simp]: "length xs = length as" "length ys = length as"
Andreas@48060
   101
        by(simp_all add: length_n_lists_elem)
Andreas@48060
   102
      have "map_of (zip as xs) = map_of (zip as ys)"
Andreas@48060
   103
      proof
Andreas@48060
   104
        fix x
Andreas@48060
   105
        from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y"
Andreas@48060
   106
          by(simp_all add: map_of_zip_is_Some[symmetric])
Andreas@48060
   107
        with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
Andreas@48060
   108
          by(auto dest: fun_cong[where x=x])
Andreas@48060
   109
      qed
Andreas@48060
   110
      with dista show "xs = ys" by(simp add: map_of_zip_inject)
Andreas@48060
   111
    qed
Andreas@48060
   112
    hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
Andreas@48060
   113
    moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
Andreas@48060
   114
    ultimately have "CARD('a \<Rightarrow> 'b) = CARD('b) ^ CARD('a)" using cb ca by simp }
Andreas@48060
   115
  moreover {
Andreas@48060
   116
    assume cb: "CARD('b) = 1"
Andreas@48060
   117
    then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
Andreas@48060
   118
    have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
Andreas@48060
   119
    proof(rule UNIV_eq_I)
Andreas@48060
   120
      fix x :: "'a \<Rightarrow> 'b"
Andreas@48060
   121
      { fix y
Andreas@48060
   122
        have "x y \<in> UNIV" ..
Andreas@48060
   123
        hence "x y = b" unfolding b by simp }
Andreas@48060
   124
      thus "x \<in> {\<lambda>x. b}" by(auto)
Andreas@48060
   125
    qed
Andreas@48060
   126
    have "CARD('a \<Rightarrow> 'b) = 1" unfolding eq by simp }
Andreas@48060
   127
  ultimately show ?thesis
Andreas@48060
   128
    by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
Andreas@48060
   129
qed
Andreas@48060
   130
Andreas@48176
   131
corollary finite_UNIV_fun:
Andreas@48176
   132
  "finite (UNIV :: ('a \<Rightarrow> 'b) set) \<longleftrightarrow>
Andreas@48176
   133
   finite (UNIV :: 'a set) \<and> finite (UNIV :: 'b set) \<or> CARD('b) = 1"
Andreas@48176
   134
  (is "?lhs \<longleftrightarrow> ?rhs")
Andreas@48176
   135
proof -
Andreas@48176
   136
  have "?lhs \<longleftrightarrow> CARD('a \<Rightarrow> 'b) > 0" by(simp add: card_gt_0_iff)
Andreas@48176
   137
  also have "\<dots> \<longleftrightarrow> CARD('a) > 0 \<and> CARD('b) > 0 \<or> CARD('b) = 1"
Andreas@48176
   138
    by(simp add: card_fun)
Andreas@48176
   139
  also have "\<dots> = ?rhs" by(simp add: card_gt_0_iff)
Andreas@48176
   140
  finally show ?thesis .
Andreas@48176
   141
qed
Andreas@48176
   142
Andreas@48060
   143
lemma card_literal: "CARD(String.literal) = 0"
Andreas@48176
   144
by(simp add: card_eq_0_iff infinite_literal)
huffman@30001
   145
wenzelm@60500
   146
subsection \<open>Classes with at least 1 and 2\<close>
huffman@30001
   147
wenzelm@60500
   148
text \<open>Class finite already captures "at least 1"\<close>
huffman@30001
   149
huffman@30001
   150
lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
huffman@29997
   151
  unfolding neq0_conv [symmetric] by simp
huffman@29997
   152
huffman@30001
   153
lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
huffman@30001
   154
  by (simp add: less_Suc_eq_le [symmetric])
huffman@30001
   155
nipkow@69663
   156
nipkow@69663
   157
class CARD_1 =
nipkow@69663
   158
  assumes CARD_1: "CARD ('a) = 1"
nipkow@69663
   159
begin
nipkow@69663
   160
nipkow@69663
   161
subclass finite
nipkow@69663
   162
proof
nipkow@69663
   163
  from CARD_1 show "finite (UNIV :: 'a set)"
nipkow@69663
   164
    by (auto intro!: card_ge_0_finite)
nipkow@69663
   165
qed
nipkow@69663
   166
nipkow@69663
   167
end
nipkow@69663
   168
wenzelm@60500
   169
text \<open>Class for cardinality "at least 2"\<close>
huffman@30001
   170
huffman@30001
   171
class card2 = finite + 
huffman@30001
   172
  assumes two_le_card: "2 \<le> CARD('a)"
huffman@30001
   173
huffman@30001
   174
lemma one_less_card: "Suc 0 < CARD('a::card2)"
huffman@30001
   175
  using two_le_card [where 'a='a] by simp
huffman@30001
   176
huffman@30001
   177
lemma one_less_int_card: "1 < int CARD('a::card2)"
huffman@30001
   178
  using one_less_card [where 'a='a] by simp
huffman@30001
   179
Andreas@48176
   180
wenzelm@60500
   181
subsection \<open>A type class for deciding finiteness of types\<close>
Andreas@48176
   182
Andreas@48176
   183
type_synonym 'a finite_UNIV = "('a, bool) phantom"
Andreas@48176
   184
Andreas@48176
   185
class finite_UNIV = 
Andreas@48176
   186
  fixes finite_UNIV :: "('a, bool) phantom"
Andreas@48176
   187
  assumes finite_UNIV: "finite_UNIV = Phantom('a) (finite (UNIV :: 'a set))"
Andreas@48176
   188
Andreas@48176
   189
lemma finite_UNIV_code [code_unfold]:
Andreas@48176
   190
  "finite (UNIV :: 'a :: finite_UNIV set)
Andreas@48176
   191
  \<longleftrightarrow> of_phantom (finite_UNIV :: 'a finite_UNIV)"
Andreas@48176
   192
by(simp add: finite_UNIV)
Andreas@48176
   193
wenzelm@60500
   194
subsection \<open>A type class for computing the cardinality of types\<close>
Andreas@48051
   195
Andreas@48059
   196
definition is_list_UNIV :: "'a list \<Rightarrow> bool"
Andreas@51116
   197
where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"
Andreas@48059
   198
Andreas@48059
   199
lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
Andreas@48059
   200
by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric] 
Andreas@48059
   201
   dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
Andreas@48059
   202
Andreas@48164
   203
type_synonym 'a card_UNIV = "('a, nat) phantom"
Andreas@48164
   204
Andreas@48176
   205
class card_UNIV = finite_UNIV +
Andreas@48164
   206
  fixes card_UNIV :: "'a card_UNIV"
Andreas@48164
   207
  assumes card_UNIV: "card_UNIV = Phantom('a) CARD('a)"
Andreas@48051
   208
wenzelm@61585
   209
subsection \<open>Instantiations for \<open>card_UNIV\<close>\<close>
Andreas@48051
   210
Andreas@48051
   211
instantiation nat :: card_UNIV begin
Andreas@48176
   212
definition "finite_UNIV = Phantom(nat) False"
Andreas@48164
   213
definition "card_UNIV = Phantom(nat) 0"
Andreas@48176
   214
instance by intro_classes (simp_all add: finite_UNIV_nat_def card_UNIV_nat_def)
Andreas@48051
   215
end
Andreas@48051
   216
Andreas@48051
   217
instantiation int :: card_UNIV begin
Andreas@48176
   218
definition "finite_UNIV = Phantom(int) False"
Andreas@48164
   219
definition "card_UNIV = Phantom(int) 0"
Andreas@48176
   220
instance by intro_classes (simp_all add: card_UNIV_int_def finite_UNIV_int_def infinite_UNIV_int)
Andreas@48051
   221
end
Andreas@48051
   222
haftmann@51143
   223
instantiation natural :: card_UNIV begin
haftmann@51143
   224
definition "finite_UNIV = Phantom(natural) False"
haftmann@51143
   225
definition "card_UNIV = Phantom(natural) 0"
wenzelm@60679
   226
instance
wenzelm@60679
   227
  by standard
wenzelm@60679
   228
    (auto simp add: finite_UNIV_natural_def card_UNIV_natural_def card_eq_0_iff
wenzelm@60679
   229
      type_definition.univ [OF type_definition_natural] natural_eq_iff
wenzelm@60679
   230
      dest!: finite_imageD intro: inj_onI)
haftmann@51143
   231
end
haftmann@51143
   232
haftmann@51143
   233
instantiation integer :: card_UNIV begin
haftmann@51143
   234
definition "finite_UNIV = Phantom(integer) False"
haftmann@51143
   235
definition "card_UNIV = Phantom(integer) 0"
wenzelm@60679
   236
instance
wenzelm@60679
   237
  by standard
wenzelm@60679
   238
    (auto simp add: finite_UNIV_integer_def card_UNIV_integer_def card_eq_0_iff
wenzelm@60679
   239
      type_definition.univ [OF type_definition_integer] infinite_UNIV_int
wenzelm@60679
   240
      dest!: finite_imageD intro: inj_onI)
Andreas@48165
   241
end
Andreas@48165
   242
Andreas@48051
   243
instantiation list :: (type) card_UNIV begin
Andreas@48176
   244
definition "finite_UNIV = Phantom('a list) False"
Andreas@48164
   245
definition "card_UNIV = Phantom('a list) 0"
Andreas@48176
   246
instance by intro_classes (simp_all add: card_UNIV_list_def finite_UNIV_list_def infinite_UNIV_listI)
Andreas@48051
   247
end
Andreas@48051
   248
Andreas@48051
   249
instantiation unit :: card_UNIV begin
Andreas@48176
   250
definition "finite_UNIV = Phantom(unit) True"
Andreas@48164
   251
definition "card_UNIV = Phantom(unit) 1"
Andreas@48176
   252
instance by intro_classes (simp_all add: card_UNIV_unit_def finite_UNIV_unit_def)
Andreas@48051
   253
end
Andreas@48051
   254
Andreas@48051
   255
instantiation bool :: card_UNIV begin
Andreas@48176
   256
definition "finite_UNIV = Phantom(bool) True"
Andreas@48164
   257
definition "card_UNIV = Phantom(bool) 2"
Andreas@48176
   258
instance by(intro_classes)(simp_all add: card_UNIV_bool_def finite_UNIV_bool_def)
Andreas@48051
   259
end
Andreas@48051
   260
Andreas@48051
   261
instantiation char :: card_UNIV begin
Andreas@48176
   262
definition "finite_UNIV = Phantom(char) True"
Andreas@48164
   263
definition "card_UNIV = Phantom(char) 256"
Andreas@48176
   264
instance by intro_classes (simp_all add: card_UNIV_char_def card_UNIV_char finite_UNIV_char_def)
Andreas@48176
   265
end
Andreas@48176
   266
Andreas@48176
   267
instantiation prod :: (finite_UNIV, finite_UNIV) finite_UNIV begin
Andreas@48176
   268
definition "finite_UNIV = Phantom('a \<times> 'b) 
Andreas@48176
   269
  (of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> of_phantom (finite_UNIV :: 'b finite_UNIV))"
Andreas@48176
   270
instance by intro_classes (simp add: finite_UNIV_prod_def finite_UNIV finite_prod)
Andreas@48051
   271
end
Andreas@48051
   272
Andreas@48051
   273
instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@48176
   274
definition "card_UNIV = Phantom('a \<times> 'b) 
Andreas@48176
   275
  (of_phantom (card_UNIV :: 'a card_UNIV) * of_phantom (card_UNIV :: 'b card_UNIV))"
Andreas@48060
   276
instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
Andreas@48051
   277
end
Andreas@48051
   278
Andreas@48176
   279
instantiation sum :: (finite_UNIV, finite_UNIV) finite_UNIV begin
Andreas@48176
   280
definition "finite_UNIV = Phantom('a + 'b)
Andreas@48176
   281
  (of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> of_phantom (finite_UNIV :: 'b finite_UNIV))"
Andreas@48176
   282
instance
nipkow@68406
   283
  by intro_classes (simp add: finite_UNIV_sum_def finite_UNIV)
Andreas@48176
   284
end
Andreas@48176
   285
Andreas@48051
   286
instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@48164
   287
definition "card_UNIV = Phantom('a + 'b)
Andreas@48164
   288
  (let ca = of_phantom (card_UNIV :: 'a card_UNIV); 
Andreas@48164
   289
       cb = of_phantom (card_UNIV :: 'b card_UNIV)
Andreas@48164
   290
   in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
Andreas@48060
   291
instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
Andreas@48051
   292
end
Andreas@48051
   293
Andreas@48176
   294
instantiation "fun" :: (finite_UNIV, card_UNIV) finite_UNIV begin
Andreas@48176
   295
definition "finite_UNIV = Phantom('a \<Rightarrow> 'b)
Andreas@48176
   296
  (let cb = of_phantom (card_UNIV :: 'b card_UNIV)
Andreas@48176
   297
   in cb = 1 \<or> of_phantom (finite_UNIV :: 'a finite_UNIV) \<and> cb \<noteq> 0)"
Andreas@48176
   298
instance
Andreas@48176
   299
  by intro_classes (auto simp add: finite_UNIV_fun_def Let_def card_UNIV finite_UNIV finite_UNIV_fun card_gt_0_iff)
Andreas@48176
   300
end
Andreas@48176
   301
Andreas@48051
   302
instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@48164
   303
definition "card_UNIV = Phantom('a \<Rightarrow> 'b)
Andreas@48164
   304
  (let ca = of_phantom (card_UNIV :: 'a card_UNIV);
Andreas@48164
   305
       cb = of_phantom (card_UNIV :: 'b card_UNIV)
Andreas@48164
   306
   in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
Andreas@48060
   307
instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
Andreas@48060
   308
end
Andreas@48051
   309
Andreas@48176
   310
instantiation option :: (finite_UNIV) finite_UNIV begin
Andreas@48176
   311
definition "finite_UNIV = Phantom('a option) (of_phantom (finite_UNIV :: 'a finite_UNIV))"
Andreas@48176
   312
instance by intro_classes (simp add: finite_UNIV_option_def finite_UNIV)
Andreas@48176
   313
end
Andreas@48176
   314
Andreas@48060
   315
instantiation option :: (card_UNIV) card_UNIV begin
Andreas@48164
   316
definition "card_UNIV = Phantom('a option)
Andreas@48164
   317
  (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c \<noteq> 0 then Suc c else 0)"
Andreas@48060
   318
instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
Andreas@48060
   319
end
Andreas@48051
   320
Andreas@48060
   321
instantiation String.literal :: card_UNIV begin
Andreas@48176
   322
definition "finite_UNIV = Phantom(String.literal) False"
Andreas@48164
   323
definition "card_UNIV = Phantom(String.literal) 0"
Andreas@48176
   324
instance
Andreas@48176
   325
  by intro_classes (simp_all add: card_UNIV_literal_def finite_UNIV_literal_def infinite_literal card_literal)
Andreas@48176
   326
end
Andreas@48176
   327
Andreas@48176
   328
instantiation set :: (finite_UNIV) finite_UNIV begin
Andreas@48176
   329
definition "finite_UNIV = Phantom('a set) (of_phantom (finite_UNIV :: 'a finite_UNIV))"
Andreas@48176
   330
instance by intro_classes (simp add: finite_UNIV_set_def finite_UNIV Finite_Set.finite_set)
Andreas@48060
   331
end
Andreas@48060
   332
Andreas@48060
   333
instantiation set :: (card_UNIV) card_UNIV begin
Andreas@48164
   334
definition "card_UNIV = Phantom('a set)
Andreas@48164
   335
  (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c = 0 then 0 else 2 ^ c)"
Andreas@48060
   336
instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
Andreas@48051
   337
end
Andreas@48051
   338
wenzelm@53015
   339
lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^sub>1]"
Andreas@48060
   340
by(auto intro: finite_1.exhaust)
Andreas@48060
   341
wenzelm@53015
   342
lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^sub>1, finite_2.a\<^sub>2]"
Andreas@48060
   343
by(auto intro: finite_2.exhaust)
Andreas@48051
   344
wenzelm@53015
   345
lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^sub>1, finite_3.a\<^sub>2, finite_3.a\<^sub>3]"
Andreas@48060
   346
by(auto intro: finite_3.exhaust)
Andreas@48051
   347
wenzelm@53015
   348
lemma UNIV_finite_4: "UNIV = set [finite_4.a\<^sub>1, finite_4.a\<^sub>2, finite_4.a\<^sub>3, finite_4.a\<^sub>4]"
Andreas@48060
   349
by(auto intro: finite_4.exhaust)
Andreas@48060
   350
Andreas@48060
   351
lemma UNIV_finite_5:
wenzelm@53015
   352
  "UNIV = set [finite_5.a\<^sub>1, finite_5.a\<^sub>2, finite_5.a\<^sub>3, finite_5.a\<^sub>4, finite_5.a\<^sub>5]"
Andreas@48060
   353
by(auto intro: finite_5.exhaust)
Andreas@48051
   354
Andreas@48060
   355
instantiation Enum.finite_1 :: card_UNIV begin
Andreas@48176
   356
definition "finite_UNIV = Phantom(Enum.finite_1) True"
Andreas@48164
   357
definition "card_UNIV = Phantom(Enum.finite_1) 1"
Andreas@48176
   358
instance
Andreas@48176
   359
  by intro_classes (simp_all add: UNIV_finite_1 card_UNIV_finite_1_def finite_UNIV_finite_1_def)
Andreas@48060
   360
end
Andreas@48060
   361
Andreas@48060
   362
instantiation Enum.finite_2 :: card_UNIV begin
Andreas@48176
   363
definition "finite_UNIV = Phantom(Enum.finite_2) True"
Andreas@48164
   364
definition "card_UNIV = Phantom(Enum.finite_2) 2"
Andreas@48176
   365
instance
Andreas@48176
   366
  by intro_classes (simp_all add: UNIV_finite_2 card_UNIV_finite_2_def finite_UNIV_finite_2_def)
Andreas@48060
   367
end
Andreas@48051
   368
Andreas@48060
   369
instantiation Enum.finite_3 :: card_UNIV begin
Andreas@48176
   370
definition "finite_UNIV = Phantom(Enum.finite_3) True"
Andreas@48164
   371
definition "card_UNIV = Phantom(Enum.finite_3) 3"
Andreas@48176
   372
instance
Andreas@48176
   373
  by intro_classes (simp_all add: UNIV_finite_3 card_UNIV_finite_3_def finite_UNIV_finite_3_def)
Andreas@48060
   374
end
Andreas@48060
   375
Andreas@48060
   376
instantiation Enum.finite_4 :: card_UNIV begin
Andreas@48176
   377
definition "finite_UNIV = Phantom(Enum.finite_4) True"
Andreas@48164
   378
definition "card_UNIV = Phantom(Enum.finite_4) 4"
Andreas@48176
   379
instance
Andreas@48176
   380
  by intro_classes (simp_all add: UNIV_finite_4 card_UNIV_finite_4_def finite_UNIV_finite_4_def)
Andreas@48060
   381
end
Andreas@48060
   382
Andreas@48060
   383
instantiation Enum.finite_5 :: card_UNIV begin
Andreas@48176
   384
definition "finite_UNIV = Phantom(Enum.finite_5) True"
Andreas@48164
   385
definition "card_UNIV = Phantom(Enum.finite_5) 5"
Andreas@48176
   386
instance
Andreas@48176
   387
  by intro_classes (simp_all add: UNIV_finite_5 card_UNIV_finite_5_def finite_UNIV_finite_5_def)
Andreas@48051
   388
end
Andreas@48051
   389
wenzelm@60500
   390
subsection \<open>Code setup for sets\<close>
Andreas@48051
   391
wenzelm@60500
   392
text \<open>
wenzelm@69593
   393
  Implement \<^term>\<open>CARD('a)\<close> via \<^term>\<open>card_UNIV\<close> and provide
wenzelm@69593
   394
  implementations for \<^term>\<open>finite\<close>, \<^term>\<open>card\<close>, \<^term>\<open>(\<subseteq>)\<close>, 
wenzelm@69593
   395
  and \<^term>\<open>(=)\<close>if the calling context already provides \<^class>\<open>finite_UNIV\<close>
wenzelm@69593
   396
  and \<^class>\<open>card_UNIV\<close> instances. If we implemented the latter
wenzelm@69593
   397
  always via \<^term>\<open>card_UNIV\<close>, we would require instances of essentially all 
Andreas@51139
   398
  element types, i.e., a lot of instantiation proofs and -- at run time --
Andreas@51139
   399
  possibly slow dictionary constructions.
wenzelm@60500
   400
\<close>
Andreas@51116
   401
wenzelm@61115
   402
context
wenzelm@61115
   403
begin
wenzelm@61115
   404
wenzelm@61115
   405
qualified definition card_UNIV' :: "'a card_UNIV"
Andreas@51139
   406
where [code del]: "card_UNIV' = Phantom('a) CARD('a)"
Andreas@51139
   407
Andreas@51139
   408
lemma CARD_code [code_unfold]:
Andreas@51139
   409
  "CARD('a) = of_phantom (card_UNIV' :: 'a card_UNIV)"
Andreas@51139
   410
by(simp add: card_UNIV'_def)
Andreas@51139
   411
Andreas@51139
   412
lemma card_UNIV'_code [code]:
Andreas@51139
   413
  "card_UNIV' = card_UNIV"
Andreas@51139
   414
by(simp add: card_UNIV card_UNIV'_def)
Andreas@51139
   415
wenzelm@61115
   416
end
Andreas@51139
   417
Andreas@48051
   418
lemma card_Compl:
Andreas@48051
   419
  "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
Andreas@48051
   420
by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
Andreas@48051
   421
Andreas@51139
   422
context fixes xs :: "'a :: finite_UNIV list"
Andreas@51139
   423
begin
Andreas@48062
   424
wenzelm@61115
   425
qualified definition finite' :: "'a set \<Rightarrow> bool"
Andreas@51139
   426
where [simp, code del, code_abbrev]: "finite' = finite"
Andreas@51139
   427
Andreas@51139
   428
lemma finite'_code [code]:
Andreas@51139
   429
  "finite' (set xs) \<longleftrightarrow> True"
Andreas@51139
   430
  "finite' (List.coset xs) \<longleftrightarrow> of_phantom (finite_UNIV :: 'a finite_UNIV)"
Andreas@48176
   431
by(simp_all add: card_gt_0_iff finite_UNIV)
Andreas@48062
   432
Andreas@51139
   433
end
Andreas@51139
   434
Andreas@51139
   435
context fixes xs :: "'a :: card_UNIV list"
Andreas@51139
   436
begin
Andreas@51139
   437
wenzelm@61115
   438
qualified definition card' :: "'a set \<Rightarrow> nat" 
Andreas@51139
   439
where [simp, code del, code_abbrev]: "card' = card"
Andreas@51139
   440
 
Andreas@51139
   441
lemma card'_code [code]:
Andreas@51139
   442
  "card' (set xs) = length (remdups xs)"
Andreas@51139
   443
  "card' (List.coset xs) = of_phantom (card_UNIV :: 'a card_UNIV) - length (remdups xs)"
Andreas@51139
   444
by(simp_all add: List.card_set card_Compl card_UNIV)
Andreas@51139
   445
Andreas@51139
   446
wenzelm@61115
   447
qualified definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
nipkow@67399
   448
where [simp, code del, code_abbrev]: "subset' = (\<subseteq>)"
Andreas@51139
   449
Andreas@51139
   450
lemma subset'_code [code]:
Andreas@51139
   451
  "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
Andreas@51139
   452
  "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
Andreas@51139
   453
  "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
Andreas@48062
   454
by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card])
Andreas@48062
   455
  (metis finite_compl finite_set rev_finite_subset)
Andreas@48062
   456
wenzelm@61115
   457
qualified definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
nipkow@67399
   458
where [simp, code del, code_abbrev]: "eq_set = (=)"
Andreas@51139
   459
Andreas@51139
   460
lemma eq_set_code [code]:
Andreas@51139
   461
  fixes ys
Andreas@48051
   462
  defines "rhs \<equiv> 
Andreas@48059
   463
  let n = CARD('a)
Andreas@48051
   464
  in if n = 0 then False else 
Andreas@48051
   465
        let xs' = remdups xs; ys' = remdups ys 
Andreas@48051
   466
        in length xs' + length ys' = n \<and> (\<forall>x \<in> set xs'. x \<notin> set ys') \<and> (\<forall>y \<in> set ys'. y \<notin> set xs')"
wenzelm@60583
   467
  shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs"
wenzelm@60583
   468
  and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs"
wenzelm@60583
   469
  and "eq_set (set xs) (set ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)"
wenzelm@60583
   470
  and "eq_set (List.coset xs) (List.coset ys) \<longleftrightarrow> (\<forall>x \<in> set xs. x \<in> set ys) \<and> (\<forall>y \<in> set ys. y \<in> set xs)"
wenzelm@61166
   471
proof goal_cases
wenzelm@60583
   472
  {
wenzelm@60583
   473
    case 1
wenzelm@60583
   474
    show ?case (is "?lhs \<longleftrightarrow> ?rhs")
wenzelm@60583
   475
    proof
wenzelm@60583
   476
      show ?rhs if ?lhs
wenzelm@60583
   477
        using that
wenzelm@60583
   478
        by (auto simp add: rhs_def Let_def List.card_set[symmetric]
wenzelm@60583
   479
          card_Un_Int[where A="set xs" and B="- set xs"] card_UNIV
wenzelm@60583
   480
          Compl_partition card_gt_0_iff dest: sym)(metis finite_compl finite_set)
wenzelm@60583
   481
      show ?lhs if ?rhs
wenzelm@60583
   482
      proof - 
wenzelm@60583
   483
        have "\<lbrakk> \<forall>y\<in>set xs. y \<notin> set ys; \<forall>x\<in>set ys. x \<notin> set xs \<rbrakk> \<Longrightarrow> set xs \<inter> set ys = {}" by blast
wenzelm@60583
   484
        with that show ?thesis
wenzelm@60583
   485
          by (auto simp add: rhs_def Let_def List.card_set[symmetric]
wenzelm@60583
   486
            card_UNIV card_gt_0_iff card_Un_Int[where A="set xs" and B="set ys"]
nipkow@62390
   487
            dest: card_eq_UNIV_imp_eq_UNIV split: if_split_asm)
wenzelm@60583
   488
      qed
wenzelm@60583
   489
    qed
wenzelm@60583
   490
  }
wenzelm@60583
   491
  moreover
wenzelm@60583
   492
  case 2
wenzelm@60583
   493
  ultimately show ?case unfolding eq_set_def by blast
wenzelm@60583
   494
next
wenzelm@60583
   495
  case 3
wenzelm@60583
   496
  show ?case unfolding eq_set_def List.coset_def by blast
wenzelm@60583
   497
next
wenzelm@60583
   498
  case 4
wenzelm@60583
   499
  show ?case unfolding eq_set_def List.coset_def by blast
Andreas@48051
   500
qed
Andreas@48051
   501
Andreas@51139
   502
end
Andreas@51139
   503
wenzelm@60500
   504
text \<open>
Andreas@51139
   505
  Provide more informative exceptions than Match for non-rewritten cases.
Andreas@51139
   506
  If generated code raises one these exceptions, then a code equation calls
Andreas@51139
   507
  the mentioned operator for an element type that is not an instance of
wenzelm@69593
   508
  \<^class>\<open>card_UNIV\<close> and is therefore not implemented via \<^term>\<open>card_UNIV\<close>.
wenzelm@69593
   509
  Constrain the element type with sort \<^class>\<open>card_UNIV\<close> to change this.
wenzelm@60500
   510
\<close>
Andreas@51139
   511
Andreas@51139
   512
lemma card_coset_error [code]:
Andreas@53745
   513
  "card (List.coset xs) = 
Andreas@53745
   514
   Code.abort (STR ''card (List.coset _) requires type class instance card_UNIV'')
Andreas@53745
   515
     (\<lambda>_. card (List.coset xs))"
Andreas@51139
   516
by(simp)
Andreas@51139
   517
Andreas@51139
   518
lemma coset_subseteq_set_code [code]:
Andreas@51139
   519
  "List.coset xs \<subseteq> set ys \<longleftrightarrow> 
Andreas@53745
   520
  (if xs = [] \<and> ys = [] then False 
Andreas@53745
   521
   else Code.abort
Andreas@53745
   522
     (STR ''subset_eq (List.coset _) (List.set _) requires type class instance card_UNIV'')
Andreas@53745
   523
     (\<lambda>_. List.coset xs \<subseteq> set ys))"
Andreas@51139
   524
by simp
Andreas@51139
   525
wenzelm@67443
   526
notepad begin \<comment> \<open>test code setup\<close>
Andreas@51139
   527
have "List.coset [True] = set [False] \<and> 
Andreas@51139
   528
      List.coset [] \<subseteq> List.set [True, False] \<and> 
Andreas@51139
   529
      finite (List.coset [True])"
Andreas@48062
   530
  by eval
Andreas@48062
   531
end
Andreas@48062
   532
Andreas@48051
   533
end