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