src/HOL/Analysis/Elementary_Topology.thy
 changeset 69516 09bb8f470959 child 69529 4ab9657b3257
1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Analysis/Elementary_Topology.thy	Thu Dec 27 23:38:55 2018 +0100
1.3 @@ -0,0 +1,5098 @@
1.4 +(*  Author:     L C Paulson, University of Cambridge
1.5 +    Author:     Amine Chaieb, University of Cambridge
1.6 +    Author:     Robert Himmelmann, TU Muenchen
1.7 +    Author:     Brian Huffman, Portland State University
1.8 +*)
1.9 +
1.10 +section \<open>Elementary Topology\<close>
1.11 +
1.12 +theory Elementary_Topology
1.13 +imports
1.14 +  "HOL-Library.Indicator_Function"
1.15 +  "HOL-Library.Countable_Set"
1.16 +  "HOL-Library.FuncSet"
1.17 +  "HOL-Library.Set_Idioms"
1.18 +  "HOL-Library.Infinite_Set"
1.19 +  Product_Vector
1.20 +begin
1.21 +
1.22 +(* FIXME: move elsewhere *)
1.23 +
1.24 +lemma approachable_lt_le: "(\<exists>(d::real) > 0. \<forall>x. f x < d \<longrightarrow> P x) \<longleftrightarrow> (\<exists>d>0. \<forall>x. f x \<le> d \<longrightarrow> P x)"
1.25 +  apply auto
1.26 +  apply (rule_tac x="d/2" in exI)
1.27 +  apply auto
1.28 +  done
1.29 +
1.30 +lemma approachable_lt_le2:  \<comment> \<open>like the above, but pushes aside an extra formula\<close>
1.31 +    "(\<exists>(d::real) > 0. \<forall>x. Q x \<longrightarrow> f x < d \<longrightarrow> P x) \<longleftrightarrow> (\<exists>d>0. \<forall>x. f x \<le> d \<longrightarrow> Q x \<longrightarrow> P x)"
1.32 +  apply auto
1.33 +  apply (rule_tac x="d/2" in exI, auto)
1.34 +  done
1.35 +
1.36 +lemma triangle_lemma:
1.37 +  fixes x y z :: real
1.38 +  assumes x: "0 \<le> x"
1.39 +    and y: "0 \<le> y"
1.40 +    and z: "0 \<le> z"
1.41 +    and xy: "x\<^sup>2 \<le> y\<^sup>2 + z\<^sup>2"
1.42 +  shows "x \<le> y + z"
1.43 +proof -
1.44 +  have "y\<^sup>2 + z\<^sup>2 \<le> y\<^sup>2 + 2 * y * z + z\<^sup>2"
1.45 +    using z y by simp
1.46 +  with xy have th: "x\<^sup>2 \<le> (y + z)\<^sup>2"
1.47 +    by (simp add: power2_eq_square field_simps)
1.48 +  from y z have yz: "y + z \<ge> 0"
1.49 +    by arith
1.50 +  from power2_le_imp_le[OF th yz] show ?thesis .
1.51 +qed
1.52 +
1.53 +definition (in monoid_add) support_on :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'b set"
1.54 +  where "support_on s f = {x\<in>s. f x \<noteq> 0}"
1.55 +
1.56 +lemma in_support_on: "x \<in> support_on s f \<longleftrightarrow> x \<in> s \<and> f x \<noteq> 0"
1.57 +  by (simp add: support_on_def)
1.58 +
1.59 +lemma support_on_simps[simp]:
1.60 +  "support_on {} f = {}"
1.61 +  "support_on (insert x s) f =
1.62 +    (if f x = 0 then support_on s f else insert x (support_on s f))"
1.63 +  "support_on (s \<union> t) f = support_on s f \<union> support_on t f"
1.64 +  "support_on (s \<inter> t) f = support_on s f \<inter> support_on t f"
1.65 +  "support_on (s - t) f = support_on s f - support_on t f"
1.66 +  "support_on (f ` s) g = f ` (support_on s (g \<circ> f))"
1.67 +  unfolding support_on_def by auto
1.68 +
1.69 +lemma support_on_cong:
1.70 +  "(\<And>x. x \<in> s \<Longrightarrow> f x = 0 \<longleftrightarrow> g x = 0) \<Longrightarrow> support_on s f = support_on s g"
1.71 +  by (auto simp: support_on_def)
1.72 +
1.73 +lemma support_on_if: "a \<noteq> 0 \<Longrightarrow> support_on A (\<lambda>x. if P x then a else 0) = {x\<in>A. P x}"
1.74 +  by (auto simp: support_on_def)
1.75 +
1.76 +lemma support_on_if_subset: "support_on A (\<lambda>x. if P x then a else 0) \<subseteq> {x \<in> A. P x}"
1.77 +  by (auto simp: support_on_def)
1.78 +
1.79 +lemma finite_support[intro]: "finite S \<Longrightarrow> finite (support_on S f)"
1.80 +  unfolding support_on_def by auto
1.81 +
1.82 +(* TODO: is supp_sum really needed? TODO: Generalize to Finite_Set.fold *)
1.83 +definition (in comm_monoid_add) supp_sum :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
1.84 +  where "supp_sum f S = (\<Sum>x\<in>support_on S f. f x)"
1.85 +
1.86 +lemma supp_sum_empty[simp]: "supp_sum f {} = 0"
1.87 +  unfolding supp_sum_def by auto
1.88 +
1.89 +lemma supp_sum_insert[simp]:
1.90 +  "finite (support_on S f) \<Longrightarrow>
1.91 +    supp_sum f (insert x S) = (if x \<in> S then supp_sum f S else f x + supp_sum f S)"
1.92 +  by (simp add: supp_sum_def in_support_on insert_absorb)
1.93 +
1.94 +lemma supp_sum_divide_distrib: "supp_sum f A / (r::'a::field) = supp_sum (\<lambda>n. f n / r) A"
1.95 +  by (cases "r = 0")
1.96 +     (auto simp: supp_sum_def sum_divide_distrib intro!: sum.cong support_on_cong)
1.97 +
1.98 +(*END OF SUPPORT, ETC.*)
1.99 +
1.100 +lemma image_affinity_interval:
1.101 +  fixes c :: "'a::ordered_real_vector"
1.102 +  shows "((\<lambda>x. m *\<^sub>R x + c) ` {a..b}) =
1.103 +           (if {a..b}={} then {}
1.104 +            else if 0 \<le> m then {m *\<^sub>R a + c .. m  *\<^sub>R b + c}
1.105 +            else {m *\<^sub>R b + c .. m *\<^sub>R a + c})"
1.106 +         (is "?lhs = ?rhs")
1.107 +proof (cases "m=0")
1.108 +  case True
1.109 +  then show ?thesis
1.110 +    by force
1.111 +next
1.112 +  case False
1.113 +  show ?thesis
1.114 +  proof
1.115 +    show "?lhs \<subseteq> ?rhs"
1.116 +      by (auto simp: scaleR_left_mono scaleR_left_mono_neg)
1.117 +    show "?rhs \<subseteq> ?lhs"
1.118 +    proof (clarsimp, intro conjI impI subsetI)
1.119 +      show "\<lbrakk>0 \<le> m; a \<le> b; x \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}\<rbrakk>
1.120 +            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
1.121 +        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
1.122 +        using False apply (auto simp: le_diff_eq pos_le_divideRI)
1.123 +        using diff_le_eq pos_le_divideR_eq by force
1.124 +      show "\<lbrakk>\<not> 0 \<le> m; a \<le> b;  x \<in> {m *\<^sub>R b + c..m *\<^sub>R a + c}\<rbrakk>
1.125 +            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
1.126 +        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
1.127 +        apply (auto simp: diff_le_eq neg_le_divideR_eq)
1.128 +        using diff_eq_diff_less_eq linordered_field_class.sign_simps(11) minus_diff_eq not_less scaleR_le_cancel_left_neg by fastforce
1.129 +    qed
1.130 +  qed
1.131 +qed
1.133 +lemma countable_PiE:
1.134 +  "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (Pi\<^sub>E I F)"
1.135 +  by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)
1.137 +lemma open_sums:
1.138 +  fixes T :: "('b::real_normed_vector) set"
1.139 +  assumes "open S \<or> open T"
1.140 +  shows "open (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
1.141 +  using assms
1.142 +proof
1.143 +  assume S: "open S"
1.144 +  show ?thesis
1.145 +  proof (clarsimp simp: open_dist)
1.146 +    fix x y
1.147 +    assume "x \<in> S" "y \<in> T"
1.148 +    with S obtain e where "e > 0" and e: "\<And>x'. dist x' x < e \<Longrightarrow> x' \<in> S"
1.149 +      by (auto simp: open_dist)
1.150 +    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
1.152 +    then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
1.153 +      using \<open>0 < e\<close> \<open>x \<in> S\<close> by blast
1.154 +  qed
1.155 +next
1.156 +  assume T: "open T"
1.157 +  show ?thesis
1.158 +  proof (clarsimp simp: open_dist)
1.159 +    fix x y
1.160 +    assume "x \<in> S" "y \<in> T"
1.161 +    with T obtain e where "e > 0" and e: "\<And>x'. dist x' y < e \<Longrightarrow> x' \<in> T"
1.162 +      by (auto simp: open_dist)
1.163 +    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
1.165 +    then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
1.166 +      using \<open>0 < e\<close> \<open>y \<in> T\<close> by blast
1.167 +  qed
1.168 +qed
1.171 +subsection \<open>Topological Basis\<close>
1.173 +context topological_space
1.174 +begin
1.176 +definition%important "topological_basis B \<longleftrightarrow>
1.177 +  (\<forall>b\<in>B. open b) \<and> (\<forall>x. open x \<longrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
1.179 +lemma topological_basis:
1.180 +  "topological_basis B \<longleftrightarrow> (\<forall>x. open x \<longleftrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
1.181 +  unfolding topological_basis_def
1.182 +  apply safe
1.183 +     apply fastforce
1.184 +    apply fastforce
1.185 +   apply (erule_tac x=x in allE, simp)
1.186 +   apply (rule_tac x="{x}" in exI, auto)
1.187 +  done
1.189 +lemma topological_basis_iff:
1.190 +  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
1.191 +  shows "topological_basis B \<longleftrightarrow> (\<forall>O'. open O' \<longrightarrow> (\<forall>x\<in>O'. \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'))"
1.192 +    (is "_ \<longleftrightarrow> ?rhs")
1.193 +proof safe
1.194 +  fix O' and x::'a
1.195 +  assume H: "topological_basis B" "open O'" "x \<in> O'"
1.196 +  then have "(\<exists>B'\<subseteq>B. \<Union>B' = O')" by (simp add: topological_basis_def)
1.197 +  then obtain B' where "B' \<subseteq> B" "O' = \<Union>B'" by auto
1.198 +  then show "\<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'" using H by auto
1.199 +next
1.200 +  assume H: ?rhs
1.201 +  show "topological_basis B"
1.202 +    using assms unfolding topological_basis_def
1.203 +  proof safe
1.204 +    fix O' :: "'a set"
1.205 +    assume "open O'"
1.206 +    with H obtain f where "\<forall>x\<in>O'. f x \<in> B \<and> x \<in> f x \<and> f x \<subseteq> O'"
1.207 +      by (force intro: bchoice simp: Bex_def)
1.208 +    then show "\<exists>B'\<subseteq>B. \<Union>B' = O'"
1.209 +      by (auto intro: exI[where x="{f x |x. x \<in> O'}"])
1.210 +  qed
1.211 +qed
1.213 +lemma topological_basisI:
1.214 +  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
1.215 +    and "\<And>O' x. open O' \<Longrightarrow> x \<in> O' \<Longrightarrow> \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'"
1.216 +  shows "topological_basis B"
1.217 +  using assms by (subst topological_basis_iff) auto
1.219 +lemma topological_basisE:
1.220 +  fixes O'
1.221 +  assumes "topological_basis B"
1.222 +    and "open O'"
1.223 +    and "x \<in> O'"
1.224 +  obtains B' where "B' \<in> B" "x \<in> B'" "B' \<subseteq> O'"
1.225 +proof atomize_elim
1.226 +  from assms have "\<And>B'. B'\<in>B \<Longrightarrow> open B'"
1.227 +    by (simp add: topological_basis_def)
1.228 +  with topological_basis_iff assms
1.229 +  show  "\<exists>B'. B' \<in> B \<and> x \<in> B' \<and> B' \<subseteq> O'"
1.230 +    using assms by (simp add: Bex_def)
1.231 +qed
1.233 +lemma topological_basis_open:
1.234 +  assumes "topological_basis B"
1.235 +    and "X \<in> B"
1.236 +  shows "open X"
1.237 +  using assms by (simp add: topological_basis_def)
1.239 +lemma topological_basis_imp_subbasis:
1.240 +  assumes B: "topological_basis B"
1.241 +  shows "open = generate_topology B"
1.242 +proof (intro ext iffI)
1.243 +  fix S :: "'a set"
1.244 +  assume "open S"
1.245 +  with B obtain B' where "B' \<subseteq> B" "S = \<Union>B'"
1.246 +    unfolding topological_basis_def by blast
1.247 +  then show "generate_topology B S"
1.248 +    by (auto intro: generate_topology.intros dest: topological_basis_open)
1.249 +next
1.250 +  fix S :: "'a set"
1.251 +  assume "generate_topology B S"
1.252 +  then show "open S"
1.253 +    by induct (auto dest: topological_basis_open[OF B])
1.254 +qed
1.256 +lemma basis_dense:
1.257 +  fixes B :: "'a set set"
1.258 +    and f :: "'a set \<Rightarrow> 'a"
1.259 +  assumes "topological_basis B"
1.260 +    and choosefrom_basis: "\<And>B'. B' \<noteq> {} \<Longrightarrow> f B' \<in> B'"
1.261 +  shows "\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>B' \<in> B. f B' \<in> X)"
1.262 +proof (intro allI impI)
1.263 +  fix X :: "'a set"
1.264 +  assume "open X" and "X \<noteq> {}"
1.265 +  from topological_basisE[OF \<open>topological_basis B\<close> \<open>open X\<close> choosefrom_basis[OF \<open>X \<noteq> {}\<close>]]
1.266 +  obtain B' where "B' \<in> B" "f X \<in> B'" "B' \<subseteq> X" .
1.267 +  then show "\<exists>B'\<in>B. f B' \<in> X"
1.268 +    by (auto intro!: choosefrom_basis)
1.269 +qed
1.271 +end
1.273 +lemma topological_basis_prod:
1.274 +  assumes A: "topological_basis A"
1.275 +    and B: "topological_basis B"
1.276 +  shows "topological_basis ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))"
1.277 +  unfolding topological_basis_def
1.278 +proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric])
1.279 +  fix S :: "('a \<times> 'b) set"
1.280 +  assume "open S"
1.281 +  then show "\<exists>X\<subseteq>A \<times> B. (\<Union>(a,b)\<in>X. a \<times> b) = S"
1.282 +  proof (safe intro!: exI[of _ "{x\<in>A \<times> B. fst x \<times> snd x \<subseteq> S}"])
1.283 +    fix x y
1.284 +    assume "(x, y) \<in> S"
1.285 +    from open_prod_elim[OF \<open>open S\<close> this]
1.286 +    obtain a b where a: "open a""x \<in> a" and b: "open b" "y \<in> b" and "a \<times> b \<subseteq> S"
1.287 +      by (metis mem_Sigma_iff)
1.288 +    moreover
1.289 +    from A a obtain A0 where "A0 \<in> A" "x \<in> A0" "A0 \<subseteq> a"
1.290 +      by (rule topological_basisE)
1.291 +    moreover
1.292 +    from B b obtain B0 where "B0 \<in> B" "y \<in> B0" "B0 \<subseteq> b"
1.293 +      by (rule topological_basisE)
1.294 +    ultimately show "(x, y) \<in> (\<Union>(a, b)\<in>{X \<in> A \<times> B. fst X \<times> snd X \<subseteq> S}. a \<times> b)"
1.295 +      by (intro UN_I[of "(A0, B0)"]) auto
1.296 +  qed auto
1.297 +qed (metis A B topological_basis_open open_Times)
1.300 +subsection \<open>Countable Basis\<close>
1.302 +locale%important countable_basis =
1.303 +  fixes B :: "'a::topological_space set set"
1.304 +  assumes is_basis: "topological_basis B"
1.305 +    and countable_basis: "countable B"
1.306 +begin
1.308 +lemma open_countable_basis_ex:
1.309 +  assumes "open X"
1.310 +  shows "\<exists>B' \<subseteq> B. X = \<Union>B'"
1.311 +  using assms countable_basis is_basis
1.312 +  unfolding topological_basis_def by blast
1.314 +lemma open_countable_basisE:
1.315 +  assumes "open X"
1.316 +  obtains B' where "B' \<subseteq> B" "X = \<Union>B'"
1.317 +  using assms open_countable_basis_ex
1.318 +  by atomize_elim simp
1.320 +lemma countable_dense_exists:
1.321 +  "\<exists>D::'a set. countable D \<and> (\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>d \<in> D. d \<in> X))"
1.322 +proof -
1.323 +  let ?f = "(\<lambda>B'. SOME x. x \<in> B')"
1.324 +  have "countable (?f ` B)" using countable_basis by simp
1.325 +  with basis_dense[OF is_basis, of ?f] show ?thesis
1.326 +    by (intro exI[where x="?f ` B"]) (metis (mono_tags) all_not_in_conv imageI someI)
1.327 +qed
1.329 +lemma countable_dense_setE:
1.330 +  obtains D :: "'a set"
1.331 +  where "countable D" "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d \<in> D. d \<in> X"
1.332 +  using countable_dense_exists by blast
1.334 +end
1.336 +lemma (in first_countable_topology) first_countable_basisE:
1.337 +  fixes x :: 'a
1.338 +  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
1.339 +    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
1.340 +proof -
1.341 +  obtain \<A> where \<A>: "(\<forall>i::nat. x \<in> \<A> i \<and> open (\<A> i))" "(\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. \<A> i \<subseteq> S))"
1.342 +    using first_countable_basis[of x] by metis
1.343 +  show thesis
1.344 +  proof
1.345 +    show "countable (range \<A>)"
1.346 +      by simp
1.347 +  qed (use \<A> in auto)
1.348 +qed
1.350 +lemma (in first_countable_topology) first_countable_basis_Int_stableE:
1.351 +  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
1.352 +    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
1.353 +    "\<And>A B. A \<in> \<A> \<Longrightarrow> B \<in> \<A> \<Longrightarrow> A \<inter> B \<in> \<A>"
1.354 +proof atomize_elim
1.355 +  obtain \<B> where \<B>:
1.356 +    "countable \<B>"
1.357 +    "\<And>B. B \<in> \<B> \<Longrightarrow> x \<in> B"
1.358 +    "\<And>B. B \<in> \<B> \<Longrightarrow> open B"
1.359 +    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>B\<in>\<B>. B \<subseteq> S"
1.360 +    by (rule first_countable_basisE) blast
1.361 +  define \<A> where [abs_def]:
1.362 +    "\<A> = (\<lambda>N. \<Inter>((\<lambda>n. from_nat_into \<B> n) ` N)) ` (Collect finite::nat set set)"
1.363 +  then show "\<exists>\<A>. countable \<A> \<and> (\<forall>A. A \<in> \<A> \<longrightarrow> x \<in> A) \<and> (\<forall>A. A \<in> \<A> \<longrightarrow> open A) \<and>
1.364 +        (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)) \<and> (\<forall>A B. A \<in> \<A> \<longrightarrow> B \<in> \<A> \<longrightarrow> A \<inter> B \<in> \<A>)"
1.365 +  proof (safe intro!: exI[where x=\<A>])
1.366 +    show "countable \<A>"
1.367 +      unfolding \<A>_def by (intro countable_image countable_Collect_finite)
1.368 +    fix A
1.369 +    assume "A \<in> \<A>"
1.370 +    then show "x \<in> A" "open A"
1.371 +      using \<B>(4)[OF open_UNIV] by (auto simp: \<A>_def intro: \<B> from_nat_into)
1.372 +  next
1.373 +    let ?int = "\<lambda>N. \<Inter>(from_nat_into \<B> ` N)"
1.374 +    fix A B
1.375 +    assume "A \<in> \<A>" "B \<in> \<A>"
1.376 +    then obtain N M where "A = ?int N" "B = ?int M" "finite (N \<union> M)"
1.377 +      by (auto simp: \<A>_def)
1.378 +    then show "A \<inter> B \<in> \<A>"
1.379 +      by (auto simp: \<A>_def intro!: image_eqI[where x="N \<union> M"])
1.380 +  next
1.381 +    fix S
1.382 +    assume "open S" "x \<in> S"
1.383 +    then obtain a where a: "a\<in>\<B>" "a \<subseteq> S" using \<B> by blast
1.384 +    then show "\<exists>a\<in>\<A>. a \<subseteq> S" using a \<B>
1.385 +      by (intro bexI[where x=a]) (auto simp: \<A>_def intro: image_eqI[where x="{to_nat_on \<B> a}"])
1.386 +  qed
1.387 +qed
1.389 +lemma (in topological_space) first_countableI:
1.390 +  assumes "countable \<A>"
1.391 +    and 1: "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
1.392 +    and 2: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>A\<in>\<A>. A \<subseteq> S"
1.393 +  shows "\<exists>\<A>::nat \<Rightarrow> 'a set. (\<forall>i. x \<in> \<A> i \<and> open (\<A> i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. \<A> i \<subseteq> S))"
1.394 +proof (safe intro!: exI[of _ "from_nat_into \<A>"])
1.395 +  fix i
1.396 +  have "\<A> \<noteq> {}" using 2[of UNIV] by auto
1.397 +  show "x \<in> from_nat_into \<A> i" "open (from_nat_into \<A> i)"
1.398 +    using range_from_nat_into_subset[OF \<open>\<A> \<noteq> {}\<close>] 1 by auto
1.399 +next
1.400 +  fix S
1.401 +  assume "open S" "x\<in>S" from 2[OF this]
1.402 +  show "\<exists>i. from_nat_into \<A> i \<subseteq> S"
1.403 +    using subset_range_from_nat_into[OF \<open>countable \<A>\<close>] by auto
1.404 +qed
1.406 +instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology
1.407 +proof
1.408 +  fix x :: "'a \<times> 'b"
1.409 +  obtain \<A> where \<A>:
1.410 +      "countable \<A>"
1.411 +      "\<And>a. a \<in> \<A> \<Longrightarrow> fst x \<in> a"
1.412 +      "\<And>a. a \<in> \<A> \<Longrightarrow> open a"
1.413 +      "\<And>S. open S \<Longrightarrow> fst x \<in> S \<Longrightarrow> \<exists>a\<in>\<A>. a \<subseteq> S"
1.414 +    by (rule first_countable_basisE[of "fst x"]) blast
1.415 +  obtain B where B:
1.416 +      "countable B"
1.417 +      "\<And>a. a \<in> B \<Longrightarrow> snd x \<in> a"
1.418 +      "\<And>a. a \<in> B \<Longrightarrow> open a"
1.419 +      "\<And>S. open S \<Longrightarrow> snd x \<in> S \<Longrightarrow> \<exists>a\<in>B. a \<subseteq> S"
1.420 +    by (rule first_countable_basisE[of "snd x"]) blast
1.421 +  show "\<exists>\<A>::nat \<Rightarrow> ('a \<times> 'b) set.
1.422 +    (\<forall>i. x \<in> \<A> i \<and> open (\<A> i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. \<A> i \<subseteq> S))"
1.423 +  proof (rule first_countableI[of "(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B)"], safe)
1.424 +    fix a b
1.425 +    assume x: "a \<in> \<A>" "b \<in> B"
1.426 +    show "x \<in> a \<times> b"
1.427 +      by (simp add: \<A>(2) B(2) mem_Times_iff x)
1.428 +    show "open (a \<times> b)"
1.429 +      by (simp add: \<A>(3) B(3) open_Times x)
1.430 +  next
1.431 +    fix S
1.432 +    assume "open S" "x \<in> S"
1.433 +    then obtain a' b' where a'b': "open a'" "open b'" "x \<in> a' \<times> b'" "a' \<times> b' \<subseteq> S"
1.434 +      by (rule open_prod_elim)
1.435 +    moreover
1.436 +    from a'b' \<A>(4)[of a'] B(4)[of b']
1.437 +    obtain a b where "a \<in> \<A>" "a \<subseteq> a'" "b \<in> B" "b \<subseteq> b'"
1.438 +      by auto
1.439 +    ultimately
1.440 +    show "\<exists>a\<in>(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B). a \<subseteq> S"
1.441 +      by (auto intro!: bexI[of _ "a \<times> b"] bexI[of _ a] bexI[of _ b])
1.442 +  qed (simp add: \<A> B)
1.443 +qed
1.445 +class second_countable_topology = topological_space +
1.446 +  assumes ex_countable_subbasis:
1.447 +    "\<exists>B::'a::topological_space set set. countable B \<and> open = generate_topology B"
1.448 +begin
1.450 +lemma ex_countable_basis: "\<exists>B::'a set set. countable B \<and> topological_basis B"
1.451 +proof -
1.452 +  from ex_countable_subbasis obtain B where B: "countable B" "open = generate_topology B"
1.453 +    by blast
1.454 +  let ?B = "Inter ` {b. finite b \<and> b \<subseteq> B }"
1.456 +  show ?thesis
1.457 +  proof (intro exI conjI)
1.458 +    show "countable ?B"
1.459 +      by (intro countable_image countable_Collect_finite_subset B)
1.460 +    {
1.461 +      fix S
1.462 +      assume "open S"
1.463 +      then have "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. (\<Union>b\<in>B'. \<Inter>b) = S"
1.464 +        unfolding B
1.465 +      proof induct
1.466 +        case UNIV
1.467 +        show ?case by (intro exI[of _ "{{}}"]) simp
1.468 +      next
1.469 +        case (Int a b)
1.470 +        then obtain x y where x: "a = \<Union>(Inter ` x)" "\<And>i. i \<in> x \<Longrightarrow> finite i \<and> i \<subseteq> B"
1.471 +          and y: "b = \<Union>(Inter ` y)" "\<And>i. i \<in> y \<Longrightarrow> finite i \<and> i \<subseteq> B"
1.472 +          by blast
1.473 +        show ?case
1.474 +          unfolding x y Int_UN_distrib2
1.475 +          by (intro exI[of _ "{i \<union> j| i j.  i \<in> x \<and> j \<in> y}"]) (auto dest: x(2) y(2))
1.476 +      next
1.477 +        case (UN K)
1.478 +        then have "\<forall>k\<in>K. \<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = k" by auto
1.479 +        then obtain k where
1.480 +            "\<forall>ka\<in>K. k ka \<subseteq> {b. finite b \<and> b \<subseteq> B} \<and> \<Union>(Inter ` (k ka)) = ka"
1.481 +          unfolding bchoice_iff ..
1.482 +        then show "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = \<Union>K"
1.483 +          by (intro exI[of _ "\<Union>(k ` K)"]) auto
1.484 +      next
1.485 +        case (Basis S)
1.486 +        then show ?case
1.487 +          by (intro exI[of _ "{{S}}"]) auto
1.488 +      qed
1.489 +      then have "(\<exists>B'\<subseteq>Inter ` {b. finite b \<and> b \<subseteq> B}. \<Union>B' = S)"
1.490 +        unfolding subset_image_iff by blast }
1.491 +    then show "topological_basis ?B"
1.492 +      unfolding topological_space_class.topological_basis_def
1.493 +      by (safe intro!: topological_space_class.open_Inter)
1.494 +         (simp_all add: B generate_topology.Basis subset_eq)
1.495 +  qed
1.496 +qed
1.498 +end
1.500 +sublocale second_countable_topology <
1.501 +  countable_basis "SOME B. countable B \<and> topological_basis B"
1.502 +  using someI_ex[OF ex_countable_basis]
1.503 +  by unfold_locales safe
1.505 +instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology
1.506 +proof
1.507 +  obtain A :: "'a set set" where "countable A" "topological_basis A"
1.508 +    using ex_countable_basis by auto
1.509 +  moreover
1.510 +  obtain B :: "'b set set" where "countable B" "topological_basis B"
1.511 +    using ex_countable_basis by auto
1.512 +  ultimately show "\<exists>B::('a \<times> 'b) set set. countable B \<and> open = generate_topology B"
1.513 +    by (auto intro!: exI[of _ "(\<lambda>(a, b). a \<times> b) ` (A \<times> B)"] topological_basis_prod
1.514 +      topological_basis_imp_subbasis)
1.515 +qed
1.517 +instance second_countable_topology \<subseteq> first_countable_topology
1.518 +proof
1.519 +  fix x :: 'a
1.520 +  define B :: "'a set set" where "B = (SOME B. countable B \<and> topological_basis B)"
1.521 +  then have B: "countable B" "topological_basis B"
1.522 +    using countable_basis is_basis
1.523 +    by (auto simp: countable_basis is_basis)
1.524 +  then show "\<exists>A::nat \<Rightarrow> 'a set.
1.525 +    (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
1.526 +    by (intro first_countableI[of "{b\<in>B. x \<in> b}"])
1.527 +       (fastforce simp: topological_space_class.topological_basis_def)+
1.528 +qed
1.530 +instance nat :: second_countable_topology
1.531 +proof
1.532 +  show "\<exists>B::nat set set. countable B \<and> open = generate_topology B"
1.533 +    by (intro exI[of _ "range lessThan \<union> range greaterThan"]) (auto simp: open_nat_def)
1.534 +qed
1.536 +lemma countable_separating_set_linorder1:
1.537 +  shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y))"
1.538 +proof -
1.539 +  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
1.540 +  define B1 where "B1 = {(LEAST x. x \<in> U)| U. U \<in> A}"
1.541 +  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
1.542 +  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
1.543 +  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
1.544 +  have "\<exists>b \<in> B1 \<union> B2. x < b \<and> b \<le> y" if "x < y" for x y
1.545 +  proof (cases)
1.546 +    assume "\<exists>z. x < z \<and> z < y"
1.547 +    then obtain z where z: "x < z \<and> z < y" by auto
1.548 +    define U where "U = {x<..<y}"
1.549 +    then have "open U" by simp
1.550 +    moreover have "z \<in> U" using z U_def by simp
1.551 +    ultimately obtain V where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
1.552 +    define w where "w = (SOME x. x \<in> V)"
1.553 +    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
1.554 +    then have "x < w \<and> w \<le> y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
1.555 +    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
1.556 +    ultimately show ?thesis by auto
1.557 +  next
1.558 +    assume "\<not>(\<exists>z. x < z \<and> z < y)"
1.559 +    then have *: "\<And>z. z > x \<Longrightarrow> z \<ge> y" by auto
1.560 +    define U where "U = {x<..}"
1.561 +    then have "open U" by simp
1.562 +    moreover have "y \<in> U" using \<open>x < y\<close> U_def by simp
1.563 +    ultimately obtain "V" where "V \<in> A" "y \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
1.564 +    have "U = {y..}" unfolding U_def using * \<open>x < y\<close> by auto
1.565 +    then have "V \<subseteq> {y..}" using \<open>V \<subseteq> U\<close> by simp
1.566 +    then have "(LEAST w. w \<in> V) = y" using \<open>y \<in> V\<close> by (meson Least_equality atLeast_iff subsetCE)
1.567 +    then have "y \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
1.568 +    moreover have "x < y \<and> y \<le> y" using \<open>x < y\<close> by simp
1.569 +    ultimately show ?thesis by auto
1.570 +  qed
1.571 +  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
1.572 +  ultimately show ?thesis by auto
1.573 +qed
1.575 +lemma countable_separating_set_linorder2:
1.576 +  shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x \<le> b \<and> b < y))"
1.577 +proof -
1.578 +  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
1.579 +  define B1 where "B1 = {(GREATEST x. x \<in> U) | U. U \<in> A}"
1.580 +  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
1.581 +  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
1.582 +  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
1.583 +  have "\<exists>b \<in> B1 \<union> B2. x \<le> b \<and> b < y" if "x < y" for x y
1.584 +  proof (cases)
1.585 +    assume "\<exists>z. x < z \<and> z < y"
1.586 +    then obtain z where z: "x < z \<and> z < y" by auto
1.587 +    define U where "U = {x<..<y}"
1.588 +    then have "open U" by simp
1.589 +    moreover have "z \<in> U" using z U_def by simp
1.590 +    ultimately obtain "V" where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
1.591 +    define w where "w = (SOME x. x \<in> V)"
1.592 +    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
1.593 +    then have "x \<le> w \<and> w < y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
1.594 +    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
1.595 +    ultimately show ?thesis by auto
1.596 +  next
1.597 +    assume "\<not>(\<exists>z. x < z \<and> z < y)"
1.598 +    then have *: "\<And>z. z < y \<Longrightarrow> z \<le> x" using leI by blast
1.599 +    define U where "U = {..<y}"
1.600 +    then have "open U" by simp
1.601 +    moreover have "x \<in> U" using \<open>x < y\<close> U_def by simp
1.602 +    ultimately obtain "V" where "V \<in> A" "x \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
1.603 +    have "U = {..x}" unfolding U_def using * \<open>x < y\<close> by auto
1.604 +    then have "V \<subseteq> {..x}" using \<open>V \<subseteq> U\<close> by simp
1.605 +    then have "(GREATEST x. x \<in> V) = x" using \<open>x \<in> V\<close> by (meson Greatest_equality atMost_iff subsetCE)
1.606 +    then have "x \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
1.607 +    moreover have "x \<le> x \<and> x < y" using \<open>x < y\<close> by simp
1.608 +    ultimately show ?thesis by auto
1.609 +  qed
1.610 +  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
1.611 +  ultimately show ?thesis by auto
1.612 +qed
1.614 +lemma countable_separating_set_dense_linorder:
1.615 +  shows "\<exists>B::('a::{linorder_topology, dense_linorder, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b < y))"
1.616 +proof -
1.617 +  obtain B::"'a set" where B: "countable B" "\<And>x y. x < y \<Longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y)"
1.618 +    using countable_separating_set_linorder1 by auto
1.619 +  have "\<exists>b \<in> B. x < b \<and> b < y" if "x < y" for x y
1.620 +  proof -
1.621 +    obtain z where "x < z" "z < y" using \<open>x < y\<close> dense by blast
1.622 +    then obtain b where "b \<in> B" "x < b \<and> b \<le> z" using B(2) by auto
1.623 +    then have "x < b \<and> b < y" using \<open>z < y\<close> by auto
1.624 +    then show ?thesis using \<open>b \<in> B\<close> by auto
1.625 +  qed
1.626 +  then show ?thesis using B(1) by auto
1.627 +qed
1.629 +subsection%important \<open>Polish spaces\<close>
1.631 +text \<open>Textbooks define Polish spaces as completely metrizable.
1.632 +  We assume the topology to be complete for a given metric.\<close>
1.634 +class polish_space = complete_space + second_countable_topology
1.636 +subsection \<open>General notion of a topology as a value\<close>
1.638 +definition%important "istopology L \<longleftrightarrow>
1.639 +  L {} \<and> (\<forall>S T. L S \<longrightarrow> L T \<longrightarrow> L (S \<inter> T)) \<and> (\<forall>K. Ball K L \<longrightarrow> L (\<Union>K))"
1.641 +typedef%important 'a topology = "{L::('a set) \<Rightarrow> bool. istopology L}"
1.642 +  morphisms "openin" "topology"
1.643 +  unfolding istopology_def by blast
1.645 +lemma istopology_openin[intro]: "istopology(openin U)"
1.646 +  using openin[of U] by blast
1.648 +lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
1.649 +  using topology_inverse[unfolded mem_Collect_eq] .
1.651 +lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
1.652 +  using topology_inverse[of U] istopology_openin[of "topology U"] by auto
1.654 +lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
1.655 +proof
1.656 +  assume "T1 = T2"
1.657 +  then show "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp
1.658 +next
1.659 +  assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
1.660 +  then have "openin T1 = openin T2" by (simp add: fun_eq_iff)
1.661 +  then have "topology (openin T1) = topology (openin T2)" by simp
1.662 +  then show "T1 = T2" unfolding openin_inverse .
1.663 +qed
1.666 +text\<open>The "universe": the union of all sets in the topology.\<close>
1.667 +definition "topspace T = \<Union>{S. openin T S}"
1.669 +subsubsection \<open>Main properties of open sets\<close>
1.671 +proposition openin_clauses:
1.672 +  fixes U :: "'a topology"
1.673 +  shows
1.674 +    "openin U {}"
1.675 +    "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
1.676 +    "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
1.677 +  using openin[of U] unfolding istopology_def mem_Collect_eq by fast+
1.679 +lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
1.680 +  unfolding topspace_def by blast
1.682 +lemma openin_empty[simp]: "openin U {}"
1.683 +  by (rule openin_clauses)
1.685 +lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
1.686 +  by (rule openin_clauses)
1.688 +lemma openin_Union[intro]: "(\<And>S. S \<in> K \<Longrightarrow> openin U S) \<Longrightarrow> openin U (\<Union>K)"
1.689 +  using openin_clauses by blast
1.691 +lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
1.692 +  using openin_Union[of "{S,T}" U] by auto
1.694 +lemma openin_topspace[intro, simp]: "openin U (topspace U)"
1.695 +  by (force simp: openin_Union topspace_def)
1.697 +lemma openin_subopen: "openin U S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>T. openin U T \<and> x \<in> T \<and> T \<subseteq> S)"
1.698 +  (is "?lhs \<longleftrightarrow> ?rhs")
1.699 +proof
1.700 +  assume ?lhs
1.701 +  then show ?rhs by auto
1.702 +next
1.703 +  assume H: ?rhs
1.704 +  let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"
1.705 +  have "openin U ?t" by (force simp: openin_Union)
1.706 +  also have "?t = S" using H by auto
1.707 +  finally show "openin U S" .
1.708 +qed
1.710 +lemma openin_INT [intro]:
1.711 +  assumes "finite I"
1.712 +          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
1.713 +  shows "openin T ((\<Inter>i \<in> I. U i) \<inter> topspace T)"
1.714 +using assms by (induct, auto simp: inf_sup_aci(2) openin_Int)
1.716 +lemma openin_INT2 [intro]:
1.717 +  assumes "finite I" "I \<noteq> {}"
1.718 +          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
1.719 +  shows "openin T (\<Inter>i \<in> I. U i)"
1.720 +proof -
1.721 +  have "(\<Inter>i \<in> I. U i) \<subseteq> topspace T"
1.722 +    using \<open>I \<noteq> {}\<close> openin_subset[OF assms(3)] by auto
1.723 +  then show ?thesis
1.724 +    using openin_INT[of _ _ U, OF assms(1) assms(3)] by (simp add: inf.absorb2 inf_commute)
1.725 +qed
1.727 +lemma openin_Inter [intro]:
1.728 +  assumes "finite \<F>" "\<F> \<noteq> {}" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (\<Inter>\<F>)"
1.729 +  by (metis (full_types) assms openin_INT2 image_ident)
1.731 +lemma openin_Int_Inter:
1.732 +  assumes "finite \<F>" "openin T U" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (U \<inter> \<Inter>\<F>)"
1.733 +  using openin_Inter [of "insert U \<F>"] assms by auto
1.736 +subsubsection \<open>Closed sets\<close>
1.738 +definition%important "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
1.740 +lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U"
1.741 +  by (metis closedin_def)
1.743 +lemma closedin_empty[simp]: "closedin U {}"
1.744 +  by (simp add: closedin_def)
1.746 +lemma closedin_topspace[intro, simp]: "closedin U (topspace U)"
1.747 +  by (simp add: closedin_def)
1.749 +lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
1.750 +  by (auto simp: Diff_Un closedin_def)
1.752 +lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union>{A - s|s. s\<in>S}"
1.753 +  by auto
1.755 +lemma closedin_Union:
1.756 +  assumes "finite S" "\<And>T. T \<in> S \<Longrightarrow> closedin U T"
1.757 +    shows "closedin U (\<Union>S)"
1.758 +  using assms by induction auto
1.760 +lemma closedin_Inter[intro]:
1.761 +  assumes Ke: "K \<noteq> {}"
1.762 +    and Kc: "\<And>S. S \<in>K \<Longrightarrow> closedin U S"
1.763 +  shows "closedin U (\<Inter>K)"
1.764 +  using Ke Kc unfolding closedin_def Diff_Inter by auto
1.766 +lemma closedin_INT[intro]:
1.767 +  assumes "A \<noteq> {}" "\<And>x. x \<in> A \<Longrightarrow> closedin U (B x)"
1.768 +  shows "closedin U (\<Inter>x\<in>A. B x)"
1.769 +  apply (rule closedin_Inter)
1.770 +  using assms
1.771 +  apply auto
1.772 +  done
1.774 +lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
1.775 +  using closedin_Inter[of "{S,T}" U] by auto
1.777 +lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
1.778 +  apply (auto simp: closedin_def Diff_Diff_Int inf_absorb2)
1.779 +  apply (metis openin_subset subset_eq)
1.780 +  done
1.782 +lemma openin_closedin: "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
1.783 +  by (simp add: openin_closedin_eq)
1.785 +lemma openin_diff[intro]:
1.786 +  assumes oS: "openin U S"
1.787 +    and cT: "closedin U T"
1.788 +  shows "openin U (S - T)"
1.789 +proof -
1.790 +  have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
1.791 +    by (auto simp: topspace_def openin_subset)
1.792 +  then show ?thesis using oS cT
1.793 +    by (auto simp: closedin_def)
1.794 +qed
1.796 +lemma closedin_diff[intro]:
1.797 +  assumes oS: "closedin U S"
1.798 +    and cT: "openin U T"
1.799 +  shows "closedin U (S - T)"
1.800 +proof -
1.801 +  have "S - T = S \<inter> (topspace U - T)"
1.802 +    using closedin_subset[of U S] oS cT by (auto simp: topspace_def)
1.803 +  then show ?thesis
1.804 +    using oS cT by (auto simp: openin_closedin_eq)
1.805 +qed
1.808 +subsection\<open>The discrete topology\<close>
1.810 +definition discrete_topology where "discrete_topology U \<equiv> topology (\<lambda>S. S \<subseteq> U)"
1.812 +lemma openin_discrete_topology [simp]: "openin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
1.813 +proof -
1.814 +  have "istopology (\<lambda>S. S \<subseteq> U)"
1.815 +    by (auto simp: istopology_def)
1.816 +  then show ?thesis
1.817 +    by (simp add: discrete_topology_def topology_inverse')
1.818 +qed
1.820 +lemma topspace_discrete_topology [simp]: "topspace(discrete_topology U) = U"
1.821 +  by (meson openin_discrete_topology openin_subset openin_topspace order_refl subset_antisym)
1.823 +lemma closedin_discrete_topology [simp]: "closedin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
1.824 +  by (simp add: closedin_def)
1.826 +lemma discrete_topology_unique:
1.827 +   "discrete_topology U = X \<longleftrightarrow> topspace X = U \<and> (\<forall>x \<in> U. openin X {x})" (is "?lhs = ?rhs")
1.828 +proof
1.829 +  assume R: ?rhs
1.830 +  then have "openin X S" if "S \<subseteq> U" for S
1.831 +    using openin_subopen subsetD that by fastforce
1.832 +  moreover have "x \<in> topspace X" if "openin X S" and "x \<in> S" for x S
1.833 +    using openin_subset that by blast
1.834 +  ultimately
1.835 +  show ?lhs
1.836 +    using R by (auto simp: topology_eq)
1.837 +qed auto
1.839 +lemma discrete_topology_unique_alt:
1.840 +  "discrete_topology U = X \<longleftrightarrow> topspace X \<subseteq> U \<and> (\<forall>x \<in> U. openin X {x})"
1.841 +  using openin_subset
1.842 +  by (auto simp: discrete_topology_unique)
1.844 +lemma subtopology_eq_discrete_topology_empty:
1.845 +   "X = discrete_topology {} \<longleftrightarrow> topspace X = {}"
1.846 +  using discrete_topology_unique [of "{}" X] by auto
1.848 +lemma subtopology_eq_discrete_topology_sing:
1.849 +   "X = discrete_topology {a} \<longleftrightarrow> topspace X = {a}"
1.850 +  by (metis discrete_topology_unique openin_topspace singletonD)
1.853 +subsection \<open>Subspace topology\<close>
1.855 +definition%important "subtopology U V = topology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
1.857 +lemma istopology_subtopology: "istopology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
1.858 +  (is "istopology ?L")
1.859 +proof -
1.860 +  have "?L {}" by blast
1.861 +  {
1.862 +    fix A B
1.863 +    assume A: "?L A" and B: "?L B"
1.864 +    from A B obtain Sa and Sb where Sa: "openin U Sa" "A = Sa \<inter> V" and Sb: "openin U Sb" "B = Sb \<inter> V"
1.865 +      by blast
1.866 +    have "A \<inter> B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"
1.867 +      using Sa Sb by blast+
1.868 +    then have "?L (A \<inter> B)" by blast
1.869 +  }
1.870 +  moreover
1.871 +  {
1.872 +    fix K
1.873 +    assume K: "K \<subseteq> Collect ?L"
1.874 +    have th0: "Collect ?L = (\<lambda>S. S \<inter> V) ` Collect (openin U)"
1.875 +      by blast
1.876 +    from K[unfolded th0 subset_image_iff]
1.877 +    obtain Sk where Sk: "Sk \<subseteq> Collect (openin U)" "K = (\<lambda>S. S \<inter> V) ` Sk"
1.878 +      by blast
1.879 +    have "\<Union>K = (\<Union>Sk) \<inter> V"
1.880 +      using Sk by auto
1.881 +    moreover have "openin U (\<Union>Sk)"
1.882 +      using Sk by (auto simp: subset_eq)
1.883 +    ultimately have "?L (\<Union>K)" by blast
1.884 +  }
1.885 +  ultimately show ?thesis
1.886 +    unfolding subset_eq mem_Collect_eq istopology_def by auto
1.887 +qed
1.889 +lemma openin_subtopology: "openin (subtopology U V) S \<longleftrightarrow> (\<exists>T. openin U T \<and> S = T \<inter> V)"
1.890 +  unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
1.891 +  by auto
1.893 +lemma openin_subtopology_Int:
1.894 +   "openin X S \<Longrightarrow> openin (subtopology X T) (S \<inter> T)"
1.895 +  using openin_subtopology by auto
1.897 +lemma openin_subtopology_Int2:
1.898 +   "openin X T \<Longrightarrow> openin (subtopology X S) (S \<inter> T)"
1.899 +  using openin_subtopology by auto
1.901 +lemma openin_subtopology_diff_closed:
1.902 +   "\<lbrakk>S \<subseteq> topspace X; closedin X T\<rbrakk> \<Longrightarrow> openin (subtopology X S) (S - T)"
1.903 +  unfolding closedin_def openin_subtopology
1.904 +  by (rule_tac x="topspace X - T" in exI) auto
1.906 +lemma openin_relative_to: "(openin X relative_to S) = openin (subtopology X S)"
1.907 +  by (force simp: relative_to_def openin_subtopology)
1.909 +lemma topspace_subtopology: "topspace (subtopology U V) = topspace U \<inter> V"
1.910 +  by (auto simp: topspace_def openin_subtopology)
1.912 +lemma closedin_subtopology: "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
1.913 +  unfolding closedin_def topspace_subtopology
1.914 +  by (auto simp: openin_subtopology)
1.916 +lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
1.917 +  unfolding openin_subtopology
1.918 +  by auto (metis IntD1 in_mono openin_subset)
1.920 +lemma subtopology_subtopology:
1.921 +   "subtopology (subtopology X S) T = subtopology X (S \<inter> T)"
1.922 +proof -
1.923 +  have eq: "\<And>T'. (\<exists>S'. T' = S' \<inter> T \<and> (\<exists>T. openin X T \<and> S' = T \<inter> S)) = (\<exists>Sa. T' = Sa \<inter> (S \<inter> T) \<and> openin X Sa)"
1.924 +    by (metis inf_assoc)
1.925 +  have "subtopology (subtopology X S) T = topology (\<lambda>Ta. \<exists>Sa. Ta = Sa \<inter> T \<and> openin (subtopology X S) Sa)"
1.926 +    by (simp add: subtopology_def)
1.927 +  also have "\<dots> = subtopology X (S \<inter> T)"
1.929 +  finally show ?thesis .
1.930 +qed
1.932 +lemma openin_subtopology_alt:
1.933 +     "openin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (openin X)"
1.934 +  by (simp add: image_iff inf_commute openin_subtopology)
1.936 +lemma closedin_subtopology_alt:
1.937 +     "closedin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (closedin X)"
1.938 +  by (simp add: image_iff inf_commute closedin_subtopology)
1.940 +lemma subtopology_superset:
1.941 +  assumes UV: "topspace U \<subseteq> V"
1.942 +  shows "subtopology U V = U"
1.943 +proof -
1.944 +  {
1.945 +    fix S
1.946 +    {
1.947 +      fix T
1.948 +      assume T: "openin U T" "S = T \<inter> V"
1.949 +      from T openin_subset[OF T(1)] UV have eq: "S = T"
1.950 +        by blast
1.951 +      have "openin U S"
1.952 +        unfolding eq using T by blast
1.953 +    }
1.954 +    moreover
1.955 +    {
1.956 +      assume S: "openin U S"
1.957 +      then have "\<exists>T. openin U T \<and> S = T \<inter> V"
1.958 +        using openin_subset[OF S] UV by auto
1.959 +    }
1.960 +    ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S"
1.961 +      by blast
1.962 +  }
1.963 +  then show ?thesis
1.964 +    unfolding topology_eq openin_subtopology by blast
1.965 +qed
1.967 +lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
1.968 +  by (simp add: subtopology_superset)
1.970 +lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
1.971 +  by (simp add: subtopology_superset)
1.973 +lemma openin_subtopology_empty:
1.974 +   "openin (subtopology U {}) S \<longleftrightarrow> S = {}"
1.975 +by (metis Int_empty_right openin_empty openin_subtopology)
1.977 +lemma closedin_subtopology_empty:
1.978 +   "closedin (subtopology U {}) S \<longleftrightarrow> S = {}"
1.979 +by (metis Int_empty_right closedin_empty closedin_subtopology)
1.981 +lemma closedin_subtopology_refl [simp]:
1.982 +   "closedin (subtopology U X) X \<longleftrightarrow> X \<subseteq> topspace U"
1.983 +by (metis closedin_def closedin_topspace inf.absorb_iff2 le_inf_iff topspace_subtopology)
1.985 +lemma closedin_topspace_empty: "topspace T = {} \<Longrightarrow> (closedin T S \<longleftrightarrow> S = {})"
1.986 +  by (simp add: closedin_def)
1.988 +lemma openin_imp_subset:
1.989 +   "openin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
1.990 +by (metis Int_iff openin_subtopology subsetI)
1.992 +lemma closedin_imp_subset:
1.993 +   "closedin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
1.994 +by (simp add: closedin_def topspace_subtopology)
1.996 +lemma openin_open_subtopology:
1.997 +     "openin X S \<Longrightarrow> openin (subtopology X S) T \<longleftrightarrow> openin X T \<and> T \<subseteq> S"
1.998 +  by (metis inf.orderE openin_Int openin_imp_subset openin_subtopology)
1.1000 +lemma closedin_closed_subtopology:
1.1001 +     "closedin X S \<Longrightarrow> (closedin (subtopology X S) T \<longleftrightarrow> closedin X T \<and> T \<subseteq> S)"
1.1002 +  by (metis closedin_Int closedin_imp_subset closedin_subtopology inf.orderE)
1.1004 +lemma openin_subtopology_Un:
1.1005 +    "\<lbrakk>openin (subtopology X T) S; openin (subtopology X U) S\<rbrakk>
1.1006 +     \<Longrightarrow> openin (subtopology X (T \<union> U)) S"
1.1007 +by (simp add: openin_subtopology) blast
1.1009 +lemma closedin_subtopology_Un:
1.1010 +    "\<lbrakk>closedin (subtopology X T) S; closedin (subtopology X U) S\<rbrakk>
1.1011 +     \<Longrightarrow> closedin (subtopology X (T \<union> U)) S"
1.1012 +by (simp add: closedin_subtopology) blast
1.1015 +subsection \<open>The standard Euclidean topology\<close>
1.1017 +definition%important euclidean :: "'a::topological_space topology"
1.1018 +  where "euclidean = topology open"
1.1020 +lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
1.1021 +  unfolding euclidean_def
1.1022 +  apply (rule cong[where x=S and y=S])
1.1023 +  apply (rule topology_inverse[symmetric])
1.1024 +  apply (auto simp: istopology_def)
1.1025 +  done
1.1027 +declare open_openin [symmetric, simp]
1.1029 +lemma topspace_euclidean [simp]: "topspace euclidean = UNIV"
1.1030 +  by (force simp: topspace_def)
1.1032 +lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
1.1033 +  by (simp add: topspace_subtopology)
1.1035 +lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
1.1036 +  by (simp add: closed_def closedin_def Compl_eq_Diff_UNIV)
1.1038 +declare closed_closedin [symmetric, simp]
1.1040 +lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
1.1041 +  using openI by auto
1.1043 +lemma openin_subtopology_self [simp]: "openin (subtopology euclidean S) S"
1.1044 +  by (metis openin_topspace topspace_euclidean_subtopology)
1.1046 +subsubsection\<open>The most basic facts about the usual topology and metric on R\<close>
1.1048 +abbreviation euclideanreal :: "real topology"
1.1049 +  where "euclideanreal \<equiv> topology open"
1.1051 +lemma real_openin [simp]: "openin euclideanreal S = open S"
1.1052 +  by (simp add: euclidean_def open_openin)
1.1054 +lemma topspace_euclideanreal [simp]: "topspace euclideanreal = UNIV"
1.1055 +  using openin_subset open_UNIV real_openin by blast
1.1057 +lemma topspace_euclideanreal_subtopology [simp]:
1.1058 +   "topspace (subtopology euclideanreal S) = S"
1.1059 +  by (simp add: topspace_subtopology)
1.1061 +lemma real_closedin [simp]: "closedin euclideanreal S = closed S"
1.1062 +  by (simp add: closed_closedin euclidean_def)
1.1064 +subsection \<open>Basic "localization" results are handy for connectedness.\<close>
1.1066 +lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
1.1067 +  by (auto simp: openin_subtopology)
1.1069 +lemma openin_Int_open:
1.1070 +   "\<lbrakk>openin (subtopology euclidean U) S; open T\<rbrakk>
1.1071 +        \<Longrightarrow> openin (subtopology euclidean U) (S \<inter> T)"
1.1072 +by (metis open_Int Int_assoc openin_open)
1.1074 +lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
1.1075 +  by (auto simp: openin_open)
1.1077 +lemma open_openin_trans[trans]:
1.1078 +  "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
1.1079 +  by (metis Int_absorb1  openin_open_Int)
1.1081 +lemma open_subset: "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
1.1082 +  by (auto simp: openin_open)
1.1084 +lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
1.1085 +  by (simp add: closedin_subtopology Int_ac)
1.1087 +lemma closedin_closed_Int: "closed S \<Longrightarrow> closedin (subtopology euclidean U) (U \<inter> S)"
1.1088 +  by (metis closedin_closed)
1.1090 +lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
1.1091 +  by (auto simp: closedin_closed)
1.1093 +lemma closedin_closed_subset:
1.1094 + "\<lbrakk>closedin (subtopology euclidean U) V; T \<subseteq> U; S = V \<inter> T\<rbrakk>
1.1095 +             \<Longrightarrow> closedin (subtopology euclidean T) S"
1.1096 +  by (metis (no_types, lifting) Int_assoc Int_commute closedin_closed inf.orderE)
1.1098 +lemma finite_imp_closedin:
1.1099 +  fixes S :: "'a::t1_space set"
1.1100 +  shows "\<lbrakk>finite S; S \<subseteq> T\<rbrakk> \<Longrightarrow> closedin (subtopology euclidean T) S"
1.1101 +    by (simp add: finite_imp_closed closed_subset)
1.1103 +lemma closedin_singleton [simp]:
1.1104 +  fixes a :: "'a::t1_space"
1.1105 +  shows "closedin (subtopology euclidean U) {a} \<longleftrightarrow> a \<in> U"
1.1106 +using closedin_subset  by (force intro: closed_subset)
1.1108 +lemma openin_euclidean_subtopology_iff:
1.1109 +  fixes S U :: "'a::metric_space set"
1.1110 +  shows "openin (subtopology euclidean U) S \<longleftrightarrow>
1.1111 +    S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)"
1.1112 +  (is "?lhs \<longleftrightarrow> ?rhs")
1.1113 +proof
1.1114 +  assume ?lhs
1.1115 +  then show ?rhs
1.1116 +    unfolding openin_open open_dist by blast
1.1117 +next
1.1118 +  define T where "T = {x. \<exists>a\<in>S. \<exists>d>0. (\<forall>y\<in>U. dist y a < d \<longrightarrow> y \<in> S) \<and> dist x a < d}"
1.1119 +  have 1: "\<forall>x\<in>T. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> T"
1.1120 +    unfolding T_def
1.1121 +    apply clarsimp
1.1122 +    apply (rule_tac x="d - dist x a" in exI)
1.1123 +    apply (clarsimp simp add: less_diff_eq)
1.1124 +    by (metis dist_commute dist_triangle_lt)
1.1125 +  assume ?rhs then have 2: "S = U \<inter> T"
1.1126 +    unfolding T_def
1.1127 +    by auto (metis dist_self)
1.1128 +  from 1 2 show ?lhs
1.1129 +    unfolding openin_open open_dist by fast
1.1130 +qed
1.1132 +lemma connected_openin:
1.1133 +      "connected S \<longleftrightarrow>
1.1134 +       \<not>(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
1.1135 +                 openin (subtopology euclidean S) E2 \<and>
1.1136 +                 S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
1.1137 +  apply (simp add: connected_def openin_open disjoint_iff_not_equal, safe)
1.1138 +  apply (simp_all, blast+)  (* SLOW *)
1.1139 +  done
1.1141 +lemma connected_openin_eq:
1.1142 +      "connected S \<longleftrightarrow>
1.1143 +       \<not>(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
1.1144 +                 openin (subtopology euclidean S) E2 \<and>
1.1145 +                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
1.1146 +                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
1.1147 +  apply (simp add: connected_openin, safe, blast)
1.1148 +  by (metis Int_lower1 Un_subset_iff openin_open subset_antisym)
1.1150 +lemma connected_closedin:
1.1151 +      "connected S \<longleftrightarrow>
1.1152 +       (\<nexists>E1 E2.
1.1153 +        closedin (subtopology euclidean S) E1 \<and>
1.1154 +        closedin (subtopology euclidean S) E2 \<and>
1.1155 +        S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
1.1156 +       (is "?lhs = ?rhs")
1.1157 +proof
1.1158 +  assume ?lhs
1.1159 +  then show ?rhs
1.1160 +    by (auto simp add: connected_closed closedin_closed)
1.1161 +next
1.1162 +  assume R: ?rhs
1.1163 +  then show ?lhs
1.1164 +  proof (clarsimp simp add: connected_closed closedin_closed)
1.1165 +    fix A B
1.1166 +    assume s_sub: "S \<subseteq> A \<union> B" "B \<inter> S \<noteq> {}"
1.1167 +      and disj: "A \<inter> B \<inter> S = {}"
1.1168 +      and cl: "closed A" "closed B"
1.1169 +    have "S \<inter> (A \<union> B) = S"
1.1170 +      using s_sub(1) by auto
1.1171 +    have "S - A = B \<inter> S"
1.1172 +      using Diff_subset_conv Un_Diff_Int disj s_sub(1) by auto
1.1173 +    then have "S \<inter> A = {}"
1.1174 +      by (metis Diff_Diff_Int Diff_disjoint Un_Diff_Int R cl closedin_closed_Int inf_commute order_refl s_sub(2))
1.1175 +    then show "A \<inter> S = {}"
1.1176 +      by blast
1.1177 +  qed
1.1178 +qed
1.1180 +lemma connected_closedin_eq:
1.1181 +      "connected S \<longleftrightarrow>
1.1182 +           \<not>(\<exists>E1 E2.
1.1183 +                 closedin (subtopology euclidean S) E1 \<and>
1.1184 +                 closedin (subtopology euclidean S) E2 \<and>
1.1185 +                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
1.1186 +                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
1.1187 +  apply (simp add: connected_closedin, safe, blast)
1.1188 +  by (metis Int_lower1 Un_subset_iff closedin_closed subset_antisym)
1.1190 +text \<open>These "transitivity" results are handy too\<close>
1.1192 +lemma openin_trans[trans]:
1.1193 +  "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T \<Longrightarrow>
1.1194 +    openin (subtopology euclidean U) S"
1.1195 +  unfolding open_openin openin_open by blast
1.1197 +lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
1.1198 +  by (auto simp: openin_open intro: openin_trans)
1.1200 +lemma closedin_trans[trans]:
1.1201 +  "closedin (subtopology euclidean T) S \<Longrightarrow> closedin (subtopology euclidean U) T \<Longrightarrow>
1.1202 +    closedin (subtopology euclidean U) S"
1.1203 +  by (auto simp: closedin_closed closed_Inter Int_assoc)
1.1205 +lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
1.1206 +  by (auto simp: closedin_closed intro: closedin_trans)
1.1208 +lemma openin_subtopology_Int_subset:
1.1209 +   "\<lbrakk>openin (subtopology euclidean u) (u \<inter> S); v \<subseteq> u\<rbrakk> \<Longrightarrow> openin (subtopology euclidean v) (v \<inter> S)"
1.1210 +  by (auto simp: openin_subtopology)
1.1212 +lemma openin_open_eq: "open s \<Longrightarrow> (openin (subtopology euclidean s) t \<longleftrightarrow> open t \<and> t \<subseteq> s)"
1.1213 +  using open_subset openin_open_trans openin_subset by fastforce
1.1216 +subsection \<open>Open and closed balls\<close>
1.1218 +definition%important ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
1.1219 +  where "ball x e = {y. dist x y < e}"
1.1221 +definition%important cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
1.1222 +  where "cball x e = {y. dist x y \<le> e}"
1.1224 +definition%important sphere :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
1.1225 +  where "sphere x e = {y. dist x y = e}"
1.1227 +lemma mem_ball [simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e"
1.1228 +  by (simp add: ball_def)
1.1230 +lemma mem_cball [simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e"
1.1231 +  by (simp add: cball_def)
1.1233 +lemma mem_sphere [simp]: "y \<in> sphere x e \<longleftrightarrow> dist x y = e"
1.1234 +  by (simp add: sphere_def)
1.1236 +lemma ball_trivial [simp]: "ball x 0 = {}"
1.1237 +  by (simp add: ball_def)
1.1239 +lemma cball_trivial [simp]: "cball x 0 = {x}"
1.1240 +  by (simp add: cball_def)
1.1242 +lemma sphere_trivial [simp]: "sphere x 0 = {x}"
1.1243 +  by (simp add: sphere_def)
1.1245 +lemma mem_ball_0 [simp]: "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
1.1246 +  for x :: "'a::real_normed_vector"
1.1247 +  by (simp add: dist_norm)
1.1249 +lemma mem_cball_0 [simp]: "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
1.1250 +  for x :: "'a::real_normed_vector"
1.1251 +  by (simp add: dist_norm)
1.1253 +lemma disjoint_ballI: "dist x y \<ge> r+s \<Longrightarrow> ball x r \<inter> ball y s = {}"
1.1254 +  using dist_triangle_less_add not_le by fastforce
1.1256 +lemma disjoint_cballI: "dist x y > r + s \<Longrightarrow> cball x r \<inter> cball y s = {}"
1.1257 +  by (metis add_mono disjoint_iff_not_equal dist_triangle2 dual_order.trans leD mem_cball)
1.1259 +lemma mem_sphere_0 [simp]: "x \<in> sphere 0 e \<longleftrightarrow> norm x = e"
1.1260 +  for x :: "'a::real_normed_vector"
1.1261 +  by (simp add: dist_norm)
1.1263 +lemma sphere_empty [simp]: "r < 0 \<Longrightarrow> sphere a r = {}"
1.1264 +  for a :: "'a::metric_space"
1.1265 +  by auto
1.1267 +lemma centre_in_ball [simp]: "x \<in> ball x e \<longleftrightarrow> 0 < e"
1.1268 +  by simp
1.1270 +lemma centre_in_cball [simp]: "x \<in> cball x e \<longleftrightarrow> 0 \<le> e"
1.1271 +  by simp
1.1273 +lemma ball_subset_cball [simp, intro]: "ball x e \<subseteq> cball x e"
1.1274 +  by (simp add: subset_eq)
1.1276 +lemma mem_ball_imp_mem_cball: "x \<in> ball y e \<Longrightarrow> x \<in> cball y e"
1.1277 +  by (auto simp: mem_ball mem_cball)
1.1279 +lemma sphere_cball [simp,intro]: "sphere z r \<subseteq> cball z r"
1.1280 +  by force
1.1282 +lemma cball_diff_sphere: "cball a r - sphere a r = ball a r"
1.1283 +  by auto
1.1285 +lemma subset_ball[intro]: "d \<le> e \<Longrightarrow> ball x d \<subseteq> ball x e"
1.1286 +  by (simp add: subset_eq)
1.1288 +lemma subset_cball[intro]: "d \<le> e \<Longrightarrow> cball x d \<subseteq> cball x e"
1.1289 +  by (simp add: subset_eq)
1.1291 +lemma mem_ball_leI: "x \<in> ball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> ball y f"
1.1292 +  by (auto simp: mem_ball mem_cball)
1.1294 +lemma mem_cball_leI: "x \<in> cball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> cball y f"
1.1295 +  by (auto simp: mem_ball mem_cball)
1.1297 +lemma cball_trans: "y \<in> cball z b \<Longrightarrow> x \<in> cball y a \<Longrightarrow> x \<in> cball z (b + a)"
1.1298 +  unfolding mem_cball
1.1299 +proof -
1.1300 +  have "dist z x \<le> dist z y + dist y x"
1.1301 +    by (rule dist_triangle)
1.1302 +  also assume "dist z y \<le> b"
1.1303 +  also assume "dist y x \<le> a"
1.1304 +  finally show "dist z x \<le> b + a" by arith
1.1305 +qed
1.1307 +lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
1.1308 +  by (simp add: set_eq_iff) arith
1.1310 +lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
1.1311 +  by (simp add: set_eq_iff)
1.1313 +lemma cball_max_Un: "cball a (max r s) = cball a r \<union> cball a s"
1.1314 +  by (simp add: set_eq_iff) arith
1.1316 +lemma cball_min_Int: "cball a (min r s) = cball a r \<inter> cball a s"
1.1317 +  by (simp add: set_eq_iff)
1.1319 +lemma cball_diff_eq_sphere: "cball a r - ball a r =  sphere a r"
1.1320 +  by (auto simp: cball_def ball_def dist_commute)
1.1323 +  fixes a :: "'a::real_normed_vector"
1.1324 +  shows "(+) b ` ball a r = ball (a+b) r"
1.1325 +apply (intro equalityI subsetI)
1.1326 +apply (force simp: dist_norm)
1.1327 +apply (rule_tac x="x-b" in image_eqI)
1.1328 +apply (auto simp: dist_norm algebra_simps)
1.1329 +done
1.1332 +  fixes a :: "'a::real_normed_vector"
1.1333 +  shows "(+) b ` cball a r = cball (a+b) r"
1.1334 +apply (intro equalityI subsetI)
1.1335 +apply (force simp: dist_norm)
1.1336 +apply (rule_tac x="x-b" in image_eqI)
1.1337 +apply (auto simp: dist_norm algebra_simps)
1.1338 +done
1.1340 +lemma open_ball [intro, simp]: "open (ball x e)"
1.1341 +proof -
1.1342 +  have "open (dist x -` {..<e})"
1.1343 +    by (intro open_vimage open_lessThan continuous_intros)
1.1344 +  also have "dist x -` {..<e} = ball x e"
1.1345 +    by auto
1.1346 +  finally show ?thesis .
1.1347 +qed
1.1349 +lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
1.1350 +  by (simp add: open_dist subset_eq mem_ball Ball_def dist_commute)
1.1352 +lemma openI [intro?]: "(\<And>x. x\<in>S \<Longrightarrow> \<exists>e>0. ball x e \<subseteq> S) \<Longrightarrow> open S"
1.1353 +  by (auto simp: open_contains_ball)
1.1355 +lemma openE[elim?]:
1.1356 +  assumes "open S" "x\<in>S"
1.1357 +  obtains e where "e>0" "ball x e \<subseteq> S"
1.1358 +  using assms unfolding open_contains_ball by auto
1.1360 +lemma open_contains_ball_eq: "open S \<Longrightarrow> x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
1.1361 +  by (metis open_contains_ball subset_eq centre_in_ball)
1.1363 +lemma openin_contains_ball:
1.1364 +    "openin (subtopology euclidean t) s \<longleftrightarrow>
1.1365 +     s \<subseteq> t \<and> (\<forall>x \<in> s. \<exists>e. 0 < e \<and> ball x e \<inter> t \<subseteq> s)"
1.1366 +    (is "?lhs = ?rhs")
1.1367 +proof
1.1368 +  assume ?lhs
1.1369 +  then show ?rhs
1.1370 +    apply (simp add: openin_open)
1.1371 +    apply (metis Int_commute Int_mono inf.cobounded2 open_contains_ball order_refl subsetCE)
1.1372 +    done
1.1373 +next
1.1374 +  assume ?rhs
1.1375 +  then show ?lhs
1.1376 +    apply (simp add: openin_euclidean_subtopology_iff)
1.1377 +    by (metis (no_types) Int_iff dist_commute inf.absorb_iff2 mem_ball)
1.1378 +qed
1.1380 +lemma openin_contains_cball:
1.1381 +   "openin (subtopology euclidean t) s \<longleftrightarrow>
1.1382 +        s \<subseteq> t \<and>
1.1383 +        (\<forall>x \<in> s. \<exists>e. 0 < e \<and> cball x e \<inter> t \<subseteq> s)"
1.1385 +apply (rule iffI)
1.1386 +apply (auto dest!: bspec)
1.1387 +apply (rule_tac x="e/2" in exI, force+)
1.1388 +done
1.1390 +lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
1.1391 +  unfolding mem_ball set_eq_iff
1.1392 +  apply (simp add: not_less)
1.1393 +  apply (metis zero_le_dist order_trans dist_self)
1.1394 +  done
1.1396 +lemma ball_empty: "e \<le> 0 \<Longrightarrow> ball x e = {}" by simp
1.1398 +lemma closed_cball [iff]: "closed (cball x e)"
1.1399 +proof -
1.1400 +  have "closed (dist x -` {..e})"
1.1401 +    by (intro closed_vimage closed_atMost continuous_intros)
1.1402 +  also have "dist x -` {..e} = cball x e"
1.1403 +    by auto
1.1404 +  finally show ?thesis .
1.1405 +qed
1.1407 +lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
1.1408 +proof -
1.1409 +  {
1.1410 +    fix x and e::real
1.1411 +    assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
1.1412 +    then have "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
1.1413 +  }
1.1414 +  moreover
1.1415 +  {
1.1416 +    fix x and e::real
1.1417 +    assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
1.1418 +    then have "\<exists>d>0. ball x d \<subseteq> S"
1.1419 +      unfolding subset_eq
1.1420 +      apply (rule_tac x="e/2" in exI, auto)
1.1421 +      done
1.1422 +  }
1.1423 +  ultimately show ?thesis
1.1424 +    unfolding open_contains_ball by auto
1.1425 +qed
1.1427 +lemma open_contains_cball_eq: "open S \<Longrightarrow> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
1.1428 +  by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)
1.1430 +lemma eventually_nhds_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>x. x \<in> ball z d) (nhds z)"
1.1431 +  by (rule eventually_nhds_in_open) simp_all
1.1433 +lemma eventually_at_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<in> A) (at z within A)"
1.1434 +  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
1.1436 +lemma eventually_at_ball': "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<noteq> z \<and> t \<in> A) (at z within A)"
1.1437 +  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
1.1439 +lemma at_within_ball: "e > 0 \<Longrightarrow> dist x y < e \<Longrightarrow> at y within ball x e = at y"
1.1440 +  by (subst at_within_open) auto
1.1442 +lemma atLeastAtMost_eq_cball:
1.1443 +  fixes a b::real
1.1444 +  shows "{a .. b} = cball ((a + b)/2) ((b - a)/2)"
1.1445 +  by (auto simp: dist_real_def field_simps mem_cball)
1.1447 +lemma greaterThanLessThan_eq_ball:
1.1448 +  fixes a b::real
1.1449 +  shows "{a <..< b} = ball ((a + b)/2) ((b - a)/2)"
1.1450 +  by (auto simp: dist_real_def field_simps mem_ball)
1.1453 +subsection \<open>Limit points\<close>
1.1455 +definition%important (in topological_space) islimpt:: "'a \<Rightarrow> 'a set \<Rightarrow> bool"  (infixr "islimpt" 60)
1.1456 +  where "x islimpt S \<longleftrightarrow> (\<forall>T. x\<in>T \<longrightarrow> open T \<longrightarrow> (\<exists>y\<in>S. y\<in>T \<and> y\<noteq>x))"
1.1458 +lemma islimptI:
1.1459 +  assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
1.1460 +  shows "x islimpt S"
1.1461 +  using assms unfolding islimpt_def by auto
1.1463 +lemma islimptE:
1.1464 +  assumes "x islimpt S" and "x \<in> T" and "open T"
1.1465 +  obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
1.1466 +  using assms unfolding islimpt_def by auto
1.1468 +lemma islimpt_iff_eventually: "x islimpt S \<longleftrightarrow> \<not> eventually (\<lambda>y. y \<notin> S) (at x)"
1.1469 +  unfolding islimpt_def eventually_at_topological by auto
1.1471 +lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> x islimpt T"
1.1472 +  unfolding islimpt_def by fast
1.1474 +lemma islimpt_approachable:
1.1475 +  fixes x :: "'a::metric_space"
1.1476 +  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
1.1477 +  unfolding islimpt_iff_eventually eventually_at by fast
1.1479 +lemma islimpt_approachable_le: "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x \<le> e)"
1.1480 +  for x :: "'a::metric_space"
1.1481 +  unfolding islimpt_approachable
1.1482 +  using approachable_lt_le [where f="\<lambda>y. dist y x" and P="\<lambda>y. y \<notin> S \<or> y = x",
1.1483 +    THEN arg_cong [where f=Not]]
1.1484 +  by (simp add: Bex_def conj_commute conj_left_commute)
1.1486 +lemma islimpt_UNIV_iff: "x islimpt UNIV \<longleftrightarrow> \<not> open {x}"
1.1487 +  unfolding islimpt_def by (safe, fast, case_tac "T = {x}", fast, fast)
1.1489 +lemma islimpt_punctured: "x islimpt S = x islimpt (S-{x})"
1.1490 +  unfolding islimpt_def by blast
1.1492 +text \<open>A perfect space has no isolated points.\<close>
1.1494 +lemma islimpt_UNIV [simp, intro]: "x islimpt UNIV"
1.1495 +  for x :: "'a::perfect_space"
1.1496 +  unfolding islimpt_UNIV_iff by (rule not_open_singleton)
1.1498 +lemma perfect_choose_dist: "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
1.1499 +  for x :: "'a::{perfect_space,metric_space}"
1.1500 +  using islimpt_UNIV [of x] by (simp add: islimpt_approachable)
1.1502 +lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
1.1503 +  unfolding closed_def
1.1504 +  apply (subst open_subopen)
1.1505 +  apply (simp add: islimpt_def subset_eq)
1.1506 +  apply (metis ComplE ComplI)
1.1507 +  done
1.1509 +lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
1.1510 +  by (auto simp: islimpt_def)
1.1512 +lemma finite_ball_include:
1.1513 +  fixes a :: "'a::metric_space"
1.1514 +  assumes "finite S"
1.1515 +  shows "\<exists>e>0. S \<subseteq> ball a e"
1.1516 +  using assms
1.1517 +proof induction
1.1518 +  case (insert x S)
1.1519 +  then obtain e0 where "e0>0" and e0:"S \<subseteq> ball a e0" by auto
1.1520 +  define e where "e = max e0 (2 * dist a x)"
1.1521 +  have "e>0" unfolding e_def using \<open>e0>0\<close> by auto
1.1522 +  moreover have "insert x S \<subseteq> ball a e"
1.1523 +    using e0 \<open>e>0\<close> unfolding e_def by auto
1.1524 +  ultimately show ?case by auto
1.1525 +qed (auto intro: zero_less_one)
1.1527 +lemma finite_set_avoid:
1.1528 +  fixes a :: "'a::metric_space"
1.1529 +  assumes "finite S"
1.1530 +  shows "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
1.1531 +  using assms
1.1532 +proof induction
1.1533 +  case (insert x S)
1.1534 +  then obtain d where "d > 0" and d: "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
1.1535 +    by blast
1.1536 +  show ?case
1.1537 +  proof (cases "x = a")
1.1538 +    case True
1.1539 +    with \<open>d > 0 \<close>d show ?thesis by auto
1.1540 +  next
1.1541 +    case False
1.1542 +    let ?d = "min d (dist a x)"
1.1543 +    from False \<open>d > 0\<close> have dp: "?d > 0"
1.1544 +      by auto
1.1545 +    from d have d': "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> ?d \<le> dist a x"
1.1546 +      by auto
1.1547 +    with dp False show ?thesis
1.1548 +      by (metis insert_iff le_less min_less_iff_conj not_less)
1.1549 +  qed
1.1550 +qed (auto intro: zero_less_one)
1.1552 +lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
1.1553 +  by (simp add: islimpt_iff_eventually eventually_conj_iff)
1.1555 +lemma discrete_imp_closed:
1.1556 +  fixes S :: "'a::metric_space set"
1.1557 +  assumes e: "0 < e"
1.1558 +    and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
1.1559 +  shows "closed S"
1.1560 +proof -
1.1561 +  have False if C: "\<And>e. e>0 \<Longrightarrow> \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" for x
1.1562 +  proof -
1.1563 +    from e have e2: "e/2 > 0" by arith
1.1564 +    from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y \<noteq> x" "dist y x < e/2"
1.1565 +      by blast
1.1566 +    let ?m = "min (e/2) (dist x y) "
1.1567 +    from e2 y(2) have mp: "?m > 0"
1.1568 +      by simp
1.1569 +    from C[OF mp] obtain z where z: "z \<in> S" "z \<noteq> x" "dist z x < ?m"
1.1570 +      by blast
1.1571 +    from z y have "dist z y < e"
1.1572 +      by (intro dist_triangle_lt [where z=x]) simp
1.1573 +    from d[rule_format, OF y(1) z(1) this] y z show ?thesis
1.1574 +      by (auto simp: dist_commute)
1.1575 +  qed
1.1576 +  then show ?thesis
1.1577 +    by (metis islimpt_approachable closed_limpt [where 'a='a])
1.1578 +qed
1.1580 +lemma closed_of_nat_image: "closed (of_nat ` A :: 'a::real_normed_algebra_1 set)"
1.1581 +  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_nat)
1.1583 +lemma closed_of_int_image: "closed (of_int ` A :: 'a::real_normed_algebra_1 set)"
1.1584 +  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_int)
1.1586 +lemma closed_Nats [simp]: "closed (\<nat> :: 'a :: real_normed_algebra_1 set)"
1.1587 +  unfolding Nats_def by (rule closed_of_nat_image)
1.1589 +lemma closed_Ints [simp]: "closed (\<int> :: 'a :: real_normed_algebra_1 set)"
1.1590 +  unfolding Ints_def by (rule closed_of_int_image)
1.1592 +lemma closed_subset_Ints:
1.1593 +  fixes A :: "'a :: real_normed_algebra_1 set"
1.1594 +  assumes "A \<subseteq> \<int>"
1.1595 +  shows   "closed A"
1.1596 +proof (intro discrete_imp_closed[OF zero_less_one] ballI impI, goal_cases)
1.1597 +  case (1 x y)
1.1598 +  with assms have "x \<in> \<int>" and "y \<in> \<int>" by auto
1.1599 +  with \<open>dist y x < 1\<close> show "y = x"
1.1600 +    by (auto elim!: Ints_cases simp: dist_of_int)
1.1601 +qed
1.1604 +subsection \<open>Interior of a Set\<close>
1.1606 +definition%important "interior S = \<Union>{T. open T \<and> T \<subseteq> S}"
1.1608 +lemma interiorI [intro?]:
1.1609 +  assumes "open T" and "x \<in> T" and "T \<subseteq> S"
1.1610 +  shows "x \<in> interior S"
1.1611 +  using assms unfolding interior_def by fast
1.1613 +lemma interiorE [elim?]:
1.1614 +  assumes "x \<in> interior S"
1.1615 +  obtains T where "open T" and "x \<in> T" and "T \<subseteq> S"
1.1616 +  using assms unfolding interior_def by fast
1.1618 +lemma open_interior [simp, intro]: "open (interior S)"
1.1619 +  by (simp add: interior_def open_Union)
1.1621 +lemma interior_subset: "interior S \<subseteq> S"
1.1622 +  by (auto simp: interior_def)
1.1624 +lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> interior S"
1.1625 +  by (auto simp: interior_def)
1.1627 +lemma interior_open: "open S \<Longrightarrow> interior S = S"
1.1628 +  by (intro equalityI interior_subset interior_maximal subset_refl)
1.1630 +lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
1.1631 +  by (metis open_interior interior_open)
1.1633 +lemma open_subset_interior: "open S \<Longrightarrow> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
1.1634 +  by (metis interior_maximal interior_subset subset_trans)
1.1636 +lemma interior_empty [simp]: "interior {} = {}"
1.1637 +  using open_empty by (rule interior_open)
1.1639 +lemma interior_UNIV [simp]: "interior UNIV = UNIV"
1.1640 +  using open_UNIV by (rule interior_open)
1.1642 +lemma interior_interior [simp]: "interior (interior S) = interior S"
1.1643 +  using open_interior by (rule interior_open)
1.1645 +lemma interior_mono: "S \<subseteq> T \<Longrightarrow> interior S \<subseteq> interior T"
1.1646 +  by (auto simp: interior_def)
1.1648 +lemma interior_unique:
1.1649 +  assumes "T \<subseteq> S" and "open T"
1.1650 +  assumes "\<And>T'. T' \<subseteq> S \<Longrightarrow> open T' \<Longrightarrow> T' \<subseteq> T"
1.1651 +  shows "interior S = T"
1.1652 +  by (intro equalityI assms interior_subset open_interior interior_maximal)
1.1654 +lemma interior_singleton [simp]: "interior {a} = {}"
1.1655 +  for a :: "'a::perfect_space"
1.1656 +  apply (rule interior_unique, simp_all)
1.1657 +  using not_open_singleton subset_singletonD
1.1658 +  apply fastforce
1.1659 +  done
1.1661 +lemma interior_Int [simp]: "interior (S \<inter> T) = interior S \<inter> interior T"
1.1662 +  by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1
1.1663 +    Int_lower2 interior_maximal interior_subset open_Int open_interior)
1.1665 +lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
1.1666 +  using open_contains_ball_eq [where S="interior S"]
1.1667 +  by (simp add: open_subset_interior)
1.1669 +lemma eventually_nhds_in_nhd: "x \<in> interior s \<Longrightarrow> eventually (\<lambda>y. y \<in> s) (nhds x)"
1.1670 +  using interior_subset[of s] by (subst eventually_nhds) blast
1.1672 +lemma interior_limit_point [intro]:
1.1673 +  fixes x :: "'a::perfect_space"
1.1674 +  assumes x: "x \<in> interior S"
1.1675 +  shows "x islimpt S"
1.1676 +  using x islimpt_UNIV [of x]
1.1677 +  unfolding interior_def islimpt_def
1.1678 +  apply (clarsimp, rename_tac T T')
1.1679 +  apply (drule_tac x="T \<inter> T'" in spec)
1.1680 +  apply (auto simp: open_Int)
1.1681 +  done
1.1683 +lemma interior_closed_Un_empty_interior:
1.1684 +  assumes cS: "closed S"
1.1685 +    and iT: "interior T = {}"
1.1686 +  shows "interior (S \<union> T) = interior S"
1.1687 +proof
1.1688 +  show "interior S \<subseteq> interior (S \<union> T)"
1.1689 +    by (rule interior_mono) (rule Un_upper1)
1.1690 +  show "interior (S \<union> T) \<subseteq> interior S"
1.1691 +  proof
1.1692 +    fix x
1.1693 +    assume "x \<in> interior (S \<union> T)"
1.1694 +    then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T" ..
1.1695 +    show "x \<in> interior S"
1.1696 +    proof (rule ccontr)
1.1697 +      assume "x \<notin> interior S"
1.1698 +      with \<open>x \<in> R\<close> \<open>open R\<close> obtain y where "y \<in> R - S"
1.1699 +        unfolding interior_def by fast
1.1700 +      from \<open>open R\<close> \<open>closed S\<close> have "open (R - S)"
1.1701 +        by (rule open_Diff)
1.1702 +      from \<open>R \<subseteq> S \<union> T\<close> have "R - S \<subseteq> T"
1.1703 +        by fast
1.1704 +      from \<open>y \<in> R - S\<close> \<open>open (R - S)\<close> \<open>R - S \<subseteq> T\<close> \<open>interior T = {}\<close> show False
1.1705 +        unfolding interior_def by fast
1.1706 +    qed
1.1707 +  qed
1.1708 +qed
1.1710 +lemma interior_Times: "interior (A \<times> B) = interior A \<times> interior B"
1.1711 +proof (rule interior_unique)
1.1712 +  show "interior A \<times> interior B \<subseteq> A \<times> B"
1.1713 +    by (intro Sigma_mono interior_subset)
1.1714 +  show "open (interior A \<times> interior B)"
1.1715 +    by (intro open_Times open_interior)
1.1716 +  fix T
1.1717 +  assume "T \<subseteq> A \<times> B" and "open T"
1.1718 +  then show "T \<subseteq> interior A \<times> interior B"
1.1719 +  proof safe
1.1720 +    fix x y
1.1721 +    assume "(x, y) \<in> T"
1.1722 +    then obtain C D where "open C" "open D" "C \<times> D \<subseteq> T" "x \<in> C" "y \<in> D"
1.1723 +      using \<open>open T\<close> unfolding open_prod_def by fast
1.1724 +    then have "open C" "open D" "C \<subseteq> A" "D \<subseteq> B" "x \<in> C" "y \<in> D"
1.1725 +      using \<open>T \<subseteq> A \<times> B\<close> by auto
1.1726 +    then show "x \<in> interior A" and "y \<in> interior B"
1.1727 +      by (auto intro: interiorI)
1.1728 +  qed
1.1729 +qed
1.1731 +lemma interior_Ici:
1.1732 +  fixes x :: "'a :: {dense_linorder,linorder_topology}"
1.1733 +  assumes "b < x"
1.1734 +  shows "interior {x ..} = {x <..}"
1.1735 +proof (rule interior_unique)
1.1736 +  fix T
1.1737 +  assume "T \<subseteq> {x ..}" "open T"
1.1738 +  moreover have "x \<notin> T"
1.1739 +  proof
1.1740 +    assume "x \<in> T"
1.1741 +    obtain y where "y < x" "{y <.. x} \<subseteq> T"
1.1742 +      using open_left[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>b < x\<close>] by auto
1.1743 +    with dense[OF \<open>y < x\<close>] obtain z where "z \<in> T" "z < x"
1.1744 +      by (auto simp: subset_eq Ball_def)
1.1745 +    with \<open>T \<subseteq> {x ..}\<close> show False by auto
1.1746 +  qed
1.1747 +  ultimately show "T \<subseteq> {x <..}"
1.1748 +    by (auto simp: subset_eq less_le)
1.1749 +qed auto
1.1751 +lemma interior_Iic:
1.1752 +  fixes x :: "'a ::{dense_linorder,linorder_topology}"
1.1753 +  assumes "x < b"
1.1754 +  shows "interior {.. x} = {..< x}"
1.1755 +proof (rule interior_unique)
1.1756 +  fix T
1.1757 +  assume "T \<subseteq> {.. x}" "open T"
1.1758 +  moreover have "x \<notin> T"
1.1759 +  proof
1.1760 +    assume "x \<in> T"
1.1761 +    obtain y where "x < y" "{x ..< y} \<subseteq> T"
1.1762 +      using open_right[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>x < b\<close>] by auto
1.1763 +    with dense[OF \<open>x < y\<close>] obtain z where "z \<in> T" "x < z"
1.1764 +      by (auto simp: subset_eq Ball_def less_le)
1.1765 +    with \<open>T \<subseteq> {.. x}\<close> show False by auto
1.1766 +  qed
1.1767 +  ultimately show "T \<subseteq> {..< x}"
1.1768 +    by (auto simp: subset_eq less_le)
1.1769 +qed auto
1.1772 +subsection \<open>Closure of a Set\<close>
1.1774 +definition%important "closure S = S \<union> {x | x. x islimpt S}"
1.1776 +lemma interior_closure: "interior S = - (closure (- S))"
1.1777 +  by (auto simp: interior_def closure_def islimpt_def)
1.1779 +lemma closure_interior: "closure S = - interior (- S)"
1.1780 +  by (simp add: interior_closure)
1.1782 +lemma closed_closure[simp, intro]: "closed (closure S)"
1.1783 +  by (simp add: closure_interior closed_Compl)
1.1785 +lemma closure_subset: "S \<subseteq> closure S"
1.1786 +  by (simp add: closure_def)
1.1788 +lemma closure_hull: "closure S = closed hull S"
1.1789 +  by (auto simp: hull_def closure_interior interior_def)
1.1791 +lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
1.1792 +  unfolding closure_hull using closed_Inter by (rule hull_eq)
1.1794 +lemma closure_closed [simp]: "closed S \<Longrightarrow> closure S = S"
1.1795 +  by (simp only: closure_eq)
1.1797 +lemma closure_closure [simp]: "closure (closure S) = closure S"
1.1798 +  unfolding closure_hull by (rule hull_hull)
1.1800 +lemma closure_mono: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
1.1801 +  unfolding closure_hull by (rule hull_mono)
1.1803 +lemma closure_minimal: "S \<subseteq> T \<Longrightarrow> closed T \<Longrightarrow> closure S \<subseteq> T"
1.1804 +  unfolding closure_hull by (rule hull_minimal)
1.1806 +lemma closure_unique:
1.1807 +  assumes "S \<subseteq> T"
1.1808 +    and "closed T"
1.1809 +    and "\<And>T'. S \<subseteq> T' \<Longrightarrow> closed T' \<Longrightarrow> T \<subseteq> T'"
1.1810 +  shows "closure S = T"
1.1811 +  using assms unfolding closure_hull by (rule hull_unique)
1.1813 +lemma closure_empty [simp]: "closure {} = {}"
1.1814 +  using closed_empty by (rule closure_closed)
1.1816 +lemma closure_UNIV [simp]: "closure UNIV = UNIV"
1.1817 +  using closed_UNIV by (rule closure_closed)
1.1819 +lemma closure_Un [simp]: "closure (S \<union> T) = closure S \<union> closure T"
1.1820 +  by (simp add: closure_interior)
1.1822 +lemma closure_eq_empty [iff]: "closure S = {} \<longleftrightarrow> S = {}"
1.1823 +  using closure_empty closure_subset[of S] by blast
1.1825 +lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
1.1826 +  using closure_eq[of S] closure_subset[of S] by simp
1.1828 +lemma open_Int_closure_eq_empty: "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
1.1829 +  using open_subset_interior[of S "- T"]
1.1830 +  using interior_subset[of "- T"]
1.1831 +  by (auto simp: closure_interior)
1.1833 +lemma open_Int_closure_subset: "open S \<Longrightarrow> S \<inter> closure T \<subseteq> closure (S \<inter> T)"
1.1834 +proof
1.1835 +  fix x
1.1836 +  assume *: "open S" "x \<in> S \<inter> closure T"
1.1837 +  have "x islimpt (S \<inter> T)" if **: "x islimpt T"
1.1838 +  proof (rule islimptI)
1.1839 +    fix A
1.1840 +    assume "x \<in> A" "open A"
1.1841 +    with * have "x \<in> A \<inter> S" "open (A \<inter> S)"
1.1842 +      by (simp_all add: open_Int)
1.1843 +    with ** obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
1.1844 +      by (rule islimptE)
1.1845 +    then have "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
1.1846 +      by simp_all
1.1847 +    then show "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
1.1848 +  qed
1.1849 +  with * show "x \<in> closure (S \<inter> T)"
1.1850 +    unfolding closure_def by blast
1.1851 +qed
1.1853 +lemma closure_complement: "closure (- S) = - interior S"
1.1854 +  by (simp add: closure_interior)
1.1856 +lemma interior_complement: "interior (- S) = - closure S"
1.1857 +  by (simp add: closure_interior)
1.1859 +lemma interior_diff: "interior(S - T) = interior S - closure T"
1.1860 +  by (simp add: Diff_eq interior_complement)
1.1862 +lemma closure_Times: "closure (A \<times> B) = closure A \<times> closure B"
1.1863 +proof (rule closure_unique)
1.1864 +  show "A \<times> B \<subseteq> closure A \<times> closure B"
1.1865 +    by (intro Sigma_mono closure_subset)
1.1866 +  show "closed (closure A \<times> closure B)"
1.1867 +    by (intro closed_Times closed_closure)
1.1868 +  fix T
1.1869 +  assume "A \<times> B \<subseteq> T" and "closed T"
1.1870 +  then show "closure A \<times> closure B \<subseteq> T"
1.1871 +    apply (simp add: closed_def open_prod_def, clarify)
1.1872 +    apply (rule ccontr)
1.1873 +    apply (drule_tac x="(a, b)" in bspec, simp, clarify, rename_tac C D)
1.1874 +    apply (simp add: closure_interior interior_def)
1.1875 +    apply (drule_tac x=C in spec)
1.1876 +    apply (drule_tac x=D in spec, auto)
1.1877 +    done
1.1878 +qed
1.1880 +lemma closure_openin_Int_closure:
1.1881 +  assumes ope: "openin (subtopology euclidean U) S" and "T \<subseteq> U"
1.1882 +  shows "closure(S \<inter> closure T) = closure(S \<inter> T)"
1.1883 +proof
1.1884 +  obtain V where "open V" and S: "S = U \<inter> V"
1.1885 +    using ope using openin_open by metis
1.1886 +  show "closure (S \<inter> closure T) \<subseteq> closure (S \<inter> T)"
1.1887 +    proof (clarsimp simp: S)
1.1888 +      fix x
1.1889 +      assume  "x \<in> closure (U \<inter> V \<inter> closure T)"
1.1890 +      then have "V \<inter> closure T \<subseteq> A \<Longrightarrow> x \<in> closure A" for A
1.1891 +          by (metis closure_mono subsetD inf.coboundedI2 inf_assoc)
1.1892 +      then have "x \<in> closure (T \<inter> V)"
1.1893 +         by (metis \<open>open V\<close> closure_closure inf_commute open_Int_closure_subset)
1.1894 +      then show "x \<in> closure (U \<inter> V \<inter> T)"
1.1895 +        by (metis \<open>T \<subseteq> U\<close> inf.absorb_iff2 inf_assoc inf_commute)
1.1896 +    qed
1.1897 +next
1.1898 +  show "closure (S \<inter> T) \<subseteq> closure (S \<inter> closure T)"
1.1899 +    by (meson Int_mono closure_mono closure_subset order_refl)
1.1900 +qed
1.1902 +lemma islimpt_in_closure: "(x islimpt S) = (x\<in>closure(S-{x}))"
1.1903 +  unfolding closure_def using islimpt_punctured by blast
1.1905 +lemma connected_imp_connected_closure: "connected S \<Longrightarrow> connected (closure S)"
1.1906 +  by (rule connectedI) (meson closure_subset open_Int open_Int_closure_eq_empty subset_trans connectedD)
1.1908 +lemma limpt_of_limpts: "x islimpt {y. y islimpt S} \<Longrightarrow> x islimpt S"
1.1909 +  for x :: "'a::metric_space"
1.1910 +  apply (clarsimp simp add: islimpt_approachable)
1.1911 +  apply (drule_tac x="e/2" in spec)
1.1912 +  apply (auto simp: simp del: less_divide_eq_numeral1)
1.1913 +  apply (drule_tac x="dist x' x" in spec)
1.1914 +  apply (auto simp: zero_less_dist_iff simp del: less_divide_eq_numeral1)
1.1915 +  apply (erule rev_bexI)
1.1916 +  apply (metis dist_commute dist_triangle_half_r less_trans less_irrefl)
1.1917 +  done
1.1919 +lemma closed_limpts:  "closed {x::'a::metric_space. x islimpt S}"
1.1920 +  using closed_limpt limpt_of_limpts by blast
1.1922 +lemma limpt_of_closure: "x islimpt closure S \<longleftrightarrow> x islimpt S"
1.1923 +  for x :: "'a::metric_space"
1.1924 +  by (auto simp: closure_def islimpt_Un dest: limpt_of_limpts)
1.1926 +lemma closedin_limpt:
1.1927 +  "closedin (subtopology euclidean T) S \<longleftrightarrow> S \<subseteq> T \<and> (\<forall>x. x islimpt S \<and> x \<in> T \<longrightarrow> x \<in> S)"
1.1928 +  apply (simp add: closedin_closed, safe)
1.1929 +   apply (simp add: closed_limpt islimpt_subset)
1.1930 +  apply (rule_tac x="closure S" in exI, simp)
1.1931 +  apply (force simp: closure_def)
1.1932 +  done
1.1934 +lemma closedin_closed_eq: "closed S \<Longrightarrow> closedin (subtopology euclidean S) T \<longleftrightarrow> closed T \<and> T \<subseteq> S"
1.1935 +  by (meson closedin_limpt closed_subset closedin_closed_trans)
1.1937 +lemma connected_closed_set:
1.1938 +   "closed S
1.1939 +    \<Longrightarrow> connected S \<longleftrightarrow> (\<nexists>A B. closed A \<and> closed B \<and> A \<noteq> {} \<and> B \<noteq> {} \<and> A \<union> B = S \<and> A \<inter> B = {})"
1.1940 +  unfolding connected_closedin_eq closedin_closed_eq connected_closedin_eq by blast
1.1942 +text \<open>If a connnected set is written as the union of two nonempty closed sets, then these sets
1.1943 +have to intersect.\<close>
1.1945 +lemma connected_as_closed_union:
1.1946 +  assumes "connected C" "C = A \<union> B" "closed A" "closed B" "A \<noteq> {}" "B \<noteq> {}"
1.1947 +  shows "A \<inter> B \<noteq> {}"
1.1948 +by (metis assms closed_Un connected_closed_set)
1.1950 +lemma closedin_subset_trans:
1.1951 +  "closedin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
1.1952 +    closedin (subtopology euclidean T) S"
1.1953 +  by (meson closedin_limpt subset_iff)
1.1955 +lemma openin_subset_trans:
1.1956 +  "openin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
1.1957 +    openin (subtopology euclidean T) S"
1.1958 +  by (auto simp: openin_open)
1.1960 +lemma openin_Times:
1.1961 +  "openin (subtopology euclidean S) S' \<Longrightarrow> openin (subtopology euclidean T) T' \<Longrightarrow>
1.1962 +    openin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
1.1963 +  unfolding openin_open using open_Times by blast
1.1965 +lemma Times_in_interior_subtopology:
1.1966 +  fixes U :: "('a::metric_space \<times> 'b::metric_space) set"
1.1967 +  assumes "(x, y) \<in> U" "openin (subtopology euclidean (S \<times> T)) U"
1.1968 +  obtains V W where "openin (subtopology euclidean S) V" "x \<in> V"
1.1969 +                    "openin (subtopology euclidean T) W" "y \<in> W" "(V \<times> W) \<subseteq> U"
1.1970 +proof -
1.1971 +  from assms obtain e where "e > 0" and "U \<subseteq> S \<times> T"
1.1972 +    and e: "\<And>x' y'. \<lbrakk>x'\<in>S; y'\<in>T; dist (x', y') (x, y) < e\<rbrakk> \<Longrightarrow> (x', y') \<in> U"
1.1973 +    by (force simp: openin_euclidean_subtopology_iff)
1.1974 +  with assms have "x \<in> S" "y \<in> T"
1.1975 +    by auto
1.1976 +  show ?thesis
1.1977 +  proof
1.1978 +    show "openin (subtopology euclidean S) (ball x (e/2) \<inter> S)"
1.1979 +      by (simp add: Int_commute openin_open_Int)
1.1980 +    show "x \<in> ball x (e / 2) \<inter> S"
1.1981 +      by (simp add: \<open>0 < e\<close> \<open>x \<in> S\<close>)
1.1982 +    show "openin (subtopology euclidean T) (ball y (e/2) \<inter> T)"
1.1983 +      by (simp add: Int_commute openin_open_Int)
1.1984 +    show "y \<in> ball y (e / 2) \<inter> T"
1.1985 +      by (simp add: \<open>0 < e\<close> \<open>y \<in> T\<close>)
1.1986 +    show "(ball x (e / 2) \<inter> S) \<times> (ball y (e / 2) \<inter> T) \<subseteq> U"
1.1987 +      by clarify (simp add: e dist_Pair_Pair \<open>0 < e\<close> dist_commute sqrt_sum_squares_half_less)
1.1988 +  qed
1.1989 +qed
1.1991 +lemma openin_Times_eq:
1.1992 +  fixes S :: "'a::metric_space set" and T :: "'b::metric_space set"
1.1993 +  shows
1.1994 +    "openin (subtopology euclidean (S \<times> T)) (S' \<times> T') \<longleftrightarrow>
1.1995 +      S' = {} \<or> T' = {} \<or> openin (subtopology euclidean S) S' \<and> openin (subtopology euclidean T) T'"
1.1996 +    (is "?lhs = ?rhs")
1.1997 +proof (cases "S' = {} \<or> T' = {}")
1.1998 +  case True
1.1999 +  then show ?thesis by auto
1.2000 +next
1.2001 +  case False
1.2002 +  then obtain x y where "x \<in> S'" "y \<in> T'"
1.2003 +    by blast
1.2004 +  show ?thesis
1.2005 +  proof
1.2006 +    assume ?lhs
1.2007 +    have "openin (subtopology euclidean S) S'"
1.2008 +      apply (subst openin_subopen, clarify)
1.2009 +      apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
1.2010 +      using \<open>y \<in> T'\<close>
1.2011 +       apply auto
1.2012 +      done
1.2013 +    moreover have "openin (subtopology euclidean T) T'"
1.2014 +      apply (subst openin_subopen, clarify)
1.2015 +      apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
1.2016 +      using \<open>x \<in> S'\<close>
1.2017 +       apply auto
1.2018 +      done
1.2019 +    ultimately show ?rhs
1.2020 +      by simp
1.2021 +  next
1.2022 +    assume ?rhs
1.2023 +    with False show ?lhs
1.2024 +      by (simp add: openin_Times)
1.2025 +  qed
1.2026 +qed
1.2028 +lemma closedin_Times:
1.2029 +  "closedin (subtopology euclidean S) S' \<Longrightarrow> closedin (subtopology euclidean T) T' \<Longrightarrow>
1.2030 +    closedin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
1.2031 +  unfolding closedin_closed using closed_Times by blast
1.2033 +lemma bdd_below_closure:
1.2034 +  fixes A :: "real set"
1.2035 +  assumes "bdd_below A"
1.2036 +  shows "bdd_below (closure A)"
1.2037 +proof -
1.2038 +  from assms obtain m where "\<And>x. x \<in> A \<Longrightarrow> m \<le> x"
1.2039 +    by (auto simp: bdd_below_def)
1.2040 +  then have "A \<subseteq> {m..}" by auto
1.2041 +  then have "closure A \<subseteq> {m..}"
1.2042 +    using closed_real_atLeast by (rule closure_minimal)
1.2043 +  then show ?thesis
1.2044 +    by (auto simp: bdd_below_def)
1.2045 +qed
1.2048 +subsection \<open>Frontier (also known as boundary)\<close>
1.2050 +definition%important "frontier S = closure S - interior S"
1.2052 +lemma frontier_closed [iff]: "closed (frontier S)"
1.2053 +  by (simp add: frontier_def closed_Diff)
1.2055 +lemma frontier_closures: "frontier S = closure S \<inter> closure (- S)"
1.2056 +  by (auto simp: frontier_def interior_closure)
1.2058 +lemma frontier_Int: "frontier(S \<inter> T) = closure(S \<inter> T) \<inter> (frontier S \<union> frontier T)"
1.2059 +proof -
1.2060 +  have "closure (S \<inter> T) \<subseteq> closure S" "closure (S \<inter> T) \<subseteq> closure T"
1.2061 +    by (simp_all add: closure_mono)
1.2062 +  then show ?thesis
1.2063 +    by (auto simp: frontier_closures)
1.2064 +qed
1.2066 +lemma frontier_Int_subset: "frontier(S \<inter> T) \<subseteq> frontier S \<union> frontier T"
1.2067 +  by (auto simp: frontier_Int)
1.2069 +lemma frontier_Int_closed:
1.2070 +  assumes "closed S" "closed T"
1.2071 +  shows "frontier(S \<inter> T) = (frontier S \<inter> T) \<union> (S \<inter> frontier T)"
1.2072 +proof -
1.2073 +  have "closure (S \<inter> T) = T \<inter> S"
1.2074 +    using assms by (simp add: Int_commute closed_Int)
1.2075 +  moreover have "T \<inter> (closure S \<inter> closure (- S)) = frontier S \<inter> T"
1.2076 +    by (simp add: Int_commute frontier_closures)
1.2077 +  ultimately show ?thesis
1.2078 +    by (simp add: Int_Un_distrib Int_assoc Int_left_commute assms frontier_closures)
1.2079 +qed
1.2082 +  fixes a :: "'a::metric_space"
1.2083 +  shows "a \<in> frontier S \<longleftrightarrow> (\<forall>e>0. (\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e))"
1.2084 +  unfolding frontier_def closure_interior
1.2085 +  by (auto simp: mem_interior subset_eq ball_def)
1.2087 +lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
1.2088 +  by (metis frontier_def closure_closed Diff_subset)
1.2090 +lemma frontier_empty [simp]: "frontier {} = {}"
1.2091 +  by (simp add: frontier_def)
1.2093 +lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
1.2094 +proof -
1.2095 +  {
1.2096 +    assume "frontier S \<subseteq> S"
1.2097 +    then have "closure S \<subseteq> S"
1.2098 +      using interior_subset unfolding frontier_def by auto
1.2099 +    then have "closed S"
1.2100 +      using closure_subset_eq by auto
1.2101 +  }
1.2102 +  then show ?thesis using frontier_subset_closed[of S] ..
1.2103 +qed
1.2105 +lemma frontier_complement [simp]: "frontier (- S) = frontier S"
1.2106 +  by (auto simp: frontier_def closure_complement interior_complement)
1.2108 +lemma frontier_Un_subset: "frontier(S \<union> T) \<subseteq> frontier S \<union> frontier T"
1.2109 +  by (metis compl_sup frontier_Int_subset frontier_complement)
1.2111 +lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
1.2112 +  using frontier_complement frontier_subset_eq[of "- S"]
1.2113 +  unfolding open_closed by auto
1.2115 +lemma frontier_UNIV [simp]: "frontier UNIV = {}"
1.2116 +  using frontier_complement frontier_empty by fastforce
1.2118 +lemma frontier_interiors: "frontier s = - interior(s) - interior(-s)"
1.2119 +  by (simp add: Int_commute frontier_def interior_closure)
1.2121 +lemma frontier_interior_subset: "frontier(interior S) \<subseteq> frontier S"
1.2122 +  by (simp add: Diff_mono frontier_interiors interior_mono interior_subset)
1.2124 +lemma connected_Int_frontier:
1.2125 +     "\<lbrakk>connected s; s \<inter> t \<noteq> {}; s - t \<noteq> {}\<rbrakk> \<Longrightarrow> (s \<inter> frontier t \<noteq> {})"
1.2126 +  apply (simp add: frontier_interiors connected_openin, safe)
1.2127 +  apply (drule_tac x="s \<inter> interior t" in spec, safe)
1.2128 +   apply (drule_tac [2] x="s \<inter> interior (-t)" in spec)
1.2129 +   apply (auto simp: disjoint_eq_subset_Compl dest: interior_subset [THEN subsetD])
1.2130 +  done
1.2132 +lemma closure_Un_frontier: "closure S = S \<union> frontier S"
1.2133 +proof -
1.2134 +  have "S \<union> interior S = S"
1.2135 +    using interior_subset by auto
1.2136 +  then show ?thesis
1.2137 +    using closure_subset by (auto simp: frontier_def)
1.2138 +qed
1.2141 +subsection%unimportant \<open>Filters and the ``eventually true'' quantifier\<close>
1.2143 +definition indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a filter"  (infixr "indirection" 70)
1.2144 +  where "a indirection v = at a within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
1.2146 +text \<open>Identify Trivial limits, where we can't approach arbitrarily closely.\<close>
1.2148 +lemma trivial_limit_within: "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
1.2149 +proof
1.2150 +  assume "trivial_limit (at a within S)"
1.2151 +  then show "\<not> a islimpt S"
1.2152 +    unfolding trivial_limit_def
1.2153 +    unfolding eventually_at_topological
1.2154 +    unfolding islimpt_def
1.2155 +    apply (clarsimp simp add: set_eq_iff)
1.2156 +    apply (rename_tac T, rule_tac x=T in exI)
1.2157 +    apply (clarsimp, drule_tac x=y in bspec, simp_all)
1.2158 +    done
1.2159 +next
1.2160 +  assume "\<not> a islimpt S"
1.2161 +  then show "trivial_limit (at a within S)"
1.2162 +    unfolding trivial_limit_def eventually_at_topological islimpt_def
1.2163 +    by metis
1.2164 +qed
1.2166 +lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
1.2167 +  using trivial_limit_within [of a UNIV] by simp
1.2169 +lemma trivial_limit_at: "\<not> trivial_limit (at a)"
1.2170 +  for a :: "'a::perfect_space"
1.2171 +  by (rule at_neq_bot)
1.2173 +lemma trivial_limit_at_infinity:
1.2174 +  "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,perfect_space}) filter)"
1.2175 +  unfolding trivial_limit_def eventually_at_infinity
1.2176 +  apply clarsimp
1.2177 +  apply (subgoal_tac "\<exists>x::'a. x \<noteq> 0", clarify)
1.2178 +   apply (rule_tac x="scaleR (b / norm x) x" in exI, simp)
1.2179 +  apply (cut_tac islimpt_UNIV [of "0::'a", unfolded islimpt_def])
1.2180 +  apply (drule_tac x=UNIV in spec, simp)
1.2181 +  done
1.2183 +lemma not_trivial_limit_within: "\<not> trivial_limit (at x within S) = (x \<in> closure (S - {x}))"
1.2184 +  using islimpt_in_closure by (metis trivial_limit_within)
1.2186 +lemma not_in_closure_trivial_limitI:
1.2187 +  "x \<notin> closure s \<Longrightarrow> trivial_limit (at x within s)"
1.2188 +  using not_trivial_limit_within[of x s]
1.2189 +  by safe (metis Diff_empty Diff_insert0 closure_subset contra_subsetD)
1.2191 +lemma filterlim_at_within_closure_implies_filterlim: "filterlim f l (at x within s)"
1.2192 +  if "x \<in> closure s \<Longrightarrow> filterlim f l (at x within s)"
1.2193 +  by (metis bot.extremum filterlim_filtercomap filterlim_mono not_in_closure_trivial_limitI that)
1.2195 +lemma at_within_eq_bot_iff: "at c within A = bot \<longleftrightarrow> c \<notin> closure (A - {c})"
1.2196 +  using not_trivial_limit_within[of c A] by blast
1.2198 +text \<open>Some property holds "sufficiently close" to the limit point.\<close>
1.2200 +lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
1.2201 +  by simp
1.2203 +lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
1.2204 +  by (simp add: filter_eq_iff)
1.2207 +subsection \<open>Limits\<close>
1.2209 +proposition Lim: "(f \<longlongrightarrow> l) net \<longleftrightarrow> trivial_limit net \<or> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
1.2210 +  by (auto simp: tendsto_iff trivial_limit_eq)
1.2212 +text \<open>Show that they yield usual definitions in the various cases.\<close>
1.2214 +proposition Lim_within_le: "(f \<longlongrightarrow> l)(at a within S) \<longleftrightarrow>
1.2215 +    (\<forall>e>0. \<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a \<le> d \<longrightarrow> dist (f x) l < e)"
1.2216 +  by (auto simp: tendsto_iff eventually_at_le)
1.2218 +proposition Lim_within: "(f \<longlongrightarrow> l) (at a within S) \<longleftrightarrow>
1.2219 +    (\<forall>e >0. \<exists>d>0. \<forall>x \<in> S. 0 < dist x a \<and> dist x a  < d \<longrightarrow> dist (f x) l < e)"
1.2220 +  by (auto simp: tendsto_iff eventually_at)
1.2222 +corollary Lim_withinI [intro?]:
1.2223 +  assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>d>0. \<forall>x \<in> S. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) l \<le> e"
1.2224 +  shows "(f \<longlongrightarrow> l) (at a within S)"
1.2225 +  apply (simp add: Lim_within, clarify)
1.2226 +  apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
1.2227 +  done
1.2229 +proposition Lim_at: "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow>
1.2230 +    (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d  \<longrightarrow> dist (f x) l < e)"
1.2231 +  by (auto simp: tendsto_iff eventually_at)
1.2233 +proposition Lim_at_infinity: "(f \<longlongrightarrow> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x \<ge> b \<longrightarrow> dist (f x) l < e)"
1.2234 +  by (auto simp: tendsto_iff eventually_at_infinity)
1.2236 +corollary Lim_at_infinityI [intro?]:
1.2237 +  assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>B. \<forall>x. norm x \<ge> B \<longrightarrow> dist (f x) l \<le> e"
1.2238 +  shows "(f \<longlongrightarrow> l) at_infinity"
1.2239 +  apply (simp add: Lim_at_infinity, clarify)
1.2240 +  apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
1.2241 +  done
1.2243 +lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f \<longlongrightarrow> l) net"
1.2244 +  by (rule topological_tendstoI) (auto elim: eventually_mono)
1.2246 +lemma Lim_transform_within_set:
1.2247 +  fixes a :: "'a::metric_space" and l :: "'b::metric_space"
1.2248 +  shows "\<lbrakk>(f \<longlongrightarrow> l) (at a within S); eventually (\<lambda>x. x \<in> S \<longleftrightarrow> x \<in> T) (at a)\<rbrakk>
1.2249 +         \<Longrightarrow> (f \<longlongrightarrow> l) (at a within T)"
1.2250 +apply (clarsimp simp: eventually_at Lim_within)
1.2251 +apply (drule_tac x=e in spec, clarify)
1.2252 +apply (rename_tac k)
1.2253 +apply (rule_tac x="min d k" in exI, simp)
1.2254 +done
1.2256 +lemma Lim_transform_within_set_eq:
1.2257 +  fixes a l :: "'a::real_normed_vector"
1.2258 +  shows "eventually (\<lambda>x. x \<in> s \<longleftrightarrow> x \<in> t) (at a)
1.2259 +         \<Longrightarrow> ((f \<longlongrightarrow> l) (at a within s) \<longleftrightarrow> (f \<longlongrightarrow> l) (at a within t))"
1.2260 +  by (force intro: Lim_transform_within_set elim: eventually_mono)
1.2262 +lemma Lim_transform_within_openin:
1.2263 +  fixes a :: "'a::metric_space"
1.2264 +  assumes f: "(f \<longlongrightarrow> l) (at a within T)"
1.2265 +    and "openin (subtopology euclidean T) S" "a \<in> S"
1.2266 +    and eq: "\<And>x. \<lbrakk>x \<in> S; x \<noteq> a\<rbrakk> \<Longrightarrow> f x = g x"
1.2267 +  shows "(g \<longlongrightarrow> l) (at a within T)"
1.2268 +proof -
1.2269 +  obtain \<epsilon> where "0 < \<epsilon>" and \<epsilon>: "ball a \<epsilon> \<inter> T \<subseteq> S"
1.2270 +    using assms by (force simp: openin_contains_ball)
1.2271 +  then have "a \<in> ball a \<epsilon>"
1.2272 +    by simp
1.2273 +  show ?thesis
1.2274 +    by (rule Lim_transform_within [OF f \<open>0 < \<epsilon>\<close> eq]) (use \<epsilon> in \<open>auto simp: dist_commute subset_iff\<close>)
1.2275 +qed
1.2277 +lemma continuous_transform_within_openin:
1.2278 +  fixes a :: "'a::metric_space"
1.2279 +  assumes "continuous (at a within T) f"
1.2280 +    and "openin (subtopology euclidean T) S" "a \<in> S"
1.2281 +    and eq: "\<And>x. x \<in> S \<Longrightarrow> f x = g x"
1.2282 +  shows "continuous (at a within T) g"
1.2283 +  using assms by (simp add: Lim_transform_within_openin continuous_within)
1.2285 +text \<open>The expected monotonicity property.\<close>
1.2287 +lemma Lim_Un:
1.2288 +  assumes "(f \<longlongrightarrow> l) (at x within S)" "(f \<longlongrightarrow> l) (at x within T)"
1.2289 +  shows "(f \<longlongrightarrow> l) (at x within (S \<union> T))"
1.2290 +  using assms unfolding at_within_union by (rule filterlim_sup)
1.2292 +lemma Lim_Un_univ:
1.2293 +  "(f \<longlongrightarrow> l) (at x within S) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within T) \<Longrightarrow>
1.2294 +    S \<union> T = UNIV \<Longrightarrow> (f \<longlongrightarrow> l) (at x)"
1.2295 +  by (metis Lim_Un)
1.2297 +text \<open>Interrelations between restricted and unrestricted limits.\<close>
1.2299 +lemma Lim_at_imp_Lim_at_within: "(f \<longlongrightarrow> l) (at x) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S)"
1.2300 +  by (metis order_refl filterlim_mono subset_UNIV at_le)
1.2302 +lemma eventually_within_interior:
1.2303 +  assumes "x \<in> interior S"
1.2304 +  shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)"
1.2305 +  (is "?lhs = ?rhs")
1.2306 +proof
1.2307 +  from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S" ..
1.2308 +  {
1.2309 +    assume ?lhs
1.2310 +    then obtain A where "open A" and "x \<in> A" and "\<forall>y\<in>A. y \<noteq> x \<longrightarrow> y \<in> S \<longrightarrow> P y"
1.2311 +      by (auto simp: eventually_at_topological)
1.2312 +    with T have "open (A \<inter> T)" and "x \<in> A \<inter> T" and "\<forall>y \<in> A \<inter> T. y \<noteq> x \<longrightarrow> P y"
1.2313 +      by auto
1.2314 +    then show ?rhs
1.2315 +      by (auto simp: eventually_at_topological)
1.2316 +  next
1.2317 +    assume ?rhs
1.2318 +    then show ?lhs
1.2319 +      by (auto elim: eventually_mono simp: eventually_at_filter)
1.2320 +  }
1.2321 +qed
1.2323 +lemma at_within_interior: "x \<in> interior S \<Longrightarrow> at x within S = at x"
1.2324 +  unfolding filter_eq_iff by (intro allI eventually_within_interior)
1.2326 +lemma Lim_within_LIMSEQ:
1.2327 +  fixes a :: "'a::first_countable_topology"
1.2328 +  assumes "\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
1.2329 +  shows "(X \<longlongrightarrow> L) (at a within T)"
1.2330 +  using assms unfolding tendsto_def [where l=L]
1.2331 +  by (simp add: sequentially_imp_eventually_within)
1.2333 +lemma Lim_right_bound:
1.2334 +  fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder, no_top} \<Rightarrow>
1.2335 +    'b::{linorder_topology, conditionally_complete_linorder}"
1.2336 +  assumes mono: "\<And>a b. a \<in> I \<Longrightarrow> b \<in> I \<Longrightarrow> x < a \<Longrightarrow> a \<le> b \<Longrightarrow> f a \<le> f b"
1.2337 +    and bnd: "\<And>a. a \<in> I \<Longrightarrow> x < a \<Longrightarrow> K \<le> f a"
1.2338 +  shows "(f \<longlongrightarrow> Inf (f ` ({x<..} \<inter> I))) (at x within ({x<..} \<inter> I))"
1.2339 +proof (cases "{x<..} \<inter> I = {}")
1.2340 +  case True
1.2341 +  then show ?thesis by simp
1.2342 +next
1.2343 +  case False
1.2344 +  show ?thesis
1.2345 +  proof (rule order_tendstoI)
1.2346 +    fix a
1.2347 +    assume a: "a < Inf (f ` ({x<..} \<inter> I))"
1.2348 +    {
1.2349 +      fix y
1.2350 +      assume "y \<in> {x<..} \<inter> I"
1.2351 +      with False bnd have "Inf (f ` ({x<..} \<inter> I)) \<le> f y"
1.2352 +        by (auto intro!: cInf_lower bdd_belowI2)
1.2353 +      with a have "a < f y"
1.2354 +        by (blast intro: less_le_trans)
1.2355 +    }
1.2356 +    then show "eventually (\<lambda>x. a < f x) (at x within ({x<..} \<inter> I))"
1.2357 +      by (auto simp: eventually_at_filter intro: exI[of _ 1] zero_less_one)
1.2358 +  next
1.2359 +    fix a
1.2360 +    assume "Inf (f ` ({x<..} \<inter> I)) < a"
1.2361 +    from cInf_lessD[OF _ this] False obtain y where y: "x < y" "y \<in> I" "f y < a"
1.2362 +      by auto
1.2363 +    then have "eventually (\<lambda>x. x \<in> I \<longrightarrow> f x < a) (at_right x)"
1.2364 +      unfolding eventually_at_right[OF \<open>x < y\<close>] by (metis less_imp_le le_less_trans mono)
1.2365 +    then show "eventually (\<lambda>x. f x < a) (at x within ({x<..} \<inter> I))"
1.2366 +      unfolding eventually_at_filter by eventually_elim simp
1.2367 +  qed
1.2368 +qed
1.2370 +text \<open>Another limit point characterization.\<close>
1.2372 +lemma limpt_sequential_inj:
1.2373 +  fixes x :: "'a::metric_space"
1.2374 +  shows "x islimpt S \<longleftrightarrow>
1.2375 +         (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> inj f \<and> (f \<longlongrightarrow> x) sequentially)"
1.2376 +         (is "?lhs = ?rhs")
1.2377 +proof
1.2378 +  assume ?lhs
1.2379 +  then have "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
1.2380 +    by (force simp: islimpt_approachable)
1.2381 +  then obtain y where y: "\<And>e. e>0 \<Longrightarrow> y e \<in> S \<and> y e \<noteq> x \<and> dist (y e) x < e"
1.2382 +    by metis
1.2383 +  define f where "f \<equiv> rec_nat (y 1) (\<lambda>n fn. y (min (inverse(2 ^ (Suc n))) (dist fn x)))"
1.2384 +  have [simp]: "f 0 = y 1"
1.2385 +               "f(Suc n) = y (min (inverse(2 ^ (Suc n))) (dist (f n) x))" for n
1.2386 +    by (simp_all add: f_def)
1.2387 +  have f: "f n \<in> S \<and> (f n \<noteq> x) \<and> dist (f n) x < inverse(2 ^ n)" for n
1.2388 +  proof (induction n)
1.2389 +    case 0 show ?case
1.2390 +      by (simp add: y)
1.2391 +  next
1.2392 +    case (Suc n) then show ?case
1.2393 +      apply (auto simp: y)
1.2394 +      by (metis half_gt_zero_iff inverse_positive_iff_positive less_divide_eq_numeral1(1) min_less_iff_conj y zero_less_dist_iff zero_less_numeral zero_less_power)
1.2395 +  qed
1.2396 +  show ?rhs
1.2397 +  proof (rule_tac x=f in exI, intro conjI allI)
1.2398 +    show "\<And>n. f n \<in> S - {x}"
1.2399 +      using f by blast
1.2400 +    have "dist (f n) x < dist (f m) x" if "m < n" for m n
1.2401 +    using that
1.2402 +    proof (induction n)
1.2403 +      case 0 then show ?case by simp
1.2404 +    next
1.2405 +      case (Suc n)
1.2406 +      then consider "m < n" | "m = n" using less_Suc_eq by blast
1.2407 +      then show ?case
1.2408 +      proof cases
1.2409 +        assume "m < n"
1.2410 +        have "dist (f(Suc n)) x = dist (y (min (inverse(2 ^ (Suc n))) (dist (f n) x))) x"
1.2411 +          by simp
1.2412 +        also have "\<dots> < dist (f n) x"
1.2413 +          by (metis dist_pos_lt f min.strict_order_iff min_less_iff_conj y)
1.2414 +        also have "\<dots> < dist (f m) x"
1.2415 +          using Suc.IH \<open>m < n\<close> by blast
1.2416 +        finally show ?thesis .
1.2417 +      next
1.2418 +        assume "m = n" then show ?case
1.2419 +          by simp (metis dist_pos_lt f half_gt_zero_iff inverse_positive_iff_positive min_less_iff_conj y zero_less_numeral zero_less_power)
1.2420 +      qed
1.2421 +    qed
1.2422 +    then show "inj f"
1.2423 +      by (metis less_irrefl linorder_injI)
1.2424 +    show "f \<longlonglongrightarrow> x"
1.2425 +      apply (rule tendstoI)
1.2426 +      apply (rule_tac c="nat (ceiling(1/e))" in eventually_sequentiallyI)
1.2427 +      apply (rule less_trans [OF f [THEN conjunct2, THEN conjunct2]])
1.2428 +      apply (simp add: field_simps)
1.2429 +      by (meson le_less_trans mult_less_cancel_left not_le of_nat_less_two_power)
1.2430 +  qed
1.2431 +next
1.2432 +  assume ?rhs
1.2433 +  then show ?lhs
1.2434 +    by (fastforce simp add: islimpt_approachable lim_sequentially)
1.2435 +qed
1.2437 +(*could prove directly from islimpt_sequential_inj, but only for metric spaces*)
1.2438 +lemma islimpt_sequential:
1.2439 +  fixes x :: "'a::first_countable_topology"
1.2440 +  shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> (f \<longlongrightarrow> x) sequentially)"
1.2441 +    (is "?lhs = ?rhs")
1.2442 +proof
1.2443 +  assume ?lhs
1.2444 +  from countable_basis_at_decseq[of x] obtain A where A:
1.2445 +      "\<And>i. open (A i)"
1.2446 +      "\<And>i. x \<in> A i"
1.2447 +      "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
1.2448 +    by blast
1.2449 +  define f where "f n = (SOME y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y)" for n
1.2450 +  {
1.2451 +    fix n
1.2452 +    from \<open>?lhs\<close> have "\<exists>y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y"
1.2453 +      unfolding islimpt_def using A(1,2)[of n] by auto
1.2454 +    then have "f n \<in> S \<and> f n \<in> A n \<and> x \<noteq> f n"
1.2455 +      unfolding f_def by (rule someI_ex)
1.2456 +    then have "f n \<in> S" "f n \<in> A n" "x \<noteq> f n" by auto
1.2457 +  }
1.2458 +  then have "\<forall>n. f n \<in> S - {x}" by auto
1.2459 +  moreover have "(\<lambda>n. f n) \<longlonglongrightarrow> x"
1.2460 +  proof (rule topological_tendstoI)
1.2461 +    fix S
1.2462 +    assume "open S" "x \<in> S"
1.2463 +    from A(3)[OF this] \<open>\<And>n. f n \<in> A n\<close>
1.2464 +    show "eventually (\<lambda>x. f x \<in> S) sequentially"
1.2465 +      by (auto elim!: eventually_mono)
1.2466 +  qed
1.2467 +  ultimately show ?rhs by fast
1.2468 +next
1.2469 +  assume ?rhs
1.2470 +  then obtain f :: "nat \<Rightarrow> 'a" where f: "\<And>n. f n \<in> S - {x}" and lim: "f \<longlonglongrightarrow> x"
1.2471 +    by auto
1.2472 +  show ?lhs
1.2473 +    unfolding islimpt_def
1.2474 +  proof safe
1.2475 +    fix T
1.2476 +    assume "open T" "x \<in> T"
1.2477 +    from lim[THEN topological_tendstoD, OF this] f
1.2478 +    show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
1.2479 +      unfolding eventually_sequentially by auto
1.2480 +  qed
1.2481 +qed
1.2483 +lemma Lim_null:
1.2484 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.2485 +  shows "(f \<longlongrightarrow> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) \<longlongrightarrow> 0) net"
1.2486 +  by (simp add: Lim dist_norm)
1.2488 +lemma Lim_null_comparison:
1.2489 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.2490 +  assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g \<longlongrightarrow> 0) net"
1.2491 +  shows "(f \<longlongrightarrow> 0) net"
1.2492 +  using assms(2)
1.2493 +proof (rule metric_tendsto_imp_tendsto)
1.2494 +  show "eventually (\<lambda>x. dist (f x) 0 \<le> dist (g x) 0) net"
1.2495 +    using assms(1) by (rule eventually_mono) (simp add: dist_norm)
1.2496 +qed
1.2498 +lemma Lim_transform_bound:
1.2499 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.2500 +    and g :: "'a \<Rightarrow> 'c::real_normed_vector"
1.2501 +  assumes "eventually (\<lambda>n. norm (f n) \<le> norm (g n)) net"
1.2502 +    and "(g \<longlongrightarrow> 0) net"
1.2503 +  shows "(f \<longlongrightarrow> 0) net"
1.2504 +  using assms(1) tendsto_norm_zero [OF assms(2)]
1.2505 +  by (rule Lim_null_comparison)
1.2507 +lemma lim_null_mult_right_bounded:
1.2508 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
1.2509 +  assumes f: "(f \<longlongrightarrow> 0) F" and g: "eventually (\<lambda>x. norm(g x) \<le> B) F"
1.2510 +    shows "((\<lambda>z. f z * g z) \<longlongrightarrow> 0) F"
1.2511 +proof -
1.2512 +  have *: "((\<lambda>x. norm (f x) * B) \<longlongrightarrow> 0) F"
1.2513 +    by (simp add: f tendsto_mult_left_zero tendsto_norm_zero)
1.2514 +  have "((\<lambda>x. norm (f x) * norm (g x)) \<longlongrightarrow> 0) F"
1.2515 +    apply (rule Lim_null_comparison [OF _ *])
1.2516 +    apply (simp add: eventually_mono [OF g] mult_left_mono)
1.2517 +    done
1.2518 +  then show ?thesis
1.2519 +    by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
1.2520 +qed
1.2522 +lemma lim_null_mult_left_bounded:
1.2523 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
1.2524 +  assumes g: "eventually (\<lambda>x. norm(g x) \<le> B) F" and f: "(f \<longlongrightarrow> 0) F"
1.2525 +    shows "((\<lambda>z. g z * f z) \<longlongrightarrow> 0) F"
1.2526 +proof -
1.2527 +  have *: "((\<lambda>x. B * norm (f x)) \<longlongrightarrow> 0) F"
1.2528 +    by (simp add: f tendsto_mult_right_zero tendsto_norm_zero)
1.2529 +  have "((\<lambda>x. norm (g x) * norm (f x)) \<longlongrightarrow> 0) F"
1.2530 +    apply (rule Lim_null_comparison [OF _ *])
1.2531 +    apply (simp add: eventually_mono [OF g] mult_right_mono)
1.2532 +    done
1.2533 +  then show ?thesis
1.2534 +    by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
1.2535 +qed
1.2537 +lemma lim_null_scaleR_bounded:
1.2538 +  assumes f: "(f \<longlongrightarrow> 0) net" and gB: "eventually (\<lambda>a. f a = 0 \<or> norm(g a) \<le> B) net"
1.2539 +    shows "((\<lambda>n. f n *\<^sub>R g n) \<longlongrightarrow> 0) net"
1.2540 +proof
1.2541 +  fix \<epsilon>::real
1.2542 +  assume "0 < \<epsilon>"
1.2543 +  then have B: "0 < \<epsilon> / (abs B + 1)" by simp
1.2544 +  have *: "\<bar>f x\<bar> * norm (g x) < \<epsilon>" if f: "\<bar>f x\<bar> * (\<bar>B\<bar> + 1) < \<epsilon>" and g: "norm (g x) \<le> B" for x
1.2545 +  proof -
1.2546 +    have "\<bar>f x\<bar> * norm (g x) \<le> \<bar>f x\<bar> * B"
1.2547 +      by (simp add: mult_left_mono g)
1.2548 +    also have "\<dots> \<le> \<bar>f x\<bar> * (\<bar>B\<bar> + 1)"
1.2549 +      by (simp add: mult_left_mono)
1.2550 +    also have "\<dots> < \<epsilon>"
1.2551 +      by (rule f)
1.2552 +    finally show ?thesis .
1.2553 +  qed
1.2554 +  show "\<forall>\<^sub>F x in net. dist (f x *\<^sub>R g x) 0 < \<epsilon>"
1.2555 +    apply (rule eventually_mono [OF eventually_conj [OF tendstoD [OF f B] gB] ])
1.2556 +    apply (auto simp: \<open>0 < \<epsilon>\<close> divide_simps * split: if_split_asm)
1.2557 +    done
1.2558 +qed
1.2560 +text\<open>Deducing things about the limit from the elements.\<close>
1.2562 +lemma Lim_in_closed_set:
1.2563 +  assumes "closed S"
1.2564 +    and "eventually (\<lambda>x. f(x) \<in> S) net"
1.2565 +    and "\<not> trivial_limit net" "(f \<longlongrightarrow> l) net"
1.2566 +  shows "l \<in> S"
1.2567 +proof (rule ccontr)
1.2568 +  assume "l \<notin> S"
1.2569 +  with \<open>closed S\<close> have "open (- S)" "l \<in> - S"
1.2570 +    by (simp_all add: open_Compl)
1.2571 +  with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
1.2572 +    by (rule topological_tendstoD)
1.2573 +  with assms(2) have "eventually (\<lambda>x. False) net"
1.2574 +    by (rule eventually_elim2) simp
1.2575 +  with assms(3) show "False"
1.2576 +    by (simp add: eventually_False)
1.2577 +qed
1.2579 +text\<open>Need to prove closed(cball(x,e)) before deducing this as a corollary.\<close>
1.2581 +lemma Lim_dist_ubound:
1.2582 +  assumes "\<not>(trivial_limit net)"
1.2583 +    and "(f \<longlongrightarrow> l) net"
1.2584 +    and "eventually (\<lambda>x. dist a (f x) \<le> e) net"
1.2585 +  shows "dist a l \<le> e"
1.2586 +  using assms by (fast intro: tendsto_le tendsto_intros)
1.2588 +lemma Lim_norm_ubound:
1.2589 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.2590 +  assumes "\<not>(trivial_limit net)" "(f \<longlongrightarrow> l) net" "eventually (\<lambda>x. norm(f x) \<le> e) net"
1.2591 +  shows "norm(l) \<le> e"
1.2592 +  using assms by (fast intro: tendsto_le tendsto_intros)
1.2594 +lemma Lim_norm_lbound:
1.2595 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.2596 +  assumes "\<not> trivial_limit net"
1.2597 +    and "(f \<longlongrightarrow> l) net"
1.2598 +    and "eventually (\<lambda>x. e \<le> norm (f x)) net"
1.2599 +  shows "e \<le> norm l"
1.2600 +  using assms by (fast intro: tendsto_le tendsto_intros)
1.2602 +text\<open>Limit under bilinear function\<close>
1.2604 +lemma Lim_bilinear:
1.2605 +  assumes "(f \<longlongrightarrow> l) net"
1.2606 +    and "(g \<longlongrightarrow> m) net"
1.2607 +    and "bounded_bilinear h"
1.2608 +  shows "((\<lambda>x. h (f x) (g x)) \<longlongrightarrow> (h l m)) net"
1.2609 +  using \<open>bounded_bilinear h\<close> \<open>(f \<longlongrightarrow> l) net\<close> \<open>(g \<longlongrightarrow> m) net\<close>
1.2610 +  by (rule bounded_bilinear.tendsto)
1.2612 +text\<open>These are special for limits out of the same vector space.\<close>
1.2614 +lemma Lim_within_id: "(id \<longlongrightarrow> a) (at a within s)"
1.2615 +  unfolding id_def by (rule tendsto_ident_at)
1.2617 +lemma Lim_at_id: "(id \<longlongrightarrow> a) (at a)"
1.2618 +  unfolding id_def by (rule tendsto_ident_at)
1.2620 +lemma Lim_at_zero:
1.2621 +  fixes a :: "'a::real_normed_vector"
1.2622 +    and l :: "'b::topological_space"
1.2623 +  shows "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) \<longlongrightarrow> l) (at 0)"
1.2624 +  using LIM_offset_zero LIM_offset_zero_cancel ..
1.2626 +text\<open>It's also sometimes useful to extract the limit point from the filter.\<close>
1.2628 +abbreviation netlimit :: "'a::t2_space filter \<Rightarrow> 'a"
1.2629 +  where "netlimit F \<equiv> Lim F (\<lambda>x. x)"
1.2631 +lemma netlimit_within: "\<not> trivial_limit (at a within S) \<Longrightarrow> netlimit (at a within S) = a"
1.2632 +  by (rule tendsto_Lim) (auto intro: tendsto_intros)
1.2634 +lemma netlimit_at [simp]:
1.2635 +  fixes a :: "'a::{perfect_space,t2_space}"
1.2636 +  shows "netlimit (at a) = a"
1.2637 +  using netlimit_within [of a UNIV] by simp
1.2639 +lemma lim_within_interior:
1.2640 +  "x \<in> interior S \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S) \<longleftrightarrow> (f \<longlongrightarrow> l) (at x)"
1.2641 +  by (metis at_within_interior)
1.2643 +lemma netlimit_within_interior:
1.2644 +  fixes x :: "'a::{t2_space,perfect_space}"
1.2645 +  assumes "x \<in> interior S"
1.2646 +  shows "netlimit (at x within S) = x"
1.2647 +  using assms by (metis at_within_interior netlimit_at)
1.2649 +lemma netlimit_at_vector:
1.2650 +  fixes a :: "'a::real_normed_vector"
1.2651 +  shows "netlimit (at a) = a"
1.2652 +proof (cases "\<exists>x. x \<noteq> a")
1.2653 +  case True then obtain x where x: "x \<noteq> a" ..
1.2654 +  have "\<not> trivial_limit (at a)"
1.2655 +    unfolding trivial_limit_def eventually_at dist_norm
1.2656 +    apply clarsimp
1.2657 +    apply (rule_tac x="a + scaleR (d / 2) (sgn (x - a))" in exI)
1.2658 +    apply (simp add: norm_sgn sgn_zero_iff x)
1.2659 +    done
1.2660 +  then show ?thesis
1.2661 +    by (rule netlimit_within [of a UNIV])
1.2662 +qed simp
1.2665 +text\<open>Useful lemmas on closure and set of possible sequential limits.\<close>
1.2667 +lemma closure_sequential:
1.2668 +  fixes l :: "'a::first_countable_topology"
1.2669 +  shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially)"
1.2670 +  (is "?lhs = ?rhs")
1.2671 +proof
1.2672 +  assume "?lhs"
1.2673 +  moreover
1.2674 +  {
1.2675 +    assume "l \<in> S"
1.2676 +    then have "?rhs" using tendsto_const[of l sequentially] by auto
1.2677 +  }
1.2678 +  moreover
1.2679 +  {
1.2680 +    assume "l islimpt S"
1.2681 +    then have "?rhs" unfolding islimpt_sequential by auto
1.2682 +  }
1.2683 +  ultimately show "?rhs"
1.2684 +    unfolding closure_def by auto
1.2685 +next
1.2686 +  assume "?rhs"
1.2687 +  then show "?lhs" unfolding closure_def islimpt_sequential by auto
1.2688 +qed
1.2690 +lemma closed_sequential_limits:
1.2691 +  fixes S :: "'a::first_countable_topology set"
1.2692 +  shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially \<longrightarrow> l \<in> S)"
1.2693 +by (metis closure_sequential closure_subset_eq subset_iff)
1.2695 +lemma closure_approachable:
1.2696 +  fixes S :: "'a::metric_space set"
1.2697 +  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
1.2698 +  apply (auto simp: closure_def islimpt_approachable)
1.2699 +  apply (metis dist_self)
1.2700 +  done
1.2702 +lemma closure_approachable_le:
1.2703 +  fixes S :: "'a::metric_space set"
1.2704 +  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x \<le> e)"
1.2705 +  unfolding closure_approachable
1.2706 +  using dense by force
1.2708 +lemma closure_approachableD:
1.2709 +  assumes "x \<in> closure S" "e>0"
1.2710 +  shows "\<exists>y\<in>S. dist x y < e"
1.2711 +  using assms unfolding closure_approachable by (auto simp: dist_commute)
1.2713 +lemma closed_approachable:
1.2714 +  fixes S :: "'a::metric_space set"
1.2715 +  shows "closed S \<Longrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
1.2716 +  by (metis closure_closed closure_approachable)
1.2718 +lemma closure_contains_Inf:
1.2719 +  fixes S :: "real set"
1.2720 +  assumes "S \<noteq> {}" "bdd_below S"
1.2721 +  shows "Inf S \<in> closure S"
1.2722 +proof -
1.2723 +  have *: "\<forall>x\<in>S. Inf S \<le> x"
1.2724 +    using cInf_lower[of _ S] assms by metis
1.2725 +  {
1.2726 +    fix e :: real
1.2727 +    assume "e > 0"
1.2728 +    then have "Inf S < Inf S + e" by simp
1.2729 +    with assms obtain x where "x \<in> S" "x < Inf S + e"
1.2730 +      by (subst (asm) cInf_less_iff) auto
1.2731 +    with * have "\<exists>x\<in>S. dist x (Inf S) < e"
1.2732 +      by (intro bexI[of _ x]) (auto simp: dist_real_def)
1.2733 +  }
1.2734 +  then show ?thesis unfolding closure_approachable by auto
1.2735 +qed
1.2737 +lemma closure_Int_ballI:
1.2738 +  fixes S :: "'a :: metric_space set"
1.2739 +  assumes "\<And>U. \<lbrakk>openin (subtopology euclidean S) U; U \<noteq> {}\<rbrakk> \<Longrightarrow> T \<inter> U \<noteq> {}"
1.2740 + shows "S \<subseteq> closure T"
1.2741 +proof (clarsimp simp: closure_approachable dist_commute)
1.2742 +  fix x and e::real
1.2743 +  assume "x \<in> S" "0 < e"
1.2744 +  with assms [of "S \<inter> ball x e"] show "\<exists>y\<in>T. dist x y < e"
1.2745 +    by force
1.2746 +qed
1.2748 +lemma closed_contains_Inf:
1.2749 +  fixes S :: "real set"
1.2750 +  shows "S \<noteq> {} \<Longrightarrow> bdd_below S \<Longrightarrow> closed S \<Longrightarrow> Inf S \<in> S"
1.2751 +  by (metis closure_contains_Inf closure_closed)
1.2753 +lemma closed_subset_contains_Inf:
1.2754 +  fixes A C :: "real set"
1.2755 +  shows "closed C \<Longrightarrow> A \<subseteq> C \<Longrightarrow> A \<noteq> {} \<Longrightarrow> bdd_below A \<Longrightarrow> Inf A \<in> C"
1.2756 +  by (metis closure_contains_Inf closure_minimal subset_eq)
1.2758 +lemma atLeastAtMost_subset_contains_Inf:
1.2759 +  fixes A :: "real set" and a b :: real
1.2760 +  shows "A \<noteq> {} \<Longrightarrow> a \<le> b \<Longrightarrow> A \<subseteq> {a..b} \<Longrightarrow> Inf A \<in> {a..b}"
1.2761 +  by (rule closed_subset_contains_Inf)
1.2762 +     (auto intro: closed_real_atLeastAtMost intro!: bdd_belowI[of A a])
1.2764 +lemma not_trivial_limit_within_ball:
1.2765 +  "\<not> trivial_limit (at x within S) \<longleftrightarrow> (\<forall>e>0. S \<inter> ball x e - {x} \<noteq> {})"
1.2766 +  (is "?lhs \<longleftrightarrow> ?rhs")
1.2767 +proof
1.2768 +  show ?rhs if ?lhs
1.2769 +  proof -
1.2770 +    {
1.2771 +      fix e :: real
1.2772 +      assume "e > 0"
1.2773 +      then obtain y where "y \<in> S - {x}" and "dist y x < e"
1.2774 +        using \<open>?lhs\<close> not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
1.2775 +        by auto
1.2776 +      then have "y \<in> S \<inter> ball x e - {x}"
1.2777 +        unfolding ball_def by (simp add: dist_commute)
1.2778 +      then have "S \<inter> ball x e - {x} \<noteq> {}" by blast
1.2779 +    }
1.2780 +    then show ?thesis by auto
1.2781 +  qed
1.2782 +  show ?lhs if ?rhs
1.2783 +  proof -
1.2784 +    {
1.2785 +      fix e :: real
1.2786 +      assume "e > 0"
1.2787 +      then obtain y where "y \<in> S \<inter> ball x e - {x}"
1.2788 +        using \<open>?rhs\<close> by blast
1.2789 +      then have "y \<in> S - {x}" and "dist y x < e"
1.2790 +        unfolding ball_def by (simp_all add: dist_commute)
1.2791 +      then have "\<exists>y \<in> S - {x}. dist y x < e"
1.2792 +        by auto
1.2793 +    }
1.2794 +    then show ?thesis
1.2795 +      using not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
1.2796 +      by auto
1.2797 +  qed
1.2798 +qed
1.2800 +lemma tendsto_If_within_closures:
1.2801 +  assumes f: "x \<in> s \<union> (closure s \<inter> closure t) \<Longrightarrow>
1.2802 +      (f \<longlongrightarrow> l x) (at x within s \<union> (closure s \<inter> closure t))"
1.2803 +  assumes g: "x \<in> t \<union> (closure s \<inter> closure t) \<Longrightarrow>
1.2804 +      (g \<longlongrightarrow> l x) (at x within t \<union> (closure s \<inter> closure t))"
1.2805 +  assumes "x \<in> s \<union> t"
1.2806 +  shows "((\<lambda>x. if x \<in> s then f x else g x) \<longlongrightarrow> l x) (at x within s \<union> t)"
1.2807 +proof -
1.2808 +  have *: "(s \<union> t) \<inter> {x. x \<in> s} = s" "(s \<union> t) \<inter> {x. x \<notin> s} = t - s"
1.2809 +    by auto
1.2810 +  have "(f \<longlongrightarrow> l x) (at x within s)"
1.2811 +    by (rule filterlim_at_within_closure_implies_filterlim)
1.2812 +       (use \<open>x \<in> _\<close> in \<open>auto simp: inf_commute closure_def intro: tendsto_within_subset[OF f]\<close>)
1.2813 +  moreover
1.2814 +  have "(g \<longlongrightarrow> l x) (at x within t - s)"
1.2815 +    by (rule filterlim_at_within_closure_implies_filterlim)
1.2816 +      (use \<open>x \<in> _\<close> in
1.2817 +        \<open>auto intro!: tendsto_within_subset[OF g] simp: closure_def intro: islimpt_subset\<close>)
1.2818 +  ultimately show ?thesis
1.2819 +    by (intro filterlim_at_within_If) (simp_all only: *)
1.2820 +qed
1.2823 +subsection \<open>Boundedness\<close>
1.2825 +  (* FIXME: This has to be unified with BSEQ!! *)
1.2826 +definition%important (in metric_space) bounded :: "'a set \<Rightarrow> bool"
1.2827 +  where "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
1.2829 +lemma bounded_subset_cball: "bounded S \<longleftrightarrow> (\<exists>e x. S \<subseteq> cball x e \<and> 0 \<le> e)"
1.2830 +  unfolding bounded_def subset_eq  by auto (meson order_trans zero_le_dist)
1.2832 +lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
1.2833 +  unfolding bounded_def
1.2836 +lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
1.2837 +  unfolding bounded_any_center [where a=0]
1.2838 +  by (simp add: dist_norm)
1.2840 +lemma bdd_above_norm: "bdd_above (norm ` X) \<longleftrightarrow> bounded X"
1.2841 +  by (simp add: bounded_iff bdd_above_def)
1.2843 +lemma bounded_norm_comp: "bounded ((\<lambda>x. norm (f x)) ` S) = bounded (f ` S)"
1.2844 +  by (simp add: bounded_iff)
1.2846 +lemma boundedI:
1.2847 +  assumes "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> B"
1.2848 +  shows "bounded S"
1.2849 +  using assms bounded_iff by blast
1.2851 +lemma bounded_empty [simp]: "bounded {}"
1.2852 +  by (simp add: bounded_def)
1.2854 +lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> bounded S"
1.2855 +  by (metis bounded_def subset_eq)
1.2857 +lemma bounded_interior[intro]: "bounded S \<Longrightarrow> bounded(interior S)"
1.2858 +  by (metis bounded_subset interior_subset)
1.2860 +lemma bounded_closure[intro]:
1.2861 +  assumes "bounded S"
1.2862 +  shows "bounded (closure S)"
1.2863 +proof -
1.2864 +  from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a"
1.2865 +    unfolding bounded_def by auto
1.2866 +  {
1.2867 +    fix y
1.2868 +    assume "y \<in> closure S"
1.2869 +    then obtain f where f: "\<forall>n. f n \<in> S"  "(f \<longlongrightarrow> y) sequentially"
1.2870 +      unfolding closure_sequential by auto
1.2871 +    have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
1.2872 +    then have "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
1.2873 +      by (simp add: f(1))
1.2874 +    have "dist x y \<le> a"
1.2875 +      apply (rule Lim_dist_ubound [of sequentially f])
1.2876 +      apply (rule trivial_limit_sequentially)
1.2877 +      apply (rule f(2))
1.2878 +      apply fact
1.2879 +      done
1.2880 +  }
1.2881 +  then show ?thesis
1.2882 +    unfolding bounded_def by auto
1.2883 +qed
1.2885 +lemma bounded_closure_image: "bounded (f ` closure S) \<Longrightarrow> bounded (f ` S)"
1.2886 +  by (simp add: bounded_subset closure_subset image_mono)
1.2888 +lemma bounded_cball[simp,intro]: "bounded (cball x e)"
1.2889 +  apply (simp add: bounded_def)
1.2890 +  apply (rule_tac x=x in exI)
1.2891 +  apply (rule_tac x=e in exI, auto)
1.2892 +  done
1.2894 +lemma bounded_ball[simp,intro]: "bounded (ball x e)"
1.2895 +  by (metis ball_subset_cball bounded_cball bounded_subset)
1.2897 +lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
1.2898 +  by (auto simp: bounded_def) (metis Un_iff bounded_any_center le_max_iff_disj)
1.2900 +lemma bounded_Union[intro]: "finite F \<Longrightarrow> \<forall>S\<in>F. bounded S \<Longrightarrow> bounded (\<Union>F)"
1.2901 +  by (induct rule: finite_induct[of F]) auto
1.2903 +lemma bounded_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. bounded (B x) \<Longrightarrow> bounded (\<Union>x\<in>A. B x)"
1.2904 +  by (induct set: finite) auto
1.2906 +lemma bounded_insert [simp]: "bounded (insert x S) \<longleftrightarrow> bounded S"
1.2907 +proof -
1.2908 +  have "\<forall>y\<in>{x}. dist x y \<le> 0"
1.2909 +    by simp
1.2910 +  then have "bounded {x}"
1.2911 +    unfolding bounded_def by fast
1.2912 +  then show ?thesis
1.2913 +    by (metis insert_is_Un bounded_Un)
1.2914 +qed
1.2916 +lemma bounded_subset_ballI: "S \<subseteq> ball x r \<Longrightarrow> bounded S"
1.2917 +  by (meson bounded_ball bounded_subset)
1.2919 +lemma bounded_subset_ballD:
1.2920 +  assumes "bounded S" shows "\<exists>r. 0 < r \<and> S \<subseteq> ball x r"
1.2921 +proof -
1.2922 +  obtain e::real and y where "S \<subseteq> cball y e"  "0 \<le> e"
1.2923 +    using assms by (auto simp: bounded_subset_cball)
1.2924 +  then show ?thesis
1.2925 +    apply (rule_tac x="dist x y + e + 1" in exI)
1.2927 +    apply (erule subset_trans)
1.2928 +    apply (clarsimp simp add: cball_def)
1.2930 +qed
1.2932 +lemma finite_imp_bounded [intro]: "finite S \<Longrightarrow> bounded S"
1.2933 +  by (induct set: finite) simp_all
1.2935 +lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x \<le> b)"
1.2936 +  apply (simp add: bounded_iff)
1.2937 +  apply (subgoal_tac "\<And>x (y::real). 0 < 1 + \<bar>y\<bar> \<and> (x \<le> y \<longrightarrow> x \<le> 1 + \<bar>y\<bar>)")
1.2938 +  apply metis
1.2939 +  apply arith
1.2940 +  done
1.2942 +lemma bounded_pos_less: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x < b)"
1.2943 +  apply (simp add: bounded_pos)
1.2944 +  apply (safe; rule_tac x="b+1" in exI; force)
1.2945 +  done
1.2947 +lemma Bseq_eq_bounded:
1.2948 +  fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
1.2949 +  shows "Bseq f \<longleftrightarrow> bounded (range f)"
1.2950 +  unfolding Bseq_def bounded_pos by auto
1.2952 +lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
1.2953 +  by (metis Int_lower1 Int_lower2 bounded_subset)
1.2955 +lemma bounded_diff[intro]: "bounded S \<Longrightarrow> bounded (S - T)"
1.2956 +  by (metis Diff_subset bounded_subset)
1.2958 +lemma not_bounded_UNIV[simp]:
1.2959 +  "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
1.2960 +proof (auto simp: bounded_pos not_le)
1.2961 +  obtain x :: 'a where "x \<noteq> 0"
1.2962 +    using perfect_choose_dist [OF zero_less_one] by fast
1.2963 +  fix b :: real
1.2964 +  assume b: "b >0"
1.2965 +  have b1: "b +1 \<ge> 0"
1.2966 +    using b by simp
1.2967 +  with \<open>x \<noteq> 0\<close> have "b < norm (scaleR (b + 1) (sgn x))"
1.2968 +    by (simp add: norm_sgn)
1.2969 +  then show "\<exists>x::'a. b < norm x" ..
1.2970 +qed
1.2972 +corollary cobounded_imp_unbounded:
1.2973 +    fixes S :: "'a::{real_normed_vector, perfect_space} set"
1.2974 +    shows "bounded (- S) \<Longrightarrow> \<not> bounded S"
1.2975 +  using bounded_Un [of S "-S"]  by (simp add: sup_compl_top)
1.2977 +lemma bounded_dist_comp:
1.2978 +  assumes "bounded (f ` S)" "bounded (g ` S)"
1.2979 +  shows "bounded ((\<lambda>x. dist (f x) (g x)) ` S)"
1.2980 +proof -
1.2981 +  from assms obtain M1 M2 where *: "dist (f x) undefined \<le> M1" "dist undefined (g x) \<le> M2" if "x \<in> S" for x
1.2982 +    by (auto simp: bounded_any_center[of _ undefined] dist_commute)
1.2983 +  have "dist (f x) (g x) \<le> M1 + M2" if "x \<in> S" for x
1.2984 +    using *[OF that]
1.2985 +    by (rule order_trans[OF dist_triangle add_mono])
1.2986 +  then show ?thesis
1.2987 +    by (auto intro!: boundedI)
1.2988 +qed
1.2990 +lemma bounded_linear_image:
1.2991 +  assumes "bounded S"
1.2992 +    and "bounded_linear f"
1.2993 +  shows "bounded (f ` S)"
1.2994 +proof -
1.2995 +  from assms(1) obtain b where "b > 0" and b: "\<forall>x\<in>S. norm x \<le> b"
1.2996 +    unfolding bounded_pos by auto
1.2997 +  from assms(2) obtain B where B: "B > 0" "\<forall>x. norm (f x) \<le> B * norm x"
1.2998 +    using bounded_linear.pos_bounded by (auto simp: ac_simps)
1.2999 +  show ?thesis
1.3000 +    unfolding bounded_pos
1.3001 +  proof (intro exI, safe)
1.3002 +    show "norm (f x) \<le> B * b" if "x \<in> S" for x
1.3003 +      by (meson B b less_imp_le mult_left_mono order_trans that)
1.3004 +  qed (use \<open>b > 0\<close> \<open>B > 0\<close> in auto)
1.3005 +qed
1.3007 +lemma bounded_scaling:
1.3008 +  fixes S :: "'a::real_normed_vector set"
1.3009 +  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
1.3010 +  apply (rule bounded_linear_image, assumption)
1.3011 +  apply (rule bounded_linear_scaleR_right)
1.3012 +  done
1.3014 +lemma bounded_scaleR_comp:
1.3015 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.3016 +  assumes "bounded (f ` S)"
1.3017 +  shows "bounded ((\<lambda>x. r *\<^sub>R f x) ` S)"
1.3018 +  using bounded_scaling[of "f ` S" r] assms
1.3019 +  by (auto simp: image_image)
1.3021 +lemma bounded_translation:
1.3022 +  fixes S :: "'a::real_normed_vector set"
1.3023 +  assumes "bounded S"
1.3024 +  shows "bounded ((\<lambda>x. a + x) ` S)"
1.3025 +proof -
1.3026 +  from assms obtain b where b: "b > 0" "\<forall>x\<in>S. norm x \<le> b"
1.3027 +    unfolding bounded_pos by auto
1.3028 +  {
1.3029 +    fix x
1.3030 +    assume "x \<in> S"
1.3031 +    then have "norm (a + x) \<le> b + norm a"
1.3032 +      using norm_triangle_ineq[of a x] b by auto
1.3033 +  }
1.3034 +  then show ?thesis
1.3035 +    unfolding bounded_pos
1.3036 +    using norm_ge_zero[of a] b(1) and add_strict_increasing[of b 0 "norm a"]
1.3037 +    by (auto intro!: exI[of _ "b + norm a"])
1.3038 +qed
1.3040 +lemma bounded_translation_minus:
1.3041 +  fixes S :: "'a::real_normed_vector set"
1.3042 +  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. x - a) ` S)"
1.3043 +using bounded_translation [of S "-a"] by simp
1.3045 +lemma bounded_uminus [simp]:
1.3046 +  fixes X :: "'a::real_normed_vector set"
1.3047 +  shows "bounded (uminus ` X) \<longleftrightarrow> bounded X"
1.3048 +by (auto simp: bounded_def dist_norm; rule_tac x="-x" in exI; force simp: add.commute norm_minus_commute)
1.3050 +lemma uminus_bounded_comp [simp]:
1.3051 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
1.3052 +  shows "bounded ((\<lambda>x. - f x) ` S) \<longleftrightarrow> bounded (f ` S)"
1.3053 +  using bounded_uminus[of "f ` S"]
1.3054 +  by (auto simp: image_image)
1.3056 +lemma bounded_plus_comp:
1.3057 +  fixes f g::"'a \<Rightarrow> 'b::real_normed_vector"
1.3058 +  assumes "bounded (f ` S)"
1.3059 +  assumes "bounded (g ` S)"
1.3060 +  shows "bounded ((\<lambda>x. f x + g x) ` S)"
1.3061 +proof -
1.3062 +  {
1.3063 +    fix B C
1.3064 +    assume "\<And>x. x\<in>S \<Longrightarrow> norm (f x) \<le> B" "\<And>x. x\<in>S \<Longrightarrow> norm (g x) \<le> C"
1.3065 +    then have "\<And>x. x \<in> S \<Longrightarrow> norm (f x + g x) \<le> B + C"
1.3066 +      by (auto intro!: norm_triangle_le add_mono)
1.3067 +  } then show ?thesis
1.3068 +    using assms by (fastforce simp: bounded_iff)
1.3069 +qed
1.3071 +lemma bounded_plus:
1.3072 +  fixes S ::"'a::real_normed_vector set"
1.3073 +  assumes "bounded S" "bounded T"
1.3074 +  shows "bounded ((\<lambda>(x,y). x + y) ` (S \<times> T))"
1.3075 +  using bounded_plus_comp [of fst "S \<times> T" snd] assms
1.3076 +  by (auto simp: split_def split: if_split_asm)
1.3078 +lemma bounded_minus_comp:
1.3079 +  "bounded (f ` S) \<Longrightarrow> bounded (g ` S) \<Longrightarrow> bounded ((\<lambda>x. f x - g x) ` S)"
1.3080 +  for f g::"'a \<Rightarrow> 'b::real_normed_vector"
1.3081 +  using bounded_plus_comp[of "f" S "\<lambda>x. - g x"]
1.3082 +  by auto
1.3084 +lemma bounded_minus:
1.3085 +  fixes S ::"'a::real_normed_vector set"
1.3086 +  assumes "bounded S" "bounded T"
1.3087 +  shows "bounded ((\<lambda>(x,y). x - y) ` (S \<times> T))"
1.3088 +  using bounded_minus_comp [of fst "S \<times> T" snd] assms
1.3089 +  by (auto simp: split_def split: if_split_asm)
1.3092 +subsection \<open>Compactness\<close>
1.3094 +subsubsection \<open>Bolzano-Weierstrass property\<close>
1.3096 +proposition heine_borel_imp_bolzano_weierstrass:
1.3097 +  assumes "compact s"
1.3098 +    and "infinite t"
1.3099 +    and "t \<subseteq> s"
1.3100 +  shows "\<exists>x \<in> s. x islimpt t"
1.3101 +proof (rule ccontr)
1.3102 +  assume "\<not> (\<exists>x \<in> s. x islimpt t)"
1.3103 +  then obtain f where f: "\<forall>x\<in>s. x \<in> f x \<and> open (f x) \<and> (\<forall>y\<in>t. y \<in> f x \<longrightarrow> y = x)"
1.3104 +    unfolding islimpt_def
1.3105 +    using bchoice[of s "\<lambda> x T. x \<in> T \<and> open T \<and> (\<forall>y\<in>t. y \<in> T \<longrightarrow> y = x)"]
1.3106 +    by auto
1.3107 +  obtain g where g: "g \<subseteq> {t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
1.3108 +    using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]]
1.3109 +    using f by auto
1.3110 +  from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa"
1.3111 +    by auto
1.3112 +  {
1.3113 +    fix x y
1.3114 +    assume "x \<in> t" "y \<in> t" "f x = f y"
1.3115 +    then have "x \<in> f x"  "y \<in> f x \<longrightarrow> y = x"
1.3116 +      using f[THEN bspec[where x=x]] and \<open>t \<subseteq> s\<close> by auto
1.3117 +    then have "x = y"
1.3118 +      using \<open>f x = f y\<close> and f[THEN bspec[where x=y]] and \<open>y \<in> t\<close> and \<open>t \<subseteq> s\<close>
1.3119 +      by auto
1.3120 +  }
1.3121 +  then have "inj_on f t"
1.3122 +    unfolding inj_on_def by simp
1.3123 +  then have "infinite (f ` t)"
1.3124 +    using assms(2) using finite_imageD by auto
1.3125 +  moreover
1.3126 +  {
1.3127 +    fix x
1.3128 +    assume "x \<in> t" "f x \<notin> g"
1.3129 +    from g(3) assms(3) \<open>x \<in> t\<close> obtain h where "h \<in> g" and "x \<in> h"
1.3130 +      by auto
1.3131 +    then obtain y where "y \<in> s" "h = f y"
1.3132 +      using g'[THEN bspec[where x=h]] by auto
1.3133 +    then have "y = x"
1.3134 +      using f[THEN bspec[where x=y]] and \<open>x\<in>t\<close> and \<open>x\<in>h\<close>[unfolded \<open>h = f y\<close>]
1.3135 +      by auto
1.3136 +    then have False
1.3137 +      using \<open>f x \<notin> g\<close> \<open>h \<in> g\<close> unfolding \<open>h = f y\<close>
1.3138 +      by auto
1.3139 +  }
1.3140 +  then have "f ` t \<subseteq> g" by auto
1.3141 +  ultimately show False
1.3142 +    using g(2) using finite_subset by auto
1.3143 +qed
1.3145 +lemma acc_point_range_imp_convergent_subsequence:
1.3146 +  fixes l :: "'a :: first_countable_topology"
1.3147 +  assumes l: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> range f)"
1.3148 +  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
1.3149 +proof -
1.3150 +  from countable_basis_at_decseq[of l]
1.3151 +  obtain A where A:
1.3152 +      "\<And>i. open (A i)"
1.3153 +      "\<And>i. l \<in> A i"
1.3154 +      "\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
1.3155 +    by blast
1.3156 +  define s where "s n i = (SOME j. i < j \<and> f j \<in> A (Suc n))" for n i
1.3157 +  {
1.3158 +    fix n i
1.3159 +    have "infinite (A (Suc n) \<inter> range f - f`{.. i})"
1.3160 +      using l A by auto
1.3161 +    then have "\<exists>x. x \<in> A (Suc n) \<inter> range f - f`{.. i}"
1.3162 +      unfolding ex_in_conv by (intro notI) simp
1.3163 +    then have "\<exists>j. f j \<in> A (Suc n) \<and> j \<notin> {.. i}"
1.3164 +      by auto
1.3165 +    then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)"
1.3166 +      by (auto simp: not_le)
1.3167 +    then have "i < s n i" "f (s n i) \<in> A (Suc n)"
1.3168 +      unfolding s_def by (auto intro: someI2_ex)
1.3169 +  }
1.3170 +  note s = this
1.3171 +  define r where "r = rec_nat (s 0 0) s"
1.3172 +  have "strict_mono r"
1.3173 +    by (auto simp: r_def s strict_mono_Suc_iff)
1.3174 +  moreover
1.3175 +  have "(\<lambda>n. f (r n)) \<longlonglongrightarrow> l"
1.3176 +  proof (rule topological_tendstoI)
1.3177 +    fix S
1.3178 +    assume "open S" "l \<in> S"
1.3179 +    with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
1.3180 +      by auto
1.3181 +    moreover
1.3182 +    {
1.3183 +      fix i
1.3184 +      assume "Suc 0 \<le> i"
1.3185 +      then have "f (r i) \<in> A i"
1.3186 +        by (cases i) (simp_all add: r_def s)
1.3187 +    }
1.3188 +    then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially"
1.3189 +      by (auto simp: eventually_sequentially)
1.3190 +    ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"
1.3191 +      by eventually_elim auto
1.3192 +  qed
1.3193 +  ultimately show "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
1.3194 +    by (auto simp: convergent_def comp_def)
1.3195 +qed
1.3197 +lemma sequence_infinite_lemma:
1.3198 +  fixes f :: "nat \<Rightarrow> 'a::t1_space"
1.3199 +  assumes "\<forall>n. f n \<noteq> l"
1.3200 +    and "(f \<longlongrightarrow> l) sequentially"
1.3201 +  shows "infinite (range f)"
1.3202 +proof
1.3203 +  assume "finite (range f)"
1.3204 +  then have "closed (range f)"
1.3205 +    by (rule finite_imp_closed)
1.3206 +  then have "open (- range f)"
1.3207 +    by (rule open_Compl)
1.3208 +  from assms(1) have "l \<in> - range f"
1.3209 +    by auto
1.3210 +  from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
1.3211 +    using \<open>open (- range f)\<close> \<open>l \<in> - range f\<close>
1.3212 +    by (rule topological_tendstoD)
1.3213 +  then show False
1.3214 +    unfolding eventually_sequentially
1.3215 +    by auto
1.3216 +qed
1.3218 +lemma closure_insert:
1.3219 +  fixes x :: "'a::t1_space"
1.3220 +  shows "closure (insert x s) = insert x (closure s)"
1.3221 +  apply (rule closure_unique)
1.3222 +  apply (rule insert_mono [OF closure_subset])
1.3223 +  apply (rule closed_insert [OF closed_closure])
1.3224 +  apply (simp add: closure_minimal)
1.3225 +  done
1.3227 +lemma islimpt_insert:
1.3228 +  fixes x :: "'a::t1_space"
1.3229 +  shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
1.3230 +proof
1.3231 +  assume *: "x islimpt (insert a s)"
1.3232 +  show "x islimpt s"
1.3233 +  proof (rule islimptI)
1.3234 +    fix t
1.3235 +    assume t: "x \<in> t" "open t"
1.3236 +    show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
1.3237 +    proof (cases "x = a")
1.3238 +      case True
1.3239 +      obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
1.3240 +        using * t by (rule islimptE)
1.3241 +      with \<open>x = a\<close> show ?thesis by auto
1.3242 +    next
1.3243 +      case False
1.3244 +      with t have t': "x \<in> t - {a}" "open (t - {a})"
1.3245 +        by (simp_all add: open_Diff)
1.3246 +      obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
1.3247 +        using * t' by (rule islimptE)
1.3248 +      then show ?thesis by auto
1.3249 +    qed
1.3250 +  qed
1.3251 +next
1.3252 +  assume "x islimpt s"
1.3253 +  then show "x islimpt (insert a s)"
1.3254 +    by (rule islimpt_subset) auto
1.3255 +qed
1.3257 +lemma islimpt_finite:
1.3258 +  fixes x :: "'a::t1_space"
1.3259 +  shows "finite s \<Longrightarrow> \<not> x islimpt s"
1.3260 +  by (induct set: finite) (simp_all add: islimpt_insert)
1.3262 +lemma islimpt_Un_finite:
1.3263 +  fixes x :: "'a::t1_space"
1.3264 +  shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
1.3265 +  by (simp add: islimpt_Un islimpt_finite)
1.3267 +lemma islimpt_eq_acc_point:
1.3268 +  fixes l :: "'a :: t1_space"
1.3269 +  shows "l islimpt S \<longleftrightarrow> (\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S))"
1.3270 +proof (safe intro!: islimptI)
1.3271 +  fix U
1.3272 +  assume "l islimpt S" "l \<in> U" "open U" "finite (U \<inter> S)"
1.3273 +  then have "l islimpt S" "l \<in> (U - (U \<inter> S - {l}))" "open (U - (U \<inter> S - {l}))"
1.3274 +    by (auto intro: finite_imp_closed)
1.3275 +  then show False
1.3276 +    by (rule islimptE) auto
1.3277 +next
1.3278 +  fix T
1.3279 +  assume *: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S)" "l \<in> T" "open T"
1.3280 +  then have "infinite (T \<inter> S - {l})"
1.3281 +    by auto
1.3282 +  then have "\<exists>x. x \<in> (T \<inter> S - {l})"
1.3283 +    unfolding ex_in_conv by (intro notI) simp
1.3284 +  then show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> l"
1.3285 +    by auto
1.3286 +qed
1.3288 +corollary infinite_openin:
1.3289 +  fixes S :: "'a :: t1_space set"
1.3290 +  shows "\<lbrakk>openin (subtopology euclidean U) S; x \<in> S; x islimpt U\<rbrakk> \<Longrightarrow> infinite S"
1.3291 +  by (clarsimp simp add: openin_open islimpt_eq_acc_point inf_commute)
1.3293 +lemma islimpt_range_imp_convergent_subsequence:
1.3294 +  fixes l :: "'a :: {t1_space, first_countable_topology}"
1.3295 +  assumes l: "l islimpt (range f)"
1.3296 +  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
1.3297 +  using l unfolding islimpt_eq_acc_point
1.3298 +  by (rule acc_point_range_imp_convergent_subsequence)
1.3300 +lemma islimpt_eq_infinite_ball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> ball x e))"
1.3301 +  apply (simp add: islimpt_eq_acc_point, safe)
1.3302 +   apply (metis Int_commute open_ball centre_in_ball)
1.3303 +  by (metis open_contains_ball Int_mono finite_subset inf_commute subset_refl)
1.3305 +lemma islimpt_eq_infinite_cball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> cball x e))"
1.3306 +  apply (simp add: islimpt_eq_infinite_ball, safe)
1.3307 +   apply (meson Int_mono ball_subset_cball finite_subset order_refl)
1.3308 +  by (metis open_ball centre_in_ball finite_Int inf.absorb_iff2 inf_assoc open_contains_cball_eq)
1.3310 +lemma sequence_unique_limpt:
1.3311 +  fixes f :: "nat \<Rightarrow> 'a::t2_space"
1.3312 +  assumes "(f \<longlongrightarrow> l) sequentially"
1.3313 +    and "l' islimpt (range f)"
1.3314 +  shows "l' = l"
1.3315 +proof (rule ccontr)
1.3316 +  assume "l' \<noteq> l"
1.3317 +  obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
1.3318 +    using hausdorff [OF \<open>l' \<noteq> l\<close>] by auto
1.3319 +  have "eventually (\<lambda>n. f n \<in> t) sequentially"
1.3320 +    using assms(1) \<open>open t\<close> \<open>l \<in> t\<close> by (rule topological_tendstoD)
1.3321 +  then obtain N where "\<forall>n\<ge>N. f n \<in> t"
1.3322 +    unfolding eventually_sequentially by auto
1.3324 +  have "UNIV = {..<N} \<union> {N..}"
1.3325 +    by auto
1.3326 +  then have "l' islimpt (f ` ({..<N} \<union> {N..}))"
1.3327 +    using assms(2) by simp
1.3328 +  then have "l' islimpt (f ` {..<N} \<union> f ` {N..})"
1.3329 +    by (simp add: image_Un)
1.3330 +  then have "l' islimpt (f ` {N..})"
1.3331 +    by (simp add: islimpt_Un_finite)
1.3332 +  then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
1.3333 +    using \<open>l' \<in> s\<close> \<open>open s\<close> by (rule islimptE)
1.3334 +  then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'"
1.3335 +    by auto
1.3336 +  with \<open>\<forall>n\<ge>N. f n \<in> t\<close> have "f n \<in> s \<inter> t"
1.3337 +    by simp
1.3338 +  with \<open>s \<inter> t = {}\<close> show False
1.3339 +    by simp
1.3340 +qed
1.3342 +lemma bolzano_weierstrass_imp_closed:
1.3343 +  fixes s :: "'a::{first_countable_topology,t2_space} set"
1.3344 +  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
1.3345 +  shows "closed s"
1.3346 +proof -
1.3347 +  {
1.3348 +    fix x l
1.3349 +    assume as: "\<forall>n::nat. x n \<in> s" "(x \<longlongrightarrow> l) sequentially"
1.3350 +    then have "l \<in> s"
1.3351 +    proof (cases "\<forall>n. x n \<noteq> l")
1.3352 +      case False
1.3353 +      then show "l\<in>s" using as(1) by auto
1.3354 +    next
1.3355 +      case True note cas = this
1.3356 +      with as(2) have "infinite (range x)"
1.3357 +        using sequence_infinite_lemma[of x l] by auto
1.3358 +      then obtain l' where "l'\<in>s" "l' islimpt (range x)"
1.3359 +        using assms[THEN spec[where x="range x"]] as(1) by auto
1.3360 +      then show "l\<in>s" using sequence_unique_limpt[of x l l']
1.3361 +        using as cas by auto
1.3362 +    qed
1.3363 +  }
1.3364 +  then show ?thesis
1.3365 +    unfolding closed_sequential_limits by fast
1.3366 +qed
1.3368 +lemma compact_imp_bounded:
1.3369 +  assumes "compact U"
1.3370 +  shows "bounded U"
1.3371 +proof -
1.3372 +  have "compact U" "\<forall>x\<in>U. open (ball x 1)" "U \<subseteq> (\<Union>x\<in>U. ball x 1)"
1.3373 +    using assms by auto
1.3374 +  then obtain D where D: "D \<subseteq> U" "finite D" "U \<subseteq> (\<Union>x\<in>D. ball x 1)"
1.3375 +    by (metis compactE_image)
1.3376 +  from \<open>finite D\<close> have "bounded (\<Union>x\<in>D. ball x 1)"
1.3377 +    by (simp add: bounded_UN)
1.3378 +  then show "bounded U" using \<open>U \<subseteq> (\<Union>x\<in>D. ball x 1)\<close>
1.3379 +    by (rule bounded_subset)
1.3380 +qed
1.3382 +text\<open>In particular, some common special cases.\<close>
1.3384 +lemma compact_Un [intro]:
1.3385 +  assumes "compact s"
1.3386 +    and "compact t"
1.3387 +  shows " compact (s \<union> t)"
1.3388 +proof (rule compactI)
1.3389 +  fix f
1.3390 +  assume *: "Ball f open" "s \<union> t \<subseteq> \<Union>f"
1.3391 +  from * \<open>compact s\<close> obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"
1.3392 +    unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
1.3393 +  moreover
1.3394 +  from * \<open>compact t\<close> obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"
1.3395 +    unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
1.3396 +  ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"
1.3397 +    by (auto intro!: exI[of _ "s' \<union> t'"])
1.3398 +qed
1.3400 +lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"
1.3401 +  by (induct set: finite) auto
1.3403 +lemma compact_UN [intro]:
1.3404 +  "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"
1.3405 +  by (rule compact_Union) auto
1.3407 +lemma closed_Int_compact [intro]:
1.3408 +  assumes "closed s"
1.3409 +    and "compact t"
1.3410 +  shows "compact (s \<inter> t)"
1.3411 +  using compact_Int_closed [of t s] assms
1.3412 +  by (simp add: Int_commute)
1.3414 +lemma compact_Int [intro]:
1.3415 +  fixes s t :: "'a :: t2_space set"
1.3416 +  assumes "compact s"
1.3417 +    and "compact t"
1.3418 +  shows "compact (s \<inter> t)"
1.3419 +  using assms by (intro compact_Int_closed compact_imp_closed)
1.3421 +lemma compact_sing [simp]: "compact {a}"
1.3422 +  unfolding compact_eq_heine_borel by auto
1.3424 +lemma compact_insert [simp]:
1.3425 +  assumes "compact s"
1.3426 +  shows "compact (insert x s)"
1.3427 +proof -
1.3428 +  have "compact ({x} \<union> s)"
1.3429 +    using compact_sing assms by (rule compact_Un)
1.3430 +  then show ?thesis by simp
1.3431 +qed
1.3433 +lemma finite_imp_compact: "finite s \<Longrightarrow> compact s"
1.3434 +  by (induct set: finite) simp_all
1.3436 +lemma open_delete:
1.3437 +  fixes s :: "'a::t1_space set"
1.3438 +  shows "open s \<Longrightarrow> open (s - {x})"
1.3439 +  by (simp add: open_Diff)
1.3441 +lemma openin_delete:
1.3442 +  fixes a :: "'a :: t1_space"
1.3443 +  shows "openin (subtopology euclidean u) s
1.3444 +         \<Longrightarrow> openin (subtopology euclidean u) (s - {a})"
1.3445 +by (metis Int_Diff open_delete openin_open)
1.3447 +text\<open>Compactness expressed with filters\<close>
1.3449 +lemma closure_iff_nhds_not_empty:
1.3450 +  "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"
1.3451 +proof safe
1.3452 +  assume x: "x \<in> closure X"
1.3453 +  fix S A
1.3454 +  assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"
1.3455 +  then have "x \<notin> closure (-S)"
1.3456 +    by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)
1.3457 +  with x have "x \<in> closure X - closure (-S)"
1.3458 +    by auto
1.3459 +  also have "\<dots> \<subseteq> closure (X \<inter> S)"
1.3460 +    using \<open>open S\<close> open_Int_closure_subset[of S X] by (simp add: closed_Compl ac_simps)
1.3461 +  finally have "X \<inter> S \<noteq> {}" by auto
1.3462 +  then show False using \<open>X \<inter> A = {}\<close> \<open>S \<subseteq> A\<close> by auto
1.3463 +next
1.3464 +  assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"
1.3465 +  from this[THEN spec, of "- X", THEN spec, of "- closure X"]
1.3466 +  show "x \<in> closure X"
1.3467 +    by (simp add: closure_subset open_Compl)
1.3468 +qed
1.3470 +corollary closure_Int_ball_not_empty:
1.3471 +  assumes "S \<subseteq> closure T" "x \<in> S" "r > 0"
1.3472 +  shows "T \<inter> ball x r \<noteq> {}"
1.3473 +  using assms centre_in_ball closure_iff_nhds_not_empty by blast
1.3475 +lemma compact_filter:
1.3476 +  "compact U \<longleftrightarrow> (\<forall>F. F \<noteq> bot \<longrightarrow> eventually (\<lambda>x. x \<in> U) F \<longrightarrow> (\<exists>x\<in>U. inf (nhds x) F \<noteq> bot))"
1.3477 +proof (intro allI iffI impI compact_fip[THEN iffD2] notI)
1.3478 +  fix F
1.3479 +  assume "compact U"
1.3480 +  assume F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"
1.3481 +  then have "U \<noteq> {}"
1.3482 +    by (auto simp: eventually_False)
1.3484 +  define Z where "Z = closure ` {A. eventually (\<lambda>x. x \<in> A) F}"
1.3485 +  then have "\<forall>z\<in>Z. closed z"
1.3486 +    by auto
1.3487 +  moreover
1.3488 +  have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"
1.3489 +    unfolding Z_def by (auto elim: eventually_mono intro: set_mp[OF closure_subset])
1.3490 +  have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"
1.3491 +  proof (intro allI impI)
1.3492 +    fix B assume "finite B" "B \<subseteq> Z"
1.3493 +    with \<open>finite B\<close> ev_Z F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"
1.3494 +      by (auto simp: eventually_ball_finite_distrib eventually_conj_iff)
1.3495 +    with F show "U \<inter> \<Inter>B \<noteq> {}"
1.3496 +      by (intro notI) (simp add: eventually_False)
1.3497 +  qed
1.3498 +  ultimately have "U \<inter> \<Inter>Z \<noteq> {}"
1.3499 +    using \<open>compact U\<close> unfolding compact_fip by blast
1.3500 +  then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z"
1.3501 +    by auto
1.3503 +  have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"
1.3504 +    unfolding eventually_inf eventually_nhds
1.3505 +  proof safe
1.3506 +    fix P Q R S
1.3507 +    assume "eventually R F" "open S" "x \<in> S"
1.3508 +    with open_Int_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]
1.3509 +    have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)
1.3510 +    moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"
1.3511 +    ultimately show False by (auto simp: set_eq_iff)
1.3512 +  qed
1.3513 +  with \<open>x \<in> U\<close> show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"
1.3514 +    by (metis eventually_bot)
1.3515 +next
1.3516 +  fix A
1.3517 +  assume A: "\<forall>a\<in>A. closed a" "\<forall>B\<subseteq>A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}" "U \<inter> \<Inter>A = {}"
1.3518 +  define F where "F = (INF a\<in>insert U A. principal a)"
1.3519 +  have "F \<noteq> bot"
1.3520 +    unfolding F_def
1.3521 +  proof (rule INF_filter_not_bot)
1.3522 +    fix X
1.3523 +    assume X: "X \<subseteq> insert U A" "finite X"
1.3524 +    with A(2)[THEN spec, of "X - {U}"] have "U \<inter> \<Inter>(X - {U}) \<noteq> {}"
1.3525 +      by auto
1.3526 +    with X show "(INF a\<in>X. principal a) \<noteq> bot"
1.3527 +      by (auto simp: INF_principal_finite principal_eq_bot_iff)
1.3528 +  qed
1.3529 +  moreover
1.3530 +  have "F \<le> principal U"
1.3531 +    unfolding F_def by auto
1.3532 +  then have "eventually (\<lambda>x. x \<in> U) F"
1.3533 +    by (auto simp: le_filter_def eventually_principal)
1.3534 +  moreover
1.3535 +  assume "\<forall>F. F \<noteq> bot \<longrightarrow> eventually (\<lambda>x. x \<in> U) F \<longrightarrow> (\<exists>x\<in>U. inf (nhds x) F \<noteq> bot)"
1.3536 +  ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"
1.3537 +    by auto
1.3539 +  { fix V assume "V \<in> A"
1.3540 +    then have "F \<le> principal V"
1.3541 +      unfolding F_def by (intro INF_lower2[of V]) auto
1.3542 +    then have V: "eventually (\<lambda>x. x \<in> V) F"
1.3543 +      by (auto simp: le_filter_def eventually_principal)
1.3544 +    have "x \<in> closure V"
1.3545 +      unfolding closure_iff_nhds_not_empty
1.3546 +    proof (intro impI allI)
1.3547 +      fix S A
1.3548 +      assume "open S" "x \<in> S" "S \<subseteq> A"
1.3549 +      then have "eventually (\<lambda>x. x \<in> A) (nhds x)"
1.3550 +        by (auto simp: eventually_nhds)
1.3551 +      with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"
1.3552 +        by (auto simp: eventually_inf)
1.3553 +      with x show "V \<inter> A \<noteq> {}"
1.3554 +        by (auto simp del: Int_iff simp add: trivial_limit_def)
1.3555 +    qed
1.3556 +    then have "x \<in> V"
1.3557 +      using \<open>V \<in> A\<close> A(1) by simp
1.3558 +  }
1.3559 +  with \<open>x\<in>U\<close> have "x \<in> U \<inter> \<Inter>A" by auto
1.3560 +  with \<open>U \<inter> \<Inter>A = {}\<close> show False by auto
1.3561 +qed
1.3563 +definition%important "countably_compact U \<longleftrightarrow>
1.3564 +    (\<forall>A. countable A \<longrightarrow> (\<forall>a\<in>A. open a) \<longrightarrow> U \<subseteq> \<Union>A \<longrightarrow> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T))"
1.3566 +lemma countably_compactE:
1.3567 +  assumes "countably_compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C" "countable C"
1.3568 +  obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
1.3569 +  using assms unfolding countably_compact_def by metis
1.3571 +lemma countably_compactI:
1.3572 +  assumes "\<And>C. \<forall>t\<in>C. open t \<Longrightarrow> s \<subseteq> \<Union>C \<Longrightarrow> countable C \<Longrightarrow> (\<exists>C'\<subseteq>C. finite C' \<and> s \<subseteq> \<Union>C')"
1.3573 +  shows "countably_compact s"
1.3574 +  using assms unfolding countably_compact_def by metis
1.3576 +lemma compact_imp_countably_compact: "compact U \<Longrightarrow> countably_compact U"
1.3577 +  by (auto simp: compact_eq_heine_borel countably_compact_def)
1.3579 +lemma countably_compact_imp_compact:
1.3580 +  assumes "countably_compact U"
1.3581 +    and ccover: "countable B" "\<forall>b\<in>B. open b"
1.3582 +    and basis: "\<And>T x. open T \<Longrightarrow> x \<in> T \<Longrightarrow> x \<in> U \<Longrightarrow> \<exists>b\<in>B. x \<in> b \<and> b \<inter> U \<subseteq> T"
1.3583 +  shows "compact U"
1.3584 +  using \<open>countably_compact U\<close>
1.3585 +  unfolding compact_eq_heine_borel countably_compact_def
1.3586 +proof safe
1.3587 +  fix A
1.3588 +  assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
1.3589 +  assume *: "\<forall>A. countable A \<longrightarrow> (\<forall>a\<in>A. open a) \<longrightarrow> U \<subseteq> \<Union>A \<longrightarrow> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
1.3590 +  moreover define C where "C = {b\<in>B. \<exists>a\<in>A. b \<inter> U \<subseteq> a}"
1.3591 +  ultimately have "countable C" "\<forall>a\<in>C. open a"
1.3592 +    unfolding C_def using ccover by auto
1.3593 +  moreover
1.3594 +  have "\<Union>A \<inter> U \<subseteq> \<Union>C"
1.3595 +  proof safe
1.3596 +    fix x a
1.3597 +    assume "x \<in> U" "x \<in> a" "a \<in> A"
1.3598 +    with basis[of a x] A obtain b where "b \<in> B" "x \<in> b" "b \<inter> U \<subseteq> a"
1.3599 +      by blast
1.3600 +    with \<open>a \<in> A\<close> show "x \<in> \<Union>C"
1.3601 +      unfolding C_def by auto
1.3602 +  qed
1.3603 +  then have "U \<subseteq> \<Union>C" using \<open>U \<subseteq> \<Union>A\<close> by auto
1.3604 +  ultimately obtain T where T: "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"
1.3605 +    using * by metis
1.3606 +  then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<inter> U \<subseteq> a"
1.3607 +    by (auto simp: C_def)
1.3608 +  then obtain f where "\<forall>t\<in>T. f t \<in> A \<and> t \<inter> U \<subseteq> f t"
1.3609 +    unfolding bchoice_iff Bex_def ..
1.3610 +  with T show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
1.3611 +    unfolding C_def by (intro exI[of _ "f`T"]) fastforce
1.3612 +qed
1.3614 +proposition countably_compact_imp_compact_second_countable:
1.3615 +  "countably_compact U \<Longrightarrow> compact (U :: 'a :: second_countable_topology set)"
1.3616 +proof (rule countably_compact_imp_compact)
1.3617 +  fix T and x :: 'a
1.3618 +  assume "open T" "x \<in> T"
1.3619 +  from topological_basisE[OF is_basis this] obtain b where
1.3620 +    "b \<in> (SOME B. countable B \<and> topological_basis B)" "x \<in> b" "b \<subseteq> T" .
1.3621 +  then show "\<exists>b\<in>SOME B. countable B \<and> topological_basis B. x \<in> b \<and> b \<inter> U \<subseteq> T"
1.3622 +    by blast
1.3623 +qed (insert countable_basis topological_basis_open[OF is_basis], auto)
1.3625 +lemma countably_compact_eq_compact:
1.3626 +  "countably_compact U \<longleftrightarrow> compact (U :: 'a :: second_countable_topology set)"
1.3627 +  using countably_compact_imp_compact_second_countable compact_imp_countably_compact by blast
1.3629 +subsubsection\<open>Sequential compactness\<close>
1.3631 +definition%important seq_compact :: "'a::topological_space set \<Rightarrow> bool"
1.3632 +  where "seq_compact S \<longleftrightarrow>
1.3633 +    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially))"
1.3635 +lemma seq_compactI:
1.3636 +  assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
1.3637 +  shows "seq_compact S"
1.3638 +  unfolding seq_compact_def using assms by fast
1.3640 +lemma seq_compactE:
1.3641 +  assumes "seq_compact S" "\<forall>n. f n \<in> S"
1.3642 +  obtains l r where "l \<in> S" "strict_mono (r :: nat \<Rightarrow> nat)" "((f \<circ> r) \<longlongrightarrow> l) sequentially"
1.3643 +  using assms unfolding seq_compact_def by fast
1.3645 +lemma closed_sequentially: (* TODO: move upwards *)
1.3646 +  assumes "closed s" and "\<forall>n. f n \<in> s" and "f \<longlonglongrightarrow> l"
1.3647 +  shows "l \<in> s"
1.3648 +proof (rule ccontr)
1.3649 +  assume "l \<notin> s"
1.3650 +  with \<open>closed s\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "eventually (\<lambda>n. f n \<in> - s) sequentially"
1.3651 +    by (fast intro: topological_tendstoD)
1.3652 +  with \<open>\<forall>n. f n \<in> s\<close> show "False"
1.3653 +    by simp
1.3654 +qed
1.3656 +lemma seq_compact_Int_closed:
1.3657 +  assumes "seq_compact s" and "closed t"
1.3658 +  shows "seq_compact (s \<inter> t)"
1.3659 +proof (rule seq_compactI)
1.3660 +  fix f assume "\<forall>n::nat. f n \<in> s \<inter> t"
1.3661 +  hence "\<forall>n. f n \<in> s" and "\<forall>n. f n \<in> t"
1.3662 +    by simp_all
1.3663 +  from \<open>seq_compact s\<close> and \<open>\<forall>n. f n \<in> s\<close>
1.3664 +  obtain l r where "l \<in> s" and r: "strict_mono r" and l: "(f \<circ> r) \<longlonglongrightarrow> l"
1.3665 +    by (rule seq_compactE)
1.3666 +  from \<open>\<forall>n. f n \<in> t\<close> have "\<forall>n. (f \<circ> r) n \<in> t"
1.3667 +    by simp
1.3668 +  from \<open>closed t\<close> and this and l have "l \<in> t"
1.3669 +    by (rule closed_sequentially)
1.3670 +  with \<open>l \<in> s\<close> and r and l show "\<exists>l\<in>s \<inter> t. \<exists>r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
1.3671 +    by fast
1.3672 +qed
1.3674 +lemma seq_compact_closed_subset:
1.3675 +  assumes "closed s" and "s \<subseteq> t" and "seq_compact t"
1.3676 +  shows "seq_compact s"
1.3677 +  using assms seq_compact_Int_closed [of t s] by (simp add: Int_absorb1)
1.3679 +lemma seq_compact_imp_countably_compact:
1.3680 +  fixes U :: "'a :: first_countable_topology set"
1.3681 +  assumes "seq_compact U"
1.3682 +  shows "countably_compact U"
1.3683 +proof (safe intro!: countably_compactI)
1.3684 +  fix A
1.3685 +  assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"
1.3686 +  have subseq: "\<And>X. range X \<subseteq> U \<Longrightarrow> \<exists>r x. x \<in> U \<and> strict_mono (r :: nat \<Rightarrow> nat) \<and> (X \<circ> r) \<longlonglongrightarrow> x"
1.3687 +    using \<open>seq_compact U\<close> by (fastforce simp: seq_compact_def subset_eq)
1.3688 +  show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
1.3689 +  proof cases
1.3690 +    assume "finite A"
1.3691 +    with A show ?thesis by auto
1.3692 +  next
1.3693 +    assume "infinite A"
1.3694 +    then have "A \<noteq> {}" by auto
1.3695 +    show ?thesis
1.3696 +    proof (rule ccontr)
1.3697 +      assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
1.3698 +      then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)"
1.3699 +        by auto
1.3700 +      then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T"
1.3701 +        by metis
1.3702 +      define X where "X n = X' (from_nat_into A ` {.. n})" for n
1.3703 +      have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"
1.3704 +        using \<open>A \<noteq> {}\<close> unfolding X_def by (intro T) (auto intro: from_nat_into)
1.3705 +      then have "range X \<subseteq> U"
1.3706 +        by auto
1.3707 +      with subseq[of X] obtain r x where "x \<in> U" and r: "strict_mono r" "(X \<circ> r) \<longlonglongrightarrow> x"
1.3708 +        by auto
1.3709 +      from \<open>x\<in>U\<close> \<open>U \<subseteq> \<Union>A\<close> from_nat_into_surj[OF \<open>countable A\<close>]
1.3710 +      obtain n where "x \<in> from_nat_into A n" by auto
1.3711 +      with r(2) A(1) from_nat_into[OF \<open>A \<noteq> {}\<close>, of n]
1.3712 +      have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"
1.3713 +        unfolding tendsto_def by (auto simp: comp_def)
1.3714 +      then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"
1.3715 +        by (auto simp: eventually_sequentially)
1.3716 +      moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"
1.3717 +        by auto
1.3718 +      moreover from \<open>strict_mono r\<close>[THEN seq_suble, of "max n N"] have "\<exists>i. n \<le> r i \<and> N \<le> i"
1.3719 +        by (auto intro!: exI[of _ "max n N"])
1.3720 +      ultimately show False
1.3721 +        by auto
1.3722 +    qed
1.3723 +  qed
1.3724 +qed
1.3726 +lemma compact_imp_seq_compact:
1.3727 +  fixes U :: "'a :: first_countable_topology set"
1.3728 +  assumes "compact U"
1.3729 +  shows "seq_compact U"
1.3730 +  unfolding seq_compact_def
1.3731 +proof safe
1.3732 +  fix X :: "nat \<Rightarrow> 'a"
1.3733 +  assume "\<forall>n. X n \<in> U"
1.3734 +  then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"
1.3735 +    by (auto simp: eventually_filtermap)
1.3736 +  moreover
1.3737 +  have "filtermap X sequentially \<noteq> bot"
1.3738 +    by (simp add: trivial_limit_def eventually_filtermap)
1.3739 +  ultimately
1.3740 +  obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")
1.3741 +    using \<open>compact U\<close> by (auto simp: compact_filter)
1.3743 +  from countable_basis_at_decseq[of x]
1.3744 +  obtain A where A:
1.3745 +      "\<And>i. open (A i)"
1.3746 +      "\<And>i. x \<in> A i"
1.3747 +      "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
1.3748 +    by blast
1.3749 +  define s where "s n i = (SOME j. i < j \<and> X j \<in> A (Suc n))" for n i
1.3750 +  {
1.3751 +    fix n i
1.3752 +    have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"
1.3753 +    proof (rule ccontr)
1.3754 +      assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"
1.3755 +      then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)"
1.3756 +        by auto
1.3757 +      then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"
1.3758 +        by (auto simp: eventually_filtermap eventually_sequentially)
1.3759 +      moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"
1.3760 +        using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)
1.3761 +      ultimately have "eventually (\<lambda>x. False) ?F"
1.3762 +        by (auto simp: eventually_inf)
1.3763 +      with x show False
1.3764 +        by (simp add: eventually_False)
1.3765 +    qed
1.3766 +    then have "i < s n i" "X (s n i) \<in> A (Suc n)"
1.3767 +      unfolding s_def by (auto intro: someI2_ex)
1.3768 +  }
1.3769 +  note s = this
1.3770 +  define r where "r = rec_nat (s 0 0) s"
1.3771 +  have "strict_mono r"
1.3772 +    by (auto simp: r_def s strict_mono_Suc_iff)
1.3773 +  moreover
1.3774 +  have "(\<lambda>n. X (r n)) \<longlonglongrightarrow> x"
1.3775 +  proof (rule topological_tendstoI)
1.3776 +    fix S
1.3777 +    assume "open S" "x \<in> S"
1.3778 +    with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
1.3779 +      by auto
1.3780 +    moreover
1.3781 +    {
1.3782 +      fix i
1.3783 +      assume "Suc 0 \<le> i"
1.3784 +      then have "X (r i) \<in> A i"
1.3785 +        by (cases i) (simp_all add: r_def s)
1.3786 +    }
1.3787 +    then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially"
1.3788 +      by (auto simp: eventually_sequentially)
1.3789 +    ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"
1.3790 +      by eventually_elim auto
1.3791 +  qed
1.3792 +  ultimately show "\<exists>x \<in> U. \<exists>r. strict_mono r \<and> (X \<circ> r) \<longlonglongrightarrow> x"
1.3793 +    using \<open>x \<in> U\<close> by (auto simp: convergent_def comp_def)
1.3794 +qed
1.3796 +lemma countably_compact_imp_acc_point:
1.3797 +  assumes "countably_compact s"
1.3798 +    and "countable t"
1.3799 +    and "infinite t"
1.3800 +    and "t \<subseteq> s"
1.3801 +  shows "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)"
1.3802 +proof (rule ccontr)
1.3803 +  define C where "C = (\<lambda>F. interior (F \<union> (- t))) ` {F. finite F \<and> F \<subseteq> t }"
1.3804 +  note \<open>countably_compact s\<close>
1.3805 +  moreover have "\<forall>t\<in>C. open t"
1.3806 +    by (auto simp: C_def)
1.3807 +  moreover
1.3808 +  assume "\<not> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
1.3809 +  then have s: "\<And>x. x \<in> s \<Longrightarrow> \<exists>U. x\<in>U \<and> open U \<and> finite (U \<inter> t)" by metis
1.3810 +  have "s \<subseteq> \<Union>C"
1.3811 +    using \<open>t \<subseteq> s\<close>
1.3812 +    unfolding C_def
1.3813 +    apply (safe dest!: s)
1.3814 +    apply (rule_tac a="U \<inter> t" in UN_I)
1.3815 +    apply (auto intro!: interiorI simp add: finite_subset)
1.3816 +    done
1.3817 +  moreover
1.3818 +  from \<open>countable t\<close> have "countable C"
1.3819 +    unfolding C_def by (auto intro: countable_Collect_finite_subset)
1.3820 +  ultimately
1.3821 +  obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> \<Union>D"
1.3822 +    by (rule countably_compactE)
1.3823 +  then obtain E where E: "E \<subseteq> {F. finite F \<and> F \<subseteq> t }" "finite E"
1.3824 +    and s: "s \<subseteq> (\<Union>F\<in>E. interior (F \<union> (- t)))"
1.3825 +    by (metis (lifting) finite_subset_image C_def)
1.3826 +  from s \<open>t \<subseteq> s\<close> have "t \<subseteq> \<Union>E"
1.3827 +    using interior_subset by blast
1.3828 +  moreover have "finite (\<Union>E)"
1.3829 +    using E by auto
1.3830 +  ultimately show False using \<open>infinite t\<close>
1.3831 +    by (auto simp: finite_subset)
1.3832 +qed
1.3834 +lemma countable_acc_point_imp_seq_compact:
1.3835 +  fixes s :: "'a::first_countable_topology set"
1.3836 +  assumes "\<forall>t. infinite t \<and> countable t \<and> t \<subseteq> s \<longrightarrow>
1.3837 +    (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
1.3838 +  shows "seq_compact s"
1.3839 +proof -
1.3840 +  {
1.3841 +    fix f :: "nat \<Rightarrow> 'a"
1.3842 +    assume f: "\<forall>n. f n \<in> s"
1.3843 +    have "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
1.3844 +    proof (cases "finite (range f)")
1.3845 +      case True
1.3846 +      obtain l where "infinite {n. f n = f l}"
1.3847 +        using pigeonhole_infinite[OF _ True] by auto
1.3848 +      then obtain r :: "nat \<Rightarrow> nat" where "strict_mono  r" and fr: "\<forall>n. f (r n) = f l"
1.3849 +        using infinite_enumerate by blast
1.3850 +      then have "strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> f l"
1.3851 +        by (simp add: fr o_def)
1.3852 +      with f show "\<exists>l\<in>s. \<exists>r. strict_mono  r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
1.3853 +        by auto
1.3854 +    next
1.3855 +      case False
1.3856 +      with f assms have "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)"
1.3857 +        by auto
1.3858 +      then obtain l where "l \<in> s" "\<forall>U. l\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" ..
1.3859 +      from this(2) have "\<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
1.3860 +        using acc_point_range_imp_convergent_subsequence[of l f] by auto
1.3861 +      with \<open>l \<in> s\<close> show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially" ..
1.3862 +    qed
1.3863 +  }
1.3864 +  then show ?thesis
1.3865 +    unfolding seq_compact_def by auto
1.3866 +qed
1.3868 +lemma seq_compact_eq_countably_compact:
1.3869 +  fixes U :: "'a :: first_countable_topology set"
1.3870 +  shows "seq_compact U \<longleftrightarrow> countably_compact U"
1.3871 +  using
1.3872 +    countable_acc_point_imp_seq_compact
1.3873 +    countably_compact_imp_acc_point
1.3874 +    seq_compact_imp_countably_compact
1.3875 +  by metis
1.3877 +lemma seq_compact_eq_acc_point:
1.3878 +  fixes s :: "'a :: first_countable_topology set"
1.3879 +  shows "seq_compact s \<longleftrightarrow>
1.3880 +    (\<forall>t. infinite t \<and> countable t \<and> t \<subseteq> s --> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)))"
1.3881 +  using
1.3882 +    countable_acc_point_imp_seq_compact[of s]
1.3883 +    countably_compact_imp_acc_point[of s]
1.3884 +    seq_compact_imp_countably_compact[of s]
1.3885 +  by metis
1.3887 +lemma seq_compact_eq_compact:
1.3888 +  fixes U :: "'a :: second_countable_topology set"
1.3889 +  shows "seq_compact U \<longleftrightarrow> compact U"
1.3890 +  using seq_compact_eq_countably_compact countably_compact_eq_compact by blast
1.3892 +proposition bolzano_weierstrass_imp_seq_compact:
1.3893 +  fixes s :: "'a::{t1_space, first_countable_topology} set"
1.3894 +  shows "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> seq_compact s"
1.3895 +  by (rule countable_acc_point_imp_seq_compact) (metis islimpt_eq_acc_point)
1.3898 +subsubsection\<open>Totally bounded\<close>
1.3900 +lemma cauchy_def: "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N \<longrightarrow> dist (s m) (s n) < e)"
1.3901 +  unfolding Cauchy_def by metis
1.3903 +proposition seq_compact_imp_totally_bounded:
1.3904 +  assumes "seq_compact s"
1.3905 +  shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>x\<in>k. ball x e)"
1.3906 +proof -
1.3907 +  { fix e::real assume "e > 0" assume *: "\<And>k. finite k \<Longrightarrow> k \<subseteq> s \<Longrightarrow> \<not> s \<subseteq> (\<Union>x\<in>k. ball x e)"
1.3908 +    let ?Q = "\<lambda>x n r. r \<in> s \<and> (\<forall>m < (n::nat). \<not> (dist (x m) r < e))"
1.3909 +    have "\<exists>x. \<forall>n::nat. ?Q x n (x n)"
1.3910 +    proof (rule dependent_wellorder_choice)
1.3911 +      fix n x assume "\<And>y. y < n \<Longrightarrow> ?Q x y (x y)"
1.3912 +      then have "\<not> s \<subseteq> (\<Union>x\<in>x ` {0..<n}. ball x e)"
1.3913 +        using *[of "x ` {0 ..< n}"] by (auto simp: subset_eq)
1.3914 +      then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)"
1.3915 +        unfolding subset_eq by auto
1.3916 +      show "\<exists>r. ?Q x n r"
1.3917 +        using z by auto
1.3918 +    qed simp
1.3919 +    then obtain x where "\<forall>n::nat. x n \<in> s" and x:"\<And>n m. m < n \<Longrightarrow> \<not> (dist (x m) (x n) < e)"
1.3920 +      by blast
1.3921 +    then obtain l r where "l \<in> s" and r:"strict_mono  r" and "((x \<circ> r) \<longlongrightarrow> l) sequentially"
1.3922 +      using assms by (metis seq_compact_def)
1.3923 +    from this(3) have "Cauchy (x \<circ> r)"
1.3924 +      using LIMSEQ_imp_Cauchy by auto
1.3925 +    then obtain N::nat where "\<And>m n. N \<le> m \<Longrightarrow> N \<le> n \<Longrightarrow> dist ((x \<circ> r) m) ((x \<circ> r) n) < e"
1.3926 +      unfolding cauchy_def using \<open>e > 0\<close> by blast
1.3927 +    then have False
1.3928 +      using x[of "r N" "r (N+1)"] r by (auto simp: strict_mono_def) }
1.3929 +  then show ?thesis
1.3930 +    by metis
1.3931 +qed
1.3933 +subsubsection\<open>Heine-Borel theorem\<close>
1.3935 +proposition seq_compact_imp_heine_borel:
1.3936 +  fixes s :: "'a :: metric_space set"
1.3937 +  assumes "seq_compact s"
1.3938 +  shows "compact s"
1.3939 +proof -
1.3940 +  from seq_compact_imp_totally_bounded[OF \<open>seq_compact s\<close>]
1.3941 +  obtain f where f: "\<forall>e>0. finite (f e) \<and> f e \<subseteq> s \<and> s \<subseteq> (\<Union>x\<in>f e. ball x e)"
1.3942 +    unfolding choice_iff' ..
1.3943 +  define K where "K = (\<lambda>(x, r). ball x r) ` ((\<Union>e \<in> \<rat> \<inter> {0 <..}. f e) \<times> \<rat>)"
1.3944 +  have "countably_compact s"
1.3945 +    using \<open>seq_compact s\<close> by (rule seq_compact_imp_countably_compact)
1.3946 +  then show "compact s"
1.3947 +  proof (rule countably_compact_imp_compact)
1.3948 +    show "countable K"
1.3949 +      unfolding K_def using f
1.3950 +      by (auto intro: countable_finite countable_subset countable_rat
1.3951 +               intro!: countable_image countable_SIGMA countable_UN)
1.3952 +    show "\<forall>b\<in>K. open b" by (auto simp: K_def)
1.3953 +  next
1.3954 +    fix T x
1.3955 +    assume T: "open T" "x \<in> T" and x: "x \<in> s"
1.3956 +    from openE[OF T] obtain e where "0 < e" "ball x e \<subseteq> T"
1.3957 +      by auto
1.3958 +    then have "0 < e / 2" "ball x (e / 2) \<subseteq> T"
1.3959 +      by auto
1.3960 +    from Rats_dense_in_real[OF \<open>0 < e / 2\<close>] obtain r where "r \<in> \<rat>" "0 < r" "r < e / 2"
1.3961 +      by auto
1.3962 +    from f[rule_format, of r] \<open>0 < r\<close> \<open>x \<in> s\<close> obtain k where "k \<in> f r" "x \<in> ball k r"
1.3963 +      by auto
1.3964 +    from \<open>r \<in> \<rat>\<close> \<open>0 < r\<close> \<open>k \<in> f r\<close> have "ball k r \<in> K"
1.3965 +      by (auto simp: K_def)
1.3966 +    then show "\<exists>b\<in>K. x \<in> b \<and> b \<inter> s \<subseteq> T"
1.3967 +    proof (rule bexI[rotated], safe)
1.3968 +      fix y
1.3969 +      assume "y \<in> ball k r"
1.3970 +      with \<open>r < e / 2\<close> \<open>x \<in> ball k r\<close> have "dist x y < e"
1.3971 +        by (intro dist_triangle_half_r [of k _ e]) (auto simp: dist_commute)
1.3972 +      with \<open>ball x e \<subseteq> T\<close> show "y \<in> T"
1.3973 +        by auto
1.3974 +    next
1.3975 +      show "x \<in> ball k r" by fact
1.3976 +    qed
1.3977 +  qed
1.3978 +qed
1.3980 +proposition compact_eq_seq_compact_metric:
1.3981 +  "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"
1.3982 +  using compact_imp_seq_compact seq_compact_imp_heine_borel by blast
1.3984 +proposition compact_def: \<comment> \<open>this is the definition of compactness in HOL Light\<close>
1.3985 +  "compact (S :: 'a::metric_space set) \<longleftrightarrow>
1.3986 +   (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l))"
1.3987 +  unfolding compact_eq_seq_compact_metric seq_compact_def by auto
1.3989 +subsubsection \<open>Complete the chain of compactness variants\<close>
1.3991 +proposition compact_eq_bolzano_weierstrass:
1.3992 +  fixes s :: "'a::metric_space set"
1.3993 +  shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))"
1.3994 +  (is "?lhs = ?rhs")
1.3995 +proof
1.3996 +  assume ?lhs
1.3997 +  then show ?rhs
1.3998 +    using heine_borel_imp_bolzano_weierstrass[of s] by auto
1.3999 +next
1.4000 +  assume ?rhs
1.4001 +  then show ?lhs
1.4002 +    unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)
1.4003 +qed
1.4005 +proposition bolzano_weierstrass_imp_bounded:
1.4006 +  "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> bounded s"
1.4007 +  using compact_imp_bounded unfolding compact_eq_bolzano_weierstrass .
1.4010 +subsection \<open>Metric spaces with the Heine-Borel property\<close>
1.4012 +text \<open>
1.4013 +  A metric space (or topological vector space) is said to have the
1.4014 +  Heine-Borel property if every closed and bounded subset is compact.
1.4015 +\<close>
1.4017 +class heine_borel = metric_space +
1.4018 +  assumes bounded_imp_convergent_subsequence:
1.4019 +    "bounded (range f) \<Longrightarrow> \<exists>l r. strict_mono (r::nat\<Rightarrow>nat) \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
1.4021 +proposition bounded_closed_imp_seq_compact:
1.4022 +  fixes s::"'a::heine_borel set"
1.4023 +  assumes "bounded s"
1.4024 +    and "closed s"
1.4025 +  shows "seq_compact s"
1.4026 +proof (unfold seq_compact_def, clarify)
1.4027 +  fix f :: "nat \<Rightarrow> 'a"
1.4028 +  assume f: "\<forall>n. f n \<in> s"
1.4029 +  with \<open>bounded s\<close> have "bounded (range f)"
1.4030 +    by (auto intro: bounded_subset)
1.4031 +  obtain l r where r: "strict_mono (r :: nat \<Rightarrow> nat)" and l: "((f \<circ> r) \<longlongrightarrow> l) sequentially"
1.4032 +    using bounded_imp_convergent_subsequence [OF \<open>bounded (range f)\<close>] by auto
1.4033 +  from f have fr: "\<forall>n. (f \<circ> r) n \<in> s"
1.4034 +    by simp
1.4035 +  have "l \<in> s" using \<open>closed s\<close> fr l
1.4036 +    by (rule closed_sequentially)
1.4037 +  show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
1.4038 +    using \<open>l \<in> s\<close> r l by blast
1.4039 +qed
1.4041 +lemma compact_eq_bounded_closed:
1.4042 +  fixes s :: "'a::heine_borel set"
1.4043 +  shows "compact s \<longleftrightarrow> bounded s \<and> closed s"
1.4044 +  (is "?lhs = ?rhs")
1.4045 +proof
1.4046 +  assume ?lhs
1.4047 +  then show ?rhs
1.4048 +    using compact_imp_closed compact_imp_bounded
1.4049 +    by blast
1.4050 +next
1.4051 +  assume ?rhs
1.4052 +  then show ?lhs
1.4053 +    using bounded_closed_imp_seq_compact[of s]
1.4054 +    unfolding compact_eq_seq_compact_metric
1.4055 +    by auto
1.4056 +qed
1.4058 +lemma compact_Inter:
1.4059 +  fixes \<F> :: "'a :: heine_borel set set"
1.4060 +  assumes com: "\<And>S. S \<in> \<F> \<Longrightarrow> compact S" and "\<F> \<noteq> {}"
1.4061 +  shows "compact(\<Inter> \<F>)"
1.4062 +  using assms
1.4063 +  by (meson Inf_lower all_not_in_conv bounded_subset closed_Inter compact_eq_bounded_closed)
1.4065 +lemma compact_closure [simp]:
1.4066 +  fixes S :: "'a::heine_borel set"
1.4067 +  shows "compact(closure S) \<longleftrightarrow> bounded S"
1.4068 +by (meson bounded_closure bounded_subset closed_closure closure_subset compact_eq_bounded_closed)
1.4070 +lemma not_compact_UNIV[simp]:
1.4071 +  fixes s :: "'a::{real_normed_vector,perfect_space,heine_borel} set"
1.4072 +  shows "\<not> compact (UNIV::'a set)"
1.4073 +    by (simp add: compact_eq_bounded_closed)
1.4075 +text\<open>Representing sets as the union of a chain of compact sets.\<close>
1.4076 +lemma closed_Union_compact_subsets:
1.4077 +  fixes S :: "'a::{heine_borel,real_normed_vector} set"
1.4078 +  assumes "closed S"
1.4079 +  obtains F where "\<And>n. compact(F n)" "\<And>n. F n \<subseteq> S" "\<And>n. F n \<subseteq> F(Suc n)"
1.4080 +                  "(\<Union>n. F n) = S" "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> \<exists>N. \<forall>n \<ge> N. K \<subseteq> F n"
1.4081 +proof
1.4082 +  show "compact (S \<inter> cball 0 (of_nat n))" for n
1.4083 +    using assms compact_eq_bounded_closed by auto
1.4084 +next
1.4085 +  show "(\<Union>n. S \<inter> cball 0 (real n)) = S"
1.4086 +    by (auto simp: real_arch_simple)
1.4087 +next
1.4088 +  fix K :: "'a set"
1.4089 +  assume "compact K" "K \<subseteq> S"
1.4090 +  then obtain N where "K \<subseteq> cball 0 N"
1.4091 +    by (meson bounded_pos mem_cball_0 compact_imp_bounded subsetI)
1.4092 +  then show "\<exists>N. \<forall>n\<ge>N. K \<subseteq> S \<inter> cball 0 (real n)"
1.4093 +    by (metis of_nat_le_iff Int_subset_iff \<open>K \<subseteq> S\<close> real_arch_simple subset_cball subset_trans)
1.4094 +qed auto
1.4096 +instance%important real :: heine_borel
1.4097 +proof%unimportant
1.4098 +  fix f :: "nat \<Rightarrow> real"
1.4099 +  assume f: "bounded (range f)"
1.4100 +  obtain r :: "nat \<Rightarrow> nat" where r: "strict_mono r" "monoseq (f \<circ> r)"
1.4101 +    unfolding comp_def by (metis seq_monosub)
1.4102 +  then have "Bseq (f \<circ> r)"
1.4103 +    unfolding Bseq_eq_bounded using f by (force intro: bounded_subset)
1.4104 +  with r show "\<exists>l r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
1.4105 +    using Bseq_monoseq_convergent[of "f \<circ> r"] by (auto simp: convergent_def)
1.4106 +qed
1.4108 +lemma compact_lemma_general:
1.4109 +  fixes f :: "nat \<Rightarrow> 'a"
1.4110 +  fixes proj::"'a \<Rightarrow> 'b \<Rightarrow> 'c::heine_borel" (infixl "proj" 60)
1.4111 +  fixes unproj:: "('b \<Rightarrow> 'c) \<Rightarrow> 'a"
1.4112 +  assumes finite_basis: "finite basis"
1.4113 +  assumes bounded_proj: "\<And>k. k \<in> basis \<Longrightarrow> bounded ((\<lambda>x. x proj k) ` range f)"
1.4114 +  assumes proj_unproj: "\<And>e k. k \<in> basis \<Longrightarrow> (unproj e) proj k = e k"
1.4115 +  assumes unproj_proj: "\<And>x. unproj (\<lambda>k. x proj k) = x"
1.4116 +  shows "\<forall>d\<subseteq>basis. \<exists>l::'a. \<exists> r::nat\<Rightarrow>nat.
1.4117 +    strict_mono r \<and> (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) proj i) (l proj i) < e) sequentially)"
1.4118 +proof safe
1.4119 +  fix d :: "'b set"
1.4120 +  assume d: "d \<subseteq> basis"
1.4121 +  with finite_basis have "finite d"
1.4122 +    by (blast intro: finite_subset)
1.4123 +  from this d show "\<exists>l::'a. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and>
1.4124 +    (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) proj i) (l proj i) < e) sequentially)"
1.4125 +  proof (induct d)
1.4126 +    case empty
1.4127 +    then show ?case
1.4128 +      unfolding strict_mono_def by auto
1.4129 +  next
1.4130 +    case (insert k d)
1.4131 +    have k[intro]: "k \<in> basis"
1.4132 +      using insert by auto
1.4133 +    have s': "bounded ((\<lambda>x. x proj k) ` range f)"
1.4134 +      using k
1.4135 +      by (rule bounded_proj)
1.4136 +    obtain l1::"'a" and r1 where r1: "strict_mono r1"
1.4137 +      and lr1: "\<forall>e > 0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) proj i) (l1 proj i) < e) sequentially"
1.4138 +      using insert(3) using insert(4) by auto
1.4139 +    have f': "\<forall>n. f (r1 n) proj k \<in> (\<lambda>x. x proj k) ` range f"
1.4140 +      by simp
1.4141 +    have "bounded (range (\<lambda>i. f (r1 i) proj k))"
1.4142 +      by (metis (lifting) bounded_subset f' image_subsetI s')
1.4143 +    then obtain l2 r2 where r2:"strict_mono r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) proj k) \<longlongrightarrow> l2) sequentially"
1.4144 +      using bounded_imp_convergent_subsequence[of "\<lambda>i. f (r1 i) proj k"]
1.4145 +      by (auto simp: o_def)
1.4146 +    define r where "r = r1 \<circ> r2"
1.4147 +    have r:"strict_mono r"
1.4148 +      using r1 and r2 unfolding r_def o_def strict_mono_def by auto
1.4149 +    moreover
1.4150 +    define l where "l = unproj (\<lambda>i. if i = k then l2 else l1 proj i)"
1.4151 +    {
1.4152 +      fix e::real
1.4153 +      assume "e > 0"
1.4154 +      from lr1 \<open>e > 0\<close> have N1: "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) proj i) (l1 proj i) < e) sequentially"
1.4155 +        by blast
1.4156 +      from lr2 \<open>e > 0\<close> have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) proj k) l2 < e) sequentially"
1.4157 +        by (rule tendstoD)
1.4158 +      from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) proj i) (l1 proj i) < e) sequentially"
1.4159 +        by (rule eventually_subseq)
1.4160 +      have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) proj i) (l proj i) < e) sequentially"
1.4161 +        using N1' N2
1.4162 +        by eventually_elim (insert insert.prems, auto simp: l_def r_def o_def proj_unproj)
1.4163 +    }
1.4164 +    ultimately show ?case by auto
1.4165 +  qed
1.4166 +qed
1.4168 +lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
1.4169 +  unfolding bounded_def
1.4170 +  by (metis (erased, hide_lams) dist_fst_le image_iff order_trans)
1.4172 +lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
1.4173 +  unfolding bounded_def
1.4174 +  by (metis (no_types, hide_lams) dist_snd_le image_iff order.trans)
1.4176 +instance%important prod :: (heine_borel, heine_borel) heine_borel
1.4177 +proof%unimportant
1.4178 +  fix f :: "nat \<Rightarrow> 'a \<times> 'b"
1.4179 +  assume f: "bounded (range f)"
1.4180 +  then have "bounded (fst ` range f)"
1.4181 +    by (rule bounded_fst)
1.4182 +  then have s1: "bounded (range (fst \<circ> f))"
1.4183 +    by (simp add: image_comp)
1.4184 +  obtain l1 r1 where r1: "strict_mono r1" and l1: "(\<lambda>n. fst (f (r1 n))) \<longlonglongrightarrow> l1"
1.4185 +    using bounded_imp_convergent_subsequence [OF s1] unfolding o_def by fast
1.4186 +  from f have s2: "bounded (range (snd \<circ> f \<circ> r1))"
1.4187 +    by (auto simp: image_comp intro: bounded_snd bounded_subset)
1.4188 +  obtain l2 r2 where r2: "strict_mono r2" and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) \<longlongrightarrow> l2) sequentially"
1.4189 +    using bounded_imp_convergent_subsequence [OF s2]
1.4190 +    unfolding o_def by fast
1.4191 +  have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) \<longlongrightarrow> l1) sequentially"
1.4192 +    using LIMSEQ_subseq_LIMSEQ [OF l1 r2] unfolding o_def .
1.4193 +  have l: "((f \<circ> (r1 \<circ> r2)) \<longlongrightarrow> (l1, l2)) sequentially"
1.4194 +    using tendsto_Pair [OF l1' l2] unfolding o_def by simp
1.4195 +  have r: "strict_mono (r1 \<circ> r2)"
1.4196 +    using r1 r2 unfolding strict_mono_def by simp
1.4197 +  show "\<exists>l r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
1.4198 +    using l r by fast
1.4199 +qed
1.4201 +subsubsection \<open>Completeness\<close>
1.4203 +proposition (in metric_space) completeI:
1.4204 +  assumes "\<And>f. \<forall>n. f n \<in> s \<Longrightarrow> Cauchy f \<Longrightarrow> \<exists>l\<in>s. f \<longlonglongrightarrow> l"
1.4205 +  shows "complete s"
1.4206 +  using assms unfolding complete_def by fast
1.4208 +proposition (in metric_space) completeE:
1.4209 +  assumes "complete s" and "\<forall>n. f n \<in> s" and "Cauchy f"
1.4210 +  obtains l where "l \<in> s" and "f \<longlonglongrightarrow> l"
1.4211 +  using assms unfolding complete_def by fast
1.4213 +(* TODO: generalize to uniform spaces *)
1.4214 +lemma compact_imp_complete:
1.4215 +  fixes s :: "'a::metric_space set"
1.4216 +  assumes "compact s"
1.4217 +  shows "complete s"
1.4218 +proof -
1.4219 +  {
1.4220 +    fix f
1.4221 +    assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
1.4222 +    from as(1) obtain l r where lr: "l\<in>s" "strict_mono r" "(f \<circ> r) \<longlonglongrightarrow> l"
1.4223 +      using assms unfolding compact_def by blast
1.4225 +    note lr' = seq_suble [OF lr(2)]
1.4226 +    {
1.4227 +      fix e :: real
1.4228 +      assume "e > 0"
1.4229 +      from as(2) obtain N where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (f m) (f n) < e/2"
1.4230 +        unfolding cauchy_def
1.4231 +        using \<open>e > 0\<close>
1.4232 +        apply (erule_tac x="e/2" in allE, auto)
1.4233 +        done
1.4234 +      from lr(3)[unfolded lim_sequentially, THEN spec[where x="e/2"]]
1.4235 +      obtain M where M:"\<forall>n\<ge>M. dist ((f \<circ> r) n) l < e/2"
1.4236 +        using \<open>e > 0\<close> by auto
1.4237 +      {
1.4238 +        fix n :: nat
1.4239 +        assume n: "n \<ge> max N M"
1.4240 +        have "dist ((f \<circ> r) n) l < e/2"
1.4241 +          using n M by auto
1.4242 +        moreover have "r n \<ge> N"
1.4243 +          using lr'[of n] n by auto
1.4244 +        then have "dist (f n) ((f \<circ> r) n) < e / 2"
1.4245 +          using N and n by auto
1.4246 +        ultimately have "dist (f n) l < e"
1.4247 +          using dist_triangle_half_r[of "f (r n)" "f n" e l]
1.4248 +          by (auto simp: dist_commute)
1.4249 +      }
1.4250 +      then have "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast
1.4251 +    }
1.4252 +    then have "\<exists>l\<in>s. (f \<longlongrightarrow> l) sequentially" using \<open>l\<in>s\<close>
1.4253 +      unfolding lim_sequentially by auto
1.4254 +  }
1.4255 +  then show ?thesis unfolding complete_def by auto
1.4256 +qed
1.4258 +proposition compact_eq_totally_bounded:
1.4259 +  "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>x\<in>k. ball x e))"
1.4260 +    (is "_ \<longleftrightarrow> ?rhs")
1.4261 +proof
1.4262 +  assume assms: "?rhs"
1.4263 +  then obtain k where k: "\<And>e. 0 < e \<Longrightarrow> finite (k e)" "\<And>e. 0 < e \<Longrightarrow> s \<subseteq> (\<Union>x\<in>k e. ball x e)"
1.4264 +    by (auto simp: choice_iff')
1.4266 +  show "compact s"
1.4267 +  proof cases
1.4268 +    assume "s = {}"
1.4269 +    then show "compact s" by (simp add: compact_def)
1.4270 +  next
1.4271 +    assume "s \<noteq> {}"
1.4272 +    show ?thesis
1.4273 +      unfolding compact_def
1.4274 +    proof safe
1.4275 +      fix f :: "nat \<Rightarrow> 'a"
1.4276 +      assume f: "\<forall>n. f n \<in> s"
1.4278 +      define e where "e n = 1 / (2 * Suc n)" for n
1.4279 +      then have [simp]: "\<And>n. 0 < e n" by auto
1.4280 +      define B where "B n U = (SOME b. infinite {n. f n \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U))" for n U
1.4281 +      {
1.4282 +        fix n U
1.4283 +        assume "infinite {n. f n \<in> U}"
1.4284 +        then have "\<exists>b\<in>k (e n). infinite {i\<in>{n. f n \<in> U}. f i \<in> ball b (e n)}"
1.4285 +          using k f by (intro pigeonhole_infinite_rel) (auto simp: subset_eq)
1.4286 +        then obtain a where
1.4287 +          "a \<in> k (e n)"
1.4288 +          "infinite {i \<in> {n. f n \<in> U}. f i \<in> ball a (e n)}" ..
1.4289 +        then have "\<exists>b. infinite {i. f i \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U)"
1.4290 +          by (intro exI[of _ "ball a (e n) \<inter> U"] exI[of _ a]) (auto simp: ac_simps)
1.4291 +        from someI_ex[OF this]
1.4292 +        have "infinite {i. f i \<in> B n U}" "\<exists>x. B n U \<subseteq> ball x (e n) \<inter> U"
1.4293 +          unfolding B_def by auto
1.4294 +      }
1.4295 +      note B = this
1.4297 +      define F where "F = rec_nat (B 0 UNIV) B"
1.4298 +      {
1.4299 +        fix n
1.4300 +        have "infinite {i. f i \<in> F n}"
1.4301 +          by (induct n) (auto simp: F_def B)
1.4302 +      }
1.4303 +      then have F: "\<And>n. \<exists>x. F (Suc n) \<subseteq> ball x (e n) \<inter> F n"
1.4304 +        using B by (simp add: F_def)
1.4305 +      then have F_dec: "\<And>m n. m \<le> n \<Longrightarrow> F n \<subseteq> F m"
1.4306 +        using decseq_SucI[of F] by (auto simp: decseq_def)
1.4308 +      obtain sel where sel: "\<And>k i. i < sel k i" "\<And>k i. f (sel k i) \<in> F k"
1.4309 +      proof (atomize_elim, unfold all_conj_distrib[symmetric], intro choice allI)
1.4310 +        fix k i
1.4311 +        have "infinite ({n. f n \<in> F k} - {.. i})"
1.4312 +          using \<open>infinite {n. f n \<in> F k}\<close> by auto
1.4313 +        from infinite_imp_nonempty[OF this]
1.4314 +        show "\<exists>x>i. f x \<in> F k"
1.4315 +          by (simp add: set_eq_iff not_le conj_commute)
1.4316 +      qed
1.4318 +      define t where "t = rec_nat (sel 0 0) (\<lambda>n i. sel (Suc n) i)"
1.4319 +      have "strict_mono t"
1.4320 +        unfolding strict_mono_Suc_iff by (simp add: t_def sel)
1.4321 +      moreover have "\<forall>i. (f \<circ> t) i \<in> s"
1.4322 +        using f by auto
1.4323 +      moreover
1.4324 +      {
1.4325 +        fix n
1.4326 +        have "(f \<circ> t) n \<in> F n"
1.4327 +          by (cases n) (simp_all add: t_def sel)
1.4328 +      }
1.4329 +      note t = this
1.4331 +      have "Cauchy (f \<circ> t)"
1.4332 +      proof (safe intro!: metric_CauchyI exI elim!: nat_approx_posE)
1.4333 +        fix r :: real and N n m
1.4334 +        assume "1 / Suc N < r" "Suc N \<le> n" "Suc N \<le> m"
1.4335 +        then have "(f \<circ> t) n \<in> F (Suc N)" "(f \<circ> t) m \<in> F (Suc N)" "2 * e N < r"
1.4336 +          using F_dec t by (auto simp: e_def field_simps of_nat_Suc)
1.4337 +        with F[of N] obtain x where "dist x ((f \<circ> t) n) < e N" "dist x ((f \<circ> t) m) < e N"
1.4338 +          by (auto simp: subset_eq)
1.4339 +        with dist_triangle[of "(f \<circ> t) m" "(f \<circ> t) n" x] \<open>2 * e N < r\<close>
1.4340 +        show "dist ((f \<circ> t) m) ((f \<circ> t) n) < r"
1.4341 +          by (simp add: dist_commute)
1.4342 +      qed
1.4344 +      ultimately show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
1.4345 +        using assms unfolding complete_def by blast
1.4346 +    qed
1.4347 +  qed
1.4348 +qed (metis compact_imp_complete compact_imp_seq_compact seq_compact_imp_totally_bounded)
1.4350 +lemma cauchy_imp_bounded:
1.4351 +  assumes "Cauchy s"
1.4352 +  shows "bounded (range s)"
1.4353 +proof -
1.4354 +  from assms obtain N :: nat where "\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < 1"
1.4355 +    unfolding cauchy_def by force
1.4356 +  then have N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
1.4357 +  moreover
1.4358 +  have "bounded (s ` {0..N})"
1.4359 +    using finite_imp_bounded[of "s ` {1..N}"] by auto
1.4360 +  then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
1.4361 +    unfolding bounded_any_center [where a="s N"] by auto
1.4362 +  ultimately show "?thesis"
1.4363 +    unfolding bounded_any_center [where a="s N"]
1.4364 +    apply (rule_tac x="max a 1" in exI, auto)
1.4365 +    apply (erule_tac x=y in allE)
1.4366 +    apply (erule_tac x=y in ballE, auto)
1.4367 +    done
1.4368 +qed
1.4370 +instance heine_borel < complete_space
1.4371 +proof
1.4372 +  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
1.4373 +  then have "bounded (range f)"
1.4374 +    by (rule cauchy_imp_bounded)
1.4375 +  then have "compact (closure (range f))"
1.4376 +    unfolding compact_eq_bounded_closed by auto
1.4377 +  then have "complete (closure (range f))"
1.4378 +    by (rule compact_imp_complete)
1.4379 +  moreover have "\<forall>n. f n \<in> closure (range f)"
1.4380 +    using closure_subset [of "range f"] by auto
1.4381 +  ultimately have "\<exists>l\<in>closure (range f). (f \<longlongrightarrow> l) sequentially"
1.4382 +    using \<open>Cauchy f\<close> unfolding complete_def by auto
1.4383 +  then show "convergent f"
1.4384 +    unfolding convergent_def by auto
1.4385 +qed
1.4387 +lemma complete_UNIV: "complete (UNIV :: ('a::complete_space) set)"
1.4388 +proof (rule completeI)
1.4389 +  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
1.4390 +  then have "convergent f" by (rule Cauchy_convergent)
1.4391 +  then show "\<exists>l\<in>UNIV. f \<longlonglongrightarrow> l" unfolding convergent_def by simp
1.4392 +qed
1.4394 +lemma complete_imp_closed:
1.4395 +  fixes S :: "'a::metric_space set"
1.4396 +  assumes "complete S"
1.4397 +  shows "closed S"
1.4398 +proof (unfold closed_sequential_limits, clarify)
1.4399 +  fix f x assume "\<forall>n. f n \<in> S" and "f \<longlonglongrightarrow> x"
1.4400 +  from \<open>f \<longlonglongrightarrow> x\<close> have "Cauchy f"
1.4401 +    by (rule LIMSEQ_imp_Cauchy)
1.4402 +  with \<open>complete S\<close> and \<open>\<forall>n. f n \<in> S\<close> obtain l where "l \<in> S" and "f \<longlonglongrightarrow> l"
1.4403 +    by (rule completeE)
1.4404 +  from \<open>f \<longlonglongrightarrow> x\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "x = l"
1.4405 +    by (rule LIMSEQ_unique)
1.4406 +  with \<open>l \<in> S\<close> show "x \<in> S"
1.4407 +    by simp
1.4408 +qed
1.4410 +lemma complete_Int_closed:
1.4411 +  fixes S :: "'a::metric_space set"
1.4412 +  assumes "complete S" and "closed t"
1.4413 +  shows "complete (S \<inter> t)"
1.4414 +proof (rule completeI)
1.4415 +  fix f assume "\<forall>n. f n \<in> S \<inter> t" and "Cauchy f"
1.4416 +  then have "\<forall>n. f n \<in> S" and "\<forall>n. f n \<in> t"
1.4417 +    by simp_all
1.4418 +  from \<open>complete S\<close> obtain l where "l \<in> S" and "f \<longlonglongrightarrow> l"
1.4419 +    using \<open>\<forall>n. f n \<in> S\<close> and \<open>Cauchy f\<close> by (rule completeE)
1.4420 +  from \<open>closed t\<close> and \<open>\<forall>n. f n \<in> t\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "l \<in> t"
1.4421 +    by (rule closed_sequentially)
1.4422 +  with \<open>l \<in> S\<close> and \<open>f \<longlonglongrightarrow> l\<close> show "\<exists>l\<in>S \<inter> t. f \<longlonglongrightarrow> l"
1.4423 +    by fast
1.4424 +qed
1.4426 +lemma complete_closed_subset:
1.4427 +  fixes S :: "'a::metric_space set"
1.4428 +  assumes "closed S" and "S \<subseteq> t" and "complete t"
1.4429 +  shows "complete S"
1.4430 +  using assms complete_Int_closed [of t S] by (simp add: Int_absorb1)
1.4432 +lemma complete_eq_closed:
1.4433 +  fixes S :: "('a::complete_space) set"
1.4434 +  shows "complete S \<longleftrightarrow> closed S"
1.4435 +proof
1.4436 +  assume "closed S" then show "complete S"
1.4437 +    using subset_UNIV complete_UNIV by (rule complete_closed_subset)
1.4438 +next
1.4439 +  assume "complete S" then show "closed S"
1.4440 +    by (rule complete_imp_closed)
1.4441 +qed
1.4443 +lemma convergent_eq_Cauchy:
1.4444 +  fixes S :: "nat \<Rightarrow> 'a::complete_space"
1.4445 +  shows "(\<exists>l. (S \<longlongrightarrow> l) sequentially) \<longleftrightarrow> Cauchy S"
1.4446 +  unfolding Cauchy_convergent_iff convergent_def ..
1.4448 +lemma convergent_imp_bounded:
1.4449 +  fixes S :: "nat \<Rightarrow> 'a::metric_space"
1.4450 +  shows "(S \<longlongrightarrow> l) sequentially \<Longrightarrow> bounded (range S)"
1.4451 +  by (intro cauchy_imp_bounded LIMSEQ_imp_Cauchy)
1.4453 +lemma frontier_subset_compact:
1.4454 +  fixes S :: "'a::heine_borel set"
1.4455 +  shows "compact S \<Longrightarrow> frontier S \<subseteq> S"
1.4456 +  using frontier_subset_closed compact_eq_bounded_closed
1.4457 +  by blast
1.4459 +subsection \<open>Continuity\<close>
1.4461 +text\<open>Derive the epsilon-delta forms, which we often use as "definitions"\<close>
1.4463 +proposition continuous_within_eps_delta:
1.4464 +  "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. \<forall>x'\<in> s.  dist x' x < d --> dist (f x') (f x) < e)"
1.4465 +  unfolding continuous_within and Lim_within  by fastforce
1.4467 +corollary continuous_at_eps_delta:
1.4468 +  "continuous (at x) f \<longleftrightarrow> (\<forall>e > 0. \<exists>d > 0. \<forall>x'. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
1.4469 +  using continuous_within_eps_delta [of x UNIV f] by simp
1.4471 +lemma continuous_at_right_real_increasing:
1.4472 +  fixes f :: "real \<Rightarrow> real"
1.4473 +  assumes nondecF: "\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y"
1.4474 +  shows "continuous (at_right a) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f (a + d) - f a < e)"
1.4475 +  apply (simp add: greaterThan_def dist_real_def continuous_within Lim_within_le)
1.4476 +  apply (intro all_cong ex_cong, safe)
1.4477 +  apply (erule_tac x="a + d" in allE, simp)
1.4478 +  apply (simp add: nondecF field_simps)
1.4479 +  apply (drule nondecF, simp)
1.4480 +  done
1.4482 +lemma continuous_at_left_real_increasing:
1.4483 +  assumes nondecF: "\<And> x y. x \<le> y \<Longrightarrow> f x \<le> ((f y) :: real)"
1.4484 +  shows "(continuous (at_left (a :: real)) f) = (\<forall>e > 0. \<exists>delta > 0. f a - f (a - delta) < e)"
1.4485 +  apply (simp add: lessThan_def dist_real_def continuous_within Lim_within_le)
1.4486 +  apply (intro all_cong ex_cong, safe)
1.4487 +  apply (erule_tac x="a - d" in allE, simp)
1.4488 +  apply (simp add: nondecF field_simps)
1.4489 +  apply (cut_tac x="a - d" and y=x in nondecF, simp_all)
1.4490 +  done
1.4492 +text\<open>Versions in terms of open balls.\<close>
1.4494 +lemma continuous_within_ball:
1.4495 +  "continuous (at x within s) f \<longleftrightarrow>
1.4496 +    (\<forall>e > 0. \<exists>d > 0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e)"
1.4497 +  (is "?lhs = ?rhs")
1.4498 +proof
1.4499 +  assume ?lhs
1.4500 +  {
1.4501 +    fix e :: real
1.4502 +    assume "e > 0"
1.4503 +    then obtain d where d: "d>0" "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e"
1.4504 +      using \<open>?lhs\<close>[unfolded continuous_within Lim_within] by auto
1.4505 +    {
1.4506 +      fix y
1.4507 +      assume "y \<in> f ` (ball x d \<inter> s)"
1.4508 +      then have "y \<in> ball (f x) e"
1.4509 +        using d(2)
1.4510 +        apply (auto simp: dist_commute)
1.4511 +        apply (erule_tac x=xa in ballE, auto)
1.4512 +        using \<open>e > 0\<close>
1.4513 +        apply auto
1.4514 +        done
1.4515 +    }
1.4516 +    then have "\<exists>d>0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e"
1.4517 +      using \<open>d > 0\<close>
1.4518 +      unfolding subset_eq ball_def by (auto simp: dist_commute)
1.4519 +  }
1.4520 +  then show ?rhs by auto
1.4521 +next
1.4522 +  assume ?rhs
1.4523 +  then show ?lhs
1.4524 +    unfolding continuous_within Lim_within ball_def subset_eq
1.4525 +    apply (auto simp: dist_commute)
1.4526 +    apply (erule_tac x=e in allE, auto)
1.4527 +    done
1.4528 +qed
1.4530 +lemma continuous_at_ball:
1.4531 +  "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f ` (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
1.4532 +proof
1.4533 +  assume ?lhs
1.4534 +  then show ?rhs
1.4535 +    unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
1.4536 +    apply auto
1.4537 +    apply (erule_tac x=e in allE, auto)
1.4538 +    apply (rule_tac x=d in exI, auto)
1.4539 +    apply (erule_tac x=xa in allE)
1.4540 +    apply (auto simp: dist_commute)
1.4541 +    done
1.4542 +next
1.4543 +  assume ?rhs
1.4544 +  then show ?lhs
1.4545 +    unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
1.4546 +    apply auto
1.4547 +    apply (erule_tac x=e in allE, auto)
1.4548 +    apply (rule_tac x=d in exI, auto)
1.4549 +    apply (erule_tac x="f xa" in allE)
1.4550 +    apply (auto simp: dist_commute)
1.4551 +    done
1.4552 +qed
1.4554 +text\<open>Define setwise continuity in terms of limits within the set.\<close>
1.4556 +lemma continuous_on_iff:
1.4557 +  "continuous_on s f \<longleftrightarrow>
1.4558 +    (\<forall>x\<in>s. \<forall>e>0. \<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
1.4559 +  unfolding continuous_on_def Lim_within
1.4560 +  by (metis dist_pos_lt dist_self)
1.4562 +lemma continuous_within_E:
1.4563 +  assumes "continuous (at x within s) f" "e>0"
1.4564 +  obtains d where "d>0"  "\<And>x'. \<lbrakk>x'\<in> s; dist x' x \<le> d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
1.4565 +  using assms apply (simp add: continuous_within_eps_delta)
1.4566 +  apply (drule spec [of _ e], clarify)
1.4567 +  apply (rule_tac d="d/2" in that, auto)
1.4568 +  done
1.4570 +lemma continuous_onI [intro?]:
1.4571 +  assumes "\<And>x e. \<lbrakk>e > 0; x \<in> s\<rbrakk> \<Longrightarrow> \<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) \<le> e"
1.4572 +  shows "continuous_on s f"
1.4573 +apply (simp add: continuous_on_iff, clarify)
1.4574 +apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
1.4575 +done
1.4577 +text\<open>Some simple consequential lemmas.\<close>
1.4579 +lemma continuous_onE:
1.4580 +    assumes "continuous_on s f" "x\<in>s" "e>0"
1.4581 +    obtains d where "d>0"  "\<And>x'. \<lbrakk>x' \<in> s; dist x' x \<le> d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
1.4582 +  using assms
1.4583 +  apply (simp add: continuous_on_iff)
1.4584 +  apply (elim ballE allE)
1.4585 +  apply (auto intro: that [where d="d/2" for d])
1.4586 +  done
1.4588 +lemma uniformly_continuous_onE:
1.4589 +  assumes "uniformly_continuous_on s f" "0 < e"
1.4590 +  obtains d where "d>0" "\<And>x x'. \<lbrakk>x\<in>s; x'\<in>s; dist x' x < d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
1.4591 +using assms
1.4592 +by (auto simp: uniformly_continuous_on_def)
1.4594 +lemma continuous_at_imp_continuous_within:
1.4595 +  "continuous (at x) f \<Longrightarrow> continuous (at x within s) f"
1.4596 +  unfolding continuous_within continuous_at using Lim_at_imp_Lim_at_within by auto
1.4598 +lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f \<longlongrightarrow> l) net"
1.4599 +  by simp
1.4601 +lemmas continuous_on = continuous_on_def \<comment> \<open>legacy theorem name\<close>
1.4603 +lemma continuous_within_subset:
1.4604 +  "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s \<Longrightarrow> continuous (at x within t) f"
1.4605 +  unfolding continuous_within by(metis tendsto_within_subset)
1.4607 +lemma continuous_on_interior:
1.4608 +  "continuous_on s f \<Longrightarrow> x \<in> interior s \<Longrightarrow> continuous (at x) f"
1.4609 +  by (metis continuous_on_eq_continuous_at continuous_on_subset interiorE)
1.4611 +lemma continuous_on_eq:
1.4612 +  "\<lbrakk>continuous_on s f; \<And>x. x \<in> s \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> continuous_on s g"
1.4613 +  unfolding continuous_on_def tendsto_def eventually_at_topological
1.4614 +  by simp
1.4616 +text \<open>Characterization of various kinds of continuity in terms of sequences.\<close>
1.4618 +lemma continuous_within_sequentiallyI:
1.4619 +  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
1.4620 +  assumes "\<And>u::nat \<Rightarrow> 'a. u \<longlonglongrightarrow> a \<Longrightarrow> (\<forall>n. u n \<in> s) \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
1.4621 +  shows "continuous (at a within s) f"
1.4622 +  using assms unfolding continuous_within tendsto_def[where l = "f a"]
1.4623 +  by (auto intro!: sequentially_imp_eventually_within)
1.4625 +lemma continuous_within_tendsto_compose:
1.4626 +  fixes f::"'a::t2_space \<Rightarrow> 'b::topological_space"
1.4627 +  assumes "continuous (at a within s) f"
1.4628 +          "eventually (\<lambda>n. x n \<in> s) F"
1.4629 +          "(x \<longlongrightarrow> a) F "
1.4630 +  shows "((\<lambda>n. f (x n)) \<longlongrightarrow> f a) F"
1.4631 +proof -
1.4632 +  have *: "filterlim x (inf (nhds a) (principal s)) F"
1.4633 +    using assms(2) assms(3) unfolding at_within_def filterlim_inf by (auto simp: filterlim_principal eventually_mono)
1.4634 +  show ?thesis
1.4635 +    by (auto simp: assms(1) continuous_within[symmetric] tendsto_at_within_iff_tendsto_nhds[symmetric] intro!: filterlim_compose[OF _ *])
1.4636 +qed
1.4638 +lemma continuous_within_tendsto_compose':
1.4639 +  fixes f::"'a::t2_space \<Rightarrow> 'b::topological_space"
1.4640 +  assumes "continuous (at a within s) f"
1.4641 +    "\<And>n. x n \<in> s"
1.4642 +    "(x \<longlongrightarrow> a) F "
1.4643 +  shows "((\<lambda>n. f (x n)) \<longlongrightarrow> f a) F"
1.4644 +  by (auto intro!: continuous_within_tendsto_compose[OF assms(1)] simp add: assms)
1.4646 +lemma continuous_within_sequentially:
1.4647 +  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
1.4648 +  shows "continuous (at a within s) f \<longleftrightarrow>
1.4649 +    (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x \<longlongrightarrow> a) sequentially
1.4650 +         \<longrightarrow> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
1.4651 +  using continuous_within_tendsto_compose'[of a s f _ sequentially]
1.4652 +    continuous_within_sequentiallyI[of a s f]
1.4653 +  by (auto simp: o_def)
1.4655 +lemma continuous_at_sequentiallyI:
1.4656 +  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
1.4657 +  assumes "\<And>u. u \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
1.4658 +  shows "continuous (at a) f"
1.4659 +  using continuous_within_sequentiallyI[of a UNIV f] assms by auto
1.4661 +lemma continuous_at_sequentially:
1.4662 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
1.4663 +  shows "continuous (at a) f \<longleftrightarrow>
1.4664 +    (\<forall>x. (x \<longlongrightarrow> a) sequentially --> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
1.4665 +  using continuous_within_sequentially[of a UNIV f] by simp
1.4667 +lemma continuous_on_sequentiallyI:
1.4668 +  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
1.4669 +  assumes "\<And>u a. (\<forall>n. u n \<in> s) \<Longrightarrow> a \<in> s \<Longrightarrow> u \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
1.4670 +  shows "continuous_on s f"
1.4671 +  using assms unfolding continuous_on_eq_continuous_within
1.4672 +  using continuous_within_sequentiallyI[of _ s f] by auto
1.4674 +lemma continuous_on_sequentially:
1.4675 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
1.4676 +  shows "continuous_on s f \<longleftrightarrow>
1.4677 +    (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x \<longlongrightarrow> a) sequentially
1.4678 +      --> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
1.4679 +    (is "?lhs = ?rhs")
1.4680 +proof
1.4681 +  assume ?rhs
1.4682 +  then show ?lhs
1.4683 +    using continuous_within_sequentially[of _ s f]
1.4684 +    unfolding continuous_on_eq_continuous_within
1.4685 +    by auto
1.4686 +next
1.4687 +  assume ?lhs
1.4688 +  then show ?rhs
1.4689 +    unfolding continuous_on_eq_continuous_within
1.4690 +    using continuous_within_sequentially[of _ s f]
1.4691 +    by auto
1.4692 +qed
1.4694 +lemma uniformly_continuous_on_sequentially:
1.4695 +  "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
1.4696 +    (\<lambda>n. dist (x n) (y n)) \<longlonglongrightarrow> 0 \<longrightarrow> (\<lambda>n. dist (f(x n)) (f(y n))) \<longlonglongrightarrow> 0)" (is "?lhs = ?rhs")
1.4697 +proof
1.4698 +  assume ?lhs
1.4699 +  {
1.4700 +    fix x y
1.4701 +    assume x: "\<forall>n. x n \<in> s"
1.4702 +      and y: "\<forall>n. y n \<in> s"
1.4703 +      and xy: "((\<lambda>n. dist (x n) (y n)) \<longlongrightarrow> 0) sequentially"
1.4704 +    {
1.4705 +      fix e :: real
1.4706 +      assume "e > 0"
1.4707 +      then obtain d where "d > 0" and d: "\<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
1.4708 +        using \<open>?lhs\<close>[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto
1.4709 +      obtain N where N: "\<forall>n\<ge>N. dist (x n) (y n) < d"
1.4710 +        using xy[unfolded lim_sequentially dist_norm] and \<open>d>0\<close> by auto
1.4711 +      {
1.4712 +        fix n
1.4713 +        assume "n\<ge>N"
1.4714 +        then have "dist (f (x n)) (f (y n)) < e"
1.4715 +          using N[THEN spec[where x=n]]
1.4716 +          using d[THEN bspec[where x="x n"], THEN bspec[where x="y n"]]
1.4717 +          using x and y
1.4718 +          by (simp add: dist_commute)
1.4719 +      }
1.4720 +      then have "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"
1.4721 +        by auto
1.4722 +    }
1.4723 +    then have "((\<lambda>n. dist (f(x n)) (f(y n))) \<longlongrightarrow> 0) sequentially"
1.4724 +      unfolding lim_sequentially and dist_real_def by auto
1.4725 +  }
1.4726 +  then show ?rhs by auto
1.4727 +next
1.4728 +  assume ?rhs
1.4729 +  {
1.4730 +    assume "\<not> ?lhs"
1.4731 +    then obtain e where "e > 0" "\<forall>d>0. \<exists>x\<in>s. \<exists>x'\<in>s. dist x' x < d \<and> \<not> dist (f x') (f x) < e"
1.4732 +      unfolding uniformly_continuous_on_def by auto
1.4733 +    then obtain fa where fa:
1.4734 +      "\<forall>x. 0 < x \<longrightarrow> fst (fa x) \<in> s \<and> snd (fa x) \<in> s \<and> dist (fst (fa x)) (snd (fa x)) < x \<and> \<not> dist (f (fst (fa x))) (f (snd (fa x))) < e"
1.4735 +      using choice[of "\<lambda>d x. d>0 \<longrightarrow> fst x \<in> s \<and> snd x \<in> s \<and> dist (snd x) (fst x) < d \<and> \<not> dist (f (snd x)) (f (fst x)) < e"]
1.4736 +      unfolding Bex_def
1.4737 +      by (auto simp: dist_commute)
1.4738 +    define x where "x n = fst (fa (inverse (real n + 1)))" for n
1.4739 +    define y where "y n = snd (fa (inverse (real n + 1)))" for n
1.4740 +    have xyn: "\<forall>n. x n \<in> s \<and> y n \<in> s"
1.4741 +      and xy0: "\<forall>n. dist (x n) (y n) < inverse (real n + 1)"
1.4742 +      and fxy:"\<forall>n. \<not> dist (f (x n)) (f (y n)) < e"
1.4743 +      unfolding x_def and y_def using fa
1.4744 +      by auto
1.4745 +    {
1.4746 +      fix e :: real
1.4747 +      assume "e > 0"
1.4748 +      then obtain N :: nat where "N \<noteq> 0" and N: "0 < inverse (real N) \<and> inverse (real N) < e"
1.4749 +        unfolding real_arch_inverse[of e] by auto
1.4750 +      {
1.4751 +        fix n :: nat
1.4752 +        assume "n \<ge> N"
1.4753 +        then have "inverse (real n + 1) < inverse (real N)"
1.4754 +          using of_nat_0_le_iff and \<open>N\<noteq>0\<close> by auto
1.4755 +        also have "\<dots> < e" using N by auto
1.4756 +        finally have "inverse (real n + 1) < e" by auto
1.4757 +        then have "dist (x n) (y n) < e"
1.4758 +          using xy0[THEN spec[where x=n]] by auto
1.4759 +      }
1.4760 +      then have "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto
1.4761 +    }
1.4762 +    then have "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"
1.4763 +      using \<open>?rhs\<close>[THEN spec[where x=x], THEN spec[where x=y]] and xyn
1.4764 +      unfolding lim_sequentially dist_real_def by auto
1.4765 +    then have False using fxy and \<open>e>0\<close> by auto
1.4766 +  }
1.4767 +  then show ?lhs
1.4768 +    unfolding uniformly_continuous_on_def by blast
1.4769 +qed
1.4771 +lemma continuous_closed_imp_Cauchy_continuous:
1.4772 +  fixes S :: "('a::complete_space) set"
1.4773 +  shows "\<lbrakk>continuous_on S f; closed S; Cauchy \<sigma>; \<And>n. (\<sigma> n) \<in> S\<rbrakk> \<Longrightarrow> Cauchy(f \<circ> \<sigma>)"
1.4774 +  apply (simp add: complete_eq_closed [symmetric] continuous_on_sequentially)
1.4775 +  by (meson LIMSEQ_imp_Cauchy complete_def)
1.4777 +text\<open>The usual transformation theorems.\<close>
1.4779 +lemma continuous_transform_within:
1.4780 +  fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
1.4781 +  assumes "continuous (at x within s) f"
1.4782 +    and "0 < d"
1.4783 +    and "x \<in> s"
1.4784 +    and "\<And>x'. \<lbrakk>x' \<in> s; dist x' x < d\<rbrakk> \<Longrightarrow> f x' = g x'"
1.4785 +  shows "continuous (at x within s) g"
1.4786 +  using assms
1.4787 +  unfolding continuous_within
1.4788 +  by (force intro: Lim_transform_within)
1.4791 +subsubsection%unimportant \<open>Structural rules for uniform continuity\<close>
1.4793 +lemma uniformly_continuous_on_dist[continuous_intros]:
1.4794 +  fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"
1.4795 +  assumes "uniformly_continuous_on s f"
1.4796 +    and "uniformly_continuous_on s g"
1.4797 +  shows "uniformly_continuous_on s (\<lambda>x. dist (f x) (g x))"
1.4798 +proof -
1.4799 +  {
1.4800 +    fix a b c d :: 'b
1.4801 +    have "\<bar>dist a b - dist c d\<bar> \<le> dist a c + dist b d"
1.4802 +      using dist_triangle2 [of a b c] dist_triangle2 [of b c d]
1.4803 +      using dist_triangle3 [of c d a] dist_triangle [of a d b]
1.4804 +      by arith
1.4805 +  } note le = this
1.4806 +  {
1.4807 +    fix x y
1.4808 +    assume f: "(\<lambda>n. dist (f (x n)) (f (y n))) \<longlonglongrightarrow> 0"
1.4809 +    assume g: "(\<lambda>n. dist (g (x n)) (g (y n))) \<longlonglongrightarrow> 0"
1.4810 +    have "(\<lambda>n. \<bar>dist (f (x n)) (g (x n)) - dist (f (y n)) (g (y n))\<bar>) \<longlonglongrightarrow> 0"
1.4811 +      by (rule Lim_transform_bound [OF _ tendsto_add_zero [OF f g]],
1.4813 +  }
1.4814 +  then show ?thesis
1.4815 +    using assms unfolding uniformly_continuous_on_sequentially
1.4816 +    unfolding dist_real_def by simp
1.4817 +qed
1.4819 +lemma uniformly_continuous_on_norm[continuous_intros]:
1.4820 +  fixes f :: "'a :: metric_space \<Rightarrow> 'b :: real_normed_vector"
1.4821 +  assumes "uniformly_continuous_on s f"
1.4822 +  shows "uniformly_continuous_on s (\<lambda>x. norm (f x))"
1.4823 +  unfolding norm_conv_dist using assms
1.4824 +  by (intro uniformly_continuous_on_dist uniformly_continuous_on_const)
1.4826 +lemma (in bounded_linear) uniformly_continuous_on[continuous_intros]:
1.4827 +  fixes g :: "_::metric_space \<Rightarrow> _"
1.4828 +  assumes "uniformly_continuous_on s g"
1.4829 +  shows "uniformly_continuous_on s (\<lambda>x. f (g x))"
1.4830 +  using assms unfolding uniformly_continuous_on_sequentially
1.4831 +  unfolding dist_norm tendsto_norm_zero_iff diff[symmetric]
1.4832 +  by (auto intro: tendsto_zero)
1.4834 +lemma uniformly_continuous_on_cmul[continuous_intros]:
1.4835 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
1.4836 +  assumes "uniformly_continuous_on s f"
1.4837 +  shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"
1.4838 +  using bounded_linear_scaleR_right assms
1.4839 +  by (rule bounded_linear.uniformly_continuous_on)
1.4841 +lemma dist_minus:
1.4842 +  fixes x y :: "'a::real_normed_vector"
1.4843 +  shows "dist (- x) (- y) = dist x y"
1.4844 +  unfolding dist_norm minus_diff_minus norm_minus_cancel ..
1.4846 +lemma uniformly_continuous_on_minus[continuous_intros]:
1.4847 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
1.4848 +  shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. - f x)"
1.4849 +  unfolding uniformly_continuous_on_def dist_minus .
1.4852 +  fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
1.4853 +  assumes "uniformly_continuous_on s f"
1.4854 +    and "uniformly_continuous_on s g"
1.4855 +  shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"
1.4856 +  using assms
1.4857 +  unfolding uniformly_continuous_on_sequentially
1.4859 +  by (auto intro: tendsto_add_zero)
1.4861 +lemma uniformly_continuous_on_diff[continuous_intros]:
1.4862 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
1.4863 +  assumes "uniformly_continuous_on s f"
1.4864 +    and "uniformly_continuous_on s g"
1.4865 +  shows "uniformly_continuous_on s (\<lambda>x. f x - g x)"
1.4866 +  using assms uniformly_continuous_on_add [of s f "- g"]
1.4867 +    by (simp add: fun_Compl_def uniformly_continuous_on_minus)
1.4869 +text \<open>Continuity in terms of open preimages.\<close>
1.4871 +lemma continuous_at_open:
1.4872 +  "continuous (at x) f \<longleftrightarrow> (\<forall>t. open t \<and> f x \<in> t --> (\<exists>s. open s \<and> x \<in> s \<and> (\<forall>x' \<in> s. (f x') \<in> t)))"
1.4873 +  unfolding continuous_within_topological [of x UNIV f]
1.4874 +  unfolding imp_conjL
1.4875 +  by (intro all_cong imp_cong ex_cong conj_cong refl) auto
1.4877 +lemma continuous_imp_tendsto:
1.4878 +  assumes "continuous (at x0) f"
1.4879 +    and "x \<longlonglongrightarrow> x0"
1.4880 +  shows "(f \<circ> x) \<longlonglongrightarrow> (f x0)"
1.4881 +proof (rule topological_tendstoI)
1.4882 +  fix S
1.4883 +  assume "open S" "f x0 \<in> S"
1.4884 +  then obtain T where T_def: "open T" "x0 \<in> T" "\<forall>x\<in>T. f x \<in> S"
1.4885 +     using assms continuous_at_open by metis
1.4886 +  then have "eventually (\<lambda>n. x n \<in> T) sequentially"
1.4887 +    using assms T_def by (auto simp: tendsto_def)
1.4888 +  then show "eventually (\<lambda>n. (f \<circ> x) n \<in> S) sequentially"
1.4889 +    using T_def by (auto elim!: eventually_mono)
1.4890 +qed
1.4892 +lemma continuous_on_open:
1.4893 +  "continuous_on S f \<longleftrightarrow>
1.4894 +    (\<forall>T. openin (subtopology euclidean (f ` S)) T \<longrightarrow>
1.4895 +      openin (subtopology euclidean S) (S \<inter> f -` T))"
1.4896 +  unfolding continuous_on_open_invariant openin_open Int_def vimage_def Int_commute
1.4897 +  by (simp add: imp_ex imageI conj_commute eq_commute cong: conj_cong)
1.4899 +lemma continuous_on_open_gen:
1.4900 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
1.4901 +  assumes "f ` S \<subseteq> T"
1.4902 +    shows "continuous_on S f \<longleftrightarrow>
1.4903 +             (\<forall>U. openin (subtopology euclidean T) U
1.4904 +                  \<longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` U))"
1.4905 +     (is "?lhs = ?rhs")
1.4906 +proof
1.4907 +  assume ?lhs
1.4908 +  then show ?rhs
1.4909 +    apply (clarsimp simp: openin_euclidean_subtopology_iff continuous_on_iff)
1.4910 +    by (metis assms image_subset_iff)
1.4911 +next
1.4912 +  have ope: "openin (subtopology euclidean T) (ball y e \<inter> T)" for y e
1.4913 +    by (simp add: Int_commute openin_open_Int)
1.4914 +  assume R [rule_format]: ?rhs
1.4915 +  show ?lhs
1.4916 +  proof (clarsimp simp add: continuous_on_iff)
1.4917 +    fix x and e::real
1.4918 +    assume "x \<in> S" and "0 < e"
1.4919 +    then have x: "x \<in> S \<inter> (f -` ball (f x) e \<inter> f -` T)"
1.4920 +      using assms by auto
1.4921 +    show "\<exists>d>0. \<forall>x'\<in>S. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
1.4922 +      using R [of "ball (f x) e \<inter> T"] x
1.4923 +      by (fastforce simp add: ope openin_euclidean_subtopology_iff [of S] dist_commute)
1.4924 +  qed
1.4925 +qed
1.4927 +lemma continuous_openin_preimage:
1.4928 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
1.4929 +  shows
1.4930 +   "\<lbrakk>continuous_on S f; f ` S \<subseteq> T; openin (subtopology euclidean T) U\<rbrakk>
1.4931 +        \<Longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` U)"
1.4934 +text \<open>Similarly in terms of closed sets.\<close>
1.4936 +lemma continuous_on_closed:
1.4937 +  "continuous_on S f \<longleftrightarrow>
1.4938 +    (\<forall>T. closedin (subtopology euclidean (f ` S)) T \<longrightarrow>
1.4939 +      closedin (subtopology euclidean S) (S \<inter> f -` T))"
1.4940 +  unfolding continuous_on_closed_invariant closedin_closed Int_def vimage_def Int_commute
1.4941 +  by (simp add: imp_ex imageI conj_commute eq_commute cong: conj_cong)
1.4943 +lemma continuous_on_closed_gen:
1.4944 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
1.4945 +  assumes "f ` S \<subseteq> T"
1.4946 +    shows "continuous_on S f \<longleftrightarrow>
1.4947 +             (\<forall>U. closedin (subtopology euclidean T) U
1.4948 +                  \<longrightarrow> closedin (subtopology euclidean S) (S \<inter> f -` U))"
1.4949 +     (is "?lhs = ?rhs")
1.4950 +proof -
1.4951 +  have *: "U \<subseteq> T \<Longrightarrow> S \<inter> f -` (T - U) = S - (S \<inter> f -` U)" for U
1.4952 +    using assms by blast
1.4953 +  show ?thesis
1.4954 +  proof
1.4955 +    assume L: ?lhs
1.4956 +    show ?rhs
1.4957 +    proof clarify
1.4958 +      fix U
1.4959 +      assume "closedin (subtopology euclidean T) U"
1.4960 +      then show "closedin (subtopology euclidean S) (S \<inter> f -` U)"
1.4961 +        using L unfolding continuous_on_open_gen [OF assms]
1.4962 +        by (metis * closedin_def inf_le1 topspace_euclidean_subtopology)
1.4963 +    qed
1.4964 +  next
1.4965 +    assume R [rule_format]: ?rhs
1.4966 +    show ?lhs
1.4967 +      unfolding continuous_on_open_gen [OF assms]
1.4968 +      by (metis * R inf_le1 openin_closedin_eq topspace_euclidean_subtopology)
1.4969 +  qed
1.4970 +qed
1.4972 +lemma continuous_closedin_preimage_gen:
1.4973 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
1.4974 +  assumes "continuous_on S f" "f ` S \<subseteq> T" "closedin (subtopology euclidean T) U"
1.4975 +    shows "closedin (subtopology euclidean S) (S \<inter> f -` U)"
1.4976 +using assms continuous_on_closed_gen by blast
1.4978 +lemma continuous_on_imp_closedin:
1.4979 +  assumes "continuous_on S f" "closedin (subtopology euclidean (f ` S)) T"
1.4980 +    shows "closedin (subtopology euclidean S) (S \<inter> f -` T)"
1.4981 +using assms continuous_on_closed by blast
1.4983 +subsection%unimportant \<open>Half-global and completely global cases\<close>
1.4985 +lemma continuous_openin_preimage_gen:
1.4986 +  assumes "continuous_on S f"  "open T"
1.4987 +  shows "openin (subtopology euclidean S) (S \<inter> f -` T)"
1.4988 +proof -
1.4989 +  have *: "(S \<inter> f -` T) = (S \<inter> f -` (T \<inter> f ` S))"
1.4990 +    by auto
1.4991 +  have "openin (subtopology euclidean (f ` S)) (T \<inter> f ` S)"
1.4992 +    using openin_open_Int[of T "f ` S", OF assms(2)] unfolding openin_open by auto
1.4993 +  then show ?thesis
1.4994 +    using assms(1)[unfolded continuous_on_open, THEN spec[where x="T \<inter> f ` S"]]
1.4995 +    using * by auto
1.4996 +qed
1.4998 +lemma continuous_closedin_preimage:
1.4999 +  assumes "continuous_on S f" and "closed T"
1.5000 +  shows "closedin (subtopology euclidean S) (S \<inter> f -` T)"
1.5001 +proof -
1.5002 +  have *: "(S \<inter> f -` T) = (S \<inter> f -` (T \<inter> f ` S))"
1.5003 +    by auto
1.5004 +  have "closedin (subtopology euclidean (f ` S)) (T \<inter> f ` S)"
1.5005 +    using closedin_closed_Int[of T "f ` S", OF assms(2)]
1.5006 +    by (simp add: Int_commute)
1.5007 +  then show ?thesis
1.5008 +    using assms(1)[unfolded continuous_on_closed, THEN spec[where x="T \<inter> f ` S"]]
1.5009 +    using * by auto
1.5010 +qed
1.5012 +lemma continuous_openin_preimage_eq:
1.5013 +   "continuous_on S f \<longleftrightarrow>
1.5014 +    (\<forall>T. open T \<longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` T))"
1.5015 +apply safe
1.5017 +apply (fastforce simp add: continuous_on_open openin_open)
1.5018 +done
1.5020 +lemma continuous_closedin_preimage_eq:
1.5021 +   "continuous_on S f \<longleftrightarrow>
1.5022 +    (\<forall>T. closed T \<longrightarrow> closedin (subtopology euclidean S) (S \<inter> f -` T))"
1.5023 +apply safe
1.5025 +apply (fastforce simp add: continuous_on_closed closedin_closed)
1.5026 +done
1.5028 +lemma continuous_open_preimage:
1.5029 +  assumes contf: "continuous_on S f" and "open S" "open T"
1.5030 +  shows "open (S \<inter> f -` T)"
1.5031 +proof-
1.5032 +  obtain U where "open U" "(S \<inter> f -` T) = S \<inter> U"
1.5033 +    using continuous_openin_preimage_gen[OF contf \<open>open T\<close>]
1.5034 +    unfolding openin_open by auto
1.5035 +  then show ?thesis
1.5036 +    using open_Int[of S U, OF \<open>open S\<close>] by auto
1.5037 +qed
1.5039 +lemma continuous_closed_preimage:
1.5040 +  assumes contf: "continuous_on S f" and "closed S" "closed T"
1.5041 +  shows "closed (S \<inter> f -` T)"
1.5042 +proof-
1.5043 +  obtain U where "closed U" "(S \<inter> f -` T) = S \<inter> U"
1.5044 +    using continuous_closedin_preimage[OF contf \<open>closed T\<close>]
1.5045 +    unfolding closedin_closed by auto
1.5046 +  then show ?thesis using closed_Int[of S U, OF \<open>closed S\<close>] by auto
1.5047 +qed
1.5049 +lemma continuous_open_vimage: "open S \<Longrightarrow> (\<And>x. continuous (at x) f) \<Longrightarrow> open (f -` S)"
1.5050 +  by (metis continuous_on_eq_continuous_within open_vimage)
1.5052 +lemma continuous_closed_vimage: "closed S \<Longrightarrow> (\<And>x. continuous (at x) f) \<Longrightarrow> closed (f -` S)"
1.5053 +  by (simp add: closed_vimage continuous_on_eq_continuous_within)
1.5055 +lemma interior_image_subset:
1.5056 +  assumes "inj f" "\<And>x. continuous (at x) f"
1.5057 +  shows "interior (f ` S) \<subseteq> f ` (interior S)"
1.5058 +proof
1.5059 +  fix x assume "x \<in> interior (f ` S)"
1.5060 +  then obtain T where as: "open T" "x \<in> T" "T \<subseteq> f ` S" ..
1.5061 +  then have "x \<in> f ` S" by auto
1.5062 +  then obtain y where y: "y \<in> S" "x = f y" by auto
1.5063 +  have "open (f -` T)"
1.5064 +    using assms \<open>open T\<close> by (simp add: continuous_at_imp_continuous_on open_vimage)
1.5065 +  moreover have "y \<in> vimage f T"
1.5066 +    using \<open>x = f y\<close> \<open>x \<in> T\<close> by simp
1.5067 +  moreover have "vimage f T \<subseteq> S"
1.5068 +    using \<open>T \<subseteq> image f S\<close> \<open>inj f\<close> unfolding inj_on_def subset_eq by auto
1.5069 +  ultimately have "y \<in> interior S" ..
1.5070 +  with \<open>x = f y\<close> show "x \<in> f ` interior S" ..
1.5071 +qed
1.5073 +subsection%unimportant \<open>Topological properties of linear functions\<close>
1.5075 +lemma linear_lim_0:
1.5076 +  assumes "bounded_linear f"
1.5077 +  shows "(f \<longlongrightarrow> 0) (at (0))"
1.5078 +proof -
1.5079 +  interpret f: bounded_linear f by fact
1.5080 +  have "(f \<longlongrightarrow> f 0) (at 0)"
1.5081 +    using tendsto_ident_at by (rule f.tendsto)
1.5082 +  then show ?thesis unfolding f.zero .
1.5083 +qed
1.5085 +lemma linear_continuous_at:
1.5086 +  assumes "bounded_linear f"
1.5087 +  shows "continuous (at a) f"
1.5088 +  unfolding continuous_at using assms
1.5089 +  apply (rule bounded_linear.tendsto)
1.5090 +  apply (rule tendsto_ident_at)
1.5091 +  done
1.5093 +lemma linear_continuous_within:
1.5094 +  "bounded_linear f \<Longrightarrow> continuous (at x within s) f"
1.5095 +  using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto
1.5097 +lemma linear_continuous_on:
1.5098 +  "bounded_linear f \<Longrightarrow> continuous_on s f"
1.5099 +  using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto
1.5101 +end
1.5102 \ No newline at end of file