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