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