src/HOL/Library/Countable.thy
author hoelzl
Thu Nov 12 17:21:51 2009 +0100 (2009-11-12)
changeset 33640 0d82107dc07a
parent 31992 f8aed98faae7
child 35374 af1c8c15340e
permissions -rw-r--r--
Remove map_compose, replaced by map_map
haftmann@26169
     1
(*  Title:      HOL/Library/Countable.thy
haftmann@26350
     2
    Author:     Alexander Krauss, TU Muenchen
haftmann@26169
     3
*)
haftmann@26169
     4
haftmann@26169
     5
header {* Encoding (almost) everything into natural numbers *}
haftmann@26169
     6
haftmann@26169
     7
theory Countable
huffman@29880
     8
imports
huffman@29880
     9
  "~~/src/HOL/List"
huffman@29880
    10
  "~~/src/HOL/Hilbert_Choice"
huffman@29880
    11
  "~~/src/HOL/Nat_Int_Bij"
huffman@29880
    12
  "~~/src/HOL/Rational"
haftmann@30663
    13
  Main
haftmann@26169
    14
begin
haftmann@26169
    15
haftmann@26169
    16
subsection {* The class of countable types *}
haftmann@26169
    17
haftmann@29797
    18
class countable =
haftmann@26169
    19
  assumes ex_inj: "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat"
haftmann@26169
    20
haftmann@26169
    21
lemma countable_classI:
haftmann@26169
    22
  fixes f :: "'a \<Rightarrow> nat"
haftmann@26169
    23
  assumes "\<And>x y. f x = f y \<Longrightarrow> x = y"
haftmann@26169
    24
  shows "OFCLASS('a, countable_class)"
haftmann@26169
    25
proof (intro_classes, rule exI)
haftmann@26169
    26
  show "inj f"
haftmann@26169
    27
    by (rule injI [OF assms]) assumption
haftmann@26169
    28
qed
haftmann@26169
    29
haftmann@26169
    30
huffman@26585
    31
subsection {* Conversion functions *}
haftmann@26169
    32
haftmann@26169
    33
definition to_nat :: "'a\<Colon>countable \<Rightarrow> nat" where
haftmann@26169
    34
  "to_nat = (SOME f. inj f)"
haftmann@26169
    35
haftmann@26169
    36
definition from_nat :: "nat \<Rightarrow> 'a\<Colon>countable" where
haftmann@26169
    37
  "from_nat = inv (to_nat \<Colon> 'a \<Rightarrow> nat)"
haftmann@26169
    38
haftmann@26169
    39
lemma inj_to_nat [simp]: "inj to_nat"
haftmann@26169
    40
  by (rule exE_some [OF ex_inj]) (simp add: to_nat_def)
haftmann@26169
    41
huffman@29910
    42
lemma surj_from_nat [simp]: "surj from_nat"
huffman@29910
    43
  unfolding from_nat_def by (simp add: inj_imp_surj_inv)
huffman@29910
    44
haftmann@26169
    45
lemma to_nat_split [simp]: "to_nat x = to_nat y \<longleftrightarrow> x = y"
haftmann@26169
    46
  using injD [OF inj_to_nat] by auto
haftmann@26169
    47
haftmann@26169
    48
lemma from_nat_to_nat [simp]:
haftmann@26169
    49
  "from_nat (to_nat x) = x"
haftmann@26169
    50
  by (simp add: from_nat_def)
haftmann@26169
    51
haftmann@26169
    52
haftmann@26169
    53
subsection {* Countable types *}
haftmann@26169
    54
haftmann@26169
    55
instance nat :: countable
haftmann@26169
    56
  by (rule countable_classI [of "id"]) simp 
haftmann@26169
    57
haftmann@26169
    58
subclass (in finite) countable
haftmann@28823
    59
proof
haftmann@26169
    60
  have "finite (UNIV\<Colon>'a set)" by (rule finite_UNIV)
nipkow@31992
    61
  with finite_conv_nat_seg_image [of "UNIV::'a set"]
haftmann@26169
    62
  obtain n and f :: "nat \<Rightarrow> 'a" 
haftmann@26169
    63
    where "UNIV = f ` {i. i < n}" by auto
haftmann@26169
    64
  then have "surj f" unfolding surj_def by auto
haftmann@26169
    65
  then have "inj (inv f)" by (rule surj_imp_inj_inv)
haftmann@26169
    66
  then show "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat" by (rule exI[of inj])
haftmann@26169
    67
qed
haftmann@26169
    68
haftmann@26169
    69
text {* Pairs *}
haftmann@26169
    70
haftmann@26169
    71
primrec sum :: "nat \<Rightarrow> nat"
haftmann@26169
    72
where
haftmann@26169
    73
  "sum 0 = 0"
haftmann@26169
    74
| "sum (Suc n) = Suc n + sum n"
haftmann@26169
    75
haftmann@26169
    76
lemma sum_arith: "sum n = n * Suc n div 2"
haftmann@26169
    77
  by (induct n) auto
haftmann@26169
    78
haftmann@26169
    79
lemma sum_mono: "n \<ge> m \<Longrightarrow> sum n \<ge> sum m"
haftmann@26169
    80
  by (induct n m rule: diff_induct) auto
haftmann@26169
    81
haftmann@26169
    82
definition
haftmann@26169
    83
  "pair_encode = (\<lambda>(m, n). sum (m + n) + m)"
haftmann@26169
    84
haftmann@26169
    85
lemma inj_pair_cencode: "inj pair_encode"
haftmann@26169
    86
  unfolding pair_encode_def
haftmann@26169
    87
proof (rule injI, simp only: split_paired_all split_conv)
haftmann@26169
    88
  fix a b c d
haftmann@26169
    89
  assume eq: "sum (a + b) + a = sum (c + d) + c"
haftmann@26169
    90
  have "a + b = c + d \<or> a + b \<ge> Suc (c + d) \<or> c + d \<ge> Suc (a + b)" by arith
haftmann@26169
    91
  then
haftmann@26169
    92
  show "(a, b) = (c, d)"
haftmann@26169
    93
  proof (elim disjE)
haftmann@26169
    94
    assume sumeq: "a + b = c + d"
haftmann@26169
    95
    then have "a = c" using eq by auto
haftmann@26169
    96
    moreover from sumeq this have "b = d" by auto
haftmann@26169
    97
    ultimately show ?thesis by simp
haftmann@26169
    98
  next
haftmann@26169
    99
    assume "a + b \<ge> Suc (c + d)"
haftmann@26169
   100
    from sum_mono[OF this] eq
haftmann@26169
   101
    show ?thesis by auto
haftmann@26169
   102
  next
haftmann@26169
   103
    assume "c + d \<ge> Suc (a + b)"
haftmann@26169
   104
    from sum_mono[OF this] eq
haftmann@26169
   105
    show ?thesis by auto
haftmann@26169
   106
  qed
haftmann@26169
   107
qed
haftmann@26169
   108
haftmann@26169
   109
instance "*" :: (countable, countable) countable
haftmann@26169
   110
by (rule countable_classI [of "\<lambda>(x, y). pair_encode (to_nat x, to_nat y)"])
haftmann@26169
   111
  (auto dest: injD [OF inj_pair_cencode] injD [OF inj_to_nat])
haftmann@26169
   112
haftmann@26169
   113
haftmann@26169
   114
text {* Sums *}
haftmann@26169
   115
haftmann@26169
   116
instance "+":: (countable, countable) countable
haftmann@26169
   117
  by (rule countable_classI [of "(\<lambda>x. case x of Inl a \<Rightarrow> to_nat (False, to_nat a)
haftmann@26169
   118
                                     | Inr b \<Rightarrow> to_nat (True, to_nat b))"])
haftmann@26169
   119
    (auto split:sum.splits)
haftmann@26169
   120
haftmann@26169
   121
haftmann@26169
   122
text {* Integers *}
haftmann@26169
   123
haftmann@26169
   124
lemma int_cases: "(i::int) = 0 \<or> i < 0 \<or> i > 0"
haftmann@26169
   125
by presburger
haftmann@26169
   126
haftmann@26169
   127
lemma int_pos_neg_zero:
haftmann@26169
   128
  obtains (zero) "(z::int) = 0" "sgn z = 0" "abs z = 0"
haftmann@26169
   129
  | (pos) n where "z = of_nat n" "sgn z = 1" "abs z = of_nat n"
haftmann@26169
   130
  | (neg) n where "z = - (of_nat n)" "sgn z = -1" "abs z = of_nat n"
krauss@26580
   131
apply atomize_elim
haftmann@26169
   132
apply (insert int_cases[of z])
haftmann@26169
   133
apply (auto simp:zsgn_def)
haftmann@26169
   134
apply (rule_tac x="nat (-z)" in exI, simp)
haftmann@26169
   135
apply (rule_tac x="nat z" in exI, simp)
haftmann@26169
   136
done
haftmann@26169
   137
haftmann@26169
   138
instance int :: countable
haftmann@26169
   139
proof (rule countable_classI [of "(\<lambda>i. to_nat (nat (sgn i + 1), nat (abs i)))"], 
haftmann@26169
   140
    auto dest: injD [OF inj_to_nat])
haftmann@26169
   141
  fix x y 
haftmann@26169
   142
  assume a: "nat (sgn x + 1) = nat (sgn y + 1)" "nat (abs x) = nat (abs y)"
haftmann@26169
   143
  show "x = y"
haftmann@26169
   144
  proof (cases rule: int_pos_neg_zero[of x])
haftmann@26169
   145
    case zero 
haftmann@26169
   146
    with a show "x = y" by (cases rule: int_pos_neg_zero[of y]) auto
haftmann@26169
   147
  next
haftmann@26169
   148
    case (pos n)
haftmann@26169
   149
    with a show "x = y" by (cases rule: int_pos_neg_zero[of y]) auto
haftmann@26169
   150
  next
haftmann@26169
   151
    case (neg n)
haftmann@26169
   152
    with a show "x = y" by (cases rule: int_pos_neg_zero[of y]) auto
haftmann@26169
   153
  qed
haftmann@26169
   154
qed
haftmann@26169
   155
haftmann@26169
   156
haftmann@26169
   157
text {* Options *}
haftmann@26169
   158
haftmann@26169
   159
instance option :: (countable) countable
haftmann@26169
   160
by (rule countable_classI[of "\<lambda>x. case x of None \<Rightarrow> 0
haftmann@26169
   161
                                     | Some y \<Rightarrow> Suc (to_nat y)"])
haftmann@26169
   162
 (auto split:option.splits)
haftmann@26169
   163
haftmann@26169
   164
haftmann@26169
   165
text {* Lists *}
haftmann@26169
   166
haftmann@26169
   167
lemma from_nat_to_nat_map [simp]: "map from_nat (map to_nat xs) = xs"
hoelzl@33640
   168
  by (simp add: comp_def)
haftmann@26169
   169
haftmann@26169
   170
primrec
haftmann@26169
   171
  list_encode :: "'a\<Colon>countable list \<Rightarrow> nat"
haftmann@26169
   172
where
haftmann@26169
   173
  "list_encode [] = 0"
haftmann@26169
   174
| "list_encode (x#xs) = Suc (to_nat (x, list_encode xs))"
haftmann@26169
   175
haftmann@26169
   176
instance list :: (countable) countable
haftmann@26169
   177
proof (rule countable_classI [of "list_encode"])
haftmann@26169
   178
  fix xs ys :: "'a list"
haftmann@26169
   179
  assume cenc: "list_encode xs = list_encode ys"
haftmann@26169
   180
  then show "xs = ys"
haftmann@26169
   181
  proof (induct xs arbitrary: ys)
haftmann@26169
   182
    case (Nil ys)
haftmann@26169
   183
    with cenc show ?case by (cases ys, auto)
haftmann@26169
   184
  next
haftmann@26169
   185
    case (Cons x xs' ys)
haftmann@26169
   186
    thus ?case by (cases ys) auto
haftmann@26169
   187
  qed
haftmann@26169
   188
qed
haftmann@26169
   189
huffman@26243
   190
huffman@26243
   191
text {* Functions *}
huffman@26243
   192
huffman@26243
   193
instance "fun" :: (finite, countable) countable
huffman@26243
   194
proof
huffman@26243
   195
  obtain xs :: "'a list" where xs: "set xs = UNIV"
huffman@26243
   196
    using finite_list [OF finite_UNIV] ..
huffman@26243
   197
  show "\<exists>to_nat::('a \<Rightarrow> 'b) \<Rightarrow> nat. inj to_nat"
huffman@26243
   198
  proof
huffman@26243
   199
    show "inj (\<lambda>f. to_nat (map f xs))"
huffman@26243
   200
      by (rule injI, simp add: xs expand_fun_eq)
huffman@26243
   201
  qed
huffman@26243
   202
qed
huffman@26243
   203
huffman@29880
   204
huffman@29880
   205
subsection {* The Rationals are Countably Infinite *}
huffman@29880
   206
huffman@29880
   207
definition nat_to_rat_surj :: "nat \<Rightarrow> rat" where
huffman@29880
   208
"nat_to_rat_surj n = (let (a,b) = nat_to_nat2 n
huffman@29880
   209
                      in Fract (nat_to_int_bij a) (nat_to_int_bij b))"
huffman@29880
   210
huffman@29880
   211
lemma surj_nat_to_rat_surj: "surj nat_to_rat_surj"
huffman@29880
   212
unfolding surj_def
huffman@29880
   213
proof
huffman@29880
   214
  fix r::rat
huffman@29880
   215
  show "\<exists>n. r = nat_to_rat_surj n"
huffman@29880
   216
  proof(cases r)
huffman@29880
   217
    fix i j assume [simp]: "r = Fract i j" and "j \<noteq> 0"
huffman@29880
   218
    have "r = (let m = inv nat_to_int_bij i; n = inv nat_to_int_bij j
huffman@29880
   219
               in nat_to_rat_surj(nat2_to_nat (m,n)))"
huffman@29880
   220
      using nat2_to_nat_inj surj_f_inv_f[OF surj_nat_to_int_bij]
huffman@29880
   221
      by(simp add:Let_def nat_to_rat_surj_def nat_to_nat2_def)
huffman@29880
   222
    thus "\<exists>n. r = nat_to_rat_surj n" by(auto simp:Let_def)
huffman@29880
   223
  qed
huffman@29880
   224
qed
huffman@29880
   225
huffman@29880
   226
lemma Rats_eq_range_nat_to_rat_surj: "\<rat> = range nat_to_rat_surj"
huffman@29880
   227
by (simp add: Rats_def surj_nat_to_rat_surj surj_range)
huffman@29880
   228
huffman@29880
   229
context field_char_0
huffman@29880
   230
begin
huffman@29880
   231
huffman@29880
   232
lemma Rats_eq_range_of_rat_o_nat_to_rat_surj:
huffman@29880
   233
  "\<rat> = range (of_rat o nat_to_rat_surj)"
huffman@29880
   234
using surj_nat_to_rat_surj
huffman@29880
   235
by (auto simp: Rats_def image_def surj_def)
huffman@29880
   236
   (blast intro: arg_cong[where f = of_rat])
huffman@29880
   237
huffman@29880
   238
lemma surj_of_rat_nat_to_rat_surj:
huffman@29880
   239
  "r\<in>\<rat> \<Longrightarrow> \<exists>n. r = of_rat(nat_to_rat_surj n)"
huffman@29880
   240
by(simp add: Rats_eq_range_of_rat_o_nat_to_rat_surj image_def)
huffman@29880
   241
haftmann@26169
   242
end
huffman@29880
   243
huffman@29880
   244
instance rat :: countable
huffman@29880
   245
proof
huffman@29880
   246
  show "\<exists>to_nat::rat \<Rightarrow> nat. inj to_nat"
huffman@29880
   247
  proof
huffman@29880
   248
    have "surj nat_to_rat_surj"
huffman@29880
   249
      by (rule surj_nat_to_rat_surj)
huffman@29880
   250
    then show "inj (inv nat_to_rat_surj)"
huffman@29880
   251
      by (rule surj_imp_inj_inv)
huffman@29880
   252
  qed
huffman@29880
   253
qed
huffman@29880
   254
huffman@29880
   255
end