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