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