src/HOL/Analysis/Connected.thy
author nipkow
Thu Dec 07 15:48:50 2017 +0100 (4 months ago)
changeset 67155 9e5b05d54f9d
parent 66953 826a5fd4d36c
child 67237 1fe0ec14a90a
permissions -rw-r--r--
canonical name
     1 (*  Author:     L C Paulson, University of Cambridge
     2     Material split off from Topology_Euclidean_Space
     3 *)
     4 
     5 section \<open>Connected Components, Homeomorphisms, Baire property, etc.\<close>
     6 
     7 theory Connected
     8 imports Topology_Euclidean_Space
     9 begin
    10 
    11 subsection \<open>More properties of closed balls, spheres, etc.\<close>
    12 
    13 lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"
    14   apply (simp add: interior_def, safe)
    15   apply (force simp: open_contains_cball)
    16   apply (rule_tac x="ball x e" in exI)
    17   apply (simp add: subset_trans [OF ball_subset_cball])
    18   done
    19 
    20 lemma islimpt_ball:
    21   fixes x y :: "'a::{real_normed_vector,perfect_space}"
    22   shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e"
    23   (is "?lhs \<longleftrightarrow> ?rhs")
    24 proof
    25   show ?rhs if ?lhs
    26   proof
    27     {
    28       assume "e \<le> 0"
    29       then have *: "ball x e = {}"
    30         using ball_eq_empty[of x e] by auto
    31       have False using \<open>?lhs\<close>
    32         unfolding * using islimpt_EMPTY[of y] by auto
    33     }
    34     then show "e > 0" by (metis not_less)
    35     show "y \<in> cball x e"
    36       using closed_cball[of x e] islimpt_subset[of y "ball x e" "cball x e"]
    37         ball_subset_cball[of x e] \<open>?lhs\<close>
    38       unfolding closed_limpt by auto
    39   qed
    40   show ?lhs if ?rhs
    41   proof -
    42     from that have "e > 0" by auto
    43     {
    44       fix d :: real
    45       assume "d > 0"
    46       have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
    47       proof (cases "d \<le> dist x y")
    48         case True
    49         then show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
    50         proof (cases "x = y")
    51           case True
    52           then have False
    53             using \<open>d \<le> dist x y\<close> \<open>d>0\<close> by auto
    54           then show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
    55             by auto
    56         next
    57           case False
    58           have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) =
    59             norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"
    60             unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[symmetric]
    61             by auto
    62           also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"
    63             using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", symmetric, of "y - x"]
    64             unfolding scaleR_minus_left scaleR_one
    65             by (auto simp: norm_minus_commute)
    66           also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"
    67             unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]
    68             unfolding distrib_right using \<open>x\<noteq>y\<close>  by auto
    69           also have "\<dots> \<le> e - d/2" using \<open>d \<le> dist x y\<close> and \<open>d>0\<close> and \<open>?rhs\<close>
    70             by (auto simp: dist_norm)
    71           finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using \<open>d>0\<close>
    72             by auto
    73           moreover
    74           have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"
    75             using \<open>x\<noteq>y\<close>[unfolded dist_nz] \<open>d>0\<close> unfolding scaleR_eq_0_iff
    76             by (auto simp: dist_commute)
    77           moreover
    78           have "dist (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) y < d"
    79             unfolding dist_norm
    80             apply simp
    81             unfolding norm_minus_cancel
    82             using \<open>d > 0\<close> \<open>x\<noteq>y\<close>[unfolded dist_nz] dist_commute[of x y]
    83             unfolding dist_norm
    84             apply auto
    85             done
    86           ultimately show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
    87             apply (rule_tac x = "y - (d / (2*dist y x)) *\<^sub>R (y - x)" in bexI)
    88             apply auto
    89             done
    90         qed
    91       next
    92         case False
    93         then have "d > dist x y" by auto
    94         show "\<exists>x' \<in> ball x e. x' \<noteq> y \<and> dist x' y < d"
    95         proof (cases "x = y")
    96           case True
    97           obtain z where **: "z \<noteq> y" "dist z y < min e d"
    98             using perfect_choose_dist[of "min e d" y]
    99             using \<open>d > 0\<close> \<open>e>0\<close> by auto
   100           show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
   101             unfolding \<open>x = y\<close>
   102             using \<open>z \<noteq> y\<close> **
   103             apply (rule_tac x=z in bexI)
   104             apply (auto simp: dist_commute)
   105             done
   106         next
   107           case False
   108           then show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
   109             using \<open>d>0\<close> \<open>d > dist x y\<close> \<open>?rhs\<close>
   110             apply (rule_tac x=x in bexI, auto)
   111             done
   112         qed
   113       qed
   114     }
   115     then show ?thesis
   116       unfolding mem_cball islimpt_approachable mem_ball by auto
   117   qed
   118 qed
   119 
   120 lemma closure_ball_lemma:
   121   fixes x y :: "'a::real_normed_vector"
   122   assumes "x \<noteq> y"
   123   shows "y islimpt ball x (dist x y)"
   124 proof (rule islimptI)
   125   fix T
   126   assume "y \<in> T" "open T"
   127   then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"
   128     unfolding open_dist by fast
   129   (* choose point between x and y, within distance r of y. *)
   130   define k where "k = min 1 (r / (2 * dist x y))"
   131   define z where "z = y + scaleR k (x - y)"
   132   have z_def2: "z = x + scaleR (1 - k) (y - x)"
   133     unfolding z_def by (simp add: algebra_simps)
   134   have "dist z y < r"
   135     unfolding z_def k_def using \<open>0 < r\<close>
   136     by (simp add: dist_norm min_def)
   137   then have "z \<in> T"
   138     using \<open>\<forall>z. dist z y < r \<longrightarrow> z \<in> T\<close> by simp
   139   have "dist x z < dist x y"
   140     unfolding z_def2 dist_norm
   141     apply (simp add: norm_minus_commute)
   142     apply (simp only: dist_norm [symmetric])
   143     apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)
   144     apply (rule mult_strict_right_mono)
   145     apply (simp add: k_def \<open>0 < r\<close> \<open>x \<noteq> y\<close>)
   146     apply (simp add: \<open>x \<noteq> y\<close>)
   147     done
   148   then have "z \<in> ball x (dist x y)"
   149     by simp
   150   have "z \<noteq> y"
   151     unfolding z_def k_def using \<open>x \<noteq> y\<close> \<open>0 < r\<close>
   152     by (simp add: min_def)
   153   show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"
   154     using \<open>z \<in> ball x (dist x y)\<close> \<open>z \<in> T\<close> \<open>z \<noteq> y\<close>
   155     by fast
   156 qed
   157 
   158 lemma closure_ball [simp]:
   159   fixes x :: "'a::real_normed_vector"
   160   shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"
   161   apply (rule equalityI)
   162   apply (rule closure_minimal)
   163   apply (rule ball_subset_cball)
   164   apply (rule closed_cball)
   165   apply (rule subsetI, rename_tac y)
   166   apply (simp add: le_less [where 'a=real])
   167   apply (erule disjE)
   168   apply (rule subsetD [OF closure_subset], simp)
   169   apply (simp add: closure_def, clarify)
   170   apply (rule closure_ball_lemma)
   171   apply simp
   172   done
   173 
   174 (* In a trivial vector space, this fails for e = 0. *)
   175 lemma interior_cball [simp]:
   176   fixes x :: "'a::{real_normed_vector, perfect_space}"
   177   shows "interior (cball x e) = ball x e"
   178 proof (cases "e \<ge> 0")
   179   case False note cs = this
   180   from cs have null: "ball x e = {}"
   181     using ball_empty[of e x] by auto
   182   moreover
   183   {
   184     fix y
   185     assume "y \<in> cball x e"
   186     then have False
   187       by (metis ball_eq_empty null cs dist_eq_0_iff dist_le_zero_iff empty_subsetI mem_cball subset_antisym subset_ball)
   188   }
   189   then have "cball x e = {}" by auto
   190   then have "interior (cball x e) = {}"
   191     using interior_empty by auto
   192   ultimately show ?thesis by blast
   193 next
   194   case True note cs = this
   195   have "ball x e \<subseteq> cball x e"
   196     using ball_subset_cball by auto
   197   moreover
   198   {
   199     fix S y
   200     assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"
   201     then obtain d where "d>0" and d: "\<forall>x'. dist x' y < d \<longrightarrow> x' \<in> S"
   202       unfolding open_dist by blast
   203     then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"
   204       using perfect_choose_dist [of d] by auto
   205     have "xa \<in> S"
   206       using d[THEN spec[where x = xa]]
   207       using xa by (auto simp: dist_commute)
   208     then have xa_cball: "xa \<in> cball x e"
   209       using as(1) by auto
   210     then have "y \<in> ball x e"
   211     proof (cases "x = y")
   212       case True
   213       then have "e > 0" using cs order.order_iff_strict xa_cball xa_y by fastforce
   214       then show "y \<in> ball x e"
   215         using \<open>x = y \<close> by simp
   216     next
   217       case False
   218       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d"
   219         unfolding dist_norm
   220         using \<open>d>0\<close> norm_ge_zero[of "y - x"] \<open>x \<noteq> y\<close> by auto
   221       then have *: "y + (d / 2 / dist y x) *\<^sub>R (y - x) \<in> cball x e"
   222         using d as(1)[unfolded subset_eq] by blast
   223       have "y - x \<noteq> 0" using \<open>x \<noteq> y\<close> by auto
   224       hence **:"d / (2 * norm (y - x)) > 0"
   225         unfolding zero_less_norm_iff[symmetric] using \<open>d>0\<close> by auto
   226       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) x =
   227         norm (y + (d / (2 * norm (y - x))) *\<^sub>R y - (d / (2 * norm (y - x))) *\<^sub>R x - x)"
   228         by (auto simp: dist_norm algebra_simps)
   229       also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"
   230         by (auto simp: algebra_simps)
   231       also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"
   232         using ** by auto
   233       also have "\<dots> = (dist y x) + d/2"
   234         using ** by (auto simp: distrib_right dist_norm)
   235       finally have "e \<ge> dist x y +d/2"
   236         using *[unfolded mem_cball] by (auto simp: dist_commute)
   237       then show "y \<in> ball x e"
   238         unfolding mem_ball using \<open>d>0\<close> by auto
   239     qed
   240   }
   241   then have "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e"
   242     by auto
   243   ultimately show ?thesis
   244     using interior_unique[of "ball x e" "cball x e"]
   245     using open_ball[of x e]
   246     by auto
   247 qed
   248 
   249 lemma interior_ball [simp]: "interior (ball x e) = ball x e"
   250   by (simp add: interior_open)
   251 
   252 lemma frontier_ball [simp]:
   253   fixes a :: "'a::real_normed_vector"
   254   shows "0 < e \<Longrightarrow> frontier (ball a e) = sphere a e"
   255   by (force simp: frontier_def)
   256 
   257 lemma frontier_cball [simp]:
   258   fixes a :: "'a::{real_normed_vector, perfect_space}"
   259   shows "frontier (cball a e) = sphere a e"
   260   by (force simp: frontier_def)
   261 
   262 lemma cball_eq_empty [simp]: "cball x e = {} \<longleftrightarrow> e < 0"
   263   apply (simp add: set_eq_iff not_le)
   264   apply (metis zero_le_dist dist_self order_less_le_trans)
   265   done
   266 
   267 lemma cball_empty [simp]: "e < 0 \<Longrightarrow> cball x e = {}"
   268   by simp
   269 
   270 lemma cball_eq_sing:
   271   fixes x :: "'a::{metric_space,perfect_space}"
   272   shows "cball x e = {x} \<longleftrightarrow> e = 0"
   273 proof (rule linorder_cases)
   274   assume e: "0 < e"
   275   obtain a where "a \<noteq> x" "dist a x < e"
   276     using perfect_choose_dist [OF e] by auto
   277   then have "a \<noteq> x" "dist x a \<le> e"
   278     by (auto simp: dist_commute)
   279   with e show ?thesis by (auto simp: set_eq_iff)
   280 qed auto
   281 
   282 lemma cball_sing:
   283   fixes x :: "'a::metric_space"
   284   shows "e = 0 \<Longrightarrow> cball x e = {x}"
   285   by (auto simp: set_eq_iff)
   286 
   287 lemma ball_divide_subset: "d \<ge> 1 \<Longrightarrow> ball x (e/d) \<subseteq> ball x e"
   288   apply (cases "e \<le> 0")
   289   apply (simp add: ball_empty divide_simps)
   290   apply (rule subset_ball)
   291   apply (simp add: divide_simps)
   292   done
   293 
   294 lemma ball_divide_subset_numeral: "ball x (e / numeral w) \<subseteq> ball x e"
   295   using ball_divide_subset one_le_numeral by blast
   296 
   297 lemma cball_divide_subset: "d \<ge> 1 \<Longrightarrow> cball x (e/d) \<subseteq> cball x e"
   298   apply (cases "e < 0")
   299   apply (simp add: divide_simps)
   300   apply (rule subset_cball)
   301   apply (metis div_by_1 frac_le not_le order_refl zero_less_one)
   302   done
   303 
   304 lemma cball_divide_subset_numeral: "cball x (e / numeral w) \<subseteq> cball x e"
   305   using cball_divide_subset one_le_numeral by blast
   306 
   307 lemma compact_cball[simp]:
   308   fixes x :: "'a::heine_borel"
   309   shows "compact (cball x e)"
   310   using compact_eq_bounded_closed bounded_cball closed_cball
   311   by blast
   312 
   313 lemma compact_frontier_bounded[intro]:
   314   fixes S :: "'a::heine_borel set"
   315   shows "bounded S \<Longrightarrow> compact (frontier S)"
   316   unfolding frontier_def
   317   using compact_eq_bounded_closed
   318   by blast
   319 
   320 lemma compact_frontier[intro]:
   321   fixes S :: "'a::heine_borel set"
   322   shows "compact S \<Longrightarrow> compact (frontier S)"
   323   using compact_eq_bounded_closed compact_frontier_bounded
   324   by blast
   325 
   326 corollary compact_sphere [simp]:
   327   fixes a :: "'a::{real_normed_vector,perfect_space,heine_borel}"
   328   shows "compact (sphere a r)"
   329 using compact_frontier [of "cball a r"] by simp
   330 
   331 corollary bounded_sphere [simp]:
   332   fixes a :: "'a::{real_normed_vector,perfect_space,heine_borel}"
   333   shows "bounded (sphere a r)"
   334 by (simp add: compact_imp_bounded)
   335 
   336 corollary closed_sphere  [simp]:
   337   fixes a :: "'a::{real_normed_vector,perfect_space,heine_borel}"
   338   shows "closed (sphere a r)"
   339 by (simp add: compact_imp_closed)
   340 
   341 subsection \<open>Connectedness\<close>
   342 
   343 lemma connected_local:
   344  "connected S \<longleftrightarrow>
   345   \<not> (\<exists>e1 e2.
   346       openin (subtopology euclidean S) e1 \<and>
   347       openin (subtopology euclidean S) e2 \<and>
   348       S \<subseteq> e1 \<union> e2 \<and>
   349       e1 \<inter> e2 = {} \<and>
   350       e1 \<noteq> {} \<and>
   351       e2 \<noteq> {})"
   352   unfolding connected_def openin_open
   353   by safe blast+
   354 
   355 lemma exists_diff:
   356   fixes P :: "'a set \<Rightarrow> bool"
   357   shows "(\<exists>S. P (- S)) \<longleftrightarrow> (\<exists>S. P S)"
   358     (is "?lhs \<longleftrightarrow> ?rhs")
   359 proof -
   360   have ?rhs if ?lhs
   361     using that by blast
   362   moreover have "P (- (- S))" if "P S" for S
   363   proof -
   364     have "S = - (- S)" by simp
   365     with that show ?thesis by metis
   366   qed
   367   ultimately show ?thesis by metis
   368 qed
   369 
   370 lemma connected_clopen: "connected S \<longleftrightarrow>
   371   (\<forall>T. openin (subtopology euclidean S) T \<and>
   372      closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")
   373 proof -
   374   have "\<not> connected S \<longleftrightarrow>
   375     (\<exists>e1 e2. open e1 \<and> open (- e2) \<and> S \<subseteq> e1 \<union> (- e2) \<and> e1 \<inter> (- e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (- e2) \<inter> S \<noteq> {})"
   376     unfolding connected_def openin_open closedin_closed
   377     by (metis double_complement)
   378   then have th0: "connected S \<longleftrightarrow>
   379     \<not> (\<exists>e2 e1. closed e2 \<and> open e1 \<and> S \<subseteq> e1 \<union> (- e2) \<and> e1 \<inter> (- e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (- e2) \<inter> S \<noteq> {})"
   380     (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)")
   381     by (simp add: closed_def) metis
   382   have th1: "?rhs \<longleftrightarrow> \<not> (\<exists>t' t. closed t'\<and>t = S\<inter>t' \<and> t\<noteq>{} \<and> t\<noteq>S \<and> (\<exists>t'. open t' \<and> t = S \<inter> t'))"
   383     (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")
   384     unfolding connected_def openin_open closedin_closed by auto
   385   have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" for e2
   386   proof -
   387     have "?P e2 e1 \<longleftrightarrow> (\<exists>t. closed e2 \<and> t = S\<inter>e2 \<and> open e1 \<and> t = S\<inter>e1 \<and> t\<noteq>{} \<and> t \<noteq> S)" for e1
   388       by auto
   389     then show ?thesis
   390       by metis
   391   qed
   392   then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)"
   393     by blast
   394   then show ?thesis
   395     by (simp add: th0 th1)
   396 qed
   397 
   398 lemma connected_linear_image:
   399   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"
   400   assumes "linear f" and "connected s"
   401   shows "connected (f ` s)"
   402 using connected_continuous_image assms linear_continuous_on linear_conv_bounded_linear by blast
   403 
   404 subsection \<open>Connected components, considered as a connectedness relation or a set\<close>
   405 
   406 definition "connected_component s x y \<equiv> \<exists>t. connected t \<and> t \<subseteq> s \<and> x \<in> t \<and> y \<in> t"
   407 
   408 abbreviation "connected_component_set s x \<equiv> Collect (connected_component s x)"
   409 
   410 lemma connected_componentI:
   411   "connected t \<Longrightarrow> t \<subseteq> s \<Longrightarrow> x \<in> t \<Longrightarrow> y \<in> t \<Longrightarrow> connected_component s x y"
   412   by (auto simp: connected_component_def)
   413 
   414 lemma connected_component_in: "connected_component s x y \<Longrightarrow> x \<in> s \<and> y \<in> s"
   415   by (auto simp: connected_component_def)
   416 
   417 lemma connected_component_refl: "x \<in> s \<Longrightarrow> connected_component s x x"
   418   by (auto simp: connected_component_def) (use connected_sing in blast)
   419 
   420 lemma connected_component_refl_eq [simp]: "connected_component s x x \<longleftrightarrow> x \<in> s"
   421   by (auto simp: connected_component_refl) (auto simp: connected_component_def)
   422 
   423 lemma connected_component_sym: "connected_component s x y \<Longrightarrow> connected_component s y x"
   424   by (auto simp: connected_component_def)
   425 
   426 lemma connected_component_trans:
   427   "connected_component s x y \<Longrightarrow> connected_component s y z \<Longrightarrow> connected_component s x z"
   428   unfolding connected_component_def
   429   by (metis Int_iff Un_iff Un_subset_iff equals0D connected_Un)
   430 
   431 lemma connected_component_of_subset:
   432   "connected_component s x y \<Longrightarrow> s \<subseteq> t \<Longrightarrow> connected_component t x y"
   433   by (auto simp: connected_component_def)
   434 
   435 lemma connected_component_Union: "connected_component_set s x = \<Union>{t. connected t \<and> x \<in> t \<and> t \<subseteq> s}"
   436   by (auto simp: connected_component_def)
   437 
   438 lemma connected_connected_component [iff]: "connected (connected_component_set s x)"
   439   by (auto simp: connected_component_Union intro: connected_Union)
   440 
   441 lemma connected_iff_eq_connected_component_set:
   442   "connected s \<longleftrightarrow> (\<forall>x \<in> s. connected_component_set s x = s)"
   443 proof (cases "s = {}")
   444   case True
   445   then show ?thesis by simp
   446 next
   447   case False
   448   then obtain x where "x \<in> s" by auto
   449   show ?thesis
   450   proof
   451     assume "connected s"
   452     then show "\<forall>x \<in> s. connected_component_set s x = s"
   453       by (force simp: connected_component_def)
   454   next
   455     assume "\<forall>x \<in> s. connected_component_set s x = s"
   456     then show "connected s"
   457       by (metis \<open>x \<in> s\<close> connected_connected_component)
   458   qed
   459 qed
   460 
   461 lemma connected_component_subset: "connected_component_set s x \<subseteq> s"
   462   using connected_component_in by blast
   463 
   464 lemma connected_component_eq_self: "connected s \<Longrightarrow> x \<in> s \<Longrightarrow> connected_component_set s x = s"
   465   by (simp add: connected_iff_eq_connected_component_set)
   466 
   467 lemma connected_iff_connected_component:
   468   "connected s \<longleftrightarrow> (\<forall>x \<in> s. \<forall>y \<in> s. connected_component s x y)"
   469   using connected_component_in by (auto simp: connected_iff_eq_connected_component_set)
   470 
   471 lemma connected_component_maximal:
   472   "x \<in> t \<Longrightarrow> connected t \<Longrightarrow> t \<subseteq> s \<Longrightarrow> t \<subseteq> (connected_component_set s x)"
   473   using connected_component_eq_self connected_component_of_subset by blast
   474 
   475 lemma connected_component_mono:
   476   "s \<subseteq> t \<Longrightarrow> connected_component_set s x \<subseteq> connected_component_set t x"
   477   by (simp add: Collect_mono connected_component_of_subset)
   478 
   479 lemma connected_component_eq_empty [simp]: "connected_component_set s x = {} \<longleftrightarrow> x \<notin> s"
   480   using connected_component_refl by (fastforce simp: connected_component_in)
   481 
   482 lemma connected_component_set_empty [simp]: "connected_component_set {} x = {}"
   483   using connected_component_eq_empty by blast
   484 
   485 lemma connected_component_eq:
   486   "y \<in> connected_component_set s x \<Longrightarrow> (connected_component_set s y = connected_component_set s x)"
   487   by (metis (no_types, lifting)
   488       Collect_cong connected_component_sym connected_component_trans mem_Collect_eq)
   489 
   490 lemma closed_connected_component:
   491   assumes s: "closed s"
   492   shows "closed (connected_component_set s x)"
   493 proof (cases "x \<in> s")
   494   case False
   495   then show ?thesis
   496     by (metis connected_component_eq_empty closed_empty)
   497 next
   498   case True
   499   show ?thesis
   500     unfolding closure_eq [symmetric]
   501   proof
   502     show "closure (connected_component_set s x) \<subseteq> connected_component_set s x"
   503       apply (rule connected_component_maximal)
   504         apply (simp add: closure_def True)
   505        apply (simp add: connected_imp_connected_closure)
   506       apply (simp add: s closure_minimal connected_component_subset)
   507       done
   508   next
   509     show "connected_component_set s x \<subseteq> closure (connected_component_set s x)"
   510       by (simp add: closure_subset)
   511   qed
   512 qed
   513 
   514 lemma connected_component_disjoint:
   515   "connected_component_set s a \<inter> connected_component_set s b = {} \<longleftrightarrow>
   516     a \<notin> connected_component_set s b"
   517   apply (auto simp: connected_component_eq)
   518   using connected_component_eq connected_component_sym
   519   apply blast
   520   done
   521 
   522 lemma connected_component_nonoverlap:
   523   "connected_component_set s a \<inter> connected_component_set s b = {} \<longleftrightarrow>
   524     a \<notin> s \<or> b \<notin> s \<or> connected_component_set s a \<noteq> connected_component_set s b"
   525   apply (auto simp: connected_component_in)
   526   using connected_component_refl_eq
   527     apply blast
   528    apply (metis connected_component_eq mem_Collect_eq)
   529   apply (metis connected_component_eq mem_Collect_eq)
   530   done
   531 
   532 lemma connected_component_overlap:
   533   "connected_component_set s a \<inter> connected_component_set s b \<noteq> {} \<longleftrightarrow>
   534     a \<in> s \<and> b \<in> s \<and> connected_component_set s a = connected_component_set s b"
   535   by (auto simp: connected_component_nonoverlap)
   536 
   537 lemma connected_component_sym_eq: "connected_component s x y \<longleftrightarrow> connected_component s y x"
   538   using connected_component_sym by blast
   539 
   540 lemma connected_component_eq_eq:
   541   "connected_component_set s x = connected_component_set s y \<longleftrightarrow>
   542     x \<notin> s \<and> y \<notin> s \<or> x \<in> s \<and> y \<in> s \<and> connected_component s x y"
   543   apply (cases "y \<in> s", simp)
   544    apply (metis connected_component_eq connected_component_eq_empty connected_component_refl_eq mem_Collect_eq)
   545   apply (cases "x \<in> s", simp)
   546    apply (metis connected_component_eq_empty)
   547   using connected_component_eq_empty
   548   apply blast
   549   done
   550 
   551 lemma connected_iff_connected_component_eq:
   552   "connected s \<longleftrightarrow> (\<forall>x \<in> s. \<forall>y \<in> s. connected_component_set s x = connected_component_set s y)"
   553   by (simp add: connected_component_eq_eq connected_iff_connected_component)
   554 
   555 lemma connected_component_idemp:
   556   "connected_component_set (connected_component_set s x) x = connected_component_set s x"
   557   apply (rule subset_antisym)
   558    apply (simp add: connected_component_subset)
   559   apply (metis connected_component_eq_empty connected_component_maximal
   560       connected_component_refl_eq connected_connected_component mem_Collect_eq set_eq_subset)
   561   done
   562 
   563 lemma connected_component_unique:
   564   "\<lbrakk>x \<in> c; c \<subseteq> s; connected c;
   565     \<And>c'. x \<in> c' \<and> c' \<subseteq> s \<and> connected c'
   566               \<Longrightarrow> c' \<subseteq> c\<rbrakk>
   567         \<Longrightarrow> connected_component_set s x = c"
   568 apply (rule subset_antisym)
   569 apply (meson connected_component_maximal connected_component_subset connected_connected_component contra_subsetD)
   570 by (simp add: connected_component_maximal)
   571 
   572 lemma joinable_connected_component_eq:
   573   "\<lbrakk>connected t; t \<subseteq> s;
   574     connected_component_set s x \<inter> t \<noteq> {};
   575     connected_component_set s y \<inter> t \<noteq> {}\<rbrakk>
   576     \<Longrightarrow> connected_component_set s x = connected_component_set s y"
   577 apply (simp add: ex_in_conv [symmetric])
   578 apply (rule connected_component_eq)
   579 by (metis (no_types, hide_lams) connected_component_eq_eq connected_component_in connected_component_maximal subsetD mem_Collect_eq)
   580 
   581 
   582 lemma Union_connected_component: "\<Union>(connected_component_set s ` s) = s"
   583   apply (rule subset_antisym)
   584   apply (simp add: SUP_least connected_component_subset)
   585   using connected_component_refl_eq
   586   by force
   587 
   588 
   589 lemma complement_connected_component_unions:
   590     "s - connected_component_set s x =
   591      \<Union>(connected_component_set s ` s - {connected_component_set s x})"
   592   apply (subst Union_connected_component [symmetric], auto)
   593   apply (metis connected_component_eq_eq connected_component_in)
   594   by (metis connected_component_eq mem_Collect_eq)
   595 
   596 lemma connected_component_intermediate_subset:
   597         "\<lbrakk>connected_component_set u a \<subseteq> t; t \<subseteq> u\<rbrakk>
   598         \<Longrightarrow> connected_component_set t a = connected_component_set u a"
   599   apply (case_tac "a \<in> u")
   600   apply (simp add: connected_component_maximal connected_component_mono subset_antisym)
   601   using connected_component_eq_empty by blast
   602 
   603 proposition connected_Times:
   604   assumes S: "connected S" and T: "connected T"
   605   shows "connected (S \<times> T)"
   606 proof (clarsimp simp add: connected_iff_connected_component)
   607   fix x y x' y'
   608   assume xy: "x \<in> S" "y \<in> T" "x' \<in> S" "y' \<in> T"
   609   with xy obtain U V where U: "connected U" "U \<subseteq> S" "x \<in> U" "x' \<in> U"
   610                        and V: "connected V" "V \<subseteq> T" "y \<in> V" "y' \<in> V"
   611     using S T \<open>x \<in> S\<close> \<open>x' \<in> S\<close> by blast+
   612   show "connected_component (S \<times> T) (x, y) (x', y')"
   613     unfolding connected_component_def
   614   proof (intro exI conjI)
   615     show "connected ((\<lambda>x. (x, y)) ` U \<union> Pair x' ` V)"
   616     proof (rule connected_Un)
   617       have "continuous_on U (\<lambda>x. (x, y))"
   618         by (intro continuous_intros)
   619       then show "connected ((\<lambda>x. (x, y)) ` U)"
   620         by (rule connected_continuous_image) (rule \<open>connected U\<close>)
   621       have "continuous_on V (Pair x')"
   622         by (intro continuous_intros)
   623       then show "connected (Pair x' ` V)"
   624         by (rule connected_continuous_image) (rule \<open>connected V\<close>)
   625     qed (use U V in auto)
   626   qed (use U V in auto)
   627 qed
   628 
   629 corollary connected_Times_eq [simp]:
   630    "connected (S \<times> T) \<longleftrightarrow> S = {} \<or> T = {} \<or> connected S \<and> connected T"  (is "?lhs = ?rhs")
   631 proof
   632   assume L: ?lhs
   633   show ?rhs
   634   proof (cases "S = {} \<or> T = {}")
   635     case True
   636     then show ?thesis by auto
   637   next
   638     case False
   639     have "connected (fst ` (S \<times> T))" "connected (snd ` (S \<times> T))"
   640       using continuous_on_fst continuous_on_snd continuous_on_id
   641       by (blast intro: connected_continuous_image [OF _ L])+
   642     with False show ?thesis
   643       by auto
   644   qed
   645 next
   646   assume ?rhs
   647   then show ?lhs
   648     by (auto simp: connected_Times)
   649 qed
   650 
   651 
   652 subsection \<open>The set of connected components of a set\<close>
   653 
   654 definition components:: "'a::topological_space set \<Rightarrow> 'a set set"
   655   where "components s \<equiv> connected_component_set s ` s"
   656 
   657 lemma components_iff: "s \<in> components u \<longleftrightarrow> (\<exists>x. x \<in> u \<and> s = connected_component_set u x)"
   658   by (auto simp: components_def)
   659 
   660 lemma componentsI: "x \<in> u \<Longrightarrow> connected_component_set u x \<in> components u"
   661   by (auto simp: components_def)
   662 
   663 lemma componentsE:
   664   assumes "s \<in> components u"
   665   obtains x where "x \<in> u" "s = connected_component_set u x"
   666   using assms by (auto simp: components_def)
   667 
   668 lemma Union_components [simp]: "\<Union>(components u) = u"
   669   apply (rule subset_antisym)
   670   using Union_connected_component components_def apply fastforce
   671   apply (metis Union_connected_component components_def set_eq_subset)
   672   done
   673 
   674 lemma pairwise_disjoint_components: "pairwise (\<lambda>X Y. X \<inter> Y = {}) (components u)"
   675   apply (simp add: pairwise_def)
   676   apply (auto simp: components_iff)
   677   apply (metis connected_component_eq_eq connected_component_in)+
   678   done
   679 
   680 lemma in_components_nonempty: "c \<in> components s \<Longrightarrow> c \<noteq> {}"
   681     by (metis components_iff connected_component_eq_empty)
   682 
   683 lemma in_components_subset: "c \<in> components s \<Longrightarrow> c \<subseteq> s"
   684   using Union_components by blast
   685 
   686 lemma in_components_connected: "c \<in> components s \<Longrightarrow> connected c"
   687   by (metis components_iff connected_connected_component)
   688 
   689 lemma in_components_maximal:
   690   "c \<in> components s \<longleftrightarrow>
   691     c \<noteq> {} \<and> c \<subseteq> s \<and> connected c \<and> (\<forall>d. d \<noteq> {} \<and> c \<subseteq> d \<and> d \<subseteq> s \<and> connected d \<longrightarrow> d = c)"
   692   apply (rule iffI)
   693    apply (simp add: in_components_nonempty in_components_connected)
   694    apply (metis (full_types) components_iff connected_component_eq_self connected_component_intermediate_subset connected_component_refl in_components_subset mem_Collect_eq rev_subsetD)
   695   apply (metis bot.extremum_uniqueI components_iff connected_component_eq_empty connected_component_maximal connected_component_subset connected_connected_component subset_emptyI)
   696   done
   697 
   698 lemma joinable_components_eq:
   699   "connected t \<and> t \<subseteq> s \<and> c1 \<in> components s \<and> c2 \<in> components s \<and> c1 \<inter> t \<noteq> {} \<and> c2 \<inter> t \<noteq> {} \<Longrightarrow> c1 = c2"
   700   by (metis (full_types) components_iff joinable_connected_component_eq)
   701 
   702 lemma closed_components: "\<lbrakk>closed s; c \<in> components s\<rbrakk> \<Longrightarrow> closed c"
   703   by (metis closed_connected_component components_iff)
   704 
   705 lemma compact_components:
   706   fixes s :: "'a::heine_borel set"
   707   shows "\<lbrakk>compact s; c \<in> components s\<rbrakk> \<Longrightarrow> compact c"
   708 by (meson bounded_subset closed_components in_components_subset compact_eq_bounded_closed)
   709 
   710 lemma components_nonoverlap:
   711     "\<lbrakk>c \<in> components s; c' \<in> components s\<rbrakk> \<Longrightarrow> (c \<inter> c' = {}) \<longleftrightarrow> (c \<noteq> c')"
   712   apply (auto simp: in_components_nonempty components_iff)
   713     using connected_component_refl apply blast
   714    apply (metis connected_component_eq_eq connected_component_in)
   715   by (metis connected_component_eq mem_Collect_eq)
   716 
   717 lemma components_eq: "\<lbrakk>c \<in> components s; c' \<in> components s\<rbrakk> \<Longrightarrow> (c = c' \<longleftrightarrow> c \<inter> c' \<noteq> {})"
   718   by (metis components_nonoverlap)
   719 
   720 lemma components_eq_empty [simp]: "components s = {} \<longleftrightarrow> s = {}"
   721   by (simp add: components_def)
   722 
   723 lemma components_empty [simp]: "components {} = {}"
   724   by simp
   725 
   726 lemma connected_eq_connected_components_eq: "connected s \<longleftrightarrow> (\<forall>c \<in> components s. \<forall>c' \<in> components s. c = c')"
   727   by (metis (no_types, hide_lams) components_iff connected_component_eq_eq connected_iff_connected_component)
   728 
   729 lemma components_eq_sing_iff: "components s = {s} \<longleftrightarrow> connected s \<and> s \<noteq> {}"
   730   apply (rule iffI)
   731   using in_components_connected apply fastforce
   732   apply safe
   733   using Union_components apply fastforce
   734    apply (metis components_iff connected_component_eq_self)
   735   using in_components_maximal
   736   apply auto
   737   done
   738 
   739 lemma components_eq_sing_exists: "(\<exists>a. components s = {a}) \<longleftrightarrow> connected s \<and> s \<noteq> {}"
   740   apply (rule iffI)
   741   using connected_eq_connected_components_eq apply fastforce
   742   apply (metis components_eq_sing_iff)
   743   done
   744 
   745 lemma connected_eq_components_subset_sing: "connected s \<longleftrightarrow> components s \<subseteq> {s}"
   746   by (metis Union_components components_empty components_eq_sing_iff connected_empty insert_subset order_refl subset_singletonD)
   747 
   748 lemma connected_eq_components_subset_sing_exists: "connected s \<longleftrightarrow> (\<exists>a. components s \<subseteq> {a})"
   749   by (metis components_eq_sing_exists connected_eq_components_subset_sing empty_iff subset_iff subset_singletonD)
   750 
   751 lemma in_components_self: "s \<in> components s \<longleftrightarrow> connected s \<and> s \<noteq> {}"
   752   by (metis components_empty components_eq_sing_iff empty_iff in_components_connected insertI1)
   753 
   754 lemma components_maximal: "\<lbrakk>c \<in> components s; connected t; t \<subseteq> s; c \<inter> t \<noteq> {}\<rbrakk> \<Longrightarrow> t \<subseteq> c"
   755   apply (simp add: components_def ex_in_conv [symmetric], clarify)
   756   by (meson connected_component_def connected_component_trans)
   757 
   758 lemma exists_component_superset: "\<lbrakk>t \<subseteq> s; s \<noteq> {}; connected t\<rbrakk> \<Longrightarrow> \<exists>c. c \<in> components s \<and> t \<subseteq> c"
   759   apply (cases "t = {}", force)
   760   apply (metis components_def ex_in_conv connected_component_maximal contra_subsetD image_eqI)
   761   done
   762 
   763 lemma components_intermediate_subset: "\<lbrakk>s \<in> components u; s \<subseteq> t; t \<subseteq> u\<rbrakk> \<Longrightarrow> s \<in> components t"
   764   apply (auto simp: components_iff)
   765   apply (metis connected_component_eq_empty connected_component_intermediate_subset)
   766   done
   767 
   768 lemma in_components_unions_complement: "c \<in> components s \<Longrightarrow> s - c = \<Union>(components s - {c})"
   769   by (metis complement_connected_component_unions components_def components_iff)
   770 
   771 lemma connected_intermediate_closure:
   772   assumes cs: "connected s" and st: "s \<subseteq> t" and ts: "t \<subseteq> closure s"
   773   shows "connected t"
   774 proof (rule connectedI)
   775   fix A B
   776   assume A: "open A" and B: "open B" and Alap: "A \<inter> t \<noteq> {}" and Blap: "B \<inter> t \<noteq> {}"
   777     and disj: "A \<inter> B \<inter> t = {}" and cover: "t \<subseteq> A \<union> B"
   778   have disjs: "A \<inter> B \<inter> s = {}"
   779     using disj st by auto
   780   have "A \<inter> closure s \<noteq> {}"
   781     using Alap Int_absorb1 ts by blast
   782   then have Alaps: "A \<inter> s \<noteq> {}"
   783     by (simp add: A open_Int_closure_eq_empty)
   784   have "B \<inter> closure s \<noteq> {}"
   785     using Blap Int_absorb1 ts by blast
   786   then have Blaps: "B \<inter> s \<noteq> {}"
   787     by (simp add: B open_Int_closure_eq_empty)
   788   then show False
   789     using cs [unfolded connected_def] A B disjs Alaps Blaps cover st
   790     by blast
   791 qed
   792 
   793 lemma closedin_connected_component: "closedin (subtopology euclidean s) (connected_component_set s x)"
   794 proof (cases "connected_component_set s x = {}")
   795   case True
   796   then show ?thesis
   797     by (metis closedin_empty)
   798 next
   799   case False
   800   then obtain y where y: "connected_component s x y"
   801     by blast
   802   have *: "connected_component_set s x \<subseteq> s \<inter> closure (connected_component_set s x)"
   803     by (auto simp: closure_def connected_component_in)
   804   have "connected_component s x y \<Longrightarrow> s \<inter> closure (connected_component_set s x) \<subseteq> connected_component_set s x"
   805     apply (rule connected_component_maximal, simp)
   806     using closure_subset connected_component_in apply fastforce
   807     using * connected_intermediate_closure apply blast+
   808     done
   809   with y * show ?thesis
   810     by (auto simp: closedin_closed)
   811 qed
   812 
   813 lemma closedin_component:
   814    "C \<in> components s \<Longrightarrow> closedin (subtopology euclidean s) C"
   815   using closedin_connected_component componentsE by blast
   816 
   817 
   818 subsection \<open>Intersecting chains of compact sets and the Baire property\<close>
   819 
   820 proposition bounded_closed_chain:
   821   fixes \<F> :: "'a::heine_borel set set"
   822   assumes "B \<in> \<F>" "bounded B" and \<F>: "\<And>S. S \<in> \<F> \<Longrightarrow> closed S" and "{} \<notin> \<F>"
   823       and chain: "\<And>S T. S \<in> \<F> \<and> T \<in> \<F> \<Longrightarrow> S \<subseteq> T \<or> T \<subseteq> S"
   824     shows "\<Inter>\<F> \<noteq> {}"
   825 proof -
   826   have "B \<inter> \<Inter>\<F> \<noteq> {}"
   827   proof (rule compact_imp_fip)
   828     show "compact B" "\<And>T. T \<in> \<F> \<Longrightarrow> closed T"
   829       by (simp_all add: assms compact_eq_bounded_closed)
   830     show "\<lbrakk>finite \<G>; \<G> \<subseteq> \<F>\<rbrakk> \<Longrightarrow> B \<inter> \<Inter>\<G> \<noteq> {}" for \<G>
   831     proof (induction \<G> rule: finite_induct)
   832       case empty
   833       with assms show ?case by force
   834     next
   835       case (insert U \<G>)
   836       then have "U \<in> \<F>" and ne: "B \<inter> \<Inter>\<G> \<noteq> {}" by auto
   837       then consider "B \<subseteq> U" | "U \<subseteq> B"
   838           using \<open>B \<in> \<F>\<close> chain by blast
   839         then show ?case
   840         proof cases
   841           case 1
   842           then show ?thesis
   843             using Int_left_commute ne by auto
   844         next
   845           case 2
   846           have "U \<noteq> {}"
   847             using \<open>U \<in> \<F>\<close> \<open>{} \<notin> \<F>\<close> by blast
   848           moreover
   849           have False if "\<And>x. x \<in> U \<Longrightarrow> \<exists>Y\<in>\<G>. x \<notin> Y"
   850           proof -
   851             have "\<And>x. x \<in> U \<Longrightarrow> \<exists>Y\<in>\<G>. Y \<subseteq> U"
   852               by (metis chain contra_subsetD insert.prems insert_subset that)
   853             then obtain Y where "Y \<in> \<G>" "Y \<subseteq> U"
   854               by (metis all_not_in_conv \<open>U \<noteq> {}\<close>)
   855             moreover obtain x where "x \<in> \<Inter>\<G>"
   856               by (metis Int_emptyI ne)
   857             ultimately show ?thesis
   858               by (metis Inf_lower subset_eq that)
   859           qed
   860           with 2 show ?thesis
   861             by blast
   862         qed
   863       qed
   864   qed
   865   then show ?thesis by blast
   866 qed
   867 
   868 corollary compact_chain:
   869   fixes \<F> :: "'a::heine_borel set set"
   870   assumes "\<And>S. S \<in> \<F> \<Longrightarrow> compact S" "{} \<notin> \<F>"
   871           "\<And>S T. S \<in> \<F> \<and> T \<in> \<F> \<Longrightarrow> S \<subseteq> T \<or> T \<subseteq> S"
   872     shows "\<Inter> \<F> \<noteq> {}"
   873 proof (cases "\<F> = {}")
   874   case True
   875   then show ?thesis by auto
   876 next
   877   case False
   878   show ?thesis
   879     by (metis False all_not_in_conv assms compact_imp_bounded compact_imp_closed bounded_closed_chain)
   880 qed
   881 
   882 lemma compact_nest:
   883   fixes F :: "'a::linorder \<Rightarrow> 'b::heine_borel set"
   884   assumes F: "\<And>n. compact(F n)" "\<And>n. F n \<noteq> {}" and mono: "\<And>m n. m \<le> n \<Longrightarrow> F n \<subseteq> F m"
   885   shows "\<Inter>range F \<noteq> {}"
   886 proof -
   887   have *: "\<And>S T. S \<in> range F \<and> T \<in> range F \<Longrightarrow> S \<subseteq> T \<or> T \<subseteq> S"
   888     by (metis mono image_iff le_cases)
   889   show ?thesis
   890     apply (rule compact_chain [OF _ _ *])
   891     using F apply (blast intro: dest: *)+
   892     done
   893 qed
   894 
   895 text\<open>The Baire property of dense sets\<close>
   896 theorem Baire:
   897   fixes S::"'a::{real_normed_vector,heine_borel} set"
   898   assumes "closed S" "countable \<G>"
   899       and ope: "\<And>T. T \<in> \<G> \<Longrightarrow> openin (subtopology euclidean S) T \<and> S \<subseteq> closure T"
   900  shows "S \<subseteq> closure(\<Inter>\<G>)"
   901 proof (cases "\<G> = {}")
   902   case True
   903   then show ?thesis
   904     using closure_subset by auto
   905 next
   906   let ?g = "from_nat_into \<G>"
   907   case False
   908   then have gin: "?g n \<in> \<G>" for n
   909     by (simp add: from_nat_into)
   910   show ?thesis
   911   proof (clarsimp simp: closure_approachable)
   912     fix x and e::real
   913     assume "x \<in> S" "0 < e"
   914     obtain TF where opeF: "\<And>n. openin (subtopology euclidean S) (TF n)"
   915                and ne: "\<And>n. TF n \<noteq> {}"
   916                and subg: "\<And>n. S \<inter> closure(TF n) \<subseteq> ?g n"
   917                and subball: "\<And>n. closure(TF n) \<subseteq> ball x e"
   918                and decr: "\<And>n. TF(Suc n) \<subseteq> TF n"
   919     proof -
   920       have *: "\<exists>Y. (openin (subtopology euclidean S) Y \<and> Y \<noteq> {} \<and>
   921                    S \<inter> closure Y \<subseteq> ?g n \<and> closure Y \<subseteq> ball x e) \<and> Y \<subseteq> U"
   922         if opeU: "openin (subtopology euclidean S) U" and "U \<noteq> {}" and cloU: "closure U \<subseteq> ball x e" for U n
   923       proof -
   924         obtain T where T: "open T" "U = T \<inter> S"
   925           using \<open>openin (subtopology euclidean S) U\<close> by (auto simp: openin_subtopology)
   926         with \<open>U \<noteq> {}\<close> have "T \<inter> closure (?g n) \<noteq> {}"
   927           using gin ope by fastforce
   928         then have "T \<inter> ?g n \<noteq> {}"
   929           using \<open>open T\<close> open_Int_closure_eq_empty by blast
   930         then obtain y where "y \<in> U" "y \<in> ?g n"
   931           using T ope [of "?g n", OF gin] by (blast dest:  openin_imp_subset)
   932         moreover have "openin (subtopology euclidean S) (U \<inter> ?g n)"
   933           using gin ope opeU by blast
   934         ultimately obtain d where U: "U \<inter> ?g n \<subseteq> S" and "d > 0" and d: "ball y d \<inter> S \<subseteq> U \<inter> ?g n"
   935           by (force simp: openin_contains_ball)
   936         show ?thesis
   937         proof (intro exI conjI)
   938           show "openin (subtopology euclidean S) (S \<inter> ball y (d/2))"
   939             by (simp add: openin_open_Int)
   940           show "S \<inter> ball y (d/2) \<noteq> {}"
   941             using \<open>0 < d\<close> \<open>y \<in> U\<close> opeU openin_imp_subset by fastforce
   942           have "S \<inter> closure (S \<inter> ball y (d/2)) \<subseteq> S \<inter> closure (ball y (d/2))"
   943             using closure_mono by blast
   944           also have "... \<subseteq> ?g n"
   945             using \<open>d > 0\<close> d by force
   946           finally show "S \<inter> closure (S \<inter> ball y (d/2)) \<subseteq> ?g n" .
   947           have "closure (S \<inter> ball y (d/2)) \<subseteq> S \<inter> ball y d"
   948           proof -
   949             have "closure (ball y (d/2)) \<subseteq> ball y d"
   950               using \<open>d > 0\<close> by auto
   951             then have "closure (S \<inter> ball y (d/2)) \<subseteq> ball y d"
   952               by (meson closure_mono inf.cobounded2 subset_trans)
   953             then show ?thesis
   954               by (simp add: \<open>closed S\<close> closure_minimal)
   955           qed
   956           also have "...  \<subseteq> ball x e"
   957             using cloU closure_subset d by blast
   958           finally show "closure (S \<inter> ball y (d/2)) \<subseteq> ball x e" .
   959           show "S \<inter> ball y (d/2) \<subseteq> U"
   960             using ball_divide_subset_numeral d by blast
   961         qed
   962       qed
   963       let ?\<Phi> = "\<lambda>n X. openin (subtopology euclidean S) X \<and> X \<noteq> {} \<and>
   964                       S \<inter> closure X \<subseteq> ?g n \<and> closure X \<subseteq> ball x e"
   965       have "closure (S \<inter> ball x (e / 2)) \<subseteq> closure(ball x (e/2))"
   966         by (simp add: closure_mono)
   967       also have "...  \<subseteq> ball x e"
   968         using \<open>e > 0\<close> by auto
   969       finally have "closure (S \<inter> ball x (e / 2)) \<subseteq> ball x e" .
   970       moreover have"openin (subtopology euclidean S) (S \<inter> ball x (e / 2))" "S \<inter> ball x (e / 2) \<noteq> {}"
   971         using \<open>0 < e\<close> \<open>x \<in> S\<close> by auto
   972       ultimately obtain Y where Y: "?\<Phi> 0 Y \<and> Y \<subseteq> S \<inter> ball x (e / 2)"
   973             using * [of "S \<inter> ball x (e/2)" 0] by metis
   974       show thesis
   975       proof (rule exE [OF dependent_nat_choice [of ?\<Phi> "\<lambda>n X Y. Y \<subseteq> X"]])
   976         show "\<exists>x. ?\<Phi> 0 x"
   977           using Y by auto
   978         show "\<exists>Y. ?\<Phi> (Suc n) Y \<and> Y \<subseteq> X" if "?\<Phi> n X" for X n
   979           using that by (blast intro: *)
   980       qed (use that in metis)
   981     qed
   982     have "(\<Inter>n. S \<inter> closure (TF n)) \<noteq> {}"
   983     proof (rule compact_nest)
   984       show "\<And>n. compact (S \<inter> closure (TF n))"
   985         by (metis closed_closure subball bounded_subset_ballI compact_eq_bounded_closed closed_Int_compact [OF \<open>closed S\<close>])
   986       show "\<And>n. S \<inter> closure (TF n) \<noteq> {}"
   987         by (metis Int_absorb1 opeF \<open>closed S\<close> closure_eq_empty closure_minimal ne openin_imp_subset)
   988       show "\<And>m n. m \<le> n \<Longrightarrow> S \<inter> closure (TF n) \<subseteq> S \<inter> closure (TF m)"
   989         by (meson closure_mono decr dual_order.refl inf_mono lift_Suc_antimono_le)
   990     qed
   991     moreover have "(\<Inter>n. S \<inter> closure (TF n)) \<subseteq> {y \<in> \<Inter>\<G>. dist y x < e}"
   992     proof (clarsimp, intro conjI)
   993       fix y
   994       assume "y \<in> S" and y: "\<forall>n. y \<in> closure (TF n)"
   995       then show "\<forall>T\<in>\<G>. y \<in> T"
   996         by (metis Int_iff from_nat_into_surj [OF \<open>countable \<G>\<close>] set_mp subg)
   997       show "dist y x < e"
   998         by (metis y dist_commute mem_ball subball subsetCE)
   999     qed
  1000     ultimately show "\<exists>y \<in> \<Inter>\<G>. dist y x < e"
  1001       by auto
  1002   qed
  1003 qed
  1004 
  1005 subsection\<open>Some theorems on sups and infs using the notion "bounded".\<close>
  1006 
  1007 lemma bounded_real: "bounded (S::real set) \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. \<bar>x\<bar> \<le> a)"
  1008   by (simp add: bounded_iff)
  1009 
  1010 lemma bounded_imp_bdd_above: "bounded S \<Longrightarrow> bdd_above (S :: real set)"
  1011   by (auto simp: bounded_def bdd_above_def dist_real_def)
  1012      (metis abs_le_D1 abs_minus_commute diff_le_eq)
  1013 
  1014 lemma bounded_imp_bdd_below: "bounded S \<Longrightarrow> bdd_below (S :: real set)"
  1015   by (auto simp: bounded_def bdd_below_def dist_real_def)
  1016      (metis abs_le_D1 add.commute diff_le_eq)
  1017 
  1018 lemma bounded_inner_imp_bdd_above:
  1019   assumes "bounded s"
  1020     shows "bdd_above ((\<lambda>x. x \<bullet> a) ` s)"
  1021 by (simp add: assms bounded_imp_bdd_above bounded_linear_image bounded_linear_inner_left)
  1022 
  1023 lemma bounded_inner_imp_bdd_below:
  1024   assumes "bounded s"
  1025     shows "bdd_below ((\<lambda>x. x \<bullet> a) ` s)"
  1026 by (simp add: assms bounded_imp_bdd_below bounded_linear_image bounded_linear_inner_left)
  1027 
  1028 lemma bounded_has_Sup:
  1029   fixes S :: "real set"
  1030   assumes "bounded S"
  1031     and "S \<noteq> {}"
  1032   shows "\<forall>x\<in>S. x \<le> Sup S"
  1033     and "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b"
  1034 proof
  1035   show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b"
  1036     using assms by (metis cSup_least)
  1037 qed (metis cSup_upper assms(1) bounded_imp_bdd_above)
  1038 
  1039 lemma Sup_insert:
  1040   fixes S :: "real set"
  1041   shows "bounded S \<Longrightarrow> Sup (insert x S) = (if S = {} then x else max x (Sup S))"
  1042   by (auto simp: bounded_imp_bdd_above sup_max cSup_insert_If)
  1043 
  1044 lemma Sup_insert_finite:
  1045   fixes S :: "'a::conditionally_complete_linorder set"
  1046   shows "finite S \<Longrightarrow> Sup (insert x S) = (if S = {} then x else max x (Sup S))"
  1047 by (simp add: cSup_insert sup_max)
  1048 
  1049 lemma bounded_has_Inf:
  1050   fixes S :: "real set"
  1051   assumes "bounded S"
  1052     and "S \<noteq> {}"
  1053   shows "\<forall>x\<in>S. x \<ge> Inf S"
  1054     and "\<forall>b. (\<forall>x\<in>S. x \<ge> b) \<longrightarrow> Inf S \<ge> b"
  1055 proof
  1056   show "\<forall>b. (\<forall>x\<in>S. x \<ge> b) \<longrightarrow> Inf S \<ge> b"
  1057     using assms by (metis cInf_greatest)
  1058 qed (metis cInf_lower assms(1) bounded_imp_bdd_below)
  1059 
  1060 lemma Inf_insert:
  1061   fixes S :: "real set"
  1062   shows "bounded S \<Longrightarrow> Inf (insert x S) = (if S = {} then x else min x (Inf S))"
  1063   by (auto simp: bounded_imp_bdd_below inf_min cInf_insert_If)
  1064 
  1065 lemma Inf_insert_finite:
  1066   fixes S :: "'a::conditionally_complete_linorder set"
  1067   shows "finite S \<Longrightarrow> Inf (insert x S) = (if S = {} then x else min x (Inf S))"
  1068 by (simp add: cInf_eq_Min)
  1069 
  1070 lemma finite_imp_less_Inf:
  1071   fixes a :: "'a::conditionally_complete_linorder"
  1072   shows "\<lbrakk>finite X; x \<in> X; \<And>x. x\<in>X \<Longrightarrow> a < x\<rbrakk> \<Longrightarrow> a < Inf X"
  1073   by (induction X rule: finite_induct) (simp_all add: cInf_eq_Min Inf_insert_finite)
  1074 
  1075 lemma finite_less_Inf_iff:
  1076   fixes a :: "'a :: conditionally_complete_linorder"
  1077   shows "\<lbrakk>finite X; X \<noteq> {}\<rbrakk> \<Longrightarrow> a < Inf X \<longleftrightarrow> (\<forall>x \<in> X. a < x)"
  1078   by (auto simp: cInf_eq_Min)
  1079 
  1080 lemma finite_imp_Sup_less:
  1081   fixes a :: "'a::conditionally_complete_linorder"
  1082   shows "\<lbrakk>finite X; x \<in> X; \<And>x. x\<in>X \<Longrightarrow> a > x\<rbrakk> \<Longrightarrow> a > Sup X"
  1083   by (induction X rule: finite_induct) (simp_all add: cSup_eq_Max Sup_insert_finite)
  1084 
  1085 lemma finite_Sup_less_iff:
  1086   fixes a :: "'a :: conditionally_complete_linorder"
  1087   shows "\<lbrakk>finite X; X \<noteq> {}\<rbrakk> \<Longrightarrow> a > Sup X \<longleftrightarrow> (\<forall>x \<in> X. a > x)"
  1088   by (auto simp: cSup_eq_Max)
  1089 
  1090 proposition is_interval_compact:
  1091    "is_interval S \<and> compact S \<longleftrightarrow> (\<exists>a b. S = cbox a b)"   (is "?lhs = ?rhs")
  1092 proof (cases "S = {}")
  1093   case True
  1094   with empty_as_interval show ?thesis by auto
  1095 next
  1096   case False
  1097   show ?thesis
  1098   proof
  1099     assume L: ?lhs
  1100     then have "is_interval S" "compact S" by auto
  1101     define a where "a \<equiv> \<Sum>i\<in>Basis. (INF x:S. x \<bullet> i) *\<^sub>R i"
  1102     define b where "b \<equiv> \<Sum>i\<in>Basis. (SUP x:S. x \<bullet> i) *\<^sub>R i"
  1103     have 1: "\<And>x i. \<lbrakk>x \<in> S; i \<in> Basis\<rbrakk> \<Longrightarrow> (INF x:S. x \<bullet> i) \<le> x \<bullet> i"
  1104       by (simp add: cInf_lower bounded_inner_imp_bdd_below compact_imp_bounded L)
  1105     have 2: "\<And>x i. \<lbrakk>x \<in> S; i \<in> Basis\<rbrakk> \<Longrightarrow> x \<bullet> i \<le> (SUP x:S. x \<bullet> i)"
  1106       by (simp add: cSup_upper bounded_inner_imp_bdd_above compact_imp_bounded L)
  1107     have 3: "x \<in> S" if inf: "\<And>i. i \<in> Basis \<Longrightarrow> (INF x:S. x \<bullet> i) \<le> x \<bullet> i"
  1108                    and sup: "\<And>i. i \<in> Basis \<Longrightarrow> x \<bullet> i \<le> (SUP x:S. x \<bullet> i)" for x
  1109     proof (rule mem_box_componentwiseI [OF \<open>is_interval S\<close>])
  1110       fix i::'a
  1111       assume i: "i \<in> Basis"
  1112       have cont: "continuous_on S (\<lambda>x. x \<bullet> i)"
  1113         by (intro continuous_intros)
  1114       obtain a where "a \<in> S" and a: "\<And>y. y\<in>S \<Longrightarrow> a \<bullet> i \<le> y \<bullet> i"
  1115         using continuous_attains_inf [OF \<open>compact S\<close> False cont] by blast
  1116       obtain b where "b \<in> S" and b: "\<And>y. y\<in>S \<Longrightarrow> y \<bullet> i \<le> b \<bullet> i"
  1117         using continuous_attains_sup [OF \<open>compact S\<close> False cont] by blast
  1118       have "a \<bullet> i \<le> (INF x:S. x \<bullet> i)"
  1119         by (simp add: False a cINF_greatest)
  1120       also have "\<dots> \<le> x \<bullet> i"
  1121         by (simp add: i inf)
  1122       finally have ai: "a \<bullet> i \<le> x \<bullet> i" .
  1123       have "x \<bullet> i \<le> (SUP x:S. x \<bullet> i)"
  1124         by (simp add: i sup)
  1125       also have "(SUP x:S. x \<bullet> i) \<le> b \<bullet> i"
  1126         by (simp add: False b cSUP_least)
  1127       finally have bi: "x \<bullet> i \<le> b \<bullet> i" .
  1128       show "x \<bullet> i \<in> (\<lambda>x. x \<bullet> i) ` S"
  1129         apply (rule_tac x="\<Sum>j\<in>Basis. (if j = i then x \<bullet> i else a \<bullet> j) *\<^sub>R j" in image_eqI)
  1130         apply (simp add: i)
  1131         apply (rule mem_is_intervalI [OF \<open>is_interval S\<close> \<open>a \<in> S\<close> \<open>b \<in> S\<close>])
  1132         using i ai bi apply force
  1133         done
  1134     qed
  1135     have "S = cbox a b"
  1136       by (auto simp: a_def b_def mem_box intro: 1 2 3)
  1137     then show ?rhs
  1138       by blast
  1139   next
  1140     assume R: ?rhs
  1141     then show ?lhs
  1142       using compact_cbox is_interval_cbox by blast
  1143   qed
  1144 qed
  1145 
  1146 subsection\<open>Relations among convergence and absolute convergence for power series.\<close>
  1147 
  1148 lemma summable_imp_bounded:
  1149   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
  1150   shows "summable f \<Longrightarrow> bounded (range f)"
  1151 by (frule summable_LIMSEQ_zero) (simp add: convergent_imp_bounded)
  1152 
  1153 lemma summable_imp_sums_bounded:
  1154    "summable f \<Longrightarrow> bounded (range (\<lambda>n. sum f {..<n}))"
  1155 by (auto simp: summable_def sums_def dest: convergent_imp_bounded)
  1156 
  1157 lemma power_series_conv_imp_absconv_weak:
  1158   fixes a:: "nat \<Rightarrow> 'a::{real_normed_div_algebra,banach}" and w :: 'a
  1159   assumes sum: "summable (\<lambda>n. a n * z ^ n)" and no: "norm w < norm z"
  1160     shows "summable (\<lambda>n. of_real(norm(a n)) * w ^ n)"
  1161 proof -
  1162   obtain M where M: "\<And>x. norm (a x * z ^ x) \<le> M"
  1163     using summable_imp_bounded [OF sum] by (force simp: bounded_iff)
  1164   then have *: "summable (\<lambda>n. norm (a n) * norm w ^ n)"
  1165     by (rule_tac M=M in Abel_lemma) (auto simp: norm_mult norm_power intro: no)
  1166   show ?thesis
  1167     apply (rule series_comparison_complex [of "(\<lambda>n. of_real(norm(a n) * norm w ^ n))"])
  1168     apply (simp only: summable_complex_of_real *)
  1169     apply (auto simp: norm_mult norm_power)
  1170     done
  1171 qed
  1172 
  1173 subsection \<open>Bounded closed nest property (proof does not use Heine-Borel)\<close>
  1174 
  1175 lemma bounded_closed_nest:
  1176   fixes s :: "nat \<Rightarrow> ('a::heine_borel) set"
  1177   assumes "\<forall>n. closed (s n)"
  1178     and "\<forall>n. s n \<noteq> {}"
  1179     and "\<forall>m n. m \<le> n \<longrightarrow> s n \<subseteq> s m"
  1180     and "bounded (s 0)"
  1181   shows "\<exists>a. \<forall>n. a \<in> s n"
  1182 proof -
  1183   from assms(2) obtain x where x: "\<forall>n. x n \<in> s n"
  1184     using choice[of "\<lambda>n x. x \<in> s n"] by auto
  1185   from assms(4,1) have "seq_compact (s 0)"
  1186     by (simp add: bounded_closed_imp_seq_compact)
  1187   then obtain l r where lr: "l \<in> s 0" "strict_mono r" "(x \<circ> r) \<longlonglongrightarrow> l"
  1188     using x and assms(3) unfolding seq_compact_def by blast
  1189   have "\<forall>n. l \<in> s n"
  1190   proof
  1191     fix n :: nat
  1192     have "closed (s n)"
  1193       using assms(1) by simp
  1194     moreover have "\<forall>i. (x \<circ> r) i \<in> s i"
  1195       using x and assms(3) and lr(2) [THEN seq_suble] by auto
  1196     then have "\<forall>i. (x \<circ> r) (i + n) \<in> s n"
  1197       using assms(3) by (fast intro!: le_add2)
  1198     moreover have "(\<lambda>i. (x \<circ> r) (i + n)) \<longlonglongrightarrow> l"
  1199       using lr(3) by (rule LIMSEQ_ignore_initial_segment)
  1200     ultimately show "l \<in> s n"
  1201       by (rule closed_sequentially)
  1202   qed
  1203   then show ?thesis ..
  1204 qed
  1205 
  1206 text \<open>Decreasing case does not even need compactness, just completeness.\<close>
  1207 
  1208 lemma decreasing_closed_nest:
  1209   fixes s :: "nat \<Rightarrow> ('a::complete_space) set"
  1210   assumes
  1211     "\<forall>n. closed (s n)"
  1212     "\<forall>n. s n \<noteq> {}"
  1213     "\<forall>m n. m \<le> n \<longrightarrow> s n \<subseteq> s m"
  1214     "\<forall>e>0. \<exists>n. \<forall>x\<in>s n. \<forall>y\<in>s n. dist x y < e"
  1215   shows "\<exists>a. \<forall>n. a \<in> s n"
  1216 proof -
  1217   have "\<forall>n. \<exists>x. x \<in> s n"
  1218     using assms(2) by auto
  1219   then have "\<exists>t. \<forall>n. t n \<in> s n"
  1220     using choice[of "\<lambda>n x. x \<in> s n"] by auto
  1221   then obtain t where t: "\<forall>n. t n \<in> s n" by auto
  1222   {
  1223     fix e :: real
  1224     assume "e > 0"
  1225     then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e"
  1226       using assms(4) by auto
  1227     {
  1228       fix m n :: nat
  1229       assume "N \<le> m \<and> N \<le> n"
  1230       then have "t m \<in> s N" "t n \<in> s N"
  1231         using assms(3) t unfolding  subset_eq t by blast+
  1232       then have "dist (t m) (t n) < e"
  1233         using N by auto
  1234     }
  1235     then have "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e"
  1236       by auto
  1237   }
  1238   then have "Cauchy t"
  1239     unfolding cauchy_def by auto
  1240   then obtain l where l:"(t \<longlongrightarrow> l) sequentially"
  1241     using complete_UNIV unfolding complete_def by auto
  1242   {
  1243     fix n :: nat
  1244     {
  1245       fix e :: real
  1246       assume "e > 0"
  1247       then obtain N :: nat where N: "\<forall>n\<ge>N. dist (t n) l < e"
  1248         using l[unfolded lim_sequentially] by auto
  1249       have "t (max n N) \<in> s n"
  1250         by (meson assms(3) contra_subsetD max.cobounded1 t)
  1251       then have "\<exists>y\<in>s n. dist y l < e"
  1252         using N max.cobounded2 by blast
  1253     }
  1254     then have "l \<in> s n"
  1255       using closed_approachable[of "s n" l] assms(1) by auto
  1256   }
  1257   then show ?thesis by auto
  1258 qed
  1259 
  1260 text \<open>Strengthen it to the intersection actually being a singleton.\<close>
  1261 
  1262 lemma decreasing_closed_nest_sing:
  1263   fixes s :: "nat \<Rightarrow> 'a::complete_space set"
  1264   assumes
  1265     "\<forall>n. closed(s n)"
  1266     "\<forall>n. s n \<noteq> {}"
  1267     "\<forall>m n. m \<le> n \<longrightarrow> s n \<subseteq> s m"
  1268     "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"
  1269   shows "\<exists>a. \<Inter>(range s) = {a}"
  1270 proof -
  1271   obtain a where a: "\<forall>n. a \<in> s n"
  1272     using decreasing_closed_nest[of s] using assms by auto
  1273   {
  1274     fix b
  1275     assume b: "b \<in> \<Inter>(range s)"
  1276     {
  1277       fix e :: real
  1278       assume "e > 0"
  1279       then have "dist a b < e"
  1280         using assms(4) and b and a by blast
  1281     }
  1282     then have "dist a b = 0"
  1283       by (metis dist_eq_0_iff dist_nz less_le)
  1284   }
  1285   with a have "\<Inter>(range s) = {a}"
  1286     unfolding image_def by auto
  1287   then show ?thesis ..
  1288 qed
  1289 
  1290 
  1291 subsection \<open>Infimum Distance\<close>
  1292 
  1293 definition "infdist x A = (if A = {} then 0 else INF a:A. dist x a)"
  1294 
  1295 lemma bdd_below_infdist[intro, simp]: "bdd_below (dist x`A)"
  1296   by (auto intro!: zero_le_dist)
  1297 
  1298 lemma infdist_notempty: "A \<noteq> {} \<Longrightarrow> infdist x A = (INF a:A. dist x a)"
  1299   by (simp add: infdist_def)
  1300 
  1301 lemma infdist_nonneg: "0 \<le> infdist x A"
  1302   by (auto simp: infdist_def intro: cINF_greatest)
  1303 
  1304 lemma infdist_le: "a \<in> A \<Longrightarrow> infdist x A \<le> dist x a"
  1305   by (auto intro: cINF_lower simp add: infdist_def)
  1306 
  1307 lemma infdist_le2: "a \<in> A \<Longrightarrow> dist x a \<le> d \<Longrightarrow> infdist x A \<le> d"
  1308   by (auto intro!: cINF_lower2 simp add: infdist_def)
  1309 
  1310 lemma infdist_zero[simp]: "a \<in> A \<Longrightarrow> infdist a A = 0"
  1311   by (auto intro!: antisym infdist_nonneg infdist_le2)
  1312 
  1313 lemma infdist_triangle: "infdist x A \<le> infdist y A + dist x y"
  1314 proof (cases "A = {}")
  1315   case True
  1316   then show ?thesis by (simp add: infdist_def)
  1317 next
  1318   case False
  1319   then obtain a where "a \<in> A" by auto
  1320   have "infdist x A \<le> Inf {dist x y + dist y a |a. a \<in> A}"
  1321   proof (rule cInf_greatest)
  1322     from \<open>A \<noteq> {}\<close> show "{dist x y + dist y a |a. a \<in> A} \<noteq> {}"
  1323       by simp
  1324     fix d
  1325     assume "d \<in> {dist x y + dist y a |a. a \<in> A}"
  1326     then obtain a where d: "d = dist x y + dist y a" "a \<in> A"
  1327       by auto
  1328     show "infdist x A \<le> d"
  1329       unfolding infdist_notempty[OF \<open>A \<noteq> {}\<close>]
  1330     proof (rule cINF_lower2)
  1331       show "a \<in> A" by fact
  1332       show "dist x a \<le> d"
  1333         unfolding d by (rule dist_triangle)
  1334     qed simp
  1335   qed
  1336   also have "\<dots> = dist x y + infdist y A"
  1337   proof (rule cInf_eq, safe)
  1338     fix a
  1339     assume "a \<in> A"
  1340     then show "dist x y + infdist y A \<le> dist x y + dist y a"
  1341       by (auto intro: infdist_le)
  1342   next
  1343     fix i
  1344     assume inf: "\<And>d. d \<in> {dist x y + dist y a |a. a \<in> A} \<Longrightarrow> i \<le> d"
  1345     then have "i - dist x y \<le> infdist y A"
  1346       unfolding infdist_notempty[OF \<open>A \<noteq> {}\<close>] using \<open>a \<in> A\<close>
  1347       by (intro cINF_greatest) (auto simp: field_simps)
  1348     then show "i \<le> dist x y + infdist y A"
  1349       by simp
  1350   qed
  1351   finally show ?thesis by simp
  1352 qed
  1353 
  1354 lemma in_closure_iff_infdist_zero:
  1355   assumes "A \<noteq> {}"
  1356   shows "x \<in> closure A \<longleftrightarrow> infdist x A = 0"
  1357 proof
  1358   assume "x \<in> closure A"
  1359   show "infdist x A = 0"
  1360   proof (rule ccontr)
  1361     assume "infdist x A \<noteq> 0"
  1362     with infdist_nonneg[of x A] have "infdist x A > 0"
  1363       by auto
  1364     then have "ball x (infdist x A) \<inter> closure A = {}"
  1365       apply auto
  1366       apply (metis \<open>x \<in> closure A\<close> closure_approachable dist_commute infdist_le not_less)
  1367       done
  1368     then have "x \<notin> closure A"
  1369       by (metis \<open>0 < infdist x A\<close> centre_in_ball disjoint_iff_not_equal)
  1370     then show False using \<open>x \<in> closure A\<close> by simp
  1371   qed
  1372 next
  1373   assume x: "infdist x A = 0"
  1374   then obtain a where "a \<in> A"
  1375     by atomize_elim (metis all_not_in_conv assms)
  1376   show "x \<in> closure A"
  1377     unfolding closure_approachable
  1378     apply safe
  1379   proof (rule ccontr)
  1380     fix e :: real
  1381     assume "e > 0"
  1382     assume "\<not> (\<exists>y\<in>A. dist y x < e)"
  1383     then have "infdist x A \<ge> e" using \<open>a \<in> A\<close>
  1384       unfolding infdist_def
  1385       by (force simp: dist_commute intro: cINF_greatest)
  1386     with x \<open>e > 0\<close> show False by auto
  1387   qed
  1388 qed
  1389 
  1390 lemma in_closed_iff_infdist_zero:
  1391   assumes "closed A" "A \<noteq> {}"
  1392   shows "x \<in> A \<longleftrightarrow> infdist x A = 0"
  1393 proof -
  1394   have "x \<in> closure A \<longleftrightarrow> infdist x A = 0"
  1395     by (rule in_closure_iff_infdist_zero) fact
  1396   with assms show ?thesis by simp
  1397 qed
  1398 
  1399 lemma tendsto_infdist [tendsto_intros]:
  1400   assumes f: "(f \<longlongrightarrow> l) F"
  1401   shows "((\<lambda>x. infdist (f x) A) \<longlongrightarrow> infdist l A) F"
  1402 proof (rule tendstoI)
  1403   fix e ::real
  1404   assume "e > 0"
  1405   from tendstoD[OF f this]
  1406   show "eventually (\<lambda>x. dist (infdist (f x) A) (infdist l A) < e) F"
  1407   proof (eventually_elim)
  1408     fix x
  1409     from infdist_triangle[of l A "f x"] infdist_triangle[of "f x" A l]
  1410     have "dist (infdist (f x) A) (infdist l A) \<le> dist (f x) l"
  1411       by (simp add: dist_commute dist_real_def)
  1412     also assume "dist (f x) l < e"
  1413     finally show "dist (infdist (f x) A) (infdist l A) < e" .
  1414   qed
  1415 qed
  1416 
  1417 lemma continuous_infdist[continuous_intros]:
  1418   assumes "continuous F f"
  1419   shows "continuous F (\<lambda>x. infdist (f x) A)"
  1420   using assms unfolding continuous_def by (rule tendsto_infdist)
  1421 
  1422 subsection \<open>Equality of continuous functions on closure and related results.\<close>
  1423 
  1424 lemma continuous_closedin_preimage_constant:
  1425   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  1426   shows "continuous_on S f \<Longrightarrow> closedin (subtopology euclidean S) {x \<in> S. f x = a}"
  1427   using continuous_closedin_preimage[of S f "{a}"] by (simp add: vimage_def Collect_conj_eq)
  1428 
  1429 lemma continuous_closed_preimage_constant:
  1430   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  1431   shows "continuous_on S f \<Longrightarrow> closed S \<Longrightarrow> closed {x \<in> S. f x = a}"
  1432   using continuous_closed_preimage[of S f "{a}"] by (simp add: vimage_def Collect_conj_eq)
  1433 
  1434 lemma continuous_constant_on_closure:
  1435   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  1436   assumes "continuous_on (closure S) f"
  1437       and "\<And>x. x \<in> S \<Longrightarrow> f x = a"
  1438       and "x \<in> closure S"
  1439   shows "f x = a"
  1440     using continuous_closed_preimage_constant[of "closure S" f a]
  1441       assms closure_minimal[of S "{x \<in> closure S. f x = a}"] closure_subset
  1442     unfolding subset_eq
  1443     by auto
  1444 
  1445 lemma image_closure_subset:
  1446   assumes contf: "continuous_on (closure S) f"
  1447     and "closed T"
  1448     and "(f ` S) \<subseteq> T"
  1449   shows "f ` (closure S) \<subseteq> T"
  1450 proof -
  1451   have "S \<subseteq> {x \<in> closure S. f x \<in> T}"
  1452     using assms(3) closure_subset by auto
  1453   moreover have "closed (closure S \<inter> f -` T)"
  1454     using continuous_closed_preimage[OF contf] \<open>closed T\<close> by auto
  1455   ultimately have "closure S = (closure S \<inter> f -` T)"
  1456     using closure_minimal[of S "(closure S \<inter> f -` T)"] by auto
  1457   then show ?thesis by auto
  1458 qed
  1459 
  1460 lemma continuous_on_closure_norm_le:
  1461   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  1462   assumes "continuous_on (closure s) f"
  1463     and "\<forall>y \<in> s. norm(f y) \<le> b"
  1464     and "x \<in> (closure s)"
  1465   shows "norm (f x) \<le> b"
  1466 proof -
  1467   have *: "f ` s \<subseteq> cball 0 b"
  1468     using assms(2)[unfolded mem_cball_0[symmetric]] by auto
  1469   show ?thesis
  1470     by (meson "*" assms(1) assms(3) closed_cball image_closure_subset image_subset_iff mem_cball_0)
  1471 qed
  1472 
  1473 lemma isCont_indicator:
  1474   fixes x :: "'a::t2_space"
  1475   shows "isCont (indicator A :: 'a \<Rightarrow> real) x = (x \<notin> frontier A)"
  1476 proof auto
  1477   fix x
  1478   assume cts_at: "isCont (indicator A :: 'a \<Rightarrow> real) x" and fr: "x \<in> frontier A"
  1479   with continuous_at_open have 1: "\<forall>V::real set. open V \<and> indicator A x \<in> V \<longrightarrow>
  1480     (\<exists>U::'a set. open U \<and> x \<in> U \<and> (\<forall>y\<in>U. indicator A y \<in> V))" by auto
  1481   show False
  1482   proof (cases "x \<in> A")
  1483     assume x: "x \<in> A"
  1484     hence "indicator A x \<in> ({0<..<2} :: real set)" by simp
  1485     hence "\<exists>U. open U \<and> x \<in> U \<and> (\<forall>y\<in>U. indicator A y \<in> ({0<..<2} :: real set))"
  1486       using 1 open_greaterThanLessThan by blast
  1487     then guess U .. note U = this
  1488     hence "\<forall>y\<in>U. indicator A y > (0::real)"
  1489       unfolding greaterThanLessThan_def by auto
  1490     hence "U \<subseteq> A" using indicator_eq_0_iff by force
  1491     hence "x \<in> interior A" using U interiorI by auto
  1492     thus ?thesis using fr unfolding frontier_def by simp
  1493   next
  1494     assume x: "x \<notin> A"
  1495     hence "indicator A x \<in> ({-1<..<1} :: real set)" by simp
  1496     hence "\<exists>U. open U \<and> x \<in> U \<and> (\<forall>y\<in>U. indicator A y \<in> ({-1<..<1} :: real set))"
  1497       using 1 open_greaterThanLessThan by blast
  1498     then guess U .. note U = this
  1499     hence "\<forall>y\<in>U. indicator A y < (1::real)"
  1500       unfolding greaterThanLessThan_def by auto
  1501     hence "U \<subseteq> -A" by auto
  1502     hence "x \<in> interior (-A)" using U interiorI by auto
  1503     thus ?thesis using fr interior_complement unfolding frontier_def by auto
  1504   qed
  1505 next
  1506   assume nfr: "x \<notin> frontier A"
  1507   hence "x \<in> interior A \<or> x \<in> interior (-A)"
  1508     by (auto simp: frontier_def closure_interior)
  1509   thus "isCont ((indicator A)::'a \<Rightarrow> real) x"
  1510   proof
  1511     assume int: "x \<in> interior A"
  1512     then obtain U where U: "open U" "x \<in> U" "U \<subseteq> A" unfolding interior_def by auto
  1513     hence "\<forall>y\<in>U. indicator A y = (1::real)" unfolding indicator_def by auto
  1514     hence "continuous_on U (indicator A)" by (simp add: continuous_on_const indicator_eq_1_iff)
  1515     thus ?thesis using U continuous_on_eq_continuous_at by auto
  1516   next
  1517     assume ext: "x \<in> interior (-A)"
  1518     then obtain U where U: "open U" "x \<in> U" "U \<subseteq> -A" unfolding interior_def by auto
  1519     then have "continuous_on U (indicator A)"
  1520       using continuous_on_topological by (auto simp: subset_iff)
  1521     thus ?thesis using U continuous_on_eq_continuous_at by auto
  1522   qed
  1523 qed
  1524 
  1525 subsection \<open>A function constant on a set\<close>
  1526 
  1527 definition constant_on  (infixl "(constant'_on)" 50)
  1528   where "f constant_on A \<equiv> \<exists>y. \<forall>x\<in>A. f x = y"
  1529 
  1530 lemma constant_on_subset: "\<lbrakk>f constant_on A; B \<subseteq> A\<rbrakk> \<Longrightarrow> f constant_on B"
  1531   unfolding constant_on_def by blast
  1532 
  1533 lemma injective_not_constant:
  1534   fixes S :: "'a::{perfect_space} set"
  1535   shows "\<lbrakk>open S; inj_on f S; f constant_on S\<rbrakk> \<Longrightarrow> S = {}"
  1536 unfolding constant_on_def
  1537 by (metis equals0I inj_on_contraD islimpt_UNIV islimpt_def)
  1538 
  1539 lemma constant_on_closureI:
  1540   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  1541   assumes cof: "f constant_on S" and contf: "continuous_on (closure S) f"
  1542     shows "f constant_on (closure S)"
  1543 using continuous_constant_on_closure [OF contf] cof unfolding constant_on_def
  1544 by metis
  1545 
  1546 subsection\<open>Relating linear images to open/closed/interior/closure\<close>
  1547 
  1548 proposition open_surjective_linear_image:
  1549   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::euclidean_space"
  1550   assumes "open A" "linear f" "surj f"
  1551     shows "open(f ` A)"
  1552 unfolding open_dist
  1553 proof clarify
  1554   fix x
  1555   assume "x \<in> A"
  1556   have "bounded (inv f ` Basis)"
  1557     by (simp add: finite_imp_bounded)
  1558   with bounded_pos obtain B where "B > 0" and B: "\<And>x. x \<in> inv f ` Basis \<Longrightarrow> norm x \<le> B"
  1559     by metis
  1560   obtain e where "e > 0" and e: "\<And>z. dist z x < e \<Longrightarrow> z \<in> A"
  1561     by (metis open_dist \<open>x \<in> A\<close> \<open>open A\<close>)
  1562   define \<delta> where "\<delta> \<equiv> e / B / DIM('b)"
  1563   show "\<exists>e>0. \<forall>y. dist y (f x) < e \<longrightarrow> y \<in> f ` A"
  1564   proof (intro exI conjI)
  1565     show "\<delta> > 0"
  1566       using \<open>e > 0\<close> \<open>B > 0\<close>  by (simp add: \<delta>_def divide_simps)
  1567     have "y \<in> f ` A" if "dist y (f x) * (B * real DIM('b)) < e" for y
  1568     proof -
  1569       define u where "u \<equiv> y - f x"
  1570       show ?thesis
  1571       proof (rule image_eqI)
  1572         show "y = f (x + (\<Sum>i\<in>Basis. (u \<bullet> i) *\<^sub>R inv f i))"
  1573           apply (simp add: linear_add linear_sum linear.scaleR \<open>linear f\<close> surj_f_inv_f \<open>surj f\<close>)
  1574           apply (simp add: euclidean_representation u_def)
  1575           done
  1576         have "dist (x + (\<Sum>i\<in>Basis. (u \<bullet> i) *\<^sub>R inv f i)) x \<le> (\<Sum>i\<in>Basis. norm ((u \<bullet> i) *\<^sub>R inv f i))"
  1577           by (simp add: dist_norm sum_norm_le)
  1578         also have "... = (\<Sum>i\<in>Basis. \<bar>u \<bullet> i\<bar> * norm (inv f i))"
  1579           by simp
  1580         also have "... \<le> (\<Sum>i\<in>Basis. \<bar>u \<bullet> i\<bar>) * B"
  1581           by (simp add: B sum_distrib_right sum_mono mult_left_mono)
  1582         also have "... \<le> DIM('b) * dist y (f x) * B"
  1583           apply (rule mult_right_mono [OF sum_bounded_above])
  1584           using \<open>0 < B\<close> by (auto simp: Basis_le_norm dist_norm u_def)
  1585         also have "... < e"
  1586           by (metis mult.commute mult.left_commute that)
  1587         finally show "x + (\<Sum>i\<in>Basis. (u \<bullet> i) *\<^sub>R inv f i) \<in> A"
  1588           by (rule e)
  1589       qed
  1590     qed
  1591     then show "\<forall>y. dist y (f x) < \<delta> \<longrightarrow> y \<in> f ` A"
  1592       using \<open>e > 0\<close> \<open>B > 0\<close>
  1593       by (auto simp: \<delta>_def divide_simps mult_less_0_iff)
  1594   qed
  1595 qed
  1596 
  1597 corollary open_bijective_linear_image_eq:
  1598   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1599   assumes "linear f" "bij f"
  1600     shows "open(f ` A) \<longleftrightarrow> open A"
  1601 proof
  1602   assume "open(f ` A)"
  1603   then have "open(f -` (f ` A))"
  1604     using assms by (force simp: linear_continuous_at linear_conv_bounded_linear continuous_open_vimage)
  1605   then show "open A"
  1606     by (simp add: assms bij_is_inj inj_vimage_image_eq)
  1607 next
  1608   assume "open A"
  1609   then show "open(f ` A)"
  1610     by (simp add: assms bij_is_surj open_surjective_linear_image)
  1611 qed
  1612 
  1613 corollary interior_bijective_linear_image:
  1614   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  1615   assumes "linear f" "bij f"
  1616   shows "interior (f ` S) = f ` interior S"  (is "?lhs = ?rhs")
  1617 proof safe
  1618   fix x
  1619   assume x: "x \<in> ?lhs"
  1620   then obtain T where "open T" and "x \<in> T" and "T \<subseteq> f ` S"
  1621     by (metis interiorE)
  1622   then show "x \<in> ?rhs"
  1623     by (metis (no_types, hide_lams) assms subsetD interior_maximal open_bijective_linear_image_eq subset_image_iff)
  1624 next
  1625   fix x
  1626   assume x: "x \<in> interior S"
  1627   then show "f x \<in> interior (f ` S)"
  1628     by (meson assms imageI image_mono interiorI interior_subset open_bijective_linear_image_eq open_interior)
  1629 qed
  1630 
  1631 lemma interior_injective_linear_image:
  1632   fixes f :: "'a::euclidean_space \<Rightarrow> 'a::euclidean_space"
  1633   assumes "linear f" "inj f"
  1634    shows "interior(f ` S) = f ` (interior S)"
  1635   by (simp add: linear_injective_imp_surjective assms bijI interior_bijective_linear_image)
  1636 
  1637 lemma interior_surjective_linear_image:
  1638   fixes f :: "'a::euclidean_space \<Rightarrow> 'a::euclidean_space"
  1639   assumes "linear f" "surj f"
  1640    shows "interior(f ` S) = f ` (interior S)"
  1641   by (simp add: assms interior_injective_linear_image linear_surjective_imp_injective)
  1642 
  1643 lemma interior_negations:
  1644   fixes S :: "'a::euclidean_space set"
  1645   shows "interior(uminus ` S) = image uminus (interior S)"
  1646   by (simp add: bij_uminus interior_bijective_linear_image linear_uminus)
  1647 
  1648 text \<open>Preservation of compactness and connectedness under continuous function.\<close>
  1649 
  1650 lemma compact_eq_openin_cover:
  1651   "compact S \<longleftrightarrow>
  1652     (\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>
  1653       (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D))"
  1654 proof safe
  1655   fix C
  1656   assume "compact S" and "\<forall>c\<in>C. openin (subtopology euclidean S) c" and "S \<subseteq> \<Union>C"
  1657   then have "\<forall>c\<in>{T. open T \<and> S \<inter> T \<in> C}. open c" and "S \<subseteq> \<Union>{T. open T \<and> S \<inter> T \<in> C}"
  1658     unfolding openin_open by force+
  1659   with \<open>compact S\<close> obtain D where "D \<subseteq> {T. open T \<and> S \<inter> T \<in> C}" and "finite D" and "S \<subseteq> \<Union>D"
  1660     by (meson compactE)
  1661   then have "image (\<lambda>T. S \<inter> T) D \<subseteq> C \<and> finite (image (\<lambda>T. S \<inter> T) D) \<and> S \<subseteq> \<Union>(image (\<lambda>T. S \<inter> T) D)"
  1662     by auto
  1663   then show "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..
  1664 next
  1665   assume 1: "\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>
  1666         (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D)"
  1667   show "compact S"
  1668   proof (rule compactI)
  1669     fix C
  1670     let ?C = "image (\<lambda>T. S \<inter> T) C"
  1671     assume "\<forall>t\<in>C. open t" and "S \<subseteq> \<Union>C"
  1672     then have "(\<forall>c\<in>?C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>?C"
  1673       unfolding openin_open by auto
  1674     with 1 obtain D where "D \<subseteq> ?C" and "finite D" and "S \<subseteq> \<Union>D"
  1675       by metis
  1676     let ?D = "inv_into C (\<lambda>T. S \<inter> T) ` D"
  1677     have "?D \<subseteq> C \<and> finite ?D \<and> S \<subseteq> \<Union>?D"
  1678     proof (intro conjI)
  1679       from \<open>D \<subseteq> ?C\<close> show "?D \<subseteq> C"
  1680         by (fast intro: inv_into_into)
  1681       from \<open>finite D\<close> show "finite ?D"
  1682         by (rule finite_imageI)
  1683       from \<open>S \<subseteq> \<Union>D\<close> show "S \<subseteq> \<Union>?D"
  1684         apply (rule subset_trans, clarsimp)
  1685         apply (frule subsetD [OF \<open>D \<subseteq> ?C\<close>, THEN f_inv_into_f])
  1686         apply (erule rev_bexI, fast)
  1687         done
  1688     qed
  1689     then show "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..
  1690   qed
  1691 qed
  1692 
  1693 subsection\<open> Theorems relating continuity and uniform continuity to closures\<close>
  1694 
  1695 lemma continuous_on_closure:
  1696    "continuous_on (closure S) f \<longleftrightarrow>
  1697     (\<forall>x e. x \<in> closure S \<and> 0 < e
  1698            \<longrightarrow> (\<exists>d. 0 < d \<and> (\<forall>y. y \<in> S \<and> dist y x < d \<longrightarrow> dist (f y) (f x) < e)))"
  1699    (is "?lhs = ?rhs")
  1700 proof
  1701   assume ?lhs then show ?rhs
  1702     unfolding continuous_on_iff  by (metis Un_iff closure_def)
  1703 next
  1704   assume R [rule_format]: ?rhs
  1705   show ?lhs
  1706   proof
  1707     fix x and e::real
  1708     assume "0 < e" and x: "x \<in> closure S"
  1709     obtain \<delta>::real where "\<delta> > 0"
  1710                    and \<delta>: "\<And>y. \<lbrakk>y \<in> S; dist y x < \<delta>\<rbrakk> \<Longrightarrow> dist (f y) (f x) < e/2"
  1711       using R [of x "e/2"] \<open>0 < e\<close> x by auto
  1712     have "dist (f y) (f x) \<le> e" if y: "y \<in> closure S" and dyx: "dist y x < \<delta>/2" for y
  1713     proof -
  1714       obtain \<delta>'::real where "\<delta>' > 0"
  1715                       and \<delta>': "\<And>z. \<lbrakk>z \<in> S; dist z y < \<delta>'\<rbrakk> \<Longrightarrow> dist (f z) (f y) < e/2"
  1716         using R [of y "e/2"] \<open>0 < e\<close> y by auto
  1717       obtain z where "z \<in> S" and z: "dist z y < min \<delta>' \<delta> / 2"
  1718         using closure_approachable y
  1719         by (metis \<open>0 < \<delta>'\<close> \<open>0 < \<delta>\<close> divide_pos_pos min_less_iff_conj zero_less_numeral)
  1720       have "dist (f z) (f y) < e/2"
  1721         apply (rule \<delta>' [OF \<open>z \<in> S\<close>])
  1722         using z \<open>0 < \<delta>'\<close> by linarith
  1723       moreover have "dist (f z) (f x) < e/2"
  1724         apply (rule \<delta> [OF \<open>z \<in> S\<close>])
  1725         using z \<open>0 < \<delta>\<close>  dist_commute[of y z] dist_triangle_half_r [of y] dyx by auto
  1726       ultimately show ?thesis
  1727         by (metis dist_commute dist_triangle_half_l less_imp_le)
  1728     qed
  1729     then show "\<exists>d>0. \<forall>x'\<in>closure S. dist x' x < d \<longrightarrow> dist (f x') (f x) \<le> e"
  1730       by (rule_tac x="\<delta>/2" in exI) (simp add: \<open>\<delta> > 0\<close>)
  1731   qed
  1732 qed
  1733 
  1734 lemma continuous_on_closure_sequentially:
  1735   fixes f :: "'a::metric_space \<Rightarrow> 'b :: metric_space"
  1736   shows
  1737    "continuous_on (closure S) f \<longleftrightarrow>
  1738     (\<forall>x a. a \<in> closure S \<and> (\<forall>n. x n \<in> S) \<and> x \<longlonglongrightarrow> a \<longrightarrow> (f \<circ> x) \<longlonglongrightarrow> f a)"
  1739    (is "?lhs = ?rhs")
  1740 proof -
  1741   have "continuous_on (closure S) f \<longleftrightarrow>
  1742            (\<forall>x \<in> closure S. continuous (at x within S) f)"
  1743     by (force simp: continuous_on_closure continuous_within_eps_delta)
  1744   also have "... = ?rhs"
  1745     by (force simp: continuous_within_sequentially)
  1746   finally show ?thesis .
  1747 qed
  1748 
  1749 lemma uniformly_continuous_on_closure:
  1750   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  1751   assumes ucont: "uniformly_continuous_on S f"
  1752       and cont: "continuous_on (closure S) f"
  1753     shows "uniformly_continuous_on (closure S) f"
  1754 unfolding uniformly_continuous_on_def
  1755 proof (intro allI impI)
  1756   fix e::real
  1757   assume "0 < e"
  1758   then obtain d::real
  1759     where "d>0"
  1760       and d: "\<And>x x'. \<lbrakk>x\<in>S; x'\<in>S; dist x' x < d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e/3"
  1761     using ucont [unfolded uniformly_continuous_on_def, rule_format, of "e/3"] by auto
  1762   show "\<exists>d>0. \<forall>x\<in>closure S. \<forall>x'\<in>closure S. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
  1763   proof (rule exI [where x="d/3"], clarsimp simp: \<open>d > 0\<close>)
  1764     fix x y
  1765     assume x: "x \<in> closure S" and y: "y \<in> closure S" and dyx: "dist y x * 3 < d"
  1766     obtain d1::real where "d1 > 0"
  1767            and d1: "\<And>w. \<lbrakk>w \<in> closure S; dist w x < d1\<rbrakk> \<Longrightarrow> dist (f w) (f x) < e/3"
  1768       using cont [unfolded continuous_on_iff, rule_format, of "x" "e/3"] \<open>0 < e\<close> x by auto
  1769      obtain x' where "x' \<in> S" and x': "dist x' x < min d1 (d / 3)"
  1770         using closure_approachable [of x S]
  1771         by (metis \<open>0 < d1\<close> \<open>0 < d\<close> divide_pos_pos min_less_iff_conj x zero_less_numeral)
  1772     obtain d2::real where "d2 > 0"
  1773            and d2: "\<forall>w \<in> closure S. dist w y < d2 \<longrightarrow> dist (f w) (f y) < e/3"
  1774       using cont [unfolded continuous_on_iff, rule_format, of "y" "e/3"] \<open>0 < e\<close> y by auto
  1775      obtain y' where "y' \<in> S" and y': "dist y' y < min d2 (d / 3)"
  1776         using closure_approachable [of y S]
  1777         by (metis \<open>0 < d2\<close> \<open>0 < d\<close> divide_pos_pos min_less_iff_conj y zero_less_numeral)
  1778      have "dist x' x < d/3" using x' by auto
  1779      moreover have "dist x y < d/3"
  1780        by (metis dist_commute dyx less_divide_eq_numeral1(1))
  1781      moreover have "dist y y' < d/3"
  1782        by (metis (no_types) dist_commute min_less_iff_conj y')
  1783      ultimately have "dist x' y' < d/3 + d/3 + d/3"
  1784        by (meson dist_commute_lessI dist_triangle_lt add_strict_mono)
  1785      then have "dist x' y' < d" by simp
  1786      then have "dist (f x') (f y') < e/3"
  1787        by (rule d [OF \<open>y' \<in> S\<close> \<open>x' \<in> S\<close>])
  1788      moreover have "dist (f x') (f x) < e/3" using \<open>x' \<in> S\<close> closure_subset x' d1
  1789        by (simp add: closure_def)
  1790      moreover have "dist (f y') (f y) < e/3" using \<open>y' \<in> S\<close> closure_subset y' d2
  1791        by (simp add: closure_def)
  1792      ultimately have "dist (f y) (f x) < e/3 + e/3 + e/3"
  1793        by (meson dist_commute_lessI dist_triangle_lt add_strict_mono)
  1794     then show "dist (f y) (f x) < e" by simp
  1795   qed
  1796 qed
  1797 
  1798 lemma uniformly_continuous_on_extension_at_closure:
  1799   fixes f::"'a::metric_space \<Rightarrow> 'b::complete_space"
  1800   assumes uc: "uniformly_continuous_on X f"
  1801   assumes "x \<in> closure X"
  1802   obtains l where "(f \<longlongrightarrow> l) (at x within X)"
  1803 proof -
  1804   from assms obtain xs where xs: "xs \<longlonglongrightarrow> x" "\<And>n. xs n \<in> X"
  1805     by (auto simp: closure_sequential)
  1806 
  1807   from uniformly_continuous_on_Cauchy[OF uc LIMSEQ_imp_Cauchy, OF xs]
  1808   obtain l where l: "(\<lambda>n. f (xs n)) \<longlonglongrightarrow> l"
  1809     by atomize_elim (simp only: convergent_eq_Cauchy)
  1810 
  1811   have "(f \<longlongrightarrow> l) (at x within X)"
  1812   proof (safe intro!: Lim_within_LIMSEQ)
  1813     fix xs'
  1814     assume "\<forall>n. xs' n \<noteq> x \<and> xs' n \<in> X"
  1815       and xs': "xs' \<longlonglongrightarrow> x"
  1816     then have "xs' n \<noteq> x" "xs' n \<in> X" for n by auto
  1817 
  1818     from uniformly_continuous_on_Cauchy[OF uc LIMSEQ_imp_Cauchy, OF \<open>xs' \<longlonglongrightarrow> x\<close> \<open>xs' _ \<in> X\<close>]
  1819     obtain l' where l': "(\<lambda>n. f (xs' n)) \<longlonglongrightarrow> l'"
  1820       by atomize_elim (simp only: convergent_eq_Cauchy)
  1821 
  1822     show "(\<lambda>n. f (xs' n)) \<longlonglongrightarrow> l"
  1823     proof (rule tendstoI)
  1824       fix e::real assume "e > 0"
  1825       define e' where "e' \<equiv> e / 2"
  1826       have "e' > 0" using \<open>e > 0\<close> by (simp add: e'_def)
  1827 
  1828       have "\<forall>\<^sub>F n in sequentially. dist (f (xs n)) l < e'"
  1829         by (simp add: \<open>0 < e'\<close> l tendstoD)
  1830       moreover
  1831       from uc[unfolded uniformly_continuous_on_def, rule_format, OF \<open>e' > 0\<close>]
  1832       obtain d where d: "d > 0" "\<And>x x'. x \<in> X \<Longrightarrow> x' \<in> X \<Longrightarrow> dist x x' < d \<Longrightarrow> dist (f x) (f x') < e'"
  1833         by auto
  1834       have "\<forall>\<^sub>F n in sequentially. dist (xs n) (xs' n) < d"
  1835         by (auto intro!: \<open>0 < d\<close> order_tendstoD tendsto_eq_intros xs xs')
  1836       ultimately
  1837       show "\<forall>\<^sub>F n in sequentially. dist (f (xs' n)) l < e"
  1838       proof eventually_elim
  1839         case (elim n)
  1840         have "dist (f (xs' n)) l \<le> dist (f (xs n)) (f (xs' n)) + dist (f (xs n)) l"
  1841           by (metis dist_triangle dist_commute)
  1842         also have "dist (f (xs n)) (f (xs' n)) < e'"
  1843           by (auto intro!: d xs \<open>xs' _ \<in> _\<close> elim)
  1844         also note \<open>dist (f (xs n)) l < e'\<close>
  1845         also have "e' + e' = e" by (simp add: e'_def)
  1846         finally show ?case by simp
  1847       qed
  1848     qed
  1849   qed
  1850   thus ?thesis ..
  1851 qed
  1852 
  1853 lemma uniformly_continuous_on_extension_on_closure:
  1854   fixes f::"'a::metric_space \<Rightarrow> 'b::complete_space"
  1855   assumes uc: "uniformly_continuous_on X f"
  1856   obtains g where "uniformly_continuous_on (closure X) g" "\<And>x. x \<in> X \<Longrightarrow> f x = g x"
  1857     "\<And>Y h x. X \<subseteq> Y \<Longrightarrow> Y \<subseteq> closure X \<Longrightarrow> continuous_on Y h \<Longrightarrow> (\<And>x. x \<in> X \<Longrightarrow> f x = h x) \<Longrightarrow> x \<in> Y \<Longrightarrow> h x = g x"
  1858 proof -
  1859   from uc have cont_f: "continuous_on X f"
  1860     by (simp add: uniformly_continuous_imp_continuous)
  1861   obtain y where y: "(f \<longlongrightarrow> y x) (at x within X)" if "x \<in> closure X" for x
  1862     apply atomize_elim
  1863     apply (rule choice)
  1864     using uniformly_continuous_on_extension_at_closure[OF assms]
  1865     by metis
  1866   let ?g = "\<lambda>x. if x \<in> X then f x else y x"
  1867 
  1868   have "uniformly_continuous_on (closure X) ?g"
  1869     unfolding uniformly_continuous_on_def
  1870   proof safe
  1871     fix e::real assume "e > 0"
  1872     define e' where "e' \<equiv> e / 3"
  1873     have "e' > 0" using \<open>e > 0\<close> by (simp add: e'_def)
  1874     from uc[unfolded uniformly_continuous_on_def, rule_format, OF \<open>0 < e'\<close>]
  1875     obtain d where "d > 0" and d: "\<And>x x'. x \<in> X \<Longrightarrow> x' \<in> X \<Longrightarrow> dist x' x < d \<Longrightarrow> dist (f x') (f x) < e'"
  1876       by auto
  1877     define d' where "d' = d / 3"
  1878     have "d' > 0" using \<open>d > 0\<close> by (simp add: d'_def)
  1879     show "\<exists>d>0. \<forall>x\<in>closure X. \<forall>x'\<in>closure X. dist x' x < d \<longrightarrow> dist (?g x') (?g x) < e"
  1880     proof (safe intro!: exI[where x=d'] \<open>d' > 0\<close>)
  1881       fix x x' assume x: "x \<in> closure X" and x': "x' \<in> closure X" and dist: "dist x' x < d'"
  1882       then obtain xs xs' where xs: "xs \<longlonglongrightarrow> x" "\<And>n. xs n \<in> X"
  1883         and xs': "xs' \<longlonglongrightarrow> x'" "\<And>n. xs' n \<in> X"
  1884         by (auto simp: closure_sequential)
  1885       have "\<forall>\<^sub>F n in sequentially. dist (xs' n) x' < d'"
  1886         and "\<forall>\<^sub>F n in sequentially. dist (xs n) x < d'"
  1887         by (auto intro!: \<open>0 < d'\<close> order_tendstoD tendsto_eq_intros xs xs')
  1888       moreover
  1889       have "(\<lambda>x. f (xs x)) \<longlonglongrightarrow> y x" if "x \<in> closure X" "x \<notin> X" "xs \<longlonglongrightarrow> x" "\<And>n. xs n \<in> X" for xs x
  1890         using that not_eventuallyD
  1891         by (force intro!: filterlim_compose[OF y[OF \<open>x \<in> closure X\<close>]] simp: filterlim_at)
  1892       then have "(\<lambda>x. f (xs' x)) \<longlonglongrightarrow> ?g x'" "(\<lambda>x. f (xs x)) \<longlonglongrightarrow> ?g x"
  1893         using x x'
  1894         by (auto intro!: continuous_on_tendsto_compose[OF cont_f] simp: xs' xs)
  1895       then have "\<forall>\<^sub>F n in sequentially. dist (f (xs' n)) (?g x') < e'"
  1896         "\<forall>\<^sub>F n in sequentially. dist (f (xs n)) (?g x) < e'"
  1897         by (auto intro!: \<open>0 < e'\<close> order_tendstoD tendsto_eq_intros)
  1898       ultimately
  1899       have "\<forall>\<^sub>F n in sequentially. dist (?g x') (?g x) < e"
  1900       proof eventually_elim
  1901         case (elim n)
  1902         have "dist (?g x') (?g x) \<le>
  1903           dist (f (xs' n)) (?g x') + dist (f (xs' n)) (f (xs n)) + dist (f (xs n)) (?g x)"
  1904           by (metis add.commute add_le_cancel_left dist_commute dist_triangle dist_triangle_le)
  1905         also
  1906         {
  1907           have "dist (xs' n) (xs n) \<le> dist (xs' n) x' + dist x' x + dist (xs n) x"
  1908             by (metis add.commute add_le_cancel_left  dist_triangle dist_triangle_le)
  1909           also note \<open>dist (xs' n) x' < d'\<close>
  1910           also note \<open>dist x' x < d'\<close>
  1911           also note \<open>dist (xs n) x < d'\<close>
  1912           finally have "dist (xs' n) (xs n) < d" by (simp add: d'_def)
  1913         }
  1914         with \<open>xs _ \<in> X\<close> \<open>xs' _ \<in> X\<close> have "dist (f (xs' n)) (f (xs n)) < e'"
  1915           by (rule d)
  1916         also note \<open>dist (f (xs' n)) (?g x') < e'\<close>
  1917         also note \<open>dist (f (xs n)) (?g x) < e'\<close>
  1918         finally show ?case by (simp add: e'_def)
  1919       qed
  1920       then show "dist (?g x') (?g x) < e" by simp
  1921     qed
  1922   qed
  1923   moreover have "f x = ?g x" if "x \<in> X" for x using that by simp
  1924   moreover
  1925   {
  1926     fix Y h x
  1927     assume Y: "x \<in> Y" "X \<subseteq> Y" "Y \<subseteq> closure X" and cont_h: "continuous_on Y h"
  1928       and extension: "(\<And>x. x \<in> X \<Longrightarrow> f x = h x)"
  1929     {
  1930       assume "x \<notin> X"
  1931       have "x \<in> closure X" using Y by auto
  1932       then obtain xs where xs: "xs \<longlonglongrightarrow> x" "\<And>n. xs n \<in> X"
  1933         by (auto simp: closure_sequential)
  1934       from continuous_on_tendsto_compose[OF cont_h xs(1)] xs(2) Y
  1935       have hx: "(\<lambda>x. f (xs x)) \<longlonglongrightarrow> h x"
  1936         by (auto simp: set_mp extension)
  1937       then have "(\<lambda>x. f (xs x)) \<longlonglongrightarrow> y x"
  1938         using \<open>x \<notin> X\<close> not_eventuallyD xs(2)
  1939         by (force intro!: filterlim_compose[OF y[OF \<open>x \<in> closure X\<close>]] simp: filterlim_at xs)
  1940       with hx have "h x = y x" by (rule LIMSEQ_unique)
  1941     } then
  1942     have "h x = ?g x"
  1943       using extension by auto
  1944   }
  1945   ultimately show ?thesis ..
  1946 qed
  1947 
  1948 lemma bounded_uniformly_continuous_image:
  1949   fixes f :: "'a :: heine_borel \<Rightarrow> 'b :: heine_borel"
  1950   assumes "uniformly_continuous_on S f" "bounded S"
  1951   shows "bounded(f ` S)"
  1952   by (metis (no_types, lifting) assms bounded_closure_image compact_closure compact_continuous_image compact_eq_bounded_closed image_cong uniformly_continuous_imp_continuous uniformly_continuous_on_extension_on_closure)
  1953 
  1954 subsection \<open>Making a continuous function avoid some value in a neighbourhood.\<close>
  1955 
  1956 lemma continuous_within_avoid:
  1957   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"
  1958   assumes "continuous (at x within s) f"
  1959     and "f x \<noteq> a"
  1960   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"
  1961 proof -
  1962   obtain U where "open U" and "f x \<in> U" and "a \<notin> U"
  1963     using t1_space [OF \<open>f x \<noteq> a\<close>] by fast
  1964   have "(f \<longlongrightarrow> f x) (at x within s)"
  1965     using assms(1) by (simp add: continuous_within)
  1966   then have "eventually (\<lambda>y. f y \<in> U) (at x within s)"
  1967     using \<open>open U\<close> and \<open>f x \<in> U\<close>
  1968     unfolding tendsto_def by fast
  1969   then have "eventually (\<lambda>y. f y \<noteq> a) (at x within s)"
  1970     using \<open>a \<notin> U\<close> by (fast elim: eventually_mono)
  1971   then show ?thesis
  1972     using \<open>f x \<noteq> a\<close> by (auto simp: dist_commute eventually_at)
  1973 qed
  1974 
  1975 lemma continuous_at_avoid:
  1976   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"
  1977   assumes "continuous (at x) f"
  1978     and "f x \<noteq> a"
  1979   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
  1980   using assms continuous_within_avoid[of x UNIV f a] by simp
  1981 
  1982 lemma continuous_on_avoid:
  1983   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"
  1984   assumes "continuous_on s f"
  1985     and "x \<in> s"
  1986     and "f x \<noteq> a"
  1987   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"
  1988   using assms(1)[unfolded continuous_on_eq_continuous_within, THEN bspec[where x=x],
  1989     OF assms(2)] continuous_within_avoid[of x s f a]
  1990   using assms(3)
  1991   by auto
  1992 
  1993 lemma continuous_on_open_avoid:
  1994   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"
  1995   assumes "continuous_on s f"
  1996     and "open s"
  1997     and "x \<in> s"
  1998     and "f x \<noteq> a"
  1999   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
  2000   using assms(1)[unfolded continuous_on_eq_continuous_at[OF assms(2)], THEN bspec[where x=x], OF assms(3)]
  2001   using continuous_at_avoid[of x f a] assms(4)
  2002   by auto
  2003 
  2004 subsection\<open>Quotient maps\<close>
  2005 
  2006 lemma quotient_map_imp_continuous_open:
  2007   assumes T: "f ` S \<subseteq> T"
  2008       and ope: "\<And>U. U \<subseteq> T
  2009               \<Longrightarrow> (openin (subtopology euclidean S) (S \<inter> f -` U) \<longleftrightarrow>
  2010                    openin (subtopology euclidean T) U)"
  2011     shows "continuous_on S f"
  2012 proof -
  2013   have [simp]: "S \<inter> f -` f ` S = S" by auto
  2014   show ?thesis
  2015     using ope [OF T]
  2016     apply (simp add: continuous_on_open)
  2017     by (meson ope openin_imp_subset openin_trans)
  2018 qed
  2019 
  2020 lemma quotient_map_imp_continuous_closed:
  2021   assumes T: "f ` S \<subseteq> T"
  2022       and ope: "\<And>U. U \<subseteq> T
  2023                   \<Longrightarrow> (closedin (subtopology euclidean S) (S \<inter> f -` U) \<longleftrightarrow>
  2024                        closedin (subtopology euclidean T) U)"
  2025     shows "continuous_on S f"
  2026 proof -
  2027   have [simp]: "S \<inter> f -` f ` S = S" by auto
  2028   show ?thesis
  2029     using ope [OF T]
  2030     apply (simp add: continuous_on_closed)
  2031     by (metis (no_types, lifting) ope closedin_imp_subset closedin_trans)
  2032 qed
  2033 
  2034 lemma open_map_imp_quotient_map:
  2035   assumes contf: "continuous_on S f"
  2036       and T: "T \<subseteq> f ` S"
  2037       and ope: "\<And>T. openin (subtopology euclidean S) T
  2038                    \<Longrightarrow> openin (subtopology euclidean (f ` S)) (f ` T)"
  2039     shows "openin (subtopology euclidean S) (S \<inter> f -` T) =
  2040            openin (subtopology euclidean (f ` S)) T"
  2041 proof -
  2042   have "T = f ` (S \<inter> f -` T)"
  2043     using T by blast
  2044   then show ?thesis
  2045     using "ope" contf continuous_on_open by metis
  2046 qed
  2047 
  2048 lemma closed_map_imp_quotient_map:
  2049   assumes contf: "continuous_on S f"
  2050       and T: "T \<subseteq> f ` S"
  2051       and ope: "\<And>T. closedin (subtopology euclidean S) T
  2052               \<Longrightarrow> closedin (subtopology euclidean (f ` S)) (f ` T)"
  2053     shows "openin (subtopology euclidean S) (S \<inter> f -` T) \<longleftrightarrow>
  2054            openin (subtopology euclidean (f ` S)) T"
  2055           (is "?lhs = ?rhs")
  2056 proof
  2057   assume ?lhs
  2058   then have *: "closedin (subtopology euclidean S) (S - (S \<inter> f -` T))"
  2059     using closedin_diff by fastforce
  2060   have [simp]: "(f ` S - f ` (S - (S \<inter> f -` T))) = T"
  2061     using T by blast
  2062   show ?rhs
  2063     using ope [OF *, unfolded closedin_def] by auto
  2064 next
  2065   assume ?rhs
  2066   with contf show ?lhs
  2067     by (auto simp: continuous_on_open)
  2068 qed
  2069 
  2070 lemma continuous_right_inverse_imp_quotient_map:
  2071   assumes contf: "continuous_on S f" and imf: "f ` S \<subseteq> T"
  2072       and contg: "continuous_on T g" and img: "g ` T \<subseteq> S"
  2073       and fg [simp]: "\<And>y. y \<in> T \<Longrightarrow> f(g y) = y"
  2074       and U: "U \<subseteq> T"
  2075     shows "openin (subtopology euclidean S) (S \<inter> f -` U) \<longleftrightarrow>
  2076            openin (subtopology euclidean T) U"
  2077           (is "?lhs = ?rhs")
  2078 proof -
  2079   have f: "\<And>Z. openin (subtopology euclidean (f ` S)) Z \<Longrightarrow>
  2080                 openin (subtopology euclidean S) (S \<inter> f -` Z)"
  2081   and  g: "\<And>Z. openin (subtopology euclidean (g ` T)) Z \<Longrightarrow>
  2082                 openin (subtopology euclidean T) (T \<inter> g -` Z)"
  2083     using contf contg by (auto simp: continuous_on_open)
  2084   show ?thesis
  2085   proof
  2086     have "T \<inter> g -` (g ` T \<inter> (S \<inter> f -` U)) = {x \<in> T. f (g x) \<in> U}"
  2087       using imf img by blast
  2088     also have "... = U"
  2089       using U by auto
  2090     finally have eq: "T \<inter> g -` (g ` T \<inter> (S \<inter> f -` U)) = U" .
  2091     assume ?lhs
  2092     then have *: "openin (subtopology euclidean (g ` T)) (g ` T \<inter> (S \<inter> f -` U))"
  2093       by (meson img openin_Int openin_subtopology_Int_subset openin_subtopology_self)
  2094     show ?rhs
  2095       using g [OF *] eq by auto
  2096   next
  2097     assume rhs: ?rhs
  2098     show ?lhs
  2099       by (metis f fg image_eqI image_subset_iff imf img openin_subopen openin_subtopology_self openin_trans rhs)
  2100   qed
  2101 qed
  2102 
  2103 lemma continuous_left_inverse_imp_quotient_map:
  2104   assumes "continuous_on S f"
  2105       and "continuous_on (f ` S) g"
  2106       and  "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x"
  2107       and "U \<subseteq> f ` S"
  2108     shows "openin (subtopology euclidean S) (S \<inter> f -` U) \<longleftrightarrow>
  2109            openin (subtopology euclidean (f ` S)) U"
  2110 apply (rule continuous_right_inverse_imp_quotient_map)
  2111 using assms apply force+
  2112 done
  2113 
  2114 
  2115 text \<open>Proving a function is constant by proving that a level set is open\<close>
  2116 
  2117 lemma continuous_levelset_openin_cases:
  2118   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  2119   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
  2120         openin (subtopology euclidean s) {x \<in> s. f x = a}
  2121         \<Longrightarrow> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"
  2122   unfolding connected_clopen
  2123   using continuous_closedin_preimage_constant by auto
  2124 
  2125 lemma continuous_levelset_openin:
  2126   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  2127   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
  2128         openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>
  2129         (\<exists>x \<in> s. f x = a)  \<Longrightarrow> (\<forall>x \<in> s. f x = a)"
  2130   using continuous_levelset_openin_cases[of s f ]
  2131   by meson
  2132 
  2133 lemma continuous_levelset_open:
  2134   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  2135   assumes "connected s"
  2136     and "continuous_on s f"
  2137     and "open {x \<in> s. f x = a}"
  2138     and "\<exists>x \<in> s.  f x = a"
  2139   shows "\<forall>x \<in> s. f x = a"
  2140   using continuous_levelset_openin[OF assms(1,2), of a, unfolded openin_open]
  2141   using assms (3,4)
  2142   by fast
  2143 
  2144 text \<open>Some arithmetical combinations (more to prove).\<close>
  2145 
  2146 lemma open_scaling[intro]:
  2147   fixes s :: "'a::real_normed_vector set"
  2148   assumes "c \<noteq> 0"
  2149     and "open s"
  2150   shows "open((\<lambda>x. c *\<^sub>R x) ` s)"
  2151 proof -
  2152   {
  2153     fix x
  2154     assume "x \<in> s"
  2155     then obtain e where "e>0"
  2156       and e:"\<forall>x'. dist x' x < e \<longrightarrow> x' \<in> s" using assms(2)[unfolded open_dist, THEN bspec[where x=x]]
  2157       by auto
  2158     have "e * \<bar>c\<bar> > 0"
  2159       using assms(1)[unfolded zero_less_abs_iff[symmetric]] \<open>e>0\<close> by auto
  2160     moreover
  2161     {
  2162       fix y
  2163       assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"
  2164       then have "norm ((1 / c) *\<^sub>R y - x) < e"
  2165         unfolding dist_norm
  2166         using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)
  2167           assms(1)[unfolded zero_less_abs_iff[symmetric]] by (simp del:zero_less_abs_iff)
  2168       then have "y \<in> op *\<^sub>R c ` s"
  2169         using rev_image_eqI[of "(1 / c) *\<^sub>R y" s y "op *\<^sub>R c"]
  2170         using e[THEN spec[where x="(1 / c) *\<^sub>R y"]]
  2171         using assms(1)
  2172         unfolding dist_norm scaleR_scaleR
  2173         by auto
  2174     }
  2175     ultimately have "\<exists>e>0. \<forall>x'. dist x' (c *\<^sub>R x) < e \<longrightarrow> x' \<in> op *\<^sub>R c ` s"
  2176       apply (rule_tac x="e * \<bar>c\<bar>" in exI, auto)
  2177       done
  2178   }
  2179   then show ?thesis unfolding open_dist by auto
  2180 qed
  2181 
  2182 lemma minus_image_eq_vimage:
  2183   fixes A :: "'a::ab_group_add set"
  2184   shows "(\<lambda>x. - x) ` A = (\<lambda>x. - x) -` A"
  2185   by (auto intro!: image_eqI [where f="\<lambda>x. - x"])
  2186 
  2187 lemma open_negations:
  2188   fixes S :: "'a::real_normed_vector set"
  2189   shows "open S \<Longrightarrow> open ((\<lambda>x. - x) ` S)"
  2190   using open_scaling [of "- 1" S] by simp
  2191 
  2192 lemma open_translation:
  2193   fixes S :: "'a::real_normed_vector set"
  2194   assumes "open S"
  2195   shows "open((\<lambda>x. a + x) ` S)"
  2196 proof -
  2197   {
  2198     fix x
  2199     have "continuous (at x) (\<lambda>x. x - a)"
  2200       by (intro continuous_diff continuous_ident continuous_const)
  2201   }
  2202   moreover have "{x. x - a \<in> S} = op + a ` S"
  2203     by force
  2204   ultimately show ?thesis
  2205     by (metis assms continuous_open_vimage vimage_def)
  2206 qed
  2207 
  2208 lemma open_affinity:
  2209   fixes S :: "'a::real_normed_vector set"
  2210   assumes "open S"  "c \<noteq> 0"
  2211   shows "open ((\<lambda>x. a + c *\<^sub>R x) ` S)"
  2212 proof -
  2213   have *: "(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)"
  2214     unfolding o_def ..
  2215   have "op + a ` op *\<^sub>R c ` S = (op + a \<circ> op *\<^sub>R c) ` S"
  2216     by auto
  2217   then show ?thesis
  2218     using assms open_translation[of "op *\<^sub>R c ` S" a]
  2219     unfolding *
  2220     by auto
  2221 qed
  2222 
  2223 lemma interior_translation:
  2224   fixes S :: "'a::real_normed_vector set"
  2225   shows "interior ((\<lambda>x. a + x) ` S) = (\<lambda>x. a + x) ` (interior S)"
  2226 proof (rule set_eqI, rule)
  2227   fix x
  2228   assume "x \<in> interior (op + a ` S)"
  2229   then obtain e where "e > 0" and e: "ball x e \<subseteq> op + a ` S"
  2230     unfolding mem_interior by auto
  2231   then have "ball (x - a) e \<subseteq> S"
  2232     unfolding subset_eq Ball_def mem_ball dist_norm
  2233     by (auto simp: diff_diff_eq)
  2234   then show "x \<in> op + a ` interior S"
  2235     unfolding image_iff
  2236     apply (rule_tac x="x - a" in bexI)
  2237     unfolding mem_interior
  2238     using \<open>e > 0\<close>
  2239     apply auto
  2240     done
  2241 next
  2242   fix x
  2243   assume "x \<in> op + a ` interior S"
  2244   then obtain y e where "e > 0" and e: "ball y e \<subseteq> S" and y: "x = a + y"
  2245     unfolding image_iff Bex_def mem_interior by auto
  2246   {
  2247     fix z
  2248     have *: "a + y - z = y + a - z" by auto
  2249     assume "z \<in> ball x e"
  2250     then have "z - a \<in> S"
  2251       using e[unfolded subset_eq, THEN bspec[where x="z - a"]]
  2252       unfolding mem_ball dist_norm y group_add_class.diff_diff_eq2 *
  2253       by auto
  2254     then have "z \<in> op + a ` S"
  2255       unfolding image_iff by (auto intro!: bexI[where x="z - a"])
  2256   }
  2257   then have "ball x e \<subseteq> op + a ` S"
  2258     unfolding subset_eq by auto
  2259   then show "x \<in> interior (op + a ` S)"
  2260     unfolding mem_interior using \<open>e > 0\<close> by auto
  2261 qed
  2262 
  2263 subsection \<open>Continuity implies uniform continuity on a compact domain.\<close>
  2264 
  2265 text\<open>From the proof of the Heine-Borel theorem: Lemma 2 in section 3.7, page 69 of
  2266 J. C. Burkill and H. Burkill. A Second Course in Mathematical Analysis (CUP, 2002)\<close>
  2267 
  2268 lemma Heine_Borel_lemma:
  2269   assumes "compact S" and Ssub: "S \<subseteq> \<Union>\<G>" and op: "\<And>G. G \<in> \<G> \<Longrightarrow> open G"
  2270   obtains e where "0 < e" "\<And>x. x \<in> S \<Longrightarrow> \<exists>G \<in> \<G>. ball x e \<subseteq> G"
  2271 proof -
  2272   have False if neg: "\<And>e. 0 < e \<Longrightarrow> \<exists>x \<in> S. \<forall>G \<in> \<G>. \<not> ball x e \<subseteq> G"
  2273   proof -
  2274     have "\<exists>x \<in> S. \<forall>G \<in> \<G>. \<not> ball x (1 / Suc n) \<subseteq> G" for n
  2275       using neg by simp
  2276     then obtain f where "\<And>n. f n \<in> S" and fG: "\<And>G n. G \<in> \<G> \<Longrightarrow> \<not> ball (f n) (1 / Suc n) \<subseteq> G"
  2277       by metis
  2278     then obtain l r where "l \<in> S" "strict_mono r" and to_l: "(f \<circ> r) \<longlonglongrightarrow> l"
  2279       using \<open>compact S\<close> compact_def that by metis
  2280     then obtain G where "l \<in> G" "G \<in> \<G>"
  2281       using Ssub by auto
  2282     then obtain e where "0 < e" and e: "\<And>z. dist z l < e \<Longrightarrow> z \<in> G"
  2283       using op open_dist by blast
  2284     obtain N1 where N1: "\<And>n. n \<ge> N1 \<Longrightarrow> dist (f (r n)) l < e/2"
  2285       using to_l apply (simp add: lim_sequentially)
  2286       using \<open>0 < e\<close> half_gt_zero that by blast
  2287     obtain N2 where N2: "of_nat N2 > 2/e"
  2288       using reals_Archimedean2 by blast
  2289     obtain x where "x \<in> ball (f (r (max N1 N2))) (1 / real (Suc (r (max N1 N2))))" and "x \<notin> G"
  2290       using fG [OF \<open>G \<in> \<G>\<close>, of "r (max N1 N2)"] by blast
  2291     then have "dist (f (r (max N1 N2))) x < 1 / real (Suc (r (max N1 N2)))"
  2292       by simp
  2293     also have "... \<le> 1 / real (Suc (max N1 N2))"
  2294       apply (simp add: divide_simps del: max.bounded_iff)
  2295       using \<open>strict_mono r\<close> seq_suble by blast
  2296     also have "... \<le> 1 / real (Suc N2)"
  2297       by (simp add: field_simps)
  2298     also have "... < e/2"
  2299       using N2 \<open>0 < e\<close> by (simp add: field_simps)
  2300     finally have "dist (f (r (max N1 N2))) x < e / 2" .
  2301     moreover have "dist (f (r (max N1 N2))) l < e/2"
  2302       using N1 max.cobounded1 by blast
  2303     ultimately have "dist x l < e"
  2304       using dist_triangle_half_r by blast
  2305     then show ?thesis
  2306       using e \<open>x \<notin> G\<close> by blast
  2307   qed
  2308   then show ?thesis
  2309     by (meson that)
  2310 qed
  2311 
  2312 lemma compact_uniformly_equicontinuous:
  2313   assumes "compact S"
  2314       and cont: "\<And>x e. \<lbrakk>x \<in> S; 0 < e\<rbrakk>
  2315                         \<Longrightarrow> \<exists>d. 0 < d \<and>
  2316                                 (\<forall>f \<in> \<F>. \<forall>x' \<in> S. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
  2317       and "0 < e"
  2318   obtains d where "0 < d"
  2319                   "\<And>f x x'. \<lbrakk>f \<in> \<F>; x \<in> S; x' \<in> S; dist x' x < d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
  2320 proof -
  2321   obtain d where d_pos: "\<And>x e. \<lbrakk>x \<in> S; 0 < e\<rbrakk> \<Longrightarrow> 0 < d x e"
  2322      and d_dist : "\<And>x x' e f. \<lbrakk>dist x' x < d x e; x \<in> S; x' \<in> S; 0 < e; f \<in> \<F>\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
  2323     using cont by metis
  2324   let ?\<G> = "((\<lambda>x. ball x (d x (e / 2))) ` S)"
  2325   have Ssub: "S \<subseteq> \<Union> ?\<G>"
  2326     by clarsimp (metis d_pos \<open>0 < e\<close> dist_self half_gt_zero_iff)
  2327   then obtain k where "0 < k" and k: "\<And>x. x \<in> S \<Longrightarrow> \<exists>G \<in> ?\<G>. ball x k \<subseteq> G"
  2328     by (rule Heine_Borel_lemma [OF \<open>compact S\<close>]) auto
  2329   moreover have "dist (f v) (f u) < e" if "f \<in> \<F>" "u \<in> S" "v \<in> S" "dist v u < k" for f u v
  2330   proof -
  2331     obtain G where "G \<in> ?\<G>" "u \<in> G" "v \<in> G"
  2332       using k that
  2333       by (metis \<open>dist v u < k\<close> \<open>u \<in> S\<close> \<open>0 < k\<close> centre_in_ball subsetD dist_commute mem_ball)
  2334     then obtain w where w: "dist w u < d w (e / 2)" "dist w v < d w (e / 2)" "w \<in> S"
  2335       by auto
  2336     with that d_dist have "dist (f w) (f v) < e/2"
  2337       by (metis \<open>0 < e\<close> dist_commute half_gt_zero)
  2338     moreover
  2339     have "dist (f w) (f u) < e/2"
  2340       using that d_dist w by (metis \<open>0 < e\<close> dist_commute divide_pos_pos zero_less_numeral)
  2341     ultimately show ?thesis
  2342       using dist_triangle_half_r by blast
  2343   qed
  2344   ultimately show ?thesis using that by blast
  2345 qed
  2346 
  2347 corollary compact_uniformly_continuous:
  2348   fixes f :: "'a :: metric_space \<Rightarrow> 'b :: metric_space"
  2349   assumes f: "continuous_on S f" and S: "compact S"
  2350   shows "uniformly_continuous_on S f"
  2351   using f
  2352     unfolding continuous_on_iff uniformly_continuous_on_def
  2353     by (force intro: compact_uniformly_equicontinuous [OF S, of "{f}"])
  2354 
  2355 subsection \<open>Topological stuff about the set of Reals\<close>
  2356 
  2357 lemma open_real:
  2358   fixes s :: "real set"
  2359   shows "open s \<longleftrightarrow> (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. \<bar>x' - x\<bar> < e --> x' \<in> s)"
  2360   unfolding open_dist dist_norm by simp
  2361 
  2362 lemma islimpt_approachable_real:
  2363   fixes s :: "real set"
  2364   shows "x islimpt s \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> s. x' \<noteq> x \<and> \<bar>x' - x\<bar> < e)"
  2365   unfolding islimpt_approachable dist_norm by simp
  2366 
  2367 lemma closed_real:
  2368   fixes s :: "real set"
  2369   shows "closed s \<longleftrightarrow> (\<forall>x. (\<forall>e>0.  \<exists>x' \<in> s. x' \<noteq> x \<and> \<bar>x' - x\<bar> < e) \<longrightarrow> x \<in> s)"
  2370   unfolding closed_limpt islimpt_approachable dist_norm by simp
  2371 
  2372 lemma continuous_at_real_range:
  2373   fixes f :: "'a::real_normed_vector \<Rightarrow> real"
  2374   shows "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. \<forall>x'. norm(x' - x) < d --> \<bar>f x' - f x\<bar> < e)"
  2375   unfolding continuous_at
  2376   unfolding Lim_at
  2377   unfolding dist_norm
  2378   apply auto
  2379   apply (erule_tac x=e in allE, auto)
  2380   apply (rule_tac x=d in exI, auto)
  2381   apply (erule_tac x=x' in allE, auto)
  2382   apply (erule_tac x=e in allE, auto)
  2383   done
  2384 
  2385 lemma continuous_on_real_range:
  2386   fixes f :: "'a::real_normed_vector \<Rightarrow> real"
  2387   shows "continuous_on s f \<longleftrightarrow>
  2388     (\<forall>x \<in> s. \<forall>e>0. \<exists>d>0. (\<forall>x' \<in> s. norm(x' - x) < d \<longrightarrow> \<bar>f x' - f x\<bar> < e))"
  2389   unfolding continuous_on_iff dist_norm by simp
  2390 
  2391 text \<open>Hence some handy theorems on distance, diameter etc. of/from a set.\<close>
  2392 
  2393 lemma distance_attains_sup:
  2394   assumes "compact s" "s \<noteq> {}"
  2395   shows "\<exists>x\<in>s. \<forall>y\<in>s. dist a y \<le> dist a x"
  2396 proof (rule continuous_attains_sup [OF assms])
  2397   {
  2398     fix x
  2399     assume "x\<in>s"
  2400     have "(dist a \<longlongrightarrow> dist a x) (at x within s)"
  2401       by (intro tendsto_dist tendsto_const tendsto_ident_at)
  2402   }
  2403   then show "continuous_on s (dist a)"
  2404     unfolding continuous_on ..
  2405 qed
  2406 
  2407 text \<open>For \emph{minimal} distance, we only need closure, not compactness.\<close>
  2408 
  2409 lemma distance_attains_inf:
  2410   fixes a :: "'a::heine_borel"
  2411   assumes "closed s" and "s \<noteq> {}"
  2412   obtains x where "x\<in>s" "\<And>y. y \<in> s \<Longrightarrow> dist a x \<le> dist a y"
  2413 proof -
  2414   from assms obtain b where "b \<in> s" by auto
  2415   let ?B = "s \<inter> cball a (dist b a)"
  2416   have "?B \<noteq> {}" using \<open>b \<in> s\<close>
  2417     by (auto simp: dist_commute)
  2418   moreover have "continuous_on ?B (dist a)"
  2419     by (auto intro!: continuous_at_imp_continuous_on continuous_dist continuous_ident continuous_const)
  2420   moreover have "compact ?B"
  2421     by (intro closed_Int_compact \<open>closed s\<close> compact_cball)
  2422   ultimately obtain x where "x \<in> ?B" "\<forall>y\<in>?B. dist a x \<le> dist a y"
  2423     by (metis continuous_attains_inf)
  2424   with that show ?thesis by fastforce
  2425 qed
  2426 
  2427 
  2428 subsection \<open>Cartesian products\<close>
  2429 
  2430 lemma bounded_Times:
  2431   assumes "bounded s" "bounded t"
  2432   shows "bounded (s \<times> t)"
  2433 proof -
  2434   obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"
  2435     using assms [unfolded bounded_def] by auto
  2436   then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<^sup>2 + b\<^sup>2)"
  2437     by (auto simp: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)
  2438   then show ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto
  2439 qed
  2440 
  2441 lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"
  2442   by (induct x) simp
  2443 
  2444 lemma seq_compact_Times: "seq_compact s \<Longrightarrow> seq_compact t \<Longrightarrow> seq_compact (s \<times> t)"
  2445   unfolding seq_compact_def
  2446   apply clarify
  2447   apply (drule_tac x="fst \<circ> f" in spec)
  2448   apply (drule mp, simp add: mem_Times_iff)
  2449   apply (clarify, rename_tac l1 r1)
  2450   apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)
  2451   apply (drule mp, simp add: mem_Times_iff)
  2452   apply (clarify, rename_tac l2 r2)
  2453   apply (rule_tac x="(l1, l2)" in rev_bexI, simp)
  2454   apply (rule_tac x="r1 \<circ> r2" in exI)
  2455   apply (rule conjI, simp add: strict_mono_def)
  2456   apply (drule_tac f=r2 in LIMSEQ_subseq_LIMSEQ, assumption)
  2457   apply (drule (1) tendsto_Pair) back
  2458   apply (simp add: o_def)
  2459   done
  2460 
  2461 lemma compact_Times:
  2462   assumes "compact s" "compact t"
  2463   shows "compact (s \<times> t)"
  2464 proof (rule compactI)
  2465   fix C
  2466   assume C: "\<forall>t\<in>C. open t" "s \<times> t \<subseteq> \<Union>C"
  2467   have "\<forall>x\<in>s. \<exists>a. open a \<and> x \<in> a \<and> (\<exists>d\<subseteq>C. finite d \<and> a \<times> t \<subseteq> \<Union>d)"
  2468   proof
  2469     fix x
  2470     assume "x \<in> s"
  2471     have "\<forall>y\<in>t. \<exists>a b c. c \<in> C \<and> open a \<and> open b \<and> x \<in> a \<and> y \<in> b \<and> a \<times> b \<subseteq> c" (is "\<forall>y\<in>t. ?P y")
  2472     proof
  2473       fix y
  2474       assume "y \<in> t"
  2475       with \<open>x \<in> s\<close> C obtain c where "c \<in> C" "(x, y) \<in> c" "open c" by auto
  2476       then show "?P y" by (auto elim!: open_prod_elim)
  2477     qed
  2478     then obtain a b c where b: "\<And>y. y \<in> t \<Longrightarrow> open (b y)"
  2479       and c: "\<And>y. y \<in> t \<Longrightarrow> c y \<in> C \<and> open (a y) \<and> open (b y) \<and> x \<in> a y \<and> y \<in> b y \<and> a y \<times> b y \<subseteq> c y"
  2480       by metis
  2481     then have "\<forall>y\<in>t. open (b y)" "t \<subseteq> (\<Union>y\<in>t. b y)" by auto
  2482     with compactE_image[OF \<open>compact t\<close>] obtain D where D: "D \<subseteq> t" "finite D" "t \<subseteq> (\<Union>y\<in>D. b y)"
  2483       by metis
  2484     moreover from D c have "(\<Inter>y\<in>D. a y) \<times> t \<subseteq> (\<Union>y\<in>D. c y)"
  2485       by (fastforce simp: subset_eq)
  2486     ultimately show "\<exists>a. open a \<and> x \<in> a \<and> (\<exists>d\<subseteq>C. finite d \<and> a \<times> t \<subseteq> \<Union>d)"
  2487       using c by (intro exI[of _ "c`D"] exI[of _ "\<Inter>(a`D)"] conjI) (auto intro!: open_INT)
  2488   qed
  2489   then obtain a d where a: "\<And>x. x\<in>s \<Longrightarrow> open (a x)" "s \<subseteq> (\<Union>x\<in>s. a x)"
  2490     and d: "\<And>x. x \<in> s \<Longrightarrow> d x \<subseteq> C \<and> finite (d x) \<and> a x \<times> t \<subseteq> \<Union>d x"
  2491     unfolding subset_eq UN_iff by metis
  2492   moreover
  2493   from compactE_image[OF \<open>compact s\<close> a]
  2494   obtain e where e: "e \<subseteq> s" "finite e" and s: "s \<subseteq> (\<Union>x\<in>e. a x)"
  2495     by auto
  2496   moreover
  2497   {
  2498     from s have "s \<times> t \<subseteq> (\<Union>x\<in>e. a x \<times> t)"
  2499       by auto
  2500     also have "\<dots> \<subseteq> (\<Union>x\<in>e. \<Union>d x)"
  2501       using d \<open>e \<subseteq> s\<close> by (intro UN_mono) auto
  2502     finally have "s \<times> t \<subseteq> (\<Union>x\<in>e. \<Union>d x)" .
  2503   }
  2504   ultimately show "\<exists>C'\<subseteq>C. finite C' \<and> s \<times> t \<subseteq> \<Union>C'"
  2505     by (intro exI[of _ "(\<Union>x\<in>e. d x)"]) (auto simp: subset_eq)
  2506 qed
  2507 
  2508 text\<open>Hence some useful properties follow quite easily.\<close>
  2509 
  2510 lemma compact_scaling:
  2511   fixes s :: "'a::real_normed_vector set"
  2512   assumes "compact s"
  2513   shows "compact ((\<lambda>x. c *\<^sub>R x) ` s)"
  2514 proof -
  2515   let ?f = "\<lambda>x. scaleR c x"
  2516   have *: "bounded_linear ?f" by (rule bounded_linear_scaleR_right)
  2517   show ?thesis
  2518     using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]
  2519     using linear_continuous_at[OF *] assms
  2520     by auto
  2521 qed
  2522 
  2523 lemma compact_negations:
  2524   fixes s :: "'a::real_normed_vector set"
  2525   assumes "compact s"
  2526   shows "compact ((\<lambda>x. - x) ` s)"
  2527   using compact_scaling [OF assms, of "- 1"] by auto
  2528 
  2529 lemma compact_sums:
  2530   fixes s t :: "'a::real_normed_vector set"
  2531   assumes "compact s"
  2532     and "compact t"
  2533   shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"
  2534 proof -
  2535   have *: "{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z) ` (s \<times> t)"
  2536     apply auto
  2537     unfolding image_iff
  2538     apply (rule_tac x="(xa, y)" in bexI)
  2539     apply auto
  2540     done
  2541   have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"
  2542     unfolding continuous_on by (rule ballI) (intro tendsto_intros)
  2543   then show ?thesis
  2544     unfolding * using compact_continuous_image compact_Times [OF assms] by auto
  2545 qed
  2546 
  2547 lemma compact_differences:
  2548   fixes s t :: "'a::real_normed_vector set"
  2549   assumes "compact s"
  2550     and "compact t"
  2551   shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"
  2552 proof-
  2553   have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus ` t)}"
  2554     apply auto
  2555     apply (rule_tac x= xa in exI, auto)
  2556     done
  2557   then show ?thesis
  2558     using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto
  2559 qed
  2560 
  2561 lemma compact_translation:
  2562   fixes s :: "'a::real_normed_vector set"
  2563   assumes "compact s"
  2564   shows "compact ((\<lambda>x. a + x) ` s)"
  2565 proof -
  2566   have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x) ` s"
  2567     by auto
  2568   then show ?thesis
  2569     using compact_sums[OF assms compact_sing[of a]] by auto
  2570 qed
  2571 
  2572 lemma compact_affinity:
  2573   fixes s :: "'a::real_normed_vector set"
  2574   assumes "compact s"
  2575   shows "compact ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  2576 proof -
  2577   have "op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s"
  2578     by auto
  2579   then show ?thesis
  2580     using compact_translation[OF compact_scaling[OF assms], of a c] by auto
  2581 qed
  2582 
  2583 text \<open>Hence we get the following.\<close>
  2584 
  2585 lemma compact_sup_maxdistance:
  2586   fixes s :: "'a::metric_space set"
  2587   assumes "compact s"
  2588     and "s \<noteq> {}"
  2589   shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. dist u v \<le> dist x y"
  2590 proof -
  2591   have "compact (s \<times> s)"
  2592     using \<open>compact s\<close> by (intro compact_Times)
  2593   moreover have "s \<times> s \<noteq> {}"
  2594     using \<open>s \<noteq> {}\<close> by auto
  2595   moreover have "continuous_on (s \<times> s) (\<lambda>x. dist (fst x) (snd x))"
  2596     by (intro continuous_at_imp_continuous_on ballI continuous_intros)
  2597   ultimately show ?thesis
  2598     using continuous_attains_sup[of "s \<times> s" "\<lambda>x. dist (fst x) (snd x)"] by auto
  2599 qed
  2600 
  2601 
  2602 subsection \<open>The diameter of a set.\<close>
  2603 
  2604 definition diameter :: "'a::metric_space set \<Rightarrow> real" where
  2605   "diameter S = (if S = {} then 0 else SUP (x,y):S\<times>S. dist x y)"
  2606 
  2607 lemma diameter_empty [simp]: "diameter{} = 0"
  2608   by (auto simp: diameter_def)
  2609 
  2610 lemma diameter_singleton [simp]: "diameter{x} = 0"
  2611   by (auto simp: diameter_def)
  2612 
  2613 lemma diameter_le:
  2614   assumes "S \<noteq> {} \<or> 0 \<le> d"
  2615       and no: "\<And>x y. \<lbrakk>x \<in> S; y \<in> S\<rbrakk> \<Longrightarrow> norm(x - y) \<le> d"
  2616     shows "diameter S \<le> d"
  2617 using assms
  2618   by (auto simp: dist_norm diameter_def intro: cSUP_least)
  2619 
  2620 lemma diameter_bounded_bound:
  2621   fixes s :: "'a :: metric_space set"
  2622   assumes s: "bounded s" "x \<in> s" "y \<in> s"
  2623   shows "dist x y \<le> diameter s"
  2624 proof -
  2625   from s obtain z d where z: "\<And>x. x \<in> s \<Longrightarrow> dist z x \<le> d"
  2626     unfolding bounded_def by auto
  2627   have "bdd_above (case_prod dist ` (s\<times>s))"
  2628   proof (intro bdd_aboveI, safe)
  2629     fix a b
  2630     assume "a \<in> s" "b \<in> s"
  2631     with z[of a] z[of b] dist_triangle[of a b z]
  2632     show "dist a b \<le> 2 * d"
  2633       by (simp add: dist_commute)
  2634   qed
  2635   moreover have "(x,y) \<in> s\<times>s" using s by auto
  2636   ultimately have "dist x y \<le> (SUP (x,y):s\<times>s. dist x y)"
  2637     by (rule cSUP_upper2) simp
  2638   with \<open>x \<in> s\<close> show ?thesis
  2639     by (auto simp: diameter_def)
  2640 qed
  2641 
  2642 lemma diameter_lower_bounded:
  2643   fixes s :: "'a :: metric_space set"
  2644   assumes s: "bounded s"
  2645     and d: "0 < d" "d < diameter s"
  2646   shows "\<exists>x\<in>s. \<exists>y\<in>s. d < dist x y"
  2647 proof (rule ccontr)
  2648   assume contr: "\<not> ?thesis"
  2649   moreover have "s \<noteq> {}"
  2650     using d by (auto simp: diameter_def)
  2651   ultimately have "diameter s \<le> d"
  2652     by (auto simp: not_less diameter_def intro!: cSUP_least)
  2653   with \<open>d < diameter s\<close> show False by auto
  2654 qed
  2655 
  2656 lemma diameter_bounded:
  2657   assumes "bounded s"
  2658   shows "\<forall>x\<in>s. \<forall>y\<in>s. dist x y \<le> diameter s"
  2659     and "\<forall>d>0. d < diameter s \<longrightarrow> (\<exists>x\<in>s. \<exists>y\<in>s. dist x y > d)"
  2660   using diameter_bounded_bound[of s] diameter_lower_bounded[of s] assms
  2661   by auto
  2662 
  2663 lemma diameter_compact_attained:
  2664   assumes "compact s"
  2665     and "s \<noteq> {}"
  2666   shows "\<exists>x\<in>s. \<exists>y\<in>s. dist x y = diameter s"
  2667 proof -
  2668   have b: "bounded s" using assms(1)
  2669     by (rule compact_imp_bounded)
  2670   then obtain x y where xys: "x\<in>s" "y\<in>s"
  2671     and xy: "\<forall>u\<in>s. \<forall>v\<in>s. dist u v \<le> dist x y"
  2672     using compact_sup_maxdistance[OF assms] by auto
  2673   then have "diameter s \<le> dist x y"
  2674     unfolding diameter_def
  2675     apply clarsimp
  2676     apply (rule cSUP_least, fast+)
  2677     done
  2678   then show ?thesis
  2679     by (metis b diameter_bounded_bound order_antisym xys)
  2680 qed
  2681 
  2682 lemma diameter_ge_0:
  2683   assumes "bounded S"  shows "0 \<le> diameter S"
  2684   by (metis all_not_in_conv assms diameter_bounded_bound diameter_empty dist_self order_refl)
  2685 
  2686 lemma diameter_subset:
  2687   assumes "S \<subseteq> T" "bounded T"
  2688   shows "diameter S \<le> diameter T"
  2689 proof (cases "S = {} \<or> T = {}")
  2690   case True
  2691   with assms show ?thesis
  2692     by (force simp: diameter_ge_0)
  2693 next
  2694   case False
  2695   then have "bdd_above ((\<lambda>x. case x of (x, xa) \<Rightarrow> dist x xa) ` (T \<times> T))"
  2696     using \<open>bounded T\<close> diameter_bounded_bound by (force simp: bdd_above_def)
  2697   with False \<open>S \<subseteq> T\<close> show ?thesis
  2698     apply (simp add: diameter_def)
  2699     apply (rule cSUP_subset_mono, auto)
  2700     done
  2701 qed
  2702 
  2703 lemma diameter_closure:
  2704   assumes "bounded S"
  2705   shows "diameter(closure S) = diameter S"
  2706 proof (rule order_antisym)
  2707   have "False" if "diameter S < diameter (closure S)"
  2708   proof -
  2709     define d where "d = diameter(closure S) - diameter(S)"
  2710     have "d > 0"
  2711       using that by (simp add: d_def)
  2712     then have "diameter(closure(S)) - d / 2 < diameter(closure(S))"
  2713       by simp
  2714     have dd: "diameter (closure S) - d / 2 = (diameter(closure(S)) + diameter(S)) / 2"
  2715       by (simp add: d_def divide_simps)
  2716      have bocl: "bounded (closure S)"
  2717       using assms by blast
  2718     moreover have "0 \<le> diameter S"
  2719       using assms diameter_ge_0 by blast
  2720     ultimately obtain x y where "x \<in> closure S" "y \<in> closure S" and xy: "diameter(closure(S)) - d / 2 < dist x y"
  2721       using diameter_bounded(2) [OF bocl, rule_format, of "diameter(closure(S)) - d / 2"] \<open>d > 0\<close> d_def by auto
  2722     then obtain x' y' where x'y': "x' \<in> S" "dist x' x < d/4" "y' \<in> S" "dist y' y < d/4"
  2723       using closure_approachable
  2724       by (metis \<open>0 < d\<close> zero_less_divide_iff zero_less_numeral)
  2725     then have "dist x' y' \<le> diameter S"
  2726       using assms diameter_bounded_bound by blast
  2727     with x'y' have "dist x y \<le> d / 4 + diameter S + d / 4"
  2728       by (meson add_mono_thms_linordered_semiring(1) dist_triangle dist_triangle3 less_eq_real_def order_trans)
  2729     then show ?thesis
  2730       using xy d_def by linarith
  2731   qed
  2732   then show "diameter (closure S) \<le> diameter S"
  2733     by fastforce
  2734   next
  2735     show "diameter S \<le> diameter (closure S)"
  2736       by (simp add: assms bounded_closure closure_subset diameter_subset)
  2737 qed
  2738 
  2739 lemma diameter_cball [simp]:
  2740   fixes a :: "'a::euclidean_space"
  2741   shows "diameter(cball a r) = (if r < 0 then 0 else 2*r)"
  2742 proof -
  2743   have "diameter(cball a r) = 2*r" if "r \<ge> 0"
  2744   proof (rule order_antisym)
  2745     show "diameter (cball a r) \<le> 2*r"
  2746     proof (rule diameter_le)
  2747       fix x y assume "x \<in> cball a r" "y \<in> cball a r"
  2748       then have "norm (x - a) \<le> r" "norm (a - y) \<le> r"
  2749         by (auto simp: dist_norm norm_minus_commute)
  2750       then have "norm (x - y) \<le> r+r"
  2751         using norm_diff_triangle_le by blast
  2752       then show "norm (x - y) \<le> 2*r" by simp
  2753     qed (simp add: that)
  2754     have "2*r = dist (a + r *\<^sub>R (SOME i. i \<in> Basis)) (a - r *\<^sub>R (SOME i. i \<in> Basis))"
  2755       apply (simp add: dist_norm)
  2756       by (metis abs_of_nonneg mult.right_neutral norm_numeral norm_scaleR norm_some_Basis real_norm_def scaleR_2 that)
  2757     also have "... \<le> diameter (cball a r)"
  2758       apply (rule diameter_bounded_bound)
  2759       using that by (auto simp: dist_norm)
  2760     finally show "2*r \<le> diameter (cball a r)" .
  2761   qed
  2762   then show ?thesis by simp
  2763 qed
  2764 
  2765 lemma diameter_ball [simp]:
  2766   fixes a :: "'a::euclidean_space"
  2767   shows "diameter(ball a r) = (if r < 0 then 0 else 2*r)"
  2768 proof -
  2769   have "diameter(ball a r) = 2*r" if "r > 0"
  2770     by (metis bounded_ball diameter_closure closure_ball diameter_cball less_eq_real_def linorder_not_less that)
  2771   then show ?thesis
  2772     by (simp add: diameter_def)
  2773 qed
  2774 
  2775 lemma diameter_closed_interval [simp]: "diameter {a..b} = (if b < a then 0 else b-a)"
  2776 proof -
  2777   have "{a .. b} = cball ((a+b)/2) ((b-a)/2)"
  2778     by (auto simp: dist_norm abs_if divide_simps split: if_split_asm)
  2779   then show ?thesis
  2780     by simp
  2781 qed
  2782 
  2783 lemma diameter_open_interval [simp]: "diameter {a<..<b} = (if b < a then 0 else b-a)"
  2784 proof -
  2785   have "{a <..< b} = ball ((a+b)/2) ((b-a)/2)"
  2786     by (auto simp: dist_norm abs_if divide_simps split: if_split_asm)
  2787   then show ?thesis
  2788     by simp
  2789 qed
  2790 
  2791 proposition Lebesgue_number_lemma:
  2792   assumes "compact S" "\<C> \<noteq> {}" "S \<subseteq> \<Union>\<C>" and ope: "\<And>B. B \<in> \<C> \<Longrightarrow> open B"
  2793   obtains \<delta> where "0 < \<delta>" "\<And>T. \<lbrakk>T \<subseteq> S; diameter T < \<delta>\<rbrakk> \<Longrightarrow> \<exists>B \<in> \<C>. T \<subseteq> B"
  2794 proof (cases "S = {}")
  2795   case True
  2796   then show ?thesis
  2797     by (metis \<open>\<C> \<noteq> {}\<close> zero_less_one empty_subsetI equals0I subset_trans that)
  2798 next
  2799   case False
  2800   { fix x assume "x \<in> S"
  2801     then obtain C where C: "x \<in> C" "C \<in> \<C>"
  2802       using \<open>S \<subseteq> \<Union>\<C>\<close> by blast
  2803     then obtain r where r: "r>0" "ball x (2*r) \<subseteq> C"
  2804       by (metis mult.commute mult_2_right not_le ope openE real_sum_of_halves zero_le_numeral zero_less_mult_iff)
  2805     then have "\<exists>r C. r > 0 \<and> ball x (2*r) \<subseteq> C \<and> C \<in> \<C>"
  2806       using C by blast
  2807   }
  2808   then obtain r where r: "\<And>x. x \<in> S \<Longrightarrow> r x > 0 \<and> (\<exists>C \<in> \<C>. ball x (2*r x) \<subseteq> C)"
  2809     by metis
  2810   then have "S \<subseteq> (\<Union>x \<in> S. ball x (r x))"
  2811     by auto
  2812   then obtain \<T> where "finite \<T>" "S \<subseteq> \<Union>\<T>" and \<T>: "\<T> \<subseteq> (\<lambda>x. ball x (r x)) ` S"
  2813     by (rule compactE [OF \<open>compact S\<close>]) auto
  2814   then obtain S0 where "S0 \<subseteq> S" "finite S0" and S0: "\<T> = (\<lambda>x. ball x (r x)) ` S0"
  2815     by (meson finite_subset_image)
  2816   then have "S0 \<noteq> {}"
  2817     using False \<open>S \<subseteq> \<Union>\<T>\<close> by auto
  2818   define \<delta> where "\<delta> = Inf (r ` S0)"
  2819   have "\<delta> > 0"
  2820     using \<open>finite S0\<close> \<open>S0 \<subseteq> S\<close> \<open>S0 \<noteq> {}\<close> r by (auto simp: \<delta>_def finite_less_Inf_iff)
  2821   show ?thesis
  2822   proof
  2823     show "0 < \<delta>"
  2824       by (simp add: \<open>0 < \<delta>\<close>)
  2825     show "\<exists>B \<in> \<C>. T \<subseteq> B" if "T \<subseteq> S" and dia: "diameter T < \<delta>" for T
  2826     proof (cases "T = {}")
  2827       case True
  2828       then show ?thesis
  2829         using \<open>\<C> \<noteq> {}\<close> by blast
  2830     next
  2831       case False
  2832       then obtain y where "y \<in> T" by blast
  2833       then have "y \<in> S"
  2834         using \<open>T \<subseteq> S\<close> by auto
  2835       then obtain x where "x \<in> S0" and x: "y \<in> ball x (r x)"
  2836         using \<open>S \<subseteq> \<Union>\<T>\<close> S0 that by blast
  2837       have "ball y \<delta> \<subseteq> ball y (r x)"
  2838         by (metis \<delta>_def \<open>S0 \<noteq> {}\<close> \<open>finite S0\<close> \<open>x \<in> S0\<close> empty_is_image finite_imageI finite_less_Inf_iff imageI less_irrefl not_le subset_ball)
  2839       also have "... \<subseteq> ball x (2*r x)"
  2840         by clarsimp (metis dist_commute dist_triangle_less_add mem_ball mult_2 x)
  2841       finally obtain C where "C \<in> \<C>" "ball y \<delta> \<subseteq> C"
  2842         by (meson r \<open>S0 \<subseteq> S\<close> \<open>x \<in> S0\<close> dual_order.trans subsetCE)
  2843       have "bounded T"
  2844         using \<open>compact S\<close> bounded_subset compact_imp_bounded \<open>T \<subseteq> S\<close> by blast
  2845       then have "T \<subseteq> ball y \<delta>"
  2846         using \<open>y \<in> T\<close> dia diameter_bounded_bound by fastforce
  2847       then show ?thesis
  2848         apply (rule_tac x=C in bexI)
  2849         using \<open>ball y \<delta> \<subseteq> C\<close> \<open>C \<in> \<C>\<close> by auto
  2850     qed
  2851   qed
  2852 qed
  2853 
  2854 lemma diameter_cbox:
  2855   fixes a b::"'a::euclidean_space"
  2856   shows "(\<forall>i \<in> Basis. a \<bullet> i \<le> b \<bullet> i) \<Longrightarrow> diameter (cbox a b) = dist a b"
  2857   by (force simp: diameter_def intro!: cSup_eq_maximum L2_set_mono
  2858      simp: euclidean_dist_l2[where 'a='a] cbox_def dist_norm)
  2859 
  2860 subsection \<open>Separation between points and sets\<close>
  2861 
  2862 lemma separate_point_closed:
  2863   fixes s :: "'a::heine_borel set"
  2864   assumes "closed s" and "a \<notin> s"
  2865   shows "\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x"
  2866 proof (cases "s = {}")
  2867   case True
  2868   then show ?thesis by(auto intro!: exI[where x=1])
  2869 next
  2870   case False
  2871   from assms obtain x where "x\<in>s" "\<forall>y\<in>s. dist a x \<le> dist a y"
  2872     using \<open>s \<noteq> {}\<close> by (blast intro: distance_attains_inf [of s a])
  2873   with \<open>x\<in>s\<close> show ?thesis using dist_pos_lt[of a x] and\<open>a \<notin> s\<close>
  2874     by blast
  2875 qed
  2876 
  2877 lemma separate_compact_closed:
  2878   fixes s t :: "'a::heine_borel set"
  2879   assumes "compact s"
  2880     and t: "closed t" "s \<inter> t = {}"
  2881   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
  2882 proof cases
  2883   assume "s \<noteq> {} \<and> t \<noteq> {}"
  2884   then have "s \<noteq> {}" "t \<noteq> {}" by auto
  2885   let ?inf = "\<lambda>x. infdist x t"
  2886   have "continuous_on s ?inf"
  2887     by (auto intro!: continuous_at_imp_continuous_on continuous_infdist continuous_ident)
  2888   then obtain x where x: "x \<in> s" "\<forall>y\<in>s. ?inf x \<le> ?inf y"
  2889     using continuous_attains_inf[OF \<open>compact s\<close> \<open>s \<noteq> {}\<close>] by auto
  2890   then have "0 < ?inf x"
  2891     using t \<open>t \<noteq> {}\<close> in_closed_iff_infdist_zero by (auto simp: less_le infdist_nonneg)
  2892   moreover have "\<forall>x'\<in>s. \<forall>y\<in>t. ?inf x \<le> dist x' y"
  2893     using x by (auto intro: order_trans infdist_le)
  2894   ultimately show ?thesis by auto
  2895 qed (auto intro!: exI[of _ 1])
  2896 
  2897 lemma separate_closed_compact:
  2898   fixes s t :: "'a::heine_borel set"
  2899   assumes "closed s"
  2900     and "compact t"
  2901     and "s \<inter> t = {}"
  2902   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
  2903 proof -
  2904   have *: "t \<inter> s = {}"
  2905     using assms(3) by auto
  2906   show ?thesis
  2907     using separate_compact_closed[OF assms(2,1) *] by (force simp: dist_commute)
  2908 qed
  2909 
  2910 
  2911 subsection \<open>Compact sets and the closure operation.\<close>
  2912 
  2913 lemma closed_scaling:
  2914   fixes S :: "'a::real_normed_vector set"
  2915   assumes "closed S"
  2916   shows "closed ((\<lambda>x. c *\<^sub>R x) ` S)"
  2917 proof (cases "c = 0")
  2918   case True then show ?thesis
  2919     by (auto simp: image_constant_conv)
  2920 next
  2921   case False
  2922   from assms have "closed ((\<lambda>x. inverse c *\<^sub>R x) -` S)"
  2923     by (simp add: continuous_closed_vimage)
  2924   also have "(\<lambda>x. inverse c *\<^sub>R x) -` S = (\<lambda>x. c *\<^sub>R x) ` S"
  2925     using \<open>c \<noteq> 0\<close> by (auto elim: image_eqI [rotated])
  2926   finally show ?thesis .
  2927 qed
  2928 
  2929 lemma closed_negations:
  2930   fixes S :: "'a::real_normed_vector set"
  2931   assumes "closed S"
  2932   shows "closed ((\<lambda>x. -x) ` S)"
  2933   using closed_scaling[OF assms, of "- 1"] by simp
  2934 
  2935 lemma compact_closed_sums:
  2936   fixes S :: "'a::real_normed_vector set"
  2937   assumes "compact S" and "closed T"
  2938   shows "closed (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
  2939 proof -
  2940   let ?S = "{x + y |x y. x \<in> S \<and> y \<in> T}"
  2941   {
  2942     fix x l
  2943     assume as: "\<forall>n. x n \<in> ?S"  "(x \<longlongrightarrow> l) sequentially"
  2944     from as(1) obtain f where f: "\<forall>n. x n = fst (f n) + snd (f n)"  "\<forall>n. fst (f n) \<in> S"  "\<forall>n. snd (f n) \<in> T"
  2945       using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> S \<and> snd y \<in> T"] by auto
  2946     obtain l' r where "l'\<in>S" and r: "strict_mono r" and lr: "(((\<lambda>n. fst (f n)) \<circ> r) \<longlongrightarrow> l') sequentially"
  2947       using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto
  2948     have "((\<lambda>n. snd (f (r n))) \<longlongrightarrow> l - l') sequentially"
  2949       using tendsto_diff[OF LIMSEQ_subseq_LIMSEQ[OF as(2) r] lr] and f(1)
  2950       unfolding o_def
  2951       by auto
  2952     then have "l - l' \<in> T"
  2953       using assms(2)[unfolded closed_sequential_limits,
  2954         THEN spec[where x="\<lambda> n. snd (f (r n))"],
  2955         THEN spec[where x="l - l'"]]
  2956       using f(3)
  2957       by auto
  2958     then have "l \<in> ?S"
  2959       using \<open>l' \<in> S\<close>
  2960       apply auto
  2961       apply (rule_tac x=l' in exI)
  2962       apply (rule_tac x="l - l'" in exI, auto)
  2963       done
  2964   }
  2965   moreover have "?S = (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
  2966     by force
  2967   ultimately show ?thesis
  2968     unfolding closed_sequential_limits
  2969     by (metis (no_types, lifting))
  2970 qed
  2971 
  2972 lemma closed_compact_sums:
  2973   fixes S T :: "'a::real_normed_vector set"
  2974   assumes "closed S" "compact T"
  2975   shows "closed (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
  2976 proof -
  2977   have "(\<Union>x\<in> T. \<Union>y \<in> S. {x + y}) = (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
  2978     by auto
  2979   then show ?thesis
  2980     using compact_closed_sums[OF assms(2,1)] by simp
  2981 qed
  2982 
  2983 lemma compact_closed_differences:
  2984   fixes S T :: "'a::real_normed_vector set"
  2985   assumes "compact S" "closed T"
  2986   shows "closed (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"
  2987 proof -
  2988   have "(\<Union>x\<in> S. \<Union>y \<in> uminus ` T. {x + y}) = (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"
  2989     by force
  2990   then show ?thesis
  2991     using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto
  2992 qed
  2993 
  2994 lemma closed_compact_differences:
  2995   fixes S T :: "'a::real_normed_vector set"
  2996   assumes "closed S" "compact T"
  2997   shows "closed (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"
  2998 proof -
  2999   have "(\<Union>x\<in> S. \<Union>y \<in> uminus ` T. {x + y}) = {x - y |x y. x \<in> S \<and> y \<in> T}"
  3000     by auto
  3001  then show ?thesis
  3002   using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp
  3003 qed
  3004 
  3005 lemma closed_translation:
  3006   fixes a :: "'a::real_normed_vector"
  3007   assumes "closed S"
  3008   shows "closed ((\<lambda>x. a + x) ` S)"
  3009 proof -
  3010   have "(\<Union>x\<in> {a}. \<Union>y \<in> S. {x + y}) = (op + a ` S)" by auto
  3011   then show ?thesis
  3012     using compact_closed_sums[OF compact_sing[of a] assms] by auto
  3013 qed
  3014 
  3015 lemma translation_Compl:
  3016   fixes a :: "'a::ab_group_add"
  3017   shows "(\<lambda>x. a + x) ` (- t) = - ((\<lambda>x. a + x) ` t)"
  3018   apply (auto simp: image_iff)
  3019   apply (rule_tac x="x - a" in bexI, auto)
  3020   done
  3021 
  3022 lemma translation_UNIV:
  3023   fixes a :: "'a::ab_group_add"
  3024   shows "range (\<lambda>x. a + x) = UNIV"
  3025   by (fact surj_plus)
  3026 
  3027 lemma translation_diff:
  3028   fixes a :: "'a::ab_group_add"
  3029   shows "(\<lambda>x. a + x) ` (s - t) = ((\<lambda>x. a + x) ` s) - ((\<lambda>x. a + x) ` t)"
  3030   by auto
  3031 
  3032 lemma translation_Int:
  3033   fixes a :: "'a::ab_group_add"
  3034   shows "(\<lambda>x. a + x) ` (s \<inter> t) = ((\<lambda>x. a + x) ` s) \<inter> ((\<lambda>x. a + x) ` t)"
  3035   by auto
  3036 
  3037 lemma closure_translation:
  3038   fixes a :: "'a::real_normed_vector"
  3039   shows "closure ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (closure s)"
  3040 proof -
  3041   have *: "op + a ` (- s) = - op + a ` s"
  3042     apply auto
  3043     unfolding image_iff
  3044     apply (rule_tac x="x - a" in bexI, auto)
  3045     done
  3046   show ?thesis
  3047     unfolding closure_interior translation_Compl
  3048     using interior_translation[of a "- s"]
  3049     unfolding *
  3050     by auto
  3051 qed
  3052 
  3053 lemma frontier_translation:
  3054   fixes a :: "'a::real_normed_vector"
  3055   shows "frontier((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (frontier s)"
  3056   unfolding frontier_def translation_diff interior_translation closure_translation
  3057   by auto
  3058 
  3059 lemma sphere_translation:
  3060   fixes a :: "'n::euclidean_space"
  3061   shows "sphere (a+c) r = op+a ` sphere c r"
  3062 apply safe
  3063 apply (rule_tac x="x-a" in image_eqI)
  3064 apply (auto simp: dist_norm algebra_simps)
  3065 done
  3066 
  3067 lemma cball_translation:
  3068   fixes a :: "'n::euclidean_space"
  3069   shows "cball (a+c) r = op+a ` cball c r"
  3070 apply safe
  3071 apply (rule_tac x="x-a" in image_eqI)
  3072 apply (auto simp: dist_norm algebra_simps)
  3073 done
  3074 
  3075 lemma ball_translation:
  3076   fixes a :: "'n::euclidean_space"
  3077   shows "ball (a+c) r = op+a ` ball c r"
  3078 apply safe
  3079 apply (rule_tac x="x-a" in image_eqI)
  3080 apply (auto simp: dist_norm algebra_simps)
  3081 done
  3082 
  3083 
  3084 subsection \<open>Closure of halfspaces and hyperplanes\<close>
  3085 
  3086 lemma continuous_on_closed_Collect_le:
  3087   fixes f g :: "'a::t2_space \<Rightarrow> real"
  3088   assumes f: "continuous_on s f" and g: "continuous_on s g" and s: "closed s"
  3089   shows "closed {x \<in> s. f x \<le> g x}"
  3090 proof -
  3091   have "closed ((\<lambda>x. g x - f x) -` {0..} \<inter> s)"
  3092     using closed_real_atLeast continuous_on_diff [OF g f]
  3093     by (simp add: continuous_on_closed_vimage [OF s])
  3094   also have "((\<lambda>x. g x - f x) -` {0..} \<inter> s) = {x\<in>s. f x \<le> g x}"
  3095     by auto
  3096   finally show ?thesis .
  3097 qed
  3098 
  3099 lemma continuous_at_inner: "continuous (at x) (inner a)"
  3100   unfolding continuous_at by (intro tendsto_intros)
  3101 
  3102 lemma closed_halfspace_le: "closed {x. inner a x \<le> b}"
  3103   by (simp add: closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)
  3104 
  3105 lemma closed_halfspace_ge: "closed {x. inner a x \<ge> b}"
  3106   by (simp add: closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)
  3107 
  3108 lemma closed_hyperplane: "closed {x. inner a x = b}"
  3109   by (simp add: closed_Collect_eq continuous_on_inner continuous_on_const continuous_on_id)
  3110 
  3111 lemma closed_halfspace_component_le: "closed {x::'a::euclidean_space. x\<bullet>i \<le> a}"
  3112   by (simp add: closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)
  3113 
  3114 lemma closed_halfspace_component_ge: "closed {x::'a::euclidean_space. x\<bullet>i \<ge> a}"
  3115   by (simp add: closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)
  3116 
  3117 lemma closed_interval_left:
  3118   fixes b :: "'a::euclidean_space"
  3119   shows "closed {x::'a. \<forall>i\<in>Basis. x\<bullet>i \<le> b\<bullet>i}"
  3120   by (simp add: Collect_ball_eq closed_INT closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)
  3121 
  3122 lemma closed_interval_right:
  3123   fixes a :: "'a::euclidean_space"
  3124   shows "closed {x::'a. \<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i}"
  3125   by (simp add: Collect_ball_eq closed_INT closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)
  3126 
  3127 lemma continuous_le_on_closure:
  3128   fixes a::real
  3129   assumes f: "continuous_on (closure s) f"
  3130       and x: "x \<in> closure(s)"
  3131       and xlo: "\<And>x. x \<in> s ==> f(x) \<le> a"
  3132     shows "f(x) \<le> a"
  3133     using image_closure_subset [OF f]
  3134   using image_closure_subset [OF f] closed_halfspace_le [of "1::real" a] assms
  3135   by force
  3136 
  3137 lemma continuous_ge_on_closure:
  3138   fixes a::real
  3139   assumes f: "continuous_on (closure s) f"
  3140       and x: "x \<in> closure(s)"
  3141       and xlo: "\<And>x. x \<in> s ==> f(x) \<ge> a"
  3142     shows "f(x) \<ge> a"
  3143   using image_closure_subset [OF f] closed_halfspace_ge [of a "1::real"] assms
  3144   by force
  3145 
  3146 lemma Lim_component_le:
  3147   fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
  3148   assumes "(f \<longlongrightarrow> l) net"
  3149     and "\<not> (trivial_limit net)"
  3150     and "eventually (\<lambda>x. f(x)\<bullet>i \<le> b) net"
  3151   shows "l\<bullet>i \<le> b"
  3152   by (rule tendsto_le[OF assms(2) tendsto_const tendsto_inner[OF assms(1) tendsto_const] assms(3)])
  3153 
  3154 lemma Lim_component_ge:
  3155   fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
  3156   assumes "(f \<longlongrightarrow> l) net"
  3157     and "\<not> (trivial_limit net)"
  3158     and "eventually (\<lambda>x. b \<le> (f x)\<bullet>i) net"
  3159   shows "b \<le> l\<bullet>i"
  3160   by (rule tendsto_le[OF assms(2) tendsto_inner[OF assms(1) tendsto_const] tendsto_const assms(3)])
  3161 
  3162 lemma Lim_component_eq:
  3163   fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
  3164   assumes net: "(f \<longlongrightarrow> l) net" "\<not> trivial_limit net"
  3165     and ev:"eventually (\<lambda>x. f(x)\<bullet>i = b) net"
  3166   shows "l\<bullet>i = b"
  3167   using ev[unfolded order_eq_iff eventually_conj_iff]
  3168   using Lim_component_ge[OF net, of b i]
  3169   using Lim_component_le[OF net, of i b]
  3170   by auto
  3171 
  3172 text \<open>Limits relative to a union.\<close>
  3173 
  3174 lemma eventually_within_Un:
  3175   "eventually P (at x within (s \<union> t)) \<longleftrightarrow>
  3176     eventually P (at x within s) \<and> eventually P (at x within t)"
  3177   unfolding eventually_at_filter
  3178   by (auto elim!: eventually_rev_mp)
  3179 
  3180 lemma Lim_within_union:
  3181  "(f \<longlongrightarrow> l) (at x within (s \<union> t)) \<longleftrightarrow>
  3182   (f \<longlongrightarrow> l) (at x within s) \<and> (f \<longlongrightarrow> l) (at x within t)"
  3183   unfolding tendsto_def
  3184   by (auto simp: eventually_within_Un)
  3185 
  3186 lemma Lim_topological:
  3187   "(f \<longlongrightarrow> l) net \<longleftrightarrow>
  3188     trivial_limit net \<or> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net)"
  3189   unfolding tendsto_def trivial_limit_eq by auto
  3190 
  3191 text \<open>Continuity relative to a union.\<close>
  3192 
  3193 lemma continuous_on_Un_local:
  3194     "\<lbrakk>closedin (subtopology euclidean (s \<union> t)) s; closedin (subtopology euclidean (s \<union> t)) t;
  3195       continuous_on s f; continuous_on t f\<rbrakk>
  3196      \<Longrightarrow> continuous_on (s \<union> t) f"
  3197   unfolding continuous_on closedin_limpt
  3198   by (metis Lim_trivial_limit Lim_within_union Un_iff trivial_limit_within)
  3199 
  3200 lemma continuous_on_cases_local:
  3201      "\<lbrakk>closedin (subtopology euclidean (s \<union> t)) s; closedin (subtopology euclidean (s \<union> t)) t;
  3202        continuous_on s f; continuous_on t g;
  3203        \<And>x. \<lbrakk>x \<in> s \<and> ~P x \<or> x \<in> t \<and> P x\<rbrakk> \<Longrightarrow> f x = g x\<rbrakk>
  3204       \<Longrightarrow> continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
  3205   by (rule continuous_on_Un_local) (auto intro: continuous_on_eq)
  3206 
  3207 lemma continuous_on_cases_le:
  3208   fixes h :: "'a :: topological_space \<Rightarrow> real"
  3209   assumes "continuous_on {t \<in> s. h t \<le> a} f"
  3210       and "continuous_on {t \<in> s. a \<le> h t} g"
  3211       and h: "continuous_on s h"
  3212       and "\<And>t. \<lbrakk>t \<in> s; h t = a\<rbrakk> \<Longrightarrow> f t = g t"
  3213     shows "continuous_on s (\<lambda>t. if h t \<le> a then f(t) else g(t))"
  3214 proof -
  3215   have s: "s = (s \<inter> h -` atMost a) \<union> (s \<inter> h -` atLeast a)"
  3216     by force
  3217   have 1: "closedin (subtopology euclidean s) (s \<inter> h -` atMost a)"
  3218     by (rule continuous_closedin_preimage [OF h closed_atMost])
  3219   have 2: "closedin (subtopology euclidean s) (s \<inter> h -` atLeast a)"
  3220     by (rule continuous_closedin_preimage [OF h closed_atLeast])
  3221   have eq: "s \<inter> h -` {..a} = {t \<in> s. h t \<le> a}" "s \<inter> h -` {a..} = {t \<in> s. a \<le> h t}"
  3222     by auto
  3223   show ?thesis
  3224     apply (rule continuous_on_subset [of s, OF _ order_refl])
  3225     apply (subst s)
  3226     apply (rule continuous_on_cases_local)
  3227     using 1 2 s assms apply (auto simp: eq)
  3228     done
  3229 qed
  3230 
  3231 lemma continuous_on_cases_1:
  3232   fixes s :: "real set"
  3233   assumes "continuous_on {t \<in> s. t \<le> a} f"
  3234       and "continuous_on {t \<in> s. a \<le> t} g"
  3235       and "a \<in> s \<Longrightarrow> f a = g a"
  3236     shows "continuous_on s (\<lambda>t. if t \<le> a then f(t) else g(t))"
  3237 using assms
  3238 by (auto simp: continuous_on_id intro: continuous_on_cases_le [where h = id, simplified])
  3239 
  3240 subsubsection\<open>Some more convenient intermediate-value theorem formulations.\<close>
  3241 
  3242 lemma connected_ivt_hyperplane:
  3243   assumes "connected S" and xy: "x \<in> S" "y \<in> S" and b: "inner a x \<le> b" "b \<le> inner a y"
  3244   shows "\<exists>z \<in> S. inner a z = b"
  3245 proof (rule ccontr)
  3246   assume as:"\<not> (\<exists>z\<in>S. inner a z = b)"
  3247   let ?A = "{x. inner a x < b}"
  3248   let ?B = "{x. inner a x > b}"
  3249   have "open ?A" "open ?B"
  3250     using open_halfspace_lt and open_halfspace_gt by auto
  3251   moreover have "?A \<inter> ?B = {}" by auto
  3252   moreover have "S \<subseteq> ?A \<union> ?B" using as by auto
  3253   ultimately show False
  3254     using \<open>connected S\<close>[unfolded connected_def not_ex,
  3255       THEN spec[where x="?A"], THEN spec[where x="?B"]]
  3256     using xy b by auto
  3257 qed
  3258 
  3259 lemma connected_ivt_component:
  3260   fixes x::"'a::euclidean_space"
  3261   shows "connected S \<Longrightarrow> x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> x\<bullet>k \<le> a \<Longrightarrow> a \<le> y\<bullet>k \<Longrightarrow> (\<exists>z\<in>S.  z\<bullet>k = a)"
  3262   using connected_ivt_hyperplane[of S x y "k::'a" a]
  3263   by (auto simp: inner_commute)
  3264 
  3265 lemma image_affinity_cbox: fixes m::real
  3266   fixes a b c :: "'a::euclidean_space"
  3267   shows "(\<lambda>x. m *\<^sub>R x + c) ` cbox a b =
  3268     (if cbox a b = {} then {}
  3269      else (if 0 \<le> m then cbox (m *\<^sub>R a + c) (m *\<^sub>R b + c)
  3270      else cbox (m *\<^sub>R b + c) (m *\<^sub>R a + c)))"
  3271 proof (cases "m = 0")
  3272   case True
  3273   {
  3274     fix x
  3275     assume "\<forall>i\<in>Basis. x \<bullet> i \<le> c \<bullet> i" "\<forall>i\<in>Basis. c \<bullet> i \<le> x \<bullet> i"
  3276     then have "x = c"
  3277       by (simp add: dual_order.antisym euclidean_eqI)
  3278   }
  3279   moreover have "c \<in> cbox (m *\<^sub>R a + c) (m *\<^sub>R b + c)"
  3280     unfolding True by (auto simp: cbox_sing)
  3281   ultimately show ?thesis using True by (auto simp: cbox_def)
  3282 next
  3283   case False
  3284   {
  3285     fix y
  3286     assume "\<forall>i\<in>Basis. a \<bullet> i \<le> y \<bullet> i" "\<forall>i\<in>Basis. y \<bullet> i \<le> b \<bullet> i" "m > 0"
  3287     then have "\<forall>i\<in>Basis. (m *\<^sub>R a + c) \<bullet> i \<le> (m *\<^sub>R y + c) \<bullet> i" and "\<forall>i\<in>Basis. (m *\<^sub>R y + c) \<bullet> i \<le> (m *\<^sub>R b + c) \<bullet> i"
  3288       by (auto simp: inner_distrib)
  3289   }
  3290   moreover
  3291   {
  3292     fix y
  3293     assume "\<forall>i\<in>Basis. a \<bullet> i \<le> y \<bullet> i" "\<forall>i\<in>Basis. y \<bullet> i \<le> b \<bullet> i" "m < 0"
  3294     then have "\<forall>i\<in>Basis. (m *\<^sub>R b + c) \<bullet> i \<le> (m *\<^sub>R y + c) \<bullet> i" and "\<forall>i\<in>Basis. (m *\<^sub>R y + c) \<bullet> i \<le> (m *\<^sub>R a + c) \<bullet> i"
  3295       by (auto simp: mult_left_mono_neg inner_distrib)
  3296   }
  3297   moreover
  3298   {
  3299     fix y
  3300     assume "m > 0" and "\<forall>i\<in>Basis. (m *\<^sub>R a + c) \<bullet> i \<le> y \<bullet> i" and "\<forall>i\<in>Basis. y \<bullet> i \<le> (m *\<^sub>R b + c) \<bullet> i"
  3301     then have "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` cbox a b"
  3302       unfolding image_iff Bex_def mem_box
  3303       apply (intro exI[where x="(1 / m) *\<^sub>R (y - c)"])
  3304       apply (auto simp: pos_le_divide_eq pos_divide_le_eq mult.commute inner_distrib inner_diff_left)
  3305       done
  3306   }
  3307   moreover
  3308   {
  3309     fix y
  3310     assume "\<forall>i\<in>Basis. (m *\<^sub>R b + c) \<bullet> i \<le> y \<bullet> i" "\<forall>i\<in>Basis. y \<bullet> i \<le> (m *\<^sub>R a + c) \<bullet> i" "m < 0"
  3311     then have "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` cbox a b"
  3312       unfolding image_iff Bex_def mem_box
  3313       apply (intro exI[where x="(1 / m) *\<^sub>R (y - c)"])
  3314       apply (auto simp: neg_le_divide_eq neg_divide_le_eq mult.commute inner_distrib inner_diff_left)
  3315       done
  3316   }
  3317   ultimately show ?thesis using False by (auto simp: cbox_def)
  3318 qed
  3319 
  3320 lemma image_smult_cbox:"(\<lambda>x. m *\<^sub>R (x::_::euclidean_space)) ` cbox a b =
  3321   (if cbox a b = {} then {} else if 0 \<le> m then cbox (m *\<^sub>R a) (m *\<^sub>R b) else cbox (m *\<^sub>R b) (m *\<^sub>R a))"
  3322   using image_affinity_cbox[of m 0 a b] by auto
  3323 
  3324 lemma islimpt_greaterThanLessThan1:
  3325   fixes a b::"'a::{linorder_topology, dense_order}"
  3326   assumes "a < b"
  3327   shows  "a islimpt {a<..<b}"
  3328 proof (rule islimptI)
  3329   fix T
  3330   assume "open T" "a \<in> T"
  3331   from open_right[OF this \<open>a < b\<close>]
  3332   obtain c where c: "a < c" "{a..<c} \<subseteq> T" by auto
  3333   with assms dense[of a "min c b"]
  3334   show "\<exists>y\<in>{a<..<b}. y \<in> T \<and> y \<noteq> a"
  3335     by (metis atLeastLessThan_iff greaterThanLessThan_iff min_less_iff_conj
  3336       not_le order.strict_implies_order subset_eq)
  3337 qed
  3338 
  3339 lemma islimpt_greaterThanLessThan2:
  3340   fixes a b::"'a::{linorder_topology, dense_order}"
  3341   assumes "a < b"
  3342   shows  "b islimpt {a<..<b}"
  3343 proof (rule islimptI)
  3344   fix T
  3345   assume "open T" "b \<in> T"
  3346   from open_left[OF this \<open>a < b\<close>]
  3347   obtain c where c: "c < b" "{c<..b} \<subseteq> T" by auto
  3348   with assms dense[of "max a c" b]
  3349   show "\<exists>y\<in>{a<..<b}. y \<in> T \<and> y \<noteq> b"
  3350     by (metis greaterThanAtMost_iff greaterThanLessThan_iff max_less_iff_conj
  3351       not_le order.strict_implies_order subset_eq)
  3352 qed
  3353 
  3354 lemma closure_greaterThanLessThan[simp]:
  3355   fixes a b::"'a::{linorder_topology, dense_order}"
  3356   shows "a < b \<Longrightarrow> closure {a <..< b} = {a .. b}" (is "_ \<Longrightarrow> ?l = ?r")
  3357 proof
  3358   have "?l \<subseteq> closure ?r"
  3359     by (rule closure_mono) auto
  3360   thus "closure {a<..<b} \<subseteq> {a..b}" by simp
  3361 qed (auto simp: closure_def order.order_iff_strict islimpt_greaterThanLessThan1
  3362   islimpt_greaterThanLessThan2)
  3363 
  3364 lemma closure_greaterThan[simp]:
  3365   fixes a b::"'a::{no_top, linorder_topology, dense_order}"
  3366   shows "closure {a<..} = {a..}"
  3367 proof -
  3368   from gt_ex obtain b where "a < b" by auto
  3369   hence "{a<..} = {a<..<b} \<union> {b..}" by auto
  3370   also have "closure \<dots> = {a..}" using \<open>a < b\<close> unfolding closure_Un
  3371     by auto
  3372   finally show ?thesis .
  3373 qed
  3374 
  3375 lemma closure_lessThan[simp]:
  3376   fixes b::"'a::{no_bot, linorder_topology, dense_order}"
  3377   shows "closure {..<b} = {..b}"
  3378 proof -
  3379   from lt_ex obtain a where "a < b" by auto
  3380   hence "{..<b} = {a<..<b} \<union> {..a}" by auto
  3381   also have "closure \<dots> = {..b}" using \<open>a < b\<close> unfolding closure_Un
  3382     by auto
  3383   finally show ?thesis .
  3384 qed
  3385 
  3386 lemma closure_atLeastLessThan[simp]:
  3387   fixes a b::"'a::{linorder_topology, dense_order}"
  3388   assumes "a < b"
  3389   shows "closure {a ..< b} = {a .. b}"
  3390 proof -
  3391   from assms have "{a ..< b} = {a} \<union> {a <..< b}" by auto
  3392   also have "closure \<dots> = {a .. b}" unfolding closure_Un
  3393     by (auto simp: assms less_imp_le)
  3394   finally show ?thesis .
  3395 qed
  3396 
  3397 lemma closure_greaterThanAtMost[simp]:
  3398   fixes a b::"'a::{linorder_topology, dense_order}"
  3399   assumes "a < b"
  3400   shows "closure {a <.. b} = {a .. b}"
  3401 proof -
  3402   from assms have "{a <.. b} = {b} \<union> {a <..< b}" by auto
  3403   also have "closure \<dots> = {a .. b}" unfolding closure_Un
  3404     by (auto simp: assms less_imp_le)
  3405   finally show ?thesis .
  3406 qed
  3407 
  3408 
  3409 subsection \<open>Homeomorphisms\<close>
  3410 
  3411 definition "homeomorphism s t f g \<longleftrightarrow>
  3412   (\<forall>x\<in>s. (g(f x) = x)) \<and> (f ` s = t) \<and> continuous_on s f \<and>
  3413   (\<forall>y\<in>t. (f(g y) = y)) \<and> (g ` t = s) \<and> continuous_on t g"
  3414 
  3415 lemma homeomorphismI [intro?]:
  3416   assumes "continuous_on S f" "continuous_on T g"
  3417           "f ` S \<subseteq> T" "g ` T \<subseteq> S" "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x" "\<And>y. y \<in> T \<Longrightarrow> f(g y) = y"
  3418     shows "homeomorphism S T f g"
  3419   using assms by (force simp: homeomorphism_def)
  3420 
  3421 lemma homeomorphism_translation:
  3422   fixes a :: "'a :: real_normed_vector"
  3423   shows "homeomorphism (op + a ` S) S (op + (- a)) (op + a)"
  3424 unfolding homeomorphism_def by (auto simp: algebra_simps continuous_intros)
  3425 
  3426 lemma homeomorphism_ident: "homeomorphism T T (\<lambda>a. a) (\<lambda>a. a)"
  3427   by (rule homeomorphismI) (auto simp: continuous_on_id)
  3428 
  3429 lemma homeomorphism_compose:
  3430   assumes "homeomorphism S T f g" "homeomorphism T U h k"
  3431     shows "homeomorphism S U (h o f) (g o k)"
  3432   using assms
  3433   unfolding homeomorphism_def
  3434   by (intro conjI ballI continuous_on_compose) (auto simp: image_comp [symmetric])
  3435 
  3436 lemma homeomorphism_symD: "homeomorphism S t f g \<Longrightarrow> homeomorphism t S g f"
  3437   by (simp add: homeomorphism_def)
  3438 
  3439 lemma homeomorphism_sym: "homeomorphism S t f g = homeomorphism t S g f"
  3440   by (force simp: homeomorphism_def)
  3441 
  3442 definition homeomorphic :: "'a::topological_space set \<Rightarrow> 'b::topological_space set \<Rightarrow> bool"
  3443     (infixr "homeomorphic" 60)
  3444   where "s homeomorphic t \<equiv> (\<exists>f g. homeomorphism s t f g)"
  3445 
  3446 lemma homeomorphic_empty [iff]:
  3447      "S homeomorphic {} \<longleftrightarrow> S = {}" "{} homeomorphic S \<longleftrightarrow> S = {}"
  3448   by (auto simp: homeomorphic_def homeomorphism_def)
  3449 
  3450 lemma homeomorphic_refl: "s homeomorphic s"
  3451   unfolding homeomorphic_def homeomorphism_def
  3452   using continuous_on_id
  3453   apply (rule_tac x = "(\<lambda>x. x)" in exI)
  3454   apply (rule_tac x = "(\<lambda>x. x)" in exI)
  3455   apply blast
  3456   done
  3457 
  3458 lemma homeomorphic_sym: "s homeomorphic t \<longleftrightarrow> t homeomorphic s"
  3459   unfolding homeomorphic_def homeomorphism_def
  3460   by blast
  3461 
  3462 lemma homeomorphic_trans [trans]:
  3463   assumes "S homeomorphic T"
  3464       and "T homeomorphic U"
  3465     shows "S homeomorphic U"
  3466   using assms
  3467   unfolding homeomorphic_def
  3468 by (metis homeomorphism_compose)
  3469 
  3470 lemma homeomorphic_minimal:
  3471   "s homeomorphic t \<longleftrightarrow>
  3472     (\<exists>f g. (\<forall>x\<in>s. f(x) \<in> t \<and> (g(f(x)) = x)) \<and>
  3473            (\<forall>y\<in>t. g(y) \<in> s \<and> (f(g(y)) = y)) \<and>
  3474            continuous_on s f \<and> continuous_on t g)"
  3475    (is "?lhs = ?rhs")
  3476 proof
  3477   assume ?lhs
  3478   then show ?rhs
  3479     by (fastforce simp: homeomorphic_def homeomorphism_def)
  3480 next
  3481   assume ?rhs
  3482   then show ?lhs
  3483     apply clarify
  3484     unfolding homeomorphic_def homeomorphism_def
  3485     by (metis equalityI image_subset_iff subsetI)
  3486  qed
  3487 
  3488 lemma homeomorphicI [intro?]:
  3489    "\<lbrakk>f ` S = T; g ` T = S;
  3490      continuous_on S f; continuous_on T g;
  3491      \<And>x. x \<in> S \<Longrightarrow> g(f(x)) = x;
  3492      \<And>y. y \<in> T \<Longrightarrow> f(g(y)) = y\<rbrakk> \<Longrightarrow> S homeomorphic T"
  3493 unfolding homeomorphic_def homeomorphism_def by metis
  3494 
  3495 lemma homeomorphism_of_subsets:
  3496    "\<lbrakk>homeomorphism S T f g; S' \<subseteq> S; T'' \<subseteq> T; f ` S' = T'\<rbrakk>
  3497     \<Longrightarrow> homeomorphism S' T' f g"
  3498 apply (auto simp: homeomorphism_def elim!: continuous_on_subset)
  3499 by (metis subsetD imageI)
  3500 
  3501 lemma homeomorphism_apply1: "\<lbrakk>homeomorphism S T f g; x \<in> S\<rbrakk> \<Longrightarrow> g(f x) = x"
  3502   by (simp add: homeomorphism_def)
  3503 
  3504 lemma homeomorphism_apply2: "\<lbrakk>homeomorphism S T f g; x \<in> T\<rbrakk> \<Longrightarrow> f(g x) = x"
  3505   by (simp add: homeomorphism_def)
  3506 
  3507 lemma homeomorphism_image1: "homeomorphism S T f g \<Longrightarrow> f ` S = T"
  3508   by (simp add: homeomorphism_def)
  3509 
  3510 lemma homeomorphism_image2: "homeomorphism S T f g \<Longrightarrow> g ` T = S"
  3511   by (simp add: homeomorphism_def)
  3512 
  3513 lemma homeomorphism_cont1: "homeomorphism S T f g \<Longrightarrow> continuous_on S f"
  3514   by (simp add: homeomorphism_def)
  3515 
  3516 lemma homeomorphism_cont2: "homeomorphism S T f g \<Longrightarrow> continuous_on T g"
  3517   by (simp add: homeomorphism_def)
  3518 
  3519 lemma continuous_on_no_limpt:
  3520    "(\<And>x. \<not> x islimpt S) \<Longrightarrow> continuous_on S f"
  3521   unfolding continuous_on_def
  3522   by (metis UNIV_I empty_iff eventually_at_topological islimptE open_UNIV tendsto_def trivial_limit_within)
  3523 
  3524 lemma continuous_on_finite:
  3525   fixes S :: "'a::t1_space set"
  3526   shows "finite S \<Longrightarrow> continuous_on S f"
  3527 by (metis continuous_on_no_limpt islimpt_finite)
  3528 
  3529 lemma homeomorphic_finite:
  3530   fixes S :: "'a::t1_space set" and T :: "'b::t1_space set"
  3531   assumes "finite T"
  3532   shows "S homeomorphic T \<longleftrightarrow> finite S \<and> finite T \<and> card S = card T" (is "?lhs = ?rhs")
  3533 proof
  3534   assume "S homeomorphic T"
  3535   with assms show ?rhs
  3536     apply (auto simp: homeomorphic_def homeomorphism_def)
  3537      apply (metis finite_imageI)
  3538     by (metis card_image_le finite_imageI le_antisym)
  3539 next
  3540   assume R: ?rhs
  3541   with finite_same_card_bij obtain h where "bij_betw h S T"
  3542     by auto
  3543   with R show ?lhs
  3544     apply (auto simp: homeomorphic_def homeomorphism_def continuous_on_finite)
  3545     apply (rule_tac x=h in exI)
  3546     apply (rule_tac x="inv_into S h" in exI)
  3547     apply (auto simp:  bij_betw_inv_into_left bij_betw_inv_into_right bij_betw_imp_surj_on inv_into_into bij_betwE)
  3548     apply (metis bij_betw_def bij_betw_inv_into)
  3549     done
  3550 qed
  3551 
  3552 text \<open>Relatively weak hypotheses if a set is compact.\<close>
  3553 
  3554 lemma homeomorphism_compact:
  3555   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  3556   assumes "compact s" "continuous_on s f"  "f ` s = t"  "inj_on f s"
  3557   shows "\<exists>g. homeomorphism s t f g"
  3558 proof -
  3559   define g where "g x = (SOME y. y\<in>s \<and> f y = x)" for x
  3560   have g: "\<forall>x\<in>s. g (f x) = x"
  3561     using assms(3) assms(4)[unfolded inj_on_def] unfolding g_def by auto
  3562   {
  3563     fix y
  3564     assume "y \<in> t"
  3565     then obtain x where x:"f x = y" "x\<in>s"
  3566       using assms(3) by auto
  3567     then have "g (f x) = x" using g by auto
  3568     then have "f (g y) = y" unfolding x(1)[symmetric] by auto
  3569   }
  3570   then have g':"\<forall>x\<in>t. f (g x) = x" by auto
  3571   moreover
  3572   {
  3573     fix x
  3574     have "x\<in>s \<Longrightarrow> x \<in> g ` t"
  3575       using g[THEN bspec[where x=x]]
  3576       unfolding image_iff
  3577       using assms(3)
  3578       by (auto intro!: bexI[where x="f x"])
  3579     moreover
  3580     {
  3581       assume "x\<in>g ` t"
  3582       then obtain y where y:"y\<in>t" "g y = x" by auto
  3583       then obtain x' where x':"x'\<in>s" "f x' = y"
  3584         using assms(3) by auto
  3585       then have "x \<in> s"
  3586         unfolding g_def
  3587         using someI2[of "\<lambda>b. b\<in>s \<and> f b = y" x' "\<lambda>x. x\<in>s"]
  3588         unfolding y(2)[symmetric] and g_def
  3589         by auto
  3590     }
  3591     ultimately have "x\<in>s \<longleftrightarrow> x \<in> g ` t" ..
  3592   }
  3593   then have "g ` t = s" by auto
  3594   ultimately show ?thesis
  3595     unfolding homeomorphism_def homeomorphic_def
  3596     apply (rule_tac x=g in exI)
  3597     using g and assms(3) and continuous_on_inv[OF assms(2,1), of g, unfolded assms(3)] and assms(2)
  3598     apply auto
  3599     done
  3600 qed
  3601 
  3602 lemma homeomorphic_compact:
  3603   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  3604   shows "compact s \<Longrightarrow> continuous_on s f \<Longrightarrow> (f ` s = t) \<Longrightarrow> inj_on f s \<Longrightarrow> s homeomorphic t"
  3605   unfolding homeomorphic_def by (metis homeomorphism_compact)
  3606 
  3607 text\<open>Preservation of topological properties.\<close>
  3608 
  3609 lemma homeomorphic_compactness: "s homeomorphic t \<Longrightarrow> (compact s \<longleftrightarrow> compact t)"
  3610   unfolding homeomorphic_def homeomorphism_def
  3611   by (metis compact_continuous_image)
  3612 
  3613 text\<open>Results on translation, scaling etc.\<close>
  3614 
  3615 lemma homeomorphic_scaling:
  3616   fixes s :: "'a::real_normed_vector set"
  3617   assumes "c \<noteq> 0"
  3618   shows "s homeomorphic ((\<lambda>x. c *\<^sub>R x) ` s)"
  3619   unfolding homeomorphic_minimal
  3620   apply (rule_tac x="\<lambda>x. c *\<^sub>R x" in exI)
  3621   apply (rule_tac x="\<lambda>x. (1 / c) *\<^sub>R x" in exI)
  3622   using assms
  3623   apply (auto simp: continuous_intros)
  3624   done
  3625 
  3626 lemma homeomorphic_translation:
  3627   fixes s :: "'a::real_normed_vector set"
  3628   shows "s homeomorphic ((\<lambda>x. a + x) ` s)"
  3629   unfolding homeomorphic_minimal
  3630   apply (rule_tac x="\<lambda>x. a + x" in exI)
  3631   apply (rule_tac x="\<lambda>x. -a + x" in exI)
  3632   using continuous_on_add [OF continuous_on_const continuous_on_id, of s a]
  3633     continuous_on_add [OF continuous_on_const continuous_on_id, of "plus a ` s" "- a"]
  3634   apply auto
  3635   done
  3636 
  3637 lemma homeomorphic_affinity:
  3638   fixes s :: "'a::real_normed_vector set"
  3639   assumes "c \<noteq> 0"
  3640   shows "s homeomorphic ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  3641 proof -
  3642   have *: "op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
  3643   show ?thesis
  3644     using homeomorphic_trans
  3645     using homeomorphic_scaling[OF assms, of s]
  3646     using homeomorphic_translation[of "(\<lambda>x. c *\<^sub>R x) ` s" a]
  3647     unfolding *
  3648     by auto
  3649 qed
  3650 
  3651 lemma homeomorphic_balls:
  3652   fixes a b ::"'a::real_normed_vector"
  3653   assumes "0 < d"  "0 < e"
  3654   shows "(ball a d) homeomorphic  (ball b e)" (is ?th)
  3655     and "(cball a d) homeomorphic (cball b e)" (is ?cth)
  3656 proof -
  3657   show ?th unfolding homeomorphic_minimal
  3658     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
  3659     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
  3660     using assms
  3661     apply (auto intro!: continuous_intros
  3662       simp: dist_commute dist_norm pos_divide_less_eq mult_strict_left_mono)
  3663     done
  3664   show ?cth unfolding homeomorphic_minimal
  3665     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
  3666     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
  3667     using assms
  3668     apply (auto intro!: continuous_intros
  3669       simp: dist_commute dist_norm pos_divide_le_eq mult_strict_left_mono)
  3670     done
  3671 qed
  3672 
  3673 lemma homeomorphic_spheres:
  3674   fixes a b ::"'a::real_normed_vector"
  3675   assumes "0 < d"  "0 < e"
  3676   shows "(sphere a d) homeomorphic (sphere b e)"
  3677 unfolding homeomorphic_minimal
  3678     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
  3679     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
  3680     using assms
  3681     apply (auto intro!: continuous_intros
  3682       simp: dist_commute dist_norm pos_divide_less_eq mult_strict_left_mono)
  3683     done
  3684 
  3685 lemma homeomorphic_ball01_UNIV:
  3686   "ball (0::'a::real_normed_vector) 1 homeomorphic (UNIV:: 'a set)"
  3687   (is "?B homeomorphic ?U")
  3688 proof
  3689   have "x \<in> (\<lambda>z. z /\<^sub>R (1 - norm z)) ` ball 0 1" for x::'a
  3690     apply (rule_tac x="x /\<^sub>R (1 + norm x)" in image_eqI)
  3691      apply (auto simp: divide_simps)
  3692     using norm_ge_zero [of x] apply linarith+
  3693     done
  3694   then show "(\<lambda>z::'a. z /\<^sub>R (1 - norm z)) ` ?B = ?U"
  3695     by blast
  3696   have "x \<in> range (\<lambda>z. (1 / (1 + norm z)) *\<^sub>R z)" if "norm x < 1" for x::'a
  3697     apply (rule_tac x="x /\<^sub>R (1 - norm x)" in image_eqI)
  3698     using that apply (auto simp: divide_simps)
  3699     done
  3700   then show "(\<lambda>z::'a. z /\<^sub>R (1 + norm z)) ` ?U = ?B"
  3701     by (force simp: divide_simps dest: add_less_zeroD)
  3702   show "continuous_on (ball 0 1) (\<lambda>z. z /\<^sub>R (1 - norm z))"
  3703     by (rule continuous_intros | force)+
  3704   show "continuous_on UNIV (\<lambda>z. z /\<^sub>R (1 + norm z))"
  3705     apply (intro continuous_intros)
  3706     apply (metis le_add_same_cancel1 norm_ge_zero not_le zero_less_one)
  3707     done
  3708   show "\<And>x. x \<in> ball 0 1 \<Longrightarrow>
  3709          x /\<^sub>R (1 - norm x) /\<^sub>R (1 + norm (x /\<^sub>R (1 - norm x))) = x"
  3710     by (auto simp: divide_simps)
  3711   show "\<And>y. y /\<^sub>R (1 + norm y) /\<^sub>R (1 - norm (y /\<^sub>R (1 + norm y))) = y"
  3712     apply (auto simp: divide_simps)
  3713     apply (metis le_add_same_cancel1 norm_ge_zero not_le zero_less_one)
  3714     done
  3715 qed
  3716 
  3717 proposition homeomorphic_ball_UNIV:
  3718   fixes a ::"'a::real_normed_vector"
  3719   assumes "0 < r" shows "ball a r homeomorphic (UNIV:: 'a set)"
  3720   using assms homeomorphic_ball01_UNIV homeomorphic_balls(1) homeomorphic_trans zero_less_one by blast
  3721 
  3722 
  3723 subsection\<open>Inverse function property for open/closed maps\<close>
  3724 
  3725 lemma continuous_on_inverse_open_map:
  3726   assumes contf: "continuous_on S f"
  3727     and imf: "f ` S = T"
  3728     and injf: "\<And>x. x \<in> S \<Longrightarrow> g (f x) = x"
  3729     and oo: "\<And>U. openin (subtopology euclidean S) U \<Longrightarrow> openin (subtopology euclidean T) (f ` U)"
  3730   shows "continuous_on T g"
  3731 proof -
  3732   from imf injf have gTS: "g ` T = S"
  3733     by force
  3734   from imf injf have fU: "U \<subseteq> S \<Longrightarrow> (f ` U) = T \<inter> g -` U" for U
  3735     by force
  3736   show ?thesis
  3737     by (simp add: continuous_on_open [of T g] gTS) (metis openin_imp_subset fU oo)
  3738 qed
  3739 
  3740 lemma continuous_on_inverse_closed_map:
  3741   assumes contf: "continuous_on S f"
  3742     and imf: "f ` S = T"
  3743     and injf: "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x"
  3744     and oo: "\<And>U. closedin (subtopology euclidean S) U \<Longrightarrow> closedin (subtopology euclidean T) (f ` U)"
  3745   shows "continuous_on T g"
  3746 proof -
  3747   from imf injf have gTS: "g ` T = S"
  3748     by force
  3749   from imf injf have fU: "U \<subseteq> S \<Longrightarrow> (f ` U) = T \<inter> g -` U" for U
  3750     by force
  3751   show ?thesis
  3752     by (simp add: continuous_on_closed [of T g] gTS) (metis closedin_imp_subset fU oo)
  3753 qed
  3754 
  3755 lemma homeomorphism_injective_open_map:
  3756   assumes contf: "continuous_on S f"
  3757     and imf: "f ` S = T"
  3758     and injf: "inj_on f S"
  3759     and oo: "\<And>U. openin (subtopology euclidean S) U \<Longrightarrow> openin (subtopology euclidean T) (f ` U)"
  3760   obtains g where "homeomorphism S T f g"
  3761 proof
  3762   have "continuous_on T (inv_into S f)"
  3763     by (metis contf continuous_on_inverse_open_map imf injf inv_into_f_f oo)
  3764   with imf injf contf show "homeomorphism S T f (inv_into S f)"
  3765     by (auto simp: homeomorphism_def)
  3766 qed
  3767 
  3768 lemma homeomorphism_injective_closed_map:
  3769   assumes contf: "continuous_on S f"
  3770     and imf: "f ` S = T"
  3771     and injf: "inj_on f S"
  3772     and oo: "\<And>U. closedin (subtopology euclidean S) U \<Longrightarrow> closedin (subtopology euclidean T) (f ` U)"
  3773   obtains g where "homeomorphism S T f g"
  3774 proof
  3775   have "continuous_on T (inv_into S f)"
  3776     by (metis contf continuous_on_inverse_closed_map imf injf inv_into_f_f oo)
  3777   with imf injf contf show "homeomorphism S T f (inv_into S f)"
  3778     by (auto simp: homeomorphism_def)
  3779 qed
  3780 
  3781 lemma homeomorphism_imp_open_map:
  3782   assumes hom: "homeomorphism S T f g"
  3783     and oo: "openin (subtopology euclidean S) U"
  3784   shows "openin (subtopology euclidean T) (f ` U)"
  3785 proof -
  3786   from hom oo have [simp]: "f ` U = T \<inter> g -` U"
  3787     using openin_subset by (fastforce simp: homeomorphism_def rev_image_eqI)
  3788   from hom have "continuous_on T g"
  3789     unfolding homeomorphism_def by blast
  3790   moreover have "g ` T = S"
  3791     by (metis hom homeomorphism_def)
  3792   ultimately show ?thesis
  3793     by (simp add: continuous_on_open oo)
  3794 qed
  3795 
  3796 lemma homeomorphism_imp_closed_map:
  3797   assumes hom: "homeomorphism S T f g"
  3798     and oo: "closedin (subtopology euclidean S) U"
  3799   shows "closedin (subtopology euclidean T) (f ` U)"
  3800 proof -
  3801   from hom oo have [simp]: "f ` U = T \<inter> g -` U"
  3802     using closedin_subset by (fastforce simp: homeomorphism_def rev_image_eqI)
  3803   from hom have "continuous_on T g"
  3804     unfolding homeomorphism_def by blast
  3805   moreover have "g ` T = S"
  3806     by (metis hom homeomorphism_def)
  3807   ultimately show ?thesis
  3808     by (simp add: continuous_on_closed oo)
  3809 qed
  3810 
  3811 
  3812 subsection \<open>"Isometry" (up to constant bounds) of injective linear map etc.\<close>
  3813 
  3814 lemma cauchy_isometric:
  3815   assumes e: "e > 0"
  3816     and s: "subspace s"
  3817     and f: "bounded_linear f"
  3818     and normf: "\<forall>x\<in>s. norm (f x) \<ge> e * norm x"
  3819     and xs: "\<forall>n. x n \<in> s"
  3820     and cf: "Cauchy (f \<circ> x)"
  3821   shows "Cauchy x"
  3822 proof -
  3823   interpret f: bounded_linear f by fact
  3824   have "\<exists>N. \<forall>n\<ge>N. norm (x n - x N) < d" if "d > 0" for d :: real
  3825   proof -
  3826     from that obtain N where N: "\<forall>n\<ge>N. norm (f (x n) - f (x N)) < e * d"
  3827       using cf[unfolded Cauchy_def o_def dist_norm, THEN spec[where x="e*d"]] e
  3828       by auto
  3829     have "norm (x n - x N) < d" if "n \<ge> N" for n
  3830     proof -
  3831       have "e * norm (x n - x N) \<le> norm (f (x n - x N))"
  3832         using subspace_diff[OF s, of "x n" "x N"]
  3833         using xs[THEN spec[where x=N]] and xs[THEN spec[where x=n]]
  3834         using normf[THEN bspec[where x="x n - x N"]]
  3835         by auto
  3836       also have "norm (f (x n - x N)) < e * d"
  3837         using \<open>N \<le> n\<close> N unfolding f.diff[symmetric] by auto
  3838       finally show ?thesis
  3839         using \<open>e>0\<close> by simp
  3840     qed
  3841     then show ?thesis by auto
  3842   qed
  3843   then show ?thesis
  3844     by (simp add: Cauchy_altdef2 dist_norm)
  3845 qed
  3846 
  3847 lemma complete_isometric_image:
  3848   assumes "0 < e"
  3849     and s: "subspace s"
  3850     and f: "bounded_linear f"
  3851     and normf: "\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)"
  3852     and cs: "complete s"
  3853   shows "complete (f ` s)"
  3854 proof -
  3855   have "\<exists>l\<in>f ` s. (g \<longlongrightarrow> l) sequentially"
  3856     if as:"\<forall>n::nat. g n \<in> f ` s" and cfg:"Cauchy g" for g
  3857   proof -
  3858     from that obtain x where "\<forall>n. x n \<in> s \<and> g n = f (x n)"
  3859       using choice[of "\<lambda> n xa. xa \<in> s \<and> g n = f xa"] by auto
  3860     then have x: "\<forall>n. x n \<in> s" "\<forall>n. g n = f (x n)" by auto
  3861     then have "f \<circ> x = g" by (simp add: fun_eq_iff)
  3862     then obtain l where "l\<in>s" and l:"(x \<longlongrightarrow> l) sequentially"
  3863       using cs[unfolded complete_def, THEN spec[where x=x]]
  3864       using cauchy_isometric[OF \<open>0 < e\<close> s f normf] and cfg and x(1)
  3865       by auto
  3866     then show ?thesis
  3867       using linear_continuous_at[OF f, unfolded continuous_at_sequentially, THEN spec[where x=x], of l]
  3868       by (auto simp: \<open>f \<circ> x = g\<close>)
  3869   qed
  3870   then show ?thesis
  3871     unfolding complete_def by auto
  3872 qed
  3873 
  3874 lemma injective_imp_isometric:
  3875   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  3876   assumes s: "closed s" "subspace s"
  3877     and f: "bounded_linear f" "\<forall>x\<in>s. f x = 0 \<longrightarrow> x = 0"
  3878   shows "\<exists>e>0. \<forall>x\<in>s. norm (f x) \<ge> e * norm x"
  3879 proof (cases "s \<subseteq> {0::'a}")
  3880   case True
  3881   have "norm x \<le> norm (f x)" if "x \<in> s" for x
  3882   proof -
  3883     from True that have "x = 0" by auto
  3884     then show ?thesis by simp
  3885   qed
  3886   then show ?thesis
  3887     by (auto intro!: exI[where x=1])
  3888 next
  3889   case False
  3890   interpret f: bounded_linear f by fact
  3891   from False obtain a where a: "a \<noteq> 0" "a \<in> s"
  3892     by auto
  3893   from False have "s \<noteq> {}"
  3894     by auto
  3895   let ?S = "{f x| x. x \<in> s \<and> norm x = norm a}"
  3896   let ?S' = "{x::'a. x\<in>s \<and> norm x = norm a}"
  3897   let ?S'' = "{x::'a. norm x = norm a}"
  3898 
  3899   have "?S'' = frontier (cball 0 (norm a))"
  3900     by (simp add: sphere_def dist_norm)
  3901   then have "compact ?S''" by (metis compact_cball compact_frontier)
  3902   moreover have "?S' = s \<inter> ?S''" by auto
  3903   ultimately have "compact ?S'"
  3904     using closed_Int_compact[of s ?S''] using s(1) by auto
  3905   moreover have *:"f ` ?S' = ?S" by auto
  3906   ultimately have "compact ?S"
  3907     using compact_continuous_image[OF linear_continuous_on[OF f(1)], of ?S'] by auto
  3908   then have "closed ?S"
  3909     using compact_imp_closed by auto
  3910   moreover from a have "?S \<noteq> {}" by auto
  3911   ultimately obtain b' where "b'\<in>?S" "\<forall>y\<in>?S. norm b' \<le> norm y"
  3912     using distance_attains_inf[of ?S 0] unfolding dist_0_norm by auto
  3913   then obtain b where "b\<in>s"
  3914     and ba: "norm b = norm a"
  3915     and b: "\<forall>x\<in>{x \<in> s. norm x = norm a}. norm (f b) \<le> norm (f x)"
  3916     unfolding *[symmetric] unfolding image_iff by auto
  3917 
  3918   let ?e = "norm (f b) / norm b"
  3919   have "norm b > 0"
  3920     using ba and a and norm_ge_zero by auto
  3921   moreover have "norm (f b) > 0"
  3922     using f(2)[THEN bspec[where x=b], OF \<open>b\<in>s\<close>]
  3923     using \<open>norm b >0\<close> by simp
  3924   ultimately have "0 < norm (f b) / norm b" by simp
  3925   moreover
  3926   have "norm (f b) / norm b * norm x \<le> norm (f x)" if "x\<in>s" for x
  3927   proof (cases "x = 0")
  3928     case True
  3929     then show "norm (f b) / norm b * norm x \<le> norm (f x)"
  3930       by auto
  3931   next
  3932     case False
  3933     with \<open>a \<noteq> 0\<close> have *: "0 < norm a / norm x"
  3934       unfolding zero_less_norm_iff[symmetric] by simp
  3935     have "\<forall>x\<in>s. c *\<^sub>R x \<in> s" for c
  3936       using s[unfolded subspace_def] by simp
  3937     with \<open>x \<in> s\<close> \<open>x \<noteq> 0\<close> have "(norm a / norm x) *\<^sub>R x \<in> {x \<in> s. norm x = norm a}"
  3938       by simp
  3939     with \<open>x \<noteq> 0\<close> \<open>a \<noteq> 0\<close> show "norm (f b) / norm b * norm x \<le> norm (f x)"
  3940       using b[THEN bspec[where x="(norm a / norm x) *\<^sub>R x"]]
  3941       unfolding f.scaleR and ba
  3942       by (auto simp: mult.commute pos_le_divide_eq pos_divide_le_eq)
  3943   qed
  3944   ultimately show ?thesis by auto
  3945 qed
  3946 
  3947 lemma closed_injective_image_subspace:
  3948   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  3949   assumes "subspace s" "bounded_linear f" "\<forall>x\<in>s. f x = 0 \<longrightarrow> x = 0" "closed s"
  3950   shows "closed(f ` s)"
  3951 proof -
  3952   obtain e where "e > 0" and e: "\<forall>x\<in>s. e * norm x \<le> norm (f x)"
  3953     using injective_imp_isometric[OF assms(4,1,2,3)] by auto
  3954   show ?thesis
  3955     using complete_isometric_image[OF \<open>e>0\<close> assms(1,2) e] and assms(4)
  3956     unfolding complete_eq_closed[symmetric] by auto
  3957 qed
  3958 
  3959 
  3960 subsection \<open>Some properties of a canonical subspace\<close>
  3961 
  3962 lemma subspace_substandard: "subspace {x::'a::euclidean_space. (\<forall>i\<in>Basis. P i \<longrightarrow> x\<bullet>i = 0)}"
  3963   by (auto simp: subspace_def inner_add_left)
  3964 
  3965 lemma closed_substandard: "closed {x::'a::euclidean_space. \<forall>i\<in>Basis. P i \<longrightarrow> x\<bullet>i = 0}"
  3966   (is "closed ?A")
  3967 proof -
  3968   let ?D = "{i\<in>Basis. P i}"
  3969   have "closed (\<Inter>i\<in>?D. {x::'a. x\<bullet>i = 0})"
  3970     by (simp add: closed_INT closed_Collect_eq continuous_on_inner
  3971         continuous_on_const continuous_on_id)
  3972   also have "(\<Inter>i\<in>?D. {x::'a. x\<bullet>i = 0}) = ?A"
  3973     by auto
  3974   finally show "closed ?A" .
  3975 qed
  3976 
  3977 lemma dim_substandard:
  3978   assumes d: "d \<subseteq> Basis"
  3979   shows "dim {x::'a::euclidean_space. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0} = card d" (is "dim ?A = _")
  3980 proof (rule dim_unique)
  3981   from d show "d \<subseteq> ?A"
  3982     by (auto simp: inner_Basis)
  3983   from d show "independent d"
  3984     by (rule independent_mono [OF independent_Basis])
  3985   have "x \<in> span d" if "\<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0" for x
  3986   proof -
  3987     have "finite d"
  3988       by (rule finite_subset [OF d finite_Basis])
  3989     then have "(\<Sum>i\<in>d. (x \<bullet> i) *\<^sub>R i) \<in> span d"
  3990       by (simp add: span_sum span_clauses)
  3991     also have "(\<Sum>i\<in>d. (x \<bullet> i) *\<^sub>R i) = (\<Sum>i\<in>Basis. (x \<bullet> i) *\<^sub>R i)"
  3992       by (rule sum.mono_neutral_cong_left [OF finite_Basis d]) (auto simp: that)
  3993     finally show "x \<in> span d"
  3994       by (simp only: euclidean_representation)
  3995   qed
  3996   then show "?A \<subseteq> span d" by auto
  3997 qed simp
  3998 
  3999 text \<open>Hence closure and completeness of all subspaces.\<close>
  4000 lemma ex_card:
  4001   assumes "n \<le> card A"
  4002   shows "\<exists>S\<subseteq>A. card S = n"
  4003 proof (cases "finite A")
  4004   case True
  4005   from ex_bij_betw_nat_finite[OF this] obtain f where f: "bij_betw f {0..<card A} A" ..
  4006   moreover from f \<open>n \<le> card A\<close> have "{..< n} \<subseteq> {..< card A}" "inj_on f {..< n}"
  4007     by (auto simp: bij_betw_def intro: subset_inj_on)
  4008   ultimately have "f ` {..< n} \<subseteq> A" "card (f ` {..< n}) = n"
  4009     by (auto simp: bij_betw_def card_image)
  4010   then show ?thesis by blast
  4011 next
  4012   case False
  4013   with \<open>n \<le> card A\<close> show ?thesis by force
  4014 qed
  4015 
  4016 lemma closed_subspace:
  4017   fixes s :: "'a::euclidean_space set"
  4018   assumes "subspace s"
  4019   shows "closed s"
  4020 proof -
  4021   have "dim s \<le> card (Basis :: 'a set)"
  4022     using dim_subset_UNIV by auto
  4023   with ex_card[OF this] obtain d :: "'a set" where t: "card d = dim s" and d: "d \<subseteq> Basis"
  4024     by auto
  4025   let ?t = "{x::'a. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  4026   have "\<exists>f. linear f \<and> f ` {x::'a. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0} = s \<and>
  4027       inj_on f {x::'a. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0}"
  4028     using dim_substandard[of d] t d assms
  4029     by (intro subspace_isomorphism[OF subspace_substandard[of "\<lambda>i. i \<notin> d"]]) (auto simp: inner_Basis)
  4030   then obtain f where f:
  4031       "linear f"
  4032       "f ` {x. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0} = s"
  4033       "inj_on f {x. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0}"
  4034     by blast
  4035   interpret f: bounded_linear f
  4036     using f by (simp add: linear_conv_bounded_linear)
  4037   have "x \<in> ?t \<Longrightarrow> f x = 0 \<Longrightarrow> x = 0" for x
  4038     using f.zero d f(3)[THEN inj_onD, of x 0] by auto
  4039   moreover have "closed ?t" by (rule closed_substandard)
  4040   moreover have "subspace ?t" by (rule subspace_substandard)
  4041   ultimately show ?thesis
  4042     using closed_injective_image_subspace[of ?t f]
  4043     unfolding f(2) using f(1) unfolding linear_conv_bounded_linear by auto
  4044 qed
  4045 
  4046 lemma complete_subspace: "subspace s \<Longrightarrow> complete s"
  4047   for s :: "'a::euclidean_space set"
  4048   using complete_eq_closed closed_subspace by auto
  4049 
  4050 lemma closed_span [iff]: "closed (span s)"
  4051   for s :: "'a::euclidean_space set"
  4052   by (simp add: closed_subspace)
  4053 
  4054 lemma dim_closure [simp]: "dim (closure s) = dim s" (is "?dc = ?d")
  4055   for s :: "'a::euclidean_space set"
  4056 proof -
  4057   have "?dc \<le> ?d"
  4058     using closure_minimal[OF span_inc, of s]
  4059     using closed_subspace[OF subspace_span, of s]
  4060     using dim_subset[of "closure s" "span s"]
  4061     by simp
  4062   then show ?thesis
  4063     using dim_subset[OF closure_subset, of s]
  4064     by simp
  4065 qed
  4066 
  4067 
  4068 subsection \<open>Affine transformations of intervals\<close>
  4069 
  4070 lemma real_affinity_le: "0 < m \<Longrightarrow> m * x + c \<le> y \<longleftrightarrow> x \<le> inverse m * y + - (c / m)"
  4071   for m :: "'a::linordered_field"
  4072   by (simp add: field_simps)
  4073 
  4074 lemma real_le_affinity: "0 < m \<Longrightarrow> y \<le> m * x + c \<longleftrightarrow> inverse m * y + - (c / m) \<le> x"
  4075   for m :: "'a::linordered_field"
  4076   by (simp add: field_simps)
  4077 
  4078 lemma real_affinity_lt: "0 < m \<Longrightarrow> m * x + c < y \<longleftrightarrow> x < inverse m * y + - (c / m)"
  4079   for m :: "'a::linordered_field"
  4080   by (simp add: field_simps)
  4081 
  4082 lemma real_lt_affinity: "0 < m \<Longrightarrow> y < m * x + c \<longleftrightarrow> inverse m * y + - (c / m) < x"
  4083   for m :: "'a::linordered_field"
  4084   by (simp add: field_simps)
  4085 
  4086 lemma real_affinity_eq: "m \<noteq> 0 \<Longrightarrow> m * x + c = y \<longleftrightarrow> x = inverse m * y + - (c / m)"
  4087   for m :: "'a::linordered_field"
  4088   by (simp add: field_simps)
  4089 
  4090 lemma real_eq_affinity: "m \<noteq> 0 \<Longrightarrow> y = m * x + c  \<longleftrightarrow> inverse m * y + - (c / m) = x"
  4091   for m :: "'a::linordered_field"
  4092   by (simp add: field_simps)
  4093 
  4094 
  4095 subsection \<open>Banach fixed point theorem (not really topological ...)\<close>
  4096 
  4097 theorem banach_fix:
  4098   assumes s: "complete s" "s \<noteq> {}"
  4099     and c: "0 \<le> c" "c < 1"
  4100     and f: "f ` s \<subseteq> s"
  4101     and lipschitz: "\<forall>x\<in>s. \<forall>y\<in>s. dist (f x) (f y) \<le> c * dist x y"
  4102   shows "\<exists>!x\<in>s. f x = x"
  4103 proof -
  4104   from c have "1 - c > 0" by simp
  4105 
  4106   from s(2) obtain z0 where z0: "z0 \<in> s" by blast
  4107   define z where "z n = (f ^^ n) z0" for n
  4108   with f z0 have z_in_s: "z n \<in> s" for n :: nat
  4109     by (induct n) auto
  4110   define d where "d = dist (z 0) (z 1)"
  4111 
  4112   have fzn: "f (z n) = z (Suc n)" for n
  4113     by (simp add: z_def)
  4114   have cf_z: "dist (z n) (z (Suc n)) \<le> (c ^ n) * d" for n :: nat
  4115   proof (induct n)
  4116     case 0
  4117     then show ?case
  4118       by (simp add: d_def)
  4119   next
  4120     case (Suc m)
  4121     with \<open>0 \<le> c\<close> have "c * dist (z m) (z (Suc m)) \<le> c ^ Suc m * d"
  4122       using mult_left_mono[of "dist (z m) (z (Suc m))" "c ^ m * d" c] by simp
  4123     then show ?case
  4124       using lipschitz[THEN bspec[where x="z m"], OF z_in_s, THEN bspec[where x="z (Suc m)"], OF z_in_s]
  4125       by (simp add: fzn mult_le_cancel_left)
  4126   qed
  4127 
  4128   have cf_z2: "(1 - c) * dist (z m) (z (m + n)) \<le> (c ^ m) * d * (1 - c ^ n)" for n m :: nat
  4129   proof (induct n)
  4130     case 0
  4131     show ?case by simp
  4132   next
  4133     case (Suc k)
  4134     from c have "(1 - c) * dist (z m) (z (m + Suc k)) \<le>
  4135         (1 - c) * (dist (z m) (z (m + k)) + dist (z (m + k)) (z (Suc (m + k))))"
  4136       by (simp add: dist_triangle)
  4137     also from c cf_z[of "m + k"] have "\<dots> \<le> (1 - c) * (dist (z m) (z (m + k)) + c ^ (m + k) * d)"
  4138       by simp
  4139     also from Suc have "\<dots> \<le> c ^ m * d * (1 - c ^ k) + (1 - c) * c ^ (m + k) * d"
  4140       by (simp add: field_simps)
  4141     also have "\<dots> = (c ^ m) * (d * (1 - c ^ k) + (1 - c) * c ^ k * d)"
  4142       by (simp add: power_add field_simps)
  4143     also from c have "\<dots> \<le> (c ^ m) * d * (1 - c ^ Suc k)"
  4144       by (simp add: field_simps)
  4145     finally show ?case by simp
  4146   qed
  4147 
  4148   have "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (z m) (z n) < e" if "e > 0" for e
  4149   proof (cases "d = 0")
  4150     case True
  4151     from \<open>1 - c > 0\<close> have "(1 - c) * x \<le> 0 \<longleftrightarrow> x \<le> 0" for x
  4152       by (metis mult_zero_left mult.commute real_mult_le_cancel_iff1)
  4153     with c cf_z2[of 0] True have "z n = z0" for n
  4154       by (simp add: z_def)
  4155     with \<open>e > 0\<close> show ?thesis by simp
  4156   next
  4157     case False
  4158     with zero_le_dist[of "z 0" "z 1"] have "d > 0"
  4159       by (metis d_def less_le)
  4160     with \<open>1 - c > 0\<close> \<open>e > 0\<close> have "0 < e * (1 - c) / d"
  4161       by simp
  4162     with c obtain N where N: "c ^ N < e * (1 - c) / d"
  4163       using real_arch_pow_inv[of "e * (1 - c) / d" c] by auto
  4164     have *: "dist (z m) (z n) < e" if "m > n" and as: "m \<ge> N" "n \<ge> N" for m n :: nat
  4165     proof -
  4166       from c \<open>n \<ge> N\<close> have *: "c ^ n \<le> c ^ N"
  4167         using power_decreasing[OF \<open>n\<ge>N\<close>, of c] by simp
  4168       from c \<open>m > n\<close> have "1 - c ^ (m - n) > 0"
  4169         using power_strict_mono[of c 1 "m - n"] by simp
  4170       with \<open>d > 0\<close> \<open>0 < 1 - c\<close> have **: "d * (1 - c ^ (m - n)) / (1 - c) > 0"
  4171         by simp
  4172       from cf_z2[of n "m - n"] \<open>m > n\<close>
  4173       have "dist (z m) (z n) \<le> c ^ n * d * (1 - c ^ (m - n)) / (1 - c)"
  4174         by (simp add: pos_le_divide_eq[OF \<open>1 - c > 0\<close>] mult.commute dist_commute)
  4175       also have "\<dots> \<le> c ^ N * d * (1 - c ^ (m - n)) / (1 - c)"
  4176         using mult_right_mono[OF * order_less_imp_le[OF **]]
  4177         by (simp add: mult.assoc)
  4178       also have "\<dots> < (e * (1 - c) / d) * d * (1 - c ^ (m - n)) / (1 - c)"
  4179         using mult_strict_right_mono[OF N **] by (auto simp: mult.assoc)
  4180       also from c \<open>d > 0\<close> \<open>1 - c > 0\<close> have "\<dots> = e * (1 - c ^ (m - n))"
  4181         by simp
  4182       also from c \<open>1 - c ^ (m - n) > 0\<close> \<open>e > 0\<close> have "\<dots> \<le> e"
  4183         using mult_right_le_one_le[of e "1 - c ^ (m - n)"] by auto
  4184       finally show ?thesis by simp
  4185     qed
  4186     have "dist (z n) (z m) < e" if "N \<le> m" "N \<le> n" for m n :: nat
  4187     proof (cases "n = m")
  4188       case True
  4189       with \<open>e > 0\<close> show ?thesis by simp
  4190     next
  4191       case False
  4192       with *[of n m] *[of m n] and that show ?thesis
  4193         by (auto simp: dist_commute nat_neq_iff)
  4194     qed
  4195     then show ?thesis by auto
  4196   qed
  4197   then have "Cauchy z"
  4198     by (simp add: cauchy_def)
  4199   then obtain x where "x\<in>s" and x:"(z \<longlongrightarrow> x) sequentially"
  4200     using s(1)[unfolded compact_def complete_def, THEN spec[where x=z]] and z_in_s by auto
  4201 
  4202   define e where "e = dist (f x) x"
  4203   have "e = 0"
  4204   proof (rule ccontr)
  4205     assume "e \<noteq> 0"
  4206     then have "e > 0"
  4207       unfolding e_def using zero_le_dist[of "f x" x]
  4208       by (metis dist_eq_0_iff dist_nz e_def)
  4209     then obtain N where N:"\<forall>n\<ge>N. dist (z n) x < e / 2"
  4210       using x[unfolded lim_sequentially, THEN spec[where x="e/2"]] by auto
  4211     then have N':"dist (z N) x < e / 2" by auto
  4212     have *: "c * dist (z N) x \<le> dist (z N) x"
  4213       unfolding mult_le_cancel_right2
  4214       using zero_le_dist[of "z N" x] and c
  4215       by (metis dist_eq_0_iff dist_nz order_less_asym less_le)
  4216     have "dist (f (z N)) (f x) \<le> c * dist (z N) x"
  4217       using lipschitz[THEN bspec[where x="z N"], THEN bspec[where x=x]]
  4218       using z_in_s[of N] \<open>x\<in>s\<close>
  4219       using c
  4220       by auto
  4221     also have "\<dots> < e / 2"
  4222       using N' and c using * by auto
  4223     finally show False
  4224       unfolding fzn
  4225       using N[THEN spec[where x="Suc N"]] and dist_triangle_half_r[of "z (Suc N)" "f x" e x]
  4226       unfolding e_def
  4227       by auto
  4228   qed
  4229   then have "f x = x" by (auto simp: e_def)
  4230   moreover have "y = x" if "f y = y" "y \<in> s" for y
  4231   proof -
  4232     from \<open>x \<in> s\<close> \<open>f x = x\<close> that have "dist x y \<le> c * dist x y"
  4233       using lipschitz[THEN bspec[where x=x], THEN bspec[where x=y]] by simp
  4234     with c and zero_le_dist[of x y] have "dist x y = 0"
  4235       by (simp add: mult_le_cancel_right1)
  4236     then show ?thesis by simp
  4237   qed
  4238   ultimately show ?thesis
  4239     using \<open>x\<in>s\<close> by blast
  4240 qed
  4241 
  4242 
  4243 subsection \<open>Edelstein fixed point theorem\<close>
  4244 
  4245 theorem edelstein_fix:
  4246   fixes s :: "'a::metric_space set"
  4247   assumes s: "compact s" "s \<noteq> {}"
  4248     and gs: "(g ` s) \<subseteq> s"
  4249     and dist: "\<forall>x\<in>s. \<forall>y\<in>s. x \<noteq> y \<longrightarrow> dist (g x) (g y) < dist x y"
  4250   shows "\<exists>!x\<in>s. g x = x"
  4251 proof -
  4252   let ?D = "(\<lambda>x. (x, x)) ` s"
  4253   have D: "compact ?D" "?D \<noteq> {}"
  4254     by (rule compact_continuous_image)
  4255        (auto intro!: s continuous_Pair continuous_ident simp: continuous_on_eq_continuous_within)
  4256 
  4257   have "\<And>x y e. x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> 0 < e \<Longrightarrow> dist y x < e \<Longrightarrow> dist (g y) (g x) < e"
  4258     using dist by fastforce
  4259   then have "continuous_on s g"
  4260     by (auto simp: continuous_on_iff)
  4261   then have cont: "continuous_on ?D (\<lambda>x. dist ((g \<circ> fst) x) (snd x))"
  4262     unfolding continuous_on_eq_continuous_within
  4263     by (intro continuous_dist ballI continuous_within_compose)
  4264        (auto intro!: continuous_fst continuous_snd continuous_ident simp: image_image)
  4265 
  4266   obtain a where "a \<in> s" and le: "\<And>x. x \<in> s \<Longrightarrow> dist (g a) a \<le> dist (g x) x"
  4267     using continuous_attains_inf[OF D cont] by auto
  4268 
  4269   have "g a = a"
  4270   proof (rule ccontr)
  4271     assume "g a \<noteq> a"
  4272     with \<open>a \<in> s\<close> gs have "dist (g (g a)) (g a) < dist (g a) a"
  4273       by (intro dist[rule_format]) auto
  4274     moreover have "dist (g a) a \<le> dist (g (g a)) (g a)"
  4275       using \<open>a \<in> s\<close> gs by (intro le) auto
  4276     ultimately show False by auto
  4277   qed
  4278   moreover have "\<And>x. x \<in> s \<Longrightarrow> g x = x \<Longrightarrow> x = a"
  4279     using dist[THEN bspec[where x=a]] \<open>g a = a\<close> and \<open>a\<in>s\<close> by auto
  4280   ultimately show "\<exists>!x\<in>s. g x = x"
  4281     using \<open>a \<in> s\<close> by blast
  4282 qed
  4283 
  4284 
  4285 lemma cball_subset_cball_iff:
  4286   fixes a :: "'a :: euclidean_space"
  4287   shows "cball a r \<subseteq> cball a' r' \<longleftrightarrow> dist a a' + r \<le> r' \<or> r < 0"
  4288     (is "?lhs \<longleftrightarrow> ?rhs")
  4289 proof
  4290   assume ?lhs
  4291   then show ?rhs
  4292   proof (cases "r < 0")
  4293     case True
  4294     then show ?rhs by simp
  4295   next
  4296     case False
  4297     then have [simp]: "r \<ge> 0" by simp
  4298     have "norm (a - a') + r \<le> r'"
  4299     proof (cases "a = a'")
  4300       case True
  4301       then show ?thesis
  4302         using subsetD [where c = "a + r *\<^sub>R (SOME i. i \<in> Basis)", OF \<open>?lhs\<close>] subsetD [where c = a, OF \<open>?lhs\<close>]
  4303         by (force simp: SOME_Basis dist_norm)
  4304     next
  4305       case False
  4306       have "norm (a' - (a + (r / norm (a - a')) *\<^sub>R (a - a'))) = norm (a' - a - (r / norm (a - a')) *\<^sub>R (a - a'))"
  4307         by (simp add: algebra_simps)
  4308       also have "... = norm ((-1 - (r / norm (a - a'))) *\<^sub>R (a - a'))"
  4309         by (simp add: algebra_simps)
  4310       also from \<open>a \<noteq> a'\<close> have "... = \<bar>- norm (a - a') - r\<bar>"
  4311         by (simp add: abs_mult_pos field_simps)
  4312       finally have [simp]: "norm (a' - (a + (r / norm (a - a')) *\<^sub>R (a - a'))) = \<bar>norm (a - a') + r\<bar>"
  4313         by linarith
  4314       from \<open>a \<noteq> a'\<close> show ?thesis
  4315         using subsetD [where c = "a' + (1 + r / norm(a - a')) *\<^sub>R (a - a')", OF \<open>?lhs\<close>]
  4316         by (simp add: dist_norm scaleR_add_left)
  4317     qed
  4318     then show ?rhs
  4319       by (simp add: dist_norm)
  4320   qed
  4321 next
  4322   assume ?rhs
  4323   then show ?lhs
  4324     by (auto simp: ball_def dist_norm)
  4325       (metis add.commute add_le_cancel_right dist_norm dist_triangle3 order_trans)
  4326 qed
  4327 
  4328 lemma cball_subset_ball_iff: "cball a r \<subseteq> ball a' r' \<longleftrightarrow> dist a a' + r < r' \<or> r < 0"
  4329   (is "?lhs \<longleftrightarrow> ?rhs")
  4330   for a :: "'a::euclidean_space"
  4331 proof
  4332   assume ?lhs
  4333   then show ?rhs
  4334   proof (cases "r < 0")
  4335     case True then
  4336     show ?rhs by simp
  4337   next
  4338     case False
  4339     then have [simp]: "r \<ge> 0" by simp
  4340     have "norm (a - a') + r < r'"
  4341     proof (cases "a = a'")
  4342       case True
  4343       then show ?thesis
  4344         using subsetD [where c = "a + r *\<^sub>R (SOME i. i \<in> Basis)", OF \<open>?lhs\<close>] subsetD [where c = a, OF \<open>?lhs\<close>]
  4345         by (force simp: SOME_Basis dist_norm)
  4346     next
  4347       case False
  4348       have False if "norm (a - a') + r \<ge> r'"
  4349       proof -
  4350         from that have "\<bar>r' - norm (a - a')\<bar> \<le> r"
  4351           by (simp split: abs_split)
  4352             (metis \<open>0 \<le> r\<close> \<open>?lhs\<close> centre_in_cball dist_commute dist_norm less_asym mem_ball subset_eq)
  4353         then show ?thesis
  4354           using subsetD [where c = "a + (r' / norm(a - a') - 1) *\<^sub>R (a - a')", OF \<open>?lhs\<close>] \<open>a \<noteq> a'\<close>
  4355           by (simp add: dist_norm field_simps)
  4356             (simp add: diff_divide_distrib scaleR_left_diff_distrib)
  4357       qed
  4358       then show ?thesis by force
  4359     qed
  4360     then show ?rhs by (simp add: dist_norm)
  4361   qed
  4362 next
  4363   assume ?rhs
  4364   then show ?lhs
  4365     by (auto simp: ball_def dist_norm)
  4366       (metis add.commute add_le_cancel_right dist_norm dist_triangle3 le_less_trans)
  4367 qed
  4368 
  4369 lemma ball_subset_cball_iff: "ball a r \<subseteq> cball a' r' \<longleftrightarrow> dist a a' + r \<le> r' \<or> r \<le> 0"
  4370   (is "?lhs = ?rhs")
  4371   for a :: "'a::euclidean_space"
  4372 proof (cases "r \<le> 0")
  4373   case True
  4374   then show ?thesis
  4375     using dist_not_less_zero less_le_trans by force
  4376 next
  4377   case False
  4378   show ?thesis
  4379   proof
  4380     assume ?lhs
  4381     then have "(cball a r \<subseteq> cball a' r')"
  4382       by (metis False closed_cball closure_ball closure_closed closure_mono not_less)
  4383     with False show ?rhs
  4384       by (fastforce iff: cball_subset_cball_iff)
  4385   next
  4386     assume ?rhs
  4387     with False show ?lhs
  4388       using ball_subset_cball cball_subset_cball_iff by blast
  4389   qed
  4390 qed
  4391 
  4392 lemma ball_subset_ball_iff:
  4393   fixes a :: "'a :: euclidean_space"
  4394   shows "ball a r \<subseteq> ball a' r' \<longleftrightarrow> dist a a' + r \<le> r' \<or> r \<le> 0"
  4395         (is "?lhs = ?rhs")
  4396 proof (cases "r \<le> 0")
  4397   case True then show ?thesis
  4398     using dist_not_less_zero less_le_trans by force
  4399 next
  4400   case False show ?thesis
  4401   proof
  4402     assume ?lhs
  4403     then have "0 < r'"
  4404       by (metis (no_types) False \<open>?lhs\<close> centre_in_ball dist_norm le_less_trans mem_ball norm_ge_zero not_less set_mp)
  4405     then have "(cball a r \<subseteq> cball a' r')"
  4406       by (metis False\<open>?lhs\<close> closure_ball closure_mono not_less)
  4407     then show ?rhs
  4408       using False cball_subset_cball_iff by fastforce
  4409   next
  4410   assume ?rhs then show ?lhs
  4411     apply (auto simp: ball_def)
  4412     apply (metis add.commute add_le_cancel_right dist_commute dist_triangle_lt not_le order_trans)
  4413     using dist_not_less_zero order.strict_trans2 apply blast
  4414     done
  4415   qed
  4416 qed
  4417 
  4418 
  4419 lemma ball_eq_ball_iff:
  4420   fixes x :: "'a :: euclidean_space"
  4421   shows "ball x d = ball y e \<longleftrightarrow> d \<le> 0 \<and> e \<le> 0 \<or> x=y \<and> d=e"
  4422         (is "?lhs = ?rhs")
  4423 proof
  4424   assume ?lhs
  4425   then show ?rhs
  4426   proof (cases "d \<le> 0 \<or> e \<le> 0")
  4427     case True
  4428       with \<open>?lhs\<close> show ?rhs
  4429         by safe (simp_all only: ball_eq_empty [of y e, symmetric] ball_eq_empty [of x d, symmetric])
  4430   next
  4431     case False
  4432     with \<open>?lhs\<close> show ?rhs
  4433       apply (auto simp: set_eq_subset ball_subset_ball_iff dist_norm norm_minus_commute algebra_simps)
  4434       apply (metis add_le_same_cancel1 le_add_same_cancel1 norm_ge_zero norm_pths(2) order_trans)
  4435       apply (metis add_increasing2 add_le_imp_le_right eq_iff norm_ge_zero)
  4436       done
  4437   qed
  4438 next
  4439   assume ?rhs then show ?lhs
  4440     by (auto simp: set_eq_subset ball_subset_ball_iff)
  4441 qed
  4442 
  4443 lemma cball_eq_cball_iff:
  4444   fixes x :: "'a :: euclidean_space"
  4445   shows "cball x d = cball y e \<longleftrightarrow> d < 0 \<and> e < 0 \<or> x=y \<and> d=e"
  4446         (is "?lhs = ?rhs")
  4447 proof
  4448   assume ?lhs
  4449   then show ?rhs
  4450   proof (cases "d < 0 \<or> e < 0")
  4451     case True
  4452       with \<open>?lhs\<close> show ?rhs
  4453         by safe (simp_all only: cball_eq_empty [of y e, symmetric] cball_eq_empty [of x d, symmetric])
  4454   next
  4455     case False
  4456     with \<open>?lhs\<close> show ?rhs
  4457       apply (auto simp: set_eq_subset cball_subset_cball_iff dist_norm norm_minus_commute algebra_simps)
  4458       apply (metis add_le_same_cancel1 le_add_same_cancel1 norm_ge_zero norm_pths(2) order_trans)
  4459       apply (metis add_increasing2 add_le_imp_le_right eq_iff norm_ge_zero)
  4460       done
  4461   qed
  4462 next
  4463   assume ?rhs then show ?lhs
  4464     by (auto simp: set_eq_subset cball_subset_cball_iff)
  4465 qed
  4466 
  4467 lemma ball_eq_cball_iff:
  4468   fixes x :: "'a :: euclidean_space"
  4469   shows "ball x d = cball y e \<longleftrightarrow> d \<le> 0 \<and> e < 0" (is "?lhs = ?rhs")
  4470 proof
  4471   assume ?lhs
  4472   then show ?rhs
  4473     apply (auto simp: set_eq_subset ball_subset_cball_iff cball_subset_ball_iff algebra_simps)
  4474     apply (metis add_increasing2 add_le_cancel_right add_less_same_cancel1 dist_not_less_zero less_le_trans zero_le_dist)
  4475     apply (metis add_less_same_cancel1 dist_not_less_zero less_le_trans not_le)
  4476     using \<open>?lhs\<close> ball_eq_empty cball_eq_empty apply blast+
  4477     done
  4478 next
  4479   assume ?rhs then show ?lhs by auto
  4480 qed
  4481 
  4482 lemma cball_eq_ball_iff:
  4483   fixes x :: "'a :: euclidean_space"
  4484   shows "cball x d = ball y e \<longleftrightarrow> d < 0 \<and> e \<le> 0"
  4485   using ball_eq_cball_iff by blast
  4486 
  4487 lemma finite_ball_avoid:
  4488   fixes S :: "'a :: euclidean_space set"
  4489   assumes "open S" "finite X" "p \<in> S"
  4490   shows "\<exists>e>0. \<forall>w\<in>ball p e. w\<in>S \<and> (w\<noteq>p \<longrightarrow> w\<notin>X)"
  4491 proof -
  4492   obtain e1 where "0 < e1" and e1_b:"ball p e1 \<subseteq> S"
  4493     using open_contains_ball_eq[OF \<open>open S\<close>] assms by auto
  4494   obtain e2 where "0 < e2" and "\<forall>x\<in>X. x \<noteq> p \<longrightarrow> e2 \<le> dist p x"
  4495     using finite_set_avoid[OF \<open>finite X\<close>,of p] by auto
  4496   hence "\<forall>w\<in>ball p (min e1 e2). w\<in>S \<and> (w\<noteq>p \<longrightarrow> w\<notin>X)" using e1_b by auto
  4497   thus "\<exists>e>0. \<forall>w\<in>ball p e. w \<in> S \<and> (w \<noteq> p \<longrightarrow> w \<notin> X)" using \<open>e2>0\<close> \<open>e1>0\<close>
  4498     apply (rule_tac x="min e1 e2" in exI)
  4499     by auto
  4500 qed
  4501 
  4502 lemma finite_cball_avoid:
  4503   fixes S :: "'a :: euclidean_space set"
  4504   assumes "open S" "finite X" "p \<in> S"
  4505   shows "\<exists>e>0. \<forall>w\<in>cball p e. w\<in>S \<and> (w\<noteq>p \<longrightarrow> w\<notin>X)"
  4506 proof -
  4507   obtain e1 where "e1>0" and e1: "\<forall>w\<in>ball p e1. w\<in>S \<and> (w\<noteq>p \<longrightarrow> w\<notin>X)"
  4508     using finite_ball_avoid[OF assms] by auto
  4509   define e2 where "e2 \<equiv> e1/2"
  4510   have "e2>0" and "e2 < e1" unfolding e2_def using \<open>e1>0\<close> by auto
  4511   then have "cball p e2 \<subseteq> ball p e1" by (subst cball_subset_ball_iff,auto)
  4512   then show "\<exists>e>0. \<forall>w\<in>cball p e. w \<in> S \<and> (w \<noteq> p \<longrightarrow> w \<notin> X)" using \<open>e2>0\<close> e1 by auto
  4513 qed
  4514 
  4515 subsection\<open>Various separability-type properties\<close>
  4516 
  4517 lemma univ_second_countable:
  4518   obtains \<B> :: "'a::euclidean_space set set"
  4519   where "countable \<B>" "\<And>C. C \<in> \<B> \<Longrightarrow> open C"
  4520        "\<And>S. open S \<Longrightarrow> \<exists>U. U \<subseteq> \<B> \<and> S = \<Union>U"
  4521 by (metis ex_countable_basis topological_basis_def)
  4522 
  4523 lemma subset_second_countable:
  4524   obtains \<B> :: "'a:: euclidean_space set set"
  4525     where "countable \<B>"
  4526           "{} \<notin> \<B>"
  4527           "\<And>C. C \<in> \<B> \<Longrightarrow> openin(subtopology euclidean S) C"
  4528           "\<And>T. openin(subtopology euclidean S) T \<Longrightarrow> \<exists>\<U>. \<U> \<subseteq> \<B> \<and> T = \<Union>\<U>"
  4529 proof -
  4530   obtain \<B> :: "'a set set"
  4531     where "countable \<B>"
  4532       and opeB: "\<And>C. C \<in> \<B> \<Longrightarrow> openin(subtopology euclidean S) C"
  4533       and \<B>:    "\<And>T. openin(subtopology euclidean S) T \<Longrightarrow> \<exists>\<U>. \<U> \<subseteq> \<B> \<and> T = \<Union>\<U>"
  4534   proof -
  4535     obtain \<C> :: "'a set set"
  4536       where "countable \<C>" and ope: "\<And>C. C \<in> \<C> \<Longrightarrow> open C"
  4537         and \<C>: "\<And>S. open S \<Longrightarrow> \<exists>U. U \<subseteq> \<C> \<and> S = \<Union>U"
  4538       by (metis univ_second_countable that)
  4539     show ?thesis
  4540     proof
  4541       show "countable ((\<lambda>C. S \<inter> C) ` \<C>)"
  4542         by (simp add: \<open>countable \<C>\<close>)
  4543       show "\<And>C. C \<in> op \<inter> S ` \<C> \<Longrightarrow> openin (subtopology euclidean S) C"
  4544         using ope by auto
  4545       show "\<And>T. openin (subtopology euclidean S) T \<Longrightarrow> \<exists>\<U>\<subseteq>op \<inter> S ` \<C>. T = \<Union>\<U>"
  4546         by (metis \<C> image_mono inf_Sup openin_open)
  4547     qed
  4548   qed
  4549   show ?thesis
  4550   proof
  4551     show "countable (\<B> - {{}})"
  4552       using \<open>countable \<B>\<close> by blast
  4553     show "\<And>C. \<lbrakk>C \<in> \<B> - {{}}\<rbrakk> \<Longrightarrow> openin (subtopology euclidean S) C"
  4554       by (simp add: \<open>\<And>C. C \<in> \<B> \<Longrightarrow> openin (subtopology euclidean S) C\<close>)
  4555     show "\<exists>\<U>\<subseteq>\<B> - {{}}. T = \<Union>\<U>" if "openin (subtopology euclidean S) T" for T
  4556       using \<B> [OF that]
  4557       apply clarify
  4558       apply (rule_tac x="\<U> - {{}}" in exI, auto)
  4559         done
  4560   qed auto
  4561 qed
  4562 
  4563 lemma univ_second_countable_sequence:
  4564   obtains B :: "nat \<Rightarrow> 'a::euclidean_space set"
  4565     where "inj B" "\<And>n. open(B n)" "\<And>S. open S \<Longrightarrow> \<exists>k. S = \<Union>{B n |n. n \<in> k}"
  4566 proof -
  4567   obtain \<B> :: "'a set set"
  4568   where "countable \<B>"
  4569     and op: "\<And>C. C \<in> \<B> \<Longrightarrow> open C"
  4570     and Un: "\<And>S. open S \<Longrightarrow> \<exists>U. U \<subseteq> \<B> \<and> S = \<Union>U"
  4571     using univ_second_countable by blast
  4572   have *: "infinite (range (\<lambda>n. ball (0::'a) (inverse(Suc n))))"
  4573     apply (rule Infinite_Set.range_inj_infinite)
  4574     apply (simp add: inj_on_def ball_eq_ball_iff)
  4575     done
  4576   have "infinite \<B>"
  4577   proof
  4578     assume "finite \<B>"
  4579     then have "finite (Union ` (Pow \<B>))"
  4580       by simp
  4581     then have "finite (range (\<lambda>n. ball (0::'a) (inverse(Suc n))))"
  4582       apply (rule rev_finite_subset)
  4583       by (metis (no_types, lifting) PowI image_eqI image_subset_iff Un [OF open_ball])
  4584     with * show False by simp
  4585   qed
  4586   obtain f :: "nat \<Rightarrow> 'a set" where "\<B> = range f" "inj f"
  4587     by (blast intro: countable_as_injective_image [OF \<open>countable \<B>\<close> \<open>infinite \<B>\<close>])
  4588   have *: "\<exists>k. S = \<Union>{f n |n. n \<in> k}" if "open S" for S
  4589     using Un [OF that]
  4590     apply clarify
  4591     apply (rule_tac x="f-`U" in exI)
  4592     using \<open>inj f\<close> \<open>\<B> = range f\<close> apply force
  4593     done
  4594   show ?thesis
  4595     apply (rule that [OF \<open>inj f\<close> _ *])
  4596     apply (auto simp: \<open>\<B> = range f\<close> op)
  4597     done
  4598 qed
  4599 
  4600 proposition separable:
  4601   fixes S :: "'a:: euclidean_space set"
  4602   obtains T where "countable T" "T \<subseteq> S" "S \<subseteq> closure T"
  4603 proof -
  4604   obtain \<B> :: "'a:: euclidean_space set set"
  4605     where "countable \<B>"
  4606       and "{} \<notin> \<B>"
  4607       and ope: "\<And>C. C \<in> \<B> \<Longrightarrow> openin(subtopology euclidean S) C"
  4608       and if_ope: "\<And>T. openin(subtopology euclidean S) T \<Longrightarrow> \<exists>\<U>. \<U> \<subseteq> \<B> \<and> T = \<Union>\<U>"
  4609     by (meson subset_second_countable)
  4610   then obtain f where f: "\<And>C. C \<in> \<B> \<Longrightarrow> f C \<in> C"
  4611     by (metis equals0I)
  4612   show ?thesis
  4613   proof
  4614     show "countable (f ` \<B>)"
  4615       by (simp add: \<open>countable \<B>\<close>)
  4616     show "f ` \<B> \<subseteq> S"
  4617       using ope f openin_imp_subset by blast
  4618     show "S \<subseteq> closure (f ` \<B>)"
  4619     proof (clarsimp simp: closure_approachable)
  4620       fix x and e::real
  4621       assume "x \<in> S" "0 < e"
  4622       have "openin (subtopology euclidean S) (S \<inter> ball x e)"
  4623         by (simp add: openin_Int_open)
  4624       with if_ope obtain \<U> where  \<U>: "\<U> \<subseteq> \<B>" "S \<inter> ball x e = \<Union>\<U>"
  4625         by meson
  4626       show "\<exists>C \<in> \<B>. dist (f C) x < e"
  4627       proof (cases "\<U> = {}")
  4628         case True
  4629         then show ?thesis
  4630           using \<open>0 < e\<close>  \<U> \<open>x \<in> S\<close> by auto
  4631       next
  4632         case False
  4633         then obtain C where "C \<in> \<U>" by blast
  4634         show ?thesis
  4635         proof
  4636           show "dist (f C) x < e"
  4637             by (metis Int_iff Union_iff \<U> \<open>C \<in> \<U>\<close> dist_commute f mem_ball subsetCE)
  4638           show "C \<in> \<B>"
  4639             using \<open>\<U> \<subseteq> \<B>\<close> \<open>C \<in> \<U>\<close> by blast
  4640         qed
  4641       qed
  4642     qed
  4643   qed
  4644 qed
  4645 
  4646 proposition Lindelof:
  4647   fixes \<F> :: "'a::euclidean_space set set"
  4648   assumes \<F>: "\<And>S. S \<in> \<F> \<Longrightarrow> open S"
  4649   obtains \<F>' where "\<F>' \<subseteq> \<F>" "countable \<F>'" "\<Union>\<F>' = \<Union>\<F>"
  4650 proof -
  4651   obtain \<B> :: "'a set set"
  4652     where "countable \<B>" "\<And>C. C \<in> \<B> \<Longrightarrow> open C"
  4653       and \<B>: "\<And>S. open S \<Longrightarrow> \<exists>U. U \<subseteq> \<B> \<and> S = \<Union>U"
  4654     using univ_second_countable by blast
  4655   define \<D> where "\<D> \<equiv> {S. S \<in> \<B> \<and> (\<exists>U. U \<in> \<F> \<and> S \<subseteq> U)}"
  4656   have "countable \<D>"
  4657     apply (rule countable_subset [OF _ \<open>countable \<B>\<close>])
  4658     apply (force simp: \<D>_def)
  4659     done
  4660   have "\<And>S. \<exists>U. S \<in> \<D> \<longrightarrow> U \<in> \<F> \<and> S \<subseteq> U"
  4661     by (simp add: \<D>_def)
  4662   then obtain G where G: "\<And>S. S \<in> \<D> \<longrightarrow> G S \<in> \<F> \<and> S \<subseteq> G S"
  4663     by metis
  4664   have "\<Union>\<F> \<subseteq> \<Union>\<D>"
  4665     unfolding \<D>_def by (blast dest: \<F> \<B>)
  4666   moreover have "\<Union>\<D> \<subseteq> \<Union>\<F>"
  4667     using \<D>_def by blast
  4668   ultimately have eq1: "\<Union>\<F> = \<Union>\<D>" ..
  4669   have eq2: "\<Union>\<D> = UNION \<D> G"
  4670     using G eq1 by auto
  4671   show ?thesis
  4672     apply (rule_tac \<F>' = "G ` \<D>" in that)
  4673     using G \<open>countable \<D>\<close>  apply (auto simp: eq1 eq2)
  4674     done
  4675 qed
  4676 
  4677 lemma Lindelof_openin:
  4678   fixes \<F> :: "'a::euclidean_space set set"
  4679   assumes "\<And>S. S \<in> \<F> \<Longrightarrow> openin (subtopology euclidean U) S"
  4680   obtains \<F>' where "\<F>' \<subseteq> \<F>" "countable \<F>'" "\<Union>\<F>' = \<Union>\<F>"
  4681 proof -
  4682   have "\<And>S. S \<in> \<F> \<Longrightarrow> \<exists>T. open T \<and> S = U \<inter> T"
  4683     using assms by (simp add: openin_open)
  4684   then obtain tf where tf: "\<And>S. S \<in> \<F> \<Longrightarrow> open (tf S) \<and> (S = U \<inter> tf S)"
  4685     by metis
  4686   have [simp]: "\<And>\<F>'. \<F>' \<subseteq> \<F> \<Longrightarrow> \<Union>\<F>' = U \<inter> \<Union>(tf ` \<F>')"
  4687     using tf by fastforce
  4688   obtain \<G> where "countable \<G> \<and> \<G> \<subseteq> tf ` \<F>" "\<Union>\<G> = UNION \<F> tf"
  4689     using tf by (force intro: Lindelof [of "tf ` \<F>"])
  4690   then obtain \<F>' where \<F>': "\<F>' \<subseteq> \<F>" "countable \<F>'" "\<Union>\<F>' = \<Union>\<F>"
  4691     by (clarsimp simp add: countable_subset_image)
  4692   then show ?thesis ..
  4693 qed
  4694 
  4695 lemma countable_disjoint_open_subsets:
  4696   fixes \<F> :: "'a::euclidean_space set set"
  4697   assumes "\<And>S. S \<in> \<F> \<Longrightarrow> open S" and pw: "pairwise disjnt \<F>"
  4698     shows "countable \<F>"
  4699 proof -
  4700   obtain \<F>' where "\<F>' \<subseteq> \<F>" "countable \<F>'" "\<Union>\<F>' = \<Union>\<F>"
  4701     by (meson assms Lindelof)
  4702   with pw have "\<F> \<subseteq> insert {} \<F>'"
  4703     by (fastforce simp add: pairwise_def disjnt_iff)
  4704   then show ?thesis
  4705     by (simp add: \<open>countable \<F>'\<close> countable_subset)
  4706 qed
  4707 
  4708 lemma closedin_compact:
  4709    "\<lbrakk>compact S; closedin (subtopology euclidean S) T\<rbrakk> \<Longrightarrow> compact T"
  4710 by (metis closedin_closed compact_Int_closed)
  4711 
  4712 lemma closedin_compact_eq:
  4713   fixes S :: "'a::t2_space set"
  4714   shows
  4715    "compact S
  4716          \<Longrightarrow> (closedin (subtopology euclidean S) T \<longleftrightarrow>
  4717               compact T \<and> T \<subseteq> S)"
  4718 by (metis closedin_imp_subset closedin_compact closed_subset compact_imp_closed)
  4719 
  4720 lemma continuous_imp_closed_map:
  4721   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  4722   assumes "closedin (subtopology euclidean S) U"
  4723           "continuous_on S f" "f ` S = T" "compact S"
  4724     shows "closedin (subtopology euclidean T) (f ` U)"
  4725   by (metis assms closedin_compact_eq compact_continuous_image continuous_on_subset subset_image_iff)
  4726 
  4727 lemma continuous_imp_quotient_map:
  4728   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  4729   assumes "continuous_on S f" "f ` S = T" "compact S" "U \<subseteq> T"
  4730     shows "openin (subtopology euclidean S) (S \<inter> f -` U) \<longleftrightarrow>
  4731            openin (subtopology euclidean T) U"
  4732   by (metis (no_types, lifting) assms closed_map_imp_quotient_map continuous_imp_closed_map)
  4733 
  4734 
  4735 lemma open_map_restrict:
  4736   assumes opeU: "openin (subtopology euclidean (S \<inter> f -` T')) U"
  4737     and oo: "\<And>U. openin (subtopology euclidean S) U \<Longrightarrow> openin (subtopology euclidean T) (f ` U)"
  4738     and "T' \<subseteq> T"
  4739   shows "openin (subtopology euclidean T') (f ` U)"
  4740 proof -
  4741   obtain V where "open V" "U = S \<inter> f -` T' \<inter> V"
  4742     using opeU by (auto simp: openin_open)
  4743   with oo [of "S \<inter> V"] \<open>T' \<subseteq> T\<close> show ?thesis
  4744     by (fastforce simp add: openin_open)
  4745 qed
  4746 
  4747 lemma closed_map_restrict:
  4748   assumes cloU: "closedin (subtopology euclidean (S \<inter> f -` T')) U"
  4749     and cc: "\<And>U. closedin (subtopology euclidean S) U \<Longrightarrow> closedin (subtopology euclidean T) (f ` U)"
  4750     and "T' \<subseteq> T"
  4751   shows "closedin (subtopology euclidean T') (f ` U)"
  4752 proof -
  4753   obtain V where "closed V" "U = S \<inter> f -` T' \<inter> V"
  4754     using cloU by (auto simp: closedin_closed)
  4755   with cc [of "S \<inter> V"] \<open>T' \<subseteq> T\<close> show ?thesis
  4756     by (fastforce simp add: closedin_closed)
  4757 qed
  4758 
  4759 lemma connected_monotone_quotient_preimage:
  4760   assumes "connected T"
  4761       and contf: "continuous_on S f" and fim: "f ` S = T"
  4762       and opT: "\<And>U. U \<subseteq> T
  4763                  \<Longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` U) \<longleftrightarrow>
  4764                      openin (subtopology euclidean T) U"
  4765       and connT: "\<And>y. y \<in> T \<Longrightarrow> connected (S \<inter> f -` {y})"
  4766     shows "connected S"
  4767 proof (rule connectedI)
  4768   fix U V
  4769   assume "open U" and "open V" and "U \<inter> S \<noteq> {}" and "V \<inter> S \<noteq> {}"
  4770     and "U \<inter> V \<inter> S = {}" and "S \<subseteq> U \<union> V"
  4771   moreover
  4772   have disjoint: "f ` (S \<inter> U) \<inter> f ` (S \<inter> V) = {}"
  4773   proof -
  4774     have False if "y \<in> f ` (S \<inter> U) \<inter> f ` (S \<inter> V)" for y
  4775     proof -
  4776       have "y \<in> T"
  4777         using fim that by blast
  4778       show ?thesis
  4779         using connectedD [OF connT [OF \<open>y \<in> T\<close>] \<open>open U\<close> \<open>open V\<close>]
  4780               \<open>S \<subseteq> U \<union> V\<close> \<open>U \<inter> V \<inter> S = {}\<close> that by fastforce
  4781     qed
  4782     then show ?thesis by blast
  4783   qed
  4784   ultimately have UU: "(S \<inter> f -` f ` (S \<inter> U)) = S \<inter> U" and VV: "(S \<inter> f -` f ` (S \<inter> V)) = S \<inter> V"
  4785     by auto
  4786   have opeU: "openin (subtopology euclidean T) (f ` (S \<inter> U))"
  4787     by (metis UU \<open>open U\<close> fim image_Int_subset le_inf_iff opT openin_open_Int)
  4788   have opeV: "openin (subtopology euclidean T) (f ` (S \<inter> V))"
  4789     by (metis opT fim VV \<open>open V\<close> openin_open_Int image_Int_subset inf.bounded_iff)
  4790   have "T \<subseteq> f ` (S \<inter> U) \<union> f ` (S \<inter> V)"
  4791     using \<open>S \<subseteq> U \<union> V\<close> fim by auto
  4792   then show False
  4793     using \<open>connected T\<close> disjoint opeU opeV \<open>U \<inter> S \<noteq> {}\<close> \<open>V \<inter> S \<noteq> {}\<close>
  4794     by (auto simp: connected_openin)
  4795 qed
  4796 
  4797 lemma connected_open_monotone_preimage:
  4798   assumes contf: "continuous_on S f" and fim: "f ` S = T"
  4799     and ST: "\<And>C. openin (subtopology euclidean S) C \<Longrightarrow> openin (subtopology euclidean T) (f ` C)"
  4800     and connT: "\<And>y. y \<in> T \<Longrightarrow> connected (S \<inter> f -` {y})"
  4801     and "connected C" "C \<subseteq> T"
  4802   shows "connected (S \<inter> f -` C)"
  4803 proof -
  4804   have contf': "continuous_on (S \<inter> f -` C) f"
  4805     by (meson contf continuous_on_subset inf_le1)
  4806   have eqC: "f ` (S \<inter> f -` C) = C"
  4807     using \<open>C \<subseteq> T\<close> fim by blast
  4808   show ?thesis
  4809   proof (rule connected_monotone_quotient_preimage [OF \<open>connected C\<close> contf' eqC])
  4810     show "connected (S \<inter> f -` C \<inter> f -` {y})" if "y \<in> C" for y
  4811     proof -
  4812       have "S \<inter> f -` C \<inter> f -` {y} = S \<inter> f -` {y}"
  4813         using that by blast
  4814       moreover have "connected (S \<inter> f -` {y})"
  4815         using \<open>C \<subseteq> T\<close> connT that by blast
  4816       ultimately show ?thesis
  4817         by metis
  4818     qed
  4819     have "\<And>U. openin (subtopology euclidean (S \<inter> f -` C)) U
  4820                \<Longrightarrow> openin (subtopology euclidean C) (f ` U)"
  4821       using open_map_restrict [OF _ ST \<open>C \<subseteq> T\<close>] by metis
  4822     then show "\<And>D. D \<subseteq> C
  4823           \<Longrightarrow> openin (subtopology euclidean (S \<inter> f -` C)) (S \<inter> f -` C \<inter> f -` D) =
  4824               openin (subtopology euclidean C) D"
  4825       using open_map_imp_quotient_map [of "(S \<inter> f -` C)" f] contf' by (simp add: eqC)
  4826   qed
  4827 qed
  4828 
  4829 
  4830 lemma connected_closed_monotone_preimage:
  4831   assumes contf: "continuous_on S f" and fim: "f ` S = T"
  4832     and ST: "\<And>C. closedin (subtopology euclidean S) C \<Longrightarrow> closedin (subtopology euclidean T) (f ` C)"
  4833     and connT: "\<And>y. y \<in> T \<Longrightarrow> connected (S \<inter> f -` {y})"
  4834     and "connected C" "C \<subseteq> T"
  4835   shows "connected (S \<inter> f -` C)"
  4836 proof -
  4837   have contf': "continuous_on (S \<inter> f -` C) f"
  4838     by (meson contf continuous_on_subset inf_le1)
  4839   have eqC: "f ` (S \<inter> f -` C) = C"
  4840     using \<open>C \<subseteq> T\<close> fim by blast
  4841   show ?thesis
  4842   proof (rule connected_monotone_quotient_preimage [OF \<open>connected C\<close> contf' eqC])
  4843     show "connected (S \<inter> f -` C \<inter> f -` {y})" if "y \<in> C" for y
  4844     proof -
  4845       have "S \<inter> f -` C \<inter> f -` {y} = S \<inter> f -` {y}"
  4846         using that by blast
  4847       moreover have "connected (S \<inter> f -` {y})"
  4848         using \<open>C \<subseteq> T\<close> connT that by blast
  4849       ultimately show ?thesis
  4850         by metis
  4851     qed
  4852     have "\<And>U. closedin (subtopology euclidean (S \<inter> f -` C)) U
  4853                \<Longrightarrow> closedin (subtopology euclidean C) (f ` U)"
  4854       using closed_map_restrict [OF _ ST \<open>C \<subseteq> T\<close>] by metis
  4855     then show "\<And>D. D \<subseteq> C
  4856           \<Longrightarrow> openin (subtopology euclidean (S \<inter> f -` C)) (S \<inter> f -` C \<inter> f -` D) =
  4857               openin (subtopology euclidean C) D"
  4858       using closed_map_imp_quotient_map [of "(S \<inter> f -` C)" f] contf' by (simp add: eqC)
  4859   qed
  4860 qed
  4861 
  4862 
  4863 
  4864 subsection\<open>A couple of lemmas about components (see Newman IV, 3.3 and 3.4).\<close>
  4865 
  4866 
  4867 lemma connected_Un_clopen_in_complement:
  4868   fixes S U :: "'a::metric_space set"
  4869   assumes "connected S" "connected U" "S \<subseteq> U" 
  4870       and opeT: "openin (subtopology euclidean (U - S)) T" 
  4871       and cloT: "closedin (subtopology euclidean (U - S)) T"
  4872     shows "connected (S \<union> T)"
  4873 proof -
  4874   have *: "\<lbrakk>\<And>x y. P x y \<longleftrightarrow> P y x; \<And>x y. P x y \<Longrightarrow> S \<subseteq> x \<or> S \<subseteq> y;
  4875             \<And>x y. \<lbrakk>P x y; S \<subseteq> x\<rbrakk> \<Longrightarrow> False\<rbrakk> \<Longrightarrow> ~(\<exists>x y. (P x y))" for P
  4876     by metis
  4877   show ?thesis
  4878     unfolding connected_closedin_eq
  4879   proof (rule *)
  4880     fix H1 H2
  4881     assume H: "closedin (subtopology euclidean (S \<union> T)) H1 \<and> 
  4882                closedin (subtopology euclidean (S \<union> T)) H2 \<and>
  4883                H1 \<union> H2 = S \<union> T \<and> H1 \<inter> H2 = {} \<and> H1 \<noteq> {} \<and> H2 \<noteq> {}"
  4884     then have clo: "closedin (subtopology euclidean S) (S \<inter> H1)"
  4885                    "closedin (subtopology euclidean S) (S \<inter> H2)"
  4886       by (metis Un_upper1 closedin_closed_subset inf_commute)+
  4887     have Seq: "S \<inter> (H1 \<union> H2) = S"
  4888       by (simp add: H)
  4889     have "S \<inter> ((S \<union> T) \<inter> H1) \<union> S \<inter> ((S \<union> T) \<inter> H2) = S"
  4890       using Seq by auto
  4891     moreover have "H1 \<inter> (S \<inter> ((S \<union> T) \<inter> H2)) = {}"
  4892       using H by blast
  4893     ultimately have "S \<inter> H1 = {} \<or> S \<inter> H2 = {}"
  4894       by (metis (no_types) H Int_assoc \<open>S \<inter> (H1 \<union> H2) = S\<close> \<open>connected S\<close>
  4895           clo Seq connected_closedin inf_bot_right inf_le1)
  4896     then show "S \<subseteq> H1 \<or> S \<subseteq> H2"
  4897       using H \<open>connected S\<close> unfolding connected_closedin by blast
  4898   next
  4899     fix H1 H2
  4900     assume H: "closedin (subtopology euclidean (S \<union> T)) H1 \<and>
  4901                closedin (subtopology euclidean (S \<union> T)) H2 \<and>
  4902                H1 \<union> H2 = S \<union> T \<and> H1 \<inter> H2 = {} \<and> H1 \<noteq> {} \<and> H2 \<noteq> {}" 
  4903        and "S \<subseteq> H1"
  4904     then have H2T: "H2 \<subseteq> T"
  4905       by auto
  4906     have "T \<subseteq> U"
  4907       using Diff_iff opeT openin_imp_subset by auto
  4908     with \<open>S \<subseteq> U\<close> have Ueq: "U = (U - S) \<union> (S \<union> T)" 
  4909       by auto
  4910     have "openin (subtopology euclidean ((U - S) \<union> (S \<union> T))) H2"
  4911     proof (rule openin_subtopology_Un)
  4912       show "openin (subtopology euclidean (S \<union> T)) H2"
  4913         using \<open>H2 \<subseteq> T\<close> apply (auto simp: openin_closedin_eq)
  4914         by (metis Diff_Diff_Int Diff_disjoint Diff_partition Diff_subset H Int_absorb1 Un_Diff)
  4915       then show "openin (subtopology euclidean (U - S)) H2"
  4916         by (meson H2T Un_upper2 opeT openin_subset_trans openin_trans)
  4917     qed
  4918     moreover have "closedin (subtopology euclidean ((U - S) \<union> (S \<union> T))) H2"
  4919     proof (rule closedin_subtopology_Un)
  4920       show "closedin (subtopology euclidean (U - S)) H2"
  4921         using H H2T cloT closedin_subset_trans 
  4922         by (blast intro: closedin_subtopology_Un closedin_trans)
  4923     qed (simp add: H)
  4924     ultimately
  4925     have H2: "H2 = {} \<or> H2 = U"
  4926       using Ueq \<open>connected U\<close> unfolding connected_clopen by metis   
  4927     then have "H2 \<subseteq> S"
  4928       by (metis Diff_partition H Un_Diff_cancel Un_subset_iff \<open>H2 \<subseteq> T\<close> assms(3) inf.orderE opeT openin_imp_subset)
  4929     moreover have "T \<subseteq> H2 - S"
  4930       by (metis (no_types) H2 H opeT openin_closedin_eq topspace_euclidean_subtopology)
  4931     ultimately show False
  4932       using H \<open>S \<subseteq> H1\<close> by blast
  4933   qed blast
  4934 qed
  4935 
  4936 
  4937 proposition component_diff_connected:
  4938   fixes S :: "'a::metric_space set"
  4939   assumes "connected S" "connected U" "S \<subseteq> U" and C: "C \<in> components (U - S)"
  4940   shows "connected(U - C)"
  4941   using \<open>connected S\<close> unfolding connected_closedin_eq not_ex de_Morgan_conj
  4942 proof clarify
  4943   fix H3 H4 
  4944   assume clo3: "closedin (subtopology euclidean (U - C)) H3" 
  4945     and clo4: "closedin (subtopology euclidean (U - C)) H4" 
  4946     and "H3 \<union> H4 = U - C" and "H3 \<inter> H4 = {}" and "H3 \<noteq> {}" and "H4 \<noteq> {}"
  4947     and * [rule_format]:
  4948     "\<forall>H1 H2. \<not> closedin (subtopology euclidean S) H1 \<or>
  4949                       \<not> closedin (subtopology euclidean S) H2 \<or>
  4950                       H1 \<union> H2 \<noteq> S \<or> H1 \<inter> H2 \<noteq> {} \<or> \<not> H1 \<noteq> {} \<or> \<not> H2 \<noteq> {}"
  4951   then have "H3 \<subseteq> U-C" and ope3: "openin (subtopology euclidean (U - C)) (U - C - H3)"
  4952     and "H4 \<subseteq> U-C" and ope4: "openin (subtopology euclidean (U - C)) (U - C - H4)"
  4953     by (auto simp: closedin_def)
  4954   have "C \<noteq> {}" "C \<subseteq> U-S" "connected C"
  4955     using C in_components_nonempty in_components_subset in_components_maximal by blast+
  4956   have cCH3: "connected (C \<union> H3)"
  4957   proof (rule connected_Un_clopen_in_complement [OF \<open>connected C\<close> \<open>connected U\<close> _ _ clo3])
  4958     show "openin (subtopology euclidean (U - C)) H3"
  4959       apply (simp add: openin_closedin_eq \<open>H3 \<subseteq> U - C\<close>)
  4960       apply (simp add: closedin_subtopology)
  4961       by (metis Diff_cancel Diff_triv Un_Diff clo4 \<open>H3 \<inter> H4 = {}\<close> \<open>H3 \<union> H4 = U - C\<close> closedin_closed inf_commute sup_bot.left_neutral)
  4962   qed (use clo3 \<open>C \<subseteq> U - S\<close> in auto)
  4963   have cCH4: "connected (C \<union> H4)"
  4964   proof (rule connected_Un_clopen_in_complement [OF \<open>connected C\<close> \<open>connected U\<close> _ _ clo4])
  4965     show "openin (subtopology euclidean (U - C)) H4"
  4966       apply (simp add: openin_closedin_eq \<open>H4 \<subseteq> U - C\<close>)
  4967       apply (simp add: closedin_subtopology)
  4968       by (metis Diff_cancel Int_commute Un_Diff Un_Diff_Int \<open>H3 \<inter> H4 = {}\<close> \<open>H3 \<union> H4 = U - C\<close> clo3 closedin_closed)
  4969   qed (use clo4 \<open>C \<subseteq> U - S\<close> in auto)
  4970   have "closedin (subtopology euclidean S) (S \<inter> H3)" "closedin (subtopology euclidean S) (S \<inter> H4)"
  4971     using clo3 clo4 \<open>S \<subseteq> U\<close> \<open>C \<subseteq> U - S\<close> by (auto simp: closedin_closed)
  4972   moreover have "S \<inter> H3 \<noteq> {}"      
  4973     using components_maximal [OF C cCH3] \<open>C \<noteq> {}\<close> \<open>C \<subseteq> U - S\<close> \<open>H3 \<noteq> {}\<close> \<open>H3 \<subseteq> U - C\<close> by auto
  4974   moreover have "S \<inter> H4 \<noteq> {}"
  4975     using components_maximal [OF C cCH4] \<open>C \<noteq> {}\<close> \<open>C \<subseteq> U - S\<close> \<open>H4 \<noteq> {}\<close> \<open>H4 \<subseteq> U - C\<close> by auto
  4976   ultimately show False
  4977     using * [of "S \<inter> H3" "S \<inter> H4"] \<open>H3 \<inter> H4 = {}\<close> \<open>C \<subseteq> U - S\<close> \<open>H3 \<union> H4 = U - C\<close> \<open>S \<subseteq> U\<close> 
  4978     by auto
  4979 qed
  4980 
  4981 subsection\<open> Finite intersection property\<close>
  4982 
  4983 text\<open>Also developed in HOL's toplogical spaces theory, but the Heine-Borel type class isn't available there.\<close>
  4984 
  4985 lemma closed_imp_fip:
  4986   fixes S :: "'a::heine_borel set"
  4987   assumes "closed S"
  4988       and T: "T \<in> \<F>" "bounded T"
  4989       and clof: "\<And>T. T \<in> \<F> \<Longrightarrow> closed T"
  4990       and none: "\<And>\<F>'. \<lbrakk>finite \<F>'; \<F>' \<subseteq> \<F>\<rbrakk> \<Longrightarrow> S \<inter> \<Inter>\<F>' \<noteq> {}"
  4991     shows "S \<inter> \<Inter>\<F> \<noteq> {}"
  4992 proof -
  4993   have "compact (S \<inter> T)"
  4994     using \<open>closed S\<close> clof compact_eq_bounded_closed T by blast
  4995   then have "(S \<inter> T) \<inter> \<Inter>\<F> \<noteq> {}"
  4996     apply (rule compact_imp_fip)
  4997      apply (simp add: clof)
  4998     by (metis Int_assoc complete_lattice_class.Inf_insert finite_insert insert_subset none \<open>T \<in> \<F>\<close>)
  4999   then show ?thesis by blast
  5000 qed
  5001 
  5002 lemma closed_imp_fip_compact:
  5003   fixes S :: "'a::heine_borel set"
  5004   shows
  5005    "\<lbrakk>closed S; \<And>T. T \<in> \<F> \<Longrightarrow> compact T;
  5006      \<And>\<F>'. \<lbrakk>finite \<F>'; \<F>' \<subseteq> \<F>\<rbrakk> \<Longrightarrow> S \<inter> \<Inter>\<F>' \<noteq> {}\<rbrakk>
  5007         \<Longrightarrow> S \<inter> \<Inter>\<F> \<noteq> {}"
  5008 by (metis Inf_greatest closed_imp_fip compact_eq_bounded_closed empty_subsetI finite.emptyI inf.orderE)
  5009 
  5010 lemma closed_fip_heine_borel:
  5011   fixes \<F> :: "'a::heine_borel set set"
  5012   assumes "closed S" "T \<in> \<F>" "bounded T"
  5013       and "\<And>T. T \<in> \<F> \<Longrightarrow> closed T"
  5014       and "\<And>\<F>'. \<lbrakk>finite \<F>'; \<F>' \<subseteq> \<F>\<rbrakk> \<Longrightarrow> \<Inter>\<F>' \<noteq> {}"
  5015     shows "\<Inter>\<F> \<noteq> {}"
  5016 proof -
  5017   have "UNIV \<inter> \<Inter>\<F> \<noteq> {}"
  5018     using assms closed_imp_fip [OF closed_UNIV] by auto
  5019   then show ?thesis by simp
  5020 qed
  5021 
  5022 lemma compact_fip_heine_borel:
  5023   fixes \<F> :: "'a::heine_borel set set"
  5024   assumes clof: "\<And>T. T \<in> \<F> \<Longrightarrow> compact T"
  5025       and none: "\<And>\<F>'. \<lbrakk>finite \<F>'; \<F>' \<subseteq> \<F>\<rbrakk> \<Longrightarrow> \<Inter>\<F>' \<noteq> {}"
  5026     shows "\<Inter>\<F> \<noteq> {}"
  5027 by (metis InterI all_not_in_conv clof closed_fip_heine_borel compact_eq_bounded_closed none)
  5028 
  5029 lemma compact_sequence_with_limit:
  5030   fixes f :: "nat \<Rightarrow> 'a::heine_borel"
  5031   shows "(f \<longlongrightarrow> l) sequentially \<Longrightarrow> compact (insert l (range f))"
  5032 apply (simp add: compact_eq_bounded_closed, auto)
  5033 apply (simp add: convergent_imp_bounded)
  5034 by (simp add: closed_limpt islimpt_insert sequence_unique_limpt)
  5035 
  5036 
  5037 subsection\<open>Componentwise limits and continuity\<close>
  5038 
  5039 text\<open>But is the premise really necessary? Need to generalise @{thm euclidean_dist_l2}\<close>
  5040 lemma Euclidean_dist_upper: "i \<in> Basis \<Longrightarrow> dist (x \<bullet> i) (y \<bullet> i) \<le> dist x y"
  5041   by (metis (no_types) member_le_L2_set euclidean_dist_l2 finite_Basis)
  5042 
  5043 text\<open>But is the premise @{term \<open>i \<in> Basis\<close>} really necessary?\<close>
  5044 lemma open_preimage_inner:
  5045   assumes "open S" "i \<in> Basis"
  5046     shows "open {x. x \<bullet> i \<in> S}"
  5047 proof (rule openI, simp)
  5048   fix x
  5049   assume x: "x \<bullet> i \<in> S"
  5050   with assms obtain e where "0 < e" and e: "ball (x \<bullet> i) e \<subseteq> S"
  5051     by (auto simp: open_contains_ball_eq)
  5052   have "\<exists>e>0. ball (y \<bullet> i) e \<subseteq> S" if dxy: "dist x y < e / 2" for y
  5053   proof (intro exI conjI)
  5054     have "dist (x \<bullet> i) (y \<bullet> i) < e / 2"
  5055       by (meson \<open>i \<in> Basis\<close> dual_order.trans Euclidean_dist_upper not_le that)
  5056     then have "dist (x \<bullet> i) z < e" if "dist (y \<bullet> i) z < e / 2" for z
  5057       by (metis dist_commute dist_triangle_half_l that)
  5058     then have "ball (y \<bullet> i) (e / 2) \<subseteq> ball (x \<bullet> i) e"
  5059       using mem_ball by blast
  5060       with e show "ball (y \<bullet> i) (e / 2) \<subseteq> S"
  5061         by (metis order_trans)
  5062   qed (simp add: \<open>0 < e\<close>)
  5063   then show "\<exists>e>0. ball x e \<subseteq> {s. s \<bullet> i \<in> S}"
  5064     by (metis (no_types, lifting) \<open>0 < e\<close> \<open>open S\<close> half_gt_zero_iff mem_Collect_eq mem_ball open_contains_ball_eq subsetI)
  5065 qed
  5066 
  5067 proposition tendsto_componentwise_iff:
  5068   fixes f :: "_ \<Rightarrow> 'b::euclidean_space"
  5069   shows "(f \<longlongrightarrow> l) F \<longleftrightarrow> (\<forall>i \<in> Basis. ((\<lambda>x. (f x \<bullet> i)) \<longlongrightarrow> (l \<bullet> i)) F)"
  5070          (is "?lhs = ?rhs")
  5071 proof
  5072   assume ?lhs
  5073   then show ?rhs
  5074     unfolding tendsto_def
  5075     apply clarify
  5076     apply (drule_tac x="{s. s \<bullet> i \<in> S}" in spec)
  5077     apply (auto simp: open_preimage_inner)
  5078     done
  5079 next
  5080   assume R: ?rhs
  5081   then have "\<And>e. e > 0 \<Longrightarrow> \<forall>i\<in>Basis. \<forall>\<^sub>F x in F. dist (f x \<bullet> i) (l \<bullet> i) < e"
  5082     unfolding tendsto_iff by blast
  5083   then have R': "\<And>e. e > 0 \<Longrightarrow> \<forall>\<^sub>F x in F. \<forall>i\<in>Basis. dist (f x \<bullet> i) (l \<bullet> i) < e"
  5084       by (simp add: eventually_ball_finite_distrib [symmetric])
  5085   show ?lhs
  5086   unfolding tendsto_iff
  5087   proof clarify
  5088     fix e::real
  5089     assume "0 < e"
  5090     have *: "L2_set (\<lambda>i. dist (f x \<bullet> i) (l \<bullet> i)) Basis < e"
  5091              if "\<forall>i\<in>Basis. dist (f x \<bullet> i) (l \<bullet> i) < e / real DIM('b)" for x
  5092     proof -
  5093       have "L2_set (\<lambda>i. dist (f x \<bullet> i) (l \<bullet> i)) Basis \<le> sum (\<lambda>i. dist (f x \<bullet> i) (l \<bullet> i)) Basis"
  5094         by (simp add: L2_set_le_sum)
  5095       also have "... < DIM('b) * (e / real DIM('b))"
  5096         apply (rule sum_bounded_above_strict)
  5097         using that by auto
  5098       also have "... = e"
  5099         by (simp add: field_simps)
  5100       finally show "L2_set (\<lambda>i. dist (f x \<bullet> i) (l \<bullet> i)) Basis < e" .
  5101     qed
  5102     have "\<forall>\<^sub>F x in F. \<forall>i\<in>Basis. dist (f x \<bullet> i) (l \<bullet> i) < e / DIM('b)"
  5103       apply (rule R')
  5104       using \<open>0 < e\<close> by simp
  5105     then show "\<forall>\<^sub>F x in F. dist (f x) l < e"
  5106       apply (rule eventually_mono)
  5107       apply (subst euclidean_dist_l2)
  5108       using * by blast
  5109   qed
  5110 qed
  5111 
  5112 
  5113 corollary continuous_componentwise:
  5114    "continuous F f \<longleftrightarrow> (\<forall>i \<in> Basis. continuous F (\<lambda>x. (f x \<bullet> i)))"
  5115 by (simp add: continuous_def tendsto_componentwise_iff [symmetric])
  5116 
  5117 corollary continuous_on_componentwise:
  5118   fixes S :: "'a :: t2_space set"
  5119   shows "continuous_on S f \<longleftrightarrow> (\<forall>i \<in> Basis. continuous_on S (\<lambda>x. (f x \<bullet> i)))"
  5120   apply (simp add: continuous_on_eq_continuous_within)
  5121   using continuous_componentwise by blast
  5122 
  5123 lemma linear_componentwise_iff:
  5124      "(linear f') \<longleftrightarrow> (\<forall>i\<in>Basis. linear (\<lambda>x. f' x \<bullet> i))"
  5125   apply (auto simp: linear_iff inner_left_distrib)
  5126    apply (metis inner_left_distrib euclidean_eq_iff)
  5127   by (metis euclidean_eqI inner_scaleR_left)
  5128 
  5129 lemma bounded_linear_componentwise_iff:
  5130      "(bounded_linear f') \<longleftrightarrow> (\<forall>i\<in>Basis. bounded_linear (\<lambda>x. f' x \<bullet> i))"
  5131      (is "?lhs = ?rhs")
  5132 proof
  5133   assume ?lhs then show ?rhs
  5134     by (simp add: bounded_linear_inner_left_comp)
  5135 next
  5136   assume ?rhs
  5137   then have "(\<forall>i\<in>Basis. \<exists>K. \<forall>x. \<bar>f' x \<bullet> i\<bar> \<le> norm x * K)" "linear f'"
  5138     by (auto simp: bounded_linear_def bounded_linear_axioms_def linear_componentwise_iff [symmetric] ball_conj_distrib)
  5139   then obtain F where F: "\<And>i x. i \<in> Basis \<Longrightarrow> \<bar>f' x \<bullet> i\<bar> \<le> norm x * F i"
  5140     by metis
  5141   have "norm (f' x) \<le> norm x * sum F Basis" for x
  5142   proof -
  5143     have "norm (f' x) \<le> (\<Sum>i\<in>Basis. \<bar>f' x \<bullet> i\<bar>)"
  5144       by (rule norm_le_l1)
  5145     also have "... \<le> (\<Sum>i\<in>Basis. norm x * F i)"
  5146       by (metis F sum_mono)
  5147     also have "... = norm x * sum F Basis"
  5148       by (simp add: sum_distrib_left)
  5149     finally show ?thesis .
  5150   qed
  5151   then show ?lhs
  5152     by (force simp: bounded_linear_def bounded_linear_axioms_def \<open>linear f'\<close>)
  5153 qed
  5154 
  5155 subsection\<open>Pasting functions together\<close>
  5156 
  5157 subsubsection\<open>on open sets\<close>
  5158 
  5159 lemma pasting_lemma:
  5160   fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::topological_space"
  5161   assumes clo: "\<And>i. i \<in> I \<Longrightarrow> openin (subtopology euclidean S) (T i)"
  5162       and cont: "\<And>i. i \<in> I \<Longrightarrow> continuous_on (T i) (f i)"
  5163       and f: "\<And>i j x. \<lbrakk>i \<in> I; j \<in> I; x \<in> S \<inter> T i \<inter> T j\<rbrakk> \<Longrightarrow> f i x = f j x"
  5164       and g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>j. j \<in> I \<and> x \<in> T j \<and> g x = f j x"
  5165     shows "continuous_on S g"
  5166 proof (clarsimp simp: continuous_openin_preimage_eq)
  5167   fix U :: "'b set"
  5168   assume "open U"
  5169   have S: "\<And>i. i \<in> I \<Longrightarrow> (T i) \<subseteq> S"
  5170     using clo openin_imp_subset by blast
  5171   have *: "(S \<inter> g -` U) = (\<Union>i \<in> I. T i \<inter> f i -` U)"
  5172     using S f g by fastforce
  5173   show "openin (subtopology euclidean S) (S \<inter> g -` U)"
  5174     apply (subst *)
  5175     apply (rule openin_Union, clarify)
  5176     using \<open>open U\<close> clo cont continuous_openin_preimage_gen openin_trans by blast
  5177 qed
  5178 
  5179 lemma pasting_lemma_exists:
  5180   fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::topological_space"
  5181   assumes S: "S \<subseteq> (\<Union>i \<in> I. T i)"
  5182       and clo: "\<And>i. i \<in> I \<Longrightarrow> openin (subtopology euclidean S) (T i)"
  5183       and cont: "\<And>i. i \<in> I \<Longrightarrow> continuous_on (T i) (f i)"
  5184       and f: "\<And>i j x. \<lbrakk>i \<in> I; j \<in> I; x \<in> S \<inter> T i \<inter> T j\<rbrakk> \<Longrightarrow> f i x = f j x"
  5185     obtains g where "continuous_on S g" "\<And>x i. \<lbrakk>i \<in> I; x \<in> S \<inter> T i\<rbrakk> \<Longrightarrow> g x = f i x"
  5186 proof
  5187   show "continuous_on S (\<lambda>x. f (SOME i. i \<in> I \<and> x \<in> T i) x)"
  5188     apply (rule pasting_lemma [OF clo cont])
  5189      apply (blast intro: f)+
  5190     apply (metis (mono_tags, lifting) S UN_iff subsetCE someI)
  5191     done
  5192 next
  5193   fix x i
  5194   assume "i \<in> I" "x \<in> S \<inter> T i"
  5195   then show "f (SOME i. i \<in> I \<and> x \<in> T i) x = f i x"
  5196     by (metis (no_types, lifting) IntD2 IntI f someI_ex)
  5197 qed
  5198 
  5199 subsubsection\<open>Likewise on closed sets, with a finiteness assumption\<close>
  5200 
  5201 lemma pasting_lemma_closed:
  5202   fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::topological_space"
  5203   assumes "finite I"
  5204       and clo: "\<And>i. i \<in> I \<Longrightarrow> closedin (subtopology euclidean S) (T i)"
  5205       and cont: "\<And>i. i \<in> I \<Longrightarrow> continuous_on (T i) (f i)"
  5206       and f: "\<And>i j x. \<lbrakk>i \<in> I; j \<in> I; x \<in> S \<inter> T i \<inter> T j\<rbrakk> \<Longrightarrow> f i x = f j x"
  5207       and g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>j. j \<in> I \<and> x \<in> T j \<and> g x = f j x"
  5208     shows "continuous_on S g"
  5209 proof (clarsimp simp: continuous_closedin_preimage_eq)
  5210   fix U :: "'b set"
  5211   assume "closed U"
  5212   have S: "\<And>i. i \<in> I \<Longrightarrow> (T i) \<subseteq> S"
  5213     using clo closedin_imp_subset by blast
  5214   have *: "(S \<inter> g -` U) = (\<Union>i \<in> I. T i \<inter> f i -` U)"
  5215     using S f g by fastforce
  5216   show "closedin (subtopology euclidean S) (S \<inter> g -` U)"
  5217     apply (subst *)
  5218     apply (rule closedin_Union)
  5219     using \<open>finite I\<close> apply simp
  5220     apply (blast intro: \<open>closed U\<close> continuous_closedin_preimage cont clo closedin_trans)
  5221     done
  5222 qed
  5223 
  5224 lemma pasting_lemma_exists_closed:
  5225   fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::topological_space"
  5226   assumes "finite I"
  5227       and S: "S \<subseteq> (\<Union>i \<in> I. T i)"
  5228       and clo: "\<And>i. i \<in> I \<Longrightarrow> closedin (subtopology euclidean S) (T i)"
  5229       and cont: "\<And>i. i \<in> I \<Longrightarrow> continuous_on (T i) (f i)"
  5230       and f: "\<And>i j x. \<lbrakk>i \<in> I; j \<in> I; x \<in> S \<inter> T i \<inter> T j\<rbrakk> \<Longrightarrow> f i x = f j x"
  5231     obtains g where "continuous_on S g" "\<And>x i. \<lbrakk>i \<in> I; x \<in> S \<inter> T i\<rbrakk> \<Longrightarrow> g x = f i x"
  5232 proof
  5233   show "continuous_on S (\<lambda>x. f (SOME i. i \<in> I \<and> x \<in> T i) x)"
  5234     apply (rule pasting_lemma_closed [OF \<open>finite I\<close> clo cont])
  5235      apply (blast intro: f)+
  5236     apply (metis (mono_tags, lifting) S UN_iff subsetCE someI)
  5237     done
  5238 next
  5239   fix x i
  5240   assume "i \<in> I" "x \<in> S \<inter> T i"
  5241   then show "f (SOME i. i \<in> I \<and> x \<in> T i) x = f i x"
  5242     by (metis (no_types, lifting) IntD2 IntI f someI_ex)
  5243 qed
  5244 
  5245 lemma tube_lemma:
  5246   assumes "compact K"
  5247   assumes "open W"
  5248   assumes "{x0} \<times> K \<subseteq> W"
  5249   shows "\<exists>X0. x0 \<in> X0 \<and> open X0 \<and> X0 \<times> K \<subseteq> W"
  5250 proof -
  5251   {
  5252     fix y assume "y \<in> K"
  5253     then have "(x0, y) \<in> W" using assms by auto
  5254     with \<open>open W\<close>
  5255     have "\<exists>X0 Y. open X0 \<and> open Y \<and> x0 \<in> X0 \<and> y \<in> Y \<and> X0 \<times> Y \<subseteq> W"
  5256       by (rule open_prod_elim) blast
  5257   }
  5258   then obtain X0 Y where
  5259     *: "\<forall>y \<in> K. open (X0 y) \<and> open (Y y) \<and> x0 \<in> X0 y \<and> y \<in> Y y \<and> X0 y \<times> Y y \<subseteq> W"
  5260     by metis
  5261   from * have "\<forall>t\<in>Y ` K. open t" "K \<subseteq> \<Union>(Y ` K)" by auto
  5262   with \<open>compact K\<close> obtain CC where CC: "CC \<subseteq> Y ` K" "finite CC" "K \<subseteq> \<Union>CC"
  5263     by (meson compactE)
  5264   then obtain c where c: "\<And>C. C \<in> CC \<Longrightarrow> c C \<in> K \<and> C = Y (c C)"
  5265     by (force intro!: choice)
  5266   with * CC show ?thesis
  5267     by (force intro!: exI[where x="\<Inter>C\<in>CC. X0 (c C)"]) (* SLOW *)
  5268 qed
  5269 
  5270 lemma continuous_on_prod_compactE:
  5271   fixes fx::"'a::topological_space \<times> 'b::topological_space \<Rightarrow> 'c::metric_space"
  5272     and e::real
  5273   assumes cont_fx: "continuous_on (U \<times> C) fx"
  5274   assumes "compact C"
  5275   assumes [intro]: "x0 \<in> U"
  5276   notes [continuous_intros] = continuous_on_compose2[OF cont_fx]
  5277   assumes "e > 0"
  5278   obtains X0 where "x0 \<in> X0" "open X0"
  5279     "\<forall>x\<in>X0 \<inter> U. \<forall>t \<in> C. dist (fx (x, t)) (fx (x0, t)) \<le> e"
  5280 proof -
  5281   define psi where "psi = (\<lambda>(x, t). dist (fx (x, t)) (fx (x0, t)))"
  5282   define W0 where "W0 = {(x, t) \<in> U \<times> C. psi (x, t) < e}"
  5283   have W0_eq: "W0 = psi -` {..<e} \<inter> U \<times> C"
  5284     by (auto simp: vimage_def W0_def)
  5285   have "open {..<e}" by simp
  5286   have "continuous_on (U \<times> C) psi"
  5287     by (auto intro!: continuous_intros simp: psi_def split_beta')
  5288   from this[unfolded continuous_on_open_invariant, rule_format, OF \<open>open {..<e}\<close>]
  5289   obtain W where W: "open W" "W \<inter> U \<times> C = W0 \<inter> U \<times> C"
  5290     unfolding W0_eq by blast
  5291   have "{x0} \<times> C \<subseteq> W \<inter> U \<times> C"
  5292     unfolding W
  5293     by (auto simp: W0_def psi_def \<open>0 < e\<close>)
  5294   then have "{x0} \<times> C \<subseteq> W" by blast
  5295   from tube_lemma[OF \<open>compact C\<close> \<open>open W\<close> this]
  5296   obtain X0 where X0: "x0 \<in> X0" "open X0" "X0 \<times> C \<subseteq> W"
  5297     by blast
  5298 
  5299   have "\<forall>x\<in>X0 \<inter> U. \<forall>t \<in> C. dist (fx (x, t)) (fx (x0, t)) \<le> e"
  5300   proof safe
  5301     fix x assume x: "x \<in> X0" "x \<in> U"
  5302     fix t assume t: "t \<in> C"
  5303     have "dist (fx (x, t)) (fx (x0, t)) = psi (x, t)"
  5304       by (auto simp: psi_def)
  5305     also
  5306     {
  5307       have "(x, t) \<in> X0 \<times> C"
  5308         using t x
  5309         by auto
  5310       also note \<open>\<dots> \<subseteq> W\<close>
  5311       finally have "(x, t) \<in> W" .
  5312       with t x have "(x, t) \<in> W \<inter> U \<times> C"
  5313         by blast
  5314       also note \<open>W \<inter> U \<times> C = W0 \<inter> U \<times> C\<close>
  5315       finally  have "psi (x, t) < e"
  5316         by (auto simp: W0_def)
  5317     }
  5318     finally show "dist (fx (x, t)) (fx (x0, t)) \<le> e" by simp
  5319   qed
  5320   from X0(1,2) this show ?thesis ..
  5321 qed
  5322 
  5323 
  5324 subsection\<open>Constancy of a function from a connected set into a finite, disconnected or discrete set\<close>
  5325 
  5326 text\<open>Still missing: versions for a set that is smaller than R, or countable.\<close>
  5327 
  5328 lemma continuous_disconnected_range_constant:
  5329   assumes S: "connected S"
  5330       and conf: "continuous_on S f"
  5331       and fim: "f ` S \<subseteq> t"
  5332       and cct: "\<And>y. y \<in> t \<Longrightarrow> connected_component_set t y = {y}"
  5333     shows "f constant_on S"
  5334 proof (cases "S = {}")
  5335   case True then show ?thesis
  5336     by (simp add: constant_on_def)
  5337 next
  5338   case False
  5339   { fix x assume "x \<in> S"
  5340     then have "f ` S \<subseteq> {f x}"
  5341     by (metis connected_continuous_image conf connected_component_maximal fim image_subset_iff rev_image_eqI S cct)
  5342   }
  5343   with False show ?thesis
  5344     unfolding constant_on_def by blast
  5345 qed
  5346 
  5347 lemma discrete_subset_disconnected:
  5348   fixes S :: "'a::topological_space set"
  5349   fixes t :: "'b::real_normed_vector set"
  5350   assumes conf: "continuous_on S f"
  5351       and no: "\<And>x. x \<in> S \<Longrightarrow> \<exists>e>0. \<forall>y. y \<in> S \<and> f y \<noteq> f x \<longrightarrow> e \<le> norm (f y - f x)"
  5352    shows "f ` S \<subseteq> {y. connected_component_set (f ` S) y = {y}}"
  5353 proof -
  5354   { fix x assume x: "x \<in> S"
  5355     then obtain e where "e>0" and ele: "\<And>y. \<lbrakk>y \<in> S; f y \<noteq> f x\<rbrakk> \<Longrightarrow> e \<le> norm (f y - f x)"
  5356       using conf no [OF x] by auto
  5357     then have e2: "0 \<le> e / 2"
  5358       by simp
  5359     have "f y = f x" if "y \<in> S" and ccs: "f y \<in> connected_component_set (f ` S) (f x)" for y
  5360       apply (rule ccontr)
  5361       using connected_closed [of "connected_component_set (f ` S) (f x)"] \<open>e>0\<close>
  5362       apply (simp add: del: ex_simps)
  5363       apply (drule spec [where x="cball (f x) (e / 2)"])
  5364       apply (drule spec [where x="- ball(f x) e"])
  5365       apply (auto simp: dist_norm open_closed [symmetric] simp del: le_divide_eq_numeral1 dest!: connected_component_in)
  5366         apply (metis diff_self e2 ele norm_minus_commute norm_zero not_less)
  5367        using centre_in_cball connected_component_refl_eq e2 x apply blast
  5368       using ccs
  5369       apply (force simp: cball_def dist_norm norm_minus_commute dest: ele [OF \<open>y \<in> S\<close>])
  5370       done
  5371     moreover have "connected_component_set (f ` S) (f x) \<subseteq> f ` S"
  5372       by (auto simp: connected_component_in)
  5373     ultimately have "connected_component_set (f ` S) (f x) = {f x}"
  5374       by (auto simp: x)
  5375   }
  5376   with assms show ?thesis
  5377     by blast
  5378 qed
  5379 
  5380 lemma finite_implies_discrete:
  5381   fixes S :: "'a::topological_space set"
  5382   assumes "finite (f ` S)"
  5383   shows "(\<forall>x \<in> S. \<exists>e>0. \<forall>y. y \<in> S \<and> f y \<noteq> f x \<longrightarrow> e \<le> norm (f y - f x))"
  5384 proof -
  5385   have "\<exists>e>0. \<forall>y. y \<in> S \<and> f y \<noteq> f x \<longrightarrow> e \<le> norm (f y - f x)" if "x \<in> S" for x
  5386   proof (cases "f ` S - {f x} = {}")
  5387     case True
  5388     with zero_less_numeral show ?thesis
  5389       by (fastforce simp add: Set.image_subset_iff cong: conj_cong)
  5390   next
  5391     case False
  5392     then obtain z where z: "z \<in> S" "f z \<noteq> f x"
  5393       by blast
  5394     have finn: "finite {norm (z - f x) |z. z \<in> f ` S - {f x}}"
  5395       using assms by simp
  5396     then have *: "0 < Inf{norm(z - f x) | z. z \<in> f ` S - {f x}}"
  5397       apply (rule finite_imp_less_Inf)
  5398       using z apply force+
  5399       done
  5400     show ?thesis
  5401       by (force intro!: * cInf_le_finite [OF finn])
  5402   qed
  5403   with assms show ?thesis
  5404     by blast
  5405 qed
  5406 
  5407 text\<open>This proof requires the existence of two separate values of the range type.\<close>
  5408 lemma finite_range_constant_imp_connected:
  5409   assumes "\<And>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.
  5410               \<lbrakk>continuous_on S f; finite(f ` S)\<rbrakk> \<Longrightarrow> f constant_on S"
  5411     shows "connected S"
  5412 proof -
  5413   { fix t u
  5414     assume clt: "closedin (subtopology euclidean S) t"
  5415        and clu: "closedin (subtopology euclidean S) u"
  5416        and tue: "t \<inter> u = {}" and tus: "t \<union> u = S"
  5417     have conif: "continuous_on S (\<lambda>x. if x \<in> t then 0 else 1)"
  5418       apply (subst tus [symmetric])
  5419       apply (rule continuous_on_cases_local)
  5420       using clt clu tue
  5421       apply (auto simp: tus continuous_on_const)
  5422       done
  5423     have fi: "finite ((\<lambda>x. if x \<in> t then 0 else 1) ` S)"
  5424       by (rule finite_subset [of _ "{0,1}"]) auto
  5425     have "t = {} \<or> u = {}"
  5426       using assms [OF conif fi] tus [symmetric]
  5427       by (auto simp: Ball_def constant_on_def) (metis IntI empty_iff one_neq_zero tue)
  5428   }
  5429   then show ?thesis
  5430     by (simp add: connected_closedin_eq)
  5431 qed
  5432 
  5433 lemma continuous_disconnected_range_constant_eq:
  5434       "(connected S \<longleftrightarrow>
  5435            (\<forall>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.
  5436             \<forall>t. continuous_on S f \<and> f ` S \<subseteq> t \<and> (\<forall>y \<in> t. connected_component_set t y = {y})
  5437             \<longrightarrow> f constant_on S))" (is ?thesis1)
  5438   and continuous_discrete_range_constant_eq:
  5439       "(connected S \<longleftrightarrow>
  5440          (\<forall>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.
  5441           continuous_on S f \<and>
  5442           (\<forall>x \<in> S. \<exists>e. 0 < e \<and> (\<forall>y. y \<in> S \<and> (f y \<noteq> f x) \<longrightarrow> e \<le> norm(f y - f x)))
  5443           \<longrightarrow> f constant_on S))" (is ?thesis2)
  5444   and continuous_finite_range_constant_eq:
  5445       "(connected S \<longleftrightarrow>
  5446          (\<forall>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.
  5447           continuous_on S f \<and> finite (f ` S)
  5448           \<longrightarrow> f constant_on S))" (is ?thesis3)
  5449 proof -
  5450   have *: "\<And>s t u v. \<lbrakk>s \<Longrightarrow> t; t \<Longrightarrow> u; u \<Longrightarrow> v; v \<Longrightarrow> s\<rbrakk>
  5451     \<Longrightarrow> (s \<longleftrightarrow> t) \<and> (s \<longleftrightarrow> u) \<and> (s \<longleftrightarrow> v)"
  5452     by blast
  5453   have "?thesis1 \<and> ?thesis2 \<and> ?thesis3"
  5454     apply (rule *)
  5455     using continuous_disconnected_range_constant apply metis
  5456     apply clarify
  5457     apply (frule discrete_subset_disconnected; blast)
  5458     apply (blast dest: finite_implies_discrete)
  5459     apply (blast intro!: finite_range_constant_imp_connected)
  5460     done
  5461   then show ?thesis1 ?thesis2 ?thesis3
  5462     by blast+
  5463 qed
  5464 
  5465 lemma continuous_discrete_range_constant:
  5466   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1"
  5467   assumes S: "connected S"
  5468       and "continuous_on S f"
  5469       and "\<And>x. x \<in> S \<Longrightarrow> \<exists>e>0. \<forall>y. y \<in> S \<and> f y \<noteq> f x \<longrightarrow> e \<le> norm (f y - f x)"
  5470     shows "f constant_on S"
  5471   using continuous_discrete_range_constant_eq [THEN iffD1, OF S] assms by blast
  5472 
  5473 lemma continuous_finite_range_constant:
  5474   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1"
  5475   assumes "connected S"
  5476       and "continuous_on S f"
  5477       and "finite (f ` S)"
  5478     shows "f constant_on S"
  5479   using assms continuous_finite_range_constant_eq  by blast
  5480 
  5481 
  5482 
  5483 subsection \<open>Continuous Extension\<close>
  5484 
  5485 definition clamp :: "'a::euclidean_space \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a" where
  5486   "clamp a b x = (if (\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)
  5487     then (\<Sum>i\<in>Basis. (if x\<bullet>i < a\<bullet>i then a\<bullet>i else if x\<bullet>i \<le> b\<bullet>i then x\<bullet>i else b\<bullet>i) *\<^sub>R i)
  5488     else a)"
  5489 
  5490 lemma clamp_in_interval[simp]:
  5491   assumes "\<And>i. i \<in> Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i"
  5492   shows "clamp a b x \<in> cbox a b"
  5493   unfolding clamp_def
  5494   using box_ne_empty(1)[of a b] assms by (auto simp: cbox_def)
  5495 
  5496 lemma clamp_cancel_cbox[simp]:
  5497   fixes x a b :: "'a::euclidean_space"
  5498   assumes x: "x \<in> cbox a b"
  5499   shows "clamp a b x = x"
  5500   using assms
  5501   by (auto simp: clamp_def mem_box intro!: euclidean_eqI[where 'a='a])
  5502 
  5503 lemma clamp_empty_interval:
  5504   assumes "i \<in> Basis" "a \<bullet> i > b \<bullet> i"
  5505   shows "clamp a b = (\<lambda>_. a)"
  5506   using assms
  5507   by (force simp: clamp_def[abs_def] split: if_splits intro!: ext)
  5508 
  5509 lemma dist_clamps_le_dist_args:
  5510   fixes x :: "'a::euclidean_space"
  5511   shows "dist (clamp a b y) (clamp a b x) \<le> dist y x"
  5512 proof cases
  5513   assume le: "(\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)"
  5514   then have "(\<Sum>i\<in>Basis. (dist (clamp a b y \<bullet> i) (clamp a b x \<bullet> i))\<^sup>2) \<le>
  5515     (\<Sum>i\<in>Basis. (dist (y \<bullet> i) (x \<bullet> i))\<^sup>2)"
  5516     by (auto intro!: sum_mono simp: clamp_def dist_real_def abs_le_square_iff[symmetric])
  5517   then show ?thesis
  5518     by (auto intro: real_sqrt_le_mono
  5519       simp: euclidean_dist_l2[where y=x] euclidean_dist_l2[where y="clamp a b x"] L2_set_def)
  5520 qed (auto simp: clamp_def)
  5521 
  5522 lemma clamp_continuous_at:
  5523   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::metric_space"
  5524     and x :: 'a
  5525   assumes f_cont: "continuous_on (cbox a b) f"
  5526   shows "continuous (at x) (\<lambda>x. f (clamp a b x))"
  5527 proof cases
  5528   assume le: "(\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)"
  5529   show ?thesis
  5530     unfolding continuous_at_eps_delta
  5531   proof safe
  5532     fix x :: 'a
  5533     fix e :: real
  5534     assume "e > 0"
  5535     moreover have "clamp a b x \<in> cbox a b"
  5536       by (simp add: clamp_in_interval le)
  5537     moreover note f_cont[simplified continuous_on_iff]
  5538     ultimately
  5539     obtain d where d: "0 < d"
  5540       "\<And>x'. x' \<in> cbox a b \<Longrightarrow> dist x' (clamp a b x) < d \<Longrightarrow> dist (f x') (f (clamp a b x)) < e"
  5541       by force
  5542     show "\<exists>d>0. \<forall>x'. dist x' x < d \<longrightarrow>
  5543       dist (f (clamp a b x')) (f (clamp a b x)) < e"
  5544       using le
  5545       by (auto intro!: d clamp_in_interval dist_clamps_le_dist_args[THEN le_less_trans])
  5546   qed
  5547 qed (auto simp: clamp_empty_interval)
  5548 
  5549 lemma clamp_continuous_on:
  5550   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::metric_space"
  5551   assumes f_cont: "continuous_on (cbox a b) f"
  5552   shows "continuous_on S (\<lambda>x. f (clamp a b x))"
  5553   using assms
  5554   by (auto intro: continuous_at_imp_continuous_on clamp_continuous_at)
  5555 
  5556 lemma clamp_bounded:
  5557   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::metric_space"
  5558   assumes bounded: "bounded (f ` (cbox a b))"
  5559   shows "bounded (range (\<lambda>x. f (clamp a b x)))"
  5560 proof cases
  5561   assume le: "(\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)"
  5562   from bounded obtain c where f_bound: "\<forall>x\<in>f ` cbox a b. dist undefined x \<le> c"
  5563     by (auto simp: bounded_any_center[where a=undefined])
  5564   then show ?thesis
  5565     by (auto intro!: exI[where x=c] clamp_in_interval[OF le[rule_format]]
  5566         simp: bounded_any_center[where a=undefined])
  5567 qed (auto simp: clamp_empty_interval image_def)
  5568 
  5569 
  5570 definition ext_cont :: "('a::euclidean_space \<Rightarrow> 'b::metric_space) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b"
  5571   where "ext_cont f a b = (\<lambda>x. f (clamp a b x))"
  5572 
  5573 lemma ext_cont_cancel_cbox[simp]:
  5574   fixes x a b :: "'a::euclidean_space"
  5575   assumes x: "x \<in> cbox a b"
  5576   shows "ext_cont f a b x = f x"
  5577   using assms
  5578   unfolding ext_cont_def
  5579   by (auto simp: clamp_def mem_box intro!: euclidean_eqI[where 'a='a] arg_cong[where f=f])
  5580 
  5581 lemma continuous_on_ext_cont[continuous_intros]:
  5582   "continuous_on (cbox a b) f \<Longrightarrow> continuous_on S (ext_cont f a b)"
  5583   by (auto intro!: clamp_continuous_on simp: ext_cont_def)
  5584 
  5585 end