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