merged;
authorwenzelm
Fri, 28 Dec 2018 19:06:33 +0100
changeset 69525 8e7134f1f585
parent 69519 0563419bf022 (diff)
parent 69524 fa94f2b2a877 (current diff)
child 69526 5574d504cf36
merged;
--- a/etc/settings	Fri Dec 28 16:58:27 2018 +0100
+++ b/etc/settings	Fri Dec 28 19:06:33 2018 +0100
@@ -153,9 +153,9 @@
 
 ISABELLE_STACK_ROOT="$ISABELLE_HOME_USER/stack"
 
-ISABELLE_STACK_RESOLVER="lts-12.13"
+ISABELLE_STACK_RESOLVER="lts-13.0"
 
-ISABELLE_GHC_VERSION="ghc-8.4.3"
+ISABELLE_GHC_VERSION="ghc-8.6.3"
 
 
 ###
--- a/src/HOL/Analysis/Abstract_Topology.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Abstract_Topology.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -1979,7 +1979,7 @@
 
 definition connected_space :: "'a topology \<Rightarrow> bool" where
   "connected_space X \<equiv>
-        ~(\<exists>E1 E2. openin X E1 \<and> openin X E2 \<and>
+        \<not>(\<exists>E1 E2. openin X E1 \<and> openin X E2 \<and>
                   topspace X \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
 
 definition connectedin :: "'a topology \<Rightarrow> 'a set \<Rightarrow> bool" where
@@ -2093,10 +2093,10 @@
 lemma connectedin_closedin:
    "connectedin X S \<longleftrightarrow>
         S \<subseteq> topspace X \<and>
-        ~(\<exists>E1 E2. closedin X E1 \<and> closedin X E2 \<and>
+        \<not>(\<exists>E1 E2. closedin X E1 \<and> closedin X E2 \<and>
                   S \<subseteq> (E1 \<union> E2) \<and>
                   (E1 \<inter> E2 \<inter> S = {}) \<and>
-                  ~(E1 \<inter> S = {}) \<and> ~(E2 \<inter> S = {}))"
+                  \<not>(E1 \<inter> S = {}) \<and> \<not>(E2 \<inter> S = {}))"
 proof -
   have *: "(\<exists>E1:: 'a set. \<exists>E2:: 'a set. (\<exists>T1:: 'a set. P1 T1 \<and> E1 = f1 T1) \<and> (\<exists>T2:: 'a set. P2 T2 \<and> E2 = f2 T2) \<and>
              R E1 E2) \<longleftrightarrow> (\<exists>T1 T2. P1 T1 \<and> P2 T2 \<and> R(f1 T1) (f2 T2))" for P1 f1 P2 f2 R
@@ -2269,7 +2269,7 @@
   unfolding connectedin_eq_not_separated_subset  by blast
 
 lemma connectedin_nonseparated_union:
-   "\<lbrakk>connectedin X S; connectedin X T; ~separatedin X S T\<rbrakk> \<Longrightarrow> connectedin X (S \<union> T)"
+   "\<lbrakk>connectedin X S; connectedin X T; \<not>separatedin X S T\<rbrakk> \<Longrightarrow> connectedin X (S \<union> T)"
   apply (simp add: connectedin_eq_not_separated_subset, auto)
     apply (metis (no_types, hide_lams) Diff_subset_conv Diff_triv disjoint_iff_not_equal separatedin_mono sup_commute)
   apply (metis (no_types, hide_lams) Diff_subset_conv Diff_triv disjoint_iff_not_equal separatedin_mono separatedin_sym sup_commute)
--- a/src/HOL/Analysis/Arcwise_Connected.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Arcwise_Connected.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,11 +2,10 @@
     Authors:    LC Paulson, based on material from HOL Light
 *)
 
-section \<open>Arcwise-connected sets\<close>
+section \<open>Arcwise-Connected Sets\<close>
 
 theory Arcwise_Connected
-  imports Path_Connected Ordered_Euclidean_Space "HOL-Computational_Algebra.Primes"
-
+imports Path_Connected Ordered_Euclidean_Space "HOL-Computational_Algebra.Primes"
 begin
 
 subsection%important \<open>The Brouwer reduction theorem\<close>
--- a/src/HOL/Analysis/Ball_Volume.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Ball_Volume.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -3,7 +3,7 @@
    Author:   Manuel Eberl, TU München
 *)
 
-section \<open>The volume of an $n$-dimensional ball\<close>
+section \<open>The Volume of an $n$-Dimensional Ball\<close>
 
 theory Ball_Volume
   imports Gamma_Function Lebesgue_Integral_Substitution
--- a/src/HOL/Analysis/Binary_Product_Measure.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Binary_Product_Measure.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,7 +2,7 @@
     Author:     Johannes Hölzl, TU München
 *)
 
-section%important \<open>Binary product measures\<close>
+section%important \<open>Binary Product Measure\<close>
 
 theory Binary_Product_Measure
 imports Nonnegative_Lebesgue_Integration
--- a/src/HOL/Analysis/Borel_Space.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Borel_Space.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -3,7 +3,7 @@
     Author:     Armin Heller, TU München
 *)
 
-section%important \<open>Borel spaces\<close>
+section%important \<open>Borel Space\<close>
 
 theory Borel_Space
 imports
--- a/src/HOL/Analysis/Brouwer_Fixpoint.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Brouwer_Fixpoint.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -1060,7 +1060,7 @@
 
 text \<open>More properties of ARs, ANRs and ENRs\<close>
 
-lemma not_AR_empty [simp]: "~ AR({})"
+lemma not_AR_empty [simp]: "\<not> AR({})"
   by (auto simp: AR_def)
 
 lemma ENR_empty [simp]: "ENR {}"
@@ -3823,7 +3823,7 @@
 lemma non_extensible_Borsuk_map:
   fixes a :: "'a :: euclidean_space"
   assumes "compact s" and cin: "c \<in> components(- s)" and boc: "bounded c" and "a \<in> c"
-    shows "~ (\<exists>g. continuous_on (s \<union> c) g \<and>
+    shows "\<not> (\<exists>g. continuous_on (s \<union> c) g \<and>
                   g ` (s \<union> c) \<subseteq> sphere 0 1 \<and>
                   (\<forall>x \<in> s. g x = inverse(norm(x - a)) *\<^sub>R (x - a)))"
 proof -
@@ -4534,7 +4534,7 @@
   fixes a :: "'a :: euclidean_space"
   assumes "compact S" and "a \<notin> S"
    shows "bounded (connected_component_set (- S) a) \<longleftrightarrow>
-          ~(\<exists>c. homotopic_with (\<lambda>x. True) S (sphere 0 1)
+          \<not>(\<exists>c. homotopic_with (\<lambda>x. True) S (sphere 0 1)
                                (\<lambda>x. inverse(norm(x - a)) *\<^sub>R (x - a)) (\<lambda>x. c))"
    (is "?lhs = ?rhs")
 proof (cases "S = {}")
@@ -4550,9 +4550,9 @@
     by (simp add: \<open>a \<notin> S\<close>)
   obtain r where "r>0" and r: "S \<subseteq> ball 0 r"
     using bounded_subset_ballD \<open>bounded S\<close> by blast
-  have "~ ?rhs \<longleftrightarrow> ~ ?lhs"
+  have "\<not> ?rhs \<longleftrightarrow> \<not> ?lhs"
   proof
-    assume notr: "~ ?rhs"
+    assume notr: "\<not> ?rhs"
     have nog: "\<nexists>g. continuous_on (S \<union> connected_component_set (- S) a) g \<and>
                    g ` (S \<union> connected_component_set (- S) a) \<subseteq> sphere 0 1 \<and>
                    (\<forall>x\<in>S. g x = (x - a) /\<^sub>R norm (x - a))"
@@ -4564,12 +4564,12 @@
       using notr
       by (auto simp: nullhomotopic_into_sphere_extension
                  [OF \<open>closed S\<close> continuous_on_Borsuk_map [OF \<open>a \<notin> S\<close>] False s01])
-    with \<open>a \<notin> S\<close> show  "~ ?lhs"
+    with \<open>a \<notin> S\<close> show  "\<not> ?lhs"
       apply (clarsimp simp: Borsuk_map_into_sphere [of a S, symmetric] dest!: nog)
       apply (drule_tac x=g in spec)
       using continuous_on_subset by fastforce 
   next
-    assume "~ ?lhs"
+    assume "\<not> ?lhs"
     then obtain b where b: "b \<in> connected_component_set (- S) a" and "r \<le> norm b"
       using bounded_iff linear by blast
     then have bnot: "b \<notin> ball 0 r"
@@ -4593,7 +4593,7 @@
     ultimately have "homotopic_with (\<lambda>x. True) S (sphere 0 1)
                          (\<lambda>x. (x - a) /\<^sub>R norm (x - a)) (\<lambda>x. c)"
       by (meson homotopic_with_subset_left homotopic_with_trans r)
-    then show "~ ?rhs"
+    then show "\<not> ?rhs"
       by blast
   qed
   then show ?thesis by blast
--- a/src/HOL/Analysis/Caratheodory.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Caratheodory.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -6,7 +6,7 @@
 section%important \<open>Caratheodory Extension Theorem\<close>
 
 theory Caratheodory
-  imports Measure_Space
+imports Measure_Space
 begin
 
 text \<open>
--- a/src/HOL/Analysis/Cartesian_Euclidean_Space.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Cartesian_Euclidean_Space.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -651,7 +651,7 @@
 
 lemma%unimportant Lim_component_eq_cart:
   fixes f :: "'a \<Rightarrow> real^'n"
-  assumes net: "(f \<longlongrightarrow> l) net" "~(trivial_limit net)" and ev:"eventually (\<lambda>x. f(x)$i = b) net"
+  assumes net: "(f \<longlongrightarrow> l) net" "\<not> trivial_limit net" and ev:"eventually (\<lambda>x. f(x)$i = b) net"
   shows "l$i = b"
   using ev[unfolded order_eq_iff eventually_conj_iff] and
     Lim_component_ge_cart[OF net, of b i] and
@@ -1060,7 +1060,7 @@
 
 lemma%unimportant matrix_nonfull_linear_equations_eq:
   fixes A :: "real^'n^'m"
-  shows "(\<exists>x. (x \<noteq> 0) \<and> A *v x = 0) \<longleftrightarrow> ~(rank A = CARD('n))"
+  shows "(\<exists>x. (x \<noteq> 0) \<and> A *v x = 0) \<longleftrightarrow> rank A \<noteq> CARD('n)"
   by (meson matrix_left_invertible_injective full_rank_injective matrix_left_invertible_ker)
 
 lemma%unimportant rank_eq_0: "rank A = 0 \<longleftrightarrow> A = 0" and rank_0 [simp]: "rank (0::real^'n^'m) = 0"
--- a/src/HOL/Analysis/Cauchy_Integral_Theorem.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Cauchy_Integral_Theorem.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -1,4 +1,4 @@
-section \<open>Complex path integrals and Cauchy's integral theorem\<close>
+section \<open>Complex Path Integrals and Cauchy's Integral Theorem\<close>
 
 text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2015)\<close>
 
@@ -941,7 +941,7 @@
   case True then show ?thesis
     by (simp add: assms contour_integral_unique has_contour_integral_integral has_contour_integral_reversepath)
 next
-  case False then have "~ f contour_integrable_on (reversepath g)"
+  case False then have "\<not> f contour_integrable_on (reversepath g)"
     by (simp add: assms contour_integrable_reversepath_eq)
   with False show ?thesis by (simp add: not_integrable_contour_integral)
 qed
@@ -5518,7 +5518,7 @@
       and ul_f: "uniform_limit (path_image \<gamma>) f l F"
       and noleB: "\<And>t. t \<in> {0..1} \<Longrightarrow> norm (vector_derivative \<gamma> (at t)) \<le> B"
       and \<gamma>: "valid_path \<gamma>"
-      and [simp]: "~ (trivial_limit F)"
+      and [simp]: "\<not> trivial_limit F"
   shows "l contour_integrable_on \<gamma>" "((\<lambda>n. contour_integral \<gamma> (f n)) \<longlongrightarrow> contour_integral \<gamma> l) F"
 proof -
   have "0 \<le> B" by (meson noleB [of 0] atLeastAtMost_iff norm_ge_zero order_refl order_trans zero_le_one)
@@ -5586,7 +5586,7 @@
 corollary%unimportant contour_integral_uniform_limit_circlepath:
   assumes "\<forall>\<^sub>F n::'a in F. (f n) contour_integrable_on (circlepath z r)"
       and "uniform_limit (sphere z r) f l F"
-      and "~ (trivial_limit F)" "0 < r"
+      and "\<not> trivial_limit F" "0 < r"
     shows "l contour_integrable_on (circlepath z r)"
           "((\<lambda>n. contour_integral (circlepath z r) (f n)) \<longlongrightarrow> contour_integral (circlepath z r) l) F"
   using assms by (auto simp: vector_derivative_circlepath norm_mult intro!: contour_integral_uniform_limit)
@@ -6550,7 +6550,7 @@
 lemma holomorphic_uniform_limit:
   assumes cont: "eventually (\<lambda>n. continuous_on (cball z r) (f n) \<and> (f n) holomorphic_on ball z r) F"
       and ulim: "uniform_limit (cball z r) f g F"
-      and F:  "~ trivial_limit F"
+      and F:  "\<not> trivial_limit F"
   obtains "continuous_on (cball z r) g" "g holomorphic_on ball z r"
 proof (cases r "0::real" rule: linorder_cases)
   case less then show ?thesis by (force simp: ball_empty less_imp_le continuous_on_def holomorphic_on_def intro: that)
@@ -6620,7 +6620,7 @@
   assumes cont: "eventually (\<lambda>n. continuous_on (cball z r) (f n) \<and>
                                (\<forall>w \<in> ball z r. ((f n) has_field_derivative (f' n w)) (at w))) F"
       and ulim: "uniform_limit (cball z r) f g F"
-      and F:  "~ trivial_limit F" and "0 < r"
+      and F:  "\<not> trivial_limit F" and "0 < r"
   obtains g' where
       "continuous_on (cball z r) g"
       "\<And>w. w \<in> ball z r \<Longrightarrow> (g has_field_derivative (g' w)) (at w) \<and> ((\<lambda>n. f' n w) \<longlongrightarrow> g' w) F"
--- a/src/HOL/Analysis/Complex_Analysis_Basics.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Complex_Analysis_Basics.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -212,7 +212,7 @@
 
 lemma real_lim:
   fixes l::complex
-  assumes "(f \<longlongrightarrow> l) F" and "~(trivial_limit F)" and "eventually P F" and "\<And>a. P a \<Longrightarrow> f a \<in> \<real>"
+  assumes "(f \<longlongrightarrow> l) F" and "\<not> trivial_limit F" and "eventually P F" and "\<And>a. P a \<Longrightarrow> f a \<in> \<real>"
   shows  "l \<in> \<real>"
 proof (rule Lim_in_closed_set[OF closed_complex_Reals _ assms(2,1)])
   show "eventually (\<lambda>x. f x \<in> \<real>) F"
--- a/src/HOL/Analysis/Complex_Transcendental.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Complex_Transcendental.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -1133,21 +1133,21 @@
 lemma cnj_tan: "cnj(tan z) = tan(cnj z)"
   by (simp add: cnj_cos cnj_sin tan_def)
 
-lemma field_differentiable_at_tan: "~(cos z = 0) \<Longrightarrow> tan field_differentiable at z"
+lemma field_differentiable_at_tan: "cos z \<noteq> 0 \<Longrightarrow> tan field_differentiable at z"
   unfolding field_differentiable_def
   using DERIV_tan by blast
 
-lemma field_differentiable_within_tan: "~(cos z = 0)
+lemma field_differentiable_within_tan: "cos z \<noteq> 0
          \<Longrightarrow> tan field_differentiable (at z within s)"
   using field_differentiable_at_tan field_differentiable_at_within by blast
 
-lemma continuous_within_tan: "~(cos z = 0) \<Longrightarrow> continuous (at z within s) tan"
+lemma continuous_within_tan: "cos z \<noteq> 0 \<Longrightarrow> continuous (at z within s) tan"
   using continuous_at_imp_continuous_within isCont_tan by blast
 
-lemma continuous_on_tan [continuous_intros]: "(\<And>z. z \<in> s \<Longrightarrow> ~(cos z = 0)) \<Longrightarrow> continuous_on s tan"
+lemma continuous_on_tan [continuous_intros]: "(\<And>z. z \<in> s \<Longrightarrow> cos z \<noteq> 0) \<Longrightarrow> continuous_on s tan"
   by (simp add: continuous_at_imp_continuous_on)
 
-lemma holomorphic_on_tan: "(\<And>z. z \<in> s \<Longrightarrow> ~(cos z = 0)) \<Longrightarrow> tan holomorphic_on s"
+lemma holomorphic_on_tan: "(\<And>z. z \<in> s \<Longrightarrow> cos z \<noteq> 0) \<Longrightarrow> tan holomorphic_on s"
   by (simp add: field_differentiable_within_tan holomorphic_on_def)
 
 
@@ -1670,7 +1670,7 @@
 
 lemma Ln_minus:
   assumes "z \<noteq> 0"
-    shows "Ln(-z) = (if Im(z) \<le> 0 \<and> ~(Re(z) < 0 \<and> Im(z) = 0)
+    shows "Ln(-z) = (if Im(z) \<le> 0 \<and> \<not>(Re(z) < 0 \<and> Im(z) = 0)
                      then Ln(z) + \<i> * pi
                      else Ln(z) - \<i> * pi)" (is "_ = ?rhs")
   using Im_Ln_le_pi [of z] mpi_less_Im_Ln [of z] assms
--- a/src/HOL/Analysis/Conformal_Mappings.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Conformal_Mappings.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -1,4 +1,4 @@
-section \<open>Conformal Mappings and Consequences of Cauchy's integral theorem\<close>
+section \<open>Conformal Mappings and Consequences of Cauchy's Integral Theorem\<close>
 
 text\<open>By John Harrison et al.  Ported from HOL Light by L C Paulson (2016)\<close>
 
@@ -359,7 +359,7 @@
   assumes holf: "f holomorphic_on S"
       and S: "open S" and "connected S"
       and "open U" and "U \<subseteq> S"
-      and fne: "~ f constant_on S"
+      and fne: "\<not> f constant_on S"
     shows "open (f ` U)"
 proof -
   have *: "open (f ` U)"
@@ -468,7 +468,7 @@
   assumes holf: "f holomorphic_on S"
       and S: "open S"
       and "open U" "U \<subseteq> S"
-      and fnc: "\<And>X. \<lbrakk>open X; X \<subseteq> S; X \<noteq> {}\<rbrakk> \<Longrightarrow> ~ f constant_on X"
+      and fnc: "\<And>X. \<lbrakk>open X; X \<subseteq> S; X \<noteq> {}\<rbrakk> \<Longrightarrow> \<not> f constant_on X"
     shows "open (f ` U)"
 proof -
   have "S = \<Union>(components S)" by simp
@@ -518,7 +518,7 @@
   assume "\<not> f constant_on S"
   then have "open (f ` U)"
     using open_mapping_thm assms by blast
-  moreover have "~ open (f ` U)"
+  moreover have "\<not> open (f ` U)"
   proof -
     have "\<exists>t. cmod (f \<xi> - t) < e \<and> t \<notin> f ` U" if "0 < e" for e
       apply (rule_tac x="if 0 < Re(f \<xi>) then f \<xi> + (e/2) else f \<xi> - (e/2)" in exI)
@@ -735,7 +735,7 @@
 lemma holomorphic_factor_zero_nonconstant:
   assumes holf: "f holomorphic_on S" and S: "open S" "connected S"
       and "\<xi> \<in> S" "f \<xi> = 0"
-      and nonconst: "~ f constant_on S"
+      and nonconst: "\<not> f constant_on S"
    obtains g r n
       where "0 < n"  "0 < r"  "ball \<xi> r \<subseteq> S"
             "g holomorphic_on ball \<xi> r"
@@ -1156,7 +1156,7 @@
   obtain r where "r > 0" "ball \<xi> r \<subseteq> S" "inj_on f (ball \<xi> r)"
     by (blast intro: that has_complex_derivative_locally_injective [OF assms])
   then have \<xi>: "\<xi> \<in> ball \<xi> r" by simp
-  then have nc: "~ f constant_on ball \<xi> r"
+  then have nc: "\<not> f constant_on ball \<xi> r"
     using \<open>inj_on f (ball \<xi> r)\<close> injective_not_constant by fastforce
   have holf': "f holomorphic_on ball \<xi> r"
     using \<open>ball \<xi> r \<subseteq> S\<close> holf holomorphic_on_subset by blast
@@ -2132,7 +2132,7 @@
       case False
       then have "t > 0"
         using 2 by (force simp: zero_less_mult_iff)
-      have "~ ball a t \<subseteq> s \<Longrightarrow> ball a t \<inter> frontier s \<noteq> {}"
+      have "\<not> ball a t \<subseteq> s \<Longrightarrow> ball a t \<inter> frontier s \<noteq> {}"
         apply (rule connected_Int_frontier [of "ball a t" s], simp_all)
         using \<open>0 < t\<close> \<open>a \<in> s\<close> centre_in_ball apply blast
         done
@@ -3260,7 +3260,7 @@
       moreover have "isolated_singularity_at h z"
         unfolding isolated_singularity_at_def h_def
         apply (rule exI[where x=e])
-        using e_holo e_nz \<open>e>0\<close> by (metis Topology_Euclidean_Space.open_ball analytic_on_open 
+        using e_holo e_nz \<open>e>0\<close> by (metis open_ball analytic_on_open 
             holomorphic_on_inverse open_delete)
       ultimately show ?thesis
         using P_exist[of h] by auto
@@ -3482,7 +3482,7 @@
 
     have [intro]: "fp \<midarrow>z\<rightarrow>fp z" "gp \<midarrow>z\<rightarrow>gp z"
         using fr(1) \<open>fr>0\<close> gr(1) \<open>gr>0\<close>
-        by (meson Topology_Euclidean_Space.open_ball ball_subset_cball centre_in_ball 
+        by (meson open_ball ball_subset_cball centre_in_ball 
             continuous_on_eq_continuous_at continuous_within holomorphic_on_imp_continuous_on 
             holomorphic_on_subset)+
     have ?thesis when "fn+gn>0" 
@@ -3958,7 +3958,7 @@
       apply (elim Lim_transform_within_open[where s="ball z r"])
       using r by auto
     moreover have "g \<midarrow>z\<rightarrow>g z"
-      by (metis (mono_tags, lifting) Topology_Euclidean_Space.open_ball at_within_open_subset 
+      by (metis (mono_tags, lifting) open_ball at_within_open_subset 
           ball_subset_cball centre_in_ball continuous_on holomorphic_on_imp_continuous_on r(1,3) subsetCE)
     ultimately have "(\<lambda>w. (g w * (w - z) powr of_int n) / g w) \<midarrow>z\<rightarrow> f z/g z"
       apply (rule_tac tendsto_divide)
@@ -4088,7 +4088,7 @@
     assume " \<not> n < 0"
     define c where "c=(if n=0 then g z else 0)"
     have [simp]:"g \<midarrow>z\<rightarrow> g z" 
-      by (metis Topology_Euclidean_Space.open_ball at_within_open ball_subset_cball centre_in_ball 
+      by (metis open_ball at_within_open ball_subset_cball centre_in_ball 
             continuous_on holomorphic_on_imp_continuous_on holomorphic_on_subset r(1) r(3) )
     have "\<forall>\<^sub>F x in at z. f x = g x * (x - z) ^ nat n"
       unfolding eventually_at_topological
@@ -4154,7 +4154,7 @@
       then show "LIM w at z. w - z :> at 0" 
         unfolding filterlim_at by (auto intro:tendsto_eq_intros)
       show "isolated_singularity_at g z" 
-        by (meson Diff_subset Topology_Euclidean_Space.open_ball analytic_on_holomorphic 
+        by (meson Diff_subset open_ball analytic_on_holomorphic 
             assms(1,2,3) holomorphic_on_subset isolated_singularity_at_def openE)
     qed
     then show "not_essential f z"
@@ -4590,7 +4590,7 @@
     assume "\<not> (\<exists>\<^sub>F w in at z. g w \<noteq> 0)"
     then have "\<forall>\<^sub>F w in nhds z. g w = 0"
       unfolding eventually_at eventually_nhds frequently_at using \<open>g z = 0\<close> 
-      by (metis Topology_Euclidean_Space.open_ball UNIV_I centre_in_ball dist_commute mem_ball)
+      by (metis open_ball UNIV_I centre_in_ball dist_commute mem_ball)
     then have "deriv g z = deriv (\<lambda>_. 0) z"
       by (intro deriv_cong_ev) auto
     then have "deriv g z = 0" by auto
--- a/src/HOL/Analysis/Connected.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Connected.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -3280,7 +3280,7 @@
 lemma continuous_on_cases_local:
      "\<lbrakk>closedin (subtopology euclidean (s \<union> t)) s; closedin (subtopology euclidean (s \<union> t)) t;
        continuous_on s f; continuous_on t g;
-       \<And>x. \<lbrakk>x \<in> s \<and> ~P x \<or> x \<in> t \<and> P x\<rbrakk> \<Longrightarrow> f x = g x\<rbrakk>
+       \<And>x. \<lbrakk>x \<in> s \<and> \<not>P x \<or> x \<in> t \<and> P x\<rbrakk> \<Longrightarrow> f x = g x\<rbrakk>
       \<Longrightarrow> continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
   by (rule continuous_on_Un_local) (auto intro: continuous_on_eq)
 
@@ -3519,7 +3519,7 @@
 lemma homeomorphism_sym: "homeomorphism S t f g = homeomorphism t S g f"
   by (force simp: homeomorphism_def)
 
-definition homeomorphic :: "'a::topological_space set \<Rightarrow> 'b::topological_space set \<Rightarrow> bool"
+definition%important homeomorphic :: "'a::topological_space set \<Rightarrow> 'b::topological_space set \<Rightarrow> bool"
     (infixr "homeomorphic" 60)
   where "s homeomorphic t \<equiv> (\<exists>f g. homeomorphism s t f g)"
 
@@ -4983,7 +4983,7 @@
     shows "connected (S \<union> T)"
 proof -
   have *: "\<lbrakk>\<And>x y. P x y \<longleftrightarrow> P y x; \<And>x y. P x y \<Longrightarrow> S \<subseteq> x \<or> S \<subseteq> y;
-            \<And>x y. \<lbrakk>P x y; S \<subseteq> x\<rbrakk> \<Longrightarrow> False\<rbrakk> \<Longrightarrow> ~(\<exists>x y. (P x y))" for P
+            \<And>x y. \<lbrakk>P x y; S \<subseteq> x\<rbrakk> \<Longrightarrow> False\<rbrakk> \<Longrightarrow> \<not>(\<exists>x y. (P x y))" for P
     by metis
   show ?thesis
     unfolding connected_closedin_eq
--- a/src/HOL/Analysis/Continuous_Extension.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Continuous_Extension.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,7 +2,7 @@
     Authors:    LC Paulson, based on material from HOL Light
 *)
 
-section \<open>Continuous extensions of functions: Urysohn's lemma, Dugundji extension theorem, Tietze\<close>
+section \<open>Continuous Extensions of Functions\<close>
 
 theory Continuous_Extension
 imports Starlike
@@ -112,7 +112,9 @@
 qed
 
 
-subsection\<open>Urysohn's lemma (for Euclidean spaces, where the proof is easy using distances)\<close>
+subsection\<open>Urysohn's Lemma for Euclidean Spaces\<close>
+
+text \<open>For Euclidean spaces the proof is easy using distances.\<close>
 
 lemma Urysohn_both_ne:
   assumes US: "closedin (subtopology euclidean U) S"
@@ -130,7 +132,7 @@
     using \<open>T \<noteq> {}\<close>  UT setdist_eq_0_closedin  by auto
   have sdpos: "0 < setdist {x} S + setdist {x} T" if "x \<in> U" for x
   proof -
-    have "~ (setdist {x} S = 0 \<and> setdist {x} T = 0)"
+    have "\<not> (setdist {x} S = 0 \<and> setdist {x} T = 0)"
       using assms by (metis IntI empty_iff setdist_eq_0_closedin that)
     then show ?thesis
       by (metis add.left_neutral add.right_neutral add_pos_pos linorder_neqE_linordered_idom not_le setdist_pos_le)
@@ -295,10 +297,9 @@
   using assms by (auto intro: Urysohn_local [of UNIV S T a b])
 
 
-subsection\<open>The Dugundji extension theorem and Tietze variants as corollaries\<close>
+subsection\<open>Dugundji's Extension Theorem and Tietze Variants\<close>
 
-text%important\<open>J. Dugundji. An extension of Tietze's theorem. Pacific J. Math. Volume 1, Number 3 (1951), 353-367.
-https://projecteuclid.org/euclid.pjm/1103052106\<close>
+text \<open>See \cite{dugundji}.\<close>
 
 theorem Dugundji:
   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_inner"
@@ -486,7 +487,7 @@
   using assms
 by (auto simp: image_subset_iff intro: Dugundji [of "cball 0 B" U S f])
 
-corollary Tietze_closed_interval:
+corollary%unimportant Tietze_closed_interval:
   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   assumes "continuous_on S f"
       and "closedin (subtopology euclidean U) S"
@@ -497,7 +498,7 @@
 apply (rule Dugundji [of "cbox a b" U S f])
 using assms by auto
 
-corollary Tietze_closed_interval_1:
+corollary%unimportant Tietze_closed_interval_1:
   fixes f :: "'a::euclidean_space \<Rightarrow> real"
   assumes "continuous_on S f"
       and "closedin (subtopology euclidean U) S"
@@ -508,7 +509,7 @@
 apply (rule Dugundji [of "cbox a b" U S f])
 using assms by (auto simp: image_subset_iff)
 
-corollary Tietze_open_interval:
+corollary%unimportant Tietze_open_interval:
   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
   assumes "continuous_on S f"
       and "closedin (subtopology euclidean U) S"
@@ -519,7 +520,7 @@
 apply (rule Dugundji [of "box a b" U S f])
 using assms by auto
 
-corollary Tietze_open_interval_1:
+corollary%unimportant Tietze_open_interval_1:
   fixes f :: "'a::euclidean_space \<Rightarrow> real"
   assumes "continuous_on S f"
       and "closedin (subtopology euclidean U) S"
@@ -530,7 +531,7 @@
 apply (rule Dugundji [of "box a b" U S f])
 using assms by (auto simp: image_subset_iff)
 
-corollary Tietze_unbounded:
+corollary%unimportant Tietze_unbounded:
   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::real_inner"
   assumes "continuous_on S f"
       and "closedin (subtopology euclidean U) S"
--- a/src/HOL/Analysis/Continuum_Not_Denumerable.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Continuum_Not_Denumerable.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -3,7 +3,7 @@
     Author:     Johannes Hölzl, TU München
 *)
 
-section \<open>Non-denumerability of the Continuum\<close>
+section \<open>Non-Denumerability of the Continuum\<close>
 
 theory Continuum_Not_Denumerable
 imports
--- a/src/HOL/Analysis/Convex_Euclidean_Space.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Convex_Euclidean_Space.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -6,7 +6,7 @@
    Author:     Johannes Hoelzl, TU Muenchen
 *)
 
-section \<open>Convex sets, functions and related things\<close>
+section \<open>Convex Sets and Functions\<close>
 
 theory Convex_Euclidean_Space
 imports
@@ -1343,7 +1343,9 @@
   by (simp add: affine_def algebra_simps) (metis distrib_right mult.left_neutral)
 
 
-subsubsection%unimportant \<open>Some explicit formulations (from Lars Schewe)\<close>
+subsubsection%unimportant \<open>Some explicit formulations\<close>
+
+text "Formalized by Lars Schewe."
 
 lemma affine:
   fixes V::"'a::real_vector set"
@@ -2070,7 +2072,9 @@
 qed
 
 
-subsection \<open>Affine dependence and consequential theorems (from Lars Schewe)\<close>
+subsection \<open>Affine dependence and consequential theorems\<close>
+
+text "Formalized by Lars Schewe."
 
 definition%important affine_dependent :: "'a::real_vector set \<Rightarrow> bool"
   where "affine_dependent s \<longleftrightarrow> (\<exists>x\<in>s. x \<in> affine hull (s - {x}))"
@@ -2082,11 +2086,11 @@
 done
 
 lemma affine_independent_subset:
-  shows "\<lbrakk>~ affine_dependent t; s \<subseteq> t\<rbrakk> \<Longrightarrow> ~ affine_dependent s"
+  shows "\<lbrakk>\<not> affine_dependent t; s \<subseteq> t\<rbrakk> \<Longrightarrow> \<not> affine_dependent s"
 by (metis affine_dependent_subset)
 
 lemma affine_independent_Diff:
-   "~ affine_dependent s \<Longrightarrow> ~ affine_dependent(s - t)"
+   "\<not> affine_dependent s \<Longrightarrow> \<not> affine_dependent(s - t)"
 by (meson Diff_subset affine_dependent_subset)
 
 proposition affine_dependent_explicit:
@@ -2601,7 +2605,9 @@
 qed (use assms in \<open>auto simp: convex_explicit\<close>)
 
 
-subsubsection%unimportant \<open>Another formulation from Lars Schewe\<close>
+subsubsection%unimportant \<open>Another formulation\<close>
+
+text "Formalized by Lars Schewe."
 
 lemma convex_hull_explicit:
   fixes p :: "'a::real_vector set"
@@ -2631,9 +2637,9 @@
     }
     moreover
     have "(\<Sum>v\<in>y ` {1..k}. sum u {i \<in> {1..k}. y i = v}) = 1"
-      unfolding sum_image_gen[OF fin, symmetric] using obt(2) by auto
+      unfolding sum.image_gen[OF fin, symmetric] using obt(2) by auto
     moreover have "(\<Sum>v\<in>y ` {1..k}. sum u {i \<in> {1..k}. y i = v} *\<^sub>R v) = x"
-      using sum_image_gen[OF fin, of "\<lambda>i. u i *\<^sub>R y i" y, symmetric]
+      using sum.image_gen[OF fin, of "\<lambda>i. u i *\<^sub>R y i" y, symmetric]
       unfolding scaleR_left.sum using obt(3) by auto
     ultimately
     have "\<exists>S u. finite S \<and> S \<subseteq> p \<and> (\<forall>x\<in>S. 0 \<le> u x) \<and> sum u S = 1 \<and> (\<Sum>v\<in>S. u v *\<^sub>R v) = x"
@@ -2677,8 +2683,8 @@
         by (auto simp: sum_constant_scaleR)
     }
     then have "(\<Sum>x = 1..card S. u (f x)) = 1" "(\<Sum>i = 1..card S. u (f i) *\<^sub>R f i) = y"
-      unfolding sum_image_gen[OF *(1), of "\<lambda>x. u (f x) *\<^sub>R f x" f]
-        and sum_image_gen[OF *(1), of "\<lambda>x. u (f x)" f]
+      unfolding sum.image_gen[OF *(1), of "\<lambda>x. u (f x) *\<^sub>R f x" f]
+        and sum.image_gen[OF *(1), of "\<lambda>x. u (f x)" f]
       unfolding f
       using sum.cong [of S S "\<lambda>y. (\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x) *\<^sub>R f x)" "\<lambda>v. u v *\<^sub>R v"]
       using sum.cong [of S S "\<lambda>y. (\<Sum>x\<in>{x \<in> {1..card S}. f x = y}. u (f x))" u]
@@ -3427,7 +3433,7 @@
 
 lemma affine_independent_iff_card:
     fixes s :: "'a::euclidean_space set"
-    shows "~ affine_dependent s \<longleftrightarrow> finite s \<and> aff_dim s = int(card s) - 1"
+    shows "\<not> affine_dependent s \<longleftrightarrow> finite s \<and> aff_dim s = int(card s) - 1"
   apply (rule iffI)
   apply (simp add: aff_dim_affine_independent aff_independent_finite)
   by (metis affine_basis_exists [of s] aff_dim_unique card_subset_eq diff_add_cancel of_nat_eq_iff)
@@ -3652,7 +3658,7 @@
 
 lemma affine_independent_card_dim_diffs:
   fixes S :: "'a :: euclidean_space set"
-  assumes "~ affine_dependent S" "a \<in> S"
+  assumes "\<not> affine_dependent S" "a \<in> S"
     shows "card S = dim {x - a|x. x \<in> S} + 1"
 proof -
   have 1: "{b - a|b. b \<in> (S - {a})} \<subseteq> {x - a|x. x \<in> S}" by auto
@@ -3766,7 +3772,7 @@
   show ?thesis
   proof safe
     assume 0: "aff_dim S = 0"
-    have "~ {a,b} \<subseteq> S" if "b \<noteq> a" for b
+    have "\<not> {a,b} \<subseteq> S" if "b \<noteq> a" for b
       by (metis "0" aff_dim_2 aff_dim_subset not_one_le_zero that)
     then show "\<exists>a. S = {a}"
       using \<open>a \<in> S\<close> by blast
@@ -3798,7 +3804,7 @@
 
 lemma disjoint_affine_hull:
   fixes s :: "'n::euclidean_space set"
-  assumes "~ affine_dependent s" "t \<subseteq> s" "u \<subseteq> s" "t \<inter> u = {}"
+  assumes "\<not> affine_dependent s" "t \<subseteq> s" "u \<subseteq> s" "t \<inter> u = {}"
     shows "(affine hull t) \<inter> (affine hull u) = {}"
 proof -
   have "finite s" using assms by (simp add: aff_independent_finite)
@@ -3813,7 +3819,7 @@
     have [simp]: "s \<inter> t = t" "s \<inter> - t \<inter> u = u" using assms by auto
     have "sum c s = 0"
       by (simp add: c_def comm_monoid_add_class.sum.If_cases \<open>finite s\<close> sum_negf)
-    moreover have "~ (\<forall>v\<in>s. c v = 0)"
+    moreover have "\<not> (\<forall>v\<in>s. c v = 0)"
       by (metis (no_types) IntD1 \<open>s \<inter> t = t\<close> a1 c_def sum_not_0 zero_neq_one)
     moreover have "(\<Sum>v\<in>s. c v *\<^sub>R v) = 0"
       by (simp add: c_def if_smult sum_negf
@@ -5747,7 +5753,9 @@
 qed auto
 
 
-subsection \<open>Radon's theorem (from Lars Schewe)\<close>
+subsection \<open>Radon's theorem\<close>
+
+text "Formalized by Lars Schewe."
 
 lemma radon_ex_lemma:
   assumes "finite c" "affine_dependent c"
--- a/src/HOL/Analysis/Cross3.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Cross3.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -99,7 +99,7 @@
   by (force simp add: axis_def cross3_simps)+
 
 lemma  cross_basis_nonzero:
-  "u \<noteq> 0 \<Longrightarrow> ~(u \<times> axis 1 1 = 0) \<or> ~(u \<times> axis 2 1 = 0) \<or> ~(u \<times> axis 3 1 = 0)"
+  "u \<noteq> 0 \<Longrightarrow> u \<times> axis 1 1 \<noteq> 0 \<or> u \<times> axis 2 1 \<noteq> 0 \<or> u \<times> axis 3 1 \<noteq> 0"
   by (clarsimp simp add: axis_def cross3_simps) (metis vector_3 exhaust_3)
 
 lemma  cross_dot_cancel:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Analysis/Elementary_Topology.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -0,0 +1,5098 @@
+(*  Author:     L C Paulson, University of Cambridge
+    Author:     Amine Chaieb, University of Cambridge
+    Author:     Robert Himmelmann, TU Muenchen
+    Author:     Brian Huffman, Portland State University
+*)
+
+section \<open>Elementary Topology\<close>
+
+theory Elementary_Topology
+imports
+  "HOL-Library.Indicator_Function"
+  "HOL-Library.Countable_Set"
+  "HOL-Library.FuncSet"
+  "HOL-Library.Set_Idioms"
+  "HOL-Library.Infinite_Set"
+  Product_Vector
+begin
+
+(* FIXME: move elsewhere *)
+
+lemma approachable_lt_le: "(\<exists>(d::real) > 0. \<forall>x. f x < d \<longrightarrow> P x) \<longleftrightarrow> (\<exists>d>0. \<forall>x. f x \<le> d \<longrightarrow> P x)"
+  apply auto
+  apply (rule_tac x="d/2" in exI)
+  apply auto
+  done
+
+lemma approachable_lt_le2:  \<comment> \<open>like the above, but pushes aside an extra formula\<close>
+    "(\<exists>(d::real) > 0. \<forall>x. Q x \<longrightarrow> f x < d \<longrightarrow> P x) \<longleftrightarrow> (\<exists>d>0. \<forall>x. f x \<le> d \<longrightarrow> Q x \<longrightarrow> P x)"
+  apply auto
+  apply (rule_tac x="d/2" in exI, auto)
+  done
+
+lemma triangle_lemma:
+  fixes x y z :: real
+  assumes x: "0 \<le> x"
+    and y: "0 \<le> y"
+    and z: "0 \<le> z"
+    and xy: "x\<^sup>2 \<le> y\<^sup>2 + z\<^sup>2"
+  shows "x \<le> y + z"
+proof -
+  have "y\<^sup>2 + z\<^sup>2 \<le> y\<^sup>2 + 2 * y * z + z\<^sup>2"
+    using z y by simp
+  with xy have th: "x\<^sup>2 \<le> (y + z)\<^sup>2"
+    by (simp add: power2_eq_square field_simps)
+  from y z have yz: "y + z \<ge> 0"
+    by arith
+  from power2_le_imp_le[OF th yz] show ?thesis .
+qed
+
+definition (in monoid_add) support_on :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'b set"
+  where "support_on s f = {x\<in>s. f x \<noteq> 0}"
+
+lemma in_support_on: "x \<in> support_on s f \<longleftrightarrow> x \<in> s \<and> f x \<noteq> 0"
+  by (simp add: support_on_def)
+
+lemma support_on_simps[simp]:
+  "support_on {} f = {}"
+  "support_on (insert x s) f =
+    (if f x = 0 then support_on s f else insert x (support_on s f))"
+  "support_on (s \<union> t) f = support_on s f \<union> support_on t f"
+  "support_on (s \<inter> t) f = support_on s f \<inter> support_on t f"
+  "support_on (s - t) f = support_on s f - support_on t f"
+  "support_on (f ` s) g = f ` (support_on s (g \<circ> f))"
+  unfolding support_on_def by auto
+
+lemma support_on_cong:
+  "(\<And>x. x \<in> s \<Longrightarrow> f x = 0 \<longleftrightarrow> g x = 0) \<Longrightarrow> support_on s f = support_on s g"
+  by (auto simp: support_on_def)
+
+lemma support_on_if: "a \<noteq> 0 \<Longrightarrow> support_on A (\<lambda>x. if P x then a else 0) = {x\<in>A. P x}"
+  by (auto simp: support_on_def)
+
+lemma support_on_if_subset: "support_on A (\<lambda>x. if P x then a else 0) \<subseteq> {x \<in> A. P x}"
+  by (auto simp: support_on_def)
+
+lemma finite_support[intro]: "finite S \<Longrightarrow> finite (support_on S f)"
+  unfolding support_on_def by auto
+
+(* TODO: is supp_sum really needed? TODO: Generalize to Finite_Set.fold *)
+definition (in comm_monoid_add) supp_sum :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
+  where "supp_sum f S = (\<Sum>x\<in>support_on S f. f x)"
+
+lemma supp_sum_empty[simp]: "supp_sum f {} = 0"
+  unfolding supp_sum_def by auto
+
+lemma supp_sum_insert[simp]:
+  "finite (support_on S f) \<Longrightarrow>
+    supp_sum f (insert x S) = (if x \<in> S then supp_sum f S else f x + supp_sum f S)"
+  by (simp add: supp_sum_def in_support_on insert_absorb)
+
+lemma supp_sum_divide_distrib: "supp_sum f A / (r::'a::field) = supp_sum (\<lambda>n. f n / r) A"
+  by (cases "r = 0")
+     (auto simp: supp_sum_def sum_divide_distrib intro!: sum.cong support_on_cong)
+
+(*END OF SUPPORT, ETC.*)
+
+lemma image_affinity_interval:
+  fixes c :: "'a::ordered_real_vector"
+  shows "((\<lambda>x. m *\<^sub>R x + c) ` {a..b}) = 
+           (if {a..b}={} then {}
+            else if 0 \<le> m then {m *\<^sub>R a + c .. m  *\<^sub>R b + c}
+            else {m *\<^sub>R b + c .. m *\<^sub>R a + c})"
+         (is "?lhs = ?rhs")
+proof (cases "m=0")
+  case True
+  then show ?thesis
+    by force
+next
+  case False
+  show ?thesis
+  proof
+    show "?lhs \<subseteq> ?rhs"
+      by (auto simp: scaleR_left_mono scaleR_left_mono_neg)
+    show "?rhs \<subseteq> ?lhs"
+    proof (clarsimp, intro conjI impI subsetI)
+      show "\<lbrakk>0 \<le> m; a \<le> b; x \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}\<rbrakk>
+            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
+        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
+        using False apply (auto simp: le_diff_eq pos_le_divideRI)
+        using diff_le_eq pos_le_divideR_eq by force
+      show "\<lbrakk>\<not> 0 \<le> m; a \<le> b;  x \<in> {m *\<^sub>R b + c..m *\<^sub>R a + c}\<rbrakk>
+            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
+        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
+        apply (auto simp: diff_le_eq neg_le_divideR_eq)
+        using diff_eq_diff_less_eq linordered_field_class.sign_simps(11) minus_diff_eq not_less scaleR_le_cancel_left_neg by fastforce
+    qed
+  qed
+qed
+
+lemma countable_PiE:
+  "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (Pi\<^sub>E I F)"
+  by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)
+
+lemma open_sums:
+  fixes T :: "('b::real_normed_vector) set"
+  assumes "open S \<or> open T"
+  shows "open (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
+  using assms
+proof
+  assume S: "open S"
+  show ?thesis
+  proof (clarsimp simp: open_dist)
+    fix x y
+    assume "x \<in> S" "y \<in> T"
+    with S obtain e where "e > 0" and e: "\<And>x'. dist x' x < e \<Longrightarrow> x' \<in> S"
+      by (auto simp: open_dist)
+    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
+      by (metis \<open>y \<in> T\<close> diff_add_cancel dist_add_cancel2)
+    then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
+      using \<open>0 < e\<close> \<open>x \<in> S\<close> by blast
+  qed
+next
+  assume T: "open T"
+  show ?thesis
+  proof (clarsimp simp: open_dist)
+    fix x y
+    assume "x \<in> S" "y \<in> T"
+    with T obtain e where "e > 0" and e: "\<And>x'. dist x' y < e \<Longrightarrow> x' \<in> T"
+      by (auto simp: open_dist)
+    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
+      by (metis \<open>x \<in> S\<close> add_diff_cancel_left' add_diff_eq diff_diff_add dist_norm)
+    then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
+      using \<open>0 < e\<close> \<open>y \<in> T\<close> by blast
+  qed
+qed
+
+
+subsection \<open>Topological Basis\<close>
+
+context topological_space
+begin
+
+definition%important "topological_basis B \<longleftrightarrow>
+  (\<forall>b\<in>B. open b) \<and> (\<forall>x. open x \<longrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
+
+lemma topological_basis:
+  "topological_basis B \<longleftrightarrow> (\<forall>x. open x \<longleftrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
+  unfolding topological_basis_def
+  apply safe
+     apply fastforce
+    apply fastforce
+   apply (erule_tac x=x in allE, simp)
+   apply (rule_tac x="{x}" in exI, auto)
+  done
+
+lemma topological_basis_iff:
+  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
+  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'))"
+    (is "_ \<longleftrightarrow> ?rhs")
+proof safe
+  fix O' and x::'a
+  assume H: "topological_basis B" "open O'" "x \<in> O'"
+  then have "(\<exists>B'\<subseteq>B. \<Union>B' = O')" by (simp add: topological_basis_def)
+  then obtain B' where "B' \<subseteq> B" "O' = \<Union>B'" by auto
+  then show "\<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'" using H by auto
+next
+  assume H: ?rhs
+  show "topological_basis B"
+    using assms unfolding topological_basis_def
+  proof safe
+    fix O' :: "'a set"
+    assume "open O'"
+    with H obtain f where "\<forall>x\<in>O'. f x \<in> B \<and> x \<in> f x \<and> f x \<subseteq> O'"
+      by (force intro: bchoice simp: Bex_def)
+    then show "\<exists>B'\<subseteq>B. \<Union>B' = O'"
+      by (auto intro: exI[where x="{f x |x. x \<in> O'}"])
+  qed
+qed
+
+lemma topological_basisI:
+  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
+    and "\<And>O' x. open O' \<Longrightarrow> x \<in> O' \<Longrightarrow> \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'"
+  shows "topological_basis B"
+  using assms by (subst topological_basis_iff) auto
+
+lemma topological_basisE:
+  fixes O'
+  assumes "topological_basis B"
+    and "open O'"
+    and "x \<in> O'"
+  obtains B' where "B' \<in> B" "x \<in> B'" "B' \<subseteq> O'"
+proof atomize_elim
+  from assms have "\<And>B'. B'\<in>B \<Longrightarrow> open B'"
+    by (simp add: topological_basis_def)
+  with topological_basis_iff assms
+  show  "\<exists>B'. B' \<in> B \<and> x \<in> B' \<and> B' \<subseteq> O'"
+    using assms by (simp add: Bex_def)
+qed
+
+lemma topological_basis_open:
+  assumes "topological_basis B"
+    and "X \<in> B"
+  shows "open X"
+  using assms by (simp add: topological_basis_def)
+
+lemma topological_basis_imp_subbasis:
+  assumes B: "topological_basis B"
+  shows "open = generate_topology B"
+proof (intro ext iffI)
+  fix S :: "'a set"
+  assume "open S"
+  with B obtain B' where "B' \<subseteq> B" "S = \<Union>B'"
+    unfolding topological_basis_def by blast
+  then show "generate_topology B S"
+    by (auto intro: generate_topology.intros dest: topological_basis_open)
+next
+  fix S :: "'a set"
+  assume "generate_topology B S"
+  then show "open S"
+    by induct (auto dest: topological_basis_open[OF B])
+qed
+
+lemma basis_dense:
+  fixes B :: "'a set set"
+    and f :: "'a set \<Rightarrow> 'a"
+  assumes "topological_basis B"
+    and choosefrom_basis: "\<And>B'. B' \<noteq> {} \<Longrightarrow> f B' \<in> B'"
+  shows "\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>B' \<in> B. f B' \<in> X)"
+proof (intro allI impI)
+  fix X :: "'a set"
+  assume "open X" and "X \<noteq> {}"
+  from topological_basisE[OF \<open>topological_basis B\<close> \<open>open X\<close> choosefrom_basis[OF \<open>X \<noteq> {}\<close>]]
+  obtain B' where "B' \<in> B" "f X \<in> B'" "B' \<subseteq> X" .
+  then show "\<exists>B'\<in>B. f B' \<in> X"
+    by (auto intro!: choosefrom_basis)
+qed
+
+end
+
+lemma topological_basis_prod:
+  assumes A: "topological_basis A"
+    and B: "topological_basis B"
+  shows "topological_basis ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))"
+  unfolding topological_basis_def
+proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric])
+  fix S :: "('a \<times> 'b) set"
+  assume "open S"
+  then show "\<exists>X\<subseteq>A \<times> B. (\<Union>(a,b)\<in>X. a \<times> b) = S"
+  proof (safe intro!: exI[of _ "{x\<in>A \<times> B. fst x \<times> snd x \<subseteq> S}"])
+    fix x y
+    assume "(x, y) \<in> S"
+    from open_prod_elim[OF \<open>open S\<close> this]
+    obtain a b where a: "open a""x \<in> a" and b: "open b" "y \<in> b" and "a \<times> b \<subseteq> S"
+      by (metis mem_Sigma_iff)
+    moreover
+    from A a obtain A0 where "A0 \<in> A" "x \<in> A0" "A0 \<subseteq> a"
+      by (rule topological_basisE)
+    moreover
+    from B b obtain B0 where "B0 \<in> B" "y \<in> B0" "B0 \<subseteq> b"
+      by (rule topological_basisE)
+    ultimately show "(x, y) \<in> (\<Union>(a, b)\<in>{X \<in> A \<times> B. fst X \<times> snd X \<subseteq> S}. a \<times> b)"
+      by (intro UN_I[of "(A0, B0)"]) auto
+  qed auto
+qed (metis A B topological_basis_open open_Times)
+
+
+subsection \<open>Countable Basis\<close>
+
+locale%important countable_basis =
+  fixes B :: "'a::topological_space set set"
+  assumes is_basis: "topological_basis B"
+    and countable_basis: "countable B"
+begin
+
+lemma open_countable_basis_ex:
+  assumes "open X"
+  shows "\<exists>B' \<subseteq> B. X = \<Union>B'"
+  using assms countable_basis is_basis
+  unfolding topological_basis_def by blast
+
+lemma open_countable_basisE:
+  assumes "open X"
+  obtains B' where "B' \<subseteq> B" "X = \<Union>B'"
+  using assms open_countable_basis_ex
+  by atomize_elim simp
+
+lemma countable_dense_exists:
+  "\<exists>D::'a set. countable D \<and> (\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>d \<in> D. d \<in> X))"
+proof -
+  let ?f = "(\<lambda>B'. SOME x. x \<in> B')"
+  have "countable (?f ` B)" using countable_basis by simp
+  with basis_dense[OF is_basis, of ?f] show ?thesis
+    by (intro exI[where x="?f ` B"]) (metis (mono_tags) all_not_in_conv imageI someI)
+qed
+
+lemma countable_dense_setE:
+  obtains D :: "'a set"
+  where "countable D" "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d \<in> D. d \<in> X"
+  using countable_dense_exists by blast
+
+end
+
+lemma (in first_countable_topology) first_countable_basisE:
+  fixes x :: 'a
+  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
+    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
+proof -
+  obtain \<A> where \<A>: "(\<forall>i::nat. x \<in> \<A> i \<and> open (\<A> i))" "(\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. \<A> i \<subseteq> S))"
+    using first_countable_basis[of x] by metis
+  show thesis
+  proof 
+    show "countable (range \<A>)"
+      by simp
+  qed (use \<A> in auto)
+qed
+
+lemma (in first_countable_topology) first_countable_basis_Int_stableE:
+  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
+    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
+    "\<And>A B. A \<in> \<A> \<Longrightarrow> B \<in> \<A> \<Longrightarrow> A \<inter> B \<in> \<A>"
+proof atomize_elim
+  obtain \<B> where \<B>:
+    "countable \<B>"
+    "\<And>B. B \<in> \<B> \<Longrightarrow> x \<in> B"
+    "\<And>B. B \<in> \<B> \<Longrightarrow> open B"
+    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>B\<in>\<B>. B \<subseteq> S"
+    by (rule first_countable_basisE) blast
+  define \<A> where [abs_def]:
+    "\<A> = (\<lambda>N. \<Inter>((\<lambda>n. from_nat_into \<B> n) ` N)) ` (Collect finite::nat set set)"
+  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>
+        (\<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>)"
+  proof (safe intro!: exI[where x=\<A>])
+    show "countable \<A>"
+      unfolding \<A>_def by (intro countable_image countable_Collect_finite)
+    fix A
+    assume "A \<in> \<A>"
+    then show "x \<in> A" "open A"
+      using \<B>(4)[OF open_UNIV] by (auto simp: \<A>_def intro: \<B> from_nat_into)
+  next
+    let ?int = "\<lambda>N. \<Inter>(from_nat_into \<B> ` N)"
+    fix A B
+    assume "A \<in> \<A>" "B \<in> \<A>"
+    then obtain N M where "A = ?int N" "B = ?int M" "finite (N \<union> M)"
+      by (auto simp: \<A>_def)
+    then show "A \<inter> B \<in> \<A>"
+      by (auto simp: \<A>_def intro!: image_eqI[where x="N \<union> M"])
+  next
+    fix S
+    assume "open S" "x \<in> S"
+    then obtain a where a: "a\<in>\<B>" "a \<subseteq> S" using \<B> by blast
+    then show "\<exists>a\<in>\<A>. a \<subseteq> S" using a \<B>
+      by (intro bexI[where x=a]) (auto simp: \<A>_def intro: image_eqI[where x="{to_nat_on \<B> a}"])
+  qed
+qed
+
+lemma (in topological_space) first_countableI:
+  assumes "countable \<A>"
+    and 1: "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
+    and 2: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>A\<in>\<A>. A \<subseteq> S"
+  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))"
+proof (safe intro!: exI[of _ "from_nat_into \<A>"])
+  fix i
+  have "\<A> \<noteq> {}" using 2[of UNIV] by auto
+  show "x \<in> from_nat_into \<A> i" "open (from_nat_into \<A> i)"
+    using range_from_nat_into_subset[OF \<open>\<A> \<noteq> {}\<close>] 1 by auto
+next
+  fix S
+  assume "open S" "x\<in>S" from 2[OF this]
+  show "\<exists>i. from_nat_into \<A> i \<subseteq> S"
+    using subset_range_from_nat_into[OF \<open>countable \<A>\<close>] by auto
+qed
+
+instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology
+proof
+  fix x :: "'a \<times> 'b"
+  obtain \<A> where \<A>:
+      "countable \<A>"
+      "\<And>a. a \<in> \<A> \<Longrightarrow> fst x \<in> a"
+      "\<And>a. a \<in> \<A> \<Longrightarrow> open a"
+      "\<And>S. open S \<Longrightarrow> fst x \<in> S \<Longrightarrow> \<exists>a\<in>\<A>. a \<subseteq> S"
+    by (rule first_countable_basisE[of "fst x"]) blast
+  obtain B where B:
+      "countable B"
+      "\<And>a. a \<in> B \<Longrightarrow> snd x \<in> a"
+      "\<And>a. a \<in> B \<Longrightarrow> open a"
+      "\<And>S. open S \<Longrightarrow> snd x \<in> S \<Longrightarrow> \<exists>a\<in>B. a \<subseteq> S"
+    by (rule first_countable_basisE[of "snd x"]) blast
+  show "\<exists>\<A>::nat \<Rightarrow> ('a \<times> 'b) 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))"
+  proof (rule first_countableI[of "(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B)"], safe)
+    fix a b
+    assume x: "a \<in> \<A>" "b \<in> B"
+    show "x \<in> a \<times> b" 
+      by (simp add: \<A>(2) B(2) mem_Times_iff x)
+    show "open (a \<times> b)"
+      by (simp add: \<A>(3) B(3) open_Times x)
+  next
+    fix S
+    assume "open S" "x \<in> S"
+    then obtain a' b' where a'b': "open a'" "open b'" "x \<in> a' \<times> b'" "a' \<times> b' \<subseteq> S"
+      by (rule open_prod_elim)
+    moreover
+    from a'b' \<A>(4)[of a'] B(4)[of b']
+    obtain a b where "a \<in> \<A>" "a \<subseteq> a'" "b \<in> B" "b \<subseteq> b'"
+      by auto
+    ultimately
+    show "\<exists>a\<in>(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B). a \<subseteq> S"
+      by (auto intro!: bexI[of _ "a \<times> b"] bexI[of _ a] bexI[of _ b])
+  qed (simp add: \<A> B)
+qed
+
+class second_countable_topology = topological_space +
+  assumes ex_countable_subbasis:
+    "\<exists>B::'a::topological_space set set. countable B \<and> open = generate_topology B"
+begin
+
+lemma ex_countable_basis: "\<exists>B::'a set set. countable B \<and> topological_basis B"
+proof -
+  from ex_countable_subbasis obtain B where B: "countable B" "open = generate_topology B"
+    by blast
+  let ?B = "Inter ` {b. finite b \<and> b \<subseteq> B }"
+
+  show ?thesis
+  proof (intro exI conjI)
+    show "countable ?B"
+      by (intro countable_image countable_Collect_finite_subset B)
+    {
+      fix S
+      assume "open S"
+      then have "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. (\<Union>b\<in>B'. \<Inter>b) = S"
+        unfolding B
+      proof induct
+        case UNIV
+        show ?case by (intro exI[of _ "{{}}"]) simp
+      next
+        case (Int a b)
+        then obtain x y where x: "a = \<Union>(Inter ` x)" "\<And>i. i \<in> x \<Longrightarrow> finite i \<and> i \<subseteq> B"
+          and y: "b = \<Union>(Inter ` y)" "\<And>i. i \<in> y \<Longrightarrow> finite i \<and> i \<subseteq> B"
+          by blast
+        show ?case
+          unfolding x y Int_UN_distrib2
+          by (intro exI[of _ "{i \<union> j| i j.  i \<in> x \<and> j \<in> y}"]) (auto dest: x(2) y(2))
+      next
+        case (UN K)
+        then have "\<forall>k\<in>K. \<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = k" by auto
+        then obtain k where
+            "\<forall>ka\<in>K. k ka \<subseteq> {b. finite b \<and> b \<subseteq> B} \<and> \<Union>(Inter ` (k ka)) = ka"
+          unfolding bchoice_iff ..
+        then show "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = \<Union>K"
+          by (intro exI[of _ "\<Union>(k ` K)"]) auto
+      next
+        case (Basis S)
+        then show ?case
+          by (intro exI[of _ "{{S}}"]) auto
+      qed
+      then have "(\<exists>B'\<subseteq>Inter ` {b. finite b \<and> b \<subseteq> B}. \<Union>B' = S)"
+        unfolding subset_image_iff by blast }
+    then show "topological_basis ?B"
+      unfolding topological_space_class.topological_basis_def
+      by (safe intro!: topological_space_class.open_Inter)
+         (simp_all add: B generate_topology.Basis subset_eq)
+  qed
+qed
+
+end
+
+sublocale second_countable_topology <
+  countable_basis "SOME B. countable B \<and> topological_basis B"
+  using someI_ex[OF ex_countable_basis]
+  by unfold_locales safe
+
+instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology
+proof
+  obtain A :: "'a set set" where "countable A" "topological_basis A"
+    using ex_countable_basis by auto
+  moreover
+  obtain B :: "'b set set" where "countable B" "topological_basis B"
+    using ex_countable_basis by auto
+  ultimately show "\<exists>B::('a \<times> 'b) set set. countable B \<and> open = generate_topology B"
+    by (auto intro!: exI[of _ "(\<lambda>(a, b). a \<times> b) ` (A \<times> B)"] topological_basis_prod
+      topological_basis_imp_subbasis)
+qed
+
+instance second_countable_topology \<subseteq> first_countable_topology
+proof
+  fix x :: 'a
+  define B :: "'a set set" where "B = (SOME B. countable B \<and> topological_basis B)"
+  then have B: "countable B" "topological_basis B"
+    using countable_basis is_basis
+    by (auto simp: countable_basis is_basis)
+  then show "\<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))"
+    by (intro first_countableI[of "{b\<in>B. x \<in> b}"])
+       (fastforce simp: topological_space_class.topological_basis_def)+
+qed
+
+instance nat :: second_countable_topology
+proof
+  show "\<exists>B::nat set set. countable B \<and> open = generate_topology B"
+    by (intro exI[of _ "range lessThan \<union> range greaterThan"]) (auto simp: open_nat_def)
+qed
+
+lemma countable_separating_set_linorder1:
+  shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y))"
+proof -
+  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
+  define B1 where "B1 = {(LEAST x. x \<in> U)| U. U \<in> A}"
+  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
+  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
+  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
+  have "\<exists>b \<in> B1 \<union> B2. x < b \<and> b \<le> y" if "x < y" for x y
+  proof (cases)
+    assume "\<exists>z. x < z \<and> z < y"
+    then obtain z where z: "x < z \<and> z < y" by auto
+    define U where "U = {x<..<y}"
+    then have "open U" by simp
+    moreover have "z \<in> U" using z U_def by simp
+    ultimately obtain V where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
+    define w where "w = (SOME x. x \<in> V)"
+    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
+    then have "x < w \<and> w \<le> y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
+    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
+    ultimately show ?thesis by auto
+  next
+    assume "\<not>(\<exists>z. x < z \<and> z < y)"
+    then have *: "\<And>z. z > x \<Longrightarrow> z \<ge> y" by auto
+    define U where "U = {x<..}"
+    then have "open U" by simp
+    moreover have "y \<in> U" using \<open>x < y\<close> U_def by simp
+    ultimately obtain "V" where "V \<in> A" "y \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
+    have "U = {y..}" unfolding U_def using * \<open>x < y\<close> by auto
+    then have "V \<subseteq> {y..}" using \<open>V \<subseteq> U\<close> by simp
+    then have "(LEAST w. w \<in> V) = y" using \<open>y \<in> V\<close> by (meson Least_equality atLeast_iff subsetCE)
+    then have "y \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
+    moreover have "x < y \<and> y \<le> y" using \<open>x < y\<close> by simp
+    ultimately show ?thesis by auto
+  qed
+  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
+  ultimately show ?thesis by auto
+qed
+
+lemma countable_separating_set_linorder2:
+  shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x \<le> b \<and> b < y))"
+proof -
+  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
+  define B1 where "B1 = {(GREATEST x. x \<in> U) | U. U \<in> A}"
+  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
+  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
+  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
+  have "\<exists>b \<in> B1 \<union> B2. x \<le> b \<and> b < y" if "x < y" for x y
+  proof (cases)
+    assume "\<exists>z. x < z \<and> z < y"
+    then obtain z where z: "x < z \<and> z < y" by auto
+    define U where "U = {x<..<y}"
+    then have "open U" by simp
+    moreover have "z \<in> U" using z U_def by simp
+    ultimately obtain "V" where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
+    define w where "w = (SOME x. x \<in> V)"
+    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
+    then have "x \<le> w \<and> w < y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
+    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
+    ultimately show ?thesis by auto
+  next
+    assume "\<not>(\<exists>z. x < z \<and> z < y)"
+    then have *: "\<And>z. z < y \<Longrightarrow> z \<le> x" using leI by blast
+    define U where "U = {..<y}"
+    then have "open U" by simp
+    moreover have "x \<in> U" using \<open>x < y\<close> U_def by simp
+    ultimately obtain "V" where "V \<in> A" "x \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
+    have "U = {..x}" unfolding U_def using * \<open>x < y\<close> by auto
+    then have "V \<subseteq> {..x}" using \<open>V \<subseteq> U\<close> by simp
+    then have "(GREATEST x. x \<in> V) = x" using \<open>x \<in> V\<close> by (meson Greatest_equality atMost_iff subsetCE)
+    then have "x \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
+    moreover have "x \<le> x \<and> x < y" using \<open>x < y\<close> by simp
+    ultimately show ?thesis by auto
+  qed
+  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
+  ultimately show ?thesis by auto
+qed
+
+lemma countable_separating_set_dense_linorder:
+  shows "\<exists>B::('a::{linorder_topology, dense_linorder, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b < y))"
+proof -
+  obtain B::"'a set" where B: "countable B" "\<And>x y. x < y \<Longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y)"
+    using countable_separating_set_linorder1 by auto
+  have "\<exists>b \<in> B. x < b \<and> b < y" if "x < y" for x y
+  proof -
+    obtain z where "x < z" "z < y" using \<open>x < y\<close> dense by blast
+    then obtain b where "b \<in> B" "x < b \<and> b \<le> z" using B(2) by auto
+    then have "x < b \<and> b < y" using \<open>z < y\<close> by auto
+    then show ?thesis using \<open>b \<in> B\<close> by auto
+  qed
+  then show ?thesis using B(1) by auto
+qed
+
+subsection%important \<open>Polish spaces\<close>
+
+text \<open>Textbooks define Polish spaces as completely metrizable.
+  We assume the topology to be complete for a given metric.\<close>
+
+class polish_space = complete_space + second_countable_topology
+
+subsection \<open>General notion of a topology as a value\<close>
+
+definition%important "istopology L \<longleftrightarrow>
+  L {} \<and> (\<forall>S T. L S \<longrightarrow> L T \<longrightarrow> L (S \<inter> T)) \<and> (\<forall>K. Ball K L \<longrightarrow> L (\<Union>K))"
+
+typedef%important 'a topology = "{L::('a set) \<Rightarrow> bool. istopology L}"
+  morphisms "openin" "topology"
+  unfolding istopology_def by blast
+
+lemma istopology_openin[intro]: "istopology(openin U)"
+  using openin[of U] by blast
+
+lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
+  using topology_inverse[unfolded mem_Collect_eq] .
+
+lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
+  using topology_inverse[of U] istopology_openin[of "topology U"] by auto
+
+lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
+proof
+  assume "T1 = T2"
+  then show "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp
+next
+  assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
+  then have "openin T1 = openin T2" by (simp add: fun_eq_iff)
+  then have "topology (openin T1) = topology (openin T2)" by simp
+  then show "T1 = T2" unfolding openin_inverse .
+qed
+
+
+text\<open>The "universe": the union of all sets in the topology.\<close>
+definition "topspace T = \<Union>{S. openin T S}"
+
+subsubsection \<open>Main properties of open sets\<close>
+
+proposition openin_clauses:
+  fixes U :: "'a topology"
+  shows
+    "openin U {}"
+    "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
+    "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
+  using openin[of U] unfolding istopology_def mem_Collect_eq by fast+
+
+lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
+  unfolding topspace_def by blast
+
+lemma openin_empty[simp]: "openin U {}"
+  by (rule openin_clauses)
+
+lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
+  by (rule openin_clauses)
+
+lemma openin_Union[intro]: "(\<And>S. S \<in> K \<Longrightarrow> openin U S) \<Longrightarrow> openin U (\<Union>K)"
+  using openin_clauses by blast
+
+lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
+  using openin_Union[of "{S,T}" U] by auto
+
+lemma openin_topspace[intro, simp]: "openin U (topspace U)"
+  by (force simp: openin_Union topspace_def)
+
+lemma openin_subopen: "openin U S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>T. openin U T \<and> x \<in> T \<and> T \<subseteq> S)"
+  (is "?lhs \<longleftrightarrow> ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs by auto
+next
+  assume H: ?rhs
+  let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"
+  have "openin U ?t" by (force simp: openin_Union)
+  also have "?t = S" using H by auto
+  finally show "openin U S" .
+qed
+
+lemma openin_INT [intro]:
+  assumes "finite I"
+          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
+  shows "openin T ((\<Inter>i \<in> I. U i) \<inter> topspace T)"
+using assms by (induct, auto simp: inf_sup_aci(2) openin_Int)
+
+lemma openin_INT2 [intro]:
+  assumes "finite I" "I \<noteq> {}"
+          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
+  shows "openin T (\<Inter>i \<in> I. U i)"
+proof -
+  have "(\<Inter>i \<in> I. U i) \<subseteq> topspace T"
+    using \<open>I \<noteq> {}\<close> openin_subset[OF assms(3)] by auto
+  then show ?thesis
+    using openin_INT[of _ _ U, OF assms(1) assms(3)] by (simp add: inf.absorb2 inf_commute)
+qed
+
+lemma openin_Inter [intro]:
+  assumes "finite \<F>" "\<F> \<noteq> {}" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (\<Inter>\<F>)"
+  by (metis (full_types) assms openin_INT2 image_ident)
+
+lemma openin_Int_Inter:
+  assumes "finite \<F>" "openin T U" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (U \<inter> \<Inter>\<F>)"
+  using openin_Inter [of "insert U \<F>"] assms by auto
+
+
+subsubsection \<open>Closed sets\<close>
+
+definition%important "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
+
+lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U"
+  by (metis closedin_def)
+
+lemma closedin_empty[simp]: "closedin U {}"
+  by (simp add: closedin_def)
+
+lemma closedin_topspace[intro, simp]: "closedin U (topspace U)"
+  by (simp add: closedin_def)
+
+lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
+  by (auto simp: Diff_Un closedin_def)
+
+lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union>{A - s|s. s\<in>S}"
+  by auto
+
+lemma closedin_Union:
+  assumes "finite S" "\<And>T. T \<in> S \<Longrightarrow> closedin U T"
+    shows "closedin U (\<Union>S)"
+  using assms by induction auto
+
+lemma closedin_Inter[intro]:
+  assumes Ke: "K \<noteq> {}"
+    and Kc: "\<And>S. S \<in>K \<Longrightarrow> closedin U S"
+  shows "closedin U (\<Inter>K)"
+  using Ke Kc unfolding closedin_def Diff_Inter by auto
+
+lemma closedin_INT[intro]:
+  assumes "A \<noteq> {}" "\<And>x. x \<in> A \<Longrightarrow> closedin U (B x)"
+  shows "closedin U (\<Inter>x\<in>A. B x)"
+  apply (rule closedin_Inter)
+  using assms
+  apply auto
+  done
+
+lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
+  using closedin_Inter[of "{S,T}" U] by auto
+
+lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
+  apply (auto simp: closedin_def Diff_Diff_Int inf_absorb2)
+  apply (metis openin_subset subset_eq)
+  done
+
+lemma openin_closedin: "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
+  by (simp add: openin_closedin_eq)
+
+lemma openin_diff[intro]:
+  assumes oS: "openin U S"
+    and cT: "closedin U T"
+  shows "openin U (S - T)"
+proof -
+  have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
+    by (auto simp: topspace_def openin_subset)
+  then show ?thesis using oS cT
+    by (auto simp: closedin_def)
+qed
+
+lemma closedin_diff[intro]:
+  assumes oS: "closedin U S"
+    and cT: "openin U T"
+  shows "closedin U (S - T)"
+proof -
+  have "S - T = S \<inter> (topspace U - T)"
+    using closedin_subset[of U S] oS cT by (auto simp: topspace_def)
+  then show ?thesis
+    using oS cT by (auto simp: openin_closedin_eq)
+qed
+
+
+subsection\<open>The discrete topology\<close>
+
+definition discrete_topology where "discrete_topology U \<equiv> topology (\<lambda>S. S \<subseteq> U)"
+
+lemma openin_discrete_topology [simp]: "openin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
+proof -
+  have "istopology (\<lambda>S. S \<subseteq> U)"
+    by (auto simp: istopology_def)
+  then show ?thesis
+    by (simp add: discrete_topology_def topology_inverse')
+qed
+
+lemma topspace_discrete_topology [simp]: "topspace(discrete_topology U) = U"
+  by (meson openin_discrete_topology openin_subset openin_topspace order_refl subset_antisym)
+
+lemma closedin_discrete_topology [simp]: "closedin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
+  by (simp add: closedin_def)
+
+lemma discrete_topology_unique:
+   "discrete_topology U = X \<longleftrightarrow> topspace X = U \<and> (\<forall>x \<in> U. openin X {x})" (is "?lhs = ?rhs")
+proof
+  assume R: ?rhs
+  then have "openin X S" if "S \<subseteq> U" for S
+    using openin_subopen subsetD that by fastforce
+  moreover have "x \<in> topspace X" if "openin X S" and "x \<in> S" for x S
+    using openin_subset that by blast
+  ultimately
+  show ?lhs
+    using R by (auto simp: topology_eq)
+qed auto
+
+lemma discrete_topology_unique_alt:
+  "discrete_topology U = X \<longleftrightarrow> topspace X \<subseteq> U \<and> (\<forall>x \<in> U. openin X {x})"
+  using openin_subset
+  by (auto simp: discrete_topology_unique)
+
+lemma subtopology_eq_discrete_topology_empty:
+   "X = discrete_topology {} \<longleftrightarrow> topspace X = {}"
+  using discrete_topology_unique [of "{}" X] by auto
+
+lemma subtopology_eq_discrete_topology_sing:
+   "X = discrete_topology {a} \<longleftrightarrow> topspace X = {a}"
+  by (metis discrete_topology_unique openin_topspace singletonD)
+
+
+subsection \<open>Subspace topology\<close>
+
+definition%important "subtopology U V = topology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
+
+lemma istopology_subtopology: "istopology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
+  (is "istopology ?L")
+proof -
+  have "?L {}" by blast
+  {
+    fix A B
+    assume A: "?L A" and B: "?L B"
+    from A B obtain Sa and Sb where Sa: "openin U Sa" "A = Sa \<inter> V" and Sb: "openin U Sb" "B = Sb \<inter> V"
+      by blast
+    have "A \<inter> B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"
+      using Sa Sb by blast+
+    then have "?L (A \<inter> B)" by blast
+  }
+  moreover
+  {
+    fix K
+    assume K: "K \<subseteq> Collect ?L"
+    have th0: "Collect ?L = (\<lambda>S. S \<inter> V) ` Collect (openin U)"
+      by blast
+    from K[unfolded th0 subset_image_iff]
+    obtain Sk where Sk: "Sk \<subseteq> Collect (openin U)" "K = (\<lambda>S. S \<inter> V) ` Sk"
+      by blast
+    have "\<Union>K = (\<Union>Sk) \<inter> V"
+      using Sk by auto
+    moreover have "openin U (\<Union>Sk)"
+      using Sk by (auto simp: subset_eq)
+    ultimately have "?L (\<Union>K)" by blast
+  }
+  ultimately show ?thesis
+    unfolding subset_eq mem_Collect_eq istopology_def by auto
+qed
+
+lemma openin_subtopology: "openin (subtopology U V) S \<longleftrightarrow> (\<exists>T. openin U T \<and> S = T \<inter> V)"
+  unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
+  by auto
+
+lemma openin_subtopology_Int:
+   "openin X S \<Longrightarrow> openin (subtopology X T) (S \<inter> T)"
+  using openin_subtopology by auto
+
+lemma openin_subtopology_Int2:
+   "openin X T \<Longrightarrow> openin (subtopology X S) (S \<inter> T)"
+  using openin_subtopology by auto
+
+lemma openin_subtopology_diff_closed:
+   "\<lbrakk>S \<subseteq> topspace X; closedin X T\<rbrakk> \<Longrightarrow> openin (subtopology X S) (S - T)"
+  unfolding closedin_def openin_subtopology
+  by (rule_tac x="topspace X - T" in exI) auto
+
+lemma openin_relative_to: "(openin X relative_to S) = openin (subtopology X S)"
+  by (force simp: relative_to_def openin_subtopology)
+
+lemma topspace_subtopology: "topspace (subtopology U V) = topspace U \<inter> V"
+  by (auto simp: topspace_def openin_subtopology)
+
+lemma closedin_subtopology: "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
+  unfolding closedin_def topspace_subtopology
+  by (auto simp: openin_subtopology)
+
+lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
+  unfolding openin_subtopology
+  by auto (metis IntD1 in_mono openin_subset)
+
+lemma subtopology_subtopology:
+   "subtopology (subtopology X S) T = subtopology X (S \<inter> T)"
+proof -
+  have eq: "\<And>T'. (\<exists>S'. T' = S' \<inter> T \<and> (\<exists>T. openin X T \<and> S' = T \<inter> S)) = (\<exists>Sa. T' = Sa \<inter> (S \<inter> T) \<and> openin X Sa)"
+    by (metis inf_assoc)
+  have "subtopology (subtopology X S) T = topology (\<lambda>Ta. \<exists>Sa. Ta = Sa \<inter> T \<and> openin (subtopology X S) Sa)"
+    by (simp add: subtopology_def)
+  also have "\<dots> = subtopology X (S \<inter> T)"
+    by (simp add: openin_subtopology eq) (simp add: subtopology_def)
+  finally show ?thesis .
+qed
+
+lemma openin_subtopology_alt:
+     "openin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (openin X)"
+  by (simp add: image_iff inf_commute openin_subtopology)
+
+lemma closedin_subtopology_alt:
+     "closedin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (closedin X)"
+  by (simp add: image_iff inf_commute closedin_subtopology)
+
+lemma subtopology_superset:
+  assumes UV: "topspace U \<subseteq> V"
+  shows "subtopology U V = U"
+proof -
+  {
+    fix S
+    {
+      fix T
+      assume T: "openin U T" "S = T \<inter> V"
+      from T openin_subset[OF T(1)] UV have eq: "S = T"
+        by blast
+      have "openin U S"
+        unfolding eq using T by blast
+    }
+    moreover
+    {
+      assume S: "openin U S"
+      then have "\<exists>T. openin U T \<and> S = T \<inter> V"
+        using openin_subset[OF S] UV by auto
+    }
+    ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S"
+      by blast
+  }
+  then show ?thesis
+    unfolding topology_eq openin_subtopology by blast
+qed
+
+lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
+  by (simp add: subtopology_superset)
+
+lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
+  by (simp add: subtopology_superset)
+
+lemma openin_subtopology_empty:
+   "openin (subtopology U {}) S \<longleftrightarrow> S = {}"
+by (metis Int_empty_right openin_empty openin_subtopology)
+
+lemma closedin_subtopology_empty:
+   "closedin (subtopology U {}) S \<longleftrightarrow> S = {}"
+by (metis Int_empty_right closedin_empty closedin_subtopology)
+
+lemma closedin_subtopology_refl [simp]:
+   "closedin (subtopology U X) X \<longleftrightarrow> X \<subseteq> topspace U"
+by (metis closedin_def closedin_topspace inf.absorb_iff2 le_inf_iff topspace_subtopology)
+
+lemma closedin_topspace_empty: "topspace T = {} \<Longrightarrow> (closedin T S \<longleftrightarrow> S = {})"
+  by (simp add: closedin_def)
+
+lemma openin_imp_subset:
+   "openin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
+by (metis Int_iff openin_subtopology subsetI)
+
+lemma closedin_imp_subset:
+   "closedin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
+by (simp add: closedin_def topspace_subtopology)
+
+lemma openin_open_subtopology:
+     "openin X S \<Longrightarrow> openin (subtopology X S) T \<longleftrightarrow> openin X T \<and> T \<subseteq> S"
+  by (metis inf.orderE openin_Int openin_imp_subset openin_subtopology)
+
+lemma closedin_closed_subtopology:
+     "closedin X S \<Longrightarrow> (closedin (subtopology X S) T \<longleftrightarrow> closedin X T \<and> T \<subseteq> S)"
+  by (metis closedin_Int closedin_imp_subset closedin_subtopology inf.orderE)
+
+lemma openin_subtopology_Un:
+    "\<lbrakk>openin (subtopology X T) S; openin (subtopology X U) S\<rbrakk>
+     \<Longrightarrow> openin (subtopology X (T \<union> U)) S"
+by (simp add: openin_subtopology) blast
+
+lemma closedin_subtopology_Un:
+    "\<lbrakk>closedin (subtopology X T) S; closedin (subtopology X U) S\<rbrakk>
+     \<Longrightarrow> closedin (subtopology X (T \<union> U)) S"
+by (simp add: closedin_subtopology) blast
+
+
+subsection \<open>The standard Euclidean topology\<close>
+
+definition%important euclidean :: "'a::topological_space topology"
+  where "euclidean = topology open"
+
+lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
+  unfolding euclidean_def
+  apply (rule cong[where x=S and y=S])
+  apply (rule topology_inverse[symmetric])
+  apply (auto simp: istopology_def)
+  done
+
+declare open_openin [symmetric, simp]
+
+lemma topspace_euclidean [simp]: "topspace euclidean = UNIV"
+  by (force simp: topspace_def)
+
+lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
+  by (simp add: topspace_subtopology)
+
+lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
+  by (simp add: closed_def closedin_def Compl_eq_Diff_UNIV)
+
+declare closed_closedin [symmetric, simp]
+
+lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
+  using openI by auto
+
+lemma openin_subtopology_self [simp]: "openin (subtopology euclidean S) S"
+  by (metis openin_topspace topspace_euclidean_subtopology)
+
+subsubsection\<open>The most basic facts about the usual topology and metric on R\<close>
+
+abbreviation euclideanreal :: "real topology"
+  where "euclideanreal \<equiv> topology open"
+
+lemma real_openin [simp]: "openin euclideanreal S = open S"
+  by (simp add: euclidean_def open_openin)
+
+lemma topspace_euclideanreal [simp]: "topspace euclideanreal = UNIV"
+  using openin_subset open_UNIV real_openin by blast
+
+lemma topspace_euclideanreal_subtopology [simp]:
+   "topspace (subtopology euclideanreal S) = S"
+  by (simp add: topspace_subtopology)
+
+lemma real_closedin [simp]: "closedin euclideanreal S = closed S"
+  by (simp add: closed_closedin euclidean_def)
+
+subsection \<open>Basic "localization" results are handy for connectedness.\<close>
+
+lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
+  by (auto simp: openin_subtopology)
+
+lemma openin_Int_open:
+   "\<lbrakk>openin (subtopology euclidean U) S; open T\<rbrakk>
+        \<Longrightarrow> openin (subtopology euclidean U) (S \<inter> T)"
+by (metis open_Int Int_assoc openin_open)
+
+lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
+  by (auto simp: openin_open)
+
+lemma open_openin_trans[trans]:
+  "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
+  by (metis Int_absorb1  openin_open_Int)
+
+lemma open_subset: "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
+  by (auto simp: openin_open)
+
+lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
+  by (simp add: closedin_subtopology Int_ac)
+
+lemma closedin_closed_Int: "closed S \<Longrightarrow> closedin (subtopology euclidean U) (U \<inter> S)"
+  by (metis closedin_closed)
+
+lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
+  by (auto simp: closedin_closed)
+
+lemma closedin_closed_subset:
+ "\<lbrakk>closedin (subtopology euclidean U) V; T \<subseteq> U; S = V \<inter> T\<rbrakk>
+             \<Longrightarrow> closedin (subtopology euclidean T) S"
+  by (metis (no_types, lifting) Int_assoc Int_commute closedin_closed inf.orderE)
+
+lemma finite_imp_closedin:
+  fixes S :: "'a::t1_space set"
+  shows "\<lbrakk>finite S; S \<subseteq> T\<rbrakk> \<Longrightarrow> closedin (subtopology euclidean T) S"
+    by (simp add: finite_imp_closed closed_subset)
+
+lemma closedin_singleton [simp]:
+  fixes a :: "'a::t1_space"
+  shows "closedin (subtopology euclidean U) {a} \<longleftrightarrow> a \<in> U"
+using closedin_subset  by (force intro: closed_subset)
+
+lemma openin_euclidean_subtopology_iff:
+  fixes S U :: "'a::metric_space set"
+  shows "openin (subtopology euclidean U) S \<longleftrightarrow>
+    S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)"
+  (is "?lhs \<longleftrightarrow> ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs
+    unfolding openin_open open_dist by blast
+next
+  define T where "T = {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}"
+  have 1: "\<forall>x\<in>T. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> T"
+    unfolding T_def
+    apply clarsimp
+    apply (rule_tac x="d - dist x a" in exI)
+    apply (clarsimp simp add: less_diff_eq)
+    by (metis dist_commute dist_triangle_lt)
+  assume ?rhs then have 2: "S = U \<inter> T"
+    unfolding T_def
+    by auto (metis dist_self)
+  from 1 2 show ?lhs
+    unfolding openin_open open_dist by fast
+qed
+
+lemma connected_openin:
+      "connected S \<longleftrightarrow>
+       \<not>(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
+                 openin (subtopology euclidean S) E2 \<and>
+                 S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
+  apply (simp add: connected_def openin_open disjoint_iff_not_equal, safe)
+  apply (simp_all, blast+)  (* SLOW *)
+  done
+
+lemma connected_openin_eq:
+      "connected S \<longleftrightarrow>
+       \<not>(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
+                 openin (subtopology euclidean S) E2 \<and>
+                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
+                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
+  apply (simp add: connected_openin, safe, blast)
+  by (metis Int_lower1 Un_subset_iff openin_open subset_antisym)
+
+lemma connected_closedin:
+      "connected S \<longleftrightarrow>
+       (\<nexists>E1 E2.
+        closedin (subtopology euclidean S) E1 \<and>
+        closedin (subtopology euclidean S) E2 \<and>
+        S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
+       (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs 
+    by (auto simp add: connected_closed closedin_closed)
+next
+  assume R: ?rhs
+  then show ?lhs 
+  proof (clarsimp simp add: connected_closed closedin_closed)
+    fix A B 
+    assume s_sub: "S \<subseteq> A \<union> B" "B \<inter> S \<noteq> {}"
+      and disj: "A \<inter> B \<inter> S = {}"
+      and cl: "closed A" "closed B"
+    have "S \<inter> (A \<union> B) = S"
+      using s_sub(1) by auto
+    have "S - A = B \<inter> S"
+      using Diff_subset_conv Un_Diff_Int disj s_sub(1) by auto
+    then have "S \<inter> A = {}"
+      by (metis Diff_Diff_Int Diff_disjoint Un_Diff_Int R cl closedin_closed_Int inf_commute order_refl s_sub(2))
+    then show "A \<inter> S = {}"
+      by blast
+  qed
+qed
+
+lemma connected_closedin_eq:
+      "connected S \<longleftrightarrow>
+           \<not>(\<exists>E1 E2.
+                 closedin (subtopology euclidean S) E1 \<and>
+                 closedin (subtopology euclidean S) E2 \<and>
+                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
+                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
+  apply (simp add: connected_closedin, safe, blast)
+  by (metis Int_lower1 Un_subset_iff closedin_closed subset_antisym)
+
+text \<open>These "transitivity" results are handy too\<close>
+
+lemma openin_trans[trans]:
+  "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T \<Longrightarrow>
+    openin (subtopology euclidean U) S"
+  unfolding open_openin openin_open by blast
+
+lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
+  by (auto simp: openin_open intro: openin_trans)
+
+lemma closedin_trans[trans]:
+  "closedin (subtopology euclidean T) S \<Longrightarrow> closedin (subtopology euclidean U) T \<Longrightarrow>
+    closedin (subtopology euclidean U) S"
+  by (auto simp: closedin_closed closed_Inter Int_assoc)
+
+lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
+  by (auto simp: closedin_closed intro: closedin_trans)
+
+lemma openin_subtopology_Int_subset:
+   "\<lbrakk>openin (subtopology euclidean u) (u \<inter> S); v \<subseteq> u\<rbrakk> \<Longrightarrow> openin (subtopology euclidean v) (v \<inter> S)"
+  by (auto simp: openin_subtopology)
+
+lemma openin_open_eq: "open s \<Longrightarrow> (openin (subtopology euclidean s) t \<longleftrightarrow> open t \<and> t \<subseteq> s)"
+  using open_subset openin_open_trans openin_subset by fastforce
+
+
+subsection \<open>Open and closed balls\<close>
+
+definition%important ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
+  where "ball x e = {y. dist x y < e}"
+
+definition%important cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
+  where "cball x e = {y. dist x y \<le> e}"
+
+definition%important sphere :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
+  where "sphere x e = {y. dist x y = e}"
+
+lemma mem_ball [simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e"
+  by (simp add: ball_def)
+
+lemma mem_cball [simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e"
+  by (simp add: cball_def)
+
+lemma mem_sphere [simp]: "y \<in> sphere x e \<longleftrightarrow> dist x y = e"
+  by (simp add: sphere_def)
+
+lemma ball_trivial [simp]: "ball x 0 = {}"
+  by (simp add: ball_def)
+
+lemma cball_trivial [simp]: "cball x 0 = {x}"
+  by (simp add: cball_def)
+
+lemma sphere_trivial [simp]: "sphere x 0 = {x}"
+  by (simp add: sphere_def)
+
+lemma mem_ball_0 [simp]: "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
+  for x :: "'a::real_normed_vector"
+  by (simp add: dist_norm)
+
+lemma mem_cball_0 [simp]: "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
+  for x :: "'a::real_normed_vector"
+  by (simp add: dist_norm)
+
+lemma disjoint_ballI: "dist x y \<ge> r+s \<Longrightarrow> ball x r \<inter> ball y s = {}"
+  using dist_triangle_less_add not_le by fastforce
+
+lemma disjoint_cballI: "dist x y > r + s \<Longrightarrow> cball x r \<inter> cball y s = {}"
+  by (metis add_mono disjoint_iff_not_equal dist_triangle2 dual_order.trans leD mem_cball)
+
+lemma mem_sphere_0 [simp]: "x \<in> sphere 0 e \<longleftrightarrow> norm x = e"
+  for x :: "'a::real_normed_vector"
+  by (simp add: dist_norm)
+
+lemma sphere_empty [simp]: "r < 0 \<Longrightarrow> sphere a r = {}"
+  for a :: "'a::metric_space"
+  by auto
+
+lemma centre_in_ball [simp]: "x \<in> ball x e \<longleftrightarrow> 0 < e"
+  by simp
+
+lemma centre_in_cball [simp]: "x \<in> cball x e \<longleftrightarrow> 0 \<le> e"
+  by simp
+
+lemma ball_subset_cball [simp, intro]: "ball x e \<subseteq> cball x e"
+  by (simp add: subset_eq)
+
+lemma mem_ball_imp_mem_cball: "x \<in> ball y e \<Longrightarrow> x \<in> cball y e"
+  by (auto simp: mem_ball mem_cball)
+
+lemma sphere_cball [simp,intro]: "sphere z r \<subseteq> cball z r"
+  by force
+
+lemma cball_diff_sphere: "cball a r - sphere a r = ball a r"
+  by auto
+
+lemma subset_ball[intro]: "d \<le> e \<Longrightarrow> ball x d \<subseteq> ball x e"
+  by (simp add: subset_eq)
+
+lemma subset_cball[intro]: "d \<le> e \<Longrightarrow> cball x d \<subseteq> cball x e"
+  by (simp add: subset_eq)
+
+lemma mem_ball_leI: "x \<in> ball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> ball y f"
+  by (auto simp: mem_ball mem_cball)
+
+lemma mem_cball_leI: "x \<in> cball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> cball y f"
+  by (auto simp: mem_ball mem_cball)
+
+lemma cball_trans: "y \<in> cball z b \<Longrightarrow> x \<in> cball y a \<Longrightarrow> x \<in> cball z (b + a)"
+  unfolding mem_cball
+proof -
+  have "dist z x \<le> dist z y + dist y x"
+    by (rule dist_triangle)
+  also assume "dist z y \<le> b"
+  also assume "dist y x \<le> a"
+  finally show "dist z x \<le> b + a" by arith
+qed
+
+lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
+  by (simp add: set_eq_iff) arith
+
+lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
+  by (simp add: set_eq_iff)
+
+lemma cball_max_Un: "cball a (max r s) = cball a r \<union> cball a s"
+  by (simp add: set_eq_iff) arith
+
+lemma cball_min_Int: "cball a (min r s) = cball a r \<inter> cball a s"
+  by (simp add: set_eq_iff)
+
+lemma cball_diff_eq_sphere: "cball a r - ball a r =  sphere a r"
+  by (auto simp: cball_def ball_def dist_commute)
+
+lemma image_add_ball [simp]:
+  fixes a :: "'a::real_normed_vector"
+  shows "(+) b ` ball a r = ball (a+b) r"
+apply (intro equalityI subsetI)
+apply (force simp: dist_norm)
+apply (rule_tac x="x-b" in image_eqI)
+apply (auto simp: dist_norm algebra_simps)
+done
+
+lemma image_add_cball [simp]:
+  fixes a :: "'a::real_normed_vector"
+  shows "(+) b ` cball a r = cball (a+b) r"
+apply (intro equalityI subsetI)
+apply (force simp: dist_norm)
+apply (rule_tac x="x-b" in image_eqI)
+apply (auto simp: dist_norm algebra_simps)
+done
+
+lemma open_ball [intro, simp]: "open (ball x e)"
+proof -
+  have "open (dist x -` {..<e})"
+    by (intro open_vimage open_lessThan continuous_intros)
+  also have "dist x -` {..<e} = ball x e"
+    by auto
+  finally show ?thesis .
+qed
+
+lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
+  by (simp add: open_dist subset_eq mem_ball Ball_def dist_commute)
+
+lemma openI [intro?]: "(\<And>x. x\<in>S \<Longrightarrow> \<exists>e>0. ball x e \<subseteq> S) \<Longrightarrow> open S"
+  by (auto simp: open_contains_ball)
+
+lemma openE[elim?]:
+  assumes "open S" "x\<in>S"
+  obtains e where "e>0" "ball x e \<subseteq> S"
+  using assms unfolding open_contains_ball by auto
+
+lemma open_contains_ball_eq: "open S \<Longrightarrow> x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
+  by (metis open_contains_ball subset_eq centre_in_ball)
+
+lemma openin_contains_ball:
+    "openin (subtopology euclidean t) s \<longleftrightarrow>
+     s \<subseteq> t \<and> (\<forall>x \<in> s. \<exists>e. 0 < e \<and> ball x e \<inter> t \<subseteq> s)"
+    (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs
+    apply (simp add: openin_open)
+    apply (metis Int_commute Int_mono inf.cobounded2 open_contains_ball order_refl subsetCE)
+    done
+next
+  assume ?rhs
+  then show ?lhs
+    apply (simp add: openin_euclidean_subtopology_iff)
+    by (metis (no_types) Int_iff dist_commute inf.absorb_iff2 mem_ball)
+qed
+
+lemma openin_contains_cball:
+   "openin (subtopology euclidean t) s \<longleftrightarrow>
+        s \<subseteq> t \<and>
+        (\<forall>x \<in> s. \<exists>e. 0 < e \<and> cball x e \<inter> t \<subseteq> s)"
+apply (simp add: openin_contains_ball)
+apply (rule iffI)
+apply (auto dest!: bspec)
+apply (rule_tac x="e/2" in exI, force+)
+done
+
+lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
+  unfolding mem_ball set_eq_iff
+  apply (simp add: not_less)
+  apply (metis zero_le_dist order_trans dist_self)
+  done
+
+lemma ball_empty: "e \<le> 0 \<Longrightarrow> ball x e = {}" by simp
+
+lemma closed_cball [iff]: "closed (cball x e)"
+proof -
+  have "closed (dist x -` {..e})"
+    by (intro closed_vimage closed_atMost continuous_intros)
+  also have "dist x -` {..e} = cball x e"
+    by auto
+  finally show ?thesis .
+qed
+
+lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
+proof -
+  {
+    fix x and e::real
+    assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
+    then have "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
+  }
+  moreover
+  {
+    fix x and e::real
+    assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
+    then have "\<exists>d>0. ball x d \<subseteq> S"
+      unfolding subset_eq
+      apply (rule_tac x="e/2" in exI, auto)
+      done
+  }
+  ultimately show ?thesis
+    unfolding open_contains_ball by auto
+qed
+
+lemma open_contains_cball_eq: "open S \<Longrightarrow> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
+  by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)
+
+lemma eventually_nhds_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>x. x \<in> ball z d) (nhds z)"
+  by (rule eventually_nhds_in_open) simp_all
+
+lemma eventually_at_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<in> A) (at z within A)"
+  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
+
+lemma eventually_at_ball': "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<noteq> z \<and> t \<in> A) (at z within A)"
+  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
+
+lemma at_within_ball: "e > 0 \<Longrightarrow> dist x y < e \<Longrightarrow> at y within ball x e = at y"
+  by (subst at_within_open) auto
+
+lemma atLeastAtMost_eq_cball:
+  fixes a b::real
+  shows "{a .. b} = cball ((a + b)/2) ((b - a)/2)"
+  by (auto simp: dist_real_def field_simps mem_cball)
+
+lemma greaterThanLessThan_eq_ball:
+  fixes a b::real
+  shows "{a <..< b} = ball ((a + b)/2) ((b - a)/2)"
+  by (auto simp: dist_real_def field_simps mem_ball)
+
+
+subsection \<open>Limit points\<close>
+
+definition%important (in topological_space) islimpt:: "'a \<Rightarrow> 'a set \<Rightarrow> bool"  (infixr "islimpt" 60)
+  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))"
+
+lemma islimptI:
+  assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
+  shows "x islimpt S"
+  using assms unfolding islimpt_def by auto
+
+lemma islimptE:
+  assumes "x islimpt S" and "x \<in> T" and "open T"
+  obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
+  using assms unfolding islimpt_def by auto
+
+lemma islimpt_iff_eventually: "x islimpt S \<longleftrightarrow> \<not> eventually (\<lambda>y. y \<notin> S) (at x)"
+  unfolding islimpt_def eventually_at_topological by auto
+
+lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> x islimpt T"
+  unfolding islimpt_def by fast
+
+lemma islimpt_approachable:
+  fixes x :: "'a::metric_space"
+  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
+  unfolding islimpt_iff_eventually eventually_at by fast
+
+lemma islimpt_approachable_le: "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x \<le> e)"
+  for x :: "'a::metric_space"
+  unfolding islimpt_approachable
+  using approachable_lt_le [where f="\<lambda>y. dist y x" and P="\<lambda>y. y \<notin> S \<or> y = x",
+    THEN arg_cong [where f=Not]]
+  by (simp add: Bex_def conj_commute conj_left_commute)
+
+lemma islimpt_UNIV_iff: "x islimpt UNIV \<longleftrightarrow> \<not> open {x}"
+  unfolding islimpt_def by (safe, fast, case_tac "T = {x}", fast, fast)
+
+lemma islimpt_punctured: "x islimpt S = x islimpt (S-{x})"
+  unfolding islimpt_def by blast
+
+text \<open>A perfect space has no isolated points.\<close>
+
+lemma islimpt_UNIV [simp, intro]: "x islimpt UNIV"
+  for x :: "'a::perfect_space"
+  unfolding islimpt_UNIV_iff by (rule not_open_singleton)
+
+lemma perfect_choose_dist: "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
+  for x :: "'a::{perfect_space,metric_space}"
+  using islimpt_UNIV [of x] by (simp add: islimpt_approachable)
+
+lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
+  unfolding closed_def
+  apply (subst open_subopen)
+  apply (simp add: islimpt_def subset_eq)
+  apply (metis ComplE ComplI)
+  done
+
+lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
+  by (auto simp: islimpt_def)
+
+lemma finite_ball_include:
+  fixes a :: "'a::metric_space"
+  assumes "finite S" 
+  shows "\<exists>e>0. S \<subseteq> ball a e"
+  using assms
+proof induction
+  case (insert x S)
+  then obtain e0 where "e0>0" and e0:"S \<subseteq> ball a e0" by auto
+  define e where "e = max e0 (2 * dist a x)"
+  have "e>0" unfolding e_def using \<open>e0>0\<close> by auto
+  moreover have "insert x S \<subseteq> ball a e"
+    using e0 \<open>e>0\<close> unfolding e_def by auto
+  ultimately show ?case by auto
+qed (auto intro: zero_less_one)
+
+lemma finite_set_avoid:
+  fixes a :: "'a::metric_space"
+  assumes "finite S"
+  shows "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
+  using assms
+proof induction
+  case (insert x S)
+  then obtain d where "d > 0" and d: "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
+    by blast
+  show ?case
+  proof (cases "x = a")
+    case True
+    with \<open>d > 0 \<close>d show ?thesis by auto
+  next
+    case False
+    let ?d = "min d (dist a x)"
+    from False \<open>d > 0\<close> have dp: "?d > 0"
+      by auto
+    from d have d': "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> ?d \<le> dist a x"
+      by auto
+    with dp False show ?thesis
+      by (metis insert_iff le_less min_less_iff_conj not_less)
+  qed
+qed (auto intro: zero_less_one)
+
+lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
+  by (simp add: islimpt_iff_eventually eventually_conj_iff)
+
+lemma discrete_imp_closed:
+  fixes S :: "'a::metric_space set"
+  assumes e: "0 < e"
+    and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
+  shows "closed S"
+proof -
+  have False if C: "\<And>e. e>0 \<Longrightarrow> \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" for x
+  proof -
+    from e have e2: "e/2 > 0" by arith
+    from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y \<noteq> x" "dist y x < e/2"
+      by blast
+    let ?m = "min (e/2) (dist x y) "
+    from e2 y(2) have mp: "?m > 0"
+      by simp
+    from C[OF mp] obtain z where z: "z \<in> S" "z \<noteq> x" "dist z x < ?m"
+      by blast
+    from z y have "dist z y < e"
+      by (intro dist_triangle_lt [where z=x]) simp
+    from d[rule_format, OF y(1) z(1) this] y z show ?thesis
+      by (auto simp: dist_commute)
+  qed
+  then show ?thesis
+    by (metis islimpt_approachable closed_limpt [where 'a='a])
+qed
+
+lemma closed_of_nat_image: "closed (of_nat ` A :: 'a::real_normed_algebra_1 set)"
+  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_nat)
+
+lemma closed_of_int_image: "closed (of_int ` A :: 'a::real_normed_algebra_1 set)"
+  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_int)
+
+lemma closed_Nats [simp]: "closed (\<nat> :: 'a :: real_normed_algebra_1 set)"
+  unfolding Nats_def by (rule closed_of_nat_image)
+
+lemma closed_Ints [simp]: "closed (\<int> :: 'a :: real_normed_algebra_1 set)"
+  unfolding Ints_def by (rule closed_of_int_image)
+
+lemma closed_subset_Ints:
+  fixes A :: "'a :: real_normed_algebra_1 set"
+  assumes "A \<subseteq> \<int>"
+  shows   "closed A"
+proof (intro discrete_imp_closed[OF zero_less_one] ballI impI, goal_cases)
+  case (1 x y)
+  with assms have "x \<in> \<int>" and "y \<in> \<int>" by auto
+  with \<open>dist y x < 1\<close> show "y = x"
+    by (auto elim!: Ints_cases simp: dist_of_int)
+qed
+
+
+subsection \<open>Interior of a Set\<close>
+
+definition%important "interior S = \<Union>{T. open T \<and> T \<subseteq> S}"
+
+lemma interiorI [intro?]:
+  assumes "open T" and "x \<in> T" and "T \<subseteq> S"
+  shows "x \<in> interior S"
+  using assms unfolding interior_def by fast
+
+lemma interiorE [elim?]:
+  assumes "x \<in> interior S"
+  obtains T where "open T" and "x \<in> T" and "T \<subseteq> S"
+  using assms unfolding interior_def by fast
+
+lemma open_interior [simp, intro]: "open (interior S)"
+  by (simp add: interior_def open_Union)
+
+lemma interior_subset: "interior S \<subseteq> S"
+  by (auto simp: interior_def)
+
+lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> interior S"
+  by (auto simp: interior_def)
+
+lemma interior_open: "open S \<Longrightarrow> interior S = S"
+  by (intro equalityI interior_subset interior_maximal subset_refl)
+
+lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
+  by (metis open_interior interior_open)
+
+lemma open_subset_interior: "open S \<Longrightarrow> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
+  by (metis interior_maximal interior_subset subset_trans)
+
+lemma interior_empty [simp]: "interior {} = {}"
+  using open_empty by (rule interior_open)
+
+lemma interior_UNIV [simp]: "interior UNIV = UNIV"
+  using open_UNIV by (rule interior_open)
+
+lemma interior_interior [simp]: "interior (interior S) = interior S"
+  using open_interior by (rule interior_open)
+
+lemma interior_mono: "S \<subseteq> T \<Longrightarrow> interior S \<subseteq> interior T"
+  by (auto simp: interior_def)
+
+lemma interior_unique:
+  assumes "T \<subseteq> S" and "open T"
+  assumes "\<And>T'. T' \<subseteq> S \<Longrightarrow> open T' \<Longrightarrow> T' \<subseteq> T"
+  shows "interior S = T"
+  by (intro equalityI assms interior_subset open_interior interior_maximal)
+
+lemma interior_singleton [simp]: "interior {a} = {}"
+  for a :: "'a::perfect_space"
+  apply (rule interior_unique, simp_all)
+  using not_open_singleton subset_singletonD
+  apply fastforce
+  done
+
+lemma interior_Int [simp]: "interior (S \<inter> T) = interior S \<inter> interior T"
+  by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1
+    Int_lower2 interior_maximal interior_subset open_Int open_interior)
+
+lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
+  using open_contains_ball_eq [where S="interior S"]
+  by (simp add: open_subset_interior)
+
+lemma eventually_nhds_in_nhd: "x \<in> interior s \<Longrightarrow> eventually (\<lambda>y. y \<in> s) (nhds x)"
+  using interior_subset[of s] by (subst eventually_nhds) blast
+
+lemma interior_limit_point [intro]:
+  fixes x :: "'a::perfect_space"
+  assumes x: "x \<in> interior S"
+  shows "x islimpt S"
+  using x islimpt_UNIV [of x]
+  unfolding interior_def islimpt_def
+  apply (clarsimp, rename_tac T T')
+  apply (drule_tac x="T \<inter> T'" in spec)
+  apply (auto simp: open_Int)
+  done
+
+lemma interior_closed_Un_empty_interior:
+  assumes cS: "closed S"
+    and iT: "interior T = {}"
+  shows "interior (S \<union> T) = interior S"
+proof
+  show "interior S \<subseteq> interior (S \<union> T)"
+    by (rule interior_mono) (rule Un_upper1)
+  show "interior (S \<union> T) \<subseteq> interior S"
+  proof
+    fix x
+    assume "x \<in> interior (S \<union> T)"
+    then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T" ..
+    show "x \<in> interior S"
+    proof (rule ccontr)
+      assume "x \<notin> interior S"
+      with \<open>x \<in> R\<close> \<open>open R\<close> obtain y where "y \<in> R - S"
+        unfolding interior_def by fast
+      from \<open>open R\<close> \<open>closed S\<close> have "open (R - S)"
+        by (rule open_Diff)
+      from \<open>R \<subseteq> S \<union> T\<close> have "R - S \<subseteq> T"
+        by fast
+      from \<open>y \<in> R - S\<close> \<open>open (R - S)\<close> \<open>R - S \<subseteq> T\<close> \<open>interior T = {}\<close> show False
+        unfolding interior_def by fast
+    qed
+  qed
+qed
+
+lemma interior_Times: "interior (A \<times> B) = interior A \<times> interior B"
+proof (rule interior_unique)
+  show "interior A \<times> interior B \<subseteq> A \<times> B"
+    by (intro Sigma_mono interior_subset)
+  show "open (interior A \<times> interior B)"
+    by (intro open_Times open_interior)
+  fix T
+  assume "T \<subseteq> A \<times> B" and "open T"
+  then show "T \<subseteq> interior A \<times> interior B"
+  proof safe
+    fix x y
+    assume "(x, y) \<in> T"
+    then obtain C D where "open C" "open D" "C \<times> D \<subseteq> T" "x \<in> C" "y \<in> D"
+      using \<open>open T\<close> unfolding open_prod_def by fast
+    then have "open C" "open D" "C \<subseteq> A" "D \<subseteq> B" "x \<in> C" "y \<in> D"
+      using \<open>T \<subseteq> A \<times> B\<close> by auto
+    then show "x \<in> interior A" and "y \<in> interior B"
+      by (auto intro: interiorI)
+  qed
+qed
+
+lemma interior_Ici:
+  fixes x :: "'a :: {dense_linorder,linorder_topology}"
+  assumes "b < x"
+  shows "interior {x ..} = {x <..}"
+proof (rule interior_unique)
+  fix T
+  assume "T \<subseteq> {x ..}" "open T"
+  moreover have "x \<notin> T"
+  proof
+    assume "x \<in> T"
+    obtain y where "y < x" "{y <.. x} \<subseteq> T"
+      using open_left[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>b < x\<close>] by auto
+    with dense[OF \<open>y < x\<close>] obtain z where "z \<in> T" "z < x"
+      by (auto simp: subset_eq Ball_def)
+    with \<open>T \<subseteq> {x ..}\<close> show False by auto
+  qed
+  ultimately show "T \<subseteq> {x <..}"
+    by (auto simp: subset_eq less_le)
+qed auto
+
+lemma interior_Iic:
+  fixes x :: "'a ::{dense_linorder,linorder_topology}"
+  assumes "x < b"
+  shows "interior {.. x} = {..< x}"
+proof (rule interior_unique)
+  fix T
+  assume "T \<subseteq> {.. x}" "open T"
+  moreover have "x \<notin> T"
+  proof
+    assume "x \<in> T"
+    obtain y where "x < y" "{x ..< y} \<subseteq> T"
+      using open_right[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>x < b\<close>] by auto
+    with dense[OF \<open>x < y\<close>] obtain z where "z \<in> T" "x < z"
+      by (auto simp: subset_eq Ball_def less_le)
+    with \<open>T \<subseteq> {.. x}\<close> show False by auto
+  qed
+  ultimately show "T \<subseteq> {..< x}"
+    by (auto simp: subset_eq less_le)
+qed auto
+
+
+subsection \<open>Closure of a Set\<close>
+
+definition%important "closure S = S \<union> {x | x. x islimpt S}"
+
+lemma interior_closure: "interior S = - (closure (- S))"
+  by (auto simp: interior_def closure_def islimpt_def)
+
+lemma closure_interior: "closure S = - interior (- S)"
+  by (simp add: interior_closure)
+
+lemma closed_closure[simp, intro]: "closed (closure S)"
+  by (simp add: closure_interior closed_Compl)
+
+lemma closure_subset: "S \<subseteq> closure S"
+  by (simp add: closure_def)
+
+lemma closure_hull: "closure S = closed hull S"
+  by (auto simp: hull_def closure_interior interior_def)
+
+lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
+  unfolding closure_hull using closed_Inter by (rule hull_eq)
+
+lemma closure_closed [simp]: "closed S \<Longrightarrow> closure S = S"
+  by (simp only: closure_eq)
+
+lemma closure_closure [simp]: "closure (closure S) = closure S"
+  unfolding closure_hull by (rule hull_hull)
+
+lemma closure_mono: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
+  unfolding closure_hull by (rule hull_mono)
+
+lemma closure_minimal: "S \<subseteq> T \<Longrightarrow> closed T \<Longrightarrow> closure S \<subseteq> T"
+  unfolding closure_hull by (rule hull_minimal)
+
+lemma closure_unique:
+  assumes "S \<subseteq> T"
+    and "closed T"
+    and "\<And>T'. S \<subseteq> T' \<Longrightarrow> closed T' \<Longrightarrow> T \<subseteq> T'"
+  shows "closure S = T"
+  using assms unfolding closure_hull by (rule hull_unique)
+
+lemma closure_empty [simp]: "closure {} = {}"
+  using closed_empty by (rule closure_closed)
+
+lemma closure_UNIV [simp]: "closure UNIV = UNIV"
+  using closed_UNIV by (rule closure_closed)
+
+lemma closure_Un [simp]: "closure (S \<union> T) = closure S \<union> closure T"
+  by (simp add: closure_interior)
+
+lemma closure_eq_empty [iff]: "closure S = {} \<longleftrightarrow> S = {}"
+  using closure_empty closure_subset[of S] by blast
+
+lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
+  using closure_eq[of S] closure_subset[of S] by simp
+
+lemma open_Int_closure_eq_empty: "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
+  using open_subset_interior[of S "- T"]
+  using interior_subset[of "- T"]
+  by (auto simp: closure_interior)
+
+lemma open_Int_closure_subset: "open S \<Longrightarrow> S \<inter> closure T \<subseteq> closure (S \<inter> T)"
+proof
+  fix x
+  assume *: "open S" "x \<in> S \<inter> closure T"
+  have "x islimpt (S \<inter> T)" if **: "x islimpt T"
+  proof (rule islimptI)
+    fix A
+    assume "x \<in> A" "open A"
+    with * have "x \<in> A \<inter> S" "open (A \<inter> S)"
+      by (simp_all add: open_Int)
+    with ** obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
+      by (rule islimptE)
+    then have "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
+      by simp_all
+    then show "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
+  qed
+  with * show "x \<in> closure (S \<inter> T)"
+    unfolding closure_def by blast
+qed
+
+lemma closure_complement: "closure (- S) = - interior S"
+  by (simp add: closure_interior)
+
+lemma interior_complement: "interior (- S) = - closure S"
+  by (simp add: closure_interior)
+
+lemma interior_diff: "interior(S - T) = interior S - closure T"
+  by (simp add: Diff_eq interior_complement)
+
+lemma closure_Times: "closure (A \<times> B) = closure A \<times> closure B"
+proof (rule closure_unique)
+  show "A \<times> B \<subseteq> closure A \<times> closure B"
+    by (intro Sigma_mono closure_subset)
+  show "closed (closure A \<times> closure B)"
+    by (intro closed_Times closed_closure)
+  fix T
+  assume "A \<times> B \<subseteq> T" and "closed T"
+  then show "closure A \<times> closure B \<subseteq> T"
+    apply (simp add: closed_def open_prod_def, clarify)
+    apply (rule ccontr)
+    apply (drule_tac x="(a, b)" in bspec, simp, clarify, rename_tac C D)
+    apply (simp add: closure_interior interior_def)
+    apply (drule_tac x=C in spec)
+    apply (drule_tac x=D in spec, auto)
+    done
+qed
+
+lemma closure_openin_Int_closure:
+  assumes ope: "openin (subtopology euclidean U) S" and "T \<subseteq> U"
+  shows "closure(S \<inter> closure T) = closure(S \<inter> T)"
+proof
+  obtain V where "open V" and S: "S = U \<inter> V"
+    using ope using openin_open by metis
+  show "closure (S \<inter> closure T) \<subseteq> closure (S \<inter> T)"
+    proof (clarsimp simp: S)
+      fix x
+      assume  "x \<in> closure (U \<inter> V \<inter> closure T)"
+      then have "V \<inter> closure T \<subseteq> A \<Longrightarrow> x \<in> closure A" for A
+          by (metis closure_mono subsetD inf.coboundedI2 inf_assoc)
+      then have "x \<in> closure (T \<inter> V)"
+         by (metis \<open>open V\<close> closure_closure inf_commute open_Int_closure_subset)
+      then show "x \<in> closure (U \<inter> V \<inter> T)"
+        by (metis \<open>T \<subseteq> U\<close> inf.absorb_iff2 inf_assoc inf_commute)
+    qed
+next
+  show "closure (S \<inter> T) \<subseteq> closure (S \<inter> closure T)"
+    by (meson Int_mono closure_mono closure_subset order_refl)
+qed
+
+lemma islimpt_in_closure: "(x islimpt S) = (x\<in>closure(S-{x}))"
+  unfolding closure_def using islimpt_punctured by blast
+
+lemma connected_imp_connected_closure: "connected S \<Longrightarrow> connected (closure S)"
+  by (rule connectedI) (meson closure_subset open_Int open_Int_closure_eq_empty subset_trans connectedD)
+
+lemma limpt_of_limpts: "x islimpt {y. y islimpt S} \<Longrightarrow> x islimpt S"
+  for x :: "'a::metric_space"
+  apply (clarsimp simp add: islimpt_approachable)
+  apply (drule_tac x="e/2" in spec)
+  apply (auto simp: simp del: less_divide_eq_numeral1)
+  apply (drule_tac x="dist x' x" in spec)
+  apply (auto simp: zero_less_dist_iff simp del: less_divide_eq_numeral1)
+  apply (erule rev_bexI)
+  apply (metis dist_commute dist_triangle_half_r less_trans less_irrefl)
+  done
+
+lemma closed_limpts:  "closed {x::'a::metric_space. x islimpt S}"
+  using closed_limpt limpt_of_limpts by blast
+
+lemma limpt_of_closure: "x islimpt closure S \<longleftrightarrow> x islimpt S"
+  for x :: "'a::metric_space"
+  by (auto simp: closure_def islimpt_Un dest: limpt_of_limpts)
+
+lemma closedin_limpt:
+  "closedin (subtopology euclidean T) S \<longleftrightarrow> S \<subseteq> T \<and> (\<forall>x. x islimpt S \<and> x \<in> T \<longrightarrow> x \<in> S)"
+  apply (simp add: closedin_closed, safe)
+   apply (simp add: closed_limpt islimpt_subset)
+  apply (rule_tac x="closure S" in exI, simp)
+  apply (force simp: closure_def)
+  done
+
+lemma closedin_closed_eq: "closed S \<Longrightarrow> closedin (subtopology euclidean S) T \<longleftrightarrow> closed T \<and> T \<subseteq> S"
+  by (meson closedin_limpt closed_subset closedin_closed_trans)
+
+lemma connected_closed_set:
+   "closed S
+    \<Longrightarrow> connected S \<longleftrightarrow> (\<nexists>A B. closed A \<and> closed B \<and> A \<noteq> {} \<and> B \<noteq> {} \<and> A \<union> B = S \<and> A \<inter> B = {})"
+  unfolding connected_closedin_eq closedin_closed_eq connected_closedin_eq by blast
+
+text \<open>If a connnected set is written as the union of two nonempty closed sets, then these sets
+have to intersect.\<close>
+
+lemma connected_as_closed_union:
+  assumes "connected C" "C = A \<union> B" "closed A" "closed B" "A \<noteq> {}" "B \<noteq> {}"
+  shows "A \<inter> B \<noteq> {}"
+by (metis assms closed_Un connected_closed_set)
+
+lemma closedin_subset_trans:
+  "closedin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
+    closedin (subtopology euclidean T) S"
+  by (meson closedin_limpt subset_iff)
+
+lemma openin_subset_trans:
+  "openin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
+    openin (subtopology euclidean T) S"
+  by (auto simp: openin_open)
+
+lemma openin_Times:
+  "openin (subtopology euclidean S) S' \<Longrightarrow> openin (subtopology euclidean T) T' \<Longrightarrow>
+    openin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
+  unfolding openin_open using open_Times by blast
+
+lemma Times_in_interior_subtopology:
+  fixes U :: "('a::metric_space \<times> 'b::metric_space) set"
+  assumes "(x, y) \<in> U" "openin (subtopology euclidean (S \<times> T)) U"
+  obtains V W where "openin (subtopology euclidean S) V" "x \<in> V"
+                    "openin (subtopology euclidean T) W" "y \<in> W" "(V \<times> W) \<subseteq> U"
+proof -
+  from assms obtain e where "e > 0" and "U \<subseteq> S \<times> T"
+    and e: "\<And>x' y'. \<lbrakk>x'\<in>S; y'\<in>T; dist (x', y') (x, y) < e\<rbrakk> \<Longrightarrow> (x', y') \<in> U"
+    by (force simp: openin_euclidean_subtopology_iff)
+  with assms have "x \<in> S" "y \<in> T"
+    by auto
+  show ?thesis
+  proof
+    show "openin (subtopology euclidean S) (ball x (e/2) \<inter> S)"
+      by (simp add: Int_commute openin_open_Int)
+    show "x \<in> ball x (e / 2) \<inter> S"
+      by (simp add: \<open>0 < e\<close> \<open>x \<in> S\<close>)
+    show "openin (subtopology euclidean T) (ball y (e/2) \<inter> T)"
+      by (simp add: Int_commute openin_open_Int)
+    show "y \<in> ball y (e / 2) \<inter> T"
+      by (simp add: \<open>0 < e\<close> \<open>y \<in> T\<close>)
+    show "(ball x (e / 2) \<inter> S) \<times> (ball y (e / 2) \<inter> T) \<subseteq> U"
+      by clarify (simp add: e dist_Pair_Pair \<open>0 < e\<close> dist_commute sqrt_sum_squares_half_less)
+  qed
+qed
+
+lemma openin_Times_eq:
+  fixes S :: "'a::metric_space set" and T :: "'b::metric_space set"
+  shows
+    "openin (subtopology euclidean (S \<times> T)) (S' \<times> T') \<longleftrightarrow>
+      S' = {} \<or> T' = {} \<or> openin (subtopology euclidean S) S' \<and> openin (subtopology euclidean T) T'"
+    (is "?lhs = ?rhs")
+proof (cases "S' = {} \<or> T' = {}")
+  case True
+  then show ?thesis by auto
+next
+  case False
+  then obtain x y where "x \<in> S'" "y \<in> T'"
+    by blast
+  show ?thesis
+  proof
+    assume ?lhs
+    have "openin (subtopology euclidean S) S'"
+      apply (subst openin_subopen, clarify)
+      apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
+      using \<open>y \<in> T'\<close>
+       apply auto
+      done
+    moreover have "openin (subtopology euclidean T) T'"
+      apply (subst openin_subopen, clarify)
+      apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
+      using \<open>x \<in> S'\<close>
+       apply auto
+      done
+    ultimately show ?rhs
+      by simp
+  next
+    assume ?rhs
+    with False show ?lhs
+      by (simp add: openin_Times)
+  qed
+qed
+
+lemma closedin_Times:
+  "closedin (subtopology euclidean S) S' \<Longrightarrow> closedin (subtopology euclidean T) T' \<Longrightarrow>
+    closedin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
+  unfolding closedin_closed using closed_Times by blast
+
+lemma bdd_below_closure:
+  fixes A :: "real set"
+  assumes "bdd_below A"
+  shows "bdd_below (closure A)"
+proof -
+  from assms obtain m where "\<And>x. x \<in> A \<Longrightarrow> m \<le> x"
+    by (auto simp: bdd_below_def)
+  then have "A \<subseteq> {m..}" by auto
+  then have "closure A \<subseteq> {m..}"
+    using closed_real_atLeast by (rule closure_minimal)
+  then show ?thesis
+    by (auto simp: bdd_below_def)
+qed
+
+
+subsection \<open>Frontier (also known as boundary)\<close>
+
+definition%important "frontier S = closure S - interior S"
+
+lemma frontier_closed [iff]: "closed (frontier S)"
+  by (simp add: frontier_def closed_Diff)
+
+lemma frontier_closures: "frontier S = closure S \<inter> closure (- S)"
+  by (auto simp: frontier_def interior_closure)
+
+lemma frontier_Int: "frontier(S \<inter> T) = closure(S \<inter> T) \<inter> (frontier S \<union> frontier T)"
+proof -
+  have "closure (S \<inter> T) \<subseteq> closure S" "closure (S \<inter> T) \<subseteq> closure T"
+    by (simp_all add: closure_mono)
+  then show ?thesis
+    by (auto simp: frontier_closures)
+qed
+
+lemma frontier_Int_subset: "frontier(S \<inter> T) \<subseteq> frontier S \<union> frontier T"
+  by (auto simp: frontier_Int)
+
+lemma frontier_Int_closed:
+  assumes "closed S" "closed T"
+  shows "frontier(S \<inter> T) = (frontier S \<inter> T) \<union> (S \<inter> frontier T)"
+proof -
+  have "closure (S \<inter> T) = T \<inter> S"
+    using assms by (simp add: Int_commute closed_Int)
+  moreover have "T \<inter> (closure S \<inter> closure (- S)) = frontier S \<inter> T"
+    by (simp add: Int_commute frontier_closures)
+  ultimately show ?thesis
+    by (simp add: Int_Un_distrib Int_assoc Int_left_commute assms frontier_closures)
+qed
+
+lemma frontier_straddle:
+  fixes a :: "'a::metric_space"
+  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))"
+  unfolding frontier_def closure_interior
+  by (auto simp: mem_interior subset_eq ball_def)
+
+lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
+  by (metis frontier_def closure_closed Diff_subset)
+
+lemma frontier_empty [simp]: "frontier {} = {}"
+  by (simp add: frontier_def)
+
+lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
+proof -
+  {
+    assume "frontier S \<subseteq> S"
+    then have "closure S \<subseteq> S"
+      using interior_subset unfolding frontier_def by auto
+    then have "closed S"
+      using closure_subset_eq by auto
+  }
+  then show ?thesis using frontier_subset_closed[of S] ..
+qed
+
+lemma frontier_complement [simp]: "frontier (- S) = frontier S"
+  by (auto simp: frontier_def closure_complement interior_complement)
+
+lemma frontier_Un_subset: "frontier(S \<union> T) \<subseteq> frontier S \<union> frontier T"
+  by (metis compl_sup frontier_Int_subset frontier_complement)
+
+lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
+  using frontier_complement frontier_subset_eq[of "- S"]
+  unfolding open_closed by auto
+
+lemma frontier_UNIV [simp]: "frontier UNIV = {}"
+  using frontier_complement frontier_empty by fastforce
+
+lemma frontier_interiors: "frontier s = - interior(s) - interior(-s)"
+  by (simp add: Int_commute frontier_def interior_closure)
+
+lemma frontier_interior_subset: "frontier(interior S) \<subseteq> frontier S"
+  by (simp add: Diff_mono frontier_interiors interior_mono interior_subset)
+
+lemma connected_Int_frontier:
+     "\<lbrakk>connected s; s \<inter> t \<noteq> {}; s - t \<noteq> {}\<rbrakk> \<Longrightarrow> (s \<inter> frontier t \<noteq> {})"
+  apply (simp add: frontier_interiors connected_openin, safe)
+  apply (drule_tac x="s \<inter> interior t" in spec, safe)
+   apply (drule_tac [2] x="s \<inter> interior (-t)" in spec)
+   apply (auto simp: disjoint_eq_subset_Compl dest: interior_subset [THEN subsetD])
+  done
+
+lemma closure_Un_frontier: "closure S = S \<union> frontier S"
+proof -
+  have "S \<union> interior S = S"
+    using interior_subset by auto
+  then show ?thesis
+    using closure_subset by (auto simp: frontier_def)
+qed
+
+
+subsection%unimportant \<open>Filters and the ``eventually true'' quantifier\<close>
+
+definition indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a filter"  (infixr "indirection" 70)
+  where "a indirection v = at a within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
+
+text \<open>Identify Trivial limits, where we can't approach arbitrarily closely.\<close>
+
+lemma trivial_limit_within: "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
+proof
+  assume "trivial_limit (at a within S)"
+  then show "\<not> a islimpt S"
+    unfolding trivial_limit_def
+    unfolding eventually_at_topological
+    unfolding islimpt_def
+    apply (clarsimp simp add: set_eq_iff)
+    apply (rename_tac T, rule_tac x=T in exI)
+    apply (clarsimp, drule_tac x=y in bspec, simp_all)
+    done
+next
+  assume "\<not> a islimpt S"
+  then show "trivial_limit (at a within S)"
+    unfolding trivial_limit_def eventually_at_topological islimpt_def
+    by metis
+qed
+
+lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
+  using trivial_limit_within [of a UNIV] by simp
+
+lemma trivial_limit_at: "\<not> trivial_limit (at a)"
+  for a :: "'a::perfect_space"
+  by (rule at_neq_bot)
+
+lemma trivial_limit_at_infinity:
+  "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,perfect_space}) filter)"
+  unfolding trivial_limit_def eventually_at_infinity
+  apply clarsimp
+  apply (subgoal_tac "\<exists>x::'a. x \<noteq> 0", clarify)
+   apply (rule_tac x="scaleR (b / norm x) x" in exI, simp)
+  apply (cut_tac islimpt_UNIV [of "0::'a", unfolded islimpt_def])
+  apply (drule_tac x=UNIV in spec, simp)
+  done
+
+lemma not_trivial_limit_within: "\<not> trivial_limit (at x within S) = (x \<in> closure (S - {x}))"
+  using islimpt_in_closure by (metis trivial_limit_within)
+
+lemma not_in_closure_trivial_limitI:
+  "x \<notin> closure s \<Longrightarrow> trivial_limit (at x within s)"
+  using not_trivial_limit_within[of x s]
+  by safe (metis Diff_empty Diff_insert0 closure_subset contra_subsetD)
+
+lemma filterlim_at_within_closure_implies_filterlim: "filterlim f l (at x within s)"
+  if "x \<in> closure s \<Longrightarrow> filterlim f l (at x within s)"
+  by (metis bot.extremum filterlim_filtercomap filterlim_mono not_in_closure_trivial_limitI that)
+
+lemma at_within_eq_bot_iff: "at c within A = bot \<longleftrightarrow> c \<notin> closure (A - {c})"
+  using not_trivial_limit_within[of c A] by blast
+
+text \<open>Some property holds "sufficiently close" to the limit point.\<close>
+
+lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
+  by simp
+
+lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
+  by (simp add: filter_eq_iff)
+
+
+subsection \<open>Limits\<close>
+
+proposition Lim: "(f \<longlongrightarrow> l) net \<longleftrightarrow> trivial_limit net \<or> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
+  by (auto simp: tendsto_iff trivial_limit_eq)
+
+text \<open>Show that they yield usual definitions in the various cases.\<close>
+
+proposition Lim_within_le: "(f \<longlongrightarrow> l)(at a within S) \<longleftrightarrow>
+    (\<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)"
+  by (auto simp: tendsto_iff eventually_at_le)
+
+proposition Lim_within: "(f \<longlongrightarrow> l) (at a within S) \<longleftrightarrow>
+    (\<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)"
+  by (auto simp: tendsto_iff eventually_at)
+
+corollary Lim_withinI [intro?]:
+  assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>d>0. \<forall>x \<in> S. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) l \<le> e"
+  shows "(f \<longlongrightarrow> l) (at a within S)"
+  apply (simp add: Lim_within, clarify)
+  apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
+  done
+
+proposition Lim_at: "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow>
+    (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d  \<longrightarrow> dist (f x) l < e)"
+  by (auto simp: tendsto_iff eventually_at)
+
+proposition Lim_at_infinity: "(f \<longlongrightarrow> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x \<ge> b \<longrightarrow> dist (f x) l < e)"
+  by (auto simp: tendsto_iff eventually_at_infinity)
+
+corollary Lim_at_infinityI [intro?]:
+  assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>B. \<forall>x. norm x \<ge> B \<longrightarrow> dist (f x) l \<le> e"
+  shows "(f \<longlongrightarrow> l) at_infinity"
+  apply (simp add: Lim_at_infinity, clarify)
+  apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
+  done
+
+lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f \<longlongrightarrow> l) net"
+  by (rule topological_tendstoI) (auto elim: eventually_mono)
+
+lemma Lim_transform_within_set:
+  fixes a :: "'a::metric_space" and l :: "'b::metric_space"
+  shows "\<lbrakk>(f \<longlongrightarrow> l) (at a within S); eventually (\<lambda>x. x \<in> S \<longleftrightarrow> x \<in> T) (at a)\<rbrakk>
+         \<Longrightarrow> (f \<longlongrightarrow> l) (at a within T)"
+apply (clarsimp simp: eventually_at Lim_within)
+apply (drule_tac x=e in spec, clarify)
+apply (rename_tac k)
+apply (rule_tac x="min d k" in exI, simp)
+done
+
+lemma Lim_transform_within_set_eq:
+  fixes a l :: "'a::real_normed_vector"
+  shows "eventually (\<lambda>x. x \<in> s \<longleftrightarrow> x \<in> t) (at a)
+         \<Longrightarrow> ((f \<longlongrightarrow> l) (at a within s) \<longleftrightarrow> (f \<longlongrightarrow> l) (at a within t))"
+  by (force intro: Lim_transform_within_set elim: eventually_mono)
+
+lemma Lim_transform_within_openin:
+  fixes a :: "'a::metric_space"
+  assumes f: "(f \<longlongrightarrow> l) (at a within T)"
+    and "openin (subtopology euclidean T) S" "a \<in> S"
+    and eq: "\<And>x. \<lbrakk>x \<in> S; x \<noteq> a\<rbrakk> \<Longrightarrow> f x = g x"
+  shows "(g \<longlongrightarrow> l) (at a within T)"
+proof -
+  obtain \<epsilon> where "0 < \<epsilon>" and \<epsilon>: "ball a \<epsilon> \<inter> T \<subseteq> S"
+    using assms by (force simp: openin_contains_ball)
+  then have "a \<in> ball a \<epsilon>"
+    by simp
+  show ?thesis
+    by (rule Lim_transform_within [OF f \<open>0 < \<epsilon>\<close> eq]) (use \<epsilon> in \<open>auto simp: dist_commute subset_iff\<close>)
+qed
+
+lemma continuous_transform_within_openin:
+  fixes a :: "'a::metric_space"
+  assumes "continuous (at a within T) f"
+    and "openin (subtopology euclidean T) S" "a \<in> S"
+    and eq: "\<And>x. x \<in> S \<Longrightarrow> f x = g x"
+  shows "continuous (at a within T) g"
+  using assms by (simp add: Lim_transform_within_openin continuous_within)
+
+text \<open>The expected monotonicity property.\<close>
+
+lemma Lim_Un:
+  assumes "(f \<longlongrightarrow> l) (at x within S)" "(f \<longlongrightarrow> l) (at x within T)"
+  shows "(f \<longlongrightarrow> l) (at x within (S \<union> T))"
+  using assms unfolding at_within_union by (rule filterlim_sup)
+
+lemma Lim_Un_univ:
+  "(f \<longlongrightarrow> l) (at x within S) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within T) \<Longrightarrow>
+    S \<union> T = UNIV \<Longrightarrow> (f \<longlongrightarrow> l) (at x)"
+  by (metis Lim_Un)
+
+text \<open>Interrelations between restricted and unrestricted limits.\<close>
+
+lemma Lim_at_imp_Lim_at_within: "(f \<longlongrightarrow> l) (at x) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S)"
+  by (metis order_refl filterlim_mono subset_UNIV at_le)
+
+lemma eventually_within_interior:
+  assumes "x \<in> interior S"
+  shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)"
+  (is "?lhs = ?rhs")
+proof
+  from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S" ..
+  {
+    assume ?lhs
+    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"
+      by (auto simp: eventually_at_topological)
+    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"
+      by auto
+    then show ?rhs
+      by (auto simp: eventually_at_topological)
+  next
+    assume ?rhs
+    then show ?lhs
+      by (auto elim: eventually_mono simp: eventually_at_filter)
+  }
+qed
+
+lemma at_within_interior: "x \<in> interior S \<Longrightarrow> at x within S = at x"
+  unfolding filter_eq_iff by (intro allI eventually_within_interior)
+
+lemma Lim_within_LIMSEQ:
+  fixes a :: "'a::first_countable_topology"
+  assumes "\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
+  shows "(X \<longlongrightarrow> L) (at a within T)"
+  using assms unfolding tendsto_def [where l=L]
+  by (simp add: sequentially_imp_eventually_within)
+
+lemma Lim_right_bound:
+  fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder, no_top} \<Rightarrow>
+    'b::{linorder_topology, conditionally_complete_linorder}"
+  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"
+    and bnd: "\<And>a. a \<in> I \<Longrightarrow> x < a \<Longrightarrow> K \<le> f a"
+  shows "(f \<longlongrightarrow> Inf (f ` ({x<..} \<inter> I))) (at x within ({x<..} \<inter> I))"
+proof (cases "{x<..} \<inter> I = {}")
+  case True
+  then show ?thesis by simp
+next
+  case False
+  show ?thesis
+  proof (rule order_tendstoI)
+    fix a
+    assume a: "a < Inf (f ` ({x<..} \<inter> I))"
+    {
+      fix y
+      assume "y \<in> {x<..} \<inter> I"
+      with False bnd have "Inf (f ` ({x<..} \<inter> I)) \<le> f y"
+        by (auto intro!: cInf_lower bdd_belowI2)
+      with a have "a < f y"
+        by (blast intro: less_le_trans)
+    }
+    then show "eventually (\<lambda>x. a < f x) (at x within ({x<..} \<inter> I))"
+      by (auto simp: eventually_at_filter intro: exI[of _ 1] zero_less_one)
+  next
+    fix a
+    assume "Inf (f ` ({x<..} \<inter> I)) < a"
+    from cInf_lessD[OF _ this] False obtain y where y: "x < y" "y \<in> I" "f y < a"
+      by auto
+    then have "eventually (\<lambda>x. x \<in> I \<longrightarrow> f x < a) (at_right x)"
+      unfolding eventually_at_right[OF \<open>x < y\<close>] by (metis less_imp_le le_less_trans mono)
+    then show "eventually (\<lambda>x. f x < a) (at x within ({x<..} \<inter> I))"
+      unfolding eventually_at_filter by eventually_elim simp
+  qed
+qed
+
+text \<open>Another limit point characterization.\<close>
+
+lemma limpt_sequential_inj:
+  fixes x :: "'a::metric_space"
+  shows "x islimpt S \<longleftrightarrow>
+         (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> inj f \<and> (f \<longlongrightarrow> x) sequentially)"
+         (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then have "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
+    by (force simp: islimpt_approachable)
+  then obtain y where y: "\<And>e. e>0 \<Longrightarrow> y e \<in> S \<and> y e \<noteq> x \<and> dist (y e) x < e"
+    by metis
+  define f where "f \<equiv> rec_nat (y 1) (\<lambda>n fn. y (min (inverse(2 ^ (Suc n))) (dist fn x)))"
+  have [simp]: "f 0 = y 1"
+               "f(Suc n) = y (min (inverse(2 ^ (Suc n))) (dist (f n) x))" for n
+    by (simp_all add: f_def)
+  have f: "f n \<in> S \<and> (f n \<noteq> x) \<and> dist (f n) x < inverse(2 ^ n)" for n
+  proof (induction n)
+    case 0 show ?case
+      by (simp add: y)
+  next
+    case (Suc n) then show ?case
+      apply (auto simp: y)
+      by (metis half_gt_zero_iff inverse_positive_iff_positive less_divide_eq_numeral1(1) min_less_iff_conj y zero_less_dist_iff zero_less_numeral zero_less_power)
+  qed
+  show ?rhs
+  proof (rule_tac x=f in exI, intro conjI allI)
+    show "\<And>n. f n \<in> S - {x}"
+      using f by blast
+    have "dist (f n) x < dist (f m) x" if "m < n" for m n
+    using that
+    proof (induction n)
+      case 0 then show ?case by simp
+    next
+      case (Suc n)
+      then consider "m < n" | "m = n" using less_Suc_eq by blast
+      then show ?case
+      proof cases
+        assume "m < n"
+        have "dist (f(Suc n)) x = dist (y (min (inverse(2 ^ (Suc n))) (dist (f n) x))) x"
+          by simp
+        also have "\<dots> < dist (f n) x"
+          by (metis dist_pos_lt f min.strict_order_iff min_less_iff_conj y)
+        also have "\<dots> < dist (f m) x"
+          using Suc.IH \<open>m < n\<close> by blast
+        finally show ?thesis .
+      next
+        assume "m = n" then show ?case
+          by simp (metis dist_pos_lt f half_gt_zero_iff inverse_positive_iff_positive min_less_iff_conj y zero_less_numeral zero_less_power)
+      qed
+    qed
+    then show "inj f"
+      by (metis less_irrefl linorder_injI)
+    show "f \<longlonglongrightarrow> x"
+      apply (rule tendstoI)
+      apply (rule_tac c="nat (ceiling(1/e))" in eventually_sequentiallyI)
+      apply (rule less_trans [OF f [THEN conjunct2, THEN conjunct2]])
+      apply (simp add: field_simps)
+      by (meson le_less_trans mult_less_cancel_left not_le of_nat_less_two_power)
+  qed
+next
+  assume ?rhs
+  then show ?lhs
+    by (fastforce simp add: islimpt_approachable lim_sequentially)
+qed
+
+(*could prove directly from islimpt_sequential_inj, but only for metric spaces*)
+lemma islimpt_sequential:
+  fixes x :: "'a::first_countable_topology"
+  shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> (f \<longlongrightarrow> x) sequentially)"
+    (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  from countable_basis_at_decseq[of x] obtain A where A:
+      "\<And>i. open (A i)"
+      "\<And>i. x \<in> A i"
+      "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
+    by blast
+  define f where "f n = (SOME y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y)" for n
+  {
+    fix n
+    from \<open>?lhs\<close> have "\<exists>y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y"
+      unfolding islimpt_def using A(1,2)[of n] by auto
+    then have "f n \<in> S \<and> f n \<in> A n \<and> x \<noteq> f n"
+      unfolding f_def by (rule someI_ex)
+    then have "f n \<in> S" "f n \<in> A n" "x \<noteq> f n" by auto
+  }
+  then have "\<forall>n. f n \<in> S - {x}" by auto
+  moreover have "(\<lambda>n. f n) \<longlonglongrightarrow> x"
+  proof (rule topological_tendstoI)
+    fix S
+    assume "open S" "x \<in> S"
+    from A(3)[OF this] \<open>\<And>n. f n \<in> A n\<close>
+    show "eventually (\<lambda>x. f x \<in> S) sequentially"
+      by (auto elim!: eventually_mono)
+  qed
+  ultimately show ?rhs by fast
+next
+  assume ?rhs
+  then obtain f :: "nat \<Rightarrow> 'a" where f: "\<And>n. f n \<in> S - {x}" and lim: "f \<longlonglongrightarrow> x"
+    by auto
+  show ?lhs
+    unfolding islimpt_def
+  proof safe
+    fix T
+    assume "open T" "x \<in> T"
+    from lim[THEN topological_tendstoD, OF this] f
+    show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
+      unfolding eventually_sequentially by auto
+  qed
+qed
+
+lemma Lim_null:
+  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
+  shows "(f \<longlongrightarrow> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) \<longlongrightarrow> 0) net"
+  by (simp add: Lim dist_norm)
+
+lemma Lim_null_comparison:
+  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
+  assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g \<longlongrightarrow> 0) net"
+  shows "(f \<longlongrightarrow> 0) net"
+  using assms(2)
+proof (rule metric_tendsto_imp_tendsto)
+  show "eventually (\<lambda>x. dist (f x) 0 \<le> dist (g x) 0) net"
+    using assms(1) by (rule eventually_mono) (simp add: dist_norm)
+qed
+
+lemma Lim_transform_bound:
+  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
+    and g :: "'a \<Rightarrow> 'c::real_normed_vector"
+  assumes "eventually (\<lambda>n. norm (f n) \<le> norm (g n)) net"
+    and "(g \<longlongrightarrow> 0) net"
+  shows "(f \<longlongrightarrow> 0) net"
+  using assms(1) tendsto_norm_zero [OF assms(2)]
+  by (rule Lim_null_comparison)
+
+lemma lim_null_mult_right_bounded:
+  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
+  assumes f: "(f \<longlongrightarrow> 0) F" and g: "eventually (\<lambda>x. norm(g x) \<le> B) F"
+    shows "((\<lambda>z. f z * g z) \<longlongrightarrow> 0) F"
+proof -
+  have *: "((\<lambda>x. norm (f x) * B) \<longlongrightarrow> 0) F"
+    by (simp add: f tendsto_mult_left_zero tendsto_norm_zero)
+  have "((\<lambda>x. norm (f x) * norm (g x)) \<longlongrightarrow> 0) F"
+    apply (rule Lim_null_comparison [OF _ *])
+    apply (simp add: eventually_mono [OF g] mult_left_mono)
+    done
+  then show ?thesis
+    by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
+qed
+
+lemma lim_null_mult_left_bounded:
+  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
+  assumes g: "eventually (\<lambda>x. norm(g x) \<le> B) F" and f: "(f \<longlongrightarrow> 0) F"
+    shows "((\<lambda>z. g z * f z) \<longlongrightarrow> 0) F"
+proof -
+  have *: "((\<lambda>x. B * norm (f x)) \<longlongrightarrow> 0) F"
+    by (simp add: f tendsto_mult_right_zero tendsto_norm_zero)
+  have "((\<lambda>x. norm (g x) * norm (f x)) \<longlongrightarrow> 0) F"
+    apply (rule Lim_null_comparison [OF _ *])
+    apply (simp add: eventually_mono [OF g] mult_right_mono)
+    done
+  then show ?thesis
+    by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
+qed
+
+lemma lim_null_scaleR_bounded:
+  assumes f: "(f \<longlongrightarrow> 0) net" and gB: "eventually (\<lambda>a. f a = 0 \<or> norm(g a) \<le> B) net"
+    shows "((\<lambda>n. f n *\<^sub>R g n) \<longlongrightarrow> 0) net"
+proof
+  fix \<epsilon>::real
+  assume "0 < \<epsilon>"
+  then have B: "0 < \<epsilon> / (abs B + 1)" by simp
+  have *: "\<bar>f x\<bar> * norm (g x) < \<epsilon>" if f: "\<bar>f x\<bar> * (\<bar>B\<bar> + 1) < \<epsilon>" and g: "norm (g x) \<le> B" for x
+  proof -
+    have "\<bar>f x\<bar> * norm (g x) \<le> \<bar>f x\<bar> * B"
+      by (simp add: mult_left_mono g)
+    also have "\<dots> \<le> \<bar>f x\<bar> * (\<bar>B\<bar> + 1)"
+      by (simp add: mult_left_mono)
+    also have "\<dots> < \<epsilon>"
+      by (rule f)
+    finally show ?thesis .
+  qed
+  show "\<forall>\<^sub>F x in net. dist (f x *\<^sub>R g x) 0 < \<epsilon>"
+    apply (rule eventually_mono [OF eventually_conj [OF tendstoD [OF f B] gB] ])
+    apply (auto simp: \<open>0 < \<epsilon>\<close> divide_simps * split: if_split_asm)
+    done
+qed
+
+text\<open>Deducing things about the limit from the elements.\<close>
+
+lemma Lim_in_closed_set:
+  assumes "closed S"
+    and "eventually (\<lambda>x. f(x) \<in> S) net"
+    and "\<not> trivial_limit net" "(f \<longlongrightarrow> l) net"
+  shows "l \<in> S"
+proof (rule ccontr)
+  assume "l \<notin> S"
+  with \<open>closed S\<close> have "open (- S)" "l \<in> - S"
+    by (simp_all add: open_Compl)
+  with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
+    by (rule topological_tendstoD)
+  with assms(2) have "eventually (\<lambda>x. False) net"
+    by (rule eventually_elim2) simp
+  with assms(3) show "False"
+    by (simp add: eventually_False)
+qed
+
+text\<open>Need to prove closed(cball(x,e)) before deducing this as a corollary.\<close>
+
+lemma Lim_dist_ubound:
+  assumes "\<not>(trivial_limit net)"
+    and "(f \<longlongrightarrow> l) net"
+    and "eventually (\<lambda>x. dist a (f x) \<le> e) net"
+  shows "dist a l \<le> e"
+  using assms by (fast intro: tendsto_le tendsto_intros)
+
+lemma Lim_norm_ubound:
+  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
+  assumes "\<not>(trivial_limit net)" "(f \<longlongrightarrow> l) net" "eventually (\<lambda>x. norm(f x) \<le> e) net"
+  shows "norm(l) \<le> e"
+  using assms by (fast intro: tendsto_le tendsto_intros)
+
+lemma Lim_norm_lbound:
+  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
+  assumes "\<not> trivial_limit net"
+    and "(f \<longlongrightarrow> l) net"
+    and "eventually (\<lambda>x. e \<le> norm (f x)) net"
+  shows "e \<le> norm l"
+  using assms by (fast intro: tendsto_le tendsto_intros)
+
+text\<open>Limit under bilinear function\<close>
+
+lemma Lim_bilinear:
+  assumes "(f \<longlongrightarrow> l) net"
+    and "(g \<longlongrightarrow> m) net"
+    and "bounded_bilinear h"
+  shows "((\<lambda>x. h (f x) (g x)) \<longlongrightarrow> (h l m)) net"
+  using \<open>bounded_bilinear h\<close> \<open>(f \<longlongrightarrow> l) net\<close> \<open>(g \<longlongrightarrow> m) net\<close>
+  by (rule bounded_bilinear.tendsto)
+
+text\<open>These are special for limits out of the same vector space.\<close>
+
+lemma Lim_within_id: "(id \<longlongrightarrow> a) (at a within s)"
+  unfolding id_def by (rule tendsto_ident_at)
+
+lemma Lim_at_id: "(id \<longlongrightarrow> a) (at a)"
+  unfolding id_def by (rule tendsto_ident_at)
+
+lemma Lim_at_zero:
+  fixes a :: "'a::real_normed_vector"
+    and l :: "'b::topological_space"
+  shows "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) \<longlongrightarrow> l) (at 0)"
+  using LIM_offset_zero LIM_offset_zero_cancel ..
+
+text\<open>It's also sometimes useful to extract the limit point from the filter.\<close>
+
+abbreviation netlimit :: "'a::t2_space filter \<Rightarrow> 'a"
+  where "netlimit F \<equiv> Lim F (\<lambda>x. x)"
+
+lemma netlimit_within: "\<not> trivial_limit (at a within S) \<Longrightarrow> netlimit (at a within S) = a"
+  by (rule tendsto_Lim) (auto intro: tendsto_intros)
+
+lemma netlimit_at [simp]:
+  fixes a :: "'a::{perfect_space,t2_space}"
+  shows "netlimit (at a) = a"
+  using netlimit_within [of a UNIV] by simp
+
+lemma lim_within_interior:
+  "x \<in> interior S \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S) \<longleftrightarrow> (f \<longlongrightarrow> l) (at x)"
+  by (metis at_within_interior)
+
+lemma netlimit_within_interior:
+  fixes x :: "'a::{t2_space,perfect_space}"
+  assumes "x \<in> interior S"
+  shows "netlimit (at x within S) = x"
+  using assms by (metis at_within_interior netlimit_at)
+
+lemma netlimit_at_vector:
+  fixes a :: "'a::real_normed_vector"
+  shows "netlimit (at a) = a"
+proof (cases "\<exists>x. x \<noteq> a")
+  case True then obtain x where x: "x \<noteq> a" ..
+  have "\<not> trivial_limit (at a)"
+    unfolding trivial_limit_def eventually_at dist_norm
+    apply clarsimp
+    apply (rule_tac x="a + scaleR (d / 2) (sgn (x - a))" in exI)
+    apply (simp add: norm_sgn sgn_zero_iff x)
+    done
+  then show ?thesis
+    by (rule netlimit_within [of a UNIV])
+qed simp
+
+
+text\<open>Useful lemmas on closure and set of possible sequential limits.\<close>
+
+lemma closure_sequential:
+  fixes l :: "'a::first_countable_topology"
+  shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially)"
+  (is "?lhs = ?rhs")
+proof
+  assume "?lhs"
+  moreover
+  {
+    assume "l \<in> S"
+    then have "?rhs" using tendsto_const[of l sequentially] by auto
+  }
+  moreover
+  {
+    assume "l islimpt S"
+    then have "?rhs" unfolding islimpt_sequential by auto
+  }
+  ultimately show "?rhs"
+    unfolding closure_def by auto
+next
+  assume "?rhs"
+  then show "?lhs" unfolding closure_def islimpt_sequential by auto
+qed
+
+lemma closed_sequential_limits:
+  fixes S :: "'a::first_countable_topology set"
+  shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially \<longrightarrow> l \<in> S)"
+by (metis closure_sequential closure_subset_eq subset_iff)
+
+lemma closure_approachable:
+  fixes S :: "'a::metric_space set"
+  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
+  apply (auto simp: closure_def islimpt_approachable)
+  apply (metis dist_self)
+  done
+
+lemma closure_approachable_le:
+  fixes S :: "'a::metric_space set"
+  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x \<le> e)"
+  unfolding closure_approachable
+  using dense by force
+
+lemma closure_approachableD:
+  assumes "x \<in> closure S" "e>0"
+  shows "\<exists>y\<in>S. dist x y < e"
+  using assms unfolding closure_approachable by (auto simp: dist_commute)
+
+lemma closed_approachable:
+  fixes S :: "'a::metric_space set"
+  shows "closed S \<Longrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
+  by (metis closure_closed closure_approachable)
+
+lemma closure_contains_Inf:
+  fixes S :: "real set"
+  assumes "S \<noteq> {}" "bdd_below S"
+  shows "Inf S \<in> closure S"
+proof -
+  have *: "\<forall>x\<in>S. Inf S \<le> x"
+    using cInf_lower[of _ S] assms by metis
+  {
+    fix e :: real
+    assume "e > 0"
+    then have "Inf S < Inf S + e" by simp
+    with assms obtain x where "x \<in> S" "x < Inf S + e"
+      by (subst (asm) cInf_less_iff) auto
+    with * have "\<exists>x\<in>S. dist x (Inf S) < e"
+      by (intro bexI[of _ x]) (auto simp: dist_real_def)
+  }
+  then show ?thesis unfolding closure_approachable by auto
+qed
+
+lemma closure_Int_ballI:
+  fixes S :: "'a :: metric_space set"
+  assumes "\<And>U. \<lbrakk>openin (subtopology euclidean S) U; U \<noteq> {}\<rbrakk> \<Longrightarrow> T \<inter> U \<noteq> {}"
+ shows "S \<subseteq> closure T"
+proof (clarsimp simp: closure_approachable dist_commute)
+  fix x and e::real
+  assume "x \<in> S" "0 < e"
+  with assms [of "S \<inter> ball x e"] show "\<exists>y\<in>T. dist x y < e"
+    by force
+qed
+
+lemma closed_contains_Inf:
+  fixes S :: "real set"
+  shows "S \<noteq> {} \<Longrightarrow> bdd_below S \<Longrightarrow> closed S \<Longrightarrow> Inf S \<in> S"
+  by (metis closure_contains_Inf closure_closed)
+
+lemma closed_subset_contains_Inf:
+  fixes A C :: "real set"
+  shows "closed C \<Longrightarrow> A \<subseteq> C \<Longrightarrow> A \<noteq> {} \<Longrightarrow> bdd_below A \<Longrightarrow> Inf A \<in> C"
+  by (metis closure_contains_Inf closure_minimal subset_eq)
+
+lemma atLeastAtMost_subset_contains_Inf:
+  fixes A :: "real set" and a b :: real
+  shows "A \<noteq> {} \<Longrightarrow> a \<le> b \<Longrightarrow> A \<subseteq> {a..b} \<Longrightarrow> Inf A \<in> {a..b}"
+  by (rule closed_subset_contains_Inf)
+     (auto intro: closed_real_atLeastAtMost intro!: bdd_belowI[of A a])
+
+lemma not_trivial_limit_within_ball:
+  "\<not> trivial_limit (at x within S) \<longleftrightarrow> (\<forall>e>0. S \<inter> ball x e - {x} \<noteq> {})"
+  (is "?lhs \<longleftrightarrow> ?rhs")
+proof
+  show ?rhs if ?lhs
+  proof -
+    {
+      fix e :: real
+      assume "e > 0"
+      then obtain y where "y \<in> S - {x}" and "dist y x < e"
+        using \<open>?lhs\<close> not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
+        by auto
+      then have "y \<in> S \<inter> ball x e - {x}"
+        unfolding ball_def by (simp add: dist_commute)
+      then have "S \<inter> ball x e - {x} \<noteq> {}" by blast
+    }
+    then show ?thesis by auto
+  qed
+  show ?lhs if ?rhs
+  proof -
+    {
+      fix e :: real
+      assume "e > 0"
+      then obtain y where "y \<in> S \<inter> ball x e - {x}"
+        using \<open>?rhs\<close> by blast
+      then have "y \<in> S - {x}" and "dist y x < e"
+        unfolding ball_def by (simp_all add: dist_commute)
+      then have "\<exists>y \<in> S - {x}. dist y x < e"
+        by auto
+    }
+    then show ?thesis
+      using not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
+      by auto
+  qed
+qed
+
+lemma tendsto_If_within_closures:
+  assumes f: "x \<in> s \<union> (closure s \<inter> closure t) \<Longrightarrow>
+      (f \<longlongrightarrow> l x) (at x within s \<union> (closure s \<inter> closure t))"
+  assumes g: "x \<in> t \<union> (closure s \<inter> closure t) \<Longrightarrow>
+      (g \<longlongrightarrow> l x) (at x within t \<union> (closure s \<inter> closure t))"
+  assumes "x \<in> s \<union> t"
+  shows "((\<lambda>x. if x \<in> s then f x else g x) \<longlongrightarrow> l x) (at x within s \<union> t)"
+proof -
+  have *: "(s \<union> t) \<inter> {x. x \<in> s} = s" "(s \<union> t) \<inter> {x. x \<notin> s} = t - s"
+    by auto
+  have "(f \<longlongrightarrow> l x) (at x within s)"
+    by (rule filterlim_at_within_closure_implies_filterlim)
+       (use \<open>x \<in> _\<close> in \<open>auto simp: inf_commute closure_def intro: tendsto_within_subset[OF f]\<close>)
+  moreover
+  have "(g \<longlongrightarrow> l x) (at x within t - s)"
+    by (rule filterlim_at_within_closure_implies_filterlim)
+      (use \<open>x \<in> _\<close> in
+        \<open>auto intro!: tendsto_within_subset[OF g] simp: closure_def intro: islimpt_subset\<close>)
+  ultimately show ?thesis
+    by (intro filterlim_at_within_If) (simp_all only: *)
+qed
+
+
+subsection \<open>Boundedness\<close>
+
+  (* FIXME: This has to be unified with BSEQ!! *)
+definition%important (in metric_space) bounded :: "'a set \<Rightarrow> bool"
+  where "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
+
+lemma bounded_subset_cball: "bounded S \<longleftrightarrow> (\<exists>e x. S \<subseteq> cball x e \<and> 0 \<le> e)"
+  unfolding bounded_def subset_eq  by auto (meson order_trans zero_le_dist)
+
+lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
+  unfolding bounded_def
+  by auto (metis add.commute add_le_cancel_right dist_commute dist_triangle_le)
+
+lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
+  unfolding bounded_any_center [where a=0]
+  by (simp add: dist_norm)
+
+lemma bdd_above_norm: "bdd_above (norm ` X) \<longleftrightarrow> bounded X"
+  by (simp add: bounded_iff bdd_above_def)
+
+lemma bounded_norm_comp: "bounded ((\<lambda>x. norm (f x)) ` S) = bounded (f ` S)"
+  by (simp add: bounded_iff)
+
+lemma boundedI:
+  assumes "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> B"
+  shows "bounded S"
+  using assms bounded_iff by blast
+
+lemma bounded_empty [simp]: "bounded {}"
+  by (simp add: bounded_def)
+
+lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> bounded S"
+  by (metis bounded_def subset_eq)
+
+lemma bounded_interior[intro]: "bounded S \<Longrightarrow> bounded(interior S)"
+  by (metis bounded_subset interior_subset)
+
+lemma bounded_closure[intro]:
+  assumes "bounded S"
+  shows "bounded (closure S)"
+proof -
+  from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a"
+    unfolding bounded_def by auto
+  {
+    fix y
+    assume "y \<in> closure S"
+    then obtain f where f: "\<forall>n. f n \<in> S"  "(f \<longlongrightarrow> y) sequentially"
+      unfolding closure_sequential by auto
+    have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
+    then have "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
+      by (simp add: f(1))
+    have "dist x y \<le> a"
+      apply (rule Lim_dist_ubound [of sequentially f])
+      apply (rule trivial_limit_sequentially)
+      apply (rule f(2))
+      apply fact
+      done
+  }
+  then show ?thesis
+    unfolding bounded_def by auto
+qed
+
+lemma bounded_closure_image: "bounded (f ` closure S) \<Longrightarrow> bounded (f ` S)"
+  by (simp add: bounded_subset closure_subset image_mono)
+
+lemma bounded_cball[simp,intro]: "bounded (cball x e)"
+  apply (simp add: bounded_def)
+  apply (rule_tac x=x in exI)
+  apply (rule_tac x=e in exI, auto)
+  done
+
+lemma bounded_ball[simp,intro]: "bounded (ball x e)"
+  by (metis ball_subset_cball bounded_cball bounded_subset)
+
+lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
+  by (auto simp: bounded_def) (metis Un_iff bounded_any_center le_max_iff_disj)
+
+lemma bounded_Union[intro]: "finite F \<Longrightarrow> \<forall>S\<in>F. bounded S \<Longrightarrow> bounded (\<Union>F)"
+  by (induct rule: finite_induct[of F]) auto
+
+lemma bounded_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. bounded (B x) \<Longrightarrow> bounded (\<Union>x\<in>A. B x)"
+  by (induct set: finite) auto
+
+lemma bounded_insert [simp]: "bounded (insert x S) \<longleftrightarrow> bounded S"
+proof -
+  have "\<forall>y\<in>{x}. dist x y \<le> 0"
+    by simp
+  then have "bounded {x}"
+    unfolding bounded_def by fast
+  then show ?thesis
+    by (metis insert_is_Un bounded_Un)
+qed
+
+lemma bounded_subset_ballI: "S \<subseteq> ball x r \<Longrightarrow> bounded S"
+  by (meson bounded_ball bounded_subset)
+
+lemma bounded_subset_ballD:
+  assumes "bounded S" shows "\<exists>r. 0 < r \<and> S \<subseteq> ball x r"
+proof -
+  obtain e::real and y where "S \<subseteq> cball y e"  "0 \<le> e"
+    using assms by (auto simp: bounded_subset_cball)
+  then show ?thesis
+    apply (rule_tac x="dist x y + e + 1" in exI)
+    apply (simp add: add.commute add_pos_nonneg)
+    apply (erule subset_trans)
+    apply (clarsimp simp add: cball_def)
+    by (metis add_le_cancel_right add_strict_increasing dist_commute dist_triangle_le zero_less_one)
+qed
+
+lemma finite_imp_bounded [intro]: "finite S \<Longrightarrow> bounded S"
+  by (induct set: finite) simp_all
+
+lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x \<le> b)"
+  apply (simp add: bounded_iff)
+  apply (subgoal_tac "\<And>x (y::real). 0 < 1 + \<bar>y\<bar> \<and> (x \<le> y \<longrightarrow> x \<le> 1 + \<bar>y\<bar>)")
+  apply metis
+  apply arith
+  done
+
+lemma bounded_pos_less: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x < b)"
+  apply (simp add: bounded_pos)
+  apply (safe; rule_tac x="b+1" in exI; force)
+  done
+
+lemma Bseq_eq_bounded:
+  fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
+  shows "Bseq f \<longleftrightarrow> bounded (range f)"
+  unfolding Bseq_def bounded_pos by auto
+
+lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
+  by (metis Int_lower1 Int_lower2 bounded_subset)
+
+lemma bounded_diff[intro]: "bounded S \<Longrightarrow> bounded (S - T)"
+  by (metis Diff_subset bounded_subset)
+
+lemma not_bounded_UNIV[simp]:
+  "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
+proof (auto simp: bounded_pos not_le)
+  obtain x :: 'a where "x \<noteq> 0"
+    using perfect_choose_dist [OF zero_less_one] by fast
+  fix b :: real
+  assume b: "b >0"
+  have b1: "b +1 \<ge> 0"
+    using b by simp
+  with \<open>x \<noteq> 0\<close> have "b < norm (scaleR (b + 1) (sgn x))"
+    by (simp add: norm_sgn)
+  then show "\<exists>x::'a. b < norm x" ..
+qed
+
+corollary cobounded_imp_unbounded:
+    fixes S :: "'a::{real_normed_vector, perfect_space} set"
+    shows "bounded (- S) \<Longrightarrow> \<not> bounded S"
+  using bounded_Un [of S "-S"]  by (simp add: sup_compl_top)
+
+lemma bounded_dist_comp:
+  assumes "bounded (f ` S)" "bounded (g ` S)"
+  shows "bounded ((\<lambda>x. dist (f x) (g x)) ` S)"
+proof -
+  from assms obtain M1 M2 where *: "dist (f x) undefined \<le> M1" "dist undefined (g x) \<le> M2" if "x \<in> S" for x
+    by (auto simp: bounded_any_center[of _ undefined] dist_commute)
+  have "dist (f x) (g x) \<le> M1 + M2" if "x \<in> S" for x
+    using *[OF that]
+    by (rule order_trans[OF dist_triangle add_mono])
+  then show ?thesis
+    by (auto intro!: boundedI)
+qed
+
+lemma bounded_linear_image:
+  assumes "bounded S"
+    and "bounded_linear f"
+  shows "bounded (f ` S)"
+proof -
+  from assms(1) obtain b where "b > 0" and b: "\<forall>x\<in>S. norm x \<le> b"
+    unfolding bounded_pos by auto
+  from assms(2) obtain B where B: "B > 0" "\<forall>x. norm (f x) \<le> B * norm x"
+    using bounded_linear.pos_bounded by (auto simp: ac_simps)
+  show ?thesis
+    unfolding bounded_pos
+  proof (intro exI, safe)
+    show "norm (f x) \<le> B * b" if "x \<in> S" for x
+      by (meson B b less_imp_le mult_left_mono order_trans that)
+  qed (use \<open>b > 0\<close> \<open>B > 0\<close> in auto)
+qed
+
+lemma bounded_scaling:
+  fixes S :: "'a::real_normed_vector set"
+  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
+  apply (rule bounded_linear_image, assumption)
+  apply (rule bounded_linear_scaleR_right)
+  done
+
+lemma bounded_scaleR_comp:
+  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
+  assumes "bounded (f ` S)"
+  shows "bounded ((\<lambda>x. r *\<^sub>R f x) ` S)"
+  using bounded_scaling[of "f ` S" r] assms
+  by (auto simp: image_image)
+
+lemma bounded_translation:
+  fixes S :: "'a::real_normed_vector set"
+  assumes "bounded S"
+  shows "bounded ((\<lambda>x. a + x) ` S)"
+proof -
+  from assms obtain b where b: "b > 0" "\<forall>x\<in>S. norm x \<le> b"
+    unfolding bounded_pos by auto
+  {
+    fix x
+    assume "x \<in> S"
+    then have "norm (a + x) \<le> b + norm a"
+      using norm_triangle_ineq[of a x] b by auto
+  }
+  then show ?thesis
+    unfolding bounded_pos
+    using norm_ge_zero[of a] b(1) and add_strict_increasing[of b 0 "norm a"]
+    by (auto intro!: exI[of _ "b + norm a"])
+qed
+
+lemma bounded_translation_minus:
+  fixes S :: "'a::real_normed_vector set"
+  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. x - a) ` S)"
+using bounded_translation [of S "-a"] by simp
+
+lemma bounded_uminus [simp]:
+  fixes X :: "'a::real_normed_vector set"
+  shows "bounded (uminus ` X) \<longleftrightarrow> bounded X"
+by (auto simp: bounded_def dist_norm; rule_tac x="-x" in exI; force simp: add.commute norm_minus_commute)
+
+lemma uminus_bounded_comp [simp]:
+  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
+  shows "bounded ((\<lambda>x. - f x) ` S) \<longleftrightarrow> bounded (f ` S)"
+  using bounded_uminus[of "f ` S"]
+  by (auto simp: image_image)
+
+lemma bounded_plus_comp:
+  fixes f g::"'a \<Rightarrow> 'b::real_normed_vector"
+  assumes "bounded (f ` S)"
+  assumes "bounded (g ` S)"
+  shows "bounded ((\<lambda>x. f x + g x) ` S)"
+proof -
+  {
+    fix B C
+    assume "\<And>x. x\<in>S \<Longrightarrow> norm (f x) \<le> B" "\<And>x. x\<in>S \<Longrightarrow> norm (g x) \<le> C"
+    then have "\<And>x. x \<in> S \<Longrightarrow> norm (f x + g x) \<le> B + C"
+      by (auto intro!: norm_triangle_le add_mono)
+  } then show ?thesis
+    using assms by (fastforce simp: bounded_iff)
+qed
+
+lemma bounded_plus:
+  fixes S ::"'a::real_normed_vector set"
+  assumes "bounded S" "bounded T"
+  shows "bounded ((\<lambda>(x,y). x + y) ` (S \<times> T))"
+  using bounded_plus_comp [of fst "S \<times> T" snd] assms
+  by (auto simp: split_def split: if_split_asm)
+
+lemma bounded_minus_comp:
+  "bounded (f ` S) \<Longrightarrow> bounded (g ` S) \<Longrightarrow> bounded ((\<lambda>x. f x - g x) ` S)"
+  for f g::"'a \<Rightarrow> 'b::real_normed_vector"
+  using bounded_plus_comp[of "f" S "\<lambda>x. - g x"]
+  by auto
+
+lemma bounded_minus:
+  fixes S ::"'a::real_normed_vector set"
+  assumes "bounded S" "bounded T"
+  shows "bounded ((\<lambda>(x,y). x - y) ` (S \<times> T))"
+  using bounded_minus_comp [of fst "S \<times> T" snd] assms
+  by (auto simp: split_def split: if_split_asm)
+
+
+subsection \<open>Compactness\<close>
+
+subsubsection \<open>Bolzano-Weierstrass property\<close>
+
+proposition heine_borel_imp_bolzano_weierstrass:
+  assumes "compact s"
+    and "infinite t"
+    and "t \<subseteq> s"
+  shows "\<exists>x \<in> s. x islimpt t"
+proof (rule ccontr)
+  assume "\<not> (\<exists>x \<in> s. x islimpt t)"
+  then obtain f where f: "\<forall>x\<in>s. x \<in> f x \<and> open (f x) \<and> (\<forall>y\<in>t. y \<in> f x \<longrightarrow> y = x)"
+    unfolding islimpt_def
+    using bchoice[of s "\<lambda> x T. x \<in> T \<and> open T \<and> (\<forall>y\<in>t. y \<in> T \<longrightarrow> y = x)"]
+    by auto
+  obtain g where g: "g \<subseteq> {t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
+    using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]]
+    using f by auto
+  from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa"
+    by auto
+  {
+    fix x y
+    assume "x \<in> t" "y \<in> t" "f x = f y"
+    then have "x \<in> f x"  "y \<in> f x \<longrightarrow> y = x"
+      using f[THEN bspec[where x=x]] and \<open>t \<subseteq> s\<close> by auto
+    then have "x = y"
+      using \<open>f x = f y\<close> and f[THEN bspec[where x=y]] and \<open>y \<in> t\<close> and \<open>t \<subseteq> s\<close>
+      by auto
+  }
+  then have "inj_on f t"
+    unfolding inj_on_def by simp
+  then have "infinite (f ` t)"
+    using assms(2) using finite_imageD by auto
+  moreover
+  {
+    fix x
+    assume "x \<in> t" "f x \<notin> g"
+    from g(3) assms(3) \<open>x \<in> t\<close> obtain h where "h \<in> g" and "x \<in> h"
+      by auto
+    then obtain y where "y \<in> s" "h = f y"
+      using g'[THEN bspec[where x=h]] by auto
+    then have "y = x"
+      using f[THEN bspec[where x=y]] and \<open>x\<in>t\<close> and \<open>x\<in>h\<close>[unfolded \<open>h = f y\<close>]
+      by auto
+    then have False
+      using \<open>f x \<notin> g\<close> \<open>h \<in> g\<close> unfolding \<open>h = f y\<close>
+      by auto
+  }
+  then have "f ` t \<subseteq> g" by auto
+  ultimately show False
+    using g(2) using finite_subset by auto
+qed
+
+lemma acc_point_range_imp_convergent_subsequence:
+  fixes l :: "'a :: first_countable_topology"
+  assumes l: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> range f)"
+  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
+proof -
+  from countable_basis_at_decseq[of l]
+  obtain A where A:
+      "\<And>i. open (A i)"
+      "\<And>i. l \<in> A i"
+      "\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
+    by blast
+  define s where "s n i = (SOME j. i < j \<and> f j \<in> A (Suc n))" for n i
+  {
+    fix n i
+    have "infinite (A (Suc n) \<inter> range f - f`{.. i})"
+      using l A by auto
+    then have "\<exists>x. x \<in> A (Suc n) \<inter> range f - f`{.. i}"
+      unfolding ex_in_conv by (intro notI) simp
+    then have "\<exists>j. f j \<in> A (Suc n) \<and> j \<notin> {.. i}"
+      by auto
+    then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)"
+      by (auto simp: not_le)
+    then have "i < s n i" "f (s n i) \<in> A (Suc n)"
+      unfolding s_def by (auto intro: someI2_ex)
+  }
+  note s = this
+  define r where "r = rec_nat (s 0 0) s"
+  have "strict_mono r"
+    by (auto simp: r_def s strict_mono_Suc_iff)
+  moreover
+  have "(\<lambda>n. f (r n)) \<longlonglongrightarrow> l"
+  proof (rule topological_tendstoI)
+    fix S
+    assume "open S" "l \<in> S"
+    with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
+      by auto
+    moreover
+    {
+      fix i
+      assume "Suc 0 \<le> i"
+      then have "f (r i) \<in> A i"
+        by (cases i) (simp_all add: r_def s)
+    }
+    then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially"
+      by (auto simp: eventually_sequentially)
+    ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"
+      by eventually_elim auto
+  qed
+  ultimately show "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
+    by (auto simp: convergent_def comp_def)
+qed
+
+lemma sequence_infinite_lemma:
+  fixes f :: "nat \<Rightarrow> 'a::t1_space"
+  assumes "\<forall>n. f n \<noteq> l"
+    and "(f \<longlongrightarrow> l) sequentially"
+  shows "infinite (range f)"
+proof
+  assume "finite (range f)"
+  then have "closed (range f)"
+    by (rule finite_imp_closed)
+  then have "open (- range f)"
+    by (rule open_Compl)
+  from assms(1) have "l \<in> - range f"
+    by auto
+  from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
+    using \<open>open (- range f)\<close> \<open>l \<in> - range f\<close>
+    by (rule topological_tendstoD)
+  then show False
+    unfolding eventually_sequentially
+    by auto
+qed
+
+lemma closure_insert:
+  fixes x :: "'a::t1_space"
+  shows "closure (insert x s) = insert x (closure s)"
+  apply (rule closure_unique)
+  apply (rule insert_mono [OF closure_subset])
+  apply (rule closed_insert [OF closed_closure])
+  apply (simp add: closure_minimal)
+  done
+
+lemma islimpt_insert:
+  fixes x :: "'a::t1_space"
+  shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
+proof
+  assume *: "x islimpt (insert a s)"
+  show "x islimpt s"
+  proof (rule islimptI)
+    fix t
+    assume t: "x \<in> t" "open t"
+    show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
+    proof (cases "x = a")
+      case True
+      obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
+        using * t by (rule islimptE)
+      with \<open>x = a\<close> show ?thesis by auto
+    next
+      case False
+      with t have t': "x \<in> t - {a}" "open (t - {a})"
+        by (simp_all add: open_Diff)
+      obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
+        using * t' by (rule islimptE)
+      then show ?thesis by auto
+    qed
+  qed
+next
+  assume "x islimpt s"
+  then show "x islimpt (insert a s)"
+    by (rule islimpt_subset) auto
+qed
+
+lemma islimpt_finite:
+  fixes x :: "'a::t1_space"
+  shows "finite s \<Longrightarrow> \<not> x islimpt s"
+  by (induct set: finite) (simp_all add: islimpt_insert)
+
+lemma islimpt_Un_finite:
+  fixes x :: "'a::t1_space"
+  shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
+  by (simp add: islimpt_Un islimpt_finite)
+
+lemma islimpt_eq_acc_point:
+  fixes l :: "'a :: t1_space"
+  shows "l islimpt S \<longleftrightarrow> (\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S))"
+proof (safe intro!: islimptI)
+  fix U
+  assume "l islimpt S" "l \<in> U" "open U" "finite (U \<inter> S)"
+  then have "l islimpt S" "l \<in> (U - (U \<inter> S - {l}))" "open (U - (U \<inter> S - {l}))"
+    by (auto intro: finite_imp_closed)
+  then show False
+    by (rule islimptE) auto
+next
+  fix T
+  assume *: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S)" "l \<in> T" "open T"
+  then have "infinite (T \<inter> S - {l})"
+    by auto
+  then have "\<exists>x. x \<in> (T \<inter> S - {l})"
+    unfolding ex_in_conv by (intro notI) simp
+  then show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> l"
+    by auto
+qed
+
+corollary infinite_openin:
+  fixes S :: "'a :: t1_space set"
+  shows "\<lbrakk>openin (subtopology euclidean U) S; x \<in> S; x islimpt U\<rbrakk> \<Longrightarrow> infinite S"
+  by (clarsimp simp add: openin_open islimpt_eq_acc_point inf_commute)
+
+lemma islimpt_range_imp_convergent_subsequence:
+  fixes l :: "'a :: {t1_space, first_countable_topology}"
+  assumes l: "l islimpt (range f)"
+  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
+  using l unfolding islimpt_eq_acc_point
+  by (rule acc_point_range_imp_convergent_subsequence)
+
+lemma islimpt_eq_infinite_ball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> ball x e))"
+  apply (simp add: islimpt_eq_acc_point, safe)
+   apply (metis Int_commute open_ball centre_in_ball)
+  by (metis open_contains_ball Int_mono finite_subset inf_commute subset_refl)
+
+lemma islimpt_eq_infinite_cball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> cball x e))"
+  apply (simp add: islimpt_eq_infinite_ball, safe)
+   apply (meson Int_mono ball_subset_cball finite_subset order_refl)
+  by (metis open_ball centre_in_ball finite_Int inf.absorb_iff2 inf_assoc open_contains_cball_eq)
+
+lemma sequence_unique_limpt:
+  fixes f :: "nat \<Rightarrow> 'a::t2_space"
+  assumes "(f \<longlongrightarrow> l) sequentially"
+    and "l' islimpt (range f)"
+  shows "l' = l"
+proof (rule ccontr)
+  assume "l' \<noteq> l"
+  obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
+    using hausdorff [OF \<open>l' \<noteq> l\<close>] by auto
+  have "eventually (\<lambda>n. f n \<in> t) sequentially"
+    using assms(1) \<open>open t\<close> \<open>l \<in> t\<close> by (rule topological_tendstoD)
+  then obtain N where "\<forall>n\<ge>N. f n \<in> t"
+    unfolding eventually_sequentially by auto
+
+  have "UNIV = {..<N} \<union> {N..}"
+    by auto
+  then have "l' islimpt (f ` ({..<N} \<union> {N..}))"
+    using assms(2) by simp
+  then have "l' islimpt (f ` {..<N} \<union> f ` {N..})"
+    by (simp add: image_Un)
+  then have "l' islimpt (f ` {N..})"
+    by (simp add: islimpt_Un_finite)
+  then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
+    using \<open>l' \<in> s\<close> \<open>open s\<close> by (rule islimptE)
+  then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'"
+    by auto
+  with \<open>\<forall>n\<ge>N. f n \<in> t\<close> have "f n \<in> s \<inter> t"
+    by simp
+  with \<open>s \<inter> t = {}\<close> show False
+    by simp
+qed
+
+lemma bolzano_weierstrass_imp_closed:
+  fixes s :: "'a::{first_countable_topology,t2_space} set"
+  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
+  shows "closed s"
+proof -
+  {
+    fix x l
+    assume as: "\<forall>n::nat. x n \<in> s" "(x \<longlongrightarrow> l) sequentially"
+    then have "l \<in> s"
+    proof (cases "\<forall>n. x n \<noteq> l")
+      case False
+      then show "l\<in>s" using as(1) by auto
+    next
+      case True note cas = this
+      with as(2) have "infinite (range x)"
+        using sequence_infinite_lemma[of x l] by auto
+      then obtain l' where "l'\<in>s" "l' islimpt (range x)"
+        using assms[THEN spec[where x="range x"]] as(1) by auto
+      then show "l\<in>s" using sequence_unique_limpt[of x l l']
+        using as cas by auto
+    qed
+  }
+  then show ?thesis
+    unfolding closed_sequential_limits by fast
+qed
+
+lemma compact_imp_bounded:
+  assumes "compact U"
+  shows "bounded U"
+proof -
+  have "compact U" "\<forall>x\<in>U. open (ball x 1)" "U \<subseteq> (\<Union>x\<in>U. ball x 1)"
+    using assms by auto
+  then obtain D where D: "D \<subseteq> U" "finite D" "U \<subseteq> (\<Union>x\<in>D. ball x 1)"
+    by (metis compactE_image)
+  from \<open>finite D\<close> have "bounded (\<Union>x\<in>D. ball x 1)"
+    by (simp add: bounded_UN)
+  then show "bounded U" using \<open>U \<subseteq> (\<Union>x\<in>D. ball x 1)\<close>
+    by (rule bounded_subset)
+qed
+
+text\<open>In particular, some common special cases.\<close>
+
+lemma compact_Un [intro]:
+  assumes "compact s"
+    and "compact t"
+  shows " compact (s \<union> t)"
+proof (rule compactI)
+  fix f
+  assume *: "Ball f open" "s \<union> t \<subseteq> \<Union>f"
+  from * \<open>compact s\<close> obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"
+    unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
+  moreover
+  from * \<open>compact t\<close> obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"
+    unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
+  ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"
+    by (auto intro!: exI[of _ "s' \<union> t'"])
+qed
+
+lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"
+  by (induct set: finite) auto
+
+lemma compact_UN [intro]:
+  "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"
+  by (rule compact_Union) auto
+
+lemma closed_Int_compact [intro]:
+  assumes "closed s"
+    and "compact t"
+  shows "compact (s \<inter> t)"
+  using compact_Int_closed [of t s] assms
+  by (simp add: Int_commute)
+
+lemma compact_Int [intro]:
+  fixes s t :: "'a :: t2_space set"
+  assumes "compact s"
+    and "compact t"
+  shows "compact (s \<inter> t)"
+  using assms by (intro compact_Int_closed compact_imp_closed)
+
+lemma compact_sing [simp]: "compact {a}"
+  unfolding compact_eq_heine_borel by auto
+
+lemma compact_insert [simp]:
+  assumes "compact s"
+  shows "compact (insert x s)"
+proof -
+  have "compact ({x} \<union> s)"
+    using compact_sing assms by (rule compact_Un)
+  then show ?thesis by simp
+qed
+
+lemma finite_imp_compact: "finite s \<Longrightarrow> compact s"
+  by (induct set: finite) simp_all
+
+lemma open_delete:
+  fixes s :: "'a::t1_space set"
+  shows "open s \<Longrightarrow> open (s - {x})"
+  by (simp add: open_Diff)
+
+lemma openin_delete:
+  fixes a :: "'a :: t1_space"
+  shows "openin (subtopology euclidean u) s
+         \<Longrightarrow> openin (subtopology euclidean u) (s - {a})"
+by (metis Int_Diff open_delete openin_open)
+
+text\<open>Compactness expressed with filters\<close>
+
+lemma closure_iff_nhds_not_empty:
+  "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"
+proof safe
+  assume x: "x \<in> closure X"
+  fix S A
+  assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"
+  then have "x \<notin> closure (-S)"
+    by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)
+  with x have "x \<in> closure X - closure (-S)"
+    by auto
+  also have "\<dots> \<subseteq> closure (X \<inter> S)"
+    using \<open>open S\<close> open_Int_closure_subset[of S X] by (simp add: closed_Compl ac_simps)
+  finally have "X \<inter> S \<noteq> {}" by auto
+  then show False using \<open>X \<inter> A = {}\<close> \<open>S \<subseteq> A\<close> by auto
+next
+  assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"
+  from this[THEN spec, of "- X", THEN spec, of "- closure X"]
+  show "x \<in> closure X"
+    by (simp add: closure_subset open_Compl)
+qed
+
+corollary closure_Int_ball_not_empty:
+  assumes "S \<subseteq> closure T" "x \<in> S" "r > 0"
+  shows "T \<inter> ball x r \<noteq> {}"
+  using assms centre_in_ball closure_iff_nhds_not_empty by blast
+
+lemma compact_filter:
+  "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))"
+proof (intro allI iffI impI compact_fip[THEN iffD2] notI)
+  fix F
+  assume "compact U"
+  assume F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"
+  then have "U \<noteq> {}"
+    by (auto simp: eventually_False)
+
+  define Z where "Z = closure ` {A. eventually (\<lambda>x. x \<in> A) F}"
+  then have "\<forall>z\<in>Z. closed z"
+    by auto
+  moreover
+  have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"
+    unfolding Z_def by (auto elim: eventually_mono intro: set_mp[OF closure_subset])
+  have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"
+  proof (intro allI impI)
+    fix B assume "finite B" "B \<subseteq> Z"
+    with \<open>finite B\<close> ev_Z F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"
+      by (auto simp: eventually_ball_finite_distrib eventually_conj_iff)
+    with F show "U \<inter> \<Inter>B \<noteq> {}"
+      by (intro notI) (simp add: eventually_False)
+  qed
+  ultimately have "U \<inter> \<Inter>Z \<noteq> {}"
+    using \<open>compact U\<close> unfolding compact_fip by blast
+  then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z"
+    by auto
+
+  have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"
+    unfolding eventually_inf eventually_nhds
+  proof safe
+    fix P Q R S
+    assume "eventually R F" "open S" "x \<in> S"
+    with open_Int_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]
+    have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)
+    moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"
+    ultimately show False by (auto simp: set_eq_iff)
+  qed
+  with \<open>x \<in> U\<close> show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"
+    by (metis eventually_bot)
+next
+  fix A
+  assume A: "\<forall>a\<in>A. closed a" "\<forall>B\<subseteq>A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}" "U \<inter> \<Inter>A = {}"
+  define F where "F = (INF a\<in>insert U A. principal a)"
+  have "F \<noteq> bot"
+    unfolding F_def
+  proof (rule INF_filter_not_bot)
+    fix X
+    assume X: "X \<subseteq> insert U A" "finite X"
+    with A(2)[THEN spec, of "X - {U}"] have "U \<inter> \<Inter>(X - {U}) \<noteq> {}"
+      by auto
+    with X show "(INF a\<in>X. principal a) \<noteq> bot"
+      by (auto simp: INF_principal_finite principal_eq_bot_iff)
+  qed
+  moreover
+  have "F \<le> principal U"
+    unfolding F_def by auto
+  then have "eventually (\<lambda>x. x \<in> U) F"
+    by (auto simp: le_filter_def eventually_principal)
+  moreover
+  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)"
+  ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"
+    by auto
+
+  { fix V assume "V \<in> A"
+    then have "F \<le> principal V"
+      unfolding F_def by (intro INF_lower2[of V]) auto
+    then have V: "eventually (\<lambda>x. x \<in> V) F"
+      by (auto simp: le_filter_def eventually_principal)
+    have "x \<in> closure V"
+      unfolding closure_iff_nhds_not_empty
+    proof (intro impI allI)
+      fix S A
+      assume "open S" "x \<in> S" "S \<subseteq> A"
+      then have "eventually (\<lambda>x. x \<in> A) (nhds x)"
+        by (auto simp: eventually_nhds)
+      with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"
+        by (auto simp: eventually_inf)
+      with x show "V \<inter> A \<noteq> {}"
+        by (auto simp del: Int_iff simp add: trivial_limit_def)
+    qed
+    then have "x \<in> V"
+      using \<open>V \<in> A\<close> A(1) by simp
+  }
+  with \<open>x\<in>U\<close> have "x \<in> U \<inter> \<Inter>A" by auto
+  with \<open>U \<inter> \<Inter>A = {}\<close> show False by auto
+qed
+
+definition%important "countably_compact U \<longleftrightarrow>
+    (\<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))"
+
+lemma countably_compactE:
+  assumes "countably_compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C" "countable C"
+  obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
+  using assms unfolding countably_compact_def by metis
+
+lemma countably_compactI:
+  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')"
+  shows "countably_compact s"
+  using assms unfolding countably_compact_def by metis
+
+lemma compact_imp_countably_compact: "compact U \<Longrightarrow> countably_compact U"
+  by (auto simp: compact_eq_heine_borel countably_compact_def)
+
+lemma countably_compact_imp_compact:
+  assumes "countably_compact U"
+    and ccover: "countable B" "\<forall>b\<in>B. open b"
+    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"
+  shows "compact U"
+  using \<open>countably_compact U\<close>
+  unfolding compact_eq_heine_borel countably_compact_def
+proof safe
+  fix A
+  assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
+  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)"
+  moreover define C where "C = {b\<in>B. \<exists>a\<in>A. b \<inter> U \<subseteq> a}"
+  ultimately have "countable C" "\<forall>a\<in>C. open a"
+    unfolding C_def using ccover by auto
+  moreover
+  have "\<Union>A \<inter> U \<subseteq> \<Union>C"
+  proof safe
+    fix x a
+    assume "x \<in> U" "x \<in> a" "a \<in> A"
+    with basis[of a x] A obtain b where "b \<in> B" "x \<in> b" "b \<inter> U \<subseteq> a"
+      by blast
+    with \<open>a \<in> A\<close> show "x \<in> \<Union>C"
+      unfolding C_def by auto
+  qed
+  then have "U \<subseteq> \<Union>C" using \<open>U \<subseteq> \<Union>A\<close> by auto
+  ultimately obtain T where T: "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"
+    using * by metis
+  then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<inter> U \<subseteq> a"
+    by (auto simp: C_def)
+  then obtain f where "\<forall>t\<in>T. f t \<in> A \<and> t \<inter> U \<subseteq> f t"
+    unfolding bchoice_iff Bex_def ..
+  with T show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
+    unfolding C_def by (intro exI[of _ "f`T"]) fastforce
+qed
+
+proposition countably_compact_imp_compact_second_countable:
+  "countably_compact U \<Longrightarrow> compact (U :: 'a :: second_countable_topology set)"
+proof (rule countably_compact_imp_compact)
+  fix T and x :: 'a
+  assume "open T" "x \<in> T"
+  from topological_basisE[OF is_basis this] obtain b where
+    "b \<in> (SOME B. countable B \<and> topological_basis B)" "x \<in> b" "b \<subseteq> T" .
+  then show "\<exists>b\<in>SOME B. countable B \<and> topological_basis B. x \<in> b \<and> b \<inter> U \<subseteq> T"
+    by blast
+qed (insert countable_basis topological_basis_open[OF is_basis], auto)
+
+lemma countably_compact_eq_compact:
+  "countably_compact U \<longleftrightarrow> compact (U :: 'a :: second_countable_topology set)"
+  using countably_compact_imp_compact_second_countable compact_imp_countably_compact by blast
+
+subsubsection\<open>Sequential compactness\<close>
+
+definition%important seq_compact :: "'a::topological_space set \<Rightarrow> bool"
+  where "seq_compact S \<longleftrightarrow>
+    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially))"
+
+lemma seq_compactI:
+  assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
+  shows "seq_compact S"
+  unfolding seq_compact_def using assms by fast
+
+lemma seq_compactE:
+  assumes "seq_compact S" "\<forall>n. f n \<in> S"
+  obtains l r where "l \<in> S" "strict_mono (r :: nat \<Rightarrow> nat)" "((f \<circ> r) \<longlongrightarrow> l) sequentially"
+  using assms unfolding seq_compact_def by fast
+
+lemma closed_sequentially: (* TODO: move upwards *)
+  assumes "closed s" and "\<forall>n. f n \<in> s" and "f \<longlonglongrightarrow> l"
+  shows "l \<in> s"
+proof (rule ccontr)
+  assume "l \<notin> s"
+  with \<open>closed s\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "eventually (\<lambda>n. f n \<in> - s) sequentially"
+    by (fast intro: topological_tendstoD)
+  with \<open>\<forall>n. f n \<in> s\<close> show "False"
+    by simp
+qed
+
+lemma seq_compact_Int_closed:
+  assumes "seq_compact s" and "closed t"
+  shows "seq_compact (s \<inter> t)"
+proof (rule seq_compactI)
+  fix f assume "\<forall>n::nat. f n \<in> s \<inter> t"
+  hence "\<forall>n. f n \<in> s" and "\<forall>n. f n \<in> t"
+    by simp_all
+  from \<open>seq_compact s\<close> and \<open>\<forall>n. f n \<in> s\<close>
+  obtain l r where "l \<in> s" and r: "strict_mono r" and l: "(f \<circ> r) \<longlonglongrightarrow> l"
+    by (rule seq_compactE)
+  from \<open>\<forall>n. f n \<in> t\<close> have "\<forall>n. (f \<circ> r) n \<in> t"
+    by simp
+  from \<open>closed t\<close> and this and l have "l \<in> t"
+    by (rule closed_sequentially)
+  with \<open>l \<in> s\<close> and r and l show "\<exists>l\<in>s \<inter> t. \<exists>r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
+    by fast
+qed
+
+lemma seq_compact_closed_subset:
+  assumes "closed s" and "s \<subseteq> t" and "seq_compact t"
+  shows "seq_compact s"
+  using assms seq_compact_Int_closed [of t s] by (simp add: Int_absorb1)
+
+lemma seq_compact_imp_countably_compact:
+  fixes U :: "'a :: first_countable_topology set"
+  assumes "seq_compact U"
+  shows "countably_compact U"
+proof (safe intro!: countably_compactI)
+  fix A
+  assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"
+  have subseq: "\<And>X. range X \<subseteq> U \<Longrightarrow> \<exists>r x. x \<in> U \<and> strict_mono (r :: nat \<Rightarrow> nat) \<and> (X \<circ> r) \<longlonglongrightarrow> x"
+    using \<open>seq_compact U\<close> by (fastforce simp: seq_compact_def subset_eq)
+  show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
+  proof cases
+    assume "finite A"
+    with A show ?thesis by auto
+  next
+    assume "infinite A"
+    then have "A \<noteq> {}" by auto
+    show ?thesis
+    proof (rule ccontr)
+      assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
+      then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)"
+        by auto
+      then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T"
+        by metis
+      define X where "X n = X' (from_nat_into A ` {.. n})" for n
+      have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"
+        using \<open>A \<noteq> {}\<close> unfolding X_def by (intro T) (auto intro: from_nat_into)
+      then have "range X \<subseteq> U"
+        by auto
+      with subseq[of X] obtain r x where "x \<in> U" and r: "strict_mono r" "(X \<circ> r) \<longlonglongrightarrow> x"
+        by auto
+      from \<open>x\<in>U\<close> \<open>U \<subseteq> \<Union>A\<close> from_nat_into_surj[OF \<open>countable A\<close>]
+      obtain n where "x \<in> from_nat_into A n" by auto
+      with r(2) A(1) from_nat_into[OF \<open>A \<noteq> {}\<close>, of n]
+      have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"
+        unfolding tendsto_def by (auto simp: comp_def)
+      then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"
+        by (auto simp: eventually_sequentially)
+      moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"
+        by auto
+      moreover from \<open>strict_mono r\<close>[THEN seq_suble, of "max n N"] have "\<exists>i. n \<le> r i \<and> N \<le> i"
+        by (auto intro!: exI[of _ "max n N"])
+      ultimately show False
+        by auto
+    qed
+  qed
+qed
+
+lemma compact_imp_seq_compact:
+  fixes U :: "'a :: first_countable_topology set"
+  assumes "compact U"
+  shows "seq_compact U"
+  unfolding seq_compact_def
+proof safe
+  fix X :: "nat \<Rightarrow> 'a"
+  assume "\<forall>n. X n \<in> U"
+  then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"
+    by (auto simp: eventually_filtermap)
+  moreover
+  have "filtermap X sequentially \<noteq> bot"
+    by (simp add: trivial_limit_def eventually_filtermap)
+  ultimately
+  obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")
+    using \<open>compact U\<close> by (auto simp: compact_filter)
+
+  from countable_basis_at_decseq[of x]
+  obtain A where A:
+      "\<And>i. open (A i)"
+      "\<And>i. x \<in> A i"
+      "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
+    by blast
+  define s where "s n i = (SOME j. i < j \<and> X j \<in> A (Suc n))" for n i
+  {
+    fix n i
+    have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"
+    proof (rule ccontr)
+      assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"
+      then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)"
+        by auto
+      then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"
+        by (auto simp: eventually_filtermap eventually_sequentially)
+      moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"
+        using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)
+      ultimately have "eventually (\<lambda>x. False) ?F"
+        by (auto simp: eventually_inf)
+      with x show False
+        by (simp add: eventually_False)
+    qed
+    then have "i < s n i" "X (s n i) \<in> A (Suc n)"
+      unfolding s_def by (auto intro: someI2_ex)
+  }
+  note s = this
+  define r where "r = rec_nat (s 0 0) s"
+  have "strict_mono r"
+    by (auto simp: r_def s strict_mono_Suc_iff)
+  moreover
+  have "(\<lambda>n. X (r n)) \<longlonglongrightarrow> x"
+  proof (rule topological_tendstoI)
+    fix S
+    assume "open S" "x \<in> S"
+    with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
+      by auto
+    moreover
+    {
+      fix i
+      assume "Suc 0 \<le> i"
+      then have "X (r i) \<in> A i"
+        by (cases i) (simp_all add: r_def s)
+    }
+    then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially"
+      by (auto simp: eventually_sequentially)
+    ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"
+      by eventually_elim auto
+  qed
+  ultimately show "\<exists>x \<in> U. \<exists>r. strict_mono r \<and> (X \<circ> r) \<longlonglongrightarrow> x"
+    using \<open>x \<in> U\<close> by (auto simp: convergent_def comp_def)
+qed
+
+lemma countably_compact_imp_acc_point:
+  assumes "countably_compact s"
+    and "countable t"
+    and "infinite t"
+    and "t \<subseteq> s"
+  shows "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)"
+proof (rule ccontr)
+  define C where "C = (\<lambda>F. interior (F \<union> (- t))) ` {F. finite F \<and> F \<subseteq> t }"
+  note \<open>countably_compact s\<close>
+  moreover have "\<forall>t\<in>C. open t"
+    by (auto simp: C_def)
+  moreover
+  assume "\<not> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
+  then have s: "\<And>x. x \<in> s \<Longrightarrow> \<exists>U. x\<in>U \<and> open U \<and> finite (U \<inter> t)" by metis
+  have "s \<subseteq> \<Union>C"
+    using \<open>t \<subseteq> s\<close>
+    unfolding C_def
+    apply (safe dest!: s)
+    apply (rule_tac a="U \<inter> t" in UN_I)
+    apply (auto intro!: interiorI simp add: finite_subset)
+    done
+  moreover
+  from \<open>countable t\<close> have "countable C"
+    unfolding C_def by (auto intro: countable_Collect_finite_subset)
+  ultimately
+  obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> \<Union>D"
+    by (rule countably_compactE)
+  then obtain E where E: "E \<subseteq> {F. finite F \<and> F \<subseteq> t }" "finite E"
+    and s: "s \<subseteq> (\<Union>F\<in>E. interior (F \<union> (- t)))"
+    by (metis (lifting) finite_subset_image C_def)
+  from s \<open>t \<subseteq> s\<close> have "t \<subseteq> \<Union>E"
+    using interior_subset by blast
+  moreover have "finite (\<Union>E)"
+    using E by auto
+  ultimately show False using \<open>infinite t\<close>
+    by (auto simp: finite_subset)
+qed
+
+lemma countable_acc_point_imp_seq_compact:
+  fixes s :: "'a::first_countable_topology set"
+  assumes "\<forall>t. infinite t \<and> countable t \<and> t \<subseteq> s \<longrightarrow>
+    (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
+  shows "seq_compact s"
+proof -
+  {
+    fix f :: "nat \<Rightarrow> 'a"
+    assume f: "\<forall>n. f n \<in> s"
+    have "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
+    proof (cases "finite (range f)")
+      case True
+      obtain l where "infinite {n. f n = f l}"
+        using pigeonhole_infinite[OF _ True] by auto
+      then obtain r :: "nat \<Rightarrow> nat" where "strict_mono  r" and fr: "\<forall>n. f (r n) = f l"
+        using infinite_enumerate by blast
+      then have "strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> f l"
+        by (simp add: fr o_def)
+      with f show "\<exists>l\<in>s. \<exists>r. strict_mono  r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
+        by auto
+    next
+      case False
+      with f assms have "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)"
+        by auto
+      then obtain l where "l \<in> s" "\<forall>U. l\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" ..
+      from this(2) have "\<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
+        using acc_point_range_imp_convergent_subsequence[of l f] by auto
+      with \<open>l \<in> s\<close> show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially" ..
+    qed
+  }
+  then show ?thesis
+    unfolding seq_compact_def by auto
+qed
+
+lemma seq_compact_eq_countably_compact:
+  fixes U :: "'a :: first_countable_topology set"
+  shows "seq_compact U \<longleftrightarrow> countably_compact U"
+  using
+    countable_acc_point_imp_seq_compact
+    countably_compact_imp_acc_point
+    seq_compact_imp_countably_compact
+  by metis
+
+lemma seq_compact_eq_acc_point:
+  fixes s :: "'a :: first_countable_topology set"
+  shows "seq_compact s \<longleftrightarrow>
+    (\<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)))"
+  using
+    countable_acc_point_imp_seq_compact[of s]
+    countably_compact_imp_acc_point[of s]
+    seq_compact_imp_countably_compact[of s]
+  by metis
+
+lemma seq_compact_eq_compact:
+  fixes U :: "'a :: second_countable_topology set"
+  shows "seq_compact U \<longleftrightarrow> compact U"
+  using seq_compact_eq_countably_compact countably_compact_eq_compact by blast
+
+proposition bolzano_weierstrass_imp_seq_compact:
+  fixes s :: "'a::{t1_space, first_countable_topology} set"
+  shows "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> seq_compact s"
+  by (rule countable_acc_point_imp_seq_compact) (metis islimpt_eq_acc_point)
+
+
+subsubsection\<open>Totally bounded\<close>
+
+lemma cauchy_def: "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N \<longrightarrow> dist (s m) (s n) < e)"
+  unfolding Cauchy_def by metis
+
+proposition seq_compact_imp_totally_bounded:
+  assumes "seq_compact s"
+  shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>x\<in>k. ball x e)"
+proof -
+  { fix e::real assume "e > 0" assume *: "\<And>k. finite k \<Longrightarrow> k \<subseteq> s \<Longrightarrow> \<not> s \<subseteq> (\<Union>x\<in>k. ball x e)"
+    let ?Q = "\<lambda>x n r. r \<in> s \<and> (\<forall>m < (n::nat). \<not> (dist (x m) r < e))"
+    have "\<exists>x. \<forall>n::nat. ?Q x n (x n)"
+    proof (rule dependent_wellorder_choice)
+      fix n x assume "\<And>y. y < n \<Longrightarrow> ?Q x y (x y)"
+      then have "\<not> s \<subseteq> (\<Union>x\<in>x ` {0..<n}. ball x e)"
+        using *[of "x ` {0 ..< n}"] by (auto simp: subset_eq)
+      then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)"
+        unfolding subset_eq by auto
+      show "\<exists>r. ?Q x n r"
+        using z by auto
+    qed simp
+    then obtain x where "\<forall>n::nat. x n \<in> s" and x:"\<And>n m. m < n \<Longrightarrow> \<not> (dist (x m) (x n) < e)"
+      by blast
+    then obtain l r where "l \<in> s" and r:"strict_mono  r" and "((x \<circ> r) \<longlongrightarrow> l) sequentially"
+      using assms by (metis seq_compact_def)
+    from this(3) have "Cauchy (x \<circ> r)"
+      using LIMSEQ_imp_Cauchy by auto
+    then obtain N::nat where "\<And>m n. N \<le> m \<Longrightarrow> N \<le> n \<Longrightarrow> dist ((x \<circ> r) m) ((x \<circ> r) n) < e"
+      unfolding cauchy_def using \<open>e > 0\<close> by blast
+    then have False
+      using x[of "r N" "r (N+1)"] r by (auto simp: strict_mono_def) }
+  then show ?thesis
+    by metis
+qed
+
+subsubsection\<open>Heine-Borel theorem\<close>
+
+proposition seq_compact_imp_heine_borel:
+  fixes s :: "'a :: metric_space set"
+  assumes "seq_compact s"
+  shows "compact s"
+proof -
+  from seq_compact_imp_totally_bounded[OF \<open>seq_compact s\<close>]
+  obtain f where f: "\<forall>e>0. finite (f e) \<and> f e \<subseteq> s \<and> s \<subseteq> (\<Union>x\<in>f e. ball x e)"
+    unfolding choice_iff' ..
+  define K where "K = (\<lambda>(x, r). ball x r) ` ((\<Union>e \<in> \<rat> \<inter> {0 <..}. f e) \<times> \<rat>)"
+  have "countably_compact s"
+    using \<open>seq_compact s\<close> by (rule seq_compact_imp_countably_compact)
+  then show "compact s"
+  proof (rule countably_compact_imp_compact)
+    show "countable K"
+      unfolding K_def using f
+      by (auto intro: countable_finite countable_subset countable_rat
+               intro!: countable_image countable_SIGMA countable_UN)
+    show "\<forall>b\<in>K. open b" by (auto simp: K_def)
+  next
+    fix T x
+    assume T: "open T" "x \<in> T" and x: "x \<in> s"
+    from openE[OF T] obtain e where "0 < e" "ball x e \<subseteq> T"
+      by auto
+    then have "0 < e / 2" "ball x (e / 2) \<subseteq> T"
+      by auto
+    from Rats_dense_in_real[OF \<open>0 < e / 2\<close>] obtain r where "r \<in> \<rat>" "0 < r" "r < e / 2"
+      by auto
+    from f[rule_format, of r] \<open>0 < r\<close> \<open>x \<in> s\<close> obtain k where "k \<in> f r" "x \<in> ball k r"
+      by auto
+    from \<open>r \<in> \<rat>\<close> \<open>0 < r\<close> \<open>k \<in> f r\<close> have "ball k r \<in> K"
+      by (auto simp: K_def)
+    then show "\<exists>b\<in>K. x \<in> b \<and> b \<inter> s \<subseteq> T"
+    proof (rule bexI[rotated], safe)
+      fix y
+      assume "y \<in> ball k r"
+      with \<open>r < e / 2\<close> \<open>x \<in> ball k r\<close> have "dist x y < e"
+        by (intro dist_triangle_half_r [of k _ e]) (auto simp: dist_commute)
+      with \<open>ball x e \<subseteq> T\<close> show "y \<in> T"
+        by auto
+    next
+      show "x \<in> ball k r" by fact
+    qed
+  qed
+qed
+
+proposition compact_eq_seq_compact_metric:
+  "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"
+  using compact_imp_seq_compact seq_compact_imp_heine_borel by blast
+
+proposition compact_def: \<comment> \<open>this is the definition of compactness in HOL Light\<close>
+  "compact (S :: 'a::metric_space set) \<longleftrightarrow>
+   (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l))"
+  unfolding compact_eq_seq_compact_metric seq_compact_def by auto
+
+subsubsection \<open>Complete the chain of compactness variants\<close>
+
+proposition compact_eq_bolzano_weierstrass:
+  fixes s :: "'a::metric_space set"
+  shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))"
+  (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs
+    using heine_borel_imp_bolzano_weierstrass[of s] by auto
+next
+  assume ?rhs
+  then show ?lhs
+    unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)
+qed
+
+proposition bolzano_weierstrass_imp_bounded:
+  "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> bounded s"
+  using compact_imp_bounded unfolding compact_eq_bolzano_weierstrass .
+
+
+subsection \<open>Metric spaces with the Heine-Borel property\<close>
+
+text \<open>
+  A metric space (or topological vector space) is said to have the
+  Heine-Borel property if every closed and bounded subset is compact.
+\<close>
+
+class heine_borel = metric_space +
+  assumes bounded_imp_convergent_subsequence:
+    "bounded (range f) \<Longrightarrow> \<exists>l r. strict_mono (r::nat\<Rightarrow>nat) \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
+
+proposition bounded_closed_imp_seq_compact:
+  fixes s::"'a::heine_borel set"
+  assumes "bounded s"
+    and "closed s"
+  shows "seq_compact s"
+proof (unfold seq_compact_def, clarify)
+  fix f :: "nat \<Rightarrow> 'a"
+  assume f: "\<forall>n. f n \<in> s"
+  with \<open>bounded s\<close> have "bounded (range f)"
+    by (auto intro: bounded_subset)
+  obtain l r where r: "strict_mono (r :: nat \<Rightarrow> nat)" and l: "((f \<circ> r) \<longlongrightarrow> l) sequentially"
+    using bounded_imp_convergent_subsequence [OF \<open>bounded (range f)\<close>] by auto
+  from f have fr: "\<forall>n. (f \<circ> r) n \<in> s"
+    by simp
+  have "l \<in> s" using \<open>closed s\<close> fr l
+    by (rule closed_sequentially)
+  show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
+    using \<open>l \<in> s\<close> r l by blast
+qed
+
+lemma compact_eq_bounded_closed:
+  fixes s :: "'a::heine_borel set"
+  shows "compact s \<longleftrightarrow> bounded s \<and> closed s"
+  (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs
+    using compact_imp_closed compact_imp_bounded
+    by blast
+next
+  assume ?rhs
+  then show ?lhs
+    using bounded_closed_imp_seq_compact[of s]
+    unfolding compact_eq_seq_compact_metric
+    by auto
+qed
+
+lemma compact_Inter:
+  fixes \<F> :: "'a :: heine_borel set set"
+  assumes com: "\<And>S. S \<in> \<F> \<Longrightarrow> compact S" and "\<F> \<noteq> {}"
+  shows "compact(\<Inter> \<F>)"
+  using assms
+  by (meson Inf_lower all_not_in_conv bounded_subset closed_Inter compact_eq_bounded_closed)
+
+lemma compact_closure [simp]:
+  fixes S :: "'a::heine_borel set"
+  shows "compact(closure S) \<longleftrightarrow> bounded S"
+by (meson bounded_closure bounded_subset closed_closure closure_subset compact_eq_bounded_closed)
+
+lemma not_compact_UNIV[simp]:
+  fixes s :: "'a::{real_normed_vector,perfect_space,heine_borel} set"
+  shows "\<not> compact (UNIV::'a set)"
+    by (simp add: compact_eq_bounded_closed)
+
+text\<open>Representing sets as the union of a chain of compact sets.\<close>
+lemma closed_Union_compact_subsets:
+  fixes S :: "'a::{heine_borel,real_normed_vector} set"
+  assumes "closed S"
+  obtains F where "\<And>n. compact(F n)" "\<And>n. F n \<subseteq> S" "\<And>n. F n \<subseteq> F(Suc n)"
+                  "(\<Union>n. F n) = S" "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> \<exists>N. \<forall>n \<ge> N. K \<subseteq> F n"
+proof
+  show "compact (S \<inter> cball 0 (of_nat n))" for n
+    using assms compact_eq_bounded_closed by auto
+next
+  show "(\<Union>n. S \<inter> cball 0 (real n)) = S"
+    by (auto simp: real_arch_simple)
+next
+  fix K :: "'a set"
+  assume "compact K" "K \<subseteq> S"
+  then obtain N where "K \<subseteq> cball 0 N"
+    by (meson bounded_pos mem_cball_0 compact_imp_bounded subsetI)
+  then show "\<exists>N. \<forall>n\<ge>N. K \<subseteq> S \<inter> cball 0 (real n)"
+    by (metis of_nat_le_iff Int_subset_iff \<open>K \<subseteq> S\<close> real_arch_simple subset_cball subset_trans)
+qed auto
+
+instance%important real :: heine_borel
+proof%unimportant
+  fix f :: "nat \<Rightarrow> real"
+  assume f: "bounded (range f)"
+  obtain r :: "nat \<Rightarrow> nat" where r: "strict_mono r" "monoseq (f \<circ> r)"
+    unfolding comp_def by (metis seq_monosub)
+  then have "Bseq (f \<circ> r)"
+    unfolding Bseq_eq_bounded using f by (force intro: bounded_subset)
+  with r show "\<exists>l r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
+    using Bseq_monoseq_convergent[of "f \<circ> r"] by (auto simp: convergent_def)
+qed
+
+lemma compact_lemma_general:
+  fixes f :: "nat \<Rightarrow> 'a"
+  fixes proj::"'a \<Rightarrow> 'b \<Rightarrow> 'c::heine_borel" (infixl "proj" 60)
+  fixes unproj:: "('b \<Rightarrow> 'c) \<Rightarrow> 'a"
+  assumes finite_basis: "finite basis"
+  assumes bounded_proj: "\<And>k. k \<in> basis \<Longrightarrow> bounded ((\<lambda>x. x proj k) ` range f)"
+  assumes proj_unproj: "\<And>e k. k \<in> basis \<Longrightarrow> (unproj e) proj k = e k"
+  assumes unproj_proj: "\<And>x. unproj (\<lambda>k. x proj k) = x"
+  shows "\<forall>d\<subseteq>basis. \<exists>l::'a. \<exists> r::nat\<Rightarrow>nat.
+    strict_mono r \<and> (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) proj i) (l proj i) < e) sequentially)"
+proof safe
+  fix d :: "'b set"
+  assume d: "d \<subseteq> basis"
+  with finite_basis have "finite d"
+    by (blast intro: finite_subset)
+  from this d show "\<exists>l::'a. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and>
+    (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) proj i) (l proj i) < e) sequentially)"
+  proof (induct d)
+    case empty
+    then show ?case
+      unfolding strict_mono_def by auto
+  next
+    case (insert k d)
+    have k[intro]: "k \<in> basis"
+      using insert by auto
+    have s': "bounded ((\<lambda>x. x proj k) ` range f)"
+      using k
+      by (rule bounded_proj)
+    obtain l1::"'a" and r1 where r1: "strict_mono r1"
+      and lr1: "\<forall>e > 0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) proj i) (l1 proj i) < e) sequentially"
+      using insert(3) using insert(4) by auto
+    have f': "\<forall>n. f (r1 n) proj k \<in> (\<lambda>x. x proj k) ` range f"
+      by simp
+    have "bounded (range (\<lambda>i. f (r1 i) proj k))"
+      by (metis (lifting) bounded_subset f' image_subsetI s')
+    then obtain l2 r2 where r2:"strict_mono r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) proj k) \<longlongrightarrow> l2) sequentially"
+      using bounded_imp_convergent_subsequence[of "\<lambda>i. f (r1 i) proj k"]
+      by (auto simp: o_def)
+    define r where "r = r1 \<circ> r2"
+    have r:"strict_mono r"
+      using r1 and r2 unfolding r_def o_def strict_mono_def by auto
+    moreover
+    define l where "l = unproj (\<lambda>i. if i = k then l2 else l1 proj i)"
+    {
+      fix e::real
+      assume "e > 0"
+      from lr1 \<open>e > 0\<close> have N1: "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) proj i) (l1 proj i) < e) sequentially"
+        by blast
+      from lr2 \<open>e > 0\<close> have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) proj k) l2 < e) sequentially"
+        by (rule tendstoD)
+      from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) proj i) (l1 proj i) < e) sequentially"
+        by (rule eventually_subseq)
+      have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) proj i) (l proj i) < e) sequentially"
+        using N1' N2
+        by eventually_elim (insert insert.prems, auto simp: l_def r_def o_def proj_unproj)
+    }
+    ultimately show ?case by auto
+  qed
+qed
+
+lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
+  unfolding bounded_def
+  by (metis (erased, hide_lams) dist_fst_le image_iff order_trans)
+
+lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
+  unfolding bounded_def
+  by (metis (no_types, hide_lams) dist_snd_le image_iff order.trans)
+
+instance%important prod :: (heine_borel, heine_borel) heine_borel
+proof%unimportant
+  fix f :: "nat \<Rightarrow> 'a \<times> 'b"
+  assume f: "bounded (range f)"
+  then have "bounded (fst ` range f)"
+    by (rule bounded_fst)
+  then have s1: "bounded (range (fst \<circ> f))"
+    by (simp add: image_comp)
+  obtain l1 r1 where r1: "strict_mono r1" and l1: "(\<lambda>n. fst (f (r1 n))) \<longlonglongrightarrow> l1"
+    using bounded_imp_convergent_subsequence [OF s1] unfolding o_def by fast
+  from f have s2: "bounded (range (snd \<circ> f \<circ> r1))"
+    by (auto simp: image_comp intro: bounded_snd bounded_subset)
+  obtain l2 r2 where r2: "strict_mono r2" and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) \<longlongrightarrow> l2) sequentially"
+    using bounded_imp_convergent_subsequence [OF s2]
+    unfolding o_def by fast
+  have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) \<longlongrightarrow> l1) sequentially"
+    using LIMSEQ_subseq_LIMSEQ [OF l1 r2] unfolding o_def .
+  have l: "((f \<circ> (r1 \<circ> r2)) \<longlongrightarrow> (l1, l2)) sequentially"
+    using tendsto_Pair [OF l1' l2] unfolding o_def by simp
+  have r: "strict_mono (r1 \<circ> r2)"
+    using r1 r2 unfolding strict_mono_def by simp
+  show "\<exists>l r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
+    using l r by fast
+qed
+
+subsubsection \<open>Completeness\<close>
+
+proposition (in metric_space) completeI:
+  assumes "\<And>f. \<forall>n. f n \<in> s \<Longrightarrow> Cauchy f \<Longrightarrow> \<exists>l\<in>s. f \<longlonglongrightarrow> l"
+  shows "complete s"
+  using assms unfolding complete_def by fast
+
+proposition (in metric_space) completeE:
+  assumes "complete s" and "\<forall>n. f n \<in> s" and "Cauchy f"
+  obtains l where "l \<in> s" and "f \<longlonglongrightarrow> l"
+  using assms unfolding complete_def by fast
+
+(* TODO: generalize to uniform spaces *)
+lemma compact_imp_complete:
+  fixes s :: "'a::metric_space set"
+  assumes "compact s"
+  shows "complete s"
+proof -
+  {
+    fix f
+    assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
+    from as(1) obtain l r where lr: "l\<in>s" "strict_mono r" "(f \<circ> r) \<longlonglongrightarrow> l"
+      using assms unfolding compact_def by blast
+
+    note lr' = seq_suble [OF lr(2)]
+    {
+      fix e :: real
+      assume "e > 0"
+      from as(2) obtain N where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (f m) (f n) < e/2"
+        unfolding cauchy_def
+        using \<open>e > 0\<close>
+        apply (erule_tac x="e/2" in allE, auto)
+        done
+      from lr(3)[unfolded lim_sequentially, THEN spec[where x="e/2"]]
+      obtain M where M:"\<forall>n\<ge>M. dist ((f \<circ> r) n) l < e/2"
+        using \<open>e > 0\<close> by auto
+      {
+        fix n :: nat
+        assume n: "n \<ge> max N M"
+        have "dist ((f \<circ> r) n) l < e/2"
+          using n M by auto
+        moreover have "r n \<ge> N"
+          using lr'[of n] n by auto
+        then have "dist (f n) ((f \<circ> r) n) < e / 2"
+          using N and n by auto
+        ultimately have "dist (f n) l < e"
+          using dist_triangle_half_r[of "f (r n)" "f n" e l]
+          by (auto simp: dist_commute)
+      }
+      then have "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast
+    }
+    then have "\<exists>l\<in>s. (f \<longlongrightarrow> l) sequentially" using \<open>l\<in>s\<close>
+      unfolding lim_sequentially by auto
+  }
+  then show ?thesis unfolding complete_def by auto
+qed
+
+proposition compact_eq_totally_bounded:
+  "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>x\<in>k. ball x e))"
+    (is "_ \<longleftrightarrow> ?rhs")
+proof
+  assume assms: "?rhs"
+  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)"
+    by (auto simp: choice_iff')
+
+  show "compact s"
+  proof cases
+    assume "s = {}"
+    then show "compact s" by (simp add: compact_def)
+  next
+    assume "s \<noteq> {}"
+    show ?thesis
+      unfolding compact_def
+    proof safe
+      fix f :: "nat \<Rightarrow> 'a"
+      assume f: "\<forall>n. f n \<in> s"
+
+      define e where "e n = 1 / (2 * Suc n)" for n
+      then have [simp]: "\<And>n. 0 < e n" by auto
+      define B where "B n U = (SOME b. infinite {n. f n \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U))" for n U
+      {
+        fix n U
+        assume "infinite {n. f n \<in> U}"
+        then have "\<exists>b\<in>k (e n). infinite {i\<in>{n. f n \<in> U}. f i \<in> ball b (e n)}"
+          using k f by (intro pigeonhole_infinite_rel) (auto simp: subset_eq)
+        then obtain a where
+          "a \<in> k (e n)"
+          "infinite {i \<in> {n. f n \<in> U}. f i \<in> ball a (e n)}" ..
+        then have "\<exists>b. infinite {i. f i \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U)"
+          by (intro exI[of _ "ball a (e n) \<inter> U"] exI[of _ a]) (auto simp: ac_simps)
+        from someI_ex[OF this]
+        have "infinite {i. f i \<in> B n U}" "\<exists>x. B n U \<subseteq> ball x (e n) \<inter> U"
+          unfolding B_def by auto
+      }
+      note B = this
+
+      define F where "F = rec_nat (B 0 UNIV) B"
+      {
+        fix n
+        have "infinite {i. f i \<in> F n}"
+          by (induct n) (auto simp: F_def B)
+      }
+      then have F: "\<And>n. \<exists>x. F (Suc n) \<subseteq> ball x (e n) \<inter> F n"
+        using B by (simp add: F_def)
+      then have F_dec: "\<And>m n. m \<le> n \<Longrightarrow> F n \<subseteq> F m"
+        using decseq_SucI[of F] by (auto simp: decseq_def)
+
+      obtain sel where sel: "\<And>k i. i < sel k i" "\<And>k i. f (sel k i) \<in> F k"
+      proof (atomize_elim, unfold all_conj_distrib[symmetric], intro choice allI)
+        fix k i
+        have "infinite ({n. f n \<in> F k} - {.. i})"
+          using \<open>infinite {n. f n \<in> F k}\<close> by auto
+        from infinite_imp_nonempty[OF this]
+        show "\<exists>x>i. f x \<in> F k"
+          by (simp add: set_eq_iff not_le conj_commute)
+      qed
+
+      define t where "t = rec_nat (sel 0 0) (\<lambda>n i. sel (Suc n) i)"
+      have "strict_mono t"
+        unfolding strict_mono_Suc_iff by (simp add: t_def sel)
+      moreover have "\<forall>i. (f \<circ> t) i \<in> s"
+        using f by auto
+      moreover
+      {
+        fix n
+        have "(f \<circ> t) n \<in> F n"
+          by (cases n) (simp_all add: t_def sel)
+      }
+      note t = this
+
+      have "Cauchy (f \<circ> t)"
+      proof (safe intro!: metric_CauchyI exI elim!: nat_approx_posE)
+        fix r :: real and N n m
+        assume "1 / Suc N < r" "Suc N \<le> n" "Suc N \<le> m"
+        then have "(f \<circ> t) n \<in> F (Suc N)" "(f \<circ> t) m \<in> F (Suc N)" "2 * e N < r"
+          using F_dec t by (auto simp: e_def field_simps of_nat_Suc)
+        with F[of N] obtain x where "dist x ((f \<circ> t) n) < e N" "dist x ((f \<circ> t) m) < e N"
+          by (auto simp: subset_eq)
+        with dist_triangle[of "(f \<circ> t) m" "(f \<circ> t) n" x] \<open>2 * e N < r\<close>
+        show "dist ((f \<circ> t) m) ((f \<circ> t) n) < r"
+          by (simp add: dist_commute)
+      qed
+
+      ultimately show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
+        using assms unfolding complete_def by blast
+    qed
+  qed
+qed (metis compact_imp_complete compact_imp_seq_compact seq_compact_imp_totally_bounded)
+
+lemma cauchy_imp_bounded:
+  assumes "Cauchy s"
+  shows "bounded (range s)"
+proof -
+  from assms obtain N :: nat where "\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < 1"
+    unfolding cauchy_def by force
+  then have N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
+  moreover
+  have "bounded (s ` {0..N})"
+    using finite_imp_bounded[of "s ` {1..N}"] by auto
+  then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
+    unfolding bounded_any_center [where a="s N"] by auto
+  ultimately show "?thesis"
+    unfolding bounded_any_center [where a="s N"]
+    apply (rule_tac x="max a 1" in exI, auto)
+    apply (erule_tac x=y in allE)
+    apply (erule_tac x=y in ballE, auto)
+    done
+qed
+
+instance heine_borel < complete_space
+proof
+  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
+  then have "bounded (range f)"
+    by (rule cauchy_imp_bounded)
+  then have "compact (closure (range f))"
+    unfolding compact_eq_bounded_closed by auto
+  then have "complete (closure (range f))"
+    by (rule compact_imp_complete)
+  moreover have "\<forall>n. f n \<in> closure (range f)"
+    using closure_subset [of "range f"] by auto
+  ultimately have "\<exists>l\<in>closure (range f). (f \<longlongrightarrow> l) sequentially"
+    using \<open>Cauchy f\<close> unfolding complete_def by auto
+  then show "convergent f"
+    unfolding convergent_def by auto
+qed
+
+lemma complete_UNIV: "complete (UNIV :: ('a::complete_space) set)"
+proof (rule completeI)
+  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
+  then have "convergent f" by (rule Cauchy_convergent)
+  then show "\<exists>l\<in>UNIV. f \<longlonglongrightarrow> l" unfolding convergent_def by simp
+qed
+
+lemma complete_imp_closed:
+  fixes S :: "'a::metric_space set"
+  assumes "complete S"
+  shows "closed S"
+proof (unfold closed_sequential_limits, clarify)
+  fix f x assume "\<forall>n. f n \<in> S" and "f \<longlonglongrightarrow> x"
+  from \<open>f \<longlonglongrightarrow> x\<close> have "Cauchy f"
+    by (rule LIMSEQ_imp_Cauchy)
+  with \<open>complete S\<close> and \<open>\<forall>n. f n \<in> S\<close> obtain l where "l \<in> S" and "f \<longlonglongrightarrow> l"
+    by (rule completeE)
+  from \<open>f \<longlonglongrightarrow> x\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "x = l"
+    by (rule LIMSEQ_unique)
+  with \<open>l \<in> S\<close> show "x \<in> S"
+    by simp
+qed
+
+lemma complete_Int_closed:
+  fixes S :: "'a::metric_space set"
+  assumes "complete S" and "closed t"
+  shows "complete (S \<inter> t)"
+proof (rule completeI)
+  fix f assume "\<forall>n. f n \<in> S \<inter> t" and "Cauchy f"
+  then have "\<forall>n. f n \<in> S" and "\<forall>n. f n \<in> t"
+    by simp_all
+  from \<open>complete S\<close> obtain l where "l \<in> S" and "f \<longlonglongrightarrow> l"
+    using \<open>\<forall>n. f n \<in> S\<close> and \<open>Cauchy f\<close> by (rule completeE)
+  from \<open>closed t\<close> and \<open>\<forall>n. f n \<in> t\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "l \<in> t"
+    by (rule closed_sequentially)
+  with \<open>l \<in> S\<close> and \<open>f \<longlonglongrightarrow> l\<close> show "\<exists>l\<in>S \<inter> t. f \<longlonglongrightarrow> l"
+    by fast
+qed
+
+lemma complete_closed_subset:
+  fixes S :: "'a::metric_space set"
+  assumes "closed S" and "S \<subseteq> t" and "complete t"
+  shows "complete S"
+  using assms complete_Int_closed [of t S] by (simp add: Int_absorb1)
+
+lemma complete_eq_closed:
+  fixes S :: "('a::complete_space) set"
+  shows "complete S \<longleftrightarrow> closed S"
+proof
+  assume "closed S" then show "complete S"
+    using subset_UNIV complete_UNIV by (rule complete_closed_subset)
+next
+  assume "complete S" then show "closed S"
+    by (rule complete_imp_closed)
+qed
+
+lemma convergent_eq_Cauchy:
+  fixes S :: "nat \<Rightarrow> 'a::complete_space"
+  shows "(\<exists>l. (S \<longlongrightarrow> l) sequentially) \<longleftrightarrow> Cauchy S"
+  unfolding Cauchy_convergent_iff convergent_def ..
+
+lemma convergent_imp_bounded:
+  fixes S :: "nat \<Rightarrow> 'a::metric_space"
+  shows "(S \<longlongrightarrow> l) sequentially \<Longrightarrow> bounded (range S)"
+  by (intro cauchy_imp_bounded LIMSEQ_imp_Cauchy)
+
+lemma frontier_subset_compact:
+  fixes S :: "'a::heine_borel set"
+  shows "compact S \<Longrightarrow> frontier S \<subseteq> S"
+  using frontier_subset_closed compact_eq_bounded_closed
+  by blast
+
+subsection \<open>Continuity\<close>
+
+text\<open>Derive the epsilon-delta forms, which we often use as "definitions"\<close>
+
+proposition continuous_within_eps_delta:
+  "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)"
+  unfolding continuous_within and Lim_within  by fastforce
+
+corollary continuous_at_eps_delta:
+  "continuous (at x) f \<longleftrightarrow> (\<forall>e > 0. \<exists>d > 0. \<forall>x'. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
+  using continuous_within_eps_delta [of x UNIV f] by simp
+
+lemma continuous_at_right_real_increasing:
+  fixes f :: "real \<Rightarrow> real"
+  assumes nondecF: "\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y"
+  shows "continuous (at_right a) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f (a + d) - f a < e)"
+  apply (simp add: greaterThan_def dist_real_def continuous_within Lim_within_le)
+  apply (intro all_cong ex_cong, safe)
+  apply (erule_tac x="a + d" in allE, simp)
+  apply (simp add: nondecF field_simps)
+  apply (drule nondecF, simp)
+  done
+
+lemma continuous_at_left_real_increasing:
+  assumes nondecF: "\<And> x y. x \<le> y \<Longrightarrow> f x \<le> ((f y) :: real)"
+  shows "(continuous (at_left (a :: real)) f) = (\<forall>e > 0. \<exists>delta > 0. f a - f (a - delta) < e)"
+  apply (simp add: lessThan_def dist_real_def continuous_within Lim_within_le)
+  apply (intro all_cong ex_cong, safe)
+  apply (erule_tac x="a - d" in allE, simp)
+  apply (simp add: nondecF field_simps)
+  apply (cut_tac x="a - d" and y=x in nondecF, simp_all)
+  done
+
+text\<open>Versions in terms of open balls.\<close>
+
+lemma continuous_within_ball:
+  "continuous (at x within s) f \<longleftrightarrow>
+    (\<forall>e > 0. \<exists>d > 0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e)"
+  (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  {
+    fix e :: real
+    assume "e > 0"
+    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"
+      using \<open>?lhs\<close>[unfolded continuous_within Lim_within] by auto
+    {
+      fix y
+      assume "y \<in> f ` (ball x d \<inter> s)"
+      then have "y \<in> ball (f x) e"
+        using d(2)
+        apply (auto simp: dist_commute)
+        apply (erule_tac x=xa in ballE, auto)
+        using \<open>e > 0\<close>
+        apply auto
+        done
+    }
+    then have "\<exists>d>0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e"
+      using \<open>d > 0\<close>
+      unfolding subset_eq ball_def by (auto simp: dist_commute)
+  }
+  then show ?rhs by auto
+next
+  assume ?rhs
+  then show ?lhs
+    unfolding continuous_within Lim_within ball_def subset_eq
+    apply (auto simp: dist_commute)
+    apply (erule_tac x=e in allE, auto)
+    done
+qed
+
+lemma continuous_at_ball:
+  "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f ` (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs
+    unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
+    apply auto
+    apply (erule_tac x=e in allE, auto)
+    apply (rule_tac x=d in exI, auto)
+    apply (erule_tac x=xa in allE)
+    apply (auto simp: dist_commute)
+    done
+next
+  assume ?rhs
+  then show ?lhs
+    unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
+    apply auto
+    apply (erule_tac x=e in allE, auto)
+    apply (rule_tac x=d in exI, auto)
+    apply (erule_tac x="f xa" in allE)
+    apply (auto simp: dist_commute)
+    done
+qed
+
+text\<open>Define setwise continuity in terms of limits within the set.\<close>
+
+lemma continuous_on_iff:
+  "continuous_on s f \<longleftrightarrow>
+    (\<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)"
+  unfolding continuous_on_def Lim_within
+  by (metis dist_pos_lt dist_self)
+
+lemma continuous_within_E:
+  assumes "continuous (at x within s) f" "e>0"
+  obtains d where "d>0"  "\<And>x'. \<lbrakk>x'\<in> s; dist x' x \<le> d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
+  using assms apply (simp add: continuous_within_eps_delta)
+  apply (drule spec [of _ e], clarify)
+  apply (rule_tac d="d/2" in that, auto)
+  done
+
+lemma continuous_onI [intro?]:
+  assumes "\<And>x e. \<lbrakk>e > 0; x \<in> s\<rbrakk> \<Longrightarrow> \<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) \<le> e"
+  shows "continuous_on s f"
+apply (simp add: continuous_on_iff, clarify)
+apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
+done
+
+text\<open>Some simple consequential lemmas.\<close>
+
+lemma continuous_onE:
+    assumes "continuous_on s f" "x\<in>s" "e>0"
+    obtains d where "d>0"  "\<And>x'. \<lbrakk>x' \<in> s; dist x' x \<le> d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
+  using assms
+  apply (simp add: continuous_on_iff)
+  apply (elim ballE allE)
+  apply (auto intro: that [where d="d/2" for d])
+  done
+
+lemma uniformly_continuous_onE:
+  assumes "uniformly_continuous_on s f" "0 < e"
+  obtains d where "d>0" "\<And>x x'. \<lbrakk>x\<in>s; x'\<in>s; dist x' x < d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
+using assms
+by (auto simp: uniformly_continuous_on_def)
+
+lemma continuous_at_imp_continuous_within:
+  "continuous (at x) f \<Longrightarrow> continuous (at x within s) f"
+  unfolding continuous_within continuous_at using Lim_at_imp_Lim_at_within by auto
+
+lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f \<longlongrightarrow> l) net"
+  by simp
+
+lemmas continuous_on = continuous_on_def \<comment> \<open>legacy theorem name\<close>
+
+lemma continuous_within_subset:
+  "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s \<Longrightarrow> continuous (at x within t) f"
+  unfolding continuous_within by(metis tendsto_within_subset)
+
+lemma continuous_on_interior:
+  "continuous_on s f \<Longrightarrow> x \<in> interior s \<Longrightarrow> continuous (at x) f"
+  by (metis continuous_on_eq_continuous_at continuous_on_subset interiorE)
+
+lemma continuous_on_eq:
+  "\<lbrakk>continuous_on s f; \<And>x. x \<in> s \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> continuous_on s g"
+  unfolding continuous_on_def tendsto_def eventually_at_topological
+  by simp
+
+text \<open>Characterization of various kinds of continuity in terms of sequences.\<close>
+
+lemma continuous_within_sequentiallyI:
+  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
+  assumes "\<And>u::nat \<Rightarrow> 'a. u \<longlonglongrightarrow> a \<Longrightarrow> (\<forall>n. u n \<in> s) \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
+  shows "continuous (at a within s) f"
+  using assms unfolding continuous_within tendsto_def[where l = "f a"]
+  by (auto intro!: sequentially_imp_eventually_within)
+
+lemma continuous_within_tendsto_compose:
+  fixes f::"'a::t2_space \<Rightarrow> 'b::topological_space"
+  assumes "continuous (at a within s) f"
+          "eventually (\<lambda>n. x n \<in> s) F"
+          "(x \<longlongrightarrow> a) F "
+  shows "((\<lambda>n. f (x n)) \<longlongrightarrow> f a) F"
+proof -
+  have *: "filterlim x (inf (nhds a) (principal s)) F"
+    using assms(2) assms(3) unfolding at_within_def filterlim_inf by (auto simp: filterlim_principal eventually_mono)
+  show ?thesis
+    by (auto simp: assms(1) continuous_within[symmetric] tendsto_at_within_iff_tendsto_nhds[symmetric] intro!: filterlim_compose[OF _ *])
+qed
+
+lemma continuous_within_tendsto_compose':
+  fixes f::"'a::t2_space \<Rightarrow> 'b::topological_space"
+  assumes "continuous (at a within s) f"
+    "\<And>n. x n \<in> s"
+    "(x \<longlongrightarrow> a) F "
+  shows "((\<lambda>n. f (x n)) \<longlongrightarrow> f a) F"
+  by (auto intro!: continuous_within_tendsto_compose[OF assms(1)] simp add: assms)
+
+lemma continuous_within_sequentially:
+  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
+  shows "continuous (at a within s) f \<longleftrightarrow>
+    (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x \<longlongrightarrow> a) sequentially
+         \<longrightarrow> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
+  using continuous_within_tendsto_compose'[of a s f _ sequentially]
+    continuous_within_sequentiallyI[of a s f]
+  by (auto simp: o_def)
+
+lemma continuous_at_sequentiallyI:
+  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
+  assumes "\<And>u. u \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
+  shows "continuous (at a) f"
+  using continuous_within_sequentiallyI[of a UNIV f] assms by auto
+
+lemma continuous_at_sequentially:
+  fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
+  shows "continuous (at a) f \<longleftrightarrow>
+    (\<forall>x. (x \<longlongrightarrow> a) sequentially --> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
+  using continuous_within_sequentially[of a UNIV f] by simp
+
+lemma continuous_on_sequentiallyI:
+  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
+  assumes "\<And>u a. (\<forall>n. u n \<in> s) \<Longrightarrow> a \<in> s \<Longrightarrow> u \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
+  shows "continuous_on s f"
+  using assms unfolding continuous_on_eq_continuous_within
+  using continuous_within_sequentiallyI[of _ s f] by auto
+
+lemma continuous_on_sequentially:
+  fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
+  shows "continuous_on s f \<longleftrightarrow>
+    (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x \<longlongrightarrow> a) sequentially
+      --> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
+    (is "?lhs = ?rhs")
+proof
+  assume ?rhs
+  then show ?lhs
+    using continuous_within_sequentially[of _ s f]
+    unfolding continuous_on_eq_continuous_within
+    by auto
+next
+  assume ?lhs
+  then show ?rhs
+    unfolding continuous_on_eq_continuous_within
+    using continuous_within_sequentially[of _ s f]
+    by auto
+qed
+
+lemma uniformly_continuous_on_sequentially:
+  "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
+    (\<lambda>n. dist (x n) (y n)) \<longlonglongrightarrow> 0 \<longrightarrow> (\<lambda>n. dist (f(x n)) (f(y n))) \<longlonglongrightarrow> 0)" (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  {
+    fix x y
+    assume x: "\<forall>n. x n \<in> s"
+      and y: "\<forall>n. y n \<in> s"
+      and xy: "((\<lambda>n. dist (x n) (y n)) \<longlongrightarrow> 0) sequentially"
+    {
+      fix e :: real
+      assume "e > 0"
+      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"
+        using \<open>?lhs\<close>[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto
+      obtain N where N: "\<forall>n\<ge>N. dist (x n) (y n) < d"
+        using xy[unfolded lim_sequentially dist_norm] and \<open>d>0\<close> by auto
+      {
+        fix n
+        assume "n\<ge>N"
+        then have "dist (f (x n)) (f (y n)) < e"
+          using N[THEN spec[where x=n]]
+          using d[THEN bspec[where x="x n"], THEN bspec[where x="y n"]]
+          using x and y
+          by (simp add: dist_commute)
+      }
+      then have "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"
+        by auto
+    }
+    then have "((\<lambda>n. dist (f(x n)) (f(y n))) \<longlongrightarrow> 0) sequentially"
+      unfolding lim_sequentially and dist_real_def by auto
+  }
+  then show ?rhs by auto
+next
+  assume ?rhs
+  {
+    assume "\<not> ?lhs"
+    then obtain e where "e > 0" "\<forall>d>0. \<exists>x\<in>s. \<exists>x'\<in>s. dist x' x < d \<and> \<not> dist (f x') (f x) < e"
+      unfolding uniformly_continuous_on_def by auto
+    then obtain fa where fa:
+      "\<forall>x. 0 < x \<longrightarrow> fst (fa x) \<in> s \<and> snd (fa x) \<in> s \<and> dist (fst (fa x)) (snd (fa x)) < x \<and> \<not> dist (f (fst (fa x))) (f (snd (fa x))) < e"
+      using choice[of "\<lambda>d x. d>0 \<longrightarrow> fst x \<in> s \<and> snd x \<in> s \<and> dist (snd x) (fst x) < d \<and> \<not> dist (f (snd x)) (f (fst x)) < e"]
+      unfolding Bex_def
+      by (auto simp: dist_commute)
+    define x where "x n = fst (fa (inverse (real n + 1)))" for n
+    define y where "y n = snd (fa (inverse (real n + 1)))" for n
+    have xyn: "\<forall>n. x n \<in> s \<and> y n \<in> s"
+      and xy0: "\<forall>n. dist (x n) (y n) < inverse (real n + 1)"
+      and fxy:"\<forall>n. \<not> dist (f (x n)) (f (y n)) < e"
+      unfolding x_def and y_def using fa
+      by auto
+    {
+      fix e :: real
+      assume "e > 0"
+      then obtain N :: nat where "N \<noteq> 0" and N: "0 < inverse (real N) \<and> inverse (real N) < e"
+        unfolding real_arch_inverse[of e] by auto
+      {
+        fix n :: nat
+        assume "n \<ge> N"
+        then have "inverse (real n + 1) < inverse (real N)"
+          using of_nat_0_le_iff and \<open>N\<noteq>0\<close> by auto
+        also have "\<dots> < e" using N by auto
+        finally have "inverse (real n + 1) < e" by auto
+        then have "dist (x n) (y n) < e"
+          using xy0[THEN spec[where x=n]] by auto
+      }
+      then have "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto
+    }
+    then have "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"
+      using \<open>?rhs\<close>[THEN spec[where x=x], THEN spec[where x=y]] and xyn
+      unfolding lim_sequentially dist_real_def by auto
+    then have False using fxy and \<open>e>0\<close> by auto
+  }
+  then show ?lhs
+    unfolding uniformly_continuous_on_def by blast
+qed
+
+lemma continuous_closed_imp_Cauchy_continuous:
+  fixes S :: "('a::complete_space) set"
+  shows "\<lbrakk>continuous_on S f; closed S; Cauchy \<sigma>; \<And>n. (\<sigma> n) \<in> S\<rbrakk> \<Longrightarrow> Cauchy(f \<circ> \<sigma>)"
+  apply (simp add: complete_eq_closed [symmetric] continuous_on_sequentially)
+  by (meson LIMSEQ_imp_Cauchy complete_def)
+
+text\<open>The usual transformation theorems.\<close>
+
+lemma continuous_transform_within:
+  fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
+  assumes "continuous (at x within s) f"
+    and "0 < d"
+    and "x \<in> s"
+    and "\<And>x'. \<lbrakk>x' \<in> s; dist x' x < d\<rbrakk> \<Longrightarrow> f x' = g x'"
+  shows "continuous (at x within s) g"
+  using assms
+  unfolding continuous_within
+  by (force intro: Lim_transform_within)
+
+
+subsubsection%unimportant \<open>Structural rules for uniform continuity\<close>
+
+lemma uniformly_continuous_on_dist[continuous_intros]:
+  fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"
+  assumes "uniformly_continuous_on s f"
+    and "uniformly_continuous_on s g"
+  shows "uniformly_continuous_on s (\<lambda>x. dist (f x) (g x))"
+proof -
+  {
+    fix a b c d :: 'b
+    have "\<bar>dist a b - dist c d\<bar> \<le> dist a c + dist b d"
+      using dist_triangle2 [of a b c] dist_triangle2 [of b c d]
+      using dist_triangle3 [of c d a] dist_triangle [of a d b]
+      by arith
+  } note le = this
+  {
+    fix x y
+    assume f: "(\<lambda>n. dist (f (x n)) (f (y n))) \<longlonglongrightarrow> 0"
+    assume g: "(\<lambda>n. dist (g (x n)) (g (y n))) \<longlonglongrightarrow> 0"
+    have "(\<lambda>n. \<bar>dist (f (x n)) (g (x n)) - dist (f (y n)) (g (y n))\<bar>) \<longlonglongrightarrow> 0"
+      by (rule Lim_transform_bound [OF _ tendsto_add_zero [OF f g]],
+        simp add: le)
+  }
+  then show ?thesis
+    using assms unfolding uniformly_continuous_on_sequentially
+    unfolding dist_real_def by simp
+qed
+
+lemma uniformly_continuous_on_norm[continuous_intros]:
+  fixes f :: "'a :: metric_space \<Rightarrow> 'b :: real_normed_vector"
+  assumes "uniformly_continuous_on s f"
+  shows "uniformly_continuous_on s (\<lambda>x. norm (f x))"
+  unfolding norm_conv_dist using assms
+  by (intro uniformly_continuous_on_dist uniformly_continuous_on_const)
+
+lemma (in bounded_linear) uniformly_continuous_on[continuous_intros]:
+  fixes g :: "_::metric_space \<Rightarrow> _"
+  assumes "uniformly_continuous_on s g"
+  shows "uniformly_continuous_on s (\<lambda>x. f (g x))"
+  using assms unfolding uniformly_continuous_on_sequentially
+  unfolding dist_norm tendsto_norm_zero_iff diff[symmetric]
+  by (auto intro: tendsto_zero)
+
+lemma uniformly_continuous_on_cmul[continuous_intros]:
+  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
+  assumes "uniformly_continuous_on s f"
+  shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"
+  using bounded_linear_scaleR_right assms
+  by (rule bounded_linear.uniformly_continuous_on)
+
+lemma dist_minus:
+  fixes x y :: "'a::real_normed_vector"
+  shows "dist (- x) (- y) = dist x y"
+  unfolding dist_norm minus_diff_minus norm_minus_cancel ..
+
+lemma uniformly_continuous_on_minus[continuous_intros]:
+  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
+  shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. - f x)"
+  unfolding uniformly_continuous_on_def dist_minus .
+
+lemma uniformly_continuous_on_add[continuous_intros]:
+  fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
+  assumes "uniformly_continuous_on s f"
+    and "uniformly_continuous_on s g"
+  shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"
+  using assms
+  unfolding uniformly_continuous_on_sequentially
+  unfolding dist_norm tendsto_norm_zero_iff add_diff_add
+  by (auto intro: tendsto_add_zero)
+
+lemma uniformly_continuous_on_diff[continuous_intros]:
+  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
+  assumes "uniformly_continuous_on s f"
+    and "uniformly_continuous_on s g"
+  shows "uniformly_continuous_on s (\<lambda>x. f x - g x)"
+  using assms uniformly_continuous_on_add [of s f "- g"]
+    by (simp add: fun_Compl_def uniformly_continuous_on_minus)
+
+text \<open>Continuity in terms of open preimages.\<close>
+
+lemma continuous_at_open:
+  "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)))"
+  unfolding continuous_within_topological [of x UNIV f]
+  unfolding imp_conjL
+  by (intro all_cong imp_cong ex_cong conj_cong refl) auto
+
+lemma continuous_imp_tendsto:
+  assumes "continuous (at x0) f"
+    and "x \<longlonglongrightarrow> x0"
+  shows "(f \<circ> x) \<longlonglongrightarrow> (f x0)"
+proof (rule topological_tendstoI)
+  fix S
+  assume "open S" "f x0 \<in> S"
+  then obtain T where T_def: "open T" "x0 \<in> T" "\<forall>x\<in>T. f x \<in> S"
+     using assms continuous_at_open by metis
+  then have "eventually (\<lambda>n. x n \<in> T) sequentially"
+    using assms T_def by (auto simp: tendsto_def)
+  then show "eventually (\<lambda>n. (f \<circ> x) n \<in> S) sequentially"
+    using T_def by (auto elim!: eventually_mono)
+qed
+
+lemma continuous_on_open:
+  "continuous_on S f \<longleftrightarrow>
+    (\<forall>T. openin (subtopology euclidean (f ` S)) T \<longrightarrow>
+      openin (subtopology euclidean S) (S \<inter> f -` T))"
+  unfolding continuous_on_open_invariant openin_open Int_def vimage_def Int_commute
+  by (simp add: imp_ex imageI conj_commute eq_commute cong: conj_cong)
+
+lemma continuous_on_open_gen:
+  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
+  assumes "f ` S \<subseteq> T"
+    shows "continuous_on S f \<longleftrightarrow>
+             (\<forall>U. openin (subtopology euclidean T) U
+                  \<longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` U))"
+     (is "?lhs = ?rhs")
+proof
+  assume ?lhs
+  then show ?rhs
+    apply (clarsimp simp: openin_euclidean_subtopology_iff continuous_on_iff)
+    by (metis assms image_subset_iff)
+next
+  have ope: "openin (subtopology euclidean T) (ball y e \<inter> T)" for y e
+    by (simp add: Int_commute openin_open_Int)
+  assume R [rule_format]: ?rhs
+  show ?lhs
+  proof (clarsimp simp add: continuous_on_iff)
+    fix x and e::real
+    assume "x \<in> S" and "0 < e"
+    then have x: "x \<in> S \<inter> (f -` ball (f x) e \<inter> f -` T)"
+      using assms by auto
+    show "\<exists>d>0. \<forall>x'\<in>S. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
+      using R [of "ball (f x) e \<inter> T"] x
+      by (fastforce simp add: ope openin_euclidean_subtopology_iff [of S] dist_commute)
+  qed
+qed
+
+lemma continuous_openin_preimage:
+  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
+  shows
+   "\<lbrakk>continuous_on S f; f ` S \<subseteq> T; openin (subtopology euclidean T) U\<rbrakk>
+        \<Longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` U)"
+by (simp add: continuous_on_open_gen)
+
+text \<open>Similarly in terms of closed sets.\<close>
+
+lemma continuous_on_closed:
+  "continuous_on S f \<longleftrightarrow>
+    (\<forall>T. closedin (subtopology euclidean (f ` S)) T \<longrightarrow>
+      closedin (subtopology euclidean S) (S \<inter> f -` T))"
+  unfolding continuous_on_closed_invariant closedin_closed Int_def vimage_def Int_commute
+  by (simp add: imp_ex imageI conj_commute eq_commute cong: conj_cong)
+
+lemma continuous_on_closed_gen:
+  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
+  assumes "f ` S \<subseteq> T"
+    shows "continuous_on S f \<longleftrightarrow>
+             (\<forall>U. closedin (subtopology euclidean T) U
+                  \<longrightarrow> closedin (subtopology euclidean S) (S \<inter> f -` U))"
+     (is "?lhs = ?rhs")
+proof -
+  have *: "U \<subseteq> T \<Longrightarrow> S \<inter> f -` (T - U) = S - (S \<inter> f -` U)" for U
+    using assms by blast
+  show ?thesis
+  proof
+    assume L: ?lhs
+    show ?rhs
+    proof clarify
+      fix U
+      assume "closedin (subtopology euclidean T) U"
+      then show "closedin (subtopology euclidean S) (S \<inter> f -` U)"
+        using L unfolding continuous_on_open_gen [OF assms]
+        by (metis * closedin_def inf_le1 topspace_euclidean_subtopology)
+    qed
+  next
+    assume R [rule_format]: ?rhs
+    show ?lhs
+      unfolding continuous_on_open_gen [OF assms]
+      by (metis * R inf_le1 openin_closedin_eq topspace_euclidean_subtopology)
+  qed
+qed
+
+lemma continuous_closedin_preimage_gen:
+  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
+  assumes "continuous_on S f" "f ` S \<subseteq> T" "closedin (subtopology euclidean T) U"
+    shows "closedin (subtopology euclidean S) (S \<inter> f -` U)"
+using assms continuous_on_closed_gen by blast
+
+lemma continuous_on_imp_closedin:
+  assumes "continuous_on S f" "closedin (subtopology euclidean (f ` S)) T"
+    shows "closedin (subtopology euclidean S) (S \<inter> f -` T)"
+using assms continuous_on_closed by blast
+
+subsection%unimportant \<open>Half-global and completely global cases\<close>
+
+lemma continuous_openin_preimage_gen:
+  assumes "continuous_on S f"  "open T"
+  shows "openin (subtopology euclidean S) (S \<inter> f -` T)"
+proof -
+  have *: "(S \<inter> f -` T) = (S \<inter> f -` (T \<inter> f ` S))"
+    by auto
+  have "openin (subtopology euclidean (f ` S)) (T \<inter> f ` S)"
+    using openin_open_Int[of T "f ` S", OF assms(2)] unfolding openin_open by auto
+  then show ?thesis
+    using assms(1)[unfolded continuous_on_open, THEN spec[where x="T \<inter> f ` S"]]
+    using * by auto
+qed
+
+lemma continuous_closedin_preimage:
+  assumes "continuous_on S f" and "closed T"
+  shows "closedin (subtopology euclidean S) (S \<inter> f -` T)"
+proof -
+  have *: "(S \<inter> f -` T) = (S \<inter> f -` (T \<inter> f ` S))"
+    by auto
+  have "closedin (subtopology euclidean (f ` S)) (T \<inter> f ` S)"
+    using closedin_closed_Int[of T "f ` S", OF assms(2)]
+    by (simp add: Int_commute)
+  then show ?thesis
+    using assms(1)[unfolded continuous_on_closed, THEN spec[where x="T \<inter> f ` S"]]
+    using * by auto
+qed
+
+lemma continuous_openin_preimage_eq:
+   "continuous_on S f \<longleftrightarrow>
+    (\<forall>T. open T \<longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` T))"
+apply safe
+apply (simp add: continuous_openin_preimage_gen)
+apply (fastforce simp add: continuous_on_open openin_open)
+done
+
+lemma continuous_closedin_preimage_eq:
+   "continuous_on S f \<longleftrightarrow>
+    (\<forall>T. closed T \<longrightarrow> closedin (subtopology euclidean S) (S \<inter> f -` T))"
+apply safe
+apply (simp add: continuous_closedin_preimage)
+apply (fastforce simp add: continuous_on_closed closedin_closed)
+done
+
+lemma continuous_open_preimage:
+  assumes contf: "continuous_on S f" and "open S" "open T"
+  shows "open (S \<inter> f -` T)"
+proof-
+  obtain U where "open U" "(S \<inter> f -` T) = S \<inter> U"
+    using continuous_openin_preimage_gen[OF contf \<open>open T\<close>]
+    unfolding openin_open by auto
+  then show ?thesis
+    using open_Int[of S U, OF \<open>open S\<close>] by auto
+qed
+
+lemma continuous_closed_preimage:
+  assumes contf: "continuous_on S f" and "closed S" "closed T"
+  shows "closed (S \<inter> f -` T)"
+proof-
+  obtain U where "closed U" "(S \<inter> f -` T) = S \<inter> U"
+    using continuous_closedin_preimage[OF contf \<open>closed T\<close>]
+    unfolding closedin_closed by auto
+  then show ?thesis using closed_Int[of S U, OF \<open>closed S\<close>] by auto
+qed
+
+lemma continuous_open_vimage: "open S \<Longrightarrow> (\<And>x. continuous (at x) f) \<Longrightarrow> open (f -` S)"
+  by (metis continuous_on_eq_continuous_within open_vimage) 
+ 
+lemma continuous_closed_vimage: "closed S \<Longrightarrow> (\<And>x. continuous (at x) f) \<Longrightarrow> closed (f -` S)"
+  by (simp add: closed_vimage continuous_on_eq_continuous_within)
+
+lemma interior_image_subset:
+  assumes "inj f" "\<And>x. continuous (at x) f"
+  shows "interior (f ` S) \<subseteq> f ` (interior S)"
+proof
+  fix x assume "x \<in> interior (f ` S)"
+  then obtain T where as: "open T" "x \<in> T" "T \<subseteq> f ` S" ..
+  then have "x \<in> f ` S" by auto
+  then obtain y where y: "y \<in> S" "x = f y" by auto
+  have "open (f -` T)"
+    using assms \<open>open T\<close> by (simp add: continuous_at_imp_continuous_on open_vimage)
+  moreover have "y \<in> vimage f T"
+    using \<open>x = f y\<close> \<open>x \<in> T\<close> by simp
+  moreover have "vimage f T \<subseteq> S"
+    using \<open>T \<subseteq> image f S\<close> \<open>inj f\<close> unfolding inj_on_def subset_eq by auto
+  ultimately have "y \<in> interior S" ..
+  with \<open>x = f y\<close> show "x \<in> f ` interior S" ..
+qed
+
+subsection%unimportant \<open>Topological properties of linear functions\<close>
+
+lemma linear_lim_0:
+  assumes "bounded_linear f"
+  shows "(f \<longlongrightarrow> 0) (at (0))"
+proof -
+  interpret f: bounded_linear f by fact
+  have "(f \<longlongrightarrow> f 0) (at 0)"
+    using tendsto_ident_at by (rule f.tendsto)
+  then show ?thesis unfolding f.zero .
+qed
+
+lemma linear_continuous_at:
+  assumes "bounded_linear f"
+  shows "continuous (at a) f"
+  unfolding continuous_at using assms
+  apply (rule bounded_linear.tendsto)
+  apply (rule tendsto_ident_at)
+  done
+
+lemma linear_continuous_within:
+  "bounded_linear f \<Longrightarrow> continuous (at x within s) f"
+  using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto
+
+lemma linear_continuous_on:
+  "bounded_linear f \<Longrightarrow> continuous_on s f"
+  using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto
+
+end
\ No newline at end of file
--- a/src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Equivalence_Lebesgue_Henstock_Integration.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -1195,8 +1195,8 @@
   proof (rule starlike_negligible [OF \<open>closed S\<close>, of a])
     fix c x
     assume cx: "a + c *\<^sub>R x \<in> S" "0 \<le> c" "a + x \<in> S"
-    with star have "~ (c < 1)" by auto
-    moreover have "~ (c > 1)"
+    with star have "\<not> (c < 1)" by auto
+    moreover have "\<not> (c > 1)"
       using star [of "1/c" "c *\<^sub>R x"] cx by force
     ultimately show "c = 1" by arith
   qed
--- a/src/HOL/Analysis/Euclidean_Space.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Euclidean_Space.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -7,9 +7,21 @@
 
 theory Euclidean_Space
 imports
-  L2_Norm Product_Vector
+  L2_Norm
+  Inner_Product
+  Product_Vector
 begin
 
+
+subsection%unimportant \<open>Interlude: Some properties of real sets\<close>
+
+lemma seq_mono_lemma:
+  assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n"
+    and "\<forall>n \<ge> m. e n \<le> e m"
+  shows "\<forall>n \<ge> m. d n < e m"
+  using assms by force
+
+
 subsection \<open>Type class of Euclidean spaces\<close>
 
 class euclidean_space = real_inner +
@@ -261,6 +273,44 @@
 
 subsubsection%unimportant \<open>Type @{typ "'a \<times> 'b"}\<close>
 
+instantiation prod :: (real_inner, real_inner) real_inner
+begin
+
+definition inner_prod_def:
+  "inner x y = inner (fst x) (fst y) + inner (snd x) (snd y)"
+
+lemma inner_Pair [simp]: "inner (a, b) (c, d) = inner a c + inner b d"
+  unfolding inner_prod_def by simp
+
+instance
+proof
+  fix r :: real
+  fix x y z :: "'a::real_inner \<times> 'b::real_inner"
+  show "inner x y = inner y x"
+    unfolding inner_prod_def
+    by (simp add: inner_commute)
+  show "inner (x + y) z = inner x z + inner y z"
+    unfolding inner_prod_def
+    by (simp add: inner_add_left)
+  show "inner (scaleR r x) y = r * inner x y"
+    unfolding inner_prod_def
+    by (simp add: distrib_left)
+  show "0 \<le> inner x x"
+    unfolding inner_prod_def
+    by (intro add_nonneg_nonneg inner_ge_zero)
+  show "inner x x = 0 \<longleftrightarrow> x = 0"
+    unfolding inner_prod_def prod_eq_iff
+    by (simp add: add_nonneg_eq_0_iff)
+  show "norm x = sqrt (inner x x)"
+    unfolding norm_prod_def inner_prod_def
+    by (simp add: power2_norm_eq_inner)
+qed
+
+end
+
+lemma inner_Pair_0: "inner x (0, b) = inner (snd x) b" "inner x (a, 0) = inner (fst x) a"
+    by (cases x, simp)+
+
 instantiation prod :: (euclidean_space, euclidean_space) euclidean_space
 begin
 
--- a/src/HOL/Analysis/Extended_Real_Limits.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Extended_Real_Limits.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -5,7 +5,7 @@
     Author:     Bogdan Grechuk, University of Edinburgh
 *)
 
-section%important \<open>Limits on the Extended real number line\<close> (* TO FIX: perhaps put all Nonstandard Analysis related
+section%important \<open>Limits on the Extended Real Number Line\<close> (* TO FIX: perhaps put all Nonstandard Analysis related
 topics together? *)
 
 theory Extended_Real_Limits
--- a/src/HOL/Analysis/FPS_Convergence.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/FPS_Convergence.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -5,7 +5,8 @@
   Connection of formal power series and actual convergent power series on Banach spaces
   (most notably the complex numbers).
 *)
-section \<open>Convergence of formal power series\<close>
+section \<open>Convergence of Formal Power Series\<close>
+
 theory FPS_Convergence
 imports
   Conformal_Mappings
--- a/src/HOL/Analysis/Fashoda_Theorem.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Fashoda_Theorem.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,7 +2,7 @@
     Author:     Robert Himmelmann, TU Muenchen (translation from HOL light)
 *)
 
-section%important \<open>Fashoda meet theorem\<close>
+section%important \<open>Fashoda Meet Theorem\<close>
 
 theory Fashoda_Theorem
 imports Brouwer_Fixpoint Path_Connected Cartesian_Euclidean_Space
--- a/src/HOL/Analysis/Finite_Cartesian_Product.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Finite_Cartesian_Product.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,7 +2,7 @@
     Author:     Amine Chaieb, University of Cambridge
 *)
 
-section%important \<open>Definition of finite Cartesian product types\<close>
+section%important \<open>Definition of Finite Cartesian Product Type\<close>
 
 theory Finite_Cartesian_Product
 imports
--- a/src/HOL/Analysis/Finite_Product_Measure.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Finite_Product_Measure.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,7 +2,7 @@
     Author:     Johannes Hölzl, TU München
 *)
 
-section%important \<open>Finite product measures\<close>
+section%important \<open>Finite Product Measure\<close>
 
 theory Finite_Product_Measure
 imports Binary_Product_Measure
--- a/src/HOL/Analysis/Function_Topology.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Function_Topology.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -7,7 +7,7 @@
 begin
 
 
-section%important \<open>Product topology\<close>
+section%important \<open>Product Topology\<close>
 
 text \<open>We want to define the product topology.
 
--- a/src/HOL/Analysis/Further_Topology.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Further_Topology.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -366,7 +366,7 @@
       by (meson f nullhomotopic_from_contractible contractible_sphere that)
   next
     case False
-    with \<open>~ s \<le> 0\<close> have "r > 0" "s > 0" by auto
+    with \<open>\<not> s \<le> 0\<close> have "r > 0" "s > 0" by auto
     show ?thesis
       apply (rule inessential_spheremap_lowdim_gen [of "cball a r" "cball b s" f])
       using  \<open>0 < r\<close> \<open>0 < s\<close> assms(1)
@@ -414,7 +414,7 @@
   assumes fin: "finite \<F>"
       and "\<And>S. S \<in> \<F> \<Longrightarrow> \<exists>g. continuous_on S g \<and> g ` S \<subseteq> T \<and> (\<forall>x \<in> S \<inter> K. g x = h x)"
       and "\<And>S. S \<in> \<F> \<Longrightarrow> closed S"
-      and K: "\<And>X Y. \<lbrakk>X \<in> \<F>; Y \<in> \<F>; ~ X \<subseteq> Y; ~ Y \<subseteq> X\<rbrakk> \<Longrightarrow> X \<inter> Y \<subseteq> K"
+      and K: "\<And>X Y. \<lbrakk>X \<in> \<F>; Y \<in> \<F>; \<not> X \<subseteq> Y; \<not> Y \<subseteq> X\<rbrakk> \<Longrightarrow> X \<inter> Y \<subseteq> K"
     shows "\<exists>g. continuous_on (\<Union>\<F>) g \<and> g ` (\<Union>\<F>) \<subseteq> T \<and> (\<forall>x \<in> \<Union>\<F> \<inter> K. g x = h x)"
 apply (simp add: Union_maximal_sets [OF fin, symmetric])
 apply (rule extending_maps_Union_aux)
@@ -567,7 +567,7 @@
     have clo: "closed S" if "S \<in> \<G> \<union> ?Faces" for S
       using that \<open>\<G> \<subseteq> \<F>\<close> face_of_polytope_polytope poly polytope_imp_closed by blast
     have K: "X \<inter> Y \<subseteq> \<Union>(\<G> \<union> {D. \<exists>C\<in>\<F>. D face_of C \<and> aff_dim D < int p})"
-                if "X \<in> \<G> \<union> ?Faces" "Y \<in> \<G> \<union> ?Faces" "~ Y \<subseteq> X" for X Y
+                if "X \<in> \<G> \<union> ?Faces" "Y \<in> \<G> \<union> ?Faces" "\<not> Y \<subseteq> X" for X Y
     proof -
       have ff: "X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
         if XY: "X face_of D" "Y face_of E" and DE: "D \<in> \<F>" "E \<in> \<F>" for D E
@@ -669,7 +669,7 @@
 assumes "finite \<F>"
     and \<F>: "\<And>X. X \<in> \<F> \<Longrightarrow> \<exists>a g. a \<notin> U \<and> continuous_on (X - {a}) g \<and> g ` (X - {a}) \<subseteq> T \<and> (\<forall>x \<in> X \<inter> K. g x = h x)"
     and clo: "\<And>X. X \<in> \<F> \<Longrightarrow> closed X"
-    and K: "\<And>X Y. \<lbrakk>X \<in> \<F>; Y \<in> \<F>; ~(X \<subseteq> Y); ~(Y \<subseteq> X)\<rbrakk> \<Longrightarrow> X \<inter> Y \<subseteq> K"
+    and K: "\<And>X Y. \<lbrakk>X \<in> \<F>; Y \<in> \<F>; \<not> X \<subseteq> Y; \<not> Y \<subseteq> X\<rbrakk> \<Longrightarrow> X \<inter> Y \<subseteq> K"
   obtains C g where "finite C" "disjnt C U" "card C \<le> card \<F>" "continuous_on (\<Union>\<F> - C) g"
                     "g ` (\<Union>\<F> - C) \<subseteq> T"
                     "\<And>x. x \<in> (\<Union>\<F> - C) \<inter> K \<Longrightarrow> g x = h x"
@@ -1188,8 +1188,8 @@
     then have "ball a d \<inter> U \<subseteq> C"
       by auto
     obtain h k where homhk: "homeomorphism (S \<union> C) (S \<union> C) h k"
-                 and subC: "{x. (~ (h x = x \<and> k x = x))} \<subseteq> C"
-                 and bou: "bounded {x. (~ (h x = x \<and> k x = x))}"
+                 and subC: "{x. (\<not> (h x = x \<and> k x = x))} \<subseteq> C"
+                 and bou: "bounded {x. (\<not> (h x = x \<and> k x = x))}"
                  and hin: "\<And>x. x \<in> C \<inter> K \<Longrightarrow> h x \<in> ball a d \<inter> U"
     proof (rule homeomorphism_grouping_points_exists_gen [of C "ball a d \<inter> U" "C \<inter> K" "S \<union> C"])
       show "openin (subtopology euclidean C) (ball a d \<inter> U)"
@@ -1539,7 +1539,7 @@
     by (simp add: assms compact_imp_bounded)
   then obtain b where b: "S \<subseteq> cbox (-b) b"
     using bounded_subset_cbox_symmetric by blast
-  define LU where "LU \<equiv> L \<union> (\<Union> {C \<in> components (T - S). ~bounded C} - cbox (-(b+One)) (b+One))"
+  define LU where "LU \<equiv> L \<union> (\<Union> {C \<in> components (T - S). \<not>bounded C} - cbox (-(b+One)) (b+One))"
   obtain K g where "finite K" "K \<subseteq> LU" "K \<subseteq> T" "disjnt K S"
                and contg: "continuous_on (T - K) g"
                and gim: "g ` (T - K) \<subseteq> rel_frontier U"
@@ -1699,7 +1699,7 @@
 theorem%important Borsuk_separation_theorem_gen:
   fixes S :: "'a::euclidean_space set"
   assumes "compact S"
-    shows "(\<forall>c \<in> components(- S). ~bounded c) \<longleftrightarrow>
+    shows "(\<forall>c \<in> components(- S). \<not>bounded c) \<longleftrightarrow>
            (\<forall>f. continuous_on S f \<and> f ` S \<subseteq> sphere (0::'a) 1
                 \<longrightarrow> (\<exists>c. homotopic_with (\<lambda>x. True) S (sphere 0 1) f (\<lambda>x. c)))"
        (is "?lhs = ?rhs")
@@ -5204,7 +5204,7 @@
 proof (rule ccontr)
   let ?S = "S \<union> \<Union>{C \<in> components(- (S \<union> T)). frontier C \<subseteq> S}"
   let ?T = "T \<union> \<Union>{C \<in> components(- (S \<union> T)). frontier C \<subseteq> T}"
-  assume "~ thesis"
+  assume "\<not> thesis"
   with that have *: "frontier C \<inter> S = {} \<or> frontier C \<inter> T = {}"
             if C: "C \<in> components (- (S \<union> T))" "C \<noteq> {}" for C
     using C by blast
--- a/src/HOL/Analysis/Great_Picard.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Great_Picard.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -874,7 +874,7 @@
            have le_er: "cmod (\<F> n x / (x - y) - \<F> n x / (x - z)) \<le> e / r"
                 if "cmod (x - z) = r/3 + r/3" for x
            proof -
-             have "~ (cmod (x - y) < r/3)"
+             have "\<not> (cmod (x - y) < r/3)"
                using y_near_z(1) that \<open>M > 0\<close> \<open>r > 0\<close>
                by (metis (full_types) norm_diff_triangle_less norm_minus_commute order_less_irrefl)
              then have r4_le_xy: "r/4 \<le> cmod (x - y)"
@@ -1000,7 +1000,7 @@
       and holf: "\<And>n::nat. \<F> n holomorphic_on S"
       and holg: "g holomorphic_on S"
       and ul_g: "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K \<F> g sequentially"
-      and nonconst: "~ g constant_on S"
+      and nonconst: "\<not> g constant_on S"
       and nz: "\<And>n z. z \<in> S \<Longrightarrow> \<F> n z \<noteq> 0"
       and "z0 \<in> S"
       shows "g z0 \<noteq> 0"
@@ -1164,7 +1164,7 @@
       and holf: "\<And>n::nat. \<F> n holomorphic_on S"
       and holg: "g holomorphic_on S"
       and ul_g: "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> uniform_limit K \<F> g sequentially"
-      and nonconst: "~ g constant_on S"
+      and nonconst: "\<not> g constant_on S"
       and inj: "\<And>n. inj_on (\<F> n) S"
     shows "inj_on g S"
 proof%unimportant -
@@ -1309,7 +1309,7 @@
       unfolding open_subopen [of U] by (auto simp: U_def)
     fix v
     assume v: "v islimpt U" "v \<in> S"
-    have "~ (\<forall>r>0. \<exists>h\<in>Y. r < cmod (h v))"
+    have "\<not> (\<forall>r>0. \<exists>h\<in>Y. r < cmod (h v))"
     proof
       assume "\<forall>r>0. \<exists>h\<in>Y. r < cmod (h v)"
       then have "\<forall>n. \<exists>h\<in>Y. Suc n < cmod (h v)"
--- a/src/HOL/Analysis/Henstock_Kurzweil_Integration.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Henstock_Kurzweil_Integration.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -3,11 +3,12 @@
                 Huge cleanup by LCP
 *)
 
-section \<open>Henstock-Kurzweil gauge integration in many dimensions\<close>
+section \<open>Henstock-Kurzweil Gauge Integration in Many Dimensions\<close>
 
 theory Henstock_Kurzweil_Integration
 imports
-  Lebesgue_Measure Tagged_Division
+  Lebesgue_Measure
+  Tagged_Division
 begin
 
 lemma norm_diff2: "\<lbrakk>y = y1 + y2; x = x1 + x2; e = e1 + e2; norm(y1 - x1) \<le> e1; norm(y2 - x2) \<le> e2\<rbrakk>
@@ -79,7 +80,7 @@
 lemma content_pos_le [iff]: "0 \<le> content X"
   by simp
 
-corollary content_nonneg [simp]: "~ content (cbox a b) < 0"
+corollary content_nonneg [simp]: "\<not> content (cbox a b) < 0"
   using not_le by blast
 
 lemma content_pos_lt: "\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i \<Longrightarrow> 0 < content (cbox a b)"
@@ -287,12 +288,12 @@
 definition integrable_on (infixr "integrable'_on" 46)
   where "f integrable_on i \<longleftrightarrow> (\<exists>y. (f has_integral y) i)"
 
-definition "integral i f = (SOME y. (f has_integral y) i \<or> ~ f integrable_on i \<and> y=0)"
+definition "integral i f = (SOME y. (f has_integral y) i \<or> \<not> f integrable_on i \<and> y=0)"
 
 lemma integrable_integral[intro]: "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
   unfolding integrable_on_def integral_def by (metis (mono_tags, lifting) someI_ex)
 
-lemma not_integrable_integral: "~ f integrable_on i \<Longrightarrow> integral i f = 0"
+lemma not_integrable_integral: "\<not> f integrable_on i \<Longrightarrow> integral i f = 0"
   unfolding integrable_on_def integral_def by blast
 
 lemma has_integral_integrable[dest]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
@@ -356,7 +357,7 @@
 lemma has_integral_iff: "(f has_integral i) S \<longleftrightarrow> (f integrable_on S \<and> integral S f = i)"
   by blast
 
-lemma eq_integralD: "integral k f = y \<Longrightarrow> (f has_integral y) k \<or> ~ f integrable_on k \<and> y=0"
+lemma eq_integralD: "integral k f = y \<Longrightarrow> (f has_integral y) k \<or> \<not> f integrable_on k \<and> y=0"
   unfolding integral_def integrable_on_def
   apply (erule subst)
   apply (rule someI_ex)
@@ -498,7 +499,7 @@
   case True then show ?thesis
     by (force intro: has_integral_mult_left)
 next
-  case False then have "~ (\<lambda>x. f x * c) integrable_on S"
+  case False then have "\<not> (\<lambda>x. f x * c) integrable_on S"
     using has_integral_mult_left [of "(\<lambda>x. f x * c)" _ S "inverse c"]
     by (auto simp add: mult.assoc)
   with False show ?thesis by (simp add: not_integrable_integral)
@@ -615,7 +616,7 @@
   case True with has_integral_cmul integrable_integral show ?thesis
     by fastforce
 next
-  case False then have "~ (\<lambda>x. c *\<^sub>R f x) integrable_on S"
+  case False then have "\<not> (\<lambda>x. c *\<^sub>R f x) integrable_on S"
     using has_integral_cmul [of "(\<lambda>x. c *\<^sub>R f x)" _ S "inverse c"] by auto
   with False show ?thesis by (simp add: not_integrable_integral)
 qed
@@ -630,7 +631,7 @@
   case True then show ?thesis
     by (simp add: has_integral_neg integrable_integral integral_unique)
 next
-  case False then have "~ (\<lambda>x. - f x) integrable_on S"
+  case False then have "\<not> (\<lambda>x. - f x) integrable_on S"
     using has_integral_neg [of "(\<lambda>x. - f x)" _ S ] by auto
   with False show ?thesis by (simp add: not_integrable_integral)
 qed
@@ -5924,7 +5925,7 @@
       next
         have "norm ((\<Sum>(x,K)\<in>\<D>. content K *\<^sub>R f (m x) x) - (\<Sum>(x,K)\<in>\<D>. integral K (f (m x))))
               \<le> norm (\<Sum>j = 0..s. \<Sum>(x,K)\<in>{xk \<in> \<D>. m (fst xk) = j}. content K *\<^sub>R f (m x) x - integral K (f (m x)))"
-          apply (subst sum_group)
+          apply (subst sum.group)
           using s by (auto simp: sum_subtractf split_def p'(1))
         also have "\<dots> < e/2"
         proof -
--- a/src/HOL/Analysis/Homeomorphism.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Homeomorphism.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -201,7 +201,7 @@
 lemma%unimportant segment_to_rel_frontier:
   fixes x :: "'a::euclidean_space"
   assumes S: "convex S" "bounded S" and x: "x \<in> rel_interior S"
-      and y: "y \<in> S" and xy: "~(x = y \<and> S = {x})"
+      and y: "y \<in> S" and xy: "\<not>(x = y \<and> S = {x})"
   obtains z where "z \<in> rel_frontier S" "y \<in> closed_segment x z"
                   "open_segment x z \<subseteq> rel_interior S"
 proof (cases "x=y")
--- a/src/HOL/Analysis/Improper_Integral.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Improper_Integral.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -924,7 +924,7 @@
           moreover have "norm (\<Sum>(x,K) \<in> T'. ?CI K h x - ?CI K f x)
                 \<le> 2*\<epsilon>/3"
           proof -
-            define T'' where "T'' \<equiv> {(x,K) \<in> T'. ~ (K \<subseteq> {x. x \<bullet> i \<le> c})}"
+            define T'' where "T'' \<equiv> {(x,K) \<in> T'. \<not> (K \<subseteq> {x. x \<bullet> i \<le> c})}"
             then have "T'' \<subseteq> T'"
               by auto
             then have "finite T''"
--- a/src/HOL/Analysis/Infinite_Set_Sum.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Infinite_Set_Sum.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -4,7 +4,8 @@
 
   A theory of sums over possible infinite sets. (Only works for absolute summability)
 *)
-section \<open>Sums over infinite sets\<close>
+section \<open>Sums over Infinite Sets\<close>
+
 theory Infinite_Set_Sum
   imports Set_Integral
 begin
--- a/src/HOL/Analysis/Inner_Product.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Inner_Product.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -177,11 +177,6 @@
   using isCont_power[OF continuous_ident, of x, unfolded isCont_def LIM_eq, rule_format, of e 2]
   by (force simp add: power2_eq_square)
 
-lemma norm_triangle_sub:
-  fixes x y :: "'a::real_normed_vector"
-  shows "norm x \<le> norm y + norm (x - y)"
-  using norm_triangle_ineq[of "y" "x - y"] by (simp add: field_simps)
-
 lemma norm_le: "norm x \<le> norm y \<longleftrightarrow> inner x x \<le> inner y y"
   by (simp add: norm_eq_sqrt_inner)
 
--- a/src/HOL/Analysis/Jordan_Curve.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Jordan_Curve.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -374,7 +374,7 @@
       qed
       then have "connected_component (- (path_image u \<union> path_image d \<union> path_image g)) x y"
         by (simp add: Un_ac)
-      moreover have "~(connected_component (- (path_image c)) x y)"
+      moreover have "\<not>(connected_component (- (path_image c)) x y)"
         by (metis (no_types, lifting) \<open>\<not> bounded outer\<close> \<open>bounded inner\<close> \<open>x \<in> inner\<close> \<open>y \<in> outer\<close> componentsE connected_component_eq inner mem_Collect_eq outer)
       ultimately show False
         by (auto simp: ud_Un [symmetric] connected_component_def)
--- a/src/HOL/Analysis/Lebesgue_Integral_Substitution.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Lebesgue_Integral_Substitution.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -6,7 +6,7 @@
     This could probably be weakened somehow.
 *)
 
-section \<open>Integration by Substition for the Lebesgue integral\<close>
+section \<open>Integration by Substition for the Lebesgue Integral\<close>
 
 theory Lebesgue_Integral_Substitution
 imports Interval_Integral
--- a/src/HOL/Analysis/Lebesgue_Measure.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Lebesgue_Measure.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -5,10 +5,15 @@
     Author:     Luke Serafin
 *)
 
-section \<open>Lebesgue measure\<close>
+section \<open>Lebesgue Measure\<close>
 
 theory Lebesgue_Measure
-  imports Finite_Product_Measure Bochner_Integration Caratheodory Complete_Measure Summation_Tests Regularity
+imports
+  Finite_Product_Measure
+  Caratheodory
+  Complete_Measure
+  Summation_Tests
+  Regularity
 begin
 
 lemma measure_eqI_lessThan:
--- a/src/HOL/Analysis/Linear_Algebra.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Linear_Algebra.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,7 +2,7 @@
     Author:     Amine Chaieb, University of Cambridge
 *)
 
-section \<open>Elementary linear algebra on Euclidean spaces\<close>
+section \<open>Elementary Linear Algebra on Euclidean Spaces\<close>
 
 theory Linear_Algebra
 imports
@@ -76,12 +76,6 @@
   using dist_triangle_half_l[OF assms[unfolded dist_norm[symmetric]]]
   unfolding dist_norm[symmetric] .
 
-lemma norm_triangle_le: "norm x + norm y \<le> e \<Longrightarrow> norm (x + y) \<le> e"
-  by (rule norm_triangle_ineq [THEN order_trans])
-
-lemma norm_triangle_lt: "norm x + norm y < e \<Longrightarrow> norm (x + y) < e"
-  by (rule norm_triangle_ineq [THEN le_less_trans])
-
 lemma abs_triangle_half_r:
   fixes y :: "'a::linordered_field"
   shows "abs (y - x1) < e / 2 \<Longrightarrow> abs (y - x2) < e / 2 \<Longrightarrow> abs (x1 - x2) < e"
@@ -99,19 +93,6 @@
     and "finite S \<Longrightarrow> sum f (insert x S) = (if x \<in> S then sum f S else f x + sum f S)"
   by (auto simp add: insert_absorb)
 
-lemma sum_norm_bound:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-  assumes K: "\<And>x. x \<in> S \<Longrightarrow> norm (f x) \<le> K"
-  shows "norm (sum f S) \<le> of_nat (card S)*K"
-  using sum_norm_le[OF K] sum_constant[symmetric]
-  by simp
-
-lemma sum_group:
-  assumes fS: "finite S" and fT: "finite T" and fST: "f ` S \<subseteq> T"
-  shows "sum (\<lambda>y. sum g {x. x \<in> S \<and> f x = y}) T = sum g S"
-  unfolding sum_image_gen[OF fS, of g f]
-  by (auto intro: sum.neutral sum.mono_neutral_right[OF fT fST])
-
 lemma vector_eq_ldot: "(\<forall>x. x \<bullet> y = x \<bullet> z) \<longleftrightarrow> y = z"
 proof
   assume "\<forall>x. x \<bullet> y = x \<bullet> z"
@@ -329,51 +310,6 @@
   by (rule adjoint_unique, simp add: adjoint_clauses [OF lf])
 
 
-subsection%unimportant \<open>Interlude: Some properties of real sets\<close>
-
-lemma seq_mono_lemma:
-  assumes "\<forall>(n::nat) \<ge> m. (d n :: real) < e n"
-    and "\<forall>n \<ge> m. e n \<le> e m"
-  shows "\<forall>n \<ge> m. d n < e m"
-  using assms by force
-
-lemma infinite_enumerate:
-  assumes fS: "infinite S"
-  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (\<forall>n. r n \<in> S)"
-  unfolding strict_mono_def
-  using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
-
-lemma approachable_lt_le: "(\<exists>(d::real) > 0. \<forall>x. f x < d \<longrightarrow> P x) \<longleftrightarrow> (\<exists>d>0. \<forall>x. f x \<le> d \<longrightarrow> P x)"
-  apply auto
-  apply (rule_tac x="d/2" in exI)
-  apply auto
-  done
-
-lemma approachable_lt_le2:  \<comment> \<open>like the above, but pushes aside an extra formula\<close>
-    "(\<exists>(d::real) > 0. \<forall>x. Q x \<longrightarrow> f x < d \<longrightarrow> P x) \<longleftrightarrow> (\<exists>d>0. \<forall>x. f x \<le> d \<longrightarrow> Q x \<longrightarrow> P x)"
-  apply auto
-  apply (rule_tac x="d/2" in exI, auto)
-  done
-
-lemma triangle_lemma:
-  fixes x y z :: real
-  assumes x: "0 \<le> x"
-    and y: "0 \<le> y"
-    and z: "0 \<le> z"
-    and xy: "x\<^sup>2 \<le> y\<^sup>2 + z\<^sup>2"
-  shows "x \<le> y + z"
-proof -
-  have "y\<^sup>2 + z\<^sup>2 \<le> y\<^sup>2 + 2 * y * z + z\<^sup>2"
-    using z y by simp
-  with xy have th: "x\<^sup>2 \<le> (y + z)\<^sup>2"
-    by (simp add: power2_eq_square field_simps)
-  from y z have yz: "y + z \<ge> 0"
-    by arith
-  from power2_le_imp_le[OF th yz] show ?thesis .
-qed
-
-
-
 subsection \<open>Archimedean properties and useful consequences\<close>
 
 text\<open>Bernoulli's inequality\<close>
--- a/src/HOL/Analysis/Lipschitz.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Lipschitz.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,9 +2,10 @@
     Author:     Sébastien Gouëzel   sebastien.gouezel@univ-rennes1.fr
     Author:     Fabian Immler, TU München
 *)
-section \<open>Lipschitz continuity\<close>
+section \<open>Lipschitz Continuity\<close>
+
 theory Lipschitz
-  imports Borel_Space
+imports Borel_Space
 begin
 
 definition%important lipschitz_on
--- a/src/HOL/Analysis/Measurable.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Measurable.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -1,7 +1,7 @@
 (*  Title:      HOL/Analysis/Measurable.thy
     Author:     Johannes Hölzl <hoelzl@in.tum.de>
 *)
-section \<open>Measurability prover\<close>
+section \<open>Measurability Prover\<close>
 theory Measurable
   imports
     Sigma_Algebra
--- a/src/HOL/Analysis/Measure_Space.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Measure_Space.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -4,7 +4,7 @@
     Author:     Armin Heller, TU München
 *)
 
-section \<open>Measure spaces and their properties\<close>
+section \<open>Measure Spaces\<close>
 
 theory Measure_Space
 imports
--- a/src/HOL/Analysis/Norm_Arith.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Norm_Arith.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,7 +2,7 @@
     Author:     Amine Chaieb, University of Cambridge
 *)
 
-section \<open>General linear decision procedure for normed spaces\<close>
+section \<open>Linear Decision Procedure for Normed Spaces\<close>
 
 theory Norm_Arith
 imports "HOL-Library.Sum_of_Squares"
--- a/src/HOL/Analysis/Operator_Norm.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Operator_Norm.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -3,7 +3,7 @@
     Author:     Brian Huffman
 *)
 
-section%important \<open>Operator Norm\<close>
+section \<open>Operator Norm\<close>
 
 theory Operator_Norm
 imports Complex_Main
@@ -11,14 +11,15 @@
 
 text \<open>This formulation yields zero if \<open>'a\<close> is the trivial vector space.\<close>
 
-definition%important onorm :: "('a::real_normed_vector \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> real"
-  where "onorm f = (SUP x. norm (f x) / norm x)"
+definition%important
+onorm :: "('a::real_normed_vector \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> real" where
+"onorm f = (SUP x. norm (f x) / norm x)"
 
-lemma%important onorm_bound:
+proposition onorm_bound:
   assumes "0 \<le> b" and "\<And>x. norm (f x) \<le> b * norm x"
   shows "onorm f \<le> b"
   unfolding onorm_def
-proof%unimportant (rule cSUP_least)
+proof (rule cSUP_least)
   fix x
   show "norm (f x) / norm x \<le> b"
     using assms by (cases "x = 0") (simp_all add: pos_divide_le_eq)
@@ -26,11 +27,11 @@
 
 text \<open>In non-trivial vector spaces, the first assumption is redundant.\<close>
 
-lemma%important onorm_le:
+lemma onorm_le:
   fixes f :: "'a::{real_normed_vector, perfect_space} \<Rightarrow> 'b::real_normed_vector"
   assumes "\<And>x. norm (f x) \<le> b * norm x"
   shows "onorm f \<le> b"
-proof%unimportant (rule onorm_bound [OF _ assms])
+proof (rule onorm_bound [OF _ assms])
   have "{0::'a} \<noteq> UNIV" by (metis not_open_singleton open_UNIV)
   then obtain a :: 'a where "a \<noteq> 0" by fast
   have "0 \<le> b * norm a"
@@ -39,10 +40,10 @@
     by (simp add: zero_le_mult_iff)
 qed
 
-lemma%important le_onorm:
+lemma le_onorm:
   assumes "bounded_linear f"
   shows "norm (f x) / norm x \<le> onorm f"
-proof%unimportant -
+proof -
   interpret f: bounded_linear f by fact
   obtain b where "0 \<le> b" and "\<forall>x. norm (f x) \<le> norm x * b"
     using f.nonneg_bounded by auto
@@ -55,10 +56,10 @@
     unfolding onorm_def by (rule cSUP_upper)
 qed
 
-lemma%important onorm:
+lemma onorm:
   assumes "bounded_linear f"
   shows "norm (f x) \<le> onorm f * norm x"
-proof%unimportant -
+proof -
   interpret f: bounded_linear f by fact
   show ?thesis
   proof (cases)
@@ -73,12 +74,12 @@
   qed
 qed
 
-lemma%unimportant onorm_pos_le:
+lemma onorm_pos_le:
   assumes f: "bounded_linear f"
   shows "0 \<le> onorm f"
   using le_onorm [OF f, where x=0] by simp
 
-lemma%unimportant onorm_zero: "onorm (\<lambda>x. 0) = 0"
+lemma onorm_zero: "onorm (\<lambda>x. 0) = 0"
 proof (rule order_antisym)
   show "onorm (\<lambda>x. 0) \<le> 0"
     by (simp add: onorm_bound)
@@ -86,20 +87,20 @@
     using bounded_linear_zero by (rule onorm_pos_le)
 qed
 
-lemma%unimportant onorm_eq_0:
+lemma onorm_eq_0:
   assumes f: "bounded_linear f"
   shows "onorm f = 0 \<longleftrightarrow> (\<forall>x. f x = 0)"
   using onorm [OF f] by (auto simp: fun_eq_iff [symmetric] onorm_zero)
 
-lemma%unimportant onorm_pos_lt:
+lemma onorm_pos_lt:
   assumes f: "bounded_linear f"
   shows "0 < onorm f \<longleftrightarrow> \<not> (\<forall>x. f x = 0)"
   by (simp add: less_le onorm_pos_le [OF f] onorm_eq_0 [OF f])
 
-lemma%unimportant onorm_id_le: "onorm (\<lambda>x. x) \<le> 1"
+lemma onorm_id_le: "onorm (\<lambda>x. x) \<le> 1"
   by (rule onorm_bound) simp_all
 
-lemma%unimportant onorm_id: "onorm (\<lambda>x. x::'a::{real_normed_vector, perfect_space}) = 1"
+lemma onorm_id: "onorm (\<lambda>x. x::'a::{real_normed_vector, perfect_space}) = 1"
 proof (rule antisym[OF onorm_id_le])
   have "{0::'a} \<noteq> UNIV" by (metis not_open_singleton open_UNIV)
   then obtain x :: 'a where "x \<noteq> 0" by fast
@@ -110,11 +111,11 @@
   finally show "1 \<le> onorm (\<lambda>x::'a. x)" .
 qed
 
-lemma%important onorm_compose:
+lemma onorm_compose:
   assumes f: "bounded_linear f"
   assumes g: "bounded_linear g"
   shows "onorm (f \<circ> g) \<le> onorm f * onorm g"
-proof%unimportant (rule onorm_bound)
+proof (rule onorm_bound)
   show "0 \<le> onorm f * onorm g"
     by (intro mult_nonneg_nonneg onorm_pos_le f g)
 next
@@ -127,7 +128,7 @@
     by (simp add: mult.assoc)
 qed
 
-lemma%unimportant onorm_scaleR_lemma:
+lemma onorm_scaleR_lemma:
   assumes f: "bounded_linear f"
   shows "onorm (\<lambda>x. r *\<^sub>R f x) \<le> \<bar>r\<bar> * onorm f"
 proof (rule onorm_bound)
@@ -141,10 +142,10 @@
     by (simp only: norm_scaleR mult.assoc)
 qed
 
-lemma%important onorm_scaleR:
+lemma onorm_scaleR:
   assumes f: "bounded_linear f"
   shows "onorm (\<lambda>x. r *\<^sub>R f x) = \<bar>r\<bar> * onorm f"
-proof%unimportant (cases "r = 0")
+proof (cases "r = 0")
   assume "r \<noteq> 0"
   show ?thesis
   proof (rule order_antisym)
@@ -160,7 +161,7 @@
   qed
 qed (simp add: onorm_zero)
 
-lemma%unimportant onorm_scaleR_left_lemma:
+lemma onorm_scaleR_left_lemma:
   assumes r: "bounded_linear r"
   shows "onorm (\<lambda>x. r x *\<^sub>R f) \<le> onorm r * norm f"
 proof (rule onorm_bound)
@@ -173,10 +174,10 @@
     by (simp add: ac_simps)
 qed (intro mult_nonneg_nonneg norm_ge_zero onorm_pos_le r)
 
-lemma%important onorm_scaleR_left:
+lemma onorm_scaleR_left:
   assumes f: "bounded_linear r"
   shows "onorm (\<lambda>x. r x *\<^sub>R f) = onorm r * norm f"
-proof%unimportant (cases "f = 0")
+proof (cases "f = 0")
   assume "f \<noteq> 0"
   show ?thesis
   proof (rule order_antisym)
@@ -200,15 +201,15 @@
   qed
 qed (simp add: onorm_zero)
 
-lemma%unimportant onorm_neg:
+lemma onorm_neg:
   shows "onorm (\<lambda>x. - f x) = onorm f"
   unfolding onorm_def by simp
 
-lemma%important onorm_triangle:
+lemma onorm_triangle:
   assumes f: "bounded_linear f"
   assumes g: "bounded_linear g"
   shows "onorm (\<lambda>x. f x + g x) \<le> onorm f + onorm g"
-proof%unimportant (rule onorm_bound)
+proof (rule onorm_bound)
   show "0 \<le> onorm f + onorm g"
     by (intro add_nonneg_nonneg onorm_pos_le f g)
 next
@@ -221,26 +222,26 @@
     by (simp only: distrib_right)
 qed
 
-lemma%important onorm_triangle_le:
+lemma onorm_triangle_le:
   assumes "bounded_linear f"
   assumes "bounded_linear g"
   assumes "onorm f + onorm g \<le> e"
   shows "onorm (\<lambda>x. f x + g x) \<le> e"
-  using%unimportant assms by%unimportant (rule onorm_triangle [THEN order_trans])
+  using assms by (rule onorm_triangle [THEN order_trans])
 
-lemma%unimportant onorm_triangle_lt:
+lemma onorm_triangle_lt:
   assumes "bounded_linear f"
   assumes "bounded_linear g"
   assumes "onorm f + onorm g < e"
   shows "onorm (\<lambda>x. f x + g x) < e"
   using assms by (rule onorm_triangle [THEN order_le_less_trans])
 
-lemma%important onorm_sum:
+lemma onorm_sum:
   assumes "finite S"
   assumes "\<And>s. s \<in> S \<Longrightarrow> bounded_linear (f s)"
   shows "onorm (\<lambda>x. sum (\<lambda>s. f s x) S) \<le> sum (\<lambda>s. onorm (f s)) S"
-  using%unimportant assms
-  by%unimportant (induction) (auto simp: onorm_zero intro!: onorm_triangle_le bounded_linear_sum)
+  using assms
+  by (induction) (auto simp: onorm_zero intro!: onorm_triangle_le bounded_linear_sum)
 
 lemmas onorm_sum_le = onorm_sum[THEN order_trans]
 
--- a/src/HOL/Analysis/Ordered_Euclidean_Space.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Ordered_Euclidean_Space.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -145,7 +145,7 @@
 qed
 
 lemma%unimportant (in order) atLeastatMost_empty'[simp]:
-  "(~ a <= b) \<Longrightarrow> {a..b} = {}"
+  "(\<not> a \<le> b) \<Longrightarrow> {a..b} = {}"
   by (auto)
 
 instance real :: ordered_euclidean_space
--- a/src/HOL/Analysis/Path_Connected.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Path_Connected.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,10 +2,10 @@
     Authors:    LC Paulson and Robert Himmelmann (TU Muenchen), based on material from HOL Light
 *)
 
-section \<open>Continuous paths and path-connected sets\<close>
+section \<open>Continuous Paths\<close>
 
 theory Path_Connected
-imports Continuous_Extension Continuum_Not_Denumerable
+  imports Continuous_Extension Continuum_Not_Denumerable
 begin
 
 subsection \<open>Paths and Arcs\<close>
@@ -132,6 +132,7 @@
   using assms inj_on_eq_iff [of f]
   by (auto simp: arc_def inj_on_def path_linear_image_eq)
 
+
 subsection%unimportant\<open>Basic lemmas about paths\<close>
 
 lemma continuous_on_path: "path f \<Longrightarrow> t \<subseteq> {0..1} \<Longrightarrow> continuous_on t f"
@@ -293,6 +294,7 @@
     done
 qed
 
+
 section%unimportant \<open>Path Images\<close>
 
 lemma bounded_path_image: "path g \<Longrightarrow> bounded(path_image g)"
@@ -591,7 +593,7 @@
     using \<open>d1 > 0\<close> \<open>d2 > 0\<close> by (simp add: min_def)
   have dist2: "dist (1 / 2 + min (1 / 2) (min d1 d2) / 4) (1 / 2) < d2"
     using \<open>d1 > 0\<close> \<open>d2 > 0\<close> by (simp add: min_def dist_norm)
-  have [simp]: "~ min (1 / 2) (min d1 d2) \<le> 0"
+  have [simp]: "\<not> min (1 / 2) (min d1 d2) \<le> 0"
     using \<open>d1 > 0\<close> \<open>d2 > 0\<close> by (simp add: min_def)
   have "dist (g2 (min (1 / 2) (min d1 d2) / 2)) (g1 1) < e/2"
        "dist (g2 (min (1 / 2) (min d1 d2) / 2)) (g2 0) < e/2"
@@ -670,7 +672,7 @@
   have False if "g1 0 = g2 u" "0 \<le> u" "u \<le> 1" for u
     using * [of 0 "(u + 1) / 2"] that assms arc_distinct_ends [OF \<open>?lhs\<close>]
     by (auto simp: joinpaths_def pathstart_def pathfinish_def split_ifs divide_simps)
-  then have n1: "~ (pathstart g1 \<in> path_image g2)"
+  then have n1: "pathstart g1 \<notin> path_image g2"
     unfolding pathstart_def path_image_def
     using atLeastAtMost_iff by blast
   show ?rhs using \<open>?lhs\<close>
@@ -757,7 +759,7 @@
 by (simp add: path_image_join sup_commute)
 
 
-section\<open>Choosing a subpath of an existing path\<close>
+subsection\<open>Subpath\<close>
 
 definition%important subpath :: "real \<Rightarrow> real \<Rightarrow> (real \<Rightarrow> 'a) \<Rightarrow> real \<Rightarrow> 'a::real_normed_vector"
   where "subpath a b g \<equiv> \<lambda>x. g((b - a) * x + a)"
@@ -937,6 +939,7 @@
 lemma join_subpaths_middle: "subpath (0) ((1 / 2)) p +++ subpath ((1 / 2)) 1 p = p"
   by (rule ext) (simp add: joinpaths_def subpath_def divide_simps)
 
+
 subsection%unimportant\<open>There is a subpath to the frontier\<close>
 
 lemma subpath_to_frontier_explicit:
@@ -1053,7 +1056,8 @@
     done
 qed
 
-subsection \<open>shiftpath: Reparametrizing a closed curve to start at some chosen point\<close>
+
+subsection \<open>Shift Path to Start at Some Given Point\<close>
 
 definition%important shiftpath :: "real \<Rightarrow> (real \<Rightarrow> 'a::topological_space) \<Rightarrow> real \<Rightarrow> 'a"
   where "shiftpath a f = (\<lambda>x. if (a + x) \<le> 1 then f (a + x) else f (a + x - 1))"
@@ -1164,7 +1168,8 @@
     by (force split: if_split_asm dest!: *)
 qed
 
-subsection \<open>Special case of straight-line paths\<close>
+
+subsection \<open>Straight-Line Paths\<close>
 
 definition%important linepath :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> real \<Rightarrow> 'a"
   where "linepath a b = (\<lambda>x. (1 - x) *\<^sub>R a + x *\<^sub>R b)"
@@ -1385,6 +1390,7 @@
     by (force simp: inj_on_def)
 qed
 
+
 subsection%unimportant \<open>Bounding a point away from a path\<close>
 
 lemma not_on_path_ball:
@@ -1415,16 +1421,17 @@
     by (rule_tac x="e/2" in exI) auto
 qed
 
-
-section \<open>Path component, considered as a "joinability" relation\<close>
-
-text \<open>(by Tom Hales)\<close>
+section "Path-Connectedness" (* TODO: separate theory? *)
+
+subsection \<open>Path component\<close>
+
+text \<open>Original formalization by Tom Hales\<close>
 
 definition%important "path_component s x y \<longleftrightarrow>
   (\<exists>g. path g \<and> path_image g \<subseteq> s \<and> pathstart g = x \<and> pathfinish g = y)"
 
 abbreviation%important
-   "path_component_set s x \<equiv> Collect (path_component s x)"
+  "path_component_set s x \<equiv> Collect (path_component s x)"
 
 lemmas path_defs = path_def pathstart_def pathfinish_def path_image_def path_component_def
 
@@ -1472,7 +1479,6 @@
   unfolding path_component_def
   by (rule_tac x="linepath a b" in exI, auto)
 
-
 subsubsection%unimportant \<open>Path components as sets\<close>
 
 lemma path_component_set:
@@ -1495,6 +1501,7 @@
    "y \<in> path_component_set s x \<Longrightarrow> path_component_set s y = path_component_set s x"
 by (metis (no_types, lifting) Collect_cong mem_Collect_eq path_component_sym path_component_trans)
 
+
 subsection \<open>Path connectedness of a space\<close>
 
 definition%important "path_connected s \<longleftrightarrow>
@@ -1804,6 +1811,7 @@
     using path_component_eq_empty by auto
 qed
 
+
 subsection%unimportant\<open>Lemmas about path-connectedness\<close>
 
 lemma path_connected_linear_image:
@@ -2442,7 +2450,7 @@
 lemma cobounded_unbounded_component:
     fixes s :: "'a :: euclidean_space set"
     assumes "bounded (-s)"
-      shows "\<exists>x. x \<in> s \<and> ~ bounded (connected_component_set s x)"
+      shows "\<exists>x. x \<in> s \<and> \<not> bounded (connected_component_set s x)"
 proof -
   obtain i::'a where i: "i \<in> Basis"
     using nonempty_Basis by blast
@@ -2450,7 +2458,7 @@
     using bounded_subset_ballD [OF assms, of 0] by auto
   then have *: "\<And>x. B \<le> norm x \<Longrightarrow> x \<in> s"
     by (force simp: ball_def dist_norm)
-  have unbounded_inner: "~ bounded {x. inner i x \<ge> B}"
+  have unbounded_inner: "\<not> bounded {x. inner i x \<ge> B}"
     apply (auto simp: bounded_def dist_norm)
     apply (rule_tac x="x + (max B e + 1 + \<bar>i \<bullet> x\<bar>) *\<^sub>R i" in exI)
     apply simp
@@ -2475,8 +2483,8 @@
 lemma cobounded_unique_unbounded_component:
     fixes s :: "'a :: euclidean_space set"
     assumes bs: "bounded (-s)" and "2 \<le> DIM('a)"
-        and bo: "~ bounded(connected_component_set s x)"
-                "~ bounded(connected_component_set s y)"
+        and bo: "\<not> bounded(connected_component_set s x)"
+                "\<not> bounded(connected_component_set s y)"
       shows "connected_component_set s x = connected_component_set s y"
 proof -
   obtain i::'a where i: "i \<in> Basis"
@@ -2507,7 +2515,7 @@
 
 lemma cobounded_unbounded_components:
     fixes s :: "'a :: euclidean_space set"
-    shows "bounded (-s) \<Longrightarrow> \<exists>c. c \<in> components s \<and> ~bounded c"
+    shows "bounded (-s) \<Longrightarrow> \<exists>c. c \<in> components s \<and> \<not>bounded c"
   by (metis cobounded_unbounded_component components_def imageI)
 
 lemma cobounded_unique_unbounded_components:
@@ -2523,7 +2531,7 @@
   by (meson cobounded_unique_unbounded_components connected_eq_connected_components_eq assms)
 
 
-section\<open>The "inside" and "outside" of a set\<close>
+section\<open>The \<open>inside\<close> and \<open>outside\<close> of a Set\<close>
 
 text%important\<open>The inside comprises the points in a bounded connected component of the set's complement.
   The outside comprises the points in unbounded connected component of the complement.\<close>
@@ -2532,9 +2540,9 @@
   "inside S \<equiv> {x. (x \<notin> S) \<and> bounded(connected_component_set ( - S) x)}"
 
 definition%important outside where
-  "outside S \<equiv> -S \<inter> {x. ~ bounded(connected_component_set (- S) x)}"
-
-lemma outside: "outside S = {x. ~ bounded(connected_component_set (- S) x)}"
+  "outside S \<equiv> -S \<inter> {x. \<not> bounded(connected_component_set (- S) x)}"
+
+lemma outside: "outside S = {x. \<not> bounded(connected_component_set (- S) x)}"
   by (auto simp: outside_def) (metis Compl_iff bounded_empty connected_component_eq_empty)
 
 lemma inside_no_overlap [simp]: "inside S \<inter> S = {}"
@@ -2619,7 +2627,7 @@
 
 lemma unbounded_outside:
     fixes S :: "'a::{real_normed_vector, perfect_space} set"
-    shows "bounded S \<Longrightarrow> ~ bounded(outside S)"
+    shows "bounded S \<Longrightarrow> \<not> bounded(outside S)"
   using cobounded_imp_unbounded cobounded_outside by blast
 
 lemma bounded_inside:
@@ -2655,7 +2663,7 @@
 lemma not_outside_connected_component_lt:
     fixes S :: "'a::euclidean_space set"
     assumes S: "bounded S" and "2 \<le> DIM('a)"
-      shows "- (outside S) = {x. \<forall>B. \<exists>y. B < norm(y) \<and> ~ (connected_component (- S) x y)}"
+      shows "- (outside S) = {x. \<forall>B. \<exists>y. B < norm(y) \<and> \<not> connected_component (- S) x y}"
 proof -
   obtain B::real where B: "0 < B" and Bno: "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> B"
     using S [simplified bounded_pos] by auto
@@ -2681,24 +2689,24 @@
 lemma not_outside_connected_component_le:
     fixes S :: "'a::euclidean_space set"
     assumes S: "bounded S"  "2 \<le> DIM('a)"
-      shows "- (outside S) = {x. \<forall>B. \<exists>y. B \<le> norm(y) \<and> ~ (connected_component (- S) x y)}"
+      shows "- (outside S) = {x. \<forall>B. \<exists>y. B \<le> norm(y) \<and> \<not> connected_component (- S) x y}"
 apply (auto intro: less_imp_le simp: not_outside_connected_component_lt [OF assms])
 by (meson gt_ex less_le_trans)
 
 lemma inside_connected_component_lt:
     fixes S :: "'a::euclidean_space set"
     assumes S: "bounded S"  "2 \<le> DIM('a)"
-      shows "inside S = {x. (x \<notin> S) \<and> (\<forall>B. \<exists>y. B < norm(y) \<and> ~(connected_component (- S) x y))}"
+      shows "inside S = {x. (x \<notin> S) \<and> (\<forall>B. \<exists>y. B < norm(y) \<and> \<not> connected_component (- S) x y)}"
   by (auto simp: inside_outside not_outside_connected_component_lt [OF assms])
 
 lemma inside_connected_component_le:
     fixes S :: "'a::euclidean_space set"
     assumes S: "bounded S"  "2 \<le> DIM('a)"
-      shows "inside S = {x. (x \<notin> S) \<and> (\<forall>B. \<exists>y. B \<le> norm(y) \<and> ~(connected_component (- S) x y))}"
+      shows "inside S = {x. (x \<notin> S) \<and> (\<forall>B. \<exists>y. B \<le> norm(y) \<and> \<not> connected_component (- S) x y)}"
   by (auto simp: inside_outside not_outside_connected_component_le [OF assms])
 
 lemma inside_subset:
-  assumes "connected U" and "~bounded U" and "T \<union> U = - S"
+  assumes "connected U" and "\<not> bounded U" and "T \<union> U = - S"
   shows "inside S \<subseteq> T"
 apply (auto simp: inside_def)
 by (metis bounded_subset [of "connected_component_set (- S) _"] connected_component_maximal
@@ -2794,7 +2802,7 @@
 lemma frontier_minimal_separating_closed:
   fixes S :: "'a::real_normed_vector set"
   assumes "closed S"
-      and nconn: "~ connected(- S)"
+      and nconn: "\<not> connected(- S)"
       and C: "C \<in> components (- S)"
       and conn: "\<And>T. \<lbrakk>closed T; T \<subset> S\<rbrakk> \<Longrightarrow> connected(- T)"
     shows "frontier C = S"
@@ -3319,11 +3327,12 @@
 
 lemma bounded_unique_outside:
     fixes s :: "'a :: euclidean_space set"
-    shows "\<lbrakk>bounded s; DIM('a) \<ge> 2\<rbrakk> \<Longrightarrow> (c \<in> components (- s) \<and> ~bounded c \<longleftrightarrow> c = outside s)"
+    shows "\<lbrakk>bounded s; DIM('a) \<ge> 2\<rbrakk> \<Longrightarrow> (c \<in> components (- s) \<and> \<not> bounded c \<longleftrightarrow> c = outside s)"
   apply (rule iffI)
   apply (metis cobounded_unique_unbounded_components connected_outside double_compl outside_bounded_nonempty outside_in_components unbounded_outside)
   by (simp add: connected_outside outside_bounded_nonempty outside_in_components unbounded_outside)
 
+
 subsection\<open>Condition for an open map's image to contain a ball\<close>
 
 proposition ball_subset_open_map_image:
@@ -3365,7 +3374,7 @@
       apply (rule le_no)
       using w wy oint
       by (force simp: imageI image_mono interiorI interior_subset frontier_def y)
-    have **: "(~(b \<inter> (- S) = {}) \<and> ~(b - (- S) = {}) \<Longrightarrow> (b \<inter> f \<noteq> {}))
+    have **: "(b \<inter> (- S) \<noteq> {} \<and> b - (- S) \<noteq> {} \<Longrightarrow> b \<inter> f \<noteq> {})
               \<Longrightarrow> (b \<inter> S \<noteq> {}) \<Longrightarrow> b \<inter> f = {} \<Longrightarrow>
               b \<subseteq> S" for b f and S :: "'b set"
       by blast
@@ -3377,10 +3386,9 @@
       by (metis dw_le norm_minus_commute not_less order_trans rle wy)
 qed
 
-section\<open> Homotopy of maps p,q : X=>Y with property P of all intermediate maps\<close>
-
-text%important\<open> We often just want to require that it fixes some subset, but to take in
-  the case of a loop homotopy, it's convenient to have a general property P.\<close>
+
+section \<open>Homotopy of Maps\<close> (* TODO separate theory? *)
+
 
 definition%important homotopic_with ::
   "[('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool, 'a set, 'b set, 'a \<Rightarrow> 'b, 'a \<Rightarrow> 'b] \<Rightarrow> bool"
@@ -3394,7 +3402,11 @@
        (\<forall>t \<in> {0..1}. P(\<lambda>x. h(t, x))))"
 
 
-text\<open> We often want to just localize the ending function equality or whatever.\<close>
+text\<open>$p, q$ are functions $X \to Y$, and the property $P$ restricts all intermediate maps.
+We often just want to require that $P$ fixes some subset, but to include the case of a loop homotopy,
+it is convenient to have a general property $P$.\<close>
+
+text \<open>We often want to just localize the ending function equality or whatever.\<close>
 proposition homotopic_with:
   fixes X :: "'a::topological_space set" and Y :: "'b::topological_space set"
   assumes "\<And>h k. (\<And>x. x \<in> X \<Longrightarrow> h x = k x) \<Longrightarrow> (P h \<longleftrightarrow> P k)"
@@ -3888,6 +3900,7 @@
   apply (auto simp: pathstart_def pathfinish_def elim!: homotopic_with_mono)
   done
 
+
 subsection\<open>Group properties for homotopy of paths\<close>
 
 text%important\<open>So taking equivalence classes under homotopy would give the fundamental group\<close>
@@ -4278,6 +4291,7 @@
     by (metis atLeastAtMost_iff imageI le_less_trans not_le path_image_def)
 qed
 
+
 subsection\<open> Homotopy and subpaths\<close>
 
 lemma homotopic_join_subpaths1:
@@ -4834,6 +4848,7 @@
     done
 qed
 
+
 subsection\<open>Local versions of topological properties in general\<close>
 
 definition%important locally :: "('a::topological_space set \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
@@ -5065,7 +5080,8 @@
     done
 qed
 
-subsection\<open>Sort of induction principle for connected sets\<close>
+
+subsection\<open>An induction principle for connected sets\<close>
 
 proposition connected_induction:
   assumes "connected S"
@@ -5084,7 +5100,7 @@
     done
   have 2: "openin (subtopology euclidean S)
              {b. \<exists>T. openin (subtopology euclidean S) T \<and>
-                     b \<in> T \<and> (\<forall>x\<in>T. P x \<longrightarrow> ~ Q x)}"
+                     b \<in> T \<and> (\<forall>x\<in>T. P x \<longrightarrow> \<not> Q x)}"
     apply (subst openin_subopen, clarify)
     apply (rule_tac x=T in exI, auto)
     done
@@ -5511,6 +5527,7 @@
     by (metis open_openin openin_topspace subtopology_superset top.extremum topspace_euclidean_subtopology)
 qed
 
+
 subsection\<open>Sura-Bura's results about compact components of sets\<close>
 
 proposition Sura_Bura_compact:
@@ -5742,7 +5759,7 @@
 qed blast
 
 
-subsection\<open>Important special cases of local connectedness and path connectedness\<close>
+subsection\<open>Special cases of local connectedness and path connectedness\<close>
 
 lemma locally_connected_1:
   assumes
@@ -6541,6 +6558,7 @@
   finally show ?thesis .
 qed
 
+
 subsection\<open>Retracts, in a general sense, preserve (co)homotopic triviality)\<close>
 
 locale%important Retracts =
@@ -6707,6 +6725,7 @@
     "S homeomorphic T \<Longrightarrow> (simply_connected S \<longleftrightarrow> simply_connected T)"
   by (metis homeomorphic_simply_connected homeomorphic_sym)
 
+
 subsection\<open>Homotopy equivalence\<close>
 
 definition%important homotopy_eqv :: "'a::topological_space set \<Rightarrow> 'b::topological_space set \<Rightarrow> bool"
@@ -6997,6 +7016,7 @@
   shows "\<lbrakk>contractible S; S homeomorphic T\<rbrakk> \<Longrightarrow> contractible T"
   by (metis homeomorphic_contractible_eq)
 
+
 subsection%unimportant\<open>Misc other results\<close>
 
 lemma bounded_connected_Compl_real:
@@ -7041,6 +7061,7 @@
     by blast
 qed
 
+
 subsection%unimportant\<open>Some Uncountable Sets\<close>
 
 lemma uncountable_closed_segment:
@@ -7155,7 +7176,7 @@
 
 lemma connected_card_eq_iff_nontrivial:
   fixes S :: "'a::metric_space set"
-  shows "connected S \<Longrightarrow> uncountable S \<longleftrightarrow> ~(\<exists>a. S \<subseteq> {a})"
+  shows "connected S \<Longrightarrow> uncountable S \<longleftrightarrow> \<not>(\<exists>a. S \<subseteq> {a})"
   apply (auto simp: countable_finite finite_subset)
   by (metis connected_uncountable is_singletonI' is_singleton_the_elem subset_singleton_iff)
 
@@ -7184,7 +7205,7 @@
 
 proposition path_connected_convex_diff_countable:
   fixes U :: "'a::euclidean_space set"
-  assumes "convex U" "~ collinear U" "countable S"
+  assumes "convex U" "\<not> collinear U" "countable S"
     shows "path_connected(U - S)"
 proof (clarsimp simp add: path_connected_def)
   fix a b
@@ -7202,7 +7223,7 @@
     have "?m \<in> U"
       using \<open>a \<in> U\<close> \<open>b \<in> U\<close> \<open>convex U\<close> convex_contains_segment by force
     obtain c where "c \<in> U" and nc_abc: "\<not> collinear {a,b,c}"
-      by (metis False \<open>a \<in> U\<close> \<open>b \<in> U\<close> \<open>~ collinear U\<close> collinear_triples insert_absorb)
+      by (metis False \<open>a \<in> U\<close> \<open>b \<in> U\<close> \<open>\<not> collinear U\<close> collinear_triples insert_absorb)
     have ncoll_mca: "\<not> collinear {?m,c,a}"
       by (metis (full_types) \<open>a \<noteq> ?m\<close> collinear_3_trans collinear_midpoint insert_commute nc_abc)
     have ncoll_mcb: "\<not> collinear {?m,c,b}"
@@ -7289,7 +7310,7 @@
 
 corollary connected_convex_diff_countable:
   fixes U :: "'a::euclidean_space set"
-  assumes "convex U" "~ collinear U" "countable S"
+  assumes "convex U" "\<not> collinear U" "countable S"
   shows "connected(U - S)"
   by (simp add: assms path_connected_convex_diff_countable path_connected_imp_connected)
 
@@ -7344,7 +7365,7 @@
 proposition path_connected_openin_diff_countable:
   fixes S :: "'a::euclidean_space set"
   assumes "connected S" and ope: "openin (subtopology euclidean (affine hull S)) S"
-      and "~ collinear S" "countable T"
+      and "\<not> collinear S" "countable T"
     shows "path_connected(S - T)"
 proof (clarsimp simp add: path_connected_component)
   fix x y
@@ -7360,8 +7381,8 @@
         by (auto simp: openin_contains_ball)
       with \<open>x \<in> U\<close> have x: "x \<in> ball x r \<inter> affine hull S"
         by auto
-      have "~ S \<subseteq> {x}"
-        using \<open>~ collinear S\<close>  collinear_subset by blast
+      have "\<not> S \<subseteq> {x}"
+        using \<open>\<not> collinear S\<close>  collinear_subset by blast
       then obtain x' where "x' \<noteq> x" "x' \<in> S"
         by blast
       obtain y where y: "y \<noteq> x" "y \<in> ball x r \<inter> affine hull S"
@@ -7403,7 +7424,7 @@
         show "x \<in> ball x r \<inter> affine hull S"
           using \<open>x \<in> S\<close> \<open>r > 0\<close> by (simp add: hull_inc)
         have "openin (subtopology euclidean (affine hull S)) (ball x r \<inter> affine hull S)"
-          by (simp add: inf.commute openin_Int_open)
+          by (subst inf.commute) (simp add: openin_Int_open)
         then show "openin (subtopology euclidean S) (ball x r \<inter> affine hull S)"
           by (rule openin_subset_trans [OF _ subS Ssub])
       qed (use * path_component_trans in \<open>auto simp: path_connected_component path_component_of_subset [OF ST]\<close>)
@@ -7414,7 +7435,7 @@
 corollary connected_openin_diff_countable:
   fixes S :: "'a::euclidean_space set"
   assumes "connected S" and ope: "openin (subtopology euclidean (affine hull S)) S"
-      and "~ collinear S" "countable T"
+      and "\<not> collinear S" "countable T"
     shows "connected(S - T)"
   by (metis path_connected_imp_connected path_connected_openin_diff_countable [OF assms])
 
@@ -7445,7 +7466,7 @@
 
 
 
-subsection\<open>Self-homeomorphisms shuffling points about in various ways\<close>
+subsection%unimportant \<open>Self-homeomorphisms shuffling points about\<close>
 
 subsubsection%unimportant\<open>The theorem \<open>homeomorphism_moving_points_exists\<close>\<close>
 
@@ -7577,7 +7598,7 @@
     done
 qed
 
-corollary homeomorphism_moving_point_2:
+corollary%unimportant homeomorphism_moving_point_2:
   fixes a :: "'a::euclidean_space"
   assumes "affine T" "a \<in> T" and u: "u \<in> ball a r \<inter> T" and v: "v \<in> ball a r \<inter> T"
   obtains f g where "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
@@ -7605,12 +7626,12 @@
 qed
 
 
-corollary homeomorphism_moving_point_3:
+corollary%unimportant homeomorphism_moving_point_3:
   fixes a :: "'a::euclidean_space"
   assumes "affine T" "a \<in> T" and ST: "ball a r \<inter> T \<subseteq> S" "S \<subseteq> T"
       and u: "u \<in> ball a r \<inter> T" and v: "v \<in> ball a r \<inter> T"
   obtains f g where "homeomorphism S S f g"
-                    "f u = v" "{x. ~ (f x = x \<and> g x = x)} \<subseteq> ball a r \<inter> T"
+                    "f u = v" "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> ball a r \<inter> T"
 proof -
   obtain f g where hom: "homeomorphism (cball a r \<inter> T) (cball a r \<inter> T) f g"
                and "f u = v" and fid: "\<And>x. \<lbrakk>x \<in> sphere a r; x \<in> T\<rbrakk> \<Longrightarrow> f x = x"
@@ -7683,21 +7704,21 @@
 qed
 
 
-proposition homeomorphism_moving_point:
+proposition%unimportant homeomorphism_moving_point:
   fixes a :: "'a::euclidean_space"
   assumes ope: "openin (subtopology euclidean (affine hull S)) S"
       and "S \<subseteq> T"
       and TS: "T \<subseteq> affine hull S"
       and S: "connected S" "a \<in> S" "b \<in> S"
   obtains f g where "homeomorphism T T f g" "f a = b"
-                    "{x. ~ (f x = x \<and> g x = x)} \<subseteq> S"
-                    "bounded {x. ~ (f x = x \<and> g x = x)}"
+                    "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S"
+                    "bounded {x. \<not> (f x = x \<and> g x = x)}"
 proof -
   have 1: "\<exists>h k. homeomorphism T T h k \<and> h (f d) = d \<and>
-              {x. ~ (h x = x \<and> k x = x)} \<subseteq> S \<and> bounded {x. ~ (h x = x \<and> k x = x)}"
+              {x. \<not> (h x = x \<and> k x = x)} \<subseteq> S \<and> bounded {x. \<not> (h x = x \<and> k x = x)}"
         if "d \<in> S" "f d \<in> S" and homfg: "homeomorphism T T f g"
-        and S: "{x. ~ (f x = x \<and> g x = x)} \<subseteq> S"
-        and bo: "bounded {x. ~ (f x = x \<and> g x = x)}" for d f g
+        and S: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S"
+        and bo: "bounded {x. \<not> (f x = x \<and> g x = x)}" for d f g
   proof (intro exI conjI)
     show homgf: "homeomorphism T T g f"
       by (metis homeomorphism_symD homfg)
@@ -7722,7 +7743,7 @@
       by force
     show "{x. \<not> ((f2 \<circ> f1) x = x \<and> (g1 \<circ> g2) x = x)} \<subseteq> S"
       using sub by force
-    have "bounded ({x. ~(f1 x = x \<and> g1 x = x)} \<union> {x. ~(f2 x = x \<and> g2 x = x)})"
+    have "bounded ({x. \<not>(f1 x = x \<and> g1 x = x)} \<union> {x. \<not>(f2 x = x \<and> g2 x = x)})"
       using bo by simp
     then show "bounded {x. \<not> ((f2 \<circ> f1) x = x \<and> (g1 \<circ> g2) x = x)}"
       by (rule bounded_subset) auto
@@ -7753,7 +7774,7 @@
       done
   qed
   have "\<exists>f g. homeomorphism T T f g \<and> f a = b \<and>
-              {x. ~ (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. ~ (f x = x \<and> g x = x)}"
+              {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
     apply (rule connected_equivalence_relation [OF S], safe)
       apply (blast intro: 1 2 3)+
     done
@@ -7769,7 +7790,7 @@
       and ope: "openin (subtopology euclidean (affine hull S)) S"
       and "S \<subseteq> T" "T \<subseteq> affine hull S" "connected S"
   shows "\<exists>f g. homeomorphism T T f g \<and> (\<forall>i \<in> K. f(x i) = y i) \<and>
-               {x. ~ (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. ~ (f x = x \<and> g x = x)}"
+               {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
   using assms
 proof (induction K)
   case empty
@@ -7783,11 +7804,11 @@
        and xyS: "\<And>i. i \<in> K \<Longrightarrow> x i \<in> S \<and> y i \<in> S"
     by (simp_all add: pairwise_insert)
   obtain f g where homfg: "homeomorphism T T f g" and feq: "\<And>i. i \<in> K \<Longrightarrow> f(x i) = y i"
-               and fg_sub: "{x. ~ (f x = x \<and> g x = x)} \<subseteq> S"
-               and bo_fg: "bounded {x. ~ (f x = x \<and> g x = x)}"
+               and fg_sub: "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S"
+               and bo_fg: "bounded {x. \<not> (f x = x \<and> g x = x)}"
     using insert.IH [OF xyS pw] insert.prems by (blast intro: that)
   then have "\<exists>f g. homeomorphism T T f g \<and> (\<forall>i \<in> K. f(x i) = y i) \<and>
-                   {x. ~ (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. ~ (f x = x \<and> g x = x)}"
+                   {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
     using insert by blast
   have aff_eq: "affine hull (S - y ` K) = affine hull S"
     apply (rule affine_hull_Diff)
@@ -7832,21 +7853,21 @@
       using feq hk_sub by (auto simp: heq)
     show "{x. \<not> ((h \<circ> f) x = x \<and> (g \<circ> k) x = x)} \<subseteq> S"
       using fg_sub hk_sub by force
-    have "bounded ({x. ~(f x = x \<and> g x = x)} \<union> {x. ~(h x = x \<and> k x = x)})"
+    have "bounded ({x. \<not>(f x = x \<and> g x = x)} \<union> {x. \<not>(h x = x \<and> k x = x)})"
       using bo_fg bo_hk bounded_Un by blast
     then show "bounded {x. \<not> ((h \<circ> f) x = x \<and> (g \<circ> k) x = x)}"
       by (rule bounded_subset) auto
   qed
 qed
 
-proposition homeomorphism_moving_points_exists:
+proposition%unimportant homeomorphism_moving_points_exists:
   fixes S :: "'a::euclidean_space set"
   assumes 2: "2 \<le> DIM('a)" "open S" "connected S" "S \<subseteq> T" "finite K"
       and KS: "\<And>i. i \<in> K \<Longrightarrow> x i \<in> S \<and> y i \<in> S"
       and pw: "pairwise (\<lambda>i j. (x i \<noteq> x j) \<and> (y i \<noteq> y j)) K"
       and S: "S \<subseteq> T" "T \<subseteq> affine hull S" "connected S"
   obtains f g where "homeomorphism T T f g" "\<And>i. i \<in> K \<Longrightarrow> f(x i) = y i"
-                    "{x. ~ (f x = x \<and> g x = x)} \<subseteq> S" "bounded {x. (~ (f x = x \<and> g x = x))}"
+                    "{x. \<not> (f x = x \<and> g x = x)} \<subseteq> S" "bounded {x. (\<not> (f x = x \<and> g x = x))}"
 proof (cases "S = {}")
   case True
   then show ?thesis
@@ -7868,7 +7889,6 @@
     using homeomorphism_moving_points_exists_gen [OF \<open>finite K\<close> KS pw _ ope S] that by fastforce
 qed
 
-
 subsubsection%unimportant\<open>The theorem \<open>homeomorphism_grouping_points_exists\<close>\<close>
 
 lemma homeomorphism_grouping_point_1:
@@ -7995,8 +8015,8 @@
   fixes T :: "real set"
   assumes "open U" "open S" "connected S" "U \<noteq> {}" "finite K" "K \<subseteq> S" "U \<subseteq> S" "S \<subseteq> T"
   obtains f g where "homeomorphism T T f g"
-                    "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U" "{x. (~ (f x = x \<and> g x = x))} \<subseteq> S"
-                    "bounded {x. (~ (f x = x \<and> g x = x))}"
+                    "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U" "{x. (\<not> (f x = x \<and> g x = x))} \<subseteq> S"
+                    "bounded {x. (\<not> (f x = x \<and> g x = x))}"
 proof -
   obtain c d where "box c d \<noteq> {}" "cbox c d \<subseteq> U"
   proof -
@@ -8106,11 +8126,11 @@
   qed
 qed
 
-proposition homeomorphism_grouping_points_exists:
+proposition%unimportant homeomorphism_grouping_points_exists:
   fixes S :: "'a::euclidean_space set"
   assumes "open U" "open S" "connected S" "U \<noteq> {}" "finite K" "K \<subseteq> S" "U \<subseteq> S" "S \<subseteq> T"
-  obtains f g where "homeomorphism T T f g" "{x. (~ (f x = x \<and> g x = x))} \<subseteq> S"
-                    "bounded {x. (~ (f x = x \<and> g x = x))}" "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U"
+  obtains f g where "homeomorphism T T f g" "{x. (\<not> (f x = x \<and> g x = x))} \<subseteq> S"
+                    "bounded {x. (\<not> (f x = x \<and> g x = x))}" "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U"
 proof (cases "2 \<le> DIM('a)")
   case True
   have TS: "T \<subseteq> affine hull S"
@@ -8172,10 +8192,10 @@
       using sub hj
       apply (drule_tac c="h x" in subsetD, force)
       by (metis imageE)
-    have "bounded (j ` {x. (~ (f x = x \<and> g x = x))})"
+    have "bounded (j ` {x. (\<not> (f x = x \<and> g x = x))})"
       by (rule bounded_linear_image [OF bou]) (use \<open>linear j\<close> linear_conv_bounded_linear in auto)
     moreover
-    have *: "{x. ~((j \<circ> f \<circ> h) x = x \<and> (j \<circ> g \<circ> h) x = x)} = j ` {x. (~ (f x = x \<and> g x = x))}"
+    have *: "{x. \<not>((j \<circ> f \<circ> h) x = x \<and> (j \<circ> g \<circ> h) x = x)} = j ` {x. (\<not> (f x = x \<and> g x = x))}"
       using hj by (auto simp: jf jg image_iff, metis+)
     ultimately show "bounded {x. \<not> ((j \<circ> f \<circ> h) x = x \<and> (j \<circ> g \<circ> h) x = x)}"
       by metis
@@ -8185,13 +8205,13 @@
 qed
 
 
-proposition homeomorphism_grouping_points_exists_gen:
+proposition%unimportant homeomorphism_grouping_points_exists_gen:
   fixes S :: "'a::euclidean_space set"
   assumes opeU: "openin (subtopology euclidean S) U"
       and opeS: "openin (subtopology euclidean (affine hull S)) S"
       and "U \<noteq> {}" "finite K" "K \<subseteq> S" and S: "S \<subseteq> T" "T \<subseteq> affine hull S" "connected S"
-  obtains f g where "homeomorphism T T f g" "{x. (~ (f x = x \<and> g x = x))} \<subseteq> S"
-                    "bounded {x. (~ (f x = x \<and> g x = x))}" "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U"
+  obtains f g where "homeomorphism T T f g" "{x. (\<not> (f x = x \<and> g x = x))} \<subseteq> S"
+                    "bounded {x. (\<not> (f x = x \<and> g x = x))}" "\<And>x. x \<in> K \<Longrightarrow> f x \<in> U"
 proof (cases "2 \<le> aff_dim S")
   case True
   have opeU': "openin (subtopology euclidean (affine hull S)) U"
@@ -8208,7 +8228,7 @@
   then obtain \<gamma> where \<gamma>: "bij_betw \<gamma> K P"
     using \<open>finite K\<close> finite_same_card_bij by blast
   have "\<exists>f g. homeomorphism T T f g \<and> (\<forall>i \<in> K. f(id i) = \<gamma> i) \<and>
-               {x. ~ (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. ~ (f x = x \<and> g x = x)}"
+               {x. \<not> (f x = x \<and> g x = x)} \<subseteq> S \<and> bounded {x. \<not> (f x = x \<and> g x = x)}"
   proof (rule homeomorphism_moving_points_exists_gen [OF \<open>finite K\<close> _ _ True opeS S])
     show "\<And>i. i \<in> K \<Longrightarrow> id i \<in> S \<and> \<gamma> i \<in> S"
       by (metis id_apply opeU openin_contains_cball subsetCE \<open>P \<subseteq> U\<close> \<open>bij_betw \<gamma> K P\<close> \<open>K \<subseteq> S\<close> bij_betwE)
@@ -8314,10 +8334,10 @@
     next
       have "compact (j ` closure {x. \<not> (f x = x \<and> g x = x)})"
         using bou by (auto simp: compact_continuous_image cont_hj)
-      then have "bounded (j ` {x. (~ (f x = x \<and> g x = x))})"
+      then have "bounded (j ` {x. \<not> (f x = x \<and> g x = x)})"
         by (rule bounded_closure_image [OF compact_imp_bounded])
       moreover
-      have *: "{x \<in> affine hull S. j (f (h x)) \<noteq> x \<or> j (g (h x)) \<noteq> x} = j ` {x. (~ (f x = x \<and> g x = x))}"
+      have *: "{x \<in> affine hull S. j (f (h x)) \<noteq> x \<or> j (g (h x)) \<noteq> x} = j ` {x. (\<not> (f x = x \<and> g x = x))}"
         using h j by (auto simp: image_iff; metis)
       ultimately have "bounded {x \<in> affine hull S. j (f (h x)) \<noteq> x \<or> j (g (h x)) \<noteq> x}"
         by metis
@@ -8337,7 +8357,8 @@
   qed
 qed
 
-subsection\<open>nullhomotopic mappings\<close>
+
+subsection\<open>Nullhomotopic mappings\<close>
 
 text\<open> A mapping out of a sphere is nullhomotopic iff it extends to the ball.
 This even works out in the degenerate cases when the radius is \<open>\<le>\<close> 0, and
--- a/src/HOL/Analysis/Poly_Roots.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Poly_Roots.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,7 +2,7 @@
     Ported from "hol_light/Multivariate/complexes.ml" by L C Paulson
 *)
 
-section%important \<open>polynomial functions: extremal behaviour and root counts\<close>
+section%important \<open>Polynomial Functions: Extremal Behaviour and Root Counts\<close>
 
 theory Poly_Roots
 imports Complex_Main
@@ -169,7 +169,7 @@
      by auto
    have c0: "c 0 = - (a * b 0)" using  b [of 0]
      by simp
-   then have extr_prem: "~ (\<exists>k\<le>n. b k \<noteq> 0) \<Longrightarrow> \<exists>k. k \<noteq> 0 \<and> k \<le> Suc n \<and> c k \<noteq> 0"
+   then have extr_prem: "\<not> (\<exists>k\<le>n. b k \<noteq> 0) \<Longrightarrow> \<exists>k. k \<noteq> 0 \<and> k \<le> Suc n \<and> c k \<noteq> 0"
      by (metis Suc.prems le0 minus_zero mult_zero_right)
    have "\<exists>k\<le>n. b k \<noteq> 0"
      apply (rule ccontr)
@@ -205,7 +205,7 @@
     shows  "(\<forall>z. (\<Sum>i\<le>n. c i * z^i) = 0) \<longleftrightarrow> (\<forall>k. k \<le> n \<longrightarrow> c k = 0)"
 proof (cases "(\<forall>z. (\<Sum>i\<le>n. c i * z^i) = 0)")
   case True
-  then have "~ finite {z. (\<Sum>i\<le>n. c i * z^i) = 0}"
+  then have "\<not> finite {z. (\<Sum>i\<le>n. c i * z^i) = 0}"
     by (simp add: infinite_UNIV_char_0)
   with True show ?thesis
     by (metis (poly_guards_query) polyfun_rootbound_finite)
--- a/src/HOL/Analysis/Polytope.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Polytope.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -270,7 +270,7 @@
 
 lemma%unimportant subset_of_face_of_affine_hull:
     fixes S :: "'a::euclidean_space set"
-  assumes T: "T face_of S" and "convex S" "U \<subseteq> S" and dis: "~disjnt (affine hull T) (rel_interior U)"
+  assumes T: "T face_of S" and "convex S" "U \<subseteq> S" and dis: "\<not> disjnt (affine hull T) (rel_interior U)"
   shows "U \<subseteq> T"
   apply (rule subset_of_face_of [OF T \<open>U \<subseteq> S\<close>])
   using face_of_imp_eq_affine_Int [OF \<open>convex S\<close> T]
@@ -846,10 +846,10 @@
       using affine_parallel_slice affine_affine_hull by metis 
     show ?thesis
     proof (intro conjI impI allI ballI exI)
-      have *: "S \<subseteq> - (affine hull S \<inter> {x. P x}) \<union> affine hull S \<inter> {x. Q x} \<Longrightarrow> S \<subseteq> {x. ~P x \<or> Q x}" 
+      have *: "S \<subseteq> - (affine hull S \<inter> {x. P x}) \<union> affine hull S \<inter> {x. Q x} \<Longrightarrow> S \<subseteq> {x. \<not> P x \<or> Q x}" 
         for P Q 
         using hull_subset by fastforce  
-      have "S \<subseteq> {x. ~ (a' \<bullet> x \<le> b') \<or> a' \<bullet> x = b'}"
+      have "S \<subseteq> {x. \<not> (a' \<bullet> x \<le> b') \<or> a' \<bullet> x = b'}"
         apply (rule *)
         apply (simp only: le eq)
         using Ssub by auto
@@ -919,7 +919,7 @@
    "{x. x extreme_point_of (convex hull S)} \<subseteq> S"
 using%unimportant extreme_point_of_convex_hull by auto
 
-lemma%unimportant extreme_point_of_empty [simp]: "~ (x extreme_point_of {})"
+lemma%unimportant extreme_point_of_empty [simp]: "\<not> (x extreme_point_of {})"
   by (simp add: extreme_point_of_def)
 
 lemma%unimportant extreme_point_of_singleton [iff]: "x extreme_point_of {a} \<longleftrightarrow> x = a"
@@ -974,10 +974,10 @@
                     (infixr "(facet'_of)" 50)
   where "F facet_of S \<longleftrightarrow> F face_of S \<and> F \<noteq> {} \<and> aff_dim F = aff_dim S - 1"
 
-lemma%unimportant facet_of_empty [simp]: "~ S facet_of {}"
+lemma%unimportant facet_of_empty [simp]: "\<not> S facet_of {}"
   by (simp add: facet_of_def)
 
-lemma%unimportant facet_of_irrefl [simp]: "~ S facet_of S "
+lemma%unimportant facet_of_irrefl [simp]: "\<not> S facet_of S "
   by (simp add: facet_of_def)
 
 lemma%unimportant facet_of_imp_face_of: "F facet_of S \<Longrightarrow> F face_of S"
@@ -1088,7 +1088,7 @@
       using * [of "norm b" "norm a" "1-u" for u] noax that
         apply (simp add: add.commute)
       done
-    ultimately have "~ (norm a < norm x) \<and> ~ (norm b < norm x)"
+    ultimately have "\<not> (norm a < norm x) \<and> \<not> (norm b < norm x)"
       by auto
     then show ?thesis
       using different_norm_3_collinear_points noax nobx that(3) by fastforce
@@ -1268,7 +1268,7 @@
 lemma%unimportant extreme_point_of_convex_hull_affine_independent:
   fixes S :: "'a::euclidean_space set"
   shows
-   "~ affine_dependent S
+   "\<not> affine_dependent S
          \<Longrightarrow> (x extreme_point_of (convex hull S) \<longleftrightarrow> x \<in> S)"
 by (metis aff_independent_finite affine_dependent_def affine_hull_convex_hull extreme_point_of_convex_hull_convex_independent finite_imp_compact hull_inc)
 
@@ -1473,7 +1473,7 @@
 
 proposition%important face_of_convex_hull_affine_independent:
   fixes S :: "'a::euclidean_space set"
-  assumes "~ affine_dependent S"
+  assumes "\<not> affine_dependent S"
     shows "(T face_of (convex hull S) \<longleftrightarrow> (\<exists>c. c \<subseteq> S \<and> T = convex hull c))"
           (is "?lhs = ?rhs")
 proof%unimportant
@@ -1495,7 +1495,7 @@
 
 lemma%unimportant facet_of_convex_hull_affine_independent:
   fixes S :: "'a::euclidean_space set"
-  assumes "~ affine_dependent S"
+  assumes "\<not> affine_dependent S"
     shows "T facet_of (convex hull S) \<longleftrightarrow>
            T \<noteq> {} \<and> (\<exists>u. u \<in> S \<and> T = convex hull (S - {u}))"
           (is "?lhs = ?rhs")
@@ -1508,7 +1508,7 @@
     by (auto simp: face_of_convex_hull_affine_independent [OF assms])
   then have affs: "aff_dim S = aff_dim c + 1"
     by (metis aff_dim_convex_hull afft eq_diff_eq)
-  have "~ affine_dependent c"
+  have "\<not> affine_dependent c"
     using \<open>c \<subseteq> S\<close> affine_dependent_subset assms by blast
   with affs have "card (S - c) = 1"
     apply (simp add: aff_dim_affine_independent [symmetric] aff_dim_convex_hull)
@@ -1543,7 +1543,7 @@
 lemma%unimportant facet_of_convex_hull_affine_independent_alt:
   fixes S :: "'a::euclidean_space set"
   shows
-   "~affine_dependent S
+   "\<not>affine_dependent S
         \<Longrightarrow> (T facet_of (convex hull S) \<longleftrightarrow>
              2 \<le> card S \<and> (\<exists>u. u \<in> S \<and> T = convex hull (S - {u})))"
 apply (simp add: facet_of_convex_hull_affine_independent)
@@ -1897,20 +1897,20 @@
     have "\<exists>a' b'. a' \<noteq> 0 \<and>
                   affine hull S \<inter> {x. a' \<bullet> x \<le> b'} = affine hull S \<inter> h \<and>
                   (\<forall>w \<in> affine hull S. (w + a') \<in> affine hull S)"
-        if "h \<in> F" "~(affine hull S \<subseteq> h)" for h
+        if "h \<in> F" "\<not>(affine hull S \<subseteq> h)" for h
     proof -
       have "a h \<noteq> 0" and "h = {x. a h \<bullet> x \<le> b h}" "h \<inter> \<Inter>F = \<Inter>F"
         using \<open>h \<in> F\<close> ab by auto
       then have "(affine hull S) \<inter> {x. a h \<bullet> x \<le> b h} \<noteq> {}"
         by (metis (no_types) affine_hull_eq_empty inf.absorb_iff2 inf_assoc inf_bot_right inf_commute seq that(2))
-      moreover have "~ (affine hull S \<subseteq> {x. a h \<bullet> x \<le> b h})"
+      moreover have "\<not> (affine hull S \<subseteq> {x. a h \<bullet> x \<le> b h})"
         using \<open>h = {x. a h \<bullet> x \<le> b h}\<close> that(2) by blast
       ultimately show ?thesis
         using affine_parallel_slice [of "affine hull S"]
         by (metis \<open>h = {x. a h \<bullet> x \<le> b h}\<close> affine_affine_hull)
     qed
     then obtain a b
-         where ab: "\<And>h. \<lbrakk>h \<in> F; ~ (affine hull S \<subseteq> h)\<rbrakk>
+         where ab: "\<And>h. \<lbrakk>h \<in> F; \<not> (affine hull S \<subseteq> h)\<rbrakk>
              \<Longrightarrow> a h \<noteq> 0 \<and>
                   affine hull S \<inter> {x. a h \<bullet> x \<le> b h} = affine hull S \<inter> h \<and>
                   (\<forall>w \<in> affine hull S. (w + a h) \<in> affine hull S)"
@@ -1918,7 +1918,7 @@
     have seq2: "S = affine hull S \<inter> (\<Inter>h\<in>{h \<in> F. \<not> affine hull S \<subseteq> h}. {x. a h \<bullet> x \<le> b h})"
       by (subst seq) (auto simp: ab INT_extend_simps)
     show ?thesis
-      apply (rule_tac x="(\<lambda>h. {x. a h \<bullet> x \<le> b h}) ` {h. h \<in> F \<and> ~(affine hull S \<subseteq> h)}" in exI)
+      apply (rule_tac x="(\<lambda>h. {x. a h \<bullet> x \<le> b h}) ` {h. h \<in> F \<and> \<not>(affine hull S \<subseteq> h)}" in exI)
       apply (intro conjI seq2)
         using \<open>finite F\<close> apply force
        using ab apply blast
@@ -1958,9 +1958,9 @@
     done
   then obtain F where F: "card F = n" "finite F" and seq: "?P F" and aff: "?Q F"
     by blast
-  then have "~ (finite g \<and> ?P g \<and> ?Q g)" if "card g < n" for g
+  then have "\<not> (finite g \<and> ?P g \<and> ?Q g)" if "card g < n" for g
     using that by (auto simp: n_def dest!: not_less_Least)
-  then have *: "~ (?P g \<and> ?Q g)" if "g \<subset> F" for g
+  then have *: "\<not> (?P g \<and> ?Q g)" if "g \<subset> F" for g
     using that \<open>finite F\<close> psubset_card_mono \<open>card F = n\<close>
     by (metis finite_Int inf.strict_order_iff)
   have 1: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subseteq> affine hull S \<inter> \<Inter>F'"
@@ -2230,7 +2230,7 @@
     using seq by blast
   have "F \<noteq> {}" using assms
     by (metis affine_Int affine_Inter affine_affine_hull ex_in_conv face_of_affine_trivial)
-  then obtain i where "i \<in> F" "~ (a i \<bullet> x < b i)"
+  then obtain i where "i \<in> F" "\<not> (a i \<bullet> x < b i)"
     using \<open>x \<in> S\<close> rels xnot by auto
   with xint have "a i \<bullet> x = b i"
     by (metis eq_iff mem_Collect_eq not_le Inter_iff faceq)
@@ -2745,7 +2745,7 @@
 
 lemma rel_boundary_of_convex_hull:
     fixes S :: "'a::euclidean_space set"
-    assumes "~ affine_dependent S"
+    assumes "\<not> affine_dependent S"
       shows "(convex hull S) - rel_interior(convex hull S) = (\<Union>a\<in>S. convex hull (S - {a}))"
 proof -
   have "finite S" by (metis assms aff_independent_finite)
@@ -3074,17 +3074,17 @@
 
 text\<open>The notion of n-simplex for integer @{term"n \<ge> -1"}\<close>
 definition simplex :: "int \<Rightarrow> 'a::euclidean_space set \<Rightarrow> bool" (infix "simplex" 50)
-  where "n simplex S \<equiv> \<exists>C. ~(affine_dependent C) \<and> int(card C) = n + 1 \<and> S = convex hull C"
+  where "n simplex S \<equiv> \<exists>C. \<not> affine_dependent C \<and> int(card C) = n + 1 \<and> S = convex hull C"
 
 lemma simplex:
     "n simplex S \<longleftrightarrow> (\<exists>C. finite C \<and>
-                       ~(affine_dependent C) \<and>
+                       \<not> affine_dependent C \<and>
                        int(card C) = n + 1 \<and>
                        S = convex hull C)"
   by (auto simp add: simplex_def intro: aff_independent_finite)
 
 lemma simplex_convex_hull:
-   "~affine_dependent C \<and> int(card C) = n + 1 \<Longrightarrow> n simplex (convex hull C)"
+   "\<not> affine_dependent C \<and> int(card C) = n + 1 \<Longrightarrow> n simplex (convex hull C)"
   by (auto simp add: simplex_def)
 
 lemma convex_simplex: "n simplex S \<Longrightarrow> convex S"
@@ -3170,7 +3170,7 @@
   assumes "n simplex S" and a: "a \<notin> affine hull S"
   shows "(n+1) simplex (convex hull (insert a S))"
 proof -
-  obtain C where C: "finite C" "~(affine_dependent C)" "int(card C) = n+1" and S: "S = convex hull C"
+  obtain C where C: "finite C" "\<not> affine_dependent C" "int(card C) = n+1" and S: "S = convex hull C"
     using assms unfolding simplex by force
   show ?thesis
     unfolding simplex
@@ -3270,11 +3270,11 @@
               U [unfolded rel_frontier_def] tnot
             by (auto simp: closed_segment_eq_open)
           ultimately
-          have "~(between (t,u) z | between (u,z) t | between (z,t) u)" if "x \<noteq> z"
+          have "\<not>(between (t,u) z | between (u,z) t | between (z,t) u)" if "x \<noteq> z"
             using that xt xu
             apply (simp add: between_mem_segment [symmetric])
             by (metis between_commute between_trans_2 between_antisym)
-          then have "~ collinear {t, z, u}" if "x \<noteq> z"
+          then have "\<not> collinear {t, z, u}" if "x \<noteq> z"
             by (auto simp: that collinear_between_cases between_commute)
           moreover have "collinear {t, z, x}"
             by (metis closed_segment_commute collinear_2 collinear_closed_segment collinear_triples ends_in_segment(1) insert_absorb insert_absorb2 xt)
@@ -3352,7 +3352,7 @@
       by (auto simp: \<N>_def poly\<M> polytope_imp_convex polytope_imp_closed)
     have in_rel_interior: "(SOME z. z \<in> rel_interior C) \<in> rel_interior C" if "C \<in> \<N>" for C
         using that poly\<M> polytope_imp_convex rel_interior_aff_dim some_in_eq by (fastforce simp: \<N>_def)
-    have *: "\<exists>T. ~affine_dependent T \<and> card T \<le> n \<and> aff_dim K < n \<and> K = convex hull T"
+    have *: "\<exists>T. \<not> affine_dependent T \<and> card T \<le> n \<and> aff_dim K < n \<and> K = convex hull T"
       if "K \<in> \<U>" for K
     proof -
       obtain r where r: "r simplex K"
--- a/src/HOL/Analysis/Product_Vector.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Product_Vector.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -5,9 +5,9 @@
 section \<open>Cartesian Products as Vector Spaces\<close>
 
 theory Product_Vector
-imports
-  Inner_Product
-  "HOL-Library.Product_Plus"
+  imports
+    Complex_Main
+    "HOL-Library.Product_Plus"
 begin
 
 lemma Times_eq_image_sum:
@@ -395,47 +395,6 @@
   using assms
   by (auto simp: has_vector_derivative_def intro!: derivative_eq_intros)
 
-
-subsection \<open>Product is an inner product space\<close>
-
-instantiation prod :: (real_inner, real_inner) real_inner
-begin
-
-definition inner_prod_def:
-  "inner x y = inner (fst x) (fst y) + inner (snd x) (snd y)"
-
-lemma inner_Pair [simp]: "inner (a, b) (c, d) = inner a c + inner b d"
-  unfolding inner_prod_def by simp
-
-instance
-proof
-  fix r :: real
-  fix x y z :: "'a::real_inner \<times> 'b::real_inner"
-  show "inner x y = inner y x"
-    unfolding inner_prod_def
-    by (simp add: inner_commute)
-  show "inner (x + y) z = inner x z + inner y z"
-    unfolding inner_prod_def
-    by (simp add: inner_add_left)
-  show "inner (scaleR r x) y = r * inner x y"
-    unfolding inner_prod_def
-    by (simp add: distrib_left)
-  show "0 \<le> inner x x"
-    unfolding inner_prod_def
-    by (intro add_nonneg_nonneg inner_ge_zero)
-  show "inner x x = 0 \<longleftrightarrow> x = 0"
-    unfolding inner_prod_def prod_eq_iff
-    by (simp add: add_nonneg_eq_0_iff)
-  show "norm x = sqrt (inner x x)"
-    unfolding norm_prod_def inner_prod_def
-    by (simp add: power2_norm_eq_inner)
-qed
-
-end
-
-lemma inner_Pair_0: "inner x (0, b) = inner (snd x) b" "inner x (a, 0) = inner (fst x) a"
-    by (cases x, simp)+
-
 lemma
   fixes x :: "'a::real_normed_vector"
   shows norm_Pair1 [simp]: "norm (0,x) = norm x"
--- a/src/HOL/Analysis/Radon_Nikodym.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Radon_Nikodym.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -2,7 +2,7 @@
     Author:     Johannes Hölzl, TU München
 *)
 
-section%important \<open>Radon-Nikod{\'y}m derivative\<close>
+section%important \<open>Radon-Nikod{\'y}m Derivative\<close>
 
 theory Radon_Nikodym
 imports Bochner_Integration
--- a/src/HOL/Analysis/Riemann_Mapping.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Riemann_Mapping.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -860,7 +860,7 @@
 lemma frontier_properties:
   assumes "simply_connected S"
   shows "if bounded S then connected(frontier S)
-         else \<forall>C \<in> components(frontier S). ~bounded C"
+         else \<forall>C \<in> components(frontier S). \<not> bounded C"
 proof -
   have "S = {} \<or> S homeomorphic ball (0::complex) 1"
     using simply_connected_eq_homeomorphic_to_disc assms openS by blast
@@ -1217,7 +1217,7 @@
 qed
 
 lemma empty_inside:
-  assumes "connected S" "\<And>C. C \<in> components (- S) \<Longrightarrow> ~bounded C"
+  assumes "connected S" "\<And>C. C \<in> components (- S) \<Longrightarrow> \<not> bounded C"
   shows "inside S = {}"
   using assms by (auto simp: components_def inside_def)
 
--- a/src/HOL/Analysis/Simplex_Content.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Simplex_Content.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -5,9 +5,10 @@
    The content of an n-dimensional simplex, including the formula for the content of a
    triangle and Heron's formula.
 *)
-section%important \<open>Volume of a simplex\<close>
+section%important \<open>Volume of a Simplex\<close>
+
 theory Simplex_Content
-  imports Change_Of_Vars
+imports Change_Of_Vars
 begin
 
 lemma fact_neq_top_ennreal [simp]: "fact n \<noteq> (top :: ennreal)"
--- a/src/HOL/Analysis/Starlike.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Starlike.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -6,10 +6,10 @@
    Author:     Johannes Hoelzl, TU Muenchen
 *)
 
-section \<open>Line segments, Starlike Sets, etc\<close>
+section \<open>Line Segments\<close>
 
 theory Starlike
-  imports Convex_Euclidean_Space
+imports Convex_Euclidean_Space
 begin
 
 subsection \<open>Midpoint\<close>
@@ -3726,7 +3726,7 @@
 
 lemma affine_independent_convex_affine_hull:
   fixes s :: "'a::euclidean_space set"
-  assumes "~affine_dependent s" "t \<subseteq> s"
+  assumes "\<not> affine_dependent s" "t \<subseteq> s"
     shows "convex hull t = affine hull t \<inter> convex hull s"
 proof -
   have fin: "finite s" "finite t" using assms aff_independent_finite finite_subset by auto
@@ -3764,7 +3764,7 @@
 
 lemma affine_independent_span_eq:
   fixes s :: "'a::euclidean_space set"
-  assumes "~affine_dependent s" "card s = Suc (DIM ('a))"
+  assumes "\<not> affine_dependent s" "card s = Suc (DIM ('a))"
     shows "affine hull s = UNIV"
 proof (cases "s = {}")
   case True then show ?thesis
@@ -3789,7 +3789,7 @@
 
 lemma affine_independent_span_gt:
   fixes s :: "'a::euclidean_space set"
-  assumes ind: "~ affine_dependent s" and dim: "DIM ('a) < card s"
+  assumes ind: "\<not> affine_dependent s" and dim: "DIM ('a) < card s"
     shows "affine hull s = UNIV"
   apply (rule affine_independent_span_eq [OF ind])
   apply (rule antisym)
@@ -3831,7 +3831,7 @@
 
 lemma rel_interior_convex_hull_explicit:
   fixes s :: "'a::euclidean_space set"
-  assumes "~ affine_dependent s"
+  assumes "\<not> affine_dependent s"
   shows "rel_interior(convex hull s) =
          {y. \<exists>u. (\<forall>x \<in> s. 0 < u x) \<and> sum u s = 1 \<and> sum (\<lambda>x. u x *\<^sub>R x) s = y}"
          (is "?lhs = ?rhs")
@@ -3904,18 +3904,18 @@
 lemma interior_convex_hull_explicit_minimal:
   fixes s :: "'a::euclidean_space set"
   shows
-   "~ affine_dependent s
+   "\<not> affine_dependent s
         ==> interior(convex hull s) =
              (if card(s) \<le> DIM('a) then {}
               else {y. \<exists>u. (\<forall>x \<in> s. 0 < u x) \<and> sum u s = 1 \<and> (\<Sum>x\<in>s. u x *\<^sub>R x) = y})"
   apply (simp add: aff_independent_finite empty_interior_convex_hull, clarify)
   apply (rule trans [of _ "rel_interior(convex hull s)"])
-  apply (simp add: affine_hull_convex_hull affine_independent_span_gt rel_interior_interior)
+  apply (simp add: affine_independent_span_gt rel_interior_interior)
   by (simp add: rel_interior_convex_hull_explicit)
 
 lemma interior_convex_hull_explicit:
   fixes s :: "'a::euclidean_space set"
-  assumes "~ affine_dependent s"
+  assumes "\<not> affine_dependent s"
   shows
    "interior(convex hull s) =
              (if card(s) \<le> DIM('a) then {}
@@ -4082,7 +4082,7 @@
 
 lemma rel_frontier_convex_hull_explicit:
   fixes s :: "'a::euclidean_space set"
-  assumes "~ affine_dependent s"
+  assumes "\<not> affine_dependent s"
   shows "rel_frontier(convex hull s) =
          {y. \<exists>u. (\<forall>x \<in> s. 0 \<le> u x) \<and> (\<exists>x \<in> s. u x = 0) \<and> sum u s = 1 \<and> sum (\<lambda>x. u x *\<^sub>R x) s = y}"
 proof -
@@ -4106,7 +4106,7 @@
 
 lemma frontier_convex_hull_explicit:
   fixes s :: "'a::euclidean_space set"
-  assumes "~ affine_dependent s"
+  assumes "\<not> affine_dependent s"
   shows "frontier(convex hull s) =
          {y. \<exists>u. (\<forall>x \<in> s. 0 \<le> u x) \<and> (DIM ('a) < card s \<longrightarrow> (\<exists>x \<in> s. u x = 0)) \<and>
              sum u s = 1 \<and> sum (\<lambda>x. u x *\<^sub>R x) s = y}"
@@ -4133,7 +4133,7 @@
 
 lemma rel_frontier_convex_hull_cases:
   fixes s :: "'a::euclidean_space set"
-  assumes "~ affine_dependent s"
+  assumes "\<not> affine_dependent s"
   shows "rel_frontier(convex hull s) = \<Union>{convex hull (s - {x}) |x. x \<in> s}"
 proof -
   have fs: "finite s"
@@ -4164,7 +4164,7 @@
 
 lemma frontier_convex_hull_eq_rel_frontier:
   fixes s :: "'a::euclidean_space set"
-  assumes "~ affine_dependent s"
+  assumes "\<not> affine_dependent s"
   shows "frontier(convex hull s) =
            (if card s \<le> DIM ('a) then convex hull s else rel_frontier(convex hull s))"
   using assms
@@ -4174,7 +4174,7 @@
 
 lemma frontier_convex_hull_cases:
   fixes s :: "'a::euclidean_space set"
-  assumes "~ affine_dependent s"
+  assumes "\<not> affine_dependent s"
   shows "frontier(convex hull s) =
            (if card s \<le> DIM ('a) then convex hull s else \<Union>{convex hull (s - {x}) |x. x \<in> s})"
 by (simp add: assms frontier_convex_hull_eq_rel_frontier rel_frontier_convex_hull_cases)
@@ -4668,7 +4668,7 @@
 
 lemma interior_convex_hull_3_minimal:
   fixes a :: "'a::euclidean_space"
-  shows "\<lbrakk>~ collinear{a,b,c}; DIM('a) = 2\<rbrakk>
+  shows "\<lbrakk>\<not> collinear{a,b,c}; DIM('a) = 2\<rbrakk>
          \<Longrightarrow> interior(convex hull {a,b,c}) =
                 {v. \<exists>x y z. 0 < x \<and> 0 < y \<and> 0 < z \<and> x + y + z = 1 \<and>
                             x *\<^sub>R a + y *\<^sub>R b + z *\<^sub>R c = v}"
@@ -4921,6 +4921,11 @@
 
 subsection%unimportant\<open>Basic lemmas about hyperplanes and halfspaces\<close>
 
+lemma halfspace_Int_eq:
+     "{x. a \<bullet> x \<le> b} \<inter> {x. b \<le> a \<bullet> x} = {x. a \<bullet> x = b}"
+     "{x. b \<le> a \<bullet> x} \<inter> {x. a \<bullet> x \<le> b} = {x. a \<bullet> x = b}"
+  by auto
+
 lemma hyperplane_eq_Ex:
   assumes "a \<noteq> 0" obtains x where "a \<bullet> x = b"
   by (rule_tac x = "(b / (a \<bullet> a)) *\<^sub>R a" in that) (simp add: assms)
@@ -5862,7 +5867,7 @@
 
 lemma affine_dependent_choose:
   fixes a :: "'a :: euclidean_space"
-  assumes "~(affine_dependent S)"
+  assumes "\<not>(affine_dependent S)"
   shows "affine_dependent(insert a S) \<longleftrightarrow> a \<notin> S \<and> a \<in> affine hull S"
         (is "?lhs = ?rhs")
 proof safe
@@ -5887,7 +5892,7 @@
 
 lemma affine_independent_insert:
   fixes a :: "'a :: euclidean_space"
-  shows "\<lbrakk>~(affine_dependent S); a \<notin> affine hull S\<rbrakk> \<Longrightarrow> ~(affine_dependent(insert a S))"
+  shows "\<lbrakk>\<not> affine_dependent S; a \<notin> affine hull S\<rbrakk> \<Longrightarrow> \<not> affine_dependent(insert a S)"
   by (simp add: affine_dependent_choose)
 
 lemma subspace_bounded_eq_trivial:
@@ -6158,7 +6163,7 @@
 
 lemma
   fixes s :: "'a::euclidean_space set"
-  assumes "~ (affine_dependent(s \<union> t))"
+  assumes "\<not> affine_dependent(s \<union> t)"
     shows convex_hull_Int_subset: "convex hull s \<inter> convex hull t \<subseteq> convex hull (s \<inter> t)" (is ?C)
       and affine_hull_Int_subset: "affine hull s \<inter> affine hull t \<subseteq> affine hull (s \<inter> t)" (is ?A)
 proof -
@@ -6201,7 +6206,7 @@
 
 proposition affine_hull_Int:
   fixes s :: "'a::euclidean_space set"
-  assumes "~ (affine_dependent(s \<union> t))"
+  assumes "\<not> affine_dependent(s \<union> t)"
     shows "affine hull (s \<inter> t) = affine hull s \<inter> affine hull t"
 apply (rule subset_antisym)
 apply (simp add: hull_mono)
@@ -6209,7 +6214,7 @@
 
 proposition convex_hull_Int:
   fixes s :: "'a::euclidean_space set"
-  assumes "~ (affine_dependent(s \<union> t))"
+  assumes "\<not> affine_dependent(s \<union> t)"
     shows "convex hull (s \<inter> t) = convex hull s \<inter> convex hull t"
 apply (rule subset_antisym)
 apply (simp add: hull_mono)
@@ -6217,7 +6222,7 @@
 
 proposition
   fixes s :: "'a::euclidean_space set set"
-  assumes "~ (affine_dependent (\<Union>s))"
+  assumes "\<not> affine_dependent (\<Union>s)"
     shows affine_hull_Inter: "affine hull (\<Inter>s) = (\<Inter>t\<in>s. affine hull t)" (is "?A")
       and convex_hull_Inter: "convex hull (\<Inter>s) = (\<Inter>t\<in>s. convex hull t)" (is "?C")
 proof -
@@ -6247,7 +6252,7 @@
 
 proposition in_convex_hull_exchange_unique:
   fixes S :: "'a::euclidean_space set"
-  assumes naff: "~ affine_dependent S" and a: "a \<in> convex hull S"
+  assumes naff: "\<not> affine_dependent S" and a: "a \<in> convex hull S"
       and S: "T \<subseteq> S" "T' \<subseteq> S"
       and x:  "x \<in> convex hull (insert a T)"
       and x': "x \<in> convex hull (insert a T')"
@@ -6395,7 +6400,7 @@
 
 corollary convex_hull_exchange_Int:
   fixes a  :: "'a::euclidean_space"
-  assumes "~ affine_dependent S" "a \<in> convex hull S" "T \<subseteq> S" "T' \<subseteq> S"
+  assumes "\<not> affine_dependent S" "a \<in> convex hull S" "T \<subseteq> S" "T' \<subseteq> S"
   shows "(convex hull (insert a T)) \<inter> (convex hull (insert a T')) =
          convex hull (insert a (T \<inter> T'))"
 apply (rule subset_antisym)
@@ -6404,7 +6409,7 @@
 
 lemma Int_closed_segment:
   fixes b :: "'a::euclidean_space"
-  assumes "b \<in> closed_segment a c \<or> ~collinear{a,b,c}"
+  assumes "b \<in> closed_segment a c \<or> \<not> collinear{a,b,c}"
     shows "closed_segment a b \<inter> closed_segment b c = {b}"
 proof (cases "c = a")
   case True
@@ -7283,7 +7288,7 @@
   fixes S :: "'a :: euclidean_space set"
   assumes "affine S"
       and "S \<inter> {x. a \<bullet> x \<le> b} \<noteq> {}"
-      and "~ (S \<subseteq> {x. a \<bullet> x \<le> b})"
+      and "\<not> (S \<subseteq> {x. a \<bullet> x \<le> b})"
   obtains a' b' where "a' \<noteq> 0"
                    "S \<inter> {x. a' \<bullet> x \<le> b'} = S \<inter> {x. a \<bullet> x \<le> b}"
                    "S \<inter> {x. a' \<bullet> x = b'} = S \<inter> {x. a \<bullet> x = b}"
@@ -7669,7 +7674,7 @@
   assumes opS: "openin (subtopology euclidean (S \<union> T)) S"
       and opT: "openin (subtopology euclidean (S \<union> T)) T"
       and contf: "continuous_on S f" and contg: "continuous_on T g"
-      and fg: "\<And>x. x \<in> S \<and> ~P x \<or> x \<in> T \<and> P x \<Longrightarrow> f x = g x"
+      and fg: "\<And>x. x \<in> S \<and> \<not>P x \<or> x \<in> T \<and> P x \<Longrightarrow> f x = g x"
     shows "continuous_on (S \<union> T) (\<lambda>x. if P x then f x else g x)"
 proof -
   have "\<And>x. x \<in> S \<Longrightarrow> (if P x then f x else g x) = f x"  "\<And>x. x \<in> T \<Longrightarrow> (if P x then f x else g x) = g x"
--- a/src/HOL/Analysis/Tagged_Division.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Tagged_Division.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -1360,7 +1360,7 @@
     and k: "k \<in> Basis"
   shows "division_points (cbox a b \<inter> {x. x\<bullet>k \<le> c}) {l \<inter> {x. x\<bullet>k \<le> c} | l . l \<in> d \<and> l \<inter> {x. x\<bullet>k \<le> c} \<noteq> {}} \<subseteq>
       division_points (cbox a b) d" (is ?t1)
-    and "division_points (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) {l \<inter> {x. x\<bullet>k \<ge> c} | l . l \<in> d \<and> ~(l \<inter> {x. x\<bullet>k \<ge> c} = {})} \<subseteq>
+    and "division_points (cbox a b \<inter> {x. x\<bullet>k \<ge> c}) {l \<inter> {x. x\<bullet>k \<ge> c} | l . l \<in> d \<and> \<not>(l \<inter> {x. x\<bullet>k \<ge> c} = {})} \<subseteq>
       division_points (cbox a b) d" (is ?t2)
 proof%unimportant -
   note assm = division_ofD[OF assms(1)]
@@ -2536,7 +2536,7 @@
     show "\<And>K. K \<in> ?D1 \<Longrightarrow> finite {L. L \<in> ?D1 \<and> K \<subseteq> L}"
       using fin by simp (metis (mono_tags, lifting) Collect_mono rev_finite_subset)
   qed
-  let ?\<D> = "{K \<in> \<D>. \<forall>K'. K' \<in> \<D> \<and> K \<noteq> K' \<longrightarrow> ~(K \<subseteq> K')}"
+  let ?\<D> = "{K \<in> \<D>. \<forall>K'. K' \<in> \<D> \<and> K \<noteq> K' \<longrightarrow> \<not>(K \<subseteq> K')}"
   show ?thesis
   proof (rule that)
     show "countable ?\<D>"
--- a/src/HOL/Analysis/Topology_Euclidean_Space.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Topology_Euclidean_Space.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -4,1402 +4,15 @@
     Author:     Brian Huffman, Portland State University
 *)
 
-section \<open>Elementary topology in Euclidean space\<close>
+section \<open>Elementary Topology in Euclidean Space\<close>
 
 theory Topology_Euclidean_Space
-imports
-  "HOL-Library.Indicator_Function"
-  "HOL-Library.Countable_Set"
-  "HOL-Library.FuncSet"
-  "HOL-Library.Set_Idioms"
-  Linear_Algebra
-  Norm_Arith
-begin
-
-(* FIXME: move elsewhere *)
-
-lemma halfspace_Int_eq:
-     "{x. a \<bullet> x \<le> b} \<inter> {x. b \<le> a \<bullet> x} = {x. a \<bullet> x = b}"
-     "{x. b \<le> a \<bullet> x} \<inter> {x. a \<bullet> x \<le> b} = {x. a \<bullet> x = b}"
-  by auto
-
-definition (in monoid_add) support_on :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'b set"
-  where "support_on s f = {x\<in>s. f x \<noteq> 0}"
-
-lemma in_support_on: "x \<in> support_on s f \<longleftrightarrow> x \<in> s \<and> f x \<noteq> 0"
-  by (simp add: support_on_def)
-
-lemma support_on_simps[simp]:
-  "support_on {} f = {}"
-  "support_on (insert x s) f =
-    (if f x = 0 then support_on s f else insert x (support_on s f))"
-  "support_on (s \<union> t) f = support_on s f \<union> support_on t f"
-  "support_on (s \<inter> t) f = support_on s f \<inter> support_on t f"
-  "support_on (s - t) f = support_on s f - support_on t f"
-  "support_on (f ` s) g = f ` (support_on s (g \<circ> f))"
-  unfolding support_on_def by auto
-
-lemma support_on_cong:
-  "(\<And>x. x \<in> s \<Longrightarrow> f x = 0 \<longleftrightarrow> g x = 0) \<Longrightarrow> support_on s f = support_on s g"
-  by (auto simp: support_on_def)
-
-lemma support_on_if: "a \<noteq> 0 \<Longrightarrow> support_on A (\<lambda>x. if P x then a else 0) = {x\<in>A. P x}"
-  by (auto simp: support_on_def)
-
-lemma support_on_if_subset: "support_on A (\<lambda>x. if P x then a else 0) \<subseteq> {x \<in> A. P x}"
-  by (auto simp: support_on_def)
-
-lemma finite_support[intro]: "finite S \<Longrightarrow> finite (support_on S f)"
-  unfolding support_on_def by auto
-
-(* TODO: is supp_sum really needed? TODO: Generalize to Finite_Set.fold *)
-definition (in comm_monoid_add) supp_sum :: "('b \<Rightarrow> 'a) \<Rightarrow> 'b set \<Rightarrow> 'a"
-  where "supp_sum f S = (\<Sum>x\<in>support_on S f. f x)"
-
-lemma supp_sum_empty[simp]: "supp_sum f {} = 0"
-  unfolding supp_sum_def by auto
-
-lemma supp_sum_insert[simp]:
-  "finite (support_on S f) \<Longrightarrow>
-    supp_sum f (insert x S) = (if x \<in> S then supp_sum f S else f x + supp_sum f S)"
-  by (simp add: supp_sum_def in_support_on insert_absorb)
-
-lemma supp_sum_divide_distrib: "supp_sum f A / (r::'a::field) = supp_sum (\<lambda>n. f n / r) A"
-  by (cases "r = 0")
-     (auto simp: supp_sum_def sum_divide_distrib intro!: sum.cong support_on_cong)
-
-(*END OF SUPPORT, ETC.*)
-
-lemma image_affinity_interval:
-  fixes c :: "'a::ordered_real_vector"
-  shows "((\<lambda>x. m *\<^sub>R x + c) ` {a..b}) = 
-           (if {a..b}={} then {}
-            else if 0 \<le> m then {m *\<^sub>R a + c .. m  *\<^sub>R b + c}
-            else {m *\<^sub>R b + c .. m *\<^sub>R a + c})"
-         (is "?lhs = ?rhs")
-proof (cases "m=0")
-  case True
-  then show ?thesis
-    by force
-next
-  case False
-  show ?thesis
-  proof
-    show "?lhs \<subseteq> ?rhs"
-      by (auto simp: scaleR_left_mono scaleR_left_mono_neg)
-    show "?rhs \<subseteq> ?lhs"
-    proof (clarsimp, intro conjI impI subsetI)
-      show "\<lbrakk>0 \<le> m; a \<le> b; x \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}\<rbrakk>
-            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
-        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
-        using False apply (auto simp: le_diff_eq pos_le_divideRI)
-        using diff_le_eq pos_le_divideR_eq by force
-      show "\<lbrakk>\<not> 0 \<le> m; a \<le> b;  x \<in> {m *\<^sub>R b + c..m *\<^sub>R a + c}\<rbrakk>
-            \<Longrightarrow> x \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}" for x
-        apply (rule_tac x="inverse m *\<^sub>R (x-c)" in rev_image_eqI)
-        apply (auto simp: diff_le_eq neg_le_divideR_eq)
-        using diff_eq_diff_less_eq linordered_field_class.sign_simps(11) minus_diff_eq not_less scaleR_le_cancel_left_neg by fastforce
-    qed
-  qed
-qed
-
-lemma countable_PiE:
-  "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (Pi\<^sub>E I F)"
-  by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)
-
-lemma open_sums:
-  fixes T :: "('b::real_normed_vector) set"
-  assumes "open S \<or> open T"
-  shows "open (\<Union>x\<in> S. \<Union>y \<in> T. {x + y})"
-  using assms
-proof
-  assume S: "open S"
-  show ?thesis
-  proof (clarsimp simp: open_dist)
-    fix x y
-    assume "x \<in> S" "y \<in> T"
-    with S obtain e where "e > 0" and e: "\<And>x'. dist x' x < e \<Longrightarrow> x' \<in> S"
-      by (auto simp: open_dist)
-    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
-      by (metis \<open>y \<in> T\<close> diff_add_cancel dist_add_cancel2)
-    then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
-      using \<open>0 < e\<close> \<open>x \<in> S\<close> by blast
-  qed
-next
-  assume T: "open T"
-  show ?thesis
-  proof (clarsimp simp: open_dist)
-    fix x y
-    assume "x \<in> S" "y \<in> T"
-    with T obtain e where "e > 0" and e: "\<And>x'. dist x' y < e \<Longrightarrow> x' \<in> T"
-      by (auto simp: open_dist)
-    then have "\<And>z. dist z (x + y) < e \<Longrightarrow> \<exists>x\<in>S. \<exists>y\<in>T. z = x + y"
-      by (metis \<open>x \<in> S\<close> add_diff_cancel_left' add_diff_eq diff_diff_add dist_norm)
-    then show "\<exists>e>0. \<forall>z. dist z (x + y) < e \<longrightarrow> (\<exists>x\<in>S. \<exists>y\<in>T. z = x + y)"
-      using \<open>0 < e\<close> \<open>y \<in> T\<close> by blast
-  qed
-qed
-
-
-subsection \<open>Topological Basis\<close>
-
-context topological_space
-begin
-
-definition%important "topological_basis B \<longleftrightarrow>
-  (\<forall>b\<in>B. open b) \<and> (\<forall>x. open x \<longrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
-
-lemma topological_basis:
-  "topological_basis B \<longleftrightarrow> (\<forall>x. open x \<longleftrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"
-  unfolding topological_basis_def
-  apply safe
-     apply fastforce
-    apply fastforce
-   apply (erule_tac x=x in allE, simp)
-   apply (rule_tac x="{x}" in exI, auto)
-  done
-
-lemma topological_basis_iff:
-  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
-  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'))"
-    (is "_ \<longleftrightarrow> ?rhs")
-proof safe
-  fix O' and x::'a
-  assume H: "topological_basis B" "open O'" "x \<in> O'"
-  then have "(\<exists>B'\<subseteq>B. \<Union>B' = O')" by (simp add: topological_basis_def)
-  then obtain B' where "B' \<subseteq> B" "O' = \<Union>B'" by auto
-  then show "\<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'" using H by auto
-next
-  assume H: ?rhs
-  show "topological_basis B"
-    using assms unfolding topological_basis_def
-  proof safe
-    fix O' :: "'a set"
-    assume "open O'"
-    with H obtain f where "\<forall>x\<in>O'. f x \<in> B \<and> x \<in> f x \<and> f x \<subseteq> O'"
-      by (force intro: bchoice simp: Bex_def)
-    then show "\<exists>B'\<subseteq>B. \<Union>B' = O'"
-      by (auto intro: exI[where x="{f x |x. x \<in> O'}"])
-  qed
-qed
-
-lemma topological_basisI:
-  assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"
-    and "\<And>O' x. open O' \<Longrightarrow> x \<in> O' \<Longrightarrow> \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'"
-  shows "topological_basis B"
-  using assms by (subst topological_basis_iff) auto
-
-lemma topological_basisE:
-  fixes O'
-  assumes "topological_basis B"
-    and "open O'"
-    and "x \<in> O'"
-  obtains B' where "B' \<in> B" "x \<in> B'" "B' \<subseteq> O'"
-proof atomize_elim
-  from assms have "\<And>B'. B'\<in>B \<Longrightarrow> open B'"
-    by (simp add: topological_basis_def)
-  with topological_basis_iff assms
-  show  "\<exists>B'. B' \<in> B \<and> x \<in> B' \<and> B' \<subseteq> O'"
-    using assms by (simp add: Bex_def)
-qed
-
-lemma topological_basis_open:
-  assumes "topological_basis B"
-    and "X \<in> B"
-  shows "open X"
-  using assms by (simp add: topological_basis_def)
-
-lemma topological_basis_imp_subbasis:
-  assumes B: "topological_basis B"
-  shows "open = generate_topology B"
-proof (intro ext iffI)
-  fix S :: "'a set"
-  assume "open S"
-  with B obtain B' where "B' \<subseteq> B" "S = \<Union>B'"
-    unfolding topological_basis_def by blast
-  then show "generate_topology B S"
-    by (auto intro: generate_topology.intros dest: topological_basis_open)
-next
-  fix S :: "'a set"
-  assume "generate_topology B S"
-  then show "open S"
-    by induct (auto dest: topological_basis_open[OF B])
-qed
-
-lemma basis_dense:
-  fixes B :: "'a set set"
-    and f :: "'a set \<Rightarrow> 'a"
-  assumes "topological_basis B"
-    and choosefrom_basis: "\<And>B'. B' \<noteq> {} \<Longrightarrow> f B' \<in> B'"
-  shows "\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>B' \<in> B. f B' \<in> X)"
-proof (intro allI impI)
-  fix X :: "'a set"
-  assume "open X" and "X \<noteq> {}"
-  from topological_basisE[OF \<open>topological_basis B\<close> \<open>open X\<close> choosefrom_basis[OF \<open>X \<noteq> {}\<close>]]
-  obtain B' where "B' \<in> B" "f X \<in> B'" "B' \<subseteq> X" .
-  then show "\<exists>B'\<in>B. f B' \<in> X"
-    by (auto intro!: choosefrom_basis)
-qed
-
-end
-
-lemma topological_basis_prod:
-  assumes A: "topological_basis A"
-    and B: "topological_basis B"
-  shows "topological_basis ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))"
-  unfolding topological_basis_def
-proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric])
-  fix S :: "('a \<times> 'b) set"
-  assume "open S"
-  then show "\<exists>X\<subseteq>A \<times> B. (\<Union>(a,b)\<in>X. a \<times> b) = S"
-  proof (safe intro!: exI[of _ "{x\<in>A \<times> B. fst x \<times> snd x \<subseteq> S}"])
-    fix x y
-    assume "(x, y) \<in> S"
-    from open_prod_elim[OF \<open>open S\<close> this]
-    obtain a b where a: "open a""x \<in> a" and b: "open b" "y \<in> b" and "a \<times> b \<subseteq> S"
-      by (metis mem_Sigma_iff)
-    moreover
-    from A a obtain A0 where "A0 \<in> A" "x \<in> A0" "A0 \<subseteq> a"
-      by (rule topological_basisE)
-    moreover
-    from B b obtain B0 where "B0 \<in> B" "y \<in> B0" "B0 \<subseteq> b"
-      by (rule topological_basisE)
-    ultimately show "(x, y) \<in> (\<Union>(a, b)\<in>{X \<in> A \<times> B. fst X \<times> snd X \<subseteq> S}. a \<times> b)"
-      by (intro UN_I[of "(A0, B0)"]) auto
-  qed auto
-qed (metis A B topological_basis_open open_Times)
-
-
-subsection \<open>Countable Basis\<close>
-
-locale%important countable_basis =
-  fixes B :: "'a::topological_space set set"
-  assumes is_basis: "topological_basis B"
-    and countable_basis: "countable B"
-begin
-
-lemma open_countable_basis_ex:
-  assumes "open X"
-  shows "\<exists>B' \<subseteq> B. X = \<Union>B'"
-  using assms countable_basis is_basis
-  unfolding topological_basis_def by blast
-
-lemma open_countable_basisE:
-  assumes "open X"
-  obtains B' where "B' \<subseteq> B" "X = \<Union>B'"
-  using assms open_countable_basis_ex
-  by atomize_elim simp
-
-lemma countable_dense_exists:
-  "\<exists>D::'a set. countable D \<and> (\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>d \<in> D. d \<in> X))"
-proof -
-  let ?f = "(\<lambda>B'. SOME x. x \<in> B')"
-  have "countable (?f ` B)" using countable_basis by simp
-  with basis_dense[OF is_basis, of ?f] show ?thesis
-    by (intro exI[where x="?f ` B"]) (metis (mono_tags) all_not_in_conv imageI someI)
-qed
-
-lemma countable_dense_setE:
-  obtains D :: "'a set"
-  where "countable D" "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d \<in> D. d \<in> X"
-  using countable_dense_exists by blast
-
-end
-
-lemma (in first_countable_topology) first_countable_basisE:
-  fixes x :: 'a
-  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
-    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
-proof -
-  obtain \<A> where \<A>: "(\<forall>i::nat. x \<in> \<A> i \<and> open (\<A> i))" "(\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. \<A> i \<subseteq> S))"
-    using first_countable_basis[of x] by metis
-  show thesis
-  proof 
-    show "countable (range \<A>)"
-      by simp
-  qed (use \<A> in auto)
-qed
-
-lemma (in first_countable_topology) first_countable_basis_Int_stableE:
-  obtains \<A> where "countable \<A>" "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
-    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>A\<in>\<A>. A \<subseteq> S)"
-    "\<And>A B. A \<in> \<A> \<Longrightarrow> B \<in> \<A> \<Longrightarrow> A \<inter> B \<in> \<A>"
-proof atomize_elim
-  obtain \<B> where \<B>:
-    "countable \<B>"
-    "\<And>B. B \<in> \<B> \<Longrightarrow> x \<in> B"
-    "\<And>B. B \<in> \<B> \<Longrightarrow> open B"
-    "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>B\<in>\<B>. B \<subseteq> S"
-    by (rule first_countable_basisE) blast
-  define \<A> where [abs_def]:
-    "\<A> = (\<lambda>N. \<Inter>((\<lambda>n. from_nat_into \<B> n) ` N)) ` (Collect finite::nat set set)"
-  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>
-        (\<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>)"
-  proof (safe intro!: exI[where x=\<A>])
-    show "countable \<A>"
-      unfolding \<A>_def by (intro countable_image countable_Collect_finite)
-    fix A
-    assume "A \<in> \<A>"
-    then show "x \<in> A" "open A"
-      using \<B>(4)[OF open_UNIV] by (auto simp: \<A>_def intro: \<B> from_nat_into)
-  next
-    let ?int = "\<lambda>N. \<Inter>(from_nat_into \<B> ` N)"
-    fix A B
-    assume "A \<in> \<A>" "B \<in> \<A>"
-    then obtain N M where "A = ?int N" "B = ?int M" "finite (N \<union> M)"
-      by (auto simp: \<A>_def)
-    then show "A \<inter> B \<in> \<A>"
-      by (auto simp: \<A>_def intro!: image_eqI[where x="N \<union> M"])
-  next
-    fix S
-    assume "open S" "x \<in> S"
-    then obtain a where a: "a\<in>\<B>" "a \<subseteq> S" using \<B> by blast
-    then show "\<exists>a\<in>\<A>. a \<subseteq> S" using a \<B>
-      by (intro bexI[where x=a]) (auto simp: \<A>_def intro: image_eqI[where x="{to_nat_on \<B> a}"])
-  qed
-qed
-
-lemma (in topological_space) first_countableI:
-  assumes "countable \<A>"
-    and 1: "\<And>A. A \<in> \<A> \<Longrightarrow> x \<in> A" "\<And>A. A \<in> \<A> \<Longrightarrow> open A"
-    and 2: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>A\<in>\<A>. A \<subseteq> S"
-  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))"
-proof (safe intro!: exI[of _ "from_nat_into \<A>"])
-  fix i
-  have "\<A> \<noteq> {}" using 2[of UNIV] by auto
-  show "x \<in> from_nat_into \<A> i" "open (from_nat_into \<A> i)"
-    using range_from_nat_into_subset[OF \<open>\<A> \<noteq> {}\<close>] 1 by auto
-next
-  fix S
-  assume "open S" "x\<in>S" from 2[OF this]
-  show "\<exists>i. from_nat_into \<A> i \<subseteq> S"
-    using subset_range_from_nat_into[OF \<open>countable \<A>\<close>] by auto
-qed
-
-instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology
-proof
-  fix x :: "'a \<times> 'b"
-  obtain \<A> where \<A>:
-      "countable \<A>"
-      "\<And>a. a \<in> \<A> \<Longrightarrow> fst x \<in> a"
-      "\<And>a. a \<in> \<A> \<Longrightarrow> open a"
-      "\<And>S. open S \<Longrightarrow> fst x \<in> S \<Longrightarrow> \<exists>a\<in>\<A>. a \<subseteq> S"
-    by (rule first_countable_basisE[of "fst x"]) blast
-  obtain B where B:
-      "countable B"
-      "\<And>a. a \<in> B \<Longrightarrow> snd x \<in> a"
-      "\<And>a. a \<in> B \<Longrightarrow> open a"
-      "\<And>S. open S \<Longrightarrow> snd x \<in> S \<Longrightarrow> \<exists>a\<in>B. a \<subseteq> S"
-    by (rule first_countable_basisE[of "snd x"]) blast
-  show "\<exists>\<A>::nat \<Rightarrow> ('a \<times> 'b) 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))"
-  proof (rule first_countableI[of "(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B)"], safe)
-    fix a b
-    assume x: "a \<in> \<A>" "b \<in> B"
-    show "x \<in> a \<times> b" 
-      by (simp add: \<A>(2) B(2) mem_Times_iff x)
-    show "open (a \<times> b)"
-      by (simp add: \<A>(3) B(3) open_Times x)
-  next
-    fix S
-    assume "open S" "x \<in> S"
-    then obtain a' b' where a'b': "open a'" "open b'" "x \<in> a' \<times> b'" "a' \<times> b' \<subseteq> S"
-      by (rule open_prod_elim)
-    moreover
-    from a'b' \<A>(4)[of a'] B(4)[of b']
-    obtain a b where "a \<in> \<A>" "a \<subseteq> a'" "b \<in> B" "b \<subseteq> b'"
-      by auto
-    ultimately
-    show "\<exists>a\<in>(\<lambda>(a, b). a \<times> b) ` (\<A> \<times> B). a \<subseteq> S"
-      by (auto intro!: bexI[of _ "a \<times> b"] bexI[of _ a] bexI[of _ b])
-  qed (simp add: \<A> B)
-qed
-
-class second_countable_topology = topological_space +
-  assumes ex_countable_subbasis:
-    "\<exists>B::'a::topological_space set set. countable B \<and> open = generate_topology B"
+  imports
+    Elementary_Topology
+    Linear_Algebra
+    Norm_Arith
 begin
 
-lemma ex_countable_basis: "\<exists>B::'a set set. countable B \<and> topological_basis B"
-proof -
-  from ex_countable_subbasis obtain B where B: "countable B" "open = generate_topology B"
-    by blast
-  let ?B = "Inter ` {b. finite b \<and> b \<subseteq> B }"
-
-  show ?thesis
-  proof (intro exI conjI)
-    show "countable ?B"
-      by (intro countable_image countable_Collect_finite_subset B)
-    {
-      fix S
-      assume "open S"
-      then have "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. (\<Union>b\<in>B'. \<Inter>b) = S"
-        unfolding B
-      proof induct
-        case UNIV
-        show ?case by (intro exI[of _ "{{}}"]) simp
-      next
-        case (Int a b)
-        then obtain x y where x: "a = \<Union>(Inter ` x)" "\<And>i. i \<in> x \<Longrightarrow> finite i \<and> i \<subseteq> B"
-          and y: "b = \<Union>(Inter ` y)" "\<And>i. i \<in> y \<Longrightarrow> finite i \<and> i \<subseteq> B"
-          by blast
-        show ?case
-          unfolding x y Int_UN_distrib2
-          by (intro exI[of _ "{i \<union> j| i j.  i \<in> x \<and> j \<in> y}"]) (auto dest: x(2) y(2))
-      next
-        case (UN K)
-        then have "\<forall>k\<in>K. \<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = k" by auto
-        then obtain k where
-            "\<forall>ka\<in>K. k ka \<subseteq> {b. finite b \<and> b \<subseteq> B} \<and> \<Union>(Inter ` (k ka)) = ka"
-          unfolding bchoice_iff ..
-        then show "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. \<Union> (Inter ` B') = \<Union>K"
-          by (intro exI[of _ "\<Union>(k ` K)"]) auto
-      next
-        case (Basis S)
-        then show ?case
-          by (intro exI[of _ "{{S}}"]) auto
-      qed
-      then have "(\<exists>B'\<subseteq>Inter ` {b. finite b \<and> b \<subseteq> B}. \<Union>B' = S)"
-        unfolding subset_image_iff by blast }
-    then show "topological_basis ?B"
-      unfolding topological_space_class.topological_basis_def
-      by (safe intro!: topological_space_class.open_Inter)
-         (simp_all add: B generate_topology.Basis subset_eq)
-  qed
-qed
-
-end
-
-sublocale second_countable_topology <
-  countable_basis "SOME B. countable B \<and> topological_basis B"
-  using someI_ex[OF ex_countable_basis]
-  by unfold_locales safe
-
-instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology
-proof
-  obtain A :: "'a set set" where "countable A" "topological_basis A"
-    using ex_countable_basis by auto
-  moreover
-  obtain B :: "'b set set" where "countable B" "topological_basis B"
-    using ex_countable_basis by auto
-  ultimately show "\<exists>B::('a \<times> 'b) set set. countable B \<and> open = generate_topology B"
-    by (auto intro!: exI[of _ "(\<lambda>(a, b). a \<times> b) ` (A \<times> B)"] topological_basis_prod
-      topological_basis_imp_subbasis)
-qed
-
-instance second_countable_topology \<subseteq> first_countable_topology
-proof
-  fix x :: 'a
-  define B :: "'a set set" where "B = (SOME B. countable B \<and> topological_basis B)"
-  then have B: "countable B" "topological_basis B"
-    using countable_basis is_basis
-    by (auto simp: countable_basis is_basis)
-  then show "\<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))"
-    by (intro first_countableI[of "{b\<in>B. x \<in> b}"])
-       (fastforce simp: topological_space_class.topological_basis_def)+
-qed
-
-instance nat :: second_countable_topology
-proof
-  show "\<exists>B::nat set set. countable B \<and> open = generate_topology B"
-    by (intro exI[of _ "range lessThan \<union> range greaterThan"]) (auto simp: open_nat_def)
-qed
-
-lemma countable_separating_set_linorder1:
-  shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y))"
-proof -
-  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
-  define B1 where "B1 = {(LEAST x. x \<in> U)| U. U \<in> A}"
-  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
-  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
-  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
-  have "\<exists>b \<in> B1 \<union> B2. x < b \<and> b \<le> y" if "x < y" for x y
-  proof (cases)
-    assume "\<exists>z. x < z \<and> z < y"
-    then obtain z where z: "x < z \<and> z < y" by auto
-    define U where "U = {x<..<y}"
-    then have "open U" by simp
-    moreover have "z \<in> U" using z U_def by simp
-    ultimately obtain V where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
-    define w where "w = (SOME x. x \<in> V)"
-    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
-    then have "x < w \<and> w \<le> y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
-    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
-    ultimately show ?thesis by auto
-  next
-    assume "\<not>(\<exists>z. x < z \<and> z < y)"
-    then have *: "\<And>z. z > x \<Longrightarrow> z \<ge> y" by auto
-    define U where "U = {x<..}"
-    then have "open U" by simp
-    moreover have "y \<in> U" using \<open>x < y\<close> U_def by simp
-    ultimately obtain "V" where "V \<in> A" "y \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
-    have "U = {y..}" unfolding U_def using * \<open>x < y\<close> by auto
-    then have "V \<subseteq> {y..}" using \<open>V \<subseteq> U\<close> by simp
-    then have "(LEAST w. w \<in> V) = y" using \<open>y \<in> V\<close> by (meson Least_equality atLeast_iff subsetCE)
-    then have "y \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
-    moreover have "x < y \<and> y \<le> y" using \<open>x < y\<close> by simp
-    ultimately show ?thesis by auto
-  qed
-  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
-  ultimately show ?thesis by auto
-qed
-
-lemma countable_separating_set_linorder2:
-  shows "\<exists>B::('a::{linorder_topology, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x \<le> b \<and> b < y))"
-proof -
-  obtain A::"'a set set" where "countable A" "topological_basis A" using ex_countable_basis by auto
-  define B1 where "B1 = {(GREATEST x. x \<in> U) | U. U \<in> A}"
-  then have "countable B1" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
-  define B2 where "B2 = {(SOME x. x \<in> U)| U. U \<in> A}"
-  then have "countable B2" using \<open>countable A\<close> by (simp add: Setcompr_eq_image)
-  have "\<exists>b \<in> B1 \<union> B2. x \<le> b \<and> b < y" if "x < y" for x y
-  proof (cases)
-    assume "\<exists>z. x < z \<and> z < y"
-    then obtain z where z: "x < z \<and> z < y" by auto
-    define U where "U = {x<..<y}"
-    then have "open U" by simp
-    moreover have "z \<in> U" using z U_def by simp
-    ultimately obtain "V" where "V \<in> A" "z \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
-    define w where "w = (SOME x. x \<in> V)"
-    then have "w \<in> V" using \<open>z \<in> V\<close> by (metis someI2)
-    then have "x \<le> w \<and> w < y" using \<open>w \<in> V\<close> \<open>V \<subseteq> U\<close> U_def by fastforce
-    moreover have "w \<in> B1 \<union> B2" using w_def B2_def \<open>V \<in> A\<close> by auto
-    ultimately show ?thesis by auto
-  next
-    assume "\<not>(\<exists>z. x < z \<and> z < y)"
-    then have *: "\<And>z. z < y \<Longrightarrow> z \<le> x" using leI by blast
-    define U where "U = {..<y}"
-    then have "open U" by simp
-    moreover have "x \<in> U" using \<open>x < y\<close> U_def by simp
-    ultimately obtain "V" where "V \<in> A" "x \<in> V" "V \<subseteq> U" using topological_basisE[OF \<open>topological_basis A\<close>] by auto
-    have "U = {..x}" unfolding U_def using * \<open>x < y\<close> by auto
-    then have "V \<subseteq> {..x}" using \<open>V \<subseteq> U\<close> by simp
-    then have "(GREATEST x. x \<in> V) = x" using \<open>x \<in> V\<close> by (meson Greatest_equality atMost_iff subsetCE)
-    then have "x \<in> B1 \<union> B2" using \<open>V \<in> A\<close> B1_def by auto
-    moreover have "x \<le> x \<and> x < y" using \<open>x < y\<close> by simp
-    ultimately show ?thesis by auto
-  qed
-  moreover have "countable (B1 \<union> B2)" using \<open>countable B1\<close> \<open>countable B2\<close> by simp
-  ultimately show ?thesis by auto
-qed
-
-lemma countable_separating_set_dense_linorder:
-  shows "\<exists>B::('a::{linorder_topology, dense_linorder, second_countable_topology} set). countable B \<and> (\<forall>x y. x < y \<longrightarrow> (\<exists>b \<in> B. x < b \<and> b < y))"
-proof -
-  obtain B::"'a set" where B: "countable B" "\<And>x y. x < y \<Longrightarrow> (\<exists>b \<in> B. x < b \<and> b \<le> y)"
-    using countable_separating_set_linorder1 by auto
-  have "\<exists>b \<in> B. x < b \<and> b < y" if "x < y" for x y
-  proof -
-    obtain z where "x < z" "z < y" using \<open>x < y\<close> dense by blast
-    then obtain b where "b \<in> B" "x < b \<and> b \<le> z" using B(2) by auto
-    then have "x < b \<and> b < y" using \<open>z < y\<close> by auto
-    then show ?thesis using \<open>b \<in> B\<close> by auto
-  qed
-  then show ?thesis using B(1) by auto
-qed
-
-subsection%important \<open>Polish spaces\<close>
-
-text \<open>Textbooks define Polish spaces as completely metrizable.
-  We assume the topology to be complete for a given metric.\<close>
-
-class polish_space = complete_space + second_countable_topology
-
-subsection \<open>General notion of a topology as a value\<close>
-
-definition%important "istopology L \<longleftrightarrow>
-  L {} \<and> (\<forall>S T. L S \<longrightarrow> L T \<longrightarrow> L (S \<inter> T)) \<and> (\<forall>K. Ball K L \<longrightarrow> L (\<Union>K))"
-
-typedef%important 'a topology = "{L::('a set) \<Rightarrow> bool. istopology L}"
-  morphisms "openin" "topology"
-  unfolding istopology_def by blast
-
-lemma istopology_openin[intro]: "istopology(openin U)"
-  using openin[of U] by blast
-
-lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"
-  using topology_inverse[unfolded mem_Collect_eq] .
-
-lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"
-  using topology_inverse[of U] istopology_openin[of "topology U"] by auto
-
-lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"
-proof
-  assume "T1 = T2"
-  then show "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp
-next
-  assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"
-  then have "openin T1 = openin T2" by (simp add: fun_eq_iff)
-  then have "topology (openin T1) = topology (openin T2)" by simp
-  then show "T1 = T2" unfolding openin_inverse .
-qed
-
-
-text\<open>The "universe": the union of all sets in the topology.\<close>
-definition "topspace T = \<Union>{S. openin T S}"
-
-subsubsection \<open>Main properties of open sets\<close>
-
-proposition openin_clauses:
-  fixes U :: "'a topology"
-  shows
-    "openin U {}"
-    "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"
-    "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"
-  using openin[of U] unfolding istopology_def mem_Collect_eq by fast+
-
-lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"
-  unfolding topspace_def by blast
-
-lemma openin_empty[simp]: "openin U {}"
-  by (rule openin_clauses)
-
-lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"
-  by (rule openin_clauses)
-
-lemma openin_Union[intro]: "(\<And>S. S \<in> K \<Longrightarrow> openin U S) \<Longrightarrow> openin U (\<Union>K)"
-  using openin_clauses by blast
-
-lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"
-  using openin_Union[of "{S,T}" U] by auto
-
-lemma openin_topspace[intro, simp]: "openin U (topspace U)"
-  by (force simp: openin_Union topspace_def)
-
-lemma openin_subopen: "openin U S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>T. openin U T \<and> x \<in> T \<and> T \<subseteq> S)"
-  (is "?lhs \<longleftrightarrow> ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs by auto
-next
-  assume H: ?rhs
-  let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"
-  have "openin U ?t" by (force simp: openin_Union)
-  also have "?t = S" using H by auto
-  finally show "openin U S" .
-qed
-
-lemma openin_INT [intro]:
-  assumes "finite I"
-          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
-  shows "openin T ((\<Inter>i \<in> I. U i) \<inter> topspace T)"
-using assms by (induct, auto simp: inf_sup_aci(2) openin_Int)
-
-lemma openin_INT2 [intro]:
-  assumes "finite I" "I \<noteq> {}"
-          "\<And>i. i \<in> I \<Longrightarrow> openin T (U i)"
-  shows "openin T (\<Inter>i \<in> I. U i)"
-proof -
-  have "(\<Inter>i \<in> I. U i) \<subseteq> topspace T"
-    using \<open>I \<noteq> {}\<close> openin_subset[OF assms(3)] by auto
-  then show ?thesis
-    using openin_INT[of _ _ U, OF assms(1) assms(3)] by (simp add: inf.absorb2 inf_commute)
-qed
-
-lemma openin_Inter [intro]:
-  assumes "finite \<F>" "\<F> \<noteq> {}" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (\<Inter>\<F>)"
-  by (metis (full_types) assms openin_INT2 image_ident)
-
-lemma openin_Int_Inter:
-  assumes "finite \<F>" "openin T U" "\<And>X. X \<in> \<F> \<Longrightarrow> openin T X" shows "openin T (U \<inter> \<Inter>\<F>)"
-  using openin_Inter [of "insert U \<F>"] assms by auto
-
-
-subsubsection \<open>Closed sets\<close>
-
-definition%important "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"
-
-lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U"
-  by (metis closedin_def)
-
-lemma closedin_empty[simp]: "closedin U {}"
-  by (simp add: closedin_def)
-
-lemma closedin_topspace[intro, simp]: "closedin U (topspace U)"
-  by (simp add: closedin_def)
-
-lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"
-  by (auto simp: Diff_Un closedin_def)
-
-lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union>{A - s|s. s\<in>S}"
-  by auto
-
-lemma closedin_Union:
-  assumes "finite S" "\<And>T. T \<in> S \<Longrightarrow> closedin U T"
-    shows "closedin U (\<Union>S)"
-  using assms by induction auto
-
-lemma closedin_Inter[intro]:
-  assumes Ke: "K \<noteq> {}"
-    and Kc: "\<And>S. S \<in>K \<Longrightarrow> closedin U S"
-  shows "closedin U (\<Inter>K)"
-  using Ke Kc unfolding closedin_def Diff_Inter by auto
-
-lemma closedin_INT[intro]:
-  assumes "A \<noteq> {}" "\<And>x. x \<in> A \<Longrightarrow> closedin U (B x)"
-  shows "closedin U (\<Inter>x\<in>A. B x)"
-  apply (rule closedin_Inter)
-  using assms
-  apply auto
-  done
-
-lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"
-  using closedin_Inter[of "{S,T}" U] by auto
-
-lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"
-  apply (auto simp: closedin_def Diff_Diff_Int inf_absorb2)
-  apply (metis openin_subset subset_eq)
-  done
-
-lemma openin_closedin: "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"
-  by (simp add: openin_closedin_eq)
-
-lemma openin_diff[intro]:
-  assumes oS: "openin U S"
-    and cT: "closedin U T"
-  shows "openin U (S - T)"
-proof -
-  have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT
-    by (auto simp: topspace_def openin_subset)
-  then show ?thesis using oS cT
-    by (auto simp: closedin_def)
-qed
-
-lemma closedin_diff[intro]:
-  assumes oS: "closedin U S"
-    and cT: "openin U T"
-  shows "closedin U (S - T)"
-proof -
-  have "S - T = S \<inter> (topspace U - T)"
-    using closedin_subset[of U S] oS cT by (auto simp: topspace_def)
-  then show ?thesis
-    using oS cT by (auto simp: openin_closedin_eq)
-qed
-
-
-subsection\<open>The discrete topology\<close>
-
-definition discrete_topology where "discrete_topology U \<equiv> topology (\<lambda>S. S \<subseteq> U)"
-
-lemma openin_discrete_topology [simp]: "openin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
-proof -
-  have "istopology (\<lambda>S. S \<subseteq> U)"
-    by (auto simp: istopology_def)
-  then show ?thesis
-    by (simp add: discrete_topology_def topology_inverse')
-qed
-
-lemma topspace_discrete_topology [simp]: "topspace(discrete_topology U) = U"
-  by (meson openin_discrete_topology openin_subset openin_topspace order_refl subset_antisym)
-
-lemma closedin_discrete_topology [simp]: "closedin (discrete_topology U) S \<longleftrightarrow> S \<subseteq> U"
-  by (simp add: closedin_def)
-
-lemma discrete_topology_unique:
-   "discrete_topology U = X \<longleftrightarrow> topspace X = U \<and> (\<forall>x \<in> U. openin X {x})" (is "?lhs = ?rhs")
-proof
-  assume R: ?rhs
-  then have "openin X S" if "S \<subseteq> U" for S
-    using openin_subopen subsetD that by fastforce
-  moreover have "x \<in> topspace X" if "openin X S" and "x \<in> S" for x S
-    using openin_subset that by blast
-  ultimately
-  show ?lhs
-    using R by (auto simp: topology_eq)
-qed auto
-
-lemma discrete_topology_unique_alt:
-  "discrete_topology U = X \<longleftrightarrow> topspace X \<subseteq> U \<and> (\<forall>x \<in> U. openin X {x})"
-  using openin_subset
-  by (auto simp: discrete_topology_unique)
-
-lemma subtopology_eq_discrete_topology_empty:
-   "X = discrete_topology {} \<longleftrightarrow> topspace X = {}"
-  using discrete_topology_unique [of "{}" X] by auto
-
-lemma subtopology_eq_discrete_topology_sing:
-   "X = discrete_topology {a} \<longleftrightarrow> topspace X = {a}"
-  by (metis discrete_topology_unique openin_topspace singletonD)
-
-
-subsection \<open>Subspace topology\<close>
-
-definition%important "subtopology U V = topology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
-
-lemma istopology_subtopology: "istopology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"
-  (is "istopology ?L")
-proof -
-  have "?L {}" by blast
-  {
-    fix A B
-    assume A: "?L A" and B: "?L B"
-    from A B obtain Sa and Sb where Sa: "openin U Sa" "A = Sa \<inter> V" and Sb: "openin U Sb" "B = Sb \<inter> V"
-      by blast
-    have "A \<inter> B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"
-      using Sa Sb by blast+
-    then have "?L (A \<inter> B)" by blast
-  }
-  moreover
-  {
-    fix K
-    assume K: "K \<subseteq> Collect ?L"
-    have th0: "Collect ?L = (\<lambda>S. S \<inter> V) ` Collect (openin U)"
-      by blast
-    from K[unfolded th0 subset_image_iff]
-    obtain Sk where Sk: "Sk \<subseteq> Collect (openin U)" "K = (\<lambda>S. S \<inter> V) ` Sk"
-      by blast
-    have "\<Union>K = (\<Union>Sk) \<inter> V"
-      using Sk by auto
-    moreover have "openin U (\<Union>Sk)"
-      using Sk by (auto simp: subset_eq)
-    ultimately have "?L (\<Union>K)" by blast
-  }
-  ultimately show ?thesis
-    unfolding subset_eq mem_Collect_eq istopology_def by auto
-qed
-
-lemma openin_subtopology: "openin (subtopology U V) S \<longleftrightarrow> (\<exists>T. openin U T \<and> S = T \<inter> V)"
-  unfolding subtopology_def topology_inverse'[OF istopology_subtopology]
-  by auto
-
-lemma openin_subtopology_Int:
-   "openin X S \<Longrightarrow> openin (subtopology X T) (S \<inter> T)"
-  using openin_subtopology by auto
-
-lemma openin_subtopology_Int2:
-   "openin X T \<Longrightarrow> openin (subtopology X S) (S \<inter> T)"
-  using openin_subtopology by auto
-
-lemma openin_subtopology_diff_closed:
-   "\<lbrakk>S \<subseteq> topspace X; closedin X T\<rbrakk> \<Longrightarrow> openin (subtopology X S) (S - T)"
-  unfolding closedin_def openin_subtopology
-  by (rule_tac x="topspace X - T" in exI) auto
-
-lemma openin_relative_to: "(openin X relative_to S) = openin (subtopology X S)"
-  by (force simp: relative_to_def openin_subtopology)
-
-lemma topspace_subtopology: "topspace (subtopology U V) = topspace U \<inter> V"
-  by (auto simp: topspace_def openin_subtopology)
-
-lemma closedin_subtopology: "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"
-  unfolding closedin_def topspace_subtopology
-  by (auto simp: openin_subtopology)
-
-lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"
-  unfolding openin_subtopology
-  by auto (metis IntD1 in_mono openin_subset)
-
-lemma subtopology_subtopology:
-   "subtopology (subtopology X S) T = subtopology X (S \<inter> T)"
-proof -
-  have eq: "\<And>T'. (\<exists>S'. T' = S' \<inter> T \<and> (\<exists>T. openin X T \<and> S' = T \<inter> S)) = (\<exists>Sa. T' = Sa \<inter> (S \<inter> T) \<and> openin X Sa)"
-    by (metis inf_assoc)
-  have "subtopology (subtopology X S) T = topology (\<lambda>Ta. \<exists>Sa. Ta = Sa \<inter> T \<and> openin (subtopology X S) Sa)"
-    by (simp add: subtopology_def)
-  also have "\<dots> = subtopology X (S \<inter> T)"
-    by (simp add: openin_subtopology eq) (simp add: subtopology_def)
-  finally show ?thesis .
-qed
-
-lemma openin_subtopology_alt:
-     "openin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (openin X)"
-  by (simp add: image_iff inf_commute openin_subtopology)
-
-lemma closedin_subtopology_alt:
-     "closedin (subtopology X U) S \<longleftrightarrow> S \<in> (\<lambda>T. U \<inter> T) ` Collect (closedin X)"
-  by (simp add: image_iff inf_commute closedin_subtopology)
-
-lemma subtopology_superset:
-  assumes UV: "topspace U \<subseteq> V"
-  shows "subtopology U V = U"
-proof -
-  {
-    fix S
-    {
-      fix T
-      assume T: "openin U T" "S = T \<inter> V"
-      from T openin_subset[OF T(1)] UV have eq: "S = T"
-        by blast
-      have "openin U S"
-        unfolding eq using T by blast
-    }
-    moreover
-    {
-      assume S: "openin U S"
-      then have "\<exists>T. openin U T \<and> S = T \<inter> V"
-        using openin_subset[OF S] UV by auto
-    }
-    ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S"
-      by blast
-  }
-  then show ?thesis
-    unfolding topology_eq openin_subtopology by blast
-qed
-
-lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"
-  by (simp add: subtopology_superset)
-
-lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"
-  by (simp add: subtopology_superset)
-
-lemma openin_subtopology_empty:
-   "openin (subtopology U {}) S \<longleftrightarrow> S = {}"
-by (metis Int_empty_right openin_empty openin_subtopology)
-
-lemma closedin_subtopology_empty:
-   "closedin (subtopology U {}) S \<longleftrightarrow> S = {}"
-by (metis Int_empty_right closedin_empty closedin_subtopology)
-
-lemma closedin_subtopology_refl [simp]:
-   "closedin (subtopology U X) X \<longleftrightarrow> X \<subseteq> topspace U"
-by (metis closedin_def closedin_topspace inf.absorb_iff2 le_inf_iff topspace_subtopology)
-
-lemma closedin_topspace_empty: "topspace T = {} \<Longrightarrow> (closedin T S \<longleftrightarrow> S = {})"
-  by (simp add: closedin_def)
-
-lemma openin_imp_subset:
-   "openin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
-by (metis Int_iff openin_subtopology subsetI)
-
-lemma closedin_imp_subset:
-   "closedin (subtopology U S) T \<Longrightarrow> T \<subseteq> S"
-by (simp add: closedin_def topspace_subtopology)
-
-lemma openin_open_subtopology:
-     "openin X S \<Longrightarrow> openin (subtopology X S) T \<longleftrightarrow> openin X T \<and> T \<subseteq> S"
-  by (metis inf.orderE openin_Int openin_imp_subset openin_subtopology)
-
-lemma closedin_closed_subtopology:
-     "closedin X S \<Longrightarrow> (closedin (subtopology X S) T \<longleftrightarrow> closedin X T \<and> T \<subseteq> S)"
-  by (metis closedin_Int closedin_imp_subset closedin_subtopology inf.orderE)
-
-lemma openin_subtopology_Un:
-    "\<lbrakk>openin (subtopology X T) S; openin (subtopology X U) S\<rbrakk>
-     \<Longrightarrow> openin (subtopology X (T \<union> U)) S"
-by (simp add: openin_subtopology) blast
-
-lemma closedin_subtopology_Un:
-    "\<lbrakk>closedin (subtopology X T) S; closedin (subtopology X U) S\<rbrakk>
-     \<Longrightarrow> closedin (subtopology X (T \<union> U)) S"
-by (simp add: closedin_subtopology) blast
-
-
-subsection \<open>The standard Euclidean topology\<close>
-
-definition%important euclidean :: "'a::topological_space topology"
-  where "euclidean = topology open"
-
-lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"
-  unfolding euclidean_def
-  apply (rule cong[where x=S and y=S])
-  apply (rule topology_inverse[symmetric])
-  apply (auto simp: istopology_def)
-  done
-
-declare open_openin [symmetric, simp]
-
-lemma topspace_euclidean [simp]: "topspace euclidean = UNIV"
-  by (force simp: topspace_def)
-
-lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"
-  by (simp add: topspace_subtopology)
-
-lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"
-  by (simp add: closed_def closedin_def Compl_eq_Diff_UNIV)
-
-declare closed_closedin [symmetric, simp]
-
-lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"
-  using openI by auto
-
-lemma openin_subtopology_self [simp]: "openin (subtopology euclidean S) S"
-  by (metis openin_topspace topspace_euclidean_subtopology)
-
-subsubsection\<open>The most basic facts about the usual topology and metric on R\<close>
-
-abbreviation euclideanreal :: "real topology"
-  where "euclideanreal \<equiv> topology open"
-
-lemma real_openin [simp]: "openin euclideanreal S = open S"
-  by (simp add: euclidean_def open_openin)
-
-lemma topspace_euclideanreal [simp]: "topspace euclideanreal = UNIV"
-  using openin_subset open_UNIV real_openin by blast
-
-lemma topspace_euclideanreal_subtopology [simp]:
-   "topspace (subtopology euclideanreal S) = S"
-  by (simp add: topspace_subtopology)
-
-lemma real_closedin [simp]: "closedin euclideanreal S = closed S"
-  by (simp add: closed_closedin euclidean_def)
-
-subsection \<open>Basic "localization" results are handy for connectedness.\<close>
-
-lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"
-  by (auto simp: openin_subtopology)
-
-lemma openin_Int_open:
-   "\<lbrakk>openin (subtopology euclidean U) S; open T\<rbrakk>
-        \<Longrightarrow> openin (subtopology euclidean U) (S \<inter> T)"
-by (metis open_Int Int_assoc openin_open)
-
-lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"
-  by (auto simp: openin_open)
-
-lemma open_openin_trans[trans]:
-  "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"
-  by (metis Int_absorb1  openin_open_Int)
-
-lemma open_subset: "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"
-  by (auto simp: openin_open)
-
-lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"
-  by (simp add: closedin_subtopology Int_ac)
-
-lemma closedin_closed_Int: "closed S \<Longrightarrow> closedin (subtopology euclidean U) (U \<inter> S)"
-  by (metis closedin_closed)
-
-lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"
-  by (auto simp: closedin_closed)
-
-lemma closedin_closed_subset:
- "\<lbrakk>closedin (subtopology euclidean U) V; T \<subseteq> U; S = V \<inter> T\<rbrakk>
-             \<Longrightarrow> closedin (subtopology euclidean T) S"
-  by (metis (no_types, lifting) Int_assoc Int_commute closedin_closed inf.orderE)
-
-lemma finite_imp_closedin:
-  fixes S :: "'a::t1_space set"
-  shows "\<lbrakk>finite S; S \<subseteq> T\<rbrakk> \<Longrightarrow> closedin (subtopology euclidean T) S"
-    by (simp add: finite_imp_closed closed_subset)
-
-lemma closedin_singleton [simp]:
-  fixes a :: "'a::t1_space"
-  shows "closedin (subtopology euclidean U) {a} \<longleftrightarrow> a \<in> U"
-using closedin_subset  by (force intro: closed_subset)
-
-lemma openin_euclidean_subtopology_iff:
-  fixes S U :: "'a::metric_space set"
-  shows "openin (subtopology euclidean U) S \<longleftrightarrow>
-    S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)"
-  (is "?lhs \<longleftrightarrow> ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs
-    unfolding openin_open open_dist by blast
-next
-  define T where "T = {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}"
-  have 1: "\<forall>x\<in>T. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> T"
-    unfolding T_def
-    apply clarsimp
-    apply (rule_tac x="d - dist x a" in exI)
-    apply (clarsimp simp add: less_diff_eq)
-    by (metis dist_commute dist_triangle_lt)
-  assume ?rhs then have 2: "S = U \<inter> T"
-    unfolding T_def
-    by auto (metis dist_self)
-  from 1 2 show ?lhs
-    unfolding openin_open open_dist by fast
-qed
-
-lemma connected_openin:
-      "connected S \<longleftrightarrow>
-       ~(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
-                 openin (subtopology euclidean S) E2 \<and>
-                 S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
-  apply (simp add: connected_def openin_open disjoint_iff_not_equal, safe)
-  apply (simp_all, blast+)  (* SLOW *)
-  done
-
-lemma connected_openin_eq:
-      "connected S \<longleftrightarrow>
-       ~(\<exists>E1 E2. openin (subtopology euclidean S) E1 \<and>
-                 openin (subtopology euclidean S) E2 \<and>
-                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
-                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
-  apply (simp add: connected_openin, safe, blast)
-  by (metis Int_lower1 Un_subset_iff openin_open subset_antisym)
-
-lemma connected_closedin:
-      "connected S \<longleftrightarrow>
-       (\<nexists>E1 E2.
-        closedin (subtopology euclidean S) E1 \<and>
-        closedin (subtopology euclidean S) E2 \<and>
-        S \<subseteq> E1 \<union> E2 \<and> E1 \<inter> E2 = {} \<and> E1 \<noteq> {} \<and> E2 \<noteq> {})"
-       (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs 
-    by (auto simp add: connected_closed closedin_closed)
-next
-  assume R: ?rhs
-  then show ?lhs 
-  proof (clarsimp simp add: connected_closed closedin_closed)
-    fix A B 
-    assume s_sub: "S \<subseteq> A \<union> B" "B \<inter> S \<noteq> {}"
-      and disj: "A \<inter> B \<inter> S = {}"
-      and cl: "closed A" "closed B"
-    have "S \<inter> (A \<union> B) = S"
-      using s_sub(1) by auto
-    have "S - A = B \<inter> S"
-      using Diff_subset_conv Un_Diff_Int disj s_sub(1) by auto
-    then have "S \<inter> A = {}"
-      by (metis Diff_Diff_Int Diff_disjoint Un_Diff_Int R cl closedin_closed_Int inf_commute order_refl s_sub(2))
-    then show "A \<inter> S = {}"
-      by blast
-  qed
-qed
-
-lemma connected_closedin_eq:
-      "connected S \<longleftrightarrow>
-           ~(\<exists>E1 E2.
-                 closedin (subtopology euclidean S) E1 \<and>
-                 closedin (subtopology euclidean S) E2 \<and>
-                 E1 \<union> E2 = S \<and> E1 \<inter> E2 = {} \<and>
-                 E1 \<noteq> {} \<and> E2 \<noteq> {})"
-  apply (simp add: connected_closedin, safe, blast)
-  by (metis Int_lower1 Un_subset_iff closedin_closed subset_antisym)
-
-text \<open>These "transitivity" results are handy too\<close>
-
-lemma openin_trans[trans]:
-  "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T \<Longrightarrow>
-    openin (subtopology euclidean U) S"
-  unfolding open_openin openin_open by blast
-
-lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"
-  by (auto simp: openin_open intro: openin_trans)
-
-lemma closedin_trans[trans]:
-  "closedin (subtopology euclidean T) S \<Longrightarrow> closedin (subtopology euclidean U) T \<Longrightarrow>
-    closedin (subtopology euclidean U) S"
-  by (auto simp: closedin_closed closed_Inter Int_assoc)
-
-lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"
-  by (auto simp: closedin_closed intro: closedin_trans)
-
-lemma openin_subtopology_Int_subset:
-   "\<lbrakk>openin (subtopology euclidean u) (u \<inter> S); v \<subseteq> u\<rbrakk> \<Longrightarrow> openin (subtopology euclidean v) (v \<inter> S)"
-  by (auto simp: openin_subtopology)
-
-lemma openin_open_eq: "open s \<Longrightarrow> (openin (subtopology euclidean s) t \<longleftrightarrow> open t \<and> t \<subseteq> s)"
-  using open_subset openin_open_trans openin_subset by fastforce
-
-
-subsection \<open>Open and closed balls\<close>
-
-definition%important ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
-  where "ball x e = {y. dist x y < e}"
-
-definition%important cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
-  where "cball x e = {y. dist x y \<le> e}"
-
-definition%important sphere :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set"
-  where "sphere x e = {y. dist x y = e}"
-
-lemma mem_ball [simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e"
-  by (simp add: ball_def)
-
-lemma mem_cball [simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e"
-  by (simp add: cball_def)
-
-lemma mem_sphere [simp]: "y \<in> sphere x e \<longleftrightarrow> dist x y = e"
-  by (simp add: sphere_def)
-
-lemma ball_trivial [simp]: "ball x 0 = {}"
-  by (simp add: ball_def)
-
-lemma cball_trivial [simp]: "cball x 0 = {x}"
-  by (simp add: cball_def)
-
-lemma sphere_trivial [simp]: "sphere x 0 = {x}"
-  by (simp add: sphere_def)
-
-lemma mem_ball_0 [simp]: "x \<in> ball 0 e \<longleftrightarrow> norm x < e"
-  for x :: "'a::real_normed_vector"
-  by (simp add: dist_norm)
-
-lemma mem_cball_0 [simp]: "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"
-  for x :: "'a::real_normed_vector"
-  by (simp add: dist_norm)
-
-lemma disjoint_ballI: "dist x y \<ge> r+s \<Longrightarrow> ball x r \<inter> ball y s = {}"
-  using dist_triangle_less_add not_le by fastforce
-
-lemma disjoint_cballI: "dist x y > r + s \<Longrightarrow> cball x r \<inter> cball y s = {}"
-  by (metis add_mono disjoint_iff_not_equal dist_triangle2 dual_order.trans leD mem_cball)
-
-lemma mem_sphere_0 [simp]: "x \<in> sphere 0 e \<longleftrightarrow> norm x = e"
-  for x :: "'a::real_normed_vector"
-  by (simp add: dist_norm)
-
-lemma sphere_empty [simp]: "r < 0 \<Longrightarrow> sphere a r = {}"
-  for a :: "'a::metric_space"
-  by auto
-
-lemma centre_in_ball [simp]: "x \<in> ball x e \<longleftrightarrow> 0 < e"
-  by simp
-
-lemma centre_in_cball [simp]: "x \<in> cball x e \<longleftrightarrow> 0 \<le> e"
-  by simp
-
-lemma ball_subset_cball [simp, intro]: "ball x e \<subseteq> cball x e"
-  by (simp add: subset_eq)
-
-lemma mem_ball_imp_mem_cball: "x \<in> ball y e \<Longrightarrow> x \<in> cball y e"
-  by (auto simp: mem_ball mem_cball)
-
-lemma sphere_cball [simp,intro]: "sphere z r \<subseteq> cball z r"
-  by force
-
-lemma cball_diff_sphere: "cball a r - sphere a r = ball a r"
-  by auto
-
-lemma subset_ball[intro]: "d \<le> e \<Longrightarrow> ball x d \<subseteq> ball x e"
-  by (simp add: subset_eq)
-
-lemma subset_cball[intro]: "d \<le> e \<Longrightarrow> cball x d \<subseteq> cball x e"
-  by (simp add: subset_eq)
-
-lemma mem_ball_leI: "x \<in> ball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> ball y f"
-  by (auto simp: mem_ball mem_cball)
-
-lemma mem_cball_leI: "x \<in> cball y e \<Longrightarrow> e \<le> f \<Longrightarrow> x \<in> cball y f"
-  by (auto simp: mem_ball mem_cball)
-
-lemma cball_trans: "y \<in> cball z b \<Longrightarrow> x \<in> cball y a \<Longrightarrow> x \<in> cball z (b + a)"
-  unfolding mem_cball
-proof -
-  have "dist z x \<le> dist z y + dist y x"
-    by (rule dist_triangle)
-  also assume "dist z y \<le> b"
-  also assume "dist y x \<le> a"
-  finally show "dist z x \<le> b + a" by arith
-qed
-
-lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"
-  by (simp add: set_eq_iff) arith
-
-lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"
-  by (simp add: set_eq_iff)
-
-lemma cball_max_Un: "cball a (max r s) = cball a r \<union> cball a s"
-  by (simp add: set_eq_iff) arith
-
-lemma cball_min_Int: "cball a (min r s) = cball a r \<inter> cball a s"
-  by (simp add: set_eq_iff)
-
-lemma cball_diff_eq_sphere: "cball a r - ball a r =  sphere a r"
-  by (auto simp: cball_def ball_def dist_commute)
-
-lemma image_add_ball [simp]:
-  fixes a :: "'a::real_normed_vector"
-  shows "(+) b ` ball a r = ball (a+b) r"
-apply (intro equalityI subsetI)
-apply (force simp: dist_norm)
-apply (rule_tac x="x-b" in image_eqI)
-apply (auto simp: dist_norm algebra_simps)
-done
-
-lemma image_add_cball [simp]:
-  fixes a :: "'a::real_normed_vector"
-  shows "(+) b ` cball a r = cball (a+b) r"
-apply (intro equalityI subsetI)
-apply (force simp: dist_norm)
-apply (rule_tac x="x-b" in image_eqI)
-apply (auto simp: dist_norm algebra_simps)
-done
-
-lemma open_ball [intro, simp]: "open (ball x e)"
-proof -
-  have "open (dist x -` {..<e})"
-    by (intro open_vimage open_lessThan continuous_intros)
-  also have "dist x -` {..<e} = ball x e"
-    by auto
-  finally show ?thesis .
-qed
-
-lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"
-  by (simp add: open_dist subset_eq mem_ball Ball_def dist_commute)
-
-lemma openI [intro?]: "(\<And>x. x\<in>S \<Longrightarrow> \<exists>e>0. ball x e \<subseteq> S) \<Longrightarrow> open S"
-  by (auto simp: open_contains_ball)
-
-lemma openE[elim?]:
-  assumes "open S" "x\<in>S"
-  obtains e where "e>0" "ball x e \<subseteq> S"
-  using assms unfolding open_contains_ball by auto
-
-lemma open_contains_ball_eq: "open S \<Longrightarrow> x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
-  by (metis open_contains_ball subset_eq centre_in_ball)
-
-lemma openin_contains_ball:
-    "openin (subtopology euclidean t) s \<longleftrightarrow>
-     s \<subseteq> t \<and> (\<forall>x \<in> s. \<exists>e. 0 < e \<and> ball x e \<inter> t \<subseteq> s)"
-    (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs
-    apply (simp add: openin_open)
-    apply (metis Int_commute Int_mono inf.cobounded2 open_contains_ball order_refl subsetCE)
-    done
-next
-  assume ?rhs
-  then show ?lhs
-    apply (simp add: openin_euclidean_subtopology_iff)
-    by (metis (no_types) Int_iff dist_commute inf.absorb_iff2 mem_ball)
-qed
-
-lemma openin_contains_cball:
-   "openin (subtopology euclidean t) s \<longleftrightarrow>
-        s \<subseteq> t \<and>
-        (\<forall>x \<in> s. \<exists>e. 0 < e \<and> cball x e \<inter> t \<subseteq> s)"
-apply (simp add: openin_contains_ball)
-apply (rule iffI)
-apply (auto dest!: bspec)
-apply (rule_tac x="e/2" in exI, force+)
-done
-
-lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"
-  unfolding mem_ball set_eq_iff
-  apply (simp add: not_less)
-  apply (metis zero_le_dist order_trans dist_self)
-  done
-
-lemma ball_empty: "e \<le> 0 \<Longrightarrow> ball x e = {}" by simp
-
-lemma closed_cball [iff]: "closed (cball x e)"
-proof -
-  have "closed (dist x -` {..e})"
-    by (intro closed_vimage closed_atMost continuous_intros)
-  also have "dist x -` {..e} = cball x e"
-    by auto
-  finally show ?thesis .
-qed
-
-lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"
-proof -
-  {
-    fix x and e::real
-    assume "x\<in>S" "e>0" "ball x e \<subseteq> S"
-    then have "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)
-  }
-  moreover
-  {
-    fix x and e::real
-    assume "x\<in>S" "e>0" "cball x e \<subseteq> S"
-    then have "\<exists>d>0. ball x d \<subseteq> S"
-      unfolding subset_eq
-      apply (rule_tac x="e/2" in exI, auto)
-      done
-  }
-  ultimately show ?thesis
-    unfolding open_contains_ball by auto
-qed
-
-lemma open_contains_cball_eq: "open S \<Longrightarrow> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"
-  by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)
-
 lemma euclidean_dist_l2:
   fixes x y :: "'a :: euclidean_space"
   shows "dist x y = L2_set (\<lambda>i. dist (x \<bullet> i) (y \<bullet> i)) Basis"
@@ -1417,28 +30,6 @@
     by (auto intro!: real_le_rsqrt)
 qed
 
-lemma eventually_nhds_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>x. x \<in> ball z d) (nhds z)"
-  by (rule eventually_nhds_in_open) simp_all
-
-lemma eventually_at_ball: "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<in> A) (at z within A)"
-  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
-
-lemma eventually_at_ball': "d > 0 \<Longrightarrow> eventually (\<lambda>t. t \<in> ball z d \<and> t \<noteq> z \<and> t \<in> A) (at z within A)"
-  unfolding eventually_at by (intro exI[of _ d]) (simp_all add: dist_commute)
-
-lemma at_within_ball: "e > 0 \<Longrightarrow> dist x y < e \<Longrightarrow> at y within ball x e = at y"
-  by (subst at_within_open) auto
-
-lemma atLeastAtMost_eq_cball:
-  fixes a b::real
-  shows "{a .. b} = cball ((a + b)/2) ((b - a)/2)"
-  by (auto simp: dist_real_def field_simps mem_cball)
-
-lemma greaterThanLessThan_eq_ball:
-  fixes a b::real
-  shows "{a <..< b} = ball ((a + b)/2) ((b - a)/2)"
-  by (auto simp: dist_real_def field_simps mem_ball)
-
 
 subsection \<open>Boxes\<close>
 
@@ -1946,7 +537,8 @@
     by (auto simp: box_def inner_sum_left inner_Basis sum.If_cases)
 qed
 
-subsection \<open>Intervals in general, including infinite and mixtures of open and closed\<close>
+
+subsection \<open>General Intervals\<close>
 
 definition%important "is_interval (s::('a::euclidean_space) set) \<longleftrightarrow>
   (\<forall>a\<in>s. \<forall>b\<in>s. \<forall>x. (\<forall>i\<in>Basis. ((a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i) \<or> (b\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> a\<bullet>i))) \<longrightarrow> x \<in> s)"
@@ -2123,2722 +715,6 @@
   using is_interval_translation[of "-c" X]
   by (metis image_cong uminus_add_conv_diff)
 
-
-subsection \<open>Limit points\<close>
-
-definition%important (in topological_space) islimpt:: "'a \<Rightarrow> 'a set \<Rightarrow> bool"  (infixr "islimpt" 60)
-  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))"
-
-lemma islimptI:
-  assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
-  shows "x islimpt S"
-  using assms unfolding islimpt_def by auto
-
-lemma islimptE:
-  assumes "x islimpt S" and "x \<in> T" and "open T"
-  obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"
-  using assms unfolding islimpt_def by auto
-
-lemma islimpt_iff_eventually: "x islimpt S \<longleftrightarrow> \<not> eventually (\<lambda>y. y \<notin> S) (at x)"
-  unfolding islimpt_def eventually_at_topological by auto
-
-lemma islimpt_subset: "x islimpt S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> x islimpt T"
-  unfolding islimpt_def by fast
-
-lemma islimpt_approachable:
-  fixes x :: "'a::metric_space"
-  shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"
-  unfolding islimpt_iff_eventually eventually_at by fast
-
-lemma islimpt_approachable_le: "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x \<le> e)"
-  for x :: "'a::metric_space"
-  unfolding islimpt_approachable
-  using approachable_lt_le [where f="\<lambda>y. dist y x" and P="\<lambda>y. y \<notin> S \<or> y = x",
-    THEN arg_cong [where f=Not]]
-  by (simp add: Bex_def conj_commute conj_left_commute)
-
-lemma islimpt_UNIV_iff: "x islimpt UNIV \<longleftrightarrow> \<not> open {x}"
-  unfolding islimpt_def by (safe, fast, case_tac "T = {x}", fast, fast)
-
-lemma islimpt_punctured: "x islimpt S = x islimpt (S-{x})"
-  unfolding islimpt_def by blast
-
-text \<open>A perfect space has no isolated points.\<close>
-
-lemma islimpt_UNIV [simp, intro]: "x islimpt UNIV"
-  for x :: "'a::perfect_space"
-  unfolding islimpt_UNIV_iff by (rule not_open_singleton)
-
-lemma perfect_choose_dist: "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"
-  for x :: "'a::{perfect_space,metric_space}"
-  using islimpt_UNIV [of x] by (simp add: islimpt_approachable)
-
-lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"
-  unfolding closed_def
-  apply (subst open_subopen)
-  apply (simp add: islimpt_def subset_eq)
-  apply (metis ComplE ComplI)
-  done
-
-lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"
-  by (auto simp: islimpt_def)
-
-lemma finite_ball_include:
-  fixes a :: "'a::metric_space"
-  assumes "finite S" 
-  shows "\<exists>e>0. S \<subseteq> ball a e"
-  using assms
-proof induction
-  case (insert x S)
-  then obtain e0 where "e0>0" and e0:"S \<subseteq> ball a e0" by auto
-  define e where "e = max e0 (2 * dist a x)"
-  have "e>0" unfolding e_def using \<open>e0>0\<close> by auto
-  moreover have "insert x S \<subseteq> ball a e"
-    using e0 \<open>e>0\<close> unfolding e_def by auto
-  ultimately show ?case by auto
-qed (auto intro: zero_less_one)
-
-lemma finite_set_avoid:
-  fixes a :: "'a::metric_space"
-  assumes "finite S"
-  shows "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
-  using assms
-proof induction
-  case (insert x S)
-  then obtain d where "d > 0" and d: "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> d \<le> dist a x"
-    by blast
-  show ?case
-  proof (cases "x = a")
-    case True
-    with \<open>d > 0 \<close>d show ?thesis by auto
-  next
-    case False
-    let ?d = "min d (dist a x)"
-    from False \<open>d > 0\<close> have dp: "?d > 0"
-      by auto
-    from d have d': "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> ?d \<le> dist a x"
-      by auto
-    with dp False show ?thesis
-      by (metis insert_iff le_less min_less_iff_conj not_less)
-  qed
-qed (auto intro: zero_less_one)
-
-lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"
-  by (simp add: islimpt_iff_eventually eventually_conj_iff)
-
-lemma discrete_imp_closed:
-  fixes S :: "'a::metric_space set"
-  assumes e: "0 < e"
-    and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"
-  shows "closed S"
-proof -
-  have False if C: "\<And>e. e>0 \<Longrightarrow> \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e" for x
-  proof -
-    from e have e2: "e/2 > 0" by arith
-    from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y \<noteq> x" "dist y x < e/2"
-      by blast
-    let ?m = "min (e/2) (dist x y) "
-    from e2 y(2) have mp: "?m > 0"
-      by simp
-    from C[OF mp] obtain z where z: "z \<in> S" "z \<noteq> x" "dist z x < ?m"
-      by blast
-    from z y have "dist z y < e"
-      by (intro dist_triangle_lt [where z=x]) simp
-    from d[rule_format, OF y(1) z(1) this] y z show ?thesis
-      by (auto simp: dist_commute)
-  qed
-  then show ?thesis
-    by (metis islimpt_approachable closed_limpt [where 'a='a])
-qed
-
-lemma closed_of_nat_image: "closed (of_nat ` A :: 'a::real_normed_algebra_1 set)"
-  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_nat)
-
-lemma closed_of_int_image: "closed (of_int ` A :: 'a::real_normed_algebra_1 set)"
-  by (rule discrete_imp_closed[of 1]) (auto simp: dist_of_int)
-
-lemma closed_Nats [simp]: "closed (\<nat> :: 'a :: real_normed_algebra_1 set)"
-  unfolding Nats_def by (rule closed_of_nat_image)
-
-lemma closed_Ints [simp]: "closed (\<int> :: 'a :: real_normed_algebra_1 set)"
-  unfolding Ints_def by (rule closed_of_int_image)
-
-lemma closed_subset_Ints:
-  fixes A :: "'a :: real_normed_algebra_1 set"
-  assumes "A \<subseteq> \<int>"
-  shows   "closed A"
-proof (intro discrete_imp_closed[OF zero_less_one] ballI impI, goal_cases)
-  case (1 x y)
-  with assms have "x \<in> \<int>" and "y \<in> \<int>" by auto
-  with \<open>dist y x < 1\<close> show "y = x"
-    by (auto elim!: Ints_cases simp: dist_of_int)
-qed
-
-
-subsection \<open>Interior of a Set\<close>
-
-definition%important "interior S = \<Union>{T. open T \<and> T \<subseteq> S}"
-
-lemma interiorI [intro?]:
-  assumes "open T" and "x \<in> T" and "T \<subseteq> S"
-  shows "x \<in> interior S"
-  using assms unfolding interior_def by fast
-
-lemma interiorE [elim?]:
-  assumes "x \<in> interior S"
-  obtains T where "open T" and "x \<in> T" and "T \<subseteq> S"
-  using assms unfolding interior_def by fast
-
-lemma open_interior [simp, intro]: "open (interior S)"
-  by (simp add: interior_def open_Union)
-
-lemma interior_subset: "interior S \<subseteq> S"
-  by (auto simp: interior_def)
-
-lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> interior S"
-  by (auto simp: interior_def)
-
-lemma interior_open: "open S \<Longrightarrow> interior S = S"
-  by (intro equalityI interior_subset interior_maximal subset_refl)
-
-lemma interior_eq: "interior S = S \<longleftrightarrow> open S"
-  by (metis open_interior interior_open)
-
-lemma open_subset_interior: "open S \<Longrightarrow> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"
-  by (metis interior_maximal interior_subset subset_trans)
-
-lemma interior_empty [simp]: "interior {} = {}"
-  using open_empty by (rule interior_open)
-
-lemma interior_UNIV [simp]: "interior UNIV = UNIV"
-  using open_UNIV by (rule interior_open)
-
-lemma interior_interior [simp]: "interior (interior S) = interior S"
-  using open_interior by (rule interior_open)
-
-lemma interior_mono: "S \<subseteq> T \<Longrightarrow> interior S \<subseteq> interior T"
-  by (auto simp: interior_def)
-
-lemma interior_unique:
-  assumes "T \<subseteq> S" and "open T"
-  assumes "\<And>T'. T' \<subseteq> S \<Longrightarrow> open T' \<Longrightarrow> T' \<subseteq> T"
-  shows "interior S = T"
-  by (intro equalityI assms interior_subset open_interior interior_maximal)
-
-lemma interior_singleton [simp]: "interior {a} = {}"
-  for a :: "'a::perfect_space"
-  apply (rule interior_unique, simp_all)
-  using not_open_singleton subset_singletonD
-  apply fastforce
-  done
-
-lemma interior_Int [simp]: "interior (S \<inter> T) = interior S \<inter> interior T"
-  by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1
-    Int_lower2 interior_maximal interior_subset open_Int open_interior)
-
-lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"
-  using open_contains_ball_eq [where S="interior S"]
-  by (simp add: open_subset_interior)
-
-lemma eventually_nhds_in_nhd: "x \<in> interior s \<Longrightarrow> eventually (\<lambda>y. y \<in> s) (nhds x)"
-  using interior_subset[of s] by (subst eventually_nhds) blast
-
-lemma interior_limit_point [intro]:
-  fixes x :: "'a::perfect_space"
-  assumes x: "x \<in> interior S"
-  shows "x islimpt S"
-  using x islimpt_UNIV [of x]
-  unfolding interior_def islimpt_def
-  apply (clarsimp, rename_tac T T')
-  apply (drule_tac x="T \<inter> T'" in spec)
-  apply (auto simp: open_Int)
-  done
-
-lemma interior_closed_Un_empty_interior:
-  assumes cS: "closed S"
-    and iT: "interior T = {}"
-  shows "interior (S \<union> T) = interior S"
-proof
-  show "interior S \<subseteq> interior (S \<union> T)"
-    by (rule interior_mono) (rule Un_upper1)
-  show "interior (S \<union> T) \<subseteq> interior S"
-  proof
-    fix x
-    assume "x \<in> interior (S \<union> T)"
-    then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T" ..
-    show "x \<in> interior S"
-    proof (rule ccontr)
-      assume "x \<notin> interior S"
-      with \<open>x \<in> R\<close> \<open>open R\<close> obtain y where "y \<in> R - S"
-        unfolding interior_def by fast
-      from \<open>open R\<close> \<open>closed S\<close> have "open (R - S)"
-        by (rule open_Diff)
-      from \<open>R \<subseteq> S \<union> T\<close> have "R - S \<subseteq> T"
-        by fast
-      from \<open>y \<in> R - S\<close> \<open>open (R - S)\<close> \<open>R - S \<subseteq> T\<close> \<open>interior T = {}\<close> show False
-        unfolding interior_def by fast
-    qed
-  qed
-qed
-
-lemma interior_Times: "interior (A \<times> B) = interior A \<times> interior B"
-proof (rule interior_unique)
-  show "interior A \<times> interior B \<subseteq> A \<times> B"
-    by (intro Sigma_mono interior_subset)
-  show "open (interior A \<times> interior B)"
-    by (intro open_Times open_interior)
-  fix T
-  assume "T \<subseteq> A \<times> B" and "open T"
-  then show "T \<subseteq> interior A \<times> interior B"
-  proof safe
-    fix x y
-    assume "(x, y) \<in> T"
-    then obtain C D where "open C" "open D" "C \<times> D \<subseteq> T" "x \<in> C" "y \<in> D"
-      using \<open>open T\<close> unfolding open_prod_def by fast
-    then have "open C" "open D" "C \<subseteq> A" "D \<subseteq> B" "x \<in> C" "y \<in> D"
-      using \<open>T \<subseteq> A \<times> B\<close> by auto
-    then show "x \<in> interior A" and "y \<in> interior B"
-      by (auto intro: interiorI)
-  qed
-qed
-
-lemma interior_Ici:
-  fixes x :: "'a :: {dense_linorder,linorder_topology}"
-  assumes "b < x"
-  shows "interior {x ..} = {x <..}"
-proof (rule interior_unique)
-  fix T
-  assume "T \<subseteq> {x ..}" "open T"
-  moreover have "x \<notin> T"
-  proof
-    assume "x \<in> T"
-    obtain y where "y < x" "{y <.. x} \<subseteq> T"
-      using open_left[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>b < x\<close>] by auto
-    with dense[OF \<open>y < x\<close>] obtain z where "z \<in> T" "z < x"
-      by (auto simp: subset_eq Ball_def)
-    with \<open>T \<subseteq> {x ..}\<close> show False by auto
-  qed
-  ultimately show "T \<subseteq> {x <..}"
-    by (auto simp: subset_eq less_le)
-qed auto
-
-lemma interior_Iic:
-  fixes x :: "'a ::{dense_linorder,linorder_topology}"
-  assumes "x < b"
-  shows "interior {.. x} = {..< x}"
-proof (rule interior_unique)
-  fix T
-  assume "T \<subseteq> {.. x}" "open T"
-  moreover have "x \<notin> T"
-  proof
-    assume "x \<in> T"
-    obtain y where "x < y" "{x ..< y} \<subseteq> T"
-      using open_right[OF \<open>open T\<close> \<open>x \<in> T\<close> \<open>x < b\<close>] by auto
-    with dense[OF \<open>x < y\<close>] obtain z where "z \<in> T" "x < z"
-      by (auto simp: subset_eq Ball_def less_le)
-    with \<open>T \<subseteq> {.. x}\<close> show False by auto
-  qed
-  ultimately show "T \<subseteq> {..< x}"
-    by (auto simp: subset_eq less_le)
-qed auto
-
-
-subsection \<open>Closure of a Set\<close>
-
-definition%important "closure S = S \<union> {x | x. x islimpt S}"
-
-lemma interior_closure: "interior S = - (closure (- S))"
-  by (auto simp: interior_def closure_def islimpt_def)
-
-lemma closure_interior: "closure S = - interior (- S)"
-  by (simp add: interior_closure)
-
-lemma closed_closure[simp, intro]: "closed (closure S)"
-  by (simp add: closure_interior closed_Compl)
-
-lemma closure_subset: "S \<subseteq> closure S"
-  by (simp add: closure_def)
-
-lemma closure_hull: "closure S = closed hull S"
-  by (auto simp: hull_def closure_interior interior_def)
-
-lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"
-  unfolding closure_hull using closed_Inter by (rule hull_eq)
-
-lemma closure_closed [simp]: "closed S \<Longrightarrow> closure S = S"
-  by (simp only: closure_eq)
-
-lemma closure_closure [simp]: "closure (closure S) = closure S"
-  unfolding closure_hull by (rule hull_hull)
-
-lemma closure_mono: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"
-  unfolding closure_hull by (rule hull_mono)
-
-lemma closure_minimal: "S \<subseteq> T \<Longrightarrow> closed T \<Longrightarrow> closure S \<subseteq> T"
-  unfolding closure_hull by (rule hull_minimal)
-
-lemma closure_unique:
-  assumes "S \<subseteq> T"
-    and "closed T"
-    and "\<And>T'. S \<subseteq> T' \<Longrightarrow> closed T' \<Longrightarrow> T \<subseteq> T'"
-  shows "closure S = T"
-  using assms unfolding closure_hull by (rule hull_unique)
-
-lemma closure_empty [simp]: "closure {} = {}"
-  using closed_empty by (rule closure_closed)
-
-lemma closure_UNIV [simp]: "closure UNIV = UNIV"
-  using closed_UNIV by (rule closure_closed)
-
-lemma closure_Un [simp]: "closure (S \<union> T) = closure S \<union> closure T"
-  by (simp add: closure_interior)
-
-lemma closure_eq_empty [iff]: "closure S = {} \<longleftrightarrow> S = {}"
-  using closure_empty closure_subset[of S] by blast
-
-lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"
-  using closure_eq[of S] closure_subset[of S] by simp
-
-lemma open_Int_closure_eq_empty: "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"
-  using open_subset_interior[of S "- T"]
-  using interior_subset[of "- T"]
-  by (auto simp: closure_interior)
-
-lemma open_Int_closure_subset: "open S \<Longrightarrow> S \<inter> closure T \<subseteq> closure (S \<inter> T)"
-proof
-  fix x
-  assume *: "open S" "x \<in> S \<inter> closure T"
-  have "x islimpt (S \<inter> T)" if **: "x islimpt T"
-  proof (rule islimptI)
-    fix A
-    assume "x \<in> A" "open A"
-    with * have "x \<in> A \<inter> S" "open (A \<inter> S)"
-      by (simp_all add: open_Int)
-    with ** obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"
-      by (rule islimptE)
-    then have "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"
-      by simp_all
-    then show "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..
-  qed
-  with * show "x \<in> closure (S \<inter> T)"
-    unfolding closure_def by blast
-qed
-
-lemma closure_complement: "closure (- S) = - interior S"
-  by (simp add: closure_interior)
-
-lemma interior_complement: "interior (- S) = - closure S"
-  by (simp add: closure_interior)
-
-lemma interior_diff: "interior(S - T) = interior S - closure T"
-  by (simp add: Diff_eq interior_complement)
-
-lemma closure_Times: "closure (A \<times> B) = closure A \<times> closure B"
-proof (rule closure_unique)
-  show "A \<times> B \<subseteq> closure A \<times> closure B"
-    by (intro Sigma_mono closure_subset)
-  show "closed (closure A \<times> closure B)"
-    by (intro closed_Times closed_closure)
-  fix T
-  assume "A \<times> B \<subseteq> T" and "closed T"
-  then show "closure A \<times> closure B \<subseteq> T"
-    apply (simp add: closed_def open_prod_def, clarify)
-    apply (rule ccontr)
-    apply (drule_tac x="(a, b)" in bspec, simp, clarify, rename_tac C D)
-    apply (simp add: closure_interior interior_def)
-    apply (drule_tac x=C in spec)
-    apply (drule_tac x=D in spec, auto)
-    done
-qed
-
-lemma closure_openin_Int_closure:
-  assumes ope: "openin (subtopology euclidean U) S" and "T \<subseteq> U"
-  shows "closure(S \<inter> closure T) = closure(S \<inter> T)"
-proof
-  obtain V where "open V" and S: "S = U \<inter> V"
-    using ope using openin_open by metis
-  show "closure (S \<inter> closure T) \<subseteq> closure (S \<inter> T)"
-    proof (clarsimp simp: S)
-      fix x
-      assume  "x \<in> closure (U \<inter> V \<inter> closure T)"
-      then have "V \<inter> closure T \<subseteq> A \<Longrightarrow> x \<in> closure A" for A
-          by (metis closure_mono subsetD inf.coboundedI2 inf_assoc)
-      then have "x \<in> closure (T \<inter> V)"
-         by (metis \<open>open V\<close> closure_closure inf_commute open_Int_closure_subset)
-      then show "x \<in> closure (U \<inter> V \<inter> T)"
-        by (metis \<open>T \<subseteq> U\<close> inf.absorb_iff2 inf_assoc inf_commute)
-    qed
-next
-  show "closure (S \<inter> T) \<subseteq> closure (S \<inter> closure T)"
-    by (meson Int_mono closure_mono closure_subset order_refl)
-qed
-
-lemma islimpt_in_closure: "(x islimpt S) = (x\<in>closure(S-{x}))"
-  unfolding closure_def using islimpt_punctured by blast
-
-lemma connected_imp_connected_closure: "connected S \<Longrightarrow> connected (closure S)"
-  by (rule connectedI) (meson closure_subset open_Int open_Int_closure_eq_empty subset_trans connectedD)
-
-lemma limpt_of_limpts: "x islimpt {y. y islimpt S} \<Longrightarrow> x islimpt S"
-  for x :: "'a::metric_space"
-  apply (clarsimp simp add: islimpt_approachable)
-  apply (drule_tac x="e/2" in spec)
-  apply (auto simp: simp del: less_divide_eq_numeral1)
-  apply (drule_tac x="dist x' x" in spec)
-  apply (auto simp: zero_less_dist_iff simp del: less_divide_eq_numeral1)
-  apply (erule rev_bexI)
-  apply (metis dist_commute dist_triangle_half_r less_trans less_irrefl)
-  done
-
-lemma closed_limpts:  "closed {x::'a::metric_space. x islimpt S}"
-  using closed_limpt limpt_of_limpts by blast
-
-lemma limpt_of_closure: "x islimpt closure S \<longleftrightarrow> x islimpt S"
-  for x :: "'a::metric_space"
-  by (auto simp: closure_def islimpt_Un dest: limpt_of_limpts)
-
-lemma closedin_limpt:
-  "closedin (subtopology euclidean T) S \<longleftrightarrow> S \<subseteq> T \<and> (\<forall>x. x islimpt S \<and> x \<in> T \<longrightarrow> x \<in> S)"
-  apply (simp add: closedin_closed, safe)
-   apply (simp add: closed_limpt islimpt_subset)
-  apply (rule_tac x="closure S" in exI, simp)
-  apply (force simp: closure_def)
-  done
-
-lemma closedin_closed_eq: "closed S \<Longrightarrow> closedin (subtopology euclidean S) T \<longleftrightarrow> closed T \<and> T \<subseteq> S"
-  by (meson closedin_limpt closed_subset closedin_closed_trans)
-
-lemma connected_closed_set:
-   "closed S
-    \<Longrightarrow> connected S \<longleftrightarrow> (\<nexists>A B. closed A \<and> closed B \<and> A \<noteq> {} \<and> B \<noteq> {} \<and> A \<union> B = S \<and> A \<inter> B = {})"
-  unfolding connected_closedin_eq closedin_closed_eq connected_closedin_eq by blast
-
-text \<open>If a connnected set is written as the union of two nonempty closed sets, then these sets
-have to intersect.\<close>
-
-lemma connected_as_closed_union:
-  assumes "connected C" "C = A \<union> B" "closed A" "closed B" "A \<noteq> {}" "B \<noteq> {}"
-  shows "A \<inter> B \<noteq> {}"
-by (metis assms closed_Un connected_closed_set)
-
-lemma closedin_subset_trans:
-  "closedin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
-    closedin (subtopology euclidean T) S"
-  by (meson closedin_limpt subset_iff)
-
-lemma openin_subset_trans:
-  "openin (subtopology euclidean U) S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> T \<subseteq> U \<Longrightarrow>
-    openin (subtopology euclidean T) S"
-  by (auto simp: openin_open)
-
-lemma openin_Times:
-  "openin (subtopology euclidean S) S' \<Longrightarrow> openin (subtopology euclidean T) T' \<Longrightarrow>
-    openin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
-  unfolding openin_open using open_Times by blast
-
-lemma Times_in_interior_subtopology:
-  fixes U :: "('a::metric_space \<times> 'b::metric_space) set"
-  assumes "(x, y) \<in> U" "openin (subtopology euclidean (S \<times> T)) U"
-  obtains V W where "openin (subtopology euclidean S) V" "x \<in> V"
-                    "openin (subtopology euclidean T) W" "y \<in> W" "(V \<times> W) \<subseteq> U"
-proof -
-  from assms obtain e where "e > 0" and "U \<subseteq> S \<times> T"
-    and e: "\<And>x' y'. \<lbrakk>x'\<in>S; y'\<in>T; dist (x', y') (x, y) < e\<rbrakk> \<Longrightarrow> (x', y') \<in> U"
-    by (force simp: openin_euclidean_subtopology_iff)
-  with assms have "x \<in> S" "y \<in> T"
-    by auto
-  show ?thesis
-  proof
-    show "openin (subtopology euclidean S) (ball x (e/2) \<inter> S)"
-      by (simp add: Int_commute openin_open_Int)
-    show "x \<in> ball x (e / 2) \<inter> S"
-      by (simp add: \<open>0 < e\<close> \<open>x \<in> S\<close>)
-    show "openin (subtopology euclidean T) (ball y (e/2) \<inter> T)"
-      by (simp add: Int_commute openin_open_Int)
-    show "y \<in> ball y (e / 2) \<inter> T"
-      by (simp add: \<open>0 < e\<close> \<open>y \<in> T\<close>)
-    show "(ball x (e / 2) \<inter> S) \<times> (ball y (e / 2) \<inter> T) \<subseteq> U"
-      by clarify (simp add: e dist_Pair_Pair \<open>0 < e\<close> dist_commute sqrt_sum_squares_half_less)
-  qed
-qed
-
-lemma openin_Times_eq:
-  fixes S :: "'a::metric_space set" and T :: "'b::metric_space set"
-  shows
-    "openin (subtopology euclidean (S \<times> T)) (S' \<times> T') \<longleftrightarrow>
-      S' = {} \<or> T' = {} \<or> openin (subtopology euclidean S) S' \<and> openin (subtopology euclidean T) T'"
-    (is "?lhs = ?rhs")
-proof (cases "S' = {} \<or> T' = {}")
-  case True
-  then show ?thesis by auto
-next
-  case False
-  then obtain x y where "x \<in> S'" "y \<in> T'"
-    by blast
-  show ?thesis
-  proof
-    assume ?lhs
-    have "openin (subtopology euclidean S) S'"
-      apply (subst openin_subopen, clarify)
-      apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
-      using \<open>y \<in> T'\<close>
-       apply auto
-      done
-    moreover have "openin (subtopology euclidean T) T'"
-      apply (subst openin_subopen, clarify)
-      apply (rule Times_in_interior_subtopology [OF _ \<open>?lhs\<close>])
-      using \<open>x \<in> S'\<close>
-       apply auto
-      done
-    ultimately show ?rhs
-      by simp
-  next
-    assume ?rhs
-    with False show ?lhs
-      by (simp add: openin_Times)
-  qed
-qed
-
-lemma closedin_Times:
-  "closedin (subtopology euclidean S) S' \<Longrightarrow> closedin (subtopology euclidean T) T' \<Longrightarrow>
-    closedin (subtopology euclidean (S \<times> T)) (S' \<times> T')"
-  unfolding closedin_closed using closed_Times by blast
-
-lemma bdd_below_closure:
-  fixes A :: "real set"
-  assumes "bdd_below A"
-  shows "bdd_below (closure A)"
-proof -
-  from assms obtain m where "\<And>x. x \<in> A \<Longrightarrow> m \<le> x"
-    by (auto simp: bdd_below_def)
-  then have "A \<subseteq> {m..}" by auto
-  then have "closure A \<subseteq> {m..}"
-    using closed_real_atLeast by (rule closure_minimal)
-  then show ?thesis
-    by (auto simp: bdd_below_def)
-qed
-
-
-subsection \<open>Frontier (also known as boundary)\<close>
-
-definition%important "frontier S = closure S - interior S"
-
-lemma frontier_closed [iff]: "closed (frontier S)"
-  by (simp add: frontier_def closed_Diff)
-
-lemma frontier_closures: "frontier S = closure S \<inter> closure (- S)"
-  by (auto simp: frontier_def interior_closure)
-
-lemma frontier_Int: "frontier(S \<inter> T) = closure(S \<inter> T) \<inter> (frontier S \<union> frontier T)"
-proof -
-  have "closure (S \<inter> T) \<subseteq> closure S" "closure (S \<inter> T) \<subseteq> closure T"
-    by (simp_all add: closure_mono)
-  then show ?thesis
-    by (auto simp: frontier_closures)
-qed
-
-lemma frontier_Int_subset: "frontier(S \<inter> T) \<subseteq> frontier S \<union> frontier T"
-  by (auto simp: frontier_Int)
-
-lemma frontier_Int_closed:
-  assumes "closed S" "closed T"
-  shows "frontier(S \<inter> T) = (frontier S \<inter> T) \<union> (S \<inter> frontier T)"
-proof -
-  have "closure (S \<inter> T) = T \<inter> S"
-    using assms by (simp add: Int_commute closed_Int)
-  moreover have "T \<inter> (closure S \<inter> closure (- S)) = frontier S \<inter> T"
-    by (simp add: Int_commute frontier_closures)
-  ultimately show ?thesis
-    by (simp add: Int_Un_distrib Int_assoc Int_left_commute assms frontier_closures)
-qed
-
-lemma frontier_straddle:
-  fixes a :: "'a::metric_space"
-  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))"
-  unfolding frontier_def closure_interior
-  by (auto simp: mem_interior subset_eq ball_def)
-
-lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"
-  by (metis frontier_def closure_closed Diff_subset)
-
-lemma frontier_empty [simp]: "frontier {} = {}"
-  by (simp add: frontier_def)
-
-lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"
-proof -
-  {
-    assume "frontier S \<subseteq> S"
-    then have "closure S \<subseteq> S"
-      using interior_subset unfolding frontier_def by auto
-    then have "closed S"
-      using closure_subset_eq by auto
-  }
-  then show ?thesis using frontier_subset_closed[of S] ..
-qed
-
-lemma frontier_complement [simp]: "frontier (- S) = frontier S"
-  by (auto simp: frontier_def closure_complement interior_complement)
-
-lemma frontier_Un_subset: "frontier(S \<union> T) \<subseteq> frontier S \<union> frontier T"
-  by (metis compl_sup frontier_Int_subset frontier_complement)
-
-lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"
-  using frontier_complement frontier_subset_eq[of "- S"]
-  unfolding open_closed by auto
-
-lemma frontier_UNIV [simp]: "frontier UNIV = {}"
-  using frontier_complement frontier_empty by fastforce
-
-lemma frontier_interiors: "frontier s = - interior(s) - interior(-s)"
-  by (simp add: Int_commute frontier_def interior_closure)
-
-lemma frontier_interior_subset: "frontier(interior S) \<subseteq> frontier S"
-  by (simp add: Diff_mono frontier_interiors interior_mono interior_subset)
-
-lemma connected_Int_frontier:
-     "\<lbrakk>connected s; s \<inter> t \<noteq> {}; s - t \<noteq> {}\<rbrakk> \<Longrightarrow> (s \<inter> frontier t \<noteq> {})"
-  apply (simp add: frontier_interiors connected_openin, safe)
-  apply (drule_tac x="s \<inter> interior t" in spec, safe)
-   apply (drule_tac [2] x="s \<inter> interior (-t)" in spec)
-   apply (auto simp: disjoint_eq_subset_Compl dest: interior_subset [THEN subsetD])
-  done
-
-lemma closure_Un_frontier: "closure S = S \<union> frontier S"
-proof -
-  have "S \<union> interior S = S"
-    using interior_subset by auto
-  then show ?thesis
-    using closure_subset by (auto simp: frontier_def)
-qed
-
-
-subsection%unimportant \<open>Filters and the ``eventually true'' quantifier\<close>
-
-definition indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a filter"  (infixr "indirection" 70)
-  where "a indirection v = at a within {b. \<exists>c\<ge>0. b - a = scaleR c v}"
-
-text \<open>Identify Trivial limits, where we can't approach arbitrarily closely.\<close>
-
-lemma trivial_limit_within: "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"
-proof
-  assume "trivial_limit (at a within S)"
-  then show "\<not> a islimpt S"
-    unfolding trivial_limit_def
-    unfolding eventually_at_topological
-    unfolding islimpt_def
-    apply (clarsimp simp add: set_eq_iff)
-    apply (rename_tac T, rule_tac x=T in exI)
-    apply (clarsimp, drule_tac x=y in bspec, simp_all)
-    done
-next
-  assume "\<not> a islimpt S"
-  then show "trivial_limit (at a within S)"
-    unfolding trivial_limit_def eventually_at_topological islimpt_def
-    by metis
-qed
-
-lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"
-  using trivial_limit_within [of a UNIV] by simp
-
-lemma trivial_limit_at: "\<not> trivial_limit (at a)"
-  for a :: "'a::perfect_space"
-  by (rule at_neq_bot)
-
-lemma trivial_limit_at_infinity:
-  "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,perfect_space}) filter)"
-  unfolding trivial_limit_def eventually_at_infinity
-  apply clarsimp
-  apply (subgoal_tac "\<exists>x::'a. x \<noteq> 0", clarify)
-   apply (rule_tac x="scaleR (b / norm x) x" in exI, simp)
-  apply (cut_tac islimpt_UNIV [of "0::'a", unfolded islimpt_def])
-  apply (drule_tac x=UNIV in spec, simp)
-  done
-
-lemma not_trivial_limit_within: "\<not> trivial_limit (at x within S) = (x \<in> closure (S - {x}))"
-  using islimpt_in_closure by (metis trivial_limit_within)
-
-lemma not_in_closure_trivial_limitI:
-  "x \<notin> closure s \<Longrightarrow> trivial_limit (at x within s)"
-  using not_trivial_limit_within[of x s]
-  by safe (metis Diff_empty Diff_insert0 closure_subset contra_subsetD)
-
-lemma filterlim_at_within_closure_implies_filterlim: "filterlim f l (at x within s)"
-  if "x \<in> closure s \<Longrightarrow> filterlim f l (at x within s)"
-  by (metis bot.extremum filterlim_filtercomap filterlim_mono not_in_closure_trivial_limitI that)
-
-lemma at_within_eq_bot_iff: "at c within A = bot \<longleftrightarrow> c \<notin> closure (A - {c})"
-  using not_trivial_limit_within[of c A] by blast
-
-text \<open>Some property holds "sufficiently close" to the limit point.\<close>
-
-lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"
-  by simp
-
-lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"
-  by (simp add: filter_eq_iff)
-
-
-subsection \<open>Limits\<close>
-
-proposition Lim: "(f \<longlongrightarrow> l) net \<longleftrightarrow> trivial_limit net \<or> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
-  by (auto simp: tendsto_iff trivial_limit_eq)
-
-text \<open>Show that they yield usual definitions in the various cases.\<close>
-
-proposition Lim_within_le: "(f \<longlongrightarrow> l)(at a within S) \<longleftrightarrow>
-    (\<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)"
-  by (auto simp: tendsto_iff eventually_at_le)
-
-proposition Lim_within: "(f \<longlongrightarrow> l) (at a within S) \<longleftrightarrow>
-    (\<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)"
-  by (auto simp: tendsto_iff eventually_at)
-
-corollary Lim_withinI [intro?]:
-  assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>d>0. \<forall>x \<in> S. 0 < dist x a \<and> dist x a < d \<longrightarrow> dist (f x) l \<le> e"
-  shows "(f \<longlongrightarrow> l) (at a within S)"
-  apply (simp add: Lim_within, clarify)
-  apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
-  done
-
-proposition Lim_at: "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow>
-    (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d  \<longrightarrow> dist (f x) l < e)"
-  by (auto simp: tendsto_iff eventually_at)
-
-proposition Lim_at_infinity: "(f \<longlongrightarrow> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x \<ge> b \<longrightarrow> dist (f x) l < e)"
-  by (auto simp: tendsto_iff eventually_at_infinity)
-
-corollary Lim_at_infinityI [intro?]:
-  assumes "\<And>e. e > 0 \<Longrightarrow> \<exists>B. \<forall>x. norm x \<ge> B \<longrightarrow> dist (f x) l \<le> e"
-  shows "(f \<longlongrightarrow> l) at_infinity"
-  apply (simp add: Lim_at_infinity, clarify)
-  apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
-  done
-
-lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f \<longlongrightarrow> l) net"
-  by (rule topological_tendstoI) (auto elim: eventually_mono)
-
-lemma Lim_transform_within_set:
-  fixes a :: "'a::metric_space" and l :: "'b::metric_space"
-  shows "\<lbrakk>(f \<longlongrightarrow> l) (at a within S); eventually (\<lambda>x. x \<in> S \<longleftrightarrow> x \<in> T) (at a)\<rbrakk>
-         \<Longrightarrow> (f \<longlongrightarrow> l) (at a within T)"
-apply (clarsimp simp: eventually_at Lim_within)
-apply (drule_tac x=e in spec, clarify)
-apply (rename_tac k)
-apply (rule_tac x="min d k" in exI, simp)
-done
-
-lemma Lim_transform_within_set_eq:
-  fixes a l :: "'a::real_normed_vector"
-  shows "eventually (\<lambda>x. x \<in> s \<longleftrightarrow> x \<in> t) (at a)
-         \<Longrightarrow> ((f \<longlongrightarrow> l) (at a within s) \<longleftrightarrow> (f \<longlongrightarrow> l) (at a within t))"
-  by (force intro: Lim_transform_within_set elim: eventually_mono)
-
-lemma Lim_transform_within_openin:
-  fixes a :: "'a::metric_space"
-  assumes f: "(f \<longlongrightarrow> l) (at a within T)"
-    and "openin (subtopology euclidean T) S" "a \<in> S"
-    and eq: "\<And>x. \<lbrakk>x \<in> S; x \<noteq> a\<rbrakk> \<Longrightarrow> f x = g x"
-  shows "(g \<longlongrightarrow> l) (at a within T)"
-proof -
-  obtain \<epsilon> where "0 < \<epsilon>" and \<epsilon>: "ball a \<epsilon> \<inter> T \<subseteq> S"
-    using assms by (force simp: openin_contains_ball)
-  then have "a \<in> ball a \<epsilon>"
-    by simp
-  show ?thesis
-    by (rule Lim_transform_within [OF f \<open>0 < \<epsilon>\<close> eq]) (use \<epsilon> in \<open>auto simp: dist_commute subset_iff\<close>)
-qed
-
-lemma continuous_transform_within_openin:
-  fixes a :: "'a::metric_space"
-  assumes "continuous (at a within T) f"
-    and "openin (subtopology euclidean T) S" "a \<in> S"
-    and eq: "\<And>x. x \<in> S \<Longrightarrow> f x = g x"
-  shows "continuous (at a within T) g"
-  using assms by (simp add: Lim_transform_within_openin continuous_within)
-
-text \<open>The expected monotonicity property.\<close>
-
-lemma Lim_Un:
-  assumes "(f \<longlongrightarrow> l) (at x within S)" "(f \<longlongrightarrow> l) (at x within T)"
-  shows "(f \<longlongrightarrow> l) (at x within (S \<union> T))"
-  using assms unfolding at_within_union by (rule filterlim_sup)
-
-lemma Lim_Un_univ:
-  "(f \<longlongrightarrow> l) (at x within S) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within T) \<Longrightarrow>
-    S \<union> T = UNIV \<Longrightarrow> (f \<longlongrightarrow> l) (at x)"
-  by (metis Lim_Un)
-
-text \<open>Interrelations between restricted and unrestricted limits.\<close>
-
-lemma Lim_at_imp_Lim_at_within: "(f \<longlongrightarrow> l) (at x) \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S)"
-  by (metis order_refl filterlim_mono subset_UNIV at_le)
-
-lemma eventually_within_interior:
-  assumes "x \<in> interior S"
-  shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)"
-  (is "?lhs = ?rhs")
-proof
-  from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S" ..
-  {
-    assume ?lhs
-    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"
-      by (auto simp: eventually_at_topological)
-    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"
-      by auto
-    then show ?rhs
-      by (auto simp: eventually_at_topological)
-  next
-    assume ?rhs
-    then show ?lhs
-      by (auto elim: eventually_mono simp: eventually_at_filter)
-  }
-qed
-
-lemma at_within_interior: "x \<in> interior S \<Longrightarrow> at x within S = at x"
-  unfolding filter_eq_iff by (intro allI eventually_within_interior)
-
-lemma Lim_within_LIMSEQ:
-  fixes a :: "'a::first_countable_topology"
-  assumes "\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
-  shows "(X \<longlongrightarrow> L) (at a within T)"
-  using assms unfolding tendsto_def [where l=L]
-  by (simp add: sequentially_imp_eventually_within)
-
-lemma Lim_right_bound:
-  fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder, no_top} \<Rightarrow>
-    'b::{linorder_topology, conditionally_complete_linorder}"
-  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"
-    and bnd: "\<And>a. a \<in> I \<Longrightarrow> x < a \<Longrightarrow> K \<le> f a"
-  shows "(f \<longlongrightarrow> Inf (f ` ({x<..} \<inter> I))) (at x within ({x<..} \<inter> I))"
-proof (cases "{x<..} \<inter> I = {}")
-  case True
-  then show ?thesis by simp
-next
-  case False
-  show ?thesis
-  proof (rule order_tendstoI)
-    fix a
-    assume a: "a < Inf (f ` ({x<..} \<inter> I))"
-    {
-      fix y
-      assume "y \<in> {x<..} \<inter> I"
-      with False bnd have "Inf (f ` ({x<..} \<inter> I)) \<le> f y"
-        by (auto intro!: cInf_lower bdd_belowI2)
-      with a have "a < f y"
-        by (blast intro: less_le_trans)
-    }
-    then show "eventually (\<lambda>x. a < f x) (at x within ({x<..} \<inter> I))"
-      by (auto simp: eventually_at_filter intro: exI[of _ 1] zero_less_one)
-  next
-    fix a
-    assume "Inf (f ` ({x<..} \<inter> I)) < a"
-    from cInf_lessD[OF _ this] False obtain y where y: "x < y" "y \<in> I" "f y < a"
-      by auto
-    then have "eventually (\<lambda>x. x \<in> I \<longrightarrow> f x < a) (at_right x)"
-      unfolding eventually_at_right[OF \<open>x < y\<close>] by (metis less_imp_le le_less_trans mono)
-    then show "eventually (\<lambda>x. f x < a) (at x within ({x<..} \<inter> I))"
-      unfolding eventually_at_filter by eventually_elim simp
-  qed
-qed
-
-text \<open>Another limit point characterization.\<close>
-
-lemma limpt_sequential_inj:
-  fixes x :: "'a::metric_space"
-  shows "x islimpt S \<longleftrightarrow>
-         (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> inj f \<and> (f \<longlongrightarrow> x) sequentially)"
-         (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then have "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"
-    by (force simp: islimpt_approachable)
-  then obtain y where y: "\<And>e. e>0 \<Longrightarrow> y e \<in> S \<and> y e \<noteq> x \<and> dist (y e) x < e"
-    by metis
-  define f where "f \<equiv> rec_nat (y 1) (\<lambda>n fn. y (min (inverse(2 ^ (Suc n))) (dist fn x)))"
-  have [simp]: "f 0 = y 1"
-               "f(Suc n) = y (min (inverse(2 ^ (Suc n))) (dist (f n) x))" for n
-    by (simp_all add: f_def)
-  have f: "f n \<in> S \<and> (f n \<noteq> x) \<and> dist (f n) x < inverse(2 ^ n)" for n
-  proof (induction n)
-    case 0 show ?case
-      by (simp add: y)
-  next
-    case (Suc n) then show ?case
-      apply (auto simp: y)
-      by (metis half_gt_zero_iff inverse_positive_iff_positive less_divide_eq_numeral1(1) min_less_iff_conj y zero_less_dist_iff zero_less_numeral zero_less_power)
-  qed
-  show ?rhs
-  proof (rule_tac x=f in exI, intro conjI allI)
-    show "\<And>n. f n \<in> S - {x}"
-      using f by blast
-    have "dist (f n) x < dist (f m) x" if "m < n" for m n
-    using that
-    proof (induction n)
-      case 0 then show ?case by simp
-    next
-      case (Suc n)
-      then consider "m < n" | "m = n" using less_Suc_eq by blast
-      then show ?case
-      proof cases
-        assume "m < n"
-        have "dist (f(Suc n)) x = dist (y (min (inverse(2 ^ (Suc n))) (dist (f n) x))) x"
-          by simp
-        also have "\<dots> < dist (f n) x"
-          by (metis dist_pos_lt f min.strict_order_iff min_less_iff_conj y)
-        also have "\<dots> < dist (f m) x"
-          using Suc.IH \<open>m < n\<close> by blast
-        finally show ?thesis .
-      next
-        assume "m = n" then show ?case
-          by simp (metis dist_pos_lt f half_gt_zero_iff inverse_positive_iff_positive min_less_iff_conj y zero_less_numeral zero_less_power)
-      qed
-    qed
-    then show "inj f"
-      by (metis less_irrefl linorder_injI)
-    show "f \<longlonglongrightarrow> x"
-      apply (rule tendstoI)
-      apply (rule_tac c="nat (ceiling(1/e))" in eventually_sequentiallyI)
-      apply (rule less_trans [OF f [THEN conjunct2, THEN conjunct2]])
-      apply (simp add: field_simps)
-      by (meson le_less_trans mult_less_cancel_left not_le of_nat_less_two_power)
-  qed
-next
-  assume ?rhs
-  then show ?lhs
-    by (fastforce simp add: islimpt_approachable lim_sequentially)
-qed
-
-(*could prove directly from islimpt_sequential_inj, but only for metric spaces*)
-lemma islimpt_sequential:
-  fixes x :: "'a::first_countable_topology"
-  shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> (f \<longlongrightarrow> x) sequentially)"
-    (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  from countable_basis_at_decseq[of x] obtain A where A:
-      "\<And>i. open (A i)"
-      "\<And>i. x \<in> A i"
-      "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
-    by blast
-  define f where "f n = (SOME y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y)" for n
-  {
-    fix n
-    from \<open>?lhs\<close> have "\<exists>y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y"
-      unfolding islimpt_def using A(1,2)[of n] by auto
-    then have "f n \<in> S \<and> f n \<in> A n \<and> x \<noteq> f n"
-      unfolding f_def by (rule someI_ex)
-    then have "f n \<in> S" "f n \<in> A n" "x \<noteq> f n" by auto
-  }
-  then have "\<forall>n. f n \<in> S - {x}" by auto
-  moreover have "(\<lambda>n. f n) \<longlonglongrightarrow> x"
-  proof (rule topological_tendstoI)
-    fix S
-    assume "open S" "x \<in> S"
-    from A(3)[OF this] \<open>\<And>n. f n \<in> A n\<close>
-    show "eventually (\<lambda>x. f x \<in> S) sequentially"
-      by (auto elim!: eventually_mono)
-  qed
-  ultimately show ?rhs by fast
-next
-  assume ?rhs
-  then obtain f :: "nat \<Rightarrow> 'a" where f: "\<And>n. f n \<in> S - {x}" and lim: "f \<longlonglongrightarrow> x"
-    by auto
-  show ?lhs
-    unfolding islimpt_def
-  proof safe
-    fix T
-    assume "open T" "x \<in> T"
-    from lim[THEN topological_tendstoD, OF this] f
-    show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"
-      unfolding eventually_sequentially by auto
-  qed
-qed
-
-lemma Lim_null:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-  shows "(f \<longlongrightarrow> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) \<longlongrightarrow> 0) net"
-  by (simp add: Lim dist_norm)
-
-lemma Lim_null_comparison:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-  assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g \<longlongrightarrow> 0) net"
-  shows "(f \<longlongrightarrow> 0) net"
-  using assms(2)
-proof (rule metric_tendsto_imp_tendsto)
-  show "eventually (\<lambda>x. dist (f x) 0 \<le> dist (g x) 0) net"
-    using assms(1) by (rule eventually_mono) (simp add: dist_norm)
-qed
-
-lemma Lim_transform_bound:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-    and g :: "'a \<Rightarrow> 'c::real_normed_vector"
-  assumes "eventually (\<lambda>n. norm (f n) \<le> norm (g n)) net"
-    and "(g \<longlongrightarrow> 0) net"
-  shows "(f \<longlongrightarrow> 0) net"
-  using assms(1) tendsto_norm_zero [OF assms(2)]
-  by (rule Lim_null_comparison)
-
-lemma lim_null_mult_right_bounded:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
-  assumes f: "(f \<longlongrightarrow> 0) F" and g: "eventually (\<lambda>x. norm(g x) \<le> B) F"
-    shows "((\<lambda>z. f z * g z) \<longlongrightarrow> 0) F"
-proof -
-  have *: "((\<lambda>x. norm (f x) * B) \<longlongrightarrow> 0) F"
-    by (simp add: f tendsto_mult_left_zero tendsto_norm_zero)
-  have "((\<lambda>x. norm (f x) * norm (g x)) \<longlongrightarrow> 0) F"
-    apply (rule Lim_null_comparison [OF _ *])
-    apply (simp add: eventually_mono [OF g] mult_left_mono)
-    done
-  then show ?thesis
-    by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
-qed
-
-lemma lim_null_mult_left_bounded:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
-  assumes g: "eventually (\<lambda>x. norm(g x) \<le> B) F" and f: "(f \<longlongrightarrow> 0) F"
-    shows "((\<lambda>z. g z * f z) \<longlongrightarrow> 0) F"
-proof -
-  have *: "((\<lambda>x. B * norm (f x)) \<longlongrightarrow> 0) F"
-    by (simp add: f tendsto_mult_right_zero tendsto_norm_zero)
-  have "((\<lambda>x. norm (g x) * norm (f x)) \<longlongrightarrow> 0) F"
-    apply (rule Lim_null_comparison [OF _ *])
-    apply (simp add: eventually_mono [OF g] mult_right_mono)
-    done
-  then show ?thesis
-    by (subst tendsto_norm_zero_iff [symmetric]) (simp add: norm_mult)
-qed
-
-lemma lim_null_scaleR_bounded:
-  assumes f: "(f \<longlongrightarrow> 0) net" and gB: "eventually (\<lambda>a. f a = 0 \<or> norm(g a) \<le> B) net"
-    shows "((\<lambda>n. f n *\<^sub>R g n) \<longlongrightarrow> 0) net"
-proof
-  fix \<epsilon>::real
-  assume "0 < \<epsilon>"
-  then have B: "0 < \<epsilon> / (abs B + 1)" by simp
-  have *: "\<bar>f x\<bar> * norm (g x) < \<epsilon>" if f: "\<bar>f x\<bar> * (\<bar>B\<bar> + 1) < \<epsilon>" and g: "norm (g x) \<le> B" for x
-  proof -
-    have "\<bar>f x\<bar> * norm (g x) \<le> \<bar>f x\<bar> * B"
-      by (simp add: mult_left_mono g)
-    also have "\<dots> \<le> \<bar>f x\<bar> * (\<bar>B\<bar> + 1)"
-      by (simp add: mult_left_mono)
-    also have "\<dots> < \<epsilon>"
-      by (rule f)
-    finally show ?thesis .
-  qed
-  show "\<forall>\<^sub>F x in net. dist (f x *\<^sub>R g x) 0 < \<epsilon>"
-    apply (rule eventually_mono [OF eventually_conj [OF tendstoD [OF f B] gB] ])
-    apply (auto simp: \<open>0 < \<epsilon>\<close> divide_simps * split: if_split_asm)
-    done
-qed
-
-text\<open>Deducing things about the limit from the elements.\<close>
-
-lemma Lim_in_closed_set:
-  assumes "closed S"
-    and "eventually (\<lambda>x. f(x) \<in> S) net"
-    and "\<not> trivial_limit net" "(f \<longlongrightarrow> l) net"
-  shows "l \<in> S"
-proof (rule ccontr)
-  assume "l \<notin> S"
-  with \<open>closed S\<close> have "open (- S)" "l \<in> - S"
-    by (simp_all add: open_Compl)
-  with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"
-    by (rule topological_tendstoD)
-  with assms(2) have "eventually (\<lambda>x. False) net"
-    by (rule eventually_elim2) simp
-  with assms(3) show "False"
-    by (simp add: eventually_False)
-qed
-
-text\<open>Need to prove closed(cball(x,e)) before deducing this as a corollary.\<close>
-
-lemma Lim_dist_ubound:
-  assumes "\<not>(trivial_limit net)"
-    and "(f \<longlongrightarrow> l) net"
-    and "eventually (\<lambda>x. dist a (f x) \<le> e) net"
-  shows "dist a l \<le> e"
-  using assms by (fast intro: tendsto_le tendsto_intros)
-
-lemma Lim_norm_ubound:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-  assumes "\<not>(trivial_limit net)" "(f \<longlongrightarrow> l) net" "eventually (\<lambda>x. norm(f x) \<le> e) net"
-  shows "norm(l) \<le> e"
-  using assms by (fast intro: tendsto_le tendsto_intros)
-
-lemma Lim_norm_lbound:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-  assumes "\<not> trivial_limit net"
-    and "(f \<longlongrightarrow> l) net"
-    and "eventually (\<lambda>x. e \<le> norm (f x)) net"
-  shows "e \<le> norm l"
-  using assms by (fast intro: tendsto_le tendsto_intros)
-
-text\<open>Limit under bilinear function\<close>
-
-lemma Lim_bilinear:
-  assumes "(f \<longlongrightarrow> l) net"
-    and "(g \<longlongrightarrow> m) net"
-    and "bounded_bilinear h"
-  shows "((\<lambda>x. h (f x) (g x)) \<longlongrightarrow> (h l m)) net"
-  using \<open>bounded_bilinear h\<close> \<open>(f \<longlongrightarrow> l) net\<close> \<open>(g \<longlongrightarrow> m) net\<close>
-  by (rule bounded_bilinear.tendsto)
-
-text\<open>These are special for limits out of the same vector space.\<close>
-
-lemma Lim_within_id: "(id \<longlongrightarrow> a) (at a within s)"
-  unfolding id_def by (rule tendsto_ident_at)
-
-lemma Lim_at_id: "(id \<longlongrightarrow> a) (at a)"
-  unfolding id_def by (rule tendsto_ident_at)
-
-lemma Lim_at_zero:
-  fixes a :: "'a::real_normed_vector"
-    and l :: "'b::topological_space"
-  shows "(f \<longlongrightarrow> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) \<longlongrightarrow> l) (at 0)"
-  using LIM_offset_zero LIM_offset_zero_cancel ..
-
-text\<open>It's also sometimes useful to extract the limit point from the filter.\<close>
-
-abbreviation netlimit :: "'a::t2_space filter \<Rightarrow> 'a"
-  where "netlimit F \<equiv> Lim F (\<lambda>x. x)"
-
-lemma netlimit_within: "\<not> trivial_limit (at a within S) \<Longrightarrow> netlimit (at a within S) = a"
-  by (rule tendsto_Lim) (auto intro: tendsto_intros)
-
-lemma netlimit_at [simp]:
-  fixes a :: "'a::{perfect_space,t2_space}"
-  shows "netlimit (at a) = a"
-  using netlimit_within [of a UNIV] by simp
-
-lemma lim_within_interior:
-  "x \<in> interior S \<Longrightarrow> (f \<longlongrightarrow> l) (at x within S) \<longleftrightarrow> (f \<longlongrightarrow> l) (at x)"
-  by (metis at_within_interior)
-
-lemma netlimit_within_interior:
-  fixes x :: "'a::{t2_space,perfect_space}"
-  assumes "x \<in> interior S"
-  shows "netlimit (at x within S) = x"
-  using assms by (metis at_within_interior netlimit_at)
-
-lemma netlimit_at_vector:
-  fixes a :: "'a::real_normed_vector"
-  shows "netlimit (at a) = a"
-proof (cases "\<exists>x. x \<noteq> a")
-  case True then obtain x where x: "x \<noteq> a" ..
-  have "\<not> trivial_limit (at a)"
-    unfolding trivial_limit_def eventually_at dist_norm
-    apply clarsimp
-    apply (rule_tac x="a + scaleR (d / 2) (sgn (x - a))" in exI)
-    apply (simp add: norm_sgn sgn_zero_iff x)
-    done
-  then show ?thesis
-    by (rule netlimit_within [of a UNIV])
-qed simp
-
-
-text\<open>Useful lemmas on closure and set of possible sequential limits.\<close>
-
-lemma closure_sequential:
-  fixes l :: "'a::first_countable_topology"
-  shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially)"
-  (is "?lhs = ?rhs")
-proof
-  assume "?lhs"
-  moreover
-  {
-    assume "l \<in> S"
-    then have "?rhs" using tendsto_const[of l sequentially] by auto
-  }
-  moreover
-  {
-    assume "l islimpt S"
-    then have "?rhs" unfolding islimpt_sequential by auto
-  }
-  ultimately show "?rhs"
-    unfolding closure_def by auto
-next
-  assume "?rhs"
-  then show "?lhs" unfolding closure_def islimpt_sequential by auto
-qed
-
-lemma closed_sequential_limits:
-  fixes S :: "'a::first_countable_topology set"
-  shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x \<longlongrightarrow> l) sequentially \<longrightarrow> l \<in> S)"
-by (metis closure_sequential closure_subset_eq subset_iff)
-
-lemma closure_approachable:
-  fixes S :: "'a::metric_space set"
-  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"
-  apply (auto simp: closure_def islimpt_approachable)
-  apply (metis dist_self)
-  done
-
-lemma closure_approachable_le:
-  fixes S :: "'a::metric_space set"
-  shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x \<le> e)"
-  unfolding closure_approachable
-  using dense by force
-
-lemma closure_approachableD:
-  assumes "x \<in> closure S" "e>0"
-  shows "\<exists>y\<in>S. dist x y < e"
-  using assms unfolding closure_approachable by (auto simp: dist_commute)
-
-lemma closed_approachable:
-  fixes S :: "'a::metric_space set"
-  shows "closed S \<Longrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"
-  by (metis closure_closed closure_approachable)
-
-lemma closure_contains_Inf:
-  fixes S :: "real set"
-  assumes "S \<noteq> {}" "bdd_below S"
-  shows "Inf S \<in> closure S"
-proof -
-  have *: "\<forall>x\<in>S. Inf S \<le> x"
-    using cInf_lower[of _ S] assms by metis
-  {
-    fix e :: real
-    assume "e > 0"
-    then have "Inf S < Inf S + e" by simp
-    with assms obtain x where "x \<in> S" "x < Inf S + e"
-      by (subst (asm) cInf_less_iff) auto
-    with * have "\<exists>x\<in>S. dist x (Inf S) < e"
-      by (intro bexI[of _ x]) (auto simp: dist_real_def)
-  }
-  then show ?thesis unfolding closure_approachable by auto
-qed
-
-lemma closure_Int_ballI:
-  fixes S :: "'a :: metric_space set"
-  assumes "\<And>U. \<lbrakk>openin (subtopology euclidean S) U; U \<noteq> {}\<rbrakk> \<Longrightarrow> T \<inter> U \<noteq> {}"
- shows "S \<subseteq> closure T"
-proof (clarsimp simp: closure_approachable dist_commute)
-  fix x and e::real
-  assume "x \<in> S" "0 < e"
-  with assms [of "S \<inter> ball x e"] show "\<exists>y\<in>T. dist x y < e"
-    by force
-qed
-
-lemma closed_contains_Inf:
-  fixes S :: "real set"
-  shows "S \<noteq> {} \<Longrightarrow> bdd_below S \<Longrightarrow> closed S \<Longrightarrow> Inf S \<in> S"
-  by (metis closure_contains_Inf closure_closed)
-
-lemma closed_subset_contains_Inf:
-  fixes A C :: "real set"
-  shows "closed C \<Longrightarrow> A \<subseteq> C \<Longrightarrow> A \<noteq> {} \<Longrightarrow> bdd_below A \<Longrightarrow> Inf A \<in> C"
-  by (metis closure_contains_Inf closure_minimal subset_eq)
-
-lemma atLeastAtMost_subset_contains_Inf:
-  fixes A :: "real set" and a b :: real
-  shows "A \<noteq> {} \<Longrightarrow> a \<le> b \<Longrightarrow> A \<subseteq> {a..b} \<Longrightarrow> Inf A \<in> {a..b}"
-  by (rule closed_subset_contains_Inf)
-     (auto intro: closed_real_atLeastAtMost intro!: bdd_belowI[of A a])
-
-lemma not_trivial_limit_within_ball:
-  "\<not> trivial_limit (at x within S) \<longleftrightarrow> (\<forall>e>0. S \<inter> ball x e - {x} \<noteq> {})"
-  (is "?lhs \<longleftrightarrow> ?rhs")
-proof
-  show ?rhs if ?lhs
-  proof -
-    {
-      fix e :: real
-      assume "e > 0"
-      then obtain y where "y \<in> S - {x}" and "dist y x < e"
-        using \<open>?lhs\<close> not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
-        by auto
-      then have "y \<in> S \<inter> ball x e - {x}"
-        unfolding ball_def by (simp add: dist_commute)
-      then have "S \<inter> ball x e - {x} \<noteq> {}" by blast
-    }
-    then show ?thesis by auto
-  qed
-  show ?lhs if ?rhs
-  proof -
-    {
-      fix e :: real
-      assume "e > 0"
-      then obtain y where "y \<in> S \<inter> ball x e - {x}"
-        using \<open>?rhs\<close> by blast
-      then have "y \<in> S - {x}" and "dist y x < e"
-        unfolding ball_def by (simp_all add: dist_commute)
-      then have "\<exists>y \<in> S - {x}. dist y x < e"
-        by auto
-    }
-    then show ?thesis
-      using not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]
-      by auto
-  qed
-qed
-
-lemma tendsto_If_within_closures:
-  assumes f: "x \<in> s \<union> (closure s \<inter> closure t) \<Longrightarrow>
-      (f \<longlongrightarrow> l x) (at x within s \<union> (closure s \<inter> closure t))"
-  assumes g: "x \<in> t \<union> (closure s \<inter> closure t) \<Longrightarrow>
-      (g \<longlongrightarrow> l x) (at x within t \<union> (closure s \<inter> closure t))"
-  assumes "x \<in> s \<union> t"
-  shows "((\<lambda>x. if x \<in> s then f x else g x) \<longlongrightarrow> l x) (at x within s \<union> t)"
-proof -
-  have *: "(s \<union> t) \<inter> {x. x \<in> s} = s" "(s \<union> t) \<inter> {x. x \<notin> s} = t - s"
-    by auto
-  have "(f \<longlongrightarrow> l x) (at x within s)"
-    by (rule filterlim_at_within_closure_implies_filterlim)
-       (use \<open>x \<in> _\<close> in \<open>auto simp: inf_commute closure_def intro: tendsto_within_subset[OF f]\<close>)
-  moreover
-  have "(g \<longlongrightarrow> l x) (at x within t - s)"
-    by (rule filterlim_at_within_closure_implies_filterlim)
-      (use \<open>x \<in> _\<close> in
-        \<open>auto intro!: tendsto_within_subset[OF g] simp: closure_def intro: islimpt_subset\<close>)
-  ultimately show ?thesis
-    by (intro filterlim_at_within_If) (simp_all only: *)
-qed
-
-
-subsection \<open>Boundedness\<close>
-
-  (* FIXME: This has to be unified with BSEQ!! *)
-definition%important (in metric_space) bounded :: "'a set \<Rightarrow> bool"
-  where "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"
-
-lemma bounded_subset_cball: "bounded S \<longleftrightarrow> (\<exists>e x. S \<subseteq> cball x e \<and> 0 \<le> e)"
-  unfolding bounded_def subset_eq  by auto (meson order_trans zero_le_dist)
-
-lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"
-  unfolding bounded_def
-  by auto (metis add.commute add_le_cancel_right dist_commute dist_triangle_le)
-
-lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"
-  unfolding bounded_any_center [where a=0]
-  by (simp add: dist_norm)
-
-lemma bdd_above_norm: "bdd_above (norm ` X) \<longleftrightarrow> bounded X"
-  by (simp add: bounded_iff bdd_above_def)
-
-lemma bounded_norm_comp: "bounded ((\<lambda>x. norm (f x)) ` S) = bounded (f ` S)"
-  by (simp add: bounded_iff)
-
-lemma boundedI:
-  assumes "\<And>x. x \<in> S \<Longrightarrow> norm x \<le> B"
-  shows "bounded S"
-  using assms bounded_iff by blast
-
-lemma bounded_empty [simp]: "bounded {}"
-  by (simp add: bounded_def)
-
-lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T \<Longrightarrow> bounded S"
-  by (metis bounded_def subset_eq)
-
-lemma bounded_interior[intro]: "bounded S \<Longrightarrow> bounded(interior S)"
-  by (metis bounded_subset interior_subset)
-
-lemma bounded_closure[intro]:
-  assumes "bounded S"
-  shows "bounded (closure S)"
-proof -
-  from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a"
-    unfolding bounded_def by auto
-  {
-    fix y
-    assume "y \<in> closure S"
-    then obtain f where f: "\<forall>n. f n \<in> S"  "(f \<longlongrightarrow> y) sequentially"
-      unfolding closure_sequential by auto
-    have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
-    then have "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
-      by (simp add: f(1))
-    have "dist x y \<le> a"
-      apply (rule Lim_dist_ubound [of sequentially f])
-      apply (rule trivial_limit_sequentially)
-      apply (rule f(2))
-      apply fact
-      done
-  }
-  then show ?thesis
-    unfolding bounded_def by auto
-qed
-
-lemma bounded_closure_image: "bounded (f ` closure S) \<Longrightarrow> bounded (f ` S)"
-  by (simp add: bounded_subset closure_subset image_mono)
-
-lemma bounded_cball[simp,intro]: "bounded (cball x e)"
-  apply (simp add: bounded_def)
-  apply (rule_tac x=x in exI)
-  apply (rule_tac x=e in exI, auto)
-  done
-
-lemma bounded_ball[simp,intro]: "bounded (ball x e)"
-  by (metis ball_subset_cball bounded_cball bounded_subset)
-
-lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
-  by (auto simp: bounded_def) (metis Un_iff bounded_any_center le_max_iff_disj)
-
-lemma bounded_Union[intro]: "finite F \<Longrightarrow> \<forall>S\<in>F. bounded S \<Longrightarrow> bounded (\<Union>F)"
-  by (induct rule: finite_induct[of F]) auto
-
-lemma bounded_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. bounded (B x) \<Longrightarrow> bounded (\<Union>x\<in>A. B x)"
-  by (induct set: finite) auto
-
-lemma bounded_insert [simp]: "bounded (insert x S) \<longleftrightarrow> bounded S"
-proof -
-  have "\<forall>y\<in>{x}. dist x y \<le> 0"
-    by simp
-  then have "bounded {x}"
-    unfolding bounded_def by fast
-  then show ?thesis
-    by (metis insert_is_Un bounded_Un)
-qed
-
-lemma bounded_subset_ballI: "S \<subseteq> ball x r \<Longrightarrow> bounded S"
-  by (meson bounded_ball bounded_subset)
-
-lemma bounded_subset_ballD:
-  assumes "bounded S" shows "\<exists>r. 0 < r \<and> S \<subseteq> ball x r"
-proof -
-  obtain e::real and y where "S \<subseteq> cball y e"  "0 \<le> e"
-    using assms by (auto simp: bounded_subset_cball)
-  then show ?thesis
-    apply (rule_tac x="dist x y + e + 1" in exI)
-    apply (simp add: add.commute add_pos_nonneg)
-    apply (erule subset_trans)
-    apply (clarsimp simp add: cball_def)
-    by (metis add_le_cancel_right add_strict_increasing dist_commute dist_triangle_le zero_less_one)
-qed
-
-lemma finite_imp_bounded [intro]: "finite S \<Longrightarrow> bounded S"
-  by (induct set: finite) simp_all
-
-lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x \<le> b)"
-  apply (simp add: bounded_iff)
-  apply (subgoal_tac "\<And>x (y::real). 0 < 1 + \<bar>y\<bar> \<and> (x \<le> y \<longrightarrow> x \<le> 1 + \<bar>y\<bar>)")
-  apply metis
-  apply arith
-  done
-
-lemma bounded_pos_less: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x < b)"
-  apply (simp add: bounded_pos)
-  apply (safe; rule_tac x="b+1" in exI; force)
-  done
-
-lemma Bseq_eq_bounded:
-  fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
-  shows "Bseq f \<longleftrightarrow> bounded (range f)"
-  unfolding Bseq_def bounded_pos by auto
-
-lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
-  by (metis Int_lower1 Int_lower2 bounded_subset)
-
-lemma bounded_diff[intro]: "bounded S \<Longrightarrow> bounded (S - T)"
-  by (metis Diff_subset bounded_subset)
-
-lemma not_bounded_UNIV[simp]:
-  "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
-proof (auto simp: bounded_pos not_le)
-  obtain x :: 'a where "x \<noteq> 0"
-    using perfect_choose_dist [OF zero_less_one] by fast
-  fix b :: real
-  assume b: "b >0"
-  have b1: "b +1 \<ge> 0"
-    using b by simp
-  with \<open>x \<noteq> 0\<close> have "b < norm (scaleR (b + 1) (sgn x))"
-    by (simp add: norm_sgn)
-  then show "\<exists>x::'a. b < norm x" ..
-qed
-
-corollary cobounded_imp_unbounded:
-    fixes S :: "'a::{real_normed_vector, perfect_space} set"
-    shows "bounded (- S) \<Longrightarrow> ~ (bounded S)"
-  using bounded_Un [of S "-S"]  by (simp add: sup_compl_top)
-
-lemma bounded_dist_comp:
-  assumes "bounded (f ` S)" "bounded (g ` S)"
-  shows "bounded ((\<lambda>x. dist (f x) (g x)) ` S)"
-proof -
-  from assms obtain M1 M2 where *: "dist (f x) undefined \<le> M1" "dist undefined (g x) \<le> M2" if "x \<in> S" for x
-    by (auto simp: bounded_any_center[of _ undefined] dist_commute)
-  have "dist (f x) (g x) \<le> M1 + M2" if "x \<in> S" for x
-    using *[OF that]
-    by (rule order_trans[OF dist_triangle add_mono])
-  then show ?thesis
-    by (auto intro!: boundedI)
-qed
-
-lemma bounded_linear_image:
-  assumes "bounded S"
-    and "bounded_linear f"
-  shows "bounded (f ` S)"
-proof -
-  from assms(1) obtain b where "b > 0" and b: "\<forall>x\<in>S. norm x \<le> b"
-    unfolding bounded_pos by auto
-  from assms(2) obtain B where B: "B > 0" "\<forall>x. norm (f x) \<le> B * norm x"
-    using bounded_linear.pos_bounded by (auto simp: ac_simps)
-  show ?thesis
-    unfolding bounded_pos
-  proof (intro exI, safe)
-    show "norm (f x) \<le> B * b" if "x \<in> S" for x
-      by (meson B b less_imp_le mult_left_mono order_trans that)
-  qed (use \<open>b > 0\<close> \<open>B > 0\<close> in auto)
-qed
-
-lemma bounded_scaling:
-  fixes S :: "'a::real_normed_vector set"
-  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
-  apply (rule bounded_linear_image, assumption)
-  apply (rule bounded_linear_scaleR_right)
-  done
-
-lemma bounded_scaleR_comp:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-  assumes "bounded (f ` S)"
-  shows "bounded ((\<lambda>x. r *\<^sub>R f x) ` S)"
-  using bounded_scaling[of "f ` S" r] assms
-  by (auto simp: image_image)
-
-lemma bounded_translation:
-  fixes S :: "'a::real_normed_vector set"
-  assumes "bounded S"
-  shows "bounded ((\<lambda>x. a + x) ` S)"
-proof -
-  from assms obtain b where b: "b > 0" "\<forall>x\<in>S. norm x \<le> b"
-    unfolding bounded_pos by auto
-  {
-    fix x
-    assume "x \<in> S"
-    then have "norm (a + x) \<le> b + norm a"
-      using norm_triangle_ineq[of a x] b by auto
-  }
-  then show ?thesis
-    unfolding bounded_pos
-    using norm_ge_zero[of a] b(1) and add_strict_increasing[of b 0 "norm a"]
-    by (auto intro!: exI[of _ "b + norm a"])
-qed
-
-lemma bounded_translation_minus:
-  fixes S :: "'a::real_normed_vector set"
-  shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. x - a) ` S)"
-using bounded_translation [of S "-a"] by simp
-
-lemma bounded_uminus [simp]:
-  fixes X :: "'a::real_normed_vector set"
-  shows "bounded (uminus ` X) \<longleftrightarrow> bounded X"
-by (auto simp: bounded_def dist_norm; rule_tac x="-x" in exI; force simp: add.commute norm_minus_commute)
-
-lemma uminus_bounded_comp [simp]:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-  shows "bounded ((\<lambda>x. - f x) ` S) \<longleftrightarrow> bounded (f ` S)"
-  using bounded_uminus[of "f ` S"]
-  by (auto simp: image_image)
-
-lemma bounded_plus_comp:
-  fixes f g::"'a \<Rightarrow> 'b::real_normed_vector"
-  assumes "bounded (f ` S)"
-  assumes "bounded (g ` S)"
-  shows "bounded ((\<lambda>x. f x + g x) ` S)"
-proof -
-  {
-    fix B C
-    assume "\<And>x. x\<in>S \<Longrightarrow> norm (f x) \<le> B" "\<And>x. x\<in>S \<Longrightarrow> norm (g x) \<le> C"
-    then have "\<And>x. x \<in> S \<Longrightarrow> norm (f x + g x) \<le> B + C"
-      by (auto intro!: norm_triangle_le add_mono)
-  } then show ?thesis
-    using assms by (fastforce simp: bounded_iff)
-qed
-
-lemma bounded_plus:
-  fixes S ::"'a::real_normed_vector set"
-  assumes "bounded S" "bounded T"
-  shows "bounded ((\<lambda>(x,y). x + y) ` (S \<times> T))"
-  using bounded_plus_comp [of fst "S \<times> T" snd] assms
-  by (auto simp: split_def split: if_split_asm)
-
-lemma bounded_minus_comp:
-  "bounded (f ` S) \<Longrightarrow> bounded (g ` S) \<Longrightarrow> bounded ((\<lambda>x. f x - g x) ` S)"
-  for f g::"'a \<Rightarrow> 'b::real_normed_vector"
-  using bounded_plus_comp[of "f" S "\<lambda>x. - g x"]
-  by auto
-
-lemma bounded_minus:
-  fixes S ::"'a::real_normed_vector set"
-  assumes "bounded S" "bounded T"
-  shows "bounded ((\<lambda>(x,y). x - y) ` (S \<times> T))"
-  using bounded_minus_comp [of fst "S \<times> T" snd] assms
-  by (auto simp: split_def split: if_split_asm)
-
-
-subsection \<open>Compactness\<close>
-
-subsubsection \<open>Bolzano-Weierstrass property\<close>
-
-proposition heine_borel_imp_bolzano_weierstrass:
-  assumes "compact s"
-    and "infinite t"
-    and "t \<subseteq> s"
-  shows "\<exists>x \<in> s. x islimpt t"
-proof (rule ccontr)
-  assume "\<not> (\<exists>x \<in> s. x islimpt t)"
-  then obtain f where f: "\<forall>x\<in>s. x \<in> f x \<and> open (f x) \<and> (\<forall>y\<in>t. y \<in> f x \<longrightarrow> y = x)"
-    unfolding islimpt_def
-    using bchoice[of s "\<lambda> x T. x \<in> T \<and> open T \<and> (\<forall>y\<in>t. y \<in> T \<longrightarrow> y = x)"]
-    by auto
-  obtain g where g: "g \<subseteq> {t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
-    using assms(1)[unfolded compact_eq_heine_borel, THEN spec[where x="{t. \<exists>x. x\<in>s \<and> t = f x}"]]
-    using f by auto
-  from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa"
-    by auto
-  {
-    fix x y
-    assume "x \<in> t" "y \<in> t" "f x = f y"
-    then have "x \<in> f x"  "y \<in> f x \<longrightarrow> y = x"
-      using f[THEN bspec[where x=x]] and \<open>t \<subseteq> s\<close> by auto
-    then have "x = y"
-      using \<open>f x = f y\<close> and f[THEN bspec[where x=y]] and \<open>y \<in> t\<close> and \<open>t \<subseteq> s\<close>
-      by auto
-  }
-  then have "inj_on f t"
-    unfolding inj_on_def by simp
-  then have "infinite (f ` t)"
-    using assms(2) using finite_imageD by auto
-  moreover
-  {
-    fix x
-    assume "x \<in> t" "f x \<notin> g"
-    from g(3) assms(3) \<open>x \<in> t\<close> obtain h where "h \<in> g" and "x \<in> h"
-      by auto
-    then obtain y where "y \<in> s" "h = f y"
-      using g'[THEN bspec[where x=h]] by auto
-    then have "y = x"
-      using f[THEN bspec[where x=y]] and \<open>x\<in>t\<close> and \<open>x\<in>h\<close>[unfolded \<open>h = f y\<close>]
-      by auto
-    then have False
-      using \<open>f x \<notin> g\<close> \<open>h \<in> g\<close> unfolding \<open>h = f y\<close>
-      by auto
-  }
-  then have "f ` t \<subseteq> g" by auto
-  ultimately show False
-    using g(2) using finite_subset by auto
-qed
-
-lemma acc_point_range_imp_convergent_subsequence:
-  fixes l :: "'a :: first_countable_topology"
-  assumes l: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> range f)"
-  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
-proof -
-  from countable_basis_at_decseq[of l]
-  obtain A where A:
-      "\<And>i. open (A i)"
-      "\<And>i. l \<in> A i"
-      "\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
-    by blast
-  define s where "s n i = (SOME j. i < j \<and> f j \<in> A (Suc n))" for n i
-  {
-    fix n i
-    have "infinite (A (Suc n) \<inter> range f - f`{.. i})"
-      using l A by auto
-    then have "\<exists>x. x \<in> A (Suc n) \<inter> range f - f`{.. i}"
-      unfolding ex_in_conv by (intro notI) simp
-    then have "\<exists>j. f j \<in> A (Suc n) \<and> j \<notin> {.. i}"
-      by auto
-    then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)"
-      by (auto simp: not_le)
-    then have "i < s n i" "f (s n i) \<in> A (Suc n)"
-      unfolding s_def by (auto intro: someI2_ex)
-  }
-  note s = this
-  define r where "r = rec_nat (s 0 0) s"
-  have "strict_mono r"
-    by (auto simp: r_def s strict_mono_Suc_iff)
-  moreover
-  have "(\<lambda>n. f (r n)) \<longlonglongrightarrow> l"
-  proof (rule topological_tendstoI)
-    fix S
-    assume "open S" "l \<in> S"
-    with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
-      by auto
-    moreover
-    {
-      fix i
-      assume "Suc 0 \<le> i"
-      then have "f (r i) \<in> A i"
-        by (cases i) (simp_all add: r_def s)
-    }
-    then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially"
-      by (auto simp: eventually_sequentially)
-    ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"
-      by eventually_elim auto
-  qed
-  ultimately show "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
-    by (auto simp: convergent_def comp_def)
-qed
-
-lemma sequence_infinite_lemma:
-  fixes f :: "nat \<Rightarrow> 'a::t1_space"
-  assumes "\<forall>n. f n \<noteq> l"
-    and "(f \<longlongrightarrow> l) sequentially"
-  shows "infinite (range f)"
-proof
-  assume "finite (range f)"
-  then have "closed (range f)"
-    by (rule finite_imp_closed)
-  then have "open (- range f)"
-    by (rule open_Compl)
-  from assms(1) have "l \<in> - range f"
-    by auto
-  from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
-    using \<open>open (- range f)\<close> \<open>l \<in> - range f\<close>
-    by (rule topological_tendstoD)
-  then show False
-    unfolding eventually_sequentially
-    by auto
-qed
-
-lemma closure_insert:
-  fixes x :: "'a::t1_space"
-  shows "closure (insert x s) = insert x (closure s)"
-  apply (rule closure_unique)
-  apply (rule insert_mono [OF closure_subset])
-  apply (rule closed_insert [OF closed_closure])
-  apply (simp add: closure_minimal)
-  done
-
-lemma islimpt_insert:
-  fixes x :: "'a::t1_space"
-  shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
-proof
-  assume *: "x islimpt (insert a s)"
-  show "x islimpt s"
-  proof (rule islimptI)
-    fix t
-    assume t: "x \<in> t" "open t"
-    show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
-    proof (cases "x = a")
-      case True
-      obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
-        using * t by (rule islimptE)
-      with \<open>x = a\<close> show ?thesis by auto
-    next
-      case False
-      with t have t': "x \<in> t - {a}" "open (t - {a})"
-        by (simp_all add: open_Diff)
-      obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
-        using * t' by (rule islimptE)
-      then show ?thesis by auto
-    qed
-  qed
-next
-  assume "x islimpt s"
-  then show "x islimpt (insert a s)"
-    by (rule islimpt_subset) auto
-qed
-
-lemma islimpt_finite:
-  fixes x :: "'a::t1_space"
-  shows "finite s \<Longrightarrow> \<not> x islimpt s"
-  by (induct set: finite) (simp_all add: islimpt_insert)
-
-lemma islimpt_Un_finite:
-  fixes x :: "'a::t1_space"
-  shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
-  by (simp add: islimpt_Un islimpt_finite)
-
-lemma islimpt_eq_acc_point:
-  fixes l :: "'a :: t1_space"
-  shows "l islimpt S \<longleftrightarrow> (\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S))"
-proof (safe intro!: islimptI)
-  fix U
-  assume "l islimpt S" "l \<in> U" "open U" "finite (U \<inter> S)"
-  then have "l islimpt S" "l \<in> (U - (U \<inter> S - {l}))" "open (U - (U \<inter> S - {l}))"
-    by (auto intro: finite_imp_closed)
-  then show False
-    by (rule islimptE) auto
-next
-  fix T
-  assume *: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S)" "l \<in> T" "open T"
-  then have "infinite (T \<inter> S - {l})"
-    by auto
-  then have "\<exists>x. x \<in> (T \<inter> S - {l})"
-    unfolding ex_in_conv by (intro notI) simp
-  then show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> l"
-    by auto
-qed
-
-corollary infinite_openin:
-  fixes S :: "'a :: t1_space set"
-  shows "\<lbrakk>openin (subtopology euclidean U) S; x \<in> S; x islimpt U\<rbrakk> \<Longrightarrow> infinite S"
-  by (clarsimp simp add: openin_open islimpt_eq_acc_point inf_commute)
-
-lemma islimpt_range_imp_convergent_subsequence:
-  fixes l :: "'a :: {t1_space, first_countable_topology}"
-  assumes l: "l islimpt (range f)"
-  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
-  using l unfolding islimpt_eq_acc_point
-  by (rule acc_point_range_imp_convergent_subsequence)
-
-lemma islimpt_eq_infinite_ball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> ball x e))"
-  apply (simp add: islimpt_eq_acc_point, safe)
-   apply (metis Int_commute open_ball centre_in_ball)
-  by (metis open_contains_ball Int_mono finite_subset inf_commute subset_refl)
-
-lemma islimpt_eq_infinite_cball: "x islimpt S \<longleftrightarrow> (\<forall>e>0. infinite(S \<inter> cball x e))"
-  apply (simp add: islimpt_eq_infinite_ball, safe)
-   apply (meson Int_mono ball_subset_cball finite_subset order_refl)
-  by (metis open_ball centre_in_ball finite_Int inf.absorb_iff2 inf_assoc open_contains_cball_eq)
-
-lemma sequence_unique_limpt:
-  fixes f :: "nat \<Rightarrow> 'a::t2_space"
-  assumes "(f \<longlongrightarrow> l) sequentially"
-    and "l' islimpt (range f)"
-  shows "l' = l"
-proof (rule ccontr)
-  assume "l' \<noteq> l"
-  obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
-    using hausdorff [OF \<open>l' \<noteq> l\<close>] by auto
-  have "eventually (\<lambda>n. f n \<in> t) sequentially"
-    using assms(1) \<open>open t\<close> \<open>l \<in> t\<close> by (rule topological_tendstoD)
-  then obtain N where "\<forall>n\<ge>N. f n \<in> t"
-    unfolding eventually_sequentially by auto
-
-  have "UNIV = {..<N} \<union> {N..}"
-    by auto
-  then have "l' islimpt (f ` ({..<N} \<union> {N..}))"
-    using assms(2) by simp
-  then have "l' islimpt (f ` {..<N} \<union> f ` {N..})"
-    by (simp add: image_Un)
-  then have "l' islimpt (f ` {N..})"
-    by (simp add: islimpt_Un_finite)
-  then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
-    using \<open>l' \<in> s\<close> \<open>open s\<close> by (rule islimptE)
-  then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'"
-    by auto
-  with \<open>\<forall>n\<ge>N. f n \<in> t\<close> have "f n \<in> s \<inter> t"
-    by simp
-  with \<open>s \<inter> t = {}\<close> show False
-    by simp
-qed
-
-lemma bolzano_weierstrass_imp_closed:
-  fixes s :: "'a::{first_countable_topology,t2_space} set"
-  assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
-  shows "closed s"
-proof -
-  {
-    fix x l
-    assume as: "\<forall>n::nat. x n \<in> s" "(x \<longlongrightarrow> l) sequentially"
-    then have "l \<in> s"
-    proof (cases "\<forall>n. x n \<noteq> l")
-      case False
-      then show "l\<in>s" using as(1) by auto
-    next
-      case True note cas = this
-      with as(2) have "infinite (range x)"
-        using sequence_infinite_lemma[of x l] by auto
-      then obtain l' where "l'\<in>s" "l' islimpt (range x)"
-        using assms[THEN spec[where x="range x"]] as(1) by auto
-      then show "l\<in>s" using sequence_unique_limpt[of x l l']
-        using as cas by auto
-    qed
-  }
-  then show ?thesis
-    unfolding closed_sequential_limits by fast
-qed
-
-lemma compact_imp_bounded:
-  assumes "compact U"
-  shows "bounded U"
-proof -
-  have "compact U" "\<forall>x\<in>U. open (ball x 1)" "U \<subseteq> (\<Union>x\<in>U. ball x 1)"
-    using assms by auto
-  then obtain D where D: "D \<subseteq> U" "finite D" "U \<subseteq> (\<Union>x\<in>D. ball x 1)"
-    by (metis compactE_image)
-  from \<open>finite D\<close> have "bounded (\<Union>x\<in>D. ball x 1)"
-    by (simp add: bounded_UN)
-  then show "bounded U" using \<open>U \<subseteq> (\<Union>x\<in>D. ball x 1)\<close>
-    by (rule bounded_subset)
-qed
-
-text\<open>In particular, some common special cases.\<close>
-
-lemma compact_Un [intro]:
-  assumes "compact s"
-    and "compact t"
-  shows " compact (s \<union> t)"
-proof (rule compactI)
-  fix f
-  assume *: "Ball f open" "s \<union> t \<subseteq> \<Union>f"
-  from * \<open>compact s\<close> obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"
-    unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
-  moreover
-  from * \<open>compact t\<close> obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"
-    unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f])
-  ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"
-    by (auto intro!: exI[of _ "s' \<union> t'"])
-qed
-
-lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"
-  by (induct set: finite) auto
-
-lemma compact_UN [intro]:
-  "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"
-  by (rule compact_Union) auto
-
-lemma closed_Int_compact [intro]:
-  assumes "closed s"
-    and "compact t"
-  shows "compact (s \<inter> t)"
-  using compact_Int_closed [of t s] assms
-  by (simp add: Int_commute)
-
-lemma compact_Int [intro]:
-  fixes s t :: "'a :: t2_space set"
-  assumes "compact s"
-    and "compact t"
-  shows "compact (s \<inter> t)"
-  using assms by (intro compact_Int_closed compact_imp_closed)
-
-lemma compact_sing [simp]: "compact {a}"
-  unfolding compact_eq_heine_borel by auto
-
-lemma compact_insert [simp]:
-  assumes "compact s"
-  shows "compact (insert x s)"
-proof -
-  have "compact ({x} \<union> s)"
-    using compact_sing assms by (rule compact_Un)
-  then show ?thesis by simp
-qed
-
-lemma finite_imp_compact: "finite s \<Longrightarrow> compact s"
-  by (induct set: finite) simp_all
-
-lemma open_delete:
-  fixes s :: "'a::t1_space set"
-  shows "open s \<Longrightarrow> open (s - {x})"
-  by (simp add: open_Diff)
-
-lemma openin_delete:
-  fixes a :: "'a :: t1_space"
-  shows "openin (subtopology euclidean u) s
-         \<Longrightarrow> openin (subtopology euclidean u) (s - {a})"
-by (metis Int_Diff open_delete openin_open)
-
-text\<open>Compactness expressed with filters\<close>
-
-lemma closure_iff_nhds_not_empty:
-  "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"
-proof safe
-  assume x: "x \<in> closure X"
-  fix S A
-  assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"
-  then have "x \<notin> closure (-S)"
-    by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)
-  with x have "x \<in> closure X - closure (-S)"
-    by auto
-  also have "\<dots> \<subseteq> closure (X \<inter> S)"
-    using \<open>open S\<close> open_Int_closure_subset[of S X] by (simp add: closed_Compl ac_simps)
-  finally have "X \<inter> S \<noteq> {}" by auto
-  then show False using \<open>X \<inter> A = {}\<close> \<open>S \<subseteq> A\<close> by auto
-next
-  assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"
-  from this[THEN spec, of "- X", THEN spec, of "- closure X"]
-  show "x \<in> closure X"
-    by (simp add: closure_subset open_Compl)
-qed
-
-corollary closure_Int_ball_not_empty:
-  assumes "S \<subseteq> closure T" "x \<in> S" "r > 0"
-  shows "T \<inter> ball x r \<noteq> {}"
-  using assms centre_in_ball closure_iff_nhds_not_empty by blast
-
-lemma compact_filter:
-  "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))"
-proof (intro allI iffI impI compact_fip[THEN iffD2] notI)
-  fix F
-  assume "compact U"
-  assume F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"
-  then have "U \<noteq> {}"
-    by (auto simp: eventually_False)
-
-  define Z where "Z = closure ` {A. eventually (\<lambda>x. x \<in> A) F}"
-  then have "\<forall>z\<in>Z. closed z"
-    by auto
-  moreover
-  have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"
-    unfolding Z_def by (auto elim: eventually_mono intro: set_mp[OF closure_subset])
-  have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"
-  proof (intro allI impI)
-    fix B assume "finite B" "B \<subseteq> Z"
-    with \<open>finite B\<close> ev_Z F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"
-      by (auto simp: eventually_ball_finite_distrib eventually_conj_iff)
-    with F show "U \<inter> \<Inter>B \<noteq> {}"
-      by (intro notI) (simp add: eventually_False)
-  qed
-  ultimately have "U \<inter> \<Inter>Z \<noteq> {}"
-    using \<open>compact U\<close> unfolding compact_fip by blast
-  then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z"
-    by auto
-
-  have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"
-    unfolding eventually_inf eventually_nhds
-  proof safe
-    fix P Q R S
-    assume "eventually R F" "open S" "x \<in> S"
-    with open_Int_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]
-    have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)
-    moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"
-    ultimately show False by (auto simp: set_eq_iff)
-  qed
-  with \<open>x \<in> U\<close> show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"
-    by (metis eventually_bot)
-next
-  fix A
-  assume A: "\<forall>a\<in>A. closed a" "\<forall>B\<subseteq>A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}" "U \<inter> \<Inter>A = {}"
-  define F where "F = (INF a\<in>insert U A. principal a)"
-  have "F \<noteq> bot"
-    unfolding F_def
-  proof (rule INF_filter_not_bot)
-    fix X
-    assume X: "X \<subseteq> insert U A" "finite X"
-    with A(2)[THEN spec, of "X - {U}"] have "U \<inter> \<Inter>(X - {U}) \<noteq> {}"
-      by auto
-    with X show "(INF a\<in>X. principal a) \<noteq> bot"
-      by (auto simp: INF_principal_finite principal_eq_bot_iff)
-  qed
-  moreover
-  have "F \<le> principal U"
-    unfolding F_def by auto
-  then have "eventually (\<lambda>x. x \<in> U) F"
-    by (auto simp: le_filter_def eventually_principal)
-  moreover
-  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)"
-  ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"
-    by auto
-
-  { fix V assume "V \<in> A"
-    then have "F \<le> principal V"
-      unfolding F_def by (intro INF_lower2[of V]) auto
-    then have V: "eventually (\<lambda>x. x \<in> V) F"
-      by (auto simp: le_filter_def eventually_principal)
-    have "x \<in> closure V"
-      unfolding closure_iff_nhds_not_empty
-    proof (intro impI allI)
-      fix S A
-      assume "open S" "x \<in> S" "S \<subseteq> A"
-      then have "eventually (\<lambda>x. x \<in> A) (nhds x)"
-        by (auto simp: eventually_nhds)
-      with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"
-        by (auto simp: eventually_inf)
-      with x show "V \<inter> A \<noteq> {}"
-        by (auto simp del: Int_iff simp add: trivial_limit_def)
-    qed
-    then have "x \<in> V"
-      using \<open>V \<in> A\<close> A(1) by simp
-  }
-  with \<open>x\<in>U\<close> have "x \<in> U \<inter> \<Inter>A" by auto
-  with \<open>U \<inter> \<Inter>A = {}\<close> show False by auto
-qed
-
-definition%important "countably_compact U \<longleftrightarrow>
-    (\<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))"
-
-lemma countably_compactE:
-  assumes "countably_compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C" "countable C"
-  obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
-  using assms unfolding countably_compact_def by metis
-
-lemma countably_compactI:
-  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')"
-  shows "countably_compact s"
-  using assms unfolding countably_compact_def by metis
-
-lemma compact_imp_countably_compact: "compact U \<Longrightarrow> countably_compact U"
-  by (auto simp: compact_eq_heine_borel countably_compact_def)
-
-lemma countably_compact_imp_compact:
-  assumes "countably_compact U"
-    and ccover: "countable B" "\<forall>b\<in>B. open b"
-    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"
-  shows "compact U"
-  using \<open>countably_compact U\<close>
-  unfolding compact_eq_heine_borel countably_compact_def
-proof safe
-  fix A
-  assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
-  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)"
-  moreover define C where "C = {b\<in>B. \<exists>a\<in>A. b \<inter> U \<subseteq> a}"
-  ultimately have "countable C" "\<forall>a\<in>C. open a"
-    unfolding C_def using ccover by auto
-  moreover
-  have "\<Union>A \<inter> U \<subseteq> \<Union>C"
-  proof safe
-    fix x a
-    assume "x \<in> U" "x \<in> a" "a \<in> A"
-    with basis[of a x] A obtain b where "b \<in> B" "x \<in> b" "b \<inter> U \<subseteq> a"
-      by blast
-    with \<open>a \<in> A\<close> show "x \<in> \<Union>C"
-      unfolding C_def by auto
-  qed
-  then have "U \<subseteq> \<Union>C" using \<open>U \<subseteq> \<Union>A\<close> by auto
-  ultimately obtain T where T: "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"
-    using * by metis
-  then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<inter> U \<subseteq> a"
-    by (auto simp: C_def)
-  then obtain f where "\<forall>t\<in>T. f t \<in> A \<and> t \<inter> U \<subseteq> f t"
-    unfolding bchoice_iff Bex_def ..
-  with T show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
-    unfolding C_def by (intro exI[of _ "f`T"]) fastforce
-qed
-
-proposition countably_compact_imp_compact_second_countable:
-  "countably_compact U \<Longrightarrow> compact (U :: 'a :: second_countable_topology set)"
-proof (rule countably_compact_imp_compact)
-  fix T and x :: 'a
-  assume "open T" "x \<in> T"
-  from topological_basisE[OF is_basis this] obtain b where
-    "b \<in> (SOME B. countable B \<and> topological_basis B)" "x \<in> b" "b \<subseteq> T" .
-  then show "\<exists>b\<in>SOME B. countable B \<and> topological_basis B. x \<in> b \<and> b \<inter> U \<subseteq> T"
-    by blast
-qed (insert countable_basis topological_basis_open[OF is_basis], auto)
-
-lemma countably_compact_eq_compact:
-  "countably_compact U \<longleftrightarrow> compact (U :: 'a :: second_countable_topology set)"
-  using countably_compact_imp_compact_second_countable compact_imp_countably_compact by blast
-
-subsubsection\<open>Sequential compactness\<close>
-
-definition%important seq_compact :: "'a::topological_space set \<Rightarrow> bool"
-  where "seq_compact S \<longleftrightarrow>
-    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially))"
-
-lemma seq_compactI:
-  assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
-  shows "seq_compact S"
-  unfolding seq_compact_def using assms by fast
-
-lemma seq_compactE:
-  assumes "seq_compact S" "\<forall>n. f n \<in> S"
-  obtains l r where "l \<in> S" "strict_mono (r :: nat \<Rightarrow> nat)" "((f \<circ> r) \<longlongrightarrow> l) sequentially"
-  using assms unfolding seq_compact_def by fast
-
-lemma closed_sequentially: (* TODO: move upwards *)
-  assumes "closed s" and "\<forall>n. f n \<in> s" and "f \<longlonglongrightarrow> l"
-  shows "l \<in> s"
-proof (rule ccontr)
-  assume "l \<notin> s"
-  with \<open>closed s\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "eventually (\<lambda>n. f n \<in> - s) sequentially"
-    by (fast intro: topological_tendstoD)
-  with \<open>\<forall>n. f n \<in> s\<close> show "False"
-    by simp
-qed
-
-lemma seq_compact_Int_closed:
-  assumes "seq_compact s" and "closed t"
-  shows "seq_compact (s \<inter> t)"
-proof (rule seq_compactI)
-  fix f assume "\<forall>n::nat. f n \<in> s \<inter> t"
-  hence "\<forall>n. f n \<in> s" and "\<forall>n. f n \<in> t"
-    by simp_all
-  from \<open>seq_compact s\<close> and \<open>\<forall>n. f n \<in> s\<close>
-  obtain l r where "l \<in> s" and r: "strict_mono r" and l: "(f \<circ> r) \<longlonglongrightarrow> l"
-    by (rule seq_compactE)
-  from \<open>\<forall>n. f n \<in> t\<close> have "\<forall>n. (f \<circ> r) n \<in> t"
-    by simp
-  from \<open>closed t\<close> and this and l have "l \<in> t"
-    by (rule closed_sequentially)
-  with \<open>l \<in> s\<close> and r and l show "\<exists>l\<in>s \<inter> t. \<exists>r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
-    by fast
-qed
-
-lemma seq_compact_closed_subset:
-  assumes "closed s" and "s \<subseteq> t" and "seq_compact t"
-  shows "seq_compact s"
-  using assms seq_compact_Int_closed [of t s] by (simp add: Int_absorb1)
-
-lemma seq_compact_imp_countably_compact:
-  fixes U :: "'a :: first_countable_topology set"
-  assumes "seq_compact U"
-  shows "countably_compact U"
-proof (safe intro!: countably_compactI)
-  fix A
-  assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"
-  have subseq: "\<And>X. range X \<subseteq> U \<Longrightarrow> \<exists>r x. x \<in> U \<and> strict_mono (r :: nat \<Rightarrow> nat) \<and> (X \<circ> r) \<longlonglongrightarrow> x"
-    using \<open>seq_compact U\<close> by (fastforce simp: seq_compact_def subset_eq)
-  show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
-  proof cases
-    assume "finite A"
-    with A show ?thesis by auto
-  next
-    assume "infinite A"
-    then have "A \<noteq> {}" by auto
-    show ?thesis
-    proof (rule ccontr)
-      assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
-      then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)"
-        by auto
-      then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T"
-        by metis
-      define X where "X n = X' (from_nat_into A ` {.. n})" for n
-      have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"
-        using \<open>A \<noteq> {}\<close> unfolding X_def by (intro T) (auto intro: from_nat_into)
-      then have "range X \<subseteq> U"
-        by auto
-      with subseq[of X] obtain r x where "x \<in> U" and r: "strict_mono r" "(X \<circ> r) \<longlonglongrightarrow> x"
-        by auto
-      from \<open>x\<in>U\<close> \<open>U \<subseteq> \<Union>A\<close> from_nat_into_surj[OF \<open>countable A\<close>]
-      obtain n where "x \<in> from_nat_into A n" by auto
-      with r(2) A(1) from_nat_into[OF \<open>A \<noteq> {}\<close>, of n]
-      have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"
-        unfolding tendsto_def by (auto simp: comp_def)
-      then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"
-        by (auto simp: eventually_sequentially)
-      moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"
-        by auto
-      moreover from \<open>strict_mono r\<close>[THEN seq_suble, of "max n N"] have "\<exists>i. n \<le> r i \<and> N \<le> i"
-        by (auto intro!: exI[of _ "max n N"])
-      ultimately show False
-        by auto
-    qed
-  qed
-qed
-
-lemma compact_imp_seq_compact:
-  fixes U :: "'a :: first_countable_topology set"
-  assumes "compact U"
-  shows "seq_compact U"
-  unfolding seq_compact_def
-proof safe
-  fix X :: "nat \<Rightarrow> 'a"
-  assume "\<forall>n. X n \<in> U"
-  then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"
-    by (auto simp: eventually_filtermap)
-  moreover
-  have "filtermap X sequentially \<noteq> bot"
-    by (simp add: trivial_limit_def eventually_filtermap)
-  ultimately
-  obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")
-    using \<open>compact U\<close> by (auto simp: compact_filter)
-
-  from countable_basis_at_decseq[of x]
-  obtain A where A:
-      "\<And>i. open (A i)"
-      "\<And>i. x \<in> A i"
-      "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
-    by blast
-  define s where "s n i = (SOME j. i < j \<and> X j \<in> A (Suc n))" for n i
-  {
-    fix n i
-    have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"
-    proof (rule ccontr)
-      assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"
-      then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)"
-        by auto
-      then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"
-        by (auto simp: eventually_filtermap eventually_sequentially)
-      moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"
-        using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)
-      ultimately have "eventually (\<lambda>x. False) ?F"
-        by (auto simp: eventually_inf)
-      with x show False
-        by (simp add: eventually_False)
-    qed
-    then have "i < s n i" "X (s n i) \<in> A (Suc n)"
-      unfolding s_def by (auto intro: someI2_ex)
-  }
-  note s = this
-  define r where "r = rec_nat (s 0 0) s"
-  have "strict_mono r"
-    by (auto simp: r_def s strict_mono_Suc_iff)
-  moreover
-  have "(\<lambda>n. X (r n)) \<longlonglongrightarrow> x"
-  proof (rule topological_tendstoI)
-    fix S
-    assume "open S" "x \<in> S"
-    with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially"
-      by auto
-    moreover
-    {
-      fix i
-      assume "Suc 0 \<le> i"
-      then have "X (r i) \<in> A i"
-        by (cases i) (simp_all add: r_def s)
-    }
-    then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially"
-      by (auto simp: eventually_sequentially)
-    ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"
-      by eventually_elim auto
-  qed
-  ultimately show "\<exists>x \<in> U. \<exists>r. strict_mono r \<and> (X \<circ> r) \<longlonglongrightarrow> x"
-    using \<open>x \<in> U\<close> by (auto simp: convergent_def comp_def)
-qed
-
-lemma countably_compact_imp_acc_point:
-  assumes "countably_compact s"
-    and "countable t"
-    and "infinite t"
-    and "t \<subseteq> s"
-  shows "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)"
-proof (rule ccontr)
-  define C where "C = (\<lambda>F. interior (F \<union> (- t))) ` {F. finite F \<and> F \<subseteq> t }"
-  note \<open>countably_compact s\<close>
-  moreover have "\<forall>t\<in>C. open t"
-    by (auto simp: C_def)
-  moreover
-  assume "\<not> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
-  then have s: "\<And>x. x \<in> s \<Longrightarrow> \<exists>U. x\<in>U \<and> open U \<and> finite (U \<inter> t)" by metis
-  have "s \<subseteq> \<Union>C"
-    using \<open>t \<subseteq> s\<close>
-    unfolding C_def
-    apply (safe dest!: s)
-    apply (rule_tac a="U \<inter> t" in UN_I)
-    apply (auto intro!: interiorI simp add: finite_subset)
-    done
-  moreover
-  from \<open>countable t\<close> have "countable C"
-    unfolding C_def by (auto intro: countable_Collect_finite_subset)
-  ultimately
-  obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> \<Union>D"
-    by (rule countably_compactE)
-  then obtain E where E: "E \<subseteq> {F. finite F \<and> F \<subseteq> t }" "finite E"
-    and s: "s \<subseteq> (\<Union>F\<in>E. interior (F \<union> (- t)))"
-    by (metis (lifting) finite_subset_image C_def)
-  from s \<open>t \<subseteq> s\<close> have "t \<subseteq> \<Union>E"
-    using interior_subset by blast
-  moreover have "finite (\<Union>E)"
-    using E by auto
-  ultimately show False using \<open>infinite t\<close>
-    by (auto simp: finite_subset)
-qed
-
-lemma countable_acc_point_imp_seq_compact:
-  fixes s :: "'a::first_countable_topology set"
-  assumes "\<forall>t. infinite t \<and> countable t \<and> t \<subseteq> s \<longrightarrow>
-    (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
-  shows "seq_compact s"
-proof -
-  {
-    fix f :: "nat \<Rightarrow> 'a"
-    assume f: "\<forall>n. f n \<in> s"
-    have "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
-    proof (cases "finite (range f)")
-      case True
-      obtain l where "infinite {n. f n = f l}"
-        using pigeonhole_infinite[OF _ True] by auto
-      then obtain r :: "nat \<Rightarrow> nat" where "strict_mono  r" and fr: "\<forall>n. f (r n) = f l"
-        using infinite_enumerate by blast
-      then have "strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> f l"
-        by (simp add: fr o_def)
-      with f show "\<exists>l\<in>s. \<exists>r. strict_mono  r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
-        by auto
-    next
-      case False
-      with f assms have "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)"
-        by auto
-      then obtain l where "l \<in> s" "\<forall>U. l\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" ..
-      from this(2) have "\<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
-        using acc_point_range_imp_convergent_subsequence[of l f] by auto
-      with \<open>l \<in> s\<close> show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially" ..
-    qed
-  }
-  then show ?thesis
-    unfolding seq_compact_def by auto
-qed
-
-lemma seq_compact_eq_countably_compact:
-  fixes U :: "'a :: first_countable_topology set"
-  shows "seq_compact U \<longleftrightarrow> countably_compact U"
-  using
-    countable_acc_point_imp_seq_compact
-    countably_compact_imp_acc_point
-    seq_compact_imp_countably_compact
-  by metis
-
-lemma seq_compact_eq_acc_point:
-  fixes s :: "'a :: first_countable_topology set"
-  shows "seq_compact s \<longleftrightarrow>
-    (\<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)))"
-  using
-    countable_acc_point_imp_seq_compact[of s]
-    countably_compact_imp_acc_point[of s]
-    seq_compact_imp_countably_compact[of s]
-  by metis
-
-lemma seq_compact_eq_compact:
-  fixes U :: "'a :: second_countable_topology set"
-  shows "seq_compact U \<longleftrightarrow> compact U"
-  using seq_compact_eq_countably_compact countably_compact_eq_compact by blast
-
-proposition bolzano_weierstrass_imp_seq_compact:
-  fixes s :: "'a::{t1_space, first_countable_topology} set"
-  shows "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> seq_compact s"
-  by (rule countable_acc_point_imp_seq_compact) (metis islimpt_eq_acc_point)
-
-
-subsubsection\<open>Totally bounded\<close>
-
-lemma cauchy_def: "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N \<longrightarrow> dist (s m) (s n) < e)"
-  unfolding Cauchy_def by metis
-
-proposition seq_compact_imp_totally_bounded:
-  assumes "seq_compact s"
-  shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>x\<in>k. ball x e)"
-proof -
-  { fix e::real assume "e > 0" assume *: "\<And>k. finite k \<Longrightarrow> k \<subseteq> s \<Longrightarrow> \<not> s \<subseteq> (\<Union>x\<in>k. ball x e)"
-    let ?Q = "\<lambda>x n r. r \<in> s \<and> (\<forall>m < (n::nat). \<not> (dist (x m) r < e))"
-    have "\<exists>x. \<forall>n::nat. ?Q x n (x n)"
-    proof (rule dependent_wellorder_choice)
-      fix n x assume "\<And>y. y < n \<Longrightarrow> ?Q x y (x y)"
-      then have "\<not> s \<subseteq> (\<Union>x\<in>x ` {0..<n}. ball x e)"
-        using *[of "x ` {0 ..< n}"] by (auto simp: subset_eq)
-      then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)"
-        unfolding subset_eq by auto
-      show "\<exists>r. ?Q x n r"
-        using z by auto
-    qed simp
-    then obtain x where "\<forall>n::nat. x n \<in> s" and x:"\<And>n m. m < n \<Longrightarrow> \<not> (dist (x m) (x n) < e)"
-      by blast
-    then obtain l r where "l \<in> s" and r:"strict_mono  r" and "((x \<circ> r) \<longlongrightarrow> l) sequentially"
-      using assms by (metis seq_compact_def)
-    from this(3) have "Cauchy (x \<circ> r)"
-      using LIMSEQ_imp_Cauchy by auto
-    then obtain N::nat where "\<And>m n. N \<le> m \<Longrightarrow> N \<le> n \<Longrightarrow> dist ((x \<circ> r) m) ((x \<circ> r) n) < e"
-      unfolding cauchy_def using \<open>e > 0\<close> by blast
-    then have False
-      using x[of "r N" "r (N+1)"] r by (auto simp: strict_mono_def) }
-  then show ?thesis
-    by metis
-qed
-
-subsubsection\<open>Heine-Borel theorem\<close>
-
-proposition seq_compact_imp_heine_borel:
-  fixes s :: "'a :: metric_space set"
-  assumes "seq_compact s"
-  shows "compact s"
-proof -
-  from seq_compact_imp_totally_bounded[OF \<open>seq_compact s\<close>]
-  obtain f where f: "\<forall>e>0. finite (f e) \<and> f e \<subseteq> s \<and> s \<subseteq> (\<Union>x\<in>f e. ball x e)"
-    unfolding choice_iff' ..
-  define K where "K = (\<lambda>(x, r). ball x r) ` ((\<Union>e \<in> \<rat> \<inter> {0 <..}. f e) \<times> \<rat>)"
-  have "countably_compact s"
-    using \<open>seq_compact s\<close> by (rule seq_compact_imp_countably_compact)
-  then show "compact s"
-  proof (rule countably_compact_imp_compact)
-    show "countable K"
-      unfolding K_def using f
-      by (auto intro: countable_finite countable_subset countable_rat
-               intro!: countable_image countable_SIGMA countable_UN)
-    show "\<forall>b\<in>K. open b" by (auto simp: K_def)
-  next
-    fix T x
-    assume T: "open T" "x \<in> T" and x: "x \<in> s"
-    from openE[OF T] obtain e where "0 < e" "ball x e \<subseteq> T"
-      by auto
-    then have "0 < e / 2" "ball x (e / 2) \<subseteq> T"
-      by auto
-    from Rats_dense_in_real[OF \<open>0 < e / 2\<close>] obtain r where "r \<in> \<rat>" "0 < r" "r < e / 2"
-      by auto
-    from f[rule_format, of r] \<open>0 < r\<close> \<open>x \<in> s\<close> obtain k where "k \<in> f r" "x \<in> ball k r"
-      by auto
-    from \<open>r \<in> \<rat>\<close> \<open>0 < r\<close> \<open>k \<in> f r\<close> have "ball k r \<in> K"
-      by (auto simp: K_def)
-    then show "\<exists>b\<in>K. x \<in> b \<and> b \<inter> s \<subseteq> T"
-    proof (rule bexI[rotated], safe)
-      fix y
-      assume "y \<in> ball k r"
-      with \<open>r < e / 2\<close> \<open>x \<in> ball k r\<close> have "dist x y < e"
-        by (intro dist_triangle_half_r [of k _ e]) (auto simp: dist_commute)
-      with \<open>ball x e \<subseteq> T\<close> show "y \<in> T"
-        by auto
-    next
-      show "x \<in> ball k r" by fact
-    qed
-  qed
-qed
-
-proposition compact_eq_seq_compact_metric:
-  "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"
-  using compact_imp_seq_compact seq_compact_imp_heine_borel by blast
-
-proposition compact_def: \<comment> \<open>this is the definition of compactness in HOL Light\<close>
-  "compact (S :: 'a::metric_space set) \<longleftrightarrow>
-   (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l))"
-  unfolding compact_eq_seq_compact_metric seq_compact_def by auto
-
-subsubsection \<open>Complete the chain of compactness variants\<close>
-
-proposition compact_eq_bolzano_weierstrass:
-  fixes s :: "'a::metric_space set"
-  shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))"
-  (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs
-    using heine_borel_imp_bolzano_weierstrass[of s] by auto
-next
-  assume ?rhs
-  then show ?lhs
-    unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)
-qed
-
-proposition bolzano_weierstrass_imp_bounded:
-  "\<forall>t. infinite t \<and> t \<subseteq> s \<longrightarrow> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> bounded s"
-  using compact_imp_bounded unfolding compact_eq_bolzano_weierstrass .
-
-
-subsection \<open>Metric spaces with the Heine-Borel property\<close>
-
-text \<open>
-  A metric space (or topological vector space) is said to have the
-  Heine-Borel property if every closed and bounded subset is compact.
-\<close>
-
-class heine_borel = metric_space +
-  assumes bounded_imp_convergent_subsequence:
-    "bounded (range f) \<Longrightarrow> \<exists>l r. strict_mono (r::nat\<Rightarrow>nat) \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
-
-proposition bounded_closed_imp_seq_compact:
-  fixes s::"'a::heine_borel set"
-  assumes "bounded s"
-    and "closed s"
-  shows "seq_compact s"
-proof (unfold seq_compact_def, clarify)
-  fix f :: "nat \<Rightarrow> 'a"
-  assume f: "\<forall>n. f n \<in> s"
-  with \<open>bounded s\<close> have "bounded (range f)"
-    by (auto intro: bounded_subset)
-  obtain l r where r: "strict_mono (r :: nat \<Rightarrow> nat)" and l: "((f \<circ> r) \<longlongrightarrow> l) sequentially"
-    using bounded_imp_convergent_subsequence [OF \<open>bounded (range f)\<close>] by auto
-  from f have fr: "\<forall>n. (f \<circ> r) n \<in> s"
-    by simp
-  have "l \<in> s" using \<open>closed s\<close> fr l
-    by (rule closed_sequentially)
-  show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
-    using \<open>l \<in> s\<close> r l by blast
-qed
-
-lemma compact_eq_bounded_closed:
-  fixes s :: "'a::heine_borel set"
-  shows "compact s \<longleftrightarrow> bounded s \<and> closed s"
-  (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs
-    using compact_imp_closed compact_imp_bounded
-    by blast
-next
-  assume ?rhs
-  then show ?lhs
-    using bounded_closed_imp_seq_compact[of s]
-    unfolding compact_eq_seq_compact_metric
-    by auto
-qed
-
-lemma compact_Inter:
-  fixes \<F> :: "'a :: heine_borel set set"
-  assumes com: "\<And>S. S \<in> \<F> \<Longrightarrow> compact S" and "\<F> \<noteq> {}"
-  shows "compact(\<Inter> \<F>)"
-  using assms
-  by (meson Inf_lower all_not_in_conv bounded_subset closed_Inter compact_eq_bounded_closed)
-
-lemma compact_closure [simp]:
-  fixes S :: "'a::heine_borel set"
-  shows "compact(closure S) \<longleftrightarrow> bounded S"
-by (meson bounded_closure bounded_subset closed_closure closure_subset compact_eq_bounded_closed)
-
-lemma not_compact_UNIV[simp]:
-  fixes s :: "'a::{real_normed_vector,perfect_space,heine_borel} set"
-  shows "~ compact (UNIV::'a set)"
-    by (simp add: compact_eq_bounded_closed)
-
-text\<open>Representing sets as the union of a chain of compact sets.\<close>
-lemma closed_Union_compact_subsets:
-  fixes S :: "'a::{heine_borel,real_normed_vector} set"
-  assumes "closed S"
-  obtains F where "\<And>n. compact(F n)" "\<And>n. F n \<subseteq> S" "\<And>n. F n \<subseteq> F(Suc n)"
-                  "(\<Union>n. F n) = S" "\<And>K. \<lbrakk>compact K; K \<subseteq> S\<rbrakk> \<Longrightarrow> \<exists>N. \<forall>n \<ge> N. K \<subseteq> F n"
-proof
-  show "compact (S \<inter> cball 0 (of_nat n))" for n
-    using assms compact_eq_bounded_closed by auto
-next
-  show "(\<Union>n. S \<inter> cball 0 (real n)) = S"
-    by (auto simp: real_arch_simple)
-next
-  fix K :: "'a set"
-  assume "compact K" "K \<subseteq> S"
-  then obtain N where "K \<subseteq> cball 0 N"
-    by (meson bounded_pos mem_cball_0 compact_imp_bounded subsetI)
-  then show "\<exists>N. \<forall>n\<ge>N. K \<subseteq> S \<inter> cball 0 (real n)"
-    by (metis of_nat_le_iff Int_subset_iff \<open>K \<subseteq> S\<close> real_arch_simple subset_cball subset_trans)
-qed auto
-
-instance%important real :: heine_borel
-proof%unimportant
-  fix f :: "nat \<Rightarrow> real"
-  assume f: "bounded (range f)"
-  obtain r :: "nat \<Rightarrow> nat" where r: "strict_mono r" "monoseq (f \<circ> r)"
-    unfolding comp_def by (metis seq_monosub)
-  then have "Bseq (f \<circ> r)"
-    unfolding Bseq_eq_bounded using f by (force intro: bounded_subset)
-  with r show "\<exists>l r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
-    using Bseq_monoseq_convergent[of "f \<circ> r"] by (auto simp: convergent_def)
-qed
-
-lemma compact_lemma_general:
-  fixes f :: "nat \<Rightarrow> 'a"
-  fixes proj::"'a \<Rightarrow> 'b \<Rightarrow> 'c::heine_borel" (infixl "proj" 60)
-  fixes unproj:: "('b \<Rightarrow> 'c) \<Rightarrow> 'a"
-  assumes finite_basis: "finite basis"
-  assumes bounded_proj: "\<And>k. k \<in> basis \<Longrightarrow> bounded ((\<lambda>x. x proj k) ` range f)"
-  assumes proj_unproj: "\<And>e k. k \<in> basis \<Longrightarrow> (unproj e) proj k = e k"
-  assumes unproj_proj: "\<And>x. unproj (\<lambda>k. x proj k) = x"
-  shows "\<forall>d\<subseteq>basis. \<exists>l::'a. \<exists> r::nat\<Rightarrow>nat.
-    strict_mono r \<and> (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) proj i) (l proj i) < e) sequentially)"
-proof safe
-  fix d :: "'b set"
-  assume d: "d \<subseteq> basis"
-  with finite_basis have "finite d"
-    by (blast intro: finite_subset)
-  from this d show "\<exists>l::'a. \<exists>r::nat\<Rightarrow>nat. strict_mono r \<and>
-    (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) proj i) (l proj i) < e) sequentially)"
-  proof (induct d)
-    case empty
-    then show ?case
-      unfolding strict_mono_def by auto
-  next
-    case (insert k d)
-    have k[intro]: "k \<in> basis"
-      using insert by auto
-    have s': "bounded ((\<lambda>x. x proj k) ` range f)"
-      using k
-      by (rule bounded_proj)
-    obtain l1::"'a" and r1 where r1: "strict_mono r1"
-      and lr1: "\<forall>e > 0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) proj i) (l1 proj i) < e) sequentially"
-      using insert(3) using insert(4) by auto
-    have f': "\<forall>n. f (r1 n) proj k \<in> (\<lambda>x. x proj k) ` range f"
-      by simp
-    have "bounded (range (\<lambda>i. f (r1 i) proj k))"
-      by (metis (lifting) bounded_subset f' image_subsetI s')
-    then obtain l2 r2 where r2:"strict_mono r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) proj k) \<longlongrightarrow> l2) sequentially"
-      using bounded_imp_convergent_subsequence[of "\<lambda>i. f (r1 i) proj k"]
-      by (auto simp: o_def)
-    define r where "r = r1 \<circ> r2"
-    have r:"strict_mono r"
-      using r1 and r2 unfolding r_def o_def strict_mono_def by auto
-    moreover
-    define l where "l = unproj (\<lambda>i. if i = k then l2 else l1 proj i)"
-    {
-      fix e::real
-      assume "e > 0"
-      from lr1 \<open>e > 0\<close> have N1: "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) proj i) (l1 proj i) < e) sequentially"
-        by blast
-      from lr2 \<open>e > 0\<close> have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) proj k) l2 < e) sequentially"
-        by (rule tendstoD)
-      from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) proj i) (l1 proj i) < e) sequentially"
-        by (rule eventually_subseq)
-      have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) proj i) (l proj i) < e) sequentially"
-        using N1' N2
-        by eventually_elim (insert insert.prems, auto simp: l_def r_def o_def proj_unproj)
-    }
-    ultimately show ?case by auto
-  qed
-qed
-
 lemma compact_lemma:
   fixes f :: "nat \<Rightarrow> 'a::euclidean_space"
   assumes "bounded (range f)"
@@ -4887,630 +763,8 @@
     by auto
 qed
 
-lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
-  unfolding bounded_def
-  by (metis (erased, hide_lams) dist_fst_le image_iff order_trans)
-
-lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
-  unfolding bounded_def
-  by (metis (no_types, hide_lams) dist_snd_le image_iff order.trans)
-
-instance%important prod :: (heine_borel, heine_borel) heine_borel
-proof%unimportant
-  fix f :: "nat \<Rightarrow> 'a \<times> 'b"
-  assume f: "bounded (range f)"
-  then have "bounded (fst ` range f)"
-    by (rule bounded_fst)
-  then have s1: "bounded (range (fst \<circ> f))"
-    by (simp add: image_comp)
-  obtain l1 r1 where r1: "strict_mono r1" and l1: "(\<lambda>n. fst (f (r1 n))) \<longlonglongrightarrow> l1"
-    using bounded_imp_convergent_subsequence [OF s1] unfolding o_def by fast
-  from f have s2: "bounded (range (snd \<circ> f \<circ> r1))"
-    by (auto simp: image_comp intro: bounded_snd bounded_subset)
-  obtain l2 r2 where r2: "strict_mono r2" and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) \<longlongrightarrow> l2) sequentially"
-    using bounded_imp_convergent_subsequence [OF s2]
-    unfolding o_def by fast
-  have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) \<longlongrightarrow> l1) sequentially"
-    using LIMSEQ_subseq_LIMSEQ [OF l1 r2] unfolding o_def .
-  have l: "((f \<circ> (r1 \<circ> r2)) \<longlongrightarrow> (l1, l2)) sequentially"
-    using tendsto_Pair [OF l1' l2] unfolding o_def by simp
-  have r: "strict_mono (r1 \<circ> r2)"
-    using r1 r2 unfolding strict_mono_def by simp
-  show "\<exists>l r. strict_mono r \<and> ((f \<circ> r) \<longlongrightarrow> l) sequentially"
-    using l r by fast
-qed
-
-subsubsection \<open>Completeness\<close>
-
-proposition (in metric_space) completeI:
-  assumes "\<And>f. \<forall>n. f n \<in> s \<Longrightarrow> Cauchy f \<Longrightarrow> \<exists>l\<in>s. f \<longlonglongrightarrow> l"
-  shows "complete s"
-  using assms unfolding complete_def by fast
-
-proposition (in metric_space) completeE:
-  assumes "complete s" and "\<forall>n. f n \<in> s" and "Cauchy f"
-  obtains l where "l \<in> s" and "f \<longlonglongrightarrow> l"
-  using assms unfolding complete_def by fast
-
-(* TODO: generalize to uniform spaces *)
-lemma compact_imp_complete:
-  fixes s :: "'a::metric_space set"
-  assumes "compact s"
-  shows "complete s"
-proof -
-  {
-    fix f
-    assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
-    from as(1) obtain l r where lr: "l\<in>s" "strict_mono r" "(f \<circ> r) \<longlonglongrightarrow> l"
-      using assms unfolding compact_def by blast
-
-    note lr' = seq_suble [OF lr(2)]
-    {
-      fix e :: real
-      assume "e > 0"
-      from as(2) obtain N where N:"\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (f m) (f n) < e/2"
-        unfolding cauchy_def
-        using \<open>e > 0\<close>
-        apply (erule_tac x="e/2" in allE, auto)
-        done
-      from lr(3)[unfolded lim_sequentially, THEN spec[where x="e/2"]]
-      obtain M where M:"\<forall>n\<ge>M. dist ((f \<circ> r) n) l < e/2"
-        using \<open>e > 0\<close> by auto
-      {
-        fix n :: nat
-        assume n: "n \<ge> max N M"
-        have "dist ((f \<circ> r) n) l < e/2"
-          using n M by auto
-        moreover have "r n \<ge> N"
-          using lr'[of n] n by auto
-        then have "dist (f n) ((f \<circ> r) n) < e / 2"
-          using N and n by auto
-        ultimately have "dist (f n) l < e"
-          using dist_triangle_half_r[of "f (r n)" "f n" e l]
-          by (auto simp: dist_commute)
-      }
-      then have "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast
-    }
-    then have "\<exists>l\<in>s. (f \<longlongrightarrow> l) sequentially" using \<open>l\<in>s\<close>
-      unfolding lim_sequentially by auto
-  }
-  then show ?thesis unfolding complete_def by auto
-qed
-
-proposition compact_eq_totally_bounded:
-  "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>x\<in>k. ball x e))"
-    (is "_ \<longleftrightarrow> ?rhs")
-proof
-  assume assms: "?rhs"
-  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)"
-    by (auto simp: choice_iff')
-
-  show "compact s"
-  proof cases
-    assume "s = {}"
-    then show "compact s" by (simp add: compact_def)
-  next
-    assume "s \<noteq> {}"
-    show ?thesis
-      unfolding compact_def
-    proof safe
-      fix f :: "nat \<Rightarrow> 'a"
-      assume f: "\<forall>n. f n \<in> s"
-
-      define e where "e n = 1 / (2 * Suc n)" for n
-      then have [simp]: "\<And>n. 0 < e n" by auto
-      define B where "B n U = (SOME b. infinite {n. f n \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U))" for n U
-      {
-        fix n U
-        assume "infinite {n. f n \<in> U}"
-        then have "\<exists>b\<in>k (e n). infinite {i\<in>{n. f n \<in> U}. f i \<in> ball b (e n)}"
-          using k f by (intro pigeonhole_infinite_rel) (auto simp: subset_eq)
-        then obtain a where
-          "a \<in> k (e n)"
-          "infinite {i \<in> {n. f n \<in> U}. f i \<in> ball a (e n)}" ..
-        then have "\<exists>b. infinite {i. f i \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U)"
-          by (intro exI[of _ "ball a (e n) \<inter> U"] exI[of _ a]) (auto simp: ac_simps)
-        from someI_ex[OF this]
-        have "infinite {i. f i \<in> B n U}" "\<exists>x. B n U \<subseteq> ball x (e n) \<inter> U"
-          unfolding B_def by auto
-      }
-      note B = this
-
-      define F where "F = rec_nat (B 0 UNIV) B"
-      {
-        fix n
-        have "infinite {i. f i \<in> F n}"
-          by (induct n) (auto simp: F_def B)
-      }
-      then have F: "\<And>n. \<exists>x. F (Suc n) \<subseteq> ball x (e n) \<inter> F n"
-        using B by (simp add: F_def)
-      then have F_dec: "\<And>m n. m \<le> n \<Longrightarrow> F n \<subseteq> F m"
-        using decseq_SucI[of F] by (auto simp: decseq_def)
-
-      obtain sel where sel: "\<And>k i. i < sel k i" "\<And>k i. f (sel k i) \<in> F k"
-      proof (atomize_elim, unfold all_conj_distrib[symmetric], intro choice allI)
-        fix k i
-        have "infinite ({n. f n \<in> F k} - {.. i})"
-          using \<open>infinite {n. f n \<in> F k}\<close> by auto
-        from infinite_imp_nonempty[OF this]
-        show "\<exists>x>i. f x \<in> F k"
-          by (simp add: set_eq_iff not_le conj_commute)
-      qed
-
-      define t where "t = rec_nat (sel 0 0) (\<lambda>n i. sel (Suc n) i)"
-      have "strict_mono t"
-        unfolding strict_mono_Suc_iff by (simp add: t_def sel)
-      moreover have "\<forall>i. (f \<circ> t) i \<in> s"
-        using f by auto
-      moreover
-      {
-        fix n
-        have "(f \<circ> t) n \<in> F n"
-          by (cases n) (simp_all add: t_def sel)
-      }
-      note t = this
-
-      have "Cauchy (f \<circ> t)"
-      proof (safe intro!: metric_CauchyI exI elim!: nat_approx_posE)
-        fix r :: real and N n m
-        assume "1 / Suc N < r" "Suc N \<le> n" "Suc N \<le> m"
-        then have "(f \<circ> t) n \<in> F (Suc N)" "(f \<circ> t) m \<in> F (Suc N)" "2 * e N < r"
-          using F_dec t by (auto simp: e_def field_simps of_nat_Suc)
-        with F[of N] obtain x where "dist x ((f \<circ> t) n) < e N" "dist x ((f \<circ> t) m) < e N"
-          by (auto simp: subset_eq)
-        with dist_triangle[of "(f \<circ> t) m" "(f \<circ> t) n" x] \<open>2 * e N < r\<close>
-        show "dist ((f \<circ> t) m) ((f \<circ> t) n) < r"
-          by (simp add: dist_commute)
-      qed
-
-      ultimately show "\<exists>l\<in>s. \<exists>r. strict_mono r \<and> (f \<circ> r) \<longlonglongrightarrow> l"
-        using assms unfolding complete_def by blast
-    qed
-  qed
-qed (metis compact_imp_complete compact_imp_seq_compact seq_compact_imp_totally_bounded)
-
-lemma cauchy_imp_bounded:
-  assumes "Cauchy s"
-  shows "bounded (range s)"
-proof -
-  from assms obtain N :: nat where "\<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < 1"
-    unfolding cauchy_def by force
-  then have N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
-  moreover
-  have "bounded (s ` {0..N})"
-    using finite_imp_bounded[of "s ` {1..N}"] by auto
-  then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
-    unfolding bounded_any_center [where a="s N"] by auto
-  ultimately show "?thesis"
-    unfolding bounded_any_center [where a="s N"]
-    apply (rule_tac x="max a 1" in exI, auto)
-    apply (erule_tac x=y in allE)
-    apply (erule_tac x=y in ballE, auto)
-    done
-qed
-
-instance heine_borel < complete_space
-proof
-  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
-  then have "bounded (range f)"
-    by (rule cauchy_imp_bounded)
-  then have "compact (closure (range f))"
-    unfolding compact_eq_bounded_closed by auto
-  then have "complete (closure (range f))"
-    by (rule compact_imp_complete)
-  moreover have "\<forall>n. f n \<in> closure (range f)"
-    using closure_subset [of "range f"] by auto
-  ultimately have "\<exists>l\<in>closure (range f). (f \<longlongrightarrow> l) sequentially"
-    using \<open>Cauchy f\<close> unfolding complete_def by auto
-  then show "convergent f"
-    unfolding convergent_def by auto
-qed
-
 instance euclidean_space \<subseteq> banach ..
 
-lemma complete_UNIV: "complete (UNIV :: ('a::complete_space) set)"
-proof (rule completeI)
-  fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
-  then have "convergent f" by (rule Cauchy_convergent)
-  then show "\<exists>l\<in>UNIV. f \<longlonglongrightarrow> l" unfolding convergent_def by simp
-qed
-
-lemma complete_imp_closed:
-  fixes S :: "'a::metric_space set"
-  assumes "complete S"
-  shows "closed S"
-proof (unfold closed_sequential_limits, clarify)
-  fix f x assume "\<forall>n. f n \<in> S" and "f \<longlonglongrightarrow> x"
-  from \<open>f \<longlonglongrightarrow> x\<close> have "Cauchy f"
-    by (rule LIMSEQ_imp_Cauchy)
-  with \<open>complete S\<close> and \<open>\<forall>n. f n \<in> S\<close> obtain l where "l \<in> S" and "f \<longlonglongrightarrow> l"
-    by (rule completeE)
-  from \<open>f \<longlonglongrightarrow> x\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "x = l"
-    by (rule LIMSEQ_unique)
-  with \<open>l \<in> S\<close> show "x \<in> S"
-    by simp
-qed
-
-lemma complete_Int_closed:
-  fixes S :: "'a::metric_space set"
-  assumes "complete S" and "closed t"
-  shows "complete (S \<inter> t)"
-proof (rule completeI)
-  fix f assume "\<forall>n. f n \<in> S \<inter> t" and "Cauchy f"
-  then have "\<forall>n. f n \<in> S" and "\<forall>n. f n \<in> t"
-    by simp_all
-  from \<open>complete S\<close> obtain l where "l \<in> S" and "f \<longlonglongrightarrow> l"
-    using \<open>\<forall>n. f n \<in> S\<close> and \<open>Cauchy f\<close> by (rule completeE)
-  from \<open>closed t\<close> and \<open>\<forall>n. f n \<in> t\<close> and \<open>f \<longlonglongrightarrow> l\<close> have "l \<in> t"
-    by (rule closed_sequentially)
-  with \<open>l \<in> S\<close> and \<open>f \<longlonglongrightarrow> l\<close> show "\<exists>l\<in>S \<inter> t. f \<longlonglongrightarrow> l"
-    by fast
-qed
-
-lemma complete_closed_subset:
-  fixes S :: "'a::metric_space set"
-  assumes "closed S" and "S \<subseteq> t" and "complete t"
-  shows "complete S"
-  using assms complete_Int_closed [of t S] by (simp add: Int_absorb1)
-
-lemma complete_eq_closed:
-  fixes S :: "('a::complete_space) set"
-  shows "complete S \<longleftrightarrow> closed S"
-proof
-  assume "closed S" then show "complete S"
-    using subset_UNIV complete_UNIV by (rule complete_closed_subset)
-next
-  assume "complete S" then show "closed S"
-    by (rule complete_imp_closed)
-qed
-
-lemma convergent_eq_Cauchy:
-  fixes S :: "nat \<Rightarrow> 'a::complete_space"
-  shows "(\<exists>l. (S \<longlongrightarrow> l) sequentially) \<longleftrightarrow> Cauchy S"
-  unfolding Cauchy_convergent_iff convergent_def ..
-
-lemma convergent_imp_bounded:
-  fixes S :: "nat \<Rightarrow> 'a::metric_space"
-  shows "(S \<longlongrightarrow> l) sequentially \<Longrightarrow> bounded (range S)"
-  by (intro cauchy_imp_bounded LIMSEQ_imp_Cauchy)
-
-lemma frontier_subset_compact:
-  fixes S :: "'a::heine_borel set"
-  shows "compact S \<Longrightarrow> frontier S \<subseteq> S"
-  using frontier_subset_closed compact_eq_bounded_closed
-  by blast
-
-subsection \<open>Continuity\<close>
-
-text\<open>Derive the epsilon-delta forms, which we often use as "definitions"\<close>
-
-proposition continuous_within_eps_delta:
-  "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)"
-  unfolding continuous_within and Lim_within  by fastforce
-
-corollary continuous_at_eps_delta:
-  "continuous (at x) f \<longleftrightarrow> (\<forall>e > 0. \<exists>d > 0. \<forall>x'. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
-  using continuous_within_eps_delta [of x UNIV f] by simp
-
-lemma continuous_at_right_real_increasing:
-  fixes f :: "real \<Rightarrow> real"
-  assumes nondecF: "\<And>x y. x \<le> y \<Longrightarrow> f x \<le> f y"
-  shows "continuous (at_right a) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f (a + d) - f a < e)"
-  apply (simp add: greaterThan_def dist_real_def continuous_within Lim_within_le)
-  apply (intro all_cong ex_cong, safe)
-  apply (erule_tac x="a + d" in allE, simp)
-  apply (simp add: nondecF field_simps)
-  apply (drule nondecF, simp)
-  done
-
-lemma continuous_at_left_real_increasing:
-  assumes nondecF: "\<And> x y. x \<le> y \<Longrightarrow> f x \<le> ((f y) :: real)"
-  shows "(continuous (at_left (a :: real)) f) = (\<forall>e > 0. \<exists>delta > 0. f a - f (a - delta) < e)"
-  apply (simp add: lessThan_def dist_real_def continuous_within Lim_within_le)
-  apply (intro all_cong ex_cong, safe)
-  apply (erule_tac x="a - d" in allE, simp)
-  apply (simp add: nondecF field_simps)
-  apply (cut_tac x="a - d" and y=x in nondecF, simp_all)
-  done
-
-text\<open>Versions in terms of open balls.\<close>
-
-lemma continuous_within_ball:
-  "continuous (at x within s) f \<longleftrightarrow>
-    (\<forall>e > 0. \<exists>d > 0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e)"
-  (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  {
-    fix e :: real
-    assume "e > 0"
-    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"
-      using \<open>?lhs\<close>[unfolded continuous_within Lim_within] by auto
-    {
-      fix y
-      assume "y \<in> f ` (ball x d \<inter> s)"
-      then have "y \<in> ball (f x) e"
-        using d(2)
-        apply (auto simp: dist_commute)
-        apply (erule_tac x=xa in ballE, auto)
-        using \<open>e > 0\<close>
-        apply auto
-        done
-    }
-    then have "\<exists>d>0. f ` (ball x d \<inter> s) \<subseteq> ball (f x) e"
-      using \<open>d > 0\<close>
-      unfolding subset_eq ball_def by (auto simp: dist_commute)
-  }
-  then show ?rhs by auto
-next
-  assume ?rhs
-  then show ?lhs
-    unfolding continuous_within Lim_within ball_def subset_eq
-    apply (auto simp: dist_commute)
-    apply (erule_tac x=e in allE, auto)
-    done
-qed
-
-lemma continuous_at_ball:
-  "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f ` (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs
-    unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
-    apply auto
-    apply (erule_tac x=e in allE, auto)
-    apply (rule_tac x=d in exI, auto)
-    apply (erule_tac x=xa in allE)
-    apply (auto simp: dist_commute)
-    done
-next
-  assume ?rhs
-  then show ?lhs
-    unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
-    apply auto
-    apply (erule_tac x=e in allE, auto)
-    apply (rule_tac x=d in exI, auto)
-    apply (erule_tac x="f xa" in allE)
-    apply (auto simp: dist_commute)
-    done
-qed
-
-text\<open>Define setwise continuity in terms of limits within the set.\<close>
-
-lemma continuous_on_iff:
-  "continuous_on s f \<longleftrightarrow>
-    (\<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)"
-  unfolding continuous_on_def Lim_within
-  by (metis dist_pos_lt dist_self)
-
-lemma continuous_within_E:
-  assumes "continuous (at x within s) f" "e>0"
-  obtains d where "d>0"  "\<And>x'. \<lbrakk>x'\<in> s; dist x' x \<le> d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
-  using assms apply (simp add: continuous_within_eps_delta)
-  apply (drule spec [of _ e], clarify)
-  apply (rule_tac d="d/2" in that, auto)
-  done
-
-lemma continuous_onI [intro?]:
-  assumes "\<And>x e. \<lbrakk>e > 0; x \<in> s\<rbrakk> \<Longrightarrow> \<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) \<le> e"
-  shows "continuous_on s f"
-apply (simp add: continuous_on_iff, clarify)
-apply (rule ex_forward [OF assms [OF half_gt_zero]], auto)
-done
-
-text\<open>Some simple consequential lemmas.\<close>
-
-lemma continuous_onE:
-    assumes "continuous_on s f" "x\<in>s" "e>0"
-    obtains d where "d>0"  "\<And>x'. \<lbrakk>x' \<in> s; dist x' x \<le> d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
-  using assms
-  apply (simp add: continuous_on_iff)
-  apply (elim ballE allE)
-  apply (auto intro: that [where d="d/2" for d])
-  done
-
-lemma uniformly_continuous_onE:
-  assumes "uniformly_continuous_on s f" "0 < e"
-  obtains d where "d>0" "\<And>x x'. \<lbrakk>x\<in>s; x'\<in>s; dist x' x < d\<rbrakk> \<Longrightarrow> dist (f x') (f x) < e"
-using assms
-by (auto simp: uniformly_continuous_on_def)
-
-lemma continuous_at_imp_continuous_within:
-  "continuous (at x) f \<Longrightarrow> continuous (at x within s) f"
-  unfolding continuous_within continuous_at using Lim_at_imp_Lim_at_within by auto
-
-lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f \<longlongrightarrow> l) net"
-  by simp
-
-lemmas continuous_on = continuous_on_def \<comment> \<open>legacy theorem name\<close>
-
-lemma continuous_within_subset:
-  "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s \<Longrightarrow> continuous (at x within t) f"
-  unfolding continuous_within by(metis tendsto_within_subset)
-
-lemma continuous_on_interior:
-  "continuous_on s f \<Longrightarrow> x \<in> interior s \<Longrightarrow> continuous (at x) f"
-  by (metis continuous_on_eq_continuous_at continuous_on_subset interiorE)
-
-lemma continuous_on_eq:
-  "\<lbrakk>continuous_on s f; \<And>x. x \<in> s \<Longrightarrow> f x = g x\<rbrakk> \<Longrightarrow> continuous_on s g"
-  unfolding continuous_on_def tendsto_def eventually_at_topological
-  by simp
-
-text \<open>Characterization of various kinds of continuity in terms of sequences.\<close>
-
-lemma continuous_within_sequentiallyI:
-  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
-  assumes "\<And>u::nat \<Rightarrow> 'a. u \<longlonglongrightarrow> a \<Longrightarrow> (\<forall>n. u n \<in> s) \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
-  shows "continuous (at a within s) f"
-  using assms unfolding continuous_within tendsto_def[where l = "f a"]
-  by (auto intro!: sequentially_imp_eventually_within)
-
-lemma continuous_within_tendsto_compose:
-  fixes f::"'a::t2_space \<Rightarrow> 'b::topological_space"
-  assumes "continuous (at a within s) f"
-          "eventually (\<lambda>n. x n \<in> s) F"
-          "(x \<longlongrightarrow> a) F "
-  shows "((\<lambda>n. f (x n)) \<longlongrightarrow> f a) F"
-proof -
-  have *: "filterlim x (inf (nhds a) (principal s)) F"
-    using assms(2) assms(3) unfolding at_within_def filterlim_inf by (auto simp: filterlim_principal eventually_mono)
-  show ?thesis
-    by (auto simp: assms(1) continuous_within[symmetric] tendsto_at_within_iff_tendsto_nhds[symmetric] intro!: filterlim_compose[OF _ *])
-qed
-
-lemma continuous_within_tendsto_compose':
-  fixes f::"'a::t2_space \<Rightarrow> 'b::topological_space"
-  assumes "continuous (at a within s) f"
-    "\<And>n. x n \<in> s"
-    "(x \<longlongrightarrow> a) F "
-  shows "((\<lambda>n. f (x n)) \<longlongrightarrow> f a) F"
-  by (auto intro!: continuous_within_tendsto_compose[OF assms(1)] simp add: assms)
-
-lemma continuous_within_sequentially:
-  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
-  shows "continuous (at a within s) f \<longleftrightarrow>
-    (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x \<longlongrightarrow> a) sequentially
-         \<longrightarrow> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
-  using continuous_within_tendsto_compose'[of a s f _ sequentially]
-    continuous_within_sequentiallyI[of a s f]
-  by (auto simp: o_def)
-
-lemma continuous_at_sequentiallyI:
-  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
-  assumes "\<And>u. u \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
-  shows "continuous (at a) f"
-  using continuous_within_sequentiallyI[of a UNIV f] assms by auto
-
-lemma continuous_at_sequentially:
-  fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
-  shows "continuous (at a) f \<longleftrightarrow>
-    (\<forall>x. (x \<longlongrightarrow> a) sequentially --> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
-  using continuous_within_sequentially[of a UNIV f] by simp
-
-lemma continuous_on_sequentiallyI:
-  fixes f :: "'a::{first_countable_topology, t2_space} \<Rightarrow> 'b::topological_space"
-  assumes "\<And>u a. (\<forall>n. u n \<in> s) \<Longrightarrow> a \<in> s \<Longrightarrow> u \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (u n)) \<longlonglongrightarrow> f a"
-  shows "continuous_on s f"
-  using assms unfolding continuous_on_eq_continuous_within
-  using continuous_within_sequentiallyI[of _ s f] by auto
-
-lemma continuous_on_sequentially:
-  fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
-  shows "continuous_on s f \<longleftrightarrow>
-    (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x \<longlongrightarrow> a) sequentially
-      --> ((f \<circ> x) \<longlongrightarrow> f a) sequentially)"
-    (is "?lhs = ?rhs")
-proof
-  assume ?rhs
-  then show ?lhs
-    using continuous_within_sequentially[of _ s f]
-    unfolding continuous_on_eq_continuous_within
-    by auto
-next
-  assume ?lhs
-  then show ?rhs
-    unfolding continuous_on_eq_continuous_within
-    using continuous_within_sequentially[of _ s f]
-    by auto
-qed
-
-lemma uniformly_continuous_on_sequentially:
-  "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
-    (\<lambda>n. dist (x n) (y n)) \<longlonglongrightarrow> 0 \<longrightarrow> (\<lambda>n. dist (f(x n)) (f(y n))) \<longlonglongrightarrow> 0)" (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  {
-    fix x y
-    assume x: "\<forall>n. x n \<in> s"
-      and y: "\<forall>n. y n \<in> s"
-      and xy: "((\<lambda>n. dist (x n) (y n)) \<longlongrightarrow> 0) sequentially"
-    {
-      fix e :: real
-      assume "e > 0"
-      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"
-        using \<open>?lhs\<close>[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto
-      obtain N where N: "\<forall>n\<ge>N. dist (x n) (y n) < d"
-        using xy[unfolded lim_sequentially dist_norm] and \<open>d>0\<close> by auto
-      {
-        fix n
-        assume "n\<ge>N"
-        then have "dist (f (x n)) (f (y n)) < e"
-          using N[THEN spec[where x=n]]
-          using d[THEN bspec[where x="x n"], THEN bspec[where x="y n"]]
-          using x and y
-          by (simp add: dist_commute)
-      }
-      then have "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"
-        by auto
-    }
-    then have "((\<lambda>n. dist (f(x n)) (f(y n))) \<longlongrightarrow> 0) sequentially"
-      unfolding lim_sequentially and dist_real_def by auto
-  }
-  then show ?rhs by auto
-next
-  assume ?rhs
-  {
-    assume "\<not> ?lhs"
-    then obtain e where "e > 0" "\<forall>d>0. \<exists>x\<in>s. \<exists>x'\<in>s. dist x' x < d \<and> \<not> dist (f x') (f x) < e"
-      unfolding uniformly_continuous_on_def by auto
-    then obtain fa where fa:
-      "\<forall>x. 0 < x \<longrightarrow> fst (fa x) \<in> s \<and> snd (fa x) \<in> s \<and> dist (fst (fa x)) (snd (fa x)) < x \<and> \<not> dist (f (fst (fa x))) (f (snd (fa x))) < e"
-      using choice[of "\<lambda>d x. d>0 \<longrightarrow> fst x \<in> s \<and> snd x \<in> s \<and> dist (snd x) (fst x) < d \<and> \<not> dist (f (snd x)) (f (fst x)) < e"]
-      unfolding Bex_def
-      by (auto simp: dist_commute)
-    define x where "x n = fst (fa (inverse (real n + 1)))" for n
-    define y where "y n = snd (fa (inverse (real n + 1)))" for n
-    have xyn: "\<forall>n. x n \<in> s \<and> y n \<in> s"
-      and xy0: "\<forall>n. dist (x n) (y n) < inverse (real n + 1)"
-      and fxy:"\<forall>n. \<not> dist (f (x n)) (f (y n)) < e"
-      unfolding x_def and y_def using fa
-      by auto
-    {
-      fix e :: real
-      assume "e > 0"
-      then obtain N :: nat where "N \<noteq> 0" and N: "0 < inverse (real N) \<and> inverse (real N) < e"
-        unfolding real_arch_inverse[of e] by auto
-      {
-        fix n :: nat
-        assume "n \<ge> N"
-        then have "inverse (real n + 1) < inverse (real N)"
-          using of_nat_0_le_iff and \<open>N\<noteq>0\<close> by auto
-        also have "\<dots> < e" using N by auto
-        finally have "inverse (real n + 1) < e" by auto
-        then have "dist (x n) (y n) < e"
-          using xy0[THEN spec[where x=n]] by auto
-      }
-      then have "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto
-    }
-    then have "\<forall>e>0. \<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"
-      using \<open>?rhs\<close>[THEN spec[where x=x], THEN spec[where x=y]] and xyn
-      unfolding lim_sequentially dist_real_def by auto
-    then have False using fxy and \<open>e>0\<close> by auto
-  }
-  then show ?lhs
-    unfolding uniformly_continuous_on_def by blast
-qed
-
-lemma continuous_closed_imp_Cauchy_continuous:
-  fixes S :: "('a::complete_space) set"
-  shows "\<lbrakk>continuous_on S f; closed S; Cauchy \<sigma>; \<And>n. (\<sigma> n) \<in> S\<rbrakk> \<Longrightarrow> Cauchy(f \<circ> \<sigma>)"
-  apply (simp add: complete_eq_closed [symmetric] continuous_on_sequentially)
-  by (meson LIMSEQ_imp_Cauchy complete_def)
-
-text\<open>The usual transformation theorems.\<close>
-
-lemma continuous_transform_within:
-  fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
-  assumes "continuous (at x within s) f"
-    and "0 < d"
-    and "x \<in> s"
-    and "\<And>x'. \<lbrakk>x' \<in> s; dist x' x < d\<rbrakk> \<Longrightarrow> f x' = g x'"
-  shows "continuous (at x within s) g"
-  using assms
-  unfolding continuous_within
-  by (force intro: Lim_transform_within)
-
 
 subsubsection%unimportant \<open>Structural rules for pointwise continuity\<close>
 
@@ -5524,6 +778,7 @@
   shows "continuous F (\<lambda>x. inner (f x) (g x))"
   using assms unfolding continuous_def by (rule tendsto_inner)
 
+
 subsubsection%unimportant \<open>Structural rules for setwise continuity\<close>
 
 lemma continuous_on_infnorm[continuous_intros]:
@@ -5538,316 +793,6 @@
   using bounded_bilinear_inner assms
   by (rule bounded_bilinear.continuous_on)
 
-subsubsection%unimportant \<open>Structural rules for uniform continuity\<close>
-
-lemma uniformly_continuous_on_dist[continuous_intros]:
-  fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"
-  assumes "uniformly_continuous_on s f"
-    and "uniformly_continuous_on s g"
-  shows "uniformly_continuous_on s (\<lambda>x. dist (f x) (g x))"
-proof -
-  {
-    fix a b c d :: 'b
-    have "\<bar>dist a b - dist c d\<bar> \<le> dist a c + dist b d"
-      using dist_triangle2 [of a b c] dist_triangle2 [of b c d]
-      using dist_triangle3 [of c d a] dist_triangle [of a d b]
-      by arith
-  } note le = this
-  {
-    fix x y
-    assume f: "(\<lambda>n. dist (f (x n)) (f (y n))) \<longlonglongrightarrow> 0"
-    assume g: "(\<lambda>n. dist (g (x n)) (g (y n))) \<longlonglongrightarrow> 0"
-    have "(\<lambda>n. \<bar>dist (f (x n)) (g (x n)) - dist (f (y n)) (g (y n))\<bar>) \<longlonglongrightarrow> 0"
-      by (rule Lim_transform_bound [OF _ tendsto_add_zero [OF f g]],
-        simp add: le)
-  }
-  then show ?thesis
-    using assms unfolding uniformly_continuous_on_sequentially
-    unfolding dist_real_def by simp
-qed
-
-lemma uniformly_continuous_on_norm[continuous_intros]:
-  fixes f :: "'a :: metric_space \<Rightarrow> 'b :: real_normed_vector"
-  assumes "uniformly_continuous_on s f"
-  shows "uniformly_continuous_on s (\<lambda>x. norm (f x))"
-  unfolding norm_conv_dist using assms
-  by (intro uniformly_continuous_on_dist uniformly_continuous_on_const)
-
-lemma (in bounded_linear) uniformly_continuous_on[continuous_intros]:
-  fixes g :: "_::metric_space \<Rightarrow> _"
-  assumes "uniformly_continuous_on s g"
-  shows "uniformly_continuous_on s (\<lambda>x. f (g x))"
-  using assms unfolding uniformly_continuous_on_sequentially
-  unfolding dist_norm tendsto_norm_zero_iff diff[symmetric]
-  by (auto intro: tendsto_zero)
-
-lemma uniformly_continuous_on_cmul[continuous_intros]:
-  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
-  assumes "uniformly_continuous_on s f"
-  shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"
-  using bounded_linear_scaleR_right assms
-  by (rule bounded_linear.uniformly_continuous_on)
-
-lemma dist_minus:
-  fixes x y :: "'a::real_normed_vector"
-  shows "dist (- x) (- y) = dist x y"
-  unfolding dist_norm minus_diff_minus norm_minus_cancel ..
-
-lemma uniformly_continuous_on_minus[continuous_intros]:
-  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
-  shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. - f x)"
-  unfolding uniformly_continuous_on_def dist_minus .
-
-lemma uniformly_continuous_on_add[continuous_intros]:
-  fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
-  assumes "uniformly_continuous_on s f"
-    and "uniformly_continuous_on s g"
-  shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"
-  using assms
-  unfolding uniformly_continuous_on_sequentially
-  unfolding dist_norm tendsto_norm_zero_iff add_diff_add
-  by (auto intro: tendsto_add_zero)
-
-lemma uniformly_continuous_on_diff[continuous_intros]:
-  fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
-  assumes "uniformly_continuous_on s f"
-    and "uniformly_continuous_on s g"
-  shows "uniformly_continuous_on s (\<lambda>x. f x - g x)"
-  using assms uniformly_continuous_on_add [of s f "- g"]
-    by (simp add: fun_Compl_def uniformly_continuous_on_minus)
-
-text \<open>Continuity in terms of open preimages.\<close>
-
-lemma continuous_at_open:
-  "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)))"
-  unfolding continuous_within_topological [of x UNIV f]
-  unfolding imp_conjL
-  by (intro all_cong imp_cong ex_cong conj_cong refl) auto
-
-lemma continuous_imp_tendsto:
-  assumes "continuous (at x0) f"
-    and "x \<longlonglongrightarrow> x0"
-  shows "(f \<circ> x) \<longlonglongrightarrow> (f x0)"
-proof (rule topological_tendstoI)
-  fix S
-  assume "open S" "f x0 \<in> S"
-  then obtain T where T_def: "open T" "x0 \<in> T" "\<forall>x\<in>T. f x \<in> S"
-     using assms continuous_at_open by metis
-  then have "eventually (\<lambda>n. x n \<in> T) sequentially"
-    using assms T_def by (auto simp: tendsto_def)
-  then show "eventually (\<lambda>n. (f \<circ> x) n \<in> S) sequentially"
-    using T_def by (auto elim!: eventually_mono)
-qed
-
-lemma continuous_on_open:
-  "continuous_on S f \<longleftrightarrow>
-    (\<forall>T. openin (subtopology euclidean (f ` S)) T \<longrightarrow>
-      openin (subtopology euclidean S) (S \<inter> f -` T))"
-  unfolding continuous_on_open_invariant openin_open Int_def vimage_def Int_commute
-  by (simp add: imp_ex imageI conj_commute eq_commute cong: conj_cong)
-
-lemma continuous_on_open_gen:
-  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
-  assumes "f ` S \<subseteq> T"
-    shows "continuous_on S f \<longleftrightarrow>
-             (\<forall>U. openin (subtopology euclidean T) U
-                  \<longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` U))"
-     (is "?lhs = ?rhs")
-proof
-  assume ?lhs
-  then show ?rhs
-    apply (clarsimp simp: openin_euclidean_subtopology_iff continuous_on_iff)
-    by (metis assms image_subset_iff)
-next
-  have ope: "openin (subtopology euclidean T) (ball y e \<inter> T)" for y e
-    by (simp add: Int_commute openin_open_Int)
-  assume R [rule_format]: ?rhs
-  show ?lhs
-  proof (clarsimp simp add: continuous_on_iff)
-    fix x and e::real
-    assume "x \<in> S" and "0 < e"
-    then have x: "x \<in> S \<inter> (f -` ball (f x) e \<inter> f -` T)"
-      using assms by auto
-    show "\<exists>d>0. \<forall>x'\<in>S. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
-      using R [of "ball (f x) e \<inter> T"] x
-      by (fastforce simp add: ope openin_euclidean_subtopology_iff [of S] dist_commute)
-  qed
-qed
-
-lemma continuous_openin_preimage:
-  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
-  shows
-   "\<lbrakk>continuous_on S f; f ` S \<subseteq> T; openin (subtopology euclidean T) U\<rbrakk>
-        \<Longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` U)"
-by (simp add: continuous_on_open_gen)
-
-text \<open>Similarly in terms of closed sets.\<close>
-
-lemma continuous_on_closed:
-  "continuous_on S f \<longleftrightarrow>
-    (\<forall>T. closedin (subtopology euclidean (f ` S)) T \<longrightarrow>
-      closedin (subtopology euclidean S) (S \<inter> f -` T))"
-  unfolding continuous_on_closed_invariant closedin_closed Int_def vimage_def Int_commute
-  by (simp add: imp_ex imageI conj_commute eq_commute cong: conj_cong)
-
-lemma continuous_on_closed_gen:
-  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
-  assumes "f ` S \<subseteq> T"
-    shows "continuous_on S f \<longleftrightarrow>
-             (\<forall>U. closedin (subtopology euclidean T) U
-                  \<longrightarrow> closedin (subtopology euclidean S) (S \<inter> f -` U))"
-     (is "?lhs = ?rhs")
-proof -
-  have *: "U \<subseteq> T \<Longrightarrow> S \<inter> f -` (T - U) = S - (S \<inter> f -` U)" for U
-    using assms by blast
-  show ?thesis
-  proof
-    assume L: ?lhs
-    show ?rhs
-    proof clarify
-      fix U
-      assume "closedin (subtopology euclidean T) U"
-      then show "closedin (subtopology euclidean S) (S \<inter> f -` U)"
-        using L unfolding continuous_on_open_gen [OF assms]
-        by (metis * closedin_def inf_le1 topspace_euclidean_subtopology)
-    qed
-  next
-    assume R [rule_format]: ?rhs
-    show ?lhs
-      unfolding continuous_on_open_gen [OF assms]
-      by (metis * R inf_le1 openin_closedin_eq topspace_euclidean_subtopology)
-  qed
-qed
-
-lemma continuous_closedin_preimage_gen:
-  fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
-  assumes "continuous_on S f" "f ` S \<subseteq> T" "closedin (subtopology euclidean T) U"
-    shows "closedin (subtopology euclidean S) (S \<inter> f -` U)"
-using assms continuous_on_closed_gen by blast
-
-lemma continuous_on_imp_closedin:
-  assumes "continuous_on S f" "closedin (subtopology euclidean (f ` S)) T"
-    shows "closedin (subtopology euclidean S) (S \<inter> f -` T)"
-using assms continuous_on_closed by blast
-
-subsection%unimportant \<open>Half-global and completely global cases\<close>
-
-lemma continuous_openin_preimage_gen:
-  assumes "continuous_on S f"  "open T"
-  shows "openin (subtopology euclidean S) (S \<inter> f -` T)"
-proof -
-  have *: "(S \<inter> f -` T) = (S \<inter> f -` (T \<inter> f ` S))"
-    by auto
-  have "openin (subtopology euclidean (f ` S)) (T \<inter> f ` S)"
-    using openin_open_Int[of T "f ` S", OF assms(2)] unfolding openin_open by auto
-  then show ?thesis
-    using assms(1)[unfolded continuous_on_open, THEN spec[where x="T \<inter> f ` S"]]
-    using * by auto
-qed
-
-lemma continuous_closedin_preimage:
-  assumes "continuous_on S f" and "closed T"
-  shows "closedin (subtopology euclidean S) (S \<inter> f -` T)"
-proof -
-  have *: "(S \<inter> f -` T) = (S \<inter> f -` (T \<inter> f ` S))"
-    by auto
-  have "closedin (subtopology euclidean (f ` S)) (T \<inter> f ` S)"
-    using closedin_closed_Int[of T "f ` S", OF assms(2)]
-    by (simp add: Int_commute)
-  then show ?thesis
-    using assms(1)[unfolded continuous_on_closed, THEN spec[where x="T \<inter> f ` S"]]
-    using * by auto
-qed
-
-lemma continuous_openin_preimage_eq:
-   "continuous_on S f \<longleftrightarrow>
-    (\<forall>T. open T \<longrightarrow> openin (subtopology euclidean S) (S \<inter> f -` T))"
-apply safe
-apply (simp add: continuous_openin_preimage_gen)
-apply (fastforce simp add: continuous_on_open openin_open)
-done
-
-lemma continuous_closedin_preimage_eq:
-   "continuous_on S f \<longleftrightarrow>
-    (\<forall>T. closed T \<longrightarrow> closedin (subtopology euclidean S) (S \<inter> f -` T))"
-apply safe
-apply (simp add: continuous_closedin_preimage)
-apply (fastforce simp add: continuous_on_closed closedin_closed)
-done
-
-lemma continuous_open_preimage:
-  assumes contf: "continuous_on S f" and "open S" "open T"
-  shows "open (S \<inter> f -` T)"
-proof-
-  obtain U where "open U" "(S \<inter> f -` T) = S \<inter> U"
-    using continuous_openin_preimage_gen[OF contf \<open>open T\<close>]
-    unfolding openin_open by auto
-  then show ?thesis
-    using open_Int[of S U, OF \<open>open S\<close>] by auto
-qed
-
-lemma continuous_closed_preimage:
-  assumes contf: "continuous_on S f" and "closed S" "closed T"
-  shows "closed (S \<inter> f -` T)"
-proof-
-  obtain U where "closed U" "(S \<inter> f -` T) = S \<inter> U"
-    using continuous_closedin_preimage[OF contf \<open>closed T\<close>]
-    unfolding closedin_closed by auto
-  then show ?thesis using closed_Int[of S U, OF \<open>closed S\<close>] by auto
-qed
-
-lemma continuous_open_vimage: "open S \<Longrightarrow> (\<And>x. continuous (at x) f) \<Longrightarrow> open (f -` S)"
-  by (metis continuous_on_eq_continuous_within open_vimage) 
- 
-lemma continuous_closed_vimage: "closed S \<Longrightarrow> (\<And>x. continuous (at x) f) \<Longrightarrow> closed (f -` S)"
-  by (simp add: closed_vimage continuous_on_eq_continuous_within)
-
-lemma interior_image_subset:
-  assumes "inj f" "\<And>x. continuous (at x) f"
-  shows "interior (f ` S) \<subseteq> f ` (interior S)"
-proof
-  fix x assume "x \<in> interior (f ` S)"
-  then obtain T where as: "open T" "x \<in> T" "T \<subseteq> f ` S" ..
-  then have "x \<in> f ` S" by auto
-  then obtain y where y: "y \<in> S" "x = f y" by auto
-  have "open (f -` T)"
-    using assms \<open>open T\<close> by (simp add: continuous_at_imp_continuous_on open_vimage)
-  moreover have "y \<in> vimage f T"
-    using \<open>x = f y\<close> \<open>x \<in> T\<close> by simp
-  moreover have "vimage f T \<subseteq> S"
-    using \<open>T \<subseteq> image f S\<close> \<open>inj f\<close> unfolding inj_on_def subset_eq by auto
-  ultimately have "y \<in> interior S" ..
-  with \<open>x = f y\<close> show "x \<in> f ` interior S" ..
-qed
-
-subsection%unimportant \<open>Topological properties of linear functions\<close>
-
-lemma linear_lim_0:
-  assumes "bounded_linear f"
-  shows "(f \<longlongrightarrow> 0) (at (0))"
-proof -
-  interpret f: bounded_linear f by fact
-  have "(f \<longlongrightarrow> f 0) (at 0)"
-    using tendsto_ident_at by (rule f.tendsto)
-  then show ?thesis unfolding f.zero .
-qed
-
-lemma linear_continuous_at:
-  assumes "bounded_linear f"
-  shows "continuous (at a) f"
-  unfolding continuous_at using assms
-  apply (rule bounded_linear.tendsto)
-  apply (rule tendsto_ident_at)
-  done
-
-lemma linear_continuous_within:
-  "bounded_linear f \<Longrightarrow> continuous (at x within s) f"
-  using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto
-
-lemma linear_continuous_on:
-  "bounded_linear f \<Longrightarrow> continuous_on s f"
-  using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto
-
 subsection%unimportant \<open>Intervals\<close>
 
 text \<open>Openness of halfspaces.\<close>
--- a/src/HOL/Analysis/Weierstrass_Theorems.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Weierstrass_Theorems.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -1,4 +1,4 @@
-section \<open>The Bernstein-Weierstrass and Stone-Weierstrass Theorems\<close>
+section \<open>Bernstein-Weierstrass and Stone-Weierstrass\<close>
 
 text\<open>By L C Paulson (2015)\<close>
 
@@ -767,7 +767,7 @@
   assumes "compact S"  "\<And>c. P(\<lambda>x. c::real)"
           "\<And>f. P f \<Longrightarrow> continuous_on S f"
           "\<And>f g. P(f) \<and> P(g) \<Longrightarrow> P(\<lambda>x. f x + g x)"  "\<And>f g. P(f) \<and> P(g) \<Longrightarrow> P(\<lambda>x. f x * g x)"
-          "\<And>x y. x \<in> S \<and> y \<in> S \<and> ~(x = y) \<Longrightarrow> \<exists>f. P(f) \<and> ~(f x = f y)"
+          "\<And>x y. x \<in> S \<and> y \<in> S \<and> x \<noteq> y \<Longrightarrow> \<exists>f. P(f) \<and> f x \<noteq> f y"
           "continuous_on S f"
        "0 < e"
     shows "\<exists>g. P(g) \<and> (\<forall>x \<in> S. \<bar>f x - g x\<bar> < e)"
--- a/src/HOL/Analysis/Winding_Numbers.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/Winding_Numbers.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -22,7 +22,7 @@
 
 lemma wn_triangle1:
   assumes "0 \<in> interior(convex hull {a,b,c})"
-    shows "~ (Im(a/b) \<le> 0 \<and> 0 \<le> Im(b/c))"
+    shows "\<not> (Im(a/b) \<le> 0 \<and> 0 \<le> Im(b/c))"
 proof -
   { assume 0: "Im(a/b) \<le> 0" "0 \<le> Im(b/c)"
     have "0 \<notin> interior (convex hull {a,b,c})"
@@ -224,7 +224,7 @@
     by (simp_all add: assms path_image_join)
   with \<open>0 < e\<close> dac have "c \<notin> affine hull {a - of_real e, a + of_real e}"
     by (simp add: segment_as_ball not_le)
-  with \<open>0 < e\<close> have *: "~collinear{a - e, c,a + e}"
+  with \<open>0 < e\<close> have *: "\<not> collinear {a - e, c,a + e}"
     using collinear_3_affine_hull [of "a-e" "a+e"] by (auto simp: insert_commute)
   have 13: "1/3 + 1/3 + 1/3 = (1::real)" by simp
   have "(1/3) *\<^sub>R (a - of_real e) + (1/3) *\<^sub>R c + (1/3) *\<^sub>R (a + of_real e) \<in> interior(convex hull {a - e, c, a + e})"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Analysis/document/root.bib	Fri Dec 28 19:06:33 2018 +0100
@@ -0,0 +1,11 @@
+@article{dugundji,
+  author	= {J. Dugundji},
+  title		= {An extension of {Tietze's} theorem},
+  journal	= {Pacific J. Math.},
+  pages		= {353-367},
+  volume  = 1,
+  number  = 3,
+  year    = 1951,
+  url     = {https://projecteuclid.org/euclid.pjm/1103052106}}
+
+@misc{dummy}
--- a/src/HOL/Analysis/document/root.tex	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Analysis/document/root.tex	Fri Dec 28 19:06:33 2018 +0100
@@ -10,7 +10,7 @@
 \usepackage{pdfsetup}
 
 \urlstyle{rm}
-\isabellestyle{it}
+\isabellestyle{literalunderscore}
 \pagestyle{myheadings}
 
 \begin{document}
@@ -26,9 +26,14 @@
 
 \newpage
 
-\renewcommand{\setisabellecontext}[1]{\markright{THEORY~``#1''}}
+\renewcommand{\setisabellecontext}[1]{\markright{#1.thy}}
 
 \parindent 0pt\parskip 0.5ex
 \input{session}
 
+\pagestyle{headings}
+\bibliographystyle{abbrv}
+\bibliography{root}
+\nocite{dummy}
+
 \end{document}
--- a/src/HOL/Groups_Big.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Groups_Big.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -439,6 +439,25 @@
     F (\<lambda>x. F (g x) {y. y \<in> B \<and> R x y}) A = F (\<lambda>y. F (\<lambda>x. g x y) {x. x \<in> A \<and> R x y}) B"
   by (simp add: inter_filter) (rule swap)
 
+lemma image_gen:
+  assumes fin: "finite S"
+  shows "F h S = F (\<lambda>y. F h {x. x \<in> S \<and> g x = y}) (g ` S)"
+proof -
+  have "{y. y\<in> g`S \<and> g x = y} = {g x}" if "x \<in> S" for x
+    using that by auto
+  then have "F h S = F (\<lambda>x. F (\<lambda>y. h x) {y. y\<in> g`S \<and> g x = y}) S"
+    by simp
+  also have "\<dots> = F (\<lambda>y. F h {x. x \<in> S \<and> g x = y}) (g ` S)"
+    by (rule swap_restrict [OF fin finite_imageI [OF fin]])
+  finally show ?thesis .
+qed
+
+lemma group:
+  assumes fS: "finite S" and fT: "finite T" and fST: "g ` S \<subseteq> T"
+  shows "F (\<lambda>y. F h {x. x \<in> S \<and> g x = y}) T = F h S"
+  unfolding image_gen[OF fS, of h g]
+  by (auto intro: neutral mono_neutral_right[OF fT fST])
+
 lemma Plus:
   fixes A :: "'b set" and B :: "'c set"
   assumes fin: "finite A" "finite B"
@@ -534,21 +553,6 @@
 in [(@{const_syntax sum}, K sum_tr')] end
 \<close>
 
-(* TODO generalization candidates *)
-
-lemma (in comm_monoid_add) sum_image_gen:
-  assumes fin: "finite S"
-  shows "sum g S = sum (\<lambda>y. sum g {x. x \<in> S \<and> f x = y}) (f ` S)"
-proof -
-  have "{y. y\<in> f`S \<and> f x = y} = {f x}" if "x \<in> S" for x
-    using that by auto
-  then have "sum g S = sum (\<lambda>x. sum (\<lambda>y. g x) {y. y\<in> f`S \<and> f x = y}) S"
-    by simp
-  also have "\<dots> = sum (\<lambda>y. sum g {x. x \<in> S \<and> f x = y}) (f ` S)"
-    by (rule sum.swap_restrict [OF fin finite_imageI [OF fin]])
-  finally show ?thesis .
-qed
-
 
 subsubsection \<open>Properties in more restricted classes of structures\<close>
 
@@ -756,7 +760,7 @@
   also have "\<dots> \<le> sum (\<lambda>y. sum g {x. x\<in>t \<and> i x = y}) (i ` t)"
     using assms(2-4) by (auto intro!: sum_mono2 sum_nonneg)
   also have "\<dots> \<le> sum g t"
-    using assms by (auto simp: sum_image_gen[symmetric])
+    using assms by (auto simp: sum.image_gen[symmetric])
   finally show ?thesis .
 qed
 
--- a/src/HOL/Library/Infinite_Set.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Library/Infinite_Set.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -266,6 +266,12 @@
   finally show ?case by simp
 qed
 
+lemma infinite_enumerate:
+  assumes fS: "infinite S"
+  shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (\<forall>n. r n \<in> S)"
+  unfolding strict_mono_def
+  using enumerate_in_set[OF fS] enumerate_mono[of _ _ S] fS by auto
+
 lemma enumerate_Suc'':
   fixes S :: "'a::wellorder set"
   assumes "infinite S"
--- a/src/HOL/ROOT	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/ROOT	Fri Dec 28 19:06:33 2018 +0100
@@ -69,6 +69,7 @@
     Analysis
   document_files
     "root.tex"
+    "root.bib"
 
 session "HOL-Analysis-ex" in "Analysis/ex" = "HOL-Analysis" +
   theories
--- a/src/HOL/Real_Vector_Spaces.thy	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/HOL/Real_Vector_Spaces.thy	Fri Dec 28 19:06:33 2018 +0100
@@ -648,33 +648,34 @@
   then show "norm (1::'a) = 1" by simp
 qed
 
-lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
+context real_normed_vector begin
+
+lemma norm_zero [simp]: "norm (0::'a) = 0"
   by simp
 
 lemma zero_less_norm_iff [simp]: "norm x > 0 \<longleftrightarrow> x \<noteq> 0"
-  for x :: "'a::real_normed_vector"
   by (simp add: order_less_le)
 
 lemma norm_not_less_zero [simp]: "\<not> norm x < 0"
-  for x :: "'a::real_normed_vector"
   by (simp add: linorder_not_less)
 
 lemma norm_le_zero_iff [simp]: "norm x \<le> 0 \<longleftrightarrow> x = 0"
-  for x :: "'a::real_normed_vector"
   by (simp add: order_le_less)
 
 lemma norm_minus_cancel [simp]: "norm (- x) = norm x"
-  for x :: "'a::real_normed_vector"
 proof -
-  have "norm (- x) = norm (scaleR (- 1) x)"
-    by (simp only: scaleR_minus_left scaleR_one)
+  have "- 1 *\<^sub>R x = - (1 *\<^sub>R x)"
+    unfolding add_eq_0_iff2[symmetric] scaleR_add_left[symmetric]
+    using norm_eq_zero
+    by fastforce
+  then have "norm (- x) = norm (scaleR (- 1) x)"
+    by (simp only: scaleR_one)
   also have "\<dots> = \<bar>- 1\<bar> * norm x"
     by (rule norm_scaleR)
   finally show ?thesis by simp
 qed
 
 lemma norm_minus_commute: "norm (a - b) = norm (b - a)"
-  for a b :: "'a::real_normed_vector"
 proof -
   have "norm (- (b - a)) = norm (b - a)"
     by (rule norm_minus_cancel)
@@ -682,22 +683,15 @@
 qed
 
 lemma dist_add_cancel [simp]: "dist (a + b) (a + c) = dist b c"
-  for a :: "'a::real_normed_vector"
   by (simp add: dist_norm)
 
 lemma dist_add_cancel2 [simp]: "dist (b + a) (c + a) = dist b c"
-  for a :: "'a::real_normed_vector"
   by (simp add: dist_norm)
 
-lemma dist_scaleR [simp]: "dist (x *\<^sub>R a) (y *\<^sub>R a) = \<bar>x - y\<bar> * norm a"
-  for a :: "'a::real_normed_vector"
-  by (metis dist_norm norm_scaleR scaleR_left.diff)
-
-lemma norm_uminus_minus: "norm (- x - y :: 'a :: real_normed_vector) = norm (x + y)"
+lemma norm_uminus_minus: "norm (- x - y) = norm (x + y)"
   by (subst (2) norm_minus_cancel[symmetric], subst minus_add_distrib) simp
 
 lemma norm_triangle_ineq2: "norm a - norm b \<le> norm (a - b)"
-  for a b :: "'a::real_normed_vector"
 proof -
   have "norm (a - b + b) \<le> norm (a - b) + norm b"
     by (rule norm_triangle_ineq)
@@ -705,7 +699,6 @@
 qed
 
 lemma norm_triangle_ineq3: "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
-  for a b :: "'a::real_normed_vector"
 proof -
   have "norm a - norm b \<le> norm (a - b)"
     by (simp add: norm_triangle_ineq2)
@@ -716,32 +709,35 @@
 qed
 
 lemma norm_triangle_ineq4: "norm (a - b) \<le> norm a + norm b"
-  for a b :: "'a::real_normed_vector"
 proof -
   have "norm (a + - b) \<le> norm a + norm (- b)"
     by (rule norm_triangle_ineq)
   then show ?thesis by simp
 qed
 
-lemma norm_triangle_le_diff:
-  fixes x y :: "'a::real_normed_vector"
-  shows "norm x + norm y \<le> e \<Longrightarrow> norm (x - y) \<le> e"
+lemma norm_triangle_le_diff: "norm x + norm y \<le> e \<Longrightarrow> norm (x - y) \<le> e"
     by (meson norm_triangle_ineq4 order_trans)
 
 lemma norm_diff_ineq: "norm a - norm b \<le> norm (a + b)"
-  for a b :: "'a::real_normed_vector"
 proof -
   have "norm a - norm (- b) \<le> norm (a - - b)"
     by (rule norm_triangle_ineq2)
   then show ?thesis by simp
 qed
 
+lemma norm_triangle_sub: "norm x \<le> norm y + norm (x - y)"
+  using norm_triangle_ineq[of "y" "x - y"] by (simp add: field_simps)
+
+lemma norm_triangle_le: "norm x + norm y \<le> e \<Longrightarrow> norm (x + y) \<le> e"
+  by (rule norm_triangle_ineq [THEN order_trans])
+
+lemma norm_triangle_lt: "norm x + norm y < e \<Longrightarrow> norm (x + y) < e"
+  by (rule norm_triangle_ineq [THEN le_less_trans])
+
 lemma norm_add_leD: "norm (a + b) \<le> c \<Longrightarrow> norm b \<le> norm a + c"
-  for a b :: "'a::real_normed_vector"
-  by (metis add.commute diff_le_eq norm_diff_ineq order.trans)
+  by (metis ab_semigroup_add_class.add.commute add_commute diff_le_eq norm_diff_ineq order_trans)
 
 lemma norm_diff_triangle_ineq: "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
-  for a b c d :: "'a::real_normed_vector"
 proof -
   have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
     by (simp add: algebra_simps)
@@ -750,42 +746,54 @@
   finally show ?thesis .
 qed
 
-lemma norm_diff_triangle_le:
-  fixes x y z :: "'a::real_normed_vector"
-  assumes "norm (x - y) \<le> e1"  "norm (y - z) \<le> e2"
-  shows "norm (x - z) \<le> e1 + e2"
-  using norm_diff_triangle_ineq [of x y y z] assms by simp
+lemma norm_diff_triangle_le: "norm (x - z) \<le> e1 + e2"
+  if "norm (x - y) \<le> e1"  "norm (y - z) \<le> e2"
+proof -
+  have "norm (x - (y + z - y)) \<le> norm (x - y) + norm (y - z)"
+    using norm_diff_triangle_ineq that diff_diff_eq2 by presburger
+  with that show ?thesis by simp
+qed
 
-lemma norm_diff_triangle_less:
-  fixes x y z :: "'a::real_normed_vector"
-  assumes "norm (x - y) < e1"  "norm (y - z) < e2"
-  shows "norm (x - z) < e1 + e2"
-  using norm_diff_triangle_ineq [of x y y z] assms by simp
+lemma norm_diff_triangle_less: "norm (x - z) < e1 + e2"
+  if "norm (x - y) < e1"  "norm (y - z) < e2"
+proof -
+  have "norm (x - z) \<le> norm (x - y) + norm (y - z)"
+    by (metis norm_diff_triangle_ineq add_diff_cancel_left' diff_diff_eq2)
+  with that show ?thesis by auto
+qed
 
 lemma norm_triangle_mono:
-  fixes a b :: "'a::real_normed_vector"
-  shows "norm a \<le> r \<Longrightarrow> norm b \<le> s \<Longrightarrow> norm (a + b) \<le> r + s"
-  by (metis add_mono_thms_linordered_semiring(1) norm_triangle_ineq order.trans)
+  "norm a \<le> r \<Longrightarrow> norm b \<le> s \<Longrightarrow> norm (a + b) \<le> r + s"
+  by (metis (mono_tags) add_mono_thms_linordered_semiring(1) norm_triangle_ineq order.trans)
 
-lemma norm_sum:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-  shows "norm (sum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
+lemma norm_sum: "norm (sum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
+  for f::"'b \<Rightarrow> 'a"
   by (induct A rule: infinite_finite_induct) (auto intro: norm_triangle_mono)
 
-lemma sum_norm_le:
-  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
-  assumes fg: "\<And>x. x \<in> S \<Longrightarrow> norm (f x) \<le> g x"
-  shows "norm (sum f S) \<le> sum g S"
-  by (rule order_trans [OF norm_sum sum_mono]) (simp add: fg)
+lemma sum_norm_le: "norm (sum f S) \<le> sum g S"
+  if "\<And>x. x \<in> S \<Longrightarrow> norm (f x) \<le> g x"
+  for f::"'b \<Rightarrow> 'a"
+  by (rule order_trans [OF norm_sum sum_mono]) (simp add: that)
 
 lemma abs_norm_cancel [simp]: "\<bar>norm a\<bar> = norm a"
-  for a :: "'a::real_normed_vector"
   by (rule abs_of_nonneg [OF norm_ge_zero])
 
+lemma sum_norm_bound:
+  "norm (sum f S) \<le> of_nat (card S)*K"
+  if "\<And>x. x \<in> S \<Longrightarrow> norm (f x) \<le> K"
+  for f :: "'b \<Rightarrow> 'a"
+  using sum_norm_le[OF that] sum_constant[symmetric]
+  by simp
+
 lemma norm_add_less: "norm x < r \<Longrightarrow> norm y < s \<Longrightarrow> norm (x + y) < r + s"
-  for x y :: "'a::real_normed_vector"
   by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
 
+end
+
+lemma dist_scaleR [simp]: "dist (x *\<^sub>R a) (y *\<^sub>R a) = \<bar>x - y\<bar> * norm a"
+  for a :: "'a::real_normed_vector"
+  by (metis dist_norm norm_scaleR scaleR_left.diff)
+
 lemma norm_mult_less: "norm x < r \<Longrightarrow> norm y < s \<Longrightarrow> norm (x * y) < r * s"
   for x y :: "'a::real_normed_algebra"
   by (rule order_le_less_trans [OF norm_mult_ineq]) (simp add: mult_strict_mono')
--- a/src/Pure/General/path.ML	Fri Dec 28 16:58:27 2018 +0100
+++ b/src/Pure/General/path.ML	Fri Dec 28 19:06:33 2018 +0100
@@ -239,4 +239,3 @@
 val explode = explode_path;
 
 end;
-