src/HOL/Library/Countable.thy
author haftmann
Thu, 01 Jul 2010 16:54:44 +0200
changeset 37678 0040bafffdef
parent 37652 6aa09d2a6cf9
child 37715 44b27ea94a16
permissions -rw-r--r--
"prod" and "sum" replace "*" and "+" respectively
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Countable.thy
26350
a170a190c5d3 adjusted authorship
haftmann
parents: 26243
diff changeset
     2
    Author:     Alexander Krauss, TU Muenchen
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
     3
*)
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
     4
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
     5
header {* Encoding (almost) everything into natural numbers *}
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
     6
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
     7
theory Countable
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
     8
imports Main Rat Nat_Bijection
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
     9
begin
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    10
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    11
subsection {* The class of countable types *}
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    12
29797
08ef36ed2f8a handling type classes without parameters
haftmann
parents: 29511
diff changeset
    13
class countable =
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    14
  assumes ex_inj: "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    15
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    16
lemma countable_classI:
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    17
  fixes f :: "'a \<Rightarrow> nat"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    18
  assumes "\<And>x y. f x = f y \<Longrightarrow> x = y"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    19
  shows "OFCLASS('a, countable_class)"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    20
proof (intro_classes, rule exI)
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    21
  show "inj f"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    22
    by (rule injI [OF assms]) assumption
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    23
qed
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    24
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    25
26585
3bf2ebb7148e fix spelling
huffman
parents: 26580
diff changeset
    26
subsection {* Conversion functions *}
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    27
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    28
definition to_nat :: "'a\<Colon>countable \<Rightarrow> nat" where
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    29
  "to_nat = (SOME f. inj f)"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    30
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    31
definition from_nat :: "nat \<Rightarrow> 'a\<Colon>countable" where
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    32
  "from_nat = inv (to_nat \<Colon> 'a \<Rightarrow> nat)"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    33
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    34
lemma inj_to_nat [simp]: "inj to_nat"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    35
  by (rule exE_some [OF ex_inj]) (simp add: to_nat_def)
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    36
29910
623c9c20966b add lemma surj_from_nat
huffman
parents: 29880
diff changeset
    37
lemma surj_from_nat [simp]: "surj from_nat"
623c9c20966b add lemma surj_from_nat
huffman
parents: 29880
diff changeset
    38
  unfolding from_nat_def by (simp add: inj_imp_surj_inv)
623c9c20966b add lemma surj_from_nat
huffman
parents: 29880
diff changeset
    39
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    40
lemma to_nat_split [simp]: "to_nat x = to_nat y \<longleftrightarrow> x = y"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    41
  using injD [OF inj_to_nat] by auto
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    42
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    43
lemma from_nat_to_nat [simp]:
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    44
  "from_nat (to_nat x) = x"
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    45
  by (simp add: from_nat_def)
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    46
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    47
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    48
subsection {* Countable types *}
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    49
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    50
instance nat :: countable
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    51
  by (rule countable_classI [of "id"]) simp
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    52
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    53
subclass (in finite) countable
28823
dcbef866c9e2 tuned unfold_locales invocation
haftmann
parents: 27487
diff changeset
    54
proof
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    55
  have "finite (UNIV\<Colon>'a set)" by (rule finite_UNIV)
31992
f8aed98faae7 More about gcd/lcm, and some cleaning up
nipkow
parents: 30663
diff changeset
    56
  with finite_conv_nat_seg_image [of "UNIV::'a set"]
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    57
  obtain n and f :: "nat \<Rightarrow> 'a" 
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    58
    where "UNIV = f ` {i. i < n}" by auto
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    59
  then have "surj f" unfolding surj_def by auto
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    60
  then have "inj (inv f)" by (rule surj_imp_inj_inv)
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    61
  then show "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat" by (rule exI[of inj])
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    62
qed
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    63
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    64
text {* Pairs *}
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    65
37678
0040bafffdef "prod" and "sum" replace "*" and "+" respectively
haftmann
parents: 37652
diff changeset
    66
instance prod :: (countable, countable) countable
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    67
  by (rule countable_classI [of "\<lambda>(x, y). prod_encode (to_nat x, to_nat y)"])
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    68
    (auto simp add: prod_encode_eq)
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    69
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    70
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    71
text {* Sums *}
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    72
37678
0040bafffdef "prod" and "sum" replace "*" and "+" respectively
haftmann
parents: 37652
diff changeset
    73
instance sum :: (countable, countable) countable
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    74
  by (rule countable_classI [of "(\<lambda>x. case x of Inl a \<Rightarrow> to_nat (False, to_nat a)
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    75
                                     | Inr b \<Rightarrow> to_nat (True, to_nat b))"])
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    76
    (simp split: sum.split_asm)
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    77
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    78
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    79
text {* Integers *}
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    80
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    81
instance int :: countable
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    82
  by (rule countable_classI [of "int_encode"])
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    83
    (simp add: int_encode_eq)
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    84
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    85
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    86
text {* Options *}
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    87
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    88
instance option :: (countable) countable
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    89
  by (rule countable_classI [of "option_case 0 (Suc \<circ> to_nat)"])
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    90
    (simp split: option.split_asm)
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    91
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    92
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    93
text {* Lists *}
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    94
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    95
instance list :: (countable) countable
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    96
  by (rule countable_classI [of "list_encode \<circ> map to_nat"])
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
    97
    (simp add: list_encode_eq)
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
    98
26243
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
    99
37652
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   100
text {* Further *}
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   101
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   102
instance String.literal :: countable
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   103
  by (rule countable_classI [of "String.literal_case to_nat"])
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   104
   (auto split: String.literal.splits)
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   105
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   106
instantiation typerep :: countable
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   107
begin
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   108
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   109
fun to_nat_typerep :: "typerep \<Rightarrow> nat" where
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   110
  "to_nat_typerep (Typerep.Typerep c ts) = to_nat (to_nat c, to_nat (map to_nat_typerep ts))"
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   111
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   112
instance proof (rule countable_classI)
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   113
  fix t t' :: typerep and ts
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   114
  have "(\<forall>t'. to_nat_typerep t = to_nat_typerep t' \<longrightarrow> t = t')
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   115
    \<and> (\<forall>ts'. map to_nat_typerep ts = map to_nat_typerep ts' \<longrightarrow> ts = ts')"
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   116
  proof (induct rule: typerep.induct)
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   117
    case (Typerep c ts) show ?case
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   118
    proof (rule allI, rule impI)
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   119
      fix t'
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   120
      assume hyp: "to_nat_typerep (Typerep.Typerep c ts) = to_nat_typerep t'"
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   121
      then obtain c' ts' where t': "t' = (Typerep.Typerep c' ts')"
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   122
        by (cases t') auto
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   123
      with Typerep hyp have "c = c'" and "ts = ts'" by simp_all
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   124
      with t' show "Typerep.Typerep c ts = t'" by simp
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   125
    qed
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   126
  next
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   127
    case Nil_typerep then show ?case by simp
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   128
  next
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   129
    case (Cons_typerep t ts) then show ?case by auto
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   130
  qed
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   131
  then have "to_nat_typerep t = to_nat_typerep t' \<Longrightarrow> t = t'" by auto
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   132
  moreover assume "to_nat_typerep t = to_nat_typerep t'"
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   133
  ultimately show "t = t'" by simp
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   134
qed
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   135
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   136
end
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   137
6aa09d2a6cf9 added literal and typerep instances
haftmann
parents: 37388
diff changeset
   138
26243
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   139
text {* Functions *}
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   140
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   141
instance "fun" :: (finite, countable) countable
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   142
proof
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   143
  obtain xs :: "'a list" where xs: "set xs = UNIV"
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   144
    using finite_list [OF finite_UNIV] ..
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   145
  show "\<exists>to_nat::('a \<Rightarrow> 'b) \<Rightarrow> nat. inj to_nat"
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   146
  proof
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   147
    show "inj (\<lambda>f. to_nat (map f xs))"
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   148
      by (rule injI, simp add: xs expand_fun_eq)
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   149
  qed
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   150
qed
69592314f977 instance fun :: (finite, countable) countable
huffman
parents: 26169
diff changeset
   151
29880
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   152
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   153
subsection {* The Rationals are Countably Infinite *}
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   154
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   155
definition nat_to_rat_surj :: "nat \<Rightarrow> rat" where
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
   156
"nat_to_rat_surj n = (let (a,b) = prod_decode n
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
   157
                      in Fract (int_decode a) (int_decode b))"
29880
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   158
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   159
lemma surj_nat_to_rat_surj: "surj nat_to_rat_surj"
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   160
unfolding surj_def
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   161
proof
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   162
  fix r::rat
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   163
  show "\<exists>n. r = nat_to_rat_surj n"
35374
af1c8c15340e adjusted to cs. e4a7947e02b8
haftmann
parents: 33640
diff changeset
   164
  proof (cases r)
af1c8c15340e adjusted to cs. e4a7947e02b8
haftmann
parents: 33640
diff changeset
   165
    fix i j assume [simp]: "r = Fract i j" and "j > 0"
35700
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
   166
    have "r = (let m = int_encode i; n = int_encode j
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
   167
               in nat_to_rat_surj(prod_encode (m,n)))"
951974ce903e new theory Library/Nat_Bijection.thy
huffman
parents: 35374
diff changeset
   168
      by (simp add: Let_def nat_to_rat_surj_def)
29880
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   169
    thus "\<exists>n. r = nat_to_rat_surj n" by(auto simp:Let_def)
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   170
  qed
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   171
qed
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   172
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   173
lemma Rats_eq_range_nat_to_rat_surj: "\<rat> = range nat_to_rat_surj"
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   174
by (simp add: Rats_def surj_nat_to_rat_surj surj_range)
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   175
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   176
context field_char_0
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   177
begin
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   178
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   179
lemma Rats_eq_range_of_rat_o_nat_to_rat_surj:
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   180
  "\<rat> = range (of_rat o nat_to_rat_surj)"
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   181
using surj_nat_to_rat_surj
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   182
by (auto simp: Rats_def image_def surj_def)
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   183
   (blast intro: arg_cong[where f = of_rat])
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   184
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   185
lemma surj_of_rat_nat_to_rat_surj:
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   186
  "r\<in>\<rat> \<Longrightarrow> \<exists>n. r = of_rat(nat_to_rat_surj n)"
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   187
by(simp add: Rats_eq_range_of_rat_o_nat_to_rat_surj image_def)
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   188
26169
73027318f9ba added theory for countable types
haftmann
parents:
diff changeset
   189
end
29880
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   190
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   191
instance rat :: countable
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   192
proof
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   193
  show "\<exists>to_nat::rat \<Rightarrow> nat. inj to_nat"
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   194
  proof
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   195
    have "surj nat_to_rat_surj"
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   196
      by (rule surj_nat_to_rat_surj)
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   197
    then show "inj (inv nat_to_rat_surj)"
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   198
      by (rule surj_imp_inj_inv)
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   199
  qed
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   200
qed
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   201
3dee8ff45d3d move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents: 29797
diff changeset
   202
end