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