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