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