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