src/HOL/Limits.thy
author hoelzl
Mon Dec 03 18:19:12 2012 +0100 (2012-12-03)
changeset 50331 4b6dc5077e98
parent 50330 d0b12171118e
child 50346 a75c6429c3c3
permissions -rw-r--r--
use filterlim in Lim and SEQ; tuned proofs
     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 lemma filtermap_sup: "filtermap f (sup F1 F2) = sup (filtermap f F1) (filtermap f F2)"
   284   by (auto simp: filter_eq_iff eventually_filtermap eventually_sup)
   285 
   286 subsection {* Order filters *}
   287 
   288 definition at_top :: "('a::order) filter"
   289   where "at_top = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   290 
   291 lemma eventually_at_top_linorder: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::linorder. \<forall>n\<ge>N. P n)"
   292   unfolding at_top_def
   293 proof (rule eventually_Abs_filter, rule is_filter.intro)
   294   fix P Q :: "'a \<Rightarrow> bool"
   295   assume "\<exists>i. \<forall>n\<ge>i. P n" and "\<exists>j. \<forall>n\<ge>j. Q n"
   296   then obtain i j where "\<forall>n\<ge>i. P n" and "\<forall>n\<ge>j. Q n" by auto
   297   then have "\<forall>n\<ge>max i j. P n \<and> Q n" by simp
   298   then show "\<exists>k. \<forall>n\<ge>k. P n \<and> Q n" ..
   299 qed auto
   300 
   301 lemma eventually_at_top_dense: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::dense_linorder. \<forall>n>N. P n)"
   302   unfolding eventually_at_top_linorder
   303 proof safe
   304   fix N assume "\<forall>n\<ge>N. P n" then show "\<exists>N. \<forall>n>N. P n" by (auto intro!: exI[of _ N])
   305 next
   306   fix N assume "\<forall>n>N. P n"
   307   moreover from gt_ex[of N] guess y ..
   308   ultimately show "\<exists>N. \<forall>n\<ge>N. P n" by (auto intro!: exI[of _ y])
   309 qed
   310 
   311 definition at_bot :: "('a::order) filter"
   312   where "at_bot = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<le>k. P n)"
   313 
   314 lemma eventually_at_bot_linorder:
   315   fixes P :: "'a::linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n\<le>N. P n)"
   316   unfolding at_bot_def
   317 proof (rule eventually_Abs_filter, rule is_filter.intro)
   318   fix P Q :: "'a \<Rightarrow> bool"
   319   assume "\<exists>i. \<forall>n\<le>i. P n" and "\<exists>j. \<forall>n\<le>j. Q n"
   320   then obtain i j where "\<forall>n\<le>i. P n" and "\<forall>n\<le>j. Q n" by auto
   321   then have "\<forall>n\<le>min i j. P n \<and> Q n" by simp
   322   then show "\<exists>k. \<forall>n\<le>k. P n \<and> Q n" ..
   323 qed auto
   324 
   325 lemma eventually_at_bot_dense:
   326   fixes P :: "'a::dense_linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n<N. P n)"
   327   unfolding eventually_at_bot_linorder
   328 proof safe
   329   fix N assume "\<forall>n\<le>N. P n" then show "\<exists>N. \<forall>n<N. P n" by (auto intro!: exI[of _ N])
   330 next
   331   fix N assume "\<forall>n<N. P n" 
   332   moreover from lt_ex[of N] guess y ..
   333   ultimately show "\<exists>N. \<forall>n\<le>N. P n" by (auto intro!: exI[of _ y])
   334 qed
   335 
   336 subsection {* Sequentially *}
   337 
   338 abbreviation sequentially :: "nat filter"
   339   where "sequentially == at_top"
   340 
   341 lemma sequentially_def: "sequentially = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   342   unfolding at_top_def by simp
   343 
   344 lemma eventually_sequentially:
   345   "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
   346   by (rule eventually_at_top_linorder)
   347 
   348 lemma sequentially_bot [simp, intro]: "sequentially \<noteq> bot"
   349   unfolding filter_eq_iff eventually_sequentially by auto
   350 
   351 lemmas trivial_limit_sequentially = sequentially_bot
   352 
   353 lemma eventually_False_sequentially [simp]:
   354   "\<not> eventually (\<lambda>n. False) sequentially"
   355   by (simp add: eventually_False)
   356 
   357 lemma le_sequentially:
   358   "F \<le> sequentially \<longleftrightarrow> (\<forall>N. eventually (\<lambda>n. N \<le> n) F)"
   359   unfolding le_filter_def eventually_sequentially
   360   by (safe, fast, drule_tac x=N in spec, auto elim: eventually_rev_mp)
   361 
   362 lemma eventually_sequentiallyI:
   363   assumes "\<And>x. c \<le> x \<Longrightarrow> P x"
   364   shows "eventually P sequentially"
   365 using assms by (auto simp: eventually_sequentially)
   366 
   367 
   368 subsection {* Standard filters *}
   369 
   370 definition within :: "'a filter \<Rightarrow> 'a set \<Rightarrow> 'a filter" (infixr "within" 70)
   371   where "F within S = Abs_filter (\<lambda>P. eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F)"
   372 
   373 definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
   374   where "nhds a = Abs_filter (\<lambda>P. \<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   375 
   376 definition (in topological_space) at :: "'a \<Rightarrow> 'a filter"
   377   where "at a = nhds a within - {a}"
   378 
   379 abbreviation at_right :: "'a\<Colon>{topological_space, order} \<Rightarrow> 'a filter" where
   380   "at_right x \<equiv> at x within {x <..}"
   381 
   382 abbreviation at_left :: "'a\<Colon>{topological_space, order} \<Rightarrow> 'a filter" where
   383   "at_left x \<equiv> at x within {..< x}"
   384 
   385 definition at_infinity :: "'a::real_normed_vector filter" where
   386   "at_infinity = Abs_filter (\<lambda>P. \<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x)"
   387 
   388 lemma eventually_within:
   389   "eventually P (F within S) = eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F"
   390   unfolding within_def
   391   by (rule eventually_Abs_filter, rule is_filter.intro)
   392      (auto elim!: eventually_rev_mp)
   393 
   394 lemma within_UNIV [simp]: "F within UNIV = F"
   395   unfolding filter_eq_iff eventually_within by simp
   396 
   397 lemma within_empty [simp]: "F within {} = bot"
   398   unfolding filter_eq_iff eventually_within by simp
   399 
   400 lemma within_le: "F within S \<le> F"
   401   unfolding le_filter_def eventually_within by (auto elim: eventually_elim1)
   402 
   403 lemma le_withinI: "F \<le> F' \<Longrightarrow> eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S"
   404   unfolding le_filter_def eventually_within by (auto elim: eventually_elim2)
   405 
   406 lemma le_within_iff: "eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S \<longleftrightarrow> F \<le> F'"
   407   by (blast intro: within_le le_withinI order_trans)
   408 
   409 lemma eventually_nhds:
   410   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   411 unfolding nhds_def
   412 proof (rule eventually_Abs_filter, rule is_filter.intro)
   413   have "open UNIV \<and> a \<in> UNIV \<and> (\<forall>x\<in>UNIV. True)" by simp
   414   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. True)" ..
   415 next
   416   fix P Q
   417   assume "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   418      and "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)"
   419   then obtain S T where
   420     "open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   421     "open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)" by auto
   422   hence "open (S \<inter> T) \<and> a \<in> S \<inter> T \<and> (\<forall>x\<in>(S \<inter> T). P x \<and> Q x)"
   423     by (simp add: open_Int)
   424   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x \<and> Q x)" ..
   425 qed auto
   426 
   427 lemma eventually_nhds_metric:
   428   "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
   429 unfolding eventually_nhds open_dist
   430 apply safe
   431 apply fast
   432 apply (rule_tac x="{x. dist x a < d}" in exI, simp)
   433 apply clarsimp
   434 apply (rule_tac x="d - dist x a" in exI, clarsimp)
   435 apply (simp only: less_diff_eq)
   436 apply (erule le_less_trans [OF dist_triangle])
   437 done
   438 
   439 lemma nhds_neq_bot [simp]: "nhds a \<noteq> bot"
   440   unfolding trivial_limit_def eventually_nhds by simp
   441 
   442 lemma eventually_at_topological:
   443   "eventually P (at a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
   444 unfolding at_def eventually_within eventually_nhds by simp
   445 
   446 lemma eventually_at:
   447   fixes a :: "'a::metric_space"
   448   shows "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
   449 unfolding at_def eventually_within eventually_nhds_metric by auto
   450 
   451 lemma eventually_within_less: (* COPY FROM Topo/eventually_within *)
   452   "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a < d \<longrightarrow> P x)"
   453   unfolding eventually_within eventually_at dist_nz by auto
   454 
   455 lemma eventually_within_le: (* COPY FROM Topo/eventually_within_le *)
   456   "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. 0 < dist x a \<and> dist x a <= d \<longrightarrow> P x)"
   457   unfolding eventually_within_less by auto (metis dense order_le_less_trans)
   458 
   459 lemma at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
   460   unfolding trivial_limit_def eventually_at_topological
   461   by (safe, case_tac "S = {a}", simp, fast, fast)
   462 
   463 lemma at_neq_bot [simp]: "at (a::'a::perfect_space) \<noteq> bot"
   464   by (simp add: at_eq_bot_iff not_open_singleton)
   465 
   466 lemma trivial_limit_at_left_real [simp]: (* maybe generalize type *)
   467   "\<not> trivial_limit (at_left (x::real))"
   468   unfolding trivial_limit_def eventually_within_le
   469   apply clarsimp
   470   apply (rule_tac x="x - d/2" in bexI)
   471   apply (auto simp: dist_real_def)
   472   done
   473 
   474 lemma trivial_limit_at_right_real [simp]: (* maybe generalize type *)
   475   "\<not> trivial_limit (at_right (x::real))"
   476   unfolding trivial_limit_def eventually_within_le
   477   apply clarsimp
   478   apply (rule_tac x="x + d/2" in bexI)
   479   apply (auto simp: dist_real_def)
   480   done
   481 
   482 lemma eventually_at_infinity:
   483   "eventually P at_infinity \<longleftrightarrow> (\<exists>b. \<forall>x. b \<le> norm x \<longrightarrow> P x)"
   484 unfolding at_infinity_def
   485 proof (rule eventually_Abs_filter, rule is_filter.intro)
   486   fix P Q :: "'a \<Rightarrow> bool"
   487   assume "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<exists>s. \<forall>x. s \<le> norm x \<longrightarrow> Q x"
   488   then obtain r s where
   489     "\<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<forall>x. s \<le> norm x \<longrightarrow> Q x" by auto
   490   then have "\<forall>x. max r s \<le> norm x \<longrightarrow> P x \<and> Q x" by simp
   491   then show "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x \<and> Q x" ..
   492 qed auto
   493 
   494 lemma at_infinity_eq_at_top_bot:
   495   "(at_infinity \<Colon> real filter) = sup at_top at_bot"
   496   unfolding sup_filter_def at_infinity_def eventually_at_top_linorder eventually_at_bot_linorder
   497 proof (intro arg_cong[where f=Abs_filter] ext iffI)
   498   fix P :: "real \<Rightarrow> bool" assume "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x"
   499   then guess r ..
   500   then have "(\<forall>x\<ge>r. P x) \<and> (\<forall>x\<le>-r. P x)" by auto
   501   then show "(\<exists>r. \<forall>x\<ge>r. P x) \<and> (\<exists>r. \<forall>x\<le>r. P x)" by auto
   502 next
   503   fix P :: "real \<Rightarrow> bool" assume "(\<exists>r. \<forall>x\<ge>r. P x) \<and> (\<exists>r. \<forall>x\<le>r. P x)"
   504   then obtain p q where "\<forall>x\<ge>p. P x" "\<forall>x\<le>q. P x" by auto
   505   then show "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x"
   506     by (intro exI[of _ "max p (-q)"])
   507        (auto simp: abs_real_def)
   508 qed
   509 
   510 lemma at_top_le_at_infinity:
   511   "at_top \<le> (at_infinity :: real filter)"
   512   unfolding at_infinity_eq_at_top_bot by simp
   513 
   514 lemma at_bot_le_at_infinity:
   515   "at_bot \<le> (at_infinity :: real filter)"
   516   unfolding at_infinity_eq_at_top_bot by simp
   517 
   518 subsection {* Boundedness *}
   519 
   520 definition Bfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   521   where "Bfun f F = (\<exists>K>0. eventually (\<lambda>x. norm (f x) \<le> K) F)"
   522 
   523 lemma BfunI:
   524   assumes K: "eventually (\<lambda>x. norm (f x) \<le> K) F" shows "Bfun f F"
   525 unfolding Bfun_def
   526 proof (intro exI conjI allI)
   527   show "0 < max K 1" by simp
   528 next
   529   show "eventually (\<lambda>x. norm (f x) \<le> max K 1) F"
   530     using K by (rule eventually_elim1, simp)
   531 qed
   532 
   533 lemma BfunE:
   534   assumes "Bfun f F"
   535   obtains B where "0 < B" and "eventually (\<lambda>x. norm (f x) \<le> B) F"
   536 using assms unfolding Bfun_def by fast
   537 
   538 
   539 subsection {* Convergence to Zero *}
   540 
   541 definition Zfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   542   where "Zfun f F = (\<forall>r>0. eventually (\<lambda>x. norm (f x) < r) F)"
   543 
   544 lemma ZfunI:
   545   "(\<And>r. 0 < r \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F) \<Longrightarrow> Zfun f F"
   546   unfolding Zfun_def by simp
   547 
   548 lemma ZfunD:
   549   "\<lbrakk>Zfun f F; 0 < r\<rbrakk> \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F"
   550   unfolding Zfun_def by simp
   551 
   552 lemma Zfun_ssubst:
   553   "eventually (\<lambda>x. f x = g x) F \<Longrightarrow> Zfun g F \<Longrightarrow> Zfun f F"
   554   unfolding Zfun_def by (auto elim!: eventually_rev_mp)
   555 
   556 lemma Zfun_zero: "Zfun (\<lambda>x. 0) F"
   557   unfolding Zfun_def by simp
   558 
   559 lemma Zfun_norm_iff: "Zfun (\<lambda>x. norm (f x)) F = Zfun (\<lambda>x. f x) F"
   560   unfolding Zfun_def by simp
   561 
   562 lemma Zfun_imp_Zfun:
   563   assumes f: "Zfun f F"
   564   assumes g: "eventually (\<lambda>x. norm (g x) \<le> norm (f x) * K) F"
   565   shows "Zfun (\<lambda>x. g x) F"
   566 proof (cases)
   567   assume K: "0 < K"
   568   show ?thesis
   569   proof (rule ZfunI)
   570     fix r::real assume "0 < r"
   571     hence "0 < r / K"
   572       using K by (rule divide_pos_pos)
   573     then have "eventually (\<lambda>x. norm (f x) < r / K) F"
   574       using ZfunD [OF f] by fast
   575     with g show "eventually (\<lambda>x. norm (g x) < r) F"
   576     proof eventually_elim
   577       case (elim x)
   578       hence "norm (f x) * K < r"
   579         by (simp add: pos_less_divide_eq K)
   580       thus ?case
   581         by (simp add: order_le_less_trans [OF elim(1)])
   582     qed
   583   qed
   584 next
   585   assume "\<not> 0 < K"
   586   hence K: "K \<le> 0" by (simp only: not_less)
   587   show ?thesis
   588   proof (rule ZfunI)
   589     fix r :: real
   590     assume "0 < r"
   591     from g show "eventually (\<lambda>x. norm (g x) < r) F"
   592     proof eventually_elim
   593       case (elim x)
   594       also have "norm (f x) * K \<le> norm (f x) * 0"
   595         using K norm_ge_zero by (rule mult_left_mono)
   596       finally show ?case
   597         using `0 < r` by simp
   598     qed
   599   qed
   600 qed
   601 
   602 lemma Zfun_le: "\<lbrakk>Zfun g F; \<forall>x. norm (f x) \<le> norm (g x)\<rbrakk> \<Longrightarrow> Zfun f F"
   603   by (erule_tac K="1" in Zfun_imp_Zfun, simp)
   604 
   605 lemma Zfun_add:
   606   assumes f: "Zfun f F" and g: "Zfun g F"
   607   shows "Zfun (\<lambda>x. f x + g x) F"
   608 proof (rule ZfunI)
   609   fix r::real assume "0 < r"
   610   hence r: "0 < r / 2" by simp
   611   have "eventually (\<lambda>x. norm (f x) < r/2) F"
   612     using f r by (rule ZfunD)
   613   moreover
   614   have "eventually (\<lambda>x. norm (g x) < r/2) F"
   615     using g r by (rule ZfunD)
   616   ultimately
   617   show "eventually (\<lambda>x. norm (f x + g x) < r) F"
   618   proof eventually_elim
   619     case (elim x)
   620     have "norm (f x + g x) \<le> norm (f x) + norm (g x)"
   621       by (rule norm_triangle_ineq)
   622     also have "\<dots> < r/2 + r/2"
   623       using elim by (rule add_strict_mono)
   624     finally show ?case
   625       by simp
   626   qed
   627 qed
   628 
   629 lemma Zfun_minus: "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. - f x) F"
   630   unfolding Zfun_def by simp
   631 
   632 lemma Zfun_diff: "\<lbrakk>Zfun f F; Zfun g F\<rbrakk> \<Longrightarrow> Zfun (\<lambda>x. f x - g x) F"
   633   by (simp only: diff_minus Zfun_add Zfun_minus)
   634 
   635 lemma (in bounded_linear) Zfun:
   636   assumes g: "Zfun g F"
   637   shows "Zfun (\<lambda>x. f (g x)) F"
   638 proof -
   639   obtain K where "\<And>x. norm (f x) \<le> norm x * K"
   640     using bounded by fast
   641   then have "eventually (\<lambda>x. norm (f (g x)) \<le> norm (g x) * K) F"
   642     by simp
   643   with g show ?thesis
   644     by (rule Zfun_imp_Zfun)
   645 qed
   646 
   647 lemma (in bounded_bilinear) Zfun:
   648   assumes f: "Zfun f F"
   649   assumes g: "Zfun g F"
   650   shows "Zfun (\<lambda>x. f x ** g x) F"
   651 proof (rule ZfunI)
   652   fix r::real assume r: "0 < r"
   653   obtain K where K: "0 < K"
   654     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   655     using pos_bounded by fast
   656   from K have K': "0 < inverse K"
   657     by (rule positive_imp_inverse_positive)
   658   have "eventually (\<lambda>x. norm (f x) < r) F"
   659     using f r by (rule ZfunD)
   660   moreover
   661   have "eventually (\<lambda>x. norm (g x) < inverse K) F"
   662     using g K' by (rule ZfunD)
   663   ultimately
   664   show "eventually (\<lambda>x. norm (f x ** g x) < r) F"
   665   proof eventually_elim
   666     case (elim x)
   667     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   668       by (rule norm_le)
   669     also have "norm (f x) * norm (g x) * K < r * inverse K * K"
   670       by (intro mult_strict_right_mono mult_strict_mono' norm_ge_zero elim K)
   671     also from K have "r * inverse K * K = r"
   672       by simp
   673     finally show ?case .
   674   qed
   675 qed
   676 
   677 lemma (in bounded_bilinear) Zfun_left:
   678   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. f x ** a) F"
   679   by (rule bounded_linear_left [THEN bounded_linear.Zfun])
   680 
   681 lemma (in bounded_bilinear) Zfun_right:
   682   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. a ** f x) F"
   683   by (rule bounded_linear_right [THEN bounded_linear.Zfun])
   684 
   685 lemmas Zfun_mult = bounded_bilinear.Zfun [OF bounded_bilinear_mult]
   686 lemmas Zfun_mult_right = bounded_bilinear.Zfun_right [OF bounded_bilinear_mult]
   687 lemmas Zfun_mult_left = bounded_bilinear.Zfun_left [OF bounded_bilinear_mult]
   688 
   689 
   690 subsection {* Limits *}
   691 
   692 definition filterlim :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b filter \<Rightarrow> 'a filter \<Rightarrow> bool" where
   693   "filterlim f F2 F1 \<longleftrightarrow> filtermap f F1 \<le> F2"
   694 
   695 syntax
   696   "_LIM" :: "pttrns \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> bool" ("(3LIM (_)/ (_)./ (_) :> (_))" [1000, 10, 0, 10] 10)
   697 
   698 translations
   699   "LIM x F1. f :> F2"   == "CONST filterlim (%x. f) F2 F1"
   700 
   701 lemma filterlim_iff:
   702   "(LIM x F1. f x :> F2) \<longleftrightarrow> (\<forall>P. eventually P F2 \<longrightarrow> eventually (\<lambda>x. P (f x)) F1)"
   703   unfolding filterlim_def le_filter_def eventually_filtermap ..
   704 
   705 lemma filterlim_compose:
   706   "filterlim g F3 F2 \<Longrightarrow> filterlim f F2 F1 \<Longrightarrow> filterlim (\<lambda>x. g (f x)) F3 F1"
   707   unfolding filterlim_def filtermap_filtermap[symmetric] by (metis filtermap_mono order_trans)
   708 
   709 lemma filterlim_mono:
   710   "filterlim f F2 F1 \<Longrightarrow> F2 \<le> F2' \<Longrightarrow> F1' \<le> F1 \<Longrightarrow> filterlim f F2' F1'"
   711   unfolding filterlim_def by (metis filtermap_mono order_trans)
   712 
   713 lemma filterlim_cong:
   714   "F1 = F1' \<Longrightarrow> F2 = F2' \<Longrightarrow> eventually (\<lambda>x. f x = g x) F2 \<Longrightarrow> filterlim f F1 F2 = filterlim g F1' F2'"
   715   by (auto simp: filterlim_def le_filter_def eventually_filtermap elim: eventually_elim2)
   716 
   717 lemma filterlim_within:
   718   "(LIM x F1. f x :> F2 within S) \<longleftrightarrow> (eventually (\<lambda>x. f x \<in> S) F1 \<and> (LIM x F1. f x :> F2))"
   719   unfolding filterlim_def
   720 proof safe
   721   assume "filtermap f F1 \<le> F2 within S" then show "eventually (\<lambda>x. f x \<in> S) F1"
   722     by (auto simp: le_filter_def eventually_filtermap eventually_within elim!: allE[of _ "\<lambda>x. x \<in> S"])
   723 qed (auto intro: within_le order_trans simp: le_within_iff eventually_filtermap)
   724 
   725 lemma filterlim_filtermap: "filterlim f F1 (filtermap g F2) = filterlim (\<lambda>x. f (g x)) F1 F2"
   726   unfolding filterlim_def filtermap_filtermap ..
   727 
   728 lemma filterlim_sup:
   729   "filterlim f F F1 \<Longrightarrow> filterlim f F F2 \<Longrightarrow> filterlim f F (sup F1 F2)"
   730   unfolding filterlim_def filtermap_sup by auto
   731 
   732 lemma filterlim_Suc: "filterlim Suc sequentially sequentially"
   733   by (simp add: filterlim_iff eventually_sequentially) (metis le_Suc_eq)
   734 
   735 abbreviation (in topological_space)
   736   tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool" (infixr "--->" 55) where
   737   "(f ---> l) F \<equiv> filterlim f (nhds l) F"
   738 
   739 ML {*
   740 structure Tendsto_Intros = Named_Thms
   741 (
   742   val name = @{binding tendsto_intros}
   743   val description = "introduction rules for tendsto"
   744 )
   745 *}
   746 
   747 setup Tendsto_Intros.setup
   748 
   749 lemma tendsto_def: "(f ---> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
   750   unfolding filterlim_def
   751 proof safe
   752   fix S assume "open S" "l \<in> S" "filtermap f F \<le> nhds l"
   753   then show "eventually (\<lambda>x. f x \<in> S) F"
   754     unfolding eventually_nhds eventually_filtermap le_filter_def
   755     by (auto elim!: allE[of _ "\<lambda>x. x \<in> S"] eventually_rev_mp)
   756 qed (auto elim!: eventually_rev_mp simp: eventually_nhds eventually_filtermap le_filter_def)
   757 
   758 lemma filterlim_at:
   759   "(LIM x F. f x :> at b) \<longleftrightarrow> (eventually (\<lambda>x. f x \<noteq> b) F \<and> (f ---> b) F)"
   760   by (simp add: at_def filterlim_within)
   761 
   762 lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f ---> l) F' \<Longrightarrow> (f ---> l) F"
   763   unfolding tendsto_def le_filter_def by fast
   764 
   765 lemma topological_tendstoI:
   766   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F)
   767     \<Longrightarrow> (f ---> l) F"
   768   unfolding tendsto_def by auto
   769 
   770 lemma topological_tendstoD:
   771   "(f ---> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
   772   unfolding tendsto_def by auto
   773 
   774 lemma tendstoI:
   775   assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
   776   shows "(f ---> l) F"
   777   apply (rule topological_tendstoI)
   778   apply (simp add: open_dist)
   779   apply (drule (1) bspec, clarify)
   780   apply (drule assms)
   781   apply (erule eventually_elim1, simp)
   782   done
   783 
   784 lemma tendstoD:
   785   "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
   786   apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
   787   apply (clarsimp simp add: open_dist)
   788   apply (rule_tac x="e - dist x l" in exI, clarsimp)
   789   apply (simp only: less_diff_eq)
   790   apply (erule le_less_trans [OF dist_triangle])
   791   apply simp
   792   apply simp
   793   done
   794 
   795 lemma tendsto_iff:
   796   "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
   797   using tendstoI tendstoD by fast
   798 
   799 lemma tendsto_Zfun_iff: "(f ---> a) F = Zfun (\<lambda>x. f x - a) F"
   800   by (simp only: tendsto_iff Zfun_def dist_norm)
   801 
   802 lemma tendsto_bot [simp]: "(f ---> a) bot"
   803   unfolding tendsto_def by simp
   804 
   805 lemma tendsto_ident_at [tendsto_intros]: "((\<lambda>x. x) ---> a) (at a)"
   806   unfolding tendsto_def eventually_at_topological by auto
   807 
   808 lemma tendsto_ident_at_within [tendsto_intros]:
   809   "((\<lambda>x. x) ---> a) (at a within S)"
   810   unfolding tendsto_def eventually_within eventually_at_topological by auto
   811 
   812 lemma tendsto_const [tendsto_intros]: "((\<lambda>x. k) ---> k) F"
   813   by (simp add: tendsto_def)
   814 
   815 lemma tendsto_unique:
   816   fixes f :: "'a \<Rightarrow> 'b::t2_space"
   817   assumes "\<not> trivial_limit F" and "(f ---> a) F" and "(f ---> b) F"
   818   shows "a = b"
   819 proof (rule ccontr)
   820   assume "a \<noteq> b"
   821   obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
   822     using hausdorff [OF `a \<noteq> b`] by fast
   823   have "eventually (\<lambda>x. f x \<in> U) F"
   824     using `(f ---> a) F` `open U` `a \<in> U` by (rule topological_tendstoD)
   825   moreover
   826   have "eventually (\<lambda>x. f x \<in> V) F"
   827     using `(f ---> b) F` `open V` `b \<in> V` by (rule topological_tendstoD)
   828   ultimately
   829   have "eventually (\<lambda>x. False) F"
   830   proof eventually_elim
   831     case (elim x)
   832     hence "f x \<in> U \<inter> V" by simp
   833     with `U \<inter> V = {}` show ?case by simp
   834   qed
   835   with `\<not> trivial_limit F` show "False"
   836     by (simp add: trivial_limit_def)
   837 qed
   838 
   839 lemma tendsto_const_iff:
   840   fixes a b :: "'a::t2_space"
   841   assumes "\<not> trivial_limit F" shows "((\<lambda>x. a) ---> b) F \<longleftrightarrow> a = b"
   842   by (safe intro!: tendsto_const tendsto_unique [OF assms tendsto_const])
   843 
   844 lemma tendsto_at_iff_tendsto_nhds:
   845   "(g ---> g l) (at l) \<longleftrightarrow> (g ---> g l) (nhds l)"
   846   unfolding tendsto_def at_def eventually_within
   847   by (intro ext all_cong imp_cong) (auto elim!: eventually_elim1)
   848 
   849 lemma tendsto_compose:
   850   "(g ---> g l) (at l) \<Longrightarrow> (f ---> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> g l) F"
   851   unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])
   852 
   853 lemma tendsto_compose_eventually:
   854   "(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"
   855   by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)
   856 
   857 lemma metric_tendsto_imp_tendsto:
   858   assumes f: "(f ---> a) F"
   859   assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
   860   shows "(g ---> b) F"
   861 proof (rule tendstoI)
   862   fix e :: real assume "0 < e"
   863   with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
   864   with le show "eventually (\<lambda>x. dist (g x) b < e) F"
   865     using le_less_trans by (rule eventually_elim2)
   866 qed
   867 
   868 subsubsection {* Distance and norms *}
   869 
   870 lemma tendsto_dist [tendsto_intros]:
   871   assumes f: "(f ---> l) F" and g: "(g ---> m) F"
   872   shows "((\<lambda>x. dist (f x) (g x)) ---> dist l m) F"
   873 proof (rule tendstoI)
   874   fix e :: real assume "0 < e"
   875   hence e2: "0 < e/2" by simp
   876   from tendstoD [OF f e2] tendstoD [OF g e2]
   877   show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) F"
   878   proof (eventually_elim)
   879     case (elim x)
   880     then show "dist (dist (f x) (g x)) (dist l m) < e"
   881       unfolding dist_real_def
   882       using dist_triangle2 [of "f x" "g x" "l"]
   883       using dist_triangle2 [of "g x" "l" "m"]
   884       using dist_triangle3 [of "l" "m" "f x"]
   885       using dist_triangle [of "f x" "m" "g x"]
   886       by arith
   887   qed
   888 qed
   889 
   890 lemma norm_conv_dist: "norm x = dist x 0"
   891   unfolding dist_norm by simp
   892 
   893 lemma tendsto_norm [tendsto_intros]:
   894   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> norm a) F"
   895   unfolding norm_conv_dist by (intro tendsto_intros)
   896 
   897 lemma tendsto_norm_zero:
   898   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> 0) F"
   899   by (drule tendsto_norm, simp)
   900 
   901 lemma tendsto_norm_zero_cancel:
   902   "((\<lambda>x. norm (f x)) ---> 0) F \<Longrightarrow> (f ---> 0) F"
   903   unfolding tendsto_iff dist_norm by simp
   904 
   905 lemma tendsto_norm_zero_iff:
   906   "((\<lambda>x. norm (f x)) ---> 0) F \<longleftrightarrow> (f ---> 0) F"
   907   unfolding tendsto_iff dist_norm by simp
   908 
   909 lemma tendsto_rabs [tendsto_intros]:
   910   "(f ---> (l::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> \<bar>l\<bar>) F"
   911   by (fold real_norm_def, rule tendsto_norm)
   912 
   913 lemma tendsto_rabs_zero:
   914   "(f ---> (0::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> 0) F"
   915   by (fold real_norm_def, rule tendsto_norm_zero)
   916 
   917 lemma tendsto_rabs_zero_cancel:
   918   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<Longrightarrow> (f ---> 0) F"
   919   by (fold real_norm_def, rule tendsto_norm_zero_cancel)
   920 
   921 lemma tendsto_rabs_zero_iff:
   922   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<longleftrightarrow> (f ---> 0) F"
   923   by (fold real_norm_def, rule tendsto_norm_zero_iff)
   924 
   925 subsubsection {* Addition and subtraction *}
   926 
   927 lemma tendsto_add [tendsto_intros]:
   928   fixes a b :: "'a::real_normed_vector"
   929   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> a + b) F"
   930   by (simp only: tendsto_Zfun_iff add_diff_add Zfun_add)
   931 
   932 lemma tendsto_add_zero:
   933   fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
   934   shows "\<lbrakk>(f ---> 0) F; (g ---> 0) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> 0) F"
   935   by (drule (1) tendsto_add, simp)
   936 
   937 lemma tendsto_minus [tendsto_intros]:
   938   fixes a :: "'a::real_normed_vector"
   939   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. - f x) ---> - a) F"
   940   by (simp only: tendsto_Zfun_iff minus_diff_minus Zfun_minus)
   941 
   942 lemma tendsto_minus_cancel:
   943   fixes a :: "'a::real_normed_vector"
   944   shows "((\<lambda>x. - f x) ---> - a) F \<Longrightarrow> (f ---> a) F"
   945   by (drule tendsto_minus, simp)
   946 
   947 lemma tendsto_minus_cancel_left:
   948     "(f ---> - (y::_::real_normed_vector)) F \<longleftrightarrow> ((\<lambda>x. - f x) ---> y) F"
   949   using tendsto_minus_cancel[of f "- y" F]  tendsto_minus[of f "- y" F]
   950   by auto
   951 
   952 lemma tendsto_diff [tendsto_intros]:
   953   fixes a b :: "'a::real_normed_vector"
   954   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x - g x) ---> a - b) F"
   955   by (simp add: diff_minus tendsto_add tendsto_minus)
   956 
   957 lemma tendsto_setsum [tendsto_intros]:
   958   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::real_normed_vector"
   959   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> a i) F"
   960   shows "((\<lambda>x. \<Sum>i\<in>S. f i x) ---> (\<Sum>i\<in>S. a i)) F"
   961 proof (cases "finite S")
   962   assume "finite S" thus ?thesis using assms
   963     by (induct, simp add: tendsto_const, simp add: tendsto_add)
   964 next
   965   assume "\<not> finite S" thus ?thesis
   966     by (simp add: tendsto_const)
   967 qed
   968 
   969 lemma real_tendsto_sandwich:
   970   fixes f g h :: "'a \<Rightarrow> real"
   971   assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
   972   assumes lim: "(f ---> c) net" "(h ---> c) net"
   973   shows "(g ---> c) net"
   974 proof -
   975   have "((\<lambda>n. g n - f n) ---> 0) net"
   976   proof (rule metric_tendsto_imp_tendsto)
   977     show "eventually (\<lambda>n. dist (g n - f n) 0 \<le> dist (h n - f n) 0) net"
   978       using ev by (rule eventually_elim2) (simp add: dist_real_def)
   979     show "((\<lambda>n. h n - f n) ---> 0) net"
   980       using tendsto_diff[OF lim(2,1)] by simp
   981   qed
   982   from tendsto_add[OF this lim(1)] show ?thesis by simp
   983 qed
   984 
   985 subsubsection {* Linear operators and multiplication *}
   986 
   987 lemma (in bounded_linear) tendsto:
   988   "(g ---> a) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> f a) F"
   989   by (simp only: tendsto_Zfun_iff diff [symmetric] Zfun)
   990 
   991 lemma (in bounded_linear) tendsto_zero:
   992   "(g ---> 0) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> 0) F"
   993   by (drule tendsto, simp only: zero)
   994 
   995 lemma (in bounded_bilinear) tendsto:
   996   "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x ** g x) ---> a ** b) F"
   997   by (simp only: tendsto_Zfun_iff prod_diff_prod
   998                  Zfun_add Zfun Zfun_left Zfun_right)
   999 
  1000 lemma (in bounded_bilinear) tendsto_zero:
  1001   assumes f: "(f ---> 0) F"
  1002   assumes g: "(g ---> 0) F"
  1003   shows "((\<lambda>x. f x ** g x) ---> 0) F"
  1004   using tendsto [OF f g] by (simp add: zero_left)
  1005 
  1006 lemma (in bounded_bilinear) tendsto_left_zero:
  1007   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. f x ** c) ---> 0) F"
  1008   by (rule bounded_linear.tendsto_zero [OF bounded_linear_left])
  1009 
  1010 lemma (in bounded_bilinear) tendsto_right_zero:
  1011   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. c ** f x) ---> 0) F"
  1012   by (rule bounded_linear.tendsto_zero [OF bounded_linear_right])
  1013 
  1014 lemmas tendsto_of_real [tendsto_intros] =
  1015   bounded_linear.tendsto [OF bounded_linear_of_real]
  1016 
  1017 lemmas tendsto_scaleR [tendsto_intros] =
  1018   bounded_bilinear.tendsto [OF bounded_bilinear_scaleR]
  1019 
  1020 lemmas tendsto_mult [tendsto_intros] =
  1021   bounded_bilinear.tendsto [OF bounded_bilinear_mult]
  1022 
  1023 lemmas tendsto_mult_zero =
  1024   bounded_bilinear.tendsto_zero [OF bounded_bilinear_mult]
  1025 
  1026 lemmas tendsto_mult_left_zero =
  1027   bounded_bilinear.tendsto_left_zero [OF bounded_bilinear_mult]
  1028 
  1029 lemmas tendsto_mult_right_zero =
  1030   bounded_bilinear.tendsto_right_zero [OF bounded_bilinear_mult]
  1031 
  1032 lemma tendsto_power [tendsto_intros]:
  1033   fixes f :: "'a \<Rightarrow> 'b::{power,real_normed_algebra}"
  1034   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. f x ^ n) ---> a ^ n) F"
  1035   by (induct n) (simp_all add: tendsto_const tendsto_mult)
  1036 
  1037 lemma tendsto_setprod [tendsto_intros]:
  1038   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::{real_normed_algebra,comm_ring_1}"
  1039   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> L i) F"
  1040   shows "((\<lambda>x. \<Prod>i\<in>S. f i x) ---> (\<Prod>i\<in>S. L i)) F"
  1041 proof (cases "finite S")
  1042   assume "finite S" thus ?thesis using assms
  1043     by (induct, simp add: tendsto_const, simp add: tendsto_mult)
  1044 next
  1045   assume "\<not> finite S" thus ?thesis
  1046     by (simp add: tendsto_const)
  1047 qed
  1048 
  1049 lemma tendsto_le_const:
  1050   fixes f :: "_ \<Rightarrow> real" 
  1051   assumes F: "\<not> trivial_limit F"
  1052   assumes x: "(f ---> x) F" and a: "eventually (\<lambda>x. a \<le> f x) F"
  1053   shows "a \<le> x"
  1054 proof (rule ccontr)
  1055   assume "\<not> a \<le> x"
  1056   with x have "eventually (\<lambda>x. f x < a) F"
  1057     by (auto simp add: tendsto_def elim!: allE[of _ "{..< a}"])
  1058   with a have "eventually (\<lambda>x. False) F"
  1059     by eventually_elim auto
  1060   with F show False
  1061     by (simp add: eventually_False)
  1062 qed
  1063 
  1064 lemma tendsto_le:
  1065   fixes f g :: "_ \<Rightarrow> real" 
  1066   assumes F: "\<not> trivial_limit F"
  1067   assumes x: "(f ---> x) F" and y: "(g ---> y) F"
  1068   assumes ev: "eventually (\<lambda>x. g x \<le> f x) F"
  1069   shows "y \<le> x"
  1070   using tendsto_le_const[OF F tendsto_diff[OF x y], of 0] ev
  1071   by (simp add: sign_simps)
  1072 
  1073 subsubsection {* Inverse and division *}
  1074 
  1075 lemma (in bounded_bilinear) Zfun_prod_Bfun:
  1076   assumes f: "Zfun f F"
  1077   assumes g: "Bfun g F"
  1078   shows "Zfun (\<lambda>x. f x ** g x) F"
  1079 proof -
  1080   obtain K where K: "0 \<le> K"
  1081     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
  1082     using nonneg_bounded by fast
  1083   obtain B where B: "0 < B"
  1084     and norm_g: "eventually (\<lambda>x. norm (g x) \<le> B) F"
  1085     using g by (rule BfunE)
  1086   have "eventually (\<lambda>x. norm (f x ** g x) \<le> norm (f x) * (B * K)) F"
  1087   using norm_g proof eventually_elim
  1088     case (elim x)
  1089     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
  1090       by (rule norm_le)
  1091     also have "\<dots> \<le> norm (f x) * B * K"
  1092       by (intro mult_mono' order_refl norm_g norm_ge_zero
  1093                 mult_nonneg_nonneg K elim)
  1094     also have "\<dots> = norm (f x) * (B * K)"
  1095       by (rule mult_assoc)
  1096     finally show "norm (f x ** g x) \<le> norm (f x) * (B * K)" .
  1097   qed
  1098   with f show ?thesis
  1099     by (rule Zfun_imp_Zfun)
  1100 qed
  1101 
  1102 lemma (in bounded_bilinear) flip:
  1103   "bounded_bilinear (\<lambda>x y. y ** x)"
  1104   apply default
  1105   apply (rule add_right)
  1106   apply (rule add_left)
  1107   apply (rule scaleR_right)
  1108   apply (rule scaleR_left)
  1109   apply (subst mult_commute)
  1110   using bounded by fast
  1111 
  1112 lemma (in bounded_bilinear) Bfun_prod_Zfun:
  1113   assumes f: "Bfun f F"
  1114   assumes g: "Zfun g F"
  1115   shows "Zfun (\<lambda>x. f x ** g x) F"
  1116   using flip g f by (rule bounded_bilinear.Zfun_prod_Bfun)
  1117 
  1118 lemma Bfun_inverse_lemma:
  1119   fixes x :: "'a::real_normed_div_algebra"
  1120   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
  1121   apply (subst nonzero_norm_inverse, clarsimp)
  1122   apply (erule (1) le_imp_inverse_le)
  1123   done
  1124 
  1125 lemma Bfun_inverse:
  1126   fixes a :: "'a::real_normed_div_algebra"
  1127   assumes f: "(f ---> a) F"
  1128   assumes a: "a \<noteq> 0"
  1129   shows "Bfun (\<lambda>x. inverse (f x)) F"
  1130 proof -
  1131   from a have "0 < norm a" by simp
  1132   hence "\<exists>r>0. r < norm a" by (rule dense)
  1133   then obtain r where r1: "0 < r" and r2: "r < norm a" by fast
  1134   have "eventually (\<lambda>x. dist (f x) a < r) F"
  1135     using tendstoD [OF f r1] by fast
  1136   hence "eventually (\<lambda>x. norm (inverse (f x)) \<le> inverse (norm a - r)) F"
  1137   proof eventually_elim
  1138     case (elim x)
  1139     hence 1: "norm (f x - a) < r"
  1140       by (simp add: dist_norm)
  1141     hence 2: "f x \<noteq> 0" using r2 by auto
  1142     hence "norm (inverse (f x)) = inverse (norm (f x))"
  1143       by (rule nonzero_norm_inverse)
  1144     also have "\<dots> \<le> inverse (norm a - r)"
  1145     proof (rule le_imp_inverse_le)
  1146       show "0 < norm a - r" using r2 by simp
  1147     next
  1148       have "norm a - norm (f x) \<le> norm (a - f x)"
  1149         by (rule norm_triangle_ineq2)
  1150       also have "\<dots> = norm (f x - a)"
  1151         by (rule norm_minus_commute)
  1152       also have "\<dots> < r" using 1 .
  1153       finally show "norm a - r \<le> norm (f x)" by simp
  1154     qed
  1155     finally show "norm (inverse (f x)) \<le> inverse (norm a - r)" .
  1156   qed
  1157   thus ?thesis by (rule BfunI)
  1158 qed
  1159 
  1160 lemma tendsto_inverse [tendsto_intros]:
  1161   fixes a :: "'a::real_normed_div_algebra"
  1162   assumes f: "(f ---> a) F"
  1163   assumes a: "a \<noteq> 0"
  1164   shows "((\<lambda>x. inverse (f x)) ---> inverse a) F"
  1165 proof -
  1166   from a have "0 < norm a" by simp
  1167   with f have "eventually (\<lambda>x. dist (f x) a < norm a) F"
  1168     by (rule tendstoD)
  1169   then have "eventually (\<lambda>x. f x \<noteq> 0) F"
  1170     unfolding dist_norm by (auto elim!: eventually_elim1)
  1171   with a have "eventually (\<lambda>x. inverse (f x) - inverse a =
  1172     - (inverse (f x) * (f x - a) * inverse a)) F"
  1173     by (auto elim!: eventually_elim1 simp: inverse_diff_inverse)
  1174   moreover have "Zfun (\<lambda>x. - (inverse (f x) * (f x - a) * inverse a)) F"
  1175     by (intro Zfun_minus Zfun_mult_left
  1176       bounded_bilinear.Bfun_prod_Zfun [OF bounded_bilinear_mult]
  1177       Bfun_inverse [OF f a] f [unfolded tendsto_Zfun_iff])
  1178   ultimately show ?thesis
  1179     unfolding tendsto_Zfun_iff by (rule Zfun_ssubst)
  1180 qed
  1181 
  1182 lemma tendsto_divide [tendsto_intros]:
  1183   fixes a b :: "'a::real_normed_field"
  1184   shows "\<lbrakk>(f ---> a) F; (g ---> b) F; b \<noteq> 0\<rbrakk>
  1185     \<Longrightarrow> ((\<lambda>x. f x / g x) ---> a / b) F"
  1186   by (simp add: tendsto_mult tendsto_inverse divide_inverse)
  1187 
  1188 lemma tendsto_sgn [tendsto_intros]:
  1189   fixes l :: "'a::real_normed_vector"
  1190   shows "\<lbrakk>(f ---> l) F; l \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. sgn (f x)) ---> sgn l) F"
  1191   unfolding sgn_div_norm by (simp add: tendsto_intros)
  1192 
  1193 subsection {* Limits to @{const at_top} and @{const at_bot} *}
  1194 
  1195 lemma filterlim_at_top:
  1196   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)"
  1197   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z < f x) F)"
  1198   by (auto simp: filterlim_iff eventually_at_top_dense elim!: eventually_elim1)
  1199 
  1200 lemma filterlim_at_top_gt:
  1201   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
  1202   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z>c. eventually (\<lambda>x. Z < f x) F)"
  1203   unfolding filterlim_at_top
  1204 proof safe
  1205   fix Z assume *: "\<forall>Z>c. eventually (\<lambda>x. Z < f x) F"
  1206   from gt_ex[of "max Z c"] guess x ..
  1207   with *[THEN spec, of x] show "eventually (\<lambda>x. Z < f x) F"
  1208     by (auto elim!: eventually_elim1)
  1209 qed simp
  1210 
  1211 lemma filterlim_at_bot: 
  1212   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)"
  1213   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. f x < Z) F)"
  1214   by (auto simp: filterlim_iff eventually_at_bot_dense elim!: eventually_elim1)
  1215 
  1216 lemma filterlim_at_bot_lt:
  1217   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
  1218   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z<c. eventually (\<lambda>x. Z > f x) F)"
  1219   unfolding filterlim_at_bot
  1220 proof safe
  1221   fix Z assume *: "\<forall>Z<c. eventually (\<lambda>x. Z > f x) F"
  1222   from lt_ex[of "min Z c"] guess x ..
  1223   with *[THEN spec, of x] show "eventually (\<lambda>x. Z > f x) F"
  1224     by (auto elim!: eventually_elim1)
  1225 qed simp
  1226 
  1227 lemma filterlim_at_infinity:
  1228   fixes f :: "_ \<Rightarrow> 'a\<Colon>real_normed_vector"
  1229   assumes "0 \<le> c"
  1230   shows "(LIM x F. f x :> at_infinity) \<longleftrightarrow> (\<forall>r>c. eventually (\<lambda>x. r \<le> norm (f x)) F)"
  1231   unfolding filterlim_iff eventually_at_infinity
  1232 proof safe
  1233   fix P :: "'a \<Rightarrow> bool" and b
  1234   assume *: "\<forall>r>c. eventually (\<lambda>x. r \<le> norm (f x)) F"
  1235     and P: "\<forall>x. b \<le> norm x \<longrightarrow> P x"
  1236   have "max b (c + 1) > c" by auto
  1237   with * have "eventually (\<lambda>x. max b (c + 1) \<le> norm (f x)) F"
  1238     by auto
  1239   then show "eventually (\<lambda>x. P (f x)) F"
  1240   proof eventually_elim
  1241     fix x assume "max b (c + 1) \<le> norm (f x)"
  1242     with P show "P (f x)" by auto
  1243   qed
  1244 qed force
  1245 
  1246 lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
  1247   unfolding filterlim_at_top
  1248   apply (intro allI)
  1249   apply (rule_tac c="natceiling (Z + 1)" in eventually_sequentiallyI)
  1250   apply (auto simp: natceiling_le_eq)
  1251   done
  1252 
  1253 lemma filterlim_inverse_at_top_pos:
  1254   "LIM x (nhds 0 within {0::real <..}). inverse x :> at_top"
  1255   unfolding filterlim_at_top_gt[where c=0] eventually_within
  1256 proof safe
  1257   fix Z :: real assume [arith]: "0 < Z"
  1258   then have "eventually (\<lambda>x. x < inverse Z) (nhds 0)"
  1259     by (auto simp add: eventually_nhds_metric dist_real_def intro!: exI[of _ "\<bar>inverse Z\<bar>"])
  1260   then show "eventually (\<lambda>x. x \<in> {0<..} \<longrightarrow> Z < inverse x) (nhds 0)"
  1261     by (auto elim!: eventually_elim1 simp: inverse_eq_divide field_simps)
  1262 qed
  1263 
  1264 lemma filterlim_inverse_at_top:
  1265   "(f ---> (0 :: real)) F \<Longrightarrow> eventually (\<lambda>x. 0 < f x) F \<Longrightarrow> LIM x F. inverse (f x) :> at_top"
  1266   by (intro filterlim_compose[OF filterlim_inverse_at_top_pos])
  1267      (simp add: filterlim_def eventually_filtermap le_within_iff)
  1268 
  1269 lemma filterlim_inverse_at_bot_neg:
  1270   "LIM x (nhds 0 within {..< 0::real}). inverse x :> at_bot"
  1271   unfolding filterlim_at_bot_lt[where c=0] eventually_within
  1272 proof safe
  1273   fix Z :: real assume [arith]: "Z < 0"
  1274   have "eventually (\<lambda>x. inverse Z < x) (nhds 0)"
  1275     by (auto simp add: eventually_nhds_metric dist_real_def intro!: exI[of _ "\<bar>inverse Z\<bar>"])
  1276   then show "eventually (\<lambda>x. x \<in> {..< 0} \<longrightarrow> inverse x < Z) (nhds 0)"
  1277     by (auto elim!: eventually_elim1 simp: inverse_eq_divide field_simps)
  1278 qed
  1279 
  1280 lemma filterlim_inverse_at_bot:
  1281   "(f ---> (0 :: real)) F \<Longrightarrow> eventually (\<lambda>x. f x < 0) F \<Longrightarrow> LIM x F. inverse (f x) :> at_bot"
  1282   by (intro filterlim_compose[OF filterlim_inverse_at_bot_neg])
  1283      (simp add: filterlim_def eventually_filtermap le_within_iff)
  1284 
  1285 lemma filterlim_uminus_at_top_at_bot: "LIM x at_bot. - x :: real :> at_top"
  1286   unfolding filterlim_at_top eventually_at_bot_dense
  1287   by (blast intro: less_minus_iff[THEN iffD1])
  1288 
  1289 lemma filterlim_uminus_at_top: "LIM x F. f x :> at_bot \<Longrightarrow> LIM x F. - (f x) :: real :> at_top"
  1290   by (rule filterlim_compose[OF filterlim_uminus_at_top_at_bot])
  1291 
  1292 lemma filterlim_uminus_at_bot_at_top: "LIM x at_top. - x :: real :> at_bot"
  1293   unfolding filterlim_at_bot eventually_at_top_dense
  1294   by (blast intro: minus_less_iff[THEN iffD1])
  1295 
  1296 lemma filterlim_uminus_at_bot: "LIM x F. f x :> at_top \<Longrightarrow> LIM x F. - (f x) :: real :> at_bot"
  1297   by (rule filterlim_compose[OF filterlim_uminus_at_bot_at_top])
  1298 
  1299 lemma tendsto_inverse_0:
  1300   fixes x :: "_ \<Rightarrow> 'a\<Colon>real_normed_div_algebra"
  1301   shows "(inverse ---> (0::'a)) at_infinity"
  1302   unfolding tendsto_Zfun_iff diff_0_right Zfun_def eventually_at_infinity
  1303 proof safe
  1304   fix r :: real assume "0 < r"
  1305   show "\<exists>b. \<forall>x. b \<le> norm x \<longrightarrow> norm (inverse x :: 'a) < r"
  1306   proof (intro exI[of _ "inverse (r / 2)"] allI impI)
  1307     fix x :: 'a
  1308     from `0 < r` have "0 < inverse (r / 2)" by simp
  1309     also assume *: "inverse (r / 2) \<le> norm x"
  1310     finally show "norm (inverse x) < r"
  1311       using * `0 < r` by (subst nonzero_norm_inverse) (simp_all add: inverse_eq_divide field_simps)
  1312   qed
  1313 qed
  1314 
  1315 lemma filterlim_inverse_at_infinity:
  1316   fixes x :: "_ \<Rightarrow> 'a\<Colon>{real_normed_div_algebra, division_ring_inverse_zero}"
  1317   shows "filterlim inverse at_infinity (at (0::'a))"
  1318   unfolding filterlim_at_infinity[OF order_refl]
  1319 proof safe
  1320   fix r :: real assume "0 < r"
  1321   then show "eventually (\<lambda>x::'a. r \<le> norm (inverse x)) (at 0)"
  1322     unfolding eventually_at norm_inverse
  1323     by (intro exI[of _ "inverse r"])
  1324        (auto simp: norm_conv_dist[symmetric] field_simps inverse_eq_divide)
  1325 qed
  1326 
  1327 lemma filterlim_inverse_at_iff:
  1328   fixes g :: "'a \<Rightarrow> 'b\<Colon>{real_normed_div_algebra, division_ring_inverse_zero}"
  1329   shows "(LIM x F. inverse (g x) :> at 0) \<longleftrightarrow> (LIM x F. g x :> at_infinity)"
  1330   unfolding filterlim_def filtermap_filtermap[symmetric]
  1331 proof
  1332   assume "filtermap g F \<le> at_infinity"
  1333   then have "filtermap inverse (filtermap g F) \<le> filtermap inverse at_infinity"
  1334     by (rule filtermap_mono)
  1335   also have "\<dots> \<le> at 0"
  1336     using tendsto_inverse_0
  1337     by (auto intro!: le_withinI exI[of _ 1]
  1338              simp: eventually_filtermap eventually_at_infinity filterlim_def at_def)
  1339   finally show "filtermap inverse (filtermap g F) \<le> at 0" .
  1340 next
  1341   assume "filtermap inverse (filtermap g F) \<le> at 0"
  1342   then have "filtermap inverse (filtermap inverse (filtermap g F)) \<le> filtermap inverse (at 0)"
  1343     by (rule filtermap_mono)
  1344   with filterlim_inverse_at_infinity show "filtermap g F \<le> at_infinity"
  1345     by (auto intro: order_trans simp: filterlim_def filtermap_filtermap)
  1346 qed
  1347 
  1348 text {*
  1349 
  1350 We only show rules for multiplication and addition when the functions are either against a real
  1351 value or against infinity. Further rules are easy to derive by using @{thm filterlim_uminus_at_top}.
  1352 
  1353 *}
  1354 
  1355 lemma filterlim_tendsto_pos_mult_at_top: 
  1356   assumes f: "(f ---> c) F" and c: "0 < c"
  1357   assumes g: "LIM x F. g x :> at_top"
  1358   shows "LIM x F. (f x * g x :: real) :> at_top"
  1359   unfolding filterlim_at_top_gt[where c=0]
  1360 proof safe
  1361   fix Z :: real assume "0 < Z"
  1362   from f `0 < c` have "eventually (\<lambda>x. c / 2 < f x) F"
  1363     by (auto dest!: tendstoD[where e="c / 2"] elim!: eventually_elim1
  1364              simp: dist_real_def abs_real_def split: split_if_asm)
  1365   moreover from g have "eventually (\<lambda>x. (Z / c * 2) < g x) F"
  1366     unfolding filterlim_at_top by auto
  1367   ultimately show "eventually (\<lambda>x. Z < f x * g x) F"
  1368   proof eventually_elim
  1369     fix x assume "c / 2 < f x" "Z / c * 2 < g x"
  1370     with `0 < Z` `0 < c` have "c / 2 * (Z / c * 2) < f x * g x"
  1371       by (intro mult_strict_mono) (auto simp: zero_le_divide_iff)
  1372     with `0 < c` show "Z < f x * g x"
  1373        by simp
  1374   qed
  1375 qed
  1376 
  1377 lemma filterlim_at_top_mult_at_top: 
  1378   assumes f: "LIM x F. f x :> at_top"
  1379   assumes g: "LIM x F. g x :> at_top"
  1380   shows "LIM x F. (f x * g x :: real) :> at_top"
  1381   unfolding filterlim_at_top_gt[where c=0]
  1382 proof safe
  1383   fix Z :: real assume "0 < Z"
  1384   from f have "eventually (\<lambda>x. 1 < f x) F"
  1385     unfolding filterlim_at_top by auto
  1386   moreover from g have "eventually (\<lambda>x. Z < g x) F"
  1387     unfolding filterlim_at_top by auto
  1388   ultimately show "eventually (\<lambda>x. Z < f x * g x) F"
  1389   proof eventually_elim
  1390     fix x assume "1 < f x" "Z < g x"
  1391     with `0 < Z` have "1 * Z < f x * g x"
  1392       by (intro mult_strict_mono) (auto simp: zero_le_divide_iff)
  1393     then show "Z < f x * g x"
  1394        by simp
  1395   qed
  1396 qed
  1397 
  1398 lemma filterlim_tendsto_add_at_top: 
  1399   assumes f: "(f ---> c) F"
  1400   assumes g: "LIM x F. g x :> at_top"
  1401   shows "LIM x F. (f x + g x :: real) :> at_top"
  1402   unfolding filterlim_at_top_gt[where c=0]
  1403 proof safe
  1404   fix Z :: real assume "0 < Z"
  1405   from f have "eventually (\<lambda>x. c - 1 < f x) F"
  1406     by (auto dest!: tendstoD[where e=1] elim!: eventually_elim1 simp: dist_real_def)
  1407   moreover from g have "eventually (\<lambda>x. Z - (c - 1) < g x) F"
  1408     unfolding filterlim_at_top by auto
  1409   ultimately show "eventually (\<lambda>x. Z < f x + g x) F"
  1410     by eventually_elim simp
  1411 qed
  1412 
  1413 lemma filterlim_at_top_add_at_top: 
  1414   assumes f: "LIM x F. f x :> at_top"
  1415   assumes g: "LIM x F. g x :> at_top"
  1416   shows "LIM x F. (f x + g x :: real) :> at_top"
  1417   unfolding filterlim_at_top_gt[where c=0]
  1418 proof safe
  1419   fix Z :: real assume "0 < Z"
  1420   from f have "eventually (\<lambda>x. 0 < f x) F"
  1421     unfolding filterlim_at_top by auto
  1422   moreover from g have "eventually (\<lambda>x. Z < g x) F"
  1423     unfolding filterlim_at_top by auto
  1424   ultimately show "eventually (\<lambda>x. Z < f x + g x) F"
  1425     by eventually_elim simp
  1426 qed
  1427 
  1428 lemma tendsto_divide_0:
  1429   fixes f :: "_ \<Rightarrow> 'a\<Colon>{real_normed_div_algebra, division_ring_inverse_zero}"
  1430   assumes f: "(f ---> c) F"
  1431   assumes g: "LIM x F. g x :> at_infinity"
  1432   shows "((\<lambda>x. f x / g x) ---> 0) F"
  1433   using tendsto_mult[OF f filterlim_compose[OF tendsto_inverse_0 g]] by (simp add: divide_inverse)
  1434 
  1435 lemma linear_plus_1_le_power:
  1436   fixes x :: real
  1437   assumes x: "0 \<le> x"
  1438   shows "real n * x + 1 \<le> (x + 1) ^ n"
  1439 proof (induct n)
  1440   case (Suc n)
  1441   have "real (Suc n) * x + 1 \<le> (x + 1) * (real n * x + 1)"
  1442     by (simp add: field_simps real_of_nat_Suc mult_nonneg_nonneg x)
  1443   also have "\<dots> \<le> (x + 1)^Suc n"
  1444     using Suc x by (simp add: mult_left_mono)
  1445   finally show ?case .
  1446 qed simp
  1447 
  1448 lemma filterlim_realpow_sequentially_gt1:
  1449   fixes x :: "'a :: real_normed_div_algebra"
  1450   assumes x[arith]: "1 < norm x"
  1451   shows "LIM n sequentially. x ^ n :> at_infinity"
  1452 proof (intro filterlim_at_infinity[THEN iffD2] allI impI)
  1453   fix y :: real assume "0 < y"
  1454   have "0 < norm x - 1" by simp
  1455   then obtain N::nat where "y < real N * (norm x - 1)" by (blast dest: reals_Archimedean3)
  1456   also have "\<dots> \<le> real N * (norm x - 1) + 1" by simp
  1457   also have "\<dots> \<le> (norm x - 1 + 1) ^ N" by (rule linear_plus_1_le_power) simp
  1458   also have "\<dots> = norm x ^ N" by simp
  1459   finally have "\<forall>n\<ge>N. y \<le> norm x ^ n"
  1460     by (metis order_less_le_trans power_increasing order_less_imp_le x)
  1461   then show "eventually (\<lambda>n. y \<le> norm (x ^ n)) sequentially"
  1462     unfolding eventually_sequentially
  1463     by (auto simp: norm_power)
  1464 qed simp
  1465 
  1466 subsection {* Relate @{const at}, @{const at_left} and @{const at_right} *}
  1467 
  1468 text {*
  1469 
  1470 This lemmas are useful for conversion between @{term "at x"} to @{term "at_left x"} and
  1471 @{term "at_right x"} and also @{term "at_right 0"}.
  1472 
  1473 *}
  1474 
  1475 lemma at_eq_sup_left_right: "at (x::real) = sup (at_left x) (at_right x)"
  1476   by (auto simp: eventually_within at_def filter_eq_iff eventually_sup 
  1477            elim: eventually_elim2 eventually_elim1)
  1478 
  1479 lemma filterlim_split_at_real:
  1480   "filterlim f F (at_left x) \<Longrightarrow> filterlim f F (at_right x) \<Longrightarrow> filterlim f F (at (x::real))"
  1481   by (subst at_eq_sup_left_right) (rule filterlim_sup)
  1482 
  1483 lemma filtermap_nhds_shift: "filtermap (\<lambda>x. x - d) (nhds a) = nhds (a - d::real)"
  1484   unfolding filter_eq_iff eventually_filtermap eventually_nhds_metric
  1485   by (intro allI ex_cong) (auto simp: dist_real_def field_simps)
  1486 
  1487 lemma filtermap_nhds_minus: "filtermap (\<lambda>x. - x) (nhds a) = nhds (- a::real)"
  1488   unfolding filter_eq_iff eventually_filtermap eventually_nhds_metric
  1489   apply (intro allI ex_cong)
  1490   apply (auto simp: dist_real_def field_simps)
  1491   apply (erule_tac x="-x" in allE)
  1492   apply simp
  1493   done
  1494 
  1495 lemma filtermap_at_shift: "filtermap (\<lambda>x. x - d) (at a) = at (a - d::real)"
  1496   unfolding at_def filtermap_nhds_shift[symmetric]
  1497   by (simp add: filter_eq_iff eventually_filtermap eventually_within)
  1498 
  1499 lemma filtermap_at_right_shift: "filtermap (\<lambda>x. x - d) (at_right a) = at_right (a - d::real)"
  1500   unfolding filtermap_at_shift[symmetric]
  1501   by (simp add: filter_eq_iff eventually_filtermap eventually_within)
  1502 
  1503 lemma at_right_to_0: "at_right (a::real) = filtermap (\<lambda>x. x + a) (at_right 0)"
  1504   using filtermap_at_right_shift[of "-a" 0] by simp
  1505 
  1506 lemma filterlim_at_right_to_0:
  1507   "filterlim f F (at_right (a::real)) \<longleftrightarrow> filterlim (\<lambda>x. f (x + a)) F (at_right 0)"
  1508   unfolding filterlim_def filtermap_filtermap at_right_to_0[of a] ..
  1509 
  1510 lemma eventually_at_right_to_0:
  1511   "eventually P (at_right (a::real)) \<longleftrightarrow> eventually (\<lambda>x. P (x + a)) (at_right 0)"
  1512   unfolding at_right_to_0[of a] by (simp add: eventually_filtermap)
  1513 
  1514 lemma filtermap_at_minus: "filtermap (\<lambda>x. - x) (at a) = at (- a::real)"
  1515   unfolding at_def filtermap_nhds_minus[symmetric]
  1516   by (simp add: filter_eq_iff eventually_filtermap eventually_within)
  1517 
  1518 lemma at_left_minus: "at_left (a::real) = filtermap (\<lambda>x. - x) (at_right (- a))"
  1519   by (simp add: filter_eq_iff eventually_filtermap eventually_within filtermap_at_minus[symmetric])
  1520 
  1521 lemma at_right_minus: "at_right (a::real) = filtermap (\<lambda>x. - x) (at_left (- a))"
  1522   by (simp add: filter_eq_iff eventually_filtermap eventually_within filtermap_at_minus[symmetric])
  1523 
  1524 lemma filterlim_at_left_to_right:
  1525   "filterlim f F (at_left (a::real)) \<longleftrightarrow> filterlim (\<lambda>x. f (- x)) F (at_right (-a))"
  1526   unfolding filterlim_def filtermap_filtermap at_left_minus[of a] ..
  1527 
  1528 lemma eventually_at_left_to_right:
  1529   "eventually P (at_left (a::real)) \<longleftrightarrow> eventually (\<lambda>x. P (- x)) (at_right (-a))"
  1530   unfolding at_left_minus[of a] by (simp add: eventually_filtermap)
  1531 
  1532 lemma filterlim_at_split:
  1533   "filterlim f F (at (x::real)) \<longleftrightarrow> filterlim f F (at_left x) \<and> filterlim f F (at_right x)"
  1534   by (subst at_eq_sup_left_right) (simp add: filterlim_def filtermap_sup)
  1535 
  1536 lemma eventually_at_split:
  1537   "eventually P (at (x::real)) \<longleftrightarrow> eventually P (at_left x) \<and> eventually P (at_right x)"
  1538   by (subst at_eq_sup_left_right) (simp add: eventually_sup)
  1539 
  1540 end
  1541