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