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