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