src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
 author hoelzl Tue Mar 05 15:43:15 2013 +0100 (2013-03-05) changeset 51344 b3d246c92dfb parent 51343 b61b32f62c78 child 51345 e7dd577cb053 permissions -rw-r--r--
continuity of pair operations
     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 class first_countable_topology = topological_space +

   185   assumes first_countable_basis:

   186     "\<exists>A. countable A \<and> (\<forall>a\<in>A. x \<in> a \<and> open a) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>a\<in>A. a \<subseteq> S))"

   187

   188 lemma (in first_countable_topology) countable_basis_at_decseq:

   189   obtains A :: "nat \<Rightarrow> 'a set" where

   190     "\<And>i. open (A i)" "\<And>i. x \<in> (A i)"

   191     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"

   192 proof atomize_elim

   193   from first_countable_basis[of x] obtain A

   194     where "countable A"

   195     and nhds: "\<And>a. a \<in> A \<Longrightarrow> open a" "\<And>a. a \<in> A \<Longrightarrow> x \<in> a"

   196     and incl: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>a\<in>A. a \<subseteq> S"  by auto

   197   then have "A \<noteq> {}" by auto

   198   with countable A have r: "A = range (from_nat_into A)" by auto

   199   def F \<equiv> "\<lambda>n. \<Inter>i\<le>n. from_nat_into A i"

   200   show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and>

   201       (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially)"

   202   proof (safe intro!: exI[of _ F])

   203     fix i

   204     show "open (F i)" using nhds(1) r by (auto simp: F_def intro!: open_INT)

   205     show "x \<in> F i" using nhds(2) r by (auto simp: F_def)

   206   next

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

   208     from incl[OF this] obtain i where "F i \<subseteq> S"

   209       by (subst (asm) r) (auto simp: F_def)

   210     moreover have "\<And>j. i \<le> j \<Longrightarrow> F j \<subseteq> F i"

   211       by (auto simp: F_def)

   212     ultimately show "eventually (\<lambda>i. F i \<subseteq> S) sequentially"

   213       by (auto simp: eventually_sequentially)

   214   qed

   215 qed

   216

   217 lemma (in first_countable_topology) first_countable_basisE:

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

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

   220   using first_countable_basis[of x]

   221   by atomize_elim auto

   222

   223 lemma (in first_countable_topology) first_countable_basis_Int_stableE:

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

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

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

   227 proof atomize_elim

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

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

   230   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>

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

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

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

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

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

   236   next

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

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

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

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

   241   next

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

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

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

   245   qed

   246 qed

   247

   248 instance prod :: (first_countable_topology, first_countable_topology) first_countable_topology

   249 proof

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

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

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

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

   254   proof (intro exI[of _ "(\<lambda>(a, b). a \<times> b)  (A \<times> B)"], safe)

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

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

   257       unfolding mem_Times_iff by (auto intro: open_Times)

   258   next

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

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

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

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

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

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

   265   qed (simp add: A B)

   266 qed

   267

   268 instance metric_space \<subseteq> first_countable_topology

   269 proof

   270   fix x :: 'a

   271   show "\<exists>A. countable A \<and> (\<forall>a\<in>A. x \<in> a \<and> open a) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>a\<in>A. a \<subseteq> S))"

   272   proof (intro exI, safe)

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

   274     then obtain r where "0 < r" "{y. dist x y < r} \<subseteq> S"

   275       by (auto simp: open_dist dist_commute subset_eq)

   276     moreover from reals_Archimedean[OF 0 < r] guess n ..

   277     moreover

   278     then have "{y. dist x y < inverse (Suc n)} \<subseteq> {y. dist x y < r}"

   279       by (auto simp: inverse_eq_divide)

   280     ultimately show "\<exists>a\<in>range (\<lambda>n. {y. dist x y < inverse (Suc n)}). a \<subseteq> S"

   281       by auto

   282   qed (auto intro: open_ball)

   283 qed

   284

   285 class second_countable_topology = topological_space +

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

   287 begin

   288

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

   290 proof -

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

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

   293

   294   show ?thesis

   295   proof (intro exI conjI)

   296     show "countable ?B"

   297       by (intro countable_image countable_Collect_finite_subset B)

   298     { fix S assume "open S"

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

   300         unfolding B

   301       proof induct

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

   303       next

   304         case (Int a b)

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

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

   307           by blast

   308         show ?case

   309           unfolding x y Int_UN_distrib2

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

   311       next

   312         case (UN K)

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

   314         then guess k unfolding bchoice_iff ..

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

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

   317       next

   318         case (Basis S) then show ?case

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

   320       qed

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

   322         unfolding subset_image_iff by blast }

   323     then show "topological_basis ?B"

   324       unfolding topological_space_class.topological_basis_def

   325       by (safe intro!: topological_space_class.open_Inter)

   326          (simp_all add: B generate_topology.Basis subset_eq)

   327   qed

   328 qed

   329

   330 end

   331

   332 sublocale second_countable_topology <

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

   334   using someI_ex[OF ex_countable_basis]

   335   by unfold_locales safe

   336

   337 instance prod :: (second_countable_topology, second_countable_topology) second_countable_topology

   338 proof

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

   340     using ex_countable_basis by auto

   341   moreover

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

   343     using ex_countable_basis by auto

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

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

   346       topological_basis_imp_subbasis)

   347 qed

   348

   349 instance second_countable_topology \<subseteq> first_countable_topology

   350 proof

   351   fix x :: 'a

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

   353   then have B: "countable B" "topological_basis B"

   354     using countable_basis is_basis

   355     by (auto simp: countable_basis is_basis)

   356   then show "\<exists>A. countable A \<and> (\<forall>a\<in>A. x \<in> a \<and> open a) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>a\<in>A. a \<subseteq> S))"

   357     by (intro exI[of _ "{b\<in>B. x \<in> b}"])

   358        (fastforce simp: topological_space_class.topological_basis_def)

   359 qed

   360

   361 subsection {* Polish spaces *}

   362

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

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

   365

   366 class polish_space = complete_space + second_countable_topology

   367

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

   369

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

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

   372   morphisms "openin" "topology"

   373   unfolding istopology_def by blast

   374

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

   376   using openin[of U] by blast

   377

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

   379   using topology_inverse[unfolded mem_Collect_eq] .

   380

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

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

   383

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

   385 proof-

   386   { assume "T1=T2"

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

   388   moreover

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

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

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

   392     hence "T1 = T2" unfolding openin_inverse .

   393   }

   394   ultimately show ?thesis by blast

   395 qed

   396

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

   398

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

   400

   401 subsubsection {* Main properties of open sets *}

   402

   403 lemma openin_clauses:

   404   fixes U :: "'a topology"

   405   shows "openin U {}"

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

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

   408   using openin[of U] unfolding istopology_def mem_Collect_eq

   409   by fast+

   410

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

   412   unfolding topspace_def by blast

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

   414

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

   416   using openin_clauses by simp

   417

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

   419   using openin_clauses by simp

   420

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

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

   423

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

   425

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

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

   428 proof

   429   assume ?lhs

   430   then show ?rhs by auto

   431 next

   432   assume H: ?rhs

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

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

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

   436   finally show "openin U S" .

   437 qed

   438

   439

   440 subsubsection {* Closed sets *}

   441

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

   443

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

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

   446 lemma closedin_topspace[intro,simp]:

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

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

   449   by (auto simp add: Diff_Un closedin_def)

   450

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

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

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

   454

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

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

   457

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

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

   460   apply (auto simp add: closedin_def Diff_Diff_Int inf_absorb2)

   461   apply (metis openin_subset subset_eq)

   462   done

   463

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

   465   by (simp add: openin_closedin_eq)

   466

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

   468 proof-

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

   470     by (auto simp add: topspace_def openin_subset)

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

   472 qed

   473

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

   475 proof-

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

   477     by (auto simp add: topspace_def )

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

   479 qed

   480

   481 subsubsection {* Subspace topology *}

   482

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

   484

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

   486   (is "istopology ?L")

   487 proof-

   488   have "?L {}" by blast

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

   490     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

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

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

   493   moreover

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

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

   496       apply (rule set_eqI)

   497       apply (simp add: Ball_def image_iff)

   498       by metis

   499     from K[unfolded th0 subset_image_iff]

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

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

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

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

   504   ultimately show ?thesis

   505     unfolding subset_eq mem_Collect_eq istopology_def by blast

   506 qed

   507

   508 lemma openin_subtopology:

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

   510   unfolding subtopology_def topology_inverse'[OF istopology_subtopology]

   511   by auto

   512

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

   514   by (auto simp add: topspace_def openin_subtopology)

   515

   516 lemma closedin_subtopology:

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

   518   unfolding closedin_def topspace_subtopology

   519   apply (simp add: openin_subtopology)

   520   apply (rule iffI)

   521   apply clarify

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

   523   by auto

   524

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

   526   unfolding openin_subtopology

   527   apply (rule iffI, clarify)

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

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

   530   apply auto

   531   done

   532

   533 lemma subtopology_superset:

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

   535   shows "subtopology U V = U"

   536 proof-

   537   {fix S

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

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

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

   541     moreover

   542     {assume S: "openin U S"

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

   544         using openin_subset[OF S] UV by auto}

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

   546   then show ?thesis unfolding topology_eq openin_subtopology by blast

   547 qed

   548

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

   550   by (simp add: subtopology_superset)

   551

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

   553   by (simp add: subtopology_superset)

   554

   555 subsubsection {* The standard Euclidean topology *}

   556

   557 definition

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

   559   "euclidean = topology open"

   560

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

   562   unfolding euclidean_def

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

   564   apply (rule topology_inverse[symmetric])

   565   apply (auto simp add: istopology_def)

   566   done

   567

   568 lemma topspace_euclidean: "topspace euclidean = UNIV"

   569   apply (simp add: topspace_def)

   570   apply (rule set_eqI)

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

   572

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

   574   by (simp add: topspace_euclidean topspace_subtopology)

   575

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

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

   578

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

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

   581

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

   583

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

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

   586

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

   588   by (auto simp add: openin_open)

   589

   590 lemma open_openin_trans[trans]:

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

   592   by (metis Int_absorb1  openin_open_Int)

   593

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

   595   by (auto simp add: openin_open)

   596

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

   598   by (simp add: closedin_subtopology closed_closedin Int_ac)

   599

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

   601   by (metis closedin_closed)

   602

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

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

   605   apply auto

   606   apply (frule closedin_closed_Int[of T S])

   607   by simp

   608

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

   610   by (auto simp add: closedin_closed)

   611

   612 lemma openin_euclidean_subtopology_iff:

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

   614   shows "openin (subtopology euclidean U) S

   615   \<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")

   616 proof

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

   618 next

   619   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}"

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

   621     unfolding T_def

   622     apply clarsimp

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

   624     apply (clarsimp simp add: less_diff_eq)

   625     apply (erule rev_bexI)

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

   627     apply (erule le_less_trans [OF dist_triangle])

   628     done

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

   630     unfolding T_def

   631     apply auto

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

   633     apply auto

   634     done

   635   from 1 2 show ?lhs

   636     unfolding openin_open open_dist by fast

   637 qed

   638

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

   640

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

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

   643   unfolding open_openin openin_open by blast

   644

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

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

   647

   648 lemma closedin_trans[trans]:

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

   650            closedin (subtopology euclidean U) T

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

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

   653

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

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

   656

   657

   658 subsection {* Open and closed balls *}

   659

   660 definition

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

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

   663

   664 definition

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

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

   667

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

   669   by (simp add: ball_def)

   670

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

   672   by (simp add: cball_def)

   673

   674 lemma mem_ball_0:

   675   fixes x :: "'a::real_normed_vector"

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

   677   by (simp add: dist_norm)

   678

   679 lemma mem_cball_0:

   680   fixes x :: "'a::real_normed_vector"

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

   682   by (simp add: dist_norm)

   683

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

   685   by simp

   686

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

   688   by simp

   689

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

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

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

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

   694   by (simp add: set_eq_iff) arith

   695

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

   697   by (simp add: set_eq_iff)

   698

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

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

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

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

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

   704

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

   706   unfolding open_dist ball_def mem_Collect_eq Ball_def

   707   unfolding dist_commute

   708   apply clarify

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

   710   using dist_triangle_alt[where z=x]

   711   apply (clarsimp simp add: diff_less_iff)

   712   apply atomize

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

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

   715   by arith

   716

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

   718   unfolding open_dist subset_eq mem_ball Ball_def dist_commute ..

   719

   720 lemma openE[elim?]:

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

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

   723   using assms unfolding open_contains_ball by auto

   724

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

   726   by (metis open_contains_ball subset_eq centre_in_ball)

   727

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

   729   unfolding mem_ball set_eq_iff

   730   apply (simp add: not_less)

   731   by (metis zero_le_dist order_trans dist_self)

   732

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

   734

   735 lemma euclidean_dist_l2:

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

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

   738   unfolding dist_norm norm_eq_sqrt_inner setL2_def

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

   740

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

   742

   743 lemma rational_boxes:

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

   745   assumes "0 < e"

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

   747 proof -

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

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

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

   751   proof

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

   753   qed

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

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

   756   proof

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

   758   qed

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

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

   761   show ?thesis

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

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

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

   765       unfolding setL2_def[symmetric] by (rule euclidean_dist_l2)

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

   767     proof (rule real_sqrt_less_mono, rule setsum_strict_mono)

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

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

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

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

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

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

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

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

   776         by (rule power_strict_mono) auto

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

   778         by (simp add: power_divide)

   779     qed auto

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

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

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

   783 qed

   784

   785 lemma open_UNION_box:

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

   787   assumes "open M"

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

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

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

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

   792 proof safe

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

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

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

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

   797     "\<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"

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

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

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

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

   802 qed (auto simp: I_def)

   803

   804 subsection{* Connectedness *}

   805

   806 definition "connected S \<longleftrightarrow>

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

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

   809

   810 lemma connected_local:

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

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

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

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

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

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

   817                  ~(e2 = {}))"

   818 unfolding connected_def openin_open by (safe, blast+)

   819

   820 lemma exists_diff:

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

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

   823 proof-

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

   825   moreover

   826   {fix S assume H: "P S"

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

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

   829   ultimately show ?thesis by metis

   830 qed

   831

   832 lemma connected_clopen: "connected S \<longleftrightarrow>

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

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

   835 proof-

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

   837     unfolding connected_def openin_open closedin_closed

   838     apply (subst exists_diff) by blast

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

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

   841

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

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

   844     unfolding connected_def openin_open closedin_closed by auto

   845   {fix e2

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

   847         by auto}

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

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

   850   then show ?thesis unfolding th0 th1 by simp

   851 qed

   852

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

   854   by (simp add: connected_def)

   855

   856

   857 subsection{* Limit points *}

   858

   859 definition (in topological_space)

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

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

   862

   863 lemma islimptI:

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

   865   shows "x islimpt S"

   866   using assms unfolding islimpt_def by auto

   867

   868 lemma islimptE:

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

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

   871   using assms unfolding islimpt_def by auto

   872

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

   874   unfolding islimpt_def eventually_at_topological by auto

   875

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

   877   unfolding islimpt_def by fast

   878

   879 lemma islimpt_approachable:

   880   fixes x :: "'a::metric_space"

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

   882   unfolding islimpt_iff_eventually eventually_at by fast

   883

   884 lemma islimpt_approachable_le:

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

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

   887   unfolding islimpt_approachable

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

   889     THEN arg_cong [where f=Not]]

   890   by (simp add: Bex_def conj_commute conj_left_commute)

   891

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

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

   894

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

   896

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

   898   unfolding islimpt_UNIV_iff by (rule not_open_singleton)

   899

   900 lemma perfect_choose_dist:

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

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

   903 using islimpt_UNIV [of x]

   904 by (simp add: islimpt_approachable)

   905

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

   907   unfolding closed_def

   908   apply (subst open_subopen)

   909   apply (simp add: islimpt_def subset_eq)

   910   by (metis ComplE ComplI)

   911

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

   913   unfolding islimpt_def by auto

   914

   915 lemma finite_set_avoid:

   916   fixes a :: "'a::metric_space"

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

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

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

   920 next

   921   case (2 x F)

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

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

   924   moreover

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

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

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

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

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

   930   ultimately show ?case by blast

   931 qed

   932

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

   934   by (simp add: islimpt_iff_eventually eventually_conj_iff)

   935

   936 lemma discrete_imp_closed:

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

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

   939   shows "closed S"

   940 proof-

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

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

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

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

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

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

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

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

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

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

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

   952 qed

   953

   954

   955 subsection {* Interior of a Set *}

   956

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

   958

   959 lemma interiorI [intro?]:

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

   961   shows "x \<in> interior S"

   962   using assms unfolding interior_def by fast

   963

   964 lemma interiorE [elim?]:

   965   assumes "x \<in> interior S"

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

   967   using assms unfolding interior_def by fast

   968

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

   970   by (simp add: interior_def open_Union)

   971

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

   973   by (auto simp add: interior_def)

   974

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

   976   by (auto simp add: interior_def)

   977

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

   979   by (intro equalityI interior_subset interior_maximal subset_refl)

   980

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

   982   by (metis open_interior interior_open)

   983

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

   985   by (metis interior_maximal interior_subset subset_trans)

   986

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

   988   using open_empty by (rule interior_open)

   989

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

   991   using open_UNIV by (rule interior_open)

   992

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

   994   using open_interior by (rule interior_open)

   995

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

   997   by (auto simp add: interior_def)

   998

   999 lemma interior_unique:

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

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

  1002   shows "interior S = T"

  1003   by (intro equalityI assms interior_subset open_interior interior_maximal)

  1004

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

  1006   by (intro equalityI Int_mono Int_greatest interior_mono Int_lower1

  1007     Int_lower2 interior_maximal interior_subset open_Int open_interior)

  1008

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

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

  1011   by (simp add: open_subset_interior)

  1012

  1013 lemma interior_limit_point [intro]:

  1014   fixes x :: "'a::perfect_space"

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

  1016   using x islimpt_UNIV [of x]

  1017   unfolding interior_def islimpt_def

  1018   apply (clarsimp, rename_tac T T')

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

  1020   apply (auto simp add: open_Int)

  1021   done

  1022

  1023 lemma interior_closed_Un_empty_interior:

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

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

  1026 proof

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

  1028     by (rule interior_mono, rule Un_upper1)

  1029 next

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

  1031   proof

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

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

  1034     show "x \<in> interior S"

  1035     proof (rule ccontr)

  1036       assume "x \<notin> interior S"

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

  1038         unfolding interior_def by fast

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

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

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

  1042       show "False" unfolding interior_def by fast

  1043     qed

  1044   qed

  1045 qed

  1046

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

  1048 proof (rule interior_unique)

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

  1050     by (intro Sigma_mono interior_subset)

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

  1052     by (intro open_Times open_interior)

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

  1054   proof (safe)

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

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

  1057       using open T unfolding open_prod_def by fast

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

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

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

  1061       by (auto intro: interiorI)

  1062   qed

  1063 qed

  1064

  1065

  1066 subsection {* Closure of a Set *}

  1067

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

  1069

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

  1071   unfolding interior_def closure_def islimpt_def by auto

  1072

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

  1074   unfolding interior_closure by simp

  1075

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

  1077   unfolding closure_interior by (simp add: closed_Compl)

  1078

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

  1080   unfolding closure_def by simp

  1081

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

  1083   unfolding hull_def closure_interior interior_def by auto

  1084

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

  1086   unfolding closure_hull using closed_Inter by (rule hull_eq)

  1087

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

  1089   unfolding closure_eq .

  1090

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

  1092   unfolding closure_hull by (rule hull_hull)

  1093

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

  1095   unfolding closure_hull by (rule hull_mono)

  1096

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

  1098   unfolding closure_hull by (rule hull_minimal)

  1099

  1100 lemma closure_unique:

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

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

  1103   shows "closure S = T"

  1104   using assms unfolding closure_hull by (rule hull_unique)

  1105

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

  1107   using closed_empty by (rule closure_closed)

  1108

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

  1110   using closed_UNIV by (rule closure_closed)

  1111

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

  1113   unfolding closure_interior by simp

  1114

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

  1116   using closure_empty closure_subset[of S]

  1117   by blast

  1118

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

  1120   using closure_eq[of S] closure_subset[of S]

  1121   by simp

  1122

  1123 lemma open_inter_closure_eq_empty:

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

  1125   using open_subset_interior[of S "- T"]

  1126   using interior_subset[of "- T"]

  1127   unfolding closure_interior

  1128   by auto

  1129

  1130 lemma open_inter_closure_subset:

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

  1132 proof

  1133   fix x

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

  1135   { assume *:"x islimpt T"

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

  1137     proof (rule islimptI)

  1138       fix A

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

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

  1141         by (simp_all add: open_Int)

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

  1143         by (rule islimptE)

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

  1145         by simp_all

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

  1147     qed

  1148   }

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

  1150     unfolding closure_def

  1151     by blast

  1152 qed

  1153

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

  1155   unfolding closure_interior by simp

  1156

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

  1158   unfolding closure_interior by simp

  1159

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

  1161 proof (rule closure_unique)

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

  1163     by (intro Sigma_mono closure_subset)

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

  1165     by (intro closed_Times closed_closure)

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

  1167     apply (simp add: closed_def open_prod_def, clarify)

  1168     apply (rule ccontr)

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

  1170     apply (simp add: closure_interior interior_def)

  1171     apply (drule_tac x=C in spec)

  1172     apply (drule_tac x=D in spec)

  1173     apply auto

  1174     done

  1175 qed

  1176

  1177

  1178 subsection {* Frontier (aka boundary) *}

  1179

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

  1181

  1182 lemma frontier_closed: "closed(frontier S)"

  1183   by (simp add: frontier_def closed_Diff)

  1184

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

  1186   by (auto simp add: frontier_def interior_closure)

  1187

  1188 lemma frontier_straddle:

  1189   fixes a :: "'a::metric_space"

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

  1191   unfolding frontier_def closure_interior

  1192   by (auto simp add: mem_interior subset_eq ball_def)

  1193

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

  1195   by (metis frontier_def closure_closed Diff_subset)

  1196

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

  1198   by (simp add: frontier_def)

  1199

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

  1201 proof-

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

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

  1204     hence "closed S" using closure_subset_eq by auto

  1205   }

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

  1207 qed

  1208

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

  1210   by (auto simp add: frontier_def closure_complement interior_complement)

  1211

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

  1213   using frontier_complement frontier_subset_eq[of "- S"]

  1214   unfolding open_closed by auto

  1215

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

  1217

  1218 definition

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

  1220     (infixr "indirection" 70) where

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

  1222

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

  1224

  1225 lemma trivial_limit_within:

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

  1227 proof

  1228   assume "trivial_limit (at a within S)"

  1229   thus "\<not> a islimpt S"

  1230     unfolding trivial_limit_def

  1231     unfolding eventually_within eventually_at_topological

  1232     unfolding islimpt_def

  1233     apply (clarsimp simp add: set_eq_iff)

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

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

  1236     done

  1237 next

  1238   assume "\<not> a islimpt S"

  1239   thus "trivial_limit (at a within S)"

  1240     unfolding trivial_limit_def

  1241     unfolding eventually_within eventually_at_topological

  1242     unfolding islimpt_def

  1243     apply clarsimp

  1244     apply (rule_tac x=T in exI)

  1245     apply auto

  1246     done

  1247 qed

  1248

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

  1250   using trivial_limit_within [of a UNIV] by simp

  1251

  1252 lemma trivial_limit_at:

  1253   fixes a :: "'a::perfect_space"

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

  1255   by (rule at_neq_bot)

  1256

  1257 lemma trivial_limit_at_infinity:

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

  1259   unfolding trivial_limit_def eventually_at_infinity

  1260   apply clarsimp

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

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

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

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

  1265   done

  1266

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

  1268

  1269 lemma eventually_at: (* FIXME: this replaces Limits.eventually_at *)

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

  1271 unfolding eventually_at dist_nz by auto

  1272

  1273 lemma eventually_within: (* FIXME: this replaces Limits.eventually_within *)

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

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

  1276   by (rule eventually_within_less)

  1277

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

  1279   unfolding trivial_limit_def

  1280   by (auto elim: eventually_rev_mp)

  1281

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

  1283   by simp

  1284

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

  1286   by (simp add: filter_eq_iff)

  1287

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

  1289

  1290 lemma eventually_rev_mono:

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

  1292 using eventually_mono [of P Q] by fast

  1293

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

  1295   by (simp add: eventually_False)

  1296

  1297

  1298 subsection {* Limits *}

  1299

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

  1301

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

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

  1304

  1305 lemma Lim:

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

  1307         trivial_limit net \<or>

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

  1309   unfolding tendsto_iff trivial_limit_eq by auto

  1310

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

  1312

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

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

  1315   by (auto simp add: tendsto_iff eventually_within_le)

  1316

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

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

  1319   by (auto simp add: tendsto_iff eventually_within)

  1320

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

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

  1323   by (auto simp add: tendsto_iff eventually_at)

  1324

  1325 lemma Lim_at_infinity:

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

  1327   by (auto simp add: tendsto_iff eventually_at_infinity)

  1328

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

  1330   by (rule topological_tendstoI, auto elim: eventually_rev_mono)

  1331

  1332 text{* The expected monotonicity property. *}

  1333

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

  1335   unfolding tendsto_def Limits.eventually_within by simp

  1336

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

  1338   unfolding tendsto_def Limits.eventually_within

  1339   by (auto elim!: eventually_elim1)

  1340

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

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

  1343   using assms unfolding tendsto_def Limits.eventually_within

  1344   apply clarify

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

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

  1347   apply (auto elim: eventually_elim2)

  1348   done

  1349

  1350 lemma Lim_Un_univ:

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

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

  1353   by (metis Lim_Un within_UNIV)

  1354

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

  1356

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

  1358   (* FIXME: rename *)

  1359   unfolding tendsto_def Limits.eventually_within

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

  1361   by (auto elim!: eventually_elim1)

  1362

  1363 lemma eventually_within_interior:

  1364   assumes "x \<in> interior S"

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

  1366 proof-

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

  1368   { assume "?lhs"

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

  1370       unfolding Limits.eventually_within Limits.eventually_at_topological

  1371       by auto

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

  1373       by auto

  1374     then have "?rhs"

  1375       unfolding Limits.eventually_at_topological by auto

  1376   } moreover

  1377   { assume "?rhs" hence "?lhs"

  1378       unfolding Limits.eventually_within

  1379       by (auto elim: eventually_elim1)

  1380   } ultimately

  1381   show "?thesis" ..

  1382 qed

  1383

  1384 lemma at_within_interior:

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

  1386   by (simp add: filter_eq_iff eventually_within_interior)

  1387

  1388 lemma at_within_open:

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

  1390   by (simp only: at_within_interior interior_open)

  1391

  1392 lemma Lim_within_open:

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

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

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

  1396   using assms by (simp only: at_within_open)

  1397

  1398 lemma Lim_within_LIMSEQ:

  1399   fixes a :: "'a::metric_space"

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

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

  1402   using assms unfolding tendsto_def [where l=L]

  1403   by (simp add: sequentially_imp_eventually_within)

  1404

  1405 lemma Lim_right_bound:

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

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

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

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

  1410 proof cases

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

  1412 next

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

  1414   show ?thesis

  1415   proof (rule Lim_within_LIMSEQ, safe)

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

  1417

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

  1419     proof (rule LIMSEQ_I, rule ccontr)

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

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

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

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

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

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

  1426

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

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

  1429         using S bnd by (intro Inf_lower[where z=K]) auto

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

  1431         by (auto simp: not_less field_simps)

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

  1433       show False by auto

  1434     qed

  1435   qed

  1436 qed

  1437

  1438 text{* Another limit point characterization. *}

  1439

  1440 lemma islimpt_sequential:

  1441   fixes x :: "'a::first_countable_topology"

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

  1443     (is "?lhs = ?rhs")

  1444 proof

  1445   assume ?lhs

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

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

  1448   { fix n

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

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

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

  1452       unfolding f_def by (rule someI_ex)

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

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

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

  1456   proof (rule topological_tendstoI)

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

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

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

  1460   qed

  1461   ultimately show ?rhs by fast

  1462 next

  1463   assume ?rhs

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

  1465   show ?lhs

  1466     unfolding islimpt_def

  1467   proof safe

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

  1469     from lim[THEN topological_tendstoD, OF this] f

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

  1471       unfolding eventually_sequentially by auto

  1472   qed

  1473 qed

  1474

  1475 lemma Lim_inv: (* TODO: delete *)

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

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

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

  1479   unfolding o_def using assms by (rule tendsto_inverse)

  1480

  1481 lemma Lim_null:

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

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

  1484   by (simp add: Lim dist_norm)

  1485

  1486 lemma Lim_null_comparison:

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

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

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

  1490 proof (rule metric_tendsto_imp_tendsto)

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

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

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

  1494 qed

  1495

  1496 lemma Lim_transform_bound:

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

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

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

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

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

  1502   by (rule Lim_null_comparison)

  1503

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

  1505

  1506 lemma Lim_in_closed_set:

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

  1508   shows "l \<in> S"

  1509 proof (rule ccontr)

  1510   assume "l \<notin> S"

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

  1512     by (simp_all add: open_Compl)

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

  1514     by (rule topological_tendstoD)

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

  1516     by (rule eventually_elim2) simp

  1517   with assms(3) show "False"

  1518     by (simp add: eventually_False)

  1519 qed

  1520

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

  1522

  1523 lemma Lim_dist_ubound:

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

  1525   shows "dist a l <= e"

  1526 proof-

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

  1528   proof (rule Lim_in_closed_set)

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

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

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

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

  1533   qed

  1534   thus ?thesis by simp

  1535 qed

  1536

  1537 lemma Lim_norm_ubound:

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

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

  1540   shows "norm(l) <= e"

  1541 proof-

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

  1543   proof (rule Lim_in_closed_set)

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

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

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

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

  1548   qed

  1549   thus ?thesis by simp

  1550 qed

  1551

  1552 lemma Lim_norm_lbound:

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

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

  1555   shows "e \<le> norm l"

  1556 proof-

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

  1558   proof (rule Lim_in_closed_set)

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

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

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

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

  1563   qed

  1564   thus ?thesis by simp

  1565 qed

  1566

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

  1568

  1569 lemma tendsto_Lim:

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

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

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

  1573

  1574 text{* Limit under bilinear function *}

  1575

  1576 lemma Lim_bilinear:

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

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

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

  1580 by (rule bounded_bilinear.tendsto)

  1581

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

  1583

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

  1585   unfolding id_def by (rule tendsto_ident_at_within)

  1586

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

  1588   unfolding id_def by (rule tendsto_ident_at)

  1589

  1590 lemma Lim_at_zero:

  1591   fixes a :: "'a::real_normed_vector"

  1592   fixes l :: "'b::topological_space"

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

  1594   using LIM_offset_zero LIM_offset_zero_cancel ..

  1595

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

  1597

  1598 definition

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

  1600   "netlimit net = (SOME a. ((\<lambda>x. x) ---> a) net)"

  1601

  1602 lemma netlimit_within:

  1603   assumes "\<not> trivial_limit (at a within S)"

  1604   shows "netlimit (at a within S) = a"

  1605 unfolding netlimit_def

  1606 apply (rule some_equality)

  1607 apply (rule Lim_at_within)

  1608 apply (rule tendsto_ident_at)

  1609 apply (erule tendsto_unique [OF assms])

  1610 apply (rule Lim_at_within)

  1611 apply (rule tendsto_ident_at)

  1612 done

  1613

  1614 lemma netlimit_at:

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

  1616   shows "netlimit (at a) = a"

  1617   using netlimit_within [of a UNIV] by simp

  1618

  1619 lemma lim_within_interior:

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

  1621   by (simp add: at_within_interior)

  1622

  1623 lemma netlimit_within_interior:

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

  1625   assumes "x \<in> interior S"

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

  1627 using assms by (simp add: at_within_interior netlimit_at)

  1628

  1629 text{* Transformation of limit. *}

  1630

  1631 lemma Lim_transform:

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

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

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

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

  1636

  1637 lemma Lim_transform_eventually:

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

  1639   apply (rule topological_tendstoI)

  1640   apply (drule (2) topological_tendstoD)

  1641   apply (erule (1) eventually_elim2, simp)

  1642   done

  1643

  1644 lemma Lim_transform_within:

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

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

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

  1648 proof (rule Lim_transform_eventually)

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

  1650     unfolding eventually_within

  1651     using assms(1,2) by auto

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

  1653 qed

  1654

  1655 lemma Lim_transform_at:

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

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

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

  1659 proof (rule Lim_transform_eventually)

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

  1661     unfolding eventually_at

  1662     using assms(1,2) by auto

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

  1664 qed

  1665

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

  1667

  1668 lemma Lim_transform_away_within:

  1669   fixes a b :: "'a::t1_space"

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

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

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

  1673 proof (rule Lim_transform_eventually)

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

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

  1676     unfolding Limits.eventually_within eventually_at_topological

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

  1678 qed

  1679

  1680 lemma Lim_transform_away_at:

  1681   fixes a b :: "'a::t1_space"

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

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

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

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

  1686   by simp

  1687

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

  1689

  1690 lemma Lim_transform_within_open:

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

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

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

  1694 proof (rule Lim_transform_eventually)

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

  1696     unfolding eventually_at_topological

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

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

  1699 qed

  1700

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

  1702

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

  1704

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

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

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

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

  1709   unfolding tendsto_def Limits.eventually_within eventually_at_topological

  1710   using assms by simp

  1711

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

  1713   assumes "a = b" "x = y"

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

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

  1716   unfolding tendsto_def eventually_at_topological

  1717   using assms by simp

  1718

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

  1720

  1721 lemma closure_sequential:

  1722   fixes l :: "'a::first_countable_topology"

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

  1724 proof

  1725   assume "?lhs" moreover

  1726   { assume "l \<in> S"

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

  1728   } moreover

  1729   { assume "l islimpt S"

  1730     hence "?rhs" unfolding islimpt_sequential by auto

  1731   } ultimately

  1732   show "?rhs" unfolding closure_def by auto

  1733 next

  1734   assume "?rhs"

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

  1736 qed

  1737

  1738 lemma closed_sequential_limits:

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

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

  1741   unfolding closed_limpt

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

  1743   by metis

  1744

  1745 lemma closure_approachable:

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

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

  1748   apply (auto simp add: closure_def islimpt_approachable)

  1749   by (metis dist_self)

  1750

  1751 lemma closed_approachable:

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

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

  1754   by (metis closure_closed closure_approachable)

  1755

  1756 subsection {* Infimum Distance *}

  1757

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

  1759

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

  1761   by (simp add: infdist_def)

  1762

  1763 lemma infdist_nonneg:

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

  1765   using assms by (auto simp add: infdist_def)

  1766

  1767 lemma infdist_le:

  1768   assumes "a \<in> A"

  1769   assumes "d = dist x a"

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

  1771   using assms by (auto intro!: SupInf.Inf_lower[where z=0] simp add: infdist_def)

  1772

  1773 lemma infdist_zero[simp]:

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

  1775 proof -

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

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

  1778 qed

  1779

  1780 lemma infdist_triangle:

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

  1782 proof cases

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

  1784 next

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

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

  1787   proof

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

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

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

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

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

  1793     proof (rule Inf_lower2)

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

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

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

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

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

  1799     qed

  1800   qed

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

  1802   proof (rule Inf_eq, safe)

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

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

  1805   next

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

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

  1808       by (intro Inf_greatest) (auto simp: field_simps)

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

  1810   qed

  1811   finally show ?thesis by simp

  1812 qed

  1813

  1814 lemma

  1815   in_closure_iff_infdist_zero:

  1816   assumes "A \<noteq> {}"

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

  1818 proof

  1819   assume "x \<in> closure A"

  1820   show "infdist x A = 0"

  1821   proof (rule ccontr)

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

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

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

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

  1826         eucl_less_not_refl euclidean_trans(2) infdist_le)

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

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

  1829   qed

  1830 next

  1831   assume x: "infdist x A = 0"

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

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

  1834   proof (safe, rule ccontr)

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

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

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

  1838       unfolding infdist_def

  1839       by (force simp: dist_commute)

  1840     with x 0 < e show False by auto

  1841   qed

  1842 qed

  1843

  1844 lemma

  1845   in_closed_iff_infdist_zero:

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

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

  1848 proof -

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

  1850     by (rule in_closure_iff_infdist_zero) fact

  1851   with assms show ?thesis by simp

  1852 qed

  1853

  1854 lemma tendsto_infdist [tendsto_intros]:

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

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

  1857 proof (rule tendstoI)

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

  1859   from tendstoD[OF f this]

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

  1861   proof (eventually_elim)

  1862     fix x

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

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

  1865       by (simp add: dist_commute dist_real_def)

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

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

  1868   qed

  1869 qed

  1870

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

  1872

  1873 lemma sequentially_offset:

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

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

  1876   using assms unfolding eventually_sequentially by (metis trans_le_add1)

  1877

  1878 lemma seq_offset:

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

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

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

  1882

  1883 lemma seq_offset_neg:

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

  1885   apply (rule topological_tendstoI)

  1886   apply (drule (2) topological_tendstoD)

  1887   apply (simp only: eventually_sequentially)

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

  1889   apply metis

  1890   by arith

  1891

  1892 lemma seq_offset_rev:

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

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

  1895

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

  1897   using LIMSEQ_inverse_real_of_nat by (rule LIMSEQ_imp_Suc)

  1898

  1899 subsection {* More properties of closed balls *}

  1900

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

  1902 unfolding cball_def closed_def

  1903 unfolding Collect_neg_eq [symmetric] not_le

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

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

  1906 apply (rename_tac x')

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

  1908 apply simp

  1909 done

  1910

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

  1912 proof-

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

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

  1915   } moreover

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

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

  1918   } ultimately

  1919   show ?thesis unfolding open_contains_ball by auto

  1920 qed

  1921

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

  1923   by (metis open_contains_cball subset_eq order_less_imp_le centre_in_cball)

  1924

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

  1926   apply (simp add: interior_def, safe)

  1927   apply (force simp add: open_contains_cball)

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

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

  1930   done

  1931

  1932 lemma islimpt_ball:

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

  1934   shows "y islimpt ball x e \<longleftrightarrow> 0 < e \<and> y \<in> cball x e" (is "?lhs = ?rhs")

  1935 proof

  1936   assume "?lhs"

  1937   { assume "e \<le> 0"

  1938     hence *:"ball x e = {}" using ball_eq_empty[of x e] by auto

  1939     have False using ?lhs unfolding * using islimpt_EMPTY[of y] by auto

  1940   }

  1941   hence "e > 0" by (metis not_less)

  1942   moreover

  1943   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

  1944   ultimately show "?rhs" by auto

  1945 next

  1946   assume "?rhs" hence "e>0"  by auto

  1947   { fix d::real assume "d>0"

  1948     have "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1949     proof(cases "d \<le> dist x y")

  1950       case True thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1951       proof(cases "x=y")

  1952         case True hence False using d \<le> dist x y d>0 by auto

  1953         thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d" by auto

  1954       next

  1955         case False

  1956

  1957         have "dist x (y - (d / (2 * dist y x)) *\<^sub>R (y - x))

  1958               = norm (x - y + (d / (2 * norm (y - x))) *\<^sub>R (y - x))"

  1959           unfolding mem_cball mem_ball dist_norm diff_diff_eq2 diff_add_eq[THEN sym] by auto

  1960         also have "\<dots> = \<bar>- 1 + d / (2 * norm (x - y))\<bar> * norm (x - y)"

  1961           using scaleR_left_distrib[of "- 1" "d / (2 * norm (y - x))", THEN sym, of "y - x"]

  1962           unfolding scaleR_minus_left scaleR_one

  1963           by (auto simp add: norm_minus_commute)

  1964         also have "\<dots> = \<bar>- norm (x - y) + d / 2\<bar>"

  1965           unfolding abs_mult_pos[of "norm (x - y)", OF norm_ge_zero[of "x - y"]]

  1966           unfolding distrib_right using x\<noteq>y[unfolded dist_nz, unfolded dist_norm] by auto

  1967         also have "\<dots> \<le> e - d/2" using d \<le> dist x y and d>0 and ?rhs by(auto simp add: dist_norm)

  1968         finally have "y - (d / (2 * dist y x)) *\<^sub>R (y - x) \<in> ball x e" using d>0 by auto

  1969

  1970         moreover

  1971

  1972         have "(d / (2*dist y x)) *\<^sub>R (y - x) \<noteq> 0"

  1973           using x\<noteq>y[unfolded dist_nz] d>0 unfolding scaleR_eq_0_iff by (auto simp add: dist_commute)

  1974         moreover

  1975         have "dist (y - (d / (2 * dist y x)) *\<^sub>R (y - x)) y < d" unfolding dist_norm apply simp unfolding norm_minus_cancel

  1976           using d>0 x\<noteq>y[unfolded dist_nz] dist_commute[of x y]

  1977           unfolding dist_norm by auto

  1978         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

  1979       qed

  1980     next

  1981       case False hence "d > dist x y" by auto

  1982       show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1983       proof(cases "x=y")

  1984         case True

  1985         obtain z where **: "z \<noteq> y" "dist z y < min e d"

  1986           using perfect_choose_dist[of "min e d" y]

  1987           using d > 0 e>0 by auto

  1988         show "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1989           unfolding x = y

  1990           using z \<noteq> y **

  1991           by (rule_tac x=z in bexI, auto simp add: dist_commute)

  1992       next

  1993         case False thus "\<exists>x'\<in>ball x e. x' \<noteq> y \<and> dist x' y < d"

  1994           using d>0 d > dist x y ?rhs by(rule_tac x=x in bexI, auto)

  1995       qed

  1996     qed  }

  1997   thus "?lhs" unfolding mem_cball islimpt_approachable mem_ball by auto

  1998 qed

  1999

  2000 lemma closure_ball_lemma:

  2001   fixes x y :: "'a::real_normed_vector"

  2002   assumes "x \<noteq> y" shows "y islimpt ball x (dist x y)"

  2003 proof (rule islimptI)

  2004   fix T assume "y \<in> T" "open T"

  2005   then obtain r where "0 < r" "\<forall>z. dist z y < r \<longrightarrow> z \<in> T"

  2006     unfolding open_dist by fast

  2007   (* choose point between x and y, within distance r of y. *)

  2008   def k \<equiv> "min 1 (r / (2 * dist x y))"

  2009   def z \<equiv> "y + scaleR k (x - y)"

  2010   have z_def2: "z = x + scaleR (1 - k) (y - x)"

  2011     unfolding z_def by (simp add: algebra_simps)

  2012   have "dist z y < r"

  2013     unfolding z_def k_def using 0 < r

  2014     by (simp add: dist_norm min_def)

  2015   hence "z \<in> T" using \<forall>z. dist z y < r \<longrightarrow> z \<in> T by simp

  2016   have "dist x z < dist x y"

  2017     unfolding z_def2 dist_norm

  2018     apply (simp add: norm_minus_commute)

  2019     apply (simp only: dist_norm [symmetric])

  2020     apply (subgoal_tac "\<bar>1 - k\<bar> * dist x y < 1 * dist x y", simp)

  2021     apply (rule mult_strict_right_mono)

  2022     apply (simp add: k_def divide_pos_pos zero_less_dist_iff 0 < r x \<noteq> y)

  2023     apply (simp add: zero_less_dist_iff x \<noteq> y)

  2024     done

  2025   hence "z \<in> ball x (dist x y)" by simp

  2026   have "z \<noteq> y"

  2027     unfolding z_def k_def using x \<noteq> y 0 < r

  2028     by (simp add: min_def)

  2029   show "\<exists>z\<in>ball x (dist x y). z \<in> T \<and> z \<noteq> y"

  2030     using z \<in> ball x (dist x y) z \<in> T z \<noteq> y

  2031     by fast

  2032 qed

  2033

  2034 lemma closure_ball:

  2035   fixes x :: "'a::real_normed_vector"

  2036   shows "0 < e \<Longrightarrow> closure (ball x e) = cball x e"

  2037 apply (rule equalityI)

  2038 apply (rule closure_minimal)

  2039 apply (rule ball_subset_cball)

  2040 apply (rule closed_cball)

  2041 apply (rule subsetI, rename_tac y)

  2042 apply (simp add: le_less [where 'a=real])

  2043 apply (erule disjE)

  2044 apply (rule subsetD [OF closure_subset], simp)

  2045 apply (simp add: closure_def)

  2046 apply clarify

  2047 apply (rule closure_ball_lemma)

  2048 apply (simp add: zero_less_dist_iff)

  2049 done

  2050

  2051 (* In a trivial vector space, this fails for e = 0. *)

  2052 lemma interior_cball:

  2053   fixes x :: "'a::{real_normed_vector, perfect_space}"

  2054   shows "interior (cball x e) = ball x e"

  2055 proof(cases "e\<ge>0")

  2056   case False note cs = this

  2057   from cs have "ball x e = {}" using ball_empty[of e x] by auto moreover

  2058   { fix y assume "y \<in> cball x e"

  2059     hence False unfolding mem_cball using dist_nz[of x y] cs by auto  }

  2060   hence "cball x e = {}" by auto

  2061   hence "interior (cball x e) = {}" using interior_empty by auto

  2062   ultimately show ?thesis by blast

  2063 next

  2064   case True note cs = this

  2065   have "ball x e \<subseteq> cball x e" using ball_subset_cball by auto moreover

  2066   { fix S y assume as: "S \<subseteq> cball x e" "open S" "y\<in>S"

  2067     then obtain d where "d>0" and d:"\<forall>x'. dist x' y < d \<longrightarrow> x' \<in> S" unfolding open_dist by blast

  2068

  2069     then obtain xa where xa_y: "xa \<noteq> y" and xa: "dist xa y < d"

  2070       using perfect_choose_dist [of d] by auto

  2071     have "xa\<in>S" using d[THEN spec[where x=xa]] using xa by(auto simp add: dist_commute)

  2072     hence xa_cball:"xa \<in> cball x e" using as(1) by auto

  2073

  2074     hence "y \<in> ball x e" proof(cases "x = y")

  2075       case True

  2076       hence "e>0" using xa_y[unfolded dist_nz] xa_cball[unfolded mem_cball] by (auto simp add: dist_commute)

  2077       thus "y \<in> ball x e" using x = y  by simp

  2078     next

  2079       case False

  2080       have "dist (y + (d / 2 / dist y x) *\<^sub>R (y - x)) y < d" unfolding dist_norm

  2081         using d>0 norm_ge_zero[of "y - x"] x \<noteq> y by auto

  2082       hence *:"y + (d / 2 / dist y x) *\<^sub>R (y - x) \<in> cball x e" using d as(1)[unfolded subset_eq] by blast

  2083       have "y - x \<noteq> 0" using x \<noteq> y by auto

  2084       hence **:"d / (2 * norm (y - x)) > 0" unfolding zero_less_norm_iff[THEN sym]

  2085         using d>0 divide_pos_pos[of d "2*norm (y - x)"] by auto

  2086

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

  2088         by (auto simp add: dist_norm algebra_simps)

  2089       also have "\<dots> = norm ((1 + d / (2 * norm (y - x))) *\<^sub>R (y - x))"

  2090         by (auto simp add: algebra_simps)

  2091       also have "\<dots> = \<bar>1 + d / (2 * norm (y - x))\<bar> * norm (y - x)"

  2092         using ** by auto

  2093       also have "\<dots> = (dist y x) + d/2"using ** by (auto simp add: distrib_right dist_norm)

  2094       finally have "e \<ge> dist x y +d/2" using *[unfolded mem_cball] by (auto simp add: dist_commute)

  2095       thus "y \<in> ball x e" unfolding mem_ball using d>0 by auto

  2096     qed  }

  2097   hence "\<forall>S \<subseteq> cball x e. open S \<longrightarrow> S \<subseteq> ball x e" by auto

  2098   ultimately show ?thesis using interior_unique[of "ball x e" "cball x e"] using open_ball[of x e] by auto

  2099 qed

  2100

  2101 lemma frontier_ball:

  2102   fixes a :: "'a::real_normed_vector"

  2103   shows "0 < e ==> frontier(ball a e) = {x. dist a x = e}"

  2104   apply (simp add: frontier_def closure_ball interior_open order_less_imp_le)

  2105   apply (simp add: set_eq_iff)

  2106   by arith

  2107

  2108 lemma frontier_cball:

  2109   fixes a :: "'a::{real_normed_vector, perfect_space}"

  2110   shows "frontier(cball a e) = {x. dist a x = e}"

  2111   apply (simp add: frontier_def interior_cball closed_cball order_less_imp_le)

  2112   apply (simp add: set_eq_iff)

  2113   by arith

  2114

  2115 lemma cball_eq_empty: "(cball x e = {}) \<longleftrightarrow> e < 0"

  2116   apply (simp add: set_eq_iff not_le)

  2117   by (metis zero_le_dist dist_self order_less_le_trans)

  2118 lemma cball_empty: "e < 0 ==> cball x e = {}" by (simp add: cball_eq_empty)

  2119

  2120 lemma cball_eq_sing:

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

  2122   shows "(cball x e = {x}) \<longleftrightarrow> e = 0"

  2123 proof (rule linorder_cases)

  2124   assume e: "0 < e"

  2125   obtain a where "a \<noteq> x" "dist a x < e"

  2126     using perfect_choose_dist [OF e] by auto

  2127   hence "a \<noteq> x" "dist x a \<le> e" by (auto simp add: dist_commute)

  2128   with e show ?thesis by (auto simp add: set_eq_iff)

  2129 qed auto

  2130

  2131 lemma cball_sing:

  2132   fixes x :: "'a::metric_space"

  2133   shows "e = 0 ==> cball x e = {x}"

  2134   by (auto simp add: set_eq_iff)

  2135

  2136

  2137 subsection {* Boundedness *}

  2138

  2139   (* FIXME: This has to be unified with BSEQ!! *)

  2140 definition (in metric_space)

  2141   bounded :: "'a set \<Rightarrow> bool" where

  2142   "bounded S \<longleftrightarrow> (\<exists>x e. \<forall>y\<in>S. dist x y \<le> e)"

  2143

  2144 lemma bounded_subset_cball: "bounded S \<longleftrightarrow> (\<exists>e x. S \<subseteq> cball x e)"

  2145   unfolding bounded_def subset_eq by auto

  2146

  2147 lemma bounded_any_center: "bounded S \<longleftrightarrow> (\<exists>e. \<forall>y\<in>S. dist a y \<le> e)"

  2148 unfolding bounded_def

  2149 apply safe

  2150 apply (rule_tac x="dist a x + e" in exI, clarify)

  2151 apply (drule (1) bspec)

  2152 apply (erule order_trans [OF dist_triangle add_left_mono])

  2153 apply auto

  2154 done

  2155

  2156 lemma bounded_iff: "bounded S \<longleftrightarrow> (\<exists>a. \<forall>x\<in>S. norm x \<le> a)"

  2157 unfolding bounded_any_center [where a=0]

  2158 by (simp add: dist_norm)

  2159

  2160 lemma bounded_realI: assumes "\<forall>x\<in>s. abs (x::real) \<le> B" shows "bounded s"

  2161   unfolding bounded_def dist_real_def apply(rule_tac x=0 in exI)

  2162   using assms by auto

  2163

  2164 lemma bounded_empty [simp]: "bounded {}"

  2165   by (simp add: bounded_def)

  2166

  2167 lemma bounded_subset: "bounded T \<Longrightarrow> S \<subseteq> T ==> bounded S"

  2168   by (metis bounded_def subset_eq)

  2169

  2170 lemma bounded_interior[intro]: "bounded S ==> bounded(interior S)"

  2171   by (metis bounded_subset interior_subset)

  2172

  2173 lemma bounded_closure[intro]: assumes "bounded S" shows "bounded(closure S)"

  2174 proof-

  2175   from assms obtain x and a where a: "\<forall>y\<in>S. dist x y \<le> a" unfolding bounded_def by auto

  2176   { fix y assume "y \<in> closure S"

  2177     then obtain f where f: "\<forall>n. f n \<in> S"  "(f ---> y) sequentially"

  2178       unfolding closure_sequential by auto

  2179     have "\<forall>n. f n \<in> S \<longrightarrow> dist x (f n) \<le> a" using a by simp

  2180     hence "eventually (\<lambda>n. dist x (f n) \<le> a) sequentially"

  2181       by (rule eventually_mono, simp add: f(1))

  2182     have "dist x y \<le> a"

  2183       apply (rule Lim_dist_ubound [of sequentially f])

  2184       apply (rule trivial_limit_sequentially)

  2185       apply (rule f(2))

  2186       apply fact

  2187       done

  2188   }

  2189   thus ?thesis unfolding bounded_def by auto

  2190 qed

  2191

  2192 lemma bounded_cball[simp,intro]: "bounded (cball x e)"

  2193   apply (simp add: bounded_def)

  2194   apply (rule_tac x=x in exI)

  2195   apply (rule_tac x=e in exI)

  2196   apply auto

  2197   done

  2198

  2199 lemma bounded_ball[simp,intro]: "bounded(ball x e)"

  2200   by (metis ball_subset_cball bounded_cball bounded_subset)

  2201

  2202 lemma bounded_Un[simp]: "bounded (S \<union> T) \<longleftrightarrow> bounded S \<and> bounded T"

  2203   apply (auto simp add: bounded_def)

  2204   apply (rename_tac x y r s)

  2205   apply (rule_tac x=x in exI)

  2206   apply (rule_tac x="max r (dist x y + s)" in exI)

  2207   apply (rule ballI, rename_tac z, safe)

  2208   apply (drule (1) bspec, simp)

  2209   apply (drule (1) bspec)

  2210   apply (rule min_max.le_supI2)

  2211   apply (erule order_trans [OF dist_triangle add_left_mono])

  2212   done

  2213

  2214 lemma bounded_Union[intro]: "finite F \<Longrightarrow> (\<forall>S\<in>F. bounded S) \<Longrightarrow> bounded(\<Union>F)"

  2215   by (induct rule: finite_induct[of F], auto)

  2216

  2217 lemma bounded_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. bounded (B x) \<Longrightarrow> bounded (\<Union>x\<in>A. B x)"

  2218   by (induct set: finite, auto)

  2219

  2220 lemma bounded_insert [simp]: "bounded (insert x S) \<longleftrightarrow> bounded S"

  2221 proof -

  2222   have "\<forall>y\<in>{x}. dist x y \<le> 0" by simp

  2223   hence "bounded {x}" unfolding bounded_def by fast

  2224   thus ?thesis by (metis insert_is_Un bounded_Un)

  2225 qed

  2226

  2227 lemma finite_imp_bounded [intro]: "finite S \<Longrightarrow> bounded S"

  2228   by (induct set: finite, simp_all)

  2229

  2230 lemma bounded_pos: "bounded S \<longleftrightarrow> (\<exists>b>0. \<forall>x\<in> S. norm x <= b)"

  2231   apply (simp add: bounded_iff)

  2232   apply (subgoal_tac "\<And>x (y::real). 0 < 1 + abs y \<and> (x <= y \<longrightarrow> x <= 1 + abs y)")

  2233   by metis arith

  2234

  2235 lemma Bseq_eq_bounded: "Bseq f \<longleftrightarrow> bounded (range f)"

  2236   unfolding Bseq_def bounded_pos by auto

  2237

  2238 lemma bounded_Int[intro]: "bounded S \<or> bounded T \<Longrightarrow> bounded (S \<inter> T)"

  2239   by (metis Int_lower1 Int_lower2 bounded_subset)

  2240

  2241 lemma bounded_diff[intro]: "bounded S ==> bounded (S - T)"

  2242 apply (metis Diff_subset bounded_subset)

  2243 done

  2244

  2245 lemma not_bounded_UNIV[simp, intro]:

  2246   "\<not> bounded (UNIV :: 'a::{real_normed_vector, perfect_space} set)"

  2247 proof(auto simp add: bounded_pos not_le)

  2248   obtain x :: 'a where "x \<noteq> 0"

  2249     using perfect_choose_dist [OF zero_less_one] by fast

  2250   fix b::real  assume b: "b >0"

  2251   have b1: "b +1 \<ge> 0" using b by simp

  2252   with x \<noteq> 0 have "b < norm (scaleR (b + 1) (sgn x))"

  2253     by (simp add: norm_sgn)

  2254   then show "\<exists>x::'a. b < norm x" ..

  2255 qed

  2256

  2257 lemma bounded_linear_image:

  2258   assumes "bounded S" "bounded_linear f"

  2259   shows "bounded(f  S)"

  2260 proof-

  2261   from assms(1) obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto

  2262   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)

  2263   { fix x assume "x\<in>S"

  2264     hence "norm x \<le> b" using b by auto

  2265     hence "norm (f x) \<le> B * b" using B(2) apply(erule_tac x=x in allE)

  2266       by (metis B(1) B(2) order_trans mult_le_cancel_left_pos)

  2267   }

  2268   thus ?thesis unfolding bounded_pos apply(rule_tac x="b*B" in exI)

  2269     using b B mult_pos_pos [of b B] by (auto simp add: mult_commute)

  2270 qed

  2271

  2272 lemma bounded_scaling:

  2273   fixes S :: "'a::real_normed_vector set"

  2274   shows "bounded S \<Longrightarrow> bounded ((\<lambda>x. c *\<^sub>R x)  S)"

  2275   apply (rule bounded_linear_image, assumption)

  2276   apply (rule bounded_linear_scaleR_right)

  2277   done

  2278

  2279 lemma bounded_translation:

  2280   fixes S :: "'a::real_normed_vector set"

  2281   assumes "bounded S" shows "bounded ((\<lambda>x. a + x)  S)"

  2282 proof-

  2283   from assms obtain b where b:"b>0" "\<forall>x\<in>S. norm x \<le> b" unfolding bounded_pos by auto

  2284   { fix x assume "x\<in>S"

  2285     hence "norm (a + x) \<le> b + norm a" using norm_triangle_ineq[of a x] b by auto

  2286   }

  2287   thus ?thesis unfolding bounded_pos using norm_ge_zero[of a] b(1) using add_strict_increasing[of b 0 "norm a"]

  2288     by (auto intro!: exI[of _ "b + norm a"])

  2289 qed

  2290

  2291

  2292 text{* Some theorems on sups and infs using the notion "bounded". *}

  2293

  2294 lemma bounded_real:

  2295   fixes S :: "real set"

  2296   shows "bounded S \<longleftrightarrow>  (\<exists>a. \<forall>x\<in>S. abs x <= a)"

  2297   by (simp add: bounded_iff)

  2298

  2299 lemma bounded_has_Sup:

  2300   fixes S :: "real set"

  2301   assumes "bounded S" "S \<noteq> {}"

  2302   shows "\<forall>x\<in>S. x <= Sup S" and "\<forall>b. (\<forall>x\<in>S. x <= b) \<longrightarrow> Sup S <= b"

  2303 proof

  2304   fix x assume "x\<in>S"

  2305   thus "x \<le> Sup S"

  2306     by (metis SupInf.Sup_upper abs_le_D1 assms(1) bounded_real)

  2307 next

  2308   show "\<forall>b. (\<forall>x\<in>S. x \<le> b) \<longrightarrow> Sup S \<le> b" using assms

  2309     by (metis SupInf.Sup_least)

  2310 qed

  2311

  2312 lemma Sup_insert:

  2313   fixes S :: "real set"

  2314   shows "bounded S ==> Sup(insert x S) = (if S = {} then x else max x (Sup S))"

  2315 by auto (metis Int_absorb Sup_insert_nonempty assms bounded_has_Sup(1) disjoint_iff_not_equal)

  2316

  2317 lemma Sup_insert_finite:

  2318   fixes S :: "real set"

  2319   shows "finite S \<Longrightarrow> Sup(insert x S) = (if S = {} then x else max x (Sup S))"

  2320   apply (rule Sup_insert)

  2321   apply (rule finite_imp_bounded)

  2322   by simp

  2323

  2324 lemma bounded_has_Inf:

  2325   fixes S :: "real set"

  2326   assumes "bounded S"  "S \<noteq> {}"

  2327   shows "\<forall>x\<in>S. x >= Inf S" and "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S >= b"

  2328 proof

  2329   fix x assume "x\<in>S"

  2330   from assms(1) obtain a where a:"\<forall>x\<in>S. \<bar>x\<bar> \<le> a" unfolding bounded_real by auto

  2331   thus "x \<ge> Inf S" using x\<in>S

  2332     by (metis Inf_lower_EX abs_le_D2 minus_le_iff)

  2333 next

  2334   show "\<forall>b. (\<forall>x\<in>S. x >= b) \<longrightarrow> Inf S \<ge> b" using assms

  2335     by (metis SupInf.Inf_greatest)

  2336 qed

  2337

  2338 lemma Inf_insert:

  2339   fixes S :: "real set"

  2340   shows "bounded S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"

  2341 by auto (metis Int_absorb Inf_insert_nonempty bounded_has_Inf(1) disjoint_iff_not_equal)

  2342

  2343 lemma Inf_insert_finite:

  2344   fixes S :: "real set"

  2345   shows "finite S ==> Inf(insert x S) = (if S = {} then x else min x (Inf S))"

  2346   by (rule Inf_insert, rule finite_imp_bounded, simp)

  2347

  2348 subsection {* Compactness *}

  2349

  2350 subsubsection{* Open-cover compactness *}

  2351

  2352 definition compact :: "'a::topological_space set \<Rightarrow> bool" where

  2353   compact_eq_heine_borel: -- "This name is used for backwards compatibility"

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

  2355

  2356 lemma compactI:

  2357   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'"

  2358   shows "compact s"

  2359   unfolding compact_eq_heine_borel using assms by metis

  2360

  2361 lemma compactE:

  2362   assumes "compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C"

  2363   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"

  2364   using assms unfolding compact_eq_heine_borel by metis

  2365

  2366 lemma compactE_image:

  2367   assumes "compact s" and "\<forall>t\<in>C. open (f t)" and "s \<subseteq> (\<Union>c\<in>C. f c)"

  2368   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> (\<Union>c\<in>C'. f c)"

  2369   using assms unfolding ball_simps[symmetric] SUP_def

  2370   by (metis (lifting) finite_subset_image compact_eq_heine_borel[of s])

  2371

  2372 subsubsection {* Bolzano-Weierstrass property *}

  2373

  2374 lemma heine_borel_imp_bolzano_weierstrass:

  2375   assumes "compact s" "infinite t"  "t \<subseteq> s"

  2376   shows "\<exists>x \<in> s. x islimpt t"

  2377 proof(rule ccontr)

  2378   assume "\<not> (\<exists>x \<in> s. x islimpt t)"

  2379   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

  2380     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

  2381   obtain g where g:"g\<subseteq>{t. \<exists>x. x \<in> s \<and> t = f x}" "finite g" "s \<subseteq> \<Union>g"

  2382     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

  2383   from g(1,3) have g':"\<forall>x\<in>g. \<exists>xa \<in> s. x = f xa" by auto

  2384   { fix x y assume "x\<in>t" "y\<in>t" "f x = f y"

  2385     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

  2386     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  }

  2387   hence "inj_on f t" unfolding inj_on_def by simp

  2388   hence "infinite (f  t)" using assms(2) using finite_imageD by auto

  2389   moreover

  2390   { fix x assume "x\<in>t" "f x \<notin> g"

  2391     from g(3) assms(3) x\<in>t obtain h where "h\<in>g" and "x\<in>h" by auto

  2392     then obtain y where "y\<in>s" "h = f y" using g'[THEN bspec[where x=h]] by auto

  2393     hence "y = x" using f[THEN bspec[where x=y]] and x\<in>t and x\<in>h[unfolded h = f y] by auto

  2394     hence False using f x \<notin> g h\<in>g unfolding h = f y by auto  }

  2395   hence "f  t \<subseteq> g" by auto

  2396   ultimately show False using g(2) using finite_subset by auto

  2397 qed

  2398

  2399 lemma acc_point_range_imp_convergent_subsequence:

  2400   fixes l :: "'a :: first_countable_topology"

  2401   assumes l: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> range f)"

  2402   shows "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2403 proof -

  2404   from countable_basis_at_decseq[of l] guess A . note A = this

  2405

  2406   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> f j \<in> A (Suc n)"

  2407   { fix n i

  2408     have "infinite (A (Suc n) \<inter> range f - f{.. i})"

  2409       using l A by auto

  2410     then have "\<exists>x. x \<in> A (Suc n) \<inter> range f - f{.. i}"

  2411       unfolding ex_in_conv by (intro notI) simp

  2412     then have "\<exists>j. f j \<in> A (Suc n) \<and> j \<notin> {.. i}"

  2413       by auto

  2414     then have "\<exists>a. i < a \<and> f a \<in> A (Suc n)"

  2415       by (auto simp: not_le)

  2416     then have "i < s n i" "f (s n i) \<in> A (Suc n)"

  2417       unfolding s_def by (auto intro: someI2_ex) }

  2418   note s = this

  2419   def r \<equiv> "nat_rec (s 0 0) s"

  2420   have "subseq r"

  2421     by (auto simp: r_def s subseq_Suc_iff)

  2422   moreover

  2423   have "(\<lambda>n. f (r n)) ----> l"

  2424   proof (rule topological_tendstoI)

  2425     fix S assume "open S" "l \<in> S"

  2426     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto

  2427     moreover

  2428     { fix i assume "Suc 0 \<le> i" then have "f (r i) \<in> A i"

  2429         by (cases i) (simp_all add: r_def s) }

  2430     then have "eventually (\<lambda>i. f (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)

  2431     ultimately show "eventually (\<lambda>i. f (r i) \<in> S) sequentially"

  2432       by eventually_elim auto

  2433   qed

  2434   ultimately show "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2435     by (auto simp: convergent_def comp_def)

  2436 qed

  2437

  2438 lemma sequence_infinite_lemma:

  2439   fixes f :: "nat \<Rightarrow> 'a::t1_space"

  2440   assumes "\<forall>n. f n \<noteq> l" and "(f ---> l) sequentially"

  2441   shows "infinite (range f)"

  2442 proof

  2443   assume "finite (range f)"

  2444   hence "closed (range f)" by (rule finite_imp_closed)

  2445   hence "open (- range f)" by (rule open_Compl)

  2446   from assms(1) have "l \<in> - range f" by auto

  2447   from assms(2) have "eventually (\<lambda>n. f n \<in> - range f) sequentially"

  2448     using open (- range f) l \<in> - range f by (rule topological_tendstoD)

  2449   thus False unfolding eventually_sequentially by auto

  2450 qed

  2451

  2452 lemma closure_insert:

  2453   fixes x :: "'a::t1_space"

  2454   shows "closure (insert x s) = insert x (closure s)"

  2455 apply (rule closure_unique)

  2456 apply (rule insert_mono [OF closure_subset])

  2457 apply (rule closed_insert [OF closed_closure])

  2458 apply (simp add: closure_minimal)

  2459 done

  2460

  2461 lemma islimpt_insert:

  2462   fixes x :: "'a::t1_space"

  2463   shows "x islimpt (insert a s) \<longleftrightarrow> x islimpt s"

  2464 proof

  2465   assume *: "x islimpt (insert a s)"

  2466   show "x islimpt s"

  2467   proof (rule islimptI)

  2468     fix t assume t: "x \<in> t" "open t"

  2469     show "\<exists>y\<in>s. y \<in> t \<and> y \<noteq> x"

  2470     proof (cases "x = a")

  2471       case True

  2472       obtain y where "y \<in> insert a s" "y \<in> t" "y \<noteq> x"

  2473         using * t by (rule islimptE)

  2474       with x = a show ?thesis by auto

  2475     next

  2476       case False

  2477       with t have t': "x \<in> t - {a}" "open (t - {a})"

  2478         by (simp_all add: open_Diff)

  2479       obtain y where "y \<in> insert a s" "y \<in> t - {a}" "y \<noteq> x"

  2480         using * t' by (rule islimptE)

  2481       thus ?thesis by auto

  2482     qed

  2483   qed

  2484 next

  2485   assume "x islimpt s" thus "x islimpt (insert a s)"

  2486     by (rule islimpt_subset) auto

  2487 qed

  2488

  2489 lemma islimpt_finite:

  2490   fixes x :: "'a::t1_space"

  2491   shows "finite s \<Longrightarrow> \<not> x islimpt s"

  2492 by (induct set: finite, simp_all add: islimpt_insert)

  2493

  2494 lemma islimpt_union_finite:

  2495   fixes x :: "'a::t1_space"

  2496   shows "finite s \<Longrightarrow> x islimpt (s \<union> t) \<longleftrightarrow> x islimpt t"

  2497 by (simp add: islimpt_Un islimpt_finite)

  2498

  2499 lemma islimpt_eq_acc_point:

  2500   fixes l :: "'a :: t1_space"

  2501   shows "l islimpt S \<longleftrightarrow> (\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S))"

  2502 proof (safe intro!: islimptI)

  2503   fix U assume "l islimpt S" "l \<in> U" "open U" "finite (U \<inter> S)"

  2504   then have "l islimpt S" "l \<in> (U - (U \<inter> S - {l}))" "open (U - (U \<inter> S - {l}))"

  2505     by (auto intro: finite_imp_closed)

  2506   then show False

  2507     by (rule islimptE) auto

  2508 next

  2509   fix T assume *: "\<forall>U. l\<in>U \<longrightarrow> open U \<longrightarrow> infinite (U \<inter> S)" "l \<in> T" "open T"

  2510   then have "infinite (T \<inter> S - {l})" by auto

  2511   then have "\<exists>x. x \<in> (T \<inter> S - {l})"

  2512     unfolding ex_in_conv by (intro notI) simp

  2513   then show "\<exists>y\<in>S. y \<in> T \<and> y \<noteq> l"

  2514     by auto

  2515 qed

  2516

  2517 lemma islimpt_range_imp_convergent_subsequence:

  2518   fixes l :: "'a :: {t1_space, first_countable_topology}"

  2519   assumes l: "l islimpt (range f)"

  2520   shows "\<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  2521   using l unfolding islimpt_eq_acc_point

  2522   by (rule acc_point_range_imp_convergent_subsequence)

  2523

  2524 lemma sequence_unique_limpt:

  2525   fixes f :: "nat \<Rightarrow> 'a::t2_space"

  2526   assumes "(f ---> l) sequentially" and "l' islimpt (range f)"

  2527   shows "l' = l"

  2528 proof (rule ccontr)

  2529   assume "l' \<noteq> l"

  2530   obtain s t where "open s" "open t" "l' \<in> s" "l \<in> t" "s \<inter> t = {}"

  2531     using hausdorff [OF l' \<noteq> l] by auto

  2532   have "eventually (\<lambda>n. f n \<in> t) sequentially"

  2533     using assms(1) open t l \<in> t by (rule topological_tendstoD)

  2534   then obtain N where "\<forall>n\<ge>N. f n \<in> t"

  2535     unfolding eventually_sequentially by auto

  2536

  2537   have "UNIV = {..<N} \<union> {N..}" by auto

  2538   hence "l' islimpt (f  ({..<N} \<union> {N..}))" using assms(2) by simp

  2539   hence "l' islimpt (f  {..<N} \<union> f  {N..})" by (simp add: image_Un)

  2540   hence "l' islimpt (f  {N..})" by (simp add: islimpt_union_finite)

  2541   then obtain y where "y \<in> f  {N..}" "y \<in> s" "y \<noteq> l'"

  2542     using l' \<in> s open s by (rule islimptE)

  2543   then obtain n where "N \<le> n" "f n \<in> s" "f n \<noteq> l'" by auto

  2544   with \<forall>n\<ge>N. f n \<in> t have "f n \<in> s \<inter> t" by simp

  2545   with s \<inter> t = {} show False by simp

  2546 qed

  2547

  2548 lemma bolzano_weierstrass_imp_closed:

  2549   fixes s :: "'a::{first_countable_topology, t2_space} set"

  2550   assumes "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t)"

  2551   shows "closed s"

  2552 proof-

  2553   { fix x l assume as: "\<forall>n::nat. x n \<in> s" "(x ---> l) sequentially"

  2554     hence "l \<in> s"

  2555     proof(cases "\<forall>n. x n \<noteq> l")

  2556       case False thus "l\<in>s" using as(1) by auto

  2557     next

  2558       case True note cas = this

  2559       with as(2) have "infinite (range x)" using sequence_infinite_lemma[of x l] by auto

  2560       then obtain l' where "l'\<in>s" "l' islimpt (range x)" using assms[THEN spec[where x="range x"]] as(1) by auto

  2561       thus "l\<in>s" using sequence_unique_limpt[of x l l'] using as cas by auto

  2562     qed  }

  2563   thus ?thesis unfolding closed_sequential_limits by fast

  2564 qed

  2565

  2566 lemma compact_imp_closed:

  2567   fixes s :: "'a::t2_space set"

  2568   assumes "compact s" shows "closed s"

  2569 unfolding closed_def

  2570 proof (rule openI)

  2571   fix y assume "y \<in> - s"

  2572   let ?C = "\<Union>x\<in>s. {u. open u \<and> x \<in> u \<and> eventually (\<lambda>y. y \<notin> u) (nhds y)}"

  2573   note compact s

  2574   moreover have "\<forall>u\<in>?C. open u" by simp

  2575   moreover have "s \<subseteq> \<Union>?C"

  2576   proof

  2577     fix x assume "x \<in> s"

  2578     with y \<in> - s have "x \<noteq> y" by clarsimp

  2579     hence "\<exists>u v. open u \<and> open v \<and> x \<in> u \<and> y \<in> v \<and> u \<inter> v = {}"

  2580       by (rule hausdorff)

  2581     with x \<in> s show "x \<in> \<Union>?C"

  2582       unfolding eventually_nhds by auto

  2583   qed

  2584   ultimately obtain D where "D \<subseteq> ?C" and "finite D" and "s \<subseteq> \<Union>D"

  2585     by (rule compactE)

  2586   from D \<subseteq> ?C have "\<forall>x\<in>D. eventually (\<lambda>y. y \<notin> x) (nhds y)" by auto

  2587   with finite D have "eventually (\<lambda>y. y \<notin> \<Union>D) (nhds y)"

  2588     by (simp add: eventually_Ball_finite)

  2589   with s \<subseteq> \<Union>D have "eventually (\<lambda>y. y \<notin> s) (nhds y)"

  2590     by (auto elim!: eventually_mono [rotated])

  2591   thus "\<exists>t. open t \<and> y \<in> t \<and> t \<subseteq> - s"

  2592     by (simp add: eventually_nhds subset_eq)

  2593 qed

  2594

  2595 lemma compact_imp_bounded:

  2596   assumes "compact U" shows "bounded U"

  2597 proof -

  2598   have "compact U" "\<forall>x\<in>U. open (ball x 1)" "U \<subseteq> (\<Union>x\<in>U. ball x 1)" using assms by auto

  2599   then obtain D where D: "D \<subseteq> U" "finite D" "U \<subseteq> (\<Union>x\<in>D. ball x 1)"

  2600     by (elim compactE_image)

  2601   from finite D have "bounded (\<Union>x\<in>D. ball x 1)"

  2602     by (simp add: bounded_UN)

  2603   thus "bounded U" using U \<subseteq> (\<Union>x\<in>D. ball x 1)

  2604     by (rule bounded_subset)

  2605 qed

  2606

  2607 text{* In particular, some common special cases. *}

  2608

  2609 lemma compact_empty[simp]:

  2610  "compact {}"

  2611   unfolding compact_eq_heine_borel

  2612   by auto

  2613

  2614 lemma compact_union [intro]:

  2615   assumes "compact s" "compact t" shows " compact (s \<union> t)"

  2616 proof (rule compactI)

  2617   fix f assume *: "Ball f open" "s \<union> t \<subseteq> \<Union>f"

  2618   from * compact s obtain s' where "s' \<subseteq> f \<and> finite s' \<and> s \<subseteq> \<Union>s'"

  2619     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis

  2620   moreover from * compact t obtain t' where "t' \<subseteq> f \<and> finite t' \<and> t \<subseteq> \<Union>t'"

  2621     unfolding compact_eq_heine_borel by (auto elim!: allE[of _ f]) metis

  2622   ultimately show "\<exists>f'\<subseteq>f. finite f' \<and> s \<union> t \<subseteq> \<Union>f'"

  2623     by (auto intro!: exI[of _ "s' \<union> t'"])

  2624 qed

  2625

  2626 lemma compact_Union [intro]: "finite S \<Longrightarrow> (\<And>T. T \<in> S \<Longrightarrow> compact T) \<Longrightarrow> compact (\<Union>S)"

  2627   by (induct set: finite) auto

  2628

  2629 lemma compact_UN [intro]:

  2630   "finite A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> compact (B x)) \<Longrightarrow> compact (\<Union>x\<in>A. B x)"

  2631   unfolding SUP_def by (rule compact_Union) auto

  2632

  2633 lemma compact_inter_closed [intro]:

  2634   assumes "compact s" and "closed t"

  2635   shows "compact (s \<inter> t)"

  2636 proof (rule compactI)

  2637   fix C assume C: "\<forall>c\<in>C. open c" and cover: "s \<inter> t \<subseteq> \<Union>C"

  2638   from C closed t have "\<forall>c\<in>C \<union> {-t}. open c" by auto

  2639   moreover from cover have "s \<subseteq> \<Union>(C \<union> {-t})" by auto

  2640   ultimately have "\<exists>D\<subseteq>C \<union> {-t}. finite D \<and> s \<subseteq> \<Union>D"

  2641     using compact s unfolding compact_eq_heine_borel by auto

  2642   then guess D ..

  2643   then show "\<exists>D\<subseteq>C. finite D \<and> s \<inter> t \<subseteq> \<Union>D"

  2644     by (intro exI[of _ "D - {-t}"]) auto

  2645 qed

  2646

  2647 lemma closed_inter_compact [intro]:

  2648   assumes "closed s" and "compact t"

  2649   shows "compact (s \<inter> t)"

  2650   using compact_inter_closed [of t s] assms

  2651   by (simp add: Int_commute)

  2652

  2653 lemma compact_inter [intro]:

  2654   fixes s t :: "'a :: t2_space set"

  2655   assumes "compact s" and "compact t"

  2656   shows "compact (s \<inter> t)"

  2657   using assms by (intro compact_inter_closed compact_imp_closed)

  2658

  2659 lemma compact_sing [simp]: "compact {a}"

  2660   unfolding compact_eq_heine_borel by auto

  2661

  2662 lemma compact_insert [simp]:

  2663   assumes "compact s" shows "compact (insert x s)"

  2664 proof -

  2665   have "compact ({x} \<union> s)"

  2666     using compact_sing assms by (rule compact_union)

  2667   thus ?thesis by simp

  2668 qed

  2669

  2670 lemma finite_imp_compact:

  2671   shows "finite s \<Longrightarrow> compact s"

  2672   by (induct set: finite) simp_all

  2673

  2674 lemma open_delete:

  2675   fixes s :: "'a::t1_space set"

  2676   shows "open s \<Longrightarrow> open (s - {x})"

  2677   by (simp add: open_Diff)

  2678

  2679 text{* Finite intersection property *}

  2680

  2681 lemma inj_setminus: "inj_on uminus (A::'a set set)"

  2682   by (auto simp: inj_on_def)

  2683

  2684 lemma compact_fip:

  2685   "compact U \<longleftrightarrow>

  2686     (\<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> {})"

  2687   (is "_ \<longleftrightarrow> ?R")

  2688 proof (safe intro!: compact_eq_heine_borel[THEN iffD2])

  2689   fix A assume "compact U" and A: "\<forall>a\<in>A. closed a" "U \<inter> \<Inter>A = {}"

  2690     and fi: "\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}"

  2691   from A have "(\<forall>a\<in>uminusA. open a) \<and> U \<subseteq> \<Union>uminusA"

  2692     by auto

  2693   with compact U obtain B where "B \<subseteq> A" "finite (uminusB)" "U \<subseteq> \<Union>(uminusB)"

  2694     unfolding compact_eq_heine_borel by (metis subset_image_iff)

  2695   with fi[THEN spec, of B] show False

  2696     by (auto dest: finite_imageD intro: inj_setminus)

  2697 next

  2698   fix A assume ?R and cover: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"

  2699   from cover have "U \<inter> \<Inter>(uminusA) = {}" "\<forall>a\<in>uminusA. closed a"

  2700     by auto

  2701   with ?R obtain B where "B \<subseteq> A" "finite (uminusB)" "U \<inter> \<Inter>uminusB = {}"

  2702     by (metis subset_image_iff)

  2703   then show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"

  2704     by  (auto intro!: exI[of _ B] inj_setminus dest: finite_imageD)

  2705 qed

  2706

  2707 lemma compact_imp_fip:

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

  2709     s \<inter> (\<Inter> f) \<noteq> {}"

  2710   unfolding compact_fip by auto

  2711

  2712 text{*Compactness expressed with filters*}

  2713

  2714 definition "filter_from_subbase B = Abs_filter (\<lambda>P. \<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"

  2715

  2716 lemma eventually_filter_from_subbase:

  2717   "eventually P (filter_from_subbase B) \<longleftrightarrow> (\<exists>X \<subseteq> B. finite X \<and> Inf X \<le> P)"

  2718     (is "_ \<longleftrightarrow> ?R P")

  2719   unfolding filter_from_subbase_def

  2720 proof (rule eventually_Abs_filter is_filter.intro)+

  2721   show "?R (\<lambda>x. True)"

  2722     by (rule exI[of _ "{}"]) (simp add: le_fun_def)

  2723 next

  2724   fix P Q assume "?R P" then guess X ..

  2725   moreover assume "?R Q" then guess Y ..

  2726   ultimately show "?R (\<lambda>x. P x \<and> Q x)"

  2727     by (intro exI[of _ "X \<union> Y"]) auto

  2728 next

  2729   fix P Q

  2730   assume "?R P" then guess X ..

  2731   moreover assume "\<forall>x. P x \<longrightarrow> Q x"

  2732   ultimately show "?R Q"

  2733     by (intro exI[of _ X]) auto

  2734 qed

  2735

  2736 lemma eventually_filter_from_subbaseI: "P \<in> B \<Longrightarrow> eventually P (filter_from_subbase B)"

  2737   by (subst eventually_filter_from_subbase) (auto intro!: exI[of _ "{P}"])

  2738

  2739 lemma filter_from_subbase_not_bot:

  2740   "\<forall>X \<subseteq> B. finite X \<longrightarrow> Inf X \<noteq> bot \<Longrightarrow> filter_from_subbase B \<noteq> bot"

  2741   unfolding trivial_limit_def eventually_filter_from_subbase by auto

  2742

  2743 lemma closure_iff_nhds_not_empty:

  2744   "x \<in> closure X \<longleftrightarrow> (\<forall>A. \<forall>S\<subseteq>A. open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {})"

  2745 proof safe

  2746   assume x: "x \<in> closure X"

  2747   fix S A assume "open S" "x \<in> S" "X \<inter> A = {}" "S \<subseteq> A"

  2748   then have "x \<notin> closure (-S)"

  2749     by (auto simp: closure_complement subset_eq[symmetric] intro: interiorI)

  2750   with x have "x \<in> closure X - closure (-S)"

  2751     by auto

  2752   also have "\<dots> \<subseteq> closure (X \<inter> S)"

  2753     using open S open_inter_closure_subset[of S X] by (simp add: closed_Compl ac_simps)

  2754   finally have "X \<inter> S \<noteq> {}" by auto

  2755   then show False using X \<inter> A = {} S \<subseteq> A by auto

  2756 next

  2757   assume "\<forall>A S. S \<subseteq> A \<longrightarrow> open S \<longrightarrow> x \<in> S \<longrightarrow> X \<inter> A \<noteq> {}"

  2758   from this[THEN spec, of "- X", THEN spec, of "- closure X"]

  2759   show "x \<in> closure X"

  2760     by (simp add: closure_subset open_Compl)

  2761 qed

  2762

  2763 lemma compact_filter:

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

  2765 proof (intro allI iffI impI compact_fip[THEN iffD2] notI)

  2766   fix F assume "compact U" and F: "F \<noteq> bot" "eventually (\<lambda>x. x \<in> U) F"

  2767   from F have "U \<noteq> {}"

  2768     by (auto simp: eventually_False)

  2769

  2770   def Z \<equiv> "closure  {A. eventually (\<lambda>x. x \<in> A) F}"

  2771   then have "\<forall>z\<in>Z. closed z"

  2772     by auto

  2773   moreover

  2774   have ev_Z: "\<And>z. z \<in> Z \<Longrightarrow> eventually (\<lambda>x. x \<in> z) F"

  2775     unfolding Z_def by (auto elim: eventually_elim1 intro: set_mp[OF closure_subset])

  2776   have "(\<forall>B \<subseteq> Z. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {})"

  2777   proof (intro allI impI)

  2778     fix B assume "finite B" "B \<subseteq> Z"

  2779     with finite B ev_Z have "eventually (\<lambda>x. \<forall>b\<in>B. x \<in> b) F"

  2780       by (auto intro!: eventually_Ball_finite)

  2781     with F(2) have "eventually (\<lambda>x. x \<in> U \<inter> (\<Inter>B)) F"

  2782       by eventually_elim auto

  2783     with F show "U \<inter> \<Inter>B \<noteq> {}"

  2784       by (intro notI) (simp add: eventually_False)

  2785   qed

  2786   ultimately have "U \<inter> \<Inter>Z \<noteq> {}"

  2787     using compact U unfolding compact_fip by blast

  2788   then obtain x where "x \<in> U" and x: "\<And>z. z \<in> Z \<Longrightarrow> x \<in> z" by auto

  2789

  2790   have "\<And>P. eventually P (inf (nhds x) F) \<Longrightarrow> P \<noteq> bot"

  2791     unfolding eventually_inf eventually_nhds

  2792   proof safe

  2793     fix P Q R S

  2794     assume "eventually R F" "open S" "x \<in> S"

  2795     with open_inter_closure_eq_empty[of S "{x. R x}"] x[of "closure {x. R x}"]

  2796     have "S \<inter> {x. R x} \<noteq> {}" by (auto simp: Z_def)

  2797     moreover assume "Ball S Q" "\<forall>x. Q x \<and> R x \<longrightarrow> bot x"

  2798     ultimately show False by (auto simp: set_eq_iff)

  2799   qed

  2800   with x \<in> U show "\<exists>x\<in>U. inf (nhds x) F \<noteq> bot"

  2801     by (metis eventually_bot)

  2802 next

  2803   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 = {}"

  2804

  2805   def P' \<equiv> "(\<lambda>a (x::'a). x \<in> a)"

  2806   then have inj_P': "\<And>A. inj_on P' A"

  2807     by (auto intro!: inj_onI simp: fun_eq_iff)

  2808   def F \<equiv> "filter_from_subbase (P'  insert U A)"

  2809   have "F \<noteq> bot"

  2810     unfolding F_def

  2811   proof (safe intro!: filter_from_subbase_not_bot)

  2812     fix X assume "X \<subseteq> P'  insert U A" "finite X" "Inf X = bot"

  2813     then obtain B where "B \<subseteq> insert U A" "finite B" and B: "Inf (P'  B) = bot"

  2814       unfolding subset_image_iff by (auto intro: inj_P' finite_imageD)

  2815     with A(2)[THEN spec, of "B - {U}"] have "U \<inter> \<Inter>(B - {U}) \<noteq> {}" by auto

  2816     with B show False by (auto simp: P'_def fun_eq_iff)

  2817   qed

  2818   moreover have "eventually (\<lambda>x. x \<in> U) F"

  2819     unfolding F_def by (rule eventually_filter_from_subbaseI) (auto simp: P'_def)

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

  2821   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) F \<noteq> bot"

  2822     by auto

  2823

  2824   { fix V assume "V \<in> A"

  2825     then have V: "eventually (\<lambda>x. x \<in> V) F"

  2826       by (auto simp add: F_def image_iff P'_def intro!: eventually_filter_from_subbaseI)

  2827     have "x \<in> closure V"

  2828       unfolding closure_iff_nhds_not_empty

  2829     proof (intro impI allI)

  2830       fix S A assume "open S" "x \<in> S" "S \<subseteq> A"

  2831       then have "eventually (\<lambda>x. x \<in> A) (nhds x)" by (auto simp: eventually_nhds)

  2832       with V have "eventually (\<lambda>x. x \<in> V \<inter> A) (inf (nhds x) F)"

  2833         by (auto simp: eventually_inf)

  2834       with x show "V \<inter> A \<noteq> {}"

  2835         by (auto simp del: Int_iff simp add: trivial_limit_def)

  2836     qed

  2837     then have "x \<in> V"

  2838       using V \<in> A A(1) by simp }

  2839   with x\<in>U have "x \<in> U \<inter> \<Inter>A" by auto

  2840   with U \<inter> \<Inter>A = {} show False by auto

  2841 qed

  2842

  2843 definition "countably_compact U \<longleftrightarrow>

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

  2845

  2846 lemma countably_compactE:

  2847   assumes "countably_compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C" "countable C"

  2848   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"

  2849   using assms unfolding countably_compact_def by metis

  2850

  2851 lemma countably_compactI:

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

  2853   shows "countably_compact s"

  2854   using assms unfolding countably_compact_def by metis

  2855

  2856 lemma compact_imp_countably_compact: "compact U \<Longrightarrow> countably_compact U"

  2857   by (auto simp: compact_eq_heine_borel countably_compact_def)

  2858

  2859 lemma countably_compact_imp_compact:

  2860   assumes "countably_compact U"

  2861   assumes ccover: "countable B" "\<forall>b\<in>B. open b"

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

  2863   shows "compact U"

  2864   using countably_compact U unfolding compact_eq_heine_borel countably_compact_def

  2865 proof safe

  2866   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"

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

  2868

  2869   moreover def C \<equiv> "{b\<in>B. \<exists>a\<in>A. b \<inter> U \<subseteq> a}"

  2870   ultimately have "countable C" "\<forall>a\<in>C. open a"

  2871     unfolding C_def using ccover by auto

  2872   moreover

  2873   have "\<Union>A \<inter> U \<subseteq> \<Union>C"

  2874   proof safe

  2875     fix x a assume "x \<in> U" "x \<in> a" "a \<in> A"

  2876     with basis[of a x] A obtain b where "b \<in> B" "x \<in> b" "b \<inter> U \<subseteq> a" by blast

  2877     with a \<in> A show "x \<in> \<Union>C" unfolding C_def

  2878       by auto

  2879   qed

  2880   then have "U \<subseteq> \<Union>C" using U \<subseteq> \<Union>A by auto

  2881   ultimately obtain T where "T\<subseteq>C" "finite T" "U \<subseteq> \<Union>T"

  2882     using * by metis

  2883   moreover then have "\<forall>t\<in>T. \<exists>a\<in>A. t \<inter> U \<subseteq> a"

  2884     by (auto simp: C_def)

  2885   then guess f unfolding bchoice_iff Bex_def ..

  2886   ultimately show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"

  2887     unfolding C_def by (intro exI[of _ "fT"]) fastforce

  2888 qed

  2889

  2890 lemma countably_compact_imp_compact_second_countable:

  2891   "countably_compact U \<Longrightarrow> compact (U :: 'a :: second_countable_topology set)"

  2892 proof (rule countably_compact_imp_compact)

  2893   fix T and x :: 'a assume "open T" "x \<in> T"

  2894   from topological_basisE[OF is_basis this] guess b .

  2895   then show "\<exists>b\<in>SOME B. countable B \<and> topological_basis B. x \<in> b \<and> b \<inter> U \<subseteq> T" by auto

  2896 qed (insert countable_basis topological_basis_open[OF is_basis], auto)

  2897

  2898 lemma countably_compact_eq_compact:

  2899   "countably_compact U \<longleftrightarrow> compact (U :: 'a :: second_countable_topology set)"

  2900   using countably_compact_imp_compact_second_countable compact_imp_countably_compact by blast

  2901

  2902 subsubsection{* Sequential compactness *}

  2903

  2904 definition

  2905   seq_compact :: "'a::topological_space set \<Rightarrow> bool" where

  2906   "seq_compact S \<longleftrightarrow>

  2907    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow>

  2908        (\<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially))"

  2909

  2910 lemma seq_compact_imp_countably_compact:

  2911   fixes U :: "'a :: first_countable_topology set"

  2912   assumes "seq_compact U"

  2913   shows "countably_compact U"

  2914 proof (safe intro!: countably_compactI)

  2915   fix A assume A: "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A" "countable A"

  2916   have subseq: "\<And>X. range X \<subseteq> U \<Longrightarrow> \<exists>r x. x \<in> U \<and> subseq r \<and> (X \<circ> r) ----> x"

  2917     using seq_compact U by (fastforce simp: seq_compact_def subset_eq)

  2918   show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"

  2919   proof cases

  2920     assume "finite A" with A show ?thesis by auto

  2921   next

  2922     assume "infinite A"

  2923     then have "A \<noteq> {}" by auto

  2924     show ?thesis

  2925     proof (rule ccontr)

  2926       assume "\<not> (\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T)"

  2927       then have "\<forall>T. \<exists>x. T \<subseteq> A \<and> finite T \<longrightarrow> (x \<in> U - \<Union>T)" by auto

  2928       then obtain X' where T: "\<And>T. T \<subseteq> A \<Longrightarrow> finite T \<Longrightarrow> X' T \<in> U - \<Union>T" by metis

  2929       def X \<equiv> "\<lambda>n. X' (from_nat_into A  {.. n})"

  2930       have X: "\<And>n. X n \<in> U - (\<Union>i\<le>n. from_nat_into A i)"

  2931         using A \<noteq> {} unfolding X_def SUP_def by (intro T) (auto intro: from_nat_into)

  2932       then have "range X \<subseteq> U" by auto

  2933       with subseq[of X] obtain r x where "x \<in> U" and r: "subseq r" "(X \<circ> r) ----> x" by auto

  2934       from x\<in>U U \<subseteq> \<Union>A from_nat_into_surj[OF countable A]

  2935       obtain n where "x \<in> from_nat_into A n" by auto

  2936       with r(2) A(1) from_nat_into[OF A \<noteq> {}, of n]

  2937       have "eventually (\<lambda>i. X (r i) \<in> from_nat_into A n) sequentially"

  2938         unfolding tendsto_def by (auto simp: comp_def)

  2939       then obtain N where "\<And>i. N \<le> i \<Longrightarrow> X (r i) \<in> from_nat_into A n"

  2940         by (auto simp: eventually_sequentially)

  2941       moreover from X have "\<And>i. n \<le> r i \<Longrightarrow> X (r i) \<notin> from_nat_into A n"

  2942         by auto

  2943       moreover from subseq r[THEN seq_suble, of "max n N"] have "\<exists>i. n \<le> r i \<and> N \<le> i"

  2944         by (auto intro!: exI[of _ "max n N"])

  2945       ultimately show False

  2946         by auto

  2947     qed

  2948   qed

  2949 qed

  2950

  2951 lemma compact_imp_seq_compact:

  2952   fixes U :: "'a :: first_countable_topology set"

  2953   assumes "compact U" shows "seq_compact U"

  2954   unfolding seq_compact_def

  2955 proof safe

  2956   fix X :: "nat \<Rightarrow> 'a" assume "\<forall>n. X n \<in> U"

  2957   then have "eventually (\<lambda>x. x \<in> U) (filtermap X sequentially)"

  2958     by (auto simp: eventually_filtermap)

  2959   moreover have "filtermap X sequentially \<noteq> bot"

  2960     by (simp add: trivial_limit_def eventually_filtermap)

  2961   ultimately obtain x where "x \<in> U" and x: "inf (nhds x) (filtermap X sequentially) \<noteq> bot" (is "?F \<noteq> _")

  2962     using compact U by (auto simp: compact_filter)

  2963

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

  2965   def s \<equiv> "\<lambda>n i. SOME j. i < j \<and> X j \<in> A (Suc n)"

  2966   { fix n i

  2967     have "\<exists>a. i < a \<and> X a \<in> A (Suc n)"

  2968     proof (rule ccontr)

  2969       assume "\<not> (\<exists>a>i. X a \<in> A (Suc n))"

  2970       then have "\<And>a. Suc i \<le> a \<Longrightarrow> X a \<notin> A (Suc n)" by auto

  2971       then have "eventually (\<lambda>x. x \<notin> A (Suc n)) (filtermap X sequentially)"

  2972         by (auto simp: eventually_filtermap eventually_sequentially)

  2973       moreover have "eventually (\<lambda>x. x \<in> A (Suc n)) (nhds x)"

  2974         using A(1,2)[of "Suc n"] by (auto simp: eventually_nhds)

  2975       ultimately have "eventually (\<lambda>x. False) ?F"

  2976         by (auto simp add: eventually_inf)

  2977       with x show False

  2978         by (simp add: eventually_False)

  2979     qed

  2980     then have "i < s n i" "X (s n i) \<in> A (Suc n)"

  2981       unfolding s_def by (auto intro: someI2_ex) }

  2982   note s = this

  2983   def r \<equiv> "nat_rec (s 0 0) s"

  2984   have "subseq r"

  2985     by (auto simp: r_def s subseq_Suc_iff)

  2986   moreover

  2987   have "(\<lambda>n. X (r n)) ----> x"

  2988   proof (rule topological_tendstoI)

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

  2990     with A(3) have "eventually (\<lambda>i. A i \<subseteq> S) sequentially" by auto

  2991     moreover

  2992     { fix i assume "Suc 0 \<le> i" then have "X (r i) \<in> A i"

  2993         by (cases i) (simp_all add: r_def s) }

  2994     then have "eventually (\<lambda>i. X (r i) \<in> A i) sequentially" by (auto simp: eventually_sequentially)

  2995     ultimately show "eventually (\<lambda>i. X (r i) \<in> S) sequentially"

  2996       by eventually_elim auto

  2997   qed

  2998   ultimately show "\<exists>x \<in> U. \<exists>r. subseq r \<and> (X \<circ> r) ----> x"

  2999     using x \<in> U by (auto simp: convergent_def comp_def)

  3000 qed

  3001

  3002 lemma seq_compactI:

  3003   assumes "\<And>f. \<forall>n. f n \<in> S \<Longrightarrow> \<exists>l\<in>S. \<exists>r. subseq r \<and> ((f o r) ---> l) sequentially"

  3004   shows "seq_compact S"

  3005   unfolding seq_compact_def using assms by fast

  3006

  3007 lemma seq_compactE:

  3008   assumes "seq_compact S" "\<forall>n. f n \<in> S"

  3009   obtains l r where "l \<in> S" "subseq r" "((f \<circ> r) ---> l) sequentially"

  3010   using assms unfolding seq_compact_def by fast

  3011

  3012 lemma countably_compact_imp_acc_point:

  3013   assumes "countably_compact s" "countable t" "infinite t"  "t \<subseteq> s"

  3014   shows "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t)"

  3015 proof (rule ccontr)

  3016   def C \<equiv> "(\<lambda>F. interior (F \<union> (- t)))  {F. finite F \<and> F \<subseteq> t }"

  3017   note countably_compact s

  3018   moreover have "\<forall>t\<in>C. open t"

  3019     by (auto simp: C_def)

  3020   moreover

  3021   assume "\<not> (\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> t))"

  3022   then have s: "\<And>x. x \<in> s \<Longrightarrow> \<exists>U. x\<in>U \<and> open U \<and> finite (U \<inter> t)" by metis

  3023   have "s \<subseteq> \<Union>C"

  3024     using t \<subseteq> s

  3025     unfolding C_def Union_image_eq

  3026     apply (safe dest!: s)

  3027     apply (rule_tac a="U \<inter> t" in UN_I)

  3028     apply (auto intro!: interiorI simp add: finite_subset)

  3029     done

  3030   moreover

  3031   from countable t have "countable C"

  3032     unfolding C_def by (auto intro: countable_Collect_finite_subset)

  3033   ultimately guess D by (rule countably_compactE)

  3034   then obtain E where E: "E \<subseteq> {F. finite F \<and> F \<subseteq> t }" "finite E" and

  3035     s: "s \<subseteq> (\<Union>F\<in>E. interior (F \<union> (- t)))"

  3036     by (metis (lifting) Union_image_eq finite_subset_image C_def)

  3037   from s t \<subseteq> s have "t \<subseteq> \<Union>E"

  3038     using interior_subset by blast

  3039   moreover have "finite (\<Union>E)"

  3040     using E by auto

  3041   ultimately show False using infinite t by (auto simp: finite_subset)

  3042 qed

  3043

  3044 lemma countable_acc_point_imp_seq_compact:

  3045   fixes s :: "'a::first_countable_topology set"

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

  3047   shows "seq_compact s"

  3048 proof -

  3049   { fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"

  3050     have "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3051     proof (cases "finite (range f)")

  3052       case True

  3053       obtain l where "infinite {n. f n = f l}"

  3054         using pigeonhole_infinite[OF _ True] by auto

  3055       then obtain r where "subseq r" and fr: "\<forall>n. f (r n) = f l"

  3056         using infinite_enumerate by blast

  3057       hence "subseq r \<and> (f \<circ> r) ----> f l"

  3058         by (simp add: fr tendsto_const o_def)

  3059       with f show "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  3060         by auto

  3061     next

  3062       case False

  3063       with f assms have "\<exists>x\<in>s. \<forall>U. x\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" by auto

  3064       then obtain l where "l \<in> s" "\<forall>U. l\<in>U \<and> open U \<longrightarrow> infinite (U \<inter> range f)" ..

  3065       from this(2) have "\<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3066         using acc_point_range_imp_convergent_subsequence[of l f] by auto

  3067       with l \<in> s show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" ..

  3068     qed

  3069   }

  3070   thus ?thesis unfolding seq_compact_def by auto

  3071 qed

  3072

  3073 lemma seq_compact_eq_countably_compact:

  3074   fixes U :: "'a :: first_countable_topology set"

  3075   shows "seq_compact U \<longleftrightarrow> countably_compact U"

  3076   using

  3077     countable_acc_point_imp_seq_compact

  3078     countably_compact_imp_acc_point

  3079     seq_compact_imp_countably_compact

  3080   by metis

  3081

  3082 lemma seq_compact_eq_acc_point:

  3083   fixes s :: "'a :: first_countable_topology set"

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

  3085   using

  3086     countable_acc_point_imp_seq_compact[of s]

  3087     countably_compact_imp_acc_point[of s]

  3088     seq_compact_imp_countably_compact[of s]

  3089   by metis

  3090

  3091 lemma seq_compact_eq_compact:

  3092   fixes U :: "'a :: second_countable_topology set"

  3093   shows "seq_compact U \<longleftrightarrow> compact U"

  3094   using seq_compact_eq_countably_compact countably_compact_eq_compact by blast

  3095

  3096 lemma bolzano_weierstrass_imp_seq_compact:

  3097   fixes s :: "'a::{t1_space, first_countable_topology} set"

  3098   shows "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> seq_compact s"

  3099   by (rule countable_acc_point_imp_seq_compact) (metis islimpt_eq_acc_point)

  3100

  3101 subsubsection{* Total boundedness *}

  3102

  3103 lemma cauchy_def:

  3104   "Cauchy s \<longleftrightarrow> (\<forall>e>0. \<exists>N. \<forall>m n. m \<ge> N \<and> n \<ge> N --> dist(s m)(s n) < e)"

  3105 unfolding Cauchy_def by blast

  3106

  3107 fun helper_1::"('a::metric_space set) \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> 'a" where

  3108   "helper_1 s e n = (SOME y::'a. y \<in> s \<and> (\<forall>m<n. \<not> (dist (helper_1 s e m) y < e)))"

  3109 declare helper_1.simps[simp del]

  3110

  3111 lemma seq_compact_imp_totally_bounded:

  3112   assumes "seq_compact s"

  3113   shows "\<forall>e>0. \<exists>k. finite k \<and> k \<subseteq> s \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e)  k))"

  3114 proof(rule, rule, rule ccontr)

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

  3116   def x \<equiv> "helper_1 s e"

  3117   { fix n

  3118     have "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)"

  3119     proof(induct_tac rule:nat_less_induct)

  3120       fix n  def Q \<equiv> "(\<lambda>y. y \<in> s \<and> (\<forall>m<n. \<not> dist (x m) y < e))"

  3121       assume as:"\<forall>m<n. x m \<in> s \<and> (\<forall>ma<m. \<not> dist (x ma) (x m) < e)"

  3122       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

  3123       then obtain z where z:"z\<in>s" "z \<notin> (\<Union>x\<in>x  {0..<n}. ball x e)" unfolding subset_eq by auto

  3124       have "Q (x n)" unfolding x_def and helper_1.simps[of s e n]

  3125         apply(rule someI2[where a=z]) unfolding x_def[symmetric] and Q_def using z by auto

  3126       thus "x n \<in> s \<and> (\<forall>m<n. \<not> dist (x m) (x n) < e)" unfolding Q_def by auto

  3127     qed }

  3128   hence "\<forall>n::nat. x n \<in> s" and x:"\<forall>n. \<forall>m < n. \<not> (dist (x m) (x n) < e)" by blast+

  3129   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

  3130   from this(3) have "Cauchy (x \<circ> r)" using LIMSEQ_imp_Cauchy by auto

  3131   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

  3132   show False

  3133     using N[THEN spec[where x=N], THEN spec[where x="N+1"]]

  3134     using r[unfolded subseq_def, THEN spec[where x=N], THEN spec[where x="N+1"]]

  3135     using x[THEN spec[where x="r (N+1)"], THEN spec[where x="r (N)"]] by auto

  3136 qed

  3137

  3138 subsubsection{* Heine-Borel theorem *}

  3139

  3140 lemma seq_compact_imp_heine_borel:

  3141   fixes s :: "'a :: metric_space set"

  3142   assumes "seq_compact s" shows "compact s"

  3143 proof -

  3144   from seq_compact_imp_totally_bounded[OF seq_compact s]

  3145   guess f unfolding choice_iff' .. note f = this

  3146   def K \<equiv> "(\<lambda>(x, r). ball x r)  ((\<Union>e \<in> \<rat> \<inter> {0 <..}. f e) \<times> \<rat>)"

  3147   have "countably_compact s"

  3148     using seq_compact s by (rule seq_compact_imp_countably_compact)

  3149   then show "compact s"

  3150   proof (rule countably_compact_imp_compact)

  3151     show "countable K"

  3152       unfolding K_def using f

  3153       by (auto intro: countable_finite countable_subset countable_rat

  3154                intro!: countable_image countable_SIGMA countable_UN)

  3155     show "\<forall>b\<in>K. open b" by (auto simp: K_def)

  3156   next

  3157     fix T x assume T: "open T" "x \<in> T" and x: "x \<in> s"

  3158     from openE[OF T] obtain e where "0 < e" "ball x e \<subseteq> T" by auto

  3159     then have "0 < e / 2" "ball x (e / 2) \<subseteq> T" by auto

  3160     from Rats_dense_in_real[OF 0 < e / 2] obtain r where "r \<in> \<rat>" "0 < r" "r < e / 2" by auto

  3161     from f[rule_format, of r] 0 < r x \<in> s obtain k where "k \<in> f r" "x \<in> ball k r"

  3162       unfolding Union_image_eq by auto

  3163     from r \<in> \<rat> 0 < r k \<in> f r have "ball k r \<in> K" by (auto simp: K_def)

  3164     then show "\<exists>b\<in>K. x \<in> b \<and> b \<inter> s \<subseteq> T"

  3165     proof (rule bexI[rotated], safe)

  3166       fix y assume "y \<in> ball k r"

  3167       with r < e / 2 x \<in> ball k r have "dist x y < e"

  3168         by (intro dist_double[where x = k and d=e]) (auto simp: dist_commute)

  3169       with ball x e \<subseteq> T show "y \<in> T" by auto

  3170     qed (rule x \<in> ball k r)

  3171   qed

  3172 qed

  3173

  3174 lemma compact_eq_seq_compact_metric:

  3175   "compact (s :: 'a::metric_space set) \<longleftrightarrow> seq_compact s"

  3176   using compact_imp_seq_compact seq_compact_imp_heine_borel by blast

  3177

  3178 lemma compact_def:

  3179   "compact (S :: 'a::metric_space set) \<longleftrightarrow>

  3180    (\<forall>f. (\<forall>n. f n \<in> S) \<longrightarrow> (\<exists>l\<in>S. \<exists>r. subseq r \<and> (f o r) ----> l))"

  3181   unfolding compact_eq_seq_compact_metric seq_compact_def by auto

  3182

  3183 subsubsection {* Complete the chain of compactness variants *}

  3184

  3185 lemma compact_eq_bolzano_weierstrass:

  3186   fixes s :: "'a::metric_space set"

  3187   shows "compact s \<longleftrightarrow> (\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t))" (is "?lhs = ?rhs")

  3188 proof

  3189   assume ?lhs thus ?rhs using heine_borel_imp_bolzano_weierstrass[of s] by auto

  3190 next

  3191   assume ?rhs thus ?lhs

  3192     unfolding compact_eq_seq_compact_metric by (rule bolzano_weierstrass_imp_seq_compact)

  3193 qed

  3194

  3195 lemma bolzano_weierstrass_imp_bounded:

  3196   "\<forall>t. infinite t \<and> t \<subseteq> s --> (\<exists>x \<in> s. x islimpt t) \<Longrightarrow> bounded s"

  3197   using compact_imp_bounded unfolding compact_eq_bolzano_weierstrass .

  3198

  3199 text {*

  3200   A metric space (or topological vector space) is said to have the

  3201   Heine-Borel property if every closed and bounded subset is compact.

  3202 *}

  3203

  3204 class heine_borel = metric_space +

  3205   assumes bounded_imp_convergent_subsequence:

  3206     "bounded (range f) \<Longrightarrow> \<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3207

  3208 lemma bounded_closed_imp_seq_compact:

  3209   fixes s::"'a::heine_borel set"

  3210   assumes "bounded s" and "closed s" shows "seq_compact s"

  3211 proof (unfold seq_compact_def, clarify)

  3212   fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"

  3213   with bounded s have "bounded (range f)" by (auto intro: bounded_subset)

  3214   obtain l r where r: "subseq r" and l: "((f \<circ> r) ---> l) sequentially"

  3215     using bounded_imp_convergent_subsequence [OF bounded (range f)] by auto

  3216   from f have fr: "\<forall>n. (f \<circ> r) n \<in> s" by simp

  3217   have "l \<in> s" using closed s fr l

  3218     unfolding closed_sequential_limits by blast

  3219   show "\<exists>l\<in>s. \<exists>r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3220     using l \<in> s r l by blast

  3221 qed

  3222

  3223 lemma compact_eq_bounded_closed:

  3224   fixes s :: "'a::heine_borel set"

  3225   shows "compact s \<longleftrightarrow> bounded s \<and> closed s"  (is "?lhs = ?rhs")

  3226 proof

  3227   assume ?lhs thus ?rhs

  3228     using compact_imp_closed compact_imp_bounded by blast

  3229 next

  3230   assume ?rhs thus ?lhs

  3231     using bounded_closed_imp_seq_compact[of s] unfolding compact_eq_seq_compact_metric by auto

  3232 qed

  3233

  3234 (* TODO: is this lemma necessary? *)

  3235 lemma bounded_increasing_convergent:

  3236   fixes s :: "nat \<Rightarrow> real"

  3237   shows "bounded {s n| n. True} \<Longrightarrow> \<forall>n. s n \<le> s (Suc n) \<Longrightarrow> \<exists>l. s ----> l"

  3238   using Bseq_mono_convergent[of s] incseq_Suc_iff[of s]

  3239   by (auto simp: image_def Bseq_eq_bounded convergent_def incseq_def)

  3240

  3241 instance real :: heine_borel

  3242 proof

  3243   fix f :: "nat \<Rightarrow> real"

  3244   assume f: "bounded (range f)"

  3245   obtain r where r: "subseq r" "monoseq (f \<circ> r)"

  3246     unfolding comp_def by (metis seq_monosub)

  3247   moreover

  3248   then have "Bseq (f \<circ> r)"

  3249     unfolding Bseq_eq_bounded using f by (auto intro: bounded_subset)

  3250   ultimately show "\<exists>l r. subseq r \<and> (f \<circ> r) ----> l"

  3251     using Bseq_monoseq_convergent[of "f \<circ> r"] by (auto simp: convergent_def)

  3252 qed

  3253

  3254 lemma compact_lemma:

  3255   fixes f :: "nat \<Rightarrow> 'a::euclidean_space"

  3256   assumes "bounded (range f)"

  3257   shows "\<forall>d\<subseteq>Basis. \<exists>l::'a. \<exists> r. subseq r \<and>

  3258         (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially)"

  3259 proof safe

  3260   fix d :: "'a set" assume d: "d \<subseteq> Basis"

  3261   with finite_Basis have "finite d" by (blast intro: finite_subset)

  3262   from this d show "\<exists>l::'a. \<exists>r. subseq r \<and>

  3263       (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially)"

  3264   proof(induct d) case empty thus ?case unfolding subseq_def by auto

  3265   next case (insert k d) have k[intro]:"k\<in>Basis" using insert by auto

  3266     have s': "bounded ((\<lambda>x. x \<bullet> k)  range f)" using bounded (range f)

  3267       by (auto intro!: bounded_linear_image bounded_linear_inner_left)

  3268     obtain l1::"'a" and r1 where r1:"subseq r1" and

  3269       lr1:"\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) \<bullet> i) (l1 \<bullet> i) < e) sequentially"

  3270       using insert(3) using insert(4) by auto

  3271     have f': "\<forall>n. f (r1 n) \<bullet> k \<in> (\<lambda>x. x \<bullet> k)  range f" by simp

  3272     have "bounded (range (\<lambda>i. f (r1 i) \<bullet> k))"

  3273       by (metis (lifting) bounded_subset f' image_subsetI s')

  3274     then obtain l2 r2 where r2:"subseq r2" and lr2:"((\<lambda>i. f (r1 (r2 i)) \<bullet> k) ---> l2) sequentially"

  3275       using bounded_imp_convergent_subsequence[of "\<lambda>i. f (r1 i) \<bullet> k"] by (auto simp: o_def)

  3276     def r \<equiv> "r1 \<circ> r2" have r:"subseq r"

  3277       using r1 and r2 unfolding r_def o_def subseq_def by auto

  3278     moreover

  3279     def l \<equiv> "(\<Sum>i\<in>Basis. (if i = k then l2 else l1\<bullet>i) *\<^sub>R i)::'a"

  3280     { fix e::real assume "e>0"

  3281       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

  3282       from lr2 e>0 have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) \<bullet> k) l2 < e) sequentially" by (rule tendstoD)

  3283       from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) \<bullet> i) (l1 \<bullet> i) < e) sequentially"

  3284         by (rule eventually_subseq)

  3285       have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially"

  3286         using N1' N2

  3287         by eventually_elim (insert insert.prems, auto simp: l_def r_def o_def)

  3288     }

  3289     ultimately show ?case by auto

  3290   qed

  3291 qed

  3292

  3293 instance euclidean_space \<subseteq> heine_borel

  3294 proof

  3295   fix f :: "nat \<Rightarrow> 'a"

  3296   assume f: "bounded (range f)"

  3297   then obtain l::'a and r where r: "subseq r"

  3298     and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e) sequentially"

  3299     using compact_lemma [OF f] by blast

  3300   { fix e::real assume "e>0"

  3301     hence "0 < e / real_of_nat DIM('a)" by (auto intro!: divide_pos_pos DIM_positive)

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

  3303       by simp

  3304     moreover

  3305     { fix n assume n: "\<forall>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i) < e / (real_of_nat DIM('a))"

  3306       have "dist (f (r n)) l \<le> (\<Sum>i\<in>Basis. dist (f (r n) \<bullet> i) (l \<bullet> i))"

  3307         apply(subst euclidean_dist_l2) using zero_le_dist by (rule setL2_le_setsum)

  3308       also have "\<dots> < (\<Sum>i\<in>(Basis::'a set). e / (real_of_nat DIM('a)))"

  3309         apply(rule setsum_strict_mono) using n by auto

  3310       finally have "dist (f (r n)) l < e"

  3311         by auto

  3312     }

  3313     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"

  3314       by (rule eventually_elim1)

  3315   }

  3316   hence *:"((f \<circ> r) ---> l) sequentially" unfolding o_def tendsto_iff by simp

  3317   with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" by auto

  3318 qed

  3319

  3320 lemma bounded_fst: "bounded s \<Longrightarrow> bounded (fst  s)"

  3321 unfolding bounded_def

  3322 apply clarify

  3323 apply (rule_tac x="a" in exI)

  3324 apply (rule_tac x="e" in exI)

  3325 apply clarsimp

  3326 apply (drule (1) bspec)

  3327 apply (simp add: dist_Pair_Pair)

  3328 apply (erule order_trans [OF real_sqrt_sum_squares_ge1])

  3329 done

  3330

  3331 lemma bounded_snd: "bounded s \<Longrightarrow> bounded (snd  s)"

  3332 unfolding bounded_def

  3333 apply clarify

  3334 apply (rule_tac x="b" in exI)

  3335 apply (rule_tac x="e" in exI)

  3336 apply clarsimp

  3337 apply (drule (1) bspec)

  3338 apply (simp add: dist_Pair_Pair)

  3339 apply (erule order_trans [OF real_sqrt_sum_squares_ge2])

  3340 done

  3341

  3342 instance prod :: (heine_borel, heine_borel) heine_borel

  3343 proof

  3344   fix f :: "nat \<Rightarrow> 'a \<times> 'b"

  3345   assume f: "bounded (range f)"

  3346   from f have s1: "bounded (range (fst \<circ> f))" unfolding image_comp by (rule bounded_fst)

  3347   obtain l1 r1 where r1: "subseq r1" and l1: "(\<lambda>n. fst (f (r1 n))) ----> l1"

  3348     using bounded_imp_convergent_subsequence [OF s1] unfolding o_def by fast

  3349   from f have s2: "bounded (range (snd \<circ> f \<circ> r1))"

  3350     by (auto simp add: image_comp intro: bounded_snd bounded_subset)

  3351   obtain l2 r2 where r2: "subseq r2"

  3352     and l2: "((\<lambda>n. snd (f (r1 (r2 n)))) ---> l2) sequentially"

  3353     using bounded_imp_convergent_subsequence [OF s2]

  3354     unfolding o_def by fast

  3355   have l1': "((\<lambda>n. fst (f (r1 (r2 n)))) ---> l1) sequentially"

  3356     using LIMSEQ_subseq_LIMSEQ [OF l1 r2] unfolding o_def .

  3357   have l: "((f \<circ> (r1 \<circ> r2)) ---> (l1, l2)) sequentially"

  3358     using tendsto_Pair [OF l1' l2] unfolding o_def by simp

  3359   have r: "subseq (r1 \<circ> r2)"

  3360     using r1 r2 unfolding subseq_def by simp

  3361   show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially"

  3362     using l r by fast

  3363 qed

  3364

  3365 subsubsection{* Completeness *}

  3366

  3367 definition complete :: "'a::metric_space set \<Rightarrow> bool" where

  3368   "complete s \<longleftrightarrow> (\<forall>f. (\<forall>n. f n \<in> s) \<and> Cauchy f \<longrightarrow> (\<exists>l\<in>s. f ----> l))"

  3369

  3370 lemma compact_imp_complete: assumes "compact s" shows "complete s"

  3371 proof-

  3372   { fix f assume as: "(\<forall>n::nat. f n \<in> s)" "Cauchy f"

  3373     from as(1) obtain l r where lr: "l\<in>s" "subseq r" "(f \<circ> r) ----> l"

  3374       using assms unfolding compact_def by blast

  3375

  3376     note lr' = seq_suble [OF lr(2)]

  3377

  3378     { fix e::real assume "e>0"

  3379       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

  3380       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

  3381       { fix n::nat assume n:"n \<ge> max N M"

  3382         have "dist ((f \<circ> r) n) l < e/2" using n M by auto

  3383         moreover have "r n \<ge> N" using lr'[of n] n by auto

  3384         hence "dist (f n) ((f \<circ> r) n) < e / 2" using N using n by auto

  3385         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)  }

  3386       hence "\<exists>N. \<forall>n\<ge>N. dist (f n) l < e" by blast  }

  3387     hence "\<exists>l\<in>s. (f ---> l) sequentially" using l\<in>s unfolding LIMSEQ_def by auto  }

  3388   thus ?thesis unfolding complete_def by auto

  3389 qed

  3390

  3391 lemma nat_approx_posE:

  3392   fixes e::real

  3393   assumes "0 < e"

  3394   obtains n::nat where "1 / (Suc n) < e"

  3395 proof atomize_elim

  3396   have " 1 / real (Suc (nat (ceiling (1/e)))) < 1 / (ceiling (1/e))"

  3397     by (rule divide_strict_left_mono) (auto intro!: mult_pos_pos simp: 0 < e)

  3398   also have "1 / (ceiling (1/e)) \<le> 1 / (1/e)"

  3399     by (rule divide_left_mono) (auto intro!: divide_pos_pos simp: 0 < e)

  3400   also have "\<dots> = e" by simp

  3401   finally show  "\<exists>n. 1 / real (Suc n) < e" ..

  3402 qed

  3403

  3404 lemma compact_eq_totally_bounded:

  3405   "compact s \<longleftrightarrow> complete s \<and> (\<forall>e>0. \<exists>k. finite k \<and> s \<subseteq> (\<Union>((\<lambda>x. ball x e)  k)))"

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

  3407 proof

  3408   assume assms: "?rhs"

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

  3410     by (auto simp: choice_iff')

  3411

  3412   show "compact s"

  3413   proof cases

  3414     assume "s = {}" thus "compact s" by (simp add: compact_def)

  3415   next

  3416     assume "s \<noteq> {}"

  3417     show ?thesis

  3418       unfolding compact_def

  3419     proof safe

  3420       fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> s"

  3421

  3422       def e \<equiv> "\<lambda>n. 1 / (2 * Suc n)"

  3423       then have [simp]: "\<And>n. 0 < e n" by auto

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

  3425       { fix n U assume "infinite {n. f n \<in> U}"

  3426         then have "\<exists>b\<in>k (e n). infinite {i\<in>{n. f n \<in> U}. f i \<in> ball b (e n)}"

  3427           using k f by (intro pigeonhole_infinite_rel) (auto simp: subset_eq)

  3428         then guess a ..

  3429         then have "\<exists>b. infinite {i. f i \<in> b} \<and> (\<exists>x. b \<subseteq> ball x (e n) \<inter> U)"

  3430           by (intro exI[of _ "ball a (e n) \<inter> U"] exI[of _ a]) (auto simp: ac_simps)

  3431         from someI_ex[OF this]

  3432         have "infinite {i. f i \<in> B n U}" "\<exists>x. B n U \<subseteq> ball x (e n) \<inter> U"

  3433           unfolding B_def by auto }

  3434       note B = this

  3435

  3436       def F \<equiv> "nat_rec (B 0 UNIV) B"

  3437       { fix n have "infinite {i. f i \<in> F n}"

  3438           by (induct n) (auto simp: F_def B) }

  3439       then have F: "\<And>n. \<exists>x. F (Suc n) \<subseteq> ball x (e n) \<inter> F n"

  3440         using B by (simp add: F_def)

  3441       then have F_dec: "\<And>m n. m \<le> n \<Longrightarrow> F n \<subseteq> F m"

  3442         using decseq_SucI[of F] by (auto simp: decseq_def)

  3443

  3444       obtain sel where sel: "\<And>k i. i < sel k i" "\<And>k i. f (sel k i) \<in> F k"

  3445       proof (atomize_elim, unfold all_conj_distrib[symmetric], intro choice allI)

  3446         fix k i

  3447         have "infinite ({n. f n \<in> F k} - {.. i})"

  3448           using infinite {n. f n \<in> F k} by auto

  3449         from infinite_imp_nonempty[OF this]

  3450         show "\<exists>x>i. f x \<in> F k"

  3451           by (simp add: set_eq_iff not_le conj_commute)

  3452       qed

  3453

  3454       def t \<equiv> "nat_rec (sel 0 0) (\<lambda>n i. sel (Suc n) i)"

  3455       have "subseq t"

  3456         unfolding subseq_Suc_iff by (simp add: t_def sel)

  3457       moreover have "\<forall>i. (f \<circ> t) i \<in> s"

  3458         using f by auto

  3459       moreover

  3460       { fix n have "(f \<circ> t) n \<in> F n"

  3461           by (cases n) (simp_all add: t_def sel) }

  3462       note t = this

  3463

  3464       have "Cauchy (f \<circ> t)"

  3465       proof (safe intro!: metric_CauchyI exI elim!: nat_approx_posE)

  3466         fix r :: real and N n m assume "1 / Suc N < r" "Suc N \<le> n" "Suc N \<le> m"

  3467         then have "(f \<circ> t) n \<in> F (Suc N)" "(f \<circ> t) m \<in> F (Suc N)" "2 * e N < r"

  3468           using F_dec t by (auto simp: e_def field_simps real_of_nat_Suc)

  3469         with F[of N] obtain x where "dist x ((f \<circ> t) n) < e N" "dist x ((f \<circ> t) m) < e N"

  3470           by (auto simp: subset_eq)

  3471         with dist_triangle[of "(f \<circ> t) m" "(f \<circ> t) n" x] 2 * e N < r

  3472         show "dist ((f \<circ> t) m) ((f \<circ> t) n) < r"

  3473           by (simp add: dist_commute)

  3474       qed

  3475

  3476       ultimately show "\<exists>l\<in>s. \<exists>r. subseq r \<and> (f \<circ> r) ----> l"

  3477         using assms unfolding complete_def by blast

  3478     qed

  3479   qed

  3480 qed (metis compact_imp_complete compact_imp_seq_compact seq_compact_imp_totally_bounded)

  3481

  3482 lemma cauchy: "Cauchy s \<longleftrightarrow> (\<forall>e>0.\<exists> N::nat. \<forall>n\<ge>N. dist(s n)(s N) < e)" (is "?lhs = ?rhs")

  3483 proof-

  3484   { assume ?rhs

  3485     { fix e::real

  3486       assume "e>0"

  3487       with ?rhs obtain N where N:"\<forall>n\<ge>N. dist (s n) (s N) < e/2"

  3488         by (erule_tac x="e/2" in allE) auto

  3489       { fix n m

  3490         assume nm:"N \<le> m \<and> N \<le> n"

  3491         hence "dist (s m) (s n) < e" using N

  3492           using dist_triangle_half_l[of "s m" "s N" "e" "s n"]

  3493           by blast

  3494       }

  3495       hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (s m) (s n) < e"

  3496         by blast

  3497     }

  3498     hence ?lhs

  3499       unfolding cauchy_def

  3500       by blast

  3501   }

  3502   thus ?thesis

  3503     unfolding cauchy_def

  3504     using dist_triangle_half_l

  3505     by blast

  3506 qed

  3507

  3508 lemma cauchy_imp_bounded: assumes "Cauchy s" shows "bounded (range s)"

  3509 proof-

  3510   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

  3511   hence N:"\<forall>n. N \<le> n \<longrightarrow> dist (s N) (s n) < 1" by auto

  3512   moreover

  3513   have "bounded (s  {0..N})" using finite_imp_bounded[of "s  {1..N}"] by auto

  3514   then obtain a where a:"\<forall>x\<in>s  {0..N}. dist (s N) x \<le> a"

  3515     unfolding bounded_any_center [where a="s N"] by auto

  3516   ultimately show "?thesis"

  3517     unfolding bounded_any_center [where a="s N"]

  3518     apply(rule_tac x="max a 1" in exI) apply auto

  3519     apply(erule_tac x=y in allE) apply(erule_tac x=y in ballE) by auto

  3520 qed

  3521

  3522 instance heine_borel < complete_space

  3523 proof

  3524   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"

  3525   hence "bounded (range f)"

  3526     by (rule cauchy_imp_bounded)

  3527   hence "compact (closure (range f))"

  3528     unfolding compact_eq_bounded_closed by auto

  3529   hence "complete (closure (range f))"

  3530     by (rule compact_imp_complete)

  3531   moreover have "\<forall>n. f n \<in> closure (range f)"

  3532     using closure_subset [of "range f"] by auto

  3533   ultimately have "\<exists>l\<in>closure (range f). (f ---> l) sequentially"

  3534     using Cauchy f unfolding complete_def by auto

  3535   then show "convergent f"

  3536     unfolding convergent_def by auto

  3537 qed

  3538

  3539 instance euclidean_space \<subseteq> banach ..

  3540

  3541 lemma complete_univ: "complete (UNIV :: 'a::complete_space set)"

  3542 proof(simp add: complete_def, rule, rule)

  3543   fix f :: "nat \<Rightarrow> 'a" assume "Cauchy f"

  3544   hence "convergent f" by (rule Cauchy_convergent)

  3545   thus "\<exists>l. f ----> l" unfolding convergent_def .

  3546 qed

  3547

  3548 lemma complete_imp_closed: assumes "complete s" shows "closed s"

  3549 proof -

  3550   { fix x assume "x islimpt s"

  3551     then obtain f where f: "\<forall>n. f n \<in> s - {x}" "(f ---> x) sequentially"

  3552       unfolding islimpt_sequential by auto

  3553     then obtain l where l: "l\<in>s" "(f ---> l) sequentially"

  3554       using complete s[unfolded complete_def] using LIMSEQ_imp_Cauchy[of f x] by auto

  3555     hence "x \<in> s"  using tendsto_unique[of sequentially f l x] trivial_limit_sequentially f(2) by auto

  3556   }

  3557   thus "closed s" unfolding closed_limpt by auto

  3558 qed

  3559

  3560 lemma complete_eq_closed:

  3561   fixes s :: "'a::complete_space set"

  3562   shows "complete s \<longleftrightarrow> closed s" (is "?lhs = ?rhs")

  3563 proof

  3564   assume ?lhs thus ?rhs by (rule complete_imp_closed)

  3565 next

  3566   assume ?rhs

  3567   { fix f assume as:"\<forall>n::nat. f n \<in> s" "Cauchy f"

  3568     then obtain l where "(f ---> l) sequentially" using complete_univ[unfolded complete_def, THEN spec[where x=f]] by auto

  3569     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  }

  3570   thus ?lhs unfolding complete_def by auto

  3571 qed

  3572

  3573 lemma convergent_eq_cauchy:

  3574   fixes s :: "nat \<Rightarrow> 'a::complete_space"

  3575   shows "(\<exists>l. (s ---> l) sequentially) \<longleftrightarrow> Cauchy s"

  3576   unfolding Cauchy_convergent_iff convergent_def ..

  3577

  3578 lemma convergent_imp_bounded:

  3579   fixes s :: "nat \<Rightarrow> 'a::metric_space"

  3580   shows "(s ---> l) sequentially \<Longrightarrow> bounded (range s)"

  3581   by (intro cauchy_imp_bounded LIMSEQ_imp_Cauchy)

  3582

  3583 lemma compact_cball[simp]:

  3584   fixes x :: "'a::heine_borel"

  3585   shows "compact(cball x e)"

  3586   using compact_eq_bounded_closed bounded_cball closed_cball

  3587   by blast

  3588

  3589 lemma compact_frontier_bounded[intro]:

  3590   fixes s :: "'a::heine_borel set"

  3591   shows "bounded s ==> compact(frontier s)"

  3592   unfolding frontier_def

  3593   using compact_eq_bounded_closed

  3594   by blast

  3595

  3596 lemma compact_frontier[intro]:

  3597   fixes s :: "'a::heine_borel set"

  3598   shows "compact s ==> compact (frontier s)"

  3599   using compact_eq_bounded_closed compact_frontier_bounded

  3600   by blast

  3601

  3602 lemma frontier_subset_compact:

  3603   fixes s :: "'a::heine_borel set"

  3604   shows "compact s ==> frontier s \<subseteq> s"

  3605   using frontier_subset_closed compact_eq_bounded_closed

  3606   by blast

  3607

  3608 subsection {* Bounded closed nest property (proof does not use Heine-Borel) *}

  3609

  3610 lemma bounded_closed_nest:

  3611   assumes "\<forall>n. closed(s n)" "\<forall>n. (s n \<noteq> {})"

  3612   "(\<forall>m n. m \<le> n --> s n \<subseteq> s m)"  "bounded(s 0)"

  3613   shows "\<exists>a::'a::heine_borel. \<forall>n::nat. a \<in> s(n)"

  3614 proof-

  3615   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

  3616   from assms(4,1) have *:"seq_compact (s 0)" using bounded_closed_imp_seq_compact[of "s 0"] by auto

  3617

  3618   then obtain l r where lr:"l\<in>s 0" "subseq r" "((x \<circ> r) ---> l) sequentially"

  3619     unfolding seq_compact_def apply(erule_tac x=x in allE)  using x using assms(3) by blast

  3620

  3621   { fix n::nat

  3622     { fix e::real assume "e>0"

  3623       with lr(3) obtain N where N:"\<forall>m\<ge>N. dist ((x \<circ> r) m) l < e" unfolding LIMSEQ_def by auto

  3624       hence "dist ((x \<circ> r) (max N n)) l < e" by auto

  3625       moreover

  3626       have "r (max N n) \<ge> n" using lr(2) using seq_suble[of r "max N n"] by auto

  3627       hence "(x \<circ> r) (max N n) \<in> s n"

  3628         using x apply(erule_tac x=n in allE)

  3629         using x apply(erule_tac x="r (max N n)" in allE)

  3630         using assms(3) apply(erule_tac x=n in allE) apply(erule_tac x="r (max N n)" in allE) by auto

  3631       ultimately have "\<exists>y\<in>s n. dist y l < e" by auto

  3632     }

  3633     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by blast

  3634   }

  3635   thus ?thesis by auto

  3636 qed

  3637

  3638 text {* Decreasing case does not even need compactness, just completeness. *}

  3639

  3640 lemma decreasing_closed_nest:

  3641   assumes "\<forall>n. closed(s n)"

  3642           "\<forall>n. (s n \<noteq> {})"

  3643           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"

  3644           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y \<in> (s n). dist x y < e"

  3645   shows "\<exists>a::'a::complete_space. \<forall>n::nat. a \<in> s n"

  3646 proof-

  3647   have "\<forall>n. \<exists> x. x\<in>s n" using assms(2) by auto

  3648   hence "\<exists>t. \<forall>n. t n \<in> s n" using choice[of "\<lambda> n x. x \<in> s n"] by auto

  3649   then obtain t where t: "\<forall>n. t n \<in> s n" by auto

  3650   { fix e::real assume "e>0"

  3651     then obtain N where N:"\<forall>x\<in>s N. \<forall>y\<in>s N. dist x y < e" using assms(4) by auto

  3652     { fix m n ::nat assume "N \<le> m \<and> N \<le> n"

  3653       hence "t m \<in> s N" "t n \<in> s N" using assms(3) t unfolding  subset_eq t by blast+

  3654       hence "dist (t m) (t n) < e" using N by auto

  3655     }

  3656     hence "\<exists>N. \<forall>m n. N \<le> m \<and> N \<le> n \<longrightarrow> dist (t m) (t n) < e" by auto

  3657   }

  3658   hence  "Cauchy t" unfolding cauchy_def by auto

  3659   then obtain l where l:"(t ---> l) sequentially" using complete_univ unfolding complete_def by auto

  3660   { fix n::nat

  3661     { fix e::real assume "e>0"

  3662       then obtain N::nat where N:"\<forall>n\<ge>N. dist (t n) l < e" using l[unfolded LIMSEQ_def] by auto

  3663       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

  3664       hence "\<exists>y\<in>s n. dist y l < e" apply(rule_tac x="t (max n N)" in bexI) using N by auto

  3665     }

  3666     hence "l \<in> s n" using closed_approachable[of "s n" l] assms(1) by auto

  3667   }

  3668   then show ?thesis by auto

  3669 qed

  3670

  3671 text {* Strengthen it to the intersection actually being a singleton. *}

  3672

  3673 lemma decreasing_closed_nest_sing:

  3674   fixes s :: "nat \<Rightarrow> 'a::complete_space set"

  3675   assumes "\<forall>n. closed(s n)"

  3676           "\<forall>n. s n \<noteq> {}"

  3677           "\<forall>m n. m \<le> n --> s n \<subseteq> s m"

  3678           "\<forall>e>0. \<exists>n. \<forall>x \<in> (s n). \<forall> y\<in>(s n). dist x y < e"

  3679   shows "\<exists>a. \<Inter>(range s) = {a}"

  3680 proof-

  3681   obtain a where a:"\<forall>n. a \<in> s n" using decreasing_closed_nest[of s] using assms by auto

  3682   { fix b assume b:"b \<in> \<Inter>(range s)"

  3683     { fix e::real assume "e>0"

  3684       hence "dist a b < e" using assms(4 )using b using a by blast

  3685     }

  3686     hence "dist a b = 0" by (metis dist_eq_0_iff dist_nz less_le)

  3687   }

  3688   with a have "\<Inter>(range s) = {a}" unfolding image_def by auto

  3689   thus ?thesis ..

  3690 qed

  3691

  3692 text{* Cauchy-type criteria for uniform convergence. *}

  3693

  3694 lemma uniformly_convergent_eq_cauchy: fixes s::"nat \<Rightarrow> 'b \<Rightarrow> 'a::complete_space" shows

  3695  "(\<exists>l. \<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e) \<longleftrightarrow>

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

  3697 proof(rule)

  3698   assume ?lhs

  3699   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

  3700   { fix e::real assume "e>0"

  3701     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

  3702     { fix n m::nat and x::"'b" assume "N \<le> m \<and> N \<le> n \<and> P x"

  3703       hence "dist (s m x) (s n x) < e"

  3704         using N[THEN spec[where x=m], THEN spec[where x=x]]

  3705         using N[THEN spec[where x=n], THEN spec[where x=x]]

  3706         using dist_triangle_half_l[of "s m x" "l x" e "s n x"] by auto  }

  3707     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  }

  3708   thus ?rhs by auto

  3709 next

  3710   assume ?rhs

  3711   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

  3712   then obtain l where l:"\<forall>x. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l x) sequentially" unfolding convergent_eq_cauchy[THEN sym]

  3713     using choice[of "\<lambda>x l. P x \<longrightarrow> ((\<lambda>n. s n x) ---> l) sequentially"] by auto

  3714   { fix e::real assume "e>0"

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

  3716       using ?rhs[THEN spec[where x="e/2"]] by auto

  3717     { fix x assume "P x"

  3718       then obtain M where M:"\<forall>n\<ge>M. dist (s n x) (l x) < e/2"

  3719         using l[THEN spec[where x=x], unfolded LIMSEQ_def] using e>0 by(auto elim!: allE[where x="e/2"])

  3720       fix n::nat assume "n\<ge>N"

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

  3722         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)  }

  3723     hence "\<exists>N. \<forall>n x. N \<le> n \<and> P x \<longrightarrow> dist(s n x)(l x) < e" by auto }

  3724   thus ?lhs by auto

  3725 qed

  3726

  3727 lemma uniformly_cauchy_imp_uniformly_convergent:

  3728   fixes s :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::complete_space"

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

  3730           "\<forall>x. P x --> (\<forall>e>0. \<exists>N. \<forall>n. N \<le> n --> dist(s n x)(l x) < e)"

  3731   shows "\<forall>e>0. \<exists>N. \<forall>n x. N \<le> n \<and> P x --> dist(s n x)(l x) < e"

  3732 proof-

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

  3734     using assms(1) unfolding uniformly_convergent_eq_cauchy[THEN sym] by auto

  3735   moreover

  3736   { fix x assume "P x"

  3737     hence "l x = l' x" using tendsto_unique[OF trivial_limit_sequentially, of "\<lambda>n. s n x" "l x" "l' x"]

  3738       using l and assms(2) unfolding LIMSEQ_def by blast  }

  3739   ultimately show ?thesis by auto

  3740 qed

  3741

  3742

  3743 subsection {* Continuity *}

  3744

  3745 text {* Define continuity over a net to take in restrictions of the set. *}

  3746

  3747 definition

  3748   continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"

  3749   where "continuous net f \<longleftrightarrow> (f ---> f(netlimit net)) net"

  3750

  3751 lemma continuous_trivial_limit:

  3752  "trivial_limit net ==> continuous net f"

  3753   unfolding continuous_def tendsto_def trivial_limit_eq by auto

  3754

  3755 lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f(x)) (at x within s)"

  3756   unfolding continuous_def

  3757   unfolding tendsto_def

  3758   using netlimit_within[of x s]

  3759   by (cases "trivial_limit (at x within s)") (auto simp add: trivial_limit_eventually)

  3760

  3761 lemma continuous_at: "continuous (at x) f \<longleftrightarrow> (f ---> f(x)) (at x)"

  3762   using continuous_within [of x UNIV f] by simp

  3763

  3764 lemma continuous_isCont: "isCont f x = continuous (at x) f"

  3765   unfolding isCont_def LIM_def

  3766   unfolding continuous_at Lim_at unfolding dist_nz by auto

  3767

  3768 lemma continuous_at_within:

  3769   assumes "continuous (at x) f"  shows "continuous (at x within s) f"

  3770   using assms unfolding continuous_at continuous_within

  3771   by (rule Lim_at_within)

  3772

  3773 text{* Derive the epsilon-delta forms, which we often use as "definitions" *}

  3774

  3775 lemma continuous_within_eps_delta:

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

  3777   unfolding continuous_within and Lim_within

  3778   apply auto unfolding dist_nz[THEN sym] apply(auto del: allE elim!:allE) apply(rule_tac x=d in exI) by auto

  3779

  3780 lemma continuous_at_eps_delta: "continuous (at x) f \<longleftrightarrow>  (\<forall>e>0. \<exists>d>0.

  3781                            \<forall>x'. dist x' x < d --> dist(f x')(f x) < e)"

  3782   using continuous_within_eps_delta [of x UNIV f] by simp

  3783

  3784 text{* Versions in terms of open balls. *}

  3785

  3786 lemma continuous_within_ball:

  3787  "continuous (at x within s) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.

  3788                             f  (ball x d \<inter> s) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")

  3789 proof

  3790   assume ?lhs

  3791   { fix e::real assume "e>0"

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

  3793       using ?lhs[unfolded continuous_within Lim_within] by auto

  3794     { fix y assume "y\<in>f  (ball x d \<inter> s)"

  3795       hence "y \<in> ball (f x) e" using d(2) unfolding dist_nz[THEN sym]

  3796         apply (auto simp add: dist_commute) apply(erule_tac x=xa in ballE) apply auto using e>0 by auto

  3797     }

  3798     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)  }

  3799   thus ?rhs by auto

  3800 next

  3801   assume ?rhs thus ?lhs unfolding continuous_within Lim_within ball_def subset_eq

  3802     apply (auto simp add: dist_commute) apply(erule_tac x=e in allE) by auto

  3803 qed

  3804

  3805 lemma continuous_at_ball:

  3806   "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0. f  (ball x d) \<subseteq> ball (f x) e)" (is "?lhs = ?rhs")

  3807 proof

  3808   assume ?lhs thus ?rhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball

  3809     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)

  3810     unfolding dist_nz[THEN sym] by auto

  3811 next

  3812   assume ?rhs thus ?lhs unfolding continuous_at Lim_at subset_eq Ball_def Bex_def image_iff mem_ball

  3813     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)

  3814 qed

  3815

  3816 text{* Define setwise continuity in terms of limits within the set. *}

  3817

  3818 definition

  3819   continuous_on ::

  3820     "'a set \<Rightarrow> ('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"

  3821 where

  3822   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f ---> f x) (at x within s))"

  3823

  3824 lemma continuous_on_topological:

  3825   "continuous_on s f \<longleftrightarrow>

  3826     (\<forall>x\<in>s. \<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>

  3827       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"

  3828 unfolding continuous_on_def tendsto_def

  3829 unfolding Limits.eventually_within eventually_at_topological

  3830 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto

  3831

  3832 lemma continuous_on_iff:

  3833   "continuous_on s f \<longleftrightarrow>

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

  3835 unfolding continuous_on_def Lim_within

  3836 apply (intro ball_cong [OF refl] all_cong ex_cong)

  3837 apply (rename_tac y, case_tac "y = x", simp)

  3838 apply (simp add: dist_nz)

  3839 done

  3840

  3841 definition

  3842   uniformly_continuous_on ::

  3843     "'a set \<Rightarrow> ('a::metric_space \<Rightarrow> 'b::metric_space) \<Rightarrow> bool"

  3844 where

  3845   "uniformly_continuous_on s f \<longleftrightarrow>

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

  3847

  3848 text{* Some simple consequential lemmas. *}

  3849

  3850 lemma uniformly_continuous_imp_continuous:

  3851  " uniformly_continuous_on s f ==> continuous_on s f"

  3852   unfolding uniformly_continuous_on_def continuous_on_iff by blast

  3853

  3854 lemma continuous_at_imp_continuous_within:

  3855  "continuous (at x) f ==> continuous (at x within s) f"

  3856   unfolding continuous_within continuous_at using Lim_at_within by auto

  3857

  3858 lemma Lim_trivial_limit: "trivial_limit net \<Longrightarrow> (f ---> l) net"

  3859 unfolding tendsto_def by (simp add: trivial_limit_eq)

  3860

  3861 lemma continuous_at_imp_continuous_on:

  3862   assumes "\<forall>x\<in>s. continuous (at x) f"

  3863   shows "continuous_on s f"

  3864 unfolding continuous_on_def

  3865 proof

  3866   fix x assume "x \<in> s"

  3867   with assms have *: "(f ---> f (netlimit (at x))) (at x)"

  3868     unfolding continuous_def by simp

  3869   have "(f ---> f x) (at x)"

  3870   proof (cases "trivial_limit (at x)")

  3871     case True thus ?thesis

  3872       by (rule Lim_trivial_limit)

  3873   next

  3874     case False

  3875     hence 1: "netlimit (at x) = x"

  3876       using netlimit_within [of x UNIV] by simp

  3877     with * show ?thesis by simp

  3878   qed

  3879   thus "(f ---> f x) (at x within s)"

  3880     by (rule Lim_at_within)

  3881 qed

  3882

  3883 lemma continuous_on_eq_continuous_within:

  3884   "continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x within s) f)"

  3885 unfolding continuous_on_def continuous_def

  3886 apply (rule ball_cong [OF refl])

  3887 apply (case_tac "trivial_limit (at x within s)")

  3888 apply (simp add: Lim_trivial_limit)

  3889 apply (simp add: netlimit_within)

  3890 done

  3891

  3892 lemmas continuous_on = continuous_on_def -- "legacy theorem name"

  3893

  3894 lemma continuous_on_eq_continuous_at:

  3895   shows "open s ==> (continuous_on s f \<longleftrightarrow> (\<forall>x \<in> s. continuous (at x) f))"

  3896   by (auto simp add: continuous_on continuous_at Lim_within_open)

  3897

  3898 lemma continuous_within_subset:

  3899  "continuous (at x within s) f \<Longrightarrow> t \<subseteq> s

  3900              ==> continuous (at x within t) f"

  3901   unfolding continuous_within by(metis Lim_within_subset)

  3902

  3903 lemma continuous_on_subset:

  3904   shows "continuous_on s f \<Longrightarrow> t \<subseteq> s ==> continuous_on t f"

  3905   unfolding continuous_on by (metis subset_eq Lim_within_subset)

  3906

  3907 lemma continuous_on_interior:

  3908   shows "continuous_on s f \<Longrightarrow> x \<in> interior s \<Longrightarrow> continuous (at x) f"

  3909   by (erule interiorE, drule (1) continuous_on_subset,

  3910     simp add: continuous_on_eq_continuous_at)

  3911

  3912 lemma continuous_on_eq:

  3913   "(\<forall>x \<in> s. f x = g x) \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on s g"

  3914   unfolding continuous_on_def tendsto_def Limits.eventually_within

  3915   by simp

  3916

  3917 text {* Characterization of various kinds of continuity in terms of sequences. *}

  3918

  3919 lemma continuous_within_sequentially:

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

  3921   shows "continuous (at a within s) f \<longleftrightarrow>

  3922                 (\<forall>x. (\<forall>n::nat. x n \<in> s) \<and> (x ---> a) sequentially

  3923                      --> ((f o x) ---> f a) sequentially)" (is "?lhs = ?rhs")

  3924 proof

  3925   assume ?lhs

  3926   { 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"

  3927     fix T::"'b set" assume "open T" and "f a \<in> T"

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

  3929       unfolding continuous_within tendsto_def eventually_within by auto

  3930     have "eventually (\<lambda>n. dist (x n) a < d) sequentially"

  3931       using x(2) d>0 by simp

  3932     hence "eventually (\<lambda>n. (f \<circ> x) n \<in> T) sequentially"

  3933     proof eventually_elim

  3934       case (elim n) thus ?case

  3935         using d x(1) f a \<in> T unfolding dist_nz[THEN sym] by auto

  3936     qed

  3937   }

  3938   thus ?rhs unfolding tendsto_iff unfolding tendsto_def by simp

  3939 next

  3940   assume ?rhs thus ?lhs

  3941     unfolding continuous_within tendsto_def [where l="f a"]

  3942     by (simp add: sequentially_imp_eventually_within)

  3943 qed

  3944

  3945 lemma continuous_at_sequentially:

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

  3947   shows "continuous (at a) f \<longleftrightarrow> (\<forall>x. (x ---> a) sequentially

  3948                   --> ((f o x) ---> f a) sequentially)"

  3949   using continuous_within_sequentially[of a UNIV f] by simp

  3950

  3951 lemma continuous_on_sequentially:

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

  3953   shows "continuous_on s f \<longleftrightarrow>

  3954     (\<forall>x. \<forall>a \<in> s. (\<forall>n. x(n) \<in> s) \<and> (x ---> a) sequentially

  3955                     --> ((f o x) ---> f(a)) sequentially)" (is "?lhs = ?rhs")

  3956 proof

  3957   assume ?rhs thus ?lhs using continuous_within_sequentially[of _ s f] unfolding continuous_on_eq_continuous_within by auto

  3958 next

  3959   assume ?lhs thus ?rhs unfolding continuous_on_eq_continuous_within using continuous_within_sequentially[of _ s f] by auto

  3960 qed

  3961

  3962 lemma uniformly_continuous_on_sequentially:

  3963   "uniformly_continuous_on s f \<longleftrightarrow> (\<forall>x y. (\<forall>n. x n \<in> s) \<and> (\<forall>n. y n \<in> s) \<and>

  3964                     ((\<lambda>n. dist (x n) (y n)) ---> 0) sequentially

  3965                     \<longrightarrow> ((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially)" (is "?lhs = ?rhs")

  3966 proof

  3967   assume ?lhs

  3968   { 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"

  3969     { fix e::real assume "e>0"

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

  3971         using ?lhs[unfolded uniformly_continuous_on_def, THEN spec[where x=e]] by auto

  3972       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

  3973       { fix n assume "n\<ge>N"

  3974         hence "dist (f (x n)) (f (y n)) < e"

  3975           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

  3976           unfolding dist_commute by simp  }

  3977       hence "\<exists>N. \<forall>n\<ge>N. dist (f (x n)) (f (y n)) < e"  by auto  }

  3978     hence "((\<lambda>n. dist (f(x n)) (f(y n))) ---> 0) sequentially" unfolding LIMSEQ_def and dist_real_def by auto  }

  3979   thus ?rhs by auto

  3980 next

  3981   assume ?rhs

  3982   { assume "\<not> ?lhs"

  3983     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

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

  3985       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

  3986       by (auto simp add: dist_commute)

  3987     def x \<equiv> "\<lambda>n::nat. fst (fa (inverse (real n + 1)))"

  3988     def y \<equiv> "\<lambda>n::nat. snd (fa (inverse (real n + 1)))"

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

  3990       unfolding x_def and y_def using fa by auto

  3991     { fix e::real assume "e>0"

  3992       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

  3993       { fix n::nat assume "n\<ge>N"

  3994         hence "inverse (real n + 1) < inverse (real N)" using real_of_nat_ge_zero and N\<noteq>0 by auto

  3995         also have "\<dots> < e" using N by auto

  3996         finally have "inverse (real n + 1) < e" by auto

  3997         hence "dist (x n) (y n) < e" using xy0[THEN spec[where x=n]] by auto  }

  3998       hence "\<exists>N. \<forall>n\<ge>N. dist (x n) (y n) < e" by auto  }

  3999     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

  4000     hence False using fxy and e>0 by auto  }

  4001   thus ?lhs unfolding uniformly_continuous_on_def by blast

  4002 qed

  4003

  4004 text{* The usual transformation theorems. *}

  4005

  4006 lemma continuous_transform_within:

  4007   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  4008   assumes "0 < d" "x \<in> s" "\<forall>x' \<in> s. dist x' x < d --> f x' = g x'"

  4009           "continuous (at x within s) f"

  4010   shows "continuous (at x within s) g"

  4011 unfolding continuous_within

  4012 proof (rule Lim_transform_within)

  4013   show "0 < d" by fact

  4014   show "\<forall>x'\<in>s. 0 < dist x' x \<and> dist x' x < d \<longrightarrow> f x' = g x'"

  4015     using assms(3) by auto

  4016   have "f x = g x"

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

  4018   thus "(f ---> g x) (at x within s)"

  4019     using assms(4) unfolding continuous_within by simp

  4020 qed

  4021

  4022 lemma continuous_transform_at:

  4023   fixes f g :: "'a::metric_space \<Rightarrow> 'b::topological_space"

  4024   assumes "0 < d" "\<forall>x'. dist x' x < d --> f x' = g x'"

  4025           "continuous (at x) f"

  4026   shows "continuous (at x) g"

  4027   using continuous_transform_within [of d x UNIV f g] assms by simp

  4028

  4029 subsubsection {* Structural rules for pointwise continuity *}

  4030

  4031 lemma continuous_within_id: "continuous (at a within s) (\<lambda>x. x)"

  4032   unfolding continuous_within by (rule tendsto_ident_at_within)

  4033

  4034 lemma continuous_at_id: "continuous (at a) (\<lambda>x. x)"

  4035   unfolding continuous_at by (rule tendsto_ident_at)

  4036

  4037 lemma continuous_const: "continuous F (\<lambda>x. c)"

  4038   unfolding continuous_def by (rule tendsto_const)

  4039

  4040 lemma continuous_fst: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. fst (f x))"

  4041   unfolding continuous_def by (rule tendsto_fst)

  4042

  4043 lemma continuous_snd: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. snd (f x))"

  4044   unfolding continuous_def by (rule tendsto_snd)

  4045

  4046 lemma continuous_Pair: "continuous F f \<Longrightarrow> continuous F g \<Longrightarrow> continuous F (\<lambda>x. (f x, g x))"

  4047   unfolding continuous_def by (rule tendsto_Pair)

  4048

  4049 lemma continuous_dist:

  4050   assumes "continuous F f" and "continuous F g"

  4051   shows "continuous F (\<lambda>x. dist (f x) (g x))"

  4052   using assms unfolding continuous_def by (rule tendsto_dist)

  4053

  4054 lemma continuous_infdist:

  4055   assumes "continuous F f"

  4056   shows "continuous F (\<lambda>x. infdist (f x) A)"

  4057   using assms unfolding continuous_def by (rule tendsto_infdist)

  4058

  4059 lemma continuous_norm:

  4060   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. norm (f x))"

  4061   unfolding continuous_def by (rule tendsto_norm)

  4062

  4063 lemma continuous_infnorm:

  4064   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. infnorm (f x))"

  4065   unfolding continuous_def by (rule tendsto_infnorm)

  4066

  4067 lemma continuous_add:

  4068   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4069   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x + g x)"

  4070   unfolding continuous_def by (rule tendsto_add)

  4071

  4072 lemma continuous_minus:

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

  4074   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. - f x)"

  4075   unfolding continuous_def by (rule tendsto_minus)

  4076

  4077 lemma continuous_diff:

  4078   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4079   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x - g x)"

  4080   unfolding continuous_def by (rule tendsto_diff)

  4081

  4082 lemma continuous_scaleR:

  4083   fixes g :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"

  4084   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x *\<^sub>R g x)"

  4085   unfolding continuous_def by (rule tendsto_scaleR)

  4086

  4087 lemma continuous_mult:

  4088   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_algebra"

  4089   shows "\<lbrakk>continuous F f; continuous F g\<rbrakk> \<Longrightarrow> continuous F (\<lambda>x. f x * g x)"

  4090   unfolding continuous_def by (rule tendsto_mult)

  4091

  4092 lemma continuous_inner:

  4093   assumes "continuous F f" and "continuous F g"

  4094   shows "continuous F (\<lambda>x. inner (f x) (g x))"

  4095   using assms unfolding continuous_def by (rule tendsto_inner)

  4096

  4097 lemma continuous_inverse:

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

  4099   assumes "continuous F f" and "f (netlimit F) \<noteq> 0"

  4100   shows "continuous F (\<lambda>x. inverse (f x))"

  4101   using assms unfolding continuous_def by (rule tendsto_inverse)

  4102

  4103 lemma continuous_at_within_inverse:

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

  4105   assumes "continuous (at a within s) f" and "f a \<noteq> 0"

  4106   shows "continuous (at a within s) (\<lambda>x. inverse (f x))"

  4107   using assms unfolding continuous_within by (rule tendsto_inverse)

  4108

  4109 lemma continuous_at_inverse:

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

  4111   assumes "continuous (at a) f" and "f a \<noteq> 0"

  4112   shows "continuous (at a) (\<lambda>x. inverse (f x))"

  4113   using assms unfolding continuous_at by (rule tendsto_inverse)

  4114

  4115 lemmas continuous_intros = continuous_at_id continuous_within_id

  4116   continuous_const continuous_dist continuous_norm continuous_infnorm

  4117   continuous_add continuous_minus continuous_diff continuous_scaleR continuous_mult

  4118   continuous_inner continuous_at_inverse continuous_at_within_inverse

  4119

  4120 subsubsection {* Structural rules for setwise continuity *}

  4121

  4122 lemma continuous_on_id: "continuous_on s (\<lambda>x. x)"

  4123   unfolding continuous_on_def by (fast intro: tendsto_ident_at_within)

  4124

  4125 lemma continuous_on_const: "continuous_on s (\<lambda>x. c)"

  4126   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4127

  4128 lemma continuous_on_norm:

  4129   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. norm (f x))"

  4130   unfolding continuous_on_def by (fast intro: tendsto_norm)

  4131

  4132 lemma continuous_on_infnorm:

  4133   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. infnorm (f x))"

  4134   unfolding continuous_on by (fast intro: tendsto_infnorm)

  4135

  4136 lemma continuous_on_minus:

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

  4138   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. - f x)"

  4139   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4140

  4141 lemma continuous_on_add:

  4142   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4143   shows "continuous_on s f \<Longrightarrow> continuous_on s g

  4144            \<Longrightarrow> continuous_on s (\<lambda>x. f x + g x)"

  4145   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4146

  4147 lemma continuous_on_diff:

  4148   fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4149   shows "continuous_on s f \<Longrightarrow> continuous_on s g

  4150            \<Longrightarrow> continuous_on s (\<lambda>x. f x - g x)"

  4151   unfolding continuous_on_def by (auto intro: tendsto_intros)

  4152

  4153 lemma (in bounded_linear) continuous_on:

  4154   "continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. f (g x))"

  4155   unfolding continuous_on_def by (fast intro: tendsto)

  4156

  4157 lemma (in bounded_bilinear) continuous_on:

  4158   "\<lbrakk>continuous_on s f; continuous_on s g\<rbrakk> \<Longrightarrow> continuous_on s (\<lambda>x. f x ** g x)"

  4159   unfolding continuous_on_def by (fast intro: tendsto)

  4160

  4161 lemma continuous_on_scaleR:

  4162   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"

  4163   assumes "continuous_on s f" and "continuous_on s g"

  4164   shows "continuous_on s (\<lambda>x. f x *\<^sub>R g x)"

  4165   using bounded_bilinear_scaleR assms

  4166   by (rule bounded_bilinear.continuous_on)

  4167

  4168 lemma continuous_on_mult:

  4169   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_normed_algebra"

  4170   assumes "continuous_on s f" and "continuous_on s g"

  4171   shows "continuous_on s (\<lambda>x. f x * g x)"

  4172   using bounded_bilinear_mult assms

  4173   by (rule bounded_bilinear.continuous_on)

  4174

  4175 lemma continuous_on_inner:

  4176   fixes g :: "'a::topological_space \<Rightarrow> 'b::real_inner"

  4177   assumes "continuous_on s f" and "continuous_on s g"

  4178   shows "continuous_on s (\<lambda>x. inner (f x) (g x))"

  4179   using bounded_bilinear_inner assms

  4180   by (rule bounded_bilinear.continuous_on)

  4181

  4182 lemma continuous_on_inverse:

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

  4184   assumes "continuous_on s f" and "\<forall>x\<in>s. f x \<noteq> 0"

  4185   shows "continuous_on s (\<lambda>x. inverse (f x))"

  4186   using assms unfolding continuous_on by (fast intro: tendsto_inverse)

  4187

  4188 subsubsection {* Structural rules for uniform continuity *}

  4189

  4190 lemma uniformly_continuous_on_id:

  4191   shows "uniformly_continuous_on s (\<lambda>x. x)"

  4192   unfolding uniformly_continuous_on_def by auto

  4193

  4194 lemma uniformly_continuous_on_const:

  4195   shows "uniformly_continuous_on s (\<lambda>x. c)"

  4196   unfolding uniformly_continuous_on_def by simp

  4197

  4198 lemma uniformly_continuous_on_dist:

  4199   fixes f g :: "'a::metric_space \<Rightarrow> 'b::metric_space"

  4200   assumes "uniformly_continuous_on s f"

  4201   assumes "uniformly_continuous_on s g"

  4202   shows "uniformly_continuous_on s (\<lambda>x. dist (f x) (g x))"

  4203 proof -

  4204   { fix a b c d :: 'b have "\<bar>dist a b - dist c d\<bar> \<le> dist a c + dist b d"

  4205       using dist_triangle2 [of a b c] dist_triangle2 [of b c d]

  4206       using dist_triangle3 [of c d a] dist_triangle [of a d b]

  4207       by arith

  4208   } note le = this

  4209   { fix x y

  4210     assume f: "(\<lambda>n. dist (f (x n)) (f (y n))) ----> 0"

  4211     assume g: "(\<lambda>n. dist (g (x n)) (g (y n))) ----> 0"

  4212     have "(\<lambda>n. \<bar>dist (f (x n)) (g (x n)) - dist (f (y n)) (g (y n))\<bar>) ----> 0"

  4213       by (rule Lim_transform_bound [OF _ tendsto_add_zero [OF f g]],

  4214         simp add: le)

  4215   }

  4216   thus ?thesis using assms unfolding uniformly_continuous_on_sequentially

  4217     unfolding dist_real_def by simp

  4218 qed

  4219

  4220 lemma uniformly_continuous_on_norm:

  4221   assumes "uniformly_continuous_on s f"

  4222   shows "uniformly_continuous_on s (\<lambda>x. norm (f x))"

  4223   unfolding norm_conv_dist using assms

  4224   by (intro uniformly_continuous_on_dist uniformly_continuous_on_const)

  4225

  4226 lemma (in bounded_linear) uniformly_continuous_on:

  4227   assumes "uniformly_continuous_on s g"

  4228   shows "uniformly_continuous_on s (\<lambda>x. f (g x))"

  4229   using assms unfolding uniformly_continuous_on_sequentially

  4230   unfolding dist_norm tendsto_norm_zero_iff diff[symmetric]

  4231   by (auto intro: tendsto_zero)

  4232

  4233 lemma uniformly_continuous_on_cmul:

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

  4235   assumes "uniformly_continuous_on s f"

  4236   shows "uniformly_continuous_on s (\<lambda>x. c *\<^sub>R f(x))"

  4237   using bounded_linear_scaleR_right assms

  4238   by (rule bounded_linear.uniformly_continuous_on)

  4239

  4240 lemma dist_minus:

  4241   fixes x y :: "'a::real_normed_vector"

  4242   shows "dist (- x) (- y) = dist x y"

  4243   unfolding dist_norm minus_diff_minus norm_minus_cancel ..

  4244

  4245 lemma uniformly_continuous_on_minus:

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

  4247   shows "uniformly_continuous_on s f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. - f x)"

  4248   unfolding uniformly_continuous_on_def dist_minus .

  4249

  4250 lemma uniformly_continuous_on_add:

  4251   fixes f g :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"

  4252   assumes "uniformly_continuous_on s f"

  4253   assumes "uniformly_continuous_on s g"

  4254   shows "uniformly_continuous_on s (\<lambda>x. f x + g x)"

  4255   using assms unfolding uniformly_continuous_on_sequentially

  4256   unfolding dist_norm tendsto_norm_zero_iff add_diff_add

  4257   by (auto intro: tendsto_add_zero)

  4258

  4259 lemma uniformly_continuous_on_diff:

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

  4261   assumes "uniformly_continuous_on s f" and "uniformly_continuous_on s g"

  4262   shows "uniformly_continuous_on s (\<lambda>x. f x - g x)"

  4263   unfolding ab_diff_minus using assms

  4264   by (intro uniformly_continuous_on_add uniformly_continuous_on_minus)

  4265

  4266 text{* Continuity of all kinds is preserved under composition. *}

  4267

  4268 lemma continuous_within_topological:

  4269   "continuous (at x within s) f \<longleftrightarrow>

  4270     (\<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow>

  4271       (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"

  4272 unfolding continuous_within

  4273 unfolding tendsto_def Limits.eventually_within eventually_at_topological

  4274 by (intro ball_cong [OF refl] all_cong imp_cong ex_cong conj_cong refl) auto

  4275

  4276 lemma continuous_within_compose:

  4277   assumes "continuous (at x within s) f"

  4278   assumes "continuous (at (f x) within f  s) g"

  4279   shows "continuous (at x within s) (g o f)"

  4280 using assms unfolding continuous_within_topological by simp metis

  4281

  4282 lemma continuous_at_compose:

  4283   assumes "continuous (at x) f" and "continuous (at (f x)) g"

  4284   shows "continuous (at x) (g o f)"

  4285 proof-

  4286   have "continuous (at (f x) within range f) g" using assms(2)

  4287     using continuous_within_subset[of "f x" UNIV g "range f"] by simp

  4288   thus ?thesis using assms(1)

  4289     using continuous_within_compose[of x UNIV f g] by simp

  4290 qed

  4291

  4292 lemma continuous_on_compose:

  4293   "continuous_on s f \<Longrightarrow> continuous_on (f  s) g \<Longrightarrow> continuous_on s (g o f)"

  4294   unfolding continuous_on_topological by simp metis

  4295

  4296 lemma uniformly_continuous_on_compose:

  4297   assumes "uniformly_continuous_on s f"  "uniformly_continuous_on (f  s) g"

  4298   shows "uniformly_continuous_on s (g o f)"

  4299 proof-

  4300   { fix e::real assume "e>0"

  4301     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

  4302     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

  4303     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  }

  4304   thus ?thesis using assms unfolding uniformly_continuous_on_def by auto

  4305 qed

  4306

  4307 lemmas continuous_on_intros = continuous_on_id continuous_on_const

  4308   continuous_on_compose continuous_on_norm continuous_on_infnorm

  4309   continuous_on_add continuous_on_minus continuous_on_diff

  4310   continuous_on_scaleR continuous_on_mult continuous_on_inverse

  4311   continuous_on_inner

  4312   uniformly_continuous_on_id uniformly_continuous_on_const

  4313   uniformly_continuous_on_dist uniformly_continuous_on_norm

  4314   uniformly_continuous_on_compose uniformly_continuous_on_add

  4315   uniformly_continuous_on_minus uniformly_continuous_on_diff

  4316   uniformly_continuous_on_cmul

  4317

  4318 text{* Continuity in terms of open preimages. *}

  4319

  4320 lemma continuous_at_open:

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

  4322 unfolding continuous_within_topological [of x UNIV f, unfolded within_UNIV]

  4323 unfolding imp_conjL by (intro all_cong imp_cong ex_cong conj_cong refl) auto

  4324

  4325 lemma continuous_on_open:

  4326   shows "continuous_on s f \<longleftrightarrow>

  4327         (\<forall>t. openin (subtopology euclidean (f  s)) t

  4328             --> openin (subtopology euclidean s) {x \<in> s. f x \<in> t})" (is "?lhs = ?rhs")

  4329 proof (safe)

  4330   fix t :: "'b set"

  4331   assume 1: "continuous_on s f"

  4332   assume 2: "openin (subtopology euclidean (f  s)) t"

  4333   from 2 obtain B where B: "open B" and t: "t = f  s \<inter> B"

  4334     unfolding openin_open by auto

  4335   def U == "\<Union>{A. open A \<and> (\<forall>x\<in>s. x \<in> A \<longrightarrow> f x \<in> B)}"

  4336   have "open U" unfolding U_def by (simp add: open_Union)

  4337   moreover have "\<forall>x\<in>s. x \<in> U \<longleftrightarrow> f x \<in> t"

  4338   proof (intro ballI iffI)

  4339     fix x assume "x \<in> s" and "x \<in> U" thus "f x \<in> t"

  4340       unfolding U_def t by auto

  4341   next

  4342     fix x assume "x \<in> s" and "f x \<in> t"

  4343     hence "x \<in> s" and "f x \<in> B"

  4344       unfolding t by auto

  4345     with 1 B obtain A where "open A" "x \<in> A" "\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B"

  4346       unfolding t continuous_on_topological by metis

  4347     then show "x \<in> U"

  4348       unfolding U_def by auto

  4349   qed

  4350   ultimately have "open U \<and> {x \<in> s. f x \<in> t} = s \<inter> U" by auto

  4351   then show "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"

  4352     unfolding openin_open by fast

  4353 next

  4354   assume "?rhs" show "continuous_on s f"

  4355   unfolding continuous_on_topological

  4356   proof (clarify)

  4357     fix x and B assume "x \<in> s" and "open B" and "f x \<in> B"

  4358     have "openin (subtopology euclidean (f  s)) (f  s \<inter> B)"

  4359       unfolding openin_open using open B by auto

  4360     then have "openin (subtopology euclidean s) {x \<in> s. f x \<in> f  s \<inter> B}"

  4361       using ?rhs by fast

  4362     then show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"

  4363       unfolding openin_open using x \<in> s and f x \<in> B by auto

  4364   qed

  4365 qed

  4366

  4367 text {* Similarly in terms of closed sets. *}

  4368

  4369 lemma continuous_on_closed:

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

  4371 proof

  4372   assume ?lhs

  4373   { fix t

  4374     have *:"s - {x \<in> s. f x \<in> f  s - t} = {x \<in> s. f x \<in> t}" by auto

  4375     have **:"f  s - (f  s - (f  s - t)) = f  s - t" by auto

  4376     assume as:"closedin (subtopology euclidean (f  s)) t"

  4377     hence "closedin (subtopology euclidean (f  s)) (f  s - (f  s - t))" unfolding closedin_def topspace_euclidean_subtopology unfolding ** by auto

  4378     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"]]

  4379       unfolding openin_closedin_eq topspace_euclidean_subtopology unfolding * by auto  }

  4380   thus ?rhs by auto

  4381 next

  4382   assume ?rhs

  4383   { fix t

  4384     have *:"s - {x \<in> s. f x \<in> f  s - t} = {x \<in> s. f x \<in> t}" by auto

  4385     assume as:"openin (subtopology euclidean (f  s)) t"

  4386     hence "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}" using ?rhs[THEN spec[where x="(f  s) - t"]]

  4387       unfolding openin_closedin_eq topspace_euclidean_subtopology *[THEN sym] closedin_subtopology by auto }

  4388   thus ?lhs unfolding continuous_on_open by auto

  4389 qed

  4390

  4391 text {* Half-global and completely global cases. *}

  4392

  4393 lemma continuous_open_in_preimage:

  4394   assumes "continuous_on s f"  "open t"

  4395   shows "openin (subtopology euclidean s) {x \<in> s. f x \<in> t}"

  4396 proof-

  4397   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

  4398   have "openin (subtopology euclidean (f  s)) (t \<inter> f  s)"

  4399     using openin_open_Int[of t "f  s", OF assms(2)] unfolding openin_open by auto

  4400   thus ?thesis using assms(1)[unfolded continuous_on_open, THEN spec[where x="t \<inter> f  s"]] using * by auto

  4401 qed

  4402

  4403 lemma continuous_closed_in_preimage:

  4404   assumes "continuous_on s f"  "closed t"

  4405   shows "closedin (subtopology euclidean s) {x \<in> s. f x \<in> t}"

  4406 proof-

  4407   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

  4408   have "closedin (subtopology euclidean (f  s)) (t \<inter> f  s)"

  4409     using closedin_closed_Int[of t "f  s", OF assms(2)] unfolding Int_commute by auto

  4410   thus ?thesis

  4411     using assms(1)[unfolded continuous_on_closed, THEN spec[where x="t \<inter> f  s"]] using * by auto

  4412 qed

  4413

  4414 lemma continuous_open_preimage:

  4415   assumes "continuous_on s f" "open s" "open t"

  4416   shows "open {x \<in> s. f x \<in> t}"

  4417 proof-

  4418   obtain T where T: "open T" "{x \<in> s. f x \<in> t} = s \<inter> T"

  4419     using continuous_open_in_preimage[OF assms(1,3)] unfolding openin_open by auto

  4420   thus ?thesis using open_Int[of s T, OF assms(2)] by auto

  4421 qed

  4422

  4423 lemma continuous_closed_preimage:

  4424   assumes "continuous_on s f" "closed s" "closed t"

  4425   shows "closed {x \<in> s. f x \<in> t}"

  4426 proof-

  4427   obtain T where T: "closed T" "{x \<in> s. f x \<in> t} = s \<inter> T"

  4428     using continuous_closed_in_preimage[OF assms(1,3)] unfolding closedin_closed by auto

  4429   thus ?thesis using closed_Int[of s T, OF assms(2)] by auto

  4430 qed

  4431

  4432 lemma continuous_open_preimage_univ:

  4433   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open {x. f x \<in> s}"

  4434   using continuous_open_preimage[of UNIV f s] open_UNIV continuous_at_imp_continuous_on by auto

  4435

  4436 lemma continuous_closed_preimage_univ:

  4437   shows "(\<forall>x. continuous (at x) f) \<Longrightarrow> closed s ==> closed {x. f x \<in> s}"

  4438   using continuous_closed_preimage[of UNIV f s] closed_UNIV continuous_at_imp_continuous_on by auto

  4439

  4440 lemma continuous_open_vimage:

  4441   shows "\<forall>x. continuous (at x) f \<Longrightarrow> open s \<Longrightarrow> open (f - s)"

  4442   unfolding vimage_def by (rule continuous_open_preimage_univ)

  4443

  4444 lemma continuous_closed_vimage:

  4445   shows "\<forall>x. continuous (at x) f \<Longrightarrow> closed s \<Longrightarrow> closed (f - s)"

  4446   unfolding vimage_def by (rule continuous_closed_preimage_univ)

  4447

  4448 lemma interior_image_subset:

  4449   assumes "\<forall>x. continuous (at x) f" "inj f"

  4450   shows "interior (f  s) \<subseteq> f  (interior s)"

  4451 proof

  4452   fix x assume "x \<in> interior (f  s)"

  4453   then obtain T where as: "open T" "x \<in> T" "T \<subseteq> f  s" ..

  4454   hence "x \<in> f  s" by auto

  4455   then obtain y where y: "y \<in> s" "x = f y" by auto

  4456   have "open (vimage f T)"

  4457     using assms(1) open T by (rule continuous_open_vimage)

  4458   moreover have "y \<in> vimage f T"

  4459     using x = f y x \<in> T by simp

  4460   moreover have "vimage f T \<subseteq> s"

  4461     using T \<subseteq> image f s inj f unfolding inj_on_def subset_eq by auto

  4462   ultimately have "y \<in> interior s" ..

  4463   with x = f y show "x \<in> f  interior s" ..

  4464 qed

  4465

  4466 text {* Equality of continuous functions on closure and related results. *}

  4467

  4468 lemma continuous_closed_in_preimage_constant:

  4469   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4470   shows "continuous_on s f ==> closedin (subtopology euclidean s) {x \<in> s. f x = a}"

  4471   using continuous_closed_in_preimage[of s f "{a}"] by auto

  4472

  4473 lemma continuous_closed_preimage_constant:

  4474   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4475   shows "continuous_on s f \<Longrightarrow> closed s ==> closed {x \<in> s. f x = a}"

  4476   using continuous_closed_preimage[of s f "{a}"] by auto

  4477

  4478 lemma continuous_constant_on_closure:

  4479   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4480   assumes "continuous_on (closure s) f"

  4481           "\<forall>x \<in> s. f x = a"

  4482   shows "\<forall>x \<in> (closure s). f x = a"

  4483     using continuous_closed_preimage_constant[of "closure s" f a]

  4484     assms closure_minimal[of s "{x \<in> closure s. f x = a}"] closure_subset unfolding subset_eq by auto

  4485

  4486 lemma image_closure_subset:

  4487   assumes "continuous_on (closure s) f"  "closed t"  "(f  s) \<subseteq> t"

  4488   shows "f  (closure s) \<subseteq> t"

  4489 proof-

  4490   have "s \<subseteq> {x \<in> closure s. f x \<in> t}" using assms(3) closure_subset by auto

  4491   moreover have "closed {x \<in> closure s. f x \<in> t}"

  4492     using continuous_closed_preimage[OF assms(1)] and assms(2) by auto

  4493   ultimately have "closure s = {x \<in> closure s . f x \<in> t}"

  4494     using closure_minimal[of s "{x \<in> closure s. f x \<in> t}"] by auto

  4495   thus ?thesis by auto

  4496 qed

  4497

  4498 lemma continuous_on_closure_norm_le:

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

  4500   assumes "continuous_on (closure s) f"  "\<forall>y \<in> s. norm(f y) \<le> b"  "x \<in> (closure s)"

  4501   shows "norm(f x) \<le> b"

  4502 proof-

  4503   have *:"f  s \<subseteq> cball 0 b" using assms(2)[unfolded mem_cball_0[THEN sym]] by auto

  4504   show ?thesis

  4505     using image_closure_subset[OF assms(1) closed_cball[of 0 b] *] assms(3)

  4506     unfolding subset_eq apply(erule_tac x="f x" in ballE) by (auto simp add: dist_norm)

  4507 qed

  4508

  4509 text {* Making a continuous function avoid some value in a neighbourhood. *}

  4510

  4511 lemma continuous_within_avoid:

  4512   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4513   assumes "continuous (at x within s) f" and "f x \<noteq> a"

  4514   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e --> f y \<noteq> a"

  4515 proof-

  4516   obtain U where "open U" and "f x \<in> U" and "a \<notin> U"

  4517     using t1_space [OF f x \<noteq> a] by fast

  4518   have "(f ---> f x) (at x within s)"

  4519     using assms(1) by (simp add: continuous_within)

  4520   hence "eventually (\<lambda>y. f y \<in> U) (at x within s)"

  4521     using open U and f x \<in> U

  4522     unfolding tendsto_def by fast

  4523   hence "eventually (\<lambda>y. f y \<noteq> a) (at x within s)"

  4524     using a \<notin> U by (fast elim: eventually_mono [rotated])

  4525   thus ?thesis

  4526     unfolding Limits.eventually_within Limits.eventually_at

  4527     by (rule ex_forward, cut_tac f x \<noteq> a, auto simp: dist_commute)

  4528 qed

  4529

  4530 lemma continuous_at_avoid:

  4531   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4532   assumes "continuous (at x) f" and "f x \<noteq> a"

  4533   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"

  4534   using assms continuous_within_avoid[of x UNIV f a] by simp

  4535

  4536 lemma continuous_on_avoid:

  4537   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4538   assumes "continuous_on s f"  "x \<in> s"  "f x \<noteq> a"

  4539   shows "\<exists>e>0. \<forall>y \<in> s. dist x y < e \<longrightarrow> f y \<noteq> a"

  4540 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

  4541

  4542 lemma continuous_on_open_avoid:

  4543   fixes f :: "'a::metric_space \<Rightarrow> 'b::t1_space"

  4544   assumes "continuous_on s f"  "open s"  "x \<in> s"  "f x \<noteq> a"

  4545   shows "\<exists>e>0. \<forall>y. dist x y < e \<longrightarrow> f y \<noteq> a"

  4546 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

  4547

  4548 text {* Proving a function is constant by proving open-ness of level set. *}

  4549

  4550 lemma continuous_levelset_open_in_cases:

  4551   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4552   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>

  4553         openin (subtopology euclidean s) {x \<in> s. f x = a}

  4554         ==> (\<forall>x \<in> s. f x \<noteq> a) \<or> (\<forall>x \<in> s. f x = a)"

  4555 unfolding connected_clopen using continuous_closed_in_preimage_constant by auto

  4556

  4557 lemma continuous_levelset_open_in:

  4558   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4559   shows "connected s \<Longrightarrow> continuous_on s f \<Longrightarrow>

  4560         openin (subtopology euclidean s) {x \<in> s. f x = a} \<Longrightarrow>

  4561         (\<exists>x \<in> s. f x = a)  ==> (\<forall>x \<in> s. f x = a)"

  4562 using continuous_levelset_open_in_cases[of s f ]

  4563 by meson

  4564

  4565 lemma continuous_levelset_open:

  4566   fixes f :: "_ \<Rightarrow> 'b::t1_space"

  4567   assumes "connected s"  "continuous_on s f"  "open {x \<in> s. f x = a}"  "\<exists>x \<in> s.  f x = a"

  4568   shows "\<forall>x \<in> s. f x = a"

  4569 using continuous_levelset_open_in[OF assms(1,2), of a, unfolded openin_open] using assms (3,4) by fast

  4570

  4571 text {* Some arithmetical combinations (more to prove). *}

  4572

  4573 lemma open_scaling[intro]:

  4574   fixes s :: "'a::real_normed_vector set"

  4575   assumes "c \<noteq> 0"  "open s"

  4576   shows "open((\<lambda>x. c *\<^sub>R x)  s)"

  4577 proof-

  4578   { fix x assume "x \<in> s"

  4579     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

  4580     have "e * abs c > 0" using assms(1)[unfolded zero_less_abs_iff[THEN sym]] using mult_pos_pos[OF e>0] by auto

  4581     moreover

  4582     { fix y assume "dist y (c *\<^sub>R x) < e * \<bar>c\<bar>"

  4583       hence "norm ((1 / c) *\<^sub>R y - x) < e" unfolding dist_norm

  4584         using norm_scaleR[of c "(1 / c) *\<^sub>R y - x", unfolded scaleR_right_diff_distrib, unfolded scaleR_scaleR] assms(1)

  4585           assms(1)[unfolded zero_less_abs_iff[THEN sym]] by (simp del:zero_less_abs_iff)

  4586       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  }

  4587     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  }

  4588   thus ?thesis unfolding open_dist by auto

  4589 qed

  4590

  4591 lemma minus_image_eq_vimage:

  4592   fixes A :: "'a::ab_group_add set"

  4593   shows "(\<lambda>x. - x)  A = (\<lambda>x. - x) - A"

  4594   by (auto intro!: image_eqI [where f="\<lambda>x. - x"])

  4595

  4596 lemma open_negations:

  4597   fixes s :: "'a::real_normed_vector set"

  4598   shows "open s ==> open ((\<lambda> x. -x)  s)"

  4599   unfolding scaleR_minus1_left [symmetric]

  4600   by (rule open_scaling, auto)

  4601

  4602 lemma open_translation:

  4603   fixes s :: "'a::real_normed_vector set"

  4604   assumes "open s"  shows "open((\<lambda>x. a + x)  s)"

  4605 proof-

  4606   { fix x have "continuous (at x) (\<lambda>x. x - a)"

  4607       by (intro continuous_diff continuous_at_id continuous_const) }

  4608   moreover have "{x. x - a \<in> s} = op + a  s" by force

  4609   ultimately show ?thesis using continuous_open_preimage_univ[of "\<lambda>x. x - a" s] using assms by auto

  4610 qed

  4611

  4612 lemma open_affinity:

  4613   fixes s :: "'a::real_normed_vector set"

  4614   assumes "open s"  "c \<noteq> 0"

  4615   shows "open ((\<lambda>x. a + c *\<^sub>R x)  s)"

  4616 proof-

  4617   have *:"(\<lambda>x. a + c *\<^sub>R x) = (\<lambda>x. a + x) \<circ> (\<lambda>x. c *\<^sub>R x)" unfolding o_def ..

  4618   have "op + a  op *\<^sub>R c  s = (op + a \<circ> op *\<^sub>R c)  s" by auto

  4619   thus ?thesis using assms open_translation[of "op *\<^sub>R c  s" a] unfolding * by auto

  4620 qed

  4621

  4622 lemma interior_translation:

  4623   fixes s :: "'a::real_normed_vector set"

  4624   shows "interior ((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (interior s)"

  4625 proof (rule set_eqI, rule)

  4626   fix x assume "x \<in> interior (op + a  s)"

  4627   then obtain e where "e>0" and e:"ball x e \<subseteq> op + a  s" unfolding mem_interior by auto

  4628   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

  4629   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

  4630 next

  4631   fix x assume "x \<in> op + a  interior s"

  4632   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

  4633   { fix z have *:"a + y - z = y + a - z" by auto

  4634     assume "z\<in>ball x e"

  4635     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

  4636     hence "z \<in> op + a  s" unfolding image_iff by(auto intro!: bexI[where x="z - a"])  }

  4637   hence "ball x e \<subseteq> op + a  s" unfolding subset_eq by auto

  4638   thus "x \<in> interior (op + a  s)" unfolding mem_interior using e>0 by auto

  4639 qed

  4640

  4641 text {* Topological properties of linear functions. *}

  4642

  4643 lemma linear_lim_0:

  4644   assumes "bounded_linear f" shows "(f ---> 0) (at (0))"

  4645 proof-

  4646   interpret f: bounded_linear f by fact

  4647   have "(f ---> f 0) (at 0)"

  4648     using tendsto_ident_at by (rule f.tendsto)

  4649   thus ?thesis unfolding f.zero .

  4650 qed

  4651

  4652 lemma linear_continuous_at:

  4653   assumes "bounded_linear f"  shows "continuous (at a) f"

  4654   unfolding continuous_at using assms

  4655   apply (rule bounded_linear.tendsto)

  4656   apply (rule tendsto_ident_at)

  4657   done

  4658

  4659 lemma linear_continuous_within:

  4660   shows "bounded_linear f ==> continuous (at x within s) f"

  4661   using continuous_at_imp_continuous_within[of x f s] using linear_continuous_at[of f] by auto

  4662

  4663 lemma linear_continuous_on:

  4664   shows "bounded_linear f ==> continuous_on s f"

  4665   using continuous_at_imp_continuous_on[of s f] using linear_continuous_at[of f] by auto

  4666

  4667 text {* Also bilinear functions, in composition form. *}

  4668

  4669 lemma bilinear_continuous_at_compose:

  4670   shows "continuous (at x) f \<Longrightarrow> continuous (at x) g \<Longrightarrow> bounded_bilinear h

  4671         ==> continuous (at x) (\<lambda>x. h (f x) (g x))"

  4672   unfolding continuous_at using Lim_bilinear[of f "f x" "(at x)" g "g x" h] by auto

  4673

  4674 lemma bilinear_continuous_within_compose:

  4675   shows "continuous (at x within s) f \<Longrightarrow> continuous (at x within s) g \<Longrightarrow> bounded_bilinear h

  4676         ==> continuous (at x within s) (\<lambda>x. h (f x) (g x))"

  4677   unfolding continuous_within using Lim_bilinear[of f "f x"] by auto

  4678

  4679 lemma bilinear_continuous_on_compose:

  4680   shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> bounded_bilinear h

  4681              ==> continuous_on s (\<lambda>x. h (f x) (g x))"

  4682   unfolding continuous_on_def

  4683   by (fast elim: bounded_bilinear.tendsto)

  4684

  4685 text {* Preservation of compactness and connectedness under continuous function. *}

  4686

  4687 lemma compact_eq_openin_cover:

  4688   "compact S \<longleftrightarrow>

  4689     (\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>

  4690       (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D))"

  4691 proof safe

  4692   fix C

  4693   assume "compact S" and "\<forall>c\<in>C. openin (subtopology euclidean S) c" and "S \<subseteq> \<Union>C"

  4694   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}"

  4695     unfolding openin_open by force+

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

  4697     by (rule compactE)

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

  4699     by auto

  4700   thus "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..

  4701 next

  4702   assume 1: "\<forall>C. (\<forall>c\<in>C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>C \<longrightarrow>

  4703         (\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D)"

  4704   show "compact S"

  4705   proof (rule compactI)

  4706     fix C

  4707     let ?C = "image (\<lambda>T. S \<inter> T) C"

  4708     assume "\<forall>t\<in>C. open t" and "S \<subseteq> \<Union>C"

  4709     hence "(\<forall>c\<in>?C. openin (subtopology euclidean S) c) \<and> S \<subseteq> \<Union>?C"

  4710       unfolding openin_open by auto

  4711     with 1 obtain D where "D \<subseteq> ?C" and "finite D" and "S \<subseteq> \<Union>D"

  4712       by metis

  4713     let ?D = "inv_into C (\<lambda>T. S \<inter> T)  D"

  4714     have "?D \<subseteq> C \<and> finite ?D \<and> S \<subseteq> \<Union>?D"

  4715     proof (intro conjI)

  4716       from D \<subseteq> ?C show "?D \<subseteq> C"

  4717         by (fast intro: inv_into_into)

  4718       from finite D show "finite ?D"

  4719         by (rule finite_imageI)

  4720       from S \<subseteq> \<Union>D show "S \<subseteq> \<Union>?D"

  4721         apply (rule subset_trans)

  4722         apply clarsimp

  4723         apply (frule subsetD [OF D \<subseteq> ?C, THEN f_inv_into_f])

  4724         apply (erule rev_bexI, fast)

  4725         done

  4726     qed

  4727     thus "\<exists>D\<subseteq>C. finite D \<and> S \<subseteq> \<Union>D" ..

  4728   qed

  4729 qed

  4730

  4731 lemma compact_continuous_image:

  4732   assumes "continuous_on s f" and "compact s"

  4733   shows "compact (f  s)"

  4734 using assms (* FIXME: long unstructured proof *)

  4735 unfolding continuous_on_open

  4736 unfolding compact_eq_openin_cover

  4737 apply clarify

  4738 apply (drule_tac x="image (\<lambda>t. {x \<in> s. f x \<in> t}) C" in spec)

  4739 apply (drule mp)

  4740 apply (rule conjI)

  4741 apply simp

  4742 apply clarsimp

  4743 apply (drule subsetD)

  4744 apply (erule imageI)

  4745 apply fast

  4746 apply (erule thin_rl)

  4747 apply clarify

  4748 apply (rule_tac x="image (inv_into C (\<lambda>t. {x \<in> s. f x \<in> t})) D" in exI)

  4749 apply (intro conjI)

  4750 apply clarify

  4751 apply (rule inv_into_into)

  4752 apply (erule (1) subsetD)

  4753 apply (erule finite_imageI)

  4754 apply (clarsimp, rename_tac x)

  4755 apply (drule (1) subsetD, clarify)

  4756 apply (drule (1) subsetD, clarify)

  4757 apply (rule rev_bexI)

  4758 apply assumption

  4759 apply (subgoal_tac "{x \<in> s. f x \<in> t} \<in> (\<lambda>t. {x \<in> s. f x \<in> t})  C")

  4760 apply (drule f_inv_into_f)

  4761 apply fast

  4762 apply (erule imageI)

  4763 done

  4764

  4765 lemma connected_continuous_image:

  4766   assumes "continuous_on s f"  "connected s"

  4767   shows "connected(f  s)"

  4768 proof-

  4769   { fix T assume as: "T \<noteq> {}"  "T \<noteq> f  s"  "openin (subtopology euclidean (f  s)) T"  "closedin (subtopology euclidean (f  s)) T"

  4770     have "{x \<in> s. f x \<in> T} = {} \<or> {x \<in> s. f x \<in> T} = s"

  4771       using assms(1)[unfolded continuous_on_open, THEN spec[where x=T]]

  4772       using assms(1)[unfolded continuous_on_closed, THEN spec[where x=T]]

  4773       using assms(2)[unfolded connected_clopen, THEN spec[where x="{x \<in> s. f x \<in> T}"]] as(3,4) by auto

  4774     hence False using as(1,2)

  4775       using as(4)[unfolded closedin_def topspace_euclidean_subtopology] by auto }

  4776   thus ?thesis unfolding connected_clopen by auto

  4777 qed

  4778

  4779 text {* Continuity implies uniform continuity on a compact domain. *}

  4780

  4781 lemma compact_uniformly_continuous:

  4782   assumes f: "continuous_on s f" and s: "compact s"

  4783   shows "uniformly_continuous_on s f"

  4784   unfolding uniformly_continuous_on_def

  4785 proof (cases, safe)

  4786   fix e :: real assume "0 < e" "s \<noteq> {}"

  4787   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) } }"

  4788   let ?b = "(\<lambda>(y, d). ball y (d/2))"

  4789   have "(\<forall>r\<in>R. open (?b r))" "s \<subseteq> (\<Union>r\<in>R. ?b r)"

  4790   proof safe

  4791     fix y assume "y \<in> s"

  4792     from continuous_open_in_preimage[OF f open_ball]

  4793     obtain T where "open T" and T: "{x \<in> s. f x \<in> ball (f y) (e/2)} = T \<inter> s"

  4794       unfolding openin_subtopology open_openin by metis

  4795     then obtain d where "ball y d \<subseteq> T" "0 < d"

  4796       using 0 < e y \<in> s by (auto elim!: openE)

  4797     with T y \<in> s show "y \<in> (\<Union>r\<in>R. ?b r)"

  4798       by (intro UN_I[of "(y, d)"]) auto

  4799   qed auto

  4800   with s obtain D where D: "finite D" "D \<subseteq> R" "s \<subseteq> (\<Union>(y, d)\<in>D. ball y (d/2))"

  4801     by (rule compactE_image)

  4802   with s \<noteq> {} have [simp]: "\<And>x. x < Min (snd  D) \<longleftrightarrow> (\<forall>(y, d)\<in>D. x < d)"

  4803     by (subst Min_gr_iff) auto

  4804   show "\<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e"

  4805   proof (rule, safe)

  4806     fix x x' assume in_s: "x' \<in> s" "x \<in> s"

  4807     with D obtain y d where x: "x \<in> ball y (d/2)" "(y, d) \<in> D"

  4808       by blast

  4809     moreover assume "dist x x' < Min (sndD) / 2"

  4810     ultimately have "dist y x' < d"

  4811       by (intro dist_double[where x=x and d=d]) (auto simp: dist_commute)

  4812     with D x in_s show  "dist (f x) (f x') < e"

  4813       by (intro dist_double[where x="f y" and d=e]) (auto simp: dist_commute subset_eq)

  4814   qed (insert D, auto)

  4815 qed auto

  4816

  4817 text{* Continuity of inverse function on compact domain. *}

  4818

  4819 lemma continuous_on_inv:

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

  4821   assumes "continuous_on s f"  "compact s"  "\<forall>x \<in> s. g (f x) = x"

  4822   shows "continuous_on (f  s) g"

  4823 unfolding continuous_on_topological

  4824 proof (clarsimp simp add: assms(3))

  4825   fix x :: 'a and B :: "'a set"

  4826   assume "x \<in> s" and "open B" and "x \<in> B"

  4827   have 1: "\<forall>x\<in>s. f x \<in> f  (s - B) \<longleftrightarrow> x \<in> s - B"

  4828     using assms(3) by (auto, metis)

  4829   have "continuous_on (s - B) f"

  4830     using continuous_on s f Diff_subset

  4831     by (rule continuous_on_subset)

  4832   moreover have "compact (s - B)"

  4833     using open B and compact s

  4834     unfolding Diff_eq by (intro compact_inter_closed closed_Compl)

  4835   ultimately have "compact (f  (s - B))"

  4836     by (rule compact_continuous_image)

  4837   hence "closed (f  (s - B))"

  4838     by (rule compact_imp_closed)

  4839   hence "open (- f  (s - B))"

  4840     by (rule open_Compl)

  4841   moreover have "f x \<in> - f  (s - B)"

  4842     using x \<in> s and x \<in> B by (simp add: 1)

  4843   moreover have "\<forall>y\<in>s. f y \<in> - f  (s - B) \<longrightarrow> y \<in> B"

  4844     by (simp add: 1)

  4845   ultimately show "\<exists>A. open A \<and> f x \<in> A \<and> (\<forall>y\<in>s. f y \<in> A \<longrightarrow> y \<in> B)"

  4846     by fast

  4847 qed

  4848

  4849 text {* A uniformly convergent limit of continuous functions is continuous. *}

  4850

  4851 lemma continuous_uniform_limit:

  4852   fixes f :: "'a \<Rightarrow> 'b::metric_space \<Rightarrow> 'c::metric_space"

  4853   assumes "\<not> trivial_limit F"

  4854   assumes "eventually (\<lambda>n. continuous_on s (f n)) F"

  4855   assumes "\<forall>e>0. eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e) F"

  4856   shows "continuous_on s g"

  4857 proof-

  4858   { fix x and e::real assume "x\<in>s" "e>0"

  4859     have "eventually (\<lambda>n. \<forall>x\<in>s. dist (f n x) (g x) < e / 3) F"

  4860       using e>0 assms(3)[THEN spec[where x="e/3"]] by auto

  4861     from eventually_happens [OF eventually_conj [OF this assms(2)]]

  4862     obtain n where n:"\<forall>x\<in>s. dist (f n x) (g x) < e / 3"  "continuous_on s (f n)"

  4863       using assms(1) by blast

  4864     have "e / 3 > 0" using e>0 by auto

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

  4866       using n(2)[unfolded continuous_on_iff, THEN bspec[where x=x], OF x\<in>s, THEN spec[where x="e/3"]] by blast

  4867     { fix y assume "y \<in> s" and "dist y x < d"

  4868       hence "dist (f n y) (f n x) < e / 3"

  4869         by (rule d [rule_format])

  4870       hence "dist (f n y) (g x) < 2 * e / 3"

  4871         using dist_triangle [of "f n y" "g x" "f n x"]

  4872         using n(1)[THEN bspec[where x=x], OF x\<in>s]

  4873         by auto

  4874       hence "dist (g y) (g x) < e"

  4875         using n(1)[THEN bspec[where x=y], OF y\<in>s]

  4876         using dist_triangle3 [of "g y" "g x" "f n y"]

  4877         by auto }

  4878     hence "\<exists>d>0. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (g x') (g x) < e"

  4879       using d>0 by auto }

  4880   thus ?thesis unfolding continuous_on_iff by auto

  4881 qed

  4882

  4883

  4884 subsection {* Topological stuff lifted from and dropped to R *}

  4885

  4886 lemma open_real:

  4887   fixes s :: "real set" shows

  4888  "open s \<longleftrightarrow>

  4889         (\<forall>x \<in> s. \<exists>e>0. \<forall>x'. abs(x' - x) < e --> x' \<in> s)" (is "?lhs = ?rhs")

  4890   unfolding open_dist dist_norm by simp

  4891

  4892 lemma islimpt_approachable_real:

  4893   fixes s :: "real set"

  4894   shows "x islimpt s \<longleftrightarrow> (\<forall>e>0.  \<exists>x'\<in> s. x' \<noteq> x \<and> abs(x' - x) < e)"

  4895   unfolding islimpt_approachable dist_norm by simp

  4896

  4897 lemma closed_real:

  4898   fixes s :: "real set"

  4899   shows "closed s \<longleftrightarrow>

  4900         (\<forall>x. (\<forall>e>0.  \<exists>x' \<in> s. x' \<noteq> x \<and> abs(x' - x) < e)

  4901             --> x \<in> s)"

  4902   unfolding closed_limpt islimpt_approachable dist_norm by simp

  4903

  4904 lemma continuous_at_real_range:

  4905   fixes f :: "'a::real_normed_vector \<Rightarrow> real"

  4906   shows "continuous (at x) f \<longleftrightarrow> (\<forall>e>0. \<exists>d>0.

  4907         \<forall>x'. norm(x' - x) < d --> abs(f x' - f x) < e)"

  4908   unfolding continuous_at unfolding Lim_at

  4909   unfolding dist_nz[THEN sym] unfolding dist_norm apply auto

  4910   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

  4911   apply(erule_tac x=e in allE) by auto

  4912

  4913 lemma continuous_on_real_range:

  4914   fixes f :: "'a::real_normed_vector \<Rightarrow> real"

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

  4916   unfolding continuous_on_iff dist_norm by simp

  4917

  4918 text {* Hence some handy theorems on distance, diameter etc. of/from a set. *}

  4919

  4920 lemma compact_attains_sup:

  4921   fixes s :: "real set"

  4922   assumes "compact s"  "s \<noteq> {}"

  4923   shows "\<exists>x \<in> s. \<forall>y \<in> s. y \<le> x"

  4924 proof-

  4925   from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto

  4926   { fix e::real assume as: "\<forall>x\<in>s. x \<le> Sup s" "Sup s \<notin> s"  "0 < e" "\<forall>x'\<in>s. x' = Sup s \<or> \<not> Sup s - x' < e"

  4927     have "isLub UNIV s (Sup s)" using Sup[OF assms(2)] unfolding setle_def using as(1) by auto

  4928     moreover have "isUb UNIV s (Sup s - e)" unfolding isUb_def unfolding setle_def using as(4,2) by auto

  4929     ultimately have False using isLub_le_isUb[of UNIV s "Sup s" "Sup s - e"] using e>0 by auto  }

  4930   thus ?thesis using bounded_has_Sup(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Sup s"]]

  4931     apply(rule_tac x="Sup s" in bexI) by auto

  4932 qed

  4933

  4934 lemma Inf:

  4935   fixes S :: "real set"

  4936   shows "S \<noteq> {} ==> (\<exists>b. b <=* S) ==> isGlb UNIV S (Inf S)"

  4937 by (auto simp add: isLb_def setle_def setge_def isGlb_def greatestP_def)

  4938

  4939 lemma compact_attains_inf:

  4940   fixes s :: "real set"

  4941   assumes "compact s" "s \<noteq> {}"  shows "\<exists>x \<in> s. \<forall>y \<in> s. x \<le> y"

  4942 proof-

  4943   from assms(1) have a:"bounded s" "closed s" unfolding compact_eq_bounded_closed by auto

  4944   { fix e::real assume as: "\<forall>x\<in>s. x \<ge> Inf s"  "Inf s \<notin> s"  "0 < e"

  4945       "\<forall>x'\<in>s. x' = Inf s \<or> \<not> abs (x' - Inf s) < e"

  4946     have "isGlb UNIV s (Inf s)" using Inf[OF assms(2)] unfolding setge_def using as(1) by auto

  4947     moreover

  4948     { fix x assume "x \<in> s"

  4949       hence *:"abs (x - Inf s) = x - Inf s" using as(1)[THEN bspec[where x=x]] by auto

  4950       have "Inf s + e \<le> x" using as(4)[THEN bspec[where x=x]] using as(2) x\<in>s unfolding * by auto }

  4951     hence "isLb UNIV s (Inf s + e)" unfolding isLb_def and setge_def by auto

  4952     ultimately have False using isGlb_le_isLb[of UNIV s "Inf s" "Inf s + e"] using e>0 by auto  }

  4953   thus ?thesis using bounded_has_Inf(1)[OF a(1) assms(2)] using a(2)[unfolded closed_real, THEN spec[where x="Inf s"]]

  4954     apply(rule_tac x="Inf s" in bexI) by auto

  4955 qed

  4956

  4957 lemma continuous_attains_sup:

  4958   fixes f :: "'a::topological_space \<Rightarrow> real"

  4959   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f

  4960         ==> (\<exists>x \<in> s. \<forall>y \<in> s.  f y \<le> f x)"

  4961   using compact_attains_sup[of "f  s"]

  4962   using compact_continuous_image[of s f] by auto

  4963

  4964 lemma continuous_attains_inf:

  4965   fixes f :: "'a::topological_space \<Rightarrow> real"

  4966   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f

  4967         \<Longrightarrow> (\<exists>x \<in> s. \<forall>y \<in> s. f x \<le> f y)"

  4968   using compact_attains_inf[of "f  s"]

  4969   using compact_continuous_image[of s f] by auto

  4970

  4971 lemma distance_attains_sup:

  4972   assumes "compact s" "s \<noteq> {}"

  4973   shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a y \<le> dist a x"

  4974 proof (rule continuous_attains_sup [OF assms])

  4975   { fix x assume "x\<in>s"

  4976     have "(dist a ---> dist a x) (at x within s)"

  4977       by (intro tendsto_dist tendsto_const Lim_at_within tendsto_ident_at)

  4978   }

  4979   thus "continuous_on s (dist a)"

  4980     unfolding continuous_on ..

  4981 qed

  4982

  4983 text {* For \emph{minimal} distance, we only need closure, not compactness. *}

  4984

  4985 lemma distance_attains_inf:

  4986   fixes a :: "'a::heine_borel"

  4987   assumes "closed s"  "s \<noteq> {}"

  4988   shows "\<exists>x \<in> s. \<forall>y \<in> s. dist a x \<le> dist a y"

  4989 proof-

  4990   from assms(2) obtain b where "b\<in>s" by auto

  4991   let ?B = "cball a (dist b a) \<inter> s"

  4992   have "b \<in> ?B" using b\<in>s by (simp add: dist_commute)

  4993   hence "?B \<noteq> {}" by auto

  4994   moreover

  4995   { fix x assume "x\<in>?B"

  4996     fix e::real assume "e>0"

  4997     { fix x' assume "x'\<in>?B" and as:"dist x' x < e"

  4998       from as have "\<bar>dist a x' - dist a x\<bar> < e"

  4999         unfolding abs_less_iff minus_diff_eq

  5000         using dist_triangle2 [of a x' x]

  5001         using dist_triangle [of a x x']

  5002         by arith

  5003     }

  5004     hence "\<exists>d>0. \<forall>x'\<in>?B. dist x' x < d \<longrightarrow> \<bar>dist a x' - dist a x\<bar> < e"

  5005       using e>0 by auto

  5006   }

  5007   hence "continuous_on (cball a (dist b a) \<inter> s) (dist a)"

  5008     unfolding continuous_on Lim_within dist_norm real_norm_def

  5009     by fast

  5010   moreover have "compact ?B"

  5011     using compact_cball[of a "dist b a"]

  5012     unfolding compact_eq_bounded_closed

  5013     using bounded_Int and closed_Int and assms(1) by auto

  5014   ultimately obtain x where "x\<in>cball a (dist b a) \<inter> s" "\<forall>y\<in>cball a (dist b a) \<inter> s. dist a x \<le> dist a y"

  5015     using continuous_attains_inf[of ?B "dist a"] by fastforce

  5016   thus ?thesis by fastforce

  5017 qed

  5018

  5019

  5020 subsection {* Pasted sets *}

  5021

  5022 lemma bounded_Times:

  5023   assumes "bounded s" "bounded t" shows "bounded (s \<times> t)"

  5024 proof-

  5025   obtain x y a b where "\<forall>z\<in>s. dist x z \<le> a" "\<forall>z\<in>t. dist y z \<le> b"

  5026     using assms [unfolded bounded_def] by auto

  5027   then have "\<forall>z\<in>s \<times> t. dist (x, y) z \<le> sqrt (a\<twosuperior> + b\<twosuperior>)"

  5028     by (auto simp add: dist_Pair_Pair real_sqrt_le_mono add_mono power_mono)

  5029   thus ?thesis unfolding bounded_any_center [where a="(x, y)"] by auto

  5030 qed

  5031

  5032 lemma mem_Times_iff: "x \<in> A \<times> B \<longleftrightarrow> fst x \<in> A \<and> snd x \<in> B"

  5033 by (induct x) simp

  5034

  5035 lemma seq_compact_Times: "seq_compact s \<Longrightarrow> seq_compact t \<Longrightarrow> seq_compact (s \<times> t)"

  5036 unfolding seq_compact_def

  5037 apply clarify

  5038 apply (drule_tac x="fst \<circ> f" in spec)

  5039 apply (drule mp, simp add: mem_Times_iff)

  5040 apply (clarify, rename_tac l1 r1)

  5041 apply (drule_tac x="snd \<circ> f \<circ> r1" in spec)

  5042 apply (drule mp, simp add: mem_Times_iff)

  5043 apply (clarify, rename_tac l2 r2)

  5044 apply (rule_tac x="(l1, l2)" in rev_bexI, simp)

  5045 apply (rule_tac x="r1 \<circ> r2" in exI)

  5046 apply (rule conjI, simp add: subseq_def)

  5047 apply (drule_tac f=r2 in LIMSEQ_subseq_LIMSEQ, assumption)

  5048 apply (drule (1) tendsto_Pair) back

  5049 apply (simp add: o_def)

  5050 done

  5051

  5052 text {* Generalize to @{class topological_space} *}

  5053 lemma compact_Times:

  5054   fixes s :: "'a::metric_space set" and t :: "'b::metric_space set"

  5055   shows "compact s \<Longrightarrow> compact t \<Longrightarrow> compact (s \<times> t)"

  5056   unfolding compact_eq_seq_compact_metric by (rule seq_compact_Times)

  5057

  5058 text{* Hence some useful properties follow quite easily. *}

  5059

  5060 lemma compact_scaling:

  5061   fixes s :: "'a::real_normed_vector set"

  5062   assumes "compact s"  shows "compact ((\<lambda>x. c *\<^sub>R x)  s)"

  5063 proof-

  5064   let ?f = "\<lambda>x. scaleR c x"

  5065   have *:"bounded_linear ?f" by (rule bounded_linear_scaleR_right)

  5066   show ?thesis using compact_continuous_image[of s ?f] continuous_at_imp_continuous_on[of s ?f]

  5067     using linear_continuous_at[OF *] assms by auto

  5068 qed

  5069

  5070 lemma compact_negations:

  5071   fixes s :: "'a::real_normed_vector set"

  5072   assumes "compact s"  shows "compact ((\<lambda>x. -x)  s)"

  5073   using compact_scaling [OF assms, of "- 1"] by auto

  5074

  5075 lemma compact_sums:

  5076   fixes s t :: "'a::real_normed_vector set"

  5077   assumes "compact s"  "compact t"  shows "compact {x + y | x y. x \<in> s \<and> y \<in> t}"

  5078 proof-

  5079   have *:"{x + y | x y. x \<in> s \<and> y \<in> t} = (\<lambda>z. fst z + snd z)  (s \<times> t)"

  5080     apply auto unfolding image_iff apply(rule_tac x="(xa, y)" in bexI) by auto

  5081   have "continuous_on (s \<times> t) (\<lambda>z. fst z + snd z)"

  5082     unfolding continuous_on by (rule ballI) (intro tendsto_intros)

  5083   thus ?thesis unfolding * using compact_continuous_image compact_Times [OF assms] by auto

  5084 qed

  5085

  5086 lemma compact_differences:

  5087   fixes s t :: "'a::real_normed_vector set"

  5088   assumes "compact s" "compact t"  shows "compact {x - y | x y. x \<in> s \<and> y \<in> t}"

  5089 proof-

  5090   have "{x - y | x y. x\<in>s \<and> y \<in> t} =  {x + y | x y. x \<in> s \<and> y \<in> (uminus  t)}"

  5091     apply auto apply(rule_tac x= xa in exI) apply auto apply(rule_tac x=xa in exI) by auto

  5092   thus ?thesis using compact_sums[OF assms(1) compact_negations[OF assms(2)]] by auto

  5093 qed

  5094

  5095 lemma compact_translation:

  5096   fixes s :: "'a::real_normed_vector set"

  5097   assumes "compact s"  shows "compact ((\<lambda>x. a + x)  s)"

  5098 proof-

  5099   have "{x + y |x y. x \<in> s \<and> y \<in> {a}} = (\<lambda>x. a + x)  s" by auto

  5100   thus ?thesis using compact_sums[OF assms compact_sing[of a]] by auto

  5101 qed

  5102

  5103 lemma compact_affinity:

  5104   fixes s :: "'a::real_normed_vector set"

  5105   assumes "compact s"  shows "compact ((\<lambda>x. a + c *\<^sub>R x)  s)"

  5106 proof-

  5107   have "op + a  op *\<^sub>R c  s = (\<lambda>x. a + c *\<^sub>R x)  s" by auto

  5108   thus ?thesis using compact_translation[OF compact_scaling[OF assms], of a c] by auto

  5109 qed

  5110

  5111 text {* Hence we get the following. *}

  5112

  5113 lemma compact_sup_maxdistance:

  5114   fixes s :: "'a::metric_space set"

  5115   assumes "compact s"  "s \<noteq> {}"

  5116   shows "\<exists>x\<in>s. \<exists>y\<in>s. \<forall>u\<in>s. \<forall>v\<in>s. dist u v \<le> dist x y"

  5117 proof-

  5118   have "compact (s \<times> s)" using compact s by (intro compact_Times)

  5119   moreover have "s \<times> s \<noteq> {}" using s \<noteq> {} by auto

  5120   moreover have "continuous_on (s \<times> s) (\<lambda>x. dist (fst x) (snd x))"

  5121     by (intro continuous_at_imp_continuous_on ballI continuous_dist

  5122       continuous_isCont[THEN iffD1] isCont_fst isCont_snd isCont_ident)

  5123   ultimately show ?thesis

  5124     using continuous_attains_sup[of "s \<times> s" "\<lambda>x. dist (fst x) (snd x)"] by auto

  5125 qed

  5126

  5127 text {* We can state this in terms of diameter of a set. *}

  5128

  5129 definition "diameter s = (if s = {} then 0::real else Sup {dist x y | x y. x \<in> s \<and> y \<in> s})"

  5130

  5131 lemma diameter_bounded_bound:

  5132   fixes s :: "'a :: metric_space set"

  5133   assumes s: "bounded s" "x \<in> s" "y \<in> s"

  5134   shows "dist x y \<le> diameter s"

  5135 proof -

  5136   let ?D = "{dist x y |x y. x \<in> s \<and> y \<in> s}"

  5137   from s obtain z d where z: "\<And>x. x \<in> s \<Longrightarrow> dist z x \<le> d"

  5138     unfolding bounded_def by auto

  5139   have "dist x y \<le> Sup ?D"

  5140   proof (rule Sup_upper, safe)

  5141     fix a b assume "a \<in> s" "b \<in> s"

  5142     with z[of a] z[of b] dist_triangle[of a b z]

  5143     show "dist a b \<le> 2 * d"

  5144       by (simp add: dist_commute)

  5145   qed (insert s, auto)

  5146   with x \<in> s show ?thesis

  5147     by (auto simp add: diameter_def)

  5148 qed

  5149

  5150 lemma diameter_lower_bounded:

  5151   fixes s :: "'a :: metric_space set"

  5152   assumes s: "bounded s" and d: "0 < d" "d < diameter s"

  5153   shows "\<exists>x\<in>s. \<exists>y\<in>s. d < dist x y"

  5154 proof (rule ccontr)

  5155   let ?D = "{dist x y |x y. x \<in> s \<and> y \<in> s}"

  5156   assume contr: "\<not> ?thesis"

  5157   moreover

  5158   from d have "s \<noteq> {}"

  5159     by (auto simp: diameter_def)

  5160   then have "?D \<noteq> {}" by auto

  5161   ultimately have "Sup ?D \<le> d"

  5162     by (intro Sup_least) (auto simp: not_less)

  5163   with d < diameter s s \<noteq> {} show False

  5164     by (auto simp: diameter_def)

  5165 qed

  5166

  5167 lemma diameter_bounded:

  5168   assumes "bounded s"

  5169   shows "\<forall>x\<in>s. \<forall>y\<in>s. dist x y \<le> diameter s"

  5170         "\<forall>d>0. d < diameter s \<longrightarrow> (\<exists>x\<in>s. \<exists>y\<in>s. dist x y > d)"

  5171   using diameter_bounded_bound[of s] diameter_lower_bounded[of s] assms

  5172   by auto

  5173

  5174 lemma diameter_compact_attained:

  5175   assumes "compact s"  "s \<noteq> {}"

  5176   shows "\<exists>x\<in>s. \<exists>y\<in>s. dist x y = diameter s"

  5177 proof -

  5178   have b:"bounded s" using assms(1) by (rule compact_imp_bounded)

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

  5180     using compact_sup_maxdistance[OF assms] by auto

  5181   hence "diameter s \<le> dist x y"

  5182     unfolding diameter_def by clarsimp (rule Sup_least, fast+)

  5183   thus ?thesis

  5184     by (metis b diameter_bounded_bound order_antisym xys)

  5185 qed

  5186

  5187 text {* Related results with closure as the conclusion. *}

  5188

  5189 lemma closed_scaling:

  5190   fixes s :: "'a::real_normed_vector set"

  5191   assumes "closed s" shows "closed ((\<lambda>x. c *\<^sub>R x)  s)"

  5192 proof(cases "s={}")

  5193   case True thus ?thesis by auto

  5194 next

  5195   case False

  5196   show ?thesis

  5197   proof(cases "c=0")

  5198     have *:"(\<lambda>x. 0)  s = {0}" using s\<noteq>{} by auto

  5199     case True thus ?thesis apply auto unfolding * by auto

  5200   next

  5201     case False

  5202     { fix x l assume as:"\<forall>n::nat. x n \<in> scaleR c  s"  "(x ---> l) sequentially"

  5203       { fix n::nat have "scaleR (1 / c) (x n) \<in> s"

  5204           using as(1)[THEN spec[where x=n]]

  5205           using c\<noteq>0 by auto

  5206       }

  5207       moreover

  5208       { fix e::real assume "e>0"

  5209         hence "0 < e *\<bar>c\<bar>"  using c\<noteq>0 mult_pos_pos[of e "abs c"] by auto

  5210         then obtain N where "\<forall>n\<ge>N. dist (x n) l < e * \<bar>c\<bar>"

  5211           using as(2)[unfolded LIMSEQ_def, THEN spec[where x="e * abs c"]] by auto

  5212         hence "\<exists>N. \<forall>n\<ge>N. dist (scaleR (1 / c) (x n)) (scaleR (1 / c) l) < e"

  5213           unfolding dist_norm unfolding scaleR_right_diff_distrib[THEN sym]

  5214           using mult_imp_div_pos_less[of "abs c" _ e] c\<noteq>0 by auto  }

  5215       hence "((\<lambda>n. scaleR (1 / c) (x n)) ---> scaleR (1 / c) l) sequentially" unfolding LIMSEQ_def by auto

  5216       ultimately have "l \<in> scaleR c  s"

  5217         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"]]

  5218         unfolding image_iff using c\<noteq>0 apply(rule_tac x="scaleR (1 / c) l" in bexI) by auto  }

  5219     thus ?thesis unfolding closed_sequential_limits by fast

  5220   qed

  5221 qed

  5222

  5223 lemma closed_negations:

  5224   fixes s :: "'a::real_normed_vector set"

  5225   assumes "closed s"  shows "closed ((\<lambda>x. -x)  s)"

  5226   using closed_scaling[OF assms, of "- 1"] by simp

  5227

  5228 lemma compact_closed_sums:

  5229   fixes s :: "'a::real_normed_vector set"

  5230   assumes "compact s"  "closed t"  shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"

  5231 proof-

  5232   let ?S = "{x + y |x y. x \<in> s \<and> y \<in> t}"

  5233   { fix x l assume as:"\<forall>n. x n \<in> ?S"  "(x ---> l) sequentially"

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

  5235       using choice[of "\<lambda>n y. x n = (fst y) + (snd y) \<and> fst y \<in> s \<and> snd y \<in> t"] by auto

  5236     obtain l' r where "l'\<in>s" and r:"subseq r" and lr:"(((\<lambda>n. fst (f n)) \<circ> r) ---> l') sequentially"

  5237       using assms(1)[unfolded compact_def, THEN spec[where x="\<lambda> n. fst (f n)"]] using f(2) by auto

  5238     have "((\<lambda>n. snd (f (r n))) ---> l - l') sequentially"

  5239       using tendsto_diff[OF LIMSEQ_subseq_LIMSEQ[OF as(2) r] lr] and f(1) unfolding o_def by auto

  5240     hence "l - l' \<in> t"

  5241       using assms(2)[unfolded closed_sequential_limits, THEN spec[where x="\<lambda> n. snd (f (r n))"], THEN spec[where x="l - l'"]]

  5242       using f(3) by auto

  5243     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

  5244   }

  5245   thus ?thesis unfolding closed_sequential_limits by fast

  5246 qed

  5247

  5248 lemma closed_compact_sums:

  5249   fixes s t :: "'a::real_normed_vector set"

  5250   assumes "closed s"  "compact t"

  5251   shows "closed {x + y | x y. x \<in> s \<and> y \<in> t}"

  5252 proof-

  5253   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

  5254     apply(rule_tac x=y in exI) apply auto apply(rule_tac x=y in exI) by auto

  5255   thus ?thesis using compact_closed_sums[OF assms(2,1)] by simp

  5256 qed

  5257

  5258 lemma compact_closed_differences:

  5259   fixes s t :: "'a::real_normed_vector set"

  5260   assumes "compact s"  "closed t"

  5261   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"

  5262 proof-

  5263   have "{x + y |x y. x \<in> s \<and> y \<in> uminus  t} =  {x - y |x y. x \<in> s \<and> y \<in> t}"

  5264     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto

  5265   thus ?thesis using compact_closed_sums[OF assms(1) closed_negations[OF assms(2)]] by auto

  5266 qed

  5267

  5268 lemma closed_compact_differences:

  5269   fixes s t :: "'a::real_normed_vector set"

  5270   assumes "closed s" "compact t"

  5271   shows "closed {x - y | x y. x \<in> s \<and> y \<in> t}"

  5272 proof-

  5273   have "{x + y |x y. x \<in> s \<and> y \<in> uminus  t} = {x - y |x y. x \<in> s \<and> y \<in> t}"

  5274     apply auto apply(rule_tac x=xa in exI) apply auto apply(rule_tac x=xa in exI) by auto

  5275  thus ?thesis using closed_compact_sums[OF assms(1) compact_negations[OF assms(2)]] by simp

  5276 qed

  5277

  5278 lemma closed_translation:

  5279   fixes a :: "'a::real_normed_vector"

  5280   assumes "closed s"  shows "closed ((\<lambda>x. a + x)  s)"

  5281 proof-

  5282   have "{a + y |y. y \<in> s} = (op + a  s)" by auto

  5283   thus ?thesis using compact_closed_sums[OF compact_sing[of a] assms] by auto

  5284 qed

  5285

  5286 lemma translation_Compl:

  5287   fixes a :: "'a::ab_group_add"

  5288   shows "(\<lambda>x. a + x)  (- t) = - ((\<lambda>x. a + x)  t)"

  5289   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in bexI) by auto

  5290

  5291 lemma translation_UNIV:

  5292   fixes a :: "'a::ab_group_add" shows "range (\<lambda>x. a + x) = UNIV"

  5293   apply (auto simp add: image_iff) apply(rule_tac x="x - a" in exI) by auto

  5294

  5295 lemma translation_diff:

  5296   fixes a :: "'a::ab_group_add"

  5297   shows "(\<lambda>x. a + x)  (s - t) = ((\<lambda>x. a + x)  s) - ((\<lambda>x. a + x)  t)"

  5298   by auto

  5299

  5300 lemma closure_translation:

  5301   fixes a :: "'a::real_normed_vector"

  5302   shows "closure ((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (closure s)"

  5303 proof-

  5304   have *:"op + a  (- s) = - op + a  s"

  5305     apply auto unfolding image_iff apply(rule_tac x="x - a" in bexI) by auto

  5306   show ?thesis unfolding closure_interior translation_Compl

  5307     using interior_translation[of a "- s"] unfolding * by auto

  5308 qed

  5309

  5310 lemma frontier_translation:

  5311   fixes a :: "'a::real_normed_vector"

  5312   shows "frontier((\<lambda>x. a + x)  s) = (\<lambda>x. a + x)  (frontier s)"

  5313   unfolding frontier_def translation_diff interior_translation closure_translation by auto

  5314

  5315

  5316 subsection {* Separation between points and sets *}

  5317

  5318 lemma separate_point_closed:

  5319   fixes s :: "'a::heine_borel set"

  5320   shows "closed s \<Longrightarrow> a \<notin> s  ==> (\<exists>d>0. \<forall>x\<in>s. d \<le> dist a x)"

  5321 proof(cases "s = {}")

  5322   case True

  5323   thus ?thesis by(auto intro!: exI[where x=1])

  5324 next

  5325   case False

  5326   assume "closed s" "a \<notin> s"

  5327   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

  5328   with x\<in>s show ?thesis using dist_pos_lt[of a x] anda \<notin> s by blast

  5329 qed

  5330

  5331 lemma separate_compact_closed:

  5332   fixes s t :: "'a::heine_borel set"

  5333   assumes "compact s" and "closed t" and "s \<inter> t = {}"

  5334   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"

  5335 proof - (* FIXME: long proof *)

  5336   let ?T = "\<Union>x\<in>s. { ball x (d / 2) | d. 0 < d \<and> (\<forall>y\<in>t. d \<le> dist x y) }"

  5337   note compact s

  5338   moreover have "\<forall>t\<in>?T. open t" by auto

  5339   moreover have "s \<subseteq> \<Union>?T"

  5340     apply auto

  5341     apply (rule rev_bexI, assumption)

  5342     apply (subgoal_tac "x \<notin> t")

  5343     apply (drule separate_point_closed [OF closed t])

  5344     apply clarify

  5345     apply (rule_tac x="ball x (d / 2)" in exI)

  5346     apply simp

  5347     apply fast

  5348     apply (cut_tac assms(3))

  5349     apply auto

  5350     done

  5351   ultimately obtain U where "U \<subseteq> ?T" and "finite U" and "s \<subseteq> \<Union>U"

  5352     by (rule compactE)

  5353   from finite U and U \<subseteq> ?T have "\<exists>d>0. \<forall>x\<in>\<Union>U. \<forall>y\<in>t. d \<le> dist x y"

  5354     apply (induct set: finite)

  5355     apply simp

  5356     apply (rule exI)

  5357     apply (rule zero_less_one)

  5358     apply clarsimp

  5359     apply (rename_tac y e)

  5360     apply (rule_tac x="min d (e / 2)" in exI)

  5361     apply simp

  5362     apply (subst ball_Un)

  5363     apply (rule conjI)

  5364     apply (intro ballI, rename_tac z)

  5365     apply (rule min_max.le_infI2)

  5366     apply (simp only: mem_ball)

  5367     apply (drule (1) bspec)

  5368     apply (cut_tac x=y and y=x and z=z in dist_triangle, arith)

  5369     apply simp

  5370     apply (intro ballI)

  5371     apply (rule min_max.le_infI1)

  5372     apply simp

  5373     done

  5374   with s \<subseteq> \<Union>U show ?thesis

  5375     by fast

  5376 qed

  5377

  5378 lemma separate_closed_compact:

  5379   fixes s t :: "'a::heine_borel set"

  5380   assumes "closed s" and "compact t" and "s \<inter> t = {}"

  5381   shows "\<exists>d>0. \<forall>x\<in>s. \<forall>y\<in>t. d \<le> dist x y"

  5382 proof-

  5383   have *:"t \<inter> s = {}" using assms(3) by auto

  5384   show ?thesis using separate_compact_closed[OF assms(2,1) *]

  5385     apply auto apply(rule_tac x=d in exI) apply auto apply (erule_tac x=y in ballE)

  5386     by (auto simp add: dist_commute)

  5387 qed

  5388

  5389

  5390 subsection {* Intervals *}

  5391

  5392 lemma interval: fixes a :: "'a::ordered_euclidean_space" shows

  5393   "{a <..< b} = {x::'a. \<forall>i\<in>Basis. a\<bullet>i < x\<bullet>i \<and> x\<bullet>i < b\<bullet>i}" and

  5394   "{a .. b} = {x::'a. \<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i}"

  5395   by(auto simp add:set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])

  5396

  5397 lemma mem_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5398   "x \<in> {a<..<b} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < x\<bullet>i \<and> x\<bullet>i < b\<bullet>i)"

  5399   "x \<in> {a .. b} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> x\<bullet>i \<and> x\<bullet>i \<le> b\<bullet>i)"

  5400   using interval[of a b] by(auto simp add: set_eq_iff eucl_le[where 'a='a] eucl_less[where 'a='a])

  5401

  5402 lemma interval_eq_empty: fixes a :: "'a::ordered_euclidean_space" shows

  5403  "({a <..< b} = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i \<le> a\<bullet>i))" (is ?th1) and

  5404  "({a  ..  b} = {} \<longleftrightarrow> (\<exists>i\<in>Basis. b\<bullet>i < a\<bullet>i))" (is ?th2)

  5405 proof-

  5406   { fix i x assume i:"i\<in>Basis" and as:"b\<bullet>i \<le> a\<bullet>i" and x:"x\<in>{a <..< b}"

  5407     hence "a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i" unfolding mem_interval by auto

  5408     hence "a\<bullet>i < b\<bullet>i" by auto

  5409     hence False using as by auto  }

  5410   moreover

  5411   { assume as:"\<forall>i\<in>Basis. \<not> (b\<bullet>i \<le> a\<bullet>i)"

  5412     let ?x = "(1/2) *\<^sub>R (a + b)"

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

  5414       have "a\<bullet>i < b\<bullet>i" using as[THEN bspec[where x=i]] i by auto

  5415       hence "a\<bullet>i < ((1/2) *\<^sub>R (a+b)) \<bullet> i" "((1/2) *\<^sub>R (a+b)) \<bullet> i < b\<bullet>i"

  5416         by (auto simp: inner_add_left) }

  5417     hence "{a <..< b} \<noteq> {}" using mem_interval(1)[of "?x" a b] by auto  }

  5418   ultimately show ?th1 by blast

  5419

  5420   { fix i x assume i:"i\<in>Basis" and as:"b\<bullet>i < a\<bullet>i" and x:"x\<in>{a .. b}"

  5421     hence "a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i" unfolding mem_interval by auto

  5422     hence "a\<bullet>i \<le> b\<bullet>i" by auto

  5423     hence False using as by auto  }

  5424   moreover

  5425   { assume as:"\<forall>i\<in>Basis. \<not> (b\<bullet>i < a\<bullet>i)"

  5426     let ?x = "(1/2) *\<^sub>R (a + b)"

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

  5428       have "a\<bullet>i \<le> b\<bullet>i" using as[THEN bspec[where x=i]] i by auto

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

  5430         by (auto simp: inner_add_left) }

  5431     hence "{a .. b} \<noteq> {}" using mem_interval(2)[of "?x" a b] by auto  }

  5432   ultimately show ?th2 by blast

  5433 qed

  5434

  5435 lemma interval_ne_empty: fixes a :: "'a::ordered_euclidean_space" shows

  5436   "{a  ..  b} \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i \<le> b\<bullet>i)" and

  5437   "{a <..< b} \<noteq> {} \<longleftrightarrow> (\<forall>i\<in>Basis. a\<bullet>i < b\<bullet>i)"

  5438   unfolding interval_eq_empty[of a b] by fastforce+

  5439

  5440 lemma interval_sing:

  5441   fixes a :: "'a::ordered_euclidean_space"

  5442   shows "{a .. a} = {a}" and "{a<..<a} = {}"

  5443   unfolding set_eq_iff mem_interval eq_iff [symmetric]

  5444   by (auto intro: euclidean_eqI simp: ex_in_conv)

  5445

  5446 lemma subset_interval_imp: fixes a :: "'a::ordered_euclidean_space" shows

  5447  "(\<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

  5448  "(\<forall>i\<in>Basis. a\<bullet>i < c\<bullet>i \<and> d\<bullet>i < b\<bullet>i) \<Longrightarrow> {c .. d} \<subseteq> {a<..<b}" and

  5449  "(\<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

  5450  "(\<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}"

  5451   unfolding subset_eq[unfolded Ball_def] unfolding mem_interval

  5452   by (best intro: order_trans less_le_trans le_less_trans less_imp_le)+

  5453

  5454 lemma interval_open_subset_closed:

  5455   fixes a :: "'a::ordered_euclidean_space"

  5456   shows "{a<..<b} \<subseteq> {a .. b}"

  5457   unfolding subset_eq [unfolded Ball_def] mem_interval

  5458   by (fast intro: less_imp_le)

  5459

  5460 lemma subset_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5461  "{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

  5462  "{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

  5463  "{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

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

  5465 proof-

  5466   show ?th1 unfolding subset_eq and Ball_def and mem_interval by (auto intro: order_trans)

  5467   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)

  5468   { assume as: "{c<..<d} \<subseteq> {a .. b}" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"

  5469     hence "{c<..<d} \<noteq> {}" unfolding interval_eq_empty by auto

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

  5471     (** TODO combine the following two parts as done in the HOL_light version. **)

  5472     { 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"

  5473       assume as2: "a\<bullet>i > c\<bullet>i"

  5474       { fix j :: 'a assume j:"j\<in>Basis"

  5475         hence "c \<bullet> j < ?x \<bullet> j \<and> ?x \<bullet> j < d \<bullet> j"

  5476           apply(cases "j=i") using as(2)[THEN bspec[where x=j]] i

  5477           by (auto simp add: as2)  }

  5478       hence "?x\<in>{c<..<d}" using i unfolding mem_interval by auto

  5479       moreover

  5480       have "?x\<notin>{a .. b}"

  5481         unfolding mem_interval apply auto apply(rule_tac x=i in bexI)

  5482         using as(2)[THEN bspec[where x=i]] and as2 i

  5483         by auto

  5484       ultimately have False using as by auto  }

  5485     hence "a\<bullet>i \<le> c\<bullet>i" by(rule ccontr)auto

  5486     moreover

  5487     { 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"

  5488       assume as2: "b\<bullet>i < d\<bullet>i"

  5489       { fix j :: 'a assume "j\<in>Basis"

  5490         hence "d \<bullet> j > ?x \<bullet> j \<and> ?x \<bullet> j > c \<bullet> j"

  5491           apply(cases "j=i") using as(2)[THEN bspec[where x=j]]

  5492           by (auto simp add: as2) }

  5493       hence "?x\<in>{c<..<d}" unfolding mem_interval by auto

  5494       moreover

  5495       have "?x\<notin>{a .. b}"

  5496         unfolding mem_interval apply auto apply(rule_tac x=i in bexI)

  5497         using as(2)[THEN bspec[where x=i]] and as2 using i

  5498         by auto

  5499       ultimately have False using as by auto  }

  5500     hence "b\<bullet>i \<ge> d\<bullet>i" by(rule ccontr)auto

  5501     ultimately

  5502     have "a\<bullet>i \<le> c\<bullet>i \<and> d\<bullet>i \<le> b\<bullet>i" by auto

  5503   } note part1 = this

  5504   show ?th3

  5505     unfolding subset_eq and Ball_def and mem_interval

  5506     apply(rule,rule,rule,rule)

  5507     apply(rule part1)

  5508     unfolding subset_eq and Ball_def and mem_interval

  5509     prefer 4

  5510     apply auto

  5511     by(erule_tac x=xa in allE,erule_tac x=xa in allE,fastforce)+

  5512   { assume as:"{c<..<d} \<subseteq> {a<..<b}" "\<forall>i\<in>Basis. c\<bullet>i < d\<bullet>i"

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

  5514     from as(1) have "{c<..<d} \<subseteq> {a..b}" using interval_open_subset_closed[of a b] by auto

  5515     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

  5516   show ?th4 unfolding subset_eq and Ball_def and mem_interval

  5517     apply(rule,rule,rule,rule) apply(rule *) unfolding subset_eq and Ball_def and mem_interval prefer 4

  5518     apply auto by(erule_tac x=xa in allE, simp)+

  5519 qed

  5520

  5521 lemma inter_interval: fixes a :: "'a::ordered_euclidean_space" shows

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

  5523   unfolding set_eq_iff and Int_iff and mem_interval by auto

  5524

  5525 lemma disjoint_interval: fixes a::"'a::ordered_euclidean_space" shows

  5526   "{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

  5527   "{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

  5528   "{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

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

  5530 proof-

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

  5532   have **: "\<And>P Q. (\<And>i :: 'a. i \<in> Basis \<Longrightarrow> Q ?z i \<Longrightarrow> P i) \<Longrightarrow>

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

  5534     by blast

  5535   note * = set_eq_iff Int_iff empty_iff mem_interval ball_conj_distrib[symmetric] eq_False ball_simps(10)

  5536   show ?th1 unfolding * by (intro **) auto

  5537   show ?th2 unfolding * by (intro **) auto

  5538   show ?th3 unfolding * by (intro **) auto

  5539   show ?th4 unfolding * by (intro **) auto

  5540 qed

  5541

  5542 (* Moved interval_open_subset_closed a bit upwards *)

  5543

  5544 lemma open_interval[intro]:

  5545   fixes a b :: "'a::ordered_euclidean_space" shows "open {a<..<b}"

  5546 proof-

  5547   have "open (\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i<..<b\<bullet>i})"

  5548     by (intro open_INT finite_lessThan ballI continuous_open_vimage allI

  5549       linear_continuous_at open_real_greaterThanLessThan finite_Basis bounded_linear_inner_left)

  5550   also have "(\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i<..<b\<bullet>i}) = {a<..<b}"

  5551     by (auto simp add: eucl_less [where 'a='a])

  5552   finally show "open {a<..<b}" .

  5553 qed

  5554

  5555 lemma closed_interval[intro]:

  5556   fixes a b :: "'a::ordered_euclidean_space" shows "closed {a .. b}"

  5557 proof-

  5558   have "closed (\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i .. b\<bullet>i})"

  5559     by (intro closed_INT ballI continuous_closed_vimage allI

  5560       linear_continuous_at closed_real_atLeastAtMost finite_Basis bounded_linear_inner_left)

  5561   also have "(\<Inter>i\<in>Basis. (\<lambda>x. x\<bullet>i) - {a\<bullet>i .. b\<bullet>i}) = {a .. b}"

  5562     by (auto simp add: eucl_le [where 'a='a])

  5563   finally show "closed {a .. b}" .

  5564 qed

  5565

  5566 lemma interior_closed_interval [intro]:

  5567   fixes a b :: "'a::ordered_euclidean_space"

  5568   shows "interior {a..b} = {a<..<b}" (is "?L = ?R")

  5569 proof(rule subset_antisym)

  5570   show "?R \<subseteq> ?L" using interval_open_subset_closed open_interval

  5571     by (rule interior_maximal)

  5572 next

  5573   { fix x assume "x \<in> interior {a..b}"

  5574     then obtain s where s:"open s" "x \<in> s" "s \<subseteq> {a..b}" ..

  5575     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

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

  5577       have "dist (x - (e / 2) *\<^sub>R i) x < e"

  5578            "dist (x + (e / 2) *\<^sub>R i) x < e"

  5579         unfolding dist_norm apply auto

  5580         unfolding norm_minus_cancel using norm_Basis[OF i] e>0 by auto

  5581       hence "a \<bullet> i \<le> (x - (e / 2) *\<^sub>R i) \<bullet> i"

  5582                      "(x + (e / 2) *\<^sub>R i) \<bullet> i \<le> b \<bullet> i"

  5583         using e[THEN spec[where x="x - (e/2) *\<^sub>R i"]]

  5584         and   e[THEN spec[where x="x + (e/2) *\<^sub>R i"]]

  5585         unfolding mem_interval using i by blast+

  5586       hence "a \<bullet> i < x \<bullet> i" and "x \<bullet> i < b \<bullet> i"

  5587         using e>0 i by (auto simp: inner_diff_left inner_Basis inner_add_left) }

  5588     hence "x \<in> {a<..<b}" unfolding mem_interval by auto  }

  5589   thus "?L \<subseteq> ?R" ..

  5590 qed

  5591

  5592 lemma bounded_closed_interval: fixes a :: "'a::ordered_euclidean_space" shows "bounded {a .. b}"

  5593 proof-

  5594   let ?b = "\<Sum>i\<in>Basis. \<bar>a\<bullet>i\<bar> + \<bar>b\<bullet>i\<bar>"

  5595   { fix x::"'a" assume x:"\<forall>i\<in>Basis. a \<bullet> i \<le> x \<bullet> i \<and> x \<bullet> i \<le> b \<bullet> i"

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

  5597       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  }

  5598     hence "(\<Sum>i\<in>Basis. \<bar>x \<bullet> i\<bar>) \<le> ?b" apply-apply(rule setsum_mono) by auto

  5599     hence "norm x \<le> ?b" using norm_le_l1[of x] by auto  }

  5600   thus ?thesis unfolding interval and bounded_iff by auto

  5601 qed

  5602

  5603 lemma bounded_interval: fixes a :: "'a::ordered_euclidean_space" shows

  5604  "bounded {a .. b} \<and> bounded {a<..<b}"

  5605   using bounded_closed_interval[of a b]

  5606   using interval_open_subset_closed[of a b]

  5607   using bounded_subset[of "{a..b}" "{a<..<b}"]

  5608   by simp

  5609

  5610 lemma not_interval_univ: fixes a :: "'a::ordered_euclidean_space" shows

  5611  "({a .. b} \<noteq> UNIV) \<and> ({a<..<b} \<noteq> UNIV)"

  5612   using bounded_interval[of a b] by auto

  5613

  5614 lemma compact_interval: fixes a :: "'a::ordered_euclidean_space" shows "compact {a .. b}"

  5615   using bounded_closed_imp_seq_compact[of "{a..b}"] using bounded_interval[of a b]

  5616   by (auto simp: compact_eq_seq_compact_metric)

  5617

  5618 lemma open_interval_midpoint: fixes a :: "'a::ordered_euclidean_space"

  5619   assumes "{a<..<b} \<noteq> {}" shows "((1/2) *\<^sub>R (a + b)) \<in> {a<..<b}"

  5620 proof-

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

  5622     hence "a \<bullet> i < ((1 / 2) *\<^sub>R (a + b)) \<bullet> i \<and> ((1 / 2) *\<^sub>R (a + b)) \<bullet> i < b \<bullet> i"

  5623       using assms[unfolded interval_ne_empty, THEN bspec[where x=i]] by (auto simp: inner_add_left)  }

  5624   thus ?thesis unfolding mem_interval by auto

  5625 qed

  5626

  5627 lemma open_closed_interval_convex: fixes x :: "'a::ordered_euclidean_space"

  5628   assumes x:"x \<in> {a<..<b}" and y:"y \<in> {a .. b}" and e:"0 < e" "e \<le> 1"

  5629   shows "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<in> {a<..<b}"

  5630 proof-

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

  5632     have "a \<bullet> i = e * (a \<bullet> i) + (1 - e) * (a \<bullet> i)" unfolding left_diff_distrib by simp

  5633     also have "\<dots> < e * (x \<bullet> i) + (1 - e) * (y \<bullet> i)" apply(rule add_less_le_mono)

  5634       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all

  5635       using x unfolding mem_interval using i apply simp

  5636       using y unfolding mem_interval using i apply simp

  5637       done

  5638     finally have "a \<bullet> i < (e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i" unfolding inner_simps by auto

  5639     moreover {

  5640     have "b \<bullet> i = e * (b\<bullet>i) + (1 - e) * (b\<bullet>i)" unfolding left_diff_distrib by simp

  5641     also have "\<dots> > e * (x \<bullet> i) + (1 - e) * (y \<bullet> i)" apply(rule add_less_le_mono)

  5642       using e unfolding mult_less_cancel_left and mult_le_cancel_left apply simp_all

  5643       using x unfolding mem_interval using i apply simp

  5644       using y unfolding mem_interval using i apply simp

  5645       done

  5646     finally have "(e *\<^sub>R x + (1 - e) *\<^sub>R y) \<bullet> i < b \<bullet> i" unfolding inner_simps by auto

  5647     } 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 }

  5648   thus ?thesis unfolding mem_interval by auto

  5649 qed

  5650

  5651 lemma closure_open_interval: fixes a :: "'a::ordered_euclidean_space"

  5652   assumes "{a<..<b} \<noteq> {}"

  5653   shows "closure {a<..<b} = {a .. b}"

  5654 proof-

  5655   have ab:"a < b" using assms[unfolded interval_ne_empty] apply(subst eucl_less) by auto

  5656   let ?c = "(1 / 2) *\<^sub>R (a + b)"

  5657   { fix x assume as:"x \<in> {a .. b}"

  5658     def f == "\<lambda>n::nat. x + (inverse (real n + 1)) *\<^sub>R (?c - x)"

  5659     { fix n assume fn:"f n < b \<longrightarrow> a < f n \<longrightarrow> f n = x" and xc:"x \<noteq> ?c"

  5660       have *:"0 < inverse (real n + 1)" "inverse (real n + 1) \<le> 1" unfolding inverse_le_1_iff by auto

  5661       have "(inverse (real n + 1)) *\<^sub>R ((1 / 2) *\<^sub>R (a + b)) + (1 - inverse (real n + 1)) *\<^sub>R x =

  5662         x + (inverse (real n + 1)) *\<^sub>R (((1 / 2) *\<^sub>R (a + b)) - x)"

  5663         by (auto simp add: algebra_simps)

  5664       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

  5665       hence False using fn unfolding f_def using xc by auto  }

  5666     moreover

  5667     { assume "\<not> (f ---> x) sequentially"

  5668       { fix e::real assume "e>0"

  5669         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

  5670         then obtain N::nat where "inverse (real (N + 1)) < e" by auto

  5671         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)

  5672         hence "\<exists>N::nat. \<forall>n\<ge>N. inverse (real n + 1) < e" by auto  }

  5673       hence "((\<lambda>n. inverse (real n + 1)) ---> 0) sequentially"

  5674         unfolding LIMSEQ_def by(auto simp add: dist_norm)

  5675       hence "(f ---> x) sequentially" unfolding f_def

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

  5677         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  }

  5678     ultimately have "x \<in> closure {a<..<b}"

  5679       using as and open_interval_midpoint[OF assms] unfolding closure_def unfolding islimpt_sequential by(cases "x=?c")auto  }

  5680   thus ?thesis using closure_minimal[OF interval_open_subset_closed closed_interval, of a b] by blast

  5681 qed

  5682

  5683 lemma bounded_subset_open_interval_symmetric: fixes s::"('a::ordered_euclidean_space) set"

  5684   assumes "bounded s"  shows "\<exists>a. s \<subseteq> {-a<..<a}"

  5685 proof-

  5686   obtain b where "b>0" and b:"\<forall>x\<in>s. norm x \<le> b" using assms[unfolded bounded_pos] by auto

  5687   def a \<equiv> "(\<Sum>i\<in>Basis. (b + 1) *\<^sub>R i)::'a"

  5688   { fix x assume "x\<in>s"

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

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

  5691       and Basis_le_norm[OF i, of x] unfolding inner_simps and a_def by auto }

  5692   thus ?thesis by(auto intro: exI[where x=a] simp add: eucl_less[where 'a='a])

  5693 qed

  5694

  5695 lemma bounded_subset_open_interval:

  5696   fixes s :: "('a::ordered_euclidean_space) set"

  5697   shows "bounded s ==> (\<exists>a b. s \<subseteq> {a<..<b})"

  5698   by (auto dest!: bounded_subset_open_interval_symmetric)

  5699

  5700 lemma bounded_subset_closed_interval_symmetric:

  5701   fixes s :: "('a::ordered_euclidean_space) set"

  5702   assumes "bounded s" shows "\<exists>a. s \<subseteq> {-a .. a}"

  5703 proof-

  5704   obtain a where "s \<subseteq> {- a<..<a}" using bounded_subset_open_interval_symmetric[OF assms] by auto

  5705   thus ?thesis using interval_open_subset_closed[of "-a" a] by auto

  5706 qed

  5707

  5708 lemma bounded_subset_closed_interval:

  5709   fixes s :: "('a::ordered_euclidean_space) set"

  5710   shows "bounded s ==> (\<exists>a b. s \<subseteq> {a .. b})"

  5711   using bounded_subset_closed_interval_symmetric[of s] by auto

  5712

  5713 lemma frontier_closed_interval:

  5714   fixes a b :: "'a::ordered_euclidean_space"

  5715   shows "frontier {a .. b} = {a .. b} - {a<..<b}"

  5716   unfolding frontier_def unfolding interior_closed_interval and closure_closed[OF closed_interval] ..

  5717

  5718 lemma frontier_open_interval:

  5719   fixes a b :: "'a::ordered_euclidean_space"

  5720   shows "frontier {a<..<b} = (if {a<..<b} = {} then {} else {a .. b} - {a<..<b})"

  5721 proof(cases "{a<..<b} = {}")

  5722   case True thus ?thesis using frontier_empty by auto

  5723 next

  5724   case False thus ?thesis unfolding frontier_def and closure_open_interval[OF False] and interior_open[OF open_interval] by auto

  5725 qed

  5726

  5727 lemma inter_interval_mixed_eq_empty: fixes a :: "'a::ordered_euclidean_space"

  5728   assumes "{c<..<d} \<noteq> {}"  shows "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> {a<..<b} \<inter> {c<..<d} = {}"

  5729   unfolding closure_open_interval[OF assms, THEN sym] unfolding open_inter_closure_eq_empty[OF open_interval] ..

  5730

  5731

  5732 (* Some stuff for half-infinite intervals too; FIXME: notation?  *)

  5733

  5734 lemma closed_interval_left: fixes b::"'a::euclidean_space"

  5735   shows "closed {x::'a. \<forall>i\<in>Basis. x\<bullet>i \<le> b\<bullet>i}"

  5736 proof-

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

  5738     fix x::"'a" assume x:"\<forall>e>0. \<exists>x'\<in>{x. \<forall>i\<in>Basis. x \<bullet> i \<le> b \<bullet> i}. x' \<noteq> x \<and> dist x' x < e"

  5739     { assume "x\<bullet>i > b\<bullet>i"

  5740       then obtain y where "y \<bull`