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