1 (* Title: HOL/Library/Countable.thy
3 Author: Alexander Krauss, TU Muenchen
6 header {* Encoding (almost) everything into natural numbers *}
9 imports Plain List Hilbert_Choice
12 subsection {* The class of countable types *}
14 class countable = itself +
15 assumes ex_inj: "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat"
17 lemma countable_classI:
18 fixes f :: "'a \<Rightarrow> nat"
19 assumes "\<And>x y. f x = f y \<Longrightarrow> x = y"
20 shows "OFCLASS('a, countable_class)"
21 proof (intro_classes, rule exI)
23 by (rule injI [OF assms]) assumption
27 subsection {* Conversion functions *}
29 definition to_nat :: "'a\<Colon>countable \<Rightarrow> nat" where
30 "to_nat = (SOME f. inj f)"
32 definition from_nat :: "nat \<Rightarrow> 'a\<Colon>countable" where
33 "from_nat = inv (to_nat \<Colon> 'a \<Rightarrow> nat)"
35 lemma inj_to_nat [simp]: "inj to_nat"
36 by (rule exE_some [OF ex_inj]) (simp add: to_nat_def)
38 lemma to_nat_split [simp]: "to_nat x = to_nat y \<longleftrightarrow> x = y"
39 using injD [OF inj_to_nat] by auto
41 lemma from_nat_to_nat [simp]:
42 "from_nat (to_nat x) = x"
43 by (simp add: from_nat_def)
46 subsection {* Countable types *}
48 instance nat :: countable
49 by (rule countable_classI [of "id"]) simp
51 subclass (in finite) countable
53 have "finite (UNIV\<Colon>'a set)" by (rule finite_UNIV)
54 with finite_conv_nat_seg_image [of UNIV]
55 obtain n and f :: "nat \<Rightarrow> 'a"
56 where "UNIV = f ` {i. i < n}" by auto
57 then have "surj f" unfolding surj_def by auto
58 then have "inj (inv f)" by (rule surj_imp_inj_inv)
59 then show "\<exists>to_nat \<Colon> 'a \<Rightarrow> nat. inj to_nat" by (rule exI[of inj])
64 primrec sum :: "nat \<Rightarrow> nat"
67 | "sum (Suc n) = Suc n + sum n"
69 lemma sum_arith: "sum n = n * Suc n div 2"
72 lemma sum_mono: "n \<ge> m \<Longrightarrow> sum n \<ge> sum m"
73 by (induct n m rule: diff_induct) auto
76 "pair_encode = (\<lambda>(m, n). sum (m + n) + m)"
78 lemma inj_pair_cencode: "inj pair_encode"
79 unfolding pair_encode_def
80 proof (rule injI, simp only: split_paired_all split_conv)
82 assume eq: "sum (a + b) + a = sum (c + d) + c"
83 have "a + b = c + d \<or> a + b \<ge> Suc (c + d) \<or> c + d \<ge> Suc (a + b)" by arith
85 show "(a, b) = (c, d)"
87 assume sumeq: "a + b = c + d"
88 then have "a = c" using eq by auto
89 moreover from sumeq this have "b = d" by auto
90 ultimately show ?thesis by simp
92 assume "a + b \<ge> Suc (c + d)"
93 from sum_mono[OF this] eq
96 assume "c + d \<ge> Suc (a + b)"
97 from sum_mono[OF this] eq
102 instance "*" :: (countable, countable) countable
103 by (rule countable_classI [of "\<lambda>(x, y). pair_encode (to_nat x, to_nat y)"])
104 (auto dest: injD [OF inj_pair_cencode] injD [OF inj_to_nat])
109 instance "+":: (countable, countable) countable
110 by (rule countable_classI [of "(\<lambda>x. case x of Inl a \<Rightarrow> to_nat (False, to_nat a)
111 | Inr b \<Rightarrow> to_nat (True, to_nat b))"])
112 (auto split:sum.splits)
117 lemma int_cases: "(i::int) = 0 \<or> i < 0 \<or> i > 0"
120 lemma int_pos_neg_zero:
121 obtains (zero) "(z::int) = 0" "sgn z = 0" "abs z = 0"
122 | (pos) n where "z = of_nat n" "sgn z = 1" "abs z = of_nat n"
123 | (neg) n where "z = - (of_nat n)" "sgn z = -1" "abs z = of_nat n"
125 apply (insert int_cases[of z])
126 apply (auto simp:zsgn_def)
127 apply (rule_tac x="nat (-z)" in exI, simp)
128 apply (rule_tac x="nat z" in exI, simp)
131 instance int :: countable
132 proof (rule countable_classI [of "(\<lambda>i. to_nat (nat (sgn i + 1), nat (abs i)))"],
133 auto dest: injD [OF inj_to_nat])
135 assume a: "nat (sgn x + 1) = nat (sgn y + 1)" "nat (abs x) = nat (abs y)"
137 proof (cases rule: int_pos_neg_zero[of x])
139 with a show "x = y" by (cases rule: int_pos_neg_zero[of y]) auto
142 with a show "x = y" by (cases rule: int_pos_neg_zero[of y]) auto
145 with a show "x = y" by (cases rule: int_pos_neg_zero[of y]) auto
152 instance option :: (countable) countable
153 by (rule countable_classI[of "\<lambda>x. case x of None \<Rightarrow> 0
154 | Some y \<Rightarrow> Suc (to_nat y)"])
155 (auto split:option.splits)
160 lemma from_nat_to_nat_map [simp]: "map from_nat (map to_nat xs) = xs"
161 by (simp add: comp_def map_compose [symmetric])
164 list_encode :: "'a\<Colon>countable list \<Rightarrow> nat"
167 | "list_encode (x#xs) = Suc (to_nat (x, list_encode xs))"
169 instance list :: (countable) countable
170 proof (rule countable_classI [of "list_encode"])
171 fix xs ys :: "'a list"
172 assume cenc: "list_encode xs = list_encode ys"
174 proof (induct xs arbitrary: ys)
176 with cenc show ?case by (cases ys, auto)
179 thus ?case by (cases ys) auto
186 instance "fun" :: (finite, countable) countable
188 obtain xs :: "'a list" where xs: "set xs = UNIV"
189 using finite_list [OF finite_UNIV] ..
190 show "\<exists>to_nat::('a \<Rightarrow> 'b) \<Rightarrow> nat. inj to_nat"
192 show "inj (\<lambda>f. to_nat (map f xs))"
193 by (rule injI, simp add: xs expand_fun_eq)