src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
 author hoelzl Fri Mar 22 10:41:43 2013 +0100 (2013-03-22) changeset 51475 ebf9d4fd00ba parent 51473 1210309fddab child 51478 270b21f3ae0a permissions -rw-r--r--
introduct the conditional_complete_lattice type class; generalize theorems about real Sup and Inf to it
     1 (*  title:      HOL/Library/Topology_Euclidian_Space.thy

     2     Author:     Amine Chaieb, University of Cambridge

     3     Author:     Robert Himmelmann, TU Muenchen

     4     Author:     Brian Huffman, Portland State University

     5 *)

     6

     7 header {* Elementary topology in Euclidean space. *}

     8

     9 theory Topology_Euclidean_Space

    10 imports

    11   Complex_Main

    12   "~~/src/HOL/Library/Countable_Set"

    13   "~~/src/HOL/Library/Glbs"

    14   "~~/src/HOL/Library/FuncSet"

    15   Linear_Algebra

    16   Norm_Arith

    17 begin

    18

    19 lemma dist_0_norm:

    20   fixes x :: "'a::real_normed_vector"

    21   shows "dist 0 x = norm x"

    22 unfolding dist_norm by simp

    23

    24 lemma dist_double: "dist x y < d / 2 \<Longrightarrow> dist x z < d / 2 \<Longrightarrow> dist y z < d"

    25   using dist_triangle[of y z x] by (simp add: dist_commute)

    26

    27 (* LEGACY *)

    28 lemma lim_subseq: "subseq r \<Longrightarrow> s ----> l \<Longrightarrow> (s o r) ----> l"

    29   by (rule LIMSEQ_subseq_LIMSEQ)

    30

    31 lemmas real_isGlb_unique = isGlb_unique[where 'a=real]

    32

    33 lemma countable_PiE:

    34   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (PiE I F)"

    35   by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)

    36

    37 subsection {* Topological Basis *}

    38

    39 context topological_space

    40 begin

    41

    42 definition "topological_basis B =

    43   ((\<forall>b\<in>B. open b) \<and> (\<forall>x. open x \<longrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x)))"

    44

    45 lemma topological_basis:

    46   "topological_basis B = (\<forall>x. open x \<longleftrightarrow> (\<exists>B'. B' \<subseteq> B \<and> \<Union>B' = x))"

    47   unfolding topological_basis_def

    48   apply safe

    49      apply fastforce

    50     apply fastforce

    51    apply (erule_tac x="x" in allE)

    52    apply simp

    53    apply (rule_tac x="{x}" in exI)

    54   apply auto

    55   done

    56

    57 lemma topological_basis_iff:

    58   assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"

    59   shows "topological_basis B \<longleftrightarrow> (\<forall>O'. open O' \<longrightarrow> (\<forall>x\<in>O'. \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'))"

    60     (is "_ \<longleftrightarrow> ?rhs")

    61 proof safe

    62   fix O' and x::'a

    63   assume H: "topological_basis B" "open O'" "x \<in> O'"

    64   hence "(\<exists>B'\<subseteq>B. \<Union>B' = O')" by (simp add: topological_basis_def)

    65   then obtain B' where "B' \<subseteq> B" "O' = \<Union>B'" by auto

    66   thus "\<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'" using H by auto

    67 next

    68   assume H: ?rhs

    69   show "topological_basis B" using assms unfolding topological_basis_def

    70   proof safe

    71     fix O'::"'a set" assume "open O'"

    72     with H obtain f where "\<forall>x\<in>O'. f x \<in> B \<and> x \<in> f x \<and> f x \<subseteq> O'"

    73       by (force intro: bchoice simp: Bex_def)

    74     thus "\<exists>B'\<subseteq>B. \<Union>B' = O'"

    75       by (auto intro: exI[where x="{f x |x. x \<in> O'}"])

    76   qed

    77 qed

    78

    79 lemma topological_basisI:

    80   assumes "\<And>B'. B' \<in> B \<Longrightarrow> open B'"

    81   assumes "\<And>O' x. open O' \<Longrightarrow> x \<in> O' \<Longrightarrow> \<exists>B'\<in>B. x \<in> B' \<and> B' \<subseteq> O'"

    82   shows "topological_basis B"

    83   using assms by (subst topological_basis_iff) auto

    84

    85 lemma topological_basisE:

    86   fixes O'

    87   assumes "topological_basis B"

    88   assumes "open O'"

    89   assumes "x \<in> O'"

    90   obtains B' where "B' \<in> B" "x \<in> B'" "B' \<subseteq> O'"

    91 proof atomize_elim

    92   from assms have "\<And>B'. B'\<in>B \<Longrightarrow> open B'" by (simp add: topological_basis_def)

    93   with topological_basis_iff assms

    94   show  "\<exists>B'. B' \<in> B \<and> x \<in> B' \<and> B' \<subseteq> O'" using assms by (simp add: Bex_def)

    95 qed

    96

    97 lemma topological_basis_open:

    98   assumes "topological_basis B"

    99   assumes "X \<in> B"

   100   shows "open X"

   101   using assms

   102   by (simp add: topological_basis_def)

   103

   104 lemma topological_basis_imp_subbasis:

   105   assumes B: "topological_basis B" shows "open = generate_topology B"

   106 proof (intro ext iffI)

   107   fix S :: "'a set" assume "open S"

   108   with B obtain B' where "B' \<subseteq> B" "S = \<Union>B'"

   109     unfolding topological_basis_def by blast

   110   then show "generate_topology B S"

   111     by (auto intro: generate_topology.intros dest: topological_basis_open)

   112 next

   113   fix S :: "'a set" assume "generate_topology B S" then show "open S"

   114     by induct (auto dest: topological_basis_open[OF B])

   115 qed

   116

   117 lemma basis_dense:

   118   fixes B::"'a set set" and f::"'a set \<Rightarrow> 'a"

   119   assumes "topological_basis B"

   120   assumes choosefrom_basis: "\<And>B'. B' \<noteq> {} \<Longrightarrow> f B' \<in> B'"

   121   shows "(\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>B' \<in> B. f B' \<in> X))"

   122 proof (intro allI impI)

   123   fix X::"'a set" assume "open X" "X \<noteq> {}"

   124   from topological_basisE[OF topological_basis B open X choosefrom_basis[OF X \<noteq> {}]]

   125   guess B' . note B' = this

   126   thus "\<exists>B'\<in>B. f B' \<in> X" by (auto intro!: choosefrom_basis)

   127 qed

   128

   129 end

   130

   131 lemma topological_basis_prod:

   132   assumes A: "topological_basis A" and B: "topological_basis B"

   133   shows "topological_basis ((\<lambda>(a, b). a \<times> b)  (A \<times> B))"

   134   unfolding topological_basis_def

   135 proof (safe, simp_all del: ex_simps add: subset_image_iff ex_simps(1)[symmetric])

   136   fix S :: "('a \<times> 'b) set" assume "open S"

   137   then show "\<exists>X\<subseteq>A \<times> B. (\<Union>(a,b)\<in>X. a \<times> b) = S"

   138   proof (safe intro!: exI[of _ "{x\<in>A \<times> B. fst x \<times> snd x \<subseteq> S}"])

   139     fix x y assume "(x, y) \<in> S"

   140     from open_prod_elim[OF open S this]

   141     obtain a b where a: "open a""x \<in> a" and b: "open b" "y \<in> b" and "a \<times> b \<subseteq> S"

   142       by (metis mem_Sigma_iff)

   143     moreover from topological_basisE[OF A a] guess A0 .

   144     moreover from topological_basisE[OF B b] guess B0 .

   145     ultimately show "(x, y) \<in> (\<Union>(a, b)\<in>{X \<in> A \<times> B. fst X \<times> snd X \<subseteq> S}. a \<times> b)"

   146       by (intro UN_I[of "(A0, B0)"]) auto

   147   qed auto

   148 qed (metis A B topological_basis_open open_Times)

   149

   150 subsection {* Countable Basis *}

   151

   152 locale countable_basis =

   153   fixes B::"'a::topological_space set set"

   154   assumes is_basis: "topological_basis B"

   155   assumes countable_basis: "countable B"

   156 begin

   157

   158 lemma open_countable_basis_ex:

   159   assumes "open X"

   160   shows "\<exists>B' \<subseteq> B. X = Union B'"

   161   using assms countable_basis is_basis unfolding topological_basis_def by blast

   162

   163 lemma open_countable_basisE:

   164   assumes "open X"

   165   obtains B' where "B' \<subseteq> B" "X = Union B'"

   166   using assms open_countable_basis_ex by (atomize_elim) simp

   167

   168 lemma countable_dense_exists:

   169   shows "\<exists>D::'a set. countable D \<and> (\<forall>X. open X \<longrightarrow> X \<noteq> {} \<longrightarrow> (\<exists>d \<in> D. d \<in> X))"

   170 proof -

   171   let ?f = "(\<lambda>B'. SOME x. x \<in> B')"

   172   have "countable (?f  B)" using countable_basis by simp

   173   with basis_dense[OF is_basis, of ?f] show ?thesis

   174     by (intro exI[where x="?f  B"]) (metis (mono_tags) all_not_in_conv imageI someI)

   175 qed

   176

   177 lemma countable_dense_setE:

   178   obtains D :: "'a set"

   179   where "countable D" "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d \<in> D. d \<in> X"

   180   using countable_dense_exists by blast

   181

   182 end

   183

   184 lemma (in first_countable_topology) first_countable_basisE:

   185   obtains A where "countable A" "\<And>a. a \<in> A \<Longrightarrow> x \<in> a" "\<And>a. a \<in> A \<Longrightarrow> open a"

   186     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>a\<in>A. a \<subseteq> S)"

   187   using first_countable_basis[of x]

   188   apply atomize_elim

   189   apply (elim exE)

   190   apply (rule_tac x="range A" in exI)

   191   apply auto

   192   done

   193

   194 lemma (in first_countable_topology) first_countable_basis_Int_stableE:

   195   obtains A where "countable A" "\<And>a. a \<in> A \<Longrightarrow> x \<in> a" "\<And>a. a \<in> A \<Longrightarrow> open a"

   196     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> (\<exists>a\<in>A. a \<subseteq> S)"

   197     "\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<inter> b \<in> A"

   198 proof atomize_elim

   199   from first_countable_basisE[of x] guess A' . note A' = this

   200   def A \<equiv> "(\<lambda>N. \<Inter>((\<lambda>n. from_nat_into A' n)  N))  (Collect finite::nat set set)"

   201   thus "\<exists>A. countable A \<and> (\<forall>a. a \<in> A \<longrightarrow> x \<in> a) \<and> (\<forall>a. a \<in> A \<longrightarrow> open a) \<and>

   202         (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> (\<exists>a\<in>A. a \<subseteq> S)) \<and> (\<forall>a b. a \<in> A \<longrightarrow> b \<in> A \<longrightarrow> a \<inter> b \<in> A)"

   203   proof (safe intro!: exI[where x=A])

   204     show "countable A" unfolding A_def by (intro countable_image countable_Collect_finite)

   205     fix a assume "a \<in> A"

   206     thus "x \<in> a" "open a" using A'(4)[OF open_UNIV] by (auto simp: A_def intro: A' from_nat_into)

   207   next

   208     let ?int = "\<lambda>N. \<Inter>from_nat_into A'  N"

   209     fix a b assume "a \<in> A" "b \<in> A"

   210     then obtain N M where "a = ?int N" "b = ?int M" "finite (N \<union> M)" by (auto simp: A_def)

   211     thus "a \<inter> b \<in> A" by (auto simp: A_def intro!: image_eqI[where x="N \<union> M"])

   212   next

   213     fix S assume "open S" "x \<in> S" then obtain a where a: "a\<in>A'" "a \<subseteq> S" using A' by blast

   214     thus "\<exists>a\<in>A. a \<subseteq> S" using a A'

   215       by (intro bexI[where x=a]) (auto simp: A_def intro: image_eqI[where x="{to_nat_on A' a}"])

   216   qed

   217 qed

   218

   219 lemma (in topological_space) first_countableI:

   220   assumes "countable A" and 1: "\<And>a. a \<in> A \<Longrightarrow> x \<in> a" "\<And>a. a \<in> A \<Longrightarrow> open a"

   221    and 2: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>a\<in>A. a \<subseteq> S"

   222   shows "\<exists>A::nat \<Rightarrow> 'a set. (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"

   223 proof (safe intro!: exI[of _ "from_nat_into A"])

   224   have "A \<noteq> {}" using 2[of UNIV] by auto

   225   { fix i show "x \<in> from_nat_into A i" "open (from_nat_into A i)"

   226       using range_from_nat_into_subset[OF A \<noteq> {}] 1 by auto }

   227   { fix S assume "open S" "x\<in>S" from 2[OF this] show "\<exists>i. from_nat_into A i \<subseteq> S"

   228       using subset_range_from_nat_into[OF countable A] by auto }

   229 qed

   230

   231 instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology

   232 proof

   233   fix x :: "'a \<times> 'b"

   234   from first_countable_basisE[of "fst x"] guess A :: "'a set set" . note A = this

   235   from first_countable_basisE[of "snd x"] guess B :: "'b set set" . note B = this

   236   show "\<exists>A::nat \<Rightarrow> ('a \<times> 'b) set. (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"

   237   proof (rule first_countableI[of "(\<lambda>(a, b). a \<times> b)  (A \<times> B)"], safe)

   238     fix a b assume x: "a \<in> A" "b \<in> B"

   239     with A(2, 3)[of a] B(2, 3)[of b] show "x \<in> a \<times> b" "open (a \<times> b)"

   240       unfolding mem_Times_iff by (auto intro: open_Times)

   241   next

   242     fix S assume "open S" "x \<in> S"

   243     from open_prod_elim[OF this] guess a' b' .

   244     moreover with A(4)[of a'] B(4)[of b']

   245     obtain a b where "a \<in> A" "a \<subseteq> a'" "b \<in> B" "b \<subseteq> b'" by auto

   246     ultimately show "\<exists>a\<in>(\<lambda>(a, b). a \<times> b)  (A \<times> B). a \<subseteq> S"

   247       by (auto intro!: bexI[of _ "a \<times> b"] bexI[of _ a] bexI[of _ b])

   248   qed (simp add: A B)

   249 qed

   250

   251 class second_countable_topology = topological_space +

   252   assumes ex_countable_subbasis: "\<exists>B::'a::topological_space set set. countable B \<and> open = generate_topology B"

   253 begin

   254

   255 lemma ex_countable_basis: "\<exists>B::'a set set. countable B \<and> topological_basis B"

   256 proof -

   257   from ex_countable_subbasis obtain B where B: "countable B" "open = generate_topology B" by blast

   258   let ?B = "Inter  {b. finite b \<and> b \<subseteq> B }"

   259

   260   show ?thesis

   261   proof (intro exI conjI)

   262     show "countable ?B"

   263       by (intro countable_image countable_Collect_finite_subset B)

   264     { fix S assume "open S"

   265       then have "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. (\<Union>b\<in>B'. \<Inter>b) = S"

   266         unfolding B

   267       proof induct

   268         case UNIV show ?case by (intro exI[of _ "{{}}"]) simp

   269       next

   270         case (Int a b)

   271         then obtain x y where x: "a = UNION x Inter" "\<And>i. i \<in> x \<Longrightarrow> finite i \<and> i \<subseteq> B"

   272           and y: "b = UNION y Inter" "\<And>i. i \<in> y \<Longrightarrow> finite i \<and> i \<subseteq> B"

   273           by blast

   274         show ?case

   275           unfolding x y Int_UN_distrib2

   276           by (intro exI[of _ "{i \<union> j| i j.  i \<in> x \<and> j \<in> y}"]) (auto dest: x(2) y(2))

   277       next

   278         case (UN K)

   279         then have "\<forall>k\<in>K. \<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. UNION B' Inter = k" by auto

   280         then guess k unfolding bchoice_iff ..

   281         then show "\<exists>B'\<subseteq>{b. finite b \<and> b \<subseteq> B}. UNION B' Inter = \<Union>K"

   282           by (intro exI[of _ "UNION K k"]) auto

   283       next

   284         case (Basis S) then show ?case

   285           by (intro exI[of _ "{{S}}"]) auto

   286       qed

   287       then have "(\<exists>B'\<subseteq>Inter  {b. finite b \<and> b \<subseteq> B}. \<Union>B' = S)"

   288         unfolding subset_image_iff by blast }

   289     then show "topological_basis ?B"

   290       unfolding topological_space_class.topological_basis_def

   291       by (safe intro!: topological_space_class.open_Inter)

   292          (simp_all add: B generate_topology.Basis subset_eq)

   293   qed

   294 qed

   295

   296 end

   297

   298 sublocale second_countable_topology <

   299   countable_basis "SOME B. countable B \<and> topological_basis B"

   300   using someI_ex[OF ex_countable_basis]

   301   by unfold_locales safe

   302

   303 instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology

   304 proof

   305   obtain A :: "'a set set" where "countable A" "topological_basis A"

   306     using ex_countable_basis by auto

   307   moreover

   308   obtain B :: "'b set set" where "countable B" "topological_basis B"

   309     using ex_countable_basis by auto

   310   ultimately show "\<exists>B::('a \<times> 'b) set set. countable B \<and> open = generate_topology B"

   311     by (auto intro!: exI[of _ "(\<lambda>(a, b). a \<times> b)  (A \<times> B)"] topological_basis_prod

   312       topological_basis_imp_subbasis)

   313 qed

   314

   315 instance second_countable_topology \<subseteq> first_countable_topology

   316 proof

   317   fix x :: 'a

   318   def B \<equiv> "SOME B::'a set set. countable B \<and> topological_basis B"

   319   then have B: "countable B" "topological_basis B"

   320     using countable_basis is_basis

   321     by (auto simp: countable_basis is_basis)

   322   then show "\<exists>A::nat \<Rightarrow> 'a set. (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"

   323     by (intro first_countableI[of "{b\<in>B. x \<in> b}"])

   324        (fastforce simp: topological_space_class.topological_basis_def)+

   325 qed

   326

   327 subsection {* Polish spaces *}

   328

   329 text {* Textbooks define Polish spaces as completely metrizable.

   330   We assume the topology to be complete for a given metric. *}

   331

   332 class polish_space = complete_space + second_countable_topology

   333

   334 subsection {* General notion of a topology as a value *}

   335

   336 definition "istopology L \<longleftrightarrow> L {} \<and> (\<forall>S T. L S \<longrightarrow> L T \<longrightarrow> L (S \<inter> T)) \<and> (\<forall>K. Ball K L \<longrightarrow> L (\<Union> K))"

   337 typedef 'a topology = "{L::('a set) \<Rightarrow> bool. istopology L}"

   338   morphisms "openin" "topology"

   339   unfolding istopology_def by blast

   340

   341 lemma istopology_open_in[intro]: "istopology(openin U)"

   342   using openin[of U] by blast

   343

   344 lemma topology_inverse': "istopology U \<Longrightarrow> openin (topology U) = U"

   345   using topology_inverse[unfolded mem_Collect_eq] .

   346

   347 lemma topology_inverse_iff: "istopology U \<longleftrightarrow> openin (topology U) = U"

   348   using topology_inverse[of U] istopology_open_in[of "topology U"] by auto

   349

   350 lemma topology_eq: "T1 = T2 \<longleftrightarrow> (\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S)"

   351 proof-

   352   { assume "T1=T2"

   353     hence "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S" by simp }

   354   moreover

   355   { assume H: "\<forall>S. openin T1 S \<longleftrightarrow> openin T2 S"

   356     hence "openin T1 = openin T2" by (simp add: fun_eq_iff)

   357     hence "topology (openin T1) = topology (openin T2)" by simp

   358     hence "T1 = T2" unfolding openin_inverse .

   359   }

   360   ultimately show ?thesis by blast

   361 qed

   362

   363 text{* Infer the "universe" from union of all sets in the topology. *}

   364

   365 definition "topspace T =  \<Union>{S. openin T S}"

   366

   367 subsubsection {* Main properties of open sets *}

   368

   369 lemma openin_clauses:

   370   fixes U :: "'a topology"

   371   shows "openin U {}"

   372   "\<And>S T. openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S\<inter>T)"

   373   "\<And>K. (\<forall>S \<in> K. openin U S) \<Longrightarrow> openin U (\<Union>K)"

   374   using openin[of U] unfolding istopology_def mem_Collect_eq

   375   by fast+

   376

   377 lemma openin_subset[intro]: "openin U S \<Longrightarrow> S \<subseteq> topspace U"

   378   unfolding topspace_def by blast

   379 lemma openin_empty[simp]: "openin U {}" by (simp add: openin_clauses)

   380

   381 lemma openin_Int[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<inter> T)"

   382   using openin_clauses by simp

   383

   384 lemma openin_Union[intro]: "(\<forall>S \<in>K. openin U S) \<Longrightarrow> openin U (\<Union> K)"

   385   using openin_clauses by simp

   386

   387 lemma openin_Un[intro]: "openin U S \<Longrightarrow> openin U T \<Longrightarrow> openin U (S \<union> T)"

   388   using openin_Union[of "{S,T}" U] by auto

   389

   390 lemma openin_topspace[intro, simp]: "openin U (topspace U)" by (simp add: openin_Union topspace_def)

   391

   392 lemma openin_subopen: "openin U S \<longleftrightarrow> (\<forall>x \<in> S. \<exists>T. openin U T \<and> x \<in> T \<and> T \<subseteq> S)"

   393   (is "?lhs \<longleftrightarrow> ?rhs")

   394 proof

   395   assume ?lhs

   396   then show ?rhs by auto

   397 next

   398   assume H: ?rhs

   399   let ?t = "\<Union>{T. openin U T \<and> T \<subseteq> S}"

   400   have "openin U ?t" by (simp add: openin_Union)

   401   also have "?t = S" using H by auto

   402   finally show "openin U S" .

   403 qed

   404

   405

   406 subsubsection {* Closed sets *}

   407

   408 definition "closedin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> openin U (topspace U - S)"

   409

   410 lemma closedin_subset: "closedin U S \<Longrightarrow> S \<subseteq> topspace U" by (metis closedin_def)

   411 lemma closedin_empty[simp]: "closedin U {}" by (simp add: closedin_def)

   412 lemma closedin_topspace[intro,simp]:

   413   "closedin U (topspace U)" by (simp add: closedin_def)

   414 lemma closedin_Un[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<union> T)"

   415   by (auto simp add: Diff_Un closedin_def)

   416

   417 lemma Diff_Inter[intro]: "A - \<Inter>S = \<Union> {A - s|s. s\<in>S}" by auto

   418 lemma closedin_Inter[intro]: assumes Ke: "K \<noteq> {}" and Kc: "\<forall>S \<in>K. closedin U S"

   419   shows "closedin U (\<Inter> K)"  using Ke Kc unfolding closedin_def Diff_Inter by auto

   420

   421 lemma closedin_Int[intro]: "closedin U S \<Longrightarrow> closedin U T \<Longrightarrow> closedin U (S \<inter> T)"

   422   using closedin_Inter[of "{S,T}" U] by auto

   423

   424 lemma Diff_Diff_Int: "A - (A - B) = A \<inter> B" by blast

   425 lemma openin_closedin_eq: "openin U S \<longleftrightarrow> S \<subseteq> topspace U \<and> closedin U (topspace U - S)"

   426   apply (auto simp add: closedin_def Diff_Diff_Int inf_absorb2)

   427   apply (metis openin_subset subset_eq)

   428   done

   429

   430 lemma openin_closedin:  "S \<subseteq> topspace U \<Longrightarrow> (openin U S \<longleftrightarrow> closedin U (topspace U - S))"

   431   by (simp add: openin_closedin_eq)

   432

   433 lemma openin_diff[intro]: assumes oS: "openin U S" and cT: "closedin U T" shows "openin U (S - T)"

   434 proof-

   435   have "S - T = S \<inter> (topspace U - T)" using openin_subset[of U S]  oS cT

   436     by (auto simp add: topspace_def openin_subset)

   437   then show ?thesis using oS cT by (auto simp add: closedin_def)

   438 qed

   439

   440 lemma closedin_diff[intro]: assumes oS: "closedin U S" and cT: "openin U T" shows "closedin U (S - T)"

   441 proof-

   442   have "S - T = S \<inter> (topspace U - T)" using closedin_subset[of U S]  oS cT

   443     by (auto simp add: topspace_def )

   444   then show ?thesis using oS cT by (auto simp add: openin_closedin_eq)

   445 qed

   446

   447 subsubsection {* Subspace topology *}

   448

   449 definition "subtopology U V = topology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"

   450

   451 lemma istopology_subtopology: "istopology (\<lambda>T. \<exists>S. T = S \<inter> V \<and> openin U S)"

   452   (is "istopology ?L")

   453 proof-

   454   have "?L {}" by blast

   455   {fix A B assume A: "?L A" and B: "?L B"

   456     from A B obtain Sa and Sb where Sa: "openin U Sa" "A = Sa \<inter> V" and Sb: "openin U Sb" "B = Sb \<inter> V" by blast

   457     have "A\<inter>B = (Sa \<inter> Sb) \<inter> V" "openin U (Sa \<inter> Sb)"  using Sa Sb by blast+

   458     then have "?L (A \<inter> B)" by blast}

   459   moreover

   460   {fix K assume K: "K \<subseteq> Collect ?L"

   461     have th0: "Collect ?L = (\<lambda>S. S \<inter> V)  Collect (openin U)"

   462       apply (rule set_eqI)

   463       apply (simp add: Ball_def image_iff)

   464       by metis

   465     from K[unfolded th0 subset_image_iff]

   466     obtain Sk where Sk: "Sk \<subseteq> Collect (openin U)" "K = (\<lambda>S. S \<inter> V)  Sk" by blast

   467     have "\<Union>K = (\<Union>Sk) \<inter> V" using Sk by auto

   468     moreover have "openin U (\<Union> Sk)" using Sk by (auto simp add: subset_eq)

   469     ultimately have "?L (\<Union>K)" by blast}

   470   ultimately show ?thesis

   471     unfolding subset_eq mem_Collect_eq istopology_def by blast

   472 qed

   473

   474 lemma openin_subtopology:

   475   "openin (subtopology U V) S \<longleftrightarrow> (\<exists> T. (openin U T) \<and> (S = T \<inter> V))"

   476   unfolding subtopology_def topology_inverse'[OF istopology_subtopology]

   477   by auto

   478

   479 lemma topspace_subtopology: "topspace(subtopology U V) = topspace U \<inter> V"

   480   by (auto simp add: topspace_def openin_subtopology)

   481

   482 lemma closedin_subtopology:

   483   "closedin (subtopology U V) S \<longleftrightarrow> (\<exists>T. closedin U T \<and> S = T \<inter> V)"

   484   unfolding closedin_def topspace_subtopology

   485   apply (simp add: openin_subtopology)

   486   apply (rule iffI)

   487   apply clarify

   488   apply (rule_tac x="topspace U - T" in exI)

   489   by auto

   490

   491 lemma openin_subtopology_refl: "openin (subtopology U V) V \<longleftrightarrow> V \<subseteq> topspace U"

   492   unfolding openin_subtopology

   493   apply (rule iffI, clarify)

   494   apply (frule openin_subset[of U])  apply blast

   495   apply (rule exI[where x="topspace U"])

   496   apply auto

   497   done

   498

   499 lemma subtopology_superset:

   500   assumes UV: "topspace U \<subseteq> V"

   501   shows "subtopology U V = U"

   502 proof-

   503   {fix S

   504     {fix T assume T: "openin U T" "S = T \<inter> V"

   505       from T openin_subset[OF T(1)] UV have eq: "S = T" by blast

   506       have "openin U S" unfolding eq using T by blast}

   507     moreover

   508     {assume S: "openin U S"

   509       hence "\<exists>T. openin U T \<and> S = T \<inter> V"

   510         using openin_subset[OF S] UV by auto}

   511     ultimately have "(\<exists>T. openin U T \<and> S = T \<inter> V) \<longleftrightarrow> openin U S" by blast}

   512   then show ?thesis unfolding topology_eq openin_subtopology by blast

   513 qed

   514

   515 lemma subtopology_topspace[simp]: "subtopology U (topspace U) = U"

   516   by (simp add: subtopology_superset)

   517

   518 lemma subtopology_UNIV[simp]: "subtopology U UNIV = U"

   519   by (simp add: subtopology_superset)

   520

   521 subsubsection {* The standard Euclidean topology *}

   522

   523 definition

   524   euclidean :: "'a::topological_space topology" where

   525   "euclidean = topology open"

   526

   527 lemma open_openin: "open S \<longleftrightarrow> openin euclidean S"

   528   unfolding euclidean_def

   529   apply (rule cong[where x=S and y=S])

   530   apply (rule topology_inverse[symmetric])

   531   apply (auto simp add: istopology_def)

   532   done

   533

   534 lemma topspace_euclidean: "topspace euclidean = UNIV"

   535   apply (simp add: topspace_def)

   536   apply (rule set_eqI)

   537   by (auto simp add: open_openin[symmetric])

   538

   539 lemma topspace_euclidean_subtopology[simp]: "topspace (subtopology euclidean S) = S"

   540   by (simp add: topspace_euclidean topspace_subtopology)

   541

   542 lemma closed_closedin: "closed S \<longleftrightarrow> closedin euclidean S"

   543   by (simp add: closed_def closedin_def topspace_euclidean open_openin Compl_eq_Diff_UNIV)

   544

   545 lemma open_subopen: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S)"

   546   by (simp add: open_openin openin_subopen[symmetric])

   547

   548 text {* Basic "localization" results are handy for connectedness. *}

   549

   550 lemma openin_open: "openin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. open T \<and> (S = U \<inter> T))"

   551   by (auto simp add: openin_subtopology open_openin[symmetric])

   552

   553 lemma openin_open_Int[intro]: "open S \<Longrightarrow> openin (subtopology euclidean U) (U \<inter> S)"

   554   by (auto simp add: openin_open)

   555

   556 lemma open_openin_trans[trans]:

   557  "open S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> openin (subtopology euclidean S) T"

   558   by (metis Int_absorb1  openin_open_Int)

   559

   560 lemma open_subset:  "S \<subseteq> T \<Longrightarrow> open S \<Longrightarrow> openin (subtopology euclidean T) S"

   561   by (auto simp add: openin_open)

   562

   563 lemma closedin_closed: "closedin (subtopology euclidean U) S \<longleftrightarrow> (\<exists>T. closed T \<and> S = U \<inter> T)"

   564   by (simp add: closedin_subtopology closed_closedin Int_ac)

   565

   566 lemma closedin_closed_Int: "closed S ==> closedin (subtopology euclidean U) (U \<inter> S)"

   567   by (metis closedin_closed)

   568

   569 lemma closed_closedin_trans: "closed S \<Longrightarrow> closed T \<Longrightarrow> T \<subseteq> S \<Longrightarrow> closedin (subtopology euclidean S) T"

   570   apply (subgoal_tac "S \<inter> T = T" )

   571   apply auto

   572   apply (frule closedin_closed_Int[of T S])

   573   by simp

   574

   575 lemma closed_subset: "S \<subseteq> T \<Longrightarrow> closed S \<Longrightarrow> closedin (subtopology euclidean T) S"

   576   by (auto simp add: closedin_closed)

   577

   578 lemma openin_euclidean_subtopology_iff:

   579   fixes S U :: "'a::metric_space set"

   580   shows "openin (subtopology euclidean U) S

   581   \<longleftrightarrow> S \<subseteq> U \<and> (\<forall>x\<in>S. \<exists>e>0. \<forall>x'\<in>U. dist x' x < e \<longrightarrow> x'\<in> S)" (is "?lhs \<longleftrightarrow> ?rhs")

   582 proof

   583   assume ?lhs thus ?rhs unfolding openin_open open_dist by blast

   584 next

   585   def T \<equiv> "{x. \<exists>a\<in>S. \<exists>d>0. (\<forall>y\<in>U. dist y a < d \<longrightarrow> y \<in> S) \<and> dist x a < d}"

   586   have 1: "\<forall>x\<in>T. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> T"

   587     unfolding T_def

   588     apply clarsimp

   589     apply (rule_tac x="d - dist x a" in exI)

   590     apply (clarsimp simp add: less_diff_eq)

   591     apply (erule rev_bexI)

   592     apply (rule_tac x=d in exI, clarify)

   593     apply (erule le_less_trans [OF dist_triangle])

   594     done

   595   assume ?rhs hence 2: "S = U \<inter> T"

   596     unfolding T_def

   597     apply auto

   598     apply (drule (1) bspec, erule rev_bexI)

   599     apply auto

   600     done

   601   from 1 2 show ?lhs

   602     unfolding openin_open open_dist by fast

   603 qed

   604

   605 text {* These "transitivity" results are handy too *}

   606

   607 lemma openin_trans[trans]: "openin (subtopology euclidean T) S \<Longrightarrow> openin (subtopology euclidean U) T

   608   \<Longrightarrow> openin (subtopology euclidean U) S"

   609   unfolding open_openin openin_open by blast

   610

   611 lemma openin_open_trans: "openin (subtopology euclidean T) S \<Longrightarrow> open T \<Longrightarrow> open S"

   612   by (auto simp add: openin_open intro: openin_trans)

   613

   614 lemma closedin_trans[trans]:

   615  "closedin (subtopology euclidean T) S \<Longrightarrow>

   616            closedin (subtopology euclidean U) T

   617            ==> closedin (subtopology euclidean U) S"

   618   by (auto simp add: closedin_closed closed_closedin closed_Inter Int_assoc)

   619

   620 lemma closedin_closed_trans: "closedin (subtopology euclidean T) S \<Longrightarrow> closed T \<Longrightarrow> closed S"

   621   by (auto simp add: closedin_closed intro: closedin_trans)

   622

   623

   624 subsection {* Open and closed balls *}

   625

   626 definition

   627   ball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where

   628   "ball x e = {y. dist x y < e}"

   629

   630 definition

   631   cball :: "'a::metric_space \<Rightarrow> real \<Rightarrow> 'a set" where

   632   "cball x e = {y. dist x y \<le> e}"

   633

   634 lemma mem_ball [simp]: "y \<in> ball x e \<longleftrightarrow> dist x y < e"

   635   by (simp add: ball_def)

   636

   637 lemma mem_cball [simp]: "y \<in> cball x e \<longleftrightarrow> dist x y \<le> e"

   638   by (simp add: cball_def)

   639

   640 lemma mem_ball_0:

   641   fixes x :: "'a::real_normed_vector"

   642   shows "x \<in> ball 0 e \<longleftrightarrow> norm x < e"

   643   by (simp add: dist_norm)

   644

   645 lemma mem_cball_0:

   646   fixes x :: "'a::real_normed_vector"

   647   shows "x \<in> cball 0 e \<longleftrightarrow> norm x \<le> e"

   648   by (simp add: dist_norm)

   649

   650 lemma centre_in_ball: "x \<in> ball x e \<longleftrightarrow> 0 < e"

   651   by simp

   652

   653 lemma centre_in_cball: "x \<in> cball x e \<longleftrightarrow> 0 \<le> e"

   654   by simp

   655

   656 lemma ball_subset_cball[simp,intro]: "ball x e \<subseteq> cball x e" by (simp add: subset_eq)

   657 lemma subset_ball[intro]: "d <= e ==> ball x d \<subseteq> ball x e" by (simp add: subset_eq)

   658 lemma subset_cball[intro]: "d <= e ==> cball x d \<subseteq> cball x e" by (simp add: subset_eq)

   659 lemma ball_max_Un: "ball a (max r s) = ball a r \<union> ball a s"

   660   by (simp add: set_eq_iff) arith

   661

   662 lemma ball_min_Int: "ball a (min r s) = ball a r \<inter> ball a s"

   663   by (simp add: set_eq_iff)

   664

   665 lemma diff_less_iff: "(a::real) - b > 0 \<longleftrightarrow> a > b"

   666   "(a::real) - b < 0 \<longleftrightarrow> a < b"

   667   "a - b < c \<longleftrightarrow> a < c +b" "a - b > c \<longleftrightarrow> a > c +b" by arith+

   668 lemma diff_le_iff: "(a::real) - b \<ge> 0 \<longleftrightarrow> a \<ge> b" "(a::real) - b \<le> 0 \<longleftrightarrow> a \<le> b"

   669   "a - b \<le> c \<longleftrightarrow> a \<le> c +b" "a - b \<ge> c \<longleftrightarrow> a \<ge> c +b"  by arith+

   670

   671 lemma open_ball[intro, simp]: "open (ball x e)"

   672   unfolding open_dist ball_def mem_Collect_eq Ball_def

   673   unfolding dist_commute

   674   apply clarify

   675   apply (rule_tac x="e - dist xa x" in exI)

   676   using dist_triangle_alt[where z=x]

   677   apply (clarsimp simp add: diff_less_iff)

   678   apply atomize

   679   apply (erule_tac x="y" in allE)

   680   apply (erule_tac x="xa" in allE)

   681   by arith

   682

   683 lemma open_contains_ball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. ball x e \<subseteq> S)"

   684   unfolding open_dist subset_eq mem_ball Ball_def dist_commute ..

   685

   686 lemma openE[elim?]:

   687   assumes "open S" "x\<in>S"

   688   obtains e where "e>0" "ball x e \<subseteq> S"

   689   using assms unfolding open_contains_ball by auto

   690

   691 lemma open_contains_ball_eq: "open S \<Longrightarrow> \<forall>x. x\<in>S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"

   692   by (metis open_contains_ball subset_eq centre_in_ball)

   693

   694 lemma ball_eq_empty[simp]: "ball x e = {} \<longleftrightarrow> e \<le> 0"

   695   unfolding mem_ball set_eq_iff

   696   apply (simp add: not_less)

   697   by (metis zero_le_dist order_trans dist_self)

   698

   699 lemma ball_empty[intro]: "e \<le> 0 ==> ball x e = {}" by simp

   700

   701 lemma euclidean_dist_l2:

   702   fixes x y :: "'a :: euclidean_space"

   703   shows "dist x y = setL2 (\<lambda>i. dist (x \<bullet> i) (y \<bullet> i)) Basis"

   704   unfolding dist_norm norm_eq_sqrt_inner setL2_def

   705   by (subst euclidean_inner) (simp add: power2_eq_square inner_diff_left)

   706

   707 definition "box a b = {x. \<forall>i\<in>Basis. a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i}"

   708

   709 lemma rational_boxes:

   710   fixes x :: "'a\<Colon>euclidean_space"

   711   assumes "0 < e"

   712   shows "\<exists>a b. (\<forall>i\<in>Basis. a \<bullet> i \<in> \<rat> \<and> b \<bullet> i \<in> \<rat> ) \<and> x \<in> box a b \<and> box a b \<subseteq> ball x e"

   713 proof -

   714   def e' \<equiv> "e / (2 * sqrt (real (DIM ('a))))"

   715   then have e: "0 < e'" using assms by (auto intro!: divide_pos_pos simp: DIM_positive)

   716   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> y < x \<bullet> i \<and> x \<bullet> i - y < e'" (is "\<forall>i. ?th i")

   717   proof

   718     fix i from Rats_dense_in_real[of "x \<bullet> i - e'" "x \<bullet> i"] e show "?th i" by auto

   719   qed

   720   from choice[OF this] guess a .. note a = this

   721   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> x \<bullet> i < y \<and> y - x \<bullet> i < e'" (is "\<forall>i. ?th i")

   722   proof

   723     fix i from Rats_dense_in_real[of "x \<bullet> i" "x \<bullet> i + e'"] e show "?th i" by auto

   724   qed

   725   from choice[OF this] guess b .. note b = this

   726   let ?a = "\<Sum>i\<in>Basis. a i *\<^sub>R i" and ?b = "\<Sum>i\<in>Basis. b i *\<^sub>R i"

   727   show ?thesis

   728   proof (rule exI[of _ ?a], rule exI[of _ ?b], safe)

   729     fix y :: 'a assume *: "y \<in> box ?a ?b"

   730     have "dist x y = sqrt (\<Sum>i\<in>Basis. (dist (x \<bullet> i) (y \<bullet> i))\<twosuperior>)"

   731       unfolding setL2_def[symmetric] by (rule euclidean_dist_l2)

   732     also have "\<dots> < sqrt (\<Sum>(i::'a)\<in>Basis. e^2 / real (DIM('a)))"

   733     proof (rule real_sqrt_less_mono, rule setsum_strict_mono)

   734       fix i :: "'a" assume i: "i \<in> Basis"

   735       have "a i < y\<bullet>i \<and> y\<bullet>i < b i" using * i by (auto simp: box_def)

   736       moreover have "a i < x\<bullet>i" "x\<bullet>i - a i < e'" using a by auto

   737       moreover have "x\<bullet>i < b i" "b i - x\<bullet>i < e'" using b by auto

   738       ultimately have "\<bar>x\<bullet>i - y\<bullet>i\<bar> < 2 * e'" by auto

   739       then have "dist (x \<bullet> i) (y \<bullet> i) < e/sqrt (real (DIM('a)))"

   740         unfolding e'_def by (auto simp: dist_real_def)

   741       then have "(dist (x \<bullet> i) (y \<bullet> i))\<twosuperior> < (e/sqrt (real (DIM('a))))\<twosuperior>"

   742         by (rule power_strict_mono) auto

   743       then show "(dist (x \<bullet> i) (y \<bullet> i))\<twosuperior> < e\<twosuperior> / real DIM('a)"

   744         by (simp add: power_divide)

   745     qed auto

   746     also have "\<dots> = e" using 0 < e by (simp add: real_eq_of_nat)

   747     finally show "y \<in> ball x e" by (auto simp: ball_def)

   748   qed (insert a b, auto simp: box_def)

   749 qed

   750

   751 lemma open_UNION_box:

   752   fixes M :: "'a\<Colon>euclidean_space set"

   753   assumes "open M"

   754   defines "a' \<equiv> \<lambda>f :: 'a \<Rightarrow> real \<times> real. (\<Sum>(i::'a)\<in>Basis. fst (f i) *\<^sub>R i)"

   755   defines "b' \<equiv> \<lambda>f :: 'a \<Rightarrow> real \<times> real. (\<Sum>(i::'a)\<in>Basis. snd (f i) *\<^sub>R i)"

   756   defines "I \<equiv> {f\<in>Basis \<rightarrow>\<^isub>E \<rat> \<times> \<rat>. box (a' f) (b' f) \<subseteq> M}"

   757   shows "M = (\<Union>f\<in>I. box (a' f) (b' f))"

   758 proof safe

   759   fix x assume "x \<in> M"

   760   obtain e where e: "e > 0" "ball x e \<subseteq> M"

   761     using openE[OF open M x \<in> M] by auto

   762   moreover then obtain a b where ab: "x \<in> box a b"

   763     "\<forall>i \<in> Basis. a \<bullet> i \<in> \<rat>" "\<forall>i\<in>Basis. b \<bullet> i \<in> \<rat>" "box a b \<subseteq> ball x e"

   764     using rational_boxes[OF e(1)] by metis

   765   ultimately show "x \<in> (\<Union>f\<in>I. box (a' f) (b' f))"

   766      by (intro UN_I[of "\<lambda>i\<in>Basis. (a \<bullet> i, b \<bullet> i)"])

   767         (auto simp: euclidean_representation I_def a'_def b'_def)

   768 qed (auto simp: I_def)

   769

   770 subsection{* Connectedness *}

   771

   772 definition "connected S \<longleftrightarrow>

   773   ~(\<exists>e1 e2. open e1 \<and> open e2 \<and> S \<subseteq> (e1 \<union> e2) \<and> (e1 \<inter> e2 \<inter> S = {})

   774   \<and> ~(e1 \<inter> S = {}) \<and> ~(e2 \<inter> S = {}))"

   775

   776 lemma connected_local:

   777  "connected S \<longleftrightarrow> ~(\<exists>e1 e2.

   778                  openin (subtopology euclidean S) e1 \<and>

   779                  openin (subtopology euclidean S) e2 \<and>

   780                  S \<subseteq> e1 \<union> e2 \<and>

   781                  e1 \<inter> e2 = {} \<and>

   782                  ~(e1 = {}) \<and>

   783                  ~(e2 = {}))"

   784 unfolding connected_def openin_open by (safe, blast+)

   785

   786 lemma exists_diff:

   787   fixes P :: "'a set \<Rightarrow> bool"

   788   shows "(\<exists>S. P(- S)) \<longleftrightarrow> (\<exists>S. P S)" (is "?lhs \<longleftrightarrow> ?rhs")

   789 proof-

   790   {assume "?lhs" hence ?rhs by blast }

   791   moreover

   792   {fix S assume H: "P S"

   793     have "S = - (- S)" by auto

   794     with H have "P (- (- S))" by metis }

   795   ultimately show ?thesis by metis

   796 qed

   797

   798 lemma connected_clopen: "connected S \<longleftrightarrow>

   799         (\<forall>T. openin (subtopology euclidean S) T \<and>

   800             closedin (subtopology euclidean S) T \<longrightarrow> T = {} \<or> T = S)" (is "?lhs \<longleftrightarrow> ?rhs")

   801 proof-

   802   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> {})"

   803     unfolding connected_def openin_open closedin_closed

   804     apply (subst exists_diff) by blast

   805   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> {})"

   806     (is " _ \<longleftrightarrow> \<not> (\<exists>e2 e1. ?P e2 e1)") apply (simp add: closed_def) by metis

   807

   808   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'))"

   809     (is "_ \<longleftrightarrow> \<not> (\<exists>t' t. ?Q t' t)")

   810     unfolding connected_def openin_open closedin_closed by auto

   811   {fix e2

   812     {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)"

   813         by auto}

   814     then have "(\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by metis}

   815   then have "\<forall>e2. (\<exists>e1. ?P e2 e1) \<longleftrightarrow> (\<exists>t. ?Q e2 t)" by blast

   816   then show ?thesis unfolding th0 th1 by simp

   817 qed

   818

   819 lemma connected_empty[simp, intro]: "connected {}"

   820   by (simp add: connected_def)

   821

   822

   823 subsection{* Limit points *}

   824

   825 definition (in topological_space)

   826   islimpt:: "'a \<Rightarrow> 'a set \<Rightarrow> bool" (infixr "islimpt" 60) where

   827   "x islimpt S \<longleftrightarrow> (\<forall>T. x\<in>T \<longrightarrow> open T \<longrightarrow> (\<exists>y\<in>S. y\<in>T \<and> y\<noteq>x))"

   828

   829 lemma islimptI:

   830   assumes "\<And>T. x \<in> T \<Longrightarrow> open T \<Longrightarrow> \<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"

   831   shows "x islimpt S"

   832   using assms unfolding islimpt_def by auto

   833

   834 lemma islimptE:

   835   assumes "x islimpt S" and "x \<in> T" and "open T"

   836   obtains y where "y \<in> S" and "y \<in> T" and "y \<noteq> x"

   837   using assms unfolding islimpt_def by auto

   838

   839 lemma islimpt_iff_eventually: "x islimpt S \<longleftrightarrow> \<not> eventually (\<lambda>y. y \<notin> S) (at x)"

   840   unfolding islimpt_def eventually_at_topological by auto

   841

   842 lemma islimpt_subset: "\<lbrakk>x islimpt S; S \<subseteq> T\<rbrakk> \<Longrightarrow> x islimpt T"

   843   unfolding islimpt_def by fast

   844

   845 lemma islimpt_approachable:

   846   fixes x :: "'a::metric_space"

   847   shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e)"

   848   unfolding islimpt_iff_eventually eventually_at by fast

   849

   850 lemma islimpt_approachable_le:

   851   fixes x :: "'a::metric_space"

   852   shows "x islimpt S \<longleftrightarrow> (\<forall>e>0. \<exists>x'\<in> S. x' \<noteq> x \<and> dist x' x <= e)"

   853   unfolding islimpt_approachable

   854   using approachable_lt_le [where f="\<lambda>y. dist y x" and P="\<lambda>y. y \<notin> S \<or> y = x",

   855     THEN arg_cong [where f=Not]]

   856   by (simp add: Bex_def conj_commute conj_left_commute)

   857

   858 lemma islimpt_UNIV_iff: "x islimpt UNIV \<longleftrightarrow> \<not> open {x}"

   859   unfolding islimpt_def by (safe, fast, case_tac "T = {x}", fast, fast)

   860

   861 lemma islimpt_punctured: "x islimpt S = x islimpt (S-{x})"

   862   unfolding islimpt_def by blast

   863

   864 text {* A perfect space has no isolated points. *}

   865

   866 lemma islimpt_UNIV [simp, intro]: "(x::'a::perfect_space) islimpt UNIV"

   867   unfolding islimpt_UNIV_iff by (rule not_open_singleton)

   868

   869 lemma perfect_choose_dist:

   870   fixes x :: "'a::{perfect_space, metric_space}"

   871   shows "0 < r \<Longrightarrow> \<exists>a. a \<noteq> x \<and> dist a x < r"

   872 using islimpt_UNIV [of x]

   873 by (simp add: islimpt_approachable)

   874

   875 lemma closed_limpt: "closed S \<longleftrightarrow> (\<forall>x. x islimpt S \<longrightarrow> x \<in> S)"

   876   unfolding closed_def

   877   apply (subst open_subopen)

   878   apply (simp add: islimpt_def subset_eq)

   879   by (metis ComplE ComplI)

   880

   881 lemma islimpt_EMPTY[simp]: "\<not> x islimpt {}"

   882   unfolding islimpt_def by auto

   883

   884 lemma finite_set_avoid:

   885   fixes a :: "'a::metric_space"

   886   assumes fS: "finite S" shows  "\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<longrightarrow> d <= dist a x"

   887 proof(induct rule: finite_induct[OF fS])

   888   case 1 thus ?case by (auto intro: zero_less_one)

   889 next

   890   case (2 x F)

   891   from 2 obtain d where d: "d >0" "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> d \<le> dist a x" by blast

   892   {assume "x = a" hence ?case using d by auto  }

   893   moreover

   894   {assume xa: "x\<noteq>a"

   895     let ?d = "min d (dist a x)"

   896     have dp: "?d > 0" using xa d(1) using dist_nz by auto

   897     from d have d': "\<forall>x\<in>F. x\<noteq>a \<longrightarrow> ?d \<le> dist a x" by auto

   898     with dp xa have ?case by(auto intro!: exI[where x="?d"]) }

   899   ultimately show ?case by blast

   900 qed

   901

   902 lemma islimpt_Un: "x islimpt (S \<union> T) \<longleftrightarrow> x islimpt S \<or> x islimpt T"

   903   by (simp add: islimpt_iff_eventually eventually_conj_iff)

   904

   905 lemma discrete_imp_closed:

   906   fixes S :: "'a::metric_space set"

   907   assumes e: "0 < e" and d: "\<forall>x \<in> S. \<forall>y \<in> S. dist y x < e \<longrightarrow> y = x"

   908   shows "closed S"

   909 proof-

   910   {fix x assume C: "\<forall>e>0. \<exists>x'\<in>S. x' \<noteq> x \<and> dist x' x < e"

   911     from e have e2: "e/2 > 0" by arith

   912     from C[rule_format, OF e2] obtain y where y: "y \<in> S" "y\<noteq>x" "dist y x < e/2" by blast

   913     let ?m = "min (e/2) (dist x y) "

   914     from e2 y(2) have mp: "?m > 0" by (simp add: dist_nz[THEN sym])

   915     from C[rule_format, OF mp] obtain z where z: "z \<in> S" "z\<noteq>x" "dist z x < ?m" by blast

   916     have th: "dist z y < e" using z y

   917       by (intro dist_triangle_lt [where z=x], simp)

   918     from d[rule_format, OF y(1) z(1) th] y z

   919     have False by (auto simp add: dist_commute)}

   920   then show ?thesis by (metis islimpt_approachable closed_limpt [where 'a='a])

   921 qed

   922

   923

   924 subsection {* Interior of a Set *}

   925

   926 definition "interior S = \<Union>{T. open T \<and> T \<subseteq> S}"

   927

   928 lemma interiorI [intro?]:

   929   assumes "open T" and "x \<in> T" and "T \<subseteq> S"

   930   shows "x \<in> interior S"

   931   using assms unfolding interior_def by fast

   932

   933 lemma interiorE [elim?]:

   934   assumes "x \<in> interior S"

   935   obtains T where "open T" and "x \<in> T" and "T \<subseteq> S"

   936   using assms unfolding interior_def by fast

   937

   938 lemma open_interior [simp, intro]: "open (interior S)"

   939   by (simp add: interior_def open_Union)

   940

   941 lemma interior_subset: "interior S \<subseteq> S"

   942   by (auto simp add: interior_def)

   943

   944 lemma interior_maximal: "T \<subseteq> S \<Longrightarrow> open T \<Longrightarrow> T \<subseteq> interior S"

   945   by (auto simp add: interior_def)

   946

   947 lemma interior_open: "open S \<Longrightarrow> interior S = S"

   948   by (intro equalityI interior_subset interior_maximal subset_refl)

   949

   950 lemma interior_eq: "interior S = S \<longleftrightarrow> open S"

   951   by (metis open_interior interior_open)

   952

   953 lemma open_subset_interior: "open S \<Longrightarrow> S \<subseteq> interior T \<longleftrightarrow> S \<subseteq> T"

   954   by (metis interior_maximal interior_subset subset_trans)

   955

   956 lemma interior_empty [simp]: "interior {} = {}"

   957   using open_empty by (rule interior_open)

   958

   959 lemma interior_UNIV [simp]: "interior UNIV = UNIV"

   960   using open_UNIV by (rule interior_open)

   961

   962 lemma interior_interior [simp]: "interior (interior S) = interior S"

   963   using open_interior by (rule interior_open)

   964

   965 lemma interior_mono: "S \<subseteq> T \<Longrightarrow> interior S \<subseteq> interior T"

   966   by (auto simp add: interior_def)

   967

   968 lemma interior_unique:

   969   assumes "T \<subseteq> S" and "open T"

   970   assumes "\<And>T'. T' \<subseteq> S \<Longrightarrow> open T' \<Longrightarrow> T' \<subseteq> T"

   971   shows "interior S = T"

   972   by (intro equalityI assms interior_subset open_interior interior_maximal)

   973

   974 lemma interior_inter [simp]: "interior (S \<inter> T) = interior S \<inter> interior T"

   975   by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1

   976     Int_lower2 interior_maximal interior_subset open_Int open_interior)

   977

   978 lemma mem_interior: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. ball x e \<subseteq> S)"

   979   using open_contains_ball_eq [where S="interior S"]

   980   by (simp add: open_subset_interior)

   981

   982 lemma interior_limit_point [intro]:

   983   fixes x :: "'a::perfect_space"

   984   assumes x: "x \<in> interior S" shows "x islimpt S"

   985   using x islimpt_UNIV [of x]

   986   unfolding interior_def islimpt_def

   987   apply (clarsimp, rename_tac T T')

   988   apply (drule_tac x="T \<inter> T'" in spec)

   989   apply (auto simp add: open_Int)

   990   done

   991

   992 lemma interior_closed_Un_empty_interior:

   993   assumes cS: "closed S" and iT: "interior T = {}"

   994   shows "interior (S \<union> T) = interior S"

   995 proof

   996   show "interior S \<subseteq> interior (S \<union> T)"

   997     by (rule interior_mono, rule Un_upper1)

   998 next

   999   show "interior (S \<union> T) \<subseteq> interior S"

  1000   proof

  1001     fix x assume "x \<in> interior (S \<union> T)"

  1002     then obtain R where "open R" "x \<in> R" "R \<subseteq> S \<union> T" ..

  1003     show "x \<in> interior S"

  1004     proof (rule ccontr)

  1005       assume "x \<notin> interior S"

  1006       with x \<in> R open R obtain y where "y \<in> R - S"

  1007         unfolding interior_def by fast

  1008       from open R closed S have "open (R - S)" by (rule open_Diff)

  1009       from R \<subseteq> S \<union> T have "R - S \<subseteq> T" by fast

  1010       from y \<in> R - S open (R - S) R - S \<subseteq> T interior T = {}

  1011       show "False" unfolding interior_def by fast

  1012     qed

  1013   qed

  1014 qed

  1015

  1016 lemma interior_Times: "interior (A \<times> B) = interior A \<times> interior B"

  1017 proof (rule interior_unique)

  1018   show "interior A \<times> interior B \<subseteq> A \<times> B"

  1019     by (intro Sigma_mono interior_subset)

  1020   show "open (interior A \<times> interior B)"

  1021     by (intro open_Times open_interior)

  1022   fix T assume "T \<subseteq> A \<times> B" and "open T" thus "T \<subseteq> interior A \<times> interior B"

  1023   proof (safe)

  1024     fix x y assume "(x, y) \<in> T"

  1025     then obtain C D where "open C" "open D" "C \<times> D \<subseteq> T" "x \<in> C" "y \<in> D"

  1026       using open T unfolding open_prod_def by fast

  1027     hence "open C" "open D" "C \<subseteq> A" "D \<subseteq> B" "x \<in> C" "y \<in> D"

  1028       using T \<subseteq> A \<times> B by auto

  1029     thus "x \<in> interior A" and "y \<in> interior B"

  1030       by (auto intro: interiorI)

  1031   qed

  1032 qed

  1033

  1034

  1035 subsection {* Closure of a Set *}

  1036

  1037 definition "closure S = S \<union> {x | x. x islimpt S}"

  1038

  1039 lemma interior_closure: "interior S = - (closure (- S))"

  1040   unfolding interior_def closure_def islimpt_def by auto

  1041

  1042 lemma closure_interior: "closure S = - interior (- S)"

  1043   unfolding interior_closure by simp

  1044

  1045 lemma closed_closure[simp, intro]: "closed (closure S)"

  1046   unfolding closure_interior by (simp add: closed_Compl)

  1047

  1048 lemma closure_subset: "S \<subseteq> closure S"

  1049   unfolding closure_def by simp

  1050

  1051 lemma closure_hull: "closure S = closed hull S"

  1052   unfolding hull_def closure_interior interior_def by auto

  1053

  1054 lemma closure_eq: "closure S = S \<longleftrightarrow> closed S"

  1055   unfolding closure_hull using closed_Inter by (rule hull_eq)

  1056

  1057 lemma closure_closed [simp]: "closed S \<Longrightarrow> closure S = S"

  1058   unfolding closure_eq .

  1059

  1060 lemma closure_closure [simp]: "closure (closure S) = closure S"

  1061   unfolding closure_hull by (rule hull_hull)

  1062

  1063 lemma closure_mono: "S \<subseteq> T \<Longrightarrow> closure S \<subseteq> closure T"

  1064   unfolding closure_hull by (rule hull_mono)

  1065

  1066 lemma closure_minimal: "S \<subseteq> T \<Longrightarrow> closed T \<Longrightarrow> closure S \<subseteq> T"

  1067   unfolding closure_hull by (rule hull_minimal)

  1068

  1069 lemma closure_unique:

  1070   assumes "S \<subseteq> T" and "closed T"

  1071   assumes "\<And>T'. S \<subseteq> T' \<Longrightarrow> closed T' \<Longrightarrow> T \<subseteq> T'"

  1072   shows "closure S = T"

  1073   using assms unfolding closure_hull by (rule hull_unique)

  1074

  1075 lemma closure_empty [simp]: "closure {} = {}"

  1076   using closed_empty by (rule closure_closed)

  1077

  1078 lemma closure_UNIV [simp]: "closure UNIV = UNIV"

  1079   using closed_UNIV by (rule closure_closed)

  1080

  1081 lemma closure_union [simp]: "closure (S \<union> T) = closure S \<union> closure T"

  1082   unfolding closure_interior by simp

  1083

  1084 lemma closure_eq_empty: "closure S = {} \<longleftrightarrow> S = {}"

  1085   using closure_empty closure_subset[of S]

  1086   by blast

  1087

  1088 lemma closure_subset_eq: "closure S \<subseteq> S \<longleftrightarrow> closed S"

  1089   using closure_eq[of S] closure_subset[of S]

  1090   by simp

  1091

  1092 lemma open_inter_closure_eq_empty:

  1093   "open S \<Longrightarrow> (S \<inter> closure T) = {} \<longleftrightarrow> S \<inter> T = {}"

  1094   using open_subset_interior[of S "- T"]

  1095   using interior_subset[of "- T"]

  1096   unfolding closure_interior

  1097   by auto

  1098

  1099 lemma open_inter_closure_subset:

  1100   "open S \<Longrightarrow> (S \<inter> (closure T)) \<subseteq> closure(S \<inter> T)"

  1101 proof

  1102   fix x

  1103   assume as: "open S" "x \<in> S \<inter> closure T"

  1104   { assume *:"x islimpt T"

  1105     have "x islimpt (S \<inter> T)"

  1106     proof (rule islimptI)

  1107       fix A

  1108       assume "x \<in> A" "open A"

  1109       with as have "x \<in> A \<inter> S" "open (A \<inter> S)"

  1110         by (simp_all add: open_Int)

  1111       with * obtain y where "y \<in> T" "y \<in> A \<inter> S" "y \<noteq> x"

  1112         by (rule islimptE)

  1113       hence "y \<in> S \<inter> T" "y \<in> A \<and> y \<noteq> x"

  1114         by simp_all

  1115       thus "\<exists>y\<in>(S \<inter> T). y \<in> A \<and> y \<noteq> x" ..

  1116     qed

  1117   }

  1118   then show "x \<in> closure (S \<inter> T)" using as

  1119     unfolding closure_def

  1120     by blast

  1121 qed

  1122

  1123 lemma closure_complement: "closure (- S) = - interior S"

  1124   unfolding closure_interior by simp

  1125

  1126 lemma interior_complement: "interior (- S) = - closure S"

  1127   unfolding closure_interior by simp

  1128

  1129 lemma closure_Times: "closure (A \<times> B) = closure A \<times> closure B"

  1130 proof (rule closure_unique)

  1131   show "A \<times> B \<subseteq> closure A \<times> closure B"

  1132     by (intro Sigma_mono closure_subset)

  1133   show "closed (closure A \<times> closure B)"

  1134     by (intro closed_Times closed_closure)

  1135   fix T assume "A \<times> B \<subseteq> T" and "closed T" thus "closure A \<times> closure B \<subseteq> T"

  1136     apply (simp add: closed_def open_prod_def, clarify)

  1137     apply (rule ccontr)

  1138     apply (drule_tac x="(a, b)" in bspec, simp, clarify, rename_tac C D)

  1139     apply (simp add: closure_interior interior_def)

  1140     apply (drule_tac x=C in spec)

  1141     apply (drule_tac x=D in spec)

  1142     apply auto

  1143     done

  1144 qed

  1145

  1146

  1147 lemma islimpt_in_closure: "(x islimpt S) = (x:closure(S-{x}))"

  1148   unfolding closure_def using islimpt_punctured by blast

  1149

  1150

  1151 subsection {* Frontier (aka boundary) *}

  1152

  1153 definition "frontier S = closure S - interior S"

  1154

  1155 lemma frontier_closed: "closed(frontier S)"

  1156   by (simp add: frontier_def closed_Diff)

  1157

  1158 lemma frontier_closures: "frontier S = (closure S) \<inter> (closure(- S))"

  1159   by (auto simp add: frontier_def interior_closure)

  1160

  1161 lemma frontier_straddle:

  1162   fixes a :: "'a::metric_space"

  1163   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))"

  1164   unfolding frontier_def closure_interior

  1165   by (auto simp add: mem_interior subset_eq ball_def)

  1166

  1167 lemma frontier_subset_closed: "closed S \<Longrightarrow> frontier S \<subseteq> S"

  1168   by (metis frontier_def closure_closed Diff_subset)

  1169

  1170 lemma frontier_empty[simp]: "frontier {} = {}"

  1171   by (simp add: frontier_def)

  1172

  1173 lemma frontier_subset_eq: "frontier S \<subseteq> S \<longleftrightarrow> closed S"

  1174 proof-

  1175   { assume "frontier S \<subseteq> S"

  1176     hence "closure S \<subseteq> S" using interior_subset unfolding frontier_def by auto

  1177     hence "closed S" using closure_subset_eq by auto

  1178   }

  1179   thus ?thesis using frontier_subset_closed[of S] ..

  1180 qed

  1181

  1182 lemma frontier_complement: "frontier(- S) = frontier S"

  1183   by (auto simp add: frontier_def closure_complement interior_complement)

  1184

  1185 lemma frontier_disjoint_eq: "frontier S \<inter> S = {} \<longleftrightarrow> open S"

  1186   using frontier_complement frontier_subset_eq[of "- S"]

  1187   unfolding open_closed by auto

  1188

  1189 subsection {* Filters and the eventually true'' quantifier *}

  1190

  1191 definition

  1192   indirection :: "'a::real_normed_vector \<Rightarrow> 'a \<Rightarrow> 'a filter"

  1193     (infixr "indirection" 70) where

  1194   "a indirection v = (at a) within {b. \<exists>c\<ge>0. b - a = scaleR c v}"

  1195

  1196 text {* Identify Trivial limits, where we can't approach arbitrarily closely. *}

  1197

  1198 lemma trivial_limit_within:

  1199   shows "trivial_limit (at a within S) \<longleftrightarrow> \<not> a islimpt S"

  1200 proof

  1201   assume "trivial_limit (at a within S)"

  1202   thus "\<not> a islimpt S"

  1203     unfolding trivial_limit_def

  1204     unfolding eventually_within eventually_at_topological

  1205     unfolding islimpt_def

  1206     apply (clarsimp simp add: set_eq_iff)

  1207     apply (rename_tac T, rule_tac x=T in exI)

  1208     apply (clarsimp, drule_tac x=y in bspec, simp_all)

  1209     done

  1210 next

  1211   assume "\<not> a islimpt S"

  1212   thus "trivial_limit (at a within S)"

  1213     unfolding trivial_limit_def

  1214     unfolding eventually_within eventually_at_topological

  1215     unfolding islimpt_def

  1216     apply clarsimp

  1217     apply (rule_tac x=T in exI)

  1218     apply auto

  1219     done

  1220 qed

  1221

  1222 lemma trivial_limit_at_iff: "trivial_limit (at a) \<longleftrightarrow> \<not> a islimpt UNIV"

  1223   using trivial_limit_within [of a UNIV] by simp

  1224

  1225 lemma trivial_limit_at:

  1226   fixes a :: "'a::perfect_space"

  1227   shows "\<not> trivial_limit (at a)"

  1228   by (rule at_neq_bot)

  1229

  1230 lemma trivial_limit_at_infinity:

  1231   "\<not> trivial_limit (at_infinity :: ('a::{real_normed_vector,perfect_space}) filter)"

  1232   unfolding trivial_limit_def eventually_at_infinity

  1233   apply clarsimp

  1234   apply (subgoal_tac "\<exists>x::'a. x \<noteq> 0", clarify)

  1235    apply (rule_tac x="scaleR (b / norm x) x" in exI, simp)

  1236   apply (cut_tac islimpt_UNIV [of "0::'a", unfolded islimpt_def])

  1237   apply (drule_tac x=UNIV in spec, simp)

  1238   done

  1239

  1240 lemma not_trivial_limit_within: "~trivial_limit (at x within S) = (x:closure(S-{x}))"

  1241   using islimpt_in_closure by (metis trivial_limit_within)

  1242

  1243 text {* Some property holds "sufficiently close" to the limit point. *}

  1244

  1245 lemma eventually_at: (* FIXME: this replaces Metric_Spaces.eventually_at *)

  1246   "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"

  1247 unfolding eventually_at dist_nz by auto

  1248

  1249 lemma eventually_within: (* FIXME: this replaces Topological_Spaces.eventually_within *)

  1250   "eventually P (at a within S) \<longleftrightarrow>

  1251         (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"

  1252   by (rule eventually_within_less)

  1253

  1254 lemma eventually_happens: "eventually P net ==> trivial_limit net \<or> (\<exists>x. P x)"

  1255   unfolding trivial_limit_def

  1256   by (auto elim: eventually_rev_mp)

  1257

  1258 lemma trivial_limit_eventually: "trivial_limit net \<Longrightarrow> eventually P net"

  1259   by simp

  1260

  1261 lemma trivial_limit_eq: "trivial_limit net \<longleftrightarrow> (\<forall>P. eventually P net)"

  1262   by (simp add: filter_eq_iff)

  1263

  1264 text{* Combining theorems for "eventually" *}

  1265

  1266 lemma eventually_rev_mono:

  1267   "eventually P net \<Longrightarrow> (\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually Q net"

  1268 using eventually_mono [of P Q] by fast

  1269

  1270 lemma not_eventually: "(\<forall>x. \<not> P x ) \<Longrightarrow> ~(trivial_limit net) ==> ~(eventually (\<lambda>x. P x) net)"

  1271   by (simp add: eventually_False)

  1272

  1273

  1274 subsection {* Limits *}

  1275

  1276 text{* Notation Lim to avoid collition with lim defined in analysis *}

  1277

  1278 definition Lim :: "'a filter \<Rightarrow> ('a \<Rightarrow> 'b::t2_space) \<Rightarrow> 'b"

  1279   where "Lim A f = (THE l. (f ---> l) A)"

  1280

  1281 text{* Uniqueness of the limit, when nontrivial. *}

  1282

  1283 lemma tendsto_Lim:

  1284   fixes f :: "'a \<Rightarrow> 'b::t2_space"

  1285   shows "~(trivial_limit net) \<Longrightarrow> (f ---> l) net ==> Lim net f = l"

  1286   unfolding Lim_def using tendsto_unique[of net f] by auto

  1287

  1288 lemma Lim:

  1289  "(f ---> l) net \<longleftrightarrow>

  1290         trivial_limit net \<or>

  1291         (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"

  1292   unfolding tendsto_iff trivial_limit_eq by auto

  1293

  1294 text{* Show that they yield usual definitions in the various cases. *}

  1295

  1296 lemma Lim_within_le: "(f ---> l)(at a within S) \<longleftrightarrow>

  1297            (\<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)"

  1298   by (auto simp add: tendsto_iff eventually_within_le)

  1299

  1300 lemma Lim_within: "(f ---> l) (at a within S) \<longleftrightarrow>

  1301         (\<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)"

  1302   by (auto simp add: tendsto_iff eventually_within)

  1303

  1304 lemma Lim_at: "(f ---> l) (at a) \<longleftrightarrow>

  1305         (\<forall>e >0. \<exists>d>0. \<forall>x. 0 < dist x a  \<and> dist x a  < d  \<longrightarrow> dist (f x) l < e)"

  1306   by (auto simp add: tendsto_iff eventually_at)

  1307

  1308 lemma Lim_at_infinity:

  1309   "(f ---> l) at_infinity \<longleftrightarrow> (\<forall>e>0. \<exists>b. \<forall>x. norm x >= b \<longrightarrow> dist (f x) l < e)"

  1310   by (auto simp add: tendsto_iff eventually_at_infinity)

  1311

  1312 lemma Lim_eventually: "eventually (\<lambda>x. f x = l) net \<Longrightarrow> (f ---> l) net"

  1313   by (rule topological_tendstoI, auto elim: eventually_rev_mono)

  1314

  1315 text{* The expected monotonicity property. *}

  1316

  1317 lemma Lim_within_empty: "(f ---> l) (net within {})"

  1318   unfolding tendsto_def Limits.eventually_within by simp

  1319

  1320 lemma Lim_within_subset: "(f ---> l) (net within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f ---> l) (net within T)"

  1321   unfolding tendsto_def Topological_Spaces.eventually_within

  1322   by (auto elim!: eventually_elim1)

  1323

  1324 lemma Lim_Un: assumes "(f ---> l) (net within S)" "(f ---> l) (net within T)"

  1325   shows "(f ---> l) (net within (S \<union> T))"

  1326   using assms unfolding tendsto_def Limits.eventually_within

  1327   apply clarify

  1328   apply (drule spec, drule (1) mp, drule (1) mp)

  1329   apply (drule spec, drule (1) mp, drule (1) mp)

  1330   apply (auto elim: eventually_elim2)

  1331   done

  1332

  1333 lemma Lim_Un_univ:

  1334  "(f ---> l) (net within S) \<Longrightarrow> (f ---> l) (net within T) \<Longrightarrow>  S \<union> T = UNIV

  1335         ==> (f ---> l) net"

  1336   by (metis Lim_Un within_UNIV)

  1337

  1338 text{* Interrelations between restricted and unrestricted limits. *}

  1339

  1340 lemma Lim_at_within: "(f ---> l) net ==> (f ---> l)(net within S)"

  1341   (* FIXME: rename *)

  1342   unfolding tendsto_def Limits.eventually_within

  1343   apply (clarify, drule spec, drule (1) mp, drule (1) mp)

  1344   by (auto elim!: eventually_elim1)

  1345

  1346 lemma eventually_within_interior:

  1347   assumes "x \<in> interior S"

  1348   shows "eventually P (at x within S) \<longleftrightarrow> eventually P (at x)" (is "?lhs = ?rhs")

  1349 proof-

  1350   from assms obtain T where T: "open T" "x \<in> T" "T \<subseteq> S" ..

  1351   { assume "?lhs"

  1352     then obtain A where "open A" "x \<in> A" "\<forall>y\<in>A. y \<noteq> x \<longrightarrow> y \<in> S \<longrightarrow> P y"

  1353       unfolding Limits.eventually_within eventually_at_topological

  1354       by auto

  1355     with T have "open (A \<inter> T)" "x \<in> A \<inter> T" "\<forall>y\<in>(A \<inter> T). y \<noteq> x \<longrightarrow> P y"

  1356       by auto

  1357     then have "?rhs"

  1358       unfolding eventually_at_topological by auto

  1359   } moreover

  1360   { assume "?rhs" hence "?lhs"

  1361       unfolding Limits.eventually_within

  1362       by (auto elim: eventually_elim1)

  1363   } ultimately

  1364   show "?thesis" ..

  1365 qed

  1366

  1367 lemma at_within_interior:

  1368   "x \<in> interior S \<Longrightarrow> at x within S = at x"

  1369   by (simp add: filter_eq_iff eventually_within_interior)

  1370

  1371 lemma at_within_open:

  1372   "\<lbrakk>x \<in> S; open S\<rbrakk> \<Longrightarrow> at x within S = at x"

  1373   by (simp only: at_within_interior interior_open)

  1374

  1375 lemma Lim_within_open:

  1376   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"

  1377   assumes"a \<in> S" "open S"

  1378   shows "(f ---> l)(at a within S) \<longleftrightarrow> (f ---> l)(at a)"

  1379   using assms by (simp only: at_within_open)

  1380

  1381 lemma Lim_within_LIMSEQ:

  1382   fixes a :: "'a::metric_space"

  1383   assumes "\<forall>S. (\<forall>n. S n \<noteq> a \<and> S n \<in> T) \<and> S ----> a \<longrightarrow> (\<lambda>n. X (S n)) ----> L"

  1384   shows "(X ---> L) (at a within T)"

  1385   using assms unfolding tendsto_def [where l=L]

  1386   by (simp add: sequentially_imp_eventually_within)

  1387

  1388 lemma Lim_right_bound:

  1389   fixes f :: "real \<Rightarrow> real"

  1390   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"

  1391   assumes bnd: "\<And>a. a \<in> I \<Longrightarrow> x < a \<Longrightarrow> K \<le> f a"

  1392   shows "(f ---> Inf (f  ({x<..} \<inter> I))) (at x within ({x<..} \<inter> I))"

  1393 proof cases

  1394   assume "{x<..} \<inter> I = {}" then show ?thesis by (simp add: Lim_within_empty)

  1395 next

  1396   assume [simp]: "{x<..} \<inter> I \<noteq> {}"

  1397   show ?thesis

  1398   proof (rule Lim_within_LIMSEQ, safe)

  1399     fix S assume S: "\<forall>n. S n \<noteq> x \<and> S n \<in> {x <..} \<inter> I" "S ----> x"

  1400

  1401     show "(\<lambda>n. f (S n)) ----> Inf (f  ({x<..} \<inter> I))"

  1402     proof (rule LIMSEQ_I, rule ccontr)

  1403       fix r :: real assume "0 < r"

  1404       with cInf_close[of "f  ({x<..} \<inter> I)" r]

  1405       obtain y where y: "x < y" "y \<in> I" "f y < Inf (f  ({x <..} \<inter> I)) + r" by auto

  1406       from x < y have "0 < y - x" by auto

  1407       from S(2)[THEN LIMSEQ_D, OF this]

  1408       obtain N where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>S n - x\<bar> < y - x" by auto

  1409

  1410       assume "\<not> (\<exists>N. \<forall>n\<ge>N. norm (f (S n) - Inf (f  ({x<..} \<inter> I))) < r)"

  1411       moreover have "\<And>n. Inf (f  ({x<..} \<inter> I)) \<le> f (S n)"

  1412         using S bnd by (intro cInf_lower[where z=K]) auto

  1413       ultimately obtain n where n: "N \<le> n" "r + Inf (f  ({x<..} \<inter> I)) \<le> f (S n)"

  1414         by (auto simp: not_less field_simps)

  1415       with N[OF n(1)] mono[OF _ y \<in> I, of "S n"] S(1)[THEN spec, of n] y

  1416       show False by auto

  1417     qed

  1418   qed

  1419 qed

  1420

  1421 text{* Another limit point characterization. *}

  1422

  1423 lemma islimpt_sequential:

  1424   fixes x :: "'a::first_countable_topology"

  1425   shows "x islimpt S \<longleftrightarrow> (\<exists>f. (\<forall>n::nat. f n \<in> S - {x}) \<and> (f ---> x) sequentially)"

  1426     (is "?lhs = ?rhs")

  1427 proof

  1428   assume ?lhs

  1429   from countable_basis_at_decseq[of x] guess A . note A = this

  1430   def f \<equiv> "\<lambda>n. SOME y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y"

  1431   { fix n

  1432     from ?lhs have "\<exists>y. y \<in> S \<and> y \<in> A n \<and> x \<noteq> y"

  1433       unfolding islimpt_def using A(1,2)[of n] by auto

  1434     then have "f n \<in> S \<and> f n \<in> A n \<and> x \<noteq> f n"

  1435       unfolding f_def by (rule someI_ex)

  1436     then have "f n \<in> S" "f n \<in> A n" "x \<noteq> f n" by auto }

  1437   then have "\<forall>n. f n \<in> S - {x}" by auto

  1438   moreover have "(\<lambda>n. f n) ----> x"

  1439   proof (rule topological_tendstoI)

  1440     fix S assume "open S" "x \<in> S"

  1441     from A(3)[OF this] \<And>n. f n \<in> A n

  1442     show "eventually (\<lambda>x. f x \<in> S) sequentially" by (auto elim!: eventually_elim1)

  1443   qed

  1444   ultimately show ?rhs by fast

  1445 next

  1446   assume ?rhs

  1447   then obtain f :: "nat \<Rightarrow> 'a" where f: "\<And>n. f n \<in> S - {x}" and lim: "f ----> x" by auto

  1448   show ?lhs

  1449     unfolding islimpt_def

  1450   proof safe

  1451     fix T assume "open T" "x \<in> T"

  1452     from lim[THEN topological_tendstoD, OF this] f

  1453     show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> x"

  1454       unfolding eventually_sequentially by auto

  1455   qed

  1456 qed

  1457

  1458 lemma Lim_inv: (* TODO: delete *)

  1459   fixes f :: "'a \<Rightarrow> real" and A :: "'a filter"

  1460   assumes "(f ---> l) A" and "l \<noteq> 0"

  1461   shows "((inverse o f) ---> inverse l) A"

  1462   unfolding o_def using assms by (rule tendsto_inverse)

  1463

  1464 lemma Lim_null:

  1465   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1466   shows "(f ---> l) net \<longleftrightarrow> ((\<lambda>x. f(x) - l) ---> 0) net"

  1467   by (simp add: Lim dist_norm)

  1468

  1469 lemma Lim_null_comparison:

  1470   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1471   assumes "eventually (\<lambda>x. norm (f x) \<le> g x) net" "(g ---> 0) net"

  1472   shows "(f ---> 0) net"

  1473 proof (rule metric_tendsto_imp_tendsto)

  1474   show "(g ---> 0) net" by fact

  1475   show "eventually (\<lambda>x. dist (f x) 0 \<le> dist (g x) 0) net"

  1476     using assms(1) by (rule eventually_elim1, simp add: dist_norm)

  1477 qed

  1478

  1479 lemma Lim_transform_bound:

  1480   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1481   fixes g :: "'a \<Rightarrow> 'c::real_normed_vector"

  1482   assumes "eventually (\<lambda>n. norm(f n) <= norm(g n)) net"  "(g ---> 0) net"

  1483   shows "(f ---> 0) net"

  1484   using assms(1) tendsto_norm_zero [OF assms(2)]

  1485   by (rule Lim_null_comparison)

  1486

  1487 text{* Deducing things about the limit from the elements. *}

  1488

  1489 lemma Lim_in_closed_set:

  1490   assumes "closed S" "eventually (\<lambda>x. f(x) \<in> S) net" "\<not>(trivial_limit net)" "(f ---> l) net"

  1491   shows "l \<in> S"

  1492 proof (rule ccontr)

  1493   assume "l \<notin> S"

  1494   with closed S have "open (- S)" "l \<in> - S"

  1495     by (simp_all add: open_Compl)

  1496   with assms(4) have "eventually (\<lambda>x. f x \<in> - S) net"

  1497     by (rule topological_tendstoD)

  1498   with assms(2) have "eventually (\<lambda>x. False) net"

  1499     by (rule eventually_elim2) simp

  1500   with assms(3) show "False"

  1501     by (simp add: eventually_False)

  1502 qed

  1503

  1504 text{* Need to prove closed(cball(x,e)) before deducing this as a corollary. *}

  1505

  1506 lemma Lim_dist_ubound:

  1507   assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. dist a (f x) <= e) net"

  1508   shows "dist a l <= e"

  1509 proof-

  1510   have "dist a l \<in> {..e}"

  1511   proof (rule Lim_in_closed_set)

  1512     show "closed {..e}" by simp

  1513     show "eventually (\<lambda>x. dist a (f x) \<in> {..e}) net" by (simp add: assms)

  1514     show "\<not> trivial_limit net" by fact

  1515     show "((\<lambda>x. dist a (f x)) ---> dist a l) net" by (intro tendsto_intros assms)

  1516   qed

  1517   thus ?thesis by simp

  1518 qed

  1519

  1520 lemma Lim_norm_ubound:

  1521   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1522   assumes "\<not>(trivial_limit net)" "(f ---> l) net" "eventually (\<lambda>x. norm(f x) <= e) net"

  1523   shows "norm(l) <= e"

  1524 proof-

  1525   have "norm l \<in> {..e}"

  1526   proof (rule Lim_in_closed_set)

  1527     show "closed {..e}" by simp

  1528     show "eventually (\<lambda>x. norm (f x) \<in> {..e}) net" by (simp add: assms)

  1529     show "\<not> trivial_limit net" by fact

  1530     show "((\<lambda>x. norm (f x)) ---> norm l) net" by (intro tendsto_intros assms)

  1531   qed

  1532   thus ?thesis by simp

  1533 qed

  1534

  1535 lemma Lim_norm_lbound:

  1536   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"

  1537   assumes "\<not> (trivial_limit net)"  "(f ---> l) net"  "eventually (\<lambda>x. e <= norm(f x)) net"

  1538   shows "e \<le> norm l"

  1539 proof-

  1540   have "norm l \<in> {e..}"

  1541   proof (rule Lim_in_closed_set)

  1542     show "closed {e..}" by simp

  1543     show "eventually (\<lambda>x. norm (f x) \<in> {e..}) net" by (simp add: assms)

  1544     show "\<not> trivial_limit net" by fact

  1545     show "((\<lambda>x. norm (f x)) ---> norm l) net" by (intro tendsto_intros assms)

  1546   qed

  1547   thus ?thesis by simp

  1548 qed

  1549

  1550 text{* Limit under bilinear function *}

  1551

  1552 lemma Lim_bilinear:

  1553   assumes "(f ---> l) net" and "(g ---> m) net" and "bounded_bilinear h"

  1554   shows "((\<lambda>x. h (f x) (g x)) ---> (h l m)) net"

  1555 using bounded_bilinear h (f ---> l) net (g ---> m) net

  1556 by (rule bounded_bilinear.tendsto)

  1557

  1558 text{* These are special for limits out of the same vector space. *}

  1559

  1560 lemma Lim_within_id: "(id ---> a) (at a within s)"

  1561   unfolding id_def by (rule tendsto_ident_at_within)

  1562

  1563 lemma Lim_at_id: "(id ---> a) (at a)"

  1564   unfolding id_def by (rule tendsto_ident_at)

  1565

  1566 lemma Lim_at_zero:

  1567   fixes a :: "'a::real_normed_vector"

  1568   fixes l :: "'b::topological_space"

  1569   shows "(f ---> l) (at a) \<longleftrightarrow> ((\<lambda>x. f(a + x)) ---> l) (at 0)" (is "?lhs = ?rhs")

  1570   using LIM_offset_zero LIM_offset_zero_cancel ..

  1571

  1572 text{* It's also sometimes useful to extract the limit point from the filter. *}

  1573

  1574 abbreviation netlimit :: "'a::t2_space filter \<Rightarrow> 'a" where

  1575   "netlimit F \<equiv> Lim F (\<lambda>x. x)"

  1576

  1577 lemma netlimit_within:

  1578   "\<not> trivial_limit (at a within S) \<Longrightarrow> netlimit (at a within S) = a"

  1579   by (rule tendsto_Lim) (auto intro: tendsto_intros)

  1580

  1581 lemma netlimit_at:

  1582   fixes a :: "'a::{perfect_space,t2_space}"

  1583   shows "netlimit (at a) = a"

  1584   using netlimit_within [of a UNIV] by simp

  1585

  1586 lemma lim_within_interior:

  1587   "x \<in> interior S \<Longrightarrow> (f ---> l) (at x within S) \<longleftrightarrow> (f ---> l) (at x)"

  1588   by (simp add: at_within_interior)

  1589

  1590 lemma netlimit_within_interior:

  1591   fixes x :: "'a::{t2_space,perfect_space}"

  1592   assumes "x \<in> interior S"

  1593   shows "netlimit (at x within S) = x"

  1594 using assms by (simp add: at_within_interior netlimit_at)

  1595

  1596 text{* Transformation of limit. *}

  1597

  1598 lemma Lim_transform:

  1599   fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"

  1600   assumes "((\<lambda>x. f x - g x) ---> 0) net" "(f ---> l) net"

  1601   shows "(g ---> l) net"

  1602   using tendsto_diff [OF assms(2) assms(1)] by simp

  1603

  1604 lemma Lim_transform_eventually:

  1605   "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> (f ---> l) net \<Longrightarrow> (g ---> l) net"

  1606   apply (rule topological_tendstoI)

  1607   apply (drule (2) topological_tendstoD)

  1608   apply (erule (1) eventually_elim2, simp)

  1609   done

  1610

  1611 lemma Lim_transform_within:

  1612   assumes "0 < d" and "\<forall>x'\<in>S. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"

  1613   and "(f ---> l) (at x within S)"

  1614   shows "(g ---> l) (at x within S)"

  1615 proof (rule Lim_transform_eventually)

  1616   show "eventually (\<lambda>x. f x = g x) (at x within S)"

  1617     unfolding eventually_within

  1618     using assms(1,2) by auto

  1619   show "(f ---> l) (at x within S)" by fact

  1620 qed

  1621

  1622 lemma Lim_transform_at:

  1623   assumes "0 < d" and "\<forall>x'. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"

  1624   and "(f ---> l) (at x)"

  1625   shows "(g ---> l) (at x)"

  1626 proof (rule Lim_transform_eventually)

  1627   show "eventually (\<lambda>x. f x = g x) (at x)"

  1628     unfolding eventually_at

  1629     using assms(1,2) by auto

  1630   show "(f ---> l) (at x)" by fact

  1631 qed

  1632

  1633 text{* Common case assuming being away from some crucial point like 0. *}

  1634

  1635 lemma Lim_transform_away_within:

  1636   fixes a b :: "'a::t1_space"

  1637   assumes "a \<noteq> b" and "\<forall>x\<in>S. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"

  1638   and "(f ---> l) (at a within S)"

  1639   shows "(g ---> l) (at a within S)"

  1640 proof (rule Lim_transform_eventually)

  1641   show "(f ---> l) (at a within S)" by fact

  1642   show "eventually (\<lambda>x. f x = g x) (at a within S)"

  1643     unfolding Limits.eventually_within eventually_at_topological

  1644     by (rule exI [where x="- {b}"], simp add: open_Compl assms)

  1645 qed

  1646

  1647 lemma Lim_transform_away_at:

  1648   fixes a b :: "'a::t1_space"

  1649   assumes ab: "a\<noteq>b" and fg: "\<forall>x. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"

  1650   and fl: "(f ---> l) (at a)"

  1651   shows "(g ---> l) (at a)"

  1652   using Lim_transform_away_within[OF ab, of UNIV f g l] fg fl

  1653   by simp

  1654

  1655 text{* Alternatively, within an open set. *}

  1656

  1657 lemma Lim_transform_within_open:

  1658   assumes "open S" and "a \<in> S" and "\<forall>x\<in>S. x \<noteq> a \<longrightarrow> f x = g x"

  1659   and "(f ---> l) (at a)"

  1660   shows "(g ---> l) (at a)"

  1661 proof (rule Lim_transform_eventually)

  1662   show "eventually (\<lambda>x. f x = g x) (at a)"

  1663     unfolding eventually_at_topological

  1664     using assms(1,2,3) by auto

  1665   show "(f ---> l) (at a)" by fact

  1666 qed

  1667

  1668 text{* A congruence rule allowing us to transform limits assuming not at point. *}

  1669

  1670 (* FIXME: Only one congruence rule for tendsto can be used at a time! *)

  1671

  1672 lemma Lim_cong_within(*[cong add]*):

  1673   assumes "a = b" "x = y" "S = T"

  1674   assumes "\<And>x. x \<noteq> b \<Longrightarrow> x \<in> T \<Longrightarrow> f x = g x"

  1675   shows "(f ---> x) (at a within S) \<longleftrightarrow> (g ---> y) (at b within T)"

  1676   unfolding tendsto_def Limits.eventually_within eventually_at_topological

  1677   using assms by simp

  1678

  1679 lemma Lim_cong_at(*[cong add]*):

  1680   assumes "a = b" "x = y"

  1681   assumes "\<And>x. x \<noteq> a \<Longrightarrow> f x = g x"

  1682   shows "((\<lambda>x. f x) ---> x) (at a) \<longleftrightarrow> ((g ---> y) (at a))"

  1683   unfolding tendsto_def eventually_at_topological

  1684   using assms by simp

  1685

  1686 text{* Useful lemmas on closure and set of possible sequential limits.*}

  1687

  1688 lemma closure_sequential:

  1689   fixes l :: "'a::first_countable_topology"

  1690   shows "l \<in> closure S \<longleftrightarrow> (\<exists>x. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially)" (is "?lhs = ?rhs")

  1691 proof

  1692   assume "?lhs" moreover

  1693   { assume "l \<in> S"

  1694     hence "?rhs" using tendsto_const[of l sequentially] by auto

  1695   } moreover

  1696   { assume "l islimpt S"

  1697     hence "?rhs" unfolding islimpt_sequential by auto

  1698   } ultimately

  1699   show "?rhs" unfolding closure_def by auto

  1700 next

  1701   assume "?rhs"

  1702   thus "?lhs" unfolding closure_def unfolding islimpt_sequential by auto

  1703 qed

  1704

  1705 lemma closed_sequential_limits:

  1706   fixes S :: "'a::first_countable_topology set"

  1707   shows "closed S \<longleftrightarrow> (\<forall>x l. (\<forall>n. x n \<in> S) \<and> (x ---> l) sequentially \<longrightarrow> l \<in> S)"

  1708   unfolding closed_limpt

  1709   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]

  1710   by metis

  1711

  1712 lemma closure_approachable:

  1713   fixes S :: "'a::metric_space set"

  1714   shows "x \<in> closure S \<longleftrightarrow> (\<forall>e>0. \<exists>y\<in>S. dist y x < e)"

  1715   apply (auto simp add: closure_def islimpt_approachable)

  1716   by (metis dist_self)

  1717

  1718 lemma closed_approachable:

  1719   fixes S :: "'a::metric_space set"

  1720   shows "closed S ==> (\<forall>e>0. \<exists>y\<in>S. dist y x < e) \<longleftrightarrow> x \<in> S"

  1721   by (metis closure_closed closure_approachable)

  1722

  1723 lemma closure_contains_Inf:

  1724   fixes S :: "real set"

  1725   assumes "S \<noteq> {}" "\<forall>x\<in>S. B \<le> x"

  1726   shows "Inf S \<in> closure S"

  1727   unfolding closure_approachable

  1728 proof safe

  1729   have *: "\<forall>x\<in>S. Inf S \<le> x"

  1730     using cInf_lower_EX[of _ S] assms by metis

  1731

  1732   fix e :: real assume "0 < e"

  1733   then obtain x where x: "x \<in> S" "x < Inf S + e"

  1734     using cInf_close S \<noteq> {} by auto

  1735   moreover then have "x > Inf S - e" using * by auto

  1736   ultimately have "abs (x - Inf S) < e" by (simp add: abs_diff_less_iff)

  1737   then show "\<exists>x\<in>S. dist x (Inf S) < e"

  1738     using x by (auto simp: dist_norm)

  1739 qed

  1740

  1741 lemma closed_contains_Inf:

  1742   fixes S :: "real set"

  1743   assumes "S \<noteq> {}" "\<forall>x\<in>S. B \<le> x"

  1744     and "closed S"

  1745   shows "Inf S \<in> S"

  1746   by (metis closure_contains_Inf closure_closed assms)

  1747

  1748

  1749 lemma not_trivial_limit_within_ball:

  1750   "(\<not> trivial_limit (at x within S)) = (\<forall>e>0. S \<inter> ball x e - {x} \<noteq> {})"

  1751   (is "?lhs = ?rhs")

  1752 proof -

  1753   { assume "?lhs"

  1754     { fix e :: real

  1755       assume "e>0"

  1756       then obtain y where "y:(S-{x}) & dist y x < e"

  1757         using ?lhs not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"]

  1758         by auto

  1759       then have "y : (S Int ball x e - {x})"

  1760         unfolding ball_def by (simp add: dist_commute)

  1761       then have "S Int ball x e - {x} ~= {}" by blast

  1762     } then have "?rhs" by auto

  1763   }

  1764   moreover

  1765   { assume "?rhs"

  1766     { fix e :: real

  1767       assume "e>0"

  1768       then obtain y where "y : (S Int ball x e - {x})" using ?rhs by blast

  1769       then have "y:(S-{x}) & dist y x < e"

  1770         unfolding ball_def by (simp add: dist_commute)

  1771       then have "EX y:(S-{x}). dist y x < e" by auto

  1772     }

  1773     then have "?lhs"

  1774       using not_trivial_limit_within[of x S] closure_approachable[of x "S - {x}"] by auto

  1775   }

  1776   ultimately show ?thesis by auto

  1777 qed

  1778

  1779 subsection {* Infimum Distance *}

  1780

  1781 definition "infdist x A = (if A = {} then 0 else Inf {dist x a|a. a \<in> A})"

  1782

  1783 lemma infdist_notempty: "A \<noteq> {} \<Longrightarrow> infdist x A = Inf {dist x a|a. a \<in> A}"

  1784   by (simp add: infdist_def)

  1785

  1786 lemma infdist_nonneg:

  1787   shows "0 \<le> infdist x A"

  1788   using assms by (auto simp add: infdist_def intro: cInf_greatest)

  1789

  1790 lemma infdist_le:

  1791   assumes "a \<in> A"

  1792   assumes "d = dist x a"

  1793   shows "infdist x A \<le> d"

  1794   using assms by (auto intro!: cInf_lower[where z=0] simp add: infdist_def)

  1795

  1796 lemma infdist_zero[simp]:

  1797   assumes "a \<in> A" shows "infdist a A = 0"

  1798 proof -

  1799   from infdist_le[OF assms, of "dist a a"] have "infdist a A \<le> 0" by auto

  1800   with infdist_nonneg[of a A] assms show "infdist a A = 0" by auto

  1801 qed

  1802

  1803 lemma infdist_triangle:

  1804   shows "infdist x A \<le> infdist y A + dist x y"

  1805 proof cases

  1806   assume "A = {}" thus ?thesis by (simp add: infdist_def)

  1807 next

  1808   assume "A \<noteq> {}" then obtain a where "a \<in> A" by auto

  1809   have "infdist x A \<le> Inf {dist x y + dist y a |a. a \<in> A}"

  1810   proof (rule cInf_greatest)

  1811     from A \<noteq> {} show "{dist x y + dist y a |a. a \<in> A} \<noteq> {}" by simp

  1812     fix d assume "d \<in> {dist x y + dist y a |a. a \<in> A}"

  1813     then obtain a where d: "d = dist x y + dist y a" "a \<in> A" by auto

  1814     show "infdist x A \<le> d"

  1815       unfolding infdist_notempty[OF A \<noteq> {}]

  1816     proof (rule cInf_lower2)

  1817       show "dist x a \<in> {dist x a |a. a \<in> A}" using a \<in> A by auto

  1818       show "dist x a \<le> d" unfolding d by (rule dist_triangle)

  1819       fix d assume "d \<in> {dist x a |a. a \<in> A}"

  1820       then obtain a where "a \<in> A" "d = dist x a" by auto

  1821       thus "infdist x A \<le> d" by (rule infdist_le)

  1822     qed

  1823   qed

  1824   also have "\<dots> = dist x y + infdist y A"

  1825   proof (rule cInf_eq, safe)

  1826     fix a assume "a \<in> A"

  1827     thus "dist x y + infdist y A \<le> dist x y + dist y a" by (auto intro: infdist_le)

  1828   next

  1829     fix i assume inf: "\<And>d. d \<in> {dist x y + dist y a |a. a \<in> A} \<Longrightarrow> i \<le> d"

  1830     hence "i - dist x y \<le> infdist y A" unfolding infdist_notempty[OF A \<noteq> {}] using a \<in> A

  1831       by (intro cInf_greatest) (auto simp: field_simps)

  1832     thus "i \<le> dist x y + infdist y A" by simp

  1833   qed

  1834   finally show ?thesis by simp

  1835 qed

  1836

  1837 lemma in_closure_iff_infdist_zero:

  1838   assumes "A \<noteq> {}"

  1839   shows "x \<in> closure A \<longleftrightarrow> infdist x A = 0"

  1840 proof

  1841   assume "x \<in> closure A"

  1842   show "infdist x A = 0"

  1843   proof (rule ccontr)

  1844     assume "infdist x A \<noteq> 0"

  1845     with infdist_nonneg[of x A] have "infdist x A > 0" by auto

  1846     hence "ball x (infdist x A) \<inter> closure A = {}" apply auto

  1847       by (metis 0 < infdist x A x \<in> closure A closure_approachable dist_commute

  1848         eucl_less_not_refl euclidean_trans(2) infdist_le)

  1849     hence "x \<notin> closure A" by (metis 0 < infdist x A centre_in_ball disjoint_iff_not_equal)

  1850     thus False using x \<in> closure A by simp

  1851   qed

  1852 next

  1853   assume x: "infdist x A = 0"

  1854   then obtain a where "a \<in> A" by atomize_elim (metis all_not_in_conv assms)

  1855   show "x \<in> closure A" unfolding closure_approachable

  1856   proof (safe, rule ccontr)

  1857     fix e::real assume "0 < e"

  1858     assume "\<not> (\<exists>y\<in>A. dist y x < e)"

  1859     hence "infdist x A \<ge> e" using a \<in> A

  1860       unfolding infdist_def

  1861       by (force simp: dist_commute intro: cInf_greatest)

  1862     with x 0 < e show False by auto

  1863   qed

  1864 qed

  1865

  1866 lemma in_closed_iff_infdist_zero:

  1867   assumes "closed A" "A \<noteq> {}"

  1868   shows "x \<in> A \<longleftrightarrow> infdist x A = 0"

  1869 proof -

  1870   have "x \<in> closure A \<longleftrightarrow> infdist x A = 0"

  1871     by (rule in_closure_iff_infdist_zero) fact

  1872   with assms show ?thesis by simp

  1873 qed

  1874

  1875 lemma tendsto_infdist [tendsto_intros]:

  1876   assumes f: "(f ---> l) F"

  1877   shows "((\<lambda>x. infdist (f x) A) ---> infdist l A) F"

  1878 proof (rule tendstoI)

  1879   fix e ::real assume "0 < e"

  1880   from tendstoD[OF f this]

  1881   show "eventually (\<lambda>x. dist (infdist (f x) A) (infdist l A) < e) F"

  1882   proof (eventually_elim)

  1883     fix x

  1884     from infdist_triangle[of l A "f x"] infdist_triangle[of "f x" A l]

  1885     have "dist (infdist (f x) A) (infdist l A) \<le> dist (f x) l"

  1886       by (simp add: dist_commute dist_real_def)

  1887     also assume "dist (f x) l < e"

  1888     finally show "dist (infdist (f x) A) (infdist l A) < e" .

  1889   qed

  1890 qed

  1891

  1892 text{* Some other lemmas about sequences. *}

  1893

  1894 lemma sequentially_offset:

  1895   assumes "eventually (\<lambda>i. P i) sequentially"

  1896   shows "eventually (\<lambda>i. P (i + k)) sequentially"

  1897   using assms unfolding eventually_sequentially by (metis trans_le_add1)

  1898

  1899 lemma seq_offset:

  1900   assumes "(f ---> l) sequentially"

  1901   shows "((\<lambda>i. f (i + k)) ---> l) sequentially"

  1902   using assms by (rule LIMSEQ_ignore_initial_segment) (* FIXME: redundant *)

  1903

  1904 lemma seq_offset_neg:

  1905   "(f ---> l) sequentially ==> ((\<lambda>i. f(i - k)) ---> l) sequentially"

  1906   apply (rule topological_tendstoI)

  1907   apply (drule (2) topological_tendstoD)

  1908   apply (simp only: eventually_sequentially)

  1909   apply (subgoal_tac "\<And>N k (n::nat). N + k <= n ==> N <= n - k")

  1910   apply metis

  1911   by arith

  1912

  1913 lemma seq_offset_rev:

  1914   "((\<lambda>i. f(i + k)) ---> l) sequentially ==> (f ---> l) sequentially"

  1915   by (rule LIMSEQ_offset) (* FIXME: redundant *)

  1916

  1917 lemma seq_harmonic: "((\<lambda>n. inverse (real n)) ---> 0) sequentially"

  1918   using LIMSEQ_inverse_real_of_nat by (rule LIMSEQ_imp_Suc)

  1919

  1920 subsection {* More properties of closed balls *}

  1921

  1922 lemma closed_cball: "closed (cball x e)"

  1923 unfolding cball_def closed_def

  1924 unfolding Collect_neg_eq [symmetric] not_le

  1925 apply (clarsimp simp add: open_dist, rename_tac y)

  1926 apply (rule_tac x="dist x y - e" in exI, clarsimp)

  1927 apply (rename_tac x')

  1928 apply (cut_tac x=x and y=x' and z=y in dist_triangle)

  1929 apply simp

  1930 done

  1931

  1932 lemma open_contains_cball: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0.  cball x e \<subseteq> S)"

  1933 proof-

  1934   { fix x and e::real assume "x\<in>S" "e>0" "ball x e \<subseteq> S"

  1935     hence "\<exists>d>0. cball x d \<subseteq> S" unfolding subset_eq by (rule_tac x="e/2" in exI, auto)

  1936   } moreover

  1937   { fix x and e::real assume "x\<in>S" "e>0" "cball x e \<subseteq> S"

  1938     hence "\<exists>d>0. ball x d \<subseteq> S" unfolding subset_eq apply(rule_tac x="e/2" in exI) by auto

  1939   } ultimately

  1940   show ?thesis unfolding open_contains_ball by auto

  1941 qed

  1942

  1943 lemma open_contains_cball_eq: "open S ==> (\<forall>x. x \<in> S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S))"

  1944   by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)

  1945

  1946 lemma mem_interior_cball: "x \<in> interior S \<longleftrightarrow> (\<exists>e>0. cball x e \<subseteq> S)"

  1947   apply (simp add: interior_def, safe)

  1948   apply (force simp add: open_contains_cball)

  1949   apply (rule_tac x="ball x e" in exI)

  1950   apply (simp add: subset_trans [OF ball_subset_cball])

  1951   done

  1952

  1953 lemma islimpt_ball:

  1954   fixes x y :: "'a::{real_normed_vector,perfect_space}"

  1955   shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e" (is "?lhs = ?rhs")

  1956 proof

  1957   assume "?lhs"

  1958   { assume "e \<le> 0"

  1959     hence *:"ball x e = {}" using ball_eq_empty[of x e] by auto

  1960     have False using ?lhs unfolding * using islimpt_EMPTY[of y] by auto

  1961   }

  1962   hence "e > 0" by (metis not_less)

  1963   moreover

  1964   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

  1965   ultimately show "?rhs" by auto

  1966 next

  1967   assume "?rhs" hence "e>0"  by auto

  1968   { fix d::real assume "d>0"

  1969     have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1970     proof(cases "d \<le> dist x y")

  1971       case True thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1972       proof(cases "x=y")

  1973         case True hence False using d \<le> dist x y d>0 by auto

  1974         thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by auto

  1975       next

  1976         case False

  1977

  1978         have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x))

  1979               = norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"

  1980           unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[THEN sym] by auto

  1981         also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"

  1982           using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", THEN sym, of "y - x"]

  1983           unfolding scaleR_minus_left scaleR_one

  1984           by (auto simp add: norm_minus_commute)

  1985         also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"

  1986           unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]

  1987           unfolding distrib_right using x\<noteq>y[unfolded dist_nz, unfolded dist_norm] by auto

  1988         also have "\<dots> \<le> e - d/2" using d \<le> dist x y and d>0 and ?rhs by(auto simp add: dist_norm)

  1989         finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using d>0 by auto

  1990

  1991         moreover

  1992

  1993         have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"

  1994           using x\<noteq>y[unfolded dist_nz] d>0 unfolding scaleR_eq_0_iff by (auto simp add: dist_commute)

  1995         moreover

  1996         have "dist (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) y < d" unfolding dist_norm apply simp unfolding norm_minus_cancel

  1997           using d>0 x\<noteq>y[unfolded dist_nz] dist_commute[of x y]

  1998           unfolding dist_norm by auto

  1999         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

  2000       qed

  2001     next

  2002       case False hence "d > dist x y" by auto

  2003       show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  2004       proof(cases "x=y")

  2005         case True

  2006         obtain z where **: "z \<noteq> y" "dist z y < min e d"

  2007           using perfect_choose_dist[of "min e d" y]

  2008           using d > 0 e>0 by auto

  2009         show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  2010           unfolding x = y

  2011           using z \<noteq> y **

  2012           by (rule_tac x=z in bexI, auto simp add: dist_commute)

  2013       next

  2014         case False thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  2015           using d>0 d > dist x y ?rhs by(rule_tac x=x in bexI, auto)

  2016       qed

  2017     qed  }

  2018   thus "?lhs" unfolding mem_cball islimpt_approachable mem_ball by auto

  2019 qed

  2020

  2021 lemma closure_ball_lemma:

  2022   fixes x y :: "'a::real_normed_vector"

  2023   assumes "x \<noteq> y" shows "y islimpt ball x (dist x y)"

  2024 proof (rule islimptI)

  2025   fix T assume "y \<in> T" "open T"

  2026   then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"

  2027     unfolding open_dist by fast

  2028   (* choose point between x and y, within distance r of y. *)

  2029   def k \<equiv> "min 1 (r / (2 * dist x y))"

  2030   def z \<equiv> "y + scaleR k (x - y)"

  2031   have z_def2: "z = x + scaleR (1 - k) (y - x)"

  2032     unfolding z_def by (simp add: algebra_simps)

  2033   have "dist z y < r"

  2034     unfolding z_def k_def using 0 < r

  2035     by (simp add: dist_norm min_def)

  2036   hence "z \<in> T" using \<forall>z. dist z y < r \<longrightarrow> z \<in> T by simp

  2037   have "dist x z < dist x y"

  2038     unfolding z_def2 dist_norm

  2039     apply (simp add: norm_minus_commute)

  2040     apply (simp only: dist_norm [symmetric])

  2041     apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)

  2042     apply (rule mult_strict_right_mono)

  2043     apply (simp add: k_def divide_pos_pos zero_less_dist_iff 0 < r x \<noteq> y)

  2044     apply (simp add: zero_less_dist_iff x \<noteq> y)

  2045     done

  2046   hence "z \<in> ball x (dist x y)" by simp

  2047   have "z \<noteq> y"

  2048     unfolding z_def k_def using x \<noteq> y 0 < r

  2049     by (simp add: min_def)

  2050   show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"

  2051     using z \<in> ball x (dist x y) z \<in> T z \<noteq> y

  2052     by fast

  2053 qed

  2054

  2055 lemma closure_ball:

  2056   fixes x :: "'a::real_normed_vector"

  2057   shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"

  2058 apply (rule equalityI)

  2059 apply (rule closure_minimal)

  2060 apply (rule ball_subset_cball)

  2061 apply (rule closed_cball)

  2062 apply (rule subsetI, rename_tac y)

  2063 apply (simp add: le_less [where 'a=real])

  2064 apply (erule disjE)

  2065 apply (rule subsetD [OF closure_subset], simp)

  2066 apply (simp add: closure_def)

  2067 apply clarify

  2068 apply (rule closure_ball_lemma)

  2069 apply (simp add: zero_less_dist_iff)

  2070 done

  2071

  2072 (* In a trivial vector space, this fails for e = 0. *)

  2073 lemma interior_cball:

  2074   fixes x :: "'a::{real_normed_vector, perfect_space}"

  2075   shows "interior (cball x e) = ball x e"

  2076 proof(cases "e\<ge>0")

  2077   case False note cs = this

  2078   from cs have "ball x e = {}" using ball_empty[of e x] by auto moreover

  2079   { fix y assume "y \<in> cball x e"

  2080     hence False unfolding mem_cball using dist_nz[of x y] cs by auto  }

  2081   hence "cball x e = {}" by auto

  2082   hence "interior (cball x e) = {}" using interior_empty by auto

  2083   ultimately show ?thesis by blast

  2084 next

  2085   case True note cs = this

  2086   have "ball x e \<subseteq> cball x e" using ball_subset_cball by auto moreover

  2087   { fix S y assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"

  2088     then obtain d where "d>0" and d:"\<forall>x'. dist x' y < d \<longrightarrow> x' \<in> S" unfolding open_dist by blast

  2089

  2090     then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"

  2091       using perfect_choose_dist [of d] by auto

  2092     have "xa\<in>S" using d[THEN spec[where x=xa]] using xa by(auto simp add: dist_commute)

  2093     hence xa_cball:"xa \<in> cball x e" using as(1) by auto

  2094

  2095     hence "y \<in> ball x e" proof(cases "x = y")

  2096       case True

  2097       hence "e>0" using xa_y[unfolded dist_nz] xa_cball[unfolded mem_cball] by (auto simp add: dist_commute)

  2098       thus "y \<in> ball x e" using x = y  by simp

  2099     next

  2100       case False

  2101       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d" unfolding dist_norm

  2102         using d>0 norm_ge_zero[of "y - x"] x \<noteq> y by auto

  2103       hence *:"y + (d / 2 / dist y x) *\<^sub>R (y - x) \<in> cball x e" using d as(1)[unfolded subset_eq] by blast

  2104       have "y - x \<noteq> 0" using x \<noteq> y by auto

  2105       hence **:"d / (2 * norm (y - x)) > 0" unfolding zero_less_norm_iff[THEN sym]

  2106         using d>0 divide_pos_pos[of d "2*norm (y - x)"] by auto

  2107

  2108       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)"

  2109         by (auto simp add: dist_norm algebra_simps)

  2110       also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"

  2111         by (auto simp add: algebra_simps)

  2112       also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"

  2113         using ** by auto

  2114       also have "\<dots> = (dist y x) + d/2"using ** by (auto simp add: distrib_right dist_norm)

  2115       finally have "e \<ge> dist x y +d/2" using *[unfolded mem_cball] by (auto simp add: dist_commute)

  2116       thus "y \<in> ball x e" unfolding mem_ball using d>0 by auto

  2117     qed  }

  2118   hence "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e" by auto

  2119   ultimately show ?thesis using interior_unique[of "ball x e" "cball x e"] using open_ball[of x e] by auto

  2120 qed

  2121

  2122 lemma frontier_ball:

  2123   fixes a :: "'a::real_normed_vector"

  2124   shows "0 < e ==> frontier(ball a e) = {x. dist a x = e}"

  2125   apply (simp add: frontier_def closure_ball interior_open order_less_imp_le)

  2126   apply (simp add: set_eq_iff)

  2127   by arith

  2128

  2129 lemma frontier_cball:

  2130   fixes a :: "'a::{real_normed_vector, perfect_space}"

  2131   shows "frontier(cball a e) = {x. dist a x = e}"

  2132   apply (simp add: frontier_def interior_cball closed_cball order_less_imp_le)

  2133   apply (simp add: set_eq_iff)

  2134   by arith

  2135

  2136 lemma cball_eq_empty: "(cball x e = {}) \<longleftrightarrow> e < 0"

  2137   apply (simp add: set_eq_iff not_le)

  2138   by (metis zero_le_dist dist_self order_less_le_trans)

  2139 lemma cball_empty: "e < 0 ==> cball x e = {}" by (simp add: cball_eq_empty)

  2140

  2141 lemma cball_eq_sing:

  2142   fixes x :: "'a::{metric_space,perfect_space}"

  2143   shows "(cball x e = {x}) \<longleftrightarrow> e = 0"

  2144 proof (rule linorder_cases)

  2145   assume e: "0 < e"

  2146   obtain a where "a \<noteq> x" "dist a x < e"

  2147     using perfect_choose_dist [OF e] by auto

  2148   hence "a \<noteq> x" "dist x a \<le> e" by (auto simp add: dist_commute)

  2149   with e show ?thesis by (auto simp add: set_eq_iff)

  2150 qed auto

  2151

  2152 lemma cball_sing:

  2153   fixes x :: "'a::metric_space"

  2154   shows "e = 0 ==> cball x e = {x}"

  2155   by (auto simp add: set_eq_iff)

  2156

  2157

  2158 subsection {* Boundedness *}

  2159

  2160   (* FIXME: This has to be unified with BSEQ!! *)

  2161 definition (in metric_space)

  2162   bounded :: "'a set \<Rightarrow> bool" where

  2163   "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"

  2164

  2165 lemma bounded_subset_cball: "bounded S \<longleftrightarrow> (\<exists>e x. S \<subseteq> cball x e)"

  2166   unfolding bounded_def subset_eq by auto

  2167

  2168 lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"

  2169 unfolding bounded_def

  2170 apply safe

  2171 apply (rule_tac x="dist a x + e" in exI, clarify)

  2172 apply (drule (1) bspec)

  2173 apply (erule order_trans [OF dist_triangle add_left_mono])

  2174 apply auto

  2175 done

  2176

  2177 lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"

  2178 unfolding bounded_any_center [where a=0]

  2179 by (simp add: dist_norm)

  2180

  2181 lemma bounded_realI: assumes "\<forall>x\<in>s. abs (x::real) \<le> B" shows "bounded s"

  2182   unfolding bounded_def dist_real_def apply(rule_tac x=0 in exI)

  2183   using assms by auto

  2184

  2185 lemma bounded_empty [simp]: "bounded {}"

  2186   by (simp add: bounded_def)

  2187

  2188 lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T ==> bounded S"

  2189   by (metis bounded_def subset_eq)

  2190

  2191 lemma bounded_interior[intro]: "bounded S ==> bounded(interior S)"

  2192   by (metis bounded_subset interior_subset)

  2193

  2194 lemma bounded_closure[intro]: assumes "bounded S" shows "bounded(closure S)"

  2195 proof-

  2196   from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a" unfolding bounded_def by auto

  2197   { fix y assume "y \<in> closure S"

  2198     then obtain f where f: "\<forall>n. f n \<in> S"  "(f ---> y) sequentially"

  2199       unfolding closure_sequential by auto

  2200     have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp

  2201     hence "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"

  2202       by (rule eventually_mono, simp add: f(1))

  2203     have "dist x y \<le> a"

  2204       apply (rule Lim_dist_ubound [of sequentially f])

  2205       apply (rule trivial_limit_sequentially)

  2206       apply (rule f(2))

  2207       apply fact

  2208       done

  2209   }

  2210   thus ?thesis unfolding bounded_def by auto

  2211 qed

  2212

  2213 lemma bounded_cball[simp,intro]: "bounded (cball x e)"

  2214   apply (simp add: bounded_def)

  2215   apply (rule_tac x=x in exI)

  2216   apply (rule_tac x=e in exI)

  2217   apply auto

  2218   done

  2219

  2220 lemma bounded_ball[simp,intro]: "bounded(ball x e)"

  2221   by (metis ball_subset_cball bounded_cball bounded_subset)

  2222

  2223 lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"

  2224   apply (auto simp add: bounded_def)

  2225   apply (rename_tac x y r s)

  2226   apply (rule_tac x=x in exI)

  2227   apply (rule_tac x="max r (dist x y + s)" in exI)

  2228   apply (rule ballI, rename_tac z, safe)

  2229   apply (drule (1) bspec, simp)

  2230   apply (drule (1) bspec)

  2231   apply (rule min_max.le_supI2)

  2232   apply (erule order_trans [OF dist_triangle add_left_mono])

  2233   done

  2234

  2235 lemma bounded_Union[intro]: "finite F \<Longrightarrow> (\<forall>S\<in>F. bounded S) \<Longrightarrow> bounded(\<Union>F)"

  2236   by (induct rule: finite_induct[of F], auto)

  2237

  2238 lemma bounded_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. bounded (B x) \<Longrightarrow> bounded (\<Union>x\<in>A. B x)"

  2239   by (induct set: finite, auto)

  2240

  2241 lemma bounded_insert [simp]: "bounded (insert x S) \<longleftrightarrow> bounded S"

  2242 proof -

  2243   have "\<forall>y\<in>{x}. dist x y \<le> 0" by simp

  2244   hence "bounded {x}" unfolding bounded_def by fast

  2245   thus ?thesis by (metis insert_is_Un bounded_Un)

  2246 qed

  2247

  2248 lemma finite_imp_bounded [intro]: "finite S \<Longrightarrow> bounded S"

  2249   by (induct set: finite, simp_all)

  2250

  2251 lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x <= b)"

  2252   apply (simp add: bounded_iff)

  2253   apply (subgoal_tac "\<And>x (y::real). 0 < 1 + abs y \<and> (x <= y \<longrightarrow> x <= 1 + abs y)")

  2254   by metis arith

  2255

  2256 lemma Bseq_eq_bounded: "Bseq f \<longleftrightarrow> bounded (range f::_::real_normed_vector set)"

  2257   unfolding Bseq_def bounded_pos by auto

  2258

  2259 lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"

  2260   by (metis Int_lower1 Int_lower2 bounded_subset)

  2261

  2262 lemma bounded_diff[intro]: "bounded S ==> bounded (S - T)"

  2263 apply (metis Diff_subset bounded_subset)

  2264 done

  2265

  2266 lemma not_bounded_UNIV[simp, intro]:

  2267   "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"

  2268 proof(auto simp add: bounded_pos not_le)

  2269   obtain x :: 'a where "x \<noteq> 0"

  2270     using perfect_choose_dist [OF zero_less_one] by fast

  2271   fix b::real  assume b: "b >0"

  2272   have b1: "b +1 \<ge> 0" using b by simp

  2273   with x \<noteq> 0 have "b < norm (scaleR (b + 1) (sgn x))"

  2274     by (simp add: norm_sgn)

  2275   then show "\<exists>x::'a. b < norm x" ..

  2276 qed

  2277

  2278 lemma bounded_linear_image:

  2279   assumes "bounded S" "bounded_linear f"

  2280   shows "bounded(f  S)"

  2281 proof-

  2282   from assms(1) obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto

  2283   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)

  2284   { fix x assume "x\<in>S"

  2285     hence "norm x \<le> b" using b by auto

  2286     hence "norm (f x) \<le> B * b" using B(2) apply(erule_tac x=x in allE)

  2287       by (metis B(1) B(2) order_trans mult_le_cancel_left_pos)

  2288   }

  2289   thus ?thesis unfolding bounded_pos apply(rule_tac x="b*B" in exI)

  2290     using b B mult_pos_pos [of b B] by (auto simp add: mult_commute)

  2291 qed

  2292

  2293 lemma bounded_scaling:

  2294   fixes S :: "'a::real_normed_vector set"

  2295   shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x)  S)"

  2296   apply (rule bounded_linear_image, assumption)

  2297   apply (rule bounded_linear_scaleR_right)

  2298   done

  2299

  2300 lemma bounded_translation:

  2301   fixes S :: "'a::real_normed_vector set"

  2302   assumes "bounded S" shows "bounded ((\<lambda>x. a + x)  S)"

  2303 proof-

  2304   from assms obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto

  2305   { fix x assume "x\<in>S"

  2306     hence "norm (a + x) \<le> b + norm a" using norm_triangle_ineq[of a x] b by auto

  2307   }

  2308   thus ?thesis unfolding bounded_pos using norm_ge_zero[of a] b(1) using add_strict_increasing[of b 0 "norm a"]

  2309     by (auto intro!: exI[of _ "b + norm a"])

  2310 qed

  2311

  2312

  2313 text{* Some theorems on sups and infs using the notion "bounded". *}

  2314

  2315 lemma bounded_real:

  2316   fixes S :: "real set"

  2317   shows "bounded S \<longleftrightarrow>  (\<exists>a. \<forall>x\<in>S. abs x <= a)"

  2318   by (simp add: bounded_iff)

  2319

  2320 lemma bounded_has_Sup:

  2321   fixes S :: "real set"

  2322   assumes "bounded S" "S \<noteq> {}"

  2323   shows "\<forall>x\<in>S. x <= Sup S" and "\<forall>b. (\<forall>x\<in>S. x <= b) \<longrightarrow> Sup S <= b"

  2324 proof

  2325   fix x assume "x\<in>S"

  2326   thus "x \<le> Sup S"

  2327     by (metis cSup_upper abs_le_D1 assms(1) bounded_real)

  2328 next

  2329   show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b" using assms

  2330     by (metis cSup_least)

  2331 qed

  2332

  2333 lemma Sup_insert:

  2334   fixes S :: "real set"

  2335   shows "bounded S ==> Sup(insert x S) = (if S = {} then x else max x (Sup S))"

  2336   apply (subst cSup_insert_If)

  2337   apply (rule bounded_has_Sup(1)[of S, rule_format])

  2338   apply (auto simp: sup_max)

  2339   done

  2340

  2341 lemma Sup_insert_finite:

  2342   fixes S :: "real set"

  2343   shows "finite S \<Longrightarrow> Sup(insert x S) = (if S = {} then x else max x (Sup S))"

  2344   apply (rule Sup_insert)

  2345   apply (rule finite_imp_bounded)

  2346   by simp

  2347

  2348 lemma bounded_has_Inf:

  2349   fixes S :: "real set"

  2350   assumes "bounded S"  "S \<noteq> {}"

  2351   shows "\<forall>x\<in>S. x >= Inf S" and "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S >= b"

  2352 proof

  2353   fix x assume "x\<in>S"

  2354   from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto

  2355   thus "x \<ge> Inf S" using x\<in>S

  2356     by (metis cInf_lower_EX abs_le_D2 minus_le_iff)

  2357 next

  2358   show "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S \<ge> b" using assms

  2359     by (metis cInf_greatest)

  2360 qed

  2361

  2362 lemma Inf_insert:

  2363   fixes S :: "real set"

  2364   shows "bounded S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"

  2365   apply (subst cInf_insert_if)

  2366   apply (rule bounded_has_Inf(1)[of S, rule_format])

  2367   apply (auto simp: inf_min)

  2368   done

  2369

  2370 lemma Inf_insert_finite:

  2371   fixes S :: "real set"

  2372   shows "finite S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"

  2373   by (rule Inf_insert, rule finite_imp_bounded, simp)

  2374

  2375 subsection {* Compactness *}

  2376

  2377 subsubsection{* Open-cover compactness *}

  2378

  2379 definition compact :: "'a::topological_space set \<Rightarrow> bool" where

  2380   compact_eq_heine_borel: -- "This name is used for backwards compatibility"

  2381     "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))"

  2382

  2383 lemma compactI:

  2384   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'"

  2385   shows "compact s"

  2386   unfolding compact_eq_heine_borel using assms by metis

  2387

  2388 lemma compactE:

  2389   assumes "compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C"

  2390   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"

  2391   using assms unfolding compact_eq_heine_borel by metis

  2392

  2393 lemma compactE_image:

  2394   assumes "compact s" and "\<forall>t\<in>C. open (f t)" and "s \<subseteq> (\<Union>c\<in>C. f c)"

  2395   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> (\<Union>c\<in>C'. f c)"

  2396   using assms unfolding ball_simps[symmetric] SUP_def

  2397   by (metis (lifting) finite_subset_image compact_eq_heine_borel[of s])

  2398

  2399 subsubsection {* Bolzano-Weierstrass property *}

  2400

  2401 lemma heine_borel_imp_bolzano_weierstrass:

  2402   assumes "compact s" "infinite t"  "t \<subseteq> s"

  2403   shows "\<exists>x \<in> s. x islimpt t"

  2404 proof(rule ccontr)

  2405   assume "\<not> (\<exists>x \<in> s. x islimpt t)"

  2406   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

  2407     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

  2408   obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"

  2409     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

  2410   from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto

  2411   { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"

  2412     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

  2413     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  }

  2414   hence "inj_on f t" unfolding inj_on_def by simp

  2415   hence "infinite (f  t)" using assms(2) using finite_imageD by auto

  2416   moreover

  2417   { fix x assume "x\<in>t" "f x \<notin> g"

  2418     from g(3) assms(3) x\<in>t obtain h where "h\<in>g" and "x\<in>h" by auto

  2419     then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto

  2420     hence "y = x" using f[THEN bspec[where x=y]] and x\<in>t and x\<in>h[unfolded h = f y] by auto

  2421     hence False using f x \<notin> g h\<in>g unfolding h = f y by auto  }

  2422   hence "f  t \<subseteq> g" by auto

  2423   ultimately show False using g(2) using finite_subset by auto

  2424 qed

  2425

  2426 lemma acc_point_range_imp_convergent_subsequence:

  2427   fixes l :: "'a :: first_countable_topology"

  2428   assumes l: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> range f)"

  2429   shows "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2430 proof -

  2431   from countable_basis_at_decseq[of l] guess A . note A = this

  2432

  2433   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> f j \<in> A (Suc n)"

  2434   { fix n i

  2435     have "infinite (A (Suc n) \<inter> range f - f{.. i})"

  2436       using l A by auto

  2437     then have "\<exists>x. x \<in> A (Suc n) \<inter> range f - f{.. i}"

  2438       unfolding ex_in_conv by (intro notI) simp

  2439     then have "\<exists>j. f j \<in> A (Suc n) \<and> j \<notin> {.. i}"

  2440       by auto

  2441     then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)"

  2442       by (auto simp: not_le)

  2443     then have "i < s n i" "f (s n i) \<in> A (Suc n)"

  2444       unfolding s_def by (auto intro: someI2_ex) }

  2445   note s = this

  2446   def r \<equiv> "nat_rec (s 0 0) s"

  2447   have "subseq r"

  2448     by (auto simp: r_def s subseq_Suc_iff)

  2449   moreover

  2450   have "(\<lambda>n. f (r n)) ----> l"

  2451   proof (rule topological_tendstoI)

  2452     fix S assume "open S" "l \<in> S"

  2453     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto

  2454     moreover

  2455     { fix i assume "Suc 0 \<le> i" then have "f (r i) \<in> A i"

  2456         by (cases i) (simp_all add: r_def s) }

  2457     then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)

  2458     ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"

  2459       by eventually_elim auto

  2460   qed

  2461   ultimately show "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2462     by (auto simp: convergent_def comp_def)

  2463 qed

  2464

  2465 lemma sequence_infinite_lemma:

  2466   fixes f :: "nat \<Rightarrow> 'a::t1_space"

  2467   assumes "\<forall>n. f n \<noteq> l" and "(f ---> l) sequentially"

  2468   shows "infinite (range f)"

  2469 proof

  2470   assume "finite (range f)"

  2471   hence "closed (range f)" by (rule finite_imp_closed)

  2472   hence "open (- range f)" by (rule open_Compl)

  2473   from assms(1) have "l \<in> - range f" by auto

  2474   from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"

  2475     using open (- range f) l \<in> - range f by (rule topological_tendstoD)

  2476   thus False unfolding eventually_sequentially by auto

  2477 qed

  2478

  2479 lemma closure_insert:

  2480   fixes x :: "'a::t1_space"

  2481   shows "closure (insert x s) = insert x (closure s)"

  2482 apply (rule closure_unique)

  2483 apply (rule insert_mono [OF closure_subset])

  2484 apply (rule closed_insert [OF closed_closure])

  2485 apply (simp add: closure_minimal)

  2486 done

  2487

  2488 lemma islimpt_insert:

  2489   fixes x :: "'a::t1_space"

  2490   shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"

  2491 proof

  2492   assume *: "x islimpt (insert a s)"

  2493   show "x islimpt s"

  2494   proof (rule islimptI)

  2495     fix t assume t: "x \<in> t" "open t"

  2496     show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"

  2497     proof (cases "x = a")

  2498       case True

  2499       obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"

  2500         using * t by (rule islimptE)

  2501       with x = a show ?thesis by auto

  2502     next

  2503       case False

  2504       with t have t': "x \<in> t - {a}" "open (t - {a})"

  2505         by (simp_all add: open_Diff)

  2506       obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"

  2507         using * t' by (rule islimptE)

  2508       thus ?thesis by auto

  2509     qed

  2510   qed

  2511 next

  2512   assume "x islimpt s" thus "x islimpt (insert a s)"

  2513     by (rule islimpt_subset) auto

  2514 qed

  2515

  2516 lemma islimpt_finite:

  2517   fixes x :: "'a::t1_space"

  2518   shows "finite s \<Longrightarrow> \<not> x islimpt s"

  2519 by (induct set: finite, simp_all add: islimpt_insert)

  2520

  2521 lemma islimpt_union_finite:

  2522   fixes x :: "'a::t1_space"

  2523   shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"

  2524 by (simp add: islimpt_Un islimpt_finite)

  2525

  2526 lemma islimpt_eq_acc_point:

  2527   fixes l :: "'a :: t1_space"

  2528   shows "l islimpt S \<longleftrightarrow> (\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S))"

  2529 proof (safe intro!: islimptI)

  2530   fix U assume "l islimpt S" "l \<in> U" "open U" "finite (U \<inter> S)"

  2531   then have "l islimpt S" "l \<in> (U - (U \<inter> S - {l}))" "open (U - (U \<inter> S - {l}))"

  2532     by (auto intro: finite_imp_closed)

  2533   then show False

  2534     by (rule islimptE) auto

  2535 next

  2536   fix T assume *: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S)" "l \<in> T" "open T"

  2537   then have "infinite (T \<inter> S - {l})" by auto

  2538   then have "\<exists>x. x \<in> (T \<inter> S - {l})"

  2539     unfolding ex_in_conv by (intro notI) simp

  2540   then show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> l"

  2541     by auto

  2542 qed

  2543

  2544 lemma islimpt_range_imp_convergent_subsequence:

  2545   fixes l :: "'a :: {t1_space, first_countable_topology}"

  2546   assumes l: "l islimpt (range f)"

  2547   shows "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2548   using l unfolding islimpt_eq_acc_point

  2549   by (rule acc_point_range_imp_convergent_subsequence)

  2550

  2551 lemma sequence_unique_limpt:

  2552   fixes f :: "nat \<Rightarrow> 'a::t2_space"

  2553   assumes "(f ---> l) sequentially" and "l' islimpt (range f)"

  2554   shows "l' = l"

  2555 proof (rule ccontr)

  2556   assume "l' \<noteq> l"

  2557   obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"

  2558     using hausdorff [OF l' \<noteq> l] by auto

  2559   have "eventually (\<lambda>n. f n \<in> t) sequentially"

  2560     using assms(1) open t l \<in> t by (rule topological_tendstoD)

  2561   then obtain N where "\<forall>n\<ge>N. f n \<in> t"

  2562     unfolding eventually_sequentially by auto

  2563

  2564   have "UNIV = {..<N} \<union> {N..}" by auto

  2565   hence "l' islimpt (f  ({..<N} \<union> {N..}))" using assms(2) by simp

  2566   hence "l' islimpt (f  {..<N} \<union> f  {N..})" by (simp add: image_Un)

  2567   hence "l' islimpt (f  {N..})" by (simp add: islimpt_union_finite)

  2568   then obtain y where "y \<in> f  {N..}" "y \<in> s" "y \<noteq> l'"

  2569     using l' \<in> s open s by (rule islimptE)

  2570   then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'" by auto

  2571   with \<forall>n\<ge>N. f n \<in> t have "f n \<in> s \<inter> t" by simp

  2572   with s \<inter> t = {} show False by simp

  2573 qed

  2574

  2575 lemma bolzano_weierstrass_imp_closed:

  2576   fixes s :: "'a::{first_countable_topology, t2_space} set"

  2577   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"

  2578   shows "closed s"

  2579 proof-

  2580   { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"

  2581     hence "l \<in> s"

  2582     proof(cases "\<forall>n. x n \<noteq> l")

  2583       case False thus "l\<in>s" using as(1) by auto

  2584     next

  2585       case True note cas = this

  2586       with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto

  2587       then obtain l' where "l'\<in>s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto

  2588       thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto

  2589     qed  }

  2590   thus ?thesis unfolding closed_sequential_limits by fast

  2591 qed

  2592

  2593 lemma compact_imp_closed:

  2594   fixes s :: "'a::t2_space set"

  2595   assumes "compact s" shows "closed s"

  2596 unfolding closed_def

  2597 proof (rule openI)

  2598   fix y assume "y \<in> - s"

  2599   let ?C = "\<Union>x\<in>s. {u. open u \<and> x \<in> u \<and> eventually (\<lambda>y. y \<notin> u) (nhds y)}"

  2600   note compact s

  2601   moreover have "\<forall>u\<in>?C. open u" by simp

  2602   moreover have "s \<subseteq> \<Union>?C"

  2603   proof

  2604     fix x assume "x \<in> s"

  2605     with y \<in> - s have "x \<noteq> y" by clarsimp

  2606     hence "\<exists>u v. open u \<and> open v \<and> x \<in> u \<and> y \<in> v \<and> u \<inter> v = {}"

  2607       by (rule hausdorff)

  2608     with x \<in> s show "x \<in> \<Union>?C"

  2609       unfolding eventually_nhds by auto

  2610   qed

  2611   ultimately obtain D where "D \<subseteq> ?C" and "finite D" and "s \<subseteq> \<Union>D"

  2612     by (rule compactE)

  2613   from D \<subseteq> ?C have "\<forall>x\<in>D. eventually (\<lambda>y. y \<notin> x) (nhds y)" by auto

  2614   with finite D have "eventually (\<lambda>y. y \<notin> \<Union>D) (nhds y)"

  2615     by (simp add: eventually_Ball_finite)

  2616   with s \<subseteq> \<Union>D have "eventually (\<lambda>y. y \<notin> s) (nhds y)"

  2617     by (auto elim!: eventually_mono [rotated])

  2618   thus "\<exists>t. open t \<and> y \<in> t \<and> t \<subseteq> - s"

  2619     by (simp add: eventually_nhds subset_eq)

  2620 qed

  2621

  2622 lemma compact_imp_bounded:

  2623   assumes "compact U" shows "bounded U"

  2624 proof -

  2625   have "compact U" "\<forall>x\<in>U. open (ball x 1)" "U \<subseteq> (\<Union>x\<in>U. ball x 1)" using assms by auto

  2626   then obtain D where D: "D \<subseteq> U" "finite D" "U \<subseteq> (\<Union>x\<in>D. ball x 1)"

  2627     by (elim compactE_image)

  2628   from finite D have "bounded (\<Union>x\<in>D. ball x 1)"

  2629     by (simp add: bounded_UN)

  2630   thus "bounded U" using U \<subseteq> (\<Union>x\<in>D. ball x 1)

  2631     by (rule bounded_subset)

  2632 qed

  2633

  2634 text{* In particular, some common special cases. *}

  2635

  2636 lemma compact_empty[simp]:

  2637  "compact {}"

  2638   unfolding compact_eq_heine_borel

  2639   by auto

  2640

  2641 lemma compact_union [intro]:

  2642   assumes "compact s" "compact t" shows " compact (s \<union> t)"

  2643 proof (rule compactI)

  2644   fix f assume *: "Ball f open" "s \<union> t \<subseteq> \<Union>f"

  2645   from * compact s obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"

  2646     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis

  2647   moreover from * compact t obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"

  2648     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis

  2649   ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"

  2650     by (auto intro!: exI[of _ "s' \<union> t'"])

  2651 qed

  2652

  2653 lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"

  2654   by (induct set: finite) auto

  2655

  2656 lemma compact_UN [intro]:

  2657   "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"

  2658   unfolding SUP_def by (rule compact_Union) auto

  2659

  2660 lemma compact_inter_closed [intro]:

  2661   assumes "compact s" and "closed t"

  2662   shows "compact (s \<inter> t)"

  2663 proof (rule compactI)

  2664   fix C assume C: "\<forall>c\<in>C. open c" and cover: "s \<inter> t \<subseteq> \<Union>C"

  2665   from C closed t have "\<forall>c\<in>C \<union> {-t}. open c" by auto

  2666   moreover from cover have "s \<subseteq> \<Union>(C \<union> {-t})" by auto

  2667   ultimately have "\<exists>D\<subseteq>C \<union> {-t}. finite D \<and> s \<subseteq> \<Union>D"

  2668     using compact s unfolding compact_eq_heine_borel by auto

  2669   then guess D ..

  2670   then show "\<exists>D\<subseteq>C. finite D \<and> s \<inter> t \<subseteq> \<Union>D"

  2671     by (intro exI[of _ "D - {-t}"]) auto

  2672 qed

  2673

  2674 lemma closed_inter_compact [intro]:

  2675   assumes "closed s" and "compact t"

  2676   shows "compact (s \<inter> t)"

  2677   using compact_inter_closed [of t s] assms

  2678   by (simp add: Int_commute)

  2679

  2680 lemma compact_inter [intro]:

  2681   fixes s t :: "'a :: t2_space set"

  2682   assumes "compact s" and "compact t"

  2683   shows "compact (s \<inter> t)"

  2684   using assms by (intro compact_inter_closed compact_imp_closed)

  2685

  2686 lemma compact_sing [simp]: "compact {a}"

  2687   unfolding compact_eq_heine_borel by auto

  2688

  2689 lemma compact_insert [simp]:

  2690   assumes "compact s" shows "compact (insert x s)"

  2691 proof -

  2692   have "compact ({x} \<union> s)"

  2693     using compact_sing assms by (rule compact_union)

  2694   thus ?thesis by simp

  2695 qed

  2696

  2697 lemma finite_imp_compact:

  2698   shows "finite s \<Longrightarrow> compact s"

  2699   by (induct set: finite) simp_all

  2700

  2701 lemma open_delete:

  2702   fixes s :: "'a::t1_space set"

  2703   shows "open s \<Longrightarrow> open (s - {x})"

  2704   by (simp add: open_Diff)

  2705

  2706 text{* Finite intersection property *}

  2707

  2708 lemma inj_setminus: "inj_on uminus (A::'a set set)"

  2709   by (auto simp: inj_on_def)

  2710

  2711 lemma compact_fip:

  2712   "compact U \<longleftrightarrow>

  2713     (\<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> {})"

  2714   (is "_ \<longleftrightarrow> ?R")

  2715 proof (safe intro!: compact_eq_heine_borel[THEN iffD2])

  2716   fix A assume "compact U" and A: "\<forall>a\<in>A. closed a" "U \<inter> \<Inter>A = {}"

  2717     and fi: "\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}"

  2718   from A have "(\<forall>a\<in>uminusA. open a) \<and> U \<subseteq> \<Union>uminusA"

  2719     by auto

  2720   with compact U obtain B where "B \<subseteq> A" "finite (uminusB)" "U \<subseteq> \<Union>(uminusB)"

  2721     unfolding compact_eq_heine_borel by (metis subset_image_iff)

  2722   with fi[THEN spec, of B] show False

  2723     by (auto dest: finite_imageD intro: inj_setminus)

  2724 next

  2725   fix A assume ?R and cover: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"

  2726   from cover have "U \<inter> \<Inter>(uminusA) = {}" "\<forall>a\<in>uminusA. closed a"

  2727     by auto

  2728   with ?R obtain B where "B \<subseteq> A" "finite (uminusB)" "U \<inter> \<Inter>uminusB = {}"

  2729     by (metis subset_image_iff)

  2730   then show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"

  2731     by  (auto intro!: exI[of _ B] inj_setminus dest: finite_imageD)

  2732 qed

  2733

  2734 lemma compact_imp_fip:

  2735   "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>

  2736     s \<inter> (\<Inter> f) \<noteq> {}"

  2737   unfolding compact_fip by auto

  2738

  2739 text{*Compactness expressed with filters*}

  2740

  2741 definition "filter_from_subbase B = Abs_filter (\<lambda>P. \<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"

  2742

  2743 lemma eventually_filter_from_subbase:

  2744   "eventually P (filter_from_subbase B) \<longleftrightarrow> (\<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"

  2745     (is "_ \<longleftrightarrow> ?R P")

  2746   unfolding filter_from_subbase_def

  2747 proof (rule eventually_Abs_filter is_filter.intro)+

  2748   show "?R (\<lambda>x. True)"

  2749     by (rule exI[of _ "{}"]) (simp add: le_fun_def)

  2750 next

  2751   fix P Q assume "?R P" then guess X ..

  2752   moreover assume "?R Q" then guess Y ..

  2753   ultimately show "?R (\<lambda>x. P x \<and> Q x)"

  2754     by (intro exI[of _ "X \<union> Y"]) auto

  2755 next

  2756   fix P Q

  2757   assume "?R P" then guess X ..

  2758   moreover assume "\<forall>x. P x \<longrightarrow> Q x"

  2759   ultimately show "?R Q"

  2760     by (intro exI[of _ X]) auto

  2761 qed

  2762

  2763 lemma eventually_filter_from_subbaseI: "P \<in> B \<Longrightarrow> eventually P (filter_from_subbase B)"

  2764   by (subst eventually_filter_from_subbase) (auto intro!: exI[of _ "{P}"])

  2765

  2766 lemma filter_from_subbase_not_bot:

  2767   "\<forall>X \<subseteq> B. finite X \<longrightarrow> Inf X \<noteq> bot \<Longrightarrow> filter_from_subbase B \<noteq> bot"

  2768   unfolding trivial_limit_def eventually_filter_from_subbase by auto

  2769

  2770 lemma closure_iff_nhds_not_empty:

  2771   "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"

  2772 proof safe

  2773   assume x: "x \<in> closure X"

  2774   fix S A assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"

  2775   then have "x \<notin> closure (-S)"

  2776     by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)

  2777   with x have "x \<in> closure X - closure (-S)"

  2778     by auto

  2779   also have "\<dots> \<subseteq> closure (X \<inter> S)"

  2780     using open S open_inter_closure_subset[of S X] by (simp add: closed_Compl ac_simps)

  2781   finally have "X \<inter> S \<noteq> {}" by auto

  2782   then show False using X \<inter> A = {} S \<subseteq> A by auto

  2783 next

  2784   assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"

  2785   from this[THEN spec, of "- X", THEN spec, of "- closure X"]

  2786   show "x \<in> closure X"

  2787     by (simp add: closure_subset open_Compl)

  2788 qed

  2789

  2790 lemma compact_filter:

  2791   "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))"

  2792 proof (intro allI iffI impI compact_fip[THEN iffD2] notI)

  2793   fix F assume "compact U" and F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"

  2794   from F have "U \<noteq> {}"

  2795     by (auto simp: eventually_False)

  2796

  2797   def Z \<equiv> "closure  {A. eventually (\<lambda>x. x \<in> A) F}"

  2798   then have "\<forall>z\<in>Z. closed z"

  2799     by auto

  2800   moreover

  2801   have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"

  2802     unfolding Z_def by (auto elim: eventually_elim1 intro: set_mp[OF closure_subset])

  2803   have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"

  2804   proof (intro allI impI)

  2805     fix B assume "finite B" "B \<subseteq> Z"

  2806     with finite B ev_Z have "eventually (\<lambda>x. \<forall>b\<in>B. x \<in> b) F"

  2807       by (auto intro!: eventually_Ball_finite)

  2808     with F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"

  2809       by eventually_elim auto

  2810     with F show "U \<inter> \<Inter>B \<noteq> {}"

  2811       by (intro notI) (simp add: eventually_False)

  2812   qed

  2813   ultimately have "U \<inter> \<Inter>Z \<noteq> {}"

  2814     using compact U unfolding compact_fip by blast

  2815   then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z" by auto

  2816

  2817   have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"

  2818     unfolding eventually_inf eventually_nhds

  2819   proof safe

  2820     fix P Q R S

  2821     assume "eventually R F" "open S" "x \<in> S"

  2822     with open_inter_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]

  2823     have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)

  2824     moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"

  2825     ultimately show False by (auto simp: set_eq_iff)

  2826   qed

  2827   with x \<in> U show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"

  2828     by (metis eventually_bot)

  2829 next

  2830   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 = {}"

  2831

  2832   def P' \<equiv> "(\<lambda>a (x::'a). x \<in> a)"

  2833   then have inj_P': "\<And>A. inj_on P' A"

  2834     by (auto intro!: inj_onI simp: fun_eq_iff)

  2835   def F \<equiv> "filter_from_subbase (P'  insert U A)"

  2836   have "F \<noteq> bot"

  2837     unfolding F_def

  2838   proof (safe intro!: filter_from_subbase_not_bot)

  2839     fix X assume "X \<subseteq> P'  insert U A" "finite X" "Inf X = bot"

  2840     then obtain B where "B \<subseteq> insert U A" "finite B" and B: "Inf (P'  B) = bot"

  2841       unfolding subset_image_iff by (auto intro: inj_P' finite_imageD)

  2842     with A(2)[THEN spec, of "B - {U}"] have "U \<inter> \<Inter>(B - {U}) \<noteq> {}" by auto

  2843     with B show False by (auto simp: P'_def fun_eq_iff)

  2844   qed

  2845   moreover have "eventually (\<lambda>x. x \<in> U) F"

  2846     unfolding F_def by (rule eventually_filter_from_subbaseI) (auto simp: P'_def)

  2847   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)"

  2848   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"

  2849     by auto

  2850

  2851   { fix V assume "V \<in> A"

  2852     then have V: "eventually (\<lambda>x. x \<in> V) F"

  2853       by (auto simp add: F_def image_iff P'_def intro!: eventually_filter_from_subbaseI)

  2854     have "x \<in> closure V"

  2855       unfolding closure_iff_nhds_not_empty

  2856     proof (intro impI allI)

  2857       fix S A assume "open S" "x \<in> S" "S \<subseteq> A"

  2858       then have "eventually (\<lambda>x. x \<in> A) (nhds x)" by (auto simp: eventually_nhds)

  2859       with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"

  2860         by (auto simp: eventually_inf)

  2861       with x show "V \<inter> A \<noteq> {}"

  2862         by (auto simp del: Int_iff simp add: trivial_limit_def)

  2863     qed

  2864     then have "x \<in> V"

  2865       using V \<in> A A(1) by simp }

  2866   with x\<in>U have "x \<in> U \<inter> \<Inter>A" by auto

  2867   with U \<inter> \<Inter>A = {} show False by auto

  2868 qed

  2869

  2870 definition "countably_compact U \<longleftrightarrow>

  2871     (\<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))"

  2872

  2873 lemma countably_compactE:

  2874   assumes "countably_compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C" "countable C"

  2875   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"

  2876   using assms unfolding countably_compact_def by metis

  2877

  2878 lemma countably_compactI:

  2879   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')"

  2880   shows "countably_compact s"

  2881   using assms unfolding countably_compact_def by metis

  2882

  2883 lemma compact_imp_countably_compact: "compact U \<Longrightarrow> countably_compact U"

  2884   by (auto simp: compact_eq_heine_borel countably_compact_def)

  2885

  2886 lemma countably_compact_imp_compact:

  2887   assumes "countably_compact U"

  2888   assumes ccover: "countable B" "\<forall>b\<in>B. open b"

  2889   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"

  2890   shows "compact U"

  2891   using countably_compact U unfolding compact_eq_heine_borel countably_compact_def

  2892 proof safe

  2893   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"

  2894   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)"

  2895

  2896   moreover def C \<equiv> "{b\<in>B. \<exists>a\<in>A. b \<inter> U \<subseteq> a}"

  2897   ultimately have "countable C" "\<forall>a\<in>C. open a"

  2898     unfolding C_def using ccover by auto

  2899   moreover

  2900   have "\<Union>A \<inter> U \<subseteq> \<Union>C"

  2901   proof safe

  2902     fix x a assume "x \<in> U" "x \<in> a" "a \<in> A"

  2903     with basis[of a x] A obtain b where "b \<in> B" "x \<in> b" "b \<inter> U \<subseteq> a" by blast

  2904     with a \<in> A show "x \<in> \<Union>C" unfolding C_def

  2905       by auto

  2906   qed

  2907   then have "U \<subseteq> \<Union>C" using U \<subseteq> \<Union>A by auto

  2908   ultimately obtain T where "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"

  2909     using * by metis

  2910   moreover then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<inter> U \<subseteq> a"

  2911     by (auto simp: C_def)

  2912   then guess f unfolding bchoice_iff Bex_def ..

  2913   ultimately show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"

  2914     unfolding C_def by (intro exI[of _ "fT"]) fastforce

  2915 qed

  2916

  2917 lemma countably_compact_imp_compact_second_countable:

  2918   "countably_compact U \<Longrightarrow> compact (U :: 'a :: second_countable_topology set)"

  2919 proof (rule countably_compact_imp_compact)

  2920   fix T and x :: 'a assume "open T" "x \<in> T"

  2921   from topological_basisE[OF is_basis this] guess b .

  2922   then show "\<exists>b\<in>SOME B. countable B \<and> topological_basis B. x \<in> b \<and> b \<inter> U \<subseteq> T" by auto

  2923 qed (insert countable_basis topological_basis_open[OF is_basis], auto)

  2924

  2925 lemma countably_compact_eq_compact:

  2926   "countably_compact U \<longleftrightarrow> compact (U :: 'a :: second_countable_topology set)"

  2927   using countably_compact_imp_compact_second_countable compact_imp_countably_compact by blast

  2928

  2929 subsubsection{* Sequential compactness *}

  2930

  2931 definition

  2932   seq_compact :: "'a::topological_space set \<Rightarrow> bool" where

  2933   "seq_compact S \<longleftrightarrow>

  2934    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>

  2935        (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"

  2936

  2937 lemma seq_compact_imp_countably_compact:

  2938   fixes U :: "'a :: first_countable_topology set"

  2939   assumes "seq_compact U"

  2940   shows "countably_compact U"

  2941 proof (safe intro!: countably_compactI)

  2942   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"

  2943   have subseq: "\<And>X. range X \<subseteq> U \<Longrightarrow> \<exists>r x. x \<in> U \<and> subseq r \<and> (X \<circ> r) ----> x"

  2944     using seq_compact U by (fastforce simp: seq_compact_def subset_eq)

  2945   show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"

  2946   proof cases

  2947     assume "finite A" with A show ?thesis by auto

  2948   next

  2949     assume "infinite A"

  2950     then have "A \<noteq> {}" by auto

  2951     show ?thesis

  2952     proof (rule ccontr)

  2953       assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"

  2954       then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)" by auto

  2955       then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T" by metis

  2956       def X \<equiv> "\<lambda>n. X' (from_nat_into A  {.. n})"

  2957       have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"

  2958         using A \<noteq> {} unfolding X_def SUP_def by (intro T) (auto intro: from_nat_into)

  2959       then have "range X \<subseteq> U" by auto

  2960       with subseq[of X] obtain r x where "x \<in> U" and r: "subseq r" "(X \<circ> r) ----> x" by auto

  2961       from x\<in>U U \<subseteq> \<Union>A from_nat_into_surj[OF countable A]

  2962       obtain n where "x \<in> from_nat_into A n" by auto

  2963       with r(2) A(1) from_nat_into[OF A \<noteq> {}, of n]

  2964       have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"

  2965         unfolding tendsto_def by (auto simp: comp_def)

  2966       then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"

  2967         by (auto simp: eventually_sequentially)

  2968       moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"

  2969         by auto

  2970       moreover from subseq r[THEN seq_suble, of "max n N"] have "\<exists>i. n \<le> r i \<and> N \<le> i"

  2971         by (auto intro!: exI[of _ "max n N"])

  2972       ultimately show False

  2973         by auto

  2974     qed

  2975   qed

  2976 qed

  2977

  2978 lemma compact_imp_seq_compact:

  2979   fixes U :: "'a :: first_countable_topology set"

  2980   assumes "compact U" shows "seq_compact U"

  2981   unfolding seq_compact_def

  2982 proof safe

  2983   fix X :: "nat \<Rightarrow> 'a" assume "\<forall>n. X n \<in> U"

  2984   then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"

  2985     by (auto simp: eventually_filtermap)

  2986   moreover have "filtermap X sequentially \<noteq> bot"

  2987     by (simp add: trivial_limit_def eventually_filtermap)

  2988   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")

  2989     using compact U by (auto simp: compact_filter)

  2990

  2991   from countable_basis_at_decseq[of x] guess A . note A = this

  2992   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> X j \<in> A (Suc n)"

  2993   { fix n i

  2994     have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"

  2995     proof (rule ccontr)

  2996       assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"

  2997       then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)" by auto

  2998       then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"

  2999         by (auto simp: eventually_filtermap eventually_sequentially)

  3000       moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"

  3001         using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)

  3002       ultimately have "eventually (\<lambda>x. False) ?F"

  3003         by (auto simp add: eventually_inf)

  3004       with x show False

  3005         by (simp add: eventually_False)

  3006     qed

  3007     then have "i < s n i" "X (s n i) \<in> A (Suc n)"

  3008       unfolding s_def by (auto intro: someI2_ex) }

  3009   note s = this

  3010   def r \<equiv> "nat_rec (s 0 0) s"

  3011   have "subseq r"

  3012     by (auto simp: r_def s subseq_Suc_iff)

  3013   moreover

  3014   have "(\<lambda>n. X (r n)) ----> x"

  3015   proof (rule topological_tendstoI)

  3016     fix S assume "open S" "x \<in> S"

  3017     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto

  3018     moreover

  3019     { fix i assume "Suc 0 \<le> i" then have "X (r i) \<in> A i"

  3020         by (cases i) (simp_all add: r_def s) }

  3021     then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)

  3022     ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"

  3023       by eventually_elim auto

  3024   qed

  3025   ultimately show "\<exists>x \<in> U. \<exists>r. subseq r \<and> (X \<circ> r) ----> x"

  3026     using x \<in> U by (auto simp: convergent_def comp_def)

  3027 qed

  3028

  3029 lemma seq_compactI:

  3030   assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially"

  3031   shows "seq_compact S"

  3032   unfolding seq_compact_def using assms by fast

  3033

  3034 lemma seq_compactE:

  3035   assumes "seq_compact S" "\<forall>n. f n \<in> S"

  3036   obtains l r where "l \<in> S" "subseq r" "((f \<circ> r) ---> l) sequentially"

  3037   using assms unfolding seq_compact_def by fast

  3038

  3039 lemma countably_compact_imp_acc_point:

  3040   assumes "countably_compact s" "countable t" "infinite t"  "t \<subseteq> s"

  3041   shows "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)"

  3042 proof (rule ccontr)

  3043   def C \<equiv> "(\<lambda>F. interior (F \<union> (- t)))  {F. finite F \<and> F \<subseteq> t }"

  3044   note countably_compact s

  3045   moreover have "\<forall>t\<in>C. open t"

  3046     by (auto simp: C_def)

  3047   moreover

  3048   assume "\<not> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"

  3049   then have s: "\<And>x. x \<in> s \<Longrightarrow> \<exists>U. x\<in>U \<and> open U \<and> finite (U \<inter> t)" by metis

  3050   have "s \<subseteq> \<Union>C"

  3051     using t \<subseteq> s

  3052     unfolding C_def Union_image_eq

  3053     apply (safe dest!: s)

  3054     apply (rule_tac a="U \<inter> t" in UN_I)

  3055     apply (auto intro!: interiorI simp add: finite_subset)

  3056     done

  3057   moreover

  3058   from countable t have "countable C"

  3059     unfolding C_def by (auto intro: countable_Collect_finite_subset)

  3060   ultimately guess D by (rule countably_compactE)

  3061   then obtain E where E: "E \<subseteq> {F. finite F \<and> F \<subseteq> t }" "finite E" and

  3062     s: "s \<subseteq> (\<Union>F\<in>E. interior (F \<union> (- t)))"

  3063     by (metis (lifting) Union_image_eq finite_subset_image C_def)

  3064   from s t \<subseteq> s have "t \<subseteq> \<Union>E"

  3065     using interior_subset by blast

  3066   moreover have "finite (\<Union>E)"

  3067     using E by auto

  3068   ultimately show False using infinite t by (auto simp: finite_subset)

  3069 qed

  3070

  3071 lemma countable_acc_point_imp_seq_compact:

  3072   fixes s :: "'a::first_countable_topology set"

  3073   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))"

  3074   shows "seq_compact s"

  3075 proof -

  3076   { fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"

  3077     have "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3078     proof (cases "finite (range f)")

  3079       case True

  3080       obtain l where "infinite {n. f n = f l}"

  3081         using pigeonhole_infinite[OF _ True] by auto

  3082       then obtain r where "subseq r" and fr: "\<forall>n. f (r n) = f l"

  3083         using infinite_enumerate by blast

  3084       hence "subseq r \<and> (f \<circ> r) ----> f l"

  3085         by (simp add: fr tendsto_const o_def)

  3086       with f show "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  3087         by auto

  3088     next

  3089       case False

  3090       with f assms have "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" by auto

  3091       then obtain l where "l \<in> s" "\<forall>U. l\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" ..

  3092       from this(2) have "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3093         using acc_point_range_imp_convergent_subsequence[of l f] by auto

  3094       with l \<in> s show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" ..

  3095     qed

  3096   }

  3097   thus ?thesis unfolding seq_compact_def by auto

  3098 qed

  3099

  3100 lemma seq_compact_eq_countably_compact:

  3101   fixes U :: "'a :: first_countable_topology set"

  3102   shows "seq_compact U \<longleftrightarrow> countably_compact U"

  3103   using

  3104     countable_acc_point_imp_seq_compact

  3105     countably_compact_imp_acc_point

  3106     seq_compact_imp_countably_compact

  3107   by metis

  3108

  3109 lemma seq_compact_eq_acc_point:

  3110   fixes s :: "'a :: first_countable_topology set"

  3111   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)))"

  3112   using

  3113     countable_acc_point_imp_seq_compact[of s]

  3114     countably_compact_imp_acc_point[of s]

  3115     seq_compact_imp_countably_compact[of s]

  3116   by metis

  3117

  3118 lemma seq_compact_eq_compact:

  3119   fixes U :: "'a :: second_countable_topology set"

  3120   shows "seq_compact U \<longleftrightarrow> compact U"

  3121   using seq_compact_eq_countably_compact countably_compact_eq_compact by blast

  3122

  3123 lemma bolzano_weierstrass_imp_seq_compact:

  3124   fixes s :: "'a::{t1_space, first_countable_topology} set"

  3125   shows "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> seq_compact s"

  3126   by (rule countable_acc_point_imp_seq_compact) (metis islimpt_eq_acc_point)

  3127

  3128 subsubsection{* Total boundedness *}

  3129

  3130 lemma cauchy_def:

  3131   "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N --> dist(s m)(s n) < e)"

  3132 unfolding Cauchy_def by metis

  3133

  3134 fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where

  3135   "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"

  3136 declare helper_1.simps[simp del]

  3137

  3138 lemma seq_compact_imp_totally_bounded:

  3139   assumes "seq_compact s"

  3140   shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e)  k))"

  3141 proof(rule, rule, rule ccontr)

  3142   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)"

  3143   def x \<equiv> "helper_1 s e"

  3144   { fix n

  3145     have "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)"

  3146     proof(induct_tac rule:nat_less_induct)

  3147       fix n  def Q \<equiv> "(\<lambda>y. y \<in> s \<and> (\<forall>m<n. \<not> dist (x m) y < e))"

  3148       assume as:"\<forall>m<n. x m \<in> s \<and> (\<forall>ma<m. \<not> dist (x ma) (x m) < e)"

  3149       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

  3150       then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x  {0..<n}. ball x e)" unfolding subset_eq by auto

  3151       have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]

  3152         apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto

  3153       thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto

  3154     qed }

  3155   hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+

  3156   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

  3157   from this(3) have "Cauchy (x \<circ> r)" using LIMSEQ_imp_Cauchy by auto

  3158   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

  3159   show False

  3160     using N[THEN spec[where x=N], THEN spec[where x="N+1"]]

  3161     using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]

  3162     using x[THEN spec[where x="r (N+1)"], THEN spec[where x="r (N)"]] by auto

  3163 qed

  3164

  3165 subsubsection{* Heine-Borel theorem *}

  3166

  3167 lemma seq_compact_imp_heine_borel:

  3168   fixes s :: "'a :: metric_space set"

  3169   assumes "seq_compact s" shows "compact s"

  3170 proof -

  3171   from seq_compact_imp_totally_bounded[OF seq_compact s]

  3172   guess f unfolding choice_iff' .. note f = this

  3173   def K \<equiv> "(\<lambda>(x, r). ball x r)  ((\<Union>e \<in> \<rat> \<inter> {0 <..}. f e) \<times> \<rat>)"

  3174   have "countably_compact s"

  3175     using seq_compact s by (rule seq_compact_imp_countably_compact)

  3176   then show "compact s"

  3177   proof (rule countably_compact_imp_compact)

  3178     show "countable K"

  3179       unfolding K_def using f

  3180       by (auto intro: countable_finite countable_subset countable_rat

  3181                intro!: countable_image countable_SIGMA countable_UN)

  3182     show "\<forall>b\<in>K. open b" by (auto simp: K_def)

  3183   next

  3184     fix T x assume T: "open T" "x \<in> T" and x: "x \<in> s"

  3185     from openE[OF T] obtain e where "0 < e" "ball x e \<subseteq> T" by auto

  3186     then have "0 < e / 2" "ball x (e / 2) \<subseteq> T" by auto

  3187     from Rats_dense_in_real[OF 0 < e / 2] obtain r where "r \<in> \<rat>" "0 < r" "r < e / 2" by auto

  3188     from f[rule_format, of r] 0 < r x \<in> s obtain k where "k \<in> f r" "x \<in> ball k r"

  3189       unfolding Union_image_eq by auto

  3190     from r \<in> \<rat> 0 < r k \<in> f r have "ball k r \<in> K" by (auto simp: K_def)

  3191     then show "\<exists>b\<in>K. x \<in> b \<and> b \<inter> s \<subseteq> T"

  3192     proof (rule bexI[rotated], safe)

  3193       fix y assume "y \<in> ball k r"

  3194       with r < e / 2 x \<in> ball k r have "dist x y < e"

  3195         by (intro dist_double[where x = k and d=e]) (auto simp: dist_commute)

  3196       with ball x e \<subseteq> T show "y \<in> T" by auto

  3197     qed (rule x \<in> ball k r)

  3198   qed

  3199 qed

  3200

  3201 lemma compact_eq_seq_compact_metric:

  3202   "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"

  3203   using compact_imp_seq_compact seq_compact_imp_heine_borel by blast

  3204

  3205 lemma compact_def:

  3206   "compact (S :: 'a::metric_space set) \<longleftrightarrow>

  3207    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r. subseq r \<and> (f o r) ----> l))"

  3208   unfolding compact_eq_seq_compact_metric seq_compact_def by auto

  3209

  3210 subsubsection {* Complete the chain of compactness variants *}

  3211

  3212 lemma compact_eq_bolzano_weierstrass:

  3213   fixes s :: "'a::metric_space set"

  3214   shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")

  3215 proof

  3216   assume ?lhs thus ?rhs using heine_borel_imp_bolzano_weierstrass[of s] by auto

  3217 next

  3218   assume ?rhs thus ?lhs

  3219     unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)

  3220 qed

  3221

  3222 lemma bolzano_weierstrass_imp_bounded:

  3223   "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> bounded s"

  3224   using compact_imp_bounded unfolding compact_eq_bolzano_weierstrass .

  3225

  3226 text {*

  3227   A metric space (or topological vector space) is said to have the

  3228   Heine-Borel property if every closed and bounded subset is compact.

  3229 *}

  3230

  3231 class heine_borel = metric_space +

  3232   assumes bounded_imp_convergent_subsequence:

  3233     "bounded (range f) \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3234

  3235 lemma bounded_closed_imp_seq_compact:

  3236   fixes s::"'a::heine_borel set"

  3237   assumes "bounded s" and "closed s" shows "seq_compact s"

  3238 proof (unfold seq_compact_def, clarify)

  3239   fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"

  3240   with bounded s have "bounded (range f)" by (auto intro: bounded_subset)

  3241   obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"

  3242     using bounded_imp_convergent_subsequence [OF bounded (range f)] by auto

  3243   from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp

  3244   have "l \<in> s" using closed s fr l

  3245     unfolding closed_sequential_limits by blast

  3246   show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3247     using l \<in> s r l by blast

  3248 qed

  3249

  3250 lemma compact_eq_bounded_closed:

  3251   fixes s :: "'a::heine_borel set"

  3252   shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")

  3253 proof

  3254   assume ?lhs thus ?rhs

  3255     using compact_imp_closed compact_imp_bounded by blast

  3256 next

  3257   assume ?rhs thus ?lhs

  3258     using bounded_closed_imp_seq_compact[of s] unfolding compact_eq_seq_compact_metric by auto

  3259 qed

  3260

  3261 (* TODO: is this lemma necessary? *)

  3262 lemma bounded_increasing_convergent:

  3263   fixes s :: "nat \<Rightarrow> real"

  3264   shows "bounded {s n| n. True} \<Longrightarrow> \<forall>n. s n \<le> s (Suc n) \<Longrightarrow> \<exists>l. s ----> l"

  3265   using Bseq_mono_convergent[of s] incseq_Suc_iff[of s]

  3266   by (auto simp: image_def Bseq_eq_bounded convergent_def incseq_def)

  3267

  3268 instance real :: heine_borel

  3269 proof

  3270   fix f :: "nat \<Rightarrow> real"

  3271   assume f: "bounded (range f)"

  3272   obtain r where r: "subseq r" "monoseq (f \<circ> r)"

  3273     unfolding comp_def by (metis seq_monosub)

  3274   moreover

  3275   then have "Bseq (f \<circ> r)"

  3276     unfolding Bseq_eq_bounded using f by (auto intro: bounded_subset)

  3277   ultimately show "\<exists>l r. subseq r \<and> (f \<circ> r) ----> l"

  3278     using Bseq_monoseq_convergent[of "f \<circ> r"] by (auto simp: convergent_def)

  3279 qed

  3280

  3281 lemma compact_lemma:

  3282   fixes f :: "nat \<Rightarrow> 'a::euclidean_space"

  3283   assumes "bounded (range f)"

  3284   shows "\<forall>d\<subseteq>Basis. \<exists>l::'a. \<exists> r. subseq r \<and>

  3285         (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially)"

  3286 proof safe

  3287   fix d :: "'a set" assume d: "d \<subseteq> Basis"

  3288   with finite_Basis have "finite d" by (blast intro: finite_subset)

  3289   from this d show "\<exists>l::'a. \<exists>r. subseq r \<and>

  3290       (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially)"

  3291   proof(induct d) case empty thus ?case unfolding subseq_def by auto

  3292   next case (insert k d) have k[intro]:"k\<in>Basis" using insert by auto

  3293     have s': "bounded ((\<lambda>x. x \<bullet> k)  range f)" using bounded (range f)

  3294       by (auto intro!: bounded_linear_image bounded_linear_inner_left)

  3295     obtain l1::"'a" and r1 where r1:"subseq r1" and

  3296       lr1:"\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) \<bullet> i) (l1 \<bullet> i) < e) sequentially"

  3297       using insert(3) using insert(4) by auto

  3298     have f': "\<forall>n. f (r1 n) \<bullet> k \<in> (\<lambda>x. x \<bullet> k)  range f" by simp

  3299     have "bounded (range (\<lambda>i. f (r1 i) \<bullet> k))"

  3300       by (metis (lifting) bounded_subset f' image_subsetI s')

  3301     then obtain l2 r2 where r2:"subseq r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) \<bullet> k) ---> l2) sequentially"

  3302       using bounded_imp_convergent_subsequence[of "\<lambda>i. f (r1 i) \<bullet> k"] by (auto simp: o_def)

  3303     def r \<equiv> "r1 \<circ> r2" have r:"subseq r"

  3304       using r1 and r2 unfolding r_def o_def subseq_def by auto

  3305     moreover

  3306     def l \<equiv> "(\<Sum>i\<in>Basis. (if i = k then l2 else l1\<bullet>i) *\<^sub>R i)::'a"

  3307     { fix e::real assume "e>0"

  3308       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

  3309       from lr2 e>0 have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) \<bullet> k) l2 < e) sequentially" by (rule tendstoD)

  3310       from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) \<bullet> i) (l1 \<bullet> i) < e) sequentially"

  3311         by (rule eventually_subseq)

  3312       have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially"

  3313         using N1' N2

  3314         by eventually_elim (insert insert.prems, auto simp: l_def r_def o_def)

  3315     }

  3316     ultimately show ?case by auto

  3317   qed

  3318 qed

  3319

  3320 instance euclidean_space \<subseteq> heine_borel

  3321 proof

  3322   fix f :: "nat \<Rightarrow> 'a"

  3323   assume f: "bounded (range f)"

  3324   then obtain l::'a and r where r: "subseq r"

  3325     and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially"

  3326     using compact_lemma [OF f] by blast

  3327   { fix e::real assume "e>0"

  3328     hence "0 < e / real_of_nat DIM('a)" by (auto intro!: divide_pos_pos DIM_positive)

  3329     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"

  3330       by simp

  3331     moreover

  3332     { fix n assume n: "\<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e / (real_of_nat DIM('a))"

  3333       have "dist (f (r n)) l \<le> (\<Sum>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i))"

  3334         apply(subst euclidean_dist_l2) using zero_le_dist by (rule setL2_le_setsum)

  3335       also have "\<dots> < (\<Sum>i\<in>(Basis::'a set). e / (real_of_nat DIM('a)))"

  3336         apply(rule setsum_strict_mono) using n by auto

  3337       finally have "dist (f (r n)) l < e"

  3338         by auto

  3339     }

  3340     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"

  3341       by (rule eventually_elim1)

  3342   }

  3343   hence *:"((f \<circ> r) ---> l) sequentially" unfolding o_def tendsto_iff by simp

  3344   with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" by auto

  3345 qed

  3346

  3347 lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst  s)"

  3348 unfolding bounded_def

  3349 apply clarify

  3350 apply (rule_tac x="a" in exI)

  3351 apply (rule_tac x="e" in exI)

  3352 apply clarsimp

  3353 apply (drule (1) bspec)

  3354 apply (simp add: dist_Pair_Pair)

  3355 apply (erule order_trans [OF real_sqrt_sum_squares_ge1])

  3356 done

  3357

  3358 lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd  s)"

  3359 unfolding bounded_def

  3360 apply clarify

  3361 apply (rule_tac x="b" in exI)

  3362 apply (rule_tac x="e" in exI)

  3363 apply clarsimp

  3364 apply (drule (1) bspec)

  3365 apply (simp add: dist_Pair_Pair)

  3366 apply (erule order_trans [OF real_sqrt_sum_squares_ge2])

  3367 done

  3368

  3369 instance prod :: (heine_borel, heine_borel) heine_borel

  3370 proof

  3371   fix f :: "nat \<Rightarrow> 'a \<times> 'b"

  3372   assume f: "bounded (range f)"

  3373   from f have s1: "bounded (range (fst \<circ> f))" unfolding image_comp by (rule bounded_fst)

  3374   obtain l1 r1 where r1: "subseq r1" and l1: "(\<lambda>n. fst (f (r1 n))) ----> l1"

  3375     using bounded_imp_convergent_subsequence [OF s1] unfolding o_def by fast

  3376   from f have s2: "bounded (range (snd \<circ> f \<circ> r1))"

  3377     by (auto simp add: image_comp intro: bounded_snd bounded_subset)

  3378   obtain l2 r2 where r2: "subseq r2"

  3379     and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) ---> l2) sequentially"

  3380     using bounded_imp_convergent_subsequence [OF s2]

  3381     unfolding o_def by fast

  3382   have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) ---> l1) sequentially"

  3383     using LIMSEQ_subseq_LIMSEQ [OF l1 r2] unfolding o_def .

  3384   have l: "((f \<circ> (r1 \<circ> r2)) ---> (l1, l2)) sequentially"

  3385     using tendsto_Pair [OF l1' l2] unfolding o_def by simp

  3386   have r: "subseq (r1 \<circ> r2)"

  3387     using r1 r2 unfolding subseq_def by simp

  3388   show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3389     using l r by fast

  3390 qed

  3391

  3392 subsubsection{* Completeness *}

  3393

  3394 definition complete :: "'a::metric_space set \<Rightarrow> bool" where

  3395   "complete s \<longleftrightarrow> (\<forall>f. (\<forall>n. f n \<in> s) \<and> Cauchy f \<longrightarrow> (\<exists>l\<in>s. f ----> l))"

  3396

  3397 lemma compact_imp_complete: assumes "compact s" shows "complete s"

  3398 proof-

  3399   { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"

  3400     from as(1) obtain l r where lr: "l\<in>s" "subseq r" "(f \<circ> r) ----> l"

  3401       using assms unfolding compact_def by blast

  3402

  3403     note lr' = seq_suble [OF lr(2)]

  3404

  3405     { fix e::real assume "e>0"

  3406       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

  3407       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

  3408       { fix n::nat assume n:"n \<ge> max N M"

  3409         have "dist ((f \<circ> r) n) l < e/2" using n M by auto

  3410         moreover have "r n \<ge> N" using lr'[of n] n by auto

  3411         hence "dist (f n) ((f \<circ> r) n) < e / 2" using N using n by auto

  3412         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)  }

  3413       hence "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast  }

  3414     hence "\<exists>l\<in>s. (f ---> l) sequentially" using l\<in>s unfolding LIMSEQ_def by auto  }

  3415   thus ?thesis unfolding complete_def by auto

  3416 qed

  3417

  3418 lemma nat_approx_posE:

  3419   fixes e::real

  3420   assumes "0 < e"

  3421   obtains n::nat where "1 / (Suc n) < e"

  3422 proof atomize_elim

  3423   have " 1 / real (Suc (nat (ceiling (1/e)))) < 1 / (ceiling (1/e))"

  3424     by (rule divide_strict_left_mono) (auto intro!: mult_pos_pos simp: 0 < e)

  3425   also have "1 / (ceiling (1/e)) \<le> 1 / (1/e)"

  3426     by (rule divide_left_mono) (auto intro!: divide_pos_pos simp: 0 < e)

  3427   also have "\<dots> = e" by simp

  3428   finally show  "\<exists>n. 1 / real (Suc n) < e" ..

  3429 qed

  3430

  3431 lemma compact_eq_totally_bounded:

  3432   "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e)  k)))"

  3433     (is "_ \<longleftrightarrow> ?rhs")

  3434 proof

  3435   assume assms: "?rhs"

  3436   then obtain k where k: "\<And>e. 0 < e \<Longrightarrow> finite (k e)" "\<And>e. 0 < e \<Longrightarrow> s \<subseteq> (\<Union>x\<in>k e. ball x e)"

  3437     by (auto simp: choice_iff')

  3438

  3439   show "compact s"

  3440   proof cases

  3441     assume "s = {}" thus "compact s" by (simp add: compact_def)

  3442   next

  3443     assume "s \<noteq> {}"

  3444     show ?thesis

  3445       unfolding compact_def

  3446     proof safe

  3447       fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"

  3448

  3449       def e \<equiv> "\<lambda>n. 1 / (2 * Suc n)"

  3450       then have [simp]: "\<And>n. 0 < e n" by auto

  3451       def B \<equiv> "\<lambda>n U. SOME b. infinite {n. f n \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U)"

  3452       { fix n U assume "infinite {n. f n \<in> U}"

  3453         then have "\<exists>b\<in>k (e n). infinite {i\<in>{n. f n \<in> U}. f i \<in> ball b (e n)}"

  3454           using k f by (intro pigeonhole_infinite_rel) (auto simp: subset_eq)

  3455         then guess a ..

  3456         then have "\<exists>b. infinite {i. f i \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U)"

  3457           by (intro exI[of _ "ball a (e n) \<inter> U"] exI[of _ a]) (auto simp: ac_simps)

  3458         from someI_ex[OF this]

  3459         have "infinite {i. f i \<in> B n U}" "\<exists>x. B n U \<subseteq> ball x (e n) \<inter> U"

  3460           unfolding B_def by auto }

  3461       note B = this

  3462

  3463       def F \<equiv> "nat_rec (B 0 UNIV) B"

  3464       { fix n have "infinite {i. f i \<in> F n}"

  3465           by (induct n) (auto simp: F_def B) }

  3466       then have F: "\<And>n. \<exists>x. F (Suc n) \<subseteq> ball x (e n) \<inter> F n"

  3467         using B by (simp add: F_def)

  3468       then have F_dec: "\<And>m n. m \<le> n \<Longrightarrow> F n \<subseteq> F m"

  3469         using decseq_SucI[of F] by (auto simp: decseq_def)

  3470

  3471       obtain sel where sel: "\<And>k i. i < sel k i" "\<And>k i. f (sel k i) \<in> F k"

  3472       proof (atomize_elim, unfold all_conj_distrib[symmetric], intro choice allI)

  3473         fix k i

  3474         have "infinite ({n. f n \<in> F k} - {.. i})"

  3475           using infinite {n. f n \<in> F k} by auto

  3476         from infinite_imp_nonempty[OF this]

  3477         show "\<exists>x>i. f x \<in> F k"

  3478           by (simp add: set_eq_iff not_le conj_commute)

  3479       qed

  3480

  3481       def t \<equiv> "nat_rec (sel 0 0) (\<lambda>n i. sel (Suc n) i)"

  3482       have "subseq t"

  3483         unfolding subseq_Suc_iff by (simp add: t_def sel)

  3484       moreover have "\<forall>i. (f \<circ> t) i \<in> s"

  3485         using f by auto

  3486       moreover

  3487       { fix n have "(f \<circ> t) n \<in> F n"

  3488           by (cases n) (simp_all add: t_def sel) }

  3489       note t = this

  3490

  3491       have "Cauchy (f \<circ> t)"

  3492       proof (safe intro!: metric_CauchyI exI elim!: nat_approx_posE)

  3493         fix r :: real and N n m assume "1 / Suc N < r" "Suc N \<le> n" "Suc N \<le> m"

  3494         then have "(f \<circ> t) n \<in> F (Suc N)" "(f \<circ> t) m \<in> F (Suc N)" "2 * e N < r"

  3495           using F_dec t by (auto simp: e_def field_simps real_of_nat_Suc)

  3496         with F[of N] obtain x where "dist x ((f \<circ> t) n) < e N" "dist x ((f \<circ> t) m) < e N"

  3497           by (auto simp: subset_eq)

  3498         with dist_triangle[of "(f \<circ> t) m" "(f \<circ> t) n" x] 2 * e N < r

  3499         show "dist ((f \<circ> t) m) ((f \<circ> t) n) < r"

  3500           by (simp add: dist_commute)

  3501       qed

  3502

  3503       ultimately show "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  3504         using assms unfolding complete_def by blast

  3505     qed

  3506   qed

  3507 qed (metis compact_imp_complete compact_imp_seq_compact seq_compact_imp_totally_bounded)

  3508

  3509 lemma cauchy: "Cauchy s \<longleftrightarrow> (\<forall>e>0.\<exists> N::nat. \<forall>n\<ge>N. dist(s n)(s N) < e)" (is "?lhs = ?rhs")

  3510 proof-

  3511   { assume ?rhs

  3512     { fix e::real

  3513       assume "e>0"

  3514       with ?rhs obtain N where N:"\<forall>n\<ge>N. dist (s n) (s N) < e/2"

  3515         by (erule_tac x="e/2" in allE) auto

  3516       { fix n m

  3517         assume nm:"N \<le> m \<and> N \<le> n"

  3518         hence "dist (s m) (s n) < e" using N

  3519           using dist_triangle_half_l[of "s m" "s N" "e" "s n"]

  3520           by blast

  3521       }

  3522       hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"

  3523         by blast

  3524     }

  3525     hence ?lhs

  3526       unfolding cauchy_def

  3527       by blast

  3528   }

  3529   thus ?thesis

  3530     unfolding cauchy_def

  3531     using dist_triangle_half_l

  3532     by blast

  3533 qed

  3534

  3535 lemma cauchy_imp_bounded: assumes "Cauchy s" shows "bounded (range s)"

  3536 proof-

  3537   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

  3538   hence N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto

  3539   moreover

  3540   have "bounded (s  {0..N})" using finite_imp_bounded[of "s  {1..N}"] by auto

  3541   then obtain a where a:"\<forall>x\<in>s  {0..N}. dist (s N) x \<le> a"

  3542     unfolding bounded_any_center [where a="s N"] by auto

  3543   ultimately show "?thesis"

  3544     unfolding bounded_any_center [where a="s N"]

  3545     apply(rule_tac x="max a 1" in exI) apply auto

  3546     apply(erule_tac x=y in allE) apply(erule_tac x=y in ballE) by auto

  3547 qed

  3548

  3549 instance heine_borel < complete_space

  3550 proof

  3551   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"

  3552   hence "bounded (range f)"

  3553     by (rule cauchy_imp_bounded)

  3554   hence "compact (closure (range f))"

  3555     unfolding compact_eq_bounded_closed by auto

  3556   hence "complete (closure (range f))"

  3557     by (rule compact_imp_complete)

  3558   moreover have "\<forall>n. f n \<in> closure (range f)"

  3559     using closure_subset [of "range f"] by auto

  3560   ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"

  3561     using Cauchy f unfolding complete_def by auto

  3562   then show "convergent f"

  3563     unfolding convergent_def by auto

  3564 qed

  3565

  3566 instance euclidean_space \<subseteq> banach ..

  3567

  3568 lemma complete_univ: "complete (UNIV :: 'a::complete_space set)"

  3569 proof(simp add: complete_def, rule, rule)

  3570   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"

  3571   hence "convergent f" by (rule Cauchy_convergent)

  3572   thus "\<exists>l. f ----> l" unfolding convergent_def .

  3573 qed

  3574

  3575 lemma complete_imp_closed: assumes "complete s" shows "closed s"

  3576 proof -

  3577   { fix x assume "x islimpt s"

  3578     then obtain f where f: "\<forall>n. f n \<in> s - {x}" "(f ---> x) sequentially"

  3579       unfolding islimpt_sequential by auto

  3580     then obtain l where l: "l\<in>s" "(f ---> l) sequentially"

  3581       using complete s[unfolded complete_def] using LIMSEQ_imp_Cauchy[of f x] by auto

  3582     hence "x \<in> s"  using tendsto_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto

  3583   }

  3584   thus "closed s" unfolding closed_limpt by auto

  3585 qed

  3586

  3587 lemma complete_eq_closed:

  3588   fixes s :: "'a::complete_space set"

  3589   shows "complete s \<longleftrightarrow> closed s" (is "?lhs = ?rhs")

  3590 proof

  3591   assume ?lhs thus ?rhs by (rule complete_imp_closed)

  3592 next

  3593   assume ?rhs

  3594   { fix f assume as:"\<forall>n::nat. f n \<in> s" "Cauchy f"

  3595     then obtain l where "(f ---> l) sequentially" using complete_univ[unfolded complete_def, THEN spec[where x=f]] by auto

  3596     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  }

  3597   thus ?lhs unfolding complete_def by auto

  3598 qed

  3599

  3600 lemma convergent_eq_cauchy:

  3601   fixes s :: "nat \<Rightarrow> 'a::complete_space"

  3602   shows "(\<exists>l. (s ---> l) sequentially) \<longleftrightarrow> Cauchy s"

  3603   unfolding Cauchy_convergent_iff convergent_def ..

  3604

  3605 lemma convergent_imp_bounded:

  3606   fixes s :: "nat \<Rightarrow> 'a::metric_space"

  3607   shows "(s ---> l) sequentially \<Longrightarrow> bounded (range s)"

  3608   by (intro cauchy_imp_bounded LIMSEQ_imp_Cauchy)

  3609

  3610 lemma compact_cball[simp]:

  3611   fixes x :: "'a::heine_borel"

  3612   shows "compact(cball x e)"

  3613   using compact_eq_bounded_closed bounded_cball closed_cball

  3614   by blast

  3615

  3616 lemma compact_frontier_bounded[intro]:

  3617   fixes s :: "'a::heine_borel set"

  3618   shows "bounded s ==> compact(frontier s)"

  3619   unfolding frontier_def

  3620   using compact_eq_bounded_closed

  3621   by blast

  3622

  3623 lemma compact_frontier[intro]:

  3624   fixes s :: "'a::heine_borel set"

  3625   shows "compact s ==> compact (frontier s)"

  3626   using compact_eq_bounded_closed compact_frontier_bounded

  3627   by blast

  3628

  3629 lemma frontier_subset_compact:

  3630   fixes s :: "'a::heine_borel set"

  3631   shows "compact s ==> frontier s \<subseteq> s"

  3632   using frontier_subset_closed compact_eq_bounded_closed

  3633   by blast

  3634

  3635 subsection {* Bounded closed nest property (proof does not use Heine-Borel) *}

  3636

  3637 lemma bounded_closed_nest:

  3638   assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"

  3639   "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"

  3640   shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"

  3641 proof-

  3642   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

  3643   from assms(4,1) have *:"seq_compact (s 0)" using bounded_closed_imp_seq_compact[of "s 0"] by auto

  3644

  3645   then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"

  3646     unfolding seq_compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast

  3647

  3648   { fix n::nat

  3649     { fix e::real assume "e>0"

  3650       with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding LIMSEQ_def by auto

  3651       hence "dist ((x \<circ> r) (max N n)) l < e" by auto

  3652       moreover

  3653       have "r (max N n) \<ge> n" using lr(2) using seq_suble[of r "max N n"] by auto

  3654       hence "(x \<circ> r) (max N n) \<in> s n"

  3655         using x apply(erule_tac x=n in allE)

  3656         using x apply(erule_tac x="r (max N n)" in allE)

  3657         using assms(3) apply(erule_tac x=n in allE) apply(erule_tac x="r (max N n)" in allE) by auto

  3658       ultimately have "\<exists>y\<in>s n. dist y l < e" by auto

  3659     }

  3660     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast

  3661   }

  3662   thus ?thesis by auto

  3663 qed

  3664

  3665 text {* Decreasing case does not even need compactness, just completeness. *}

  3666

  3667 lemma decreasing_closed_nest:

  3668   assumes "\<forall>n. closed(s n)"

  3669           "\<forall>n. (s n \<noteq> {})"

  3670           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"

  3671           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y \<in> (s n). dist x y < e"

  3672   shows "\<exists>a::'a::complete_space. \<forall>n::nat. a \<in> s n"

  3673 proof-

  3674   have "\<forall>n. \<exists> x. x\<in>s n" using assms(2) by auto

  3675   hence "\<exists>t. \<forall>n. t n \<in> s n" using choice[of "\<lambda> n x. x \<in> s n"] by auto

  3676   then obtain t where t: "\<forall>n. t n \<in> s n" by auto

  3677   { fix e::real assume "e>0"

  3678     then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e" using assms(4) by auto

  3679     { fix m n ::nat assume "N \<le> m \<and> N \<le> n"

  3680       hence "t m \<in> s N" "t n \<in> s N" using assms(3) t unfolding  subset_eq t by blast+

  3681       hence "dist (t m) (t n) < e" using N by auto

  3682     }

  3683     hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e" by auto

  3684   }

  3685   hence  "Cauchy t" unfolding cauchy_def by auto

  3686   then obtain l where l:"(t ---> l) sequentially" using complete_univ unfolding complete_def by auto

  3687   { fix n::nat

  3688     { fix e::real assume "e>0"

  3689       then obtain N::nat where N:"\<forall>n\<ge>N. dist (t n) l < e" using l[unfolded LIMSEQ_def] by auto

  3690       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

  3691       hence "\<exists>y\<in>s n. dist y l < e" apply(rule_tac x="t (max n N)" in bexI) using N by auto

  3692     }

  3693     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by auto

  3694   }

  3695   then show ?thesis by auto

  3696 qed

  3697

  3698 text {* Strengthen it to the intersection actually being a singleton. *}

  3699

  3700 lemma decreasing_closed_nest_sing:

  3701   fixes s :: "nat \<Rightarrow> 'a::complete_space set"

  3702   assumes "\<forall>n. closed(s n)"

  3703           "\<forall>n. s n \<noteq> {}"

  3704           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"

  3705           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"

  3706   shows "\<exists>a. \<Inter>(range s) = {a}"

  3707 proof-

  3708   obtain a where a:"\<forall>n. a \<in> s n" using decreasing_closed_nest[of s] using assms by auto

  3709   { fix b assume b:"b \<in> \<Inter>(range s)"

  3710     { fix e::real assume "e>0"

  3711       hence "dist a b < e" using assms(4 )using b using a by blast

  3712     }

  3713     hence "dist a b = 0" by (metis dist_eq_0_iff dist_nz less_le)

  3714   }

  3715   with a have "\<Inter>(range s) = {a}" unfolding image_def by auto

  3716   thus ?thesis ..

  3717 qed

  3718

  3719 text{* Cauchy-type criteria for uniform convergence. *}

  3720

  3721 lemma uniformly_convergent_eq_cauchy: fixes s::"nat \<Rightarrow> 'b \<Rightarrow> 'a::complete_space" shows

  3722  "(\<exists>l. \<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e) \<longleftrightarrow>

  3723   (\<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")

  3724 proof(rule)

  3725   assume ?lhs

  3726   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

  3727   { fix e::real assume "e>0"

  3728     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

  3729     { fix n m::nat and x::"'b" assume "N \<le> m \<and> N \<le> n \<and> P x"

  3730       hence "dist (s m x) (s n x) < e"

  3731         using N[THEN spec[where x=m], THEN spec[where x=x]]

  3732         using N[THEN spec[where x=n], THEN spec[where x=x]]

  3733         using dist_triangle_half_l[of "s m x" "l x" e "s n x"] by auto  }

  3734     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  }

  3735   thus ?rhs by auto

  3736 next

  3737   assume ?rhs

  3738   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

  3739   then obtain l where l:"\<forall>x. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l x) sequentially" unfolding convergent_eq_cauchy[THEN sym]

  3740     using choice[of "\<lambda>x l. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l) sequentially"] by auto

  3741   { fix e::real assume "e>0"

  3742     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"

  3743       using ?rhs[THEN spec[where x="e/2"]] by auto

  3744     { fix x assume "P x"

  3745       then obtain M where M:"\<forall>n\<ge>M. dist (s n x) (l x) < e/2"

  3746         using l[THEN spec[where x=x], unfolded LIMSEQ_def] using e>0 by(auto elim!: allE[where x="e/2"])

  3747       fix n::nat assume "n\<ge>N"

  3748       hence "dist(s n x)(l x) < e"  using P xand N[THEN spec[where x=n], THEN spec[where x="N+M"], THEN spec[where x=x]]

  3749         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)  }

  3750     hence "\<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist(s n x)(l x) < e" by auto }

  3751   thus ?lhs by auto

  3752 qed

  3753

  3754 lemma uniformly_cauchy_imp_uniformly_convergent:

  3755   fixes s :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::complete_space"

  3756   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"

  3757           "\<forall>x. P x --> (\<forall>e>0. \<exists>N. \<forall>n. N \<le> n --> dist(s n x)(l x) < e)"

  3758   shows "\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e"

  3759 proof-

  3760   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"

  3761     using assms(1) unfolding uniformly_convergent_eq_cauchy[THEN sym] by auto

  3762   moreover

  3763   { fix x assume "P x"

  3764     hence "l x = l' x" using tendsto_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]

  3765       using l and assms(2) unfolding LIMSEQ_def by blast  }

  3766   ultimately show ?thesis by auto

  3767 qed

  3768

  3769

  3770 subsection {* Continuity *}

  3771

  3772 text {* Define continuity over a net to take in restrictions of the set. *}

  3773

  3774 definition

  3775   continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"

  3776   where "continuous net f \<longleftrightarrow> (f ---> f(netlimit net)) net"

  3777

  3778 lemma continuous_trivial_limit:

  3779  "trivial_limit net ==> continuous net f"

  3780   unfolding continuous_def tendsto_def trivial_limit_eq by auto

  3781

  3782 lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f(x)) (at x within s)"

  3783   unfolding continuous_def

  3784   unfolding tendsto_def

  3785   using netlimit_within[of x s]

  3786   by (cases "trivial_limit (at x within s)") (auto simp add: trivial_limit_eventually)

  3787

  3788 lemma continuous_at: "continuous (at x) f \<longleftrightarrow> (f ---> f(x)) (at x)"

  3789   using continuous_within [of x UNIV f] by simp

  3790

  3791 lemma continuous_isCont: "isCont f x = continuous (at x) f"

  3792   unfolding isCont_def LIM_def

  3793   unfolding continuous_at Lim_at unfolding dist_nz by auto

  3794

  3795 lemma continuous_at_within:

  3796   assumes "continuous (at x) f"  shows "continuous (at x within s) f"

  3797   using assms unfolding continuous_at continuous_within

  3798   by (rule Lim_at_within)

  3799

  3800

  3801 text{* Derive the epsilon-delta forms, which we often use as "definitions" *}

  3802

  3803 lemma continuous_within_eps_delta:

  3804   "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)"

  3805   unfolding continuous_within and Lim_within

  3806   apply auto unfolding dist_nz[THEN sym] apply(auto del: allE elim!:allE) apply(rule_tac x=d in exI) by auto

  3807

  3808 lemma continuous_at_eps_delta: "continuous (at x) f \<longleftrightarrow>  (\<forall>e>0. \<exists>d>0.

  3809                            \<forall>x'. dist x' x < d --> dist(f x')(f x) < e)"

  3810   using continuous_within_eps_delta [of x UNIV f] by simp

  3811

  3812 text{* Versions in terms of open balls. *}

  3813

  3814 lemma continuous_within_ball:

  3815  "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.

  3816                             f  (ball x d \<inter> s) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")

  3817 proof

  3818   assume ?lhs

  3819   { fix e::real assume "e>0"

  3820     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"

  3821       using ?lhs[unfolded continuous_within Lim_within] by auto

  3822     { fix y assume "y\<in>f  (ball x d \<inter> s)"

  3823       hence "y \<in> ball (f x) e" using d(2) unfolding dist_nz[THEN sym]

  3824         apply (auto simp add: dist_commute) apply(erule_tac x=xa in ballE) apply auto using e>0 by auto

  3825     }

  3826     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)  }

  3827   thus ?rhs by auto

  3828 next

  3829   assume ?rhs thus ?lhs unfolding continuous_within Lim_within ball_def subset_eq

  3830     apply (auto simp add: dist_commute) apply(erule_tac x=e in allE) by auto

  3831 qed

  3832

  3833 lemma continuous_at_ball:

  3834   "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f  (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")

  3835 proof

  3836   assume ?lhs thus ?rhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball

  3837     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)

  3838     unfolding dist_nz[THEN sym] by auto

  3839 next

  3840   assume ?rhs thus ?lhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball

  3841     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)

  3842 qed

  3843

  3844 text{* Define setwise continuity in terms of limits within the set. *}

  3845

  3846 definition

  3847   continuous_on ::

  3848     "'a set \<Rightarrow> ('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"

  3849 where

  3850   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f ---> f x) (at x within s))"

  3851

  3852 lemma continuous_on_topological:

  3853   "continuous_on s f \<longleftrightarrow>

  3854     (\<forall>x\<in>s. \<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>

  3855       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"

  3856 unfolding continuous_on_def tendsto_def

  3857 unfolding Limits.eventually_within eventually_at_topological

  3858 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto

  3859

  3860 lemma continuous_on_iff:

  3861   "continuous_on s f \<longleftrightarrow>

  3862     (\<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)"

  3863 unfolding continuous_on_def Lim_within

  3864 apply (intro ball_cong [OF refl] all_cong ex_cong)

  3865 apply (rename_tac y, case_tac "y = x", simp)

  3866 apply (simp add: dist_nz)

  3867 done

  3868

  3869 definition

  3870   uniformly_continuous_on ::

  3871     "'a set \<Rightarrow> ('a::metric_space \<Rightarrow> 'b::metric_space) \<Rightarrow> bool"

  3872 where

  3873   "uniformly_continuous_on s f \<longleftrightarrow>

  3874     (\<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)"

  3875

  3876 text{* Some simple consequential lemmas. *}

  3877

  3878 lemma uniformly_continuous_imp_continuous:

  3879  " uniformly_continuous_on s f ==> continuous_on s f"

  3880   unfolding uniformly_continuous_on_def continuous_on_iff by blast

  3881

  3882 lemma continuous_at_imp_continuous_within:

  3883  "continuous (at x) f ==> continuous (at x within s) f"

  3884   unfolding continuous_within continuous_at using Lim_at_within by auto

  3885

  3886 lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f ---> l) net"

  3887 unfolding tendsto_def by (simp add: trivial_limit_eq)

  3888

  3889 lemma continuous_at_imp_continuous_on:

  3890   assumes "\<forall>x\<in>s. continuous (at x) f"

  3891   shows "continuous_on s f"

  3892 unfolding continuous_on_def

  3893 proof

  3894   fix x assume "x \<in> s"

  3895   with assms have *: "(f ---> f (netlimit (at x))) (at x)"

  3896     unfolding continuous_def by simp

  3897   have "(f ---> f x) (at x)"

  3898   proof (cases "trivial_limit (at x)")

  3899     case True thus ?thesis

  3900       by (rule Lim_trivial_limit)

  3901   next

  3902     case False

  3903     hence 1: "netlimit (at x) = x"

  3904       using netlimit_within [of x UNIV] by simp

  3905     with * show ?thesis by simp

  3906   qed

  3907   thus "(f ---> f x) (at x within s)"

  3908     by (rule Lim_at_within)

  3909 qed

  3910

  3911 lemma continuous_on_eq_continuous_within:

  3912   "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x within s) f)"

  3913 unfolding continuous_on_def continuous_def

  3914 apply (rule ball_cong [OF refl])

  3915 apply (case_tac "trivial_limit (at x within s)")

  3916 apply (simp add: Lim_trivial_limit)

  3917 apply (simp add: netlimit_within)

  3918 done

  3919

  3920 lemmas continuous_on = continuous_on_def -- "legacy theorem name"

  3921

  3922 lemma continuous_on_eq_continuous_at:

  3923   shows "open s ==> (continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x) f))"

  3924   by (auto simp add: continuous_on continuous_at Lim_within_open)

  3925

  3926 lemma continuous_within_subset:

  3927  "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s

  3928              ==> continuous (at x within t) f"

  3929   unfolding continuous_within by(metis Lim_within_subset)

  3930

  3931 lemma continuous_on_subset:

  3932   shows "continuous_on s f \<Longrightarrow> t \<subseteq> s ==> continuous_on t f"

  3933   unfolding continuous_on by (metis subset_eq Lim_within_subset)

  3934

  3935 lemma continuous_on_interior:

  3936   shows "continuous_on s f \<Longrightarrow> x \<in> interior s \<Longrightarrow> continuous (at x) f"

  3937   by (erule interiorE, drule (1) continuous_on_subset,

  3938     simp add: continuous_on_eq_continuous_at)

  3939

  3940 lemma continuous_on_eq:

  3941   "(\<forall>x \<in> s. f x = g x) \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on s g"

  3942   unfolding continuous_on_def tendsto_def Limits.eventually_within

  3943   by simp

  3944

  3945 text {* Characterization of various kinds of continuity in terms of sequences. *}

  3946

  3947 lemma continuous_within_sequentially:

  3948   fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  3949   shows "continuous (at a within s) f \<longleftrightarrow>

  3950                 (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x ---> a) sequentially

  3951                      --> ((f o x) ---> f a) sequentially)" (is "?lhs = ?rhs")

  3952 proof

  3953   assume ?lhs

  3954   { 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"

  3955     fix T::"'b set" assume "open T" and "f a \<in> T"

  3956     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"

  3957       unfolding continuous_within tendsto_def eventually_within by auto

  3958     have "eventually (\<lambda>n. dist (x n) a < d) sequentially"

  3959       using x(2) d>0 by simp

  3960     hence "eventually (\<lambda>n. (f \<circ> x) n \<in> T) sequentially"

  3961     proof eventually_elim

  3962       case (elim n) thus ?case

  3963         using d x(1) f a \<in> T unfolding dist_nz[THEN sym] by auto

  3964     qed

  3965   }

  3966   thus ?rhs unfolding tendsto_iff unfolding tendsto_def by simp

  3967 next

  3968   assume ?rhs thus ?lhs

  3969     unfolding continuous_within tendsto_def [where l="f a"]

  3970     by (simp add: sequentially_imp_eventually_within)

  3971 qed

  3972

  3973 lemma continuous_at_sequentially:

  3974   fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  3975   shows "continuous (at a) f \<longleftrightarrow> (\<forall>x. (x ---> a) sequentially

  3976                   --> ((f o x) ---> f a) sequentially)"

  3977   using continuous_within_sequentially[of a UNIV f] by simp

  3978

  3979 lemma continuous_on_sequentially:

  3980   fixes f :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  3981   shows "continuous_on s f \<longleftrightarrow>

  3982     (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x ---> a) sequentially

  3983                     --> ((f o x) ---> f(a)) sequentially)" (is "?lhs = ?rhs")

  3984 proof

  3985   assume ?rhs thus ?lhs using continuous_within_sequentially[of _ s f] unfolding continuous_on_eq_continuous_within by auto

  3986 next

  3987   assume ?lhs thus ?rhs unfolding continuous_on_eq_continuous_within using continuous_within_sequentially[of _ s f] by auto

  3988 qed

  3989

  3990 lemma uniformly_continuous_on_sequentially:

  3991   "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>

  3992                     ((\<lambda>n. dist (x n) (y n)) ---> 0) sequentially

  3993                     \<longrightarrow> ((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially)" (is "?lhs = ?rhs")

  3994 proof

  3995   assume ?lhs

  3996   { 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"

  3997     { fix e::real assume "e>0"

  3998       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"

  3999         using ?lhs[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto

  4000       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

  4001       { fix n assume "n\<ge>N"

  4002         hence "dist (f (x n)) (f (y n)) < e"

  4003           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

  4004           unfolding dist_commute by simp  }

  4005       hence "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"  by auto  }

  4006     hence "((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially" unfolding LIMSEQ_def and dist_real_def by auto  }

  4007   thus ?rhs by auto

  4008 next

  4009   assume ?rhs

  4010   { assume "\<not> ?lhs"

  4011     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

  4012     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"

  4013       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

  4014       by (auto simp add: dist_commute)

  4015     def x \<equiv> "\<lambda>n::nat. fst (fa (inverse (real n + 1)))"

  4016     def y \<equiv> "\<lambda>n::nat. snd (fa (inverse (real n + 1)))"

  4017     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"

  4018       unfolding x_def and y_def using fa by auto

  4019     { fix e::real assume "e>0"

  4020       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

  4021       { fix n::nat assume "n\<ge>N"

  4022         hence "inverse (real n + 1) < inverse (real N)" using real_of_nat_ge_zero and N\<noteq>0 by auto

  4023         also have "\<dots> < e" using N by auto

  4024         finally have "inverse (real n + 1) < e" by auto

  4025         hence "dist (x n) (y n) < e" using xy0[THEN spec[where x=n]] by auto  }

  4026       hence "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto  }

  4027     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

  4028     hence False using fxy and e>0 by auto  }

  4029   thus ?lhs unfolding uniformly_continuous_on_def by blast

  4030 qed

  4031

  4032 text{* The usual transformation theorems. *}

  4033

  4034 lemma continuous_transform_within:

  4035   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  4036   assumes "0 < d" "x \<in> s" "\<forall>x' \<in> s. dist x' x < d --> f x' = g x'"

  4037           "continuous (at x within s) f"

  4038   shows "continuous (at x within s) g"

  4039 unfolding continuous_within

  4040 proof (rule Lim_transform_within)

  4041   show "0 < d" by fact

  4042   show "\<forall>x'\<in>s. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"

  4043     using assms(3) by auto

  4044   have "f x = g x"

  4045     using assms(1,2,3) by auto

  4046   thus "(f ---> g x) (at x within s)"

  4047     using assms(4) unfolding continuous_within by simp

  4048 qed

  4049

  4050 lemma continuous_transform_at:

  4051   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  4052   assumes "0 < d" "\<forall>x'. dist x' x < d --> f x' = g x'"

  4053           "continuous (at x) f"

  4054   shows "continuous (at x) g"

  4055   using continuous_transform_within [of d x UNIV f g] assms by simp

  4056

  4057 subsubsection {* Structural rules for pointwise continuity *}

  4058

  4059 ML {*

  4060

  4061 structure Continuous_Intros = Named_Thms

  4062 (

  4063   val name = @{binding continuous_intros}

  4064   val description = "Structural introduction rules for pointwise continuity"

  4065 )

  4066

  4067 *}

  4068

  4069 setup Continuous_Intros.setup

  4070

  4071 lemma continuous_within_id[continuous_intros]: "continuous (at a within s) (\<lambda>x. x)"

  4072   unfolding continuous_within by (rule tendsto_ident_at_within)

  4073

  4074 lemma continuous_at_id[continuous_intros]: "continuous (at a) (\<lambda>x. x)"

  4075   unfolding continuous_at by (rule tendsto_ident_at)

  4076

  4077 lemma continuous_const[continuous_intros]: "continuous F (\<lambda>x. c)"

  4078   unfolding continuous_def by (rule tendsto_const)

  4079

  4080 lemma continuous_fst[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. fst (f x))"

  4081   unfolding continuous_def by (rule tendsto_fst)

  4082

  4083 lemma continuous_snd[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. snd (f x))"

  4084   unfolding continuous_def by (rule tendsto_snd)

  4085

  4086 lemma continuous_Pair[continuous_intros]: "continuous F f \<Longrightarrow> continuous F g \<Longrightarrow> continuous F (\<lambda>x. (f x, g x))"

  4087   unfolding continuous_def by (rule tendsto_Pair)

  4088

  4089 lemma continuous_dist[continuous_intros]:

  4090   assumes "continuous F f" and "continuous F g"

  4091   shows "continuous F (\<lambda>x. dist (f x) (g x))"

  4092   using assms unfolding continuous_def by (rule tendsto_dist)

  4093

  4094 lemma continuous_infdist[continuous_intros]:

  4095   assumes "continuous F f"

  4096   shows "continuous F (\<lambda>x. infdist (f x) A)"

  4097   using assms unfolding continuous_def by (rule tendsto_infdist)

  4098

  4099 lemma continuous_norm[continuous_intros]:

  4100   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. norm (f x))"

  4101   unfolding continuous_def by (rule tendsto_norm)

  4102

  4103 lemma continuous_infnorm[continuous_intros]:

  4104   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. infnorm (f x))"

  4105   unfolding continuous_def by (rule tendsto_infnorm)

  4106

  4107 lemma continuous_add[continuous_intros]:

  4108   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4109   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x + g x)"

  4110   unfolding continuous_def by (rule tendsto_add)

  4111

  4112 lemma continuous_minus[continuous_intros]:

  4113   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4114   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. - f x)"

  4115   unfolding continuous_def by (rule tendsto_minus)

  4116

  4117 lemma continuous_diff[continuous_intros]:

  4118   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4119   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x - g x)"

  4120   unfolding continuous_def by (rule tendsto_diff)

  4121

  4122 lemma continuous_scaleR[continuous_intros]:

  4123   fixes g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4124   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x *\<^sub>R g x)"

  4125   unfolding continuous_def by (rule tendsto_scaleR)

  4126

  4127 lemma continuous_mult[continuous_intros]:

  4128   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_algebra"

  4129   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x * g x)"

  4130   unfolding continuous_def by (rule tendsto_mult)

  4131

  4132 lemma continuous_inner[continuous_intros]:

  4133   assumes "continuous F f" and "continuous F g"

  4134   shows "continuous F (\<lambda>x. inner (f x) (g x))"

  4135   using assms unfolding continuous_def by (rule tendsto_inner)

  4136

  4137 lemma continuous_inverse[continuous_intros]:

  4138   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"

  4139   assumes "continuous F f" and "f (netlimit F) \<noteq> 0"

  4140   shows "continuous F (\<lambda>x. inverse (f x))"

  4141   using assms unfolding continuous_def by (rule tendsto_inverse)

  4142

  4143 lemma continuous_at_within_inverse[continuous_intros]:

  4144   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"

  4145   assumes "continuous (at a within s) f" and "f a \<noteq> 0"

  4146   shows "continuous (at a within s) (\<lambda>x. inverse (f x))"

  4147   using assms unfolding continuous_within by (rule tendsto_inverse)

  4148

  4149 lemma continuous_at_inverse[continuous_intros]:

  4150   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"

  4151   assumes "continuous (at a) f" and "f a \<noteq> 0"

  4152   shows "continuous (at a) (\<lambda>x. inverse (f x))"

  4153   using assms unfolding continuous_at by (rule tendsto_inverse)

  4154

  4155 subsubsection {* Structural rules for setwise continuity *}

  4156

  4157 ML {*

  4158

  4159 structure Continuous_On_Intros = Named_Thms

  4160 (

  4161   val name = @{binding continuous_on_intros}

  4162   val description = "Structural introduction rules for setwise continuity"

  4163 )

  4164

  4165 *}

  4166

  4167 setup Continuous_On_Intros.setup

  4168

  4169 lemma continuous_on_id[continuous_on_intros]: "continuous_on s (\<lambda>x. x)"

  4170   unfolding continuous_on_def by (fast intro: tendsto_ident_at_within)

  4171

  4172 lemma continuous_on_const[continuous_on_intros]: "continuous_on s (\<lambda>x. c)"

  4173   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4174

  4175 lemma continuous_on_norm[continuous_on_intros]:

  4176   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. norm (f x))"

  4177   unfolding continuous_on_def by (fast intro: tendsto_norm)

  4178

  4179 lemma continuous_on_infnorm[continuous_on_intros]:

  4180   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. infnorm (f x))"

  4181   unfolding continuous_on by (fast intro: tendsto_infnorm)

  4182

  4183 lemma continuous_on_minus[continuous_on_intros]:

  4184   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4185   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. - f x)"

  4186   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4187

  4188 lemma continuous_on_add[continuous_on_intros]:

  4189   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4190   shows "continuous_on s f \<Longrightarrow> continuous_on s g

  4191            \<Longrightarrow> continuous_on s (\<lambda>x. f x + g x)"

  4192   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4193

  4194 lemma continuous_on_diff[continuous_on_intros]:

  4195   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4196   shows "continuous_on s f \<Longrightarrow> continuous_on s g

  4197            \<Longrightarrow> continuous_on s (\<lambda>x. f x - g x)"

  4198   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4199

  4200 lemma (in bounded_linear) continuous_on[continuous_on_intros]:

  4201   "continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. f (g x))"

  4202   unfolding continuous_on_def by (fast intro: tendsto)

  4203

  4204 lemma (in bounded_bilinear) continuous_on[continuous_on_intros]:

  4205   "\<lbrakk>continuous_on s f; continuous_on s g\<rbrakk> \<Longrightarrow> continuous_on s (\<lambda>x. f x ** g x)"

  4206   unfolding continuous_on_def by (fast intro: tendsto)

  4207

  4208 lemma continuous_on_scaleR[continuous_on_intros]:

  4209   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4210   assumes "continuous_on s f" and "continuous_on s g"

  4211   shows "continuous_on s (\<lambda>x. f x *\<^sub>R g x)"

  4212   using bounded_bilinear_scaleR assms

  4213   by (rule bounded_bilinear.continuous_on)

  4214

  4215 lemma continuous_on_mult[continuous_on_intros]:

  4216   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra"

  4217   assumes "continuous_on s f" and "continuous_on s g"

  4218   shows "continuous_on s (\<lambda>x. f x * g x)"

  4219   using bounded_bilinear_mult assms

  4220   by (rule bounded_bilinear.continuous_on)

  4221

  4222 lemma continuous_on_inner[continuous_on_intros]:

  4223   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_inner"

  4224   assumes "continuous_on s f" and "continuous_on s g"

  4225   shows "continuous_on s (\<lambda>x. inner (f x) (g x))"

  4226   using bounded_bilinear_inner assms

  4227   by (rule bounded_bilinear.continuous_on)

  4228

  4229 lemma continuous_on_inverse[continuous_on_intros]:

  4230   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_div_algebra"

  4231   assumes "continuous_on s f" and "\<forall>x\<in>s. f x \<noteq> 0"

  4232   shows "continuous_on s (\<lambda>x. inverse (f x))"

  4233   using assms unfolding continuous_on by (fast intro: tendsto_inverse)

  4234

  4235 subsubsection {* Structural rules for uniform continuity *}

  4236

  4237 lemma uniformly_continuous_on_id[continuous_on_intros]:

  4238   shows "uniformly_continuous_on s (\<lambda>x. x)"

  4239   unfolding uniformly_continuous_on_def by auto

  4240

  4241 lemma uniformly_continuous_on_const[continuous_on_intros]:

  4242   shows "uniformly_continuous_on s (\<lambda>x. c)"

  4243   unfolding uniformly_continuous_on_def by simp

  4244

  4245 lemma uniformly_continuous_on_dist[continuous_on_intros]:

  4246   fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"

  4247   assumes "uniformly_continuous_on s f"

  4248   assumes "uniformly_continuous_on s g"

  4249   shows "uniformly_continuous_on s (\<lambda>x. dist (f x) (g x))"

  4250 proof -

  4251   { fix a b c d :: 'b have "\<bar>dist a b - dist c d\<bar> \<le> dist a c + dist b d"

  4252       using dist_triangle2 [of a b c] dist_triangle2 [of b c d]

  4253       using dist_triangle3 [of c d a] dist_triangle [of a d b]

  4254       by arith

  4255   } note le = this

  4256   { fix x y

  4257     assume f: "(\<lambda>n. dist (f (x n)) (f (y n))) ----> 0"

  4258     assume g: "(\<lambda>n. dist (g (x n)) (g (y n))) ----> 0"

  4259     have "(\<lambda>n. \<bar>dist (f (x n)) (g (x n)) - dist (f (y n)) (g (y n))\<bar>) ----> 0"

  4260       by (rule Lim_transform_bound [OF _ tendsto_add_zero [OF f g]],

  4261         simp add: le)

  4262   }

  4263   thus ?thesis using assms unfolding uniformly_continuous_on_sequentially

  4264     unfolding dist_real_def by simp

  4265 qed

  4266

  4267 lemma uniformly_continuous_on_norm[continuous_on_intros]:

  4268   assumes "uniformly_continuous_on s f"

  4269   shows "uniformly_continuous_on s (\<lambda>x. norm (f x))"

  4270   unfolding norm_conv_dist using assms

  4271   by (intro uniformly_continuous_on_dist uniformly_continuous_on_const)

  4272

  4273 lemma (in bounded_linear) uniformly_continuous_on[continuous_on_intros]:

  4274   assumes "uniformly_continuous_on s g"

  4275   shows "uniformly_continuous_on s (\<lambda>x. f (g x))"

  4276   using assms unfolding uniformly_continuous_on_sequentially

  4277   unfolding dist_norm tendsto_norm_zero_iff diff[symmetric]

  4278   by (auto intro: tendsto_zero)

  4279

  4280 lemma uniformly_continuous_on_cmul[continuous_on_intros]:

  4281   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4282   assumes "uniformly_continuous_on s f"

  4283   shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"

  4284   using bounded_linear_scaleR_right assms

  4285   by (rule bounded_linear.uniformly_continuous_on)

  4286

  4287 lemma dist_minus:

  4288   fixes x y :: "'a::real_normed_vector"

  4289   shows "dist (- x) (- y) = dist x y"

  4290   unfolding dist_norm minus_diff_minus norm_minus_cancel ..

  4291

  4292 lemma uniformly_continuous_on_minus[continuous_on_intros]:

  4293   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4294   shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. - f x)"

  4295   unfolding uniformly_continuous_on_def dist_minus .

  4296

  4297 lemma uniformly_continuous_on_add[continuous_on_intros]:

  4298   fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4299   assumes "uniformly_continuous_on s f"

  4300   assumes "uniformly_continuous_on s g"

  4301   shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"

  4302   using assms unfolding uniformly_continuous_on_sequentially

  4303   unfolding dist_norm tendsto_norm_zero_iff add_diff_add

  4304   by (auto intro: tendsto_add_zero)

  4305

  4306 lemma uniformly_continuous_on_diff[continuous_on_intros]:

  4307   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4308   assumes "uniformly_continuous_on s f" and "uniformly_continuous_on s g"

  4309   shows "uniformly_continuous_on s (\<lambda>x. f x - g x)"

  4310   unfolding ab_diff_minus using assms

  4311   by (intro uniformly_continuous_on_add uniformly_continuous_on_minus)

  4312

  4313 text{* Continuity of all kinds is preserved under composition. *}

  4314

  4315 lemma continuous_within_topological:

  4316   "continuous (at x within s) f \<longleftrightarrow>

  4317     (\<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>

  4318       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"

  4319 unfolding continuous_within

  4320 unfolding tendsto_def Limits.eventually_within eventually_at_topological

  4321 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto

  4322

  4323 lemma continuous_within_compose[continuous_intros]:

  4324   assumes "continuous (at x within s) f"

  4325   assumes "continuous (at (f x) within f  s) g"

  4326   shows "continuous (at x within s) (g o f)"

  4327 using assms unfolding continuous_within_topological by simp metis

  4328

  4329 lemma continuous_at_compose[continuous_intros]:

  4330   assumes "continuous (at x) f" and "continuous (at (f x)) g"

  4331   shows "continuous (at x) (g o f)"

  4332 proof-

  4333   have "continuous (at (f x) within range f) g" using assms(2)

  4334     using continuous_within_subset[of "f x" UNIV g "range f"] by simp

  4335   thus ?thesis using assms(1)

  4336     using continuous_within_compose[of x UNIV f g] by simp

  4337 qed

  4338

  4339 lemma continuous_on_compose[continuous_on_intros]:

  4340   "continuous_on s f \<Longrightarrow> continuous_on (f  s) g \<Longrightarrow> continuous_on s (g o f)"

  4341   unfolding continuous_on_topological by simp metis

  4342

  4343 lemma uniformly_continuous_on_compose[continuous_on_intros]:

  4344   assumes "uniformly_continuous_on s f"  "uniformly_continuous_on (f  s) g"

  4345   shows "uniformly_continuous_on s (g o f)"

  4346 proof-

  4347   { fix e::real assume "e>0"

  4348     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

  4349     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

  4350     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  }

  4351   thus ?thesis using assms unfolding uniformly_continuous_on_def by auto

  4352 qed

  4353

  4354 text{* Continuity in terms of open preimages. *}

  4355

  4356 lemma continuous_at_open:

  4357   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)))"

  4358 unfolding continuous_within_topological [of x UNIV f, unfolded within_UNIV]

  4359 unfolding imp_conjL by (intro all_cong imp_cong ex_cong conj_cong refl) auto

  4360

  4361 lemma continuous_imp_tendsto:

  4362   assumes "continuous (at x0) f" and "x ----> x0"

  4363   shows "(f \<circ> x) ----> (f x0)"

  4364 proof (rule topological_tendstoI)

  4365   fix S

  4366   assume "open S" "f x0 \<in> S"

  4367   then obtain T where T_def: "open T" "x0 \<in> T" "\<forall>x\<in>T. f x \<in> S"

  4368      using assms continuous_at_open by metis

  4369   then have "eventually (\<lambda>n. x n \<in> T) sequentially"

  4370     using assms T_def by (auto simp: tendsto_def)

  4371   then show "eventually (\<lambda>n. (f \<circ> x) n \<in> S) sequentially"

  4372     using T_def by (auto elim!: eventually_elim1)

  4373 qed

  4374

  4375 lemma continuous_on_open:

  4376   shows "continuous_on s f \<longleftrightarrow>

  4377         (\<forall>t. openin (subtopology euclidean (f  s)) t

  4378             --> openin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")

  4379 proof (safe)

  4380   fix t :: "'b set"

  4381   assume 1: "continuous_on s f"

  4382   assume 2: "openin (subtopology euclidean (f  s)) t"

  4383   from 2 obtain B where B: "open B" and t: "t = f  s \<inter> B"

  4384     unfolding openin_open by auto

  4385   def U == "\<Union>{A. open A \<and> (\<forall>x\<in>s. x \<in> A \<longrightarrow> f x \<in> B)}"

  4386   have "open U" unfolding U_def by (simp add: open_Union)

  4387   moreover have "\<forall>x\<in>s. x \<in> U \<longleftrightarrow> f x \<in> t"

  4388   proof (intro ballI iffI)

  4389     fix x assume "x \<in> s" and "x \<in> U" thus "f x \<in> t"

  4390       unfolding U_def t by auto

  4391   next

  4392     fix x assume "x \<in> s" and "f x \<in> t"

  4393     hence "x \<in> s" and "f x \<in> B"

  4394       unfolding t by auto

  4395     with 1 B obtain A where "open A" "x \<in> A" "\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B"

  4396       unfolding t continuous_on_topological by metis

  4397     then show "x \<in> U"

  4398       unfolding U_def by auto

  4399   qed

  4400   ultimately have "open U \<and> {x \<in> s. f x \<in> t} = s \<inter> U" by auto

  4401   then show "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"

  4402     unfolding openin_open by fast

  4403 next

  4404   assume "?rhs" show "continuous_on s f"

  4405   unfolding continuous_on_topological

  4406   proof (clarify)

  4407     fix x and B assume "x \<in> s" and "open B" and "f x \<in> B"

  4408     have "openin (subtopology euclidean (f  s)) (f  s \<inter> B)"

  4409       unfolding openin_open using open B by auto

  4410     then have "openin (subtopology euclidean s) {x \<in> s. f x \<in> f  s \<inter> B}"

  4411       using ?rhs by fast

  4412     then show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"

  4413       unfolding openin_open using x \<in> s and f x \<in> B by auto

  4414   qed

  4415 qed

  4416

  4417 text {* Similarly in terms of closed sets. *}

  4418

  4419 lemma continuous_on_closed:

  4420   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")

  4421 proof

  4422   assume ?lhs

  4423   { fix t

  4424     have *:"s - {x \<in> s. f x \<in> f  s - t} = {x \<in> s. f x \<in> t}" by auto

  4425     have **:"f  s - (f  s - (f  s - t)) = f  s - t" by auto

  4426     assume as:"closedin (subtopology euclidean (f  s)) t"

  4427     hence "closedin (subtopology euclidean (f  s)) (f  s - (f  s - t))" unfolding closedin_def topspace_euclidean_subtopology unfolding ** by auto

  4428     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"]]

  4429       unfolding openin_closedin_eq topspace_euclidean_subtopology unfolding * by auto  }

  4430   thus ?rhs by auto

  4431 next

  4432   assume ?rhs

  4433   { fix t

  4434     have *:"s - {x \<in> s. f x \<in> f  s - t} = {x \<in> s. f x \<in> t}" by auto

  4435     assume as:"openin (subtopology euclidean (f  s)) t"

  4436     hence "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using ?rhs[THEN spec[where x="(f  s) - t"]]

  4437       unfolding openin_closedin_eq topspace_euclidean_subtopology *[THEN sym] closedin_subtopology by auto }

  4438   thus ?lhs unfolding continuous_on_open by auto

  4439 qed

  4440

  4441 text {* Half-global and completely global cases. *}

  4442

  4443 lemma continuous_open_in_preimage:

  4444   assumes "continuous_on s f"  "open t"

  4445   shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"

  4446 proof-

  4447   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

  4448   have "openin (subtopology euclidean (f  s)) (t \<inter> f  s)"

  4449     using openin_open_Int[of t "f  s", OF assms(2)] unfolding openin_open by auto

  4450   thus ?thesis using assms(1)[unfolded continuous_on_open, THEN spec[where x="t \<inter> f  s"]] using * by auto

  4451 qed

  4452

  4453 lemma continuous_closed_in_preimage:

  4454   assumes "continuous_on s f"  "closed t"

  4455   shows "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}"

  4456 proof-

  4457   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

  4458   have "closedin (subtopology euclidean (f  s)) (t \<inter> f  s)"

  4459     using closedin_closed_Int[of t "f  s", OF assms(2)] unfolding Int_commute by auto

  4460   thus ?thesis

  4461     using assms(1)[unfolded continuous_on_closed, THEN spec[where x="t \<inter> f  s"]] using * by auto

  4462 qed

  4463

  4464 lemma continuous_open_preimage:

  4465   assumes "continuous_on s f" "open s" "open t"

  4466   shows "open {x \<in> s. f x \<in> t}"

  4467 proof-

  4468   obtain T where T: "open T" "{x \<in> s. f x \<in> t} = s \<inter> T"

  4469     using continuous_open_in_preimage[OF assms(1,3)] unfolding openin_open by auto

  4470   thus ?thesis using open_Int[of s T, OF assms(2)] by auto

  4471 qed

  4472

  4473 lemma continuous_closed_preimage:

  4474   assumes "continuous_on s f" "closed s" "closed t"

  4475   shows "closed {x \<in> s. f x \<in> t}"

  4476 proof-

  4477   obtain T where T: "closed T" "{x \<in> s. f x \<in> t} = s \<inter> T"

  4478     using continuous_closed_in_preimage[OF assms(1,3)] unfolding closedin_closed by auto

  4479   thus ?thesis using closed_Int[of s T, OF assms(2)] by auto

  4480 qed

  4481

  4482 lemma continuous_open_preimage_univ:

  4483   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open {x. f x \<in> s}"

  4484   using continuous_open_preimage[of UNIV f s] open_UNIV continuous_at_imp_continuous_on by auto

  4485

  4486 lemma continuous_closed_preimage_univ:

  4487   shows "(\<forall>x. continuous (at x) f) \<Longrightarrow> closed s ==> closed {x. f x \<in> s}"

  4488   using continuous_closed_preimage[of UNIV f s] closed_UNIV continuous_at_imp_continuous_on by auto

  4489

  4490 lemma continuous_open_vimage:

  4491   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open (f - s)"

  4492   unfolding vimage_def by (rule continuous_open_preimage_univ)

  4493

  4494 lemma continuous_closed_vimage:

  4495   shows "\<forall>x. continuous (at x) f \<Longrightarrow> closed s \<Longrightarrow> closed (f - s)"

  4496   unfolding vimage_def by (rule continuous_closed_preimage_univ)

  4497

  4498 lemma interior_image_subset:

  4499   assumes "\<forall>x. continuous (at x) f" "inj f"

  4500   shows "interior (f  s) \<subseteq> f  (interior s)"

  4501 proof

  4502   fix x assume "x \<in> interior (f  s)"

  4503   then obtain T where as: "open T" "x \<in> T" "T \<subseteq> f  s" ..

  4504   hence "x \<in> f  s" by auto

  4505   then obtain y where y: "y \<in> s" "x = f y" by auto

  4506   have "open (vimage f T)"

  4507     using assms(1) open T by (rule continuous_open_vimage)

  4508   moreover have "y \<in> vimage f T"

  4509     using x = f y x \<in> T by simp

  4510   moreover have "vimage f T \<subseteq> s"

  4511     using T \<subseteq> image f s inj f unfolding inj_on_def subset_eq by auto

  4512   ultimately have "y \<in> interior s" ..

  4513   with x = f y show "x \<in> f  interior s" ..

  4514 qed

  4515

  4516 text {* Equality of continuous functions on closure and related results. *}

  4517

  4518 lemma continuous_closed_in_preimage_constant:

  4519   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4520   shows "continuous_on s f ==> closedin (subtopology euclidean s) {x \<in> s. f x = a}"

  4521   using continuous_closed_in_preimage[of s f "{a}"] by auto

  4522

  4523 lemma continuous_closed_preimage_constant:

  4524   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4525   shows "continuous_on s f \<Longrightarrow> closed s ==> closed {x \<in> s. f x = a}"

  4526   using continuous_closed_preimage[of s f "{a}"] by auto

  4527

  4528 lemma continuous_constant_on_closure:

  4529   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4530   assumes "continuous_on (closure s) f"

  4531           "\<forall>x \<in> s. f x = a"

  4532   shows "\<forall>x \<in> (closure s). f x = a"

  4533     using continuous_closed_preimage_constant[of "closure s" f a]

  4534     assms closure_minimal[of s "{x \<in> closure s. f x = a}"] closure_subset unfolding subset_eq by auto

  4535

  4536 lemma image_closure_subset:

  4537   assumes "continuous_on (closure s) f"  "closed t"  "(f  s) \<subseteq> t"

  4538   shows "f  (closure s) \<subseteq> t"

  4539 proof-

  4540   have "s \<subseteq> {x \<in> closure s. f x \<in> t}" using assms(3) closure_subset by auto

  4541   moreover have "closed {x \<in> closure s. f x \<in> t}"

  4542     using continuous_closed_preimage[OF assms(1)] and assms(2) by auto

  4543   ultimately have "closure s = {x \<in> closure s . f x \<in> t}"

  4544     using closure_minimal[of s "{x \<in> closure s. f x \<in> t}"] by auto

  4545   thus ?thesis by auto

  4546 qed

  4547

  4548 lemma continuous_on_closure_norm_le:

  4549   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4550   assumes "continuous_on (closure s) f"  "\<forall>y \<in> s. norm(f y) \<le> b"  "x \<in> (closure s)"

  4551   shows "norm(f x) \<le> b"

  4552 proof-

  4553   have *:"f  s \<subseteq> cball 0 b" using assms(2)[unfolded mem_cball_0[THEN sym]] by auto

  4554   show ?thesis

  4555     using image_closure_subset[OF assms(1) closed_cball[of 0 b] *] assms(3)

  4556     unfolding subset_eq apply(erule_tac x="f x" in ballE) by (auto simp add: dist_norm)

  4557 qed

  4558

  4559 text {* Making a continuous function avoid some value in a neighbourhood. *}

  4560

  4561 lemma continuous_within_avoid:

  4562   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4563   assumes "continuous (at x within s) f" and "f x \<noteq> a"

  4564   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"

  4565 proof-

  4566   obtain U where "open U" and "f x \<in> U" and "a \<notin> U"

  4567     using t1_space [OF f x \<noteq> a] by fast

  4568   have "(f ---> f x) (at x within s)"

  4569     using assms(1) by (simp add: continuous_within)

  4570   hence "eventually (\<lambda>y. f y \<in> U) (at x within s)"

  4571     using open U and f x \<in> U

  4572     unfolding tendsto_def by fast

  4573   hence "eventually (\<lambda>y. f y \<noteq> a) (at x within s)"

  4574     using a \<notin> U by (fast elim: eventually_mono [rotated])

  4575   thus ?thesis

  4576     unfolding Limits.eventually_within Metric_Spaces.eventually_at

  4577     by (rule ex_forward, cut_tac f x \<noteq> a, auto simp: dist_commute)

  4578 qed

  4579

  4580 lemma continuous_at_avoid:

  4581   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4582   assumes "continuous (at x) f" and "f x \<noteq> a"

  4583   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"

  4584   using assms continuous_within_avoid[of x UNIV f a] by simp

  4585

  4586 lemma continuous_on_avoid:

  4587   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4588   assumes "continuous_on s f"  "x \<in> s"  "f x \<noteq> a"

  4589   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"

  4590 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

  4591

  4592 lemma continuous_on_open_avoid:

  4593   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4594   assumes "continuous_on s f"  "open s"  "x \<in> s"  "f x \<noteq> a"

  4595   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"

  4596 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

  4597

  4598 text {* Proving a function is constant by proving open-ness of level set. *}

  4599

  4600 lemma continuous_levelset_open_in_cases:

  4601   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4602   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>

  4603         openin (subtopology euclidean s) {x \<in> s. f x = a}

  4604         ==> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"

  4605 unfolding connected_clopen using continuous_closed_in_preimage_constant by auto

  4606

  4607 lemma continuous_levelset_open_in:

  4608   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4609   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>

  4610         openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>

  4611         (\<exists>x \<in> s. f x = a)  ==> (\<forall>x \<in> s. f x = a)"

  4612 using continuous_levelset_open_in_cases[of s f ]

  4613 by meson

  4614

  4615 lemma continuous_levelset_open:

  4616   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4617   assumes "connected s"  "continuous_on s f"  "open {x \<in> s. f x = a}"  "\<exists>x \<in> s.  f x = a"

  4618   shows "\<forall>x \<in> s. f x = a"

  4619 using continuous_levelset_open_in[OF assms(1,2), of a, unfolded openin_open] using assms (3,4) by fast

  4620

  4621 text {* Some arithmetical combinations (more to prove). *}

  4622

  4623 lemma open_scaling[intro]:

  4624   fixes s :: "'a::real_normed_vector set"

  4625   assumes "c \<noteq> 0"  "open s"

  4626   shows "open((\<lambda>x. c *\<^sub>R x)  s)"

  4627 proof-

  4628   { fix x assume "x \<in> s"

  4629     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

  4630     have "e * abs c > 0" using assms(1)[unfolded zero_less_abs_iff[THEN sym]] using mult_pos_pos[OF e>0] by auto

  4631     moreover

  4632     { fix y assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"

  4633       hence "norm ((1 / c) *\<^sub>R y - x) < e" unfolding dist_norm

  4634         using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)

  4635           assms(1)[unfolded zero_less_abs_iff[THEN sym]] by (simp del:zero_less_abs_iff)

  4636       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  }

  4637     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  }

  4638   thus ?thesis unfolding open_dist by auto

  4639 qed

  4640

  4641 lemma minus_image_eq_vimage:

  4642   fixes A :: "'a::ab_group_add set"

  4643   shows "(\<lambda>x. - x)  A = (\<lambda>x. - x) - A"

  4644   by (auto intro!: image_eqI [where f="\<lambda>x. - x"])

  4645

  4646 lemma open_negations:

  4647   fixes s :: "'a::real_normed_vector set"

  4648   shows "open s ==> open ((\<lambda> x. -x)  s)"

  4649   unfolding scaleR_minus1_left [symmetric]

  4650   by (rule open_scaling, auto)

  4651

  4652 lemma open_translation:

  4653   fixes s :: "'a::real_normed_vector set"

  4654   assumes "open s"  shows "open((\<lambda>x. a + x)  s)"

  4655 proof-

  4656   { fix x have "continuous (at x) (\<lambda>x. x - a)"

  4657       by (intro continuous_diff continuous_at_id continuous_const) }

  4658   moreover have "{x. x - a \<in> s} = op + a  s" by force

  4659   ultimately show ?thesis using continuous_open_preimage_univ[of "\<lambda>x. x - a" s] using assms by auto

  4660 qed

  4661

  4662 lemma open_affinity:

  4663   fixes s :: "'a::real_normed_vector set"

  4664   assumes "open s"  "c \<noteq> 0"

  4665   shows "open ((\<lambda>x. a + c *\<^sub>R x)  s)"

  4666 proof-

  4667   have *:"(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)" unfolding o_def ..

  4668   have "op + a  op *\<^sub>R c  s = (op + a \<circ> op *\<^sub>R c)  s" by auto

  4669   thus ?thesis using assms open_translation[of "op *\<^sub>R c  s" a] unfolding * by auto

  4670 qed

  4671

  4672 lemma interior_translation:

  4673   fixes s :: "'a::real_normed_vector set"

  4674   shows "interior ((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (interior s)"

  4675 proof (rule set_eqI, rule)

  4676   fix x assume "x \<in> interior (op + a  s)"

  4677   then obtain e where "e>0" and e:"ball x e \<subseteq> op + a  s" unfolding mem_interior by auto

  4678   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

  4679   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

  4680 next

  4681   fix x assume "x \<in> op + a  interior s"

  4682   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

  4683   { fix z have *:"a + y - z = y + a - z" by auto

  4684     assume "z\<in>ball x e"

  4685     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

  4686     hence "z \<in> op + a  s" unfolding image_iff by(auto intro!: bexI[where x="z - a"])  }

  4687   hence "ball x e \<subseteq> op + a  s" unfolding subset_eq by auto

  4688   thus "x \<in> interior (op + a  s)" unfolding mem_interior using e>0 by auto

  4689 qed

  4690

  4691 text {* Topological properties of linear functions. *}

  4692

  4693 lemma linear_lim_0:

  4694   assumes "bounded_linear f" shows "(f ---> 0) (at (0))"

  4695 proof-

  4696   interpret f: bounded_linear f by fact

  4697   have "(f ---> f 0) (at 0)"

  4698     using tendsto_ident_at by (rule f.tendsto)

  4699   thus ?thesis unfolding f.zero .

  4700 qed

  4701

  4702 lemma linear_continuous_at:

  4703   assumes "bounded_linear f"  shows "continuous (at a) f"

  4704   unfolding continuous_at using assms

  4705   apply (rule bounded_linear.tendsto)

  4706   apply (rule tendsto_ident_at)

  4707   done

  4708

  4709 lemma linear_continuous_within:

  4710   shows "bounded_linear f ==> continuous (at x within s) f"

  4711   using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto

  4712

  4713 lemma linear_continuous_on:

  4714   shows "bounded_linear f ==> continuous_on s f"

  4715   using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto

  4716

  4717 text {* Also bilinear functions, in composition form. *}

  4718

  4719 lemma bilinear_continuous_at_compose:

  4720   shows "continuous (at x) f \<Longrightarrow> continuous (at x) g \<Longrightarrow> bounded_bilinear h

  4721         ==> continuous (at x) (\<lambda>x. h (f x) (g x))"

  4722   unfolding continuous_at using Lim_bilinear[of f "f x" "(at x)" g "g x" h] by auto

  4723

  4724 lemma bilinear_continuous_within_compose:

  4725   shows "continuous (at x within s) f \<Longrightarrow> continuous (at x within s) g \<Longrightarrow> bounded_bilinear h

  4726         ==> continuous (at x within s) (\<lambda>x. h (f x) (g x))"

  4727   unfolding continuous_within using Lim_bilinear[of f "f x"] by auto

  4728

  4729 lemma bilinear_continuous_on_compose:

  4730   shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> bounded_bilinear h

  4731              ==> continuous_on s (\<lambda>x. h (f x) (g x))"

  4732   unfolding continuous_on_def

  4733   by (fast elim: bounded_bilinear.tendsto)

  4734

  4735 text {* Preservation of compactness and connectedness under continuous function. *}

  4736

  4737 lemma compact_eq_openin_cover:

  4738   "compact S \<longleftrightarrow>

  4739     (\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>

  4740       (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D))"

  4741 proof safe

  4742   fix C

  4743   assume "compact S" and "\<forall>c\<in>C. openin (subtopology euclidean S) c" and "S \<subseteq> \<Union>C"

  4744   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}"

  4745     unfolding openin_open by force+

  4746   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"

  4747     by (rule compactE)

  4748   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)"

  4749     by auto

  4750   thus "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..

  4751 next

  4752   assume 1: "\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>

  4753         (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D)"

  4754   show "compact S"

  4755   proof (rule compactI)

  4756     fix C

  4757     let ?C = "image (\<lambda>T. S \<inter> T) C"

  4758     assume "\<forall>t\<in>C. open t" and "S \<subseteq> \<Union>C"

  4759     hence "(\<forall>c\<in>?C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>?C"

  4760       unfolding openin_open by auto

  4761     with 1 obtain D where "D \<subseteq> ?C" and "finite D" and "S \<subseteq> \<Union>D"

  4762       by metis

  4763     let ?D = "inv_into C (\<lambda>T. S \<inter> T)  D"

  4764     have "?D \<subseteq> C \<and> finite ?D \<and> S \<subseteq> \<Union>?D"

  4765     proof (intro conjI)

  4766       from D \<subseteq> ?C show "?D \<subseteq> C"

  4767         by (fast intro: inv_into_into)

  4768       from finite D show "finite ?D"

  4769         by (rule finite_imageI)

  4770       from S \<subseteq> \<Union>D show "S \<subseteq> \<Union>?D"

  4771         apply (rule subset_trans)

  4772         apply clarsimp

  4773         apply (frule subsetD [OF D \<subseteq> ?C, THEN f_inv_into_f])

  4774         apply (erule rev_bexI, fast)

  4775         done

  4776     qed

  4777     thus "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..

  4778   qed

  4779 qed

  4780

  4781 lemma compact_continuous_image:

  4782   assumes "continuous_on s f" and "compact s"

  4783   shows "compact (f  s)"

  4784 using assms (* FIXME: long unstructured proof *)

  4785 unfolding continuous_on_open

  4786 unfolding compact_eq_openin_cover

  4787 apply clarify

  4788 apply (drule_tac x="image (\<lambda>t. {x \<in> s. f x \<in> t}) C" in spec)

  4789 apply (drule mp)

  4790 apply (rule conjI)

  4791 apply simp

  4792 apply clarsimp

  4793 apply (drule subsetD)

  4794 apply (erule imageI)

  4795 apply fast

  4796 apply (erule thin_rl)

  4797 apply clarify

  4798 apply (rule_tac x="image (inv_into C (\<lambda>t. {x \<in> s. f x \<in> t})) D" in exI)

  4799 apply (intro conjI)

  4800 apply clarify

  4801 apply (rule inv_into_into)

  4802 apply (erule (1) subsetD)

  4803 apply (erule finite_imageI)

  4804 apply (clarsimp, rename_tac x)

  4805 apply (drule (1) subsetD, clarify)

  4806 apply (drule (1) subsetD, clarify)

  4807 apply (rule rev_bexI)

  4808 apply assumption

  4809 apply (subgoal_tac "{x \<in> s. f x \<in> t} \<in> (\<lambda>t. {x \<in> s. f x \<in> t})  C")

  4810 apply (drule f_inv_into_f)

  4811 apply fast

  4812 apply (erule imageI)

  4813 done

  4814

  4815 lemma connected_continuous_image:

  4816   assumes "continuous_on s f"  "connected s"

  4817   shows "connected(f  s)"

  4818 proof-

  4819   { fix T assume as: "T \<noteq> {}"  "T \<noteq> f  s"  "openin (subtopology euclidean (f  s)) T"  "closedin (subtopology euclidean (f  s)) T"

  4820     have "{x \<in> s. f x \<in> T} = {} \<or> {x \<in> s. f x \<in> T} = s"

  4821       using assms(1)[unfolded continuous_on_open, THEN spec[where x=T]]

  4822       using assms(1)[unfolded continuous_on_closed, THEN spec[where x=T]]

  4823       using assms(2)[unfolded connected_clopen, THEN spec[where x="{x \<in> s. f x \<in> T}"]] as(3,4) by auto

  4824     hence False using as(1,2)

  4825       using as(4)[unfolded closedin_def topspace_euclidean_subtopology] by auto }

  4826   thus ?thesis unfolding connected_clopen by auto

  4827 qed

  4828

  4829 text {* Continuity implies uniform continuity on a compact domain. *}

  4830

  4831 lemma compact_uniformly_continuous:

  4832   assumes f: "continuous_on s f" and s: "compact s"

  4833   shows "uniformly_continuous_on s f"

  4834   unfolding uniformly_continuous_on_def

  4835 proof (cases, safe)

  4836   fix e :: real assume "0 < e" "s \<noteq> {}"

  4837   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) } }"

  4838   let ?b = "(\<lambda>(y, d). ball y (d/2))"

  4839   have "(\<forall>r\<in>R. open (?b r))" "s \<subseteq> (\<Union>r\<in>R. ?b r)"

  4840   proof safe

  4841     fix y assume "y \<in> s"

  4842     from continuous_open_in_preimage[OF f open_ball]

  4843     obtain T where "open T" and T: "{x \<in> s. f x \<in> ball (f y) (e/2)} = T \<inter> s"

  4844       unfolding openin_subtopology open_openin by metis

  4845     then obtain d where "ball y d \<subseteq> T" "0 < d"

  4846       using 0 < e y \<in> s by (auto elim!: openE)

  4847     with T y \<in> s show "y \<in> (\<Union>r\<in>R. ?b r)"

  4848       by (intro UN_I[of "(y, d)"]) auto

  4849   qed auto

  4850   with s obtain D where D: "finite D" "D \<subseteq> R" "s \<subseteq> (\<Union>(y, d)\<in>D. ball y (d/2))"

  4851     by (rule compactE_image)

  4852   with s \<noteq> {} have [simp]: "\<And>x. x < Min (snd  D) \<longleftrightarrow> (\<forall>(y, d)\<in>D. x < d)"

  4853     by (subst Min_gr_iff) auto

  4854   show "\<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"

  4855   proof (rule, safe)

  4856     fix x x' assume in_s: "x' \<in> s" "x \<in> s"

  4857     with D obtain y d where x: "x \<in> ball y (d/2)" "(y, d) \<in> D"

  4858       by blast

  4859     moreover assume "dist x x' < Min (sndD) / 2"

  4860     ultimately have "dist y x' < d"

  4861       by (intro dist_double[where x=x and d=d]) (auto simp: dist_commute)

  4862     with D x in_s show  "dist (f x) (f x') < e"

  4863       by (intro dist_double[where x="f y" and d=e]) (auto simp: dist_commute subset_eq)

  4864   qed (insert D, auto)

  4865 qed auto

  4866

  4867 text{* Continuity of inverse function on compact domain. *}

  4868

  4869 lemma continuous_on_inv:

  4870   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"

  4871   assumes "continuous_on s f"  "compact s"  "\<forall>x \<in> s. g (f x) = x"

  4872   shows "continuous_on (f  s) g"

  4873 unfolding continuous_on_topological

  4874 proof (clarsimp simp add: assms(3))

  4875   fix x :: 'a and B :: "'a set"

  4876   assume "x \<in> s" and "open B" and "x \<in> B"

  4877   have 1: "\<forall>x\<in>s. f x \<in> f  (s - B) \<longleftrightarrow> x \<in> s - B"

  4878     using assms(3) by (auto, metis)

  4879   have "continuous_on (s - B) f"

  4880     using continuous_on s f Diff_subset

  4881     by (rule continuous_on_subset)

  4882   moreover have "compact (s - B)"

  4883     using open B and compact s

  4884     unfolding Diff_eq by (intro compact_inter_closed closed_Compl)

  4885   ultimately have "compact (f  (s - B))"

  4886     by (rule compact_continuous_image)

  4887   hence "closed (f  (s - B))"

  4888     by (rule compact_imp_closed)

  4889   hence "open (- f  (s - B))"

  4890     by (rule open_Compl)

  4891   moreover have "f x \<in> - f  (s - B)"

  4892     using x \<in> s and x \<in> B by (simp add: 1)

  4893   moreover have "\<forall>y\<in>s. f y \<in> - f  (s - B) \<longrightarrow> y \<in> B"

  4894     by (simp add: 1)

  4895   ultimately show "\<exists>A. open A \<and> f x \<in> A \<and> (\<forall>y\<in>s. f y \<in> A \<longrightarrow> y \<in> B)"

  4896     by fast

  4897 qed

  4898

  4899 text {* A uniformly convergent limit of continuous functions is continuous. *}

  4900

  4901 lemma continuous_uniform_limit:

  4902   fixes f :: "'a \<Rightarrow> 'b::metric_space \<Rightarrow> 'c::metric_space"

  4903   assumes "\<not> trivial_limit F"

  4904   assumes "eventually (\<lambda>n. continuous_on s (f n)) F"

  4905   assumes "\<forall>e>0. eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e) F"

  4906   shows "continuous_on s g"

  4907 proof-

  4908   { fix x and e::real assume "x\<in>s" "e>0"

  4909     have "eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e / 3) F"

  4910       using e>0 assms(3)[THEN spec[where x="e/3"]] by auto

  4911     from eventually_happens [OF eventually_conj [OF this assms(2)]]

  4912     obtain n where n:"\<forall>x\<in>s. dist (f n x) (g x) < e / 3"  "continuous_on s (f n)"

  4913       using assms(1) by blast

  4914     have "e / 3 > 0" using e>0 by auto

  4915     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"

  4916       using n(2)[unfolded continuous_on_iff, THEN bspec[where x=x], OF x\<in>s, THEN spec[where x="e/3"]] by blast

  4917     { fix y assume "y \<in> s" and "dist y x < d"

  4918       hence "dist (f n y) (f n x) < e / 3"

  4919         by (rule d [rule_format])

  4920       hence "dist (f n y) (g x) < 2 * e / 3"

  4921         using dist_triangle [of "f n y" "g x" "f n x"]

  4922         using n(1)[THEN bspec[where x=x], OF x\<in>s]

  4923         by auto

  4924       hence "dist (g y) (g x) < e"

  4925         using n(1)[THEN bspec[where x=y], OF y\<in>s]

  4926         using dist_triangle3 [of "g y" "g x" "f n y"]

  4927         by auto }

  4928     hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e"

  4929       using d>0 by auto }

  4930   thus ?thesis unfolding continuous_on_iff by auto

  4931 qed

  4932

  4933

  4934 subsection {* Topological stuff lifted from and dropped to R *}

  4935

  4936 lemma open_real:

  4937   fixes s :: "real set" shows

  4938  "open s \<longleftrightarrow>

  4939         (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. abs(x' - x) < e --> x' \<in> s)" (is "?lhs = ?rhs")

  4940   unfolding open_dist dist_norm by simp

  4941

  4942 lemma islimpt_approachable_real:

  4943   fixes s :: "real set"

  4944   shows "x islimpt s \<longleftrightarrow> (\<forall>e>0.  \<exists>x'\<in> s. x' \<noteq> x \<and> abs(x' - x) < e)"

  4945   unfolding islimpt_approachable dist_norm by simp

  4946

  4947 lemma closed_real:

  4948   fixes s :: "real set"

  4949   shows "closed s \<longleftrightarrow>

  4950         (\<forall>x. (\<forall>e>0.  \<exists>x' \<in> s. x' \<noteq> x \<and> abs(x' - x) < e)

  4951             --> x \<in> s)"

  4952   unfolding closed_limpt islimpt_approachable dist_norm by simp

  4953

  4954 lemma continuous_at_real_range:

  4955   fixes f :: "'a::real_normed_vector \<Rightarrow> real"

  4956   shows "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.

  4957         \<forall>x'. norm(x' - x) < d --> abs(f x' - f x) < e)"

  4958   unfolding continuous_at unfolding Lim_at

  4959   unfolding dist_nz[THEN sym] unfolding dist_norm apply auto

  4960   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

  4961   apply(erule_tac x=e in allE) by auto

  4962

  4963 lemma continuous_on_real_range:

  4964   fixes f :: "'a::real_normed_vector \<Rightarrow> real"

  4965   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))"

  4966   unfolding continuous_on_iff dist_norm by simp

  4967

  4968 lemma compact_attains_sup:

  4969   fixes S :: "'a::linorder_topology set"

  4970   assumes "compact S" "S \<noteq> {}"

  4971   shows "\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s"

  4972 proof (rule classical)

  4973   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s)"

  4974   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. s < t s"

  4975     by (metis not_le)

  4976   then have "\<forall>s\<in>S. open {..< t s}" "S \<subseteq> (\<Union>s\<in>S. {..< t s})"

  4977     by auto

  4978   with compact S obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {..< t s})"

  4979     by (erule compactE_image)

  4980   with S \<noteq> {} have Max: "Max (tC) \<in> tC" and "\<forall>s\<in>tC. s \<le> Max (tC)"

  4981     by (auto intro!: Max_in)

  4982   with C have "S \<subseteq> {..< Max (tC)}"

  4983     by (auto intro: less_le_trans simp: subset_eq)

  4984   with t Max C \<subseteq> S show ?thesis

  4985     by fastforce

  4986 qed

  4987

  4988 lemma compact_attains_inf:

  4989   fixes S :: "'a::linorder_topology set"

  4990   assumes "compact S" "S \<noteq> {}"

  4991   shows "\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t"

  4992 proof (rule classical)

  4993   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t)"

  4994   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. t s < s"

  4995     by (metis not_le)

  4996   then have "\<forall>s\<in>S. open {t s <..}" "S \<subseteq> (\<Union>s\<in>S. {t s <..})"

  4997     by auto

  4998   with compact S obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {t s <..})"

  4999     by (erule compactE_image)

  5000   with S \<noteq> {} have Min: "Min (tC) \<in> tC" and "\<forall>s\<in>tC. Min (tC) \<le> s"

  5001     by (auto intro!: Min_in)

  5002   with C have "S \<subseteq> {Min (tC) <..}"

  5003     by (auto intro: le_less_trans simp: subset_eq)

  5004   with t Min C \<subseteq> S show ?thesis

  5005     by fastforce

  5006 qed

  5007

  5008 lemma continuous_attains_sup:

  5009   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"

  5010   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f \<Longrightarrow> (\<exists>x\<in>s. \<forall>y\<in>s.  f y \<le> f x)"

  5011   using compact_attains_sup[of "f  s"] compact_continuous_image[of s f] by auto

  5012

  5013 lemma continuous_attains_inf:

  5014   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"

  5015   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f \<Longrightarrow> (\<exists>x\<in>s. \<forall>y\<in>s. f x \<le> f y)"

  5016   using compact_attains_inf[of "f  s"] compact_continuous_image[of s f] by auto

  5017

  5018 text {* Hence some handy theorems on distance, diameter etc. of/from a set. *}

  5019

  5020 lemma distance_attains_sup:

  5021   assumes "compact s" "s \<noteq> {}"

  5022   shows "\<exists>x\<in>s. \<forall>y\<in>s. dist a y \<le> dist a x"

  5023 proof (rule continuous_attains_sup [OF assms])

  5024   { fix x assume "x\<in>s"

  5025     have "(dist a ---> dist a x) (at x within s)"

  5026       by (intro tendsto_dist tendsto_const Lim_at_within tendsto_ident_at)

  5027   }

  5028   thus "continuous_on s (dist a)"

  5029     unfolding continuous_on ..

  5030 qed

  5031

  5032 text {* For \emph{minimal} distance, we only need closure, not compactness. *}

  5033

  5034 lemma distance_attains_inf:

  5035   fixes a :: "'a::heine_borel"

  5036   assumes "closed s"  "s \<noteq> {}"

  5037   shows "\<exists>x\<in>s. \<forall>y\<in>s. dist a x \<le> dist a y"

  5038 proof-

  5039   from assms(2) obtain b where "b \<in> s" by auto

  5040   let ?B = "s \<inter> cball a (dist b a)"

  5041   have "?B \<noteq> {}" using b \<in> s by (auto simp add: dist_commute)

  5042   moreover have "continuous_on ?B (dist a)"

  5043     by (auto intro!: continuous_at_imp_continuous_on continuous_dist continuous_at_id continuous_const)

  5044   moreover have "compact ?B"

  5045     by (intro closed_inter_compact closed s compact_cball)

  5046   ultimately obtain x where "x \<in> ?B" "\<forall>y\<in>?B. dist a x \<le> dist a y"

  5047     by (metis continuous_attains_inf)

  5048   thus ?thesis by fastforce

  5049 qed

  5050

  5051

  5052 subsection {* Pasted sets *}

  5053

  5054 lemma bounded_Times:

  5055   assumes "bounded s" "bounded t" shows "bounded (s \<times> t)"

  5056 proof-

  5057   obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"

  5058     using assms [unfolded bounded_def] by auto

  5059   then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<twosuperior> + b\<twosuperior>)"

  5060     by (auto simp add: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)

  5061   thus ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto

  5062 qed

  5063

  5064 lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"

  5065 by (induct x) simp

  5066

  5067 lemma seq_compact_Times: "seq_compact s \<Longrightarrow> seq_compact t \<Longrightarrow> seq_compact (s \<times> t)"

  5068 unfolding seq_compact_def

  5069 apply clarify

  5070 apply (drule_tac x="fst \<circ> f" in spec)

  5071 apply (drule mp, simp add: mem_Times_iff)

  5072 apply (clarify, rename_tac l1 r1)

  5073 apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)

  5074 apply (drule mp, simp add: mem_Times_iff)

  5075 apply (clarify, rename_tac l2 r2)

  5076 apply (rule_tac x="(l1, l2)" in rev_bexI, simp)

  5077 apply (rule_tac x="r1 \<circ> r2" in exI)

  5078 apply (rule conjI, simp add: subseq_def)

  5079 apply (drule_tac f=r2 in LIMSEQ_subseq_LIMSEQ, assumption)

  5080 apply (drule (1) tendsto_Pair) back

  5081 apply (simp add: o_def)

  5082 done

  5083

  5084 lemma compact_Times:

  5085   assumes "compact s" "compact t"

  5086   shows "compact (s \<times> t)"

  5087 proof (rule compactI)

  5088   fix C assume C: "\<forall>t\<in>C. open t" "s \<times> t \<subseteq> \<Union>C"

  5089   have "\<forall>x\<in>s. \<exists>a. open a \<and> x \<in> a \<and> (\<exists>d\<subseteq>C. finite d \<and> a \<times> t \<subseteq> \<Union>d)"

  5090   proof

  5091     fix x assume "x \<in> s"

  5092     have "\<forall>y\<in>t. \<exists>a b c. c \<in> C \<and> open a \<and> open b \<and> x \<in> a \<and> y \<in> b \<and> a \<times> b \<subseteq> c" (is "\<forall>y\<in>t. ?P y")

  5093     proof

  5094       fix y assume "y \<in> t"

  5095       with x \<in> s C obtain c where "c \<in> C" "(x, y) \<in> c" "open c" by auto

  5096       then show "?P y" by (auto elim!: open_prod_elim)

  5097     qed

  5098     then obtain a b c where b: "\<And>y. y \<in> t \<Longrightarrow> open (b y)"

  5099       and c: "\<And>y. y \<in> t \<Longrightarrow> c y \<in> C \<and> open (a y) \<and> open (b y) \<and> x \<in> a y \<and> y \<in> b y \<and> a y \<times> b y \<subseteq> c y"

  5100       by metis

  5101     then have "\<forall>y\<in>t. open (b y)" "t \<subseteq> (\<Union>y\<in>t. b y)" by auto

  5102     from compactE_image[OF compact t this] obtain D where "D \<subseteq> t" "finite D" "t \<subseteq> (\<Union>y\<in>D. b y)"

  5103       by auto

  5104     moreover with c have "(\<Inter>y\<in>D. a y) \<times> t \<subseteq> (\<Union>y\<in>D. c y)"

  5105       by (fastforce simp: subset_eq)

  5106     ultimately show "\<exists>a. open a \<and> x \<in> a \<and> (\<exists>d\<subseteq>C. finite d \<and> a \<times> t \<subseteq> \<Union>d)"

  5107       using c by (intro exI[of _ "cD"] exI[of _ "\<Inter>aD"] conjI) (auto intro!: open_INT)

  5108   qed

  5109   then obtain a d where a: "\<forall>x\<in>s. open (a x)" "s \<subseteq> (\<Union>x\<in>s. a x)"

  5110     and d: "\<And>x. x \<in> s \<Longrightarrow> d x \<subseteq> C \<and> finite (d x) \<and> a x \<times> t \<subseteq> \<Union>d x"

  5111     unfolding subset_eq UN_iff by metis

  5112   moreover from compactE_image[OF compact s a] obtain e where e: "e \<subseteq> s" "finite e"

  5113     and s: "s \<subseteq> (\<Union>x\<in>e. a x)" by auto

  5114   moreover

  5115   { from s have "s \<times> t \<subseteq> (\<Union>x\<in>e. a x \<times> t)" by auto

  5116     also have "\<dots> \<subseteq> (\<Union>x\<in>e. \<Union>d x)" using d e \<subseteq> s by (intro UN_mono) auto

  5117     finally have "s \<times> t \<subseteq> (\<Union>x\<in>e. \<Union>d x)" . }

  5118   ultimately show "\<exists>C'\<subseteq>C. finite C' \<and> s \<times> t \<subseteq> \<Union>C'"

  5119     by (intro exI[of _ "(\<Union>x\<in>e. d x)"]) (auto simp add: subset_eq)

  5120 qed

  5121

  5122 text{* Hence some useful properties follow quite easily. *}

  5123

  5124 lemma compact_scaling:

  5125   fixes s :: "'a::real_normed_vector set"

  5126   assumes "compact s"  shows "compact ((\<lambda>x. c *\<^sub>R x)  s)"

  5127 proof-

  5128   let ?f = "\<lambda>x. scaleR c x"

  5129   have *:"bounded_linear ?f" by (rule bounded_linear_scaleR_right)

  5130   show ?thesis using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]

  5131     using linear_continuous_at[OF *] assms by auto

  5132 qed

  5133

  5134 lemma compact_negations:

  5135   fixes s :: "'a::real_normed_vector set"

  5136   assumes "compact s"  shows "compact ((\<lambda>x. -x)  s)"

  5137   using compact_scaling [OF assms, of "- 1"] by auto

  5138

  5139 lemma compact_sums:

  5140   fixes s t :: "'a::real_normed_vector set"

  5141   assumes "compact s"  "compact t"  shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"

  5142 proof-

  5143   have *:"{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z)  (s \<times> t)"

  5144     apply auto unfolding image_iff apply(rule_tac x="(xa, y)" in bexI) by auto

  5145   have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"

  5146     unfolding continuous_on by (rule ballI) (intro tendsto_intros)

  5147   thus ?thesis unfolding * using compact_continuous_image compact_Times [OF assms] by auto

  5148 qed

  5149

  5150 lemma compact_differences:

  5151   fixes s t :: "'a::real_normed_vector set"

  5152   assumes "compact s" "compact t"  shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"

  5153 proof-

  5154   have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus  t)}"

  5155     apply auto apply(rule_tac x= xa in exI) apply auto apply(rule_tac x=xa in exI) by auto

  5156   thus ?thesis using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto

  5157 qed

  5158

  5159 lemma compact_translation:

  5160   fixes s :: "'a::real_normed_vector set"

  5161   assumes "compact s"  shows "compact ((\<lambda>x. a + x)  s)"

  5162 proof-

  5163   have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x)  s" by auto

  5164   thus ?thesis using compact_sums[OF assms compact_sing[of a]] by auto

  5165 qed

  5166

  5167 lemma compact_affinity:

  5168   fixes s :: "'a::real_normed_vector set"

  5169   assumes "compact s"  shows "compact ((\<lambda>x. a + c *\<^sub>R x)  s)"

  5170 proof-

  5171   have "op + a  op *\<^sub>R c  s = (\<lambda>x. a + c *\<^sub>R x)  s" by auto

  5172   thus ?thesis using compact_translation[OF compact_scaling[OF assms], of a c] by auto

  5173 qed

  5174

  5175 text {* Hence we get the following. *}

  5176

  5177 lemma compact_sup_maxdistance:

  5178   fixes s :: "'a::metric_space set"

  5179   assumes "compact s"  "s \<noteq> {}"

  5180   shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. dist u v \<le> dist x y"

  5181 proof-

  5182   have "compact (s \<times> s)" using compact s by (intro compact_Times)

  5183   moreover have "s \<times> s \<noteq> {}" using s \<noteq> {} by auto

  5184   moreover have "continuous_on (s \<times> s) (\<lambda>x. dist (fst x) (snd x))"

  5185     by (intro continuous_at_imp_continuous_on ballI continuous_dist

  5186       continuous_isCont[THEN iffD1] isCont_fst isCont_snd isCont_ident)

  5187   ultimately show ?thesis

  5188     using continuous_attains_sup[of "s \<times> s" "\<lambda>x. dist (fst x) (snd x)"] by auto

  5189 qed

  5190

  5191 text {* We can state this in terms of diameter of a set. *}

  5192

  5193 definition "diameter s = (if s = {} then 0::real else Sup {dist x y | x y. x \<in> s \<and> y \<in> s})"

  5194

  5195 lemma diameter_bounded_bound:

  5196   fixes s :: "'a :: metric_space set"

  5197   assumes s: "bounded s" "x \<in> s" "y \<in> s"

  5198   shows "dist x y \<le> diameter s"

  5199 proof -

  5200   let ?D = "{dist x y |x y. x \<in> s \<and> y \<in> s}"

  5201   from s obtain z d where z: "\<And>x. x \<in> s \<Longrightarrow> dist z x \<le> d"

  5202     unfolding bounded_def by auto

  5203   have "dist x y \<le> Sup ?D"

  5204   proof (rule cSup_upper, safe)

  5205     fix a b assume "a \<in> s" "b \<in> s"

  5206     with z[of a] z[of b] dist_triangle[of a b z]

  5207     show "dist a b \<le> 2 * d"

  5208       by (simp add: dist_commute)

  5209   qed (insert s, auto)

  5210   with x \<in> s show ?thesis

  5211     by (auto simp add: diameter_def)

  5212 qed

  5213

  5214 lemma diameter_lower_bounded:

  5215   fixes s :: "'a :: metric_space set"

  5216   assumes s: "bounded s" and d: "0 < d" "d < diameter s"

  5217   shows "\<exists>x\<in>s. \<exists>y\<in>s. d < dist x y"

  5218 proof (rule ccontr)

  5219   let ?D = "{dist x y |x y. x \<in> s \<and> y \<in> s}"

  5220   assume contr: "\<not> ?thesis"

  5221   moreover

  5222   from d have "s \<noteq> {}"

  5223     by (auto simp: diameter_def)

  5224   then have "?D \<noteq> {}" by auto

  5225   ultimately have "Sup ?D \<le> d"

  5226     by (intro cSup_least) (auto simp: not_less)

  5227   with d < diameter s s \<noteq> {} show False

  5228     by (auto simp: diameter_def)

  5229 qed

  5230

  5231 lemma diameter_bounded:

  5232   assumes "bounded s"

  5233   shows "\<forall>x\<in>s. \<forall>y\<in>s. dist x y \<le> diameter s"

  5234         "\<forall>d>0. d < diameter s \<longrightarrow> (\<exists>x\<in>s. \<exists>y\<in>s. dist x y > d)"

  5235   using diameter_bounded_bound[of s] diameter_lower_bounded[of s] assms

  5236   by auto

  5237

  5238 lemma diameter_compact_attained:

  5239   assumes "compact s"  "s \<noteq> {}"

  5240   shows "\<exists>x\<in>s. \<exists>y\<in>s. dist x y = diameter s"

  5241 proof -

  5242   have b:"bounded s" using assms(1) by (rule compact_imp_bounded)

  5243   then obtain x y where xys:"x\<in>s" "y\<in>s" and xy:"\<forall>u\<in>s. \<forall>v\<in>s. dist u v \<le> dist x y"

  5244     using compact_sup_maxdistance[OF assms] by auto

  5245   hence "diameter s \<le> dist x y"

  5246     unfolding diameter_def by clarsimp (rule cSup_least, fast+)

  5247   thus ?thesis

  5248     by (metis b diameter_bounded_bound order_antisym xys)

  5249 qed

  5250

  5251 text {* Related results with closure as the conclusion. *}

  5252

  5253 lemma closed_scaling:

  5254   fixes s :: "'a::real_normed_vector set"

  5255   assumes "closed s" shows "closed ((\<lambda>x. c *\<^sub>R x)  s)"

  5256 proof(cases "s={}")

  5257   case True thus ?thesis by auto

  5258 next

  5259   case False

  5260   show ?thesis

  5261   proof(cases "c=0")

  5262     have *:"(\<lambda>x. 0)  s = {0}" using s\<noteq>{} by auto

  5263     case True thus ?thesis apply auto unfolding * by auto

  5264   next

  5265     case False

  5266     { fix x l assume as:"\<forall>n::nat. x n \<in> scaleR c  s"  "(x ---> l) sequentially"

  5267       { fix n::nat have "scaleR (1 / c) (x n) \<in> s"

  5268           using as(1)[THEN spec[where x=n]]

  5269           using c\<noteq>0 by auto

  5270       }

  5271       moreover

  5272       { fix e::real assume "e>0"

  5273         hence "0 < e *\<bar>c\<bar>"  using c\<noteq>0 mult_pos_pos[of e "abs c"] by auto

  5274         then obtain N where "\<forall>n\<ge>N. dist (x n) l < e * \<bar>c\<bar>"

  5275           using as(2)[unfolded LIMSEQ_def, THEN spec[where x="e * abs c"]] by auto

  5276         hence "\<exists>N. \<forall>n\<ge>N. dist (scaleR (1 / c) (x n)) (scaleR (1 / c) l) < e"

  5277           unfolding dist_norm unfolding scaleR_right_diff_distrib[THEN sym]

  5278           using mult_imp_div_pos_less[of "abs c" _ e] c\<noteq>0 by auto  }

  5279       hence "((\<lambda>n. scaleR (1 / c) (x n)) ---> scaleR (1 / c) l) sequentially" unfolding LIMSEQ_def by auto

  5280       ultimately have "l \<in> scaleR c  s"

  5281         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"]]

  5282         unfolding image_iff using c\<noteq>0 apply(rule_tac x="scaleR (1 / c) l" in bexI) by auto  }

  5283     thus ?thesis unfolding closed_sequential_limits by fast

  5284   qed

  5285 qed

  5286

  5287 lemma closed_negations:

  5288   fixes s :: "'a::real_normed_vector set"

  5289   assumes "closed s"  shows "closed ((\<lambda>x. -x)  s)"

  5290   using closed_scaling[OF assms, of "- 1"] by simp

  5291

  5292 lemma compact_closed_sums:

  5293   fixes s :: "'a::real_normed_vector set"

  5294   assumes "compact s"  "closed t"  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"

  5295 proof-

  5296   let ?S = "{x + y |x y. x \<in> s \<and> y \<in> t}"

  5297   { fix x l assume as:"\<forall>n. x n \<in> ?S"  "(x ---> l) sequentially"

  5298     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"

  5299       using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> s \<and> snd y \<in> t"] by auto

  5300     obtain l' r where "l'\<in>s" and r:"subseq r" and lr:"(((\<lambda>n. fst (f n)) \<circ> r) ---> l') sequentially"

  5301       using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto

  5302     have "((\<lambda>n. snd (f (r n))) ---> l - l') sequentially"

  5303       using tendsto_diff[OF LIMSEQ_subseq_LIMSEQ[OF as(2) r] lr] and f(1) unfolding o_def by auto

  5304     hence "l - l' \<in> t"

  5305       using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda> n. snd (f (r n))"], THEN spec[where x="l - l'"]]

  5306       using f(3) by auto

  5307     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

  5308   }

  5309   thus ?thesis unfolding closed_sequential_limits by fast

  5310 qed

  5311

  5312 lemma closed_compact_sums:

  5313   fixes s t :: "'a::real_normed_vector set"

  5314   assumes "closed s"  "compact t"

  5315   shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"

  5316 proof-

  5317   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

  5318     apply(rule_tac x=y in exI) apply auto apply(rule_tac x=y in exI) by auto

  5319   thus ?thesis using compact_closed_sums[OF assms(2,1)] by simp

  5320 qed

  5321

  5322 lemma compact_closed_differences:

  5323   fixes s t :: "'a::real_normed_vector set"

  5324   assumes "compact s"  "closed t"

  5325   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"

  5326 proof-

  5327   have "{x + y |x y. x \<in> s \<and> y \<in> uminus  t} =  {x - y |x y. x \<in> s \<and> y \<in> t}"

  5328     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto

  5329   thus ?thesis using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto

  5330 qed

  5331

  5332 lemma closed_compact_differences:

  5333   fixes s t :: "'a::real_normed_vector set"

  5334   assumes "closed s" "compact t"

  5335   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"

  5336 proof-

  5337   have "{x + y |x y. x \<in> s \<and> y \<in> uminus  t} = {x - y |x y. x \<in> s \<and> y \<in> t}"

  5338     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto

  5339  thus ?thesis using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp

  5340 qed

  5341

  5342 lemma closed_translation:

  5343   fixes a :: "'a::real_normed_vector"

  5344   assumes "closed s"  shows "closed ((\<lambda>x. a + x)  s)"

  5345 proof-

  5346   have "{a + y |y. y \<in> s} = (op + a  s)" by auto

  5347   thus ?thesis using compact_closed_sums[OF compact_sing[of a] assms] by auto

  5348 qed

  5349

  5350 lemma translation_Compl:

  5351   fixes a :: "'a::ab_group_add"

  5352   shows "(\<lambda>x. a + x)  (- t) = - ((\<lambda>x. a + x)  t)"

  5353   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in bexI) by auto

  5354

  5355 lemma translation_UNIV:

  5356   fixes a :: "'a::ab_group_add" shows "range (\<lambda>x. a + x) = UNIV"

  5357   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in exI) by auto

  5358

  5359 lemma translation_diff:

  5360   fixes a :: "'a::ab_group_add"

  5361   shows "(\<lambda>x. a + x)  (s - t) = ((\<lambda>x. a + x)  s) - ((\<lambda>x. a + x)  t)"

  5362   by auto

  5363

  5364 lemma closure_translation:

  5365   fixes a :: "'a::real_normed_vector"

  5366   shows "closure ((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (closure s)"

  5367 proof-

  5368   have *:"op + a  (- s) = - op + a  s"

  5369     apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto

  5370   show ?thesis unfolding closure_interior translation_Compl

  5371     using interior_translation[of a "- s"] unfolding * by auto

  5372 qed

  5373

  5374 lemma frontier_translation:

  5375   fixes a :: "'a::real_normed_vector"

  5376   shows "frontier((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (frontier s)"

  5377   unfolding frontier_def translation_diff interior_translation closure_translation by auto

  5378

  5379

  5380 subsection {* Separation between points and sets *}

  5381

  5382 lemma separate_point_closed:

  5383   fixes s :: "'a::heine_borel set"

  5384   shows "closed s \<Longrightarrow> a \<notin> s  ==> (\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x)"

  5385 proof(cases "s = {}")

  5386   case True

  5387   thus ?thesis by(auto intro!: exI[where x=1])

  5388 next

  5389   case False

  5390   assume "closed s" "a \<notin> s"

  5391   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

  5392   with x\<in>s show ?thesis using dist_pos_lt[of a x] anda \<notin> s by blast

  5393 qed

  5394

  5395 lemma separate_compact_closed:

  5396   fixes s t :: "'a::heine_borel set"

  5397   assumes "compact s" and t: "closed t" "s \<inter> t = {}"

  5398   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"

  5399 proof cases

  5400   assume "s \<noteq> {} \<and> t \<noteq> {}"

  5401   then have "s \<noteq> {}" "t \<noteq> {}" by auto

  5402   let ?inf = "\<lambda>x. infdist x t"

  5403   have "continuous_on s ?inf"

  5404     by (auto intro!: continuous_at_imp_continuous_on continuous_infdist continuous_at_id)

  5405   then obtain x where x: "x \<in> s" "\<forall>y\<in>s. ?inf x \<le> ?inf y"

  5406     using continuous_attains_inf[OF compact s s \<noteq> {}] by auto

  5407   then have "0 < ?inf x"

  5408     using t t \<noteq> {} in_closed_iff_infdist_zero by (auto simp: less_le infdist_nonneg)

  5409   moreover have "\<forall>x'\<in>s. \<forall>y\<in>t. ?inf x \<le> dist x' y"

  5410     using x by (auto intro: order_trans infdist_le)

  5411   ultimately show ?thesis

  5412     by auto

  5413 qed (auto intro!: exI[of _ 1])

  5414

  5415 lemma separate_closed_compact:

  5416   fixes s t :: "'a::heine_borel set"

  5417   assumes "closed s" and "compact t" and "s \<inter> t = {}"

  5418   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"

  5419 proof-

  5420   have *:"t \<inter> s = {}" using assms(3) by auto

  5421   show ?thesis using separate_compact_closed[OF assms(2,1) *]

  5422     apply auto apply(rule_tac x=d in exI) apply auto apply (erule_tac x=y in ballE)

  5423     by (auto simp add: dist_commute)

  5424 qed

  5425

  5426

  5427 subsection {* Intervals *}

  5428

  5429 lemma interval: fixes a :: "'a::ordered_euclidean_space" shows

  5430   "{a <..< b} = {x::'a. \<forall>i\<in>Basis. a\<bullet>i < x\<bullet>i \<and> x\<bullet>i < b\<bullet>i}" and

  5431   "{a .. b} = {x::'a. \<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i}"

  5432   by(auto simp add:set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])

  5433

  5434 lemma mem_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5435   "x \<in> {a<..<b} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < x\<bullet>i \<and> x\<bullet>i < b\<bullet>i)"

  5436   "x \<in> {a .. b} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i)"

  5437   using interval[of a b] by(auto simp add: set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])

  5438

  5439 lemma interval_eq_empty: fixes a :: "'a::ordered_euclidean_space" shows

  5440  "({a <..< b} = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i \<le> a\<bullet>i))" (is ?th1) and

  5441  "({a  ..  b} = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i < a\<bullet>i))" (is ?th2)

  5442 proof-

  5443   { fix i x assume i:"i\<in>Basis" and as:"b\<bullet>i \<le> a\<bullet>i" and x:"x\<in>{a <..< b}"

  5444     hence "a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i" unfolding mem_interval by auto

  5445     hence "a\<bullet>i < b\<bullet>i" by auto

  5446     hence False using as by auto  }

  5447   moreover

  5448   { assume as:"\<forall>i\<in>Basis. \<not> (b\<bullet>i \<le> a\<bullet>i)"

  5449     let ?x = "(1/2) *\<^sub>R (a + b)"

  5450     { fix i :: 'a assume i:"i\<in>Basis"

  5451       have "a\<bullet>i < b\<bullet>i" using as[THEN bspec[where x=i]] i by auto

  5452       hence "a\<bullet>i < ((1/2) *\<^sub>R (a+b)) \<bullet> i" "((1/2) *\<^sub>R (a+b)) \<bullet> i < b\<bullet>i"

  5453         by (auto simp: inner_add_left) }

  5454     hence "{a <..< b} \<noteq> {}" using mem_interval(1)[of "?x" a b] by auto  }

  5455   ultimately show ?th1 by blast

  5456

  5457   { fix i x assume i:"i\<in>Basis" and as:"b\<bullet>i < a\<bullet>i" and x:"x\<in>{a .. b}"

  5458     hence "a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i" unfolding mem_interval by auto

  5459     hence "a\<bullet>i \<le> b\<bullet>i" by auto

  5460     hence False using as by auto  }

  5461   moreover

  5462   { assume as:"\<forall>i\<in>Basis. \<not> (b\<bullet>i < a\<bullet>i)"

  5463     let ?x = "(1/2) *\<^sub>R (a + b)"

  5464     { fix i :: 'a assume i:"i\<in>Basis"

  5465       have "a\<bullet>i \<le> b\<bullet>i" using as[THEN bspec[where x=i]] i by auto

  5466       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"

  5467         by (auto simp: inner_add_left) }

  5468     hence "{a .. b} \<noteq> {}" using mem_interval(2)[of "?x" a b] by auto  }

  5469   ultimately show ?th2 by blast

  5470 qed

  5471

  5472 lemma interval_ne_empty: fixes a :: "'a::ordered_euclidean_space" shows

  5473   "{a  ..  b} \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i)" and

  5474   "{a <..< b} \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i)"

  5475   unfolding interval_eq_empty[of a b] by fastforce+

  5476

  5477 lemma interval_sing:

  5478   fixes a :: "'a::ordered_euclidean_space"

  5479   shows "{a .. a} = {a}" and "{a<..<a} = {}"

  5480   unfolding set_eq_iff mem_interval eq_iff [symmetric]

  5481   by (auto intro: euclidean_eqI simp: ex_in_conv)

  5482

  5483 lemma subset_interval_imp: fixes a :: "'a::ordered_euclidean_space" shows

  5484  "(\<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

  5485  "(\<forall>i\<in>Basis. a\<bullet>i < c\<bullet>i \<and> d\<bullet>i < b\<bullet>i) \<Longrightarrow> {c .. d} \<subseteq> {a<..<b}" and

  5486  "(\<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

  5487  "(\<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}"

  5488   unfolding subset_eq[unfolded Ball_def] unfolding mem_interval

  5489   by (best intro: order_trans less_le_trans le_less_trans less_imp_le)+

  5490

  5491 lemma interval_open_subset_closed:

  5492   fixes a :: "'a::ordered_euclidean_space"

  5493   shows "{a<..<b} \<subseteq> {a .. b}"

  5494   unfolding subset_eq [unfolded Ball_def] mem_interval

  5495   by (fast intro: less_imp_le)

  5496

  5497 lemma subset_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5498  "{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

  5499  "{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

  5500  "{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

  5501  "{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)

  5502 proof-

  5503   show ?th1 unfolding subset_eq and Ball_def and mem_interval by (auto intro: order_trans)

  5504   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)

  5505   { assume as: "{c<..<d} \<subseteq> {a .. b}" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"

  5506     hence "{c<..<d} \<noteq> {}" unfolding interval_eq_empty by auto

  5507     fix i :: 'a assume i:"i\<in>Basis"

  5508     (** TODO combine the following two parts as done in the HOL_light version. **)

  5509     { 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"

  5510       assume as2: "a\<bullet>i > c\<bullet>i"

  5511       { fix j :: 'a assume j:"j\<in>Basis"

  5512         hence "c \<bullet> j < ?x \<bullet> j \<and> ?x \<bullet> j < d \<bullet> j"

  5513           apply(cases "j=i") using as(2)[THEN bspec[where x=j]] i

  5514           by (auto simp add: as2)  }

  5515       hence "?x\<in>{c<..<d}" using i unfolding mem_interval by auto

  5516       moreover

  5517       have "?x\<notin>{a .. b}"

  5518         unfolding mem_interval apply auto apply(rule_tac x=i in bexI)

  5519         using as(2)[THEN bspec[where x=i]] and as2 i

  5520         by auto

  5521       ultimately have False using as by auto  }

  5522     hence "a\<bullet>i \<le> c\<bullet>i" by(rule ccontr)auto

  5523     moreover

  5524     { 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"

  5525       assume as2: "b\<bullet>i < d\<bullet>i"

  5526       { fix j :: 'a assume "j\<in>Basis"

  5527         hence "d \<bullet> j > ?x \<bullet> j \<and> ?x \<bullet> j > c \<bullet> j"

  5528           apply(cases "j=i") using as(2)[THEN bspec[where x=j]]

  5529           by (auto simp add: as2) }

  5530       hence "?x\<in>{c<..<d}" unfolding mem_interval by auto

  5531       moreover

  5532       have "?x\<notin>{a .. b}"

  5533         unfolding mem_interval apply auto apply(rule_tac x=i in bexI)

  5534         using as(2)[THEN bspec[where x=i]] and as2 using i

  5535         by auto

  5536       ultimately have False using as by auto  }

  5537     hence "b\<bullet>i \<ge> d\<bullet>i" by(rule ccontr)auto

  5538     ultimately

  5539     have "a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i" by auto

  5540   } note part1 = this

  5541   show ?th3

  5542     unfolding subset_eq and Ball_def and mem_interval

  5543     apply(rule,rule,rule,rule)

  5544     apply(rule part1)

  5545     unfolding subset_eq and Ball_def and mem_interval

  5546     prefer 4

  5547     apply auto

  5548     by(erule_tac x=xa in allE,erule_tac x=xa in allE,fastforce)+

  5549   { assume as:"{c<..<d} \<subseteq> {a<..<b}" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"

  5550     fix i :: 'a assume i:"i\<in>Basis"

  5551     from as(1) have "{c<..<d} \<subseteq> {a..b}" using interval_open_subset_closed[of a b] by auto

  5552     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

  5553   show ?th4 unfolding subset_eq and Ball_def and mem_interval

  5554     apply(rule,rule,rule,rule) apply(rule *) unfolding subset_eq and Ball_def and mem_interval prefer 4

  5555     apply auto by(erule_tac x=xa in allE, simp)+

  5556 qed

  5557

  5558 lemma inter_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5559  "{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)}"

  5560   unfolding set_eq_iff and Int_iff and mem_interval by auto

  5561

  5562 lemma disjoint_interval: fixes a::"'a::ordered_euclidean_space" shows

  5563   "{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

  5564   "{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

  5565   "{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

  5566   "{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)

  5567 proof-

  5568   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"

  5569   have **: "\<And>P Q. (\<And>i :: 'a. i \<in> Basis \<Longrightarrow> Q ?z i \<Longrightarrow> P i) \<Longrightarrow>

  5570       (\<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)"

  5571     by blast

  5572   note * = set_eq_iff Int_iff empty_iff mem_interval ball_conj_distrib[symmetric] eq_False ball_simps(10)

  5573   show ?th1 unfolding * by (intro **) auto

  5574   show ?th2 unfolding * by (intro **) auto

  5575   show ?th3 unfolding * by (intro **) auto

  5576   show ?th4 unfolding * by (intro **) auto

  5577 qed

  5578

  5579 (* Moved interval_open_subset_closed a bit upwards *)

  5580

  5581 lemma open_interval[intro]:

  5582   fixes a b :: "'a::ordered_euclidean_space" shows "open {a<..<b}"

  5583 proof-

  5584   have "open (\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i<..<b\<bullet>i})"

  5585     by (intro open_INT finite_lessThan ballI continuous_open_vimage allI

  5586       linear_continuous_at open_real_greaterThanLessThan finite_Basis bounded_linear_inner_left)

  5587   also have "(\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i<..<b\<bullet>i}) = {a<..<b}"

  5588     by (auto simp add: eucl_less [where 'a='a])

  5589   finally show "open {a<..<b}" .

  5590 qed

  5591

  5592 lemma closed_interval[intro]:

  5593   fixes a b :: "'a::ordered_euclidean_space" shows "closed {a .. b}"

  5594 proof-

  5595   have "closed (\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i .. b\<bullet>i})"

  5596     by (intro closed_INT ballI continuous_closed_vimage allI

  5597       linear_continuous_at closed_real_atLeastAtMost finite_Basis bounded_linear_inner_left)

  5598   also have "(\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i .. b\<bullet>i}) = {a .. b}"

  5599     by (auto simp add: eucl_le [where 'a='a])

  5600   finally show "closed {a .. b}" .

  5601 qed

  5602

  5603 lemma interior_closed_interval [intro]:

  5604   fixes a b :: "'a::ordered_euclidean_space"

  5605   shows "interior {a..b} = {a<..<b}" (is "?L = ?R")

  5606 proof(rule subset_antisym)

  5607   show "?R \<subseteq> ?L" using interval_open_subset_closed open_interval

  5608     by (rule interior_maximal)

  5609 next

  5610   { fix x assume "x \<in> interior {a..b}"

  5611     then obtain s where s:"open s" "x \<in> s" "s \<subseteq> {a..b}" ..

  5612     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

  5613     { fix i :: 'a assume i:"i\<in>Basis"

  5614       have "dist (x - (e / 2) *\<^sub>R i) x < e"

  5615            "dist (x + (e / 2) *\<^sub>R i) x < e"

  5616         unfolding dist_norm apply auto

  5617         unfolding norm_minus_cancel using norm_Basis[OF i] e>0 by auto

  5618       hence "a \<bullet> i \<le> (x - (e / 2) *\<^sub>R i) \<bullet> i"

  5619                      "(x + (e / 2) *\<^sub>R i) \<bullet> i \<le> b \<bullet> i"

  5620         using e[THEN spec[where x="x - (e/2) *\<^sub>R i"]]

  5621         and   e[THEN spec[where x="x + (e/2) *\<^sub>R i"]]

  5622         unfolding mem_interval using i by blast+

  5623       hence "a \<bullet> i < x \<bullet> i" and "x \<bullet> i < b \<bullet> i"

  5624         using e>0 i by (auto simp: inner_diff_left inner_Basis inner_add_left) }

  5625     hence "x \<in> {a<..<b}" unfolding mem_interval by auto  }

  5626   thus "?L \<subseteq> ?R" ..

  5627 qed

  5628

  5629 lemma bounded_closed_interval: fixes a :: "'a::ordered_euclidean_space" shows "bounded {a .. b}"

  5630 proof-

  5631   let ?b = "\<Sum>i\<in>Basis. \<bar>a\<bullet>i\<bar> + \<bar>b\<bullet>i\<bar>"

  5632   { fix x::"'a" assume x:"\<forall>i\<in>Basis. a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i"

  5633     { fix i :: 'a assume "i\<in>Basis"

  5634       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  }

  5635     hence "(\<Sum>i\<in>Basis. \<bar>x \<bullet> i\<bar>) \<le> ?b" apply-apply(rule setsum_mono) by auto

  5636     hence "norm x \<le> ?b" using norm_le_l1[of x] by auto  }

  5637   thus ?thesis unfolding interval and bounded_iff by auto

  5638 qed

  5639

  5640 lemma bounded_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5641  "bounded {a .. b} \<and> bounded {a<..<b}"

  5642   using bounded_closed_interval[of a b]

  5643   using interval_open_subset_closed[of a b]

  5644   using bounded_subset[of "{a..b}" "{a<..<b}"]

  5645   by simp

  5646

  5647 lemma not_interval_univ: fixes a :: "'a::ordered_euclidean_space" shows

  5648  "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"

  5649   using bounded_interval[of a b] by auto

  5650

  5651 lemma compact_interval: fixes a :: "'a::ordered_euclidean_space" shows "compact {a .. b}"

  5652   using bounded_closed_imp_seq_compact[of "{a..b}"] using bounded_interval[of a b]

  5653   by (auto simp: compact_eq_seq_compact_metric)

  5654

  5655 lemma open_interval_midpoint: fixes a :: "'a::ordered_euclidean_space"

  5656   assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"

  5657 proof-

  5658   { fix i :: 'a assume "i\<in>Basis"

  5659     hence "a \<bullet> i < ((1 / 2) *\<^sub>R (a + b)) \<bullet> i \<and> ((1 / 2) *\<^sub>R (a + b)) \<bullet> i < b \<bullet> i"

  5660       using assms[unfolded interval_ne_empty, THEN bspec[where x=i]] by (auto simp: inner_add_left)  }

  5661   thus ?thesis unfolding mem_interval by auto

  5662 qed

  5663

  5664 lemma open_closed_interval_convex: fixes x :: "'a::ordered_euclidean_space"

  5665   assumes x:"x \<in> {a<..<b}" and y:"y \<in> {a .. b}" and e:"0 < e" "e \<le> 1"

  5666   shows "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<in> {a<..<b}"

  5667 proof-

  5668   { fix i :: 'a assume i:"i\<in>Basis"

  5669     have "a \<bullet> i = e * (a \<bullet> i) + (1 - e) * (a \<bullet> i)" unfolding left_diff_distrib by simp

  5670     also have "\<dots> < e * (x \<bullet> i) + (1 - e) * (y \<bullet> i)" apply(rule add_less_le_mono)

  5671       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all

  5672       using x unfolding mem_interval using i apply simp

  5673       using y unfolding mem_interval using i apply simp

  5674       done

  5675     finally have "a \<bullet> i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i" unfolding inner_simps by auto

  5676     moreover {

  5677     have "b \<bullet> i = e * (b\<bullet>i) + (1 - e) * (b\<bullet>i)" unfolding left_diff_distrib by simp

  5678     also have "\<dots> > e * (x \<bullet> i) + (1 - e) * (y \<bullet> i)" apply(rule add_less_le_mono)

  5679       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all

  5680       using x unfolding mem_interval using i apply simp

  5681       using y unfolding mem_interval using i apply simp

  5682       done

  5683     finally have "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i < b \<bullet> i" unfolding inner_simps by auto

  5684     } 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 }

  5685   thus ?thesis unfolding mem_interval by auto

  5686 qed

  5687

  5688 lemma closure_open_interval: fixes a :: "'a::ordered_euclidean_space"

  5689   assumes "{a<..<b} \<noteq> {}"

  5690   shows "closure {a<..<b} = {a .. b}"

  5691 proof-

  5692   have ab:"a < b" using assms[unfolded interval_ne_empty] apply(subst eucl_less) by auto

  5693   let ?c = "(1 / 2) *\<^sub>R (a + b)"

  5694   { fix x assume as:"x \<in> {a .. b}"

  5695     def f == "\<lambda>n::nat. x + (inverse (real n + 1)) *\<^sub>R (?c - x)"

  5696     { fix n assume fn:"f n < b \<longrightarrow> a < f n \<longrightarrow> f n = x" and xc:"x \<noteq> ?c"

  5697       have *:"0 < inverse (real n + 1)" "inverse (real n + 1) \<le> 1" unfolding inverse_le_1_iff by auto

  5698       have "(inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b)) + (1 - inverse (real n + 1)) *\<^sub>R x =

  5699         x + (inverse (real n + 1)) *\<^sub>R (((1 / 2) *\<^sub>R (a + b)) - x)"

  5700         by (auto simp add: algebra_simps)

  5701       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

  5702       hence False using fn unfolding f_def using xc by auto  }

  5703     moreover

  5704     { assume "\<not> (f ---> x) sequentially"

  5705       { fix e::real assume "e>0"

  5706         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

  5707         then obtain N::nat where "inverse (real (N + 1)) < e" by auto

  5708         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)

  5709         hence "\<exists>N::nat. \<forall>n\<ge>N. inverse (real n + 1) < e" by auto  }

  5710       hence "((\<lambda>n. inverse (real n + 1)) ---> 0) sequentially"

  5711         unfolding LIMSEQ_def by(auto simp add: dist_norm)

  5712       hence "(f ---> x) sequentially" unfolding f_def

  5713         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]

  5714         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  }

  5715     ultimately have "x \<in> closure {a<..<b}"

  5716       using as and open_interval_midpoint[OF assms] unfolding closure_def unfolding islimpt_sequential by(cases "x=?c")auto  }

  5717   thus ?thesis using closure_minimal[OF interval_open_subset_closed closed_interval, of a b] by blast

  5718 qed

  5719

  5720 lemma bounded_subset_open_interval_symmetric: fixes s::"('a::ordered_euclidean_space) set"

  5721   assumes "bounded s"  shows "\<exists>a. s \<subseteq> {-a<..<a}"

  5722 proof-

  5723   obtain b where "b>0" and b:"\<forall>x\<in>s. norm x \<le> b" using assms[unfolded bounded_pos] by auto

  5724   def a \<equiv> "(\<Sum>i\<in>Basis. (b + 1) *\<^sub>R i)::'a"

  5725   { fix x assume "x\<in>s"

  5726     fix i :: 'a assume i:"i\<in>Basis"

  5727     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]

  5728       and Basis_le_norm[OF i, of x] unfolding inner_simps and a_def by auto }

  5729   thus ?thesis by(auto intro: exI[where x=a] simp add: eucl_less[where 'a='a])

  5730 qed

  5731

  5732 lemma bounded_subset_open_interval:

  5733   fixes s :: "('a::ordered_euclidean_space) set"
`