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