src/HOL/Topological_Spaces.thy
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 66827 c94531b5007d
child 67149 e61557884799
permissions -rw-r--r--
more robust sorted_entries;
     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
    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 closed :: "'a set \<Rightarrow> bool"
    26   where "closed S \<longleftrightarrow> open (- S)"
    27 
    28 lemma open_empty [continuous_intros, intro, simp]: "open {}"
    29   using open_Union [of "{}"] by simp
    30 
    31 lemma open_Un [continuous_intros, intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<union> T)"
    32   using open_Union [of "{S, T}"] by simp
    33 
    34 lemma open_UN [continuous_intros, intro]: "\<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Union>x\<in>A. B x)"
    35   using open_Union [of "B ` A"] by simp
    36 
    37 lemma open_Inter [continuous_intros, intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. open T \<Longrightarrow> open (\<Inter>S)"
    38   by (induct set: finite) auto
    39 
    40 lemma open_INT [continuous_intros, intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Inter>x\<in>A. B x)"
    41   using open_Inter [of "B ` A"] by simp
    42 
    43 lemma openI:
    44   assumes "\<And>x. x \<in> S \<Longrightarrow> \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S"
    45   shows "open S"
    46 proof -
    47   have "open (\<Union>{T. open T \<and> T \<subseteq> S})" by auto
    48   moreover have "\<Union>{T. open T \<and> T \<subseteq> S} = S" by (auto dest!: assms)
    49   ultimately show "open S" by simp
    50 qed
    51 
    52 lemma closed_empty [continuous_intros, intro, simp]: "closed {}"
    53   unfolding closed_def by simp
    54 
    55 lemma closed_Un [continuous_intros, intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<union> T)"
    56   unfolding closed_def by auto
    57 
    58 lemma closed_UNIV [continuous_intros, intro, simp]: "closed UNIV"
    59   unfolding closed_def by simp
    60 
    61 lemma closed_Int [continuous_intros, intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<inter> T)"
    62   unfolding closed_def by auto
    63 
    64 lemma closed_INT [continuous_intros, intro]: "\<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Inter>x\<in>A. B x)"
    65   unfolding closed_def by auto
    66 
    67 lemma closed_Inter [continuous_intros, intro]: "\<forall>S\<in>K. closed S \<Longrightarrow> closed (\<Inter>K)"
    68   unfolding closed_def uminus_Inf by auto
    69 
    70 lemma closed_Union [continuous_intros, intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. closed T \<Longrightarrow> closed (\<Union>S)"
    71   by (induct set: finite) auto
    72 
    73 lemma closed_UN [continuous_intros, intro]:
    74   "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   by (simp add: closed_def)
    79 
    80 lemma closed_open: "closed S \<longleftrightarrow> open (- S)"
    81   by (rule closed_def)
    82 
    83 lemma open_Diff [continuous_intros, intro]: "open S \<Longrightarrow> closed T \<Longrightarrow> open (S - T)"
    84   by (simp add: closed_open Diff_eq open_Int)
    85 
    86 lemma closed_Diff [continuous_intros, intro]: "closed S \<Longrightarrow> open T \<Longrightarrow> closed (S - T)"
    87   by (simp add: open_closed Diff_eq closed_Int)
    88 
    89 lemma open_Compl [continuous_intros, intro]: "closed S \<Longrightarrow> open (- S)"
    90   by (simp add: closed_open)
    91 
    92 lemma closed_Compl [continuous_intros, intro]: "open S \<Longrightarrow> closed (- S)"
    93   by (simp add: 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:
    99   assumes "open {x. P x}" "open {x. Q x}"
   100   shows "open {x. P x \<and> Q x}"
   101   using open_Int[OF assms] by (simp add: Int_def)
   102 
   103 lemma open_Collect_disj:
   104   assumes "open {x. P x}" "open {x. Q x}"
   105   shows "open {x. P x \<or> Q x}"
   106   using open_Un[OF assms] by (simp add: Un_def)
   107 
   108 lemma open_Collect_ex: "(\<And>i. open {x. P i x}) \<Longrightarrow> open {x. \<exists>i. P i x}"
   109   using open_UN[of UNIV "\<lambda>i. {x. P i x}"] unfolding Collect_ex_eq by simp
   110 
   111 lemma open_Collect_imp: "closed {x. P x} \<Longrightarrow> open {x. Q x} \<Longrightarrow> open {x. P x \<longrightarrow> Q x}"
   112   unfolding imp_conv_disj by (intro open_Collect_disj open_Collect_neg)
   113 
   114 lemma open_Collect_const: "open {x. P}"
   115   by (cases P) auto
   116 
   117 lemma closed_Collect_neg: "open {x. P x} \<Longrightarrow> closed {x. \<not> P x}"
   118   unfolding Collect_neg_eq by (rule closed_Compl)
   119 
   120 lemma closed_Collect_conj:
   121   assumes "closed {x. P x}" "closed {x. Q x}"
   122   shows "closed {x. P x \<and> Q x}"
   123   using closed_Int[OF assms] by (simp add: Int_def)
   124 
   125 lemma closed_Collect_disj:
   126   assumes "closed {x. P x}" "closed {x. Q x}"
   127   shows "closed {x. P x \<or> Q x}"
   128   using closed_Un[OF assms] by (simp add: Un_def)
   129 
   130 lemma closed_Collect_all: "(\<And>i. closed {x. P i x}) \<Longrightarrow> closed {x. \<forall>i. P i x}"
   131   using closed_INT[of UNIV "\<lambda>i. {x. P i x}"] by (simp add: Collect_all_eq)
   132 
   133 lemma closed_Collect_imp: "open {x. P x} \<Longrightarrow> closed {x. Q x} \<Longrightarrow> closed {x. P x \<longrightarrow> Q x}"
   134   unfolding imp_conv_disj by (intro closed_Collect_disj closed_Collect_neg)
   135 
   136 lemma closed_Collect_const: "closed {x. P}"
   137   by (cases P) auto
   138 
   139 end
   140 
   141 
   142 subsection \<open>Hausdorff and other separation properties\<close>
   143 
   144 class t0_space = topological_space +
   145   assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
   146 
   147 class t1_space = topological_space +
   148   assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
   149 
   150 instance t1_space \<subseteq> t0_space
   151   by standard (fast dest: t1_space)
   152 
   153 context t1_space begin
   154 
   155 lemma separation_t1: "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U)"
   156   using t1_space[of x y] by blast
   157 
   158 lemma closed_singleton [iff]: "closed {a}"
   159 proof -
   160   let ?T = "\<Union>{S. open S \<and> a \<notin> S}"
   161   have "open ?T"
   162     by (simp add: open_Union)
   163   also have "?T = - {a}"
   164     by (auto simp add: set_eq_iff separation_t1)
   165   finally show "closed {a}"
   166     by (simp only: closed_def)
   167 qed
   168 
   169 lemma closed_insert [continuous_intros, simp]:
   170   assumes "closed S"
   171   shows "closed (insert a S)"
   172 proof -
   173   from closed_singleton assms have "closed ({a} \<union> S)"
   174     by (rule closed_Un)
   175   then show "closed (insert a S)"
   176     by simp
   177 qed
   178 
   179 lemma finite_imp_closed: "finite S \<Longrightarrow> closed S"
   180   by (induct pred: finite) simp_all
   181 
   182 end
   183 
   184 text \<open>T2 spaces are also known as Hausdorff spaces.\<close>
   185 
   186 class t2_space = topological_space +
   187   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 = {}"
   188 
   189 instance t2_space \<subseteq> t1_space
   190   by standard (fast dest: hausdorff)
   191 
   192 lemma (in t2_space) separation_t2: "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
   193   using hausdorff [of x y] by blast
   194 
   195 lemma (in t0_space) separation_t0: "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U))"
   196   using t0_space [of x y] by blast
   197 
   198 
   199 text \<open>A perfect space is a topological space with no isolated points.\<close>
   200 
   201 class perfect_space = topological_space +
   202   assumes not_open_singleton: "\<not> open {x}"
   203 
   204 lemma (in perfect_space) UNIV_not_singleton: "UNIV \<noteq> {x}"
   205   for x::'a
   206   by (metis (no_types) open_UNIV not_open_singleton)
   207 
   208 
   209 subsection \<open>Generators for toplogies\<close>
   210 
   211 inductive generate_topology :: "'a set set \<Rightarrow> 'a set \<Rightarrow> bool" for S :: "'a set set"
   212   where
   213     UNIV: "generate_topology S UNIV"
   214   | Int: "generate_topology S (a \<inter> b)" if "generate_topology S a" and "generate_topology S b"
   215   | UN: "generate_topology S (\<Union>K)" if "(\<And>k. k \<in> K \<Longrightarrow> generate_topology S k)"
   216   | Basis: "generate_topology S s" if "s \<in> S"
   217 
   218 hide_fact (open) UNIV Int UN Basis
   219 
   220 lemma generate_topology_Union:
   221   "(\<And>k. k \<in> I \<Longrightarrow> generate_topology S (K k)) \<Longrightarrow> generate_topology S (\<Union>k\<in>I. K k)"
   222   using generate_topology.UN [of "K ` I"] by auto
   223 
   224 lemma topological_space_generate_topology: "class.topological_space (generate_topology S)"
   225   by standard (auto intro: generate_topology.intros)
   226 
   227 
   228 subsection \<open>Order topologies\<close>
   229 
   230 class order_topology = order + "open" +
   231   assumes open_generated_order: "open = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
   232 begin
   233 
   234 subclass topological_space
   235   unfolding open_generated_order
   236   by (rule topological_space_generate_topology)
   237 
   238 lemma open_greaterThan [continuous_intros, simp]: "open {a <..}"
   239   unfolding open_generated_order by (auto intro: generate_topology.Basis)
   240 
   241 lemma open_lessThan [continuous_intros, simp]: "open {..< a}"
   242   unfolding open_generated_order by (auto intro: generate_topology.Basis)
   243 
   244 lemma open_greaterThanLessThan [continuous_intros, simp]: "open {a <..< b}"
   245    unfolding greaterThanLessThan_eq by (simp add: open_Int)
   246 
   247 end
   248 
   249 class linorder_topology = linorder + order_topology
   250 
   251 lemma closed_atMost [continuous_intros, simp]: "closed {..a}"
   252   for a :: "'a::linorder_topology"
   253   by (simp add: closed_open)
   254 
   255 lemma closed_atLeast [continuous_intros, simp]: "closed {a..}"
   256   for a :: "'a::linorder_topology"
   257   by (simp add: closed_open)
   258 
   259 lemma closed_atLeastAtMost [continuous_intros, simp]: "closed {a..b}"
   260   for a b :: "'a::linorder_topology"
   261 proof -
   262   have "{a .. b} = {a ..} \<inter> {.. b}"
   263     by auto
   264   then show ?thesis
   265     by (simp add: closed_Int)
   266 qed
   267 
   268 lemma (in linorder) less_separate:
   269   assumes "x < y"
   270   shows "\<exists>a b. x \<in> {..< a} \<and> y \<in> {b <..} \<and> {..< a} \<inter> {b <..} = {}"
   271 proof (cases "\<exists>z. x < z \<and> z < y")
   272   case True
   273   then obtain z where "x < z \<and> z < y" ..
   274   then have "x \<in> {..< z} \<and> y \<in> {z <..} \<and> {z <..} \<inter> {..< z} = {}"
   275     by auto
   276   then show ?thesis by blast
   277 next
   278   case False
   279   with \<open>x < y\<close> have "x \<in> {..< y}" "y \<in> {x <..}" "{x <..} \<inter> {..< y} = {}"
   280     by auto
   281   then show ?thesis by blast
   282 qed
   283 
   284 instance linorder_topology \<subseteq> t2_space
   285 proof
   286   fix x y :: 'a
   287   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 = {}"
   288     using less_separate [of x y] less_separate [of y x]
   289     by (elim neqE; metis open_lessThan open_greaterThan Int_commute)
   290 qed
   291 
   292 lemma (in linorder_topology) open_right:
   293   assumes "open S" "x \<in> S"
   294     and gt_ex: "x < y"
   295   shows "\<exists>b>x. {x ..< b} \<subseteq> S"
   296   using assms unfolding open_generated_order
   297 proof induct
   298   case UNIV
   299   then show ?case by blast
   300 next
   301   case (Int A B)
   302   then obtain a b where "a > x" "{x ..< a} \<subseteq> A"  "b > x" "{x ..< b} \<subseteq> B"
   303     by auto
   304   then show ?case
   305     by (auto intro!: exI[of _ "min a b"])
   306 next
   307   case UN
   308   then show ?case by blast
   309 next
   310   case Basis
   311   then show ?case
   312     by (fastforce intro: exI[of _ y] gt_ex)
   313 qed
   314 
   315 lemma (in linorder_topology) open_left:
   316   assumes "open S" "x \<in> S"
   317     and lt_ex: "y < x"
   318   shows "\<exists>b<x. {b <.. x} \<subseteq> S"
   319   using assms unfolding open_generated_order
   320 proof induction
   321   case UNIV
   322   then show ?case by blast
   323 next
   324   case (Int A B)
   325   then obtain a b where "a < x" "{a <.. x} \<subseteq> A"  "b < x" "{b <.. x} \<subseteq> B"
   326     by auto
   327   then show ?case
   328     by (auto intro!: exI[of _ "max a b"])
   329 next
   330   case UN
   331   then show ?case by blast
   332 next
   333   case Basis
   334   then show ?case
   335     by (fastforce intro: exI[of _ y] lt_ex)
   336 qed
   337 
   338 
   339 subsection \<open>Setup some topologies\<close>
   340 
   341 subsubsection \<open>Boolean is an order topology\<close>
   342 
   343 class discrete_topology = topological_space +
   344   assumes open_discrete: "\<And>A. open A"
   345 
   346 instance discrete_topology < t2_space
   347 proof
   348   fix x y :: 'a
   349   assume "x \<noteq> y"
   350   then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
   351     by (intro exI[of _ "{_}"]) (auto intro!: open_discrete)
   352 qed
   353 
   354 instantiation bool :: linorder_topology
   355 begin
   356 
   357 definition open_bool :: "bool set \<Rightarrow> bool"
   358   where "open_bool = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
   359 
   360 instance
   361   by standard (rule open_bool_def)
   362 
   363 end
   364 
   365 instance bool :: discrete_topology
   366 proof
   367   fix A :: "bool set"
   368   have *: "{False <..} = {True}" "{..< True} = {False}"
   369     by auto
   370   have "A = UNIV \<or> A = {} \<or> A = {False <..} \<or> A = {..< True}"
   371     using subset_UNIV[of A] unfolding UNIV_bool * by blast
   372   then show "open A"
   373     by auto
   374 qed
   375 
   376 instantiation nat :: linorder_topology
   377 begin
   378 
   379 definition open_nat :: "nat set \<Rightarrow> bool"
   380   where "open_nat = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
   381 
   382 instance
   383   by standard (rule open_nat_def)
   384 
   385 end
   386 
   387 instance nat :: discrete_topology
   388 proof
   389   fix A :: "nat set"
   390   have "open {n}" for n :: nat
   391   proof (cases n)
   392     case 0
   393     moreover have "{0} = {..<1::nat}"
   394       by auto
   395     ultimately show ?thesis
   396        by auto
   397   next
   398     case (Suc n')
   399     then have "{n} = {..<Suc n} \<inter> {n' <..}"
   400       by auto
   401     with Suc show ?thesis
   402       by (auto intro: open_lessThan open_greaterThan)
   403   qed
   404   then have "open (\<Union>a\<in>A. {a})"
   405     by (intro open_UN) auto
   406   then show "open A"
   407     by simp
   408 qed
   409 
   410 instantiation int :: linorder_topology
   411 begin
   412 
   413 definition open_int :: "int set \<Rightarrow> bool"
   414   where "open_int = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
   415 
   416 instance
   417   by standard (rule open_int_def)
   418 
   419 end
   420 
   421 instance int :: discrete_topology
   422 proof
   423   fix A :: "int set"
   424   have "{..<i + 1} \<inter> {i-1 <..} = {i}" for i :: int
   425     by auto
   426   then have "open {i}" for i :: int
   427     using open_Int[OF open_lessThan[of "i + 1"] open_greaterThan[of "i - 1"]] by auto
   428   then have "open (\<Union>a\<in>A. {a})"
   429     by (intro open_UN) auto
   430   then show "open A"
   431     by simp
   432 qed
   433 
   434 
   435 subsubsection \<open>Topological filters\<close>
   436 
   437 definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
   438   where "nhds a = (INF S:{S. open S \<and> a \<in> S}. principal S)"
   439 
   440 definition (in topological_space) at_within :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a filter"
   441     ("at (_)/ within (_)" [1000, 60] 60)
   442   where "at a within s = inf (nhds a) (principal (s - {a}))"
   443 
   444 abbreviation (in topological_space) at :: "'a \<Rightarrow> 'a filter"  ("at")
   445   where "at x \<equiv> at x within (CONST UNIV)"
   446 
   447 abbreviation (in order_topology) at_right :: "'a \<Rightarrow> 'a filter"
   448   where "at_right x \<equiv> at x within {x <..}"
   449 
   450 abbreviation (in order_topology) at_left :: "'a \<Rightarrow> 'a filter"
   451   where "at_left x \<equiv> at x within {..< x}"
   452 
   453 lemma (in topological_space) nhds_generated_topology:
   454   "open = generate_topology T \<Longrightarrow> nhds x = (INF S:{S\<in>T. x \<in> S}. principal S)"
   455   unfolding nhds_def
   456 proof (safe intro!: antisym INF_greatest)
   457   fix S
   458   assume "generate_topology T S" "x \<in> S"
   459   then show "(INF S:{S \<in> T. x \<in> S}. principal S) \<le> principal S"
   460     by induct
   461       (auto intro: INF_lower order_trans simp: inf_principal[symmetric] simp del: inf_principal)
   462 qed (auto intro!: INF_lower intro: generate_topology.intros)
   463 
   464 lemma (in topological_space) eventually_nhds:
   465   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   466   unfolding nhds_def by (subst eventually_INF_base) (auto simp: eventually_principal)
   467 
   468 lemma eventually_eventually:
   469   "eventually (\<lambda>y. eventually P (nhds y)) (nhds x) = eventually P (nhds x)"
   470   by (auto simp: eventually_nhds)
   471 
   472 lemma (in topological_space) eventually_nhds_in_open:
   473   "open s \<Longrightarrow> x \<in> s \<Longrightarrow> eventually (\<lambda>y. y \<in> s) (nhds x)"
   474   by (subst eventually_nhds) blast
   475 
   476 lemma (in topological_space) eventually_nhds_x_imp_x: "eventually P (nhds x) \<Longrightarrow> P x"
   477   by (subst (asm) eventually_nhds) blast
   478 
   479 lemma (in topological_space) nhds_neq_bot [simp]: "nhds a \<noteq> bot"
   480   by (simp add: trivial_limit_def eventually_nhds)
   481 
   482 lemma (in t1_space) t1_space_nhds: "x \<noteq> y \<Longrightarrow> (\<forall>\<^sub>F x in nhds x. x \<noteq> y)"
   483   by (drule t1_space) (auto simp: eventually_nhds)
   484 
   485 lemma (in topological_space) nhds_discrete_open: "open {x} \<Longrightarrow> nhds x = principal {x}"
   486   by (auto simp: nhds_def intro!: antisym INF_greatest INF_lower2[of "{x}"])
   487 
   488 lemma (in discrete_topology) nhds_discrete: "nhds x = principal {x}"
   489   by (simp add: nhds_discrete_open open_discrete)
   490 
   491 lemma (in discrete_topology) at_discrete: "at x within S = bot"
   492   unfolding at_within_def nhds_discrete by simp
   493 
   494 lemma (in topological_space) at_within_eq:
   495   "at x within s = (INF S:{S. open S \<and> x \<in> S}. principal (S \<inter> s - {x}))"
   496   unfolding nhds_def at_within_def
   497   by (subst INF_inf_const2[symmetric]) (auto simp: Diff_Int_distrib)
   498 
   499 lemma (in topological_space) eventually_at_filter:
   500   "eventually P (at a within s) \<longleftrightarrow> eventually (\<lambda>x. x \<noteq> a \<longrightarrow> x \<in> s \<longrightarrow> P x) (nhds a)"
   501   by (simp add: at_within_def eventually_inf_principal imp_conjL[symmetric] conj_commute)
   502 
   503 lemma (in topological_space) at_le: "s \<subseteq> t \<Longrightarrow> at x within s \<le> at x within t"
   504   unfolding at_within_def by (intro inf_mono) auto
   505 
   506 lemma (in topological_space) eventually_at_topological:
   507   "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))"
   508   by (simp add: eventually_nhds eventually_at_filter)
   509 
   510 lemma (in topological_space) at_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> at a within S = at a"
   511   unfolding filter_eq_iff eventually_at_topological by (metis open_Int Int_iff UNIV_I)
   512 
   513 lemma (in topological_space) at_within_open_NO_MATCH:
   514   "a \<in> s \<Longrightarrow> open s \<Longrightarrow> NO_MATCH UNIV s \<Longrightarrow> at a within s = at a"
   515   by (simp only: at_within_open)
   516 
   517 lemma (in topological_space) at_within_open_subset:
   518   "a \<in> S \<Longrightarrow> open S \<Longrightarrow> S \<subseteq> T \<Longrightarrow> at a within T = at a"
   519   by (metis at_le at_within_open dual_order.antisym subset_UNIV)
   520 
   521 lemma (in topological_space) at_within_nhd:
   522   assumes "x \<in> S" "open S" "T \<inter> S - {x} = U \<inter> S - {x}"
   523   shows "at x within T = at x within U"
   524   unfolding filter_eq_iff eventually_at_filter
   525 proof (intro allI eventually_subst)
   526   have "eventually (\<lambda>x. x \<in> S) (nhds x)"
   527     using \<open>x \<in> S\<close> \<open>open S\<close> by (auto simp: eventually_nhds)
   528   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
   529     by eventually_elim (insert \<open>T \<inter> S - {x} = U \<inter> S - {x}\<close>, blast)
   530 qed
   531 
   532 lemma (in topological_space) at_within_empty [simp]: "at a within {} = bot"
   533   unfolding at_within_def by simp
   534 
   535 lemma (in topological_space) at_within_union:
   536   "at x within (S \<union> T) = sup (at x within S) (at x within T)"
   537   unfolding filter_eq_iff eventually_sup eventually_at_filter
   538   by (auto elim!: eventually_rev_mp)
   539 
   540 lemma (in topological_space) at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
   541   unfolding trivial_limit_def eventually_at_topological
   542   apply safe
   543    apply (case_tac "S = {a}")
   544     apply simp
   545    apply fast
   546   apply fast
   547   done
   548 
   549 lemma (in perfect_space) at_neq_bot [simp]: "at a \<noteq> bot"
   550   by (simp add: at_eq_bot_iff not_open_singleton)
   551 
   552 lemma (in order_topology) nhds_order:
   553   "nhds x = inf (INF a:{x <..}. principal {..< a}) (INF a:{..< x}. principal {a <..})"
   554 proof -
   555   have 1: "{S \<in> range lessThan \<union> range greaterThan. x \<in> S} =
   556       (\<lambda>a. {..< a}) ` {x <..} \<union> (\<lambda>a. {a <..}) ` {..< x}"
   557     by auto
   558   show ?thesis
   559     by (simp only: nhds_generated_topology[OF open_generated_order] INF_union 1 INF_image comp_def)
   560 qed
   561 
   562 lemma (in topological_space) filterlim_at_within_If:
   563   assumes "filterlim f G (at x within (A \<inter> {x. P x}))"
   564     and "filterlim g G (at x within (A \<inter> {x. \<not>P x}))"
   565   shows "filterlim (\<lambda>x. if P x then f x else g x) G (at x within A)"
   566 proof (rule filterlim_If)
   567   note assms(1)
   568   also have "at x within (A \<inter> {x. P x}) = inf (nhds x) (principal (A \<inter> Collect P - {x}))"
   569     by (simp add: at_within_def)
   570   also have "A \<inter> Collect P - {x} = (A - {x}) \<inter> Collect P"
   571     by blast
   572   also have "inf (nhds x) (principal \<dots>) = inf (at x within A) (principal (Collect P))"
   573     by (simp add: at_within_def inf_assoc)
   574   finally show "filterlim f G (inf (at x within A) (principal (Collect P)))" .
   575 next
   576   note assms(2)
   577   also have "at x within (A \<inter> {x. \<not> P x}) = inf (nhds x) (principal (A \<inter> {x. \<not> P x} - {x}))"
   578     by (simp add: at_within_def)
   579   also have "A \<inter> {x. \<not> P x} - {x} = (A - {x}) \<inter> {x. \<not> P x}"
   580     by blast
   581   also have "inf (nhds x) (principal \<dots>) = inf (at x within A) (principal {x. \<not> P x})"
   582     by (simp add: at_within_def inf_assoc)
   583   finally show "filterlim g G (inf (at x within A) (principal {x. \<not> P x}))" .
   584 qed
   585 
   586 lemma (in topological_space) filterlim_at_If:
   587   assumes "filterlim f G (at x within {x. P x})"
   588     and "filterlim g G (at x within {x. \<not>P x})"
   589   shows "filterlim (\<lambda>x. if P x then f x else g x) G (at x)"
   590   using assms by (intro filterlim_at_within_If) simp_all
   591 
   592 lemma (in linorder_topology) at_within_order:
   593   assumes "UNIV \<noteq> {x}"
   594   shows "at x within s =
   595     inf (INF a:{x <..}. principal ({..< a} \<inter> s - {x}))
   596         (INF a:{..< x}. principal ({a <..} \<inter> s - {x}))"
   597 proof (cases "{x <..} = {}" "{..< x} = {}" rule: case_split [case_product case_split])
   598   case True_True
   599   have "UNIV = {..< x} \<union> {x} \<union> {x <..}"
   600     by auto
   601   with assms True_True show ?thesis
   602     by auto
   603 qed (auto simp del: inf_principal simp: at_within_def nhds_order Int_Diff
   604       inf_principal[symmetric] INF_inf_const2 inf_sup_aci[where 'a="'a filter"])
   605 
   606 lemma (in linorder_topology) at_left_eq:
   607   "y < x \<Longrightarrow> at_left x = (INF a:{..< x}. principal {a <..< x})"
   608   by (subst at_within_order)
   609      (auto simp: greaterThan_Int_greaterThan greaterThanLessThan_eq[symmetric] min.absorb2 INF_constant
   610            intro!: INF_lower2 inf_absorb2)
   611 
   612 lemma (in linorder_topology) eventually_at_left:
   613   "y < x \<Longrightarrow> eventually P (at_left x) \<longleftrightarrow> (\<exists>b<x. \<forall>y>b. y < x \<longrightarrow> P y)"
   614   unfolding at_left_eq
   615   by (subst eventually_INF_base) (auto simp: eventually_principal Ball_def)
   616 
   617 lemma (in linorder_topology) at_right_eq:
   618   "x < y \<Longrightarrow> at_right x = (INF a:{x <..}. principal {x <..< a})"
   619   by (subst at_within_order)
   620      (auto simp: lessThan_Int_lessThan greaterThanLessThan_eq[symmetric] max.absorb2 INF_constant Int_commute
   621            intro!: INF_lower2 inf_absorb1)
   622 
   623 lemma (in linorder_topology) eventually_at_right:
   624   "x < y \<Longrightarrow> eventually P (at_right x) \<longleftrightarrow> (\<exists>b>x. \<forall>y>x. y < b \<longrightarrow> P y)"
   625   unfolding at_right_eq
   626   by (subst eventually_INF_base) (auto simp: eventually_principal Ball_def)
   627 
   628 lemma eventually_at_right_less: "\<forall>\<^sub>F y in at_right (x::'a::{linorder_topology, no_top}). x < y"
   629   using gt_ex[of x] eventually_at_right[of x] by auto
   630 
   631 lemma trivial_limit_at_right_top: "at_right (top::_::{order_top,linorder_topology}) = bot"
   632   by (auto simp: filter_eq_iff eventually_at_topological)
   633 
   634 lemma trivial_limit_at_left_bot: "at_left (bot::_::{order_bot,linorder_topology}) = bot"
   635   by (auto simp: filter_eq_iff eventually_at_topological)
   636 
   637 lemma trivial_limit_at_left_real [simp]: "\<not> trivial_limit (at_left x)"
   638   for x :: "'a::{no_bot,dense_order,linorder_topology}"
   639   using lt_ex [of x]
   640   by safe (auto simp add: trivial_limit_def eventually_at_left dest: dense)
   641 
   642 lemma trivial_limit_at_right_real [simp]: "\<not> trivial_limit (at_right x)"
   643   for x :: "'a::{no_top,dense_order,linorder_topology}"
   644   using gt_ex[of x]
   645   by safe (auto simp add: trivial_limit_def eventually_at_right dest: dense)
   646 
   647 lemma (in linorder_topology) at_eq_sup_left_right: "at x = sup (at_left x) (at_right x)"
   648   by (auto simp: eventually_at_filter filter_eq_iff eventually_sup
   649       elim: eventually_elim2 eventually_mono)
   650 
   651 lemma (in linorder_topology) eventually_at_split:
   652   "eventually P (at x) \<longleftrightarrow> eventually P (at_left x) \<and> eventually P (at_right x)"
   653   by (subst at_eq_sup_left_right) (simp add: eventually_sup)
   654 
   655 lemma (in order_topology) eventually_at_leftI:
   656   assumes "\<And>x. x \<in> {a<..<b} \<Longrightarrow> P x" "a < b"
   657   shows   "eventually P (at_left b)"
   658   using assms unfolding eventually_at_topological by (intro exI[of _ "{a<..}"]) auto
   659 
   660 lemma (in order_topology) eventually_at_rightI:
   661   assumes "\<And>x. x \<in> {a<..<b} \<Longrightarrow> P x" "a < b"
   662   shows   "eventually P (at_right a)"
   663   using assms unfolding eventually_at_topological by (intro exI[of _ "{..<b}"]) auto
   664 
   665 lemma eventually_filtercomap_nhds:
   666   "eventually P (filtercomap f (nhds x)) \<longleftrightarrow> (\<exists>S. open S \<and> x \<in> S \<and> (\<forall>x. f x \<in> S \<longrightarrow> P x))"
   667   unfolding eventually_filtercomap eventually_nhds by auto
   668 
   669 lemma eventually_filtercomap_at_topological:
   670   "eventually P (filtercomap f (at A within B)) \<longleftrightarrow> 
   671      (\<exists>S. open S \<and> A \<in> S \<and> (\<forall>x. f x \<in> S \<inter> B - {A} \<longrightarrow> P x))" (is "?lhs = ?rhs")
   672   unfolding at_within_def filtercomap_inf eventually_inf_principal filtercomap_principal 
   673           eventually_filtercomap_nhds eventually_principal by blast
   674     
   675 
   676 
   677 subsubsection \<open>Tendsto\<close>
   678 
   679 abbreviation (in topological_space)
   680   tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool"  (infixr "\<longlongrightarrow>" 55)
   681   where "(f \<longlongrightarrow> l) F \<equiv> filterlim f (nhds l) F"
   682 
   683 definition (in t2_space) Lim :: "'f filter \<Rightarrow> ('f \<Rightarrow> 'a) \<Rightarrow> 'a"
   684   where "Lim A f = (THE l. (f \<longlongrightarrow> l) A)"
   685 
   686 lemma (in topological_space) tendsto_eq_rhs: "(f \<longlongrightarrow> x) F \<Longrightarrow> x = y \<Longrightarrow> (f \<longlongrightarrow> y) F"
   687   by simp
   688 
   689 named_theorems tendsto_intros "introduction rules for tendsto"
   690 setup \<open>
   691   Global_Theory.add_thms_dynamic (@{binding tendsto_eq_intros},
   692     fn context =>
   693       Named_Theorems.get (Context.proof_of context) @{named_theorems tendsto_intros}
   694       |> map_filter (try (fn thm => @{thm tendsto_eq_rhs} OF [thm])))
   695 \<close>
   696 
   697 context topological_space begin
   698 
   699 lemma tendsto_def:
   700    "(f \<longlongrightarrow> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
   701    unfolding nhds_def filterlim_INF filterlim_principal by auto
   702 
   703 lemma tendsto_cong: "(f \<longlongrightarrow> c) F \<longleftrightarrow> (g \<longlongrightarrow> c) F" if "eventually (\<lambda>x. f x = g x) F"
   704   by (rule filterlim_cong [OF refl refl that])
   705 
   706 lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f \<longlongrightarrow> l) F' \<Longrightarrow> (f \<longlongrightarrow> l) F"
   707   unfolding tendsto_def le_filter_def by fast
   708 
   709 lemma tendsto_ident_at [tendsto_intros, simp, intro]: "((\<lambda>x. x) \<longlongrightarrow> a) (at a within s)"
   710   by (auto simp: tendsto_def eventually_at_topological)
   711 
   712 lemma tendsto_const [tendsto_intros, simp, intro]: "((\<lambda>x. k) \<longlongrightarrow> k) F"
   713   by (simp add: tendsto_def)
   714 
   715 lemma  filterlim_at:
   716   "(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"
   717   by (simp add: at_within_def filterlim_inf filterlim_principal conj_commute)
   718 
   719 lemma  filterlim_at_withinI:
   720   assumes "filterlim f (nhds c) F"
   721   assumes "eventually (\<lambda>x. f x \<in> A - {c}) F"
   722   shows   "filterlim f (at c within A) F"
   723   using assms by (simp add: filterlim_at)
   724 
   725 lemma filterlim_atI:
   726   assumes "filterlim f (nhds c) F"
   727   assumes "eventually (\<lambda>x. f x \<noteq> c) F"
   728   shows   "filterlim f (at c) F"
   729   using assms by (intro filterlim_at_withinI) simp_all
   730 
   731 lemma topological_tendstoI:
   732   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F) \<Longrightarrow> (f \<longlongrightarrow> l) F"
   733   by (auto simp: tendsto_def)
   734 
   735 lemma topological_tendstoD:
   736   "(f \<longlongrightarrow> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
   737   by (auto simp: tendsto_def)
   738 
   739 lemma tendsto_bot [simp]: "(f \<longlongrightarrow> a) bot"
   740   by (simp add: tendsto_def)
   741 
   742 end
   743 
   744 lemma tendsto_within_subset:
   745   "(f \<longlongrightarrow> l) (at x within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f \<longlongrightarrow> l) (at x within T)"
   746   by (blast intro: tendsto_mono at_le)
   747 
   748 lemma (in order_topology) order_tendsto_iff:
   749   "(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)"
   750   by (auto simp: nhds_order filterlim_inf filterlim_INF filterlim_principal)
   751 
   752 lemma (in order_topology) order_tendstoI:
   753   "(\<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>
   754     (f \<longlongrightarrow> y) F"
   755   by (auto simp: order_tendsto_iff)
   756 
   757 lemma (in order_topology) order_tendstoD:
   758   assumes "(f \<longlongrightarrow> y) F"
   759   shows "a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F"
   760     and "y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F"
   761   using assms by (auto simp: order_tendsto_iff)
   762 
   763 lemma (in linorder_topology) tendsto_max:
   764   assumes X: "(X \<longlongrightarrow> x) net"
   765     and Y: "(Y \<longlongrightarrow> y) net"
   766   shows "((\<lambda>x. max (X x) (Y x)) \<longlongrightarrow> max x y) net"
   767 proof (rule order_tendstoI)
   768   fix a
   769   assume "a < max x y"
   770   then show "eventually (\<lambda>x. a < max (X x) (Y x)) net"
   771     using order_tendstoD(1)[OF X, of a] order_tendstoD(1)[OF Y, of a]
   772     by (auto simp: less_max_iff_disj elim: eventually_mono)
   773 next
   774   fix a
   775   assume "max x y < a"
   776   then show "eventually (\<lambda>x. max (X x) (Y x) < a) net"
   777     using order_tendstoD(2)[OF X, of a] order_tendstoD(2)[OF Y, of a]
   778     by (auto simp: eventually_conj_iff)
   779 qed
   780 
   781 lemma (in linorder_topology) tendsto_min:
   782   assumes X: "(X \<longlongrightarrow> x) net"
   783     and Y: "(Y \<longlongrightarrow> y) net"
   784   shows "((\<lambda>x. min (X x) (Y x)) \<longlongrightarrow> min x y) net"
   785 proof (rule order_tendstoI)
   786   fix a
   787   assume "a < min x y"
   788   then show "eventually (\<lambda>x. a < min (X x) (Y x)) net"
   789     using order_tendstoD(1)[OF X, of a] order_tendstoD(1)[OF Y, of a]
   790     by (auto simp: eventually_conj_iff)
   791 next
   792   fix a
   793   assume "min x y < a"
   794   then show "eventually (\<lambda>x. min (X x) (Y x) < a) net"
   795     using order_tendstoD(2)[OF X, of a] order_tendstoD(2)[OF Y, of a]
   796     by (auto simp: min_less_iff_disj elim: eventually_mono)
   797 qed
   798 
   799 lemma (in order_topology)
   800   assumes "a < b"
   801   shows at_within_Icc_at_right: "at a within {a..b} = at_right a"
   802     and at_within_Icc_at_left:  "at b within {a..b} = at_left b"
   803   using order_tendstoD(2)[OF tendsto_ident_at assms, of "{a<..}"]
   804   using order_tendstoD(1)[OF tendsto_ident_at assms, of "{..<b}"]
   805   by (auto intro!: order_class.antisym filter_leI
   806       simp: eventually_at_filter less_le
   807       elim: eventually_elim2)
   808 
   809 lemma (in order_topology) at_within_Icc_at: "a < x \<Longrightarrow> x < b \<Longrightarrow> at x within {a..b} = at x"
   810   by (rule at_within_open_subset[where S="{a<..<b}"]) auto
   811 
   812 lemma (in t2_space) tendsto_unique:
   813   assumes "F \<noteq> bot"
   814     and "(f \<longlongrightarrow> a) F"
   815     and "(f \<longlongrightarrow> b) F"
   816   shows "a = b"
   817 proof (rule ccontr)
   818   assume "a \<noteq> b"
   819   obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
   820     using hausdorff [OF \<open>a \<noteq> b\<close>] by fast
   821   have "eventually (\<lambda>x. f x \<in> U) F"
   822     using \<open>(f \<longlongrightarrow> a) F\<close> \<open>open U\<close> \<open>a \<in> U\<close> by (rule topological_tendstoD)
   823   moreover
   824   have "eventually (\<lambda>x. f x \<in> V) F"
   825     using \<open>(f \<longlongrightarrow> b) F\<close> \<open>open V\<close> \<open>b \<in> V\<close> by (rule topological_tendstoD)
   826   ultimately
   827   have "eventually (\<lambda>x. False) F"
   828   proof eventually_elim
   829     case (elim x)
   830     then have "f x \<in> U \<inter> V" by simp
   831     with \<open>U \<inter> V = {}\<close> show ?case by simp
   832   qed
   833   with \<open>\<not> trivial_limit F\<close> show "False"
   834     by (simp add: trivial_limit_def)
   835 qed
   836 
   837 lemma (in t2_space) tendsto_const_iff:
   838   fixes a b :: 'a
   839   assumes "\<not> trivial_limit F"
   840   shows "((\<lambda>x. a) \<longlongrightarrow> b) F \<longleftrightarrow> a = b"
   841   by (auto intro!: tendsto_unique [OF assms tendsto_const])
   842 
   843 lemma (in order_topology) increasing_tendsto:
   844   assumes bdd: "eventually (\<lambda>n. f n \<le> l) F"
   845     and en: "\<And>x. x < l \<Longrightarrow> eventually (\<lambda>n. x < f n) F"
   846   shows "(f \<longlongrightarrow> l) F"
   847   using assms by (intro order_tendstoI) (auto elim!: eventually_mono)
   848 
   849 lemma (in order_topology) decreasing_tendsto:
   850   assumes bdd: "eventually (\<lambda>n. l \<le> f n) F"
   851     and en: "\<And>x. l < x \<Longrightarrow> eventually (\<lambda>n. f n < x) F"
   852   shows "(f \<longlongrightarrow> l) F"
   853   using assms by (intro order_tendstoI) (auto elim!: eventually_mono)
   854 
   855 lemma (in order_topology) tendsto_sandwich:
   856   assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
   857   assumes lim: "(f \<longlongrightarrow> c) net" "(h \<longlongrightarrow> c) net"
   858   shows "(g \<longlongrightarrow> c) net"
   859 proof (rule order_tendstoI)
   860   fix a
   861   show "a < c \<Longrightarrow> eventually (\<lambda>x. a < g x) net"
   862     using order_tendstoD[OF lim(1), of a] ev by (auto elim: eventually_elim2)
   863 next
   864   fix a
   865   show "c < a \<Longrightarrow> eventually (\<lambda>x. g x < a) net"
   866     using order_tendstoD[OF lim(2), of a] ev by (auto elim: eventually_elim2)
   867 qed
   868 
   869 lemma (in t1_space) limit_frequently_eq:
   870   assumes "F \<noteq> bot"
   871     and "frequently (\<lambda>x. f x = c) F"
   872     and "(f \<longlongrightarrow> d) F"
   873   shows "d = c"
   874 proof (rule ccontr)
   875   assume "d \<noteq> c"
   876   from t1_space[OF this] obtain U where "open U" "d \<in> U" "c \<notin> U"
   877     by blast
   878   with assms have "eventually (\<lambda>x. f x \<in> U) F"
   879     unfolding tendsto_def by blast
   880   then have "eventually (\<lambda>x. f x \<noteq> c) F"
   881     by eventually_elim (insert \<open>c \<notin> U\<close>, blast)
   882   with assms(2) show False
   883     unfolding frequently_def by contradiction
   884 qed
   885 
   886 lemma (in t1_space) tendsto_imp_eventually_ne:
   887   assumes  "(f \<longlongrightarrow> c) F" "c \<noteq> c'"
   888   shows "eventually (\<lambda>z. f z \<noteq> c') F"
   889 proof (cases "F=bot")
   890   case True
   891   thus ?thesis by auto
   892 next
   893   case False
   894   show ?thesis
   895   proof (rule ccontr)
   896     assume "\<not> eventually (\<lambda>z. f z \<noteq> c') F"
   897     then have "frequently (\<lambda>z. f z = c') F"
   898       by (simp add: frequently_def)
   899     from limit_frequently_eq[OF False this \<open>(f \<longlongrightarrow> c) F\<close>] and \<open>c \<noteq> c'\<close> show False
   900       by contradiction
   901   qed
   902 qed
   903 
   904 lemma (in linorder_topology) tendsto_le:
   905   assumes F: "\<not> trivial_limit F"
   906     and x: "(f \<longlongrightarrow> x) F"
   907     and y: "(g \<longlongrightarrow> y) F"
   908     and ev: "eventually (\<lambda>x. g x \<le> f x) F"
   909   shows "y \<le> x"
   910 proof (rule ccontr)
   911   assume "\<not> y \<le> x"
   912   with less_separate[of x y] obtain a b where xy: "x < a" "b < y" "{..<a} \<inter> {b<..} = {}"
   913     by (auto simp: not_le)
   914   then have "eventually (\<lambda>x. f x < a) F" "eventually (\<lambda>x. b < g x) F"
   915     using x y by (auto intro: order_tendstoD)
   916   with ev have "eventually (\<lambda>x. False) F"
   917     by eventually_elim (insert xy, fastforce)
   918   with F show False
   919     by (simp add: eventually_False)
   920 qed
   921 
   922 lemma (in linorder_topology) tendsto_lowerbound:
   923   assumes x: "(f \<longlongrightarrow> x) F"
   924       and ev: "eventually (\<lambda>i. a \<le> f i) F"
   925       and F: "\<not> trivial_limit F"
   926   shows "a \<le> x"
   927   using F x tendsto_const ev by (rule tendsto_le)
   928 
   929 lemma (in linorder_topology) tendsto_upperbound:
   930   assumes x: "(f \<longlongrightarrow> x) F"
   931       and ev: "eventually (\<lambda>i. a \<ge> f i) F"
   932       and F: "\<not> trivial_limit F"
   933   shows "a \<ge> x"
   934   by (rule tendsto_le [OF F tendsto_const x ev])
   935 
   936 
   937 subsubsection \<open>Rules about @{const Lim}\<close>
   938 
   939 lemma tendsto_Lim: "\<not> trivial_limit net \<Longrightarrow> (f \<longlongrightarrow> l) net \<Longrightarrow> Lim net f = l"
   940   unfolding Lim_def using tendsto_unique [of net f] by auto
   941 
   942 lemma Lim_ident_at: "\<not> trivial_limit (at x within s) \<Longrightarrow> Lim (at x within s) (\<lambda>x. x) = x"
   943   by (rule tendsto_Lim[OF _ tendsto_ident_at]) auto
   944 
   945 lemma filterlim_at_bot_at_right:
   946   fixes f :: "'a::linorder_topology \<Rightarrow> 'b::linorder"
   947   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   948     and bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
   949     and Q: "eventually Q (at_right a)"
   950     and bound: "\<And>b. Q b \<Longrightarrow> a < b"
   951     and P: "eventually P at_bot"
   952   shows "filterlim f at_bot (at_right a)"
   953 proof -
   954   from P obtain x where x: "\<And>y. y \<le> x \<Longrightarrow> P y"
   955     unfolding eventually_at_bot_linorder by auto
   956   show ?thesis
   957   proof (intro filterlim_at_bot_le[THEN iffD2] allI impI)
   958     fix z
   959     assume "z \<le> x"
   960     with x have "P z" by auto
   961     have "eventually (\<lambda>x. x \<le> g z) (at_right a)"
   962       using bound[OF bij(2)[OF \<open>P z\<close>]]
   963       unfolding eventually_at_right[OF bound[OF bij(2)[OF \<open>P z\<close>]]]
   964       by (auto intro!: exI[of _ "g z"])
   965     with Q show "eventually (\<lambda>x. f x \<le> z) (at_right a)"
   966       by eventually_elim (metis bij \<open>P z\<close> mono)
   967   qed
   968 qed
   969 
   970 lemma filterlim_at_top_at_left:
   971   fixes f :: "'a::linorder_topology \<Rightarrow> 'b::linorder"
   972   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   973     and bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
   974     and Q: "eventually Q (at_left a)"
   975     and bound: "\<And>b. Q b \<Longrightarrow> b < a"
   976     and P: "eventually P at_top"
   977   shows "filterlim f at_top (at_left a)"
   978 proof -
   979   from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
   980     unfolding eventually_at_top_linorder by auto
   981   show ?thesis
   982   proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
   983     fix z
   984     assume "x \<le> z"
   985     with x have "P z" by auto
   986     have "eventually (\<lambda>x. g z \<le> x) (at_left a)"
   987       using bound[OF bij(2)[OF \<open>P z\<close>]]
   988       unfolding eventually_at_left[OF bound[OF bij(2)[OF \<open>P z\<close>]]]
   989       by (auto intro!: exI[of _ "g z"])
   990     with Q show "eventually (\<lambda>x. z \<le> f x) (at_left a)"
   991       by eventually_elim (metis bij \<open>P z\<close> mono)
   992   qed
   993 qed
   994 
   995 lemma filterlim_split_at:
   996   "filterlim f F (at_left x) \<Longrightarrow> filterlim f F (at_right x) \<Longrightarrow>
   997     filterlim f F (at x)"
   998   for x :: "'a::linorder_topology"
   999   by (subst at_eq_sup_left_right) (rule filterlim_sup)
  1000 
  1001 lemma filterlim_at_split:
  1002   "filterlim f F (at x) \<longleftrightarrow> filterlim f F (at_left x) \<and> filterlim f F (at_right x)"
  1003   for x :: "'a::linorder_topology"
  1004   by (subst at_eq_sup_left_right) (simp add: filterlim_def filtermap_sup)
  1005 
  1006 lemma eventually_nhds_top:
  1007   fixes P :: "'a :: {order_top,linorder_topology} \<Rightarrow> bool"
  1008     and b :: 'a
  1009   assumes "b < top"
  1010   shows "eventually P (nhds top) \<longleftrightarrow> (\<exists>b<top. (\<forall>z. b < z \<longrightarrow> P z))"
  1011   unfolding eventually_nhds
  1012 proof safe
  1013   fix S :: "'a set"
  1014   assume "open S" "top \<in> S"
  1015   note open_left[OF this \<open>b < top\<close>]
  1016   moreover assume "\<forall>s\<in>S. P s"
  1017   ultimately show "\<exists>b<top. \<forall>z>b. P z"
  1018     by (auto simp: subset_eq Ball_def)
  1019 next
  1020   fix b
  1021   assume "b < top" "\<forall>z>b. P z"
  1022   then show "\<exists>S. open S \<and> top \<in> S \<and> (\<forall>xa\<in>S. P xa)"
  1023     by (intro exI[of _ "{b <..}"]) auto
  1024 qed
  1025 
  1026 lemma tendsto_at_within_iff_tendsto_nhds:
  1027   "(g \<longlongrightarrow> g l) (at l within S) \<longleftrightarrow> (g \<longlongrightarrow> g l) (inf (nhds l) (principal S))"
  1028   unfolding tendsto_def eventually_at_filter eventually_inf_principal
  1029   by (intro ext all_cong imp_cong) (auto elim!: eventually_mono)
  1030 
  1031 
  1032 subsection \<open>Limits on sequences\<close>
  1033 
  1034 abbreviation (in topological_space)
  1035   LIMSEQ :: "[nat \<Rightarrow> 'a, 'a] \<Rightarrow> bool"  ("((_)/ \<longlonglongrightarrow> (_))" [60, 60] 60)
  1036   where "X \<longlonglongrightarrow> L \<equiv> (X \<longlongrightarrow> L) sequentially"
  1037 
  1038 abbreviation (in t2_space) lim :: "(nat \<Rightarrow> 'a) \<Rightarrow> 'a"
  1039   where "lim X \<equiv> Lim sequentially X"
  1040 
  1041 definition (in topological_space) convergent :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool"
  1042   where "convergent X = (\<exists>L. X \<longlonglongrightarrow> L)"
  1043 
  1044 lemma lim_def: "lim X = (THE L. X \<longlonglongrightarrow> L)"
  1045   unfolding Lim_def ..
  1046 
  1047 
  1048 subsubsection \<open>Monotone sequences and subsequences\<close>
  1049 
  1050 text \<open>
  1051   Definition of monotonicity.
  1052   The use of disjunction here complicates proofs considerably.
  1053   One alternative is to add a Boolean argument to indicate the direction.
  1054   Another is to develop the notions of increasing and decreasing first.
  1055 \<close>
  1056 definition monoseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool"
  1057   where "monoseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X m \<le> X n) \<or> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m)"
  1058 
  1059 abbreviation incseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool"
  1060   where "incseq X \<equiv> mono X"
  1061 
  1062 lemma incseq_def: "incseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<ge> X m)"
  1063   unfolding mono_def ..
  1064 
  1065 abbreviation decseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool"
  1066   where "decseq X \<equiv> antimono X"
  1067 
  1068 lemma decseq_def: "decseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m)"
  1069   unfolding antimono_def ..
  1070 
  1071 text \<open>Definition of subsequence.\<close>
  1072 
  1073 (* For compatibility with the old "subseq" *)
  1074 lemma strict_mono_leD: "strict_mono r \<Longrightarrow> m \<le> n \<Longrightarrow> r m \<le> r n"
  1075   by (erule (1) monoD [OF strict_mono_mono])
  1076 
  1077 lemma strict_mono_id: "strict_mono id"
  1078   by (simp add: strict_mono_def)
  1079 
  1080 lemma incseq_SucI: "(\<And>n. X n \<le> X (Suc n)) \<Longrightarrow> incseq X"
  1081   using lift_Suc_mono_le[of X] by (auto simp: incseq_def)
  1082 
  1083 lemma incseqD: "incseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f i \<le> f j"
  1084   by (auto simp: incseq_def)
  1085 
  1086 lemma incseq_SucD: "incseq A \<Longrightarrow> A i \<le> A (Suc i)"
  1087   using incseqD[of A i "Suc i"] by auto
  1088 
  1089 lemma incseq_Suc_iff: "incseq f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
  1090   by (auto intro: incseq_SucI dest: incseq_SucD)
  1091 
  1092 lemma incseq_const[simp, intro]: "incseq (\<lambda>x. k)"
  1093   unfolding incseq_def by auto
  1094 
  1095 lemma decseq_SucI: "(\<And>n. X (Suc n) \<le> X n) \<Longrightarrow> decseq X"
  1096   using order.lift_Suc_mono_le[OF dual_order, of X] by (auto simp: decseq_def)
  1097 
  1098 lemma decseqD: "decseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f j \<le> f i"
  1099   by (auto simp: decseq_def)
  1100 
  1101 lemma decseq_SucD: "decseq A \<Longrightarrow> A (Suc i) \<le> A i"
  1102   using decseqD[of A i "Suc i"] by auto
  1103 
  1104 lemma decseq_Suc_iff: "decseq f \<longleftrightarrow> (\<forall>n. f (Suc n) \<le> f n)"
  1105   by (auto intro: decseq_SucI dest: decseq_SucD)
  1106 
  1107 lemma decseq_const[simp, intro]: "decseq (\<lambda>x. k)"
  1108   unfolding decseq_def by auto
  1109 
  1110 lemma monoseq_iff: "monoseq X \<longleftrightarrow> incseq X \<or> decseq X"
  1111   unfolding monoseq_def incseq_def decseq_def ..
  1112 
  1113 lemma monoseq_Suc: "monoseq X \<longleftrightarrow> (\<forall>n. X n \<le> X (Suc n)) \<or> (\<forall>n. X (Suc n) \<le> X n)"
  1114   unfolding monoseq_iff incseq_Suc_iff decseq_Suc_iff ..
  1115 
  1116 lemma monoI1: "\<forall>m. \<forall>n \<ge> m. X m \<le> X n \<Longrightarrow> monoseq X"
  1117   by (simp add: monoseq_def)
  1118 
  1119 lemma monoI2: "\<forall>m. \<forall>n \<ge> m. X n \<le> X m \<Longrightarrow> monoseq X"
  1120   by (simp add: monoseq_def)
  1121 
  1122 lemma mono_SucI1: "\<forall>n. X n \<le> X (Suc n) \<Longrightarrow> monoseq X"
  1123   by (simp add: monoseq_Suc)
  1124 
  1125 lemma mono_SucI2: "\<forall>n. X (Suc n) \<le> X n \<Longrightarrow> monoseq X"
  1126   by (simp add: monoseq_Suc)
  1127 
  1128 lemma monoseq_minus:
  1129   fixes a :: "nat \<Rightarrow> 'a::ordered_ab_group_add"
  1130   assumes "monoseq a"
  1131   shows "monoseq (\<lambda> n. - a n)"
  1132 proof (cases "\<forall>m. \<forall>n \<ge> m. a m \<le> a n")
  1133   case True
  1134   then have "\<forall>m. \<forall>n \<ge> m. - a n \<le> - a m" by auto
  1135   then show ?thesis by (rule monoI2)
  1136 next
  1137   case False
  1138   then have "\<forall>m. \<forall>n \<ge> m. - a m \<le> - a n"
  1139     using \<open>monoseq a\<close>[unfolded monoseq_def] by auto
  1140   then show ?thesis by (rule monoI1)
  1141 qed
  1142 
  1143 
  1144 text \<open>Subsequence (alternative definition, (e.g. Hoskins)\<close>
  1145 
  1146 lemma strict_mono_Suc_iff: "strict_mono f \<longleftrightarrow> (\<forall>n. f n < f (Suc n))"
  1147 proof (intro iffI strict_monoI)
  1148   assume *: "\<forall>n. f n < f (Suc n)"
  1149   fix m n :: nat assume "m < n"
  1150   thus "f m < f n"
  1151     by (induction rule: less_Suc_induct) (use * in auto)
  1152 qed (auto simp: strict_mono_def)
  1153 
  1154 lemma strict_mono_add: "strict_mono (\<lambda>n::'a::linordered_semidom. n + k)"
  1155   by (auto simp: strict_mono_def)
  1156 
  1157 text \<open>For any sequence, there is a monotonic subsequence.\<close>
  1158 lemma seq_monosub:
  1159   fixes s :: "nat \<Rightarrow> 'a::linorder"
  1160   shows "\<exists>f. strict_mono f \<and> monoseq (\<lambda>n. (s (f n)))"
  1161 proof (cases "\<forall>n. \<exists>p>n. \<forall>m\<ge>p. s m \<le> s p")
  1162   case True
  1163   then have "\<exists>f. \<forall>n. (\<forall>m\<ge>f n. s m \<le> s (f n)) \<and> f n < f (Suc n)"
  1164     by (intro dependent_nat_choice) (auto simp: conj_commute)
  1165   then obtain f :: "nat \<Rightarrow> nat" 
  1166     where f: "strict_mono f" and mono: "\<And>n m. f n \<le> m \<Longrightarrow> s m \<le> s (f n)"
  1167     by (auto simp: strict_mono_Suc_iff)
  1168   then have "incseq f"
  1169     unfolding strict_mono_Suc_iff incseq_Suc_iff by (auto intro: less_imp_le)
  1170   then have "monoseq (\<lambda>n. s (f n))"
  1171     by (auto simp add: incseq_def intro!: mono monoI2)
  1172   with f show ?thesis
  1173     by auto
  1174 next
  1175   case False
  1176   then obtain N where N: "p > N \<Longrightarrow> \<exists>m>p. s p < s m" for p
  1177     by (force simp: not_le le_less)
  1178   have "\<exists>f. \<forall>n. N < f n \<and> f n < f (Suc n) \<and> s (f n) \<le> s (f (Suc n))"
  1179   proof (intro dependent_nat_choice)
  1180     fix x
  1181     assume "N < x" with N[of x]
  1182     show "\<exists>y>N. x < y \<and> s x \<le> s y"
  1183       by (auto intro: less_trans)
  1184   qed auto
  1185   then show ?thesis
  1186     by (auto simp: monoseq_iff incseq_Suc_iff strict_mono_Suc_iff)
  1187 qed
  1188 
  1189 lemma seq_suble:
  1190   assumes sf: "strict_mono (f :: nat \<Rightarrow> nat)"
  1191   shows "n \<le> f n"
  1192 proof (induct n)
  1193   case 0
  1194   show ?case by simp
  1195 next
  1196   case (Suc n)
  1197   with sf [unfolded strict_mono_Suc_iff, rule_format, of n] have "n < f (Suc n)"
  1198      by arith
  1199   then show ?case by arith
  1200 qed
  1201 
  1202 lemma eventually_subseq:
  1203   "strict_mono r \<Longrightarrow> eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
  1204   unfolding eventually_sequentially by (metis seq_suble le_trans)
  1205 
  1206 lemma not_eventually_sequentiallyD:
  1207   assumes "\<not> eventually P sequentially"
  1208   shows "\<exists>r::nat\<Rightarrow>nat. strict_mono r \<and> (\<forall>n. \<not> P (r n))"
  1209 proof -
  1210   from assms have "\<forall>n. \<exists>m\<ge>n. \<not> P m"
  1211     unfolding eventually_sequentially by (simp add: not_less)
  1212   then obtain r where "\<And>n. r n \<ge> n" "\<And>n. \<not> P (r n)"
  1213     by (auto simp: choice_iff)
  1214   then show ?thesis
  1215     by (auto intro!: exI[of _ "\<lambda>n. r (((Suc \<circ> r) ^^ Suc n) 0)"]
  1216              simp: less_eq_Suc_le strict_mono_Suc_iff)
  1217 qed
  1218 
  1219 lemma sequentially_offset: 
  1220   assumes "eventually (\<lambda>i. P i) sequentially"
  1221   shows "eventually (\<lambda>i. P (i + k)) sequentially"
  1222   using assms by (rule eventually_sequentially_seg [THEN iffD2])
  1223 
  1224 lemma seq_offset_neg: 
  1225   "(f \<longlongrightarrow> l) sequentially \<Longrightarrow> ((\<lambda>i. f(i - k)) \<longlongrightarrow> l) sequentially"
  1226   apply (erule filterlim_compose)
  1227   apply (simp add: filterlim_def le_sequentially eventually_filtermap eventually_sequentially, arith)
  1228   done
  1229 
  1230 lemma filterlim_subseq: "strict_mono f \<Longrightarrow> filterlim f sequentially sequentially"
  1231   unfolding filterlim_iff by (metis eventually_subseq)
  1232 
  1233 lemma strict_mono_o: "strict_mono r \<Longrightarrow> strict_mono s \<Longrightarrow> strict_mono (r \<circ> s)"
  1234   unfolding strict_mono_def by simp
  1235 
  1236 lemma incseq_imp_monoseq:  "incseq X \<Longrightarrow> monoseq X"
  1237   by (simp add: incseq_def monoseq_def)
  1238 
  1239 lemma decseq_imp_monoseq:  "decseq X \<Longrightarrow> monoseq X"
  1240   by (simp add: decseq_def monoseq_def)
  1241 
  1242 lemma decseq_eq_incseq: "decseq X = incseq (\<lambda>n. - X n)"
  1243   for X :: "nat \<Rightarrow> 'a::ordered_ab_group_add"
  1244   by (simp add: decseq_def incseq_def)
  1245 
  1246 lemma INT_decseq_offset:
  1247   assumes "decseq F"
  1248   shows "(\<Inter>i. F i) = (\<Inter>i\<in>{n..}. F i)"
  1249 proof safe
  1250   fix x i
  1251   assume x: "x \<in> (\<Inter>i\<in>{n..}. F i)"
  1252   show "x \<in> F i"
  1253   proof cases
  1254     from x have "x \<in> F n" by auto
  1255     also assume "i \<le> n" with \<open>decseq F\<close> have "F n \<subseteq> F i"
  1256       unfolding decseq_def by simp
  1257     finally show ?thesis .
  1258   qed (insert x, simp)
  1259 qed auto
  1260 
  1261 lemma LIMSEQ_const_iff: "(\<lambda>n. k) \<longlonglongrightarrow> l \<longleftrightarrow> k = l"
  1262   for k l :: "'a::t2_space"
  1263   using trivial_limit_sequentially by (rule tendsto_const_iff)
  1264 
  1265 lemma LIMSEQ_SUP: "incseq X \<Longrightarrow> X \<longlonglongrightarrow> (SUP i. X i :: 'a::{complete_linorder,linorder_topology})"
  1266   by (intro increasing_tendsto)
  1267     (auto simp: SUP_upper less_SUP_iff incseq_def eventually_sequentially intro: less_le_trans)
  1268 
  1269 lemma LIMSEQ_INF: "decseq X \<Longrightarrow> X \<longlonglongrightarrow> (INF i. X i :: 'a::{complete_linorder,linorder_topology})"
  1270   by (intro decreasing_tendsto)
  1271     (auto simp: INF_lower INF_less_iff decseq_def eventually_sequentially intro: le_less_trans)
  1272 
  1273 lemma LIMSEQ_ignore_initial_segment: "f \<longlonglongrightarrow> a \<Longrightarrow> (\<lambda>n. f (n + k)) \<longlonglongrightarrow> a"
  1274   unfolding tendsto_def by (subst eventually_sequentially_seg[where k=k])
  1275 
  1276 lemma LIMSEQ_offset: "(\<lambda>n. f (n + k)) \<longlonglongrightarrow> a \<Longrightarrow> f \<longlonglongrightarrow> a"
  1277   unfolding tendsto_def
  1278   by (subst (asm) eventually_sequentially_seg[where k=k])
  1279 
  1280 lemma LIMSEQ_Suc: "f \<longlonglongrightarrow> l \<Longrightarrow> (\<lambda>n. f (Suc n)) \<longlonglongrightarrow> l"
  1281   by (drule LIMSEQ_ignore_initial_segment [where k="Suc 0"]) simp
  1282 
  1283 lemma LIMSEQ_imp_Suc: "(\<lambda>n. f (Suc n)) \<longlonglongrightarrow> l \<Longrightarrow> f \<longlonglongrightarrow> l"
  1284   by (rule LIMSEQ_offset [where k="Suc 0"]) simp
  1285 
  1286 lemma LIMSEQ_Suc_iff: "(\<lambda>n. f (Suc n)) \<longlonglongrightarrow> l = f \<longlonglongrightarrow> l"
  1287   by (blast intro: LIMSEQ_imp_Suc LIMSEQ_Suc)
  1288 
  1289 lemma LIMSEQ_unique: "X \<longlonglongrightarrow> a \<Longrightarrow> X \<longlonglongrightarrow> b \<Longrightarrow> a = b"
  1290   for a b :: "'a::t2_space"
  1291   using trivial_limit_sequentially by (rule tendsto_unique)
  1292 
  1293 lemma LIMSEQ_le_const: "X \<longlonglongrightarrow> x \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. a \<le> X n \<Longrightarrow> a \<le> x"
  1294   for a x :: "'a::linorder_topology"
  1295   by (simp add: eventually_at_top_linorder tendsto_lowerbound)
  1296 
  1297 lemma LIMSEQ_le: "X \<longlonglongrightarrow> x \<Longrightarrow> Y \<longlonglongrightarrow> y \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. X n \<le> Y n \<Longrightarrow> x \<le> y"
  1298   for x y :: "'a::linorder_topology"
  1299   using tendsto_le[of sequentially Y y X x] by (simp add: eventually_sequentially)
  1300 
  1301 lemma LIMSEQ_le_const2: "X \<longlonglongrightarrow> x \<Longrightarrow> \<exists>N. \<forall>n\<ge>N. X n \<le> a \<Longrightarrow> x \<le> a"
  1302   for a x :: "'a::linorder_topology"
  1303   by (rule LIMSEQ_le[of X x "\<lambda>n. a"]) auto
  1304 
  1305 lemma convergentD: "convergent X \<Longrightarrow> \<exists>L. X \<longlonglongrightarrow> L"
  1306   by (simp add: convergent_def)
  1307 
  1308 lemma convergentI: "X \<longlonglongrightarrow> L \<Longrightarrow> convergent X"
  1309   by (auto simp add: convergent_def)
  1310 
  1311 lemma convergent_LIMSEQ_iff: "convergent X \<longleftrightarrow> X \<longlonglongrightarrow> lim X"
  1312   by (auto intro: theI LIMSEQ_unique simp add: convergent_def lim_def)
  1313 
  1314 lemma convergent_const: "convergent (\<lambda>n. c)"
  1315   by (rule convergentI) (rule tendsto_const)
  1316 
  1317 lemma monoseq_le:
  1318   "monoseq a \<Longrightarrow> a \<longlonglongrightarrow> x \<Longrightarrow>
  1319     (\<forall>n. a n \<le> x) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n) \<or>
  1320     (\<forall>n. x \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m)"
  1321   for x :: "'a::linorder_topology"
  1322   by (metis LIMSEQ_le_const LIMSEQ_le_const2 decseq_def incseq_def monoseq_iff)
  1323 
  1324 lemma LIMSEQ_subseq_LIMSEQ: "X \<longlonglongrightarrow> L \<Longrightarrow> strict_mono f \<Longrightarrow> (X \<circ> f) \<longlonglongrightarrow> L"
  1325   unfolding comp_def by (rule filterlim_compose [of X, OF _ filterlim_subseq])
  1326 
  1327 lemma convergent_subseq_convergent: "convergent X \<Longrightarrow> strict_mono f \<Longrightarrow> convergent (X \<circ> f)"
  1328   by (auto simp: convergent_def intro: LIMSEQ_subseq_LIMSEQ)
  1329 
  1330 lemma limI: "X \<longlonglongrightarrow> L \<Longrightarrow> lim X = L"
  1331   by (rule tendsto_Lim) (rule trivial_limit_sequentially)
  1332 
  1333 lemma lim_le: "convergent f \<Longrightarrow> (\<And>n. f n \<le> x) \<Longrightarrow> lim f \<le> x"
  1334   for x :: "'a::linorder_topology"
  1335   using LIMSEQ_le_const2[of f "lim f" x] by (simp add: convergent_LIMSEQ_iff)
  1336 
  1337 lemma lim_const [simp]: "lim (\<lambda>m. a) = a"
  1338   by (simp add: limI)
  1339 
  1340 
  1341 subsubsection \<open>Increasing and Decreasing Series\<close>
  1342 
  1343 lemma incseq_le: "incseq X \<Longrightarrow> X \<longlonglongrightarrow> L \<Longrightarrow> X n \<le> L"
  1344   for L :: "'a::linorder_topology"
  1345   by (metis incseq_def LIMSEQ_le_const)
  1346 
  1347 lemma decseq_le: "decseq X \<Longrightarrow> X \<longlonglongrightarrow> L \<Longrightarrow> L \<le> X n"
  1348   for L :: "'a::linorder_topology"
  1349   by (metis decseq_def LIMSEQ_le_const2)
  1350 
  1351 
  1352 subsection \<open>First countable topologies\<close>
  1353 
  1354 class first_countable_topology = topological_space +
  1355   assumes first_countable_basis:
  1356     "\<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))"
  1357 
  1358 lemma (in first_countable_topology) countable_basis_at_decseq:
  1359   obtains A :: "nat \<Rightarrow> 'a set" where
  1360     "\<And>i. open (A i)" "\<And>i. x \<in> (A i)"
  1361     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  1362 proof atomize_elim
  1363   from first_countable_basis[of x] obtain A :: "nat \<Rightarrow> 'a set"
  1364     where nhds: "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1365       and incl: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>i. A i \<subseteq> S"
  1366     by auto
  1367   define F where "F n = (\<Inter>i\<le>n. A i)" for n
  1368   show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and>
  1369     (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially)"
  1370   proof (safe intro!: exI[of _ F])
  1371     fix i
  1372     show "open (F i)"
  1373       using nhds(1) by (auto simp: F_def)
  1374     show "x \<in> F i"
  1375       using nhds(2) by (auto simp: F_def)
  1376   next
  1377     fix S
  1378     assume "open S" "x \<in> S"
  1379     from incl[OF this] obtain i where "F i \<subseteq> S"
  1380       unfolding F_def by auto
  1381     moreover have "\<And>j. i \<le> j \<Longrightarrow> F j \<subseteq> F i"
  1382       by (simp add: Inf_superset_mono F_def image_mono)
  1383     ultimately show "eventually (\<lambda>i. F i \<subseteq> S) sequentially"
  1384       by (auto simp: eventually_sequentially)
  1385   qed
  1386 qed
  1387 
  1388 lemma (in first_countable_topology) nhds_countable:
  1389   obtains X :: "nat \<Rightarrow> 'a set"
  1390   where "decseq X" "\<And>n. open (X n)" "\<And>n. x \<in> X n" "nhds x = (INF n. principal (X n))"
  1391 proof -
  1392   from first_countable_basis obtain A :: "nat \<Rightarrow> 'a set"
  1393     where *: "\<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"
  1394     by metis
  1395   show thesis
  1396   proof
  1397     show "decseq (\<lambda>n. \<Inter>i\<le>n. A i)"
  1398       by (simp add: antimono_iff_le_Suc atMost_Suc)
  1399     show "x \<in> (\<Inter>i\<le>n. A i)" "\<And>n. open (\<Inter>i\<le>n. A i)" for n
  1400       using * by auto
  1401     show "nhds x = (INF n. principal (\<Inter>i\<le>n. A i))"
  1402       using *
  1403       unfolding nhds_def
  1404       apply -
  1405       apply (rule INF_eq)
  1406        apply simp_all
  1407        apply fastforce
  1408       apply (intro exI [of _ "\<Inter>i\<le>n. A i" for n] conjI open_INT)
  1409          apply auto
  1410       done
  1411   qed
  1412 qed
  1413 
  1414 lemma (in first_countable_topology) countable_basis:
  1415   obtains A :: "nat \<Rightarrow> 'a set" where
  1416     "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1417     "\<And>F. (\<forall>n. F n \<in> A n) \<Longrightarrow> F \<longlonglongrightarrow> x"
  1418 proof atomize_elim
  1419   obtain A :: "nat \<Rightarrow> 'a set" where *:
  1420     "\<And>i. open (A i)"
  1421     "\<And>i. x \<in> A i"
  1422     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  1423     by (rule countable_basis_at_decseq) blast
  1424   have "eventually (\<lambda>n. F n \<in> S) sequentially"
  1425     if "\<forall>n. F n \<in> A n" "open S" "x \<in> S" for F S
  1426     using *(3)[of S] that by (auto elim: eventually_mono simp: subset_eq)
  1427   with * 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)"
  1428     by (intro exI[of _ A]) (auto simp: tendsto_def)
  1429 qed
  1430 
  1431 lemma (in first_countable_topology) sequentially_imp_eventually_nhds_within:
  1432   assumes "\<forall>f. (\<forall>n. f n \<in> s) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially"
  1433   shows "eventually P (inf (nhds a) (principal s))"
  1434 proof (rule ccontr)
  1435   obtain A :: "nat \<Rightarrow> 'a set" where *:
  1436     "\<And>i. open (A i)"
  1437     "\<And>i. a \<in> A i"
  1438     "\<And>F. \<forall>n. F n \<in> A n \<Longrightarrow> F \<longlonglongrightarrow> a"
  1439     by (rule countable_basis) blast
  1440   assume "\<not> ?thesis"
  1441   with * have "\<exists>F. \<forall>n. F n \<in> s \<and> F n \<in> A n \<and> \<not> P (F n)"
  1442     unfolding eventually_inf_principal eventually_nhds
  1443     by (intro choice) fastforce
  1444   then obtain F where F: "\<forall>n. F n \<in> s" and "\<forall>n. F n \<in> A n" and F': "\<forall>n. \<not> P (F n)"
  1445     by blast
  1446   with * have "F \<longlonglongrightarrow> a"
  1447     by auto
  1448   then have "eventually (\<lambda>n. P (F n)) sequentially"
  1449     using assms F by simp
  1450   then show False
  1451     by (simp add: F')
  1452 qed
  1453 
  1454 lemma (in first_countable_topology) eventually_nhds_within_iff_sequentially:
  1455   "eventually P (inf (nhds a) (principal s)) \<longleftrightarrow>
  1456     (\<forall>f. (\<forall>n. f n \<in> s) \<and> f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
  1457 proof (safe intro!: sequentially_imp_eventually_nhds_within)
  1458   assume "eventually P (inf (nhds a) (principal s))"
  1459   then obtain S where "open S" "a \<in> S" "\<forall>x\<in>S. x \<in> s \<longrightarrow> P x"
  1460     by (auto simp: eventually_inf_principal eventually_nhds)
  1461   moreover
  1462   fix f
  1463   assume "\<forall>n. f n \<in> s" "f \<longlonglongrightarrow> a"
  1464   ultimately show "eventually (\<lambda>n. P (f n)) sequentially"
  1465     by (auto dest!: topological_tendstoD elim: eventually_mono)
  1466 qed
  1467 
  1468 lemma (in first_countable_topology) eventually_nhds_iff_sequentially:
  1469   "eventually P (nhds a) \<longleftrightarrow> (\<forall>f. f \<longlonglongrightarrow> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
  1470   using eventually_nhds_within_iff_sequentially[of P a UNIV] by simp
  1471 
  1472 lemma tendsto_at_iff_sequentially:
  1473   "(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))"
  1474   for f :: "'a::first_countable_topology \<Rightarrow> _"
  1475   unfolding filterlim_def[of _ "nhds a"] le_filter_def eventually_filtermap
  1476     at_within_def eventually_nhds_within_iff_sequentially comp_def
  1477   by metis
  1478 
  1479 lemma approx_from_above_dense_linorder:
  1480   fixes x::"'a::{dense_linorder, linorder_topology, first_countable_topology}"
  1481   assumes "x < y"
  1482   shows "\<exists>u. (\<forall>n. u n > x) \<and> (u \<longlonglongrightarrow> x)"
  1483 proof -
  1484   obtain A :: "nat \<Rightarrow> 'a set" where A: "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1485                                       "\<And>F. (\<forall>n. F n \<in> A n) \<Longrightarrow> F \<longlonglongrightarrow> x"
  1486     by (metis first_countable_topology_class.countable_basis)
  1487   define u where "u = (\<lambda>n. SOME z. z \<in> A n \<and> z > x)"
  1488   have "\<exists>z. z \<in> U \<and> x < z" if "x \<in> U" "open U" for U
  1489     using open_right[OF `open U` `x \<in> U` `x < y`]
  1490     by (meson atLeastLessThan_iff dense less_imp_le subset_eq)
  1491   then have *: "u n \<in> A n \<and> x < u n" for n
  1492     using `x \<in> A n` `open (A n)` unfolding u_def by (metis (no_types, lifting) someI_ex)
  1493   then have "u \<longlonglongrightarrow> x" using A(3) by simp
  1494   then show ?thesis using * by auto
  1495 qed
  1496 
  1497 lemma approx_from_below_dense_linorder:
  1498   fixes x::"'a::{dense_linorder, linorder_topology, first_countable_topology}"
  1499   assumes "x > y"
  1500   shows "\<exists>u. (\<forall>n. u n < x) \<and> (u \<longlonglongrightarrow> x)"
  1501 proof -
  1502   obtain A :: "nat \<Rightarrow> 'a set" where A: "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1503                                       "\<And>F. (\<forall>n. F n \<in> A n) \<Longrightarrow> F \<longlonglongrightarrow> x"
  1504     by (metis first_countable_topology_class.countable_basis)
  1505   define u where "u = (\<lambda>n. SOME z. z \<in> A n \<and> z < x)"
  1506   have "\<exists>z. z \<in> U \<and> z < x" if "x \<in> U" "open U" for U
  1507     using open_left[OF `open U` `x \<in> U` `x > y`]
  1508     by (meson dense greaterThanAtMost_iff less_imp_le subset_eq)
  1509   then have *: "u n \<in> A n \<and> u n < x" for n
  1510     using `x \<in> A n` `open (A n)` unfolding u_def by (metis (no_types, lifting) someI_ex)
  1511   then have "u \<longlonglongrightarrow> x" using A(3) by simp
  1512   then show ?thesis using * by auto
  1513 qed
  1514 
  1515 
  1516 subsection \<open>Function limit at a point\<close>
  1517 
  1518 abbreviation LIM :: "('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
  1519     ("((_)/ \<midarrow>(_)/\<rightarrow> (_))" [60, 0, 60] 60)
  1520   where "f \<midarrow>a\<rightarrow> L \<equiv> (f \<longlongrightarrow> L) (at a)"
  1521 
  1522 lemma tendsto_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> (f \<longlongrightarrow> l) (at a within S) \<longleftrightarrow> (f \<midarrow>a\<rightarrow> l)"
  1523   by (simp add: tendsto_def at_within_open[where S = S])
  1524 
  1525 lemma tendsto_within_open_NO_MATCH:
  1526   "a \<in> S \<Longrightarrow> NO_MATCH UNIV S \<Longrightarrow> open S \<Longrightarrow> (f \<longlongrightarrow> l)(at a within S) \<longleftrightarrow> (f \<longlongrightarrow> l)(at a)"
  1527   for f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  1528   using tendsto_within_open by blast
  1529 
  1530 lemma LIM_const_not_eq[tendsto_intros]: "k \<noteq> L \<Longrightarrow> \<not> (\<lambda>x. k) \<midarrow>a\<rightarrow> L"
  1531   for a :: "'a::perfect_space" and k L :: "'b::t2_space"
  1532   by (simp add: tendsto_const_iff)
  1533 
  1534 lemmas LIM_not_zero = LIM_const_not_eq [where L = 0]
  1535 
  1536 lemma LIM_const_eq: "(\<lambda>x. k) \<midarrow>a\<rightarrow> L \<Longrightarrow> k = L"
  1537   for a :: "'a::perfect_space" and k L :: "'b::t2_space"
  1538   by (simp add: tendsto_const_iff)
  1539 
  1540 lemma LIM_unique: "f \<midarrow>a\<rightarrow> L \<Longrightarrow> f \<midarrow>a\<rightarrow> M \<Longrightarrow> L = M"
  1541   for a :: "'a::perfect_space" and L M :: "'b::t2_space"
  1542   using at_neq_bot by (rule tendsto_unique)
  1543 
  1544 
  1545 text \<open>Limits are equal for functions equal except at limit point.\<close>
  1546 lemma LIM_equal: "\<forall>x. x \<noteq> a \<longrightarrow> f x = g x \<Longrightarrow> (f \<midarrow>a\<rightarrow> l) \<longleftrightarrow> (g \<midarrow>a\<rightarrow> l)"
  1547   by (simp add: tendsto_def eventually_at_topological)
  1548 
  1549 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)"
  1550   by (simp add: LIM_equal)
  1551 
  1552 lemma LIM_cong_limit: "f \<midarrow>x\<rightarrow> L \<Longrightarrow> K = L \<Longrightarrow> f \<midarrow>x\<rightarrow> K"
  1553   by simp
  1554 
  1555 lemma tendsto_at_iff_tendsto_nhds: "g \<midarrow>l\<rightarrow> g l \<longleftrightarrow> (g \<longlongrightarrow> g l) (nhds l)"
  1556   unfolding tendsto_def eventually_at_filter
  1557   by (intro ext all_cong imp_cong) (auto elim!: eventually_mono)
  1558 
  1559 lemma tendsto_compose: "g \<midarrow>l\<rightarrow> g l \<Longrightarrow> (f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) \<longlongrightarrow> g l) F"
  1560   unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])
  1561 
  1562 lemma tendsto_compose_eventually:
  1563   "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"
  1564   by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)
  1565 
  1566 lemma LIM_compose_eventually:
  1567   assumes "f \<midarrow>a\<rightarrow> b"
  1568     and "g \<midarrow>b\<rightarrow> c"
  1569     and "eventually (\<lambda>x. f x \<noteq> b) (at a)"
  1570   shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> c"
  1571   using assms(2,1,3) by (rule tendsto_compose_eventually)
  1572 
  1573 lemma tendsto_compose_filtermap: "((g \<circ> f) \<longlongrightarrow> T) F \<longleftrightarrow> (g \<longlongrightarrow> T) (filtermap f F)"
  1574   by (simp add: filterlim_def filtermap_filtermap comp_def)
  1575 
  1576 lemma tendsto_compose_at:
  1577   assumes f: "(f \<longlongrightarrow> y) F" and g: "(g \<longlongrightarrow> z) (at y)" and fg: "eventually (\<lambda>w. f w = y \<longrightarrow> g y = z) F"
  1578   shows "((g \<circ> f) \<longlongrightarrow> z) F"
  1579 proof -
  1580   have "(\<forall>\<^sub>F a in F. f a \<noteq> y) \<or> g y = z"
  1581     using fg by force
  1582   moreover have "(g \<longlongrightarrow> z) (filtermap f F) \<or> \<not> (\<forall>\<^sub>F a in F. f a \<noteq> y)"
  1583     by (metis (no_types) filterlim_atI filterlim_def tendsto_mono f g)
  1584   ultimately show ?thesis
  1585     by (metis (no_types) f filterlim_compose filterlim_filtermap g tendsto_at_iff_tendsto_nhds tendsto_compose_filtermap)
  1586 qed
  1587 
  1588 
  1589 subsubsection \<open>Relation of \<open>LIM\<close> and \<open>LIMSEQ\<close>\<close>
  1590 
  1591 lemma (in first_countable_topology) sequentially_imp_eventually_within:
  1592   "(\<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>
  1593     eventually P (at a within s)"
  1594   unfolding at_within_def
  1595   by (intro sequentially_imp_eventually_nhds_within) auto
  1596 
  1597 lemma (in first_countable_topology) sequentially_imp_eventually_at:
  1598   "(\<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)"
  1599   using sequentially_imp_eventually_within [where s=UNIV] by simp
  1600 
  1601 lemma LIMSEQ_SEQ_conv1:
  1602   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  1603   assumes f: "f \<midarrow>a\<rightarrow> l"
  1604   shows "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. f (S n)) \<longlonglongrightarrow> l"
  1605   using tendsto_compose_eventually [OF f, where F=sequentially] by simp
  1606 
  1607 lemma LIMSEQ_SEQ_conv2:
  1608   fixes f :: "'a::first_countable_topology \<Rightarrow> 'b::topological_space"
  1609   assumes "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. f (S n)) \<longlonglongrightarrow> l"
  1610   shows "f \<midarrow>a\<rightarrow> l"
  1611   using assms unfolding tendsto_def [where l=l] by (simp add: sequentially_imp_eventually_at)
  1612 
  1613 lemma LIMSEQ_SEQ_conv: "(\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S \<longlonglongrightarrow> a \<longrightarrow> (\<lambda>n. X (S n)) \<longlonglongrightarrow> L) \<longleftrightarrow> X \<midarrow>a\<rightarrow> L"
  1614   for a :: "'a::first_countable_topology" and L :: "'b::topological_space"
  1615   using LIMSEQ_SEQ_conv2 LIMSEQ_SEQ_conv1 ..
  1616 
  1617 lemma sequentially_imp_eventually_at_left:
  1618   fixes a :: "'a::{linorder_topology,first_countable_topology}"
  1619   assumes b[simp]: "b < a"
  1620     and *: "\<And>f. (\<And>n. b < f n) \<Longrightarrow> (\<And>n. f n < a) \<Longrightarrow> incseq f \<Longrightarrow> f \<longlonglongrightarrow> a \<Longrightarrow>
  1621       eventually (\<lambda>n. P (f n)) sequentially"
  1622   shows "eventually P (at_left a)"
  1623 proof (safe intro!: sequentially_imp_eventually_within)
  1624   fix X
  1625   assume X: "\<forall>n. X n \<in> {..< a} \<and> X n \<noteq> a" "X \<longlonglongrightarrow> a"
  1626   show "eventually (\<lambda>n. P (X n)) sequentially"
  1627   proof (rule ccontr)
  1628     assume neg: "\<not> ?thesis"
  1629     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))"
  1630       (is "\<exists>s. ?P s")
  1631     proof (rule dependent_nat_choice)
  1632       have "\<not> eventually (\<lambda>n. b < X n \<longrightarrow> P (X n)) sequentially"
  1633         by (intro not_eventually_impI neg order_tendstoD(1) [OF X(2) b])
  1634       then show "\<exists>x. \<not> P (X x) \<and> b < X x"
  1635         by (auto dest!: not_eventuallyD)
  1636     next
  1637       fix x n
  1638       have "\<not> eventually (\<lambda>n. Suc x \<le> n \<longrightarrow> b < X n \<longrightarrow> X x < X n \<longrightarrow> P (X n)) sequentially"
  1639         using X
  1640         by (intro not_eventually_impI order_tendstoD(1)[OF X(2)] eventually_ge_at_top neg) auto
  1641       then show "\<exists>n. (\<not> P (X n) \<and> b < X n) \<and> (X x \<le> X n \<and> Suc x \<le> n)"
  1642         by (auto dest!: not_eventuallyD)
  1643     qed
  1644     then obtain s where "?P s" ..
  1645     with X have "b < X (s n)"
  1646       and "X (s n) < a"
  1647       and "incseq (\<lambda>n. X (s n))"
  1648       and "(\<lambda>n. X (s n)) \<longlonglongrightarrow> a"
  1649       and "\<not> P (X (s n))"
  1650       for n
  1651       by (auto simp: strict_mono_Suc_iff Suc_le_eq incseq_Suc_iff
  1652           intro!: LIMSEQ_subseq_LIMSEQ[OF \<open>X \<longlonglongrightarrow> a\<close>, unfolded comp_def])
  1653     from *[OF this(1,2,3,4)] this(5) show False
  1654       by auto
  1655   qed
  1656 qed
  1657 
  1658 lemma tendsto_at_left_sequentially:
  1659   fixes a b :: "'b::{linorder_topology,first_countable_topology}"
  1660   assumes "b < a"
  1661   assumes *: "\<And>S. (\<And>n. S n < a) \<Longrightarrow> (\<And>n. b < S n) \<Longrightarrow> incseq S \<Longrightarrow> S \<longlonglongrightarrow> a \<Longrightarrow>
  1662     (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
  1663   shows "(X \<longlongrightarrow> L) (at_left a)"
  1664   using assms by (simp add: tendsto_def [where l=L] sequentially_imp_eventually_at_left)
  1665 
  1666 lemma sequentially_imp_eventually_at_right:
  1667   fixes a b :: "'a::{linorder_topology,first_countable_topology}"
  1668   assumes b[simp]: "a < b"
  1669   assumes *: "\<And>f. (\<And>n. a < f n) \<Longrightarrow> (\<And>n. f n < b) \<Longrightarrow> decseq f \<Longrightarrow> f \<longlonglongrightarrow> a \<Longrightarrow>
  1670     eventually (\<lambda>n. P (f n)) sequentially"
  1671   shows "eventually P (at_right a)"
  1672 proof (safe intro!: sequentially_imp_eventually_within)
  1673   fix X
  1674   assume X: "\<forall>n. X n \<in> {a <..} \<and> X n \<noteq> a" "X \<longlonglongrightarrow> a"
  1675   show "eventually (\<lambda>n. P (X n)) sequentially"
  1676   proof (rule ccontr)
  1677     assume neg: "\<not> ?thesis"
  1678     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))"
  1679       (is "\<exists>s. ?P s")
  1680     proof (rule dependent_nat_choice)
  1681       have "\<not> eventually (\<lambda>n. X n < b \<longrightarrow> P (X n)) sequentially"
  1682         by (intro not_eventually_impI neg order_tendstoD(2) [OF X(2) b])
  1683       then show "\<exists>x. \<not> P (X x) \<and> X x < b"
  1684         by (auto dest!: not_eventuallyD)
  1685     next
  1686       fix x n
  1687       have "\<not> eventually (\<lambda>n. Suc x \<le> n \<longrightarrow> X n < b \<longrightarrow> X n < X x \<longrightarrow> P (X n)) sequentially"
  1688         using X
  1689         by (intro not_eventually_impI order_tendstoD(2)[OF X(2)] eventually_ge_at_top neg) auto
  1690       then show "\<exists>n. (\<not> P (X n) \<and> X n < b) \<and> (X n \<le> X x \<and> Suc x \<le> n)"
  1691         by (auto dest!: not_eventuallyD)
  1692     qed
  1693     then obtain s where "?P s" ..
  1694     with X have "a < X (s n)"
  1695       and "X (s n) < b"
  1696       and "decseq (\<lambda>n. X (s n))"
  1697       and "(\<lambda>n. X (s n)) \<longlonglongrightarrow> a"
  1698       and "\<not> P (X (s n))"
  1699       for n
  1700       by (auto simp: strict_mono_Suc_iff Suc_le_eq decseq_Suc_iff
  1701           intro!: LIMSEQ_subseq_LIMSEQ[OF \<open>X \<longlonglongrightarrow> a\<close>, unfolded comp_def])
  1702     from *[OF this(1,2,3,4)] this(5) show False
  1703       by auto
  1704   qed
  1705 qed
  1706 
  1707 lemma tendsto_at_right_sequentially:
  1708   fixes a :: "_ :: {linorder_topology, first_countable_topology}"
  1709   assumes "a < b"
  1710     and *: "\<And>S. (\<And>n. a < S n) \<Longrightarrow> (\<And>n. S n < b) \<Longrightarrow> decseq S \<Longrightarrow> S \<longlonglongrightarrow> a \<Longrightarrow>
  1711       (\<lambda>n. X (S n)) \<longlonglongrightarrow> L"
  1712   shows "(X \<longlongrightarrow> L) (at_right a)"
  1713   using assms by (simp add: tendsto_def [where l=L] sequentially_imp_eventually_at_right)
  1714 
  1715 
  1716 subsection \<open>Continuity\<close>
  1717 
  1718 subsubsection \<open>Continuity on a set\<close>
  1719 
  1720 definition continuous_on :: "'a set \<Rightarrow> ('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"
  1721   where "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f \<longlongrightarrow> f x) (at x within s))"
  1722 
  1723 lemma continuous_on_cong [cong]:
  1724   "s = t \<Longrightarrow> (\<And>x. x \<in> t \<Longrightarrow> f x = g x) \<Longrightarrow> continuous_on s f \<longleftrightarrow> continuous_on t g"
  1725   unfolding continuous_on_def
  1726   by (intro ball_cong filterlim_cong) (auto simp: eventually_at_filter)
  1727 
  1728 lemma continuous_on_strong_cong:
  1729   "s = t \<Longrightarrow> (\<And>x. x \<in> t =simp=> f x = g x) \<Longrightarrow> continuous_on s f \<longleftrightarrow> continuous_on t g"
  1730   unfolding simp_implies_def by (rule continuous_on_cong)
  1731 
  1732 lemma continuous_on_topological:
  1733   "continuous_on s f \<longleftrightarrow>
  1734     (\<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)))"
  1735   unfolding continuous_on_def tendsto_def eventually_at_topological by metis
  1736 
  1737 lemma continuous_on_open_invariant:
  1738   "continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s))"
  1739 proof safe
  1740   fix B :: "'b set"
  1741   assume "continuous_on s f" "open B"
  1742   then have "\<forall>x\<in>f -` B \<inter> s. (\<exists>A. open A \<and> x \<in> A \<and> s \<inter> A \<subseteq> f -` B)"
  1743     by (auto simp: continuous_on_topological subset_eq Ball_def imp_conjL)
  1744   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"
  1745     unfolding bchoice_iff ..
  1746   then show "\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s"
  1747     by (intro exI[of _ "\<Union>x\<in>f -` B \<inter> s. A x"]) auto
  1748 next
  1749   assume B: "\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s)"
  1750   show "continuous_on s f"
  1751     unfolding continuous_on_topological
  1752   proof safe
  1753     fix x B
  1754     assume "x \<in> s" "open B" "f x \<in> B"
  1755     with B obtain A where A: "open A" "A \<inter> s = f -` B \<inter> s"
  1756       by auto
  1757     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)"
  1758       by (intro exI[of _ A]) auto
  1759   qed
  1760 qed
  1761 
  1762 lemma continuous_on_open_vimage:
  1763   "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> open (f -` B \<inter> s))"
  1764   unfolding continuous_on_open_invariant
  1765   by (metis open_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
  1766 
  1767 corollary continuous_imp_open_vimage:
  1768   assumes "continuous_on s f" "open s" "open B" "f -` B \<subseteq> s"
  1769   shows "open (f -` B)"
  1770   by (metis assms continuous_on_open_vimage le_iff_inf)
  1771 
  1772 corollary open_vimage[continuous_intros]:
  1773   assumes "open s"
  1774     and "continuous_on UNIV f"
  1775   shows "open (f -` s)"
  1776   using assms by (simp add: continuous_on_open_vimage [OF open_UNIV])
  1777 
  1778 lemma continuous_on_closed_invariant:
  1779   "continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> (\<exists>A. closed A \<and> A \<inter> s = f -` B \<inter> s))"
  1780 proof -
  1781   have *: "(\<And>A. P A \<longleftrightarrow> Q (- A)) \<Longrightarrow> (\<forall>A. P A) \<longleftrightarrow> (\<forall>A. Q A)"
  1782     for P Q :: "'b set \<Rightarrow> bool"
  1783     by (metis double_compl)
  1784   show ?thesis
  1785     unfolding continuous_on_open_invariant
  1786     by (intro *) (auto simp: open_closed[symmetric])
  1787 qed
  1788 
  1789 lemma continuous_on_closed_vimage:
  1790   "closed s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> closed (f -` B \<inter> s))"
  1791   unfolding continuous_on_closed_invariant
  1792   by (metis closed_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
  1793 
  1794 corollary closed_vimage_Int[continuous_intros]:
  1795   assumes "closed s"
  1796     and "continuous_on t f"
  1797     and t: "closed t"
  1798   shows "closed (f -` s \<inter> t)"
  1799   using assms by (simp add: continuous_on_closed_vimage [OF t])
  1800 
  1801 corollary closed_vimage[continuous_intros]:
  1802   assumes "closed s"
  1803     and "continuous_on UNIV f"
  1804   shows "closed (f -` s)"
  1805   using closed_vimage_Int [OF assms] by simp
  1806 
  1807 lemma continuous_on_empty [simp]: "continuous_on {} f"
  1808   by (simp add: continuous_on_def)
  1809 
  1810 lemma continuous_on_sing [simp]: "continuous_on {x} f"
  1811   by (simp add: continuous_on_def at_within_def)
  1812 
  1813 lemma continuous_on_open_Union:
  1814   "(\<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"
  1815   unfolding continuous_on_def
  1816   by safe (metis open_Union at_within_open UnionI)
  1817 
  1818 lemma continuous_on_open_UN:
  1819   "(\<And>s. s \<in> S \<Longrightarrow> open (A s)) \<Longrightarrow> (\<And>s. s \<in> S \<Longrightarrow> continuous_on (A s) f) \<Longrightarrow>
  1820     continuous_on (\<Union>s\<in>S. A s) f"
  1821   by (rule continuous_on_open_Union) auto
  1822 
  1823 lemma continuous_on_open_Un:
  1824   "open s \<Longrightarrow> open t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t f \<Longrightarrow> continuous_on (s \<union> t) f"
  1825   using continuous_on_open_Union [of "{s,t}"] by auto
  1826 
  1827 lemma continuous_on_closed_Un:
  1828   "closed s \<Longrightarrow> closed t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t f \<Longrightarrow> continuous_on (s \<union> t) f"
  1829   by (auto simp add: continuous_on_closed_vimage closed_Un Int_Un_distrib)
  1830 
  1831 lemma continuous_on_If:
  1832   assumes closed: "closed s" "closed t"
  1833     and cont: "continuous_on s f" "continuous_on t g"
  1834     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"
  1835   shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
  1836     (is "continuous_on _ ?h")
  1837 proof-
  1838   from P have "\<forall>x\<in>s. f x = ?h x" "\<forall>x\<in>t. g x = ?h x"
  1839     by auto
  1840   with cont have "continuous_on s ?h" "continuous_on t ?h"
  1841     by simp_all
  1842   with closed show ?thesis
  1843     by (rule continuous_on_closed_Un)
  1844 qed
  1845 
  1846 lemma continuous_on_cases:
  1847   "closed s \<Longrightarrow> closed t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t g \<Longrightarrow>
  1848     \<forall>x. (x\<in>s \<and> \<not> P x) \<or> (x \<in> t \<and> P x) \<longrightarrow> f x = g x \<Longrightarrow>
  1849     continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)"
  1850   by (rule continuous_on_If) auto
  1851 
  1852 lemma continuous_on_id[continuous_intros]: "continuous_on s (\<lambda>x. x)"
  1853   unfolding continuous_on_def by fast
  1854 
  1855 lemma continuous_on_id'[continuous_intros]: "continuous_on s id"
  1856   unfolding continuous_on_def id_def by fast
  1857 
  1858 lemma continuous_on_const[continuous_intros]: "continuous_on s (\<lambda>x. c)"
  1859   unfolding continuous_on_def by auto
  1860 
  1861 lemma continuous_on_subset: "continuous_on s f \<Longrightarrow> t \<subseteq> s \<Longrightarrow> continuous_on t f"
  1862   unfolding continuous_on_def
  1863   by (metis subset_eq tendsto_within_subset)
  1864 
  1865 lemma continuous_on_compose[continuous_intros]:
  1866   "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g \<circ> f)"
  1867   unfolding continuous_on_topological by simp metis
  1868 
  1869 lemma continuous_on_compose2:
  1870   "continuous_on t g \<Longrightarrow> continuous_on s f \<Longrightarrow> f ` s \<subseteq> t \<Longrightarrow> continuous_on s (\<lambda>x. g (f x))"
  1871   using continuous_on_compose[of s f g] continuous_on_subset by (force simp add: comp_def)
  1872 
  1873 lemma continuous_on_generate_topology:
  1874   assumes *: "open = generate_topology X"
  1875     and **: "\<And>B. B \<in> X \<Longrightarrow> \<exists>C. open C \<and> C \<inter> A = f -` B \<inter> A"
  1876   shows "continuous_on A f"
  1877   unfolding continuous_on_open_invariant
  1878 proof safe
  1879   fix B :: "'a set"
  1880   assume "open B"
  1881   then show "\<exists>C. open C \<and> C \<inter> A = f -` B \<inter> A"
  1882     unfolding *
  1883   proof induct
  1884     case (UN K)
  1885     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"
  1886       by metis
  1887     then show ?case
  1888       by (intro exI[of _ "\<Union>k\<in>K. C k"]) blast
  1889   qed (auto intro: **)
  1890 qed
  1891 
  1892 lemma continuous_onI_mono:
  1893   fixes f :: "'a::linorder_topology \<Rightarrow> 'b::{dense_order,linorder_topology}"
  1894   assumes "open (f`A)"
  1895     and mono: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
  1896   shows "continuous_on A f"
  1897 proof (rule continuous_on_generate_topology[OF open_generated_order], safe)
  1898   have monoD: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> f x < f y \<Longrightarrow> x < y"
  1899     by (auto simp: not_le[symmetric] mono)
  1900   have "\<exists>x. x \<in> A \<and> f x < b \<and> a < x" if a: "a \<in> A" and fa: "f a < b" for a b
  1901   proof -
  1902     obtain y where "f a < y" "{f a ..< y} \<subseteq> f`A"
  1903       using open_right[OF \<open>open (f`A)\<close>, of "f a" b] a fa
  1904       by auto
  1905     obtain z where z: "f a < z" "z < min b y"
  1906       using dense[of "f a" "min b y"] \<open>f a < y\<close> \<open>f a < b\<close> by auto
  1907     then obtain c where "z = f c" "c \<in> A"
  1908       using \<open>{f a ..< y} \<subseteq> f`A\<close>[THEN subsetD, of z] by (auto simp: less_imp_le)
  1909     with a z show ?thesis
  1910       by (auto intro!: exI[of _ c] simp: monoD)
  1911   qed
  1912   then show "\<exists>C. open C \<and> C \<inter> A = f -` {..<b} \<inter> A" for b
  1913     by (intro exI[of _ "(\<Union>x\<in>{x\<in>A. f x < b}. {..< x})"])
  1914        (auto intro: le_less_trans[OF mono] less_imp_le)
  1915 
  1916   have "\<exists>x. x \<in> A \<and> b < f x \<and> x < a" if a: "a \<in> A" and fa: "b < f a" for a b
  1917   proof -
  1918     note a fa
  1919     moreover
  1920     obtain y where "y < f a" "{y <.. f a} \<subseteq> f`A"
  1921       using open_left[OF \<open>open (f`A)\<close>, of "f a" b]  a fa
  1922       by auto
  1923     then obtain z where z: "max b y < z" "z < f a"
  1924       using dense[of "max b y" "f a"] \<open>y < f a\<close> \<open>b < f a\<close> by auto
  1925     then obtain c where "z = f c" "c \<in> A"
  1926       using \<open>{y <.. f a} \<subseteq> f`A\<close>[THEN subsetD, of z] by (auto simp: less_imp_le)
  1927     with a z show ?thesis
  1928       by (auto intro!: exI[of _ c] simp: monoD)
  1929   qed
  1930   then show "\<exists>C. open C \<and> C \<inter> A = f -` {b <..} \<inter> A" for b
  1931     by (intro exI[of _ "(\<Union>x\<in>{x\<in>A. b < f x}. {x <..})"])
  1932        (auto intro: less_le_trans[OF _ mono] less_imp_le)
  1933 qed
  1934 
  1935 lemma continuous_on_IccI:
  1936   "\<lbrakk>(f \<longlongrightarrow> f a) (at_right a);
  1937     (f \<longlongrightarrow> f b) (at_left b);
  1938     (\<And>x. a < x \<Longrightarrow> x < b \<Longrightarrow> f \<midarrow>x\<rightarrow> f x); a < b\<rbrakk> \<Longrightarrow>
  1939     continuous_on {a .. b} f"
  1940   for a::"'a::linorder_topology"
  1941   using at_within_open[of _ "{a<..<b}"]
  1942   by (auto simp: continuous_on_def at_within_Icc_at_right at_within_Icc_at_left le_less
  1943       at_within_Icc_at)
  1944 
  1945 lemma
  1946   fixes a b::"'a::linorder_topology"
  1947   assumes "continuous_on {a .. b} f" "a < b"
  1948   shows continuous_on_Icc_at_rightD: "(f \<longlongrightarrow> f a) (at_right a)"
  1949     and continuous_on_Icc_at_leftD: "(f \<longlongrightarrow> f b) (at_left b)"
  1950   using assms
  1951   by (auto simp: at_within_Icc_at_right at_within_Icc_at_left continuous_on_def
  1952       dest: bspec[where x=a] bspec[where x=b])
  1953 
  1954 
  1955 subsubsection \<open>Continuity at a point\<close>
  1956 
  1957 definition continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool"
  1958   where "continuous F f \<longleftrightarrow> (f \<longlongrightarrow> f (Lim F (\<lambda>x. x))) F"
  1959 
  1960 lemma continuous_bot[continuous_intros, simp]: "continuous bot f"
  1961   unfolding continuous_def by auto
  1962 
  1963 lemma continuous_trivial_limit: "trivial_limit net \<Longrightarrow> continuous net f"
  1964   by simp
  1965 
  1966 lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f \<longlongrightarrow> f x) (at x within s)"
  1967   by (cases "trivial_limit (at x within s)") (auto simp add: Lim_ident_at continuous_def)
  1968 
  1969 lemma continuous_within_topological:
  1970   "continuous (at x within s) f \<longleftrightarrow>
  1971     (\<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)))"
  1972   unfolding continuous_within tendsto_def eventually_at_topological by metis
  1973 
  1974 lemma continuous_within_compose[continuous_intros]:
  1975   "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
  1976     continuous (at x within s) (g \<circ> f)"
  1977   by (simp add: continuous_within_topological) metis
  1978 
  1979 lemma continuous_within_compose2:
  1980   "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
  1981     continuous (at x within s) (\<lambda>x. g (f x))"
  1982   using continuous_within_compose[of x s f g] by (simp add: comp_def)
  1983 
  1984 lemma continuous_at: "continuous (at x) f \<longleftrightarrow> f \<midarrow>x\<rightarrow> f x"
  1985   using continuous_within[of x UNIV f] by simp
  1986 
  1987 lemma continuous_ident[continuous_intros, simp]: "continuous (at x within S) (\<lambda>x. x)"
  1988   unfolding continuous_within by (rule tendsto_ident_at)
  1989 
  1990 lemma continuous_const[continuous_intros, simp]: "continuous F (\<lambda>x. c)"
  1991   unfolding continuous_def by (rule tendsto_const)
  1992 
  1993 lemma continuous_on_eq_continuous_within:
  1994   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. continuous (at x within s) f)"
  1995   unfolding continuous_on_def continuous_within ..
  1996 
  1997 abbreviation isCont :: "('a::t2_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> bool"
  1998   where "isCont f a \<equiv> continuous (at a) f"
  1999 
  2000 lemma isCont_def: "isCont f a \<longleftrightarrow> f \<midarrow>a\<rightarrow> f a"
  2001   by (rule continuous_at)
  2002 
  2003 lemma isCont_cong:
  2004   assumes "eventually (\<lambda>x. f x = g x) (nhds x)"
  2005   shows "isCont f x \<longleftrightarrow> isCont g x"
  2006 proof -
  2007   from assms have [simp]: "f x = g x"
  2008     by (rule eventually_nhds_x_imp_x)
  2009   from assms have "eventually (\<lambda>x. f x = g x) (at x)"
  2010     by (auto simp: eventually_at_filter elim!: eventually_mono)
  2011   with assms have "isCont f x \<longleftrightarrow> isCont g x" unfolding isCont_def
  2012     by (intro filterlim_cong) (auto elim!: eventually_mono)
  2013   with assms show ?thesis by simp
  2014 qed
  2015 
  2016 lemma continuous_at_imp_continuous_at_within: "isCont f x \<Longrightarrow> continuous (at x within s) f"
  2017   by (auto intro: tendsto_mono at_le simp: continuous_at continuous_within)
  2018 
  2019 lemma continuous_on_eq_continuous_at: "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. isCont f x)"
  2020   by (simp add: continuous_on_def continuous_at at_within_open[of _ s])
  2021 
  2022 lemma continuous_within_open: "a \<in> A \<Longrightarrow> open A \<Longrightarrow> continuous (at a within A) f \<longleftrightarrow> isCont f a"
  2023   by (simp add: at_within_open_NO_MATCH)
  2024 
  2025 lemma continuous_at_imp_continuous_on: "\<forall>x\<in>s. isCont f x \<Longrightarrow> continuous_on s f"
  2026   by (auto intro: continuous_at_imp_continuous_at_within simp: continuous_on_eq_continuous_within)
  2027 
  2028 lemma isCont_o2: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (\<lambda>x. g (f x)) a"
  2029   unfolding isCont_def by (rule tendsto_compose)
  2030 
  2031 lemma continuous_at_compose[continuous_intros]: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (g \<circ> f) a"
  2032   unfolding o_def by (rule isCont_o2)
  2033 
  2034 lemma isCont_tendsto_compose: "isCont g l \<Longrightarrow> (f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) \<longlongrightarrow> g l) F"
  2035   unfolding isCont_def by (rule tendsto_compose)
  2036 
  2037 lemma continuous_on_tendsto_compose:
  2038   assumes f_cont: "continuous_on s f"
  2039     and g: "(g \<longlongrightarrow> l) F"
  2040     and l: "l \<in> s"
  2041     and ev: "\<forall>\<^sub>Fx in F. g x \<in> s"
  2042   shows "((\<lambda>x. f (g x)) \<longlongrightarrow> f l) F"
  2043 proof -
  2044   from f_cont l have f: "(f \<longlongrightarrow> f l) (at l within s)"
  2045     by (simp add: continuous_on_def)
  2046   have i: "((\<lambda>x. if g x = l then f l else f (g x)) \<longlongrightarrow> f l) F"
  2047     by (rule filterlim_If)
  2048        (auto intro!: filterlim_compose[OF f] eventually_conj tendsto_mono[OF _ g]
  2049              simp: filterlim_at eventually_inf_principal eventually_mono[OF ev])
  2050   show ?thesis
  2051     by (rule filterlim_cong[THEN iffD1[OF _ i]]) auto
  2052 qed
  2053 
  2054 lemma continuous_within_compose3:
  2055   "isCont g (f x) \<Longrightarrow> continuous (at x within s) f \<Longrightarrow> continuous (at x within s) (\<lambda>x. g (f x))"
  2056   using continuous_at_imp_continuous_at_within continuous_within_compose2 by blast
  2057 
  2058 lemma filtermap_nhds_open_map:
  2059   assumes cont: "isCont f a"
  2060     and open_map: "\<And>S. open S \<Longrightarrow> open (f`S)"
  2061   shows "filtermap f (nhds a) = nhds (f a)"
  2062   unfolding filter_eq_iff
  2063 proof safe
  2064   fix P
  2065   assume "eventually P (filtermap f (nhds a))"
  2066   then obtain S where "open S" "a \<in> S" "\<forall>x\<in>S. P (f x)"
  2067     by (auto simp: eventually_filtermap eventually_nhds)
  2068   then show "eventually P (nhds (f a))"
  2069     unfolding eventually_nhds by (intro exI[of _ "f`S"]) (auto intro!: open_map)
  2070 qed (metis filterlim_iff tendsto_at_iff_tendsto_nhds isCont_def eventually_filtermap cont)
  2071 
  2072 lemma continuous_at_split:
  2073   "continuous (at x) f \<longleftrightarrow> continuous (at_left x) f \<and> continuous (at_right x) f"
  2074   for x :: "'a::linorder_topology"
  2075   by (simp add: continuous_within filterlim_at_split)
  2076 
  2077 text \<open>
  2078   The following open/closed Collect lemmas are ported from
  2079   Sébastien Gouëzel's \<open>Ergodic_Theory\<close>.
  2080 \<close>
  2081 lemma open_Collect_neq:
  2082   fixes f g :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  2083   assumes f: "continuous_on UNIV f" and g: "continuous_on UNIV g"
  2084   shows "open {x. f x \<noteq> g x}"
  2085 proof (rule openI)
  2086   fix t
  2087   assume "t \<in> {x. f x \<noteq> g x}"
  2088   then obtain U V where *: "open U" "open V" "f t \<in> U" "g t \<in> V" "U \<inter> V = {}"
  2089     by (auto simp add: separation_t2)
  2090   with open_vimage[OF \<open>open U\<close> f] open_vimage[OF \<open>open V\<close> g]
  2091   show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {x. f x \<noteq> g x}"
  2092     by (intro exI[of _ "f -` U \<inter> g -` V"]) auto
  2093 qed
  2094 
  2095 lemma closed_Collect_eq:
  2096   fixes f g :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  2097   assumes f: "continuous_on UNIV f" and g: "continuous_on UNIV g"
  2098   shows "closed {x. f x = g x}"
  2099   using open_Collect_neq[OF f g] by (simp add: closed_def Collect_neg_eq)
  2100 
  2101 lemma open_Collect_less:
  2102   fixes f g :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
  2103   assumes f: "continuous_on UNIV f" and g: "continuous_on UNIV g"
  2104   shows "open {x. f x < g x}"
  2105 proof (rule openI)
  2106   fix t
  2107   assume t: "t \<in> {x. f x < g x}"
  2108   show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {x. f x < g x}"
  2109   proof (cases "\<exists>z. f t < z \<and> z < g t")
  2110     case True
  2111     then obtain z where "f t < z \<and> z < g t" by blast
  2112     then show ?thesis
  2113       using open_vimage[OF _ f, of "{..< z}"] open_vimage[OF _ g, of "{z <..}"]
  2114       by (intro exI[of _ "f -` {..<z} \<inter> g -` {z<..}"]) auto
  2115   next
  2116     case False
  2117     then have *: "{g t ..} = {f t <..}" "{..< g t} = {.. f t}"
  2118       using t by (auto intro: leI)
  2119     show ?thesis
  2120       using open_vimage[OF _ f, of "{..< g t}"] open_vimage[OF _ g, of "{f t <..}"] t
  2121       apply (intro exI[of _ "f -` {..< g t} \<inter> g -` {f t<..}"])
  2122       apply (simp add: open_Int)
  2123       apply (auto simp add: *)
  2124       done
  2125   qed
  2126 qed
  2127 
  2128 lemma closed_Collect_le:
  2129   fixes f g :: "'a :: topological_space \<Rightarrow> 'b::linorder_topology"
  2130   assumes f: "continuous_on UNIV f"
  2131     and g: "continuous_on UNIV g"
  2132   shows "closed {x. f x \<le> g x}"
  2133   using open_Collect_less [OF g f]
  2134   by (simp add: closed_def Collect_neg_eq[symmetric] not_le)
  2135 
  2136 
  2137 subsubsection \<open>Open-cover compactness\<close>
  2138 
  2139 context topological_space
  2140 begin
  2141 
  2142 definition compact :: "'a set \<Rightarrow> bool"
  2143   where compact_eq_heine_borel:  (* This name is used for backwards compatibility *)
  2144     "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))"
  2145 
  2146 lemma compactI:
  2147   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'"
  2148   shows "compact s"
  2149   unfolding compact_eq_heine_borel using assms by metis
  2150 
  2151 lemma compact_empty[simp]: "compact {}"
  2152   by (auto intro!: compactI)
  2153 
  2154 lemma compactE: (*related to COMPACT_IMP_HEINE_BOREL in HOL Light*)
  2155   assumes "compact S" "S \<subseteq> \<Union>\<T>" "\<And>B. B \<in> \<T> \<Longrightarrow> open B"
  2156   obtains \<T>' where "\<T>' \<subseteq> \<T>" "finite \<T>'" "S \<subseteq> \<Union>\<T>'"
  2157   by (meson assms compact_eq_heine_borel)
  2158 
  2159 lemma compactE_image:
  2160   assumes "compact S"
  2161     and op: "\<And>T. T \<in> C \<Longrightarrow> open (f T)"
  2162     and S: "S \<subseteq> (\<Union>c\<in>C. f c)"
  2163   obtains C' where "C' \<subseteq> C" and "finite C'" and "S \<subseteq> (\<Union>c\<in>C'. f c)"
  2164     apply (rule compactE[OF \<open>compact S\<close> S])
  2165     using op apply force
  2166     by (metis finite_subset_image)
  2167 
  2168 lemma compact_Int_closed [intro]:
  2169   assumes "compact S"
  2170     and "closed T"
  2171   shows "compact (S \<inter> T)"
  2172 proof (rule compactI)
  2173   fix C
  2174   assume C: "\<forall>c\<in>C. open c"
  2175   assume cover: "S \<inter> T \<subseteq> \<Union>C"
  2176   from C \<open>closed T\<close> have "\<forall>c\<in>C \<union> {- T}. open c"
  2177     by auto
  2178   moreover from cover have "S \<subseteq> \<Union>(C \<union> {- T})"
  2179     by auto
  2180   ultimately have "\<exists>D\<subseteq>C \<union> {- T}. finite D \<and> S \<subseteq> \<Union>D"
  2181     using \<open>compact S\<close> unfolding compact_eq_heine_borel by auto
  2182   then obtain D where "D \<subseteq> C \<union> {- T} \<and> finite D \<and> S \<subseteq> \<Union>D" ..
  2183   then show "\<exists>D\<subseteq>C. finite D \<and> S \<inter> T \<subseteq> \<Union>D"
  2184     by (intro exI[of _ "D - {-T}"]) auto
  2185 qed
  2186 
  2187 lemma compact_diff: "\<lbrakk>compact S; open T\<rbrakk> \<Longrightarrow> compact(S - T)"
  2188   by (simp add: Diff_eq compact_Int_closed open_closed)
  2189 
  2190 lemma inj_setminus: "inj_on uminus (A::'a set set)"
  2191   by (auto simp: inj_on_def)
  2192 
  2193 
  2194 subsection \<open>Finite intersection property\<close>
  2195 
  2196 lemma compact_fip:
  2197   "compact U \<longleftrightarrow>
  2198     (\<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> {})"
  2199   (is "_ \<longleftrightarrow> ?R")
  2200 proof (safe intro!: compact_eq_heine_borel[THEN iffD2])
  2201   fix A
  2202   assume "compact U"
  2203   assume A: "\<forall>a\<in>A. closed a" "U \<inter> \<Inter>A = {}"
  2204   assume fin: "\<forall>B \<subseteq> A. finite B \<longrightarrow> U \<inter> \<Inter>B \<noteq> {}"
  2205   from A have "(\<forall>a\<in>uminus`A. open a) \<and> U \<subseteq> \<Union>(uminus`A)"
  2206     by auto
  2207   with \<open>compact U\<close> obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<subseteq> \<Union>(uminus`B)"
  2208     unfolding compact_eq_heine_borel by (metis subset_image_iff)
  2209   with fin[THEN spec, of B] show False
  2210     by (auto dest: finite_imageD intro: inj_setminus)
  2211 next
  2212   fix A
  2213   assume ?R
  2214   assume "\<forall>a\<in>A. open a" "U \<subseteq> \<Union>A"
  2215   then have "U \<inter> \<Inter>(uminus`A) = {}" "\<forall>a\<in>uminus`A. closed a"
  2216     by auto
  2217   with \<open>?R\<close> obtain B where "B \<subseteq> A" "finite (uminus`B)" "U \<inter> \<Inter>(uminus`B) = {}"
  2218     by (metis subset_image_iff)
  2219   then show "\<exists>T\<subseteq>A. finite T \<and> U \<subseteq> \<Union>T"
  2220     by (auto intro!: exI[of _ B] inj_setminus dest: finite_imageD)
  2221 qed
  2222 
  2223 lemma compact_imp_fip:
  2224   assumes "compact S"
  2225     and "\<And>T. T \<in> F \<Longrightarrow> closed T"
  2226     and "\<And>F'. finite F' \<Longrightarrow> F' \<subseteq> F \<Longrightarrow> S \<inter> (\<Inter>F') \<noteq> {}"
  2227   shows "S \<inter> (\<Inter>F) \<noteq> {}"
  2228   using assms unfolding compact_fip by auto
  2229 
  2230 lemma compact_imp_fip_image:
  2231   assumes "compact s"
  2232     and P: "\<And>i. i \<in> I \<Longrightarrow> closed (f i)"
  2233     and Q: "\<And>I'. finite I' \<Longrightarrow> I' \<subseteq> I \<Longrightarrow> (s \<inter> (\<Inter>i\<in>I'. f i) \<noteq> {})"
  2234   shows "s \<inter> (\<Inter>i\<in>I. f i) \<noteq> {}"
  2235 proof -
  2236   note \<open>compact s\<close>
  2237   moreover from P have "\<forall>i \<in> f ` I. closed i"
  2238     by blast
  2239   moreover have "\<forall>A. finite A \<and> A \<subseteq> f ` I \<longrightarrow> (s \<inter> (\<Inter>A) \<noteq> {})"
  2240     apply rule
  2241     apply rule
  2242     apply (erule conjE)
  2243   proof -
  2244     fix A :: "'a set set"
  2245     assume "finite A" and "A \<subseteq> f ` I"
  2246     then obtain B where "B \<subseteq> I" and "finite B" and "A = f ` B"
  2247       using finite_subset_image [of A f I] by blast
  2248     with Q [of B] show "s \<inter> \<Inter>A \<noteq> {}"
  2249       by simp
  2250   qed
  2251   ultimately have "s \<inter> (\<Inter>(f ` I)) \<noteq> {}"
  2252     by (metis compact_imp_fip)
  2253   then show ?thesis by simp
  2254 qed
  2255 
  2256 end
  2257 
  2258 lemma (in t2_space) compact_imp_closed:
  2259   assumes "compact s"
  2260   shows "closed s"
  2261   unfolding closed_def
  2262 proof (rule openI)
  2263   fix y
  2264   assume "y \<in> - s"
  2265   let ?C = "\<Union>x\<in>s. {u. open u \<and> x \<in> u \<and> eventually (\<lambda>y. y \<notin> u) (nhds y)}"
  2266   have "s \<subseteq> \<Union>?C"
  2267   proof
  2268     fix x
  2269     assume "x \<in> s"
  2270     with \<open>y \<in> - s\<close> have "x \<noteq> y" by clarsimp
  2271     then have "\<exists>u v. open u \<and> open v \<and> x \<in> u \<and> y \<in> v \<and> u \<inter> v = {}"
  2272       by (rule hausdorff)
  2273     with \<open>x \<in> s\<close> show "x \<in> \<Union>?C"
  2274       unfolding eventually_nhds by auto
  2275   qed
  2276   then obtain D where "D \<subseteq> ?C" and "finite D" and "s \<subseteq> \<Union>D"
  2277     by (rule compactE [OF \<open>compact s\<close>]) auto
  2278   from \<open>D \<subseteq> ?C\<close> have "\<forall>x\<in>D. eventually (\<lambda>y. y \<notin> x) (nhds y)"
  2279     by auto
  2280   with \<open>finite D\<close> have "eventually (\<lambda>y. y \<notin> \<Union>D) (nhds y)"
  2281     by (simp add: eventually_ball_finite)
  2282   with \<open>s \<subseteq> \<Union>D\<close> have "eventually (\<lambda>y. y \<notin> s) (nhds y)"
  2283     by (auto elim!: eventually_mono)
  2284   then show "\<exists>t. open t \<and> y \<in> t \<and> t \<subseteq> - s"
  2285     by (simp add: eventually_nhds subset_eq)
  2286 qed
  2287 
  2288 lemma compact_continuous_image:
  2289   assumes f: "continuous_on s f"
  2290     and s: "compact s"
  2291   shows "compact (f ` s)"
  2292 proof (rule compactI)
  2293   fix C
  2294   assume "\<forall>c\<in>C. open c" and cover: "f`s \<subseteq> \<Union>C"
  2295   with f have "\<forall>c\<in>C. \<exists>A. open A \<and> A \<inter> s = f -` c \<inter> s"
  2296     unfolding continuous_on_open_invariant by blast
  2297   then obtain A where A: "\<forall>c\<in>C. open (A c) \<and> A c \<inter> s = f -` c \<inter> s"
  2298     unfolding bchoice_iff ..
  2299   with cover have "\<And>c. c \<in> C \<Longrightarrow> open (A c)" "s \<subseteq> (\<Union>c\<in>C. A c)"
  2300     by (fastforce simp add: subset_eq set_eq_iff)+
  2301   from compactE_image[OF s this] obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> (\<Union>c\<in>D. A c)" .
  2302   with A show "\<exists>D \<subseteq> C. finite D \<and> f`s \<subseteq> \<Union>D"
  2303     by (intro exI[of _ D]) (fastforce simp add: subset_eq set_eq_iff)+
  2304 qed
  2305 
  2306 lemma continuous_on_inv:
  2307   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  2308   assumes "continuous_on s f"
  2309     and "compact s"
  2310     and "\<forall>x\<in>s. g (f x) = x"
  2311   shows "continuous_on (f ` s) g"
  2312   unfolding continuous_on_topological
  2313 proof (clarsimp simp add: assms(3))
  2314   fix x :: 'a and B :: "'a set"
  2315   assume "x \<in> s" and "open B" and "x \<in> B"
  2316   have 1: "\<forall>x\<in>s. f x \<in> f ` (s - B) \<longleftrightarrow> x \<in> s - B"
  2317     using assms(3) by (auto, metis)
  2318   have "continuous_on (s - B) f"
  2319     using \<open>continuous_on s f\<close> Diff_subset
  2320     by (rule continuous_on_subset)
  2321   moreover have "compact (s - B)"
  2322     using \<open>open B\<close> and \<open>compact s\<close>
  2323     unfolding Diff_eq by (intro compact_Int_closed closed_Compl)
  2324   ultimately have "compact (f ` (s - B))"
  2325     by (rule compact_continuous_image)
  2326   then have "closed (f ` (s - B))"
  2327     by (rule compact_imp_closed)
  2328   then have "open (- f ` (s - B))"
  2329     by (rule open_Compl)
  2330   moreover have "f x \<in> - f ` (s - B)"
  2331     using \<open>x \<in> s\<close> and \<open>x \<in> B\<close> by (simp add: 1)
  2332   moreover have "\<forall>y\<in>s. f y \<in> - f ` (s - B) \<longrightarrow> y \<in> B"
  2333     by (simp add: 1)
  2334   ultimately show "\<exists>A. open A \<and> f x \<in> A \<and> (\<forall>y\<in>s. f y \<in> A \<longrightarrow> y \<in> B)"
  2335     by fast
  2336 qed
  2337 
  2338 lemma continuous_on_inv_into:
  2339   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  2340   assumes s: "continuous_on s f" "compact s"
  2341     and f: "inj_on f s"
  2342   shows "continuous_on (f ` s) (the_inv_into s f)"
  2343   by (rule continuous_on_inv[OF s]) (auto simp: the_inv_into_f_f[OF f])
  2344 
  2345 lemma (in linorder_topology) compact_attains_sup:
  2346   assumes "compact S" "S \<noteq> {}"
  2347   shows "\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s"
  2348 proof (rule classical)
  2349   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s)"
  2350   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. s < t s"
  2351     by (metis not_le)
  2352   then have "\<And>s. s\<in>S \<Longrightarrow> open {..< t s}" "S \<subseteq> (\<Union>s\<in>S. {..< t s})"
  2353     by auto
  2354   with \<open>compact S\<close> obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {..< t s})"
  2355     by (metis compactE_image)
  2356   with \<open>S \<noteq> {}\<close> have Max: "Max (t`C) \<in> t`C" and "\<forall>s\<in>t`C. s \<le> Max (t`C)"
  2357     by (auto intro!: Max_in)
  2358   with C have "S \<subseteq> {..< Max (t`C)}"
  2359     by (auto intro: less_le_trans simp: subset_eq)
  2360   with t Max \<open>C \<subseteq> S\<close> show ?thesis
  2361     by fastforce
  2362 qed
  2363 
  2364 lemma (in linorder_topology) compact_attains_inf:
  2365   assumes "compact S" "S \<noteq> {}"
  2366   shows "\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t"
  2367 proof (rule classical)
  2368   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t)"
  2369   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. t s < s"
  2370     by (metis not_le)
  2371   then have "\<And>s. s\<in>S \<Longrightarrow> open {t s <..}" "S \<subseteq> (\<Union>s\<in>S. {t s <..})"
  2372     by auto
  2373   with \<open>compact S\<close> obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {t s <..})"
  2374     by (metis compactE_image)
  2375   with \<open>S \<noteq> {}\<close> have Min: "Min (t`C) \<in> t`C" and "\<forall>s\<in>t`C. Min (t`C) \<le> s"
  2376     by (auto intro!: Min_in)
  2377   with C have "S \<subseteq> {Min (t`C) <..}"
  2378     by (auto intro: le_less_trans simp: subset_eq)
  2379   with t Min \<open>C \<subseteq> S\<close> show ?thesis
  2380     by fastforce
  2381 qed
  2382 
  2383 lemma continuous_attains_sup:
  2384   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
  2385   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)"
  2386   using compact_attains_sup[of "f ` s"] compact_continuous_image[of s f] by auto
  2387 
  2388 lemma continuous_attains_inf:
  2389   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
  2390   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)"
  2391   using compact_attains_inf[of "f ` s"] compact_continuous_image[of s f] by auto
  2392 
  2393 
  2394 subsection \<open>Connectedness\<close>
  2395 
  2396 context topological_space
  2397 begin
  2398 
  2399 definition "connected S \<longleftrightarrow>
  2400   \<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> {})"
  2401 
  2402 lemma connectedI:
  2403   "(\<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)
  2404   \<Longrightarrow> connected U"
  2405   by (auto simp: connected_def)
  2406 
  2407 lemma connected_empty [simp]: "connected {}"
  2408   by (auto intro!: connectedI)
  2409 
  2410 lemma connected_sing [simp]: "connected {x}"
  2411   by (auto intro!: connectedI)
  2412 
  2413 lemma connectedD:
  2414   "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 = {}"
  2415   by (auto simp: connected_def)
  2416 
  2417 end
  2418 
  2419 lemma connected_closed:
  2420   "connected s \<longleftrightarrow>
  2421     \<not> (\<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> {})"
  2422   apply (simp add: connected_def del: ex_simps, safe)
  2423    apply (drule_tac x="-A" in spec)
  2424    apply (drule_tac x="-B" in spec)
  2425    apply (fastforce simp add: closed_def [symmetric])
  2426   apply (drule_tac x="-A" in spec)
  2427   apply (drule_tac x="-B" in spec)
  2428   apply (fastforce simp add: open_closed [symmetric])
  2429   done
  2430 
  2431 lemma connected_closedD:
  2432   "\<lbrakk>connected s; A \<inter> B \<inter> s = {}; s \<subseteq> A \<union> B; closed A; closed B\<rbrakk> \<Longrightarrow> A \<inter> s = {} \<or> B \<inter> s = {}"
  2433   by (simp add: connected_closed)
  2434 
  2435 lemma connected_Union:
  2436   assumes cs: "\<And>s. s \<in> S \<Longrightarrow> connected s"
  2437     and ne: "\<Inter>S \<noteq> {}"
  2438   shows "connected(\<Union>S)"
  2439 proof (rule connectedI)
  2440   fix A B
  2441   assume A: "open A" and B: "open B" and Alap: "A \<inter> \<Union>S \<noteq> {}" and Blap: "B \<inter> \<Union>S \<noteq> {}"
  2442     and disj: "A \<inter> B \<inter> \<Union>S = {}" and cover: "\<Union>S \<subseteq> A \<union> B"
  2443   have disjs:"\<And>s. s \<in> S \<Longrightarrow> A \<inter> B \<inter> s = {}"
  2444     using disj by auto
  2445   obtain sa where sa: "sa \<in> S" "A \<inter> sa \<noteq> {}"
  2446     using Alap by auto
  2447   obtain sb where sb: "sb \<in> S" "B \<inter> sb \<noteq> {}"
  2448     using Blap by auto
  2449   obtain x where x: "\<And>s. s \<in> S \<Longrightarrow> x \<in> s"
  2450     using ne by auto
  2451   then have "x \<in> \<Union>S"
  2452     using \<open>sa \<in> S\<close> by blast
  2453   then have "x \<in> A \<or> x \<in> B"
  2454     using cover by auto
  2455   then show False
  2456     using cs [unfolded connected_def]
  2457     by (metis A B IntI Sup_upper sa sb disjs x cover empty_iff subset_trans)
  2458 qed
  2459 
  2460 lemma connected_Un: "connected s \<Longrightarrow> connected t \<Longrightarrow> s \<inter> t \<noteq> {} \<Longrightarrow> connected (s \<union> t)"
  2461   using connected_Union [of "{s,t}"] by auto
  2462 
  2463 lemma connected_diff_open_from_closed:
  2464   assumes st: "s \<subseteq> t"
  2465     and tu: "t \<subseteq> u"
  2466     and s: "open s"
  2467     and t: "closed t"
  2468     and u: "connected u"
  2469     and ts: "connected (t - s)"
  2470   shows "connected(u - s)"
  2471 proof (rule connectedI)
  2472   fix A B
  2473   assume AB: "open A" "open B" "A \<inter> (u - s) \<noteq> {}" "B \<inter> (u - s) \<noteq> {}"
  2474     and disj: "A \<inter> B \<inter> (u - s) = {}"
  2475     and cover: "u - s \<subseteq> A \<union> B"
  2476   then consider "A \<inter> (t - s) = {}" | "B \<inter> (t - s) = {}"
  2477     using st ts tu connectedD [of "t-s" "A" "B"] by auto
  2478   then show False
  2479   proof cases
  2480     case 1
  2481     then have "(A - t) \<inter> (B \<union> s) \<inter> u = {}"
  2482       using disj st by auto
  2483     moreover have "u \<subseteq> (A - t) \<union> (B \<union> s)"
  2484       using 1 cover by auto
  2485     ultimately show False
  2486       using connectedD [of u "A - t" "B \<union> s"] AB s t 1 u by auto
  2487   next
  2488     case 2
  2489     then have "(A \<union> s) \<inter> (B - t) \<inter> u = {}"
  2490       using disj st by auto
  2491     moreover have "u \<subseteq> (A \<union> s) \<union> (B - t)"
  2492       using 2 cover by auto
  2493     ultimately show False
  2494       using connectedD [of u "A \<union> s" "B - t"] AB s t 2 u by auto
  2495   qed
  2496 qed
  2497 
  2498 lemma connected_iff_const:
  2499   fixes S :: "'a::topological_space set"
  2500   shows "connected S \<longleftrightarrow> (\<forall>P::'a \<Rightarrow> bool. continuous_on S P \<longrightarrow> (\<exists>c. \<forall>s\<in>S. P s = c))"
  2501 proof safe
  2502   fix P :: "'a \<Rightarrow> bool"
  2503   assume "connected S" "continuous_on S P"
  2504   then have "\<And>b. \<exists>A. open A \<and> A \<inter> S = P -` {b} \<inter> S"
  2505     unfolding continuous_on_open_invariant by (simp add: open_discrete)
  2506   from this[of True] this[of False]
  2507   obtain t f where "open t" "open f" and *: "f \<inter> S = P -` {False} \<inter> S" "t \<inter> S = P -` {True} \<inter> S"
  2508     by meson
  2509   then have "t \<inter> S = {} \<or> f \<inter> S = {}"
  2510     by (intro connectedD[OF \<open>connected S\<close>])  auto
  2511   then show "\<exists>c. \<forall>s\<in>S. P s = c"
  2512   proof (rule disjE)
  2513     assume "t \<inter> S = {}"
  2514     then show ?thesis
  2515       unfolding * by (intro exI[of _ False]) auto
  2516   next
  2517     assume "f \<inter> S = {}"
  2518     then show ?thesis
  2519       unfolding * by (intro exI[of _ True]) auto
  2520   qed
  2521 next
  2522   assume P: "\<forall>P::'a \<Rightarrow> bool. continuous_on S P \<longrightarrow> (\<exists>c. \<forall>s\<in>S. P s = c)"
  2523   show "connected S"
  2524   proof (rule connectedI)
  2525     fix A B
  2526     assume *: "open A" "open B" "A \<inter> S \<noteq> {}" "B \<inter> S \<noteq> {}" "A \<inter> B \<inter> S = {}" "S \<subseteq> A \<union> B"
  2527     have "continuous_on S (\<lambda>x. x \<in> A)"
  2528       unfolding continuous_on_open_invariant
  2529     proof safe
  2530       fix C :: "bool set"
  2531       have "C = UNIV \<or> C = {True} \<or> C = {False} \<or> C = {}"
  2532         using subset_UNIV[of C] unfolding UNIV_bool by auto
  2533       with * show "\<exists>T. open T \<and> T \<inter> S = (\<lambda>x. x \<in> A) -` C \<inter> S"
  2534         by (intro exI[of _ "(if True \<in> C then A else {}) \<union> (if False \<in> C then B else {})"]) auto
  2535     qed
  2536     from P[rule_format, OF this] obtain c where "\<And>s. s \<in> S \<Longrightarrow> (s \<in> A) = c"
  2537       by blast
  2538     with * show False
  2539       by (cases c) auto
  2540   qed
  2541 qed
  2542 
  2543 lemma connectedD_const: "connected S \<Longrightarrow> continuous_on S P \<Longrightarrow> \<exists>c. \<forall>s\<in>S. P s = c"
  2544   for P :: "'a::topological_space \<Rightarrow> bool"
  2545   by (auto simp: connected_iff_const)
  2546 
  2547 lemma connectedI_const:
  2548   "(\<And>P::'a::topological_space \<Rightarrow> bool. continuous_on S P \<Longrightarrow> \<exists>c. \<forall>s\<in>S. P s = c) \<Longrightarrow> connected S"
  2549   by (auto simp: connected_iff_const)
  2550 
  2551 lemma connected_local_const:
  2552   assumes "connected A" "a \<in> A" "b \<in> A"
  2553     and *: "\<forall>a\<in>A. eventually (\<lambda>b. f a = f b) (at a within A)"
  2554   shows "f a = f b"
  2555 proof -
  2556   obtain S where S: "\<And>a. a \<in> A \<Longrightarrow> a \<in> S a" "\<And>a. a \<in> A \<Longrightarrow> open (S a)"
  2557     "\<And>a x. a \<in> A \<Longrightarrow> x \<in> S a \<Longrightarrow> x \<in> A \<Longrightarrow> f a = f x"
  2558     using * unfolding eventually_at_topological by metis
  2559   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"
  2560   have "?P \<inter> A = {} \<or> ?N \<inter> A = {}"
  2561     using \<open>connected A\<close> S \<open>a\<in>A\<close>
  2562     by (intro connectedD) (auto, metis)
  2563   then show "f a = f b"
  2564   proof
  2565     assume "?N \<inter> A = {}"
  2566     then have "\<forall>x\<in>A. f a = f x"
  2567       using S(1) by auto
  2568     with \<open>b\<in>A\<close> show ?thesis by auto
  2569   next
  2570     assume "?P \<inter> A = {}" then show ?thesis
  2571       using \<open>a \<in> A\<close> S(1)[of a] by auto
  2572   qed
  2573 qed
  2574 
  2575 lemma (in linorder_topology) connectedD_interval:
  2576   assumes "connected U"
  2577     and xy: "x \<in> U" "y \<in> U"
  2578     and "x \<le> z" "z \<le> y"
  2579   shows "z \<in> U"
  2580 proof -
  2581   have eq: "{..<z} \<union> {z<..} = - {z}"
  2582     by auto
  2583   have "\<not> connected U" if "z \<notin> U" "x < z" "z < y"
  2584     using xy that
  2585     apply (simp only: connected_def simp_thms)
  2586     apply (rule_tac exI[of _ "{..< z}"])
  2587     apply (rule_tac exI[of _ "{z <..}"])
  2588     apply (auto simp add: eq)
  2589     done
  2590   with assms show "z \<in> U"
  2591     by (metis less_le)
  2592 qed
  2593 
  2594 lemma connected_continuous_image:
  2595   assumes *: "continuous_on s f"
  2596     and "connected s"
  2597   shows "connected (f ` s)"
  2598 proof (rule connectedI_const)
  2599   fix P :: "'b \<Rightarrow> bool"
  2600   assume "continuous_on (f ` s) P"
  2601   then have "continuous_on s (P \<circ> f)"
  2602     by (rule continuous_on_compose[OF *])
  2603   from connectedD_const[OF \<open>connected s\<close> this] show "\<exists>c. \<forall>s\<in>f ` s. P s = c"
  2604     by auto
  2605 qed
  2606 
  2607 
  2608 section \<open>Linear Continuum Topologies\<close>
  2609 
  2610 class linear_continuum_topology = linorder_topology + linear_continuum
  2611 begin
  2612 
  2613 lemma Inf_notin_open:
  2614   assumes A: "open A"
  2615     and bnd: "\<forall>a\<in>A. x < a"
  2616   shows "Inf A \<notin> A"
  2617 proof
  2618   assume "Inf A \<in> A"
  2619   then obtain b where "b < Inf A" "{b <.. Inf A} \<subseteq> A"
  2620     using open_left[of A "Inf A" x] assms by auto
  2621   with dense[of b "Inf A"] obtain c where "c < Inf A" "c \<in> A"
  2622     by (auto simp: subset_eq)
  2623   then show False
  2624     using cInf_lower[OF \<open>c \<in> A\<close>] bnd
  2625     by (metis not_le less_imp_le bdd_belowI)
  2626 qed
  2627 
  2628 lemma Sup_notin_open:
  2629   assumes A: "open A"
  2630     and bnd: "\<forall>a\<in>A. a < x"
  2631   shows "Sup A \<notin> A"
  2632 proof
  2633   assume "Sup A \<in> A"
  2634   with assms obtain b where "Sup A < b" "{Sup A ..< b} \<subseteq> A"
  2635     using open_right[of A "Sup A" x] by auto
  2636   with dense[of "Sup A" b] obtain c where "Sup A < c" "c \<in> A"
  2637     by (auto simp: subset_eq)
  2638   then show False
  2639     using cSup_upper[OF \<open>c \<in> A\<close>] bnd
  2640     by (metis less_imp_le not_le bdd_aboveI)
  2641 qed
  2642 
  2643 end
  2644 
  2645 instance linear_continuum_topology \<subseteq> perfect_space
  2646 proof
  2647   fix x :: 'a
  2648   obtain y where "x < y \<or> y < x"
  2649     using ex_gt_or_lt [of x] ..
  2650   with Inf_notin_open[of "{x}" y] Sup_notin_open[of "{x}" y] show "\<not> open {x}"
  2651     by auto
  2652 qed
  2653 
  2654 lemma connectedI_interval:
  2655   fixes U :: "'a :: linear_continuum_topology set"
  2656   assumes *: "\<And>x y z. x \<in> U \<Longrightarrow> y \<in> U \<Longrightarrow> x \<le> z \<Longrightarrow> z \<le> y \<Longrightarrow> z \<in> U"
  2657   shows "connected U"
  2658 proof (rule connectedI)
  2659   {
  2660     fix A B
  2661     assume "open A" "open B" "A \<inter> B \<inter> U = {}" "U \<subseteq> A \<union> B"
  2662     fix x y
  2663     assume "x < y" "x \<in> A" "y \<in> B" "x \<in> U" "y \<in> U"
  2664 
  2665     let ?z = "Inf (B \<inter> {x <..})"
  2666 
  2667     have "x \<le> ?z" "?z \<le> y"
  2668       using \<open>y \<in> B\<close> \<open>x < y\<close> by (auto intro: cInf_lower cInf_greatest)
  2669     with \<open>x \<in> U\<close> \<open>y \<in> U\<close> have "?z \<in> U"
  2670       by (rule *)
  2671     moreover have "?z \<notin> B \<inter> {x <..}"
  2672       using \<open>open B\<close> by (intro Inf_notin_open) auto
  2673     ultimately have "?z \<in> A"
  2674       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
  2675     have "\<exists>b\<in>B. b \<in> A \<and> b \<in> U" if "?z < y"
  2676     proof -
  2677       obtain a where "?z < a" "{?z ..< a} \<subseteq> A"
  2678         using open_right[OF \<open>open A\<close> \<open>?z \<in> A\<close> \<open>?z < y\<close>] by auto
  2679       moreover obtain b where "b \<in> B" "x < b" "b < min a y"
  2680         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>
  2681         by auto
  2682       moreover have "?z \<le> b"
  2683         using \<open>b \<in> B\<close> \<open>x < b\<close>
  2684         by (intro cInf_lower) auto
  2685       moreover have "b \<in> U"
  2686         using \<open>x \<le> ?z\<close> \<open>?z \<le> b\<close> \<open>b < min a y\<close>
  2687         by (intro *[OF \<open>x \<in> U\<close> \<open>y \<in> U\<close>]) (auto simp: less_imp_le)
  2688       ultimately show ?thesis
  2689         by (intro bexI[of _ b]) auto
  2690     qed
  2691     then have False
  2692       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>
  2693       unfolding le_less by blast
  2694   }
  2695   note not_disjoint = this
  2696 
  2697   fix A B assume AB: "open A" "open B" "U \<subseteq> A \<union> B" "A \<inter> B \<inter> U = {}"
  2698   moreover assume "A \<inter> U \<noteq> {}" then obtain x where x: "x \<in> U" "x \<in> A" by auto
  2699   moreover assume "B \<inter> U \<noteq> {}" then obtain y where y: "y \<in> U" "y \<in> B" by auto
  2700   moreover note not_disjoint[of B A y x] not_disjoint[of A B x y]
  2701   ultimately show False
  2702     by (cases x y rule: linorder_cases) auto
  2703 qed
  2704 
  2705 lemma connected_iff_interval: "connected U \<longleftrightarrow> (\<forall>x\<in>U. \<forall>y\<in>U. \<forall>z. x \<le> z \<longrightarrow> z \<le> y \<longrightarrow> z \<in> U)"
  2706   for U :: "'a::linear_continuum_topology set"
  2707   by (auto intro: connectedI_interval dest: connectedD_interval)
  2708 
  2709 lemma connected_UNIV[simp]: "connected (UNIV::'a::linear_continuum_topology set)"
  2710   by (simp add: connected_iff_interval)
  2711 
  2712 lemma connected_Ioi[simp]: "connected {a<..}"
  2713   for a :: "'a::linear_continuum_topology"
  2714   by (auto simp: connected_iff_interval)
  2715 
  2716 lemma connected_Ici[simp]: "connected {a..}"
  2717   for a :: "'a::linear_continuum_topology"
  2718   by (auto simp: connected_iff_interval)
  2719 
  2720 lemma connected_Iio[simp]: "connected {..<a}"
  2721   for a :: "'a::linear_continuum_topology"
  2722   by (auto simp: connected_iff_interval)
  2723 
  2724 lemma connected_Iic[simp]: "connected {..a}"
  2725   for a :: "'a::linear_continuum_topology"
  2726   by (auto simp: connected_iff_interval)
  2727 
  2728 lemma connected_Ioo[simp]: "connected {a<..<b}"
  2729   for a b :: "'a::linear_continuum_topology"
  2730   unfolding connected_iff_interval by auto
  2731 
  2732 lemma connected_Ioc[simp]: "connected {a<..b}"
  2733   for a b :: "'a::linear_continuum_topology"
  2734   by (auto simp: connected_iff_interval)
  2735 
  2736 lemma connected_Ico[simp]: "connected {a..<b}"
  2737   for a b :: "'a::linear_continuum_topology"
  2738   by (auto simp: connected_iff_interval)
  2739 
  2740 lemma connected_Icc[simp]: "connected {a..b}"
  2741   for a b :: "'a::linear_continuum_topology"
  2742   by (auto simp: connected_iff_interval)
  2743 
  2744 lemma connected_contains_Ioo:
  2745   fixes A :: "'a :: linorder_topology set"
  2746   assumes "connected A" "a \<in> A" "b \<in> A" shows "{a <..< b} \<subseteq> A"
  2747   using connectedD_interval[OF assms] by (simp add: subset_eq Ball_def less_imp_le)
  2748 
  2749 lemma connected_contains_Icc:
  2750   fixes A :: "'a::linorder_topology set"
  2751   assumes "connected A" "a \<in> A" "b \<in> A"
  2752   shows "{a..b} \<subseteq> A"
  2753 proof
  2754   fix x assume "x \<in> {a..b}"
  2755   then have "x = a \<or> x = b \<or> x \<in> {a<..<b}"
  2756     by auto
  2757   then show "x \<in> A"
  2758     using assms connected_contains_Ioo[of A a b] by auto
  2759 qed
  2760 
  2761 
  2762 subsection \<open>Intermediate Value Theorem\<close>
  2763 
  2764 lemma IVT':
  2765   fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b::linorder_topology"
  2766   assumes y: "f a \<le> y" "y \<le> f b" "a \<le> b"
  2767     and *: "continuous_on {a .. b} f"
  2768   shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2769 proof -
  2770   have "connected {a..b}"
  2771     unfolding connected_iff_interval by auto
  2772   from connected_continuous_image[OF * this, THEN connectedD_interval, of "f a" "f b" y] y
  2773   show ?thesis
  2774     by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
  2775 qed
  2776 
  2777 lemma IVT2':
  2778   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2779   assumes y: "f b \<le> y" "y \<le> f a" "a \<le> b"
  2780     and *: "continuous_on {a .. b} f"
  2781   shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2782 proof -
  2783   have "connected {a..b}"
  2784     unfolding connected_iff_interval by auto
  2785   from connected_continuous_image[OF * this, THEN connectedD_interval, of "f b" "f a" y] y
  2786   show ?thesis
  2787     by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
  2788 qed
  2789 
  2790 lemma IVT:
  2791   fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b::linorder_topology"
  2792   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>
  2793     \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2794   by (rule IVT') (auto intro: continuous_at_imp_continuous_on)
  2795 
  2796 lemma IVT2:
  2797   fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b::linorder_topology"
  2798   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>
  2799     \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2800   by (rule IVT2') (auto intro: continuous_at_imp_continuous_on)
  2801 
  2802 lemma continuous_inj_imp_mono:
  2803   fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b::linorder_topology"
  2804   assumes x: "a < x" "x < b"
  2805     and cont: "continuous_on {a..b} f"
  2806     and inj: "inj_on f {a..b}"
  2807   shows "(f a < f x \<and> f x < f b) \<or> (f b < f x \<and> f x < f a)"
  2808 proof -
  2809   note I = inj_on_eq_iff[OF inj]
  2810   {
  2811     assume "f x < f a" "f x < f b"
  2812     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"
  2813       using IVT'[of f x "min (f a) (f b)" b] IVT2'[of f x "min (f a) (f b)" a] x
  2814       by (auto simp: continuous_on_subset[OF cont] less_imp_le)
  2815     with x I have False by auto
  2816   }
  2817   moreover
  2818   {
  2819     assume "f a < f x" "f b < f x"
  2820     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"
  2821       using IVT'[of f a "max (f a) (f b)" x] IVT2'[of f b "max (f a) (f b)" x] x
  2822       by (auto simp: continuous_on_subset[OF cont] less_imp_le)
  2823     with x I have False by auto
  2824   }
  2825   ultimately show ?thesis
  2826     using I[of a x] I[of x b] x less_trans[OF x]
  2827     by (auto simp add: le_less less_imp_neq neq_iff)
  2828 qed
  2829 
  2830 lemma continuous_at_Sup_mono:
  2831   fixes f :: "'a::{linorder_topology,conditionally_complete_linorder} \<Rightarrow>
  2832     'b::{linorder_topology,conditionally_complete_linorder}"
  2833   assumes "mono f"
  2834     and cont: "continuous (at_left (Sup S)) f"
  2835     and S: "S \<noteq> {}" "bdd_above S"
  2836   shows "f (Sup S) = (SUP s:S. f s)"
  2837 proof (rule antisym)
  2838   have f: "(f \<longlongrightarrow> f (Sup S)) (at_left (Sup S))"
  2839     using cont unfolding continuous_within .
  2840   show "f (Sup S) \<le> (SUP s:S. f s)"
  2841   proof cases
  2842     assume "Sup S \<in> S"
  2843     then show ?thesis
  2844       by (rule cSUP_upper) (auto intro: bdd_above_image_mono S \<open>mono f\<close>)
  2845   next
  2846     assume "Sup S \<notin> S"
  2847     from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
  2848       by auto
  2849     with \<open>Sup S \<notin> S\<close> S have "s < Sup S"
  2850       unfolding less_le by (blast intro: cSup_upper)
  2851     show ?thesis
  2852     proof (rule ccontr)
  2853       assume "\<not> ?thesis"
  2854       with order_tendstoD(1)[OF f, of "SUP s:S. f s"] obtain b where "b < Sup S"
  2855         and *: "\<And>y. b < y \<Longrightarrow> y < Sup S \<Longrightarrow> (SUP s:S. f s) < f y"
  2856         by (auto simp: not_le eventually_at_left[OF \<open>s < Sup S\<close>])
  2857       with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "b < c"
  2858         using less_cSupD[of S b] by auto
  2859       with \<open>Sup S \<notin> S\<close> S have "c < Sup S"
  2860         unfolding less_le by (blast intro: cSup_upper)
  2861       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]]
  2862       show False
  2863         by (auto simp: assms)
  2864     qed
  2865   qed
  2866 qed (intro cSUP_least \<open>mono f\<close>[THEN monoD] cSup_upper S)
  2867 
  2868 lemma continuous_at_Sup_antimono:
  2869   fixes f :: "'a::{linorder_topology,conditionally_complete_linorder} \<Rightarrow>
  2870     'b::{linorder_topology,conditionally_complete_linorder}"
  2871   assumes "antimono f"
  2872     and cont: "continuous (at_left (Sup S)) f"
  2873     and S: "S \<noteq> {}" "bdd_above S"
  2874   shows "f (Sup S) = (INF s:S. f s)"
  2875 proof (rule antisym)
  2876   have f: "(f \<longlongrightarrow> f (Sup S)) (at_left (Sup S))"
  2877     using cont unfolding continuous_within .
  2878   show "(INF s:S. f s) \<le> f (Sup S)"
  2879   proof cases
  2880     assume "Sup S \<in> S"
  2881     then show ?thesis
  2882       by (intro cINF_lower) (auto intro: bdd_below_image_antimono S \<open>antimono f\<close>)
  2883   next
  2884     assume "Sup S \<notin> S"
  2885     from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
  2886       by auto
  2887     with \<open>Sup S \<notin> S\<close> S have "s < Sup S"
  2888       unfolding less_le by (blast intro: cSup_upper)
  2889     show ?thesis
  2890     proof (rule ccontr)
  2891       assume "\<not> ?thesis"
  2892       with order_tendstoD(2)[OF f, of "INF s:S. f s"] obtain b where "b < Sup S"
  2893         and *: "\<And>y. b < y \<Longrightarrow> y < Sup S \<Longrightarrow> f y < (INF s:S. f s)"
  2894         by (auto simp: not_le eventually_at_left[OF \<open>s < Sup S\<close>])
  2895       with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "b < c"
  2896         using less_cSupD[of S b] by auto
  2897       with \<open>Sup S \<notin> S\<close> S have "c < Sup S"
  2898         unfolding less_le by (blast intro: cSup_upper)
  2899       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>
  2900       show False
  2901         by (auto simp: assms)
  2902     qed
  2903   qed
  2904 qed (intro cINF_greatest \<open>antimono f\<close>[THEN antimonoD] cSup_upper S)
  2905 
  2906 lemma continuous_at_Inf_mono:
  2907   fixes f :: "'a::{linorder_topology,conditionally_complete_linorder} \<Rightarrow>
  2908     'b::{linorder_topology,conditionally_complete_linorder}"
  2909   assumes "mono f"
  2910     and cont: "continuous (at_right (Inf S)) f"
  2911     and S: "S \<noteq> {}" "bdd_below S"
  2912   shows "f (Inf S) = (INF s:S. f s)"
  2913 proof (rule antisym)
  2914   have f: "(f \<longlongrightarrow> f (Inf S)) (at_right (Inf S))"
  2915     using cont unfolding continuous_within .
  2916   show "(INF s:S. f s) \<le> f (Inf S)"
  2917   proof cases
  2918     assume "Inf S \<in> S"
  2919     then show ?thesis
  2920       by (rule cINF_lower[rotated]) (auto intro: bdd_below_image_mono S \<open>mono f\<close>)
  2921   next
  2922     assume "Inf S \<notin> S"
  2923     from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
  2924       by auto
  2925     with \<open>Inf S \<notin> S\<close> S have "Inf S < s"
  2926       unfolding less_le by (blast intro: cInf_lower)
  2927     show ?thesis
  2928     proof (rule ccontr)
  2929       assume "\<not> ?thesis"
  2930       with order_tendstoD(2)[OF f, of "INF s:S. f s"] obtain b where "Inf S < b"
  2931         and *: "\<And>y. Inf S < y \<Longrightarrow> y < b \<Longrightarrow> f y < (INF s:S. f s)"
  2932         by (auto simp: not_le eventually_at_right[OF \<open>Inf S < s\<close>])
  2933       with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "c < b"
  2934         using cInf_lessD[of S b] by auto
  2935       with \<open>Inf S \<notin> S\<close> S have "Inf S < c"
  2936         unfolding less_le by (blast intro: cInf_lower)
  2937       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>]
  2938       show False
  2939         by (auto simp: assms)
  2940     qed
  2941   qed
  2942 qed (intro cINF_greatest \<open>mono f\<close>[THEN monoD] cInf_lower \<open>bdd_below S\<close> \<open>S \<noteq> {}\<close>)
  2943 
  2944 lemma continuous_at_Inf_antimono:
  2945   fixes f :: "'a::{linorder_topology,conditionally_complete_linorder} \<Rightarrow>
  2946     'b::{linorder_topology,conditionally_complete_linorder}"
  2947   assumes "antimono f"
  2948     and cont: "continuous (at_right (Inf S)) f"
  2949     and S: "S \<noteq> {}" "bdd_below S"
  2950   shows "f (Inf S) = (SUP s:S. f s)"
  2951 proof (rule antisym)
  2952   have f: "(f \<longlongrightarrow> f (Inf S)) (at_right (Inf S))"
  2953     using cont unfolding continuous_within .
  2954   show "f (Inf S) \<le> (SUP s:S. f s)"
  2955   proof cases
  2956     assume "Inf S \<in> S"
  2957     then show ?thesis
  2958       by (rule cSUP_upper) (auto intro: bdd_above_image_antimono S \<open>antimono f\<close>)
  2959   next
  2960     assume "Inf S \<notin> S"
  2961     from \<open>S \<noteq> {}\<close> obtain s where "s \<in> S"
  2962       by auto
  2963     with \<open>Inf S \<notin> S\<close> S have "Inf S < s"
  2964       unfolding less_le by (blast intro: cInf_lower)
  2965     show ?thesis
  2966     proof (rule ccontr)
  2967       assume "\<not> ?thesis"
  2968       with order_tendstoD(1)[OF f, of "SUP s:S. f s"] obtain b where "Inf S < b"
  2969         and *: "\<And>y. Inf S < y \<Longrightarrow> y < b \<Longrightarrow> (SUP s:S. f s) < f y"
  2970         by (auto simp: not_le eventually_at_right[OF \<open>Inf S < s\<close>])
  2971       with \<open>S \<noteq> {}\<close> obtain c where "c \<in> S" "c < b"
  2972         using cInf_lessD[of S b] by auto
  2973       with \<open>Inf S \<notin> S\<close> S have "Inf S < c"
  2974         unfolding less_le by (blast intro: cInf_lower)
  2975       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]]
  2976       show False
  2977         by (auto simp: assms)
  2978     qed
  2979   qed
  2980 qed (intro cSUP_least \<open>antimono f\<close>[THEN antimonoD] cInf_lower S)
  2981 
  2982 
  2983 subsection \<open>Uniform spaces\<close>
  2984 
  2985 class uniformity =
  2986   fixes uniformity :: "('a \<times> 'a) filter"
  2987 begin
  2988 
  2989 abbreviation uniformity_on :: "'a set \<Rightarrow> ('a \<times> 'a) filter"
  2990   where "uniformity_on s \<equiv> inf uniformity (principal (s\<times>s))"
  2991 
  2992 end
  2993 
  2994 lemma uniformity_Abort:
  2995   "uniformity =
  2996     Filter.abstract_filter (\<lambda>u. Code.abort (STR ''uniformity is not executable'') (\<lambda>u. uniformity))"
  2997   by simp
  2998 
  2999 class open_uniformity = "open" + uniformity +
  3000   assumes open_uniformity:
  3001     "\<And>U. open U \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"
  3002 
  3003 class uniform_space = open_uniformity +
  3004   assumes uniformity_refl: "eventually E uniformity \<Longrightarrow> E (x, x)"
  3005     and uniformity_sym: "eventually E uniformity \<Longrightarrow> eventually (\<lambda>(x, y). E (y, x)) uniformity"
  3006     and uniformity_trans:
  3007       "eventually E uniformity \<Longrightarrow>
  3008         \<exists>D. eventually D uniformity \<and> (\<forall>x y z. D (x, y) \<longrightarrow> D (y, z) \<longrightarrow> E (x, z))"
  3009 begin
  3010 
  3011 subclass topological_space
  3012   by standard (force elim: eventually_mono eventually_elim2 simp: split_beta' open_uniformity)+
  3013 
  3014 lemma uniformity_bot: "uniformity \<noteq> bot"
  3015   using uniformity_refl by auto
  3016 
  3017 lemma uniformity_trans':
  3018   "eventually E uniformity \<Longrightarrow>
  3019     eventually (\<lambda>((x, y), (y', z)). y = y' \<longrightarrow> E (x, z)) (uniformity \<times>\<^sub>F uniformity)"
  3020   by (drule uniformity_trans) (auto simp add: eventually_prod_same)
  3021 
  3022 lemma uniformity_transE:
  3023   assumes "eventually E uniformity"
  3024   obtains D where "eventually D uniformity" "\<And>x y z. D (x, y) \<Longrightarrow> D (y, z) \<Longrightarrow> E (x, z)"
  3025   using uniformity_trans [OF assms] by auto
  3026 
  3027 lemma eventually_nhds_uniformity:
  3028   "eventually P (nhds x) \<longleftrightarrow> eventually (\<lambda>(x', y). x' = x \<longrightarrow> P y) uniformity"
  3029   (is "_ \<longleftrightarrow> ?N P x")
  3030   unfolding eventually_nhds
  3031 proof safe
  3032   assume *: "?N P x"
  3033   have "?N (?N P) x" if "?N P x" for x
  3034   proof -
  3035     from that obtain D where ev: "eventually D uniformity"
  3036       and D: "D (a, b) \<Longrightarrow> D (b, c) \<Longrightarrow> case (a, c) of (x', y) \<Rightarrow> x' = x \<longrightarrow> P y" for a b c
  3037       by (rule uniformity_transE) simp
  3038     from ev show ?thesis
  3039       by eventually_elim (insert ev D, force elim: eventually_mono split: prod.split)
  3040   qed
  3041   then have "open {x. ?N P x}"
  3042     by (simp add: open_uniformity)
  3043   then show "\<exists>S. open S \<and> x \<in> S \<and> (\<forall>x\<in>S. P x)"
  3044     by (intro exI[of _ "{x. ?N P x}"]) (auto dest: uniformity_refl simp: *)
  3045 qed (force simp add: open_uniformity elim: eventually_mono)
  3046 
  3047 
  3048 subsubsection \<open>Totally bounded sets\<close>
  3049 
  3050 definition totally_bounded :: "'a set \<Rightarrow> bool"
  3051   where "totally_bounded S \<longleftrightarrow>
  3052     (\<forall>E. eventually E uniformity \<longrightarrow> (\<exists>X. finite X \<and> (\<forall>s\<in>S. \<exists>x\<in>X. E (x, s))))"
  3053 
  3054 lemma totally_bounded_empty[iff]: "totally_bounded {}"
  3055   by (auto simp add: totally_bounded_def)
  3056 
  3057 lemma totally_bounded_subset: "totally_bounded S \<Longrightarrow> T \<subseteq> S \<Longrightarrow> totally_bounded T"
  3058   by (fastforce simp add: totally_bounded_def)
  3059 
  3060 lemma totally_bounded_Union[intro]:
  3061   assumes M: "finite M" "\<And>S. S \<in> M \<Longrightarrow> totally_bounded S"
  3062   shows "totally_bounded (\<Union>M)"
  3063   unfolding totally_bounded_def
  3064 proof safe
  3065   fix E
  3066   assume "eventually E uniformity"
  3067   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))"
  3068     by (metis totally_bounded_def)
  3069   with \<open>finite M\<close> show "\<exists>X. finite X \<and> (\<forall>s\<in>\<Union>M. \<exists>x\<in>X. E (x, s))"
  3070     by (intro exI[of _ "\<Union>S\<in>M. X S"]) force
  3071 qed
  3072 
  3073 
  3074 subsubsection \<open>Cauchy filter\<close>
  3075 
  3076 definition cauchy_filter :: "'a filter \<Rightarrow> bool"
  3077   where "cauchy_filter F \<longleftrightarrow> F \<times>\<^sub>F F \<le> uniformity"
  3078 
  3079 definition Cauchy :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool"
  3080   where Cauchy_uniform: "Cauchy X = cauchy_filter (filtermap X sequentially)"
  3081 
  3082 lemma Cauchy_uniform_iff:
  3083   "Cauchy X \<longleftrightarrow> (\<forall>P. eventually P uniformity \<longrightarrow> (\<exists>N. \<forall>n\<ge>N. \<forall>m\<ge>N. P (X n, X m)))"
  3084   unfolding Cauchy_uniform cauchy_filter_def le_filter_def eventually_prod_same
  3085     eventually_filtermap eventually_sequentially
  3086 proof safe
  3087   let ?U = "\<lambda>P. eventually P uniformity"
  3088   {
  3089     fix P
  3090     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)))"
  3091     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)"
  3092       by metis
  3093     then show "\<exists>N. \<forall>n\<ge>N. \<forall>m\<ge>N. P (X n, X m)"
  3094       by blast
  3095   next
  3096     fix P
  3097     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))"
  3098     then obtain Q where "?U Q" and Q: "\<And>x y z. Q (x, y) \<Longrightarrow> Q (y, z) \<Longrightarrow> P (x, z)"
  3099       by (auto elim: uniformity_transE)
  3100     then have "?U (\<lambda>x. Q x \<and> (\<lambda>(x, y). Q (y, x)) x)"
  3101       unfolding eventually_conj_iff by (simp add: uniformity_sym)
  3102     from P[rule_format, OF this]
  3103     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)"
  3104       by auto
  3105     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))"
  3106     proof (safe intro!: exI[of _ "\<lambda>x. \<forall>n\<ge>N. Q (x, X n) \<and> Q (X n, x)"] exI[of _ N] N)
  3107       fix x y
  3108       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)"
  3109       then have "Q (x, X N)" "Q (X N, y)" by auto
  3110       then show "P (x, y)"
  3111         by (rule Q)
  3112     qed
  3113   }
  3114 qed
  3115 
  3116 lemma nhds_imp_cauchy_filter:
  3117   assumes *: "F \<le> nhds x"
  3118   shows "cauchy_filter F"
  3119 proof -
  3120   have "F \<times>\<^sub>F F \<le> nhds x \<times>\<^sub>F nhds x"
  3121     by (intro prod_filter_mono *)
  3122   also have "\<dots> \<le> uniformity"
  3123     unfolding le_filter_def eventually_nhds_uniformity eventually_prod_same
  3124   proof safe
  3125     fix P
  3126     assume "eventually P uniformity"
  3127     then obtain Ql where ev: "eventually Ql uniformity"
  3128       and "Ql (x, y) \<Longrightarrow> Ql (y, z) \<Longrightarrow> P (x, z)" for x y z
  3129       by (rule uniformity_transE) simp
  3130     with ev[THEN uniformity_sym]
  3131     show "\<exists>Q. eventually (\<lambda>(x', y). x' = x \<longrightarrow> Q y) uniformity \<and>
  3132         (\<forall>x y. Q x \<longrightarrow> Q y \<longrightarrow> P (x, y))"
  3133       by (rule_tac exI[of _ "\<lambda>y. Ql (y, x) \<and> Ql (x, y)"]) (fastforce elim: eventually_elim2)
  3134   qed
  3135   finally show ?thesis
  3136     by (simp add: cauchy_filter_def)
  3137 qed
  3138 
  3139 lemma LIMSEQ_imp_Cauchy: "X \<longlonglongrightarrow> x \<Longrightarrow> Cauchy X"
  3140   unfolding Cauchy_uniform filterlim_def by (intro nhds_imp_cauchy_filter)
  3141 
  3142 lemma Cauchy_subseq_Cauchy:
  3143   assumes "Cauchy X" "strict_mono f"
  3144   shows "Cauchy (X \<circ> f)"
  3145   unfolding Cauchy_uniform comp_def filtermap_filtermap[symmetric] cauchy_filter_def
  3146   by (rule order_trans[OF _ \<open>Cauchy X\<close>[unfolded Cauchy_uniform cauchy_filter_def]])
  3147      (intro prod_filter_mono filtermap_mono filterlim_subseq[OF \<open>strict_mono f\<close>, unfolded filterlim_def])
  3148 
  3149 lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"
  3150   unfolding convergent_def by (erule exE, erule LIMSEQ_imp_Cauchy)
  3151 
  3152 definition complete :: "'a set \<Rightarrow> bool"
  3153   where complete_uniform: "complete S \<longleftrightarrow>
  3154     (\<forall>F \<le> principal S. F \<noteq> bot \<longrightarrow> cauchy_filter F \<longrightarrow> (\<exists>x\<in>S. F \<le> nhds x))"
  3155 
  3156 end
  3157 
  3158 
  3159 subsubsection \<open>Uniformly continuous functions\<close>
  3160 
  3161 definition uniformly_continuous_on :: "'a set \<Rightarrow> ('a::uniform_space \<Rightarrow> 'b::uniform_space) \<Rightarrow> bool"
  3162   where uniformly_continuous_on_uniformity: "uniformly_continuous_on s f \<longleftrightarrow>
  3163     (LIM (x, y) (uniformity_on s). (f x, f y) :> uniformity)"
  3164 
  3165 lemma uniformly_continuous_onD:
  3166   "uniformly_continuous_on s f \<Longrightarrow> eventually E uniformity \<Longrightarrow>
  3167     eventually (\<lambda>(x, y). x \<in> s \<longrightarrow> y \<in> s \<longrightarrow> E (f x, f y)) uniformity"
  3168   by (simp add: uniformly_continuous_on_uniformity filterlim_iff
  3169       eventually_inf_principal split_beta' mem_Times_iff imp_conjL)
  3170 
  3171 lemma uniformly_continuous_on_const[continuous_intros]: "uniformly_continuous_on s (\<lambda>x. c)"
  3172   by (auto simp: uniformly_continuous_on_uniformity filterlim_iff uniformity_refl)
  3173 
  3174 lemma uniformly_continuous_on_id[continuous_intros]: "uniformly_continuous_on s (\<lambda>x. x)"
  3175   by (auto simp: uniformly_continuous_on_uniformity filterlim_def)
  3176 
  3177 lemma uniformly_continuous_on_compose[continuous_intros]:
  3178   "uniformly_continuous_on s g \<Longrightarrow> uniformly_continuous_on (g`s) f \<Longrightarrow>
  3179     uniformly_continuous_on s (\<lambda>x. f (g x))"
  3180   using filterlim_compose[of "\<lambda>(x, y). (f x, f y)" uniformity
  3181       "uniformity_on (g`s)"  "\<lambda>(x, y). (g x, g y)" "uniformity_on s"]
  3182   by (simp add: split_beta' uniformly_continuous_on_uniformity
  3183       filterlim_inf filterlim_principal eventually_inf_principal mem_Times_iff)
  3184 
  3185 lemma uniformly_continuous_imp_continuous:
  3186   assumes f: "uniformly_continuous_on s f"
  3187   shows "continuous_on s f"
  3188   by (auto simp: filterlim_iff eventually_at_filter eventually_nhds_uniformity continuous_on_def
  3189            elim: eventually_mono dest!: uniformly_continuous_onD[OF f])
  3190 
  3191 
  3192 section \<open>Product Topology\<close>
  3193 
  3194 subsection \<open>Product is a topological space\<close>
  3195 
  3196 instantiation prod :: (topological_space, topological_space) topological_space
  3197 begin
  3198 
  3199 definition open_prod_def[code del]:
  3200   "open (S :: ('a \<times> 'b) set) \<longleftrightarrow>
  3201     (\<forall>x\<in>S. \<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S)"
  3202 
  3203 lemma open_prod_elim:
  3204   assumes "open S" and "x \<in> S"
  3205   obtains A B where "open A" and "open B" and "x \<in> A \<times> B" and "A \<times> B \<subseteq> S"
  3206   using assms unfolding open_prod_def by fast
  3207 
  3208 lemma open_prod_intro:
  3209   assumes "\<And>x. x \<in> S \<Longrightarrow> \<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S"
  3210   shows "open S"
  3211   using assms unfolding open_prod_def by fast
  3212 
  3213 instance
  3214 proof
  3215   show "open (UNIV :: ('a \<times> 'b) set)"
  3216     unfolding open_prod_def by auto
  3217 next
  3218   fix S T :: "('a \<times> 'b) set"
  3219   assume "open S" "open T"
  3220   show "open (S \<inter> T)"
  3221   proof (rule open_prod_intro)
  3222     fix x
  3223     assume x: "x \<in> S \<inter> T"
  3224     from x have "x \<in> S" by simp
  3225     obtain Sa Sb where A: "open Sa" "open Sb" "x \<in> Sa \<times> Sb" "Sa \<times> Sb \<subseteq> S"
  3226       using \<open>open S\<close> and \<open>x \<in> S\<close> by (rule open_prod_elim)
  3227     from x have "x \<in> T" by simp
  3228     obtain Ta Tb where B: "open Ta" "open Tb" "x \<in> Ta \<times> Tb" "Ta \<times> Tb \<subseteq> T"
  3229       using \<open>open T\<close> and \<open>x \<in> T\<close> by (rule open_prod_elim)
  3230     let ?A = "Sa \<inter> Ta" and ?B = "Sb \<inter> Tb"
  3231     have "open ?A \<and> open ?B \<and> x \<in> ?A \<times> ?B \<and> ?A \<times> ?B \<subseteq> S \<inter> T"
  3232       using A B by (auto simp add: open_Int)
  3233     then show "\<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S \<inter> T"
  3234       by fast
  3235   qed
  3236 next
  3237   fix K :: "('a \<times> 'b) set set"
  3238   assume "\<forall>S\<in>K. open S"
  3239   then show "open (\<Union>K)"
  3240     unfolding open_prod_def by fast
  3241 qed
  3242 
  3243 end
  3244 
  3245 declare [[code abort: "open :: ('a::topological_space \<times> 'b::topological_space) set \<Rightarrow> bool"]]
  3246 
  3247 lemma open_Times: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<times> T)"
  3248   unfolding open_prod_def by auto
  3249 
  3250 lemma fst_vimage_eq_Times: "fst -` S = S \<times> UNIV"
  3251   by auto
  3252 
  3253 lemma snd_vimage_eq_Times: "snd -` S = UNIV \<times> S"
  3254   by auto
  3255 
  3256 lemma open_vimage_fst: "open S \<Longrightarrow> open (fst -` S)"
  3257   by (simp add: fst_vimage_eq_Times open_Times)
  3258 
  3259 lemma open_vimage_snd: "open S \<Longrightarrow> open (snd -` S)"
  3260   by (simp add: snd_vimage_eq_Times open_Times)
  3261 
  3262 lemma closed_vimage_fst: "closed S \<Longrightarrow> closed (fst -` S)"
  3263   unfolding closed_open vimage_Compl [symmetric]
  3264   by (rule open_vimage_fst)
  3265 
  3266 lemma closed_vimage_snd: "closed S \<Longrightarrow> closed (snd -` S)"
  3267   unfolding closed_open vimage_Compl [symmetric]
  3268   by (rule open_vimage_snd)
  3269 
  3270 lemma closed_Times: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<times> T)"
  3271 proof -
  3272   have "S \<times> T = (fst -` S) \<inter> (snd -` T)"
  3273     by auto
  3274   then show "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<times> T)"
  3275     by (simp add: closed_vimage_fst closed_vimage_snd closed_Int)
  3276 qed
  3277 
  3278 lemma subset_fst_imageI: "A \<times> B \<subseteq> S \<Longrightarrow> y \<in> B \<Longrightarrow> A \<subseteq> fst ` S"
  3279   unfolding image_def subset_eq by force
  3280 
  3281 lemma subset_snd_imageI: "A \<times> B \<subseteq> S \<Longrightarrow> x \<in> A \<Longrightarrow> B \<subseteq> snd ` S"
  3282   unfolding image_def subset_eq by force
  3283 
  3284 lemma open_image_fst:
  3285   assumes "open S"
  3286   shows "open (fst ` S)"
  3287 proof (rule openI)
  3288   fix x
  3289   assume "x \<in> fst ` S"
  3290   then obtain y where "(x, y) \<in> S"
  3291     by auto
  3292   then obtain A B where "open A" "open B" "x \<in> A" "y \<in> B" "A \<times> B \<subseteq> S"
  3293     using \<open>open S\<close> unfolding open_prod_def by auto
  3294   from \<open>A \<times> B \<subseteq> S\<close> \<open>y \<in> B\<close> have "A \<subseteq> fst ` S"
  3295     by (rule subset_fst_imageI)
  3296   with \<open>open A\<close> \<open>x \<in> A\<close> have "open A \<and> x \<in> A \<and> A \<subseteq> fst ` S"
  3297     by simp
  3298   then show "\<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> fst ` S" ..
  3299 qed
  3300 
  3301 lemma open_image_snd:
  3302   assumes "open S"
  3303   shows "open (snd ` S)"
  3304 proof (rule openI)
  3305   fix y
  3306   assume "y \<in> snd ` S"
  3307   then obtain x where "(x, y) \<in> S"
  3308     by auto
  3309   then obtain A B where "open A" "open B" "x \<in> A" "y \<in> B" "A \<times> B \<subseteq> S"
  3310     using \<open>open S\<close> unfolding open_prod_def by auto
  3311   from \<open>A \<times> B \<subseteq> S\<close> \<open>x \<in> A\<close> have "B \<subseteq> snd ` S"
  3312     by (rule subset_snd_imageI)
  3313   with \<open>open B\<close> \<open>y \<in> B\<close> have "open B \<and> y \<in> B \<and> B \<subseteq> snd ` S"
  3314     by simp
  3315   then show "\<exists>T. open T \<and> y \<in> T \<and> T \<subseteq> snd ` S" ..
  3316 qed
  3317 
  3318 lemma nhds_prod: "nhds (a, b) = nhds a \<times>\<^sub>F nhds b"
  3319   unfolding nhds_def
  3320 proof (subst prod_filter_INF, auto intro!: antisym INF_greatest simp: principal_prod_principal)
  3321   fix S T
  3322   assume "open S" "a \<in> S" "open T" "b \<in> T"
  3323   then show "(INF x : {S. open S \<and> (a, b) \<in> S}. principal x) \<le> principal (S \<times> T)"
  3324     by (intro INF_lower) (auto intro!: open_Times)
  3325 next
  3326   fix S'
  3327   assume "open S'" "(a, b) \<in> S'"
  3328   then obtain S T where "open S" "a \<in> S" "open T" "b \<in> T" "S \<times> T \<subseteq> S'"
  3329     by (auto elim: open_prod_elim)
  3330   then show "(INF x : {S. open S \<and> a \<in> S}. INF y : {S. open S \<and> b \<in> S}.
  3331       principal (x \<times> y)) \<le> principal S'"
  3332     by (auto intro!: INF_lower2)
  3333 qed
  3334 
  3335 
  3336 subsubsection \<open>Continuity of operations\<close>
  3337 
  3338 lemma tendsto_fst [tendsto_intros]:
  3339   assumes "(f \<longlongrightarrow> a) F"
  3340   shows "((\<lambda>x. fst (f x)) \<longlongrightarrow> fst a) F"
  3341 proof (rule topological_tendstoI)
  3342   fix S
  3343   assume "open S" and "fst a \<in> S"
  3344   then have "open (fst -` S)" and "a \<in> fst -` S"
  3345     by (simp_all add: open_vimage_fst)
  3346   with assms have "eventually (\<lambda>x. f x \<in> fst -` S) F"
  3347     by (rule topological_tendstoD)
  3348   then show "eventually (\<lambda>x. fst (f x) \<in> S) F"
  3349     by simp
  3350 qed
  3351 
  3352 lemma tendsto_snd [tendsto_intros]:
  3353   assumes "(f \<longlongrightarrow> a) F"
  3354   shows "((\<lambda>x. snd (f x)) \<longlongrightarrow> snd a) F"
  3355 proof (rule topological_tendstoI)
  3356   fix S
  3357   assume "open S" and "snd a \<in> S"
  3358   then have "open (snd -` S)" and "a \<in> snd -` S"
  3359     by (simp_all add: open_vimage_snd)
  3360   with assms have "eventually (\<lambda>x. f x \<in> snd -` S) F"
  3361     by (rule topological_tendstoD)
  3362   then show "eventually (\<lambda>x. snd (f x) \<in> S) F"
  3363     by simp
  3364 qed
  3365 
  3366 lemma tendsto_Pair [tendsto_intros]:
  3367   assumes "(f \<longlongrightarrow> a) F" and "(g \<longlongrightarrow> b) F"
  3368   shows "((\<lambda>x. (f x, g x)) \<longlongrightarrow> (a, b)) F"
  3369 proof (rule topological_tendstoI)
  3370   fix S
  3371   assume "open S" and "(a, b) \<in> S"
  3372   then obtain A B where "open A" "open B" "a \<in> A" "b \<in> B" "A \<times> B \<subseteq> S"
  3373     unfolding open_prod_def by fast
  3374   have "eventually (\<lambda>x. f x \<in> A) F"
  3375     using \<open>(f \<longlongrightarrow> a) F\<close> \<open>open A\<close> \<open>a \<in> A\<close>
  3376     by (rule topological_tendstoD)
  3377   moreover
  3378   have "eventually (\<lambda>x. g x \<in> B) F"
  3379     using \<open>(g \<longlongrightarrow> b) F\<close> \<open>open B\<close> \<open>b \<in> B\<close>
  3380     by (rule topological_tendstoD)
  3381   ultimately
  3382   show "eventually (\<lambda>x. (f x, g x) \<in> S) F"
  3383     by (rule eventually_elim2)
  3384        (simp add: subsetD [OF \<open>A \<times> B \<subseteq> S\<close>])
  3385 qed
  3386 
  3387 lemma continuous_fst[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. fst (f x))"
  3388   unfolding continuous_def by (rule tendsto_fst)
  3389 
  3390 lemma continuous_snd[continuous_intros]: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. snd (f x))"
  3391   unfolding continuous_def by (rule tendsto_snd)
  3392 
  3393 lemma continuous_Pair[continuous_intros]:
  3394   "continuous F f \<Longrightarrow> continuous F g \<Longrightarrow> continuous F (\<lambda>x. (f x, g x))"
  3395   unfolding continuous_def by (rule tendsto_Pair)
  3396 
  3397 lemma continuous_on_fst[continuous_intros]:
  3398   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. fst (f x))"
  3399   unfolding continuous_on_def by (auto intro: tendsto_fst)
  3400 
  3401 lemma continuous_on_snd[continuous_intros]:
  3402   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. snd (f x))"
  3403   unfolding continuous_on_def by (auto intro: tendsto_snd)
  3404 
  3405 lemma continuous_on_Pair[continuous_intros]:
  3406   "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. (f x, g x))"
  3407   unfolding continuous_on_def by (auto intro: tendsto_Pair)
  3408 
  3409 lemma continuous_on_swap[continuous_intros]: "continuous_on A prod.swap"
  3410   by (simp add: prod.swap_def continuous_on_fst continuous_on_snd
  3411       continuous_on_Pair continuous_on_id)
  3412 
  3413 lemma continuous_on_swap_args:
  3414   assumes "continuous_on (A\<times>B) (\<lambda>(x,y). d x y)"
  3415     shows "continuous_on (B\<times>A) (\<lambda>(x,y). d y x)"
  3416 proof -
  3417   have "(\<lambda>(x,y). d y x) = (\<lambda>(x,y). d x y) \<circ> prod.swap"
  3418     by force
  3419   then show ?thesis
  3420     apply (rule ssubst)
  3421     apply (rule continuous_on_compose)
  3422      apply (force intro: continuous_on_subset [OF continuous_on_swap])
  3423     apply (force intro: continuous_on_subset [OF assms])
  3424     done
  3425 qed
  3426 
  3427 lemma isCont_fst [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. fst (f x)) a"
  3428   by (fact continuous_fst)
  3429 
  3430 lemma isCont_snd [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. snd (f x)) a"
  3431   by (fact continuous_snd)
  3432 
  3433 lemma isCont_Pair [simp]: "\<lbrakk>isCont f a; isCont g a\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. (f x, g x)) a"
  3434   by (fact continuous_Pair)
  3435 
  3436 
  3437 subsubsection \<open>Separation axioms\<close>
  3438 
  3439 instance prod :: (t0_space, t0_space) t0_space
  3440 proof
  3441   fix x y :: "'a \<times> 'b"
  3442   assume "x \<noteq> y"
  3443   then have "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
  3444     by (simp add: prod_eq_iff)
  3445   then show "\<exists>U. open U \<and> (x \<in> U) \<noteq> (y \<in> U)"
  3446     by (fast dest: t0_space elim: open_vimage_fst open_vimage_snd)
  3447 qed
  3448 
  3449 instance prod :: (t1_space, t1_space) t1_space
  3450 proof
  3451   fix x y :: "'a \<times> 'b"
  3452   assume "x \<noteq> y"
  3453   then have "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
  3454     by (simp add: prod_eq_iff)
  3455   then show "\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
  3456     by (fast dest: t1_space elim: open_vimage_fst open_vimage_snd)
  3457 qed
  3458 
  3459 instance prod :: (t2_space, t2_space) t2_space
  3460 proof
  3461   fix x y :: "'a \<times> 'b"
  3462   assume "x \<noteq> y"
  3463   then have "fst x \<noteq> fst y \<or> snd x \<noteq> snd y"
  3464     by (simp add: prod_eq_iff)
  3465   then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
  3466     by (fast dest: hausdorff elim: open_vimage_fst open_vimage_snd)
  3467 qed
  3468 
  3469 lemma isCont_swap[continuous_intros]: "isCont prod.swap a"
  3470   using continuous_on_eq_continuous_within continuous_on_swap by blast
  3471 
  3472 lemma open_diagonal_complement:
  3473   "open {(x,y) | x y. x \<noteq> (y::('a::t2_space))}"
  3474 proof (rule topological_space_class.openI)
  3475   fix t assume "t \<in> {(x, y) | x y. x \<noteq> (y::'a)}"
  3476   then obtain x y where "t = (x,y)" "x \<noteq> y" by blast
  3477   then obtain U V where *: "open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
  3478     by (auto simp add: separation_t2)
  3479   define T where "T = U \<times> V"
  3480   have "open T" using * open_Times T_def by auto
  3481   moreover have "t \<in> T" unfolding T_def using `t = (x,y)` * by auto
  3482   moreover have "T \<subseteq> {(x, y) | x y. x \<noteq> y}" unfolding T_def using * by auto
  3483   ultimately show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {(x, y) | x y. x \<noteq> y}" by auto
  3484 qed
  3485 
  3486 lemma closed_diagonal:
  3487   "closed {y. \<exists> x::('a::t2_space). y = (x,x)}"
  3488 proof -
  3489   have "{y. \<exists> x::'a. y = (x,x)} = UNIV - {(x,y) | x y. x \<noteq> y}" by auto
  3490   then show ?thesis using open_diagonal_complement closed_Diff by auto
  3491 qed
  3492 
  3493 lemma open_superdiagonal:
  3494   "open {(x,y) | x y. x > (y::'a::{linorder_topology})}"
  3495 proof (rule topological_space_class.openI)
  3496   fix t assume "t \<in> {(x, y) | x y. y < (x::'a)}"
  3497   then obtain x y where "t = (x, y)" "x > y" by blast
  3498   show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {(x, y) | x y. y < x}"
  3499   proof (cases)
  3500     assume "\<exists>z. y < z \<and> z < x"
  3501     then obtain z where z: "y < z \<and> z < x" by blast
  3502     define T where "T = {z<..} \<times> {..<z}"
  3503     have "open T" unfolding T_def by (simp add: open_Times)
  3504     moreover have "t \<in> T" using T_def z `t = (x,y)` by auto
  3505     moreover have "T \<subseteq> {(x, y) | x y. y < x}" unfolding T_def by auto
  3506     ultimately show ?thesis by auto
  3507   next
  3508     assume "\<not>(\<exists>z. y < z \<and> z < x)"
  3509     then have *: "{x ..} = {y<..}" "{..< x} = {..y}"
  3510       using `x > y` apply auto using leI by blast
  3511     define T where "T = {x ..} \<times> {.. y}"
  3512     then have "T = {y<..} \<times> {..< x}" using * by simp
  3513     then have "open T" unfolding T_def by (simp add: open_Times)
  3514     moreover have "t \<in> T" using T_def `t = (x,y)` by auto
  3515     moreover have "T \<subseteq> {(x, y) | x y. y < x}" unfolding T_def using `x > y` by auto
  3516     ultimately show ?thesis by auto
  3517   qed
  3518 qed
  3519 
  3520 lemma closed_subdiagonal:
  3521   "closed {(x,y) | x y. x \<le> (y::'a::{linorder_topology})}"
  3522 proof -
  3523   have "{(x,y) | x y. x \<le> (y::'a)} = UNIV - {(x,y) | x y. x > (y::'a)}" by auto
  3524   then show ?thesis using open_superdiagonal closed_Diff by auto
  3525 qed
  3526 
  3527 lemma open_subdiagonal:
  3528   "open {(x,y) | x y. x < (y::'a::{linorder_topology})}"
  3529 proof (rule topological_space_class.openI)
  3530   fix t assume "t \<in> {(x, y) | x y. y > (x::'a)}"
  3531   then obtain x y where "t = (x, y)" "x < y" by blast
  3532   show "\<exists>T. open T \<and> t \<in> T \<and> T \<subseteq> {(x, y) | x y. y > x}"
  3533   proof (cases)
  3534     assume "\<exists>z. y > z \<and> z > x"
  3535     then obtain z where z: "y > z \<and> z > x" by blast
  3536     define T where "T = {..<z} \<times> {z<..}"
  3537     have "open T" unfolding T_def by (simp add: open_Times)
  3538     moreover have "t \<in> T" using T_def z `t = (x,y)` by auto
  3539     moreover have "T \<subseteq> {(x, y) |x y. y > x}" unfolding T_def by auto
  3540     ultimately show ?thesis by auto
  3541   next
  3542     assume "\<not>(\<exists>z. y > z \<and> z > x)"
  3543     then have *: "{..x} = {..<y}" "{x<..} = {y..}"
  3544       using `x < y` apply auto using leI by blast
  3545     define T where "T = {..x} \<times> {y..}"
  3546     then have "T = {..<y} \<times> {x<..}" using * by simp
  3547     then have "open T" unfolding T_def by (simp add: open_Times)
  3548     moreover have "t \<in> T" using T_def `t = (x,y)` by auto
  3549     moreover have "T \<subseteq> {(x, y) |x y. y > x}" unfolding T_def using `x < y` by auto
  3550     ultimately show ?thesis by auto
  3551   qed
  3552 qed
  3553 
  3554 lemma closed_superdiagonal:
  3555   "closed {(x,y) | x y. x \<ge> (y::('a::{linorder_topology}))}"
  3556 proof -
  3557   have "{(x,y) | x y. x \<ge> (y::'a)} = UNIV - {(x,y) | x y. x < y}" by auto
  3558   then show ?thesis using open_subdiagonal closed_Diff by auto
  3559 qed
  3560 
  3561 end