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