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