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