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