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