most of Topology_Euclidean_Space (now Elementary_Topology) requires fewer dependencies
authorimmler
Thu Dec 27 23:38:55 2018 +0100 (4 months ago)
changeset 6951609bb8f470959
parent 69515 5bbb2bd564fa
child 69517 dc20f278e8f3
most of Topology_Euclidean_Space (now Elementary_Topology) requires fewer dependencies
src/HOL/Analysis/Conformal_Mappings.thy
src/HOL/Analysis/Elementary_Topology.thy
src/HOL/Analysis/Euclidean_Space.thy
src/HOL/Analysis/Linear_Algebra.thy
src/HOL/Analysis/Path_Connected.thy
src/HOL/Analysis/Starlike.thy
src/HOL/Analysis/Topology_Euclidean_Space.thy
src/HOL/Library/Infinite_Set.thy
     1.1 --- a/src/HOL/Analysis/Conformal_Mappings.thy	Thu Dec 27 22:54:29 2018 +0100
     1.2 +++ b/src/HOL/Analysis/Conformal_Mappings.thy	Thu Dec 27 23:38:55 2018 +0100
     1.3 @@ -3260,7 +3260,7 @@
     1.4        moreover have "isolated_singularity_at h z"
     1.5          unfolding isolated_singularity_at_def h_def
     1.6          apply (rule exI[where x=e])
     1.7 -        using e_holo e_nz \<open>e>0\<close> by (metis Topology_Euclidean_Space.open_ball analytic_on_open 
     1.8 +        using e_holo e_nz \<open>e>0\<close> by (metis open_ball analytic_on_open 
     1.9              holomorphic_on_inverse open_delete)
    1.10        ultimately show ?thesis
    1.11          using P_exist[of h] by auto
    1.12 @@ -3482,7 +3482,7 @@
    1.13  
    1.14      have [intro]: "fp \<midarrow>z\<rightarrow>fp z" "gp \<midarrow>z\<rightarrow>gp z"
    1.15          using fr(1) \<open>fr>0\<close> gr(1) \<open>gr>0\<close>
    1.16 -        by (meson Topology_Euclidean_Space.open_ball ball_subset_cball centre_in_ball 
    1.17 +        by (meson open_ball ball_subset_cball centre_in_ball 
    1.18              continuous_on_eq_continuous_at continuous_within holomorphic_on_imp_continuous_on 
    1.19              holomorphic_on_subset)+
    1.20      have ?thesis when "fn+gn>0" 
    1.21 @@ -3958,7 +3958,7 @@
    1.22        apply (elim Lim_transform_within_open[where s="ball z r"])
    1.23        using r by auto
    1.24      moreover have "g \<midarrow>z\<rightarrow>g z"
    1.25 -      by (metis (mono_tags, lifting) Topology_Euclidean_Space.open_ball at_within_open_subset 
    1.26 +      by (metis (mono_tags, lifting) open_ball at_within_open_subset 
    1.27            ball_subset_cball centre_in_ball continuous_on holomorphic_on_imp_continuous_on r(1,3) subsetCE)
    1.28      ultimately have "(\<lambda>w. (g w * (w - z) powr of_int n) / g w) \<midarrow>z\<rightarrow> f z/g z"
    1.29        apply (rule_tac tendsto_divide)
    1.30 @@ -4088,7 +4088,7 @@
    1.31      assume " \<not> n < 0"
    1.32      define c where "c=(if n=0 then g z else 0)"
    1.33      have [simp]:"g \<midarrow>z\<rightarrow> g z" 
    1.34 -      by (metis Topology_Euclidean_Space.open_ball at_within_open ball_subset_cball centre_in_ball 
    1.35 +      by (metis open_ball at_within_open ball_subset_cball centre_in_ball 
    1.36              continuous_on holomorphic_on_imp_continuous_on holomorphic_on_subset r(1) r(3) )
    1.37      have "\<forall>\<^sub>F x in at z. f x = g x * (x - z) ^ nat n"
    1.38        unfolding eventually_at_topological
    1.39 @@ -4154,7 +4154,7 @@
    1.40        then show "LIM w at z. w - z :> at 0" 
    1.41          unfolding filterlim_at by (auto intro:tendsto_eq_intros)
    1.42        show "isolated_singularity_at g z" 
    1.43 -        by (meson Diff_subset Topology_Euclidean_Space.open_ball analytic_on_holomorphic 
    1.44 +        by (meson Diff_subset open_ball analytic_on_holomorphic 
    1.45              assms(1,2,3) holomorphic_on_subset isolated_singularity_at_def openE)
    1.46      qed
    1.47      then show "not_essential f z"
    1.48 @@ -4590,7 +4590,7 @@
    1.49      assume "\<not> (\<exists>\<^sub>F w in at z. g w \<noteq> 0)"
    1.50      then have "\<forall>\<^sub>F w in nhds z. g w = 0"
    1.51        unfolding eventually_at eventually_nhds frequently_at using \<open>g z = 0\<close> 
    1.52 -      by (metis Topology_Euclidean_Space.open_ball UNIV_I centre_in_ball dist_commute mem_ball)
    1.53 +      by (metis open_ball UNIV_I centre_in_ball dist_commute mem_ball)
    1.54      then have "deriv g z = deriv (\<lambda>_. 0) z"
    1.55        by (intro deriv_cong_ev) auto
    1.56      then have "deriv g z = 0" by auto
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Analysis/Elementary_Topology.thy	Thu Dec 27 23:38:55 2018 +0100
     2.3 @@ -0,0 +1,5098 @@
     2.4 +(*  Author:     L C Paulson, University of Cambridge
     2.5 +    Author:     Amine Chaieb, University of Cambridge
     2.6 +    Author:     Robert Himmelmann, TU Muenchen
     2.7 +    Author:     Brian Huffman, Portland State University
     2.8 +*)
     2.9 +
    2.10 +section \<open>Elementary Topology\<close>
    2.11 +
    2.12 +theory Elementary_Topology
    2.13 +imports
    2.14 +  "HOL-Library.Indicator_Function"
    2.15 +  "HOL-Library.Countable_Set"
    2.16 +  "HOL-Library.FuncSet"
    2.17 +  "HOL-Library.Set_Idioms"
    2.18 +  "HOL-Library.Infinite_Set"
    2.19 +  Product_Vector
    2.20 +begin
    2.21 +
    2.22 +(* FIXME: move elsewhere *)
    2.23 +
    2.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)"
    2.25 +  apply auto
    2.26 +  apply (rule_tac x="d/2" in exI)
    2.27 +  apply auto
    2.28 +  done
    2.29 +
    2.30 +lemma approachable_lt_le2:  \<comment> \<open>like the above, but pushes aside an extra formula\<close>
    2.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)"
    2.32 +  apply auto
    2.33 +  apply (rule_tac x="d/2" in exI, auto)
    2.34 +  done
    2.35 +
    2.36 +lemma triangle_lemma:
    2.37 +  fixes x y z :: real
    2.38 +  assumes x: "0 \<le> x"
    2.39 +    and y: "0 \<le> y"
    2.40 +    and z: "0 \<le> z"
    2.41 +    and xy: "x\<^sup>2 \<le> y\<^sup>2 + z\<^sup>2"
    2.42 +  shows "x \<le> y + z"
    2.43 +proof -
    2.44 +  have "y\<^sup>2 + z\<^sup>2 \<le> y\<^sup>2 + 2 * y * z + z\<^sup>2"
    2.45 +    using z y by simp
    2.46 +  with xy have th: "x\<^sup>2 \<le> (y + z)\<^sup>2"
    2.47 +    by (simp add: power2_eq_square field_simps)
    2.48 +  from y z have yz: "y + z \<ge> 0"
    2.49 +    by arith
    2.50 +  from power2_le_imp_le[OF th yz] show ?thesis .
    2.51 +qed
    2.52 +
    2.53 +definition (in monoid_add) support_on :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'b set"
    2.54 +  where "support_on s f = {x\<in>s. f x \<noteq> 0}"
    2.55 +
    2.56 +lemma in_support_on: "x \<in> support_on s f \<longleftrightarrow> x \<in> s \<and> f x \<noteq> 0"
    2.57 +  by (simp add: support_on_def)
    2.58 +
    2.59 +lemma support_on_simps[simp]:
    2.60 +  "support_on {} f = {}"
    2.61 +  "support_on (insert x s) f =
    2.62 +    (if f x = 0 then support_on s f else insert x (support_on s f))"
    2.63 +  "support_on (s \<union> t) f = support_on s f \<union> support_on t f"
    2.64 +  "support_on (s \<inter> t) f = support_on s f \<inter> support_on t f"
    2.65 +  "support_on (s - t) f = support_on s f - support_on t f"
    2.66 +  "support_on (f ` s) g = f ` (support_on s (g \<circ> f))"
    2.67 +  unfolding support_on_def by auto
    2.68 +
    2.69 +lemma support_on_cong:
    2.70 +  "(\<And>x. x \<in> s \<Longrightarrow> f x = 0 \<longleftrightarrow> g x = 0) \<Longrightarrow> support_on s f = support_on s g"
    2.71 +  by (auto simp: support_on_def)
    2.72 +
    2.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}"
    2.74 +  by (auto simp: support_on_def)
    2.75 +
    2.76 +lemma support_on_if_subset: "support_on A (\<lambda>x. if P x then a else 0) \<subseteq> {x \<in> A. P x}"
    2.77 +  by (auto simp: support_on_def)
    2.78 +
    2.79 +lemma finite_support[intro]: "finite S \<Longrightarrow> finite (support_on S f)"
    2.80 +  unfolding support_on_def by auto
    2.81 +
    2.82 +(* TODO: is supp_sum really needed? TODO: Generalize to Finite_Set.fold *)
    2.83 +definition (in comm_monoid_add) supp_sum :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
    2.84 +  where "supp_sum f S = (\<Sum>x\<in>support_on S f. f x)"
    2.85 +
    2.86 +lemma supp_sum_empty[simp]: "supp_sum f {} = 0"
    2.87 +  unfolding supp_sum_def by auto
    2.88 +
    2.89 +lemma supp_sum_insert[simp]:
    2.90 +  "finite (support_on S f) \<Longrightarrow>
    2.91 +    supp_sum f (insert x S) = (if x \<in> S then supp_sum f S else f x + supp_sum f S)"
    2.92 +  by (simp add: supp_sum_def in_support_on insert_absorb)
    2.93 +
    2.94 +lemma supp_sum_divide_distrib: "supp_sum f A / (r::'a::field) = supp_sum (\<lambda>n. f n / r) A"
    2.95 +  by (cases "r = 0")
    2.96 +     (auto simp: supp_sum_def sum_divide_distrib intro!: sum.cong support_on_cong)
    2.97 +
    2.98 +(*END OF SUPPORT, ETC.*)
    2.99 +
   2.100 +lemma image_affinity_interval:
   2.101 +  fixes c :: "'a::ordered_real_vector"
   2.102 +  shows "((\<lambda>x. m *\<^sub>R x + c) ` {a..b}) = 
   2.103 +           (if {a..b}={} then {}
   2.104 +            else if 0 \<le> m then {m *\<^sub>R a + c .. m  *\<^sub>R b + c}
   2.105 +            else {m *\<^sub>R b + c .. m *\<^sub>R a + c})"
   2.106 +         (is "?lhs = ?rhs")
   2.107 +proof (cases "m=0")
   2.108 +  case True
   2.109 +  then show ?thesis
   2.110 +    by force
   2.111 +next
   2.112 +  case False
   2.113 +  show ?thesis
   2.114 +  proof
   2.115 +    show "?lhs \<subseteq> ?rhs"
   2.116 +      by (auto simp: scaleR_left_mono scaleR_left_mono_neg)
   2.117 +    show "?rhs \<subseteq> ?lhs"
   2.118 +    proof (clarsimp, intro conjI impI subsetI)
   2.119 +      show "\<lbrakk>0 \<le> m; a \<le> b; x \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}\<rbrakk>
   2.120 +            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
   2.121 +        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
   2.122 +        using False apply (auto simp: le_diff_eq pos_le_divideRI)
   2.123 +        using diff_le_eq pos_le_divideR_eq by force
   2.124 +      show "\<lbrakk>\<not> 0 \<le> m; a \<le> b;  x \<in> {m *\<^sub>R b + c..m *\<^sub>R a + c}\<rbrakk>
   2.125 +            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
   2.126 +        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
   2.127 +        apply (auto simp: diff_le_eq neg_le_divideR_eq)
   2.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
   2.129 +    qed
   2.130 +  qed
   2.131 +qed
   2.132 +
   2.133 +lemma countable_PiE:
   2.134 +  "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (Pi\<^sub>E I F)"
   2.135 +  by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)
   2.136 +
   2.137 +lemma open_sums:
   2.138 +  fixes T :: "('b::real_normed_vector) set"
   2.139 +  assumes "open S \<or> open T"
   2.140 +  shows "open (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
   2.141 +  using assms
   2.142 +proof
   2.143 +  assume S: "open S"
   2.144 +  show ?thesis
   2.145 +  proof (clarsimp simp: open_dist)
   2.146 +    fix x y
   2.147 +    assume "x \<in> S" "y \<in> T"
   2.148 +    with S obtain e where "e > 0" and e: "\<And>x'. dist x' x < e \<Longrightarrow> x' \<in> S"
   2.149 +      by (auto simp: open_dist)
   2.150 +    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
   2.151 +      by (metis \<open>y \<in> T\<close> diff_add_cancel dist_add_cancel2)
   2.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)"
   2.153 +      using \<open>0 < e\<close> \<open>x \<in> S\<close> by blast
   2.154 +  qed
   2.155 +next
   2.156 +  assume T: "open T"
   2.157 +  show ?thesis
   2.158 +  proof (clarsimp simp: open_dist)
   2.159 +    fix x y
   2.160 +    assume "x \<in> S" "y \<in> T"
   2.161 +    with T obtain e where "e > 0" and e: "\<And>x'. dist x' y < e \<Longrightarrow> x' \<in> T"
   2.162 +      by (auto simp: open_dist)
   2.163 +    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
   2.164 +      by (metis \<open>x \<in> S\<close> add_diff_cancel_left' add_diff_eq diff_diff_add dist_norm)
   2.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)"
   2.166 +      using \<open>0 < e\<close> \<open>y \<in> T\<close> by blast
   2.167 +  qed
   2.168 +qed
   2.169 +
   2.170 +
   2.171 +subsection \<open>Topological Basis\<close>
   2.172 +
   2.173 +context topological_space
   2.174 +begin
   2.175 +
   2.176 +definition%important "topological_basis B \<longleftrightarrow>
   2.177 +  (\<forall>b\<in>B. open b) \<and> (\<forall>x. open x \<longrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
   2.178 +
   2.179 +lemma topological_basis:
   2.180 +  "topological_basis B \<longleftrightarrow> (\<forall>x. open x \<longleftrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
   2.181 +  unfolding topological_basis_def
   2.182 +  apply safe
   2.183 +     apply fastforce
   2.184 +    apply fastforce
   2.185 +   apply (erule_tac x=x in allE, simp)
   2.186 +   apply (rule_tac x="{x}" in exI, auto)
   2.187 +  done
   2.188 +
   2.189 +lemma topological_basis_iff:
   2.190 +  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
   2.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'))"
   2.192 +    (is "_ \<longleftrightarrow> ?rhs")
   2.193 +proof safe
   2.194 +  fix O' and x::'a
   2.195 +  assume H: "topological_basis B" "open O'" "x \<in> O'"
   2.196 +  then have "(\<exists>B'\<subseteq>B. \<Union>B' = O')" by (simp add: topological_basis_def)
   2.197 +  then obtain B' where "B' \<subseteq> B" "O' = \<Union>B'" by auto
   2.198 +  then show "\<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'" using H by auto
   2.199 +next
   2.200 +  assume H: ?rhs
   2.201 +  show "topological_basis B"
   2.202 +    using assms unfolding topological_basis_def
   2.203 +  proof safe
   2.204 +    fix O' :: "'a set"
   2.205 +    assume "open O'"
   2.206 +    with H obtain f where "\<forall>x\<in>O'. f x \<in> B \<and> x \<in> f x \<and> f x \<subseteq> O'"
   2.207 +      by (force intro: bchoice simp: Bex_def)
   2.208 +    then show "\<exists>B'\<subseteq>B. \<Union>B' = O'"
   2.209 +      by (auto intro: exI[where x="{f x |x. x \<in> O'}"])
   2.210 +  qed
   2.211 +qed
   2.212 +
   2.213 +lemma topological_basisI:
   2.214 +  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
   2.215 +    and "\<And>O' x. open O' \<Longrightarrow> x \<in> O' \<Longrightarrow> \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'"
   2.216 +  shows "topological_basis B"
   2.217 +  using assms by (subst topological_basis_iff) auto
   2.218 +
   2.219 +lemma topological_basisE:
   2.220 +  fixes O'
   2.221 +  assumes "topological_basis B"
   2.222 +    and "open O'"
   2.223 +    and "x \<in> O'"
   2.224 +  obtains B' where "B' \<in> B" "x \<in> B'" "B' \<subseteq> O'"
   2.225 +proof atomize_elim
   2.226 +  from assms have "\<And>B'. B'\<in>B \<Longrightarrow> open B'"
   2.227 +    by (simp add: topological_basis_def)
   2.228 +  with topological_basis_iff assms
   2.229 +  show  "\<exists>B'. B' \<in> B \<and> x \<in> B' \<and> B' \<subseteq> O'"
   2.230 +    using assms by (simp add: Bex_def)
   2.231 +qed
   2.232 +
   2.233 +lemma topological_basis_open:
   2.234 +  assumes "topological_basis B"
   2.235 +    and "X \<in> B"
   2.236 +  shows "open X"
   2.237 +  using assms by (simp add: topological_basis_def)
   2.238 +
   2.239 +lemma topological_basis_imp_subbasis:
   2.240 +  assumes B: "topological_basis B"
   2.241 +  shows "open = generate_topology B"
   2.242 +proof (intro ext iffI)
   2.243 +  fix S :: "'a set"
   2.244 +  assume "open S"
   2.245 +  with B obtain B' where "B' \<subseteq> B" "S = \<Union>B'"
   2.246 +    unfolding topological_basis_def by blast
   2.247 +  then show "generate_topology B S"
   2.248 +    by (auto intro: generate_topology.intros dest: topological_basis_open)
   2.249 +next
   2.250 +  fix S :: "'a set"
   2.251 +  assume "generate_topology B S"
   2.252 +  then show "open S"
   2.253 +    by induct (auto dest: topological_basis_open[OF B])
   2.254 +qed
   2.255 +
   2.256 +lemma basis_dense:
   2.257 +  fixes B :: "'a set set"
   2.258 +    and f :: "'a set \<Rightarrow> 'a"
   2.259 +  assumes "topological_basis B"
   2.260 +    and choosefrom_basis: "\<And>B'. B' \<noteq> {} \<Longrightarrow> f B' \<in> B'"
   2.261 +  shows "\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>B' \<in> B. f B' \<in> X)"
   2.262 +proof (intro allI impI)
   2.263 +  fix X :: "'a set"
   2.264 +  assume "open X" and "X \<noteq> {}"
   2.265 +  from topological_basisE[OF \<open>topological_basis B\<close> \<open>open X\<close> choosefrom_basis[OF \<open>X \<noteq> {}\<close>]]
   2.266 +  obtain B' where "B' \<in> B" "f X \<in> B'" "B' \<subseteq> X" .
   2.267 +  then show "\<exists>B'\<in>B. f B' \<in> X"
   2.268 +    by (auto intro!: choosefrom_basis)
   2.269 +qed
   2.270 +
   2.271 +end
   2.272 +
   2.273 +lemma topological_basis_prod:
   2.274 +  assumes A: "topological_basis A"
   2.275 +    and B: "topological_basis B"
   2.276 +  shows "topological_basis ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))"
   2.277 +  unfolding topological_basis_def
   2.278 +proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric])
   2.279 +  fix S :: "('a \<times> 'b) set"
   2.280 +  assume "open S"
   2.281 +  then show "\<exists>X\<subseteq>A \<times> B. (\<Union>(a,b)\<in>X. a \<times> b) = S"
   2.282 +  proof (safe intro!: exI[of _ "{x\<in>A \<times> B. fst x \<times> snd x \<subseteq> S}"])
   2.283 +    fix x y
   2.284 +    assume "(x, y) \<in> S"
   2.285 +    from open_prod_elim[OF \<open>open S\<close> this]
   2.286 +    obtain a b where a: "open a""x \<in> a" and b: "open b" "y \<in> b" and "a \<times> b \<subseteq> S"
   2.287 +      by (metis mem_Sigma_iff)
   2.288 +    moreover
   2.289 +    from A a obtain A0 where "A0 \<in> A" "x \<in> A0" "A0 \<subseteq> a"
   2.290 +      by (rule topological_basisE)
   2.291 +    moreover
   2.292 +    from B b obtain B0 where "B0 \<in> B" "y \<in> B0" "B0 \<subseteq> b"
   2.293 +      by (rule topological_basisE)
   2.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)"
   2.295 +      by (intro UN_I[of "(A0, B0)"]) auto
   2.296 +  qed auto
   2.297 +qed (metis A B topological_basis_open open_Times)
   2.298 +
   2.299 +
   2.300 +subsection \<open>Countable Basis\<close>
   2.301 +
   2.302 +locale%important countable_basis =
   2.303 +  fixes B :: "'a::topological_space set set"
   2.304 +  assumes is_basis: "topological_basis B"
   2.305 +    and countable_basis: "countable B"
   2.306 +begin
   2.307 +
   2.308 +lemma open_countable_basis_ex:
   2.309 +  assumes "open X"
   2.310 +  shows "\<exists>B' \<subseteq> B. X = \<Union>B'"
   2.311 +  using assms countable_basis is_basis
   2.312 +  unfolding topological_basis_def by blast
   2.313 +
   2.314 +lemma open_countable_basisE:
   2.315 +  assumes "open X"
   2.316 +  obtains B' where "B' \<subseteq> B" "X = \<Union>B'"
   2.317 +  using assms open_countable_basis_ex
   2.318 +  by atomize_elim simp
   2.319 +
   2.320 +lemma countable_dense_exists:
   2.321 +  "\<exists>D::'a set. countable D \<and> (\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>d \<in> D. d \<in> X))"
   2.322 +proof -
   2.323 +  let ?f = "(\<lambda>B'. SOME x. x \<in> B')"
   2.324 +  have "countable (?f ` B)" using countable_basis by simp
   2.325 +  with basis_dense[OF is_basis, of ?f] show ?thesis
   2.326 +    by (intro exI[where x="?f ` B"]) (metis (mono_tags) all_not_in_conv imageI someI)
   2.327 +qed
   2.328 +
   2.329 +lemma countable_dense_setE:
   2.330 +  obtains D :: "'a set"
   2.331 +  where "countable D" "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d \<in> D. d \<in> X"
   2.332 +  using countable_dense_exists by blast
   2.333 +
   2.334 +end
   2.335 +
   2.336 +lemma (in first_countable_topology) first_countable_basisE:
   2.337 +  fixes x :: 'a
   2.338 +  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
   2.339 +    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
   2.340 +proof -
   2.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))"
   2.342 +    using first_countable_basis[of x] by metis
   2.343 +  show thesis
   2.344 +  proof 
   2.345 +    show "countable (range \<A>)"
   2.346 +      by simp
   2.347 +  qed (use \<A> in auto)
   2.348 +qed
   2.349 +
   2.350 +lemma (in first_countable_topology) first_countable_basis_Int_stableE:
   2.351 +  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
   2.352 +    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
   2.353 +    "\<And>A B. A \<in> \<A> \<Longrightarrow> B \<in> \<A> \<Longrightarrow> A \<inter> B \<in> \<A>"
   2.354 +proof atomize_elim
   2.355 +  obtain \<B> where \<B>:
   2.356 +    "countable \<B>"
   2.357 +    "\<And>B. B \<in> \<B> \<Longrightarrow> x \<in> B"
   2.358 +    "\<And>B. B \<in> \<B> \<Longrightarrow> open B"
   2.359 +    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>B\<in>\<B>. B \<subseteq> S"
   2.360 +    by (rule first_countable_basisE) blast
   2.361 +  define \<A> where [abs_def]:
   2.362 +    "\<A> = (\<lambda>N. \<Inter>((\<lambda>n. from_nat_into \<B> n) ` N)) ` (Collect finite::nat set set)"
   2.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>
   2.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>)"
   2.365 +  proof (safe intro!: exI[where x=\<A>])
   2.366 +    show "countable \<A>"
   2.367 +      unfolding \<A>_def by (intro countable_image countable_Collect_finite)
   2.368 +    fix A
   2.369 +    assume "A \<in> \<A>"
   2.370 +    then show "x \<in> A" "open A"
   2.371 +      using \<B>(4)[OF open_UNIV] by (auto simp: \<A>_def intro: \<B> from_nat_into)
   2.372 +  next
   2.373 +    let ?int = "\<lambda>N. \<Inter>(from_nat_into \<B> ` N)"
   2.374 +    fix A B
   2.375 +    assume "A \<in> \<A>" "B \<in> \<A>"
   2.376 +    then obtain N M where "A = ?int N" "B = ?int M" "finite (N \<union> M)"
   2.377 +      by (auto simp: \<A>_def)
   2.378 +    then show "A \<inter> B \<in> \<A>"
   2.379 +      by (auto simp: \<A>_def intro!: image_eqI[where x="N \<union> M"])
   2.380 +  next
   2.381 +    fix S
   2.382 +    assume "open S" "x \<in> S"
   2.383 +    then obtain a where a: "a\<in>\<B>" "a \<subseteq> S" using \<B> by blast
   2.384 +    then show "\<exists>a\<in>\<A>. a \<subseteq> S" using a \<B>
   2.385 +      by (intro bexI[where x=a]) (auto simp: \<A>_def intro: image_eqI[where x="{to_nat_on \<B> a}"])
   2.386 +  qed
   2.387 +qed
   2.388 +
   2.389 +lemma (in topological_space) first_countableI:
   2.390 +  assumes "countable \<A>"
   2.391 +    and 1: "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
   2.392 +    and 2: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>A\<in>\<A>. A \<subseteq> S"
   2.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))"
   2.394 +proof (safe intro!: exI[of _ "from_nat_into \<A>"])
   2.395 +  fix i
   2.396 +  have "\<A> \<noteq> {}" using 2[of UNIV] by auto
   2.397 +  show "x \<in> from_nat_into \<A> i" "open (from_nat_into \<A> i)"
   2.398 +    using range_from_nat_into_subset[OF \<open>\<A> \<noteq> {}\<close>] 1 by auto
   2.399 +next
   2.400 +  fix S
   2.401 +  assume "open S" "x\<in>S" from 2[OF this]
   2.402 +  show "\<exists>i. from_nat_into \<A> i \<subseteq> S"
   2.403 +    using subset_range_from_nat_into[OF \<open>countable \<A>\<close>] by auto
   2.404 +qed
   2.405 +
   2.406 +instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology
   2.407 +proof
   2.408 +  fix x :: "'a \<times> 'b"
   2.409 +  obtain \<A> where \<A>:
   2.410 +      "countable \<A>"
   2.411 +      "\<And>a. a \<in> \<A> \<Longrightarrow> fst x \<in> a"
   2.412 +      "\<And>a. a \<in> \<A> \<Longrightarrow> open a"
   2.413 +      "\<And>S. open S \<Longrightarrow> fst x \<in> S \<Longrightarrow> \<exists>a\<in>\<A>. a \<subseteq> S"
   2.414 +    by (rule first_countable_basisE[of "fst x"]) blast
   2.415 +  obtain B where B:
   2.416 +      "countable B"
   2.417 +      "\<And>a. a \<in> B \<Longrightarrow> snd x \<in> a"
   2.418 +      "\<And>a. a \<in> B \<Longrightarrow> open a"
   2.419 +      "\<And>S. open S \<Longrightarrow> snd x \<in> S \<Longrightarrow> \<exists>a\<in>B. a \<subseteq> S"
   2.420 +    by (rule first_countable_basisE[of "snd x"]) blast
   2.421 +  show "\<exists>\<A>::nat \<Rightarrow> ('a \<times> 'b) set.
   2.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))"
   2.423 +  proof (rule first_countableI[of "(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B)"], safe)
   2.424 +    fix a b
   2.425 +    assume x: "a \<in> \<A>" "b \<in> B"
   2.426 +    show "x \<in> a \<times> b" 
   2.427 +      by (simp add: \<A>(2) B(2) mem_Times_iff x)
   2.428 +    show "open (a \<times> b)"
   2.429 +      by (simp add: \<A>(3) B(3) open_Times x)
   2.430 +  next
   2.431 +    fix S
   2.432 +    assume "open S" "x \<in> S"
   2.433 +    then obtain a' b' where a'b': "open a'" "open b'" "x \<in> a' \<times> b'" "a' \<times> b' \<subseteq> S"
   2.434 +      by (rule open_prod_elim)
   2.435 +    moreover
   2.436 +    from a'b' \<A>(4)[of a'] B(4)[of b']
   2.437 +    obtain a b where "a \<in> \<A>" "a \<subseteq> a'" "b \<in> B" "b \<subseteq> b'"
   2.438 +      by auto
   2.439 +    ultimately
   2.440 +    show "\<exists>a\<in>(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B). a \<subseteq> S"
   2.441 +      by (auto intro!: bexI[of _ "a \<times> b"] bexI[of _ a] bexI[of _ b])
   2.442 +  qed (simp add: \<A> B)
   2.443 +qed
   2.444 +
   2.445 +class second_countable_topology = topological_space +
   2.446 +  assumes ex_countable_subbasis:
   2.447 +    "\<exists>B::'a::topological_space set set. countable B \<and> open = generate_topology B"
   2.448 +begin
   2.449 +
   2.450 +lemma ex_countable_basis: "\<exists>B::'a set set. countable B \<and> topological_basis B"
   2.451 +proof -
   2.452 +  from ex_countable_subbasis obtain B where B: "countable B" "open = generate_topology B"
   2.453 +    by blast
   2.454 +  let ?B = "Inter ` {b. finite b \<and> b \<subseteq> B }"
   2.455 +
   2.456 +  show ?thesis
   2.457 +  proof (intro exI conjI)
   2.458 +    show "countable ?B"
   2.459 +      by (intro countable_image countable_Collect_finite_subset B)
   2.460 +    {
   2.461 +      fix S
   2.462 +      assume "open S"
   2.463 +      then have "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. (\<Union>b\<in>B'. \<Inter>b) = S"
   2.464 +        unfolding B
   2.465 +      proof induct
   2.466 +        case UNIV
   2.467 +        show ?case by (intro exI[of _ "{{}}"]) simp
   2.468 +      next
   2.469 +        case (Int a b)
   2.470 +        then obtain x y where x: "a = \<Union>(Inter ` x)" "\<And>i. i \<in> x \<Longrightarrow> finite i \<and> i \<subseteq> B"
   2.471 +          and y: "b = \<Union>(Inter ` y)" "\<And>i. i \<in> y \<Longrightarrow> finite i \<and> i \<subseteq> B"
   2.472 +          by blast
   2.473 +        show ?case
   2.474 +          unfolding x y Int_UN_distrib2
   2.475 +          by (intro exI[of _ "{i \<union> j| i j.  i \<in> x \<and> j \<in> y}"]) (auto dest: x(2) y(2))
   2.476 +      next
   2.477 +        case (UN K)
   2.478 +        then have "\<forall>k\<in>K. \<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = k" by auto
   2.479 +        then obtain k where
   2.480 +            "\<forall>ka\<in>K. k ka \<subseteq> {b. finite b \<and> b \<subseteq> B} \<and> \<Union>(Inter ` (k ka)) = ka"
   2.481 +          unfolding bchoice_iff ..
   2.482 +        then show "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = \<Union>K"
   2.483 +          by (intro exI[of _ "\<Union>(k ` K)"]) auto
   2.484 +      next
   2.485 +        case (Basis S)
   2.486 +        then show ?case
   2.487 +          by (intro exI[of _ "{{S}}"]) auto
   2.488 +      qed
   2.489 +      then have "(\<exists>B'\<subseteq>Inter ` {b. finite b \<and> b \<subseteq> B}. \<Union>B' = S)"
   2.490 +        unfolding subset_image_iff by blast }
   2.491 +    then show "topological_basis ?B"
   2.492 +      unfolding topological_space_class.topological_basis_def
   2.493 +      by (safe intro!: topological_space_class.open_Inter)
   2.494 +         (simp_all add: B generate_topology.Basis subset_eq)
   2.495 +  qed
   2.496 +qed
   2.497 +
   2.498 +end
   2.499 +
   2.500 +sublocale second_countable_topology <
   2.501 +  countable_basis "SOME B. countable B \<and> topological_basis B"
   2.502 +  using someI_ex[OF ex_countable_basis]
   2.503 +  by unfold_locales safe
   2.504 +
   2.505 +instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology
   2.506 +proof
   2.507 +  obtain A :: "'a set set" where "countable A" "topological_basis A"
   2.508 +    using ex_countable_basis by auto
   2.509 +  moreover
   2.510 +  obtain B :: "'b set set" where "countable B" "topological_basis B"
   2.511 +    using ex_countable_basis by auto
   2.512 +  ultimately show "\<exists>B::('a \<times> 'b) set set. countable B \<and> open = generate_topology B"
   2.513 +    by (auto intro!: exI[of _ "(\<lambda>(a, b). a \<times> b) ` (A \<times> B)"] topological_basis_prod
   2.514 +      topological_basis_imp_subbasis)
   2.515 +qed
   2.516 +
   2.517 +instance second_countable_topology \<subseteq> first_countable_topology
   2.518 +proof
   2.519 +  fix x :: 'a
   2.520 +  define B :: "'a set set" where "B = (SOME B. countable B \<and> topological_basis B)"
   2.521 +  then have B: "countable B" "topological_basis B"
   2.522 +    using countable_basis is_basis
   2.523 +    by (auto simp: countable_basis is_basis)
   2.524 +  then show "\<exists>A::nat \<Rightarrow> 'a set.
   2.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))"
   2.526 +    by (intro first_countableI[of "{b\<in>B. x \<in> b}"])
   2.527 +       (fastforce simp: topological_space_class.topological_basis_def)+
   2.528 +qed
   2.529 +
   2.530 +instance nat :: second_countable_topology
   2.531 +proof
   2.532 +  show "\<exists>B::nat set set. countable B \<and> open = generate_topology B"
   2.533 +    by (intro exI[of _ "range lessThan \<union> range greaterThan"]) (auto simp: open_nat_def)
   2.534 +qed
   2.535 +
   2.536 +lemma countable_separating_set_linorder1:
   2.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))"
   2.538 +proof -
   2.539 +  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
   2.540 +  define B1 where "B1 = {(LEAST x. x \<in> U)| U. U \<in> A}"
   2.541 +  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   2.542 +  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
   2.543 +  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   2.544 +  have "\<exists>b \<in> B1 \<union> B2. x < b \<and> b \<le> y" if "x < y" for x y
   2.545 +  proof (cases)
   2.546 +    assume "\<exists>z. x < z \<and> z < y"
   2.547 +    then obtain z where z: "x < z \<and> z < y" by auto
   2.548 +    define U where "U = {x<..<y}"
   2.549 +    then have "open U" by simp
   2.550 +    moreover have "z \<in> U" using z U_def by simp
   2.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
   2.552 +    define w where "w = (SOME x. x \<in> V)"
   2.553 +    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
   2.554 +    then have "x < w \<and> w \<le> y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
   2.555 +    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
   2.556 +    ultimately show ?thesis by auto
   2.557 +  next
   2.558 +    assume "\<not>(\<exists>z. x < z \<and> z < y)"
   2.559 +    then have *: "\<And>z. z > x \<Longrightarrow> z \<ge> y" by auto
   2.560 +    define U where "U = {x<..}"
   2.561 +    then have "open U" by simp
   2.562 +    moreover have "y \<in> U" using \<open>x < y\<close> U_def by simp
   2.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
   2.564 +    have "U = {y..}" unfolding U_def using * \<open>x < y\<close> by auto
   2.565 +    then have "V \<subseteq> {y..}" using \<open>V \<subseteq> U\<close> by simp
   2.566 +    then have "(LEAST w. w \<in> V) = y" using \<open>y \<in> V\<close> by (meson Least_equality atLeast_iff subsetCE)
   2.567 +    then have "y \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
   2.568 +    moreover have "x < y \<and> y \<le> y" using \<open>x < y\<close> by simp
   2.569 +    ultimately show ?thesis by auto
   2.570 +  qed
   2.571 +  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
   2.572 +  ultimately show ?thesis by auto
   2.573 +qed
   2.574 +
   2.575 +lemma countable_separating_set_linorder2:
   2.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))"
   2.577 +proof -
   2.578 +  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
   2.579 +  define B1 where "B1 = {(GREATEST x. x \<in> U) | U. U \<in> A}"
   2.580 +  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   2.581 +  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
   2.582 +  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   2.583 +  have "\<exists>b \<in> B1 \<union> B2. x \<le> b \<and> b < y" if "x < y" for x y
   2.584 +  proof (cases)
   2.585 +    assume "\<exists>z. x < z \<and> z < y"
   2.586 +    then obtain z where z: "x < z \<and> z < y" by auto
   2.587 +    define U where "U = {x<..<y}"
   2.588 +    then have "open U" by simp
   2.589 +    moreover have "z \<in> U" using z U_def by simp
   2.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
   2.591 +    define w where "w = (SOME x. x \<in> V)"
   2.592 +    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
   2.593 +    then have "x \<le> w \<and> w < y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
   2.594 +    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
   2.595 +    ultimately show ?thesis by auto
   2.596 +  next
   2.597 +    assume "\<not>(\<exists>z. x < z \<and> z < y)"
   2.598 +    then have *: "\<And>z. z < y \<Longrightarrow> z \<le> x" using leI by blast
   2.599 +    define U where "U = {..<y}"
   2.600 +    then have "open U" by simp
   2.601 +    moreover have "x \<in> U" using \<open>x < y\<close> U_def by simp
   2.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
   2.603 +    have "U = {..x}" unfolding U_def using * \<open>x < y\<close> by auto
   2.604 +    then have "V \<subseteq> {..x}" using \<open>V \<subseteq> U\<close> by simp
   2.605 +    then have "(GREATEST x. x \<in> V) = x" using \<open>x \<in> V\<close> by (meson Greatest_equality atMost_iff subsetCE)
   2.606 +    then have "x \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
   2.607 +    moreover have "x \<le> x \<and> x < y" using \<open>x < y\<close> by simp
   2.608 +    ultimately show ?thesis by auto
   2.609 +  qed
   2.610 +  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
   2.611 +  ultimately show ?thesis by auto
   2.612 +qed
   2.613 +
   2.614 +lemma countable_separating_set_dense_linorder:
   2.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))"
   2.616 +proof -
   2.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)"
   2.618 +    using countable_separating_set_linorder1 by auto
   2.619 +  have "\<exists>b \<in> B. x < b \<and> b < y" if "x < y" for x y
   2.620 +  proof -
   2.621 +    obtain z where "x < z" "z < y" using \<open>x < y\<close> dense by blast
   2.622 +    then obtain b where "b \<in> B" "x < b \<and> b \<le> z" using B(2) by auto
   2.623 +    then have "x < b \<and> b < y" using \<open>z < y\<close> by auto
   2.624 +    then show ?thesis using \<open>b \<in> B\<close> by auto
   2.625 +  qed
   2.626 +  then show ?thesis using B(1) by auto
   2.627 +qed
   2.628 +
   2.629 +subsection%important \<open>Polish spaces\<close>
   2.630 +
   2.631 +text \<open>Textbooks define Polish spaces as completely metrizable.
   2.632 +  We assume the topology to be complete for a given metric.\<close>
   2.633 +
   2.634 +class polish_space = complete_space + second_countable_topology
   2.635 +
   2.636 +subsection \<open>General notion of a topology as a value\<close>
   2.637 +
   2.638 +definition%important "istopology L \<longleftrightarrow>
   2.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))"
   2.640 +
   2.641 +typedef%important 'a topology = "{L::('a set) \<Rightarrow> bool. istopology L}"
   2.642 +  morphisms "openin" "topology"
   2.643 +  unfolding istopology_def by blast
   2.644 +
   2.645 +lemma istopology_openin[intro]: "istopology(openin U)"
   2.646 +  using openin[of U] by blast
   2.647 +
   2.648 +lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
   2.649 +  using topology_inverse[unfolded mem_Collect_eq] .
   2.650 +
   2.651 +lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
   2.652 +  using topology_inverse[of U] istopology_openin[of "topology U"] by auto
   2.653 +
   2.654 +lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
   2.655 +proof
   2.656 +  assume "T1 = T2"
   2.657 +  then show "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp
   2.658 +next
   2.659 +  assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
   2.660 +  then have "openin T1 = openin T2" by (simp add: fun_eq_iff)
   2.661 +  then have "topology (openin T1) = topology (openin T2)" by simp
   2.662 +  then show "T1 = T2" unfolding openin_inverse .
   2.663 +qed
   2.664 +
   2.665 +
   2.666 +text\<open>The "universe": the union of all sets in the topology.\<close>
   2.667 +definition "topspace T = \<Union>{S. openin T S}"
   2.668 +
   2.669 +subsubsection \<open>Main properties of open sets\<close>
   2.670 +
   2.671 +proposition openin_clauses:
   2.672 +  fixes U :: "'a topology"
   2.673 +  shows
   2.674 +    "openin U {}"
   2.675 +    "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
   2.676 +    "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
   2.677 +  using openin[of U] unfolding istopology_def mem_Collect_eq by fast+
   2.678 +
   2.679 +lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
   2.680 +  unfolding topspace_def by blast
   2.681 +
   2.682 +lemma openin_empty[simp]: "openin U {}"
   2.683 +  by (rule openin_clauses)
   2.684 +
   2.685 +lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
   2.686 +  by (rule openin_clauses)
   2.687 +
   2.688 +lemma openin_Union[intro]: "(\<And>S. S \<in> K \<Longrightarrow> openin U S) \<Longrightarrow> openin U (\<Union>K)"
   2.689 +  using openin_clauses by blast
   2.690 +
   2.691 +lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
   2.692 +  using openin_Union[of "{S,T}" U] by auto
   2.693 +
   2.694 +lemma openin_topspace[intro, simp]: "openin U (topspace U)"
   2.695 +  by (force simp: openin_Union topspace_def)
   2.696 +
   2.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)"
   2.698 +  (is "?lhs \<longleftrightarrow> ?rhs")
   2.699 +proof
   2.700 +  assume ?lhs
   2.701 +  then show ?rhs by auto
   2.702 +next
   2.703 +  assume H: ?rhs
   2.704 +  let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"
   2.705 +  have "openin U ?t" by (force simp: openin_Union)
   2.706 +  also have "?t = S" using H by auto
   2.707 +  finally show "openin U S" .
   2.708 +qed
   2.709 +
   2.710 +lemma openin_INT [intro]:
   2.711 +  assumes "finite I"
   2.712 +          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
   2.713 +  shows "openin T ((\<Inter>i \<in> I. U i) \<inter> topspace T)"
   2.714 +using assms by (induct, auto simp: inf_sup_aci(2) openin_Int)
   2.715 +
   2.716 +lemma openin_INT2 [intro]:
   2.717 +  assumes "finite I" "I \<noteq> {}"
   2.718 +          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
   2.719 +  shows "openin T (\<Inter>i \<in> I. U i)"
   2.720 +proof -
   2.721 +  have "(\<Inter>i \<in> I. U i) \<subseteq> topspace T"
   2.722 +    using \<open>I \<noteq> {}\<close> openin_subset[OF assms(3)] by auto
   2.723 +  then show ?thesis
   2.724 +    using openin_INT[of _ _ U, OF assms(1) assms(3)] by (simp add: inf.absorb2 inf_commute)
   2.725 +qed
   2.726 +
   2.727 +lemma openin_Inter [intro]:
   2.728 +  assumes "finite \<F>" "\<F> \<noteq> {}" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (\<Inter>\<F>)"
   2.729 +  by (metis (full_types) assms openin_INT2 image_ident)
   2.730 +
   2.731 +lemma openin_Int_Inter:
   2.732 +  assumes "finite \<F>" "openin T U" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (U \<inter> \<Inter>\<F>)"
   2.733 +  using openin_Inter [of "insert U \<F>"] assms by auto
   2.734 +
   2.735 +
   2.736 +subsubsection \<open>Closed sets\<close>
   2.737 +
   2.738 +definition%important "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
   2.739 +
   2.740 +lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U"
   2.741 +  by (metis closedin_def)
   2.742 +
   2.743 +lemma closedin_empty[simp]: "closedin U {}"
   2.744 +  by (simp add: closedin_def)
   2.745 +
   2.746 +lemma closedin_topspace[intro, simp]: "closedin U (topspace U)"
   2.747 +  by (simp add: closedin_def)
   2.748 +
   2.749 +lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
   2.750 +  by (auto simp: Diff_Un closedin_def)
   2.751 +
   2.752 +lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union>{A - s|s. s\<in>S}"
   2.753 +  by auto
   2.754 +
   2.755 +lemma closedin_Union:
   2.756 +  assumes "finite S" "\<And>T. T \<in> S \<Longrightarrow> closedin U T"
   2.757 +    shows "closedin U (\<Union>S)"
   2.758 +  using assms by induction auto
   2.759 +
   2.760 +lemma closedin_Inter[intro]:
   2.761 +  assumes Ke: "K \<noteq> {}"
   2.762 +    and Kc: "\<And>S. S \<in>K \<Longrightarrow> closedin U S"
   2.763 +  shows "closedin U (\<Inter>K)"
   2.764 +  using Ke Kc unfolding closedin_def Diff_Inter by auto
   2.765 +
   2.766 +lemma closedin_INT[intro]:
   2.767 +  assumes "A \<noteq> {}" "\<And>x. x \<in> A \<Longrightarrow> closedin U (B x)"
   2.768 +  shows "closedin U (\<Inter>x\<in>A. B x)"
   2.769 +  apply (rule closedin_Inter)
   2.770 +  using assms
   2.771 +  apply auto
   2.772 +  done
   2.773 +
   2.774 +lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
   2.775 +  using closedin_Inter[of "{S,T}" U] by auto
   2.776 +
   2.777 +lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
   2.778 +  apply (auto simp: closedin_def Diff_Diff_Int inf_absorb2)
   2.779 +  apply (metis openin_subset subset_eq)
   2.780 +  done
   2.781 +
   2.782 +lemma openin_closedin: "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
   2.783 +  by (simp add: openin_closedin_eq)
   2.784 +
   2.785 +lemma openin_diff[intro]:
   2.786 +  assumes oS: "openin U S"
   2.787 +    and cT: "closedin U T"
   2.788 +  shows "openin U (S - T)"
   2.789 +proof -
   2.790 +  have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
   2.791 +    by (auto simp: topspace_def openin_subset)
   2.792 +  then show ?thesis using oS cT
   2.793 +    by (auto simp: closedin_def)
   2.794 +qed
   2.795 +
   2.796 +lemma closedin_diff[intro]:
   2.797 +  assumes oS: "closedin U S"
   2.798 +    and cT: "openin U T"
   2.799 +  shows "closedin U (S - T)"
   2.800 +proof -
   2.801 +  have "S - T = S \<inter> (topspace U - T)"
   2.802 +    using closedin_subset[of U S] oS cT by (auto simp: topspace_def)
   2.803 +  then show ?thesis
   2.804 +    using oS cT by (auto simp: openin_closedin_eq)
   2.805 +qed
   2.806 +
   2.807 +
   2.808 +subsection\<open>The discrete topology\<close>
   2.809 +
   2.810 +definition discrete_topology where "discrete_topology U \<equiv> topology (\<lambda>S. S \<subseteq> U)"
   2.811 +
   2.812 +lemma openin_discrete_topology [simp]: "openin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
   2.813 +proof -
   2.814 +  have "istopology (\<lambda>S. S \<subseteq> U)"
   2.815 +    by (auto simp: istopology_def)
   2.816 +  then show ?thesis
   2.817 +    by (simp add: discrete_topology_def topology_inverse')
   2.818 +qed
   2.819 +
   2.820 +lemma topspace_discrete_topology [simp]: "topspace(discrete_topology U) = U"
   2.821 +  by (meson openin_discrete_topology openin_subset openin_topspace order_refl subset_antisym)
   2.822 +
   2.823 +lemma closedin_discrete_topology [simp]: "closedin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
   2.824 +  by (simp add: closedin_def)
   2.825 +
   2.826 +lemma discrete_topology_unique:
   2.827 +   "discrete_topology U = X \<longleftrightarrow> topspace X = U \<and> (\<forall>x \<in> U. openin X {x})" (is "?lhs = ?rhs")
   2.828 +proof
   2.829 +  assume R: ?rhs
   2.830 +  then have "openin X S" if "S \<subseteq> U" for S
   2.831 +    using openin_subopen subsetD that by fastforce
   2.832 +  moreover have "x \<in> topspace X" if "openin X S" and "x \<in> S" for x S
   2.833 +    using openin_subset that by blast
   2.834 +  ultimately
   2.835 +  show ?lhs
   2.836 +    using R by (auto simp: topology_eq)
   2.837 +qed auto
   2.838 +
   2.839 +lemma discrete_topology_unique_alt:
   2.840 +  "discrete_topology U = X \<longleftrightarrow> topspace X \<subseteq> U \<and> (\<forall>x \<in> U. openin X {x})"
   2.841 +  using openin_subset
   2.842 +  by (auto simp: discrete_topology_unique)
   2.843 +
   2.844 +lemma subtopology_eq_discrete_topology_empty:
   2.845 +   "X = discrete_topology {} \<longleftrightarrow> topspace X = {}"
   2.846 +  using discrete_topology_unique [of "{}" X] by auto
   2.847 +
   2.848 +lemma subtopology_eq_discrete_topology_sing:
   2.849 +   "X = discrete_topology {a} \<longleftrightarrow> topspace X = {a}"
   2.850 +  by (metis discrete_topology_unique openin_topspace singletonD)
   2.851 +
   2.852 +
   2.853 +subsection \<open>Subspace topology\<close>
   2.854 +
   2.855 +definition%important "subtopology U V = topology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
   2.856 +
   2.857 +lemma istopology_subtopology: "istopology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
   2.858 +  (is "istopology ?L")
   2.859 +proof -
   2.860 +  have "?L {}" by blast
   2.861 +  {
   2.862 +    fix A B
   2.863 +    assume A: "?L A" and B: "?L B"
   2.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"
   2.865 +      by blast
   2.866 +    have "A \<inter> B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"
   2.867 +      using Sa Sb by blast+
   2.868 +    then have "?L (A \<inter> B)" by blast
   2.869 +  }
   2.870 +  moreover
   2.871 +  {
   2.872 +    fix K
   2.873 +    assume K: "K \<subseteq> Collect ?L"
   2.874 +    have th0: "Collect ?L = (\<lambda>S. S \<inter> V) ` Collect (openin U)"
   2.875 +      by blast
   2.876 +    from K[unfolded th0 subset_image_iff]
   2.877 +    obtain Sk where Sk: "Sk \<subseteq> Collect (openin U)" "K = (\<lambda>S. S \<inter> V) ` Sk"
   2.878 +      by blast
   2.879 +    have "\<Union>K = (\<Union>Sk) \<inter> V"
   2.880 +      using Sk by auto
   2.881 +    moreover have "openin U (\<Union>Sk)"
   2.882 +      using Sk by (auto simp: subset_eq)
   2.883 +    ultimately have "?L (\<Union>K)" by blast
   2.884 +  }
   2.885 +  ultimately show ?thesis
   2.886 +    unfolding subset_eq mem_Collect_eq istopology_def by auto
   2.887 +qed
   2.888 +
   2.889 +lemma openin_subtopology: "openin (subtopology U V) S \<longleftrightarrow> (\<exists>T. openin U T \<and> S = T \<inter> V)"
   2.890 +  unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
   2.891 +  by auto
   2.892 +
   2.893 +lemma openin_subtopology_Int:
   2.894 +   "openin X S \<Longrightarrow> openin (subtopology X T) (S \<inter> T)"
   2.895 +  using openin_subtopology by auto
   2.896 +
   2.897 +lemma openin_subtopology_Int2:
   2.898 +   "openin X T \<Longrightarrow> openin (subtopology X S) (S \<inter> T)"
   2.899 +  using openin_subtopology by auto
   2.900 +
   2.901 +lemma openin_subtopology_diff_closed:
   2.902 +   "\<lbrakk>S \<subseteq> topspace X; closedin X T\<rbrakk> \<Longrightarrow> openin (subtopology X S) (S - T)"
   2.903 +  unfolding closedin_def openin_subtopology
   2.904 +  by (rule_tac x="topspace X - T" in exI) auto
   2.905 +
   2.906 +lemma openin_relative_to: "(openin X relative_to S) = openin (subtopology X S)"
   2.907 +  by (force simp: relative_to_def openin_subtopology)
   2.908 +
   2.909 +lemma topspace_subtopology: "topspace (subtopology U V) = topspace U \<inter> V"
   2.910 +  by (auto simp: topspace_def openin_subtopology)
   2.911 +
   2.912 +lemma closedin_subtopology: "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
   2.913 +  unfolding closedin_def topspace_subtopology
   2.914 +  by (auto simp: openin_subtopology)
   2.915 +
   2.916 +lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
   2.917 +  unfolding openin_subtopology
   2.918 +  by auto (metis IntD1 in_mono openin_subset)
   2.919 +
   2.920 +lemma subtopology_subtopology:
   2.921 +   "subtopology (subtopology X S) T = subtopology X (S \<inter> T)"
   2.922 +proof -
   2.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)"
   2.924 +    by (metis inf_assoc)
   2.925 +  have "subtopology (subtopology X S) T = topology (\<lambda>Ta. \<exists>Sa. Ta = Sa \<inter> T \<and> openin (subtopology X S) Sa)"
   2.926 +    by (simp add: subtopology_def)
   2.927 +  also have "\<dots> = subtopology X (S \<inter> T)"
   2.928 +    by (simp add: openin_subtopology eq) (simp add: subtopology_def)
   2.929 +  finally show ?thesis .
   2.930 +qed
   2.931 +
   2.932 +lemma openin_subtopology_alt:
   2.933 +     "openin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (openin X)"
   2.934 +  by (simp add: image_iff inf_commute openin_subtopology)
   2.935 +
   2.936 +lemma closedin_subtopology_alt:
   2.937 +     "closedin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (closedin X)"
   2.938 +  by (simp add: image_iff inf_commute closedin_subtopology)
   2.939 +
   2.940 +lemma subtopology_superset:
   2.941 +  assumes UV: "topspace U \<subseteq> V"
   2.942 +  shows "subtopology U V = U"
   2.943 +proof -
   2.944 +  {
   2.945 +    fix S
   2.946 +    {
   2.947 +      fix T
   2.948 +      assume T: "openin U T" "S = T \<inter> V"
   2.949 +      from T openin_subset[OF T(1)] UV have eq: "S = T"
   2.950 +        by blast
   2.951 +      have "openin U S"
   2.952 +        unfolding eq using T by blast
   2.953 +    }
   2.954 +    moreover
   2.955 +    {
   2.956 +      assume S: "openin U S"
   2.957 +      then have "\<exists>T. openin U T \<and> S = T \<inter> V"
   2.958 +        using openin_subset[OF S] UV by auto
   2.959 +    }
   2.960 +    ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S"
   2.961 +      by blast
   2.962 +  }
   2.963 +  then show ?thesis
   2.964 +    unfolding topology_eq openin_subtopology by blast
   2.965 +qed
   2.966 +
   2.967 +lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
   2.968 +  by (simp add: subtopology_superset)
   2.969 +
   2.970 +lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
   2.971 +  by (simp add: subtopology_superset)
   2.972 +
   2.973 +lemma openin_subtopology_empty:
   2.974 +   "openin (subtopology U {}) S \<longleftrightarrow> S = {}"
   2.975 +by (metis Int_empty_right openin_empty openin_subtopology)
   2.976 +
   2.977 +lemma closedin_subtopology_empty:
   2.978 +   "closedin (subtopology U {}) S \<longleftrightarrow> S = {}"
   2.979 +by (metis Int_empty_right closedin_empty closedin_subtopology)
   2.980 +
   2.981 +lemma closedin_subtopology_refl [simp]:
   2.982 +   "closedin (subtopology U X) X \<longleftrightarrow> X \<subseteq> topspace U"
   2.983 +by (metis closedin_def closedin_topspace inf.absorb_iff2 le_inf_iff topspace_subtopology)
   2.984 +
   2.985 +lemma closedin_topspace_empty: "topspace T = {} \<Longrightarrow> (closedin T S \<longleftrightarrow> S = {})"
   2.986 +  by (simp add: closedin_def)
   2.987 +
   2.988 +lemma openin_imp_subset:
   2.989 +   "openin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
   2.990 +by (metis Int_iff openin_subtopology subsetI)
   2.991 +
   2.992 +lemma closedin_imp_subset:
   2.993 +   "closedin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
   2.994 +by (simp add: closedin_def topspace_subtopology)
   2.995 +
   2.996 +lemma openin_open_subtopology:
   2.997 +     "openin X S \<Longrightarrow> openin (subtopology X S) T \<longleftrightarrow> openin X T \<and> T \<subseteq> S"
   2.998 +  by (metis inf.orderE openin_Int openin_imp_subset openin_subtopology)
   2.999 +
  2.1000 +lemma closedin_closed_subtopology:
  2.1001 +     "closedin X S \<Longrightarrow> (closedin (subtopology X S) T \<longleftrightarrow> closedin X T \<and> T \<subseteq> S)"
  2.1002 +  by (metis closedin_Int closedin_imp_subset closedin_subtopology inf.orderE)
  2.1003 +
  2.1004 +lemma openin_subtopology_Un:
  2.1005 +    "\<lbrakk>openin (subtopology X T) S; openin (subtopology X U) S\<rbrakk>
  2.1006 +     \<Longrightarrow> openin (subtopology X (T \<union> U)) S"
  2.1007 +by (simp add: openin_subtopology) blast
  2.1008 +
  2.1009 +lemma closedin_subtopology_Un:
  2.1010 +    "\<lbrakk>closedin (subtopology X T) S; closedin (subtopology X U) S\<rbrakk>
  2.1011 +     \<Longrightarrow> closedin (subtopology X (T \<union> U)) S"
  2.1012 +by (simp add: closedin_subtopology) blast
  2.1013 +
  2.1014 +
  2.1015 +subsection \<open>The standard Euclidean topology\<close>
  2.1016 +
  2.1017 +definition%important euclidean :: "'a::topological_space topology"
  2.1018 +  where "euclidean = topology open"
  2.1019 +
  2.1020 +lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
  2.1021 +  unfolding euclidean_def
  2.1022 +  apply (rule cong[where x=S and y=S])
  2.1023 +  apply (rule topology_inverse[symmetric])
  2.1024 +  apply (auto simp: istopology_def)
  2.1025 +  done
  2.1026 +
  2.1027 +declare open_openin [symmetric, simp]
  2.1028 +
  2.1029 +lemma topspace_euclidean [simp]: "topspace euclidean = UNIV"
  2.1030 +  by (force simp: topspace_def)
  2.1031 +
  2.1032 +lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
  2.1033 +  by (simp add: topspace_subtopology)
  2.1034 +
  2.1035 +lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
  2.1036 +  by (simp add: closed_def closedin_def Compl_eq_Diff_UNIV)
  2.1037 +
  2.1038 +declare closed_closedin [symmetric, simp]
  2.1039 +
  2.1040 +lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
  2.1041 +  using openI by auto
  2.1042 +
  2.1043 +lemma openin_subtopology_self [simp]: "openin (subtopology euclidean S) S"
  2.1044 +  by (metis openin_topspace topspace_euclidean_subtopology)
  2.1045 +
  2.1046 +subsubsection\<open>The most basic facts about the usual topology and metric on R\<close>
  2.1047 +
  2.1048 +abbreviation euclideanreal :: "real topology"
  2.1049 +  where "euclideanreal \<equiv> topology open"
  2.1050 +
  2.1051 +lemma real_openin [simp]: "openin euclideanreal S = open S"
  2.1052 +  by (simp add: euclidean_def open_openin)
  2.1053 +
  2.1054 +lemma topspace_euclideanreal [simp]: "topspace euclideanreal = UNIV"
  2.1055 +  using openin_subset open_UNIV real_openin by blast
  2.1056 +
  2.1057 +lemma topspace_euclideanreal_subtopology [simp]:
  2.1058 +   "topspace (subtopology euclideanreal S) = S"
  2.1059 +  by (simp add: topspace_subtopology)
  2.1060 +
  2.1061 +lemma real_closedin [simp]: "closedin euclideanreal S = closed S"
  2.1062 +  by (simp add: closed_closedin euclidean_def)
  2.1063 +
  2.1064 +subsection \<open>Basic "localization" results are handy for connectedness.\<close>
  2.1065 +
  2.1066 +lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
  2.1067 +  by (auto simp: openin_subtopology)
  2.1068 +
  2.1069 +lemma openin_Int_open:
  2.1070 +   "\<lbrakk>openin (subtopology euclidean U) S; open T\<rbrakk>
  2.1071 +        \<Longrightarrow> openin (subtopology euclidean U) (S \<inter> T)"
  2.1072 +by (metis open_Int Int_assoc openin_open)
  2.1073 +
  2.1074 +lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
  2.1075 +  by (auto simp: openin_open)
  2.1076 +
  2.1077 +lemma open_openin_trans[trans]:
  2.1078 +  "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
  2.1079 +  by (metis Int_absorb1  openin_open_Int)
  2.1080 +
  2.1081 +lemma open_subset: "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
  2.1082 +  by (auto simp: openin_open)
  2.1083 +
  2.1084 +lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
  2.1085 +  by (simp add: closedin_subtopology Int_ac)
  2.1086 +
  2.1087 +lemma closedin_closed_Int: "closed S \<Longrightarrow> closedin (subtopology euclidean U) (U \<inter> S)"
  2.1088 +  by (metis closedin_closed)
  2.1089 +
  2.1090 +lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
  2.1091 +  by (auto simp: closedin_closed)
  2.1092 +
  2.1093 +lemma closedin_closed_subset:
  2.1094 + "\<lbrakk>closedin (subtopology euclidean U) V; T \<subseteq> U; S = V \<inter> T\<rbrakk>
  2.1095 +             \<Longrightarrow> closedin (subtopology euclidean T) S"
  2.1096 +  by (metis (no_types, lifting) Int_assoc Int_commute closedin_closed inf.orderE)
  2.1097 +
  2.1098 +lemma finite_imp_closedin:
  2.1099 +  fixes S :: "'a::t1_space set"
  2.1100 +  shows "\<lbrakk>finite S; S \<subseteq> T\<rbrakk> \<Longrightarrow> closedin (subtopology euclidean T) S"
  2.1101 +    by (simp add: finite_imp_closed closed_subset)
  2.1102 +
  2.1103 +lemma closedin_singleton [simp]:
  2.1104 +  fixes a :: "'a::t1_space"
  2.1105 +  shows "closedin (subtopology euclidean U) {a} \<longleftrightarrow> a \<in> U"
  2.1106 +using closedin_subset  by (force intro: closed_subset)
  2.1107 +
  2.1108 +lemma openin_euclidean_subtopology_iff:
  2.1109 +  fixes S U :: "'a::metric_space set"
  2.1110 +  shows "openin (subtopology euclidean U) S \<longleftrightarrow>
  2.1111 +    S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)"
  2.1112 +  (is "?lhs \<longleftrightarrow> ?rhs")
  2.1113 +proof
  2.1114 +  assume ?lhs
  2.1115 +  then show ?rhs
  2.1116 +    unfolding openin_open open_dist by blast
  2.1117 +next
  2.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}"
  2.1119 +  have 1: "\<forall>x\<in>T. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> T"
  2.1120 +    unfolding T_def
  2.1121 +    apply clarsimp
  2.1122 +    apply (rule_tac x="d - dist x a" in exI)
  2.1123 +    apply (clarsimp simp add: less_diff_eq)
  2.1124 +    by (metis dist_commute dist_triangle_lt)
  2.1125 +  assume ?rhs then have 2: "S = U \<inter> T"
  2.1126 +    unfolding T_def
  2.1127 +    by auto (metis dist_self)
  2.1128 +  from 1 2 show ?lhs
  2.1129 +    unfolding openin_open open_dist by fast
  2.1130 +qed
  2.1131 +
  2.1132 +lemma connected_openin:
  2.1133 +      "connected S \<longleftrightarrow>
  2.1134 +       \<not>(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
  2.1135 +                 openin (subtopology euclidean S) E2 \<and>
  2.1136 +                 S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
  2.1137 +  apply (simp add: connected_def openin_open disjoint_iff_not_equal, safe)
  2.1138 +  apply (simp_all, blast+)  (* SLOW *)
  2.1139 +  done
  2.1140 +
  2.1141 +lemma connected_openin_eq:
  2.1142 +      "connected S \<longleftrightarrow>
  2.1143 +       \<not>(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
  2.1144 +                 openin (subtopology euclidean S) E2 \<and>
  2.1145 +                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
  2.1146 +                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
  2.1147 +  apply (simp add: connected_openin, safe, blast)
  2.1148 +  by (metis Int_lower1 Un_subset_iff openin_open subset_antisym)
  2.1149 +
  2.1150 +lemma connected_closedin:
  2.1151 +      "connected S \<longleftrightarrow>
  2.1152 +       (\<nexists>E1 E2.
  2.1153 +        closedin (subtopology euclidean S) E1 \<and>
  2.1154 +        closedin (subtopology euclidean S) E2 \<and>
  2.1155 +        S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
  2.1156 +       (is "?lhs = ?rhs")
  2.1157 +proof
  2.1158 +  assume ?lhs
  2.1159 +  then show ?rhs 
  2.1160 +    by (auto simp add: connected_closed closedin_closed)
  2.1161 +next
  2.1162 +  assume R: ?rhs
  2.1163 +  then show ?lhs 
  2.1164 +  proof (clarsimp simp add: connected_closed closedin_closed)
  2.1165 +    fix A B 
  2.1166 +    assume s_sub: "S \<subseteq> A \<union> B" "B \<inter> S \<noteq> {}"
  2.1167 +      and disj: "A \<inter> B \<inter> S = {}"
  2.1168 +      and cl: "closed A" "closed B"
  2.1169 +    have "S \<inter> (A \<union> B) = S"
  2.1170 +      using s_sub(1) by auto
  2.1171 +    have "S - A = B \<inter> S"
  2.1172 +      using Diff_subset_conv Un_Diff_Int disj s_sub(1) by auto
  2.1173 +    then have "S \<inter> A = {}"
  2.1174 +      by (metis Diff_Diff_Int Diff_disjoint Un_Diff_Int R cl closedin_closed_Int inf_commute order_refl s_sub(2))
  2.1175 +    then show "A \<inter> S = {}"
  2.1176 +      by blast
  2.1177 +  qed
  2.1178 +qed
  2.1179 +
  2.1180 +lemma connected_closedin_eq:
  2.1181 +      "connected S \<longleftrightarrow>
  2.1182 +           \<not>(\<exists>E1 E2.
  2.1183 +                 closedin (subtopology euclidean S) E1 \<and>
  2.1184 +                 closedin (subtopology euclidean S) E2 \<and>
  2.1185 +                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
  2.1186 +                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
  2.1187 +  apply (simp add: connected_closedin, safe, blast)
  2.1188 +  by (metis Int_lower1 Un_subset_iff closedin_closed subset_antisym)
  2.1189 +
  2.1190 +text \<open>These "transitivity" results are handy too\<close>
  2.1191 +
  2.1192 +lemma openin_trans[trans]:
  2.1193 +  "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T \<Longrightarrow>
  2.1194 +    openin (subtopology euclidean U) S"
  2.1195 +  unfolding open_openin openin_open by blast
  2.1196 +
  2.1197 +lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
  2.1198 +  by (auto simp: openin_open intro: openin_trans)
  2.1199 +
  2.1200 +lemma closedin_trans[trans]:
  2.1201 +  "closedin (subtopology euclidean T) S \<Longrightarrow> closedin (subtopology euclidean U) T \<Longrightarrow>
  2.1202 +    closedin (subtopology euclidean U) S"
  2.1203 +  by (auto simp: closedin_closed closed_Inter Int_assoc)
  2.1204 +
  2.1205 +lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
  2.1206 +  by (auto simp: closedin_closed intro: closedin_trans)
  2.1207 +
  2.1208 +lemma openin_subtopology_Int_subset:
  2.1209 +   "\<lbrakk>openin (subtopology euclidean u) (u \<inter> S); v \<subseteq> u\<rbrakk> \<Longrightarrow> openin (subtopology euclidean v) (v \<inter> S)"
  2.1210 +  by (auto simp: openin_subtopology)
  2.1211 +
  2.1212 +lemma openin_open_eq: "open s \<Longrightarrow> (openin (subtopology euclidean s) t \<longleftrightarrow> open t \<and> t \<subseteq> s)"
  2.1213 +  using open_subset openin_open_trans openin_subset by fastforce
  2.1214 +
  2.1215 +
  2.1216 +subsection \<open>Open and closed balls\<close>
  2.1217 +
  2.1218 +definition%important ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
  2.1219 +  where "ball x e = {y. dist x y < e}"
  2.1220 +
  2.1221 +definition%important cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
  2.1222 +  where "cball x e = {y. dist x y \<le> e}"
  2.1223 +
  2.1224 +definition%important sphere :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
  2.1225 +  where "sphere x e = {y. dist x y = e}"
  2.1226 +
  2.1227 +lemma mem_ball [simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e"
  2.1228 +  by (simp add: ball_def)
  2.1229 +
  2.1230 +lemma mem_cball [simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e"
  2.1231 +  by (simp add: cball_def)
  2.1232 +
  2.1233 +lemma mem_sphere [simp]: "y \<in> sphere x e \<longleftrightarrow> dist x y = e"
  2.1234 +  by (simp add: sphere_def)
  2.1235 +
  2.1236 +lemma ball_trivial [simp]: "ball x 0 = {}"
  2.1237 +  by (simp add: ball_def)
  2.1238 +
  2.1239 +lemma cball_trivial [simp]: "cball x 0 = {x}"
  2.1240 +  by (simp add: cball_def)
  2.1241 +
  2.1242 +lemma sphere_trivial [simp]: "sphere x 0 = {x}"
  2.1243 +  by (simp add: sphere_def)
  2.1244 +
  2.1245 +lemma mem_ball_0 [simp]: "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
  2.1246 +  for x :: "'a::real_normed_vector"
  2.1247 +  by (simp add: dist_norm)
  2.1248 +
  2.1249 +lemma mem_cball_0 [simp]: "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
  2.1250 +  for x :: "'a::real_normed_vector"
  2.1251 +  by (simp add: dist_norm)
  2.1252 +
  2.1253 +lemma disjoint_ballI: "dist x y \<ge> r+s \<Longrightarrow> ball x r \<inter> ball y s = {}"
  2.1254 +  using dist_triangle_less_add not_le by fastforce
  2.1255 +
  2.1256 +lemma disjoint_cballI: "dist x y > r + s \<Longrightarrow> cball x r \<inter> cball y s = {}"
  2.1257 +  by (metis add_mono disjoint_iff_not_equal dist_triangle2 dual_order.trans leD mem_cball)
  2.1258 +
  2.1259 +lemma mem_sphere_0 [simp]: "x \<in> sphere 0 e \<longleftrightarrow> norm x = e"
  2.1260 +  for x :: "'a::real_normed_vector"
  2.1261 +  by (simp add: dist_norm)
  2.1262 +
  2.1263 +lemma sphere_empty [simp]: "r < 0 \<Longrightarrow> sphere a r = {}"
  2.1264 +  for a :: "'a::metric_space"
  2.1265 +  by auto
  2.1266 +
  2.1267 +lemma centre_in_ball [simp]: "x \<in> ball x e \<longleftrightarrow> 0 < e"
  2.1268 +  by simp
  2.1269 +
  2.1270 +lemma centre_in_cball [simp]: "x \<in> cball x e \<longleftrightarrow> 0 \<le> e"
  2.1271 +  by simp
  2.1272 +
  2.1273 +lemma ball_subset_cball [simp, intro]: "ball x e \<subseteq> cball x e"
  2.1274 +  by (simp add: subset_eq)
  2.1275 +
  2.1276 +lemma mem_ball_imp_mem_cball: "x \<in> ball y e \<Longrightarrow> x \<in> cball y e"
  2.1277 +  by (auto simp: mem_ball mem_cball)
  2.1278 +
  2.1279 +lemma sphere_cball [simp,intro]: "sphere z r \<subseteq> cball z r"
  2.1280 +  by force
  2.1281 +
  2.1282 +lemma cball_diff_sphere: "cball a r - sphere a r = ball a r"
  2.1283 +  by auto
  2.1284 +
  2.1285 +lemma subset_ball[intro]: "d \<le> e \<Longrightarrow> ball x d \<subseteq> ball x e"
  2.1286 +  by (simp add: subset_eq)
  2.1287 +
  2.1288 +lemma subset_cball[intro]: "d \<le> e \<Longrightarrow> cball x d \<subseteq> cball x e"
  2.1289 +  by (simp add: subset_eq)
  2.1290 +
  2.1291 +lemma mem_ball_leI: "x \<in> ball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> ball y f"
  2.1292 +  by (auto simp: mem_ball mem_cball)
  2.1293 +
  2.1294 +lemma mem_cball_leI: "x \<in> cball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> cball y f"
  2.1295 +  by (auto simp: mem_ball mem_cball)
  2.1296 +
  2.1297 +lemma cball_trans: "y \<in> cball z b \<Longrightarrow> x \<in> cball y a \<Longrightarrow> x \<in> cball z (b + a)"
  2.1298 +  unfolding mem_cball
  2.1299 +proof -
  2.1300 +  have "dist z x \<le> dist z y + dist y x"
  2.1301 +    by (rule dist_triangle)
  2.1302 +  also assume "dist z y \<le> b"
  2.1303 +  also assume "dist y x \<le> a"
  2.1304 +  finally show "dist z x \<le> b + a" by arith
  2.1305 +qed
  2.1306 +
  2.1307 +lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
  2.1308 +  by (simp add: set_eq_iff) arith
  2.1309 +
  2.1310 +lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
  2.1311 +  by (simp add: set_eq_iff)
  2.1312 +
  2.1313 +lemma cball_max_Un: "cball a (max r s) = cball a r \<union> cball a s"
  2.1314 +  by (simp add: set_eq_iff) arith
  2.1315 +
  2.1316 +lemma cball_min_Int: "cball a (min r s) = cball a r \<inter> cball a s"
  2.1317 +  by (simp add: set_eq_iff)
  2.1318 +
  2.1319 +lemma cball_diff_eq_sphere: "cball a r - ball a r =  sphere a r"
  2.1320 +  by (auto simp: cball_def ball_def dist_commute)
  2.1321 +
  2.1322 +lemma image_add_ball [simp]:
  2.1323 +  fixes a :: "'a::real_normed_vector"
  2.1324 +  shows "(+) b ` ball a r = ball (a+b) r"
  2.1325 +apply (intro equalityI subsetI)
  2.1326 +apply (force simp: dist_norm)
  2.1327 +apply (rule_tac x="x-b" in image_eqI)
  2.1328 +apply (auto simp: dist_norm algebra_simps)
  2.1329 +done
  2.1330 +
  2.1331 +lemma image_add_cball [simp]:
  2.1332 +  fixes a :: "'a::real_normed_vector"
  2.1333 +  shows "(+) b ` cball a r = cball (a+b) r"
  2.1334 +apply (intro equalityI subsetI)
  2.1335 +apply (force simp: dist_norm)
  2.1336 +apply (rule_tac x="x-b" in image_eqI)
  2.1337 +apply (auto simp: dist_norm algebra_simps)
  2.1338 +done
  2.1339 +
  2.1340 +lemma open_ball [intro, simp]: "open (ball x e)"
  2.1341 +proof -
  2.1342 +  have "open (dist x -` {..<e})"
  2.1343 +    by (intro open_vimage open_lessThan continuous_intros)
  2.1344 +  also have "dist x -` {..<e} = ball x e"
  2.1345 +    by auto
  2.1346 +  finally show ?thesis .
  2.1347 +qed
  2.1348 +
  2.1349 +lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
  2.1350 +  by (simp add: open_dist subset_eq mem_ball Ball_def dist_commute)
  2.1351 +
  2.1352 +lemma openI [intro?]: "(\<And>x. x\<in>S \<Longrightarrow> \<exists>e>0. ball x e \<subseteq> S) \<Longrightarrow> open S"
  2.1353 +  by (auto simp: open_contains_ball)
  2.1354 +
  2.1355 +lemma openE[elim?]:
  2.1356 +  assumes "open S" "x\<in>S"
  2.1357 +  obtains e where "e>0" "ball x e \<subseteq> S"
  2.1358 +  using assms unfolding open_contains_ball by auto
  2.1359 +
  2.1360 +lemma open_contains_ball_eq: "open S \<Longrightarrow> x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
  2.1361 +  by (metis open_contains_ball subset_eq centre_in_ball)
  2.1362 +
  2.1363 +lemma openin_contains_ball:
  2.1364 +    "openin (subtopology euclidean t) s \<longleftrightarrow>
  2.1365 +     s \<subseteq> t \<and> (\<forall>x \<in> s. \<exists>e. 0 < e \<and> ball x e \<inter> t \<subseteq> s)"
  2.1366 +    (is "?lhs = ?rhs")
  2.1367 +proof
  2.1368 +  assume ?lhs
  2.1369 +  then show ?rhs
  2.1370 +    apply (simp add: openin_open)
  2.1371 +    apply (metis Int_commute Int_mono inf.cobounded2 open_contains_ball order_refl subsetCE)
  2.1372 +    done
  2.1373 +next
  2.1374 +  assume ?rhs
  2.1375 +  then show ?lhs
  2.1376 +    apply (simp add: openin_euclidean_subtopology_iff)
  2.1377 +    by (metis (no_types) Int_iff dist_commute inf.absorb_iff2 mem_ball)
  2.1378 +qed
  2.1379 +
  2.1380 +lemma openin_contains_cball:
  2.1381 +   "openin (subtopology euclidean t) s \<longleftrightarrow>
  2.1382 +        s \<subseteq> t \<and>
  2.1383 +        (\<forall>x \<in> s. \<exists>e. 0 < e \<and> cball x e \<inter> t \<subseteq> s)"
  2.1384 +apply (simp add: openin_contains_ball)
  2.1385 +apply (rule iffI)
  2.1386 +apply (auto dest!: bspec)
  2.1387 +apply (rule_tac x="e/2" in exI, force+)
  2.1388 +done
  2.1389 +
  2.1390 +lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
  2.1391 +  unfolding mem_ball set_eq_iff
  2.1392 +  apply (simp add: not_less)
  2.1393 +  apply (metis zero_le_dist order_trans dist_self)
  2.1394 +  done
  2.1395 +
  2.1396 +lemma ball_empty: "e \<le> 0 \<Longrightarrow> ball x e = {}" by simp
  2.1397 +
  2.1398 +lemma closed_cball [iff]: "closed (cball x e)"
  2.1399 +proof -
  2.1400 +  have "closed (dist x -` {..e})"
  2.1401 +    by (intro closed_vimage closed_atMost continuous_intros)
  2.1402 +  also have "dist x -` {..e} = cball x e"
  2.1403 +    by auto
  2.1404 +  finally show ?thesis .
  2.1405 +qed
  2.1406 +
  2.1407 +lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
  2.1408 +proof -
  2.1409 +  {
  2.1410 +    fix x and e::real
  2.1411 +    assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
  2.1412 +    then have "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
  2.1413 +  }
  2.1414 +  moreover
  2.1415 +  {
  2.1416 +    fix x and e::real
  2.1417 +    assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
  2.1418 +    then have "\<exists>d>0. ball x d \<subseteq> S"
  2.1419 +      unfolding subset_eq
  2.1420 +      apply (rule_tac x="e/2" in exI, auto)
  2.1421 +      done
  2.1422 +  }
  2.1423 +  ultimately show ?thesis
  2.1424 +    unfolding open_contains_ball by auto
  2.1425 +qed
  2.1426 +
  2.1427 +lemma open_contains_cball_eq: "open S \<Longrightarrow> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
  2.1428 +  by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)
  2.1429 +
  2.1430 +lemma eventually_nhds_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>x. x \<in> ball z d) (nhds z)"
  2.1431 +  by (rule eventually_nhds_in_open) simp_all
  2.1432 +
  2.1433 +lemma eventually_at_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<in> A) (at z within A)"
  2.1434 +  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
  2.1435 +
  2.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)"
  2.1437 +  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
  2.1438 +
  2.1439 +lemma at_within_ball: "e > 0 \<Longrightarrow> dist x y < e \<Longrightarrow> at y within ball x e = at y"
  2.1440 +  by (subst at_within_open) auto
  2.1441 +
  2.1442 +lemma atLeastAtMost_eq_cball:
  2.1443 +  fixes a b::real
  2.1444 +  shows "{a .. b} = cball ((a + b)/2) ((b - a)/2)"
  2.1445 +  by (auto simp: dist_real_def field_simps mem_cball)
  2.1446 +
  2.1447 +lemma greaterThanLessThan_eq_ball:
  2.1448 +  fixes a b::real
  2.1449 +  shows "{a <..< b} = ball ((a + b)/2) ((b - a)/2)"
  2.1450 +  by (auto simp: dist_real_def field_simps mem_ball)
  2.1451 +
  2.1452 +
  2.1453 +subsection \<open>Limit points\<close>
  2.1454 +
  2.1455 +definition%important (in topological_space) islimpt:: "'a \<Rightarrow> 'a set \<Rightarrow> bool"  (infixr "islimpt" 60)
  2.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))"
  2.1457 +
  2.1458 +lemma islimptI:
  2.1459 +  assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
  2.1460 +  shows "x islimpt S"
  2.1461 +  using assms unfolding islimpt_def by auto
  2.1462 +
  2.1463 +lemma islimptE:
  2.1464 +  assumes "x islimpt S" and "x \<in> T" and "open T"
  2.1465 +  obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
  2.1466 +  using assms unfolding islimpt_def by auto
  2.1467 +
  2.1468 +lemma islimpt_iff_eventually: "x islimpt S \<longleftrightarrow> \<not> eventually (\<lambda>y. y \<notin> S) (at x)"
  2.1469 +  unfolding islimpt_def eventually_at_topological by auto
  2.1470 +
  2.1471 +lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> x islimpt T"
  2.1472 +  unfolding islimpt_def by fast
  2.1473 +
  2.1474 +lemma islimpt_approachable:
  2.1475 +  fixes x :: "'a::metric_space"
  2.1476 +  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
  2.1477 +  unfolding islimpt_iff_eventually eventually_at by fast
  2.1478 +
  2.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)"
  2.1480 +  for x :: "'a::metric_space"
  2.1481 +  unfolding islimpt_approachable
  2.1482 +  using approachable_lt_le [where f="\<lambda>y. dist y x" and P="\<lambda>y. y \<notin> S \<or> y = x",
  2.1483 +    THEN arg_cong [where f=Not]]
  2.1484 +  by (simp add: Bex_def conj_commute conj_left_commute)
  2.1485 +
  2.1486 +lemma islimpt_UNIV_iff: "x islimpt UNIV \<longleftrightarrow> \<not> open {x}"
  2.1487 +  unfolding islimpt_def by (safe, fast, case_tac "T = {x}", fast, fast)
  2.1488 +
  2.1489 +lemma islimpt_punctured: "x islimpt S = x islimpt (S-{x})"
  2.1490 +  unfolding islimpt_def by blast
  2.1491 +
  2.1492 +text \<open>A perfect space has no isolated points.\<close>
  2.1493 +
  2.1494 +lemma islimpt_UNIV [simp, intro]: "x islimpt UNIV"
  2.1495 +  for x :: "'a::perfect_space"
  2.1496 +  unfolding islimpt_UNIV_iff by (rule not_open_singleton)
  2.1497 +
  2.1498 +lemma perfect_choose_dist: "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
  2.1499 +  for x :: "'a::{perfect_space,metric_space}"
  2.1500 +  using islimpt_UNIV [of x] by (simp add: islimpt_approachable)
  2.1501 +
  2.1502 +lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
  2.1503 +  unfolding closed_def
  2.1504 +  apply (subst open_subopen)
  2.1505 +  apply (simp add: islimpt_def subset_eq)
  2.1506 +  apply (metis ComplE ComplI)
  2.1507 +  done
  2.1508 +
  2.1509 +lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
  2.1510 +  by (auto simp: islimpt_def)
  2.1511 +
  2.1512 +lemma finite_ball_include:
  2.1513 +  fixes a :: "'a::metric_space"
  2.1514 +  assumes "finite S" 
  2.1515 +  shows "\<exists>e>0. S \<subseteq> ball a e"
  2.1516 +  using assms
  2.1517 +proof induction
  2.1518 +  case (insert x S)
  2.1519 +  then obtain e0 where "e0>0" and e0:"S \<subseteq> ball a e0" by auto
  2.1520 +  define e where "e = max e0 (2 * dist a x)"
  2.1521 +  have "e>0" unfolding e_def using \<open>e0>0\<close> by auto
  2.1522 +  moreover have "insert x S \<subseteq> ball a e"
  2.1523 +    using e0 \<open>e>0\<close> unfolding e_def by auto
  2.1524 +  ultimately show ?case by auto
  2.1525 +qed (auto intro: zero_less_one)
  2.1526 +
  2.1527 +lemma finite_set_avoid:
  2.1528 +  fixes a :: "'a::metric_space"
  2.1529 +  assumes "finite S"
  2.1530 +  shows "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
  2.1531 +  using assms
  2.1532 +proof induction
  2.1533 +  case (insert x S)
  2.1534 +  then obtain d where "d > 0" and d: "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
  2.1535 +    by blast
  2.1536 +  show ?case
  2.1537 +  proof (cases "x = a")
  2.1538 +    case True
  2.1539 +    with \<open>d > 0 \<close>d show ?thesis by auto
  2.1540 +  next
  2.1541 +    case False
  2.1542 +    let ?d = "min d (dist a x)"
  2.1543 +    from False \<open>d > 0\<close> have dp: "?d > 0"
  2.1544 +      by auto
  2.1545 +    from d have d': "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> ?d \<le> dist a x"
  2.1546 +      by auto
  2.1547 +    with dp False show ?thesis
  2.1548 +      by (metis insert_iff le_less min_less_iff_conj not_less)
  2.1549 +  qed
  2.1550 +qed (auto intro: zero_less_one)
  2.1551 +
  2.1552 +lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
  2.1553 +  by (simp add: islimpt_iff_eventually eventually_conj_iff)
  2.1554 +
  2.1555 +lemma discrete_imp_closed:
  2.1556 +  fixes S :: "'a::metric_space set"
  2.1557 +  assumes e: "0 < e"
  2.1558 +    and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
  2.1559 +  shows "closed S"
  2.1560 +proof -
  2.1561 +  have False if C: "\<And>e. e>0 \<Longrightarrow> \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" for x
  2.1562 +  proof -
  2.1563 +    from e have e2: "e/2 > 0" by arith
  2.1564 +    from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y \<noteq> x" "dist y x < e/2"
  2.1565 +      by blast
  2.1566 +    let ?m = "min (e/2) (dist x y) "
  2.1567 +    from e2 y(2) have mp: "?m > 0"
  2.1568 +      by simp
  2.1569 +    from C[OF mp] obtain z where z: "z \<in> S" "z \<noteq> x" "dist z x < ?m"
  2.1570 +      by blast
  2.1571 +    from z y have "dist z y < e"
  2.1572 +      by (intro dist_triangle_lt [where z=x]) simp
  2.1573 +    from d[rule_format, OF y(1) z(1) this] y z show ?thesis
  2.1574 +      by (auto simp: dist_commute)
  2.1575 +  qed
  2.1576 +  then show ?thesis
  2.1577 +    by (metis islimpt_approachable closed_limpt [where 'a='a])
  2.1578 +qed
  2.1579 +
  2.1580 +lemma closed_of_nat_image: "closed (of_nat ` A :: 'a::real_normed_algebra_1 set)"
  2.1581 +  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_nat)
  2.1582 +
  2.1583 +lemma closed_of_int_image: "closed (of_int ` A :: 'a::real_normed_algebra_1 set)"
  2.1584 +  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_int)
  2.1585 +
  2.1586 +lemma closed_Nats [simp]: "closed (\<nat> :: 'a :: real_normed_algebra_1 set)"
  2.1587 +  unfolding Nats_def by (rule closed_of_nat_image)
  2.1588 +
  2.1589 +lemma closed_Ints [simp]: "closed (\<int> :: 'a :: real_normed_algebra_1 set)"
  2.1590 +  unfolding Ints_def by (rule closed_of_int_image)
  2.1591 +
  2.1592 +lemma closed_subset_Ints:
  2.1593 +  fixes A :: "'a :: real_normed_algebra_1 set"
  2.1594 +  assumes "A \<subseteq> \<int>"
  2.1595 +  shows   "closed A"
  2.1596 +proof (intro discrete_imp_closed[OF zero_less_one] ballI impI, goal_cases)
  2.1597 +  case (1 x y)
  2.1598 +  with assms have "x \<in> \<int>" and "y \<in> \<int>" by auto
  2.1599 +  with \<open>dist y x < 1\<close> show "y = x"
  2.1600 +    by (auto elim!: Ints_cases simp: dist_of_int)
  2.1601 +qed
  2.1602 +
  2.1603 +
  2.1604 +subsection \<open>Interior of a Set\<close>
  2.1605 +
  2.1606 +definition%important "interior S = \<Union>{T. open T \<and> T \<subseteq> S}"
  2.1607 +
  2.1608 +lemma interiorI [intro?]:
  2.1609 +  assumes "open T" and "x \<in> T" and "T \<subseteq> S"
  2.1610 +  shows "x \<in> interior S"
  2.1611 +  using assms unfolding interior_def by fast
  2.1612 +
  2.1613 +lemma interiorE [elim?]:
  2.1614 +  assumes "x \<in> interior S"
  2.1615 +  obtains T where "open T" and "x \<in> T" and "T \<subseteq> S"
  2.1616 +  using assms unfolding interior_def by fast
  2.1617 +
  2.1618 +lemma open_interior [simp, intro]: "open (interior S)"
  2.1619 +  by (simp add: interior_def open_Union)
  2.1620 +
  2.1621 +lemma interior_subset: "interior S \<subseteq> S"
  2.1622 +  by (auto simp: interior_def)
  2.1623 +
  2.1624 +lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> interior S"
  2.1625 +  by (auto simp: interior_def)
  2.1626 +
  2.1627 +lemma interior_open: "open S \<Longrightarrow> interior S = S"
  2.1628 +  by (intro equalityI interior_subset interior_maximal subset_refl)
  2.1629 +
  2.1630 +lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
  2.1631 +  by (metis open_interior interior_open)
  2.1632 +
  2.1633 +lemma open_subset_interior: "open S \<Longrightarrow> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
  2.1634 +  by (metis interior_maximal interior_subset subset_trans)
  2.1635 +
  2.1636 +lemma interior_empty [simp]: "interior {} = {}"
  2.1637 +  using open_empty by (rule interior_open)
  2.1638 +
  2.1639 +lemma interior_UNIV [simp]: "interior UNIV = UNIV"
  2.1640 +  using open_UNIV by (rule interior_open)
  2.1641 +
  2.1642 +lemma interior_interior [simp]: "interior (interior S) = interior S"
  2.1643 +  using open_interior by (rule interior_open)
  2.1644 +
  2.1645 +lemma interior_mono: "S \<subseteq> T \<Longrightarrow> interior S \<subseteq> interior T"
  2.1646 +  by (auto simp: interior_def)
  2.1647 +
  2.1648 +lemma interior_unique:
  2.1649 +  assumes "T \<subseteq> S" and "open T"
  2.1650 +  assumes "\<And>T'. T' \<subseteq> S \<Longrightarrow> open T' \<Longrightarrow> T' \<subseteq> T"
  2.1651 +  shows "interior S = T"
  2.1652 +  by (intro equalityI assms interior_subset open_interior interior_maximal)
  2.1653 +
  2.1654 +lemma interior_singleton [simp]: "interior {a} = {}"
  2.1655 +  for a :: "'a::perfect_space"
  2.1656 +  apply (rule interior_unique, simp_all)
  2.1657 +  using not_open_singleton subset_singletonD
  2.1658 +  apply fastforce
  2.1659 +  done
  2.1660 +
  2.1661 +lemma interior_Int [simp]: "interior (S \<inter> T) = interior S \<inter> interior T"
  2.1662 +  by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1
  2.1663 +    Int_lower2 interior_maximal interior_subset open_Int open_interior)
  2.1664 +
  2.1665 +lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
  2.1666 +  using open_contains_ball_eq [where S="interior S"]
  2.1667 +  by (simp add: open_subset_interior)
  2.1668 +
  2.1669 +lemma eventually_nhds_in_nhd: "x \<in> interior s \<Longrightarrow> eventually (\<lambda>y. y \<in> s) (nhds x)"
  2.1670 +  using interior_subset[of s] by (subst eventually_nhds) blast
  2.1671 +
  2.1672 +lemma interior_limit_point [intro]:
  2.1673 +  fixes x :: "'a::perfect_space"
  2.1674 +  assumes x: "x \<in> interior S"
  2.1675 +  shows "x islimpt S"
  2.1676 +  using x islimpt_UNIV [of x]
  2.1677 +  unfolding interior_def islimpt_def
  2.1678 +  apply (clarsimp, rename_tac T T')
  2.1679 +  apply (drule_tac x="T \<inter> T'" in spec)
  2.1680 +  apply (auto simp: open_Int)
  2.1681 +  done
  2.1682 +
  2.1683 +lemma interior_closed_Un_empty_interior:
  2.1684 +  assumes cS: "closed S"
  2.1685 +    and iT: "interior T = {}"
  2.1686 +  shows "interior (S \<union> T) = interior S"
  2.1687 +proof
  2.1688 +  show "interior S \<subseteq> interior (S \<union> T)"
  2.1689 +    by (rule interior_mono) (rule Un_upper1)
  2.1690 +  show "interior (S \<union> T) \<subseteq> interior S"
  2.1691 +  proof
  2.1692 +    fix x
  2.1693 +    assume "x \<in> interior (S \<union> T)"
  2.1694 +    then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T" ..
  2.1695 +    show "x \<in> interior S"
  2.1696 +    proof (rule ccontr)
  2.1697 +      assume "x \<notin> interior S"
  2.1698 +      with \<open>x \<in> R\<close> \<open>open R\<close> obtain y where "y \<in> R - S"
  2.1699 +        unfolding interior_def by fast
  2.1700 +      from \<open>open R\<close> \<open>closed S\<close> have "open (R - S)"
  2.1701 +        by (rule open_Diff)
  2.1702 +      from \<open>R \<subseteq> S \<union> T\<close> have "R - S \<subseteq> T"
  2.1703 +        by fast
  2.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
  2.1705 +        unfolding interior_def by fast
  2.1706 +    qed
  2.1707 +  qed
  2.1708 +qed
  2.1709 +
  2.1710 +lemma interior_Times: "interior (A \<times> B) = interior A \<times> interior B"
  2.1711 +proof (rule interior_unique)
  2.1712 +  show "interior A \<times> interior B \<subseteq> A \<times> B"
  2.1713 +    by (intro Sigma_mono interior_subset)
  2.1714 +  show "open (interior A \<times> interior B)"
  2.1715 +    by (intro open_Times open_interior)
  2.1716 +  fix T
  2.1717 +  assume "T \<subseteq> A \<times> B" and "open T"
  2.1718 +  then show "T \<subseteq> interior A \<times> interior B"
  2.1719 +  proof safe
  2.1720 +    fix x y
  2.1721 +    assume "(x, y) \<in> T"
  2.1722 +    then obtain C D where "open C" "open D" "C \<times> D \<subseteq> T" "x \<in> C" "y \<in> D"
  2.1723 +      using \<open>open T\<close> unfolding open_prod_def by fast
  2.1724 +    then have "open C" "open D" "C \<subseteq> A" "D \<subseteq> B" "x \<in> C" "y \<in> D"
  2.1725 +      using \<open>T \<subseteq> A \<times> B\<close> by auto
  2.1726 +    then show "x \<in> interior A" and "y \<in> interior B"
  2.1727 +      by (auto intro: interiorI)
  2.1728 +  qed
  2.1729 +qed
  2.1730 +
  2.1731 +lemma interior_Ici:
  2.1732 +  fixes x :: "'a :: {dense_linorder,linorder_topology}"
  2.1733 +  assumes "b < x"
  2.1734 +  shows "interior {x ..} = {x <..}"
  2.1735 +proof (rule interior_unique)
  2.1736 +  fix T
  2.1737 +  assume "T \<subseteq> {x ..}" "open T"
  2.1738 +  moreover have "x \<notin> T"
  2.1739 +  proof
  2.1740 +    assume "x \<in> T"
  2.1741 +    obtain y where "y < x" "{y <.. x} \<subseteq> T"
  2.1742 +      using open_left[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>b < x\<close>] by auto
  2.1743 +    with dense[OF \<open>y < x\<close>] obtain z where "z \<in> T" "z < x"
  2.1744 +      by (auto simp: subset_eq Ball_def)
  2.1745 +    with \<open>T \<subseteq> {x ..}\<close> show False by auto
  2.1746 +  qed
  2.1747 +  ultimately show "T \<subseteq> {x <..}"
  2.1748 +    by (auto simp: subset_eq less_le)
  2.1749 +qed auto
  2.1750 +
  2.1751 +lemma interior_Iic:
  2.1752 +  fixes x :: "'a ::{dense_linorder,linorder_topology}"
  2.1753 +  assumes "x < b"
  2.1754 +  shows "interior {.. x} = {..< x}"
  2.1755 +proof (rule interior_unique)
  2.1756 +  fix T
  2.1757 +  assume "T \<subseteq> {.. x}" "open T"
  2.1758 +  moreover have "x \<notin> T"
  2.1759 +  proof
  2.1760 +    assume "x \<in> T"
  2.1761 +    obtain y where "x < y" "{x ..< y} \<subseteq> T"
  2.1762 +      using open_right[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>x < b\<close>] by auto
  2.1763 +    with dense[OF \<open>x < y\<close>] obtain z where "z \<in> T" "x < z"
  2.1764 +      by (auto simp: subset_eq Ball_def less_le)
  2.1765 +    with \<open>T \<subseteq> {.. x}\<close> show False by auto
  2.1766 +  qed
  2.1767 +  ultimately show "T \<subseteq> {..< x}"
  2.1768 +    by (auto simp: subset_eq less_le)
  2.1769 +qed auto
  2.1770 +
  2.1771 +
  2.1772 +subsection \<open>Closure of a Set\<close>
  2.1773 +
  2.1774 +definition%important "closure S = S \<union> {x | x. x islimpt S}"
  2.1775 +
  2.1776 +lemma interior_closure: "interior S = - (closure (- S))"
  2.1777 +  by (auto simp: interior_def closure_def islimpt_def)
  2.1778 +
  2.1779 +lemma closure_interior: "closure S = - interior (- S)"
  2.1780 +  by (simp add: interior_closure)
  2.1781 +
  2.1782 +lemma closed_closure[simp, intro]: "closed (closure S)"
  2.1783 +  by (simp add: closure_interior closed_Compl)
  2.1784 +
  2.1785 +lemma closure_subset: "S \<subseteq> closure S"
  2.1786 +  by (simp add: closure_def)
  2.1787 +
  2.1788 +lemma closure_hull: "closure S = closed hull S"
  2.1789 +  by (auto simp: hull_def closure_interior interior_def)
  2.1790 +
  2.1791 +lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
  2.1792 +  unfolding closure_hull using closed_Inter by (rule hull_eq)
  2.1793 +
  2.1794 +lemma closure_closed [simp]: "closed S \<Longrightarrow> closure S = S"
  2.1795 +  by (simp only: closure_eq)
  2.1796 +
  2.1797 +lemma closure_closure [simp]: "closure (closure S) = closure S"
  2.1798 +  unfolding closure_hull by (rule hull_hull)
  2.1799 +
  2.1800 +lemma closure_mono: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
  2.1801 +  unfolding closure_hull by (rule hull_mono)
  2.1802 +
  2.1803 +lemma closure_minimal: "S \<subseteq> T \<Longrightarrow> closed T \<Longrightarrow> closure S \<subseteq> T"
  2.1804 +  unfolding closure_hull by (rule hull_minimal)
  2.1805 +
  2.1806 +lemma closure_unique:
  2.1807 +  assumes "S \<subseteq> T"
  2.1808 +    and "closed T"
  2.1809 +    and "\<And>T'. S \<subseteq> T' \<Longrightarrow> closed T' \<Longrightarrow> T \<subseteq> T'"
  2.1810 +  shows "closure S = T"
  2.1811 +  using assms unfolding closure_hull by (rule hull_unique)
  2.1812 +
  2.1813 +lemma closure_empty [simp]: "closure {} = {}"
  2.1814 +  using closed_empty by (rule closure_closed)
  2.1815 +
  2.1816 +lemma closure_UNIV [simp]: "closure UNIV = UNIV"
  2.1817 +  using closed_UNIV by (rule closure_closed)
  2.1818 +
  2.1819 +lemma closure_Un [simp]: "closure (S \<union> T) = closure S \<union> closure T"
  2.1820 +  by (simp add: closure_interior)
  2.1821 +
  2.1822 +lemma closure_eq_empty [iff]: "closure S = {} \<longleftrightarrow> S = {}"
  2.1823 +  using closure_empty closure_subset[of S] by blast
  2.1824 +
  2.1825 +lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
  2.1826 +  using closure_eq[of S] closure_subset[of S] by simp
  2.1827 +
  2.1828 +lemma open_Int_closure_eq_empty: "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
  2.1829 +  using open_subset_interior[of S "- T"]
  2.1830 +  using interior_subset[of "- T"]
  2.1831 +  by (auto simp: closure_interior)
  2.1832 +
  2.1833 +lemma open_Int_closure_subset: "open S \<Longrightarrow> S \<inter> closure T \<subseteq> closure (S \<inter> T)"
  2.1834 +proof
  2.1835 +  fix x
  2.1836 +  assume *: "open S" "x \<in> S \<inter> closure T"
  2.1837 +  have "x islimpt (S \<inter> T)" if **: "x islimpt T"
  2.1838 +  proof (rule islimptI)
  2.1839 +    fix A
  2.1840 +    assume "x \<in> A" "open A"
  2.1841 +    with * have "x \<in> A \<inter> S" "open (A \<inter> S)"
  2.1842 +      by (simp_all add: open_Int)
  2.1843 +    with ** obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
  2.1844 +      by (rule islimptE)
  2.1845 +    then have "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
  2.1846 +      by simp_all
  2.1847 +    then show "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
  2.1848 +  qed
  2.1849 +  with * show "x \<in> closure (S \<inter> T)"
  2.1850 +    unfolding closure_def by blast
  2.1851 +qed
  2.1852 +
  2.1853 +lemma closure_complement: "closure (- S) = - interior S"
  2.1854 +  by (simp add: closure_interior)
  2.1855 +
  2.1856 +lemma interior_complement: "interior (- S) = - closure S"
  2.1857 +  by (simp add: closure_interior)
  2.1858 +
  2.1859 +lemma interior_diff: "interior(S - T) = interior S - closure T"
  2.1860 +  by (simp add: Diff_eq interior_complement)
  2.1861 +
  2.1862 +lemma closure_Times: "closure (A \<times> B) = closure A \<times> closure B"
  2.1863 +proof (rule closure_unique)
  2.1864 +  show "A \<times> B \<subseteq> closure A \<times> closure B"
  2.1865 +    by (intro Sigma_mono closure_subset)
  2.1866 +  show "closed (closure A \<times> closure B)"
  2.1867 +    by (intro closed_Times closed_closure)
  2.1868 +  fix T
  2.1869 +  assume "A \<times> B \<subseteq> T" and "closed T"
  2.1870 +  then show "closure A \<times> closure B \<subseteq> T"
  2.1871 +    apply (simp add: closed_def open_prod_def, clarify)
  2.1872 +    apply (rule ccontr)
  2.1873 +    apply (drule_tac x="(a, b)" in bspec, simp, clarify, rename_tac C D)
  2.1874 +    apply (simp add: closure_interior interior_def)
  2.1875 +    apply (drule_tac x=C in spec)
  2.1876 +    apply (drule_tac x=D in spec, auto)
  2.1877 +    done
  2.1878 +qed
  2.1879 +
  2.1880 +lemma closure_openin_Int_closure:
  2.1881 +  assumes ope: "openin (subtopology euclidean U) S" and "T \<subseteq> U"
  2.1882 +  shows "closure(S \<inter> closure T) = closure(S \<inter> T)"
  2.1883 +proof
  2.1884 +  obtain V where "open V" and S: "S = U \<inter> V"
  2.1885 +    using ope using openin_open by metis
  2.1886 +  show "closure (S \<inter> closure T) \<subseteq> closure (S \<inter> T)"
  2.1887 +    proof (clarsimp simp: S)
  2.1888 +      fix x
  2.1889 +      assume  "x \<in> closure (U \<inter> V \<inter> closure T)"
  2.1890 +      then have "V \<inter> closure T \<subseteq> A \<Longrightarrow> x \<in> closure A" for A
  2.1891 +          by (metis closure_mono subsetD inf.coboundedI2 inf_assoc)
  2.1892 +      then have "x \<in> closure (T \<inter> V)"
  2.1893 +         by (metis \<open>open V\<close> closure_closure inf_commute open_Int_closure_subset)
  2.1894 +      then show "x \<in> closure (U \<inter> V \<inter> T)"
  2.1895 +        by (metis \<open>T \<subseteq> U\<close> inf.absorb_iff2 inf_assoc inf_commute)
  2.1896 +    qed
  2.1897 +next
  2.1898 +  show "closure (S \<inter> T) \<subseteq> closure (S \<inter> closure T)"
  2.1899 +    by (meson Int_mono closure_mono closure_subset order_refl)
  2.1900 +qed
  2.1901 +
  2.1902 +lemma islimpt_in_closure: "(x islimpt S) = (x\<in>closure(S-{x}))"
  2.1903 +  unfolding closure_def using islimpt_punctured by blast
  2.1904 +
  2.1905 +lemma connected_imp_connected_closure: "connected S \<Longrightarrow> connected (closure S)"
  2.1906 +  by (rule connectedI) (meson closure_subset open_Int open_Int_closure_eq_empty subset_trans connectedD)
  2.1907 +
  2.1908 +lemma limpt_of_limpts: "x islimpt {y. y islimpt S} \<Longrightarrow> x islimpt S"
  2.1909 +  for x :: "'a::metric_space"
  2.1910 +  apply (clarsimp simp add: islimpt_approachable)
  2.1911 +  apply (drule_tac x="e/2" in spec)
  2.1912 +  apply (auto simp: simp del: less_divide_eq_numeral1)
  2.1913 +  apply (drule_tac x="dist x' x" in spec)
  2.1914 +  apply (auto simp: zero_less_dist_iff simp del: less_divide_eq_numeral1)
  2.1915 +  apply (erule rev_bexI)
  2.1916 +  apply (metis dist_commute dist_triangle_half_r less_trans less_irrefl)
  2.1917 +  done
  2.1918 +
  2.1919 +lemma closed_limpts:  "closed {x::'a::metric_space. x islimpt S}"
  2.1920 +  using closed_limpt limpt_of_limpts by blast
  2.1921 +
  2.1922 +lemma limpt_of_closure: "x islimpt closure S \<longleftrightarrow> x islimpt S"
  2.1923 +  for x :: "'a::metric_space"
  2.1924 +  by (auto simp: closure_def islimpt_Un dest: limpt_of_limpts)
  2.1925 +
  2.1926 +lemma closedin_limpt:
  2.1927 +  "closedin (subtopology euclidean T) S \<longleftrightarrow> S \<subseteq> T \<and> (\<forall>x. x islimpt S \<and> x \<in> T \<longrightarrow> x \<in> S)"
  2.1928 +  apply (simp add: closedin_closed, safe)
  2.1929 +   apply (simp add: closed_limpt islimpt_subset)
  2.1930 +  apply (rule_tac x="closure S" in exI, simp)
  2.1931 +  apply (force simp: closure_def)
  2.1932 +  done
  2.1933 +
  2.1934 +lemma closedin_closed_eq: "closed S \<Longrightarrow> closedin (subtopology euclidean S) T \<longleftrightarrow> closed T \<and> T \<subseteq> S"
  2.1935 +  by (meson closedin_limpt closed_subset closedin_closed_trans)
  2.1936 +
  2.1937 +lemma connected_closed_set:
  2.1938 +   "closed S
  2.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 = {})"
  2.1940 +  unfolding connected_closedin_eq closedin_closed_eq connected_closedin_eq by blast
  2.1941 +
  2.1942 +text \<open>If a connnected set is written as the union of two nonempty closed sets, then these sets
  2.1943 +have to intersect.\<close>
  2.1944 +
  2.1945 +lemma connected_as_closed_union:
  2.1946 +  assumes "connected C" "C = A \<union> B" "closed A" "closed B" "A \<noteq> {}" "B \<noteq> {}"
  2.1947 +  shows "A \<inter> B \<noteq> {}"
  2.1948 +by (metis assms closed_Un connected_closed_set)
  2.1949 +
  2.1950 +lemma closedin_subset_trans:
  2.1951 +  "closedin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
  2.1952 +    closedin (subtopology euclidean T) S"
  2.1953 +  by (meson closedin_limpt subset_iff)
  2.1954 +
  2.1955 +lemma openin_subset_trans:
  2.1956 +  "openin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
  2.1957 +    openin (subtopology euclidean T) S"
  2.1958 +  by (auto simp: openin_open)
  2.1959 +
  2.1960 +lemma openin_Times:
  2.1961 +  "openin (subtopology euclidean S) S' \<Longrightarrow> openin (subtopology euclidean T) T' \<Longrightarrow>
  2.1962 +    openin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
  2.1963 +  unfolding openin_open using open_Times by blast
  2.1964 +
  2.1965 +lemma Times_in_interior_subtopology:
  2.1966 +  fixes U :: "('a::metric_space \<times> 'b::metric_space) set"
  2.1967 +  assumes "(x, y) \<in> U" "openin (subtopology euclidean (S \<times> T)) U"
  2.1968 +  obtains V W where "openin (subtopology euclidean S) V" "x \<in> V"
  2.1969 +                    "openin (subtopology euclidean T) W" "y \<in> W" "(V \<times> W) \<subseteq> U"
  2.1970 +proof -
  2.1971 +  from assms obtain e where "e > 0" and "U \<subseteq> S \<times> T"
  2.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"
  2.1973 +    by (force simp: openin_euclidean_subtopology_iff)
  2.1974 +  with assms have "x \<in> S" "y \<in> T"
  2.1975 +    by auto
  2.1976 +  show ?thesis
  2.1977 +  proof
  2.1978 +    show "openin (subtopology euclidean S) (ball x (e/2) \<inter> S)"
  2.1979 +      by (simp add: Int_commute openin_open_Int)
  2.1980 +    show "x \<in> ball x (e / 2) \<inter> S"
  2.1981 +      by (simp add: \<open>0 < e\<close> \<open>x \<in> S\<close>)
  2.1982 +    show "openin (subtopology euclidean T) (ball y (e/2) \<inter> T)"
  2.1983 +      by (simp add: Int_commute openin_open_Int)
  2.1984 +    show "y \<in> ball y (e / 2) \<inter> T"
  2.1985 +      by (simp add: \<open>0 < e\<close> \<open>y \<in> T\<close>)
  2.1986 +    show "(ball x (e / 2) \<inter> S) \<times> (ball y (e / 2) \<inter> T) \<subseteq> U"
  2.1987 +      by clarify (simp add: e dist_Pair_Pair \<open>0 < e\<close> dist_commute sqrt_sum_squares_half_less)
  2.1988 +  qed
  2.1989 +qed
  2.1990 +
  2.1991 +lemma openin_Times_eq:
  2.1992 +  fixes S :: "'a::metric_space set" and T :: "'b::metric_space set"
  2.1993 +  shows
  2.1994 +    "openin (subtopology euclidean (S \<times> T)) (S' \<times> T') \<longleftrightarrow>
  2.1995 +      S' = {} \<or> T' = {} \<or> openin (subtopology euclidean S) S' \<and> openin (subtopology euclidean T) T'"
  2.1996 +    (is "?lhs = ?rhs")
  2.1997 +proof (cases "S' = {} \<or> T' = {}")
  2.1998 +  case True
  2.1999 +  then show ?thesis by auto
  2.2000 +next
  2.2001 +  case False
  2.2002 +  then obtain x y where "x \<in> S'" "y \<in> T'"
  2.2003 +    by blast
  2.2004 +  show ?thesis
  2.2005 +  proof
  2.2006 +    assume ?lhs
  2.2007 +    have "openin (subtopology euclidean S) S'"
  2.2008 +      apply (subst openin_subopen, clarify)
  2.2009 +      apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
  2.2010 +      using \<open>y \<in> T'\<close>
  2.2011 +       apply auto
  2.2012 +      done
  2.2013 +    moreover have "openin (subtopology euclidean T) T'"
  2.2014 +      apply (subst openin_subopen, clarify)
  2.2015 +      apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
  2.2016 +      using \<open>x \<in> S'\<close>
  2.2017 +       apply auto
  2.2018 +      done
  2.2019 +    ultimately show ?rhs
  2.2020 +      by simp
  2.2021 +  next
  2.2022 +    assume ?rhs
  2.2023 +    with False show ?lhs
  2.2024 +      by (simp add: openin_Times)
  2.2025 +  qed
  2.2026 +qed
  2.2027 +
  2.2028 +lemma closedin_Times:
  2.2029 +  "closedin (subtopology euclidean S) S' \<Longrightarrow> closedin (subtopology euclidean T) T' \<Longrightarrow>
  2.2030 +    closedin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
  2.2031 +  unfolding closedin_closed using closed_Times by blast
  2.2032 +
  2.2033 +lemma bdd_below_closure:
  2.2034 +  fixes A :: "real set"
  2.2035 +  assumes "bdd_below A"
  2.2036 +  shows "bdd_below (closure A)"
  2.2037 +proof -
  2.2038 +  from assms obtain m where "\<And>x. x \<in> A \<Longrightarrow> m \<le> x"
  2.2039 +    by (auto simp: bdd_below_def)
  2.2040 +  then have "A \<subseteq> {m..}" by auto
  2.2041 +  then have "closure A \<subseteq> {m..}"
  2.2042 +    using closed_real_atLeast by (rule closure_minimal)
  2.2043 +  then show ?thesis
  2.2044 +    by (auto simp: bdd_below_def)
  2.2045 +qed
  2.2046 +
  2.2047 +
  2.2048 +subsection \<open>Frontier (also known as boundary)\<close>
  2.2049 +
  2.2050 +definition%important "frontier S = closure S - interior S"
  2.2051 +
  2.2052 +lemma frontier_closed [iff]: "closed (frontier S)"
  2.2053 +  by (simp add: frontier_def closed_Diff)
  2.2054 +
  2.2055 +lemma frontier_closures: "frontier S = closure S \<inter> closure (- S)"
  2.2056 +  by (auto simp: frontier_def interior_closure)
  2.2057 +
  2.2058 +lemma frontier_Int: "frontier(S \<inter> T) = closure(S \<inter> T) \<inter> (frontier S \<union> frontier T)"
  2.2059 +proof -
  2.2060 +  have "closure (S \<inter> T) \<subseteq> closure S" "closure (S \<inter> T) \<subseteq> closure T"
  2.2061 +    by (simp_all add: closure_mono)
  2.2062 +  then show ?thesis
  2.2063 +    by (auto simp: frontier_closures)
  2.2064 +qed
  2.2065 +
  2.2066 +lemma frontier_Int_subset: "frontier(S \<inter> T) \<subseteq> frontier S \<union> frontier T"
  2.2067 +  by (auto simp: frontier_Int)
  2.2068 +
  2.2069 +lemma frontier_Int_closed:
  2.2070 +  assumes "closed S" "closed T"
  2.2071 +  shows "frontier(S \<inter> T) = (frontier S \<inter> T) \<union> (S \<inter> frontier T)"
  2.2072 +proof -
  2.2073 +  have "closure (S \<inter> T) = T \<inter> S"
  2.2074 +    using assms by (simp add: Int_commute closed_Int)
  2.2075 +  moreover have "T \<inter> (closure S \<inter> closure (- S)) = frontier S \<inter> T"
  2.2076 +    by (simp add: Int_commute frontier_closures)
  2.2077 +  ultimately show ?thesis
  2.2078 +    by (simp add: Int_Un_distrib Int_assoc Int_left_commute assms frontier_closures)
  2.2079 +qed
  2.2080 +
  2.2081 +lemma frontier_straddle:
  2.2082 +  fixes a :: "'a::metric_space"
  2.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))"
  2.2084 +  unfolding frontier_def closure_interior
  2.2085 +  by (auto simp: mem_interior subset_eq ball_def)
  2.2086 +
  2.2087 +lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
  2.2088 +  by (metis frontier_def closure_closed Diff_subset)
  2.2089 +
  2.2090 +lemma frontier_empty [simp]: "frontier {} = {}"
  2.2091 +  by (simp add: frontier_def)
  2.2092 +
  2.2093 +lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
  2.2094 +proof -
  2.2095 +  {
  2.2096 +    assume "frontier S \<subseteq> S"
  2.2097 +    then have "closure S \<subseteq> S"
  2.2098 +      using interior_subset unfolding frontier_def by auto
  2.2099 +    then have "closed S"
  2.2100 +      using closure_subset_eq by auto
  2.2101 +  }
  2.2102 +  then show ?thesis using frontier_subset_closed[of S] ..
  2.2103 +qed
  2.2104 +
  2.2105 +lemma frontier_complement [simp]: "frontier (- S) = frontier S"
  2.2106 +  by (auto simp: frontier_def closure_complement interior_complement)
  2.2107 +
  2.2108 +lemma frontier_Un_subset: "frontier(S \<union> T) \<subseteq> frontier S \<union> frontier T"
  2.2109 +  by (metis compl_sup frontier_Int_subset frontier_complement)
  2.2110 +
  2.2111 +lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
  2.2112 +  using frontier_complement frontier_subset_eq[of "- S"]
  2.2113 +  unfolding open_closed by auto
  2.2114 +
  2.2115 +lemma frontier_UNIV [simp]: "frontier UNIV = {}"
  2.2116 +  using frontier_complement frontier_empty by fastforce
  2.2117 +
  2.2118 +lemma frontier_interiors: "frontier s = - interior(s) - interior(-s)"
  2.2119 +  by (simp add: Int_commute frontier_def interior_closure)
  2.2120 +
  2.2121 +lemma frontier_interior_subset: "frontier(interior S) \<subseteq> frontier S"
  2.2122 +  by (simp add: Diff_mono frontier_interiors interior_mono interior_subset)
  2.2123 +
  2.2124 +lemma connected_Int_frontier:
  2.2125 +     "\<lbrakk>connected s; s \<inter> t \<noteq> {}; s - t \<noteq> {}\<rbrakk> \<Longrightarrow> (s \<inter> frontier t \<noteq> {})"
  2.2126 +  apply (simp add: frontier_interiors connected_openin, safe)
  2.2127 +  apply (drule_tac x="s \<inter> interior t" in spec, safe)
  2.2128 +   apply (drule_tac [2] x="s \<inter> interior (-t)" in spec)
  2.2129 +   apply (auto simp: disjoint_eq_subset_Compl dest: interior_subset [THEN subsetD])
  2.2130 +  done
  2.2131 +
  2.2132 +lemma closure_Un_frontier: "closure S = S \<union> frontier S"
  2.2133 +proof -
  2.2134 +  have "S \<union> interior S = S"
  2.2135 +    using interior_subset by auto
  2.2136 +  then show ?thesis
  2.2137 +    using closure_subset by (auto simp: frontier_def)
  2.2138 +qed
  2.2139 +
  2.2140 +
  2.2141 +subsection%unimportant \<open>Filters and the ``eventually true'' quantifier\<close>
  2.2142 +
  2.2143 +definition indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a filter"  (infixr "indirection" 70)
  2.2144 +  where "a indirection v = at a within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
  2.2145 +
  2.2146 +text \<open>Identify Trivial limits, where we can't approach arbitrarily closely.\<close>
  2.2147 +
  2.2148 +lemma trivial_limit_within: "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
  2.2149 +proof
  2.2150 +  assume "trivial_limit (at a within S)"
  2.2151 +  then show "\<not> a islimpt S"
  2.2152 +    unfolding trivial_limit_def
  2.2153 +    unfolding eventually_at_topological
  2.2154 +    unfolding islimpt_def
  2.2155 +    apply (clarsimp simp add: set_eq_iff)
  2.2156 +    apply (rename_tac T, rule_tac x=T in exI)
  2.2157 +    apply (clarsimp, drule_tac x=y in bspec, simp_all)
  2.2158 +    done
  2.2159 +next
  2.2160 +  assume "\<not> a islimpt S"
  2.2161 +  then show "trivial_limit (at a within S)"
  2.2162 +    unfolding trivial_limit_def eventually_at_topological islimpt_def
  2.2163 +    by metis
  2.2164 +qed
  2.2165 +
  2.2166 +lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
  2.2167 +  using trivial_limit_within [of a UNIV] by simp
  2.2168 +
  2.2169 +lemma trivial_limit_at: "\<not> trivial_limit (at a)"
  2.2170 +  for a :: "'a::perfect_space"
  2.2171 +  by (rule at_neq_bot)
  2.2172 +
  2.2173 +lemma trivial_limit_at_infinity:
  2.2174 +  "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,perfect_space}) filter)"
  2.2175 +  unfolding trivial_limit_def eventually_at_infinity
  2.2176 +  apply clarsimp
  2.2177 +  apply (subgoal_tac "\<exists>x::'a. x \<noteq> 0", clarify)
  2.2178 +   apply (rule_tac x="scaleR (b / norm x) x" in exI, simp)
  2.2179 +  apply (cut_tac islimpt_UNIV [of "0::'a", unfolded islimpt_def])
  2.2180 +  apply (drule_tac x=UNIV in spec, simp)
  2.2181 +  done
  2.2182 +
  2.2183 +lemma not_trivial_limit_within: "\<not> trivial_limit (at x within S) = (x \<in> closure (S - {x}))"
  2.2184 +  using islimpt_in_closure by (metis trivial_limit_within)
  2.2185 +
  2.2186 +lemma not_in_closure_trivial_limitI:
  2.2187 +  "x \<notin> closure s \<Longrightarrow> trivial_limit (at x within s)"
  2.2188 +  using not_trivial_limit_within[of x s]
  2.2189 +  by safe (metis Diff_empty Diff_insert0 closure_subset contra_subsetD)
  2.2190 +
  2.2191 +lemma filterlim_at_within_closure_implies_filterlim: "filterlim f l (at x within s)"
  2.2192 +  if "x \<in> closure s \<Longrightarrow> filterlim f l (at x within s)"
  2.2193 +  by (metis bot.extremum filterlim_filtercomap filterlim_mono not_in_closure_trivial_limitI that)
  2.2194 +
  2.2195 +lemma at_within_eq_bot_iff: "at c within A = bot \<longleftrightarrow> c \<notin> closure (A - {c})"
  2.2196 +  using not_trivial_limit_within[of c A] by blast
  2.2197 +
  2.2198 +text \<open>Some property holds "sufficiently close" to the limit point.\<close>
  2.2199 +
  2.2200 +lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
  2.2201 +  by simp
  2.2202 +
  2.2203 +lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
  2.2204 +  by (simp add: filter_eq_iff)
  2.2205 +
  2.2206 +
  2.2207 +subsection \<open>Limits\<close>
  2.2208 +
  2.2209 +proposition Lim: "(f \<longlongrightarrow> l) net \<longleftrightarrow> trivial_limit net \<or> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
  2.2210 +  by (auto simp: tendsto_iff trivial_limit_eq)
  2.2211 +
  2.2212 +text \<open>Show that they yield usual definitions in the various cases.\<close>
  2.2213 +
  2.2214 +proposition Lim_within_le: "(f \<longlongrightarrow> l)(at a within S) \<longleftrightarrow>
  2.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)"
  2.2216 +  by (auto simp: tendsto_iff eventually_at_le)
  2.2217 +
  2.2218 +proposition Lim_within: "(f \<longlongrightarrow> l) (at a within S) \<longleftrightarrow>
  2.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)"
  2.2220 +  by (auto simp: tendsto_iff eventually_at)
  2.2221 +
  2.2222 +corollary Lim_withinI [intro?]:
  2.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"
  2.2224 +  shows "(f \<longlongrightarrow> l) (at a within S)"
  2.2225 +  apply (simp add: Lim_within, clarify)
  2.2226 +  apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
  2.2227 +  done
  2.2228 +
  2.2229 +proposition Lim_at: "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow>
  2.2230 +    (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d  \<longrightarrow> dist (f x) l < e)"
  2.2231 +  by (auto simp: tendsto_iff eventually_at)
  2.2232 +
  2.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)"
  2.2234 +  by (auto simp: tendsto_iff eventually_at_infinity)
  2.2235 +
  2.2236 +corollary Lim_at_infinityI [intro?]:
  2.2237 +  assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>B. \<forall>x. norm x \<ge> B \<longrightarrow> dist (f x) l \<le> e"
  2.2238 +  shows "(f \<longlongrightarrow> l) at_infinity"
  2.2239 +  apply (simp add: Lim_at_infinity, clarify)
  2.2240 +  apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
  2.2241 +  done
  2.2242 +
  2.2243 +lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f \<longlongrightarrow> l) net"
  2.2244 +  by (rule topological_tendstoI) (auto elim: eventually_mono)
  2.2245 +
  2.2246 +lemma Lim_transform_within_set:
  2.2247 +  fixes a :: "'a::metric_space" and l :: "'b::metric_space"
  2.2248 +  shows "\<lbrakk>(f \<longlongrightarrow> l) (at a within S); eventually (\<lambda>x. x \<in> S \<longleftrightarrow> x \<in> T) (at a)\<rbrakk>
  2.2249 +         \<Longrightarrow> (f \<longlongrightarrow> l) (at a within T)"
  2.2250 +apply (clarsimp simp: eventually_at Lim_within)
  2.2251 +apply (drule_tac x=e in spec, clarify)
  2.2252 +apply (rename_tac k)
  2.2253 +apply (rule_tac x="min d k" in exI, simp)
  2.2254 +done
  2.2255 +
  2.2256 +lemma Lim_transform_within_set_eq:
  2.2257 +  fixes a l :: "'a::real_normed_vector"
  2.2258 +  shows "eventually (\<lambda>x. x \<in> s \<longleftrightarrow> x \<in> t) (at a)
  2.2259 +         \<Longrightarrow> ((f \<longlongrightarrow> l) (at a within s) \<longleftrightarrow> (f \<longlongrightarrow> l) (at a within t))"
  2.2260 +  by (force intro: Lim_transform_within_set elim: eventually_mono)
  2.2261 +
  2.2262 +lemma Lim_transform_within_openin:
  2.2263 +  fixes a :: "'a::metric_space"
  2.2264 +  assumes f: "(f \<longlongrightarrow> l) (at a within T)"
  2.2265 +    and "openin (subtopology euclidean T) S" "a \<in> S"
  2.2266 +    and eq: "\<And>x. \<lbrakk>x \<in> S; x \<noteq> a\<rbrakk> \<Longrightarrow> f x = g x"
  2.2267 +  shows "(g \<longlongrightarrow> l) (at a within T)"
  2.2268 +proof -
  2.2269 +  obtain \<epsilon> where "0 < \<epsilon>" and \<epsilon>: "ball a \<epsilon> \<inter> T \<subseteq> S"
  2.2270 +    using assms by (force simp: openin_contains_ball)
  2.2271 +  then have "a \<in> ball a \<epsilon>"
  2.2272 +    by simp
  2.2273 +  show ?thesis
  2.2274 +    by (rule Lim_transform_within [OF f \<open>0 < \<epsilon>\<close> eq]) (use \<epsilon> in \<open>auto simp: dist_commute subset_iff\<close>)
  2.2275 +qed
  2.2276 +
  2.2277 +lemma continuous_transform_within_openin:
  2.2278 +  fixes a :: "'a::metric_space"
  2.2279 +  assumes "continuous (at a within T) f"
  2.2280 +    and "openin (subtopology euclidean T) S" "a \<in> S"
  2.2281 +    and eq: "\<And>x. x \<in> S \<Longrightarrow> f x = g x"
  2.2282 +  shows "continuous (at a within T) g"
  2.2283 +  using assms by (simp add: Lim_transform_within_openin continuous_within)
  2.2284 +
  2.2285 +text \<open>The expected monotonicity property.\<close>
  2.2286 +
  2.2287 +lemma Lim_Un:
  2.2288 +  assumes "(f \<longlongrightarrow> l) (at x within S)" "(f \<longlongrightarrow> l) (at x within T)"
  2.2289 +  shows "(f \<longlongrightarrow> l) (at x within (S \<union> T))"
  2.2290 +  using assms unfolding at_within_union by (rule filterlim_sup)
  2.2291 +
  2.2292 +lemma Lim_Un_univ:
  2.2293 +  "(f \<longlongrightarrow> l) (at x within S) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within T) \<Longrightarrow>
  2.2294 +    S \<union> T = UNIV \<Longrightarrow> (f \<longlongrightarrow> l) (at x)"
  2.2295 +  by (metis Lim_Un)
  2.2296 +
  2.2297 +text \<open>Interrelations between restricted and unrestricted limits.\<close>
  2.2298 +
  2.2299 +lemma Lim_at_imp_Lim_at_within: "(f \<longlongrightarrow> l) (at x) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S)"
  2.2300 +  by (metis order_refl filterlim_mono subset_UNIV at_le)
  2.2301 +
  2.2302 +lemma eventually_within_interior:
  2.2303 +  assumes "x \<in> interior S"
  2.2304 +  shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)"
  2.2305 +  (is "?lhs = ?rhs")
  2.2306 +proof
  2.2307 +  from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S" ..
  2.2308 +  {
  2.2309 +    assume ?lhs
  2.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"
  2.2311 +      by (auto simp: eventually_at_topological)
  2.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"
  2.2313 +      by auto
  2.2314 +    then show ?rhs
  2.2315 +      by (auto simp: eventually_at_topological)
  2.2316 +  next
  2.2317 +    assume ?rhs
  2.2318 +    then show ?lhs
  2.2319 +      by (auto elim: eventually_mono simp: eventually_at_filter)
  2.2320 +  }
  2.2321 +qed
  2.2322 +
  2.2323 +lemma at_within_interior: "x \<in> interior S \<Longrightarrow> at x within S = at x"
  2.2324 +  unfolding filter_eq_iff by (intro allI eventually_within_interior)
  2.2325 +
  2.2326 +lemma Lim_within_LIMSEQ:
  2.2327 +  fixes a :: "'a::first_countable_topology"
  2.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"
  2.2329 +  shows "(X \<longlongrightarrow> L) (at a within T)"
  2.2330 +  using assms unfolding tendsto_def [where l=L]
  2.2331 +  by (simp add: sequentially_imp_eventually_within)
  2.2332 +
  2.2333 +lemma Lim_right_bound:
  2.2334 +  fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder, no_top} \<Rightarrow>
  2.2335 +    'b::{linorder_topology, conditionally_complete_linorder}"
  2.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"
  2.2337 +    and bnd: "\<And>a. a \<in> I \<Longrightarrow> x < a \<Longrightarrow> K \<le> f a"
  2.2338 +  shows "(f \<longlongrightarrow> Inf (f ` ({x<..} \<inter> I))) (at x within ({x<..} \<inter> I))"
  2.2339 +proof (cases "{x<..} \<inter> I = {}")
  2.2340 +  case True
  2.2341 +  then show ?thesis by simp
  2.2342 +next
  2.2343 +  case False
  2.2344 +  show ?thesis
  2.2345 +  proof (rule order_tendstoI)
  2.2346 +    fix a
  2.2347 +    assume a: "a < Inf (f ` ({x<..} \<inter> I))"
  2.2348 +    {
  2.2349 +      fix y
  2.2350 +      assume "y \<in> {x<..} \<inter> I"
  2.2351 +      with False bnd have "Inf (f ` ({x<..} \<inter> I)) \<le> f y"
  2.2352 +        by (auto intro!: cInf_lower bdd_belowI2)
  2.2353 +      with a have "a < f y"
  2.2354 +        by (blast intro: less_le_trans)
  2.2355 +    }
  2.2356 +    then show "eventually (\<lambda>x. a < f x) (at x within ({x<..} \<inter> I))"
  2.2357 +      by (auto simp: eventually_at_filter intro: exI[of _ 1] zero_less_one)
  2.2358 +  next
  2.2359 +    fix a
  2.2360 +    assume "Inf (f ` ({x<..} \<inter> I)) < a"
  2.2361 +    from cInf_lessD[OF _ this] False obtain y where y: "x < y" "y \<in> I" "f y < a"
  2.2362 +      by auto
  2.2363 +    then have "eventually (\<lambda>x. x \<in> I \<longrightarrow> f x < a) (at_right x)"
  2.2364 +      unfolding eventually_at_right[OF \<open>x < y\<close>] by (metis less_imp_le le_less_trans mono)
  2.2365 +    then show "eventually (\<lambda>x. f x < a) (at x within ({x<..} \<inter> I))"
  2.2366 +      unfolding eventually_at_filter by eventually_elim simp
  2.2367 +  qed
  2.2368 +qed
  2.2369 +
  2.2370 +text \<open>Another limit point characterization.\<close>
  2.2371 +
  2.2372 +lemma limpt_sequential_inj:
  2.2373 +  fixes x :: "'a::metric_space"
  2.2374 +  shows "x islimpt S \<longleftrightarrow>
  2.2375 +         (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> inj f \<and> (f \<longlongrightarrow> x) sequentially)"
  2.2376 +         (is "?lhs = ?rhs")
  2.2377 +proof
  2.2378 +  assume ?lhs
  2.2379 +  then have "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
  2.2380 +    by (force simp: islimpt_approachable)
  2.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"
  2.2382 +    by metis
  2.2383 +  define f where "f \<equiv> rec_nat (y 1) (\<lambda>n fn. y (min (inverse(2 ^ (Suc n))) (dist fn x)))"
  2.2384 +  have [simp]: "f 0 = y 1"
  2.2385 +               "f(Suc n) = y (min (inverse(2 ^ (Suc n))) (dist (f n) x))" for n
  2.2386 +    by (simp_all add: f_def)
  2.2387 +  have f: "f n \<in> S \<and> (f n \<noteq> x) \<and> dist (f n) x < inverse(2 ^ n)" for n
  2.2388 +  proof (induction n)
  2.2389 +    case 0 show ?case
  2.2390 +      by (simp add: y)
  2.2391 +  next
  2.2392 +    case (Suc n) then show ?case
  2.2393 +      apply (auto simp: y)
  2.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)
  2.2395 +  qed
  2.2396 +  show ?rhs
  2.2397 +  proof (rule_tac x=f in exI, intro conjI allI)
  2.2398 +    show "\<And>n. f n \<in> S - {x}"
  2.2399 +      using f by blast
  2.2400 +    have "dist (f n) x < dist (f m) x" if "m < n" for m n
  2.2401 +    using that
  2.2402 +    proof (induction n)
  2.2403 +      case 0 then show ?case by simp
  2.2404 +    next
  2.2405 +      case (Suc n)
  2.2406 +      then consider "m < n" | "m = n" using less_Suc_eq by blast
  2.2407 +      then show ?case
  2.2408 +      proof cases
  2.2409 +        assume "m < n"
  2.2410 +        have "dist (f(Suc n)) x = dist (y (min (inverse(2 ^ (Suc n))) (dist (f n) x))) x"
  2.2411 +          by simp
  2.2412 +        also have "\<dots> < dist (f n) x"
  2.2413 +          by (metis dist_pos_lt f min.strict_order_iff min_less_iff_conj y)
  2.2414 +        also have "\<dots> < dist (f m) x"
  2.2415 +          using Suc.IH \<open>m < n\<close> by blast
  2.2416 +        finally show ?thesis .
  2.2417 +      next
  2.2418 +        assume "m = n" then show ?case
  2.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)
  2.2420 +      qed
  2.2421 +    qed
  2.2422 +    then show "inj f"
  2.2423 +      by (metis less_irrefl linorder_injI)
  2.2424 +    show "f \<longlonglongrightarrow> x"
  2.2425 +      apply (rule tendstoI)
  2.2426 +      apply (rule_tac c="nat (ceiling(1/e))" in eventually_sequentiallyI)
  2.2427 +      apply (rule less_trans [OF f [THEN conjunct2, THEN conjunct2]])
  2.2428 +      apply (simp add: field_simps)
  2.2429 +      by (meson le_less_trans mult_less_cancel_left not_le of_nat_less_two_power)
  2.2430 +  qed
  2.2431 +next
  2.2432 +  assume ?rhs
  2.2433 +  then show ?lhs
  2.2434 +    by (fastforce simp add: islimpt_approachable lim_sequentially)
  2.2435 +qed
  2.2436 +
  2.2437 +(*could prove directly from islimpt_sequential_inj, but only for metric spaces*)
  2.2438 +lemma islimpt_sequential:
  2.2439 +  fixes x :: "'a::first_countable_topology"
  2.2440 +  shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> (f \<longlongrightarrow> x) sequentially)"
  2.2441 +    (is "?lhs = ?rhs")
  2.2442 +proof
  2.2443 +  assume ?lhs
  2.2444 +  from countable_basis_at_decseq[of x] obtain A where A:
  2.2445 +      "\<And>i. open (A i)"
  2.2446 +      "\<And>i. x \<in> A i"
  2.2447 +      "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  2.2448 +    by blast
  2.2449 +  define f where "f n = (SOME y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y)" for n
  2.2450 +  {
  2.2451 +    fix n
  2.2452 +    from \<open>?lhs\<close> have "\<exists>y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y"
  2.2453 +      unfolding islimpt_def using A(1,2)[of n] by auto
  2.2454 +    then have "f n \<in> S \<and> f n \<in> A n \<and> x \<noteq> f n"
  2.2455 +      unfolding f_def by (rule someI_ex)
  2.2456 +    then have "f n \<in> S" "f n \<in> A n" "x \<noteq> f n" by auto
  2.2457 +  }
  2.2458 +  then have "\<forall>n. f n \<in> S - {x}" by auto
  2.2459 +  moreover have "(\<lambda>n. f n) \<longlonglongrightarrow> x"
  2.2460 +  proof (rule topological_tendstoI)
  2.2461 +    fix S
  2.2462 +    assume "open S" "x \<in> S"
  2.2463 +    from A(3)[OF this] \<open>\<And>n. f n \<in> A n\<close>
  2.2464 +    show "eventually (\<lambda>x. f x \<in> S) sequentially"
  2.2465 +      by (auto elim!: eventually_mono)
  2.2466 +  qed
  2.2467 +  ultimately show ?rhs by fast
  2.2468 +next
  2.2469 +  assume ?rhs
  2.2470 +  then obtain f :: "nat \<Rightarrow> 'a" where f: "\<And>n. f n \<in> S - {x}" and lim: "f \<longlonglongrightarrow> x"
  2.2471 +    by auto
  2.2472 +  show ?lhs
  2.2473 +    unfolding islimpt_def
  2.2474 +  proof safe
  2.2475 +    fix T
  2.2476 +    assume "open T" "x \<in> T"
  2.2477 +    from lim[THEN topological_tendstoD, OF this] f
  2.2478 +    show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
  2.2479 +      unfolding eventually_sequentially by auto
  2.2480 +  qed
  2.2481 +qed
  2.2482 +
  2.2483 +lemma Lim_null:
  2.2484 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  2.2485 +  shows "(f \<longlongrightarrow> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) \<longlongrightarrow> 0) net"
  2.2486 +  by (simp add: Lim dist_norm)
  2.2487 +
  2.2488 +lemma Lim_null_comparison:
  2.2489 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  2.2490 +  assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g \<longlongrightarrow> 0) net"
  2.2491 +  shows "(f \<longlongrightarrow> 0) net"
  2.2492 +  using assms(2)
  2.2493 +proof (rule metric_tendsto_imp_tendsto)
  2.2494 +  show "eventually (\<lambda>x. dist (f x) 0 \<le> dist (g x) 0) net"
  2.2495 +    using assms(1) by (rule eventually_mono) (simp add: dist_norm)
  2.2496 +qed
  2.2497 +
  2.2498 +lemma Lim_transform_bound:
  2.2499 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  2.2500 +    and g :: "'a \<Rightarrow> 'c::real_normed_vector"
  2.2501 +  assumes "eventually (\<lambda>n. norm (f n) \<le> norm (g n)) net"
  2.2502 +    and "(g \<longlongrightarrow> 0) net"
  2.2503 +  shows "(f \<longlongrightarrow> 0) net"
  2.2504 +  using assms(1) tendsto_norm_zero [OF assms(2)]
  2.2505 +  by (rule Lim_null_comparison)
  2.2506 +
  2.2507 +lemma lim_null_mult_right_bounded:
  2.2508 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
  2.2509 +  assumes f: "(f \<longlongrightarrow> 0) F" and g: "eventually (\<lambda>x. norm(g x) \<le> B) F"
  2.2510 +    shows "((\<lambda>z. f z * g z) \<longlongrightarrow> 0) F"
  2.2511 +proof -
  2.2512 +  have *: "((\<lambda>x. norm (f x) * B) \<longlongrightarrow> 0) F"
  2.2513 +    by (simp add: f tendsto_mult_left_zero tendsto_norm_zero)
  2.2514 +  have "((\<lambda>x. norm (f x) * norm (g x)) \<longlongrightarrow> 0) F"
  2.2515 +    apply (rule Lim_null_comparison [OF _ *])
  2.2516 +    apply (simp add: eventually_mono [OF g] mult_left_mono)
  2.2517 +    done
  2.2518 +  then show ?thesis
  2.2519 +    by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
  2.2520 +qed
  2.2521 +
  2.2522 +lemma lim_null_mult_left_bounded:
  2.2523 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
  2.2524 +  assumes g: "eventually (\<lambda>x. norm(g x) \<le> B) F" and f: "(f \<longlongrightarrow> 0) F"
  2.2525 +    shows "((\<lambda>z. g z * f z) \<longlongrightarrow> 0) F"
  2.2526 +proof -
  2.2527 +  have *: "((\<lambda>x. B * norm (f x)) \<longlongrightarrow> 0) F"
  2.2528 +    by (simp add: f tendsto_mult_right_zero tendsto_norm_zero)
  2.2529 +  have "((\<lambda>x. norm (g x) * norm (f x)) \<longlongrightarrow> 0) F"
  2.2530 +    apply (rule Lim_null_comparison [OF _ *])
  2.2531 +    apply (simp add: eventually_mono [OF g] mult_right_mono)
  2.2532 +    done
  2.2533 +  then show ?thesis
  2.2534 +    by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
  2.2535 +qed
  2.2536 +
  2.2537 +lemma lim_null_scaleR_bounded:
  2.2538 +  assumes f: "(f \<longlongrightarrow> 0) net" and gB: "eventually (\<lambda>a. f a = 0 \<or> norm(g a) \<le> B) net"
  2.2539 +    shows "((\<lambda>n. f n *\<^sub>R g n) \<longlongrightarrow> 0) net"
  2.2540 +proof
  2.2541 +  fix \<epsilon>::real
  2.2542 +  assume "0 < \<epsilon>"
  2.2543 +  then have B: "0 < \<epsilon> / (abs B + 1)" by simp
  2.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
  2.2545 +  proof -
  2.2546 +    have "\<bar>f x\<bar> * norm (g x) \<le> \<bar>f x\<bar> * B"
  2.2547 +      by (simp add: mult_left_mono g)
  2.2548 +    also have "\<dots> \<le> \<bar>f x\<bar> * (\<bar>B\<bar> + 1)"
  2.2549 +      by (simp add: mult_left_mono)
  2.2550 +    also have "\<dots> < \<epsilon>"
  2.2551 +      by (rule f)
  2.2552 +    finally show ?thesis .
  2.2553 +  qed
  2.2554 +  show "\<forall>\<^sub>F x in net. dist (f x *\<^sub>R g x) 0 < \<epsilon>"
  2.2555 +    apply (rule eventually_mono [OF eventually_conj [OF tendstoD [OF f B] gB] ])
  2.2556 +    apply (auto simp: \<open>0 < \<epsilon>\<close> divide_simps * split: if_split_asm)
  2.2557 +    done
  2.2558 +qed
  2.2559 +
  2.2560 +text\<open>Deducing things about the limit from the elements.\<close>
  2.2561 +
  2.2562 +lemma Lim_in_closed_set:
  2.2563 +  assumes "closed S"
  2.2564 +    and "eventually (\<lambda>x. f(x) \<in> S) net"
  2.2565 +    and "\<not> trivial_limit net" "(f \<longlongrightarrow> l) net"
  2.2566 +  shows "l \<in> S"
  2.2567 +proof (rule ccontr)
  2.2568 +  assume "l \<notin> S"
  2.2569 +  with \<open>closed S\<close> have "open (- S)" "l \<in> - S"
  2.2570 +    by (simp_all add: open_Compl)
  2.2571 +  with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
  2.2572 +    by (rule topological_tendstoD)
  2.2573 +  with assms(2) have "eventually (\<lambda>x. False) net"
  2.2574 +    by (rule eventually_elim2) simp
  2.2575 +  with assms(3) show "False"
  2.2576 +    by (simp add: eventually_False)
  2.2577 +qed
  2.2578 +
  2.2579 +text\<open>Need to prove closed(cball(x,e)) before deducing this as a corollary.\<close>
  2.2580 +
  2.2581 +lemma Lim_dist_ubound:
  2.2582 +  assumes "\<not>(trivial_limit net)"
  2.2583 +    and "(f \<longlongrightarrow> l) net"
  2.2584 +    and "eventually (\<lambda>x. dist a (f x) \<le> e) net"
  2.2585 +  shows "dist a l \<le> e"
  2.2586 +  using assms by (fast intro: tendsto_le tendsto_intros)
  2.2587 +
  2.2588 +lemma Lim_norm_ubound:
  2.2589 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  2.2590 +  assumes "\<not>(trivial_limit net)" "(f \<longlongrightarrow> l) net" "eventually (\<lambda>x. norm(f x) \<le> e) net"
  2.2591 +  shows "norm(l) \<le> e"
  2.2592 +  using assms by (fast intro: tendsto_le tendsto_intros)
  2.2593 +
  2.2594 +lemma Lim_norm_lbound:
  2.2595 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  2.2596 +  assumes "\<not> trivial_limit net"
  2.2597 +    and "(f \<longlongrightarrow> l) net"
  2.2598 +    and "eventually (\<lambda>x. e \<le> norm (f x)) net"
  2.2599 +  shows "e \<le> norm l"
  2.2600 +  using assms by (fast intro: tendsto_le tendsto_intros)
  2.2601 +
  2.2602 +text\<open>Limit under bilinear function\<close>
  2.2603 +
  2.2604 +lemma Lim_bilinear:
  2.2605 +  assumes "(f \<longlongrightarrow> l) net"
  2.2606 +    and "(g \<longlongrightarrow> m) net"
  2.2607 +    and "bounded_bilinear h"
  2.2608 +  shows "((\<lambda>x. h (f x) (g x)) \<longlongrightarrow> (h l m)) net"
  2.2609 +  using \<open>bounded_bilinear h\<close> \<open>(f \<longlongrightarrow> l) net\<close> \<open>(g \<longlongrightarrow> m) net\<close>
  2.2610 +  by (rule bounded_bilinear.tendsto)
  2.2611 +
  2.2612 +text\<open>These are special for limits out of the same vector space.\<close>
  2.2613 +
  2.2614 +lemma Lim_within_id: "(id \<longlongrightarrow> a) (at a within s)"
  2.2615 +  unfolding id_def by (rule tendsto_ident_at)
  2.2616 +
  2.2617 +lemma Lim_at_id: "(id \<longlongrightarrow> a) (at a)"
  2.2618 +  unfolding id_def by (rule tendsto_ident_at)
  2.2619 +
  2.2620 +lemma Lim_at_zero:
  2.2621 +  fixes a :: "'a::real_normed_vector"
  2.2622 +    and l :: "'b::topological_space"
  2.2623 +  shows "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) \<longlongrightarrow> l) (at 0)"
  2.2624 +  using LIM_offset_zero LIM_offset_zero_cancel ..
  2.2625 +
  2.2626 +text\<open>It's also sometimes useful to extract the limit point from the filter.\<close>
  2.2627 +
  2.2628 +abbreviation netlimit :: "'a::t2_space filter \<Rightarrow> 'a"
  2.2629 +  where "netlimit F \<equiv> Lim F (\<lambda>x. x)"
  2.2630 +
  2.2631 +lemma netlimit_within: "\<not> trivial_limit (at a within S) \<Longrightarrow> netlimit (at a within S) = a"
  2.2632 +  by (rule tendsto_Lim) (auto intro: tendsto_intros)
  2.2633 +
  2.2634 +lemma netlimit_at [simp]:
  2.2635 +  fixes a :: "'a::{perfect_space,t2_space}"
  2.2636 +  shows "netlimit (at a) = a"
  2.2637 +  using netlimit_within [of a UNIV] by simp
  2.2638 +
  2.2639 +lemma lim_within_interior:
  2.2640 +  "x \<in> interior S \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S) \<longleftrightarrow> (f \<longlongrightarrow> l) (at x)"
  2.2641 +  by (metis at_within_interior)
  2.2642 +
  2.2643 +lemma netlimit_within_interior:
  2.2644 +  fixes x :: "'a::{t2_space,perfect_space}"
  2.2645 +  assumes "x \<in> interior S"
  2.2646 +  shows "netlimit (at x within S) = x"
  2.2647 +  using assms by (metis at_within_interior netlimit_at)
  2.2648 +
  2.2649 +lemma netlimit_at_vector:
  2.2650 +  fixes a :: "'a::real_normed_vector"
  2.2651 +  shows "netlimit (at a) = a"
  2.2652 +proof (cases "\<exists>x. x \<noteq> a")
  2.2653 +  case True then obtain x where x: "x \<noteq> a" ..
  2.2654 +  have "\<not> trivial_limit (at a)"
  2.2655 +    unfolding trivial_limit_def eventually_at dist_norm
  2.2656 +    apply clarsimp
  2.2657 +    apply (rule_tac x="a + scaleR (d / 2) (sgn (x - a))" in exI)
  2.2658 +    apply (simp add: norm_sgn sgn_zero_iff x)
  2.2659 +    done
  2.2660 +  then show ?thesis
  2.2661 +    by (rule netlimit_within [of a UNIV])
  2.2662 +qed simp
  2.2663 +
  2.2664 +
  2.2665 +text\<open>Useful lemmas on closure and set of possible sequential limits.\<close>
  2.2666 +
  2.2667 +lemma closure_sequential:
  2.2668 +  fixes l :: "'a::first_countable_topology"
  2.2669 +  shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially)"
  2.2670 +  (is "?lhs = ?rhs")
  2.2671 +proof
  2.2672 +  assume "?lhs"
  2.2673 +  moreover
  2.2674 +  {
  2.2675 +    assume "l \<in> S"
  2.2676 +    then have "?rhs" using tendsto_const[of l sequentially] by auto
  2.2677 +  }
  2.2678 +  moreover
  2.2679 +  {
  2.2680 +    assume "l islimpt S"
  2.2681 +    then have "?rhs" unfolding islimpt_sequential by auto
  2.2682 +  }
  2.2683 +  ultimately show "?rhs"
  2.2684 +    unfolding closure_def by auto
  2.2685 +next
  2.2686 +  assume "?rhs"
  2.2687 +  then show "?lhs" unfolding closure_def islimpt_sequential by auto
  2.2688 +qed
  2.2689 +
  2.2690 +lemma closed_sequential_limits:
  2.2691 +  fixes S :: "'a::first_countable_topology set"
  2.2692 +  shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially \<longrightarrow> l \<in> S)"
  2.2693 +by (metis closure_sequential closure_subset_eq subset_iff)
  2.2694 +
  2.2695 +lemma closure_approachable:
  2.2696 +  fixes S :: "'a::metric_space set"
  2.2697 +  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
  2.2698 +  apply (auto simp: closure_def islimpt_approachable)
  2.2699 +  apply (metis dist_self)
  2.2700 +  done
  2.2701 +
  2.2702 +lemma closure_approachable_le:
  2.2703 +  fixes S :: "'a::metric_space set"
  2.2704 +  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x \<le> e)"
  2.2705 +  unfolding closure_approachable
  2.2706 +  using dense by force
  2.2707 +
  2.2708 +lemma closure_approachableD:
  2.2709 +  assumes "x \<in> closure S" "e>0"
  2.2710 +  shows "\<exists>y\<in>S. dist x y < e"
  2.2711 +  using assms unfolding closure_approachable by (auto simp: dist_commute)
  2.2712 +
  2.2713 +lemma closed_approachable:
  2.2714 +  fixes S :: "'a::metric_space set"
  2.2715 +  shows "closed S \<Longrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
  2.2716 +  by (metis closure_closed closure_approachable)
  2.2717 +
  2.2718 +lemma closure_contains_Inf:
  2.2719 +  fixes S :: "real set"
  2.2720 +  assumes "S \<noteq> {}" "bdd_below S"
  2.2721 +  shows "Inf S \<in> closure S"
  2.2722 +proof -
  2.2723 +  have *: "\<forall>x\<in>S. Inf S \<le> x"
  2.2724 +    using cInf_lower[of _ S] assms by metis
  2.2725 +  {
  2.2726 +    fix e :: real
  2.2727 +    assume "e > 0"
  2.2728 +    then have "Inf S < Inf S + e" by simp
  2.2729 +    with assms obtain x where "x \<in> S" "x < Inf S + e"
  2.2730 +      by (subst (asm) cInf_less_iff) auto
  2.2731 +    with * have "\<exists>x\<in>S. dist x (Inf S) < e"
  2.2732 +      by (intro bexI[of _ x]) (auto simp: dist_real_def)
  2.2733 +  }
  2.2734 +  then show ?thesis unfolding closure_approachable by auto
  2.2735 +qed
  2.2736 +
  2.2737 +lemma closure_Int_ballI:
  2.2738 +  fixes S :: "'a :: metric_space set"
  2.2739 +  assumes "\<And>U. \<lbrakk>openin (subtopology euclidean S) U; U \<noteq> {}\<rbrakk> \<Longrightarrow> T \<inter> U \<noteq> {}"
  2.2740 + shows "S \<subseteq> closure T"
  2.2741 +proof (clarsimp simp: closure_approachable dist_commute)
  2.2742 +  fix x and e::real
  2.2743 +  assume "x \<in> S" "0 < e"
  2.2744 +  with assms [of "S \<inter> ball x e"] show "\<exists>y\<in>T. dist x y < e"
  2.2745 +    by force
  2.2746 +qed
  2.2747 +
  2.2748 +lemma closed_contains_Inf:
  2.2749 +  fixes S :: "real set"
  2.2750 +  shows "S \<noteq> {} \<Longrightarrow> bdd_below S \<Longrightarrow> closed S \<Longrightarrow> Inf S \<in> S"
  2.2751 +  by (metis closure_contains_Inf closure_closed)
  2.2752 +
  2.2753 +lemma closed_subset_contains_Inf:
  2.2754 +  fixes A C :: "real set"
  2.2755 +  shows "closed C \<Longrightarrow> A \<subseteq> C \<Longrightarrow> A \<noteq> {} \<Longrightarrow> bdd_below A \<Longrightarrow> Inf A \<in> C"
  2.2756 +  by (metis closure_contains_Inf closure_minimal subset_eq)
  2.2757 +
  2.2758 +lemma atLeastAtMost_subset_contains_Inf:
  2.2759 +  fixes A :: "real set" and a b :: real
  2.2760 +  shows "A \<noteq> {} \<Longrightarrow> a \<le> b \<Longrightarrow> A \<subseteq> {a..b} \<Longrightarrow> Inf A \<in> {a..b}"
  2.2761 +  by (rule closed_subset_contains_Inf)
  2.2762 +     (auto intro: closed_real_atLeastAtMost intro!: bdd_belowI[of A a])
  2.2763 +
  2.2764 +lemma not_trivial_limit_within_ball:
  2.2765 +  "\<not> trivial_limit (at x within S) \<longleftrightarrow> (\<forall>e>0. S \<inter> ball x e - {x} \<noteq> {})"
  2.2766 +  (is "?lhs \<longleftrightarrow> ?rhs")
  2.2767 +proof
  2.2768 +  show ?rhs if ?lhs
  2.2769 +  proof -
  2.2770 +    {
  2.2771 +      fix e :: real
  2.2772 +      assume "e > 0"
  2.2773 +      then obtain y where "y \<in> S - {x}" and "dist y x < e"
  2.2774 +        using \<open>?lhs\<close> not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
  2.2775 +        by auto
  2.2776 +      then have "y \<in> S \<inter> ball x e - {x}"
  2.2777 +        unfolding ball_def by (simp add: dist_commute)
  2.2778 +      then have "S \<inter> ball x e - {x} \<noteq> {}" by blast
  2.2779 +    }
  2.2780 +    then show ?thesis by auto
  2.2781 +  qed
  2.2782 +  show ?lhs if ?rhs
  2.2783 +  proof -
  2.2784 +    {
  2.2785 +      fix e :: real
  2.2786 +      assume "e > 0"
  2.2787 +      then obtain y where "y \<in> S \<inter> ball x e - {x}"
  2.2788 +        using \<open>?rhs\<close> by blast
  2.2789 +      then have "y \<in> S - {x}" and "dist y x < e"
  2.2790 +        unfolding ball_def by (simp_all add: dist_commute)
  2.2791 +      then have "\<exists>y \<in> S - {x}. dist y x < e"
  2.2792 +        by auto
  2.2793 +    }
  2.2794 +    then show ?thesis
  2.2795 +      using not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
  2.2796 +      by auto
  2.2797 +  qed
  2.2798 +qed
  2.2799 +
  2.2800 +lemma tendsto_If_within_closures:
  2.2801 +  assumes f: "x \<in> s \<union> (closure s \<inter> closure t) \<Longrightarrow>
  2.2802 +      (f \<longlongrightarrow> l x) (at x within s \<union> (closure s \<inter> closure t))"
  2.2803 +  assumes g: "x \<in> t \<union> (closure s \<inter> closure t) \<Longrightarrow>
  2.2804 +      (g \<longlongrightarrow> l x) (at x within t \<union> (closure s \<inter> closure t))"
  2.2805 +  assumes "x \<in> s \<union> t"
  2.2806 +  shows "((\<lambda>x. if x \<in> s then f x else g x) \<longlongrightarrow> l x) (at x within s \<union> t)"
  2.2807 +proof -
  2.2808 +  have *: "(s \<union> t) \<inter> {x. x \<in> s} = s" "(s \<union> t) \<inter> {x. x \<notin> s} = t - s"
  2.2809 +    by auto
  2.2810 +  have "(f \<longlongrightarrow> l x) (at x within s)"
  2.2811 +    by (rule filterlim_at_within_closure_implies_filterlim)
  2.2812 +       (use \<open>x \<in> _\<close> in \<open>auto simp: inf_commute closure_def intro: tendsto_within_subset[OF f]\<close>)
  2.2813 +  moreover
  2.2814 +  have "(g \<longlongrightarrow> l x) (at x within t - s)"
  2.2815 +    by (rule filterlim_at_within_closure_implies_filterlim)
  2.2816 +      (use \<open>x \<in> _\<close> in
  2.2817 +        \<open>auto intro!: tendsto_within_subset[OF g] simp: closure_def intro: islimpt_subset\<close>)
  2.2818 +  ultimately show ?thesis
  2.2819 +    by (intro filterlim_at_within_If) (simp_all only: *)
  2.2820 +qed
  2.2821 +
  2.2822 +
  2.2823 +subsection \<open>Boundedness\<close>
  2.2824 +
  2.2825 +  (* FIXME: This has to be unified with BSEQ!! *)
  2.2826 +definition%important (in metric_space) bounded :: "'a set \<Rightarrow> bool"
  2.2827 +  where "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
  2.2828 +
  2.2829 +lemma bounded_subset_cball: "bounded S \<longleftrightarrow> (\<exists>e x. S \<subseteq> cball x e \<and> 0 \<le> e)"
  2.2830 +  unfolding bounded_def subset_eq  by auto (meson order_trans zero_le_dist)
  2.2831 +
  2.2832 +lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
  2.2833 +  unfolding bounded_def
  2.2834 +  by auto (metis add.commute add_le_cancel_right dist_commute dist_triangle_le)
  2.2835 +
  2.2836 +lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
  2.2837 +  unfolding bounded_any_center [where a=0]
  2.2838 +  by (simp add: dist_norm)
  2.2839 +
  2.2840 +lemma bdd_above_norm: "bdd_above (norm ` X) \<longleftrightarrow> bounded X"
  2.2841 +  by (simp add: bounded_iff bdd_above_def)
  2.2842 +
  2.2843 +lemma bounded_norm_comp: "bounded ((\<lambda>x. norm (f x)) ` S) = bounded (f ` S)"
  2.2844 +  by (simp add: bounded_iff)
  2.2845 +
  2.2846 +lemma boundedI:
  2.2847 +  assumes "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> B"
  2.2848 +  shows "bounded S"
  2.2849 +  using assms bounded_iff by blast
  2.2850 +
  2.2851 +lemma bounded_empty [simp]: "bounded {}"
  2.2852 +  by (simp add: bounded_def)
  2.2853 +
  2.2854 +lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> bounded S"
  2.2855 +  by (metis bounded_def subset_eq)
  2.2856 +
  2.2857 +lemma bounded_interior[intro]: "bounded S \<Longrightarrow> bounded(interior S)"
  2.2858 +  by (metis bounded_subset interior_subset)
  2.2859 +
  2.2860 +lemma bounded_closure[intro]:
  2.2861 +  assumes "bounded S"
  2.2862 +  shows "bounded (closure S)"
  2.2863 +proof -
  2.2864 +  from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a"
  2.2865 +    unfolding bounded_def by auto
  2.2866 +  {
  2.2867 +    fix y
  2.2868 +    assume "y \<in> closure S"
  2.2869 +    then obtain f where f: "\<forall>n. f n \<in> S"  "(f \<longlongrightarrow> y) sequentially"
  2.2870 +      unfolding closure_sequential by auto
  2.2871 +    have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
  2.2872 +    then have "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
  2.2873 +      by (simp add: f(1))
  2.2874 +    have "dist x y \<le> a"
  2.2875 +      apply (rule Lim_dist_ubound [of sequentially f])
  2.2876 +      apply (rule trivial_limit_sequentially)
  2.2877 +      apply (rule f(2))
  2.2878 +      apply fact
  2.2879 +      done
  2.2880 +  }
  2.2881 +  then show ?thesis
  2.2882 +    unfolding bounded_def by auto
  2.2883 +qed
  2.2884 +
  2.2885 +lemma bounded_closure_image: "bounded (f ` closure S) \<Longrightarrow> bounded (f ` S)"
  2.2886 +  by (simp add: bounded_subset closure_subset image_mono)
  2.2887 +
  2.2888 +lemma bounded_cball[simp,intro]: "bounded (cball x e)"
  2.2889 +  apply (simp add: bounded_def)
  2.2890 +  apply (rule_tac x=x in exI)
  2.2891 +  apply (rule_tac x=e in exI, auto)
  2.2892 +  done
  2.2893 +
  2.2894 +lemma bounded_ball[simp,intro]: "bounded (ball x e)"
  2.2895 +  by (metis ball_subset_cball bounded_cball bounded_subset)
  2.2896 +
  2.2897 +lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
  2.2898 +  by (auto simp: bounded_def) (metis Un_iff bounded_any_center le_max_iff_disj)
  2.2899 +
  2.2900 +lemma bounded_Union[intro]: "finite F \<Longrightarrow> \<forall>S\<in>F. bounded S \<Longrightarrow> bounded (\<Union>F)"
  2.2901 +  by (induct rule: finite_induct[of F]) auto
  2.2902 +
  2.2903 +lemma bounded_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. bounded (B x) \<Longrightarrow> bounded (\<Union>x\<in>A. B x)"
  2.2904 +  by (induct set: finite) auto
  2.2905 +
  2.2906 +lemma bounded_insert [simp]: "bounded (insert x S) \<longleftrightarrow> bounded S"
  2.2907 +proof -
  2.2908 +  have "\<forall>y\<in>{x}. dist x y \<le> 0"
  2.2909 +    by simp
  2.2910 +  then have "bounded {x}"
  2.2911 +    unfolding bounded_def by fast
  2.2912 +  then show ?thesis
  2.2913 +    by (metis insert_is_Un bounded_Un)
  2.2914 +qed
  2.2915 +
  2.2916 +lemma bounded_subset_ballI: "S \<subseteq> ball x r \<Longrightarrow> bounded S"
  2.2917 +  by (meson bounded_ball bounded_subset)
  2.2918 +
  2.2919 +lemma bounded_subset_ballD:
  2.2920 +  assumes "bounded S" shows "\<exists>r. 0 < r \<and> S \<subseteq> ball x r"
  2.2921 +proof -
  2.2922 +  obtain e::real and y where "S \<subseteq> cball y e"  "0 \<le> e"
  2.2923 +    using assms by (auto simp: bounded_subset_cball)
  2.2924 +  then show ?thesis
  2.2925 +    apply (rule_tac x="dist x y + e + 1" in exI)
  2.2926 +    apply (simp add: add.commute add_pos_nonneg)
  2.2927 +    apply (erule subset_trans)
  2.2928 +    apply (clarsimp simp add: cball_def)
  2.2929 +    by (metis add_le_cancel_right add_strict_increasing dist_commute dist_triangle_le zero_less_one)
  2.2930 +qed
  2.2931 +
  2.2932 +lemma finite_imp_bounded [intro]: "finite S \<Longrightarrow> bounded S"
  2.2933 +  by (induct set: finite) simp_all
  2.2934 +
  2.2935 +lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x \<le> b)"
  2.2936 +  apply (simp add: bounded_iff)
  2.2937 +  apply (subgoal_tac "\<And>x (y::real). 0 < 1 + \<bar>y\<bar> \<and> (x \<le> y \<longrightarrow> x \<le> 1 + \<bar>y\<bar>)")
  2.2938 +  apply metis
  2.2939 +  apply arith
  2.2940 +  done
  2.2941 +
  2.2942 +lemma bounded_pos_less: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x < b)"
  2.2943 +  apply (simp add: bounded_pos)
  2.2944 +  apply (safe; rule_tac x="b+1" in exI; force)
  2.2945 +  done
  2.2946 +
  2.2947 +lemma Bseq_eq_bounded:
  2.2948 +  fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
  2.2949 +  shows "Bseq f \<longleftrightarrow> bounded (range f)"
  2.2950 +  unfolding Bseq_def bounded_pos by auto
  2.2951 +
  2.2952 +lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
  2.2953 +  by (metis Int_lower1 Int_lower2 bounded_subset)
  2.2954 +
  2.2955 +lemma bounded_diff[intro]: "bounded S \<Longrightarrow> bounded (S - T)"
  2.2956 +  by (metis Diff_subset bounded_subset)
  2.2957 +
  2.2958 +lemma not_bounded_UNIV[simp]:
  2.2959 +  "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
  2.2960 +proof (auto simp: bounded_pos not_le)
  2.2961 +  obtain x :: 'a where "x \<noteq> 0"
  2.2962 +    using perfect_choose_dist [OF zero_less_one] by fast
  2.2963 +  fix b :: real
  2.2964 +  assume b: "b >0"
  2.2965 +  have b1: "b +1 \<ge> 0"
  2.2966 +    using b by simp
  2.2967 +  with \<open>x \<noteq> 0\<close> have "b < norm (scaleR (b + 1) (sgn x))"
  2.2968 +    by (simp add: norm_sgn)
  2.2969 +  then show "\<exists>x::'a. b < norm x" ..
  2.2970 +qed
  2.2971 +
  2.2972 +corollary cobounded_imp_unbounded:
  2.2973 +    fixes S :: "'a::{real_normed_vector, perfect_space} set"
  2.2974 +    shows "bounded (- S) \<Longrightarrow> \<not> bounded S"
  2.2975 +  using bounded_Un [of S "-S"]  by (simp add: sup_compl_top)
  2.2976 +
  2.2977 +lemma bounded_dist_comp:
  2.2978 +  assumes "bounded (f ` S)" "bounded (g ` S)"
  2.2979 +  shows "bounded ((\<lambda>x. dist (f x) (g x)) ` S)"
  2.2980 +proof -
  2.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
  2.2982 +    by (auto simp: bounded_any_center[of _ undefined] dist_commute)
  2.2983 +  have "dist (f x) (g x) \<le> M1 + M2" if "x \<in> S" for x
  2.2984 +    using *[OF that]
  2.2985 +    by (rule order_trans[OF dist_triangle add_mono])
  2.2986 +  then show ?thesis
  2.2987 +    by (auto intro!: boundedI)
  2.2988 +qed
  2.2989 +
  2.2990 +lemma bounded_linear_image:
  2.2991 +  assumes "bounded S"
  2.2992 +    and "bounded_linear f"
  2.2993 +  shows "bounded (f ` S)"
  2.2994 +proof -
  2.2995 +  from assms(1) obtain b where "b > 0" and b: "\<forall>x\<in>S. norm x \<le> b"
  2.2996 +    unfolding bounded_pos by auto
  2.2997 +  from assms(2) obtain B where B: "B > 0" "\<forall>x. norm (f x) \<le> B * norm x"
  2.2998 +    using bounded_linear.pos_bounded by (auto simp: ac_simps)
  2.2999 +  show ?thesis
  2.3000 +    unfolding bounded_pos
  2.3001 +  proof (intro exI, safe)
  2.3002 +    show "norm (f x) \<le> B * b" if "x \<in> S" for x
  2.3003 +      by (meson B b less_imp_le mult_left_mono order_trans that)
  2.3004 +  qed (use \<open>b > 0\<close> \<open>B > 0\<close> in auto)
  2.3005 +qed
  2.3006 +
  2.3007 +lemma bounded_scaling:
  2.3008 +  fixes S :: "'a::real_normed_vector set"
  2.3009 +  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
  2.3010 +  apply (rule bounded_linear_image, assumption)
  2.3011 +  apply (rule bounded_linear_scaleR_right)
  2.3012 +  done
  2.3013 +
  2.3014 +lemma bounded_scaleR_comp:
  2.3015 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  2.3016 +  assumes "bounded (f ` S)"
  2.3017 +  shows "bounded ((\<lambda>x. r *\<^sub>R f x) ` S)"
  2.3018 +  using bounded_scaling[of "f ` S" r] assms
  2.3019 +  by (auto simp: image_image)
  2.3020 +
  2.3021 +lemma bounded_translation:
  2.3022 +  fixes S :: "'a::real_normed_vector set"
  2.3023 +  assumes "bounded S"
  2.3024 +  shows "bounded ((\<lambda>x. a + x) ` S)"
  2.3025 +proof -
  2.3026 +  from assms obtain b where b: "b > 0" "\<forall>x\<in>S. norm x \<le> b"
  2.3027 +    unfolding bounded_pos by auto
  2.3028 +  {
  2.3029 +    fix x
  2.3030 +    assume "x \<in> S"
  2.3031 +    then have "norm (a + x) \<le> b + norm a"
  2.3032 +      using norm_triangle_ineq[of a x] b by auto
  2.3033 +  }
  2.3034 +  then show ?thesis
  2.3035 +    unfolding bounded_pos
  2.3036 +    using norm_ge_zero[of a] b(1) and add_strict_increasing[of b 0 "norm a"]
  2.3037 +    by (auto intro!: exI[of _ "b + norm a"])
  2.3038 +qed
  2.3039 +
  2.3040 +lemma bounded_translation_minus:
  2.3041 +  fixes S :: "'a::real_normed_vector set"
  2.3042 +  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. x - a) ` S)"
  2.3043 +using bounded_translation [of S "-a"] by simp
  2.3044 +
  2.3045 +lemma bounded_uminus [simp]:
  2.3046 +  fixes X :: "'a::real_normed_vector set"
  2.3047 +  shows "bounded (uminus ` X) \<longleftrightarrow> bounded X"
  2.3048 +by (auto simp: bounded_def dist_norm; rule_tac x="-x" in exI; force simp: add.commute norm_minus_commute)
  2.3049 +
  2.3050 +lemma uminus_bounded_comp [simp]:
  2.3051 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  2.3052 +  shows "bounded ((\<lambda>x. - f x) ` S) \<longleftrightarrow> bounded (f ` S)"
  2.3053 +  using bounded_uminus[of "f ` S"]
  2.3054 +  by (auto simp: image_image)
  2.3055 +
  2.3056 +lemma bounded_plus_comp:
  2.3057 +  fixes f g::"'a \<Rightarrow> 'b::real_normed_vector"
  2.3058 +  assumes "bounded (f ` S)"
  2.3059 +  assumes "bounded (g ` S)"
  2.3060 +  shows "bounded ((\<lambda>x. f x + g x) ` S)"
  2.3061 +proof -
  2.3062 +  {
  2.3063 +    fix B C
  2.3064 +    assume "\<And>x. x\<in>S \<Longrightarrow> norm (f x) \<le> B" "\<And>x. x\<in>S \<Longrightarrow> norm (g x) \<le> C"
  2.3065 +    then have "\<And>x. x \<in> S \<Longrightarrow> norm (f x + g x) \<le> B + C"
  2.3066 +      by (auto intro!: norm_triangle_le add_mono)
  2.3067 +  } then show ?thesis
  2.3068 +    using assms by (fastforce simp: bounded_iff)
  2.3069 +qed
  2.3070 +
  2.3071 +lemma bounded_plus:
  2.3072 +  fixes S ::"'a::real_normed_vector set"
  2.3073 +  assumes "bounded S" "bounded T"
  2.3074 +  shows "bounded ((\<lambda>(x,y). x + y) ` (S \<times> T))"
  2.3075 +  using bounded_plus_comp [of fst "S \<times> T" snd] assms
  2.3076 +  by (auto simp: split_def split: if_split_asm)
  2.3077 +
  2.3078 +lemma bounded_minus_comp:
  2.3079 +  "bounded (f ` S) \<Longrightarrow> bounded (g ` S) \<Longrightarrow> bounded ((\<lambda>x. f x - g x) ` S)"
  2.3080 +  for f g::"'a \<Rightarrow> 'b::real_normed_vector"
  2.3081 +  using bounded_plus_comp[of "f" S "\<lambda>x. - g x"]
  2.3082 +  by auto
  2.3083 +
  2.3084 +lemma bounded_minus:
  2.3085 +  fixes S ::"'a::real_normed_vector set"
  2.3086 +  assumes "bounded S" "bounded T"
  2.3087 +  shows "bounded ((\<lambda>(x,y). x - y) ` (S \<times> T))"
  2.3088 +  using bounded_minus_comp [of fst "S \<times> T" snd] assms
  2.3089 +  by (auto simp: split_def split: if_split_asm)
  2.3090 +
  2.3091 +
  2.3092 +subsection \<open>Compactness\<close>
  2.3093 +
  2.3094 +subsubsection \<open>Bolzano-Weierstrass property\<close>
  2.3095 +
  2.3096 +proposition heine_borel_imp_bolzano_weierstrass:
  2.3097 +  assumes "compact s"
  2.3098 +    and "infinite t"
  2.3099 +    and "t \<subseteq> s"
  2.3100 +  shows "\<exists>x \<in> s. x islimpt t"
  2.3101 +proof (rule ccontr)
  2.3102 +  assume "\<not> (\<exists>x \<in> s. x islimpt t)"
  2.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)"
  2.3104 +    unfolding islimpt_def
  2.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)"]
  2.3106 +    by auto
  2.3107 +  obtain g where g: "g \<subseteq> {t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
  2.3108 +    using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]]
  2.3109 +    using f by auto
  2.3110 +  from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa"
  2.3111 +    by auto
  2.3112 +  {
  2.3113 +    fix x y
  2.3114 +    assume "x \<in> t" "y \<in> t" "f x = f y"
  2.3115 +    then have "x \<in> f x"  "y \<in> f x \<longrightarrow> y = x"
  2.3116 +      using f[THEN bspec[where x=x]] and \<open>t \<subseteq> s\<close> by auto
  2.3117 +    then have "x = y"
  2.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>
  2.3119 +      by auto
  2.3120 +  }
  2.3121 +  then have "inj_on f t"
  2.3122 +    unfolding inj_on_def by simp
  2.3123 +  then have "infinite (f ` t)"
  2.3124 +    using assms(2) using finite_imageD by auto
  2.3125 +  moreover
  2.3126 +  {
  2.3127 +    fix x
  2.3128 +    assume "x \<in> t" "f x \<notin> g"
  2.3129 +    from g(3) assms(3) \<open>x \<in> t\<close> obtain h where "h \<in> g" and "x \<in> h"
  2.3130 +      by auto
  2.3131 +    then obtain y where "y \<in> s" "h = f y"
  2.3132 +      using g'[THEN bspec[where x=h]] by auto
  2.3133 +    then have "y = x"
  2.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>]
  2.3135 +      by auto
  2.3136 +    then have False
  2.3137 +      using \<open>f x \<notin> g\<close> \<open>h \<in> g\<close> unfolding \<open>h = f y\<close>
  2.3138 +      by auto
  2.3139 +  }
  2.3140 +  then have "f ` t \<subseteq> g" by auto
  2.3141 +  ultimately show False
  2.3142 +    using g(2) using finite_subset by auto
  2.3143 +qed
  2.3144 +
  2.3145 +lemma acc_point_range_imp_convergent_subsequence:
  2.3146 +  fixes l :: "'a :: first_countable_topology"
  2.3147 +  assumes l: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> range f)"
  2.3148 +  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  2.3149 +proof -
  2.3150 +  from countable_basis_at_decseq[of l]
  2.3151 +  obtain A where A:
  2.3152 +      "\<And>i. open (A i)"
  2.3153 +      "\<And>i. l \<in> A i"
  2.3154 +      "\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  2.3155 +    by blast
  2.3156 +  define s where "s n i = (SOME j. i < j \<and> f j \<in> A (Suc n))" for n i
  2.3157 +  {
  2.3158 +    fix n i
  2.3159 +    have "infinite (A (Suc n) \<inter> range f - f`{.. i})"
  2.3160 +      using l A by auto
  2.3161 +    then have "\<exists>x. x \<in> A (Suc n) \<inter> range f - f`{.. i}"
  2.3162 +      unfolding ex_in_conv by (intro notI) simp
  2.3163 +    then have "\<exists>j. f j \<in> A (Suc n) \<and> j \<notin> {.. i}"
  2.3164 +      by auto
  2.3165 +    then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)"
  2.3166 +      by (auto simp: not_le)
  2.3167 +    then have "i < s n i" "f (s n i) \<in> A (Suc n)"
  2.3168 +      unfolding s_def by (auto intro: someI2_ex)
  2.3169 +  }
  2.3170 +  note s = this
  2.3171 +  define r where "r = rec_nat (s 0 0) s"
  2.3172 +  have "strict_mono r"
  2.3173 +    by (auto simp: r_def s strict_mono_Suc_iff)
  2.3174 +  moreover
  2.3175 +  have "(\<lambda>n. f (r n)) \<longlonglongrightarrow> l"
  2.3176 +  proof (rule topological_tendstoI)
  2.3177 +    fix S
  2.3178 +    assume "open S" "l \<in> S"
  2.3179 +    with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  2.3180 +      by auto
  2.3181 +    moreover
  2.3182 +    {
  2.3183 +      fix i
  2.3184 +      assume "Suc 0 \<le> i"
  2.3185 +      then have "f (r i) \<in> A i"
  2.3186 +        by (cases i) (simp_all add: r_def s)
  2.3187 +    }
  2.3188 +    then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially"
  2.3189 +      by (auto simp: eventually_sequentially)
  2.3190 +    ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"
  2.3191 +      by eventually_elim auto
  2.3192 +  qed
  2.3193 +  ultimately show "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  2.3194 +    by (auto simp: convergent_def comp_def)
  2.3195 +qed
  2.3196 +
  2.3197 +lemma sequence_infinite_lemma:
  2.3198 +  fixes f :: "nat \<Rightarrow> 'a::t1_space"
  2.3199 +  assumes "\<forall>n. f n \<noteq> l"
  2.3200 +    and "(f \<longlongrightarrow> l) sequentially"
  2.3201 +  shows "infinite (range f)"
  2.3202 +proof
  2.3203 +  assume "finite (range f)"
  2.3204 +  then have "closed (range f)"
  2.3205 +    by (rule finite_imp_closed)
  2.3206 +  then have "open (- range f)"
  2.3207 +    by (rule open_Compl)
  2.3208 +  from assms(1) have "l \<in> - range f"
  2.3209 +    by auto
  2.3210 +  from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
  2.3211 +    using \<open>open (- range f)\<close> \<open>l \<in> - range f\<close>
  2.3212 +    by (rule topological_tendstoD)
  2.3213 +  then show False
  2.3214 +    unfolding eventually_sequentially
  2.3215 +    by auto
  2.3216 +qed
  2.3217 +
  2.3218 +lemma closure_insert:
  2.3219 +  fixes x :: "'a::t1_space"
  2.3220 +  shows "closure (insert x s) = insert x (closure s)"
  2.3221 +  apply (rule closure_unique)
  2.3222 +  apply (rule insert_mono [OF closure_subset])
  2.3223 +  apply (rule closed_insert [OF closed_closure])
  2.3224 +  apply (simp add: closure_minimal)
  2.3225 +  done
  2.3226 +
  2.3227 +lemma islimpt_insert:
  2.3228 +  fixes x :: "'a::t1_space"
  2.3229 +  shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
  2.3230 +proof
  2.3231 +  assume *: "x islimpt (insert a s)"
  2.3232 +  show "x islimpt s"
  2.3233 +  proof (rule islimptI)
  2.3234 +    fix t
  2.3235 +    assume t: "x \<in> t" "open t"
  2.3236 +    show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
  2.3237 +    proof (cases "x = a")
  2.3238 +      case True
  2.3239 +      obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
  2.3240 +        using * t by (rule islimptE)
  2.3241 +      with \<open>x = a\<close> show ?thesis by auto
  2.3242 +    next
  2.3243 +      case False
  2.3244 +      with t have t': "x \<in> t - {a}" "open (t - {a})"
  2.3245 +        by (simp_all add: open_Diff)
  2.3246 +      obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
  2.3247 +        using * t' by (rule islimptE)
  2.3248 +      then show ?thesis by auto
  2.3249 +    qed
  2.3250 +  qed
  2.3251 +next
  2.3252 +  assume "x islimpt s"
  2.3253 +  then show "x islimpt (insert a s)"
  2.3254 +    by (rule islimpt_subset) auto
  2.3255 +qed
  2.3256 +
  2.3257 +lemma islimpt_finite:
  2.3258 +  fixes x :: "'a::t1_space"
  2.3259 +  shows "finite s \<Longrightarrow> \<not> x islimpt s"
  2.3260 +  by (induct set: finite) (simp_all add: islimpt_insert)
  2.3261 +
  2.3262 +lemma islimpt_Un_finite:
  2.3263 +  fixes x :: "'a::t1_space"
  2.3264 +  shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
  2.3265 +  by (simp add: islimpt_Un islimpt_finite)
  2.3266 +
  2.3267 +lemma islimpt_eq_acc_point:
  2.3268 +  fixes l :: "'a :: t1_space"
  2.3269 +  shows "l islimpt S \<longleftrightarrow> (\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S))"
  2.3270 +proof (safe intro!: islimptI)
  2.3271 +  fix U
  2.3272 +  assume "l islimpt S" "l \<in> U" "open U" "finite (U \<inter> S)"
  2.3273 +  then have "l islimpt S" "l \<in> (U - (U \<inter> S - {l}))" "open (U - (U \<inter> S - {l}))"
  2.3274 +    by (auto intro: finite_imp_closed)
  2.3275 +  then show False
  2.3276 +    by (rule islimptE) auto
  2.3277 +next
  2.3278 +  fix T
  2.3279 +  assume *: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S)" "l \<in> T" "open T"
  2.3280 +  then have "infinite (T \<inter> S - {l})"
  2.3281 +    by auto
  2.3282 +  then have "\<exists>x. x \<in> (T \<inter> S - {l})"
  2.3283 +    unfolding ex_in_conv by (intro notI) simp
  2.3284 +  then show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> l"
  2.3285 +    by auto
  2.3286 +qed
  2.3287 +
  2.3288 +corollary infinite_openin:
  2.3289 +  fixes S :: "'a :: t1_space set"
  2.3290 +  shows "\<lbrakk>openin (subtopology euclidean U) S; x \<in> S; x islimpt U\<rbrakk> \<Longrightarrow> infinite S"
  2.3291 +  by (clarsimp simp add: openin_open islimpt_eq_acc_point inf_commute)
  2.3292 +
  2.3293 +lemma islimpt_range_imp_convergent_subsequence:
  2.3294 +  fixes l :: "'a :: {t1_space, first_countable_topology}"
  2.3295 +  assumes l: "l islimpt (range f)"
  2.3296 +  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  2.3297 +  using l unfolding islimpt_eq_acc_point
  2.3298 +  by (rule acc_point_range_imp_convergent_subsequence)
  2.3299 +
  2.3300 +lemma islimpt_eq_infinite_ball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> ball x e))"
  2.3301 +  apply (simp add: islimpt_eq_acc_point, safe)
  2.3302 +   apply (metis Int_commute open_ball centre_in_ball)
  2.3303 +  by (metis open_contains_ball Int_mono finite_subset inf_commute subset_refl)
  2.3304 +
  2.3305 +lemma islimpt_eq_infinite_cball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> cball x e))"
  2.3306 +  apply (simp add: islimpt_eq_infinite_ball, safe)
  2.3307 +   apply (meson Int_mono ball_subset_cball finite_subset order_refl)
  2.3308 +  by (metis open_ball centre_in_ball finite_Int inf.absorb_iff2 inf_assoc open_contains_cball_eq)
  2.3309 +
  2.3310 +lemma sequence_unique_limpt:
  2.3311 +  fixes f :: "nat \<Rightarrow> 'a::t2_space"
  2.3312 +  assumes "(f \<longlongrightarrow> l) sequentially"
  2.3313 +    and "l' islimpt (range f)"
  2.3314 +  shows "l' = l"
  2.3315 +proof (rule ccontr)
  2.3316 +  assume "l' \<noteq> l"
  2.3317 +  obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
  2.3318 +    using hausdorff [OF \<open>l' \<noteq> l\<close>] by auto
  2.3319 +  have "eventually (\<lambda>n. f n \<in> t) sequentially"
  2.3320 +    using assms(1) \<open>open t\<close> \<open>l \<in> t\<close> by (rule topological_tendstoD)
  2.3321 +  then obtain N where "\<forall>n\<ge>N. f n \<in> t"
  2.3322 +    unfolding eventually_sequentially by auto
  2.3323 +
  2.3324 +  have "UNIV = {..<N} \<union> {N..}"
  2.3325 +    by auto
  2.3326 +  then have "l' islimpt (f ` ({..<N} \<union> {N..}))"
  2.3327 +    using assms(2) by simp
  2.3328 +  then have "l' islimpt (f ` {..<N} \<union> f ` {N..})"
  2.3329 +    by (simp add: image_Un)
  2.3330 +  then have "l' islimpt (f ` {N..})"
  2.3331 +    by (simp add: islimpt_Un_finite)
  2.3332 +  then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
  2.3333 +    using \<open>l' \<in> s\<close> \<open>open s\<close> by (rule islimptE)
  2.3334 +  then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'"
  2.3335 +    by auto
  2.3336 +  with \<open>\<forall>n\<ge>N. f n \<in> t\<close> have "f n \<in> s \<inter> t"
  2.3337 +    by simp
  2.3338 +  with \<open>s \<inter> t = {}\<close> show False
  2.3339 +    by simp
  2.3340 +qed
  2.3341 +
  2.3342 +lemma bolzano_weierstrass_imp_closed:
  2.3343 +  fixes s :: "'a::{first_countable_topology,t2_space} set"
  2.3344 +  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
  2.3345 +  shows "closed s"
  2.3346 +proof -
  2.3347 +  {
  2.3348 +    fix x l
  2.3349 +    assume as: "\<forall>n::nat. x n \<in> s" "(x \<longlongrightarrow> l) sequentially"
  2.3350 +    then have "l \<in> s"
  2.3351 +    proof (cases "\<forall>n. x n \<noteq> l")
  2.3352 +      case False
  2.3353 +      then show "l\<in>s" using as(1) by auto
  2.3354 +    next
  2.3355 +      case True note cas = this
  2.3356 +      with as(2) have "infinite (range x)"
  2.3357 +        using sequence_infinite_lemma[of x l] by auto
  2.3358 +      then obtain l' where "l'\<in>s" "l' islimpt (range x)"
  2.3359 +        using assms[THEN spec[where x="range x"]] as(1) by auto
  2.3360 +      then show "l\<in>s" using sequence_unique_limpt[of x l l']
  2.3361 +        using as cas by auto
  2.3362 +    qed
  2.3363 +  }
  2.3364 +  then show ?thesis
  2.3365 +    unfolding closed_sequential_limits by fast
  2.3366 +qed
  2.3367 +
  2.3368 +lemma compact_imp_bounded:
  2.3369 +  assumes "compact U"
  2.3370 +  shows "bounded U"
  2.3371 +proof -
  2.3372 +  have "compact U" "\<forall>x\<in>U. open (ball x 1)" "U \<subseteq> (\<Union>x\<in>U. ball x 1)"
  2.3373 +    using assms by auto
  2.3374 +  then obtain D where D: "D \<subseteq> U" "finite D" "U \<subseteq> (\<Union>x\<in>D. ball x 1)"
  2.3375 +    by (metis compactE_image)
  2.3376 +  from \<open>finite D\<close> have "bounded (\<Union>x\<in>D. ball x 1)"
  2.3377 +    by (simp add: bounded_UN)
  2.3378 +  then show "bounded U" using \<open>U \<subseteq> (\<Union>x\<in>D. ball x 1)\<close>
  2.3379 +    by (rule bounded_subset)
  2.3380 +qed
  2.3381 +
  2.3382 +text\<open>In particular, some common special cases.\<close>
  2.3383 +
  2.3384 +lemma compact_Un [intro]:
  2.3385 +  assumes "compact s"
  2.3386 +    and "compact t"
  2.3387 +  shows " compact (s \<union> t)"
  2.3388 +proof (rule compactI)
  2.3389 +  fix f
  2.3390 +  assume *: "Ball f open" "s \<union> t \<subseteq> \<Union>f"
  2.3391 +  from * \<open>compact s\<close> obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"
  2.3392 +    unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
  2.3393 +  moreover
  2.3394 +  from * \<open>compact t\<close> obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"
  2.3395 +    unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
  2.3396 +  ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"
  2.3397 +    by (auto intro!: exI[of _ "s' \<union> t'"])
  2.3398 +qed
  2.3399 +
  2.3400 +lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"
  2.3401 +  by (induct set: finite) auto
  2.3402 +
  2.3403 +lemma compact_UN [intro]:
  2.3404 +  "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"
  2.3405 +  by (rule compact_Union) auto
  2.3406 +
  2.3407 +lemma closed_Int_compact [intro]:
  2.3408 +  assumes "closed s"
  2.3409 +    and "compact t"
  2.3410 +  shows "compact (s \<inter> t)"
  2.3411 +  using compact_Int_closed [of t s] assms
  2.3412 +  by (simp add: Int_commute)
  2.3413 +
  2.3414 +lemma compact_Int [intro]:
  2.3415 +  fixes s t :: "'a :: t2_space set"
  2.3416 +  assumes "compact s"
  2.3417 +    and "compact t"
  2.3418 +  shows "compact (s \<inter> t)"
  2.3419 +  using assms by (intro compact_Int_closed compact_imp_closed)
  2.3420 +
  2.3421 +lemma compact_sing [simp]: "compact {a}"
  2.3422 +  unfolding compact_eq_heine_borel by auto
  2.3423 +
  2.3424 +lemma compact_insert [simp]:
  2.3425 +  assumes "compact s"
  2.3426 +  shows "compact (insert x s)"
  2.3427 +proof -
  2.3428 +  have "compact ({x} \<union> s)"
  2.3429 +    using compact_sing assms by (rule compact_Un)
  2.3430 +  then show ?thesis by simp
  2.3431 +qed
  2.3432 +
  2.3433 +lemma finite_imp_compact: "finite s \<Longrightarrow> compact s"
  2.3434 +  by (induct set: finite) simp_all
  2.3435 +
  2.3436 +lemma open_delete:
  2.3437 +  fixes s :: "'a::t1_space set"
  2.3438 +  shows "open s \<Longrightarrow> open (s - {x})"
  2.3439 +  by (simp add: open_Diff)
  2.3440 +
  2.3441 +lemma openin_delete:
  2.3442 +  fixes a :: "'a :: t1_space"
  2.3443 +  shows "openin (subtopology euclidean u) s
  2.3444 +         \<Longrightarrow> openin (subtopology euclidean u) (s - {a})"
  2.3445 +by (metis Int_Diff open_delete openin_open)
  2.3446 +
  2.3447 +text\<open>Compactness expressed with filters\<close>
  2.3448 +
  2.3449 +lemma closure_iff_nhds_not_empty:
  2.3450 +  "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"
  2.3451 +proof safe
  2.3452 +  assume x: "x \<in> closure X"
  2.3453 +  fix S A
  2.3454 +  assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"
  2.3455 +  then have "x \<notin> closure (-S)"
  2.3456 +    by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)
  2.3457 +  with x have "x \<in> closure X - closure (-S)"
  2.3458 +    by auto
  2.3459 +  also have "\<dots> \<subseteq> closure (X \<inter> S)"
  2.3460 +    using \<open>open S\<close> open_Int_closure_subset[of S X] by (simp add: closed_Compl ac_simps)
  2.3461 +  finally have "X \<inter> S \<noteq> {}" by auto
  2.3462 +  then show False using \<open>X \<inter> A = {}\<close> \<open>S \<subseteq> A\<close> by auto
  2.3463 +next
  2.3464 +  assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"
  2.3465 +  from this[THEN spec, of "- X", THEN spec, of "- closure X"]
  2.3466 +  show "x \<in> closure X"
  2.3467 +    by (simp add: closure_subset open_Compl)
  2.3468 +qed
  2.3469 +
  2.3470 +corollary closure_Int_ball_not_empty:
  2.3471 +  assumes "S \<subseteq> closure T" "x \<in> S" "r > 0"
  2.3472 +  shows "T \<inter> ball x r \<noteq> {}"
  2.3473 +  using assms centre_in_ball closure_iff_nhds_not_empty by blast
  2.3474 +
  2.3475 +lemma compact_filter:
  2.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))"
  2.3477 +proof (intro allI iffI impI compact_fip[THEN iffD2] notI)
  2.3478 +  fix F
  2.3479 +  assume "compact U"
  2.3480 +  assume F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"
  2.3481 +  then have "U \<noteq> {}"
  2.3482 +    by (auto simp: eventually_False)
  2.3483 +
  2.3484 +  define Z where "Z = closure ` {A. eventually (\<lambda>x. x \<in> A) F}"
  2.3485 +  then have "\<forall>z\<in>Z. closed z"
  2.3486 +    by auto
  2.3487 +  moreover
  2.3488 +  have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"
  2.3489 +    unfolding Z_def by (auto elim: eventually_mono intro: set_mp[OF closure_subset])
  2.3490 +  have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"
  2.3491 +  proof (intro allI impI)
  2.3492 +    fix B assume "finite B" "B \<subseteq> Z"
  2.3493 +    with \<open>finite B\<close> ev_Z F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"
  2.3494 +      by (auto simp: eventually_ball_finite_distrib eventually_conj_iff)
  2.3495 +    with F show "U \<inter> \<Inter>B \<noteq> {}"
  2.3496 +      by (intro notI) (simp add: eventually_False)
  2.3497 +  qed
  2.3498 +  ultimately have "U \<inter> \<Inter>Z \<noteq> {}"
  2.3499 +    using \<open>compact U\<close> unfolding compact_fip by blast
  2.3500 +  then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z"
  2.3501 +    by auto
  2.3502 +
  2.3503 +  have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"
  2.3504 +    unfolding eventually_inf eventually_nhds
  2.3505 +  proof safe
  2.3506 +    fix P Q R S
  2.3507 +    assume "eventually R F" "open S" "x \<in> S"
  2.3508 +    with open_Int_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]
  2.3509 +    have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)
  2.3510 +    moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"
  2.3511 +    ultimately show False by (auto simp: set_eq_iff)
  2.3512 +  qed
  2.3513 +  with \<open>x \<in> U\<close> show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"
  2.3514 +    by (metis eventually_bot)
  2.3515 +next
  2.3516 +  fix A
  2.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 = {}"
  2.3518 +  define F where "F = (INF a\<in>insert U A. principal a)"
  2.3519 +  have "F \<noteq> bot"
  2.3520 +    unfolding F_def
  2.3521 +  proof (rule INF_filter_not_bot)
  2.3522 +    fix X
  2.3523 +    assume X: "X \<subseteq> insert U A" "finite X"
  2.3524 +    with A(2)[THEN spec, of "X - {U}"] have "U \<inter> \<Inter>(X - {U}) \<noteq> {}"
  2.3525 +      by auto
  2.3526 +    with X show "(INF a\<in>X. principal a) \<noteq> bot"
  2.3527 +      by (auto simp: INF_principal_finite principal_eq_bot_iff)
  2.3528 +  qed
  2.3529 +  moreover
  2.3530 +  have "F \<le> principal U"
  2.3531 +    unfolding F_def by auto
  2.3532 +  then have "eventually (\<lambda>x. x \<in> U) F"
  2.3533 +    by (auto simp: le_filter_def eventually_principal)
  2.3534 +  moreover
  2.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)"
  2.3536 +  ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"
  2.3537 +    by auto
  2.3538 +
  2.3539 +  { fix V assume "V \<in> A"
  2.3540 +    then have "F \<le> principal V"
  2.3541 +      unfolding F_def by (intro INF_lower2[of V]) auto
  2.3542 +    then have V: "eventually (\<lambda>x. x \<in> V) F"
  2.3543 +      by (auto simp: le_filter_def eventually_principal)
  2.3544 +    have "x \<in> closure V"
  2.3545 +      unfolding closure_iff_nhds_not_empty
  2.3546 +    proof (intro impI allI)
  2.3547 +      fix S A
  2.3548 +      assume "open S" "x \<in> S" "S \<subseteq> A"
  2.3549 +      then have "eventually (\<lambda>x. x \<in> A) (nhds x)"
  2.3550 +        by (auto simp: eventually_nhds)
  2.3551 +      with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"
  2.3552 +        by (auto simp: eventually_inf)
  2.3553 +      with x show "V \<inter> A \<noteq> {}"
  2.3554 +        by (auto simp del: Int_iff simp add: trivial_limit_def)
  2.3555 +    qed
  2.3556 +    then have "x \<in> V"
  2.3557 +      using \<open>V \<in> A\<close> A(1) by simp
  2.3558 +  }
  2.3559 +  with \<open>x\<in>U\<close> have "x \<in> U \<inter> \<Inter>A" by auto
  2.3560 +  with \<open>U \<inter> \<Inter>A = {}\<close> show False by auto
  2.3561 +qed
  2.3562 +
  2.3563 +definition%important "countably_compact U \<longleftrightarrow>
  2.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))"
  2.3565 +
  2.3566 +lemma countably_compactE:
  2.3567 +  assumes "countably_compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C" "countable C"
  2.3568 +  obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
  2.3569 +  using assms unfolding countably_compact_def by metis
  2.3570 +
  2.3571 +lemma countably_compactI:
  2.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')"
  2.3573 +  shows "countably_compact s"
  2.3574 +  using assms unfolding countably_compact_def by metis
  2.3575 +
  2.3576 +lemma compact_imp_countably_compact: "compact U \<Longrightarrow> countably_compact U"
  2.3577 +  by (auto simp: compact_eq_heine_borel countably_compact_def)
  2.3578 +
  2.3579 +lemma countably_compact_imp_compact:
  2.3580 +  assumes "countably_compact U"
  2.3581 +    and ccover: "countable B" "\<forall>b\<in>B. open b"
  2.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"
  2.3583 +  shows "compact U"
  2.3584 +  using \<open>countably_compact U\<close>
  2.3585 +  unfolding compact_eq_heine_borel countably_compact_def
  2.3586 +proof safe
  2.3587 +  fix A
  2.3588 +  assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
  2.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)"
  2.3590 +  moreover define C where "C = {b\<in>B. \<exists>a\<in>A. b \<inter> U \<subseteq> a}"
  2.3591 +  ultimately have "countable C" "\<forall>a\<in>C. open a"
  2.3592 +    unfolding C_def using ccover by auto
  2.3593 +  moreover
  2.3594 +  have "\<Union>A \<inter> U \<subseteq> \<Union>C"
  2.3595 +  proof safe
  2.3596 +    fix x a
  2.3597 +    assume "x \<in> U" "x \<in> a" "a \<in> A"
  2.3598 +    with basis[of a x] A obtain b where "b \<in> B" "x \<in> b" "b \<inter> U \<subseteq> a"
  2.3599 +      by blast
  2.3600 +    with \<open>a \<in> A\<close> show "x \<in> \<Union>C"
  2.3601 +      unfolding C_def by auto
  2.3602 +  qed
  2.3603 +  then have "U \<subseteq> \<Union>C" using \<open>U \<subseteq> \<Union>A\<close> by auto
  2.3604 +  ultimately obtain T where T: "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"
  2.3605 +    using * by metis
  2.3606 +  then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<inter> U \<subseteq> a"
  2.3607 +    by (auto simp: C_def)
  2.3608 +  then obtain f where "\<forall>t\<in>T. f t \<in> A \<and> t \<inter> U \<subseteq> f t"
  2.3609 +    unfolding bchoice_iff Bex_def ..
  2.3610 +  with T show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  2.3611 +    unfolding C_def by (intro exI[of _ "f`T"]) fastforce
  2.3612 +qed
  2.3613 +
  2.3614 +proposition countably_compact_imp_compact_second_countable:
  2.3615 +  "countably_compact U \<Longrightarrow> compact (U :: 'a :: second_countable_topology set)"
  2.3616 +proof (rule countably_compact_imp_compact)
  2.3617 +  fix T and x :: 'a
  2.3618 +  assume "open T" "x \<in> T"
  2.3619 +  from topological_basisE[OF is_basis this] obtain b where
  2.3620 +    "b \<in> (SOME B. countable B \<and> topological_basis B)" "x \<in> b" "b \<subseteq> T" .
  2.3621 +  then show "\<exists>b\<in>SOME B. countable B \<and> topological_basis B. x \<in> b \<and> b \<inter> U \<subseteq> T"
  2.3622 +    by blast
  2.3623 +qed (insert countable_basis topological_basis_open[OF is_basis], auto)
  2.3624 +
  2.3625 +lemma countably_compact_eq_compact:
  2.3626 +  "countably_compact U \<longleftrightarrow> compact (U :: 'a :: second_countable_topology set)"
  2.3627 +  using countably_compact_imp_compact_second_countable compact_imp_countably_compact by blast
  2.3628 +
  2.3629 +subsubsection\<open>Sequential compactness\<close>
  2.3630 +
  2.3631 +definition%important seq_compact :: "'a::topological_space set \<Rightarrow> bool"
  2.3632 +  where "seq_compact S \<longleftrightarrow>
  2.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))"
  2.3634 +
  2.3635 +lemma seq_compactI:
  2.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"
  2.3637 +  shows "seq_compact S"
  2.3638 +  unfolding seq_compact_def using assms by fast
  2.3639 +
  2.3640 +lemma seq_compactE:
  2.3641 +  assumes "seq_compact S" "\<forall>n. f n \<in> S"
  2.3642 +  obtains l r where "l \<in> S" "strict_mono (r :: nat \<Rightarrow> nat)" "((f \<circ> r) \<longlongrightarrow> l) sequentially"
  2.3643 +  using assms unfolding seq_compact_def by fast
  2.3644 +
  2.3645 +lemma closed_sequentially: (* TODO: move upwards *)
  2.3646 +  assumes "closed s" and "\<forall>n. f n \<in> s" and "f \<longlonglongrightarrow> l"
  2.3647 +  shows "l \<in> s"
  2.3648 +proof (rule ccontr)
  2.3649 +  assume "l \<notin> s"
  2.3650 +  with \<open>closed s\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "eventually (\<lambda>n. f n \<in> - s) sequentially"
  2.3651 +    by (fast intro: topological_tendstoD)
  2.3652 +  with \<open>\<forall>n. f n \<in> s\<close> show "False"
  2.3653 +    by simp
  2.3654 +qed
  2.3655 +
  2.3656 +lemma seq_compact_Int_closed:
  2.3657 +  assumes "seq_compact s" and "closed t"
  2.3658 +  shows "seq_compact (s \<inter> t)"
  2.3659 +proof (rule seq_compactI)
  2.3660 +  fix f assume "\<forall>n::nat. f n \<in> s \<inter> t"
  2.3661 +  hence "\<forall>n. f n \<in> s" and "\<forall>n. f n \<in> t"
  2.3662 +    by simp_all
  2.3663 +  from \<open>seq_compact s\<close> and \<open>\<forall>n. f n \<in> s\<close>
  2.3664 +  obtain l r where "l \<in> s" and r: "strict_mono r" and l: "(f \<circ> r) \<longlonglongrightarrow> l"
  2.3665 +    by (rule seq_compactE)
  2.3666 +  from \<open>\<forall>n. f n \<in> t\<close> have "\<forall>n. (f \<circ> r) n \<in> t"
  2.3667 +    by simp
  2.3668 +  from \<open>closed t\<close> and this and l have "l \<in> t"
  2.3669 +    by (rule closed_sequentially)
  2.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"
  2.3671 +    by fast
  2.3672 +qed
  2.3673 +
  2.3674 +lemma seq_compact_closed_subset:
  2.3675 +  assumes "closed s" and "s \<subseteq> t" and "seq_compact t"
  2.3676 +  shows "seq_compact s"
  2.3677 +  using assms seq_compact_Int_closed [of t s] by (simp add: Int_absorb1)
  2.3678 +
  2.3679 +lemma seq_compact_imp_countably_compact:
  2.3680 +  fixes U :: "'a :: first_countable_topology set"
  2.3681 +  assumes "seq_compact U"
  2.3682 +  shows "countably_compact U"
  2.3683 +proof (safe intro!: countably_compactI)
  2.3684 +  fix A
  2.3685 +  assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"
  2.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"
  2.3687 +    using \<open>seq_compact U\<close> by (fastforce simp: seq_compact_def subset_eq)
  2.3688 +  show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  2.3689 +  proof cases
  2.3690 +    assume "finite A"
  2.3691 +    with A show ?thesis by auto
  2.3692 +  next
  2.3693 +    assume "infinite A"
  2.3694 +    then have "A \<noteq> {}" by auto
  2.3695 +    show ?thesis
  2.3696 +    proof (rule ccontr)
  2.3697 +      assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
  2.3698 +      then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)"
  2.3699 +        by auto
  2.3700 +      then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T"
  2.3701 +        by metis
  2.3702 +      define X where "X n = X' (from_nat_into A ` {.. n})" for n
  2.3703 +      have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"
  2.3704 +        using \<open>A \<noteq> {}\<close> unfolding X_def by (intro T) (auto intro: from_nat_into)
  2.3705 +      then have "range X \<subseteq> U"
  2.3706 +        by auto
  2.3707 +      with subseq[of X] obtain r x where "x \<in> U" and r: "strict_mono r" "(X \<circ> r) \<longlonglongrightarrow> x"
  2.3708 +        by auto
  2.3709 +      from \<open>x\<in>U\<close> \<open>U \<subseteq> \<Union>A\<close> from_nat_into_surj[OF \<open>countable A\<close>]
  2.3710 +      obtain n where "x \<in> from_nat_into A n" by auto
  2.3711 +      with r(2) A(1) from_nat_into[OF \<open>A \<noteq> {}\<close>, of n]
  2.3712 +      have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"
  2.3713 +        unfolding tendsto_def by (auto simp: comp_def)
  2.3714 +      then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"
  2.3715 +        by (auto simp: eventually_sequentially)
  2.3716 +      moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"
  2.3717 +        by auto
  2.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"
  2.3719 +        by (auto intro!: exI[of _ "max n N"])
  2.3720 +      ultimately show False
  2.3721 +        by auto
  2.3722 +    qed
  2.3723 +  qed
  2.3724 +qed
  2.3725 +
  2.3726 +lemma compact_imp_seq_compact:
  2.3727 +  fixes U :: "'a :: first_countable_topology set"
  2.3728 +  assumes "compact U"
  2.3729 +  shows "seq_compact U"
  2.3730 +  unfolding seq_compact_def
  2.3731 +proof safe
  2.3732 +  fix X :: "nat \<Rightarrow> 'a"
  2.3733 +  assume "\<forall>n. X n \<in> U"
  2.3734 +  then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"
  2.3735 +    by (auto simp: eventually_filtermap)
  2.3736 +  moreover
  2.3737 +  have "filtermap X sequentially \<noteq> bot"
  2.3738 +    by (simp add: trivial_limit_def eventually_filtermap)
  2.3739 +  ultimately
  2.3740 +  obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")
  2.3741 +    using \<open>compact U\<close> by (auto simp: compact_filter)
  2.3742 +
  2.3743 +  from countable_basis_at_decseq[of x]
  2.3744 +  obtain A where A:
  2.3745 +      "\<And>i. open (A i)"
  2.3746 +      "\<And>i. x \<in> A i"
  2.3747 +      "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  2.3748 +    by blast
  2.3749 +  define s where "s n i = (SOME j. i < j \<and> X j \<in> A (Suc n))" for n i
  2.3750 +  {
  2.3751 +    fix n i
  2.3752 +    have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"
  2.3753 +    proof (rule ccontr)
  2.3754 +      assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"
  2.3755 +      then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)"
  2.3756 +        by auto
  2.3757 +      then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"
  2.3758 +        by (auto simp: eventually_filtermap eventually_sequentially)
  2.3759 +      moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"
  2.3760 +        using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)
  2.3761 +      ultimately have "eventually (\<lambda>x. False) ?F"
  2.3762 +        by (auto simp: eventually_inf)
  2.3763 +      with x show False
  2.3764 +        by (simp add: eventually_False)
  2.3765 +    qed
  2.3766 +    then have "i < s n i" "X (s n i) \<in> A (Suc n)"
  2.3767 +      unfolding s_def by (auto intro: someI2_ex)
  2.3768 +  }
  2.3769 +  note s = this
  2.3770 +  define r where "r = rec_nat (s 0 0) s"
  2.3771 +  have "strict_mono r"
  2.3772 +    by (auto simp: r_def s strict_mono_Suc_iff)
  2.3773 +  moreover
  2.3774 +  have "(\<lambda>n. X (r n)) \<longlonglongrightarrow> x"
  2.3775 +  proof (rule topological_tendstoI)
  2.3776 +    fix S
  2.3777 +    assume "open S" "x \<in> S"
  2.3778 +    with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  2.3779 +      by auto
  2.3780 +    moreover
  2.3781 +    {
  2.3782 +      fix i
  2.3783 +      assume "Suc 0 \<le> i"
  2.3784 +      then have "X (r i) \<in> A i"
  2.3785 +        by (cases i) (simp_all add: r_def s)
  2.3786 +    }
  2.3787 +    then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially"
  2.3788 +      by (auto simp: eventually_sequentially)
  2.3789 +    ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"
  2.3790 +      by eventually_elim auto
  2.3791 +  qed
  2.3792 +  ultimately show "\<exists>x \<in> U. \<exists>r. strict_mono r \<and> (X \<circ> r) \<longlonglongrightarrow> x"
  2.3793 +    using \<open>x \<in> U\<close> by (auto simp: convergent_def comp_def)
  2.3794 +qed
  2.3795 +
  2.3796 +lemma countably_compact_imp_acc_point:
  2.3797 +  assumes "countably_compact s"
  2.3798 +    and "countable t"
  2.3799 +    and "infinite t"
  2.3800 +    and "t \<subseteq> s"
  2.3801 +  shows "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)"
  2.3802 +proof (rule ccontr)
  2.3803 +  define C where "C = (\<lambda>F. interior (F \<union> (- t))) ` {F. finite F \<and> F \<subseteq> t }"
  2.3804 +  note \<open>countably_compact s\<close>
  2.3805 +  moreover have "\<forall>t\<in>C. open t"
  2.3806 +    by (auto simp: C_def)
  2.3807 +  moreover
  2.3808 +  assume "\<not> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
  2.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
  2.3810 +  have "s \<subseteq> \<Union>C"
  2.3811 +    using \<open>t \<subseteq> s\<close>
  2.3812 +    unfolding C_def
  2.3813 +    apply (safe dest!: s)
  2.3814 +    apply (rule_tac a="U \<inter> t" in UN_I)
  2.3815 +    apply (auto intro!: interiorI simp add: finite_subset)
  2.3816 +    done
  2.3817 +  moreover
  2.3818 +  from \<open>countable t\<close> have "countable C"
  2.3819 +    unfolding C_def by (auto intro: countable_Collect_finite_subset)
  2.3820 +  ultimately
  2.3821 +  obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> \<Union>D"
  2.3822 +    by (rule countably_compactE)
  2.3823 +  then obtain E where E: "E \<subseteq> {F. finite F \<and> F \<subseteq> t }" "finite E"
  2.3824 +    and s: "s \<subseteq> (\<Union>F\<in>E. interior (F \<union> (- t)))"
  2.3825 +    by (metis (lifting) finite_subset_image C_def)
  2.3826 +  from s \<open>t \<subseteq> s\<close> have "t \<subseteq> \<Union>E"
  2.3827 +    using interior_subset by blast
  2.3828 +  moreover have "finite (\<Union>E)"
  2.3829 +    using E by auto
  2.3830 +  ultimately show False using \<open>infinite t\<close>
  2.3831 +    by (auto simp: finite_subset)
  2.3832 +qed
  2.3833 +
  2.3834 +lemma countable_acc_point_imp_seq_compact:
  2.3835 +  fixes s :: "'a::first_countable_topology set"
  2.3836 +  assumes "\<forall>t. infinite t \<and> countable t \<and> t \<subseteq> s \<longrightarrow>
  2.3837 +    (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
  2.3838 +  shows "seq_compact s"
  2.3839 +proof -
  2.3840 +  {
  2.3841 +    fix f :: "nat \<Rightarrow> 'a"
  2.3842 +    assume f: "\<forall>n. f n \<in> s"
  2.3843 +    have "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  2.3844 +    proof (cases "finite (range f)")
  2.3845 +      case True
  2.3846 +      obtain l where "infinite {n. f n = f l}"
  2.3847 +        using pigeonhole_infinite[OF _ True] by auto
  2.3848 +      then obtain r :: "nat \<Rightarrow> nat" where "strict_mono  r" and fr: "\<forall>n. f (r n) = f l"
  2.3849 +        using infinite_enumerate by blast
  2.3850 +      then have "strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> f l"
  2.3851 +        by (simp add: fr o_def)
  2.3852 +      with f show "\<exists>l\<in>s. \<exists>r. strict_mono  r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  2.3853 +        by auto
  2.3854 +    next
  2.3855 +      case False
  2.3856 +      with f assms have "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)"
  2.3857 +        by auto
  2.3858 +      then obtain l where "l \<in> s" "\<forall>U. l\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" ..
  2.3859 +      from this(2) have "\<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  2.3860 +        using acc_point_range_imp_convergent_subsequence[of l f] by auto
  2.3861 +      with \<open>l \<in> s\<close> show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially" ..
  2.3862 +    qed
  2.3863 +  }
  2.3864 +  then show ?thesis
  2.3865 +    unfolding seq_compact_def by auto
  2.3866 +qed
  2.3867 +
  2.3868 +lemma seq_compact_eq_countably_compact:
  2.3869 +  fixes U :: "'a :: first_countable_topology set"
  2.3870 +  shows "seq_compact U \<longleftrightarrow> countably_compact U"
  2.3871 +  using
  2.3872 +    countable_acc_point_imp_seq_compact
  2.3873 +    countably_compact_imp_acc_point
  2.3874 +    seq_compact_imp_countably_compact
  2.3875 +  by metis
  2.3876 +
  2.3877 +lemma seq_compact_eq_acc_point:
  2.3878 +  fixes s :: "'a :: first_countable_topology set"
  2.3879 +  shows "seq_compact s \<longleftrightarrow>
  2.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)))"
  2.3881 +  using
  2.3882 +    countable_acc_point_imp_seq_compact[of s]
  2.3883 +    countably_compact_imp_acc_point[of s]
  2.3884 +    seq_compact_imp_countably_compact[of s]
  2.3885 +  by metis
  2.3886 +
  2.3887 +lemma seq_compact_eq_compact:
  2.3888 +  fixes U :: "'a :: second_countable_topology set"
  2.3889 +  shows "seq_compact U \<longleftrightarrow> compact U"
  2.3890 +  using seq_compact_eq_countably_compact countably_compact_eq_compact by blast
  2.3891 +
  2.3892 +proposition bolzano_weierstrass_imp_seq_compact:
  2.3893 +  fixes s :: "'a::{t1_space, first_countable_topology} set"
  2.3894 +  shows "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> seq_compact s"
  2.3895 +  by (rule countable_acc_point_imp_seq_compact) (metis islimpt_eq_acc_point)
  2.3896 +
  2.3897 +
  2.3898 +subsubsection\<open>Totally bounded\<close>
  2.3899 +
  2.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)"
  2.3901 +  unfolding Cauchy_def by metis
  2.3902 +
  2.3903 +proposition seq_compact_imp_totally_bounded:
  2.3904 +  assumes "seq_compact s"
  2.3905 +  shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>x\<in>k. ball x e)"
  2.3906 +proof -
  2.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)"
  2.3908 +    let ?Q = "\<lambda>x n r. r \<in> s \<and> (\<forall>m < (n::nat). \<not> (dist (x m) r < e))"
  2.3909 +    have "\<exists>x. \<forall>n::nat. ?Q x n (x n)"
  2.3910 +    proof (rule dependent_wellorder_choice)
  2.3911 +      fix n x assume "\<And>y. y < n \<Longrightarrow> ?Q x y (x y)"
  2.3912 +      then have "\<not> s \<subseteq> (\<Union>x\<in>x ` {0..<n}. ball x e)"
  2.3913 +        using *[of "x ` {0 ..< n}"] by (auto simp: subset_eq)
  2.3914 +      then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)"
  2.3915 +        unfolding subset_eq by auto
  2.3916 +      show "\<exists>r. ?Q x n r"
  2.3917 +        using z by auto
  2.3918 +    qed simp
  2.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)"
  2.3920 +      by blast
  2.3921 +    then obtain l r where "l \<in> s" and r:"strict_mono  r" and "((x \<circ> r) \<longlongrightarrow> l) sequentially"
  2.3922 +      using assms by (metis seq_compact_def)
  2.3923 +    from this(3) have "Cauchy (x \<circ> r)"
  2.3924 +      using LIMSEQ_imp_Cauchy by auto
  2.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"
  2.3926 +      unfolding cauchy_def using \<open>e > 0\<close> by blast
  2.3927 +    then have False
  2.3928 +      using x[of "r N" "r (N+1)"] r by (auto simp: strict_mono_def) }
  2.3929 +  then show ?thesis
  2.3930 +    by metis
  2.3931 +qed
  2.3932 +
  2.3933 +subsubsection\<open>Heine-Borel theorem\<close>
  2.3934 +
  2.3935 +proposition seq_compact_imp_heine_borel:
  2.3936 +  fixes s :: "'a :: metric_space set"
  2.3937 +  assumes "seq_compact s"
  2.3938 +  shows "compact s"
  2.3939 +proof -
  2.3940 +  from seq_compact_imp_totally_bounded[OF \<open>seq_compact s\<close>]
  2.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)"
  2.3942 +    unfolding choice_iff' ..
  2.3943 +  define K where "K = (\<lambda>(x, r). ball x r) ` ((\<Union>e \<in> \<rat> \<inter> {0 <..}. f e) \<times> \<rat>)"
  2.3944 +  have "countably_compact s"
  2.3945 +    using \<open>seq_compact s\<close> by (rule seq_compact_imp_countably_compact)
  2.3946 +  then show "compact s"
  2.3947 +  proof (rule countably_compact_imp_compact)
  2.3948 +    show "countable K"
  2.3949 +      unfolding K_def using f
  2.3950 +      by (auto intro: countable_finite countable_subset countable_rat
  2.3951 +               intro!: countable_image countable_SIGMA countable_UN)
  2.3952 +    show "\<forall>b\<in>K. open b" by (auto simp: K_def)
  2.3953 +  next
  2.3954 +    fix T x
  2.3955 +    assume T: "open T" "x \<in> T" and x: "x \<in> s"
  2.3956 +    from openE[OF T] obtain e where "0 < e" "ball x e \<subseteq> T"
  2.3957 +      by auto
  2.3958 +    then have "0 < e / 2" "ball x (e / 2) \<subseteq> T"
  2.3959 +      by auto
  2.3960 +    from Rats_dense_in_real[OF \<open>0 < e / 2\<close>] obtain r where "r \<in> \<rat>" "0 < r" "r < e / 2"
  2.3961 +      by auto
  2.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"
  2.3963 +      by auto
  2.3964 +    from \<open>r \<in> \<rat>\<close> \<open>0 < r\<close> \<open>k \<in> f r\<close> have "ball k r \<in> K"
  2.3965 +      by (auto simp: K_def)
  2.3966 +    then show "\<exists>b\<in>K. x \<in> b \<and> b \<inter> s \<subseteq> T"
  2.3967 +    proof (rule bexI[rotated], safe)
  2.3968 +      fix y
  2.3969 +      assume "y \<in> ball k r"
  2.3970 +      with \<open>r < e / 2\<close> \<open>x \<in> ball k r\<close> have "dist x y < e"
  2.3971 +        by (intro dist_triangle_half_r [of k _ e]) (auto simp: dist_commute)
  2.3972 +      with \<open>ball x e \<subseteq> T\<close> show "y \<in> T"
  2.3973 +        by auto
  2.3974 +    next
  2.3975 +      show "x \<in> ball k r" by fact
  2.3976 +    qed
  2.3977 +  qed
  2.3978 +qed
  2.3979 +
  2.3980 +proposition compact_eq_seq_compact_metric:
  2.3981 +  "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"
  2.3982 +  using compact_imp_seq_compact seq_compact_imp_heine_borel by blast
  2.3983 +
  2.3984 +proposition compact_def: \<comment> \<open>this is the definition of compactness in HOL Light\<close>
  2.3985 +  "compact (S :: 'a::metric_space set) \<longleftrightarrow>
  2.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))"
  2.3987 +  unfolding compact_eq_seq_compact_metric seq_compact_def by auto
  2.3988 +
  2.3989 +subsubsection \<open>Complete the chain of compactness variants\<close>
  2.3990 +
  2.3991 +proposition compact_eq_bolzano_weierstrass:
  2.3992 +  fixes s :: "'a::metric_space set"
  2.3993 +  shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))"
  2.3994 +  (is "?lhs = ?rhs")
  2.3995 +proof
  2.3996 +  assume ?lhs
  2.3997 +  then show ?rhs
  2.3998 +    using heine_borel_imp_bolzano_weierstrass[of s] by auto
  2.3999 +next
  2.4000 +  assume ?rhs
  2.4001 +  then show ?lhs
  2.4002 +    unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)
  2.4003 +qed
  2.4004 +
  2.4005 +proposition bolzano_weierstrass_imp_bounded:
  2.4006 +  "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> bounded s"
  2.4007 +  using compact_imp_bounded unfolding compact_eq_bolzano_weierstrass .
  2.4008 +
  2.4009 +
  2.4010 +subsection \<open>Metric spaces with the Heine-Borel property\<close>
  2.4011 +
  2.4012 +text \<open>
  2.4013 +  A metric space (or topological vector space) is said to have the
  2.4014 +  Heine-Borel property if every closed and bounded subset is compact.
  2.4015 +\<close>
  2.4016 +
  2.4017 +class heine_borel = metric_space +
  2.4018 +  assumes bounded_imp_convergent_subsequence:
  2.4019 +    "bounded (range f) \<Longrightarrow> \<exists>l r. strict_mono (r::nat\<Rightarrow>nat) \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  2.4020 +
  2.4021 +proposition bounded_closed_imp_seq_compact:
  2.4022 +  fixes s::"'a::heine_borel set"
  2.4023 +  assumes "bounded s"
  2.4024 +    and "closed s"
  2.4025 +  shows "seq_compact s"
  2.4026 +proof (unfold seq_compact_def, clarify)
  2.4027 +  fix f :: "nat \<Rightarrow> 'a"
  2.4028 +  assume f: "\<forall>n. f n \<in> s"
  2.4029 +  with \<open>bounded s\<close> have "bounded (range f)"
  2.4030 +    by (auto intro: bounded_subset)
  2.4031 +  obtain l r where r: "strict_mono (r :: nat \<Rightarrow> nat)" and l: "((f \<circ> r) \<longlongrightarrow> l) sequentially"
  2.4032 +    using bounded_imp_convergent_subsequence [OF \<open>bounded (range f)\<close>] by auto
  2.4033 +  from f have fr: "\<forall>n. (f \<circ> r) n \<in> s"
  2.4034 +    by simp
  2.4035 +  have "l \<in> s" using \<open>closed s\<close> fr l
  2.4036 +    by (rule closed_sequentially)
  2.4037 +  show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  2.4038 +    using \<open>l \<in> s\<close> r l by blast
  2.4039 +qed
  2.4040 +
  2.4041 +lemma compact_eq_bounded_closed:
  2.4042 +  fixes s :: "'a::heine_borel set"
  2.4043 +  shows "compact s \<longleftrightarrow> bounded s \<and> closed s"
  2.4044 +  (is "?lhs = ?rhs")
  2.4045 +proof
  2.4046 +  assume ?lhs
  2.4047 +  then show ?rhs
  2.4048 +    using compact_imp_closed compact_imp_bounded
  2.4049 +    by blast
  2.4050 +next
  2.4051 +  assume ?rhs
  2.4052 +  then show ?lhs
  2.4053 +    using bounded_closed_imp_seq_compact[of s]
  2.4054 +    unfolding compact_eq_seq_compact_metric
  2.4055 +    by auto
  2.4056 +qed
  2.4057 +
  2.4058 +lemma compact_Inter:
  2.4059 +  fixes \<F> :: "'a :: heine_borel set set"
  2.4060 +  assumes com: "\<And>S. S \<in> \<F> \<Longrightarrow> compact S" and "\<F> \<noteq> {}"
  2.4061 +  shows "compact(\<Inter> \<F>)"
  2.4062 +  using assms
  2.4063 +  by (meson Inf_lower all_not_in_conv bounded_subset closed_Inter compact_eq_bounded_closed)
  2.4064 +
  2.4065 +lemma compact_closure [simp]:
  2.4066 +  fixes S :: "'a::heine_borel set"
  2.4067 +  shows "compact(closure S) \<longleftrightarrow> bounded S"
  2.4068 +by (meson bounded_closure bounded_subset closed_closure closure_subset compact_eq_bounded_closed)
  2.4069 +
  2.4070 +lemma not_compact_UNIV[simp]:
  2.4071 +  fixes s :: "'a::{real_normed_vector,perfect_space,heine_borel} set"
  2.4072 +  shows "\<not> compact (UNIV::'a set)"
  2.4073 +    by (simp add: compact_eq_bounded_closed)
  2.4074 +
  2.4075 +text\<open>Representing sets as the union of a chain of compact sets.\<close>
  2.4076 +lemma closed_Union_compact_subsets:
  2.4077 +  fixes S :: "'a::{heine_borel,real_normed_vector} set"
  2.4078 +  assumes "closed S"
  2.4079 +  obtains F where "\<And>n. compact(F n)" "\<And>n. F n \<subseteq> S" "\<And>n. F n \<subseteq> F(Suc n)"
  2.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"
  2.4081 +proof
  2.4082 +  show "compact (S \<inter> cball 0 (of_nat n))" for n
  2.4083 +    using assms compact_eq_bounded_closed by auto
  2.4084 +next
  2.4085 +  show "(\<Union>n. S \<inter> cball 0 (real n)) = S"
  2.4086 +    by (auto simp: real_arch_simple)
  2.4087 +next
  2.4088 +  fix K :: "'a set"
  2.4089 +  assume "compact K" "K \<subseteq> S"
  2.4090 +  then obtain N where "K \<subseteq> cball 0 N"
  2.4091 +    by (meson bounded_pos mem_cball_0 compact_imp_bounded subsetI)
  2.4092 +  then show "\<exists>N. \<forall>n\<ge>N. K \<subseteq> S \<inter> cball 0 (real n)"
  2.4093 +    by (metis of_nat_le_iff Int_subset_iff \<open>K \<subseteq> S\<close> real_arch_simple subset_cball subset_trans)
  2.4094 +qed auto
  2.4095 +
  2.4096 +instance%important real :: heine_borel
  2.4097 +proof%unimportant
  2.4098 +  fix f :: "nat \<Rightarrow> real"
  2.4099 +  assume f: "bounded (range f)"
  2.4100 +  obtain r :: "nat \<Rightarrow> nat" where r: "strict_mono r" "monoseq (f \<circ> r)"
  2.4101 +    unfolding comp_def by (metis seq_monosub)
  2.4102 +  then have "Bseq (f \<circ> r)"
  2.4103 +    unfolding Bseq_eq_bounded using f by (force intro: bounded_subset)
  2.4104 +  with r show "\<exists>l r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  2.4105 +    using Bseq_monoseq_convergent[of "f \<circ> r"] by (auto simp: convergent_def)
  2.4106 +qed
  2.4107 +
  2.4108 +lemma compact_lemma_general:
  2.4109 +  fixes f :: "nat \<Rightarrow> 'a"
  2.4110 +  fixes proj::"'a \<Rightarrow> 'b \<Rightarrow> 'c::heine_borel" (infixl "proj" 60)
  2.4111 +  fixes unproj:: "('b \<Rightarrow> 'c) \<Rightarrow> 'a"
  2.4112 +  assumes finite_basis: "finite basis"
  2.4113 +  assumes bounded_proj: "\<And>k. k \<in> basis \<Longrightarrow> bounded ((\<lambda>x. x proj k) ` range f)"
  2.4114 +  assumes proj_unproj: "\<And>e k. k \<in> basis \<Longrightarrow> (unproj e) proj k = e k"
  2.4115 +  assumes unproj_proj: "\<And>x. unproj (\<lambda>k. x proj k) = x"
  2.4116 +  shows "\<forall>d\<subseteq>basis. \<exists>l::'a. \<exists> r::nat\<Rightarrow>nat.
  2.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)"
  2.4118 +proof safe
  2.4119 +  fix d :: "'b set"
  2.4120 +  assume d: "d \<subseteq> basis"
  2.4121 +  with finite_basis have "finite d"
  2.4122 +    by (blast intro: finite_subset)
  2.4123 +  from this d show "\<exists>l::'a. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and>
  2.4124 +    (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) proj i) (l proj i) < e) sequentially)"
  2.4125 +  proof (induct d)
  2.4126 +    case empty
  2.4127 +    then show ?case
  2.4128 +      unfolding strict_mono_def by auto
  2.4129 +  next
  2.4130 +    case (insert k d)
  2.4131 +    have k[intro]: "k \<in> basis"
  2.4132 +      using insert by auto
  2.4133 +    have s': "bounded ((\<lambda>x. x proj k) ` range f)"
  2.4134 +      using k
  2.4135 +      by (rule bounded_proj)
  2.4136 +    obtain l1::"'a" and r1 where r1: "strict_mono r1"
  2.4137 +      and lr1: "\<forall>e > 0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) proj i) (l1 proj i) < e) sequentially"
  2.4138 +      using insert(3) using insert(4) by auto
  2.4139 +    have f': "\<forall>n. f (r1 n) proj k \<in> (\<lambda>x. x proj k) ` range f"
  2.4140 +      by simp
  2.4141 +    have "bounded (range (\<lambda>i. f (r1 i) proj k))"
  2.4142 +      by (metis (lifting) bounded_subset f' image_subsetI s')
  2.4143 +    then obtain l2 r2 where r2:"strict_mono r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) proj k) \<longlongrightarrow> l2) sequentially"
  2.4144 +      using bounded_imp_convergent_subsequence[of "\<lambda>i. f (r1 i) proj k"]
  2.4145 +      by (auto simp: o_def)
  2.4146 +    define r where "r = r1 \<circ> r2"
  2.4147 +    have r:"strict_mono r"
  2.4148 +      using r1 and r2 unfolding r_def o_def strict_mono_def by auto
  2.4149 +    moreover
  2.4150 +    define l where "l = unproj (\<lambda>i. if i = k then l2 else l1 proj i)"
  2.4151 +    {
  2.4152 +      fix e::real
  2.4153 +      assume "e > 0"
  2.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"
  2.4155 +        by blast
  2.4156 +      from lr2 \<open>e > 0\<close> have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) proj k) l2 < e) sequentially"
  2.4157 +        by (rule tendstoD)
  2.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"
  2.4159 +        by (rule eventually_subseq)
  2.4160 +      have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) proj i) (l proj i) < e) sequentially"
  2.4161 +        using N1' N2
  2.4162 +        by eventually_elim (insert insert.prems, auto simp: l_def r_def o_def proj_unproj)
  2.4163 +    }
  2.4164 +    ultimately show ?case by auto
  2.4165 +  qed
  2.4166 +qed
  2.4167 +
  2.4168 +lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
  2.4169 +  unfolding bounded_def
  2.4170 +  by (metis (erased, hide_lams) dist_fst_le image_iff order_trans)
  2.4171 +
  2.4172 +lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
  2.4173 +  unfolding bounded_def
  2.4174 +  by (metis (no_types, hide_lams) dist_snd_le image_iff order.trans)
  2.4175 +
  2.4176 +instance%important prod :: (heine_borel, heine_borel) heine_borel
  2.4177 +proof%unimportant
  2.4178 +  fix f :: "nat \<Rightarrow> 'a \<times> 'b"
  2.4179 +  assume f: "bounded (range f)"
  2.4180 +  then have "bounded (fst ` range f)"
  2.4181 +    by (rule bounded_fst)
  2.4182 +  then have s1: "bounded (range (fst \<circ> f))"
  2.4183 +    by (simp add: image_comp)
  2.4184 +  obtain l1 r1 where r1: "strict_mono r1" and l1: "(\<lambda>n. fst (f (r1 n))) \<longlonglongrightarrow> l1"
  2.4185 +    using bounded_imp_convergent_subsequence [OF s1] unfolding o_def by fast
  2.4186 +  from f have s2: "bounded (range (snd \<circ> f \<circ> r1))"
  2.4187 +    by (auto simp: image_comp intro: bounded_snd bounded_subset)
  2.4188 +  obtain l2 r2 where r2: "strict_mono r2" and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) \<longlongrightarrow> l2) sequentially"
  2.4189 +    using bounded_imp_convergent_subsequence [OF s2]
  2.4190 +    unfolding o_def by fast
  2.4191 +  have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) \<longlongrightarrow> l1) sequentially"
  2.4192 +    using LIMSEQ_subseq_LIMSEQ [OF l1 r2] unfolding o_def .
  2.4193 +  have l: "((f \<circ> (r1 \<circ> r2)) \<longlongrightarrow> (l1, l2)) sequentially"
  2.4194 +    using tendsto_Pair [OF l1' l2] unfolding o_def by simp
  2.4195 +  have r: "strict_mono (r1 \<circ> r2)"
  2.4196 +    using r1 r2 unfolding strict_mono_def by simp
  2.4197 +  show "\<exists>l r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
  2.4198 +    using l r by fast
  2.4199 +qed
  2.4200 +
  2.4201 +subsubsection \<open>Completeness\<close>
  2.4202 +
  2.4203 +proposition (in metric_space) completeI:
  2.4204 +  assumes "\<And>f. \<forall>n. f n \<in> s \<Longrightarrow> Cauchy f \<Longrightarrow> \<exists>l\<in>s. f \<longlonglongrightarrow> l"
  2.4205 +  shows "complete s"
  2.4206 +  using assms unfolding complete_def by fast
  2.4207 +
  2.4208 +proposition (in metric_space) completeE:
  2.4209 +  assumes "complete s" and "\<forall>n. f n \<in> s" and "Cauchy f"
  2.4210 +  obtains l where "l \<in> s" and "f \<longlonglongrightarrow> l"
  2.4211 +  using assms unfolding complete_def by fast
  2.4212 +
  2.4213 +(* TODO: generalize to uniform spaces *)
  2.4214 +lemma compact_imp_complete:
  2.4215 +  fixes s :: "'a::metric_space set"
  2.4216 +  assumes "compact s"
  2.4217 +  shows "complete s"
  2.4218 +proof -
  2.4219 +  {
  2.4220 +    fix f
  2.4221 +    assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
  2.4222 +    from as(1) obtain l r where lr: "l\<in>s" "strict_mono r" "(f \<circ> r) \<longlonglongrightarrow> l"
  2.4223 +      using assms unfolding compact_def by blast
  2.4224 +
  2.4225 +    note lr' = seq_suble [OF lr(2)]
  2.4226 +    {
  2.4227 +      fix e :: real
  2.4228 +      assume "e > 0"
  2.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"
  2.4230 +        unfolding cauchy_def
  2.4231 +        using \<open>e > 0\<close>
  2.4232 +        apply (erule_tac x="e/2" in allE, auto)
  2.4233 +        done
  2.4234 +      from lr(3)[unfolded lim_sequentially, THEN spec[where x="e/2"]]
  2.4235 +      obtain M where M:"\<forall>n\<ge>M. dist ((f \<circ> r) n) l < e/2"
  2.4236 +        using \<open>e > 0\<close> by auto
  2.4237 +      {
  2.4238 +        fix n :: nat
  2.4239 +        assume n: "n \<ge> max N M"
  2.4240 +        have "dist ((f \<circ> r) n) l < e/2"
  2.4241 +          using n M by auto
  2.4242 +        moreover have "r n \<ge> N"
  2.4243 +          using lr'[of n] n by auto
  2.4244 +        then have "dist (f n) ((f \<circ> r) n) < e / 2"
  2.4245 +          using N and n by auto
  2.4246 +        ultimately have "dist (f n) l < e"
  2.4247 +          using dist_triangle_half_r[of "f (r n)" "f n" e l]
  2.4248 +          by (auto simp: dist_commute)
  2.4249 +      }
  2.4250 +      then have "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast
  2.4251 +    }
  2.4252 +    then have "\<exists>l\<in>s. (f \<longlongrightarrow> l) sequentially" using \<open>l\<in>s\<close>
  2.4253 +      unfolding lim_sequentially by auto
  2.4254 +  }
  2.4255 +  then show ?thesis unfolding complete_def by auto
  2.4256 +qed
  2.4257 +
  2.4258 +proposition compact_eq_totally_bounded:
  2.4259 +  "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>x\<in>k. ball x e))"
  2.4260 +    (is "_ \<longleftrightarrow> ?rhs")
  2.4261 +proof
  2.4262 +  assume assms: "?rhs"
  2.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)"
  2.4264 +    by (auto simp: choice_iff')
  2.4265 +
  2.4266 +  show "compact s"
  2.4267 +  proof cases
  2.4268 +    assume "s = {}"
  2.4269 +    then show "compact s" by (simp add: compact_def)
  2.4270 +  next
  2.4271 +    assume "s \<noteq> {}"
  2.4272 +    show ?thesis
  2.4273 +      unfolding compact_def
  2.4274 +    proof safe
  2.4275 +      fix f :: "nat \<Rightarrow> 'a"
  2.4276 +      assume f: "\<forall>n. f n \<in> s"
  2.4277 +
  2.4278 +      define e where "e n = 1 / (2 * Suc n)" for n
  2.4279 +      then have [simp]: "\<And>n. 0 < e n" by auto
  2.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
  2.4281 +      {
  2.4282 +        fix n U
  2.4283 +        assume "infinite {n. f n \<in> U}"
  2.4284 +        then have "\<exists>b\<in>k (e n). infinite {i\<in>{n. f n \<in> U}. f i \<in> ball b (e n)}"
  2.4285 +          using k f by (intro pigeonhole_infinite_rel) (auto simp: subset_eq)
  2.4286 +        then obtain a where
  2.4287 +          "a \<in> k (e n)"
  2.4288 +          "infinite {i \<in> {n. f n \<in> U}. f i \<in> ball a (e n)}" ..
  2.4289 +        then have "\<exists>b. infinite {i. f i \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U)"
  2.4290 +          by (intro exI[of _ "ball a (e n) \<inter> U"] exI[of _ a]) (auto simp: ac_simps)
  2.4291 +        from someI_ex[OF this]
  2.4292 +        have "infinite {i. f i \<in> B n U}" "\<exists>x. B n U \<subseteq> ball x (e n) \<inter> U"
  2.4293 +          unfolding B_def by auto
  2.4294 +      }
  2.4295 +      note B = this
  2.4296 +
  2.4297 +      define F where "F = rec_nat (B 0 UNIV) B"
  2.4298 +      {
  2.4299 +        fix n
  2.4300 +        have "infinite {i. f i \<in> F n}"
  2.4301 +          by (induct n) (auto simp: F_def B)
  2.4302 +      }
  2.4303 +      then have F: "\<And>n. \<exists>x. F (Suc n) \<subseteq> ball x (e n) \<inter> F n"
  2.4304 +        using B by (simp add: F_def)
  2.4305 +      then have F_dec: "\<And>m n. m \<le> n \<Longrightarrow> F n \<subseteq> F m"
  2.4306 +        using decseq_SucI[of F] by (auto simp: decseq_def)
  2.4307 +
  2.4308 +      obtain sel where sel: "\<And>k i. i < sel k i" "\<And>k i. f (sel k i) \<in> F k"
  2.4309 +      proof (atomize_elim, unfold all_conj_distrib[symmetric], intro choice allI)
  2.4310 +        fix k i
  2.4311 +        have "infinite ({n. f n \<in> F k} - {.. i})"
  2.4312 +          using \<open>infinite {n. f n \<in> F k}\<close> by auto
  2.4313 +        from infinite_imp_nonempty[OF this]
  2.4314 +        show "\<exists>x>i. f x \<in> F k"
  2.4315 +          by (simp add: set_eq_iff not_le conj_commute)
  2.4316 +      qed
  2.4317 +
  2.4318 +      define t where "t = rec_nat (sel 0 0) (\<lambda>n i. sel (Suc n) i)"
  2.4319 +      have "strict_mono t"
  2.4320 +        unfolding strict_mono_Suc_iff by (simp add: t_def sel)
  2.4321 +      moreover have "\<forall>i. (f \<circ> t) i \<in> s"
  2.4322 +        using f by auto
  2.4323 +      moreover
  2.4324 +      {
  2.4325 +        fix n
  2.4326 +        have "(f \<circ> t) n \<in> F n"
  2.4327 +          by (cases n) (simp_all add: t_def sel)
  2.4328 +      }
  2.4329 +      note t = this
  2.4330 +
  2.4331 +      have "Cauchy (f \<circ> t)"
  2.4332 +      proof (safe intro!: metric_CauchyI exI elim!: nat_approx_posE)
  2.4333 +        fix r :: real and N n m
  2.4334 +        assume "1 / Suc N < r" "Suc N \<le> n" "Suc N \<le> m"
  2.4335 +        then have "(f \<circ> t) n \<in> F (Suc N)" "(f \<circ> t) m \<in> F (Suc N)" "2 * e N < r"
  2.4336 +          using F_dec t by (auto simp: e_def field_simps of_nat_Suc)
  2.4337 +        with F[of N] obtain x where "dist x ((f \<circ> t) n) < e N" "dist x ((f \<circ> t) m) < e N"
  2.4338 +          by (auto simp: subset_eq)
  2.4339 +        with dist_triangle[of "(f \<circ> t) m" "(f \<circ> t) n" x] \<open>2 * e N < r\<close>
  2.4340 +        show "dist ((f \<circ> t) m) ((f \<circ> t) n) < r"
  2.4341 +          by (simp add: dist_commute)
  2.4342 +      qed
  2.4343 +
  2.4344 +      ultimately show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
  2.4345 +        using assms unfolding complete_def by blast
  2.4346 +    qed
  2.4347 +  qed
  2.4348 +qed (metis compact_imp_complete compact_imp_seq_compact seq_compact_imp_totally_bounded)
  2.4349 +
  2.4350 +lemma cauchy_imp_bounded:
  2.4351 +  assumes "Cauchy s"
  2.4352 +  shows "bounded (range s)"
  2.4353 +proof -
  2.4354 +  from assms obtain N :: nat where "\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < 1"
  2.4355 +    unfolding cauchy_def by force
  2.4356 +  then have N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
  2.4357 +  moreover
  2.4358 +  have "bounded (s ` {0..N})"
  2.4359 +    using finite_imp_bounded[of "s ` {1..N}"] by auto
  2.4360 +  then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
  2.4361 +    unfolding bounded_any_center [where a="s N"] by auto
  2.4362 +  ultimately show "?thesis"
  2.4363 +    unfolding bounded_any_center [where a="s N"]
  2.4364 +    apply (rule_tac x="max a 1" in exI, auto)
  2.4365 +    apply (erule_tac x=y in allE)
  2.4366 +    apply (erule_tac x=y in ballE, auto)
  2.4367 +    done
  2.4368 +qed
  2.4369 +
  2.4370 +instance heine_borel < complete_space
  2.4371 +proof
  2.4372 +  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  2.4373 +  then have "bounded (range f)"
  2.4374 +    by (rule cauchy_imp_bounded)
  2.4375 +  then have "compact (closure (range f))"
  2.4376 +    unfolding compact_eq_bounded_closed by auto
  2.4377 +  then have "complete (closure (range f))"
  2.4378 +    by (rule compact_imp_complete)
  2.4379 +  moreover have "\<forall>n. f n \<in> closure (range f)"
  2.4380 +    using closure_subset [of "range f"] by auto
  2.4381 +  ultimately have "\<exists>l\<in>closure (range f). (f \<longlongrightarrow> l) sequentially"
  2.4382 +    using \<open>Cauchy f\<close> unfolding complete_def by auto
  2.4383 +  then show "convergent f"
  2.4384 +    unfolding convergent_def by auto
  2.4385 +qed
  2.4386 +
  2.4387 +lemma complete_UNIV: "complete (UNIV :: ('a::complete_space) set)"
  2.4388 +proof (rule completeI)
  2.4389 +  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  2.4390 +  then have "convergent f" by (rule Cauchy_convergent)
  2.4391 +  then show "\<exists>l\<in>UNIV. f \<longlonglongrightarrow> l" unfolding convergent_def by simp
  2.4392 +qed
  2.4393 +
  2.4394 +lemma complete_imp_closed:
  2.4395 +  fixes S :: "'a::metric_space set"
  2.4396 +  assumes "complete S"
  2.4397 +  shows "closed S"
  2.4398 +proof (unfold closed_sequential_limits, clarify)
  2.4399 +  fix f x assume "\<forall>n. f n \<in> S" and "f \<longlonglongrightarrow> x"
  2.4400 +  from \<open>f \<longlonglongrightarrow> x\<close> have "Cauchy f"
  2.4401 +    by (rule LIMSEQ_imp_Cauchy)
  2.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"
  2.4403 +    by (rule completeE)
  2.4404 +  from \<open>f \<longlonglongrightarrow> x\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "x = l"
  2.4405 +    by (rule LIMSEQ_unique)
  2.4406 +  with \<open>l \<in> S\<close> show "x \<in> S"
  2.4407 +    by simp
  2.4408 +qed
  2.4409 +
  2.4410 +lemma complete_Int_closed:
  2.4411 +  fixes S :: "'a::metric_space set"
  2.4412 +  assumes "complete S" and "closed t"
  2.4413 +  shows "complete (S \<inter> t)"
  2.4414 +proof (rule completeI)
  2.4415 +  fix f assume "\<forall>n. f n \<in> S \<inter> t" and "Cauchy f"
  2.4416 +  then have "\<forall>n. f n \<in> S" and "\<forall>n. f n \<in> t"
  2.4417 +    by simp_all
  2.4418 +  from \<open>complete S\<close> obtain l where "l \<in> S" and "f \<longlonglongrightarrow> l"
  2.4419 +    using \<open>\<forall>n. f n \<in> S\<close> and \<open>Cauchy f\<close> by (rule completeE)
  2.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"
  2.4421 +    by (rule closed_sequentially)
  2.4422 +  with \<open>l \<in> S\<close> and \<open>f \<longlonglongrightarrow> l\<close> show "\<exists>l\<in>S \<inter> t. f \<longlonglongrightarrow> l"
  2.4423 +    by fast
  2.4424 +qed
  2.4425 +
  2.4426 +lemma complete_closed_subset:
  2.4427 +  fixes S :: "'a::metric_space set"
  2.4428 +  assumes "closed S" and "S \<subseteq> t" and "complete t"
  2.4429 +  shows "complete S"
  2.4430 +  using assms complete_Int_closed [of t S] by (simp add: Int_absorb1)
  2.4431 +
  2.4432 +lemma complete_eq_closed:
  2.4433 +  fixes S :: "('a::complete_space) set"
  2.4434 +  shows "complete S \<longleftrightarrow> closed S"
  2.4435 +proof
  2.4436 +  assume "closed S" then show "complete S"
  2.4437 +    using subset_UNIV complete_UNIV by (rule complete_closed_subset)
  2.4438 +next
  2.4439 +  assume "complete S" then show "closed S"
  2.4440 +    by (rule complete_imp_closed)
  2.4441 +qed
  2.4442 +
  2.4443 +lemma convergent_eq_Cauchy:
  2.4444 +  fixes S :: "nat \<Rightarrow> 'a::complete_space"
  2.4445 +  shows "(\<exists>l. (S \<longlongrightarrow> l) sequentially) \<longleftrightarrow> Cauchy S"
  2.4446 +  unfolding Cauchy_convergent_iff convergent_def ..
  2.4447 +
  2.4448 +lemma convergent_imp_bounded:
  2.4449 +  fixes S :: "nat \<Rightarrow> 'a::metric_space"
  2.4450 +  shows "(S \<longlongrightarrow> l) sequentially \<Longrightarrow> bounded (range S)"
  2.4451 +  by (intro cauchy_imp_bounded LIMSEQ_imp_Cauchy)
  2.4452 +
  2.4453 +lemma frontier_subset_compact:
  2.4454 +  fixes S :: "'a::heine_borel set"
  2.4455 +  shows "compact S \<Longrightarrow> frontier S \<subseteq> S"
  2.4456 +  using frontier_subset_closed compact_eq_bounded_closed
  2.4457 +  by blast
  2.4458 +
  2.4459 +subsection \<open>Continuity\<close>
  2.4460 +
  2.4461 +text\<open>Derive the epsilon-delta forms, which we often use as "definitions"\<close>
  2.4462 +
  2.4463 +proposition continuous_within_eps_delta:
  2.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)"
  2.4465 +  unfolding continuous_within and Lim_within  by fastforce
  2.4466 +
  2.4467 +corollary continuous_at_eps_delta:
  2.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)"
  2.4469 +  using continuous_within_eps_delta [of x UNIV f] by simp
  2.4470 +
  2.4471 +lemma continuous_at_right_real_increasing:
  2.4472 +  fixes f :: "real \<Rightarrow> real"
  2.4473 +  assumes nondecF: "\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y"
  2.4474 +  shows "continuous (at_right a) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f (a + d) - f a < e)"
  2.4475 +  apply (simp add: greaterThan_def dist_real_def continuous_within Lim_within_le)
  2.4476 +  apply (intro all_cong ex_cong, safe)
  2.4477 +  apply (erule_tac x="a + d" in allE, simp)
  2.4478 +  apply (simp add: nondecF field_simps)
  2.4479 +  apply (drule nondecF, simp)
  2.4480 +  done
  2.4481 +
  2.4482 +lemma continuous_at_left_real_increasing:
  2.4483 +  assumes nondecF: "\<And> x y. x \<le> y \<Longrightarrow> f x \<le> ((f y) :: real)"
  2.4484 +  shows "(continuous (at_left (a :: real)) f) = (\<forall>e > 0. \<exists>delta > 0. f a - f (a - delta) < e)"
  2.4485 +  apply (simp add: lessThan_def dist_real_def continuous_within Lim_within_le)
  2.4486 +  apply (intro all_cong ex_cong, safe)
  2.4487 +  apply (erule_tac x="a - d" in allE, simp)
  2.4488 +  apply (simp add: nondecF field_simps)
  2.4489 +  apply (cut_tac x="a - d" and y=x in nondecF, simp_all)
  2.4490 +  done
  2.4491 +
  2.4492 +text\<open>Versions in terms of open balls.\<close>
  2.4493 +
  2.4494 +lemma continuous_within_ball:
  2.4495 +  "continuous (at x within s) f \<longleftrightarrow>
  2.4496 +    (\<forall>e > 0. \<exists>d > 0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e)"
  2.4497 +  (is "?lhs = ?rhs")
  2.4498 +proof
  2.4499 +  assume ?lhs
  2.4500 +  {
  2.4501 +    fix e :: real
  2.4502 +    assume "e > 0"
  2.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"
  2.4504 +      using \<open>?lhs\<close>[unfolded continuous_within Lim_within] by auto
  2.4505 +    {
  2.4506 +      fix y
  2.4507 +      assume "y \<in> f ` (ball x d \<inter> s)"
  2.4508 +      then have "y \<in> ball (f x) e"
  2.4509 +        using d(2)
  2.4510 +        apply (auto simp: dist_commute)
  2.4511 +        apply (erule_tac x=xa in ballE, auto)
  2.4512 +        using \<open>e > 0\<close>
  2.4513 +        apply auto
  2.4514 +        done
  2.4515 +    }
  2.4516 +    then have "\<exists>d>0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e"
  2.4517 +      using \<open>d > 0\<close>
  2.4518 +      unfolding subset_eq ball_def by (auto simp: dist_commute)
  2.4519 +  }
  2.4520 +  then show ?rhs by auto
  2.4521 +next
  2.4522 +  assume ?rhs
  2.4523 +  then show ?lhs
  2.4524 +    unfolding continuous_within Lim_within ball_def subset_eq
  2.4525 +    apply (auto simp: dist_commute)
  2.4526 +    apply (erule_tac x=e in allE, auto)
  2.4527 +    done
  2.4528 +qed
  2.4529 +
  2.4530 +lemma continuous_at_ball:
  2.4531 +  "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f ` (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
  2.4532 +proof
  2.4533 +  assume ?lhs
  2.4534 +  then show ?rhs
  2.4535 +    unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
  2.4536 +    apply auto
  2.4537 +    apply (erule_tac x=e in allE, auto)
  2.4538 +    apply (rule_tac x=d in exI, auto)
  2.4539 +    apply (erule_tac x=xa in allE)
  2.4540 +    apply (auto simp: dist_commute)
  2.4541 +    done
  2.4542 +next
  2.4543 +  assume ?rhs
  2.4544 +  then show ?lhs
  2.4545 +    unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
  2.4546 +    apply auto
  2.4547 +    apply (erule_tac x=e in allE, auto)
  2.4548 +    apply (rule_tac x=d in exI, auto)
  2.4549 +    apply (erule_tac x="f xa" in allE)
  2.4550 +    apply (auto simp: dist_commute)
  2.4551 +    done
  2.4552 +qed
  2.4553 +
  2.4554 +text\<open>Define setwise continuity in terms of limits within the set.\<close>
  2.4555 +
  2.4556 +lemma continuous_on_iff:
  2.4557 +  "continuous_on s f \<longleftrightarrow>
  2.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)"
  2.4559 +  unfolding continuous_on_def Lim_within
  2.4560 +  by (metis dist_pos_lt dist_self)
  2.4561 +
  2.4562 +lemma continuous_within_E:
  2.4563 +  assumes "continuous (at x within s) f" "e>0"
  2.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"
  2.4565 +  using assms apply (simp add: continuous_within_eps_delta)
  2.4566 +  apply (drule spec [of _ e], clarify)
  2.4567 +  apply (rule_tac d="d/2" in that, auto)
  2.4568 +  done
  2.4569 +
  2.4570 +lemma continuous_onI [intro?]:
  2.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"
  2.4572 +  shows "continuous_on s f"
  2.4573 +apply (simp add: continuous_on_iff, clarify)
  2.4574 +apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
  2.4575 +done
  2.4576 +
  2.4577 +text\<open>Some simple consequential lemmas.\<close>
  2.4578 +
  2.4579 +lemma continuous_onE:
  2.4580 +    assumes "continuous_on s f" "x\<in>s" "e>0"
  2.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"
  2.4582 +  using assms
  2.4583 +  apply (simp add: continuous_on_iff)
  2.4584 +  apply (elim ballE allE)
  2.4585 +  apply (auto intro: that [where d="d/2" for d])
  2.4586 +  done
  2.4587 +
  2.4588 +lemma uniformly_continuous_onE:
  2.4589 +  assumes "uniformly_continuous_on s f" "0 < e"
  2.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"
  2.4591 +using assms
  2.4592 +by (auto simp: uniformly_continuous_on_def)
  2.4593 +
  2.4594 +lemma continuous_at_imp_continuous_within:
  2.4595 +  "continuous (at x) f \<Longrightarrow> continuous (at x within s) f"
  2.4596 +  unfolding continuous_within continuous_at using Lim_at_imp_Lim_at_within by auto
  2.4597 +
  2.4598 +lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f \<longlongrightarrow> l) net"
  2.4599 +  by simp
  2.4600 +
  2.4601 +lemmas continuous_on = continuous_on_def \<comment> \<open>legacy theorem name\<close>
  2.4602 +
  2.4603 +lemma continuous_within_subset:
  2.4604 +  "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s \<Longrightarrow> continuous (at x within t) f"
  2.4605 +  unfolding continuous_within by(metis tendsto_within_subset)
  2.4606 +
  2.4607 +lemma continuous_on_interior:
  2.4608 +  "continuous_on s f \<Longrightarrow> x \<in> interior s \<Longrightarrow> continuous (at x) f"
  2.4609 +  by (metis continuous_on_eq_continuous_at continuous_on_subset interiorE)
  2.4610 +
  2.4611 +lemma continuous_on_eq:
  2.4612 +  "\<lbrakk>continuous_on s f; \<And>x. x \<in> s \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> continuous_on s g"
  2.4613 +  unfolding continuous_on_def tendsto_def eventually_at_topological
  2.4614 +  by simp
  2.4615 +
  2.4616 +text \<open>Characterization of various kinds of continuity in terms of sequences.\<close>
  2.4617 +
  2.4618 +lemma continuous_within_sequentiallyI:
  2.4619 +  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
  2.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"
  2.4621 +  shows "continuous (at a within s) f"
  2.4622 +  using assms unfolding continuous_within tendsto_def[where l = "f a"]
  2.4623 +  by (auto intro!: sequentially_imp_eventually_within)
  2.4624 +
  2.4625 +lemma continuous_within_tendsto_compose:
  2.4626 +  fixes f::"'a::t2_space \<Rightarrow> 'b::topological_space"
  2.4627 +  assumes "continuous (at a within s) f"
  2.4628 +          "eventually (\<lambda>n. x n \<in> s) F"
  2.4629 +          "(x \<longlongrightarrow> a) F "
  2.4630 +  shows "((\<lambda>n. f (x n)) \<longlongrightarrow> f a) F"
  2.4631 +proof -
  2.4632 +  have *: "filterlim x (inf (nhds a) (principal s)) F"
  2.4633 +    using assms(2) assms(3) unfolding at_within_def filterlim_inf by (auto simp: filterlim_principal eventually_mono)
  2.4634 +  show ?thesis
  2.4635 +    by (auto simp: assms(1) continuous_within[symmetric] tendsto_at_within_iff_tendsto_nhds[symmetric] intro!: filterlim_compose[OF _ *])
  2.4636 +qed
  2.4637 +
  2.4638 +lemma continuous_within_tendsto_compose':
  2.4639 +  fixes f::"'a::t2_space \<Rightarrow> 'b::topological_space"
  2.4640 +  assumes "continuous (at a within s) f"
  2.4641 +    "\<And>n. x n \<in> s"
  2.4642 +    "(x \<longlongrightarrow> a) F "
  2.4643 +  shows "((\<lambda>n. f (x n)) \<longlongrightarrow> f a) F"
  2.4644 +  by (auto intro!: continuous_within_tendsto_compose[OF assms(1)] simp add: assms)
  2.4645 +
  2.4646 +lemma continuous_within_sequentially:
  2.4647 +  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
  2.4648 +  shows "continuous (at a within s) f \<longleftrightarrow>
  2.4649 +    (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x \<longlongrightarrow> a) sequentially
  2.4650 +         \<longrightarrow> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
  2.4651 +  using continuous_within_tendsto_compose'[of a s f _ sequentially]
  2.4652 +    continuous_within_sequentiallyI[of a s f]
  2.4653 +  by (auto simp: o_def)
  2.4654 +
  2.4655 +lemma continuous_at_sequentiallyI:
  2.4656 +  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
  2.4657 +  assumes "\<And>u. u \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
  2.4658 +  shows "continuous (at a) f"
  2.4659 +  using continuous_within_sequentiallyI[of a UNIV f] assms by auto
  2.4660 +
  2.4661 +lemma continuous_at_sequentially:
  2.4662 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  2.4663 +  shows "continuous (at a) f \<longleftrightarrow>
  2.4664 +    (\<forall>x. (x \<longlongrightarrow> a) sequentially --> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
  2.4665 +  using continuous_within_sequentially[of a UNIV f] by simp
  2.4666 +
  2.4667 +lemma continuous_on_sequentiallyI:
  2.4668 +  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
  2.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"
  2.4670 +  shows "continuous_on s f"
  2.4671 +  using assms unfolding continuous_on_eq_continuous_within
  2.4672 +  using continuous_within_sequentiallyI[of _ s f] by auto
  2.4673 +
  2.4674 +lemma continuous_on_sequentially:
  2.4675 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  2.4676 +  shows "continuous_on s f \<longleftrightarrow>
  2.4677 +    (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x \<longlongrightarrow> a) sequentially
  2.4678 +      --> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
  2.4679 +    (is "?lhs = ?rhs")
  2.4680 +proof
  2.4681 +  assume ?rhs
  2.4682 +  then show ?lhs
  2.4683 +    using continuous_within_sequentially[of _ s f]
  2.4684 +    unfolding continuous_on_eq_continuous_within
  2.4685 +    by auto
  2.4686 +next
  2.4687 +  assume ?lhs
  2.4688 +  then show ?rhs
  2.4689 +    unfolding continuous_on_eq_continuous_within
  2.4690 +    using continuous_within_sequentially[of _ s f]
  2.4691 +    by auto
  2.4692 +qed
  2.4693 +
  2.4694 +lemma uniformly_continuous_on_sequentially:
  2.4695 +  "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
  2.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")
  2.4697 +proof
  2.4698 +  assume ?lhs
  2.4699 +  {
  2.4700 +    fix x y
  2.4701 +    assume x: "\<forall>n. x n \<in> s"
  2.4702 +      and y: "\<forall>n. y n \<in> s"
  2.4703 +      and xy: "((\<lambda>n. dist (x n) (y n)) \<longlongrightarrow> 0) sequentially"
  2.4704 +    {
  2.4705 +      fix e :: real
  2.4706 +      assume "e > 0"
  2.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"
  2.4708 +        using \<open>?lhs\<close>[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto
  2.4709 +      obtain N where N: "\<forall>n\<ge>N. dist (x n) (y n) < d"
  2.4710 +        using xy[unfolded lim_sequentially dist_norm] and \<open>d>0\<close> by auto
  2.4711 +      {
  2.4712 +        fix n
  2.4713 +        assume "n\<ge>N"
  2.4714 +        then have "dist (f (x n)) (f (y n)) < e"
  2.4715 +          using N[THEN spec[where x=n]]
  2.4716 +          using d[THEN bspec[where x="x n"], THEN bspec[where x="y n"]]
  2.4717 +          using x and y
  2.4718 +          by (simp add: dist_commute)
  2.4719 +      }
  2.4720 +      then have "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"
  2.4721 +        by auto
  2.4722 +    }
  2.4723 +    then have "((\<lambda>n. dist (f(x n)) (f(y n))) \<longlongrightarrow> 0) sequentially"
  2.4724 +      unfolding lim_sequentially and dist_real_def by auto
  2.4725 +  }
  2.4726 +  then show ?rhs by auto
  2.4727 +next
  2.4728 +  assume ?rhs
  2.4729 +  {
  2.4730 +    assume "\<not> ?lhs"
  2.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"
  2.4732 +      unfolding uniformly_continuous_on_def by auto
  2.4733 +    then obtain fa where fa:
  2.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"
  2.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"]
  2.4736 +      unfolding Bex_def
  2.4737 +      by (auto simp: dist_commute)
  2.4738 +    define x where "x n = fst (fa (inverse (real n + 1)))" for n
  2.4739 +    define y where "y n = snd (fa (inverse (real n + 1)))" for n
  2.4740 +    have xyn: "\<forall>n. x n \<in> s \<and> y n \<in> s"
  2.4741 +      and xy0: "\<forall>n. dist (x n) (y n) < inverse (real n + 1)"
  2.4742 +      and fxy:"\<forall>n. \<not> dist (f (x n)) (f (y n)) < e"
  2.4743 +      unfolding x_def and y_def using fa
  2.4744 +      by auto
  2.4745 +    {
  2.4746 +      fix e :: real
  2.4747 +      assume "e > 0"
  2.4748 +      then obtain N :: nat where "N \<noteq> 0" and N: "0 < inverse (real N) \<and> inverse (real N) < e"
  2.4749 +        unfolding real_arch_inverse[of e] by auto
  2.4750 +      {
  2.4751 +        fix n :: nat
  2.4752 +        assume "n \<ge> N"
  2.4753 +        then have "inverse (real n + 1) < inverse (real N)"
  2.4754 +          using of_nat_0_le_iff and \<open>N\<noteq>0\<close> by auto
  2.4755 +        also have "\<dots> < e" using N by auto
  2.4756 +        finally have "inverse (real n + 1) < e" by auto
  2.4757 +        then have "dist (x n) (y n) < e"
  2.4758 +          using xy0[THEN spec[where x=n]] by auto
  2.4759 +      }
  2.4760 +      then have "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto
  2.4761 +    }
  2.4762 +    then have "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"
  2.4763 +      using \<open>?rhs\<close>[THEN spec[where x=x], THEN spec[where x=y]] and xyn
  2.4764 +      unfolding lim_sequentially dist_real_def by auto
  2.4765 +    then have False using fxy and \<open>e>0\<close> by auto
  2.4766 +  }
  2.4767 +  then show ?lhs
  2.4768 +    unfolding uniformly_continuous_on_def by blast
  2.4769 +qed
  2.4770 +
  2.4771 +lemma continuous_closed_imp_Cauchy_continuous:
  2.4772 +  fixes S :: "('a::complete_space) set"
  2.4773 +  shows "\<lbrakk>continuous_on S f; closed S; Cauchy \<sigma>; \<And>n. (\<sigma> n) \<in> S\<rbrakk> \<Longrightarrow> Cauchy(f \<circ> \<sigma>)"
  2.4774 +  apply (simp add: complete_eq_closed [symmetric] continuous_on_sequentially)
  2.4775 +  by (meson LIMSEQ_imp_Cauchy complete_def)
  2.4776 +
  2.4777 +text\<open>The usual transformation theorems.\<close>
  2.4778 +
  2.4779 +lemma continuous_transform_within:
  2.4780 +  fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  2.4781 +  assumes "continuous (at x within s) f"
  2.4782 +    and "0 < d"
  2.4783 +    and "x \<in> s"
  2.4784 +    and "\<And>x'. \<lbrakk>x' \<in> s; dist x' x < d\<rbrakk> \<Longrightarrow> f x' = g x'"
  2.4785 +  shows "continuous (at x within s) g"
  2.4786 +  using assms
  2.4787 +  unfolding continuous_within
  2.4788 +  by (force intro: Lim_transform_within)
  2.4789 +
  2.4790 +
  2.4791 +subsubsection%unimportant \<open>Structural rules for uniform continuity\<close>
  2.4792 +
  2.4793 +lemma uniformly_continuous_on_dist[continuous_intros]:
  2.4794 +  fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  2.4795 +  assumes "uniformly_continuous_on s f"
  2.4796 +    and "uniformly_continuous_on s g"
  2.4797 +  shows "uniformly_continuous_on s (\<lambda>x. dist (f x) (g x))"
  2.4798 +proof -
  2.4799 +  {
  2.4800 +    fix a b c d :: 'b
  2.4801 +    have "\<bar>dist a b - dist c d\<bar> \<le> dist a c + dist b d"
  2.4802 +      using dist_triangle2 [of a b c] dist_triangle2 [of b c d]
  2.4803 +      using dist_triangle3 [of c d a] dist_triangle [of a d b]
  2.4804 +      by arith
  2.4805 +  } note le = this
  2.4806 +  {
  2.4807 +    fix x y
  2.4808 +    assume f: "(\<lambda>n. dist (f (x n)) (f (y n))) \<longlonglongrightarrow> 0"
  2.4809 +    assume g: "(\<lambda>n. dist (g (x n)) (g (y n))) \<longlonglongrightarrow> 0"
  2.4810 +    have "(\<lambda>n. \<bar>dist (f (x n)) (g (x n)) - dist (f (y n)) (g (y n))\<bar>) \<longlonglongrightarrow> 0"
  2.4811 +      by (rule Lim_transform_bound [OF _ tendsto_add_zero [OF f g]],
  2.4812 +        simp add: le)
  2.4813 +  }
  2.4814 +  then show ?thesis
  2.4815 +    using assms unfolding uniformly_continuous_on_sequentially
  2.4816 +    unfolding dist_real_def by simp
  2.4817 +qed
  2.4818 +
  2.4819 +lemma uniformly_continuous_on_norm[continuous_intros]:
  2.4820 +  fixes f :: "'a :: metric_space \<Rightarrow> 'b :: real_normed_vector"
  2.4821 +  assumes "uniformly_continuous_on s f"
  2.4822 +  shows "uniformly_continuous_on s (\<lambda>x. norm (f x))"
  2.4823 +  unfolding norm_conv_dist using assms
  2.4824 +  by (intro uniformly_continuous_on_dist uniformly_continuous_on_const)
  2.4825 +
  2.4826 +lemma (in bounded_linear) uniformly_continuous_on[continuous_intros]:
  2.4827 +  fixes g :: "_::metric_space \<Rightarrow> _"
  2.4828 +  assumes "uniformly_continuous_on s g"
  2.4829 +  shows "uniformly_continuous_on s (\<lambda>x. f (g x))"
  2.4830 +  using assms unfolding uniformly_continuous_on_sequentially
  2.4831 +  unfolding dist_norm tendsto_norm_zero_iff diff[symmetric]
  2.4832 +  by (auto intro: tendsto_zero)
  2.4833 +
  2.4834 +lemma uniformly_continuous_on_cmul[continuous_intros]:
  2.4835 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  2.4836 +  assumes "uniformly_continuous_on s f"
  2.4837 +  shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"
  2.4838 +  using bounded_linear_scaleR_right assms
  2.4839 +  by (rule bounded_linear.uniformly_continuous_on)
  2.4840 +
  2.4841 +lemma dist_minus:
  2.4842 +  fixes x y :: "'a::real_normed_vector"
  2.4843 +  shows "dist (- x) (- y) = dist x y"
  2.4844 +  unfolding dist_norm minus_diff_minus norm_minus_cancel ..
  2.4845 +
  2.4846 +lemma uniformly_continuous_on_minus[continuous_intros]:
  2.4847 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  2.4848 +  shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. - f x)"
  2.4849 +  unfolding uniformly_continuous_on_def dist_minus .
  2.4850 +
  2.4851 +lemma uniformly_continuous_on_add[continuous_intros]:
  2.4852 +  fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  2.4853 +  assumes "uniformly_continuous_on s f"
  2.4854 +    and "uniformly_continuous_on s g"
  2.4855 +  shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"
  2.4856 +  using assms
  2.4857 +  unfolding uniformly_continuous_on_sequentially
  2.4858 +  unfolding dist_norm tendsto_norm_zero_iff add_diff_add
  2.4859 +  by (auto intro: tendsto_add_zero)
  2.4860 +
  2.4861 +lemma uniformly_continuous_on_diff[continuous_intros]:
  2.4862 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  2.4863 +  assumes "uniformly_continuous_on s f"
  2.4864 +    and "uniformly_continuous_on s g"
  2.4865 +  shows "uniformly_continuous_on s (\<lambda>x. f x - g x)"
  2.4866 +  using assms uniformly_continuous_on_add [of s f "- g"]
  2.4867 +    by (simp add: fun_Compl_def uniformly_continuous_on_minus)
  2.4868 +
  2.4869 +text \<open>Continuity in terms of open preimages.\<close>
  2.4870 +
  2.4871 +lemma continuous_at_open:
  2.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)))"
  2.4873 +  unfolding continuous_within_topological [of x UNIV f]
  2.4874 +  unfolding imp_conjL
  2.4875 +  by (intro all_cong imp_cong ex_cong conj_cong refl) auto
  2.4876 +
  2.4877 +lemma continuous_imp_tendsto:
  2.4878 +  assumes "continuous (at x0) f"
  2.4879 +    and "x \<longlonglongrightarrow> x0"
  2.4880 +  shows "(f \<circ> x) \<longlonglongrightarrow> (f x0)"
  2.4881 +proof (rule topological_tendstoI)
  2.4882 +  fix S
  2.4883 +  assume "open S" "f x0 \<in> S"
  2.4884 +  then obtain T where T_def: "open T" "x0 \<in> T" "\<forall>x\<in>T. f x \<in> S"
  2.4885 +     using assms continuous_at_open by metis
  2.4886 +  then have "eventually (\<lambda>n. x n \<in> T) sequentially"
  2.4887 +    using assms T_def by (auto simp: tendsto_def)
  2.4888 +  then show "eventually (\<lambda>n. (f \<circ> x) n \<in> S) sequentially"
  2.4889 +    using T_def by (auto elim!: eventually_mono)
  2.4890 +qed
  2.4891 +
  2.4892 +lemma continuous_on_open:
  2.4893 +  "continuous_on S f \<longleftrightarrow>
  2.4894 +    (\<forall>T. openin (subtopology euclidean (f ` S)) T \<longrightarrow>
  2.4895 +      openin (subtopology euclidean S) (S \<inter> f -` T))"
  2.4896 +  unfolding continuous_on_open_invariant openin_open Int_def vimage_def Int_commute
  2.4897 +  by (simp add: imp_ex imageI conj_commute eq_commute cong: conj_cong)
  2.4898 +
  2.4899 +lemma continuous_on_open_gen:
  2.4900 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  2.4901 +  assumes "f ` S \<subseteq> T"
  2.4902 +    shows "continuous_on S f \<longleftrightarrow>
  2.4903 +             (\<forall>U. openin (subtopology euclidean T) U
  2.4904 +                  \<longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` U))"
  2.4905 +     (is "?lhs = ?rhs")
  2.4906 +proof
  2.4907 +  assume ?lhs
  2.4908 +  then show ?rhs
  2.4909 +    apply (clarsimp simp: openin_euclidean_subtopology_iff continuous_on_iff)
  2.4910 +    by (metis assms image_subset_iff)
  2.4911 +next
  2.4912 +  have ope: "openin (subtopology euclidean T) (ball y e \<inter> T)" for y e
  2.4913 +    by (simp add: Int_commute openin_open_Int)
  2.4914 +  assume R [rule_format]: ?rhs
  2.4915 +  show ?lhs
  2.4916 +  proof (clarsimp simp add: continuous_on_iff)
  2.4917 +    fix x and e::real
  2.4918 +    assume "x \<in> S" and "0 < e"
  2.4919 +    then have x: "x \<in> S \<inter> (f -` ball (f x) e \<inter> f -` T)"
  2.4920 +      using assms by auto
  2.4921 +    show "\<exists>d>0. \<forall>x'\<in>S. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
  2.4922 +      using R [of "ball (f x) e \<inter> T"] x
  2.4923 +      by (fastforce simp add: ope openin_euclidean_subtopology_iff [of S] dist_commute)
  2.4924 +  qed
  2.4925 +qed
  2.4926 +
  2.4927 +lemma continuous_openin_preimage:
  2.4928 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  2.4929 +  shows
  2.4930 +   "\<lbrakk>continuous_on S f; f ` S \<subseteq> T; openin (subtopology euclidean T) U\<rbrakk>
  2.4931 +        \<Longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` U)"
  2.4932 +by (simp add: continuous_on_open_gen)
  2.4933 +
  2.4934 +text \<open>Similarly in terms of closed sets.\<close>
  2.4935 +
  2.4936 +lemma continuous_on_closed:
  2.4937 +  "continuous_on S f \<longleftrightarrow>
  2.4938 +    (\<forall>T. closedin (subtopology euclidean (f ` S)) T \<longrightarrow>
  2.4939 +      closedin (subtopology euclidean S) (S \<inter> f -` T))"
  2.4940 +  unfolding continuous_on_closed_invariant closedin_closed Int_def vimage_def Int_commute
  2.4941 +  by (simp add: imp_ex imageI conj_commute eq_commute cong: conj_cong)
  2.4942 +
  2.4943 +lemma continuous_on_closed_gen:
  2.4944 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  2.4945 +  assumes "f ` S \<subseteq> T"
  2.4946 +    shows "continuous_on S f \<longleftrightarrow>
  2.4947 +             (\<forall>U. closedin (subtopology euclidean T) U
  2.4948 +                  \<longrightarrow> closedin (subtopology euclidean S) (S \<inter> f -` U))"
  2.4949 +     (is "?lhs = ?rhs")
  2.4950 +proof -
  2.4951 +  have *: "U \<subseteq> T \<Longrightarrow> S \<inter> f -` (T - U) = S - (S \<inter> f -` U)" for U
  2.4952 +    using assms by blast
  2.4953 +  show ?thesis
  2.4954 +  proof
  2.4955 +    assume L: ?lhs
  2.4956 +    show ?rhs
  2.4957 +    proof clarify
  2.4958 +      fix U
  2.4959 +      assume "closedin (subtopology euclidean T) U"
  2.4960 +      then show "closedin (subtopology euclidean S) (S \<inter> f -` U)"
  2.4961 +        using L unfolding continuous_on_open_gen [OF assms]
  2.4962 +        by (metis * closedin_def inf_le1 topspace_euclidean_subtopology)
  2.4963 +    qed
  2.4964 +  next
  2.4965 +    assume R [rule_format]: ?rhs
  2.4966 +    show ?lhs
  2.4967 +      unfolding continuous_on_open_gen [OF assms]
  2.4968 +      by (metis * R inf_le1 openin_closedin_eq topspace_euclidean_subtopology)
  2.4969 +  qed
  2.4970 +qed
  2.4971 +
  2.4972 +lemma continuous_closedin_preimage_gen:
  2.4973 +  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  2.4974 +  assumes "continuous_on S f" "f ` S \<subseteq> T" "closedin (subtopology euclidean T) U"
  2.4975 +    shows "closedin (subtopology euclidean S) (S \<inter> f -` U)"
  2.4976 +using assms continuous_on_closed_gen by blast
  2.4977 +
  2.4978 +lemma continuous_on_imp_closedin:
  2.4979 +  assumes "continuous_on S f" "closedin (subtopology euclidean (f ` S)) T"
  2.4980 +    shows "closedin (subtopology euclidean S) (S \<inter> f -` T)"
  2.4981 +using assms continuous_on_closed by blast
  2.4982 +
  2.4983 +subsection%unimportant \<open>Half-global and completely global cases\<close>
  2.4984 +
  2.4985 +lemma continuous_openin_preimage_gen:
  2.4986 +  assumes "continuous_on S f"  "open T"
  2.4987 +  shows "openin (subtopology euclidean S) (S \<inter> f -` T)"
  2.4988 +proof -
  2.4989 +  have *: "(S \<inter> f -` T) = (S \<inter> f -` (T \<inter> f ` S))"
  2.4990 +    by auto
  2.4991 +  have "openin (subtopology euclidean (f ` S)) (T \<inter> f ` S)"
  2.4992 +    using openin_open_Int[of T "f ` S", OF assms(2)] unfolding openin_open by auto
  2.4993 +  then show ?thesis
  2.4994 +    using assms(1)[unfolded continuous_on_open, THEN spec[where x="T \<inter> f ` S"]]
  2.4995 +    using * by auto
  2.4996 +qed
  2.4997 +
  2.4998 +lemma continuous_closedin_preimage:
  2.4999 +  assumes "continuous_on S f" and "closed T"
  2.5000 +  shows "closedin (subtopology euclidean S) (S \<inter> f -` T)"
  2.5001 +proof -
  2.5002 +  have *: "(S \<inter> f -` T) = (S \<inter> f -` (T \<inter> f ` S))"
  2.5003 +    by auto
  2.5004 +  have "closedin (subtopology euclidean (f ` S)) (T \<inter> f ` S)"
  2.5005 +    using closedin_closed_Int[of T "f ` S", OF assms(2)]
  2.5006 +    by (simp add: Int_commute)
  2.5007 +  then show ?thesis
  2.5008 +    using assms(1)[unfolded continuous_on_closed, THEN spec[where x="T \<inter> f ` S"]]
  2.5009 +    using * by auto
  2.5010 +qed
  2.5011 +
  2.5012 +lemma continuous_openin_preimage_eq:
  2.5013 +   "continuous_on S f \<longleftrightarrow>
  2.5014 +    (\<forall>T. open T \<longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` T))"
  2.5015 +apply safe
  2.5016 +apply (simp add: continuous_openin_preimage_gen)
  2.5017 +apply (fastforce simp add: continuous_on_open openin_open)
  2.5018 +done
  2.5019 +
  2.5020 +lemma continuous_closedin_preimage_eq:
  2.5021 +   "continuous_on S f \<longleftrightarrow>
  2.5022 +    (\<forall>T. closed T \<longrightarrow> closedin (subtopology euclidean S) (S \<inter> f -` T))"
  2.5023 +apply safe
  2.5024 +apply (simp add: continuous_closedin_preimage)
  2.5025 +apply (fastforce simp add: continuous_on_closed closedin_closed)
  2.5026 +done
  2.5027 +
  2.5028 +lemma continuous_open_preimage:
  2.5029 +  assumes contf: "continuous_on S f" and "open S" "open T"
  2.5030 +  shows "open (S \<inter> f -` T)"
  2.5031 +proof-
  2.5032 +  obtain U where "open U" "(S \<inter> f -` T) = S \<inter> U"
  2.5033 +    using continuous_openin_preimage_gen[OF contf \<open>open T\<close>]
  2.5034 +    unfolding openin_open by auto
  2.5035 +  then show ?thesis
  2.5036 +    using open_Int[of S U, OF \<open>open S\<close>] by auto
  2.5037 +qed
  2.5038 +
  2.5039 +lemma continuous_closed_preimage:
  2.5040 +  assumes contf: "continuous_on S f" and "closed S" "closed T"
  2.5041 +  shows "closed (S \<inter> f -` T)"
  2.5042 +proof-
  2.5043 +  obtain U where "closed U" "(S \<inter> f -` T) = S \<inter> U"
  2.5044 +    using continuous_closedin_preimage[OF contf \<open>closed T\<close>]
  2.5045 +    unfolding closedin_closed by auto
  2.5046 +  then show ?thesis using closed_Int[of S U, OF \<open>closed S\<close>] by auto
  2.5047 +qed
  2.5048 +
  2.5049 +lemma continuous_open_vimage: "open S \<Longrightarrow> (\<And>x. continuous (at x) f) \<Longrightarrow> open (f -` S)"
  2.5050 +  by (metis continuous_on_eq_continuous_within open_vimage) 
  2.5051 + 
  2.5052 +lemma continuous_closed_vimage: "closed S \<Longrightarrow> (\<And>x. continuous (at x) f) \<Longrightarrow> closed (f -` S)"
  2.5053 +  by (simp add: closed_vimage continuous_on_eq_continuous_within)
  2.5054 +
  2.5055 +lemma interior_image_subset:
  2.5056 +  assumes "inj f" "\<And>x. continuous (at x) f"
  2.5057 +  shows "interior (f ` S) \<subseteq> f ` (interior S)"
  2.5058 +proof
  2.5059 +  fix x assume "x \<in> interior (f ` S)"
  2.5060 +  then obtain T where as: "open T" "x \<in> T" "T \<subseteq> f ` S" ..
  2.5061 +  then have "x \<in> f ` S" by auto
  2.5062 +  then obtain y where y: "y \<in> S" "x = f y" by auto
  2.5063 +  have "open (f -` T)"
  2.5064 +    using assms \<open>open T\<close> by (simp add: continuous_at_imp_continuous_on open_vimage)
  2.5065 +  moreover have "y \<in> vimage f T"
  2.5066 +    using \<open>x = f y\<close> \<open>x \<in> T\<close> by simp
  2.5067 +  moreover have "vimage f T \<subseteq> S"
  2.5068 +    using \<open>T \<subseteq> image f S\<close> \<open>inj f\<close> unfolding inj_on_def subset_eq by auto
  2.5069 +  ultimately have "y \<in> interior S" ..
  2.5070 +  with \<open>x = f y\<close> show "x \<in> f ` interior S" ..
  2.5071 +qed
  2.5072 +
  2.5073 +subsection%unimportant \<open>Topological properties of linear functions\<close>
  2.5074 +
  2.5075 +lemma linear_lim_0:
  2.5076 +  assumes "bounded_linear f"
  2.5077 +  shows "(f \<longlongrightarrow> 0) (at (0))"
  2.5078 +proof -
  2.5079 +  interpret f: bounded_linear f by fact
  2.5080 +  have "(f \<longlongrightarrow> f 0) (at 0)"
  2.5081 +    using tendsto_ident_at by (rule f.tendsto)
  2.5082 +  then show ?thesis unfolding f.zero .
  2.5083 +qed
  2.5084 +
  2.5085 +lemma linear_continuous_at:
  2.5086 +  assumes "bounded_linear f"
  2.5087 +  shows "continuous (at a) f"
  2.5088 +  unfolding continuous_at using assms
  2.5089 +  apply (rule bounded_linear.tendsto)
  2.5090 +  apply (rule tendsto_ident_at)
  2.5091 +  done
  2.5092 +
  2.5093 +lemma linear_continuous_within:
  2.5094 +  "bounded_linear f \<Longrightarrow> continuous (at x within s) f"
  2.5095 +  using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto
  2.5096 +
  2.5097 +lemma linear_continuous_on:
  2.5098 +  "bounded_linear f \<Longrightarrow> continuous_on s f"
  2.5099 +  using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto
  2.5100 +
  2.5101 +end
  2.5102 \ No newline at end of file
     3.1 --- a/src/HOL/Analysis/Euclidean_Space.thy	Thu Dec 27 22:54:29 2018 +0100
     3.2 +++ b/src/HOL/Analysis/Euclidean_Space.thy	Thu Dec 27 23:38:55 2018 +0100
     3.3 @@ -12,6 +12,16 @@
     3.4    Product_Vector
     3.5  begin
     3.6  
     3.7 +
     3.8 +subsection%unimportant \<open>Interlude: Some properties of real sets\<close>
     3.9 +
    3.10 +lemma seq_mono_lemma:
    3.11 +  assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n"
    3.12 +    and "\<forall>n \<ge> m. e n \<le> e m"
    3.13 +  shows "\<forall>n \<ge> m. d n < e m"
    3.14 +  using assms by force
    3.15 +
    3.16 +
    3.17  subsection \<open>Type class of Euclidean spaces\<close>
    3.18  
    3.19  class euclidean_space = real_inner +
     4.1 --- a/src/HOL/Analysis/Linear_Algebra.thy	Thu Dec 27 22:54:29 2018 +0100
     4.2 +++ b/src/HOL/Analysis/Linear_Algebra.thy	Thu Dec 27 23:38:55 2018 +0100
     4.3 @@ -310,51 +310,6 @@
     4.4    by (rule adjoint_unique, simp add: adjoint_clauses [OF lf])
     4.5  
     4.6  
     4.7 -subsection%unimportant \<open>Interlude: Some properties of real sets\<close>
     4.8 -
     4.9 -lemma seq_mono_lemma:
    4.10 -  assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n"
    4.11 -    and "\<forall>n \<ge> m. e n \<le> e m"
    4.12 -  shows "\<forall>n \<ge> m. d n < e m"
    4.13 -  using assms by force
    4.14 -
    4.15 -lemma infinite_enumerate:
    4.16 -  assumes fS: "infinite S"
    4.17 -  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (\<forall>n. r n \<in> S)"
    4.18 -  unfolding strict_mono_def
    4.19 -  using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
    4.20 -
    4.21 -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)"
    4.22 -  apply auto
    4.23 -  apply (rule_tac x="d/2" in exI)
    4.24 -  apply auto
    4.25 -  done
    4.26 -
    4.27 -lemma approachable_lt_le2:  \<comment> \<open>like the above, but pushes aside an extra formula\<close>
    4.28 -    "(\<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)"
    4.29 -  apply auto
    4.30 -  apply (rule_tac x="d/2" in exI, auto)
    4.31 -  done
    4.32 -
    4.33 -lemma triangle_lemma:
    4.34 -  fixes x y z :: real
    4.35 -  assumes x: "0 \<le> x"
    4.36 -    and y: "0 \<le> y"
    4.37 -    and z: "0 \<le> z"
    4.38 -    and xy: "x\<^sup>2 \<le> y\<^sup>2 + z\<^sup>2"
    4.39 -  shows "x \<le> y + z"
    4.40 -proof -
    4.41 -  have "y\<^sup>2 + z\<^sup>2 \<le> y\<^sup>2 + 2 * y * z + z\<^sup>2"
    4.42 -    using z y by simp
    4.43 -  with xy have th: "x\<^sup>2 \<le> (y + z)\<^sup>2"
    4.44 -    by (simp add: power2_eq_square field_simps)
    4.45 -  from y z have yz: "y + z \<ge> 0"
    4.46 -    by arith
    4.47 -  from power2_le_imp_le[OF th yz] show ?thesis .
    4.48 -qed
    4.49 -
    4.50 -
    4.51 -
    4.52  subsection \<open>Archimedean properties and useful consequences\<close>
    4.53  
    4.54  text\<open>Bernoulli's inequality\<close>
     5.1 --- a/src/HOL/Analysis/Path_Connected.thy	Thu Dec 27 22:54:29 2018 +0100
     5.2 +++ b/src/HOL/Analysis/Path_Connected.thy	Thu Dec 27 23:38:55 2018 +0100
     5.3 @@ -5,7 +5,7 @@
     5.4  section \<open>Continuous paths and path-connected sets\<close>
     5.5  
     5.6  theory Path_Connected
     5.7 -imports Continuous_Extension Continuum_Not_Denumerable
     5.8 +  imports Continuous_Extension Continuum_Not_Denumerable
     5.9  begin
    5.10  
    5.11  subsection \<open>Paths and Arcs\<close>
    5.12 @@ -7421,7 +7421,7 @@
    5.13          show "x \<in> ball x r \<inter> affine hull S"
    5.14            using \<open>x \<in> S\<close> \<open>r > 0\<close> by (simp add: hull_inc)
    5.15          have "openin (subtopology euclidean (affine hull S)) (ball x r \<inter> affine hull S)"
    5.16 -          by (simp add: inf.commute openin_Int_open)
    5.17 +          by (subst inf.commute) (simp add: openin_Int_open)
    5.18          then show "openin (subtopology euclidean S) (ball x r \<inter> affine hull S)"
    5.19            by (rule openin_subset_trans [OF _ subS Ssub])
    5.20        qed (use * path_component_trans in \<open>auto simp: path_connected_component path_component_of_subset [OF ST]\<close>)
     6.1 --- a/src/HOL/Analysis/Starlike.thy	Thu Dec 27 22:54:29 2018 +0100
     6.2 +++ b/src/HOL/Analysis/Starlike.thy	Thu Dec 27 23:38:55 2018 +0100
     6.3 @@ -4921,6 +4921,11 @@
     6.4  
     6.5  subsection%unimportant\<open>Basic lemmas about hyperplanes and halfspaces\<close>
     6.6  
     6.7 +lemma halfspace_Int_eq:
     6.8 +     "{x. a \<bullet> x \<le> b} \<inter> {x. b \<le> a \<bullet> x} = {x. a \<bullet> x = b}"
     6.9 +     "{x. b \<le> a \<bullet> x} \<inter> {x. a \<bullet> x \<le> b} = {x. a \<bullet> x = b}"
    6.10 +  by auto
    6.11 +
    6.12  lemma hyperplane_eq_Ex:
    6.13    assumes "a \<noteq> 0" obtains x where "a \<bullet> x = b"
    6.14    by (rule_tac x = "(b / (a \<bullet> a)) *\<^sub>R a" in that) (simp add: assms)
     7.1 --- a/src/HOL/Analysis/Topology_Euclidean_Space.thy	Thu Dec 27 22:54:29 2018 +0100
     7.2 +++ b/src/HOL/Analysis/Topology_Euclidean_Space.thy	Thu Dec 27 23:38:55 2018 +0100
     7.3 @@ -4,1402 +4,15 @@
     7.4      Author:     Brian Huffman, Portland State University
     7.5  *)
     7.6  
     7.7 -section \<open>Elementary topology in Euclidean space\<close>
     7.8 +section \<open>Elementary Topology in Euclidean Space\<close>
     7.9  
    7.10  theory Topology_Euclidean_Space
    7.11 -imports
    7.12 -  "HOL-Library.Indicator_Function"
    7.13 -  "HOL-Library.Countable_Set"
    7.14 -  "HOL-Library.FuncSet"
    7.15 -  "HOL-Library.Set_Idioms"
    7.16 -  Linear_Algebra
    7.17 -  Norm_Arith
    7.18 -begin
    7.19 -
    7.20 -(* FIXME: move elsewhere *)
    7.21 -
    7.22 -lemma halfspace_Int_eq:
    7.23 -     "{x. a \<bullet> x \<le> b} \<inter> {x. b \<le> a \<bullet> x} = {x. a \<bullet> x = b}"
    7.24 -     "{x. b \<le> a \<bullet> x} \<inter> {x. a \<bullet> x \<le> b} = {x. a \<bullet> x = b}"
    7.25 -  by auto
    7.26 -
    7.27 -definition (in monoid_add) support_on :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'b set"
    7.28 -  where "support_on s f = {x\<in>s. f x \<noteq> 0}"
    7.29 -
    7.30 -lemma in_support_on: "x \<in> support_on s f \<longleftrightarrow> x \<in> s \<and> f x \<noteq> 0"
    7.31 -  by (simp add: support_on_def)
    7.32 -
    7.33 -lemma support_on_simps[simp]:
    7.34 -  "support_on {} f = {}"
    7.35 -  "support_on (insert x s) f =
    7.36 -    (if f x = 0 then support_on s f else insert x (support_on s f))"
    7.37 -  "support_on (s \<union> t) f = support_on s f \<union> support_on t f"
    7.38 -  "support_on (s \<inter> t) f = support_on s f \<inter> support_on t f"
    7.39 -  "support_on (s - t) f = support_on s f - support_on t f"
    7.40 -  "support_on (f ` s) g = f ` (support_on s (g \<circ> f))"
    7.41 -  unfolding support_on_def by auto
    7.42 -
    7.43 -lemma support_on_cong:
    7.44 -  "(\<And>x. x \<in> s \<Longrightarrow> f x = 0 \<longleftrightarrow> g x = 0) \<Longrightarrow> support_on s f = support_on s g"
    7.45 -  by (auto simp: support_on_def)
    7.46 -
    7.47 -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}"
    7.48 -  by (auto simp: support_on_def)
    7.49 -
    7.50 -lemma support_on_if_subset: "support_on A (\<lambda>x. if P x then a else 0) \<subseteq> {x \<in> A. P x}"
    7.51 -  by (auto simp: support_on_def)
    7.52 -
    7.53 -lemma finite_support[intro]: "finite S \<Longrightarrow> finite (support_on S f)"
    7.54 -  unfolding support_on_def by auto
    7.55 -
    7.56 -(* TODO: is supp_sum really needed? TODO: Generalize to Finite_Set.fold *)
    7.57 -definition (in comm_monoid_add) supp_sum :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
    7.58 -  where "supp_sum f S = (\<Sum>x\<in>support_on S f. f x)"
    7.59 -
    7.60 -lemma supp_sum_empty[simp]: "supp_sum f {} = 0"
    7.61 -  unfolding supp_sum_def by auto
    7.62 -
    7.63 -lemma supp_sum_insert[simp]:
    7.64 -  "finite (support_on S f) \<Longrightarrow>
    7.65 -    supp_sum f (insert x S) = (if x \<in> S then supp_sum f S else f x + supp_sum f S)"
    7.66 -  by (simp add: supp_sum_def in_support_on insert_absorb)
    7.67 -
    7.68 -lemma supp_sum_divide_distrib: "supp_sum f A / (r::'a::field) = supp_sum (\<lambda>n. f n / r) A"
    7.69 -  by (cases "r = 0")
    7.70 -     (auto simp: supp_sum_def sum_divide_distrib intro!: sum.cong support_on_cong)
    7.71 -
    7.72 -(*END OF SUPPORT, ETC.*)
    7.73 -
    7.74 -lemma image_affinity_interval:
    7.75 -  fixes c :: "'a::ordered_real_vector"
    7.76 -  shows "((\<lambda>x. m *\<^sub>R x + c) ` {a..b}) = 
    7.77 -           (if {a..b}={} then {}
    7.78 -            else if 0 \<le> m then {m *\<^sub>R a + c .. m  *\<^sub>R b + c}
    7.79 -            else {m *\<^sub>R b + c .. m *\<^sub>R a + c})"
    7.80 -         (is "?lhs = ?rhs")
    7.81 -proof (cases "m=0")
    7.82 -  case True
    7.83 -  then show ?thesis
    7.84 -    by force
    7.85 -next
    7.86 -  case False
    7.87 -  show ?thesis
    7.88 -  proof
    7.89 -    show "?lhs \<subseteq> ?rhs"
    7.90 -      by (auto simp: scaleR_left_mono scaleR_left_mono_neg)
    7.91 -    show "?rhs \<subseteq> ?lhs"
    7.92 -    proof (clarsimp, intro conjI impI subsetI)
    7.93 -      show "\<lbrakk>0 \<le> m; a \<le> b; x \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}\<rbrakk>
    7.94 -            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
    7.95 -        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
    7.96 -        using False apply (auto simp: le_diff_eq pos_le_divideRI)
    7.97 -        using diff_le_eq pos_le_divideR_eq by force
    7.98 -      show "\<lbrakk>\<not> 0 \<le> m; a \<le> b;  x \<in> {m *\<^sub>R b + c..m *\<^sub>R a + c}\<rbrakk>
    7.99 -            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
   7.100 -        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
   7.101 -        apply (auto simp: diff_le_eq neg_le_divideR_eq)
   7.102 -        using diff_eq_diff_less_eq linordered_field_class.sign_simps(11) minus_diff_eq not_less scaleR_le_cancel_left_neg by fastforce
   7.103 -    qed
   7.104 -  qed
   7.105 -qed
   7.106 -
   7.107 -lemma countable_PiE:
   7.108 -  "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (Pi\<^sub>E I F)"
   7.109 -  by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)
   7.110 -
   7.111 -lemma open_sums:
   7.112 -  fixes T :: "('b::real_normed_vector) set"
   7.113 -  assumes "open S \<or> open T"
   7.114 -  shows "open (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
   7.115 -  using assms
   7.116 -proof
   7.117 -  assume S: "open S"
   7.118 -  show ?thesis
   7.119 -  proof (clarsimp simp: open_dist)
   7.120 -    fix x y
   7.121 -    assume "x \<in> S" "y \<in> T"
   7.122 -    with S obtain e where "e > 0" and e: "\<And>x'. dist x' x < e \<Longrightarrow> x' \<in> S"
   7.123 -      by (auto simp: open_dist)
   7.124 -    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
   7.125 -      by (metis \<open>y \<in> T\<close> diff_add_cancel dist_add_cancel2)
   7.126 -    then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
   7.127 -      using \<open>0 < e\<close> \<open>x \<in> S\<close> by blast
   7.128 -  qed
   7.129 -next
   7.130 -  assume T: "open T"
   7.131 -  show ?thesis
   7.132 -  proof (clarsimp simp: open_dist)
   7.133 -    fix x y
   7.134 -    assume "x \<in> S" "y \<in> T"
   7.135 -    with T obtain e where "e > 0" and e: "\<And>x'. dist x' y < e \<Longrightarrow> x' \<in> T"
   7.136 -      by (auto simp: open_dist)
   7.137 -    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
   7.138 -      by (metis \<open>x \<in> S\<close> add_diff_cancel_left' add_diff_eq diff_diff_add dist_norm)
   7.139 -    then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
   7.140 -      using \<open>0 < e\<close> \<open>y \<in> T\<close> by blast
   7.141 -  qed
   7.142 -qed
   7.143 -
   7.144 -
   7.145 -subsection \<open>Topological Basis\<close>
   7.146 -
   7.147 -context topological_space
   7.148 -begin
   7.149 -
   7.150 -definition%important "topological_basis B \<longleftrightarrow>
   7.151 -  (\<forall>b\<in>B. open b) \<and> (\<forall>x. open x \<longrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
   7.152 -
   7.153 -lemma topological_basis:
   7.154 -  "topological_basis B \<longleftrightarrow> (\<forall>x. open x \<longleftrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
   7.155 -  unfolding topological_basis_def
   7.156 -  apply safe
   7.157 -     apply fastforce
   7.158 -    apply fastforce
   7.159 -   apply (erule_tac x=x in allE, simp)
   7.160 -   apply (rule_tac x="{x}" in exI, auto)
   7.161 -  done
   7.162 -
   7.163 -lemma topological_basis_iff:
   7.164 -  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
   7.165 -  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'))"
   7.166 -    (is "_ \<longleftrightarrow> ?rhs")
   7.167 -proof safe
   7.168 -  fix O' and x::'a
   7.169 -  assume H: "topological_basis B" "open O'" "x \<in> O'"
   7.170 -  then have "(\<exists>B'\<subseteq>B. \<Union>B' = O')" by (simp add: topological_basis_def)
   7.171 -  then obtain B' where "B' \<subseteq> B" "O' = \<Union>B'" by auto
   7.172 -  then show "\<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'" using H by auto
   7.173 -next
   7.174 -  assume H: ?rhs
   7.175 -  show "topological_basis B"
   7.176 -    using assms unfolding topological_basis_def
   7.177 -  proof safe
   7.178 -    fix O' :: "'a set"
   7.179 -    assume "open O'"
   7.180 -    with H obtain f where "\<forall>x\<in>O'. f x \<in> B \<and> x \<in> f x \<and> f x \<subseteq> O'"
   7.181 -      by (force intro: bchoice simp: Bex_def)
   7.182 -    then show "\<exists>B'\<subseteq>B. \<Union>B' = O'"
   7.183 -      by (auto intro: exI[where x="{f x |x. x \<in> O'}"])
   7.184 -  qed
   7.185 -qed
   7.186 -
   7.187 -lemma topological_basisI:
   7.188 -  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
   7.189 -    and "\<And>O' x. open O' \<Longrightarrow> x \<in> O' \<Longrightarrow> \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'"
   7.190 -  shows "topological_basis B"
   7.191 -  using assms by (subst topological_basis_iff) auto
   7.192 -
   7.193 -lemma topological_basisE:
   7.194 -  fixes O'
   7.195 -  assumes "topological_basis B"
   7.196 -    and "open O'"
   7.197 -    and "x \<in> O'"
   7.198 -  obtains B' where "B' \<in> B" "x \<in> B'" "B' \<subseteq> O'"
   7.199 -proof atomize_elim
   7.200 -  from assms have "\<And>B'. B'\<in>B \<Longrightarrow> open B'"
   7.201 -    by (simp add: topological_basis_def)
   7.202 -  with topological_basis_iff assms
   7.203 -  show  "\<exists>B'. B' \<in> B \<and> x \<in> B' \<and> B' \<subseteq> O'"
   7.204 -    using assms by (simp add: Bex_def)
   7.205 -qed
   7.206 -
   7.207 -lemma topological_basis_open:
   7.208 -  assumes "topological_basis B"
   7.209 -    and "X \<in> B"
   7.210 -  shows "open X"
   7.211 -  using assms by (simp add: topological_basis_def)
   7.212 -
   7.213 -lemma topological_basis_imp_subbasis:
   7.214 -  assumes B: "topological_basis B"
   7.215 -  shows "open = generate_topology B"
   7.216 -proof (intro ext iffI)
   7.217 -  fix S :: "'a set"
   7.218 -  assume "open S"
   7.219 -  with B obtain B' where "B' \<subseteq> B" "S = \<Union>B'"
   7.220 -    unfolding topological_basis_def by blast
   7.221 -  then show "generate_topology B S"
   7.222 -    by (auto intro: generate_topology.intros dest: topological_basis_open)
   7.223 -next
   7.224 -  fix S :: "'a set"
   7.225 -  assume "generate_topology B S"
   7.226 -  then show "open S"
   7.227 -    by induct (auto dest: topological_basis_open[OF B])
   7.228 -qed
   7.229 -
   7.230 -lemma basis_dense:
   7.231 -  fixes B :: "'a set set"
   7.232 -    and f :: "'a set \<Rightarrow> 'a"
   7.233 -  assumes "topological_basis B"
   7.234 -    and choosefrom_basis: "\<And>B'. B' \<noteq> {} \<Longrightarrow> f B' \<in> B'"
   7.235 -  shows "\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>B' \<in> B. f B' \<in> X)"
   7.236 -proof (intro allI impI)
   7.237 -  fix X :: "'a set"
   7.238 -  assume "open X" and "X \<noteq> {}"
   7.239 -  from topological_basisE[OF \<open>topological_basis B\<close> \<open>open X\<close> choosefrom_basis[OF \<open>X \<noteq> {}\<close>]]
   7.240 -  obtain B' where "B' \<in> B" "f X \<in> B'" "B' \<subseteq> X" .
   7.241 -  then show "\<exists>B'\<in>B. f B' \<in> X"
   7.242 -    by (auto intro!: choosefrom_basis)
   7.243 -qed
   7.244 -
   7.245 -end
   7.246 -
   7.247 -lemma topological_basis_prod:
   7.248 -  assumes A: "topological_basis A"
   7.249 -    and B: "topological_basis B"
   7.250 -  shows "topological_basis ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))"
   7.251 -  unfolding topological_basis_def
   7.252 -proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric])
   7.253 -  fix S :: "('a \<times> 'b) set"
   7.254 -  assume "open S"
   7.255 -  then show "\<exists>X\<subseteq>A \<times> B. (\<Union>(a,b)\<in>X. a \<times> b) = S"
   7.256 -  proof (safe intro!: exI[of _ "{x\<in>A \<times> B. fst x \<times> snd x \<subseteq> S}"])
   7.257 -    fix x y
   7.258 -    assume "(x, y) \<in> S"
   7.259 -    from open_prod_elim[OF \<open>open S\<close> this]
   7.260 -    obtain a b where a: "open a""x \<in> a" and b: "open b" "y \<in> b" and "a \<times> b \<subseteq> S"
   7.261 -      by (metis mem_Sigma_iff)
   7.262 -    moreover
   7.263 -    from A a obtain A0 where "A0 \<in> A" "x \<in> A0" "A0 \<subseteq> a"
   7.264 -      by (rule topological_basisE)
   7.265 -    moreover
   7.266 -    from B b obtain B0 where "B0 \<in> B" "y \<in> B0" "B0 \<subseteq> b"
   7.267 -      by (rule topological_basisE)
   7.268 -    ultimately show "(x, y) \<in> (\<Union>(a, b)\<in>{X \<in> A \<times> B. fst X \<times> snd X \<subseteq> S}. a \<times> b)"
   7.269 -      by (intro UN_I[of "(A0, B0)"]) auto
   7.270 -  qed auto
   7.271 -qed (metis A B topological_basis_open open_Times)
   7.272 -
   7.273 -
   7.274 -subsection \<open>Countable Basis\<close>
   7.275 -
   7.276 -locale%important countable_basis =
   7.277 -  fixes B :: "'a::topological_space set set"
   7.278 -  assumes is_basis: "topological_basis B"
   7.279 -    and countable_basis: "countable B"
   7.280 -begin
   7.281 -
   7.282 -lemma open_countable_basis_ex:
   7.283 -  assumes "open X"
   7.284 -  shows "\<exists>B' \<subseteq> B. X = \<Union>B'"
   7.285 -  using assms countable_basis is_basis
   7.286 -  unfolding topological_basis_def by blast
   7.287 -
   7.288 -lemma open_countable_basisE:
   7.289 -  assumes "open X"
   7.290 -  obtains B' where "B' \<subseteq> B" "X = \<Union>B'"
   7.291 -  using assms open_countable_basis_ex
   7.292 -  by atomize_elim simp
   7.293 -
   7.294 -lemma countable_dense_exists:
   7.295 -  "\<exists>D::'a set. countable D \<and> (\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>d \<in> D. d \<in> X))"
   7.296 -proof -
   7.297 -  let ?f = "(\<lambda>B'. SOME x. x \<in> B')"
   7.298 -  have "countable (?f ` B)" using countable_basis by simp
   7.299 -  with basis_dense[OF is_basis, of ?f] show ?thesis
   7.300 -    by (intro exI[where x="?f ` B"]) (metis (mono_tags) all_not_in_conv imageI someI)
   7.301 -qed
   7.302 -
   7.303 -lemma countable_dense_setE:
   7.304 -  obtains D :: "'a set"
   7.305 -  where "countable D" "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d \<in> D. d \<in> X"
   7.306 -  using countable_dense_exists by blast
   7.307 -
   7.308 -end
   7.309 -
   7.310 -lemma (in first_countable_topology) first_countable_basisE:
   7.311 -  fixes x :: 'a
   7.312 -  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
   7.313 -    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
   7.314 -proof -
   7.315 -  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))"
   7.316 -    using first_countable_basis[of x] by metis
   7.317 -  show thesis
   7.318 -  proof 
   7.319 -    show "countable (range \<A>)"
   7.320 -      by simp
   7.321 -  qed (use \<A> in auto)
   7.322 -qed
   7.323 -
   7.324 -lemma (in first_countable_topology) first_countable_basis_Int_stableE:
   7.325 -  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
   7.326 -    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
   7.327 -    "\<And>A B. A \<in> \<A> \<Longrightarrow> B \<in> \<A> \<Longrightarrow> A \<inter> B \<in> \<A>"
   7.328 -proof atomize_elim
   7.329 -  obtain \<B> where \<B>:
   7.330 -    "countable \<B>"
   7.331 -    "\<And>B. B \<in> \<B> \<Longrightarrow> x \<in> B"
   7.332 -    "\<And>B. B \<in> \<B> \<Longrightarrow> open B"
   7.333 -    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>B\<in>\<B>. B \<subseteq> S"
   7.334 -    by (rule first_countable_basisE) blast
   7.335 -  define \<A> where [abs_def]:
   7.336 -    "\<A> = (\<lambda>N. \<Inter>((\<lambda>n. from_nat_into \<B> n) ` N)) ` (Collect finite::nat set set)"
   7.337 -  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>
   7.338 -        (\<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>)"
   7.339 -  proof (safe intro!: exI[where x=\<A>])
   7.340 -    show "countable \<A>"
   7.341 -      unfolding \<A>_def by (intro countable_image countable_Collect_finite)
   7.342 -    fix A
   7.343 -    assume "A \<in> \<A>"
   7.344 -    then show "x \<in> A" "open A"
   7.345 -      using \<B>(4)[OF open_UNIV] by (auto simp: \<A>_def intro: \<B> from_nat_into)
   7.346 -  next
   7.347 -    let ?int = "\<lambda>N. \<Inter>(from_nat_into \<B> ` N)"
   7.348 -    fix A B
   7.349 -    assume "A \<in> \<A>" "B \<in> \<A>"
   7.350 -    then obtain N M where "A = ?int N" "B = ?int M" "finite (N \<union> M)"
   7.351 -      by (auto simp: \<A>_def)
   7.352 -    then show "A \<inter> B \<in> \<A>"
   7.353 -      by (auto simp: \<A>_def intro!: image_eqI[where x="N \<union> M"])
   7.354 -  next
   7.355 -    fix S
   7.356 -    assume "open S" "x \<in> S"
   7.357 -    then obtain a where a: "a\<in>\<B>" "a \<subseteq> S" using \<B> by blast
   7.358 -    then show "\<exists>a\<in>\<A>. a \<subseteq> S" using a \<B>
   7.359 -      by (intro bexI[where x=a]) (auto simp: \<A>_def intro: image_eqI[where x="{to_nat_on \<B> a}"])
   7.360 -  qed
   7.361 -qed
   7.362 -
   7.363 -lemma (in topological_space) first_countableI:
   7.364 -  assumes "countable \<A>"
   7.365 -    and 1: "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
   7.366 -    and 2: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>A\<in>\<A>. A \<subseteq> S"
   7.367 -  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))"
   7.368 -proof (safe intro!: exI[of _ "from_nat_into \<A>"])
   7.369 -  fix i
   7.370 -  have "\<A> \<noteq> {}" using 2[of UNIV] by auto
   7.371 -  show "x \<in> from_nat_into \<A> i" "open (from_nat_into \<A> i)"
   7.372 -    using range_from_nat_into_subset[OF \<open>\<A> \<noteq> {}\<close>] 1 by auto
   7.373 -next
   7.374 -  fix S
   7.375 -  assume "open S" "x\<in>S" from 2[OF this]
   7.376 -  show "\<exists>i. from_nat_into \<A> i \<subseteq> S"
   7.377 -    using subset_range_from_nat_into[OF \<open>countable \<A>\<close>] by auto
   7.378 -qed
   7.379 -
   7.380 -instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology
   7.381 -proof
   7.382 -  fix x :: "'a \<times> 'b"
   7.383 -  obtain \<A> where \<A>:
   7.384 -      "countable \<A>"
   7.385 -      "\<And>a. a \<in> \<A> \<Longrightarrow> fst x \<in> a"
   7.386 -      "\<And>a. a \<in> \<A> \<Longrightarrow> open a"
   7.387 -      "\<And>S. open S \<Longrightarrow> fst x \<in> S \<Longrightarrow> \<exists>a\<in>\<A>. a \<subseteq> S"
   7.388 -    by (rule first_countable_basisE[of "fst x"]) blast
   7.389 -  obtain B where B:
   7.390 -      "countable B"
   7.391 -      "\<And>a. a \<in> B \<Longrightarrow> snd x \<in> a"
   7.392 -      "\<And>a. a \<in> B \<Longrightarrow> open a"
   7.393 -      "\<And>S. open S \<Longrightarrow> snd x \<in> S \<Longrightarrow> \<exists>a\<in>B. a \<subseteq> S"
   7.394 -    by (rule first_countable_basisE[of "snd x"]) blast
   7.395 -  show "\<exists>\<A>::nat \<Rightarrow> ('a \<times> 'b) set.
   7.396 -    (\<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))"
   7.397 -  proof (rule first_countableI[of "(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B)"], safe)
   7.398 -    fix a b
   7.399 -    assume x: "a \<in> \<A>" "b \<in> B"
   7.400 -    show "x \<in> a \<times> b" 
   7.401 -      by (simp add: \<A>(2) B(2) mem_Times_iff x)
   7.402 -    show "open (a \<times> b)"
   7.403 -      by (simp add: \<A>(3) B(3) open_Times x)
   7.404 -  next
   7.405 -    fix S
   7.406 -    assume "open S" "x \<in> S"
   7.407 -    then obtain a' b' where a'b': "open a'" "open b'" "x \<in> a' \<times> b'" "a' \<times> b' \<subseteq> S"
   7.408 -      by (rule open_prod_elim)
   7.409 -    moreover
   7.410 -    from a'b' \<A>(4)[of a'] B(4)[of b']
   7.411 -    obtain a b where "a \<in> \<A>" "a \<subseteq> a'" "b \<in> B" "b \<subseteq> b'"
   7.412 -      by auto
   7.413 -    ultimately
   7.414 -    show "\<exists>a\<in>(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B). a \<subseteq> S"
   7.415 -      by (auto intro!: bexI[of _ "a \<times> b"] bexI[of _ a] bexI[of _ b])
   7.416 -  qed (simp add: \<A> B)
   7.417 -qed
   7.418 -
   7.419 -class second_countable_topology = topological_space +
   7.420 -  assumes ex_countable_subbasis:
   7.421 -    "\<exists>B::'a::topological_space set set. countable B \<and> open = generate_topology B"
   7.422 +  imports
   7.423 +    Elementary_Topology
   7.424 +    Linear_Algebra
   7.425 +    Norm_Arith
   7.426  begin
   7.427  
   7.428 -lemma ex_countable_basis: "\<exists>B::'a set set. countable B \<and> topological_basis B"
   7.429 -proof -
   7.430 -  from ex_countable_subbasis obtain B where B: "countable B" "open = generate_topology B"
   7.431 -    by blast
   7.432 -  let ?B = "Inter ` {b. finite b \<and> b \<subseteq> B }"
   7.433 -
   7.434 -  show ?thesis
   7.435 -  proof (intro exI conjI)
   7.436 -    show "countable ?B"
   7.437 -      by (intro countable_image countable_Collect_finite_subset B)
   7.438 -    {
   7.439 -      fix S
   7.440 -      assume "open S"
   7.441 -      then have "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. (\<Union>b\<in>B'. \<Inter>b) = S"
   7.442 -        unfolding B
   7.443 -      proof induct
   7.444 -        case UNIV
   7.445 -        show ?case by (intro exI[of _ "{{}}"]) simp
   7.446 -      next
   7.447 -        case (Int a b)
   7.448 -        then obtain x y where x: "a = \<Union>(Inter ` x)" "\<And>i. i \<in> x \<Longrightarrow> finite i \<and> i \<subseteq> B"
   7.449 -          and y: "b = \<Union>(Inter ` y)" "\<And>i. i \<in> y \<Longrightarrow> finite i \<and> i \<subseteq> B"
   7.450 -          by blast
   7.451 -        show ?case
   7.452 -          unfolding x y Int_UN_distrib2
   7.453 -          by (intro exI[of _ "{i \<union> j| i j.  i \<in> x \<and> j \<in> y}"]) (auto dest: x(2) y(2))
   7.454 -      next
   7.455 -        case (UN K)
   7.456 -        then have "\<forall>k\<in>K. \<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = k" by auto
   7.457 -        then obtain k where
   7.458 -            "\<forall>ka\<in>K. k ka \<subseteq> {b. finite b \<and> b \<subseteq> B} \<and> \<Union>(Inter ` (k ka)) = ka"
   7.459 -          unfolding bchoice_iff ..
   7.460 -        then show "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = \<Union>K"
   7.461 -          by (intro exI[of _ "\<Union>(k ` K)"]) auto
   7.462 -      next
   7.463 -        case (Basis S)
   7.464 -        then show ?case
   7.465 -          by (intro exI[of _ "{{S}}"]) auto
   7.466 -      qed
   7.467 -      then have "(\<exists>B'\<subseteq>Inter ` {b. finite b \<and> b \<subseteq> B}. \<Union>B' = S)"
   7.468 -        unfolding subset_image_iff by blast }
   7.469 -    then show "topological_basis ?B"
   7.470 -      unfolding topological_space_class.topological_basis_def
   7.471 -      by (safe intro!: topological_space_class.open_Inter)
   7.472 -         (simp_all add: B generate_topology.Basis subset_eq)
   7.473 -  qed
   7.474 -qed
   7.475 -
   7.476 -end
   7.477 -
   7.478 -sublocale second_countable_topology <
   7.479 -  countable_basis "SOME B. countable B \<and> topological_basis B"
   7.480 -  using someI_ex[OF ex_countable_basis]
   7.481 -  by unfold_locales safe
   7.482 -
   7.483 -instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology
   7.484 -proof
   7.485 -  obtain A :: "'a set set" where "countable A" "topological_basis A"
   7.486 -    using ex_countable_basis by auto
   7.487 -  moreover
   7.488 -  obtain B :: "'b set set" where "countable B" "topological_basis B"
   7.489 -    using ex_countable_basis by auto
   7.490 -  ultimately show "\<exists>B::('a \<times> 'b) set set. countable B \<and> open = generate_topology B"
   7.491 -    by (auto intro!: exI[of _ "(\<lambda>(a, b). a \<times> b) ` (A \<times> B)"] topological_basis_prod
   7.492 -      topological_basis_imp_subbasis)
   7.493 -qed
   7.494 -
   7.495 -instance second_countable_topology \<subseteq> first_countable_topology
   7.496 -proof
   7.497 -  fix x :: 'a
   7.498 -  define B :: "'a set set" where "B = (SOME B. countable B \<and> topological_basis B)"
   7.499 -  then have B: "countable B" "topological_basis B"
   7.500 -    using countable_basis is_basis
   7.501 -    by (auto simp: countable_basis is_basis)
   7.502 -  then show "\<exists>A::nat \<Rightarrow> 'a set.
   7.503 -    (\<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))"
   7.504 -    by (intro first_countableI[of "{b\<in>B. x \<in> b}"])
   7.505 -       (fastforce simp: topological_space_class.topological_basis_def)+
   7.506 -qed
   7.507 -
   7.508 -instance nat :: second_countable_topology
   7.509 -proof
   7.510 -  show "\<exists>B::nat set set. countable B \<and> open = generate_topology B"
   7.511 -    by (intro exI[of _ "range lessThan \<union> range greaterThan"]) (auto simp: open_nat_def)
   7.512 -qed
   7.513 -
   7.514 -lemma countable_separating_set_linorder1:
   7.515 -  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))"
   7.516 -proof -
   7.517 -  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
   7.518 -  define B1 where "B1 = {(LEAST x. x \<in> U)| U. U \<in> A}"
   7.519 -  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   7.520 -  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
   7.521 -  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   7.522 -  have "\<exists>b \<in> B1 \<union> B2. x < b \<and> b \<le> y" if "x < y" for x y
   7.523 -  proof (cases)
   7.524 -    assume "\<exists>z. x < z \<and> z < y"
   7.525 -    then obtain z where z: "x < z \<and> z < y" by auto
   7.526 -    define U where "U = {x<..<y}"
   7.527 -    then have "open U" by simp
   7.528 -    moreover have "z \<in> U" using z U_def by simp
   7.529 -    ultimately obtain V where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
   7.530 -    define w where "w = (SOME x. x \<in> V)"
   7.531 -    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
   7.532 -    then have "x < w \<and> w \<le> y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
   7.533 -    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
   7.534 -    ultimately show ?thesis by auto
   7.535 -  next
   7.536 -    assume "\<not>(\<exists>z. x < z \<and> z < y)"
   7.537 -    then have *: "\<And>z. z > x \<Longrightarrow> z \<ge> y" by auto
   7.538 -    define U where "U = {x<..}"
   7.539 -    then have "open U" by simp
   7.540 -    moreover have "y \<in> U" using \<open>x < y\<close> U_def by simp
   7.541 -    ultimately obtain "V" where "V \<in> A" "y \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
   7.542 -    have "U = {y..}" unfolding U_def using * \<open>x < y\<close> by auto
   7.543 -    then have "V \<subseteq> {y..}" using \<open>V \<subseteq> U\<close> by simp
   7.544 -    then have "(LEAST w. w \<in> V) = y" using \<open>y \<in> V\<close> by (meson Least_equality atLeast_iff subsetCE)
   7.545 -    then have "y \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
   7.546 -    moreover have "x < y \<and> y \<le> y" using \<open>x < y\<close> by simp
   7.547 -    ultimately show ?thesis by auto
   7.548 -  qed
   7.549 -  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
   7.550 -  ultimately show ?thesis by auto
   7.551 -qed
   7.552 -
   7.553 -lemma countable_separating_set_linorder2:
   7.554 -  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))"
   7.555 -proof -
   7.556 -  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
   7.557 -  define B1 where "B1 = {(GREATEST x. x \<in> U) | U. U \<in> A}"
   7.558 -  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   7.559 -  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
   7.560 -  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
   7.561 -  have "\<exists>b \<in> B1 \<union> B2. x \<le> b \<and> b < y" if "x < y" for x y
   7.562 -  proof (cases)
   7.563 -    assume "\<exists>z. x < z \<and> z < y"
   7.564 -    then obtain z where z: "x < z \<and> z < y" by auto
   7.565 -    define U where "U = {x<..<y}"
   7.566 -    then have "open U" by simp
   7.567 -    moreover have "z \<in> U" using z U_def by simp
   7.568 -    ultimately obtain "V" where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
   7.569 -    define w where "w = (SOME x. x \<in> V)"
   7.570 -    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
   7.571 -    then have "x \<le> w \<and> w < y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
   7.572 -    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
   7.573 -    ultimately show ?thesis by auto
   7.574 -  next
   7.575 -    assume "\<not>(\<exists>z. x < z \<and> z < y)"
   7.576 -    then have *: "\<And>z. z < y \<Longrightarrow> z \<le> x" using leI by blast
   7.577 -    define U where "U = {..<y}"
   7.578 -    then have "open U" by simp
   7.579 -    moreover have "x \<in> U" using \<open>x < y\<close> U_def by simp
   7.580 -    ultimately obtain "V" where "V \<in> A" "x \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
   7.581 -    have "U = {..x}" unfolding U_def using * \<open>x < y\<close> by auto
   7.582 -    then have "V \<subseteq> {..x}" using \<open>V \<subseteq> U\<close> by simp
   7.583 -    then have "(GREATEST x. x \<in> V) = x" using \<open>x \<in> V\<close> by (meson Greatest_equality atMost_iff subsetCE)
   7.584 -    then have "x \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
   7.585 -    moreover have "x \<le> x \<and> x < y" using \<open>x < y\<close> by simp
   7.586 -    ultimately show ?thesis by auto
   7.587 -  qed
   7.588 -  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
   7.589 -  ultimately show ?thesis by auto
   7.590 -qed
   7.591 -
   7.592 -lemma countable_separating_set_dense_linorder:
   7.593 -  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))"
   7.594 -proof -
   7.595 -  obtain B::"'a set" where B: "countable B" "\<And>x y. x < y \<Longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y)"
   7.596 -    using countable_separating_set_linorder1 by auto
   7.597 -  have "\<exists>b \<in> B. x < b \<and> b < y" if "x < y" for x y
   7.598 -  proof -
   7.599 -    obtain z where "x < z" "z < y" using \<open>x < y\<close> dense by blast
   7.600 -    then obtain b where "b \<in> B" "x < b \<and> b \<le> z" using B(2) by auto
   7.601 -    then have "x < b \<and> b < y" using \<open>z < y\<close> by auto
   7.602 -    then show ?thesis using \<open>b \<in> B\<close> by auto
   7.603 -  qed
   7.604 -  then show ?thesis using B(1) by auto
   7.605 -qed
   7.606 -
   7.607 -subsection%important \<open>Polish spaces\<close>
   7.608 -
   7.609 -text \<open>Textbooks define Polish spaces as completely metrizable.
   7.610 -  We assume the topology to be complete for a given metric.\<close>
   7.611 -
   7.612 -class polish_space = complete_space + second_countable_topology
   7.613 -
   7.614 -subsection \<open>General notion of a topology as a value\<close>
   7.615 -
   7.616 -definition%important "istopology L \<longleftrightarrow>
   7.617 -  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))"
   7.618 -
   7.619 -typedef%important 'a topology = "{L::('a set) \<Rightarrow> bool. istopology L}"
   7.620 -  morphisms "openin" "topology"
   7.621 -  unfolding istopology_def by blast
   7.622 -
   7.623 -lemma istopology_openin[intro]: "istopology(openin U)"
   7.624 -  using openin[of U] by blast
   7.625 -
   7.626 -lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
   7.627 -  using topology_inverse[unfolded mem_Collect_eq] .
   7.628 -
   7.629 -lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
   7.630 -  using topology_inverse[of U] istopology_openin[of "topology U"] by auto
   7.631 -
   7.632 -lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
   7.633 -proof
   7.634 -  assume "T1 = T2"
   7.635 -  then show "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp
   7.636 -next
   7.637 -  assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
   7.638 -  then have "openin T1 = openin T2" by (simp add: fun_eq_iff)
   7.639 -  then have "topology (openin T1) = topology (openin T2)" by simp
   7.640 -  then show "T1 = T2" unfolding openin_inverse .
   7.641 -qed
   7.642 -
   7.643 -
   7.644 -text\<open>The "universe": the union of all sets in the topology.\<close>
   7.645 -definition "topspace T = \<Union>{S. openin T S}"
   7.646 -
   7.647 -subsubsection \<open>Main properties of open sets\<close>
   7.648 -
   7.649 -proposition openin_clauses:
   7.650 -  fixes U :: "'a topology"
   7.651 -  shows
   7.652 -    "openin U {}"
   7.653 -    "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
   7.654 -    "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
   7.655 -  using openin[of U] unfolding istopology_def mem_Collect_eq by fast+
   7.656 -
   7.657 -lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
   7.658 -  unfolding topspace_def by blast
   7.659 -
   7.660 -lemma openin_empty[simp]: "openin U {}"
   7.661 -  by (rule openin_clauses)
   7.662 -
   7.663 -lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
   7.664 -  by (rule openin_clauses)
   7.665 -
   7.666 -lemma openin_Union[intro]: "(\<And>S. S \<in> K \<Longrightarrow> openin U S) \<Longrightarrow> openin U (\<Union>K)"
   7.667 -  using openin_clauses by blast
   7.668 -
   7.669 -lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
   7.670 -  using openin_Union[of "{S,T}" U] by auto
   7.671 -
   7.672 -lemma openin_topspace[intro, simp]: "openin U (topspace U)"
   7.673 -  by (force simp: openin_Union topspace_def)
   7.674 -
   7.675 -lemma openin_subopen: "openin U S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>T. openin U T \<and> x \<in> T \<and> T \<subseteq> S)"
   7.676 -  (is "?lhs \<longleftrightarrow> ?rhs")
   7.677 -proof
   7.678 -  assume ?lhs
   7.679 -  then show ?rhs by auto
   7.680 -next
   7.681 -  assume H: ?rhs
   7.682 -  let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"
   7.683 -  have "openin U ?t" by (force simp: openin_Union)
   7.684 -  also have "?t = S" using H by auto
   7.685 -  finally show "openin U S" .
   7.686 -qed
   7.687 -
   7.688 -lemma openin_INT [intro]:
   7.689 -  assumes "finite I"
   7.690 -          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
   7.691 -  shows "openin T ((\<Inter>i \<in> I. U i) \<inter> topspace T)"
   7.692 -using assms by (induct, auto simp: inf_sup_aci(2) openin_Int)
   7.693 -
   7.694 -lemma openin_INT2 [intro]:
   7.695 -  assumes "finite I" "I \<noteq> {}"
   7.696 -          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
   7.697 -  shows "openin T (\<Inter>i \<in> I. U i)"
   7.698 -proof -
   7.699 -  have "(\<Inter>i \<in> I. U i) \<subseteq> topspace T"
   7.700 -    using \<open>I \<noteq> {}\<close> openin_subset[OF assms(3)] by auto
   7.701 -  then show ?thesis
   7.702 -    using openin_INT[of _ _ U, OF assms(1) assms(3)] by (simp add: inf.absorb2 inf_commute)
   7.703 -qed
   7.704 -
   7.705 -lemma openin_Inter [intro]:
   7.706 -  assumes "finite \<F>" "\<F> \<noteq> {}" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (\<Inter>\<F>)"
   7.707 -  by (metis (full_types) assms openin_INT2 image_ident)
   7.708 -
   7.709 -lemma openin_Int_Inter:
   7.710 -  assumes "finite \<F>" "openin T U" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (U \<inter> \<Inter>\<F>)"
   7.711 -  using openin_Inter [of "insert U \<F>"] assms by auto
   7.712 -
   7.713 -
   7.714 -subsubsection \<open>Closed sets\<close>
   7.715 -
   7.716 -definition%important "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
   7.717 -
   7.718 -lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U"
   7.719 -  by (metis closedin_def)
   7.720 -
   7.721 -lemma closedin_empty[simp]: "closedin U {}"
   7.722 -  by (simp add: closedin_def)
   7.723 -
   7.724 -lemma closedin_topspace[intro, simp]: "closedin U (topspace U)"
   7.725 -  by (simp add: closedin_def)
   7.726 -
   7.727 -lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
   7.728 -  by (auto simp: Diff_Un closedin_def)
   7.729 -
   7.730 -lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union>{A - s|s. s\<in>S}"
   7.731 -  by auto
   7.732 -
   7.733 -lemma closedin_Union:
   7.734 -  assumes "finite S" "\<And>T. T \<in> S \<Longrightarrow> closedin U T"
   7.735 -    shows "closedin U (\<Union>S)"
   7.736 -  using assms by induction auto
   7.737 -
   7.738 -lemma closedin_Inter[intro]:
   7.739 -  assumes Ke: "K \<noteq> {}"
   7.740 -    and Kc: "\<And>S. S \<in>K \<Longrightarrow> closedin U S"
   7.741 -  shows "closedin U (\<Inter>K)"
   7.742 -  using Ke Kc unfolding closedin_def Diff_Inter by auto
   7.743 -
   7.744 -lemma closedin_INT[intro]:
   7.745 -  assumes "A \<noteq> {}" "\<And>x. x \<in> A \<Longrightarrow> closedin U (B x)"
   7.746 -  shows "closedin U (\<Inter>x\<in>A. B x)"
   7.747 -  apply (rule closedin_Inter)
   7.748 -  using assms
   7.749 -  apply auto
   7.750 -  done
   7.751 -
   7.752 -lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
   7.753 -  using closedin_Inter[of "{S,T}" U] by auto
   7.754 -
   7.755 -lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
   7.756 -  apply (auto simp: closedin_def Diff_Diff_Int inf_absorb2)
   7.757 -  apply (metis openin_subset subset_eq)
   7.758 -  done
   7.759 -
   7.760 -lemma openin_closedin: "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
   7.761 -  by (simp add: openin_closedin_eq)
   7.762 -
   7.763 -lemma openin_diff[intro]:
   7.764 -  assumes oS: "openin U S"
   7.765 -    and cT: "closedin U T"
   7.766 -  shows "openin U (S - T)"
   7.767 -proof -
   7.768 -  have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
   7.769 -    by (auto simp: topspace_def openin_subset)
   7.770 -  then show ?thesis using oS cT
   7.771 -    by (auto simp: closedin_def)
   7.772 -qed
   7.773 -
   7.774 -lemma closedin_diff[intro]:
   7.775 -  assumes oS: "closedin U S"
   7.776 -    and cT: "openin U T"
   7.777 -  shows "closedin U (S - T)"
   7.778 -proof -
   7.779 -  have "S - T = S \<inter> (topspace U - T)"
   7.780 -    using closedin_subset[of U S] oS cT by (auto simp: topspace_def)
   7.781 -  then show ?thesis
   7.782 -    using oS cT by (auto simp: openin_closedin_eq)
   7.783 -qed
   7.784 -
   7.785 -
   7.786 -subsection\<open>The discrete topology\<close>
   7.787 -
   7.788 -definition discrete_topology where "discrete_topology U \<equiv> topology (\<lambda>S. S \<subseteq> U)"
   7.789 -
   7.790 -lemma openin_discrete_topology [simp]: "openin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
   7.791 -proof -
   7.792 -  have "istopology (\<lambda>S. S \<subseteq> U)"
   7.793 -    by (auto simp: istopology_def)
   7.794 -  then show ?thesis
   7.795 -    by (simp add: discrete_topology_def topology_inverse')
   7.796 -qed
   7.797 -
   7.798 -lemma topspace_discrete_topology [simp]: "topspace(discrete_topology U) = U"
   7.799 -  by (meson openin_discrete_topology openin_subset openin_topspace order_refl subset_antisym)
   7.800 -
   7.801 -lemma closedin_discrete_topology [simp]: "closedin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
   7.802 -  by (simp add: closedin_def)
   7.803 -
   7.804 -lemma discrete_topology_unique:
   7.805 -   "discrete_topology U = X \<longleftrightarrow> topspace X = U \<and> (\<forall>x \<in> U. openin X {x})" (is "?lhs = ?rhs")
   7.806 -proof
   7.807 -  assume R: ?rhs
   7.808 -  then have "openin X S" if "S \<subseteq> U" for S
   7.809 -    using openin_subopen subsetD that by fastforce
   7.810 -  moreover have "x \<in> topspace X" if "openin X S" and "x \<in> S" for x S
   7.811 -    using openin_subset that by blast
   7.812 -  ultimately
   7.813 -  show ?lhs
   7.814 -    using R by (auto simp: topology_eq)
   7.815 -qed auto
   7.816 -
   7.817 -lemma discrete_topology_unique_alt:
   7.818 -  "discrete_topology U = X \<longleftrightarrow> topspace X \<subseteq> U \<and> (\<forall>x \<in> U. openin X {x})"
   7.819 -  using openin_subset
   7.820 -  by (auto simp: discrete_topology_unique)
   7.821 -
   7.822 -lemma subtopology_eq_discrete_topology_empty:
   7.823 -   "X = discrete_topology {} \<longleftrightarrow> topspace X = {}"
   7.824 -  using discrete_topology_unique [of "{}" X] by auto
   7.825 -
   7.826 -lemma subtopology_eq_discrete_topology_sing:
   7.827 -   "X = discrete_topology {a} \<longleftrightarrow> topspace X = {a}"
   7.828 -  by (metis discrete_topology_unique openin_topspace singletonD)
   7.829 -
   7.830 -
   7.831 -subsection \<open>Subspace topology\<close>
   7.832 -
   7.833 -definition%important "subtopology U V = topology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
   7.834 -
   7.835 -lemma istopology_subtopology: "istopology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
   7.836 -  (is "istopology ?L")
   7.837 -proof -
   7.838 -  have "?L {}" by blast
   7.839 -  {
   7.840 -    fix A B
   7.841 -    assume A: "?L A" and B: "?L B"
   7.842 -    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"
   7.843 -      by blast
   7.844 -    have "A \<inter> B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"
   7.845 -      using Sa Sb by blast+
   7.846 -    then have "?L (A \<inter> B)" by blast
   7.847 -  }
   7.848 -  moreover
   7.849 -  {
   7.850 -    fix K
   7.851 -    assume K: "K \<subseteq> Collect ?L"
   7.852 -    have th0: "Collect ?L = (\<lambda>S. S \<inter> V) ` Collect (openin U)"
   7.853 -      by blast
   7.854 -    from K[unfolded th0 subset_image_iff]
   7.855 -    obtain Sk where Sk: "Sk \<subseteq> Collect (openin U)" "K = (\<lambda>S. S \<inter> V) ` Sk"
   7.856 -      by blast
   7.857 -    have "\<Union>K = (\<Union>Sk) \<inter> V"
   7.858 -      using Sk by auto
   7.859 -    moreover have "openin U (\<Union>Sk)"
   7.860 -      using Sk by (auto simp: subset_eq)
   7.861 -    ultimately have "?L (\<Union>K)" by blast
   7.862 -  }
   7.863 -  ultimately show ?thesis
   7.864 -    unfolding subset_eq mem_Collect_eq istopology_def by auto
   7.865 -qed
   7.866 -
   7.867 -lemma openin_subtopology: "openin (subtopology U V) S \<longleftrightarrow> (\<exists>T. openin U T \<and> S = T \<inter> V)"
   7.868 -  unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
   7.869 -  by auto
   7.870 -
   7.871 -lemma openin_subtopology_Int:
   7.872 -   "openin X S \<Longrightarrow> openin (subtopology X T) (S \<inter> T)"
   7.873 -  using openin_subtopology by auto
   7.874 -
   7.875 -lemma openin_subtopology_Int2:
   7.876 -   "openin X T \<Longrightarrow> openin (subtopology X S) (S \<inter> T)"
   7.877 -  using openin_subtopology by auto
   7.878 -
   7.879 -lemma openin_subtopology_diff_closed:
   7.880 -   "\<lbrakk>S \<subseteq> topspace X; closedin X T\<rbrakk> \<Longrightarrow> openin (subtopology X S) (S - T)"
   7.881 -  unfolding closedin_def openin_subtopology
   7.882 -  by (rule_tac x="topspace X - T" in exI) auto
   7.883 -
   7.884 -lemma openin_relative_to: "(openin X relative_to S) = openin (subtopology X S)"
   7.885 -  by (force simp: relative_to_def openin_subtopology)
   7.886 -
   7.887 -lemma topspace_subtopology: "topspace (subtopology U V) = topspace U \<inter> V"
   7.888 -  by (auto simp: topspace_def openin_subtopology)
   7.889 -
   7.890 -lemma closedin_subtopology: "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
   7.891 -  unfolding closedin_def topspace_subtopology
   7.892 -  by (auto simp: openin_subtopology)
   7.893 -
   7.894 -lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
   7.895 -  unfolding openin_subtopology
   7.896 -  by auto (metis IntD1 in_mono openin_subset)
   7.897 -
   7.898 -lemma subtopology_subtopology:
   7.899 -   "subtopology (subtopology X S) T = subtopology X (S \<inter> T)"
   7.900 -proof -
   7.901 -  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)"
   7.902 -    by (metis inf_assoc)
   7.903 -  have "subtopology (subtopology X S) T = topology (\<lambda>Ta. \<exists>Sa. Ta = Sa \<inter> T \<and> openin (subtopology X S) Sa)"
   7.904 -    by (simp add: subtopology_def)
   7.905 -  also have "\<dots> = subtopology X (S \<inter> T)"
   7.906 -    by (simp add: openin_subtopology eq) (simp add: subtopology_def)
   7.907 -  finally show ?thesis .
   7.908 -qed
   7.909 -
   7.910 -lemma openin_subtopology_alt:
   7.911 -     "openin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (openin X)"
   7.912 -  by (simp add: image_iff inf_commute openin_subtopology)
   7.913 -
   7.914 -lemma closedin_subtopology_alt:
   7.915 -     "closedin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (closedin X)"
   7.916 -  by (simp add: image_iff inf_commute closedin_subtopology)
   7.917 -
   7.918 -lemma subtopology_superset:
   7.919 -  assumes UV: "topspace U \<subseteq> V"
   7.920 -  shows "subtopology U V = U"
   7.921 -proof -
   7.922 -  {
   7.923 -    fix S
   7.924 -    {
   7.925 -      fix T
   7.926 -      assume T: "openin U T" "S = T \<inter> V"
   7.927 -      from T openin_subset[OF T(1)] UV have eq: "S = T"
   7.928 -        by blast
   7.929 -      have "openin U S"
   7.930 -        unfolding eq using T by blast
   7.931 -    }
   7.932 -    moreover
   7.933 -    {
   7.934 -      assume S: "openin U S"
   7.935 -      then have "\<exists>T. openin U T \<and> S = T \<inter> V"
   7.936 -        using openin_subset[OF S] UV by auto
   7.937 -    }
   7.938 -    ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S"
   7.939 -      by blast
   7.940 -  }
   7.941 -  then show ?thesis
   7.942 -    unfolding topology_eq openin_subtopology by blast
   7.943 -qed
   7.944 -
   7.945 -lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
   7.946 -  by (simp add: subtopology_superset)
   7.947 -
   7.948 -lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
   7.949 -  by (simp add: subtopology_superset)
   7.950 -
   7.951 -lemma openin_subtopology_empty:
   7.952 -   "openin (subtopology U {}) S \<longleftrightarrow> S = {}"
   7.953 -by (metis Int_empty_right openin_empty openin_subtopology)
   7.954 -
   7.955 -lemma closedin_subtopology_empty:
   7.956 -   "closedin (subtopology U {}) S \<longleftrightarrow> S = {}"
   7.957 -by (metis Int_empty_right closedin_empty closedin_subtopology)
   7.958 -
   7.959 -lemma closedin_subtopology_refl [simp]:
   7.960 -   "closedin (subtopology U X) X \<longleftrightarrow> X \<subseteq> topspace U"
   7.961 -by (metis closedin_def closedin_topspace inf.absorb_iff2 le_inf_iff topspace_subtopology)
   7.962 -
   7.963 -lemma closedin_topspace_empty: "topspace T = {} \<Longrightarrow> (closedin T S \<longleftrightarrow> S = {})"
   7.964 -  by (simp add: closedin_def)
   7.965 -
   7.966 -lemma openin_imp_subset:
   7.967 -   "openin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
   7.968 -by (metis Int_iff openin_subtopology subsetI)
   7.969 -
   7.970 -lemma closedin_imp_subset:
   7.971 -   "closedin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
   7.972 -by (simp add: closedin_def topspace_subtopology)
   7.973 -
   7.974 -lemma openin_open_subtopology:
   7.975 -     "openin X S \<Longrightarrow> openin (subtopology X S) T \<longleftrightarrow> openin X T \<and> T \<subseteq> S"
   7.976 -  by (metis inf.orderE openin_Int openin_imp_subset openin_subtopology)
   7.977 -
   7.978 -lemma closedin_closed_subtopology:
   7.979 -     "closedin X S \<Longrightarrow> (closedin (subtopology X S) T \<longleftrightarrow> closedin X T \<and> T \<subseteq> S)"
   7.980 -  by (metis closedin_Int closedin_imp_subset closedin_subtopology inf.orderE)
   7.981 -
   7.982 -lemma openin_subtopology_Un:
   7.983 -    "\<lbrakk>openin (subtopology X T) S; openin (subtopology X U) S\<rbrakk>
   7.984 -     \<Longrightarrow> openin (subtopology X (T \<union> U)) S"
   7.985 -by (simp add: openin_subtopology) blast
   7.986 -
   7.987 -lemma closedin_subtopology_Un:
   7.988 -    "\<lbrakk>closedin (subtopology X T) S; closedin (subtopology X U) S\<rbrakk>
   7.989 -     \<Longrightarrow> closedin (subtopology X (T \<union> U)) S"
   7.990 -by (simp add: closedin_subtopology) blast
   7.991 -
   7.992 -
   7.993 -subsection \<open>The standard Euclidean topology\<close>
   7.994 -
   7.995 -definition%important euclidean :: "'a::topological_space topology"
   7.996 -  where "euclidean = topology open"
   7.997 -
   7.998 -lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
   7.999 -  unfolding euclidean_def
  7.1000 -  apply (rule cong[where x=S and y=S])
  7.1001 -  apply (rule topology_inverse[symmetric])
  7.1002 -  apply (auto simp: istopology_def)
  7.1003 -  done
  7.1004 -
  7.1005 -declare open_openin [symmetric, simp]
  7.1006 -
  7.1007 -lemma topspace_euclidean [simp]: "topspace euclidean = UNIV"
  7.1008 -  by (force simp: topspace_def)
  7.1009 -
  7.1010 -lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
  7.1011 -  by (simp add: topspace_subtopology)
  7.1012 -
  7.1013 -lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
  7.1014 -  by (simp add: closed_def closedin_def Compl_eq_Diff_UNIV)
  7.1015 -
  7.1016 -declare closed_closedin [symmetric, simp]
  7.1017 -
  7.1018 -lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
  7.1019 -  using openI by auto
  7.1020 -
  7.1021 -lemma openin_subtopology_self [simp]: "openin (subtopology euclidean S) S"
  7.1022 -  by (metis openin_topspace topspace_euclidean_subtopology)
  7.1023 -
  7.1024 -subsubsection\<open>The most basic facts about the usual topology and metric on R\<close>
  7.1025 -
  7.1026 -abbreviation euclideanreal :: "real topology"
  7.1027 -  where "euclideanreal \<equiv> topology open"
  7.1028 -
  7.1029 -lemma real_openin [simp]: "openin euclideanreal S = open S"
  7.1030 -  by (simp add: euclidean_def open_openin)
  7.1031 -
  7.1032 -lemma topspace_euclideanreal [simp]: "topspace euclideanreal = UNIV"
  7.1033 -  using openin_subset open_UNIV real_openin by blast
  7.1034 -
  7.1035 -lemma topspace_euclideanreal_subtopology [simp]:
  7.1036 -   "topspace (subtopology euclideanreal S) = S"
  7.1037 -  by (simp add: topspace_subtopology)
  7.1038 -
  7.1039 -lemma real_closedin [simp]: "closedin euclideanreal S = closed S"
  7.1040 -  by (simp add: closed_closedin euclidean_def)
  7.1041 -
  7.1042 -subsection \<open>Basic "localization" results are handy for connectedness.\<close>
  7.1043 -
  7.1044 -lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
  7.1045 -  by (auto simp: openin_subtopology)
  7.1046 -
  7.1047 -lemma openin_Int_open:
  7.1048 -   "\<lbrakk>openin (subtopology euclidean U) S; open T\<rbrakk>
  7.1049 -        \<Longrightarrow> openin (subtopology euclidean U) (S \<inter> T)"
  7.1050 -by (metis open_Int Int_assoc openin_open)
  7.1051 -
  7.1052 -lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
  7.1053 -  by (auto simp: openin_open)
  7.1054 -
  7.1055 -lemma open_openin_trans[trans]:
  7.1056 -  "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
  7.1057 -  by (metis Int_absorb1  openin_open_Int)
  7.1058 -
  7.1059 -lemma open_subset: "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
  7.1060 -  by (auto simp: openin_open)
  7.1061 -
  7.1062 -lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
  7.1063 -  by (simp add: closedin_subtopology Int_ac)
  7.1064 -
  7.1065 -lemma closedin_closed_Int: "closed S \<Longrightarrow> closedin (subtopology euclidean U) (U \<inter> S)"
  7.1066 -  by (metis closedin_closed)
  7.1067 -
  7.1068 -lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
  7.1069 -  by (auto simp: closedin_closed)
  7.1070 -
  7.1071 -lemma closedin_closed_subset:
  7.1072 - "\<lbrakk>closedin (subtopology euclidean U) V; T \<subseteq> U; S = V \<inter> T\<rbrakk>
  7.1073 -             \<Longrightarrow> closedin (subtopology euclidean T) S"
  7.1074 -  by (metis (no_types, lifting) Int_assoc Int_commute closedin_closed inf.orderE)
  7.1075 -
  7.1076 -lemma finite_imp_closedin:
  7.1077 -  fixes S :: "'a::t1_space set"
  7.1078 -  shows "\<lbrakk>finite S; S \<subseteq> T\<rbrakk> \<Longrightarrow> closedin (subtopology euclidean T) S"
  7.1079 -    by (simp add: finite_imp_closed closed_subset)
  7.1080 -
  7.1081 -lemma closedin_singleton [simp]:
  7.1082 -  fixes a :: "'a::t1_space"
  7.1083 -  shows "closedin (subtopology euclidean U) {a} \<longleftrightarrow> a \<in> U"
  7.1084 -using closedin_subset  by (force intro: closed_subset)
  7.1085 -
  7.1086 -lemma openin_euclidean_subtopology_iff:
  7.1087 -  fixes S U :: "'a::metric_space set"
  7.1088 -  shows "openin (subtopology euclidean U) S \<longleftrightarrow>
  7.1089 -    S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)"
  7.1090 -  (is "?lhs \<longleftrightarrow> ?rhs")
  7.1091 -proof
  7.1092 -  assume ?lhs
  7.1093 -  then show ?rhs
  7.1094 -    unfolding openin_open open_dist by blast
  7.1095 -next
  7.1096 -  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}"
  7.1097 -  have 1: "\<forall>x\<in>T. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> T"
  7.1098 -    unfolding T_def
  7.1099 -    apply clarsimp
  7.1100 -    apply (rule_tac x="d - dist x a" in exI)
  7.1101 -    apply (clarsimp simp add: less_diff_eq)
  7.1102 -    by (metis dist_commute dist_triangle_lt)
  7.1103 -  assume ?rhs then have 2: "S = U \<inter> T"
  7.1104 -    unfolding T_def
  7.1105 -    by auto (metis dist_self)
  7.1106 -  from 1 2 show ?lhs
  7.1107 -    unfolding openin_open open_dist by fast
  7.1108 -qed
  7.1109 -
  7.1110 -lemma connected_openin:
  7.1111 -      "connected S \<longleftrightarrow>
  7.1112 -       \<not>(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
  7.1113 -                 openin (subtopology euclidean S) E2 \<and>
  7.1114 -                 S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
  7.1115 -  apply (simp add: connected_def openin_open disjoint_iff_not_equal, safe)
  7.1116 -  apply (simp_all, blast+)  (* SLOW *)
  7.1117 -  done
  7.1118 -
  7.1119 -lemma connected_openin_eq:
  7.1120 -      "connected S \<longleftrightarrow>
  7.1121 -       \<not>(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
  7.1122 -                 openin (subtopology euclidean S) E2 \<and>
  7.1123 -                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
  7.1124 -                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
  7.1125 -  apply (simp add: connected_openin, safe, blast)
  7.1126 -  by (metis Int_lower1 Un_subset_iff openin_open subset_antisym)
  7.1127 -
  7.1128 -lemma connected_closedin:
  7.1129 -      "connected S \<longleftrightarrow>
  7.1130 -       (\<nexists>E1 E2.
  7.1131 -        closedin (subtopology euclidean S) E1 \<and>
  7.1132 -        closedin (subtopology euclidean S) E2 \<and>
  7.1133 -        S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
  7.1134 -       (is "?lhs = ?rhs")
  7.1135 -proof
  7.1136 -  assume ?lhs
  7.1137 -  then show ?rhs 
  7.1138 -    by (auto simp add: connected_closed closedin_closed)
  7.1139 -next
  7.1140 -  assume R: ?rhs
  7.1141 -  then show ?lhs 
  7.1142 -  proof (clarsimp simp add: connected_closed closedin_closed)
  7.1143 -    fix A B 
  7.1144 -    assume s_sub: "S \<subseteq> A \<union> B" "B \<inter> S \<noteq> {}"
  7.1145 -      and disj: "A \<inter> B \<inter> S = {}"
  7.1146 -      and cl: "closed A" "closed B"
  7.1147 -    have "S \<inter> (A \<union> B) = S"
  7.1148 -      using s_sub(1) by auto
  7.1149 -    have "S - A = B \<inter> S"
  7.1150 -      using Diff_subset_conv Un_Diff_Int disj s_sub(1) by auto
  7.1151 -    then have "S \<inter> A = {}"
  7.1152 -      by (metis Diff_Diff_Int Diff_disjoint Un_Diff_Int R cl closedin_closed_Int inf_commute order_refl s_sub(2))
  7.1153 -    then show "A \<inter> S = {}"
  7.1154 -      by blast
  7.1155 -  qed
  7.1156 -qed
  7.1157 -
  7.1158 -lemma connected_closedin_eq:
  7.1159 -      "connected S \<longleftrightarrow>
  7.1160 -           \<not>(\<exists>E1 E2.
  7.1161 -                 closedin (subtopology euclidean S) E1 \<and>
  7.1162 -                 closedin (subtopology euclidean S) E2 \<and>
  7.1163 -                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
  7.1164 -                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
  7.1165 -  apply (simp add: connected_closedin, safe, blast)
  7.1166 -  by (metis Int_lower1 Un_subset_iff closedin_closed subset_antisym)
  7.1167 -
  7.1168 -text \<open>These "transitivity" results are handy too\<close>
  7.1169 -
  7.1170 -lemma openin_trans[trans]:
  7.1171 -  "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T \<Longrightarrow>
  7.1172 -    openin (subtopology euclidean U) S"
  7.1173 -  unfolding open_openin openin_open by blast
  7.1174 -
  7.1175 -lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
  7.1176 -  by (auto simp: openin_open intro: openin_trans)
  7.1177 -
  7.1178 -lemma closedin_trans[trans]:
  7.1179 -  "closedin (subtopology euclidean T) S \<Longrightarrow> closedin (subtopology euclidean U) T \<Longrightarrow>
  7.1180 -    closedin (subtopology euclidean U) S"
  7.1181 -  by (auto simp: closedin_closed closed_Inter Int_assoc)
  7.1182 -
  7.1183 -lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
  7.1184 -  by (auto simp: closedin_closed intro: closedin_trans)
  7.1185 -
  7.1186 -lemma openin_subtopology_Int_subset:
  7.1187 -   "\<lbrakk>openin (subtopology euclidean u) (u \<inter> S); v \<subseteq> u\<rbrakk> \<Longrightarrow> openin (subtopology euclidean v) (v \<inter> S)"
  7.1188 -  by (auto simp: openin_subtopology)
  7.1189 -
  7.1190 -lemma openin_open_eq: "open s \<Longrightarrow> (openin (subtopology euclidean s) t \<longleftrightarrow> open t \<and> t \<subseteq> s)"
  7.1191 -  using open_subset openin_open_trans openin_subset by fastforce
  7.1192 -
  7.1193 -
  7.1194 -subsection \<open>Open and closed balls\<close>
  7.1195 -
  7.1196 -definition%important ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
  7.1197 -  where "ball x e = {y. dist x y < e}"
  7.1198 -
  7.1199 -definition%important cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
  7.1200 -  where "cball x e = {y. dist x y \<le> e}"
  7.1201 -
  7.1202 -definition%important sphere :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
  7.1203 -  where "sphere x e = {y. dist x y = e}"
  7.1204 -
  7.1205 -lemma mem_ball [simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e"
  7.1206 -  by (simp add: ball_def)
  7.1207 -
  7.1208 -lemma mem_cball [simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e"
  7.1209 -  by (simp add: cball_def)
  7.1210 -
  7.1211 -lemma mem_sphere [simp]: "y \<in> sphere x e \<longleftrightarrow> dist x y = e"
  7.1212 -  by (simp add: sphere_def)
  7.1213 -
  7.1214 -lemma ball_trivial [simp]: "ball x 0 = {}"
  7.1215 -  by (simp add: ball_def)
  7.1216 -
  7.1217 -lemma cball_trivial [simp]: "cball x 0 = {x}"
  7.1218 -  by (simp add: cball_def)
  7.1219 -
  7.1220 -lemma sphere_trivial [simp]: "sphere x 0 = {x}"
  7.1221 -  by (simp add: sphere_def)
  7.1222 -
  7.1223 -lemma mem_ball_0 [simp]: "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
  7.1224 -  for x :: "'a::real_normed_vector"
  7.1225 -  by (simp add: dist_norm)
  7.1226 -
  7.1227 -lemma mem_cball_0 [simp]: "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
  7.1228 -  for x :: "'a::real_normed_vector"
  7.1229 -  by (simp add: dist_norm)
  7.1230 -
  7.1231 -lemma disjoint_ballI: "dist x y \<ge> r+s \<Longrightarrow> ball x r \<inter> ball y s = {}"
  7.1232 -  using dist_triangle_less_add not_le by fastforce
  7.1233 -
  7.1234 -lemma disjoint_cballI: "dist x y > r + s \<Longrightarrow> cball x r \<inter> cball y s = {}"
  7.1235 -  by (metis add_mono disjoint_iff_not_equal dist_triangle2 dual_order.trans leD mem_cball)
  7.1236 -
  7.1237 -lemma mem_sphere_0 [simp]: "x \<in> sphere 0 e \<longleftrightarrow> norm x = e"
  7.1238 -  for x :: "'a::real_normed_vector"
  7.1239 -  by (simp add: dist_norm)
  7.1240 -
  7.1241 -lemma sphere_empty [simp]: "r < 0 \<Longrightarrow> sphere a r = {}"
  7.1242 -  for a :: "'a::metric_space"
  7.1243 -  by auto
  7.1244 -
  7.1245 -lemma centre_in_ball [simp]: "x \<in> ball x e \<longleftrightarrow> 0 < e"
  7.1246 -  by simp
  7.1247 -
  7.1248 -lemma centre_in_cball [simp]: "x \<in> cball x e \<longleftrightarrow> 0 \<le> e"
  7.1249 -  by simp
  7.1250 -
  7.1251 -lemma ball_subset_cball [simp, intro]: "ball x e \<subseteq> cball x e"
  7.1252 -  by (simp add: subset_eq)
  7.1253 -
  7.1254 -lemma mem_ball_imp_mem_cball: "x \<in> ball y e \<Longrightarrow> x \<in> cball y e"
  7.1255 -  by (auto simp: mem_ball mem_cball)
  7.1256 -
  7.1257 -lemma sphere_cball [simp,intro]: "sphere z r \<subseteq> cball z r"
  7.1258 -  by force
  7.1259 -
  7.1260 -lemma cball_diff_sphere: "cball a r - sphere a r = ball a r"
  7.1261 -  by auto
  7.1262 -
  7.1263 -lemma subset_ball[intro]: "d \<le> e \<Longrightarrow> ball x d \<subseteq> ball x e"
  7.1264 -  by (simp add: subset_eq)
  7.1265 -
  7.1266 -lemma subset_cball[intro]: "d \<le> e \<Longrightarrow> cball x d \<subseteq> cball x e"
  7.1267 -  by (simp add: subset_eq)
  7.1268 -
  7.1269 -lemma mem_ball_leI: "x \<in> ball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> ball y f"
  7.1270 -  by (auto simp: mem_ball mem_cball)
  7.1271 -
  7.1272 -lemma mem_cball_leI: "x \<in> cball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> cball y f"
  7.1273 -  by (auto simp: mem_ball mem_cball)
  7.1274 -
  7.1275 -lemma cball_trans: "y \<in> cball z b \<Longrightarrow> x \<in> cball y a \<Longrightarrow> x \<in> cball z (b + a)"
  7.1276 -  unfolding mem_cball
  7.1277 -proof -
  7.1278 -  have "dist z x \<le> dist z y + dist y x"
  7.1279 -    by (rule dist_triangle)
  7.1280 -  also assume "dist z y \<le> b"
  7.1281 -  also assume "dist y x \<le> a"
  7.1282 -  finally show "dist z x \<le> b + a" by arith
  7.1283 -qed
  7.1284 -
  7.1285 -lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
  7.1286 -  by (simp add: set_eq_iff) arith
  7.1287 -
  7.1288 -lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
  7.1289 -  by (simp add: set_eq_iff)
  7.1290 -
  7.1291 -lemma cball_max_Un: "cball a (max r s) = cball a r \<union> cball a s"
  7.1292 -  by (simp add: set_eq_iff) arith
  7.1293 -
  7.1294 -lemma cball_min_Int: "cball a (min r s) = cball a r \<inter> cball a s"
  7.1295 -  by (simp add: set_eq_iff)
  7.1296 -
  7.1297 -lemma cball_diff_eq_sphere: "cball a r - ball a r =  sphere a r"
  7.1298 -  by (auto simp: cball_def ball_def dist_commute)
  7.1299 -
  7.1300 -lemma image_add_ball [simp]:
  7.1301 -  fixes a :: "'a::real_normed_vector"
  7.1302 -  shows "(+) b ` ball a r = ball (a+b) r"
  7.1303 -apply (intro equalityI subsetI)
  7.1304 -apply (force simp: dist_norm)
  7.1305 -apply (rule_tac x="x-b" in image_eqI)
  7.1306 -apply (auto simp: dist_norm algebra_simps)
  7.1307 -done
  7.1308 -
  7.1309 -lemma image_add_cball [simp]:
  7.1310 -  fixes a :: "'a::real_normed_vector"
  7.1311 -  shows "(+) b ` cball a r = cball (a+b) r"
  7.1312 -apply (intro equalityI subsetI)
  7.1313 -apply (force simp: dist_norm)
  7.1314 -apply (rule_tac x="x-b" in image_eqI)
  7.1315 -apply (auto simp: dist_norm algebra_simps)
  7.1316 -done
  7.1317 -
  7.1318 -lemma open_ball [intro, simp]: "open (ball x e)"
  7.1319 -proof -
  7.1320 -  have "open (dist x -` {..<e})"
  7.1321 -    by (intro open_vimage open_lessThan continuous_intros)
  7.1322 -  also have "dist x -` {..<e} = ball x e"
  7.1323 -    by auto
  7.1324 -  finally show ?thesis .
  7.1325 -qed
  7.1326 -
  7.1327 -lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
  7.1328 -  by (simp add: open_dist subset_eq mem_ball Ball_def dist_commute)
  7.1329 -
  7.1330 -lemma openI [intro?]: "(\<And>x. x\<in>S \<Longrightarrow> \<exists>e>0. ball x e \<subseteq> S) \<Longrightarrow> open S"
  7.1331 -  by (auto simp: open_contains_ball)
  7.1332 -
  7.1333 -lemma openE[elim?]:
  7.1334 -  assumes "open S" "x\<in>S"
  7.1335 -  obtains e where "e>0" "ball x e \<subseteq> S"
  7.1336 -  using assms unfolding open_contains_ball by auto
  7.1337 -
  7.1338 -lemma open_contains_ball_eq: "open S \<Longrightarrow> x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
  7.1339 -  by (metis open_contains_ball subset_eq centre_in_ball)
  7.1340 -
  7.1341 -lemma openin_contains_ball:
  7.1342 -    "openin (subtopology euclidean t) s \<longleftrightarrow>
  7.1343 -     s \<subseteq> t \<and> (\<forall>x \<in> s. \<exists>e. 0 < e \<and> ball x e \<inter> t \<subseteq> s)"
  7.1344 -    (is "?lhs = ?rhs")
  7.1345 -proof
  7.1346 -  assume ?lhs
  7.1347 -  then show ?rhs
  7.1348 -    apply (simp add: openin_open)
  7.1349 -    apply (metis Int_commute Int_mono inf.cobounded2 open_contains_ball order_refl subsetCE)
  7.1350 -    done
  7.1351 -next
  7.1352 -  assume ?rhs
  7.1353 -  then show ?lhs
  7.1354 -    apply (simp add: openin_euclidean_subtopology_iff)
  7.1355 -    by (metis (no_types) Int_iff dist_commute inf.absorb_iff2 mem_ball)
  7.1356 -qed
  7.1357 -
  7.1358 -lemma openin_contains_cball:
  7.1359 -   "openin (subtopology euclidean t) s \<longleftrightarrow>
  7.1360 -        s \<subseteq> t \<and>
  7.1361 -        (\<forall>x \<in> s. \<exists>e. 0 < e \<and> cball x e \<inter> t \<subseteq> s)"
  7.1362 -apply (simp add: openin_contains_ball)
  7.1363 -apply (rule iffI)
  7.1364 -apply (auto dest!: bspec)
  7.1365 -apply (rule_tac x="e/2" in exI, force+)
  7.1366 -done
  7.1367 -
  7.1368 -lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
  7.1369 -  unfolding mem_ball set_eq_iff
  7.1370 -  apply (simp add: not_less)
  7.1371 -  apply (metis zero_le_dist order_trans dist_self)
  7.1372 -  done
  7.1373 -
  7.1374 -lemma ball_empty: "e \<le> 0 \<Longrightarrow> ball x e = {}" by simp
  7.1375 -
  7.1376 -lemma closed_cball [iff]: "closed (cball x e)"
  7.1377 -proof -
  7.1378 -  have "closed (dist x -` {..e})"
  7.1379 -    by (intro closed_vimage closed_atMost continuous_intros)
  7.1380 -  also have "dist x -` {..e} = cball x e"
  7.1381 -    by auto
  7.1382 -  finally show ?thesis .
  7.1383 -qed
  7.1384 -
  7.1385 -lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
  7.1386 -proof -
  7.1387 -  {
  7.1388 -    fix x and e::real
  7.1389 -    assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
  7.1390 -    then have "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
  7.1391 -  }
  7.1392 -  moreover
  7.1393 -  {
  7.1394 -    fix x and e::real
  7.1395 -    assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
  7.1396 -    then have "\<exists>d>0. ball x d \<subseteq> S"
  7.1397 -      unfolding subset_eq
  7.1398 -      apply (rule_tac x="e/2" in exI, auto)
  7.1399 -      done
  7.1400 -  }
  7.1401 -  ultimately show ?thesis
  7.1402 -    unfolding open_contains_ball by auto
  7.1403 -qed
  7.1404 -
  7.1405 -lemma open_contains_cball_eq: "open S \<Longrightarrow> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
  7.1406 -  by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)
  7.1407 -
  7.1408  lemma euclidean_dist_l2:
  7.1409    fixes x y :: "'a :: euclidean_space"
  7.1410    shows "dist x y = L2_set (\<lambda>i. dist (x \<bullet> i) (y \<bullet> i)) Basis"
  7.1411 @@ -1417,28 +30,6 @@
  7.1412      by (auto intro!: real_le_rsqrt)
  7.1413  qed
  7.1414  
  7.1415 -lemma eventually_nhds_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>x. x \<in> ball z d) (nhds z)"
  7.1416 -  by (rule eventually_nhds_in_open) simp_all
  7.1417 -
  7.1418 -lemma eventually_at_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<in> A) (at z within A)"
  7.1419 -  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
  7.1420 -
  7.1421 -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)"
  7.1422 -  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
  7.1423 -
  7.1424 -lemma at_within_ball: "e > 0 \<Longrightarrow> dist x y < e \<Longrightarrow> at y within ball x e = at y"
  7.1425 -  by (subst at_within_open) auto
  7.1426 -
  7.1427 -lemma atLeastAtMost_eq_cball:
  7.1428 -  fixes a b::real
  7.1429 -  shows "{a .. b} = cball ((a + b)/2) ((b - a)/2)"
  7.1430 -  by (auto simp: dist_real_def field_simps mem_cball)
  7.1431 -
  7.1432 -lemma greaterThanLessThan_eq_ball:
  7.1433 -  fixes a b::real
  7.1434 -  shows "{a <..< b} = ball ((a + b)/2) ((b - a)/2)"
  7.1435 -  by (auto simp: dist_real_def field_simps mem_ball)
  7.1436 -
  7.1437  
  7.1438  subsection \<open>Boxes\<close>
  7.1439  
  7.1440 @@ -1946,6 +537,7 @@
  7.1441      by (auto simp: box_def inner_sum_left inner_Basis sum.If_cases)
  7.1442  qed
  7.1443  
  7.1444 +
  7.1445  subsection \<open>General Intervals\<close>
  7.1446  
  7.1447  definition%important "is_interval (s::('a::euclidean_space) set) \<longleftrightarrow>
  7.1448 @@ -2123,2722 +715,6 @@
  7.1449    using is_interval_translation[of "-c" X]
  7.1450    by (metis image_cong uminus_add_conv_diff)
  7.1451  
  7.1452 -
  7.1453 -subsection \<open>Limit points\<close>
  7.1454 -
  7.1455 -definition%important (in topological_space) islimpt:: "'a \<Rightarrow> 'a set \<Rightarrow> bool"  (infixr "islimpt" 60)
  7.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))"
  7.1457 -
  7.1458 -lemma islimptI:
  7.1459 -  assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
  7.1460 -  shows "x islimpt S"
  7.1461 -  using assms unfolding islimpt_def by auto
  7.1462 -
  7.1463 -lemma islimptE:
  7.1464 -  assumes "x islimpt S" and "x \<in> T" and "open T"
  7.1465 -  obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
  7.1466 -  using assms unfolding islimpt_def by auto
  7.1467 -
  7.1468 -lemma islimpt_iff_eventually: "x islimpt S \<longleftrightarrow> \<not> eventually (\<lambda>y. y \<notin> S) (at x)"
  7.1469 -  unfolding islimpt_def eventually_at_topological by auto
  7.1470 -
  7.1471 -lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> x islimpt T"
  7.1472 -  unfolding islimpt_def by fast
  7.1473 -
  7.1474 -lemma islimpt_approachable:
  7.1475 -  fixes x :: "'a::metric_space"
  7.1476 -  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
  7.1477 -  unfolding islimpt_iff_eventually eventually_at by fast
  7.1478 -
  7.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)"
  7.1480 -  for x :: "'a::metric_space"
  7.1481 -  unfolding islimpt_approachable
  7.1482 -  using approachable_lt_le [where f="\<lambda>y. dist y x" and P="\<lambda>y. y \<notin> S \<or> y = x",
  7.1483 -    THEN arg_cong [where f=Not]]
  7.1484 -  by (simp add: Bex_def conj_commute conj_left_commute)
  7.1485 -
  7.1486 -lemma islimpt_UNIV_iff: "x islimpt UNIV \<longleftrightarrow> \<not> open {x}"
  7.1487 -  unfolding islimpt_def by (safe, fast, case_tac "T = {x}", fast, fast)
  7.1488 -
  7.1489 -lemma islimpt_punctured: "x islimpt S = x islimpt (S-{x})"
  7.1490 -  unfolding islimpt_def by blast
  7.1491 -
  7.1492 -text \<open>A perfect space has no isolated points.\<close>
  7.1493 -
  7.1494 -lemma islimpt_UNIV [simp, intro]: "x islimpt UNIV"
  7.1495 -  for x :: "'a::perfect_space"
  7.1496 -  unfolding islimpt_UNIV_iff by (rule not_open_singleton)
  7.1497 -
  7.1498 -lemma perfect_choose_dist: "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
  7.1499 -  for x :: "'a::{perfect_space,metric_space}"
  7.1500 -  using islimpt_UNIV [of x] by (simp add: islimpt_approachable)
  7.1501 -
  7.1502 -lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
  7.1503 -  unfolding closed_def
  7.1504 -  apply (subst open_subopen)
  7.1505 -  apply (simp add: islimpt_def subset_eq)
  7.1506 -  apply (metis ComplE ComplI)
  7.1507 -  done
  7.1508 -
  7.1509 -lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
  7.1510 -  by (auto simp: islimpt_def)
  7.1511 -
  7.1512 -lemma finite_ball_include:
  7.1513 -  fixes a :: "'a::metric_space"
  7.1514 -  assumes "finite S" 
  7.1515 -  shows "\<exists>e>0. S \<subseteq> ball a e"
  7.1516 -  using assms
  7.1517 -proof induction
  7.1518 -  case (insert x S)
  7.1519 -  then obtain e0 where "e0>0" and e0:"S \<subseteq> ball a e0" by auto
  7.1520 -  define e where "e = max e0 (2 * dist a x)"
  7.1521 -  have "e>0" unfolding e_def using \<open>e0>0\<close> by auto
  7.1522 -  moreover have "insert x S \<subseteq> ball a e"
  7.1523 -    using e0 \<open>e>0\<close> unfolding e_def by auto
  7.1524 -  ultimately show ?case by auto
  7.1525 -qed (auto intro: zero_less_one)
  7.1526 -
  7.1527 -lemma finite_set_avoid:
  7.1528 -  fixes a :: "'a::metric_space"
  7.1529 -  assumes "finite S"
  7.1530 -  shows "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
  7.1531 -  using assms
  7.1532 -proof induction
  7.1533 -  case (insert x S)
  7.1534 -  then obtain d where "d > 0" and d: "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
  7.1535 -    by blast
  7.1536 -  show ?case
  7.1537 -  proof (cases "x = a")
  7.1538 -    case True
  7.1539 -    with \<open>d > 0 \<close>d show ?thesis by auto
  7.1540 -  next
  7.1541 -    case False
  7.1542 -    let ?d = "min d (dist a x)"
  7.1543 -    from False \<open>d > 0\<close> have dp: "?d > 0"
  7.1544 -      by auto
  7.1545 -    from d have d': "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> ?d \<le> dist a x"
  7.1546 -      by auto
  7.1547 -    with dp False show ?thesis
  7.1548 -      by (metis insert_iff le_less min_less_iff_conj not_less)
  7.1549 -  qed
  7.1550 -qed (auto intro: zero_less_one)
  7.1551 -
  7.1552 -lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
  7.1553 -  by (simp add: islimpt_iff_eventually eventually_conj_iff)
  7.1554 -
  7.1555 -lemma discrete_imp_closed:
  7.1556 -  fixes S :: "'a::metric_space set"
  7.1557 -  assumes e: "0 < e"
  7.1558 -    and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
  7.1559 -  shows "closed S"
  7.1560 -proof -
  7.1561 -  have False if C: "\<And>e. e>0 \<Longrightarrow> \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" for x
  7.1562 -  proof -
  7.1563 -    from e have e2: "e/2 > 0" by arith
  7.1564 -    from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y \<noteq> x" "dist y x < e/2"
  7.1565 -      by blast
  7.1566 -    let ?m = "min (e/2) (dist x y) "
  7.1567 -    from e2 y(2) have mp: "?m > 0"
  7.1568 -      by simp
  7.1569 -    from C[OF mp] obtain z where z: "z \<in> S" "z \<noteq> x" "dist z x < ?m"
  7.1570 -      by blast
  7.1571 -    from z y have "dist z y < e"
  7.1572 -      by (intro dist_triangle_lt [where z=x]) simp
  7.1573 -    from d[rule_format, OF y(1) z(1) this] y z show ?thesis
  7.1574 -      by (auto simp: dist_commute)
  7.1575 -  qed
  7.1576 -  then show ?thesis
  7.1577 -    by (metis islimpt_approachable closed_limpt [where 'a='a])
  7.1578 -qed
  7.1579 -
  7.1580 -lemma closed_of_nat_image: "closed (of_nat ` A :: 'a::real_normed_algebra_1 set)"
  7.1581 -  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_nat)
  7.1582 -
  7.1583 -lemma closed_of_int_image: "closed (of_int ` A :: 'a::real_normed_algebra_1 set)"
  7.1584 -  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_int)
  7.1585 -
  7.1586 -lemma closed_Nats [simp]: "closed (\<nat> :: 'a :: real_normed_algebra_1 set)"
  7.1587 -  unfolding Nats_def by (rule closed_of_nat_image)
  7.1588 -
  7.1589 -lemma closed_Ints [simp]: "closed (\<int> :: 'a :: real_normed_algebra_1 set)"
  7.1590 -  unfolding Ints_def by (rule closed_of_int_image)
  7.1591 -
  7.1592 -lemma closed_subset_Ints:
  7.1593 -  fixes A :: "'a :: real_normed_algebra_1 set"
  7.1594 -  assumes "A \<subseteq> \<int>"
  7.1595 -  shows   "closed A"
  7.1596 -proof (intro discrete_imp_closed[OF zero_less_one] ballI impI, goal_cases)
  7.1597 -  case (1 x y)
  7.1598 -  with assms have "x \<in> \<int>" and "y \<in> \<int>" by auto
  7.1599 -  with \<open>dist y x < 1\<close> show "y = x"
  7.1600 -    by (auto elim!: Ints_cases simp: dist_of_int)
  7.1601 -qed
  7.1602 -
  7.1603 -
  7.1604 -subsection \<open>Interior of a Set\<close>
  7.1605 -
  7.1606 -definition%important "interior S = \<Union>{T. open T \<and> T \<subseteq> S}"
  7.1607 -
  7.1608 -lemma interiorI [intro?]:
  7.1609 -  assumes "open T" and "x \<in> T" and "T \<subseteq> S"
  7.1610 -  shows "x \<in> interior S"
  7.1611 -  using assms unfolding interior_def by fast
  7.1612 -
  7.1613 -lemma interiorE [elim?]:
  7.1614 -  assumes "x \<in> interior S"
  7.1615 -  obtains T where "open T" and "x \<in> T" and "T \<subseteq> S"
  7.1616 -  using assms unfolding interior_def by fast
  7.1617 -
  7.1618 -lemma open_interior [simp, intro]: "open (interior S)"
  7.1619 -  by (simp add: interior_def open_Union)
  7.1620 -
  7.1621 -lemma interior_subset: "interior S \<subseteq> S"
  7.1622 -  by (auto simp: interior_def)
  7.1623 -
  7.1624 -lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> interior S"
  7.1625 -  by (auto simp: interior_def)
  7.1626 -
  7.1627 -lemma interior_open: "open S \<Longrightarrow> interior S = S"
  7.1628 -  by (intro equalityI interior_subset interior_maximal subset_refl)
  7.1629 -
  7.1630 -lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
  7.1631 -  by (metis open_interior interior_open)
  7.1632 -
  7.1633 -lemma open_subset_interior: "open S \<Longrightarrow> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
  7.1634 -  by (metis interior_maximal interior_subset subset_trans)
  7.1635 -
  7.1636 -lemma interior_empty [simp]: "interior {} = {}"
  7.1637 -  using open_empty by (rule interior_open)
  7.1638 -
  7.1639 -lemma interior_UNIV [simp]: "interior UNIV = UNIV"
  7.1640 -  using open_UNIV by (rule interior_open)
  7.1641 -
  7.1642 -lemma interior_interior [simp]: "interior (interior S) = interior S"
  7.1643 -  using open_interior by (rule interior_open)
  7.1644 -
  7.1645 -lemma interior_mono: "S \<subseteq> T \<Longrightarrow> interior S \<subseteq> interior T"
  7.1646 -  by (auto simp: interior_def)
  7.1647 -
  7.1648 -lemma interior_unique:
  7.1649 -  assumes "T \<subseteq> S" and "open T"
  7.1650 -  assumes "\<And>T'. T' \<subseteq> S \<Longrightarrow> open T' \<Longrightarrow> T' \<subseteq> T"
  7.1651 -  shows "interior S = T"
  7.1652 -  by (intro equalityI assms interior_subset open_interior interior_maximal)
  7.1653 -
  7.1654 -lemma interior_singleton [simp]: "interior {a} = {}"
  7.1655 -  for a :: "'a::perfect_space"
  7.1656 -  apply (rule interior_unique, simp_all)
  7.1657 -  using not_open_singleton subset_singletonD
  7.1658 -  apply fastforce
  7.1659 -  done
  7.1660 -
  7.1661 -lemma interior_Int [simp]: "interior (S \<inter> T) = interior S \<inter> interior T"
  7.1662 -  by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1
  7.1663 -    Int_lower2 interior_maximal interior_subset open_Int open_interior)
  7.1664 -
  7.1665 -lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
  7.1666 -  using open_contains_ball_eq [where S="interior S"]
  7.1667 -  by (simp add: open_subset_interior)
  7.1668 -
  7.1669 -lemma eventually_nhds_in_nhd: "x \<in> interior s \<Longrightarrow> eventually (\<lambda>y. y \<in> s) (nhds x)"
  7.1670 -  using interior_subset[of s] by (subst eventually_nhds) blast
  7.1671 -
  7.1672 -lemma interior_limit_point [intro]:
  7.1673 -  fixes x :: "'a::perfect_space"
  7.1674 -  assumes x: "x \<in> interior S"
  7.1675 -  shows "x islimpt S"
  7.1676 -  using x islimpt_UNIV [of x]
  7.1677 -  unfolding interior_def islimpt_def
  7.1678 -  apply (clarsimp, rename_tac T T')
  7.1679 -  apply (drule_tac x="T \<inter> T'" in spec)
  7.1680 -  apply (auto simp: open_Int)
  7.1681 -  done
  7.1682 -
  7.1683 -lemma interior_closed_Un_empty_interior:
  7.1684 -  assumes cS: "closed S"
  7.1685 -    and iT: "interior T = {}"
  7.1686 -  shows "interior (S \<union> T) = interior S"
  7.1687 -proof
  7.1688 -  show "interior S \<subseteq> interior (S \<union> T)"
  7.1689 -    by (rule interior_mono) (rule Un_upper1)
  7.1690 -  show "interior (S \<union> T) \<subseteq> interior S"
  7.1691 -  proof
  7.1692 -    fix x
  7.1693 -    assume "x \<in> interior (S \<union> T)"
  7.1694 -    then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T" ..
  7.1695 -    show "x \<in> interior S"
  7.1696 -    proof (rule ccontr)
  7.1697 -      assume "x \<notin> interior S"
  7.1698 -      with \<open>x \<in> R\<close> \<open>open R\<close> obtain y where "y \<in> R - S"
  7.1699 -        unfolding interior_def by fast
  7.1700 -      from \<open>open R\<close> \<open>closed S\<close> have "open (R - S)"
  7.1701 -        by (rule open_Diff)
  7.1702 -      from \<open>R \<subseteq> S \<union> T\<close> have "R - S \<subseteq> T"
  7.1703 -        by fast
  7.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
  7.1705 -        unfolding interior_def by fast
  7.1706 -    qed
  7.1707 -  qed
  7.1708 -qed
  7.1709 -
  7.1710 -lemma interior_Times: "interior (A \<times> B) = interior A \<times> interior B"
  7.1711 -proof (rule interior_unique)
  7.1712 -  show "interior A \<times> interior B \<subseteq> A \<times> B"
  7.1713 -    by (intro Sigma_mono interior_subset)
  7.1714 -  show "open (interior A \<times> interior B)"
  7.1715 -    by (intro open_Times open_interior)
  7.1716 -  fix T
  7.1717 -  assume "T \<subseteq> A \<times> B" and "open T"
  7.1718 -  then show "T \<subseteq> interior A \<times> interior B"
  7.1719 -  proof safe
  7.1720 -    fix x y
  7.1721 -    assume "(x, y) \<in> T"
  7.1722 -    then obtain C D where "open C" "open D" "C \<times> D \<subseteq> T" "x \<in> C" "y \<in> D"
  7.1723 -      using \<open>open T\<close> unfolding open_prod_def by fast
  7.1724 -    then have "open C" "open D" "C \<subseteq> A" "D \<subseteq> B" "x \<in> C" "y \<in> D"
  7.1725 -      using \<open>T \<subseteq> A \<times> B\<close> by auto
  7.1726 -    then show "x \<in> interior A" and "y \<in> interior B"
  7.1727 -      by (auto intro: interiorI)
  7.1728 -  qed
  7.1729 -qed
  7.1730 -
  7.1731 -lemma interior_Ici:
  7.1732 -  fixes x :: "'a :: {dense_linorder,linorder_topology}"
  7.1733 -  assumes "b < x"
  7.1734 -  shows "interior {x ..} = {x <..}"
  7.1735 -proof (rule interior_unique)
  7.1736 -  fix T
  7.1737 -  assume "T \<subseteq> {x ..}" "open T"
  7.1738 -  moreover have "x \<notin> T"
  7.1739 -  proof
  7.1740 -    assume "x \<in> T"
  7.1741 -    obtain y where "y < x" "{y <.. x} \<subseteq> T"
  7.1742 -      using open_left[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>b < x\<close>] by auto
  7.1743 -    with dense[OF \<open>y < x\<close>] obtain z where "z \<in> T" "z < x"
  7.1744 -      by (auto simp: subset_eq Ball_def)
  7.1745 -    with \<open>T \<subseteq> {x ..}\<close> show False by auto
  7.1746 -  qed
  7.1747 -  ultimately show "T \<subseteq> {x <..}"
  7.1748 -    by (auto simp: subset_eq less_le)
  7.1749 -qed auto
  7.1750 -
  7.1751 -lemma interior_Iic:
  7.1752 -  fixes x :: "'a ::{dense_linorder,linorder_topology}"
  7.1753 -  assumes "x < b"
  7.1754 -  shows "interior {.. x} = {..< x}"
  7.1755 -proof (rule interior_unique)
  7.1756 -  fix T
  7.1757 -  assume "T \<subseteq> {.. x}" "open T"
  7.1758 -  moreover have "x \<notin> T&quo