src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
author huffman
Mon, 08 Aug 2011 15:27:24 -0700
changeset 44947 cddb05f94183
parent 44946 5952bd355779
child 44952 730f7cced3a6
permissions -rw-r--r--
generalize sequence lemmas
     1 (*  title:      HOL/Library/Topology_Euclidian_Space.thy
     2     Author:     Amine Chaieb, University of Cambridge
     3     Author:     Robert Himmelmann, TU Muenchen
     4     Author:     Brian Huffman, Portland State University
     5 *)
     6 
     7 header {* Elementary topology in Euclidean space. *}
     8 
     9 theory Topology_Euclidean_Space
    10 imports SEQ Euclidean_Space "~~/src/HOL/Library/Glbs"
    11 begin
    12 
    13 (* to be moved elsewhere *)
    14 
    15 lemma euclidean_dist_l2:"dist x (y::'a::euclidean_space) = setL2 (\<lambda>i. dist(x$$i) (y$$i)) {..<DIM('a)}"
    16   unfolding dist_norm norm_eq_sqrt_inner setL2_def apply(subst euclidean_inner)
    17   apply(auto simp add:power2_eq_square) unfolding euclidean_component.diff ..
    18 
    19 lemma dist_nth_le: "dist (x $$ i) (y $$ i) \<le> dist x (y::'a::euclidean_space)"
    20   apply(subst(2) euclidean_dist_l2) apply(cases "i<DIM('a)")
    21   apply(rule member_le_setL2) by auto
    22 
    23 subsection{* General notion of a topology *}
    24 
    25 definition "istopology L \<longleftrightarrow> {} \<in> L \<and> (\<forall>S \<in>L. \<forall>T \<in>L. S \<inter> T \<in> L) \<and> (\<forall>K. K \<subseteq>L \<longrightarrow> \<Union> K \<in> L)"
    26 typedef (open) 'a topology = "{L::('a set) set. istopology L}"
    27   morphisms "openin" "topology"
    28   unfolding istopology_def by blast
    29 
    30 lemma istopology_open_in[intro]: "istopology(openin U)"
    31   using openin[of U] by blast
    32 
    33 lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
    34   using topology_inverse[unfolded mem_def Collect_def] .
    35 
    36 lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
    37   using topology_inverse[of U] istopology_open_in[of "topology U"] by auto
    38 
    39 lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
    40 proof-
    41   {assume "T1=T2" hence "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp}
    42   moreover
    43   {assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
    44     hence "openin T1 = openin T2" by (metis mem_def set_eqI)
    45     hence "topology (openin T1) = topology (openin T2)" by simp
    46     hence "T1 = T2" unfolding openin_inverse .}
    47   ultimately show ?thesis by blast
    48 qed
    49 
    50 text{* Infer the "universe" from union of all sets in the topology. *}
    51 
    52 definition "topspace T =  \<Union>{S. openin T S}"
    53 
    54 subsection{* Main properties of open sets *}
    55 
    56 lemma openin_clauses:
    57   fixes U :: "'a topology"
    58   shows "openin U {}"
    59   "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
    60   "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
    61   using openin[of U] unfolding istopology_def Collect_def mem_def
    62   unfolding subset_eq Ball_def mem_def by auto
    63 
    64 lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
    65   unfolding topspace_def by blast
    66 lemma openin_empty[simp]: "openin U {}" by (simp add: openin_clauses)
    67 
    68 lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
    69   using openin_clauses by simp
    70 
    71 lemma openin_Union[intro]: "(\<forall>S \<in>K. openin U S) \<Longrightarrow> openin U (\<Union> K)"
    72   using openin_clauses by simp
    73 
    74 lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
    75   using openin_Union[of "{S,T}" U] by auto
    76 
    77 lemma openin_topspace[intro, simp]: "openin U (topspace U)" by (simp add: openin_Union topspace_def)
    78 
    79 lemma openin_subopen: "openin U S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>T. openin U T \<and> x \<in> T \<and> T \<subseteq> S)" (is "?lhs \<longleftrightarrow> ?rhs")
    80 proof
    81   assume ?lhs then show ?rhs by auto
    82 next
    83   assume H: ?rhs
    84   let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"
    85   have "openin U ?t" by (simp add: openin_Union)
    86   also have "?t = S" using H by auto
    87   finally show "openin U S" .
    88 qed
    89 
    90 subsection{* Closed sets *}
    91 
    92 definition "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
    93 
    94 lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U" by (metis closedin_def)
    95 lemma closedin_empty[simp]: "closedin U {}" by (simp add: closedin_def)
    96 lemma closedin_topspace[intro,simp]:
    97   "closedin U (topspace U)" by (simp add: closedin_def)
    98 lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
    99   by (auto simp add: Diff_Un closedin_def)
   100 
   101 lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union> {A - s|s. s\<in>S}" by auto
   102 lemma closedin_Inter[intro]: assumes Ke: "K \<noteq> {}" and Kc: "\<forall>S \<in>K. closedin U S"
   103   shows "closedin U (\<Inter> K)"  using Ke Kc unfolding closedin_def Diff_Inter by auto
   104 
   105 lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
   106   using closedin_Inter[of "{S,T}" U] by auto
   107 
   108 lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B" by blast
   109 lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
   110   apply (auto simp add: closedin_def Diff_Diff_Int inf_absorb2)
   111   apply (metis openin_subset subset_eq)
   112   done
   113 
   114 lemma openin_closedin:  "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
   115   by (simp add: openin_closedin_eq)
   116 
   117 lemma openin_diff[intro]: assumes oS: "openin U S" and cT: "closedin U T" shows "openin U (S - T)"
   118 proof-
   119   have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
   120     by (auto simp add: topspace_def openin_subset)
   121   then show ?thesis using oS cT by (auto simp add: closedin_def)
   122 qed
   123 
   124 lemma closedin_diff[intro]: assumes oS: "closedin U S" and cT: "openin U T" shows "closedin U (S - T)"
   125 proof-
   126   have "S - T = S \<inter> (topspace U - T)" using closedin_subset[of U S]  oS cT
   127     by (auto simp add: topspace_def )
   128   then show ?thesis using oS cT by (auto simp add: openin_closedin_eq)
   129 qed
   130 
   131 subsection{* Subspace topology. *}
   132 
   133 definition "subtopology U V = topology {S \<inter> V |S. openin U S}"
   134 
   135 lemma istopology_subtopology: "istopology {S \<inter> V |S. openin U S}" (is "istopology ?L")
   136 proof-
   137   have "{} \<in> ?L" by blast
   138   {fix A B assume A: "A \<in> ?L" and B: "B \<in> ?L"
   139     from A B obtain Sa and Sb where Sa: "openin U Sa" "A = Sa \<inter> V" and Sb: "openin U Sb" "B = Sb \<inter> V" by blast
   140     have "A\<inter>B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"  using Sa Sb by blast+
   141     then have "A \<inter> B \<in> ?L" by blast}
   142   moreover
   143   {fix K assume K: "K \<subseteq> ?L"
   144     have th0: "?L = (\<lambda>S. S \<inter> V) ` openin U "
   145       apply (rule set_eqI)
   146       apply (simp add: Ball_def image_iff)
   147       by (metis mem_def)
   148     from K[unfolded th0 subset_image_iff]
   149     obtain Sk where Sk: "Sk \<subseteq> openin U" "K = (\<lambda>S. S \<inter> V) ` Sk" by blast
   150     have "\<Union>K = (\<Union>Sk) \<inter> V" using Sk by auto
   151     moreover have "openin U (\<Union> Sk)" using Sk by (auto simp add: subset_eq mem_def)
   152     ultimately have "\<Union>K \<in> ?L" by blast}
   153   ultimately show ?thesis unfolding istopology_def by blast
   154 qed
   155 
   156 lemma openin_subtopology:
   157   "openin (subtopology U V) S \<longleftrightarrow> (\<exists> T. (openin U T) \<and> (S = T \<inter> V))"
   158   unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
   159   by (auto simp add: Collect_def)
   160 
   161 lemma topspace_subtopology: "topspace(subtopology U V) = topspace U \<inter> V"
   162   by (auto simp add: topspace_def openin_subtopology)
   163 
   164 lemma closedin_subtopology:
   165   "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
   166   unfolding closedin_def topspace_subtopology
   167   apply (simp add: openin_subtopology)
   168   apply (rule iffI)
   169   apply clarify
   170   apply (rule_tac x="topspace U - T" in exI)
   171   by auto
   172 
   173 lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
   174   unfolding openin_subtopology
   175   apply (rule iffI, clarify)
   176   apply (frule openin_subset[of U])  apply blast
   177   apply (rule exI[where x="topspace U"])
   178   by auto
   179 
   180 lemma subtopology_superset: assumes UV: "topspace U \<subseteq> V"
   181   shows "subtopology U V = U"
   182 proof-
   183   {fix S
   184     {fix T assume T: "openin U T" "S = T \<inter> V"
   185       from T openin_subset[OF T(1)] UV have eq: "S = T" by blast
   186       have "openin U S" unfolding eq using T by blast}
   187     moreover
   188     {assume S: "openin U S"
   189       hence "\<exists>T. openin U T \<and> S = T \<inter> V"
   190         using openin_subset[OF S] UV by auto}
   191     ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S" by blast}
   192   then show ?thesis unfolding topology_eq openin_subtopology by blast
   193 qed
   194 
   195 
   196 lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
   197   by (simp add: subtopology_superset)
   198 
   199 lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
   200   by (simp add: subtopology_superset)
   201 
   202 subsection{* The universal Euclidean versions are what we use most of the time *}
   203 
   204 definition
   205   euclidean :: "'a::topological_space topology" where
   206   "euclidean = topology open"
   207 
   208 lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
   209   unfolding euclidean_def
   210   apply (rule cong[where x=S and y=S])
   211   apply (rule topology_inverse[symmetric])
   212   apply (auto simp add: istopology_def)
   213   by (auto simp add: mem_def subset_eq)
   214 
   215 lemma topspace_euclidean: "topspace euclidean = UNIV"
   216   apply (simp add: topspace_def)
   217   apply (rule set_eqI)
   218   by (auto simp add: open_openin[symmetric])
   219 
   220 lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
   221   by (simp add: topspace_euclidean topspace_subtopology)
   222 
   223 lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
   224   by (simp add: closed_def closedin_def topspace_euclidean open_openin Compl_eq_Diff_UNIV)
   225 
   226 lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
   227   by (simp add: open_openin openin_subopen[symmetric])
   228 
   229 subsection{* Open and closed balls. *}
   230 
   231 definition
   232   ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
   233   "ball x e = {y. dist x y < e}"
   234 
   235 definition
   236   cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where
   237   "cball x e = {y. dist x y \<le> e}"
   238 
   239 lemma mem_ball[simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e" by (simp add: ball_def)
   240 lemma mem_cball[simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e" by (simp add: cball_def)
   241 
   242 lemma mem_ball_0 [simp]:
   243   fixes x :: "'a::real_normed_vector"
   244   shows "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
   245   by (simp add: dist_norm)
   246 
   247 lemma mem_cball_0 [simp]:
   248   fixes x :: "'a::real_normed_vector"
   249   shows "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
   250   by (simp add: dist_norm)
   251 
   252 lemma centre_in_cball[simp]: "x \<in> cball x e \<longleftrightarrow> 0\<le> e"  by simp
   253 lemma ball_subset_cball[simp,intro]: "ball x e \<subseteq> cball x e" by (simp add: subset_eq)
   254 lemma subset_ball[intro]: "d <= e ==> ball x d \<subseteq> ball x e" by (simp add: subset_eq)
   255 lemma subset_cball[intro]: "d <= e ==> cball x d \<subseteq> cball x e" by (simp add: subset_eq)
   256 lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
   257   by (simp add: set_eq_iff) arith
   258 
   259 lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
   260   by (simp add: set_eq_iff)
   261 
   262 lemma diff_less_iff: "(a::real) - b > 0 \<longleftrightarrow> a > b"
   263   "(a::real) - b < 0 \<longleftrightarrow> a < b"
   264   "a - b < c \<longleftrightarrow> a < c +b" "a - b > c \<longleftrightarrow> a > c +b" by arith+
   265 lemma diff_le_iff: "(a::real) - b \<ge> 0 \<longleftrightarrow> a \<ge> b" "(a::real) - b \<le> 0 \<longleftrightarrow> a \<le> b"
   266   "a - b \<le> c \<longleftrightarrow> a \<le> c +b" "a - b \<ge> c \<longleftrightarrow> a \<ge> c +b"  by arith+
   267 
   268 lemma open_ball[intro, simp]: "open (ball x e)"
   269   unfolding open_dist ball_def Collect_def Ball_def mem_def
   270   unfolding dist_commute
   271   apply clarify
   272   apply (rule_tac x="e - dist xa x" in exI)
   273   using dist_triangle_alt[where z=x]
   274   apply (clarsimp simp add: diff_less_iff)
   275   apply atomize
   276   apply (erule_tac x="y" in allE)
   277   apply (erule_tac x="xa" in allE)
   278   by arith
   279 
   280 lemma centre_in_ball[simp]: "x \<in> ball x e \<longleftrightarrow> e > 0" by (metis mem_ball dist_self)
   281 lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
   282   unfolding open_dist subset_eq mem_ball Ball_def dist_commute ..
   283 
   284 lemma openE[elim?]:
   285   assumes "open S" "x\<in>S" 
   286   obtains e where "e>0" "ball x e \<subseteq> S"
   287   using assms unfolding open_contains_ball by auto
   288 
   289 lemma open_contains_ball_eq: "open S \<Longrightarrow> \<forall>x. x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
   290   by (metis open_contains_ball subset_eq centre_in_ball)
   291 
   292 lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
   293   unfolding mem_ball set_eq_iff
   294   apply (simp add: not_less)
   295   by (metis zero_le_dist order_trans dist_self)
   296 
   297 lemma ball_empty[intro]: "e \<le> 0 ==> ball x e = {}" by simp
   298 
   299 subsection{* Basic "localization" results are handy for connectedness. *}
   300 
   301 lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
   302   by (auto simp add: openin_subtopology open_openin[symmetric])
   303 
   304 lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
   305   by (auto simp add: openin_open)
   306 
   307 lemma open_openin_trans[trans]:
   308  "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
   309   by (metis Int_absorb1  openin_open_Int)
   310 
   311 lemma open_subset:  "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
   312   by (auto simp add: openin_open)
   313 
   314 lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
   315   by (simp add: closedin_subtopology closed_closedin Int_ac)
   316 
   317 lemma closedin_closed_Int: "closed S ==> closedin (subtopology euclidean U) (U \<inter> S)"
   318   by (metis closedin_closed)
   319 
   320 lemma closed_closedin_trans: "closed S \<Longrightarrow> closed T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> closedin (subtopology euclidean S) T"
   321   apply (subgoal_tac "S \<inter> T = T" )
   322   apply auto
   323   apply (frule closedin_closed_Int[of T S])
   324   by simp
   325 
   326 lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
   327   by (auto simp add: closedin_closed)
   328 
   329 lemma openin_euclidean_subtopology_iff:
   330   fixes S U :: "'a::metric_space set"
   331   shows "openin (subtopology euclidean U) S
   332   \<longleftrightarrow> S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)" (is "?lhs \<longleftrightarrow> ?rhs")
   333 proof-
   334   {assume ?lhs hence ?rhs unfolding openin_subtopology open_openin[symmetric]
   335       by (simp add: open_dist) blast}
   336   moreover
   337   {assume SU: "S \<subseteq> U" and H: "\<And>x. x \<in> S \<Longrightarrow> \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x' \<in> S"
   338     from H obtain d where d: "\<And>x . x\<in> S \<Longrightarrow> d x > 0 \<and> (\<forall>x' \<in> U. dist x' x < d x \<longrightarrow> x' \<in> S)"
   339       by metis
   340     let ?T = "\<Union>{B. \<exists>x\<in>S. B = ball x (d x)}"
   341     have oT: "open ?T" by auto
   342     { fix x assume "x\<in>S"
   343       hence "x \<in> \<Union>{B. \<exists>x\<in>S. B = ball x (d x)}"
   344         apply simp apply(rule_tac x="ball x(d x)" in exI) apply auto
   345         by (rule d [THEN conjunct1])
   346       hence "x\<in> ?T \<inter> U" using SU and `x\<in>S` by auto  }
   347     moreover
   348     { fix y assume "y\<in>?T"
   349       then obtain B where "y\<in>B" "B\<in>{B. \<exists>x\<in>S. B = ball x (d x)}" by auto
   350       then obtain x where "x\<in>S" and x:"y \<in> ball x (d x)" by auto
   351       assume "y\<in>U"
   352       hence "y\<in>S" using d[OF `x\<in>S`] and x by(auto simp add: dist_commute) }
   353     ultimately have "S = ?T \<inter> U" by blast
   354     with oT have ?lhs unfolding openin_subtopology open_openin[symmetric] by blast}
   355   ultimately show ?thesis by blast
   356 qed
   357 
   358 text{* These "transitivity" results are handy too. *}
   359 
   360 lemma openin_trans[trans]: "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T
   361   \<Longrightarrow> openin (subtopology euclidean U) S"
   362   unfolding open_openin openin_open by blast
   363 
   364 lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
   365   by (auto simp add: openin_open intro: openin_trans)
   366 
   367 lemma closedin_trans[trans]:
   368  "closedin (subtopology euclidean T) S \<Longrightarrow>
   369            closedin (subtopology euclidean U) T
   370            ==> closedin (subtopology euclidean U) S"
   371   by (auto simp add: closedin_closed closed_closedin closed_Inter Int_assoc)
   372 
   373 lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
   374   by (auto simp add: closedin_closed intro: closedin_trans)
   375 
   376 subsection{* Connectedness *}
   377 
   378 definition "connected S \<longleftrightarrow>
   379   ~(\<exists>e1 e2. open e1 \<and> open e2 \<and> S \<subseteq> (e1 \<union> e2) \<and> (e1 \<inter> e2 \<inter> S = {})
   380   \<and> ~(e1 \<inter> S = {}) \<and> ~(e2 \<inter> S = {}))"
   381 
   382 lemma connected_local:
   383  "connected S \<longleftrightarrow> ~(\<exists>e1 e2.
   384                  openin (subtopology euclidean S) e1 \<and>
   385                  openin (subtopology euclidean S) e2 \<and>
   386                  S \<subseteq> e1 \<union> e2 \<and>
   387                  e1 \<inter> e2 = {} \<and>
   388                  ~(e1 = {}) \<and>
   389                  ~(e2 = {}))"
   390 unfolding connected_def openin_open by (safe, blast+)
   391 
   392 lemma exists_diff:
   393   fixes P :: "'a set \<Rightarrow> bool"
   394   shows "(\<exists>S. P(- S)) \<longleftrightarrow> (\<exists>S. P S)" (is "?lhs \<longleftrightarrow> ?rhs")
   395 proof-
   396   {assume "?lhs" hence ?rhs by blast }
   397   moreover
   398   {fix S assume H: "P S"
   399     have "S = - (- S)" by auto
   400     with H have "P (- (- S))" by metis }
   401   ultimately show ?thesis by metis
   402 qed
   403 
   404 lemma connected_clopen: "connected S \<longleftrightarrow>
   405         (\<forall>T. openin (subtopology euclidean S) T \<and>
   406             closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")
   407 proof-
   408   have " \<not> connected S \<longleftrightarrow> (\<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> {})"
   409     unfolding connected_def openin_open closedin_closed
   410     apply (subst exists_diff) by blast
   411   hence th0: "connected S \<longleftrightarrow> \<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> {})"
   412     (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)") apply (simp add: closed_def) by metis
   413 
   414   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'))"
   415     (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")
   416     unfolding connected_def openin_open closedin_closed by auto
   417   {fix e2
   418     {fix e1 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)"
   419         by auto}
   420     then have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by metis}
   421   then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by blast
   422   then show ?thesis unfolding th0 th1 by simp
   423 qed
   424 
   425 lemma connected_empty[simp, intro]: "connected {}"
   426   by (simp add: connected_def)
   427 
   428 subsection{* Limit points *}
   429 
   430 definition
   431   islimpt:: "'a::topological_space \<Rightarrow> 'a set \<Rightarrow> bool"
   432     (infixr "islimpt" 60) where
   433   "x islimpt S \<longleftrightarrow> (\<forall>T. x\<in>T \<longrightarrow> open T \<longrightarrow> (\<exists>y\<in>S. y\<in>T \<and> y\<noteq>x))"
   434 
   435 lemma islimptI:
   436   assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
   437   shows "x islimpt S"
   438   using assms unfolding islimpt_def by auto
   439 
   440 lemma islimptE:
   441   assumes "x islimpt S" and "x \<in> T" and "open T"
   442   obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
   443   using assms unfolding islimpt_def by auto
   444 
   445 lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T ==> x islimpt T" by (auto simp add: islimpt_def)
   446 
   447 lemma islimpt_approachable:
   448   fixes x :: "'a::metric_space"
   449   shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
   450   unfolding islimpt_def
   451   apply auto
   452   apply(erule_tac x="ball x e" in allE)
   453   apply auto
   454   apply(rule_tac x=y in bexI)
   455   apply (auto simp add: dist_commute)
   456   apply (simp add: open_dist, drule (1) bspec)
   457   apply (clarify, drule spec, drule (1) mp, auto)
   458   done
   459 
   460 lemma islimpt_approachable_le:
   461   fixes x :: "'a::metric_space"
   462   shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x <= e)"
   463   unfolding islimpt_approachable
   464   using approachable_lt_le[where f="\<lambda>x'. dist x' x" and P="\<lambda>x'. \<not> (x'\<in>S \<and> x'\<noteq>x)"]
   465   by metis 
   466 
   467 class perfect_space =
   468   assumes islimpt_UNIV [simp, intro]: "(x::'a::topological_space) islimpt UNIV"
   469 
   470 lemma perfect_choose_dist:
   471   fixes x :: "'a::{perfect_space, metric_space}"
   472   shows "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
   473 using islimpt_UNIV [of x]
   474 by (simp add: islimpt_approachable)
   475 
   476 instance real :: perfect_space
   477 apply default
   478 apply (rule islimpt_approachable [THEN iffD2])
   479 apply (clarify, rule_tac x="x + e/2" in bexI)
   480 apply (auto simp add: dist_norm)
   481 done
   482 
   483 instance euclidean_space \<subseteq> perfect_space
   484 proof fix x::'a
   485   { fix e :: real assume "0 < e"
   486     def a \<equiv> "x $$ 0"
   487     have "a islimpt UNIV" by (rule islimpt_UNIV)
   488     with `0 < e` obtain b where "b \<noteq> a" and "dist b a < e"
   489       unfolding islimpt_approachable by auto
   490     def y \<equiv> "\<chi>\<chi> i. if i = 0 then b else x$$i :: 'a"
   491     from `b \<noteq> a` have "y \<noteq> x" unfolding a_def y_def apply(subst euclidean_eq) apply safe
   492       apply(erule_tac x=0 in allE) using DIM_positive[where 'a='a] by auto
   493 
   494     have *:"(\<Sum>i<DIM('a). (dist (y $$ i) (x $$ i))\<twosuperior>) = (\<Sum>i\<in>{0}. (dist (y $$ i) (x $$ i))\<twosuperior>)"
   495       apply(rule setsum_mono_zero_right) unfolding y_def by auto
   496     from `dist b a < e` have "dist y x < e"
   497       apply(subst euclidean_dist_l2)
   498       unfolding setL2_def * unfolding y_def a_def using `0 < e` by auto
   499     from `y \<noteq> x` and `dist y x < e`
   500     have "\<exists>y\<in>UNIV. y \<noteq> x \<and> dist y x < e" by auto
   501   }
   502   then show "x islimpt UNIV" unfolding islimpt_approachable by blast
   503 qed
   504 
   505 lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
   506   unfolding closed_def
   507   apply (subst open_subopen)
   508   apply (simp add: islimpt_def subset_eq)
   509   by (metis ComplE ComplI insertCI insert_absorb mem_def)
   510 
   511 lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
   512   unfolding islimpt_def by auto
   513 
   514 lemma finite_set_avoid:
   515   fixes a :: "'a::metric_space"
   516   assumes fS: "finite S" shows  "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d <= dist a x"
   517 proof(induct rule: finite_induct[OF fS])
   518   case 1 thus ?case by (auto intro: zero_less_one)
   519 next
   520   case (2 x F)
   521   from 2 obtain d where d: "d >0" "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> d \<le> dist a x" by blast
   522   {assume "x = a" hence ?case using d by auto  }
   523   moreover
   524   {assume xa: "x\<noteq>a"
   525     let ?d = "min d (dist a x)"
   526     have dp: "?d > 0" using xa d(1) using dist_nz by auto
   527     from d have d': "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> ?d \<le> dist a x" by auto
   528     with dp xa have ?case by(auto intro!: exI[where x="?d"]) }
   529   ultimately show ?case by blast
   530 qed
   531 
   532 lemma islimpt_finite:
   533   fixes S :: "'a::metric_space set"
   534   assumes fS: "finite S" shows "\<not> a islimpt S"
   535   unfolding islimpt_approachable
   536   using finite_set_avoid[OF fS, of a] by (metis dist_commute  not_le)
   537 
   538 lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
   539   apply (rule iffI)
   540   defer
   541   apply (metis Un_upper1 Un_upper2 islimpt_subset)
   542   unfolding islimpt_def
   543   apply (rule ccontr, clarsimp, rename_tac A B)
   544   apply (drule_tac x="A \<inter> B" in spec)
   545   apply (auto simp add: open_Int)
   546   done
   547 
   548 lemma discrete_imp_closed:
   549   fixes S :: "'a::metric_space set"
   550   assumes e: "0 < e" and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
   551   shows "closed S"
   552 proof-
   553   {fix x assume C: "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
   554     from e have e2: "e/2 > 0" by arith
   555     from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y\<noteq>x" "dist y x < e/2" by blast
   556     let ?m = "min (e/2) (dist x y) "
   557     from e2 y(2) have mp: "?m > 0" by (simp add: dist_nz[THEN sym])
   558     from C[rule_format, OF mp] obtain z where z: "z \<in> S" "z\<noteq>x" "dist z x < ?m" by blast
   559     have th: "dist z y < e" using z y
   560       by (intro dist_triangle_lt [where z=x], simp)
   561     from d[rule_format, OF y(1) z(1) th] y z
   562     have False by (auto simp add: dist_commute)}
   563   then show ?thesis by (metis islimpt_approachable closed_limpt [where 'a='a])
   564 qed
   565 
   566 subsection{* Interior of a Set *}
   567 definition "interior S = {x. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S}"
   568 
   569 lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
   570   apply (simp add: set_eq_iff interior_def)
   571   apply (subst (2) open_subopen) by (safe, blast+)
   572 
   573 lemma interior_open: "open S ==> (interior S = S)" by (metis interior_eq)
   574 
   575 lemma interior_empty[simp]: "interior {} = {}" by (simp add: interior_def)
   576 
   577 lemma open_interior[simp, intro]: "open(interior S)"
   578   apply (simp add: interior_def)
   579   apply (subst open_subopen) by blast
   580 
   581 lemma interior_interior[simp]: "interior(interior S) = interior S" by (metis interior_eq open_interior)
   582 lemma interior_subset: "interior S \<subseteq> S" by (auto simp add: interior_def)
   583 lemma subset_interior: "S \<subseteq> T ==> (interior S) \<subseteq> (interior T)" by (auto simp add: interior_def)
   584 lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T ==> T \<subseteq> (interior S)" by (auto simp add: interior_def)
   585 lemma interior_unique: "T \<subseteq> S \<Longrightarrow> open T  \<Longrightarrow> (\<forall>T'. T' \<subseteq> S \<and> open T' \<longrightarrow> T' \<subseteq> T) \<Longrightarrow> interior S = T"
   586   by (metis equalityI interior_maximal interior_subset open_interior)
   587 lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e. 0 < e \<and> ball x e \<subseteq> S)"
   588   apply (simp add: interior_def)
   589   by (metis open_contains_ball centre_in_ball open_ball subset_trans)
   590 
   591 lemma open_subset_interior: "open S ==> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
   592   by (metis interior_maximal interior_subset subset_trans)
   593 
   594 lemma interior_inter[simp]: "interior(S \<inter> T) = interior S \<inter> interior T"
   595   apply (rule equalityI, simp)
   596   apply (metis Int_lower1 Int_lower2 subset_interior)
   597   by (metis Int_mono interior_subset open_Int open_interior open_subset_interior)
   598 
   599 lemma interior_limit_point [intro]:
   600   fixes x :: "'a::perfect_space"
   601   assumes x: "x \<in> interior S" shows "x islimpt S"
   602   using x islimpt_UNIV [of x]
   603   unfolding interior_def islimpt_def
   604   apply (clarsimp, rename_tac T T')
   605   apply (drule_tac x="T \<inter> T'" in spec)
   606   apply (auto simp add: open_Int)
   607   done
   608 
   609 lemma interior_closed_Un_empty_interior:
   610   assumes cS: "closed S" and iT: "interior T = {}"
   611   shows "interior(S \<union> T) = interior S"
   612 proof
   613   show "interior S \<subseteq> interior (S\<union>T)"
   614     by (rule subset_interior, blast)
   615 next
   616   show "interior (S \<union> T) \<subseteq> interior S"
   617   proof
   618     fix x assume "x \<in> interior (S \<union> T)"
   619     then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T"
   620       unfolding interior_def by fast
   621     show "x \<in> interior S"
   622     proof (rule ccontr)
   623       assume "x \<notin> interior S"
   624       with `x \<in> R` `open R` obtain y where "y \<in> R - S"
   625         unfolding interior_def set_eq_iff by fast
   626       from `open R` `closed S` have "open (R - S)" by (rule open_Diff)
   627       from `R \<subseteq> S \<union> T` have "R - S \<subseteq> T" by fast
   628       from `y \<in> R - S` `open (R - S)` `R - S \<subseteq> T` `interior T = {}`
   629       show "False" unfolding interior_def by fast
   630     qed
   631   qed
   632 qed
   633 
   634 
   635 subsection{* Closure of a Set *}
   636 
   637 definition "closure S = S \<union> {x | x. x islimpt S}"
   638 
   639 lemma closure_interior: "closure S = - interior (- S)"
   640 proof-
   641   { fix x
   642     have "x\<in>- interior (- S) \<longleftrightarrow> x \<in> closure S"  (is "?lhs = ?rhs")
   643     proof
   644       let ?exT = "\<lambda> y. (\<exists>T. open T \<and> y \<in> T \<and> T \<subseteq> - S)"
   645       assume "?lhs"
   646       hence *:"\<not> ?exT x"
   647         unfolding interior_def
   648         by simp
   649       { assume "\<not> ?rhs"
   650         hence False using *
   651           unfolding closure_def islimpt_def
   652           by blast
   653       }
   654       thus "?rhs"
   655         by blast
   656     next
   657       assume "?rhs" thus "?lhs"
   658         unfolding closure_def interior_def islimpt_def
   659         by blast
   660     qed
   661   }
   662   thus ?thesis
   663     by blast
   664 qed
   665 
   666 lemma interior_closure: "interior S = - (closure (- S))"
   667 proof-
   668   { fix x
   669     have "x \<in> interior S \<longleftrightarrow> x \<in> - (closure (- S))"
   670       unfolding interior_def closure_def islimpt_def
   671       by auto
   672   }
   673   thus ?thesis
   674     by blast
   675 qed
   676 
   677 lemma closed_closure[simp, intro]: "closed (closure S)"
   678 proof-
   679   have "closed (- interior (-S))" by blast
   680   thus ?thesis using closure_interior[of S] by simp
   681 qed
   682 
   683 lemma closure_hull: "closure S = closed hull S"
   684 proof-
   685   have "S \<subseteq> closure S"
   686     unfolding closure_def
   687     by blast
   688   moreover
   689   have "closed (closure S)"
   690     using closed_closure[of S]
   691     by assumption
   692   moreover
   693   { fix t
   694     assume *:"S \<subseteq> t" "closed t"
   695     { fix x
   696       assume "x islimpt S"
   697       hence "x islimpt t" using *(1)
   698         using islimpt_subset[of x, of S, of t]
   699         by blast
   700     }
   701     with * have "closure S \<subseteq> t"
   702       unfolding closure_def
   703       using closed_limpt[of t]
   704       by auto
   705   }
   706   ultimately show ?thesis
   707     using hull_unique[of S, of "closure S", of closed]
   708     unfolding mem_def
   709     by simp
   710 qed
   711 
   712 lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
   713   unfolding closure_hull
   714   using hull_eq[of closed, unfolded mem_def, OF  closed_Inter, of S]
   715   by (metis mem_def subset_eq)
   716 
   717 lemma closure_closed[simp]: "closed S \<Longrightarrow> closure S = S"
   718   using closure_eq[of S]
   719   by simp
   720 
   721 lemma closure_closure[simp]: "closure (closure S) = closure S"
   722   unfolding closure_hull
   723   using hull_hull[of closed S]
   724   by assumption
   725 
   726 lemma closure_subset: "S \<subseteq> closure S"
   727   unfolding closure_hull
   728   using hull_subset[of S closed]
   729   by assumption
   730 
   731 lemma subset_closure: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
   732   unfolding closure_hull
   733   using hull_mono[of S T closed]
   734   by assumption
   735 
   736 lemma closure_minimal: "S \<subseteq> T \<Longrightarrow>  closed T \<Longrightarrow> closure S \<subseteq> T"
   737   using hull_minimal[of S T closed]
   738   unfolding closure_hull mem_def
   739   by simp
   740 
   741 lemma closure_unique: "S \<subseteq> T \<and> closed T \<and> (\<forall> T'. S \<subseteq> T' \<and> closed T' \<longrightarrow> T \<subseteq> T') \<Longrightarrow> closure S = T"
   742   using hull_unique[of S T closed]
   743   unfolding closure_hull mem_def
   744   by simp
   745 
   746 lemma closure_empty[simp]: "closure {} = {}"
   747   using closed_empty closure_closed[of "{}"]
   748   by simp
   749 
   750 lemma closure_univ[simp]: "closure UNIV = UNIV"
   751   using closure_closed[of UNIV]
   752   by simp
   753 
   754 lemma closure_eq_empty: "closure S = {} \<longleftrightarrow> S = {}"
   755   using closure_empty closure_subset[of S]
   756   by blast
   757 
   758 lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
   759   using closure_eq[of S] closure_subset[of S]
   760   by simp
   761 
   762 lemma open_inter_closure_eq_empty:
   763   "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
   764   using open_subset_interior[of S "- T"]
   765   using interior_subset[of "- T"]
   766   unfolding closure_interior
   767   by auto
   768 
   769 lemma open_inter_closure_subset:
   770   "open S \<Longrightarrow> (S \<inter> (closure T)) \<subseteq> closure(S \<inter> T)"
   771 proof
   772   fix x
   773   assume as: "open S" "x \<in> S \<inter> closure T"
   774   { assume *:"x islimpt T"
   775     have "x islimpt (S \<inter> T)"
   776     proof (rule islimptI)
   777       fix A
   778       assume "x \<in> A" "open A"
   779       with as have "x \<in> A \<inter> S" "open (A \<inter> S)"
   780         by (simp_all add: open_Int)
   781       with * obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
   782         by (rule islimptE)
   783       hence "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
   784         by simp_all
   785       thus "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
   786     qed
   787   }
   788   then show "x \<in> closure (S \<inter> T)" using as
   789     unfolding closure_def
   790     by blast
   791 qed
   792 
   793 lemma closure_complement: "closure(- S) = - interior(S)"
   794 proof-
   795   have "S = - (- S)"
   796     by auto
   797   thus ?thesis
   798     unfolding closure_interior
   799     by auto
   800 qed
   801 
   802 lemma interior_complement: "interior(- S) = - closure(S)"
   803   unfolding closure_interior
   804   by blast
   805 
   806 subsection{* Frontier (aka boundary) *}
   807 
   808 definition "frontier S = closure S - interior S"
   809 
   810 lemma frontier_closed: "closed(frontier S)"
   811   by (simp add: frontier_def closed_Diff)
   812 
   813 lemma frontier_closures: "frontier S = (closure S) \<inter> (closure(- S))"
   814   by (auto simp add: frontier_def interior_closure)
   815 
   816 lemma frontier_straddle:
   817   fixes a :: "'a::metric_space"
   818   shows "a \<in> frontier S \<longleftrightarrow> (\<forall>e>0. (\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e))" (is "?lhs \<longleftrightarrow> ?rhs")
   819 proof
   820   assume "?lhs"
   821   { fix e::real
   822     assume "e > 0"
   823     let ?rhse = "(\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e)"
   824     { assume "a\<in>S"
   825       have "\<exists>x\<in>S. dist a x < e" using `e>0` `a\<in>S` by(rule_tac x=a in bexI) auto
   826       moreover have "\<exists>x. x \<notin> S \<and> dist a x < e" using `?lhs` `a\<in>S`
   827         unfolding frontier_closures closure_def islimpt_def using `e>0`
   828         by (auto, erule_tac x="ball a e" in allE, auto)
   829       ultimately have ?rhse by auto
   830     }
   831     moreover
   832     { assume "a\<notin>S"
   833       hence ?rhse using `?lhs`
   834         unfolding frontier_closures closure_def islimpt_def
   835         using open_ball[of a e] `e > 0`
   836           by simp (metis centre_in_ball mem_ball open_ball) 
   837     }
   838     ultimately have ?rhse by auto
   839   }
   840   thus ?rhs by auto
   841 next
   842   assume ?rhs
   843   moreover
   844   { fix T assume "a\<notin>S" and
   845     as:"\<forall>e>0. (\<exists>x\<in>S. dist a x < e) \<and> (\<exists>x. x \<notin> S \<and> dist a x < e)" "a \<notin> S" "a \<in> T" "open T"
   846     from `open T` `a \<in> T` have "\<exists>e>0. ball a e \<subseteq> T" unfolding open_contains_ball[of T] by auto
   847     then obtain e where "e>0" "ball a e \<subseteq> T" by auto
   848     then obtain y where y:"y\<in>S" "dist a y < e"  using as(1) by auto
   849     have "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> a"
   850       using `dist a y < e` `ball a e \<subseteq> T` unfolding ball_def using `y\<in>S` `a\<notin>S` by auto
   851   }
   852   hence "a \<in> closure S" unfolding closure_def islimpt_def using `?rhs` by auto
   853   moreover
   854   { fix T assume "a \<in> T"  "open T" "a\<in>S"
   855     then obtain e where "e>0" and balle: "ball a e \<subseteq> T" unfolding open_contains_ball using `?rhs` by auto
   856     obtain x where "x \<notin> S" "dist a x < e" using `?rhs` using `e>0` by auto
   857     hence "\<exists>y\<in>- S. y \<in> T \<and> y \<noteq> a" using balle `a\<in>S` unfolding ball_def by (rule_tac x=x in bexI)auto
   858   }
   859   hence "a islimpt (- S) \<or> a\<notin>S" unfolding islimpt_def by auto
   860   ultimately show ?lhs unfolding frontier_closures using closure_def[of "- S"] by auto
   861 qed
   862 
   863 lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
   864   by (metis frontier_def closure_closed Diff_subset)
   865 
   866 lemma frontier_empty[simp]: "frontier {} = {}"
   867   by (simp add: frontier_def)
   868 
   869 lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
   870 proof-
   871   { assume "frontier S \<subseteq> S"
   872     hence "closure S \<subseteq> S" using interior_subset unfolding frontier_def by auto
   873     hence "closed S" using closure_subset_eq by auto
   874   }
   875   thus ?thesis using frontier_subset_closed[of S] ..
   876 qed
   877 
   878 lemma frontier_complement: "frontier(- S) = frontier S"
   879   by (auto simp add: frontier_def closure_complement interior_complement)
   880 
   881 lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
   882   using frontier_complement frontier_subset_eq[of "- S"]
   883   unfolding open_closed by auto
   884 
   885 subsection {* Nets and the ``eventually true'' quantifier *}
   886 
   887 text {* Common nets and The "within" modifier for nets. *}
   888 
   889 definition
   890   at_infinity :: "'a::real_normed_vector net" where
   891   "at_infinity = Abs_net (\<lambda>P. \<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x)"
   892 
   893 definition
   894   indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a net" (infixr "indirection" 70) where
   895   "a indirection v = (at a) within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
   896 
   897 text{* Prove That They are all nets. *}
   898 
   899 lemma eventually_at_infinity:
   900   "eventually P at_infinity \<longleftrightarrow> (\<exists>b. \<forall>x. norm x >= b \<longrightarrow> P x)"
   901 unfolding at_infinity_def
   902 proof (rule eventually_Abs_net, rule is_filter.intro)
   903   fix P Q :: "'a \<Rightarrow> bool"
   904   assume "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<exists>s. \<forall>x. s \<le> norm x \<longrightarrow> Q x"
   905   then obtain r s where
   906     "\<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<forall>x. s \<le> norm x \<longrightarrow> Q x" by auto
   907   then have "\<forall>x. max r s \<le> norm x \<longrightarrow> P x \<and> Q x" by simp
   908   then show "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x \<and> Q x" ..
   909 qed auto
   910 
   911 text {* Identify Trivial limits, where we can't approach arbitrarily closely. *}
   912 
   913 lemma trivial_limit_within:
   914   shows "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
   915 proof
   916   assume "trivial_limit (at a within S)"
   917   thus "\<not> a islimpt S"
   918     unfolding trivial_limit_def
   919     unfolding eventually_within eventually_at_topological
   920     unfolding islimpt_def
   921     apply (clarsimp simp add: set_eq_iff)
   922     apply (rename_tac T, rule_tac x=T in exI)
   923     apply (clarsimp, drule_tac x=y in bspec, simp_all)
   924     done
   925 next
   926   assume "\<not> a islimpt S"
   927   thus "trivial_limit (at a within S)"
   928     unfolding trivial_limit_def
   929     unfolding eventually_within eventually_at_topological
   930     unfolding islimpt_def
   931     apply clarsimp
   932     apply (rule_tac x=T in exI)
   933     apply auto
   934     done
   935 qed
   936 
   937 lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
   938   using trivial_limit_within [of a UNIV]
   939   by (simp add: within_UNIV)
   940 
   941 lemma trivial_limit_at:
   942   fixes a :: "'a::perfect_space"
   943   shows "\<not> trivial_limit (at a)"
   944   by (simp add: trivial_limit_at_iff)
   945 
   946 lemma trivial_limit_at_infinity:
   947   "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,perfect_space}) net)"
   948   unfolding trivial_limit_def eventually_at_infinity
   949   apply clarsimp
   950   apply (subgoal_tac "\<exists>x::'a. x \<noteq> 0", clarify)
   951    apply (rule_tac x="scaleR (b / norm x) x" in exI, simp)
   952   apply (cut_tac islimpt_UNIV [of "0::'a", unfolded islimpt_def])
   953   apply (drule_tac x=UNIV in spec, simp)
   954   done
   955 
   956 text {* Some property holds "sufficiently close" to the limit point. *}
   957 
   958 lemma eventually_at: (* FIXME: this replaces Limits.eventually_at *)
   959   "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
   960 unfolding eventually_at dist_nz by auto
   961 
   962 lemma eventually_within: "eventually P (at a within S) \<longleftrightarrow>
   963         (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
   964 unfolding eventually_within eventually_at dist_nz by auto
   965 
   966 lemma eventually_within_le: "eventually P (at a within S) \<longleftrightarrow>
   967         (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a <= d \<longrightarrow> P x)" (is "?lhs = ?rhs")
   968 unfolding eventually_within
   969 by auto (metis Rats_dense_in_nn_real order_le_less_trans order_refl) 
   970 
   971 lemma eventually_happens: "eventually P net ==> trivial_limit net \<or> (\<exists>x. P x)"
   972   unfolding trivial_limit_def
   973   by (auto elim: eventually_rev_mp)
   974 
   975 lemma always_eventually: "(\<forall>x. P x) ==> eventually P net"
   976 proof -
   977   assume "\<forall>x. P x" hence "P = (\<lambda>x. True)" by (simp add: ext)
   978   thus "eventually P net" by simp
   979 qed
   980 
   981 lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
   982   unfolding trivial_limit_def by (auto elim: eventually_rev_mp)
   983 
   984 lemma eventually_False: "eventually (\<lambda>x. False) net \<longleftrightarrow> trivial_limit net"
   985   unfolding trivial_limit_def ..
   986 
   987 
   988 lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
   989   apply (safe elim!: trivial_limit_eventually)
   990   apply (simp add: eventually_False [symmetric])
   991   done
   992 
   993 text{* Combining theorems for "eventually" *}
   994 
   995 lemma eventually_conjI:
   996   "\<lbrakk>eventually (\<lambda>x. P x) net; eventually (\<lambda>x. Q x) net\<rbrakk>
   997     \<Longrightarrow> eventually (\<lambda>x. P x \<and> Q x) net"
   998 by (rule eventually_conj)
   999 
  1000 lemma eventually_rev_mono:
  1001   "eventually P net \<Longrightarrow> (\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually Q net"
  1002 using eventually_mono [of P Q] by fast
  1003 
  1004 lemma eventually_and: " eventually (\<lambda>x. P x \<and> Q x) net \<longleftrightarrow> eventually P net \<and> eventually Q net"
  1005   by (auto intro!: eventually_conjI elim: eventually_rev_mono)
  1006 
  1007 lemma eventually_false: "eventually (\<lambda>x. False) net \<longleftrightarrow> trivial_limit net"
  1008   by (auto simp add: eventually_False)
  1009 
  1010 lemma not_eventually: "(\<forall>x. \<not> P x ) \<Longrightarrow> ~(trivial_limit net) ==> ~(eventually (\<lambda>x. P x) net)"
  1011   by (simp add: eventually_False)
  1012 
  1013 subsection {* Limits *}
  1014 
  1015   text{* Notation Lim to avoid collition with lim defined in analysis *}
  1016 definition
  1017   Lim :: "'a net \<Rightarrow> ('a \<Rightarrow> 'b::t2_space) \<Rightarrow> 'b" where
  1018   "Lim net f = (THE l. (f ---> l) net)"
  1019 
  1020 lemma Lim:
  1021  "(f ---> l) net \<longleftrightarrow>
  1022         trivial_limit net \<or>
  1023         (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
  1024   unfolding tendsto_iff trivial_limit_eq by auto
  1025 
  1026 
  1027 text{* Show that they yield usual definitions in the various cases. *}
  1028 
  1029 lemma Lim_within_le: "(f ---> l)(at a within S) \<longleftrightarrow>
  1030            (\<forall>e>0. \<exists>d>0. \<forall>x\<in>S. 0 < dist x a  \<and> dist x a  <= d \<longrightarrow> dist (f x) l < e)"
  1031   by (auto simp add: tendsto_iff eventually_within_le)
  1032 
  1033 lemma Lim_within: "(f ---> l) (at a within S) \<longleftrightarrow>
  1034         (\<forall>e >0. \<exists>d>0. \<forall>x \<in> S. 0 < dist x a  \<and> dist x a  < d  \<longrightarrow> dist (f x) l < e)"
  1035   by (auto simp add: tendsto_iff eventually_within)
  1036 
  1037 lemma Lim_at: "(f ---> l) (at a) \<longleftrightarrow>
  1038         (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a  \<and> dist x a  < d  \<longrightarrow> dist (f x) l < e)"
  1039   by (auto simp add: tendsto_iff eventually_at)
  1040 
  1041 lemma Lim_at_iff_LIM: "(f ---> l) (at a) \<longleftrightarrow> f -- a --> l"
  1042   unfolding Lim_at LIM_def by (simp only: zero_less_dist_iff)
  1043 
  1044 lemma Lim_at_infinity:
  1045   "(f ---> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x >= b \<longrightarrow> dist (f x) l < e)"
  1046   by (auto simp add: tendsto_iff eventually_at_infinity)
  1047 
  1048 lemma Lim_sequentially:
  1049  "(S ---> l) sequentially \<longleftrightarrow>
  1050           (\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (S n) l < e)"
  1051   by (auto simp add: tendsto_iff eventually_sequentially)
  1052 
  1053 lemma Lim_sequentially_iff_LIMSEQ: "(S ---> l) sequentially \<longleftrightarrow> S ----> l"
  1054   unfolding Lim_sequentially LIMSEQ_def ..
  1055 
  1056 lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f ---> l) net"
  1057   by (rule topological_tendstoI, auto elim: eventually_rev_mono)
  1058 
  1059 text{* The expected monotonicity property. *}
  1060 
  1061 lemma Lim_within_empty: "(f ---> l) (net within {})"
  1062   unfolding tendsto_def Limits.eventually_within by simp
  1063 
  1064 lemma Lim_within_subset: "(f ---> l) (net within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f ---> l) (net within T)"
  1065   unfolding tendsto_def Limits.eventually_within
  1066   by (auto elim!: eventually_elim1)
  1067 
  1068 lemma Lim_Un: assumes "(f ---> l) (net within S)" "(f ---> l) (net within T)"
  1069   shows "(f ---> l) (net within (S \<union> T))"
  1070   using assms unfolding tendsto_def Limits.eventually_within
  1071   apply clarify
  1072   apply (drule spec, drule (1) mp, drule (1) mp)
  1073   apply (drule spec, drule (1) mp, drule (1) mp)
  1074   apply (auto elim: eventually_elim2)
  1075   done
  1076 
  1077 lemma Lim_Un_univ:
  1078  "(f ---> l) (net within S) \<Longrightarrow> (f ---> l) (net within T) \<Longrightarrow>  S \<union> T = UNIV
  1079         ==> (f ---> l) net"
  1080   by (metis Lim_Un within_UNIV)
  1081 
  1082 text{* Interrelations between restricted and unrestricted limits. *}
  1083 
  1084 lemma Lim_at_within: "(f ---> l) net ==> (f ---> l)(net within S)"
  1085   (* FIXME: rename *)
  1086   unfolding tendsto_def Limits.eventually_within
  1087   apply (clarify, drule spec, drule (1) mp, drule (1) mp)
  1088   by (auto elim!: eventually_elim1)
  1089 
  1090 lemma Lim_within_open:
  1091   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  1092   assumes"a \<in> S" "open S"
  1093   shows "(f ---> l)(at a within S) \<longleftrightarrow> (f ---> l)(at a)" (is "?lhs \<longleftrightarrow> ?rhs")
  1094 proof
  1095   assume ?lhs
  1096   { fix A assume "open A" "l \<in> A"
  1097     with `?lhs` have "eventually (\<lambda>x. f x \<in> A) (at a within S)"
  1098       by (rule topological_tendstoD)
  1099     hence "eventually (\<lambda>x. x \<in> S \<longrightarrow> f x \<in> A) (at a)"
  1100       unfolding Limits.eventually_within .
  1101     then obtain T where "open T" "a \<in> T" "\<forall>x\<in>T. x \<noteq> a \<longrightarrow> x \<in> S \<longrightarrow> f x \<in> A"
  1102       unfolding eventually_at_topological by fast
  1103     hence "open (T \<inter> S)" "a \<in> T \<inter> S" "\<forall>x\<in>(T \<inter> S). x \<noteq> a \<longrightarrow> f x \<in> A"
  1104       using assms by auto
  1105     hence "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. x \<noteq> a \<longrightarrow> f x \<in> A)"
  1106       by fast
  1107     hence "eventually (\<lambda>x. f x \<in> A) (at a)"
  1108       unfolding eventually_at_topological .
  1109   }
  1110   thus ?rhs by (rule topological_tendstoI)
  1111 next
  1112   assume ?rhs
  1113   thus ?lhs by (rule Lim_at_within)
  1114 qed
  1115 
  1116 lemma Lim_within_LIMSEQ:
  1117   fixes a :: real and L :: "'a::metric_space"
  1118   assumes "\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L"
  1119   shows "(X ---> L) (at a within T)"
  1120 proof (rule ccontr)
  1121   assume "\<not> (X ---> L) (at a within T)"
  1122   hence "\<exists>r>0. \<forall>s>0. \<exists>x\<in>T. x \<noteq> a \<and> \<bar>x - a\<bar> < s \<and> r \<le> dist (X x) L"
  1123     unfolding tendsto_iff eventually_within dist_norm by (simp add: not_less[symmetric])
  1124   then obtain r where r: "r > 0" "\<And>s. s > 0 \<Longrightarrow> \<exists>x\<in>T-{a}. \<bar>x - a\<bar> < s \<and> dist (X x) L \<ge> r" by blast
  1125 
  1126   let ?F = "\<lambda>n::nat. SOME x. x \<in> T \<and> x \<noteq> a \<and> \<bar>x - a\<bar> < inverse (real (Suc n)) \<and> dist (X x) L \<ge> r"
  1127   have "\<And>n. \<exists>x. x \<in> T \<and> x \<noteq> a \<and> \<bar>x - a\<bar> < inverse (real (Suc n)) \<and> dist (X x) L \<ge> r"
  1128     using r by (simp add: Bex_def)
  1129   hence F: "\<And>n. ?F n \<in> T \<and> ?F n \<noteq> a \<and> \<bar>?F n - a\<bar> < inverse (real (Suc n)) \<and> dist (X (?F n)) L \<ge> r"
  1130     by (rule someI_ex)
  1131   hence F1: "\<And>n. ?F n \<in> T \<and> ?F n \<noteq> a"
  1132     and F2: "\<And>n. \<bar>?F n - a\<bar> < inverse (real (Suc n))"
  1133     and F3: "\<And>n. dist (X (?F n)) L \<ge> r"
  1134     by fast+
  1135 
  1136   have "?F ----> a"
  1137   proof (rule LIMSEQ_I, unfold real_norm_def)
  1138       fix e::real
  1139       assume "0 < e"
  1140         (* choose no such that inverse (real (Suc n)) < e *)
  1141       then have "\<exists>no. inverse (real (Suc no)) < e" by (rule reals_Archimedean)
  1142       then obtain m where nodef: "inverse (real (Suc m)) < e" by auto
  1143       show "\<exists>no. \<forall>n. no \<le> n --> \<bar>?F n - a\<bar> < e"
  1144       proof (intro exI allI impI)
  1145         fix n
  1146         assume mlen: "m \<le> n"
  1147         have "\<bar>?F n - a\<bar> < inverse (real (Suc n))"
  1148           by (rule F2)
  1149         also have "inverse (real (Suc n)) \<le> inverse (real (Suc m))"
  1150           using mlen by auto
  1151         also from nodef have
  1152           "inverse (real (Suc m)) < e" .
  1153         finally show "\<bar>?F n - a\<bar> < e" .
  1154       qed
  1155   qed
  1156   moreover note `\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L`
  1157   ultimately have "(\<lambda>n. X (?F n)) ----> L" using F1 by simp
  1158   
  1159   moreover have "\<not> ((\<lambda>n. X (?F n)) ----> L)"
  1160   proof -
  1161     {
  1162       fix no::nat
  1163       obtain n where "n = no + 1" by simp
  1164       then have nolen: "no \<le> n" by simp
  1165         (* We prove this by showing that for any m there is an n\<ge>m such that |X (?F n) - L| \<ge> r *)
  1166       have "dist (X (?F n)) L \<ge> r"
  1167         by (rule F3)
  1168       with nolen have "\<exists>n. no \<le> n \<and> dist (X (?F n)) L \<ge> r" by fast
  1169     }
  1170     then have "(\<forall>no. \<exists>n. no \<le> n \<and> dist (X (?F n)) L \<ge> r)" by simp
  1171     with r have "\<exists>e>0. (\<forall>no. \<exists>n. no \<le> n \<and> dist (X (?F n)) L \<ge> e)" by auto
  1172     thus ?thesis by (unfold LIMSEQ_def, auto simp add: not_less)
  1173   qed
  1174   ultimately show False by simp
  1175 qed
  1176 
  1177 lemma Lim_right_bound:
  1178   fixes f :: "real \<Rightarrow> real"
  1179   assumes mono: "\<And>a b. a \<in> I \<Longrightarrow> b \<in> I \<Longrightarrow> x < a \<Longrightarrow> a \<le> b \<Longrightarrow> f a \<le> f b"
  1180   assumes bnd: "\<And>a. a \<in> I \<Longrightarrow> x < a \<Longrightarrow> K \<le> f a"
  1181   shows "(f ---> Inf (f ` ({x<..} \<inter> I))) (at x within ({x<..} \<inter> I))"
  1182 proof cases
  1183   assume "{x<..} \<inter> I = {}" then show ?thesis by (simp add: Lim_within_empty)
  1184 next
  1185   assume [simp]: "{x<..} \<inter> I \<noteq> {}"
  1186   show ?thesis
  1187   proof (rule Lim_within_LIMSEQ, safe)
  1188     fix S assume S: "\<forall>n. S n \<noteq> x \<and> S n \<in> {x <..} \<inter> I" "S ----> x"
  1189     
  1190     show "(\<lambda>n. f (S n)) ----> Inf (f ` ({x<..} \<inter> I))"
  1191     proof (rule LIMSEQ_I, rule ccontr)
  1192       fix r :: real assume "0 < r"
  1193       with Inf_close[of "f ` ({x<..} \<inter> I)" r]
  1194       obtain y where y: "x < y" "y \<in> I" "f y < Inf (f ` ({x <..} \<inter> I)) + r" by auto
  1195       from `x < y` have "0 < y - x" by auto
  1196       from S(2)[THEN LIMSEQ_D, OF this]
  1197       obtain N where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>S n - x\<bar> < y - x" by auto
  1198       
  1199       assume "\<not> (\<exists>N. \<forall>n\<ge>N. norm (f (S n) - Inf (f ` ({x<..} \<inter> I))) < r)"
  1200       moreover have "\<And>n. Inf (f ` ({x<..} \<inter> I)) \<le> f (S n)"
  1201         using S bnd by (intro Inf_lower[where z=K]) auto
  1202       ultimately obtain n where n: "N \<le> n" "r + Inf (f ` ({x<..} \<inter> I)) \<le> f (S n)"
  1203         by (auto simp: not_less field_simps)
  1204       with N[OF n(1)] mono[OF _ `y \<in> I`, of "S n"] S(1)[THEN spec, of n] y
  1205       show False by auto
  1206     qed
  1207   qed
  1208 qed
  1209 
  1210 text{* Another limit point characterization. *}
  1211 
  1212 lemma islimpt_sequential:
  1213   fixes x :: "'a::metric_space"
  1214   shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S -{x}) \<and> (f ---> x) sequentially)"
  1215     (is "?lhs = ?rhs")
  1216 proof
  1217   assume ?lhs
  1218   then obtain f where f:"\<forall>y. y>0 \<longrightarrow> f y \<in> S \<and> f y \<noteq> x \<and> dist (f y) x < y"
  1219     unfolding islimpt_approachable using choice[of "\<lambda>e y. e>0 \<longrightarrow> y\<in>S \<and> y\<noteq>x \<and> dist y x < e"] by auto
  1220   { fix n::nat
  1221     have "f (inverse (real n + 1)) \<in> S - {x}" using f by auto
  1222   }
  1223   moreover
  1224   { fix e::real assume "e>0"
  1225     hence "\<exists>N::nat. inverse (real (N + 1)) < e" using real_arch_inv[of e] apply (auto simp add: Suc_pred') apply(rule_tac x="n - 1" in exI) by auto
  1226     then obtain N::nat where "inverse (real (N + 1)) < e" by auto
  1227     hence "\<forall>n\<ge>N. inverse (real n + 1) < e" by (auto, metis Suc_le_mono le_SucE less_imp_inverse_less nat_le_real_less order_less_trans real_of_nat_Suc real_of_nat_Suc_gt_zero)
  1228     moreover have "\<forall>n\<ge>N. dist (f (inverse (real n + 1))) x < (inverse (real n + 1))" using f `e>0` by auto
  1229     ultimately have "\<exists>N::nat. \<forall>n\<ge>N. dist (f (inverse (real n + 1))) x < e" apply(rule_tac x=N in exI) apply auto apply(erule_tac x=n in allE)+ by auto
  1230   }
  1231   hence " ((\<lambda>n. f (inverse (real n + 1))) ---> x) sequentially"
  1232     unfolding Lim_sequentially using f by auto
  1233   ultimately show ?rhs apply (rule_tac x="(\<lambda>n::nat. f (inverse (real n + 1)))" in exI) by auto
  1234 next
  1235   assume ?rhs
  1236   then obtain f::"nat\<Rightarrow>'a"  where f:"(\<forall>n. f n \<in> S - {x})" "(\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f n) x < e)" unfolding Lim_sequentially by auto
  1237   { fix e::real assume "e>0"
  1238     then obtain N where "dist (f N) x < e" using f(2) by auto
  1239     moreover have "f N\<in>S" "f N \<noteq> x" using f(1) by auto
  1240     ultimately have "\<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" by auto
  1241   }
  1242   thus ?lhs unfolding islimpt_approachable by auto
  1243 qed
  1244 
  1245 text{* Basic arithmetical combining theorems for limits. *}
  1246 
  1247 lemma Lim_linear:
  1248   assumes "(f ---> l) net" "bounded_linear h"
  1249   shows "((\<lambda>x. h (f x)) ---> h l) net"
  1250 using `bounded_linear h` `(f ---> l) net`
  1251 by (rule bounded_linear.tendsto)
  1252 
  1253 lemma Lim_ident_at: "((\<lambda>x. x) ---> a) (at a)"
  1254   unfolding tendsto_def Limits.eventually_at_topological by fast
  1255 
  1256 lemma Lim_const[intro]: "((\<lambda>x. a) ---> a) net" by (rule tendsto_const)
  1257 
  1258 lemma Lim_cmul[intro]:
  1259   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1260   shows "(f ---> l) net ==> ((\<lambda>x. c *\<^sub>R f x) ---> c *\<^sub>R l) net"
  1261   by (intro tendsto_intros)
  1262 
  1263 lemma Lim_neg:
  1264   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1265   shows "(f ---> l) net ==> ((\<lambda>x. -(f x)) ---> -l) net"
  1266   by (rule tendsto_minus)
  1267 
  1268 lemma Lim_add: fixes f :: "'a \<Rightarrow> 'b::real_normed_vector" shows
  1269  "(f ---> l) net \<Longrightarrow> (g ---> m) net \<Longrightarrow> ((\<lambda>x. f(x) + g(x)) ---> l + m) net"
  1270   by (rule tendsto_add)
  1271 
  1272 lemma Lim_sub:
  1273   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1274   shows "(f ---> l) net \<Longrightarrow> (g ---> m) net \<Longrightarrow> ((\<lambda>x. f(x) - g(x)) ---> l - m) net"
  1275   by (rule tendsto_diff)
  1276 
  1277 lemma Lim_mul:
  1278   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1279   assumes "(c ---> d) net"  "(f ---> l) net"
  1280   shows "((\<lambda>x. c(x) *\<^sub>R f x) ---> (d *\<^sub>R l)) net"
  1281   using assms by (rule scaleR.tendsto)
  1282 
  1283 lemma Lim_inv:
  1284   fixes f :: "'a \<Rightarrow> real"
  1285   assumes "(f ---> l) (net::'a net)"  "l \<noteq> 0"
  1286   shows "((inverse o f) ---> inverse l) net"
  1287   unfolding o_def using assms by (rule tendsto_inverse)
  1288 
  1289 lemma Lim_vmul:
  1290   fixes c :: "'a \<Rightarrow> real" and v :: "'b::real_normed_vector"
  1291   shows "(c ---> d) net ==> ((\<lambda>x. c(x) *\<^sub>R v) ---> d *\<^sub>R v) net"
  1292   by (intro tendsto_intros)
  1293 
  1294 lemma Lim_null:
  1295   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1296   shows "(f ---> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) ---> 0) net" by (simp add: Lim dist_norm)
  1297 
  1298 lemma Lim_null_norm:
  1299   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1300   shows "(f ---> 0) net \<longleftrightarrow> ((\<lambda>x. norm(f x)) ---> 0) net"
  1301   by (simp add: Lim dist_norm)
  1302 
  1303 lemma Lim_null_comparison:
  1304   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1305   assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g ---> 0) net"
  1306   shows "(f ---> 0) net"
  1307 proof(simp add: tendsto_iff, rule+)
  1308   fix e::real assume "0<e"
  1309   { fix x
  1310     assume "norm (f x) \<le> g x" "dist (g x) 0 < e"
  1311     hence "dist (f x) 0 < e" by (simp add: dist_norm)
  1312   }
  1313   thus "eventually (\<lambda>x. dist (f x) 0 < e) net"
  1314     using eventually_and[of "\<lambda>x. norm(f x) <= g x" "\<lambda>x. dist (g x) 0 < e" net]
  1315     using eventually_mono[of "(\<lambda>x. norm (f x) \<le> g x \<and> dist (g x) 0 < e)" "(\<lambda>x. dist (f x) 0 < e)" net]
  1316     using assms `e>0` unfolding tendsto_iff by auto
  1317 qed
  1318 
  1319 lemma Lim_component:
  1320   fixes f :: "'a \<Rightarrow> ('a::euclidean_space)"
  1321   shows "(f ---> l) net \<Longrightarrow> ((\<lambda>a. f a $$i) ---> l$$i) net"
  1322   unfolding tendsto_iff
  1323   apply (clarify)
  1324   apply (drule spec, drule (1) mp)
  1325   apply (erule eventually_elim1)
  1326   apply (erule le_less_trans [OF dist_nth_le])
  1327   done
  1328 
  1329 lemma Lim_transform_bound:
  1330   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1331   fixes g :: "'a \<Rightarrow> 'c::real_normed_vector"
  1332   assumes "eventually (\<lambda>n. norm(f n) <= norm(g n)) net"  "(g ---> 0) net"
  1333   shows "(f ---> 0) net"
  1334 proof (rule tendstoI)
  1335   fix e::real assume "e>0"
  1336   { fix x
  1337     assume "norm (f x) \<le> norm (g x)" "dist (g x) 0 < e"
  1338     hence "dist (f x) 0 < e" by (simp add: dist_norm)}
  1339   thus "eventually (\<lambda>x. dist (f x) 0 < e) net"
  1340     using eventually_and[of "\<lambda>x. norm (f x) \<le> norm (g x)" "\<lambda>x. dist (g x) 0 < e" net]
  1341     using eventually_mono[of "\<lambda>x. norm (f x) \<le> norm (g x) \<and> dist (g x) 0 < e" "\<lambda>x. dist (f x) 0 < e" net]
  1342     using assms `e>0` unfolding tendsto_iff by blast
  1343 qed
  1344 
  1345 text{* Deducing things about the limit from the elements. *}
  1346 
  1347 lemma Lim_in_closed_set:
  1348   assumes "closed S" "eventually (\<lambda>x. f(x) \<in> S) net" "\<not>(trivial_limit net)" "(f ---> l) net"
  1349   shows "l \<in> S"
  1350 proof (rule ccontr)
  1351   assume "l \<notin> S"
  1352   with `closed S` have "open (- S)" "l \<in> - S"
  1353     by (simp_all add: open_Compl)
  1354   with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
  1355     by (rule topological_tendstoD)
  1356   with assms(2) have "eventually (\<lambda>x. False) net"
  1357     by (rule eventually_elim2) simp
  1358   with assms(3) show "False"
  1359     by (simp add: eventually_False)
  1360 qed
  1361 
  1362 text{* Need to prove closed(cball(x,e)) before deducing this as a corollary. *}
  1363 
  1364 lemma Lim_dist_ubound:
  1365   assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. dist a (f x) <= e) net"
  1366   shows "dist a l <= e"
  1367 proof (rule ccontr)
  1368   assume "\<not> dist a l \<le> e"
  1369   then have "0 < dist a l - e" by simp
  1370   with assms(2) have "eventually (\<lambda>x. dist (f x) l < dist a l - e) net"
  1371     by (rule tendstoD)
  1372   with assms(3) have "eventually (\<lambda>x. dist a (f x) \<le> e \<and> dist (f x) l < dist a l - e) net"
  1373     by (rule eventually_conjI)
  1374   then obtain w where "dist a (f w) \<le> e" "dist (f w) l < dist a l - e"
  1375     using assms(1) eventually_happens by auto
  1376   hence "dist a (f w) + dist (f w) l < e + (dist a l - e)"
  1377     by (rule add_le_less_mono)
  1378   hence "dist a (f w) + dist (f w) l < dist a l"
  1379     by simp
  1380   also have "\<dots> \<le> dist a (f w) + dist (f w) l"
  1381     by (rule dist_triangle)
  1382   finally show False by simp
  1383 qed
  1384 
  1385 lemma Lim_norm_ubound:
  1386   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1387   assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. norm(f x) <= e) net"
  1388   shows "norm(l) <= e"
  1389 proof (rule ccontr)
  1390   assume "\<not> norm l \<le> e"
  1391   then have "0 < norm l - e" by simp
  1392   with assms(2) have "eventually (\<lambda>x. dist (f x) l < norm l - e) net"
  1393     by (rule tendstoD)
  1394   with assms(3) have "eventually (\<lambda>x. norm (f x) \<le> e \<and> dist (f x) l < norm l - e) net"
  1395     by (rule eventually_conjI)
  1396   then obtain w where "norm (f w) \<le> e" "dist (f w) l < norm l - e"
  1397     using assms(1) eventually_happens by auto
  1398   hence "norm (f w - l) < norm l - e" "norm (f w) \<le> e" by (simp_all add: dist_norm)
  1399   hence "norm (f w - l) + norm (f w) < norm l" by simp
  1400   hence "norm (f w - l - f w) < norm l" by (rule le_less_trans [OF norm_triangle_ineq4])
  1401   thus False using `\<not> norm l \<le> e` by simp
  1402 qed
  1403 
  1404 lemma Lim_norm_lbound:
  1405   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
  1406   assumes "\<not> (trivial_limit net)"  "(f ---> l) net"  "eventually (\<lambda>x. e <= norm(f x)) net"
  1407   shows "e \<le> norm l"
  1408 proof (rule ccontr)
  1409   assume "\<not> e \<le> norm l"
  1410   then have "0 < e - norm l" by simp
  1411   with assms(2) have "eventually (\<lambda>x. dist (f x) l < e - norm l) net"
  1412     by (rule tendstoD)
  1413   with assms(3) have "eventually (\<lambda>x. e \<le> norm (f x) \<and> dist (f x) l < e - norm l) net"
  1414     by (rule eventually_conjI)
  1415   then obtain w where "e \<le> norm (f w)" "dist (f w) l < e - norm l"
  1416     using assms(1) eventually_happens by auto
  1417   hence "norm (f w - l) + norm l < e" "e \<le> norm (f w)" by (simp_all add: dist_norm)
  1418   hence "norm (f w - l) + norm l < norm (f w)" by (rule less_le_trans)
  1419   hence "norm (f w - l + l) < norm (f w)" by (rule le_less_trans [OF norm_triangle_ineq])
  1420   thus False by simp
  1421 qed
  1422 
  1423 text{* Uniqueness of the limit, when nontrivial. *}
  1424 
  1425 lemma tendsto_Lim:
  1426   fixes f :: "'a \<Rightarrow> 'b::t2_space"
  1427   shows "~(trivial_limit net) \<Longrightarrow> (f ---> l) net ==> Lim net f = l"
  1428   unfolding Lim_def using tendsto_unique[of net f] by auto
  1429 
  1430 text{* Limit under bilinear function *}
  1431 
  1432 lemma Lim_bilinear:
  1433   assumes "(f ---> l) net" and "(g ---> m) net" and "bounded_bilinear h"
  1434   shows "((\<lambda>x. h (f x) (g x)) ---> (h l m)) net"
  1435 using `bounded_bilinear h` `(f ---> l) net` `(g ---> m) net`
  1436 by (rule bounded_bilinear.tendsto)
  1437 
  1438 text{* These are special for limits out of the same vector space. *}
  1439 
  1440 lemma Lim_within_id: "(id ---> a) (at a within s)"
  1441   unfolding tendsto_def Limits.eventually_within eventually_at_topological
  1442   by auto
  1443 
  1444 lemmas Lim_intros = Lim_add Lim_const Lim_sub Lim_cmul Lim_vmul Lim_within_id
  1445 
  1446 lemma Lim_at_id: "(id ---> a) (at a)"
  1447 apply (subst within_UNIV[symmetric]) by (simp add: Lim_within_id)
  1448 
  1449 lemma Lim_at_zero:
  1450   fixes a :: "'a::real_normed_vector"
  1451   fixes l :: "'b::topological_space"
  1452   shows "(f ---> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) ---> l) (at 0)" (is "?lhs = ?rhs")
  1453 proof
  1454   assume "?lhs"
  1455   { fix S assume "open S" "l \<in> S"
  1456     with `?lhs` have "eventually (\<lambda>x. f x \<in> S) (at a)"
  1457       by (rule topological_tendstoD)
  1458     then obtain d where d: "d>0" "\<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<in> S"
  1459       unfolding Limits.eventually_at by fast
  1460     { fix x::"'a" assume "x \<noteq> 0 \<and> dist x 0 < d"
  1461       hence "f (a + x) \<in> S" using d
  1462       apply(erule_tac x="x+a" in allE)
  1463       by (auto simp add: add_commute dist_norm dist_commute)
  1464     }
  1465     hence "\<exists>d>0. \<forall>x. x \<noteq> 0 \<and> dist x 0 < d \<longrightarrow> f (a + x) \<in> S"
  1466       using d(1) by auto
  1467     hence "eventually (\<lambda>x. f (a + x) \<in> S) (at 0)"
  1468       unfolding Limits.eventually_at .
  1469   }
  1470   thus "?rhs" by (rule topological_tendstoI)
  1471 next
  1472   assume "?rhs"
  1473   { fix S assume "open S" "l \<in> S"
  1474     with `?rhs` have "eventually (\<lambda>x. f (a + x) \<in> S) (at 0)"
  1475       by (rule topological_tendstoD)
  1476     then obtain d where d: "d>0" "\<forall>x. x \<noteq> 0 \<and> dist x 0 < d \<longrightarrow> f (a + x) \<in> S"
  1477       unfolding Limits.eventually_at by fast
  1478     { fix x::"'a" assume "x \<noteq> a \<and> dist x a < d"
  1479       hence "f x \<in> S" using d apply(erule_tac x="x-a" in allE)
  1480         by(auto simp add: add_commute dist_norm dist_commute)
  1481     }
  1482     hence "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<in> S" using d(1) by auto
  1483     hence "eventually (\<lambda>x. f x \<in> S) (at a)" unfolding Limits.eventually_at .
  1484   }
  1485   thus "?lhs" by (rule topological_tendstoI)
  1486 qed
  1487 
  1488 text{* It's also sometimes useful to extract the limit point from the net.  *}
  1489 
  1490 definition
  1491   netlimit :: "'a::t2_space net \<Rightarrow> 'a" where
  1492   "netlimit net = (SOME a. ((\<lambda>x. x) ---> a) net)"
  1493 
  1494 lemma netlimit_within:
  1495   assumes "\<not> trivial_limit (at a within S)"
  1496   shows "netlimit (at a within S) = a"
  1497 unfolding netlimit_def
  1498 apply (rule some_equality)
  1499 apply (rule Lim_at_within)
  1500 apply (rule Lim_ident_at)
  1501 apply (erule tendsto_unique [OF assms])
  1502 apply (rule Lim_at_within)
  1503 apply (rule Lim_ident_at)
  1504 done
  1505 
  1506 lemma netlimit_at:
  1507   fixes a :: "'a::{perfect_space,t2_space}"
  1508   shows "netlimit (at a) = a"
  1509   apply (subst within_UNIV[symmetric])
  1510   using netlimit_within[of a UNIV]
  1511   by (simp add: trivial_limit_at within_UNIV)
  1512 
  1513 text{* Transformation of limit. *}
  1514 
  1515 lemma Lim_transform:
  1516   fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
  1517   assumes "((\<lambda>x. f x - g x) ---> 0) net" "(f ---> l) net"
  1518   shows "(g ---> l) net"
  1519 proof-
  1520   from assms have "((\<lambda>x. f x - g x - f x) ---> 0 - l) net" using Lim_sub[of "\<lambda>x. f x - g x" 0 net f l] by auto
  1521   thus "?thesis" using Lim_neg [of "\<lambda> x. - g x" "-l" net] by auto
  1522 qed
  1523 
  1524 lemma Lim_transform_eventually:
  1525   "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> (f ---> l) net \<Longrightarrow> (g ---> l) net"
  1526   apply (rule topological_tendstoI)
  1527   apply (drule (2) topological_tendstoD)
  1528   apply (erule (1) eventually_elim2, simp)
  1529   done
  1530 
  1531 lemma Lim_transform_within:
  1532   assumes "0 < d" and "\<forall>x'\<in>S. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"
  1533   and "(f ---> l) (at x within S)"
  1534   shows "(g ---> l) (at x within S)"
  1535 proof (rule Lim_transform_eventually)
  1536   show "eventually (\<lambda>x. f x = g x) (at x within S)"
  1537     unfolding eventually_within
  1538     using assms(1,2) by auto
  1539   show "(f ---> l) (at x within S)" by fact
  1540 qed
  1541 
  1542 lemma Lim_transform_at:
  1543   assumes "0 < d" and "\<forall>x'. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"
  1544   and "(f ---> l) (at x)"
  1545   shows "(g ---> l) (at x)"
  1546 proof (rule Lim_transform_eventually)
  1547   show "eventually (\<lambda>x. f x = g x) (at x)"
  1548     unfolding eventually_at
  1549     using assms(1,2) by auto
  1550   show "(f ---> l) (at x)" by fact
  1551 qed
  1552 
  1553 text{* Common case assuming being away from some crucial point like 0. *}
  1554 
  1555 lemma Lim_transform_away_within:
  1556   fixes a b :: "'a::t1_space"
  1557   assumes "a \<noteq> b" and "\<forall>x\<in>S. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
  1558   and "(f ---> l) (at a within S)"
  1559   shows "(g ---> l) (at a within S)"
  1560 proof (rule Lim_transform_eventually)
  1561   show "(f ---> l) (at a within S)" by fact
  1562   show "eventually (\<lambda>x. f x = g x) (at a within S)"
  1563     unfolding Limits.eventually_within eventually_at_topological
  1564     by (rule exI [where x="- {b}"], simp add: open_Compl assms)
  1565 qed
  1566 
  1567 lemma Lim_transform_away_at:
  1568   fixes a b :: "'a::t1_space"
  1569   assumes ab: "a\<noteq>b" and fg: "\<forall>x. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
  1570   and fl: "(f ---> l) (at a)"
  1571   shows "(g ---> l) (at a)"
  1572   using Lim_transform_away_within[OF ab, of UNIV f g l] fg fl
  1573   by (auto simp add: within_UNIV)
  1574 
  1575 text{* Alternatively, within an open set. *}
  1576 
  1577 lemma Lim_transform_within_open:
  1578   assumes "open S" and "a \<in> S" and "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> f x = g x"
  1579   and "(f ---> l) (at a)"
  1580   shows "(g ---> l) (at a)"
  1581 proof (rule Lim_transform_eventually)
  1582   show "eventually (\<lambda>x. f x = g x) (at a)"
  1583     unfolding eventually_at_topological
  1584     using assms(1,2,3) by auto
  1585   show "(f ---> l) (at a)" by fact
  1586 qed
  1587 
  1588 text{* A congruence rule allowing us to transform limits assuming not at point. *}
  1589 
  1590 (* FIXME: Only one congruence rule for tendsto can be used at a time! *)
  1591 
  1592 lemma Lim_cong_within(*[cong add]*):
  1593   assumes "a = b" "x = y" "S = T"
  1594   assumes "\<And>x. x \<noteq> b \<Longrightarrow> x \<in> T \<Longrightarrow> f x = g x"
  1595   shows "(f ---> x) (at a within S) \<longleftrightarrow> (g ---> y) (at b within T)"
  1596   unfolding tendsto_def Limits.eventually_within eventually_at_topological
  1597   using assms by simp
  1598 
  1599 lemma Lim_cong_at(*[cong add]*):
  1600   assumes "a = b" "x = y"
  1601   assumes "\<And>x. x \<noteq> a \<Longrightarrow> f x = g x"
  1602   shows "((\<lambda>x. f x) ---> x) (at a) \<longleftrightarrow> ((g ---> y) (at a))"
  1603   unfolding tendsto_def eventually_at_topological
  1604   using assms by simp
  1605 
  1606 text{* Useful lemmas on closure and set of possible sequential limits.*}
  1607 
  1608 lemma closure_sequential:
  1609   fixes l :: "'a::metric_space"
  1610   shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially)" (is "?lhs = ?rhs")
  1611 proof
  1612   assume "?lhs" moreover
  1613   { assume "l \<in> S"
  1614     hence "?rhs" using Lim_const[of l sequentially] by auto
  1615   } moreover
  1616   { assume "l islimpt S"
  1617     hence "?rhs" unfolding islimpt_sequential by auto
  1618   } ultimately
  1619   show "?rhs" unfolding closure_def by auto
  1620 next
  1621   assume "?rhs"
  1622   thus "?lhs" unfolding closure_def unfolding islimpt_sequential by auto
  1623 qed
  1624 
  1625 lemma closed_sequential_limits:
  1626   fixes S :: "'a::metric_space set"
  1627   shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially \<longrightarrow> l \<in> S)"
  1628   unfolding closed_limpt
  1629   using closure_sequential [where 'a='a] closure_closed [where 'a='a] closed_limpt [where 'a='a] islimpt_sequential [where 'a='a] mem_delete [where 'a='a]
  1630   by metis
  1631 
  1632 lemma closure_approachable:
  1633   fixes S :: "'a::metric_space set"
  1634   shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
  1635   apply (auto simp add: closure_def islimpt_approachable)
  1636   by (metis dist_self)
  1637 
  1638 lemma closed_approachable:
  1639   fixes S :: "'a::metric_space set"
  1640   shows "closed S ==> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
  1641   by (metis closure_closed closure_approachable)
  1642 
  1643 text{* Some other lemmas about sequences. *}
  1644 
  1645 lemma sequentially_offset:
  1646   assumes "eventually (\<lambda>i. P i) sequentially"
  1647   shows "eventually (\<lambda>i. P (i + k)) sequentially"
  1648   using assms unfolding eventually_sequentially by (metis trans_le_add1)
  1649 
  1650 lemma seq_offset:
  1651   assumes "(f ---> l) sequentially"
  1652   shows "((\<lambda>i. f (i + k)) ---> l) sequentially"
  1653   using assms unfolding tendsto_def
  1654   by clarify (rule sequentially_offset, simp)
  1655 
  1656 lemma seq_offset_neg:
  1657   "(f ---> l) sequentially ==> ((\<lambda>i. f(i - k)) ---> l) sequentially"
  1658   apply (rule topological_tendstoI)
  1659   apply (drule (2) topological_tendstoD)
  1660   apply (simp only: eventually_sequentially)
  1661   apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k")
  1662   apply metis
  1663   by arith
  1664 
  1665 lemma seq_offset_rev:
  1666   "((\<lambda>i. f(i + k)) ---> l) sequentially ==> (f ---> l) sequentially"
  1667   apply (rule topological_tendstoI)
  1668   apply (drule (2) topological_tendstoD)
  1669   apply (simp only: eventually_sequentially)
  1670   apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k \<and> (n - k) + k = n")
  1671   by metis arith
  1672 
  1673 lemma seq_harmonic: "((\<lambda>n. inverse (real n)) ---> 0) sequentially"
  1674 proof-
  1675   { fix e::real assume "e>0"
  1676     hence "\<exists>N::nat. \<forall>n::nat\<ge>N. inverse (real n) < e"
  1677       using real_arch_inv[of e] apply auto apply(rule_tac x=n in exI)
  1678       by (metis le_imp_inverse_le not_less real_of_nat_gt_zero_cancel_iff real_of_nat_less_iff xt1(7))
  1679   }
  1680   thus ?thesis unfolding Lim_sequentially dist_norm by simp
  1681 qed
  1682 
  1683 subsection {* More properties of closed balls. *}
  1684 
  1685 lemma closed_cball: "closed (cball x e)"
  1686 unfolding cball_def closed_def
  1687 unfolding Collect_neg_eq [symmetric] not_le
  1688 apply (clarsimp simp add: open_dist, rename_tac y)
  1689 apply (rule_tac x="dist x y - e" in exI, clarsimp)
  1690 apply (rename_tac x')
  1691 apply (cut_tac x=x and y=x' and z=y in dist_triangle)
  1692 apply simp
  1693 done
  1694 
  1695 lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
  1696 proof-
  1697   { fix x and e::real assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
  1698     hence "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
  1699   } moreover
  1700   { fix x and e::real assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
  1701     hence "\<exists>d>0. ball x d \<subseteq> S" unfolding subset_eq apply(rule_tac x="e/2" in exI) by auto
  1702   } ultimately
  1703   show ?thesis unfolding open_contains_ball by auto
  1704 qed
  1705 
  1706 lemma open_contains_cball_eq: "open S ==> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
  1707   by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball mem_def)
  1708 
  1709 lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"
  1710   apply (simp add: interior_def, safe)
  1711   apply (force simp add: open_contains_cball)
  1712   apply (rule_tac x="ball x e" in exI)
  1713   apply (simp add: subset_trans [OF ball_subset_cball])
  1714   done
  1715 
  1716 lemma islimpt_ball:
  1717   fixes x y :: "'a::{real_normed_vector,perfect_space}"
  1718   shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e" (is "?lhs = ?rhs")
  1719 proof
  1720   assume "?lhs"
  1721   { assume "e \<le> 0"
  1722     hence *:"ball x e = {}" using ball_eq_empty[of x e] by auto
  1723     have False using `?lhs` unfolding * using islimpt_EMPTY[of y] by auto
  1724   }
  1725   hence "e > 0" by (metis not_less)
  1726   moreover
  1727   have "y \<in> cball x e" using closed_cball[of x e] islimpt_subset[of y "ball x e" "cball x e"] ball_subset_cball[of x e] `?lhs` unfolding closed_limpt by auto
  1728   ultimately show "?rhs" by auto
  1729 next
  1730   assume "?rhs" hence "e>0"  by auto
  1731   { fix d::real assume "d>0"
  1732     have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1733     proof(cases "d \<le> dist x y")
  1734       case True thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1735       proof(cases "x=y")
  1736         case True hence False using `d \<le> dist x y` `d>0` by auto
  1737         thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by auto
  1738       next
  1739         case False
  1740 
  1741         have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x))
  1742               = norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"
  1743           unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[THEN sym] by auto
  1744         also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"
  1745           using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", THEN sym, of "y - x"]
  1746           unfolding scaleR_minus_left scaleR_one
  1747           by (auto simp add: norm_minus_commute)
  1748         also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"
  1749           unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]
  1750           unfolding left_distrib using `x\<noteq>y`[unfolded dist_nz, unfolded dist_norm] by auto
  1751         also have "\<dots> \<le> e - d/2" using `d \<le> dist x y` and `d>0` and `?rhs` by(auto simp add: dist_norm)
  1752         finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using `d>0` by auto
  1753 
  1754         moreover
  1755 
  1756         have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"
  1757           using `x\<noteq>y`[unfolded dist_nz] `d>0` unfolding scaleR_eq_0_iff by (auto simp add: dist_commute)
  1758         moreover
  1759         have "dist (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) y < d" unfolding dist_norm apply simp unfolding norm_minus_cancel
  1760           using `d>0` `x\<noteq>y`[unfolded dist_nz] dist_commute[of x y]
  1761           unfolding dist_norm by auto
  1762         ultimately show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by (rule_tac  x="y - (d / (2*dist y x)) *\<^sub>R (y - x)" in bexI) auto
  1763       qed
  1764     next
  1765       case False hence "d > dist x y" by auto
  1766       show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1767       proof(cases "x=y")
  1768         case True
  1769         obtain z where **: "z \<noteq> y" "dist z y < min e d"
  1770           using perfect_choose_dist[of "min e d" y]
  1771           using `d > 0` `e>0` by auto
  1772         show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1773           unfolding `x = y`
  1774           using `z \<noteq> y` **
  1775           by (rule_tac x=z in bexI, auto simp add: dist_commute)
  1776       next
  1777         case False thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"
  1778           using `d>0` `d > dist x y` `?rhs` by(rule_tac x=x in bexI, auto)
  1779       qed
  1780     qed  }
  1781   thus "?lhs" unfolding mem_cball islimpt_approachable mem_ball by auto
  1782 qed
  1783 
  1784 lemma closure_ball_lemma:
  1785   fixes x y :: "'a::real_normed_vector"
  1786   assumes "x \<noteq> y" shows "y islimpt ball x (dist x y)"
  1787 proof (rule islimptI)
  1788   fix T assume "y \<in> T" "open T"
  1789   then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"
  1790     unfolding open_dist by fast
  1791   (* choose point between x and y, within distance r of y. *)
  1792   def k \<equiv> "min 1 (r / (2 * dist x y))"
  1793   def z \<equiv> "y + scaleR k (x - y)"
  1794   have z_def2: "z = x + scaleR (1 - k) (y - x)"
  1795     unfolding z_def by (simp add: algebra_simps)
  1796   have "dist z y < r"
  1797     unfolding z_def k_def using `0 < r`
  1798     by (simp add: dist_norm min_def)
  1799   hence "z \<in> T" using `\<forall>z. dist z y < r \<longrightarrow> z \<in> T` by simp
  1800   have "dist x z < dist x y"
  1801     unfolding z_def2 dist_norm
  1802     apply (simp add: norm_minus_commute)
  1803     apply (simp only: dist_norm [symmetric])
  1804     apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)
  1805     apply (rule mult_strict_right_mono)
  1806     apply (simp add: k_def divide_pos_pos zero_less_dist_iff `0 < r` `x \<noteq> y`)
  1807     apply (simp add: zero_less_dist_iff `x \<noteq> y`)
  1808     done
  1809   hence "z \<in> ball x (dist x y)" by simp
  1810   have "z \<noteq> y"
  1811     unfolding z_def k_def using `x \<noteq> y` `0 < r`
  1812     by (simp add: min_def)
  1813   show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"
  1814     using `z \<in> ball x (dist x y)` `z \<in> T` `z \<noteq> y`
  1815     by fast
  1816 qed
  1817 
  1818 lemma closure_ball:
  1819   fixes x :: "'a::real_normed_vector"
  1820   shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"
  1821 apply (rule equalityI)
  1822 apply (rule closure_minimal)
  1823 apply (rule ball_subset_cball)
  1824 apply (rule closed_cball)
  1825 apply (rule subsetI, rename_tac y)
  1826 apply (simp add: le_less [where 'a=real])
  1827 apply (erule disjE)
  1828 apply (rule subsetD [OF closure_subset], simp)
  1829 apply (simp add: closure_def)
  1830 apply clarify
  1831 apply (rule closure_ball_lemma)
  1832 apply (simp add: zero_less_dist_iff)
  1833 done
  1834 
  1835 (* In a trivial vector space, this fails for e = 0. *)
  1836 lemma interior_cball:
  1837   fixes x :: "'a::{real_normed_vector, perfect_space}"
  1838   shows "interior (cball x e) = ball x e"
  1839 proof(cases "e\<ge>0")
  1840   case False note cs = this
  1841   from cs have "ball x e = {}" using ball_empty[of e x] by auto moreover
  1842   { fix y assume "y \<in> cball x e"
  1843     hence False unfolding mem_cball using dist_nz[of x y] cs by auto  }
  1844   hence "cball x e = {}" by auto
  1845   hence "interior (cball x e) = {}" using interior_empty by auto
  1846   ultimately show ?thesis by blast
  1847 next
  1848   case True note cs = this
  1849   have "ball x e \<subseteq> cball x e" using ball_subset_cball by auto moreover
  1850   { fix S y assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"
  1851     then obtain d where "d>0" and d:"\<forall>x'. dist x' y < d \<longrightarrow> x' \<in> S" unfolding open_dist by blast
  1852 
  1853     then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"
  1854       using perfect_choose_dist [of d] by auto
  1855     have "xa\<in>S" using d[THEN spec[where x=xa]] using xa by(auto simp add: dist_commute)
  1856     hence xa_cball:"xa \<in> cball x e" using as(1) by auto
  1857 
  1858     hence "y \<in> ball x e" proof(cases "x = y")
  1859       case True
  1860       hence "e>0" using xa_y[unfolded dist_nz] xa_cball[unfolded mem_cball] by (auto simp add: dist_commute)
  1861       thus "y \<in> ball x e" using `x = y ` by simp
  1862     next
  1863       case False
  1864       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d" unfolding dist_norm
  1865         using `d>0` norm_ge_zero[of "y - x"] `x \<noteq> y` by auto
  1866       hence *:"y + (d / 2 / dist y x) *\<^sub>R (y - x) \<in> cball x e" using d as(1)[unfolded subset_eq] by blast
  1867       have "y - x \<noteq> 0" using `x \<noteq> y` by auto
  1868       hence **:"d / (2 * norm (y - x)) > 0" unfolding zero_less_norm_iff[THEN sym]
  1869         using `d>0` divide_pos_pos[of d "2*norm (y - x)"] by auto
  1870 
  1871       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) x = norm (y + (d / (2 * norm (y - x))) *\<^sub>R y - (d / (2 * norm (y - x))) *\<^sub>R x - x)"
  1872         by (auto simp add: dist_norm algebra_simps)
  1873       also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"
  1874         by (auto simp add: algebra_simps)
  1875       also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"
  1876         using ** by auto
  1877       also have "\<dots> = (dist y x) + d/2"using ** by (auto simp add: left_distrib dist_norm)
  1878       finally have "e \<ge> dist x y +d/2" using *[unfolded mem_cball] by (auto simp add: dist_commute)
  1879       thus "y \<in> ball x e" unfolding mem_ball using `d>0` by auto
  1880     qed  }
  1881   hence "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e" by auto
  1882   ultimately show ?thesis using interior_unique[of "ball x e" "cball x e"] using open_ball[of x e] by auto
  1883 qed
  1884 
  1885 lemma frontier_ball:
  1886   fixes a :: "'a::real_normed_vector"
  1887   shows "0 < e ==> frontier(ball a e) = {x. dist a x = e}"
  1888   apply (simp add: frontier_def closure_ball interior_open order_less_imp_le)
  1889   apply (simp add: set_eq_iff)
  1890   by arith
  1891 
  1892 lemma frontier_cball:
  1893   fixes a :: "'a::{real_normed_vector, perfect_space}"
  1894   shows "frontier(cball a e) = {x. dist a x = e}"
  1895   apply (simp add: frontier_def interior_cball closed_cball order_less_imp_le)
  1896   apply (simp add: set_eq_iff)
  1897   by arith
  1898 
  1899 lemma cball_eq_empty: "(cball x e = {}) \<longleftrightarrow> e < 0"
  1900   apply (simp add: set_eq_iff not_le)
  1901   by (metis zero_le_dist dist_self order_less_le_trans)
  1902 lemma cball_empty: "e < 0 ==> cball x e = {}" by (simp add: cball_eq_empty)
  1903 
  1904 lemma cball_eq_sing:
  1905   fixes x :: "'a::{metric_space,perfect_space}"
  1906   shows "(cball x e = {x}) \<longleftrightarrow> e = 0"
  1907 proof (rule linorder_cases)
  1908   assume e: "0 < e"
  1909   obtain a where "a \<noteq> x" "dist a x < e"
  1910     using perfect_choose_dist [OF e] by auto
  1911   hence "a \<noteq> x" "dist x a \<le> e" by (auto simp add: dist_commute)
  1912   with e show ?thesis by (auto simp add: set_eq_iff)
  1913 qed auto
  1914 
  1915 lemma cball_sing:
  1916   fixes x :: "'a::metric_space"
  1917   shows "e = 0 ==> cball x e = {x}"
  1918   by (auto simp add: set_eq_iff)
  1919 
  1920 text{* For points in the interior, localization of limits makes no difference.   *}
  1921 
  1922 lemma eventually_within_interior:
  1923   assumes "x \<in> interior S"
  1924   shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)" (is "?lhs = ?rhs")
  1925 proof-
  1926   from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S"
  1927     unfolding interior_def by fast
  1928   { assume "?lhs"
  1929     then obtain A where "open A" "x \<in> A" "\<forall>y\<in>A. y \<noteq> x \<longrightarrow> y \<in> S \<longrightarrow> P y"
  1930       unfolding Limits.eventually_within Limits.eventually_at_topological
  1931       by auto
  1932     with T have "open (A \<inter> T)" "x \<in> A \<inter> T" "\<forall>y\<in>(A \<inter> T). y \<noteq> x \<longrightarrow> P y"
  1933       by auto
  1934     then have "?rhs"
  1935       unfolding Limits.eventually_at_topological by auto
  1936   } moreover
  1937   { assume "?rhs" hence "?lhs"
  1938       unfolding Limits.eventually_within
  1939       by (auto elim: eventually_elim1)
  1940   } ultimately
  1941   show "?thesis" ..
  1942 qed
  1943 
  1944 lemma at_within_interior:
  1945   "x \<in> interior S \<Longrightarrow> at x within S = at x"
  1946   by (simp add: expand_net_eq eventually_within_interior)
  1947 
  1948 lemma lim_within_interior:
  1949   "x \<in> interior S \<Longrightarrow> (f ---> l) (at x within S) \<longleftrightarrow> (f ---> l) (at x)"
  1950   by (simp add: at_within_interior)
  1951 
  1952 lemma netlimit_within_interior:
  1953   fixes x :: "'a::{t2_space,perfect_space}"
  1954   assumes "x \<in> interior S"
  1955   shows "netlimit (at x within S) = x"
  1956 using assms by (simp add: at_within_interior netlimit_at)
  1957 
  1958 subsection{* Boundedness. *}
  1959 
  1960   (* FIXME: This has to be unified with BSEQ!! *)
  1961 definition
  1962   bounded :: "'a::metric_space set \<Rightarrow> bool" where
  1963   "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
  1964 
  1965 lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
  1966 unfolding bounded_def
  1967 apply safe
  1968 apply (rule_tac x="dist a x + e" in exI, clarify)
  1969 apply (drule (1) bspec)
  1970 apply (erule order_trans [OF dist_triangle add_left_mono])
  1971 apply auto
  1972 done
  1973 
  1974 lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
  1975 unfolding bounded_any_center [where a=0]
  1976 by (simp add: dist_norm)
  1977 
  1978 lemma bounded_empty[simp]: "bounded {}" by (simp add: bounded_def)
  1979 lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T ==> bounded S"
  1980   by (metis bounded_def subset_eq)
  1981 
  1982 lemma bounded_interior[intro]: "bounded S ==> bounded(interior S)"
  1983   by (metis bounded_subset interior_subset)
  1984 
  1985 lemma bounded_closure[intro]: assumes "bounded S" shows "bounded(closure S)"
  1986 proof-
  1987   from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a" unfolding bounded_def by auto
  1988   { fix y assume "y \<in> closure S"
  1989     then obtain f where f: "\<forall>n. f n \<in> S"  "(f ---> y) sequentially"
  1990       unfolding closure_sequential by auto
  1991     have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
  1992     hence "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
  1993       by (rule eventually_mono, simp add: f(1))
  1994     have "dist x y \<le> a"
  1995       apply (rule Lim_dist_ubound [of sequentially f])
  1996       apply (rule trivial_limit_sequentially)
  1997       apply (rule f(2))
  1998       apply fact
  1999       done
  2000   }
  2001   thus ?thesis unfolding bounded_def by auto
  2002 qed
  2003 
  2004 lemma bounded_cball[simp,intro]: "bounded (cball x e)"
  2005   apply (simp add: bounded_def)
  2006   apply (rule_tac x=x in exI)
  2007   apply (rule_tac x=e in exI)
  2008   apply auto
  2009   done
  2010 
  2011 lemma bounded_ball[simp,intro]: "bounded(ball x e)"
  2012   by (metis ball_subset_cball bounded_cball bounded_subset)
  2013 
  2014 lemma finite_imp_bounded[intro]:
  2015   fixes S :: "'a::metric_space set" assumes "finite S" shows "bounded S"
  2016 proof-
  2017   { fix a and F :: "'a set" assume as:"bounded F"
  2018     then obtain x e where "\<forall>y\<in>F. dist x y \<le> e" unfolding bounded_def by auto
  2019     hence "\<forall>y\<in>(insert a F). dist x y \<le> max e (dist x a)" by auto
  2020     hence "bounded (insert a F)" unfolding bounded_def by (intro exI)
  2021   }
  2022   thus ?thesis using finite_induct[of S bounded]  using bounded_empty assms by auto
  2023 qed
  2024 
  2025 lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
  2026   apply (auto simp add: bounded_def)
  2027   apply (rename_tac x y r s)
  2028   apply (rule_tac x=x in exI)
  2029   apply (rule_tac x="max r (dist x y + s)" in exI)
  2030   apply (rule ballI, rename_tac z, safe)
  2031   apply (drule (1) bspec, simp)
  2032   apply (drule (1) bspec)
  2033   apply (rule min_max.le_supI2)
  2034   apply (erule order_trans [OF dist_triangle add_left_mono])
  2035   done
  2036 
  2037 lemma bounded_Union[intro]: "finite F \<Longrightarrow> (\<forall>S\<in>F. bounded S) \<Longrightarrow> bounded(\<Union>F)"
  2038   by (induct rule: finite_induct[of F], auto)
  2039 
  2040 lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x <= b)"
  2041   apply (simp add: bounded_iff)
  2042   apply (subgoal_tac "\<And>x (y::real). 0 < 1 + abs y \<and> (x <= y \<longrightarrow> x <= 1 + abs y)")
  2043   by metis arith
  2044 
  2045 lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
  2046   by (metis Int_lower1 Int_lower2 bounded_subset)
  2047 
  2048 lemma bounded_diff[intro]: "bounded S ==> bounded (S - T)"
  2049 apply (metis Diff_subset bounded_subset)
  2050 done
  2051 
  2052 lemma bounded_insert[intro]:"bounded(insert x S) \<longleftrightarrow> bounded S"
  2053   by (metis Diff_cancel Un_empty_right Un_insert_right bounded_Un bounded_subset finite.emptyI finite_imp_bounded infinite_remove subset_insertI)
  2054 
  2055 lemma not_bounded_UNIV[simp, intro]:
  2056   "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
  2057 proof(auto simp add: bounded_pos not_le)
  2058   obtain x :: 'a where "x \<noteq> 0"
  2059     using perfect_choose_dist [OF zero_less_one] by fast
  2060   fix b::real  assume b: "b >0"
  2061   have b1: "b +1 \<ge> 0" using b by simp
  2062   with `x \<noteq> 0` have "b < norm (scaleR (b + 1) (sgn x))"
  2063     by (simp add: norm_sgn)
  2064   then show "\<exists>x::'a. b < norm x" ..
  2065 qed
  2066 
  2067 lemma bounded_linear_image:
  2068   assumes "bounded S" "bounded_linear f"
  2069   shows "bounded(f ` S)"
  2070 proof-
  2071   from assms(1) obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
  2072   from assms(2) obtain B where B:"B>0" "\<forall>x. norm (f x) \<le> B * norm x" using bounded_linear.pos_bounded by (auto simp add: mult_ac)
  2073   { fix x assume "x\<in>S"
  2074     hence "norm x \<le> b" using b by auto
  2075     hence "norm (f x) \<le> B * b" using B(2) apply(erule_tac x=x in allE)
  2076       by (metis B(1) B(2) order_trans mult_le_cancel_left_pos)
  2077   }
  2078   thus ?thesis unfolding bounded_pos apply(rule_tac x="b*B" in exI)
  2079     using b B mult_pos_pos [of b B] by (auto simp add: mult_commute)
  2080 qed
  2081 
  2082 lemma bounded_scaling:
  2083   fixes S :: "'a::real_normed_vector set"
  2084   shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
  2085   apply (rule bounded_linear_image, assumption)
  2086   apply (rule scaleR.bounded_linear_right)
  2087   done
  2088 
  2089 lemma bounded_translation:
  2090   fixes S :: "'a::real_normed_vector set"
  2091   assumes "bounded S" shows "bounded ((\<lambda>x. a + x) ` S)"
  2092 proof-
  2093   from assms obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
  2094   { fix x assume "x\<in>S"
  2095     hence "norm (a + x) \<le> b + norm a" using norm_triangle_ineq[of a x] b by auto
  2096   }
  2097   thus ?thesis unfolding bounded_pos using norm_ge_zero[of a] b(1) using add_strict_increasing[of b 0 "norm a"]
  2098     by (auto intro!: add exI[of _ "b + norm a"])
  2099 qed
  2100 
  2101 
  2102 text{* Some theorems on sups and infs using the notion "bounded". *}
  2103 
  2104 lemma bounded_real:
  2105   fixes S :: "real set"
  2106   shows "bounded S \<longleftrightarrow>  (\<exists>a. \<forall>x\<in>S. abs x <= a)"
  2107   by (simp add: bounded_iff)
  2108 
  2109 lemma bounded_has_Sup:
  2110   fixes S :: "real set"
  2111   assumes "bounded S" "S \<noteq> {}"
  2112   shows "\<forall>x\<in>S. x <= Sup S" and "\<forall>b. (\<forall>x\<in>S. x <= b) \<longrightarrow> Sup S <= b"
  2113 proof
  2114   fix x assume "x\<in>S"
  2115   thus "x \<le> Sup S"
  2116     by (metis SupInf.Sup_upper abs_le_D1 assms(1) bounded_real)
  2117 next
  2118   show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b" using assms
  2119     by (metis SupInf.Sup_least)
  2120 qed
  2121 
  2122 lemma Sup_insert:
  2123   fixes S :: "real set"
  2124   shows "bounded S ==> Sup(insert x S) = (if S = {} then x else max x (Sup S))" 
  2125 by auto (metis Int_absorb Sup_insert_nonempty assms bounded_has_Sup(1) disjoint_iff_not_equal) 
  2126 
  2127 lemma Sup_insert_finite:
  2128   fixes S :: "real set"
  2129   shows "finite S \<Longrightarrow> Sup(insert x S) = (if S = {} then x else max x (Sup S))"
  2130   apply (rule Sup_insert)
  2131   apply (rule finite_imp_bounded)
  2132   by simp
  2133 
  2134 lemma bounded_has_Inf:
  2135   fixes S :: "real set"
  2136   assumes "bounded S"  "S \<noteq> {}"
  2137   shows "\<forall>x\<in>S. x >= Inf S" and "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S >= b"
  2138 proof
  2139   fix x assume "x\<in>S"
  2140   from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto
  2141   thus "x \<ge> Inf S" using `x\<in>S`
  2142     by (metis Inf_lower_EX abs_le_D2 minus_le_iff)
  2143 next
  2144   show "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S \<ge> b" using assms
  2145     by (metis SupInf.Inf_greatest)
  2146 qed
  2147 
  2148 lemma Inf_insert:
  2149   fixes S :: "real set"
  2150   shows "bounded S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))" 
  2151 by auto (metis Int_absorb Inf_insert_nonempty bounded_has_Inf(1) disjoint_iff_not_equal) 
  2152 lemma Inf_insert_finite:
  2153   fixes S :: "real set"
  2154   shows "finite S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"
  2155   by (rule Inf_insert, rule finite_imp_bounded, simp)
  2156 
  2157 
  2158 (* TODO: Move this to RComplete.thy -- would need to include Glb into RComplete *)
  2159 lemma real_isGlb_unique: "[| isGlb R S x; isGlb R S y |] ==> x = (y::real)"
  2160   apply (frule isGlb_isLb)
  2161   apply (frule_tac x = y in isGlb_isLb)
  2162   apply (blast intro!: order_antisym dest!: isGlb_le_isLb)
  2163   done
  2164 
  2165 subsection {* Equivalent versions of compactness *}
  2166 
  2167 subsubsection{* Sequential compactness *}
  2168 
  2169 definition
  2170   compact :: "'a::metric_space set \<Rightarrow> bool" where (* TODO: generalize *)
  2171   "compact S \<longleftrightarrow>
  2172    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>
  2173        (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"
  2174 
  2175 lemma compactI:
  2176   assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially"
  2177   shows "compact S"
  2178   unfolding compact_def using assms by fast
  2179 
  2180 lemma compactE:
  2181   assumes "compact S" "\<forall>n. f n \<in> S"
  2182   obtains l r where "l \<in> S" "subseq r" "((f \<circ> r) ---> l) sequentially"
  2183   using assms unfolding compact_def by fast
  2184 
  2185 text {*
  2186   A metric space (or topological vector space) is said to have the
  2187   Heine-Borel property if every closed and bounded subset is compact.
  2188 *}
  2189 
  2190 class heine_borel =
  2191   assumes bounded_imp_convergent_subsequence:
  2192     "bounded s \<Longrightarrow> \<forall>n. f n \<in> s
  2193       \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2194 
  2195 lemma bounded_closed_imp_compact:
  2196   fixes s::"'a::heine_borel set"
  2197   assumes "bounded s" and "closed s" shows "compact s"
  2198 proof (unfold compact_def, clarify)
  2199   fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
  2200   obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
  2201     using bounded_imp_convergent_subsequence [OF `bounded s` `\<forall>n. f n \<in> s`] by auto
  2202   from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp
  2203   have "l \<in> s" using `closed s` fr l
  2204     unfolding closed_sequential_limits by blast
  2205   show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2206     using `l \<in> s` r l by blast
  2207 qed
  2208 
  2209 lemma subseq_bigger: assumes "subseq r" shows "n \<le> r n"
  2210 proof(induct n)
  2211   show "0 \<le> r 0" by auto
  2212 next
  2213   fix n assume "n \<le> r n"
  2214   moreover have "r n < r (Suc n)"
  2215     using assms [unfolded subseq_def] by auto
  2216   ultimately show "Suc n \<le> r (Suc n)" by auto
  2217 qed
  2218 
  2219 lemma eventually_subseq:
  2220   assumes r: "subseq r"
  2221   shows "eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
  2222 unfolding eventually_sequentially
  2223 by (metis subseq_bigger [OF r] le_trans)
  2224 
  2225 lemma lim_subseq:
  2226   "subseq r \<Longrightarrow> (s ---> l) sequentially \<Longrightarrow> ((s o r) ---> l) sequentially"
  2227 unfolding tendsto_def eventually_sequentially o_def
  2228 by (metis subseq_bigger le_trans)
  2229 
  2230 lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
  2231   unfolding Ex1_def
  2232   apply (rule_tac x="nat_rec e f" in exI)
  2233   apply (rule conjI)+
  2234 apply (rule def_nat_rec_0, simp)
  2235 apply (rule allI, rule def_nat_rec_Suc, simp)
  2236 apply (rule allI, rule impI, rule ext)
  2237 apply (erule conjE)
  2238 apply (induct_tac x)
  2239 apply simp
  2240 apply (erule_tac x="n" in allE)
  2241 apply (simp)
  2242 done
  2243 
  2244 lemma convergent_bounded_increasing: fixes s ::"nat\<Rightarrow>real"
  2245   assumes "incseq s" and "\<forall>n. abs(s n) \<le> b"
  2246   shows "\<exists> l. \<forall>e::real>0. \<exists> N. \<forall>n \<ge> N.  abs(s n - l) < e"
  2247 proof-
  2248   have "isUb UNIV (range s) b" using assms(2) and abs_le_D1 unfolding isUb_def and setle_def by auto
  2249   then obtain t where t:"isLub UNIV (range s) t" using reals_complete[of "range s" ] by auto
  2250   { fix e::real assume "e>0" and as:"\<forall>N. \<exists>n\<ge>N. \<not> \<bar>s n - t\<bar> < e"
  2251     { fix n::nat
  2252       obtain N where "N\<ge>n" and n:"\<bar>s N - t\<bar> \<ge> e" using as[THEN spec[where x=n]] by auto
  2253       have "t \<ge> s N" using isLub_isUb[OF t, unfolded isUb_def setle_def] by auto
  2254       with n have "s N \<le> t - e" using `e>0` by auto
  2255       hence "s n \<le> t - e" using assms(1)[unfolded incseq_def, THEN spec[where x=n], THEN spec[where x=N]] using `n\<le>N` by auto  }
  2256     hence "isUb UNIV (range s) (t - e)" unfolding isUb_def and setle_def by auto
  2257     hence False using isLub_le_isUb[OF t, of "t - e"] and `e>0` by auto  }
  2258   thus ?thesis by blast
  2259 qed
  2260 
  2261 lemma convergent_bounded_monotone: fixes s::"nat \<Rightarrow> real"
  2262   assumes "\<forall>n. abs(s n) \<le> b" and "monoseq s"
  2263   shows "\<exists>l. \<forall>e::real>0. \<exists>N. \<forall>n\<ge>N. abs(s n - l) < e"
  2264   using convergent_bounded_increasing[of s b] assms using convergent_bounded_increasing[of "\<lambda>n. - s n" b]
  2265   unfolding monoseq_def incseq_def
  2266   apply auto unfolding minus_add_distrib[THEN sym, unfolded diff_minus[THEN sym]]
  2267   unfolding abs_minus_cancel by(rule_tac x="-l" in exI)auto
  2268 
  2269 (* TODO: merge this lemma with the ones above *)
  2270 lemma bounded_increasing_convergent: fixes s::"nat \<Rightarrow> real"
  2271   assumes "bounded {s n| n::nat. True}"  "\<forall>n. (s n) \<le>(s(Suc n))"
  2272   shows "\<exists>l. (s ---> l) sequentially"
  2273 proof-
  2274   obtain a where a:"\<forall>n. \<bar> (s n)\<bar> \<le>  a" using assms(1)[unfolded bounded_iff] by auto
  2275   { fix m::nat
  2276     have "\<And> n. n\<ge>m \<longrightarrow>  (s m) \<le> (s n)"
  2277       apply(induct_tac n) apply simp using assms(2) apply(erule_tac x="na" in allE)
  2278       apply(case_tac "m \<le> na") unfolding not_less_eq_eq by(auto simp add: not_less_eq_eq)  }
  2279   hence "\<forall>m n. m \<le> n \<longrightarrow> (s m) \<le> (s n)" by auto
  2280   then obtain l where "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. \<bar> (s n) - l\<bar> < e" using convergent_bounded_monotone[OF a]
  2281     unfolding monoseq_def by auto
  2282   thus ?thesis unfolding Lim_sequentially apply(rule_tac x="l" in exI)
  2283     unfolding dist_norm  by auto
  2284 qed
  2285 
  2286 lemma compact_real_lemma:
  2287   assumes "\<forall>n::nat. abs(s n) \<le> b"
  2288   shows "\<exists>(l::real) r. subseq r \<and> ((s \<circ> r) ---> l) sequentially"
  2289 proof-
  2290   obtain r where r:"subseq r" "monoseq (\<lambda>n. s (r n))"
  2291     using seq_monosub[of s] by auto
  2292   thus ?thesis using convergent_bounded_monotone[of "\<lambda>n. s (r n)" b] and assms
  2293     unfolding tendsto_iff dist_norm eventually_sequentially by auto
  2294 qed
  2295 
  2296 instance real :: heine_borel
  2297 proof
  2298   fix s :: "real set" and f :: "nat \<Rightarrow> real"
  2299   assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
  2300   then obtain b where b: "\<forall>n. abs (f n) \<le> b"
  2301     unfolding bounded_iff by auto
  2302   obtain l :: real and r :: "nat \<Rightarrow> nat" where
  2303     r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
  2304     using compact_real_lemma [OF b] by auto
  2305   thus "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2306     by auto
  2307 qed
  2308 
  2309 lemma bounded_component: "bounded s \<Longrightarrow>
  2310   bounded ((\<lambda>x. x $$ i) ` (s::'a::euclidean_space set))"
  2311 unfolding bounded_def
  2312 apply clarify
  2313 apply (rule_tac x="x $$ i" in exI)
  2314 apply (rule_tac x="e" in exI)
  2315 apply clarify
  2316 apply (rule order_trans[OF dist_nth_le],simp)
  2317 done
  2318 
  2319 lemma compact_lemma:
  2320   fixes f :: "nat \<Rightarrow> 'a::euclidean_space"
  2321   assumes "bounded s" and "\<forall>n. f n \<in> s"
  2322   shows "\<forall>d. \<exists>l::'a. \<exists> r. subseq r \<and>
  2323         (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $$ i) (l $$ i) < e) sequentially)"
  2324 proof
  2325   fix d'::"nat set" def d \<equiv> "d' \<inter> {..<DIM('a)}"
  2326   have "finite d" "d\<subseteq>{..<DIM('a)}" unfolding d_def by auto
  2327   hence "\<exists>l::'a. \<exists>r. subseq r \<and>
  2328       (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $$ i) (l $$ i) < e) sequentially)"
  2329   proof(induct d) case empty thus ?case unfolding subseq_def by auto
  2330   next case (insert k d) have k[intro]:"k<DIM('a)" using insert by auto
  2331     have s': "bounded ((\<lambda>x. x $$ k) ` s)" using `bounded s` by (rule bounded_component)
  2332     obtain l1::"'a" and r1 where r1:"subseq r1" and
  2333       lr1:"\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $$ i) (l1 $$ i) < e) sequentially"
  2334       using insert(3) using insert(4) by auto
  2335     have f': "\<forall>n. f (r1 n) $$ k \<in> (\<lambda>x. x $$ k) ` s" using `\<forall>n. f n \<in> s` by simp
  2336     obtain l2 r2 where r2:"subseq r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) $$ k) ---> l2) sequentially"
  2337       using bounded_imp_convergent_subsequence[OF s' f'] unfolding o_def by auto
  2338     def r \<equiv> "r1 \<circ> r2" have r:"subseq r"
  2339       using r1 and r2 unfolding r_def o_def subseq_def by auto
  2340     moreover
  2341     def l \<equiv> "(\<chi>\<chi> i. if i = k then l2 else l1$$i)::'a"
  2342     { fix e::real assume "e>0"
  2343       from lr1 `e>0` have N1:"eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $$ i) (l1 $$ i) < e) sequentially" by blast
  2344       from lr2 `e>0` have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) $$ k) l2 < e) sequentially" by (rule tendstoD)
  2345       from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) $$ i) (l1 $$ i) < e) sequentially"
  2346         by (rule eventually_subseq)
  2347       have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) $$ i) (l $$ i) < e) sequentially"
  2348         using N1' N2 apply(rule eventually_elim2) unfolding l_def r_def o_def
  2349         using insert.prems by auto
  2350     }
  2351     ultimately show ?case by auto
  2352   qed
  2353   thus "\<exists>l::'a. \<exists>r. subseq r \<and>
  2354       (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d'. dist (f (r n) $$ i) (l $$ i) < e) sequentially)"
  2355     apply safe apply(rule_tac x=l in exI,rule_tac x=r in exI) apply safe
  2356     apply(erule_tac x=e in allE) unfolding d_def eventually_sequentially apply safe 
  2357     apply(rule_tac x=N in exI) apply safe apply(erule_tac x=n in allE,safe)
  2358     apply(erule_tac x=i in ballE) 
  2359   proof- fix i and r::"nat=>nat" and n::nat and e::real and l::'a
  2360     assume "i\<in>d'" "i \<notin> d' \<inter> {..<DIM('a)}" and e:"e>0"
  2361     hence *:"i\<ge>DIM('a)" by auto
  2362     thus "dist (f (r n) $$ i) (l $$ i) < e" using e by auto
  2363   qed
  2364 qed
  2365 
  2366 instance euclidean_space \<subseteq> heine_borel
  2367 proof
  2368   fix s :: "'a set" and f :: "nat \<Rightarrow> 'a"
  2369   assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
  2370   then obtain l::'a and r where r: "subseq r"
  2371     and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>UNIV. dist (f (r n) $$ i) (l $$ i) < e) sequentially"
  2372     using compact_lemma [OF s f] by blast
  2373   let ?d = "{..<DIM('a)}"
  2374   { fix e::real assume "e>0"
  2375     hence "0 < e / (real_of_nat (card ?d))"
  2376       using DIM_positive using divide_pos_pos[of e, of "real_of_nat (card ?d)"] by auto
  2377     with l have "eventually (\<lambda>n. \<forall>i. dist (f (r n) $$ i) (l $$ i) < e / (real_of_nat (card ?d))) sequentially"
  2378       by simp
  2379     moreover
  2380     { fix n assume n: "\<forall>i. dist (f (r n) $$ i) (l $$ i) < e / (real_of_nat (card ?d))"
  2381       have "dist (f (r n)) l \<le> (\<Sum>i\<in>?d. dist (f (r n) $$ i) (l $$ i))"
  2382         apply(subst euclidean_dist_l2) using zero_le_dist by (rule setL2_le_setsum)
  2383       also have "\<dots> < (\<Sum>i\<in>?d. e / (real_of_nat (card ?d)))"
  2384         apply(rule setsum_strict_mono) using n by auto
  2385       finally have "dist (f (r n)) l < e" unfolding setsum_constant
  2386         using DIM_positive[where 'a='a] by auto
  2387     }
  2388     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
  2389       by (rule eventually_elim1)
  2390   }
  2391   hence *:"((f \<circ> r) ---> l) sequentially" unfolding o_def tendsto_iff by simp
  2392   with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" by auto
  2393 qed
  2394 
  2395 lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
  2396 unfolding bounded_def
  2397 apply clarify
  2398 apply (rule_tac x="a" in exI)
  2399 apply (rule_tac x="e" in exI)
  2400 apply clarsimp
  2401 apply (drule (1) bspec)
  2402 apply (simp add: dist_Pair_Pair)
  2403 apply (erule order_trans [OF real_sqrt_sum_squares_ge1])
  2404 done
  2405 
  2406 lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
  2407 unfolding bounded_def
  2408 apply clarify
  2409 apply (rule_tac x="b" in exI)
  2410 apply (rule_tac x="e" in exI)
  2411 apply clarsimp
  2412 apply (drule (1) bspec)
  2413 apply (simp add: dist_Pair_Pair)
  2414 apply (erule order_trans [OF real_sqrt_sum_squares_ge2])
  2415 done
  2416 
  2417 instance prod :: (heine_borel, heine_borel) heine_borel
  2418 proof
  2419   fix s :: "('a * 'b) set" and f :: "nat \<Rightarrow> 'a * 'b"
  2420   assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
  2421   from s have s1: "bounded (fst ` s)" by (rule bounded_fst)
  2422   from f have f1: "\<forall>n. fst (f n) \<in> fst ` s" by simp
  2423   obtain l1 r1 where r1: "subseq r1"
  2424     and l1: "((\<lambda>n. fst (f (r1 n))) ---> l1) sequentially"
  2425     using bounded_imp_convergent_subsequence [OF s1 f1]
  2426     unfolding o_def by fast
  2427   from s have s2: "bounded (snd ` s)" by (rule bounded_snd)
  2428   from f have f2: "\<forall>n. snd (f (r1 n)) \<in> snd ` s" by simp
  2429   obtain l2 r2 where r2: "subseq r2"
  2430     and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) ---> l2) sequentially"
  2431     using bounded_imp_convergent_subsequence [OF s2 f2]
  2432     unfolding o_def by fast
  2433   have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) ---> l1) sequentially"
  2434     using lim_subseq [OF r2 l1] unfolding o_def .
  2435   have l: "((f \<circ> (r1 \<circ> r2)) ---> (l1, l2)) sequentially"
  2436     using tendsto_Pair [OF l1' l2] unfolding o_def by simp
  2437   have r: "subseq (r1 \<circ> r2)"
  2438     using r1 r2 unfolding subseq_def by simp
  2439   show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2440     using l r by fast
  2441 qed
  2442 
  2443 subsubsection{* Completeness *}
  2444 
  2445 lemma cauchy_def:
  2446   "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N --> dist(s m)(s n) < e)"
  2447 unfolding Cauchy_def by blast
  2448 
  2449 definition
  2450   complete :: "'a::metric_space set \<Rightarrow> bool" where
  2451   "complete s \<longleftrightarrow> (\<forall>f. (\<forall>n. f n \<in> s) \<and> Cauchy f
  2452                       --> (\<exists>l \<in> s. (f ---> l) sequentially))"
  2453 
  2454 lemma cauchy: "Cauchy s \<longleftrightarrow> (\<forall>e>0.\<exists> N::nat. \<forall>n\<ge>N. dist(s n)(s N) < e)" (is "?lhs = ?rhs")
  2455 proof-
  2456   { assume ?rhs
  2457     { fix e::real
  2458       assume "e>0"
  2459       with `?rhs` obtain N where N:"\<forall>n\<ge>N. dist (s n) (s N) < e/2"
  2460         by (erule_tac x="e/2" in allE) auto
  2461       { fix n m
  2462         assume nm:"N \<le> m \<and> N \<le> n"
  2463         hence "dist (s m) (s n) < e" using N
  2464           using dist_triangle_half_l[of "s m" "s N" "e" "s n"]
  2465           by blast
  2466       }
  2467       hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"
  2468         by blast
  2469     }
  2470     hence ?lhs
  2471       unfolding cauchy_def
  2472       by blast
  2473   }
  2474   thus ?thesis
  2475     unfolding cauchy_def
  2476     using dist_triangle_half_l
  2477     by blast
  2478 qed
  2479 
  2480 lemma convergent_imp_cauchy:
  2481  "(s ---> l) sequentially ==> Cauchy s"
  2482 proof(simp only: cauchy_def, rule, rule)
  2483   fix e::real assume "e>0" "(s ---> l) sequentially"
  2484   then obtain N::nat where N:"\<forall>n\<ge>N. dist (s n) l < e/2" unfolding Lim_sequentially by(erule_tac x="e/2" in allE) auto
  2485   thus "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"  using dist_triangle_half_l[of _ l e _] by (rule_tac x=N in exI) auto
  2486 qed
  2487 
  2488 lemma cauchy_imp_bounded: assumes "Cauchy s" shows "bounded (range s)"
  2489 proof-
  2490   from assms obtain N::nat where "\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < 1" unfolding cauchy_def apply(erule_tac x= 1 in allE) by auto
  2491   hence N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
  2492   moreover
  2493   have "bounded (s ` {0..N})" using finite_imp_bounded[of "s ` {1..N}"] by auto
  2494   then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
  2495     unfolding bounded_any_center [where a="s N"] by auto
  2496   ultimately show "?thesis"
  2497     unfolding bounded_any_center [where a="s N"]
  2498     apply(rule_tac x="max a 1" in exI) apply auto
  2499     apply(erule_tac x=y in allE) apply(erule_tac x=y in ballE) by auto
  2500 qed
  2501 
  2502 lemma compact_imp_complete: assumes "compact s" shows "complete s"
  2503 proof-
  2504   { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
  2505     from as(1) obtain l r where lr: "l\<in>s" "subseq r" "((f \<circ> r) ---> l) sequentially" using assms unfolding compact_def by blast
  2506 
  2507     note lr' = subseq_bigger [OF lr(2)]
  2508 
  2509     { fix e::real assume "e>0"
  2510       from as(2) obtain N where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (f m) (f n) < e/2" unfolding cauchy_def using `e>0` apply (erule_tac x="e/2" in allE) by auto
  2511       from lr(3)[unfolded Lim_sequentially, THEN spec[where x="e/2"]] obtain M where M:"\<forall>n\<ge>M. dist ((f \<circ> r) n) l < e/2" using `e>0` by auto
  2512       { fix n::nat assume n:"n \<ge> max N M"
  2513         have "dist ((f \<circ> r) n) l < e/2" using n M by auto
  2514         moreover have "r n \<ge> N" using lr'[of n] n by auto
  2515         hence "dist (f n) ((f \<circ> r) n) < e / 2" using N using n by auto
  2516         ultimately have "dist (f n) l < e" using dist_triangle_half_r[of "f (r n)" "f n" e l] by (auto simp add: dist_commute)  }
  2517       hence "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast  }
  2518     hence "\<exists>l\<in>s. (f ---> l) sequentially" using `l\<in>s` unfolding Lim_sequentially by auto  }
  2519   thus ?thesis unfolding complete_def by auto
  2520 qed
  2521 
  2522 instance heine_borel < complete_space
  2523 proof
  2524   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  2525   hence "bounded (range f)"
  2526     by (rule cauchy_imp_bounded)
  2527   hence "compact (closure (range f))"
  2528     using bounded_closed_imp_compact [of "closure (range f)"] by auto
  2529   hence "complete (closure (range f))"
  2530     by (rule compact_imp_complete)
  2531   moreover have "\<forall>n. f n \<in> closure (range f)"
  2532     using closure_subset [of "range f"] by auto
  2533   ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"
  2534     using `Cauchy f` unfolding complete_def by auto
  2535   then show "convergent f"
  2536     unfolding convergent_def by auto
  2537 qed
  2538 
  2539 lemma complete_univ: "complete (UNIV :: 'a::complete_space set)"
  2540 proof(simp add: complete_def, rule, rule)
  2541   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  2542   hence "convergent f" by (rule Cauchy_convergent)
  2543   thus "\<exists>l. f ----> l" unfolding convergent_def .  
  2544 qed
  2545 
  2546 lemma complete_imp_closed: assumes "complete s" shows "closed s"
  2547 proof -
  2548   { fix x assume "x islimpt s"
  2549     then obtain f where f: "\<forall>n. f n \<in> s - {x}" "(f ---> x) sequentially"
  2550       unfolding islimpt_sequential by auto
  2551     then obtain l where l: "l\<in>s" "(f ---> l) sequentially"
  2552       using `complete s`[unfolded complete_def] using convergent_imp_cauchy[of f x] by auto
  2553     hence "x \<in> s"  using tendsto_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto
  2554   }
  2555   thus "closed s" unfolding closed_limpt by auto
  2556 qed
  2557 
  2558 lemma complete_eq_closed:
  2559   fixes s :: "'a::complete_space set"
  2560   shows "complete s \<longleftrightarrow> closed s" (is "?lhs = ?rhs")
  2561 proof
  2562   assume ?lhs thus ?rhs by (rule complete_imp_closed)
  2563 next
  2564   assume ?rhs
  2565   { fix f assume as:"\<forall>n::nat. f n \<in> s" "Cauchy f"
  2566     then obtain l where "(f ---> l) sequentially" using complete_univ[unfolded complete_def, THEN spec[where x=f]] by auto
  2567     hence "\<exists>l\<in>s. (f ---> l) sequentially" using `?rhs`[unfolded closed_sequential_limits, THEN spec[where x=f], THEN spec[where x=l]] using as(1) by auto  }
  2568   thus ?lhs unfolding complete_def by auto
  2569 qed
  2570 
  2571 lemma convergent_eq_cauchy:
  2572   fixes s :: "nat \<Rightarrow> 'a::complete_space"
  2573   shows "(\<exists>l. (s ---> l) sequentially) \<longleftrightarrow> Cauchy s" (is "?lhs = ?rhs")
  2574 proof
  2575   assume ?lhs then obtain l where "(s ---> l) sequentially" by auto
  2576   thus ?rhs using convergent_imp_cauchy by auto
  2577 next
  2578   assume ?rhs thus ?lhs using complete_univ[unfolded complete_def, THEN spec[where x=s]] by auto
  2579 qed
  2580 
  2581 lemma convergent_imp_bounded:
  2582   fixes s :: "nat \<Rightarrow> 'a::metric_space"
  2583   shows "(s ---> l) sequentially ==> bounded (s ` (UNIV::(nat set)))"
  2584   using convergent_imp_cauchy[of s]
  2585   using cauchy_imp_bounded[of s]
  2586   unfolding image_def
  2587   by auto
  2588 
  2589 subsubsection{* Total boundedness *}
  2590 
  2591 fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where
  2592   "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"
  2593 declare helper_1.simps[simp del]
  2594 
  2595 lemma compact_imp_totally_bounded:
  2596   assumes "compact s"
  2597   shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k))"
  2598 proof(rule, rule, rule ccontr)
  2599   fix e::real assume "e>0" and assm:"\<not> (\<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k)"
  2600   def x \<equiv> "helper_1 s e"
  2601   { fix n
  2602     have "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)"
  2603     proof(induct_tac rule:nat_less_induct)
  2604       fix n  def Q \<equiv> "(\<lambda>y. y \<in> s \<and> (\<forall>m<n. \<not> dist (x m) y < e))"
  2605       assume as:"\<forall>m<n. x m \<in> s \<and> (\<forall>ma<m. \<not> dist (x ma) (x m) < e)"
  2606       have "\<not> s \<subseteq> (\<Union>x\<in>x ` {0..<n}. ball x e)" using assm apply simp apply(erule_tac x="x ` {0 ..< n}" in allE) using as by auto
  2607       then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)" unfolding subset_eq by auto
  2608       have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]
  2609         apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto
  2610       thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto
  2611     qed }
  2612   hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+
  2613   then obtain l r where "l\<in>s" and r:"subseq r" and "((x \<circ> r) ---> l) sequentially" using assms(1)[unfolded compact_def, THEN spec[where x=x]] by auto
  2614   from this(3) have "Cauchy (x \<circ> r)" using convergent_imp_cauchy by auto
  2615   then obtain N::nat where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist ((x \<circ> r) m) ((x \<circ> r) n) < e" unfolding cauchy_def using `e>0` by auto
  2616   show False
  2617     using N[THEN spec[where x=N], THEN spec[where x="N+1"]]
  2618     using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]
  2619     using x[THEN spec[where x="r (N+1)"], THEN spec[where x="r (N)"]] by auto
  2620 qed
  2621 
  2622 subsubsection{* Heine-Borel theorem *}
  2623 
  2624 text {* Following Burkill \& Burkill vol. 2. *}
  2625 
  2626 lemma heine_borel_lemma: fixes s::"'a::metric_space set"
  2627   assumes "compact s"  "s \<subseteq> (\<Union> t)"  "\<forall>b \<in> t. open b"
  2628   shows "\<exists>e>0. \<forall>x \<in> s. \<exists>b \<in> t. ball x e \<subseteq> b"
  2629 proof(rule ccontr)
  2630   assume "\<not> (\<exists>e>0. \<forall>x\<in>s. \<exists>b\<in>t. ball x e \<subseteq> b)"
  2631   hence cont:"\<forall>e>0. \<exists>x\<in>s. \<forall>xa\<in>t. \<not> (ball x e \<subseteq> xa)" by auto
  2632   { fix n::nat
  2633     have "1 / real (n + 1) > 0" by auto
  2634     hence "\<exists>x. x\<in>s \<and> (\<forall>xa\<in>t. \<not> (ball x (inverse (real (n+1))) \<subseteq> xa))" using cont unfolding Bex_def by auto }
  2635   hence "\<forall>n::nat. \<exists>x. x \<in> s \<and> (\<forall>xa\<in>t. \<not> ball x (inverse (real (n + 1))) \<subseteq> xa)" by auto
  2636   then obtain f where f:"\<forall>n::nat. f n \<in> s \<and> (\<forall>xa\<in>t. \<not> ball (f n) (inverse (real (n + 1))) \<subseteq> xa)"
  2637     using choice[of "\<lambda>n::nat. \<lambda>x. x\<in>s \<and> (\<forall>xa\<in>t. \<not> ball x (inverse (real (n + 1))) \<subseteq> xa)"] by auto
  2638 
  2639   then obtain l r where l:"l\<in>s" and r:"subseq r" and lr:"((f \<circ> r) ---> l) sequentially"
  2640     using assms(1)[unfolded compact_def, THEN spec[where x=f]] by auto
  2641 
  2642   obtain b where "l\<in>b" "b\<in>t" using assms(2) and l by auto
  2643   then obtain e where "e>0" and e:"\<forall>z. dist z l < e \<longrightarrow> z\<in>b"
  2644     using assms(3)[THEN bspec[where x=b]] unfolding open_dist by auto
  2645 
  2646   then obtain N1 where N1:"\<forall>n\<ge>N1. dist ((f \<circ> r) n) l < e / 2"
  2647     using lr[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
  2648 
  2649   obtain N2::nat where N2:"N2>0" "inverse (real N2) < e /2" using real_arch_inv[of "e/2"] and `e>0` by auto
  2650   have N2':"inverse (real (r (N1 + N2) +1 )) < e/2"
  2651     apply(rule order_less_trans) apply(rule less_imp_inverse_less) using N2
  2652     using subseq_bigger[OF r, of "N1 + N2"] by auto
  2653 
  2654   def x \<equiv> "(f (r (N1 + N2)))"
  2655   have x:"\<not> ball x (inverse (real (r (N1 + N2) + 1))) \<subseteq> b" unfolding x_def
  2656     using f[THEN spec[where x="r (N1 + N2)"]] using `b\<in>t` by auto
  2657   have "\<exists>y\<in>ball x (inverse (real (r (N1 + N2) + 1))). y\<notin>b" apply(rule ccontr) using x by auto
  2658   then obtain y where y:"y \<in> ball x (inverse (real (r (N1 + N2) + 1)))" "y \<notin> b" by auto
  2659 
  2660   have "dist x l < e/2" using N1 unfolding x_def o_def by auto
  2661   hence "dist y l < e" using y N2' using dist_triangle[of y l x]by (auto simp add:dist_commute)
  2662 
  2663   thus False using e and `y\<notin>b` by auto
  2664 qed
  2665 
  2666 lemma compact_imp_heine_borel: "compact s ==> (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
  2667                \<longrightarrow> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))"
  2668 proof clarify
  2669   fix f assume "compact s" " \<forall>t\<in>f. open t" "s \<subseteq> \<Union>f"
  2670   then obtain e::real where "e>0" and "\<forall>x\<in>s. \<exists>b\<in>f. ball x e \<subseteq> b" using heine_borel_lemma[of s f] by auto
  2671   hence "\<forall>x\<in>s. \<exists>b. b\<in>f \<and> ball x e \<subseteq> b" by auto
  2672   hence "\<exists>bb. \<forall>x\<in>s. bb x \<in>f \<and> ball x e \<subseteq> bb x" using bchoice[of s "\<lambda>x b. b\<in>f \<and> ball x e \<subseteq> b"] by auto
  2673   then obtain  bb where bb:"\<forall>x\<in>s. (bb x) \<in> f \<and> ball x e \<subseteq> (bb x)" by blast
  2674 
  2675   from `compact s` have  "\<exists> k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" using compact_imp_totally_bounded[of s] `e>0` by auto
  2676   then obtain k where k:"finite k" "k \<subseteq> s" "s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" by auto
  2677 
  2678   have "finite (bb ` k)" using k(1) by auto
  2679   moreover
  2680   { fix x assume "x\<in>s"
  2681     hence "x\<in>\<Union>(\<lambda>x. ball x e) ` k" using k(3)  unfolding subset_eq by auto
  2682     hence "\<exists>X\<in>bb ` k. x \<in> X" using bb k(2) by blast
  2683     hence "x \<in> \<Union>(bb ` k)" using  Union_iff[of x "bb ` k"] by auto
  2684   }
  2685   ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f'" using bb k(2) by (rule_tac x="bb ` k" in exI) auto
  2686 qed
  2687 
  2688 subsubsection {* Bolzano-Weierstrass property *}
  2689 
  2690 lemma heine_borel_imp_bolzano_weierstrass:
  2691   assumes "\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f) --> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f'))"
  2692           "infinite t"  "t \<subseteq> s"
  2693   shows "\<exists>x \<in> s. x islimpt t"
  2694 proof(rule ccontr)
  2695   assume "\<not> (\<exists>x \<in> s. x islimpt t)"
  2696   then obtain f where f:"\<forall>x\<in>s. x \<in> f x \<and> open (f x) \<and> (\<forall>y\<in>t. y \<in> f x \<longrightarrow> y = x)" unfolding islimpt_def
  2697     using bchoice[of s "\<lambda> x T. x \<in> T \<and> open T \<and> (\<forall>y\<in>t. y \<in> T \<longrightarrow> y = x)"] by auto
  2698   obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
  2699     using assms(1)[THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]] using f by auto
  2700   from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto
  2701   { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"
  2702     hence "x \<in> f x"  "y \<in> f x \<longrightarrow> y = x" using f[THEN bspec[where x=x]] and `t\<subseteq>s` by auto
  2703     hence "x = y" using `f x = f y` and f[THEN bspec[where x=y]] and `y\<in>t` and `t\<subseteq>s` by auto  }
  2704   hence "inj_on f t" unfolding inj_on_def by simp
  2705   hence "infinite (f ` t)" using assms(2) using finite_imageD by auto
  2706   moreover
  2707   { fix x assume "x\<in>t" "f x \<notin> g"
  2708     from g(3) assms(3) `x\<in>t` obtain h where "h\<in>g" and "x\<in>h" by auto
  2709     then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto
  2710     hence "y = x" using f[THEN bspec[where x=y]] and `x\<in>t` and `x\<in>h`[unfolded `h = f y`] by auto
  2711     hence False using `f x \<notin> g` `h\<in>g` unfolding `h = f y` by auto  }
  2712   hence "f ` t \<subseteq> g" by auto
  2713   ultimately show False using g(2) using finite_subset by auto
  2714 qed
  2715 
  2716 subsubsection {* Complete the chain of compactness variants *}
  2717 
  2718 lemma islimpt_range_imp_convergent_subsequence:
  2719   fixes f :: "nat \<Rightarrow> 'a::metric_space"
  2720   assumes "l islimpt (range f)"
  2721   shows "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2722 proof (intro exI conjI)
  2723   have *: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. 0 < dist (f n) l \<and> dist (f n) l < e"
  2724     using assms unfolding islimpt_def
  2725     by (drule_tac x="ball l e" in spec)
  2726        (auto simp add: zero_less_dist_iff dist_commute)
  2727 
  2728   def t \<equiv> "\<lambda>e. LEAST n. 0 < dist (f n) l \<and> dist (f n) l < e"
  2729   have f_t_neq: "\<And>e. 0 < e \<Longrightarrow> 0 < dist (f (t e)) l"
  2730     unfolding t_def by (rule LeastI2_ex [OF * conjunct1])
  2731   have f_t_closer: "\<And>e. 0 < e \<Longrightarrow> dist (f (t e)) l < e"
  2732     unfolding t_def by (rule LeastI2_ex [OF * conjunct2])
  2733   have t_le: "\<And>n e. 0 < dist (f n) l \<Longrightarrow> dist (f n) l < e \<Longrightarrow> t e \<le> n"
  2734     unfolding t_def by (simp add: Least_le)
  2735   have less_tD: "\<And>n e. n < t e \<Longrightarrow> 0 < dist (f n) l \<Longrightarrow> e \<le> dist (f n) l"
  2736     unfolding t_def by (drule not_less_Least) simp
  2737   have t_antimono: "\<And>e e'. 0 < e \<Longrightarrow> e \<le> e' \<Longrightarrow> t e' \<le> t e"
  2738     apply (rule t_le)
  2739     apply (erule f_t_neq)
  2740     apply (erule (1) less_le_trans [OF f_t_closer])
  2741     done
  2742   have t_dist_f_neq: "\<And>n. 0 < dist (f n) l \<Longrightarrow> t (dist (f n) l) \<noteq> n"
  2743     by (drule f_t_closer) auto
  2744   have t_less: "\<And>e. 0 < e \<Longrightarrow> t e < t (dist (f (t e)) l)"
  2745     apply (subst less_le)
  2746     apply (rule conjI)
  2747     apply (rule t_antimono)
  2748     apply (erule f_t_neq)
  2749     apply (erule f_t_closer [THEN less_imp_le])
  2750     apply (rule t_dist_f_neq [symmetric])
  2751     apply (erule f_t_neq)
  2752     done
  2753   have dist_f_t_less':
  2754     "\<And>e e'. 0 < e \<Longrightarrow> 0 < e' \<Longrightarrow> t e \<le> t e' \<Longrightarrow> dist (f (t e')) l < e"
  2755     apply (simp add: le_less)
  2756     apply (erule disjE)
  2757     apply (rule less_trans)
  2758     apply (erule f_t_closer)
  2759     apply (rule le_less_trans)
  2760     apply (erule less_tD)
  2761     apply (erule f_t_neq)
  2762     apply (erule f_t_closer)
  2763     apply (erule subst)
  2764     apply (erule f_t_closer)
  2765     done
  2766 
  2767   def r \<equiv> "nat_rec (t 1) (\<lambda>_ x. t (dist (f x) l))"
  2768   have r_simps: "r 0 = t 1" "\<And>n. r (Suc n) = t (dist (f (r n)) l)"
  2769     unfolding r_def by simp_all
  2770   have f_r_neq: "\<And>n. 0 < dist (f (r n)) l"
  2771     by (induct_tac n) (simp_all add: r_simps f_t_neq)
  2772 
  2773   show "subseq r"
  2774     unfolding subseq_Suc_iff
  2775     apply (rule allI)
  2776     apply (case_tac n)
  2777     apply (simp_all add: r_simps)
  2778     apply (rule t_less, rule zero_less_one)
  2779     apply (rule t_less, rule f_r_neq)
  2780     done
  2781   show "((f \<circ> r) ---> l) sequentially"
  2782     unfolding Lim_sequentially o_def
  2783     apply (clarify, rule_tac x="t e" in exI, clarify)
  2784     apply (drule le_trans, rule seq_suble [OF `subseq r`])
  2785     apply (case_tac n, simp_all add: r_simps dist_f_t_less' f_r_neq)
  2786     done
  2787 qed
  2788 
  2789 lemma finite_range_imp_infinite_repeats:
  2790   fixes f :: "nat \<Rightarrow> 'a"
  2791   assumes "finite (range f)"
  2792   shows "\<exists>k. infinite {n. f n = k}"
  2793 proof -
  2794   { fix A :: "'a set" assume "finite A"
  2795     hence "\<And>f. infinite {n. f n \<in> A} \<Longrightarrow> \<exists>k. infinite {n. f n = k}"
  2796     proof (induct)
  2797       case empty thus ?case by simp
  2798     next
  2799       case (insert x A)
  2800      show ?case
  2801       proof (cases "finite {n. f n = x}")
  2802         case True
  2803         with `infinite {n. f n \<in> insert x A}`
  2804         have "infinite {n. f n \<in> A}" by simp
  2805         thus "\<exists>k. infinite {n. f n = k}" by (rule insert)
  2806       next
  2807         case False thus "\<exists>k. infinite {n. f n = k}" ..
  2808       qed
  2809     qed
  2810   } note H = this
  2811   from assms show "\<exists>k. infinite {n. f n = k}"
  2812     by (rule H) simp
  2813 qed
  2814 
  2815 lemma bolzano_weierstrass_imp_compact:
  2816   fixes s :: "'a::metric_space set"
  2817   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
  2818   shows "compact s"
  2819 proof -
  2820   { fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
  2821     have "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2822     proof (cases "finite (range f)")
  2823       case True
  2824       hence "\<exists>l. infinite {n. f n = l}"
  2825         by (rule finite_range_imp_infinite_repeats)
  2826       then obtain l where "infinite {n. f n = l}" ..
  2827       hence "\<exists>r. subseq r \<and> (\<forall>n. r n \<in> {n. f n = l})"
  2828         by (rule infinite_enumerate)
  2829       then obtain r where "subseq r" and fr: "\<forall>n. f (r n) = l" by auto
  2830       hence "subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2831         unfolding o_def by (simp add: fr Lim_const)
  2832       hence "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2833         by - (rule exI)
  2834       from f have "\<forall>n. f (r n) \<in> s" by simp
  2835       hence "l \<in> s" by (simp add: fr)
  2836       thus "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2837         by (rule rev_bexI) fact
  2838     next
  2839       case False
  2840       with f assms have "\<exists>x\<in>s. x islimpt (range f)" by auto
  2841       then obtain l where "l \<in> s" "l islimpt (range f)" ..
  2842       have "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  2843         using `l islimpt (range f)`
  2844         by (rule islimpt_range_imp_convergent_subsequence)
  2845       with `l \<in> s` show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" ..
  2846     qed
  2847   }
  2848   thus ?thesis unfolding compact_def by auto
  2849 qed
  2850 
  2851 primrec helper_2::"(real \<Rightarrow> 'a::metric_space) \<Rightarrow> nat \<Rightarrow> 'a" where
  2852   "helper_2 beyond 0 = beyond 0" |
  2853   "helper_2 beyond (Suc n) = beyond (dist undefined (helper_2 beyond n) + 1 )"
  2854 
  2855 lemma bolzano_weierstrass_imp_bounded: fixes s::"'a::metric_space set"
  2856   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
  2857   shows "bounded s"
  2858 proof(rule ccontr)
  2859   assume "\<not> bounded s"
  2860   then obtain beyond where "\<forall>a. beyond a \<in>s \<and> \<not> dist undefined (beyond a) \<le> a"
  2861     unfolding bounded_any_center [where a=undefined]
  2862     apply simp using choice[of "\<lambda>a x. x\<in>s \<and> \<not> dist undefined x \<le> a"] by auto
  2863   hence beyond:"\<And>a. beyond a \<in>s" "\<And>a. dist undefined (beyond a) > a"
  2864     unfolding linorder_not_le by auto
  2865   def x \<equiv> "helper_2 beyond"
  2866 
  2867   { fix m n ::nat assume "m<n"
  2868     hence "dist undefined (x m) + 1 < dist undefined (x n)"
  2869     proof(induct n)
  2870       case 0 thus ?case by auto
  2871     next
  2872       case (Suc n)
  2873       have *:"dist undefined (x n) + 1 < dist undefined (x (Suc n))"
  2874         unfolding x_def and helper_2.simps
  2875         using beyond(2)[of "dist undefined (helper_2 beyond n) + 1"] by auto
  2876       thus ?case proof(cases "m < n")
  2877         case True thus ?thesis using Suc and * by auto
  2878       next
  2879         case False hence "m = n" using Suc(2) by auto
  2880         thus ?thesis using * by auto
  2881       qed
  2882     qed  } note * = this
  2883   { fix m n ::nat assume "m\<noteq>n"
  2884     have "1 < dist (x m) (x n)"
  2885     proof(cases "m<n")
  2886       case True
  2887       hence "1 < dist undefined (x n) - dist undefined (x m)" using *[of m n] by auto
  2888       thus ?thesis using dist_triangle [of undefined "x n" "x m"] by arith
  2889     next
  2890       case False hence "n<m" using `m\<noteq>n` by auto
  2891       hence "1 < dist undefined (x m) - dist undefined (x n)" using *[of n m] by auto
  2892       thus ?thesis using dist_triangle2 [of undefined "x m" "x n"] by arith
  2893     qed  } note ** = this
  2894   { fix a b assume "x a = x b" "a \<noteq> b"
  2895     hence False using **[of a b] by auto  }
  2896   hence "inj x" unfolding inj_on_def by auto
  2897   moreover
  2898   { fix n::nat
  2899     have "x n \<in> s"
  2900     proof(cases "n = 0")
  2901       case True thus ?thesis unfolding x_def using beyond by auto
  2902     next
  2903       case False then obtain z where "n = Suc z" using not0_implies_Suc by auto
  2904       thus ?thesis unfolding x_def using beyond by auto
  2905     qed  }
  2906   ultimately have "infinite (range x) \<and> range x \<subseteq> s" unfolding x_def using range_inj_infinite[of "helper_2 beyond"] using beyond(1) by auto
  2907 
  2908   then obtain l where "l\<in>s" and l:"l islimpt range x" using assms[THEN spec[where x="range x"]] by auto
  2909   then obtain y where "x y \<noteq> l" and y:"dist (x y) l < 1/2" unfolding islimpt_approachable apply(erule_tac x="1/2" in allE) by auto
  2910   then obtain z where "x z \<noteq> l" and z:"dist (x z) l < dist (x y) l" using l[unfolded islimpt_approachable, THEN spec[where x="dist (x y) l"]]
  2911     unfolding dist_nz by auto
  2912   show False using y and z and dist_triangle_half_l[of "x y" l 1 "x z"] and **[of y z] by auto
  2913 qed
  2914 
  2915 lemma sequence_infinite_lemma:
  2916   fixes f :: "nat \<Rightarrow> 'a::t1_space"
  2917   assumes "\<forall>n. f n \<noteq> l" and "(f ---> l) sequentially"
  2918   shows "infinite (range f)"
  2919 proof
  2920   assume "finite (range f)"
  2921   hence "closed (range f)" by (rule finite_imp_closed)
  2922   hence "open (- range f)" by (rule open_Compl)
  2923   from assms(1) have "l \<in> - range f" by auto
  2924   from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
  2925     using `open (- range f)` `l \<in> - range f` by (rule topological_tendstoD)
  2926   thus False unfolding eventually_sequentially by auto
  2927 qed
  2928 
  2929 lemma closure_insert:
  2930   fixes x :: "'a::t1_space"
  2931   shows "closure (insert x s) = insert x (closure s)"
  2932 apply (rule closure_unique)
  2933 apply (rule conjI [OF insert_mono [OF closure_subset]])
  2934 apply (rule conjI [OF closed_insert [OF closed_closure]])
  2935 apply (simp add: closure_minimal)
  2936 done
  2937 
  2938 lemma islimpt_insert:
  2939   fixes x :: "'a::t1_space"
  2940   shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
  2941 proof
  2942   assume *: "x islimpt (insert a s)"
  2943   show "x islimpt s"
  2944   proof (rule islimptI)
  2945     fix t assume t: "x \<in> t" "open t"
  2946     show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
  2947     proof (cases "x = a")
  2948       case True
  2949       obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
  2950         using * t by (rule islimptE)
  2951       with `x = a` show ?thesis by auto
  2952     next
  2953       case False
  2954       with t have t': "x \<in> t - {a}" "open (t - {a})"
  2955         by (simp_all add: open_Diff)
  2956       obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
  2957         using * t' by (rule islimptE)
  2958       thus ?thesis by auto
  2959     qed
  2960   qed
  2961 next
  2962   assume "x islimpt s" thus "x islimpt (insert a s)"
  2963     by (rule islimpt_subset) auto
  2964 qed
  2965 
  2966 lemma islimpt_union_finite:
  2967   fixes x :: "'a::t1_space"
  2968   shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
  2969 by (induct set: finite, simp_all add: islimpt_insert)
  2970  
  2971 lemma sequence_unique_limpt:
  2972   fixes f :: "nat \<Rightarrow> 'a::t2_space"
  2973   assumes "(f ---> l) sequentially" and "l' islimpt (range f)"
  2974   shows "l' = l"
  2975 proof (rule ccontr)
  2976   assume "l' \<noteq> l"
  2977   obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
  2978     using hausdorff [OF `l' \<noteq> l`] by auto
  2979   have "eventually (\<lambda>n. f n \<in> t) sequentially"
  2980     using assms(1) `open t` `l \<in> t` by (rule topological_tendstoD)
  2981   then obtain N where "\<forall>n\<ge>N. f n \<in> t"
  2982     unfolding eventually_sequentially by auto
  2983 
  2984   have "UNIV = {..<N} \<union> {N..}" by auto
  2985   hence "l' islimpt (f ` ({..<N} \<union> {N..}))" using assms(2) by simp
  2986   hence "l' islimpt (f ` {..<N} \<union> f ` {N..})" by (simp add: image_Un)
  2987   hence "l' islimpt (f ` {N..})" by (simp add: islimpt_union_finite)
  2988   then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
  2989     using `l' \<in> s` `open s` by (rule islimptE)
  2990   then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'" by auto
  2991   with `\<forall>n\<ge>N. f n \<in> t` have "f n \<in> s \<inter> t" by simp
  2992   with `s \<inter> t = {}` show False by simp
  2993 qed
  2994 
  2995 lemma bolzano_weierstrass_imp_closed:
  2996   fixes s :: "'a::metric_space set" (* TODO: can this be generalized? *)
  2997   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
  2998   shows "closed s"
  2999 proof-
  3000   { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"
  3001     hence "l \<in> s"
  3002     proof(cases "\<forall>n. x n \<noteq> l")
  3003       case False thus "l\<in>s" using as(1) by auto
  3004     next
  3005       case True note cas = this
  3006       with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto
  3007       then obtain l' where "l'\<in>s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto
  3008       thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto
  3009     qed  }
  3010   thus ?thesis unfolding closed_sequential_limits by fast
  3011 qed
  3012 
  3013 text{* Hence express everything as an equivalence.   *}
  3014 
  3015 lemma compact_eq_heine_borel:
  3016   fixes s :: "'a::metric_space set"
  3017   shows "compact s \<longleftrightarrow>
  3018            (\<forall>f. (\<forall>t \<in> f. open t) \<and> s \<subseteq> (\<Union> f)
  3019                --> (\<exists>f'. f' \<subseteq> f \<and> finite f' \<and> s \<subseteq> (\<Union> f')))" (is "?lhs = ?rhs")
  3020 proof
  3021   assume ?lhs thus ?rhs by (rule compact_imp_heine_borel)
  3022 next
  3023   assume ?rhs
  3024   hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x\<in>s. x islimpt t)"
  3025     by (blast intro: heine_borel_imp_bolzano_weierstrass[of s])
  3026   thus ?lhs by (rule bolzano_weierstrass_imp_compact)
  3027 qed
  3028 
  3029 lemma compact_eq_bolzano_weierstrass:
  3030   fixes s :: "'a::metric_space set"
  3031   shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")
  3032 proof
  3033   assume ?lhs thus ?rhs unfolding compact_eq_heine_borel using heine_borel_imp_bolzano_weierstrass[of s] by auto
  3034 next
  3035   assume ?rhs thus ?lhs by (rule bolzano_weierstrass_imp_compact)
  3036 qed
  3037 
  3038 lemma compact_eq_bounded_closed:
  3039   fixes s :: "'a::heine_borel set"
  3040   shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")
  3041 proof
  3042   assume ?lhs thus ?rhs unfolding compact_eq_bolzano_weierstrass using bolzano_weierstrass_imp_bounded bolzano_weierstrass_imp_closed by auto
  3043 next
  3044   assume ?rhs thus ?lhs using bounded_closed_imp_compact by auto
  3045 qed
  3046 
  3047 lemma compact_imp_bounded:
  3048   fixes s :: "'a::metric_space set"
  3049   shows "compact s ==> bounded s"
  3050 proof -
  3051   assume "compact s"
  3052   hence "\<forall>f. (\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"
  3053     by (rule compact_imp_heine_borel)
  3054   hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
  3055     using heine_borel_imp_bolzano_weierstrass[of s] by auto
  3056   thus "bounded s"
  3057     by (rule bolzano_weierstrass_imp_bounded)
  3058 qed
  3059 
  3060 lemma compact_imp_closed:
  3061   fixes s :: "'a::metric_space set"
  3062   shows "compact s ==> closed s"
  3063 proof -
  3064   assume "compact s"
  3065   hence "\<forall>f. (\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"
  3066     by (rule compact_imp_heine_borel)
  3067   hence "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t)"
  3068     using heine_borel_imp_bolzano_weierstrass[of s] by auto
  3069   thus "closed s"
  3070     by (rule bolzano_weierstrass_imp_closed)
  3071 qed
  3072 
  3073 text{* In particular, some common special cases. *}
  3074 
  3075 lemma compact_empty[simp]:
  3076  "compact {}"
  3077   unfolding compact_def
  3078   by simp
  3079 
  3080 lemma subseq_o: "subseq r \<Longrightarrow> subseq s \<Longrightarrow> subseq (r \<circ> s)"
  3081   unfolding subseq_def by simp (* TODO: move somewhere else *)
  3082 
  3083 lemma compact_union [intro]:
  3084   assumes "compact s" and "compact t"
  3085   shows "compact (s \<union> t)"
  3086 proof (rule compactI)
  3087   fix f :: "nat \<Rightarrow> 'a"
  3088   assume "\<forall>n. f n \<in> s \<union> t"
  3089   hence "infinite {n. f n \<in> s \<union> t}" by simp
  3090   hence "infinite {n. f n \<in> s} \<or> infinite {n. f n \<in> t}" by simp
  3091   thus "\<exists>l\<in>s \<union> t. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  3092   proof
  3093     assume "infinite {n. f n \<in> s}"
  3094     from infinite_enumerate [OF this]
  3095     obtain q where "subseq q" "\<forall>n. (f \<circ> q) n \<in> s" by auto
  3096     obtain r l where "l \<in> s" "subseq r" "((f \<circ> q \<circ> r) ---> l) sequentially"
  3097       using `compact s` `\<forall>n. (f \<circ> q) n \<in> s` by (rule compactE)
  3098     hence "l \<in> s \<union> t" "subseq (q \<circ> r)" "((f \<circ> (q \<circ> r)) ---> l) sequentially"
  3099       using `subseq q` by (simp_all add: subseq_o o_assoc)
  3100     thus ?thesis by auto
  3101   next
  3102     assume "infinite {n. f n \<in> t}"
  3103     from infinite_enumerate [OF this]
  3104     obtain q where "subseq q" "\<forall>n. (f \<circ> q) n \<in> t" by auto
  3105     obtain r l where "l \<in> t" "subseq r" "((f \<circ> q \<circ> r) ---> l) sequentially"
  3106       using `compact t` `\<forall>n. (f \<circ> q) n \<in> t` by (rule compactE)
  3107     hence "l \<in> s \<union> t" "subseq (q \<circ> r)" "((f \<circ> (q \<circ> r)) ---> l) sequentially"
  3108       using `subseq q` by (simp_all add: subseq_o o_assoc)
  3109     thus ?thesis by auto
  3110   qed
  3111 qed
  3112 
  3113 lemma compact_inter_closed [intro]:
  3114   assumes "compact s" and "closed t"
  3115   shows "compact (s \<inter> t)"
  3116 proof (rule compactI)
  3117   fix f :: "nat \<Rightarrow> 'a"
  3118   assume "\<forall>n. f n \<in> s \<inter> t"
  3119   hence "\<forall>n. f n \<in> s" and "\<forall>n. f n \<in> t" by simp_all
  3120   obtain l r where "l \<in> s" "subseq r" "((f \<circ> r) ---> l) sequentially"
  3121     using `compact s` `\<forall>n. f n \<in> s` by (rule compactE)
  3122   moreover
  3123   from `closed t` `\<forall>n. f n \<in> t` `((f \<circ> r) ---> l) sequentially` have "l \<in> t"
  3124     unfolding closed_sequential_limits o_def by fast
  3125   ultimately show "\<exists>l\<in>s \<inter> t. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  3126     by auto
  3127 qed
  3128 
  3129 lemma closed_inter_compact [intro]:
  3130   assumes "closed s" and "compact t"
  3131   shows "compact (s \<inter> t)"
  3132   using compact_inter_closed [of t s] assms
  3133   by (simp add: Int_commute)
  3134 
  3135 lemma compact_inter [intro]:
  3136   assumes "compact s" and "compact t"
  3137   shows "compact (s \<inter> t)"
  3138   using assms by (intro compact_inter_closed compact_imp_closed)
  3139 
  3140 lemma compact_sing [simp]: "compact {a}"
  3141   unfolding compact_def o_def subseq_def
  3142   by (auto simp add: tendsto_const)
  3143 
  3144 lemma compact_insert [simp]:
  3145   assumes "compact s" shows "compact (insert x s)"
  3146 proof -
  3147   have "compact ({x} \<union> s)"
  3148     using compact_sing assms by (rule compact_union)
  3149   thus ?thesis by simp
  3150 qed
  3151 
  3152 lemma finite_imp_compact:
  3153   shows "finite s \<Longrightarrow> compact s"
  3154   by (induct set: finite) simp_all
  3155 
  3156 lemma compact_cball[simp]:
  3157   fixes x :: "'a::heine_borel"
  3158   shows "compact(cball x e)"
  3159   using compact_eq_bounded_closed bounded_cball closed_cball
  3160   by blast
  3161 
  3162 lemma compact_frontier_bounded[intro]:
  3163   fixes s :: "'a::heine_borel set"
  3164   shows "bounded s ==> compact(frontier s)"
  3165   unfolding frontier_def
  3166   using compact_eq_bounded_closed
  3167   by blast
  3168 
  3169 lemma compact_frontier[intro]:
  3170   fixes s :: "'a::heine_borel set"
  3171   shows "compact s ==> compact (frontier s)"
  3172   using compact_eq_bounded_closed compact_frontier_bounded
  3173   by blast
  3174 
  3175 lemma frontier_subset_compact:
  3176   fixes s :: "'a::heine_borel set"
  3177   shows "compact s ==> frontier s \<subseteq> s"
  3178   using frontier_subset_closed compact_eq_bounded_closed
  3179   by blast
  3180 
  3181 lemma open_delete:
  3182   fixes s :: "'a::t1_space set"
  3183   shows "open s \<Longrightarrow> open (s - {x})"
  3184   by (simp add: open_Diff)
  3185 
  3186 text{* Finite intersection property. I could make it an equivalence in fact. *}
  3187 
  3188 lemma compact_imp_fip:
  3189   assumes "compact s"  "\<forall>t \<in> f. closed t"
  3190         "\<forall>f'. finite f' \<and> f' \<subseteq> f --> (s \<inter> (\<Inter> f') \<noteq> {})"
  3191   shows "s \<inter> (\<Inter> f) \<noteq> {}"
  3192 proof
  3193   assume as:"s \<inter> (\<Inter> f) = {}"
  3194   hence "s \<subseteq> \<Union> uminus ` f" by auto
  3195   moreover have "Ball (uminus ` f) open" using open_Diff closed_Diff using assms(2) by auto
  3196   ultimately obtain f' where f':"f' \<subseteq> uminus ` f"  "finite f'"  "s \<subseteq> \<Union>f'" using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="(\<lambda>t. - t) ` f"]] by auto
  3197   hence "finite (uminus ` f') \<and> uminus ` f' \<subseteq> f" by(auto simp add: Diff_Diff_Int)
  3198   hence "s \<inter> \<Inter>uminus ` f' \<noteq> {}" using assms(3)[THEN spec[where x="uminus ` f'"]] by auto
  3199   thus False using f'(3) unfolding subset_eq and Union_iff by blast
  3200 qed
  3201 
  3202 subsection{* Bounded closed nest property (proof does not use Heine-Borel).            *}
  3203 
  3204 lemma bounded_closed_nest:
  3205   assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"
  3206   "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"
  3207   shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"
  3208 proof-
  3209   from assms(2) obtain x where x:"\<forall>n::nat. x n \<in> s n" using choice[of "\<lambda>n x. x\<in> s n"] by auto
  3210   from assms(4,1) have *:"compact (s 0)" using bounded_closed_imp_compact[of "s 0"] by auto
  3211 
  3212   then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"
  3213     unfolding compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast
  3214 
  3215   { fix n::nat
  3216     { fix e::real assume "e>0"
  3217       with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding Lim_sequentially by auto
  3218       hence "dist ((x \<circ> r) (max N n)) l < e" by auto
  3219       moreover
  3220       have "r (max N n) \<ge> n" using lr(2) using subseq_bigger[of r "max N n"] by auto
  3221       hence "(x \<circ> r) (max N n) \<in> s n"
  3222         using x apply(erule_tac x=n in allE)
  3223         using x apply(erule_tac x="r (max N n)" in allE)
  3224         using assms(3) apply(erule_tac x=n in allE)apply( erule_tac x="r (max N n)" in allE) by auto
  3225       ultimately have "\<exists>y\<in>s n. dist y l < e" by auto
  3226     }
  3227     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast
  3228   }
  3229   thus ?thesis by auto
  3230 qed
  3231 
  3232 text{* Decreasing case does not even need compactness, just completeness.        *}
  3233 
  3234 lemma decreasing_closed_nest:
  3235   assumes "\<forall>n. closed(s n)"
  3236           "\<forall>n. (s n \<noteq> {})"
  3237           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
  3238           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y \<in> (s n). dist x y < e"
  3239   shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s n"
  3240 proof-
  3241   have "\<forall>n. \<exists> x. x\<in>s n" using assms(2) by auto
  3242   hence "\<exists>t. \<forall>n. t n \<in> s n" using choice[of "\<lambda> n x. x \<in> s n"] by auto
  3243   then obtain t where t: "\<forall>n. t n \<in> s n" by auto
  3244   { fix e::real assume "e>0"
  3245     then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e" using assms(4) by auto
  3246     { fix m n ::nat assume "N \<le> m \<and> N \<le> n"
  3247       hence "t m \<in> s N" "t n \<in> s N" using assms(3) t unfolding  subset_eq t by blast+
  3248       hence "dist (t m) (t n) < e" using N by auto
  3249     }
  3250     hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e" by auto
  3251   }
  3252   hence  "Cauchy t" unfolding cauchy_def by auto
  3253   then obtain l where l:"(t ---> l) sequentially" using complete_univ unfolding complete_def by auto
  3254   { fix n::nat
  3255     { fix e::real assume "e>0"
  3256       then obtain N::nat where N:"\<forall>n\<ge>N. dist (t n) l < e" using l[unfolded Lim_sequentially] by auto
  3257       have "t (max n N) \<in> s n" using assms(3) unfolding subset_eq apply(erule_tac x=n in allE) apply (erule_tac x="max n N" in allE) using t by auto
  3258       hence "\<exists>y\<in>s n. dist y l < e" apply(rule_tac x="t (max n N)" in bexI) using N by auto
  3259     }
  3260     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by auto
  3261   }
  3262   then show ?thesis by auto
  3263 qed
  3264 
  3265 text{* Strengthen it to the intersection actually being a singleton.             *}
  3266 
  3267 lemma decreasing_closed_nest_sing:
  3268   fixes s :: "nat \<Rightarrow> 'a::heine_borel set"
  3269   assumes "\<forall>n. closed(s n)"
  3270           "\<forall>n. s n \<noteq> {}"
  3271           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
  3272           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"
  3273   shows "\<exists>a. \<Inter>(range s) = {a}"
  3274 proof-
  3275   obtain a where a:"\<forall>n. a \<in> s n" using decreasing_closed_nest[of s] using assms by auto
  3276   { fix b assume b:"b \<in> \<Inter>(range s)"
  3277     { fix e::real assume "e>0"
  3278       hence "dist a b < e" using assms(4 )using b using a by blast
  3279     }
  3280     hence "dist a b = 0" by (metis dist_eq_0_iff dist_nz less_le)
  3281   }
  3282   with a have "\<Inter>(range s) = {a}" unfolding image_def by auto
  3283   thus ?thesis ..
  3284 qed
  3285 
  3286 text{* Cauchy-type criteria for uniform convergence. *}
  3287 
  3288 lemma uniformly_convergent_eq_cauchy: fixes s::"nat \<Rightarrow> 'b \<Rightarrow> 'a::heine_borel" shows
  3289  "(\<exists>l. \<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e) \<longleftrightarrow>
  3290   (\<forall>e>0. \<exists>N. \<forall>m n x. N \<le> m \<and> N \<le> n \<and> P x  --> dist (s m x) (s n x) < e)" (is "?lhs = ?rhs")
  3291 proof(rule)
  3292   assume ?lhs
  3293   then obtain l where l:"\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist (s n x) (l x) < e" by auto
  3294   { fix e::real assume "e>0"
  3295     then obtain N::nat where N:"\<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist (s n x) (l x) < e / 2" using l[THEN spec[where x="e/2"]] by auto
  3296     { fix n m::nat and x::"'b" assume "N \<le> m \<and> N \<le> n \<and> P x"
  3297       hence "dist (s m x) (s n x) < e"
  3298         using N[THEN spec[where x=m], THEN spec[where x=x]]
  3299         using N[THEN spec[where x=n], THEN spec[where x=x]]
  3300         using dist_triangle_half_l[of "s m x" "l x" e "s n x"] by auto  }
  3301     hence "\<exists>N. \<forall>m n x. N \<le> m \<and> N \<le> n \<and> P x  --> dist (s m x) (s n x) < e"  by auto  }
  3302   thus ?rhs by auto
  3303 next
  3304   assume ?rhs
  3305   hence "\<forall>x. P x \<longrightarrow> Cauchy (\<lambda>n. s n x)" unfolding cauchy_def apply auto by (erule_tac x=e in allE)auto
  3306   then obtain l where l:"\<forall>x. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l x) sequentially" unfolding convergent_eq_cauchy[THEN sym]
  3307     using choice[of "\<lambda>x l. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l) sequentially"] by auto
  3308   { fix e::real assume "e>0"
  3309     then obtain N where N:"\<forall>m n x. N \<le> m \<and> N \<le> n \<and> P x \<longrightarrow> dist (s m x) (s n x) < e/2"
  3310       using `?rhs`[THEN spec[where x="e/2"]] by auto
  3311     { fix x assume "P x"
  3312       then obtain M where M:"\<forall>n\<ge>M. dist (s n x) (l x) < e/2"
  3313         using l[THEN spec[where x=x], unfolded Lim_sequentially] using `e>0` by(auto elim!: allE[where x="e/2"])
  3314       fix n::nat assume "n\<ge>N"
  3315       hence "dist(s n x)(l x) < e"  using `P x`and N[THEN spec[where x=n], THEN spec[where x="N+M"], THEN spec[where x=x]]
  3316         using M[THEN spec[where x="N+M"]] and dist_triangle_half_l[of "s n x" "s (N+M) x" e "l x"] by (auto simp add: dist_commute)  }
  3317     hence "\<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist(s n x)(l x) < e" by auto }
  3318   thus ?lhs by auto
  3319 qed
  3320 
  3321 lemma uniformly_cauchy_imp_uniformly_convergent:
  3322   fixes s :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::heine_borel"
  3323   assumes "\<forall>e>0.\<exists>N. \<forall>m (n::nat) x. N \<le> m \<and> N \<le> n \<and> P x --> dist(s m x)(s n x) < e"
  3324           "\<forall>x. P x --> (\<forall>e>0. \<exists>N. \<forall>n. N \<le> n --> dist(s n x)(l x) < e)"
  3325   shows "\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e"
  3326 proof-
  3327   obtain l' where l:"\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist (s n x) (l' x) < e"
  3328     using assms(1) unfolding uniformly_convergent_eq_cauchy[THEN sym] by auto
  3329   moreover
  3330   { fix x assume "P x"
  3331     hence "l x = l' x" using tendsto_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]
  3332       using l and assms(2) unfolding Lim_sequentially by blast  }
  3333   ultimately show ?thesis by auto
  3334 qed
  3335 
  3336 subsection {* Continuity *}
  3337 
  3338 text {* Define continuity over a net to take in restrictions of the set. *}
  3339 
  3340 definition
  3341   continuous :: "'a::t2_space net \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool" where
  3342   "continuous net f \<longleftrightarrow> (f ---> f(netlimit net)) net"
  3343 
  3344 lemma continuous_trivial_limit:
  3345  "trivial_limit net ==> continuous net f"
  3346   unfolding continuous_def tendsto_def trivial_limit_eq by auto
  3347 
  3348 lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f(x)) (at x within s)"
  3349   unfolding continuous_def
  3350   unfolding tendsto_def
  3351   using netlimit_within[of x s]
  3352   by (cases "trivial_limit (at x within s)") (auto simp add: trivial_limit_eventually)
  3353 
  3354 lemma continuous_at: "continuous (at x) f \<longleftrightarrow> (f ---> f(x)) (at x)"
  3355   using continuous_within [of x UNIV f] by (simp add: within_UNIV)
  3356 
  3357 lemma continuous_at_within:
  3358   assumes "continuous (at x) f"  shows "continuous (at x within s) f"
  3359   using assms unfolding continuous_at continuous_within
  3360   by (rule Lim_at_within)
  3361 
  3362 text{* Derive the epsilon-delta forms, which we often use as "definitions" *}
  3363 
  3364 lemma continuous_within_eps_delta:
  3365   "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. \<forall>x'\<in> s.  dist x' x < d --> dist (f x') (f x) < e)"
  3366   unfolding continuous_within and Lim_within
  3367   apply auto unfolding dist_nz[THEN sym] apply(auto elim!:allE) apply(rule_tac x=d in exI) by auto
  3368 
  3369 lemma continuous_at_eps_delta: "continuous (at x) f \<longleftrightarrow>  (\<forall>e>0. \<exists>d>0.
  3370                            \<forall>x'. dist x' x < d --> dist(f x')(f x) < e)"
  3371   using continuous_within_eps_delta[of x UNIV f]
  3372   unfolding within_UNIV by blast
  3373 
  3374 text{* Versions in terms of open balls. *}
  3375 
  3376 lemma continuous_within_ball:
  3377  "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
  3378                             f ` (ball x d \<inter> s) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
  3379 proof
  3380   assume ?lhs
  3381   { fix e::real assume "e>0"
  3382     then obtain d where d: "d>0" "\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < e"
  3383       using `?lhs`[unfolded continuous_within Lim_within] by auto
  3384     { fix y assume "y\<in>f ` (ball x d \<inter> s)"
  3385       hence "y \<in> ball (f x) e" using d(2) unfolding dist_nz[THEN sym]
  3386         apply (auto simp add: dist_commute) apply(erule_tac x=xa in ballE) apply auto using `e>0` by auto
  3387     }
  3388     hence "\<exists>d>0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e" using `d>0` unfolding subset_eq ball_def by (auto simp add: dist_commute)  }
  3389   thus ?rhs by auto
  3390 next
  3391   assume ?rhs thus ?lhs unfolding continuous_within Lim_within ball_def subset_eq
  3392     apply (auto simp add: dist_commute) apply(erule_tac x=e in allE) by auto
  3393 qed
  3394 
  3395 lemma continuous_at_ball:
  3396   "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f ` (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
  3397 proof
  3398   assume ?lhs thus ?rhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
  3399     apply auto apply(erule_tac x=e in allE) apply auto apply(rule_tac x=d in exI) apply auto apply(erule_tac x=xa in allE) apply (auto simp add: dist_commute dist_nz)
  3400     unfolding dist_nz[THEN sym] by auto
  3401 next
  3402   assume ?rhs thus ?lhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
  3403     apply auto apply(erule_tac x=e in allE) apply auto apply(rule_tac x=d in exI) apply auto apply(erule_tac x="f xa" in allE) by (auto simp add: dist_commute dist_nz)
  3404 qed
  3405 
  3406 text{* Define setwise continuity in terms of limits within the set. *}
  3407 
  3408 definition
  3409   continuous_on ::
  3410     "'a set \<Rightarrow> ('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"
  3411 where
  3412   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f ---> f x) (at x within s))"
  3413 
  3414 lemma continuous_on_topological:
  3415   "continuous_on s f \<longleftrightarrow>
  3416     (\<forall>x\<in>s. \<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>
  3417       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
  3418 unfolding continuous_on_def tendsto_def
  3419 unfolding Limits.eventually_within eventually_at_topological
  3420 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto
  3421 
  3422 lemma continuous_on_iff:
  3423   "continuous_on s f \<longleftrightarrow>
  3424     (\<forall>x\<in>s. \<forall>e>0. \<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
  3425 unfolding continuous_on_def Lim_within
  3426 apply (intro ball_cong [OF refl] all_cong ex_cong)
  3427 apply (rename_tac y, case_tac "y = x", simp)
  3428 apply (simp add: dist_nz)
  3429 done
  3430 
  3431 definition
  3432   uniformly_continuous_on ::
  3433     "'a set \<Rightarrow> ('a::metric_space \<Rightarrow> 'b::metric_space) \<Rightarrow> bool"
  3434 where
  3435   "uniformly_continuous_on s f \<longleftrightarrow>
  3436     (\<forall>e>0. \<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
  3437 
  3438 text{* Some simple consequential lemmas. *}
  3439 
  3440 lemma uniformly_continuous_imp_continuous:
  3441  " uniformly_continuous_on s f ==> continuous_on s f"
  3442   unfolding uniformly_continuous_on_def continuous_on_iff by blast
  3443 
  3444 lemma continuous_at_imp_continuous_within:
  3445  "continuous (at x) f ==> continuous (at x within s) f"
  3446   unfolding continuous_within continuous_at using Lim_at_within by auto
  3447 
  3448 lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f ---> l) net"
  3449 unfolding tendsto_def by (simp add: trivial_limit_eq)
  3450 
  3451 lemma continuous_at_imp_continuous_on:
  3452   assumes "\<forall>x\<in>s. continuous (at x) f"
  3453   shows "continuous_on s f"
  3454 unfolding continuous_on_def
  3455 proof
  3456   fix x assume "x \<in> s"
  3457   with assms have *: "(f ---> f (netlimit (at x))) (at x)"
  3458     unfolding continuous_def by simp
  3459   have "(f ---> f x) (at x)"
  3460   proof (cases "trivial_limit (at x)")
  3461     case True thus ?thesis
  3462       by (rule Lim_trivial_limit)
  3463   next
  3464     case False
  3465     hence 1: "netlimit (at x) = x"
  3466       using netlimit_within [of x UNIV]
  3467       by (simp add: within_UNIV)
  3468     with * show ?thesis by simp
  3469   qed
  3470   thus "(f ---> f x) (at x within s)"
  3471     by (rule Lim_at_within)
  3472 qed
  3473 
  3474 lemma continuous_on_eq_continuous_within:
  3475   "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x within s) f)"
  3476 unfolding continuous_on_def continuous_def
  3477 apply (rule ball_cong [OF refl])
  3478 apply (case_tac "trivial_limit (at x within s)")
  3479 apply (simp add: Lim_trivial_limit)
  3480 apply (simp add: netlimit_within)
  3481 done
  3482 
  3483 lemmas continuous_on = continuous_on_def -- "legacy theorem name"
  3484 
  3485 lemma continuous_on_eq_continuous_at:
  3486   shows "open s ==> (continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x) f))"
  3487   by (auto simp add: continuous_on continuous_at Lim_within_open)
  3488 
  3489 lemma continuous_within_subset:
  3490  "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s
  3491              ==> continuous (at x within t) f"
  3492   unfolding continuous_within by(metis Lim_within_subset)
  3493 
  3494 lemma continuous_on_subset:
  3495   shows "continuous_on s f \<Longrightarrow> t \<subseteq> s ==> continuous_on t f"
  3496   unfolding continuous_on by (metis subset_eq Lim_within_subset)
  3497 
  3498 lemma continuous_on_interior:
  3499   shows "continuous_on s f \<Longrightarrow> x \<in> interior s ==> continuous (at x) f"
  3500 unfolding interior_def
  3501 apply simp
  3502 by (meson continuous_on_eq_continuous_at continuous_on_subset)
  3503 
  3504 lemma continuous_on_eq:
  3505   "(\<forall>x \<in> s. f x = g x) \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on s g"
  3506   unfolding continuous_on_def tendsto_def Limits.eventually_within
  3507   by simp
  3508 
  3509 text{* Characterization of various kinds of continuity in terms of sequences.  *}
  3510 
  3511 (* \<longrightarrow> could be generalized, but \<longleftarrow> requires metric space *)
  3512 lemma continuous_within_sequentially:
  3513   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  3514   shows "continuous (at a within s) f \<longleftrightarrow>
  3515                 (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x ---> a) sequentially
  3516                      --> ((f o x) ---> f a) sequentially)" (is "?lhs = ?rhs")
  3517 proof
  3518   assume ?lhs
  3519   { fix x::"nat \<Rightarrow> 'a" assume x:"\<forall>n. x n \<in> s" "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (x n) a < e"
  3520     fix e::real assume "e>0"
  3521     from `?lhs` obtain d where "d>0" and d:"\<forall>x\<in>s. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) (f a) < e" unfolding continuous_within Lim_within using `e>0` by auto
  3522     from x(2) `d>0` obtain N where N:"\<forall>n\<ge>N. dist (x n) a < d" by auto
  3523     hence "\<exists>N. \<forall>n\<ge>N. dist ((f \<circ> x) n) (f a) < e"
  3524       apply(rule_tac  x=N in exI) using N d  apply auto using x(1)
  3525       apply(erule_tac x=n in allE) apply(erule_tac x=n in allE)
  3526       apply(erule_tac x="x n" in ballE)  apply auto unfolding dist_nz[THEN sym] apply auto using `e>0` by auto
  3527   }
  3528   thus ?rhs unfolding continuous_within unfolding Lim_sequentially by simp
  3529 next
  3530   assume ?rhs
  3531   { fix e::real assume "e>0"
  3532     assume "\<not> (\<exists>d>0. \<forall>x\<in>s. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) (f a) < e)"
  3533     hence "\<forall>d. \<exists>x. d>0 \<longrightarrow> x\<in>s \<and> (0 < dist x a \<and> dist x a < d \<and> \<not> dist (f x) (f a) < e)" by blast
  3534     then obtain x where x:"\<forall>d>0. x d \<in> s \<and> (0 < dist (x d) a \<and> dist (x d) a < d \<and> \<not> dist (f (x d)) (f a) < e)"
  3535       using choice[of "\<lambda>d x.0<d \<longrightarrow> x\<in>s \<and> (0 < dist x a \<and> dist x a < d \<and> \<not> dist (f x) (f a) < e)"] by auto
  3536     { fix d::real assume "d>0"
  3537       hence "\<exists>N::nat. inverse (real (N + 1)) < d" using real_arch_inv[of d] by (auto, rule_tac x="n - 1" in exI)auto
  3538       then obtain N::nat where N:"inverse (real (N + 1)) < d" by auto
  3539       { fix n::nat assume n:"n\<ge>N"
  3540         hence "dist (x (inverse (real (n + 1)))) a < inverse (real (n + 1))" using x[THEN spec[where x="inverse (real (n + 1))"]] by auto
  3541         moreover have "inverse (real (n + 1)) < d" using N n by (auto, metis Suc_le_mono le_SucE less_imp_inverse_less nat_le_real_less order_less_trans real_of_nat_Suc real_of_nat_Suc_gt_zero)
  3542         ultimately have "dist (x (inverse (real (n + 1)))) a < d" by auto
  3543       }
  3544       hence "\<exists>N::nat. \<forall>n\<ge>N. dist (x (inverse (real (n + 1)))) a < d" by auto
  3545     }
  3546     hence "(\<forall>n::nat. x (inverse (real (n + 1))) \<in> s) \<and> (\<forall>e>0. \<exists>N::nat. \<forall>n\<ge>N. dist (x (inverse (real (n + 1)))) a < e)" using x by auto
  3547     hence "\<forall>e>0. \<exists>N::nat. \<forall>n\<ge>N. dist (f (x (inverse (real (n + 1))))) (f a) < e"  using `?rhs`[THEN spec[where x="\<lambda>n::nat. x (inverse (real (n+1)))"], unfolded Lim_sequentially] by auto
  3548     hence "False" apply(erule_tac x=e in allE) using `e>0` using x by auto
  3549   }
  3550   thus ?lhs  unfolding continuous_within unfolding Lim_within unfolding Lim_sequentially by blast
  3551 qed
  3552 
  3553 lemma continuous_at_sequentially:
  3554   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  3555   shows "continuous (at a) f \<longleftrightarrow> (\<forall>x. (x ---> a) sequentially
  3556                   --> ((f o x) ---> f a) sequentially)"
  3557   using continuous_within_sequentially[of a UNIV f] unfolding within_UNIV by auto
  3558 
  3559 lemma continuous_on_sequentially:
  3560   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  3561   shows "continuous_on s f \<longleftrightarrow>
  3562     (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x ---> a) sequentially
  3563                     --> ((f o x) ---> f(a)) sequentially)" (is "?lhs = ?rhs")
  3564 proof
  3565   assume ?rhs thus ?lhs using continuous_within_sequentially[of _ s f] unfolding continuous_on_eq_continuous_within by auto
  3566 next
  3567   assume ?lhs thus ?rhs unfolding continuous_on_eq_continuous_within using continuous_within_sequentially[of _ s f] by auto
  3568 qed
  3569 
  3570 lemma uniformly_continuous_on_sequentially':
  3571   "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
  3572                     ((\<lambda>n. dist (x n) (y n)) ---> 0) sequentially
  3573                     \<longrightarrow> ((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially)" (is "?lhs = ?rhs")
  3574 proof
  3575   assume ?lhs
  3576   { fix x y assume x:"\<forall>n. x n \<in> s" and y:"\<forall>n. y n \<in> s" and xy:"((\<lambda>n. dist (x n) (y n)) ---> 0) sequentially"
  3577     { fix e::real assume "e>0"
  3578       then obtain d where "d>0" and d:"\<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
  3579         using `?lhs`[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto
  3580       obtain N where N:"\<forall>n\<ge>N. dist (x n) (y n) < d" using xy[unfolded Lim_sequentially dist_norm] and `d>0` by auto
  3581       { fix n assume "n\<ge>N"
  3582         hence "dist (f (x n)) (f (y n)) < e"
  3583           using N[THEN spec[where x=n]] using d[THEN bspec[where x="x n"], THEN bspec[where x="y n"]] using x and y
  3584           unfolding dist_commute by simp  }
  3585       hence "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"  by auto  }
  3586     hence "((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially" unfolding Lim_sequentially and dist_real_def by auto  }
  3587   thus ?rhs by auto
  3588 next
  3589   assume ?rhs
  3590   { assume "\<not> ?lhs"
  3591     then obtain e where "e>0" "\<forall>d>0. \<exists>x\<in>s. \<exists>x'\<in>s. dist x' x < d \<and> \<not> dist (f x') (f x) < e" unfolding uniformly_continuous_on_def by auto
  3592     then obtain fa where fa:"\<forall>x.  0 < x \<longrightarrow> fst (fa x) \<in> s \<and> snd (fa x) \<in> s \<and> dist (fst (fa x)) (snd (fa x)) < x \<and> \<not> dist (f (fst (fa x))) (f (snd (fa x))) < e"
  3593       using choice[of "\<lambda>d x. d>0 \<longrightarrow> fst x \<in> s \<and> snd x \<in> s \<and> dist (snd x) (fst x) < d \<and> \<not> dist (f (snd x)) (f (fst x)) < e"] unfolding Bex_def
  3594       by (auto simp add: dist_commute)
  3595     def x \<equiv> "\<lambda>n::nat. fst (fa (inverse (real n + 1)))"
  3596     def y \<equiv> "\<lambda>n::nat. snd (fa (inverse (real n + 1)))"
  3597     have xyn:"\<forall>n. x n \<in> s \<and> y n \<in> s" and xy0:"\<forall>n. dist (x n) (y n) < inverse (real n + 1)" and fxy:"\<forall>n. \<not> dist (f (x n)) (f (y n)) < e"
  3598       unfolding x_def and y_def using fa by auto
  3599     { fix e::real assume "e>0"
  3600       then obtain N::nat where "N \<noteq> 0" and N:"0 < inverse (real N) \<and> inverse (real N) < e" unfolding real_arch_inv[of e]   by auto
  3601       { fix n::nat assume "n\<ge>N"
  3602         hence "inverse (real n + 1) < inverse (real N)" using real_of_nat_ge_zero and `N\<noteq>0` by auto
  3603         also have "\<dots> < e" using N by auto
  3604         finally have "inverse (real n + 1) < e" by auto
  3605         hence "dist (x n) (y n) < e" using xy0[THEN spec[where x=n]] by auto  }
  3606       hence "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto  }
  3607     hence "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e" using `?rhs`[THEN spec[where x=x], THEN spec[where x=y]] and xyn unfolding Lim_sequentially dist_real_def by auto
  3608     hence False using fxy and `e>0` by auto  }
  3609   thus ?lhs unfolding uniformly_continuous_on_def by blast
  3610 qed
  3611 
  3612 lemma uniformly_continuous_on_sequentially:
  3613   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
  3614   shows "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
  3615                     ((\<lambda>n. x n - y n) ---> 0) sequentially
  3616                     \<longrightarrow> ((\<lambda>n. f(x n) - f(y n)) ---> 0) sequentially)" (is "?lhs = ?rhs")
  3617 (* BH: maybe the previous lemma should replace this one? *)
  3618 unfolding uniformly_continuous_on_sequentially'
  3619 unfolding dist_norm Lim_null_norm [symmetric] ..
  3620 
  3621 text{* The usual transformation theorems. *}
  3622 
  3623 lemma continuous_transform_within:
  3624   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  3625   assumes "0 < d" "x \<in> s" "\<forall>x' \<in> s. dist x' x < d --> f x' = g x'"
  3626           "continuous (at x within s) f"
  3627   shows "continuous (at x within s) g"
  3628 unfolding continuous_within
  3629 proof (rule Lim_transform_within)
  3630   show "0 < d" by fact
  3631   show "\<forall>x'\<in>s. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"
  3632     using assms(3) by auto
  3633   have "f x = g x"
  3634     using assms(1,2,3) by auto
  3635   thus "(f ---> g x) (at x within s)"
  3636     using assms(4) unfolding continuous_within by simp
  3637 qed
  3638 
  3639 lemma continuous_transform_at:
  3640   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  3641   assumes "0 < d" "\<forall>x'. dist x' x < d --> f x' = g x'"
  3642           "continuous (at x) f"
  3643   shows "continuous (at x) g"
  3644   using continuous_transform_within [of d x UNIV f g] assms
  3645   by (simp add: within_UNIV)
  3646 
  3647 text{* Combination results for pointwise continuity. *}
  3648 
  3649 lemma continuous_const: "continuous net (\<lambda>x. c)"
  3650   by (auto simp add: continuous_def Lim_const)
  3651 
  3652 lemma continuous_cmul:
  3653   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  3654   shows "continuous net f ==> continuous net (\<lambda>x. c *\<^sub>R f x)"
  3655   by (auto simp add: continuous_def Lim_cmul)
  3656 
  3657 lemma continuous_neg:
  3658   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  3659   shows "continuous net f ==> continuous net (\<lambda>x. -(f x))"
  3660   by (auto simp add: continuous_def Lim_neg)
  3661 
  3662 lemma continuous_add:
  3663   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  3664   shows "continuous net f \<Longrightarrow> continuous net g \<Longrightarrow> continuous net (\<lambda>x. f x + g x)"
  3665   by (auto simp add: continuous_def Lim_add)
  3666 
  3667 lemma continuous_sub:
  3668   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  3669   shows "continuous net f \<Longrightarrow> continuous net g \<Longrightarrow> continuous net (\<lambda>x. f x - g x)"
  3670   by (auto simp add: continuous_def Lim_sub)
  3671 
  3672 
  3673 text{* Same thing for setwise continuity. *}
  3674 
  3675 lemma continuous_on_const:
  3676  "continuous_on s (\<lambda>x. c)"
  3677   unfolding continuous_on_def by auto
  3678 
  3679 lemma continuous_on_cmul:
  3680   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  3681   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. c *\<^sub>R (f x))"
  3682   unfolding continuous_on_def by (auto intro: tendsto_intros)
  3683 
  3684 lemma continuous_on_neg:
  3685   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  3686   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. - f x)"
  3687   unfolding continuous_on_def by (auto intro: tendsto_intros)
  3688 
  3689 lemma continuous_on_add:
  3690   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  3691   shows "continuous_on s f \<Longrightarrow> continuous_on s g
  3692            \<Longrightarrow> continuous_on s (\<lambda>x. f x + g x)"
  3693   unfolding continuous_on_def by (auto intro: tendsto_intros)
  3694 
  3695 lemma continuous_on_sub:
  3696   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  3697   shows "continuous_on s f \<Longrightarrow> continuous_on s g
  3698            \<Longrightarrow> continuous_on s (\<lambda>x. f x - g x)"
  3699   unfolding continuous_on_def by (auto intro: tendsto_intros)
  3700 
  3701 text{* Same thing for uniform continuity, using sequential formulations. *}
  3702 
  3703 lemma uniformly_continuous_on_const:
  3704  "uniformly_continuous_on s (\<lambda>x. c)"
  3705   unfolding uniformly_continuous_on_def by simp
  3706 
  3707 lemma uniformly_continuous_on_cmul:
  3708   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  3709   assumes "uniformly_continuous_on s f"
  3710   shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"
  3711 proof-
  3712   { fix x y assume "((\<lambda>n. f (x n) - f (y n)) ---> 0) sequentially"
  3713     hence "((\<lambda>n. c *\<^sub>R f (x n) - c *\<^sub>R f (y n)) ---> 0) sequentially"
  3714       using Lim_cmul[of "(\<lambda>n. f (x n) - f (y n))" 0 sequentially c]
  3715       unfolding scaleR_zero_right scaleR_right_diff_distrib by auto
  3716   }
  3717   thus ?thesis using assms unfolding uniformly_continuous_on_sequentially'
  3718     unfolding dist_norm Lim_null_norm [symmetric] by auto
  3719 qed
  3720 
  3721 lemma dist_minus:
  3722   fixes x y :: "'a::real_normed_vector"
  3723   shows "dist (- x) (- y) = dist x y"
  3724   unfolding dist_norm minus_diff_minus norm_minus_cancel ..
  3725 
  3726 lemma uniformly_continuous_on_neg:
  3727   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  3728   shows "uniformly_continuous_on s f
  3729          ==> uniformly_continuous_on s (\<lambda>x. -(f x))"
  3730   unfolding uniformly_continuous_on_def dist_minus .
  3731 
  3732 lemma uniformly_continuous_on_add:
  3733   fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  3734   assumes "uniformly_continuous_on s f" "uniformly_continuous_on s g"
  3735   shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"
  3736 proof-
  3737   {  fix x y assume "((\<lambda>n. f (x n) - f (y n)) ---> 0) sequentially"
  3738                     "((\<lambda>n. g (x n) - g (y n)) ---> 0) sequentially"
  3739     hence "((\<lambda>xa. f (x xa) - f (y xa) + (g (x xa) - g (y xa))) ---> 0 + 0) sequentially"
  3740       using Lim_add[of "\<lambda> n. f (x n) - f (y n)" 0  sequentially "\<lambda> n. g (x n) - g (y n)" 0] by auto
  3741     hence "((\<lambda>n. f (x n) + g (x n) - (f (y n) + g (y n))) ---> 0) sequentially" unfolding Lim_sequentially and add_diff_add [symmetric] by auto  }
  3742   thus ?thesis using assms unfolding uniformly_continuous_on_sequentially'
  3743     unfolding dist_norm Lim_null_norm [symmetric] by auto
  3744 qed
  3745 
  3746 lemma uniformly_continuous_on_sub:
  3747   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  3748   shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s g
  3749            ==> uniformly_continuous_on s  (\<lambda>x. f x - g x)"
  3750   unfolding ab_diff_minus
  3751   using uniformly_continuous_on_add[of s f "\<lambda>x. - g x"]
  3752   using uniformly_continuous_on_neg[of s g] by auto
  3753 
  3754 text{* Identity function is continuous in every sense. *}
  3755 
  3756 lemma continuous_within_id:
  3757  "continuous (at a within s) (\<lambda>x. x)"
  3758   unfolding continuous_within by (rule Lim_at_within [OF Lim_ident_at])
  3759 
  3760 lemma continuous_at_id:
  3761  "continuous (at a) (\<lambda>x. x)"
  3762   unfolding continuous_at by (rule Lim_ident_at)
  3763 
  3764 lemma continuous_on_id:
  3765  "continuous_on s (\<lambda>x. x)"
  3766   unfolding continuous_on_def by (auto intro: tendsto_ident_at_within)
  3767 
  3768 lemma uniformly_continuous_on_id:
  3769  "uniformly_continuous_on s (\<lambda>x. x)"
  3770   unfolding uniformly_continuous_on_def by auto
  3771 
  3772 text{* Continuity of all kinds is preserved under composition. *}
  3773 
  3774 lemma continuous_within_topological:
  3775   "continuous (at x within s) f \<longleftrightarrow>
  3776     (\<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>
  3777       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
  3778 unfolding continuous_within
  3779 unfolding tendsto_def Limits.eventually_within eventually_at_topological
  3780 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto
  3781 
  3782 lemma continuous_within_compose:
  3783   assumes "continuous (at x within s) f"
  3784   assumes "continuous (at (f x) within f ` s) g"
  3785   shows "continuous (at x within s) (g o f)"
  3786 using assms unfolding continuous_within_topological by simp metis
  3787 
  3788 lemma continuous_at_compose:
  3789   assumes "continuous (at x) f"  "continuous (at (f x)) g"
  3790   shows "continuous (at x) (g o f)"
  3791 proof-
  3792   have " continuous (at (f x) within range f) g" using assms(2) using continuous_within_subset[of "f x" UNIV g "range f", unfolded within_UNIV] by auto
  3793   thus ?thesis using assms(1) using continuous_within_compose[of x UNIV f g, unfolded within_UNIV] by auto
  3794 qed
  3795 
  3796 lemma continuous_on_compose:
  3797   "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g o f)"
  3798   unfolding continuous_on_topological by simp metis
  3799 
  3800 lemma uniformly_continuous_on_compose:
  3801   assumes "uniformly_continuous_on s f"  "uniformly_continuous_on (f ` s) g"
  3802   shows "uniformly_continuous_on s (g o f)"
  3803 proof-
  3804   { fix e::real assume "e>0"
  3805     then obtain d where "d>0" and d:"\<forall>x\<in>f ` s. \<forall>x'\<in>f ` s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e" using assms(2) unfolding uniformly_continuous_on_def by auto
  3806     obtain d' where "d'>0" "\<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d' \<longrightarrow> dist (f x') (f x) < d" using `d>0` using assms(1) unfolding uniformly_continuous_on_def by auto
  3807     hence "\<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist ((g \<circ> f) x') ((g \<circ> f) x) < e" using `d>0` using d by auto  }
  3808   thus ?thesis using assms unfolding uniformly_continuous_on_def by auto
  3809 qed
  3810 
  3811 text{* Continuity in terms of open preimages. *}
  3812 
  3813 lemma continuous_at_open:
  3814   shows "continuous (at x) f \<longleftrightarrow> (\<forall>t. open t \<and> f x \<in> t --> (\<exists>s. open s \<and> x \<in> s \<and> (\<forall>x' \<in> s. (f x') \<in> t)))"
  3815 unfolding continuous_within_topological [of x UNIV f, unfolded within_UNIV]
  3816 unfolding imp_conjL by (intro all_cong imp_cong ex_cong conj_cong refl) auto
  3817 
  3818 lemma continuous_on_open:
  3819   shows "continuous_on s f \<longleftrightarrow>
  3820         (\<forall>t. openin (subtopology euclidean (f ` s)) t
  3821             --> openin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")
  3822 proof (safe)
  3823   fix t :: "'b set"
  3824   assume 1: "continuous_on s f"
  3825   assume 2: "openin (subtopology euclidean (f ` s)) t"
  3826   from 2 obtain B where B: "open B" and t: "t = f ` s \<inter> B"
  3827     unfolding openin_open by auto
  3828   def U == "\<Union>{A. open A \<and> (\<forall>x\<in>s. x \<in> A \<longrightarrow> f x \<in> B)}"
  3829   have "open U" unfolding U_def by (simp add: open_Union)
  3830   moreover have "\<forall>x\<in>s. x \<in> U \<longleftrightarrow> f x \<in> t"
  3831   proof (intro ballI iffI)
  3832     fix x assume "x \<in> s" and "x \<in> U" thus "f x \<in> t"
  3833       unfolding U_def t by auto
  3834   next
  3835     fix x assume "x \<in> s" and "f x \<in> t"
  3836     hence "x \<in> s" and "f x \<in> B"
  3837       unfolding t by auto
  3838     with 1 B obtain A where "open A" "x \<in> A" "\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B"
  3839       unfolding t continuous_on_topological by metis
  3840     then show "x \<in> U"
  3841       unfolding U_def by auto
  3842   qed
  3843   ultimately have "open U \<and> {x \<in> s. f x \<in> t} = s \<inter> U" by auto
  3844   then show "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
  3845     unfolding openin_open by fast
  3846 next
  3847   assume "?rhs" show "continuous_on s f"
  3848   unfolding continuous_on_topological
  3849   proof (clarify)
  3850     fix x and B assume "x \<in> s" and "open B" and "f x \<in> B"
  3851     have "openin (subtopology euclidean (f ` s)) (f ` s \<inter> B)"
  3852       unfolding openin_open using `open B` by auto
  3853     then have "openin (subtopology euclidean s) {x \<in> s. f x \<in> f ` s \<inter> B}"
  3854       using `?rhs` by fast
  3855     then show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"
  3856       unfolding openin_open using `x \<in> s` and `f x \<in> B` by auto
  3857   qed
  3858 qed
  3859 
  3860 text {* Similarly in terms of closed sets. *}
  3861 
  3862 lemma continuous_on_closed:
  3863   shows "continuous_on s f \<longleftrightarrow>  (\<forall>t. closedin (subtopology euclidean (f ` s)) t  --> closedin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")
  3864 proof
  3865   assume ?lhs
  3866   { fix t
  3867     have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
  3868     have **:"f ` s - (f ` s - (f ` s - t)) = f ` s - t" by auto
  3869     assume as:"closedin (subtopology euclidean (f ` s)) t"
  3870     hence "closedin (subtopology euclidean (f ` s)) (f ` s - (f ` s - t))" unfolding closedin_def topspace_euclidean_subtopology unfolding ** by auto
  3871     hence "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using `?lhs`[unfolded continuous_on_open, THEN spec[where x="(f ` s) - t"]]
  3872       unfolding openin_closedin_eq topspace_euclidean_subtopology unfolding * by auto  }
  3873   thus ?rhs by auto
  3874 next
  3875   assume ?rhs
  3876   { fix t
  3877     have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
  3878     assume as:"openin (subtopology euclidean (f ` s)) t"
  3879     hence "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using `?rhs`[THEN spec[where x="(f ` s) - t"]]
  3880       unfolding openin_closedin_eq topspace_euclidean_subtopology *[THEN sym] closedin_subtopology by auto }
  3881   thus ?lhs unfolding continuous_on_open by auto
  3882 qed
  3883 
  3884 text{* Half-global and completely global cases.                                  *}
  3885 
  3886 lemma continuous_open_in_preimage:
  3887   assumes "continuous_on s f"  "open t"
  3888   shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
  3889 proof-
  3890   have *:"\<forall>x. x \<in> s \<and> f x \<in> t \<longleftrightarrow> x \<in> s \<and> f x \<in> (t \<inter> f ` s)" by auto
  3891   have "openin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
  3892     using openin_open_Int[of t "f ` s", OF assms(2)] unfolding openin_open by auto
  3893   thus ?thesis using assms(1)[unfolded continuous_on_open, THEN spec[where x="t \<inter> f ` s"]] using * by auto
  3894 qed
  3895 
  3896 lemma continuous_closed_in_preimage:
  3897   assumes "continuous_on s f"  "closed t"
  3898   shows "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
  3899 proof-
  3900   have *:"\<forall>x. x \<in> s \<and> f x \<in> t \<longleftrightarrow> x \<in> s \<and> f x \<in> (t \<inter> f ` s)" by auto
  3901   have "closedin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
  3902     using closedin_closed_Int[of t "f ` s", OF assms(2)] unfolding Int_commute by auto
  3903   thus ?thesis
  3904     using assms(1)[unfolded continuous_on_closed, THEN spec[where x="t \<inter> f ` s"]] using * by auto
  3905 qed
  3906 
  3907 lemma continuous_open_preimage:
  3908   assumes "continuous_on s f" "open s" "open t"
  3909   shows "open {x \<in> s. f x \<in> t}"
  3910 proof-
  3911   obtain T where T: "open T" "{x \<in> s. f x \<in> t} = s \<inter> T"
  3912     using continuous_open_in_preimage[OF assms(1,3)] unfolding openin_open by auto
  3913   thus ?thesis using open_Int[of s T, OF assms(2)] by auto
  3914 qed
  3915 
  3916 lemma continuous_closed_preimage:
  3917   assumes "continuous_on s f" "closed s" "closed t"
  3918   shows "closed {x \<in> s. f x \<in> t}"
  3919 proof-
  3920   obtain T where T: "closed T" "{x \<in> s. f x \<in> t} = s \<inter> T"
  3921     using continuous_closed_in_preimage[OF assms(1,3)] unfolding closedin_closed by auto
  3922   thus ?thesis using closed_Int[of s T, OF assms(2)] by auto
  3923 qed
  3924 
  3925 lemma continuous_open_preimage_univ:
  3926   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open {x. f x \<in> s}"
  3927   using continuous_open_preimage[of UNIV f s] open_UNIV continuous_at_imp_continuous_on by auto
  3928 
  3929 lemma continuous_closed_preimage_univ:
  3930   shows "(\<forall>x. continuous (at x) f) \<Longrightarrow> closed s ==> closed {x. f x \<in> s}"
  3931   using continuous_closed_preimage[of UNIV f s] closed_UNIV continuous_at_imp_continuous_on by auto
  3932 
  3933 lemma continuous_open_vimage:
  3934   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open (f -` s)"
  3935   unfolding vimage_def by (rule continuous_open_preimage_univ)
  3936 
  3937 lemma continuous_closed_vimage:
  3938   shows "\<forall>x. continuous (at x) f \<Longrightarrow> closed s \<Longrightarrow> closed (f -` s)"
  3939   unfolding vimage_def by (rule continuous_closed_preimage_univ)
  3940 
  3941 lemma interior_image_subset:
  3942   assumes "\<forall>x. continuous (at x) f" "inj f"
  3943   shows "interior (f ` s) \<subseteq> f ` (interior s)"
  3944   apply rule unfolding interior_def mem_Collect_eq image_iff apply safe
  3945 proof- fix x T assume as:"open T" "x \<in> T" "T \<subseteq> f ` s" 
  3946   hence "x \<in> f ` s" by auto then guess y unfolding image_iff .. note y=this
  3947   thus "\<exists>xa\<in>{x. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> s}. x = f xa" apply(rule_tac x=y in bexI) using assms as
  3948     apply safe apply(rule_tac x="{x. f x \<in> T}" in exI) apply(safe,rule continuous_open_preimage_univ)
  3949   proof- fix x assume "f x \<in> T" hence "f x \<in> f ` s" using as by auto
  3950     thus "x \<in> s" unfolding inj_image_mem_iff[OF assms(2)] . qed auto qed
  3951 
  3952 text{* Equality of continuous functions on closure and related results.          *}
  3953 
  3954 lemma continuous_closed_in_preimage_constant:
  3955   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  3956   shows "continuous_on s f ==> closedin (subtopology euclidean s) {x \<in> s. f x = a}"
  3957   using continuous_closed_in_preimage[of s f "{a}"] by auto
  3958 
  3959 lemma continuous_closed_preimage_constant:
  3960   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  3961   shows "continuous_on s f \<Longrightarrow> closed s ==> closed {x \<in> s. f x = a}"
  3962   using continuous_closed_preimage[of s f "{a}"] by auto
  3963 
  3964 lemma continuous_constant_on_closure:
  3965   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  3966   assumes "continuous_on (closure s) f"
  3967           "\<forall>x \<in> s. f x = a"
  3968   shows "\<forall>x \<in> (closure s). f x = a"
  3969     using continuous_closed_preimage_constant[of "closure s" f a]
  3970     assms closure_minimal[of s "{x \<in> closure s. f x = a}"] closure_subset unfolding subset_eq by auto
  3971 
  3972 lemma image_closure_subset:
  3973   assumes "continuous_on (closure s) f"  "closed t"  "(f ` s) \<subseteq> t"
  3974   shows "f ` (closure s) \<subseteq> t"
  3975 proof-
  3976   have "s \<subseteq> {x \<in> closure s. f x \<in> t}" using assms(3) closure_subset by auto
  3977   moreover have "closed {x \<in> closure s. f x \<in> t}"
  3978     using continuous_closed_preimage[OF assms(1)] and assms(2) by auto
  3979   ultimately have "closure s = {x \<in> closure s . f x \<in> t}"
  3980     using closure_minimal[of s "{x \<in> closure s. f x \<in> t}"] by auto
  3981   thus ?thesis by auto
  3982 qed
  3983 
  3984 lemma continuous_on_closure_norm_le:
  3985   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  3986   assumes "continuous_on (closure s) f"  "\<forall>y \<in> s. norm(f y) \<le> b"  "x \<in> (closure s)"
  3987   shows "norm(f x) \<le> b"
  3988 proof-
  3989   have *:"f ` s \<subseteq> cball 0 b" using assms(2)[unfolded mem_cball_0[THEN sym]] by auto
  3990   show ?thesis
  3991     using image_closure_subset[OF assms(1) closed_cball[of 0 b] *] assms(3)
  3992     unfolding subset_eq apply(erule_tac x="f x" in ballE) by (auto simp add: dist_norm)
  3993 qed
  3994 
  3995 text{* Making a continuous function avoid some value in a neighbourhood.         *}
  3996 
  3997 lemma continuous_within_avoid:
  3998   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  3999   assumes "continuous (at x within s) f"  "x \<in> s"  "f x \<noteq> a"
  4000   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"
  4001 proof-
  4002   obtain d where "d>0" and d:"\<forall>xa\<in>s. 0 < dist xa x \<and> dist xa x < d \<longrightarrow> dist (f xa) (f x) < dist (f x) a"
  4003     using assms(1)[unfolded continuous_within Lim_within, THEN spec[where x="dist (f x) a"]] assms(3)[unfolded dist_nz] by auto
  4004   { fix y assume " y\<in>s"  "dist x y < d"
  4005     hence "f y \<noteq> a" using d[THEN bspec[where x=y]] assms(3)[unfolded dist_nz]
  4006       apply auto unfolding dist_nz[THEN sym] by (auto simp add: dist_commute) }
  4007   thus ?thesis using `d>0` by auto
  4008 qed
  4009 
  4010 lemma continuous_at_avoid:
  4011   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* FIXME: generalize *)
  4012   assumes "continuous (at x) f"  "f x \<noteq> a"
  4013   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
  4014 using assms using continuous_within_avoid[of x UNIV f a, unfolded within_UNIV] by auto
  4015 
  4016 lemma continuous_on_avoid:
  4017   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* TODO: generalize *)
  4018   assumes "continuous_on s f"  "x \<in> s"  "f x \<noteq> a"
  4019   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"
  4020 using assms(1)[unfolded continuous_on_eq_continuous_within, THEN bspec[where x=x], OF assms(2)]  continuous_within_avoid[of x s f a]  assms(2,3) by auto
  4021 
  4022 lemma continuous_on_open_avoid:
  4023   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space" (* TODO: generalize *)
  4024   assumes "continuous_on s f"  "open s"  "x \<in> s"  "f x \<noteq> a"
  4025   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
  4026 using assms(1)[unfolded continuous_on_eq_continuous_at[OF assms(2)], THEN bspec[where x=x], OF assms(3)]  continuous_at_avoid[of x f a]  assms(3,4) by auto
  4027 
  4028 text{* Proving a function is constant by proving open-ness of level set.         *}
  4029 
  4030 lemma continuous_levelset_open_in_cases:
  4031   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  4032   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
  4033         openin (subtopology euclidean s) {x \<in> s. f x = a}
  4034         ==> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"
  4035 unfolding connected_clopen using continuous_closed_in_preimage_constant by auto
  4036 
  4037 lemma continuous_levelset_open_in:
  4038   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  4039   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
  4040         openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>
  4041         (\<exists>x \<in> s. f x = a)  ==> (\<forall>x \<in> s. f x = a)"
  4042 using continuous_levelset_open_in_cases[of s f ]
  4043 by meson
  4044 
  4045 lemma continuous_levelset_open:
  4046   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  4047   assumes "connected s"  "continuous_on s f"  "open {x \<in> s. f x = a}"  "\<exists>x \<in> s.  f x = a"
  4048   shows "\<forall>x \<in> s. f x = a"
  4049 using continuous_levelset_open_in[OF assms(1,2), of a, unfolded openin_open] using assms (3,4) by fast
  4050 
  4051 text{* Some arithmetical combinations (more to prove).                           *}
  4052 
  4053 lemma open_scaling[intro]:
  4054   fixes s :: "'a::real_normed_vector set"
  4055   assumes "c \<noteq> 0"  "open s"
  4056   shows "open((\<lambda>x. c *\<^sub>R x) ` s)"
  4057 proof-
  4058   { fix x assume "x \<in> s"
  4059     then obtain e where "e>0" and e:"\<forall>x'. dist x' x < e \<longrightarrow> x' \<in> s" using assms(2)[unfolded open_dist, THEN bspec[where x=x]] by auto
  4060     have "e * abs c > 0" using assms(1)[unfolded zero_less_abs_iff[THEN sym]] using mult_pos_pos[OF `e>0`] by auto
  4061     moreover
  4062     { fix y assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"
  4063       hence "norm ((1 / c) *\<^sub>R y - x) < e" unfolding dist_norm
  4064         using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)
  4065           assms(1)[unfolded zero_less_abs_iff[THEN sym]] by (simp del:zero_less_abs_iff)
  4066       hence "y \<in> op *\<^sub>R c ` s" using rev_image_eqI[of "(1 / c) *\<^sub>R y" s y "op *\<^sub>R c"]  e[THEN spec[where x="(1 / c) *\<^sub>R y"]]  assms(1) unfolding dist_norm scaleR_scaleR by auto  }
  4067     ultimately have "\<exists>e>0. \<forall>x'. dist x' (c *\<^sub>R x) < e \<longrightarrow> x' \<in> op *\<^sub>R c ` s" apply(rule_tac x="e * abs c" in exI) by auto  }
  4068   thus ?thesis unfolding open_dist by auto
  4069 qed
  4070 
  4071 lemma minus_image_eq_vimage:
  4072   fixes A :: "'a::ab_group_add set"
  4073   shows "(\<lambda>x. - x) ` A = (\<lambda>x. - x) -` A"
  4074   by (auto intro!: image_eqI [where f="\<lambda>x. - x"])
  4075 
  4076 lemma open_negations:
  4077   fixes s :: "'a::real_normed_vector set"
  4078   shows "open s ==> open ((\<lambda> x. -x) ` s)"
  4079   unfolding scaleR_minus1_left [symmetric]
  4080   by (rule open_scaling, auto)
  4081 
  4082 lemma open_translation:
  4083   fixes s :: "'a::real_normed_vector set"
  4084   assumes "open s"  shows "open((\<lambda>x. a + x) ` s)"
  4085 proof-
  4086   { fix x have "continuous (at x) (\<lambda>x. x - a)" using continuous_sub[of "at x" "\<lambda>x. x" "\<lambda>x. a"] continuous_at_id[of x] continuous_const[of "at x" a] by auto  }
  4087   moreover have "{x. x - a \<in> s}  = op + a ` s" apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto
  4088   ultimately show ?thesis using continuous_open_preimage_univ[of "\<lambda>x. x - a" s] using assms by auto
  4089 qed
  4090 
  4091 lemma open_affinity:
  4092   fixes s :: "'a::real_normed_vector set"
  4093   assumes "open s"  "c \<noteq> 0"
  4094   shows "open ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  4095 proof-
  4096   have *:"(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)" unfolding o_def ..
  4097   have "op + a ` op *\<^sub>R c ` s = (op + a \<circ> op *\<^sub>R c) ` s" by auto
  4098   thus ?thesis using assms open_translation[of "op *\<^sub>R c ` s" a] unfolding * by auto
  4099 qed
  4100 
  4101 lemma interior_translation:
  4102   fixes s :: "'a::real_normed_vector set"
  4103   shows "interior ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (interior s)"
  4104 proof (rule set_eqI, rule)
  4105   fix x assume "x \<in> interior (op + a ` s)"
  4106   then obtain e where "e>0" and e:"ball x e \<subseteq> op + a ` s" unfolding mem_interior by auto
  4107   hence "ball (x - a) e \<subseteq> s" unfolding subset_eq Ball_def mem_ball dist_norm apply auto apply(erule_tac x="a + xa" in allE) unfolding ab_group_add_class.diff_diff_eq[THEN sym] by auto
  4108   thus "x \<in> op + a ` interior s" unfolding image_iff apply(rule_tac x="x - a" in bexI) unfolding mem_interior using `e > 0` by auto
  4109 next
  4110   fix x assume "x \<in> op + a ` interior s"
  4111   then obtain y e where "e>0" and e:"ball y e \<subseteq> s" and y:"x = a + y" unfolding image_iff Bex_def mem_interior by auto
  4112   { fix z have *:"a + y - z = y + a - z" by auto
  4113     assume "z\<in>ball x e"
  4114     hence "z - a \<in> s" using e[unfolded subset_eq, THEN bspec[where x="z - a"]] unfolding mem_ball dist_norm y ab_group_add_class.diff_diff_eq2 * by auto
  4115     hence "z \<in> op + a ` s" unfolding image_iff by(auto intro!: bexI[where x="z - a"])  }
  4116   hence "ball x e \<subseteq> op + a ` s" unfolding subset_eq by auto
  4117   thus "x \<in> interior (op + a ` s)" unfolding mem_interior using `e>0` by auto
  4118 qed
  4119 
  4120 text {* We can now extend limit compositions to consider the scalar multiplier.   *}
  4121 
  4122 lemma continuous_vmul:
  4123   fixes c :: "'a::metric_space \<Rightarrow> real" and v :: "'b::real_normed_vector"
  4124   shows "continuous net c ==> continuous net (\<lambda>x. c(x) *\<^sub>R v)"
  4125   unfolding continuous_def using Lim_vmul[of c] by auto
  4126 
  4127 lemma continuous_mul:
  4128   fixes c :: "'a::metric_space \<Rightarrow> real"
  4129   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  4130   shows "continuous net c \<Longrightarrow> continuous net f
  4131              ==> continuous net (\<lambda>x. c(x) *\<^sub>R f x) "
  4132   unfolding continuous_def by (intro tendsto_intros)
  4133 
  4134 lemmas continuous_intros = continuous_add continuous_vmul continuous_cmul
  4135   continuous_const continuous_sub continuous_at_id continuous_within_id continuous_mul
  4136 
  4137 lemma continuous_on_vmul:
  4138   fixes c :: "'a::metric_space \<Rightarrow> real" and v :: "'b::real_normed_vector"
  4139   shows "continuous_on s c ==> continuous_on s (\<lambda>x. c(x) *\<^sub>R v)"
  4140   unfolding continuous_on_eq_continuous_within using continuous_vmul[of _ c] by auto
  4141 
  4142 lemma continuous_on_mul:
  4143   fixes c :: "'a::metric_space \<Rightarrow> real"
  4144   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  4145   shows "continuous_on s c \<Longrightarrow> continuous_on s f
  4146              ==> continuous_on s (\<lambda>x. c(x) *\<^sub>R f x)"
  4147   unfolding continuous_on_eq_continuous_within using continuous_mul[of _ c] by auto
  4148 
  4149 lemma continuous_on_mul_real:
  4150   fixes f :: "'a::metric_space \<Rightarrow> real"
  4151   fixes g :: "'a::metric_space \<Rightarrow> real"
  4152   shows "continuous_on s f \<Longrightarrow> continuous_on s g
  4153              ==> continuous_on s (\<lambda>x. f x * g x)"
  4154   using continuous_on_mul[of s f g] unfolding real_scaleR_def .
  4155 
  4156 lemmas continuous_on_intros = continuous_on_add continuous_on_const
  4157   continuous_on_id continuous_on_compose continuous_on_cmul continuous_on_neg
  4158   continuous_on_sub continuous_on_mul continuous_on_vmul continuous_on_mul_real
  4159   uniformly_continuous_on_add uniformly_continuous_on_const
  4160   uniformly_continuous_on_id uniformly_continuous_on_compose
  4161   uniformly_continuous_on_cmul uniformly_continuous_on_neg
  4162   uniformly_continuous_on_sub
  4163 
  4164 text{* And so we have continuity of inverse.                                     *}
  4165 
  4166 lemma continuous_inv:
  4167   fixes f :: "'a::metric_space \<Rightarrow> real"
  4168   shows "continuous net f \<Longrightarrow> f(netlimit net) \<noteq> 0
  4169            ==> continuous net (inverse o f)"
  4170   unfolding continuous_def using Lim_inv by auto
  4171 
  4172 lemma continuous_at_within_inv:
  4173   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_field"
  4174   assumes "continuous (at a within s) f" "f a \<noteq> 0"
  4175   shows "continuous (at a within s) (inverse o f)"
  4176   using assms unfolding continuous_within o_def
  4177   by (intro tendsto_intros)
  4178 
  4179 lemma continuous_at_inv:
  4180   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_field"
  4181   shows "continuous (at a) f \<Longrightarrow> f a \<noteq> 0
  4182          ==> continuous (at a) (inverse o f) "
  4183   using within_UNIV[THEN sym, of "at a"] using continuous_at_within_inv[of a UNIV] by auto
  4184 
  4185 text {* Topological properties of linear functions. *}
  4186 
  4187 lemma linear_lim_0:
  4188   assumes "bounded_linear f" shows "(f ---> 0) (at (0))"
  4189 proof-
  4190   interpret f: bounded_linear f by fact
  4191   have "(f ---> f 0) (at 0)"
  4192     using tendsto_ident_at by (rule f.tendsto)
  4193   thus ?thesis unfolding f.zero .
  4194 qed
  4195 
  4196 lemma linear_continuous_at:
  4197   assumes "bounded_linear f"  shows "continuous (at a) f"
  4198   unfolding continuous_at using assms
  4199   apply (rule bounded_linear.tendsto)
  4200   apply (rule tendsto_ident_at)
  4201   done
  4202 
  4203 lemma linear_continuous_within:
  4204   shows "bounded_linear f ==> continuous (at x within s) f"
  4205   using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto
  4206 
  4207 lemma linear_continuous_on:
  4208   shows "bounded_linear f ==> continuous_on s f"
  4209   using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto
  4210 
  4211 text{* Also bilinear functions, in composition form.                             *}
  4212 
  4213 lemma bilinear_continuous_at_compose:
  4214   shows "continuous (at x) f \<Longrightarrow> continuous (at x) g \<Longrightarrow> bounded_bilinear h
  4215         ==> continuous (at x) (\<lambda>x. h (f x) (g x))"
  4216   unfolding continuous_at using Lim_bilinear[of f "f x" "(at x)" g "g x" h] by auto
  4217 
  4218 lemma bilinear_continuous_within_compose:
  4219   shows "continuous (at x within s) f \<Longrightarrow> continuous (at x within s) g \<Longrightarrow> bounded_bilinear h
  4220         ==> continuous (at x within s) (\<lambda>x. h (f x) (g x))"
  4221   unfolding continuous_within using Lim_bilinear[of f "f x"] by auto
  4222 
  4223 lemma bilinear_continuous_on_compose:
  4224   shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> bounded_bilinear h
  4225              ==> continuous_on s (\<lambda>x. h (f x) (g x))"
  4226   unfolding continuous_on_def
  4227   by (fast elim: bounded_bilinear.tendsto)
  4228 
  4229 text {* Preservation of compactness and connectedness under continuous function.  *}
  4230 
  4231 lemma compact_continuous_image:
  4232   assumes "continuous_on s f"  "compact s"
  4233   shows "compact(f ` s)"
  4234 proof-
  4235   { fix x assume x:"\<forall>n::nat. x n \<in> f ` s"
  4236     then obtain y where y:"\<forall>n. y n \<in> s \<and> x n = f (y n)" unfolding image_iff Bex_def using choice[of "\<lambda>n xa. xa \<in> s \<and> x n = f xa"] by auto
  4237     then obtain l r where "l\<in>s" and r:"subseq r" and lr:"((y \<circ> r) ---> l) sequentially" using assms(2)[unfolded compact_def, THEN spec[where x=y]] by auto
  4238     { fix e::real assume "e>0"
  4239       then obtain d where "d>0" and d:"\<forall>x'\<in>s. dist x' l < d \<longrightarrow> dist (f x') (f l) < e" using assms(1)[unfolded continuous_on_iff, THEN bspec[where x=l], OF `l\<in>s`] by auto
  4240       then obtain N::nat where N:"\<forall>n\<ge>N. dist ((y \<circ> r) n) l < d" using lr[unfolded Lim_sequentially, THEN spec[where x=d]] by auto
  4241       { fix n::nat assume "n\<ge>N" hence "dist ((x \<circ> r) n) (f l) < e" using N[THEN spec[where x=n]] d[THEN bspec[where x="y (r n)"]] y[THEN spec[where x="r n"]] by auto  }
  4242       hence "\<exists>N. \<forall>n\<ge>N. dist ((x \<circ> r) n) (f l) < e" by auto  }
  4243     hence "\<exists>l\<in>f ` s. \<exists>r. subseq r \<and> ((x \<circ> r) ---> l) sequentially" unfolding Lim_sequentially using r lr `l\<in>s` by auto  }
  4244   thus ?thesis unfolding compact_def by auto
  4245 qed
  4246 
  4247 lemma connected_continuous_image:
  4248   assumes "continuous_on s f"  "connected s"
  4249   shows "connected(f ` s)"
  4250 proof-
  4251   { fix T assume as: "T \<noteq> {}"  "T \<noteq> f ` s"  "openin (subtopology euclidean (f ` s)) T"  "closedin (subtopology euclidean (f ` s)) T"
  4252     have "{x \<in> s. f x \<in> T} = {} \<or> {x \<in> s. f x \<in> T} = s"
  4253       using assms(1)[unfolded continuous_on_open, THEN spec[where x=T]]
  4254       using assms(1)[unfolded continuous_on_closed, THEN spec[where x=T]]
  4255       using assms(2)[unfolded connected_clopen, THEN spec[where x="{x \<in> s. f x \<in> T}"]] as(3,4) by auto
  4256     hence False using as(1,2)
  4257       using as(4)[unfolded closedin_def topspace_euclidean_subtopology] by auto }
  4258   thus ?thesis unfolding connected_clopen by auto
  4259 qed
  4260 
  4261 text{* Continuity implies uniform continuity on a compact domain.                *}
  4262 
  4263 lemma compact_uniformly_continuous:
  4264   assumes "continuous_on s f"  "compact s"
  4265   shows "uniformly_continuous_on s f"
  4266 proof-
  4267     { fix x assume x:"x\<in>s"
  4268       hence "\<forall>xa. \<exists>y. 0 < xa \<longrightarrow> (y > 0 \<and> (\<forall>x'\<in>s. dist x' x < y \<longrightarrow> dist (f x') (f x) < xa))" using assms(1)[unfolded continuous_on_iff, THEN bspec[where x=x]] by auto
  4269       hence "\<exists>fa. \<forall>xa>0. \<forall>x'\<in>s. fa xa > 0 \<and> (dist x' x < fa xa \<longrightarrow> dist (f x') (f x) < xa)" using choice[of "\<lambda>e d. e>0 \<longrightarrow> d>0 \<and>(\<forall>x'\<in>s. (dist x' x < d \<longrightarrow> dist (f x') (f x) < e))"] by auto  }
  4270     then have "\<forall>x\<in>s. \<exists>y. \<forall>xa. 0 < xa \<longrightarrow> (\<forall>x'\<in>s. y xa > 0 \<and> (dist x' x < y xa \<longrightarrow> dist (f x') (f x) < xa))" by auto
  4271     then obtain d where d:"\<forall>e>0. \<forall>x\<in>s. \<forall>x'\<in>s. d x e > 0 \<and> (dist x' x < d x e \<longrightarrow> dist (f x') (f x) < e)"
  4272       using bchoice[of s "\<lambda>x fa. \<forall>xa>0. \<forall>x'\<in>s. fa xa > 0 \<and> (dist x' x < fa xa \<longrightarrow> dist (f x') (f x) < xa)"] by blast
  4273 
  4274   { fix e::real assume "e>0"
  4275 
  4276     { fix x assume "x\<in>s" hence "x \<in> ball x (d x (e / 2))" unfolding centre_in_ball using d[THEN spec[where x="e/2"]] using `e>0` by auto  }
  4277     hence "s \<subseteq> \<Union>{ball x (d x (e / 2)) |x. x \<in> s}" unfolding subset_eq by auto
  4278     moreover
  4279     { fix b assume "b\<in>{ball x (d x (e / 2)) |x. x \<in> s}" hence "open b" by auto  }
  4280     ultimately obtain ea where "ea>0" and ea:"\<forall>x\<in>s. \<exists>b\<in>{ball x (d x (e / 2)) |x. x \<in> s}. ball x ea \<subseteq> b" using heine_borel_lemma[OF assms(2), of "{ball x (d x (e / 2)) | x. x\<in>s }"] by auto
  4281 
  4282     { fix x y assume "x\<in>s" "y\<in>s" and as:"dist y x < ea"
  4283       obtain z where "z\<in>s" and z:"ball x ea \<subseteq> ball z (d z (e / 2))" using ea[THEN bspec[where x=x]] and `x\<in>s` by auto
  4284       hence "x\<in>ball z (d z (e / 2))" using `ea>0` unfolding subset_eq by auto
  4285       hence "dist (f z) (f x) < e / 2" using d[THEN spec[where x="e/2"]] and `e>0` and `x\<in>s` and `z\<in>s`
  4286         by (auto  simp add: dist_commute)
  4287       moreover have "y\<in>ball z (d z (e / 2))" using as and `ea>0` and z[unfolded subset_eq]
  4288         by (auto simp add: dist_commute)
  4289       hence "dist (f z) (f y) < e / 2" using d[THEN spec[where x="e/2"]] and `e>0` and `y\<in>s` and `z\<in>s`
  4290         by (auto  simp add: dist_commute)
  4291       ultimately have "dist (f y) (f x) < e" using dist_triangle_half_r[of "f z" "f x" e "f y"]
  4292         by (auto simp add: dist_commute)  }
  4293     then have "\<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e" using `ea>0` by auto  }
  4294   thus ?thesis unfolding uniformly_continuous_on_def by auto
  4295 qed
  4296 
  4297 text{* Continuity of inverse function on compact domain. *}
  4298 
  4299 lemma continuous_on_inverse:
  4300   fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
  4301     (* TODO: can this be generalized more? *)
  4302   assumes "continuous_on s f"  "compact s"  "\<forall>x \<in> s. g (f x) = x"
  4303   shows "continuous_on (f ` s) g"
  4304 proof-
  4305   have *:"g ` f ` s = s" using assms(3) by (auto simp add: image_iff)
  4306   { fix t assume t:"closedin (subtopology euclidean (g ` f ` s)) t"
  4307     then obtain T where T: "closed T" "t = s \<inter> T" unfolding closedin_closed unfolding * by auto
  4308     have "continuous_on (s \<inter> T) f" using continuous_on_subset[OF assms(1), of "s \<inter> t"]
  4309       unfolding T(2) and Int_left_absorb by auto
  4310     moreover have "compact (s \<inter> T)"
  4311       using assms(2) unfolding compact_eq_bounded_closed
  4312       using bounded_subset[of s "s \<inter> T"] and T(1) by auto
  4313     ultimately have "closed (f ` t)" using T(1) unfolding T(2)
  4314       using compact_continuous_image [of "s \<inter> T" f] unfolding compact_eq_bounded_closed by auto
  4315     moreover have "{x \<in> f ` s. g x \<in> t} = f ` s \<inter> f ` t" using assms(3) unfolding T(2) by auto
  4316     ultimately have "closedin (subtopology euclidean (f ` s)) {x \<in> f ` s. g x \<in> t}"
  4317       unfolding closedin_closed by auto  }
  4318   thus ?thesis unfolding continuous_on_closed by auto
  4319 qed
  4320 
  4321 text {* A uniformly convergent limit of continuous functions is continuous. *}
  4322 
  4323 lemma norm_triangle_lt:
  4324   fixes x y :: "'a::real_normed_vector"
  4325   shows "norm x + norm y < e \<Longrightarrow> norm (x + y) < e"
  4326 by (rule le_less_trans [OF norm_triangle_ineq])
  4327 
  4328 lemma continuous_uniform_limit:
  4329   fixes f :: "'a \<Rightarrow> 'b::metric_space \<Rightarrow> 'c::real_normed_vector"
  4330   assumes "\<not> (trivial_limit net)"  "eventually (\<lambda>n. continuous_on s (f n)) net"
  4331   "\<forall>e>0. eventually (\<lambda>n. \<forall>x \<in> s. norm(f n x - g x) < e) net"
  4332   shows "continuous_on s g"
  4333 proof-
  4334   { fix x and e::real assume "x\<in>s" "e>0"
  4335     have "eventually (\<lambda>n. \<forall>x\<in>s. norm (f n x - g x) < e / 3) net" using `e>0` assms(3)[THEN spec[where x="e/3"]] by auto
  4336     then obtain n where n:"\<forall>xa\<in>s. norm (f n xa - g xa) < e / 3"  "continuous_on s (f n)"
  4337       using eventually_and[of "(\<lambda>n. \<forall>x\<in>s. norm (f n x - g x) < e / 3)" "(\<lambda>n. continuous_on s (f n))" net] assms(1,2) eventually_happens by blast
  4338     have "e / 3 > 0" using `e>0` by auto
  4339     then obtain d where "d>0" and d:"\<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f n x') (f n x) < e / 3"
  4340       using n(2)[unfolded continuous_on_iff, THEN bspec[where x=x], OF `x\<in>s`, THEN spec[where x="e/3"]] by blast
  4341     { fix y assume "y\<in>s" "dist y x < d"
  4342       hence "dist (f n y) (f n x) < e / 3" using d[THEN bspec[where x=y]] by auto
  4343       hence "norm (f n y - g x) < 2 * e / 3" using norm_triangle_lt[of "f n y - f n x" "f n x - g x" "2*e/3"]
  4344         using n(1)[THEN bspec[where x=x], OF `x\<in>s`] unfolding dist_norm unfolding ab_group_add_class.ab_diff_minus by auto
  4345       hence "dist (g y) (g x) < e" unfolding dist_norm using n(1)[THEN bspec[where x=y], OF `y\<in>s`]
  4346         unfolding norm_minus_cancel[of "f n y - g y", THEN sym] using norm_triangle_lt[of "f n y - g x" "g y - f n y" e] by (auto simp add: uminus_add_conv_diff)  }
  4347     hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e" using `d>0` by auto  }
  4348   thus ?thesis unfolding continuous_on_iff by auto
  4349 qed
  4350 
  4351 subsection{* Topological stuff lifted from and dropped to R                            *}
  4352 
  4353 
  4354 lemma open_real:
  4355   fixes s :: "real set" shows
  4356  "open s \<longleftrightarrow>
  4357         (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. abs(x' - x) < e --> x' \<in> s)" (is "?lhs = ?rhs")
  4358   unfolding open_dist dist_norm by simp
  4359 
  4360 lemma islimpt_approachable_real:
  4361   fixes s :: "real set"
  4362   shows "x islimpt s \<longleftrightarrow> (\<forall>e>0.  \<exists>x'\<in> s. x' \<noteq> x \<and> abs(x' - x) < e)"
  4363   unfolding islimpt_approachable dist_norm by simp
  4364 
  4365 lemma closed_real:
  4366   fixes s :: "real set"
  4367   shows "closed s \<longleftrightarrow>
  4368         (\<forall>x. (\<forall>e>0.  \<exists>x' \<in> s. x' \<noteq> x \<and> abs(x' - x) < e)
  4369             --> x \<in> s)"
  4370   unfolding closed_limpt islimpt_approachable dist_norm by simp
  4371 
  4372 lemma continuous_at_real_range:
  4373   fixes f :: "'a::real_normed_vector \<Rightarrow> real"
  4374   shows "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
  4375         \<forall>x'. norm(x' - x) < d --> abs(f x' - f x) < e)"
  4376   unfolding continuous_at unfolding Lim_at
  4377   unfolding dist_nz[THEN sym] unfolding dist_norm apply auto
  4378   apply(erule_tac x=e in allE) apply auto apply (rule_tac x=d in exI) apply auto apply (erule_tac x=x' in allE) apply auto
  4379   apply(erule_tac x=e in allE) by auto
  4380 
  4381 lemma continuous_on_real_range:
  4382   fixes f :: "'a::real_normed_vector \<Rightarrow> real"
  4383   shows "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. \<forall>e>0. \<exists>d>0. (\<forall>x' \<in> s. norm(x' - x) < d --> abs(f x' - f x) < e))"
  4384   unfolding continuous_on_iff dist_norm by simp
  4385 
  4386 lemma continuous_at_norm: "continuous (at x) norm"
  4387   unfolding continuous_at by (intro tendsto_intros)
  4388 
  4389 lemma continuous_on_norm: "continuous_on s norm"
  4390 unfolding continuous_on by (intro ballI tendsto_intros)
  4391 
  4392 lemma continuous_at_infnorm: "continuous (at x) infnorm"
  4393   unfolding continuous_at Lim_at o_def unfolding dist_norm
  4394   apply auto apply (rule_tac x=e in exI) apply auto
  4395   using order_trans[OF real_abs_sub_infnorm infnorm_le_norm, of _ x] by (metis xt1(7))
  4396 
  4397 text{* Hence some handy theorems on distance, diameter etc. of/from a set.       *}
  4398 
  4399 lemma compact_attains_sup:
  4400   fixes s :: "real set"
  4401   assumes "compact s"  "s \<noteq> {}"
  4402   shows "\<exists>x \<in> s. \<forall>y \<in> s. y \<le> x"
  4403 proof-
  4404   from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
  4405   { fix e::real assume as: "\<forall>x\<in>s. x \<le> Sup s" "Sup s \<notin> s"  "0 < e" "\<forall>x'\<in>s. x' = Sup s \<or> \<not> Sup s - x' < e"
  4406     have "isLub UNIV s (Sup s)" using Sup[OF assms(2)] unfolding setle_def using as(1) by auto
  4407     moreover have "isUb UNIV s (Sup s - e)" unfolding isUb_def unfolding setle_def using as(4,2) by auto
  4408     ultimately have False using isLub_le_isUb[of UNIV s "Sup s" "Sup s - e"] using `e>0` by auto  }
  4409   thus ?thesis using bounded_has_Sup(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Sup s"]]
  4410     apply(rule_tac x="Sup s" in bexI) by auto
  4411 qed
  4412 
  4413 lemma Inf:
  4414   fixes S :: "real set"
  4415   shows "S \<noteq> {} ==> (\<exists>b. b <=* S) ==> isGlb UNIV S (Inf S)"
  4416 by (auto simp add: isLb_def setle_def setge_def isGlb_def greatestP_def) 
  4417 
  4418 lemma compact_attains_inf:
  4419   fixes s :: "real set"
  4420   assumes "compact s" "s \<noteq> {}"  shows "\<exists>x \<in> s. \<forall>y \<in> s. x \<le> y"
  4421 proof-
  4422   from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
  4423   { fix e::real assume as: "\<forall>x\<in>s. x \<ge> Inf s"  "Inf s \<notin> s"  "0 < e"
  4424       "\<forall>x'\<in>s. x' = Inf s \<or> \<not> abs (x' - Inf s) < e"
  4425     have "isGlb UNIV s (Inf s)" using Inf[OF assms(2)] unfolding setge_def using as(1) by auto
  4426     moreover
  4427     { fix x assume "x \<in> s"
  4428       hence *:"abs (x - Inf s) = x - Inf s" using as(1)[THEN bspec[where x=x]] by auto
  4429       have "Inf s + e \<le> x" using as(4)[THEN bspec[where x=x]] using as(2) `x\<in>s` unfolding * by auto }
  4430     hence "isLb UNIV s (Inf s + e)" unfolding isLb_def and setge_def by auto
  4431     ultimately have False using isGlb_le_isLb[of UNIV s "Inf s" "Inf s + e"] using `e>0` by auto  }
  4432   thus ?thesis using bounded_has_Inf(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Inf s"]]
  4433     apply(rule_tac x="Inf s" in bexI) by auto
  4434 qed
  4435 
  4436 lemma continuous_attains_sup:
  4437   fixes f :: "'a::metric_space \<Rightarrow> real"
  4438   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
  4439         ==> (\<exists>x \<in> s. \<forall>y \<in> s.  f y \<le> f x)"
  4440   using compact_attains_sup[of "f ` s"]
  4441   using compact_continuous_image[of s f] by auto
  4442 
  4443 lemma continuous_attains_inf:
  4444   fixes f :: "'a::metric_space \<Rightarrow> real"
  4445   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
  4446         \<Longrightarrow> (\<exists>x \<in> s. \<forall>y \<in> s. f x \<le> f y)"
  4447   using compact_attains_inf[of "f ` s"]
  4448   using compact_continuous_image[of s f] by auto
  4449 
  4450 lemma distance_attains_sup:
  4451   assumes "compact s" "s \<noteq> {}"
  4452   shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a y \<le> dist a x"
  4453 proof (rule continuous_attains_sup [OF assms])
  4454   { fix x assume "x\<in>s"
  4455     have "(dist a ---> dist a x) (at x within s)"
  4456       by (intro tendsto_dist tendsto_const Lim_at_within Lim_ident_at)
  4457   }
  4458   thus "continuous_on s (dist a)"
  4459     unfolding continuous_on ..
  4460 qed
  4461 
  4462 text{* For *minimal* distance, we only need closure, not compactness.            *}
  4463 
  4464 lemma distance_attains_inf:
  4465   fixes a :: "'a::heine_borel"
  4466   assumes "closed s"  "s \<noteq> {}"
  4467   shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a x \<le> dist a y"
  4468 proof-
  4469   from assms(2) obtain b where "b\<in>s" by auto
  4470   let ?B = "cball a (dist b a) \<inter> s"
  4471   have "b \<in> ?B" using `b\<in>s` by (simp add: dist_commute)
  4472   hence "?B \<noteq> {}" by auto
  4473   moreover
  4474   { fix x assume "x\<in>?B"
  4475     fix e::real assume "e>0"
  4476     { fix x' assume "x'\<in>?B" and as:"dist x' x < e"
  4477       from as have "\<bar>dist a x' - dist a x\<bar> < e"
  4478         unfolding abs_less_iff minus_diff_eq
  4479         using dist_triangle2 [of a x' x]
  4480         using dist_triangle [of a x x']
  4481         by arith
  4482     }
  4483     hence "\<exists>d>0. \<forall>x'\<in>?B. dist x' x < d \<longrightarrow> \<bar>dist a x' - dist a x\<bar> < e"
  4484       using `e>0` by auto
  4485   }
  4486   hence "continuous_on (cball a (dist b a) \<inter> s) (dist a)"
  4487     unfolding continuous_on Lim_within dist_norm real_norm_def
  4488     by fast
  4489   moreover have "compact ?B"
  4490     using compact_cball[of a "dist b a"]
  4491     unfolding compact_eq_bounded_closed
  4492     using bounded_Int and closed_Int and assms(1) by auto
  4493   ultimately obtain x where "x\<in>cball a (dist b a) \<inter> s" "\<forall>y\<in>cball a (dist b a) \<inter> s. dist a x \<le> dist a y"
  4494     using continuous_attains_inf[of ?B "dist a"] by fastsimp
  4495   thus ?thesis by fastsimp
  4496 qed
  4497 
  4498 subsection {* Pasted sets *}
  4499 
  4500 lemma bounded_Times:
  4501   assumes "bounded s" "bounded t" shows "bounded (s \<times> t)"
  4502 proof-
  4503   obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"
  4504     using assms [unfolded bounded_def] by auto
  4505   then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<twosuperior> + b\<twosuperior>)"
  4506     by (auto simp add: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)
  4507   thus ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto
  4508 qed
  4509 
  4510 lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"
  4511 by (induct x) simp
  4512 
  4513 lemma compact_Times: "compact s \<Longrightarrow> compact t \<Longrightarrow> compact (s \<times> t)"
  4514 unfolding compact_def
  4515 apply clarify
  4516 apply (drule_tac x="fst \<circ> f" in spec)
  4517 apply (drule mp, simp add: mem_Times_iff)
  4518 apply (clarify, rename_tac l1 r1)
  4519 apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)
  4520 apply (drule mp, simp add: mem_Times_iff)
  4521 apply (clarify, rename_tac l2 r2)
  4522 apply (rule_tac x="(l1, l2)" in rev_bexI, simp)
  4523 apply (rule_tac x="r1 \<circ> r2" in exI)
  4524 apply (rule conjI, simp add: subseq_def)
  4525 apply (drule_tac r=r2 in lim_subseq [COMP swap_prems_rl], assumption)
  4526 apply (drule (1) tendsto_Pair) back
  4527 apply (simp add: o_def)
  4528 done
  4529 
  4530 text{* Hence some useful properties follow quite easily.                         *}
  4531 
  4532 lemma compact_scaling:
  4533   fixes s :: "'a::real_normed_vector set"
  4534   assumes "compact s"  shows "compact ((\<lambda>x. c *\<^sub>R x) ` s)"
  4535 proof-
  4536   let ?f = "\<lambda>x. scaleR c x"
  4537   have *:"bounded_linear ?f" by (rule scaleR.bounded_linear_right)
  4538   show ?thesis using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]
  4539     using linear_continuous_at[OF *] assms by auto
  4540 qed
  4541 
  4542 lemma compact_negations:
  4543   fixes s :: "'a::real_normed_vector set"
  4544   assumes "compact s"  shows "compact ((\<lambda>x. -x) ` s)"
  4545   using compact_scaling [OF assms, of "- 1"] by auto
  4546 
  4547 lemma compact_sums:
  4548   fixes s t :: "'a::real_normed_vector set"
  4549   assumes "compact s"  "compact t"  shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"
  4550 proof-
  4551   have *:"{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z) ` (s \<times> t)"
  4552     apply auto unfolding image_iff apply(rule_tac x="(xa, y)" in bexI) by auto
  4553   have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"
  4554     unfolding continuous_on by (rule ballI) (intro tendsto_intros)
  4555   thus ?thesis unfolding * using compact_continuous_image compact_Times [OF assms] by auto
  4556 qed
  4557 
  4558 lemma compact_differences:
  4559   fixes s t :: "'a::real_normed_vector set"
  4560   assumes "compact s" "compact t"  shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"
  4561 proof-
  4562   have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus ` t)}"
  4563     apply auto apply(rule_tac x= xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
  4564   thus ?thesis using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto
  4565 qed
  4566 
  4567 lemma compact_translation:
  4568   fixes s :: "'a::real_normed_vector set"
  4569   assumes "compact s"  shows "compact ((\<lambda>x. a + x) ` s)"
  4570 proof-
  4571   have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x) ` s" by auto
  4572   thus ?thesis using compact_sums[OF assms compact_sing[of a]] by auto
  4573 qed
  4574 
  4575 lemma compact_affinity:
  4576   fixes s :: "'a::real_normed_vector set"
  4577   assumes "compact s"  shows "compact ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  4578 proof-
  4579   have "op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
  4580   thus ?thesis using compact_translation[OF compact_scaling[OF assms], of a c] by auto
  4581 qed
  4582 
  4583 text{* Hence we get the following.                                               *}
  4584 
  4585 lemma compact_sup_maxdistance:
  4586   fixes s :: "'a::real_normed_vector set"
  4587   assumes "compact s"  "s \<noteq> {}"
  4588   shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. norm(u - v) \<le> norm(x - y)"
  4589 proof-
  4590   have "{x - y | x y . x\<in>s \<and> y\<in>s} \<noteq> {}" using `s \<noteq> {}` by auto
  4591   then obtain x where x:"x\<in>{x - y |x y. x \<in> s \<and> y \<in> s}"  "\<forall>y\<in>{x - y |x y. x \<in> s \<and> y \<in> s}. norm y \<le> norm x"
  4592     using compact_differences[OF assms(1) assms(1)]
  4593     using distance_attains_sup[where 'a="'a", unfolded dist_norm, of "{x - y | x y . x\<in>s \<and> y\<in>s}" 0] by auto
  4594   from x(1) obtain a b where "a\<in>s" "b\<in>s" "x = a - b" by auto
  4595   thus ?thesis using x(2)[unfolded `x = a - b`] by blast
  4596 qed
  4597 
  4598 text{* We can state this in terms of diameter of a set.                          *}
  4599 
  4600 definition "diameter s = (if s = {} then 0::real else Sup {norm(x - y) | x y. x \<in> s \<and> y \<in> s})"
  4601   (* TODO: generalize to class metric_space *)
  4602 
  4603 lemma diameter_bounded:
  4604   assumes "bounded s"
  4605   shows "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
  4606         "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)"
  4607 proof-
  4608   let ?D = "{norm (x - y) |x y. x \<in> s \<and> y \<in> s}"
  4609   obtain a where a:"\<forall>x\<in>s. norm x \<le> a" using assms[unfolded bounded_iff] by auto
  4610   { fix x y assume "x \<in> s" "y \<in> s"
  4611     hence "norm (x - y) \<le> 2 * a" using norm_triangle_ineq[of x "-y", unfolded norm_minus_cancel] a[THEN bspec[where x=x]] a[THEN bspec[where x=y]] by (auto simp add: field_simps)  }
  4612   note * = this
  4613   { fix x y assume "x\<in>s" "y\<in>s"  hence "s \<noteq> {}" by auto
  4614     have "norm(x - y) \<le> diameter s" unfolding diameter_def using `s\<noteq>{}` *[OF `x\<in>s` `y\<in>s`] `x\<in>s` `y\<in>s`
  4615       by simp (blast intro!: Sup_upper *) }
  4616   moreover
  4617   { fix d::real assume "d>0" "d < diameter s"
  4618     hence "s\<noteq>{}" unfolding diameter_def by auto
  4619     have "\<exists>d' \<in> ?D. d' > d"
  4620     proof(rule ccontr)
  4621       assume "\<not> (\<exists>d'\<in>{norm (x - y) |x y. x \<in> s \<and> y \<in> s}. d < d')"
  4622       hence "\<forall>d'\<in>?D. d' \<le> d" by auto (metis not_leE) 
  4623       thus False using `d < diameter s` `s\<noteq>{}` 
  4624         apply (auto simp add: diameter_def) 
  4625         apply (drule Sup_real_iff [THEN [2] rev_iffD2])
  4626         apply (auto, force) 
  4627         done
  4628     qed
  4629     hence "\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d" by auto  }
  4630   ultimately show "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
  4631         "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)" by auto
  4632 qed
  4633 
  4634 lemma diameter_bounded_bound:
  4635  "bounded s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s ==> norm(x - y) \<le> diameter s"
  4636   using diameter_bounded by blast
  4637 
  4638 lemma diameter_compact_attained:
  4639   fixes s :: "'a::real_normed_vector set"
  4640   assumes "compact s"  "s \<noteq> {}"
  4641   shows "\<exists>x\<in>s. \<exists>y\<in>s. (norm(x - y) = diameter s)"
  4642 proof-
  4643   have b:"bounded s" using assms(1) by (rule compact_imp_bounded)
  4644   then obtain x y where xys:"x\<in>s" "y\<in>s" and xy:"\<forall>u\<in>s. \<forall>v\<in>s. norm (u - v) \<le> norm (x - y)" using compact_sup_maxdistance[OF assms] by auto
  4645   hence "diameter s \<le> norm (x - y)"
  4646     unfolding diameter_def by clarsimp (rule Sup_least, fast+)
  4647   thus ?thesis
  4648     by (metis b diameter_bounded_bound order_antisym xys)
  4649 qed
  4650 
  4651 text{* Related results with closure as the conclusion.                           *}
  4652 
  4653 lemma closed_scaling:
  4654   fixes s :: "'a::real_normed_vector set"
  4655   assumes "closed s" shows "closed ((\<lambda>x. c *\<^sub>R x) ` s)"
  4656 proof(cases "s={}")
  4657   case True thus ?thesis by auto
  4658 next
  4659   case False
  4660   show ?thesis
  4661   proof(cases "c=0")
  4662     have *:"(\<lambda>x. 0) ` s = {0}" using `s\<noteq>{}` by auto
  4663     case True thus ?thesis apply auto unfolding * by auto
  4664   next
  4665     case False
  4666     { fix x l assume as:"\<forall>n::nat. x n \<in> scaleR c ` s"  "(x ---> l) sequentially"
  4667       { fix n::nat have "scaleR (1 / c) (x n) \<in> s"
  4668           using as(1)[THEN spec[where x=n]]
  4669           using `c\<noteq>0` by auto
  4670       }
  4671       moreover
  4672       { fix e::real assume "e>0"
  4673         hence "0 < e *\<bar>c\<bar>"  using `c\<noteq>0` mult_pos_pos[of e "abs c"] by auto
  4674         then obtain N where "\<forall>n\<ge>N. dist (x n) l < e * \<bar>c\<bar>"
  4675           using as(2)[unfolded Lim_sequentially, THEN spec[where x="e * abs c"]] by auto
  4676         hence "\<exists>N. \<forall>n\<ge>N. dist (scaleR (1 / c) (x n)) (scaleR (1 / c) l) < e"
  4677           unfolding dist_norm unfolding scaleR_right_diff_distrib[THEN sym]
  4678           using mult_imp_div_pos_less[of "abs c" _ e] `c\<noteq>0` by auto  }
  4679       hence "((\<lambda>n. scaleR (1 / c) (x n)) ---> scaleR (1 / c) l) sequentially" unfolding Lim_sequentially by auto
  4680       ultimately have "l \<in> scaleR c ` s"
  4681         using assms[unfolded closed_sequential_limits, THEN spec[where x="\<lambda>n. scaleR (1/c) (x n)"], THEN spec[where x="scaleR (1/c) l"]]
  4682         unfolding image_iff using `c\<noteq>0` apply(rule_tac x="scaleR (1 / c) l" in bexI) by auto  }
  4683     thus ?thesis unfolding closed_sequential_limits by fast
  4684   qed
  4685 qed
  4686 
  4687 lemma closed_negations:
  4688   fixes s :: "'a::real_normed_vector set"
  4689   assumes "closed s"  shows "closed ((\<lambda>x. -x) ` s)"
  4690   using closed_scaling[OF assms, of "- 1"] by simp
  4691 
  4692 lemma compact_closed_sums:
  4693   fixes s :: "'a::real_normed_vector set"
  4694   assumes "compact s"  "closed t"  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
  4695 proof-
  4696   let ?S = "{x + y |x y. x \<in> s \<and> y \<in> t}"
  4697   { fix x l assume as:"\<forall>n. x n \<in> ?S"  "(x ---> l) sequentially"
  4698     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"
  4699       using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> s \<and> snd y \<in> t"] by auto
  4700     obtain l' r where "l'\<in>s" and r:"subseq r" and lr:"(((\<lambda>n. fst (f n)) \<circ> r) ---> l') sequentially"
  4701       using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto
  4702     have "((\<lambda>n. snd (f (r n))) ---> l - l') sequentially"
  4703       using Lim_sub[OF lim_subseq[OF r as(2)] lr] and f(1) unfolding o_def by auto
  4704     hence "l - l' \<in> t"
  4705       using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda> n. snd (f (r n))"], THEN spec[where x="l - l'"]]
  4706       using f(3) by auto
  4707     hence "l \<in> ?S" using `l' \<in> s` apply auto apply(rule_tac x=l' in exI) apply(rule_tac x="l - l'" in exI) by auto
  4708   }
  4709   thus ?thesis unfolding closed_sequential_limits by fast
  4710 qed
  4711 
  4712 lemma closed_compact_sums:
  4713   fixes s t :: "'a::real_normed_vector set"
  4714   assumes "closed s"  "compact t"
  4715   shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
  4716 proof-
  4717   have "{x + y |x y. x \<in> t \<and> y \<in> s} = {x + y |x y. x \<in> s \<and> y \<in> t}" apply auto
  4718     apply(rule_tac x=y in exI) apply auto apply(rule_tac x=y in exI) by auto
  4719   thus ?thesis using compact_closed_sums[OF assms(2,1)] by simp
  4720 qed
  4721 
  4722 lemma compact_closed_differences:
  4723   fixes s t :: "'a::real_normed_vector set"
  4724   assumes "compact s"  "closed t"
  4725   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
  4726 proof-
  4727   have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} =  {x - y |x y. x \<in> s \<and> y \<in> t}"
  4728     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
  4729   thus ?thesis using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto
  4730 qed
  4731 
  4732 lemma closed_compact_differences:
  4733   fixes s t :: "'a::real_normed_vector set"
  4734   assumes "closed s" "compact t"
  4735   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
  4736 proof-
  4737   have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} = {x - y |x y. x \<in> s \<and> y \<in> t}"
  4738     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
  4739  thus ?thesis using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp
  4740 qed
  4741 
  4742 lemma closed_translation:
  4743   fixes a :: "'a::real_normed_vector"
  4744   assumes "closed s"  shows "closed ((\<lambda>x. a + x) ` s)"
  4745 proof-
  4746   have "{a + y |y. y \<in> s} = (op + a ` s)" by auto
  4747   thus ?thesis using compact_closed_sums[OF compact_sing[of a] assms] by auto
  4748 qed
  4749 
  4750 lemma translation_Compl:
  4751   fixes a :: "'a::ab_group_add"
  4752   shows "(\<lambda>x. a + x) ` (- t) = - ((\<lambda>x. a + x) ` t)"
  4753   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in bexI) by auto
  4754 
  4755 lemma translation_UNIV:
  4756   fixes a :: "'a::ab_group_add" shows "range (\<lambda>x. a + x) = UNIV"
  4757   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in exI) by auto
  4758 
  4759 lemma translation_diff:
  4760   fixes a :: "'a::ab_group_add"
  4761   shows "(\<lambda>x. a + x) ` (s - t) = ((\<lambda>x. a + x) ` s) - ((\<lambda>x. a + x) ` t)"
  4762   by auto
  4763 
  4764 lemma closure_translation:
  4765   fixes a :: "'a::real_normed_vector"
  4766   shows "closure ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (closure s)"
  4767 proof-
  4768   have *:"op + a ` (- s) = - op + a ` s"
  4769     apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto
  4770   show ?thesis unfolding closure_interior translation_Compl
  4771     using interior_translation[of a "- s"] unfolding * by auto
  4772 qed
  4773 
  4774 lemma frontier_translation:
  4775   fixes a :: "'a::real_normed_vector"
  4776   shows "frontier((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (frontier s)"
  4777   unfolding frontier_def translation_diff interior_translation closure_translation by auto
  4778 
  4779 subsection{* Separation between points and sets.                                       *}
  4780 
  4781 lemma separate_point_closed:
  4782   fixes s :: "'a::heine_borel set"
  4783   shows "closed s \<Longrightarrow> a \<notin> s  ==> (\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x)"
  4784 proof(cases "s = {}")
  4785   case True
  4786   thus ?thesis by(auto intro!: exI[where x=1])
  4787 next
  4788   case False
  4789   assume "closed s" "a \<notin> s"
  4790   then obtain x where "x\<in>s" "\<forall>y\<in>s. dist a x \<le> dist a y" using `s \<noteq> {}` distance_attains_inf [of s a] by blast
  4791   with `x\<in>s` show ?thesis using dist_pos_lt[of a x] and`a \<notin> s` by blast
  4792 qed
  4793 
  4794 lemma separate_compact_closed:
  4795   fixes s t :: "'a::{heine_borel, real_normed_vector} set"
  4796     (* TODO: does this generalize to heine_borel? *)
  4797   assumes "compact s" and "closed t" and "s \<inter> t = {}"
  4798   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
  4799 proof-
  4800   have "0 \<notin> {x - y |x y. x \<in> s \<and> y \<in> t}" using assms(3) by auto
  4801   then obtain d where "d>0" and d:"\<forall>x\<in>{x - y |x y. x \<in> s \<and> y \<in> t}. d \<le> dist 0 x"
  4802     using separate_point_closed[OF compact_closed_differences[OF assms(1,2)], of 0] by auto
  4803   { fix x y assume "x\<in>s" "y\<in>t"
  4804     hence "x - y \<in> {x - y |x y. x \<in> s \<and> y \<in> t}" by auto
  4805     hence "d \<le> dist (x - y) 0" using d[THEN bspec[where x="x - y"]] using dist_commute
  4806       by (auto  simp add: dist_commute)
  4807     hence "d \<le> dist x y" unfolding dist_norm by auto  }
  4808   thus ?thesis using `d>0` by auto
  4809 qed
  4810 
  4811 lemma separate_closed_compact:
  4812   fixes s t :: "'a::{heine_borel, real_normed_vector} set"
  4813   assumes "closed s" and "compact t" and "s \<inter> t = {}"
  4814   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
  4815 proof-
  4816   have *:"t \<inter> s = {}" using assms(3) by auto
  4817   show ?thesis using separate_compact_closed[OF assms(2,1) *]
  4818     apply auto apply(rule_tac x=d in exI) apply auto apply (erule_tac x=y in ballE)
  4819     by (auto simp add: dist_commute)
  4820 qed
  4821 
  4822 subsection {* Intervals *}
  4823   
  4824 lemma interval: fixes a :: "'a::ordered_euclidean_space" shows
  4825   "{a <..< b} = {x::'a. \<forall>i<DIM('a). a$$i < x$$i \<and> x$$i < b$$i}" and
  4826   "{a .. b} = {x::'a. \<forall>i<DIM('a). a$$i \<le> x$$i \<and> x$$i \<le> b$$i}"
  4827   by(auto simp add:set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])
  4828 
  4829 lemma mem_interval: fixes a :: "'a::ordered_euclidean_space" shows
  4830   "x \<in> {a<..<b} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i < x$$i \<and> x$$i < b$$i)"
  4831   "x \<in> {a .. b} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i \<le> x$$i \<and> x$$i \<le> b$$i)"
  4832   using interval[of a b] by(auto simp add: set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])
  4833 
  4834 lemma interval_eq_empty: fixes a :: "'a::ordered_euclidean_space" shows
  4835  "({a <..< b} = {} \<longleftrightarrow> (\<exists>i<DIM('a). b$$i \<le> a$$i))" (is ?th1) and
  4836  "({a  ..  b} = {} \<longleftrightarrow> (\<exists>i<DIM('a). b$$i < a$$i))" (is ?th2)
  4837 proof-
  4838   { fix i x assume i:"i<DIM('a)" and as:"b$$i \<le> a$$i" and x:"x\<in>{a <..< b}"
  4839     hence "a $$ i < x $$ i \<and> x $$ i < b $$ i" unfolding mem_interval by auto
  4840     hence "a$$i < b$$i" by auto
  4841     hence False using as by auto  }
  4842   moreover
  4843   { assume as:"\<forall>i<DIM('a). \<not> (b$$i \<le> a$$i)"
  4844     let ?x = "(1/2) *\<^sub>R (a + b)"
  4845     { fix i assume i:"i<DIM('a)" 
  4846       have "a$$i < b$$i" using as[THEN spec[where x=i]] using i by auto
  4847       hence "a$$i < ((1/2) *\<^sub>R (a+b)) $$ i" "((1/2) *\<^sub>R (a+b)) $$ i < b$$i"
  4848         unfolding euclidean_simps by auto }
  4849     hence "{a <..< b} \<noteq> {}" using mem_interval(1)[of "?x" a b] by auto  }
  4850   ultimately show ?th1 by blast
  4851 
  4852   { fix i x assume i:"i<DIM('a)" and as:"b$$i < a$$i" and x:"x\<in>{a .. b}"
  4853     hence "a $$ i \<le> x $$ i \<and> x $$ i \<le> b $$ i" unfolding mem_interval by auto
  4854     hence "a$$i \<le> b$$i" by auto
  4855     hence False using as by auto  }
  4856   moreover
  4857   { assume as:"\<forall>i<DIM('a). \<not> (b$$i < a$$i)"
  4858     let ?x = "(1/2) *\<^sub>R (a + b)"
  4859     { fix i assume i:"i<DIM('a)"
  4860       have "a$$i \<le> b$$i" using as[THEN spec[where x=i]] by auto
  4861       hence "a$$i \<le> ((1/2) *\<^sub>R (a+b)) $$ i" "((1/2) *\<^sub>R (a+b)) $$ i \<le> b$$i"
  4862         unfolding euclidean_simps by auto }
  4863     hence "{a .. b} \<noteq> {}" using mem_interval(2)[of "?x" a b] by auto  }
  4864   ultimately show ?th2 by blast
  4865 qed
  4866 
  4867 lemma interval_ne_empty: fixes a :: "'a::ordered_euclidean_space" shows
  4868   "{a  ..  b} \<noteq> {} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i \<le> b$$i)" and
  4869   "{a <..< b} \<noteq> {} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i < b$$i)"
  4870   unfolding interval_eq_empty[of a b] by fastsimp+
  4871 
  4872 lemma interval_sing: fixes a :: "'a::ordered_euclidean_space" shows
  4873  "{a .. a} = {a}" "{a<..<a} = {}"
  4874   apply(auto simp add: set_eq_iff euclidean_eq[where 'a='a] eucl_less[where 'a='a] eucl_le[where 'a='a])
  4875   apply (simp add: order_eq_iff) apply(rule_tac x=0 in exI) by (auto simp add: not_less less_imp_le)
  4876 
  4877 lemma subset_interval_imp: fixes a :: "'a::ordered_euclidean_space" shows
  4878  "(\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i) \<Longrightarrow> {c .. d} \<subseteq> {a .. b}" and
  4879  "(\<forall>i<DIM('a). a$$i < c$$i \<and> d$$i < b$$i) \<Longrightarrow> {c .. d} \<subseteq> {a<..<b}" and
  4880  "(\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i) \<Longrightarrow> {c<..<d} \<subseteq> {a .. b}" and
  4881  "(\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i) \<Longrightarrow> {c<..<d} \<subseteq> {a<..<b}"
  4882   unfolding subset_eq[unfolded Ball_def] unfolding mem_interval 
  4883   by (auto intro: order_trans less_le_trans le_less_trans less_imp_le) (* BH: Why doesn't just "auto" work here? *)
  4884 
  4885 lemma interval_open_subset_closed:  fixes a :: "'a::ordered_euclidean_space" shows
  4886  "{a<..<b} \<subseteq> {a .. b}"
  4887 proof(simp add: subset_eq, rule)
  4888   fix x
  4889   assume x:"x \<in>{a<..<b}"
  4890   { fix i assume "i<DIM('a)"
  4891     hence "a $$ i \<le> x $$ i"
  4892       using x order_less_imp_le[of "a$$i" "x$$i"] 
  4893       by(simp add: set_eq_iff eucl_less[where 'a='a] eucl_le[where 'a='a] euclidean_eq)
  4894   }
  4895   moreover
  4896   { fix i assume "i<DIM('a)"
  4897     hence "x $$ i \<le> b $$ i"
  4898       using x order_less_imp_le[of "x$$i" "b$$i"]
  4899       by(simp add: set_eq_iff eucl_less[where 'a='a] eucl_le[where 'a='a] euclidean_eq)
  4900   }
  4901   ultimately
  4902   show "a \<le> x \<and> x \<le> b"
  4903     by(simp add: set_eq_iff eucl_less[where 'a='a] eucl_le[where 'a='a] euclidean_eq)
  4904 qed
  4905 
  4906 lemma subset_interval: fixes a :: "'a::ordered_euclidean_space" shows
  4907  "{c .. d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i<DIM('a). c$$i \<le> d$$i) --> (\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i)" (is ?th1) and
  4908  "{c .. d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i<DIM('a). c$$i \<le> d$$i) --> (\<forall>i<DIM('a). a$$i < c$$i \<and> d$$i < b$$i)" (is ?th2) and
  4909  "{c<..<d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i<DIM('a). c$$i < d$$i) --> (\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i)" (is ?th3) and
  4910  "{c<..<d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i<DIM('a). c$$i < d$$i) --> (\<forall>i<DIM('a). a$$i \<le> c$$i \<and> d$$i \<le> b$$i)" (is ?th4)
  4911 proof-
  4912   show ?th1 unfolding subset_eq and Ball_def and mem_interval by (auto intro: order_trans)
  4913   show ?th2 unfolding subset_eq and Ball_def and mem_interval by (auto intro: le_less_trans less_le_trans order_trans less_imp_le)
  4914   { assume as: "{c<..<d} \<subseteq> {a .. b}" "\<forall>i<DIM('a). c$$i < d$$i"
  4915     hence "{c<..<d} \<noteq> {}" unfolding interval_eq_empty by auto
  4916     fix i assume i:"i<DIM('a)"
  4917     (** TODO combine the following two parts as done in the HOL_light version. **)
  4918     { let ?x = "(\<chi>\<chi> j. (if j=i then ((min (a$$j) (d$$j))+c$$j)/2 else (c$$j+d$$j)/2))::'a"
  4919       assume as2: "a$$i > c$$i"
  4920       { fix j assume j:"j<DIM('a)"
  4921         hence "c $$ j < ?x $$ j \<and> ?x $$ j < d $$ j"
  4922           apply(cases "j=i") using as(2)[THEN spec[where x=j]] i
  4923           by (auto simp add: as2)  }
  4924       hence "?x\<in>{c<..<d}" using i unfolding mem_interval by auto
  4925       moreover
  4926       have "?x\<notin>{a .. b}"
  4927         unfolding mem_interval apply auto apply(rule_tac x=i in exI)
  4928         using as(2)[THEN spec[where x=i]] and as2 i
  4929         by auto
  4930       ultimately have False using as by auto  }
  4931     hence "a$$i \<le> c$$i" by(rule ccontr)auto
  4932     moreover
  4933     { let ?x = "(\<chi>\<chi> j. (if j=i then ((max (b$$j) (c$$j))+d$$j)/2 else (c$$j+d$$j)/2))::'a"
  4934       assume as2: "b$$i < d$$i"
  4935       { fix j assume "j<DIM('a)"
  4936         hence "d $$ j > ?x $$ j \<and> ?x $$ j > c $$ j" 
  4937           apply(cases "j=i") using as(2)[THEN spec[where x=j]]
  4938           by (auto simp add: as2)  }
  4939       hence "?x\<in>{c<..<d}" unfolding mem_interval by auto
  4940       moreover
  4941       have "?x\<notin>{a .. b}"
  4942         unfolding mem_interval apply auto apply(rule_tac x=i in exI)
  4943         using as(2)[THEN spec[where x=i]] and as2 using i
  4944         by auto
  4945       ultimately have False using as by auto  }
  4946     hence "b$$i \<ge> d$$i" by(rule ccontr)auto
  4947     ultimately
  4948     have "a$$i \<le> c$$i \<and> d$$i \<le> b$$i" by auto
  4949   } note part1 = this
  4950   show ?th3 unfolding subset_eq and Ball_def and mem_interval 
  4951     apply(rule,rule,rule,rule) apply(rule part1) unfolding subset_eq and Ball_def and mem_interval
  4952     prefer 4 apply auto by(erule_tac x=i in allE,erule_tac x=i in allE,fastsimp)+ 
  4953   { assume as:"{c<..<d} \<subseteq> {a<..<b}" "\<forall>i<DIM('a). c$$i < d$$i"
  4954     fix i assume i:"i<DIM('a)"
  4955     from as(1) have "{c<..<d} \<subseteq> {a..b}" using interval_open_subset_closed[of a b] by auto
  4956     hence "a$$i \<le> c$$i \<and> d$$i \<le> b$$i" using part1 and as(2) using i by auto  } note * = this
  4957   show ?th4 unfolding subset_eq and Ball_def and mem_interval 
  4958     apply(rule,rule,rule,rule) apply(rule *) unfolding subset_eq and Ball_def and mem_interval prefer 4
  4959     apply auto by(erule_tac x=i in allE, simp)+ 
  4960 qed
  4961 
  4962 lemma disjoint_interval: fixes a::"'a::ordered_euclidean_space" shows
  4963   "{a .. b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i<DIM('a). (b$$i < a$$i \<or> d$$i < c$$i \<or> b$$i < c$$i \<or> d$$i < a$$i))" (is ?th1) and
  4964   "{a .. b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i<DIM('a). (b$$i < a$$i \<or> d$$i \<le> c$$i \<or> b$$i \<le> c$$i \<or> d$$i \<le> a$$i))" (is ?th2) and
  4965   "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i<DIM('a). (b$$i \<le> a$$i \<or> d$$i < c$$i \<or> b$$i \<le> c$$i \<or> d$$i \<le> a$$i))" (is ?th3) and
  4966   "{a<..<b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i<DIM('a). (b$$i \<le> a$$i \<or> d$$i \<le> c$$i \<or> b$$i \<le> c$$i \<or> d$$i \<le> a$$i))" (is ?th4)
  4967 proof-
  4968   let ?z = "(\<chi>\<chi> i. ((max (a$$i) (c$$i)) + (min (b$$i) (d$$i))) / 2)::'a"
  4969   note * = set_eq_iff Int_iff empty_iff mem_interval all_conj_distrib[THEN sym] eq_False
  4970   show ?th1 unfolding * apply safe apply(erule_tac x="?z" in allE)
  4971     unfolding not_all apply(erule exE,rule_tac x=x in exI) apply(erule_tac[2-] x=i in allE) by auto
  4972   show ?th2 unfolding * apply safe apply(erule_tac x="?z" in allE)
  4973     unfolding not_all apply(erule exE,rule_tac x=x in exI) apply(erule_tac[2-] x=i in allE) by auto
  4974   show ?th3 unfolding * apply safe apply(erule_tac x="?z" in allE)
  4975     unfolding not_all apply(erule exE,rule_tac x=x in exI) apply(erule_tac[2-] x=i in allE) by auto
  4976   show ?th4 unfolding * apply safe apply(erule_tac x="?z" in allE)
  4977     unfolding not_all apply(erule exE,rule_tac x=x in exI) apply(erule_tac[2-] x=i in allE) by auto
  4978 qed
  4979 
  4980 lemma inter_interval: fixes a :: "'a::ordered_euclidean_space" shows
  4981  "{a .. b} \<inter> {c .. d} =  {(\<chi>\<chi> i. max (a$$i) (c$$i)) .. (\<chi>\<chi> i. min (b$$i) (d$$i))}"
  4982   unfolding set_eq_iff and Int_iff and mem_interval
  4983   by auto
  4984 
  4985 (* Moved interval_open_subset_closed a bit upwards *)
  4986 
  4987 lemma open_interval_lemma: fixes x :: "real" shows
  4988  "a < x \<Longrightarrow> x < b ==> (\<exists>d>0. \<forall>x'. abs(x' - x) < d --> a < x' \<and> x' < b)"
  4989   by(rule_tac x="min (x - a) (b - x)" in exI, auto)
  4990 
  4991 lemma open_interval[intro]: fixes a :: "'a::ordered_euclidean_space" shows "open {a<..<b}"
  4992 proof-
  4993   { fix x assume x:"x\<in>{a<..<b}"
  4994     { fix i assume "i<DIM('a)"
  4995       hence "\<exists>d>0. \<forall>x'. abs (x' - (x$$i)) < d \<longrightarrow> a$$i < x' \<and> x' < b$$i"
  4996         using x[unfolded mem_interval, THEN spec[where x=i]]
  4997         using open_interval_lemma[of "a$$i" "x$$i" "b$$i"] by auto  }
  4998     hence "\<forall>i\<in>{..<DIM('a)}. \<exists>d>0. \<forall>x'. abs (x' - (x$$i)) < d \<longrightarrow> a$$i < x' \<and> x' < b$$i" by auto
  4999     from bchoice[OF this] guess d .. note d=this
  5000     let ?d = "Min (d ` {..<DIM('a)})"
  5001     have **:"finite (d ` {..<DIM('a)})" "d ` {..<DIM('a)} \<noteq> {}" by auto
  5002     have "?d>0" using Min_gr_iff[OF **] using d by auto
  5003     moreover
  5004     { fix x' assume as:"dist x' x < ?d"
  5005       { fix i assume i:"i<DIM('a)"
  5006         hence "\<bar>x'$$i - x $$ i\<bar> < d i"
  5007           using norm_bound_component_lt[OF as[unfolded dist_norm], of i]
  5008           unfolding euclidean_simps Min_gr_iff[OF **] by auto
  5009         hence "a $$ i < x' $$ i" "x' $$ i < b $$ i" using i and d[THEN bspec[where x=i]] by auto  }
  5010       hence "a < x' \<and> x' < b" apply(subst(2) eucl_less,subst(1) eucl_less) by auto  }
  5011     ultimately have "\<exists>e>0. \<forall>x'. dist x' x < e \<longrightarrow> x' \<in> {a<..<b}" by auto
  5012   }
  5013   thus ?thesis unfolding open_dist using open_interval_lemma by auto
  5014 qed
  5015 
  5016 lemma closed_interval[intro]: fixes a :: "'a::ordered_euclidean_space" shows "closed {a .. b}"
  5017 proof-
  5018   { fix x i assume i:"i<DIM('a)"
  5019     assume as:"\<forall>e>0. \<exists>x'\<in>{a..b}. x' \<noteq> x \<and> dist x' x < e"(* and xab:"a$$i > x$$i \<or> b$$i < x$$i"*)
  5020     { assume xa:"a$$i > x$$i"
  5021       with as obtain y where y:"y\<in>{a..b}" "y \<noteq> x" "dist y x < a$$i - x$$i" by(erule_tac x="a$$i - x$$i" in allE)auto
  5022       hence False unfolding mem_interval and dist_norm
  5023         using component_le_norm[of "y-x" i, unfolded euclidean_simps] and xa using i
  5024         by(auto elim!: allE[where x=i])
  5025     } hence "a$$i \<le> x$$i" by(rule ccontr)auto
  5026     moreover
  5027     { assume xb:"b$$i < x$$i"
  5028       with as obtain y where y:"y\<in>{a..b}" "y \<noteq> x" "dist y x < x$$i - b$$i"
  5029         by(erule_tac x="x$$i - b$$i" in allE)auto
  5030       hence False unfolding mem_interval and dist_norm
  5031         using component_le_norm[of "y-x" i, unfolded euclidean_simps] and xb using i
  5032         by(auto elim!: allE[where x=i])
  5033     } hence "x$$i \<le> b$$i" by(rule ccontr)auto
  5034     ultimately
  5035     have "a $$ i \<le> x $$ i \<and> x $$ i \<le> b $$ i" by auto }
  5036   thus ?thesis unfolding closed_limpt islimpt_approachable mem_interval by auto
  5037 qed
  5038 
  5039 lemma interior_closed_interval[intro]: fixes a :: "'a::ordered_euclidean_space" shows
  5040  "interior {a .. b} = {a<..<b}" (is "?L = ?R")
  5041 proof(rule subset_antisym)
  5042   show "?R \<subseteq> ?L" using interior_maximal[OF interval_open_subset_closed open_interval] by auto
  5043 next
  5044   { fix x assume "\<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> {a..b}"
  5045     then obtain s where s:"open s" "x \<in> s" "s \<subseteq> {a..b}" by auto
  5046     then obtain e where "e>0" and e:"\<forall>x'. dist x' x < e \<longrightarrow> x' \<in> {a..b}" unfolding open_dist and subset_eq by auto
  5047     { fix i assume i:"i<DIM('a)"
  5048       have "dist (x - (e / 2) *\<^sub>R basis i) x < e"
  5049            "dist (x + (e / 2) *\<^sub>R basis i) x < e"
  5050         unfolding dist_norm apply auto
  5051         unfolding norm_minus_cancel using norm_basis and `e>0` by auto
  5052       hence "a $$ i \<le> (x - (e / 2) *\<^sub>R basis i) $$ i"
  5053                      "(x + (e / 2) *\<^sub>R basis i) $$ i \<le> b $$ i"
  5054         using e[THEN spec[where x="x - (e/2) *\<^sub>R basis i"]]
  5055         and   e[THEN spec[where x="x + (e/2) *\<^sub>R basis i"]]
  5056         unfolding mem_interval by (auto elim!: allE[where x=i])
  5057       hence "a $$ i < x $$ i" and "x $$ i < b $$ i" unfolding euclidean_simps
  5058         unfolding basis_component using `e>0` i by auto  }
  5059     hence "x \<in> {a<..<b}" unfolding mem_interval by auto  }
  5060   thus "?L \<subseteq> ?R" unfolding interior_def and subset_eq by auto
  5061 qed
  5062 
  5063 lemma bounded_closed_interval: fixes a :: "'a::ordered_euclidean_space" shows "bounded {a .. b}"
  5064 proof-
  5065   let ?b = "\<Sum>i<DIM('a). \<bar>a$$i\<bar> + \<bar>b$$i\<bar>"
  5066   { fix x::"'a" assume x:"\<forall>i<DIM('a). a $$ i \<le> x $$ i \<and> x $$ i \<le> b $$ i"
  5067     { fix i assume "i<DIM('a)"
  5068       hence "\<bar>x$$i\<bar> \<le> \<bar>a$$i\<bar> + \<bar>b$$i\<bar>" using x[THEN spec[where x=i]] by auto  }
  5069     hence "(\<Sum>i<DIM('a). \<bar>x $$ i\<bar>) \<le> ?b" apply-apply(rule setsum_mono) by auto
  5070     hence "norm x \<le> ?b" using norm_le_l1[of x] by auto  }
  5071   thus ?thesis unfolding interval and bounded_iff by auto
  5072 qed
  5073 
  5074 lemma bounded_interval: fixes a :: "'a::ordered_euclidean_space" shows
  5075  "bounded {a .. b} \<and> bounded {a<..<b}"
  5076   using bounded_closed_interval[of a b]
  5077   using interval_open_subset_closed[of a b]
  5078   using bounded_subset[of "{a..b}" "{a<..<b}"]
  5079   by simp
  5080 
  5081 lemma not_interval_univ: fixes a :: "'a::ordered_euclidean_space" shows
  5082  "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"
  5083   using bounded_interval[of a b] by auto
  5084 
  5085 lemma compact_interval: fixes a :: "'a::ordered_euclidean_space" shows "compact {a .. b}"
  5086   using bounded_closed_imp_compact[of "{a..b}"] using bounded_interval[of a b]
  5087   by auto
  5088 
  5089 lemma open_interval_midpoint: fixes a :: "'a::ordered_euclidean_space"
  5090   assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"
  5091 proof-
  5092   { fix i assume "i<DIM('a)"
  5093     hence "a $$ i < ((1 / 2) *\<^sub>R (a + b)) $$ i \<and> ((1 / 2) *\<^sub>R (a + b)) $$ i < b $$ i"
  5094       using assms[unfolded interval_ne_empty, THEN spec[where x=i]]
  5095       unfolding euclidean_simps by auto  }
  5096   thus ?thesis unfolding mem_interval by auto
  5097 qed
  5098 
  5099 lemma open_closed_interval_convex: fixes x :: "'a::ordered_euclidean_space"
  5100   assumes x:"x \<in> {a<..<b}" and y:"y \<in> {a .. b}" and e:"0 < e" "e \<le> 1"
  5101   shows "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<in> {a<..<b}"
  5102 proof-
  5103   { fix i assume i:"i<DIM('a)"
  5104     have "a $$ i = e * a$$i + (1 - e) * a$$i" unfolding left_diff_distrib by simp
  5105     also have "\<dots> < e * x $$ i + (1 - e) * y $$ i" apply(rule add_less_le_mono)
  5106       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
  5107       using x unfolding mem_interval using i apply simp
  5108       using y unfolding mem_interval using i apply simp
  5109       done
  5110     finally have "a $$ i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) $$ i" unfolding euclidean_simps by auto
  5111     moreover {
  5112     have "b $$ i = e * b$$i + (1 - e) * b$$i" unfolding left_diff_distrib by simp
  5113     also have "\<dots> > e * x $$ i + (1 - e) * y $$ i" apply(rule add_less_le_mono)
  5114       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
  5115       using x unfolding mem_interval using i apply simp
  5116       using y unfolding mem_interval using i apply simp
  5117       done
  5118     finally have "(e *\<^sub>R x + (1 - e) *\<^sub>R y) $$ i < b $$ i" unfolding euclidean_simps by auto
  5119     } ultimately have "a $$ i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) $$ i \<and> (e *\<^sub>R x + (1 - e) *\<^sub>R y) $$ i < b $$ i" by auto }
  5120   thus ?thesis unfolding mem_interval by auto
  5121 qed
  5122 
  5123 lemma closure_open_interval: fixes a :: "'a::ordered_euclidean_space"
  5124   assumes "{a<..<b} \<noteq> {}"
  5125   shows "closure {a<..<b} = {a .. b}"
  5126 proof-
  5127   have ab:"a < b" using assms[unfolded interval_ne_empty] apply(subst eucl_less) by auto
  5128   let ?c = "(1 / 2) *\<^sub>R (a + b)"
  5129   { fix x assume as:"x \<in> {a .. b}"
  5130     def f == "\<lambda>n::nat. x + (inverse (real n + 1)) *\<^sub>R (?c - x)"
  5131     { fix n assume fn:"f n < b \<longrightarrow> a < f n \<longrightarrow> f n = x" and xc:"x \<noteq> ?c"
  5132       have *:"0 < inverse (real n + 1)" "inverse (real n + 1) \<le> 1" unfolding inverse_le_1_iff by auto
  5133       have "(inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b)) + (1 - inverse (real n + 1)) *\<^sub>R x =
  5134         x + (inverse (real n + 1)) *\<^sub>R (((1 / 2) *\<^sub>R (a + b)) - x)"
  5135         by (auto simp add: algebra_simps)
  5136       hence "f n < b" and "a < f n" using open_closed_interval_convex[OF open_interval_midpoint[OF assms] as *] unfolding f_def by auto
  5137       hence False using fn unfolding f_def using xc by auto  }
  5138     moreover
  5139     { assume "\<not> (f ---> x) sequentially"
  5140       { fix e::real assume "e>0"
  5141         hence "\<exists>N::nat. inverse (real (N + 1)) < e" using real_arch_inv[of e] apply (auto simp add: Suc_pred') apply(rule_tac x="n - 1" in exI) by auto
  5142         then obtain N::nat where "inverse (real (N + 1)) < e" by auto
  5143         hence "\<forall>n\<ge>N. inverse (real n + 1) < e" by (auto, metis Suc_le_mono le_SucE less_imp_inverse_less nat_le_real_less order_less_trans real_of_nat_Suc real_of_nat_Suc_gt_zero)
  5144         hence "\<exists>N::nat. \<forall>n\<ge>N. inverse (real n + 1) < e" by auto  }
  5145       hence "((\<lambda>n. inverse (real n + 1)) ---> 0) sequentially"
  5146         unfolding Lim_sequentially by(auto simp add: dist_norm)
  5147       hence "(f ---> x) sequentially" unfolding f_def
  5148         using Lim_add[OF Lim_const, of "\<lambda>n::nat. (inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b) - x)" 0 sequentially x]
  5149         using Lim_vmul[of "\<lambda>n::nat. inverse (real n + 1)" 0 sequentially "((1 / 2) *\<^sub>R (a + b) - x)"] by auto  }
  5150     ultimately have "x \<in> closure {a<..<b}"
  5151       using as and open_interval_midpoint[OF assms] unfolding closure_def unfolding islimpt_sequential by(cases "x=?c")auto  }
  5152   thus ?thesis using closure_minimal[OF interval_open_subset_closed closed_interval, of a b] by blast
  5153 qed
  5154 
  5155 lemma bounded_subset_open_interval_symmetric: fixes s::"('a::ordered_euclidean_space) set"
  5156   assumes "bounded s"  shows "\<exists>a. s \<subseteq> {-a<..<a}"
  5157 proof-
  5158   obtain b where "b>0" and b:"\<forall>x\<in>s. norm x \<le> b" using assms[unfolded bounded_pos] by auto
  5159   def a \<equiv> "(\<chi>\<chi> i. b+1)::'a"
  5160   { fix x assume "x\<in>s"
  5161     fix i assume i:"i<DIM('a)"
  5162     hence "(-a)$$i < x$$i" and "x$$i < a$$i" using b[THEN bspec[where x=x], OF `x\<in>s`]
  5163       and component_le_norm[of x i] unfolding euclidean_simps and a_def by auto  }
  5164   thus ?thesis by(auto intro: exI[where x=a] simp add: eucl_less[where 'a='a])
  5165 qed
  5166 
  5167 lemma bounded_subset_open_interval:
  5168   fixes s :: "('a::ordered_euclidean_space) set"
  5169   shows "bounded s ==> (\<exists>a b. s \<subseteq> {a<..<b})"
  5170   by (auto dest!: bounded_subset_open_interval_symmetric)
  5171 
  5172 lemma bounded_subset_closed_interval_symmetric:
  5173   fixes s :: "('a::ordered_euclidean_space) set"
  5174   assumes "bounded s" shows "\<exists>a. s \<subseteq> {-a .. a}"
  5175 proof-
  5176   obtain a where "s \<subseteq> {- a<..<a}" using bounded_subset_open_interval_symmetric[OF assms] by auto
  5177   thus ?thesis using interval_open_subset_closed[of "-a" a] by auto
  5178 qed
  5179 
  5180 lemma bounded_subset_closed_interval:
  5181   fixes s :: "('a::ordered_euclidean_space) set"
  5182   shows "bounded s ==> (\<exists>a b. s \<subseteq> {a .. b})"
  5183   using bounded_subset_closed_interval_symmetric[of s] by auto
  5184 
  5185 lemma frontier_closed_interval:
  5186   fixes a b :: "'a::ordered_euclidean_space"
  5187   shows "frontier {a .. b} = {a .. b} - {a<..<b}"
  5188   unfolding frontier_def unfolding interior_closed_interval and closure_closed[OF closed_interval] ..
  5189 
  5190 lemma frontier_open_interval:
  5191   fixes a b :: "'a::ordered_euclidean_space"
  5192   shows "frontier {a<..<b} = (if {a<..<b} = {} then {} else {a .. b} - {a<..<b})"
  5193 proof(cases "{a<..<b} = {}")
  5194   case True thus ?thesis using frontier_empty by auto
  5195 next
  5196   case False thus ?thesis unfolding frontier_def and closure_open_interval[OF False] and interior_open[OF open_interval] by auto
  5197 qed
  5198 
  5199 lemma inter_interval_mixed_eq_empty: fixes a :: "'a::ordered_euclidean_space"
  5200   assumes "{c<..<d} \<noteq> {}"  shows "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> {a<..<b} \<inter> {c<..<d} = {}"
  5201   unfolding closure_open_interval[OF assms, THEN sym] unfolding open_inter_closure_eq_empty[OF open_interval] ..
  5202 
  5203 
  5204 (* Some stuff for half-infinite intervals too; FIXME: notation?  *)
  5205 
  5206 lemma closed_interval_left: fixes b::"'a::euclidean_space"
  5207   shows "closed {x::'a. \<forall>i<DIM('a). x$$i \<le> b$$i}"
  5208 proof-
  5209   { fix i assume i:"i<DIM('a)"
  5210     fix x::"'a" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i<DIM('a). x $$ i \<le> b $$ i}. x' \<noteq> x \<and> dist x' x < e"
  5211     { assume "x$$i > b$$i"
  5212       then obtain y where "y $$ i \<le> b $$ i"  "y \<noteq> x"  "dist y x < x$$i - b$$i"
  5213         using x[THEN spec[where x="x$$i - b$$i"]] using i by auto
  5214       hence False using component_le_norm[of "y - x" i] unfolding dist_norm euclidean_simps using i 
  5215         by auto   }
  5216     hence "x$$i \<le> b$$i" by(rule ccontr)auto  }
  5217   thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
  5218 qed
  5219 
  5220 lemma closed_interval_right: fixes a::"'a::euclidean_space"
  5221   shows "closed {x::'a. \<forall>i<DIM('a). a$$i \<le> x$$i}"
  5222 proof-
  5223   { fix i assume i:"i<DIM('a)"
  5224     fix x::"'a" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i<DIM('a). a $$ i \<le> x $$ i}. x' \<noteq> x \<and> dist x' x < e"
  5225     { assume "a$$i > x$$i"
  5226       then obtain y where "a $$ i \<le> y $$ i"  "y \<noteq> x"  "dist y x < a$$i - x$$i"
  5227         using x[THEN spec[where x="a$$i - x$$i"]] i by auto
  5228       hence False using component_le_norm[of "y - x" i] unfolding dist_norm and euclidean_simps by auto   }
  5229     hence "a$$i \<le> x$$i" by(rule ccontr)auto  }
  5230   thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
  5231 qed
  5232 
  5233 text {* Intervals in general, including infinite and mixtures of open and closed. *}
  5234 
  5235 definition "is_interval (s::('a::euclidean_space) set) \<longleftrightarrow>
  5236   (\<forall>a\<in>s. \<forall>b\<in>s. \<forall>x. (\<forall>i<DIM('a). ((a$$i \<le> x$$i \<and> x$$i \<le> b$$i) \<or> (b$$i \<le> x$$i \<and> x$$i \<le> a$$i))) \<longrightarrow> x \<in> s)"
  5237 
  5238 lemma is_interval_interval: "is_interval {a .. b::'a::ordered_euclidean_space}" (is ?th1)
  5239   "is_interval {a<..<b}" (is ?th2) proof -
  5240   have *:"\<And>x y z::real. x < y \<Longrightarrow> y < z \<Longrightarrow> x < z" by auto
  5241   show ?th1 ?th2  unfolding is_interval_def mem_interval Ball_def atLeastAtMost_iff
  5242     by(meson order_trans le_less_trans less_le_trans *)+ qed
  5243 
  5244 lemma is_interval_empty:
  5245  "is_interval {}"
  5246   unfolding is_interval_def
  5247   by simp
  5248 
  5249 lemma is_interval_univ:
  5250  "is_interval UNIV"
  5251   unfolding is_interval_def
  5252   by simp
  5253 
  5254 subsection{* Closure of halfspaces and hyperplanes.                                    *}
  5255 
  5256 lemma Lim_inner:
  5257   assumes "(f ---> l) net"  shows "((\<lambda>y. inner a (f y)) ---> inner a l) net"
  5258   by (intro tendsto_intros assms)
  5259 
  5260 lemma continuous_at_inner: "continuous (at x) (inner a)"
  5261   unfolding continuous_at by (intro tendsto_intros)
  5262 
  5263 lemma continuous_at_euclidean_component[intro!, simp]: "continuous (at x) (\<lambda>x. x $$ i)"
  5264   unfolding euclidean_component_def by (rule continuous_at_inner)
  5265 
  5266 lemma continuous_on_inner:
  5267   fixes s :: "'a::real_inner set"
  5268   shows "continuous_on s (inner a)"
  5269   unfolding continuous_on by (rule ballI) (intro tendsto_intros)
  5270 
  5271 lemma closed_halfspace_le: "closed {x. inner a x \<le> b}"
  5272 proof-
  5273   have "\<forall>x. continuous (at x) (inner a)"
  5274     unfolding continuous_at by (rule allI) (intro tendsto_intros)
  5275   hence "closed (inner a -` {..b})"
  5276     using closed_real_atMost by (rule continuous_closed_vimage)
  5277   moreover have "{x. inner a x \<le> b} = inner a -` {..b}" by auto
  5278   ultimately show ?thesis by simp
  5279 qed
  5280 
  5281 lemma closed_halfspace_ge: "closed {x. inner a x \<ge> b}"
  5282   using closed_halfspace_le[of "-a" "-b"] unfolding inner_minus_left by auto
  5283 
  5284 lemma closed_hyperplane: "closed {x. inner a x = b}"
  5285 proof-
  5286   have "{x. inner a x = b} = {x. inner a x \<ge> b} \<inter> {x. inner a x \<le> b}" by auto
  5287   thus ?thesis using closed_halfspace_le[of a b] and closed_halfspace_ge[of b a] using closed_Int by auto
  5288 qed
  5289 
  5290 lemma closed_halfspace_component_le:
  5291   shows "closed {x::'a::euclidean_space. x$$i \<le> a}"
  5292   using closed_halfspace_le[of "(basis i)::'a" a] unfolding euclidean_component_def .
  5293 
  5294 lemma closed_halfspace_component_ge:
  5295   shows "closed {x::'a::euclidean_space. x$$i \<ge> a}"
  5296   using closed_halfspace_ge[of a "(basis i)::'a"] unfolding euclidean_component_def .
  5297 
  5298 text{* Openness of halfspaces.                                                   *}
  5299 
  5300 lemma open_halfspace_lt: "open {x. inner a x < b}"
  5301 proof-
  5302   have "- {x. b \<le> inner a x} = {x. inner a x < b}" by auto
  5303   thus ?thesis using closed_halfspace_ge[unfolded closed_def, of b a] by auto
  5304 qed
  5305 
  5306 lemma open_halfspace_gt: "open {x. inner a x > b}"
  5307 proof-
  5308   have "- {x. b \<ge> inner a x} = {x. inner a x > b}" by auto
  5309   thus ?thesis using closed_halfspace_le[unfolded closed_def, of a b] by auto
  5310 qed
  5311 
  5312 lemma open_halfspace_component_lt:
  5313   shows "open {x::'a::euclidean_space. x$$i < a}"
  5314   using open_halfspace_lt[of "(basis i)::'a" a] unfolding euclidean_component_def .
  5315 
  5316 lemma open_halfspace_component_gt:
  5317   shows "open {x::'a::euclidean_space. x$$i  > a}"
  5318   using open_halfspace_gt[of a "(basis i)::'a"] unfolding euclidean_component_def .
  5319 
  5320 text{* Instantiation for intervals on @{text ordered_euclidean_space} *}
  5321 
  5322 lemma eucl_lessThan_eq_halfspaces:
  5323   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5324   shows "{..<a} = (\<Inter>i<DIM('a). {x. x $$ i < a $$ i})"
  5325  by (auto simp: eucl_less[where 'a='a])
  5326 
  5327 lemma eucl_greaterThan_eq_halfspaces:
  5328   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5329   shows "{a<..} = (\<Inter>i<DIM('a). {x. a $$ i < x $$ i})"
  5330  by (auto simp: eucl_less[where 'a='a])
  5331 
  5332 lemma eucl_atMost_eq_halfspaces:
  5333   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5334   shows "{.. a} = (\<Inter>i<DIM('a). {x. x $$ i \<le> a $$ i})"
  5335  by (auto simp: eucl_le[where 'a='a])
  5336 
  5337 lemma eucl_atLeast_eq_halfspaces:
  5338   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5339   shows "{a ..} = (\<Inter>i<DIM('a). {x. a $$ i \<le> x $$ i})"
  5340  by (auto simp: eucl_le[where 'a='a])
  5341 
  5342 lemma open_eucl_lessThan[simp, intro]:
  5343   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5344   shows "open {..< a}"
  5345   by (auto simp: eucl_lessThan_eq_halfspaces open_halfspace_component_lt)
  5346 
  5347 lemma open_eucl_greaterThan[simp, intro]:
  5348   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5349   shows "open {a <..}"
  5350   by (auto simp: eucl_greaterThan_eq_halfspaces open_halfspace_component_gt)
  5351 
  5352 lemma closed_eucl_atMost[simp, intro]:
  5353   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5354   shows "closed {.. a}"
  5355   unfolding eucl_atMost_eq_halfspaces
  5356 proof (safe intro!: closed_INT)
  5357   fix i :: nat
  5358   have "- {x::'a. x $$ i \<le> a $$ i} = {x. a $$ i < x $$ i}" by auto
  5359   then show "closed {x::'a. x $$ i \<le> a $$ i}"
  5360     by (simp add: closed_def open_halfspace_component_gt)
  5361 qed
  5362 
  5363 lemma closed_eucl_atLeast[simp, intro]:
  5364   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5365   shows "closed {a ..}"
  5366   unfolding eucl_atLeast_eq_halfspaces
  5367 proof (safe intro!: closed_INT)
  5368   fix i :: nat
  5369   have "- {x::'a. a $$ i \<le> x $$ i} = {x. x $$ i < a $$ i}" by auto
  5370   then show "closed {x::'a. a $$ i \<le> x $$ i}"
  5371     by (simp add: closed_def open_halfspace_component_lt)
  5372 qed
  5373 
  5374 lemma open_vimage_euclidean_component: "open S \<Longrightarrow> open ((\<lambda>x. x $$ i) -` S)"
  5375   by (auto intro!: continuous_open_vimage)
  5376 
  5377 text{* This gives a simple derivation of limit component bounds.                 *}
  5378 
  5379 lemma Lim_component_le: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
  5380   assumes "(f ---> l) net" "\<not> (trivial_limit net)"  "eventually (\<lambda>x. f(x)$$i \<le> b) net"
  5381   shows "l$$i \<le> b"
  5382 proof-
  5383   { fix x have "x \<in> {x::'b. inner (basis i) x \<le> b} \<longleftrightarrow> x$$i \<le> b"
  5384       unfolding euclidean_component_def by auto  } note * = this
  5385   show ?thesis using Lim_in_closed_set[of "{x. inner (basis i) x \<le> b}" f net l] unfolding *
  5386     using closed_halfspace_le[of "(basis i)::'b" b] and assms(1,2,3) by auto
  5387 qed
  5388 
  5389 lemma Lim_component_ge: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
  5390   assumes "(f ---> l) net"  "\<not> (trivial_limit net)"  "eventually (\<lambda>x. b \<le> (f x)$$i) net"
  5391   shows "b \<le> l$$i"
  5392 proof-
  5393   { fix x have "x \<in> {x::'b. inner (basis i) x \<ge> b} \<longleftrightarrow> x$$i \<ge> b"
  5394       unfolding euclidean_component_def by auto  } note * = this
  5395   show ?thesis using Lim_in_closed_set[of "{x. inner (basis i) x \<ge> b}" f net l] unfolding *
  5396     using closed_halfspace_ge[of b "(basis i)"] and assms(1,2,3) by auto
  5397 qed
  5398 
  5399 lemma Lim_component_eq: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
  5400   assumes net:"(f ---> l) net" "~(trivial_limit net)" and ev:"eventually (\<lambda>x. f(x)$$i = b) net"
  5401   shows "l$$i = b"
  5402   using ev[unfolded order_eq_iff eventually_and] using Lim_component_ge[OF net, of b i] and Lim_component_le[OF net, of i b] by auto
  5403 text{* Limits relative to a union.                                               *}
  5404 
  5405 lemma eventually_within_Un:
  5406   "eventually P (net within (s \<union> t)) \<longleftrightarrow>
  5407     eventually P (net within s) \<and> eventually P (net within t)"
  5408   unfolding Limits.eventually_within
  5409   by (auto elim!: eventually_rev_mp)
  5410 
  5411 lemma Lim_within_union:
  5412  "(f ---> l) (net within (s \<union> t)) \<longleftrightarrow>
  5413   (f ---> l) (net within s) \<and> (f ---> l) (net within t)"
  5414   unfolding tendsto_def
  5415   by (auto simp add: eventually_within_Un)
  5416 
  5417 lemma Lim_topological:
  5418  "(f ---> l) net \<longleftrightarrow>
  5419         trivial_limit net \<or>
  5420         (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net)"
  5421   unfolding tendsto_def trivial_limit_eq by auto
  5422 
  5423 lemma continuous_on_union:
  5424   assumes "closed s" "closed t" "continuous_on s f" "continuous_on t f"
  5425   shows "continuous_on (s \<union> t) f"
  5426   using assms unfolding continuous_on Lim_within_union
  5427   unfolding Lim_topological trivial_limit_within closed_limpt by auto
  5428 
  5429 lemma continuous_on_cases:
  5430   assumes "closed s" "closed t" "continuous_on s f" "continuous_on t g"
  5431           "\<forall>x. (x\<in>s \<and> \<not> P x) \<or> (x \<in> t \<and> P x) \<longrightarrow> f x = g x"
  5432   shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
  5433 proof-
  5434   let ?h = "(\<lambda>x. if P x then f x else g x)"
  5435   have "\<forall>x\<in>s. f x = (if P x then f x else g x)" using assms(5) by auto
  5436   hence "continuous_on s ?h" using continuous_on_eq[of s f ?h] using assms(3) by auto
  5437   moreover
  5438   have "\<forall>x\<in>t. g x = (if P x then f x else g x)" using assms(5) by auto
  5439   hence "continuous_on t ?h" using continuous_on_eq[of t g ?h] using assms(4) by auto
  5440   ultimately show ?thesis using continuous_on_union[OF assms(1,2), of ?h] by auto
  5441 qed
  5442 
  5443 
  5444 text{* Some more convenient intermediate-value theorem formulations.             *}
  5445 
  5446 lemma connected_ivt_hyperplane:
  5447   assumes "connected s" "x \<in> s" "y \<in> s" "inner a x \<le> b" "b \<le> inner a y"
  5448   shows "\<exists>z \<in> s. inner a z = b"
  5449 proof(rule ccontr)
  5450   assume as:"\<not> (\<exists>z\<in>s. inner a z = b)"
  5451   let ?A = "{x. inner a x < b}"
  5452   let ?B = "{x. inner a x > b}"
  5453   have "open ?A" "open ?B" using open_halfspace_lt and open_halfspace_gt by auto
  5454   moreover have "?A \<inter> ?B = {}" by auto
  5455   moreover have "s \<subseteq> ?A \<union> ?B" using as by auto
  5456   ultimately show False using assms(1)[unfolded connected_def not_ex, THEN spec[where x="?A"], THEN spec[where x="?B"]] and assms(2-5) by auto
  5457 qed
  5458 
  5459 lemma connected_ivt_component: fixes x::"'a::euclidean_space" shows
  5460  "connected s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> x$$k \<le> a \<Longrightarrow> a \<le> y$$k \<Longrightarrow> (\<exists>z\<in>s.  z$$k = a)"
  5461   using connected_ivt_hyperplane[of s x y "(basis k)::'a" a]
  5462   unfolding euclidean_component_def by auto
  5463 
  5464 subsection {* Homeomorphisms *}
  5465 
  5466 definition "homeomorphism s t f g \<equiv>
  5467      (\<forall>x\<in>s. (g(f x) = x)) \<and> (f ` s = t) \<and> continuous_on s f \<and>
  5468      (\<forall>y\<in>t. (f(g y) = y)) \<and> (g ` t = s) \<and> continuous_on t g"
  5469 
  5470 definition
  5471   homeomorphic :: "'a::metric_space set \<Rightarrow> 'b::metric_space set \<Rightarrow> bool"
  5472     (infixr "homeomorphic" 60) where
  5473   homeomorphic_def: "s homeomorphic t \<equiv> (\<exists>f g. homeomorphism s t f g)"
  5474 
  5475 lemma homeomorphic_refl: "s homeomorphic s"
  5476   unfolding homeomorphic_def
  5477   unfolding homeomorphism_def
  5478   using continuous_on_id
  5479   apply(rule_tac x = "(\<lambda>x. x)" in exI)
  5480   apply(rule_tac x = "(\<lambda>x. x)" in exI)
  5481   by blast
  5482 
  5483 lemma homeomorphic_sym:
  5484  "s homeomorphic t \<longleftrightarrow> t homeomorphic s"
  5485 unfolding homeomorphic_def
  5486 unfolding homeomorphism_def
  5487 by blast 
  5488 
  5489 lemma homeomorphic_trans:
  5490   assumes "s homeomorphic t" "t homeomorphic u" shows "s homeomorphic u"
  5491 proof-
  5492   obtain f1 g1 where fg1:"\<forall>x\<in>s. g1 (f1 x) = x"  "f1 ` s = t" "continuous_on s f1" "\<forall>y\<in>t. f1 (g1 y) = y" "g1 ` t = s" "continuous_on t g1"
  5493     using assms(1) unfolding homeomorphic_def homeomorphism_def by auto
  5494   obtain f2 g2 where fg2:"\<forall>x\<in>t. g2 (f2 x) = x"  "f2 ` t = u" "continuous_on t f2" "\<forall>y\<in>u. f2 (g2 y) = y" "g2 ` u = t" "continuous_on u g2"
  5495     using assms(2) unfolding homeomorphic_def homeomorphism_def by auto
  5496 
  5497   { fix x assume "x\<in>s" hence "(g1 \<circ> g2) ((f2 \<circ> f1) x) = x" using fg1(1)[THEN bspec[where x=x]] and fg2(1)[THEN bspec[where x="f1 x"]] and fg1(2) by auto }
  5498   moreover have "(f2 \<circ> f1) ` s = u" using fg1(2) fg2(2) by auto
  5499   moreover have "continuous_on s (f2 \<circ> f1)" using continuous_on_compose[OF fg1(3)] and fg2(3) unfolding fg1(2) by auto
  5500   moreover { fix y assume "y\<in>u" hence "(f2 \<circ> f1) ((g1 \<circ> g2) y) = y" using fg2(4)[THEN bspec[where x=y]] and fg1(4)[THEN bspec[where x="g2 y"]] and fg2(5) by auto }
  5501   moreover have "(g1 \<circ> g2) ` u = s" using fg1(5) fg2(5) by auto
  5502   moreover have "continuous_on u (g1 \<circ> g2)" using continuous_on_compose[OF fg2(6)] and fg1(6)  unfolding fg2(5) by auto
  5503   ultimately show ?thesis unfolding homeomorphic_def homeomorphism_def apply(rule_tac x="f2 \<circ> f1" in exI) apply(rule_tac x="g1 \<circ> g2" in exI) by auto
  5504 qed
  5505 
  5506 lemma homeomorphic_minimal:
  5507  "s homeomorphic t \<longleftrightarrow>
  5508     (\<exists>f g. (\<forall>x\<in>s. f(x) \<in> t \<and> (g(f(x)) = x)) \<and>
  5509            (\<forall>y\<in>t. g(y) \<in> s \<and> (f(g(y)) = y)) \<and>
  5510            continuous_on s f \<and> continuous_on t g)"
  5511 unfolding homeomorphic_def homeomorphism_def
  5512 apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI)
  5513 apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI) apply auto
  5514 unfolding image_iff
  5515 apply(erule_tac x="g x" in ballE) apply(erule_tac x="x" in ballE)
  5516 apply auto apply(rule_tac x="g x" in bexI) apply auto
  5517 apply(erule_tac x="f x" in ballE) apply(erule_tac x="x" in ballE)
  5518 apply auto apply(rule_tac x="f x" in bexI) by auto
  5519 
  5520 text {* Relatively weak hypotheses if a set is compact. *}
  5521 
  5522 lemma homeomorphism_compact:
  5523   fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
  5524     (* class constraint due to continuous_on_inverse *)
  5525   assumes "compact s" "continuous_on s f"  "f ` s = t"  "inj_on f s"
  5526   shows "\<exists>g. homeomorphism s t f g"
  5527 proof-
  5528   def g \<equiv> "\<lambda>x. SOME y. y\<in>s \<and> f y = x"
  5529   have g:"\<forall>x\<in>s. g (f x) = x" using assms(3) assms(4)[unfolded inj_on_def] unfolding g_def by auto
  5530   { fix y assume "y\<in>t"
  5531     then obtain x where x:"f x = y" "x\<in>s" using assms(3) by auto
  5532     hence "g (f x) = x" using g by auto
  5533     hence "f (g y) = y" unfolding x(1)[THEN sym] by auto  }
  5534   hence g':"\<forall>x\<in>t. f (g x) = x" by auto
  5535   moreover
  5536   { fix x
  5537     have "x\<in>s \<Longrightarrow> x \<in> g ` t" using g[THEN bspec[where x=x]] unfolding image_iff using assms(3) by(auto intro!: bexI[where x="f x"])
  5538     moreover
  5539     { assume "x\<in>g ` t"
  5540       then obtain y where y:"y\<in>t" "g y = x" by auto
  5541       then obtain x' where x':"x'\<in>s" "f x' = y" using assms(3) by auto
  5542       hence "x \<in> s" unfolding g_def using someI2[of "\<lambda>b. b\<in>s \<and> f b = y" x' "\<lambda>x. x\<in>s"] unfolding y(2)[THEN sym] and g_def by auto }
  5543     ultimately have "x\<in>s \<longleftrightarrow> x \<in> g ` t" ..  }
  5544   hence "g ` t = s" by auto
  5545   ultimately
  5546   show ?thesis unfolding homeomorphism_def homeomorphic_def
  5547     apply(rule_tac x=g in exI) using g and assms(3) and continuous_on_inverse[OF assms(2,1), of g, unfolded assms(3)] and assms(2) by auto
  5548 qed
  5549 
  5550 lemma homeomorphic_compact:
  5551   fixes f :: "'a::heine_borel \<Rightarrow> 'b::heine_borel"
  5552     (* class constraint due to continuous_on_inverse *)
  5553   shows "compact s \<Longrightarrow> continuous_on s f \<Longrightarrow> (f ` s = t) \<Longrightarrow> inj_on f s
  5554           \<Longrightarrow> s homeomorphic t"
  5555   unfolding homeomorphic_def by (metis homeomorphism_compact)
  5556 
  5557 text{* Preservation of topological properties.                                   *}
  5558 
  5559 lemma homeomorphic_compactness:
  5560  "s homeomorphic t ==> (compact s \<longleftrightarrow> compact t)"
  5561 unfolding homeomorphic_def homeomorphism_def
  5562 by (metis compact_continuous_image)
  5563 
  5564 text{* Results on translation, scaling etc.                                      *}
  5565 
  5566 lemma homeomorphic_scaling:
  5567   fixes s :: "'a::real_normed_vector set"
  5568   assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. c *\<^sub>R x) ` s)"
  5569   unfolding homeomorphic_minimal
  5570   apply(rule_tac x="\<lambda>x. c *\<^sub>R x" in exI)
  5571   apply(rule_tac x="\<lambda>x. (1 / c) *\<^sub>R x" in exI)
  5572   using assms apply auto
  5573   using continuous_on_cmul[OF continuous_on_id] by auto
  5574 
  5575 lemma homeomorphic_translation:
  5576   fixes s :: "'a::real_normed_vector set"
  5577   shows "s homeomorphic ((\<lambda>x. a + x) ` s)"
  5578   unfolding homeomorphic_minimal
  5579   apply(rule_tac x="\<lambda>x. a + x" in exI)
  5580   apply(rule_tac x="\<lambda>x. -a + x" in exI)
  5581   using continuous_on_add[OF continuous_on_const continuous_on_id] by auto
  5582 
  5583 lemma homeomorphic_affinity:
  5584   fixes s :: "'a::real_normed_vector set"
  5585   assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  5586 proof-
  5587   have *:"op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
  5588   show ?thesis
  5589     using homeomorphic_trans
  5590     using homeomorphic_scaling[OF assms, of s]
  5591     using homeomorphic_translation[of "(\<lambda>x. c *\<^sub>R x) ` s" a] unfolding * by auto
  5592 qed
  5593 
  5594 lemma homeomorphic_balls:
  5595   fixes a b ::"'a::real_normed_vector" (* FIXME: generalize to metric_space *)
  5596   assumes "0 < d"  "0 < e"
  5597   shows "(ball a d) homeomorphic  (ball b e)" (is ?th)
  5598         "(cball a d) homeomorphic (cball b e)" (is ?cth)
  5599 proof-
  5600   have *:"\<bar>e / d\<bar> > 0" "\<bar>d / e\<bar> >0" using assms using divide_pos_pos by auto
  5601   show ?th unfolding homeomorphic_minimal
  5602     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
  5603     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
  5604     using assms apply (auto simp add: dist_commute)
  5605     unfolding dist_norm
  5606     apply (auto simp add: pos_divide_less_eq mult_strict_left_mono)
  5607     unfolding continuous_on
  5608     by (intro ballI tendsto_intros, simp)+
  5609 next
  5610   have *:"\<bar>e / d\<bar> > 0" "\<bar>d / e\<bar> >0" using assms using divide_pos_pos by auto
  5611   show ?cth unfolding homeomorphic_minimal
  5612     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
  5613     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
  5614     using assms apply (auto simp add: dist_commute)
  5615     unfolding dist_norm
  5616     apply (auto simp add: pos_divide_le_eq)
  5617     unfolding continuous_on
  5618     by (intro ballI tendsto_intros, simp)+
  5619 qed
  5620 
  5621 text{* "Isometry" (up to constant bounds) of injective linear map etc.           *}
  5622 
  5623 lemma cauchy_isometric:
  5624   fixes x :: "nat \<Rightarrow> 'a::euclidean_space"
  5625   assumes e:"0 < e" and s:"subspace s" and f:"bounded_linear f" and normf:"\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)" and xs:"\<forall>n::nat. x n \<in> s" and cf:"Cauchy(f o x)"
  5626   shows "Cauchy x"
  5627 proof-
  5628   interpret f: bounded_linear f by fact
  5629   { fix d::real assume "d>0"
  5630     then obtain N where N:"\<forall>n\<ge>N. norm (f (x n) - f (x N)) < e * d"
  5631       using cf[unfolded cauchy o_def dist_norm, THEN spec[where x="e*d"]] and e and mult_pos_pos[of e d] by auto
  5632     { fix n assume "n\<ge>N"
  5633       hence "norm (f (x n - x N)) < e * d" using N[THEN spec[where x=n]] unfolding f.diff[THEN sym] by auto
  5634       moreover have "e * norm (x n - x N) \<le> norm (f (x n - x N))"
  5635         using subspace_sub[OF s, of "x n" "x N"] using xs[THEN spec[where x=N]] and xs[THEN spec[where x=n]]
  5636         using normf[THEN bspec[where x="x n - x N"]] by auto
  5637       ultimately have "norm (x n - x N) < d" using `e>0`
  5638         using mult_left_less_imp_less[of e "norm (x n - x N)" d] by auto   }
  5639     hence "\<exists>N. \<forall>n\<ge>N. norm (x n - x N) < d" by auto }
  5640   thus ?thesis unfolding cauchy and dist_norm by auto
  5641 qed
  5642 
  5643 lemma complete_isometric_image:
  5644   fixes f :: "'a::euclidean_space => 'b::euclidean_space"
  5645   assumes "0 < e" and s:"subspace s" and f:"bounded_linear f" and normf:"\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)" and cs:"complete s"
  5646   shows "complete(f ` s)"
  5647 proof-
  5648   { fix g assume as:"\<forall>n::nat. g n \<in> f ` s" and cfg:"Cauchy g"
  5649     then obtain x where "\<forall>n. x n \<in> s \<and> g n = f (x n)" 
  5650       using choice[of "\<lambda> n xa. xa \<in> s \<and> g n = f xa"] by auto
  5651     hence x:"\<forall>n. x n \<in> s"  "\<forall>n. g n = f (x n)" by auto
  5652     hence "f \<circ> x = g" unfolding fun_eq_iff by auto
  5653     then obtain l where "l\<in>s" and l:"(x ---> l) sequentially"
  5654       using cs[unfolded complete_def, THEN spec[where x="x"]]
  5655       using cauchy_isometric[OF `0<e` s f normf] and cfg and x(1) by auto
  5656     hence "\<exists>l\<in>f ` s. (g ---> l) sequentially"
  5657       using linear_continuous_at[OF f, unfolded continuous_at_sequentially, THEN spec[where x=x], of l]
  5658       unfolding `f \<circ> x = g` by auto  }
  5659   thus ?thesis unfolding complete_def by auto
  5660 qed
  5661 
  5662 lemma dist_0_norm:
  5663   fixes x :: "'a::real_normed_vector"
  5664   shows "dist 0 x = norm x"
  5665 unfolding dist_norm by simp
  5666 
  5667 lemma injective_imp_isometric: fixes f::"'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  5668   assumes s:"closed s"  "subspace s"  and f:"bounded_linear f" "\<forall>x\<in>s. (f x = 0) \<longrightarrow> (x = 0)"
  5669   shows "\<exists>e>0. \<forall>x\<in>s. norm (f x) \<ge> e * norm(x)"
  5670 proof(cases "s \<subseteq> {0::'a}")
  5671   case True
  5672   { fix x assume "x \<in> s"
  5673     hence "x = 0" using True by auto
  5674     hence "norm x \<le> norm (f x)" by auto  }
  5675   thus ?thesis by(auto intro!: exI[where x=1])
  5676 next
  5677   interpret f: bounded_linear f by fact
  5678   case False
  5679   then obtain a where a:"a\<noteq>0" "a\<in>s" by auto
  5680   from False have "s \<noteq> {}" by auto
  5681   let ?S = "{f x| x. (x \<in> s \<and> norm x = norm a)}"
  5682   let ?S' = "{x::'a. x\<in>s \<and> norm x = norm a}"
  5683   let ?S'' = "{x::'a. norm x = norm a}"
  5684 
  5685   have "?S'' = frontier(cball 0 (norm a))" unfolding frontier_cball and dist_norm by auto
  5686   hence "compact ?S''" using compact_frontier[OF compact_cball, of 0 "norm a"] by auto
  5687   moreover have "?S' = s \<inter> ?S''" by auto
  5688   ultimately have "compact ?S'" using closed_inter_compact[of s ?S''] using s(1) by auto
  5689   moreover have *:"f ` ?S' = ?S" by auto
  5690   ultimately have "compact ?S" using compact_continuous_image[OF linear_continuous_on[OF f(1)], of ?S'] by auto
  5691   hence "closed ?S" using compact_imp_closed by auto
  5692   moreover have "?S \<noteq> {}" using a by auto
  5693   ultimately obtain b' where "b'\<in>?S" "\<forall>y\<in>?S. norm b' \<le> norm y" using distance_attains_inf[of ?S 0] unfolding dist_0_norm by auto
  5694   then obtain b where "b\<in>s" and ba:"norm b = norm a" and b:"\<forall>x\<in>{x \<in> s. norm x = norm a}. norm (f b) \<le> norm (f x)" unfolding *[THEN sym] unfolding image_iff by auto
  5695 
  5696   let ?e = "norm (f b) / norm b"
  5697   have "norm b > 0" using ba and a and norm_ge_zero by auto
  5698   moreover have "norm (f b) > 0" using f(2)[THEN bspec[where x=b], OF `b\<in>s`] using `norm b >0` unfolding zero_less_norm_iff by auto
  5699   ultimately have "0 < norm (f b) / norm b" by(simp only: divide_pos_pos)
  5700   moreover
  5701   { fix x assume "x\<in>s"
  5702     hence "norm (f b) / norm b * norm x \<le> norm (f x)"
  5703     proof(cases "x=0")
  5704       case True thus "norm (f b) / norm b * norm x \<le> norm (f x)" by auto
  5705     next
  5706       case False
  5707       hence *:"0 < norm a / norm x" using `a\<noteq>0` unfolding zero_less_norm_iff[THEN sym] by(simp only: divide_pos_pos)
  5708       have "\<forall>c. \<forall>x\<in>s. c *\<^sub>R x \<in> s" using s[unfolded subspace_def] by auto
  5709       hence "(norm a / norm x) *\<^sub>R x \<in> {x \<in> s. norm x = norm a}" using `x\<in>s` and `x\<noteq>0` by auto
  5710       thus "norm (f b) / norm b * norm x \<le> norm (f x)" using b[THEN bspec[where x="(norm a / norm x) *\<^sub>R x"]]
  5711         unfolding f.scaleR and ba using `x\<noteq>0` `a\<noteq>0`
  5712         by (auto simp add: mult_commute pos_le_divide_eq pos_divide_le_eq)
  5713     qed }
  5714   ultimately
  5715   show ?thesis by auto
  5716 qed
  5717 
  5718 lemma closed_injective_image_subspace:
  5719   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  5720   assumes "subspace s" "bounded_linear f" "\<forall>x\<in>s. f x = 0 --> x = 0" "closed s"
  5721   shows "closed(f ` s)"
  5722 proof-
  5723   obtain e where "e>0" and e:"\<forall>x\<in>s. e * norm x \<le> norm (f x)" using injective_imp_isometric[OF assms(4,1,2,3)] by auto
  5724   show ?thesis using complete_isometric_image[OF `e>0` assms(1,2) e] and assms(4)
  5725     unfolding complete_eq_closed[THEN sym] by auto
  5726 qed
  5727 
  5728 subsection{* Some properties of a canonical subspace.                                  *}
  5729 
  5730 (** move **)
  5731 declare euclidean_component.zero[simp]  
  5732 
  5733 lemma subspace_substandard:
  5734   "subspace {x::'a::euclidean_space. (\<forall>i<DIM('a). P i \<longrightarrow> x$$i = 0)}"
  5735   unfolding subspace_def by(auto simp add: euclidean_simps)
  5736 
  5737 lemma closed_substandard:
  5738  "closed {x::'a::euclidean_space. \<forall>i<DIM('a). P i --> x$$i = 0}" (is "closed ?A")
  5739 proof-
  5740   let ?D = "{i. P i} \<inter> {..<DIM('a)}"
  5741   let ?Bs = "{{x::'a. inner (basis i) x = 0}| i. i \<in> ?D}"
  5742   { fix x
  5743     { assume "x\<in>?A"
  5744       hence x:"\<forall>i\<in>?D. x $$ i = 0" by auto
  5745       hence "x\<in> \<Inter> ?Bs" by(auto simp add: x euclidean_component_def) }
  5746     moreover
  5747     { assume x:"x\<in>\<Inter>?Bs"
  5748       { fix i assume i:"i \<in> ?D"
  5749         then obtain B where BB:"B \<in> ?Bs" and B:"B = {x::'a. inner (basis i) x = 0}" by auto
  5750         hence "x $$ i = 0" unfolding B using x unfolding euclidean_component_def by auto  }
  5751       hence "x\<in>?A" by auto }
  5752     ultimately have "x\<in>?A \<longleftrightarrow> x\<in> \<Inter>?Bs" .. }
  5753   hence "?A = \<Inter> ?Bs" by auto
  5754   thus ?thesis by(auto simp add: closed_Inter closed_hyperplane)
  5755 qed
  5756 
  5757 lemma dim_substandard: assumes "d\<subseteq>{..<DIM('a::euclidean_space)}"
  5758   shows "dim {x::'a::euclidean_space. \<forall>i<DIM('a). i \<notin> d \<longrightarrow> x$$i = 0} = card d" (is "dim ?A = _")
  5759 proof-
  5760   let ?D = "{..<DIM('a)}"
  5761   let ?B = "(basis::nat => 'a) ` d"
  5762   let ?bas = "basis::nat \<Rightarrow> 'a"
  5763   have "?B \<subseteq> ?A" by(auto simp add:basis_component)
  5764   moreover
  5765   { fix x::"'a" assume "x\<in>?A"
  5766     hence "finite d" "x\<in>?A" using assms by(auto intro:finite_subset)
  5767     hence "x\<in> span ?B"
  5768     proof(induct d arbitrary: x)
  5769       case empty hence "x=0" apply(subst euclidean_eq) by auto
  5770       thus ?case using subspace_0[OF subspace_span[of "{}"]] by auto
  5771     next
  5772       case (insert k F)
  5773       hence *:"\<forall>i<DIM('a). i \<notin> insert k F \<longrightarrow> x $$ i = 0" by auto
  5774       have **:"F \<subseteq> insert k F" by auto
  5775       def y \<equiv> "x - x$$k *\<^sub>R basis k"
  5776       have y:"x = y + (x$$k) *\<^sub>R basis k" unfolding y_def by auto
  5777       { fix i assume i':"i \<notin> F"
  5778         hence "y $$ i = 0" unfolding y_def 
  5779           using *[THEN spec[where x=i]] by(auto simp add: euclidean_simps basis_component) }
  5780       hence "y \<in> span (basis ` F)" using insert(3) by auto
  5781       hence "y \<in> span (basis ` (insert k F))"
  5782         using span_mono[of "?bas ` F" "?bas ` (insert k F)"]
  5783         using image_mono[OF **, of basis] using assms by auto
  5784       moreover
  5785       have "basis k \<in> span (?bas ` (insert k F))" by(rule span_superset, auto)
  5786       hence "x$$k *\<^sub>R basis k \<in> span (?bas ` (insert k F))"
  5787         using span_mul by auto
  5788       ultimately
  5789       have "y + x$$k *\<^sub>R basis k \<in> span (?bas ` (insert k F))"
  5790         using span_add by auto
  5791       thus ?case using y by auto
  5792     qed
  5793   }
  5794   hence "?A \<subseteq> span ?B" by auto
  5795   moreover
  5796   { fix x assume "x \<in> ?B"
  5797     hence "x\<in>{(basis i)::'a |i. i \<in> ?D}" using assms by auto  }
  5798   hence "independent ?B" using independent_mono[OF independent_basis, of ?B] and assms by auto
  5799   moreover
  5800   have "d \<subseteq> ?D" unfolding subset_eq using assms by auto
  5801   hence *:"inj_on (basis::nat\<Rightarrow>'a) d" using subset_inj_on[OF basis_inj, of "d"] by auto
  5802   have "card ?B = card d" unfolding card_image[OF *] by auto
  5803   ultimately show ?thesis using dim_unique[of "basis ` d" ?A] by auto
  5804 qed
  5805 
  5806 text{* Hence closure and completeness of all subspaces.                          *}
  5807 
  5808 lemma closed_subspace_lemma: "n \<le> card (UNIV::'n::finite set) \<Longrightarrow> \<exists>A::'n set. card A = n"
  5809 apply (induct n)
  5810 apply (rule_tac x="{}" in exI, simp)
  5811 apply clarsimp
  5812 apply (subgoal_tac "\<exists>x. x \<notin> A")
  5813 apply (erule exE)
  5814 apply (rule_tac x="insert x A" in exI, simp)
  5815 apply (subgoal_tac "A \<noteq> UNIV", auto)
  5816 done
  5817 
  5818 lemma closed_subspace: fixes s::"('a::euclidean_space) set"
  5819   assumes "subspace s" shows "closed s"
  5820 proof-
  5821   have *:"dim s \<le> DIM('a)" using dim_subset_UNIV by auto
  5822   def d \<equiv> "{..<dim s}" have t:"card d = dim s" unfolding d_def by auto
  5823   let ?t = "{x::'a. \<forall>i<DIM('a). i \<notin> d \<longrightarrow> x$$i = 0}"
  5824   have "\<exists>f. linear f \<and> f ` {x::'a. \<forall>i<DIM('a). i \<notin> d \<longrightarrow> x $$ i = 0} = s \<and>
  5825       inj_on f {x::'a. \<forall>i<DIM('a). i \<notin> d \<longrightarrow> x $$ i = 0}"
  5826     apply(rule subspace_isomorphism[OF subspace_substandard[of "\<lambda>i. i \<notin> d"]])
  5827     using dim_substandard[of d,where 'a='a] and t unfolding d_def using * assms by auto
  5828   then guess f apply-by(erule exE conjE)+ note f = this
  5829   interpret f: bounded_linear f using f unfolding linear_conv_bounded_linear by auto
  5830   have "\<forall>x\<in>?t. f x = 0 \<longrightarrow> x = 0" using f.zero using f(3)[unfolded inj_on_def]
  5831     by(erule_tac x=0 in ballE) auto
  5832   moreover have "closed ?t" using closed_substandard .
  5833   moreover have "subspace ?t" using subspace_substandard .
  5834   ultimately show ?thesis using closed_injective_image_subspace[of ?t f]
  5835     unfolding f(2) using f(1) unfolding linear_conv_bounded_linear by auto
  5836 qed
  5837 
  5838 lemma complete_subspace:
  5839   fixes s :: "('a::euclidean_space) set" shows "subspace s ==> complete s"
  5840   using complete_eq_closed closed_subspace
  5841   by auto
  5842 
  5843 lemma dim_closure:
  5844   fixes s :: "('a::euclidean_space) set"
  5845   shows "dim(closure s) = dim s" (is "?dc = ?d")
  5846 proof-
  5847   have "?dc \<le> ?d" using closure_minimal[OF span_inc, of s]
  5848     using closed_subspace[OF subspace_span, of s]
  5849     using dim_subset[of "closure s" "span s"] unfolding dim_span by auto
  5850   thus ?thesis using dim_subset[OF closure_subset, of s] by auto
  5851 qed
  5852 
  5853 subsection {* Affine transformations of intervals *}
  5854 
  5855 lemma real_affinity_le:
  5856  "0 < (m::'a::linordered_field) ==> (m * x + c \<le> y \<longleftrightarrow> x \<le> inverse(m) * y + -(c / m))"
  5857   by (simp add: field_simps inverse_eq_divide)
  5858 
  5859 lemma real_le_affinity:
  5860  "0 < (m::'a::linordered_field) ==> (y \<le> m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) \<le> x)"
  5861   by (simp add: field_simps inverse_eq_divide)
  5862 
  5863 lemma real_affinity_lt:
  5864  "0 < (m::'a::linordered_field) ==> (m * x + c < y \<longleftrightarrow> x < inverse(m) * y + -(c / m))"
  5865   by (simp add: field_simps inverse_eq_divide)
  5866 
  5867 lemma real_lt_affinity:
  5868  "0 < (m::'a::linordered_field) ==> (y < m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) < x)"
  5869   by (simp add: field_simps inverse_eq_divide)
  5870 
  5871 lemma real_affinity_eq:
  5872  "(m::'a::linordered_field) \<noteq> 0 ==> (m * x + c = y \<longleftrightarrow> x = inverse(m) * y + -(c / m))"
  5873   by (simp add: field_simps inverse_eq_divide)
  5874 
  5875 lemma real_eq_affinity:
  5876  "(m::'a::linordered_field) \<noteq> 0 ==> (y = m * x + c  \<longleftrightarrow> inverse(m) * y + -(c / m) = x)"
  5877   by (simp add: field_simps inverse_eq_divide)
  5878 
  5879 lemma image_affinity_interval: fixes m::real
  5880   fixes a b c :: "'a::ordered_euclidean_space"
  5881   shows "(\<lambda>x. m *\<^sub>R x + c) ` {a .. b} =
  5882             (if {a .. b} = {} then {}
  5883             else (if 0 \<le> m then {m *\<^sub>R a + c .. m *\<^sub>R b + c}
  5884             else {m *\<^sub>R b + c .. m *\<^sub>R a + c}))"
  5885 proof(cases "m=0")  
  5886   { fix x assume "x \<le> c" "c \<le> x"
  5887     hence "x=c" unfolding eucl_le[where 'a='a] apply-
  5888       apply(subst euclidean_eq) by (auto intro: order_antisym) }
  5889   moreover case True
  5890   moreover have "c \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}" unfolding True by(auto simp add: eucl_le[where 'a='a])
  5891   ultimately show ?thesis by auto
  5892 next
  5893   case False
  5894   { fix y assume "a \<le> y" "y \<le> b" "m > 0"
  5895     hence "m *\<^sub>R a + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R b + c"
  5896       unfolding eucl_le[where 'a='a] by(auto simp add: euclidean_simps)
  5897   } moreover
  5898   { fix y assume "a \<le> y" "y \<le> b" "m < 0"
  5899     hence "m *\<^sub>R b + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R a + c"
  5900       unfolding eucl_le[where 'a='a] by(auto simp add: mult_left_mono_neg euclidean_simps)
  5901   } moreover
  5902   { fix y assume "m > 0"  "m *\<^sub>R a + c \<le> y"  "y \<le> m *\<^sub>R b + c"
  5903     hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
  5904       unfolding image_iff Bex_def mem_interval eucl_le[where 'a='a]
  5905       apply(auto simp add: pth_3[symmetric] 
  5906         intro!: exI[where x="(1 / m) *\<^sub>R (y - c)"]) 
  5907       by(auto simp add: pos_le_divide_eq pos_divide_le_eq mult_commute diff_le_iff euclidean_simps)
  5908   } moreover
  5909   { fix y assume "m *\<^sub>R b + c \<le> y" "y \<le> m *\<^sub>R a + c" "m < 0"
  5910     hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
  5911       unfolding image_iff Bex_def mem_interval eucl_le[where 'a='a]
  5912       apply(auto simp add: pth_3[symmetric]
  5913         intro!: exI[where x="(1 / m) *\<^sub>R (y - c)"])
  5914       by(auto simp add: neg_le_divide_eq neg_divide_le_eq mult_commute diff_le_iff euclidean_simps)
  5915   }
  5916   ultimately show ?thesis using False by auto
  5917 qed
  5918 
  5919 lemma image_smult_interval:"(\<lambda>x. m *\<^sub>R (x::_::ordered_euclidean_space)) ` {a..b} =
  5920   (if {a..b} = {} then {} else if 0 \<le> m then {m *\<^sub>R a..m *\<^sub>R b} else {m *\<^sub>R b..m *\<^sub>R a})"
  5921   using image_affinity_interval[of m 0 a b] by auto
  5922 
  5923 subsection{* Banach fixed point theorem (not really topological...) *}
  5924 
  5925 lemma banach_fix:
  5926   assumes s:"complete s" "s \<noteq> {}" and c:"0 \<le> c" "c < 1" and f:"(f ` s) \<subseteq> s" and
  5927           lipschitz:"\<forall>x\<in>s. \<forall>y\<in>s. dist (f x) (f y) \<le> c * dist x y"
  5928   shows "\<exists>! x\<in>s. (f x = x)"
  5929 proof-
  5930   have "1 - c > 0" using c by auto
  5931 
  5932   from s(2) obtain z0 where "z0 \<in> s" by auto
  5933   def z \<equiv> "\<lambda>n. (f ^^ n) z0"
  5934   { fix n::nat
  5935     have "z n \<in> s" unfolding z_def
  5936     proof(induct n) case 0 thus ?case using `z0 \<in>s` by auto
  5937     next case Suc thus ?case using f by auto qed }
  5938   note z_in_s = this
  5939 
  5940   def d \<equiv> "dist (z 0) (z 1)"
  5941 
  5942   have fzn:"\<And>n. f (z n) = z (Suc n)" unfolding z_def by auto
  5943   { fix n::nat
  5944     have "dist (z n) (z (Suc n)) \<le> (c ^ n) * d"
  5945     proof(induct n)
  5946       case 0 thus ?case unfolding d_def by auto
  5947     next
  5948       case (Suc m)
  5949       hence "c * dist (z m) (z (Suc m)) \<le> c ^ Suc m * d"
  5950         using `0 \<le> c` using mult_left_mono[of "dist (z m) (z (Suc m))" "c ^ m * d" c] by auto
  5951       thus ?case using lipschitz[THEN bspec[where x="z m"], OF z_in_s, THEN bspec[where x="z (Suc m)"], OF z_in_s]
  5952         unfolding fzn and mult_le_cancel_left by auto
  5953     qed
  5954   } note cf_z = this
  5955 
  5956   { fix n m::nat
  5957     have "(1 - c) * dist (z m) (z (m+n)) \<le> (c ^ m) * d * (1 - c ^ n)"
  5958     proof(induct n)
  5959       case 0 show ?case by auto
  5960     next
  5961       case (Suc k)
  5962       have "(1 - c) * dist (z m) (z (m + Suc k)) \<le> (1 - c) * (dist (z m) (z (m + k)) + dist (z (m + k)) (z (Suc (m + k))))"
  5963         using dist_triangle and c by(auto simp add: dist_triangle)
  5964       also have "\<dots> \<le> (1 - c) * (dist (z m) (z (m + k)) + c ^ (m + k) * d)"
  5965         using cf_z[of "m + k"] and c by auto
  5966       also have "\<dots> \<le> c ^ m * d * (1 - c ^ k) + (1 - c) * c ^ (m + k) * d"
  5967         using Suc by (auto simp add: field_simps)
  5968       also have "\<dots> = (c ^ m) * (d * (1 - c ^ k) + (1 - c) * c ^ k * d)"
  5969         unfolding power_add by (auto simp add: field_simps)
  5970       also have "\<dots> \<le> (c ^ m) * d * (1 - c ^ Suc k)"
  5971         using c by (auto simp add: field_simps)
  5972       finally show ?case by auto
  5973     qed
  5974   } note cf_z2 = this
  5975   { fix e::real assume "e>0"
  5976     hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (z m) (z n) < e"
  5977     proof(cases "d = 0")
  5978       case True
  5979       have *: "\<And>x. ((1 - c) * x \<le> 0) = (x \<le> 0)" using `1 - c > 0`
  5980         by (metis mult_zero_left real_mult_commute real_mult_le_cancel_iff1)
  5981       from True have "\<And>n. z n = z0" using cf_z2[of 0] and c unfolding z_def
  5982         by (simp add: *)
  5983       thus ?thesis using `e>0` by auto
  5984     next
  5985       case False hence "d>0" unfolding d_def using zero_le_dist[of "z 0" "z 1"]
  5986         by (metis False d_def less_le)
  5987       hence "0 < e * (1 - c) / d" using `e>0` and `1-c>0`
  5988         using divide_pos_pos[of "e * (1 - c)" d] and mult_pos_pos[of e "1 - c"] by auto
  5989       then obtain N where N:"c ^ N < e * (1 - c) / d" using real_arch_pow_inv[of "e * (1 - c) / d" c] and c by auto
  5990       { fix m n::nat assume "m>n" and as:"m\<ge>N" "n\<ge>N"
  5991         have *:"c ^ n \<le> c ^ N" using `n\<ge>N` and c using power_decreasing[OF `n\<ge>N`, of c] by auto
  5992         have "1 - c ^ (m - n) > 0" using c and power_strict_mono[of c 1 "m - n"] using `m>n` by auto
  5993         hence **:"d * (1 - c ^ (m - n)) / (1 - c) > 0"
  5994           using mult_pos_pos[OF `d>0`, of "1 - c ^ (m - n)"]
  5995           using divide_pos_pos[of "d * (1 - c ^ (m - n))" "1 - c"]
  5996           using `0 < 1 - c` by auto
  5997 
  5998         have "dist (z m) (z n) \<le> c ^ n * d * (1 - c ^ (m - n)) / (1 - c)"
  5999           using cf_z2[of n "m - n"] and `m>n` unfolding pos_le_divide_eq[OF `1-c>0`]
  6000           by (auto simp add: mult_commute dist_commute)
  6001         also have "\<dots> \<le> c ^ N * d * (1 - c ^ (m - n)) / (1 - c)"
  6002           using mult_right_mono[OF * order_less_imp_le[OF **]]
  6003           unfolding mult_assoc by auto
  6004         also have "\<dots> < (e * (1 - c) / d) * d * (1 - c ^ (m - n)) / (1 - c)"
  6005           using mult_strict_right_mono[OF N **] unfolding mult_assoc by auto
  6006         also have "\<dots> = e * (1 - c ^ (m - n))" using c and `d>0` and `1 - c > 0` by auto
  6007         also have "\<dots> \<le> e" using c and `1 - c ^ (m - n) > 0` and `e>0` using mult_right_le_one_le[of e "1 - c ^ (m - n)"] by auto
  6008         finally have  "dist (z m) (z n) < e" by auto
  6009       } note * = this
  6010       { fix m n::nat assume as:"N\<le>m" "N\<le>n"
  6011         hence "dist (z n) (z m) < e"
  6012         proof(cases "n = m")
  6013           case True thus ?thesis using `e>0` by auto
  6014         next
  6015           case False thus ?thesis using as and *[of n m] *[of m n] unfolding nat_neq_iff by (auto simp add: dist_commute)
  6016         qed }
  6017       thus ?thesis by auto
  6018     qed
  6019   }
  6020   hence "Cauchy z" unfolding cauchy_def by auto
  6021   then obtain x where "x\<in>s" and x:"(z ---> x) sequentially" using s(1)[unfolded compact_def complete_def, THEN spec[where x=z]] and z_in_s by auto
  6022 
  6023   def e \<equiv> "dist (f x) x"
  6024   have "e = 0" proof(rule ccontr)
  6025     assume "e \<noteq> 0" hence "e>0" unfolding e_def using zero_le_dist[of "f x" x]
  6026       by (metis dist_eq_0_iff dist_nz e_def)
  6027     then obtain N where N:"\<forall>n\<ge>N. dist (z n) x < e / 2"
  6028       using x[unfolded Lim_sequentially, THEN spec[where x="e/2"]] by auto
  6029     hence N':"dist (z N) x < e / 2" by auto
  6030 
  6031     have *:"c * dist (z N) x \<le> dist (z N) x" unfolding mult_le_cancel_right2
  6032       using zero_le_dist[of "z N" x] and c
  6033       by (metis dist_eq_0_iff dist_nz order_less_asym less_le)
  6034     have "dist (f (z N)) (f x) \<le> c * dist (z N) x" using lipschitz[THEN bspec[where x="z N"], THEN bspec[where x=x]]
  6035       using z_in_s[of N] `x\<in>s` using c by auto
  6036     also have "\<dots> < e / 2" using N' and c using * by auto
  6037     finally show False unfolding fzn
  6038       using N[THEN spec[where x="Suc N"]] and dist_triangle_half_r[of "z (Suc N)" "f x" e x]
  6039       unfolding e_def by auto
  6040   qed
  6041   hence "f x = x" unfolding e_def by auto
  6042   moreover
  6043   { fix y assume "f y = y" "y\<in>s"
  6044     hence "dist x y \<le> c * dist x y" using lipschitz[THEN bspec[where x=x], THEN bspec[where x=y]]
  6045       using `x\<in>s` and `f x = x` by auto
  6046     hence "dist x y = 0" unfolding mult_le_cancel_right1
  6047       using c and zero_le_dist[of x y] by auto
  6048     hence "y = x" by auto
  6049   }
  6050   ultimately show ?thesis using `x\<in>s` by blast+
  6051 qed
  6052 
  6053 subsection{* Edelstein fixed point theorem.                                            *}
  6054 
  6055 lemma edelstein_fix:
  6056   fixes s :: "'a::real_normed_vector set"
  6057   assumes s:"compact s" "s \<noteq> {}" and gs:"(g ` s) \<subseteq> s"
  6058       and dist:"\<forall>x\<in>s. \<forall>y\<in>s. x \<noteq> y \<longrightarrow> dist (g x) (g y) < dist x y"
  6059   shows "\<exists>! x\<in>s. g x = x"
  6060 proof(cases "\<exists>x\<in>s. g x \<noteq> x")
  6061   obtain x where "x\<in>s" using s(2) by auto
  6062   case False hence g:"\<forall>x\<in>s. g x = x" by auto
  6063   { fix y assume "y\<in>s"
  6064     hence "x = y" using `x\<in>s` and dist[THEN bspec[where x=x], THEN bspec[where x=y]]
  6065       unfolding g[THEN bspec[where x=x], OF `x\<in>s`]
  6066       unfolding g[THEN bspec[where x=y], OF `y\<in>s`] by auto  }
  6067   thus ?thesis using `x\<in>s` and g by blast+
  6068 next
  6069   case True
  6070   then obtain x where [simp]:"x\<in>s" and "g x \<noteq> x" by auto
  6071   { fix x y assume "x \<in> s" "y \<in> s"
  6072     hence "dist (g x) (g y) \<le> dist x y"
  6073       using dist[THEN bspec[where x=x], THEN bspec[where x=y]] by auto } note dist' = this
  6074   def y \<equiv> "g x"
  6075   have [simp]:"y\<in>s" unfolding y_def using gs[unfolded image_subset_iff] and `x\<in>s` by blast
  6076   def f \<equiv> "\<lambda>n. g ^^ n"
  6077   have [simp]:"\<And>n z. g (f n z) = f (Suc n) z" unfolding f_def by auto
  6078   have [simp]:"\<And>z. f 0 z = z" unfolding f_def by auto
  6079   { fix n::nat and z assume "z\<in>s"
  6080     have "f n z \<in> s" unfolding f_def
  6081     proof(induct n)
  6082       case 0 thus ?case using `z\<in>s` by simp
  6083     next
  6084       case (Suc n) thus ?case using gs[unfolded image_subset_iff] by auto
  6085     qed } note fs = this
  6086   { fix m n ::nat assume "m\<le>n"
  6087     fix w z assume "w\<in>s" "z\<in>s"
  6088     have "dist (f n w) (f n z) \<le> dist (f m w) (f m z)" using `m\<le>n`
  6089     proof(induct n)
  6090       case 0 thus ?case by auto
  6091     next
  6092       case (Suc n)
  6093       thus ?case proof(cases "m\<le>n")
  6094         case True thus ?thesis using Suc(1)
  6095           using dist'[OF fs fs, OF `w\<in>s` `z\<in>s`, of n n] by auto
  6096       next
  6097         case False hence mn:"m = Suc n" using Suc(2) by simp
  6098         show ?thesis unfolding mn  by auto
  6099       qed
  6100     qed } note distf = this
  6101 
  6102   def h \<equiv> "\<lambda>n. (f n x, f n y)"
  6103   let ?s2 = "s \<times> s"
  6104   obtain l r where "l\<in>?s2" and r:"subseq r" and lr:"((h \<circ> r) ---> l) sequentially"
  6105     using compact_Times [OF s(1) s(1), unfolded compact_def, THEN spec[where x=h]] unfolding  h_def
  6106     using fs[OF `x\<in>s`] and fs[OF `y\<in>s`] by blast
  6107   def a \<equiv> "fst l" def b \<equiv> "snd l"
  6108   have lab:"l = (a, b)" unfolding a_def b_def by simp
  6109   have [simp]:"a\<in>s" "b\<in>s" unfolding a_def b_def using `l\<in>?s2` by auto
  6110 
  6111   have lima:"((fst \<circ> (h \<circ> r)) ---> a) sequentially"
  6112    and limb:"((snd \<circ> (h \<circ> r)) ---> b) sequentially"
  6113     using lr
  6114     unfolding o_def a_def b_def by (simp_all add: tendsto_intros)
  6115 
  6116   { fix n::nat
  6117     have *:"\<And>fx fy (x::'a) y. dist fx fy \<le> dist x y \<Longrightarrow> \<not> (dist (fx - fy) (a - b) < dist a b - dist x y)" unfolding dist_norm by norm
  6118     { fix x y :: 'a
  6119       have "dist (-x) (-y) = dist x y" unfolding dist_norm
  6120         using norm_minus_cancel[of "x - y"] by (auto simp add: uminus_add_conv_diff) } note ** = this
  6121 
  6122     { assume as:"dist a b > dist (f n x) (f n y)"
  6123       then obtain Na Nb where "\<forall>m\<ge>Na. dist (f (r m) x) a < (dist a b - dist (f n x) (f n y)) / 2"
  6124         and "\<forall>m\<ge>Nb. dist (f (r m) y) b < (dist a b - dist (f n x) (f n y)) / 2"
  6125         using lima limb unfolding h_def Lim_sequentially by (fastsimp simp del: less_divide_eq_number_of1)
  6126       hence "dist (f (r (Na + Nb + n)) x - f (r (Na + Nb + n)) y) (a - b) < dist a b - dist (f n x) (f n y)"
  6127         apply(erule_tac x="Na+Nb+n" in allE)
  6128         apply(erule_tac x="Na+Nb+n" in allE) apply simp
  6129         using dist_triangle_add_half[of a "f (r (Na + Nb + n)) x" "dist a b - dist (f n x) (f n y)"
  6130           "-b"  "- f (r (Na + Nb + n)) y"]
  6131         unfolding ** by (auto simp add: algebra_simps dist_commute)
  6132       moreover
  6133       have "dist (f (r (Na + Nb + n)) x - f (r (Na + Nb + n)) y) (a - b) \<ge> dist a b - dist (f n x) (f n y)"
  6134         using distf[of n "r (Na+Nb+n)", OF _ `x\<in>s` `y\<in>s`]
  6135         using subseq_bigger[OF r, of "Na+Nb+n"]
  6136         using *[of "f (r (Na + Nb + n)) x" "f (r (Na + Nb + n)) y" "f n x" "f n y"] by auto
  6137       ultimately have False by simp
  6138     }
  6139     hence "dist a b \<le> dist (f n x) (f n y)" by(rule ccontr)auto }
  6140   note ab_fn = this
  6141 
  6142   have [simp]:"a = b" proof(rule ccontr)
  6143     def e \<equiv> "dist a b - dist (g a) (g b)"
  6144     assume "a\<noteq>b" hence "e > 0" unfolding e_def using dist by fastsimp
  6145     hence "\<exists>n. dist (f n x) a < e/2 \<and> dist (f n y) b < e/2"
  6146       using lima limb unfolding Lim_sequentially
  6147       apply (auto elim!: allE[where x="e/2"]) apply(rule_tac x="r (max N Na)" in exI) unfolding h_def by fastsimp
  6148     then obtain n where n:"dist (f n x) a < e/2 \<and> dist (f n y) b < e/2" by auto
  6149     have "dist (f (Suc n) x) (g a) \<le> dist (f n x) a"
  6150       using dist[THEN bspec[where x="f n x"], THEN bspec[where x="a"]] and fs by auto
  6151     moreover have "dist (f (Suc n) y) (g b) \<le> dist (f n y) b"
  6152       using dist[THEN bspec[where x="f n y"], THEN bspec[where x="b"]] and fs by auto
  6153     ultimately have "dist (f (Suc n) x) (g a) + dist (f (Suc n) y) (g b) < e" using n by auto
  6154     thus False unfolding e_def using ab_fn[of "Suc n"] by norm
  6155   qed
  6156 
  6157   have [simp]:"\<And>n. f (Suc n) x = f n y" unfolding f_def y_def by(induct_tac n)auto
  6158   { fix x y assume "x\<in>s" "y\<in>s" moreover
  6159     fix e::real assume "e>0" ultimately
  6160     have "dist y x < e \<longrightarrow> dist (g y) (g x) < e" using dist by fastsimp }
  6161   hence "continuous_on s g" unfolding continuous_on_iff by auto
  6162 
  6163   hence "((snd \<circ> h \<circ> r) ---> g a) sequentially" unfolding continuous_on_sequentially
  6164     apply (rule allE[where x="\<lambda>n. (fst \<circ> h \<circ> r) n"]) apply (erule ballE[where x=a])
  6165     using lima unfolding h_def o_def using fs[OF `x\<in>s`] by (auto simp add: y_def)
  6166   hence "g a = a" using tendsto_unique[OF trivial_limit_sequentially limb, of "g a"]
  6167     unfolding `a=b` and o_assoc by auto
  6168   moreover
  6169   { fix x assume "x\<in>s" "g x = x" "x\<noteq>a"
  6170     hence "False" using dist[THEN bspec[where x=a], THEN bspec[where x=x]]
  6171       using `g a = a` and `a\<in>s` by auto  }
  6172   ultimately show "\<exists>!x\<in>s. g x = x" using `a\<in>s` by blast
  6173 qed
  6174 
  6175 
  6176 (** TODO move this someplace else within this theory **)
  6177 instance euclidean_space \<subseteq> banach ..
  6178 
  6179 end