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