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