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