src/HOL/Library/Cardinality.thy
author Andreas Lochbihler
Thu, 31 May 2012 17:04:11 +0200
changeset 48052 b74766e4c11e
parent 48051 53a0df441e20
child 48053 9bc78a08ff0a
permissions -rw-r--r--
tuned instantiations dropped redundant lemmas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37653
847e95ca9b0a split off Cardinality from Numeral_Type
haftmann
parents: 36350
diff changeset
     1
(*  Title:      HOL/Library/Cardinality.thy
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
     2
    Author:     Brian Huffman, Andreas Lochbihler
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
     3
*)
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
     4
37653
847e95ca9b0a split off Cardinality from Numeral_Type
haftmann
parents: 36350
diff changeset
     5
header {* Cardinality of types *}
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
     6
37653
847e95ca9b0a split off Cardinality from Numeral_Type
haftmann
parents: 36350
diff changeset
     7
theory Cardinality
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 44142
diff changeset
     8
imports "~~/src/HOL/Main"
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
     9
begin
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    10
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    11
subsection {* Preliminary lemmas *}
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    12
(* These should be moved elsewhere *)
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    13
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    14
lemma (in type_definition) univ:
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    15
  "UNIV = Abs ` A"
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    16
proof
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    17
  show "Abs ` A \<subseteq> UNIV" by (rule subset_UNIV)
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    18
  show "UNIV \<subseteq> Abs ` A"
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    19
  proof
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    20
    fix x :: 'b
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    21
    have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    22
    moreover have "Rep x \<in> A" by (rule Rep)
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    23
    ultimately show "x \<in> Abs ` A" by (rule image_eqI)
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    24
  qed
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    25
qed
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    26
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    27
lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    28
  by (simp add: univ card_image inj_on_def Abs_inject)
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    29
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    30
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    31
subsection {* Cardinalities of types *}
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    32
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    33
syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    34
35431
8758fe1fc9f8 cleanup type translations;
wenzelm
parents: 35362
diff changeset
    35
translations "CARD('t)" => "CONST card (CONST UNIV \<Colon> 't set)"
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    36
42247
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
    37
typed_print_translation (advanced) {*
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
    38
  let
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
    39
    fun card_univ_tr' ctxt _ [Const (@{const_syntax UNIV}, Type (_, [T, _]))] =
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
    40
      Syntax.const @{syntax_const "_type_card"} $ Syntax_Phases.term_of_typ ctxt T;
12fe41a92cd5 typed_print_translation: discontinued show_sorts argument;
wenzelm
parents: 42245
diff changeset
    41
  in [(@{const_syntax card}, card_univ_tr')] end
24407
61b10ffb2549 typed print translation for CARD('a);
huffman
parents: 24406
diff changeset
    42
*}
61b10ffb2549 typed print translation for CARD('a);
huffman
parents: 24406
diff changeset
    43
30001
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    44
lemma card_unit [simp]: "CARD(unit) = 1"
26153
b037fd9016fa other UNIV lemmas
haftmann
parents: 25459
diff changeset
    45
  unfolding UNIV_unit by simp
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    46
30001
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    47
lemma card_prod [simp]: "CARD('a \<times> 'b) = CARD('a::finite) * CARD('b::finite)"
26153
b037fd9016fa other UNIV lemmas
haftmann
parents: 25459
diff changeset
    48
  unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    49
30001
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    50
lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
26153
b037fd9016fa other UNIV lemmas
haftmann
parents: 25459
diff changeset
    51
  unfolding UNIV_Plus_UNIV [symmetric] by (simp only: finite card_Plus)
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    52
30001
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    53
lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
31080
21ffc770ebc0 lemmas by Andreas Lochbihler
nipkow
parents: 31021
diff changeset
    54
  unfolding UNIV_option_conv
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    55
  apply (subgoal_tac "(None::'a option) \<notin> range Some")
29997
f6756c097c2d number_ring instances for numeral types
huffman
parents: 29629
diff changeset
    56
  apply (simp add: card_image)
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    57
  apply fast
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    58
  done
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    59
30001
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    60
lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
26153
b037fd9016fa other UNIV lemmas
haftmann
parents: 25459
diff changeset
    61
  unfolding Pow_UNIV [symmetric]
47221
7205eb4a0a05 rephrase lemma card_Pow using '2' instead of 'Suc (Suc 0)'
huffman
parents: 47108
diff changeset
    62
  by (simp only: card_Pow finite)
24332
e3a2b75b1cf9 boolean algebras as locales and numbers as types by Brian Huffman
kleing
parents:
diff changeset
    63
30001
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    64
lemma card_nat [simp]: "CARD(nat) = 0"
44142
8e27e0177518 avoid warnings about duplicate rules
huffman
parents: 42247
diff changeset
    65
  by (simp add: card_eq_0_iff)
30001
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    66
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    67
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    68
subsection {* Classes with at least 1 and 2  *}
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    69
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    70
text {* Class finite already captures "at least 1" *}
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    71
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    72
lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
29997
f6756c097c2d number_ring instances for numeral types
huffman
parents: 29629
diff changeset
    73
  unfolding neq0_conv [symmetric] by simp
f6756c097c2d number_ring instances for numeral types
huffman
parents: 29629
diff changeset
    74
30001
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    75
lemma one_le_card_finite [simp]: "Suc 0 \<le> CARD('a::finite)"
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    76
  by (simp add: less_Suc_eq_le [symmetric])
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    77
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    78
text {* Class for cardinality "at least 2" *}
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    79
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    80
class card2 = finite + 
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    81
  assumes two_le_card: "2 \<le> CARD('a)"
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    82
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    83
lemma one_less_card: "Suc 0 < CARD('a::card2)"
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    84
  using two_le_card [where 'a='a] by simp
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    85
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    86
lemma one_less_int_card: "1 < int CARD('a::card2)"
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    87
  using one_less_card [where 'a='a] by simp
dd27e16677b2 cleaned up
huffman
parents: 29999
diff changeset
    88
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    89
subsection {* A type class for computing the cardinality of types *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    90
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    91
class card_UNIV = 
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    92
  fixes card_UNIV :: "'a itself \<Rightarrow> nat"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    93
  assumes card_UNIV: "card_UNIV x = card (UNIV :: 'a set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    94
begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    95
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    96
lemma card_UNIV_neq_0_finite_UNIV:
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    97
  "card_UNIV x \<noteq> 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    98
by(simp add: card_UNIV card_eq_0_iff)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
    99
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   100
lemma card_UNIV_ge_0_finite_UNIV:
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   101
  "card_UNIV x > 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   102
by(auto simp add: card_UNIV intro: card_ge_0_finite finite_UNIV_card_ge_0)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   103
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   104
lemma card_UNIV_eq_0_infinite_UNIV:
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   105
  "card_UNIV x = 0 \<longleftrightarrow> \<not> finite (UNIV :: 'a set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   106
by(simp add: card_UNIV card_eq_0_iff)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   107
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   108
definition is_list_UNIV :: "'a list \<Rightarrow> bool"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   109
where "is_list_UNIV xs = (let c = card_UNIV (TYPE('a)) in if c = 0 then False else size (remdups xs) = c)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   110
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   111
lemma is_list_UNIV_iff: fixes xs :: "'a list"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   112
  shows "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   113
proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   114
  assume "is_list_UNIV xs"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   115
  hence c: "card_UNIV (TYPE('a)) > 0" and xs: "size (remdups xs) = card_UNIV (TYPE('a))"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   116
    unfolding is_list_UNIV_def by(simp_all add: Let_def split: split_if_asm)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   117
  from c have fin: "finite (UNIV :: 'a set)" by(auto simp add: card_UNIV_ge_0_finite_UNIV)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   118
  have "card (set (remdups xs)) = size (remdups xs)" by(subst distinct_card) auto
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   119
  also note set_remdups
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   120
  finally show "set xs = UNIV" using fin unfolding xs card_UNIV by-(rule card_eq_UNIV_imp_eq_UNIV)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   121
next
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   122
  assume xs: "set xs = UNIV"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   123
  from finite_set[of xs] have fin: "finite (UNIV :: 'a set)" unfolding xs .
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   124
  hence "card_UNIV (TYPE ('a)) \<noteq> 0" unfolding card_UNIV_neq_0_finite_UNIV .
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   125
  moreover have "size (remdups xs) = card (set (remdups xs))"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   126
    by(subst distinct_card) auto
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   127
  ultimately show "is_list_UNIV xs" using xs by(simp add: is_list_UNIV_def Let_def card_UNIV)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   128
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   129
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   130
lemma card_UNIV_eq_0_is_list_UNIV_False:
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   131
  assumes cU0: "card_UNIV x = 0"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   132
  shows "is_list_UNIV = (\<lambda>xs. False)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   133
proof(rule ext)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   134
  fix xs :: "'a list"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   135
  from cU0 have "\<not> finite (UNIV :: 'a set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   136
    by(auto simp only: card_UNIV_eq_0_infinite_UNIV)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   137
  moreover have "finite (set xs)" by(rule finite_set)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   138
  ultimately have "(UNIV :: 'a set) \<noteq> set xs" by(auto simp del: finite_set)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   139
  thus "is_list_UNIV xs = False" unfolding is_list_UNIV_iff by simp
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   140
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   141
29997
f6756c097c2d number_ring instances for numeral types
huffman
parents: 29629
diff changeset
   142
end
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   143
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   144
subsection {* Instantiations for @{text "card_UNIV"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   145
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   146
subsubsection {* @{typ "nat"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   147
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   148
instantiation nat :: card_UNIV begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   149
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   150
definition "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   151
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   152
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   153
  fix x :: "nat itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   154
  show "card_UNIV x = card (UNIV :: nat set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   155
    unfolding card_UNIV_nat_def by simp
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   156
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   157
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   158
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   159
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   160
subsubsection {* @{typ "int"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   161
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   162
instantiation int :: card_UNIV begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   163
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   164
definition "card_UNIV = (\<lambda>a :: int itself. 0)"
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   165
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   166
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   167
  fix x :: "int itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   168
  show "card_UNIV x = card (UNIV :: int set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   169
    unfolding card_UNIV_int_def by(simp add: infinite_UNIV_int)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   170
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   171
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   172
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   173
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   174
subsubsection {* @{typ "'a list"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   175
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   176
instantiation list :: (type) card_UNIV begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   177
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   178
definition "card_UNIV = (\<lambda>a :: 'a list itself. 0)"
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   179
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   180
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   181
  fix x :: "'a list itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   182
  show "card_UNIV x = card (UNIV :: 'a list set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   183
    unfolding card_UNIV_list_def by(simp add: infinite_UNIV_listI)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   184
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   185
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   186
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   187
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   188
subsubsection {* @{typ "unit"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   189
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   190
instantiation unit :: card_UNIV begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   191
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   192
definition "card_UNIV = (\<lambda>a :: unit itself. 1)"
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   193
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   194
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   195
  fix x :: "unit itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   196
  show "card_UNIV x = card (UNIV :: unit set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   197
    by(simp add: card_UNIV_unit_def card_UNIV_unit)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   198
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   199
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   200
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   201
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   202
subsubsection {* @{typ "bool"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   203
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   204
instantiation bool :: card_UNIV begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   205
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   206
definition "card_UNIV = (\<lambda>a :: bool itself. 2)"
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   207
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   208
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   209
  fix x :: "bool itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   210
  show "card_UNIV x = card (UNIV :: bool set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   211
    by(simp add: card_UNIV_bool_def card_UNIV_bool)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   212
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   213
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   214
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   215
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   216
subsubsection {* @{typ "char"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   217
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   218
lemma card_UNIV_char: "card (UNIV :: char set) = 256"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   219
proof -
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   220
  from enum_distinct
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   221
  have "card (set (Enum.enum :: char list)) = length (Enum.enum :: char list)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   222
    by (rule distinct_card)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   223
  also have "set Enum.enum = (UNIV :: char set)" by (auto intro: in_enum)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   224
  also note enum_chars
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   225
  finally show ?thesis by (simp add: chars_def)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   226
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   227
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   228
instantiation char :: card_UNIV begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   229
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   230
definition "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   231
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   232
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   233
  fix x :: "char itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   234
  show "card_UNIV x = card (UNIV :: char set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   235
    by(simp add: card_UNIV_char_def card_UNIV_char)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   236
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   237
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   238
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   239
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   240
subsubsection {* @{typ "'a \<times> 'b"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   241
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   242
instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   243
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   244
definition "card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   245
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   246
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   247
  fix x :: "('a \<times> 'b) itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   248
  show "card_UNIV x = card (UNIV :: ('a \<times> 'b) set)"
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   249
    by(simp add: card_UNIV_prod_def card_UNIV UNIV_Times_UNIV[symmetric] card_cartesian_product del: UNIV_Times_UNIV)
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   250
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   251
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   252
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   253
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   254
subsubsection {* @{typ "'a + 'b"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   255
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   256
instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   257
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   258
definition "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a + 'b) itself. 
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   259
  let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   260
  in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   261
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   262
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   263
  fix x :: "('a + 'b) itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   264
  show "card_UNIV x = card (UNIV :: ('a + 'b) set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   265
    by (auto simp add: card_UNIV_sum_def card_UNIV card_eq_0_iff UNIV_Plus_UNIV[symmetric] finite_Plus_iff Let_def card_Plus simp del: UNIV_Plus_UNIV dest!: card_ge_0_finite)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   266
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   267
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   268
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   269
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   270
subsubsection {* @{typ "'a \<Rightarrow> 'b"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   271
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   272
instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   273
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   274
definition "card_UNIV = 
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   275
  (\<lambda>a :: ('a \<Rightarrow> 'b) itself. let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   276
                            in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   277
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   278
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   279
  fix x :: "('a \<Rightarrow> 'b) itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   280
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   281
  { assume "0 < card (UNIV :: 'a set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   282
    and "0 < card (UNIV :: 'b set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   283
    hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   284
      by(simp_all only: card_ge_0_finite)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   285
    from finite_distinct_list[OF finb] obtain bs 
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   286
      where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   287
    from finite_distinct_list[OF fina] obtain as
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   288
      where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   289
    have cb: "card (UNIV :: 'b set) = length bs"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   290
      unfolding bs[symmetric] distinct_card[OF distb] ..
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   291
    have ca: "card (UNIV :: 'a set) = length as"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   292
      unfolding as[symmetric] distinct_card[OF dista] ..
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   293
    let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (Enum.n_lists (length as) bs)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   294
    have "UNIV = set ?xs"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   295
    proof(rule UNIV_eq_I)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   296
      fix f :: "'a \<Rightarrow> 'b"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   297
      from as have "f = the \<circ> map_of (zip as (map f as))"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   298
        by(auto simp add: map_of_zip_map intro: ext)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   299
      thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   300
    qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   301
    moreover have "distinct ?xs" unfolding distinct_map
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   302
    proof(intro conjI distinct_n_lists distb inj_onI)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   303
      fix xs ys :: "'b list"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   304
      assume xs: "xs \<in> set (Enum.n_lists (length as) bs)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   305
        and ys: "ys \<in> set (Enum.n_lists (length as) bs)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   306
        and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   307
      from xs ys have [simp]: "length xs = length as" "length ys = length as"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   308
        by(simp_all add: length_n_lists_elem)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   309
      have "map_of (zip as xs) = map_of (zip as ys)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   310
      proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   311
        fix x
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   312
        from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   313
          by(simp_all add: map_of_zip_is_Some[symmetric])
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   314
        with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   315
          by(auto dest: fun_cong[where x=x])
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   316
      qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   317
      with dista show "xs = ys" by(simp add: map_of_zip_inject)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   318
    qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   319
    hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   320
    moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   321
    ultimately have "card (UNIV :: ('a \<Rightarrow> 'b) set) = card (UNIV :: 'b set) ^ card (UNIV :: 'a set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   322
      using cb ca by simp }
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   323
  moreover {
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   324
    assume cb: "card (UNIV :: 'b set) = Suc 0"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   325
    then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   326
    have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   327
    proof(rule UNIV_eq_I)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   328
      fix x :: "'a \<Rightarrow> 'b"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   329
      { fix y
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   330
        have "x y \<in> UNIV" ..
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   331
        hence "x y = b" unfolding b by simp }
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   332
      thus "x \<in> {\<lambda>x. b}" by(auto intro: ext)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   333
    qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   334
    have "card (UNIV :: ('a \<Rightarrow> 'b) set) = Suc 0" unfolding eq by simp }
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   335
  ultimately show "card_UNIV x = card (UNIV :: ('a \<Rightarrow> 'b) set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   336
    unfolding card_UNIV_fun_def card_UNIV Let_def
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   337
    by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   338
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   339
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   340
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   341
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   342
subsubsection {* @{typ "'a option"} *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   343
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   344
instantiation option :: (card_UNIV) card_UNIV
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   345
begin
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   346
48052
b74766e4c11e tuned instantiations
Andreas Lochbihler
parents: 48051
diff changeset
   347
definition "card_UNIV = (\<lambda>a :: 'a option itself. let c = card_UNIV (TYPE('a)) in if c \<noteq> 0 then Suc c else 0)"
48051
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   348
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   349
instance proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   350
  fix x :: "'a option itself"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   351
  show "card_UNIV x = card (UNIV :: 'a option set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   352
    unfolding UNIV_option_conv
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   353
    by(auto simp add: card_UNIV_option_def card_UNIV card_eq_0_iff Let_def intro: inj_Some dest: finite_imageD)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   354
      (subst card_insert_disjoint, auto simp add: card_eq_0_iff card_image inj_Some intro: finite_imageI card_ge_0_finite)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   355
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   356
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   357
end
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   358
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   359
subsection {* Code setup for equality on sets *}
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   360
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   361
definition eq_set :: "'a :: card_UNIV set \<Rightarrow> 'a :: card_UNIV set \<Rightarrow> bool"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   362
where [simp, code del]: "eq_set = op ="
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   363
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   364
lemmas [code_unfold] = eq_set_def[symmetric]
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   365
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   366
lemma card_Compl:
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   367
  "finite A \<Longrightarrow> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   368
by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   369
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   370
lemma eq_set_code [code]:
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   371
  fixes xs ys :: "'a :: card_UNIV list"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   372
  defines "rhs \<equiv> 
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   373
  let n = card_UNIV TYPE('a)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   374
  in if n = 0 then False else 
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   375
        let xs' = remdups xs; ys' = remdups ys 
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   376
        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')"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   377
  shows "eq_set (List.coset xs) (set ys) \<longleftrightarrow> rhs" (is ?thesis1)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   378
  and "eq_set (set ys) (List.coset xs) \<longleftrightarrow> rhs" (is ?thesis2)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   379
  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)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   380
  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)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   381
proof -
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   382
  show ?thesis1 (is "?lhs \<longleftrightarrow> ?rhs")
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   383
  proof
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   384
    assume ?lhs thus ?rhs
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   385
      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)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   386
  next
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   387
    assume ?rhs
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   388
    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
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   389
    ultimately show ?lhs
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   390
      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)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   391
  qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   392
  thus ?thesis2 unfolding eq_set_def by blast
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   393
  show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   394
qed
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   395
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   396
(* test code setup *)
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   397
value [code] "List.coset [True] = set [False] \<and> set [] = List.coset [True, False]"
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   398
53a0df441e20 unify Card_Univ and Cardinality
Andreas Lochbihler
parents: 47221
diff changeset
   399
end