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