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