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