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