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