src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
author hoelzl
Thu, 17 Jan 2013 14:38:12 +0100
changeset 51959 03b11adf1f33
parent 51958 88a00a1c7c2c
child 51963 8c742f9de9f5
permissions -rw-r--r--
simplified prove of compact_imp_bounded
     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 {}" by (simp add: bounded_def)
  2156 lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T ==> bounded S"
  2157   by (metis bounded_def subset_eq)
  2158 
  2159 lemma bounded_interior[intro]: "bounded S ==> bounded(interior S)"
  2160   by (metis bounded_subset interior_subset)
  2161 
  2162 lemma bounded_closure[intro]: assumes "bounded S" shows "bounded(closure S)"
  2163 proof-
  2164   from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a" unfolding bounded_def by auto
  2165   { fix y assume "y \<in> closure S"
  2166     then obtain f where f: "\<forall>n. f n \<in> S"  "(f ---> y) sequentially"
  2167       unfolding closure_sequential by auto
  2168     have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp
  2169     hence "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"
  2170       by (rule eventually_mono, simp add: f(1))
  2171     have "dist x y \<le> a"
  2172       apply (rule Lim_dist_ubound [of sequentially f])
  2173       apply (rule trivial_limit_sequentially)
  2174       apply (rule f(2))
  2175       apply fact
  2176       done
  2177   }
  2178   thus ?thesis unfolding bounded_def by auto
  2179 qed
  2180 
  2181 lemma bounded_cball[simp,intro]: "bounded (cball x e)"
  2182   apply (simp add: bounded_def)
  2183   apply (rule_tac x=x in exI)
  2184   apply (rule_tac x=e in exI)
  2185   apply auto
  2186   done
  2187 
  2188 lemma bounded_ball[simp,intro]: "bounded(ball x e)"
  2189   by (metis ball_subset_cball bounded_cball bounded_subset)
  2190 
  2191 lemma finite_imp_bounded[intro]:
  2192   fixes S :: "'a::metric_space set" assumes "finite S" shows "bounded S"
  2193 proof-
  2194   { fix a and F :: "'a set" assume as:"bounded F"
  2195     then obtain x e where "\<forall>y\<in>F. dist x y \<le> e" unfolding bounded_def by auto
  2196     hence "\<forall>y\<in>(insert a F). dist x y \<le> max e (dist x a)" by auto
  2197     hence "bounded (insert a F)" unfolding bounded_def by (intro exI)
  2198   }
  2199   thus ?thesis using finite_induct[of S bounded]  using bounded_empty assms by auto
  2200 qed
  2201 
  2202 lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"
  2203   apply (auto simp add: bounded_def)
  2204   apply (rename_tac x y r s)
  2205   apply (rule_tac x=x in exI)
  2206   apply (rule_tac x="max r (dist x y + s)" in exI)
  2207   apply (rule ballI, rename_tac z, safe)
  2208   apply (drule (1) bspec, simp)
  2209   apply (drule (1) bspec)
  2210   apply (rule min_max.le_supI2)
  2211   apply (erule order_trans [OF dist_triangle add_left_mono])
  2212   done
  2213 
  2214 lemma bounded_Union[intro]: "finite F \<Longrightarrow> (\<forall>S\<in>F. bounded S) \<Longrightarrow> bounded(\<Union>F)"
  2215   by (induct rule: finite_induct[of F], auto)
  2216 
  2217 lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x <= b)"
  2218   apply (simp add: bounded_iff)
  2219   apply (subgoal_tac "\<And>x (y::real). 0 < 1 + abs y \<and> (x <= y \<longrightarrow> x <= 1 + abs y)")
  2220   by metis arith
  2221 
  2222 lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"
  2223   by (metis Int_lower1 Int_lower2 bounded_subset)
  2224 
  2225 lemma bounded_diff[intro]: "bounded S ==> bounded (S - T)"
  2226 apply (metis Diff_subset bounded_subset)
  2227 done
  2228 
  2229 lemma bounded_insert[intro]:"bounded(insert x S) \<longleftrightarrow> bounded S"
  2230   by (metis Diff_cancel Un_empty_right Un_insert_right bounded_Un bounded_subset finite.emptyI finite_imp_bounded infinite_remove subset_insertI)
  2231 
  2232 lemma not_bounded_UNIV[simp, intro]:
  2233   "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"
  2234 proof(auto simp add: bounded_pos not_le)
  2235   obtain x :: 'a where "x \<noteq> 0"
  2236     using perfect_choose_dist [OF zero_less_one] by fast
  2237   fix b::real  assume b: "b >0"
  2238   have b1: "b +1 \<ge> 0" using b by simp
  2239   with `x \<noteq> 0` have "b < norm (scaleR (b + 1) (sgn x))"
  2240     by (simp add: norm_sgn)
  2241   then show "\<exists>x::'a. b < norm x" ..
  2242 qed
  2243 
  2244 lemma bounded_linear_image:
  2245   assumes "bounded S" "bounded_linear f"
  2246   shows "bounded(f ` S)"
  2247 proof-
  2248   from assms(1) obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
  2249   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)
  2250   { fix x assume "x\<in>S"
  2251     hence "norm x \<le> b" using b by auto
  2252     hence "norm (f x) \<le> B * b" using B(2) apply(erule_tac x=x in allE)
  2253       by (metis B(1) B(2) order_trans mult_le_cancel_left_pos)
  2254   }
  2255   thus ?thesis unfolding bounded_pos apply(rule_tac x="b*B" in exI)
  2256     using b B mult_pos_pos [of b B] by (auto simp add: mult_commute)
  2257 qed
  2258 
  2259 lemma bounded_scaling:
  2260   fixes S :: "'a::real_normed_vector set"
  2261   shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x) ` S)"
  2262   apply (rule bounded_linear_image, assumption)
  2263   apply (rule bounded_linear_scaleR_right)
  2264   done
  2265 
  2266 lemma bounded_translation:
  2267   fixes S :: "'a::real_normed_vector set"
  2268   assumes "bounded S" shows "bounded ((\<lambda>x. a + x) ` S)"
  2269 proof-
  2270   from assms obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto
  2271   { fix x assume "x\<in>S"
  2272     hence "norm (a + x) \<le> b + norm a" using norm_triangle_ineq[of a x] b by auto
  2273   }
  2274   thus ?thesis unfolding bounded_pos using norm_ge_zero[of a] b(1) using add_strict_increasing[of b 0 "norm a"]
  2275     by (auto intro!: exI[of _ "b + norm a"])
  2276 qed
  2277 
  2278 
  2279 text{* Some theorems on sups and infs using the notion "bounded". *}
  2280 
  2281 lemma bounded_real:
  2282   fixes S :: "real set"
  2283   shows "bounded S \<longleftrightarrow>  (\<exists>a. \<forall>x\<in>S. abs x <= a)"
  2284   by (simp add: bounded_iff)
  2285 
  2286 lemma bounded_has_Sup:
  2287   fixes S :: "real set"
  2288   assumes "bounded S" "S \<noteq> {}"
  2289   shows "\<forall>x\<in>S. x <= Sup S" and "\<forall>b. (\<forall>x\<in>S. x <= b) \<longrightarrow> Sup S <= b"
  2290 proof
  2291   fix x assume "x\<in>S"
  2292   thus "x \<le> Sup S"
  2293     by (metis SupInf.Sup_upper abs_le_D1 assms(1) bounded_real)
  2294 next
  2295   show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b" using assms
  2296     by (metis SupInf.Sup_least)
  2297 qed
  2298 
  2299 lemma Sup_insert:
  2300   fixes S :: "real set"
  2301   shows "bounded S ==> Sup(insert x S) = (if S = {} then x else max x (Sup S))" 
  2302 by auto (metis Int_absorb Sup_insert_nonempty assms bounded_has_Sup(1) disjoint_iff_not_equal) 
  2303 
  2304 lemma Sup_insert_finite:
  2305   fixes S :: "real set"
  2306   shows "finite S \<Longrightarrow> Sup(insert x S) = (if S = {} then x else max x (Sup S))"
  2307   apply (rule Sup_insert)
  2308   apply (rule finite_imp_bounded)
  2309   by simp
  2310 
  2311 lemma bounded_has_Inf:
  2312   fixes S :: "real set"
  2313   assumes "bounded S"  "S \<noteq> {}"
  2314   shows "\<forall>x\<in>S. x >= Inf S" and "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S >= b"
  2315 proof
  2316   fix x assume "x\<in>S"
  2317   from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto
  2318   thus "x \<ge> Inf S" using `x\<in>S`
  2319     by (metis Inf_lower_EX abs_le_D2 minus_le_iff)
  2320 next
  2321   show "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S \<ge> b" using assms
  2322     by (metis SupInf.Inf_greatest)
  2323 qed
  2324 
  2325 lemma Inf_insert:
  2326   fixes S :: "real set"
  2327   shows "bounded S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))" 
  2328 by auto (metis Int_absorb Inf_insert_nonempty bounded_has_Inf(1) disjoint_iff_not_equal)
  2329 
  2330 lemma Inf_insert_finite:
  2331   fixes S :: "real set"
  2332   shows "finite S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"
  2333   by (rule Inf_insert, rule finite_imp_bounded, simp)
  2334 
  2335 subsection {* Compactness *}
  2336 
  2337 subsubsection{* Open-cover compactness *}
  2338 
  2339 definition compact :: "'a::topological_space set \<Rightarrow> bool" where
  2340   compact_eq_heine_borel: -- "This name is used for backwards compatibility"
  2341     "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))"
  2342 
  2343 lemma compactI:
  2344   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'"
  2345   shows "compact s"
  2346   unfolding compact_eq_heine_borel using assms by metis
  2347 
  2348 lemma compactE:
  2349   assumes "compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C"
  2350   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
  2351   using assms unfolding compact_eq_heine_borel by metis
  2352 
  2353 lemma compactE_image:
  2354   assumes "compact s" and "\<forall>t\<in>C. open (f t)" and "s \<subseteq> (\<Union>c\<in>C. f c)"
  2355   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> (\<Union>c\<in>C'. f c)"
  2356   using assms unfolding ball_simps[symmetric] SUP_def
  2357   by (metis (lifting) finite_subset_image compact_eq_heine_borel[of s])
  2358 
  2359 subsubsection {* Bolzano-Weierstrass property *}
  2360 
  2361 lemma heine_borel_imp_bolzano_weierstrass:
  2362   assumes "compact s" "infinite t"  "t \<subseteq> s"
  2363   shows "\<exists>x \<in> s. x islimpt t"
  2364 proof(rule ccontr)
  2365   assume "\<not> (\<exists>x \<in> s. x islimpt t)"
  2366   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
  2367     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
  2368   obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"
  2369     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
  2370   from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto
  2371   { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"
  2372     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
  2373     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  }
  2374   hence "inj_on f t" unfolding inj_on_def by simp
  2375   hence "infinite (f ` t)" using assms(2) using finite_imageD by auto
  2376   moreover
  2377   { fix x assume "x\<in>t" "f x \<notin> g"
  2378     from g(3) assms(3) `x\<in>t` obtain h where "h\<in>g" and "x\<in>h" by auto
  2379     then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto
  2380     hence "y = x" using f[THEN bspec[where x=y]] and `x\<in>t` and `x\<in>h`[unfolded `h = f y`] by auto
  2381     hence False using `f x \<notin> g` `h\<in>g` unfolding `h = f y` by auto  }
  2382   hence "f ` t \<subseteq> g" by auto
  2383   ultimately show False using g(2) using finite_subset by auto
  2384 qed
  2385 
  2386 lemma acc_point_range_imp_convergent_subsequence:
  2387   fixes l :: "'a :: first_countable_topology"
  2388   assumes l: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> range f)"
  2389   shows "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"
  2390 proof -
  2391   from countable_basis_at_decseq[of l] guess A . note A = this
  2392 
  2393   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> f j \<in> A (Suc n)"
  2394   { fix n i
  2395     have "infinite (A (Suc n) \<inter> range f - f`{.. i})"
  2396       using l A by auto
  2397     then have "\<exists>x. x \<in> A (Suc n) \<inter> range f - f`{.. i}"
  2398       unfolding ex_in_conv by (intro notI) simp
  2399     then have "\<exists>j. f j \<in> A (Suc n) \<and> j \<notin> {.. i}"
  2400       by auto
  2401     then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)"
  2402       by (auto simp: not_le)
  2403     then have "i < s n i" "f (s n i) \<in> A (Suc n)"
  2404       unfolding s_def by (auto intro: someI2_ex) }
  2405   note s = this
  2406   def r \<equiv> "nat_rec (s 0 0) s"
  2407   have "subseq r"
  2408     by (auto simp: r_def s subseq_Suc_iff)
  2409   moreover
  2410   have "(\<lambda>n. f (r n)) ----> l"
  2411   proof (rule topological_tendstoI)
  2412     fix S assume "open S" "l \<in> S"
  2413     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto
  2414     moreover
  2415     { fix i assume "Suc 0 \<le> i" then have "f (r i) \<in> A i"
  2416         by (cases i) (simp_all add: r_def s) }
  2417     then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)
  2418     ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"
  2419       by eventually_elim auto
  2420   qed
  2421   ultimately show "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"
  2422     by (auto simp: convergent_def comp_def)
  2423 qed
  2424 
  2425 lemma sequence_infinite_lemma:
  2426   fixes f :: "nat \<Rightarrow> 'a::t1_space"
  2427   assumes "\<forall>n. f n \<noteq> l" and "(f ---> l) sequentially"
  2428   shows "infinite (range f)"
  2429 proof
  2430   assume "finite (range f)"
  2431   hence "closed (range f)" by (rule finite_imp_closed)
  2432   hence "open (- range f)" by (rule open_Compl)
  2433   from assms(1) have "l \<in> - range f" by auto
  2434   from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"
  2435     using `open (- range f)` `l \<in> - range f` by (rule topological_tendstoD)
  2436   thus False unfolding eventually_sequentially by auto
  2437 qed
  2438 
  2439 lemma closure_insert:
  2440   fixes x :: "'a::t1_space"
  2441   shows "closure (insert x s) = insert x (closure s)"
  2442 apply (rule closure_unique)
  2443 apply (rule insert_mono [OF closure_subset])
  2444 apply (rule closed_insert [OF closed_closure])
  2445 apply (simp add: closure_minimal)
  2446 done
  2447 
  2448 lemma islimpt_insert:
  2449   fixes x :: "'a::t1_space"
  2450   shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"
  2451 proof
  2452   assume *: "x islimpt (insert a s)"
  2453   show "x islimpt s"
  2454   proof (rule islimptI)
  2455     fix t assume t: "x \<in> t" "open t"
  2456     show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"
  2457     proof (cases "x = a")
  2458       case True
  2459       obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"
  2460         using * t by (rule islimptE)
  2461       with `x = a` show ?thesis by auto
  2462     next
  2463       case False
  2464       with t have t': "x \<in> t - {a}" "open (t - {a})"
  2465         by (simp_all add: open_Diff)
  2466       obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"
  2467         using * t' by (rule islimptE)
  2468       thus ?thesis by auto
  2469     qed
  2470   qed
  2471 next
  2472   assume "x islimpt s" thus "x islimpt (insert a s)"
  2473     by (rule islimpt_subset) auto
  2474 qed
  2475 
  2476 lemma islimpt_finite:
  2477   fixes x :: "'a::t1_space"
  2478   shows "finite s \<Longrightarrow> \<not> x islimpt s"
  2479 by (induct set: finite, simp_all add: islimpt_insert)
  2480 
  2481 lemma islimpt_union_finite:
  2482   fixes x :: "'a::t1_space"
  2483   shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"
  2484 by (simp add: islimpt_Un islimpt_finite)
  2485 
  2486 lemma islimpt_eq_acc_point:
  2487   fixes l :: "'a :: t1_space"
  2488   shows "l islimpt S \<longleftrightarrow> (\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S))"
  2489 proof (safe intro!: islimptI)
  2490   fix U assume "l islimpt S" "l \<in> U" "open U" "finite (U \<inter> S)"
  2491   then have "l islimpt S" "l \<in> (U - (U \<inter> S - {l}))" "open (U - (U \<inter> S - {l}))"
  2492     by (auto intro: finite_imp_closed)
  2493   then show False
  2494     by (rule islimptE) auto
  2495 next
  2496   fix T assume *: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S)" "l \<in> T" "open T"
  2497   then have "infinite (T \<inter> S - {l})" by auto
  2498   then have "\<exists>x. x \<in> (T \<inter> S - {l})"
  2499     unfolding ex_in_conv by (intro notI) simp
  2500   then show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> l"
  2501     by auto
  2502 qed
  2503 
  2504 lemma islimpt_range_imp_convergent_subsequence:
  2505   fixes l :: "'a :: {t1_space, first_countable_topology}"
  2506   assumes l: "l islimpt (range f)"
  2507   shows "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"
  2508   using l unfolding islimpt_eq_acc_point
  2509   by (rule acc_point_range_imp_convergent_subsequence)
  2510 
  2511 lemma sequence_unique_limpt:
  2512   fixes f :: "nat \<Rightarrow> 'a::t2_space"
  2513   assumes "(f ---> l) sequentially" and "l' islimpt (range f)"
  2514   shows "l' = l"
  2515 proof (rule ccontr)
  2516   assume "l' \<noteq> l"
  2517   obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"
  2518     using hausdorff [OF `l' \<noteq> l`] by auto
  2519   have "eventually (\<lambda>n. f n \<in> t) sequentially"
  2520     using assms(1) `open t` `l \<in> t` by (rule topological_tendstoD)
  2521   then obtain N where "\<forall>n\<ge>N. f n \<in> t"
  2522     unfolding eventually_sequentially by auto
  2523 
  2524   have "UNIV = {..<N} \<union> {N..}" by auto
  2525   hence "l' islimpt (f ` ({..<N} \<union> {N..}))" using assms(2) by simp
  2526   hence "l' islimpt (f ` {..<N} \<union> f ` {N..})" by (simp add: image_Un)
  2527   hence "l' islimpt (f ` {N..})" by (simp add: islimpt_union_finite)
  2528   then obtain y where "y \<in> f ` {N..}" "y \<in> s" "y \<noteq> l'"
  2529     using `l' \<in> s` `open s` by (rule islimptE)
  2530   then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'" by auto
  2531   with `\<forall>n\<ge>N. f n \<in> t` have "f n \<in> s \<inter> t" by simp
  2532   with `s \<inter> t = {}` show False by simp
  2533 qed
  2534 
  2535 lemma bolzano_weierstrass_imp_closed:
  2536   fixes s :: "'a::{first_countable_topology, t2_space} set"
  2537   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"
  2538   shows "closed s"
  2539 proof-
  2540   { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"
  2541     hence "l \<in> s"
  2542     proof(cases "\<forall>n. x n \<noteq> l")
  2543       case False thus "l\<in>s" using as(1) by auto
  2544     next
  2545       case True note cas = this
  2546       with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto
  2547       then obtain l' where "l'\<in>s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto
  2548       thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto
  2549     qed  }
  2550   thus ?thesis unfolding closed_sequential_limits by fast
  2551 qed
  2552 
  2553 lemma compact_imp_closed:
  2554   fixes s :: "'a::t2_space set"
  2555   assumes "compact s" shows "closed s"
  2556 unfolding closed_def
  2557 proof (rule openI)
  2558   fix y assume "y \<in> - s"
  2559   let ?C = "\<Union>x\<in>s. {u. open u \<and> x \<in> u \<and> eventually (\<lambda>y. y \<notin> u) (nhds y)}"
  2560   note `compact s`
  2561   moreover have "\<forall>u\<in>?C. open u" by simp
  2562   moreover have "s \<subseteq> \<Union>?C"
  2563   proof
  2564     fix x assume "x \<in> s"
  2565     with `y \<in> - s` have "x \<noteq> y" by clarsimp
  2566     hence "\<exists>u v. open u \<and> open v \<and> x \<in> u \<and> y \<in> v \<and> u \<inter> v = {}"
  2567       by (rule hausdorff)
  2568     with `x \<in> s` show "x \<in> \<Union>?C"
  2569       unfolding eventually_nhds by auto
  2570   qed
  2571   ultimately obtain D where "D \<subseteq> ?C" and "finite D" and "s \<subseteq> \<Union>D"
  2572     by (rule compactE)
  2573   from `D \<subseteq> ?C` have "\<forall>x\<in>D. eventually (\<lambda>y. y \<notin> x) (nhds y)" by auto
  2574   with `finite D` have "eventually (\<lambda>y. y \<notin> \<Union>D) (nhds y)"
  2575     by (simp add: eventually_Ball_finite)
  2576   with `s \<subseteq> \<Union>D` have "eventually (\<lambda>y. y \<notin> s) (nhds y)"
  2577     by (auto elim!: eventually_mono [rotated])
  2578   thus "\<exists>t. open t \<and> y \<in> t \<and> t \<subseteq> - s"
  2579     by (simp add: eventually_nhds subset_eq)
  2580 qed
  2581 
  2582 lemma compact_imp_bounded:
  2583   assumes "compact U" shows "bounded U"
  2584 proof -
  2585   have "compact U" "\<forall>x\<in>U. open (ball x 1)" "U \<subseteq> (\<Union>x\<in>U. ball x 1)" using assms by auto
  2586   then obtain D where D: "D \<subseteq> U" "finite D" "U \<subseteq> (\<Union>x\<in>D. ball x 1)"
  2587     by (elim compactE_image)
  2588   def d \<equiv> "SOME d. d \<in> D"
  2589   show "bounded U"
  2590     unfolding bounded_def
  2591   proof (intro exI, safe)
  2592     fix x assume "x \<in> U"
  2593     with D obtain d' where "d' \<in> D" "x \<in> ball d' 1" by auto
  2594     moreover have "dist d x \<le> dist d d' + dist d' x"
  2595       using dist_triangle[of d x d'] by (simp add: dist_commute)
  2596     moreover
  2597     from `x\<in>U` D have "d \<in> D"
  2598       unfolding d_def by (rule_tac someI_ex) auto
  2599     ultimately
  2600     show "dist d x \<le> Max ((\<lambda>d'. dist d d' + 1) ` D)"
  2601       using D by (subst Max_ge_iff) (auto intro!: bexI[of _ d'])
  2602   qed
  2603 qed
  2604 
  2605 text{* In particular, some common special cases. *}
  2606 
  2607 lemma compact_empty[simp]:
  2608  "compact {}"
  2609   unfolding compact_eq_heine_borel
  2610   by auto
  2611 
  2612 lemma compact_union [intro]:
  2613   assumes "compact s" "compact t" shows " compact (s \<union> t)"
  2614 proof (rule compactI)
  2615   fix f assume *: "Ball f open" "s \<union> t \<subseteq> \<Union>f"
  2616   from * `compact s` obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"
  2617     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis
  2618   moreover from * `compact t` obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"
  2619     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis
  2620   ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"
  2621     by (auto intro!: exI[of _ "s' \<union> t'"])
  2622 qed
  2623 
  2624 lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"
  2625   by (induct set: finite) auto
  2626 
  2627 lemma compact_UN [intro]:
  2628   "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"
  2629   unfolding SUP_def by (rule compact_Union) auto
  2630 
  2631 lemma compact_inter_closed [intro]:
  2632   assumes "compact s" and "closed t"
  2633   shows "compact (s \<inter> t)"
  2634 proof (rule compactI)
  2635   fix C assume C: "\<forall>c\<in>C. open c" and cover: "s \<inter> t \<subseteq> \<Union>C"
  2636   from C `closed t` have "\<forall>c\<in>C \<union> {-t}. open c" by auto
  2637   moreover from cover have "s \<subseteq> \<Union>(C \<union> {-t})" by auto
  2638   ultimately have "\<exists>D\<subseteq>C \<union> {-t}. finite D \<and> s \<subseteq> \<Union>D"
  2639     using `compact s` unfolding compact_eq_heine_borel by auto
  2640   then guess D ..
  2641   then show "\<exists>D\<subseteq>C. finite D \<and> s \<inter> t \<subseteq> \<Union>D"
  2642     by (intro exI[of _ "D - {-t}"]) auto
  2643 qed
  2644 
  2645 lemma closed_inter_compact [intro]:
  2646   assumes "closed s" and "compact t"
  2647   shows "compact (s \<inter> t)"
  2648   using compact_inter_closed [of t s] assms
  2649   by (simp add: Int_commute)
  2650 
  2651 lemma compact_inter [intro]:
  2652   fixes s t :: "'a :: t2_space set"
  2653   assumes "compact s" and "compact t"
  2654   shows "compact (s \<inter> t)"
  2655   using assms by (intro compact_inter_closed compact_imp_closed)
  2656 
  2657 lemma compact_sing [simp]: "compact {a}"
  2658   unfolding compact_eq_heine_borel by auto
  2659 
  2660 lemma compact_insert [simp]:
  2661   assumes "compact s" shows "compact (insert x s)"
  2662 proof -
  2663   have "compact ({x} \<union> s)"
  2664     using compact_sing assms by (rule compact_union)
  2665   thus ?thesis by simp
  2666 qed
  2667 
  2668 lemma finite_imp_compact:
  2669   shows "finite s \<Longrightarrow> compact s"
  2670   by (induct set: finite) simp_all
  2671 
  2672 lemma open_delete:
  2673   fixes s :: "'a::t1_space set"
  2674   shows "open s \<Longrightarrow> open (s - {x})"
  2675   by (simp add: open_Diff)
  2676 
  2677 text{* Finite intersection property *}
  2678 
  2679 lemma inj_setminus: "inj_on uminus (A::'a set set)"
  2680   by (auto simp: inj_on_def)
  2681 
  2682 lemma compact_fip:
  2683   "compact U \<longleftrightarrow>
  2684     (\<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> {})"
  2685   (is "_ \<longleftrightarrow> ?R")
  2686 proof (safe intro!: compact_eq_heine_borel[THEN iffD2])
  2687   fix A assume "compact U" and A: "\<forall>a\<in>A. closed a" "U \<inter> \<Inter>A = {}"
  2688     and fi: "\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}"
  2689   from A have "(\<forall>a\<in>uminus`A. open a) \<and> U \<subseteq> \<Union>uminus`A"
  2690     by auto
  2691   with `compact U` obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<subseteq> \<Union>(uminus`B)"
  2692     unfolding compact_eq_heine_borel by (metis subset_image_iff)
  2693   with fi[THEN spec, of B] show False
  2694     by (auto dest: finite_imageD intro: inj_setminus)
  2695 next
  2696   fix A assume ?R and cover: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
  2697   from cover have "U \<inter> \<Inter>(uminus`A) = {}" "\<forall>a\<in>uminus`A. closed a"
  2698     by auto
  2699   with `?R` obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<inter> \<Inter>uminus`B = {}"
  2700     by (metis subset_image_iff)
  2701   then show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  2702     by  (auto intro!: exI[of _ B] inj_setminus dest: finite_imageD)
  2703 qed
  2704 
  2705 lemma compact_imp_fip:
  2706   "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>
  2707     s \<inter> (\<Inter> f) \<noteq> {}"
  2708   unfolding compact_fip by auto
  2709 
  2710 text{*Compactness expressed with filters*}
  2711 
  2712 definition "filter_from_subbase B = Abs_filter (\<lambda>P. \<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"
  2713 
  2714 lemma eventually_filter_from_subbase:
  2715   "eventually P (filter_from_subbase B) \<longleftrightarrow> (\<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"
  2716     (is "_ \<longleftrightarrow> ?R P")
  2717   unfolding filter_from_subbase_def
  2718 proof (rule eventually_Abs_filter is_filter.intro)+
  2719   show "?R (\<lambda>x. True)"
  2720     by (rule exI[of _ "{}"]) (simp add: le_fun_def)
  2721 next
  2722   fix P Q assume "?R P" then guess X ..
  2723   moreover assume "?R Q" then guess Y ..
  2724   ultimately show "?R (\<lambda>x. P x \<and> Q x)"
  2725     by (intro exI[of _ "X \<union> Y"]) auto
  2726 next
  2727   fix P Q
  2728   assume "?R P" then guess X ..
  2729   moreover assume "\<forall>x. P x \<longrightarrow> Q x"
  2730   ultimately show "?R Q"
  2731     by (intro exI[of _ X]) auto
  2732 qed
  2733 
  2734 lemma eventually_filter_from_subbaseI: "P \<in> B \<Longrightarrow> eventually P (filter_from_subbase B)"
  2735   by (subst eventually_filter_from_subbase) (auto intro!: exI[of _ "{P}"])
  2736 
  2737 lemma filter_from_subbase_not_bot:
  2738   "\<forall>X \<subseteq> B. finite X \<longrightarrow> Inf X \<noteq> bot \<Longrightarrow> filter_from_subbase B \<noteq> bot"
  2739   unfolding trivial_limit_def eventually_filter_from_subbase by auto
  2740 
  2741 lemma closure_iff_nhds_not_empty:
  2742   "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"
  2743 proof safe
  2744   assume x: "x \<in> closure X"
  2745   fix S A assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"
  2746   then have "x \<notin> closure (-S)" 
  2747     by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)
  2748   with x have "x \<in> closure X - closure (-S)"
  2749     by auto
  2750   also have "\<dots> \<subseteq> closure (X \<inter> S)"
  2751     using `open S` open_inter_closure_subset[of S X] by (simp add: closed_Compl ac_simps)
  2752   finally have "X \<inter> S \<noteq> {}" by auto
  2753   then show False using `X \<inter> A = {}` `S \<subseteq> A` by auto
  2754 next
  2755   assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"
  2756   from this[THEN spec, of "- X", THEN spec, of "- closure X"]
  2757   show "x \<in> closure X"
  2758     by (simp add: closure_subset open_Compl)
  2759 qed
  2760 
  2761 lemma compact_filter:
  2762   "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))"
  2763 proof (intro allI iffI impI compact_fip[THEN iffD2] notI)
  2764   fix F assume "compact U" and F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"
  2765   from F have "U \<noteq> {}"
  2766     by (auto simp: eventually_False)
  2767 
  2768   def Z \<equiv> "closure ` {A. eventually (\<lambda>x. x \<in> A) F}"
  2769   then have "\<forall>z\<in>Z. closed z"
  2770     by auto
  2771   moreover 
  2772   have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"
  2773     unfolding Z_def by (auto elim: eventually_elim1 intro: set_mp[OF closure_subset])
  2774   have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"
  2775   proof (intro allI impI)
  2776     fix B assume "finite B" "B \<subseteq> Z"
  2777     with `finite B` ev_Z have "eventually (\<lambda>x. \<forall>b\<in>B. x \<in> b) F"
  2778       by (auto intro!: eventually_Ball_finite)
  2779     with F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"
  2780       by eventually_elim auto
  2781     with F show "U \<inter> \<Inter>B \<noteq> {}"
  2782       by (intro notI) (simp add: eventually_False)
  2783   qed
  2784   ultimately have "U \<inter> \<Inter>Z \<noteq> {}"
  2785     using `compact U` unfolding compact_fip by blast
  2786   then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z" by auto
  2787 
  2788   have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"
  2789     unfolding eventually_inf eventually_nhds
  2790   proof safe
  2791     fix P Q R S
  2792     assume "eventually R F" "open S" "x \<in> S"
  2793     with open_inter_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]
  2794     have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)
  2795     moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"
  2796     ultimately show False by (auto simp: set_eq_iff)
  2797   qed
  2798   with `x \<in> U` show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"
  2799     by (metis eventually_bot)
  2800 next
  2801   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 = {}"
  2802 
  2803   def P' \<equiv> "(\<lambda>a (x::'a). x \<in> a)"
  2804   then have inj_P': "\<And>A. inj_on P' A"
  2805     by (auto intro!: inj_onI simp: fun_eq_iff)
  2806   def F \<equiv> "filter_from_subbase (P' ` insert U A)"
  2807   have "F \<noteq> bot"
  2808     unfolding F_def
  2809   proof (safe intro!: filter_from_subbase_not_bot)
  2810     fix X assume "X \<subseteq> P' ` insert U A" "finite X" "Inf X = bot"
  2811     then obtain B where "B \<subseteq> insert U A" "finite B" and B: "Inf (P' ` B) = bot"
  2812       unfolding subset_image_iff by (auto intro: inj_P' finite_imageD)
  2813     with A(2)[THEN spec, of "B - {U}"] have "U \<inter> \<Inter>(B - {U}) \<noteq> {}" by auto
  2814     with B show False by (auto simp: P'_def fun_eq_iff)
  2815   qed
  2816   moreover have "eventually (\<lambda>x. x \<in> U) F"
  2817     unfolding F_def by (rule eventually_filter_from_subbaseI) (auto simp: P'_def)
  2818   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)"
  2819   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"
  2820     by auto
  2821 
  2822   { fix V assume "V \<in> A"
  2823     then have V: "eventually (\<lambda>x. x \<in> V) F"
  2824       by (auto simp add: F_def image_iff P'_def intro!: eventually_filter_from_subbaseI)
  2825     have "x \<in> closure V"
  2826       unfolding closure_iff_nhds_not_empty
  2827     proof (intro impI allI)
  2828       fix S A assume "open S" "x \<in> S" "S \<subseteq> A"
  2829       then have "eventually (\<lambda>x. x \<in> A) (nhds x)" by (auto simp: eventually_nhds)
  2830       with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"
  2831         by (auto simp: eventually_inf)
  2832       with x show "V \<inter> A \<noteq> {}"
  2833         by (auto simp del: Int_iff simp add: trivial_limit_def)
  2834     qed
  2835     then have "x \<in> V"
  2836       using `V \<in> A` A(1) by simp }
  2837   with `x\<in>U` have "x \<in> U \<inter> \<Inter>A" by auto
  2838   with `U \<inter> \<Inter>A = {}` show False by auto
  2839 qed
  2840 
  2841 definition "countably_compact U \<longleftrightarrow>
  2842     (\<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))"
  2843 
  2844 lemma countably_compactE:
  2845   assumes "countably_compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C" "countable C"
  2846   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
  2847   using assms unfolding countably_compact_def by metis
  2848 
  2849 lemma countably_compactI:
  2850   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')"
  2851   shows "countably_compact s"
  2852   using assms unfolding countably_compact_def by metis
  2853 
  2854 lemma compact_imp_countably_compact: "compact U \<Longrightarrow> countably_compact U"
  2855   by (auto simp: compact_eq_heine_borel countably_compact_def)
  2856 
  2857 lemma countably_compact_imp_compact:
  2858   assumes "countably_compact U"
  2859   assumes ccover: "countable B" "\<forall>b\<in>B. open b"
  2860   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"
  2861   shows "compact U"
  2862   using `countably_compact U` unfolding compact_eq_heine_borel countably_compact_def
  2863 proof safe
  2864   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
  2865   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)"
  2866 
  2867   moreover def C \<equiv> "{b\<in>B. \<exists>a\<in>A. b \<inter> U \<subseteq> a}"
  2868   ultimately have "countable C" "\<forall>a\<in>C. open a"
  2869     unfolding C_def using ccover by auto
  2870   moreover
  2871   have "\<Union>A \<inter> U \<subseteq> \<Union>C"
  2872   proof safe
  2873     fix x a assume "x \<in> U" "x \<in> a" "a \<in> A"
  2874     with basis[of a x] A obtain b where "b \<in> B" "x \<in> b" "b \<inter> U \<subseteq> a" by blast
  2875     with `a \<in> A` show "x \<in> \<Union>C" unfolding C_def
  2876       by auto
  2877   qed
  2878   then have "U \<subseteq> \<Union>C" using `U \<subseteq> \<Union>A` by auto
  2879   ultimately obtain T where "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"
  2880     using * by metis
  2881   moreover then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<inter> U \<subseteq> a"
  2882     by (auto simp: C_def)
  2883   then guess f unfolding bchoice_iff Bex_def ..
  2884   ultimately show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  2885     unfolding C_def by (intro exI[of _ "f`T"]) fastforce
  2886 qed
  2887 
  2888 lemma countably_compact_imp_compact_second_countable:
  2889   "countably_compact U \<Longrightarrow> compact (U :: 'a :: second_countable_topology set)"
  2890 proof (rule countably_compact_imp_compact)
  2891   fix T and x :: 'a assume "open T" "x \<in> T"
  2892   from topological_basisE[OF is_basis this] guess b .
  2893   then show "\<exists>b\<in>SOME B. countable B \<and> topological_basis B. x \<in> b \<and> b \<inter> U \<subseteq> T" by auto
  2894 qed (insert countable_basis topological_basis_open[OF is_basis], auto)
  2895 
  2896 lemma countably_compact_eq_compact:
  2897   "countably_compact U \<longleftrightarrow> compact (U :: 'a :: second_countable_topology set)"
  2898   using countably_compact_imp_compact_second_countable compact_imp_countably_compact by blast
  2899   
  2900 subsubsection{* Sequential compactness *}
  2901 
  2902 definition
  2903   seq_compact :: "'a::topological_space set \<Rightarrow> bool" where
  2904   "seq_compact S \<longleftrightarrow>
  2905    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>
  2906        (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"
  2907 
  2908 lemma seq_compact_imp_countably_compact:
  2909   fixes U :: "'a :: first_countable_topology set"
  2910   assumes "seq_compact U"
  2911   shows "countably_compact U"
  2912 proof (safe intro!: countably_compactI)
  2913   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"
  2914   have subseq: "\<And>X. range X \<subseteq> U \<Longrightarrow> \<exists>r x. x \<in> U \<and> subseq r \<and> (X \<circ> r) ----> x"
  2915     using `seq_compact U` by (fastforce simp: seq_compact_def subset_eq)
  2916   show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  2917   proof cases
  2918     assume "finite A" with A show ?thesis by auto
  2919   next
  2920     assume "infinite A"
  2921     then have "A \<noteq> {}" by auto
  2922     show ?thesis
  2923     proof (rule ccontr)
  2924       assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"
  2925       then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)" by auto
  2926       then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T" by metis
  2927       def X \<equiv> "\<lambda>n. X' (from_nat_into A ` {.. n})"
  2928       have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"
  2929         using `A \<noteq> {}` unfolding X_def SUP_def by (intro T) (auto intro: from_nat_into)
  2930       then have "range X \<subseteq> U" by auto
  2931       with subseq[of X] obtain r x where "x \<in> U" and r: "subseq r" "(X \<circ> r) ----> x" by auto
  2932       from `x\<in>U` `U \<subseteq> \<Union>A` from_nat_into_surj[OF `countable A`]
  2933       obtain n where "x \<in> from_nat_into A n" by auto
  2934       with r(2) A(1) from_nat_into[OF `A \<noteq> {}`, of n]
  2935       have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"
  2936         unfolding tendsto_def by (auto simp: comp_def)
  2937       then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"
  2938         by (auto simp: eventually_sequentially)
  2939       moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"
  2940         by auto
  2941       moreover from `subseq r`[THEN seq_suble, of "max n N"] have "\<exists>i. n \<le> r i \<and> N \<le> i"
  2942         by (auto intro!: exI[of _ "max n N"])
  2943       ultimately show False
  2944         by auto
  2945     qed
  2946   qed
  2947 qed
  2948 
  2949 lemma compact_imp_seq_compact:
  2950   fixes U :: "'a :: first_countable_topology set"
  2951   assumes "compact U" shows "seq_compact U"
  2952   unfolding seq_compact_def
  2953 proof safe
  2954   fix X :: "nat \<Rightarrow> 'a" assume "\<forall>n. X n \<in> U"
  2955   then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"
  2956     by (auto simp: eventually_filtermap)
  2957   moreover have "filtermap X sequentially \<noteq> bot"
  2958     by (simp add: trivial_limit_def eventually_filtermap)
  2959   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")
  2960     using `compact U` by (auto simp: compact_filter)
  2961 
  2962   from countable_basis_at_decseq[of x] guess A . note A = this
  2963   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> X j \<in> A (Suc n)"
  2964   { fix n i
  2965     have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"
  2966     proof (rule ccontr)
  2967       assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"
  2968       then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)" by auto
  2969       then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"
  2970         by (auto simp: eventually_filtermap eventually_sequentially)
  2971       moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"
  2972         using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)
  2973       ultimately have "eventually (\<lambda>x. False) ?F"
  2974         by (auto simp add: eventually_inf)
  2975       with x show False
  2976         by (simp add: eventually_False)
  2977     qed
  2978     then have "i < s n i" "X (s n i) \<in> A (Suc n)"
  2979       unfolding s_def by (auto intro: someI2_ex) }
  2980   note s = this
  2981   def r \<equiv> "nat_rec (s 0 0) s"
  2982   have "subseq r"
  2983     by (auto simp: r_def s subseq_Suc_iff)
  2984   moreover
  2985   have "(\<lambda>n. X (r n)) ----> x"
  2986   proof (rule topological_tendstoI)
  2987     fix S assume "open S" "x \<in> S"
  2988     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto
  2989     moreover
  2990     { fix i assume "Suc 0 \<le> i" then have "X (r i) \<in> A i"
  2991         by (cases i) (simp_all add: r_def s) }
  2992     then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)
  2993     ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"
  2994       by eventually_elim auto
  2995   qed
  2996   ultimately show "\<exists>x \<in> U. \<exists>r. subseq r \<and> (X \<circ> r) ----> x"
  2997     using `x \<in> U` by (auto simp: convergent_def comp_def)
  2998 qed
  2999 
  3000 lemma seq_compactI:
  3001   assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially"
  3002   shows "seq_compact S"
  3003   unfolding seq_compact_def using assms by fast
  3004 
  3005 lemma seq_compactE:
  3006   assumes "seq_compact S" "\<forall>n. f n \<in> S"
  3007   obtains l r where "l \<in> S" "subseq r" "((f \<circ> r) ---> l) sequentially"
  3008   using assms unfolding seq_compact_def by fast
  3009 
  3010 lemma countably_compact_imp_acc_point:
  3011   assumes "countably_compact s" "countable t" "infinite t"  "t \<subseteq> s"
  3012   shows "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)"
  3013 proof (rule ccontr)
  3014   def C \<equiv> "(\<lambda>F. interior (F \<union> (- t))) ` {F. finite F \<and> F \<subseteq> t }"  
  3015   note `countably_compact s`
  3016   moreover have "\<forall>t\<in>C. open t" 
  3017     by (auto simp: C_def)
  3018   moreover
  3019   assume "\<not> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"
  3020   then have s: "\<And>x. x \<in> s \<Longrightarrow> \<exists>U. x\<in>U \<and> open U \<and> finite (U \<inter> t)" by metis
  3021   have "s \<subseteq> \<Union>C"
  3022     using `t \<subseteq> s`
  3023     unfolding C_def Union_image_eq
  3024     apply (safe dest!: s)
  3025     apply (rule_tac a="U \<inter> t" in UN_I)
  3026     apply (auto intro!: interiorI simp add: finite_subset)
  3027     done
  3028   moreover
  3029   from `countable t` have "countable C"
  3030     unfolding C_def by (auto intro: countable_Collect_finite_subset)
  3031   ultimately guess D by (rule countably_compactE)
  3032   then obtain E where E: "E \<subseteq> {F. finite F \<and> F \<subseteq> t }" "finite E" and
  3033     s: "s \<subseteq> (\<Union>F\<in>E. interior (F \<union> (- t)))"
  3034     by (metis (lifting) Union_image_eq finite_subset_image C_def)
  3035   from s `t \<subseteq> s` have "t \<subseteq> \<Union>E"
  3036     using interior_subset by blast
  3037   moreover have "finite (\<Union>E)"
  3038     using E by auto
  3039   ultimately show False using `infinite t` by (auto simp: finite_subset)
  3040 qed
  3041 
  3042 lemma countable_acc_point_imp_seq_compact:
  3043   fixes s :: "'a::first_countable_topology set"
  3044   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))"
  3045   shows "seq_compact s"
  3046 proof -
  3047   { fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
  3048     have "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  3049     proof (cases "finite (range f)")
  3050       case True
  3051       obtain l where "infinite {n. f n = f l}"
  3052         using pigeonhole_infinite[OF _ True] by auto
  3053       then obtain r where "subseq r" and fr: "\<forall>n. f (r n) = f l"
  3054         using infinite_enumerate by blast
  3055       hence "subseq r \<and> (f \<circ> r) ----> f l"
  3056         by (simp add: fr tendsto_const o_def)
  3057       with f show "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l"
  3058         by auto
  3059     next
  3060       case False
  3061       with f assms have "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" by auto
  3062       then obtain l where "l \<in> s" "\<forall>U. l\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" ..
  3063       from this(2) have "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  3064         using acc_point_range_imp_convergent_subsequence[of l f] by auto
  3065       with `l \<in> s` show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" ..
  3066     qed
  3067   }
  3068   thus ?thesis unfolding seq_compact_def by auto
  3069 qed
  3070 
  3071 lemma seq_compact_eq_countably_compact:
  3072   fixes U :: "'a :: first_countable_topology set"
  3073   shows "seq_compact U \<longleftrightarrow> countably_compact U"
  3074   using
  3075     countable_acc_point_imp_seq_compact
  3076     countably_compact_imp_acc_point
  3077     seq_compact_imp_countably_compact
  3078   by metis
  3079 
  3080 lemma seq_compact_eq_acc_point:
  3081   fixes s :: "'a :: first_countable_topology set"
  3082   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)))"
  3083   using
  3084     countable_acc_point_imp_seq_compact[of s]
  3085     countably_compact_imp_acc_point[of s]
  3086     seq_compact_imp_countably_compact[of s]
  3087   by metis
  3088 
  3089 lemma seq_compact_eq_compact:
  3090   fixes U :: "'a :: second_countable_topology set"
  3091   shows "seq_compact U \<longleftrightarrow> compact U"
  3092   using seq_compact_eq_countably_compact countably_compact_eq_compact by blast
  3093 
  3094 lemma bolzano_weierstrass_imp_seq_compact:
  3095   fixes s :: "'a::{t1_space, first_countable_topology} set"
  3096   shows "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> seq_compact s"
  3097   by (rule countable_acc_point_imp_seq_compact) (metis islimpt_eq_acc_point)
  3098 
  3099 subsubsection{* Total boundedness *}
  3100 
  3101 lemma cauchy_def:
  3102   "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N --> dist(s m)(s n) < e)"
  3103 unfolding Cauchy_def by blast
  3104 
  3105 fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where
  3106   "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"
  3107 declare helper_1.simps[simp del]
  3108 
  3109 lemma seq_compact_imp_totally_bounded:
  3110   assumes "seq_compact s"
  3111   shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k))"
  3112 proof(rule, rule, rule ccontr)
  3113   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)"
  3114   def x \<equiv> "helper_1 s e"
  3115   { fix n
  3116     have "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)"
  3117     proof(induct_tac rule:nat_less_induct)
  3118       fix n  def Q \<equiv> "(\<lambda>y. y \<in> s \<and> (\<forall>m<n. \<not> dist (x m) y < e))"
  3119       assume as:"\<forall>m<n. x m \<in> s \<and> (\<forall>ma<m. \<not> dist (x ma) (x m) < e)"
  3120       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
  3121       then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x ` {0..<n}. ball x e)" unfolding subset_eq by auto
  3122       have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]
  3123         apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto
  3124       thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto
  3125     qed }
  3126   hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+
  3127   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
  3128   from this(3) have "Cauchy (x \<circ> r)" using LIMSEQ_imp_Cauchy by auto
  3129   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
  3130   show False
  3131     using N[THEN spec[where x=N], THEN spec[where x="N+1"]]
  3132     using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]
  3133     using x[THEN spec[where x="r (N+1)"], THEN spec[where x="r (N)"]] by auto
  3134 qed
  3135 
  3136 subsubsection{* Heine-Borel theorem *}
  3137 
  3138 lemma seq_compact_imp_heine_borel:
  3139   fixes s :: "'a :: metric_space set"
  3140   assumes "seq_compact s" shows "compact s"
  3141 proof -
  3142   from seq_compact_imp_totally_bounded[OF `seq_compact s`]
  3143   guess f unfolding choice_iff' .. note f = this
  3144   def K \<equiv> "(\<lambda>(x, r). ball x r) ` ((\<Union>e \<in> \<rat> \<inter> {0 <..}. f e) \<times> \<rat>)"
  3145   have "countably_compact s"
  3146     using `seq_compact s` by (rule seq_compact_imp_countably_compact)
  3147   then show "compact s"
  3148   proof (rule countably_compact_imp_compact)
  3149     show "countable K"
  3150       unfolding K_def using f
  3151       by (auto intro: countable_finite countable_subset countable_rat
  3152                intro!: countable_image countable_SIGMA countable_UN)
  3153     show "\<forall>b\<in>K. open b" by (auto simp: K_def)
  3154   next
  3155     fix T x assume T: "open T" "x \<in> T" and x: "x \<in> s"
  3156     from openE[OF T] obtain e where "0 < e" "ball x e \<subseteq> T" by auto
  3157     then have "0 < e / 2" "ball x (e / 2) \<subseteq> T" by auto
  3158     from Rats_dense_in_real[OF `0 < e / 2`] obtain r where "r \<in> \<rat>" "0 < r" "r < e / 2" by auto
  3159     from f[rule_format, of r] `0 < r` `x \<in> s` obtain k where "k \<in> f r" "x \<in> ball k r"
  3160       unfolding Union_image_eq by auto
  3161     from `r \<in> \<rat>` `0 < r` `k \<in> f r` have "ball k r \<in> K" by (auto simp: K_def)
  3162     then show "\<exists>b\<in>K. x \<in> b \<and> b \<inter> s \<subseteq> T"
  3163     proof (rule bexI[rotated], safe)
  3164       fix y assume "y \<in> ball k r"
  3165       with `r < e / 2` `x \<in> ball k r` have "dist x y < e"
  3166         by (intro dist_double[where x = k and d=e]) (auto simp: dist_commute)
  3167       with `ball x e \<subseteq> T` show "y \<in> T" by auto
  3168     qed (rule `x \<in> ball k r`)
  3169   qed
  3170 qed
  3171 
  3172 lemma compact_eq_seq_compact_metric:
  3173   "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"
  3174   using compact_imp_seq_compact seq_compact_imp_heine_borel by blast
  3175 
  3176 lemma compact_def:
  3177   "compact (S :: 'a::metric_space set) \<longleftrightarrow>
  3178    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r. subseq r \<and> (f o r) ----> l))"
  3179   unfolding compact_eq_seq_compact_metric seq_compact_def by auto
  3180 
  3181 subsubsection {* Complete the chain of compactness variants *}
  3182 
  3183 lemma compact_eq_bolzano_weierstrass:
  3184   fixes s :: "'a::metric_space set"
  3185   shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")
  3186 proof
  3187   assume ?lhs thus ?rhs using heine_borel_imp_bolzano_weierstrass[of s] by auto
  3188 next
  3189   assume ?rhs thus ?lhs
  3190     unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)
  3191 qed
  3192 
  3193 lemma bolzano_weierstrass_imp_bounded:
  3194   "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> bounded s"
  3195   using compact_imp_bounded unfolding compact_eq_bolzano_weierstrass .
  3196 
  3197 text {*
  3198   A metric space (or topological vector space) is said to have the
  3199   Heine-Borel property if every closed and bounded subset is compact.
  3200 *}
  3201 
  3202 class heine_borel = metric_space +
  3203   assumes bounded_imp_convergent_subsequence:
  3204     "bounded s \<Longrightarrow> \<forall>n. f n \<in> s
  3205       \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  3206 
  3207 lemma bounded_closed_imp_seq_compact:
  3208   fixes s::"'a::heine_borel set"
  3209   assumes "bounded s" and "closed s" shows "seq_compact s"
  3210 proof (unfold seq_compact_def, clarify)
  3211   fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"
  3212   obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
  3213     using bounded_imp_convergent_subsequence [OF `bounded s` `\<forall>n. f n \<in> s`] by auto
  3214   from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp
  3215   have "l \<in> s" using `closed s` fr l
  3216     unfolding closed_sequential_limits by blast
  3217   show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  3218     using `l \<in> s` r l by blast
  3219 qed
  3220 
  3221 lemma compact_eq_bounded_closed:
  3222   fixes s :: "'a::heine_borel set"
  3223   shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")
  3224 proof
  3225   assume ?lhs thus ?rhs
  3226     using compact_imp_closed compact_imp_bounded by blast
  3227 next
  3228   assume ?rhs thus ?lhs
  3229     using bounded_closed_imp_seq_compact[of s] unfolding compact_eq_seq_compact_metric by auto
  3230 qed
  3231 
  3232 lemma lim_subseq:
  3233   "subseq r \<Longrightarrow> (s ---> l) sequentially \<Longrightarrow> ((s o r) ---> l) sequentially"
  3234 unfolding tendsto_def eventually_sequentially o_def
  3235 by (metis seq_suble le_trans)
  3236 
  3237 lemma num_Axiom: "EX! g. g 0 = e \<and> (\<forall>n. g (Suc n) = f n (g n))"
  3238   unfolding Ex1_def
  3239   apply (rule_tac x="nat_rec e f" in exI)
  3240   apply (rule conjI)+
  3241 apply (rule def_nat_rec_0, simp)
  3242 apply (rule allI, rule def_nat_rec_Suc, simp)
  3243 apply (rule allI, rule impI, rule ext)
  3244 apply (erule conjE)
  3245 apply (induct_tac x)
  3246 apply simp
  3247 apply (erule_tac x="n" in allE)
  3248 apply (simp)
  3249 done
  3250 
  3251 lemma convergent_bounded_increasing: fixes s ::"nat\<Rightarrow>real"
  3252   assumes "incseq s" and "\<forall>n. abs(s n) \<le> b"
  3253   shows "\<exists> l. \<forall>e::real>0. \<exists> N. \<forall>n \<ge> N.  abs(s n - l) < e"
  3254 proof-
  3255   have "isUb UNIV (range s) b" using assms(2) and abs_le_D1 unfolding isUb_def and setle_def by auto
  3256   then obtain t where t:"isLub UNIV (range s) t" using reals_complete[of "range s" ] by auto
  3257   { fix e::real assume "e>0" and as:"\<forall>N. \<exists>n\<ge>N. \<not> \<bar>s n - t\<bar> < e"
  3258     { fix n::nat
  3259       obtain N where "N\<ge>n" and n:"\<bar>s N - t\<bar> \<ge> e" using as[THEN spec[where x=n]] by auto
  3260       have "t \<ge> s N" using isLub_isUb[OF t, unfolded isUb_def setle_def] by auto
  3261       with n have "s N \<le> t - e" using `e>0` by auto
  3262       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  }
  3263     hence "isUb UNIV (range s) (t - e)" unfolding isUb_def and setle_def by auto
  3264     hence False using isLub_le_isUb[OF t, of "t - e"] and `e>0` by auto  }
  3265   thus ?thesis by blast
  3266 qed
  3267 
  3268 lemma convergent_bounded_monotone: fixes s::"nat \<Rightarrow> real"
  3269   assumes "\<forall>n. abs(s n) \<le> b" and "monoseq s"
  3270   shows "\<exists>l. \<forall>e::real>0. \<exists>N. \<forall>n\<ge>N. abs(s n - l) < e"
  3271   using convergent_bounded_increasing[of s b] assms using convergent_bounded_increasing[of "\<lambda>n. - s n" b]
  3272   unfolding monoseq_def incseq_def
  3273   apply auto unfolding minus_add_distrib[THEN sym, unfolded diff_minus[THEN sym]]
  3274   unfolding abs_minus_cancel by(rule_tac x="-l" in exI)auto
  3275 
  3276 (* TODO: merge this lemma with the ones above *)
  3277 lemma bounded_increasing_convergent: fixes s::"nat \<Rightarrow> real"
  3278   assumes "bounded {s n| n::nat. True}"  "\<forall>n. (s n) \<le>(s(Suc n))"
  3279   shows "\<exists>l. (s ---> l) sequentially"
  3280 proof-
  3281   obtain a where a:"\<forall>n. \<bar> (s n)\<bar> \<le>  a" using assms(1)[unfolded bounded_iff] by auto
  3282   { fix m::nat
  3283     have "\<And> n. n\<ge>m \<longrightarrow>  (s m) \<le> (s n)"
  3284       apply(induct_tac n) apply simp using assms(2) apply(erule_tac x="na" in allE)
  3285       apply(case_tac "m \<le> na") unfolding not_less_eq_eq by(auto simp add: not_less_eq_eq)  }
  3286   hence "\<forall>m n. m \<le> n \<longrightarrow> (s m) \<le> (s n)" by auto
  3287   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]
  3288     unfolding monoseq_def by auto
  3289   thus ?thesis unfolding LIMSEQ_def apply(rule_tac x="l" in exI)
  3290     unfolding dist_norm  by auto
  3291 qed
  3292 
  3293 lemma compact_real_lemma:
  3294   assumes "\<forall>n::nat. abs(s n) \<le> b"
  3295   shows "\<exists>(l::real) r. subseq r \<and> ((s \<circ> r) ---> l) sequentially"
  3296 proof-
  3297   obtain r where r:"subseq r" "monoseq (\<lambda>n. s (r n))"
  3298     using seq_monosub[of s] by auto
  3299   thus ?thesis using convergent_bounded_monotone[of "\<lambda>n. s (r n)" b] and assms
  3300     unfolding tendsto_iff dist_norm eventually_sequentially by auto
  3301 qed
  3302 
  3303 instance real :: heine_borel
  3304 proof
  3305   fix s :: "real set" and f :: "nat \<Rightarrow> real"
  3306   assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
  3307   then obtain b where b: "\<forall>n. abs (f n) \<le> b"
  3308     unfolding bounded_iff by auto
  3309   obtain l :: real and r :: "nat \<Rightarrow> nat" where
  3310     r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"
  3311     using compact_real_lemma [OF b] by auto
  3312   thus "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  3313     by auto
  3314 qed
  3315 
  3316 lemma compact_lemma:
  3317   fixes f :: "nat \<Rightarrow> 'a::euclidean_space"
  3318   assumes "bounded s" and "\<forall>n. f n \<in> s"
  3319   shows "\<forall>d\<subseteq>Basis. \<exists>l::'a. \<exists> r. subseq r \<and>
  3320         (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially)"
  3321 proof safe
  3322   fix d :: "'a set" assume d: "d \<subseteq> Basis" 
  3323   with finite_Basis have "finite d" by (blast intro: finite_subset)
  3324   from this d show "\<exists>l::'a. \<exists>r. subseq r \<and>
  3325       (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially)"
  3326   proof(induct d) case empty thus ?case unfolding subseq_def by auto
  3327   next case (insert k d) have k[intro]:"k\<in>Basis" using insert by auto
  3328     have s': "bounded ((\<lambda>x. x \<bullet> k) ` s)" using `bounded s`
  3329       by (auto intro!: bounded_linear_image bounded_linear_inner_left)
  3330     obtain l1::"'a" and r1 where r1:"subseq r1" and
  3331       lr1:"\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) \<bullet> i) (l1 \<bullet> i) < e) sequentially"
  3332       using insert(3) using insert(4) by auto
  3333     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
  3334     obtain l2 r2 where r2:"subseq r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) \<bullet> k) ---> l2) sequentially"
  3335       using bounded_imp_convergent_subsequence[OF s' f'] unfolding o_def by auto
  3336     def r \<equiv> "r1 \<circ> r2" have r:"subseq r"
  3337       using r1 and r2 unfolding r_def o_def subseq_def by auto
  3338     moreover
  3339     def l \<equiv> "(\<Sum>i\<in>Basis. (if i = k then l2 else l1\<bullet>i) *\<^sub>R i)::'a"
  3340     { fix e::real assume "e>0"
  3341       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
  3342       from lr2 `e>0` have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) \<bullet> k) l2 < e) sequentially" by (rule tendstoD)
  3343       from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) \<bullet> i) (l1 \<bullet> i) < e) sequentially"
  3344         by (rule eventually_subseq)
  3345       have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially"
  3346         using N1' N2 
  3347         by eventually_elim (insert insert.prems, auto simp: l_def r_def o_def)
  3348     }
  3349     ultimately show ?case by auto
  3350   qed
  3351 qed
  3352 
  3353 instance euclidean_space \<subseteq> heine_borel
  3354 proof
  3355   fix s :: "'a set" and f :: "nat \<Rightarrow> 'a"
  3356   assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
  3357   then obtain l::'a and r where r: "subseq r"
  3358     and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially"
  3359     using compact_lemma [OF s f] by blast
  3360   { fix e::real assume "e>0"
  3361     hence "0 < e / real_of_nat DIM('a)" by (auto intro!: divide_pos_pos DIM_positive)
  3362     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"
  3363       by simp
  3364     moreover
  3365     { fix n assume n: "\<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e / (real_of_nat DIM('a))"
  3366       have "dist (f (r n)) l \<le> (\<Sum>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i))"
  3367         apply(subst euclidean_dist_l2) using zero_le_dist by (rule setL2_le_setsum)
  3368       also have "\<dots> < (\<Sum>i\<in>(Basis::'a set). e / (real_of_nat DIM('a)))"
  3369         apply(rule setsum_strict_mono) using n by auto
  3370       finally have "dist (f (r n)) l < e" 
  3371         by auto
  3372     }
  3373     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
  3374       by (rule eventually_elim1)
  3375   }
  3376   hence *:"((f \<circ> r) ---> l) sequentially" unfolding o_def tendsto_iff by simp
  3377   with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" by auto
  3378 qed
  3379 
  3380 lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst ` s)"
  3381 unfolding bounded_def
  3382 apply clarify
  3383 apply (rule_tac x="a" in exI)
  3384 apply (rule_tac x="e" in exI)
  3385 apply clarsimp
  3386 apply (drule (1) bspec)
  3387 apply (simp add: dist_Pair_Pair)
  3388 apply (erule order_trans [OF real_sqrt_sum_squares_ge1])
  3389 done
  3390 
  3391 lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd ` s)"
  3392 unfolding bounded_def
  3393 apply clarify
  3394 apply (rule_tac x="b" in exI)
  3395 apply (rule_tac x="e" in exI)
  3396 apply clarsimp
  3397 apply (drule (1) bspec)
  3398 apply (simp add: dist_Pair_Pair)
  3399 apply (erule order_trans [OF real_sqrt_sum_squares_ge2])
  3400 done
  3401 
  3402 instance prod :: (heine_borel, heine_borel) heine_borel
  3403 proof
  3404   fix s :: "('a * 'b) set" and f :: "nat \<Rightarrow> 'a * 'b"
  3405   assume s: "bounded s" and f: "\<forall>n. f n \<in> s"
  3406   from s have s1: "bounded (fst ` s)" by (rule bounded_fst)
  3407   from f have f1: "\<forall>n. fst (f n) \<in> fst ` s" by simp
  3408   obtain l1 r1 where r1: "subseq r1"
  3409     and l1: "((\<lambda>n. fst (f (r1 n))) ---> l1) sequentially"
  3410     using bounded_imp_convergent_subsequence [OF s1 f1]
  3411     unfolding o_def by fast
  3412   from s have s2: "bounded (snd ` s)" by (rule bounded_snd)
  3413   from f have f2: "\<forall>n. snd (f (r1 n)) \<in> snd ` s" by simp
  3414   obtain l2 r2 where r2: "subseq r2"
  3415     and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) ---> l2) sequentially"
  3416     using bounded_imp_convergent_subsequence [OF s2 f2]
  3417     unfolding o_def by fast
  3418   have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) ---> l1) sequentially"
  3419     using lim_subseq [OF r2 l1] unfolding o_def .
  3420   have l: "((f \<circ> (r1 \<circ> r2)) ---> (l1, l2)) sequentially"
  3421     using tendsto_Pair [OF l1' l2] unfolding o_def by simp
  3422   have r: "subseq (r1 \<circ> r2)"
  3423     using r1 r2 unfolding subseq_def by simp
  3424   show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"
  3425     using l r by fast
  3426 qed
  3427 
  3428 subsubsection{* Completeness *}
  3429 
  3430 definition
  3431   complete :: "'a::metric_space set \<Rightarrow> bool" where
  3432   "complete s \<longleftrightarrow> (\<forall>f. (\<forall>n. f n \<in> s) \<and> Cauchy f
  3433                       --> (\<exists>l \<in> s. (f ---> l) sequentially))"
  3434 
  3435 lemma cauchy: "Cauchy s \<longleftrightarrow> (\<forall>e>0.\<exists> N::nat. \<forall>n\<ge>N. dist(s n)(s N) < e)" (is "?lhs = ?rhs")
  3436 proof-
  3437   { assume ?rhs
  3438     { fix e::real
  3439       assume "e>0"
  3440       with `?rhs` obtain N where N:"\<forall>n\<ge>N. dist (s n) (s N) < e/2"
  3441         by (erule_tac x="e/2" in allE) auto
  3442       { fix n m
  3443         assume nm:"N \<le> m \<and> N \<le> n"
  3444         hence "dist (s m) (s n) < e" using N
  3445           using dist_triangle_half_l[of "s m" "s N" "e" "s n"]
  3446           by blast
  3447       }
  3448       hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"
  3449         by blast
  3450     }
  3451     hence ?lhs
  3452       unfolding cauchy_def
  3453       by blast
  3454   }
  3455   thus ?thesis
  3456     unfolding cauchy_def
  3457     using dist_triangle_half_l
  3458     by blast
  3459 qed
  3460 
  3461 lemma cauchy_imp_bounded: assumes "Cauchy s" shows "bounded (range s)"
  3462 proof-
  3463   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
  3464   hence N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto
  3465   moreover
  3466   have "bounded (s ` {0..N})" using finite_imp_bounded[of "s ` {1..N}"] by auto
  3467   then obtain a where a:"\<forall>x\<in>s ` {0..N}. dist (s N) x \<le> a"
  3468     unfolding bounded_any_center [where a="s N"] by auto
  3469   ultimately show "?thesis"
  3470     unfolding bounded_any_center [where a="s N"]
  3471     apply(rule_tac x="max a 1" in exI) apply auto
  3472     apply(erule_tac x=y in allE) apply(erule_tac x=y in ballE) by auto
  3473 qed
  3474 
  3475 lemma seq_compact_imp_complete: assumes "seq_compact s" shows "complete s"
  3476 proof-
  3477   { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"
  3478     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
  3479 
  3480     note lr' = seq_suble [OF lr(2)]
  3481 
  3482     { fix e::real assume "e>0"
  3483       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
  3484       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
  3485       { fix n::nat assume n:"n \<ge> max N M"
  3486         have "dist ((f \<circ> r) n) l < e/2" using n M by auto
  3487         moreover have "r n \<ge> N" using lr'[of n] n by auto
  3488         hence "dist (f n) ((f \<circ> r) n) < e / 2" using N using n by auto
  3489         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)  }
  3490       hence "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast  }
  3491     hence "\<exists>l\<in>s. (f ---> l) sequentially" using `l\<in>s` unfolding LIMSEQ_def by auto  }
  3492   thus ?thesis unfolding complete_def by auto
  3493 qed
  3494 
  3495 instance heine_borel < complete_space
  3496 proof
  3497   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  3498   hence "bounded (range f)"
  3499     by (rule cauchy_imp_bounded)
  3500   hence "seq_compact (closure (range f))"
  3501     using bounded_closed_imp_seq_compact [of "closure (range f)"] by auto
  3502   hence "complete (closure (range f))"
  3503     by (rule seq_compact_imp_complete)
  3504   moreover have "\<forall>n. f n \<in> closure (range f)"
  3505     using closure_subset [of "range f"] by auto
  3506   ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"
  3507     using `Cauchy f` unfolding complete_def by auto
  3508   then show "convergent f"
  3509     unfolding convergent_def by auto
  3510 qed
  3511 
  3512 instance euclidean_space \<subseteq> banach ..
  3513 
  3514 lemma complete_univ: "complete (UNIV :: 'a::complete_space set)"
  3515 proof(simp add: complete_def, rule, rule)
  3516   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"
  3517   hence "convergent f" by (rule Cauchy_convergent)
  3518   thus "\<exists>l. f ----> l" unfolding convergent_def .  
  3519 qed
  3520 
  3521 lemma complete_imp_closed: assumes "complete s" shows "closed s"
  3522 proof -
  3523   { fix x assume "x islimpt s"
  3524     then obtain f where f: "\<forall>n. f n \<in> s - {x}" "(f ---> x) sequentially"
  3525       unfolding islimpt_sequential by auto
  3526     then obtain l where l: "l\<in>s" "(f ---> l) sequentially"
  3527       using `complete s`[unfolded complete_def] using LIMSEQ_imp_Cauchy[of f x] by auto
  3528     hence "x \<in> s"  using tendsto_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto
  3529   }
  3530   thus "closed s" unfolding closed_limpt by auto
  3531 qed
  3532 
  3533 lemma complete_eq_closed:
  3534   fixes s :: "'a::complete_space set"
  3535   shows "complete s \<longleftrightarrow> closed s" (is "?lhs = ?rhs")
  3536 proof
  3537   assume ?lhs thus ?rhs by (rule complete_imp_closed)
  3538 next
  3539   assume ?rhs
  3540   { fix f assume as:"\<forall>n::nat. f n \<in> s" "Cauchy f"
  3541     then obtain l where "(f ---> l) sequentially" using complete_univ[unfolded complete_def, THEN spec[where x=f]] by auto
  3542     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  }
  3543   thus ?lhs unfolding complete_def by auto
  3544 qed
  3545 
  3546 lemma convergent_eq_cauchy:
  3547   fixes s :: "nat \<Rightarrow> 'a::complete_space"
  3548   shows "(\<exists>l. (s ---> l) sequentially) \<longleftrightarrow> Cauchy s"
  3549   unfolding Cauchy_convergent_iff convergent_def ..
  3550 
  3551 lemma convergent_imp_bounded:
  3552   fixes s :: "nat \<Rightarrow> 'a::metric_space"
  3553   shows "(s ---> l) sequentially \<Longrightarrow> bounded (range s)"
  3554   by (intro cauchy_imp_bounded LIMSEQ_imp_Cauchy)
  3555 
  3556 lemma nat_approx_posE:
  3557   fixes e::real
  3558   assumes "0 < e"
  3559   obtains n::nat where "1 / (Suc n) < e"
  3560 proof atomize_elim
  3561   have " 1 / real (Suc (nat (ceiling (1/e)))) < 1 / (ceiling (1/e))"
  3562     by (rule divide_strict_left_mono) (auto intro!: mult_pos_pos simp: `0 < e`)
  3563   also have "1 / (ceiling (1/e)) \<le> 1 / (1/e)"
  3564     by (rule divide_left_mono) (auto intro!: divide_pos_pos simp: `0 < e`)
  3565   also have "\<dots> = e" by simp
  3566   finally show  "\<exists>n. 1 / real (Suc n) < e" ..
  3567 qed
  3568 
  3569 lemma compact_eq_totally_bounded:
  3570   "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k)))"
  3571 proof (safe intro!: seq_compact_imp_complete[unfolded  compact_eq_seq_compact_metric[symmetric]])
  3572   fix e::real
  3573   def f \<equiv> "(\<lambda>x::'a. ball x e) ` UNIV"
  3574   assume "0 < e" "compact s"
  3575   hence "(\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f \<longrightarrow> (\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')"
  3576     by (simp add: compact_eq_heine_borel)
  3577   moreover
  3578   have d0: "\<And>x::'a. dist x x < e" using `0 < e` by simp
  3579   hence "(\<forall>t\<in>f. open t) \<and> s \<subseteq> \<Union>f" by (auto simp: f_def intro!: d0)
  3580   ultimately have "(\<exists>f'\<subseteq>f. finite f' \<and> s \<subseteq> \<Union>f')" ..
  3581   then guess K .. note K = this
  3582   have "\<forall>K'\<in>K. \<exists>k. K' = ball k e" using K by (auto simp: f_def)
  3583   then obtain k where "\<And>K'. K' \<in> K \<Longrightarrow> K' = ball (k K') e" unfolding bchoice_iff by blast
  3584   thus "\<exists>k. finite k \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k" using K
  3585     by (intro exI[where x="k ` K"]) (auto simp: f_def)
  3586 next
  3587   assume assms: "complete s" "\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> \<Union>(\<lambda>x. ball x e) ` k"
  3588   show "compact s"
  3589   proof cases
  3590     assume "s = {}" thus "compact s" by (simp add: compact_def)
  3591   next
  3592     assume "s \<noteq> {}"
  3593     show ?thesis
  3594       unfolding compact_def
  3595     proof safe
  3596       fix f::"nat \<Rightarrow> _" assume "\<forall>n. f n \<in> s" hence f: "\<And>n. f n \<in> s" by simp
  3597       from assms have "\<forall>e. \<exists>k. e>0 \<longrightarrow> finite k \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` k))" by simp
  3598       then obtain K where
  3599         K: "\<And>e. e > 0 \<Longrightarrow> finite (K e) \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` (K e)))"
  3600         unfolding choice_iff by blast
  3601       {
  3602         fix e::real and f' have f': "\<And>n::nat. (f o f') n \<in> s" using f by auto
  3603         assume "e > 0"
  3604         from K[OF this] have K: "finite (K e)" "s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` (K e)))"
  3605           by simp_all
  3606         have "\<exists>k\<in>(K e). \<exists>r. subseq r \<and> (\<forall>i. (f o f' o r) i \<in> ball k e)"
  3607         proof (rule ccontr)
  3608           from K have "finite (K e)" "K e \<noteq> {}" "s \<subseteq> (\<Union>((\<lambda>x. ball x e) ` (K e)))"
  3609             using `s \<noteq> {}`
  3610             by auto
  3611           moreover
  3612           assume "\<not> (\<exists>k\<in>K e. \<exists>r. subseq r \<and> (\<forall>i. (f \<circ> f' o r) i \<in> ball k e))"
  3613           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
  3614           ultimately
  3615           show False using f'
  3616           proof (induct arbitrary: s f f' rule: finite_ne_induct)
  3617             case (singleton x)
  3618             have "\<exists>i. (f \<circ> f' o id) i \<notin> ball x e" by (rule singleton) (auto simp: subseq_def)
  3619             thus ?case using singleton by (auto simp: ball_def)
  3620           next
  3621             case (insert x A)
  3622             show ?case
  3623             proof cases
  3624               have inf_ms: "infinite ((f o f') -` s)" using insert by (simp add: vimage_def)
  3625               have "infinite ((f o f') -` \<Union>((\<lambda>x. ball x e) ` (insert x A)))"
  3626                 using insert by (intro infinite_super[OF _ inf_ms]) auto
  3627               also have "((f o f') -` \<Union>((\<lambda>x. ball x e) ` (insert x A))) =
  3628                 {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
  3629               finally have "infinite \<dots>" .
  3630               moreover assume "finite {m. (f o f') m \<in> ball x e}"
  3631               ultimately have inf: "infinite {m. (f o f') m \<in> \<Union>((\<lambda>x. ball x e) ` A)}" by blast
  3632               hence "A \<noteq> {}" by auto then obtain k where "k \<in> A" by auto
  3633               def r \<equiv> "enumerate {m. (f o f') m \<in> \<Union>((\<lambda>x. ball x e) ` A)}"
  3634               have r_mono: "\<And>n m. n < m \<Longrightarrow> r n < r m"
  3635                 using enumerate_mono[OF _ inf] by (simp add: r_def)
  3636               hence "subseq r" by (simp add: subseq_def)
  3637               have r_in_set: "\<And>n. r n \<in> {m. (f o f') m \<in> \<Union>((\<lambda>x. ball x e) ` A)}"
  3638                 using enumerate_in_set[OF inf] by (simp add: r_def)
  3639               show False
  3640               proof (rule insert)
  3641                 show "\<Union>(\<lambda>x. ball x e) ` A \<subseteq> \<Union>(\<lambda>x. ball x e) ` A" by simp
  3642                 fix k s assume "k \<in> A" "subseq s"
  3643                 thus "\<exists>i. (f o f' o r o s) i \<notin> ball k e" using `subseq r`
  3644                   by (subst (2) o_assoc[symmetric]) (intro insert(6) subseq_o, simp_all)
  3645               next
  3646                 fix n show "(f \<circ> f' o r) n \<in> \<Union>(\<lambda>x. ball x e) ` A" using r_in_set by auto
  3647               qed
  3648             next
  3649               assume inf: "infinite {m. (f o f') m \<in> ball x e}"
  3650               def r \<equiv> "enumerate {m. (f o f') m \<in> ball x e}"
  3651               have r_mono: "\<And>n m. n < m \<Longrightarrow> r n < r m"
  3652                 using enumerate_mono[OF _ inf] by (simp add: r_def)
  3653               hence "subseq r" by (simp add: subseq_def)
  3654               from insert(6)[OF insertI1 this] obtain i where "(f o f') (r i) \<notin> ball x e" by auto
  3655               moreover
  3656               have r_in_set: "\<And>n. r n \<in> {m. (f o f') m \<in> ball x e}"
  3657                 using enumerate_in_set[OF inf] by (simp add: r_def)
  3658               hence "(f o f') (r i) \<in> ball x e" by simp
  3659               ultimately show False by simp
  3660             qed
  3661           qed
  3662         qed
  3663       }
  3664       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
  3665       let ?e = "\<lambda>n. 1 / real (Suc n)"
  3666       let ?P = "\<lambda>n s. \<exists>k\<in>K (?e n). (\<forall>i. (f o s) i \<in> ball k (?e n))"
  3667       interpret subseqs ?P using ex by unfold_locales force
  3668       from `complete s` have limI: "\<And>f. (\<And>n. f n \<in> s) \<Longrightarrow> Cauchy f \<Longrightarrow> (\<exists>l\<in>s. f ----> l)"
  3669         by (simp add: complete_def)
  3670       have "\<exists>l\<in>s. (f o diagseq) ----> l"
  3671       proof (intro limI metric_CauchyI)
  3672         fix e::real assume "0 < e" hence "0 < e / 2" by auto
  3673         from nat_approx_posE[OF this] guess n . note n = this
  3674         show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist ((f \<circ> diagseq) m) ((f \<circ> diagseq) n) < e"
  3675         proof (rule exI[where x="Suc n"], safe)
  3676           fix m mm assume "Suc n \<le> m" "Suc n \<le> mm"
  3677           let ?e = "1 / real (Suc n)"
  3678           from reducer_reduces[of n] obtain k where
  3679             "k\<in>K ?e"  "\<And>i. (f o seqseq (Suc n)) i \<in> ball k ?e"
  3680             unfolding seqseq_reducer by auto
  3681           moreover
  3682           note diagseq_sub[OF `Suc n \<le> m`] diagseq_sub[OF `Suc n \<le> mm`]
  3683           ultimately have "{(f o diagseq) m, (f o diagseq) mm} \<subseteq> ball k ?e" by auto
  3684           also have "\<dots> \<subseteq> ball k (e / 2)" using n by (intro subset_ball) simp
  3685           finally
  3686           have "dist k ((f \<circ> diagseq) m) + dist k ((f \<circ> diagseq) mm) < e / 2 + e /2"
  3687             by (intro add_strict_mono) auto
  3688           hence "dist ((f \<circ> diagseq) m) k + dist ((f \<circ> diagseq) mm) k < e"
  3689             by (simp add: dist_commute)
  3690           moreover have "dist ((f \<circ> diagseq) m) ((f \<circ> diagseq) mm) \<le>
  3691             dist ((f \<circ> diagseq) m) k + dist ((f \<circ> diagseq) mm) k"
  3692             by (rule dist_triangle2)
  3693           ultimately show "dist ((f \<circ> diagseq) m) ((f \<circ> diagseq) mm) < e"
  3694             by simp
  3695         qed
  3696       next
  3697         fix n show "(f o diagseq) n \<in> s" using f by simp
  3698       qed
  3699       thus "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l" using subseq_diagseq by auto
  3700     qed
  3701   qed
  3702 qed
  3703 
  3704 lemma compact_cball[simp]:
  3705   fixes x :: "'a::heine_borel"
  3706   shows "compact(cball x e)"
  3707   using compact_eq_bounded_closed bounded_cball closed_cball
  3708   by blast
  3709 
  3710 lemma compact_frontier_bounded[intro]:
  3711   fixes s :: "'a::heine_borel set"
  3712   shows "bounded s ==> compact(frontier s)"
  3713   unfolding frontier_def
  3714   using compact_eq_bounded_closed
  3715   by blast
  3716 
  3717 lemma compact_frontier[intro]:
  3718   fixes s :: "'a::heine_borel set"
  3719   shows "compact s ==> compact (frontier s)"
  3720   using compact_eq_bounded_closed compact_frontier_bounded
  3721   by blast
  3722 
  3723 lemma frontier_subset_compact:
  3724   fixes s :: "'a::heine_borel set"
  3725   shows "compact s ==> frontier s \<subseteq> s"
  3726   using frontier_subset_closed compact_eq_bounded_closed
  3727   by blast
  3728 
  3729 subsection {* Bounded closed nest property (proof does not use Heine-Borel) *}
  3730 
  3731 lemma bounded_closed_nest:
  3732   assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"
  3733   "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"
  3734   shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"
  3735 proof-
  3736   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
  3737   from assms(4,1) have *:"seq_compact (s 0)" using bounded_closed_imp_seq_compact[of "s 0"] by auto
  3738 
  3739   then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"
  3740     unfolding seq_compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast
  3741 
  3742   { fix n::nat
  3743     { fix e::real assume "e>0"
  3744       with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding LIMSEQ_def by auto
  3745       hence "dist ((x \<circ> r) (max N n)) l < e" by auto
  3746       moreover
  3747       have "r (max N n) \<ge> n" using lr(2) using seq_suble[of r "max N n"] by auto
  3748       hence "(x \<circ> r) (max N n) \<in> s n"
  3749         using x apply(erule_tac x=n in allE)
  3750         using x apply(erule_tac x="r (max N n)" in allE)
  3751         using assms(3) apply(erule_tac x=n in allE) apply(erule_tac x="r (max N n)" in allE) by auto
  3752       ultimately have "\<exists>y\<in>s n. dist y l < e" by auto
  3753     }
  3754     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast
  3755   }
  3756   thus ?thesis by auto
  3757 qed
  3758 
  3759 text {* Decreasing case does not even need compactness, just completeness. *}
  3760 
  3761 lemma decreasing_closed_nest:
  3762   assumes "\<forall>n. closed(s n)"
  3763           "\<forall>n. (s n \<noteq> {})"
  3764           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
  3765           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y \<in> (s n). dist x y < e"
  3766   shows "\<exists>a::'a::complete_space. \<forall>n::nat. a \<in> s n"
  3767 proof-
  3768   have "\<forall>n. \<exists> x. x\<in>s n" using assms(2) by auto
  3769   hence "\<exists>t. \<forall>n. t n \<in> s n" using choice[of "\<lambda> n x. x \<in> s n"] by auto
  3770   then obtain t where t: "\<forall>n. t n \<in> s n" by auto
  3771   { fix e::real assume "e>0"
  3772     then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e" using assms(4) by auto
  3773     { fix m n ::nat assume "N \<le> m \<and> N \<le> n"
  3774       hence "t m \<in> s N" "t n \<in> s N" using assms(3) t unfolding  subset_eq t by blast+
  3775       hence "dist (t m) (t n) < e" using N by auto
  3776     }
  3777     hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e" by auto
  3778   }
  3779   hence  "Cauchy t" unfolding cauchy_def by auto
  3780   then obtain l where l:"(t ---> l) sequentially" using complete_univ unfolding complete_def by auto
  3781   { fix n::nat
  3782     { fix e::real assume "e>0"
  3783       then obtain N::nat where N:"\<forall>n\<ge>N. dist (t n) l < e" using l[unfolded LIMSEQ_def] by auto
  3784       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
  3785       hence "\<exists>y\<in>s n. dist y l < e" apply(rule_tac x="t (max n N)" in bexI) using N by auto
  3786     }
  3787     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by auto
  3788   }
  3789   then show ?thesis by auto
  3790 qed
  3791 
  3792 text {* Strengthen it to the intersection actually being a singleton. *}
  3793 
  3794 lemma decreasing_closed_nest_sing:
  3795   fixes s :: "nat \<Rightarrow> 'a::complete_space set"
  3796   assumes "\<forall>n. closed(s n)"
  3797           "\<forall>n. s n \<noteq> {}"
  3798           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"
  3799           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"
  3800   shows "\<exists>a. \<Inter>(range s) = {a}"
  3801 proof-
  3802   obtain a where a:"\<forall>n. a \<in> s n" using decreasing_closed_nest[of s] using assms by auto
  3803   { fix b assume b:"b \<in> \<Inter>(range s)"
  3804     { fix e::real assume "e>0"
  3805       hence "dist a b < e" using assms(4 )using b using a by blast
  3806     }
  3807     hence "dist a b = 0" by (metis dist_eq_0_iff dist_nz less_le)
  3808   }
  3809   with a have "\<Inter>(range s) = {a}" unfolding image_def by auto
  3810   thus ?thesis ..
  3811 qed
  3812 
  3813 text{* Cauchy-type criteria for uniform convergence. *}
  3814 
  3815 lemma uniformly_convergent_eq_cauchy: fixes s::"nat \<Rightarrow> 'b \<Rightarrow> 'a::heine_borel" shows
  3816  "(\<exists>l. \<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e) \<longleftrightarrow>
  3817   (\<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")
  3818 proof(rule)
  3819   assume ?lhs
  3820   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
  3821   { fix e::real assume "e>0"
  3822     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
  3823     { fix n m::nat and x::"'b" assume "N \<le> m \<and> N \<le> n \<and> P x"
  3824       hence "dist (s m x) (s n x) < e"
  3825         using N[THEN spec[where x=m], THEN spec[where x=x]]
  3826         using N[THEN spec[where x=n], THEN spec[where x=x]]
  3827         using dist_triangle_half_l[of "s m x" "l x" e "s n x"] by auto  }
  3828     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  }
  3829   thus ?rhs by auto
  3830 next
  3831   assume ?rhs
  3832   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
  3833   then obtain l where l:"\<forall>x. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l x) sequentially" unfolding convergent_eq_cauchy[THEN sym]
  3834     using choice[of "\<lambda>x l. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l) sequentially"] by auto
  3835   { fix e::real assume "e>0"
  3836     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"
  3837       using `?rhs`[THEN spec[where x="e/2"]] by auto
  3838     { fix x assume "P x"
  3839       then obtain M where M:"\<forall>n\<ge>M. dist (s n x) (l x) < e/2"
  3840         using l[THEN spec[where x=x], unfolded LIMSEQ_def] using `e>0` by(auto elim!: allE[where x="e/2"])
  3841       fix n::nat assume "n\<ge>N"
  3842       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]]
  3843         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)  }
  3844     hence "\<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist(s n x)(l x) < e" by auto }
  3845   thus ?lhs by auto
  3846 qed
  3847 
  3848 lemma uniformly_cauchy_imp_uniformly_convergent:
  3849   fixes s :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::heine_borel"
  3850   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"
  3851           "\<forall>x. P x --> (\<forall>e>0. \<exists>N. \<forall>n. N \<le> n --> dist(s n x)(l x) < e)"
  3852   shows "\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e"
  3853 proof-
  3854   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"
  3855     using assms(1) unfolding uniformly_convergent_eq_cauchy[THEN sym] by auto
  3856   moreover
  3857   { fix x assume "P x"
  3858     hence "l x = l' x" using tendsto_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]
  3859       using l and assms(2) unfolding LIMSEQ_def by blast  }
  3860   ultimately show ?thesis by auto
  3861 qed
  3862 
  3863 
  3864 subsection {* Continuity *}
  3865 
  3866 text {* Define continuity over a net to take in restrictions of the set. *}
  3867 
  3868 definition
  3869   continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"
  3870   where "continuous net f \<longleftrightarrow> (f ---> f(netlimit net)) net"
  3871 
  3872 lemma continuous_trivial_limit:
  3873  "trivial_limit net ==> continuous net f"
  3874   unfolding continuous_def tendsto_def trivial_limit_eq by auto
  3875 
  3876 lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f(x)) (at x within s)"
  3877   unfolding continuous_def
  3878   unfolding tendsto_def
  3879   using netlimit_within[of x s]
  3880   by (cases "trivial_limit (at x within s)") (auto simp add: trivial_limit_eventually)
  3881 
  3882 lemma continuous_at: "continuous (at x) f \<longleftrightarrow> (f ---> f(x)) (at x)"
  3883   using continuous_within [of x UNIV f] by simp
  3884 
  3885 lemma continuous_at_within:
  3886   assumes "continuous (at x) f"  shows "continuous (at x within s) f"
  3887   using assms unfolding continuous_at continuous_within
  3888   by (rule Lim_at_within)
  3889 
  3890 text{* Derive the epsilon-delta forms, which we often use as "definitions" *}
  3891 
  3892 lemma continuous_within_eps_delta:
  3893   "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)"
  3894   unfolding continuous_within and Lim_within
  3895   apply auto unfolding dist_nz[THEN sym] apply(auto del: allE elim!:allE) apply(rule_tac x=d in exI) by auto
  3896 
  3897 lemma continuous_at_eps_delta: "continuous (at x) f \<longleftrightarrow>  (\<forall>e>0. \<exists>d>0.
  3898                            \<forall>x'. dist x' x < d --> dist(f x')(f x) < e)"
  3899   using continuous_within_eps_delta [of x UNIV f] by simp
  3900 
  3901 text{* Versions in terms of open balls. *}
  3902 
  3903 lemma continuous_within_ball:
  3904  "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
  3905                             f ` (ball x d \<inter> s) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
  3906 proof
  3907   assume ?lhs
  3908   { fix e::real assume "e>0"
  3909     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"
  3910       using `?lhs`[unfolded continuous_within Lim_within] by auto
  3911     { fix y assume "y\<in>f ` (ball x d \<inter> s)"
  3912       hence "y \<in> ball (f x) e" using d(2) unfolding dist_nz[THEN sym]
  3913         apply (auto simp add: dist_commute) apply(erule_tac x=xa in ballE) apply auto using `e>0` by auto
  3914     }
  3915     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)  }
  3916   thus ?rhs by auto
  3917 next
  3918   assume ?rhs thus ?lhs unfolding continuous_within Lim_within ball_def subset_eq
  3919     apply (auto simp add: dist_commute) apply(erule_tac x=e in allE) by auto
  3920 qed
  3921 
  3922 lemma continuous_at_ball:
  3923   "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f ` (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")
  3924 proof
  3925   assume ?lhs thus ?rhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
  3926     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)
  3927     unfolding dist_nz[THEN sym] by auto
  3928 next
  3929   assume ?rhs thus ?lhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball
  3930     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)
  3931 qed
  3932 
  3933 text{* Define setwise continuity in terms of limits within the set. *}
  3934 
  3935 definition
  3936   continuous_on ::
  3937     "'a set \<Rightarrow> ('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"
  3938 where
  3939   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f ---> f x) (at x within s))"
  3940 
  3941 lemma continuous_on_topological:
  3942   "continuous_on s f \<longleftrightarrow>
  3943     (\<forall>x\<in>s. \<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>
  3944       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
  3945 unfolding continuous_on_def tendsto_def
  3946 unfolding Limits.eventually_within eventually_at_topological
  3947 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto
  3948 
  3949 lemma continuous_on_iff:
  3950   "continuous_on s f \<longleftrightarrow>
  3951     (\<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)"
  3952 unfolding continuous_on_def Lim_within
  3953 apply (intro ball_cong [OF refl] all_cong ex_cong)
  3954 apply (rename_tac y, case_tac "y = x", simp)
  3955 apply (simp add: dist_nz)
  3956 done
  3957 
  3958 definition
  3959   uniformly_continuous_on ::
  3960     "'a set \<Rightarrow> ('a::metric_space \<Rightarrow> 'b::metric_space) \<Rightarrow> bool"
  3961 where
  3962   "uniformly_continuous_on s f \<longleftrightarrow>
  3963     (\<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)"
  3964 
  3965 text{* Some simple consequential lemmas. *}
  3966 
  3967 lemma uniformly_continuous_imp_continuous:
  3968  " uniformly_continuous_on s f ==> continuous_on s f"
  3969   unfolding uniformly_continuous_on_def continuous_on_iff by blast
  3970 
  3971 lemma continuous_at_imp_continuous_within:
  3972  "continuous (at x) f ==> continuous (at x within s) f"
  3973   unfolding continuous_within continuous_at using Lim_at_within by auto
  3974 
  3975 lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f ---> l) net"
  3976 unfolding tendsto_def by (simp add: trivial_limit_eq)
  3977 
  3978 lemma continuous_at_imp_continuous_on:
  3979   assumes "\<forall>x\<in>s. continuous (at x) f"
  3980   shows "continuous_on s f"
  3981 unfolding continuous_on_def
  3982 proof
  3983   fix x assume "x \<in> s"
  3984   with assms have *: "(f ---> f (netlimit (at x))) (at x)"
  3985     unfolding continuous_def by simp
  3986   have "(f ---> f x) (at x)"
  3987   proof (cases "trivial_limit (at x)")
  3988     case True thus ?thesis
  3989       by (rule Lim_trivial_limit)
  3990   next
  3991     case False
  3992     hence 1: "netlimit (at x) = x"
  3993       using netlimit_within [of x UNIV] by simp
  3994     with * show ?thesis by simp
  3995   qed
  3996   thus "(f ---> f x) (at x within s)"
  3997     by (rule Lim_at_within)
  3998 qed
  3999 
  4000 lemma continuous_on_eq_continuous_within:
  4001   "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x within s) f)"
  4002 unfolding continuous_on_def continuous_def
  4003 apply (rule ball_cong [OF refl])
  4004 apply (case_tac "trivial_limit (at x within s)")
  4005 apply (simp add: Lim_trivial_limit)
  4006 apply (simp add: netlimit_within)
  4007 done
  4008 
  4009 lemmas continuous_on = continuous_on_def -- "legacy theorem name"
  4010 
  4011 lemma continuous_on_eq_continuous_at:
  4012   shows "open s ==> (continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x) f))"
  4013   by (auto simp add: continuous_on continuous_at Lim_within_open)
  4014 
  4015 lemma continuous_within_subset:
  4016  "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s
  4017              ==> continuous (at x within t) f"
  4018   unfolding continuous_within by(metis Lim_within_subset)
  4019 
  4020 lemma continuous_on_subset:
  4021   shows "continuous_on s f \<Longrightarrow> t \<subseteq> s ==> continuous_on t f"
  4022   unfolding continuous_on by (metis subset_eq Lim_within_subset)
  4023 
  4024 lemma continuous_on_interior:
  4025   shows "continuous_on s f \<Longrightarrow> x \<in> interior s \<Longrightarrow> continuous (at x) f"
  4026   by (erule interiorE, drule (1) continuous_on_subset,
  4027     simp add: continuous_on_eq_continuous_at)
  4028 
  4029 lemma continuous_on_eq:
  4030   "(\<forall>x \<in> s. f x = g x) \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on s g"
  4031   unfolding continuous_on_def tendsto_def Limits.eventually_within
  4032   by simp
  4033 
  4034 text {* Characterization of various kinds of continuity in terms of sequences. *}
  4035 
  4036 lemma continuous_within_sequentially:
  4037   fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  4038   shows "continuous (at a within s) f \<longleftrightarrow>
  4039                 (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x ---> a) sequentially
  4040                      --> ((f o x) ---> f a) sequentially)" (is "?lhs = ?rhs")
  4041 proof
  4042   assume ?lhs
  4043   { 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"
  4044     fix T::"'b set" assume "open T" and "f a \<in> T"
  4045     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"
  4046       unfolding continuous_within tendsto_def eventually_within by auto
  4047     have "eventually (\<lambda>n. dist (x n) a < d) sequentially"
  4048       using x(2) `d>0` by simp
  4049     hence "eventually (\<lambda>n. (f \<circ> x) n \<in> T) sequentially"
  4050     proof eventually_elim
  4051       case (elim n) thus ?case
  4052         using d x(1) `f a \<in> T` unfolding dist_nz[THEN sym] by auto
  4053     qed
  4054   }
  4055   thus ?rhs unfolding tendsto_iff unfolding tendsto_def by simp
  4056 next
  4057   assume ?rhs thus ?lhs
  4058     unfolding continuous_within tendsto_def [where l="f a"]
  4059     by (simp add: sequentially_imp_eventually_within)
  4060 qed
  4061 
  4062 lemma continuous_at_sequentially:
  4063   fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  4064   shows "continuous (at a) f \<longleftrightarrow> (\<forall>x. (x ---> a) sequentially
  4065                   --> ((f o x) ---> f a) sequentially)"
  4066   using continuous_within_sequentially[of a UNIV f] by simp
  4067 
  4068 lemma continuous_on_sequentially:
  4069   fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  4070   shows "continuous_on s f \<longleftrightarrow>
  4071     (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x ---> a) sequentially
  4072                     --> ((f o x) ---> f(a)) sequentially)" (is "?lhs = ?rhs")
  4073 proof
  4074   assume ?rhs thus ?lhs using continuous_within_sequentially[of _ s f] unfolding continuous_on_eq_continuous_within by auto
  4075 next
  4076   assume ?lhs thus ?rhs unfolding continuous_on_eq_continuous_within using continuous_within_sequentially[of _ s f] by auto
  4077 qed
  4078 
  4079 lemma uniformly_continuous_on_sequentially:
  4080   "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>
  4081                     ((\<lambda>n. dist (x n) (y n)) ---> 0) sequentially
  4082                     \<longrightarrow> ((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially)" (is "?lhs = ?rhs")
  4083 proof
  4084   assume ?lhs
  4085   { 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"
  4086     { fix e::real assume "e>0"
  4087       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"
  4088         using `?lhs`[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto
  4089       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
  4090       { fix n assume "n\<ge>N"
  4091         hence "dist (f (x n)) (f (y n)) < e"
  4092           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
  4093           unfolding dist_commute by simp  }
  4094       hence "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"  by auto  }
  4095     hence "((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially" unfolding LIMSEQ_def and dist_real_def by auto  }
  4096   thus ?rhs by auto
  4097 next
  4098   assume ?rhs
  4099   { assume "\<not> ?lhs"
  4100     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
  4101     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"
  4102       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
  4103       by (auto simp add: dist_commute)
  4104     def x \<equiv> "\<lambda>n::nat. fst (fa (inverse (real n + 1)))"
  4105     def y \<equiv> "\<lambda>n::nat. snd (fa (inverse (real n + 1)))"
  4106     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"
  4107       unfolding x_def and y_def using fa by auto
  4108     { fix e::real assume "e>0"
  4109       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
  4110       { fix n::nat assume "n\<ge>N"
  4111         hence "inverse (real n + 1) < inverse (real N)" using real_of_nat_ge_zero and `N\<noteq>0` by auto
  4112         also have "\<dots> < e" using N by auto
  4113         finally have "inverse (real n + 1) < e" by auto
  4114         hence "dist (x n) (y n) < e" using xy0[THEN spec[where x=n]] by auto  }
  4115       hence "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto  }
  4116     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
  4117     hence False using fxy and `e>0` by auto  }
  4118   thus ?lhs unfolding uniformly_continuous_on_def by blast
  4119 qed
  4120 
  4121 text{* The usual transformation theorems. *}
  4122 
  4123 lemma continuous_transform_within:
  4124   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  4125   assumes "0 < d" "x \<in> s" "\<forall>x' \<in> s. dist x' x < d --> f x' = g x'"
  4126           "continuous (at x within s) f"
  4127   shows "continuous (at x within s) g"
  4128 unfolding continuous_within
  4129 proof (rule Lim_transform_within)
  4130   show "0 < d" by fact
  4131   show "\<forall>x'\<in>s. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"
  4132     using assms(3) by auto
  4133   have "f x = g x"
  4134     using assms(1,2,3) by auto
  4135   thus "(f ---> g x) (at x within s)"
  4136     using assms(4) unfolding continuous_within by simp
  4137 qed
  4138 
  4139 lemma continuous_transform_at:
  4140   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"
  4141   assumes "0 < d" "\<forall>x'. dist x' x < d --> f x' = g x'"
  4142           "continuous (at x) f"
  4143   shows "continuous (at x) g"
  4144   using continuous_transform_within [of d x UNIV f g] assms by simp
  4145 
  4146 subsubsection {* Structural rules for pointwise continuity *}
  4147 
  4148 lemma continuous_within_id: "continuous (at a within s) (\<lambda>x. x)"
  4149   unfolding continuous_within by (rule tendsto_ident_at_within)
  4150 
  4151 lemma continuous_at_id: "continuous (at a) (\<lambda>x. x)"
  4152   unfolding continuous_at by (rule tendsto_ident_at)
  4153 
  4154 lemma continuous_const: "continuous F (\<lambda>x. c)"
  4155   unfolding continuous_def by (rule tendsto_const)
  4156 
  4157 lemma continuous_dist:
  4158   assumes "continuous F f" and "continuous F g"
  4159   shows "continuous F (\<lambda>x. dist (f x) (g x))"
  4160   using assms unfolding continuous_def by (rule tendsto_dist)
  4161 
  4162 lemma continuous_infdist:
  4163   assumes "continuous F f"
  4164   shows "continuous F (\<lambda>x. infdist (f x) A)"
  4165   using assms unfolding continuous_def by (rule tendsto_infdist)
  4166 
  4167 lemma continuous_norm:
  4168   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. norm (f x))"
  4169   unfolding continuous_def by (rule tendsto_norm)
  4170 
  4171 lemma continuous_infnorm:
  4172   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. infnorm (f x))"
  4173   unfolding continuous_def by (rule tendsto_infnorm)
  4174 
  4175 lemma continuous_add:
  4176   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  4177   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x + g x)"
  4178   unfolding continuous_def by (rule tendsto_add)
  4179 
  4180 lemma continuous_minus:
  4181   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  4182   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. - f x)"
  4183   unfolding continuous_def by (rule tendsto_minus)
  4184 
  4185 lemma continuous_diff:
  4186   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  4187   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x - g x)"
  4188   unfolding continuous_def by (rule tendsto_diff)
  4189 
  4190 lemma continuous_scaleR:
  4191   fixes g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  4192   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x *\<^sub>R g x)"
  4193   unfolding continuous_def by (rule tendsto_scaleR)
  4194 
  4195 lemma continuous_mult:
  4196   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_algebra"
  4197   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x * g x)"
  4198   unfolding continuous_def by (rule tendsto_mult)
  4199 
  4200 lemma continuous_inner:
  4201   assumes "continuous F f" and "continuous F g"
  4202   shows "continuous F (\<lambda>x. inner (f x) (g x))"
  4203   using assms unfolding continuous_def by (rule tendsto_inner)
  4204 
  4205 lemma continuous_inverse:
  4206   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"
  4207   assumes "continuous F f" and "f (netlimit F) \<noteq> 0"
  4208   shows "continuous F (\<lambda>x. inverse (f x))"
  4209   using assms unfolding continuous_def by (rule tendsto_inverse)
  4210 
  4211 lemma continuous_at_within_inverse:
  4212   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"
  4213   assumes "continuous (at a within s) f" and "f a \<noteq> 0"
  4214   shows "continuous (at a within s) (\<lambda>x. inverse (f x))"
  4215   using assms unfolding continuous_within by (rule tendsto_inverse)
  4216 
  4217 lemma continuous_at_inverse:
  4218   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"
  4219   assumes "continuous (at a) f" and "f a \<noteq> 0"
  4220   shows "continuous (at a) (\<lambda>x. inverse (f x))"
  4221   using assms unfolding continuous_at by (rule tendsto_inverse)
  4222 
  4223 lemmas continuous_intros = continuous_at_id continuous_within_id
  4224   continuous_const continuous_dist continuous_norm continuous_infnorm
  4225   continuous_add continuous_minus continuous_diff continuous_scaleR continuous_mult
  4226   continuous_inner continuous_at_inverse continuous_at_within_inverse
  4227 
  4228 subsubsection {* Structural rules for setwise continuity *}
  4229 
  4230 lemma continuous_on_id: "continuous_on s (\<lambda>x. x)"
  4231   unfolding continuous_on_def by (fast intro: tendsto_ident_at_within)
  4232 
  4233 lemma continuous_on_const: "continuous_on s (\<lambda>x. c)"
  4234   unfolding continuous_on_def by (auto intro: tendsto_intros)
  4235 
  4236 lemma continuous_on_norm:
  4237   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. norm (f x))"
  4238   unfolding continuous_on_def by (fast intro: tendsto_norm)
  4239 
  4240 lemma continuous_on_infnorm:
  4241   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. infnorm (f x))"
  4242   unfolding continuous_on by (fast intro: tendsto_infnorm)
  4243 
  4244 lemma continuous_on_minus:
  4245   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  4246   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. - f x)"
  4247   unfolding continuous_on_def by (auto intro: tendsto_intros)
  4248 
  4249 lemma continuous_on_add:
  4250   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  4251   shows "continuous_on s f \<Longrightarrow> continuous_on s g
  4252            \<Longrightarrow> continuous_on s (\<lambda>x. f x + g x)"
  4253   unfolding continuous_on_def by (auto intro: tendsto_intros)
  4254 
  4255 lemma continuous_on_diff:
  4256   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  4257   shows "continuous_on s f \<Longrightarrow> continuous_on s g
  4258            \<Longrightarrow> continuous_on s (\<lambda>x. f x - g x)"
  4259   unfolding continuous_on_def by (auto intro: tendsto_intros)
  4260 
  4261 lemma (in bounded_linear) continuous_on:
  4262   "continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. f (g x))"
  4263   unfolding continuous_on_def by (fast intro: tendsto)
  4264 
  4265 lemma (in bounded_bilinear) continuous_on:
  4266   "\<lbrakk>continuous_on s f; continuous_on s g\<rbrakk> \<Longrightarrow> continuous_on s (\<lambda>x. f x ** g x)"
  4267   unfolding continuous_on_def by (fast intro: tendsto)
  4268 
  4269 lemma continuous_on_scaleR:
  4270   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  4271   assumes "continuous_on s f" and "continuous_on s g"
  4272   shows "continuous_on s (\<lambda>x. f x *\<^sub>R g x)"
  4273   using bounded_bilinear_scaleR assms
  4274   by (rule bounded_bilinear.continuous_on)
  4275 
  4276 lemma continuous_on_mult:
  4277   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra"
  4278   assumes "continuous_on s f" and "continuous_on s g"
  4279   shows "continuous_on s (\<lambda>x. f x * g x)"
  4280   using bounded_bilinear_mult assms
  4281   by (rule bounded_bilinear.continuous_on)
  4282 
  4283 lemma continuous_on_inner:
  4284   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_inner"
  4285   assumes "continuous_on s f" and "continuous_on s g"
  4286   shows "continuous_on s (\<lambda>x. inner (f x) (g x))"
  4287   using bounded_bilinear_inner assms
  4288   by (rule bounded_bilinear.continuous_on)
  4289 
  4290 lemma continuous_on_inverse:
  4291   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_div_algebra"
  4292   assumes "continuous_on s f" and "\<forall>x\<in>s. f x \<noteq> 0"
  4293   shows "continuous_on s (\<lambda>x. inverse (f x))"
  4294   using assms unfolding continuous_on by (fast intro: tendsto_inverse)
  4295 
  4296 subsubsection {* Structural rules for uniform continuity *}
  4297 
  4298 lemma uniformly_continuous_on_id:
  4299   shows "uniformly_continuous_on s (\<lambda>x. x)"
  4300   unfolding uniformly_continuous_on_def by auto
  4301 
  4302 lemma uniformly_continuous_on_const:
  4303   shows "uniformly_continuous_on s (\<lambda>x. c)"
  4304   unfolding uniformly_continuous_on_def by simp
  4305 
  4306 lemma uniformly_continuous_on_dist:
  4307   fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  4308   assumes "uniformly_continuous_on s f"
  4309   assumes "uniformly_continuous_on s g"
  4310   shows "uniformly_continuous_on s (\<lambda>x. dist (f x) (g x))"
  4311 proof -
  4312   { fix a b c d :: 'b have "\<bar>dist a b - dist c d\<bar> \<le> dist a c + dist b d"
  4313       using dist_triangle2 [of a b c] dist_triangle2 [of b c d]
  4314       using dist_triangle3 [of c d a] dist_triangle [of a d b]
  4315       by arith
  4316   } note le = this
  4317   { fix x y
  4318     assume f: "(\<lambda>n. dist (f (x n)) (f (y n))) ----> 0"
  4319     assume g: "(\<lambda>n. dist (g (x n)) (g (y n))) ----> 0"
  4320     have "(\<lambda>n. \<bar>dist (f (x n)) (g (x n)) - dist (f (y n)) (g (y n))\<bar>) ----> 0"
  4321       by (rule Lim_transform_bound [OF _ tendsto_add_zero [OF f g]],
  4322         simp add: le)
  4323   }
  4324   thus ?thesis using assms unfolding uniformly_continuous_on_sequentially
  4325     unfolding dist_real_def by simp
  4326 qed
  4327 
  4328 lemma uniformly_continuous_on_norm:
  4329   assumes "uniformly_continuous_on s f"
  4330   shows "uniformly_continuous_on s (\<lambda>x. norm (f x))"
  4331   unfolding norm_conv_dist using assms
  4332   by (intro uniformly_continuous_on_dist uniformly_continuous_on_const)
  4333 
  4334 lemma (in bounded_linear) uniformly_continuous_on:
  4335   assumes "uniformly_continuous_on s g"
  4336   shows "uniformly_continuous_on s (\<lambda>x. f (g x))"
  4337   using assms unfolding uniformly_continuous_on_sequentially
  4338   unfolding dist_norm tendsto_norm_zero_iff diff[symmetric]
  4339   by (auto intro: tendsto_zero)
  4340 
  4341 lemma uniformly_continuous_on_cmul:
  4342   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  4343   assumes "uniformly_continuous_on s f"
  4344   shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"
  4345   using bounded_linear_scaleR_right assms
  4346   by (rule bounded_linear.uniformly_continuous_on)
  4347 
  4348 lemma dist_minus:
  4349   fixes x y :: "'a::real_normed_vector"
  4350   shows "dist (- x) (- y) = dist x y"
  4351   unfolding dist_norm minus_diff_minus norm_minus_cancel ..
  4352 
  4353 lemma uniformly_continuous_on_minus:
  4354   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  4355   shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. - f x)"
  4356   unfolding uniformly_continuous_on_def dist_minus .
  4357 
  4358 lemma uniformly_continuous_on_add:
  4359   fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  4360   assumes "uniformly_continuous_on s f"
  4361   assumes "uniformly_continuous_on s g"
  4362   shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"
  4363   using assms unfolding uniformly_continuous_on_sequentially
  4364   unfolding dist_norm tendsto_norm_zero_iff add_diff_add
  4365   by (auto intro: tendsto_add_zero)
  4366 
  4367 lemma uniformly_continuous_on_diff:
  4368   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  4369   assumes "uniformly_continuous_on s f" and "uniformly_continuous_on s g"
  4370   shows "uniformly_continuous_on s (\<lambda>x. f x - g x)"
  4371   unfolding ab_diff_minus using assms
  4372   by (intro uniformly_continuous_on_add uniformly_continuous_on_minus)
  4373 
  4374 text{* Continuity of all kinds is preserved under composition. *}
  4375 
  4376 lemma continuous_within_topological:
  4377   "continuous (at x within s) f \<longleftrightarrow>
  4378     (\<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>
  4379       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
  4380 unfolding continuous_within
  4381 unfolding tendsto_def Limits.eventually_within eventually_at_topological
  4382 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto
  4383 
  4384 lemma continuous_within_compose:
  4385   assumes "continuous (at x within s) f"
  4386   assumes "continuous (at (f x) within f ` s) g"
  4387   shows "continuous (at x within s) (g o f)"
  4388 using assms unfolding continuous_within_topological by simp metis
  4389 
  4390 lemma continuous_at_compose:
  4391   assumes "continuous (at x) f" and "continuous (at (f x)) g"
  4392   shows "continuous (at x) (g o f)"
  4393 proof-
  4394   have "continuous (at (f x) within range f) g" using assms(2)
  4395     using continuous_within_subset[of "f x" UNIV g "range f"] by simp
  4396   thus ?thesis using assms(1)
  4397     using continuous_within_compose[of x UNIV f g] by simp
  4398 qed
  4399 
  4400 lemma continuous_on_compose:
  4401   "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g o f)"
  4402   unfolding continuous_on_topological by simp metis
  4403 
  4404 lemma uniformly_continuous_on_compose:
  4405   assumes "uniformly_continuous_on s f"  "uniformly_continuous_on (f ` s) g"
  4406   shows "uniformly_continuous_on s (g o f)"
  4407 proof-
  4408   { fix e::real assume "e>0"
  4409     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
  4410     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
  4411     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  }
  4412   thus ?thesis using assms unfolding uniformly_continuous_on_def by auto
  4413 qed
  4414 
  4415 lemmas continuous_on_intros = continuous_on_id continuous_on_const
  4416   continuous_on_compose continuous_on_norm continuous_on_infnorm
  4417   continuous_on_add continuous_on_minus continuous_on_diff
  4418   continuous_on_scaleR continuous_on_mult continuous_on_inverse
  4419   continuous_on_inner
  4420   uniformly_continuous_on_id uniformly_continuous_on_const
  4421   uniformly_continuous_on_dist uniformly_continuous_on_norm
  4422   uniformly_continuous_on_compose uniformly_continuous_on_add
  4423   uniformly_continuous_on_minus uniformly_continuous_on_diff
  4424   uniformly_continuous_on_cmul
  4425 
  4426 text{* Continuity in terms of open preimages. *}
  4427 
  4428 lemma continuous_at_open:
  4429   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)))"
  4430 unfolding continuous_within_topological [of x UNIV f, unfolded within_UNIV]
  4431 unfolding imp_conjL by (intro all_cong imp_cong ex_cong conj_cong refl) auto
  4432 
  4433 lemma continuous_on_open:
  4434   shows "continuous_on s f \<longleftrightarrow>
  4435         (\<forall>t. openin (subtopology euclidean (f ` s)) t
  4436             --> openin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")
  4437 proof (safe)
  4438   fix t :: "'b set"
  4439   assume 1: "continuous_on s f"
  4440   assume 2: "openin (subtopology euclidean (f ` s)) t"
  4441   from 2 obtain B where B: "open B" and t: "t = f ` s \<inter> B"
  4442     unfolding openin_open by auto
  4443   def U == "\<Union>{A. open A \<and> (\<forall>x\<in>s. x \<in> A \<longrightarrow> f x \<in> B)}"
  4444   have "open U" unfolding U_def by (simp add: open_Union)
  4445   moreover have "\<forall>x\<in>s. x \<in> U \<longleftrightarrow> f x \<in> t"
  4446   proof (intro ballI iffI)
  4447     fix x assume "x \<in> s" and "x \<in> U" thus "f x \<in> t"
  4448       unfolding U_def t by auto
  4449   next
  4450     fix x assume "x \<in> s" and "f x \<in> t"
  4451     hence "x \<in> s" and "f x \<in> B"
  4452       unfolding t by auto
  4453     with 1 B obtain A where "open A" "x \<in> A" "\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B"
  4454       unfolding t continuous_on_topological by metis
  4455     then show "x \<in> U"
  4456       unfolding U_def by auto
  4457   qed
  4458   ultimately have "open U \<and> {x \<in> s. f x \<in> t} = s \<inter> U" by auto
  4459   then show "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
  4460     unfolding openin_open by fast
  4461 next
  4462   assume "?rhs" show "continuous_on s f"
  4463   unfolding continuous_on_topological
  4464   proof (clarify)
  4465     fix x and B assume "x \<in> s" and "open B" and "f x \<in> B"
  4466     have "openin (subtopology euclidean (f ` s)) (f ` s \<inter> B)"
  4467       unfolding openin_open using `open B` by auto
  4468     then have "openin (subtopology euclidean s) {x \<in> s. f x \<in> f ` s \<inter> B}"
  4469       using `?rhs` by fast
  4470     then show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"
  4471       unfolding openin_open using `x \<in> s` and `f x \<in> B` by auto
  4472   qed
  4473 qed
  4474 
  4475 text {* Similarly in terms of closed sets. *}
  4476 
  4477 lemma continuous_on_closed:
  4478   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")
  4479 proof
  4480   assume ?lhs
  4481   { fix t
  4482     have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
  4483     have **:"f ` s - (f ` s - (f ` s - t)) = f ` s - t" by auto
  4484     assume as:"closedin (subtopology euclidean (f ` s)) t"
  4485     hence "closedin (subtopology euclidean (f ` s)) (f ` s - (f ` s - t))" unfolding closedin_def topspace_euclidean_subtopology unfolding ** by auto
  4486     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"]]
  4487       unfolding openin_closedin_eq topspace_euclidean_subtopology unfolding * by auto  }
  4488   thus ?rhs by auto
  4489 next
  4490   assume ?rhs
  4491   { fix t
  4492     have *:"s - {x \<in> s. f x \<in> f ` s - t} = {x \<in> s. f x \<in> t}" by auto
  4493     assume as:"openin (subtopology euclidean (f ` s)) t"
  4494     hence "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using `?rhs`[THEN spec[where x="(f ` s) - t"]]
  4495       unfolding openin_closedin_eq topspace_euclidean_subtopology *[THEN sym] closedin_subtopology by auto }
  4496   thus ?lhs unfolding continuous_on_open by auto
  4497 qed
  4498 
  4499 text {* Half-global and completely global cases. *}
  4500 
  4501 lemma continuous_open_in_preimage:
  4502   assumes "continuous_on s f"  "open t"
  4503   shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
  4504 proof-
  4505   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
  4506   have "openin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
  4507     using openin_open_Int[of t "f ` s", OF assms(2)] unfolding openin_open by auto
  4508   thus ?thesis using assms(1)[unfolded continuous_on_open, THEN spec[where x="t \<inter> f ` s"]] using * by auto
  4509 qed
  4510 
  4511 lemma continuous_closed_in_preimage:
  4512   assumes "continuous_on s f"  "closed t"
  4513   shows "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}"
  4514 proof-
  4515   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
  4516   have "closedin (subtopology euclidean (f ` s)) (t \<inter> f ` s)"
  4517     using closedin_closed_Int[of t "f ` s", OF assms(2)] unfolding Int_commute by auto
  4518   thus ?thesis
  4519     using assms(1)[unfolded continuous_on_closed, THEN spec[where x="t \<inter> f ` s"]] using * by auto
  4520 qed
  4521 
  4522 lemma continuous_open_preimage:
  4523   assumes "continuous_on s f" "open s" "open t"
  4524   shows "open {x \<in> s. f x \<in> t}"
  4525 proof-
  4526   obtain T where T: "open T" "{x \<in> s. f x \<in> t} = s \<inter> T"
  4527     using continuous_open_in_preimage[OF assms(1,3)] unfolding openin_open by auto
  4528   thus ?thesis using open_Int[of s T, OF assms(2)] by auto
  4529 qed
  4530 
  4531 lemma continuous_closed_preimage:
  4532   assumes "continuous_on s f" "closed s" "closed t"
  4533   shows "closed {x \<in> s. f x \<in> t}"
  4534 proof-
  4535   obtain T where T: "closed T" "{x \<in> s. f x \<in> t} = s \<inter> T"
  4536     using continuous_closed_in_preimage[OF assms(1,3)] unfolding closedin_closed by auto
  4537   thus ?thesis using closed_Int[of s T, OF assms(2)] by auto
  4538 qed
  4539 
  4540 lemma continuous_open_preimage_univ:
  4541   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open {x. f x \<in> s}"
  4542   using continuous_open_preimage[of UNIV f s] open_UNIV continuous_at_imp_continuous_on by auto
  4543 
  4544 lemma continuous_closed_preimage_univ:
  4545   shows "(\<forall>x. continuous (at x) f) \<Longrightarrow> closed s ==> closed {x. f x \<in> s}"
  4546   using continuous_closed_preimage[of UNIV f s] closed_UNIV continuous_at_imp_continuous_on by auto
  4547 
  4548 lemma continuous_open_vimage:
  4549   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open (f -` s)"
  4550   unfolding vimage_def by (rule continuous_open_preimage_univ)
  4551 
  4552 lemma continuous_closed_vimage:
  4553   shows "\<forall>x. continuous (at x) f \<Longrightarrow> closed s \<Longrightarrow> closed (f -` s)"
  4554   unfolding vimage_def by (rule continuous_closed_preimage_univ)
  4555 
  4556 lemma interior_image_subset:
  4557   assumes "\<forall>x. continuous (at x) f" "inj f"
  4558   shows "interior (f ` s) \<subseteq> f ` (interior s)"
  4559 proof
  4560   fix x assume "x \<in> interior (f ` s)"
  4561   then obtain T where as: "open T" "x \<in> T" "T \<subseteq> f ` s" ..
  4562   hence "x \<in> f ` s" by auto
  4563   then obtain y where y: "y \<in> s" "x = f y" by auto
  4564   have "open (vimage f T)"
  4565     using assms(1) `open T` by (rule continuous_open_vimage)
  4566   moreover have "y \<in> vimage f T"
  4567     using `x = f y` `x \<in> T` by simp
  4568   moreover have "vimage f T \<subseteq> s"
  4569     using `T \<subseteq> image f s` `inj f` unfolding inj_on_def subset_eq by auto
  4570   ultimately have "y \<in> interior s" ..
  4571   with `x = f y` show "x \<in> f ` interior s" ..
  4572 qed
  4573 
  4574 text {* Equality of continuous functions on closure and related results. *}
  4575 
  4576 lemma continuous_closed_in_preimage_constant:
  4577   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  4578   shows "continuous_on s f ==> closedin (subtopology euclidean s) {x \<in> s. f x = a}"
  4579   using continuous_closed_in_preimage[of s f "{a}"] by auto
  4580 
  4581 lemma continuous_closed_preimage_constant:
  4582   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  4583   shows "continuous_on s f \<Longrightarrow> closed s ==> closed {x \<in> s. f x = a}"
  4584   using continuous_closed_preimage[of s f "{a}"] by auto
  4585 
  4586 lemma continuous_constant_on_closure:
  4587   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  4588   assumes "continuous_on (closure s) f"
  4589           "\<forall>x \<in> s. f x = a"
  4590   shows "\<forall>x \<in> (closure s). f x = a"
  4591     using continuous_closed_preimage_constant[of "closure s" f a]
  4592     assms closure_minimal[of s "{x \<in> closure s. f x = a}"] closure_subset unfolding subset_eq by auto
  4593 
  4594 lemma image_closure_subset:
  4595   assumes "continuous_on (closure s) f"  "closed t"  "(f ` s) \<subseteq> t"
  4596   shows "f ` (closure s) \<subseteq> t"
  4597 proof-
  4598   have "s \<subseteq> {x \<in> closure s. f x \<in> t}" using assms(3) closure_subset by auto
  4599   moreover have "closed {x \<in> closure s. f x \<in> t}"
  4600     using continuous_closed_preimage[OF assms(1)] and assms(2) by auto
  4601   ultimately have "closure s = {x \<in> closure s . f x \<in> t}"
  4602     using closure_minimal[of s "{x \<in> closure s. f x \<in> t}"] by auto
  4603   thus ?thesis by auto
  4604 qed
  4605 
  4606 lemma continuous_on_closure_norm_le:
  4607   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  4608   assumes "continuous_on (closure s) f"  "\<forall>y \<in> s. norm(f y) \<le> b"  "x \<in> (closure s)"
  4609   shows "norm(f x) \<le> b"
  4610 proof-
  4611   have *:"f ` s \<subseteq> cball 0 b" using assms(2)[unfolded mem_cball_0[THEN sym]] by auto
  4612   show ?thesis
  4613     using image_closure_subset[OF assms(1) closed_cball[of 0 b] *] assms(3)
  4614     unfolding subset_eq apply(erule_tac x="f x" in ballE) by (auto simp add: dist_norm)
  4615 qed
  4616 
  4617 text {* Making a continuous function avoid some value in a neighbourhood. *}
  4618 
  4619 lemma continuous_within_avoid:
  4620   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"
  4621   assumes "continuous (at x within s) f" and "f x \<noteq> a"
  4622   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"
  4623 proof-
  4624   obtain U where "open U" and "f x \<in> U" and "a \<notin> U"
  4625     using t1_space [OF `f x \<noteq> a`] by fast
  4626   have "(f ---> f x) (at x within s)"
  4627     using assms(1) by (simp add: continuous_within)
  4628   hence "eventually (\<lambda>y. f y \<in> U) (at x within s)"
  4629     using `open U` and `f x \<in> U`
  4630     unfolding tendsto_def by fast
  4631   hence "eventually (\<lambda>y. f y \<noteq> a) (at x within s)"
  4632     using `a \<notin> U` by (fast elim: eventually_mono [rotated])
  4633   thus ?thesis
  4634     unfolding Limits.eventually_within Limits.eventually_at
  4635     by (rule ex_forward, cut_tac `f x \<noteq> a`, auto simp: dist_commute)
  4636 qed
  4637 
  4638 lemma continuous_at_avoid:
  4639   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"
  4640   assumes "continuous (at x) f" and "f x \<noteq> a"
  4641   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
  4642   using assms continuous_within_avoid[of x UNIV f a] by simp
  4643 
  4644 lemma continuous_on_avoid:
  4645   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"
  4646   assumes "continuous_on s f"  "x \<in> s"  "f x \<noteq> a"
  4647   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"
  4648 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
  4649 
  4650 lemma continuous_on_open_avoid:
  4651   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"
  4652   assumes "continuous_on s f"  "open s"  "x \<in> s"  "f x \<noteq> a"
  4653   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"
  4654 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
  4655 
  4656 text {* Proving a function is constant by proving open-ness of level set. *}
  4657 
  4658 lemma continuous_levelset_open_in_cases:
  4659   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  4660   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
  4661         openin (subtopology euclidean s) {x \<in> s. f x = a}
  4662         ==> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"
  4663 unfolding connected_clopen using continuous_closed_in_preimage_constant by auto
  4664 
  4665 lemma continuous_levelset_open_in:
  4666   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  4667   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>
  4668         openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>
  4669         (\<exists>x \<in> s. f x = a)  ==> (\<forall>x \<in> s. f x = a)"
  4670 using continuous_levelset_open_in_cases[of s f ]
  4671 by meson
  4672 
  4673 lemma continuous_levelset_open:
  4674   fixes f :: "_ \<Rightarrow> 'b::t1_space"
  4675   assumes "connected s"  "continuous_on s f"  "open {x \<in> s. f x = a}"  "\<exists>x \<in> s.  f x = a"
  4676   shows "\<forall>x \<in> s. f x = a"
  4677 using continuous_levelset_open_in[OF assms(1,2), of a, unfolded openin_open] using assms (3,4) by fast
  4678 
  4679 text {* Some arithmetical combinations (more to prove). *}
  4680 
  4681 lemma open_scaling[intro]:
  4682   fixes s :: "'a::real_normed_vector set"
  4683   assumes "c \<noteq> 0"  "open s"
  4684   shows "open((\<lambda>x. c *\<^sub>R x) ` s)"
  4685 proof-
  4686   { fix x assume "x \<in> s"
  4687     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
  4688     have "e * abs c > 0" using assms(1)[unfolded zero_less_abs_iff[THEN sym]] using mult_pos_pos[OF `e>0`] by auto
  4689     moreover
  4690     { fix y assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"
  4691       hence "norm ((1 / c) *\<^sub>R y - x) < e" unfolding dist_norm
  4692         using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)
  4693           assms(1)[unfolded zero_less_abs_iff[THEN sym]] by (simp del:zero_less_abs_iff)
  4694       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  }
  4695     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  }
  4696   thus ?thesis unfolding open_dist by auto
  4697 qed
  4698 
  4699 lemma minus_image_eq_vimage:
  4700   fixes A :: "'a::ab_group_add set"
  4701   shows "(\<lambda>x. - x) ` A = (\<lambda>x. - x) -` A"
  4702   by (auto intro!: image_eqI [where f="\<lambda>x. - x"])
  4703 
  4704 lemma open_negations:
  4705   fixes s :: "'a::real_normed_vector set"
  4706   shows "open s ==> open ((\<lambda> x. -x) ` s)"
  4707   unfolding scaleR_minus1_left [symmetric]
  4708   by (rule open_scaling, auto)
  4709 
  4710 lemma open_translation:
  4711   fixes s :: "'a::real_normed_vector set"
  4712   assumes "open s"  shows "open((\<lambda>x. a + x) ` s)"
  4713 proof-
  4714   { fix x have "continuous (at x) (\<lambda>x. x - a)"
  4715       by (intro continuous_diff continuous_at_id continuous_const) }
  4716   moreover have "{x. x - a \<in> s} = op + a ` s" by force
  4717   ultimately show ?thesis using continuous_open_preimage_univ[of "\<lambda>x. x - a" s] using assms by auto
  4718 qed
  4719 
  4720 lemma open_affinity:
  4721   fixes s :: "'a::real_normed_vector set"
  4722   assumes "open s"  "c \<noteq> 0"
  4723   shows "open ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  4724 proof-
  4725   have *:"(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)" unfolding o_def ..
  4726   have "op + a ` op *\<^sub>R c ` s = (op + a \<circ> op *\<^sub>R c) ` s" by auto
  4727   thus ?thesis using assms open_translation[of "op *\<^sub>R c ` s" a] unfolding * by auto
  4728 qed
  4729 
  4730 lemma interior_translation:
  4731   fixes s :: "'a::real_normed_vector set"
  4732   shows "interior ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (interior s)"
  4733 proof (rule set_eqI, rule)
  4734   fix x assume "x \<in> interior (op + a ` s)"
  4735   then obtain e where "e>0" and e:"ball x e \<subseteq> op + a ` s" unfolding mem_interior by auto
  4736   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
  4737   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
  4738 next
  4739   fix x assume "x \<in> op + a ` interior s"
  4740   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
  4741   { fix z have *:"a + y - z = y + a - z" by auto
  4742     assume "z\<in>ball x e"
  4743     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
  4744     hence "z \<in> op + a ` s" unfolding image_iff by(auto intro!: bexI[where x="z - a"])  }
  4745   hence "ball x e \<subseteq> op + a ` s" unfolding subset_eq by auto
  4746   thus "x \<in> interior (op + a ` s)" unfolding mem_interior using `e>0` by auto
  4747 qed
  4748 
  4749 text {* Topological properties of linear functions. *}
  4750 
  4751 lemma linear_lim_0:
  4752   assumes "bounded_linear f" shows "(f ---> 0) (at (0))"
  4753 proof-
  4754   interpret f: bounded_linear f by fact
  4755   have "(f ---> f 0) (at 0)"
  4756     using tendsto_ident_at by (rule f.tendsto)
  4757   thus ?thesis unfolding f.zero .
  4758 qed
  4759 
  4760 lemma linear_continuous_at:
  4761   assumes "bounded_linear f"  shows "continuous (at a) f"
  4762   unfolding continuous_at using assms
  4763   apply (rule bounded_linear.tendsto)
  4764   apply (rule tendsto_ident_at)
  4765   done
  4766 
  4767 lemma linear_continuous_within:
  4768   shows "bounded_linear f ==> continuous (at x within s) f"
  4769   using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto
  4770 
  4771 lemma linear_continuous_on:
  4772   shows "bounded_linear f ==> continuous_on s f"
  4773   using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto
  4774 
  4775 text {* Also bilinear functions, in composition form. *}
  4776 
  4777 lemma bilinear_continuous_at_compose:
  4778   shows "continuous (at x) f \<Longrightarrow> continuous (at x) g \<Longrightarrow> bounded_bilinear h
  4779         ==> continuous (at x) (\<lambda>x. h (f x) (g x))"
  4780   unfolding continuous_at using Lim_bilinear[of f "f x" "(at x)" g "g x" h] by auto
  4781 
  4782 lemma bilinear_continuous_within_compose:
  4783   shows "continuous (at x within s) f \<Longrightarrow> continuous (at x within s) g \<Longrightarrow> bounded_bilinear h
  4784         ==> continuous (at x within s) (\<lambda>x. h (f x) (g x))"
  4785   unfolding continuous_within using Lim_bilinear[of f "f x"] by auto
  4786 
  4787 lemma bilinear_continuous_on_compose:
  4788   shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> bounded_bilinear h
  4789              ==> continuous_on s (\<lambda>x. h (f x) (g x))"
  4790   unfolding continuous_on_def
  4791   by (fast elim: bounded_bilinear.tendsto)
  4792 
  4793 text {* Preservation of compactness and connectedness under continuous function. *}
  4794 
  4795 lemma compact_eq_openin_cover:
  4796   "compact S \<longleftrightarrow>
  4797     (\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>
  4798       (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D))"
  4799 proof safe
  4800   fix C
  4801   assume "compact S" and "\<forall>c\<in>C. openin (subtopology euclidean S) c" and "S \<subseteq> \<Union>C"
  4802   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}"
  4803     unfolding openin_open by force+
  4804   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"
  4805     by (rule compactE)
  4806   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)"
  4807     by auto
  4808   thus "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..
  4809 next
  4810   assume 1: "\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>
  4811         (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D)"
  4812   show "compact S"
  4813   proof (rule compactI)
  4814     fix C
  4815     let ?C = "image (\<lambda>T. S \<inter> T) C"
  4816     assume "\<forall>t\<in>C. open t" and "S \<subseteq> \<Union>C"
  4817     hence "(\<forall>c\<in>?C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>?C"
  4818       unfolding openin_open by auto
  4819     with 1 obtain D where "D \<subseteq> ?C" and "finite D" and "S \<subseteq> \<Union>D"
  4820       by metis
  4821     let ?D = "inv_into C (\<lambda>T. S \<inter> T) ` D"
  4822     have "?D \<subseteq> C \<and> finite ?D \<and> S \<subseteq> \<Union>?D"
  4823     proof (intro conjI)
  4824       from `D \<subseteq> ?C` show "?D \<subseteq> C"
  4825         by (fast intro: inv_into_into)
  4826       from `finite D` show "finite ?D"
  4827         by (rule finite_imageI)
  4828       from `S \<subseteq> \<Union>D` show "S \<subseteq> \<Union>?D"
  4829         apply (rule subset_trans)
  4830         apply clarsimp
  4831         apply (frule subsetD [OF `D \<subseteq> ?C`, THEN f_inv_into_f])
  4832         apply (erule rev_bexI, fast)
  4833         done
  4834     qed
  4835     thus "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..
  4836   qed
  4837 qed
  4838 
  4839 lemma compact_continuous_image:
  4840   assumes "continuous_on s f" and "compact s"
  4841   shows "compact (f ` s)"
  4842 using assms (* FIXME: long unstructured proof *)
  4843 unfolding continuous_on_open
  4844 unfolding compact_eq_openin_cover
  4845 apply clarify
  4846 apply (drule_tac x="image (\<lambda>t. {x \<in> s. f x \<in> t}) C" in spec)
  4847 apply (drule mp)
  4848 apply (rule conjI)
  4849 apply simp
  4850 apply clarsimp
  4851 apply (drule subsetD)
  4852 apply (erule imageI)
  4853 apply fast
  4854 apply (erule thin_rl)
  4855 apply clarify
  4856 apply (rule_tac x="image (inv_into C (\<lambda>t. {x \<in> s. f x \<in> t})) D" in exI)
  4857 apply (intro conjI)
  4858 apply clarify
  4859 apply (rule inv_into_into)
  4860 apply (erule (1) subsetD)
  4861 apply (erule finite_imageI)
  4862 apply (clarsimp, rename_tac x)
  4863 apply (drule (1) subsetD, clarify)
  4864 apply (drule (1) subsetD, clarify)
  4865 apply (rule rev_bexI)
  4866 apply assumption
  4867 apply (subgoal_tac "{x \<in> s. f x \<in> t} \<in> (\<lambda>t. {x \<in> s. f x \<in> t}) ` C")
  4868 apply (drule f_inv_into_f)
  4869 apply fast
  4870 apply (erule imageI)
  4871 done
  4872 
  4873 lemma connected_continuous_image:
  4874   assumes "continuous_on s f"  "connected s"
  4875   shows "connected(f ` s)"
  4876 proof-
  4877   { fix T assume as: "T \<noteq> {}"  "T \<noteq> f ` s"  "openin (subtopology euclidean (f ` s)) T"  "closedin (subtopology euclidean (f ` s)) T"
  4878     have "{x \<in> s. f x \<in> T} = {} \<or> {x \<in> s. f x \<in> T} = s"
  4879       using assms(1)[unfolded continuous_on_open, THEN spec[where x=T]]
  4880       using assms(1)[unfolded continuous_on_closed, THEN spec[where x=T]]
  4881       using assms(2)[unfolded connected_clopen, THEN spec[where x="{x \<in> s. f x \<in> T}"]] as(3,4) by auto
  4882     hence False using as(1,2)
  4883       using as(4)[unfolded closedin_def topspace_euclidean_subtopology] by auto }
  4884   thus ?thesis unfolding connected_clopen by auto
  4885 qed
  4886 
  4887 text {* Continuity implies uniform continuity on a compact domain. *}
  4888   
  4889 lemma compact_uniformly_continuous:
  4890   assumes f: "continuous_on s f" and s: "compact s"
  4891   shows "uniformly_continuous_on s f"
  4892   unfolding uniformly_continuous_on_def
  4893 proof (cases, safe)
  4894   fix e :: real assume "0 < e" "s \<noteq> {}"
  4895   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) } }"
  4896   let ?b = "(\<lambda>(y, d). ball y (d/2))"
  4897   have "(\<forall>r\<in>R. open (?b r))" "s \<subseteq> (\<Union>r\<in>R. ?b r)"
  4898   proof safe
  4899     fix y assume "y \<in> s"
  4900     from continuous_open_in_preimage[OF f open_ball]
  4901     obtain T where "open T" and T: "{x \<in> s. f x \<in> ball (f y) (e/2)} = T \<inter> s"
  4902       unfolding openin_subtopology open_openin by metis
  4903     then obtain d where "ball y d \<subseteq> T" "0 < d"
  4904       using `0 < e` `y \<in> s` by (auto elim!: openE)
  4905     with T `y \<in> s` show "y \<in> (\<Union>r\<in>R. ?b r)"
  4906       by (intro UN_I[of "(y, d)"]) auto
  4907   qed auto
  4908   with s obtain D where D: "finite D" "D \<subseteq> R" "s \<subseteq> (\<Union>(y, d)\<in>D. ball y (d/2))"
  4909     by (rule compactE_image)
  4910   with `s \<noteq> {}` have [simp]: "\<And>x. x < Min (snd ` D) \<longleftrightarrow> (\<forall>(y, d)\<in>D. x < d)"
  4911     by (subst Min_gr_iff) auto
  4912   show "\<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"
  4913   proof (rule, safe)
  4914     fix x x' assume in_s: "x' \<in> s" "x \<in> s"
  4915     with D obtain y d where x: "x \<in> ball y (d/2)" "(y, d) \<in> D"
  4916       by blast
  4917     moreover assume "dist x x' < Min (snd`D) / 2"
  4918     ultimately have "dist y x' < d"
  4919       by (intro dist_double[where x=x and d=d]) (auto simp: dist_commute)
  4920     with D x in_s show  "dist (f x) (f x') < e"
  4921       by (intro dist_double[where x="f y" and d=e]) (auto simp: dist_commute subset_eq)
  4922   qed (insert D, auto)
  4923 qed auto
  4924 
  4925 text{* Continuity of inverse function on compact domain. *}
  4926 
  4927 lemma continuous_on_inv:
  4928   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  4929   assumes "continuous_on s f"  "compact s"  "\<forall>x \<in> s. g (f x) = x"
  4930   shows "continuous_on (f ` s) g"
  4931 unfolding continuous_on_topological
  4932 proof (clarsimp simp add: assms(3))
  4933   fix x :: 'a and B :: "'a set"
  4934   assume "x \<in> s" and "open B" and "x \<in> B"
  4935   have 1: "\<forall>x\<in>s. f x \<in> f ` (s - B) \<longleftrightarrow> x \<in> s - B"
  4936     using assms(3) by (auto, metis)
  4937   have "continuous_on (s - B) f"
  4938     using `continuous_on s f` Diff_subset
  4939     by (rule continuous_on_subset)
  4940   moreover have "compact (s - B)"
  4941     using `open B` and `compact s`
  4942     unfolding Diff_eq by (intro compact_inter_closed closed_Compl)
  4943   ultimately have "compact (f ` (s - B))"
  4944     by (rule compact_continuous_image)
  4945   hence "closed (f ` (s - B))"
  4946     by (rule compact_imp_closed)
  4947   hence "open (- f ` (s - B))"
  4948     by (rule open_Compl)
  4949   moreover have "f x \<in> - f ` (s - B)"
  4950     using `x \<in> s` and `x \<in> B` by (simp add: 1)
  4951   moreover have "\<forall>y\<in>s. f y \<in> - f ` (s - B) \<longrightarrow> y \<in> B"
  4952     by (simp add: 1)
  4953   ultimately show "\<exists>A. open A \<and> f x \<in> A \<and> (\<forall>y\<in>s. f y \<in> A \<longrightarrow> y \<in> B)"
  4954     by fast
  4955 qed
  4956 
  4957 text {* A uniformly convergent limit of continuous functions is continuous. *}
  4958 
  4959 lemma continuous_uniform_limit:
  4960   fixes f :: "'a \<Rightarrow> 'b::metric_space \<Rightarrow> 'c::metric_space"
  4961   assumes "\<not> trivial_limit F"
  4962   assumes "eventually (\<lambda>n. continuous_on s (f n)) F"
  4963   assumes "\<forall>e>0. eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e) F"
  4964   shows "continuous_on s g"
  4965 proof-
  4966   { fix x and e::real assume "x\<in>s" "e>0"
  4967     have "eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e / 3) F"
  4968       using `e>0` assms(3)[THEN spec[where x="e/3"]] by auto
  4969     from eventually_happens [OF eventually_conj [OF this assms(2)]]
  4970     obtain n where n:"\<forall>x\<in>s. dist (f n x) (g x) < e / 3"  "continuous_on s (f n)"
  4971       using assms(1) by blast
  4972     have "e / 3 > 0" using `e>0` by auto
  4973     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"
  4974       using n(2)[unfolded continuous_on_iff, THEN bspec[where x=x], OF `x\<in>s`, THEN spec[where x="e/3"]] by blast
  4975     { fix y assume "y \<in> s" and "dist y x < d"
  4976       hence "dist (f n y) (f n x) < e / 3"
  4977         by (rule d [rule_format])
  4978       hence "dist (f n y) (g x) < 2 * e / 3"
  4979         using dist_triangle [of "f n y" "g x" "f n x"]
  4980         using n(1)[THEN bspec[where x=x], OF `x\<in>s`]
  4981         by auto
  4982       hence "dist (g y) (g x) < e"
  4983         using n(1)[THEN bspec[where x=y], OF `y\<in>s`]
  4984         using dist_triangle3 [of "g y" "g x" "f n y"]
  4985         by auto }
  4986     hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e"
  4987       using `d>0` by auto }
  4988   thus ?thesis unfolding continuous_on_iff by auto
  4989 qed
  4990 
  4991 
  4992 subsection {* Topological stuff lifted from and dropped to R *}
  4993 
  4994 lemma open_real:
  4995   fixes s :: "real set" shows
  4996  "open s \<longleftrightarrow>
  4997         (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. abs(x' - x) < e --> x' \<in> s)" (is "?lhs = ?rhs")
  4998   unfolding open_dist dist_norm by simp
  4999 
  5000 lemma islimpt_approachable_real:
  5001   fixes s :: "real set"
  5002   shows "x islimpt s \<longleftrightarrow> (\<forall>e>0.  \<exists>x'\<in> s. x' \<noteq> x \<and> abs(x' - x) < e)"
  5003   unfolding islimpt_approachable dist_norm by simp
  5004 
  5005 lemma closed_real:
  5006   fixes s :: "real set"
  5007   shows "closed s \<longleftrightarrow>
  5008         (\<forall>x. (\<forall>e>0.  \<exists>x' \<in> s. x' \<noteq> x \<and> abs(x' - x) < e)
  5009             --> x \<in> s)"
  5010   unfolding closed_limpt islimpt_approachable dist_norm by simp
  5011 
  5012 lemma continuous_at_real_range:
  5013   fixes f :: "'a::real_normed_vector \<Rightarrow> real"
  5014   shows "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.
  5015         \<forall>x'. norm(x' - x) < d --> abs(f x' - f x) < e)"
  5016   unfolding continuous_at unfolding Lim_at
  5017   unfolding dist_nz[THEN sym] unfolding dist_norm apply auto
  5018   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
  5019   apply(erule_tac x=e in allE) by auto
  5020 
  5021 lemma continuous_on_real_range:
  5022   fixes f :: "'a::real_normed_vector \<Rightarrow> real"
  5023   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))"
  5024   unfolding continuous_on_iff dist_norm by simp
  5025 
  5026 text {* Hence some handy theorems on distance, diameter etc. of/from a set. *}
  5027 
  5028 lemma compact_attains_sup:
  5029   fixes s :: "real set"
  5030   assumes "compact s"  "s \<noteq> {}"
  5031   shows "\<exists>x \<in> s. \<forall>y \<in> s. y \<le> x"
  5032 proof-
  5033   from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
  5034   { 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"
  5035     have "isLub UNIV s (Sup s)" using Sup[OF assms(2)] unfolding setle_def using as(1) by auto
  5036     moreover have "isUb UNIV s (Sup s - e)" unfolding isUb_def unfolding setle_def using as(4,2) by auto
  5037     ultimately have False using isLub_le_isUb[of UNIV s "Sup s" "Sup s - e"] using `e>0` by auto  }
  5038   thus ?thesis using bounded_has_Sup(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Sup s"]]
  5039     apply(rule_tac x="Sup s" in bexI) by auto
  5040 qed
  5041 
  5042 lemma Inf:
  5043   fixes S :: "real set"
  5044   shows "S \<noteq> {} ==> (\<exists>b. b <=* S) ==> isGlb UNIV S (Inf S)"
  5045 by (auto simp add: isLb_def setle_def setge_def isGlb_def greatestP_def) 
  5046 
  5047 lemma compact_attains_inf:
  5048   fixes s :: "real set"
  5049   assumes "compact s" "s \<noteq> {}"  shows "\<exists>x \<in> s. \<forall>y \<in> s. x \<le> y"
  5050 proof-
  5051   from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto
  5052   { fix e::real assume as: "\<forall>x\<in>s. x \<ge> Inf s"  "Inf s \<notin> s"  "0 < e"
  5053       "\<forall>x'\<in>s. x' = Inf s \<or> \<not> abs (x' - Inf s) < e"
  5054     have "isGlb UNIV s (Inf s)" using Inf[OF assms(2)] unfolding setge_def using as(1) by auto
  5055     moreover
  5056     { fix x assume "x \<in> s"
  5057       hence *:"abs (x - Inf s) = x - Inf s" using as(1)[THEN bspec[where x=x]] by auto
  5058       have "Inf s + e \<le> x" using as(4)[THEN bspec[where x=x]] using as(2) `x\<in>s` unfolding * by auto }
  5059     hence "isLb UNIV s (Inf s + e)" unfolding isLb_def and setge_def by auto
  5060     ultimately have False using isGlb_le_isLb[of UNIV s "Inf s" "Inf s + e"] using `e>0` by auto  }
  5061   thus ?thesis using bounded_has_Inf(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Inf s"]]
  5062     apply(rule_tac x="Inf s" in bexI) by auto
  5063 qed
  5064 
  5065 lemma continuous_attains_sup:
  5066   fixes f :: "'a::metric_space \<Rightarrow> real"
  5067   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
  5068         ==> (\<exists>x \<in> s. \<forall>y \<in> s.  f y \<le> f x)"
  5069   using compact_attains_sup[of "f ` s"]
  5070   using compact_continuous_image[of s f] by auto
  5071 
  5072 lemma continuous_attains_inf:
  5073   fixes f :: "'a::metric_space \<Rightarrow> real"
  5074   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f
  5075         \<Longrightarrow> (\<exists>x \<in> s. \<forall>y \<in> s. f x \<le> f y)"
  5076   using compact_attains_inf[of "f ` s"]
  5077   using compact_continuous_image[of s f] by auto
  5078 
  5079 lemma distance_attains_sup:
  5080   assumes "compact s" "s \<noteq> {}"
  5081   shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a y \<le> dist a x"
  5082 proof (rule continuous_attains_sup [OF assms])
  5083   { fix x assume "x\<in>s"
  5084     have "(dist a ---> dist a x) (at x within s)"
  5085       by (intro tendsto_dist tendsto_const Lim_at_within tendsto_ident_at)
  5086   }
  5087   thus "continuous_on s (dist a)"
  5088     unfolding continuous_on ..
  5089 qed
  5090 
  5091 text {* For \emph{minimal} distance, we only need closure, not compactness. *}
  5092 
  5093 lemma distance_attains_inf:
  5094   fixes a :: "'a::heine_borel"
  5095   assumes "closed s"  "s \<noteq> {}"
  5096   shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a x \<le> dist a y"
  5097 proof-
  5098   from assms(2) obtain b where "b\<in>s" by auto
  5099   let ?B = "cball a (dist b a) \<inter> s"
  5100   have "b \<in> ?B" using `b\<in>s` by (simp add: dist_commute)
  5101   hence "?B \<noteq> {}" by auto
  5102   moreover
  5103   { fix x assume "x\<in>?B"
  5104     fix e::real assume "e>0"
  5105     { fix x' assume "x'\<in>?B" and as:"dist x' x < e"
  5106       from as have "\<bar>dist a x' - dist a x\<bar> < e"
  5107         unfolding abs_less_iff minus_diff_eq
  5108         using dist_triangle2 [of a x' x]
  5109         using dist_triangle [of a x x']
  5110         by arith
  5111     }
  5112     hence "\<exists>d>0. \<forall>x'\<in>?B. dist x' x < d \<longrightarrow> \<bar>dist a x' - dist a x\<bar> < e"
  5113       using `e>0` by auto
  5114   }
  5115   hence "continuous_on (cball a (dist b a) \<inter> s) (dist a)"
  5116     unfolding continuous_on Lim_within dist_norm real_norm_def
  5117     by fast
  5118   moreover have "compact ?B"
  5119     using compact_cball[of a "dist b a"]
  5120     unfolding compact_eq_bounded_closed
  5121     using bounded_Int and closed_Int and assms(1) by auto
  5122   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"
  5123     using continuous_attains_inf[of ?B "dist a"] by fastforce
  5124   thus ?thesis by fastforce
  5125 qed
  5126 
  5127 
  5128 subsection {* Pasted sets *}
  5129 
  5130 lemma bounded_Times:
  5131   assumes "bounded s" "bounded t" shows "bounded (s \<times> t)"
  5132 proof-
  5133   obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"
  5134     using assms [unfolded bounded_def] by auto
  5135   then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<twosuperior> + b\<twosuperior>)"
  5136     by (auto simp add: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)
  5137   thus ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto
  5138 qed
  5139 
  5140 lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"
  5141 by (induct x) simp
  5142 
  5143 lemma seq_compact_Times: "seq_compact s \<Longrightarrow> seq_compact t \<Longrightarrow> seq_compact (s \<times> t)"
  5144 unfolding seq_compact_def
  5145 apply clarify
  5146 apply (drule_tac x="fst \<circ> f" in spec)
  5147 apply (drule mp, simp add: mem_Times_iff)
  5148 apply (clarify, rename_tac l1 r1)
  5149 apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)
  5150 apply (drule mp, simp add: mem_Times_iff)
  5151 apply (clarify, rename_tac l2 r2)
  5152 apply (rule_tac x="(l1, l2)" in rev_bexI, simp)
  5153 apply (rule_tac x="r1 \<circ> r2" in exI)
  5154 apply (rule conjI, simp add: subseq_def)
  5155 apply (drule_tac r=r2 in lim_subseq [rotated], assumption)
  5156 apply (drule (1) tendsto_Pair) back
  5157 apply (simp add: o_def)
  5158 done
  5159 
  5160 text {* Generalize to @{class topological_space} *}
  5161 lemma compact_Times: 
  5162   fixes s :: "'a::metric_space set" and t :: "'b::metric_space set"
  5163   shows "compact s \<Longrightarrow> compact t \<Longrightarrow> compact (s \<times> t)"
  5164   unfolding compact_eq_seq_compact_metric by (rule seq_compact_Times)
  5165 
  5166 text{* Hence some useful properties follow quite easily. *}
  5167 
  5168 lemma compact_scaling:
  5169   fixes s :: "'a::real_normed_vector set"
  5170   assumes "compact s"  shows "compact ((\<lambda>x. c *\<^sub>R x) ` s)"
  5171 proof-
  5172   let ?f = "\<lambda>x. scaleR c x"
  5173   have *:"bounded_linear ?f" by (rule bounded_linear_scaleR_right)
  5174   show ?thesis using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]
  5175     using linear_continuous_at[OF *] assms by auto
  5176 qed
  5177 
  5178 lemma compact_negations:
  5179   fixes s :: "'a::real_normed_vector set"
  5180   assumes "compact s"  shows "compact ((\<lambda>x. -x) ` s)"
  5181   using compact_scaling [OF assms, of "- 1"] by auto
  5182 
  5183 lemma compact_sums:
  5184   fixes s t :: "'a::real_normed_vector set"
  5185   assumes "compact s"  "compact t"  shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"
  5186 proof-
  5187   have *:"{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z) ` (s \<times> t)"
  5188     apply auto unfolding image_iff apply(rule_tac x="(xa, y)" in bexI) by auto
  5189   have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"
  5190     unfolding continuous_on by (rule ballI) (intro tendsto_intros)
  5191   thus ?thesis unfolding * using compact_continuous_image compact_Times [OF assms] by auto
  5192 qed
  5193 
  5194 lemma compact_differences:
  5195   fixes s t :: "'a::real_normed_vector set"
  5196   assumes "compact s" "compact t"  shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"
  5197 proof-
  5198   have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus ` t)}"
  5199     apply auto apply(rule_tac x= xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
  5200   thus ?thesis using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto
  5201 qed
  5202 
  5203 lemma compact_translation:
  5204   fixes s :: "'a::real_normed_vector set"
  5205   assumes "compact s"  shows "compact ((\<lambda>x. a + x) ` s)"
  5206 proof-
  5207   have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x) ` s" by auto
  5208   thus ?thesis using compact_sums[OF assms compact_sing[of a]] by auto
  5209 qed
  5210 
  5211 lemma compact_affinity:
  5212   fixes s :: "'a::real_normed_vector set"
  5213   assumes "compact s"  shows "compact ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  5214 proof-
  5215   have "op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
  5216   thus ?thesis using compact_translation[OF compact_scaling[OF assms], of a c] by auto
  5217 qed
  5218 
  5219 text {* Hence we get the following. *}
  5220 
  5221 lemma compact_sup_maxdistance:
  5222   fixes s :: "'a::real_normed_vector set"
  5223   assumes "compact s"  "s \<noteq> {}"
  5224   shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. norm(u - v) \<le> norm(x - y)"
  5225 proof-
  5226   have "{x - y | x y . x\<in>s \<and> y\<in>s} \<noteq> {}" using `s \<noteq> {}` by auto
  5227   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"
  5228     using compact_differences[OF assms(1) assms(1)]
  5229     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
  5230   from x(1) obtain a b where "a\<in>s" "b\<in>s" "x = a - b" by auto
  5231   thus ?thesis using x(2)[unfolded `x = a - b`] by blast
  5232 qed
  5233 
  5234 text {* We can state this in terms of diameter of a set. *}
  5235 
  5236 definition "diameter s = (if s = {} then 0::real else Sup {norm(x - y) | x y. x \<in> s \<and> y \<in> s})"
  5237   (* TODO: generalize to class metric_space *)
  5238 
  5239 lemma diameter_bounded:
  5240   assumes "bounded s"
  5241   shows "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
  5242         "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)"
  5243 proof-
  5244   let ?D = "{norm (x - y) |x y. x \<in> s \<and> y \<in> s}"
  5245   obtain a where a:"\<forall>x\<in>s. norm x \<le> a" using assms[unfolded bounded_iff] by auto
  5246   { fix x y assume "x \<in> s" "y \<in> s"
  5247     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)  }
  5248   note * = this
  5249   { fix x y assume "x\<in>s" "y\<in>s"  hence "s \<noteq> {}" by auto
  5250     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`
  5251       by simp (blast del: Sup_upper intro!: * Sup_upper) }
  5252   moreover
  5253   { fix d::real assume "d>0" "d < diameter s"
  5254     hence "s\<noteq>{}" unfolding diameter_def by auto
  5255     have "\<exists>d' \<in> ?D. d' > d"
  5256     proof(rule ccontr)
  5257       assume "\<not> (\<exists>d'\<in>{norm (x - y) |x y. x \<in> s \<and> y \<in> s}. d < d')"
  5258       hence "\<forall>d'\<in>?D. d' \<le> d" by auto (metis not_leE) 
  5259       thus False using `d < diameter s` `s\<noteq>{}` 
  5260         apply (auto simp add: diameter_def) 
  5261         apply (drule Sup_real_iff [THEN [2] rev_iffD2])
  5262         apply (auto, force) 
  5263         done
  5264     qed
  5265     hence "\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d" by auto  }
  5266   ultimately show "\<forall>x\<in>s. \<forall>y\<in>s. norm(x - y) \<le> diameter s"
  5267         "\<forall>d>0. d < diameter s --> (\<exists>x\<in>s. \<exists>y\<in>s. norm(x - y) > d)" by auto
  5268 qed
  5269 
  5270 lemma diameter_bounded_bound:
  5271  "bounded s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s ==> norm(x - y) \<le> diameter s"
  5272   using diameter_bounded by blast
  5273 
  5274 lemma diameter_compact_attained:
  5275   fixes s :: "'a::real_normed_vector set"
  5276   assumes "compact s"  "s \<noteq> {}"
  5277   shows "\<exists>x\<in>s. \<exists>y\<in>s. (norm(x - y) = diameter s)"
  5278 proof-
  5279   have b:"bounded s" using assms(1) by (rule compact_imp_bounded)
  5280   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
  5281   hence "diameter s \<le> norm (x - y)"
  5282     unfolding diameter_def by clarsimp (rule Sup_least, fast+)
  5283   thus ?thesis
  5284     by (metis b diameter_bounded_bound order_antisym xys)
  5285 qed
  5286 
  5287 text {* Related results with closure as the conclusion. *}
  5288 
  5289 lemma closed_scaling:
  5290   fixes s :: "'a::real_normed_vector set"
  5291   assumes "closed s" shows "closed ((\<lambda>x. c *\<^sub>R x) ` s)"
  5292 proof(cases "s={}")
  5293   case True thus ?thesis by auto
  5294 next
  5295   case False
  5296   show ?thesis
  5297   proof(cases "c=0")
  5298     have *:"(\<lambda>x. 0) ` s = {0}" using `s\<noteq>{}` by auto
  5299     case True thus ?thesis apply auto unfolding * by auto
  5300   next
  5301     case False
  5302     { fix x l assume as:"\<forall>n::nat. x n \<in> scaleR c ` s"  "(x ---> l) sequentially"
  5303       { fix n::nat have "scaleR (1 / c) (x n) \<in> s"
  5304           using as(1)[THEN spec[where x=n]]
  5305           using `c\<noteq>0` by auto
  5306       }
  5307       moreover
  5308       { fix e::real assume "e>0"
  5309         hence "0 < e *\<bar>c\<bar>"  using `c\<noteq>0` mult_pos_pos[of e "abs c"] by auto
  5310         then obtain N where "\<forall>n\<ge>N. dist (x n) l < e * \<bar>c\<bar>"
  5311           using as(2)[unfolded LIMSEQ_def, THEN spec[where x="e * abs c"]] by auto
  5312         hence "\<exists>N. \<forall>n\<ge>N. dist (scaleR (1 / c) (x n)) (scaleR (1 / c) l) < e"
  5313           unfolding dist_norm unfolding scaleR_right_diff_distrib[THEN sym]
  5314           using mult_imp_div_pos_less[of "abs c" _ e] `c\<noteq>0` by auto  }
  5315       hence "((\<lambda>n. scaleR (1 / c) (x n)) ---> scaleR (1 / c) l) sequentially" unfolding LIMSEQ_def by auto
  5316       ultimately have "l \<in> scaleR c ` s"
  5317         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"]]
  5318         unfolding image_iff using `c\<noteq>0` apply(rule_tac x="scaleR (1 / c) l" in bexI) by auto  }
  5319     thus ?thesis unfolding closed_sequential_limits by fast
  5320   qed
  5321 qed
  5322 
  5323 lemma closed_negations:
  5324   fixes s :: "'a::real_normed_vector set"
  5325   assumes "closed s"  shows "closed ((\<lambda>x. -x) ` s)"
  5326   using closed_scaling[OF assms, of "- 1"] by simp
  5327 
  5328 lemma compact_closed_sums:
  5329   fixes s :: "'a::real_normed_vector set"
  5330   assumes "compact s"  "closed t"  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
  5331 proof-
  5332   let ?S = "{x + y |x y. x \<in> s \<and> y \<in> t}"
  5333   { fix x l assume as:"\<forall>n. x n \<in> ?S"  "(x ---> l) sequentially"
  5334     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"
  5335       using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> s \<and> snd y \<in> t"] by auto
  5336     obtain l' r where "l'\<in>s" and r:"subseq r" and lr:"(((\<lambda>n. fst (f n)) \<circ> r) ---> l') sequentially"
  5337       using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto
  5338     have "((\<lambda>n. snd (f (r n))) ---> l - l') sequentially"
  5339       using tendsto_diff[OF lim_subseq[OF r as(2)] lr] and f(1) unfolding o_def by auto
  5340     hence "l - l' \<in> t"
  5341       using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda> n. snd (f (r n))"], THEN spec[where x="l - l'"]]
  5342       using f(3) by auto
  5343     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
  5344   }
  5345   thus ?thesis unfolding closed_sequential_limits by fast
  5346 qed
  5347 
  5348 lemma closed_compact_sums:
  5349   fixes s t :: "'a::real_normed_vector set"
  5350   assumes "closed s"  "compact t"
  5351   shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"
  5352 proof-
  5353   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
  5354     apply(rule_tac x=y in exI) apply auto apply(rule_tac x=y in exI) by auto
  5355   thus ?thesis using compact_closed_sums[OF assms(2,1)] by simp
  5356 qed
  5357 
  5358 lemma compact_closed_differences:
  5359   fixes s t :: "'a::real_normed_vector set"
  5360   assumes "compact s"  "closed t"
  5361   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
  5362 proof-
  5363   have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} =  {x - y |x y. x \<in> s \<and> y \<in> t}"
  5364     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
  5365   thus ?thesis using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto
  5366 qed
  5367 
  5368 lemma closed_compact_differences:
  5369   fixes s t :: "'a::real_normed_vector set"
  5370   assumes "closed s" "compact t"
  5371   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"
  5372 proof-
  5373   have "{x + y |x y. x \<in> s \<and> y \<in> uminus ` t} = {x - y |x y. x \<in> s \<and> y \<in> t}"
  5374     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto
  5375  thus ?thesis using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp
  5376 qed
  5377 
  5378 lemma closed_translation:
  5379   fixes a :: "'a::real_normed_vector"
  5380   assumes "closed s"  shows "closed ((\<lambda>x. a + x) ` s)"
  5381 proof-
  5382   have "{a + y |y. y \<in> s} = (op + a ` s)" by auto
  5383   thus ?thesis using compact_closed_sums[OF compact_sing[of a] assms] by auto
  5384 qed
  5385 
  5386 lemma translation_Compl:
  5387   fixes a :: "'a::ab_group_add"
  5388   shows "(\<lambda>x. a + x) ` (- t) = - ((\<lambda>x. a + x) ` t)"
  5389   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in bexI) by auto
  5390 
  5391 lemma translation_UNIV:
  5392   fixes a :: "'a::ab_group_add" shows "range (\<lambda>x. a + x) = UNIV"
  5393   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in exI) by auto
  5394 
  5395 lemma translation_diff:
  5396   fixes a :: "'a::ab_group_add"
  5397   shows "(\<lambda>x. a + x) ` (s - t) = ((\<lambda>x. a + x) ` s) - ((\<lambda>x. a + x) ` t)"
  5398   by auto
  5399 
  5400 lemma closure_translation:
  5401   fixes a :: "'a::real_normed_vector"
  5402   shows "closure ((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (closure s)"
  5403 proof-
  5404   have *:"op + a ` (- s) = - op + a ` s"
  5405     apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto
  5406   show ?thesis unfolding closure_interior translation_Compl
  5407     using interior_translation[of a "- s"] unfolding * by auto
  5408 qed
  5409 
  5410 lemma frontier_translation:
  5411   fixes a :: "'a::real_normed_vector"
  5412   shows "frontier((\<lambda>x. a + x) ` s) = (\<lambda>x. a + x) ` (frontier s)"
  5413   unfolding frontier_def translation_diff interior_translation closure_translation by auto
  5414 
  5415 
  5416 subsection {* Separation between points and sets *}
  5417 
  5418 lemma separate_point_closed:
  5419   fixes s :: "'a::heine_borel set"
  5420   shows "closed s \<Longrightarrow> a \<notin> s  ==> (\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x)"
  5421 proof(cases "s = {}")
  5422   case True
  5423   thus ?thesis by(auto intro!: exI[where x=1])
  5424 next
  5425   case False
  5426   assume "closed s" "a \<notin> s"
  5427   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
  5428   with `x\<in>s` show ?thesis using dist_pos_lt[of a x] and`a \<notin> s` by blast
  5429 qed
  5430 
  5431 lemma separate_compact_closed:
  5432   fixes s t :: "'a::{heine_borel, real_normed_vector} set"
  5433     (* TODO: does this generalize to heine_borel? *)
  5434   assumes "compact s" and "closed t" and "s \<inter> t = {}"
  5435   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
  5436 proof-
  5437   have "0 \<notin> {x - y |x y. x \<in> s \<and> y \<in> t}" using assms(3) by auto
  5438   then obtain d where "d>0" and d:"\<forall>x\<in>{x - y |x y. x \<in> s \<and> y \<in> t}. d \<le> dist 0 x"
  5439     using separate_point_closed[OF compact_closed_differences[OF assms(1,2)], of 0] by auto
  5440   { fix x y assume "x\<in>s" "y\<in>t"
  5441     hence "x - y \<in> {x - y |x y. x \<in> s \<and> y \<in> t}" by auto
  5442     hence "d \<le> dist (x - y) 0" using d[THEN bspec[where x="x - y"]] using dist_commute
  5443       by (auto  simp add: dist_commute)
  5444     hence "d \<le> dist x y" unfolding dist_norm by auto  }
  5445   thus ?thesis using `d>0` by auto
  5446 qed
  5447 
  5448 lemma separate_closed_compact:
  5449   fixes s t :: "'a::{heine_borel, real_normed_vector} set"
  5450   assumes "closed s" and "compact t" and "s \<inter> t = {}"
  5451   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"
  5452 proof-
  5453   have *:"t \<inter> s = {}" using assms(3) by auto
  5454   show ?thesis using separate_compact_closed[OF assms(2,1) *]
  5455     apply auto apply(rule_tac x=d in exI) apply auto apply (erule_tac x=y in ballE)
  5456     by (auto simp add: dist_commute)
  5457 qed
  5458 
  5459 
  5460 subsection {* Intervals *}
  5461   
  5462 lemma interval: fixes a :: "'a::ordered_euclidean_space" shows
  5463   "{a <..< b} = {x::'a. \<forall>i\<in>Basis. a\<bullet>i < x\<bullet>i \<and> x\<bullet>i < b\<bullet>i}" and
  5464   "{a .. b} = {x::'a. \<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i}"
  5465   by(auto simp add:set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])
  5466 
  5467 lemma mem_interval: fixes a :: "'a::ordered_euclidean_space" shows
  5468   "x \<in> {a<..<b} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < x\<bullet>i \<and> x\<bullet>i < b\<bullet>i)"
  5469   "x \<in> {a .. b} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i)"
  5470   using interval[of a b] by(auto simp add: set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])
  5471 
  5472 lemma interval_eq_empty: fixes a :: "'a::ordered_euclidean_space" shows
  5473  "({a <..< b} = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i \<le> a\<bullet>i))" (is ?th1) and
  5474  "({a  ..  b} = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i < a\<bullet>i))" (is ?th2)
  5475 proof-
  5476   { fix i x assume i:"i\<in>Basis" and as:"b\<bullet>i \<le> a\<bullet>i" and x:"x\<in>{a <..< b}"
  5477     hence "a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i" unfolding mem_interval by auto
  5478     hence "a\<bullet>i < b\<bullet>i" by auto
  5479     hence False using as by auto  }
  5480   moreover
  5481   { assume as:"\<forall>i\<in>Basis. \<not> (b\<bullet>i \<le> a\<bullet>i)"
  5482     let ?x = "(1/2) *\<^sub>R (a + b)"
  5483     { fix i :: 'a assume i:"i\<in>Basis" 
  5484       have "a\<bullet>i < b\<bullet>i" using as[THEN bspec[where x=i]] i by auto
  5485       hence "a\<bullet>i < ((1/2) *\<^sub>R (a+b)) \<bullet> i" "((1/2) *\<^sub>R (a+b)) \<bullet> i < b\<bullet>i"
  5486         by (auto simp: inner_add_left) }
  5487     hence "{a <..< b} \<noteq> {}" using mem_interval(1)[of "?x" a b] by auto  }
  5488   ultimately show ?th1 by blast
  5489 
  5490   { fix i x assume i:"i\<in>Basis" and as:"b\<bullet>i < a\<bullet>i" and x:"x\<in>{a .. b}"
  5491     hence "a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i" unfolding mem_interval by auto
  5492     hence "a\<bullet>i \<le> b\<bullet>i" by auto
  5493     hence False using as by auto  }
  5494   moreover
  5495   { assume as:"\<forall>i\<in>Basis. \<not> (b\<bullet>i < a\<bullet>i)"
  5496     let ?x = "(1/2) *\<^sub>R (a + b)"
  5497     { fix i :: 'a assume i:"i\<in>Basis"
  5498       have "a\<bullet>i \<le> b\<bullet>i" using as[THEN bspec[where x=i]] i by auto
  5499       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"
  5500         by (auto simp: inner_add_left) }
  5501     hence "{a .. b} \<noteq> {}" using mem_interval(2)[of "?x" a b] by auto  }
  5502   ultimately show ?th2 by blast
  5503 qed
  5504 
  5505 lemma interval_ne_empty: fixes a :: "'a::ordered_euclidean_space" shows
  5506   "{a  ..  b} \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i)" and
  5507   "{a <..< b} \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i)"
  5508   unfolding interval_eq_empty[of a b] by fastforce+
  5509 
  5510 lemma interval_sing:
  5511   fixes a :: "'a::ordered_euclidean_space"
  5512   shows "{a .. a} = {a}" and "{a<..<a} = {}"
  5513   unfolding set_eq_iff mem_interval eq_iff [symmetric]
  5514   by (auto intro: euclidean_eqI simp: ex_in_conv)
  5515 
  5516 lemma subset_interval_imp: fixes a :: "'a::ordered_euclidean_space" shows
  5517  "(\<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
  5518  "(\<forall>i\<in>Basis. a\<bullet>i < c\<bullet>i \<and> d\<bullet>i < b\<bullet>i) \<Longrightarrow> {c .. d} \<subseteq> {a<..<b}" and
  5519  "(\<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
  5520  "(\<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}"
  5521   unfolding subset_eq[unfolded Ball_def] unfolding mem_interval
  5522   by (best intro: order_trans less_le_trans le_less_trans less_imp_le)+
  5523 
  5524 lemma interval_open_subset_closed:
  5525   fixes a :: "'a::ordered_euclidean_space"
  5526   shows "{a<..<b} \<subseteq> {a .. b}"
  5527   unfolding subset_eq [unfolded Ball_def] mem_interval
  5528   by (fast intro: less_imp_le)
  5529 
  5530 lemma subset_interval: fixes a :: "'a::ordered_euclidean_space" shows
  5531  "{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
  5532  "{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
  5533  "{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
  5534  "{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)
  5535 proof-
  5536   show ?th1 unfolding subset_eq and Ball_def and mem_interval by (auto intro: order_trans)
  5537   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)
  5538   { assume as: "{c<..<d} \<subseteq> {a .. b}" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"
  5539     hence "{c<..<d} \<noteq> {}" unfolding interval_eq_empty by auto
  5540     fix i :: 'a assume i:"i\<in>Basis"
  5541     (** TODO combine the following two parts as done in the HOL_light version. **)
  5542     { 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"
  5543       assume as2: "a\<bullet>i > c\<bullet>i"
  5544       { fix j :: 'a assume j:"j\<in>Basis"
  5545         hence "c \<bullet> j < ?x \<bullet> j \<and> ?x \<bullet> j < d \<bullet> j"
  5546           apply(cases "j=i") using as(2)[THEN bspec[where x=j]] i
  5547           by (auto simp add: as2)  }
  5548       hence "?x\<in>{c<..<d}" using i unfolding mem_interval by auto
  5549       moreover
  5550       have "?x\<notin>{a .. b}"
  5551         unfolding mem_interval apply auto apply(rule_tac x=i in bexI)
  5552         using as(2)[THEN bspec[where x=i]] and as2 i
  5553         by auto
  5554       ultimately have False using as by auto  }
  5555     hence "a\<bullet>i \<le> c\<bullet>i" by(rule ccontr)auto
  5556     moreover
  5557     { 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"
  5558       assume as2: "b\<bullet>i < d\<bullet>i"
  5559       { fix j :: 'a assume "j\<in>Basis"
  5560         hence "d \<bullet> j > ?x \<bullet> j \<and> ?x \<bullet> j > c \<bullet> j" 
  5561           apply(cases "j=i") using as(2)[THEN bspec[where x=j]]
  5562           by (auto simp add: as2) }
  5563       hence "?x\<in>{c<..<d}" unfolding mem_interval by auto
  5564       moreover
  5565       have "?x\<notin>{a .. b}"
  5566         unfolding mem_interval apply auto apply(rule_tac x=i in bexI)
  5567         using as(2)[THEN bspec[where x=i]] and as2 using i
  5568         by auto
  5569       ultimately have False using as by auto  }
  5570     hence "b\<bullet>i \<ge> d\<bullet>i" by(rule ccontr)auto
  5571     ultimately
  5572     have "a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i" by auto
  5573   } note part1 = this
  5574   show ?th3
  5575     unfolding subset_eq and Ball_def and mem_interval 
  5576     apply(rule,rule,rule,rule) 
  5577     apply(rule part1)
  5578     unfolding subset_eq and Ball_def and mem_interval
  5579     prefer 4
  5580     apply auto 
  5581     by(erule_tac x=xa in allE,erule_tac x=xa in allE,fastforce)+ 
  5582   { assume as:"{c<..<d} \<subseteq> {a<..<b}" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"
  5583     fix i :: 'a assume i:"i\<in>Basis"
  5584     from as(1) have "{c<..<d} \<subseteq> {a..b}" using interval_open_subset_closed[of a b] by auto
  5585     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
  5586   show ?th4 unfolding subset_eq and Ball_def and mem_interval 
  5587     apply(rule,rule,rule,rule) apply(rule *) unfolding subset_eq and Ball_def and mem_interval prefer 4
  5588     apply auto by(erule_tac x=xa in allE, simp)+ 
  5589 qed
  5590 
  5591 lemma inter_interval: fixes a :: "'a::ordered_euclidean_space" shows
  5592  "{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)}"
  5593   unfolding set_eq_iff and Int_iff and mem_interval by auto
  5594 
  5595 lemma disjoint_interval: fixes a::"'a::ordered_euclidean_space" shows
  5596   "{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
  5597   "{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
  5598   "{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
  5599   "{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)
  5600 proof-
  5601   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"
  5602   have **: "\<And>P Q. (\<And>i :: 'a. i \<in> Basis \<Longrightarrow> Q ?z i \<Longrightarrow> P i) \<Longrightarrow>
  5603       (\<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)" 
  5604     by blast
  5605   note * = set_eq_iff Int_iff empty_iff mem_interval ball_conj_distrib[symmetric] eq_False ball_simps(10)
  5606   show ?th1 unfolding * by (intro **) auto
  5607   show ?th2 unfolding * by (intro **) auto
  5608   show ?th3 unfolding * by (intro **) auto
  5609   show ?th4 unfolding * by (intro **) auto
  5610 qed
  5611 
  5612 (* Moved interval_open_subset_closed a bit upwards *)
  5613 
  5614 lemma open_interval[intro]:
  5615   fixes a b :: "'a::ordered_euclidean_space" shows "open {a<..<b}"
  5616 proof-
  5617   have "open (\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) -` {a\<bullet>i<..<b\<bullet>i})"
  5618     by (intro open_INT finite_lessThan ballI continuous_open_vimage allI
  5619       linear_continuous_at open_real_greaterThanLessThan finite_Basis bounded_linear_inner_left)
  5620   also have "(\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) -` {a\<bullet>i<..<b\<bullet>i}) = {a<..<b}"
  5621     by (auto simp add: eucl_less [where 'a='a])
  5622   finally show "open {a<..<b}" .
  5623 qed
  5624 
  5625 lemma closed_interval[intro]:
  5626   fixes a b :: "'a::ordered_euclidean_space" shows "closed {a .. b}"
  5627 proof-
  5628   have "closed (\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) -` {a\<bullet>i .. b\<bullet>i})"
  5629     by (intro closed_INT ballI continuous_closed_vimage allI
  5630       linear_continuous_at closed_real_atLeastAtMost finite_Basis bounded_linear_inner_left)
  5631   also have "(\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) -` {a\<bullet>i .. b\<bullet>i}) = {a .. b}"
  5632     by (auto simp add: eucl_le [where 'a='a])
  5633   finally show "closed {a .. b}" .
  5634 qed
  5635 
  5636 lemma interior_closed_interval [intro]:
  5637   fixes a b :: "'a::ordered_euclidean_space"
  5638   shows "interior {a..b} = {a<..<b}" (is "?L = ?R")
  5639 proof(rule subset_antisym)
  5640   show "?R \<subseteq> ?L" using interval_open_subset_closed open_interval
  5641     by (rule interior_maximal)
  5642 next
  5643   { fix x assume "x \<in> interior {a..b}"
  5644     then obtain s where s:"open s" "x \<in> s" "s \<subseteq> {a..b}" ..
  5645     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
  5646     { fix i :: 'a assume i:"i\<in>Basis"
  5647       have "dist (x - (e / 2) *\<^sub>R i) x < e"
  5648            "dist (x + (e / 2) *\<^sub>R i) x < e"
  5649         unfolding dist_norm apply auto
  5650         unfolding norm_minus_cancel using norm_Basis[OF i] `e>0` by auto
  5651       hence "a \<bullet> i \<le> (x - (e / 2) *\<^sub>R i) \<bullet> i"
  5652                      "(x + (e / 2) *\<^sub>R i) \<bullet> i \<le> b \<bullet> i"
  5653         using e[THEN spec[where x="x - (e/2) *\<^sub>R i"]]
  5654         and   e[THEN spec[where x="x + (e/2) *\<^sub>R i"]]
  5655         unfolding mem_interval using i by blast+
  5656       hence "a \<bullet> i < x \<bullet> i" and "x \<bullet> i < b \<bullet> i"
  5657         using `e>0` i by (auto simp: inner_diff_left inner_Basis inner_add_left) }
  5658     hence "x \<in> {a<..<b}" unfolding mem_interval by auto  }
  5659   thus "?L \<subseteq> ?R" ..
  5660 qed
  5661 
  5662 lemma bounded_closed_interval: fixes a :: "'a::ordered_euclidean_space" shows "bounded {a .. b}"
  5663 proof-
  5664   let ?b = "\<Sum>i\<in>Basis. \<bar>a\<bullet>i\<bar> + \<bar>b\<bullet>i\<bar>"
  5665   { fix x::"'a" assume x:"\<forall>i\<in>Basis. a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i"
  5666     { fix i :: 'a assume "i\<in>Basis"
  5667       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  }
  5668     hence "(\<Sum>i\<in>Basis. \<bar>x \<bullet> i\<bar>) \<le> ?b" apply-apply(rule setsum_mono) by auto
  5669     hence "norm x \<le> ?b" using norm_le_l1[of x] by auto  }
  5670   thus ?thesis unfolding interval and bounded_iff by auto
  5671 qed
  5672 
  5673 lemma bounded_interval: fixes a :: "'a::ordered_euclidean_space" shows
  5674  "bounded {a .. b} \<and> bounded {a<..<b}"
  5675   using bounded_closed_interval[of a b]
  5676   using interval_open_subset_closed[of a b]
  5677   using bounded_subset[of "{a..b}" "{a<..<b}"]
  5678   by simp
  5679 
  5680 lemma not_interval_univ: fixes a :: "'a::ordered_euclidean_space" shows
  5681  "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"
  5682   using bounded_interval[of a b] by auto
  5683 
  5684 lemma compact_interval: fixes a :: "'a::ordered_euclidean_space" shows "compact {a .. b}"
  5685   using bounded_closed_imp_seq_compact[of "{a..b}"] using bounded_interval[of a b]
  5686   by (auto simp: compact_eq_seq_compact_metric)
  5687 
  5688 lemma open_interval_midpoint: fixes a :: "'a::ordered_euclidean_space"
  5689   assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"
  5690 proof-
  5691   { fix i :: 'a assume "i\<in>Basis"
  5692     hence "a \<bullet> i < ((1 / 2) *\<^sub>R (a + b)) \<bullet> i \<and> ((1 / 2) *\<^sub>R (a + b)) \<bullet> i < b \<bullet> i"
  5693       using assms[unfolded interval_ne_empty, THEN bspec[where x=i]] by (auto simp: inner_add_left)  }
  5694   thus ?thesis unfolding mem_interval by auto
  5695 qed
  5696 
  5697 lemma open_closed_interval_convex: fixes x :: "'a::ordered_euclidean_space"
  5698   assumes x:"x \<in> {a<..<b}" and y:"y \<in> {a .. b}" and e:"0 < e" "e \<le> 1"
  5699   shows "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<in> {a<..<b}"
  5700 proof-
  5701   { fix i :: 'a assume i:"i\<in>Basis"
  5702     have "a \<bullet> i = e * (a \<bullet> i) + (1 - e) * (a \<bullet> i)" unfolding left_diff_distrib by simp
  5703     also have "\<dots> < e * (x \<bullet> i) + (1 - e) * (y \<bullet> i)" apply(rule add_less_le_mono)
  5704       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
  5705       using x unfolding mem_interval using i apply simp
  5706       using y unfolding mem_interval using i apply simp
  5707       done
  5708     finally have "a \<bullet> i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i" unfolding inner_simps by auto
  5709     moreover {
  5710     have "b \<bullet> i = e * (b\<bullet>i) + (1 - e) * (b\<bullet>i)" unfolding left_diff_distrib by simp
  5711     also have "\<dots> > e * (x \<bullet> i) + (1 - e) * (y \<bullet> i)" apply(rule add_less_le_mono)
  5712       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all
  5713       using x unfolding mem_interval using i apply simp
  5714       using y unfolding mem_interval using i apply simp
  5715       done
  5716     finally have "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i < b \<bullet> i" unfolding inner_simps by auto
  5717     } ultimately have "a \<bullet> i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i \<and> (e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i < b \<bullet> i" by auto }
  5718   thus ?thesis unfolding mem_interval by auto
  5719 qed
  5720 
  5721 lemma closure_open_interval: fixes a :: "'a::ordered_euclidean_space"
  5722   assumes "{a<..<b} \<noteq> {}"
  5723   shows "closure {a<..<b} = {a .. b}"
  5724 proof-
  5725   have ab:"a < b" using assms[unfolded interval_ne_empty] apply(subst eucl_less) by auto
  5726   let ?c = "(1 / 2) *\<^sub>R (a + b)"
  5727   { fix x assume as:"x \<in> {a .. b}"
  5728     def f == "\<lambda>n::nat. x + (inverse (real n + 1)) *\<^sub>R (?c - x)"
  5729     { fix n assume fn:"f n < b \<longrightarrow> a < f n \<longrightarrow> f n = x" and xc:"x \<noteq> ?c"
  5730       have *:"0 < inverse (real n + 1)" "inverse (real n + 1) \<le> 1" unfolding inverse_le_1_iff by auto
  5731       have "(inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b)) + (1 - inverse (real n + 1)) *\<^sub>R x =
  5732         x + (inverse (real n + 1)) *\<^sub>R (((1 / 2) *\<^sub>R (a + b)) - x)"
  5733         by (auto simp add: algebra_simps)
  5734       hence "f n < b" and "a < f n" using open_closed_interval_convex[OF open_interval_midpoint[OF assms] as *] unfolding f_def by auto
  5735       hence False using fn unfolding f_def using xc by auto  }
  5736     moreover
  5737     { assume "\<not> (f ---> x) sequentially"
  5738       { fix e::real assume "e>0"
  5739         hence "\<exists>N::nat. inverse (real (N + 1)) < e" using real_arch_inv[of e] apply (auto simp add: Suc_pred') apply(rule_tac x="n - 1" in exI) by auto
  5740         then obtain N::nat where "inverse (real (N + 1)) < e" by auto
  5741         hence "\<forall>n\<ge>N. inverse (real n + 1) < e" by (auto, metis Suc_le_mono le_SucE less_imp_inverse_less nat_le_real_less order_less_trans real_of_nat_Suc real_of_nat_Suc_gt_zero)
  5742         hence "\<exists>N::nat. \<forall>n\<ge>N. inverse (real n + 1) < e" by auto  }
  5743       hence "((\<lambda>n. inverse (real n + 1)) ---> 0) sequentially"
  5744         unfolding LIMSEQ_def by(auto simp add: dist_norm)
  5745       hence "(f ---> x) sequentially" unfolding f_def
  5746         using tendsto_add[OF tendsto_const, of "\<lambda>n::nat. (inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b) - x)" 0 sequentially x]
  5747         using tendsto_scaleR [OF _ tendsto_const, of "\<lambda>n::nat. inverse (real n + 1)" 0 sequentially "((1 / 2) *\<^sub>R (a + b) - x)"] by auto  }
  5748     ultimately have "x \<in> closure {a<..<b}"
  5749       using as and open_interval_midpoint[OF assms] unfolding closure_def unfolding islimpt_sequential by(cases "x=?c")auto  }
  5750   thus ?thesis using closure_minimal[OF interval_open_subset_closed closed_interval, of a b] by blast
  5751 qed
  5752 
  5753 lemma bounded_subset_open_interval_symmetric: fixes s::"('a::ordered_euclidean_space) set"
  5754   assumes "bounded s"  shows "\<exists>a. s \<subseteq> {-a<..<a}"
  5755 proof-
  5756   obtain b where "b>0" and b:"\<forall>x\<in>s. norm x \<le> b" using assms[unfolded bounded_pos] by auto
  5757   def a \<equiv> "(\<Sum>i\<in>Basis. (b + 1) *\<^sub>R i)::'a"
  5758   { fix x assume "x\<in>s"
  5759     fix i :: 'a assume i:"i\<in>Basis"
  5760     hence "(-a)\<bullet>i < x\<bullet>i" and "x\<bullet>i < a\<bullet>i" using b[THEN bspec[where x=x], OF `x\<in>s`]
  5761       and Basis_le_norm[OF i, of x] unfolding inner_simps and a_def by auto }
  5762   thus ?thesis by(auto intro: exI[where x=a] simp add: eucl_less[where 'a='a])
  5763 qed
  5764 
  5765 lemma bounded_subset_open_interval:
  5766   fixes s :: "('a::ordered_euclidean_space) set"
  5767   shows "bounded s ==> (\<exists>a b. s \<subseteq> {a<..<b})"
  5768   by (auto dest!: bounded_subset_open_interval_symmetric)
  5769 
  5770 lemma bounded_subset_closed_interval_symmetric:
  5771   fixes s :: "('a::ordered_euclidean_space) set"
  5772   assumes "bounded s" shows "\<exists>a. s \<subseteq> {-a .. a}"
  5773 proof-
  5774   obtain a where "s \<subseteq> {- a<..<a}" using bounded_subset_open_interval_symmetric[OF assms] by auto
  5775   thus ?thesis using interval_open_subset_closed[of "-a" a] by auto
  5776 qed
  5777 
  5778 lemma bounded_subset_closed_interval:
  5779   fixes s :: "('a::ordered_euclidean_space) set"
  5780   shows "bounded s ==> (\<exists>a b. s \<subseteq> {a .. b})"
  5781   using bounded_subset_closed_interval_symmetric[of s] by auto
  5782 
  5783 lemma frontier_closed_interval:
  5784   fixes a b :: "'a::ordered_euclidean_space"
  5785   shows "frontier {a .. b} = {a .. b} - {a<..<b}"
  5786   unfolding frontier_def unfolding interior_closed_interval and closure_closed[OF closed_interval] ..
  5787 
  5788 lemma frontier_open_interval:
  5789   fixes a b :: "'a::ordered_euclidean_space"
  5790   shows "frontier {a<..<b} = (if {a<..<b} = {} then {} else {a .. b} - {a<..<b})"
  5791 proof(cases "{a<..<b} = {}")
  5792   case True thus ?thesis using frontier_empty by auto
  5793 next
  5794   case False thus ?thesis unfolding frontier_def and closure_open_interval[OF False] and interior_open[OF open_interval] by auto
  5795 qed
  5796 
  5797 lemma inter_interval_mixed_eq_empty: fixes a :: "'a::ordered_euclidean_space"
  5798   assumes "{c<..<d} \<noteq> {}"  shows "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> {a<..<b} \<inter> {c<..<d} = {}"
  5799   unfolding closure_open_interval[OF assms, THEN sym] unfolding open_inter_closure_eq_empty[OF open_interval] ..
  5800 
  5801 
  5802 (* Some stuff for half-infinite intervals too; FIXME: notation?  *)
  5803 
  5804 lemma closed_interval_left: fixes b::"'a::euclidean_space"
  5805   shows "closed {x::'a. \<forall>i\<in>Basis. x\<bullet>i \<le> b\<bullet>i}"
  5806 proof-
  5807   { fix i :: 'a assume i:"i\<in>Basis"
  5808     fix x::"'a" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i\<in>Basis. x \<bullet> i \<le> b \<bullet> i}. x' \<noteq> x \<and> dist x' x < e"
  5809     { assume "x\<bullet>i > b\<bullet>i"
  5810       then obtain y where "y \<bullet> i \<le> b \<bullet> i"  "y \<noteq> x"  "dist y x < x\<bullet>i - b\<bullet>i"
  5811         using x[THEN spec[where x="x\<bullet>i - b\<bullet>i"]] using i by auto
  5812       hence False using Basis_le_norm[OF i, of "y - x"] unfolding dist_norm inner_simps using i 
  5813         by auto }
  5814     hence "x\<bullet>i \<le> b\<bullet>i" by(rule ccontr)auto  }
  5815   thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
  5816 qed
  5817 
  5818 lemma closed_interval_right: fixes a::"'a::euclidean_space"
  5819   shows "closed {x::'a. \<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i}"
  5820 proof-
  5821   { fix i :: 'a assume i:"i\<in>Basis"
  5822     fix x::"'a" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i\<in>Basis. a \<bullet> i \<le> x \<bullet> i}. x' \<noteq> x \<and> dist x' x < e"
  5823     { assume "a\<bullet>i > x\<bullet>i"
  5824       then obtain y where "a \<bullet> i \<le> y \<bullet> i"  "y \<noteq> x"  "dist y x < a\<bullet>i - x\<bullet>i"
  5825         using x[THEN spec[where x="a\<bullet>i - x\<bullet>i"]] i by auto
  5826       hence False using Basis_le_norm[OF i, of "y - x"] unfolding dist_norm inner_simps by auto }
  5827     hence "a\<bullet>i \<le> x\<bullet>i" by(rule ccontr)auto  }
  5828   thus ?thesis unfolding closed_limpt unfolding islimpt_approachable by blast
  5829 qed
  5830 
  5831 lemma open_box: "open (box a b)"
  5832 proof -
  5833   have "open (\<Inter>i\<in>Basis. (op \<bullet> i) -` {a \<bullet> i <..< b \<bullet> i})"
  5834     by (auto intro!: continuous_open_vimage continuous_inner continuous_at_id continuous_const)
  5835   also have "(\<Inter>i\<in>Basis. (op \<bullet> i) -` {a \<bullet> i <..< b \<bullet> i}) = box a b"
  5836     by (auto simp add: box_def inner_commute)
  5837   finally show ?thesis .
  5838 qed
  5839 
  5840 instance euclidean_space \<subseteq> second_countable_topology
  5841 proof
  5842   def a \<equiv> "\<lambda>f :: 'a \<Rightarrow> (real \<times> real). \<Sum>i\<in>Basis. fst (f i) *\<^sub>R i"
  5843   then have a: "\<And>f. (\<Sum>i\<in>Basis. fst (f i) *\<^sub>R i) = a f" by simp
  5844   def b \<equiv> "\<lambda>f :: 'a \<Rightarrow> (real \<times> real). \<Sum>i\<in>Basis. snd (f i) *\<^sub>R i"
  5845   then have b: "\<And>f. (\<Sum>i\<in>Basis. snd (f i) *\<^sub>R i) = b f" by simp
  5846   def B \<equiv> "(\<lambda>f. box (a f) (b f)) ` (Basis \<rightarrow>\<^isub>E (\<rat> \<times> \<rat>))"
  5847 
  5848   have "countable B" unfolding B_def 
  5849     by (intro countable_image countable_PiE finite_Basis countable_SIGMA countable_rat)
  5850   moreover
  5851   have "Ball B open" by (simp add: B_def open_box)
  5852   moreover have "(\<forall>A. open A \<longrightarrow> (\<exists>B'\<subseteq>B. \<Union>B' = A))"
  5853   proof safe
  5854     fix A::"'a set" assume "open A"
  5855     show "\<exists>B'\<subseteq>B. \<Union>B' = A"
  5856       apply (rule exI[of _ "{b\<in>B. b \<subseteq> A}"])
  5857       apply (subst (3) open_UNION_box[OF `open A`])
  5858       apply (auto simp add: a b B_def)
  5859       done
  5860   qed
  5861   ultimately
  5862   show "\<exists>B::'a set set. countable B \<and> topological_basis B" unfolding topological_basis_def by blast
  5863 qed
  5864 
  5865 instance ordered_euclidean_space \<subseteq> polish_space ..
  5866 
  5867 text {* Intervals in general, including infinite and mixtures of open and closed. *}
  5868 
  5869 definition "is_interval (s::('a::euclidean_space) set) \<longleftrightarrow>
  5870   (\<forall>a\<in>s. \<forall>b\<in>s. \<forall>x. (\<forall>i\<in>Basis. ((a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i) \<or> (b\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> a\<bullet>i))) \<longrightarrow> x \<in> s)"
  5871 
  5872 lemma is_interval_interval: "is_interval {a .. b::'a::ordered_euclidean_space}" (is ?th1)
  5873   "is_interval {a<..<b}" (is ?th2) proof -
  5874   show ?th1 ?th2  unfolding is_interval_def mem_interval Ball_def atLeastAtMost_iff
  5875     by(meson order_trans le_less_trans less_le_trans less_trans)+ qed
  5876 
  5877 lemma is_interval_empty:
  5878  "is_interval {}"
  5879   unfolding is_interval_def
  5880   by simp
  5881 
  5882 lemma is_interval_univ:
  5883  "is_interval UNIV"
  5884   unfolding is_interval_def
  5885   by simp
  5886 
  5887 
  5888 subsection {* Closure of halfspaces and hyperplanes *}
  5889 
  5890 lemma isCont_open_vimage:
  5891   assumes "\<And>x. isCont f x" and "open s" shows "open (f -` s)"
  5892 proof -
  5893   from assms(1) have "continuous_on UNIV f"
  5894     unfolding isCont_def continuous_on_def within_UNIV by simp
  5895   hence "open {x \<in> UNIV. f x \<in> s}"
  5896     using open_UNIV `open s` by (rule continuous_open_preimage)
  5897   thus "open (f -` s)"
  5898     by (simp add: vimage_def)
  5899 qed
  5900 
  5901 lemma isCont_closed_vimage:
  5902   assumes "\<And>x. isCont f x" and "closed s" shows "closed (f -` s)"
  5903   using assms unfolding closed_def vimage_Compl [symmetric]
  5904   by (rule isCont_open_vimage)
  5905 
  5906 lemma open_Collect_less:
  5907   fixes f g :: "'a::topological_space \<Rightarrow> real"
  5908   assumes f: "\<And>x. isCont f x"
  5909   assumes g: "\<And>x. isCont g x"
  5910   shows "open {x. f x < g x}"
  5911 proof -
  5912   have "open ((\<lambda>x. g x - f x) -` {0<..})"
  5913     using isCont_diff [OF g f] open_real_greaterThan
  5914     by (rule isCont_open_vimage)
  5915   also have "((\<lambda>x. g x - f x) -` {0<..}) = {x. f x < g x}"
  5916     by auto
  5917   finally show ?thesis .
  5918 qed
  5919 
  5920 lemma closed_Collect_le:
  5921   fixes f g :: "'a::topological_space \<Rightarrow> real"
  5922   assumes f: "\<And>x. isCont f x"
  5923   assumes g: "\<And>x. isCont g x"
  5924   shows "closed {x. f x \<le> g x}"
  5925 proof -
  5926   have "closed ((\<lambda>x. g x - f x) -` {0..})"
  5927     using isCont_diff [OF g f] closed_real_atLeast
  5928     by (rule isCont_closed_vimage)
  5929   also have "((\<lambda>x. g x - f x) -` {0..}) = {x. f x \<le> g x}"
  5930     by auto
  5931   finally show ?thesis .
  5932 qed
  5933 
  5934 lemma closed_Collect_eq:
  5935   fixes f g :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  5936   assumes f: "\<And>x. isCont f x"
  5937   assumes g: "\<And>x. isCont g x"
  5938   shows "closed {x. f x = g x}"
  5939 proof -
  5940   have "open {(x::'b, y::'b). x \<noteq> y}"
  5941     unfolding open_prod_def by (auto dest!: hausdorff)
  5942   hence "closed {(x::'b, y::'b). x = y}"
  5943     unfolding closed_def split_def Collect_neg_eq .
  5944   with isCont_Pair [OF f g]
  5945   have "closed ((\<lambda>x. (f x, g x)) -` {(x, y). x = y})"
  5946     by (rule isCont_closed_vimage)
  5947   also have "\<dots> = {x. f x = g x}" by auto
  5948   finally show ?thesis .
  5949 qed
  5950 
  5951 lemma continuous_at_inner: "continuous (at x) (inner a)"
  5952   unfolding continuous_at by (intro tendsto_intros)
  5953 
  5954 lemma closed_halfspace_le: "closed {x. inner a x \<le> b}"
  5955   by (simp add: closed_Collect_le)
  5956 
  5957 lemma closed_halfspace_ge: "closed {x. inner a x \<ge> b}"
  5958   by (simp add: closed_Collect_le)
  5959 
  5960 lemma closed_hyperplane: "closed {x. inner a x = b}"
  5961   by (simp add: closed_Collect_eq)
  5962 
  5963 lemma closed_halfspace_component_le:
  5964   shows "closed {x::'a::euclidean_space. x\<bullet>i \<le> a}"
  5965   by (simp add: closed_Collect_le)
  5966 
  5967 lemma closed_halfspace_component_ge:
  5968   shows "closed {x::'a::euclidean_space. x\<bullet>i \<ge> a}"
  5969   by (simp add: closed_Collect_le)
  5970 
  5971 text {* Openness of halfspaces. *}
  5972 
  5973 lemma open_halfspace_lt: "open {x. inner a x < b}"
  5974   by (simp add: open_Collect_less)
  5975 
  5976 lemma open_halfspace_gt: "open {x. inner a x > b}"
  5977   by (simp add: open_Collect_less)
  5978 
  5979 lemma open_halfspace_component_lt:
  5980   shows "open {x::'a::euclidean_space. x\<bullet>i < a}"
  5981   by (simp add: open_Collect_less)
  5982 
  5983 lemma open_halfspace_component_gt:
  5984   shows "open {x::'a::euclidean_space. x\<bullet>i > a}"
  5985   by (simp add: open_Collect_less)
  5986 
  5987 text{* Instantiation for intervals on @{text ordered_euclidean_space} *}
  5988 
  5989 lemma eucl_lessThan_eq_halfspaces:
  5990   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5991   shows "{..<a} = (\<Inter>i\<in>Basis. {x. x \<bullet> i < a \<bullet> i})"
  5992  by (auto simp: eucl_less[where 'a='a])
  5993 
  5994 lemma eucl_greaterThan_eq_halfspaces:
  5995   fixes a :: "'a\<Colon>ordered_euclidean_space"
  5996   shows "{a<..} = (\<Inter>i\<in>Basis. {x. a \<bullet> i < x \<bullet> i})"
  5997  by (auto simp: eucl_less[where 'a='a])
  5998 
  5999 lemma eucl_atMost_eq_halfspaces:
  6000   fixes a :: "'a\<Colon>ordered_euclidean_space"
  6001   shows "{.. a} = (\<Inter>i\<in>Basis. {x. x \<bullet> i \<le> a \<bullet> i})"
  6002  by (auto simp: eucl_le[where 'a='a])
  6003 
  6004 lemma eucl_atLeast_eq_halfspaces:
  6005   fixes a :: "'a\<Colon>ordered_euclidean_space"
  6006   shows "{a ..} = (\<Inter>i\<in>Basis. {x. a \<bullet> i \<le> x \<bullet> i})"
  6007  by (auto simp: eucl_le[where 'a='a])
  6008 
  6009 lemma open_eucl_lessThan[simp, intro]:
  6010   fixes a :: "'a\<Colon>ordered_euclidean_space"
  6011   shows "open {..< a}"
  6012   by (auto simp: eucl_lessThan_eq_halfspaces open_halfspace_component_lt)
  6013 
  6014 lemma open_eucl_greaterThan[simp, intro]:
  6015   fixes a :: "'a\<Colon>ordered_euclidean_space"
  6016   shows "open {a <..}"
  6017   by (auto simp: eucl_greaterThan_eq_halfspaces open_halfspace_component_gt)
  6018 
  6019 lemma closed_eucl_atMost[simp, intro]:
  6020   fixes a :: "'a\<Colon>ordered_euclidean_space"
  6021   shows "closed {.. a}"
  6022   unfolding eucl_atMost_eq_halfspaces
  6023   by (simp add: closed_INT closed_Collect_le)
  6024 
  6025 lemma closed_eucl_atLeast[simp, intro]:
  6026   fixes a :: "'a\<Colon>ordered_euclidean_space"
  6027   shows "closed {a ..}"
  6028   unfolding eucl_atLeast_eq_halfspaces
  6029   by (simp add: closed_INT closed_Collect_le)
  6030 
  6031 text {* This gives a simple derivation of limit component bounds. *}
  6032 
  6033 lemma Lim_component_le: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
  6034   assumes "(f ---> l) net" "\<not> (trivial_limit net)"  "eventually (\<lambda>x. f(x)\<bullet>i \<le> b) net"
  6035   shows "l\<bullet>i \<le> b"
  6036   by (rule tendsto_le[OF assms(2) tendsto_const tendsto_inner[OF assms(1) tendsto_const] assms(3)])
  6037 
  6038 lemma Lim_component_ge: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
  6039   assumes "(f ---> l) net"  "\<not> (trivial_limit net)"  "eventually (\<lambda>x. b \<le> (f x)\<bullet>i) net"
  6040   shows "b \<le> l\<bullet>i"
  6041   by (rule tendsto_le[OF assms(2) tendsto_inner[OF assms(1) tendsto_const] tendsto_const assms(3)])
  6042 
  6043 lemma Lim_component_eq: fixes f :: "'a \<Rightarrow> 'b::euclidean_space"
  6044   assumes net:"(f ---> l) net" "~(trivial_limit net)" and ev:"eventually (\<lambda>x. f(x)\<bullet>i = b) net"
  6045   shows "l\<bullet>i = b"
  6046   using ev[unfolded order_eq_iff eventually_conj_iff]
  6047   using Lim_component_ge[OF net, of b i] and Lim_component_le[OF net, of i b] by auto
  6048 
  6049 text{* Limits relative to a union.                                               *}
  6050 
  6051 lemma eventually_within_Un:
  6052   "eventually P (net within (s \<union> t)) \<longleftrightarrow>
  6053     eventually P (net within s) \<and> eventually P (net within t)"
  6054   unfolding Limits.eventually_within
  6055   by (auto elim!: eventually_rev_mp)
  6056 
  6057 lemma Lim_within_union:
  6058  "(f ---> l) (net within (s \<union> t)) \<longleftrightarrow>
  6059   (f ---> l) (net within s) \<and> (f ---> l) (net within t)"
  6060   unfolding tendsto_def
  6061   by (auto simp add: eventually_within_Un)
  6062 
  6063 lemma Lim_topological:
  6064  "(f ---> l) net \<longleftrightarrow>
  6065         trivial_limit net \<or>
  6066         (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net)"
  6067   unfolding tendsto_def trivial_limit_eq by auto
  6068 
  6069 lemma continuous_on_union:
  6070   assumes "closed s" "closed t" "continuous_on s f" "continuous_on t f"
  6071   shows "continuous_on (s \<union> t) f"
  6072   using assms unfolding continuous_on Lim_within_union
  6073   unfolding Lim_topological trivial_limit_within closed_limpt by auto
  6074 
  6075 lemma continuous_on_cases:
  6076   assumes "closed s" "closed t" "continuous_on s f" "continuous_on t g"
  6077           "\<forall>x. (x\<in>s \<and> \<not> P x) \<or> (x \<in> t \<and> P x) \<longrightarrow> f x = g x"
  6078   shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
  6079 proof-
  6080   let ?h = "(\<lambda>x. if P x then f x else g x)"
  6081   have "\<forall>x\<in>s. f x = (if P x then f x else g x)" using assms(5) by auto
  6082   hence "continuous_on s ?h" using continuous_on_eq[of s f ?h] using assms(3) by auto
  6083   moreover
  6084   have "\<forall>x\<in>t. g x = (if P x then f x else g x)" using assms(5) by auto
  6085   hence "continuous_on t ?h" using continuous_on_eq[of t g ?h] using assms(4) by auto
  6086   ultimately show ?thesis using continuous_on_union[OF assms(1,2), of ?h] by auto
  6087 qed
  6088 
  6089 
  6090 text{* Some more convenient intermediate-value theorem formulations.             *}
  6091 
  6092 lemma connected_ivt_hyperplane:
  6093   assumes "connected s" "x \<in> s" "y \<in> s" "inner a x \<le> b" "b \<le> inner a y"
  6094   shows "\<exists>z \<in> s. inner a z = b"
  6095 proof(rule ccontr)
  6096   assume as:"\<not> (\<exists>z\<in>s. inner a z = b)"
  6097   let ?A = "{x. inner a x < b}"
  6098   let ?B = "{x. inner a x > b}"
  6099   have "open ?A" "open ?B" using open_halfspace_lt and open_halfspace_gt by auto
  6100   moreover have "?A \<inter> ?B = {}" by auto
  6101   moreover have "s \<subseteq> ?A \<union> ?B" using as by auto
  6102   ultimately show False using assms(1)[unfolded connected_def not_ex, THEN spec[where x="?A"], THEN spec[where x="?B"]] and assms(2-5) by auto
  6103 qed
  6104 
  6105 lemma connected_ivt_component: fixes x::"'a::euclidean_space" shows
  6106  "connected s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> x\<bullet>k \<le> a \<Longrightarrow> a \<le> y\<bullet>k \<Longrightarrow> (\<exists>z\<in>s.  z\<bullet>k = a)"
  6107   using connected_ivt_hyperplane[of s x y "k::'a" a] by (auto simp: inner_commute)
  6108 
  6109 
  6110 subsection {* Homeomorphisms *}
  6111 
  6112 definition "homeomorphism s t f g \<equiv>
  6113      (\<forall>x\<in>s. (g(f x) = x)) \<and> (f ` s = t) \<and> continuous_on s f \<and>
  6114      (\<forall>y\<in>t. (f(g y) = y)) \<and> (g ` t = s) \<and> continuous_on t g"
  6115 
  6116 definition
  6117   homeomorphic :: "'a::topological_space set \<Rightarrow> 'b::topological_space set \<Rightarrow> bool"
  6118     (infixr "homeomorphic" 60) where
  6119   homeomorphic_def: "s homeomorphic t \<equiv> (\<exists>f g. homeomorphism s t f g)"
  6120 
  6121 lemma homeomorphic_refl: "s homeomorphic s"
  6122   unfolding homeomorphic_def
  6123   unfolding homeomorphism_def
  6124   using continuous_on_id
  6125   apply(rule_tac x = "(\<lambda>x. x)" in exI)
  6126   apply(rule_tac x = "(\<lambda>x. x)" in exI)
  6127   by blast
  6128 
  6129 lemma homeomorphic_sym:
  6130  "s homeomorphic t \<longleftrightarrow> t homeomorphic s"
  6131 unfolding homeomorphic_def
  6132 unfolding homeomorphism_def
  6133 by blast 
  6134 
  6135 lemma homeomorphic_trans:
  6136   assumes "s homeomorphic t" "t homeomorphic u" shows "s homeomorphic u"
  6137 proof-
  6138   obtain f1 g1 where fg1:"\<forall>x\<in>s. g1 (f1 x) = x"  "f1 ` s = t" "continuous_on s f1" "\<forall>y\<in>t. f1 (g1 y) = y" "g1 ` t = s" "continuous_on t g1"
  6139     using assms(1) unfolding homeomorphic_def homeomorphism_def by auto
  6140   obtain f2 g2 where fg2:"\<forall>x\<in>t. g2 (f2 x) = x"  "f2 ` t = u" "continuous_on t f2" "\<forall>y\<in>u. f2 (g2 y) = y" "g2 ` u = t" "continuous_on u g2"
  6141     using assms(2) unfolding homeomorphic_def homeomorphism_def by auto
  6142 
  6143   { fix x assume "x\<in>s" hence "(g1 \<circ> g2) ((f2 \<circ> f1) x) = x" using fg1(1)[THEN bspec[where x=x]] and fg2(1)[THEN bspec[where x="f1 x"]] and fg1(2) by auto }
  6144   moreover have "(f2 \<circ> f1) ` s = u" using fg1(2) fg2(2) by auto
  6145   moreover have "continuous_on s (f2 \<circ> f1)" using continuous_on_compose[OF fg1(3)] and fg2(3) unfolding fg1(2) by auto
  6146   moreover { fix y assume "y\<in>u" hence "(f2 \<circ> f1) ((g1 \<circ> g2) y) = y" using fg2(4)[THEN bspec[where x=y]] and fg1(4)[THEN bspec[where x="g2 y"]] and fg2(5) by auto }
  6147   moreover have "(g1 \<circ> g2) ` u = s" using fg1(5) fg2(5) by auto
  6148   moreover have "continuous_on u (g1 \<circ> g2)" using continuous_on_compose[OF fg2(6)] and fg1(6)  unfolding fg2(5) by auto
  6149   ultimately show ?thesis unfolding homeomorphic_def homeomorphism_def apply(rule_tac x="f2 \<circ> f1" in exI) apply(rule_tac x="g1 \<circ> g2" in exI) by auto
  6150 qed
  6151 
  6152 lemma homeomorphic_minimal:
  6153  "s homeomorphic t \<longleftrightarrow>
  6154     (\<exists>f g. (\<forall>x\<in>s. f(x) \<in> t \<and> (g(f(x)) = x)) \<and>
  6155            (\<forall>y\<in>t. g(y) \<in> s \<and> (f(g(y)) = y)) \<and>
  6156            continuous_on s f \<and> continuous_on t g)"
  6157 unfolding homeomorphic_def homeomorphism_def
  6158 apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI)
  6159 apply auto apply (rule_tac x=f in exI) apply (rule_tac x=g in exI) apply auto
  6160 unfolding image_iff
  6161 apply(erule_tac x="g x" in ballE) apply(erule_tac x="x" in ballE)
  6162 apply auto apply(rule_tac x="g x" in bexI) apply auto
  6163 apply(erule_tac x="f x" in ballE) apply(erule_tac x="x" in ballE)
  6164 apply auto apply(rule_tac x="f x" in bexI) by auto
  6165 
  6166 text {* Relatively weak hypotheses if a set is compact. *}
  6167 
  6168 lemma homeomorphism_compact:
  6169   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  6170   assumes "compact s" "continuous_on s f"  "f ` s = t"  "inj_on f s"
  6171   shows "\<exists>g. homeomorphism s t f g"
  6172 proof-
  6173   def g \<equiv> "\<lambda>x. SOME y. y\<in>s \<and> f y = x"
  6174   have g:"\<forall>x\<in>s. g (f x) = x" using assms(3) assms(4)[unfolded inj_on_def] unfolding g_def by auto
  6175   { fix y assume "y\<in>t"
  6176     then obtain x where x:"f x = y" "x\<in>s" using assms(3) by auto
  6177     hence "g (f x) = x" using g by auto
  6178     hence "f (g y) = y" unfolding x(1)[THEN sym] by auto  }
  6179   hence g':"\<forall>x\<in>t. f (g x) = x" by auto
  6180   moreover
  6181   { fix x
  6182     have "x\<in>s \<Longrightarrow> x \<in> g ` t" using g[THEN bspec[where x=x]] unfolding image_iff using assms(3) by(auto intro!: bexI[where x="f x"])
  6183     moreover
  6184     { assume "x\<in>g ` t"
  6185       then obtain y where y:"y\<in>t" "g y = x" by auto
  6186       then obtain x' where x':"x'\<in>s" "f x' = y" using assms(3) by auto
  6187       hence "x \<in> s" unfolding g_def using someI2[of "\<lambda>b. b\<in>s \<and> f b = y" x' "\<lambda>x. x\<in>s"] unfolding y(2)[THEN sym] and g_def by auto }
  6188     ultimately have "x\<in>s \<longleftrightarrow> x \<in> g ` t" ..  }
  6189   hence "g ` t = s" by auto
  6190   ultimately
  6191   show ?thesis unfolding homeomorphism_def homeomorphic_def
  6192     apply(rule_tac x=g in exI) using g and assms(3) and continuous_on_inv[OF assms(2,1), of g, unfolded assms(3)] and assms(2) by auto
  6193 qed
  6194 
  6195 lemma homeomorphic_compact:
  6196   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  6197   shows "compact s \<Longrightarrow> continuous_on s f \<Longrightarrow> (f ` s = t) \<Longrightarrow> inj_on f s
  6198           \<Longrightarrow> s homeomorphic t"
  6199   unfolding homeomorphic_def by (metis homeomorphism_compact)
  6200 
  6201 text{* Preservation of topological properties.                                   *}
  6202 
  6203 lemma homeomorphic_compactness:
  6204  "s homeomorphic t ==> (compact s \<longleftrightarrow> compact t)"
  6205 unfolding homeomorphic_def homeomorphism_def
  6206 by (metis compact_continuous_image)
  6207 
  6208 text{* Results on translation, scaling etc.                                      *}
  6209 
  6210 lemma homeomorphic_scaling:
  6211   fixes s :: "'a::real_normed_vector set"
  6212   assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. c *\<^sub>R x) ` s)"
  6213   unfolding homeomorphic_minimal
  6214   apply(rule_tac x="\<lambda>x. c *\<^sub>R x" in exI)
  6215   apply(rule_tac x="\<lambda>x. (1 / c) *\<^sub>R x" in exI)
  6216   using assms by (auto simp add: continuous_on_intros)
  6217 
  6218 lemma homeomorphic_translation:
  6219   fixes s :: "'a::real_normed_vector set"
  6220   shows "s homeomorphic ((\<lambda>x. a + x) ` s)"
  6221   unfolding homeomorphic_minimal
  6222   apply(rule_tac x="\<lambda>x. a + x" in exI)
  6223   apply(rule_tac x="\<lambda>x. -a + x" in exI)
  6224   using continuous_on_add[OF continuous_on_const continuous_on_id] by auto
  6225 
  6226 lemma homeomorphic_affinity:
  6227   fixes s :: "'a::real_normed_vector set"
  6228   assumes "c \<noteq> 0"  shows "s homeomorphic ((\<lambda>x. a + c *\<^sub>R x) ` s)"
  6229 proof-
  6230   have *:"op + a ` op *\<^sub>R c ` s = (\<lambda>x. a + c *\<^sub>R x) ` s" by auto
  6231   show ?thesis
  6232     using homeomorphic_trans
  6233     using homeomorphic_scaling[OF assms, of s]
  6234     using homeomorphic_translation[of "(\<lambda>x. c *\<^sub>R x) ` s" a] unfolding * by auto
  6235 qed
  6236 
  6237 lemma homeomorphic_balls:
  6238   fixes a b ::"'a::real_normed_vector"
  6239   assumes "0 < d"  "0 < e"
  6240   shows "(ball a d) homeomorphic  (ball b e)" (is ?th)
  6241         "(cball a d) homeomorphic (cball b e)" (is ?cth)
  6242 proof-
  6243   show ?th unfolding homeomorphic_minimal
  6244     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
  6245     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
  6246     using assms apply (auto simp add: dist_commute)
  6247     unfolding dist_norm
  6248     apply (auto simp add: pos_divide_less_eq mult_strict_left_mono)
  6249     unfolding continuous_on
  6250     by (intro ballI tendsto_intros, simp)+
  6251 next
  6252   show ?cth unfolding homeomorphic_minimal
  6253     apply(rule_tac x="\<lambda>x. b + (e/d) *\<^sub>R (x - a)" in exI)
  6254     apply(rule_tac x="\<lambda>x. a + (d/e) *\<^sub>R (x - b)" in exI)
  6255     using assms apply (auto simp add: dist_commute)
  6256     unfolding dist_norm
  6257     apply (auto simp add: pos_divide_le_eq)
  6258     unfolding continuous_on
  6259     by (intro ballI tendsto_intros, simp)+
  6260 qed
  6261 
  6262 text{* "Isometry" (up to constant bounds) of injective linear map etc.           *}
  6263 
  6264 lemma cauchy_isometric:
  6265   fixes x :: "nat \<Rightarrow> 'a::euclidean_space"
  6266   assumes e:"0 < e" and s:"subspace s" and f:"bounded_linear f" and normf:"\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)" and xs:"\<forall>n::nat. x n \<in> s" and cf:"Cauchy(f o x)"
  6267   shows "Cauchy x"
  6268 proof-
  6269   interpret f: bounded_linear f by fact
  6270   { fix d::real assume "d>0"
  6271     then obtain N where N:"\<forall>n\<ge>N. norm (f (x n) - f (x N)) < e * d"
  6272       using cf[unfolded cauchy o_def dist_norm, THEN spec[where x="e*d"]] and e and mult_pos_pos[of e d] by auto
  6273     { fix n assume "n\<ge>N"
  6274       have "e * norm (x n - x N) \<le> norm (f (x n - x N))"
  6275         using subspace_sub[OF s, of "x n" "x N"] using xs[THEN spec[where x=N]] and xs[THEN spec[where x=n]]
  6276         using normf[THEN bspec[where x="x n - x N"]] by auto
  6277       also have "norm (f (x n - x N)) < e * d"
  6278         using `N \<le> n` N unfolding f.diff[THEN sym] by auto
  6279       finally have "norm (x n - x N) < d" using `e>0` by simp }
  6280     hence "\<exists>N. \<forall>n\<ge>N. norm (x n - x N) < d" by auto }
  6281   thus ?thesis unfolding cauchy and dist_norm by auto
  6282 qed
  6283 
  6284 lemma complete_isometric_image:
  6285   fixes f :: "'a::euclidean_space => 'b::euclidean_space"
  6286   assumes "0 < e" and s:"subspace s" and f:"bounded_linear f" and normf:"\<forall>x\<in>s. norm(f x) \<ge> e * norm(x)" and cs:"complete s"
  6287   shows "complete(f ` s)"
  6288 proof-
  6289   { fix g assume as:"\<forall>n::nat. g n \<in> f ` s" and cfg:"Cauchy g"
  6290     then obtain x where "\<forall>n. x n \<in> s \<and> g n = f (x n)" 
  6291       using choice[of "\<lambda> n xa. xa \<in> s \<and> g n = f xa"] by auto
  6292     hence x:"\<forall>n. x n \<in> s"  "\<forall>n. g n = f (x n)" by auto
  6293     hence "f \<circ> x = g" unfolding fun_eq_iff by auto
  6294     then obtain l where "l\<in>s" and l:"(x ---> l) sequentially"
  6295       using cs[unfolded complete_def, THEN spec[where x="x"]]
  6296       using cauchy_isometric[OF `0<e` s f normf] and cfg and x(1) by auto
  6297     hence "\<exists>l\<in>f ` s. (g ---> l) sequentially"
  6298       using linear_continuous_at[OF f, unfolded continuous_at_sequentially, THEN spec[where x=x], of l]
  6299       unfolding `f \<circ> x = g` by auto  }
  6300   thus ?thesis unfolding complete_def by auto
  6301 qed
  6302 
  6303 lemma dist_0_norm:
  6304   fixes x :: "'a::real_normed_vector"
  6305   shows "dist 0 x = norm x"
  6306 unfolding dist_norm by simp
  6307 
  6308 lemma injective_imp_isometric: fixes f::"'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  6309   assumes s:"closed s"  "subspace s"  and f:"bounded_linear f" "\<forall>x\<in>s. (f x = 0) \<longrightarrow> (x = 0)"
  6310   shows "\<exists>e>0. \<forall>x\<in>s. norm (f x) \<ge> e * norm(x)"
  6311 proof(cases "s \<subseteq> {0::'a}")
  6312   case True
  6313   { fix x assume "x \<in> s"
  6314     hence "x = 0" using True by auto
  6315     hence "norm x \<le> norm (f x)" by auto  }
  6316   thus ?thesis by(auto intro!: exI[where x=1])
  6317 next
  6318   interpret f: bounded_linear f by fact
  6319   case False
  6320   then obtain a where a:"a\<noteq>0" "a\<in>s" by auto
  6321   from False have "s \<noteq> {}" by auto
  6322   let ?S = "{f x| x. (x \<in> s \<and> norm x = norm a)}"
  6323   let ?S' = "{x::'a. x\<in>s \<and> norm x = norm a}"
  6324   let ?S'' = "{x::'a. norm x = norm a}"
  6325 
  6326   have "?S'' = frontier(cball 0 (norm a))" unfolding frontier_cball and dist_norm by auto
  6327   hence "compact ?S''" using compact_frontier[OF compact_cball, of 0 "norm a"] by auto
  6328   moreover have "?S' = s \<inter> ?S''" by auto
  6329   ultimately have "compact ?S'" using closed_inter_compact[of s ?S''] using s(1) by auto
  6330   moreover have *:"f ` ?S' = ?S" by auto
  6331   ultimately have "compact ?S" using compact_continuous_image[OF linear_continuous_on[OF f(1)], of ?S'] by auto
  6332   hence "closed ?S" using compact_imp_closed by auto
  6333   moreover have "?S \<noteq> {}" using a by auto
  6334   ultimately obtain b' where "b'\<in>?S" "\<forall>y\<in>?S. norm b' \<le> norm y" using distance_attains_inf[of ?S 0] unfolding dist_0_norm by auto
  6335   then obtain b where "b\<in>s" and ba:"norm b = norm a" and b:"\<forall>x\<in>{x \<in> s. norm x = norm a}. norm (f b) \<le> norm (f x)" unfolding *[THEN sym] unfolding image_iff by auto
  6336 
  6337   let ?e = "norm (f b) / norm b"
  6338   have "norm b > 0" using ba and a and norm_ge_zero by auto
  6339   moreover have "norm (f b) > 0" using f(2)[THEN bspec[where x=b], OF `b\<in>s`] using `norm b >0` unfolding zero_less_norm_iff by auto
  6340   ultimately have "0 < norm (f b) / norm b" by(simp only: divide_pos_pos)
  6341   moreover
  6342   { fix x assume "x\<in>s"
  6343     hence "norm (f b) / norm b * norm x \<le> norm (f x)"
  6344     proof(cases "x=0")
  6345       case True thus "norm (f b) / norm b * norm x \<le> norm (f x)" by auto
  6346     next
  6347       case False
  6348       hence *:"0 < norm a / norm x" using `a\<noteq>0` unfolding zero_less_norm_iff[THEN sym] by(simp only: divide_pos_pos)
  6349       have "\<forall>c. \<forall>x\<in>s. c *\<^sub>R x \<in> s" using s[unfolded subspace_def] by auto
  6350       hence "(norm a / norm x) *\<^sub>R x \<in> {x \<in> s. norm x = norm a}" using `x\<in>s` and `x\<noteq>0` by auto
  6351       thus "norm (f b) / norm b * norm x \<le> norm (f x)" using b[THEN bspec[where x="(norm a / norm x) *\<^sub>R x"]]
  6352         unfolding f.scaleR and ba using `x\<noteq>0` `a\<noteq>0`
  6353         by (auto simp add: mult_commute pos_le_divide_eq pos_divide_le_eq)
  6354     qed }
  6355   ultimately
  6356   show ?thesis by auto
  6357 qed
  6358 
  6359 lemma closed_injective_image_subspace:
  6360   fixes f :: "'a::euclidean_space \<Rightarrow> 'b::euclidean_space"
  6361   assumes "subspace s" "bounded_linear f" "\<forall>x\<in>s. f x = 0 --> x = 0" "closed s"
  6362   shows "closed(f ` s)"
  6363 proof-
  6364   obtain e where "e>0" and e:"\<forall>x\<in>s. e * norm x \<le> norm (f x)" using injective_imp_isometric[OF assms(4,1,2,3)] by auto
  6365   show ?thesis using complete_isometric_image[OF `e>0` assms(1,2) e] and assms(4)
  6366     unfolding complete_eq_closed[THEN sym] by auto
  6367 qed
  6368 
  6369 
  6370 subsection {* Some properties of a canonical subspace *}
  6371 
  6372 lemma subspace_substandard:
  6373   "subspace {x::'a::euclidean_space. (\<forall>i\<in>Basis. P i \<longrightarrow> x\<bullet>i = 0)}"
  6374   unfolding subspace_def by (auto simp: inner_add_left)
  6375 
  6376 lemma closed_substandard:
  6377  "closed {x::'a::euclidean_space. \<forall>i\<in>Basis. P i --> x\<bullet>i = 0}" (is "closed ?A")
  6378 proof-
  6379   let ?D = "{i\<in>Basis. P i}"
  6380   have "closed (\<Inter>i\<in>?D. {x::'a. x\<bullet>i = 0})"
  6381     by (simp add: closed_INT closed_Collect_eq)
  6382   also have "(\<Inter>i\<in>?D. {x::'a. x\<bullet>i = 0}) = ?A"
  6383     by auto
  6384   finally show "closed ?A" .
  6385 qed
  6386 
  6387 lemma dim_substandard: assumes d: "d \<subseteq> Basis"
  6388   shows "dim {x::'a::euclidean_space. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0} = card d" (is "dim ?A = _")
  6389 proof-
  6390   let ?D = "Basis :: 'a set"
  6391   have "d \<subseteq> ?A" using d by (auto simp: inner_Basis)
  6392   moreover
  6393   { fix x::"'a" assume "x \<in> ?A"
  6394     hence "finite d" "x \<in> ?A" using assms by(auto intro: finite_subset[OF _ finite_Basis])
  6395     from this d have "x \<in> span d"
  6396     proof(induct d arbitrary: x)
  6397       case empty hence "x=0" apply(rule_tac euclidean_eqI) by auto
  6398       thus ?case using subspace_0[OF subspace_span[of "{}"]] by auto
  6399     next
  6400       case (insert k F)
  6401       hence *:"\<forall>i\<in>Basis. i \<notin> insert k F \<longrightarrow> x \<bullet> i = 0" by auto
  6402       have **:"F \<subseteq> insert k F" by auto
  6403       def y \<equiv> "x - (x\<bullet>k) *\<^sub>R k"
  6404       have y:"x = y + (x\<bullet>k) *\<^sub>R k" unfolding y_def by auto
  6405       { fix i assume i': "i \<notin> F" "i \<in> Basis"
  6406         hence "y \<bullet> i = 0" unfolding y_def 
  6407           using *[THEN bspec[where x=i]] insert by (auto simp: inner_simps inner_Basis) }
  6408       hence "y \<in> span F" using insert by auto
  6409       hence "y \<in> span (insert k F)"
  6410         using span_mono[of F "insert k F"] using assms by auto
  6411       moreover
  6412       have "k \<in> span (insert k F)" by(rule span_superset, auto)
  6413       hence "(x\<bullet>k) *\<^sub>R k \<in> span (insert k F)"
  6414         using span_mul by auto
  6415       ultimately
  6416       have "y + (x\<bullet>k) *\<^sub>R k \<in> span (insert k F)"
  6417         using span_add by auto
  6418       thus ?case using y by auto
  6419     qed
  6420   }
  6421   hence "?A \<subseteq> span d" by auto
  6422   moreover
  6423   { fix x assume "x \<in> d" hence "x \<in> ?D" using assms by auto  }
  6424   hence "independent d" using independent_mono[OF independent_Basis, of d] and assms by auto
  6425   moreover
  6426   have "d \<subseteq> ?D" unfolding subset_eq using assms by auto
  6427   ultimately show ?thesis using dim_unique[of d ?A] by auto
  6428 qed
  6429 
  6430 text{* Hence closure and completeness of all subspaces.                          *}
  6431 
  6432 lemma ex_card: assumes "n \<le> card A" shows "\<exists>S\<subseteq>A. card S = n"
  6433 proof cases
  6434   assume "finite A"
  6435   from ex_bij_betw_nat_finite[OF this] guess f ..
  6436   moreover with `n \<le> card A` have "{..< n} \<subseteq> {..< card A}" "inj_on f {..< n}"
  6437     by (auto simp: bij_betw_def intro: subset_inj_on)
  6438   ultimately have "f ` {..< n} \<subseteq> A" "card (f ` {..< n}) = n"
  6439     by (auto simp: bij_betw_def card_image)
  6440   then show ?thesis by blast
  6441 next
  6442   assume "\<not> finite A" with `n \<le> card A` show ?thesis by force
  6443 qed
  6444 
  6445 lemma closed_subspace: fixes s::"('a::euclidean_space) set"
  6446   assumes "subspace s" shows "closed s"
  6447 proof-
  6448   have "dim s \<le> card (Basis :: 'a set)" using dim_subset_UNIV by auto
  6449   with ex_card[OF this] obtain d :: "'a set" where t: "card d = dim s" and d: "d \<subseteq> Basis" by auto
  6450   let ?t = "{x::'a. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x\<bullet>i = 0}"
  6451   have "\<exists>f. linear f \<and> f ` {x::'a. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0} = s \<and>
  6452       inj_on f {x::'a. \<forall>i\<in>Basis. i \<notin> d \<longrightarrow> x \<bullet> i = 0}"
  6453     using dim_substandard[of d] t d assms
  6454     by (intro subspace_isomorphism[OF subspace_substandard[of "\<lambda>i. i \<notin> d"]]) (auto simp: inner_Basis)
  6455   then guess f by (elim exE conjE) note f = this
  6456   interpret f: bounded_linear f using f unfolding linear_conv_bounded_linear by auto
  6457   { fix x have "x\<in>?t \<Longrightarrow> f x = 0 \<Longrightarrow> x = 0" using f.zero d f(3)[THEN inj_onD, of x 0] by auto }
  6458   moreover have "closed ?t" using closed_substandard .
  6459   moreover have "subspace ?t" using subspace_substandard .
  6460   ultimately show ?thesis using closed_injective_image_subspace[of ?t f]
  6461     unfolding f(2) using f(1) unfolding linear_conv_bounded_linear by auto
  6462 qed
  6463 
  6464 lemma complete_subspace:
  6465   fixes s :: "('a::euclidean_space) set" shows "subspace s ==> complete s"
  6466   using complete_eq_closed closed_subspace
  6467   by auto
  6468 
  6469 lemma dim_closure:
  6470   fixes s :: "('a::euclidean_space) set"
  6471   shows "dim(closure s) = dim s" (is "?dc = ?d")
  6472 proof-
  6473   have "?dc \<le> ?d" using closure_minimal[OF span_inc, of s]
  6474     using closed_subspace[OF subspace_span, of s]
  6475     using dim_subset[of "closure s" "span s"] unfolding dim_span by auto
  6476   thus ?thesis using dim_subset[OF closure_subset, of s] by auto
  6477 qed
  6478 
  6479 
  6480 subsection {* Affine transformations of intervals *}
  6481 
  6482 lemma real_affinity_le:
  6483  "0 < (m::'a::linordered_field) ==> (m * x + c \<le> y \<longleftrightarrow> x \<le> inverse(m) * y + -(c / m))"
  6484   by (simp add: field_simps inverse_eq_divide)
  6485 
  6486 lemma real_le_affinity:
  6487  "0 < (m::'a::linordered_field) ==> (y \<le> m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) \<le> x)"
  6488   by (simp add: field_simps inverse_eq_divide)
  6489 
  6490 lemma real_affinity_lt:
  6491  "0 < (m::'a::linordered_field) ==> (m * x + c < y \<longleftrightarrow> x < inverse(m) * y + -(c / m))"
  6492   by (simp add: field_simps inverse_eq_divide)
  6493 
  6494 lemma real_lt_affinity:
  6495  "0 < (m::'a::linordered_field) ==> (y < m * x + c \<longleftrightarrow> inverse(m) * y + -(c / m) < x)"
  6496   by (simp add: field_simps inverse_eq_divide)
  6497 
  6498 lemma real_affinity_eq:
  6499  "(m::'a::linordered_field) \<noteq> 0 ==> (m * x + c = y \<longleftrightarrow> x = inverse(m) * y + -(c / m))"
  6500   by (simp add: field_simps inverse_eq_divide)
  6501 
  6502 lemma real_eq_affinity:
  6503  "(m::'a::linordered_field) \<noteq> 0 ==> (y = m * x + c  \<longleftrightarrow> inverse(m) * y + -(c / m) = x)"
  6504   by (simp add: field_simps inverse_eq_divide)
  6505 
  6506 lemma image_affinity_interval: fixes m::real
  6507   fixes a b c :: "'a::ordered_euclidean_space"
  6508   shows "(\<lambda>x. m *\<^sub>R x + c) ` {a .. b} =
  6509             (if {a .. b} = {} then {}
  6510             else (if 0 \<le> m then {m *\<^sub>R a + c .. m *\<^sub>R b + c}
  6511             else {m *\<^sub>R b + c .. m *\<^sub>R a + c}))"
  6512 proof(cases "m=0")  
  6513   { fix x assume "x \<le> c" "c \<le> x"
  6514     hence "x=c" unfolding eucl_le[where 'a='a] apply-
  6515       apply(subst euclidean_eq_iff) by (auto intro: order_antisym) }
  6516   moreover case True
  6517   moreover have "c \<in> {m *\<^sub>R a + c..m *\<^sub>R b + c}" unfolding True by(auto simp add: eucl_le[where 'a='a])
  6518   ultimately show ?thesis by auto
  6519 next
  6520   case False
  6521   { fix y assume "a \<le> y" "y \<le> b" "m > 0"
  6522     hence "m *\<^sub>R a + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R b + c"
  6523       unfolding eucl_le[where 'a='a] by (auto simp: inner_simps)
  6524   } moreover
  6525   { fix y assume "a \<le> y" "y \<le> b" "m < 0"
  6526     hence "m *\<^sub>R b + c \<le> m *\<^sub>R y + c"  "m *\<^sub>R y + c \<le> m *\<^sub>R a + c"
  6527       unfolding eucl_le[where 'a='a] by(auto simp add: mult_left_mono_neg inner_simps)
  6528   } moreover
  6529   { fix y assume "m > 0"  "m *\<^sub>R a + c \<le> y"  "y \<le> m *\<^sub>R b + c"
  6530     hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
  6531       unfolding image_iff Bex_def mem_interval eucl_le[where 'a='a]
  6532       apply (intro exI[where x="(1 / m) *\<^sub>R (y - c)"])
  6533       by(auto simp add: pos_le_divide_eq pos_divide_le_eq mult_commute diff_le_iff inner_simps)
  6534   } moreover
  6535   { fix y assume "m *\<^sub>R b + c \<le> y" "y \<le> m *\<^sub>R a + c" "m < 0"
  6536     hence "y \<in> (\<lambda>x. m *\<^sub>R x + c) ` {a..b}"
  6537       unfolding image_iff Bex_def mem_interval eucl_le[where 'a='a]
  6538       apply (intro exI[where x="(1 / m) *\<^sub>R (y - c)"])
  6539       by(auto simp add: neg_le_divide_eq neg_divide_le_eq mult_commute diff_le_iff inner_simps)
  6540   }
  6541   ultimately show ?thesis using False by auto
  6542 qed
  6543 
  6544 lemma image_smult_interval:"(\<lambda>x. m *\<^sub>R (x::_::ordered_euclidean_space)) ` {a..b} =
  6545   (if {a..b} = {} then {} else if 0 \<le> m then {m *\<^sub>R a..m *\<^sub>R b} else {m *\<^sub>R b..m *\<^sub>R a})"
  6546   using image_affinity_interval[of m 0 a b] by auto
  6547 
  6548 
  6549 subsection {* Banach fixed point theorem (not really topological...) *}
  6550 
  6551 lemma banach_fix:
  6552   assumes s:"complete s" "s \<noteq> {}" and c:"0 \<le> c" "c < 1" and f:"(f ` s) \<subseteq> s" and
  6553           lipschitz:"\<forall>x\<in>s. \<forall>y\<in>s. dist (f x) (f y) \<le> c * dist x y"
  6554   shows "\<exists>! x\<in>s. (f x = x)"
  6555 proof-
  6556   have "1 - c > 0" using c by auto
  6557 
  6558   from s(2) obtain z0 where "z0 \<in> s" by auto
  6559   def z \<equiv> "\<lambda>n. (f ^^ n) z0"
  6560   { fix n::nat
  6561     have "z n \<in> s" unfolding z_def
  6562     proof(induct n) case 0 thus ?case using `z0 \<in>s` by auto
  6563     next case Suc thus ?case using f by auto qed }
  6564   note z_in_s = this
  6565 
  6566   def d \<equiv> "dist (z 0) (z 1)"
  6567 
  6568   have fzn:"\<And>n. f (z n) = z (Suc n)" unfolding z_def by auto
  6569   { fix n::nat
  6570     have "dist (z n) (z (Suc n)) \<le> (c ^ n) * d"
  6571     proof(induct n)
  6572       case 0 thus ?case unfolding d_def by auto
  6573     next
  6574       case (Suc m)
  6575       hence "c * dist (z m) (z (Suc m)) \<le> c ^ Suc m * d"
  6576         using `0 \<le> c` using mult_left_mono[of "dist (z m) (z (Suc m))" "c ^ m * d" c] by auto
  6577       thus ?case using lipschitz[THEN bspec[where x="z m"], OF z_in_s, THEN bspec[where x="z (Suc m)"], OF z_in_s]
  6578         unfolding fzn and mult_le_cancel_left by auto
  6579     qed
  6580   } note cf_z = this
  6581 
  6582   { fix n m::nat
  6583     have "(1 - c) * dist (z m) (z (m+n)) \<le> (c ^ m) * d * (1 - c ^ n)"
  6584     proof(induct n)
  6585       case 0 show ?case by auto
  6586     next
  6587       case (Suc k)
  6588       have "(1 - c) * dist (z m) (z (m + Suc k)) \<le> (1 - c) * (dist (z m) (z (m + k)) + dist (z (m + k)) (z (Suc (m + k))))"
  6589         using dist_triangle and c by(auto simp add: dist_triangle)
  6590       also have "\<dots> \<le> (1 - c) * (dist (z m) (z (m + k)) + c ^ (m + k) * d)"
  6591         using cf_z[of "m + k"] and c by auto
  6592       also have "\<dots> \<le> c ^ m * d * (1 - c ^ k) + (1 - c) * c ^ (m + k) * d"
  6593         using Suc by (auto simp add: field_simps)
  6594       also have "\<dots> = (c ^ m) * (d * (1 - c ^ k) + (1 - c) * c ^ k * d)"
  6595         unfolding power_add by (auto simp add: field_simps)
  6596       also have "\<dots> \<le> (c ^ m) * d * (1 - c ^ Suc k)"
  6597         using c by (auto simp add: field_simps)
  6598       finally show ?case by auto
  6599     qed
  6600   } note cf_z2 = this
  6601   { fix e::real assume "e>0"
  6602     hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (z m) (z n) < e"
  6603     proof(cases "d = 0")
  6604       case True
  6605       have *: "\<And>x. ((1 - c) * x \<le> 0) = (x \<le> 0)" using `1 - c > 0`
  6606         by (metis mult_zero_left mult_commute real_mult_le_cancel_iff1)
  6607       from True have "\<And>n. z n = z0" using cf_z2[of 0] and c unfolding z_def
  6608         by (simp add: *)
  6609       thus ?thesis using `e>0` by auto
  6610     next
  6611       case False hence "d>0" unfolding d_def using zero_le_dist[of "z 0" "z 1"]
  6612         by (metis False d_def less_le)
  6613       hence "0 < e * (1 - c) / d" using `e>0` and `1-c>0`
  6614         using divide_pos_pos[of "e * (1 - c)" d] and mult_pos_pos[of e "1 - c"] by auto
  6615       then obtain N where N:"c ^ N < e * (1 - c) / d" using real_arch_pow_inv[of "e * (1 - c) / d" c] and c by auto
  6616       { fix m n::nat assume "m>n" and as:"m\<ge>N" "n\<ge>N"
  6617         have *:"c ^ n \<le> c ^ N" using `n\<ge>N` and c using power_decreasing[OF `n\<ge>N`, of c] by auto
  6618         have "1 - c ^ (m - n) > 0" using c and power_strict_mono[of c 1 "m - n"] using `m>n` by auto
  6619         hence **:"d * (1 - c ^ (m - n)) / (1 - c) > 0"
  6620           using mult_pos_pos[OF `d>0`, of "1 - c ^ (m - n)"]
  6621           using divide_pos_pos[of "d * (1 - c ^ (m - n))" "1 - c"]
  6622           using `0 < 1 - c` by auto
  6623 
  6624         have "dist (z m) (z n) \<le> c ^ n * d * (1 - c ^ (m - n)) / (1 - c)"
  6625           using cf_z2[of n "m - n"] and `m>n` unfolding pos_le_divide_eq[OF `1-c>0`]
  6626           by (auto simp add: mult_commute dist_commute)
  6627         also have "\<dots> \<le> c ^ N * d * (1 - c ^ (m - n)) / (1 - c)"
  6628           using mult_right_mono[OF * order_less_imp_le[OF **]]
  6629           unfolding mult_assoc by auto
  6630         also have "\<dots> < (e * (1 - c) / d) * d * (1 - c ^ (m - n)) / (1 - c)"
  6631           using mult_strict_right_mono[OF N **] unfolding mult_assoc by auto
  6632         also have "\<dots> = e * (1 - c ^ (m - n))" using c and `d>0` and `1 - c > 0` by auto
  6633         also have "\<dots> \<le> e" using c and `1 - c ^ (m - n) > 0` and `e>0` using mult_right_le_one_le[of e "1 - c ^ (m - n)"] by auto
  6634         finally have  "dist (z m) (z n) < e" by auto
  6635       } note * = this
  6636       { fix m n::nat assume as:"N\<le>m" "N\<le>n"
  6637         hence "dist (z n) (z m) < e"
  6638         proof(cases "n = m")
  6639           case True thus ?thesis using `e>0` by auto
  6640         next
  6641           case False thus ?thesis using as and *[of n m] *[of m n] unfolding nat_neq_iff by (auto simp add: dist_commute)
  6642         qed }
  6643       thus ?thesis by auto
  6644     qed
  6645   }
  6646   hence "Cauchy z" unfolding cauchy_def by auto
  6647   then obtain x where "x\<in>s" and x:"(z ---> x) sequentially" using s(1)[unfolded compact_def complete_def, THEN spec[where x=z]] and z_in_s by auto
  6648 
  6649   def e \<equiv> "dist (f x) x"
  6650   have "e = 0" proof(rule ccontr)
  6651     assume "e \<noteq> 0" hence "e>0" unfolding e_def using zero_le_dist[of "f x" x]
  6652       by (metis dist_eq_0_iff dist_nz e_def)
  6653     then obtain N where N:"\<forall>n\<ge>N. dist (z n) x < e / 2"
  6654       using x[unfolded LIMSEQ_def, THEN spec[where x="e/2"]] by auto
  6655     hence N':"dist (z N) x < e / 2" by auto
  6656 
  6657     have *:"c * dist (z N) x \<le> dist (z N) x" unfolding mult_le_cancel_right2
  6658       using zero_le_dist[of "z N" x] and c
  6659       by (metis dist_eq_0_iff dist_nz order_less_asym less_le)
  6660     have "dist (f (z N)) (f x) \<le> c * dist (z N) x" using lipschitz[THEN bspec[where x="z N"], THEN bspec[where x=x]]
  6661       using z_in_s[of N] `x\<in>s` using c by auto
  6662     also have "\<dots> < e / 2" using N' and c using * by auto
  6663     finally show False unfolding fzn
  6664       using N[THEN spec[where x="Suc N"]] and dist_triangle_half_r[of "z (Suc N)" "f x" e x]
  6665       unfolding e_def by auto
  6666   qed
  6667   hence "f x = x" unfolding e_def by auto
  6668   moreover
  6669   { fix y assume "f y = y" "y\<in>s"
  6670     hence "dist x y \<le> c * dist x y" using lipschitz[THEN bspec[where x=x], THEN bspec[where x=y]]
  6671       using `x\<in>s` and `f x = x` by auto
  6672     hence "dist x y = 0" unfolding mult_le_cancel_right1
  6673       using c and zero_le_dist[of x y] by auto
  6674     hence "y = x" by auto
  6675   }
  6676   ultimately show ?thesis using `x\<in>s` by blast+
  6677 qed
  6678 
  6679 subsection {* Edelstein fixed point theorem *}
  6680 
  6681 lemma edelstein_fix:
  6682   fixes s :: "'a::real_normed_vector set"
  6683   assumes s:"compact s" "s \<noteq> {}" and gs:"(g ` s) \<subseteq> s"
  6684       and dist:"\<forall>x\<in>s. \<forall>y\<in>s. x \<noteq> y \<longrightarrow> dist (g x) (g y) < dist x y"
  6685   shows "\<exists>! x\<in>s. g x = x"
  6686 proof(cases "\<exists>x\<in>s. g x \<noteq> x")
  6687   obtain x where "x\<in>s" using s(2) by auto
  6688   case False hence g:"\<forall>x\<in>s. g x = x" by auto
  6689   { fix y assume "y\<in>s"
  6690     hence "x = y" using `x\<in>s` and dist[THEN bspec[where x=x], THEN bspec[where x=y]]
  6691       unfolding g[THEN bspec[where x=x], OF `x\<in>s`]
  6692       unfolding g[THEN bspec[where x=y], OF `y\<in>s`] by auto  }
  6693   thus ?thesis using `x\<in>s` and g by blast+
  6694 next
  6695   case True
  6696   then obtain x where [simp]:"x\<in>s" and "g x \<noteq> x" by auto
  6697   { fix x y assume "x \<in> s" "y \<in> s"
  6698     hence "dist (g x) (g y) \<le> dist x y"
  6699       using dist[THEN bspec[where x=x], THEN bspec[where x=y]] by auto } note dist' = this
  6700   def y \<equiv> "g x"
  6701   have [simp]:"y\<in>s" unfolding y_def using gs[unfolded image_subset_iff] and `x\<in>s` by blast
  6702   def f \<equiv> "\<lambda>n. g ^^ n"
  6703   have [simp]:"\<And>n z. g (f n z) = f (Suc n) z" unfolding f_def by auto
  6704   have [simp]:"\<And>z. f 0 z = z" unfolding f_def by auto
  6705   { fix n::nat and z assume "z\<in>s"
  6706     have "f n z \<in> s" unfolding f_def
  6707     proof(induct n)
  6708       case 0 thus ?case using `z\<in>s` by simp
  6709     next
  6710       case (Suc n) thus ?case using gs[unfolded image_subset_iff] by auto
  6711     qed } note fs = this
  6712   { fix m n ::nat assume "m\<le>n"
  6713     fix w z assume "w\<in>s" "z\<in>s"
  6714     have "dist (f n w) (f n z) \<le> dist (f m w) (f m z)" using `m\<le>n`
  6715     proof(induct n)
  6716       case 0 thus ?case by auto
  6717     next
  6718       case (Suc n)
  6719       thus ?case proof(cases "m\<le>n")
  6720         case True thus ?thesis using Suc(1)
  6721           using dist'[OF fs fs, OF `w\<in>s` `z\<in>s`, of n n] by auto
  6722       next
  6723         case False hence mn:"m = Suc n" using Suc(2) by simp
  6724         show ?thesis unfolding mn  by auto
  6725       qed
  6726     qed } note distf = this
  6727 
  6728   def h \<equiv> "\<lambda>n. (f n x, f n y)"
  6729   let ?s2 = "s \<times> s"
  6730   obtain l r where "l\<in>?s2" and r:"subseq r" and lr:"((h \<circ> r) ---> l) sequentially"
  6731     using compact_Times [OF s(1) s(1), unfolded compact_def, THEN spec[where x=h]] unfolding  h_def
  6732     using fs[OF `x\<in>s`] and fs[OF `y\<in>s`] by blast
  6733   def a \<equiv> "fst l" def b \<equiv> "snd l"
  6734   have lab:"l = (a, b)" unfolding a_def b_def by simp
  6735   have [simp]:"a\<in>s" "b\<in>s" unfolding a_def b_def using `l\<in>?s2` by auto
  6736 
  6737   have lima:"((fst \<circ> (h \<circ> r)) ---> a) sequentially"
  6738    and limb:"((snd \<circ> (h \<circ> r)) ---> b) sequentially"
  6739     using lr
  6740     unfolding o_def a_def b_def by (rule tendsto_intros)+
  6741 
  6742   { fix n::nat
  6743     have *:"\<And>fx fy (x::'a) y. dist fx fy \<le> dist x y \<Longrightarrow> \<not> (dist (fx - fy) (a - b) < dist a b - dist x y)" unfolding dist_norm by norm
  6744     { fix x y :: 'a
  6745       have "dist (-x) (-y) = dist x y" unfolding dist_norm
  6746         using norm_minus_cancel[of "x - y"] by (auto simp add: uminus_add_conv_diff) } note ** = this
  6747 
  6748     { assume as:"dist a b > dist (f n x) (f n y)"
  6749       then obtain Na Nb where "\<forall>m\<ge>Na. dist (f (r m) x) a < (dist a b - dist (f n x) (f n y)) / 2"
  6750         and "\<forall>m\<ge>Nb. dist (f (r m) y) b < (dist a b - dist (f n x) (f n y)) / 2"
  6751         using lima limb unfolding h_def LIMSEQ_def by (fastforce simp del: less_divide_eq_numeral1)
  6752       hence "dist (f (r (Na + Nb + n)) x - f (r (Na + Nb + n)) y) (a - b) < dist a b - dist (f n x) (f n y)"
  6753         apply(erule_tac x="Na+Nb+n" in allE)
  6754         apply(erule_tac x="Na+Nb+n" in allE) apply simp
  6755         using dist_triangle_add_half[of a "f (r (Na + Nb + n)) x" "dist a b - dist (f n x) (f n y)"
  6756           "-b"  "- f (r (Na + Nb + n)) y"]
  6757         unfolding ** by (auto simp add: algebra_simps dist_commute)
  6758       moreover
  6759       have "dist (f (r (Na + Nb + n)) x - f (r (Na + Nb + n)) y) (a - b) \<ge> dist a b - dist (f n x) (f n y)"
  6760         using distf[of n "r (Na+Nb+n)", OF _ `x\<in>s` `y\<in>s`]
  6761         using seq_suble[OF r, of "Na+Nb+n"]
  6762         using *[of "f (r (Na + Nb + n)) x" "f (r (Na + Nb + n)) y" "f n x" "f n y"] by auto
  6763       ultimately have False by simp
  6764     }
  6765     hence "dist a b \<le> dist (f n x) (f n y)" by(rule ccontr)auto }
  6766   note ab_fn = this
  6767 
  6768   have [simp]:"a = b" proof(rule ccontr)
  6769     def e \<equiv> "dist a b - dist (g a) (g b)"
  6770     assume "a\<noteq>b" hence "e > 0" unfolding e_def using dist by fastforce
  6771     hence "\<exists>n. dist (f n x) a < e/2 \<and> dist (f n y) b < e/2"
  6772       using lima limb unfolding LIMSEQ_def
  6773       apply (auto elim!: allE[where x="e/2"]) apply(rename_tac N N', rule_tac x="r (max N N')" in exI) unfolding h_def by fastforce
  6774     then obtain n where n:"dist (f n x) a < e/2 \<and> dist (f n y) b < e/2" by auto
  6775     have "dist (f (Suc n) x) (g a) \<le> dist (f n x) a"
  6776       using dist[THEN bspec[where x="f n x"], THEN bspec[where x="a"]] and fs by auto
  6777     moreover have "dist (f (Suc n) y) (g b) \<le> dist (f n y) b"
  6778       using dist[THEN bspec[where x="f n y"], THEN bspec[where x="b"]] and fs by auto
  6779     ultimately have "dist (f (Suc n) x) (g a) + dist (f (Suc n) y) (g b) < e" using n by auto
  6780     thus False unfolding e_def using ab_fn[of "Suc n"] by norm
  6781   qed
  6782 
  6783   have [simp]:"\<And>n. f (Suc n) x = f n y" unfolding f_def y_def by(induct_tac n)auto
  6784   { fix x y assume "x\<in>s" "y\<in>s" moreover
  6785     fix e::real assume "e>0" ultimately
  6786     have "dist y x < e \<longrightarrow> dist (g y) (g x) < e" using dist by fastforce }
  6787   hence "continuous_on s g" unfolding continuous_on_iff by auto
  6788 
  6789   hence "((snd \<circ> h \<circ> r) ---> g a) sequentially" unfolding continuous_on_sequentially
  6790     apply (rule allE[where x="\<lambda>n. (fst \<circ> h \<circ> r) n"]) apply (erule ballE[where x=a])
  6791     using lima unfolding h_def o_def using fs[OF `x\<in>s`] by (auto simp add: y_def)
  6792   hence "g a = a" using tendsto_unique[OF trivial_limit_sequentially limb, of "g a"]
  6793     unfolding `a=b` and o_assoc by auto
  6794   moreover
  6795   { fix x assume "x\<in>s" "g x = x" "x\<noteq>a"
  6796     hence "False" using dist[THEN bspec[where x=a], THEN bspec[where x=x]]
  6797       using `g a = a` and `a\<in>s` by auto  }
  6798   ultimately show "\<exists>!x\<in>s. g x = x" using `a\<in>s` by blast
  6799 qed
  6800 
  6801 declare tendsto_const [intro] (* FIXME: move *)
  6802 
  6803 end