src/HOL/Analysis/Connected.thy
 author paulson Tue Oct 10 22:18:58 2017 +0100 (20 months ago) changeset 66835 ecc99a5a1ab8 parent 66827 c94531b5007d child 66884 c2128ab11f61 permissions -rw-r--r--
fixed markup
     1 (*  Author:     L C Paulson, University of Cambridge

     2     Material split off from Topology_Euclidean_Space

     3 *)

     4

     5 section \<open>Connected Components, Homeomorphisms, Baire property, etc.\<close>

     6

     7 theory Connected

     8 imports Topology_Euclidean_Space

     9 begin

    10

    11 subsection \<open>More properties of closed balls, spheres, etc.\<close>

    12

    13 lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"

    14   apply (simp add: interior_def, safe)

    15   apply (force simp: open_contains_cball)

    16   apply (rule_tac x="ball x e" in exI)

    17   apply (simp add: subset_trans [OF ball_subset_cball])

    18   done

    19

    20 lemma islimpt_ball:

    21   fixes x y :: "'a::{real_normed_vector,perfect_space}"

    22   shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e"

    23   (is "?lhs \<longleftrightarrow> ?rhs")

    24 proof

    25   show ?rhs if ?lhs

    26   proof

    27     {

    28       assume "e \<le> 0"

    29       then have *: "ball x e = {}"

    30         using ball_eq_empty[of x e] by auto

    31       have False using \<open>?lhs\<close>

    32         unfolding * using islimpt_EMPTY[of y] by auto

    33     }

    34     then show "e > 0" by (metis not_less)

    35     show "y \<in> cball x e"

    36       using closed_cball[of x e] islimpt_subset[of y "ball x e" "cball x e"]

    37         ball_subset_cball[of x e] \<open>?lhs\<close>

    38       unfolding closed_limpt by auto

    39   qed

    40   show ?lhs if ?rhs

    41   proof -

    42     from that have "e > 0" by auto

    43     {

    44       fix d :: real

    45       assume "d > 0"

    46       have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

    47       proof (cases "d \<le> dist x y")

    48         case True

    49         then show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

    50         proof (cases "x = y")

    51           case True

    52           then have False

    53             using \<open>d \<le> dist x y\<close> \<open>d>0\<close> by auto

    54           then show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

    55             by auto

    56         next

    57           case False

    58           have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) =

    59             norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"

    60             unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[symmetric]

    61             by auto

    62           also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"

    63             using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", symmetric, of "y - x"]

    64             unfolding scaleR_minus_left scaleR_one

    65             by (auto simp: norm_minus_commute)

    66           also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"

    67             unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]

    68             unfolding distrib_right using \<open>x\<noteq>y\<close>  by auto

    69           also have "\<dots> \<le> e - d/2" using \<open>d \<le> dist x y\<close> and \<open>d>0\<close> and \<open>?rhs\<close>

    70             by (auto simp: dist_norm)

    71           finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using \<open>d>0\<close>

    72             by auto

    73           moreover

    74           have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"

    75             using \<open>x\<noteq>y\<close>[unfolded dist_nz] \<open>d>0\<close> unfolding scaleR_eq_0_iff

    76             by (auto simp: dist_commute)

    77           moreover

    78           have "dist (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) y < d"

    79             unfolding dist_norm

    80             apply simp

    81             unfolding norm_minus_cancel

    82             using \<open>d > 0\<close> \<open>x\<noteq>y\<close>[unfolded dist_nz] dist_commute[of x y]

    83             unfolding dist_norm

    84             apply auto

    85             done

    86           ultimately show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

    87             apply (rule_tac x = "y - (d / (2*dist y x)) *\<^sub>R (y - x)" in bexI)

    88             apply auto

    89             done

    90         qed

    91       next

    92         case False

    93         then have "d > dist x y" by auto

    94         show "\<exists>x' \<in> ball x e. x' \<noteq> y \<and> dist x' y < d"

    95         proof (cases "x = y")

    96           case True

    97           obtain z where **: "z \<noteq> y" "dist z y < min e d"

    98             using perfect_choose_dist[of "min e d" y]

    99             using \<open>d > 0\<close> \<open>e>0\<close> by auto

   100           show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

   101             unfolding \<open>x = y\<close>

   102             using \<open>z \<noteq> y\<close> **

   103             apply (rule_tac x=z in bexI)

   104             apply (auto simp: dist_commute)

   105             done

   106         next

   107           case False

   108           then show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

   109             using \<open>d>0\<close> \<open>d > dist x y\<close> \<open>?rhs\<close>

   110             apply (rule_tac x=x in bexI, auto)

   111             done

   112         qed

   113       qed

   114     }

   115     then show ?thesis

   116       unfolding mem_cball islimpt_approachable mem_ball by auto

   117   qed

   118 qed

   119

   120 lemma closure_ball_lemma:

   121   fixes x y :: "'a::real_normed_vector"

   122   assumes "x \<noteq> y"

   123   shows "y islimpt ball x (dist x y)"

   124 proof (rule islimptI)

   125   fix T

   126   assume "y \<in> T" "open T"

   127   then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"

   128     unfolding open_dist by fast

   129   (* choose point between x and y, within distance r of y. *)

   130   define k where "k = min 1 (r / (2 * dist x y))"

   131   define z where "z = y + scaleR k (x - y)"

   132   have z_def2: "z = x + scaleR (1 - k) (y - x)"

   133     unfolding z_def by (simp add: algebra_simps)

   134   have "dist z y < r"

   135     unfolding z_def k_def using \<open>0 < r\<close>

   136     by (simp add: dist_norm min_def)

   137   then have "z \<in> T"

   138     using \<open>\<forall>z. dist z y < r \<longrightarrow> z \<in> T\<close> by simp

   139   have "dist x z < dist x y"

   140     unfolding z_def2 dist_norm

   141     apply (simp add: norm_minus_commute)

   142     apply (simp only: dist_norm [symmetric])

   143     apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)

   144     apply (rule mult_strict_right_mono)

   145     apply (simp add: k_def \<open>0 < r\<close> \<open>x \<noteq> y\<close>)

   146     apply (simp add: \<open>x \<noteq> y\<close>)

   147     done

   148   then have "z \<in> ball x (dist x y)"

   149     by simp

   150   have "z \<noteq> y"

   151     unfolding z_def k_def using \<open>x \<noteq> y\<close> \<open>0 < r\<close>

   152     by (simp add: min_def)

   153   show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"

   154     using \<open>z \<in> ball x (dist x y)\<close> \<open>z \<in> T\<close> \<open>z \<noteq> y\<close>

   155     by fast

   156 qed

   157

   158 lemma closure_ball [simp]:

   159   fixes x :: "'a::real_normed_vector"

   160   shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"

   161   apply (rule equalityI)

   162   apply (rule closure_minimal)

   163   apply (rule ball_subset_cball)

   164   apply (rule closed_cball)

   165   apply (rule subsetI, rename_tac y)

   166   apply (simp add: le_less [where 'a=real])

   167   apply (erule disjE)

   168   apply (rule subsetD [OF closure_subset], simp)

   169   apply (simp add: closure_def, clarify)

   170   apply (rule closure_ball_lemma)

   171   apply (simp add: zero_less_dist_iff)

   172   done

   173

   174 (* In a trivial vector space, this fails for e = 0. *)

   175 lemma interior_cball [simp]:

   176   fixes x :: "'a::{real_normed_vector, perfect_space}"

   177   shows "interior (cball x e) = ball x e"

   178 proof (cases "e \<ge> 0")

   179   case False note cs = this

   180   from cs have null: "ball x e = {}"

   181     using ball_empty[of e x] by auto

   182   moreover

   183   {

   184     fix y

   185     assume "y \<in> cball x e"

   186     then have False

   187       by (metis ball_eq_empty null cs dist_eq_0_iff dist_le_zero_iff empty_subsetI mem_cball subset_antisym subset_ball)

   188   }

   189   then have "cball x e = {}" by auto

   190   then have "interior (cball x e) = {}"

   191     using interior_empty by auto

   192   ultimately show ?thesis by blast

   193 next

   194   case True note cs = this

   195   have "ball x e \<subseteq> cball x e"

   196     using ball_subset_cball by auto

   197   moreover

   198   {

   199     fix S y

   200     assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"

   201     then obtain d where "d>0" and d: "\<forall>x'. dist x' y < d \<longrightarrow> x' \<in> S"

   202       unfolding open_dist by blast

   203     then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"

   204       using perfect_choose_dist [of d] by auto

   205     have "xa \<in> S"

   206       using d[THEN spec[where x = xa]]

   207       using xa by (auto simp: dist_commute)

   208     then have xa_cball: "xa \<in> cball x e"

   209       using as(1) by auto

   210     then have "y \<in> ball x e"

   211     proof (cases "x = y")

   212       case True

   213       then have "e > 0" using cs order.order_iff_strict xa_cball xa_y by fastforce

   214       then show "y \<in> ball x e"

   215         using \<open>x = y \<close> by simp

   216     next

   217       case False

   218       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d"

   219         unfolding dist_norm

   220         using \<open>d>0\<close> norm_ge_zero[of "y - x"] \<open>x \<noteq> y\<close> by auto

   221       then have *: "y + (d / 2 / dist y x) *\<^sub>R (y - x) \<in> cball x e"

   222         using d as(1)[unfolded subset_eq] by blast

   223       have "y - x \<noteq> 0" using \<open>x \<noteq> y\<close> by auto

   224       hence **:"d / (2 * norm (y - x)) > 0"

   225         unfolding zero_less_norm_iff[symmetric] using \<open>d>0\<close> by auto

   226       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) x =

   227         norm (y + (d / (2 * norm (y - x))) *\<^sub>R y - (d / (2 * norm (y - x))) *\<^sub>R x - x)"

   228         by (auto simp: dist_norm algebra_simps)

   229       also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"

   230         by (auto simp: algebra_simps)

   231       also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"

   232         using ** by auto

   233       also have "\<dots> = (dist y x) + d/2"

   234         using ** by (auto simp: distrib_right dist_norm)

   235       finally have "e \<ge> dist x y +d/2"

   236         using *[unfolded mem_cball] by (auto simp: dist_commute)

   237       then show "y \<in> ball x e"

   238         unfolding mem_ball using \<open>d>0\<close> by auto

   239     qed

   240   }

   241   then have "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e"

   242     by auto

   243   ultimately show ?thesis

   244     using interior_unique[of "ball x e" "cball x e"]

   245     using open_ball[of x e]

   246     by auto

   247 qed

   248

   249 lemma interior_ball [simp]: "interior (ball x e) = ball x e"

   250   by (simp add: interior_open)

   251

   252 lemma frontier_ball [simp]:

   253   fixes a :: "'a::real_normed_vector"

   254   shows "0 < e \<Longrightarrow> frontier (ball a e) = sphere a e"

   255   by (force simp: frontier_def)

   256

   257 lemma frontier_cball [simp]:

   258   fixes a :: "'a::{real_normed_vector, perfect_space}"

   259   shows "frontier (cball a e) = sphere a e"

   260   by (force simp: frontier_def)

   261

   262 lemma cball_eq_empty [simp]: "cball x e = {} \<longleftrightarrow> e < 0"

   263   apply (simp add: set_eq_iff not_le)

   264   apply (metis zero_le_dist dist_self order_less_le_trans)

   265   done

   266

   267 lemma cball_empty [simp]: "e < 0 \<Longrightarrow> cball x e = {}"

   268   by simp

   269

   270 lemma cball_eq_sing:

   271   fixes x :: "'a::{metric_space,perfect_space}"

   272   shows "cball x e = {x} \<longleftrightarrow> e = 0"

   273 proof (rule linorder_cases)

   274   assume e: "0 < e"

   275   obtain a where "a \<noteq> x" "dist a x < e"

   276     using perfect_choose_dist [OF e] by auto

   277   then have "a \<noteq> x" "dist x a \<le> e"

   278     by (auto simp: dist_commute)

   279   with e show ?thesis by (auto simp: set_eq_iff)

   280 qed auto

   281

   282 lemma cball_sing:

   283   fixes x :: "'a::metric_space"

   284   shows "e = 0 \<Longrightarrow> cball x e = {x}"

   285   by (auto simp: set_eq_iff)

   286

   287 lemma ball_divide_subset: "d \<ge> 1 \<Longrightarrow> ball x (e/d) \<subseteq> ball x e"

   288   apply (cases "e \<le> 0")

   289   apply (simp add: ball_empty divide_simps)

   290   apply (rule subset_ball)

   291   apply (simp add: divide_simps)

   292   done

   293

   294 lemma ball_divide_subset_numeral: "ball x (e / numeral w) \<subseteq> ball x e"

   295   using ball_divide_subset one_le_numeral by blast

   296

   297 lemma cball_divide_subset: "d \<ge> 1 \<Longrightarrow> cball x (e/d) \<subseteq> cball x e"

   298   apply (cases "e < 0")

   299   apply (simp add: divide_simps)

   300   apply (rule subset_cball)

   301   apply (metis div_by_1 frac_le not_le order_refl zero_less_one)

   302   done

   303

   304 lemma cball_divide_subset_numeral: "cball x (e / numeral w) \<subseteq> cball x e"

   305   using cball_divide_subset one_le_numeral by blast

   306

   307 lemma compact_cball[simp]:

   308   fixes x :: "'a::heine_borel"

   309   shows "compact (cball x e)"

   310   using compact_eq_bounded_closed bounded_cball closed_cball

   311   by blast

   312

   313 lemma compact_frontier_bounded[intro]:

   314   fixes S :: "'a::heine_borel set"

   315   shows "bounded S \<Longrightarrow> compact (frontier S)"

   316   unfolding frontier_def

   317   using compact_eq_bounded_closed

   318   by blast

   319

   320 lemma compact_frontier[intro]:

   321   fixes S :: "'a::heine_borel set"

   322   shows "compact S \<Longrightarrow> compact (frontier S)"

   323   using compact_eq_bounded_closed compact_frontier_bounded

   324   by blast

   325

   326 corollary compact_sphere [simp]:

   327   fixes a :: "'a::{real_normed_vector,perfect_space,heine_borel}"

   328   shows "compact (sphere a r)"

   329 using compact_frontier [of "cball a r"] by simp

   330

   331 corollary bounded_sphere [simp]:

   332   fixes a :: "'a::{real_normed_vector,perfect_space,heine_borel}"

   333   shows "bounded (sphere a r)"

   334 by (simp add: compact_imp_bounded)

   335

   336 corollary closed_sphere  [simp]:

   337   fixes a :: "'a::{real_normed_vector,perfect_space,heine_borel}"

   338   shows "closed (sphere a r)"

   339 by (simp add: compact_imp_closed)

   340

   341 subsection \<open>Connectedness\<close>

   342

   343 lemma connected_local:

   344  "connected S \<longleftrightarrow>

   345   \<not> (\<exists>e1 e2.

   346       openin (subtopology euclidean S) e1 \<and>

   347       openin (subtopology euclidean S) e2 \<and>

   348       S \<subseteq> e1 \<union> e2 \<and>

   349       e1 \<inter> e2 = {} \<and>

   350       e1 \<noteq> {} \<and>

   351       e2 \<noteq> {})"

   352   unfolding connected_def openin_open

   353   by safe blast+

   354

   355 lemma exists_diff:

   356   fixes P :: "'a set \<Rightarrow> bool"

   357   shows "(\<exists>S. P (- S)) \<longleftrightarrow> (\<exists>S. P S)"

   358     (is "?lhs \<longleftrightarrow> ?rhs")

   359 proof -

   360   have ?rhs if ?lhs

   361     using that by blast

   362   moreover have "P (- (- S))" if "P S" for S

   363   proof -

   364     have "S = - (- S)" by simp

   365     with that show ?thesis by metis

   366   qed

   367   ultimately show ?thesis by metis

   368 qed

   369

   370 lemma connected_clopen: "connected S \<longleftrightarrow>

   371   (\<forall>T. openin (subtopology euclidean S) T \<and>

   372      closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")

   373 proof -

   374   have "\<not> connected S \<longleftrightarrow>

   375     (\<exists>e1 e2. open e1 \<and> open (- e2) \<and> S \<subseteq> e1 \<union> (- e2) \<and> e1 \<inter> (- e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (- e2) \<inter> S \<noteq> {})"

   376     unfolding connected_def openin_open closedin_closed

   377     by (metis double_complement)

   378   then have th0: "connected S \<longleftrightarrow>

   379     \<not> (\<exists>e2 e1. closed e2 \<and> open e1 \<and> S \<subseteq> e1 \<union> (- e2) \<and> e1 \<inter> (- e2) \<inter> S = {} \<and> e1 \<inter> S \<noteq> {} \<and> (- e2) \<inter> S \<noteq> {})"

   380     (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)")

   381     by (simp add: closed_def) metis

   382   have th1: "?rhs \<longleftrightarrow> \<not> (\<exists>t' t. closed t'\<and>t = S\<inter>t' \<and> t\<noteq>{} \<and> t\<noteq>S \<and> (\<exists>t'. open t' \<and> t = S \<inter> t'))"

   383     (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")

   384     unfolding connected_def openin_open closedin_closed by auto

   385   have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" for e2

   386   proof -

   387     have "?P e2 e1 \<longleftrightarrow> (\<exists>t. closed e2 \<and> t = S\<inter>e2 \<and> open e1 \<and> t = S\<inter>e1 \<and> t\<noteq>{} \<and> t \<noteq> S)" for e1

   388       by auto

   389     then show ?thesis

   390       by metis

   391   qed

   392   then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)"

   393     by blast

   394   then show ?thesis

   395     by (simp add: th0 th1)

   396 qed

   397

   398 lemma connected_continuous_image:

   399   assumes "continuous_on s f"

   400     and "connected s"

   401   shows "connected(f  s)"

   402 proof -

   403   {

   404     fix T

   405     assume as:

   406       "T \<noteq> {}"

   407       "T \<noteq> f  s"

   408       "openin (subtopology euclidean (f  s)) T"

   409       "closedin (subtopology euclidean (f  s)) T"

   410     have "{x \<in> s. f x \<in> T} = {} \<or> {x \<in> s. f x \<in> T} = s"

   411       using assms(1)[unfolded continuous_on_open, THEN spec[where x=T]]

   412       using assms(1)[unfolded continuous_on_closed, THEN spec[where x=T]]

   413       using assms(2)[unfolded connected_clopen, THEN spec[where x="{x \<in> s. f x \<in> T}"]] as(3,4) by auto

   414     then have False using as(1,2)

   415       using as(4)[unfolded closedin_def topspace_euclidean_subtopology] by auto

   416   }

   417   then show ?thesis

   418     unfolding connected_clopen by auto

   419 qed

   420

   421 lemma connected_linear_image:

   422   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_normed_vector"

   423   assumes "linear f" and "connected s"

   424   shows "connected (f  s)"

   425 using connected_continuous_image assms linear_continuous_on linear_conv_bounded_linear by blast

   426

   427 subsection \<open>Connected components, considered as a connectedness relation or a set\<close>

   428

   429 definition "connected_component s x y \<equiv> \<exists>t. connected t \<and> t \<subseteq> s \<and> x \<in> t \<and> y \<in> t"

   430

   431 abbreviation "connected_component_set s x \<equiv> Collect (connected_component s x)"

   432

   433 lemma connected_componentI:

   434   "connected t \<Longrightarrow> t \<subseteq> s \<Longrightarrow> x \<in> t \<Longrightarrow> y \<in> t \<Longrightarrow> connected_component s x y"

   435   by (auto simp: connected_component_def)

   436

   437 lemma connected_component_in: "connected_component s x y \<Longrightarrow> x \<in> s \<and> y \<in> s"

   438   by (auto simp: connected_component_def)

   439

   440 lemma connected_component_refl: "x \<in> s \<Longrightarrow> connected_component s x x"

   441   by (auto simp: connected_component_def) (use connected_sing in blast)

   442

   443 lemma connected_component_refl_eq [simp]: "connected_component s x x \<longleftrightarrow> x \<in> s"

   444   by (auto simp: connected_component_refl) (auto simp: connected_component_def)

   445

   446 lemma connected_component_sym: "connected_component s x y \<Longrightarrow> connected_component s y x"

   447   by (auto simp: connected_component_def)

   448

   449 lemma connected_component_trans:

   450   "connected_component s x y \<Longrightarrow> connected_component s y z \<Longrightarrow> connected_component s x z"

   451   unfolding connected_component_def

   452   by (metis Int_iff Un_iff Un_subset_iff equals0D connected_Un)

   453

   454 lemma connected_component_of_subset:

   455   "connected_component s x y \<Longrightarrow> s \<subseteq> t \<Longrightarrow> connected_component t x y"

   456   by (auto simp: connected_component_def)

   457

   458 lemma connected_component_Union: "connected_component_set s x = \<Union>{t. connected t \<and> x \<in> t \<and> t \<subseteq> s}"

   459   by (auto simp: connected_component_def)

   460

   461 lemma connected_connected_component [iff]: "connected (connected_component_set s x)"

   462   by (auto simp: connected_component_Union intro: connected_Union)

   463

   464 lemma connected_iff_eq_connected_component_set:

   465   "connected s \<longleftrightarrow> (\<forall>x \<in> s. connected_component_set s x = s)"

   466 proof (cases "s = {}")

   467   case True

   468   then show ?thesis by simp

   469 next

   470   case False

   471   then obtain x where "x \<in> s" by auto

   472   show ?thesis

   473   proof

   474     assume "connected s"

   475     then show "\<forall>x \<in> s. connected_component_set s x = s"

   476       by (force simp: connected_component_def)

   477   next

   478     assume "\<forall>x \<in> s. connected_component_set s x = s"

   479     then show "connected s"

   480       by (metis \<open>x \<in> s\<close> connected_connected_component)

   481   qed

   482 qed

   483

   484 lemma connected_component_subset: "connected_component_set s x \<subseteq> s"

   485   using connected_component_in by blast

   486

   487 lemma connected_component_eq_self: "connected s \<Longrightarrow> x \<in> s \<Longrightarrow> connected_component_set s x = s"

   488   by (simp add: connected_iff_eq_connected_component_set)

   489

   490 lemma connected_iff_connected_component:

   491   "connected s \<longleftrightarrow> (\<forall>x \<in> s. \<forall>y \<in> s. connected_component s x y)"

   492   using connected_component_in by (auto simp: connected_iff_eq_connected_component_set)

   493

   494 lemma connected_component_maximal:

   495   "x \<in> t \<Longrightarrow> connected t \<Longrightarrow> t \<subseteq> s \<Longrightarrow> t \<subseteq> (connected_component_set s x)"

   496   using connected_component_eq_self connected_component_of_subset by blast

   497

   498 lemma connected_component_mono:

   499   "s \<subseteq> t \<Longrightarrow> connected_component_set s x \<subseteq> connected_component_set t x"

   500   by (simp add: Collect_mono connected_component_of_subset)

   501

   502 lemma connected_component_eq_empty [simp]: "connected_component_set s x = {} \<longleftrightarrow> x \<notin> s"

   503   using connected_component_refl by (fastforce simp: connected_component_in)

   504

   505 lemma connected_component_set_empty [simp]: "connected_component_set {} x = {}"

   506   using connected_component_eq_empty by blast

   507

   508 lemma connected_component_eq:

   509   "y \<in> connected_component_set s x \<Longrightarrow> (connected_component_set s y = connected_component_set s x)"

   510   by (metis (no_types, lifting)

   511       Collect_cong connected_component_sym connected_component_trans mem_Collect_eq)

   512

   513 lemma closed_connected_component:

   514   assumes s: "closed s"

   515   shows "closed (connected_component_set s x)"

   516 proof (cases "x \<in> s")

   517   case False

   518   then show ?thesis

   519     by (metis connected_component_eq_empty closed_empty)

   520 next

   521   case True

   522   show ?thesis

   523     unfolding closure_eq [symmetric]

   524   proof

   525     show "closure (connected_component_set s x) \<subseteq> connected_component_set s x"

   526       apply (rule connected_component_maximal)

   527         apply (simp add: closure_def True)

   528        apply (simp add: connected_imp_connected_closure)

   529       apply (simp add: s closure_minimal connected_component_subset)

   530       done

   531   next

   532     show "connected_component_set s x \<subseteq> closure (connected_component_set s x)"

   533       by (simp add: closure_subset)

   534   qed

   535 qed

   536

   537 lemma connected_component_disjoint:

   538   "connected_component_set s a \<inter> connected_component_set s b = {} \<longleftrightarrow>

   539     a \<notin> connected_component_set s b"

   540   apply (auto simp: connected_component_eq)

   541   using connected_component_eq connected_component_sym

   542   apply blast

   543   done

   544

   545 lemma connected_component_nonoverlap:

   546   "connected_component_set s a \<inter> connected_component_set s b = {} \<longleftrightarrow>

   547     a \<notin> s \<or> b \<notin> s \<or> connected_component_set s a \<noteq> connected_component_set s b"

   548   apply (auto simp: connected_component_in)

   549   using connected_component_refl_eq

   550     apply blast

   551    apply (metis connected_component_eq mem_Collect_eq)

   552   apply (metis connected_component_eq mem_Collect_eq)

   553   done

   554

   555 lemma connected_component_overlap:

   556   "connected_component_set s a \<inter> connected_component_set s b \<noteq> {} \<longleftrightarrow>

   557     a \<in> s \<and> b \<in> s \<and> connected_component_set s a = connected_component_set s b"

   558   by (auto simp: connected_component_nonoverlap)

   559

   560 lemma connected_component_sym_eq: "connected_component s x y \<longleftrightarrow> connected_component s y x"

   561   using connected_component_sym by blast

   562

   563 lemma connected_component_eq_eq:

   564   "connected_component_set s x = connected_component_set s y \<longleftrightarrow>

   565     x \<notin> s \<and> y \<notin> s \<or> x \<in> s \<and> y \<in> s \<and> connected_component s x y"

   566   apply (cases "y \<in> s", simp)

   567    apply (metis connected_component_eq connected_component_eq_empty connected_component_refl_eq mem_Collect_eq)

   568   apply (cases "x \<in> s", simp)

   569    apply (metis connected_component_eq_empty)

   570   using connected_component_eq_empty

   571   apply blast

   572   done

   573

   574 lemma connected_iff_connected_component_eq:

   575   "connected s \<longleftrightarrow> (\<forall>x \<in> s. \<forall>y \<in> s. connected_component_set s x = connected_component_set s y)"

   576   by (simp add: connected_component_eq_eq connected_iff_connected_component)

   577

   578 lemma connected_component_idemp:

   579   "connected_component_set (connected_component_set s x) x = connected_component_set s x"

   580   apply (rule subset_antisym)

   581    apply (simp add: connected_component_subset)

   582   apply (metis connected_component_eq_empty connected_component_maximal

   583       connected_component_refl_eq connected_connected_component mem_Collect_eq set_eq_subset)

   584   done

   585

   586 lemma connected_component_unique:

   587   "\<lbrakk>x \<in> c; c \<subseteq> s; connected c;

   588     \<And>c'. x \<in> c' \<and> c' \<subseteq> s \<and> connected c'

   589               \<Longrightarrow> c' \<subseteq> c\<rbrakk>

   590         \<Longrightarrow> connected_component_set s x = c"

   591 apply (rule subset_antisym)

   592 apply (meson connected_component_maximal connected_component_subset connected_connected_component contra_subsetD)

   593 by (simp add: connected_component_maximal)

   594

   595 lemma joinable_connected_component_eq:

   596   "\<lbrakk>connected t; t \<subseteq> s;

   597     connected_component_set s x \<inter> t \<noteq> {};

   598     connected_component_set s y \<inter> t \<noteq> {}\<rbrakk>

   599     \<Longrightarrow> connected_component_set s x = connected_component_set s y"

   600 apply (simp add: ex_in_conv [symmetric])

   601 apply (rule connected_component_eq)

   602 by (metis (no_types, hide_lams) connected_component_eq_eq connected_component_in connected_component_maximal subsetD mem_Collect_eq)

   603

   604

   605 lemma Union_connected_component: "\<Union>(connected_component_set s  s) = s"

   606   apply (rule subset_antisym)

   607   apply (simp add: SUP_least connected_component_subset)

   608   using connected_component_refl_eq

   609   by force

   610

   611

   612 lemma complement_connected_component_unions:

   613     "s - connected_component_set s x =

   614      \<Union>(connected_component_set s  s - {connected_component_set s x})"

   615   apply (subst Union_connected_component [symmetric], auto)

   616   apply (metis connected_component_eq_eq connected_component_in)

   617   by (metis connected_component_eq mem_Collect_eq)

   618

   619 lemma connected_component_intermediate_subset:

   620         "\<lbrakk>connected_component_set u a \<subseteq> t; t \<subseteq> u\<rbrakk>

   621         \<Longrightarrow> connected_component_set t a = connected_component_set u a"

   622   apply (case_tac "a \<in> u")

   623   apply (simp add: connected_component_maximal connected_component_mono subset_antisym)

   624   using connected_component_eq_empty by blast

   625

   626 proposition connected_Times:

   627   assumes S: "connected S" and T: "connected T"

   628   shows "connected (S \<times> T)"

   629 proof (clarsimp simp add: connected_iff_connected_component)

   630   fix x y x' y'

   631   assume xy: "x \<in> S" "y \<in> T" "x' \<in> S" "y' \<in> T"

   632   with xy obtain U V where U: "connected U" "U \<subseteq> S" "x \<in> U" "x' \<in> U"

   633                        and V: "connected V" "V \<subseteq> T" "y \<in> V" "y' \<in> V"

   634     using S T \<open>x \<in> S\<close> \<open>x' \<in> S\<close> by blast+

   635   show "connected_component (S \<times> T) (x, y) (x', y')"

   636     unfolding connected_component_def

   637   proof (intro exI conjI)

   638     show "connected ((\<lambda>x. (x, y))  U \<union> Pair x'  V)"

   639     proof (rule connected_Un)

   640       have "continuous_on U (\<lambda>x. (x, y))"

   641         by (intro continuous_intros)

   642       then show "connected ((\<lambda>x. (x, y))  U)"

   643         by (rule connected_continuous_image) (rule \<open>connected U\<close>)

   644       have "continuous_on V (Pair x')"

   645         by (intro continuous_intros)

   646       then show "connected (Pair x'  V)"

   647         by (rule connected_continuous_image) (rule \<open>connected V\<close>)

   648     qed (use U V in auto)

   649   qed (use U V in auto)

   650 qed

   651

   652 corollary connected_Times_eq [simp]:

   653    "connected (S \<times> T) \<longleftrightarrow> S = {} \<or> T = {} \<or> connected S \<and> connected T"  (is "?lhs = ?rhs")

   654 proof

   655   assume L: ?lhs

   656   show ?rhs

   657   proof (cases "S = {} \<or> T = {}")

   658     case True

   659     then show ?thesis by auto

   660   next

   661     case False

   662     have "connected (fst  (S \<times> T))" "connected (snd  (S \<times> T))"

   663       using continuous_on_fst continuous_on_snd continuous_on_id

   664       by (blast intro: connected_continuous_image [OF _ L])+

   665     with False show ?thesis

   666       by auto

   667   qed

   668 next

   669   assume ?rhs

   670   then show ?lhs

   671     by (auto simp: connected_Times)

   672 qed

   673

   674

   675 subsection \<open>The set of connected components of a set\<close>

   676

   677 definition components:: "'a::topological_space set \<Rightarrow> 'a set set"

   678   where "components s \<equiv> connected_component_set s  s"

   679

   680 lemma components_iff: "s \<in> components u \<longleftrightarrow> (\<exists>x. x \<in> u \<and> s = connected_component_set u x)"

   681   by (auto simp: components_def)

   682

   683 lemma componentsI: "x \<in> u \<Longrightarrow> connected_component_set u x \<in> components u"

   684   by (auto simp: components_def)

   685

   686 lemma componentsE:

   687   assumes "s \<in> components u"

   688   obtains x where "x \<in> u" "s = connected_component_set u x"

   689   using assms by (auto simp: components_def)

   690

   691 lemma Union_components [simp]: "\<Union>(components u) = u"

   692   apply (rule subset_antisym)

   693   using Union_connected_component components_def apply fastforce

   694   apply (metis Union_connected_component components_def set_eq_subset)

   695   done

   696

   697 lemma pairwise_disjoint_components: "pairwise (\<lambda>X Y. X \<inter> Y = {}) (components u)"

   698   apply (simp add: pairwise_def)

   699   apply (auto simp: components_iff)

   700   apply (metis connected_component_eq_eq connected_component_in)+

   701   done

   702

   703 lemma in_components_nonempty: "c \<in> components s \<Longrightarrow> c \<noteq> {}"

   704     by (metis components_iff connected_component_eq_empty)

   705

   706 lemma in_components_subset: "c \<in> components s \<Longrightarrow> c \<subseteq> s"

   707   using Union_components by blast

   708

   709 lemma in_components_connected: "c \<in> components s \<Longrightarrow> connected c"

   710   by (metis components_iff connected_connected_component)

   711

   712 lemma in_components_maximal:

   713   "c \<in> components s \<longleftrightarrow>

   714     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)"

   715   apply (rule iffI)

   716    apply (simp add: in_components_nonempty in_components_connected)

   717    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)

   718   apply (metis bot.extremum_uniqueI components_iff connected_component_eq_empty connected_component_maximal connected_component_subset connected_connected_component subset_emptyI)

   719   done

   720

   721 lemma joinable_components_eq:

   722   "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"

   723   by (metis (full_types) components_iff joinable_connected_component_eq)

   724

   725 lemma closed_components: "\<lbrakk>closed s; c \<in> components s\<rbrakk> \<Longrightarrow> closed c"

   726   by (metis closed_connected_component components_iff)

   727

   728 lemma compact_components:

   729   fixes s :: "'a::heine_borel set"

   730   shows "\<lbrakk>compact s; c \<in> components s\<rbrakk> \<Longrightarrow> compact c"

   731 by (meson bounded_subset closed_components in_components_subset compact_eq_bounded_closed)

   732

   733 lemma components_nonoverlap:

   734     "\<lbrakk>c \<in> components s; c' \<in> components s\<rbrakk> \<Longrightarrow> (c \<inter> c' = {}) \<longleftrightarrow> (c \<noteq> c')"

   735   apply (auto simp: in_components_nonempty components_iff)

   736     using connected_component_refl apply blast

   737    apply (metis connected_component_eq_eq connected_component_in)

   738   by (metis connected_component_eq mem_Collect_eq)

   739

   740 lemma components_eq: "\<lbrakk>c \<in> components s; c' \<in> components s\<rbrakk> \<Longrightarrow> (c = c' \<longleftrightarrow> c \<inter> c' \<noteq> {})"

   741   by (metis components_nonoverlap)

   742

   743 lemma components_eq_empty [simp]: "components s = {} \<longleftrightarrow> s = {}"

   744   by (simp add: components_def)

   745

   746 lemma components_empty [simp]: "components {} = {}"

   747   by simp

   748

   749 lemma connected_eq_connected_components_eq: "connected s \<longleftrightarrow> (\<forall>c \<in> components s. \<forall>c' \<in> components s. c = c')"

   750   by (metis (no_types, hide_lams) components_iff connected_component_eq_eq connected_iff_connected_component)

   751

   752 lemma components_eq_sing_iff: "components s = {s} \<longleftrightarrow> connected s \<and> s \<noteq> {}"

   753   apply (rule iffI)

   754   using in_components_connected apply fastforce

   755   apply safe

   756   using Union_components apply fastforce

   757    apply (metis components_iff connected_component_eq_self)

   758   using in_components_maximal

   759   apply auto

   760   done

   761

   762 lemma components_eq_sing_exists: "(\<exists>a. components s = {a}) \<longleftrightarrow> connected s \<and> s \<noteq> {}"

   763   apply (rule iffI)

   764   using connected_eq_connected_components_eq apply fastforce

   765   apply (metis components_eq_sing_iff)

   766   done

   767

   768 lemma connected_eq_components_subset_sing: "connected s \<longleftrightarrow> components s \<subseteq> {s}"

   769   by (metis Union_components components_empty components_eq_sing_iff connected_empty insert_subset order_refl subset_singletonD)

   770

   771 lemma connected_eq_components_subset_sing_exists: "connected s \<longleftrightarrow> (\<exists>a. components s \<subseteq> {a})"

   772   by (metis components_eq_sing_exists connected_eq_components_subset_sing empty_iff subset_iff subset_singletonD)

   773

   774 lemma in_components_self: "s \<in> components s \<longleftrightarrow> connected s \<and> s \<noteq> {}"

   775   by (metis components_empty components_eq_sing_iff empty_iff in_components_connected insertI1)

   776

   777 lemma components_maximal: "\<lbrakk>c \<in> components s; connected t; t \<subseteq> s; c \<inter> t \<noteq> {}\<rbrakk> \<Longrightarrow> t \<subseteq> c"

   778   apply (simp add: components_def ex_in_conv [symmetric], clarify)

   779   by (meson connected_component_def connected_component_trans)

   780

   781 lemma exists_component_superset: "\<lbrakk>t \<subseteq> s; s \<noteq> {}; connected t\<rbrakk> \<Longrightarrow> \<exists>c. c \<in> components s \<and> t \<subseteq> c"

   782   apply (cases "t = {}", force)

   783   apply (metis components_def ex_in_conv connected_component_maximal contra_subsetD image_eqI)

   784   done

   785

   786 lemma components_intermediate_subset: "\<lbrakk>s \<in> components u; s \<subseteq> t; t \<subseteq> u\<rbrakk> \<Longrightarrow> s \<in> components t"

   787   apply (auto simp: components_iff)

   788   apply (metis connected_component_eq_empty connected_component_intermediate_subset)

   789   done

   790

   791 lemma in_components_unions_complement: "c \<in> components s \<Longrightarrow> s - c = \<Union>(components s - {c})"

   792   by (metis complement_connected_component_unions components_def components_iff)

   793

   794 lemma connected_intermediate_closure:

   795   assumes cs: "connected s" and st: "s \<subseteq> t" and ts: "t \<subseteq> closure s"

   796   shows "connected t"

   797 proof (rule connectedI)

   798   fix A B

   799   assume A: "open A" and B: "open B" and Alap: "A \<inter> t \<noteq> {}" and Blap: "B \<inter> t \<noteq> {}"

   800     and disj: "A \<inter> B \<inter> t = {}" and cover: "t \<subseteq> A \<union> B"

   801   have disjs: "A \<inter> B \<inter> s = {}"

   802     using disj st by auto

   803   have "A \<inter> closure s \<noteq> {}"

   804     using Alap Int_absorb1 ts by blast

   805   then have Alaps: "A \<inter> s \<noteq> {}"

   806     by (simp add: A open_Int_closure_eq_empty)

   807   have "B \<inter> closure s \<noteq> {}"

   808     using Blap Int_absorb1 ts by blast

   809   then have Blaps: "B \<inter> s \<noteq> {}"

   810     by (simp add: B open_Int_closure_eq_empty)

   811   then show False

   812     using cs [unfolded connected_def] A B disjs Alaps Blaps cover st

   813     by blast

   814 qed

   815

   816 lemma closedin_connected_component: "closedin (subtopology euclidean s) (connected_component_set s x)"

   817 proof (cases "connected_component_set s x = {}")

   818   case True

   819   then show ?thesis

   820     by (metis closedin_empty)

   821 next

   822   case False

   823   then obtain y where y: "connected_component s x y"

   824     by blast

   825   have *: "connected_component_set s x \<subseteq> s \<inter> closure (connected_component_set s x)"

   826     by (auto simp: closure_def connected_component_in)

   827   have "connected_component s x y \<Longrightarrow> s \<inter> closure (connected_component_set s x) \<subseteq> connected_component_set s x"

   828     apply (rule connected_component_maximal, simp)

   829     using closure_subset connected_component_in apply fastforce

   830     using * connected_intermediate_closure apply blast+

   831     done

   832   with y * show ?thesis

   833     by (auto simp: closedin_closed)

   834 qed

   835

   836

   837 subsection \<open>Intersecting chains of compact sets and the Baire property\<close>

   838

   839 proposition bounded_closed_chain:

   840   fixes \<F> :: "'a::heine_borel set set"

   841   assumes "B \<in> \<F>" "bounded B" and \<F>: "\<And>S. S \<in> \<F> \<Longrightarrow> closed S" and "{} \<notin> \<F>"

   842       and chain: "\<And>S T. S \<in> \<F> \<and> T \<in> \<F> \<Longrightarrow> S \<subseteq> T \<or> T \<subseteq> S"

   843     shows "\<Inter>\<F> \<noteq> {}"

   844 proof -

   845   have "B \<inter> \<Inter>\<F> \<noteq> {}"

   846   proof (rule compact_imp_fip)

   847     show "compact B" "\<And>T. T \<in> \<F> \<Longrightarrow> closed T"

   848       by (simp_all add: assms compact_eq_bounded_closed)

   849     show "\<lbrakk>finite \<G>; \<G> \<subseteq> \<F>\<rbrakk> \<Longrightarrow> B \<inter> \<Inter>\<G> \<noteq> {}" for \<G>

   850     proof (induction \<G> rule: finite_induct)

   851       case empty

   852       with assms show ?case by force

   853     next

   854       case (insert U \<G>)

   855       then have "U \<in> \<F>" and ne: "B \<inter> \<Inter>\<G> \<noteq> {}" by auto

   856       then consider "B \<subseteq> U" | "U \<subseteq> B"

   857           using \<open>B \<in> \<F>\<close> chain by blast

   858         then show ?case

   859         proof cases

   860           case 1

   861           then show ?thesis

   862             using Int_left_commute ne by auto

   863         next

   864           case 2

   865           have "U \<noteq> {}"

   866             using \<open>U \<in> \<F>\<close> \<open>{} \<notin> \<F>\<close> by blast

   867           moreover

   868           have False if "\<And>x. x \<in> U \<Longrightarrow> \<exists>Y\<in>\<G>. x \<notin> Y"

   869           proof -

   870             have "\<And>x. x \<in> U \<Longrightarrow> \<exists>Y\<in>\<G>. Y \<subseteq> U"

   871               by (metis chain contra_subsetD insert.prems insert_subset that)

   872             then obtain Y where "Y \<in> \<G>" "Y \<subseteq> U"

   873               by (metis all_not_in_conv \<open>U \<noteq> {}\<close>)

   874             moreover obtain x where "x \<in> \<Inter>\<G>"

   875               by (metis Int_emptyI ne)

   876             ultimately show ?thesis

   877               by (metis Inf_lower subset_eq that)

   878           qed

   879           with 2 show ?thesis

   880             by blast

   881         qed

   882       qed

   883   qed

   884   then show ?thesis by blast

   885 qed

   886

   887 corollary compact_chain:

   888   fixes \<F> :: "'a::heine_borel set set"

   889   assumes "\<And>S. S \<in> \<F> \<Longrightarrow> compact S" "{} \<notin> \<F>"

   890           "\<And>S T. S \<in> \<F> \<and> T \<in> \<F> \<Longrightarrow> S \<subseteq> T \<or> T \<subseteq> S"

   891     shows "\<Inter> \<F> \<noteq> {}"

   892 proof (cases "\<F> = {}")

   893   case True

   894   then show ?thesis by auto

   895 next

   896   case False

   897   show ?thesis

   898     by (metis False all_not_in_conv assms compact_imp_bounded compact_imp_closed bounded_closed_chain)

   899 qed

   900

   901 lemma compact_nest:

   902   fixes F :: "'a::linorder \<Rightarrow> 'b::heine_borel set"

   903   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"

   904   shows "\<Inter>range F \<noteq> {}"

   905 proof -

   906   have *: "\<And>S T. S \<in> range F \<and> T \<in> range F \<Longrightarrow> S \<subseteq> T \<or> T \<subseteq> S"

   907     by (metis mono image_iff le_cases)

   908   show ?thesis

   909     apply (rule compact_chain [OF _ _ *])

   910     using F apply (blast intro: dest: *)+

   911     done

   912 qed

   913

   914 text\<open>The Baire property of dense sets\<close>

   915 theorem Baire:

   916   fixes S::"'a::{real_normed_vector,heine_borel} set"

   917   assumes "closed S" "countable \<G>"

   918       and ope: "\<And>T. T \<in> \<G> \<Longrightarrow> openin (subtopology euclidean S) T \<and> S \<subseteq> closure T"

   919  shows "S \<subseteq> closure(\<Inter>\<G>)"

   920 proof (cases "\<G> = {}")

   921   case True

   922   then show ?thesis

   923     using closure_subset by auto

   924 next

   925   let ?g = "from_nat_into \<G>"

   926   case False

   927   then have gin: "?g n \<in> \<G>" for n

   928     by (simp add: from_nat_into)

   929   show ?thesis

   930   proof (clarsimp simp: closure_approachable)

   931     fix x and e::real

   932     assume "x \<in> S" "0 < e"

   933     obtain TF where opeF: "\<And>n. openin (subtopology euclidean S) (TF n)"

   934                and ne: "\<And>n. TF n \<noteq> {}"

   935                and subg: "\<And>n. S \<inter> closure(TF n) \<subseteq> ?g n"

   936                and subball: "\<And>n. closure(TF n) \<subseteq> ball x e"

   937                and decr: "\<And>n. TF(Suc n) \<subseteq> TF n"

   938     proof -

   939       have *: "\<exists>Y. (openin (subtopology euclidean S) Y \<and> Y \<noteq> {} \<and>

   940                    S \<inter> closure Y \<subseteq> ?g n \<and> closure Y \<subseteq> ball x e) \<and> Y \<subseteq> U"

   941         if opeU: "openin (subtopology euclidean S) U" and "U \<noteq> {}" and cloU: "closure U \<subseteq> ball x e" for U n

   942       proof -

   943         obtain T where T: "open T" "U = T \<inter> S"

   944           using \<open>openin (subtopology euclidean S) U\<close> by (auto simp: openin_subtopology)

   945         with \<open>U \<noteq> {}\<close> have "T \<inter> closure (?g n) \<noteq> {}"

   946           using gin ope by fastforce

   947         then have "T \<inter> ?g n \<noteq> {}"

   948           using \<open>open T\<close> open_Int_closure_eq_empty by blast

   949         then obtain y where "y \<in> U" "y \<in> ?g n"

   950           using T ope [of "?g n", OF gin] by (blast dest:  openin_imp_subset)

   951         moreover have "openin (subtopology euclidean S) (U \<inter> ?g n)"

   952           using gin ope opeU by blast

   953         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"

   954           by (force simp: openin_contains_ball)

   955         show ?thesis

   956         proof (intro exI conjI)

   957           show "openin (subtopology euclidean S) (S \<inter> ball y (d/2))"

   958             by (simp add: openin_open_Int)

   959           show "S \<inter> ball y (d/2) \<noteq> {}"

   960             using \<open>0 < d\<close> \<open>y \<in> U\<close> opeU openin_imp_subset by fastforce

   961           have "S \<inter> closure (S \<inter> ball y (d/2)) \<subseteq> S \<inter> closure (ball y (d/2))"

   962             using closure_mono by blast

   963           also have "... \<subseteq> ?g n"

   964             using \<open>d > 0\<close> d by force

   965           finally show "S \<inter> closure (S \<inter> ball y (d/2)) \<subseteq> ?g n" .

   966           have "closure (S \<inter> ball y (d/2)) \<subseteq> S \<inter> ball y d"

   967           proof -

   968             have "closure (ball y (d/2)) \<subseteq> ball y d"

   969               using \<open>d > 0\<close> by auto

   970             then have "closure (S \<inter> ball y (d/2)) \<subseteq> ball y d"

   971               by (meson closure_mono inf.cobounded2 subset_trans)

   972             then show ?thesis

   973               by (simp add: \<open>closed S\<close> closure_minimal)

   974           qed

   975           also have "...  \<subseteq> ball x e"

   976             using cloU closure_subset d by blast

   977           finally show "closure (S \<inter> ball y (d/2)) \<subseteq> ball x e" .

   978           show "S \<inter> ball y (d/2) \<subseteq> U"

   979             using ball_divide_subset_numeral d by blast

   980         qed

   981       qed

   982       let ?\<Phi> = "\<lambda>n X. openin (subtopology euclidean S) X \<and> X \<noteq> {} \<and>

   983                       S \<inter> closure X \<subseteq> ?g n \<and> closure X \<subseteq> ball x e"

   984       have "closure (S \<inter> ball x (e / 2)) \<subseteq> closure(ball x (e/2))"

   985         by (simp add: closure_mono)

   986       also have "...  \<subseteq> ball x e"

   987         using \<open>e > 0\<close> by auto

   988       finally have "closure (S \<inter> ball x (e / 2)) \<subseteq> ball x e" .

   989       moreover have"openin (subtopology euclidean S) (S \<inter> ball x (e / 2))" "S \<inter> ball x (e / 2) \<noteq> {}"

   990         using \<open>0 < e\<close> \<open>x \<in> S\<close> by auto

   991       ultimately obtain Y where Y: "?\<Phi> 0 Y \<and> Y \<subseteq> S \<inter> ball x (e / 2)"

   992             using * [of "S \<inter> ball x (e/2)" 0] by metis

   993       show thesis

   994       proof (rule exE [OF dependent_nat_choice [of ?\<Phi> "\<lambda>n X Y. Y \<subseteq> X"]])

   995         show "\<exists>x. ?\<Phi> 0 x"

   996           using Y by auto

   997         show "\<exists>Y. ?\<Phi> (Suc n) Y \<and> Y \<subseteq> X" if "?\<Phi> n X" for X n

   998           using that by (blast intro: *)

   999       qed (use that in metis)

  1000     qed

  1001     have "(\<Inter>n. S \<inter> closure (TF n)) \<noteq> {}"

  1002     proof (rule compact_nest)

  1003       show "\<And>n. compact (S \<inter> closure (TF n))"

  1004         by (metis closed_closure subball bounded_subset_ballI compact_eq_bounded_closed closed_Int_compact [OF \<open>closed S\<close>])

  1005       show "\<And>n. S \<inter> closure (TF n) \<noteq> {}"

  1006         by (metis Int_absorb1 opeF \<open>closed S\<close> closure_eq_empty closure_minimal ne openin_imp_subset)

  1007       show "\<And>m n. m \<le> n \<Longrightarrow> S \<inter> closure (TF n) \<subseteq> S \<inter> closure (TF m)"

  1008         by (meson closure_mono decr dual_order.refl inf_mono lift_Suc_antimono_le)

  1009     qed

  1010     moreover have "(\<Inter>n. S \<inter> closure (TF n)) \<subseteq> {y \<in> \<Inter>\<G>. dist y x < e}"

  1011     proof (clarsimp, intro conjI)

  1012       fix y

  1013       assume "y \<in> S" and y: "\<forall>n. y \<in> closure (TF n)"

  1014       then show "\<forall>T\<in>\<G>. y \<in> T"

  1015         by (metis Int_iff from_nat_into_surj [OF \<open>countable \<G>\<close>] set_mp subg)

  1016       show "dist y x < e"

  1017         by (metis y dist_commute mem_ball subball subsetCE)

  1018     qed

  1019     ultimately show "\<exists>y \<in> \<Inter>\<G>. dist y x < e"

  1020       by auto

  1021   qed

  1022 qed

  1023

  1024 subsection\<open>Some theorems on sups and infs using the notion "bounded".\<close>

  1025

  1026 lemma bounded_real: "bounded (S::real set) \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. \<bar>x\<bar> \<le> a)"

  1027   by (simp add: bounded_iff)

  1028

  1029 lemma bounded_imp_bdd_above: "bounded S \<Longrightarrow> bdd_above (S :: real set)"

  1030   by (auto simp: bounded_def bdd_above_def dist_real_def)

  1031      (metis abs_le_D1 abs_minus_commute diff_le_eq)

  1032

  1033 lemma bounded_imp_bdd_below: "bounded S \<Longrightarrow> bdd_below (S :: real set)"

  1034   by (auto simp: bounded_def bdd_below_def dist_real_def)

  1035      (metis abs_le_D1 add.commute diff_le_eq)

  1036

  1037 lemma bounded_inner_imp_bdd_above:

  1038   assumes "bounded s"

  1039     shows "bdd_above ((\<lambda>x. x \<bullet> a)  s)"

  1040 by (simp add: assms bounded_imp_bdd_above bounded_linear_image bounded_linear_inner_left)

  1041

  1042 lemma bounded_inner_imp_bdd_below:

  1043   assumes "bounded s"

  1044     shows "bdd_below ((\<lambda>x. x \<bullet> a)  s)"

  1045 by (simp add: assms bounded_imp_bdd_below bounded_linear_image bounded_linear_inner_left)

  1046

  1047 lemma bounded_has_Sup:

  1048   fixes S :: "real set"

  1049   assumes "bounded S"

  1050     and "S \<noteq> {}"

  1051   shows "\<forall>x\<in>S. x \<le> Sup S"

  1052     and "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b"

  1053 proof

  1054   show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b"

  1055     using assms by (metis cSup_least)

  1056 qed (metis cSup_upper assms(1) bounded_imp_bdd_above)

  1057

  1058 lemma Sup_insert:

  1059   fixes S :: "real set"

  1060   shows "bounded S \<Longrightarrow> Sup (insert x S) = (if S = {} then x else max x (Sup S))"

  1061   by (auto simp: bounded_imp_bdd_above sup_max cSup_insert_If)

  1062

  1063 lemma Sup_insert_finite:

  1064   fixes S :: "'a::conditionally_complete_linorder set"

  1065   shows "finite S \<Longrightarrow> Sup (insert x S) = (if S = {} then x else max x (Sup S))"

  1066 by (simp add: cSup_insert sup_max)

  1067

  1068 lemma bounded_has_Inf:

  1069   fixes S :: "real set"

  1070   assumes "bounded S"

  1071     and "S \<noteq> {}"

  1072   shows "\<forall>x\<in>S. x \<ge> Inf S"

  1073     and "\<forall>b. (\<forall>x\<in>S. x \<ge> b) \<longrightarrow> Inf S \<ge> b"

  1074 proof

  1075   show "\<forall>b. (\<forall>x\<in>S. x \<ge> b) \<longrightarrow> Inf S \<ge> b"

  1076     using assms by (metis cInf_greatest)

  1077 qed (metis cInf_lower assms(1) bounded_imp_bdd_below)

  1078

  1079 lemma Inf_insert:

  1080   fixes S :: "real set"

  1081   shows "bounded S \<Longrightarrow> Inf (insert x S) = (if S = {} then x else min x (Inf S))"

  1082   by (auto simp: bounded_imp_bdd_below inf_min cInf_insert_If)

  1083

  1084 lemma Inf_insert_finite:

  1085   fixes S :: "'a::conditionally_complete_linorder set"

  1086   shows "finite S \<Longrightarrow> Inf (insert x S) = (if S = {} then x else min x (Inf S))"

  1087 by (simp add: cInf_eq_Min)

  1088

  1089 lemma finite_imp_less_Inf:

  1090   fixes a :: "'a::conditionally_complete_linorder"

  1091   shows "\<lbrakk>finite X; x \<in> X; \<And>x. x\<in>X \<Longrightarrow> a < x\<rbrakk> \<Longrightarrow> a < Inf X"

  1092   by (induction X rule: finite_induct) (simp_all add: cInf_eq_Min Inf_insert_finite)

  1093

  1094 lemma finite_less_Inf_iff:

  1095   fixes a :: "'a :: conditionally_complete_linorder"

  1096   shows "\<lbrakk>finite X; X \<noteq> {}\<rbrakk> \<Longrightarrow> a < Inf X \<longleftrightarrow> (\<forall>x \<in> X. a < x)"

  1097   by (auto simp: cInf_eq_Min)

  1098

  1099 lemma finite_imp_Sup_less:

  1100   fixes a :: "'a::conditionally_complete_linorder"

  1101   shows "\<lbrakk>finite X; x \<in> X; \<And>x. x\<in>X \<Longrightarrow> a > x\<rbrakk> \<Longrightarrow> a > Sup X"

  1102   by (induction X rule: finite_induct) (simp_all add: cSup_eq_Max Sup_insert_finite)

  1103

  1104 lemma finite_Sup_less_iff:

  1105   fixes a :: "'a :: conditionally_complete_linorder"

  1106   shows "\<lbrakk>finite X; X \<noteq> {}\<rbrakk> \<Longrightarrow> a > Sup X \<longleftrightarrow> (\<forall>x \<in> X. a > x)"

  1107   by (auto simp: cSup_eq_Max)

  1108

  1109 proposition is_interval_compact:

  1110    "is_interval S \<and> compact S \<longleftrightarrow> (\<exists>a b. S = cbox a b)"   (is "?lhs = ?rhs")

  1111 proof (cases "S = {}")

  1112   case True

  1113   with empty_as_interval show ?thesis by auto

  1114 next

  1115   case False

  1116   show ?thesis

  1117   proof

  1118     assume L: ?lhs

  1119     then have "is_interval S" "compact S" by auto

  1120     define a where "a \<equiv> \<Sum>i\<in>Basis. (INF x:S. x \<bullet> i) *\<^sub>R i"

  1121     define b where "b \<equiv> \<Sum>i\<in>Basis. (SUP x:S. x \<bullet> i) *\<^sub>R i"

  1122     have 1: "\<And>x i. \<lbrakk>x \<in> S; i \<in> Basis\<rbrakk> \<Longrightarrow> (INF x:S. x \<bullet> i) \<le> x \<bullet> i"

  1123       by (simp add: cInf_lower bounded_inner_imp_bdd_below compact_imp_bounded L)

  1124     have 2: "\<And>x i. \<lbrakk>x \<in> S; i \<in> Basis\<rbrakk> \<Longrightarrow> x \<bullet> i \<le> (SUP x:S. x \<bullet> i)"

  1125       by (simp add: cSup_upper bounded_inner_imp_bdd_above compact_imp_bounded L)

  1126     have 3: "x \<in> S" if inf: "\<And>i. i \<in> Basis \<Longrightarrow> (INF x:S. x \<bullet> i) \<le> x \<bullet> i"

  1127                    and sup: "\<And>i. i \<in> Basis \<Longrightarrow> x \<bullet> i \<le> (SUP x:S. x \<bullet> i)" for x

  1128     proof (rule mem_box_componentwiseI [OF \<open>is_interval S\<close>])

  1129       fix i::'a

  1130       assume i: "i \<in> Basis"

  1131       have cont: "continuous_on S (\<lambda>x. x \<bullet> i)"

  1132         by (intro continuous_intros)

  1133       obtain a where "a \<in> S" and a: "\<And>y. y\<in>S \<Longrightarrow> a \<bullet> i \<le> y \<bullet> i"

  1134         using continuous_attains_inf [OF \<open>compact S\<close> False cont] by blast

  1135       obtain b where "b \<in> S" and b: "\<And>y. y\<in>S \<Longrightarrow> y \<bullet> i \<le> b \<bullet> i"

  1136         using continuous_attains_sup [OF \<open>compact S\<close> False cont] by blast

  1137       have "a \<bullet> i \<le> (INF x:S. x \<bullet> i)"

  1138         by (simp add: False a cINF_greatest)

  1139       also have "\<dots> \<le> x \<bullet> i"

  1140         by (simp add: i inf)

  1141       finally have ai: "a \<bullet> i \<le> x \<bullet> i" .

  1142       have "x \<bullet> i \<le> (SUP x:S. x \<bullet> i)"

  1143         by (simp add: i sup)

  1144       also have "(SUP x:S. x \<bullet> i) \<le> b \<bullet> i"

  1145         by (simp add: False b cSUP_least)

  1146       finally have bi: "x \<bullet> i \<le> b \<bullet> i" .

  1147       show "x \<bullet> i \<in> (\<lambda>x. x \<bullet> i)  S"

  1148         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)

  1149         apply (simp add: i)

  1150         apply (rule mem_is_intervalI [OF \<open>is_interval S\<close> \<open>a \<in> S\<close> \<open>b \<in> S\<close>])

  1151         using i ai bi apply force

  1152         done

  1153     qed

  1154     have "S = cbox a b"

  1155       by (auto simp: a_def b_def mem_box intro: 1 2 3)

  1156     then show ?rhs

  1157       by blast

  1158   next

  1159     assume R: ?rhs

  1160     then show ?lhs

  1161       using compact_cbox is_interval_cbox by blast

  1162   qed

  1163 qed

  1164

  1165 subsection\<open>Relations among convergence and absolute convergence for power series.\<close>

  1166

  1167 lemma summable_imp_bounded:

  1168   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"

  1169   shows "summable f \<Longrightarrow> bounded (range f)"

  1170 by (frule summable_LIMSEQ_zero) (simp add: convergent_imp_bounded)

  1171

  1172 lemma summable_imp_sums_bounded:

  1173    "summable f \<Longrightarrow> bounded (range (\<lambda>n. sum f {..<n}))"

  1174 by (auto simp: summable_def sums_def dest: convergent_imp_bounded)

  1175

  1176 lemma power_series_conv_imp_absconv_weak:

  1177   fixes a:: "nat \<Rightarrow> 'a::{real_normed_div_algebra,banach}" and w :: 'a

  1178   assumes sum: "summable (\<lambda>n. a n * z ^ n)" and no: "norm w < norm z"

  1179     shows "summable (\<lambda>n. of_real(norm(a n)) * w ^ n)"

  1180 proof -

  1181   obtain M where M: "\<And>x. norm (a x * z ^ x) \<le> M"

  1182     using summable_imp_bounded [OF sum] by (force simp: bounded_iff)

  1183   then have *: "summable (\<lambda>n. norm (a n) * norm w ^ n)"

  1184     by (rule_tac M=M in Abel_lemma) (auto simp: norm_mult norm_power intro: no)

  1185   show ?thesis

  1186     apply (rule series_comparison_complex [of "(\<lambda>n. of_real(norm(a n) * norm w ^ n))"])

  1187     apply (simp only: summable_complex_of_real *)

  1188     apply (auto simp: norm_mult norm_power)

  1189     done

  1190 qed

  1191

  1192 subsection \<open>Bounded closed nest property (proof does not use Heine-Borel)\<close>

  1193

  1194 lemma bounded_closed_nest:

  1195   fixes s :: "nat \<Rightarrow> ('a::heine_borel) set"

  1196   assumes "\<forall>n. closed (s n)"

  1197     and "\<forall>n. s n \<noteq> {}"

  1198     and "\<forall>m n. m \<le> n \<longrightarrow> s n \<subseteq> s m"

  1199     and "bounded (s 0)"

  1200   shows "\<exists>a. \<forall>n. a \<in> s n"

  1201 proof -

  1202   from assms(2) obtain x where x: "\<forall>n. x n \<in> s n"

  1203     using choice[of "\<lambda>n x. x \<in> s n"] by auto

  1204   from assms(4,1) have "seq_compact (s 0)"

  1205     by (simp add: bounded_closed_imp_seq_compact)

  1206   then obtain l r where lr: "l \<in> s 0" "strict_mono r" "(x \<circ> r) \<longlonglongrightarrow> l"

  1207     using x and assms(3) unfolding seq_compact_def by blast

  1208   have "\<forall>n. l \<in> s n"

  1209   proof

  1210     fix n :: nat

  1211     have "closed (s n)"

  1212       using assms(1) by simp

  1213     moreover have "\<forall>i. (x \<circ> r) i \<in> s i"

  1214       using x and assms(3) and lr(2) [THEN seq_suble] by auto

  1215     then have "\<forall>i. (x \<circ> r) (i + n) \<in> s n"

  1216       using assms(3) by (fast intro!: le_add2)

  1217     moreover have "(\<lambda>i. (x \<circ> r) (i + n)) \<longlonglongrightarrow> l"

  1218       using lr(3) by (rule LIMSEQ_ignore_initial_segment)

  1219     ultimately show "l \<in> s n"

  1220       by (rule closed_sequentially)

  1221   qed

  1222   then show ?thesis ..

  1223 qed

  1224

  1225 text \<open>Decreasing case does not even need compactness, just completeness.\<close>

  1226

  1227 lemma decreasing_closed_nest:

  1228   fixes s :: "nat \<Rightarrow> ('a::complete_space) set"

  1229   assumes

  1230     "\<forall>n. closed (s n)"

  1231     "\<forall>n. s n \<noteq> {}"

  1232     "\<forall>m n. m \<le> n \<longrightarrow> s n \<subseteq> s m"

  1233     "\<forall>e>0. \<exists>n. \<forall>x\<in>s n. \<forall>y\<in>s n. dist x y < e"

  1234   shows "\<exists>a. \<forall>n. a \<in> s n"

  1235 proof -

  1236   have "\<forall>n. \<exists>x. x \<in> s n"

  1237     using assms(2) by auto

  1238   then have "\<exists>t. \<forall>n. t n \<in> s n"

  1239     using choice[of "\<lambda>n x. x \<in> s n"] by auto

  1240   then obtain t where t: "\<forall>n. t n \<in> s n" by auto

  1241   {

  1242     fix e :: real

  1243     assume "e > 0"

  1244     then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e"

  1245       using assms(4) by auto

  1246     {

  1247       fix m n :: nat

  1248       assume "N \<le> m \<and> N \<le> n"

  1249       then have "t m \<in> s N" "t n \<in> s N"

  1250         using assms(3) t unfolding  subset_eq t by blast+

  1251       then have "dist (t m) (t n) < e"

  1252         using N by auto

  1253     }

  1254     then have "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e"

  1255       by auto

  1256   }

  1257   then have "Cauchy t"

  1258     unfolding cauchy_def by auto

  1259   then obtain l where l:"(t \<longlongrightarrow> l) sequentially"

  1260     using complete_UNIV unfolding complete_def by auto

  1261   {

  1262     fix n :: nat

  1263     {

  1264       fix e :: real

  1265       assume "e > 0"

  1266       then obtain N :: nat where N: "\<forall>n\<ge>N. dist (t n) l < e"

  1267         using l[unfolded lim_sequentially] by auto

  1268       have "t (max n N) \<in> s n"

  1269         by (meson assms(3) contra_subsetD max.cobounded1 t)

  1270       then have "\<exists>y\<in>s n. dist y l < e"

  1271         using N max.cobounded2 by blast

  1272     }

  1273     then have "l \<in> s n"

  1274       using closed_approachable[of "s n" l] assms(1) by auto

  1275   }

  1276   then show ?thesis by auto

  1277 qed

  1278

  1279 text \<open>Strengthen it to the intersection actually being a singleton.\<close>

  1280

  1281 lemma decreasing_closed_nest_sing:

  1282   fixes s :: "nat \<Rightarrow> 'a::complete_space set"

  1283   assumes

  1284     "\<forall>n. closed(s n)"

  1285     "\<forall>n. s n \<noteq> {}"

  1286     "\<forall>m n. m \<le> n \<longrightarrow> s n \<subseteq> s m"

  1287     "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"

  1288   shows "\<exists>a. \<Inter>(range s) = {a}"

  1289 proof -

  1290   obtain a where a: "\<forall>n. a \<in> s n"

  1291     using decreasing_closed_nest[of s] using assms by auto

  1292   {

  1293     fix b

  1294     assume b: "b \<in> \<Inter>(range s)"

  1295     {

  1296       fix e :: real

  1297       assume "e > 0"

  1298       then have "dist a b < e"

  1299         using assms(4) and b and a by blast

  1300     }

  1301     then have "dist a b = 0"

  1302       by (metis dist_eq_0_iff dist_nz less_le)

  1303   }

  1304   with a have "\<Inter>(range s) = {a}"

  1305     unfolding image_def by auto

  1306   then show ?thesis ..

  1307 qed

  1308

  1309

  1310 subsection \<open>Infimum Distance\<close>

  1311

  1312 definition "infdist x A = (if A = {} then 0 else INF a:A. dist x a)"

  1313

  1314 lemma bdd_below_infdist[intro, simp]: "bdd_below (dist xA)"

  1315   by (auto intro!: zero_le_dist)

  1316

  1317 lemma infdist_notempty: "A \<noteq> {} \<Longrightarrow> infdist x A = (INF a:A. dist x a)"

  1318   by (simp add: infdist_def)

  1319

  1320 lemma infdist_nonneg: "0 \<le> infdist x A"

  1321   by (auto simp: infdist_def intro: cINF_greatest)

  1322

  1323 lemma infdist_le: "a \<in> A \<Longrightarrow> infdist x A \<le> dist x a"

  1324   by (auto intro: cINF_lower simp add: infdist_def)

  1325

  1326 lemma infdist_le2: "a \<in> A \<Longrightarrow> dist x a \<le> d \<Longrightarrow> infdist x A \<le> d"

  1327   by (auto intro!: cINF_lower2 simp add: infdist_def)

  1328

  1329 lemma infdist_zero[simp]: "a \<in> A \<Longrightarrow> infdist a A = 0"

  1330   by (auto intro!: antisym infdist_nonneg infdist_le2)

  1331

  1332 lemma infdist_triangle: "infdist x A \<le> infdist y A + dist x y"

  1333 proof (cases "A = {}")

  1334   case True

  1335   then show ?thesis by (simp add: infdist_def)

  1336 next

  1337   case False

  1338   then obtain a where "a \<in> A" by auto

  1339   have "infdist x A \<le> Inf {dist x y + dist y a |a. a \<in> A}"

  1340   proof (rule cInf_greatest)

  1341     from \<open>A \<noteq> {}\<close> show "{dist x y + dist y a |a. a \<in> A} \<noteq> {}"

  1342       by simp

  1343     fix d

  1344     assume "d \<in> {dist x y + dist y a |a. a \<in> A}"

  1345     then obtain a where d: "d = dist x y + dist y a" "a \<in> A"

  1346       by auto

  1347     show "infdist x A \<le> d"

  1348       unfolding infdist_notempty[OF \<open>A \<noteq> {}\<close>]

  1349     proof (rule cINF_lower2)

  1350       show "a \<in> A" by fact

  1351       show "dist x a \<le> d"

  1352         unfolding d by (rule dist_triangle)

  1353     qed simp

  1354   qed

  1355   also have "\<dots> = dist x y + infdist y A"

  1356   proof (rule cInf_eq, safe)

  1357     fix a

  1358     assume "a \<in> A"

  1359     then show "dist x y + infdist y A \<le> dist x y + dist y a"

  1360       by (auto intro: infdist_le)

  1361   next

  1362     fix i

  1363     assume inf: "\<And>d. d \<in> {dist x y + dist y a |a. a \<in> A} \<Longrightarrow> i \<le> d"

  1364     then have "i - dist x y \<le> infdist y A"

  1365       unfolding infdist_notempty[OF \<open>A \<noteq> {}\<close>] using \<open>a \<in> A\<close>

  1366       by (intro cINF_greatest) (auto simp: field_simps)

  1367     then show "i \<le> dist x y + infdist y A"

  1368       by simp

  1369   qed

  1370   finally show ?thesis by simp

  1371 qed

  1372

  1373 lemma in_closure_iff_infdist_zero:

  1374   assumes "A \<noteq> {}"

  1375   shows "x \<in> closure A \<longleftrightarrow> infdist x A = 0"

  1376 proof

  1377   assume "x \<in> closure A"

  1378   show "infdist x A = 0"

  1379   proof (rule ccontr)

  1380     assume "infdist x A \<noteq> 0"

  1381     with infdist_nonneg[of x A] have "infdist x A > 0"

  1382       by auto

  1383     then have "ball x (infdist x A) \<inter> closure A = {}"

  1384       apply auto

  1385       apply (metis \<open>x \<in> closure A\<close> closure_approachable dist_commute infdist_le not_less)

  1386       done

  1387     then have "x \<notin> closure A"

  1388       by (metis \<open>0 < infdist x A\<close> centre_in_ball disjoint_iff_not_equal)

  1389     then show False using \<open>x \<in> closure A\<close> by simp

  1390   qed

  1391 next

  1392   assume x: "infdist x A = 0"

  1393   then obtain a where "a \<in> A"

  1394     by atomize_elim (metis all_not_in_conv assms)

  1395   show "x \<in> closure A"

  1396     unfolding closure_approachable

  1397     apply safe

  1398   proof (rule ccontr)

  1399     fix e :: real

  1400     assume "e > 0"

  1401     assume "\<not> (\<exists>y\<in>A. dist y x < e)"

  1402     then have "infdist x A \<ge> e" using \<open>a \<in> A\<close>

  1403       unfolding infdist_def

  1404       by (force simp: dist_commute intro: cINF_greatest)

  1405     with x \<open>e > 0\<close> show False by auto

  1406   qed

  1407 qed

  1408

  1409 lemma in_closed_iff_infdist_zero:

  1410   assumes "closed A" "A \<noteq> {}"

  1411   shows "x \<in> A \<longleftrightarrow> infdist x A = 0"

  1412 proof -

  1413   have "x \<in> closure A \<longleftrightarrow> infdist x A = 0"

  1414     by (rule in_closure_iff_infdist_zero) fact

  1415   with assms show ?thesis by simp

  1416 qed

  1417

  1418 lemma tendsto_infdist [tendsto_intros]:

  1419   assumes f: "(f \<longlongrightarrow> l) F"

  1420   shows "((\<lambda>x. infdist (f x) A) \<longlongrightarrow> infdist l A) F"

  1421 proof (rule tendstoI)

  1422   fix e ::real

  1423   assume "e > 0"

  1424   from tendstoD[OF f this]

  1425   show "eventually (\<lambda>x. dist (infdist (f x) A) (infdist l A) < e) F"

  1426   proof (eventually_elim)

  1427     fix x

  1428     from infdist_triangle[of l A "f x"] infdist_triangle[of "f x" A l]

  1429     have "dist (infdist (f x) A) (infdist l A) \<le> dist (f x) l"

  1430       by (simp add: dist_commute dist_real_def)

  1431     also assume "dist (f x) l < e"

  1432     finally show "dist (infdist (f x) A) (infdist l A) < e" .

  1433   qed

  1434 qed

  1435

  1436 lemma continuous_infdist[continuous_intros]:

  1437   assumes "continuous F f"

  1438   shows "continuous F (\<lambda>x. infdist (f x) A)"

  1439   using assms unfolding continuous_def by (rule tendsto_infdist)

  1440

  1441 subsection \<open>Equality of continuous functions on closure and related results.\<close>

  1442

  1443 lemma continuous_closedin_preimage_constant:

  1444   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  1445   shows "continuous_on s f \<Longrightarrow> closedin (subtopology euclidean s) {x \<in> s. f x = a}"

  1446   using continuous_closedin_preimage[of s f "{a}"] by auto

  1447

  1448 lemma continuous_closed_preimage_constant:

  1449   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  1450   shows "continuous_on s f \<Longrightarrow> closed s \<Longrightarrow> closed {x \<in> s. f x = a}"

  1451   using continuous_closed_preimage[of s f "{a}"] by auto

  1452

  1453 lemma continuous_constant_on_closure:

  1454   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  1455   assumes "continuous_on (closure S) f"

  1456       and "\<And>x. x \<in> S \<Longrightarrow> f x = a"

  1457       and "x \<in> closure S"

  1458   shows "f x = a"

  1459     using continuous_closed_preimage_constant[of "closure S" f a]

  1460       assms closure_minimal[of S "{x \<in> closure S. f x = a}"] closure_subset

  1461     unfolding subset_eq

  1462     by auto

  1463

  1464 lemma image_closure_subset:

  1465   assumes "continuous_on (closure s) f"

  1466     and "closed t"

  1467     and "(f  s) \<subseteq> t"

  1468   shows "f  (closure s) \<subseteq> t"

  1469 proof -

  1470   have "s \<subseteq> {x \<in> closure s. f x \<in> t}"

  1471     using assms(3) closure_subset by auto

  1472   moreover have "closed {x \<in> closure s. f x \<in> t}"

  1473     using continuous_closed_preimage[OF assms(1)] and assms(2) by auto

  1474   ultimately have "closure s = {x \<in> closure s . f x \<in> t}"

  1475     using closure_minimal[of s "{x \<in> closure s. f x \<in> t}"] by auto

  1476   then show ?thesis by auto

  1477 qed

  1478

  1479 lemma continuous_on_closure_norm_le:

  1480   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  1481   assumes "continuous_on (closure s) f"

  1482     and "\<forall>y \<in> s. norm(f y) \<le> b"

  1483     and "x \<in> (closure s)"

  1484   shows "norm (f x) \<le> b"

  1485 proof -

  1486   have *: "f  s \<subseteq> cball 0 b"

  1487     using assms(2)[unfolded mem_cball_0[symmetric]] by auto

  1488   show ?thesis

  1489     by (meson "*" assms(1) assms(3) closed_cball image_closure_subset image_subset_iff mem_cball_0)

  1490 qed

  1491

  1492 lemma isCont_indicator:

  1493   fixes x :: "'a::t2_space"

  1494   shows "isCont (indicator A :: 'a \<Rightarrow> real) x = (x \<notin> frontier A)"

  1495 proof auto

  1496   fix x

  1497   assume cts_at: "isCont (indicator A :: 'a \<Rightarrow> real) x" and fr: "x \<in> frontier A"

  1498   with continuous_at_open have 1: "\<forall>V::real set. open V \<and> indicator A x \<in> V \<longrightarrow>

  1499     (\<exists>U::'a set. open U \<and> x \<in> U \<and> (\<forall>y\<in>U. indicator A y \<in> V))" by auto

  1500   show False

  1501   proof (cases "x \<in> A")

  1502     assume x: "x \<in> A"

  1503     hence "indicator A x \<in> ({0<..<2} :: real set)" by simp

  1504     hence "\<exists>U. open U \<and> x \<in> U \<and> (\<forall>y\<in>U. indicator A y \<in> ({0<..<2} :: real set))"

  1505       using 1 open_greaterThanLessThan by blast

  1506     then guess U .. note U = this

  1507     hence "\<forall>y\<in>U. indicator A y > (0::real)"

  1508       unfolding greaterThanLessThan_def by auto

  1509     hence "U \<subseteq> A" using indicator_eq_0_iff by force

  1510     hence "x \<in> interior A" using U interiorI by auto

  1511     thus ?thesis using fr unfolding frontier_def by simp

  1512   next

  1513     assume x: "x \<notin> A"

  1514     hence "indicator A x \<in> ({-1<..<1} :: real set)" by simp

  1515     hence "\<exists>U. open U \<and> x \<in> U \<and> (\<forall>y\<in>U. indicator A y \<in> ({-1<..<1} :: real set))"

  1516       using 1 open_greaterThanLessThan by blast

  1517     then guess U .. note U = this

  1518     hence "\<forall>y\<in>U. indicator A y < (1::real)"

  1519       unfolding greaterThanLessThan_def by auto

  1520     hence "U \<subseteq> -A" by auto

  1521     hence "x \<in> interior (-A)" using U interiorI by auto

  1522     thus ?thesis using fr interior_complement unfolding frontier_def by auto

  1523   qed

  1524 next

  1525   assume nfr: "x \<notin> frontier A"

  1526   hence "x \<in> interior A \<or> x \<in> interior (-A)"

  1527     by (auto simp: frontier_def closure_interior)

  1528   thus "isCont ((indicator A)::'a \<Rightarrow> real) x"

  1529   proof

  1530     assume int: "x \<in> interior A"

  1531     then obtain U where U: "open U" "x \<in> U" "U \<subseteq> A" unfolding interior_def by auto

  1532     hence "\<forall>y\<in>U. indicator A y = (1::real)" unfolding indicator_def by auto

  1533     hence "continuous_on U (indicator A)" by (simp add: continuous_on_const indicator_eq_1_iff)

  1534     thus ?thesis using U continuous_on_eq_continuous_at by auto

  1535   next

  1536     assume ext: "x \<in> interior (-A)"

  1537     then obtain U where U: "open U" "x \<in> U" "U \<subseteq> -A" unfolding interior_def by auto

  1538     then have "continuous_on U (indicator A)"

  1539       using continuous_on_topological by (auto simp: subset_iff)

  1540     thus ?thesis using U continuous_on_eq_continuous_at by auto

  1541   qed

  1542 qed

  1543

  1544 subsection \<open>A function constant on a set\<close>

  1545

  1546 definition constant_on  (infixl "(constant'_on)" 50)

  1547   where "f constant_on A \<equiv> \<exists>y. \<forall>x\<in>A. f x = y"

  1548

  1549 lemma constant_on_subset: "\<lbrakk>f constant_on A; B \<subseteq> A\<rbrakk> \<Longrightarrow> f constant_on B"

  1550   unfolding constant_on_def by blast

  1551

  1552 lemma injective_not_constant:

  1553   fixes S :: "'a::{perfect_space} set"

  1554   shows "\<lbrakk>open S; inj_on f S; f constant_on S\<rbrakk> \<Longrightarrow> S = {}"

  1555 unfolding constant_on_def

  1556 by (metis equals0I inj_on_contraD islimpt_UNIV islimpt_def)

  1557

  1558 lemma constant_on_closureI:

  1559   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  1560   assumes cof: "f constant_on S" and contf: "continuous_on (closure S) f"

  1561     shows "f constant_on (closure S)"

  1562 using continuous_constant_on_closure [OF contf] cof unfolding constant_on_def

  1563 by metis

  1564

  1565 subsection\<open>Relating linear images to open/closed/interior/closure\<close>

  1566

  1567 proposition open_surjective_linear_image:

  1568   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::euclidean_space"

  1569   assumes "open A" "linear f" "surj f"

  1570     shows "open(f  A)"

  1571 unfolding open_dist

  1572 proof clarify

  1573   fix x

  1574   assume "x \<in> A"

  1575   have "bounded (inv f  Basis)"

  1576     by (simp add: finite_imp_bounded)

  1577   with bounded_pos obtain B where "B > 0" and B: "\<And>x. x \<in> inv f  Basis \<Longrightarrow> norm x \<le> B"

  1578     by metis

  1579   obtain e where "e > 0" and e: "\<And>z. dist z x < e \<Longrightarrow> z \<in> A"

  1580     by (metis open_dist \<open>x \<in> A\<close> \<open>open A\<close>)

  1581   define \<delta> where "\<delta> \<equiv> e / B / DIM('b)"

  1582   show "\<exists>e>0. \<forall>y. dist y (f x) < e \<longrightarrow> y \<in> f  A"

  1583   proof (intro exI conjI)

  1584     show "\<delta> > 0"

  1585       using \<open>e > 0\<close> \<open>B > 0\<close>  by (simp add: \<delta>_def divide_simps)

  1586     have "y \<in> f  A" if "dist y (f x) * (B * real DIM('b)) < e" for y

  1587     proof -

  1588       define u where "u \<equiv> y - f x"

  1589       show ?thesis

  1590       proof (rule image_eqI)

  1591         show "y = f (x + (\<Sum>i\<in>Basis. (u \<bullet> i) *\<^sub>R inv f i))"

  1592           apply (simp add: linear_add linear_sum linear.scaleR \<open>linear f\<close> surj_f_inv_f \<open>surj f\<close>)

  1593           apply (simp add: euclidean_representation u_def)

  1594           done

  1595         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))"

  1596           by (simp add: dist_norm sum_norm_le)

  1597         also have "... = (\<Sum>i\<in>Basis. \<bar>u \<bullet> i\<bar> * norm (inv f i))"

  1598           by simp

  1599         also have "... \<le> (\<Sum>i\<in>Basis. \<bar>u \<bullet> i\<bar>) * B"

  1600           by (simp add: B sum_distrib_right sum_mono mult_left_mono)

  1601         also have "... \<le> DIM('b) * dist y (f x) * B"

  1602           apply (rule mult_right_mono [OF sum_bounded_above])

  1603           using \<open>0 < B\<close> by (auto simp: Basis_le_norm dist_norm u_def)

  1604         also have "... < e"

  1605           by (metis mult.commute mult.left_commute that)

  1606         finally show "x + (\<Sum>i\<in>Basis. (u \<bullet> i) *\<^sub>R inv f i) \<in> A"

  1607           by (rule e)

  1608       qed

  1609     qed

  1610     then show "\<forall>y. dist y (f x) < \<delta> \<longrightarrow> y \<in> f  A"

  1611       using \<open>e > 0\<close> \<open>B > 0\<close>

  1612       by (auto simp: \<delta>_def divide_simps mult_less_0_iff)

  1613   qed

  1614 qed

  1615

  1616 corollary open_bijective_linear_image_eq:

  1617   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"

  1618   assumes "linear f" "bij f"

  1619     shows "open(f  A) \<longleftrightarrow> open A"

  1620 proof

  1621   assume "open(f  A)"

  1622   then have "open(f - (f  A))"

  1623     using assms by (force simp: linear_continuous_at linear_conv_bounded_linear continuous_open_vimage)

  1624   then show "open A"

  1625     by (simp add: assms bij_is_inj inj_vimage_image_eq)

  1626 next

  1627   assume "open A"

  1628   then show "open(f  A)"

  1629     by (simp add: assms bij_is_surj open_surjective_linear_image)

  1630 qed

  1631

  1632 corollary interior_bijective_linear_image:

  1633   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"

  1634   assumes "linear f" "bij f"

  1635   shows "interior (f  S) = f  interior S"  (is "?lhs = ?rhs")

  1636 proof safe

  1637   fix x

  1638   assume x: "x \<in> ?lhs"

  1639   then obtain T where "open T" and "x \<in> T" and "T \<subseteq> f  S"

  1640     by (metis interiorE)

  1641   then show "x \<in> ?rhs"

  1642     by (metis (no_types, hide_lams) assms subsetD interior_maximal open_bijective_linear_image_eq subset_image_iff)

  1643 next

  1644   fix x

  1645   assume x: "x \<in> interior S"

  1646   then show "f x \<in> interior (f  S)"

  1647     by (meson assms imageI image_mono interiorI interior_subset open_bijective_linear_image_eq open_interior)

  1648 qed

  1649

  1650 lemma interior_injective_linear_image:

  1651   fixes f :: "'a::euclidean_space \<Rightarrow> 'a::euclidean_space"

  1652   assumes "linear f" "inj f"

  1653    shows "interior(f  S) = f  (interior S)"

  1654   by (simp add: linear_injective_imp_surjective assms bijI interior_bijective_linear_image)

  1655

  1656 lemma interior_surjective_linear_image:

  1657   fixes f :: "'a::euclidean_space \<Rightarrow> 'a::euclidean_space"

  1658   assumes "linear f" "surj f"

  1659    shows "interior(f  S) = f  (interior S)"

  1660   by (simp add: assms interior_injective_linear_image linear_surjective_imp_injective)

  1661

  1662 lemma interior_negations:

  1663   fixes S :: "'a::euclidean_space set"

  1664   shows "interior(uminus  S) = image uminus (interior S)"

  1665   by (simp add: bij_uminus interior_bijective_linear_image linear_uminus)

  1666

  1667 text \<open>Preservation of compactness and connectedness under continuous function.\<close>

  1668

  1669 lemma compact_eq_openin_cover:

  1670   "compact S \<longleftrightarrow>

  1671     (\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>

  1672       (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D))"

  1673 proof safe

  1674   fix C

  1675   assume "compact S" and "\<forall>c\<in>C. openin (subtopology euclidean S) c" and "S \<subseteq> \<Union>C"

  1676   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}"

  1677     unfolding openin_open by force+

  1678   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"

  1679     by (meson compactE)

  1680   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)"

  1681     by auto

  1682   then show "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..

  1683 next

  1684   assume 1: "\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>

  1685         (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D)"

  1686   show "compact S"

  1687   proof (rule compactI)

  1688     fix C

  1689     let ?C = "image (\<lambda>T. S \<inter> T) C"

  1690     assume "\<forall>t\<in>C. open t" and "S \<subseteq> \<Union>C"

  1691     then have "(\<forall>c\<in>?C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>?C"

  1692       unfolding openin_open by auto

  1693     with 1 obtain D where "D \<subseteq> ?C" and "finite D" and "S \<subseteq> \<Union>D"

  1694       by metis

  1695     let ?D = "inv_into C (\<lambda>T. S \<inter> T)  D"

  1696     have "?D \<subseteq> C \<and> finite ?D \<and> S \<subseteq> \<Union>?D"

  1697     proof (intro conjI)

  1698       from \<open>D \<subseteq> ?C\<close> show "?D \<subseteq> C"

  1699         by (fast intro: inv_into_into)

  1700       from \<open>finite D\<close> show "finite ?D"

  1701         by (rule finite_imageI)

  1702       from \<open>S \<subseteq> \<Union>D\<close> show "S \<subseteq> \<Union>?D"

  1703         apply (rule subset_trans, clarsimp)

  1704         apply (frule subsetD [OF \<open>D \<subseteq> ?C\<close>, THEN f_inv_into_f])

  1705         apply (erule rev_bexI, fast)

  1706         done

  1707     qed

  1708     then show "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..

  1709   qed

  1710 qed

  1711

  1712 subsection\<open> Theorems relating continuity and uniform continuity to closures\<close>

  1713

  1714 lemma continuous_on_closure:

  1715    "continuous_on (closure S) f \<longleftrightarrow>

  1716     (\<forall>x e. x \<in> closure S \<and> 0 < e

  1717            \<longrightarrow> (\<exists>d. 0 < d \<and> (\<forall>y. y \<in> S \<and> dist y x < d \<longrightarrow> dist (f y) (f x) < e)))"

  1718    (is "?lhs = ?rhs")

  1719 proof

  1720   assume ?lhs then show ?rhs

  1721     unfolding continuous_on_iff  by (metis Un_iff closure_def)

  1722 next

  1723   assume R [rule_format]: ?rhs

  1724   show ?lhs

  1725   proof

  1726     fix x and e::real

  1727     assume "0 < e" and x: "x \<in> closure S"

  1728     obtain \<delta>::real where "\<delta> > 0"

  1729                    and \<delta>: "\<And>y. \<lbrakk>y \<in> S; dist y x < \<delta>\<rbrakk> \<Longrightarrow> dist (f y) (f x) < e/2"

  1730       using R [of x "e/2"] \<open>0 < e\<close> x by auto

  1731     have "dist (f y) (f x) \<le> e" if y: "y \<in> closure S" and dyx: "dist y x < \<delta>/2" for y

  1732     proof -

  1733       obtain \<delta>'::real where "\<delta>' > 0"

  1734                       and \<delta>': "\<And>z. \<lbrakk>z \<in> S; dist z y < \<delta>'\<rbrakk> \<Longrightarrow> dist (f z) (f y) < e/2"

  1735         using R [of y "e/2"] \<open>0 < e\<close> y by auto

  1736       obtain z where "z \<in> S" and z: "dist z y < min \<delta>' \<delta> / 2"

  1737         using closure_approachable y

  1738         by (metis \<open>0 < \<delta>'\<close> \<open>0 < \<delta>\<close> divide_pos_pos min_less_iff_conj zero_less_numeral)

  1739       have "dist (f z) (f y) < e/2"

  1740         apply (rule \<delta>' [OF \<open>z \<in> S\<close>])

  1741         using z \<open>0 < \<delta>'\<close> by linarith

  1742       moreover have "dist (f z) (f x) < e/2"

  1743         apply (rule \<delta> [OF \<open>z \<in> S\<close>])

  1744         using z \<open>0 < \<delta>\<close>  dist_commute[of y z] dist_triangle_half_r [of y] dyx by auto

  1745       ultimately show ?thesis

  1746         by (metis dist_commute dist_triangle_half_l less_imp_le)

  1747     qed

  1748     then show "\<exists>d>0. \<forall>x'\<in>closure S. dist x' x < d \<longrightarrow> dist (f x') (f x) \<le> e"

  1749       by (rule_tac x="\<delta>/2" in exI) (simp add: \<open>\<delta> > 0\<close>)

  1750   qed

  1751 qed

  1752

  1753 lemma continuous_on_closure_sequentially:

  1754   fixes f :: "'a::metric_space \<Rightarrow> 'b :: metric_space"

  1755   shows

  1756    "continuous_on (closure S) f \<longleftrightarrow>

  1757     (\<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)"

  1758    (is "?lhs = ?rhs")

  1759 proof -

  1760   have "continuous_on (closure S) f \<longleftrightarrow>

  1761            (\<forall>x \<in> closure S. continuous (at x within S) f)"

  1762     by (force simp: continuous_on_closure continuous_within_eps_delta)

  1763   also have "... = ?rhs"

  1764     by (force simp: continuous_within_sequentially)

  1765   finally show ?thesis .

  1766 qed

  1767

  1768 lemma uniformly_continuous_on_closure:

  1769   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"

  1770   assumes ucont: "uniformly_continuous_on S f"

  1771       and cont: "continuous_on (closure S) f"

  1772     shows "uniformly_continuous_on (closure S) f"

  1773 unfolding uniformly_continuous_on_def

  1774 proof (intro allI impI)

  1775   fix e::real

  1776   assume "0 < e"

  1777   then obtain d::real

  1778     where "d>0"

  1779       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"

  1780     using ucont [unfolded uniformly_continuous_on_def, rule_format, of "e/3"] by auto

  1781   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"

  1782   proof (rule exI [where x="d/3"], clarsimp simp: \<open>d > 0\<close>)

  1783     fix x y

  1784     assume x: "x \<in> closure S" and y: "y \<in> closure S" and dyx: "dist y x * 3 < d"

  1785     obtain d1::real where "d1 > 0"

  1786            and d1: "\<And>w. \<lbrakk>w \<in> closure S; dist w x < d1\<rbrakk> \<Longrightarrow> dist (f w) (f x) < e/3"

  1787       using cont [unfolded continuous_on_iff, rule_format, of "x" "e/3"] \<open>0 < e\<close> x by auto

  1788      obtain x' where "x' \<in> S" and x': "dist x' x < min d1 (d / 3)"

  1789         using closure_approachable [of x S]

  1790         by (metis \<open>0 < d1\<close> \<open>0 < d\<close> divide_pos_pos min_less_iff_conj x zero_less_numeral)

  1791     obtain d2::real where "d2 > 0"

  1792            and d2: "\<forall>w \<in> closure S. dist w y < d2 \<longrightarrow> dist (f w) (f y) < e/3"

  1793       using cont [unfolded continuous_on_iff, rule_format, of "y" "e/3"] \<open>0 < e\<close> y by auto

  1794      obtain y' where "y' \<in> S" and y': "dist y' y < min d2 (d / 3)"

  1795         using closure_approachable [of y S]

  1796         by (metis \<open>0 < d2\<close> \<open>0 < d\<close> divide_pos_pos min_less_iff_conj y zero_less_numeral)

  1797      have "dist x' x < d/3" using x' by auto

  1798      moreover have "dist x y < d/3"

  1799        by (metis dist_commute dyx less_divide_eq_numeral1(1))

  1800      moreover have "dist y y' < d/3"

  1801        by (metis (no_types) dist_commute min_less_iff_conj y')

  1802      ultimately have "dist x' y' < d/3 + d/3 + d/3"

  1803        by (meson dist_commute_lessI dist_triangle_lt add_strict_mono)

  1804      then have "dist x' y' < d" by simp

  1805      then have "dist (f x') (f y') < e/3"

  1806        by (rule d [OF \<open>y' \<in> S\<close> \<open>x' \<in> S\<close>])

  1807      moreover have "dist (f x') (f x) < e/3" using \<open>x' \<in> S\<close> closure_subset x' d1

  1808        by (simp add: closure_def)

  1809      moreover have "dist (f y') (f y) < e/3" using \<open>y' \<in> S\<close> closure_subset y' d2

  1810        by (simp add: closure_def)

  1811      ultimately have "dist (f y) (f x) < e/3 + e/3 + e/3"

  1812        by (meson dist_commute_lessI dist_triangle_lt add_strict_mono)

  1813     then show "dist (f y) (f x) < e" by simp

  1814   qed

  1815 qed

  1816

  1817 lemma uniformly_continuous_on_extension_at_closure:

  1818   fixes f::"'a::metric_space \<Rightarrow> 'b::complete_space"

  1819   assumes uc: "uniformly_continuous_on X f"

  1820   assumes "x \<in> closure X"

  1821   obtains l where "(f \<longlongrightarrow> l) (at x within X)"

  1822 proof -

  1823   from assms obtain xs where xs: "xs \<longlonglongrightarrow> x" "\<And>n. xs n \<in> X"

  1824     by (auto simp: closure_sequential)

  1825

  1826   from uniformly_continuous_on_Cauchy[OF uc LIMSEQ_imp_Cauchy, OF xs]

  1827   obtain l where l: "(\<lambda>n. f (xs n)) \<longlonglongrightarrow> l"

  1828     by atomize_elim (simp only: convergent_eq_Cauchy)

  1829

  1830   have "(f \<longlongrightarrow> l) (at x within X)"

  1831   proof (safe intro!: Lim_within_LIMSEQ)

  1832     fix xs'

  1833     assume "\<forall>n. xs' n \<noteq> x \<and> xs' n \<in> X"

  1834       and xs': "xs' \<longlonglongrightarrow> x"

  1835     then have "xs' n \<noteq> x" "xs' n \<in> X" for n by auto

  1836

  1837     from uniformly_continuous_on_Cauchy[OF uc LIMSEQ_imp_Cauchy, OF \<open>xs' \<longlonglongrightarrow> x\<close> \<open>xs' _ \<in> X\<close>]

  1838     obtain l' where l': "(\<lambda>n. f (xs' n)) \<longlonglongrightarrow> l'"

  1839       by atomize_elim (simp only: convergent_eq_Cauchy)

  1840

  1841     show "(\<lambda>n. f (xs' n)) \<longlonglongrightarrow> l"

  1842     proof (rule tendstoI)

  1843       fix e::real assume "e > 0"

  1844       define e' where "e' \<equiv> e / 2"

  1845       have "e' > 0" using \<open>e > 0\<close> by (simp add: e'_def)

  1846

  1847       have "\<forall>\<^sub>F n in sequentially. dist (f (xs n)) l < e'"

  1848         by (simp add: \<open>0 < e'\<close> l tendstoD)

  1849       moreover

  1850       from uc[unfolded uniformly_continuous_on_def, rule_format, OF \<open>e' > 0\<close>]

  1851       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'"

  1852         by auto

  1853       have "\<forall>\<^sub>F n in sequentially. dist (xs n) (xs' n) < d"

  1854         by (auto intro!: \<open>0 < d\<close> order_tendstoD tendsto_eq_intros xs xs')

  1855       ultimately

  1856       show "\<forall>\<^sub>F n in sequentially. dist (f (xs' n)) l < e"

  1857       proof eventually_elim

  1858         case (elim n)

  1859         have "dist (f (xs' n)) l \<le> dist (f (xs n)) (f (xs' n)) + dist (f (xs n)) l"

  1860           by (metis dist_triangle dist_commute)

  1861         also have "dist (f (xs n)) (f (xs' n)) < e'"

  1862           by (auto intro!: d xs \<open>xs' _ \<in> _\<close> elim)

  1863         also note \<open>dist (f (xs n)) l < e'\<close>

  1864         also have "e' + e' = e" by (simp add: e'_def)

  1865         finally show ?case by simp

  1866       qed

  1867     qed

  1868   qed

  1869   thus ?thesis ..

  1870 qed

  1871

  1872 lemma uniformly_continuous_on_extension_on_closure:

  1873   fixes f::"'a::metric_space \<Rightarrow> 'b::complete_space"

  1874   assumes uc: "uniformly_continuous_on X f"

  1875   obtains g where "uniformly_continuous_on (closure X) g" "\<And>x. x \<in> X \<Longrightarrow> f x = g x"

  1876     "\<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"

  1877 proof -

  1878   from uc have cont_f: "continuous_on X f"

  1879     by (simp add: uniformly_continuous_imp_continuous)

  1880   obtain y where y: "(f \<longlongrightarrow> y x) (at x within X)" if "x \<in> closure X" for x

  1881     apply atomize_elim

  1882     apply (rule choice)

  1883     using uniformly_continuous_on_extension_at_closure[OF assms]

  1884     by metis

  1885   let ?g = "\<lambda>x. if x \<in> X then f x else y x"

  1886

  1887   have "uniformly_continuous_on (closure X) ?g"

  1888     unfolding uniformly_continuous_on_def

  1889   proof safe

  1890     fix e::real assume "e > 0"

  1891     define e' where "e' \<equiv> e / 3"

  1892     have "e' > 0" using \<open>e > 0\<close> by (simp add: e'_def)

  1893     from uc[unfolded uniformly_continuous_on_def, rule_format, OF \<open>0 < e'\<close>]

  1894     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'"

  1895       by auto

  1896     define d' where "d' = d / 3"

  1897     have "d' > 0" using \<open>d > 0\<close> by (simp add: d'_def)

  1898     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"

  1899     proof (safe intro!: exI[where x=d'] \<open>d' > 0\<close>)

  1900       fix x x' assume x: "x \<in> closure X" and x': "x' \<in> closure X" and dist: "dist x' x < d'"

  1901       then obtain xs xs' where xs: "xs \<longlonglongrightarrow> x" "\<And>n. xs n \<in> X"

  1902         and xs': "xs' \<longlonglongrightarrow> x'" "\<And>n. xs' n \<in> X"

  1903         by (auto simp: closure_sequential)

  1904       have "\<forall>\<^sub>F n in sequentially. dist (xs' n) x' < d'"

  1905         and "\<forall>\<^sub>F n in sequentially. dist (xs n) x < d'"

  1906         by (auto intro!: \<open>0 < d'\<close> order_tendstoD tendsto_eq_intros xs xs')

  1907       moreover

  1908       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

  1909         using that not_eventuallyD

  1910         by (force intro!: filterlim_compose[OF y[OF \<open>x \<in> closure X\<close>]] simp: filterlim_at)

  1911       then have "(\<lambda>x. f (xs' x)) \<longlonglongrightarrow> ?g x'" "(\<lambda>x. f (xs x)) \<longlonglongrightarrow> ?g x"

  1912         using x x'

  1913         by (auto intro!: continuous_on_tendsto_compose[OF cont_f] simp: xs' xs)

  1914       then have "\<forall>\<^sub>F n in sequentially. dist (f (xs' n)) (?g x') < e'"

  1915         "\<forall>\<^sub>F n in sequentially. dist (f (xs n)) (?g x) < e'"

  1916         by (auto intro!: \<open>0 < e'\<close> order_tendstoD tendsto_eq_intros)

  1917       ultimately

  1918       have "\<forall>\<^sub>F n in sequentially. dist (?g x') (?g x) < e"

  1919       proof eventually_elim

  1920         case (elim n)

  1921         have "dist (?g x') (?g x) \<le>

  1922           dist (f (xs' n)) (?g x') + dist (f (xs' n)) (f (xs n)) + dist (f (xs n)) (?g x)"

  1923           by (metis add.commute add_le_cancel_left dist_commute dist_triangle dist_triangle_le)

  1924         also

  1925         {

  1926           have "dist (xs' n) (xs n) \<le> dist (xs' n) x' + dist x' x + dist (xs n) x"

  1927             by (metis add.commute add_le_cancel_left  dist_triangle dist_triangle_le)

  1928           also note \<open>dist (xs' n) x' < d'\<close>

  1929           also note \<open>dist x' x < d'\<close>

  1930           also note \<open>dist (xs n) x < d'\<close>

  1931           finally have "dist (xs' n) (xs n) < d" by (simp add: d'_def)

  1932         }

  1933         with \<open>xs _ \<in> X\<close> \<open>xs' _ \<in> X\<close> have "dist (f (xs' n)) (f (xs n)) < e'"

  1934           by (rule d)

  1935         also note \<open>dist (f (xs' n)) (?g x') < e'\<close>

  1936         also note \<open>dist (f (xs n)) (?g x) < e'\<close>

  1937         finally show ?case by (simp add: e'_def)

  1938       qed

  1939       then show "dist (?g x') (?g x) < e" by simp

  1940     qed

  1941   qed

  1942   moreover have "f x = ?g x" if "x \<in> X" for x using that by simp

  1943   moreover

  1944   {

  1945     fix Y h x

  1946     assume Y: "x \<in> Y" "X \<subseteq> Y" "Y \<subseteq> closure X" and cont_h: "continuous_on Y h"

  1947       and extension: "(\<And>x. x \<in> X \<Longrightarrow> f x = h x)"

  1948     {

  1949       assume "x \<notin> X"

  1950       have "x \<in> closure X" using Y by auto

  1951       then obtain xs where xs: "xs \<longlonglongrightarrow> x" "\<And>n. xs n \<in> X"

  1952         by (auto simp: closure_sequential)

  1953       from continuous_on_tendsto_compose[OF cont_h xs(1)] xs(2) Y

  1954       have hx: "(\<lambda>x. f (xs x)) \<longlonglongrightarrow> h x"

  1955         by (auto simp: set_mp extension)

  1956       then have "(\<lambda>x. f (xs x)) \<longlonglongrightarrow> y x"

  1957         using \<open>x \<notin> X\<close> not_eventuallyD xs(2)

  1958         by (force intro!: filterlim_compose[OF y[OF \<open>x \<in> closure X\<close>]] simp: filterlim_at xs)

  1959       with hx have "h x = y x" by (rule LIMSEQ_unique)

  1960     } then

  1961     have "h x = ?g x"

  1962       using extension by auto

  1963   }

  1964   ultimately show ?thesis ..

  1965 qed

  1966

  1967 lemma bounded_uniformly_continuous_image:

  1968   fixes f :: "'a :: heine_borel \<Rightarrow> 'b :: heine_borel"

  1969   assumes "uniformly_continuous_on S f" "bounded S"

  1970   shows "bounded(image f S)"

  1971   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)

  1972

  1973 subsection \<open>Making a continuous function avoid some value in a neighbourhood.\<close>

  1974

  1975 lemma continuous_within_avoid:

  1976   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  1977   assumes "continuous (at x within s) f"

  1978     and "f x \<noteq> a"

  1979   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"

  1980 proof -

  1981   obtain U where "open U" and "f x \<in> U" and "a \<notin> U"

  1982     using t1_space [OF \<open>f x \<noteq> a\<close>] by fast

  1983   have "(f \<longlongrightarrow> f x) (at x within s)"

  1984     using assms(1) by (simp add: continuous_within)

  1985   then have "eventually (\<lambda>y. f y \<in> U) (at x within s)"

  1986     using \<open>open U\<close> and \<open>f x \<in> U\<close>

  1987     unfolding tendsto_def by fast

  1988   then have "eventually (\<lambda>y. f y \<noteq> a) (at x within s)"

  1989     using \<open>a \<notin> U\<close> by (fast elim: eventually_mono)

  1990   then show ?thesis

  1991     using \<open>f x \<noteq> a\<close> by (auto simp: dist_commute zero_less_dist_iff eventually_at)

  1992 qed

  1993

  1994 lemma continuous_at_avoid:

  1995   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  1996   assumes "continuous (at x) f"

  1997     and "f x \<noteq> a"

  1998   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"

  1999   using assms continuous_within_avoid[of x UNIV f a] by simp

  2000

  2001 lemma continuous_on_avoid:

  2002   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  2003   assumes "continuous_on s f"

  2004     and "x \<in> s"

  2005     and "f x \<noteq> a"

  2006   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"

  2007   using assms(1)[unfolded continuous_on_eq_continuous_within, THEN bspec[where x=x],

  2008     OF assms(2)] continuous_within_avoid[of x s f a]

  2009   using assms(3)

  2010   by auto

  2011

  2012 lemma continuous_on_open_avoid:

  2013   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  2014   assumes "continuous_on s f"

  2015     and "open s"

  2016     and "x \<in> s"

  2017     and "f x \<noteq> a"

  2018   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"

  2019   using assms(1)[unfolded continuous_on_eq_continuous_at[OF assms(2)], THEN bspec[where x=x], OF assms(3)]

  2020   using continuous_at_avoid[of x f a] assms(4)

  2021   by auto

  2022

  2023 subsection\<open>Quotient maps\<close>

  2024

  2025 lemma quotient_map_imp_continuous_open:

  2026   assumes t: "f  s \<subseteq> t"

  2027       and ope: "\<And>u. u \<subseteq> t

  2028               \<Longrightarrow> (openin (subtopology euclidean s) {x. x \<in> s \<and> f x \<in> u} \<longleftrightarrow>

  2029                    openin (subtopology euclidean t) u)"

  2030     shows "continuous_on s f"

  2031 proof -

  2032   have [simp]: "{x \<in> s. f x \<in> f  s} = s" by auto

  2033   show ?thesis

  2034     using ope [OF t]

  2035     apply (simp add: continuous_on_open)

  2036     by (metis (no_types, lifting) "ope"  openin_imp_subset openin_trans)

  2037 qed

  2038

  2039 lemma quotient_map_imp_continuous_closed:

  2040   assumes t: "f  s \<subseteq> t"

  2041       and ope: "\<And>u. u \<subseteq> t

  2042                   \<Longrightarrow> (closedin (subtopology euclidean s) {x. x \<in> s \<and> f x \<in> u} \<longleftrightarrow>

  2043                        closedin (subtopology euclidean t) u)"

  2044     shows "continuous_on s f"

  2045 proof -

  2046   have [simp]: "{x \<in> s. f x \<in> f  s} = s" by auto

  2047   show ?thesis

  2048     using ope [OF t]

  2049     apply (simp add: continuous_on_closed)

  2050     by (metis (no_types, lifting) "ope" closedin_imp_subset closedin_subtopology_refl closedin_trans openin_subtopology_refl openin_subtopology_self)

  2051 qed

  2052

  2053 lemma open_map_imp_quotient_map:

  2054   assumes contf: "continuous_on s f"

  2055       and t: "t \<subseteq> f  s"

  2056       and ope: "\<And>t. openin (subtopology euclidean s) t

  2057                    \<Longrightarrow> openin (subtopology euclidean (f  s)) (f  t)"

  2058     shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t} =

  2059            openin (subtopology euclidean (f  s)) t"

  2060 proof -

  2061   have "t = image f {x. x \<in> s \<and> f x \<in> t}"

  2062     using t by blast

  2063   then show ?thesis

  2064     using "ope" contf continuous_on_open by fastforce

  2065 qed

  2066

  2067 lemma closed_map_imp_quotient_map:

  2068   assumes contf: "continuous_on s f"

  2069       and t: "t \<subseteq> f  s"

  2070       and ope: "\<And>t. closedin (subtopology euclidean s) t

  2071               \<Longrightarrow> closedin (subtopology euclidean (f  s)) (f  t)"

  2072     shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t} \<longleftrightarrow>

  2073            openin (subtopology euclidean (f  s)) t"

  2074           (is "?lhs = ?rhs")

  2075 proof

  2076   assume ?lhs

  2077   then have *: "closedin (subtopology euclidean s) (s - {x \<in> s. f x \<in> t})"

  2078     using closedin_diff by fastforce

  2079   have [simp]: "(f  s - f  (s - {x \<in> s. f x \<in> t})) = t"

  2080     using t by blast

  2081   show ?rhs

  2082     using ope [OF *, unfolded closedin_def] by auto

  2083 next

  2084   assume ?rhs

  2085   with contf show ?lhs

  2086     by (auto simp: continuous_on_open)

  2087 qed

  2088

  2089 lemma continuous_right_inverse_imp_quotient_map:

  2090   assumes contf: "continuous_on s f" and imf: "f  s \<subseteq> t"

  2091       and contg: "continuous_on t g" and img: "g  t \<subseteq> s"

  2092       and fg [simp]: "\<And>y. y \<in> t \<Longrightarrow> f(g y) = y"

  2093       and u: "u \<subseteq> t"

  2094     shows "openin (subtopology euclidean s) {x. x \<in> s \<and> f x \<in> u} \<longleftrightarrow>

  2095            openin (subtopology euclidean t) u"

  2096           (is "?lhs = ?rhs")

  2097 proof -

  2098   have f: "\<And>z. openin (subtopology euclidean (f  s)) z \<Longrightarrow>

  2099                 openin (subtopology euclidean s) {x \<in> s. f x \<in> z}"

  2100   and  g: "\<And>z. openin (subtopology euclidean (g  t)) z \<Longrightarrow>

  2101                 openin (subtopology euclidean t) {x \<in> t. g x \<in> z}"

  2102     using contf contg by (auto simp: continuous_on_open)

  2103   show ?thesis

  2104   proof

  2105     have "{x \<in> t. g x \<in> g  t \<and> g x \<in> s \<and> f (g x) \<in> u} = {x \<in> t. f (g x) \<in> u}"

  2106       using imf img by blast

  2107     also have "... = u"

  2108       using u by auto

  2109     finally have [simp]: "{x \<in> t. g x \<in> g  t \<and> g x \<in> s \<and> f (g x) \<in> u} = u" .

  2110     assume ?lhs

  2111     then have *: "openin (subtopology euclidean (g  t)) (g  t \<inter> {x \<in> s. f x \<in> u})"

  2112       by (meson img openin_Int openin_subtopology_Int_subset openin_subtopology_self)

  2113     show ?rhs

  2114       using g [OF *] by simp

  2115   next

  2116     assume rhs: ?rhs

  2117     show ?lhs

  2118       apply (rule f)

  2119       by (metis fg image_eqI image_subset_iff imf img openin_subopen openin_subtopology_self openin_trans rhs)

  2120   qed

  2121 qed

  2122

  2123 lemma continuous_left_inverse_imp_quotient_map:

  2124   assumes "continuous_on s f"

  2125       and "continuous_on (f  s) g"

  2126       and  "\<And>x. x \<in> s \<Longrightarrow> g(f x) = x"

  2127       and "u \<subseteq> f  s"

  2128     shows "openin (subtopology euclidean s) {x. x \<in> s \<and> f x \<in> u} \<longleftrightarrow>

  2129            openin (subtopology euclidean (f  s)) u"

  2130 apply (rule continuous_right_inverse_imp_quotient_map)

  2131 using assms

  2132 apply force+

  2133 done

  2134

  2135 text \<open>Proving a function is constant by proving that a level set is open\<close>

  2136

  2137 lemma continuous_levelset_openin_cases:

  2138   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  2139   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>

  2140         openin (subtopology euclidean s) {x \<in> s. f x = a}

  2141         \<Longrightarrow> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"

  2142   unfolding connected_clopen

  2143   using continuous_closedin_preimage_constant by auto

  2144

  2145 lemma continuous_levelset_openin:

  2146   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  2147   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>

  2148         openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>

  2149         (\<exists>x \<in> s. f x = a)  \<Longrightarrow> (\<forall>x \<in> s. f x = a)"

  2150   using continuous_levelset_openin_cases[of s f ]

  2151   by meson

  2152

  2153 lemma continuous_levelset_open:

  2154   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  2155   assumes "connected s"

  2156     and "continuous_on s f"

  2157     and "open {x \<in> s. f x = a}"

  2158     and "\<exists>x \<in> s.  f x = a"

  2159   shows "\<forall>x \<in> s. f x = a"

  2160   using continuous_levelset_openin[OF assms(1,2), of a, unfolded openin_open]

  2161   using assms (3,4)

  2162   by fast

  2163

  2164 text \<open>Some arithmetical combinations (more to prove).\<close>

  2165

  2166 lemma open_scaling[intro]:

  2167   fixes s :: "'a::real_normed_vector set"

  2168   assumes "c \<noteq> 0"

  2169     and "open s"

  2170   shows "open((\<lambda>x. c *\<^sub>R x)  s)"

  2171 proof -

  2172   {

  2173     fix x

  2174     assume "x \<in> s"

  2175     then obtain e where "e>0"

  2176       and e:"\<forall>x'. dist x' x < e \<longrightarrow> x' \<in> s" using assms(2)[unfolded open_dist, THEN bspec[where x=x]]

  2177       by auto

  2178     have "e * \<bar>c\<bar> > 0"

  2179       using assms(1)[unfolded zero_less_abs_iff[symmetric]] \<open>e>0\<close> by auto

  2180     moreover

  2181     {

  2182       fix y

  2183       assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"

  2184       then have "norm ((1 / c) *\<^sub>R y - x) < e"

  2185         unfolding dist_norm

  2186         using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)

  2187           assms(1)[unfolded zero_less_abs_iff[symmetric]] by (simp del:zero_less_abs_iff)

  2188       then have "y \<in> op *\<^sub>R c  s"

  2189         using rev_image_eqI[of "(1 / c) *\<^sub>R y" s y "op *\<^sub>R c"]

  2190         using e[THEN spec[where x="(1 / c) *\<^sub>R y"]]

  2191         using assms(1)

  2192         unfolding dist_norm scaleR_scaleR

  2193         by auto

  2194     }

  2195     ultimately have "\<exists>e>0. \<forall>x'. dist x' (c *\<^sub>R x) < e \<longrightarrow> x' \<in> op *\<^sub>R c  s"

  2196       apply (rule_tac x="e * \<bar>c\<bar>" in exI, auto)

  2197       done

  2198   }

  2199   then show ?thesis unfolding open_dist by auto

  2200 qed

  2201

  2202 lemma minus_image_eq_vimage:

  2203   fixes A :: "'a::ab_group_add set"

  2204   shows "(\<lambda>x. - x)  A = (\<lambda>x. - x) - A"

  2205   by (auto intro!: image_eqI [where f="\<lambda>x. - x"])

  2206

  2207 lemma open_negations:

  2208   fixes S :: "'a::real_normed_vector set"

  2209   shows "open S \<Longrightarrow> open ((\<lambda>x. - x)  S)"

  2210   using open_scaling [of "- 1" S] by simp

  2211

  2212 lemma open_translation:

  2213   fixes S :: "'a::real_normed_vector set"

  2214   assumes "open S"

  2215   shows "open((\<lambda>x. a + x)  S)"

  2216 proof -

  2217   {

  2218     fix x

  2219     have "continuous (at x) (\<lambda>x. x - a)"

  2220       by (intro continuous_diff continuous_ident continuous_const)

  2221   }

  2222   moreover have "{x. x - a \<in> S} = op + a  S"

  2223     by force

  2224   ultimately show ?thesis

  2225     by (metis assms continuous_open_vimage vimage_def)

  2226 qed

  2227

  2228 lemma open_affinity:

  2229   fixes S :: "'a::real_normed_vector set"

  2230   assumes "open S"  "c \<noteq> 0"

  2231   shows "open ((\<lambda>x. a + c *\<^sub>R x)  S)"

  2232 proof -

  2233   have *: "(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)"

  2234     unfolding o_def ..

  2235   have "op + a  op *\<^sub>R c  S = (op + a \<circ> op *\<^sub>R c)  S"

  2236     by auto

  2237   then show ?thesis

  2238     using assms open_translation[of "op *\<^sub>R c  S" a]

  2239     unfolding *

  2240     by auto

  2241 qed

  2242

  2243 lemma interior_translation:

  2244   fixes S :: "'a::real_normed_vector set"

  2245   shows "interior ((\<lambda>x. a + x)  S) = (\<lambda>x. a + x)  (interior S)"

  2246 proof (rule set_eqI, rule)

  2247   fix x

  2248   assume "x \<in> interior (op + a  S)"

  2249   then obtain e where "e > 0" and e: "ball x e \<subseteq> op + a  S"

  2250     unfolding mem_interior by auto

  2251   then have "ball (x - a) e \<subseteq> S"

  2252     unfolding subset_eq Ball_def mem_ball dist_norm

  2253     by (auto simp: diff_diff_eq)

  2254   then show "x \<in> op + a  interior S"

  2255     unfolding image_iff

  2256     apply (rule_tac x="x - a" in bexI)

  2257     unfolding mem_interior

  2258     using \<open>e > 0\<close>

  2259     apply auto

  2260     done

  2261 next

  2262   fix x

  2263   assume "x \<in> op + a  interior S"

  2264   then obtain y e where "e > 0" and e: "ball y e \<subseteq> S" and y: "x = a + y"

  2265     unfolding image_iff Bex_def mem_interior by auto

  2266   {

  2267     fix z

  2268     have *: "a + y - z = y + a - z" by auto

  2269     assume "z \<in> ball x e"

  2270     then have "z - a \<in> S"

  2271       using e[unfolded subset_eq, THEN bspec[where x="z - a"]]

  2272       unfolding mem_ball dist_norm y group_add_class.diff_diff_eq2 *

  2273       by auto

  2274     then have "z \<in> op + a  S"

  2275       unfolding image_iff by (auto intro!: bexI[where x="z - a"])

  2276   }

  2277   then have "ball x e \<subseteq> op + a  S"

  2278     unfolding subset_eq by auto

  2279   then show "x \<in> interior (op + a  S)"

  2280     unfolding mem_interior using \<open>e > 0\<close> by auto

  2281 qed

  2282

  2283 subsection \<open>Continuity implies uniform continuity on a compact domain.\<close>

  2284

  2285 text\<open>From the proof of the Heine-Borel theorem: Lemma 2 in section 3.7, page 69 of

  2286 J. C. Burkill and H. Burkill. A Second Course in Mathematical Analysis (CUP, 2002)\<close>

  2287

  2288 lemma Heine_Borel_lemma:

  2289   assumes "compact S" and Ssub: "S \<subseteq> \<Union>\<G>" and op: "\<And>G. G \<in> \<G> \<Longrightarrow> open G"

  2290   obtains e where "0 < e" "\<And>x. x \<in> S \<Longrightarrow> \<exists>G \<in> \<G>. ball x e \<subseteq> G"

  2291 proof -

  2292   have False if neg: "\<And>e. 0 < e \<Longrightarrow> \<exists>x \<in> S. \<forall>G \<in> \<G>. \<not> ball x e \<subseteq> G"

  2293   proof -

  2294     have "\<exists>x \<in> S. \<forall>G \<in> \<G>. \<not> ball x (1 / Suc n) \<subseteq> G" for n

  2295       using neg by simp

  2296     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"

  2297       by metis

  2298     then obtain l r where "l \<in> S" "strict_mono r" and to_l: "(f \<circ> r) \<longlonglongrightarrow> l"

  2299       using \<open>compact S\<close> compact_def that by metis

  2300     then obtain G where "l \<in> G" "G \<in> \<G>"

  2301       using Ssub by auto

  2302     then obtain e where "0 < e" and e: "\<And>z. dist z l < e \<Longrightarrow> z \<in> G"

  2303       using op open_dist by blast

  2304     obtain N1 where N1: "\<And>n. n \<ge> N1 \<Longrightarrow> dist (f (r n)) l < e/2"

  2305       using to_l apply (simp add: lim_sequentially)

  2306       using \<open>0 < e\<close> half_gt_zero that by blast

  2307     obtain N2 where N2: "of_nat N2 > 2/e"

  2308       using reals_Archimedean2 by blast

  2309     obtain x where "x \<in> ball (f (r (max N1 N2))) (1 / real (Suc (r (max N1 N2))))" and "x \<notin> G"

  2310       using fG [OF \<open>G \<in> \<G>\<close>, of "r (max N1 N2)"] by blast

  2311     then have "dist (f (r (max N1 N2))) x < 1 / real (Suc (r (max N1 N2)))"

  2312       by simp

  2313     also have "... \<le> 1 / real (Suc (max N1 N2))"

  2314       apply (simp add: divide_simps del: max.bounded_iff)

  2315       using \<open>strict_mono r\<close> seq_suble by blast

  2316     also have "... \<le> 1 / real (Suc N2)"

  2317       by (simp add: field_simps)

  2318     also have "... < e/2"

  2319       using N2 \<open>0 < e\<close> by (simp add: field_simps)

  2320     finally have "dist (f (r (max N1 N2))) x < e / 2" .

  2321     moreover have "dist (f (r (max N1 N2))) l < e/2"

  2322       using N1 max.cobounded1 by blast

  2323     ultimately have "dist x l < e"

  2324       using dist_triangle_half_r by blast

  2325     then show ?thesis

  2326       using e \<open>x \<notin> G\<close> by blast

  2327   qed

  2328   then show ?thesis

  2329     by (meson that)

  2330 qed

  2331

  2332 lemma compact_uniformly_equicontinuous:

  2333   assumes "compact S"

  2334       and cont: "\<And>x e. \<lbrakk>x \<in> S; 0 < e\<rbrakk>

  2335                         \<Longrightarrow> \<exists>d. 0 < d \<and>

  2336                                 (\<forall>f \<in> \<F>. \<forall>x' \<in> S. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"

  2337       and "0 < e"

  2338   obtains d where "0 < d"

  2339                   "\<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"

  2340 proof -

  2341   obtain d where d_pos: "\<And>x e. \<lbrakk>x \<in> S; 0 < e\<rbrakk> \<Longrightarrow> 0 < d x e"

  2342      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"

  2343     using cont by metis

  2344   let ?\<G> = "((\<lambda>x. ball x (d x (e / 2)))  S)"

  2345   have Ssub: "S \<subseteq> \<Union> ?\<G>"

  2346     by clarsimp (metis d_pos \<open>0 < e\<close> dist_self half_gt_zero_iff)

  2347   then obtain k where "0 < k" and k: "\<And>x. x \<in> S \<Longrightarrow> \<exists>G \<in> ?\<G>. ball x k \<subseteq> G"

  2348     by (rule Heine_Borel_lemma [OF \<open>compact S\<close>]) auto

  2349   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

  2350   proof -

  2351     obtain G where "G \<in> ?\<G>" "u \<in> G" "v \<in> G"

  2352       using k that

  2353       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)

  2354     then obtain w where w: "dist w u < d w (e / 2)" "dist w v < d w (e / 2)" "w \<in> S"

  2355       by auto

  2356     with that d_dist have "dist (f w) (f v) < e/2"

  2357       by (metis \<open>0 < e\<close> dist_commute half_gt_zero)

  2358     moreover

  2359     have "dist (f w) (f u) < e/2"

  2360       using that d_dist w by (metis \<open>0 < e\<close> dist_commute divide_pos_pos zero_less_numeral)

  2361     ultimately show ?thesis

  2362       using dist_triangle_half_r by blast

  2363   qed

  2364   ultimately show ?thesis using that by blast

  2365 qed

  2366

  2367 corollary compact_uniformly_continuous:

  2368   fixes f :: "'a :: metric_space \<Rightarrow> 'b :: metric_space"

  2369   assumes f: "continuous_on S f" and S: "compact S"

  2370   shows "uniformly_continuous_on S f"

  2371   using f

  2372     unfolding continuous_on_iff uniformly_continuous_on_def

  2373     by (force intro: compact_uniformly_equicontinuous [OF S, of "{f}"])

  2374

  2375 subsection \<open>Topological stuff about the set of Reals\<close>

  2376

  2377 lemma open_real:

  2378   fixes s :: "real set"

  2379   shows "open s \<longleftrightarrow> (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. \<bar>x' - x\<bar> < e --> x' \<in> s)"

  2380   unfolding open_dist dist_norm by simp

  2381

  2382 lemma islimpt_approachable_real:

  2383   fixes s :: "real set"

  2384   shows "x islimpt s \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> s. x' \<noteq> x \<and> \<bar>x' - x\<bar> < e)"

  2385   unfolding islimpt_approachable dist_norm by simp

  2386

  2387 lemma closed_real:

  2388   fixes s :: "real set"

  2389   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)"

  2390   unfolding closed_limpt islimpt_approachable dist_norm by simp

  2391

  2392 lemma continuous_at_real_range:

  2393   fixes f :: "'a::real_normed_vector \<Rightarrow> real"

  2394   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)"

  2395   unfolding continuous_at

  2396   unfolding Lim_at

  2397   unfolding dist_norm

  2398   apply auto

  2399   apply (erule_tac x=e in allE, auto)

  2400   apply (rule_tac x=d in exI, auto)

  2401   apply (erule_tac x=x' in allE, auto)

  2402   apply (erule_tac x=e in allE, auto)

  2403   done

  2404

  2405 lemma continuous_on_real_range:

  2406   fixes f :: "'a::real_normed_vector \<Rightarrow> real"

  2407   shows "continuous_on s f \<longleftrightarrow>

  2408     (\<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))"

  2409   unfolding continuous_on_iff dist_norm by simp

  2410

  2411 text \<open>Hence some handy theorems on distance, diameter etc. of/from a set.\<close>

  2412

  2413 lemma distance_attains_sup:

  2414   assumes "compact s" "s \<noteq> {}"

  2415   shows "\<exists>x\<in>s. \<forall>y\<in>s. dist a y \<le> dist a x"

  2416 proof (rule continuous_attains_sup [OF assms])

  2417   {

  2418     fix x

  2419     assume "x\<in>s"

  2420     have "(dist a \<longlongrightarrow> dist a x) (at x within s)"

  2421       by (intro tendsto_dist tendsto_const tendsto_ident_at)

  2422   }

  2423   then show "continuous_on s (dist a)"

  2424     unfolding continuous_on ..

  2425 qed

  2426

  2427 text \<open>For \emph{minimal} distance, we only need closure, not compactness.\<close>

  2428

  2429 lemma distance_attains_inf:

  2430   fixes a :: "'a::heine_borel"

  2431   assumes "closed s" and "s \<noteq> {}"

  2432   obtains x where "x\<in>s" "\<And>y. y \<in> s \<Longrightarrow> dist a x \<le> dist a y"

  2433 proof -

  2434   from assms obtain b where "b \<in> s" by auto

  2435   let ?B = "s \<inter> cball a (dist b a)"

  2436   have "?B \<noteq> {}" using \<open>b \<in> s\<close>

  2437     by (auto simp: dist_commute)

  2438   moreover have "continuous_on ?B (dist a)"

  2439     by (auto intro!: continuous_at_imp_continuous_on continuous_dist continuous_ident continuous_const)

  2440   moreover have "compact ?B"

  2441     by (intro closed_Int_compact \<open>closed s\<close> compact_cball)

  2442   ultimately obtain x where "x \<in> ?B" "\<forall>y\<in>?B. dist a x \<le> dist a y"

  2443     by (metis continuous_attains_inf)

  2444   with that show ?thesis by fastforce

  2445 qed

  2446

  2447

  2448 subsection \<open>Cartesian products\<close>

  2449

  2450 lemma bounded_Times:

  2451   assumes "bounded s" "bounded t"

  2452   shows "bounded (s \<times> t)"

  2453 proof -

  2454   obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"

  2455     using assms [unfolded bounded_def] by auto

  2456   then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<^sup>2 + b\<^sup>2)"

  2457     by (auto simp: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)

  2458   then show ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto

  2459 qed

  2460

  2461 lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"

  2462   by (induct x) simp

  2463

  2464 lemma seq_compact_Times: "seq_compact s \<Longrightarrow> seq_compact t \<Longrightarrow> seq_compact (s \<times> t)"

  2465   unfolding seq_compact_def

  2466   apply clarify

  2467   apply (drule_tac x="fst \<circ> f" in spec)

  2468   apply (drule mp, simp add: mem_Times_iff)

  2469   apply (clarify, rename_tac l1 r1)

  2470   apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)

  2471   apply (drule mp, simp add: mem_Times_iff)

  2472   apply (clarify, rename_tac l2 r2)

  2473   apply (rule_tac x="(l1, l2)" in rev_bexI, simp)

  2474   apply (rule_tac x="r1 \<circ> r2" in exI)

  2475   apply (rule conjI, simp add: strict_mono_def)

  2476   apply (drule_tac f=r2 in LIMSEQ_subseq_LIMSEQ, assumption)

  2477   apply (drule (1) tendsto_Pair) back

  2478   apply (simp add: o_def)

  2479   done

  2480

  2481 lemma compact_Times:

  2482   assumes "compact s" "compact t"

  2483   shows "compact (s \<times> t)"

  2484 proof (rule compactI)

  2485   fix C

  2486   assume C: "\<forall>t\<in>C. open t" "s \<times> t \<subseteq> \<Union>C"

  2487   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)"

  2488   proof

  2489     fix x

  2490     assume "x \<in> s"

  2491     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")

  2492     proof

  2493       fix y

  2494       assume "y \<in> t"

  2495       with \<open>x \<in> s\<close> C obtain c where "c \<in> C" "(x, y) \<in> c" "open c" by auto

  2496       then show "?P y" by (auto elim!: open_prod_elim)

  2497     qed

  2498     then obtain a b c where b: "\<And>y. y \<in> t \<Longrightarrow> open (b y)"

  2499       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"

  2500       by metis

  2501     then have "\<forall>y\<in>t. open (b y)" "t \<subseteq> (\<Union>y\<in>t. b y)" by auto

  2502     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)"

  2503       by metis

  2504     moreover from D c have "(\<Inter>y\<in>D. a y) \<times> t \<subseteq> (\<Union>y\<in>D. c y)"

  2505       by (fastforce simp: subset_eq)

  2506     ultimately show "\<exists>a. open a \<and> x \<in> a \<and> (\<exists>d\<subseteq>C. finite d \<and> a \<times> t \<subseteq> \<Union>d)"

  2507       using c by (intro exI[of _ "cD"] exI[of _ "\<Inter>(aD)"] conjI) (auto intro!: open_INT)

  2508   qed

  2509   then obtain a d where a: "\<And>x. x\<in>s \<Longrightarrow> open (a x)" "s \<subseteq> (\<Union>x\<in>s. a x)"

  2510     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"

  2511     unfolding subset_eq UN_iff by metis

  2512   moreover

  2513   from compactE_image[OF \<open>compact s\<close> a]

  2514   obtain e where e: "e \<subseteq> s" "finite e" and s: "s \<subseteq> (\<Union>x\<in>e. a x)"

  2515     by auto

  2516   moreover

  2517   {

  2518     from s have "s \<times> t \<subseteq> (\<Union>x\<in>e. a x \<times> t)"

  2519       by auto

  2520     also have "\<dots> \<subseteq> (\<Union>x\<in>e. \<Union>d x)"

  2521       using d \<open>e \<subseteq> s\<close> by (intro UN_mono) auto

  2522     finally have "s \<times> t \<subseteq> (\<Union>x\<in>e. \<Union>d x)" .

  2523   }

  2524   ultimately show "\<exists>C'\<subseteq>C. finite C' \<and> s \<times> t \<subseteq> \<Union>C'"

  2525     by (intro exI[of _ "(\<Union>x\<in>e. d x)"]) (auto simp: subset_eq)

  2526 qed

  2527

  2528 text\<open>Hence some useful properties follow quite easily.\<close>

  2529

  2530 lemma compact_scaling:

  2531   fixes s :: "'a::real_normed_vector set"

  2532   assumes "compact s"

  2533   shows "compact ((\<lambda>x. c *\<^sub>R x)  s)"

  2534 proof -

  2535   let ?f = "\<lambda>x. scaleR c x"

  2536   have *: "bounded_linear ?f" by (rule bounded_linear_scaleR_right)

  2537   show ?thesis

  2538     using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]

  2539     using linear_continuous_at[OF *] assms

  2540     by auto

  2541 qed

  2542

  2543 lemma compact_negations:

  2544   fixes s :: "'a::real_normed_vector set"

  2545   assumes "compact s"

  2546   shows "compact ((\<lambda>x. - x)  s)"

  2547   using compact_scaling [OF assms, of "- 1"] by auto

  2548

  2549 lemma compact_sums:

  2550   fixes s t :: "'a::real_normed_vector set"

  2551   assumes "compact s"

  2552     and "compact t"

  2553   shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"

  2554 proof -

  2555   have *: "{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z)  (s \<times> t)"

  2556     apply auto

  2557     unfolding image_iff

  2558     apply (rule_tac x="(xa, y)" in bexI)

  2559     apply auto

  2560     done

  2561   have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"

  2562     unfolding continuous_on by (rule ballI) (intro tendsto_intros)

  2563   then show ?thesis

  2564     unfolding * using compact_continuous_image compact_Times [OF assms] by auto

  2565 qed

  2566

  2567 lemma compact_differences:

  2568   fixes s t :: "'a::real_normed_vector set"

  2569   assumes "compact s"

  2570     and "compact t"

  2571   shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"

  2572 proof-

  2573   have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus  t)}"

  2574     apply auto

  2575     apply (rule_tac x= xa in exI, auto)

  2576     done

  2577   then show ?thesis

  2578     using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto

  2579 qed

  2580

  2581 lemma compact_translation:

  2582   fixes s :: "'a::real_normed_vector set"

  2583   assumes "compact s"

  2584   shows "compact ((\<lambda>x. a + x)  s)"

  2585 proof -

  2586   have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x)  s"

  2587     by auto

  2588   then show ?thesis

  2589     using compact_sums[OF assms compact_sing[of a]] by auto

  2590 qed

  2591

  2592 lemma compact_affinity:

  2593   fixes s :: "'a::real_normed_vector set"

  2594   assumes "compact s"

  2595   shows "compact ((\<lambda>x. a + c *\<^sub>R x)  s)"

  2596 proof -

  2597   have "op + a  op *\<^sub>R c  s = (\<lambda>x. a + c *\<^sub>R x)  s"

  2598     by auto

  2599   then show ?thesis

  2600     using compact_translation[OF compact_scaling[OF assms], of a c] by auto

  2601 qed

  2602

  2603 text \<open>Hence we get the following.\<close>

  2604

  2605 lemma compact_sup_maxdistance:

  2606   fixes s :: "'a::metric_space set"

  2607   assumes "compact s"

  2608     and "s \<noteq> {}"

  2609   shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. dist u v \<le> dist x y"

  2610 proof -

  2611   have "compact (s \<times> s)"

  2612     using \<open>compact s\<close> by (intro compact_Times)

  2613   moreover have "s \<times> s \<noteq> {}"

  2614     using \<open>s \<noteq> {}\<close> by auto

  2615   moreover have "continuous_on (s \<times> s) (\<lambda>x. dist (fst x) (snd x))"

  2616     by (intro continuous_at_imp_continuous_on ballI continuous_intros)

  2617   ultimately show ?thesis

  2618     using continuous_attains_sup[of "s \<times> s" "\<lambda>x. dist (fst x) (snd x)"] by auto

  2619 qed

  2620

  2621

  2622 subsection \<open>The diameter of a set.\<close>

  2623

  2624 definition diameter :: "'a::metric_space set \<Rightarrow> real" where

  2625   "diameter S = (if S = {} then 0 else SUP (x,y):S\<times>S. dist x y)"

  2626

  2627 lemma diameter_empty [simp]: "diameter{} = 0"

  2628   by (auto simp: diameter_def)

  2629

  2630 lemma diameter_singleton [simp]: "diameter{x} = 0"

  2631   by (auto simp: diameter_def)

  2632

  2633 lemma diameter_le:

  2634   assumes "S \<noteq> {} \<or> 0 \<le> d"

  2635       and no: "\<And>x y. \<lbrakk>x \<in> S; y \<in> S\<rbrakk> \<Longrightarrow> norm(x - y) \<le> d"

  2636     shows "diameter S \<le> d"

  2637 using assms

  2638   by (auto simp: dist_norm diameter_def intro: cSUP_least)

  2639

  2640 lemma diameter_bounded_bound:

  2641   fixes s :: "'a :: metric_space set"

  2642   assumes s: "bounded s" "x \<in> s" "y \<in> s"

  2643   shows "dist x y \<le> diameter s"

  2644 proof -

  2645   from s obtain z d where z: "\<And>x. x \<in> s \<Longrightarrow> dist z x \<le> d"

  2646     unfolding bounded_def by auto

  2647   have "bdd_above (case_prod dist  (s\<times>s))"

  2648   proof (intro bdd_aboveI, safe)

  2649     fix a b

  2650     assume "a \<in> s" "b \<in> s"

  2651     with z[of a] z[of b] dist_triangle[of a b z]

  2652     show "dist a b \<le> 2 * d"

  2653       by (simp add: dist_commute)

  2654   qed

  2655   moreover have "(x,y) \<in> s\<times>s" using s by auto

  2656   ultimately have "dist x y \<le> (SUP (x,y):s\<times>s. dist x y)"

  2657     by (rule cSUP_upper2) simp

  2658   with \<open>x \<in> s\<close> show ?thesis

  2659     by (auto simp: diameter_def)

  2660 qed

  2661

  2662 lemma diameter_lower_bounded:

  2663   fixes s :: "'a :: metric_space set"

  2664   assumes s: "bounded s"

  2665     and d: "0 < d" "d < diameter s"

  2666   shows "\<exists>x\<in>s. \<exists>y\<in>s. d < dist x y"

  2667 proof (rule ccontr)

  2668   assume contr: "\<not> ?thesis"

  2669   moreover have "s \<noteq> {}"

  2670     using d by (auto simp: diameter_def)

  2671   ultimately have "diameter s \<le> d"

  2672     by (auto simp: not_less diameter_def intro!: cSUP_least)

  2673   with \<open>d < diameter s\<close> show False by auto

  2674 qed

  2675

  2676 lemma diameter_bounded:

  2677   assumes "bounded s"

  2678   shows "\<forall>x\<in>s. \<forall>y\<in>s. dist x y \<le> diameter s"

  2679     and "\<forall>d>0. d < diameter s \<longrightarrow> (\<exists>x\<in>s. \<exists>y\<in>s. dist x y > d)"

  2680   using diameter_bounded_bound[of s] diameter_lower_bounded[of s] assms

  2681   by auto

  2682

  2683 lemma diameter_compact_attained:

  2684   assumes "compact s"

  2685     and "s \<noteq> {}"

  2686   shows "\<exists>x\<in>s. \<exists>y\<in>s. dist x y = diameter s"

  2687 proof -

  2688   have b: "bounded s" using assms(1)

  2689     by (rule compact_imp_bounded)

  2690   then obtain x y where xys: "x\<in>s" "y\<in>s"

  2691     and xy: "\<forall>u\<in>s. \<forall>v\<in>s. dist u v \<le> dist x y"

  2692     using compact_sup_maxdistance[OF assms] by auto

  2693   then have "diameter s \<le> dist x y"

  2694     unfolding diameter_def

  2695     apply clarsimp

  2696     apply (rule cSUP_least, fast+)

  2697     done

  2698   then show ?thesis

  2699     by (metis b diameter_bounded_bound order_antisym xys)

  2700 qed

  2701

  2702 lemma diameter_ge_0:

  2703   assumes "bounded S"  shows "0 \<le> diameter S"

  2704   by (metis all_not_in_conv assms diameter_bounded_bound diameter_empty dist_self order_refl)

  2705

  2706 lemma diameter_subset:

  2707   assumes "S \<subseteq> T" "bounded T"

  2708   shows "diameter S \<le> diameter T"

  2709 proof (cases "S = {} \<or> T = {}")

  2710   case True

  2711   with assms show ?thesis

  2712     by (force simp: diameter_ge_0)

  2713 next

  2714   case False

  2715   then have "bdd_above ((\<lambda>x. case x of (x, xa) \<Rightarrow> dist x xa)  (T \<times> T))"

  2716     using \<open>bounded T\<close> diameter_bounded_bound by (force simp: bdd_above_def)

  2717   with False \<open>S \<subseteq> T\<close> show ?thesis

  2718     apply (simp add: diameter_def)

  2719     apply (rule cSUP_subset_mono, auto)

  2720     done

  2721 qed

  2722

  2723 lemma diameter_closure:

  2724   assumes "bounded S"

  2725   shows "diameter(closure S) = diameter S"

  2726 proof (rule order_antisym)

  2727   have "False" if "diameter S < diameter (closure S)"

  2728   proof -

  2729     define d where "d = diameter(closure S) - diameter(S)"

  2730     have "d > 0"

  2731       using that by (simp add: d_def)

  2732     then have "diameter(closure(S)) - d / 2 < diameter(closure(S))"

  2733       by simp

  2734     have dd: "diameter (closure S) - d / 2 = (diameter(closure(S)) + diameter(S)) / 2"

  2735       by (simp add: d_def divide_simps)

  2736      have bocl: "bounded (closure S)"

  2737       using assms by blast

  2738     moreover have "0 \<le> diameter S"

  2739       using assms diameter_ge_0 by blast

  2740     ultimately obtain x y where "x \<in> closure S" "y \<in> closure S" and xy: "diameter(closure(S)) - d / 2 < dist x y"

  2741       using diameter_bounded(2) [OF bocl, rule_format, of "diameter(closure(S)) - d / 2"] \<open>d > 0\<close> d_def by auto

  2742     then obtain x' y' where x'y': "x' \<in> S" "dist x' x < d/4" "y' \<in> S" "dist y' y < d/4"

  2743       using closure_approachable

  2744       by (metis \<open>0 < d\<close> zero_less_divide_iff zero_less_numeral)

  2745     then have "dist x' y' \<le> diameter S"

  2746       using assms diameter_bounded_bound by blast

  2747     with x'y' have "dist x y \<le> d / 4 + diameter S + d / 4"

  2748       by (meson add_mono_thms_linordered_semiring(1) dist_triangle dist_triangle3 less_eq_real_def order_trans)

  2749     then show ?thesis

  2750       using xy d_def by linarith

  2751   qed

  2752   then show "diameter (closure S) \<le> diameter S"

  2753     by fastforce

  2754   next

  2755     show "diameter S \<le> diameter (closure S)"

  2756       by (simp add: assms bounded_closure closure_subset diameter_subset)

  2757 qed

  2758

  2759 lemma diameter_cball [simp]:

  2760   fixes a :: "'a::euclidean_space"

  2761   shows "diameter(cball a r) = (if r < 0 then 0 else 2*r)"

  2762 proof -

  2763   have "diameter(cball a r) = 2*r" if "r \<ge> 0"

  2764   proof (rule order_antisym)

  2765     show "diameter (cball a r) \<le> 2*r"

  2766     proof (rule diameter_le)

  2767       fix x y assume "x \<in> cball a r" "y \<in> cball a r"

  2768       then have "norm (x - a) \<le> r" "norm (a - y) \<le> r"

  2769         by (auto simp: dist_norm norm_minus_commute)

  2770       then have "norm (x - y) \<le> r+r"

  2771         using norm_diff_triangle_le by blast

  2772       then show "norm (x - y) \<le> 2*r" by simp

  2773     qed (simp add: that)

  2774     have "2*r = dist (a + r *\<^sub>R (SOME i. i \<in> Basis)) (a - r *\<^sub>R (SOME i. i \<in> Basis))"

  2775       apply (simp add: dist_norm)

  2776       by (metis abs_of_nonneg mult.right_neutral norm_numeral norm_scaleR norm_some_Basis real_norm_def scaleR_2 that)

  2777     also have "... \<le> diameter (cball a r)"

  2778       apply (rule diameter_bounded_bound)

  2779       using that by (auto simp: dist_norm)

  2780     finally show "2*r \<le> diameter (cball a r)" .

  2781   qed

  2782   then show ?thesis by simp

  2783 qed

  2784

  2785 lemma diameter_ball [simp]:

  2786   fixes a :: "'a::euclidean_space"

  2787   shows "diameter(ball a r) = (if r < 0 then 0 else 2*r)"

  2788 proof -

  2789   have "diameter(ball a r) = 2*r" if "r > 0"

  2790     by (metis bounded_ball diameter_closure closure_ball diameter_cball less_eq_real_def linorder_not_less that)

  2791   then show ?thesis

  2792     by (simp add: diameter_def)

  2793 qed

  2794

  2795 lemma diameter_closed_interval [simp]: "diameter {a..b} = (if b < a then 0 else b-a)"

  2796 proof -

  2797   have "{a .. b} = cball ((a+b)/2) ((b-a)/2)"

  2798     by (auto simp: dist_norm abs_if divide_simps split: if_split_asm)

  2799   then show ?thesis

  2800     by simp

  2801 qed

  2802

  2803 lemma diameter_open_interval [simp]: "diameter {a<..<b} = (if b < a then 0 else b-a)"

  2804 proof -

  2805   have "{a <..< b} = ball ((a+b)/2) ((b-a)/2)"

  2806     by (auto simp: dist_norm abs_if divide_simps split: if_split_asm)

  2807   then show ?thesis

  2808     by simp

  2809 qed

  2810

  2811 proposition Lebesgue_number_lemma:

  2812   assumes "compact S" "\<C> \<noteq> {}" "S \<subseteq> \<Union>\<C>" and ope: "\<And>B. B \<in> \<C> \<Longrightarrow> open B"

  2813   obtains \<delta> where "0 < \<delta>" "\<And>T. \<lbrakk>T \<subseteq> S; diameter T < \<delta>\<rbrakk> \<Longrightarrow> \<exists>B \<in> \<C>. T \<subseteq> B"

  2814 proof (cases "S = {}")

  2815   case True

  2816   then show ?thesis

  2817     by (metis \<open>\<C> \<noteq> {}\<close> zero_less_one empty_subsetI equals0I subset_trans that)

  2818 next

  2819   case False

  2820   { fix x assume "x \<in> S"

  2821     then obtain C where C: "x \<in> C" "C \<in> \<C>"

  2822       using \<open>S \<subseteq> \<Union>\<C>\<close> by blast

  2823     then obtain r where r: "r>0" "ball x (2*r) \<subseteq> C"

  2824       by (metis mult.commute mult_2_right not_le ope openE real_sum_of_halves zero_le_numeral zero_less_mult_iff)

  2825     then have "\<exists>r C. r > 0 \<and> ball x (2*r) \<subseteq> C \<and> C \<in> \<C>"

  2826       using C by blast

  2827   }

  2828   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)"

  2829     by metis

  2830   then have "S \<subseteq> (\<Union>x \<in> S. ball x (r x))"

  2831     by auto

  2832   then obtain \<T> where "finite \<T>" "S \<subseteq> \<Union>\<T>" and \<T>: "\<T> \<subseteq> (\<lambda>x. ball x (r x))  S"

  2833     by (rule compactE [OF \<open>compact S\<close>]) auto

  2834   then obtain S0 where "S0 \<subseteq> S" "finite S0" and S0: "\<T> = (\<lambda>x. ball x (r x))  S0"

  2835     by (meson finite_subset_image)

  2836   then have "S0 \<noteq> {}"

  2837     using False \<open>S \<subseteq> \<Union>\<T>\<close> by auto

  2838   define \<delta> where "\<delta> = Inf (r  S0)"

  2839   have "\<delta> > 0"

  2840     using \<open>finite S0\<close> \<open>S0 \<subseteq> S\<close> \<open>S0 \<noteq> {}\<close> r by (auto simp: \<delta>_def finite_less_Inf_iff)

  2841   show ?thesis

  2842   proof

  2843     show "0 < \<delta>"

  2844       by (simp add: \<open>0 < \<delta>\<close>)

  2845     show "\<exists>B \<in> \<C>. T \<subseteq> B" if "T \<subseteq> S" and dia: "diameter T < \<delta>" for T

  2846     proof (cases "T = {}")

  2847       case True

  2848       then show ?thesis

  2849         using \<open>\<C> \<noteq> {}\<close> by blast

  2850     next

  2851       case False

  2852       then obtain y where "y \<in> T" by blast

  2853       then have "y \<in> S"

  2854         using \<open>T \<subseteq> S\<close> by auto

  2855       then obtain x where "x \<in> S0" and x: "y \<in> ball x (r x)"

  2856         using \<open>S \<subseteq> \<Union>\<T>\<close> S0 that by blast

  2857       have "ball y \<delta> \<subseteq> ball y (r x)"

  2858         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)

  2859       also have "... \<subseteq> ball x (2*r x)"

  2860         by clarsimp (metis dist_commute dist_triangle_less_add mem_ball mult_2 x)

  2861       finally obtain C where "C \<in> \<C>" "ball y \<delta> \<subseteq> C"

  2862         by (meson r \<open>S0 \<subseteq> S\<close> \<open>x \<in> S0\<close> dual_order.trans subsetCE)

  2863       have "bounded T"

  2864         using \<open>compact S\<close> bounded_subset compact_imp_bounded \<open>T \<subseteq> S\<close> by blast

  2865       then have "T \<subseteq> ball y \<delta>"

  2866         using \<open>y \<in> T\<close> dia diameter_bounded_bound by fastforce

  2867       then show ?thesis

  2868         apply (rule_tac x=C in bexI)

  2869         using \<open>ball y \<delta> \<subseteq> C\<close> \<open>C \<in> \<C>\<close> by auto

  2870     qed

  2871   qed

  2872 qed

  2873

  2874 lemma diameter_cbox:

  2875   fixes a b::"'a::euclidean_space"

  2876   shows "(\<forall>i \<in> Basis. a \<bullet> i \<le> b \<bullet> i) \<Longrightarrow> diameter (cbox a b) = dist a b"

  2877   by (force simp: diameter_def intro!: cSup_eq_maximum setL2_mono

  2878      simp: euclidean_dist_l2[where 'a='a] cbox_def dist_norm)

  2879

  2880 subsection \<open>Separation between points and sets\<close>

  2881

  2882 lemma separate_point_closed:

  2883   fixes s :: "'a::heine_borel set"

  2884   assumes "closed s" and "a \<notin> s"

  2885   shows "\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x"

  2886 proof (cases "s = {}")

  2887   case True

  2888   then show ?thesis by(auto intro!: exI[where x=1])

  2889 next

  2890   case False

  2891   from assms obtain x where "x\<in>s" "\<forall>y\<in>s. dist a x \<le> dist a y"

  2892     using \<open>s \<noteq> {}\<close> by (blast intro: distance_attains_inf [of s a])

  2893   with \<open>x\<in>s\<close> show ?thesis using dist_pos_lt[of a x] and\<open>a \<notin> s\<close>

  2894     by blast

  2895 qed

  2896

  2897 lemma separate_compact_closed:

  2898   fixes s t :: "'a::heine_borel set"

  2899   assumes "compact s"

  2900     and t: "closed t" "s \<inter> t = {}"

  2901   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"

  2902 proof cases

  2903   assume "s \<noteq> {} \<and> t \<noteq> {}"

  2904   then have "s \<noteq> {}" "t \<noteq> {}" by auto

  2905   let ?inf = "\<lambda>x. infdist x t"

  2906   have "continuous_on s ?inf"

  2907     by (auto intro!: continuous_at_imp_continuous_on continuous_infdist continuous_ident)

  2908   then obtain x where x: "x \<in> s" "\<forall>y\<in>s. ?inf x \<le> ?inf y"

  2909     using continuous_attains_inf[OF \<open>compact s\<close> \<open>s \<noteq> {}\<close>] by auto

  2910   then have "0 < ?inf x"

  2911     using t \<open>t \<noteq> {}\<close> in_closed_iff_infdist_zero by (auto simp: less_le infdist_nonneg)

  2912   moreover have "\<forall>x'\<in>s. \<forall>y\<in>t. ?inf x \<le> dist x' y"

  2913     using x by (auto intro: order_trans infdist_le)

  2914   ultimately show ?thesis by auto

  2915 qed (auto intro!: exI[of _ 1])

  2916

  2917 lemma separate_closed_compact:

  2918   fixes s t :: "'a::heine_borel set"

  2919   assumes "closed s"

  2920     and "compact t"

  2921     and "s \<inter> t = {}"

  2922   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"

  2923 proof -

  2924   have *: "t \<inter> s = {}"

  2925     using assms(3) by auto

  2926   show ?thesis

  2927     using separate_compact_closed[OF assms(2,1) *] by (force simp: dist_commute)

  2928 qed

  2929

  2930

  2931 subsection \<open>Compact sets and the closure operation.\<close>

  2932

  2933 lemma closed_scaling:

  2934   fixes S :: "'a::real_normed_vector set"

  2935   assumes "closed S"

  2936   shows "closed ((\<lambda>x. c *\<^sub>R x)  S)"

  2937 proof (cases "c = 0")

  2938   case True then show ?thesis

  2939     by (auto simp: image_constant_conv)

  2940 next

  2941   case False

  2942   from assms have "closed ((\<lambda>x. inverse c *\<^sub>R x) - S)"

  2943     by (simp add: continuous_closed_vimage)

  2944   also have "(\<lambda>x. inverse c *\<^sub>R x) - S = (\<lambda>x. c *\<^sub>R x)  S"

  2945     using \<open>c \<noteq> 0\<close> by (auto elim: image_eqI [rotated])

  2946   finally show ?thesis .

  2947 qed

  2948

  2949 lemma closed_negations:

  2950   fixes S :: "'a::real_normed_vector set"

  2951   assumes "closed S"

  2952   shows "closed ((\<lambda>x. -x)  S)"

  2953   using closed_scaling[OF assms, of "- 1"] by simp

  2954

  2955 lemma compact_closed_sums:

  2956   fixes S :: "'a::real_normed_vector set"

  2957   assumes "compact S" and "closed T"

  2958   shows "closed (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"

  2959 proof -

  2960   let ?S = "{x + y |x y. x \<in> S \<and> y \<in> T}"

  2961   {

  2962     fix x l

  2963     assume as: "\<forall>n. x n \<in> ?S"  "(x \<longlongrightarrow> l) sequentially"

  2964     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"

  2965       using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> S \<and> snd y \<in> T"] by auto

  2966     obtain l' r where "l'\<in>S" and r: "strict_mono r" and lr: "(((\<lambda>n. fst (f n)) \<circ> r) \<longlongrightarrow> l') sequentially"

  2967       using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto

  2968     have "((\<lambda>n. snd (f (r n))) \<longlongrightarrow> l - l') sequentially"

  2969       using tendsto_diff[OF LIMSEQ_subseq_LIMSEQ[OF as(2) r] lr] and f(1)

  2970       unfolding o_def

  2971       by auto

  2972     then have "l - l' \<in> T"

  2973       using assms(2)[unfolded closed_sequential_limits,

  2974         THEN spec[where x="\<lambda> n. snd (f (r n))"],

  2975         THEN spec[where x="l - l'"]]

  2976       using f(3)

  2977       by auto

  2978     then have "l \<in> ?S"

  2979       using \<open>l' \<in> S\<close>

  2980       apply auto

  2981       apply (rule_tac x=l' in exI)

  2982       apply (rule_tac x="l - l'" in exI, auto)

  2983       done

  2984   }

  2985   moreover have "?S = (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"

  2986     by force

  2987   ultimately show ?thesis

  2988     unfolding closed_sequential_limits

  2989     by (metis (no_types, lifting))

  2990 qed

  2991

  2992 lemma closed_compact_sums:

  2993   fixes S T :: "'a::real_normed_vector set"

  2994   assumes "closed S" "compact T"

  2995   shows "closed (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"

  2996 proof -

  2997   have "(\<Union>x\<in> T. \<Union>y \<in> S. {x + y}) = (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"

  2998     by auto

  2999   then show ?thesis

  3000     using compact_closed_sums[OF assms(2,1)] by simp

  3001 qed

  3002

  3003 lemma compact_closed_differences:

  3004   fixes S T :: "'a::real_normed_vector set"

  3005   assumes "compact S" "closed T"

  3006   shows "closed (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"

  3007 proof -

  3008   have "(\<Union>x\<in> S. \<Union>y \<in> uminus  T. {x + y}) = (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"

  3009     by force

  3010   then show ?thesis

  3011     using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto

  3012 qed

  3013

  3014 lemma closed_compact_differences:

  3015   fixes S T :: "'a::real_normed_vector set"

  3016   assumes "closed S" "compact T"

  3017   shows "closed (\<Union>x\<in> S. \<Union>y \<in> T. {x - y})"

  3018 proof -

  3019   have "(\<Union>x\<in> S. \<Union>y \<in> uminus  T. {x + y}) = {x - y |x y. x \<in> S \<and> y \<in> T}"

  3020     by auto

  3021  then show ?thesis

  3022   using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp

  3023 qed

  3024

  3025 lemma closed_translation:

  3026   fixes a :: "'a::real_normed_vector"

  3027   assumes "closed S"

  3028   shows "closed ((\<lambda>x. a + x)  S)"

  3029 proof -

  3030   have "(\<Union>x\<in> {a}. \<Union>y \<in> S. {x + y}) = (op + a  S)" by auto

  3031   then show ?thesis

  3032     using compact_closed_sums[OF compact_sing[of a] assms] by auto

  3033 qed

  3034

  3035 lemma translation_Compl:

  3036   fixes a :: "'a::ab_group_add"

  3037   shows "(\<lambda>x. a + x)  (- t) = - ((\<lambda>x. a + x)  t)"

  3038   apply (auto simp: image_iff)

  3039   apply (rule_tac x="x - a" in bexI, auto)

  3040   done

  3041

  3042 lemma translation_UNIV:

  3043   fixes a :: "'a::ab_group_add"

  3044   shows "range (\<lambda>x. a + x) = UNIV"

  3045   apply (auto simp: image_iff)

  3046   apply (rule_tac x="x - a" in exI, auto)

  3047   done

  3048

  3049 lemma translation_diff:

  3050   fixes a :: "'a::ab_group_add"

  3051   shows "(\<lambda>x. a + x)  (s - t) = ((\<lambda>x. a + x)  s) - ((\<lambda>x. a + x)  t)"

  3052   by auto

  3053

  3054 lemma translation_Int:

  3055   fixes a :: "'a::ab_group_add"

  3056   shows "(\<lambda>x. a + x)  (s \<inter> t) = ((\<lambda>x. a + x)  s) \<inter> ((\<lambda>x. a + x)  t)"

  3057   by auto

  3058

  3059 lemma closure_translation:

  3060   fixes a :: "'a::real_normed_vector"

  3061   shows "closure ((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (closure s)"

  3062 proof -

  3063   have *: "op + a  (- s) = - op + a  s"

  3064     apply auto

  3065     unfolding image_iff

  3066     apply (rule_tac x="x - a" in bexI, auto)

  3067     done

  3068   show ?thesis

  3069     unfolding closure_interior translation_Compl

  3070     using interior_translation[of a "- s"]

  3071     unfolding *

  3072     by auto

  3073 qed

  3074

  3075 lemma frontier_translation:

  3076   fixes a :: "'a::real_normed_vector"

  3077   shows "frontier((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (frontier s)"

  3078   unfolding frontier_def translation_diff interior_translation closure_translation

  3079   by auto

  3080

  3081 lemma sphere_translation:

  3082   fixes a :: "'n::euclidean_space"

  3083   shows "sphere (a+c) r = op+a  sphere c r"

  3084 apply safe

  3085 apply (rule_tac x="x-a" in image_eqI)

  3086 apply (auto simp: dist_norm algebra_simps)

  3087 done

  3088

  3089 lemma cball_translation:

  3090   fixes a :: "'n::euclidean_space"

  3091   shows "cball (a+c) r = op+a  cball c r"

  3092 apply safe

  3093 apply (rule_tac x="x-a" in image_eqI)

  3094 apply (auto simp: dist_norm algebra_simps)

  3095 done

  3096

  3097 lemma ball_translation:

  3098   fixes a :: "'n::euclidean_space"

  3099   shows "ball (a+c) r = op+a  ball c r"

  3100 apply safe

  3101 apply (rule_tac x="x-a" in image_eqI)

  3102 apply (auto simp: dist_norm algebra_simps)

  3103 done

  3104

  3105

  3106 subsection \<open>Closure of halfspaces and hyperplanes\<close>

  3107

  3108 lemma continuous_on_closed_Collect_le:

  3109   fixes f g :: "'a::t2_space \<Rightarrow> real"

  3110   assumes f: "continuous_on s f" and g: "continuous_on s g" and s: "closed s"

  3111   shows "closed {x \<in> s. f x \<le> g x}"

  3112 proof -

  3113   have "closed ((\<lambda>x. g x - f x) - {0..} \<inter> s)"

  3114     using closed_real_atLeast continuous_on_diff [OF g f]

  3115     by (simp add: continuous_on_closed_vimage [OF s])

  3116   also have "((\<lambda>x. g x - f x) - {0..} \<inter> s) = {x\<in>s. f x \<le> g x}"

  3117     by auto

  3118   finally show ?thesis .

  3119 qed

  3120

  3121 lemma continuous_at_inner: "continuous (at x) (inner a)"

  3122   unfolding continuous_at by (intro tendsto_intros)

  3123

  3124 lemma closed_halfspace_le: "closed {x. inner a x \<le> b}"

  3125   by (simp add: closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)

  3126

  3127 lemma closed_halfspace_ge: "closed {x. inner a x \<ge> b}"

  3128   by (simp add: closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)

  3129

  3130 lemma closed_hyperplane: "closed {x. inner a x = b}"

  3131   by (simp add: closed_Collect_eq continuous_on_inner continuous_on_const continuous_on_id)

  3132

  3133 lemma closed_halfspace_component_le: "closed {x::'a::euclidean_space. x\<bullet>i \<le> a}"

  3134   by (simp add: closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)

  3135

  3136 lemma closed_halfspace_component_ge: "closed {x::'a::euclidean_space. x\<bullet>i \<ge> a}"

  3137   by (simp add: closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)

  3138

  3139 lemma closed_interval_left:

  3140   fixes b :: "'a::euclidean_space"

  3141   shows "closed {x::'a. \<forall>i\<in>Basis. x\<bullet>i \<le> b\<bullet>i}"

  3142   by (simp add: Collect_ball_eq closed_INT closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)

  3143

  3144 lemma closed_interval_right:

  3145   fixes a :: "'a::euclidean_space"

  3146   shows "closed {x::'a. \<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i}"

  3147   by (simp add: Collect_ball_eq closed_INT closed_Collect_le continuous_on_inner continuous_on_const continuous_on_id)

  3148

  3149 lemma continuous_le_on_closure:

  3150   fixes a::real

  3151   assumes f: "continuous_on (closure s) f"

  3152       and x: "x \<in> closure(s)"

  3153       and xlo: "\<And>x. x \<in> s ==> f(x) \<le> a"

  3154     shows "f(x) \<le> a"

  3155     using image_closure_subset [OF f]

  3156   using image_closure_subset [OF f] closed_halfspace_le [of "1::real" a] assms

  3157   by force

  3158

  3159 lemma continuous_ge_on_closure:

  3160   fixes a::real

  3161   assumes f: "continuous_on (closure s) f"

  3162       and x: "x \<in> closure(s)"

  3163       and xlo: "\<And>x. x \<in> s ==> f(x) \<ge> a"

  3164     shows "f(x) \<ge> a"

  3165   using image_closure_subset [OF f] closed_halfspace_ge [of a "1::real"] assms

  3166   by force

  3167

  3168 lemma Lim_component_le:

  3169   fixes f :: "'a \<Rightarrow> 'b::euclidean_space"

  3170   assumes "(f \<longlongrightarrow> l) net"

  3171     and "\<not> (trivial_limit net)"

  3172     and "eventually (\<lambda>x. f(x)\<bullet>i \<le> b) net"

  3173   shows "l\<bullet>i \<le> b"

  3174   by (rule tendsto_le[OF assms(2) tendsto_const tendsto_inner[OF assms(1) tendsto_const] assms(3)])

  3175

  3176 lemma Lim_component_ge:

  3177   fixes f :: "'a \<Rightarrow> 'b::euclidean_space"

  3178   assumes "(f \<longlongrightarrow> l) net"

  3179     and "\<not> (trivial_limit net)"

  3180     and "eventually (\<lambda>x. b \<le> (f x)\<bullet>i) net"

  3181   shows "b \<le> l\<bullet>i"

  3182   by (rule tendsto_le[OF assms(2) tendsto_inner[OF assms(1) tendsto_const] tendsto_const assms(3)])

  3183

  3184 lemma Lim_component_eq:

  3185   fixes f :: "'a \<Rightarrow> 'b::euclidean_space"

  3186   assumes net: "(f \<longlongrightarrow> l) net" "\<not> trivial_limit net"

  3187     and ev:"eventually (\<lambda>x. f(x)\<bullet>i = b) net"

  3188   shows "l\<bullet>i = b"

  3189   using ev[unfolded order_eq_iff eventually_conj_iff]

  3190   using Lim_component_ge[OF net, of b i]

  3191   using Lim_component_le[OF net, of i b]

  3192   by auto

  3193

  3194 text \<open>Limits relative to a union.\<close>

  3195

  3196 lemma eventually_within_Un:

  3197   "eventually P (at x within (s \<union> t)) \<longleftrightarrow>

  3198     eventually P (at x within s) \<and> eventually P (at x within t)"

  3199   unfolding eventually_at_filter

  3200   by (auto elim!: eventually_rev_mp)

  3201

  3202 lemma Lim_within_union:

  3203  "(f \<longlongrightarrow> l) (at x within (s \<union> t)) \<longleftrightarrow>

  3204   (f \<longlongrightarrow> l) (at x within s) \<and> (f \<longlongrightarrow> l) (at x within t)"

  3205   unfolding tendsto_def

  3206   by (auto simp: eventually_within_Un)

  3207

  3208 lemma Lim_topological:

  3209   "(f \<longlongrightarrow> l) net \<longleftrightarrow>

  3210     trivial_limit net \<or> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net)"

  3211   unfolding tendsto_def trivial_limit_eq by auto

  3212

  3213 text \<open>Continuity relative to a union.\<close>

  3214

  3215 lemma continuous_on_Un_local:

  3216     "\<lbrakk>closedin (subtopology euclidean (s \<union> t)) s; closedin (subtopology euclidean (s \<union> t)) t;

  3217       continuous_on s f; continuous_on t f\<rbrakk>

  3218      \<Longrightarrow> continuous_on (s \<union> t) f"

  3219   unfolding continuous_on closedin_limpt

  3220   by (metis Lim_trivial_limit Lim_within_union Un_iff trivial_limit_within)

  3221

  3222 lemma continuous_on_cases_local:

  3223      "\<lbrakk>closedin (subtopology euclidean (s \<union> t)) s; closedin (subtopology euclidean (s \<union> t)) t;

  3224        continuous_on s f; continuous_on t g;

  3225        \<And>x. \<lbrakk>x \<in> s \<and> ~P x \<or> x \<in> t \<and> P x\<rbrakk> \<Longrightarrow> f x = g x\<rbrakk>

  3226       \<Longrightarrow> continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"

  3227   by (rule continuous_on_Un_local) (auto intro: continuous_on_eq)

  3228

  3229 lemma continuous_on_cases_le:

  3230   fixes h :: "'a :: topological_space \<Rightarrow> real"

  3231   assumes "continuous_on {t \<in> s. h t \<le> a} f"

  3232       and "continuous_on {t \<in> s. a \<le> h t} g"

  3233       and h: "continuous_on s h"

  3234       and "\<And>t. \<lbrakk>t \<in> s; h t = a\<rbrakk> \<Longrightarrow> f t = g t"

  3235     shows "continuous_on s (\<lambda>t. if h t \<le> a then f(t) else g(t))"

  3236 proof -

  3237   have s: "s = {t \<in> s. h t \<in> atMost a} \<union> {t \<in> s. h t \<in> atLeast a}"

  3238     by force

  3239   have 1: "closedin (subtopology euclidean s) {t \<in> s. h t \<in> atMost a}"

  3240     by (rule continuous_closedin_preimage [OF h closed_atMost])

  3241   have 2: "closedin (subtopology euclidean s) {t \<in> s. h t \<in> atLeast a}"

  3242     by (rule continuous_closedin_preimage [OF h closed_atLeast])

  3243   show ?thesis

  3244     apply (rule continuous_on_subset [of s, OF _ order_refl])

  3245     apply (subst s)

  3246     apply (rule continuous_on_cases_local)

  3247     using 1 2 s assms apply auto

  3248     done

  3249 qed

  3250

  3251 lemma continuous_on_cases_1:

  3252   fixes s :: "real set"

  3253   assumes "continuous_on {t \<in> s. t \<le> a} f"

  3254       and "continuous_on {t \<in> s. a \<le> t} g"

  3255       and "a \<in> s \<Longrightarrow> f a = g a"

  3256     shows "continuous_on s (\<lambda>t. if t \<le> a then f(t) else g(t))"

  3257 using assms

  3258 by (auto simp: continuous_on_id intro: continuous_on_cases_le [where h = id, simplified])

  3259

  3260 text\<open>Some more convenient intermediate-value theorem formulations.\<close>

  3261

  3262 lemma connected_ivt_hyperplane:

  3263   assumes "connected s"

  3264     and "x \<in> s"

  3265     and "y \<in> s"

  3266     and "inner a x \<le> b"

  3267     and "b \<le> inner a y"

  3268   shows "\<exists>z \<in> s. inner a z = b"

  3269 proof (rule ccontr)

  3270   assume as:"\<not> (\<exists>z\<in>s. inner a z = b)"

  3271   let ?A = "{x. inner a x < b}"

  3272   let ?B = "{x. inner a x > b}"

  3273   have "open ?A" "open ?B"

  3274     using open_halfspace_lt and open_halfspace_gt by auto

  3275   moreover

  3276   have "?A \<inter> ?B = {}" by auto

  3277   moreover

  3278   have "s \<subseteq> ?A \<union> ?B" using as by auto

  3279   ultimately

  3280   show False

  3281     using assms(1)[unfolded connected_def not_ex,

  3282       THEN spec[where x="?A"], THEN spec[where x="?B"]]

  3283     using assms(2-5)

  3284     by auto

  3285 qed

  3286

  3287 lemma connected_ivt_component:

  3288   fixes x::"'a::euclidean_space"

  3289   shows "connected s \<Longrightarrow>

  3290     x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow>

  3291     x\<bullet>k \<le> a \<Longrightarrow> a \<le> y\<bullet>k \<Longrightarrow> (\<exists>z\<in>s.  z\<bullet>k = a)"

  3292   using connected_ivt_hyperplane[of s x y "k::'a" a]

  3293   by (auto simp: inner_commute)

  3294

  3295 lemma image_affinity_cbox: fixes m::real

  3296   fixes a b c :: "'a::euclidean_space"

  3297   shows "(\<lambda>x. m *\<^sub>R x + c)  cbox a b =

  3298     (if cbox a b = {} then {}

  3299      else (if 0 \<le> m then cbox (m *\<^sub>R a + c) (m *\<^sub>R b + c)

  3300      else cbox (m *\<^sub>R b + c) (m *\<^sub>R a + c)))"

  3301 proof (cases "m = 0")

  3302   case True

  3303   {

  3304     fix x

  3305     assume "\<forall>i\<in>Basis. x \<bullet> i \<le> c \<bullet> i" "\<forall>i\<in>Basis. c \<bullet> i \<le> x \<bullet> i"

  3306     then have "x = c"

  3307       by (simp add: dual_order.antisym euclidean_eqI)

  3308   }

  3309   moreover have "c \<in> cbox (m *\<^sub>R a + c) (m *\<^sub>R b + c)"

  3310     unfolding True by (auto simp: cbox_sing)

  3311   ultimately show ?thesis using True by (auto simp: cbox_def)

  3312 next

  3313   case False

  3314   {

  3315     fix y

  3316     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"

  3317     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"

  3318       by (auto simp: inner_distrib)

  3319   }

  3320   moreover

  3321   {

  3322     fix y

  3323     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"

  3324     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"

  3325       by (auto simp: mult_left_mono_neg inner_distrib)

  3326   }

  3327   moreover

  3328   {

  3329     fix y

  3330     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"

  3331     then have "y \<in> (\<lambda>x. m *\<^sub>R x + c)  cbox a b"

  3332       unfolding image_iff Bex_def mem_box

  3333       apply (intro exI[where x="(1 / m) *\<^sub>R (y - c)"])

  3334       apply (auto simp: pos_le_divide_eq pos_divide_le_eq mult.commute inner_distrib inner_diff_left)

  3335       done

  3336   }

  3337   moreover

  3338   {

  3339     fix y

  3340     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"

  3341     then have "y \<in> (\<lambda>x. m *\<^sub>R x + c)  cbox a b"

  3342       unfolding image_iff Bex_def mem_box

  3343       apply (intro exI[where x="(1 / m) *\<^sub>R (y - c)"])

  3344       apply (auto simp: neg_le_divide_eq neg_divide_le_eq mult.commute inner_distrib inner_diff_left)

  3345       done

  3346   }

  3347   ultimately show ?thesis using False by (auto simp: cbox_def)

  3348 qed

  3349

  3350 lemma image_smult_cbox:"(\<lambda>x. m *\<^sub>R (x::_::euclidean_space))  cbox a b =

  3351   (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))"

  3352   using image_affinity_cbox[of m 0 a b] by auto

  3353

  3354 lemma islimpt_greaterThanLessThan1:

  3355   fixes a b::"'a::{linorder_topology, dense_order}"

  3356   assumes "a < b"

  3357   shows  "a islimpt {a<..<b}"

  3358 proof (rule islimptI)

  3359   fix T

  3360   assume "open T" "a \<in> T"

  3361   from open_right[OF this \<open>a < b\<close>]

  3362   obtain c where c: "a < c" "{a..<c} \<subseteq> T" by auto

  3363   with assms dense[of a "min c b"]

  3364   show "\<exists>y\<in>{a<..<b}. y \<in> T \<and> y \<noteq> a"

  3365     by (metis atLeastLessThan_iff greaterThanLessThan_iff min_less_iff_conj

  3366       not_le order.strict_implies_order subset_eq)

  3367 qed

  3368

  3369 lemma islimpt_greaterThanLessThan2:

  3370   fixes a b::"'a::{linorder_topology, dense_order}"

  3371   assumes "a < b"

  3372   shows  "b islimpt {a<..<b}"

  3373 proof (rule islimptI)

  3374   fix T

  3375   assume "open T" "b \<in> T"

  3376   from open_left[OF this \<open>a < b\<close>]

  3377   obtain c where c: "c < b" "{c<..b} \<subseteq> T" by auto

  3378   with assms dense[of "max a c" b]

  3379   show "\<exists>y\<in>{a<..<b}. y \<in> T \<and> y \<noteq> b"

  3380     by (metis greaterThanAtMost_iff greaterThanLessThan_iff max_less_iff_conj

  3381       not_le order.strict_implies_order subset_eq)

  3382 qed

  3383

  3384 lemma closure_greaterThanLessThan[simp]:

  3385   fixes a b::"'a::{linorder_topology, dense_order}"

  3386   shows "a < b \<Longrightarrow> closure {a <..< b} = {a .. b}" (is "_ \<Longrightarrow> ?l = ?r")

  3387 proof

  3388   have "?l \<subseteq> closure ?r"

  3389     by (rule closure_mono) auto

  3390   thus "closure {a<..<b} \<subseteq> {a..b}" by simp

  3391 qed (auto simp: closure_def order.order_iff_strict islimpt_greaterThanLessThan1

  3392   islimpt_greaterThanLessThan2)

  3393

  3394 lemma closure_greaterThan[simp]:

  3395   fixes a b::"'a::{no_top, linorder_topology, dense_order}"

  3396   shows "closure {a<..} = {a..}"

  3397 proof -

  3398   from gt_ex obtain b where "a < b" by auto

  3399   hence "{a<..} = {a<..<b} \<union> {b..}" by auto

  3400   also have "closure \<dots> = {a..}" using \<open>a < b\<close> unfolding closure_Un

  3401     by auto

  3402   finally show ?thesis .

  3403 qed

  3404

  3405 lemma closure_lessThan[simp]:

  3406   fixes b::"'a::{no_bot, linorder_topology, dense_order}"

  3407   shows "closure {..<b} = {..b}"

  3408 proof -

  3409   from lt_ex obtain a where "a < b" by auto

  3410   hence "{..<b} = {a<..<b} \<union> {..a}" by auto

  3411   also have "closure \<dots> = {..b}" using \<open>a < b\<close> unfolding closure_Un

  3412     by auto

  3413   finally show ?thesis .

  3414 qed

  3415

  3416 lemma closure_atLeastLessThan[simp]:

  3417   fixes a b::"'a::{linorder_topology, dense_order}"

  3418   assumes "a < b"

  3419   shows "closure {a ..< b} = {a .. b}"

  3420 proof -

  3421   from assms have "{a ..< b} = {a} \<union> {a <..< b}" by auto

  3422   also have "closure \<dots> = {a .. b}" unfolding closure_Un

  3423     by (auto simp: assms less_imp_le)

  3424   finally show ?thesis .

  3425 qed

  3426

  3427 lemma closure_greaterThanAtMost[simp]:

  3428   fixes a b::"'a::{linorder_topology, dense_order}"

  3429   assumes "a < b"

  3430   shows "closure {a <.. b} = {a .. b}"

  3431 proof -

  3432   from assms have "{a <.. b} = {b} \<union> {a <..< b}" by auto

  3433   also have "closure \<dots> = {a .. b}" unfolding closure_Un

  3434     by (auto simp: assms less_imp_le)

  3435   finally show ?thesis .

  3436 qed

  3437

  3438

  3439 subsection \<open>Homeomorphisms\<close>

  3440

  3441 definition "homeomorphism s t f g \<longleftrightarrow>

  3442   (\<forall>x\<in>s. (g(f x) = x)) \<and> (f  s = t) \<and> continuous_on s f \<and>

  3443   (\<forall>y\<in>t. (f(g y) = y)) \<and> (g  t = s) \<and> continuous_on t g"

  3444

  3445 lemma homeomorphismI [intro?]:

  3446   assumes "continuous_on S f" "continuous_on T g"

  3447           "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"

  3448     shows "homeomorphism S T f g"

  3449   using assms by (force simp: homeomorphism_def)

  3450

  3451 lemma homeomorphism_translation:

  3452   fixes a :: "'a :: real_normed_vector"

  3453   shows "homeomorphism (op + a  S) S (op + (- a)) (op + a)"

  3454 unfolding homeomorphism_def by (auto simp: algebra_simps continuous_intros)

  3455

  3456 lemma homeomorphism_ident: "homeomorphism T T (\<lambda>a. a) (\<lambda>a. a)"

  3457   by (rule homeomorphismI) (auto simp: continuous_on_id)

  3458

  3459 lemma homeomorphism_compose:

  3460   assumes "homeomorphism S T f g" "homeomorphism T U h k"

  3461     shows "homeomorphism S U (h o f) (g o k)"

  3462   using assms

  3463   unfolding homeomorphism_def

  3464   by (intro conjI ballI continuous_on_compose) (auto simp: image_comp [symmetric])

  3465

  3466 lemma homeomorphism_symD: "homeomorphism S t f g \<Longrightarrow> homeomorphism t S g f"

  3467   by (simp add: homeomorphism_def)

  3468

  3469 lemma homeomorphism_sym: "homeomorphism S t f g = homeomorphism t S g f"

  3470   by (force simp: homeomorphism_def)

  3471

  3472 definition homeomorphic :: "'a::topological_space set \<Rightarrow> 'b::topological_space set \<Rightarrow> bool"

  3473     (infixr "homeomorphic" 60)

  3474   where "s homeomorphic t \<equiv> (\<exists>f g. homeomorphism s t f g)"

  3475

  3476 lemma homeomorphic_empty [iff]:

  3477      "S homeomorphic {} \<longleftrightarrow> S = {}" "{} homeomorphic S \<longleftrightarrow> S = {}"

  3478   by (auto simp: homeomorphic_def homeomorphism_def)

  3479

  3480 lemma homeomorphic_refl: "s homeomorphic s"

  3481   unfolding homeomorphic_def homeomorphism_def

  3482   using continuous_on_id

  3483   apply (rule_tac x = "(\<lambda>x. x)" in exI)

  3484   apply (rule_tac x = "(\<lambda>x. x)" in exI)

  3485   apply blast

  3486   done

  3487

  3488 lemma homeomorphic_sym: "s homeomorphic t \<longleftrightarrow> t homeomorphic s"

  3489   unfolding homeomorphic_def homeomorphism_def

  3490   by blast

  3491

  3492 lemma homeomorphic_trans [trans]:

  3493   assumes "S homeomorphic T"

  3494       and "T homeomorphic U"

  3495     shows "S homeomorphic U"

  3496   using assms

  3497   unfolding homeomorphic_def

  3498 by (metis homeomorphism_compose)

  3499

  3500 lemma homeomorphic_minimal:

  3501   "s homeomorphic t \<longleftrightarrow>

  3502     (\<exists>f g. (\<forall>x\<in>s. f(x) \<in> t \<and> (g(f(x)) = x)) \<and>

  3503            (\<forall>y\<in>t. g(y) \<in> s \<and> (f(g(y)) = y)) \<and>

  3504            continuous_on s f \<and> continuous_on t g)"

  3505    (is "?lhs = ?rhs")

  3506 proof

  3507   assume ?lhs

  3508   then show ?rhs

  3509     by (fastforce simp: homeomorphic_def homeomorphism_def)

  3510 next

  3511   assume ?rhs

  3512   then show ?lhs

  3513     apply clarify

  3514     unfolding homeomorphic_def homeomorphism_def

  3515     by (metis equalityI image_subset_iff subsetI)

  3516  qed

  3517

  3518 lemma homeomorphicI [intro?]:

  3519    "\<lbrakk>f  S = T; g  T = S;

  3520      continuous_on S f; continuous_on T g;

  3521      \<And>x. x \<in> S \<Longrightarrow> g(f(x)) = x;

  3522      \<And>y. y \<in> T \<Longrightarrow> f(g(y)) = y\<rbrakk> \<Longrightarrow> S homeomorphic T"

  3523 unfolding homeomorphic_def homeomorphism_def by metis

  3524

  3525 lemma homeomorphism_of_subsets:

  3526    "\<lbrakk>homeomorphism S T f g; S' \<subseteq> S; T'' \<subseteq> T; f  S' = T'\<rbrakk>

  3527     \<Longrightarrow> homeomorphism S' T' f g"

  3528 apply (auto simp: homeomorphism_def elim!: continuous_on_subset)

  3529 by (metis subsetD imageI)

  3530

  3531 lemma homeomorphism_apply1: "\<lbrakk>homeomorphism S T f g; x \<in> S\<rbrakk> \<Longrightarrow> g(f x) = x"

  3532   by (simp add: homeomorphism_def)

  3533

  3534 lemma homeomorphism_apply2: "\<lbrakk>homeomorphism S T f g; x \<in> T\<rbrakk> \<Longrightarrow> f(g x) = x"

  3535   by (simp add: homeomorphism_def)

  3536

  3537 lemma homeomorphism_image1: "homeomorphism S T f g \<Longrightarrow> f  S = T"

  3538   by (simp add: homeomorphism_def)

  3539

  3540 lemma homeomorphism_image2: "homeomorphism S T f g \<Longrightarrow> g  T = S"

  3541   by (simp add: homeomorphism_def)

  3542

  3543 lemma homeomorphism_cont1: "homeomorphism S T f g \<Longrightarrow> continuous_on S f"

  3544   by (simp add: homeomorphism_def)

  3545

  3546 lemma homeomorphism_cont2: "homeomorphism S T f g \<Longrightarrow> continuous_on T g"

  3547   by (simp add: homeomorphism_def)

  3548

  3549 lemma continuous_on_no_limpt:

  3550    "(\<And>x. \<not> x islimpt S) \<Longrightarrow> continuous_on S f"

  3551   unfolding continuous_on_def

  3552   by (metis UNIV_I empty_iff eventually_at_topological islimptE open_UNIV tendsto_def trivial_limit_within)

  3553

  3554 lemma continuous_on_finite:

  3555   fixes S :: "'a::t1_space set"

  3556   shows "finite S \<Longrightarrow> continuous_on S f"

  3557 by (metis continuous_on_no_limpt islimpt_finite)

  3558

  3559 lemma homeomorphic_finite:

  3560   fixes S :: "'a::t1_space set" and T :: "'b::t1_space set"

  3561   assumes "finite T"

  3562   shows "S homeomorphic T \<longleftrightarrow> finite S \<and> finite T \<and> card S = card T" (is "?lhs = ?rhs")

  3563 proof

  3564   assume "S homeomorphic T"

  3565   with assms show ?rhs

  3566     apply (auto simp: homeomorphic_def homeomorphism_def)

  3567      apply (metis finite_imageI)

  3568     by (metis card_image_le finite_imageI le_antisym)

  3569 next

  3570   assume R: ?rhs

  3571   with finite_same_card_bij obtain h where "bij_betw h S T"

  3572     by auto

  3573   with R show ?lhs

  3574     apply (auto simp: homeomorphic_def homeomorphism_def continuous_on_finite)

  3575     apply (rule_tac x=h in exI)

  3576     apply (rule_tac x="inv_into S h" in exI)

  3577     apply (auto simp:  bij_betw_inv_into_left bij_betw_inv_into_right bij_betw_imp_surj_on inv_into_into bij_betwE)

  3578     apply (metis bij_betw_def bij_betw_inv_into)

  3579     done

  3580 qed

  3581

  3582 text \<open>Relatively weak hypotheses if a set is compact.\<close>

  3583

  3584 lemma homeomorphism_compact:

  3585   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"

  3586   assumes "compact s" "continuous_on s f"  "f  s = t"  "inj_on f s"

  3587   shows "\<exists>g. homeomorphism s t f g"

  3588 proof -

  3589   define g where "g x = (SOME y. y\<in>s \<and> f y = x)" for x

  3590   have g: "\<forall>x\<in>s. g (f x) = x"

  3591     using assms(3) assms(4)[unfolded inj_on_def] unfolding g_def by auto

  3592   {

  3593     fix y

  3594     assume "y \<in> t"

  3595     then obtain x where x:"f x = y" "x\<in>s"

  3596       using assms(3) by auto

  3597     then have "g (f x) = x" using g by auto

  3598     then have "f (g y) = y" unfolding x(1)[symmetric] by auto

  3599   }

  3600   then have g':"\<forall>x\<in>t. f (g x) = x" by auto

  3601   moreover

  3602   {

  3603     fix x

  3604     have "x\<in>s \<Longrightarrow> x \<in> g  t"

  3605       using g[THEN bspec[where x=x]]

  3606       unfolding image_iff

  3607       using assms(3)

  3608       by (auto intro!: bexI[where x="f x"])

  3609     moreover

  3610     {

  3611       assume "x\<in>g  t"

  3612       then obtain y where y:"y\<in>t" "g y = x" by auto

  3613       then obtain x' where x':"x'\<in>s" "f x' = y"

  3614         using assms(3) by auto

  3615       then have "x \<in> s"

  3616         unfolding g_def

  3617         using someI2[of "\<lambda>b. b\<in>s \<and> f b = y" x' "\<lambda>x. x\<in>s"]

  3618         unfolding y(2)[symmetric] and g_def

  3619         by auto

  3620     }

  3621     ultimately have "x\<in>s \<longleftrightarrow> x \<in> g  t" ..

  3622   }

  3623   then have "g  t = s" by auto

  3624   ultimately show ?thesis

  3625     unfolding homeomorphism_def homeomorphic_def

  3626     apply (rule_tac x=g in exI)

  3627     using g and assms(3) and continuous_on_inv[OF assms(2,1), of g, unfolded assms(3)] and assms(2)

  3628     apply auto

  3629     done

  3630 qed

  3631

  3632 lemma homeomorphic_compact:

  3633   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"

  3634   shows "compact s \<Longrightarrow> continuous_on s f \<Longrightarrow> (f  s = t) \<Longrightarrow> inj_on f s \<Longrightarrow> s homeomorphic t"

  3635   unfolding homeomorphic_def by (metis homeomorphism_compact)

  3636

  3637 text\<open>Preservation of topological properties.\<close>

  3638

  3639 lemma homeomorphic_compactness: "s homeomorphic t \<Longrightarrow> (compact s \<longleftrightarrow> compact t)"

  3640   unfolding homeomorphic_def homeomorphism_def

  3641   by (metis compact_continuous_image)

  3642

  3643 text\<open>Results on translation, scaling etc.\<close>

  3644

  3645 lemma homeomorphic_scaling:

  3646   fixes s :: "'a::real_normed_vector set"

  3647   assumes "c \<noteq> 0"

  3648   shows "s homeomorphic ((\<lambda>x. c *\<^sub>R x)  s)"

  3649   unfolding homeomorphic_minimal

  3650   apply (rule_tac x="\<lambda>x. c *\<^sub>R x" in exI)

  3651   apply (rule_tac x="\<lambda>x. (1 / c) *\<^sub>R x" in exI)

  3652   using assms

  3653   apply (auto simp: continuous_intros)

  3654   done

  3655

  3656 lemma homeomorphic_translation:

  3657   fixes s :: "'a::real_normed_vector set"

  3658   shows "s homeomorphic ((\<lambda>x. a + x)  s)"

  3659   unfolding homeomorphic_minimal

  3660   apply (rule_tac x="\<lambda>x. a + x" in exI)

  3661   apply (rule_tac x="\<lambda>x. -a + x" in exI)

  3662   using continuous_on_add [OF continuous_on_const continuous_on_id, of s a]

  3663     continuous_on_add [OF continuous_on_const continuous_on_id, of "plus a  s" "- a"]

  3664   apply auto

  3665   done

  3666

  3667 lemma homeomorphic_affinity:

  3668   fixes s :: "'a::real_normed_vector set"

  3669   assumes "c \<noteq> 0"

  3670   shows "s homeomorphic ((\<lambda>x. a + c *\<^sub>R x)  s)"

  3671 proof -

  3672   have *: "op + a  op *\<^sub>R c  s = (\<lambda>x. a + c *\<^sub>R x)  s" by auto

  3673   show ?thesis

  3674     using homeomorphic_trans

  3675     using homeomorphic_scaling[OF assms, of s]

  3676     using homeomorphic_translation[of "(\<lambda>x. c *\<^sub>R x)  s" a]

  3677     unfolding *

  3678     by auto

  3679 qed

  3680

  3681 lemma homeomorphic_balls:

  3682   fixes a b ::"'a::real_normed_vector"

  3683   assumes "0 < d"  "0 < e"

  3684   shows "(ball a d) homeomorphic  (ball b e)" (is ?th)

  3685     and "(cball a d) homeomorphic (cball b e)" (is ?cth)

  3686 proof -

  3687   show ?th unfolding homeomorphic_minimal

  3688     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)

  3689     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)

  3690     using assms

  3691     apply (auto intro!: continuous_intros

  3692       simp: dist_commute dist_norm pos_divide_less_eq mult_strict_left_mono)

  3693     done

  3694   show ?cth unfolding homeomorphic_minimal

  3695     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)

  3696     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)

  3697     using assms

  3698     apply (auto intro!: continuous_intros

  3699       simp: dist_commute dist_norm pos_divide_le_eq mult_strict_left_mono)

  3700     done

  3701 qed

  3702

  3703 lemma homeomorphic_spheres:

  3704   fixes a b ::"'a::real_normed_vector"

  3705   assumes "0 < d"  "0 < e"

  3706   shows "(sphere a d) homeomorphic (sphere b e)"

  3707 unfolding homeomorphic_minimal

  3708     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)

  3709     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)

  3710     using assms

  3711     apply (auto intro!: continuous_intros

  3712       simp: dist_commute dist_norm pos_divide_less_eq mult_strict_left_mono)

  3713     done

  3714

  3715 lemma homeomorphic_ball01_UNIV:

  3716   "ball (0::'a::real_normed_vector) 1 homeomorphic (UNIV:: 'a set)"

  3717   (is "?B homeomorphic ?U")

  3718 proof

  3719   have "x \<in> (\<lambda>z. z /\<^sub>R (1 - norm z))  ball 0 1" for x::'a

  3720     apply (rule_tac x="x /\<^sub>R (1 + norm x)" in image_eqI)

  3721      apply (auto simp: divide_simps)

  3722     using norm_ge_zero [of x] apply linarith+

  3723     done

  3724   then show "(\<lambda>z::'a. z /\<^sub>R (1 - norm z))  ?B = ?U"

  3725     by blast

  3726   have "x \<in> range (\<lambda>z. (1 / (1 + norm z)) *\<^sub>R z)" if "norm x < 1" for x::'a

  3727     apply (rule_tac x="x /\<^sub>R (1 - norm x)" in image_eqI)

  3728     using that apply (auto simp: divide_simps)

  3729     done

  3730   then show "(\<lambda>z::'a. z /\<^sub>R (1 + norm z))  ?U = ?B"

  3731     by (force simp: divide_simps dest: add_less_zeroD)

  3732   show "continuous_on (ball 0 1) (\<lambda>z. z /\<^sub>R (1 - norm z))"

  3733     by (rule continuous_intros | force)+

  3734   show "continuous_on UNIV (\<lambda>z. z /\<^sub>R (1 + norm z))"

  3735     apply (intro continuous_intros)

  3736     apply (metis le_add_same_cancel1 norm_ge_zero not_le zero_less_one)

  3737     done

  3738   show "\<And>x. x \<in> ball 0 1 \<Longrightarrow>

  3739          x /\<^sub>R (1 - norm x) /\<^sub>R (1 + norm (x /\<^sub>R (1 - norm x))) = x"

  3740     by (auto simp: divide_simps)

  3741   show "\<And>y. y /\<^sub>R (1 + norm y) /\<^sub>R (1 - norm (y /\<^sub>R (1 + norm y))) = y"

  3742     apply (auto simp: divide_simps)

  3743     apply (metis le_add_same_cancel1 norm_ge_zero not_le zero_less_one)

  3744     done

  3745 qed

  3746

  3747 proposition homeomorphic_ball_UNIV:

  3748   fixes a ::"'a::real_normed_vector"

  3749   assumes "0 < r" shows "ball a r homeomorphic (UNIV:: 'a set)"

  3750   using assms homeomorphic_ball01_UNIV homeomorphic_balls(1) homeomorphic_trans zero_less_one by blast

  3751

  3752

  3753 subsection\<open>Inverse function property for open/closed maps\<close>

  3754

  3755 lemma continuous_on_inverse_open_map:

  3756   assumes contf: "continuous_on S f"

  3757     and imf: "f  S = T"

  3758     and injf: "\<And>x. x \<in> S \<Longrightarrow> g (f x) = x"

  3759     and oo: "\<And>U. openin (subtopology euclidean S) U \<Longrightarrow> openin (subtopology euclidean T) (f  U)"

  3760   shows "continuous_on T g"

  3761 proof -

  3762   from imf injf have gTS: "g  T = S"

  3763     by force

  3764   from imf injf have fU: "U \<subseteq> S \<Longrightarrow> (f  U) = {x \<in> T. g x \<in> U}" for U

  3765     by force

  3766   show ?thesis

  3767     by (simp add: continuous_on_open [of T g] gTS) (metis openin_imp_subset fU oo)

  3768 qed

  3769

  3770 lemma continuous_on_inverse_closed_map:

  3771   assumes contf: "continuous_on S f"

  3772     and imf: "f  S = T"

  3773     and injf: "\<And>x. x \<in> S \<Longrightarrow> g(f x) = x"

  3774     and oo: "\<And>U. closedin (subtopology euclidean S) U \<Longrightarrow> closedin (subtopology euclidean T) (f  U)"

  3775   shows "continuous_on T g"

  3776 proof -

  3777   from imf injf have gTS: "g  T = S"

  3778     by force

  3779   from imf injf have fU: "U \<subseteq> S \<Longrightarrow> (f  U) = {x \<in> T. g x \<in> U}" for U

  3780     by force

  3781   show ?thesis

  3782     by (simp add: continuous_on_closed [of T g] gTS) (metis closedin_imp_subset fU oo)

  3783 qed

  3784

  3785 lemma homeomorphism_injective_open_map:

  3786   assumes contf: "continuous_on S f"

  3787     and imf: "f  S = T"

  3788     and injf: "inj_on f S"

  3789     and oo: "\<And>U. openin (subtopology euclidean S) U \<Longrightarrow> openin (subtopology euclidean T) (f  U)"

  3790   obtains g where "homeomorphism S T f g"

  3791 proof

  3792   have "continuous_on T (inv_into S f)"

  3793     by (metis contf continuous_on_inverse_open_map imf injf inv_into_f_f oo)

  3794   with imf injf contf show "homeomorphism S T f (inv_into S f)"

  3795     by (auto simp: homeomorphism_def)

  3796 qed

  3797

  3798 lemma homeomorphism_injective_closed_map:

  3799   assumes contf: "continuous_on S f"

  3800     and imf: "f  S = T"

  3801     and injf: "inj_on f S"

  3802     and oo: "\<And>U. closedin (subtopology euclidean S) U \<Longrightarrow> closedin (subtopology euclidean T) (f  U)"

  3803   obtains g where "homeomorphism S T f g"

  3804 proof

  3805   have "continuous_on T (inv_into S f)"

  3806     by (metis contf continuous_on_inverse_closed_map imf injf inv_into_f_f oo)

  3807   with imf injf contf show "homeomorphism S T f (inv_into S f)"

  3808     by (auto simp: homeomorphism_def)

  3809 qed

  3810

  3811 lemma homeomorphism_imp_open_map:

  3812   assumes hom: "homeomorphism S T f g"

  3813     and oo: "openin (subtopology euclidean S) U"

  3814   shows "openin (subtopology euclidean T) (f  U)"

  3815 proof -

  3816   from hom oo have [simp]: "f  U = {y. y \<in> T \<and> g y \<in> U}"

  3817     using openin_subset by (fastforce simp: homeomorphism_def rev_image_eqI)

  3818   from hom have "continuous_on T g"

  3819     unfolding homeomorphism_def by blast

  3820   moreover have "g  T = S"

  3821     by (metis hom homeomorphism_def)

  3822   ultimately show ?thesis

  3823     by (simp add: continuous_on_open oo)

  3824 qed

  3825

  3826 lemma homeomorphism_imp_closed_map:

  3827   assumes hom: "homeomorphism S T f g"

  3828     and oo: "closedin (subtopology euclidean S) U"

  3829   shows "closedin (subtopology euclidean T) (f  U)"

  3830 proof -

  3831   from hom oo have [simp]: "f  U = {y. y \<in> T \<and> g y \<in> U}"

  3832     using closedin_subset by (fastforce simp: homeomorphism_def rev_image_eqI)

  3833   from hom have "continuous_on T g"

  3834     unfolding homeomorphism_def by blast

  3835   moreover have "g  T = S"

  3836     by (metis hom homeomorphism_def)

  3837   ultimately show ?thesis

  3838     by (simp add: continuous_on_closed oo)

  3839 qed

  3840

  3841

  3842 subsection \<open>"Isometry" (up to constant bounds) of injective linear map etc.\<close>

  3843

  3844 lemma cauchy_isometric:

  3845   assumes e: "e > 0"

  3846     and s: "subspace s"

  3847     and f: "bounded_linear f"

  3848     and normf: "\<forall>x\<in>s. norm (f x) \<ge> e * norm x"

  3849     and xs: "\<forall>n. x n \<in> s"

  3850     and cf: "Cauchy (f \<circ> x)"

  3851   shows "Cauchy x"

  3852 proof -

  3853   interpret f: bounded_linear f by fact

  3854   have "\<exists>N. \<forall>n\<ge>N. norm (x n - x N) < d" if "d > 0" for d :: real

  3855   proof -

  3856     from that obtain N where N: "\<forall>n\<ge>N. norm (f (x n) - f (x N)) < e * d"

  3857       using cf[unfolded Cauchy_def o_def dist_norm, THEN spec[where x="e*d"]] e

  3858       by auto

  3859     have "norm (x n - x N) < d" if "n \<ge> N" for n

  3860     proof -

  3861       have "e * norm (x n - x N) \<le> norm (f (x n - x N))"

  3862         using subspace_diff[OF s, of "x n" "x N"]

  3863         using xs[THEN spec[where x=N]] and xs[THEN spec[where x=n]]

  3864         using normf[THEN bspec[where x="x n - x N"]]

  3865         by auto

  3866       also have "norm (f (x n - x N)) < e * d"

  3867         using \<open>N \<le> n\<close> N unfolding f.diff[symmetric] by auto

  3868       finally show ?thesis

  3869         using \<open>e>0\<close> by simp

  3870     qed

  3871     then show ?thesis by auto

  3872   qed

  3873   then show ?thesis

  3874     by (simp add: Cauchy_altdef2 dist_norm)

  3875 qed

  3876

  3877 lemma complete_isometric_image:

  3878   assumes "0 < e"

  3879     and s: "subspace s"

  3880     and f: "bounded_linear f"

  3881     and normf: "\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)"

  3882     and cs: "complete s"

  3883   shows "complete (f  s)"

  3884 proof -

  3885   have "\<exists>l\<in>f  s. (g \<longlongrightarrow> l) sequentially"

  3886     if as:"\<forall>n::nat. g n \<in> f  s" and cfg:"Cauchy g" for g

  3887   proof -

  3888     from that obtain x where "\<forall>n. x n \<in> s \<and> g n = f (x n)"

  3889       using choice[of "\<lambda> n xa. xa \<in> s \<and> g n = f xa"] by auto

  3890     then have x: "\<forall>n. x n \<in> s" "\<forall>n. g n = f (x n)" by auto

  3891     then have "f \<circ> x = g" by (simp add: fun_eq_iff)

  3892     then obtain l where "l\<in>s" and l:"(x \<longlongrightarrow> l) sequentially"

  3893       using cs[unfolded complete_def, THEN spec[where x=x]]

  3894       using cauchy_isometric[OF \<open>0 < e\<close> s f normf] and cfg and x(1)

  3895       by auto

  3896     then show ?thesis

  3897       using linear_continuous_at[OF f, unfolded continuous_at_sequentially, THEN spec[where x=x], of l]

  3898       by (auto simp: \<open>f \<circ> x = g\<close>)

  3899   qed

  3900   then show ?thesis

  3901     unfolding complete_def by auto

  3902 qed

  3903

  3904 lemma injective_imp_isometric:

  3905   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"

  3906   assumes s: "closed s" "subspace s"

  3907     and f: "bounded_linear f" "\<forall>x\<in>s. f x = 0 \<longrightarrow> x = 0"

  3908   shows "\<exists>e>0. \<forall>x\<in>s. norm (f x) \<ge> e * norm x"

  3909 proof (cases "s \<subseteq> {0::'a}")

  3910   case True

  3911   have "norm x \<le> norm (f x)" if "x \<in> s" for x

  3912   proof -

  3913     from True that have "x = 0" by auto

  3914     then show ?thesis by simp

  3915   qed

  3916   then show ?thesis

  3917     by (auto intro!: exI[where x=1])

  3918 next

  3919   case False

  3920   interpret f: bounded_linear f by fact

  3921   from False obtain a where a: "a \<noteq> 0" "a \<in> s"

  3922     by auto

  3923   from False have "s \<noteq> {}"

  3924     by auto

  3925   let ?S = "{f x| x. x \<in> s \<and> norm x = norm a}"

  3926   let ?S' = "{x::'a. x\<in>s \<and> norm x = norm a}"

  3927   let ?S'' = "{x::'a. norm x = norm a}"

  3928

  3929   have "?S'' = frontier (cball 0 (norm a))"

  3930     by (simp add: sphere_def dist_norm)

  3931   then have "compact ?S''" by (metis compact_cball compact_frontier)

  3932   moreover have "?S' = s \<inter> ?S''" by auto

  3933   ultimately have "compact ?S'"

  3934     using closed_Int_compact[of s ?S''] using s(1) by auto

  3935   moreover have *:"f  ?S' = ?S" by auto

  3936   ultimately have "compact ?S"

  3937     using compact_continuous_image[OF linear_continuous_on[OF f(1)], of ?S'] by auto

  3938   then have "closed ?S"

  3939     using compact_imp_closed by auto

  3940   moreover from a have "?S \<noteq> {}" by auto

  3941   ultimately obtain b' where "b'\<in>?S" "\<forall>y\<in>?S. norm b' \<le> norm y"

  3942     using distance_attains_inf[of ?S 0] unfolding dist_0_norm by auto

  3943   then obtain b where "b\<in>s"

  3944     and ba: "norm b = norm a"

  3945     and b: "\<forall>x\<in>{x \<in> s. norm x = norm a}. norm (f b) \<le> norm (f x)"

  3946     unfolding *[symmetric] unfolding image_iff by auto

  3947

  3948   let ?e = "norm (f b) / norm b"

  3949   have "norm b > 0"

  3950     using ba and a and norm_ge_zero by auto

  3951   moreover have "norm (f b) > 0"

  3952     using f(2)[THEN bspec[where x=b], OF \<open>b\<in>s\<close>]

  3953     using \<open>norm b >0\<close> by simp

  3954   ultimately have "0 < norm (f b) / norm b" by simp

  3955   moreover

  3956   have "norm (f b) / norm b * norm x \<le> norm (f x)" if "x\<in>s" for x

  3957   proof (cases "x = 0")

  3958     case True

  3959     then show "norm (f b) / norm b * norm x \<le> norm (f x)"

  3960       by auto

  3961   next

  3962     case False

  3963     with \<open>a \<noteq> 0\<close> have *: "0 < norm a / norm x"

  3964       unfolding zero_less_norm_iff[symmetric] by simp

  3965     have "\<forall>x\<in>s. c *\<^sub>R x \<in> s" for c

  3966       using s[unfolded subspace_def] by simp

  3967     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}"

  3968       by simp

  3969     with \<open>x \<noteq> 0\<close> \<open>a \<noteq> 0\<close> show "norm (f b) / norm b * norm x \<le> norm (f x)"

  3970       using b[THEN bspec[where x="(norm a / norm x) *\<^sub>R x"]]

  3971       unfolding f.scaleR and ba

  3972       by (auto simp: mult.commute pos_le_divide_eq pos_divide_le_eq)

  3973   qed

  3974   ultimately show ?thesis by auto

  3975 qed

  3976

  3977 lemma closed_injective_image_subspace:

  3978   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"

  3979   assumes "subspace s" "bounded_linear f" "\<forall>x\<in>s. f x = 0 \<longrightarrow> x = 0" "closed s"

  3980   shows "closed(f  s)"

  3981 proof -

  3982   obtain e where "e > 0" and e: "\<forall>x\<in>s. e * norm x \<le> norm (f x)"

  3983     using injective_imp_isometric[OF assms(4,1,2,3)] by auto

  3984   show ?thesis

  3985     using complete_isometric_image[OF \<open>e>0\<close> assms(1,2) e] and assms(4)

  3986     unfolding complete_eq_closed[symmetric] by auto

  3987 qed

  3988

  3989

  3990 subsection \<open>Some properties of a canonical subspace\<close>

  3991

  3992 lemma subspace_substandard: "subspace {x::'a::euclidean_space. (\<forall>i\<in>Basis. P i \<longrightarrow> x\<bullet>i = 0)}"

  3993   by (auto simp: subspace_def inner_add_left)

  3994

  3995 lemma closed_substandard: "closed {x::'a::euclidean_space. \<forall>i\<in>Basis. P i \<longrightarrow> x\<bullet>i = 0}"

  3996   (is "closed ?A")

  3997 proof -

  3998   let ?D = "{i\<in>Basis. P i}"

  3999   have "closed (\<Inter>i\<in>?D. {x::'a. x\<bullet>i = 0})"

  4000     by (simp add: closed_INT closed_Collect_eq continuous_on_inner

  4001         continuous_on_const continuous_on_id)

  4002   also have "(\<Inter>i\<in>?D. {x::'a. x\<bullet>i = 0}) = ?A"

  4003     by auto

  4004   finally show "closed ?A" .

  4005 qed

  4006

  4007 lemma dim_substandard:

  4008   assumes d: "d \<subseteq> Basis"

  4009   shows "dim {x::'a::euclidean_space. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0} = card d" (is "dim ?A = _")

  4010 proof (rule dim_unique)

  4011   from d show "d \<subseteq> ?A"

  4012     by (auto simp: inner_Basis)

  4013   from d show "independent d"

  4014     by (rule independent_mono [OF independent_Basis])

  4015   have "x \<in> span d" if "\<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0" for x

  4016   proof -

  4017     have "finite d"

  4018       by (rule finite_subset [OF d finite_Basis])

  4019     then have "(\<Sum>i\<in>d. (x \<bullet> i) *\<^sub>R i) \<in> span d"

  4020       by (simp add: span_sum span_clauses)

  4021     also have "(\<Sum>i\<in>d. (x \<bullet> i) *\<^sub>R i) = (\<Sum>i\<in>Basis. (x \<bullet> i) *\<^sub>R i)"

  4022       by (rule sum.mono_neutral_cong_left [OF finite_Basis d]) (auto simp: that)

  4023     finally show "x \<in> span d"

  4024       by (simp only: euclidean_representation)

  4025   qed

  4026   then show "?A \<subseteq> span d" by auto

  4027 qed simp

  4028

  4029 text \<open>Hence closure and completeness of all subspaces.\<close>

  4030 lemma ex_card:

  4031   assumes "n \<le> card A"

  4032   shows "\<exists>S\<subseteq>A. card S = n"

  4033 proof (cases "finite A")

  4034   case True

  4035   from ex_bij_betw_nat_finite[OF this] obtain f where f: "bij_betw f {0..<card A} A" ..

  4036   moreover from f \<open>n \<le> card A\<close> have "{..< n} \<subseteq> {..< card A}" "inj_on f {..< n}"

  4037     by (auto simp: bij_betw_def intro: subset_inj_on)

  4038   ultimately have "f  {..< n} \<subseteq> A" "card (f  {..< n}) = n"

  4039     by (auto simp: bij_betw_def card_image)

  4040   then show ?thesis by blast

  4041 next

  4042   case False

  4043   with \<open>n \<le> card A\<close> show ?thesis by force

  4044 qed

  4045

  4046 lemma closed_subspace:

  4047   fixes s :: "'a::euclidean_space set"

  4048   assumes "subspace s"

  4049   shows "closed s"

  4050 proof -

  4051   have "dim s \<le> card (Basis :: 'a set)"

  4052     using dim_subset_UNIV by auto

  4053   with ex_card[OF this] obtain d :: "'a set" where t: "card d = dim s" and d: "d \<subseteq> Basis"

  4054     by auto

  4055   let ?t = "{x::'a. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"

  4056   have "\<exists>f. linear f \<and> f  {x::'a. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0} = s \<and>

  4057       inj_on f {x::'a. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0}"

  4058     using dim_substandard[of d] t d assms

  4059     by (intro subspace_isomorphism[OF subspace_substandard[of "\<lambda>i. i \<notin> d"]]) (auto simp: inner_Basis)

  4060   then obtain f where f:

  4061       "linear f"

  4062       "f  {x. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0} = s"

  4063       "inj_on f {x. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0}"

  4064     by blast

  4065   interpret f: bounded_linear f

  4066     using f by (simp add: linear_conv_bounded_linear)

  4067   have "x \<in> ?t \<Longrightarrow> f x = 0 \<Longrightarrow> x = 0" for x

  4068     using f.zero d f(3)[THEN inj_onD, of x 0] by auto

  4069   moreover have "closed ?t" by (rule closed_substandard)

  4070   moreover have "subspace ?t" by (rule subspace_substandard)

  4071   ultimately show ?thesis

  4072     using closed_injective_image_subspace[of ?t f]

  4073     unfolding f(2) using f(1) unfolding linear_conv_bounded_linear by auto

  4074 qed

  4075

  4076 lemma complete_subspace: "subspace s \<Longrightarrow> complete s"

  4077   for s :: "'a::euclidean_space set"

  4078   using complete_eq_closed closed_subspace by auto

  4079

  4080 lemma closed_span [iff]: "closed (span s)"

  4081   for s :: "'a::euclidean_space set"

  4082   by (simp add: closed_subspace)

  4083

  4084 lemma dim_closure [simp]: "dim (closure s) = dim s" (is "?dc = ?d")

  4085   for s :: "'a::euclidean_space set"

  4086 proof -

  4087   have "?dc \<le> ?d"

  4088     using closure_minimal[OF span_inc, of s]

  4089     using closed_subspace[OF subspace_span, of s]

  4090     using dim_subset[of "closure s" "span s"]

  4091     by simp

  4092   then show ?thesis

  4093     using dim_subset[OF closure_subset, of s]

  4094     by simp

  4095 qed

  4096

  4097

  4098 subsection \<open>Affine transformations of intervals\<close>

  4099

  4100 lemma real_affinity_le: "0 < m \<Longrightarrow> m * x + c \<le> y \<longleftrightarrow> x \<le> inverse m * y + - (c / m)"

  4101   for m :: "'a::linordered_field"

  4102   by (simp add: field_simps)

  4103

  4104 lemma real_le_affinity: "0 < m \<Longrightarrow> y \<le> m * x + c \<longleftrightarrow> inverse m * y + - (c / m) \<le> x"

  4105   for m :: "'a::linordered_field"

  4106   by (simp add: field_simps)

  4107

  4108 lemma real_affinity_lt: "0 < m \<Longrightarrow> m * x + c < y \<longleftrightarrow> x < inverse m * y + - (c / m)"

  4109   for m :: "'a::linordered_field"

  4110   by (simp add: field_simps)

  4111

  4112 lemma real_lt_affinity: "0 < m \<Longrightarrow> y < m * x + c \<longleftrightarrow> inverse m * y + - (c / m) < x"

  4113   for m :: "'a::linordered_field"

  4114   by (simp add: field_simps)

  4115

  4116 lemma real_affinity_eq: "m \<noteq> 0 \<Longrightarrow> m * x + c = y \<longleftrightarrow> x = inverse m * y + - (c / m)"

  4117   for m :: "'a::linordered_field"

  4118   by (simp add: field_simps)

  4119

  4120 lemma real_eq_affinity: "m \<noteq> 0 \<Longrightarrow> y = m * x + c  \<longleftrightarrow> inverse m * y + - (c / m) = x"

  4121   for m :: "'a::linordered_field"

  4122   by (simp add: field_simps)

  4123

  4124

  4125 subsection \<open>Banach fixed point theorem (not really topological ...)\<close>

  4126

  4127 theorem banach_fix:

  4128   assumes s: "complete s" "s \<noteq> {}"

  4129     and c: "0 \<le> c" "c < 1"

  4130     and f: "f  s \<subseteq> s"

  4131     and lipschitz: "\<forall>x\<in>s. \<forall>y\<in>s. dist (f x) (f y) \<le> c * dist x y"

  4132   shows "\<exists>!x\<in>s. f x = x"

  4133 proof -

  4134   from c have "1 - c > 0" by simp

  4135

  4136   from s(2) obtain z0 where z0: "z0 \<in> s" by blast

  4137   define z where "z n = (f ^^ n) z0" for n

  4138   with f z0 have z_in_s: "z n \<in> s" for n :: nat

  4139     by (induct n) auto

  4140   define d where "d = dist (z 0) (z 1)"

  4141

  4142   have fzn: "f (z n) = z (Suc n)" for n

  4143     by (simp add: z_def)

  4144   have cf_z: "dist (z n) (z (Suc n)) \<le> (c ^ n) * d" for n :: nat

  4145   proof (induct n)

  4146     case 0

  4147     then show ?case

  4148       by (simp add: d_def)

  4149   next

  4150     case (Suc m)

  4151     with \<open>0 \<le> c\<close> have "c * dist (z m) (z (Suc m)) \<le> c ^ Suc m * d"

  4152       using mult_left_mono[of "dist (z m) (z (Suc m))" "c ^ m * d" c] by simp

  4153     then show ?case

  4154       using lipschitz[THEN bspec[where x="z m"], OF z_in_s, THEN bspec[where x="z (Suc m)"], OF z_in_s]

  4155       by (simp add: fzn mult_le_cancel_left)

  4156   qed

  4157

  4158   have cf_z2: "(1 - c) * dist (z m) (z (m + n)) \<le> (c ^ m) * d * (1 - c ^ n)" for n m :: nat

  4159   proof (induct n)

  4160     case 0

  4161     show ?case by simp

  4162   next

  4163     case (Suc k)

  4164     from c have "(1 - c) * dist (z m) (z (m + Suc k)) \<le>

  4165         (1 - c) * (dist (z m) (z (m + k)) + dist (z (m + k)) (z (Suc (m + k))))"

  4166       by (simp add: dist_triangle)

  4167     also from c cf_z[of "m + k"] have "\<dots> \<le> (1 - c) * (dist (z m) (z (m + k)) + c ^ (m + k) * d)"

  4168       by simp

  4169     also from Suc have "\<dots> \<le> c ^ m * d * (1 - c ^ k) + (1 - c) * c ^ (m + k) * d"

  4170       by (simp add: field_simps)

  4171     also have "\<dots> = (c ^ m) * (d * (1 - c ^ k) + (1 - c) * c ^ k * d)"

  4172       by (simp add: power_add field_simps)

  4173     also from c have "\<dots> \<le> (c ^ m) * d * (1 - c ^ Suc k)"

  4174       by (simp add: field_simps)

  4175     finally show ?case by simp

  4176   qed

  4177

  4178   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

  4179   proof (cases "d = 0")

  4180     case True

  4181     from \<open>1 - c > 0\<close> have "(1 - c) * x \<le> 0 \<longleftrightarrow> x \<le> 0" for x

  4182       by (metis mult_zero_left mult.commute real_mult_le_cancel_iff1)

  4183     with c cf_z2[of 0] True have "z n = z0" for n

  4184       by (simp add: z_def)

  4185     with \<open>e > 0\<close> show ?thesis by simp

  4186   next

  4187     case False

  4188     with zero_le_dist[of "z 0" "z 1"] have "d > 0"

  4189       by (metis d_def less_le)

  4190     with \<open>1 - c > 0\<close> \<open>e > 0\<close> have "0 < e * (1 - c) / d"

  4191       by simp

  4192     with c obtain N where N: "c ^ N < e * (1 - c) / d"

  4193       using real_arch_pow_inv[of "e * (1 - c) / d" c] by auto

  4194     have *: "dist (z m) (z n) < e" if "m > n" and as: "m \<ge> N" "n \<ge> N" for m n :: nat

  4195     proof -

  4196       from c \<open>n \<ge> N\<close> have *: "c ^ n \<le> c ^ N"

  4197         using power_decreasing[OF \<open>n\<ge>N\<close>, of c] by simp

  4198       from c \<open>m > n\<close> have "1 - c ^ (m - n) > 0"

  4199         using power_strict_mono[of c 1 "m - n"] by simp

  4200       with \<open>d > 0\<close> \<open>0 < 1 - c\<close> have **: "d * (1 - c ^ (m - n)) / (1 - c) > 0"

  4201         by simp

  4202       from cf_z2[of n "m - n"] \<open>m > n\<close>

  4203       have "dist (z m) (z n) \<le> c ^ n * d * (1 - c ^ (m - n)) / (1 - c)"

  4204         by (simp add: pos_le_divide_eq[OF \<open>1 - c > 0\<close>] mult.commute dist_commute)

  4205       also have "\<dots> \<le> c ^ N * d * (1 - c ^ (m - n)) / (1 - c)"

  4206         using mult_right_mono[OF * order_less_imp_le[OF **]]

  4207         by (simp add: mult.assoc)

  4208       also have "\<dots> < (e * (1 - c) / d) * d * (1 - c ^ (m - n)) / (1 - c)"

  4209         using mult_strict_right_mono[OF N **] by (auto simp: mult.assoc)

  4210       also from c \<open>d > 0\<close> \<open>1 - c > 0\<close> have "\<dots> = e * (1 - c ^ (m - n))"

  4211         by simp

  4212       also from c \<open>1 - c ^ (m - n) > 0\<close> \<open>e > 0\<close> have "\<dots> \<le> e"

  4213         using mult_right_le_one_le[of e "1 - c ^ (m - n)"] by auto

  4214       finally show ?thesis by simp

  4215     qed

  4216     have "dist (z n) (z m) < e" if "N \<le> m" "N \<le> n" for m n :: nat

  4217     proof (cases "n = m")

  4218       case True

  4219       with \<open>e > 0\<close> show ?thesis by simp

  4220     next

  4221       case False

  4222       with *[of n m] *[of m n] and that show ?thesis

  4223         by (auto simp: dist_commute nat_neq_iff)

  4224     qed

  4225     then show ?thesis by auto

  4226   qed

  4227   then have "Cauchy z"

  4228     by (simp add: cauchy_def)

  4229   then obtain x where "x\<in>s" and x:"(z \<longlongrightarrow> x) sequentially"

  4230     using s(1)[unfolded compact_def complete_def, THEN spec[where x=z]] and z_in_s by auto

  4231

  4232   define e where "e = dist (f x) x"

  4233   have "e = 0"

  4234   proof (rule ccontr)

  4235     assume "e \<noteq> 0"

  4236     then have "e > 0"

  4237       unfolding e_def using zero_le_dist[of "f x" x]

  4238       by (metis dist_eq_0_iff dist_nz e_def)

  4239     then obtain N where N:"\<forall>n\<ge>N. dist (z n) x < e / 2"

  4240       using x[unfolded lim_sequentially, THEN spec[where x="e/2"]] by auto

  4241     then have N':"dist (z N) x < e / 2" by auto

  4242     have *: "c * dist (z N) x \<le> dist (z N) x"

  4243       unfolding mult_le_cancel_right2

  4244       using zero_le_dist[of "z N" x] and c

  4245       by (metis dist_eq_0_iff dist_nz order_less_asym less_le)

  4246     have "dist (f (z N)) (f x) \<le> c * dist (z N) x"

  4247       using lipschitz[THEN bspec[where x="z N"], THEN bspec[where x=x]]

  4248       using z_in_s[of N] \<open>x\<in>s\<close>

  4249       using c

  4250       by auto

  4251     also have "\<dots> < e / 2"

  4252       using N' and c using * by auto

  4253     finally show False

  4254       unfolding fzn

  4255       using N[THEN spec[where x="Suc N"]] and dist_triangle_half_r[of "z (Suc N)" "f x" e x]

  4256       unfolding e_def

  4257       by auto

  4258   qed

  4259   then have "f x = x" by (auto simp: e_def)

  4260   moreover have "y = x" if "f y = y" "y \<in> s" for y

  4261   proof -

  4262     from \<open>x \<in> s\<close> \<open>f x = x\<close> that have "dist x y \<le> c * dist x y"

  4263       using lipschitz[THEN bspec[where x=x], THEN bspec[where x=y]] by simp

  4264     with c and zero_le_dist[of x y] have "dist x y = 0"

  4265       by (simp add: mult_le_cancel_right1)

  4266     then show ?thesis by simp

  4267   qed

  4268   ultimately show ?thesis

  4269     using \<open>x\<in>s\<close> by blast

  4270 qed

  4271

  4272

  4273 subsection \<open>Edelstein fixed point theorem\<close>

  4274

  4275 theorem edelstein_fix:

  4276   fixes s :: "'a::metric_space set"

  4277   assumes s: "compact s" "s \<noteq> {}"

  4278     and gs: "(g  s) \<subseteq> s"

  4279     and dist: "\<forall>x\<in>s. \<forall>y\<in>s. x \<noteq> y \<longrightarrow> dist (g x) (g y) < dist x y"

  4280   shows "\<exists>!x\<in>s. g x = x"

  4281 proof -

  4282   let ?D = "(\<lambda>x. (x, x))  s"

  4283   have D: "compact ?D" "?D \<noteq> {}"

  4284     by (rule compact_continuous_image)

  4285        (auto intro!: s continuous_Pair continuous_ident simp: continuous_on_eq_continuous_within)

  4286

  4287   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"

  4288     using dist by fastforce

  4289   then have "continuous_on s g"

  4290     by (auto simp: continuous_on_iff)

  4291   then have cont: "continuous_on ?D (\<lambda>x. dist ((g \<circ> fst) x) (snd x))"

  4292     unfolding continuous_on_eq_continuous_within

  4293     by (intro continuous_dist ballI continuous_within_compose)

  4294        (auto intro!: continuous_fst continuous_snd continuous_ident simp: image_image)

  4295

  4296   obtain a where "a \<in> s" and le: "\<And>x. x \<in> s \<Longrightarrow> dist (g a) a \<le> dist (g x) x"

  4297     using continuous_attains_inf[OF D cont] by auto

  4298

  4299   have "g a = a"

  4300   proof (rule ccontr)

  4301     assume "g a \<noteq> a"

  4302     with \<open>a \<in> s\<close> gs have "dist (g (g a)) (g a) < dist (g a) a"

  4303       by (intro dist[rule_format]) auto

  4304     moreover have "dist (g a) a \<le> dist (g (g a)) (g a)"

  4305       using \<open>a \<in> s\<close> gs by (intro le) auto

  4306     ultimately show False by auto

  4307   qed

  4308   moreover have "\<And>x. x \<in> s \<Longrightarrow> g x = x \<Longrightarrow> x = a"

  4309     using dist[THEN bspec[where x=a]] \<open>g a = a\<close> and \<open>a\<in>s\<close> by auto

  4310   ultimately show "\<exists>!x\<in>s. g x = x"

  4311     using \<open>a \<in> s\<close> by blast

  4312 qed

  4313

  4314

  4315 lemma cball_subset_cball_iff:

  4316   fixes a :: "'a :: euclidean_space"

  4317   shows "cball a r \<subseteq> cball a' r' \<longleftrightarrow> dist a a' + r \<le> r' \<or> r < 0"

  4318     (is "?lhs \<longleftrightarrow> ?rhs")

  4319 proof

  4320   assume ?lhs

  4321   then show ?rhs

  4322   proof (cases "r < 0")

  4323     case True

  4324     then show ?rhs by simp

  4325   next

  4326     case False

  4327     then have [simp]: "r \<ge> 0" by simp

  4328     have "norm (a - a') + r \<le> r'"

  4329     proof (cases "a = a'")

  4330       case True

  4331       then show ?thesis

  4332         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>]

  4333         by (force simp: SOME_Basis dist_norm)

  4334     next

  4335       case False

  4336       have "norm (a' - (a + (r / norm (a - a')) *\<^sub>R (a - a'))) = norm (a' - a - (r / norm (a - a')) *\<^sub>R (a - a'))"

  4337         by (simp add: algebra_simps)

  4338       also have "... = norm ((-1 - (r / norm (a - a'))) *\<^sub>R (a - a'))"

  4339         by (simp add: algebra_simps)

  4340       also from \<open>a \<noteq> a'\<close> have "... = \<bar>- norm (a - a') - r\<bar>"

  4341         by (simp add: abs_mult_pos field_simps)

  4342       finally have [simp]: "norm (a' - (a + (r / norm (a - a')) *\<^sub>R (a - a'))) = \<bar>norm (a - a') + r\<bar>"

  4343         by linarith

  4344       from \<open>a \<noteq> a'\<close> show ?thesis

  4345         using subsetD [where c = "a' + (1 + r / norm(a - a')) *\<^sub>R (a - a')", OF \<open>?lhs\<close>]

  4346         by (simp add: dist_norm scaleR_add_left)

  4347     qed

  4348     then show ?rhs

  4349       by (simp add: dist_norm)

  4350   qed

  4351 next

  4352   assume ?rhs

  4353   then show ?lhs

  4354     by (auto simp: ball_def dist_norm)

  4355       (metis add.commute add_le_cancel_right dist_norm dist_triangle3 order_trans)

  4356 qed

  4357

  4358 lemma cball_subset_ball_iff: "cball a r \<subseteq> ball a' r' \<longleftrightarrow> dist a a' + r < r' \<or> r < 0"

  4359   (is "?lhs \<longleftrightarrow> ?rhs")

  4360   for a :: "'a::euclidean_space"

  4361 proof

  4362   assume ?lhs

  4363   then show ?rhs

  4364   proof (cases "r < 0")

  4365     case True then

  4366     show ?rhs by simp

  4367   next

  4368     case False

  4369     then have [simp]: "r \<ge> 0" by simp

  4370     have "norm (a - a') + r < r'"

  4371     proof (cases "a = a'")

  4372       case True

  4373       then show ?thesis

  4374         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>]

  4375         by (force simp: SOME_Basis dist_norm)

  4376     next

  4377       case False

  4378       have False if "norm (a - a') + r \<ge> r'"

  4379       proof -

  4380         from that have "\<bar>r' - norm (a - a')\<bar> \<le> r"

  4381           by (simp split: abs_split)

  4382             (metis \<open>0 \<le> r\<close> \<open>?lhs\<close> centre_in_cball dist_commute dist_norm less_asym mem_ball subset_eq)

  4383         then show ?thesis

  4384           using subsetD [where c = "a + (r' / norm(a - a') - 1) *\<^sub>R (a - a')", OF \<open>?lhs\<close>] \<open>a \<noteq> a'\<close>

  4385           by (simp add: dist_norm field_simps)

  4386             (simp add: diff_divide_distrib scaleR_left_diff_distrib)

  4387       qed

  4388       then show ?thesis by force

  4389     qed

  4390     then show ?rhs by (simp add: dist_norm)

  4391   qed

  4392 next

  4393   assume ?rhs

  4394   then show ?lhs

  4395     by (auto simp: ball_def dist_norm)

  4396       (metis add.commute add_le_cancel_right dist_norm dist_triangle3 le_less_trans)

  4397 qed

  4398

  4399 lemma ball_subset_cball_iff: "ball a r \<subseteq> cball a' r' \<longleftrightarrow> dist a a' + r \<le> r' \<or> r \<le> 0"

  4400   (is "?lhs = ?rhs")

  4401   for a :: "'a::euclidean_space"

  4402 proof (cases "r \<le> 0")

  4403   case True

  4404   then show ?thesis

  4405     using dist_not_less_zero less_le_trans by force

  4406 next

  4407   case False

  4408   show ?thesis

  4409   proof

  4410     assume ?lhs

  4411     then have "(cball a r \<subseteq> cball a' r')"

  4412       by (metis False closed_cball closure_ball closure_closed closure_mono not_less)

  4413     with False show ?rhs

  4414       by (fastforce iff: cball_subset_cball_iff)

  4415   next

  4416     assume ?rhs

  4417     with False show ?lhs

  4418       using ball_subset_cball cball_subset_cball_iff by blast

  4419   qed

  4420 qed

  4421

  4422 lemma ball_subset_ball_iff:

  4423   fixes a :: "'a :: euclidean_space"

  4424   shows "ball a r \<subseteq> ball a' r' \<longleftrightarrow> dist a a' + r \<le> r' \<or> r \<le> 0"

  4425         (is "?lhs = ?rhs")

  4426 proof (cases "r \<le> 0")

  4427   case True then show ?thesis

  4428     using dist_not_less_zero less_le_trans by force

  4429 next

  4430   case False show ?thesis

  4431   proof

  4432     assume ?lhs

  4433     then have "0 < r'"

  4434       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)

  4435     then have "(cball a r \<subseteq> cball a' r')"

  4436       by (metis False\<open>?lhs\<close> closure_ball closure_mono not_less)

  4437     then show ?rhs

  4438       using False cball_subset_cball_iff by fastforce

  4439   next

  4440   assume ?rhs then show ?lhs

  4441     apply (auto simp: ball_def)

  4442     apply (metis add.commute add_le_cancel_right dist_commute dist_triangle_lt not_le order_trans)

  4443     using dist_not_less_zero order.strict_trans2 apply blast

  4444     done

  4445   qed

  4446 qed

  4447

  4448

  4449 lemma ball_eq_ball_iff:

  4450   fixes x :: "'a :: euclidean_space"

  4451   shows "ball x d = ball y e \<longleftrightarrow> d \<le> 0 \<and> e \<le> 0 \<or> x=y \<and> d=e"

  4452         (is "?lhs = ?rhs")

  4453 proof

  4454   assume ?lhs

  4455   then show ?rhs

  4456   proof (cases "d \<le> 0 \<or> e \<le> 0")

  4457     case True

  4458       with \<open>?lhs\<close> show ?rhs

  4459         by safe (simp_all only: ball_eq_empty [of y e, symmetric] ball_eq_empty [of x d, symmetric])

  4460   next

  4461     case False

  4462     with \<open>?lhs\<close> show ?rhs

  4463       apply (auto simp: set_eq_subset ball_subset_ball_iff dist_norm norm_minus_commute algebra_simps)

  4464       apply (metis add_le_same_cancel1 le_add_same_cancel1 norm_ge_zero norm_pths(2) order_trans)

  4465       apply (metis add_increasing2 add_le_imp_le_right eq_iff norm_ge_zero)

  4466       done

  4467   qed

  4468 next

  4469   assume ?rhs then show ?lhs

  4470     by (auto simp: set_eq_subset ball_subset_ball_iff)

  4471 qed

  4472

  4473 lemma cball_eq_cball_iff:

  4474   fixes x :: "'a :: euclidean_space"

  4475   shows "cball x d = cball y e \<longleftrightarrow> d < 0 \<and> e < 0 \<or> x=y \<and> d=e"

  4476         (is "?lhs = ?rhs")

  4477 proof

  4478   assume ?lhs

  4479   then show ?rhs

  4480   proof (cases "d < 0 \<or> e < 0")

  4481     case True

  4482       with \<open>?lhs\<close> show ?rhs

  4483         by safe (simp_all only: cball_eq_empty [of y e, symmetric] cball_eq_empty [of x d, symmetric])

  4484   next

  4485     case False

  4486     with \<open>?lhs\<close> show ?rhs

  4487       apply (auto simp: set_eq_subset cball_subset_cball_iff dist_norm norm_minus_commute algebra_simps)

  4488       apply (metis add_le_same_cancel1 le_add_same_cancel1 norm_ge_zero norm_pths(2) order_trans)

  4489       apply (metis add_increasing2 add_le_imp_le_right eq_iff norm_ge_zero)

  4490       done

  4491   qed

  4492 next

  4493   assume ?rhs then show ?lhs

  4494     by (auto simp: set_eq_subset cball_subset_cball_iff)

  4495 qed

  4496

  4497 lemma ball_eq_cball_iff:

  4498   fixes x :: "'a :: euclidean_space"

  4499   shows "ball x d = cball y e \<longleftrightarrow> d \<le> 0 \<and> e < 0" (is "?lhs = ?rhs")

  4500 proof

  4501   assume ?lhs

  4502   then show ?rhs

  4503     apply (auto simp: set_eq_subset ball_subset_cball_iff cball_subset_ball_iff algebra_simps)

  4504     apply (metis add_increasing2 add_le_cancel_right add_less_same_cancel1 dist_not_less_zero less_le_trans zero_le_dist)

  4505     apply (metis add_less_same_cancel1 dist_not_less_zero less_le_trans not_le)

  4506     using \<open>?lhs\<close> ball_eq_empty cball_eq_empty apply blast+

  4507     done

  4508 next

  4509   assume ?rhs then show ?lhs by auto

  4510 qed

  4511

  4512 lemma cball_eq_ball_iff:

  4513   fixes x :: "'a :: euclidean_space"

  4514   shows "cball x d = ball y e \<longleftrightarrow> d < 0 \<and> e \<le> 0"

  4515   using ball_eq_cball_iff by blast

  4516

  4517 lemma finite_ball_avoid:

  4518   fixes S :: "'a :: euclidean_space set"

  4519   assumes "open S" "finite X" "p \<in> S"

  4520   shows "\<exists>e>0. \<forall>w\<in>ball p e. w\<in>S \<and> (w\<noteq>p \<longrightarrow> w\<notin>X)"

  4521 proof -

  4522   obtain e1 where "0 < e1" and e1_b:"ball p e1 \<subseteq> S"

  4523     using open_contains_ball_eq[OF \<open>open S\<close>] assms by auto

  4524   obtain e2 where "0 < e2" and "\<forall>x\<in>X. x \<noteq> p \<longrightarrow> e2 \<le> dist p x"

  4525     using finite_set_avoid[OF \<open>finite X\<close>,of p] by auto

  4526   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

  4527   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>

  4528     apply (rule_tac x="min e1 e2" in exI)

  4529     by auto

  4530 qed

  4531

  4532 lemma finite_cball_avoid:

  4533   fixes S :: "'a :: euclidean_space set"

  4534   assumes "open S" "finite X" "p \<in> S"

  4535   shows "\<exists>e>0. \<forall>w\<in>cball p e. w\<in>S \<and> (w\<noteq>p \<longrightarrow> w\<notin>X)"

  4536 proof -

  4537   obtain e1 where "e1>0" and e1: "\<forall>w\<in>ball p e1. w\<in>S \<and> (w\<noteq>p \<longrightarrow> w\<notin>X)"

  4538     using finite_ball_avoid[OF assms] by auto

  4539   define e2 where "e2 \<equiv> e1/2"

  4540   have "e2>0" and "e2 < e1" unfolding e2_def using \<open>e1>0\<close> by auto

  4541   then have "cball p e2 \<subseteq> ball p e1" by (subst cball_subset_ball_iff,auto)

  4542   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

  4543 qed

  4544

  4545 subsection\<open>Various separability-type properties\<close>

  4546

  4547 lemma univ_second_countable:

  4548   obtains \<B> :: "'a::euclidean_space set set"

  4549   where "countable \<B>" "\<And>C. C \<in> \<B> \<Longrightarrow> open C"

  4550        "\<And>S. open S \<Longrightarrow> \<exists>U. U \<subseteq> \<B> \<and> S = \<Union>U"

  4551 by (metis ex_countable_basis topological_basis_def)

  4552

  4553 lemma subset_second_countable:

  4554   obtains \<B> :: "'a:: euclidean_space set set"

  4555     where "countable \<B>"

  4556           "{} \<notin> \<B>"

  4557           "\<And>C. C \<in> \<B> \<Longrightarrow> openin(subtopology euclidean S) C"

  4558           "\<And>T. openin(subtopology euclidean S) T \<Longrightarrow> \<exists>\<U>. \<U> \<subseteq> \<B> \<and> T = \<Union>\<U>"

  4559 proof -

  4560   obtain \<B> :: "'a set set"

  4561     where "countable \<B>"

  4562       and opeB: "\<And>C. C \<in> \<B> \<Longrightarrow> openin(subtopology euclidean S) C"

  4563       and \<B>:    "\<And>T. openin(subtopology euclidean S) T \<Longrightarrow> \<exists>\<U>. \<U> \<subseteq> \<B> \<and> T = \<Union>\<U>"

  4564   proof -

  4565     obtain \<C> :: "'a set set"

  4566       where "countable \<C>" and ope: "\<And>C. C \<in> \<C> \<Longrightarrow> open C"

  4567         and \<C>: "\<And>S. open S \<Longrightarrow> \<exists>U. U \<subseteq> \<C> \<and> S = \<Union>U"

  4568       by (metis univ_second_countable that)

  4569     show ?thesis

  4570     proof

  4571       show "countable ((\<lambda>C. S \<inter> C)  \<C>)"

  4572         by (simp add: \<open>countable \<C>\<close>)

  4573       show "\<And>C. C \<in> op \<inter> S  \<C> \<Longrightarrow> openin (subtopology euclidean S) C"

  4574         using ope by auto

  4575       show "\<And>T. openin (subtopology euclidean S) T \<Longrightarrow> \<exists>\<U>\<subseteq>op \<inter> S  \<C>. T = \<Union>\<U>"

  4576         by (metis \<C> image_mono inf_Sup openin_open)

  4577     qed

  4578   qed

  4579   show ?thesis

  4580   proof

  4581     show "countable (\<B> - {{}})"

  4582       using \<open>countable \<B>\<close> by blast

  4583     show "\<And>C. \<lbrakk>C \<in> \<B> - {{}}\<rbrakk> \<Longrightarrow> openin (subtopology euclidean S) C"

  4584       by (simp add: \<open>\<And>C. C \<in> \<B> \<Longrightarrow> openin (subtopology euclidean S) C\<close>)

  4585     show "\<exists>\<U>\<subseteq>\<B> - {{}}. T = \<Union>\<U>" if "openin (subtopology euclidean S) T" for T

  4586       using \<B> [OF that]

  4587       apply clarify

  4588       apply (rule_tac x="\<U> - {{}}" in exI, auto)

  4589         done

  4590   qed auto

  4591 qed

  4592

  4593 lemma univ_second_countable_sequence:

  4594   obtains B :: "nat \<Rightarrow> 'a::euclidean_space set"

  4595     where "inj B" "\<And>n. open(B n)" "\<And>S. open S \<Longrightarrow> \<exists>k. S = \<Union>{B n |n. n \<in> k}"

  4596 proof -

  4597   obtain \<B> :: "'a set set"

  4598   where "countable \<B>"

  4599     and op: "\<And>C. C \<in> \<B> \<Longrightarrow> open C"

  4600     and Un: "\<And>S. open S \<Longrightarrow> \<exists>U. U \<subseteq> \<B> \<and> S = \<Union>U"

  4601     using univ_second_countable by blast

  4602   have *: "infinite (range (\<lambda>n. ball (0::'a) (inverse(Suc n))))"

  4603     apply (rule Infinite_Set.range_inj_infinite)

  4604     apply (simp add: inj_on_def ball_eq_ball_iff)

  4605     done

  4606   have "infinite \<B>"

  4607   proof

  4608     assume "finite \<B>"

  4609     then have "finite (Union  (Pow \<B>))"

  4610       by simp

  4611     then have "finite (range (\<lambda>n. ball (0::'a) (inverse(Suc n))))"

  4612       apply (rule rev_finite_subset)

  4613       by (metis (no_types, lifting) PowI image_eqI image_subset_iff Un [OF open_ball])

  4614     with * show False by simp

  4615   qed

  4616   obtain f :: "nat \<Rightarrow> 'a set" where "\<B> = range f" "inj f"

  4617     by (blast intro: countable_as_injective_image [OF \<open>countable \<B>\<close> \<open>infinite \<B>\<close>])

  4618   have *: "\<exists>k. S = \<Union>{f n |n. n \<in> k}" if "open S" for S

  4619     using Un [OF that]

  4620     apply clarify

  4621     apply (rule_tac x="f-U" in exI)

  4622     using \<open>inj f\<close> \<open>\<B> = range f\<close> apply force

  4623     done

  4624   show ?thesis

  4625     apply (rule that [OF \<open>inj f\<close> _ *])

  4626     apply (auto simp: \<open>\<B> = range f\<close> op)

  4627     done

  4628 qed

  4629

  4630 proposition separable:

  4631   fixes S :: "'a:: euclidean_space set"

  4632   obtains T where "countable T" "T \<subseteq> S" "S \<subseteq> closure T"

  4633 proof -

  4634   obtain \<B> :: "'a:: euclidean_space set set"

  4635     where "countable \<B>"

  4636       and "{} \<notin> \<B>"

  4637       and ope: "\<And>C. C \<in> \<B> \<Longrightarrow> openin(subtopology euclidean S) C"

  4638       and if_ope: "\<And>T. openin(subtopology euclidean S) T \<Longrightarrow> \<exists>\<U>. \<U> \<subseteq> \<B> \<and> T = \<Union>\<U>"

  4639     by (meson subset_second_countable)

  4640   then obtain f where f: "\<And>C. C \<in> \<B> \<Longrightarrow> f C \<in> C"

  4641     by (metis equals0I)

  4642   show ?thesis

  4643   proof

  4644     show "countable (f  \<B>)"

  4645       by (simp add: \<open>countable \<B>\<close>)

  4646     show "f  \<B> \<subseteq> S"

  4647       using ope f openin_imp_subset by blast

  4648     show "S \<subseteq> closure (f  \<B>)"

  4649     proof (clarsimp simp: closure_approachable)

  4650       fix x and e::real

  4651       assume "x \<in> S" "0 < e"

  4652       have "openin (subtopology euclidean S) (S \<inter> ball x e)"

  4653         by (simp add: openin_Int_open)

  4654       with if_ope obtain \<U> where  \<U>: "\<U> \<subseteq> \<B>" "S \<inter> ball x e = \<Union>\<U>"

  4655         by meson

  4656       show "\<exists>C \<in> \<B>. dist (f C) x < e"

  4657       proof (cases "\<U> = {}")

  4658         case True

  4659         then show ?thesis

  4660           using \<open>0 < e\<close>  \<U> \<open>x \<in> S\<close> by auto

  4661       next

  4662         case False

  4663         then obtain C where "C \<in> \<U>" by blast

  4664         show ?thesis

  4665         proof

  4666           show "dist (f C) x < e"

  4667             by (metis Int_iff Union_iff \<U> \<open>C \<in> \<U>\<close> dist_commute f mem_ball subsetCE)

  4668           show "C \<in> \<B>"

  4669             using \<open>\<U> \<subseteq> \<B>\<close> \<open>C \<in> \<U>\<close> by blast

  4670         qed

  4671       qed

  4672     qed

  4673   qed

  4674 qed

  4675

  4676 proposition Lindelof:

  4677   fixes \<F> :: "'a::euclidean_space set set"

  4678   assumes \<F>: "\<And>S. S \<in> \<F> \<Longrightarrow> open S"

  4679   obtains \<F>' where "\<F>' \<subseteq> \<F>" "countable \<F>'" "\<Union>\<F>' = \<Union>\<F>"

  4680 proof -

  4681   obtain \<B> :: "'a set set"

  4682     where "countable \<B>" "\<And>C. C \<in> \<B> \<Longrightarrow> open C"

  4683       and \<B>: "\<And>S. open S \<Longrightarrow> \<exists>U. U \<subseteq> \<B> \<and> S = \<Union>U"

  4684     using univ_second_countable by blast

  4685   define \<D> where "\<D> \<equiv> {S. S \<in> \<B> \<and> (\<exists>U. U \<in> \<F> \<and> S \<subseteq> U)}"

  4686   have "countable \<D>"

  4687     apply (rule countable_subset [OF _ \<open>countable \<B>\<close>])

  4688     apply (force simp: \<D>_def)

  4689     done

  4690   have "\<And>S. \<exists>U. S \<in> \<D> \<longrightarrow> U \<in> \<F> \<and> S \<subseteq> U"

  4691     by (simp add: \<D>_def)

  4692   then obtain G where G: "\<And>S. S \<in> \<D> \<longrightarrow> G S \<in> \<F> \<and> S \<subseteq> G S"

  4693     by metis

  4694   have "\<Union>\<F> \<subseteq> \<Union>\<D>"

  4695     unfolding \<D>_def by (blast dest: \<F> \<B>)

  4696   moreover have "\<Union>\<D> \<subseteq> \<Union>\<F>"

  4697     using \<D>_def by blast

  4698   ultimately have eq1: "\<Union>\<F> = \<Union>\<D>" ..

  4699   have eq2: "\<Union>\<D> = UNION \<D> G"

  4700     using G eq1 by auto

  4701   show ?thesis

  4702     apply (rule_tac \<F>' = "G  \<D>" in that)

  4703     using G \<open>countable \<D>\<close>  apply (auto simp: eq1 eq2)

  4704     done

  4705 qed

  4706

  4707 lemma Lindelof_openin:

  4708   fixes \<F> :: "'a::euclidean_space set set"

  4709   assumes "\<And>S. S \<in> \<F> \<Longrightarrow> openin (subtopology euclidean U) S"

  4710   obtains \<F>' where "\<F>' \<subseteq> \<F>" "countable \<F>'" "\<Union>\<F>' = \<Union>\<F>"

  4711 proof -

  4712   have "\<And>S. S \<in> \<F> \<Longrightarrow> \<exists>T. open T \<and> S = U \<inter> T"

  4713     using assms by (simp add: openin_open)

  4714   then obtain tf where tf: "\<And>S. S \<in> \<F> \<Longrightarrow> open (tf S) \<and> (S = U \<inter> tf S)"

  4715     by metis

  4716   have [simp]: "\<And>\<F>'. \<F>' \<subseteq> \<F> \<Longrightarrow> \<Union>\<F>' = U \<inter> \<Union>(tf  \<F>')"

  4717     using tf by fastforce

  4718   obtain \<G> where "countable \<G> \<and> \<G> \<subseteq> tf  \<F>" "\<Union>\<G> = UNION \<F> tf"

  4719     using tf by (force intro: Lindelof [of "tf  \<F>"])

  4720   then obtain \<F>' where \<F>': "\<F>' \<subseteq> \<F>" "countable \<F>'" "\<Union>\<F>' = \<Union>\<F>"

  4721     by (clarsimp simp add: countable_subset_image)

  4722   then show ?thesis ..

  4723 qed

  4724

  4725 lemma countable_disjoint_open_subsets:

  4726   fixes \<F> :: "'a::euclidean_space set set"

  4727   assumes "\<And>S. S \<in> \<F> \<Longrightarrow> open S" and pw: "pairwise disjnt \<F>"

  4728     shows "countable \<F>"

  4729 proof -

  4730   obtain \<F>' where "\<F>' \<subseteq> \<F>" "countable \<F>'" "\<Union>\<F>' = \<Union>\<F>"

  4731     by (meson assms Lindelof)

  4732   with pw have "\<F> \<subseteq> insert {} \<F>'"

  4733     by (fastforce simp add: pairwise_def disjnt_iff)

  4734   then show ?thesis

  4735     by (simp add: \<open>countable \<F>'\<close> countable_subset)

  4736 qed

  4737

  4738 lemma closedin_compact:

  4739    "\<lbrakk>compact S; closedin (subtopology euclidean S) T\<rbrakk> \<Longrightarrow> compact T"

  4740 by (metis closedin_closed compact_Int_closed)

  4741

  4742 lemma closedin_compact_eq:

  4743   fixes S :: "'a::t2_space set"

  4744   shows

  4745    "compact S

  4746          \<Longrightarrow> (closedin (subtopology euclidean S) T \<longleftrightarrow>

  4747               compact T \<and> T \<subseteq> S)"

  4748 by (metis closedin_imp_subset closedin_compact closed_subset compact_imp_closed)

  4749

  4750 lemma continuous_imp_closed_map:

  4751   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"

  4752   assumes "closedin (subtopology euclidean S) U"

  4753           "continuous_on S f" "image f S = T" "compact S"

  4754     shows "closedin (subtopology euclidean T) (image f U)"

  4755   by (metis assms closedin_compact_eq compact_continuous_image continuous_on_subset subset_image_iff)

  4756

  4757 lemma continuous_imp_quotient_map:

  4758   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"

  4759   assumes "continuous_on S f" "image f S = T" "compact S" "U \<subseteq> T"

  4760     shows "openin (subtopology euclidean S) {x. x \<in> S \<and> f x \<in> U} \<longleftrightarrow>

  4761            openin (subtopology euclidean T) U"

  4762   by (metis (no_types, lifting) Collect_cong assms closed_map_imp_quotient_map continuous_imp_closed_map)

  4763

  4764

  4765 subsection\<open> Finite intersection property\<close>

  4766

  4767 text\<open>Also developed in HOL's toplogical spaces theory, but the Heine-Borel type class isn't available there.\<close>

  4768

  4769 lemma closed_imp_fip:

  4770   fixes S :: "'a::heine_borel set"

  4771   assumes "closed S"

  4772       and T: "T \<in> \<F>" "bounded T"

  4773       and clof: "\<And>T. T \<in> \<F> \<Longrightarrow> closed T"

  4774       and none: "\<And>\<F>'. \<lbrakk>finite \<F>'; \<F>' \<subseteq> \<F>\<rbrakk> \<Longrightarrow> S \<inter> \<Inter>\<F>' \<noteq> {}"

  4775     shows "S \<inter> \<Inter>\<F> \<noteq> {}"

  4776 proof -

  4777   have "compact (S \<inter> T)"

  4778     using \<open>closed S\<close> clof compact_eq_bounded_closed T by blast

  4779   then have "(S \<inter> T) \<inter> \<Inter>\<F> \<noteq> {}"

  4780     apply (rule compact_imp_fip)

  4781      apply (simp add: clof)

  4782     by (metis Int_assoc complete_lattice_class.Inf_insert finite_insert insert_subset none \<open>T \<in> \<F>\<close>)

  4783   then show ?thesis by blast

  4784 qed

  4785

  4786 lemma closed_imp_fip_compact:

  4787   fixes S :: "'a::heine_borel set"

  4788   shows

  4789    "\<lbrakk>closed S; \<And>T. T \<in> \<F> \<Longrightarrow> compact T;

  4790      \<And>\<F>'. \<lbrakk>finite \<F>'; \<F>' \<subseteq> \<F>\<rbrakk> \<Longrightarrow> S \<inter> \<Inter>\<F>' \<noteq> {}\<rbrakk>

  4791         \<Longrightarrow> S \<inter> \<Inter>\<F> \<noteq> {}"

  4792 by (metis Inf_greatest closed_imp_fip compact_eq_bounded_closed empty_subsetI finite.emptyI inf.orderE)

  4793

  4794 lemma closed_fip_heine_borel:

  4795   fixes \<F> :: "'a::heine_borel set set"

  4796   assumes "closed S" "T \<in> \<F>" "bounded T"

  4797       and "\<And>T. T \<in> \<F> \<Longrightarrow> closed T"

  4798       and "\<And>\<F>'. \<lbrakk>finite \<F>'; \<F>' \<subseteq> \<F>\<rbrakk> \<Longrightarrow> \<Inter>\<F>' \<noteq> {}"

  4799     shows "\<Inter>\<F> \<noteq> {}"

  4800 proof -

  4801   have "UNIV \<inter> \<Inter>\<F> \<noteq> {}"

  4802     using assms closed_imp_fip [OF closed_UNIV] by auto

  4803   then show ?thesis by simp

  4804 qed

  4805

  4806 lemma compact_fip_heine_borel:

  4807   fixes \<F> :: "'a::heine_borel set set"

  4808   assumes clof: "\<And>T. T \<in> \<F> \<Longrightarrow> compact T"

  4809       and none: "\<And>\<F>'. \<lbrakk>finite \<F>'; \<F>' \<subseteq> \<F>\<rbrakk> \<Longrightarrow> \<Inter>\<F>' \<noteq> {}"

  4810     shows "\<Inter>\<F> \<noteq> {}"

  4811 by (metis InterI all_not_in_conv clof closed_fip_heine_borel compact_eq_bounded_closed none)

  4812

  4813 lemma compact_sequence_with_limit:

  4814   fixes f :: "nat \<Rightarrow> 'a::heine_borel"

  4815   shows "(f \<longlongrightarrow> l) sequentially \<Longrightarrow> compact (insert l (range f))"

  4816 apply (simp add: compact_eq_bounded_closed, auto)

  4817 apply (simp add: convergent_imp_bounded)

  4818 by (simp add: closed_limpt islimpt_insert sequence_unique_limpt)

  4819

  4820

  4821 subsection\<open>Componentwise limits and continuity\<close>

  4822

  4823 text\<open>But is the premise really necessary? Need to generalise @{thm euclidean_dist_l2}\<close>

  4824 lemma Euclidean_dist_upper: "i \<in> Basis \<Longrightarrow> dist (x \<bullet> i) (y \<bullet> i) \<le> dist x y"

  4825   by (metis (no_types) member_le_setL2 euclidean_dist_l2 finite_Basis)

  4826

  4827 text\<open>But is the premise @{term \<open>i \<in> Basis\<close>} really necessary?\<close>

  4828 lemma open_preimage_inner:

  4829   assumes "open S" "i \<in> Basis"

  4830     shows "open {x. x \<bullet> i \<in> S}"

  4831 proof (rule openI, simp)

  4832   fix x

  4833   assume x: "x \<bullet> i \<in> S"

  4834   with assms obtain e where "0 < e" and e: "ball (x \<bullet> i) e \<subseteq> S"

  4835     by (auto simp: open_contains_ball_eq)

  4836   have "\<exists>e>0. ball (y \<bullet> i) e \<subseteq> S" if dxy: "dist x y < e / 2" for y

  4837   proof (intro exI conjI)

  4838     have "dist (x \<bullet> i) (y \<bullet> i) < e / 2"

  4839       by (meson \<open>i \<in> Basis\<close> dual_order.trans Euclidean_dist_upper not_le that)

  4840     then have "dist (x \<bullet> i) z < e" if "dist (y \<bullet> i) z < e / 2" for z

  4841       by (metis dist_commute dist_triangle_half_l that)

  4842     then have "ball (y \<bullet> i) (e / 2) \<subseteq> ball (x \<bullet> i) e"

  4843       using mem_ball by blast

  4844       with e show "ball (y \<bullet> i) (e / 2) \<subseteq> S"

  4845         by (metis order_trans)

  4846   qed (simp add: \<open>0 < e\<close>)

  4847   then show "\<exists>e>0. ball x e \<subseteq> {s. s \<bullet> i \<in> S}"

  4848     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)

  4849 qed

  4850

  4851 proposition tendsto_componentwise_iff:

  4852   fixes f :: "_ \<Rightarrow> 'b::euclidean_space"

  4853   shows "(f \<longlongrightarrow> l) F \<longleftrightarrow> (\<forall>i \<in> Basis. ((\<lambda>x. (f x \<bullet> i)) \<longlongrightarrow> (l \<bullet> i)) F)"

  4854          (is "?lhs = ?rhs")

  4855 proof

  4856   assume ?lhs

  4857   then show ?rhs

  4858     unfolding tendsto_def

  4859     apply clarify

  4860     apply (drule_tac x="{s. s \<bullet> i \<in> S}" in spec)

  4861     apply (auto simp: open_preimage_inner)

  4862     done

  4863 next

  4864   assume R: ?rhs

  4865   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"

  4866     unfolding tendsto_iff by blast

  4867   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"

  4868       by (simp add: eventually_ball_finite_distrib [symmetric])

  4869   show ?lhs

  4870   unfolding tendsto_iff

  4871   proof clarify

  4872     fix e::real

  4873     assume "0 < e"

  4874     have *: "setL2 (\<lambda>i. dist (f x \<bullet> i) (l \<bullet> i)) Basis < e"

  4875              if "\<forall>i\<in>Basis. dist (f x \<bullet> i) (l \<bullet> i) < e / real DIM('b)" for x

  4876     proof -

  4877       have "setL2 (\<lambda>i. dist (f x \<bullet> i) (l \<bullet> i)) Basis \<le> sum (\<lambda>i. dist (f x \<bullet> i) (l \<bullet> i)) Basis"

  4878         by (simp add: setL2_le_sum)

  4879       also have "... < DIM('b) * (e / real DIM('b))"

  4880         apply (rule sum_bounded_above_strict)

  4881         using that by auto

  4882       also have "... = e"

  4883         by (simp add: field_simps)

  4884       finally show "setL2 (\<lambda>i. dist (f x \<bullet> i) (l \<bullet> i)) Basis < e" .

  4885     qed

  4886     have "\<forall>\<^sub>F x in F. \<forall>i\<in>Basis. dist (f x \<bullet> i) (l \<bullet> i) < e / DIM('b)"

  4887       apply (rule R')

  4888       using \<open>0 < e\<close> by simp

  4889     then show "\<forall>\<^sub>F x in F. dist (f x) l < e"

  4890       apply (rule eventually_mono)

  4891       apply (subst euclidean_dist_l2)

  4892       using * by blast

  4893   qed

  4894 qed

  4895

  4896

  4897 corollary continuous_componentwise:

  4898    "continuous F f \<longleftrightarrow> (\<forall>i \<in> Basis. continuous F (\<lambda>x. (f x \<bullet> i)))"

  4899 by (simp add: continuous_def tendsto_componentwise_iff [symmetric])

  4900

  4901 corollary continuous_on_componentwise:

  4902   fixes S :: "'a :: t2_space set"

  4903   shows "continuous_on S f \<longleftrightarrow> (\<forall>i \<in> Basis. continuous_on S (\<lambda>x. (f x \<bullet> i)))"

  4904   apply (simp add: continuous_on_eq_continuous_within)

  4905   using continuous_componentwise by blast

  4906

  4907 lemma linear_componentwise_iff:

  4908      "(linear f') \<longleftrightarrow> (\<forall>i\<in>Basis. linear (\<lambda>x. f' x \<bullet> i))"

  4909   apply (auto simp: linear_iff inner_left_distrib)

  4910    apply (metis inner_left_distrib euclidean_eq_iff)

  4911   by (metis euclidean_eqI inner_scaleR_left)

  4912

  4913 lemma bounded_linear_componentwise_iff:

  4914      "(bounded_linear f') \<longleftrightarrow> (\<forall>i\<in>Basis. bounded_linear (\<lambda>x. f' x \<bullet> i))"

  4915      (is "?lhs = ?rhs")

  4916 proof

  4917   assume ?lhs then show ?rhs

  4918     by (simp add: bounded_linear_inner_left_comp)

  4919 next

  4920   assume ?rhs

  4921   then have "(\<forall>i\<in>Basis. \<exists>K. \<forall>x. \<bar>f' x \<bullet> i\<bar> \<le> norm x * K)" "linear f'"

  4922     by (auto simp: bounded_linear_def bounded_linear_axioms_def linear_componentwise_iff [symmetric] ball_conj_distrib)

  4923   then obtain F where F: "\<And>i x. i \<in> Basis \<Longrightarrow> \<bar>f' x \<bullet> i\<bar> \<le> norm x * F i"

  4924     by metis

  4925   have "norm (f' x) \<le> norm x * sum F Basis" for x

  4926   proof -

  4927     have "norm (f' x) \<le> (\<Sum>i\<in>Basis. \<bar>f' x \<bullet> i\<bar>)"

  4928       by (rule norm_le_l1)

  4929     also have "... \<le> (\<Sum>i\<in>Basis. norm x * F i)"

  4930       by (metis F sum_mono)

  4931     also have "... = norm x * sum F Basis"

  4932       by (simp add: sum_distrib_left)

  4933     finally show ?thesis .

  4934   qed

  4935   then show ?lhs

  4936     by (force simp: bounded_linear_def bounded_linear_axioms_def \<open>linear f'\<close>)

  4937 qed

  4938

  4939 subsection\<open>Pasting functions together\<close>

  4940

  4941 subsubsection\<open>on open sets\<close>

  4942

  4943 lemma pasting_lemma:

  4944   fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::topological_space"

  4945   assumes clo: "\<And>i. i \<in> I \<Longrightarrow> openin (subtopology euclidean S) (T i)"

  4946       and cont: "\<And>i. i \<in> I \<Longrightarrow> continuous_on (T i) (f i)"

  4947       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"

  4948       and g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>j. j \<in> I \<and> x \<in> T j \<and> g x = f j x"

  4949     shows "continuous_on S g"

  4950 proof (clarsimp simp: continuous_openin_preimage_eq)

  4951   fix U :: "'b set"

  4952   assume "open U"

  4953   have S: "\<And>i. i \<in> I \<Longrightarrow> (T i) \<subseteq> S"

  4954     using clo openin_imp_subset by blast

  4955   have *: "{x \<in> S. g x \<in> U} = \<Union>{{x. x \<in> (T i) \<and> (f i x) \<in> U} |i. i \<in> I}"

  4956     apply (auto simp: dest: S)

  4957       apply (metis (no_types, lifting) g mem_Collect_eq)

  4958     using clo f g openin_imp_subset by fastforce

  4959   show "openin (subtopology euclidean S) {x \<in> S. g x \<in> U}"

  4960     apply (subst *)

  4961     apply (rule openin_Union, clarify)

  4962     apply (metis (full_types) \<open>open U\<close> cont clo openin_trans continuous_openin_preimage_gen)

  4963     done

  4964 qed

  4965

  4966 lemma pasting_lemma_exists:

  4967   fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::topological_space"

  4968   assumes S: "S \<subseteq> (\<Union>i \<in> I. T i)"

  4969       and clo: "\<And>i. i \<in> I \<Longrightarrow> openin (subtopology euclidean S) (T i)"

  4970       and cont: "\<And>i. i \<in> I \<Longrightarrow> continuous_on (T i) (f i)"

  4971       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"

  4972     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"

  4973 proof

  4974   show "continuous_on S (\<lambda>x. f (SOME i. i \<in> I \<and> x \<in> T i) x)"

  4975     apply (rule pasting_lemma [OF clo cont])

  4976      apply (blast intro: f)+

  4977     apply (metis (mono_tags, lifting) S UN_iff subsetCE someI)

  4978     done

  4979 next

  4980   fix x i

  4981   assume "i \<in> I" "x \<in> S \<inter> T i"

  4982   then show "f (SOME i. i \<in> I \<and> x \<in> T i) x = f i x"

  4983     by (metis (no_types, lifting) IntD2 IntI f someI_ex)

  4984 qed

  4985

  4986 subsubsection\<open>Likewise on closed sets, with a finiteness assumption\<close>

  4987

  4988 lemma pasting_lemma_closed:

  4989   fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::topological_space"

  4990   assumes "finite I"

  4991       and clo: "\<And>i. i \<in> I \<Longrightarrow> closedin (subtopology euclidean S) (T i)"

  4992       and cont: "\<And>i. i \<in> I \<Longrightarrow> continuous_on (T i) (f i)"

  4993       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"

  4994       and g: "\<And>x. x \<in> S \<Longrightarrow> \<exists>j. j \<in> I \<and> x \<in> T j \<and> g x = f j x"

  4995     shows "continuous_on S g"

  4996 proof (clarsimp simp: continuous_closedin_preimage_eq)

  4997   fix U :: "'b set"

  4998   assume "closed U"

  4999   have *: "{x \<in> S. g x \<in> U} = \<Union>{{x. x \<in> (T i) \<and> (f i x) \<in> U} |i. i \<in> I}"

  5000     apply auto

  5001     apply (metis (no_types, lifting) g mem_Collect_eq)

  5002     using clo closedin_closed apply blast

  5003     apply (metis Int_iff f g clo closedin_limpt inf.absorb_iff2)

  5004     done

  5005   show "closedin (subtopology euclidean S) {x \<in> S. g x \<in> U}"

  5006     apply (subst *)

  5007     apply (rule closedin_Union)

  5008     using \<open>finite I\<close> apply simp

  5009     apply (blast intro: \<open>closed U\<close> continuous_closedin_preimage cont clo closedin_trans)

  5010     done

  5011 qed

  5012

  5013 lemma pasting_lemma_exists_closed:

  5014   fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::topological_space"

  5015   assumes "finite I"

  5016       and S: "S \<subseteq> (\<Union>i \<in> I. T i)"

  5017       and clo: "\<And>i. i \<in> I \<Longrightarrow> closedin (subtopology euclidean S) (T i)"

  5018       and cont: "\<And>i. i \<in> I \<Longrightarrow> continuous_on (T i) (f i)"

  5019       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"

  5020     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"

  5021 proof

  5022   show "continuous_on S (\<lambda>x. f (SOME i. i \<in> I \<and> x \<in> T i) x)"

  5023     apply (rule pasting_lemma_closed [OF \<open>finite I\<close> clo cont])

  5024      apply (blast intro: f)+

  5025     apply (metis (mono_tags, lifting) S UN_iff subsetCE someI)

  5026     done

  5027 next

  5028   fix x i

  5029   assume "i \<in> I" "x \<in> S \<inter> T i"

  5030   then show "f (SOME i. i \<in> I \<and> x \<in> T i) x = f i x"

  5031     by (metis (no_types, lifting) IntD2 IntI f someI_ex)

  5032 qed

  5033

  5034 lemma tube_lemma:

  5035   assumes "compact K"

  5036   assumes "open W"

  5037   assumes "{x0} \<times> K \<subseteq> W"

  5038   shows "\<exists>X0. x0 \<in> X0 \<and> open X0 \<and> X0 \<times> K \<subseteq> W"

  5039 proof -

  5040   {

  5041     fix y assume "y \<in> K"

  5042     then have "(x0, y) \<in> W" using assms by auto

  5043     with \<open>open W\<close>

  5044     have "\<exists>X0 Y. open X0 \<and> open Y \<and> x0 \<in> X0 \<and> y \<in> Y \<and> X0 \<times> Y \<subseteq> W"

  5045       by (rule open_prod_elim) blast

  5046   }

  5047   then obtain X0 Y where

  5048     *: "\<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"

  5049     by metis

  5050   from * have "\<forall>t\<in>Y  K. open t" "K \<subseteq> \<Union>(Y  K)" by auto

  5051   with \<open>compact K\<close> obtain CC where CC: "CC \<subseteq> Y  K" "finite CC" "K \<subseteq> \<Union>CC"

  5052     by (meson compactE)

  5053   then obtain c where c: "\<And>C. C \<in> CC \<Longrightarrow> c C \<in> K \<and> C = Y (c C)"

  5054     by (force intro!: choice)

  5055   with * CC show ?thesis

  5056     by (force intro!: exI[where x="\<Inter>C\<in>CC. X0 (c C)"]) (* SLOW *)

  5057 qed

  5058

  5059 lemma continuous_on_prod_compactE:

  5060   fixes fx::"'a::topological_space \<times> 'b::topological_space \<Rightarrow> 'c::metric_space"

  5061     and e::real

  5062   assumes cont_fx: "continuous_on (U \<times> C) fx"

  5063   assumes "compact C"

  5064   assumes [intro]: "x0 \<in> U"

  5065   notes [continuous_intros] = continuous_on_compose2[OF cont_fx]

  5066   assumes "e > 0"

  5067   obtains X0 where "x0 \<in> X0" "open X0"

  5068     "\<forall>x\<in>X0 \<inter> U. \<forall>t \<in> C. dist (fx (x, t)) (fx (x0, t)) \<le> e"

  5069 proof -

  5070   define psi where "psi = (\<lambda>(x, t). dist (fx (x, t)) (fx (x0, t)))"

  5071   define W0 where "W0 = {(x, t) \<in> U \<times> C. psi (x, t) < e}"

  5072   have W0_eq: "W0 = psi - {..<e} \<inter> U \<times> C"

  5073     by (auto simp: vimage_def W0_def)

  5074   have "open {..<e}" by simp

  5075   have "continuous_on (U \<times> C) psi"

  5076     by (auto intro!: continuous_intros simp: psi_def split_beta')

  5077   from this[unfolded continuous_on_open_invariant, rule_format, OF \<open>open {..<e}\<close>]

  5078   obtain W where W: "open W" "W \<inter> U \<times> C = W0 \<inter> U \<times> C"

  5079     unfolding W0_eq by blast

  5080   have "{x0} \<times> C \<subseteq> W \<inter> U \<times> C"

  5081     unfolding W

  5082     by (auto simp: W0_def psi_def \<open>0 < e\<close>)

  5083   then have "{x0} \<times> C \<subseteq> W" by blast

  5084   from tube_lemma[OF \<open>compact C\<close> \<open>open W\<close> this]

  5085   obtain X0 where X0: "x0 \<in> X0" "open X0" "X0 \<times> C \<subseteq> W"

  5086     by blast

  5087

  5088   have "\<forall>x\<in>X0 \<inter> U. \<forall>t \<in> C. dist (fx (x, t)) (fx (x0, t)) \<le> e"

  5089   proof safe

  5090     fix x assume x: "x \<in> X0" "x \<in> U"

  5091     fix t assume t: "t \<in> C"

  5092     have "dist (fx (x, t)) (fx (x0, t)) = psi (x, t)"

  5093       by (auto simp: psi_def)

  5094     also

  5095     {

  5096       have "(x, t) \<in> X0 \<times> C"

  5097         using t x

  5098         by auto

  5099       also note \<open>\<dots> \<subseteq> W\<close>

  5100       finally have "(x, t) \<in> W" .

  5101       with t x have "(x, t) \<in> W \<inter> U \<times> C"

  5102         by blast

  5103       also note \<open>W \<inter> U \<times> C = W0 \<inter> U \<times> C\<close>

  5104       finally  have "psi (x, t) < e"

  5105         by (auto simp: W0_def)

  5106     }

  5107     finally show "dist (fx (x, t)) (fx (x0, t)) \<le> e" by simp

  5108   qed

  5109   from X0(1,2) this show ?thesis ..

  5110 qed

  5111

  5112

  5113 subsection\<open>Constancy of a function from a connected set into a finite, disconnected or discrete set\<close>

  5114

  5115 text\<open>Still missing: versions for a set that is smaller than R, or countable.\<close>

  5116

  5117 lemma continuous_disconnected_range_constant:

  5118   assumes S: "connected S"

  5119       and conf: "continuous_on S f"

  5120       and fim: "f  S \<subseteq> t"

  5121       and cct: "\<And>y. y \<in> t \<Longrightarrow> connected_component_set t y = {y}"

  5122     shows "\<exists>a. \<forall>x \<in> S. f x = a"

  5123 proof (cases "S = {}")

  5124   case True then show ?thesis by force

  5125 next

  5126   case False

  5127   { fix x assume "x \<in> S"

  5128     then have "f  S \<subseteq> {f x}"

  5129     by (metis connected_continuous_image conf connected_component_maximal fim image_subset_iff rev_image_eqI S cct)

  5130   }

  5131   with False show ?thesis

  5132     by blast

  5133 qed

  5134

  5135 lemma discrete_subset_disconnected:

  5136   fixes S :: "'a::topological_space set"

  5137   fixes t :: "'b::real_normed_vector set"

  5138   assumes conf: "continuous_on S f"

  5139       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)"

  5140    shows "f  S \<subseteq> {y. connected_component_set (f  S) y = {y}}"

  5141 proof -

  5142   { fix x assume x: "x \<in> S"

  5143     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)"

  5144       using conf no [OF x] by auto

  5145     then have e2: "0 \<le> e / 2"

  5146       by simp

  5147     have "f y = f x" if "y \<in> S" and ccs: "f y \<in> connected_component_set (f  S) (f x)" for y

  5148       apply (rule ccontr)

  5149       using connected_closed [of "connected_component_set (f  S) (f x)"] \<open>e>0\<close>

  5150       apply (simp add: del: ex_simps)

  5151       apply (drule spec [where x="cball (f x) (e / 2)"])

  5152       apply (drule spec [where x="- ball(f x) e"])

  5153       apply (auto simp: dist_norm open_closed [symmetric] simp del: le_divide_eq_numeral1 dest!: connected_component_in)

  5154         apply (metis diff_self e2 ele norm_minus_commute norm_zero not_less)

  5155        using centre_in_cball connected_component_refl_eq e2 x apply blast

  5156       using ccs

  5157       apply (force simp: cball_def dist_norm norm_minus_commute dest: ele [OF \<open>y \<in> S\<close>])

  5158       done

  5159     moreover have "connected_component_set (f  S) (f x) \<subseteq> f  S"

  5160       by (auto simp: connected_component_in)

  5161     ultimately have "connected_component_set (f  S) (f x) = {f x}"

  5162       by (auto simp: x)

  5163   }

  5164   with assms show ?thesis

  5165     by blast

  5166 qed

  5167

  5168 lemma finite_implies_discrete:

  5169   fixes S :: "'a::topological_space set"

  5170   assumes "finite (f  S)"

  5171   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))"

  5172 proof -

  5173   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

  5174   proof (cases "f  S - {f x} = {}")

  5175     case True

  5176     with zero_less_numeral show ?thesis

  5177       by (fastforce simp add: Set.image_subset_iff cong: conj_cong)

  5178   next

  5179     case False

  5180     then obtain z where z: "z \<in> S" "f z \<noteq> f x"

  5181       by blast

  5182     have finn: "finite {norm (z - f x) |z. z \<in> f  S - {f x}}"

  5183       using assms by simp

  5184     then have *: "0 < Inf{norm(z - f x) | z. z \<in> f  S - {f x}}"

  5185       apply (rule finite_imp_less_Inf)

  5186       using z apply force+

  5187       done

  5188     show ?thesis

  5189       by (force intro!: * cInf_le_finite [OF finn])

  5190   qed

  5191   with assms show ?thesis

  5192     by blast

  5193 qed

  5194

  5195 text\<open>This proof requires the existence of two separate values of the range type.\<close>

  5196 lemma finite_range_constant_imp_connected:

  5197   assumes "\<And>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.

  5198               \<lbrakk>continuous_on S f; finite(f  S)\<rbrakk> \<Longrightarrow> \<exists>a. \<forall>x \<in> S. f x = a"

  5199     shows "connected S"

  5200 proof -

  5201   { fix t u

  5202     assume clt: "closedin (subtopology euclidean S) t"

  5203        and clu: "closedin (subtopology euclidean S) u"

  5204        and tue: "t \<inter> u = {}" and tus: "t \<union> u = S"

  5205     have conif: "continuous_on S (\<lambda>x. if x \<in> t then 0 else 1)"

  5206       apply (subst tus [symmetric])

  5207       apply (rule continuous_on_cases_local)

  5208       using clt clu tue

  5209       apply (auto simp: tus continuous_on_const)

  5210       done

  5211     have fi: "finite ((\<lambda>x. if x \<in> t then 0 else 1)  S)"

  5212       by (rule finite_subset [of _ "{0,1}"]) auto

  5213     have "t = {} \<or> u = {}"

  5214       using assms [OF conif fi] tus [symmetric]

  5215       by (auto simp: Ball_def) (metis IntI empty_iff one_neq_zero tue)

  5216   }

  5217   then show ?thesis

  5218     by (simp add: connected_closedin_eq)

  5219 qed

  5220

  5221 lemma continuous_disconnected_range_constant_eq:

  5222       "(connected S \<longleftrightarrow>

  5223            (\<forall>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.

  5224             \<forall>t. continuous_on S f \<and> f  S \<subseteq> t \<and> (\<forall>y \<in> t. connected_component_set t y = {y})

  5225             \<longrightarrow> (\<exists>a::'b. \<forall>x \<in> S. f x = a)))" (is ?thesis1)

  5226   and continuous_discrete_range_constant_eq:

  5227       "(connected S \<longleftrightarrow>

  5228          (\<forall>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.

  5229           continuous_on S f \<and>

  5230           (\<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)))

  5231           \<longrightarrow> (\<exists>a::'b. \<forall>x \<in> S. f x = a)))" (is ?thesis2)

  5232   and continuous_finite_range_constant_eq:

  5233       "(connected S \<longleftrightarrow>

  5234          (\<forall>f::'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1.

  5235           continuous_on S f \<and> finite (f  S)

  5236           \<longrightarrow> (\<exists>a::'b. \<forall>x \<in> S. f x = a)))" (is ?thesis3)

  5237 proof -

  5238   have *: "\<And>s t u v. \<lbrakk>s \<Longrightarrow> t; t \<Longrightarrow> u; u \<Longrightarrow> v; v \<Longrightarrow> s\<rbrakk>

  5239     \<Longrightarrow> (s \<longleftrightarrow> t) \<and> (s \<longleftrightarrow> u) \<and> (s \<longleftrightarrow> v)"

  5240     by blast

  5241   have "?thesis1 \<and> ?thesis2 \<and> ?thesis3"

  5242     apply (rule *)

  5243     using continuous_disconnected_range_constant apply metis

  5244     apply clarify

  5245     apply (frule discrete_subset_disconnected; blast)

  5246     apply (blast dest: finite_implies_discrete)

  5247     apply (blast intro!: finite_range_constant_imp_connected)

  5248     done

  5249   then show ?thesis1 ?thesis2 ?thesis3

  5250     by blast+

  5251 qed

  5252

  5253 lemma continuous_discrete_range_constant:

  5254   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1"

  5255   assumes S: "connected S"

  5256       and "continuous_on S f"

  5257       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)"

  5258     obtains a where "\<And>x. x \<in> S \<Longrightarrow> f x = a"

  5259   using continuous_discrete_range_constant_eq [THEN iffD1, OF S] assms

  5260   by blast

  5261

  5262 lemma continuous_finite_range_constant:

  5263   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra_1"

  5264   assumes "connected S"

  5265       and "continuous_on S f"

  5266       and "finite (f  S)"

  5267     obtains a where "\<And>x. x \<in> S \<Longrightarrow> f x = a"

  5268   using assms continuous_finite_range_constant_eq

  5269   by blast

  5270

  5271

  5272

  5273 subsection \<open>Continuous Extension\<close>

  5274

  5275 definition clamp :: "'a::euclidean_space \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a" where

  5276   "clamp a b x = (if (\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)

  5277     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)

  5278     else a)"

  5279

  5280 lemma clamp_in_interval[simp]:

  5281   assumes "\<And>i. i \<in> Basis \<Longrightarrow> a \<bullet> i \<le> b \<bullet> i"

  5282   shows "clamp a b x \<in> cbox a b"

  5283   unfolding clamp_def

  5284   using box_ne_empty(1)[of a b] assms by (auto simp: cbox_def)

  5285

  5286 lemma clamp_cancel_cbox[simp]:

  5287   fixes x a b :: "'a::euclidean_space"

  5288   assumes x: "x \<in> cbox a b"

  5289   shows "clamp a b x = x"

  5290   using assms

  5291   by (auto simp: clamp_def mem_box intro!: euclidean_eqI[where 'a='a])

  5292

  5293 lemma clamp_empty_interval:

  5294   assumes "i \<in> Basis" "a \<bullet> i > b \<bullet> i"

  5295   shows "clamp a b = (\<lambda>_. a)"

  5296   using assms

  5297   by (force simp: clamp_def[abs_def] split: if_splits intro!: ext)

  5298

  5299 lemma dist_clamps_le_dist_args:

  5300   fixes x :: "'a::euclidean_space"

  5301   shows "dist (clamp a b y) (clamp a b x) \<le> dist y x"

  5302 proof cases

  5303   assume le: "(\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)"

  5304   then have "(\<Sum>i\<in>Basis. (dist (clamp a b y \<bullet> i) (clamp a b x \<bullet> i))\<^sup>2) \<le>

  5305     (\<Sum>i\<in>Basis. (dist (y \<bullet> i) (x \<bullet> i))\<^sup>2)"

  5306     by (auto intro!: sum_mono simp: clamp_def dist_real_def abs_le_square_iff[symmetric])

  5307   then show ?thesis

  5308     by (auto intro: real_sqrt_le_mono

  5309       simp: euclidean_dist_l2[where y=x] euclidean_dist_l2[where y="clamp a b x"] setL2_def)

  5310 qed (auto simp: clamp_def)

  5311

  5312 lemma clamp_continuous_at:

  5313   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::metric_space"

  5314     and x :: 'a

  5315   assumes f_cont: "continuous_on (cbox a b) f"

  5316   shows "continuous (at x) (\<lambda>x. f (clamp a b x))"

  5317 proof cases

  5318   assume le: "(\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)"

  5319   show ?thesis

  5320     unfolding continuous_at_eps_delta

  5321   proof safe

  5322     fix x :: 'a

  5323     fix e :: real

  5324     assume "e > 0"

  5325     moreover have "clamp a b x \<in> cbox a b"

  5326       by (simp add: clamp_in_interval le)

  5327     moreover note f_cont[simplified continuous_on_iff]

  5328     ultimately

  5329     obtain d where d: "0 < d"

  5330       "\<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"

  5331       by force

  5332     show "\<exists>d>0. \<forall>x'. dist x' x < d \<longrightarrow>

  5333       dist (f (clamp a b x')) (f (clamp a b x)) < e"

  5334       using le

  5335       by (auto intro!: d clamp_in_interval dist_clamps_le_dist_args[THEN le_less_trans])

  5336   qed

  5337 qed (auto simp: clamp_empty_interval)

  5338

  5339 lemma clamp_continuous_on:

  5340   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::metric_space"

  5341   assumes f_cont: "continuous_on (cbox a b) f"

  5342   shows "continuous_on S (\<lambda>x. f (clamp a b x))"

  5343   using assms

  5344   by (auto intro: continuous_at_imp_continuous_on clamp_continuous_at)

  5345

  5346 lemma clamp_bounded:

  5347   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::metric_space"

  5348   assumes bounded: "bounded (f  (cbox a b))"

  5349   shows "bounded (range (\<lambda>x. f (clamp a b x)))"

  5350 proof cases

  5351   assume le: "(\<forall>i\<in>Basis. a \<bullet> i \<le> b \<bullet> i)"

  5352   from bounded obtain c where f_bound: "\<forall>x\<in>f  cbox a b. dist undefined x \<le> c"

  5353     by (auto simp: bounded_any_center[where a=undefined])

  5354   then show ?thesis

  5355     by (auto intro!: exI[where x=c] clamp_in_interval[OF le[rule_format]]

  5356         simp: bounded_any_center[where a=undefined])

  5357 qed (auto simp: clamp_empty_interval image_def)

  5358

  5359

  5360 definition ext_cont :: "('a::euclidean_space \<Rightarrow> 'b::metric_space) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'b"

  5361   where "ext_cont f a b = (\<lambda>x. f (clamp a b x))"

  5362

  5363 lemma ext_cont_cancel_cbox[simp]:

  5364   fixes x a b :: "'a::euclidean_space"

  5365   assumes x: "x \<in> cbox a b"

  5366   shows "ext_cont f a b x = f x"

  5367   using assms

  5368   unfolding ext_cont_def

  5369   by (auto simp: clamp_def mem_box intro!: euclidean_eqI[where 'a='a] arg_cong[where f=f])

  5370

  5371 lemma continuous_on_ext_cont[continuous_intros]:

  5372   "continuous_on (cbox a b) f \<Longrightarrow> continuous_on S (ext_cont f a b)"

  5373   by (auto intro!: clamp_continuous_on simp: ext_cont_def)

  5374

  5375 end