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