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