src/HOL/Library/Cardinality.thy
author huffman
Sat Jun 02 08:32:42 2012 +0200 (2012-06-02)
changeset 48067 9f458b3efb5c
parent 48063 f02b4302d5dd
parent 48062 9014e78ccde2
child 48070 02d64fd40852
permissions -rw-r--r--
merged
haftmann@37653
     1
(*  Title:      HOL/Library/Cardinality.thy
Andreas@48051
     2
    Author:     Brian Huffman, Andreas Lochbihler
kleing@24332
     3
*)
kleing@24332
     4
haftmann@37653
     5
header {* Cardinality of types *}
kleing@24332
     6
haftmann@37653
     7
theory Cardinality
huffman@47108
     8
imports "~~/src/HOL/Main"
kleing@24332
     9
begin
kleing@24332
    10
kleing@24332
    11
subsection {* Preliminary lemmas *}
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
Andreas@48060
    30
lemma finite_range_Some: "finite (range (Some :: 'a \<Rightarrow> 'a option)) = finite (UNIV :: 'a set)"
Andreas@48060
    31
by(auto dest: finite_imageD intro: inj_Some)
Andreas@48060
    32
kleing@24332
    33
kleing@24332
    34
subsection {* Cardinalities of types *}
kleing@24332
    35
kleing@24332
    36
syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
kleing@24332
    37
wenzelm@35431
    38
translations "CARD('t)" => "CONST card (CONST UNIV \<Colon> 't set)"
kleing@24332
    39
wenzelm@42247
    40
typed_print_translation (advanced) {*
wenzelm@42247
    41
  let
wenzelm@42247
    42
    fun card_univ_tr' ctxt _ [Const (@{const_syntax UNIV}, Type (_, [T, _]))] =
wenzelm@42247
    43
      Syntax.const @{syntax_const "_type_card"} $ Syntax_Phases.term_of_typ ctxt T;
wenzelm@42247
    44
  in [(@{const_syntax card}, card_univ_tr')] end
huffman@24407
    45
*}
huffman@24407
    46
Andreas@48058
    47
lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a) * CARD('b)"
haftmann@26153
    48
  unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
kleing@24332
    49
Andreas@48060
    50
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
    51
unfolding UNIV_Plus_UNIV[symmetric]
Andreas@48060
    52
by(auto simp add: card_eq_0_iff card_Plus simp del: UNIV_Plus_UNIV)
Andreas@48060
    53
huffman@30001
    54
lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
Andreas@48060
    55
by(simp add: card_UNIV_sum)
Andreas@48060
    56
Andreas@48060
    57
lemma card_UNIV_option: "CARD('a option) = (if CARD('a) = 0 then 0 else CARD('a) + 1)"
Andreas@48060
    58
proof -
Andreas@48060
    59
  have "(None :: 'a option) \<notin> range Some" by clarsimp
Andreas@48060
    60
  thus ?thesis
Andreas@48060
    61
    by(simp add: UNIV_option_conv card_eq_0_iff finite_range_Some card_insert_disjoint card_image)
Andreas@48060
    62
qed
kleing@24332
    63
huffman@30001
    64
lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
Andreas@48060
    65
by(simp add: card_UNIV_option)
Andreas@48060
    66
Andreas@48060
    67
lemma card_UNIV_set: "CARD('a set) = (if CARD('a) = 0 then 0 else 2 ^ CARD('a))"
Andreas@48060
    68
by(simp add: Pow_UNIV[symmetric] card_eq_0_iff card_Pow del: Pow_UNIV)
kleing@24332
    69
huffman@30001
    70
lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
Andreas@48060
    71
by(simp add: card_UNIV_set)
kleing@24332
    72
huffman@30001
    73
lemma card_nat [simp]: "CARD(nat) = 0"
huffman@44142
    74
  by (simp add: card_eq_0_iff)
huffman@30001
    75
Andreas@48060
    76
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
    77
proof -
Andreas@48060
    78
  {  assume "0 < CARD('a)" and "0 < CARD('b)"
Andreas@48060
    79
    hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
Andreas@48060
    80
      by(simp_all only: card_ge_0_finite)
Andreas@48060
    81
    from finite_distinct_list[OF finb] obtain bs 
Andreas@48060
    82
      where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
Andreas@48060
    83
    from finite_distinct_list[OF fina] obtain as
Andreas@48060
    84
      where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
Andreas@48060
    85
    have cb: "CARD('b) = length bs"
Andreas@48060
    86
      unfolding bs[symmetric] distinct_card[OF distb] ..
Andreas@48060
    87
    have ca: "CARD('a) = length as"
Andreas@48060
    88
      unfolding as[symmetric] distinct_card[OF dista] ..
Andreas@48060
    89
    let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (Enum.n_lists (length as) bs)"
Andreas@48060
    90
    have "UNIV = set ?xs"
Andreas@48060
    91
    proof(rule UNIV_eq_I)
Andreas@48060
    92
      fix f :: "'a \<Rightarrow> 'b"
Andreas@48060
    93
      from as have "f = the \<circ> map_of (zip as (map f as))"
Andreas@48060
    94
        by(auto simp add: map_of_zip_map)
Andreas@48060
    95
      thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
Andreas@48060
    96
    qed
Andreas@48060
    97
    moreover have "distinct ?xs" unfolding distinct_map
Andreas@48060
    98
    proof(intro conjI distinct_n_lists distb inj_onI)
Andreas@48060
    99
      fix xs ys :: "'b list"
Andreas@48060
   100
      assume xs: "xs \<in> set (Enum.n_lists (length as) bs)"
Andreas@48060
   101
        and ys: "ys \<in> set (Enum.n_lists (length as) bs)"
Andreas@48060
   102
        and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
Andreas@48060
   103
      from xs ys have [simp]: "length xs = length as" "length ys = length as"
Andreas@48060
   104
        by(simp_all add: length_n_lists_elem)
Andreas@48060
   105
      have "map_of (zip as xs) = map_of (zip as ys)"
Andreas@48060
   106
      proof
Andreas@48060
   107
        fix x
Andreas@48060
   108
        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
   109
          by(simp_all add: map_of_zip_is_Some[symmetric])
Andreas@48060
   110
        with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
Andreas@48060
   111
          by(auto dest: fun_cong[where x=x])
Andreas@48060
   112
      qed
Andreas@48060
   113
      with dista show "xs = ys" by(simp add: map_of_zip_inject)
Andreas@48060
   114
    qed
Andreas@48060
   115
    hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
Andreas@48060
   116
    moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
Andreas@48060
   117
    ultimately have "CARD('a \<Rightarrow> 'b) = CARD('b) ^ CARD('a)" using cb ca by simp }
Andreas@48060
   118
  moreover {
Andreas@48060
   119
    assume cb: "CARD('b) = 1"
Andreas@48060
   120
    then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
Andreas@48060
   121
    have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
Andreas@48060
   122
    proof(rule UNIV_eq_I)
Andreas@48060
   123
      fix x :: "'a \<Rightarrow> 'b"
Andreas@48060
   124
      { fix y
Andreas@48060
   125
        have "x y \<in> UNIV" ..
Andreas@48060
   126
        hence "x y = b" unfolding b by simp }
Andreas@48060
   127
      thus "x \<in> {\<lambda>x. b}" by(auto)
Andreas@48060
   128
    qed
Andreas@48060
   129
    have "CARD('a \<Rightarrow> 'b) = 1" unfolding eq by simp }
Andreas@48060
   130
  ultimately show ?thesis
Andreas@48060
   131
    by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
Andreas@48060
   132
qed
Andreas@48060
   133
Andreas@48060
   134
lemma card_nibble: "CARD(nibble) = 16"
Andreas@48060
   135
unfolding UNIV_nibble by simp
Andreas@48060
   136
Andreas@48060
   137
lemma card_UNIV_char: "CARD(char) = 256"
Andreas@48060
   138
proof -
Andreas@48060
   139
  have "inj (\<lambda>(x, y). Char x y)" by(auto intro: injI)
Andreas@48060
   140
  thus ?thesis unfolding UNIV_char by(simp add: card_image card_nibble)
Andreas@48060
   141
qed
Andreas@48060
   142
Andreas@48060
   143
lemma card_literal: "CARD(String.literal) = 0"
Andreas@48060
   144
proof -
Andreas@48060
   145
  have "inj STR" by(auto intro: injI)
Andreas@48060
   146
  thus ?thesis by(simp add: type_definition.univ[OF type_definition_literal] card_image infinite_UNIV_listI)
Andreas@48060
   147
qed
huffman@30001
   148
huffman@30001
   149
subsection {* Classes with at least 1 and 2  *}
huffman@30001
   150
huffman@30001
   151
text {* Class finite already captures "at least 1" *}
huffman@30001
   152
huffman@30001
   153
lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
huffman@29997
   154
  unfolding neq0_conv [symmetric] by simp
huffman@29997
   155
huffman@30001
   156
lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
huffman@30001
   157
  by (simp add: less_Suc_eq_le [symmetric])
huffman@30001
   158
huffman@30001
   159
text {* Class for cardinality "at least 2" *}
huffman@30001
   160
huffman@30001
   161
class card2 = finite + 
huffman@30001
   162
  assumes two_le_card: "2 \<le> CARD('a)"
huffman@30001
   163
huffman@30001
   164
lemma one_less_card: "Suc 0 < CARD('a::card2)"
huffman@30001
   165
  using two_le_card [where 'a='a] by simp
huffman@30001
   166
huffman@30001
   167
lemma one_less_int_card: "1 < int CARD('a::card2)"
huffman@30001
   168
  using one_less_card [where 'a='a] by simp
huffman@30001
   169
Andreas@48051
   170
subsection {* A type class for computing the cardinality of types *}
Andreas@48051
   171
Andreas@48059
   172
definition is_list_UNIV :: "'a list \<Rightarrow> bool"
Andreas@48059
   173
where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"
Andreas@48059
   174
Andreas@48059
   175
lemmas [code_unfold] = is_list_UNIV_def[abs_def]
Andreas@48059
   176
Andreas@48059
   177
lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
Andreas@48059
   178
by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric] 
Andreas@48059
   179
   dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
Andreas@48059
   180
Andreas@48051
   181
class card_UNIV = 
Andreas@48051
   182
  fixes card_UNIV :: "'a itself \<Rightarrow> nat"
Andreas@48059
   183
  assumes card_UNIV: "card_UNIV x = CARD('a)"
Andreas@48051
   184
Andreas@48059
   185
lemma card_UNIV_code [code_unfold]: "CARD('a :: card_UNIV) = card_UNIV TYPE('a)"
Andreas@48059
   186
by(simp add: card_UNIV)
Andreas@48051
   187
Andreas@48060
   188
lemma finite_UNIV_conv_card_UNIV [code_unfold]:
Andreas@48060
   189
  "finite (UNIV :: 'a :: card_UNIV set) \<longleftrightarrow> card_UNIV TYPE('a) > 0"
Andreas@48060
   190
by(simp add: card_UNIV card_gt_0_iff)
Andreas@48051
   191
Andreas@48060
   192
subsection {* Instantiations for @{text "card_UNIV"} *}
Andreas@48051
   193
Andreas@48051
   194
instantiation nat :: card_UNIV begin
Andreas@48051
   195
definition "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
Andreas@48053
   196
instance by intro_classes (simp add: card_UNIV_nat_def)
Andreas@48051
   197
end
Andreas@48051
   198
Andreas@48051
   199
instantiation int :: card_UNIV begin
Andreas@48052
   200
definition "card_UNIV = (\<lambda>a :: int itself. 0)"
Andreas@48053
   201
instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int)
Andreas@48051
   202
end
Andreas@48051
   203
Andreas@48051
   204
instantiation list :: (type) card_UNIV begin
Andreas@48052
   205
definition "card_UNIV = (\<lambda>a :: 'a list itself. 0)"
Andreas@48053
   206
instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI)
Andreas@48051
   207
end
Andreas@48051
   208
Andreas@48051
   209
instantiation unit :: card_UNIV begin
Andreas@48052
   210
definition "card_UNIV = (\<lambda>a :: unit itself. 1)"
Andreas@48053
   211
instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit)
Andreas@48051
   212
end
Andreas@48051
   213
Andreas@48051
   214
instantiation bool :: card_UNIV begin
Andreas@48052
   215
definition "card_UNIV = (\<lambda>a :: bool itself. 2)"
Andreas@48053
   216
instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool)
Andreas@48051
   217
end
Andreas@48051
   218
Andreas@48051
   219
instantiation char :: card_UNIV begin
Andreas@48052
   220
definition "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
Andreas@48053
   221
instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char)
Andreas@48051
   222
end
Andreas@48051
   223
Andreas@48051
   224
instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@48052
   225
definition "card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
Andreas@48060
   226
instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
Andreas@48051
   227
end
Andreas@48051
   228
Andreas@48051
   229
instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@48060
   230
definition "card_UNIV = (\<lambda>a :: ('a + 'b) itself. 
Andreas@48052
   231
  let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
Andreas@48052
   232
  in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
Andreas@48060
   233
instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
Andreas@48051
   234
end
Andreas@48051
   235
Andreas@48051
   236
instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@48060
   237
definition "card_UNIV = (\<lambda>a :: ('a \<Rightarrow> 'b) itself. 
Andreas@48060
   238
  let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
Andreas@48060
   239
  in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
Andreas@48060
   240
instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
Andreas@48060
   241
end
Andreas@48051
   242
Andreas@48060
   243
instantiation option :: (card_UNIV) card_UNIV begin
Andreas@48060
   244
definition "card_UNIV = (\<lambda>a :: 'a option itself. 
Andreas@48060
   245
  let c = card_UNIV (TYPE('a)) in if c \<noteq> 0 then Suc c else 0)"
Andreas@48060
   246
instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
Andreas@48060
   247
end
Andreas@48051
   248
Andreas@48060
   249
instantiation String.literal :: card_UNIV begin
Andreas@48060
   250
definition "card_UNIV = (\<lambda>a :: String.literal itself. 0)"
Andreas@48060
   251
instance by intro_classes (simp add: card_UNIV_literal_def card_literal)
Andreas@48060
   252
end
Andreas@48060
   253
Andreas@48060
   254
instantiation set :: (card_UNIV) card_UNIV begin
Andreas@48060
   255
definition "card_UNIV = (\<lambda>a :: 'a set itself.
Andreas@48060
   256
  let c = card_UNIV (TYPE('a)) in if c = 0 then 0 else 2 ^ c)"
Andreas@48060
   257
instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
Andreas@48051
   258
end
Andreas@48051
   259
Andreas@48060
   260
Andreas@48060
   261
lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]"
Andreas@48060
   262
by(auto intro: finite_1.exhaust)
Andreas@48060
   263
Andreas@48060
   264
lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]"
Andreas@48060
   265
by(auto intro: finite_2.exhaust)
Andreas@48051
   266
Andreas@48060
   267
lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^isub>1, finite_3.a\<^isub>2, finite_3.a\<^isub>3]"
Andreas@48060
   268
by(auto intro: finite_3.exhaust)
Andreas@48051
   269
Andreas@48060
   270
lemma UNIV_finite_4: "UNIV = set [finite_4.a\<^isub>1, finite_4.a\<^isub>2, finite_4.a\<^isub>3, finite_4.a\<^isub>4]"
Andreas@48060
   271
by(auto intro: finite_4.exhaust)
Andreas@48060
   272
Andreas@48060
   273
lemma UNIV_finite_5:
Andreas@48060
   274
  "UNIV = set [finite_5.a\<^isub>1, finite_5.a\<^isub>2, finite_5.a\<^isub>3, finite_5.a\<^isub>4, finite_5.a\<^isub>5]"
Andreas@48060
   275
by(auto intro: finite_5.exhaust)
Andreas@48051
   276
Andreas@48060
   277
instantiation Enum.finite_1 :: card_UNIV begin
Andreas@48060
   278
definition "card_UNIV = (\<lambda>a :: Enum.finite_1 itself. 1)"
Andreas@48060
   279
instance by intro_classes (simp add: UNIV_finite_1 card_UNIV_finite_1_def)
Andreas@48060
   280
end
Andreas@48060
   281
Andreas@48060
   282
instantiation Enum.finite_2 :: card_UNIV begin
Andreas@48060
   283
definition "card_UNIV = (\<lambda>a :: Enum.finite_2 itself. 2)"
Andreas@48060
   284
instance by intro_classes (simp add: UNIV_finite_2 card_UNIV_finite_2_def)
Andreas@48060
   285
end
Andreas@48051
   286
Andreas@48060
   287
instantiation Enum.finite_3 :: card_UNIV begin
Andreas@48060
   288
definition "card_UNIV = (\<lambda>a :: Enum.finite_3 itself. 3)"
Andreas@48060
   289
instance by intro_classes (simp add: UNIV_finite_3 card_UNIV_finite_3_def)
Andreas@48060
   290
end
Andreas@48060
   291
Andreas@48060
   292
instantiation Enum.finite_4 :: card_UNIV begin
Andreas@48060
   293
definition "card_UNIV = (\<lambda>a :: Enum.finite_4 itself. 4)"
Andreas@48060
   294
instance by intro_classes (simp add: UNIV_finite_4 card_UNIV_finite_4_def)
Andreas@48060
   295
end
Andreas@48060
   296
Andreas@48060
   297
instantiation Enum.finite_5 :: card_UNIV begin
Andreas@48060
   298
definition "card_UNIV = (\<lambda>a :: Enum.finite_5 itself. 5)"
Andreas@48060
   299
instance by intro_classes (simp add: UNIV_finite_5 card_UNIV_finite_5_def)
Andreas@48051
   300
end
Andreas@48051
   301
Andreas@48062
   302
subsection {* Code setup for sets *}
Andreas@48051
   303
Andreas@48051
   304
lemma card_Compl:
Andreas@48051
   305
  "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
Andreas@48051
   306
by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
Andreas@48051
   307
Andreas@48062
   308
context fixes xs :: "'a :: card_UNIV list"
Andreas@48062
   309
begin
Andreas@48062
   310
Andreas@48062
   311
definition card' :: "'a set \<Rightarrow> nat" 
Andreas@48062
   312
where [simp, code del, code_abbrev]: "card' = card"
Andreas@48062
   313
Andreas@48062
   314
lemma card'_code [code]:
Andreas@48062
   315
  "card' (set xs) = length (remdups xs)"
Andreas@48062
   316
  "card' (List.coset xs) =  card_UNIV TYPE('a) - length (remdups xs)"
Andreas@48062
   317
by(simp_all add: List.card_set card_Compl card_UNIV)
Andreas@48062
   318
Andreas@48062
   319
lemma card'_UNIV [code_unfold]: "card' (UNIV :: 'a :: card_UNIV set) = card_UNIV TYPE('a)"
Andreas@48062
   320
by(simp add: card_UNIV)
Andreas@48062
   321
Andreas@48062
   322
definition finite' :: "'a set \<Rightarrow> bool"
Andreas@48062
   323
where [simp, code del, code_abbrev]: "finite' = finite"
Andreas@48062
   324
Andreas@48062
   325
lemma finite'_code [code]:
Andreas@48062
   326
  "finite' (set xs) \<longleftrightarrow> True"
Andreas@48062
   327
  "finite' (List.coset xs) \<longleftrightarrow> CARD('a) > 0"
Andreas@48062
   328
by(simp_all add: card_gt_0_iff)
Andreas@48062
   329
Andreas@48062
   330
definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
Andreas@48062
   331
where [simp, code del, code_abbrev]: "subset' = op \<subseteq>"
Andreas@48062
   332
Andreas@48062
   333
lemma subset'_code [code]:
Andreas@48062
   334
  "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
Andreas@48062
   335
  "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
Andreas@48062
   336
  "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
Andreas@48062
   337
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
   338
  (metis finite_compl finite_set rev_finite_subset)
Andreas@48062
   339
Andreas@48062
   340
definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
Andreas@48062
   341
where [simp, code del, code_abbrev]: "eq_set = op ="
Andreas@48062
   342
Andreas@48051
   343
lemma eq_set_code [code]:
Andreas@48062
   344
  fixes ys
Andreas@48051
   345
  defines "rhs \<equiv> 
Andreas@48059
   346
  let n = CARD('a)
Andreas@48051
   347
  in if n = 0 then False else 
Andreas@48051
   348
        let xs' = remdups xs; ys' = remdups ys 
Andreas@48051
   349
        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')"
Andreas@48051
   350
  shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
Andreas@48051
   351
  and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
Andreas@48051
   352
  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)" (is ?thesis3)
Andreas@48051
   353
  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)" (is ?thesis4)
Andreas@48051
   354
proof -
Andreas@48051
   355
  show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
Andreas@48051
   356
  proof
Andreas@48051
   357
    assume ?lhs thus ?rhs
Andreas@48051
   358
      by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_Un_Int[where A="set xs" and B="- set xs"] card_UNIV Compl_partition card_gt_0_iff dest: sym)(metis finite_compl finite_set)
Andreas@48051
   359
  next
Andreas@48051
   360
    assume ?rhs
Andreas@48051
   361
    moreover 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
Andreas@48051
   362
    ultimately show ?lhs
Andreas@48051
   363
      by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_UNIV card_gt_0_iff card_Un_Int[where A="set xs" and B="set ys"] dest: card_eq_UNIV_imp_eq_UNIV split: split_if_asm)
Andreas@48051
   364
  qed
Andreas@48051
   365
  thus ?thesis2 unfolding eq_set_def by blast
Andreas@48051
   366
  show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
Andreas@48051
   367
qed
Andreas@48051
   368
Andreas@48062
   369
end
Andreas@48062
   370
Andreas@48062
   371
notepad begin (* test code setup *)
Andreas@48062
   372
have "List.coset [True] = set [False] \<and> List.coset [] \<subseteq> List.set [True, False] \<and> finite (List.coset [True])"
Andreas@48062
   373
  by eval
Andreas@48062
   374
end
Andreas@48062
   375
Andreas@48062
   376
hide_const (open) card' finite' subset' eq_set
Andreas@48051
   377
Andreas@48051
   378
end