src/HOL/Topological_Spaces.thy
author hoelzl
Tue Mar 26 12:20:58 2013 +0100 (2013-03-26)
changeset 51526 155263089e7b
parent 51518 6a56b7088a6a
child 51641 cd05e9fcc63d
permissions -rw-r--r--
move SEQ.thy and Lim.thy to Limits.thy
     1 (*  Title:      HOL/Basic_Topology.thy
     2     Author:     Brian Huffman
     3     Author:     Johannes Hölzl
     4 *)
     5 
     6 header {* Topological Spaces *}
     7 
     8 theory Topological_Spaces
     9 imports Main Conditional_Complete_Lattices
    10 begin
    11 
    12 subsection {* Topological space *}
    13 
    14 class "open" =
    15   fixes "open" :: "'a set \<Rightarrow> bool"
    16 
    17 class topological_space = "open" +
    18   assumes open_UNIV [simp, intro]: "open UNIV"
    19   assumes open_Int [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<inter> T)"
    20   assumes open_Union [intro]: "\<forall>S\<in>K. open S \<Longrightarrow> open (\<Union> K)"
    21 begin
    22 
    23 definition
    24   closed :: "'a set \<Rightarrow> bool" where
    25   "closed S \<longleftrightarrow> open (- S)"
    26 
    27 lemma open_empty [intro, simp]: "open {}"
    28   using open_Union [of "{}"] by simp
    29 
    30 lemma open_Un [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<union> T)"
    31   using open_Union [of "{S, T}"] by simp
    32 
    33 lemma open_UN [intro]: "\<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Union>x\<in>A. B x)"
    34   unfolding SUP_def by (rule open_Union) auto
    35 
    36 lemma open_Inter [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. open T \<Longrightarrow> open (\<Inter>S)"
    37   by (induct set: finite) auto
    38 
    39 lemma open_INT [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Inter>x\<in>A. B x)"
    40   unfolding INF_def by (rule open_Inter) auto
    41 
    42 lemma openI:
    43   assumes "\<And>x. x \<in> S \<Longrightarrow> \<exists>T. open T \<and> x \<in> T \<and> T \<subseteq> S"
    44   shows "open S"
    45 proof -
    46   have "open (\<Union>{T. open T \<and> T \<subseteq> S})" by auto
    47   moreover have "\<Union>{T. open T \<and> T \<subseteq> S} = S" by (auto dest!: assms)
    48   ultimately show "open S" by simp
    49 qed
    50 
    51 lemma closed_empty [intro, simp]:  "closed {}"
    52   unfolding closed_def by simp
    53 
    54 lemma closed_Un [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<union> T)"
    55   unfolding closed_def by auto
    56 
    57 lemma closed_UNIV [intro, simp]: "closed UNIV"
    58   unfolding closed_def by simp
    59 
    60 lemma closed_Int [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<inter> T)"
    61   unfolding closed_def by auto
    62 
    63 lemma closed_INT [intro]: "\<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Inter>x\<in>A. B x)"
    64   unfolding closed_def by auto
    65 
    66 lemma closed_Inter [intro]: "\<forall>S\<in>K. closed S \<Longrightarrow> closed (\<Inter> K)"
    67   unfolding closed_def uminus_Inf by auto
    68 
    69 lemma closed_Union [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. closed T \<Longrightarrow> closed (\<Union>S)"
    70   by (induct set: finite) auto
    71 
    72 lemma closed_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Union>x\<in>A. B x)"
    73   unfolding SUP_def by (rule closed_Union) auto
    74 
    75 lemma open_closed: "open S \<longleftrightarrow> closed (- S)"
    76   unfolding closed_def by simp
    77 
    78 lemma closed_open: "closed S \<longleftrightarrow> open (- S)"
    79   unfolding closed_def by simp
    80 
    81 lemma open_Diff [intro]: "open S \<Longrightarrow> closed T \<Longrightarrow> open (S - T)"
    82   unfolding closed_open Diff_eq by (rule open_Int)
    83 
    84 lemma closed_Diff [intro]: "closed S \<Longrightarrow> open T \<Longrightarrow> closed (S - T)"
    85   unfolding open_closed Diff_eq by (rule closed_Int)
    86 
    87 lemma open_Compl [intro]: "closed S \<Longrightarrow> open (- S)"
    88   unfolding closed_open .
    89 
    90 lemma closed_Compl [intro]: "open S \<Longrightarrow> closed (- S)"
    91   unfolding open_closed .
    92 
    93 end
    94 
    95 subsection{* Hausdorff and other separation properties *}
    96 
    97 class t0_space = topological_space +
    98   assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
    99 
   100 class t1_space = topological_space +
   101   assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
   102 
   103 instance t1_space \<subseteq> t0_space
   104 proof qed (fast dest: t1_space)
   105 
   106 lemma separation_t1:
   107   fixes x y :: "'a::t1_space"
   108   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U)"
   109   using t1_space[of x y] by blast
   110 
   111 lemma closed_singleton:
   112   fixes a :: "'a::t1_space"
   113   shows "closed {a}"
   114 proof -
   115   let ?T = "\<Union>{S. open S \<and> a \<notin> S}"
   116   have "open ?T" by (simp add: open_Union)
   117   also have "?T = - {a}"
   118     by (simp add: set_eq_iff separation_t1, auto)
   119   finally show "closed {a}" unfolding closed_def .
   120 qed
   121 
   122 lemma closed_insert [simp]:
   123   fixes a :: "'a::t1_space"
   124   assumes "closed S" shows "closed (insert a S)"
   125 proof -
   126   from closed_singleton assms
   127   have "closed ({a} \<union> S)" by (rule closed_Un)
   128   thus "closed (insert a S)" by simp
   129 qed
   130 
   131 lemma finite_imp_closed:
   132   fixes S :: "'a::t1_space set"
   133   shows "finite S \<Longrightarrow> closed S"
   134 by (induct set: finite, simp_all)
   135 
   136 text {* T2 spaces are also known as Hausdorff spaces. *}
   137 
   138 class t2_space = topological_space +
   139   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 = {}"
   140 
   141 instance t2_space \<subseteq> t1_space
   142 proof qed (fast dest: hausdorff)
   143 
   144 lemma separation_t2:
   145   fixes x y :: "'a::t2_space"
   146   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
   147   using hausdorff[of x y] by blast
   148 
   149 lemma separation_t0:
   150   fixes x y :: "'a::t0_space"
   151   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> ~(x\<in>U \<longleftrightarrow> y\<in>U))"
   152   using t0_space[of x y] by blast
   153 
   154 text {* A perfect space is a topological space with no isolated points. *}
   155 
   156 class perfect_space = topological_space +
   157   assumes not_open_singleton: "\<not> open {x}"
   158 
   159 
   160 subsection {* Generators for toplogies *}
   161 
   162 inductive generate_topology for S where
   163   UNIV: "generate_topology S UNIV"
   164 | Int: "generate_topology S a \<Longrightarrow> generate_topology S b \<Longrightarrow> generate_topology S (a \<inter> b)"
   165 | UN: "(\<And>k. k \<in> K \<Longrightarrow> generate_topology S k) \<Longrightarrow> generate_topology S (\<Union>K)"
   166 | Basis: "s \<in> S \<Longrightarrow> generate_topology S s"
   167 
   168 hide_fact (open) UNIV Int UN Basis 
   169 
   170 lemma generate_topology_Union: 
   171   "(\<And>k. k \<in> I \<Longrightarrow> generate_topology S (K k)) \<Longrightarrow> generate_topology S (\<Union>k\<in>I. K k)"
   172   unfolding SUP_def by (intro generate_topology.UN) auto
   173 
   174 lemma topological_space_generate_topology:
   175   "class.topological_space (generate_topology S)"
   176   by default (auto intro: generate_topology.intros)
   177 
   178 subsection {* Order topologies *}
   179 
   180 class order_topology = order + "open" +
   181   assumes open_generated_order: "open = generate_topology (range (\<lambda>a. {..< a}) \<union> range (\<lambda>a. {a <..}))"
   182 begin
   183 
   184 subclass topological_space
   185   unfolding open_generated_order
   186   by (rule topological_space_generate_topology)
   187 
   188 lemma open_greaterThan [simp]: "open {a <..}"
   189   unfolding open_generated_order by (auto intro: generate_topology.Basis)
   190 
   191 lemma open_lessThan [simp]: "open {..< a}"
   192   unfolding open_generated_order by (auto intro: generate_topology.Basis)
   193 
   194 lemma open_greaterThanLessThan [simp]: "open {a <..< b}"
   195    unfolding greaterThanLessThan_eq by (simp add: open_Int)
   196 
   197 end
   198 
   199 class linorder_topology = linorder + order_topology
   200 
   201 lemma closed_atMost [simp]: "closed {.. a::'a::linorder_topology}"
   202   by (simp add: closed_open)
   203 
   204 lemma closed_atLeast [simp]: "closed {a::'a::linorder_topology ..}"
   205   by (simp add: closed_open)
   206 
   207 lemma closed_atLeastAtMost [simp]: "closed {a::'a::linorder_topology .. b}"
   208 proof -
   209   have "{a .. b} = {a ..} \<inter> {.. b}"
   210     by auto
   211   then show ?thesis
   212     by (simp add: closed_Int)
   213 qed
   214 
   215 lemma (in linorder) less_separate:
   216   assumes "x < y"
   217   shows "\<exists>a b. x \<in> {..< a} \<and> y \<in> {b <..} \<and> {..< a} \<inter> {b <..} = {}"
   218 proof cases
   219   assume "\<exists>z. x < z \<and> z < y"
   220   then guess z ..
   221   then have "x \<in> {..< z} \<and> y \<in> {z <..} \<and> {z <..} \<inter> {..< z} = {}"
   222     by auto
   223   then show ?thesis by blast
   224 next
   225   assume "\<not> (\<exists>z. x < z \<and> z < y)"
   226   with `x < y` have "x \<in> {..< y} \<and> y \<in> {x <..} \<and> {x <..} \<inter> {..< y} = {}"
   227     by auto
   228   then show ?thesis by blast
   229 qed
   230 
   231 instance linorder_topology \<subseteq> t2_space
   232 proof
   233   fix x y :: 'a
   234   from less_separate[of x y] less_separate[of y x]
   235   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 = {}"
   236     by (elim neqE) (metis open_lessThan open_greaterThan Int_commute)+
   237 qed
   238 
   239 lemma (in linorder_topology) open_right:
   240   assumes "open S" "x \<in> S" and gt_ex: "x < y" shows "\<exists>b>x. {x ..< b} \<subseteq> S"
   241   using assms unfolding open_generated_order
   242 proof induction
   243   case (Int A B)
   244   then obtain a b where "a > x" "{x ..< a} \<subseteq> A"  "b > x" "{x ..< b} \<subseteq> B" by auto
   245   then show ?case by (auto intro!: exI[of _ "min a b"])
   246 next
   247   case (Basis S) then show ?case by (fastforce intro: exI[of _ y] gt_ex)
   248 qed blast+
   249 
   250 lemma (in linorder_topology) open_left:
   251   assumes "open S" "x \<in> S" and lt_ex: "y < x" shows "\<exists>b<x. {b <.. x} \<subseteq> S"
   252   using assms unfolding open_generated_order
   253 proof induction
   254   case (Int A B)
   255   then obtain a b where "a < x" "{a <.. x} \<subseteq> A"  "b < x" "{b <.. x} \<subseteq> B" by auto
   256   then show ?case by (auto intro!: exI[of _ "max a b"])
   257 next
   258   case (Basis S) then show ?case by (fastforce intro: exI[of _ y] lt_ex)
   259 qed blast+
   260 
   261 subsection {* Filters *}
   262 
   263 text {*
   264   This definition also allows non-proper filters.
   265 *}
   266 
   267 locale is_filter =
   268   fixes F :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
   269   assumes True: "F (\<lambda>x. True)"
   270   assumes conj: "F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x) \<Longrightarrow> F (\<lambda>x. P x \<and> Q x)"
   271   assumes mono: "\<forall>x. P x \<longrightarrow> Q x \<Longrightarrow> F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x)"
   272 
   273 typedef 'a filter = "{F :: ('a \<Rightarrow> bool) \<Rightarrow> bool. is_filter F}"
   274 proof
   275   show "(\<lambda>x. True) \<in> ?filter" by (auto intro: is_filter.intro)
   276 qed
   277 
   278 lemma is_filter_Rep_filter: "is_filter (Rep_filter F)"
   279   using Rep_filter [of F] by simp
   280 
   281 lemma Abs_filter_inverse':
   282   assumes "is_filter F" shows "Rep_filter (Abs_filter F) = F"
   283   using assms by (simp add: Abs_filter_inverse)
   284 
   285 
   286 subsubsection {* Eventually *}
   287 
   288 definition eventually :: "('a \<Rightarrow> bool) \<Rightarrow> 'a filter \<Rightarrow> bool"
   289   where "eventually P F \<longleftrightarrow> Rep_filter F P"
   290 
   291 lemma eventually_Abs_filter:
   292   assumes "is_filter F" shows "eventually P (Abs_filter F) = F P"
   293   unfolding eventually_def using assms by (simp add: Abs_filter_inverse)
   294 
   295 lemma filter_eq_iff:
   296   shows "F = F' \<longleftrightarrow> (\<forall>P. eventually P F = eventually P F')"
   297   unfolding Rep_filter_inject [symmetric] fun_eq_iff eventually_def ..
   298 
   299 lemma eventually_True [simp]: "eventually (\<lambda>x. True) F"
   300   unfolding eventually_def
   301   by (rule is_filter.True [OF is_filter_Rep_filter])
   302 
   303 lemma always_eventually: "\<forall>x. P x \<Longrightarrow> eventually P F"
   304 proof -
   305   assume "\<forall>x. P x" hence "P = (\<lambda>x. True)" by (simp add: ext)
   306   thus "eventually P F" by simp
   307 qed
   308 
   309 lemma eventually_mono:
   310   "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually P F \<Longrightarrow> eventually Q F"
   311   unfolding eventually_def
   312   by (rule is_filter.mono [OF is_filter_Rep_filter])
   313 
   314 lemma eventually_conj:
   315   assumes P: "eventually (\<lambda>x. P x) F"
   316   assumes Q: "eventually (\<lambda>x. Q x) F"
   317   shows "eventually (\<lambda>x. P x \<and> Q x) F"
   318   using assms unfolding eventually_def
   319   by (rule is_filter.conj [OF is_filter_Rep_filter])
   320 
   321 lemma eventually_Ball_finite:
   322   assumes "finite A" and "\<forall>y\<in>A. eventually (\<lambda>x. P x y) net"
   323   shows "eventually (\<lambda>x. \<forall>y\<in>A. P x y) net"
   324 using assms by (induct set: finite, simp, simp add: eventually_conj)
   325 
   326 lemma eventually_all_finite:
   327   fixes P :: "'a \<Rightarrow> 'b::finite \<Rightarrow> bool"
   328   assumes "\<And>y. eventually (\<lambda>x. P x y) net"
   329   shows "eventually (\<lambda>x. \<forall>y. P x y) net"
   330 using eventually_Ball_finite [of UNIV P] assms by simp
   331 
   332 lemma eventually_mp:
   333   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
   334   assumes "eventually (\<lambda>x. P x) F"
   335   shows "eventually (\<lambda>x. Q x) F"
   336 proof (rule eventually_mono)
   337   show "\<forall>x. (P x \<longrightarrow> Q x) \<and> P x \<longrightarrow> Q x" by simp
   338   show "eventually (\<lambda>x. (P x \<longrightarrow> Q x) \<and> P x) F"
   339     using assms by (rule eventually_conj)
   340 qed
   341 
   342 lemma eventually_rev_mp:
   343   assumes "eventually (\<lambda>x. P x) F"
   344   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
   345   shows "eventually (\<lambda>x. Q x) F"
   346 using assms(2) assms(1) by (rule eventually_mp)
   347 
   348 lemma eventually_conj_iff:
   349   "eventually (\<lambda>x. P x \<and> Q x) F \<longleftrightarrow> eventually P F \<and> eventually Q F"
   350   by (auto intro: eventually_conj elim: eventually_rev_mp)
   351 
   352 lemma eventually_elim1:
   353   assumes "eventually (\<lambda>i. P i) F"
   354   assumes "\<And>i. P i \<Longrightarrow> Q i"
   355   shows "eventually (\<lambda>i. Q i) F"
   356   using assms by (auto elim!: eventually_rev_mp)
   357 
   358 lemma eventually_elim2:
   359   assumes "eventually (\<lambda>i. P i) F"
   360   assumes "eventually (\<lambda>i. Q i) F"
   361   assumes "\<And>i. P i \<Longrightarrow> Q i \<Longrightarrow> R i"
   362   shows "eventually (\<lambda>i. R i) F"
   363   using assms by (auto elim!: eventually_rev_mp)
   364 
   365 lemma eventually_subst:
   366   assumes "eventually (\<lambda>n. P n = Q n) F"
   367   shows "eventually P F = eventually Q F" (is "?L = ?R")
   368 proof -
   369   from assms have "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
   370       and "eventually (\<lambda>x. Q x \<longrightarrow> P x) F"
   371     by (auto elim: eventually_elim1)
   372   then show ?thesis by (auto elim: eventually_elim2)
   373 qed
   374 
   375 ML {*
   376   fun eventually_elim_tac ctxt thms thm =
   377     let
   378       val thy = Proof_Context.theory_of ctxt
   379       val mp_thms = thms RL [@{thm eventually_rev_mp}]
   380       val raw_elim_thm =
   381         (@{thm allI} RS @{thm always_eventually})
   382         |> fold (fn thm1 => fn thm2 => thm2 RS thm1) mp_thms
   383         |> fold (fn _ => fn thm => @{thm impI} RS thm) thms
   384       val cases_prop = prop_of (raw_elim_thm RS thm)
   385       val cases = (Rule_Cases.make_common (thy, cases_prop) [(("elim", []), [])])
   386     in
   387       CASES cases (rtac raw_elim_thm 1) thm
   388     end
   389 *}
   390 
   391 method_setup eventually_elim = {*
   392   Scan.succeed (fn ctxt => METHOD_CASES (eventually_elim_tac ctxt))
   393 *} "elimination of eventually quantifiers"
   394 
   395 
   396 subsubsection {* Finer-than relation *}
   397 
   398 text {* @{term "F \<le> F'"} means that filter @{term F} is finer than
   399 filter @{term F'}. *}
   400 
   401 instantiation filter :: (type) complete_lattice
   402 begin
   403 
   404 definition le_filter_def:
   405   "F \<le> F' \<longleftrightarrow> (\<forall>P. eventually P F' \<longrightarrow> eventually P F)"
   406 
   407 definition
   408   "(F :: 'a filter) < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   409 
   410 definition
   411   "top = Abs_filter (\<lambda>P. \<forall>x. P x)"
   412 
   413 definition
   414   "bot = Abs_filter (\<lambda>P. True)"
   415 
   416 definition
   417   "sup F F' = Abs_filter (\<lambda>P. eventually P F \<and> eventually P F')"
   418 
   419 definition
   420   "inf F F' = Abs_filter
   421       (\<lambda>P. \<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   422 
   423 definition
   424   "Sup S = Abs_filter (\<lambda>P. \<forall>F\<in>S. eventually P F)"
   425 
   426 definition
   427   "Inf S = Sup {F::'a filter. \<forall>F'\<in>S. F \<le> F'}"
   428 
   429 lemma eventually_top [simp]: "eventually P top \<longleftrightarrow> (\<forall>x. P x)"
   430   unfolding top_filter_def
   431   by (rule eventually_Abs_filter, rule is_filter.intro, auto)
   432 
   433 lemma eventually_bot [simp]: "eventually P bot"
   434   unfolding bot_filter_def
   435   by (subst eventually_Abs_filter, rule is_filter.intro, auto)
   436 
   437 lemma eventually_sup:
   438   "eventually P (sup F F') \<longleftrightarrow> eventually P F \<and> eventually P F'"
   439   unfolding sup_filter_def
   440   by (rule eventually_Abs_filter, rule is_filter.intro)
   441      (auto elim!: eventually_rev_mp)
   442 
   443 lemma eventually_inf:
   444   "eventually P (inf F F') \<longleftrightarrow>
   445    (\<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   446   unfolding inf_filter_def
   447   apply (rule eventually_Abs_filter, rule is_filter.intro)
   448   apply (fast intro: eventually_True)
   449   apply clarify
   450   apply (intro exI conjI)
   451   apply (erule (1) eventually_conj)
   452   apply (erule (1) eventually_conj)
   453   apply simp
   454   apply auto
   455   done
   456 
   457 lemma eventually_Sup:
   458   "eventually P (Sup S) \<longleftrightarrow> (\<forall>F\<in>S. eventually P F)"
   459   unfolding Sup_filter_def
   460   apply (rule eventually_Abs_filter, rule is_filter.intro)
   461   apply (auto intro: eventually_conj elim!: eventually_rev_mp)
   462   done
   463 
   464 instance proof
   465   fix F F' F'' :: "'a filter" and S :: "'a filter set"
   466   { show "F < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   467     by (rule less_filter_def) }
   468   { show "F \<le> F"
   469     unfolding le_filter_def by simp }
   470   { assume "F \<le> F'" and "F' \<le> F''" thus "F \<le> F''"
   471     unfolding le_filter_def by simp }
   472   { assume "F \<le> F'" and "F' \<le> F" thus "F = F'"
   473     unfolding le_filter_def filter_eq_iff by fast }
   474   { show "F \<le> top"
   475     unfolding le_filter_def eventually_top by (simp add: always_eventually) }
   476   { show "bot \<le> F"
   477     unfolding le_filter_def by simp }
   478   { show "F \<le> sup F F'" and "F' \<le> sup F F'"
   479     unfolding le_filter_def eventually_sup by simp_all }
   480   { assume "F \<le> F''" and "F' \<le> F''" thus "sup F F' \<le> F''"
   481     unfolding le_filter_def eventually_sup by simp }
   482   { show "inf F F' \<le> F" and "inf F F' \<le> F'"
   483     unfolding le_filter_def eventually_inf by (auto intro: eventually_True) }
   484   { assume "F \<le> F'" and "F \<le> F''" thus "F \<le> inf F' F''"
   485     unfolding le_filter_def eventually_inf
   486     by (auto elim!: eventually_mono intro: eventually_conj) }
   487   { assume "F \<in> S" thus "F \<le> Sup S"
   488     unfolding le_filter_def eventually_Sup by simp }
   489   { assume "\<And>F. F \<in> S \<Longrightarrow> F \<le> F'" thus "Sup S \<le> F'"
   490     unfolding le_filter_def eventually_Sup by simp }
   491   { assume "F'' \<in> S" thus "Inf S \<le> F''"
   492     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   493   { assume "\<And>F'. F' \<in> S \<Longrightarrow> F \<le> F'" thus "F \<le> Inf S"
   494     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   495 qed
   496 
   497 end
   498 
   499 lemma filter_leD:
   500   "F \<le> F' \<Longrightarrow> eventually P F' \<Longrightarrow> eventually P F"
   501   unfolding le_filter_def by simp
   502 
   503 lemma filter_leI:
   504   "(\<And>P. eventually P F' \<Longrightarrow> eventually P F) \<Longrightarrow> F \<le> F'"
   505   unfolding le_filter_def by simp
   506 
   507 lemma eventually_False:
   508   "eventually (\<lambda>x. False) F \<longleftrightarrow> F = bot"
   509   unfolding filter_eq_iff by (auto elim: eventually_rev_mp)
   510 
   511 abbreviation (input) trivial_limit :: "'a filter \<Rightarrow> bool"
   512   where "trivial_limit F \<equiv> F = bot"
   513 
   514 lemma trivial_limit_def: "trivial_limit F \<longleftrightarrow> eventually (\<lambda>x. False) F"
   515   by (rule eventually_False [symmetric])
   516 
   517 lemma eventually_const: "\<not> trivial_limit net \<Longrightarrow> eventually (\<lambda>x. P) net \<longleftrightarrow> P"
   518   by (cases P) (simp_all add: eventually_False)
   519 
   520 
   521 subsubsection {* Map function for filters *}
   522 
   523 definition filtermap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a filter \<Rightarrow> 'b filter"
   524   where "filtermap f F = Abs_filter (\<lambda>P. eventually (\<lambda>x. P (f x)) F)"
   525 
   526 lemma eventually_filtermap:
   527   "eventually P (filtermap f F) = eventually (\<lambda>x. P (f x)) F"
   528   unfolding filtermap_def
   529   apply (rule eventually_Abs_filter)
   530   apply (rule is_filter.intro)
   531   apply (auto elim!: eventually_rev_mp)
   532   done
   533 
   534 lemma filtermap_ident: "filtermap (\<lambda>x. x) F = F"
   535   by (simp add: filter_eq_iff eventually_filtermap)
   536 
   537 lemma filtermap_filtermap:
   538   "filtermap f (filtermap g F) = filtermap (\<lambda>x. f (g x)) F"
   539   by (simp add: filter_eq_iff eventually_filtermap)
   540 
   541 lemma filtermap_mono: "F \<le> F' \<Longrightarrow> filtermap f F \<le> filtermap f F'"
   542   unfolding le_filter_def eventually_filtermap by simp
   543 
   544 lemma filtermap_bot [simp]: "filtermap f bot = bot"
   545   by (simp add: filter_eq_iff eventually_filtermap)
   546 
   547 lemma filtermap_sup: "filtermap f (sup F1 F2) = sup (filtermap f F1) (filtermap f F2)"
   548   by (auto simp: filter_eq_iff eventually_filtermap eventually_sup)
   549 
   550 subsubsection {* Order filters *}
   551 
   552 definition at_top :: "('a::order) filter"
   553   where "at_top = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   554 
   555 lemma eventually_at_top_linorder: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::linorder. \<forall>n\<ge>N. P n)"
   556   unfolding at_top_def
   557 proof (rule eventually_Abs_filter, rule is_filter.intro)
   558   fix P Q :: "'a \<Rightarrow> bool"
   559   assume "\<exists>i. \<forall>n\<ge>i. P n" and "\<exists>j. \<forall>n\<ge>j. Q n"
   560   then obtain i j where "\<forall>n\<ge>i. P n" and "\<forall>n\<ge>j. Q n" by auto
   561   then have "\<forall>n\<ge>max i j. P n \<and> Q n" by simp
   562   then show "\<exists>k. \<forall>n\<ge>k. P n \<and> Q n" ..
   563 qed auto
   564 
   565 lemma eventually_ge_at_top:
   566   "eventually (\<lambda>x. (c::_::linorder) \<le> x) at_top"
   567   unfolding eventually_at_top_linorder by auto
   568 
   569 lemma eventually_at_top_dense: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::dense_linorder. \<forall>n>N. P n)"
   570   unfolding eventually_at_top_linorder
   571 proof safe
   572   fix N assume "\<forall>n\<ge>N. P n" then show "\<exists>N. \<forall>n>N. P n" by (auto intro!: exI[of _ N])
   573 next
   574   fix N assume "\<forall>n>N. P n"
   575   moreover from gt_ex[of N] guess y ..
   576   ultimately show "\<exists>N. \<forall>n\<ge>N. P n" by (auto intro!: exI[of _ y])
   577 qed
   578 
   579 lemma eventually_gt_at_top:
   580   "eventually (\<lambda>x. (c::_::dense_linorder) < x) at_top"
   581   unfolding eventually_at_top_dense by auto
   582 
   583 definition at_bot :: "('a::order) filter"
   584   where "at_bot = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<le>k. P n)"
   585 
   586 lemma eventually_at_bot_linorder:
   587   fixes P :: "'a::linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n\<le>N. P n)"
   588   unfolding at_bot_def
   589 proof (rule eventually_Abs_filter, rule is_filter.intro)
   590   fix P Q :: "'a \<Rightarrow> bool"
   591   assume "\<exists>i. \<forall>n\<le>i. P n" and "\<exists>j. \<forall>n\<le>j. Q n"
   592   then obtain i j where "\<forall>n\<le>i. P n" and "\<forall>n\<le>j. Q n" by auto
   593   then have "\<forall>n\<le>min i j. P n \<and> Q n" by simp
   594   then show "\<exists>k. \<forall>n\<le>k. P n \<and> Q n" ..
   595 qed auto
   596 
   597 lemma eventually_le_at_bot:
   598   "eventually (\<lambda>x. x \<le> (c::_::linorder)) at_bot"
   599   unfolding eventually_at_bot_linorder by auto
   600 
   601 lemma eventually_at_bot_dense:
   602   fixes P :: "'a::dense_linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n<N. P n)"
   603   unfolding eventually_at_bot_linorder
   604 proof safe
   605   fix N assume "\<forall>n\<le>N. P n" then show "\<exists>N. \<forall>n<N. P n" by (auto intro!: exI[of _ N])
   606 next
   607   fix N assume "\<forall>n<N. P n" 
   608   moreover from lt_ex[of N] guess y ..
   609   ultimately show "\<exists>N. \<forall>n\<le>N. P n" by (auto intro!: exI[of _ y])
   610 qed
   611 
   612 lemma eventually_gt_at_bot:
   613   "eventually (\<lambda>x. x < (c::_::dense_linorder)) at_bot"
   614   unfolding eventually_at_bot_dense by auto
   615 
   616 subsection {* Sequentially *}
   617 
   618 abbreviation sequentially :: "nat filter"
   619   where "sequentially == at_top"
   620 
   621 lemma sequentially_def: "sequentially = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   622   unfolding at_top_def by simp
   623 
   624 lemma eventually_sequentially:
   625   "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
   626   by (rule eventually_at_top_linorder)
   627 
   628 lemma sequentially_bot [simp, intro]: "sequentially \<noteq> bot"
   629   unfolding filter_eq_iff eventually_sequentially by auto
   630 
   631 lemmas trivial_limit_sequentially = sequentially_bot
   632 
   633 lemma eventually_False_sequentially [simp]:
   634   "\<not> eventually (\<lambda>n. False) sequentially"
   635   by (simp add: eventually_False)
   636 
   637 lemma le_sequentially:
   638   "F \<le> sequentially \<longleftrightarrow> (\<forall>N. eventually (\<lambda>n. N \<le> n) F)"
   639   unfolding le_filter_def eventually_sequentially
   640   by (safe, fast, drule_tac x=N in spec, auto elim: eventually_rev_mp)
   641 
   642 lemma eventually_sequentiallyI:
   643   assumes "\<And>x. c \<le> x \<Longrightarrow> P x"
   644   shows "eventually P sequentially"
   645 using assms by (auto simp: eventually_sequentially)
   646 
   647 lemma eventually_sequentially_seg:
   648   "eventually (\<lambda>n. P (n + k)) sequentially \<longleftrightarrow> eventually P sequentially"
   649   unfolding eventually_sequentially
   650   apply safe
   651    apply (rule_tac x="N + k" in exI)
   652    apply rule
   653    apply (erule_tac x="n - k" in allE)
   654    apply auto []
   655   apply (rule_tac x=N in exI)
   656   apply auto []
   657   done
   658 
   659 subsubsection {* Standard filters *}
   660 
   661 definition within :: "'a filter \<Rightarrow> 'a set \<Rightarrow> 'a filter" (infixr "within" 70)
   662   where "F within S = Abs_filter (\<lambda>P. eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F)"
   663 
   664 lemma eventually_within:
   665   "eventually P (F within S) = eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F"
   666   unfolding within_def
   667   by (rule eventually_Abs_filter, rule is_filter.intro)
   668      (auto elim!: eventually_rev_mp)
   669 
   670 lemma within_UNIV [simp]: "F within UNIV = F"
   671   unfolding filter_eq_iff eventually_within by simp
   672 
   673 lemma within_empty [simp]: "F within {} = bot"
   674   unfolding filter_eq_iff eventually_within by simp
   675 
   676 lemma within_within_eq: "(F within S) within T = F within (S \<inter> T)"
   677   by (auto simp: filter_eq_iff eventually_within elim: eventually_elim1)
   678 
   679 lemma within_le: "F within S \<le> F"
   680   unfolding le_filter_def eventually_within by (auto elim: eventually_elim1)
   681 
   682 lemma le_withinI: "F \<le> F' \<Longrightarrow> eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S"
   683   unfolding le_filter_def eventually_within by (auto elim: eventually_elim2)
   684 
   685 lemma le_within_iff: "eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S \<longleftrightarrow> F \<le> F'"
   686   by (blast intro: within_le le_withinI order_trans)
   687 
   688 subsubsection {* Topological filters *}
   689 
   690 definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
   691   where "nhds a = Abs_filter (\<lambda>P. \<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   692 
   693 definition (in topological_space) at :: "'a \<Rightarrow> 'a filter"
   694   where "at a = nhds a within - {a}"
   695 
   696 abbreviation (in order_topology) at_right :: "'a \<Rightarrow> 'a filter" where
   697   "at_right x \<equiv> at x within {x <..}"
   698 
   699 abbreviation (in order_topology) at_left :: "'a \<Rightarrow> 'a filter" where
   700   "at_left x \<equiv> at x within {..< x}"
   701 
   702 lemma (in topological_space) eventually_nhds:
   703   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   704   unfolding nhds_def
   705 proof (rule eventually_Abs_filter, rule is_filter.intro)
   706   have "open UNIV \<and> a \<in> UNIV \<and> (\<forall>x\<in>UNIV. True)" by simp
   707   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. True)" ..
   708 next
   709   fix P Q
   710   assume "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   711      and "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)"
   712   then obtain S T where
   713     "open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   714     "open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)" by auto
   715   hence "open (S \<inter> T) \<and> a \<in> S \<inter> T \<and> (\<forall>x\<in>(S \<inter> T). P x \<and> Q x)"
   716     by (simp add: open_Int)
   717   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x \<and> Q x)" ..
   718 qed auto
   719 
   720 lemma nhds_neq_bot [simp]: "nhds a \<noteq> bot"
   721   unfolding trivial_limit_def eventually_nhds by simp
   722 
   723 lemma eventually_at_topological:
   724   "eventually P (at a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
   725 unfolding at_def eventually_within eventually_nhds by simp
   726 
   727 lemma at_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> at a within S = at a"
   728   unfolding filter_eq_iff eventually_within eventually_at_topological by (metis open_Int Int_iff)
   729 
   730 lemma at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
   731   unfolding trivial_limit_def eventually_at_topological
   732   by (safe, case_tac "S = {a}", simp, fast, fast)
   733 
   734 lemma at_neq_bot [simp]: "at (a::'a::perfect_space) \<noteq> bot"
   735   by (simp add: at_eq_bot_iff not_open_singleton)
   736 
   737 lemma eventually_at_right:
   738   fixes x :: "'a :: {no_top, linorder_topology}"
   739   shows "eventually P (at_right x) \<longleftrightarrow> (\<exists>b. x < b \<and> (\<forall>z. x < z \<and> z < b \<longrightarrow> P z))"
   740   unfolding eventually_nhds eventually_within at_def
   741 proof safe
   742   from gt_ex[of x] guess y ..
   743   moreover fix S assume "open S" "x \<in> S" note open_right[OF this, of y]
   744   moreover note gt_ex[of x]
   745   moreover assume "\<forall>s\<in>S. s \<in> - {x} \<longrightarrow> s \<in> {x<..} \<longrightarrow> P s"
   746   ultimately show "\<exists>b>x. \<forall>z. x < z \<and> z < b \<longrightarrow> P z"
   747     by (auto simp: subset_eq Ball_def)
   748 next
   749   fix b assume "x < b" "\<forall>z. x < z \<and> z < b \<longrightarrow> P z"
   750   then show "\<exists>S. open S \<and> x \<in> S \<and> (\<forall>xa\<in>S. xa \<in> - {x} \<longrightarrow> xa \<in> {x<..} \<longrightarrow> P xa)"
   751     by (intro exI[of _ "{..< b}"]) auto
   752 qed
   753 
   754 lemma eventually_at_left:
   755   fixes x :: "'a :: {no_bot, linorder_topology}"
   756   shows "eventually P (at_left x) \<longleftrightarrow> (\<exists>b. x > b \<and> (\<forall>z. b < z \<and> z < x \<longrightarrow> P z))"
   757   unfolding eventually_nhds eventually_within at_def
   758 proof safe
   759   from lt_ex[of x] guess y ..
   760   moreover fix S assume "open S" "x \<in> S" note open_left[OF this, of y]
   761   moreover assume "\<forall>s\<in>S. s \<in> - {x} \<longrightarrow> s \<in> {..<x} \<longrightarrow> P s"
   762   ultimately show "\<exists>b<x. \<forall>z. b < z \<and> z < x \<longrightarrow> P z"
   763     by (auto simp: subset_eq Ball_def)
   764 next
   765   fix b assume "b < x" "\<forall>z. b < z \<and> z < x \<longrightarrow> P z"
   766   then show "\<exists>S. open S \<and> x \<in> S \<and> (\<forall>xa\<in>S. xa \<in> - {x} \<longrightarrow> xa \<in> {..<x} \<longrightarrow> P xa)"
   767     by (intro exI[of _ "{b <..}"]) auto
   768 qed
   769 
   770 lemma trivial_limit_at_left_real [simp]:
   771   "\<not> trivial_limit (at_left (x::'a::{no_bot, dense_linorder, linorder_topology}))"
   772   unfolding trivial_limit_def eventually_at_left by (auto dest: dense)
   773 
   774 lemma trivial_limit_at_right_real [simp]:
   775   "\<not> trivial_limit (at_right (x::'a::{no_top, dense_linorder, linorder_topology}))"
   776   unfolding trivial_limit_def eventually_at_right by (auto dest: dense)
   777 
   778 lemma at_within_eq: "at x within T = nhds x within (T - {x})"
   779   unfolding at_def within_within_eq by (simp add: ac_simps Diff_eq)
   780 
   781 lemma at_eq_sup_left_right: "at (x::'a::linorder_topology) = sup (at_left x) (at_right x)"
   782   by (auto simp: eventually_within at_def filter_eq_iff eventually_sup 
   783            elim: eventually_elim2 eventually_elim1)
   784 
   785 lemma eventually_at_split:
   786   "eventually P (at (x::'a::linorder_topology)) \<longleftrightarrow> eventually P (at_left x) \<and> eventually P (at_right x)"
   787   by (subst at_eq_sup_left_right) (simp add: eventually_sup)
   788 
   789 subsection {* Limits *}
   790 
   791 definition filterlim :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b filter \<Rightarrow> 'a filter \<Rightarrow> bool" where
   792   "filterlim f F2 F1 \<longleftrightarrow> filtermap f F1 \<le> F2"
   793 
   794 syntax
   795   "_LIM" :: "pttrns \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> bool" ("(3LIM (_)/ (_)./ (_) :> (_))" [1000, 10, 0, 10] 10)
   796 
   797 translations
   798   "LIM x F1. f :> F2"   == "CONST filterlim (%x. f) F2 F1"
   799 
   800 lemma filterlim_iff:
   801   "(LIM x F1. f x :> F2) \<longleftrightarrow> (\<forall>P. eventually P F2 \<longrightarrow> eventually (\<lambda>x. P (f x)) F1)"
   802   unfolding filterlim_def le_filter_def eventually_filtermap ..
   803 
   804 lemma filterlim_compose:
   805   "filterlim g F3 F2 \<Longrightarrow> filterlim f F2 F1 \<Longrightarrow> filterlim (\<lambda>x. g (f x)) F3 F1"
   806   unfolding filterlim_def filtermap_filtermap[symmetric] by (metis filtermap_mono order_trans)
   807 
   808 lemma filterlim_mono:
   809   "filterlim f F2 F1 \<Longrightarrow> F2 \<le> F2' \<Longrightarrow> F1' \<le> F1 \<Longrightarrow> filterlim f F2' F1'"
   810   unfolding filterlim_def by (metis filtermap_mono order_trans)
   811 
   812 lemma filterlim_ident: "LIM x F. x :> F"
   813   by (simp add: filterlim_def filtermap_ident)
   814 
   815 lemma filterlim_cong:
   816   "F1 = F1' \<Longrightarrow> F2 = F2' \<Longrightarrow> eventually (\<lambda>x. f x = g x) F2 \<Longrightarrow> filterlim f F1 F2 = filterlim g F1' F2'"
   817   by (auto simp: filterlim_def le_filter_def eventually_filtermap elim: eventually_elim2)
   818 
   819 lemma filterlim_within:
   820   "(LIM x F1. f x :> F2 within S) \<longleftrightarrow> (eventually (\<lambda>x. f x \<in> S) F1 \<and> (LIM x F1. f x :> F2))"
   821   unfolding filterlim_def
   822 proof safe
   823   assume "filtermap f F1 \<le> F2 within S" then show "eventually (\<lambda>x. f x \<in> S) F1"
   824     by (auto simp: le_filter_def eventually_filtermap eventually_within elim!: allE[of _ "\<lambda>x. x \<in> S"])
   825 qed (auto intro: within_le order_trans simp: le_within_iff eventually_filtermap)
   826 
   827 lemma filterlim_filtermap: "filterlim f F1 (filtermap g F2) = filterlim (\<lambda>x. f (g x)) F1 F2"
   828   unfolding filterlim_def filtermap_filtermap ..
   829 
   830 lemma filterlim_sup:
   831   "filterlim f F F1 \<Longrightarrow> filterlim f F F2 \<Longrightarrow> filterlim f F (sup F1 F2)"
   832   unfolding filterlim_def filtermap_sup by auto
   833 
   834 lemma filterlim_Suc: "filterlim Suc sequentially sequentially"
   835   by (simp add: filterlim_iff eventually_sequentially) (metis le_Suc_eq)
   836 
   837 subsubsection {* Tendsto *}
   838 
   839 abbreviation (in topological_space)
   840   tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool" (infixr "--->" 55) where
   841   "(f ---> l) F \<equiv> filterlim f (nhds l) F"
   842 
   843 definition (in t2_space) Lim :: "'f filter \<Rightarrow> ('f \<Rightarrow> 'a) \<Rightarrow> 'a" where
   844   "Lim A f = (THE l. (f ---> l) A)"
   845 
   846 lemma tendsto_eq_rhs: "(f ---> x) F \<Longrightarrow> x = y \<Longrightarrow> (f ---> y) F"
   847   by simp
   848 
   849 ML {*
   850 
   851 structure Tendsto_Intros = Named_Thms
   852 (
   853   val name = @{binding tendsto_intros}
   854   val description = "introduction rules for tendsto"
   855 )
   856 
   857 *}
   858 
   859 setup {*
   860   Tendsto_Intros.setup #>
   861   Global_Theory.add_thms_dynamic (@{binding tendsto_eq_intros},
   862     map (fn thm => @{thm tendsto_eq_rhs} OF [thm]) o Tendsto_Intros.get o Context.proof_of);
   863 *}
   864 
   865 lemma (in topological_space) tendsto_def:
   866    "(f ---> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
   867   unfolding filterlim_def
   868 proof safe
   869   fix S assume "open S" "l \<in> S" "filtermap f F \<le> nhds l"
   870   then show "eventually (\<lambda>x. f x \<in> S) F"
   871     unfolding eventually_nhds eventually_filtermap le_filter_def
   872     by (auto elim!: allE[of _ "\<lambda>x. x \<in> S"] eventually_rev_mp)
   873 qed (auto elim!: eventually_rev_mp simp: eventually_nhds eventually_filtermap le_filter_def)
   874 
   875 lemma tendsto_within_subset: "(f ---> l) (net within S) \<Longrightarrow> T \<subseteq> S \<Longrightarrow> (f ---> l) (net within T)"
   876   by (auto simp: tendsto_def eventually_within elim!: eventually_elim1)
   877 
   878 lemma filterlim_at:
   879   "(LIM x F. f x :> at b) \<longleftrightarrow> (eventually (\<lambda>x. f x \<noteq> b) F \<and> (f ---> b) F)"
   880   by (simp add: at_def filterlim_within)
   881 
   882 lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f ---> l) F' \<Longrightarrow> (f ---> l) F"
   883   unfolding tendsto_def le_filter_def by fast
   884 
   885 lemma (in topological_space) topological_tendstoI:
   886   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F)
   887     \<Longrightarrow> (f ---> l) F"
   888   unfolding tendsto_def by auto
   889 
   890 lemma (in topological_space) topological_tendstoD:
   891   "(f ---> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
   892   unfolding tendsto_def by auto
   893 
   894 lemma order_tendstoI:
   895   fixes y :: "_ :: order_topology"
   896   assumes "\<And>a. a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F"
   897   assumes "\<And>a. y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F"
   898   shows "(f ---> y) F"
   899 proof (rule topological_tendstoI)
   900   fix S assume "open S" "y \<in> S"
   901   then show "eventually (\<lambda>x. f x \<in> S) F"
   902     unfolding open_generated_order
   903   proof induct
   904     case (UN K)
   905     then obtain k where "y \<in> k" "k \<in> K" by auto
   906     with UN(2)[of k] show ?case
   907       by (auto elim: eventually_elim1)
   908   qed (insert assms, auto elim: eventually_elim2)
   909 qed
   910 
   911 lemma order_tendstoD:
   912   fixes y :: "_ :: order_topology"
   913   assumes y: "(f ---> y) F"
   914   shows "a < y \<Longrightarrow> eventually (\<lambda>x. a < f x) F"
   915     and "y < a \<Longrightarrow> eventually (\<lambda>x. f x < a) F"
   916   using topological_tendstoD[OF y, of "{..< a}"] topological_tendstoD[OF y, of "{a <..}"] by auto
   917 
   918 lemma order_tendsto_iff: 
   919   fixes f :: "_ \<Rightarrow> 'a :: order_topology"
   920   shows "(f ---> x) F \<longleftrightarrow>(\<forall>l<x. eventually (\<lambda>x. l < f x) F) \<and> (\<forall>u>x. eventually (\<lambda>x. f x < u) F)"
   921   by (metis order_tendstoI order_tendstoD)
   922 
   923 lemma tendsto_bot [simp]: "(f ---> a) bot"
   924   unfolding tendsto_def by simp
   925 
   926 lemma tendsto_ident_at [tendsto_intros]: "((\<lambda>x. x) ---> a) (at a)"
   927   unfolding tendsto_def eventually_at_topological by auto
   928 
   929 lemma tendsto_ident_at_within [tendsto_intros]:
   930   "((\<lambda>x. x) ---> a) (at a within S)"
   931   unfolding tendsto_def eventually_within eventually_at_topological by auto
   932 
   933 lemma (in topological_space) tendsto_const [tendsto_intros]: "((\<lambda>x. k) ---> k) F"
   934   by (simp add: tendsto_def)
   935 
   936 lemma (in t2_space) tendsto_unique:
   937   assumes "\<not> trivial_limit F" and "(f ---> a) F" and "(f ---> b) F"
   938   shows "a = b"
   939 proof (rule ccontr)
   940   assume "a \<noteq> b"
   941   obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
   942     using hausdorff [OF `a \<noteq> b`] by fast
   943   have "eventually (\<lambda>x. f x \<in> U) F"
   944     using `(f ---> a) F` `open U` `a \<in> U` by (rule topological_tendstoD)
   945   moreover
   946   have "eventually (\<lambda>x. f x \<in> V) F"
   947     using `(f ---> b) F` `open V` `b \<in> V` by (rule topological_tendstoD)
   948   ultimately
   949   have "eventually (\<lambda>x. False) F"
   950   proof eventually_elim
   951     case (elim x)
   952     hence "f x \<in> U \<inter> V" by simp
   953     with `U \<inter> V = {}` show ?case by simp
   954   qed
   955   with `\<not> trivial_limit F` show "False"
   956     by (simp add: trivial_limit_def)
   957 qed
   958 
   959 lemma (in t2_space) tendsto_const_iff:
   960   assumes "\<not> trivial_limit F" shows "((\<lambda>x. a :: 'a) ---> b) F \<longleftrightarrow> a = b"
   961   by (safe intro!: tendsto_const tendsto_unique [OF assms tendsto_const])
   962 
   963 lemma increasing_tendsto:
   964   fixes f :: "_ \<Rightarrow> 'a::order_topology"
   965   assumes bdd: "eventually (\<lambda>n. f n \<le> l) F"
   966       and en: "\<And>x. x < l \<Longrightarrow> eventually (\<lambda>n. x < f n) F"
   967   shows "(f ---> l) F"
   968   using assms by (intro order_tendstoI) (auto elim!: eventually_elim1)
   969 
   970 lemma decreasing_tendsto:
   971   fixes f :: "_ \<Rightarrow> 'a::order_topology"
   972   assumes bdd: "eventually (\<lambda>n. l \<le> f n) F"
   973       and en: "\<And>x. l < x \<Longrightarrow> eventually (\<lambda>n. f n < x) F"
   974   shows "(f ---> l) F"
   975   using assms by (intro order_tendstoI) (auto elim!: eventually_elim1)
   976 
   977 lemma tendsto_sandwich:
   978   fixes f g h :: "'a \<Rightarrow> 'b::order_topology"
   979   assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
   980   assumes lim: "(f ---> c) net" "(h ---> c) net"
   981   shows "(g ---> c) net"
   982 proof (rule order_tendstoI)
   983   fix a show "a < c \<Longrightarrow> eventually (\<lambda>x. a < g x) net"
   984     using order_tendstoD[OF lim(1), of a] ev by (auto elim: eventually_elim2)
   985 next
   986   fix a show "c < a \<Longrightarrow> eventually (\<lambda>x. g x < a) net"
   987     using order_tendstoD[OF lim(2), of a] ev by (auto elim: eventually_elim2)
   988 qed
   989 
   990 lemma tendsto_le:
   991   fixes f g :: "'a \<Rightarrow> 'b::linorder_topology"
   992   assumes F: "\<not> trivial_limit F"
   993   assumes x: "(f ---> x) F" and y: "(g ---> y) F"
   994   assumes ev: "eventually (\<lambda>x. g x \<le> f x) F"
   995   shows "y \<le> x"
   996 proof (rule ccontr)
   997   assume "\<not> y \<le> x"
   998   with less_separate[of x y] obtain a b where xy: "x < a" "b < y" "{..<a} \<inter> {b<..} = {}"
   999     by (auto simp: not_le)
  1000   then have "eventually (\<lambda>x. f x < a) F" "eventually (\<lambda>x. b < g x) F"
  1001     using x y by (auto intro: order_tendstoD)
  1002   with ev have "eventually (\<lambda>x. False) F"
  1003     by eventually_elim (insert xy, fastforce)
  1004   with F show False
  1005     by (simp add: eventually_False)
  1006 qed
  1007 
  1008 lemma tendsto_le_const:
  1009   fixes f :: "'a \<Rightarrow> 'b::linorder_topology"
  1010   assumes F: "\<not> trivial_limit F"
  1011   assumes x: "(f ---> x) F" and a: "eventually (\<lambda>x. a \<le> f x) F"
  1012   shows "a \<le> x"
  1013   using F x tendsto_const a by (rule tendsto_le)
  1014 
  1015 subsubsection {* Rules about @{const Lim} *}
  1016 
  1017 lemma (in t2_space) tendsto_Lim:
  1018   "\<not>(trivial_limit net) \<Longrightarrow> (f ---> l) net \<Longrightarrow> Lim net f = l"
  1019   unfolding Lim_def using tendsto_unique[of net f] by auto
  1020 
  1021 lemma Lim_ident_at: "\<not> trivial_limit (at x) \<Longrightarrow> Lim (at x) (\<lambda>x. x) = x"
  1022   by (rule tendsto_Lim[OF _ tendsto_ident_at]) auto
  1023 
  1024 lemma Lim_ident_at_within: "\<not> trivial_limit (at x within s) \<Longrightarrow> Lim (at x within s) (\<lambda>x. x) = x"
  1025   by (rule tendsto_Lim[OF _ tendsto_ident_at_within]) auto
  1026 
  1027 subsection {* Limits to @{const at_top} and @{const at_bot} *}
  1028 
  1029 lemma filterlim_at_top:
  1030   fixes f :: "'a \<Rightarrow> ('b::linorder)"
  1031   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z \<le> f x) F)"
  1032   by (auto simp: filterlim_iff eventually_at_top_linorder elim!: eventually_elim1)
  1033 
  1034 lemma filterlim_at_top_dense:
  1035   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)"
  1036   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z < f x) F)"
  1037   by (metis eventually_elim1[of _ F] eventually_gt_at_top order_less_imp_le
  1038             filterlim_at_top[of f F] filterlim_iff[of f at_top F])
  1039 
  1040 lemma filterlim_at_top_ge:
  1041   fixes f :: "'a \<Rightarrow> ('b::linorder)" and c :: "'b"
  1042   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z\<ge>c. eventually (\<lambda>x. Z \<le> f x) F)"
  1043   unfolding filterlim_at_top
  1044 proof safe
  1045   fix Z assume *: "\<forall>Z\<ge>c. eventually (\<lambda>x. Z \<le> f x) F"
  1046   with *[THEN spec, of "max Z c"] show "eventually (\<lambda>x. Z \<le> f x) F"
  1047     by (auto elim!: eventually_elim1)
  1048 qed simp
  1049 
  1050 lemma filterlim_at_top_at_top:
  1051   fixes f :: "'a::linorder \<Rightarrow> 'b::linorder"
  1052   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
  1053   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
  1054   assumes Q: "eventually Q at_top"
  1055   assumes P: "eventually P at_top"
  1056   shows "filterlim f at_top at_top"
  1057 proof -
  1058   from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
  1059     unfolding eventually_at_top_linorder by auto
  1060   show ?thesis
  1061   proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
  1062     fix z assume "x \<le> z"
  1063     with x have "P z" by auto
  1064     have "eventually (\<lambda>x. g z \<le> x) at_top"
  1065       by (rule eventually_ge_at_top)
  1066     with Q show "eventually (\<lambda>x. z \<le> f x) at_top"
  1067       by eventually_elim (metis mono bij `P z`)
  1068   qed
  1069 qed
  1070 
  1071 lemma filterlim_at_top_gt:
  1072   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
  1073   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z>c. eventually (\<lambda>x. Z \<le> f x) F)"
  1074   by (metis filterlim_at_top order_less_le_trans gt_ex filterlim_at_top_ge)
  1075 
  1076 lemma filterlim_at_bot: 
  1077   fixes f :: "'a \<Rightarrow> ('b::linorder)"
  1078   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. f x \<le> Z) F)"
  1079   by (auto simp: filterlim_iff eventually_at_bot_linorder elim!: eventually_elim1)
  1080 
  1081 lemma filterlim_at_bot_le:
  1082   fixes f :: "'a \<Rightarrow> ('b::linorder)" and c :: "'b"
  1083   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z\<le>c. eventually (\<lambda>x. Z \<ge> f x) F)"
  1084   unfolding filterlim_at_bot
  1085 proof safe
  1086   fix Z assume *: "\<forall>Z\<le>c. eventually (\<lambda>x. Z \<ge> f x) F"
  1087   with *[THEN spec, of "min Z c"] show "eventually (\<lambda>x. Z \<ge> f x) F"
  1088     by (auto elim!: eventually_elim1)
  1089 qed simp
  1090 
  1091 lemma filterlim_at_bot_lt:
  1092   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
  1093   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z<c. eventually (\<lambda>x. Z \<ge> f x) F)"
  1094   by (metis filterlim_at_bot filterlim_at_bot_le lt_ex order_le_less_trans)
  1095 
  1096 lemma filterlim_at_bot_at_right:
  1097   fixes f :: "'a::{no_top, linorder_topology} \<Rightarrow> 'b::linorder"
  1098   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
  1099   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
  1100   assumes Q: "eventually Q (at_right a)" and bound: "\<And>b. Q b \<Longrightarrow> a < b"
  1101   assumes P: "eventually P at_bot"
  1102   shows "filterlim f at_bot (at_right a)"
  1103 proof -
  1104   from P obtain x where x: "\<And>y. y \<le> x \<Longrightarrow> P y"
  1105     unfolding eventually_at_bot_linorder by auto
  1106   show ?thesis
  1107   proof (intro filterlim_at_bot_le[THEN iffD2] allI impI)
  1108     fix z assume "z \<le> x"
  1109     with x have "P z" by auto
  1110     have "eventually (\<lambda>x. x \<le> g z) (at_right a)"
  1111       using bound[OF bij(2)[OF `P z`]]
  1112       unfolding eventually_at_right by (auto intro!: exI[of _ "g z"])
  1113     with Q show "eventually (\<lambda>x. f x \<le> z) (at_right a)"
  1114       by eventually_elim (metis bij `P z` mono)
  1115   qed
  1116 qed
  1117 
  1118 lemma filterlim_at_top_at_left:
  1119   fixes f :: "'a::{no_bot, linorder_topology} \<Rightarrow> 'b::linorder"
  1120   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
  1121   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
  1122   assumes Q: "eventually Q (at_left a)" and bound: "\<And>b. Q b \<Longrightarrow> b < a"
  1123   assumes P: "eventually P at_top"
  1124   shows "filterlim f at_top (at_left a)"
  1125 proof -
  1126   from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
  1127     unfolding eventually_at_top_linorder by auto
  1128   show ?thesis
  1129   proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
  1130     fix z assume "x \<le> z"
  1131     with x have "P z" by auto
  1132     have "eventually (\<lambda>x. g z \<le> x) (at_left a)"
  1133       using bound[OF bij(2)[OF `P z`]]
  1134       unfolding eventually_at_left by (auto intro!: exI[of _ "g z"])
  1135     with Q show "eventually (\<lambda>x. z \<le> f x) (at_left a)"
  1136       by eventually_elim (metis bij `P z` mono)
  1137   qed
  1138 qed
  1139 
  1140 lemma filterlim_split_at:
  1141   "filterlim f F (at_left x) \<Longrightarrow> filterlim f F (at_right x) \<Longrightarrow> filterlim f F (at (x::'a::linorder_topology))"
  1142   by (subst at_eq_sup_left_right) (rule filterlim_sup)
  1143 
  1144 lemma filterlim_at_split:
  1145   "filterlim f F (at (x::'a::linorder_topology)) \<longleftrightarrow> filterlim f F (at_left x) \<and> filterlim f F (at_right x)"
  1146   by (subst at_eq_sup_left_right) (simp add: filterlim_def filtermap_sup)
  1147 
  1148 
  1149 subsection {* Limits on sequences *}
  1150 
  1151 abbreviation (in topological_space)
  1152   LIMSEQ :: "[nat \<Rightarrow> 'a, 'a] \<Rightarrow> bool"
  1153     ("((_)/ ----> (_))" [60, 60] 60) where
  1154   "X ----> L \<equiv> (X ---> L) sequentially"
  1155 
  1156 abbreviation (in t2_space) lim :: "(nat \<Rightarrow> 'a) \<Rightarrow> 'a" where
  1157   "lim X \<equiv> Lim sequentially X"
  1158 
  1159 definition (in topological_space) convergent :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
  1160   "convergent X = (\<exists>L. X ----> L)"
  1161 
  1162 lemma lim_def: "lim X = (THE L. X ----> L)"
  1163   unfolding Lim_def ..
  1164 
  1165 subsubsection {* Monotone sequences and subsequences *}
  1166 
  1167 definition
  1168   monoseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
  1169     --{*Definition of monotonicity.
  1170         The use of disjunction here complicates proofs considerably.
  1171         One alternative is to add a Boolean argument to indicate the direction.
  1172         Another is to develop the notions of increasing and decreasing first.*}
  1173   "monoseq X = ((\<forall>m. \<forall>n\<ge>m. X m \<le> X n) | (\<forall>m. \<forall>n\<ge>m. X n \<le> X m))"
  1174 
  1175 definition
  1176   incseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
  1177     --{*Increasing sequence*}
  1178   "incseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X m \<le> X n)"
  1179 
  1180 definition
  1181   decseq :: "(nat \<Rightarrow> 'a::order) \<Rightarrow> bool" where
  1182     --{*Decreasing sequence*}
  1183   "decseq X \<longleftrightarrow> (\<forall>m. \<forall>n\<ge>m. X n \<le> X m)"
  1184 
  1185 definition
  1186   subseq :: "(nat \<Rightarrow> nat) \<Rightarrow> bool" where
  1187     --{*Definition of subsequence*}
  1188   "subseq f \<longleftrightarrow> (\<forall>m. \<forall>n>m. f m < f n)"
  1189 
  1190 lemma incseq_mono: "mono f \<longleftrightarrow> incseq f"
  1191   unfolding mono_def incseq_def by auto
  1192 
  1193 lemma incseq_SucI:
  1194   "(\<And>n. X n \<le> X (Suc n)) \<Longrightarrow> incseq X"
  1195   using lift_Suc_mono_le[of X]
  1196   by (auto simp: incseq_def)
  1197 
  1198 lemma incseqD: "\<And>i j. incseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f i \<le> f j"
  1199   by (auto simp: incseq_def)
  1200 
  1201 lemma incseq_SucD: "incseq A \<Longrightarrow> A i \<le> A (Suc i)"
  1202   using incseqD[of A i "Suc i"] by auto
  1203 
  1204 lemma incseq_Suc_iff: "incseq f \<longleftrightarrow> (\<forall>n. f n \<le> f (Suc n))"
  1205   by (auto intro: incseq_SucI dest: incseq_SucD)
  1206 
  1207 lemma incseq_const[simp, intro]: "incseq (\<lambda>x. k)"
  1208   unfolding incseq_def by auto
  1209 
  1210 lemma decseq_SucI:
  1211   "(\<And>n. X (Suc n) \<le> X n) \<Longrightarrow> decseq X"
  1212   using order.lift_Suc_mono_le[OF dual_order, of X]
  1213   by (auto simp: decseq_def)
  1214 
  1215 lemma decseqD: "\<And>i j. decseq f \<Longrightarrow> i \<le> j \<Longrightarrow> f j \<le> f i"
  1216   by (auto simp: decseq_def)
  1217 
  1218 lemma decseq_SucD: "decseq A \<Longrightarrow> A (Suc i) \<le> A i"
  1219   using decseqD[of A i "Suc i"] by auto
  1220 
  1221 lemma decseq_Suc_iff: "decseq f \<longleftrightarrow> (\<forall>n. f (Suc n) \<le> f n)"
  1222   by (auto intro: decseq_SucI dest: decseq_SucD)
  1223 
  1224 lemma decseq_const[simp, intro]: "decseq (\<lambda>x. k)"
  1225   unfolding decseq_def by auto
  1226 
  1227 lemma monoseq_iff: "monoseq X \<longleftrightarrow> incseq X \<or> decseq X"
  1228   unfolding monoseq_def incseq_def decseq_def ..
  1229 
  1230 lemma monoseq_Suc:
  1231   "monoseq X \<longleftrightarrow> (\<forall>n. X n \<le> X (Suc n)) \<or> (\<forall>n. X (Suc n) \<le> X n)"
  1232   unfolding monoseq_iff incseq_Suc_iff decseq_Suc_iff ..
  1233 
  1234 lemma monoI1: "\<forall>m. \<forall> n \<ge> m. X m \<le> X n ==> monoseq X"
  1235 by (simp add: monoseq_def)
  1236 
  1237 lemma monoI2: "\<forall>m. \<forall> n \<ge> m. X n \<le> X m ==> monoseq X"
  1238 by (simp add: monoseq_def)
  1239 
  1240 lemma mono_SucI1: "\<forall>n. X n \<le> X (Suc n) ==> monoseq X"
  1241 by (simp add: monoseq_Suc)
  1242 
  1243 lemma mono_SucI2: "\<forall>n. X (Suc n) \<le> X n ==> monoseq X"
  1244 by (simp add: monoseq_Suc)
  1245 
  1246 lemma monoseq_minus:
  1247   fixes a :: "nat \<Rightarrow> 'a::ordered_ab_group_add"
  1248   assumes "monoseq a"
  1249   shows "monoseq (\<lambda> n. - a n)"
  1250 proof (cases "\<forall> m. \<forall> n \<ge> m. a m \<le> a n")
  1251   case True
  1252   hence "\<forall> m. \<forall> n \<ge> m. - a n \<le> - a m" by auto
  1253   thus ?thesis by (rule monoI2)
  1254 next
  1255   case False
  1256   hence "\<forall> m. \<forall> n \<ge> m. - a m \<le> - a n" using `monoseq a`[unfolded monoseq_def] by auto
  1257   thus ?thesis by (rule monoI1)
  1258 qed
  1259 
  1260 text{*Subsequence (alternative definition, (e.g. Hoskins)*}
  1261 
  1262 lemma subseq_Suc_iff: "subseq f = (\<forall>n. (f n) < (f (Suc n)))"
  1263 apply (simp add: subseq_def)
  1264 apply (auto dest!: less_imp_Suc_add)
  1265 apply (induct_tac k)
  1266 apply (auto intro: less_trans)
  1267 done
  1268 
  1269 text{* for any sequence, there is a monotonic subsequence *}
  1270 lemma seq_monosub:
  1271   fixes s :: "nat => 'a::linorder"
  1272   shows "\<exists>f. subseq f \<and> monoseq (\<lambda> n. (s (f n)))"
  1273 proof cases
  1274   let "?P p n" = "p > n \<and> (\<forall>m\<ge>p. s m \<le> s p)"
  1275   assume *: "\<forall>n. \<exists>p. ?P p n"
  1276   def f \<equiv> "nat_rec (SOME p. ?P p 0) (\<lambda>_ n. SOME p. ?P p n)"
  1277   have f_0: "f 0 = (SOME p. ?P p 0)" unfolding f_def by simp
  1278   have f_Suc: "\<And>i. f (Suc i) = (SOME p. ?P p (f i))" unfolding f_def nat_rec_Suc ..
  1279   have P_0: "?P (f 0) 0" unfolding f_0 using *[rule_format] by (rule someI2_ex) auto
  1280   have P_Suc: "\<And>i. ?P (f (Suc i)) (f i)" unfolding f_Suc using *[rule_format] by (rule someI2_ex) auto
  1281   then have "subseq f" unfolding subseq_Suc_iff by auto
  1282   moreover have "monoseq (\<lambda>n. s (f n))" unfolding monoseq_Suc
  1283   proof (intro disjI2 allI)
  1284     fix n show "s (f (Suc n)) \<le> s (f n)"
  1285     proof (cases n)
  1286       case 0 with P_Suc[of 0] P_0 show ?thesis by auto
  1287     next
  1288       case (Suc m)
  1289       from P_Suc[of n] Suc have "f (Suc m) \<le> f (Suc (Suc m))" by simp
  1290       with P_Suc Suc show ?thesis by simp
  1291     qed
  1292   qed
  1293   ultimately show ?thesis by auto
  1294 next
  1295   let "?P p m" = "m < p \<and> s m < s p"
  1296   assume "\<not> (\<forall>n. \<exists>p>n. (\<forall>m\<ge>p. s m \<le> s p))"
  1297   then obtain N where N: "\<And>p. p > N \<Longrightarrow> \<exists>m>p. s p < s m" by (force simp: not_le le_less)
  1298   def f \<equiv> "nat_rec (SOME p. ?P p (Suc N)) (\<lambda>_ n. SOME p. ?P p n)"
  1299   have f_0: "f 0 = (SOME p. ?P p (Suc N))" unfolding f_def by simp
  1300   have f_Suc: "\<And>i. f (Suc i) = (SOME p. ?P p (f i))" unfolding f_def nat_rec_Suc ..
  1301   have P_0: "?P (f 0) (Suc N)"
  1302     unfolding f_0 some_eq_ex[of "\<lambda>p. ?P p (Suc N)"] using N[of "Suc N"] by auto
  1303   { fix i have "N < f i \<Longrightarrow> ?P (f (Suc i)) (f i)"
  1304       unfolding f_Suc some_eq_ex[of "\<lambda>p. ?P p (f i)"] using N[of "f i"] . }
  1305   note P' = this
  1306   { fix i have "N < f i \<and> ?P (f (Suc i)) (f i)"
  1307       by (induct i) (insert P_0 P', auto) }
  1308   then have "subseq f" "monoseq (\<lambda>x. s (f x))"
  1309     unfolding subseq_Suc_iff monoseq_Suc by (auto simp: not_le intro: less_imp_le)
  1310   then show ?thesis by auto
  1311 qed
  1312 
  1313 lemma seq_suble: assumes sf: "subseq f" shows "n \<le> f n"
  1314 proof(induct n)
  1315   case 0 thus ?case by simp
  1316 next
  1317   case (Suc n)
  1318   from sf[unfolded subseq_Suc_iff, rule_format, of n] Suc.hyps
  1319   have "n < f (Suc n)" by arith
  1320   thus ?case by arith
  1321 qed
  1322 
  1323 lemma eventually_subseq:
  1324   "subseq r \<Longrightarrow> eventually P sequentially \<Longrightarrow> eventually (\<lambda>n. P (r n)) sequentially"
  1325   unfolding eventually_sequentially by (metis seq_suble le_trans)
  1326 
  1327 lemma not_eventually_sequentiallyD:
  1328   assumes P: "\<not> eventually P sequentially"
  1329   shows "\<exists>r. subseq r \<and> (\<forall>n. \<not> P (r n))"
  1330 proof -
  1331   from P have "\<forall>n. \<exists>m\<ge>n. \<not> P m"
  1332     unfolding eventually_sequentially by (simp add: not_less)
  1333   then obtain r where "\<And>n. r n \<ge> n" "\<And>n. \<not> P (r n)"
  1334     by (auto simp: choice_iff)
  1335   then show ?thesis
  1336     by (auto intro!: exI[of _ "\<lambda>n. r (((Suc \<circ> r) ^^ Suc n) 0)"]
  1337              simp: less_eq_Suc_le subseq_Suc_iff)
  1338 qed
  1339 
  1340 lemma filterlim_subseq: "subseq f \<Longrightarrow> filterlim f sequentially sequentially"
  1341   unfolding filterlim_iff by (metis eventually_subseq)
  1342 
  1343 lemma subseq_o: "subseq r \<Longrightarrow> subseq s \<Longrightarrow> subseq (r \<circ> s)"
  1344   unfolding subseq_def by simp
  1345 
  1346 lemma subseq_mono: assumes "subseq r" "m < n" shows "r m < r n"
  1347   using assms by (auto simp: subseq_def)
  1348 
  1349 lemma incseq_imp_monoseq:  "incseq X \<Longrightarrow> monoseq X"
  1350   by (simp add: incseq_def monoseq_def)
  1351 
  1352 lemma decseq_imp_monoseq:  "decseq X \<Longrightarrow> monoseq X"
  1353   by (simp add: decseq_def monoseq_def)
  1354 
  1355 lemma decseq_eq_incseq:
  1356   fixes X :: "nat \<Rightarrow> 'a::ordered_ab_group_add" shows "decseq X = incseq (\<lambda>n. - X n)" 
  1357   by (simp add: decseq_def incseq_def)
  1358 
  1359 lemma INT_decseq_offset:
  1360   assumes "decseq F"
  1361   shows "(\<Inter>i. F i) = (\<Inter>i\<in>{n..}. F i)"
  1362 proof safe
  1363   fix x i assume x: "x \<in> (\<Inter>i\<in>{n..}. F i)"
  1364   show "x \<in> F i"
  1365   proof cases
  1366     from x have "x \<in> F n" by auto
  1367     also assume "i \<le> n" with `decseq F` have "F n \<subseteq> F i"
  1368       unfolding decseq_def by simp
  1369     finally show ?thesis .
  1370   qed (insert x, simp)
  1371 qed auto
  1372 
  1373 lemma LIMSEQ_const_iff:
  1374   fixes k l :: "'a::t2_space"
  1375   shows "(\<lambda>n. k) ----> l \<longleftrightarrow> k = l"
  1376   using trivial_limit_sequentially by (rule tendsto_const_iff)
  1377 
  1378 lemma LIMSEQ_SUP:
  1379   "incseq X \<Longrightarrow> X ----> (SUP i. X i :: 'a :: {complete_linorder, linorder_topology})"
  1380   by (intro increasing_tendsto)
  1381      (auto simp: SUP_upper less_SUP_iff incseq_def eventually_sequentially intro: less_le_trans)
  1382 
  1383 lemma LIMSEQ_INF:
  1384   "decseq X \<Longrightarrow> X ----> (INF i. X i :: 'a :: {complete_linorder, linorder_topology})"
  1385   by (intro decreasing_tendsto)
  1386      (auto simp: INF_lower INF_less_iff decseq_def eventually_sequentially intro: le_less_trans)
  1387 
  1388 lemma LIMSEQ_ignore_initial_segment:
  1389   "f ----> a \<Longrightarrow> (\<lambda>n. f (n + k)) ----> a"
  1390   unfolding tendsto_def
  1391   by (subst eventually_sequentially_seg[where k=k])
  1392 
  1393 lemma LIMSEQ_offset:
  1394   "(\<lambda>n. f (n + k)) ----> a \<Longrightarrow> f ----> a"
  1395   unfolding tendsto_def
  1396   by (subst (asm) eventually_sequentially_seg[where k=k])
  1397 
  1398 lemma LIMSEQ_Suc: "f ----> l \<Longrightarrow> (\<lambda>n. f (Suc n)) ----> l"
  1399 by (drule_tac k="Suc 0" in LIMSEQ_ignore_initial_segment, simp)
  1400 
  1401 lemma LIMSEQ_imp_Suc: "(\<lambda>n. f (Suc n)) ----> l \<Longrightarrow> f ----> l"
  1402 by (rule_tac k="Suc 0" in LIMSEQ_offset, simp)
  1403 
  1404 lemma LIMSEQ_Suc_iff: "(\<lambda>n. f (Suc n)) ----> l = f ----> l"
  1405 by (blast intro: LIMSEQ_imp_Suc LIMSEQ_Suc)
  1406 
  1407 lemma LIMSEQ_unique:
  1408   fixes a b :: "'a::t2_space"
  1409   shows "\<lbrakk>X ----> a; X ----> b\<rbrakk> \<Longrightarrow> a = b"
  1410   using trivial_limit_sequentially by (rule tendsto_unique)
  1411 
  1412 lemma LIMSEQ_le_const:
  1413   "\<lbrakk>X ----> (x::'a::linorder_topology); \<exists>N. \<forall>n\<ge>N. a \<le> X n\<rbrakk> \<Longrightarrow> a \<le> x"
  1414   using tendsto_le_const[of sequentially X x a] by (simp add: eventually_sequentially)
  1415 
  1416 lemma LIMSEQ_le:
  1417   "\<lbrakk>X ----> x; Y ----> y; \<exists>N. \<forall>n\<ge>N. X n \<le> Y n\<rbrakk> \<Longrightarrow> x \<le> (y::'a::linorder_topology)"
  1418   using tendsto_le[of sequentially Y y X x] by (simp add: eventually_sequentially)
  1419 
  1420 lemma LIMSEQ_le_const2:
  1421   "\<lbrakk>X ----> (x::'a::linorder_topology); \<exists>N. \<forall>n\<ge>N. X n \<le> a\<rbrakk> \<Longrightarrow> x \<le> a"
  1422   by (rule LIMSEQ_le[of X x "\<lambda>n. a"]) (auto simp: tendsto_const)
  1423 
  1424 lemma convergentD: "convergent X ==> \<exists>L. (X ----> L)"
  1425 by (simp add: convergent_def)
  1426 
  1427 lemma convergentI: "(X ----> L) ==> convergent X"
  1428 by (auto simp add: convergent_def)
  1429 
  1430 lemma convergent_LIMSEQ_iff: "convergent X = (X ----> lim X)"
  1431 by (auto intro: theI LIMSEQ_unique simp add: convergent_def lim_def)
  1432 
  1433 lemma convergent_const: "convergent (\<lambda>n. c)"
  1434   by (rule convergentI, rule tendsto_const)
  1435 
  1436 lemma monoseq_le:
  1437   "monoseq a \<Longrightarrow> a ----> (x::'a::linorder_topology) \<Longrightarrow>
  1438     ((\<forall> n. a n \<le> x) \<and> (\<forall>m. \<forall>n\<ge>m. a m \<le> a n)) \<or> ((\<forall> n. x \<le> a n) \<and> (\<forall>m. \<forall>n\<ge>m. a n \<le> a m))"
  1439   by (metis LIMSEQ_le_const LIMSEQ_le_const2 decseq_def incseq_def monoseq_iff)
  1440 
  1441 lemma LIMSEQ_subseq_LIMSEQ:
  1442   "\<lbrakk> X ----> L; subseq f \<rbrakk> \<Longrightarrow> (X o f) ----> L"
  1443   unfolding comp_def by (rule filterlim_compose[of X, OF _ filterlim_subseq])
  1444 
  1445 lemma convergent_subseq_convergent:
  1446   "\<lbrakk>convergent X; subseq f\<rbrakk> \<Longrightarrow> convergent (X o f)"
  1447   unfolding convergent_def by (auto intro: LIMSEQ_subseq_LIMSEQ)
  1448 
  1449 lemma limI: "X ----> L ==> lim X = L"
  1450 apply (simp add: lim_def)
  1451 apply (blast intro: LIMSEQ_unique)
  1452 done
  1453 
  1454 lemma lim_le: "convergent f \<Longrightarrow> (\<And>n. f n \<le> (x::'a::linorder_topology)) \<Longrightarrow> lim f \<le> x"
  1455   using LIMSEQ_le_const2[of f "lim f" x] by (simp add: convergent_LIMSEQ_iff)
  1456 
  1457 subsubsection{*Increasing and Decreasing Series*}
  1458 
  1459 lemma incseq_le: "incseq X \<Longrightarrow> X ----> L \<Longrightarrow> X n \<le> (L::'a::linorder_topology)"
  1460   by (metis incseq_def LIMSEQ_le_const)
  1461 
  1462 lemma decseq_le: "decseq X \<Longrightarrow> X ----> L \<Longrightarrow> (L::'a::linorder_topology) \<le> X n"
  1463   by (metis decseq_def LIMSEQ_le_const2)
  1464 
  1465 subsection {* First countable topologies *}
  1466 
  1467 class first_countable_topology = topological_space +
  1468   assumes first_countable_basis:
  1469     "\<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))"
  1470 
  1471 lemma (in first_countable_topology) countable_basis_at_decseq:
  1472   obtains A :: "nat \<Rightarrow> 'a set" where
  1473     "\<And>i. open (A i)" "\<And>i. x \<in> (A i)"
  1474     "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially"
  1475 proof atomize_elim
  1476   from first_countable_basis[of x] obtain A :: "nat \<Rightarrow> 'a set" where
  1477     nhds: "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1478     and incl: "\<And>S. open S \<Longrightarrow> x \<in> S \<Longrightarrow> \<exists>i. A i \<subseteq> S"  by auto
  1479   def F \<equiv> "\<lambda>n. \<Inter>i\<le>n. A i"
  1480   show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and>
  1481       (\<forall>S. open S \<longrightarrow> x \<in> S \<longrightarrow> eventually (\<lambda>i. A i \<subseteq> S) sequentially)"
  1482   proof (safe intro!: exI[of _ F])
  1483     fix i
  1484     show "open (F i)" using nhds(1) by (auto simp: F_def)
  1485     show "x \<in> F i" using nhds(2) by (auto simp: F_def)
  1486   next
  1487     fix S assume "open S" "x \<in> S"
  1488     from incl[OF this] obtain i where "F i \<subseteq> S" unfolding F_def by auto
  1489     moreover have "\<And>j. i \<le> j \<Longrightarrow> F j \<subseteq> F i"
  1490       by (auto simp: F_def)
  1491     ultimately show "eventually (\<lambda>i. F i \<subseteq> S) sequentially"
  1492       by (auto simp: eventually_sequentially)
  1493   qed
  1494 qed
  1495 
  1496 lemma (in first_countable_topology) countable_basis:
  1497   obtains A :: "nat \<Rightarrow> 'a set" where
  1498     "\<And>i. open (A i)" "\<And>i. x \<in> A i"
  1499     "\<And>F. (\<forall>n. F n \<in> A n) \<Longrightarrow> F ----> x"
  1500 proof atomize_elim
  1501   from countable_basis_at_decseq[of x] guess A . note A = this
  1502   { fix F S assume "\<forall>n. F n \<in> A n" "open S" "x \<in> S"
  1503     with A(3)[of S] have "eventually (\<lambda>n. F n \<in> S) sequentially"
  1504       by (auto elim: eventually_elim1 simp: subset_eq) }
  1505   with A show "\<exists>A. (\<forall>i. open (A i)) \<and> (\<forall>i. x \<in> A i) \<and> (\<forall>F. (\<forall>n. F n \<in> A n) \<longrightarrow> F ----> x)"
  1506     by (intro exI[of _ A]) (auto simp: tendsto_def)
  1507 qed
  1508 
  1509 lemma (in first_countable_topology) sequentially_imp_eventually_nhds_within:
  1510   assumes "\<forall>f. (\<forall>n. f n \<in> s) \<and> f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially"
  1511   shows "eventually P (nhds a within s)"
  1512 proof (rule ccontr)
  1513   from countable_basis[of a] guess A . note A = this
  1514   assume "\<not> eventually P (nhds a within s)"
  1515   with A have P: "\<exists>F. \<forall>n. F n \<in> s \<and> F n \<in> A n \<and> \<not> P (F n)"
  1516     unfolding eventually_within eventually_nhds by (intro choice) fastforce
  1517   then guess F ..
  1518   hence F0: "\<forall>n. F n \<in> s" and F2: "\<forall>n. F n \<in> A n" and F3: "\<forall>n. \<not> P (F n)"
  1519     by fast+
  1520   with A have "F ----> a" by auto
  1521   hence "eventually (\<lambda>n. P (F n)) sequentially"
  1522     using assms F0 by simp
  1523   thus "False" by (simp add: F3)
  1524 qed
  1525 
  1526 lemma (in first_countable_topology) eventually_nhds_within_iff_sequentially:
  1527   "eventually P (nhds a within s) \<longleftrightarrow> 
  1528     (\<forall>f. (\<forall>n. f n \<in> s) \<and> f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
  1529 proof (safe intro!: sequentially_imp_eventually_nhds_within)
  1530   assume "eventually P (nhds a within s)" 
  1531   then obtain S where "open S" "a \<in> S" "\<forall>x\<in>S. x \<in> s \<longrightarrow> P x"
  1532     by (auto simp: eventually_within eventually_nhds)
  1533   moreover fix f assume "\<forall>n. f n \<in> s" "f ----> a"
  1534   ultimately show "eventually (\<lambda>n. P (f n)) sequentially"
  1535     by (auto dest!: topological_tendstoD elim: eventually_elim1)
  1536 qed
  1537 
  1538 lemma (in first_countable_topology) eventually_nhds_iff_sequentially:
  1539   "eventually P (nhds a) \<longleftrightarrow> (\<forall>f. f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially)"
  1540   using eventually_nhds_within_iff_sequentially[of P a UNIV] by simp
  1541 
  1542 subsection {* Function limit at a point *}
  1543 
  1544 abbreviation
  1545   LIM :: "('a::topological_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
  1546         ("((_)/ -- (_)/ --> (_))" [60, 0, 60] 60) where
  1547   "f -- a --> L \<equiv> (f ---> L) (at a)"
  1548 
  1549 lemma tendsto_within_open: "a \<in> S \<Longrightarrow> open S \<Longrightarrow> (f ---> l) (at a within S) \<longleftrightarrow> (f -- a --> l)"
  1550   unfolding tendsto_def by (simp add: at_within_open)
  1551 
  1552 lemma LIM_const_not_eq[tendsto_intros]:
  1553   fixes a :: "'a::perfect_space"
  1554   fixes k L :: "'b::t2_space"
  1555   shows "k \<noteq> L \<Longrightarrow> \<not> (\<lambda>x. k) -- a --> L"
  1556   by (simp add: tendsto_const_iff)
  1557 
  1558 lemmas LIM_not_zero = LIM_const_not_eq [where L = 0]
  1559 
  1560 lemma LIM_const_eq:
  1561   fixes a :: "'a::perfect_space"
  1562   fixes k L :: "'b::t2_space"
  1563   shows "(\<lambda>x. k) -- a --> L \<Longrightarrow> k = L"
  1564   by (simp add: tendsto_const_iff)
  1565 
  1566 lemma LIM_unique:
  1567   fixes a :: "'a::perfect_space" and L M :: "'b::t2_space"
  1568   shows "f -- a --> L \<Longrightarrow> f -- a --> M \<Longrightarrow> L = M"
  1569   using at_neq_bot by (rule tendsto_unique)
  1570 
  1571 text {* Limits are equal for functions equal except at limit point *}
  1572 
  1573 lemma LIM_equal: "\<forall>x. x \<noteq> a --> (f x = g x) \<Longrightarrow> (f -- a --> l) \<longleftrightarrow> (g -- a --> l)"
  1574   unfolding tendsto_def eventually_at_topological by simp
  1575 
  1576 lemma LIM_cong: "a = b \<Longrightarrow> (\<And>x. x \<noteq> b \<Longrightarrow> f x = g x) \<Longrightarrow> l = m \<Longrightarrow> (f -- a --> l) \<longleftrightarrow> (g -- b --> m)"
  1577   by (simp add: LIM_equal)
  1578 
  1579 lemma LIM_cong_limit: "f -- x --> L \<Longrightarrow> K = L \<Longrightarrow> f -- x --> K"
  1580   by simp
  1581 
  1582 lemma tendsto_at_iff_tendsto_nhds:
  1583   "g -- l --> g l \<longleftrightarrow> (g ---> g l) (nhds l)"
  1584   unfolding tendsto_def at_def eventually_within
  1585   by (intro ext all_cong imp_cong) (auto elim!: eventually_elim1)
  1586 
  1587 lemma tendsto_compose:
  1588   "g -- l --> g l \<Longrightarrow> (f ---> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> g l) F"
  1589   unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])
  1590 
  1591 lemma LIM_o: "\<lbrakk>g -- l --> g l; f -- a --> l\<rbrakk> \<Longrightarrow> (g \<circ> f) -- a --> g l"
  1592   unfolding o_def by (rule tendsto_compose)
  1593 
  1594 lemma tendsto_compose_eventually:
  1595   "g -- l --> m \<Longrightarrow> (f ---> l) F \<Longrightarrow> eventually (\<lambda>x. f x \<noteq> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> m) F"
  1596   by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)
  1597 
  1598 lemma LIM_compose_eventually:
  1599   assumes f: "f -- a --> b"
  1600   assumes g: "g -- b --> c"
  1601   assumes inj: "eventually (\<lambda>x. f x \<noteq> b) (at a)"
  1602   shows "(\<lambda>x. g (f x)) -- a --> c"
  1603   using g f inj by (rule tendsto_compose_eventually)
  1604 
  1605 subsubsection {* Relation of LIM and LIMSEQ *}
  1606 
  1607 lemma (in first_countable_topology) sequentially_imp_eventually_within:
  1608   "(\<forall>f. (\<forall>n. f n \<in> s \<and> f n \<noteq> a) \<and> f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially) \<Longrightarrow>
  1609     eventually P (at a within s)"
  1610   unfolding at_def within_within_eq
  1611   by (intro sequentially_imp_eventually_nhds_within) auto
  1612 
  1613 lemma (in first_countable_topology) sequentially_imp_eventually_at:
  1614   "(\<forall>f. (\<forall>n. f n \<noteq> a) \<and> f ----> a \<longrightarrow> eventually (\<lambda>n. P (f n)) sequentially) \<Longrightarrow> eventually P (at a)"
  1615   using assms sequentially_imp_eventually_within [where s=UNIV] by simp
  1616 
  1617 lemma LIMSEQ_SEQ_conv1:
  1618   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
  1619   assumes f: "f -- a --> l"
  1620   shows "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> a \<longrightarrow> (\<lambda>n. f (S n)) ----> l"
  1621   using tendsto_compose_eventually [OF f, where F=sequentially] by simp
  1622 
  1623 lemma LIMSEQ_SEQ_conv2:
  1624   fixes f :: "'a::first_countable_topology \<Rightarrow> 'b::topological_space"
  1625   assumes "\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> a \<longrightarrow> (\<lambda>n. f (S n)) ----> l"
  1626   shows "f -- a --> l"
  1627   using assms unfolding tendsto_def [where l=l] by (simp add: sequentially_imp_eventually_at)
  1628 
  1629 lemma LIMSEQ_SEQ_conv:
  1630   "(\<forall>S. (\<forall>n. S n \<noteq> a) \<and> S ----> (a::'a::first_countable_topology) \<longrightarrow> (\<lambda>n. X (S n)) ----> L) =
  1631    (X -- a --> (L::'b::topological_space))"
  1632   using LIMSEQ_SEQ_conv2 LIMSEQ_SEQ_conv1 ..
  1633 
  1634 subsection {* Continuity *}
  1635 
  1636 subsubsection {* Continuity on a set *}
  1637 
  1638 definition continuous_on :: "'a set \<Rightarrow> ('a :: topological_space \<Rightarrow> 'b :: topological_space) \<Rightarrow> bool" where
  1639   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. (f ---> f x) (at x within s))"
  1640 
  1641 lemma continuous_on_cong [cong]:
  1642   "s = t \<Longrightarrow> (\<And>x. x \<in> t \<Longrightarrow> f x = g x) \<Longrightarrow> continuous_on s f \<longleftrightarrow> continuous_on t g"
  1643   unfolding continuous_on_def by (intro ball_cong filterlim_cong) (auto simp: eventually_within)
  1644 
  1645 lemma continuous_on_topological:
  1646   "continuous_on s f \<longleftrightarrow>
  1647     (\<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)))"
  1648   unfolding continuous_on_def tendsto_def eventually_within eventually_at_topological by metis
  1649 
  1650 lemma continuous_on_open_invariant:
  1651   "continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s))"
  1652 proof safe
  1653   fix B :: "'b set" assume "continuous_on s f" "open B"
  1654   then have "\<forall>x\<in>f -` B \<inter> s. (\<exists>A. open A \<and> x \<in> A \<and> s \<inter> A \<subseteq> f -` B)"
  1655     by (auto simp: continuous_on_topological subset_eq Ball_def imp_conjL)
  1656   then guess A unfolding bchoice_iff ..
  1657   then show "\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s"
  1658     by (intro exI[of _ "\<Union>x\<in>f -` B \<inter> s. A x"]) auto
  1659 next
  1660   assume B: "\<forall>B. open B \<longrightarrow> (\<exists>A. open A \<and> A \<inter> s = f -` B \<inter> s)"
  1661   show "continuous_on s f"
  1662     unfolding continuous_on_topological
  1663   proof safe
  1664     fix x B assume "x \<in> s" "open B" "f x \<in> B"
  1665     with B obtain A where A: "open A" "A \<inter> s = f -` B \<inter> s" by auto
  1666     with `x \<in> s` `f x \<in> B` show "\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)"
  1667       by (intro exI[of _ A]) auto
  1668   qed
  1669 qed
  1670 
  1671 lemma continuous_on_open_vimage:
  1672   "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. open B \<longrightarrow> open (f -` B \<inter> s))"
  1673   unfolding continuous_on_open_invariant
  1674   by (metis open_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
  1675 
  1676 lemma continuous_on_closed_invariant:
  1677   "continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> (\<exists>A. closed A \<and> A \<inter> s = f -` B \<inter> s))"
  1678 proof -
  1679   have *: "\<And>P Q::'b set\<Rightarrow>bool. (\<And>A. P A \<longleftrightarrow> Q (- A)) \<Longrightarrow> (\<forall>A. P A) \<longleftrightarrow> (\<forall>A. Q A)"
  1680     by (metis double_compl)
  1681   show ?thesis
  1682     unfolding continuous_on_open_invariant by (intro *) (auto simp: open_closed[symmetric])
  1683 qed
  1684 
  1685 lemma continuous_on_closed_vimage:
  1686   "closed s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>B. closed B \<longrightarrow> closed (f -` B \<inter> s))"
  1687   unfolding continuous_on_closed_invariant
  1688   by (metis closed_Int Int_absorb Int_commute[of s] Int_assoc[of _ _ s])
  1689 
  1690 lemma continuous_on_open_Union:
  1691   "(\<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"
  1692   unfolding continuous_on_def by (simp add: tendsto_within_open open_Union)
  1693 
  1694 lemma continuous_on_open_UN:
  1695   "(\<And>s. s \<in> S \<Longrightarrow> open (A s)) \<Longrightarrow> (\<And>s. s \<in> S \<Longrightarrow> continuous_on (A s) f) \<Longrightarrow> continuous_on (\<Union>s\<in>S. A s) f"
  1696   unfolding Union_image_eq[symmetric] by (rule continuous_on_open_Union) auto
  1697 
  1698 lemma continuous_on_closed_Un:
  1699   "closed s \<Longrightarrow> closed t \<Longrightarrow> continuous_on s f \<Longrightarrow> continuous_on t f \<Longrightarrow> continuous_on (s \<union> t) f"
  1700   by (auto simp add: continuous_on_closed_vimage closed_Un Int_Un_distrib)
  1701 
  1702 lemma continuous_on_If:
  1703   assumes closed: "closed s" "closed t" and cont: "continuous_on s f" "continuous_on t g"
  1704     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"
  1705   shows "continuous_on (s \<union> t) (\<lambda>x. if P x then f x else g x)" (is "continuous_on _ ?h")
  1706 proof-
  1707   from P have "\<forall>x\<in>s. f x = ?h x" "\<forall>x\<in>t. g x = ?h x"
  1708     by auto
  1709   with cont have "continuous_on s ?h" "continuous_on t ?h"
  1710     by simp_all
  1711   with closed show ?thesis
  1712     by (rule continuous_on_closed_Un)
  1713 qed
  1714 
  1715 ML {*
  1716 
  1717 structure Continuous_On_Intros = Named_Thms
  1718 (
  1719   val name = @{binding continuous_on_intros}
  1720   val description = "Structural introduction rules for setwise continuity"
  1721 )
  1722 
  1723 *}
  1724 
  1725 setup Continuous_On_Intros.setup
  1726 
  1727 lemma continuous_on_id[continuous_on_intros]: "continuous_on s (\<lambda>x. x)"
  1728   unfolding continuous_on_def by (fast intro: tendsto_ident_at_within)
  1729 
  1730 lemma continuous_on_const[continuous_on_intros]: "continuous_on s (\<lambda>x. c)"
  1731   unfolding continuous_on_def by (auto intro: tendsto_const)
  1732 
  1733 lemma continuous_on_compose[continuous_on_intros]:
  1734   "continuous_on s f \<Longrightarrow> continuous_on (f ` s) g \<Longrightarrow> continuous_on s (g o f)"
  1735   unfolding continuous_on_topological by simp metis
  1736 
  1737 lemma continuous_on_compose2:
  1738   "continuous_on t g \<Longrightarrow> continuous_on s f \<Longrightarrow> t = f ` s \<Longrightarrow> continuous_on s (\<lambda>x. g (f x))"
  1739   using continuous_on_compose[of s f g] by (simp add: comp_def)
  1740 
  1741 subsubsection {* Continuity at a point *}
  1742 
  1743 definition continuous :: "'a::t2_space filter \<Rightarrow> ('a \<Rightarrow> 'b::topological_space) \<Rightarrow> bool" where
  1744   "continuous F f \<longleftrightarrow> (f ---> f (Lim F (\<lambda>x. x))) F"
  1745 
  1746 ML {*
  1747 
  1748 structure Continuous_Intros = Named_Thms
  1749 (
  1750   val name = @{binding continuous_intros}
  1751   val description = "Structural introduction rules for pointwise continuity"
  1752 )
  1753 
  1754 *}
  1755 
  1756 setup Continuous_Intros.setup
  1757 
  1758 lemma continuous_bot[continuous_intros, simp]: "continuous bot f"
  1759   unfolding continuous_def by auto
  1760 
  1761 lemma continuous_trivial_limit: "trivial_limit net \<Longrightarrow> continuous net f"
  1762   by simp
  1763 
  1764 lemma continuous_within: "continuous (at x within s) f \<longleftrightarrow> (f ---> f x) (at x within s)"
  1765   by (cases "trivial_limit (at x within s)") (auto simp add: Lim_ident_at_within continuous_def)
  1766 
  1767 lemma continuous_within_topological:
  1768   "continuous (at x within s) f \<longleftrightarrow>
  1769     (\<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)))"
  1770   unfolding continuous_within tendsto_def eventually_within eventually_at_topological by metis
  1771 
  1772 lemma continuous_within_compose[continuous_intros]:
  1773   "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
  1774   continuous (at x within s) (g o f)"
  1775   by (simp add: continuous_within_topological) metis
  1776 
  1777 lemma continuous_within_compose2:
  1778   "continuous (at x within s) f \<Longrightarrow> continuous (at (f x) within f ` s) g \<Longrightarrow>
  1779   continuous (at x within s) (\<lambda>x. g (f x))"
  1780   using continuous_within_compose[of x s f g] by (simp add: comp_def)
  1781 
  1782 lemma continuous_at: "continuous (at x) f \<longleftrightarrow> f -- x --> f x"
  1783   using continuous_within[of x UNIV f] by simp
  1784 
  1785 lemma continuous_ident[continuous_intros, simp]: "continuous (at x within S) (\<lambda>x. x)"
  1786   unfolding continuous_within by (rule tendsto_ident_at_within)
  1787 
  1788 lemma continuous_const[continuous_intros, simp]: "continuous F (\<lambda>x. c)"
  1789   unfolding continuous_def by (rule tendsto_const)
  1790 
  1791 lemma continuous_on_eq_continuous_within:
  1792   "continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. continuous (at x within s) f)"
  1793   unfolding continuous_on_def continuous_within ..
  1794 
  1795 abbreviation isCont :: "('a::t2_space \<Rightarrow> 'b::topological_space) \<Rightarrow> 'a \<Rightarrow> bool" where
  1796   "isCont f a \<equiv> continuous (at a) f"
  1797 
  1798 lemma isCont_def: "isCont f a \<longleftrightarrow> f -- a --> f a"
  1799   by (rule continuous_at)
  1800 
  1801 lemma continuous_at_within: "isCont f x \<Longrightarrow> continuous (at x within s) f"
  1802   by (auto intro: within_le filterlim_mono simp: continuous_at continuous_within)
  1803 
  1804 lemma continuous_on_eq_continuous_at: "open s \<Longrightarrow> continuous_on s f \<longleftrightarrow> (\<forall>x\<in>s. isCont f x)"
  1805   by (simp add: continuous_on_def continuous_at tendsto_within_open)
  1806 
  1807 lemma continuous_on_subset: "continuous_on s f \<Longrightarrow> t \<subseteq> s \<Longrightarrow> continuous_on t f"
  1808   unfolding continuous_on_def by (metis subset_eq tendsto_within_subset)
  1809 
  1810 lemma continuous_at_imp_continuous_on: "\<forall>x\<in>s. isCont f x \<Longrightarrow> continuous_on s f"
  1811   by (auto intro: continuous_at_within simp: continuous_on_eq_continuous_within)
  1812 
  1813 lemma isContI_continuous: "continuous (at x within UNIV) f \<Longrightarrow> isCont f x"
  1814   by simp
  1815 
  1816 lemma isCont_ident[continuous_intros, simp]: "isCont (\<lambda>x. x) a"
  1817   using continuous_ident by (rule isContI_continuous)
  1818 
  1819 lemmas isCont_const = continuous_const
  1820 
  1821 lemma isCont_o2: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (\<lambda>x. g (f x)) a"
  1822   unfolding isCont_def by (rule tendsto_compose)
  1823 
  1824 lemma isCont_o[continuous_intros]: "isCont f a \<Longrightarrow> isCont g (f a) \<Longrightarrow> isCont (g \<circ> f) a"
  1825   unfolding o_def by (rule isCont_o2)
  1826 
  1827 lemma isCont_tendsto_compose: "isCont g l \<Longrightarrow> (f ---> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> g l) F"
  1828   unfolding isCont_def by (rule tendsto_compose)
  1829 
  1830 lemma continuous_within_compose3:
  1831   "isCont g (f x) \<Longrightarrow> continuous (at x within s) f \<Longrightarrow> continuous (at x within s) (\<lambda>x. g (f x))"
  1832   using continuous_within_compose2[of x s f g] by (simp add: continuous_at_within)
  1833 
  1834 subsubsection{* Open-cover compactness *}
  1835 
  1836 context topological_space
  1837 begin
  1838 
  1839 definition compact :: "'a set \<Rightarrow> bool" where
  1840   compact_eq_heine_borel: -- "This name is used for backwards compatibility"
  1841     "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))"
  1842 
  1843 lemma compactI:
  1844   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'"
  1845   shows "compact s"
  1846   unfolding compact_eq_heine_borel using assms by metis
  1847 
  1848 lemma compact_empty[simp]: "compact {}"
  1849   by (auto intro!: compactI)
  1850 
  1851 lemma compactE:
  1852   assumes "compact s" and "\<forall>t\<in>C. open t" and "s \<subseteq> \<Union>C"
  1853   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> \<Union>C'"
  1854   using assms unfolding compact_eq_heine_borel by metis
  1855 
  1856 lemma compactE_image:
  1857   assumes "compact s" and "\<forall>t\<in>C. open (f t)" and "s \<subseteq> (\<Union>c\<in>C. f c)"
  1858   obtains C' where "C' \<subseteq> C" and "finite C'" and "s \<subseteq> (\<Union>c\<in>C'. f c)"
  1859   using assms unfolding ball_simps[symmetric] SUP_def
  1860   by (metis (lifting) finite_subset_image compact_eq_heine_borel[of s])
  1861 
  1862 lemma compact_inter_closed [intro]:
  1863   assumes "compact s" and "closed t"
  1864   shows "compact (s \<inter> t)"
  1865 proof (rule compactI)
  1866   fix C assume C: "\<forall>c\<in>C. open c" and cover: "s \<inter> t \<subseteq> \<Union>C"
  1867   from C `closed t` have "\<forall>c\<in>C \<union> {-t}. open c" by auto
  1868   moreover from cover have "s \<subseteq> \<Union>(C \<union> {-t})" by auto
  1869   ultimately have "\<exists>D\<subseteq>C \<union> {-t}. finite D \<and> s \<subseteq> \<Union>D"
  1870     using `compact s` unfolding compact_eq_heine_borel by auto
  1871   then guess D ..
  1872   then show "\<exists>D\<subseteq>C. finite D \<and> s \<inter> t \<subseteq> \<Union>D"
  1873     by (intro exI[of _ "D - {-t}"]) auto
  1874 qed
  1875 
  1876 end
  1877 
  1878 lemma (in t2_space) compact_imp_closed:
  1879   assumes "compact s" shows "closed s"
  1880 unfolding closed_def
  1881 proof (rule openI)
  1882   fix y assume "y \<in> - s"
  1883   let ?C = "\<Union>x\<in>s. {u. open u \<and> x \<in> u \<and> eventually (\<lambda>y. y \<notin> u) (nhds y)}"
  1884   note `compact s`
  1885   moreover have "\<forall>u\<in>?C. open u" by simp
  1886   moreover have "s \<subseteq> \<Union>?C"
  1887   proof
  1888     fix x assume "x \<in> s"
  1889     with `y \<in> - s` have "x \<noteq> y" by clarsimp
  1890     hence "\<exists>u v. open u \<and> open v \<and> x \<in> u \<and> y \<in> v \<and> u \<inter> v = {}"
  1891       by (rule hausdorff)
  1892     with `x \<in> s` show "x \<in> \<Union>?C"
  1893       unfolding eventually_nhds by auto
  1894   qed
  1895   ultimately obtain D where "D \<subseteq> ?C" and "finite D" and "s \<subseteq> \<Union>D"
  1896     by (rule compactE)
  1897   from `D \<subseteq> ?C` have "\<forall>x\<in>D. eventually (\<lambda>y. y \<notin> x) (nhds y)" by auto
  1898   with `finite D` have "eventually (\<lambda>y. y \<notin> \<Union>D) (nhds y)"
  1899     by (simp add: eventually_Ball_finite)
  1900   with `s \<subseteq> \<Union>D` have "eventually (\<lambda>y. y \<notin> s) (nhds y)"
  1901     by (auto elim!: eventually_mono [rotated])
  1902   thus "\<exists>t. open t \<and> y \<in> t \<and> t \<subseteq> - s"
  1903     by (simp add: eventually_nhds subset_eq)
  1904 qed
  1905 
  1906 lemma compact_continuous_image:
  1907   assumes f: "continuous_on s f" and s: "compact s"
  1908   shows "compact (f ` s)"
  1909 proof (rule compactI)
  1910   fix C assume "\<forall>c\<in>C. open c" and cover: "f`s \<subseteq> \<Union>C"
  1911   with f have "\<forall>c\<in>C. \<exists>A. open A \<and> A \<inter> s = f -` c \<inter> s"
  1912     unfolding continuous_on_open_invariant by blast
  1913   then guess A unfolding bchoice_iff .. note A = this
  1914   with cover have "\<forall>c\<in>C. open (A c)" "s \<subseteq> (\<Union>c\<in>C. A c)"
  1915     by (fastforce simp add: subset_eq set_eq_iff)+
  1916   from compactE_image[OF s this] obtain D where "D \<subseteq> C" "finite D" "s \<subseteq> (\<Union>c\<in>D. A c)" .
  1917   with A show "\<exists>D \<subseteq> C. finite D \<and> f`s \<subseteq> \<Union>D"
  1918     by (intro exI[of _ D]) (fastforce simp add: subset_eq set_eq_iff)+
  1919 qed
  1920 
  1921 lemma continuous_on_inv:
  1922   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  1923   assumes "continuous_on s f"  "compact s"  "\<forall>x\<in>s. g (f x) = x"
  1924   shows "continuous_on (f ` s) g"
  1925 unfolding continuous_on_topological
  1926 proof (clarsimp simp add: assms(3))
  1927   fix x :: 'a and B :: "'a set"
  1928   assume "x \<in> s" and "open B" and "x \<in> B"
  1929   have 1: "\<forall>x\<in>s. f x \<in> f ` (s - B) \<longleftrightarrow> x \<in> s - B"
  1930     using assms(3) by (auto, metis)
  1931   have "continuous_on (s - B) f"
  1932     using `continuous_on s f` Diff_subset
  1933     by (rule continuous_on_subset)
  1934   moreover have "compact (s - B)"
  1935     using `open B` and `compact s`
  1936     unfolding Diff_eq by (intro compact_inter_closed closed_Compl)
  1937   ultimately have "compact (f ` (s - B))"
  1938     by (rule compact_continuous_image)
  1939   hence "closed (f ` (s - B))"
  1940     by (rule compact_imp_closed)
  1941   hence "open (- f ` (s - B))"
  1942     by (rule open_Compl)
  1943   moreover have "f x \<in> - f ` (s - B)"
  1944     using `x \<in> s` and `x \<in> B` by (simp add: 1)
  1945   moreover have "\<forall>y\<in>s. f y \<in> - f ` (s - B) \<longrightarrow> y \<in> B"
  1946     by (simp add: 1)
  1947   ultimately show "\<exists>A. open A \<and> f x \<in> A \<and> (\<forall>y\<in>s. f y \<in> A \<longrightarrow> y \<in> B)"
  1948     by fast
  1949 qed
  1950 
  1951 lemma continuous_on_inv_into:
  1952   fixes f :: "'a::topological_space \<Rightarrow> 'b::t2_space"
  1953   assumes s: "continuous_on s f" "compact s" and f: "inj_on f s"
  1954   shows "continuous_on (f ` s) (the_inv_into s f)"
  1955   by (rule continuous_on_inv[OF s]) (auto simp: the_inv_into_f_f[OF f])
  1956 
  1957 lemma (in linorder_topology) compact_attains_sup:
  1958   assumes "compact S" "S \<noteq> {}"
  1959   shows "\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s"
  1960 proof (rule classical)
  1961   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. t \<le> s)"
  1962   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. s < t s"
  1963     by (metis not_le)
  1964   then have "\<forall>s\<in>S. open {..< t s}" "S \<subseteq> (\<Union>s\<in>S. {..< t s})"
  1965     by auto
  1966   with `compact S` obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {..< t s})"
  1967     by (erule compactE_image)
  1968   with `S \<noteq> {}` have Max: "Max (t`C) \<in> t`C" and "\<forall>s\<in>t`C. s \<le> Max (t`C)"
  1969     by (auto intro!: Max_in)
  1970   with C have "S \<subseteq> {..< Max (t`C)}"
  1971     by (auto intro: less_le_trans simp: subset_eq)
  1972   with t Max `C \<subseteq> S` show ?thesis
  1973     by fastforce
  1974 qed
  1975 
  1976 lemma (in linorder_topology) compact_attains_inf:
  1977   assumes "compact S" "S \<noteq> {}"
  1978   shows "\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t"
  1979 proof (rule classical)
  1980   assume "\<not> (\<exists>s\<in>S. \<forall>t\<in>S. s \<le> t)"
  1981   then obtain t where t: "\<forall>s\<in>S. t s \<in> S" and "\<forall>s\<in>S. t s < s"
  1982     by (metis not_le)
  1983   then have "\<forall>s\<in>S. open {t s <..}" "S \<subseteq> (\<Union>s\<in>S. {t s <..})"
  1984     by auto
  1985   with `compact S` obtain C where "C \<subseteq> S" "finite C" and C: "S \<subseteq> (\<Union>s\<in>C. {t s <..})"
  1986     by (erule compactE_image)
  1987   with `S \<noteq> {}` have Min: "Min (t`C) \<in> t`C" and "\<forall>s\<in>t`C. Min (t`C) \<le> s"
  1988     by (auto intro!: Min_in)
  1989   with C have "S \<subseteq> {Min (t`C) <..}"
  1990     by (auto intro: le_less_trans simp: subset_eq)
  1991   with t Min `C \<subseteq> S` show ?thesis
  1992     by fastforce
  1993 qed
  1994 
  1995 lemma continuous_attains_sup:
  1996   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
  1997   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)"
  1998   using compact_attains_sup[of "f ` s"] compact_continuous_image[of s f] by auto
  1999 
  2000 lemma continuous_attains_inf:
  2001   fixes f :: "'a::topological_space \<Rightarrow> 'b::linorder_topology"
  2002   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)"
  2003   using compact_attains_inf[of "f ` s"] compact_continuous_image[of s f] by auto
  2004 
  2005 
  2006 subsection {* Connectedness *}
  2007 
  2008 context topological_space
  2009 begin
  2010 
  2011 definition "connected S \<longleftrightarrow>
  2012   \<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> {})"
  2013 
  2014 lemma connectedI:
  2015   "(\<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)
  2016   \<Longrightarrow> connected U"
  2017   by (auto simp: connected_def)
  2018 
  2019 lemma connected_empty[simp]: "connected {}"
  2020   by (auto intro!: connectedI)
  2021 
  2022 end
  2023 
  2024 lemma (in linorder_topology) connectedD_interval:
  2025   assumes "connected U" and xy: "x \<in> U" "y \<in> U" and "x \<le> z" "z \<le> y"
  2026   shows "z \<in> U"
  2027 proof -
  2028   have eq: "{..<z} \<union> {z<..} = - {z}"
  2029     by auto
  2030   { assume "z \<notin> U" "x < z" "z < y"
  2031     with xy have "\<not> connected U"
  2032       unfolding connected_def simp_thms
  2033       apply (rule_tac exI[of _ "{..< z}"])
  2034       apply (rule_tac exI[of _ "{z <..}"])
  2035       apply (auto simp add: eq)
  2036       done }
  2037   with assms show "z \<in> U"
  2038     by (metis less_le)
  2039 qed
  2040 
  2041 lemma connected_continuous_image:
  2042   assumes *: "continuous_on s f"
  2043   assumes "connected s"
  2044   shows "connected (f ` s)"
  2045 proof (rule connectedI)
  2046   fix A B assume A: "open A" "A \<inter> f ` s \<noteq> {}" and B: "open B" "B \<inter> f ` s \<noteq> {}" and
  2047     AB: "A \<inter> B \<inter> f ` s = {}" "f ` s \<subseteq> A \<union> B"
  2048   obtain A' where A': "open A'" "f -` A \<inter> s = A' \<inter> s"
  2049     using * `open A` unfolding continuous_on_open_invariant by metis
  2050   obtain B' where B': "open B'" "f -` B \<inter> s = B' \<inter> s"
  2051     using * `open B` unfolding continuous_on_open_invariant by metis
  2052 
  2053   have "\<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> {}"
  2054   proof (rule exI[of _ A'], rule exI[of _ B'], intro conjI)
  2055     have "s \<subseteq> (f -` A \<inter> s) \<union> (f -` B \<inter> s)" using AB by auto
  2056     then show "s \<subseteq> A' \<union> B'" using A' B' by auto
  2057   next
  2058     have "(f -` A \<inter> s) \<inter> (f -` B \<inter> s) = {}" using AB by auto
  2059     then show "A' \<inter> B' \<inter> s = {}" using A' B' by auto
  2060   qed (insert A' B' A B, auto)
  2061   with `connected s` show False
  2062     unfolding connected_def by blast
  2063 qed
  2064 
  2065 
  2066 section {* Connectedness *}
  2067 
  2068 class linear_continuum_topology = linorder_topology + conditional_complete_linorder + inner_dense_linorder
  2069 begin
  2070 
  2071 lemma Inf_notin_open:
  2072   assumes A: "open A" and bnd: "\<forall>a\<in>A. x < a"
  2073   shows "Inf A \<notin> A"
  2074 proof
  2075   assume "Inf A \<in> A"
  2076   then obtain b where "b < Inf A" "{b <.. Inf A} \<subseteq> A"
  2077     using open_left[of A "Inf A" x] assms by auto
  2078   with dense[of b "Inf A"] obtain c where "c < Inf A" "c \<in> A"
  2079     by (auto simp: subset_eq)
  2080   then show False
  2081     using cInf_lower[OF `c \<in> A`, of x] bnd by (metis less_imp_le not_le)
  2082 qed
  2083 
  2084 lemma Sup_notin_open:
  2085   assumes A: "open A" and bnd: "\<forall>a\<in>A. a < x"
  2086   shows "Sup A \<notin> A"
  2087 proof
  2088   assume "Sup A \<in> A"
  2089   then obtain b where "Sup A < b" "{Sup A ..< b} \<subseteq> A"
  2090     using open_right[of A "Sup A" x] assms by auto
  2091   with dense[of "Sup A" b] obtain c where "Sup A < c" "c \<in> A"
  2092     by (auto simp: subset_eq)
  2093   then show False
  2094     using cSup_upper[OF `c \<in> A`, of x] bnd by (metis less_imp_le not_le)
  2095 qed
  2096 
  2097 end
  2098 
  2099 lemma connectedI_interval:
  2100   fixes U :: "'a :: linear_continuum_topology set"
  2101   assumes *: "\<And>x y z. x \<in> U \<Longrightarrow> y \<in> U \<Longrightarrow> x \<le> z \<Longrightarrow> z \<le> y \<Longrightarrow> z \<in> U"
  2102   shows "connected U"
  2103 proof (rule connectedI)
  2104   { fix A B assume "open A" "open B" "A \<inter> B \<inter> U = {}" "U \<subseteq> A \<union> B"
  2105     fix x y assume "x < y" "x \<in> A" "y \<in> B" "x \<in> U" "y \<in> U"
  2106 
  2107     let ?z = "Inf (B \<inter> {x <..})"
  2108 
  2109     have "x \<le> ?z" "?z \<le> y"
  2110       using `y \<in> B` `x < y` by (auto intro: cInf_lower[where z=x] cInf_greatest)
  2111     with `x \<in> U` `y \<in> U` have "?z \<in> U"
  2112       by (rule *)
  2113     moreover have "?z \<notin> B \<inter> {x <..}"
  2114       using `open B` by (intro Inf_notin_open) auto
  2115     ultimately have "?z \<in> A"
  2116       using `x \<le> ?z` `A \<inter> B \<inter> U = {}` `x \<in> A` `U \<subseteq> A \<union> B` by auto
  2117 
  2118     { assume "?z < y"
  2119       obtain a where "?z < a" "{?z ..< a} \<subseteq> A"
  2120         using open_right[OF `open A` `?z \<in> A` `?z < y`] by auto
  2121       moreover obtain b where "b \<in> B" "x < b" "b < min a y"
  2122         using cInf_less_iff[of "B \<inter> {x <..}" x "min a y"] `?z < a` `?z < y` `x < y` `y \<in> B`
  2123         by (auto intro: less_imp_le)
  2124       moreover then have "?z \<le> b"
  2125         by (intro cInf_lower[where z=x]) auto
  2126       moreover have "b \<in> U"
  2127         using `x \<le> ?z` `?z \<le> b` `b < min a y`
  2128         by (intro *[OF `x \<in> U` `y \<in> U`]) (auto simp: less_imp_le)
  2129       ultimately have "\<exists>b\<in>B. b \<in> A \<and> b \<in> U"
  2130         by (intro bexI[of _ b]) auto }
  2131     then have False
  2132       using `?z \<le> y` `?z \<in> A` `y \<in> B` `y \<in> U` `A \<inter> B \<inter> U = {}` unfolding le_less by blast }
  2133   note not_disjoint = this
  2134 
  2135   fix A B assume AB: "open A" "open B" "U \<subseteq> A \<union> B" "A \<inter> B \<inter> U = {}"
  2136   moreover assume "A \<inter> U \<noteq> {}" then obtain x where x: "x \<in> U" "x \<in> A" by auto
  2137   moreover assume "B \<inter> U \<noteq> {}" then obtain y where y: "y \<in> U" "y \<in> B" by auto
  2138   moreover note not_disjoint[of B A y x] not_disjoint[of A B x y]
  2139   ultimately show False by (cases x y rule: linorder_cases) auto
  2140 qed
  2141 
  2142 lemma connected_iff_interval:
  2143   fixes U :: "'a :: linear_continuum_topology set"
  2144   shows "connected U \<longleftrightarrow> (\<forall>x\<in>U. \<forall>y\<in>U. \<forall>z. x \<le> z \<longrightarrow> z \<le> y \<longrightarrow> z \<in> U)"
  2145   by (auto intro: connectedI_interval dest: connectedD_interval)
  2146 
  2147 lemma connected_UNIV[simp]: "connected (UNIV::'a::linear_continuum_topology set)"
  2148   unfolding connected_iff_interval by auto
  2149 
  2150 lemma connected_Ioi[simp]: "connected {a::'a::linear_continuum_topology <..}"
  2151   unfolding connected_iff_interval by auto
  2152 
  2153 lemma connected_Ici[simp]: "connected {a::'a::linear_continuum_topology ..}"
  2154   unfolding connected_iff_interval by auto
  2155 
  2156 lemma connected_Iio[simp]: "connected {..< a::'a::linear_continuum_topology}"
  2157   unfolding connected_iff_interval by auto
  2158 
  2159 lemma connected_Iic[simp]: "connected {.. a::'a::linear_continuum_topology}"
  2160   unfolding connected_iff_interval by auto
  2161 
  2162 lemma connected_Ioo[simp]: "connected {a <..< b::'a::linear_continuum_topology}"
  2163   unfolding connected_iff_interval by auto
  2164 
  2165 lemma connected_Ioc[simp]: "connected {a <.. b::'a::linear_continuum_topology}"
  2166   unfolding connected_iff_interval by auto
  2167 
  2168 lemma connected_Ico[simp]: "connected {a ..< b::'a::linear_continuum_topology}"
  2169   unfolding connected_iff_interval by auto
  2170 
  2171 lemma connected_Icc[simp]: "connected {a .. b::'a::linear_continuum_topology}"
  2172   unfolding connected_iff_interval by auto
  2173 
  2174 lemma connected_contains_Ioo: 
  2175   fixes A :: "'a :: linorder_topology set"
  2176   assumes A: "connected A" "a \<in> A" "b \<in> A" shows "{a <..< b} \<subseteq> A"
  2177   using connectedD_interval[OF A] by (simp add: subset_eq Ball_def less_imp_le)
  2178 
  2179 subsection {* Intermediate Value Theorem *}
  2180 
  2181 lemma IVT':
  2182   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2183   assumes y: "f a \<le> y" "y \<le> f b" "a \<le> b"
  2184   assumes *: "continuous_on {a .. b} f"
  2185   shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2186 proof -
  2187   have "connected {a..b}"
  2188     unfolding connected_iff_interval by auto
  2189   from connected_continuous_image[OF * this, THEN connectedD_interval, of "f a" "f b" y] y
  2190   show ?thesis
  2191     by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
  2192 qed
  2193 
  2194 lemma IVT2':
  2195   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2196   assumes y: "f b \<le> y" "y \<le> f a" "a \<le> b"
  2197   assumes *: "continuous_on {a .. b} f"
  2198   shows "\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2199 proof -
  2200   have "connected {a..b}"
  2201     unfolding connected_iff_interval by auto
  2202   from connected_continuous_image[OF * this, THEN connectedD_interval, of "f b" "f a" y] y
  2203   show ?thesis
  2204     by (auto simp add: atLeastAtMost_def atLeast_def atMost_def)
  2205 qed
  2206 
  2207 lemma IVT:
  2208   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2209   shows "f a \<le> y \<Longrightarrow> y \<le> f b \<Longrightarrow> a \<le> b \<Longrightarrow> (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x) \<Longrightarrow> \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2210   by (rule IVT') (auto intro: continuous_at_imp_continuous_on)
  2211 
  2212 lemma IVT2:
  2213   fixes f :: "'a :: linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2214   shows "f b \<le> y \<Longrightarrow> y \<le> f a \<Longrightarrow> a \<le> b \<Longrightarrow> (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x) \<Longrightarrow> \<exists>x. a \<le> x \<and> x \<le> b \<and> f x = y"
  2215   by (rule IVT2') (auto intro: continuous_at_imp_continuous_on)
  2216 
  2217 lemma continuous_inj_imp_mono:
  2218   fixes f :: "'a::linear_continuum_topology \<Rightarrow> 'b :: linorder_topology"
  2219   assumes x: "a < x" "x < b"
  2220   assumes cont: "continuous_on {a..b} f"
  2221   assumes inj: "inj_on f {a..b}"
  2222   shows "(f a < f x \<and> f x < f b) \<or> (f b < f x \<and> f x < f a)"
  2223 proof -
  2224   note I = inj_on_iff[OF inj]
  2225   { assume "f x < f a" "f x < f b"
  2226     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"
  2227       using IVT'[of f x "min (f a) (f b)" b] IVT2'[of f x "min (f a) (f b)" a] x
  2228       by (auto simp: continuous_on_subset[OF cont] less_imp_le)
  2229     with x I have False by auto }
  2230   moreover
  2231   { assume "f a < f x" "f b < f x"
  2232     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"
  2233       using IVT'[of f a "max (f a) (f b)" x] IVT2'[of f b "max (f a) (f b)" x] x
  2234       by (auto simp: continuous_on_subset[OF cont] less_imp_le)
  2235     with x I have False by auto }
  2236   ultimately show ?thesis
  2237     using I[of a x] I[of x b] x less_trans[OF x] by (auto simp add: le_less less_imp_neq neq_iff)
  2238 qed
  2239 
  2240 end
  2241