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