src/HOL/Limits.thy
author hoelzl
Mon Dec 03 18:19:05 2012 +0100 (2012-12-03)
changeset 50326 b5afeccab2db
parent 50325 5e40ad9f212a
child 50327 bbea2e82871c
permissions -rw-r--r--
add filterlim rules for exp and ln to infinity
     1 (*  Title       : Limits.thy
     2     Author      : Brian Huffman
     3 *)
     4 
     5 header {* Filters and Limits *}
     6 
     7 theory Limits
     8 imports RealVector
     9 begin
    10 
    11 subsection {* Filters *}
    12 
    13 text {*
    14   This definition also allows non-proper filters.
    15 *}
    16 
    17 locale is_filter =
    18   fixes F :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
    19   assumes True: "F (\<lambda>x. True)"
    20   assumes conj: "F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x) \<Longrightarrow> F (\<lambda>x. P x \<and> Q x)"
    21   assumes mono: "\<forall>x. P x \<longrightarrow> Q x \<Longrightarrow> F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x)"
    22 
    23 typedef 'a filter = "{F :: ('a \<Rightarrow> bool) \<Rightarrow> bool. is_filter F}"
    24 proof
    25   show "(\<lambda>x. True) \<in> ?filter" by (auto intro: is_filter.intro)
    26 qed
    27 
    28 lemma is_filter_Rep_filter: "is_filter (Rep_filter F)"
    29   using Rep_filter [of F] by simp
    30 
    31 lemma Abs_filter_inverse':
    32   assumes "is_filter F" shows "Rep_filter (Abs_filter F) = F"
    33   using assms by (simp add: Abs_filter_inverse)
    34 
    35 
    36 subsection {* Eventually *}
    37 
    38 definition eventually :: "('a \<Rightarrow> bool) \<Rightarrow> 'a filter \<Rightarrow> bool"
    39   where "eventually P F \<longleftrightarrow> Rep_filter F P"
    40 
    41 lemma eventually_Abs_filter:
    42   assumes "is_filter F" shows "eventually P (Abs_filter F) = F P"
    43   unfolding eventually_def using assms by (simp add: Abs_filter_inverse)
    44 
    45 lemma filter_eq_iff:
    46   shows "F = F' \<longleftrightarrow> (\<forall>P. eventually P F = eventually P F')"
    47   unfolding Rep_filter_inject [symmetric] fun_eq_iff eventually_def ..
    48 
    49 lemma eventually_True [simp]: "eventually (\<lambda>x. True) F"
    50   unfolding eventually_def
    51   by (rule is_filter.True [OF is_filter_Rep_filter])
    52 
    53 lemma always_eventually: "\<forall>x. P x \<Longrightarrow> eventually P F"
    54 proof -
    55   assume "\<forall>x. P x" hence "P = (\<lambda>x. True)" by (simp add: ext)
    56   thus "eventually P F" by simp
    57 qed
    58 
    59 lemma eventually_mono:
    60   "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually P F \<Longrightarrow> eventually Q F"
    61   unfolding eventually_def
    62   by (rule is_filter.mono [OF is_filter_Rep_filter])
    63 
    64 lemma eventually_conj:
    65   assumes P: "eventually (\<lambda>x. P x) F"
    66   assumes Q: "eventually (\<lambda>x. Q x) F"
    67   shows "eventually (\<lambda>x. P x \<and> Q x) F"
    68   using assms unfolding eventually_def
    69   by (rule is_filter.conj [OF is_filter_Rep_filter])
    70 
    71 lemma eventually_mp:
    72   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
    73   assumes "eventually (\<lambda>x. P x) F"
    74   shows "eventually (\<lambda>x. Q x) F"
    75 proof (rule eventually_mono)
    76   show "\<forall>x. (P x \<longrightarrow> Q x) \<and> P x \<longrightarrow> Q x" by simp
    77   show "eventually (\<lambda>x. (P x \<longrightarrow> Q x) \<and> P x) F"
    78     using assms by (rule eventually_conj)
    79 qed
    80 
    81 lemma eventually_rev_mp:
    82   assumes "eventually (\<lambda>x. P x) F"
    83   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
    84   shows "eventually (\<lambda>x. Q x) F"
    85 using assms(2) assms(1) by (rule eventually_mp)
    86 
    87 lemma eventually_conj_iff:
    88   "eventually (\<lambda>x. P x \<and> Q x) F \<longleftrightarrow> eventually P F \<and> eventually Q F"
    89   by (auto intro: eventually_conj elim: eventually_rev_mp)
    90 
    91 lemma eventually_elim1:
    92   assumes "eventually (\<lambda>i. P i) F"
    93   assumes "\<And>i. P i \<Longrightarrow> Q i"
    94   shows "eventually (\<lambda>i. Q i) F"
    95   using assms by (auto elim!: eventually_rev_mp)
    96 
    97 lemma eventually_elim2:
    98   assumes "eventually (\<lambda>i. P i) F"
    99   assumes "eventually (\<lambda>i. Q i) F"
   100   assumes "\<And>i. P i \<Longrightarrow> Q i \<Longrightarrow> R i"
   101   shows "eventually (\<lambda>i. R i) F"
   102   using assms by (auto elim!: eventually_rev_mp)
   103 
   104 lemma eventually_subst:
   105   assumes "eventually (\<lambda>n. P n = Q n) F"
   106   shows "eventually P F = eventually Q F" (is "?L = ?R")
   107 proof -
   108   from assms have "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
   109       and "eventually (\<lambda>x. Q x \<longrightarrow> P x) F"
   110     by (auto elim: eventually_elim1)
   111   then show ?thesis by (auto elim: eventually_elim2)
   112 qed
   113 
   114 ML {*
   115   fun eventually_elim_tac ctxt thms thm =
   116     let
   117       val thy = Proof_Context.theory_of ctxt
   118       val mp_thms = thms RL [@{thm eventually_rev_mp}]
   119       val raw_elim_thm =
   120         (@{thm allI} RS @{thm always_eventually})
   121         |> fold (fn thm1 => fn thm2 => thm2 RS thm1) mp_thms
   122         |> fold (fn _ => fn thm => @{thm impI} RS thm) thms
   123       val cases_prop = prop_of (raw_elim_thm RS thm)
   124       val cases = (Rule_Cases.make_common (thy, cases_prop) [(("elim", []), [])])
   125     in
   126       CASES cases (rtac raw_elim_thm 1) thm
   127     end
   128 *}
   129 
   130 method_setup eventually_elim = {*
   131   Scan.succeed (fn ctxt => METHOD_CASES (eventually_elim_tac ctxt))
   132 *} "elimination of eventually quantifiers"
   133 
   134 
   135 subsection {* Finer-than relation *}
   136 
   137 text {* @{term "F \<le> F'"} means that filter @{term F} is finer than
   138 filter @{term F'}. *}
   139 
   140 instantiation filter :: (type) complete_lattice
   141 begin
   142 
   143 definition le_filter_def:
   144   "F \<le> F' \<longleftrightarrow> (\<forall>P. eventually P F' \<longrightarrow> eventually P F)"
   145 
   146 definition
   147   "(F :: 'a filter) < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   148 
   149 definition
   150   "top = Abs_filter (\<lambda>P. \<forall>x. P x)"
   151 
   152 definition
   153   "bot = Abs_filter (\<lambda>P. True)"
   154 
   155 definition
   156   "sup F F' = Abs_filter (\<lambda>P. eventually P F \<and> eventually P F')"
   157 
   158 definition
   159   "inf F F' = Abs_filter
   160       (\<lambda>P. \<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   161 
   162 definition
   163   "Sup S = Abs_filter (\<lambda>P. \<forall>F\<in>S. eventually P F)"
   164 
   165 definition
   166   "Inf S = Sup {F::'a filter. \<forall>F'\<in>S. F \<le> F'}"
   167 
   168 lemma eventually_top [simp]: "eventually P top \<longleftrightarrow> (\<forall>x. P x)"
   169   unfolding top_filter_def
   170   by (rule eventually_Abs_filter, rule is_filter.intro, auto)
   171 
   172 lemma eventually_bot [simp]: "eventually P bot"
   173   unfolding bot_filter_def
   174   by (subst eventually_Abs_filter, rule is_filter.intro, auto)
   175 
   176 lemma eventually_sup:
   177   "eventually P (sup F F') \<longleftrightarrow> eventually P F \<and> eventually P F'"
   178   unfolding sup_filter_def
   179   by (rule eventually_Abs_filter, rule is_filter.intro)
   180      (auto elim!: eventually_rev_mp)
   181 
   182 lemma eventually_inf:
   183   "eventually P (inf F F') \<longleftrightarrow>
   184    (\<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   185   unfolding inf_filter_def
   186   apply (rule eventually_Abs_filter, rule is_filter.intro)
   187   apply (fast intro: eventually_True)
   188   apply clarify
   189   apply (intro exI conjI)
   190   apply (erule (1) eventually_conj)
   191   apply (erule (1) eventually_conj)
   192   apply simp
   193   apply auto
   194   done
   195 
   196 lemma eventually_Sup:
   197   "eventually P (Sup S) \<longleftrightarrow> (\<forall>F\<in>S. eventually P F)"
   198   unfolding Sup_filter_def
   199   apply (rule eventually_Abs_filter, rule is_filter.intro)
   200   apply (auto intro: eventually_conj elim!: eventually_rev_mp)
   201   done
   202 
   203 instance proof
   204   fix F F' F'' :: "'a filter" and S :: "'a filter set"
   205   { show "F < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   206     by (rule less_filter_def) }
   207   { show "F \<le> F"
   208     unfolding le_filter_def by simp }
   209   { assume "F \<le> F'" and "F' \<le> F''" thus "F \<le> F''"
   210     unfolding le_filter_def by simp }
   211   { assume "F \<le> F'" and "F' \<le> F" thus "F = F'"
   212     unfolding le_filter_def filter_eq_iff by fast }
   213   { show "F \<le> top"
   214     unfolding le_filter_def eventually_top by (simp add: always_eventually) }
   215   { show "bot \<le> F"
   216     unfolding le_filter_def by simp }
   217   { show "F \<le> sup F F'" and "F' \<le> sup F F'"
   218     unfolding le_filter_def eventually_sup by simp_all }
   219   { assume "F \<le> F''" and "F' \<le> F''" thus "sup F F' \<le> F''"
   220     unfolding le_filter_def eventually_sup by simp }
   221   { show "inf F F' \<le> F" and "inf F F' \<le> F'"
   222     unfolding le_filter_def eventually_inf by (auto intro: eventually_True) }
   223   { assume "F \<le> F'" and "F \<le> F''" thus "F \<le> inf F' F''"
   224     unfolding le_filter_def eventually_inf
   225     by (auto elim!: eventually_mono intro: eventually_conj) }
   226   { assume "F \<in> S" thus "F \<le> Sup S"
   227     unfolding le_filter_def eventually_Sup by simp }
   228   { assume "\<And>F. F \<in> S \<Longrightarrow> F \<le> F'" thus "Sup S \<le> F'"
   229     unfolding le_filter_def eventually_Sup by simp }
   230   { assume "F'' \<in> S" thus "Inf S \<le> F''"
   231     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   232   { assume "\<And>F'. F' \<in> S \<Longrightarrow> F \<le> F'" thus "F \<le> Inf S"
   233     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   234 qed
   235 
   236 end
   237 
   238 lemma filter_leD:
   239   "F \<le> F' \<Longrightarrow> eventually P F' \<Longrightarrow> eventually P F"
   240   unfolding le_filter_def by simp
   241 
   242 lemma filter_leI:
   243   "(\<And>P. eventually P F' \<Longrightarrow> eventually P F) \<Longrightarrow> F \<le> F'"
   244   unfolding le_filter_def by simp
   245 
   246 lemma eventually_False:
   247   "eventually (\<lambda>x. False) F \<longleftrightarrow> F = bot"
   248   unfolding filter_eq_iff by (auto elim: eventually_rev_mp)
   249 
   250 abbreviation (input) trivial_limit :: "'a filter \<Rightarrow> bool"
   251   where "trivial_limit F \<equiv> F = bot"
   252 
   253 lemma trivial_limit_def: "trivial_limit F \<longleftrightarrow> eventually (\<lambda>x. False) F"
   254   by (rule eventually_False [symmetric])
   255 
   256 
   257 subsection {* Map function for filters *}
   258 
   259 definition filtermap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a filter \<Rightarrow> 'b filter"
   260   where "filtermap f F = Abs_filter (\<lambda>P. eventually (\<lambda>x. P (f x)) F)"
   261 
   262 lemma eventually_filtermap:
   263   "eventually P (filtermap f F) = eventually (\<lambda>x. P (f x)) F"
   264   unfolding filtermap_def
   265   apply (rule eventually_Abs_filter)
   266   apply (rule is_filter.intro)
   267   apply (auto elim!: eventually_rev_mp)
   268   done
   269 
   270 lemma filtermap_ident: "filtermap (\<lambda>x. x) F = F"
   271   by (simp add: filter_eq_iff eventually_filtermap)
   272 
   273 lemma filtermap_filtermap:
   274   "filtermap f (filtermap g F) = filtermap (\<lambda>x. f (g x)) F"
   275   by (simp add: filter_eq_iff eventually_filtermap)
   276 
   277 lemma filtermap_mono: "F \<le> F' \<Longrightarrow> filtermap f F \<le> filtermap f F'"
   278   unfolding le_filter_def eventually_filtermap by simp
   279 
   280 lemma filtermap_bot [simp]: "filtermap f bot = bot"
   281   by (simp add: filter_eq_iff eventually_filtermap)
   282 
   283 subsection {* Order filters *}
   284 
   285 definition at_top :: "('a::order) filter"
   286   where "at_top = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   287 
   288 lemma eventually_at_top_linorder: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::linorder. \<forall>n\<ge>N. P n)"
   289   unfolding at_top_def
   290 proof (rule eventually_Abs_filter, rule is_filter.intro)
   291   fix P Q :: "'a \<Rightarrow> bool"
   292   assume "\<exists>i. \<forall>n\<ge>i. P n" and "\<exists>j. \<forall>n\<ge>j. Q n"
   293   then obtain i j where "\<forall>n\<ge>i. P n" and "\<forall>n\<ge>j. Q n" by auto
   294   then have "\<forall>n\<ge>max i j. P n \<and> Q n" by simp
   295   then show "\<exists>k. \<forall>n\<ge>k. P n \<and> Q n" ..
   296 qed auto
   297 
   298 lemma eventually_at_top_dense: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::dense_linorder. \<forall>n>N. P n)"
   299   unfolding eventually_at_top_linorder
   300 proof safe
   301   fix N assume "\<forall>n\<ge>N. P n" then show "\<exists>N. \<forall>n>N. P n" by (auto intro!: exI[of _ N])
   302 next
   303   fix N assume "\<forall>n>N. P n"
   304   moreover from gt_ex[of N] guess y ..
   305   ultimately show "\<exists>N. \<forall>n\<ge>N. P n" by (auto intro!: exI[of _ y])
   306 qed
   307 
   308 definition at_bot :: "('a::order) filter"
   309   where "at_bot = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<le>k. P n)"
   310 
   311 lemma eventually_at_bot_linorder:
   312   fixes P :: "'a::linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n\<le>N. P n)"
   313   unfolding at_bot_def
   314 proof (rule eventually_Abs_filter, rule is_filter.intro)
   315   fix P Q :: "'a \<Rightarrow> bool"
   316   assume "\<exists>i. \<forall>n\<le>i. P n" and "\<exists>j. \<forall>n\<le>j. Q n"
   317   then obtain i j where "\<forall>n\<le>i. P n" and "\<forall>n\<le>j. Q n" by auto
   318   then have "\<forall>n\<le>min i j. P n \<and> Q n" by simp
   319   then show "\<exists>k. \<forall>n\<le>k. P n \<and> Q n" ..
   320 qed auto
   321 
   322 lemma eventually_at_bot_dense:
   323   fixes P :: "'a::dense_linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n<N. P n)"
   324   unfolding eventually_at_bot_linorder
   325 proof safe
   326   fix N assume "\<forall>n\<le>N. P n" then show "\<exists>N. \<forall>n<N. P n" by (auto intro!: exI[of _ N])
   327 next
   328   fix N assume "\<forall>n<N. P n" 
   329   moreover from lt_ex[of N] guess y ..
   330   ultimately show "\<exists>N. \<forall>n\<le>N. P n" by (auto intro!: exI[of _ y])
   331 qed
   332 
   333 subsection {* Sequentially *}
   334 
   335 abbreviation sequentially :: "nat filter"
   336   where "sequentially == at_top"
   337 
   338 lemma sequentially_def: "sequentially = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   339   unfolding at_top_def by simp
   340 
   341 lemma eventually_sequentially:
   342   "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
   343   by (rule eventually_at_top_linorder)
   344 
   345 lemma sequentially_bot [simp, intro]: "sequentially \<noteq> bot"
   346   unfolding filter_eq_iff eventually_sequentially by auto
   347 
   348 lemmas trivial_limit_sequentially = sequentially_bot
   349 
   350 lemma eventually_False_sequentially [simp]:
   351   "\<not> eventually (\<lambda>n. False) sequentially"
   352   by (simp add: eventually_False)
   353 
   354 lemma le_sequentially:
   355   "F \<le> sequentially \<longleftrightarrow> (\<forall>N. eventually (\<lambda>n. N \<le> n) F)"
   356   unfolding le_filter_def eventually_sequentially
   357   by (safe, fast, drule_tac x=N in spec, auto elim: eventually_rev_mp)
   358 
   359 lemma eventually_sequentiallyI:
   360   assumes "\<And>x. c \<le> x \<Longrightarrow> P x"
   361   shows "eventually P sequentially"
   362 using assms by (auto simp: eventually_sequentially)
   363 
   364 
   365 subsection {* Standard filters *}
   366 
   367 definition within :: "'a filter \<Rightarrow> 'a set \<Rightarrow> 'a filter" (infixr "within" 70)
   368   where "F within S = Abs_filter (\<lambda>P. eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F)"
   369 
   370 definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
   371   where "nhds a = Abs_filter (\<lambda>P. \<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   372 
   373 definition (in topological_space) at :: "'a \<Rightarrow> 'a filter"
   374   where "at a = nhds a within - {a}"
   375 
   376 abbreviation at_right :: "'a\<Colon>{topological_space, order} \<Rightarrow> 'a filter" where
   377   "at_right x \<equiv> at x within {x <..}"
   378 
   379 abbreviation at_left :: "'a\<Colon>{topological_space, order} \<Rightarrow> 'a filter" where
   380   "at_left x \<equiv> at x within {..< x}"
   381 
   382 definition at_infinity :: "'a::real_normed_vector filter" where
   383   "at_infinity = Abs_filter (\<lambda>P. \<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x)"
   384 
   385 lemma eventually_within:
   386   "eventually P (F within S) = eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F"
   387   unfolding within_def
   388   by (rule eventually_Abs_filter, rule is_filter.intro)
   389      (auto elim!: eventually_rev_mp)
   390 
   391 lemma within_UNIV [simp]: "F within UNIV = F"
   392   unfolding filter_eq_iff eventually_within by simp
   393 
   394 lemma within_empty [simp]: "F within {} = bot"
   395   unfolding filter_eq_iff eventually_within by simp
   396 
   397 lemma within_le: "F within S \<le> F"
   398   unfolding le_filter_def eventually_within by (auto elim: eventually_elim1)
   399 
   400 lemma le_withinI: "F \<le> F' \<Longrightarrow> eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S"
   401   unfolding le_filter_def eventually_within by (auto elim: eventually_elim2)
   402 
   403 lemma le_within_iff: "eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S \<longleftrightarrow> F \<le> F'"
   404   by (blast intro: within_le le_withinI order_trans)
   405 
   406 lemma eventually_nhds:
   407   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   408 unfolding nhds_def
   409 proof (rule eventually_Abs_filter, rule is_filter.intro)
   410   have "open UNIV \<and> a \<in> UNIV \<and> (\<forall>x\<in>UNIV. True)" by simp
   411   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. True)" ..
   412 next
   413   fix P Q
   414   assume "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   415      and "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)"
   416   then obtain S T where
   417     "open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   418     "open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)" by auto
   419   hence "open (S \<inter> T) \<and> a \<in> S \<inter> T \<and> (\<forall>x\<in>(S \<inter> T). P x \<and> Q x)"
   420     by (simp add: open_Int)
   421   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x \<and> Q x)" ..
   422 qed auto
   423 
   424 lemma eventually_nhds_metric:
   425   "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
   426 unfolding eventually_nhds open_dist
   427 apply safe
   428 apply fast
   429 apply (rule_tac x="{x. dist x a < d}" in exI, simp)
   430 apply clarsimp
   431 apply (rule_tac x="d - dist x a" in exI, clarsimp)
   432 apply (simp only: less_diff_eq)
   433 apply (erule le_less_trans [OF dist_triangle])
   434 done
   435 
   436 lemma nhds_neq_bot [simp]: "nhds a \<noteq> bot"
   437   unfolding trivial_limit_def eventually_nhds by simp
   438 
   439 lemma eventually_at_topological:
   440   "eventually P (at a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
   441 unfolding at_def eventually_within eventually_nhds by simp
   442 
   443 lemma eventually_at:
   444   fixes a :: "'a::metric_space"
   445   shows "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
   446 unfolding at_def eventually_within eventually_nhds_metric by auto
   447 
   448 lemma at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
   449   unfolding trivial_limit_def eventually_at_topological
   450   by (safe, case_tac "S = {a}", simp, fast, fast)
   451 
   452 lemma at_neq_bot [simp]: "at (a::'a::perfect_space) \<noteq> bot"
   453   by (simp add: at_eq_bot_iff not_open_singleton)
   454 
   455 lemma eventually_at_infinity:
   456   "eventually P at_infinity \<longleftrightarrow> (\<exists>b. \<forall>x. b \<le> norm x \<longrightarrow> P x)"
   457 unfolding at_infinity_def
   458 proof (rule eventually_Abs_filter, rule is_filter.intro)
   459   fix P Q :: "'a \<Rightarrow> bool"
   460   assume "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<exists>s. \<forall>x. s \<le> norm x \<longrightarrow> Q x"
   461   then obtain r s where
   462     "\<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<forall>x. s \<le> norm x \<longrightarrow> Q x" by auto
   463   then have "\<forall>x. max r s \<le> norm x \<longrightarrow> P x \<and> Q x" by simp
   464   then show "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x \<and> Q x" ..
   465 qed auto
   466 
   467 lemma at_infinity_eq_at_top_bot:
   468   "(at_infinity \<Colon> real filter) = sup at_top at_bot"
   469   unfolding sup_filter_def at_infinity_def eventually_at_top_linorder eventually_at_bot_linorder
   470 proof (intro arg_cong[where f=Abs_filter] ext iffI)
   471   fix P :: "real \<Rightarrow> bool" assume "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x"
   472   then guess r ..
   473   then have "(\<forall>x\<ge>r. P x) \<and> (\<forall>x\<le>-r. P x)" by auto
   474   then show "(\<exists>r. \<forall>x\<ge>r. P x) \<and> (\<exists>r. \<forall>x\<le>r. P x)" by auto
   475 next
   476   fix P :: "real \<Rightarrow> bool" assume "(\<exists>r. \<forall>x\<ge>r. P x) \<and> (\<exists>r. \<forall>x\<le>r. P x)"
   477   then obtain p q where "\<forall>x\<ge>p. P x" "\<forall>x\<le>q. P x" by auto
   478   then show "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x"
   479     by (intro exI[of _ "max p (-q)"])
   480        (auto simp: abs_real_def)
   481 qed
   482 
   483 lemma at_top_le_at_infinity:
   484   "at_top \<le> (at_infinity :: real filter)"
   485   unfolding at_infinity_eq_at_top_bot by simp
   486 
   487 lemma at_bot_le_at_infinity:
   488   "at_bot \<le> (at_infinity :: real filter)"
   489   unfolding at_infinity_eq_at_top_bot by simp
   490 
   491 subsection {* Boundedness *}
   492 
   493 definition Bfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   494   where "Bfun f F = (\<exists>K>0. eventually (\<lambda>x. norm (f x) \<le> K) F)"
   495 
   496 lemma BfunI:
   497   assumes K: "eventually (\<lambda>x. norm (f x) \<le> K) F" shows "Bfun f F"
   498 unfolding Bfun_def
   499 proof (intro exI conjI allI)
   500   show "0 < max K 1" by simp
   501 next
   502   show "eventually (\<lambda>x. norm (f x) \<le> max K 1) F"
   503     using K by (rule eventually_elim1, simp)
   504 qed
   505 
   506 lemma BfunE:
   507   assumes "Bfun f F"
   508   obtains B where "0 < B" and "eventually (\<lambda>x. norm (f x) \<le> B) F"
   509 using assms unfolding Bfun_def by fast
   510 
   511 
   512 subsection {* Convergence to Zero *}
   513 
   514 definition Zfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   515   where "Zfun f F = (\<forall>r>0. eventually (\<lambda>x. norm (f x) < r) F)"
   516 
   517 lemma ZfunI:
   518   "(\<And>r. 0 < r \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F) \<Longrightarrow> Zfun f F"
   519   unfolding Zfun_def by simp
   520 
   521 lemma ZfunD:
   522   "\<lbrakk>Zfun f F; 0 < r\<rbrakk> \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F"
   523   unfolding Zfun_def by simp
   524 
   525 lemma Zfun_ssubst:
   526   "eventually (\<lambda>x. f x = g x) F \<Longrightarrow> Zfun g F \<Longrightarrow> Zfun f F"
   527   unfolding Zfun_def by (auto elim!: eventually_rev_mp)
   528 
   529 lemma Zfun_zero: "Zfun (\<lambda>x. 0) F"
   530   unfolding Zfun_def by simp
   531 
   532 lemma Zfun_norm_iff: "Zfun (\<lambda>x. norm (f x)) F = Zfun (\<lambda>x. f x) F"
   533   unfolding Zfun_def by simp
   534 
   535 lemma Zfun_imp_Zfun:
   536   assumes f: "Zfun f F"
   537   assumes g: "eventually (\<lambda>x. norm (g x) \<le> norm (f x) * K) F"
   538   shows "Zfun (\<lambda>x. g x) F"
   539 proof (cases)
   540   assume K: "0 < K"
   541   show ?thesis
   542   proof (rule ZfunI)
   543     fix r::real assume "0 < r"
   544     hence "0 < r / K"
   545       using K by (rule divide_pos_pos)
   546     then have "eventually (\<lambda>x. norm (f x) < r / K) F"
   547       using ZfunD [OF f] by fast
   548     with g show "eventually (\<lambda>x. norm (g x) < r) F"
   549     proof eventually_elim
   550       case (elim x)
   551       hence "norm (f x) * K < r"
   552         by (simp add: pos_less_divide_eq K)
   553       thus ?case
   554         by (simp add: order_le_less_trans [OF elim(1)])
   555     qed
   556   qed
   557 next
   558   assume "\<not> 0 < K"
   559   hence K: "K \<le> 0" by (simp only: not_less)
   560   show ?thesis
   561   proof (rule ZfunI)
   562     fix r :: real
   563     assume "0 < r"
   564     from g show "eventually (\<lambda>x. norm (g x) < r) F"
   565     proof eventually_elim
   566       case (elim x)
   567       also have "norm (f x) * K \<le> norm (f x) * 0"
   568         using K norm_ge_zero by (rule mult_left_mono)
   569       finally show ?case
   570         using `0 < r` by simp
   571     qed
   572   qed
   573 qed
   574 
   575 lemma Zfun_le: "\<lbrakk>Zfun g F; \<forall>x. norm (f x) \<le> norm (g x)\<rbrakk> \<Longrightarrow> Zfun f F"
   576   by (erule_tac K="1" in Zfun_imp_Zfun, simp)
   577 
   578 lemma Zfun_add:
   579   assumes f: "Zfun f F" and g: "Zfun g F"
   580   shows "Zfun (\<lambda>x. f x + g x) F"
   581 proof (rule ZfunI)
   582   fix r::real assume "0 < r"
   583   hence r: "0 < r / 2" by simp
   584   have "eventually (\<lambda>x. norm (f x) < r/2) F"
   585     using f r by (rule ZfunD)
   586   moreover
   587   have "eventually (\<lambda>x. norm (g x) < r/2) F"
   588     using g r by (rule ZfunD)
   589   ultimately
   590   show "eventually (\<lambda>x. norm (f x + g x) < r) F"
   591   proof eventually_elim
   592     case (elim x)
   593     have "norm (f x + g x) \<le> norm (f x) + norm (g x)"
   594       by (rule norm_triangle_ineq)
   595     also have "\<dots> < r/2 + r/2"
   596       using elim by (rule add_strict_mono)
   597     finally show ?case
   598       by simp
   599   qed
   600 qed
   601 
   602 lemma Zfun_minus: "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. - f x) F"
   603   unfolding Zfun_def by simp
   604 
   605 lemma Zfun_diff: "\<lbrakk>Zfun f F; Zfun g F\<rbrakk> \<Longrightarrow> Zfun (\<lambda>x. f x - g x) F"
   606   by (simp only: diff_minus Zfun_add Zfun_minus)
   607 
   608 lemma (in bounded_linear) Zfun:
   609   assumes g: "Zfun g F"
   610   shows "Zfun (\<lambda>x. f (g x)) F"
   611 proof -
   612   obtain K where "\<And>x. norm (f x) \<le> norm x * K"
   613     using bounded by fast
   614   then have "eventually (\<lambda>x. norm (f (g x)) \<le> norm (g x) * K) F"
   615     by simp
   616   with g show ?thesis
   617     by (rule Zfun_imp_Zfun)
   618 qed
   619 
   620 lemma (in bounded_bilinear) Zfun:
   621   assumes f: "Zfun f F"
   622   assumes g: "Zfun g F"
   623   shows "Zfun (\<lambda>x. f x ** g x) F"
   624 proof (rule ZfunI)
   625   fix r::real assume r: "0 < r"
   626   obtain K where K: "0 < K"
   627     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   628     using pos_bounded by fast
   629   from K have K': "0 < inverse K"
   630     by (rule positive_imp_inverse_positive)
   631   have "eventually (\<lambda>x. norm (f x) < r) F"
   632     using f r by (rule ZfunD)
   633   moreover
   634   have "eventually (\<lambda>x. norm (g x) < inverse K) F"
   635     using g K' by (rule ZfunD)
   636   ultimately
   637   show "eventually (\<lambda>x. norm (f x ** g x) < r) F"
   638   proof eventually_elim
   639     case (elim x)
   640     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   641       by (rule norm_le)
   642     also have "norm (f x) * norm (g x) * K < r * inverse K * K"
   643       by (intro mult_strict_right_mono mult_strict_mono' norm_ge_zero elim K)
   644     also from K have "r * inverse K * K = r"
   645       by simp
   646     finally show ?case .
   647   qed
   648 qed
   649 
   650 lemma (in bounded_bilinear) Zfun_left:
   651   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. f x ** a) F"
   652   by (rule bounded_linear_left [THEN bounded_linear.Zfun])
   653 
   654 lemma (in bounded_bilinear) Zfun_right:
   655   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. a ** f x) F"
   656   by (rule bounded_linear_right [THEN bounded_linear.Zfun])
   657 
   658 lemmas Zfun_mult = bounded_bilinear.Zfun [OF bounded_bilinear_mult]
   659 lemmas Zfun_mult_right = bounded_bilinear.Zfun_right [OF bounded_bilinear_mult]
   660 lemmas Zfun_mult_left = bounded_bilinear.Zfun_left [OF bounded_bilinear_mult]
   661 
   662 
   663 subsection {* Limits *}
   664 
   665 definition filterlim :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b filter \<Rightarrow> 'a filter \<Rightarrow> bool" where
   666   "filterlim f F2 F1 \<longleftrightarrow> filtermap f F1 \<le> F2"
   667 
   668 syntax
   669   "_LIM" :: "pttrns \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> bool" ("(3LIM (_)/ (_)./ (_) :> (_))" [1000, 10, 0, 10] 10)
   670 
   671 translations
   672   "LIM x F1. f :> F2"   == "CONST filterlim (%x. f) F2 F1"
   673 
   674 lemma filterlim_iff:
   675   "(LIM x F1. f x :> F2) \<longleftrightarrow> (\<forall>P. eventually P F2 \<longrightarrow> eventually (\<lambda>x. P (f x)) F1)"
   676   unfolding filterlim_def le_filter_def eventually_filtermap ..
   677 
   678 lemma filterlim_compose: 
   679   "filterlim g F3 F2 \<Longrightarrow> filterlim f F2 F1 \<Longrightarrow> filterlim (\<lambda>x. g (f x)) F3 F1"
   680   unfolding filterlim_def filtermap_filtermap[symmetric] by (metis filtermap_mono order_trans)
   681 
   682 lemma filterlim_mono: 
   683   "filterlim f F2 F1 \<Longrightarrow> F2 \<le> F2' \<Longrightarrow> F1' \<le> F1 \<Longrightarrow> filterlim f F2' F1'"
   684   unfolding filterlim_def by (metis filtermap_mono order_trans)
   685 
   686 lemma filterlim_within:
   687   "(LIM x F1. f x :> F2 within S) \<longleftrightarrow> (eventually (\<lambda>x. f x \<in> S) F1 \<and> (LIM x F1. f x :> F2))"
   688   unfolding filterlim_def
   689 proof safe
   690   assume "filtermap f F1 \<le> F2 within S" then show "eventually (\<lambda>x. f x \<in> S) F1"
   691     by (auto simp: le_filter_def eventually_filtermap eventually_within elim!: allE[of _ "\<lambda>x. x \<in> S"])
   692 qed (auto intro: within_le order_trans simp: le_within_iff eventually_filtermap)
   693 
   694 abbreviation (in topological_space)
   695   tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool" (infixr "--->" 55) where
   696   "(f ---> l) F \<equiv> filterlim f (nhds l) F"
   697 
   698 ML {*
   699 structure Tendsto_Intros = Named_Thms
   700 (
   701   val name = @{binding tendsto_intros}
   702   val description = "introduction rules for tendsto"
   703 )
   704 *}
   705 
   706 setup Tendsto_Intros.setup
   707 
   708 lemma tendsto_def: "(f ---> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
   709   unfolding filterlim_def
   710 proof safe
   711   fix S assume "open S" "l \<in> S" "filtermap f F \<le> nhds l"
   712   then show "eventually (\<lambda>x. f x \<in> S) F"
   713     unfolding eventually_nhds eventually_filtermap le_filter_def
   714     by (auto elim!: allE[of _ "\<lambda>x. x \<in> S"] eventually_rev_mp)
   715 qed (auto elim!: eventually_rev_mp simp: eventually_nhds eventually_filtermap le_filter_def)
   716 
   717 lemma filterlim_at:
   718   "(LIM x F. f x :> at b) \<longleftrightarrow> (eventually (\<lambda>x. f x \<noteq> b) F \<and> (f ---> b) F)"
   719   by (simp add: at_def filterlim_within)
   720 
   721 lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f ---> l) F' \<Longrightarrow> (f ---> l) F"
   722   unfolding tendsto_def le_filter_def by fast
   723 
   724 lemma topological_tendstoI:
   725   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F)
   726     \<Longrightarrow> (f ---> l) F"
   727   unfolding tendsto_def by auto
   728 
   729 lemma topological_tendstoD:
   730   "(f ---> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
   731   unfolding tendsto_def by auto
   732 
   733 lemma tendstoI:
   734   assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
   735   shows "(f ---> l) F"
   736   apply (rule topological_tendstoI)
   737   apply (simp add: open_dist)
   738   apply (drule (1) bspec, clarify)
   739   apply (drule assms)
   740   apply (erule eventually_elim1, simp)
   741   done
   742 
   743 lemma tendstoD:
   744   "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
   745   apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
   746   apply (clarsimp simp add: open_dist)
   747   apply (rule_tac x="e - dist x l" in exI, clarsimp)
   748   apply (simp only: less_diff_eq)
   749   apply (erule le_less_trans [OF dist_triangle])
   750   apply simp
   751   apply simp
   752   done
   753 
   754 lemma tendsto_iff:
   755   "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
   756   using tendstoI tendstoD by fast
   757 
   758 lemma tendsto_Zfun_iff: "(f ---> a) F = Zfun (\<lambda>x. f x - a) F"
   759   by (simp only: tendsto_iff Zfun_def dist_norm)
   760 
   761 lemma tendsto_bot [simp]: "(f ---> a) bot"
   762   unfolding tendsto_def by simp
   763 
   764 lemma tendsto_ident_at [tendsto_intros]: "((\<lambda>x. x) ---> a) (at a)"
   765   unfolding tendsto_def eventually_at_topological by auto
   766 
   767 lemma tendsto_ident_at_within [tendsto_intros]:
   768   "((\<lambda>x. x) ---> a) (at a within S)"
   769   unfolding tendsto_def eventually_within eventually_at_topological by auto
   770 
   771 lemma tendsto_const [tendsto_intros]: "((\<lambda>x. k) ---> k) F"
   772   by (simp add: tendsto_def)
   773 
   774 lemma tendsto_unique:
   775   fixes f :: "'a \<Rightarrow> 'b::t2_space"
   776   assumes "\<not> trivial_limit F" and "(f ---> a) F" and "(f ---> b) F"
   777   shows "a = b"
   778 proof (rule ccontr)
   779   assume "a \<noteq> b"
   780   obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
   781     using hausdorff [OF `a \<noteq> b`] by fast
   782   have "eventually (\<lambda>x. f x \<in> U) F"
   783     using `(f ---> a) F` `open U` `a \<in> U` by (rule topological_tendstoD)
   784   moreover
   785   have "eventually (\<lambda>x. f x \<in> V) F"
   786     using `(f ---> b) F` `open V` `b \<in> V` by (rule topological_tendstoD)
   787   ultimately
   788   have "eventually (\<lambda>x. False) F"
   789   proof eventually_elim
   790     case (elim x)
   791     hence "f x \<in> U \<inter> V" by simp
   792     with `U \<inter> V = {}` show ?case by simp
   793   qed
   794   with `\<not> trivial_limit F` show "False"
   795     by (simp add: trivial_limit_def)
   796 qed
   797 
   798 lemma tendsto_const_iff:
   799   fixes a b :: "'a::t2_space"
   800   assumes "\<not> trivial_limit F" shows "((\<lambda>x. a) ---> b) F \<longleftrightarrow> a = b"
   801   by (safe intro!: tendsto_const tendsto_unique [OF assms tendsto_const])
   802 
   803 lemma tendsto_at_iff_tendsto_nhds:
   804   "(g ---> g l) (at l) \<longleftrightarrow> (g ---> g l) (nhds l)"
   805   unfolding tendsto_def at_def eventually_within
   806   by (intro ext all_cong imp_cong) (auto elim!: eventually_elim1)
   807 
   808 lemma tendsto_compose:
   809   "(g ---> g l) (at l) \<Longrightarrow> (f ---> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> g l) F"
   810   unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])
   811 
   812 lemma tendsto_compose_eventually:
   813   "(g ---> m) (at l) \<Longrightarrow> (f ---> l) F \<Longrightarrow> eventually (\<lambda>x. f x \<noteq> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> m) F"
   814   by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)
   815 
   816 lemma metric_tendsto_imp_tendsto:
   817   assumes f: "(f ---> a) F"
   818   assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
   819   shows "(g ---> b) F"
   820 proof (rule tendstoI)
   821   fix e :: real assume "0 < e"
   822   with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
   823   with le show "eventually (\<lambda>x. dist (g x) b < e) F"
   824     using le_less_trans by (rule eventually_elim2)
   825 qed
   826 
   827 subsubsection {* Distance and norms *}
   828 
   829 lemma tendsto_dist [tendsto_intros]:
   830   assumes f: "(f ---> l) F" and g: "(g ---> m) F"
   831   shows "((\<lambda>x. dist (f x) (g x)) ---> dist l m) F"
   832 proof (rule tendstoI)
   833   fix e :: real assume "0 < e"
   834   hence e2: "0 < e/2" by simp
   835   from tendstoD [OF f e2] tendstoD [OF g e2]
   836   show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) F"
   837   proof (eventually_elim)
   838     case (elim x)
   839     then show "dist (dist (f x) (g x)) (dist l m) < e"
   840       unfolding dist_real_def
   841       using dist_triangle2 [of "f x" "g x" "l"]
   842       using dist_triangle2 [of "g x" "l" "m"]
   843       using dist_triangle3 [of "l" "m" "f x"]
   844       using dist_triangle [of "f x" "m" "g x"]
   845       by arith
   846   qed
   847 qed
   848 
   849 lemma norm_conv_dist: "norm x = dist x 0"
   850   unfolding dist_norm by simp
   851 
   852 lemma tendsto_norm [tendsto_intros]:
   853   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> norm a) F"
   854   unfolding norm_conv_dist by (intro tendsto_intros)
   855 
   856 lemma tendsto_norm_zero:
   857   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> 0) F"
   858   by (drule tendsto_norm, simp)
   859 
   860 lemma tendsto_norm_zero_cancel:
   861   "((\<lambda>x. norm (f x)) ---> 0) F \<Longrightarrow> (f ---> 0) F"
   862   unfolding tendsto_iff dist_norm by simp
   863 
   864 lemma tendsto_norm_zero_iff:
   865   "((\<lambda>x. norm (f x)) ---> 0) F \<longleftrightarrow> (f ---> 0) F"
   866   unfolding tendsto_iff dist_norm by simp
   867 
   868 lemma tendsto_rabs [tendsto_intros]:
   869   "(f ---> (l::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> \<bar>l\<bar>) F"
   870   by (fold real_norm_def, rule tendsto_norm)
   871 
   872 lemma tendsto_rabs_zero:
   873   "(f ---> (0::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> 0) F"
   874   by (fold real_norm_def, rule tendsto_norm_zero)
   875 
   876 lemma tendsto_rabs_zero_cancel:
   877   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<Longrightarrow> (f ---> 0) F"
   878   by (fold real_norm_def, rule tendsto_norm_zero_cancel)
   879 
   880 lemma tendsto_rabs_zero_iff:
   881   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<longleftrightarrow> (f ---> 0) F"
   882   by (fold real_norm_def, rule tendsto_norm_zero_iff)
   883 
   884 subsubsection {* Addition and subtraction *}
   885 
   886 lemma tendsto_add [tendsto_intros]:
   887   fixes a b :: "'a::real_normed_vector"
   888   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> a + b) F"
   889   by (simp only: tendsto_Zfun_iff add_diff_add Zfun_add)
   890 
   891 lemma tendsto_add_zero:
   892   fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
   893   shows "\<lbrakk>(f ---> 0) F; (g ---> 0) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> 0) F"
   894   by (drule (1) tendsto_add, simp)
   895 
   896 lemma tendsto_minus [tendsto_intros]:
   897   fixes a :: "'a::real_normed_vector"
   898   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. - f x) ---> - a) F"
   899   by (simp only: tendsto_Zfun_iff minus_diff_minus Zfun_minus)
   900 
   901 lemma tendsto_minus_cancel:
   902   fixes a :: "'a::real_normed_vector"
   903   shows "((\<lambda>x. - f x) ---> - a) F \<Longrightarrow> (f ---> a) F"
   904   by (drule tendsto_minus, simp)
   905 
   906 lemma tendsto_diff [tendsto_intros]:
   907   fixes a b :: "'a::real_normed_vector"
   908   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x - g x) ---> a - b) F"
   909   by (simp add: diff_minus tendsto_add tendsto_minus)
   910 
   911 lemma tendsto_setsum [tendsto_intros]:
   912   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::real_normed_vector"
   913   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> a i) F"
   914   shows "((\<lambda>x. \<Sum>i\<in>S. f i x) ---> (\<Sum>i\<in>S. a i)) F"
   915 proof (cases "finite S")
   916   assume "finite S" thus ?thesis using assms
   917     by (induct, simp add: tendsto_const, simp add: tendsto_add)
   918 next
   919   assume "\<not> finite S" thus ?thesis
   920     by (simp add: tendsto_const)
   921 qed
   922 
   923 lemma real_tendsto_sandwich:
   924   fixes f g h :: "'a \<Rightarrow> real"
   925   assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
   926   assumes lim: "(f ---> c) net" "(h ---> c) net"
   927   shows "(g ---> c) net"
   928 proof -
   929   have "((\<lambda>n. g n - f n) ---> 0) net"
   930   proof (rule metric_tendsto_imp_tendsto)
   931     show "eventually (\<lambda>n. dist (g n - f n) 0 \<le> dist (h n - f n) 0) net"
   932       using ev by (rule eventually_elim2) (simp add: dist_real_def)
   933     show "((\<lambda>n. h n - f n) ---> 0) net"
   934       using tendsto_diff[OF lim(2,1)] by simp
   935   qed
   936   from tendsto_add[OF this lim(1)] show ?thesis by simp
   937 qed
   938 
   939 subsubsection {* Linear operators and multiplication *}
   940 
   941 lemma (in bounded_linear) tendsto:
   942   "(g ---> a) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> f a) F"
   943   by (simp only: tendsto_Zfun_iff diff [symmetric] Zfun)
   944 
   945 lemma (in bounded_linear) tendsto_zero:
   946   "(g ---> 0) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> 0) F"
   947   by (drule tendsto, simp only: zero)
   948 
   949 lemma (in bounded_bilinear) tendsto:
   950   "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x ** g x) ---> a ** b) F"
   951   by (simp only: tendsto_Zfun_iff prod_diff_prod
   952                  Zfun_add Zfun Zfun_left Zfun_right)
   953 
   954 lemma (in bounded_bilinear) tendsto_zero:
   955   assumes f: "(f ---> 0) F"
   956   assumes g: "(g ---> 0) F"
   957   shows "((\<lambda>x. f x ** g x) ---> 0) F"
   958   using tendsto [OF f g] by (simp add: zero_left)
   959 
   960 lemma (in bounded_bilinear) tendsto_left_zero:
   961   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. f x ** c) ---> 0) F"
   962   by (rule bounded_linear.tendsto_zero [OF bounded_linear_left])
   963 
   964 lemma (in bounded_bilinear) tendsto_right_zero:
   965   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. c ** f x) ---> 0) F"
   966   by (rule bounded_linear.tendsto_zero [OF bounded_linear_right])
   967 
   968 lemmas tendsto_of_real [tendsto_intros] =
   969   bounded_linear.tendsto [OF bounded_linear_of_real]
   970 
   971 lemmas tendsto_scaleR [tendsto_intros] =
   972   bounded_bilinear.tendsto [OF bounded_bilinear_scaleR]
   973 
   974 lemmas tendsto_mult [tendsto_intros] =
   975   bounded_bilinear.tendsto [OF bounded_bilinear_mult]
   976 
   977 lemmas tendsto_mult_zero =
   978   bounded_bilinear.tendsto_zero [OF bounded_bilinear_mult]
   979 
   980 lemmas tendsto_mult_left_zero =
   981   bounded_bilinear.tendsto_left_zero [OF bounded_bilinear_mult]
   982 
   983 lemmas tendsto_mult_right_zero =
   984   bounded_bilinear.tendsto_right_zero [OF bounded_bilinear_mult]
   985 
   986 lemma tendsto_power [tendsto_intros]:
   987   fixes f :: "'a \<Rightarrow> 'b::{power,real_normed_algebra}"
   988   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. f x ^ n) ---> a ^ n) F"
   989   by (induct n) (simp_all add: tendsto_const tendsto_mult)
   990 
   991 lemma tendsto_setprod [tendsto_intros]:
   992   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::{real_normed_algebra,comm_ring_1}"
   993   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> L i) F"
   994   shows "((\<lambda>x. \<Prod>i\<in>S. f i x) ---> (\<Prod>i\<in>S. L i)) F"
   995 proof (cases "finite S")
   996   assume "finite S" thus ?thesis using assms
   997     by (induct, simp add: tendsto_const, simp add: tendsto_mult)
   998 next
   999   assume "\<not> finite S" thus ?thesis
  1000     by (simp add: tendsto_const)
  1001 qed
  1002 
  1003 subsubsection {* Inverse and division *}
  1004 
  1005 lemma (in bounded_bilinear) Zfun_prod_Bfun:
  1006   assumes f: "Zfun f F"
  1007   assumes g: "Bfun g F"
  1008   shows "Zfun (\<lambda>x. f x ** g x) F"
  1009 proof -
  1010   obtain K where K: "0 \<le> K"
  1011     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
  1012     using nonneg_bounded by fast
  1013   obtain B where B: "0 < B"
  1014     and norm_g: "eventually (\<lambda>x. norm (g x) \<le> B) F"
  1015     using g by (rule BfunE)
  1016   have "eventually (\<lambda>x. norm (f x ** g x) \<le> norm (f x) * (B * K)) F"
  1017   using norm_g proof eventually_elim
  1018     case (elim x)
  1019     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
  1020       by (rule norm_le)
  1021     also have "\<dots> \<le> norm (f x) * B * K"
  1022       by (intro mult_mono' order_refl norm_g norm_ge_zero
  1023                 mult_nonneg_nonneg K elim)
  1024     also have "\<dots> = norm (f x) * (B * K)"
  1025       by (rule mult_assoc)
  1026     finally show "norm (f x ** g x) \<le> norm (f x) * (B * K)" .
  1027   qed
  1028   with f show ?thesis
  1029     by (rule Zfun_imp_Zfun)
  1030 qed
  1031 
  1032 lemma (in bounded_bilinear) flip:
  1033   "bounded_bilinear (\<lambda>x y. y ** x)"
  1034   apply default
  1035   apply (rule add_right)
  1036   apply (rule add_left)
  1037   apply (rule scaleR_right)
  1038   apply (rule scaleR_left)
  1039   apply (subst mult_commute)
  1040   using bounded by fast
  1041 
  1042 lemma (in bounded_bilinear) Bfun_prod_Zfun:
  1043   assumes f: "Bfun f F"
  1044   assumes g: "Zfun g F"
  1045   shows "Zfun (\<lambda>x. f x ** g x) F"
  1046   using flip g f by (rule bounded_bilinear.Zfun_prod_Bfun)
  1047 
  1048 lemma Bfun_inverse_lemma:
  1049   fixes x :: "'a::real_normed_div_algebra"
  1050   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
  1051   apply (subst nonzero_norm_inverse, clarsimp)
  1052   apply (erule (1) le_imp_inverse_le)
  1053   done
  1054 
  1055 lemma Bfun_inverse:
  1056   fixes a :: "'a::real_normed_div_algebra"
  1057   assumes f: "(f ---> a) F"
  1058   assumes a: "a \<noteq> 0"
  1059   shows "Bfun (\<lambda>x. inverse (f x)) F"
  1060 proof -
  1061   from a have "0 < norm a" by simp
  1062   hence "\<exists>r>0. r < norm a" by (rule dense)
  1063   then obtain r where r1: "0 < r" and r2: "r < norm a" by fast
  1064   have "eventually (\<lambda>x. dist (f x) a < r) F"
  1065     using tendstoD [OF f r1] by fast
  1066   hence "eventually (\<lambda>x. norm (inverse (f x)) \<le> inverse (norm a - r)) F"
  1067   proof eventually_elim
  1068     case (elim x)
  1069     hence 1: "norm (f x - a) < r"
  1070       by (simp add: dist_norm)
  1071     hence 2: "f x \<noteq> 0" using r2 by auto
  1072     hence "norm (inverse (f x)) = inverse (norm (f x))"
  1073       by (rule nonzero_norm_inverse)
  1074     also have "\<dots> \<le> inverse (norm a - r)"
  1075     proof (rule le_imp_inverse_le)
  1076       show "0 < norm a - r" using r2 by simp
  1077     next
  1078       have "norm a - norm (f x) \<le> norm (a - f x)"
  1079         by (rule norm_triangle_ineq2)
  1080       also have "\<dots> = norm (f x - a)"
  1081         by (rule norm_minus_commute)
  1082       also have "\<dots> < r" using 1 .
  1083       finally show "norm a - r \<le> norm (f x)" by simp
  1084     qed
  1085     finally show "norm (inverse (f x)) \<le> inverse (norm a - r)" .
  1086   qed
  1087   thus ?thesis by (rule BfunI)
  1088 qed
  1089 
  1090 lemma tendsto_inverse [tendsto_intros]:
  1091   fixes a :: "'a::real_normed_div_algebra"
  1092   assumes f: "(f ---> a) F"
  1093   assumes a: "a \<noteq> 0"
  1094   shows "((\<lambda>x. inverse (f x)) ---> inverse a) F"
  1095 proof -
  1096   from a have "0 < norm a" by simp
  1097   with f have "eventually (\<lambda>x. dist (f x) a < norm a) F"
  1098     by (rule tendstoD)
  1099   then have "eventually (\<lambda>x. f x \<noteq> 0) F"
  1100     unfolding dist_norm by (auto elim!: eventually_elim1)
  1101   with a have "eventually (\<lambda>x. inverse (f x) - inverse a =
  1102     - (inverse (f x) * (f x - a) * inverse a)) F"
  1103     by (auto elim!: eventually_elim1 simp: inverse_diff_inverse)
  1104   moreover have "Zfun (\<lambda>x. - (inverse (f x) * (f x - a) * inverse a)) F"
  1105     by (intro Zfun_minus Zfun_mult_left
  1106       bounded_bilinear.Bfun_prod_Zfun [OF bounded_bilinear_mult]
  1107       Bfun_inverse [OF f a] f [unfolded tendsto_Zfun_iff])
  1108   ultimately show ?thesis
  1109     unfolding tendsto_Zfun_iff by (rule Zfun_ssubst)
  1110 qed
  1111 
  1112 lemma tendsto_divide [tendsto_intros]:
  1113   fixes a b :: "'a::real_normed_field"
  1114   shows "\<lbrakk>(f ---> a) F; (g ---> b) F; b \<noteq> 0\<rbrakk>
  1115     \<Longrightarrow> ((\<lambda>x. f x / g x) ---> a / b) F"
  1116   by (simp add: tendsto_mult tendsto_inverse divide_inverse)
  1117 
  1118 lemma tendsto_sgn [tendsto_intros]:
  1119   fixes l :: "'a::real_normed_vector"
  1120   shows "\<lbrakk>(f ---> l) F; l \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. sgn (f x)) ---> sgn l) F"
  1121   unfolding sgn_div_norm by (simp add: tendsto_intros)
  1122 
  1123 subsection {* Limits to @{const at_top} and @{const at_bot} *}
  1124 
  1125 lemma filterlim_at_top:
  1126   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)"
  1127   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z < f x) F)"
  1128   by (auto simp: filterlim_iff eventually_at_top_dense elim!: eventually_elim1)
  1129 
  1130 lemma filterlim_at_top_gt:
  1131   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
  1132   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z>c. eventually (\<lambda>x. Z < f x) F)"
  1133   unfolding filterlim_at_top
  1134 proof safe
  1135   fix Z assume *: "\<forall>Z>c. eventually (\<lambda>x. Z < f x) F"
  1136   from gt_ex[of "max Z c"] guess x ..
  1137   with *[THEN spec, of x] show "eventually (\<lambda>x. Z < f x) F"
  1138     by (auto elim!: eventually_elim1)
  1139 qed simp
  1140 
  1141 lemma filterlim_at_bot: 
  1142   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)"
  1143   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. f x < Z) F)"
  1144   by (auto simp: filterlim_iff eventually_at_bot_dense elim!: eventually_elim1)
  1145 
  1146 lemma filterlim_at_bot_lt:
  1147   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
  1148   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z<c. eventually (\<lambda>x. Z > f x) F)"
  1149   unfolding filterlim_at_bot
  1150 proof safe
  1151   fix Z assume *: "\<forall>Z<c. eventually (\<lambda>x. Z > f x) F"
  1152   from lt_ex[of "min Z c"] guess x ..
  1153   with *[THEN spec, of x] show "eventually (\<lambda>x. Z > f x) F"
  1154     by (auto elim!: eventually_elim1)
  1155 qed simp
  1156 
  1157 lemma filterlim_at_infinity:
  1158   fixes f :: "_ \<Rightarrow> 'a\<Colon>real_normed_vector"
  1159   assumes "0 \<le> c"
  1160   shows "(LIM x F. f x :> at_infinity) \<longleftrightarrow> (\<forall>r>c. eventually (\<lambda>x. r \<le> norm (f x)) F)"
  1161   unfolding filterlim_iff eventually_at_infinity
  1162 proof safe
  1163   fix P :: "'a \<Rightarrow> bool" and b
  1164   assume *: "\<forall>r>c. eventually (\<lambda>x. r \<le> norm (f x)) F"
  1165     and P: "\<forall>x. b \<le> norm x \<longrightarrow> P x"
  1166   have "max b (c + 1) > c" by auto
  1167   with * have "eventually (\<lambda>x. max b (c + 1) \<le> norm (f x)) F"
  1168     by auto
  1169   then show "eventually (\<lambda>x. P (f x)) F"
  1170   proof eventually_elim
  1171     fix x assume "max b (c + 1) \<le> norm (f x)"
  1172     with P show "P (f x)" by auto
  1173   qed
  1174 qed force
  1175 
  1176 lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
  1177   unfolding filterlim_at_top
  1178   apply (intro allI)
  1179   apply (rule_tac c="natceiling (Z + 1)" in eventually_sequentiallyI)
  1180   apply (auto simp: natceiling_le_eq)
  1181   done
  1182 
  1183 lemma filterlim_inverse_at_top_pos:
  1184   "LIM x (nhds 0 within {0::real <..}). inverse x :> at_top"
  1185   unfolding filterlim_at_top_gt[where c=0] eventually_within
  1186 proof safe
  1187   fix Z :: real assume [arith]: "0 < Z"
  1188   then have "eventually (\<lambda>x. x < inverse Z) (nhds 0)"
  1189     by (auto simp add: eventually_nhds_metric dist_real_def intro!: exI[of _ "\<bar>inverse Z\<bar>"])
  1190   then show "eventually (\<lambda>x. x \<in> {0<..} \<longrightarrow> Z < inverse x) (nhds 0)"
  1191     by (auto elim!: eventually_elim1 simp: inverse_eq_divide field_simps)
  1192 qed
  1193 
  1194 lemma filterlim_inverse_at_top:
  1195   "(f ---> (0 :: real)) F \<Longrightarrow> eventually (\<lambda>x. 0 < f x) F \<Longrightarrow> LIM x F. inverse (f x) :> at_top"
  1196   by (intro filterlim_compose[OF filterlim_inverse_at_top_pos])
  1197      (simp add: filterlim_def eventually_filtermap le_within_iff)
  1198 
  1199 lemma filterlim_inverse_at_bot_neg:
  1200   "LIM x (nhds 0 within {..< 0::real}). inverse x :> at_bot"
  1201   unfolding filterlim_at_bot_lt[where c=0] eventually_within
  1202 proof safe
  1203   fix Z :: real assume [arith]: "Z < 0"
  1204   have "eventually (\<lambda>x. inverse Z < x) (nhds 0)"
  1205     by (auto simp add: eventually_nhds_metric dist_real_def intro!: exI[of _ "\<bar>inverse Z\<bar>"])
  1206   then show "eventually (\<lambda>x. x \<in> {..< 0} \<longrightarrow> inverse x < Z) (nhds 0)"
  1207     by (auto elim!: eventually_elim1 simp: inverse_eq_divide field_simps)
  1208 qed
  1209 
  1210 lemma filterlim_inverse_at_bot:
  1211   "(f ---> (0 :: real)) F \<Longrightarrow> eventually (\<lambda>x. f x < 0) F \<Longrightarrow> LIM x F. inverse (f x) :> at_bot"
  1212   by (intro filterlim_compose[OF filterlim_inverse_at_bot_neg])
  1213      (simp add: filterlim_def eventually_filtermap le_within_iff)
  1214 
  1215 lemma filterlim_uminus_at_top_at_bot: "LIM x at_bot. - x :: real :> at_top"
  1216   unfolding filterlim_at_top eventually_at_bot_dense
  1217   by (blast intro: less_minus_iff[THEN iffD1])
  1218 
  1219 lemma filterlim_uminus_at_top: "LIM x F. f x :> at_bot \<Longrightarrow> LIM x F. - (f x) :: real :> at_top"
  1220   by (rule filterlim_compose[OF filterlim_uminus_at_top_at_bot])
  1221 
  1222 lemma filterlim_uminus_at_bot_at_top: "LIM x at_top. - x :: real :> at_bot"
  1223   unfolding filterlim_at_bot eventually_at_top_dense
  1224   by (blast intro: minus_less_iff[THEN iffD1])
  1225 
  1226 lemma filterlim_uminus_at_bot: "LIM x F. f x :> at_top \<Longrightarrow> LIM x F. - (f x) :: real :> at_bot"
  1227   by (rule filterlim_compose[OF filterlim_uminus_at_bot_at_top])
  1228 
  1229 lemma tendsto_inverse_0:
  1230   fixes x :: "_ \<Rightarrow> 'a\<Colon>real_normed_div_algebra"
  1231   shows "(inverse ---> (0::'a)) at_infinity"
  1232   unfolding tendsto_Zfun_iff diff_0_right Zfun_def eventually_at_infinity
  1233 proof safe
  1234   fix r :: real assume "0 < r"
  1235   show "\<exists>b. \<forall>x. b \<le> norm x \<longrightarrow> norm (inverse x :: 'a) < r"
  1236   proof (intro exI[of _ "inverse (r / 2)"] allI impI)
  1237     fix x :: 'a
  1238     from `0 < r` have "0 < inverse (r / 2)" by simp
  1239     also assume *: "inverse (r / 2) \<le> norm x"
  1240     finally show "norm (inverse x) < r"
  1241       using * `0 < r` by (subst nonzero_norm_inverse) (simp_all add: inverse_eq_divide field_simps)
  1242   qed
  1243 qed
  1244 
  1245 lemma filterlim_inverse_at_infinity:
  1246   fixes x :: "_ \<Rightarrow> 'a\<Colon>{real_normed_div_algebra, division_ring_inverse_zero}"
  1247   shows "filterlim inverse at_infinity (at (0::'a))"
  1248   unfolding filterlim_at_infinity[OF order_refl]
  1249 proof safe
  1250   fix r :: real assume "0 < r"
  1251   then show "eventually (\<lambda>x::'a. r \<le> norm (inverse x)) (at 0)"
  1252     unfolding eventually_at norm_inverse
  1253     by (intro exI[of _ "inverse r"])
  1254        (auto simp: norm_conv_dist[symmetric] field_simps inverse_eq_divide)
  1255 qed
  1256 
  1257 lemma filterlim_inverse_at_iff:
  1258   fixes g :: "'a \<Rightarrow> 'b\<Colon>{real_normed_div_algebra, division_ring_inverse_zero}"
  1259   shows "(LIM x F. inverse (g x) :> at 0) \<longleftrightarrow> (LIM x F. g x :> at_infinity)"
  1260   unfolding filterlim_def filtermap_filtermap[symmetric]
  1261 proof
  1262   assume "filtermap g F \<le> at_infinity"
  1263   then have "filtermap inverse (filtermap g F) \<le> filtermap inverse at_infinity"
  1264     by (rule filtermap_mono)
  1265   also have "\<dots> \<le> at 0"
  1266     using tendsto_inverse_0
  1267     by (auto intro!: le_withinI exI[of _ 1]
  1268              simp: eventually_filtermap eventually_at_infinity filterlim_def at_def)
  1269   finally show "filtermap inverse (filtermap g F) \<le> at 0" .
  1270 next
  1271   assume "filtermap inverse (filtermap g F) \<le> at 0"
  1272   then have "filtermap inverse (filtermap inverse (filtermap g F)) \<le> filtermap inverse (at 0)"
  1273     by (rule filtermap_mono)
  1274   with filterlim_inverse_at_infinity show "filtermap g F \<le> at_infinity"
  1275     by (auto intro: order_trans simp: filterlim_def filtermap_filtermap)
  1276 qed
  1277 
  1278 text {*
  1279 
  1280 We only show rules for multiplication and addition when the functions are either against a real
  1281 value or against infinity. Further rules are easy to derive by using @{thm filterlim_uminus_at_top}.
  1282 
  1283 *}
  1284 
  1285 lemma filterlim_tendsto_pos_mult_at_top: 
  1286   assumes f: "(f ---> c) F" and c: "0 < c"
  1287   assumes g: "LIM x F. g x :> at_top"
  1288   shows "LIM x F. (f x * g x :: real) :> at_top"
  1289   unfolding filterlim_at_top_gt[where c=0]
  1290 proof safe
  1291   fix Z :: real assume "0 < Z"
  1292   from f `0 < c` have "eventually (\<lambda>x. c / 2 < f x) F"
  1293     by (auto dest!: tendstoD[where e="c / 2"] elim!: eventually_elim1
  1294              simp: dist_real_def abs_real_def split: split_if_asm)
  1295   moreover from g have "eventually (\<lambda>x. (Z / c * 2) < g x) F"
  1296     unfolding filterlim_at_top by auto
  1297   ultimately show "eventually (\<lambda>x. Z < f x * g x) F"
  1298   proof eventually_elim
  1299     fix x assume "c / 2 < f x" "Z / c * 2 < g x"
  1300     with `0 < Z` `0 < c` have "c / 2 * (Z / c * 2) < f x * g x"
  1301       by (intro mult_strict_mono) (auto simp: zero_le_divide_iff)
  1302     with `0 < c` show "Z < f x * g x"
  1303        by simp
  1304   qed
  1305 qed
  1306 
  1307 lemma filterlim_at_top_mult_at_top: 
  1308   assumes f: "LIM x F. f x :> at_top"
  1309   assumes g: "LIM x F. g x :> at_top"
  1310   shows "LIM x F. (f x * g x :: real) :> at_top"
  1311   unfolding filterlim_at_top_gt[where c=0]
  1312 proof safe
  1313   fix Z :: real assume "0 < Z"
  1314   from f have "eventually (\<lambda>x. 1 < f x) F"
  1315     unfolding filterlim_at_top by auto
  1316   moreover from g have "eventually (\<lambda>x. Z < g x) F"
  1317     unfolding filterlim_at_top by auto
  1318   ultimately show "eventually (\<lambda>x. Z < f x * g x) F"
  1319   proof eventually_elim
  1320     fix x assume "1 < f x" "Z < g x"
  1321     with `0 < Z` have "1 * Z < f x * g x"
  1322       by (intro mult_strict_mono) (auto simp: zero_le_divide_iff)
  1323     then show "Z < f x * g x"
  1324        by simp
  1325   qed
  1326 qed
  1327 
  1328 lemma filterlim_tendsto_add_at_top: 
  1329   assumes f: "(f ---> c) F"
  1330   assumes g: "LIM x F. g x :> at_top"
  1331   shows "LIM x F. (f x + g x :: real) :> at_top"
  1332   unfolding filterlim_at_top_gt[where c=0]
  1333 proof safe
  1334   fix Z :: real assume "0 < Z"
  1335   from f have "eventually (\<lambda>x. c - 1 < f x) F"
  1336     by (auto dest!: tendstoD[where e=1] elim!: eventually_elim1 simp: dist_real_def)
  1337   moreover from g have "eventually (\<lambda>x. Z - (c - 1) < g x) F"
  1338     unfolding filterlim_at_top by auto
  1339   ultimately show "eventually (\<lambda>x. Z < f x + g x) F"
  1340     by eventually_elim simp
  1341 qed
  1342 
  1343 lemma filterlim_at_top_add_at_top: 
  1344   assumes f: "LIM x F. f x :> at_top"
  1345   assumes g: "LIM x F. g x :> at_top"
  1346   shows "LIM x F. (f x + g x :: real) :> at_top"
  1347   unfolding filterlim_at_top_gt[where c=0]
  1348 proof safe
  1349   fix Z :: real assume "0 < Z"
  1350   from f have "eventually (\<lambda>x. 0 < f x) F"
  1351     unfolding filterlim_at_top by auto
  1352   moreover from g have "eventually (\<lambda>x. Z < g x) F"
  1353     unfolding filterlim_at_top by auto
  1354   ultimately show "eventually (\<lambda>x. Z < f x + g x) F"
  1355     by eventually_elim simp
  1356 qed
  1357 
  1358 end
  1359