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