src/HOL/Topological_Spaces.thy
author hoelzl
Fri Jan 08 17:41:04 2016 +0100 (2016-01-08)
changeset 62102 877463945ce9
parent 62101 26c0a70f78a3
child 62123 df65f5c27c15
permissions -rw-r--r--
fix code generation for uniformity: uniformity is a non-computable pure data.
     1 (*  Title:      HOL/Topological_Spaces.thy
     2     Author:     Brian Huffman
     3     Author:     Johannes Hölzl
     4 *)
     5 
     6 section \<open>Topological Spaces\<close>
     7 
     8 theory Topological_Spaces
     9 imports Main Conditionally_Complete_Lattices
    10 begin
    11 
    12 named_theorems continuous_intros "structural introduction rules for continuity"
    13 
    14 subsection \<open>Topological space\<close>
    15 
    16 class "open" =
    17   fixes "open" :: "'a set \<Rightarrow> bool"
    18 
    19 class topological_space = "open" +
    20   assumes open_UNIV [simp, intro]: "open UNIV"
    21   assumes open_Int [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<inter> T)"
    22   assumes open_Union [intro]: "\<forall>S\<in>K. open S \<Longrightarrow> open (\<Union>K)"
    23 begin
    24 
    25 definition
    26   closed :: "'a set \<Rightarrow> bool" where
    27   "closed S \<longleftrightarrow> open (- S)"
    28 
    29 lemma open_empty [continuous_intros, intro, simp]: "open {}"
    30   using open_Union [of "{}"] by simp
    31 
    32 lemma open_Un [continuous_intros, intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<union> T)"
    33   using open_Union [of "{S, T}"] by simp
    34 
    35 lemma open_UN [continuous_intros, intro]: "\<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Union>x\<in>A. B x)"
    36   using open_Union [of "B ` A"] by simp
    37 
    38 lemma open_Inter [continuous_intros, intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. open T \<Longrightarrow> open (\<Inter>S)"
    39   by (induct set: finite) auto
    40 
    41 lemma open_INT [continuous_intros, intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Inter>x\<in>A. B x)"
    42   using open_Inter [of "B ` A"] by simp
    43 
    44 lemma openI:
    45   assumes "\<And>x. x \<in> S \<Longrightarrow> \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S"
    46   shows "open S"
    47 proof -
    48   have "open (\<Union>{T. open T \<and> T \<subseteq> S})" by auto
    49   moreover have "\<Union>{T. open T \<and> T \<subseteq> S} = S" by (auto dest!: assms)
    50   ultimately show "open S" by simp
    51 qed
    52 
    53 lemma closed_empty [continuous_intros, intro, simp]:  "closed {}"
    54   unfolding closed_def by simp
    55 
    56 lemma closed_Un [continuous_intros, intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<union> T)"
    57   unfolding closed_def by auto
    58 
    59 lemma closed_UNIV [continuous_intros, intro, simp]: "closed UNIV"
    60   unfolding closed_def by simp
    61 
    62 lemma closed_Int [continuous_intros, intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<inter> T)"
    63   unfolding closed_def by auto
    64 
    65 lemma closed_INT [continuous_intros, intro]: "\<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Inter>x\<in>A. B x)"
    66   unfolding closed_def by auto
    67 
    68 lemma closed_Inter [continuous_intros, intro]: "\<forall>S\<in>K. closed S \<Longrightarrow> closed (\<Inter>K)"
    69   unfolding closed_def uminus_Inf by auto
    70 
    71 lemma closed_Union [continuous_intros, intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. closed T \<Longrightarrow> closed (\<Union>S)"
    72   by (induct set: finite) auto
    73 
    74 lemma closed_UN [continuous_intros, intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Union>x\<in>A. B x)"
    75   using closed_Union [of "B ` A"] by simp
    76 
    77 lemma open_closed: "open S \<longleftrightarrow> closed (- S)"
    78   unfolding closed_def by simp
    79 
    80 lemma closed_open: "closed S \<longleftrightarrow> open (- S)"
    81   unfolding closed_def by simp
    82 
    83 lemma open_Diff [continuous_intros, intro]: "open S \<Longrightarrow> closed T \<Longrightarrow> open (S - T)"
    84   unfolding closed_open Diff_eq by (rule open_Int)
    85 
    86 lemma closed_Diff [continuous_intros, intro]: "closed S \<Longrightarrow> open T \<Longrightarrow> closed (S - T)"
    87   unfolding open_closed Diff_eq by (rule closed_Int)
    88 
    89 lemma open_Compl [continuous_intros, intro]: "closed S \<Longrightarrow> open (- S)"
    90   unfolding closed_open .
    91 
    92 lemma closed_Compl [continuous_intros, intro]: "open S \<Longrightarrow> closed (- S)"
    93   unfolding open_closed .
    94 
    95 lemma open_Collect_neg: "closed {x. P x} \<Longrightarrow> open {x. \<not> P x}"
    96   unfolding Collect_neg_eq by (rule open_Compl)
    97 
    98 lemma open_Collect_conj: assumes "open {x. P x}" "open {x. Q x}" shows "open {x. P x \<and> Q x}"
    99   using open_Int[OF assms] by (simp add: Int_def)
   100 
   101 lemma open_Collect_disj: assumes "open {x. P x}" "open {x. Q x}" shows "open {x. P x \<or> Q x}"
   102   using open_Un[OF assms] by (simp add: Un_def)
   103 
   104 lemma open_Collect_ex: "(\<And>i. open {x. P i x}) \<Longrightarrow> open {x. \<exists>i. P i x}"
   105   using open_UN[of UNIV "\<lambda>i. {x. P i x}"] unfolding Collect_ex_eq by simp
   106 
   107 lemma open_Collect_imp: "closed {x. P x} \<Longrightarrow> open {x. Q x} \<Longrightarrow> open {x. P x \<longrightarrow> Q x}"
   108   unfolding imp_conv_disj by (intro open_Collect_disj open_Collect_neg)
   109 
   110 lemma open_Collect_const: "open {x. P}"
   111   by (cases P) auto
   112 
   113 lemma closed_Collect_neg: "open {x. P x} \<Longrightarrow> closed {x. \<not> P x}"
   114   unfolding Collect_neg_eq by (rule closed_Compl)
   115 
   116 lemma closed_Collect_conj: assumes "closed {x. P x}" "closed {x. Q x}" shows "closed {x. P x \<and> Q x}"
   117   using closed_Int[OF assms] by (simp add: Int_def)
   118 
   119 lemma closed_Collect_disj: assumes "closed {x. P x}" "closed {x. Q x}" shows "closed {x. P x \<or> Q x}"
   120   using closed_Un[OF assms] by (simp add: Un_def)
   121 
   122 lemma closed_Collect_all: "(\<And>i. closed {x. P i x}) \<Longrightarrow> closed {x. \<forall>i. P i x}"
   123   using closed_INT[of UNIV "\<lambda>i. {x. P i x}"] unfolding Collect_all_eq by simp
   124 
   125 lemma closed_Collect_imp: "open {x. P x} \<Longrightarrow> closed {x. Q x} \<Longrightarrow> closed {x. P x \<longrightarrow> Q x}"
   126   unfolding imp_conv_disj by (intro closed_Collect_disj closed_Collect_neg)
   127 
   128 lemma closed_Collect_const: "closed {x. P}"
   129   by (cases P) auto
   130 
   131 end
   132 
   133 subsection\<open>Hausdorff and other separation properties\<close>
   134 
   135 class t0_space = topological_space +
   136   assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
   137 
   138 class t1_space = topological_space +
   139   assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
   140 
   141 instance t1_space \<subseteq> t0_space
   142 proof qed (fast dest: t1_space)
   143 
   144 lemma separation_t1:
   145   fixes x y :: "'a::t1_space"
   146   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U)"
   147   using t1_space[of x y] by blast
   148 
   149 lemma closed_singleton:
   150   fixes a :: "'a::t1_space"
   151   shows "closed {a}"
   152 proof -
   153   let ?T = "\<Union>{S. open S \<and> a \<notin> S}"
   154   have "open ?T" by (simp add: open_Union)
   155   also have "?T = - {a}"
   156     by (simp add: set_eq_iff separation_t1, auto)
   157   finally show "closed {a}" unfolding closed_def .
   158 qed
   159 
   160 lemma closed_insert [continuous_intros, simp]:
   161   fixes a :: "'a::t1_space"
   162   assumes "closed S" shows "closed (insert a S)"
   163 proof -
   164   from closed_singleton assms
   165   have "closed ({a} \<union> S)" by (rule closed_Un)
   166   thus "closed (insert a S)" by simp
   167 qed
   168 
   169 lemma finite_imp_closed:
   170   fixes S :: "'a::t1_space set"
   171   shows "finite S \<Longrightarrow> closed S"
   172 by (induct set: finite, simp_all)
   173 
   174 text \<open>T2 spaces are also known as Hausdorff spaces.\<close>
   175 
   176 class t2_space = topological_space +
   177   assumes hausdorff: "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
   178 
   179 instance t2_space \<subseteq> t1_space
   180 proof qed (fast dest: hausdorff)
   181 
   182 lemma separation_t2:
   183   fixes x y :: "'a::t2_space"
   184   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
   185   using hausdorff[of x y] by blast
   186 
   187 lemma separation_t0:
   188   fixes x y :: "'a::t0_space"
   189   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> ~(x\<in>U \<longleftrightarrow> y\<in>U))"
   190   using t0_space[of x y] by blast
   191 
   192 text \<open>A perfect space is a topological space with no isolated points.\<close>
   193 
   194 class perfect_space = topological_space +
   195   assumes not_open_singleton: "\<not> open {x}"
   196 
   197 
   198 subsection \<open>Generators for toplogies\<close>
   199 
   200 inductive generate_topology for S where
   201   UNIV: "generate_topology S UNIV"
   202 | Int: "generate_topology S a \<Longrightarrow> generate_topology S b \<Longrightarrow> generate_topology S (a \<inter> b)"
   203 | UN: "(\<And>k. k \<in> K \<Longrightarrow> generate_topology S k) \<Longrightarrow> generate_topology S (\<Union>K)"
   204 | Basis: "s \<in> S \<Longrightarrow> generate_topology S s"
   205 
   206 hide_fact (open) UNIV Int UN Basis
   207 
   208 lemma generate_topology_Union:
   209   "(\<And>k. k \<in> I \<Longrightarrow> generate_topology S (K k)) \<Longrightarrow> generate_topology S (\<Union>k\<in>I. K k)"
   210   using generate_topology.UN [of "K ` I"] by auto
   211 
   212 lemma topological_space_generate_topology:
   213   "class.topological_space (generate_topology S)"
   214   by standard (auto intro: generate_topology.intros)
   215 
   216 subsection \<open>Order topologies\<close>
   217 
   218 class order_topology = order + "open" +
   219   assumes open_generated_order: "open = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
   220 begin
   221 
   222 subclass topological_space
   223   unfolding open_generated_order
   224   by (rule topological_space_generate_topology)
   225 
   226 lemma open_greaterThan [continuous_intros, simp]: "open {a <..}"
   227   unfolding open_generated_order by (auto intro: generate_topology.Basis)
   228 
   229 lemma open_lessThan [continuous_intros, simp]: "open {..< a}"
   230   unfolding open_generated_order by (auto intro: generate_topology.Basis)
   231 
   232 lemma open_greaterThanLessThan [continuous_intros, simp]: "open {a <..< b}"
   233    unfolding greaterThanLessThan_eq by (simp add: open_Int)
   234 
   235 end
   236 
   237 class linorder_topology = linorder + order_topology
   238 
   239 lemma closed_atMost [continuous_intros, simp]: "closed {.. a::'a::linorder_topology}"
   240   by (simp add: closed_open)
   241 
   242 lemma closed_atLeast [continuous_intros, simp]: "closed {a::'a::linorder_topology ..}"
   243   by (simp add: closed_open)
   244 
   245 lemma closed_atLeastAtMost [continuous_intros, simp]: "closed {a::'a::linorder_topology .. b}"
   246 proof -
   247   have "{a .. b} = {a ..} \<inter> {.. b}"
   248     by auto
   249   then show ?thesis
   250     by (simp add: closed_Int)
   251 qed
   252 
   253 lemma (in linorder) less_separate:
   254   assumes "x < y"
   255   shows "\<exists>a b. x \<in> {..< a} \<and> y \<in> {b <..} \<and> {..< a} \<inter> {b <..} = {}"
   256 proof (cases "\<exists>z. x < z \<and> z < y")
   257   case True
   258   then obtain z where "x < z \<and> z < y" ..
   259   then have "x \<in> {..< z} \<and> y \<in> {z <..} \<and> {z <..} \<inter> {..< z} = {}"
   260     by auto
   261   then show ?thesis by blast
   262 next
   263   case False
   264   with \<open>x < y\<close> have "x \<in> {..< y} \<and> y \<in> {x <..} \<and> {x <..} \<inter> {..< y} = {}"
   265     by auto
   266   then show ?thesis by blast
   267 qed
   268 
   269 instance linorder_topology \<subseteq> t2_space
   270 proof
   271   fix x y :: 'a
   272   from less_separate[of x y] less_separate[of y x]
   273   show "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
   274     by (elim neqE) (metis open_lessThan open_greaterThan Int_commute)+
   275 qed
   276 
   277 lemma (in linorder_topology) open_right:
   278   assumes "open S" "x \<in> S" and gt_ex: "x < y" shows "\<exists>b>x. {x ..< b} \<subseteq> S"
   279   using assms unfolding open_generated_order
   280 proof induction
   281   case (Int A B)
   282   then obtain a b where "a > x" "{x ..< a} \<subseteq> A"  "b > x" "{x ..< b} \<subseteq> B" by auto
   283   then show ?case by (auto intro!: exI[of _ "min a b"])
   284 next
   285   case (Basis S) then show ?case by (fastforce intro: exI[of _ y] gt_ex)
   286 qed blast+
   287 
   288 lemma (in linorder_topology) open_left:
   289   assumes "open S" "x \<in> S" and lt_ex: "y < x" shows "\<exists>b<x. {b <.. x} \<subseteq> S"
   290   using assms unfolding open_generated_order
   291 proof induction
   292   case (Int A B)
   293   then obtain a b where "a < x" "{a <.. x} \<subseteq> A"  "b < x" "{b <.. x} \<subseteq> B" by auto
   294   then show ?case by (auto intro!: exI[of _ "max a b"])
   295 next
   296   case (Basis S) then show ?case by (fastforce intro: exI[of _ y] lt_ex)
   297 qed blast+
   298 
   299 subsubsection \<open>Boolean is an order topology\<close>
   300 
   301 text \<open>It also is a discrete topology, but don't have a type class for it (yet).\<close>
   302 
   303 instantiation bool :: order_topology
   304 begin
   305 
   306 definition open_bool :: "bool set \<Rightarrow> bool" where
   307   "open_bool = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
   308 
   309 instance
   310   proof qed (rule open_bool_def)
   311 
   312 end
   313 
   314 lemma open_bool[simp, intro!]: "open (A::bool set)"
   315 proof -
   316   have *: "{False <..} = {True}" "{..< True} = {False}"
   317     by auto
   318   have "A = UNIV \<or> A = {} \<or> A = {False <..} \<or> A = {..< True}"
   319     using subset_UNIV[of A] unfolding UNIV_bool * by auto
   320   then show "open A"
   321     by auto
   322 qed
   323 
   324 subsubsection \<open>Topological filters\<close>
   325 
   326 definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
   327   where "nhds a = (INF S:{S. open S \<and> a \<in> S}. principal S)"
   328 
   329 definition (in topological_space) at_within :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a filter" ("at (_) within (_)" [1000, 60] 60)
   330   where "at a within s = inf (nhds a) (principal (s - {a}))"
   331 
   332 abbreviation (in topological_space) at :: "'a \<Rightarrow> 'a filter" ("at") where
   333   "at x \<equiv> at x within (CONST UNIV)"
   334 
   335 abbreviation (in order_topology) at_right :: "'a \<Rightarrow> 'a filter" where
   336   "at_right x \<equiv> at x within {x <..}"
   337 
   338 abbreviation (in order_topology) at_left :: "'a \<Rightarrow> 'a filter" where
   339   "at_left x \<equiv> at x within {..< x}"
   340 
   341 lemma (in topological_space) nhds_generated_topology:
   342   "open = generate_topology T \<Longrightarrow> nhds x = (INF S:{S\<in>T. x \<in> S}. principal S)"
   343   unfolding nhds_def
   344 proof (safe intro!: antisym INF_greatest)
   345   fix S assume "generate_topology T S" "x \<in> S"
   346   then show "(INF S:{S \<in> T. x \<in> S}. principal S) \<le> principal S"
   347     by induction
   348        (auto intro: INF_lower order_trans simp add: inf_principal[symmetric] simp del: inf_principal)
   349 qed (auto intro!: INF_lower intro: generate_topology.intros)
   350 
   351 lemma (in topological_space) eventually_nhds:
   352   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   353   unfolding nhds_def by (subst eventually_INF_base) (auto simp: eventually_principal)
   354 
   355 lemma (in topological_space) eventually_nhds_in_open:
   356   "open s \<Longrightarrow> x \<in> s \<Longrightarrow> eventually (\<lambda>y. y \<in> s) (nhds x)"
   357   by (subst eventually_nhds) blast
   358 
   359 lemma nhds_neq_bot [simp]: "nhds a \<noteq> bot"
   360   unfolding trivial_limit_def eventually_nhds by simp
   361 
   362 lemma (in t1_space) t1_space_nhds:
   363   "x \<noteq> y \<Longrightarrow> (\<forall>\<^sub>F x in nhds x. x \<noteq> y)"
   364   by (drule t1_space) (auto simp: eventually_nhds)
   365 
   366 lemma at_within_eq: "at x within s = (INF S:{S. open S \<and> x \<in> S}. principal (S \<inter> s - {x}))"
   367   unfolding nhds_def at_within_def by (subst INF_inf_const2[symmetric]) (auto simp add: Diff_Int_distrib)
   368 
   369 lemma eventually_at_filter:
   370   "eventually P (at a within s) \<longleftrightarrow> eventually (\<lambda>x. x \<noteq> a \<longrightarrow> x \<in> s \<longrightarrow> P x) (nhds a)"
   371   unfolding at_within_def eventually_inf_principal by (simp add: imp_conjL[symmetric] conj_commute)
   372 
   373 lemma at_le: "s \<subseteq> t \<Longrightarrow> at x within s \<le> at x within t"
   374   unfolding at_within_def by (intro inf_mono) auto
   375 
   376 lemma eventually_at_topological:
   377   "eventually P (at a within s) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> x \<in> s \<longrightarrow> P x))"
   378   unfolding eventually_nhds eventually_at_filter by simp
   379 
   380 lemma at_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> at a within S = at a"
   381   unfolding filter_eq_iff eventually_at_topological by (metis open_Int Int_iff UNIV_I)
   382 
   383 lemma at_within_open_NO_MATCH:
   384   "a \<in> s \<Longrightarrow> open s \<Longrightarrow> NO_MATCH UNIV s \<Longrightarrow> at a within s = at a"
   385   by (simp only: at_within_open)
   386 
   387 lemma at_within_nhd:
   388   assumes "x \<in> S" "open S" "T \<inter> S - {x} = U \<inter> S - {x}"
   389   shows "at x within T = at x within U"
   390   unfolding filter_eq_iff eventually_at_filter
   391 proof (intro allI eventually_subst)
   392   have "eventually (\<lambda>x. x \<in> S) (nhds x)"
   393     using \<open>x \<in> S\<close> \<open>open S\<close> by (auto simp: eventually_nhds)
   394   then show "\<forall>\<^sub>F n in nhds x. (n \<noteq> x \<longrightarrow> n \<in> T \<longrightarrow> P n) = (n \<noteq> x \<longrightarrow> n \<in> U \<longrightarrow> P n)" for P
   395     by eventually_elim (insert \<open>T \<inter> S - {x} = U \<inter> S - {x}\<close>, blast)
   396 qed
   397 
   398 lemma at_within_empty [simp]: "at a within {} = bot"
   399   unfolding at_within_def by simp
   400 
   401 lemma at_within_union: "at x within (S \<union> T) = sup (at x within S) (at x within T)"
   402   unfolding filter_eq_iff eventually_sup eventually_at_filter
   403   by (auto elim!: eventually_rev_mp)
   404 
   405 lemma at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
   406   unfolding trivial_limit_def eventually_at_topological
   407   by (safe, case_tac "S = {a}", simp, fast, fast)
   408 
   409 lemma at_neq_bot [simp]: "at (a::'a::perfect_space) \<noteq> bot"
   410   by (simp add: at_eq_bot_iff not_open_singleton)
   411 
   412 lemma (in order_topology) nhds_order: "nhds x =
   413   inf (INF a:{x <..}. principal {..< a}) (INF a:{..< x}. principal {a <..})"
   414 proof -
   415   have 1: "{S \<in> range lessThan \<union> range greaterThan. x \<in> S} =
   416       (\<lambda>a. {..< a}) ` {x <..} \<union> (\<lambda>a. {a <..}) ` {..< x}"
   417     by auto
   418   show ?thesis
   419     unfolding nhds_generated_topology[OF open_generated_order] INF_union 1 INF_image comp_def ..
   420 qed
   421 
   422 lemma (in linorder_topology) at_within_order: "UNIV \<noteq> {x} \<Longrightarrow>
   423   at x within s = inf (INF a:{x <..}. principal ({..< a} \<inter> s - {x}))
   424                       (INF a:{..< x}. principal ({a <..} \<inter> s - {x}))"
   425 proof (cases "{x <..} = {}" "{..< x} = {}" rule: case_split[case_product case_split])
   426   assume "UNIV \<noteq> {x}" "{x<..} = {}" "{..< x} = {}"
   427   moreover have "UNIV = {..< x} \<union> {x} \<union> {x <..}"
   428     by auto
   429   ultimately show ?thesis
   430     by auto
   431 qed (auto simp: at_within_def nhds_order Int_Diff inf_principal[symmetric] INF_inf_const2
   432                 inf_sup_aci[where 'a="'a filter"]
   433           simp del: inf_principal)
   434 
   435 lemma (in linorder_topology) at_left_eq:
   436   "y < x \<Longrightarrow> at_left x = (INF a:{..< x}. principal {a <..< x})"
   437   by (subst at_within_order)
   438      (auto simp: greaterThan_Int_greaterThan greaterThanLessThan_eq[symmetric] min.absorb2 INF_constant
   439            intro!: INF_lower2 inf_absorb2)
   440 
   441 lemma (in linorder_topology) eventually_at_left:
   442   "y < x \<Longrightarrow> eventually P (at_left x) \<longleftrightarrow> (\<exists>b<x. \<forall>y>b. y < x \<longrightarrow> P y)"
   443   unfolding at_left_eq by (subst eventually_INF_base) (auto simp: eventually_principal Ball_def)
   444 
   445 lemma (in linorder_topology) at_right_eq:
   446   "x < y \<Longrightarrow> at_right x = (INF a:{x <..}. principal {x <..< a})"
   447   by (subst at_within_order)
   448      (auto simp: lessThan_Int_lessThan greaterThanLessThan_eq[symmetric] max.absorb2 INF_constant Int_commute
   449            intro!: INF_lower2 inf_absorb1)
   450 
   451 lemma (in linorder_topology) eventually_at_right:
   452   "x < y \<Longrightarrow> eventually P (at_right x) \<longleftrightarrow> (\<exists>b>x. \<forall>y>x. y < b \<longrightarrow> P y)"
   453   unfolding at_right_eq by (subst eventually_INF_base) (auto simp: eventually_principal Ball_def)
   454 
   455 lemma eventually_at_right_less: "\<forall>\<^sub>F y in at_right (x::'a::{linorder_topology, no_top}). x < y"
   456   using gt_ex[of x] eventually_at_right[of x] by auto
   457 
   458 lemma trivial_limit_at_right_top: "at_right (top::_::{order_top, linorder_topology}) = bot"
   459   unfolding filter_eq_iff eventually_at_topological by auto
   460 
   461 lemma trivial_limit_at_left_bot: "at_left (bot::_::{order_bot, linorder_topology}) = bot"
   462   unfolding filter_eq_iff eventually_at_topological by auto
   463 
   464 lemma trivial_limit_at_left_real [simp]:
   465   "\<not> trivial_limit (at_left (x::'a::{no_bot, dense_order, linorder_topology}))"
   466   using lt_ex[of x]
   467   by safe (auto simp add: trivial_limit_def eventually_at_left dest: dense)
   468 
   469 lemma trivial_limit_at_right_real [simp]:
   470   "\<not> trivial_limit (at_right (x::'a::{no_top, dense_order, linorder_topology}))"
   471   using gt_ex[of x]
   472   by safe (auto simp add: trivial_limit_def eventually_at_right dest: dense)
   473 
   474 lemma at_eq_sup_left_right: "at (x::'a::linorder_topology) = sup (at_left x) (at_right x)"
   475   by (auto simp: eventually_at_filter filter_eq_iff eventually_sup
   476            elim: eventually_elim2 eventually_mono)
   477 
   478 lemma eventually_at_split:
   479   "eventually P (at (x::'a::linorder_topology)) \<longleftrightarrow> eventually P (at_left x) \<and> eventually P (at_right x)"
   480   by (subst at_eq_sup_left_right) (simp add: eventually_sup)
   481 
   482 subsubsection \<open>Tendsto\<close>
   483 
   484 abbreviation (in topological_space)
   485   tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool" (infixr "\<longlongrightarrow>" 55) where
   486   "(f \<longlongrightarrow> l) F \<equiv> filterlim f (nhds l) F"
   487 
   488 definition (in t2_space) Lim :: "'f filter \<Rightarrow> ('f \<Rightarrow> 'a) \<Rightarrow> 'a" where
   489   "Lim A f = (THE l. (f \<longlongrightarrow> l) A)"
   490 
   491 lemma tendsto_eq_rhs: "(f \<longlongrightarrow> x) F \<Longrightarrow> x = y \<Longrightarrow> (f \<longlongrightarrow> y) F"
   492   by simp
   493 
   494 named_theorems tendsto_intros "introduction rules for tendsto"
   495 setup \<open>
   496   Global_Theory.add_thms_dynamic (@{binding tendsto_eq_intros},
   497     fn context =>
   498       Named_Theorems.get (Context.proof_of context) @{named_theorems tendsto_intros}
   499       |> map_filter (try (fn thm => @{thm tendsto_eq_rhs} OF [thm])))
   500 \<close>
   501 
   502 lemma (in topological_space) tendsto_def:
   503    "(f \<longlongrightarrow> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
   504    unfolding nhds_def filterlim_INF filterlim_principal by auto
   505 
   506 lemma tendsto_cong:
   507   assumes "eventually (\<lambda>x. f x = g x) F"
   508   shows   "(f \<longlongrightarrow> c) F \<longleftrightarrow> (g \<longlongrightarrow> c) F"
   509   by (rule filterlim_cong[OF refl refl assms])
   510 
   511 
   512 lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f \<longlongrightarrow> l) F' \<Longrightarrow> (f \<longlongrightarrow> l) F"
   513   unfolding tendsto_def le_filter_def by fast
   514 
   515 lemma tendsto_within_subset: "(f \<longlongrightarrow> l) (at x within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f \<longlongrightarrow> l) (at x within T)"
   516   by (blast intro: tendsto_mono at_le)
   517 
   518 lemma filterlim_at:
   519   "(LIM x F. f x :> at b within s) \<longleftrightarrow> (eventually (\<lambda>x. f x \<in> s \<and> f x \<noteq> b) F \<and> (f \<longlongrightarrow> b) F)"
   520   by (simp add: at_within_def filterlim_inf filterlim_principal conj_commute)
   521 
   522 lemma (in topological_space) topological_tendstoI:
   523   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F) \<Longrightarrow> (f \<longlongrightarrow> l) F"
   524   unfolding tendsto_def by auto
   525 
   526 lemma (in topological_space) topological_tendstoD:
   527   "(f \<longlongrightarrow> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
   528   unfolding tendsto_def by auto
   529 
   530 lemma (in order_topology) order_tendsto_iff:
   531   "(f \<longlongrightarrow> x) F \<longleftrightarrow> (\<forall>l<x. eventually (\<lambda>x. l < f x) F) \<and> (\<forall>u>x. eventually (\<lambda>x. f x < u) F)"
   532   unfolding nhds_order filterlim_inf filterlim_INF filterlim_principal by auto
   533 
   534 lemma (in order_topology) order_tendstoI:
   535   "(\<And>a. a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F) \<Longrightarrow> (\<And>a. y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F) \<Longrightarrow>
   536     (f \<longlongrightarrow> y) F"
   537   unfolding order_tendsto_iff by auto
   538 
   539 lemma (in order_topology) order_tendstoD:
   540   assumes "(f \<longlongrightarrow> y) F"
   541   shows "a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F"
   542     and "y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F"
   543   using assms unfolding order_tendsto_iff by auto
   544 
   545 lemma tendsto_bot [simp]: "(f \<longlongrightarrow> a) bot"
   546   unfolding tendsto_def by simp
   547 
   548 lemma (in linorder_topology) tendsto_max:
   549   assumes X: "(X \<longlongrightarrow> x) net"
   550   assumes Y: "(Y \<longlongrightarrow> y) net"
   551   shows "((\<lambda>x. max (X x) (Y x)) \<longlongrightarrow> max x y) net"
   552 proof (rule order_tendstoI)
   553   fix a assume "a < max x y"
   554   then show "eventually (\<lambda>x. a < max (X x) (Y x)) net"
   555     using order_tendstoD(1)[OF X, of a] order_tendstoD(1)[OF Y, of a]
   556     by (auto simp: less_max_iff_disj elim: eventually_mono)
   557 next
   558   fix a assume "max x y < a"
   559   then show "eventually (\<lambda>x. max (X x) (Y x) < a) net"
   560     using order_tendstoD(2)[OF X, of a] order_tendstoD(2)[OF Y, of a]
   561     by (auto simp: eventually_conj_iff)
   562 qed
   563 
   564 lemma (in linorder_topology) tendsto_min:
   565   assumes X: "(X \<longlongrightarrow> x) net"
   566   assumes Y: "(Y \<longlongrightarrow> y) net"
   567   shows "((\<lambda>x. min (X x) (Y x)) \<longlongrightarrow> min x y) net"
   568 proof (rule order_tendstoI)
   569   fix a assume "a < min x y"
   570   then show "eventually (\<lambda>x. a < min (X x) (Y x)) net"
   571     using order_tendstoD(1)[OF X, of a] order_tendstoD(1)[OF Y, of a]
   572     by (auto simp: eventually_conj_iff)
   573 next
   574   fix a assume "min x y < a"
   575   then show "eventually (\<lambda>x. min (X x) (Y x) < a) net"
   576     using order_tendstoD(2)[OF X, of a] order_tendstoD(2)[OF Y, of a]
   577     by (auto simp: min_less_iff_disj elim: eventually_mono)
   578 qed
   579 
   580 lemma tendsto_ident_at [tendsto_intros, simp, intro]: "((\<lambda>x. x) \<longlongrightarrow> a) (at a within s)"
   581   unfolding tendsto_def eventually_at_topological by auto
   582 
   583 lemma (in topological_space) tendsto_const [tendsto_intros, simp, intro]: "((\<lambda>x. k) \<longlongrightarrow> k) F"
   584   by (simp add: tendsto_def)
   585 
   586 lemma (in t2_space) tendsto_unique:
   587   assumes "F \<noteq> bot" and "(f \<longlongrightarrow> a) F" and "(f \<longlongrightarrow> b) F"
   588   shows "a = b"
   589 proof (rule ccontr)
   590   assume "a \<noteq> b"
   591   obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
   592     using hausdorff [OF \<open>a \<noteq> b\<close>] by fast
   593   have "eventually (\<lambda>x. f x \<in> U) F"
   594     using \<open>(f \<longlongrightarrow> a) F\<close> \<open>open U\<close> \<open>a \<in> U\<close> by (rule topological_tendstoD)
   595   moreover
   596   have "eventually (\<lambda>x. f x \<in> V) F"
   597     using \<open>(f \<longlongrightarrow> b) F\<close> \<open>open V\<close> \<open>b \<in> V\<close> by (rule topological_tendstoD)
   598   ultimately
   599   have "eventually (\<lambda>x. False) F"
   600   proof eventually_elim
   601     case (elim x)
   602     hence "f x \<in> U \<inter> V" by simp
   603     with \<open>U \<inter> V = {}\<close> show ?case by simp
   604   qed
   605   with \<open>\<not> trivial_limit F\<close> show "False"
   606     by (simp add: trivial_limit_def)
   607 qed
   608 
   609 lemma (in t2_space) tendsto_const_iff:
   610   assumes "\<not> trivial_limit F" shows "((\<lambda>x. a :: 'a) \<longlongrightarrow> b) F \<longleftrightarrow> a = b"
   611   by (auto intro!: tendsto_unique [OF assms tendsto_const])
   612 
   613 lemma increasing_tendsto:
   614   fixes f :: "_ \<Rightarrow> 'a::order_topology"
   615   assumes bdd: "eventually (\<lambda>n. f n \<le> l) F"
   616       and en: "\<And>x. x < l \<Longrightarrow> eventually (\<lambda>n. x < f n) F"
   617   shows "(f \<longlongrightarrow> l) F"
   618   using assms by (intro order_tendstoI) (auto elim!: eventually_mono)
   619 
   620 lemma decreasing_tendsto:
   621   fixes f :: "_ \<Rightarrow> 'a::order_topology"
   622   assumes bdd: "eventually (\<lambda>n. l \<le> f n) F"
   623       and en: "\<And>x. l < x \<Longrightarrow> eventually (\<lambda>n. f n < x) F"
   624   shows "(f \<longlongrightarrow> l) F"
   625   using assms by (intro order_tendstoI) (auto elim!: eventually_mono)
   626 
   627 lemma tendsto_sandwich:
   628   fixes f g h :: "'a \<Rightarrow> 'b::order_topology"
   629   assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
   630   assumes lim: "(f \<longlongrightarrow> c) net" "(h \<longlongrightarrow> c) net"
   631   shows "(g \<longlongrightarrow> c) net"
   632 proof (rule order_tendstoI)
   633   fix a show "a < c \<Longrightarrow> eventually (\<lambda>x. a < g x) net"
   634     using order_tendstoD[OF lim(1), of a] ev by (auto elim: eventually_elim2)
   635 next
   636   fix a show "c < a \<Longrightarrow> eventually (\<lambda>x. g x < a) net"
   637     using order_tendstoD[OF lim(2), of a] ev by (auto elim: eventually_elim2)
   638 qed
   639 
   640 lemma limit_frequently_eq:
   641   assumes "F \<noteq> bot"
   642   assumes "frequently (\<lambda>x. f x = c) F"
   643   assumes "(f \<longlongrightarrow> d) F"
   644   shows   "d = (c :: 'a :: t1_space)"
   645 proof (rule ccontr)
   646   assume "d \<noteq> c"
   647   from t1_space[OF this] obtain U where "open U" "d \<in> U" "c \<notin> U" by blast
   648   from this assms have "eventually (\<lambda>x. f x \<in> U) F" unfolding tendsto_def by blast
   649   hence "eventually (\<lambda>x. f x \<noteq> c) F" by eventually_elim (insert \<open>c \<notin> U\<close>, blast)
   650   with assms(2) show False unfolding frequently_def by contradiction
   651 qed
   652 
   653 lemma tendsto_imp_eventually_ne:
   654   assumes "F \<noteq> bot" "(f \<longlongrightarrow> c) F" "c \<noteq> (c' :: 'a :: t1_space)"
   655   shows   "eventually (\<lambda>z. f z \<noteq> c') F"
   656 proof (rule ccontr)
   657   assume "\<not>eventually (\<lambda>z. f z \<noteq> c') F"
   658   hence "frequently (\<lambda>z. f z = c') F" by (simp add: frequently_def)
   659   from limit_frequently_eq[OF assms(1) this assms(2)] and assms(3) show False by contradiction
   660 qed
   661 
   662 lemma tendsto_le:
   663   fixes f g :: "'a \<Rightarrow> 'b::linorder_topology"
   664   assumes F: "\<not> trivial_limit F"
   665   assumes x: "(f \<longlongrightarrow> x) F" and y: "(g \<longlongrightarrow> y) F"
   666   assumes ev: "eventually (\<lambda>x. g x \<le> f x) F"
   667   shows "y \<le> x"
   668 proof (rule ccontr)
   669   assume "\<not> y \<le> x"
   670   with less_separate[of x y] obtain a b where xy: "x < a" "b < y" "{..<a} \<inter> {b<..} = {}"
   671     by (auto simp: not_le)
   672   then have "eventually (\<lambda>x. f x < a) F" "eventually (\<lambda>x. b < g x) F"
   673     using x y by (auto intro: order_tendstoD)
   674   with ev have "eventually (\<lambda>x. False) F"
   675     by eventually_elim (insert xy, fastforce)
   676   with F show False
   677     by (simp add: eventually_False)
   678 qed
   679 
   680 lemma tendsto_le_const:
   681   fixes f :: "'a \<Rightarrow> 'b::linorder_topology"
   682   assumes F: "\<not> trivial_limit F"
   683   assumes x: "(f \<longlongrightarrow> x) F" and a: "eventually (\<lambda>i. a \<le> f i) F"
   684   shows "a \<le> x"
   685   using F x tendsto_const a by (rule tendsto_le)
   686 
   687 lemma tendsto_ge_const:
   688   fixes f :: "'a \<Rightarrow> 'b::linorder_topology"
   689   assumes F: "\<not> trivial_limit F"
   690   assumes x: "(f \<longlongrightarrow> x) F" and a: "eventually (\<lambda>i. a \<ge> f i) F"
   691   shows "a \<ge> x"
   692   by (rule tendsto_le [OF F tendsto_const x a])
   693 
   694 
   695 subsubsection \<open>Rules about @{const Lim}\<close>
   696 
   697 lemma tendsto_Lim:
   698   "\<not>(trivial_limit net) \<Longrightarrow> (f \<longlongrightarrow> l) net \<Longrightarrow> Lim net f = l"
   699   unfolding Lim_def using tendsto_unique[of net f] by auto
   700 
   701 lemma Lim_ident_at: "\<not> trivial_limit (at x within s) \<Longrightarrow> Lim (at x within s) (\<lambda>x. x) = x"
   702   by (rule tendsto_Lim[OF _ tendsto_ident_at]) auto
   703 
   704 lemma filterlim_at_bot_at_right:
   705   fixes f :: "'a::linorder_topology \<Rightarrow> 'b::linorder"
   706   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   707   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
   708   assumes Q: "eventually Q (at_right a)" and bound: "\<And>b. Q b \<Longrightarrow> a < b"
   709   assumes P: "eventually P at_bot"
   710   shows "filterlim f at_bot (at_right a)"
   711 proof -
   712   from P obtain x where x: "\<And>y. y \<le> x \<Longrightarrow> P y"
   713     unfolding eventually_at_bot_linorder by auto
   714   show ?thesis
   715   proof (intro filterlim_at_bot_le[THEN iffD2] allI impI)
   716     fix z assume "z \<le> x"
   717     with x have "P z" by auto
   718     have "eventually (\<lambda>x. x \<le> g z) (at_right a)"
   719       using bound[OF bij(2)[OF \<open>P z\<close>]]
   720       unfolding eventually_at_right[OF bound[OF bij(2)[OF \<open>P z\<close>]]] by (auto intro!: exI[of _ "g z"])
   721     with Q show "eventually (\<lambda>x. f x \<le> z) (at_right a)"
   722       by eventually_elim (metis bij \<open>P z\<close> mono)
   723   qed
   724 qed
   725 
   726 lemma filterlim_at_top_at_left:
   727   fixes f :: "'a::linorder_topology \<Rightarrow> 'b::linorder"
   728   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   729   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
   730   assumes Q: "eventually Q (at_left a)" and bound: "\<And>b. Q b \<Longrightarrow> b < a"
   731   assumes P: "eventually P at_top"
   732   shows "filterlim f at_top (at_left a)"
   733 proof -
   734   from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
   735     unfolding eventually_at_top_linorder by auto
   736   show ?thesis
   737   proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
   738     fix z assume "x \<le> z"
   739     with x have "P z" by auto
   740     have "eventually (\<lambda>x. g z \<le> x) (at_left a)"
   741       using bound[OF bij(2)[OF \<open>P z\<close>]]
   742       unfolding eventually_at_left[OF bound[OF bij(2)[OF \<open>P z\<close>]]] by (auto intro!: exI[of _ "g z"])
   743     with Q show "eventually (\<lambda>x. z \<le> f x) (at_left a)"
   744       by eventually_elim (metis bij \<open>P z\<close> mono)
   745   qed
   746 qed
   747 
   748 lemma filterlim_split_at:
   749   "filterlim f F (at_left x) \<Longrightarrow> filterlim f F (at_right x) \<Longrightarrow> filterlim f F (at (x::'a::linorder_topology))"
   750   by (subst at_eq_sup_left_right) (rule filterlim_sup)
   751 
   752 lemma filterlim_at_split:
   753   "filterlim f F (at (x::'a::linorder_topology)) \<longleftrightarrow> filterlim f F (at_left x) \<and> filterlim f F (at_right x)"
   754   by (subst at_eq_sup_left_right) (simp add: filterlim_def filtermap_sup)
   755 
   756 lemma eventually_nhds_top:
   757   fixes P :: "'a :: {order_top, linorder_topology} \<Rightarrow> bool"
   758   assumes "(b::'a) < top"
   759   shows "eventually P (nhds top) \<longleftrightarrow> (\<exists>b<top. (\<forall>z. b < z \<longrightarrow> P z))"
   760   unfolding eventually_nhds
   761 proof safe
   762   fix S :: "'a set" assume "open S" "top \<in> S"
   763   note open_left[OF this \<open>b < top\<close>]
   764   moreover assume "\<forall>s\<in>S. P s"
   765   ultimately show "\<exists>b<top. \<forall>z>b. P z"
   766     by (auto simp: subset_eq Ball_def)
   767 next
   768   fix b assume "b < top" "\<forall>z>b. P z"
   769   then show "\<exists>S. open S \<and> top \<in> S \<and> (\<forall>xa\<in>S. P xa)"
   770     by (intro exI[of _ "{b <..}"]) auto
   771 qed
   772 
   773 lemma tendsto_at_within_iff_tendsto_nhds:
   774   "(g \<longlongrightarrow> g l) (at l within S) \<longleftrightarrow> (g \<longlongrightarrow> g l) (inf (nhds l) (principal S))"
   775   unfolding tendsto_def eventually_at_filter eventually_inf_principal
   776   by (intro ext all_cong imp_cong) (auto elim!: eventually_mono)
   777 
   778 subsection \<open>Limits on sequences\<close>
   779 
   780 abbreviation (in topological_space)
   781   LIMSEQ :: "[nat \<Rightarrow> 'a, 'a] \<Rightarrow> bool"
   782     ("((_)/ \<longlonglongrightarrow> (_))" [60, 60] 60) where
   783   "X \<longlonglongrightarrow> L \<equiv> (X \<longlongrightarrow> L) sequentially"
   784 
   785 abbreviation (in t2_space) lim :: "(nat \<Rightarrow> 'a) \<Rightarrow> 'a" where
   786   "lim X \<equiv> Lim sequentially X"
   787 
   788 definition (in topological_space) convergent :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
   789   "convergent X = (\<exists>L. X \<longlonglongrightarrow> L)"
   790 
   791 lemma lim_def: "lim X = (THE L. X \<longlonglongrightarrow> L)"
   792   unfolding Lim_def ..
   793 
   794 subsubsection \<open>Monotone sequences and subsequences\<close>
   795 
   796 definition
   797   monoseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
   798     \<comment>\<open>Definition of monotonicity.
   799         The use of disjunction here complicates proofs considerably.
   800         One alternative is to add a Boolean argument to indicate the direction.
   801         Another is to develop the notions of increasing and decreasing first.\<close>
   802   "monoseq X = ((\<forall>m. \<forall>n\<ge>m. X m \<le> X n) \<or> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m))"
   803 
   804 abbreviation incseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
   805   "incseq X \<equiv> mono X"
   806 
   807 lemma incseq_def: "incseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<ge> X m)"
   808   unfolding mono_def ..
   809 
   810 abbreviation decseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
   811   "decseq X \<equiv> antimono X"
   812 
   813 lemma decseq_def: "decseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m)"
   814   unfolding antimono_def ..
   815 
   816 definition
   817   subseq :: "(nat \<Rightarrow> nat) \<Rightarrow> bool" where
   818     \<comment>\<open>Definition of subsequence\<close>
   819   "subseq f \<longleftrightarrow> (\<forall>m. \<forall>n>m. f m < f n)"
   820 
   821 lemma incseq_SucI:
   822   "(\<And>n. X n \<le> X (Suc n)) \<Longrightarrow> incseq X"
   823   using lift_Suc_mono_le[of X]
   824   by (auto simp: incseq_def)
   825 
   826 lemma incseqD: "\<And>i j. incseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f i \<le> f j"
   827   by (auto simp: incseq_def)
   828 
   829 lemma incseq_SucD: "incseq A \<Longrightarrow> A i \<le> A (Suc i)"
   830   using incseqD[of A i "Suc i"] by auto
   831 
   832 lemma incseq_Suc_iff: "incseq f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
   833   by (auto intro: incseq_SucI dest: incseq_SucD)
   834 
   835 lemma incseq_const[simp, intro]: "incseq (\<lambda>x. k)"
   836   unfolding incseq_def by auto
   837 
   838 lemma decseq_SucI:
   839   "(\<And>n. X (Suc n) \<le> X n) \<Longrightarrow> decseq X"
   840   using order.lift_Suc_mono_le[OF dual_order, of X]
   841   by (auto simp: decseq_def)
   842 
   843 lemma decseqD: "\<And>i j. decseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f j \<le> f i"
   844   by (auto simp: decseq_def)
   845 
   846 lemma decseq_SucD: "decseq A \<Longrightarrow> A (Suc i) \<le> A i"
   847   using decseqD[of A i "Suc i"] by auto
   848 
   849 lemma decseq_Suc_iff: "decseq f \<longleftrightarrow> (\<forall>n. f (Suc n) \<le> f n)"
   850   by (auto intro: decseq_SucI dest: decseq_SucD)
   851 
   852 lemma decseq_const[simp, intro]: "decseq (\<lambda>x. k)"
   853   unfolding decseq_def by auto
   854 
   855 lemma monoseq_iff: "monoseq X \<longleftrightarrow> incseq X \<or> decseq X"
   856   unfolding monoseq_def incseq_def decseq_def ..
   857 
   858 lemma monoseq_Suc:
   859   "monoseq X \<longleftrightarrow> (\<forall>n. X n \<le> X (Suc n)) \<or> (\<forall>n. X (Suc n) \<le> X n)"
   860   unfolding monoseq_iff incseq_Suc_iff decseq_Suc_iff ..
   861 
   862 lemma monoI1: "\<forall>m. \<forall> n \<ge> m. X m \<le> X n ==> monoseq X"
   863 by (simp add: monoseq_def)
   864 
   865 lemma monoI2: "\<forall>m. \<forall> n \<ge> m. X n \<le> X m ==> monoseq X"
   866 by (simp add: monoseq_def)
   867 
   868 lemma mono_SucI1: "\<forall>n. X n \<le> X (Suc n) ==> monoseq X"
   869 by (simp add: monoseq_Suc)
   870 
   871 lemma mono_SucI2: "\<forall>n. X (Suc n) \<le> X n ==> monoseq X"
   872 by (simp add: monoseq_Suc)
   873 
   874 lemma monoseq_minus:
   875   fixes a :: "nat \<Rightarrow> 'a::ordered_ab_group_add"
   876   assumes "monoseq a"
   877   shows "monoseq (\<lambda> n. - a n)"
   878 proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
   879   case True
   880   hence "\<forall> m. \<forall> n \<ge> m. - a n \<le> - a m" by auto
   881   thus ?thesis by (rule monoI2)
   882 next
   883   case False
   884   hence "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" using \<open>monoseq a\<close>[unfolded monoseq_def] by auto
   885   thus ?thesis by (rule monoI1)
   886 qed
   887 
   888 text\<open>Subsequence (alternative definition, (e.g. Hoskins)\<close>
   889 
   890 lemma subseq_Suc_iff: "subseq f = (\<forall>n. (f n) < (f (Suc n)))"
   891 apply (simp add: subseq_def)
   892 apply (auto dest!: less_imp_Suc_add)
   893 apply (induct_tac k)
   894 apply (auto intro: less_trans)
   895 done
   896 
   897 text\<open>for any sequence, there is a monotonic subsequence\<close>
   898 lemma seq_monosub:
   899   fixes s :: "nat => 'a::linorder"
   900   shows "\<exists>f. subseq f \<and> monoseq (\<lambda>n. (s (f n)))"
   901 proof cases
   902   assume "\<forall>n. \<exists>p>n. \<forall>m\<ge>p. s m \<le> s p"
   903   then have "\<exists>f. \<forall>n. (\<forall>m\<ge>f n. s m \<le> s (f n)) \<and> f n < f (Suc n)"
   904     by (intro dependent_nat_choice) (auto simp: conj_commute)
   905   then obtain f where "subseq f" and mono: "\<And>n m. f n \<le> m \<Longrightarrow> s m \<le> s (f n)"
   906     by (auto simp: subseq_Suc_iff)
   907   moreover
   908   then have "incseq f"
   909     unfolding subseq_Suc_iff incseq_Suc_iff by (auto intro: less_imp_le)
   910   then have "monoseq (\<lambda>n. s (f n))"
   911     by (auto simp add: incseq_def intro!: mono monoI2)
   912   ultimately show ?thesis
   913     by auto
   914 next
   915   assume "\<not> (\<forall>n. \<exists>p>n. (\<forall>m\<ge>p. s m \<le> s p))"
   916   then obtain N where N: "\<And>p. p > N \<Longrightarrow> \<exists>m>p. s p < s m" by (force simp: not_le le_less)
   917   have "\<exists>f. \<forall>n. N < f n \<and> f n < f (Suc n) \<and> s (f n) \<le> s (f (Suc n))"
   918   proof (intro dependent_nat_choice)
   919     fix x assume "N < x" with N[of x] show "\<exists>y>N. x < y \<and> s x \<le> s y"
   920       by (auto intro: less_trans)
   921   qed auto
   922   then show ?thesis
   923     by (auto simp: monoseq_iff incseq_Suc_iff subseq_Suc_iff)
   924 qed
   925 
   926 lemma seq_suble: assumes sf: "subseq f" shows "n \<le> f n"
   927 proof(induct n)
   928   case 0 thus ?case by simp
   929 next
   930   case (Suc n)
   931   from sf[unfolded subseq_Suc_iff, rule_format, of n] Suc.hyps
   932   have "n < f (Suc n)" by arith
   933   thus ?case by arith
   934 qed
   935 
   936 lemma eventually_subseq:
   937   "subseq r \<Longrightarrow> eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
   938   unfolding eventually_sequentially by (metis seq_suble le_trans)
   939 
   940 lemma not_eventually_sequentiallyD:
   941   assumes P: "\<not> eventually P sequentially"
   942   shows "\<exists>r. subseq r \<and> (\<forall>n. \<not> P (r n))"
   943 proof -
   944   from P have "\<forall>n. \<exists>m\<ge>n. \<not> P m"
   945     unfolding eventually_sequentially by (simp add: not_less)
   946   then obtain r where "\<And>n. r n \<ge> n" "\<And>n. \<not> P (r n)"
   947     by (auto simp: choice_iff)
   948   then show ?thesis
   949     by (auto intro!: exI[of _ "\<lambda>n. r (((Suc \<circ> r) ^^ Suc n) 0)"]
   950              simp: less_eq_Suc_le subseq_Suc_iff)
   951 qed
   952 
   953 lemma filterlim_subseq: "subseq f \<Longrightarrow> filterlim f sequentially sequentially"
   954   unfolding filterlim_iff by (metis eventually_subseq)
   955 
   956 lemma subseq_o: "subseq r \<Longrightarrow> subseq s \<Longrightarrow> subseq (r \<circ> s)"
   957   unfolding subseq_def by simp
   958 
   959 lemma subseq_mono: assumes "subseq r" "m < n" shows "r m < r n"
   960   using assms by (auto simp: subseq_def)
   961 
   962 lemma subseq_imp_inj_on: "subseq g \<Longrightarrow> inj_on g A"
   963 proof (rule inj_onI)
   964   assume g: "subseq g"
   965   fix x y assume "g x = g y"
   966   with subseq_mono[OF g, of x y] subseq_mono[OF g, of y x] show "x = y"
   967     by (cases x y rule: linorder_cases) simp_all
   968 qed
   969 
   970 lemma subseq_strict_mono: "subseq g \<Longrightarrow> strict_mono g"
   971   by (intro strict_monoI subseq_mono[of g])
   972 
   973 lemma incseq_imp_monoseq:  "incseq X \<Longrightarrow> monoseq X"
   974   by (simp add: incseq_def monoseq_def)
   975 
   976 lemma decseq_imp_monoseq:  "decseq X \<Longrightarrow> monoseq X"
   977   by (simp add: decseq_def monoseq_def)
   978 
   979 lemma decseq_eq_incseq:
   980   fixes X :: "nat \<Rightarrow> 'a::ordered_ab_group_add" shows "decseq X = incseq (\<lambda>n. - X n)"
   981   by (simp add: decseq_def incseq_def)
   982 
   983 lemma INT_decseq_offset:
   984   assumes "decseq F"
   985   shows "(\<Inter>i. F i) = (\<Inter>i\<in>{n..}. F i)"
   986 proof safe
   987   fix x i assume x: "x \<in> (\<Inter>i\<in>{n..}. F i)"
   988   show "x \<in> F i"
   989   proof cases
   990     from x have "x \<in> F n" by auto
   991     also assume "i \<le> n" with \<open>decseq F\<close> have "F n \<subseteq> F i"
   992       unfolding decseq_def by simp
   993     finally show ?thesis .
   994   qed (insert x, simp)
   995 qed auto
   996 
   997 lemma LIMSEQ_const_iff:
   998   fixes k l :: "'a::t2_space"
   999   shows "(\<lambda>n. k) \<longlonglongrightarrow> l \<longleftrightarrow> k = l"
  1000   using trivial_limit_sequentially by (rule tendsto_const_iff)
  1001 
  1002 lemma LIMSEQ_SUP:
  1003   "incseq X \<Longrightarrow> X \<longlonglongrightarrow> (SUP i. X i :: 'a :: {complete_linorder, linorder_topology})"
  1004   by (intro increasing_tendsto)
  1005      (auto simp: SUP_upper less_SUP_iff incseq_def eventually_sequentially intro: less_le_trans)
  1006 
  1007 lemma LIMSEQ_INF:
  1008   "decseq X \<Longrightarrow> X \<longlonglongrightarrow> (INF i. X i :: 'a :: {complete_linorder, linorder_topology})"
  1009   by (intro decreasing_tendsto)
  1010      (auto simp: INF_lower INF_less_iff decseq_def eventually_sequentially intro: le_less_trans)
  1011 
  1012 lemma LIMSEQ_ignore_initial_segment:
  1013   "f \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (n + k)) \<longlonglongrightarrow> a"
  1014   unfolding tendsto_def
  1015   by (subst eventually_sequentially_seg[where k=k])
  1016 
  1017 lemma LIMSEQ_offset:
  1018   "(\<lambda>n. f (n + k)) \<longlonglongrightarrow> a \<Longrightarrow> f \<longlonglongrightarrow> a"
  1019   unfolding tendsto_def
  1020   by (subst (asm) eventually_sequentially_seg[where k=k])
  1021 
  1022 lemma LIMSEQ_Suc: "f \<longlonglongrightarrow> l \<Longrightarrow> (\<lambda>n. f (Suc n)) \<longlonglongrightarrow> l"
  1023 by (drule_tac k="Suc 0" in LIMSEQ_ignore_initial_segment, simp)
  1024 
  1025 lemma LIMSEQ_imp_Suc: "(\<lambda>n. f (Suc n)) \<longlonglongrightarrow> l \<Longrightarrow> f \<longlonglongrightarrow> l"
  1026 by (rule_tac k="Suc 0" in LIMSEQ_offset, simp)
  1027 
  1028 lemma LIMSEQ_Suc_iff: "(\<lambda>n. f (Suc n)) \<longlonglongrightarrow> l = f \<longlonglongrightarrow> l"
  1029 by (blast intro: LIMSEQ_imp_Suc LIMSEQ_Suc)
  1030 
  1031 lemma LIMSEQ_unique:
  1032   fixes a b :: "'a::t2_space"
  1033   shows "\<lbrakk>X \<longlonglongrightarrow> a; X \<longlonglongrightarrow> b\<rbrakk> \<Longrightarrow> a = b"
  1034   using trivial_limit_sequentially by (rule tendsto_unique)
  1035 
  1036 lemma LIMSEQ_le_const:
  1037   "\<lbrakk>X \<longlonglongrightarrow> (x::'a::linorder_topology); \<exists>N. \<forall>n\<ge>N. a \<le> X n\<rbrakk> \<Longrightarrow> a \<le> x"
  1038   using tendsto_le_const[of sequentially X x a] by (simp add: eventually_sequentially)
  1039 
  1040 lemma LIMSEQ_le:
  1041   "\<lbrakk>X \<longlonglongrightarrow> x; Y \<longlonglongrightarrow> y; \<exists>N. \<forall>n\<ge>N. X n \<le> Y n\<rbrakk> \<Longrightarrow> x \<le> (y::'a::linorder_topology)"
  1042   using tendsto_le[of sequentially Y y X x] by (simp add: eventually_sequentially)
  1043 
  1044 lemma LIMSEQ_le_const2:
  1045   "\<lbrakk>X \<longlonglongrightarrow> (x::'a::linorder_topology); \<exists>N. \<forall>n\<ge>N. X n \<le> a\<rbrakk> \<Longrightarrow> x \<le> a"
  1046   by (rule LIMSEQ_le[of X x "\<lambda>n. a"]) auto
  1047 
  1048 lemma convergentD: "convergent X ==> \<exists>L. (X \<longlonglongrightarrow> L)"
  1049 by (simp add: convergent_def)
  1050 
  1051 lemma convergentI: "(X \<longlonglongrightarrow> L) ==> convergent X"
  1052 by (auto simp add: convergent_def)
  1053 
  1054 lemma convergent_LIMSEQ_iff: "convergent X = (X \<longlonglongrightarrow> lim X)"
  1055 by (auto intro: theI LIMSEQ_unique simp add: convergent_def lim_def)
  1056 
  1057 lemma convergent_const: "convergent (\<lambda>n. c)"
  1058   by (rule convergentI, rule tendsto_const)
  1059 
  1060 lemma monoseq_le:
  1061   "monoseq a \<Longrightarrow> a \<longlonglongrightarrow> (x::'a::linorder_topology) \<Longrightarrow>
  1062     ((\<forall> n. a n \<le> x) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)) \<or> ((\<forall> n. x \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m))"
  1063   by (metis LIMSEQ_le_const LIMSEQ_le_const2 decseq_def incseq_def monoseq_iff)
  1064 
  1065 lemma LIMSEQ_subseq_LIMSEQ:
  1066   "\<lbrakk> X \<longlonglongrightarrow> L; subseq f \<rbrakk> \<Longrightarrow> (X o f) \<longlonglongrightarrow> L"
  1067   unfolding comp_def by (rule filterlim_compose[of X, OF _ filterlim_subseq])
  1068 
  1069 lemma convergent_subseq_convergent:
  1070   "\<lbrakk>convergent X; subseq f\<rbrakk> \<Longrightarrow> convergent (X o f)"
  1071   unfolding convergent_def by (auto intro: LIMSEQ_subseq_LIMSEQ)
  1072 
  1073 lemma limI: "X \<longlonglongrightarrow> L ==> lim X = L"
  1074   by (rule tendsto_Lim) (rule trivial_limit_sequentially)
  1075 
  1076 lemma lim_le: "convergent f \<Longrightarrow> (\<And>n. f n \<le> (x::'a::linorder_topology)) \<Longrightarrow> lim f \<le> x"
  1077   using LIMSEQ_le_const2[of f "lim f" x] by (simp add: convergent_LIMSEQ_iff)
  1078 
  1079 subsubsection\<open>Increasing and Decreasing Series\<close>
  1080 
  1081 lemma incseq_le: "incseq X \<Longrightarrow> X \<longlonglongrightarrow> L \<Longrightarrow> X n \<le> (L::'a::linorder_topology)"
  1082   by (metis incseq_def LIMSEQ_le_const)
  1083 
  1084 lemma decseq_le: "decseq X \<Longrightarrow> X \<longlonglongrightarrow> L \<Longrightarrow> (L::'a::linorder_topology) \<le> X n"
  1085   by (metis decseq_def LIMSEQ_le_const2)
  1086 
  1087 subsection \<open>First countable topologies\<close>
  1088 
  1089 class first_countable_topology = topological_space +
  1090   assumes first_countable_basis:
  1091     "\<exists>A::nat \<Rightarrow> 'a set. (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
  1092 
  1093 lemma (in first_countable_topology) countable_basis_at_decseq:
  1094   obtains A :: "nat \<Rightarrow> 'a set" where
  1095     "\<And>i. open (A i)" "\<And>i. x \<in> (A i)"
  1096     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  1097 proof atomize_elim
  1098   from first_countable_basis[of x] obtain A :: "nat \<Rightarrow> 'a set" where
  1099     nhds: "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1100     and incl: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>i. A i \<subseteq> S"  by auto
  1101   def F \<equiv> "\<lambda>n. \<Inter>i\<le>n. A i"
  1102   show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and>
  1103       (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially)"
  1104   proof (safe intro!: exI[of _ F])
  1105     fix i
  1106     show "open (F i)" using nhds(1) by (auto simp: F_def)
  1107     show "x \<in> F i" using nhds(2) by (auto simp: F_def)
  1108   next
  1109     fix S assume "open S" "x \<in> S"
  1110     from incl[OF this] obtain i where "F i \<subseteq> S" unfolding F_def by auto
  1111     moreover have "\<And>j. i \<le> j \<Longrightarrow> F j \<subseteq> F i"
  1112       by (auto simp: F_def)
  1113     ultimately show "eventually (\<lambda>i. F i \<subseteq> S) sequentially"
  1114       by (auto simp: eventually_sequentially)
  1115   qed
  1116 qed
  1117 
  1118 lemma (in first_countable_topology) nhds_countable:
  1119   obtains X :: "nat \<Rightarrow> 'a set"
  1120   where "decseq X" "\<And>n. open (X n)" "\<And>n. x \<in> X n" "nhds x = (INF n. principal (X n))"
  1121 proof -
  1122   from first_countable_basis obtain A :: "nat \<Rightarrow> 'a set"
  1123     where A: "\<And>n. x \<in> A n" "\<And>n. open (A n)" "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>i. A i \<subseteq> S"
  1124     by metis
  1125   show thesis
  1126   proof
  1127     show "decseq (\<lambda>n. \<Inter>i\<le>n. A i)"
  1128       by (auto simp: decseq_def)
  1129     show "\<And>n. x \<in> (\<Inter>i\<le>n. A i)" "\<And>n. open (\<Inter>i\<le>n. A i)"
  1130       using A by auto
  1131     show "nhds x = (INF n. principal (\<Inter>i\<le>n. A i))"
  1132       using A unfolding nhds_def
  1133       apply (intro INF_eq)
  1134       apply simp_all
  1135       apply force
  1136       apply (intro exI[of _ "\<Inter>i\<le>n. A i" for n] conjI open_INT)
  1137       apply auto
  1138       done
  1139   qed
  1140 qed
  1141 
  1142 lemma (in first_countable_topology) countable_basis:
  1143   obtains A :: "nat \<Rightarrow> 'a set" where
  1144     "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1145     "\<And>F. (\<forall>n. F n \<in> A n) \<Longrightarrow> F \<longlonglongrightarrow> x"
  1146 proof atomize_elim
  1147   obtain A :: "nat \<Rightarrow> 'a set" where A:
  1148     "\<And>i. open (A i)"
  1149     "\<And>i. x \<in> A i"
  1150     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  1151     by (rule countable_basis_at_decseq) blast
  1152   {
  1153     fix F S assume "\<forall>n. F n \<in> A n" "open S" "x \<in> S"
  1154     with A(3)[of S] have "eventually (\<lambda>n. F n \<in> S) sequentially"
  1155       by (auto elim: eventually_mono simp: subset_eq)
  1156   }
  1157   with A show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and> (\<forall>F. (\<forall>n. F n \<in> A n) \<longrightarrow> F \<longlonglongrightarrow> x)"
  1158     by (intro exI[of _ A]) (auto simp: tendsto_def)
  1159 qed
  1160 
  1161 lemma (in first_countable_topology) sequentially_imp_eventually_nhds_within:
  1162   assumes "\<forall>f. (\<forall>n. f n \<in> s) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially"
  1163   shows "eventually P (inf (nhds a) (principal s))"
  1164 proof (rule ccontr)
  1165   obtain A :: "nat \<Rightarrow> 'a set" where A:
  1166     "\<And>i. open (A i)"
  1167     "\<And>i. a \<in> A i"
  1168     "\<And>F. \<forall>n. F n \<in> A n \<Longrightarrow> F \<longlonglongrightarrow> a"
  1169     by (rule countable_basis) blast
  1170   assume "\<not> ?thesis"
  1171   with A have P: "\<exists>F. \<forall>n. F n \<in> s \<and> F n \<in> A n \<and> \<not> P (F n)"
  1172     unfolding eventually_inf_principal eventually_nhds by (intro choice) fastforce
  1173   then obtain F where F0: "\<forall>n. F n \<in> s" and F2: "\<forall>n. F n \<in> A n" and F3: "\<forall>n. \<not> P (F n)"
  1174     by blast
  1175   with A have "F \<longlonglongrightarrow> a" by auto
  1176   hence "eventually (\<lambda>n. P (F n)) sequentially"
  1177     using assms F0 by simp
  1178   thus "False" by (simp add: F3)
  1179 qed
  1180 
  1181 lemma (in first_countable_topology) eventually_nhds_within_iff_sequentially:
  1182   "eventually P (inf (nhds a) (principal s)) \<longleftrightarrow>
  1183     (\<forall>f. (\<forall>n. f n \<in> s) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
  1184 proof (safe intro!: sequentially_imp_eventually_nhds_within)
  1185   assume "eventually P (inf (nhds a) (principal s))"
  1186   then obtain S where "open S" "a \<in> S" "\<forall>x\<in>S. x \<in> s \<longrightarrow> P x"
  1187     by (auto simp: eventually_inf_principal eventually_nhds)
  1188   moreover fix f assume "\<forall>n. f n \<in> s" "f \<longlonglongrightarrow> a"
  1189   ultimately show "eventually (\<lambda>n. P (f n)) sequentially"
  1190     by (auto dest!: topological_tendstoD elim: eventually_mono)
  1191 qed
  1192 
  1193 lemma (in first_countable_topology) eventually_nhds_iff_sequentially:
  1194   "eventually P (nhds a) \<longleftrightarrow> (\<forall>f. f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
  1195   using eventually_nhds_within_iff_sequentially[of P a UNIV] by simp
  1196 
  1197 lemma tendsto_at_iff_sequentially:
  1198   fixes f :: "'a :: first_countable_topology \<Rightarrow> _"
  1199   shows "(f \<longlongrightarrow> a) (at x within s) \<longleftrightarrow> (\<forall>X. (\<forall>i. X i \<in> s - {x}) \<longrightarrow> X \<longlonglongrightarrow> x \<longrightarrow> ((f \<circ> X) \<longlonglongrightarrow> a))"
  1200   unfolding filterlim_def[of _ "nhds a"] le_filter_def eventually_filtermap at_within_def eventually_nhds_within_iff_sequentially comp_def
  1201   by metis
  1202 
  1203 subsection \<open>Function limit at a point\<close>
  1204 
  1205 abbreviation
  1206   LIM :: "('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
  1207         ("((_)/ \<midarrow>(_)/\<rightarrow> (_))" [60, 0, 60] 60) where
  1208   "f \<midarrow>a\<rightarrow> L \<equiv> (f \<longlongrightarrow> L) (at a)"
  1209 
  1210 lemma tendsto_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> (f \<longlongrightarrow> l) (at a within S) \<longleftrightarrow> (f \<midarrow>a\<rightarrow> l)"
  1211   unfolding tendsto_def by (simp add: at_within_open[where S=S])
  1212 
  1213 lemma LIM_const_not_eq[tendsto_intros]:
  1214   fixes a :: "'a::perfect_space"
  1215   fixes k L :: "'b::t2_space"
  1216   shows "k \<noteq> L \<Longrightarrow> \<not> (\<lambda>x. k) \<midarrow>a\<rightarrow> L"
  1217   by (simp add: tendsto_const_iff)
  1218 
  1219 lemmas LIM_not_zero = LIM_const_not_eq [where L = 0]
  1220 
  1221 lemma LIM_const_eq:
  1222   fixes a :: "'a::perfect_space"
  1223   fixes k L :: "'b::t2_space"
  1224   shows "(\<lambda>x. k) \<midarrow>a\<rightarrow> L \<Longrightarrow> k = L"
  1225   by (simp add: tendsto_const_iff)
  1226 
  1227 lemma LIM_unique:
  1228   fixes a :: "'a::perfect_space" and L M :: "'b::t2_space"
  1229   shows "f \<midarrow>a\<rightarrow> L \<Longrightarrow> f \<midarrow>a\<rightarrow> M \<Longrightarrow> L = M"
  1230   using at_neq_bot by (rule tendsto_unique)
  1231 
  1232 text \<open>Limits are equal for functions equal except at limit point\<close>
  1233 
  1234 lemma LIM_equal: "\<forall>x. x \<noteq> a --> (f x = g x) \<Longrightarrow> (f \<midarrow>a\<rightarrow> l) \<longleftrightarrow> (g \<midarrow>a\<rightarrow> l)"
  1235   unfolding tendsto_def eventually_at_topological by simp
  1236 
  1237 lemma LIM_cong: "a = b \<Longrightarrow> (\<And>x. x \<noteq> b \<Longrightarrow> f x = g x) \<Longrightarrow> l = m \<Longrightarrow> (f \<midarrow>a\<rightarrow> l) \<longleftrightarrow> (g \<midarrow>b\<rightarrow> m)"
  1238   by (simp add: LIM_equal)
  1239 
  1240 lemma LIM_cong_limit: "f \<midarrow>x\<rightarrow> L \<Longrightarrow> K = L \<Longrightarrow> f \<midarrow>x\<rightarrow> K"
  1241   by simp
  1242 
  1243 lemma tendsto_at_iff_tendsto_nhds:
  1244   "g \<midarrow>l\<rightarrow> g l \<longleftrightarrow> (g \<longlongrightarrow> g l) (nhds l)"
  1245   unfolding tendsto_def eventually_at_filter
  1246   by (intro ext all_cong imp_cong) (auto elim!: eventually_mono)
  1247 
  1248 lemma tendsto_compose:
  1249   "g \<midarrow>l\<rightarrow> g l \<Longrightarrow> (f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) \<longlongrightarrow> g l) F"
  1250   unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])
  1251 
  1252 lemma LIM_o: "\<lbrakk>g \<midarrow>l\<rightarrow> g l; f \<midarrow>a\<rightarrow> l\<rbrakk> \<Longrightarrow> (g \<circ> f) \<midarrow>a\<rightarrow> g l"
  1253   unfolding o_def by (rule tendsto_compose)
  1254 
  1255 lemma tendsto_compose_eventually:
  1256   "g \<midarrow>l\<rightarrow> m \<Longrightarrow> (f \<longlongrightarrow> l) F \<Longrightarrow> eventually (\<lambda>x. f x \<noteq> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) \<longlongrightarrow> m) F"
  1257   by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)
  1258 
  1259 lemma LIM_compose_eventually:
  1260   assumes f: "f \<midarrow>a\<rightarrow> b"
  1261   assumes g: "g \<midarrow>b\<rightarrow> c"
  1262   assumes inj: "eventually (\<lambda>x. f x \<noteq> b) (at a)"
  1263   shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> c"
  1264   using g f inj by (rule tendsto_compose_eventually)
  1265 
  1266 lemma tendsto_compose_filtermap: "((g \<circ> f) \<longlongrightarrow> T) F \<longleftrightarrow> (g \<longlongrightarrow> T) (filtermap f F)"
  1267   by (simp add: filterlim_def filtermap_filtermap comp_def)
  1268 
  1269 subsubsection \<open>Relation of LIM and LIMSEQ\<close>
  1270 
  1271 lemma (in first_countable_topology) sequentially_imp_eventually_within:
  1272   "(\<forall>f. (\<forall>n. f n \<in> s \<and> f n \<noteq> a) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially) \<Longrightarrow>
  1273     eventually P (at a within s)"
  1274   unfolding at_within_def
  1275   by (intro sequentially_imp_eventually_nhds_within) auto
  1276 
  1277 lemma (in first_countable_topology) sequentially_imp_eventually_at:
  1278   "(\<forall>f. (\<forall>n. f n \<noteq> a) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially) \<Longrightarrow> eventually P (at a)"
  1279   using assms sequentially_imp_eventually_within [where s=UNIV] by simp
  1280 
  1281 lemma LIMSEQ_SEQ_conv1:
  1282   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  1283   assumes f: "f \<midarrow>a\<rightarrow> l"
  1284   shows "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. f (S n)) \<longlonglongrightarrow> l"
  1285   using tendsto_compose_eventually [OF f, where F=sequentially] by simp
  1286 
  1287 lemma LIMSEQ_SEQ_conv2:
  1288   fixes f :: "'a::first_countable_topology \<Rightarrow> 'b::topological_space"
  1289   assumes "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. f (S n)) \<longlonglongrightarrow> l"
  1290   shows "f \<midarrow>a\<rightarrow> l"
  1291   using assms unfolding tendsto_def [where l=l] by (simp add: sequentially_imp_eventually_at)
  1292 
  1293 lemma LIMSEQ_SEQ_conv:
  1294   "(\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S \<longlonglongrightarrow> (a::'a::first_countable_topology) \<longrightarrow> (\<lambda>n. X (S n)) \<longlonglongrightarrow> L) =
  1295    (X \<midarrow>a\<rightarrow> (L::'b::topological_space))"
  1296   using LIMSEQ_SEQ_conv2 LIMSEQ_SEQ_conv1 ..
  1297 
  1298 lemma sequentially_imp_eventually_at_left:
  1299   fixes a :: "'a :: {linorder_topology, first_countable_topology}"
  1300   assumes b[simp]: "b < a"
  1301   assumes *: "\<And>f. (\<And>n. b < f n) \<Longrightarrow> (\<And>n. f n < a) \<Longrightarrow> incseq f \<Longrightarrow> f \<longlonglongrightarrow> a \<Longrightarrow> eventually (\<lambda>n. P (f n)) sequentially"
  1302   shows "eventually P (at_left a)"
  1303 proof (safe intro!: sequentially_imp_eventually_within)
  1304   fix X assume X: "\<forall>n. X n \<in> {..< a} \<and> X n \<noteq> a" "X \<longlonglongrightarrow> a"
  1305   show "eventually (\<lambda>n. P (X n)) sequentially"
  1306   proof (rule ccontr)
  1307     assume neg: "\<not> eventually (\<lambda>n. P (X n)) sequentially"
  1308     have "\<exists>s. \<forall>n. (\<not> P (X (s n)) \<and> b < X (s n)) \<and> (X (s n) \<le> X (s (Suc n)) \<and> Suc (s n) \<le> s (Suc n))"
  1309     proof (rule dependent_nat_choice)
  1310       have "\<not> eventually (\<lambda>n. b < X n \<longrightarrow> P (X n)) sequentially"
  1311         by (intro not_eventually_impI neg order_tendstoD(1) [OF X(2) b])
  1312       then show "\<exists>x. \<not> P (X x) \<and> b < X x"
  1313         by (auto dest!: not_eventuallyD)
  1314     next
  1315       fix x n
  1316       have "\<not> eventually (\<lambda>n. Suc x \<le> n \<longrightarrow> b < X n \<longrightarrow> X x < X n \<longrightarrow> P (X n)) sequentially"
  1317         using X by (intro not_eventually_impI order_tendstoD(1)[OF X(2)] eventually_ge_at_top neg) auto
  1318       then show "\<exists>n. (\<not> P (X n) \<and> b < X n) \<and> (X x \<le> X n \<and> Suc x \<le> n)"
  1319         by (auto dest!: not_eventuallyD)
  1320     qed
  1321     then guess s ..
  1322     then have "\<And>n. b < X (s n)" "\<And>n. X (s n) < a" "incseq (\<lambda>n. X (s n))" "(\<lambda>n. X (s n)) \<longlonglongrightarrow> a" "\<And>n. \<not> P (X (s n))"
  1323       using X by (auto simp: subseq_Suc_iff Suc_le_eq incseq_Suc_iff intro!: LIMSEQ_subseq_LIMSEQ[OF \<open>X \<longlonglongrightarrow> a\<close>, unfolded comp_def])
  1324     from *[OF this(1,2,3,4)] this(5) show False by auto
  1325   qed
  1326 qed
  1327 
  1328 lemma tendsto_at_left_sequentially:
  1329   fixes a :: "_ :: {linorder_topology, first_countable_topology}"
  1330   assumes "b < a"
  1331   assumes *: "\<And>S. (\<And>n. S n < a) \<Longrightarrow> (\<And>n. b < S n) \<Longrightarrow> incseq S \<Longrightarrow> S \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
  1332   shows "(X \<longlongrightarrow> L) (at_left a)"
  1333   using assms unfolding tendsto_def [where l=L]
  1334   by (simp add: sequentially_imp_eventually_at_left)
  1335 
  1336 lemma sequentially_imp_eventually_at_right:
  1337   fixes a :: "'a :: {linorder_topology, first_countable_topology}"
  1338   assumes b[simp]: "a < b"
  1339   assumes *: "\<And>f. (\<And>n. a < f n) \<Longrightarrow> (\<And>n. f n < b) \<Longrightarrow> decseq f \<Longrightarrow> f \<longlonglongrightarrow> a \<Longrightarrow> eventually (\<lambda>n. P (f n)) sequentially"
  1340   shows "eventually P (at_right a)"
  1341 proof (safe intro!: sequentially_imp_eventually_within)
  1342   fix X assume X: "\<forall>n. X n \<in> {a <..} \<and> X n \<noteq> a" "X \<longlonglongrightarrow> a"
  1343   show "eventually (\<lambda>n. P (X n)) sequentially"
  1344   proof (rule ccontr)
  1345     assume neg: "\<not> eventually (\<lambda>n. P (X n)) sequentially"
  1346     have "\<exists>s. \<forall>n. (\<not> P (X (s n)) \<and> X (s n) < b) \<and> (X (s (Suc n)) \<le> X (s n) \<and> Suc (s n) \<le> s (Suc n))"
  1347     proof (rule dependent_nat_choice)
  1348       have "\<not> eventually (\<lambda>n. X n < b \<longrightarrow> P (X n)) sequentially"
  1349         by (intro not_eventually_impI neg order_tendstoD(2) [OF X(2) b])
  1350       then show "\<exists>x. \<not> P (X x) \<and> X x < b"
  1351         by (auto dest!: not_eventuallyD)
  1352     next
  1353       fix x n
  1354       have "\<not> eventually (\<lambda>n. Suc x \<le> n \<longrightarrow> X n < b \<longrightarrow> X n < X x \<longrightarrow> P (X n)) sequentially"
  1355         using X by (intro not_eventually_impI order_tendstoD(2)[OF X(2)] eventually_ge_at_top neg) auto
  1356       then show "\<exists>n. (\<not> P (X n) \<and> X n < b) \<and> (X n \<le> X x \<and> Suc x \<le> n)"
  1357         by (auto dest!: not_eventuallyD)
  1358     qed
  1359     then guess s ..
  1360     then have "\<And>n. a < X (s n)" "\<And>n. X (s n) < b" "decseq (\<lambda>n. X (s n))" "(\<lambda>n. X (s n)) \<longlonglongrightarrow> a" "\<And>n. \<not> P (X (s n))"
  1361       using X by (auto simp: subseq_Suc_iff Suc_le_eq decseq_Suc_iff intro!: LIMSEQ_subseq_LIMSEQ[OF \<open>X \<longlonglongrightarrow> a\<close>, unfolded comp_def])
  1362     from *[OF this(1,2,3,4)] this(5) show False by auto
  1363   qed
  1364 qed
  1365 
  1366 lemma tendsto_at_right_sequentially:
  1367   fixes a :: "_ :: {linorder_topology, first_countable_topology}"
  1368   assumes "a < b"
  1369   assumes *: "\<And>S. (\<And>n. a < S n) \<Longrightarrow> (\<And>n. S n < b) \<Longrightarrow> decseq S \<Longrightarrow> S \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
  1370   shows "(X \<longlongrightarrow> L) (at_right a)"
  1371   using assms unfolding tendsto_def [where l=L]
  1372   by (simp add: sequentially_imp_eventually_at_right)
  1373 
  1374 subsection \<open>Continuity\<close>
  1375 
  1376 subsubsection \<open>Continuity on a set\<close>
  1377 
  1378 definition continuous_on :: "'a set \<Rightarrow> ('a :: topological_space \<Rightarrow> 'b :: topological_space) \<Rightarrow> bool" where
  1379   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f \<longlongrightarrow> f x) (at x within s))"
  1380 
  1381 lemma continuous_on_cong [cong]:
  1382   "s = t \<Longrightarrow> (\<And>x. x \<in> t \<Longrightarrow> f x = g x) \<Longrightarrow> continuous_on s f \<longleftrightarrow> continuous_on t g"
  1383   unfolding continuous_on_def by (intro ball_cong filterlim_cong) (auto simp: eventually_at_filter)
  1384 
  1385 lemma continuous_on_topological:
  1386   "continuous_on s f \<longleftrightarrow>
  1387     (\<forall>x\<in>s. \<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow> (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
  1388   unfolding continuous_on_def tendsto_def eventually_at_topological by metis
  1389 
  1390 lemma continuous_on_open_invariant:
  1391   "continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s))"
  1392 proof safe
  1393   fix B :: "'b set" assume "continuous_on s f" "open B"
  1394   then have "\<forall>x\<in>f -` B \<inter> s. (\<exists>A. open A \<and> x \<in> A \<and> s \<inter> A \<subseteq> f -` B)"
  1395     by (auto simp: continuous_on_topological subset_eq Ball_def imp_conjL)
  1396   then obtain A where "\<forall>x\<in>f -` B \<inter> s. open (A x) \<and> x \<in> A x \<and> s \<inter> A x \<subseteq> f -` B"
  1397     unfolding bchoice_iff ..
  1398   then show "\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s"
  1399     by (intro exI[of _ "\<Union>x\<in>f -` B \<inter> s. A x"]) auto
  1400 next
  1401   assume B: "\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s)"
  1402   show "continuous_on s f"
  1403     unfolding continuous_on_topological
  1404   proof safe
  1405     fix x B assume "x \<in> s" "open B" "f x \<in> B"
  1406     with B obtain A where A: "open A" "A \<inter> s = f -` B \<inter> s" by auto
  1407     with \<open>x \<in> s\<close> \<open>f x \<in> B\<close> show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"
  1408       by (intro exI[of _ A]) auto
  1409   qed
  1410 qed
  1411 
  1412 lemma continuous_on_open_vimage:
  1413   "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> open (f -` B \<inter> s))"
  1414   unfolding continuous_on_open_invariant
  1415   by (metis open_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
  1416 
  1417 corollary continuous_imp_open_vimage:
  1418   assumes "continuous_on s f" "open s" "open B" "f -` B \<subseteq> s"
  1419     shows "open (f -` B)"
  1420 by (metis assms continuous_on_open_vimage le_iff_inf)
  1421 
  1422 corollary open_vimage[continuous_intros]:
  1423   assumes "open s" and "continuous_on UNIV f"
  1424   shows "open (f -` s)"
  1425   using assms unfolding continuous_on_open_vimage [OF open_UNIV]
  1426   by simp
  1427 
  1428 lemma continuous_on_closed_invariant:
  1429   "continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> (\<exists>A. closed A \<and> A \<inter> s = f -` B \<inter> s))"
  1430 proof -
  1431   have *: "\<And>P Q::'b set\<Rightarrow>bool. (\<And>A. P A \<longleftrightarrow> Q (- A)) \<Longrightarrow> (\<forall>A. P A) \<longleftrightarrow> (\<forall>A. Q A)"
  1432     by (metis double_compl)
  1433   show ?thesis
  1434     unfolding continuous_on_open_invariant by (intro *) (auto simp: open_closed[symmetric])
  1435 qed
  1436 
  1437 lemma continuous_on_closed_vimage:
  1438   "closed s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> closed (f -` B \<inter> s))"
  1439   unfolding continuous_on_closed_invariant
  1440   by (metis closed_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
  1441 
  1442 corollary closed_vimage_Int[continuous_intros]:
  1443   assumes "closed s" and "continuous_on t f" and t: "closed t"
  1444   shows "closed (f -` s \<inter> t)"
  1445   using assms unfolding continuous_on_closed_vimage [OF t]  by simp
  1446 
  1447 corollary closed_vimage[continuous_intros]:
  1448   assumes "closed s" and "continuous_on UNIV f"
  1449   shows "closed (f -` s)"
  1450   using closed_vimage_Int [OF assms] by simp
  1451 
  1452 lemma continuous_on_empty: "continuous_on {} f"
  1453   by (simp add: continuous_on_def)
  1454 
  1455 lemma continuous_on_sing: "continuous_on {x} f"
  1456   by (simp add: continuous_on_def at_within_def)
  1457 
  1458 lemma continuous_on_open_Union:
  1459   "(\<And>s. s \<in> S \<Longrightarrow> open s) \<Longrightarrow> (\<And>s. s \<in> S \<Longrightarrow> continuous_on s f) \<Longrightarrow> continuous_on (\<Union>S) f"
  1460   unfolding continuous_on_def by safe (metis open_Union at_within_open UnionI)
  1461 
  1462 lemma continuous_on_open_UN:
  1463   "(\<And>s. s \<in> S \<Longrightarrow> open (A s)) \<Longrightarrow> (\<And>s. s \<in> S \<Longrightarrow> continuous_on (A s) f) \<Longrightarrow> continuous_on (\<Union>s\<in>S. A s) f"
  1464   unfolding Union_image_eq[symmetric] by (rule continuous_on_open_Union) auto
  1465 
  1466 lemma continuous_on_open_Un:
  1467   "open s \<Longrightarrow> open t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t f \<Longrightarrow> continuous_on (s \<union> t) f"
  1468   using continuous_on_open_Union [of "{s,t}"] by auto
  1469 
  1470 lemma continuous_on_closed_Un:
  1471   "closed s \<Longrightarrow> closed t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t f \<Longrightarrow> continuous_on (s \<union> t) f"
  1472   by (auto simp add: continuous_on_closed_vimage closed_Un Int_Un_distrib)
  1473 
  1474 lemma continuous_on_If:
  1475   assumes closed: "closed s" "closed t" and cont: "continuous_on s f" "continuous_on t g"
  1476     and P: "\<And>x. x \<in> s \<Longrightarrow> \<not> P x \<Longrightarrow> f x = g x" "\<And>x. x \<in> t \<Longrightarrow> P x \<Longrightarrow> f x = g x"
  1477   shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)" (is "continuous_on _ ?h")
  1478 proof-
  1479   from P have "\<forall>x\<in>s. f x = ?h x" "\<forall>x\<in>t. g x = ?h x"
  1480     by auto
  1481   with cont have "continuous_on s ?h" "continuous_on t ?h"
  1482     by simp_all
  1483   with closed show ?thesis
  1484     by (rule continuous_on_closed_Un)
  1485 qed
  1486 
  1487 lemma continuous_on_id[continuous_intros]: "continuous_on s (\<lambda>x. x)"
  1488   unfolding continuous_on_def by fast
  1489 
  1490 lemma continuous_on_const[continuous_intros]: "continuous_on s (\<lambda>x. c)"
  1491   unfolding continuous_on_def by auto
  1492 
  1493 lemma continuous_on_subset: "continuous_on s f \<Longrightarrow> t \<subseteq> s \<Longrightarrow> continuous_on t f"
  1494   unfolding continuous_on_def by (metis subset_eq tendsto_within_subset)
  1495 
  1496 lemma continuous_on_compose[continuous_intros]:
  1497   "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g o f)"
  1498   unfolding continuous_on_topological by simp metis
  1499 
  1500 lemma continuous_on_compose2:
  1501   "continuous_on t g \<Longrightarrow> continuous_on s f \<Longrightarrow> f ` s \<subseteq> t \<Longrightarrow> continuous_on s (\<lambda>x. g (f x))"
  1502   using continuous_on_compose[of s f g] continuous_on_subset by (force simp add: comp_def)
  1503 
  1504 lemma continuous_on_generate_topology:
  1505   assumes *: "open = generate_topology X"
  1506   assumes **: "\<And>B. B \<in> X \<Longrightarrow> \<exists>C. open C \<and> C \<inter> A = f -` B \<inter> A"
  1507   shows "continuous_on A f"
  1508   unfolding continuous_on_open_invariant
  1509 proof safe
  1510   fix B :: "'a set" assume "open B" then show "\<exists>C. open C \<and> C \<inter> A = f -` B \<inter> A"
  1511     unfolding *
  1512   proof induction
  1513     case (UN K)
  1514     then obtain C where "\<And>k. k \<in> K \<Longrightarrow> open (C k)" "\<And>k. k \<in> K \<Longrightarrow> C k \<inter> A = f -` k \<inter> A"
  1515       by metis
  1516     then show ?case
  1517       by (intro exI[of _ "\<Union>k\<in>K. C k"]) blast
  1518   qed (auto intro: **)
  1519 qed
  1520 
  1521 lemma continuous_onI_mono:
  1522   fixes f :: "'a::linorder_topology \<Rightarrow> 'b::{dense_order, linorder_topology}"
  1523   assumes "open (f`A)"
  1524   assumes mono: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
  1525   shows "continuous_on A f"
  1526 proof (rule continuous_on_generate_topology[OF open_generated_order], safe)
  1527   have monoD: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> f x < f y \<Longrightarrow> x < y"
  1528     by (auto simp: not_le[symmetric] mono)
  1529 
  1530   { fix a b assume "a \<in> A" "f a < b"
  1531     moreover
  1532     with open_right[OF \<open>open (f`A)\<close>, of "f a" b] obtain y where "f a < y" "{f a ..< y} \<subseteq> f`A"
  1533       by auto
  1534     moreover then obtain z where "f a < z" "z < min b y"
  1535       using dense[of "f a" "min b y"] \<open>f a < y\<close> \<open>f a < b\<close> by auto
  1536     moreover then obtain c where "z = f c" "c \<in> A"
  1537       using \<open>{f a ..< y} \<subseteq> f`A\<close>[THEN subsetD, of z] by (auto simp: less_imp_le)
  1538     ultimately have "\<exists>x. x \<in> A \<and> f x < b \<and> a < x"
  1539       by (auto intro!: exI[of _ c] simp: monoD) }
  1540   then show "\<exists>C. open C \<and> C \<inter> A = f -` {..<b} \<inter> A" for b
  1541     by (intro exI[of _ "(\<Union>x\<in>{x\<in>A. f x < b}. {..< x})"])
  1542        (auto intro: le_less_trans[OF mono] less_imp_le)
  1543 
  1544   { fix a b assume "a \<in> A" "b < f a"
  1545     moreover
  1546     with open_left[OF \<open>open (f`A)\<close>, of "f a" b] obtain y where "y < f a" "{y <.. f a} \<subseteq> f`A"
  1547       by auto
  1548     moreover then obtain z where "max b y < z" "z < f a"
  1549       using dense[of "max b y" "f a"] \<open>y < f a\<close> \<open>b < f a\<close> by auto
  1550     moreover then obtain c where "z = f c" "c \<in> A"
  1551       using \<open>{y <.. f a} \<subseteq> f`A\<close>[THEN subsetD, of z] by (auto simp: less_imp_le)
  1552     ultimately have "\<exists>x. x \<in> A \<and> b < f x \<and> x < a"
  1553       by (auto intro!: exI[of _ c] simp: monoD) }
  1554   then show "\<exists>C. open C \<and> C \<inter> A = f -` {b <..} \<inter> A" for b
  1555     by (intro exI[of _ "(\<Union>x\<in>{x\<in>A. b < f x}. {x <..})"])
  1556        (auto intro: less_le_trans[OF _ mono] less_imp_le)
  1557 qed
  1558 
  1559 subsubsection \<open>Continuity at a point\<close>
  1560 
  1561 definition continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool" where
  1562   "continuous F f \<longleftrightarrow> (f \<longlongrightarrow> f (Lim F (\<lambda>x. x))) F"
  1563 
  1564 lemma continuous_bot[continuous_intros, simp]: "continuous bot f"
  1565   unfolding continuous_def by auto
  1566 
  1567 lemma continuous_trivial_limit: "trivial_limit net \<Longrightarrow> continuous net f"
  1568   by simp
  1569 
  1570 lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f \<longlongrightarrow> f x) (at x within s)"
  1571   by (cases "trivial_limit (at x within s)") (auto simp add: Lim_ident_at continuous_def)
  1572 
  1573 lemma continuous_within_topological:
  1574   "continuous (at x within s) f \<longleftrightarrow>
  1575     (\<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow> (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"
  1576   unfolding continuous_within tendsto_def eventually_at_topological by metis
  1577 
  1578 lemma continuous_within_compose[continuous_intros]:
  1579   "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
  1580   continuous (at x within s) (g o f)"
  1581   by (simp add: continuous_within_topological) metis
  1582 
  1583 lemma continuous_within_compose2:
  1584   "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
  1585   continuous (at x within s) (\<lambda>x. g (f x))"
  1586   using continuous_within_compose[of x s f g] by (simp add: comp_def)
  1587 
  1588 lemma continuous_at: "continuous (at x) f \<longleftrightarrow> f \<midarrow>x\<rightarrow> f x"
  1589   using continuous_within[of x UNIV f] by simp
  1590 
  1591 lemma continuous_ident[continuous_intros, simp]: "continuous (at x within S) (\<lambda>x. x)"
  1592   unfolding continuous_within by (rule tendsto_ident_at)
  1593 
  1594 lemma continuous_const[continuous_intros, simp]: "continuous F (\<lambda>x. c)"
  1595   unfolding continuous_def by (rule tendsto_const)
  1596 
  1597 lemma continuous_on_eq_continuous_within:
  1598   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. continuous (at x within s) f)"
  1599   unfolding continuous_on_def continuous_within ..
  1600 
  1601 abbreviation isCont :: "('a::t2_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> bool" where
  1602   "isCont f a \<equiv> continuous (at a) f"
  1603 
  1604 lemma isCont_def: "isCont f a \<longleftrightarrow> f \<midarrow>a\<rightarrow> f a"
  1605   by (rule continuous_at)
  1606 
  1607 lemma continuous_at_imp_continuous_at_within: "isCont f x \<Longrightarrow> continuous (at x within s) f"
  1608   by (auto intro: tendsto_mono at_le simp: continuous_at continuous_within)
  1609 
  1610 lemma continuous_on_eq_continuous_at: "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. isCont f x)"
  1611   by (simp add: continuous_on_def continuous_at at_within_open[of _ s])
  1612 
  1613 lemma continuous_within_open: "a \<in> A \<Longrightarrow> open A \<Longrightarrow> continuous (at a within A) f \<longleftrightarrow> isCont f a"
  1614   by (simp add: at_within_open_NO_MATCH)
  1615 
  1616 lemma continuous_at_imp_continuous_on: "\<forall>x\<in>s. isCont f x \<Longrightarrow> continuous_on s f"
  1617   by (auto intro: continuous_at_imp_continuous_at_within simp: continuous_on_eq_continuous_within)
  1618 
  1619 lemma isCont_o2: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (\<lambda>x. g (f x)) a"
  1620   unfolding isCont_def by (rule tendsto_compose)
  1621 
  1622 lemma isCont_o[continuous_intros]: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (g \<circ> f) a"
  1623   unfolding o_def by (rule isCont_o2)
  1624 
  1625 lemma isCont_tendsto_compose: "isCont g l \<Longrightarrow> (f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) \<longlongrightarrow> g l) F"
  1626   unfolding isCont_def by (rule tendsto_compose)
  1627 
  1628 lemma continuous_on_tendsto_compose:
  1629   assumes f_cont: "continuous_on s f"
  1630   assumes g: "(g \<longlongrightarrow> l) F"
  1631   assumes l: "l \<in> s"
  1632   assumes ev: "\<forall>\<^sub>F x in F. g x \<in> s"
  1633   shows "((\<lambda>x. f (g x)) \<longlongrightarrow> f l) F"
  1634 proof -
  1635   from f_cont l have f: "(f \<longlongrightarrow> f l) (at l within s)"
  1636     by (simp add: continuous_on_def)
  1637   have i: "((\<lambda>x. if g x = l then f l else f (g x)) \<longlongrightarrow> f l) F"
  1638     by (rule filterlim_If)
  1639        (auto intro!: filterlim_compose[OF f] eventually_conj tendsto_mono[OF _ g]
  1640              simp: filterlim_at eventually_inf_principal eventually_mono[OF ev])
  1641   show ?thesis
  1642     by (rule filterlim_cong[THEN iffD1[OF _ i]]) auto
  1643 qed
  1644 
  1645 lemma continuous_within_compose3:
  1646   "isCont g (f x) \<Longrightarrow> continuous (at x within s) f \<Longrightarrow> continuous (at x within s) (\<lambda>x. g (f x))"
  1647   using continuous_within_compose2[of x s f g] by (simp add: continuous_at_imp_continuous_at_within)
  1648 
  1649 lemma filtermap_nhds_open_map:
  1650   assumes cont: "isCont f a" and open_map: "\<And>S. open S \<Longrightarrow> open (f`S)"
  1651   shows "filtermap f (nhds a) = nhds (f a)"
  1652   unfolding filter_eq_iff
  1653 proof safe
  1654   fix P assume "eventually P (filtermap f (nhds a))"
  1655   then guess S unfolding eventually_filtermap eventually_nhds ..
  1656   then show "eventually P (nhds (f a))"
  1657     unfolding eventually_nhds by (intro exI[of _ "f`S"]) (auto intro!: open_map)
  1658 qed (metis filterlim_iff tendsto_at_iff_tendsto_nhds isCont_def eventually_filtermap cont)
  1659 
  1660 lemma continuous_at_split:
  1661   "continuous (at (x::'a::linorder_topology)) f = (continuous (at_left x) f \<and> continuous (at_right x) f)"
  1662   by (simp add: continuous_within filterlim_at_split)
  1663 
  1664 subsubsection \<open>Open-cover compactness\<close>
  1665 
  1666 context topological_space
  1667 begin
  1668 
  1669 definition compact :: "'a set \<Rightarrow> bool" where
  1670   compact_eq_heine_borel: \<comment> "This name is used for backwards compatibility"
  1671     "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))"
  1672 
  1673 lemma compactI:
  1674   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'"
  1675   shows "compact s"
  1676   unfolding compact_eq_heine_borel using assms by metis
  1677 
  1678 lemma compact_empty[simp]: "compact {}"
  1679   by (auto intro!: compactI)
  1680 
  1681 lemma compactE:
  1682   assumes "compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C"
  1683   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
  1684   using assms unfolding compact_eq_heine_borel by metis
  1685 
  1686 lemma compactE_image:
  1687   assumes "compact s" and "\<forall>t\<in>C. open (f t)" and "s \<subseteq> (\<Union>c\<in>C. f c)"
  1688   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> (\<Union>c\<in>C'. f c)"
  1689   using assms unfolding ball_simps[symmetric] SUP_def
  1690   by (metis (lifting) finite_subset_image compact_eq_heine_borel[of s])
  1691 
  1692 lemma compact_inter_closed [intro]:
  1693   assumes "compact s" and "closed t"
  1694   shows "compact (s \<inter> t)"
  1695 proof (rule compactI)
  1696   fix C assume C: "\<forall>c\<in>C. open c" and cover: "s \<inter> t \<subseteq> \<Union>C"
  1697   from C \<open>closed t\<close> have "\<forall>c\<in>C \<union> {-t}. open c" by auto
  1698   moreover from cover have "s \<subseteq> \<Union>(C \<union> {-t})" by auto
  1699   ultimately have "\<exists>D\<subseteq>C \<union> {-t}. finite D \<and> s \<subseteq> \<Union>D"
  1700     using \<open>compact s\<close> unfolding compact_eq_heine_borel by auto
  1701   then obtain D where "D \<subseteq> C \<union> {- t} \<and> finite D \<and> s \<subseteq> \<Union>D" ..
  1702   then show "\<exists>D\<subseteq>C. finite D \<and> s \<inter> t \<subseteq> \<Union>D"
  1703     by (intro exI[of _ "D - {-t}"]) auto
  1704 qed
  1705 
  1706 lemma inj_setminus: "inj_on uminus (A::'a set set)"
  1707   by (auto simp: inj_on_def)
  1708 
  1709 lemma compact_fip:
  1710   "compact U \<longleftrightarrow>
  1711     (\<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> {})"
  1712   (is "_ \<longleftrightarrow> ?R")
  1713 proof (safe intro!: compact_eq_heine_borel[THEN iffD2])
  1714   fix A
  1715   assume "compact U"
  1716     and A: "\<forall>a\<in>A. closed a" "U \<inter> \<Inter>A = {}"
  1717     and fi: "\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}"
  1718   from A have "(\<forall>a\<in>uminus`A. open a) \<and> U \<subseteq> \<Union>(uminus`A)"
  1719     by auto
  1720   with \<open>compact U\<close> obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<subseteq> \<Union>(uminus`B)"
  1721     unfolding compact_eq_heine_borel by (metis subset_image_iff)
  1722   with fi[THEN spec, of B] show False
  1723     by (auto dest: finite_imageD intro: inj_setminus)
  1724 next
  1725   fix A
  1726   assume ?R
  1727   assume "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
  1728   then have "U \<inter> \<Inter>(uminus`A) = {}" "\<forall>a\<in>uminus`A. closed a"
  1729     by auto
  1730   with \<open>?R\<close> obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<inter> \<Inter>(uminus`B) = {}"
  1731     by (metis subset_image_iff)
  1732   then show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  1733     by  (auto intro!: exI[of _ B] inj_setminus dest: finite_imageD)
  1734 qed
  1735 
  1736 lemma compact_imp_fip:
  1737   "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>
  1738     s \<inter> (\<Inter>f) \<noteq> {}"
  1739   unfolding compact_fip by auto
  1740 
  1741 lemma compact_imp_fip_image:
  1742   assumes "compact s"
  1743     and P: "\<And>i. i \<in> I \<Longrightarrow> closed (f i)"
  1744     and Q: "\<And>I'. finite I' \<Longrightarrow> I' \<subseteq> I \<Longrightarrow> (s \<inter> (\<Inter>i\<in>I'. f i) \<noteq> {})"
  1745   shows "s \<inter> (\<Inter>i\<in>I. f i) \<noteq> {}"
  1746 proof -
  1747   note \<open>compact s\<close>
  1748   moreover from P have "\<forall>i \<in> f ` I. closed i" by blast
  1749   moreover have "\<forall>A. finite A \<and> A \<subseteq> f ` I \<longrightarrow> (s \<inter> (\<Inter>A) \<noteq> {})"
  1750   proof (rule, rule, erule conjE)
  1751     fix A :: "'a set set"
  1752     assume "finite A"
  1753     moreover assume "A \<subseteq> f ` I"
  1754     ultimately obtain B where "B \<subseteq> I" and "finite B" and "A = f ` B"
  1755       using finite_subset_image [of A f I] by blast
  1756     with Q [of B] show "s \<inter> \<Inter>A \<noteq> {}" by simp
  1757   qed
  1758   ultimately have "s \<inter> (\<Inter>(f ` I)) \<noteq> {}" by (rule compact_imp_fip)
  1759   then show ?thesis by simp
  1760 qed
  1761 
  1762 end
  1763 
  1764 lemma (in t2_space) compact_imp_closed:
  1765   assumes "compact s" shows "closed s"
  1766 unfolding closed_def
  1767 proof (rule openI)
  1768   fix y assume "y \<in> - s"
  1769   let ?C = "\<Union>x\<in>s. {u. open u \<and> x \<in> u \<and> eventually (\<lambda>y. y \<notin> u) (nhds y)}"
  1770   note \<open>compact s\<close>
  1771   moreover have "\<forall>u\<in>?C. open u" by simp
  1772   moreover have "s \<subseteq> \<Union>?C"
  1773   proof
  1774     fix x assume "x \<in> s"
  1775     with \<open>y \<in> - s\<close> have "x \<noteq> y" by clarsimp
  1776     hence "\<exists>u v. open u \<and> open v \<and> x \<in> u \<and> y \<in> v \<and> u \<inter> v = {}"
  1777       by (rule hausdorff)
  1778     with \<open>x \<in> s\<close> show "x \<in> \<Union>?C"
  1779       unfolding eventually_nhds by auto
  1780   qed
  1781   ultimately obtain D where "D \<subseteq> ?C" and "finite D" and "s \<subseteq> \<Union>D"
  1782     by (rule compactE)
  1783   from \<open>D \<subseteq> ?C\<close> have "\<forall>x\<in>D. eventually (\<lambda>y. y \<notin> x) (nhds y)" by auto
  1784   with \<open>finite D\<close> have "eventually (\<lambda>y. y \<notin> \<Union>D) (nhds y)"
  1785     by (simp add: eventually_ball_finite)
  1786   with \<open>s \<subseteq> \<Union>D\<close> have "eventually (\<lambda>y. y \<notin> s) (nhds y)"
  1787     by (auto elim!: eventually_mono)
  1788   thus "\<exists>t. open t \<and> y \<in> t \<and> t \<subseteq> - s"
  1789     by (simp add: eventually_nhds subset_eq)
  1790 qed
  1791 
  1792 lemma compact_continuous_image:
  1793   assumes f: "continuous_on s f" and s: "compact s"
  1794   shows "compact (f ` s)"
  1795 proof (rule compactI)
  1796   fix C assume "\<forall>c\<in>C. open c" and cover: "f`s \<subseteq> \<Union>C"
  1797   with f have "\<forall>c\<in>C. \<exists>A. open A \<and> A \<inter> s = f -` c \<inter> s"
  1798     unfolding continuous_on_open_invariant by blast
  1799   then obtain A where A: "\<forall>c\<in>C. open (A c) \<and> A c \<inter> s = f -` c \<inter> s"
  1800     unfolding bchoice_iff ..
  1801   with cover have "\<forall>c\<in>C. open (A c)" "s \<subseteq> (\<Union>c\<in>C. A c)"
  1802     by (fastforce simp add: subset_eq set_eq_iff)+
  1803   from compactE_image[OF s this] obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> (\<Union>c\<in>D. A c)" .
  1804   with A show "\<exists>D \<subseteq> C. finite D \<and> f`s \<subseteq> \<Union>D"
  1805     by (intro exI[of _ D]) (fastforce simp add: subset_eq set_eq_iff)+
  1806 qed
  1807 
  1808 lemma continuous_on_inv:
  1809   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  1810   assumes "continuous_on s f"  "compact s"  "\<forall>x\<in>s. g (f x) = x"
  1811   shows "continuous_on (f ` s) g"
  1812 unfolding continuous_on_topological
  1813 proof (clarsimp simp add: assms(3))
  1814   fix x :: 'a and B :: "'a set"
  1815   assume "x \<in> s" and "open B" and "x \<in> B"
  1816   have 1: "\<forall>x\<in>s. f x \<in> f ` (s - B) \<longleftrightarrow> x \<in> s - B"
  1817     using assms(3) by (auto, metis)
  1818   have "continuous_on (s - B) f"
  1819     using \<open>continuous_on s f\<close> Diff_subset
  1820     by (rule continuous_on_subset)
  1821   moreover have "compact (s - B)"
  1822     using \<open>open B\<close> and \<open>compact s\<close>
  1823     unfolding Diff_eq by (intro compact_inter_closed closed_Compl)
  1824   ultimately have "compact (f ` (s - B))"
  1825     by (rule compact_continuous_image)
  1826   hence "closed (f ` (s - B))"
  1827     by (rule compact_imp_closed)
  1828   hence "open (- f ` (s - B))"
  1829     by (rule open_Compl)
  1830   moreover have "f x \<in> - f ` (s - B)"
  1831     using \<open>x \<in> s\<close> and \<open>x \<in> B\<close> by (simp add: 1)
  1832   moreover have "\<forall>y\<in>s. f y \<in> - f ` (s - B) \<longrightarrow> y \<in> B"
  1833     by (simp add: 1)
  1834   ultimately show "\<exists>A. open A \<and> f x \<in> A \<and> (\<forall>y\<in>s. f y \<in> A \<longrightarrow> y \<in> B)"
  1835     by fast
  1836 qed
  1837 
  1838 lemma continuous_on_inv_into:
  1839   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  1840   assumes s: "continuous_on s f" "compact s" and f: "inj_on f s"
  1841   shows "continuous_on (f ` s) (the_inv_into s f)"
  1842   by (rule continuous_on_inv[OF s]) (auto simp: the_inv_into_f_f[OF f])
  1843 
  1844 lemma (in linorder_topology) compact_attains_sup:
  1845   assumes "compact S" "S \<noteq> {}"
  1846   shows "\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s"
  1847 proof (rule classical)
  1848   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s)"
  1849   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. s < t s"
  1850     by (metis not_le)
  1851   then have "\<forall>s\<in>S. open {..< t s}" "S \<subseteq> (\<Union>s\<in>S. {..< t s})"
  1852     by auto
  1853   with \<open>compact S\<close> obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {..< t s})"
  1854     by (erule compactE_image)
  1855   with \<open>S \<noteq> {}\<close> have Max: "Max (t`C) \<in> t`C" and "\<forall>s\<in>t`C. s \<le> Max (t`C)"
  1856     by (auto intro!: Max_in)
  1857   with C have "S \<subseteq> {..< Max (t`C)}"
  1858     by (auto intro: less_le_trans simp: subset_eq)
  1859   with t Max \<open>C \<subseteq> S\<close> show ?thesis
  1860     by fastforce
  1861 qed
  1862 
  1863 lemma (in linorder_topology) compact_attains_inf:
  1864   assumes "compact S" "S \<noteq> {}"
  1865   shows "\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t"
  1866 proof (rule classical)
  1867   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t)"
  1868   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. t s < s"
  1869     by (metis not_le)
  1870   then have "\<forall>s\<in>S. open {t s <..}" "S \<subseteq> (\<Union>s\<in>S. {t s <..})"
  1871     by auto
  1872   with \<open>compact S\<close> obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {t s <..})"
  1873     by (erule compactE_image)
  1874   with \<open>S \<noteq> {}\<close> have Min: "Min (t`C) \<in> t`C" and "\<forall>s\<in>t`C. Min (t`C) \<le> s"
  1875     by (auto intro!: Min_in)
  1876   with C have "S \<subseteq> {Min (t`C) <..}"
  1877     by (auto intro: le_less_trans simp: subset_eq)
  1878   with t Min \<open>C \<subseteq> S\<close> show ?thesis
  1879     by fastforce
  1880 qed
  1881 
  1882 lemma continuous_attains_sup:
  1883   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
  1884   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f \<Longrightarrow> (\<exists>x\<in>s. \<forall>y\<in>s.  f y \<le> f x)"
  1885   using compact_attains_sup[of "f ` s"] compact_continuous_image[of s f] by auto
  1886 
  1887 lemma continuous_attains_inf:
  1888   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
  1889   shows "compact s \<Longrightarrow> s \<noteq> {} \<Longrightarrow> continuous_on s f \<Longrightarrow> (\<exists>x\<in>s. \<forall>y\<in>s. f x \<le> f y)"
  1890   using compact_attains_inf[of "f ` s"] compact_continuous_image[of s f] by auto
  1891 
  1892 subsection \<open>Connectedness\<close>
  1893 
  1894 context topological_space
  1895 begin
  1896 
  1897 definition "connected S \<longleftrightarrow>
  1898   \<not> (\<exists>A B. open A \<and> open B \<and> S \<subseteq> A \<union> B \<and> A \<inter> B \<inter> S = {} \<and> A \<inter> S \<noteq> {} \<and> B \<inter> S \<noteq> {})"
  1899 
  1900 lemma connectedI:
  1901   "(\<And>A B. open A \<Longrightarrow> open B \<Longrightarrow> A \<inter> U \<noteq> {} \<Longrightarrow> B \<inter> U \<noteq> {} \<Longrightarrow> A \<inter> B \<inter> U = {} \<Longrightarrow> U \<subseteq> A \<union> B \<Longrightarrow> False)
  1902   \<Longrightarrow> connected U"
  1903   by (auto simp: connected_def)
  1904 
  1905 lemma connected_empty [simp]: "connected {}"
  1906   by (auto intro!: connectedI)
  1907 
  1908 lemma connected_sing [simp]: "connected {x}"
  1909   by (auto intro!: connectedI)
  1910 
  1911 lemma connectedD:
  1912   "connected A \<Longrightarrow> open U \<Longrightarrow> open V \<Longrightarrow> U \<inter> V \<inter> A = {} \<Longrightarrow> A \<subseteq> U \<union> V \<Longrightarrow> U \<inter> A = {} \<or> V \<inter> A = {}"
  1913   by (auto simp: connected_def)
  1914 
  1915 end
  1916 
  1917 lemma connected_closed:
  1918     "connected s \<longleftrightarrow>
  1919      ~ (\<exists>A B. closed A \<and> closed B \<and> s \<subseteq> A \<union> B \<and> A \<inter> B \<inter> s = {} \<and> A \<inter> s \<noteq> {} \<and> B \<inter> s \<noteq> {})"
  1920 apply (simp add: connected_def del: ex_simps, safe)
  1921 apply (drule_tac x="-A" in spec)
  1922 apply (drule_tac x="-B" in spec)
  1923 apply (fastforce simp add: closed_def [symmetric])
  1924 apply (drule_tac x="-A" in spec)
  1925 apply (drule_tac x="-B" in spec)
  1926 apply (fastforce simp add: open_closed [symmetric])
  1927 done
  1928 
  1929 
  1930 lemma connected_Union:
  1931   assumes cs: "\<And>s. s \<in> S \<Longrightarrow> connected s" and ne: "\<Inter>S \<noteq> {}"
  1932     shows "connected(\<Union>S)"
  1933 proof (rule connectedI)
  1934   fix A B
  1935   assume A: "open A" and B: "open B" and Alap: "A \<inter> \<Union>S \<noteq> {}" and Blap: "B \<inter> \<Union>S \<noteq> {}"
  1936      and disj: "A \<inter> B \<inter> \<Union>S = {}" and cover: "\<Union>S \<subseteq> A \<union> B"
  1937   have disjs:"\<And>s. s \<in> S \<Longrightarrow> A \<inter> B \<inter> s = {}"
  1938     using disj by auto
  1939   obtain sa where sa: "sa \<in> S" "A \<inter> sa \<noteq> {}"
  1940     using Alap by auto
  1941   obtain sb where sb: "sb \<in> S" "B \<inter> sb \<noteq> {}"
  1942     using Blap by auto
  1943   obtain x where x: "\<And>s. s \<in> S \<Longrightarrow> x \<in> s"
  1944     using ne by auto
  1945   then have "x \<in> \<Union>S"
  1946     using \<open>sa \<in> S\<close> by blast
  1947   then have "x \<in> A \<or> x \<in> B"
  1948     using cover by auto
  1949   then show False
  1950     using cs [unfolded connected_def]
  1951     by (metis A B IntI Sup_upper sa sb disjs x cover empty_iff subset_trans)
  1952 qed
  1953 
  1954 lemma connected_Un: "\<lbrakk>connected s; connected t; s \<inter> t \<noteq> {}\<rbrakk> \<Longrightarrow> connected (s \<union> t)"
  1955   using connected_Union [of "{s,t}"] by auto
  1956 
  1957 lemma connected_diff_open_from_closed:
  1958   assumes st: "s \<subseteq> t" and tu: "t \<subseteq> u" and s: "open s"
  1959       and t: "closed t" and u: "connected u" and ts: "connected (t - s)"
  1960   shows "connected(u - s)"
  1961 proof (rule connectedI)
  1962   fix A B
  1963   assume AB: "open A" "open B" "A \<inter> (u - s) \<noteq> {}" "B \<inter> (u - s) \<noteq> {}"
  1964      and disj: "A \<inter> B \<inter> (u - s) = {}" and cover: "u - s \<subseteq> A \<union> B"
  1965   then consider "A \<inter> (t - s) = {}" | "B \<inter> (t - s) = {}"
  1966     using st ts tu connectedD [of "t-s" "A" "B"]
  1967     by auto
  1968   then show False
  1969   proof cases
  1970     case 1
  1971     then have "(A - t) \<inter> (B \<union> s) \<inter> u = {}"
  1972       using disj st by auto
  1973     moreover have  "u \<subseteq> (A - t) \<union> (B \<union> s)" using 1 cover by auto
  1974     ultimately show False
  1975       using connectedD [of u "A - t" "B \<union> s"] AB s t 1 u
  1976       by auto
  1977   next
  1978     case 2
  1979     then have "(A \<union> s) \<inter> (B - t) \<inter> u = {}"
  1980       using disj st
  1981       by auto
  1982     moreover have "u \<subseteq> (A \<union> s) \<union> (B - t)" using 2 cover by auto
  1983     ultimately show False
  1984       using connectedD [of u "A \<union> s" "B - t"] AB s t 2 u
  1985       by auto
  1986   qed
  1987 qed
  1988 
  1989 lemma connected_iff_const:
  1990   fixes S :: "'a::topological_space set"
  1991   shows "connected S \<longleftrightarrow> (\<forall>P::'a \<Rightarrow> bool. continuous_on S P \<longrightarrow> (\<exists>c. \<forall>s\<in>S. P s = c))"
  1992 proof safe
  1993   fix P :: "'a \<Rightarrow> bool" assume "connected S" "continuous_on S P"
  1994   then have "\<And>b. \<exists>A. open A \<and> A \<inter> S = P -` {b} \<inter> S"
  1995     unfolding continuous_on_open_invariant by simp
  1996   from this[of True] this[of False]
  1997   obtain t f where "open t" "open f" and *: "f \<inter> S = P -` {False} \<inter> S" "t \<inter> S = P -` {True} \<inter> S"
  1998     by auto
  1999   then have "t \<inter> S = {} \<or> f \<inter> S = {}"
  2000     by (intro connectedD[OF \<open>connected S\<close>])  auto
  2001   then show "\<exists>c. \<forall>s\<in>S. P s = c"
  2002   proof (rule disjE)
  2003     assume "t \<inter> S = {}" then show ?thesis
  2004       unfolding * by (intro exI[of _ False]) auto
  2005   next
  2006     assume "f \<inter> S = {}" then show ?thesis
  2007       unfolding * by (intro exI[of _ True]) auto
  2008   qed
  2009 next
  2010   assume P: "\<forall>P::'a \<Rightarrow> bool. continuous_on S P \<longrightarrow> (\<exists>c. \<forall>s\<in>S. P s = c)"
  2011   show "connected S"
  2012   proof (rule connectedI)
  2013     fix A B assume *: "open A" "open B" "A \<inter> S \<noteq> {}" "B \<inter> S \<noteq> {}" "A \<inter> B \<inter> S = {}" "S \<subseteq> A \<union> B"
  2014     have "continuous_on S (\<lambda>x. x \<in> A)"
  2015       unfolding continuous_on_open_invariant
  2016     proof safe
  2017       fix C :: "bool set"
  2018       have "C = UNIV \<or> C = {True} \<or> C = {False} \<or> C = {}"
  2019         using subset_UNIV[of C] unfolding UNIV_bool by auto
  2020       with * show "\<exists>T. open T \<and> T \<inter> S = (\<lambda>x. x \<in> A) -` C \<inter> S"
  2021         by (intro exI[of _ "(if True \<in> C then A else {}) \<union> (if False \<in> C then B else {})"]) auto
  2022     qed
  2023     from P[rule_format, OF this] obtain c where "\<And>s. s \<in> S \<Longrightarrow> (s \<in> A) = c" by blast
  2024     with * show False
  2025       by (cases c) auto
  2026   qed
  2027 qed
  2028 
  2029 lemma connectedD_const:
  2030   fixes P :: "'a::topological_space \<Rightarrow> bool"
  2031   shows "connected S \<Longrightarrow> continuous_on S P \<Longrightarrow> \<exists>c. \<forall>s\<in>S. P s = c"
  2032   unfolding connected_iff_const by auto
  2033 
  2034 lemma connectedI_const:
  2035   "(\<And>P::'a::topological_space \<Rightarrow> bool. continuous_on S P \<Longrightarrow> \<exists>c. \<forall>s\<in>S. P s = c) \<Longrightarrow> connected S"
  2036   unfolding connected_iff_const by auto
  2037 
  2038 lemma connected_local_const:
  2039   assumes "connected A" "a \<in> A" "b \<in> A"
  2040   assumes *: "\<forall>a\<in>A. eventually (\<lambda>b. f a = f b) (at a within A)"
  2041   shows "f a = f b"
  2042 proof -
  2043   obtain S where S: "\<And>a. a \<in> A \<Longrightarrow> a \<in> S a" "\<And>a. a \<in> A \<Longrightarrow> open (S a)"
  2044     "\<And>a x. a \<in> A \<Longrightarrow> x \<in> S a \<Longrightarrow> x \<in> A \<Longrightarrow> f a = f x"
  2045     using * unfolding eventually_at_topological by metis
  2046 
  2047   let ?P = "\<Union>b\<in>{b\<in>A. f a = f b}. S b" and ?N = "\<Union>b\<in>{b\<in>A. f a \<noteq> f b}. S b"
  2048   have "?P \<inter> A = {} \<or> ?N \<inter> A = {}"
  2049     using \<open>connected A\<close> S \<open>a\<in>A\<close>
  2050     by (intro connectedD) (auto, metis)
  2051   then show "f a = f b"
  2052   proof
  2053     assume "?N \<inter> A = {}"
  2054     then have "\<forall>x\<in>A. f a = f x"
  2055       using S(1) by auto
  2056     with \<open>b\<in>A\<close> show ?thesis by auto
  2057   next
  2058     assume "?P \<inter> A = {}" then show ?thesis
  2059       using \<open>a \<in> A\<close> S(1)[of a] by auto
  2060   qed
  2061 qed
  2062 
  2063 lemma (in linorder_topology) connectedD_interval:
  2064   assumes "connected U" and xy: "x \<in> U" "y \<in> U" and "x \<le> z" "z \<le> y"
  2065   shows "z \<in> U"
  2066 proof -
  2067   have eq: "{..<z} \<union> {z<..} = - {z}"
  2068     by auto
  2069   { assume "z \<notin> U" "x < z" "z < y"
  2070     with xy have "\<not> connected U"
  2071       unfolding connected_def simp_thms
  2072       apply (rule_tac exI[of _ "{..< z}"])
  2073       apply (rule_tac exI[of _ "{z <..}"])
  2074       apply (auto simp add: eq)
  2075       done }
  2076   with assms show "z \<in> U"
  2077     by (metis less_le)
  2078 qed
  2079 
  2080 lemma connected_continuous_image:
  2081   assumes *: "continuous_on s f"
  2082   assumes "connected s"
  2083   shows "connected (f ` s)"
  2084 proof (rule connectedI_const)
  2085   fix P :: "'b \<Rightarrow> bool" assume "continuous_on (f ` s) P"
  2086   then have "continuous_on s (P \<circ> f)"
  2087     by (rule continuous_on_compose[OF *])
  2088   from connectedD_const[OF \<open>connected s\<close> this] show "\<exists>c. \<forall>s\<in>f ` s. P s = c"
  2089     by auto
  2090 qed
  2091 
  2092 
  2093 section \<open>Linear Continuum Topologies\<close>
  2094 
  2095 class linear_continuum_topology = linorder_topology + linear_continuum
  2096 begin
  2097 
  2098 lemma Inf_notin_open:
  2099   assumes A: "open A" and bnd: "\<forall>a\<in>A. x < a"
  2100   shows "Inf A \<notin> A"
  2101 proof
  2102   assume "Inf A \<in> A"
  2103   then obtain b where "b < Inf A" "{b <.. Inf A} \<subseteq> A"
  2104     using open_left[of A "Inf A" x] assms by auto
  2105   with dense[of b "Inf A"] obtain c where "c < Inf A" "c \<in> A"
  2106     by (auto simp: subset_eq)
  2107   then show False
  2108     using cInf_lower[OF \<open>c \<in> A\<close>] bnd by (metis not_le less_imp_le bdd_belowI)
  2109 qed
  2110 
  2111 lemma Sup_notin_open:
  2112   assumes A: "open A" and bnd: "\<forall>a\<in>A. a < x"
  2113   shows "Sup A \<notin> A"
  2114 proof
  2115   assume "Sup A \<in> A"
  2116   then obtain b where "Sup A < b" "{Sup A ..< b} \<subseteq> A"
  2117     using open_right[of A "Sup A" x] assms by auto
  2118   with dense[of "Sup A" b] obtain c where "Sup A < c" "c \<in> A"
  2119     by (auto simp: subset_eq)
  2120   then show False
  2121     using cSup_upper[OF \<open>c \<in> A\<close>] bnd by (metis less_imp_le not_le bdd_aboveI)
  2122 qed
  2123 
  2124 end
  2125 
  2126 instance linear_continuum_topology \<subseteq> perfect_space
  2127 proof
  2128   fix x :: 'a
  2129   obtain y where "x < y \<or> y < x"
  2130     using ex_gt_or_lt [of x] ..
  2131   with Inf_notin_open[of "{x}" y] Sup_notin_open[of "{x}" y]
  2132   show "\<not> open {x}"
  2133     by auto
  2134 qed
  2135 
  2136 lemma connectedI_interval:
  2137   fixes U :: "'a :: linear_continuum_topology set"
  2138   assumes *: "\<And>x y z. x \<in> U \<Longrightarrow> y \<in> U \<Longrightarrow> x \<le> z \<Longrightarrow> z \<le> y \<Longrightarrow> z \<in> U"
  2139   shows "connected U"
  2140 proof (rule connectedI)
  2141   { fix A B assume "open A" "open B" "A \<inter> B \<inter> U = {}" "U \<subseteq> A \<union> B"
  2142     fix x y assume "x < y" "x \<in> A" "y \<in> B" "x \<in> U" "y \<in> U"
  2143 
  2144     let ?z = "Inf (B \<inter> {x <..})"
  2145 
  2146     have "x \<le> ?z" "?z \<le> y"
  2147       using \<open>y \<in> B\<close> \<open>x < y\<close> by (auto intro: cInf_lower cInf_greatest)
  2148     with \<open>x \<in> U\<close> \<open>y \<in> U\<close> have "?z \<in> U"
  2149       by (rule *)
  2150     moreover have "?z \<notin> B \<inter> {x <..}"
  2151       using \<open>open B\<close> by (intro Inf_notin_open) auto
  2152     ultimately have "?z \<in> A"
  2153       using \<open>x \<le> ?z\<close> \<open>A \<inter> B \<inter> U = {}\<close> \<open>x \<in> A\<close> \<open>U \<subseteq> A \<union> B\<close> by auto
  2154 
  2155     { assume "?z < y"
  2156       obtain a where "?z < a" "{?z ..< a} \<subseteq> A"
  2157         using open_right[OF \<open>open A\<close> \<open>?z \<in> A\<close> \<open>?z < y\<close>] by auto
  2158       moreover obtain b where "b \<in> B" "x < b" "b < min a y"
  2159         using cInf_less_iff[of "B \<inter> {x <..}" "min a y"] \<open>?z < a\<close> \<open>?z < y\<close> \<open>x < y\<close> \<open>y \<in> B\<close>
  2160         by (auto intro: less_imp_le)
  2161       moreover have "?z \<le> b"
  2162         using \<open>b \<in> B\<close> \<open>x < b\<close>
  2163         by (intro cInf_lower) auto
  2164       moreover have "b \<in> U"
  2165         using \<open>x \<le> ?z\<close> \<open>?z \<le> b\<close> \<open>b < min a y\<close>
  2166         by (intro *[OF \<open>x \<in> U\<close> \<open>y \<in> U\<close>]) (auto simp: less_imp_le)
  2167       ultimately have "\<exists>b\<in>B. b \<in> A \<and> b \<in> U"
  2168         by (intro bexI[of _ b]) auto }
  2169     then have False
  2170       using \<open>?z \<le> y\<close> \<open>?z \<in> A\<close> \<open>y \<in> B\<close> \<open>y \<in> U\<close> \<open>A \<inter> B \<inter> U = {}\<close> unfolding le_less by blast }
  2171   note not_disjoint = this
  2172 
  2173   fix A B assume AB: "open A" "open B" "U \<subseteq> A \<union> B" "A \<inter> B \<inter> U = {}"
  2174   moreover assume "A \<inter> U \<noteq> {}" then obtain x where x: "x \<in> U" "x \<in> A" by auto
  2175   moreover assume "B \<inter> U \<noteq> {}" then obtain y where y: "y \<in> U" "y \<in> B" by auto
  2176   moreover note not_disjoint[of B A y x] not_disjoint[of A B x y]
  2177   ultimately show False by (cases x y rule: linorder_cases) auto
  2178 qed
  2179 
  2180 lemma connected_iff_interval:
  2181   fixes U :: "'a :: linear_continuum_topology set"
  2182   shows "connected U \<longleftrightarrow> (\<forall>x\<in>U. \<forall>y\<in>U. \<forall>z. x \<le> z \<longrightarrow> z \<le> y \<longrightarrow> z \<in> U)"
  2183   by (auto intro: connectedI_interval dest: connectedD_interval)
  2184 
  2185 lemma connected_UNIV[simp]: "connected (UNIV::'a::linear_continuum_topology set)"
  2186   unfolding connected_iff_interval by auto
  2187 
  2188 lemma connected_Ioi[simp]: "connected {a::'a::linear_continuum_topology <..}"
  2189   unfolding connected_iff_interval by auto
  2190 
  2191 lemma connected_Ici[simp]: "connected {a::'a::linear_continuum_topology ..}"
  2192   unfolding connected_iff_interval by auto
  2193 
  2194 lemma connected_Iio[simp]: "connected {..< a::'a::linear_continuum_topology}"
  2195   unfolding connected_iff_interval by auto
  2196 
  2197 lemma connected_Iic[simp]: "connected {.. a::'a::linear_continuum_topology}"
  2198   unfolding connected_iff_interval by auto
  2199 
  2200 lemma connected_Ioo[simp]: "connected {a <..< b::'a::linear_continuum_topology}"
  2201   unfolding connected_iff_interval by auto
  2202 
  2203 lemma connected_Ioc[simp]: "connected {a <.. b::'a::linear_continuum_topology}"
  2204   unfolding connected_iff_interval by auto
  2205 
  2206 lemma connected_Ico[simp]: "connected {a ..< b::'a::linear_continuum_topology}"
  2207   unfolding connected_iff_interval by auto
  2208 
  2209 lemma connected_Icc[simp]: "connected {a .. b::'a::linear_continuum_topology}"
  2210   unfolding connected_iff_interval by auto
  2211 
  2212 lemma connected_contains_Ioo:
  2213   fixes A :: "'a :: linorder_topology set"
  2214   assumes A: "connected A" "a \<in> A" "b \<in> A" shows "{a <..< b} \<subseteq> A"
  2215   using connectedD_interval[OF A] by (simp add: subset_eq Ball_def less_imp_le)
  2216 
  2217 lemma connected_contains_Icc:
  2218   assumes "connected (A :: ('a :: {linorder_topology}) set)" "a \<in> A" "b \<in> A"
  2219   shows   "{a..b} \<subseteq> A"
  2220 proof
  2221   fix x assume "x \<in> {a..b}"
  2222   hence "x = a \<or> x = b \<or> x \<in> {a<..<b}" by auto
  2223   thus "x \<in> A" using assms connected_contains_Ioo[of A a b] by auto
  2224 qed
  2225 
  2226 subsection \<open>Intermediate Value Theorem\<close>
  2227 
  2228 lemma IVT':
  2229   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2230   assumes y: "f a \<le> y" "y \<le> f b" "a \<le> b"
  2231   assumes *: "continuous_on {a .. b} f"
  2232   shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2233 proof -
  2234   have "connected {a..b}"
  2235     unfolding connected_iff_interval by auto
  2236   from connected_continuous_image[OF * this, THEN connectedD_interval, of "f a" "f b" y] y
  2237   show ?thesis
  2238     by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
  2239 qed
  2240 
  2241 lemma IVT2':
  2242   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2243   assumes y: "f b \<le> y" "y \<le> f a" "a \<le> b"
  2244   assumes *: "continuous_on {a .. b} f"
  2245   shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2246 proof -
  2247   have "connected {a..b}"
  2248     unfolding connected_iff_interval by auto
  2249   from connected_continuous_image[OF * this, THEN connectedD_interval, of "f b" "f a" y] y
  2250   show ?thesis
  2251     by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
  2252 qed
  2253 
  2254 lemma IVT:
  2255   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2256   shows "f a \<le> y \<Longrightarrow> y \<le> f b \<Longrightarrow> a \<le> b \<Longrightarrow> (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x) \<Longrightarrow> \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2257   by (rule IVT') (auto intro: continuous_at_imp_continuous_on)
  2258 
  2259 lemma IVT2:
  2260   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2261   shows "f b \<le> y \<Longrightarrow> y \<le> f a \<Longrightarrow> a \<le> b \<Longrightarrow> (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x) \<Longrightarrow> \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2262   by (rule IVT2') (auto intro: continuous_at_imp_continuous_on)
  2263 
  2264 lemma continuous_inj_imp_mono:
  2265   fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2266   assumes x: "a < x" "x < b"
  2267   assumes cont: "continuous_on {a..b} f"
  2268   assumes inj: "inj_on f {a..b}"
  2269   shows "(f a < f x \<and> f x < f b) \<or> (f b < f x \<and> f x < f a)"
  2270 proof -
  2271   note I = inj_on_eq_iff[OF inj]
  2272   { assume "f x < f a" "f x < f b"
  2273     then obtain s t where "x \<le> s" "s \<le> b" "a \<le> t" "t \<le> x" "f s = f t" "f x < f s"
  2274       using IVT'[of f x "min (f a) (f b)" b] IVT2'[of f x "min (f a) (f b)" a] x
  2275       by (auto simp: continuous_on_subset[OF cont] less_imp_le)
  2276     with x I have False by auto }
  2277   moreover
  2278   { assume "f a < f x" "f b < f x"
  2279     then obtain s t where "x \<le> s" "s \<le> b" "a \<le> t" "t \<le> x" "f s = f t" "f s < f x"
  2280       using IVT'[of f a "max (f a) (f b)" x] IVT2'[of f b "max (f a) (f b)" x] x
  2281       by (auto simp: continuous_on_subset[OF cont] less_imp_le)
  2282     with x I have False by auto }
  2283   ultimately show ?thesis
  2284     using I[of a x] I[of x b] x less_trans[OF x] by (auto simp add: le_less less_imp_neq neq_iff)
  2285 qed
  2286 
  2287 lemma continuous_at_Sup_mono:
  2288   fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder} \<Rightarrow> 'b :: {linorder_topology, conditionally_complete_linorder}"
  2289   assumes "mono f"
  2290   assumes cont: "continuous (at_left (Sup S)) f"
  2291   assumes S: "S \<noteq> {}" "bdd_above S"
  2292   shows "f (Sup S) = (SUP s:S. f s)"
  2293 proof (rule antisym)
  2294   have f: "(f \<longlongrightarrow> f (Sup S)) (at_left (Sup S))"
  2295     using cont unfolding continuous_within .
  2296 
  2297   show "f (Sup S) \<le> (SUP s:S. f s)"
  2298   proof cases
  2299     assume "Sup S \<in> S" then show ?thesis
  2300       by (rule cSUP_upper) (auto intro: bdd_above_image_mono S \<open>mono f\<close>)
  2301   next
  2302     assume "Sup S \<notin> S"
  2303     from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
  2304       by auto
  2305     with \<open>Sup S \<notin> S\<close> S have "s < Sup S"
  2306       unfolding less_le by (blast intro: cSup_upper)
  2307     show ?thesis
  2308     proof (rule ccontr)
  2309       assume "\<not> ?thesis"
  2310       with order_tendstoD(1)[OF f, of "SUP s:S. f s"] obtain b where "b < Sup S"
  2311         and *: "\<And>y. b < y \<Longrightarrow> y < Sup S \<Longrightarrow> (SUP s:S. f s) < f y"
  2312         by (auto simp: not_le eventually_at_left[OF \<open>s < Sup S\<close>])
  2313       with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "b < c"
  2314         using less_cSupD[of S b] by auto
  2315       with \<open>Sup S \<notin> S\<close> S have "c < Sup S"
  2316         unfolding less_le by (blast intro: cSup_upper)
  2317       from *[OF \<open>b < c\<close> \<open>c < Sup S\<close>] cSUP_upper[OF \<open>c \<in> S\<close> bdd_above_image_mono[of f]]
  2318       show False
  2319         by (auto simp: assms)
  2320     qed
  2321   qed
  2322 qed (intro cSUP_least \<open>mono f\<close>[THEN monoD] cSup_upper S)
  2323 
  2324 lemma continuous_at_Sup_antimono:
  2325   fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder} \<Rightarrow> 'b :: {linorder_topology, conditionally_complete_linorder}"
  2326   assumes "antimono f"
  2327   assumes cont: "continuous (at_left (Sup S)) f"
  2328   assumes S: "S \<noteq> {}" "bdd_above S"
  2329   shows "f (Sup S) = (INF s:S. f s)"
  2330 proof (rule antisym)
  2331   have f: "(f \<longlongrightarrow> f (Sup S)) (at_left (Sup S))"
  2332     using cont unfolding continuous_within .
  2333 
  2334   show "(INF s:S. f s) \<le> f (Sup S)"
  2335   proof cases
  2336     assume "Sup S \<in> S" then show ?thesis
  2337       by (intro cINF_lower) (auto intro: bdd_below_image_antimono S \<open>antimono f\<close>)
  2338   next
  2339     assume "Sup S \<notin> S"
  2340     from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
  2341       by auto
  2342     with \<open>Sup S \<notin> S\<close> S have "s < Sup S"
  2343       unfolding less_le by (blast intro: cSup_upper)
  2344     show ?thesis
  2345     proof (rule ccontr)
  2346       assume "\<not> ?thesis"
  2347       with order_tendstoD(2)[OF f, of "INF s:S. f s"] obtain b where "b < Sup S"
  2348         and *: "\<And>y. b < y \<Longrightarrow> y < Sup S \<Longrightarrow> f y < (INF s:S. f s)"
  2349         by (auto simp: not_le eventually_at_left[OF \<open>s < Sup S\<close>])
  2350       with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "b < c"
  2351         using less_cSupD[of S b] by auto
  2352       with \<open>Sup S \<notin> S\<close> S have "c < Sup S"
  2353         unfolding less_le by (blast intro: cSup_upper)
  2354       from *[OF \<open>b < c\<close> \<open>c < Sup S\<close>] cINF_lower[OF bdd_below_image_antimono, of f S c] \<open>c \<in> S\<close>
  2355       show False
  2356         by (auto simp: assms)
  2357     qed
  2358   qed
  2359 qed (intro cINF_greatest \<open>antimono f\<close>[THEN antimonoD] cSup_upper S)
  2360 
  2361 lemma continuous_at_Inf_mono:
  2362   fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder} \<Rightarrow> 'b :: {linorder_topology, conditionally_complete_linorder}"
  2363   assumes "mono f"
  2364   assumes cont: "continuous (at_right (Inf S)) f"
  2365   assumes S: "S \<noteq> {}" "bdd_below S"
  2366   shows "f (Inf S) = (INF s:S. f s)"
  2367 proof (rule antisym)
  2368   have f: "(f \<longlongrightarrow> f (Inf S)) (at_right (Inf S))"
  2369     using cont unfolding continuous_within .
  2370 
  2371   show "(INF s:S. f s) \<le> f (Inf S)"
  2372   proof cases
  2373     assume "Inf S \<in> S" then show ?thesis
  2374       by (rule cINF_lower[rotated]) (auto intro: bdd_below_image_mono S \<open>mono f\<close>)
  2375   next
  2376     assume "Inf S \<notin> S"
  2377     from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
  2378       by auto
  2379     with \<open>Inf S \<notin> S\<close> S have "Inf S < s"
  2380       unfolding less_le by (blast intro: cInf_lower)
  2381     show ?thesis
  2382     proof (rule ccontr)
  2383       assume "\<not> ?thesis"
  2384       with order_tendstoD(2)[OF f, of "INF s:S. f s"] obtain b where "Inf S < b"
  2385         and *: "\<And>y. Inf S < y \<Longrightarrow> y < b \<Longrightarrow> f y < (INF s:S. f s)"
  2386         by (auto simp: not_le eventually_at_right[OF \<open>Inf S < s\<close>])
  2387       with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "c < b"
  2388         using cInf_lessD[of S b] by auto
  2389       with \<open>Inf S \<notin> S\<close> S have "Inf S < c"
  2390         unfolding less_le by (blast intro: cInf_lower)
  2391       from *[OF \<open>Inf S < c\<close> \<open>c < b\<close>] cINF_lower[OF bdd_below_image_mono[of f] \<open>c \<in> S\<close>]
  2392       show False
  2393         by (auto simp: assms)
  2394     qed
  2395   qed
  2396 qed (intro cINF_greatest \<open>mono f\<close>[THEN monoD] cInf_lower \<open>bdd_below S\<close> \<open>S \<noteq> {}\<close>)
  2397 
  2398 lemma continuous_at_Inf_antimono:
  2399   fixes f :: "'a :: {linorder_topology, conditionally_complete_linorder} \<Rightarrow> 'b :: {linorder_topology, conditionally_complete_linorder}"
  2400   assumes "antimono f"
  2401   assumes cont: "continuous (at_right (Inf S)) f"
  2402   assumes S: "S \<noteq> {}" "bdd_below S"
  2403   shows "f (Inf S) = (SUP s:S. f s)"
  2404 proof (rule antisym)
  2405   have f: "(f \<longlongrightarrow> f (Inf S)) (at_right (Inf S))"
  2406     using cont unfolding continuous_within .
  2407 
  2408   show "f (Inf S) \<le> (SUP s:S. f s)"
  2409   proof cases
  2410     assume "Inf S \<in> S" then show ?thesis
  2411       by (rule cSUP_upper) (auto intro: bdd_above_image_antimono S \<open>antimono f\<close>)
  2412   next
  2413     assume "Inf S \<notin> S"
  2414     from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
  2415       by auto
  2416     with \<open>Inf S \<notin> S\<close> S have "Inf S < s"
  2417       unfolding less_le by (blast intro: cInf_lower)
  2418     show ?thesis
  2419     proof (rule ccontr)
  2420       assume "\<not> ?thesis"
  2421       with order_tendstoD(1)[OF f, of "SUP s:S. f s"] obtain b where "Inf S < b"
  2422         and *: "\<And>y. Inf S < y \<Longrightarrow> y < b \<Longrightarrow> (SUP s:S. f s) < f y"
  2423         by (auto simp: not_le eventually_at_right[OF \<open>Inf S < s\<close>])
  2424       with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "c < b"
  2425         using cInf_lessD[of S b] by auto
  2426       with \<open>Inf S \<notin> S\<close> S have "Inf S < c"
  2427         unfolding less_le by (blast intro: cInf_lower)
  2428       from *[OF \<open>Inf S < c\<close> \<open>c < b\<close>] cSUP_upper[OF \<open>c \<in> S\<close> bdd_above_image_antimono[of f]]
  2429       show False
  2430         by (auto simp: assms)
  2431     qed
  2432   qed
  2433 qed (intro cSUP_least \<open>antimono f\<close>[THEN antimonoD] cInf_lower S)
  2434 
  2435 subsection \<open>Uniform spaces\<close>
  2436 
  2437 class uniformity =
  2438   fixes uniformity :: "('a \<times> 'a) filter"
  2439 begin
  2440 
  2441 abbreviation uniformity_on :: "'a set \<Rightarrow> ('a \<times> 'a) filter" where
  2442   "uniformity_on s \<equiv> inf uniformity (principal (s\<times>s))"
  2443 
  2444 end
  2445 
  2446 lemma uniformity_Abort:
  2447   "uniformity = Abs_filter (\<lambda>P. Code.abort (STR ''uniformity is not executable'') (\<lambda>x. Rep_filter uniformity P))"
  2448   unfolding Code.abort_def Rep_filter_inverse ..
  2449 
  2450 class open_uniformity = "open" + uniformity +
  2451   assumes open_uniformity: "\<And>U. open U \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"
  2452 
  2453 class uniform_space = open_uniformity +
  2454   assumes uniformity_refl: "eventually E uniformity \<Longrightarrow> E (x, x)"
  2455   assumes uniformity_sym: "eventually E uniformity \<Longrightarrow> eventually (\<lambda>(x, y). E (y, x)) uniformity"
  2456   assumes uniformity_trans: "eventually E uniformity \<Longrightarrow> \<exists>D. eventually D uniformity \<and> (\<forall>x y z. D (x, y) \<longrightarrow> D (y, z) \<longrightarrow> E (x, z))"
  2457 begin
  2458 
  2459 subclass topological_space
  2460   proof qed (force elim: eventually_mono eventually_elim2 simp: split_beta' open_uniformity)+
  2461 
  2462 lemma uniformity_bot: "uniformity \<noteq> bot"
  2463   using uniformity_refl by auto
  2464 
  2465 lemma uniformity_trans':
  2466   "eventually E uniformity \<Longrightarrow> eventually (\<lambda>((x, y), (y', z)). y = y' \<longrightarrow> E (x, z)) (uniformity \<times>\<^sub>F uniformity)"
  2467   by (drule uniformity_trans) (auto simp add: eventually_prod_same)
  2468 
  2469 lemma uniformity_transE:
  2470   assumes E: "eventually E uniformity"
  2471   obtains D where "eventually D uniformity" "\<And>x y z. D (x, y) \<Longrightarrow> D (y, z) \<Longrightarrow> E (x, z)"
  2472   using uniformity_trans[OF E] by auto
  2473 
  2474 lemma eventually_nhds_uniformity:
  2475   "eventually P (nhds x) \<longleftrightarrow> eventually (\<lambda>(x', y). x' = x \<longrightarrow> P y) uniformity" (is "_ \<longleftrightarrow> ?N P x")
  2476   unfolding eventually_nhds
  2477 proof safe
  2478   assume *: "?N P x"
  2479   { fix x assume "?N P x"
  2480     then guess D by (rule uniformity_transE) note D = this
  2481     from D(1) have "?N (?N P) x"
  2482       by eventually_elim (insert D, force elim: eventually_mono split: prod.split) }
  2483   then have "open {x. ?N P x}"
  2484     by (simp add: open_uniformity)
  2485   then show "\<exists>S. open S \<and> x \<in> S \<and> (\<forall>x\<in>S. P x)"
  2486     by (intro exI[of _ "{x. ?N P x}"]) (auto dest: uniformity_refl simp: *)
  2487 qed (force simp add: open_uniformity elim: eventually_mono)
  2488 
  2489 subsubsection \<open>Totally bounded sets\<close>
  2490 
  2491 definition totally_bounded :: "'a set \<Rightarrow> bool" where
  2492   "totally_bounded S \<longleftrightarrow>
  2493     (\<forall>E. eventually E uniformity \<longrightarrow> (\<exists>X. finite X \<and> (\<forall>s\<in>S. \<exists>x\<in>X. E (x, s))))"
  2494 
  2495 lemma totally_bounded_empty[iff]: "totally_bounded {}"
  2496   by (auto simp add: totally_bounded_def)
  2497 
  2498 lemma totally_bounded_subset: "totally_bounded S \<Longrightarrow> T \<subseteq> S \<Longrightarrow> totally_bounded T"
  2499   by (force simp add: totally_bounded_def)
  2500 
  2501 lemma totally_bounded_Union[intro]:
  2502   assumes M: "finite M" "\<And>S. S \<in> M \<Longrightarrow> totally_bounded S" shows "totally_bounded (\<Union>M)"
  2503   unfolding totally_bounded_def
  2504 proof safe
  2505   fix E assume "eventually E uniformity"
  2506   with M obtain X where "\<forall>S\<in>M. finite (X S) \<and> (\<forall>s\<in>S. \<exists>x\<in>X S. E (x, s))"
  2507     by (metis totally_bounded_def)
  2508   with `finite M` show "\<exists>X. finite X \<and> (\<forall>s\<in>\<Union>M. \<exists>x\<in>X. E (x, s))"
  2509     by (intro exI[of _ "\<Union>S\<in>M. X S"]) force
  2510 qed
  2511 
  2512 subsubsection \<open>Cauchy filter\<close>
  2513 
  2514 definition cauchy_filter :: "'a filter \<Rightarrow> bool" where
  2515   "cauchy_filter F \<longleftrightarrow> F \<times>\<^sub>F F \<le> uniformity"
  2516 
  2517 definition Cauchy :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
  2518   Cauchy_uniform: "Cauchy X = cauchy_filter (filtermap X sequentially)"
  2519 
  2520 lemma Cauchy_uniform_iff:
  2521   "Cauchy X \<longleftrightarrow> (\<forall>P. eventually P uniformity \<longrightarrow> (\<exists>N. \<forall>n\<ge>N. \<forall>m\<ge>N. P (X n, X m)))"
  2522   unfolding Cauchy_uniform cauchy_filter_def le_filter_def eventually_prod_same
  2523     eventually_filtermap eventually_sequentially
  2524 proof safe
  2525   let ?U = "\<lambda>P. eventually P uniformity"
  2526   { fix P assume "?U P" "\<forall>P. ?U P \<longrightarrow> (\<exists>Q. (\<exists>N. \<forall>n\<ge>N. Q (X n)) \<and> (\<forall>x y. Q x \<longrightarrow> Q y \<longrightarrow> P (x, y)))"
  2527     then obtain Q N where "\<And>n. n \<ge> N \<Longrightarrow> Q (X n)" "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> P (x, y)"
  2528       by metis
  2529     then show "\<exists>N. \<forall>n\<ge>N. \<forall>m\<ge>N. P (X n, X m)"
  2530       by blast }
  2531   { fix P assume "?U P" and P: "\<forall>P. ?U P \<longrightarrow> (\<exists>N. \<forall>n\<ge>N. \<forall>m\<ge>N. P (X n, X m))"
  2532     then obtain Q where "?U Q" and Q: "\<And>x y z. Q (x, y) \<Longrightarrow> Q (y, z) \<Longrightarrow> P (x, z)"
  2533       by (auto elim: uniformity_transE)
  2534     then have "?U (\<lambda>x. Q x \<and> (\<lambda>(x, y). Q (y, x)) x)"
  2535       unfolding eventually_conj_iff by (simp add: uniformity_sym)
  2536     from P[rule_format, OF this]
  2537     obtain N where N: "\<And>n m. n \<ge> N \<Longrightarrow> m \<ge> N \<Longrightarrow> Q (X n, X m) \<and> Q (X m, X n)"
  2538       by auto
  2539     show "\<exists>Q. (\<exists>N. \<forall>n\<ge>N. Q (X n)) \<and> (\<forall>x y. Q x \<longrightarrow> Q y \<longrightarrow> P (x, y))"
  2540     proof (safe intro!: exI[of _ "\<lambda>x. \<forall>n\<ge>N. Q (x, X n) \<and> Q (X n, x)"] exI[of _ N] N)
  2541       fix x y assume "\<forall>n\<ge>N. Q (x, X n) \<and> Q (X n, x)" "\<forall>n\<ge>N. Q (y, X n) \<and> Q (X n, y)"
  2542       then have "Q (x, X N)" "Q (X N, y)" by auto
  2543       then show "P (x, y)"
  2544         by (rule Q)
  2545     qed }
  2546 qed
  2547 
  2548 lemma nhds_imp_cauchy_filter:
  2549   assumes *: "F \<le> nhds x" shows "cauchy_filter F"
  2550 proof -
  2551   have "F \<times>\<^sub>F F \<le> nhds x \<times>\<^sub>F nhds x"
  2552     by (intro prod_filter_mono *)
  2553   also have "\<dots> \<le> uniformity"
  2554     unfolding le_filter_def eventually_nhds_uniformity eventually_prod_same
  2555   proof safe
  2556     fix P assume "eventually P uniformity"
  2557     then guess Ql by (rule uniformity_transE) note Ql = this
  2558     moreover note Ql(1)[THEN uniformity_sym]
  2559     ultimately show "\<exists>Q. eventually (\<lambda>(x', y). x' = x \<longrightarrow> Q y) uniformity \<and> (\<forall>x y. Q x \<longrightarrow> Q y \<longrightarrow> P (x, y))"
  2560       by (rule_tac exI[of _ "\<lambda>y. Ql (y, x) \<and> Ql (x, y)"]) (fastforce elim: eventually_elim2)
  2561   qed
  2562   finally show ?thesis
  2563     by (simp add: cauchy_filter_def)
  2564 qed
  2565 
  2566 lemma LIMSEQ_imp_Cauchy: "X \<longlonglongrightarrow> x \<Longrightarrow> Cauchy X"
  2567   unfolding Cauchy_uniform filterlim_def by (intro nhds_imp_cauchy_filter)
  2568 
  2569 lemma Cauchy_subseq_Cauchy: assumes "Cauchy X" "subseq f" shows "Cauchy (X \<circ> f)"
  2570   unfolding Cauchy_uniform comp_def filtermap_filtermap[symmetric] cauchy_filter_def
  2571   by (rule order_trans[OF _ \<open>Cauchy X\<close>[unfolded Cauchy_uniform cauchy_filter_def]])
  2572      (intro prod_filter_mono filtermap_mono filterlim_subseq[OF \<open>subseq f\<close>, unfolded filterlim_def])
  2573 
  2574 lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"
  2575   unfolding convergent_def by (erule exE, erule LIMSEQ_imp_Cauchy)
  2576 
  2577 definition complete :: "'a set \<Rightarrow> bool" where
  2578   complete_uniform: "complete S \<longleftrightarrow> (\<forall>F \<le> principal S. F \<noteq> bot \<longrightarrow> cauchy_filter F \<longrightarrow> (\<exists>x\<in>S. F \<le> nhds x))"
  2579 
  2580 end
  2581 
  2582 subsubsection \<open>Uniformly continuous functions\<close>
  2583 
  2584 definition uniformly_continuous_on :: "'a set \<Rightarrow> ('a::uniform_space \<Rightarrow> 'b::uniform_space) \<Rightarrow> bool" where
  2585   uniformly_continuous_on_uniformity: "uniformly_continuous_on s f \<longleftrightarrow>
  2586     (LIM (x, y) (uniformity_on s). (f x, f y) :> uniformity)"
  2587 
  2588 lemma uniformly_continuous_onD:
  2589   "uniformly_continuous_on s f \<Longrightarrow> eventually E uniformity
  2590     \<Longrightarrow> eventually (\<lambda>(x, y). x \<in> s \<longrightarrow> y \<in> s \<longrightarrow> E (f x, f y)) uniformity"
  2591   by (simp add: uniformly_continuous_on_uniformity filterlim_iff eventually_inf_principal split_beta' mem_Times_iff imp_conjL)
  2592 
  2593 lemma uniformly_continuous_on_const[continuous_intros]: "uniformly_continuous_on s (\<lambda>x. c)"
  2594   by (auto simp: uniformly_continuous_on_uniformity filterlim_iff uniformity_refl)
  2595 
  2596 lemma uniformly_continuous_on_id[continuous_intros]: "uniformly_continuous_on s (\<lambda>x. x)"
  2597   by (auto simp: uniformly_continuous_on_uniformity filterlim_def)
  2598 
  2599 lemma uniformly_continuous_on_compose[continuous_intros]:
  2600   "uniformly_continuous_on s g \<Longrightarrow> uniformly_continuous_on (g`s) f \<Longrightarrow> uniformly_continuous_on s (\<lambda>x. f (g x))"
  2601   using filterlim_compose[of "\<lambda>(x, y). (f x, f y)" uniformity "uniformity_on (g`s)"  "\<lambda>(x, y). (g x, g y)" "uniformity_on s"]
  2602   by (simp add: split_beta' uniformly_continuous_on_uniformity filterlim_inf filterlim_principal eventually_inf_principal mem_Times_iff)
  2603 
  2604 lemma uniformly_continuous_imp_continuous: assumes f: "uniformly_continuous_on s f" shows "continuous_on s f"
  2605   by (auto simp: filterlim_iff eventually_at_filter eventually_nhds_uniformity continuous_on_def
  2606            elim: eventually_mono dest!: uniformly_continuous_onD[OF f])
  2607 
  2608 end