src/HOL/Library/Card_Univ.thy
author Andreas Lochbihler
Tue, 29 May 2012 15:31:58 +0200
changeset 48028 a5377f6d9f14
child 48032 ba9e0f5b686d
permissions -rw-r--r--
move FinFuns from AFP to repository
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48028
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
     1
(* Author: Andreas Lochbihler, KIT *)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
     2
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
     3
header {* A type class for computing the cardinality of a type's universe *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
     4
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
     5
theory Card_Univ imports Main begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
     6
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
     7
subsection {* A type class for computing the cardinality of a type's universe *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
     8
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
     9
class card_UNIV = 
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    10
  fixes card_UNIV :: "'a itself \<Rightarrow> nat"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    11
  assumes card_UNIV: "card_UNIV x = card (UNIV :: 'a set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    12
begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    13
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    14
lemma card_UNIV_neq_0_finite_UNIV:
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    15
  "card_UNIV x \<noteq> 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    16
by(simp add: card_UNIV card_eq_0_iff)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    17
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    18
lemma card_UNIV_ge_0_finite_UNIV:
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    19
  "card_UNIV x > 0 \<longleftrightarrow> finite (UNIV :: 'a set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    20
by(auto simp add: card_UNIV intro: card_ge_0_finite finite_UNIV_card_ge_0)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    21
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    22
lemma card_UNIV_eq_0_infinite_UNIV:
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    23
  "card_UNIV x = 0 \<longleftrightarrow> \<not> finite (UNIV :: 'a set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    24
by(simp add: card_UNIV card_eq_0_iff)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    25
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    26
definition is_list_UNIV :: "'a list \<Rightarrow> bool"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    27
where "is_list_UNIV xs = (let c = card_UNIV (TYPE('a)) in if c = 0 then False else size (remdups xs) = c)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    28
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    29
lemma is_list_UNIV_iff:
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    30
  fixes xs :: "'a list"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    31
  shows "is_list_UNIV xs \<longleftrightarrow> set xs = UNIV"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    32
proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    33
  assume "is_list_UNIV xs"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    34
  hence c: "card_UNIV (TYPE('a)) > 0" and xs: "size (remdups xs) = card_UNIV (TYPE('a))"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    35
    unfolding is_list_UNIV_def by(simp_all add: Let_def split: split_if_asm)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    36
  from c have fin: "finite (UNIV :: 'a set)" by(auto simp add: card_UNIV_ge_0_finite_UNIV)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    37
  have "card (set (remdups xs)) = size (remdups xs)" by(subst distinct_card) auto
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    38
  also note set_remdups
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    39
  finally show "set xs = UNIV" using fin unfolding xs card_UNIV by-(rule card_eq_UNIV_imp_eq_UNIV)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    40
next
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    41
  assume xs: "set xs = UNIV"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    42
  from finite_set[of xs] have fin: "finite (UNIV :: 'a set)" unfolding xs .
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    43
  hence "card_UNIV (TYPE ('a)) \<noteq> 0" unfolding card_UNIV_neq_0_finite_UNIV .
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    44
  moreover have "size (remdups xs) = card (set (remdups xs))"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    45
    by(subst distinct_card) auto
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    46
  ultimately show "is_list_UNIV xs" using xs by(simp add: is_list_UNIV_def Let_def card_UNIV)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    47
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    48
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    49
lemma card_UNIV_eq_0_is_list_UNIV_False:
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    50
  assumes cU0: "card_UNIV x = 0"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    51
  shows "is_list_UNIV = (\<lambda>xs. False)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    52
proof(rule ext)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    53
  fix xs :: "'a list"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    54
  from cU0 have "\<not> finite (UNIV :: 'a set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    55
    by(auto simp only: card_UNIV_eq_0_infinite_UNIV)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    56
  moreover have "finite (set xs)" by(rule finite_set)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    57
  ultimately have "(UNIV :: 'a set) \<noteq> set xs" by(auto simp del: finite_set)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    58
  thus "is_list_UNIV xs = False" unfolding is_list_UNIV_iff by simp
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    59
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    60
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    61
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    62
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    63
subsection {* Instantiations for @{text "card_UNIV"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    64
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    65
subsubsection {* @{typ "nat"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    66
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    67
instantiation nat :: card_UNIV begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    68
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    69
definition card_UNIV_nat_def:
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    70
  "card_UNIV_class.card_UNIV = (\<lambda>a :: nat itself. 0)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    71
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    72
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    73
  fix x :: "nat itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    74
  show "card_UNIV x = card (UNIV :: nat set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    75
    unfolding card_UNIV_nat_def by simp
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    76
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    77
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    78
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    79
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    80
subsubsection {* @{typ "int"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    81
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    82
instantiation int :: card_UNIV begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    83
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    84
definition card_UNIV_int_def:
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    85
  "card_UNIV_class.card_UNIV = (\<lambda>a :: int itself. 0)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    86
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    87
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    88
  fix x :: "int itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    89
  show "card_UNIV x = card (UNIV :: int set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    90
    unfolding card_UNIV_int_def by(simp add: infinite_UNIV_int)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    91
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    92
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    93
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    94
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    95
subsubsection {* @{typ "'a list"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    96
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    97
instantiation list :: (type) card_UNIV begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    98
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
    99
definition card_UNIV_list_def:
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   100
  "card_UNIV_class.card_UNIV = (\<lambda>a :: 'a list itself. 0)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   101
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   102
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   103
  fix x :: "'a list itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   104
  show "card_UNIV x = card (UNIV :: 'a list set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   105
    unfolding card_UNIV_list_def by(simp add: infinite_UNIV_listI)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   106
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   107
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   108
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   109
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   110
subsubsection {* @{typ "unit"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   111
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   112
lemma card_UNIV_unit: "card (UNIV :: unit set) = 1"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   113
  unfolding UNIV_unit by simp
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   114
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   115
instantiation unit :: card_UNIV begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   116
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   117
definition card_UNIV_unit_def: 
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   118
  "card_UNIV_class.card_UNIV = (\<lambda>a :: unit itself. 1)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   119
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   120
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   121
  fix x :: "unit itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   122
  show "card_UNIV x = card (UNIV :: unit set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   123
    by(simp add: card_UNIV_unit_def card_UNIV_unit)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   124
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   125
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   126
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   127
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   128
subsubsection {* @{typ "bool"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   129
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   130
lemma card_UNIV_bool: "card (UNIV :: bool set) = 2"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   131
  unfolding UNIV_bool by simp
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   132
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   133
instantiation bool :: card_UNIV begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   134
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   135
definition card_UNIV_bool_def: 
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   136
  "card_UNIV_class.card_UNIV = (\<lambda>a :: bool itself. 2)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   137
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   138
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   139
  fix x :: "bool itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   140
  show "card_UNIV x = card (UNIV :: bool set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   141
    by(simp add: card_UNIV_bool_def card_UNIV_bool)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   142
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   143
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   144
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   145
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   146
subsubsection {* @{typ "char"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   147
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   148
lemma card_UNIV_char: "card (UNIV :: char set) = 256"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   149
proof -
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   150
  from enum_distinct
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   151
  have "card (set (Enum.enum :: char list)) = length (Enum.enum :: char list)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   152
    by (rule distinct_card)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   153
  also have "set Enum.enum = (UNIV :: char set)" by (auto intro: in_enum)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   154
  also note enum_chars
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   155
  finally show ?thesis by (simp add: chars_def)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   156
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   157
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   158
instantiation char :: card_UNIV begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   159
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   160
definition card_UNIV_char_def: 
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   161
  "card_UNIV_class.card_UNIV = (\<lambda>a :: char itself. 256)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   162
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   163
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   164
  fix x :: "char itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   165
  show "card_UNIV x = card (UNIV :: char set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   166
    by(simp add: card_UNIV_char_def card_UNIV_char)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   167
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   168
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   169
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   170
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   171
subsubsection {* @{typ "'a \<times> 'b"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   172
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   173
instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   174
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   175
definition card_UNIV_product_def: 
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   176
  "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a \<times> 'b) itself. card_UNIV (TYPE('a)) * card_UNIV (TYPE('b)))"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   177
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   178
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   179
  fix x :: "('a \<times> 'b) itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   180
  show "card_UNIV x = card (UNIV :: ('a \<times> 'b) set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   181
    by(simp add: card_UNIV_product_def card_UNIV UNIV_Times_UNIV[symmetric] card_cartesian_product del: UNIV_Times_UNIV)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   182
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   183
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   184
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   185
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   186
subsubsection {* @{typ "'a + 'b"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   187
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   188
instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   189
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   190
definition card_UNIV_sum_def: 
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   191
  "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a + 'b) itself. let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   192
                           in if ca \<noteq> 0 \<and> cb \<noteq> 0 then ca + cb else 0)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   193
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   194
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   195
  fix x :: "('a + 'b) itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   196
  show "card_UNIV x = card (UNIV :: ('a + 'b) set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   197
    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)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   198
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   199
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   200
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   201
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   202
subsubsection {* @{typ "'a \<Rightarrow> 'b"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   203
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   204
instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   205
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   206
definition card_UNIV_fun_def: 
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   207
  "card_UNIV_class.card_UNIV = (\<lambda>a :: ('a \<Rightarrow> 'b) itself. let ca = card_UNIV (TYPE('a)); cb = card_UNIV (TYPE('b))
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   208
                           in if ca \<noteq> 0 \<and> cb \<noteq> 0 \<or> cb = 1 then cb ^ ca else 0)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   209
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   210
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   211
  fix x :: "('a \<Rightarrow> 'b) itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   212
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   213
  { assume "0 < card (UNIV :: 'a set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   214
    and "0 < card (UNIV :: 'b set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   215
    hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   216
      by(simp_all only: card_ge_0_finite)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   217
    from finite_distinct_list[OF finb] obtain bs 
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   218
      where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   219
    from finite_distinct_list[OF fina] obtain as
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   220
      where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   221
    have cb: "card (UNIV :: 'b set) = length bs"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   222
      unfolding bs[symmetric] distinct_card[OF distb] ..
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   223
    have ca: "card (UNIV :: 'a set) = length as"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   224
      unfolding as[symmetric] distinct_card[OF dista] ..
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   225
    let ?xs = "map (\<lambda>ys. the o map_of (zip as ys)) (Enum.n_lists (length as) bs)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   226
    have "UNIV = set ?xs"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   227
    proof(rule UNIV_eq_I)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   228
      fix f :: "'a \<Rightarrow> 'b"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   229
      from as have "f = the \<circ> map_of (zip as (map f as))"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   230
        by(auto simp add: map_of_zip_map intro: ext)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   231
      thus "f \<in> set ?xs" using bs by(auto simp add: set_n_lists)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   232
    qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   233
    moreover have "distinct ?xs" unfolding distinct_map
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   234
    proof(intro conjI distinct_n_lists distb inj_onI)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   235
      fix xs ys :: "'b list"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   236
      assume xs: "xs \<in> set (Enum.n_lists (length as) bs)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   237
        and ys: "ys \<in> set (Enum.n_lists (length as) bs)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   238
        and eq: "the \<circ> map_of (zip as xs) = the \<circ> map_of (zip as ys)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   239
      from xs ys have [simp]: "length xs = length as" "length ys = length as"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   240
        by(simp_all add: length_n_lists_elem)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   241
      have "map_of (zip as xs) = map_of (zip as ys)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   242
      proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   243
        fix x
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   244
        from as bs have "\<exists>y. map_of (zip as xs) x = Some y" "\<exists>y. map_of (zip as ys) x = Some y"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   245
          by(simp_all add: map_of_zip_is_Some[symmetric])
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   246
        with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   247
          by(auto dest: fun_cong[where x=x])
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   248
      qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   249
      with dista show "xs = ys" by(simp add: map_of_zip_inject)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   250
    qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   251
    hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   252
    moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   253
    ultimately have "card (UNIV :: ('a \<Rightarrow> 'b) set) = card (UNIV :: 'b set) ^ card (UNIV :: 'a set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   254
      using cb ca by simp }
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   255
  moreover {
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   256
    assume cb: "card (UNIV :: 'b set) = Suc 0"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   257
    then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   258
    have eq: "UNIV = {\<lambda>x :: 'a. b ::'b}"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   259
    proof(rule UNIV_eq_I)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   260
      fix x :: "'a \<Rightarrow> 'b"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   261
      { fix y
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   262
        have "x y \<in> UNIV" ..
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   263
        hence "x y = b" unfolding b by simp }
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   264
      thus "x \<in> {\<lambda>x. b}" by(auto intro: ext)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   265
    qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   266
    have "card (UNIV :: ('a \<Rightarrow> 'b) set) = Suc 0" unfolding eq by simp }
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   267
  ultimately show "card_UNIV x = card (UNIV :: ('a \<Rightarrow> 'b) set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   268
    unfolding card_UNIV_fun_def card_UNIV Let_def
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   269
    by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   270
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   271
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   272
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   273
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   274
subsubsection {* @{typ "'a option"} *}
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   275
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   276
instantiation option :: (card_UNIV) card_UNIV
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   277
begin
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   278
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   279
definition card_UNIV_option_def: 
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   280
  "card_UNIV_class.card_UNIV = (\<lambda>a :: 'a option itself. let c = card_UNIV (TYPE('a))
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   281
                           in if c \<noteq> 0 then Suc c else 0)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   282
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   283
instance proof
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   284
  fix x :: "'a option itself"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   285
  show "card_UNIV x = card (UNIV :: 'a option set)"
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   286
    unfolding UNIV_option_conv
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   287
    by(auto simp add: card_UNIV_option_def card_UNIV card_eq_0_iff Let_def intro: inj_Some dest: finite_imageD)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   288
      (subst card_insert_disjoint, auto simp add: card_eq_0_iff card_image inj_Some intro: finite_imageI card_ge_0_finite)
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   289
qed
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   290
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   291
end
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   292
a5377f6d9f14 move FinFuns from AFP to repository
Andreas Lochbihler
parents:
diff changeset
   293
end