src/HOL/Library/Countable_Set.thy
author haftmann
Tue Feb 19 19:44:10 2013 +0100 (2013-02-19)
changeset 51188 9b5bf1a9a710
parent 50936 b28f258ebc1a
child 51542 738598beeb26
permissions -rw-r--r--
dropped spurious left-over from 0a2371e7ced3
hoelzl@50134
     1
(*  Title:      HOL/Library/Countable_Set.thy
hoelzl@50134
     2
    Author:     Johannes Hölzl
hoelzl@50134
     3
    Author:     Andrei Popescu
hoelzl@50134
     4
*)
hoelzl@50134
     5
hoelzl@50134
     6
header {* Countable sets *}
hoelzl@50134
     7
hoelzl@50134
     8
theory Countable_Set
hoelzl@50134
     9
  imports "~~/src/HOL/Library/Countable" "~~/src/HOL/Library/Infinite_Set"
hoelzl@50134
    10
begin
hoelzl@50134
    11
hoelzl@50134
    12
subsection {* Predicate for countable sets *}
hoelzl@50134
    13
hoelzl@50134
    14
definition countable :: "'a set \<Rightarrow> bool" where
hoelzl@50134
    15
  "countable S \<longleftrightarrow> (\<exists>f::'a \<Rightarrow> nat. inj_on f S)"
hoelzl@50134
    16
hoelzl@50134
    17
lemma countableE:
hoelzl@50134
    18
  assumes S: "countable S" obtains f :: "'a \<Rightarrow> nat" where "inj_on f S"
hoelzl@50134
    19
  using S by (auto simp: countable_def)
hoelzl@50134
    20
hoelzl@50134
    21
lemma countableI: "inj_on (f::'a \<Rightarrow> nat) S \<Longrightarrow> countable S"
hoelzl@50134
    22
  by (auto simp: countable_def)
hoelzl@50134
    23
hoelzl@50134
    24
lemma countableI': "inj_on (f::'a \<Rightarrow> 'b::countable) S \<Longrightarrow> countable S"
hoelzl@50134
    25
  using comp_inj_on[of f S to_nat] by (auto intro: countableI)
hoelzl@50134
    26
hoelzl@50134
    27
lemma countableE_bij:
hoelzl@50134
    28
  assumes S: "countable S" obtains f :: "nat \<Rightarrow> 'a" and C :: "nat set" where "bij_betw f C S"
hoelzl@50134
    29
  using S by (blast elim: countableE dest: inj_on_imp_bij_betw bij_betw_inv)
hoelzl@50134
    30
hoelzl@50134
    31
lemma countableI_bij: "bij_betw f (C::nat set) S \<Longrightarrow> countable S"
hoelzl@50134
    32
  by (blast intro: countableI bij_betw_inv_into bij_betw_imp_inj_on)
hoelzl@50134
    33
hoelzl@50134
    34
lemma countable_finite: "finite S \<Longrightarrow> countable S"
hoelzl@50134
    35
  by (blast dest: finite_imp_inj_to_nat_seg countableI)
hoelzl@50134
    36
hoelzl@50134
    37
lemma countableI_bij1: "bij_betw f A B \<Longrightarrow> countable A \<Longrightarrow> countable B"
hoelzl@50134
    38
  by (blast elim: countableE_bij intro: bij_betw_trans countableI_bij)
hoelzl@50134
    39
hoelzl@50134
    40
lemma countableI_bij2: "bij_betw f B A \<Longrightarrow> countable A \<Longrightarrow> countable B"
hoelzl@50134
    41
  by (blast elim: countableE_bij intro: bij_betw_trans bij_betw_inv_into countableI_bij)
hoelzl@50134
    42
hoelzl@50134
    43
lemma countable_iff_bij[simp]: "bij_betw f A B \<Longrightarrow> countable A \<longleftrightarrow> countable B"
hoelzl@50134
    44
  by (blast intro: countableI_bij1 countableI_bij2)
hoelzl@50134
    45
hoelzl@50134
    46
lemma countable_subset: "A \<subseteq> B \<Longrightarrow> countable B \<Longrightarrow> countable A"
hoelzl@50134
    47
  by (auto simp: countable_def intro: subset_inj_on)
hoelzl@50134
    48
hoelzl@50134
    49
lemma countableI_type[intro, simp]: "countable (A:: 'a :: countable set)"
hoelzl@50134
    50
  using countableI[of to_nat A] by auto
hoelzl@50134
    51
hoelzl@50134
    52
subsection {* Enumerate a countable set *}
hoelzl@50134
    53
hoelzl@50134
    54
lemma countableE_infinite:
hoelzl@50134
    55
  assumes "countable S" "infinite S"
hoelzl@50134
    56
  obtains e :: "'a \<Rightarrow> nat" where "bij_betw e S UNIV"
hoelzl@50134
    57
proof -
hoelzl@50134
    58
  from `countable S`[THEN countableE] guess f .
hoelzl@50134
    59
  then have "bij_betw f S (f`S)"
hoelzl@50134
    60
    unfolding bij_betw_def by simp
hoelzl@50134
    61
  moreover
hoelzl@50134
    62
  from `inj_on f S` `infinite S` have inf_fS: "infinite (f`S)"
hoelzl@50134
    63
    by (auto dest: finite_imageD)
hoelzl@50134
    64
  then have "bij_betw (the_inv_into UNIV (enumerate (f`S))) (f`S) UNIV"
hoelzl@50134
    65
    by (intro bij_betw_the_inv_into bij_enumerate)
hoelzl@50134
    66
  ultimately have "bij_betw (the_inv_into UNIV (enumerate (f`S)) \<circ> f) S UNIV"
hoelzl@50134
    67
    by (rule bij_betw_trans)
hoelzl@50134
    68
  then show thesis ..
hoelzl@50134
    69
qed
hoelzl@50134
    70
hoelzl@50134
    71
lemma countable_enum_cases:
hoelzl@50134
    72
  assumes "countable S"
hoelzl@50134
    73
  obtains (finite) f :: "'a \<Rightarrow> nat" where "finite S" "bij_betw f S {..<card S}"
hoelzl@50134
    74
        | (infinite) f :: "'a \<Rightarrow> nat" where "infinite S" "bij_betw f S UNIV"
hoelzl@50134
    75
  using ex_bij_betw_finite_nat[of S] countableE_infinite `countable S`
hoelzl@50134
    76
  by (cases "finite S") (auto simp add: atLeast0LessThan)
hoelzl@50134
    77
hoelzl@50134
    78
definition to_nat_on :: "'a set \<Rightarrow> 'a \<Rightarrow> nat" where
hoelzl@50134
    79
  "to_nat_on S = (SOME f. if finite S then bij_betw f S {..< card S} else bij_betw f S UNIV)"
hoelzl@50134
    80
hoelzl@50134
    81
definition from_nat_into :: "'a set \<Rightarrow> nat \<Rightarrow> 'a" where
hoelzl@50144
    82
  "from_nat_into S n = (if n \<in> to_nat_on S ` S then inv_into S (to_nat_on S) n else SOME s. s\<in>S)"
hoelzl@50134
    83
hoelzl@50134
    84
lemma to_nat_on_finite: "finite S \<Longrightarrow> bij_betw (to_nat_on S) S {..< card S}"
hoelzl@50134
    85
  using ex_bij_betw_finite_nat unfolding to_nat_on_def
hoelzl@50134
    86
  by (intro someI2_ex[where Q="\<lambda>f. bij_betw f S {..<card S}"]) (auto simp add: atLeast0LessThan)
hoelzl@50134
    87
hoelzl@50134
    88
lemma to_nat_on_infinite: "countable S \<Longrightarrow> infinite S \<Longrightarrow> bij_betw (to_nat_on S) S UNIV"
hoelzl@50134
    89
  using countableE_infinite unfolding to_nat_on_def
hoelzl@50134
    90
  by (intro someI2_ex[where Q="\<lambda>f. bij_betw f S UNIV"]) auto
hoelzl@50134
    91
hoelzl@50148
    92
lemma bij_betw_from_nat_into_finite: "finite S \<Longrightarrow> bij_betw (from_nat_into S) {..< card S} S"
hoelzl@50144
    93
  unfolding from_nat_into_def[abs_def]
hoelzl@50144
    94
  using to_nat_on_finite[of S]
hoelzl@50144
    95
  apply (subst bij_betw_cong)
hoelzl@50144
    96
  apply (split split_if)
hoelzl@50144
    97
  apply (simp add: bij_betw_def)
hoelzl@50144
    98
  apply (auto cong: bij_betw_cong
hoelzl@50144
    99
              intro: bij_betw_inv_into to_nat_on_finite)
hoelzl@50144
   100
  done
hoelzl@50134
   101
hoelzl@50148
   102
lemma bij_betw_from_nat_into: "countable S \<Longrightarrow> infinite S \<Longrightarrow> bij_betw (from_nat_into S) UNIV S"
hoelzl@50144
   103
  unfolding from_nat_into_def[abs_def]
hoelzl@50144
   104
  using to_nat_on_infinite[of S, unfolded bij_betw_def]
hoelzl@50144
   105
  by (auto cong: bij_betw_cong intro: bij_betw_inv_into to_nat_on_infinite)
hoelzl@50134
   106
hoelzl@50134
   107
lemma inj_on_to_nat_on[intro]: "countable A \<Longrightarrow> inj_on (to_nat_on A) A"
hoelzl@50134
   108
  using to_nat_on_infinite[of A] to_nat_on_finite[of A]
hoelzl@50134
   109
  by (cases "finite A") (auto simp: bij_betw_def)
hoelzl@50134
   110
hoelzl@50134
   111
lemma to_nat_on_inj[simp]:
hoelzl@50134
   112
  "countable A \<Longrightarrow> a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> to_nat_on A a = to_nat_on A b \<longleftrightarrow> a = b"
hoelzl@50134
   113
  using inj_on_to_nat_on[of A] by (auto dest: inj_onD)
hoelzl@50134
   114
hoelzl@50144
   115
lemma from_nat_into_to_nat_on[simp]: "countable A \<Longrightarrow> a \<in> A \<Longrightarrow> from_nat_into A (to_nat_on A a) = a"
hoelzl@50134
   116
  by (auto simp: from_nat_into_def intro!: inv_into_f_f)
hoelzl@50134
   117
hoelzl@50134
   118
lemma subset_range_from_nat_into: "countable A \<Longrightarrow> A \<subseteq> range (from_nat_into A)"
hoelzl@50134
   119
  by (auto intro: from_nat_into_to_nat_on[symmetric])
hoelzl@50134
   120
hoelzl@50144
   121
lemma from_nat_into: "A \<noteq> {} \<Longrightarrow> from_nat_into A n \<in> A"
hoelzl@50144
   122
  unfolding from_nat_into_def by (metis equals0I inv_into_into someI_ex)
hoelzl@50144
   123
hoelzl@50144
   124
lemma range_from_nat_into_subset: "A \<noteq> {} \<Longrightarrow> range (from_nat_into A) \<subseteq> A"
hoelzl@50144
   125
  using from_nat_into[of A] by auto
hoelzl@50144
   126
hoelzl@50148
   127
lemma range_from_nat_into[simp]: "A \<noteq> {} \<Longrightarrow> countable A \<Longrightarrow> range (from_nat_into A) = A"
hoelzl@50144
   128
  by (metis equalityI range_from_nat_into_subset subset_range_from_nat_into)
hoelzl@50144
   129
hoelzl@50144
   130
lemma image_to_nat_on: "countable A \<Longrightarrow> infinite A \<Longrightarrow> to_nat_on A ` A = UNIV"
hoelzl@50144
   131
  using to_nat_on_infinite[of A] by (simp add: bij_betw_def)
hoelzl@50144
   132
hoelzl@50144
   133
lemma to_nat_on_surj: "countable A \<Longrightarrow> infinite A \<Longrightarrow> \<exists>a\<in>A. to_nat_on A a = n"
hoelzl@50144
   134
  by (metis (no_types) image_iff iso_tuple_UNIV_I image_to_nat_on)
hoelzl@50144
   135
hoelzl@50144
   136
lemma to_nat_on_from_nat_into[simp]: "n \<in> to_nat_on A ` A \<Longrightarrow> to_nat_on A (from_nat_into A n) = n"
hoelzl@50144
   137
  by (simp add: f_inv_into_f from_nat_into_def)
hoelzl@50144
   138
hoelzl@50148
   139
lemma to_nat_on_from_nat_into_infinite[simp]:
hoelzl@50144
   140
  "countable A \<Longrightarrow> infinite A \<Longrightarrow> to_nat_on A (from_nat_into A n) = n"
hoelzl@50144
   141
  by (metis image_iff to_nat_on_surj to_nat_on_from_nat_into)
hoelzl@50144
   142
hoelzl@50144
   143
lemma from_nat_into_inj:
hoelzl@50148
   144
  "countable A \<Longrightarrow> m \<in> to_nat_on A ` A \<Longrightarrow> n \<in> to_nat_on A ` A \<Longrightarrow>
hoelzl@50148
   145
    from_nat_into A m = from_nat_into A n \<longleftrightarrow> m = n"
hoelzl@50148
   146
  by (subst to_nat_on_inj[symmetric, of A]) auto
hoelzl@50148
   147
hoelzl@50148
   148
lemma from_nat_into_inj_infinite[simp]:
hoelzl@50148
   149
  "countable A \<Longrightarrow> infinite A \<Longrightarrow> from_nat_into A m = from_nat_into A n \<longleftrightarrow> m = n"
hoelzl@50148
   150
  using image_to_nat_on[of A] from_nat_into_inj[of A m n] by simp
hoelzl@50148
   151
hoelzl@50148
   152
lemma eq_from_nat_into_iff:
hoelzl@50148
   153
  "countable A \<Longrightarrow> x \<in> A \<Longrightarrow> i \<in> to_nat_on A ` A \<Longrightarrow> x = from_nat_into A i \<longleftrightarrow> i = to_nat_on A x"
hoelzl@50148
   154
  by auto
hoelzl@50144
   155
hoelzl@50144
   156
lemma from_nat_into_surj: "countable A \<Longrightarrow> a \<in> A \<Longrightarrow> \<exists>n. from_nat_into A n = a"
hoelzl@50144
   157
  by (rule exI[of _ "to_nat_on A a"]) simp
hoelzl@50144
   158
hoelzl@50144
   159
lemma from_nat_into_inject[simp]:
hoelzl@50148
   160
  "A \<noteq> {} \<Longrightarrow> countable A \<Longrightarrow> B \<noteq> {} \<Longrightarrow> countable B \<Longrightarrow> from_nat_into A = from_nat_into B \<longleftrightarrow> A = B"
hoelzl@50148
   161
  by (metis range_from_nat_into)
hoelzl@50144
   162
hoelzl@50148
   163
lemma inj_on_from_nat_into: "inj_on from_nat_into ({A. A \<noteq> {} \<and> countable A})"
hoelzl@50148
   164
  unfolding inj_on_def by auto
hoelzl@50144
   165
hoelzl@50134
   166
subsection {* Closure properties of countability *}
hoelzl@50134
   167
hoelzl@50134
   168
lemma countable_SIGMA[intro, simp]:
hoelzl@50134
   169
  "countable I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (A i)) \<Longrightarrow> countable (SIGMA i : I. A i)"
hoelzl@50134
   170
  by (intro countableI'[of "\<lambda>(i, a). (to_nat_on I i, to_nat_on (A i) a)"]) (auto simp: inj_on_def)
hoelzl@50134
   171
hoelzl@50134
   172
lemma countable_image[intro, simp]: assumes A: "countable A" shows "countable (f`A)"
hoelzl@50134
   173
proof -
hoelzl@50134
   174
  from A guess g by (rule countableE)
hoelzl@50134
   175
  moreover have "inj_on (inv_into A f) (f`A)" "inv_into A f ` f ` A \<subseteq> A"
hoelzl@50134
   176
    by (auto intro: inj_on_inv_into inv_into_into)
hoelzl@50134
   177
  ultimately show ?thesis
hoelzl@50134
   178
    by (blast dest: comp_inj_on subset_inj_on intro: countableI)
hoelzl@50134
   179
qed
hoelzl@50134
   180
hoelzl@50134
   181
lemma countable_UN[intro, simp]:
hoelzl@50134
   182
  fixes I :: "'i set" and A :: "'i => 'a set"
hoelzl@50134
   183
  assumes I: "countable I"
hoelzl@50134
   184
  assumes A: "\<And>i. i \<in> I \<Longrightarrow> countable (A i)"
hoelzl@50134
   185
  shows "countable (\<Union>i\<in>I. A i)"
hoelzl@50134
   186
proof -
hoelzl@50134
   187
  have "(\<Union>i\<in>I. A i) = snd ` (SIGMA i : I. A i)" by (auto simp: image_iff)
hoelzl@50134
   188
  then show ?thesis by (simp add: assms)
hoelzl@50134
   189
qed
hoelzl@50134
   190
hoelzl@50134
   191
lemma countable_Un[intro]: "countable A \<Longrightarrow> countable B \<Longrightarrow> countable (A \<union> B)"
hoelzl@50134
   192
  by (rule countable_UN[of "{True, False}" "\<lambda>True \<Rightarrow> A | False \<Rightarrow> B", simplified])
hoelzl@50134
   193
     (simp split: bool.split)
hoelzl@50134
   194
hoelzl@50134
   195
lemma countable_Un_iff[simp]: "countable (A \<union> B) \<longleftrightarrow> countable A \<and> countable B"
hoelzl@50134
   196
  by (metis countable_Un countable_subset inf_sup_ord(3,4))
hoelzl@50134
   197
hoelzl@50134
   198
lemma countable_Plus[intro, simp]:
hoelzl@50134
   199
  "countable A \<Longrightarrow> countable B \<Longrightarrow> countable (A <+> B)"
hoelzl@50134
   200
  by (simp add: Plus_def)
hoelzl@50134
   201
hoelzl@50134
   202
lemma countable_empty[intro, simp]: "countable {}"
hoelzl@50134
   203
  by (blast intro: countable_finite)
hoelzl@50134
   204
hoelzl@50134
   205
lemma countable_insert[intro, simp]: "countable A \<Longrightarrow> countable (insert a A)"
hoelzl@50134
   206
  using countable_Un[of "{a}" A] by (auto simp: countable_finite)
hoelzl@50134
   207
hoelzl@50134
   208
lemma countable_Int1[intro, simp]: "countable A \<Longrightarrow> countable (A \<inter> B)"
hoelzl@50134
   209
  by (force intro: countable_subset)
hoelzl@50134
   210
hoelzl@50134
   211
lemma countable_Int2[intro, simp]: "countable B \<Longrightarrow> countable (A \<inter> B)"
hoelzl@50134
   212
  by (blast intro: countable_subset)
hoelzl@50134
   213
hoelzl@50134
   214
lemma countable_INT[intro, simp]: "i \<in> I \<Longrightarrow> countable (A i) \<Longrightarrow> countable (\<Inter>i\<in>I. A i)"
hoelzl@50134
   215
  by (blast intro: countable_subset)
hoelzl@50134
   216
hoelzl@50134
   217
lemma countable_Diff[intro, simp]: "countable A \<Longrightarrow> countable (A - B)"
hoelzl@50134
   218
  by (blast intro: countable_subset)
hoelzl@50134
   219
hoelzl@50134
   220
lemma countable_vimage: "B \<subseteq> range f \<Longrightarrow> countable (f -` B) \<Longrightarrow> countable B"
hoelzl@50134
   221
  by (metis Int_absorb2 assms countable_image image_vimage_eq)
hoelzl@50134
   222
hoelzl@50134
   223
lemma surj_countable_vimage: "surj f \<Longrightarrow> countable (f -` B) \<Longrightarrow> countable B"
hoelzl@50134
   224
  by (metis countable_vimage top_greatest)
hoelzl@50134
   225
hoelzl@50144
   226
lemma countable_Collect[simp]: "countable A \<Longrightarrow> countable {a \<in> A. \<phi> a}"
hoelzl@50144
   227
  by (metis Collect_conj_eq Int_absorb Int_commute Int_def countable_Int1)
hoelzl@50144
   228
hoelzl@50134
   229
lemma countable_lists[intro, simp]:
hoelzl@50134
   230
  assumes A: "countable A" shows "countable (lists A)"
hoelzl@50134
   231
proof -
hoelzl@50134
   232
  have "countable (lists (range (from_nat_into A)))"
hoelzl@50134
   233
    by (auto simp: lists_image)
hoelzl@50134
   234
  with A show ?thesis
hoelzl@50134
   235
    by (auto dest: subset_range_from_nat_into countable_subset lists_mono)
hoelzl@50134
   236
qed
hoelzl@50134
   237
immler@50245
   238
lemma Collect_finite_eq_lists: "Collect finite = set ` lists UNIV"
immler@50245
   239
  using finite_list by auto
immler@50245
   240
immler@50245
   241
lemma countable_Collect_finite: "countable (Collect (finite::'a::countable set\<Rightarrow>bool))"
immler@50245
   242
  by (simp add: Collect_finite_eq_lists)
immler@50245
   243
hoelzl@50936
   244
lemma countable_rat: "countable \<rat>"
hoelzl@50936
   245
  unfolding Rats_def by auto
hoelzl@50936
   246
hoelzl@50936
   247
lemma Collect_finite_subset_eq_lists: "{A. finite A \<and> A \<subseteq> T} = set ` lists T"
hoelzl@50936
   248
  using finite_list by (auto simp: lists_eq_set)
hoelzl@50936
   249
hoelzl@50936
   250
lemma countable_Collect_finite_subset:
hoelzl@50936
   251
  "countable T \<Longrightarrow> countable {A. finite A \<and> A \<subseteq> T}"
hoelzl@50936
   252
  unfolding Collect_finite_subset_eq_lists by auto
hoelzl@50936
   253
hoelzl@50134
   254
subsection {* Misc lemmas *}
hoelzl@50134
   255
hoelzl@50134
   256
lemma countable_all:
hoelzl@50134
   257
  assumes S: "countable S"
hoelzl@50134
   258
  shows "(\<forall>s\<in>S. P s) \<longleftrightarrow> (\<forall>n::nat. from_nat_into S n \<in> S \<longrightarrow> P (from_nat_into S n))"
hoelzl@50134
   259
  using S[THEN subset_range_from_nat_into] by auto
hoelzl@50134
   260
hoelzl@50134
   261
end