author  haftmann 
Fri, 26 Feb 2010 09:20:18 +0100  
changeset 35374  af1c8c15340e 
parent 33640  0d82107dc07a 
child 35700  951974ce903e 
permissions  rwrr 
26169  1 
(* Title: HOL/Library/Countable.thy 
26350  2 
Author: Alexander Krauss, TU Muenchen 
26169  3 
*) 
4 

5 
header {* Encoding (almost) everything into natural numbers *} 

6 

7 
theory Countable 

35374  8 
imports Main Rat Nat_Int_Bij 
26169  9 
begin 
10 

11 
subsection {* The class of countable types *} 

12 

29797  13 
class countable = 
26169  14 
assumes ex_inj: "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat" 
15 

16 
lemma countable_classI: 

17 
fixes f :: "'a \<Rightarrow> nat" 

18 
assumes "\<And>x y. f x = f y \<Longrightarrow> x = y" 

19 
shows "OFCLASS('a, countable_class)" 

20 
proof (intro_classes, rule exI) 

21 
show "inj f" 

22 
by (rule injI [OF assms]) assumption 

23 
qed 

24 

25 

26585  26 
subsection {* Conversion functions *} 
26169  27 

28 
definition to_nat :: "'a\<Colon>countable \<Rightarrow> nat" where 

29 
"to_nat = (SOME f. inj f)" 

30 

31 
definition from_nat :: "nat \<Rightarrow> 'a\<Colon>countable" where 

32 
"from_nat = inv (to_nat \<Colon> 'a \<Rightarrow> nat)" 

33 

34 
lemma inj_to_nat [simp]: "inj to_nat" 

35 
by (rule exE_some [OF ex_inj]) (simp add: to_nat_def) 

36 

29910  37 
lemma surj_from_nat [simp]: "surj from_nat" 
38 
unfolding from_nat_def by (simp add: inj_imp_surj_inv) 

39 

26169  40 
lemma to_nat_split [simp]: "to_nat x = to_nat y \<longleftrightarrow> x = y" 
41 
using injD [OF inj_to_nat] by auto 

42 

43 
lemma from_nat_to_nat [simp]: 

44 
"from_nat (to_nat x) = x" 

45 
by (simp add: from_nat_def) 

46 

47 

48 
subsection {* Countable types *} 

49 

50 
instance nat :: countable 

51 
by (rule countable_classI [of "id"]) simp 

52 

53 
subclass (in finite) countable 

28823  54 
proof 
26169  55 
have "finite (UNIV\<Colon>'a set)" by (rule finite_UNIV) 
31992  56 
with finite_conv_nat_seg_image [of "UNIV::'a set"] 
26169  57 
obtain n and f :: "nat \<Rightarrow> 'a" 
58 
where "UNIV = f ` {i. i < n}" by auto 

59 
then have "surj f" unfolding surj_def by auto 

60 
then have "inj (inv f)" by (rule surj_imp_inj_inv) 

61 
then show "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat" by (rule exI[of inj]) 

62 
qed 

63 

64 
text {* Pairs *} 

65 

66 
primrec sum :: "nat \<Rightarrow> nat" 

67 
where 

68 
"sum 0 = 0" 

69 
 "sum (Suc n) = Suc n + sum n" 

70 

71 
lemma sum_arith: "sum n = n * Suc n div 2" 

72 
by (induct n) auto 

73 

74 
lemma sum_mono: "n \<ge> m \<Longrightarrow> sum n \<ge> sum m" 

75 
by (induct n m rule: diff_induct) auto 

76 

77 
definition 

78 
"pair_encode = (\<lambda>(m, n). sum (m + n) + m)" 

79 

80 
lemma inj_pair_cencode: "inj pair_encode" 

81 
unfolding pair_encode_def 

82 
proof (rule injI, simp only: split_paired_all split_conv) 

83 
fix a b c d 

84 
assume eq: "sum (a + b) + a = sum (c + d) + c" 

85 
have "a + b = c + d \<or> a + b \<ge> Suc (c + d) \<or> c + d \<ge> Suc (a + b)" by arith 

86 
then 

87 
show "(a, b) = (c, d)" 

88 
proof (elim disjE) 

89 
assume sumeq: "a + b = c + d" 

90 
then have "a = c" using eq by auto 

91 
moreover from sumeq this have "b = d" by auto 

92 
ultimately show ?thesis by simp 

93 
next 

94 
assume "a + b \<ge> Suc (c + d)" 

95 
from sum_mono[OF this] eq 

96 
show ?thesis by auto 

97 
next 

98 
assume "c + d \<ge> Suc (a + b)" 

99 
from sum_mono[OF this] eq 

100 
show ?thesis by auto 

101 
qed 

102 
qed 

103 

104 
instance "*" :: (countable, countable) countable 

105 
by (rule countable_classI [of "\<lambda>(x, y). pair_encode (to_nat x, to_nat y)"]) 

106 
(auto dest: injD [OF inj_pair_cencode] injD [OF inj_to_nat]) 

107 

108 

109 
text {* Sums *} 

110 

111 
instance "+":: (countable, countable) countable 

112 
by (rule countable_classI [of "(\<lambda>x. case x of Inl a \<Rightarrow> to_nat (False, to_nat a) 

113 
 Inr b \<Rightarrow> to_nat (True, to_nat b))"]) 

114 
(auto split:sum.splits) 

115 

116 

117 
text {* Integers *} 

118 

119 
lemma int_cases: "(i::int) = 0 \<or> i < 0 \<or> i > 0" 

120 
by presburger 

121 

122 
lemma int_pos_neg_zero: 

123 
obtains (zero) "(z::int) = 0" "sgn z = 0" "abs z = 0" 

124 
 (pos) n where "z = of_nat n" "sgn z = 1" "abs z = of_nat n" 

125 
 (neg) n where "z =  (of_nat n)" "sgn z = 1" "abs z = of_nat n" 

26580
c3e597a476fd
Generic conversion and tactic "atomize_elim" to convert elimination rules
krauss
parents:
26350
diff
changeset

126 
apply atomize_elim 
26169  127 
apply (insert int_cases[of z]) 
128 
apply (auto simp:zsgn_def) 

129 
apply (rule_tac x="nat (z)" in exI, simp) 

130 
apply (rule_tac x="nat z" in exI, simp) 

131 
done 

132 

133 
instance int :: countable 

134 
proof (rule countable_classI [of "(\<lambda>i. to_nat (nat (sgn i + 1), nat (abs i)))"], 

135 
auto dest: injD [OF inj_to_nat]) 

136 
fix x y 

137 
assume a: "nat (sgn x + 1) = nat (sgn y + 1)" "nat (abs x) = nat (abs y)" 

138 
show "x = y" 

139 
proof (cases rule: int_pos_neg_zero[of x]) 

140 
case zero 

141 
with a show "x = y" by (cases rule: int_pos_neg_zero[of y]) auto 

142 
next 

143 
case (pos n) 

144 
with a show "x = y" by (cases rule: int_pos_neg_zero[of y]) auto 

145 
next 

146 
case (neg n) 

147 
with a show "x = y" by (cases rule: int_pos_neg_zero[of y]) auto 

148 
qed 

149 
qed 

150 

151 

152 
text {* Options *} 

153 

154 
instance option :: (countable) countable 

155 
by (rule countable_classI[of "\<lambda>x. case x of None \<Rightarrow> 0 

156 
 Some y \<Rightarrow> Suc (to_nat y)"]) 

157 
(auto split:option.splits) 

158 

159 

160 
text {* Lists *} 

161 

162 
lemma from_nat_to_nat_map [simp]: "map from_nat (map to_nat xs) = xs" 

33640  163 
by (simp add: comp_def) 
26169  164 

165 
primrec 

166 
list_encode :: "'a\<Colon>countable list \<Rightarrow> nat" 

167 
where 

168 
"list_encode [] = 0" 

169 
 "list_encode (x#xs) = Suc (to_nat (x, list_encode xs))" 

170 

171 
instance list :: (countable) countable 

172 
proof (rule countable_classI [of "list_encode"]) 

173 
fix xs ys :: "'a list" 

174 
assume cenc: "list_encode xs = list_encode ys" 

175 
then show "xs = ys" 

176 
proof (induct xs arbitrary: ys) 

177 
case (Nil ys) 

178 
with cenc show ?case by (cases ys, auto) 

179 
next 

180 
case (Cons x xs' ys) 

181 
thus ?case by (cases ys) auto 

182 
qed 

183 
qed 

184 

26243  185 

186 
text {* Functions *} 

187 

188 
instance "fun" :: (finite, countable) countable 

189 
proof 

190 
obtain xs :: "'a list" where xs: "set xs = UNIV" 

191 
using finite_list [OF finite_UNIV] .. 

192 
show "\<exists>to_nat::('a \<Rightarrow> 'b) \<Rightarrow> nat. inj to_nat" 

193 
proof 

194 
show "inj (\<lambda>f. to_nat (map f xs))" 

195 
by (rule injI, simp add: xs expand_fun_eq) 

196 
qed 

197 
qed 

198 

29880
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

199 

3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

200 
subsection {* The Rationals are Countably Infinite *} 
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 
definition nat_to_rat_surj :: "nat \<Rightarrow> rat" where 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

203 
"nat_to_rat_surj n = (let (a,b) = nat_to_nat2 n 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

204 
in Fract (nat_to_int_bij a) (nat_to_int_bij b))" 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

205 

3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

206 
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

207 
unfolding surj_def 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

208 
proof 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

209 
fix r::rat 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

210 
show "\<exists>n. r = nat_to_rat_surj n" 
35374  211 
proof (cases r) 
212 
fix i j assume [simp]: "r = Fract i j" and "j > 0" 

29880
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

213 
have "r = (let m = inv nat_to_int_bij i; n = inv nat_to_int_bij j 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

214 
in nat_to_rat_surj(nat2_to_nat (m,n)))" 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

215 
using nat2_to_nat_inj surj_f_inv_f[OF surj_nat_to_int_bij] 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

216 
by(simp add:Let_def nat_to_rat_surj_def nat_to_nat2_def) 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

217 
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

218 
qed 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

219 
qed 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

220 

3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

221 
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

222 
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

223 

3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

224 
context field_char_0 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

225 
begin 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

226 

3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

227 
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

228 
"\<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

229 
using surj_nat_to_rat_surj 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

230 
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

231 
(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

232 

3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

233 
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

234 
"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

235 
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

236 

26169  237 
end 
29880
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

238 

3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

239 
instance rat :: countable 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

240 
proof 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

241 
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

242 
proof 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

243 
have "surj nat_to_rat_surj" 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

244 
by (rule surj_nat_to_rat_surj) 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

245 
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

246 
by (rule surj_imp_inj_inv) 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

247 
qed 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

248 
qed 
3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

249 

3dee8ff45d3d
move countability proof from Rational to Countable; add instance rat :: countable
huffman
parents:
29797
diff
changeset

250 
end 