src/HOL/Library/Cardinality.thy
author Andreas Lochbihler
Thu Jun 28 09:18:58 2012 +0200 (2012-06-28)
changeset 48165 d07a0b9601aa
parent 48164 e97369f20c30
child 48176 3d9c1ddb9f47
permissions -rw-r--r--
instantiate card_UNIV with nibble and code_numeral
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
Andreas@48164
     8
imports Phantom_Type
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@48070
   173
where [code del]: "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
lemma is_list_UNIV_iff: "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
Andreas@48059
   176
by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric] 
Andreas@48059
   177
   dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)
Andreas@48059
   178
Andreas@48164
   179
type_synonym 'a card_UNIV = "('a, nat) phantom"
Andreas@48164
   180
Andreas@48051
   181
class card_UNIV = 
Andreas@48164
   182
  fixes card_UNIV :: "'a card_UNIV"
Andreas@48164
   183
  assumes card_UNIV: "card_UNIV = Phantom('a) CARD('a)"
Andreas@48051
   184
Andreas@48164
   185
lemma card_UNIV_code [code_unfold]: 
Andreas@48164
   186
  "CARD('a :: card_UNIV) = of_phantom (card_UNIV :: 'a card_UNIV)"
Andreas@48059
   187
by(simp add: card_UNIV)
Andreas@48051
   188
Andreas@48070
   189
lemma is_list_UNIV_code [code]:
Andreas@48070
   190
  "is_list_UNIV (xs :: 'a list) = 
Andreas@48070
   191
  (let c = CARD('a :: card_UNIV) in if c = 0 then False else size (remdups xs) = c)"
Andreas@48070
   192
by(rule is_list_UNIV_def)
Andreas@48070
   193
Andreas@48060
   194
lemma finite_UNIV_conv_card_UNIV [code_unfold]:
Andreas@48164
   195
  "finite (UNIV :: 'a :: card_UNIV set) \<longleftrightarrow> 
Andreas@48164
   196
  of_phantom (card_UNIV :: 'a card_UNIV) > 0"
Andreas@48060
   197
by(simp add: card_UNIV card_gt_0_iff)
Andreas@48051
   198
Andreas@48060
   199
subsection {* Instantiations for @{text "card_UNIV"} *}
Andreas@48051
   200
Andreas@48051
   201
instantiation nat :: card_UNIV begin
Andreas@48164
   202
definition "card_UNIV = Phantom(nat) 0"
Andreas@48053
   203
instance by intro_classes (simp add: card_UNIV_nat_def)
Andreas@48051
   204
end
Andreas@48051
   205
Andreas@48051
   206
instantiation int :: card_UNIV begin
Andreas@48164
   207
definition "card_UNIV = Phantom(int) 0"
Andreas@48053
   208
instance by intro_classes (simp add: card_UNIV_int_def infinite_UNIV_int)
Andreas@48051
   209
end
Andreas@48051
   210
Andreas@48165
   211
instantiation code_numeral :: card_UNIV begin
Andreas@48165
   212
definition "card_UNIV = Phantom(code_numeral) 0"
Andreas@48165
   213
instance 
Andreas@48165
   214
  by(intro_classes)(auto simp add: card_UNIV_code_numeral_def type_definition.univ[OF type_definition_code_numeral] card_eq_0_iff dest!: finite_imageD intro: inj_onI)
Andreas@48165
   215
end
Andreas@48165
   216
Andreas@48051
   217
instantiation list :: (type) card_UNIV begin
Andreas@48164
   218
definition "card_UNIV = Phantom('a list) 0"
Andreas@48053
   219
instance by intro_classes (simp add: card_UNIV_list_def infinite_UNIV_listI)
Andreas@48051
   220
end
Andreas@48051
   221
Andreas@48051
   222
instantiation unit :: card_UNIV begin
Andreas@48164
   223
definition "card_UNIV = Phantom(unit) 1"
Andreas@48053
   224
instance by intro_classes (simp add: card_UNIV_unit_def card_UNIV_unit)
Andreas@48051
   225
end
Andreas@48051
   226
Andreas@48051
   227
instantiation bool :: card_UNIV begin
Andreas@48164
   228
definition "card_UNIV = Phantom(bool) 2"
Andreas@48053
   229
instance by(intro_classes)(simp add: card_UNIV_bool_def card_UNIV_bool)
Andreas@48051
   230
end
Andreas@48051
   231
Andreas@48165
   232
instantiation nibble :: card_UNIV begin
Andreas@48165
   233
definition "card_UNIV = Phantom(nibble) 16"
Andreas@48165
   234
instance by(intro_classes)(simp add: card_UNIV_nibble_def card_nibble)
Andreas@48165
   235
end
Andreas@48165
   236
Andreas@48051
   237
instantiation char :: card_UNIV begin
Andreas@48164
   238
definition "card_UNIV = Phantom(char) 256"
Andreas@48053
   239
instance by intro_classes (simp add: card_UNIV_char_def card_UNIV_char)
Andreas@48051
   240
end
Andreas@48051
   241
Andreas@48051
   242
instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@48164
   243
definition "card_UNIV = 
Andreas@48164
   244
  Phantom('a \<times> 'b) (of_phantom (card_UNIV :: 'a card_UNIV) * of_phantom (card_UNIV :: 'b card_UNIV))"
Andreas@48060
   245
instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)
Andreas@48051
   246
end
Andreas@48051
   247
Andreas@48051
   248
instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@48164
   249
definition "card_UNIV = Phantom('a + 'b)
Andreas@48164
   250
  (let ca = of_phantom (card_UNIV :: 'a card_UNIV); 
Andreas@48164
   251
       cb = of_phantom (card_UNIV :: 'b card_UNIV)
Andreas@48164
   252
   in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
Andreas@48060
   253
instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)
Andreas@48051
   254
end
Andreas@48051
   255
Andreas@48051
   256
instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
Andreas@48164
   257
definition "card_UNIV = Phantom('a \<Rightarrow> 'b)
Andreas@48164
   258
  (let ca = of_phantom (card_UNIV :: 'a card_UNIV);
Andreas@48164
   259
       cb = of_phantom (card_UNIV :: 'b card_UNIV)
Andreas@48164
   260
   in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
Andreas@48060
   261
instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)
Andreas@48060
   262
end
Andreas@48051
   263
Andreas@48060
   264
instantiation option :: (card_UNIV) card_UNIV begin
Andreas@48164
   265
definition "card_UNIV = Phantom('a option)
Andreas@48164
   266
  (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c \<noteq> 0 then Suc c else 0)"
Andreas@48060
   267
instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)
Andreas@48060
   268
end
Andreas@48051
   269
Andreas@48060
   270
instantiation String.literal :: card_UNIV begin
Andreas@48164
   271
definition "card_UNIV = Phantom(String.literal) 0"
Andreas@48060
   272
instance by intro_classes (simp add: card_UNIV_literal_def card_literal)
Andreas@48060
   273
end
Andreas@48060
   274
Andreas@48060
   275
instantiation set :: (card_UNIV) card_UNIV begin
Andreas@48164
   276
definition "card_UNIV = Phantom('a set)
Andreas@48164
   277
  (let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c = 0 then 0 else 2 ^ c)"
Andreas@48060
   278
instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)
Andreas@48051
   279
end
Andreas@48051
   280
Andreas@48060
   281
Andreas@48060
   282
lemma UNIV_finite_1: "UNIV = set [finite_1.a\<^isub>1]"
Andreas@48060
   283
by(auto intro: finite_1.exhaust)
Andreas@48060
   284
Andreas@48060
   285
lemma UNIV_finite_2: "UNIV = set [finite_2.a\<^isub>1, finite_2.a\<^isub>2]"
Andreas@48060
   286
by(auto intro: finite_2.exhaust)
Andreas@48051
   287
Andreas@48060
   288
lemma UNIV_finite_3: "UNIV = set [finite_3.a\<^isub>1, finite_3.a\<^isub>2, finite_3.a\<^isub>3]"
Andreas@48060
   289
by(auto intro: finite_3.exhaust)
Andreas@48051
   290
Andreas@48060
   291
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
   292
by(auto intro: finite_4.exhaust)
Andreas@48060
   293
Andreas@48060
   294
lemma UNIV_finite_5:
Andreas@48060
   295
  "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
   296
by(auto intro: finite_5.exhaust)
Andreas@48051
   297
Andreas@48060
   298
instantiation Enum.finite_1 :: card_UNIV begin
Andreas@48164
   299
definition "card_UNIV = Phantom(Enum.finite_1) 1"
Andreas@48060
   300
instance by intro_classes (simp add: UNIV_finite_1 card_UNIV_finite_1_def)
Andreas@48060
   301
end
Andreas@48060
   302
Andreas@48060
   303
instantiation Enum.finite_2 :: card_UNIV begin
Andreas@48164
   304
definition "card_UNIV = Phantom(Enum.finite_2) 2"
Andreas@48060
   305
instance by intro_classes (simp add: UNIV_finite_2 card_UNIV_finite_2_def)
Andreas@48060
   306
end
Andreas@48051
   307
Andreas@48060
   308
instantiation Enum.finite_3 :: card_UNIV begin
Andreas@48164
   309
definition "card_UNIV = Phantom(Enum.finite_3) 3"
Andreas@48060
   310
instance by intro_classes (simp add: UNIV_finite_3 card_UNIV_finite_3_def)
Andreas@48060
   311
end
Andreas@48060
   312
Andreas@48060
   313
instantiation Enum.finite_4 :: card_UNIV begin
Andreas@48164
   314
definition "card_UNIV = Phantom(Enum.finite_4) 4"
Andreas@48060
   315
instance by intro_classes (simp add: UNIV_finite_4 card_UNIV_finite_4_def)
Andreas@48060
   316
end
Andreas@48060
   317
Andreas@48060
   318
instantiation Enum.finite_5 :: card_UNIV begin
Andreas@48164
   319
definition "card_UNIV = Phantom(Enum.finite_5) 5"
Andreas@48060
   320
instance by intro_classes (simp add: UNIV_finite_5 card_UNIV_finite_5_def)
Andreas@48051
   321
end
Andreas@48051
   322
Andreas@48062
   323
subsection {* Code setup for sets *}
Andreas@48051
   324
Andreas@48051
   325
lemma card_Compl:
Andreas@48051
   326
  "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
Andreas@48051
   327
by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
Andreas@48051
   328
Andreas@48062
   329
context fixes xs :: "'a :: card_UNIV list"
Andreas@48062
   330
begin
Andreas@48062
   331
Andreas@48062
   332
definition card' :: "'a set \<Rightarrow> nat" 
Andreas@48062
   333
where [simp, code del, code_abbrev]: "card' = card"
Andreas@48062
   334
Andreas@48062
   335
lemma card'_code [code]:
Andreas@48062
   336
  "card' (set xs) = length (remdups xs)"
Andreas@48164
   337
  "card' (List.coset xs) = of_phantom (card_UNIV :: 'a card_UNIV) - length (remdups xs)"
Andreas@48062
   338
by(simp_all add: List.card_set card_Compl card_UNIV)
Andreas@48062
   339
Andreas@48164
   340
lemma card'_UNIV [code_unfold]: 
Andreas@48164
   341
  "card' (UNIV :: 'a :: card_UNIV set) = of_phantom (card_UNIV :: 'a card_UNIV)"
Andreas@48062
   342
by(simp add: card_UNIV)
Andreas@48062
   343
Andreas@48062
   344
definition finite' :: "'a set \<Rightarrow> bool"
Andreas@48062
   345
where [simp, code del, code_abbrev]: "finite' = finite"
Andreas@48062
   346
Andreas@48062
   347
lemma finite'_code [code]:
Andreas@48062
   348
  "finite' (set xs) \<longleftrightarrow> True"
Andreas@48062
   349
  "finite' (List.coset xs) \<longleftrightarrow> CARD('a) > 0"
Andreas@48062
   350
by(simp_all add: card_gt_0_iff)
Andreas@48062
   351
Andreas@48062
   352
definition subset' :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
Andreas@48062
   353
where [simp, code del, code_abbrev]: "subset' = op \<subseteq>"
Andreas@48062
   354
Andreas@48062
   355
lemma subset'_code [code]:
Andreas@48062
   356
  "subset' A (List.coset ys) \<longleftrightarrow> (\<forall>y \<in> set ys. y \<notin> A)"
Andreas@48062
   357
  "subset' (set ys) B \<longleftrightarrow> (\<forall>y \<in> set ys. y \<in> B)"
Andreas@48062
   358
  "subset' (List.coset xs) (set ys) \<longleftrightarrow> (let n = CARD('a) in n > 0 \<and> card(set (xs @ ys)) = n)"
Andreas@48062
   359
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
   360
  (metis finite_compl finite_set rev_finite_subset)
Andreas@48062
   361
Andreas@48062
   362
definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
Andreas@48062
   363
where [simp, code del, code_abbrev]: "eq_set = op ="
Andreas@48062
   364
Andreas@48051
   365
lemma eq_set_code [code]:
Andreas@48062
   366
  fixes ys
Andreas@48051
   367
  defines "rhs \<equiv> 
Andreas@48059
   368
  let n = CARD('a)
Andreas@48051
   369
  in if n = 0 then False else 
Andreas@48051
   370
        let xs' = remdups xs; ys' = remdups ys 
Andreas@48051
   371
        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
   372
  shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
Andreas@48051
   373
  and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
Andreas@48051
   374
  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
   375
  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
   376
proof -
Andreas@48051
   377
  show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
Andreas@48051
   378
  proof
Andreas@48051
   379
    assume ?lhs thus ?rhs
Andreas@48051
   380
      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
   381
  next
Andreas@48051
   382
    assume ?rhs
Andreas@48051
   383
    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
   384
    ultimately show ?lhs
Andreas@48051
   385
      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
   386
  qed
Andreas@48051
   387
  thus ?thesis2 unfolding eq_set_def by blast
Andreas@48051
   388
  show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
Andreas@48051
   389
qed
Andreas@48051
   390
Andreas@48062
   391
end
Andreas@48062
   392
Andreas@48062
   393
notepad begin (* test code setup *)
Andreas@48062
   394
have "List.coset [True] = set [False] \<and> List.coset [] \<subseteq> List.set [True, False] \<and> finite (List.coset [True])"
Andreas@48062
   395
  by eval
Andreas@48062
   396
end
Andreas@48062
   397
Andreas@48062
   398
hide_const (open) card' finite' subset' eq_set
Andreas@48051
   399
Andreas@48051
   400
end